]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/postprocess.c
For consistency, remove references to vlc from libvlc
[vlc] / modules / codec / ffmpeg / postprocess.c
1 /*****************************************************************************
2  * postprocess.c: video postprocessing using the ffmpeg library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include <vlc/vlc.h>
26 #include <vlc/vout.h>
27 #include <vlc/decoder.h>
28
29 /* ffmpeg header */
30 #ifdef HAVE_FFMPEG_AVCODEC_H
31 #   include <ffmpeg/avcodec.h>
32 #else
33 #   include <avcodec.h>
34 #endif
35
36 #include "ffmpeg.h"
37
38 #ifdef LIBAVCODEC_PP
39
40 #ifdef HAVE_POSTPROC_POSTPROCESS_H
41 #   include <postproc/postprocess.h>
42 #else
43 #   include <libpostproc/postprocess.h>
44 #endif
45
46 #ifndef PP_CPU_CAPS_ALTIVEC
47 #   define PP_CPU_CAPS_ALTIVEC 0
48 #endif
49
50 /*****************************************************************************
51  * video_postproc_sys_t : ffmpeg video postprocessing descriptor
52  *****************************************************************************/
53 typedef struct video_postproc_sys_t
54 {
55     pp_context_t *pp_context;
56     pp_mode_t    *pp_mode;
57
58     vlc_bool_t   *pb_pp;
59
60     int i_width;
61     int i_height;
62
63 } video_postproc_sys_t;
64
65 static int PPQCallback( vlc_object_t *p_this, char const *psz_cmd,
66                         vlc_value_t oldval, vlc_value_t newval, void *p_data );
67
68 /*****************************************************************************
69  * OpenPostproc: probe and open the postproc
70  *****************************************************************************/
71 void *E_(OpenPostproc)( decoder_t *p_dec, vlc_bool_t *pb_pp )
72 {
73     video_postproc_sys_t *p_sys;
74     vlc_value_t val, val_orig, text;
75
76     p_sys = malloc( sizeof(video_postproc_sys_t) );
77     p_sys->pp_context = NULL;
78     p_sys->pp_mode = NULL;
79
80     *pb_pp = VLC_FALSE;
81     p_sys->pb_pp = pb_pp;
82
83     /* Create object variable if not already done */
84     if( var_Type( p_dec, "ffmpeg-pp-q" ) == 0 )
85     {
86         var_Create( p_dec, "ffmpeg-pp-q",
87                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
88         text.psz_string = _("Post processing");
89         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_SETTEXT, &text, NULL );
90
91         var_Get( p_dec, "ffmpeg-pp-q", &val_orig );
92         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_DELCHOICE, &val_orig, NULL );
93
94         val.i_int = 0; text.psz_string = _("Disable");
95         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
96         val.i_int = 1; text.psz_string = _("1 (Lowest)");
97         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
98         val.i_int = 2;
99         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
100         val.i_int = 3;
101         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
102         val.i_int = 4;
103         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
104         val.i_int = 5;
105         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
106         val.i_int = 6; text.psz_string = _("6 (Highest)");
107         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
108         var_AddCallback( p_dec, "ffmpeg-pp-q", PPQCallback, p_sys );
109     }
110
111     /* ***** Load post processing if enabled ***** */
112     var_Get( p_dec, "ffmpeg-pp-q", &val );
113     var_Set( p_dec, "ffmpeg-pp-q", val_orig );
114     if( val_orig.i_int )
115         *pb_pp = VLC_TRUE;
116
117     return p_sys;
118 }
119
120 /*****************************************************************************
121  * InitPostproc: 
122  *****************************************************************************/
123 int E_(InitPostproc)( decoder_t *p_dec, void *p_data,
124                       int i_width, int i_height, int pix_fmt )
125 {
126     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
127     int32_t i_cpu = p_dec->p_libvlc_global->i_cpu;
128     int i_flags = 0;
129
130     /* Set CPU capabilities */
131     if( i_cpu & CPU_CAPABILITY_MMX )
132     {
133         i_flags |= PP_CPU_CAPS_MMX;
134     }
135     if( i_cpu & CPU_CAPABILITY_MMXEXT )
136     {
137         i_flags |= PP_CPU_CAPS_MMX2;
138     }
139     if( i_cpu & CPU_CAPABILITY_3DNOW )
140     {
141         i_flags |= PP_CPU_CAPS_3DNOW;
142     }
143     if( i_cpu & CPU_CAPABILITY_ALTIVEC )
144     {
145         i_flags |= PP_CPU_CAPS_ALTIVEC;
146     }
147
148     switch( pix_fmt )
149     {
150     case PIX_FMT_YUV444P:
151         i_flags |= PP_FORMAT_444;
152         break;
153     case PIX_FMT_YUV422P:
154         i_flags |= PP_FORMAT_422;
155         break;
156     case PIX_FMT_YUV411P:
157         i_flags |= PP_FORMAT_411;
158         break;
159     default:
160         i_flags |= PP_FORMAT_420;
161         break;
162     }
163
164     p_sys->pp_context = pp_get_context( i_width, i_height, i_flags );
165     p_sys->i_width = i_width;
166     p_sys->i_height = i_height;
167
168     return VLC_SUCCESS;
169 }
170
171 /*****************************************************************************
172  * PostprocPict: 
173  *****************************************************************************/
174 int E_(PostprocPict)( decoder_t *p_dec, void *p_data,
175                       picture_t *p_pic, AVFrame *p_ff_pic )
176 {
177     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
178
179     uint8_t *src[3], *dst[3];
180     int i_plane, i_src_stride[3], i_dst_stride[3];
181
182     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
183     {
184         src[i_plane] = p_ff_pic->data[i_plane];
185         dst[i_plane] = p_pic->p[i_plane].p_pixels;
186
187         i_src_stride[i_plane] = p_ff_pic->linesize[i_plane];
188         i_dst_stride[i_plane] = p_pic->p[i_plane].i_pitch;
189     }
190
191     pp_postprocess( src, i_src_stride, dst, i_dst_stride,
192                     p_sys->i_width, p_sys->i_height,
193                     p_ff_pic->qscale_table, p_ff_pic->qstride,
194                     p_sys->pp_mode, p_sys->pp_context,
195                     p_ff_pic->pict_type );
196
197     return VLC_SUCCESS;
198 }
199
200 /*****************************************************************************
201  * ClosePostproc: 
202  *****************************************************************************/
203 void E_(ClosePostproc)( decoder_t *p_dec, void *p_data )
204 {
205     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
206
207     if( p_sys && p_sys->pp_mode )
208     {
209         pp_free_mode( p_sys->pp_mode );
210         if( p_sys->pp_context ) pp_free_context( p_sys->pp_context );
211     }
212
213     var_DelCallback( p_dec, "ffmpeg-pp-q", PPQCallback, p_sys );
214
215     if( p_sys ) free( p_sys );
216 }
217
218 /*****************************************************************************
219  * object variables callbacks: a bunch of object variables are used by the
220  * interfaces to interact with the decoder.
221  *****************************************************************************/
222 static int PPQCallback( vlc_object_t *p_this, char const *psz_cmd,
223                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
224 {
225     decoder_t *p_dec = (decoder_t *)p_this;
226     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
227
228     if( newval.i_int > 0 )
229     {
230         int  i_quality = newval.i_int;
231         char *psz_name = config_GetPsz( p_dec, "ffmpeg-pp-name" );
232         pp_mode_t *pp_mode;
233
234         if( !psz_name )
235         {
236             psz_name = strdup( "default" );
237         }
238         else if( *psz_name == '\0' )
239         {
240             free( psz_name );
241             psz_name = strdup( "default" );
242         }
243
244         pp_mode = pp_get_mode_by_name_and_quality( psz_name, i_quality );
245
246         if( !pp_mode )
247         {
248             msg_Err( p_dec, "failed getting mode for postproc" );
249             newval.i_int = 0;
250         }
251         else
252         {
253             msg_Dbg( p_dec, "postprocessing enabled" );
254         }
255         free( psz_name );
256
257         p_sys->pp_mode = pp_mode;
258     }
259     else
260     {
261         msg_Dbg( p_dec, "postprocessing disabled" );
262     }
263
264     *p_sys->pb_pp = newval.i_int;
265
266     return VLC_SUCCESS;
267 }
268
269 #endif /* LIBAVCODEC_PP */