]> git.sesse.net Git - vlc/blob - modules/video_filter/postproc.c
Revive postprocessing. It is now a video filter (the interface code
[vlc] / modules / video_filter / postproc.c
1 /*****************************************************************************
2  * postproc.c: video postprocessing using libpostproc
3  *****************************************************************************
4  * Copyright (C) 1999-2008 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_vout.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 /* defined by libpostproc */
67 #define NAME_LONGTEXT pp_help
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     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     add_string( FILTER_PREFIX "name", "default", NULL, NAME_TEXT,
89                 NAME_LONGTEXT, true );
90         add_deprecated_alias( "ffmpeg-pp-name" );
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     pp_context_t *pp_context; /* Never changes after init */
103     pp_mode_t    *pp_mode; /* Set to NULL if post processing is disabled */
104
105     vlc_mutex_t lock; /* Lock when using or changing pp_mode */
106 };
107
108
109 /*****************************************************************************
110  * OpenPostproc: probe and open the postproc
111  *****************************************************************************/
112 static int OpenPostproc( vlc_object_t *p_this )
113 {
114     filter_t *p_filter = (filter_t *)p_this;
115     filter_sys_t *p_sys;
116     vlc_value_t val, val_orig, text;
117     unsigned i_cpu = vlc_CPU();
118     int i_flags = 0;
119
120     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma ||
121         p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height ||
122         p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width )
123     {
124         msg_Err( p_filter, "Filter input and output formats must be identical" );
125         return VLC_EGENERIC;
126     }
127
128     /* Set CPU capabilities */
129     if( i_cpu & CPU_CAPABILITY_MMX )
130         i_flags |= PP_CPU_CAPS_MMX;
131     if( i_cpu & CPU_CAPABILITY_MMXEXT )
132         i_flags |= PP_CPU_CAPS_MMX2;
133     if( i_cpu & CPU_CAPABILITY_3DNOW )
134         i_flags |= PP_CPU_CAPS_3DNOW;
135     if( i_cpu & CPU_CAPABILITY_ALTIVEC )
136         i_flags |= PP_CPU_CAPS_ALTIVEC;
137
138     switch( p_filter->fmt_in.video.i_chroma )
139     {
140         case VLC_FOURCC('I','4','4','4'):
141         case VLC_FOURCC('J','4','4','4'):
142             i_flags |= PP_FORMAT_444;
143             break;
144         case VLC_FOURCC('I','4','2','2'):
145         case VLC_FOURCC('J','4','2','2'):
146             i_flags |= PP_FORMAT_422;
147             break;
148         case VLC_FOURCC('I','4','1','1'):
149             i_flags |= PP_FORMAT_411;
150             break;
151         case VLC_FOURCC('I','4','2','0'):
152         case VLC_FOURCC('I','Y','U','V'):
153         case VLC_FOURCC('J','4','2','0'):
154         case VLC_FOURCC('Y','V','1','2'):
155         /* case VLC_FOURCC('Y','U','V','A'): FIXME Should work but alpha plane needs to be copied manually and I'm kind of feeling too lazy to write the code to do that ATM (i_pitch vs i_visible_pitch...). */
156             i_flags |= PP_FORMAT_420;
157             break;
158         default:
159             msg_Err( p_filter, "Unsupported input chroma (%4s)",
160                       (char*)&p_filter->fmt_in.video.i_chroma );
161             return VLC_EGENERIC;
162     }
163
164     p_sys = malloc( sizeof( filter_sys_t ) );
165     if( !p_sys ) return VLC_ENOMEM;
166     p_filter->p_sys = p_sys;
167
168     p_sys->pp_context = pp_get_context( p_filter->fmt_in.video.i_width,
169                                         p_filter->fmt_in.video.i_height,
170                                         i_flags );
171     if( !p_sys->pp_context )
172     {
173         msg_Err( p_filter, "Error while creating post processing context." );
174         free( p_sys );
175         return VLC_EGENERIC;
176     }
177
178     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
179                        p_filter->p_cfg );
180
181     var_Create( p_filter, FILTER_PREFIX "q",
182                 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT |
183                 VLC_VAR_ISCOMMAND );
184     var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_SETISCOMMAND, NULL, NULL ); /* For some obscure reason the VLC_VAR_ISCOMMAND isn't taken into account in during var_Create */
185     var_AddCallback( p_filter, FILTER_PREFIX "q", PPQCallback, NULL );
186     text.psz_string = _("Post processing");
187     var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_SETTEXT, &text, NULL );
188
189     var_Get( p_filter, FILTER_PREFIX "q", &val_orig );
190     var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_DELCHOICE, &val_orig, NULL );
191
192     val.psz_string = var_CreateGetNonEmptyStringCommand(
193                                             p_filter, FILTER_PREFIX "name" );
194     var_AddCallback( p_filter, FILTER_PREFIX "name", PPNameCallback, NULL );
195     if( val_orig.i_int )
196     {
197         p_sys->pp_mode = pp_get_mode_by_name_and_quality( val.psz_string?:
198                                                           "default",
199                                                           val_orig.i_int );
200
201         if( !p_sys->pp_mode )
202         {
203             msg_Err( p_filter, "Error while creating post processing mode." );
204             free( val.psz_string );
205             var_Destroy( p_filter, FILTER_PREFIX "q" );
206             pp_free_context( p_sys->pp_context );
207             free( p_sys );
208             return VLC_EGENERIC;
209         }
210     }
211     else
212     {
213         p_sys->pp_mode = NULL;
214     }
215     free( val.psz_string );
216
217     for( val.i_int = 0; val.i_int <= PP_QUALITY_MAX; val.i_int++ )
218     {
219         switch( val.i_int )
220         {
221             case 0:
222                 text.psz_string = _("Disable");
223                 break;
224             case 1:
225                 text.psz_string = _("Lowest");
226                 break;
227             case PP_QUALITY_MAX:
228                 text.psz_string = _("Highest");
229                 break;
230             default:
231                 text.psz_string = NULL;
232                 break;
233         }
234         var_Change( p_filter, FILTER_PREFIX "q", VLC_VAR_ADDCHOICE,
235                     &val, text.psz_string?&text:NULL );
236     }
237
238     vlc_mutex_init( &p_sys->lock );
239
240     p_filter->pf_video_filter = PostprocPict;
241
242     return VLC_SUCCESS;
243 }
244
245 /*****************************************************************************
246  * ClosePostproc
247  *****************************************************************************/
248 static void ClosePostproc( vlc_object_t *p_this )
249 {
250     filter_t *p_filter = (filter_t *)p_this;
251     filter_sys_t *p_sys = p_filter->p_sys;
252     vlc_mutex_destroy( &p_sys->lock );
253     pp_free_context( p_sys->pp_context );
254     if( p_sys->pp_mode ) pp_free_mode( p_sys->pp_mode );
255     free( p_sys );
256 }
257
258 /*****************************************************************************
259  * PostprocPict
260  *****************************************************************************/
261 static picture_t *PostprocPict( filter_t *p_filter, picture_t *p_pic )
262 {
263     filter_sys_t *p_sys = p_filter->p_sys;
264
265     const uint8_t *src[3];
266     uint8_t *dst[3];
267     int i_plane;
268     int i_src_stride[3], i_dst_stride[3];
269
270     vlc_mutex_lock( &p_sys->lock ); /* Lock to prevent issues if pp_mode is changed */
271     if( !p_sys->pp_mode )
272     {
273         vlc_mutex_unlock( &p_sys->lock );
274         return p_pic;
275     }
276
277     picture_t *p_outpic = p_filter->pf_vout_buffer_new( p_filter );
278     if( !p_outpic )
279     {
280         msg_Warn( p_filter, "can't get output picture" );
281         if( p_pic->pf_release )
282             p_pic->pf_release( p_pic );
283         vlc_mutex_unlock( &p_sys->lock );
284         return NULL;
285     }
286
287     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
288     {
289         src[i_plane] = p_pic->p[i_plane].p_pixels;
290         dst[i_plane] = p_outpic->p[i_plane].p_pixels;
291
292         /* I'm not sure what happens if i_pitch != i_visible_pitch ...
293          * at least it shouldn't crash. */
294         i_src_stride[i_plane] = p_pic->p[i_plane].i_pitch;
295         i_dst_stride[i_plane] = p_outpic->p[i_plane].i_pitch;
296     }
297
298     pp_postprocess( src, i_src_stride, dst, i_dst_stride,
299                     p_filter->fmt_in.video.i_width,
300                     p_filter->fmt_in.video.i_height,
301                     NULL /* FIXME ? works by selecting a default table. But maybe setting our own might help improve post processing quality ... */,
302                     0 /* FIXME */,
303                     p_sys->pp_mode, p_sys->pp_context,
304                     PP_PICT_TYPE_QP2 /* FIXME ? This should be set only for mpeg2 type codecs if I understand correctly. */ );
305     vlc_mutex_unlock( &p_sys->lock );
306
307     return CopyInfoAndRelease( p_outpic, p_pic );
308 }
309
310 /*****************************************************************************
311  *
312  *****************************************************************************/
313 static void PPChangeMode( filter_t *p_filter, const char *psz_name,
314                           int i_quality )
315 {
316     filter_sys_t *p_sys = p_filter->p_sys;
317     vlc_mutex_lock( &p_sys->lock );
318     if( i_quality > 0 )
319     {
320         pp_mode_t *pp_mode = pp_get_mode_by_name_and_quality( psz_name?:
321                                                               "default",
322                                                               i_quality );
323         if( pp_mode )
324         {
325             pp_free_mode( p_sys->pp_mode );
326             p_sys->pp_mode = pp_mode;
327         }
328         else
329             msg_Warn( p_filter, "Error while changing post processing mode. "
330                       "Keeping previous mode." );
331     }
332     else
333     {
334         pp_free_mode( p_sys->pp_mode );
335         p_sys->pp_mode = NULL;
336     }
337     vlc_mutex_unlock( &p_sys->lock );
338 }
339
340 static int PPQCallback( vlc_object_t *p_this, const char *psz_var,
341                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
342 {
343     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
344     filter_t *p_filter = (filter_t *)p_this;
345
346     char *psz_name = var_GetNonEmptyString( p_filter, FILTER_PREFIX "name" );
347     PPChangeMode( p_filter, psz_name, newval.i_int );
348     free( psz_name );
349     return VLC_SUCCESS;
350 }
351
352 static int PPNameCallback( vlc_object_t *p_this, const char *psz_var,
353                            vlc_value_t oldval, vlc_value_t newval, void *p_data )
354 {
355     VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
356     filter_t *p_filter = (filter_t *)p_this;
357
358     int i_quality = var_GetInteger( p_filter, FILTER_PREFIX "q" );
359     PPChangeMode( p_filter, *newval.psz_string?newval.psz_string:NULL, i_quality );
360     return VLC_SUCCESS;
361 }