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