]> git.sesse.net Git - vlc/blob - modules/video_filter/postproc.c
Fix a crash if BEST_AUTOCROP isn't defined (non initialized mutex).
[vlc] / modules / video_filter / postproc.c
1 /*****************************************************************************
2  * postproc.c: video postprocessing using libpostproc
3  *****************************************************************************
4  * Copyright (C) 1999-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *          Antoine Cellerier <dionoea at videolan dot org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_filter.h>
33
34 #include "filter_picture.h"
35
36 #ifdef HAVE_POSTPROC_POSTPROCESS_H
37 #   include <postproc/postprocess.h>
38 #else
39 #   include <libpostproc/postprocess.h>
40 #endif
41
42 #ifndef PP_CPU_CAPS_ALTIVEC
43 #   define PP_CPU_CAPS_ALTIVEC 0
44 #endif
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int OpenPostproc( vlc_object_t * );
50 static void ClosePostproc( vlc_object_t * );
51
52 static picture_t *PostprocPict( filter_t *, picture_t * );
53
54 static int PPQCallback( vlc_object_t *, char const *,
55                         vlc_value_t, vlc_value_t, void * );
56 static int PPNameCallback( vlc_object_t *, char const *,
57                            vlc_value_t, vlc_value_t, void * );
58
59 #define Q_TEXT N_("Post processing quality")
60 #define Q_LONGTEXT N_( \
61     "Quality of post processing. Valid range is 0 to 6\n" \
62     "Higher levels require considerable more CPU power, but produce " \
63     "better looking pictures." )
64
65 #define NAME_TEXT N_("FFmpeg post processing filter chains")
66 #define NAME_LONGTEXT NAME_TEXT
67
68 #define FILTER_PREFIX "postproc-"
69
70 /*****************************************************************************
71  * Module descriptor
72  *****************************************************************************/
73 vlc_module_begin ()
74     set_description( N_("Video post processing filter") )
75     set_shortname( N_("Postproc" ) )
76     add_shortcut( "postprocess" ) /* name is "postproc" */
77     add_shortcut( "pp" )
78     set_category( CAT_VIDEO )
79     set_subcategory( SUBCAT_VIDEO_VFILTER )
80
81     set_capability( "video filter2", 0 )
82
83     set_callbacks( OpenPostproc, ClosePostproc )
84
85     add_integer_with_range( FILTER_PREFIX "q", PP_QUALITY_MAX, 0,
86                             PP_QUALITY_MAX, NULL, Q_TEXT, Q_LONGTEXT, false )
87         add_deprecated_alias( "ffmpeg-pp-q" )
88         change_safe()
89     add_string( FILTER_PREFIX "name", "default", NULL, NAME_TEXT,
90                 NAME_LONGTEXT, true )
91         add_deprecated_alias( "ffmpeg-pp-name" )
92 vlc_module_end ()
93
94 static const char *const ppsz_filter_options[] = {
95     "q", "name", NULL
96 };
97
98 /*****************************************************************************
99  * filter_sys_t : libpostproc video postprocessing descriptor
100  *****************************************************************************/
101 struct filter_sys_t
102 {
103     /* Never changes after init */
104     pp_context_t *pp_context;
105
106     /* Set to NULL if post processing is disabled */
107     pp_mode_t    *pp_mode;
108
109     /* Set to true if previous pic had a quant matrix
110        (used to prevent spamming warning messages) */
111     bool b_had_matrix;
112
113     /* Lock when using or changing pp_mode */
114     vlc_mutex_t lock;
115 };
116
117
118 /*****************************************************************************
119  * OpenPostproc: probe and open the postproc
120  *****************************************************************************/
121 static int OpenPostproc( vlc_object_t *p_this )
122 {
123     filter_t *p_filter = (filter_t *)p_this;
124     filter_sys_t *p_sys;
125     vlc_value_t val, val_orig, text;
126     unsigned i_cpu = vlc_CPU();
127     int i_flags = 0;
128
129     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma ||
130         p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height ||
131         p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width )
132     {
133         msg_Err( p_filter, "Filter input and output formats must be identical" );
134         return VLC_EGENERIC;
135     }
136
137     /* Set CPU capabilities */
138     if( i_cpu & CPU_CAPABILITY_MMX )
139         i_flags |= PP_CPU_CAPS_MMX;
140     if( i_cpu & CPU_CAPABILITY_MMXEXT )
141         i_flags |= PP_CPU_CAPS_MMX2;
142     if( i_cpu & CPU_CAPABILITY_3DNOW )
143         i_flags |= PP_CPU_CAPS_3DNOW;
144     if( i_cpu & CPU_CAPABILITY_ALTIVEC )
145         i_flags |= PP_CPU_CAPS_ALTIVEC;
146
147     switch( p_filter->fmt_in.video.i_chroma )
148     {
149         case VLC_CODEC_I444:
150         case VLC_CODEC_J444:
151         /* case VLC_CODEC_YUVA:
152            FIXME: Should work but alpha plane needs to be copied manually and
153                   I'm kind of feeling too lazy to write the code to do that ATM
154                   (i_pitch vs i_visible_pitch...). */
155             i_flags |= PP_FORMAT_444;
156             break;
157         case VLC_CODEC_I422:
158         case VLC_CODEC_J422:
159             i_flags |= PP_FORMAT_422;
160             break;
161         case VLC_CODEC_I411:
162             i_flags |= PP_FORMAT_411;
163             break;
164         case VLC_CODEC_I420:
165         case VLC_CODEC_J420:
166         case VLC_CODEC_YV12:
167             i_flags |= PP_FORMAT_420;
168             break;
169         default:
170             msg_Err( p_filter, "Unsupported input chroma (%4s)",
171                       (char*)&p_filter->fmt_in.video.i_chroma );
172             return VLC_EGENERIC;
173     }
174
175     p_sys = malloc( sizeof( filter_sys_t ) );
176     if( !p_sys ) return VLC_ENOMEM;
177     p_filter->p_sys = p_sys;
178
179     p_sys->pp_context = pp_get_context( p_filter->fmt_in.video.i_width,
180                                         p_filter->fmt_in.video.i_height,
181                                         i_flags );
182     if( !p_sys->pp_context )
183     {
184         msg_Err( p_filter, "Error while creating post processing context." );
185         free( p_sys );
186         return VLC_EGENERIC;
187     }
188
189     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
190                        p_filter->p_cfg );
191
192     var_Create( p_filter, FILTER_PREFIX "q",
193                 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT |
194                 VLC_VAR_ISCOMMAND );
195     /* For some obscure reason the VLC_VAR_ISCOMMAND isn't taken into account
196        in during var_Create */
197     var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_SETISCOMMAND, NULL, NULL );
198
199     text.psz_string = _("Post processing");
200     var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_SETTEXT, &text, NULL );
201
202     var_Get( p_filter, FILTER_PREFIX "q", &val_orig );
203     var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_DELCHOICE, &val_orig, NULL );
204
205     val.psz_string = var_GetNonEmptyString( p_filter, FILTER_PREFIX "name" );
206     if( val_orig.i_int )
207     {
208         p_sys->pp_mode = pp_get_mode_by_name_and_quality( val.psz_string ?
209                                                           val.psz_string :
210                                                           "default",
211                                                           val_orig.i_int );
212
213         if( !p_sys->pp_mode )
214         {
215             msg_Err( p_filter, "Error while creating post processing mode." );
216             free( val.psz_string );
217             var_Destroy( p_filter, FILTER_PREFIX "q" );
218             pp_free_context( p_sys->pp_context );
219             free( p_sys );
220             return VLC_EGENERIC;
221         }
222     }
223     else
224     {
225         p_sys->pp_mode = NULL;
226     }
227     free( val.psz_string );
228
229     for( val.i_int = 0; val.i_int <= PP_QUALITY_MAX; val.i_int++ )
230     {
231         switch( val.i_int )
232         {
233             case 0:
234                 text.psz_string = _("Disable");
235                 break;
236             case 1:
237                 text.psz_string = _("Lowest");
238                 break;
239             case PP_QUALITY_MAX:
240                 text.psz_string = _("Highest");
241                 break;
242             default:
243                 text.psz_string = NULL;
244                 break;
245         }
246         var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_ADDCHOICE,
247                     &val, text.psz_string?&text:NULL );
248     }
249
250     vlc_mutex_init( &p_sys->lock );
251
252     /* Add the callback at the end to prevent crashes */
253     var_AddCallback( p_filter, FILTER_PREFIX "q", PPQCallback, NULL );
254     var_AddCallback( p_filter, FILTER_PREFIX "name", PPNameCallback, NULL );
255
256     p_filter->pf_video_filter = PostprocPict;
257     p_sys->b_had_matrix = true;
258
259     return VLC_SUCCESS;
260 }
261
262 /*****************************************************************************
263  * ClosePostproc
264  *****************************************************************************/
265 static void ClosePostproc( vlc_object_t *p_this )
266 {
267     filter_t *p_filter = (filter_t *)p_this;
268     filter_sys_t *p_sys = p_filter->p_sys;
269
270     /* delete the callback before destroying the mutex */
271     var_DelCallback( p_filter, FILTER_PREFIX "q", PPQCallback, NULL );
272     var_DelCallback( p_filter, FILTER_PREFIX "name", PPNameCallback, NULL );
273
274     /* Destroy the resources */
275     vlc_mutex_destroy( &p_sys->lock );
276     pp_free_context( p_sys->pp_context );
277     if( p_sys->pp_mode ) pp_free_mode( p_sys->pp_mode );
278     free( p_sys );
279 }
280
281 /*****************************************************************************
282  * PostprocPict
283  *****************************************************************************/
284 static picture_t *PostprocPict( filter_t *p_filter, picture_t *p_pic )
285 {
286     filter_sys_t *p_sys = p_filter->p_sys;
287
288     const uint8_t *src[3];
289     uint8_t *dst[3];
290     int i_plane;
291     int i_src_stride[3], i_dst_stride[3];
292
293     /* Lock to prevent issues if pp_mode is changed */
294     vlc_mutex_lock( &p_sys->lock );
295     if( !p_sys->pp_mode )
296     {
297         vlc_mutex_unlock( &p_sys->lock );
298         return p_pic;
299     }
300
301     picture_t *p_outpic = filter_NewPicture( p_filter );
302     if( !p_outpic )
303     {
304         picture_Release( p_pic );
305         vlc_mutex_unlock( &p_sys->lock );
306         return NULL;
307     }
308
309     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
310     {
311         src[i_plane] = p_pic->p[i_plane].p_pixels;
312         dst[i_plane] = p_outpic->p[i_plane].p_pixels;
313
314         /* I'm not sure what happens if i_pitch != i_visible_pitch ...
315          * at least it shouldn't crash. */
316         i_src_stride[i_plane] = p_pic->p[i_plane].i_pitch;
317         i_dst_stride[i_plane] = p_outpic->p[i_plane].i_pitch;
318     }
319
320     if( !p_pic->p_q && p_sys->b_had_matrix )
321     {
322         msg_Warn( p_filter, "Quantification table was not set by video decoder. Postprocessing won't look good." );
323         p_sys->b_had_matrix = false;
324     }
325     else if( p_pic->p_q )
326     {
327         p_sys->b_had_matrix = true;
328     }
329
330     pp_postprocess( src, i_src_stride, dst, i_dst_stride,
331                     p_filter->fmt_in.video.i_width,
332                     p_filter->fmt_in.video.i_height,
333                     p_pic->p_q, p_pic->i_qstride,
334                     p_sys->pp_mode, p_sys->pp_context,
335                     p_pic->i_qtype == QTYPE_MPEG2 ? PP_PICT_TYPE_QP2 : 0 );
336     vlc_mutex_unlock( &p_sys->lock );
337
338     return CopyInfoAndRelease( p_outpic, p_pic );
339 }
340
341 /*****************************************************************************
342  * PPChangeMode: change the current mode and quality
343  *****************************************************************************/
344 static void PPChangeMode( filter_t *p_filter, const char *psz_name,
345                           int i_quality )
346 {
347     filter_sys_t *p_sys = p_filter->p_sys;
348     vlc_mutex_lock( &p_sys->lock );
349     if( i_quality > 0 )
350     {
351         pp_mode_t *pp_mode = pp_get_mode_by_name_and_quality( psz_name ?
352                                                               psz_name :
353                                                               "default",
354                                                               i_quality );
355         if( pp_mode )
356         {
357             pp_free_mode( p_sys->pp_mode );
358             p_sys->pp_mode = pp_mode;
359         }
360         else
361             msg_Warn( p_filter, "Error while changing post processing mode. "
362                       "Keeping previous mode." );
363     }
364     else
365     {
366         pp_free_mode( p_sys->pp_mode );
367         p_sys->pp_mode = NULL;
368     }
369     vlc_mutex_unlock( &p_sys->lock );
370 }
371
372 static int PPQCallback( vlc_object_t *p_this, const char *psz_var,
373                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
374 {
375     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
376     filter_t *p_filter = (filter_t *)p_this;
377
378     char *psz_name = var_GetNonEmptyString( p_filter, FILTER_PREFIX "name" );
379     PPChangeMode( p_filter, psz_name, newval.i_int );
380     free( psz_name );
381     return VLC_SUCCESS;
382 }
383
384 static int PPNameCallback( vlc_object_t *p_this, const char *psz_var,
385                            vlc_value_t oldval, vlc_value_t newval, void *p_data )
386 {
387     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
388     filter_t *p_filter = (filter_t *)p_this;
389
390     int i_quality = var_GetInteger( p_filter, FILTER_PREFIX "q" );
391     PPChangeMode( p_filter, *newval.psz_string ? newval.psz_string : NULL,
392                   i_quality );
393     return VLC_SUCCESS;
394 }