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