]> git.sesse.net Git - vlc/blob - modules/codec/ffmpeg/postprocess.c
293515ec6dd344dfd1037b126a6edb80cb5c18a1
[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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc/vlc.h>
30 #include <vlc_vout.h>
31 #include <vlc_codec.h>
32
33 /* ffmpeg header */
34 #ifdef HAVE_LIBAVCODEC_AVCODEC_H
35 #   include <libavcodec/avcodec.h>
36 #else
37 #   include <avcodec.h>
38 #endif
39
40 #include "ffmpeg.h"
41
42 #ifdef HAVE_POSTPROC_POSTPROCESS_H
43 #   include <postproc/postprocess.h>
44 #else
45 #   include <libpostproc/postprocess.h>
46 #endif
47
48 #ifndef PP_CPU_CAPS_ALTIVEC
49 #   define PP_CPU_CAPS_ALTIVEC 0
50 #endif
51
52 /*****************************************************************************
53  * video_postproc_sys_t : ffmpeg video postprocessing descriptor
54  *****************************************************************************/
55 typedef struct video_postproc_sys_t
56 {
57     pp_context_t *pp_context;
58     pp_mode_t    *pp_mode;
59
60     vlc_bool_t   *pb_pp;
61
62     int i_width;
63     int i_height;
64
65 } video_postproc_sys_t;
66
67 static int PPQCallback( vlc_object_t *p_this, char const *psz_cmd,
68                         vlc_value_t oldval, vlc_value_t newval, void *p_data );
69
70 /*****************************************************************************
71  * OpenPostproc: probe and open the postproc
72  *****************************************************************************/
73 void *E_(OpenPostproc)( decoder_t *p_dec, vlc_bool_t *pb_pp )
74 {
75     video_postproc_sys_t *p_sys;
76     vlc_value_t val, val_orig, text;
77
78     p_sys = malloc( sizeof(video_postproc_sys_t) );
79     p_sys->pp_context = NULL;
80     p_sys->pp_mode = NULL;
81
82     *pb_pp = VLC_FALSE;
83     p_sys->pb_pp = pb_pp;
84
85     /* Create object variable if not already done */
86     if( var_Type( p_dec, "ffmpeg-pp-q" ) == 0 )
87     {
88         var_Create( p_dec, "ffmpeg-pp-q",
89                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
90         text.psz_string = _("Post processing");
91         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_SETTEXT, &text, NULL );
92
93         var_Get( p_dec, "ffmpeg-pp-q", &val_orig );
94         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_DELCHOICE, &val_orig, NULL );
95
96         val.i_int = 0; text.psz_string = _("Disable");
97         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
98         val.i_int = 1; text.psz_string = _("1 (Lowest)");
99         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
100         val.i_int = 2;
101         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
102         val.i_int = 3;
103         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
104         val.i_int = 4;
105         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
106         val.i_int = 5;
107         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, NULL );
108         val.i_int = 6; text.psz_string = _("6 (Highest)");
109         var_Change( p_dec, "ffmpeg-pp-q", VLC_VAR_ADDCHOICE, &val, &text );
110         var_AddCallback( p_dec, "ffmpeg-pp-q", PPQCallback, p_sys );
111     }
112
113     /* ***** Load post processing if enabled ***** */
114     var_Get( p_dec, "ffmpeg-pp-q", &val );
115     var_Set( p_dec, "ffmpeg-pp-q", val_orig );
116     if( val_orig.i_int )
117         *pb_pp = VLC_TRUE;
118
119     return p_sys;
120 }
121
122 /*****************************************************************************
123  * InitPostproc:
124  *****************************************************************************/
125 int E_(InitPostproc)( void *p_data, int i_width, int i_height, int pix_fmt )
126 {
127     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
128     unsigned i_cpu = vlc_CPU();
129     int i_flags = 0;
130
131     /* Set CPU capabilities */
132     if( i_cpu & CPU_CAPABILITY_MMX )
133     {
134         i_flags |= PP_CPU_CAPS_MMX;
135     }
136     if( i_cpu & CPU_CAPABILITY_MMXEXT )
137     {
138         i_flags |= PP_CPU_CAPS_MMX2;
139     }
140     if( i_cpu & CPU_CAPABILITY_3DNOW )
141     {
142         i_flags |= PP_CPU_CAPS_3DNOW;
143     }
144     if( i_cpu & CPU_CAPABILITY_ALTIVEC )
145     {
146         i_flags |= PP_CPU_CAPS_ALTIVEC;
147     }
148
149     switch( pix_fmt )
150     {
151     case PIX_FMT_YUV444P:
152         i_flags |= PP_FORMAT_444;
153         break;
154     case PIX_FMT_YUV422P:
155         i_flags |= PP_FORMAT_422;
156         break;
157     case PIX_FMT_YUV411P:
158         i_flags |= PP_FORMAT_411;
159         break;
160     default:
161         i_flags |= PP_FORMAT_420;
162         break;
163     }
164
165     p_sys->pp_context = pp_get_context( i_width, i_height, i_flags );
166     p_sys->i_width = i_width;
167     p_sys->i_height = i_height;
168
169     return VLC_SUCCESS;
170 }
171
172 /*****************************************************************************
173  * PostprocPict:
174  *****************************************************************************/
175 int E_(PostprocPict)( void *p_data, 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     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
226     decoder_t *p_dec = (decoder_t *)p_this;
227     video_postproc_sys_t *p_sys = (video_postproc_sys_t *)p_data;
228
229     if( newval.i_int > 0 )
230     {
231         int  i_quality = newval.i_int;
232         char *psz_name = config_GetPsz( p_dec, "ffmpeg-pp-name" );
233         pp_mode_t *pp_mode;
234
235         if( !psz_name )
236         {
237             psz_name = strdup( "default" );
238         }
239         else if( *psz_name == '\0' )
240         {
241             free( psz_name );
242             psz_name = strdup( "default" );
243         }
244
245         pp_mode = pp_get_mode_by_name_and_quality( psz_name, i_quality );
246
247         if( !pp_mode )
248         {
249             msg_Err( p_dec, "failed getting mode for postproc" );
250             newval.i_int = 0;
251         }
252         else
253         {
254             msg_Dbg( p_dec, "postprocessing enabled" );
255         }
256         free( psz_name );
257
258         p_sys->pp_mode = pp_mode;
259     }
260     else
261     {
262         msg_Dbg( p_dec, "postprocessing disabled" );
263     }
264
265     *p_sys->pb_pp = newval.i_int;
266
267     return VLC_SUCCESS;
268 }