]> git.sesse.net Git - vlc/blob - modules/video_filter/canvas.c
decoder: fix data race in input_DecoderChangePause()
[vlc] / modules / video_filter / canvas.c
1 /*****************************************************************************
2  * canvas.c : automatically resize and padd a video to fit in canvas
3  *****************************************************************************
4  * Copyright (C) 2008 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <limits.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_filter.h>
37
38 /*****************************************************************************
39  * Local and extern prototypes.
40  *****************************************************************************/
41 static int  Activate( vlc_object_t * );
42 static void Destroy( vlc_object_t * );
43 static picture_t *Filter( filter_t *, picture_t * );
44
45 /* This module effectively implements a form of picture-in-picture.
46  *  - The outer picture is called the canvas.
47  *  - The innter picture is callsed the subpicture.
48  *
49  * NB, all of the following operatons take into account aspect ratio
50  *
51  * A canvas is of canvas_{width,height}.
52  * In Pad mode:
53  *  - The subpicture is upconverted with a inverse scalefactor of:
54  *     (The size of subpicture's largest dimension)
55  *     --------------------------------------------
56  *     (The size of canvas's equivalent dimension)
57  *
58  *   Ie, The subpicture's largest dimension is made equal to the
59  *   equivalent canvas dimension.
60  *
61  *  - The upconverted subpicture's smallest dimension is then padded
62  *    to make the upconverted subpicture have the same dimensions of
63  *    the canvas.
64  *
65  * In Crop mode:
66  *  - The subpicture is upconverted with an inverse scalefactor of:
67  *     (The size of subpicture's smallest dimension)
68  *     --------------------------------------------
69  *     (The size of canvas's equivalent dimension)
70  *
71  *   Ie, The subpicture's smallest dimension is made equal to the
72  *   equivalent canvas dimension. (The subpicture will then be
73  *   larger than the canvas)
74  *
75  *  - The upconverted subpicture's largest dimension is then cropped
76  *    to make the upconverted subpicture have the same dimensions of
77  *    the canvas.
78  */
79
80 /* NB, use of `padd' in this module is a 16-17th Century spelling of `pad' */
81
82 #define WIDTH_TEXT N_( "Output width" )
83 #define WIDTH_LONGTEXT N_( \
84     "Output (canvas) image width" )
85 #define HEIGHT_TEXT N_( "Output height" )
86 #define HEIGHT_LONGTEXT N_( \
87     "Output (canvas) image height" )
88 #define ASPECT_TEXT N_( "Output picture aspect ratio" )
89 #define ASPECT_LONGTEXT N_( \
90     "Set the canvas' picture aspect ratio. " \
91     "If omitted, the canvas is assumed to have the same SAR as the input." )
92 #define PADD_TEXT N_( "Pad video" )
93 #define PADD_LONGTEXT N_( \
94     "If enabled, video will be padded to fit in canvas after scaling. " \
95     "Otherwise, video will be cropped to fix in canvas after scaling." )
96 #define CANVAS_HELP N_( "Automatically resize and pad a video" )
97
98 #define CFG_PREFIX "canvas-"
99
100 /*****************************************************************************
101  * Module descriptor
102  *****************************************************************************/
103 vlc_module_begin ()
104     set_shortname( N_("Canvas") )
105     set_description( N_("Canvas video filter") )
106     set_capability( "video filter2", 0 )
107     set_help( CANVAS_HELP )
108     set_callbacks( Activate, Destroy )
109
110     set_category( CAT_VIDEO )
111     set_subcategory( SUBCAT_VIDEO_VFILTER )
112
113     add_integer_with_range( CFG_PREFIX "width", 0, 0, INT_MAX,
114                             WIDTH_TEXT, WIDTH_LONGTEXT, false )
115     add_integer_with_range( CFG_PREFIX "height", 0, 0, INT_MAX,
116                             HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
117
118     add_string( CFG_PREFIX "aspect", NULL,
119                 ASPECT_TEXT, ASPECT_LONGTEXT, false )
120
121     add_bool( CFG_PREFIX "padd", true,
122               PADD_TEXT, PADD_LONGTEXT, false )
123 vlc_module_end ()
124
125 static const char *const ppsz_filter_options[] = {
126     "width", "height", "aspect", "padd", NULL
127 };
128
129 struct filter_sys_t
130 {
131     filter_chain_t *p_chain;
132 };
133
134 static picture_t *video_new( filter_t *p_filter )
135 {
136     return filter_NewPicture( p_filter->owner.sys );
137 }
138
139 /*****************************************************************************
140  *
141  *****************************************************************************/
142 static int Activate( vlc_object_t *p_this )
143 {
144     filter_t *p_filter = (filter_t *)p_this;
145     unsigned i_canvas_width; /* visible width of output canvas */
146     unsigned i_canvas_height; /* visible height of output canvas */
147     unsigned i_canvas_aspect; /* canvas PictureAspectRatio */
148     es_format_t fmt; /* target format after up/down conversion */
149     char psz_croppadd[100];
150     int i_padd,i_offset;
151     char *psz_aspect, *psz_parser;
152     bool b_padd;
153     unsigned i_fmt_in_aspect;
154
155     if( !p_filter->b_allow_fmt_out_change )
156     {
157         msg_Err( p_filter, "Picture format change isn't allowed" );
158         return VLC_EGENERIC;
159     }
160
161     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
162     {
163         msg_Err( p_filter, "Input and output chromas don't match" );
164         return VLC_EGENERIC;
165     }
166
167     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
168                        p_filter->p_cfg );
169
170     i_canvas_width = var_CreateGetInteger( p_filter, CFG_PREFIX "width" );
171     i_canvas_height = var_CreateGetInteger( p_filter, CFG_PREFIX "height" );
172
173     if( i_canvas_width == 0 || i_canvas_height == 0 )
174     {
175         msg_Err( p_filter, "Width and height options must be set" );
176         return VLC_EGENERIC;
177     }
178
179     if( i_canvas_width & 1 || i_canvas_height & 1 )
180     {
181         /* If this restriction were ever relaxed, it is very important to
182          * get the field polatiry correct */
183         msg_Err( p_filter, "Width and height options must be even integers" );
184         return VLC_EGENERIC;
185     }
186
187     if( p_filter->fmt_in.video.i_sar_num )
188         i_fmt_in_aspect = (int64_t)p_filter->fmt_in.video.i_sar_num *
189                       p_filter->fmt_in.video.i_visible_width *
190                       VOUT_ASPECT_FACTOR /
191                       p_filter->fmt_in.video.i_sar_den /
192                       p_filter->fmt_in.video.i_visible_height;
193     else
194         i_fmt_in_aspect = (int64_t)p_filter->fmt_in.video.i_visible_width *
195                       VOUT_ASPECT_FACTOR /
196                       p_filter->fmt_in.video.i_visible_height;
197
198     psz_aspect = var_CreateGetNonEmptyString( p_filter, CFG_PREFIX "aspect" );
199     if( psz_aspect )
200     {
201         psz_parser = strchr( psz_aspect, ':' );
202         int numerator = atoi( psz_aspect );
203         int denominator = psz_parser ? atoi( psz_parser+1 ) : 0;
204         denominator = denominator == 0 ? 1 : denominator;
205         i_canvas_aspect = numerator * VOUT_ASPECT_FACTOR / denominator;
206         free( psz_aspect );
207
208         if( numerator <= 0 || denominator < 0 )
209         {
210             msg_Err( p_filter, "Aspect ratio must be strictly positive" );
211             return VLC_EGENERIC;
212         }
213     }
214     else
215     {
216         /* if there is no user supplied aspect ratio, assume the canvas
217          * has the same sample aspect ratio as the subpicture */
218         /* aspect = subpic_sar * canvas_width / canvas_height
219          *  where subpic_sar = subpic_ph * subpic_par / subpic_pw */
220         i_canvas_aspect = (uint64_t) p_filter->fmt_in.video.i_visible_height
221                         * i_fmt_in_aspect
222                         * i_canvas_width
223                         / (i_canvas_height * p_filter->fmt_in.video.i_visible_width);
224     }
225
226     b_padd = var_CreateGetBool( p_filter, CFG_PREFIX "padd" );
227
228     filter_sys_t *p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
229     if( !p_sys )
230         return VLC_ENOMEM;
231     p_filter->p_sys = p_sys;
232
233     filter_owner_t owner = {
234         .sys = p_filter,
235         .video = {
236             .buffer_new = video_new,
237         },
238     };
239
240     p_sys->p_chain = filter_chain_NewVideo( p_filter, true, &owner );
241     if( !p_sys->p_chain )
242     {
243         msg_Err( p_filter, "Could not allocate filter chain" );
244         free( p_sys );
245         return VLC_EGENERIC;
246     }
247
248     es_format_Copy( &fmt, &p_filter->fmt_in );
249
250     /* one dimension will end up with one of the following: */
251     fmt.video.i_visible_width = i_canvas_width;
252     fmt.video.i_visible_height = i_canvas_height;
253
254     if( b_padd )
255     {
256         /* Padd */
257         if( i_canvas_aspect > i_fmt_in_aspect )
258         {
259             /* The canvas has a wider aspect than the subpicture:
260              *  ie, pillarbox the [scaled] subpicture */
261             /* The following is derived form:
262              * width = upconverted_subpic_height * subpic_par / canvas_sar
263              *  where canvas_sar = canvas_width / (canvas_height * canvas_par)
264              * then simplify */
265             fmt.video.i_visible_width = i_canvas_width
266                               * i_fmt_in_aspect
267                               / i_canvas_aspect;
268             if( fmt.video.i_visible_width & 1 ) fmt.video.i_visible_width -= 1;
269
270             i_padd = (i_canvas_width - fmt.video.i_visible_width) / 2;
271             i_offset = (i_padd & 1);
272             snprintf( psz_croppadd, 100, "croppadd{paddleft=%d,paddright=%d}",
273                       i_padd - i_offset, i_padd + i_offset );
274         }
275         else
276         {
277             /* The canvas has a taller aspect than the subpicture:
278              *  ie, letterbox the [scaled] subpicture */
279             fmt.video.i_visible_height = i_canvas_height
280                                * i_canvas_aspect
281                                / i_fmt_in_aspect;
282             if( fmt.video.i_visible_height & 1 ) fmt.video.i_visible_height -= 1;
283
284             i_padd = (i_canvas_height - fmt.video.i_visible_height ) / 2;
285             i_offset = (i_padd & 1);
286             snprintf( psz_croppadd, 100, "croppadd{paddtop=%d,paddbottom=%d}",
287                       i_padd - i_offset, i_padd + i_offset );
288         }
289     }
290     else
291     {
292         /* Crop */
293         if( i_canvas_aspect < i_fmt_in_aspect )
294         {
295             /* The canvas has a narrower aspect than the subpicture:
296              *  ie, crop the [scaled] subpicture horizontally */
297             fmt.video.i_visible_width = i_canvas_width
298                               * i_fmt_in_aspect
299                               / i_canvas_aspect;
300             if( fmt.video.i_visible_width & 1 ) fmt.video.i_visible_width -= 1;
301
302             i_padd = (fmt.video.i_visible_width - i_canvas_width) / 2;
303             i_offset = (i_padd & 1);
304             snprintf( psz_croppadd, 100, "croppadd{cropleft=%d,cropright=%d}",
305                       i_padd - i_offset, i_padd + i_offset );
306         }
307         else
308         {
309             /* The canvas has a shorter aspect than the subpicture:
310              *  ie, crop the [scaled] subpicture vertically */
311             fmt.video.i_visible_height = i_canvas_height
312                                * i_canvas_aspect
313                                / i_fmt_in_aspect;
314             if( fmt.video.i_visible_height & 1 ) fmt.video.i_visible_height -= 1;
315
316             i_padd = (fmt.video.i_visible_height - i_canvas_height) / 2;
317             i_offset = (i_padd & 1);
318             snprintf( psz_croppadd, 100, "croppadd{croptop=%d,cropbottom=%d}",
319                       i_padd - i_offset, i_padd + i_offset );
320         }
321     }
322
323     /* xxx, should the clean area include the letter-boxing?
324      *  probably not, as some codecs can make use of that information
325      *  and it should be a scaled version of the input clean area
326      *   -- davidf */
327     fmt.video.i_width = p_filter->fmt_in.video.i_width * fmt.video.i_visible_width / p_filter->fmt_in.video.i_visible_width;
328     fmt.video.i_height = p_filter->fmt_in.video.i_height * fmt.video.i_visible_height / p_filter->fmt_in.video.i_visible_height;
329
330     filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &fmt );
331     /* Append scaling module */
332     filter_chain_AppendFilter( p_sys->p_chain, NULL, NULL, NULL, NULL );
333     /* Append croppadd module if we actually do cropping or padding instead of just scaling*/
334     if( i_padd > 0 )
335         filter_chain_AppendFromString( p_sys->p_chain, psz_croppadd );
336
337     fmt = *filter_chain_GetFmtOut( p_sys->p_chain );
338     es_format_Copy( &p_filter->fmt_out, &fmt );
339
340     vlc_ureduce( &p_filter->fmt_out.video.i_sar_num,
341         &p_filter->fmt_out.video.i_sar_den,
342         i_canvas_aspect    * p_filter->fmt_out.video.i_visible_height,
343         VOUT_ASPECT_FACTOR * p_filter->fmt_out.video.i_visible_width,
344         0);
345
346     if( p_filter->fmt_out.video.i_visible_width != i_canvas_width
347      || p_filter->fmt_out.video.i_visible_height != i_canvas_height )
348     {
349         msg_Warn( p_filter, "Looks like something went wrong. "
350                   "Output size is %dx%d while we asked for %dx%d",
351                   p_filter->fmt_out.video.i_visible_width,
352                   p_filter->fmt_out.video.i_visible_height,
353                   i_canvas_width, i_canvas_height );
354     }
355
356     p_filter->pf_video_filter = Filter;
357
358     return VLC_SUCCESS;
359 }
360
361 /*****************************************************************************
362  *
363  *****************************************************************************/
364 static void Destroy( vlc_object_t *p_this )
365 {
366     filter_t *p_filter = (filter_t *)p_this;
367     filter_chain_Delete( p_filter->p_sys->p_chain );
368     free( p_filter->p_sys );
369 }
370
371 /*****************************************************************************
372  *
373  *****************************************************************************/
374 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
375 {
376     return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );
377 }