1 /*****************************************************************************
2 * canvas.c : automatically resize and padd a video to fit in canvas
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
7 * Authors: Antoine Cellerier <dionoea at videolan dot org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_filter.h>
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 * );
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.
49 * NB, all of the following operatons take into account aspect ratio
51 * A canvas is of canvas_{width,height}.
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)
58 * Ie, The subpicture's largest dimension is made equal to the
59 * equivalent canvas dimension.
61 * - The upconverted subpicture's smallest dimension is then padded
62 * to make the upconverted subpicture have the same dimensions of
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)
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)
75 * - The upconverted subpicture's largest dimension is then cropped
76 * to make the upconverted subpicture have the same dimensions of
80 /* NB, use of `padd' in this module is a 16-17th Century spelling of `pad' */
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" )
98 #define CFG_PREFIX "canvas-"
100 /*****************************************************************************
102 *****************************************************************************/
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 )
110 set_category( CAT_VIDEO )
111 set_subcategory( SUBCAT_VIDEO_VFILTER )
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 )
118 add_string( CFG_PREFIX "aspect", NULL,
119 ASPECT_TEXT, ASPECT_LONGTEXT, false )
121 add_bool( CFG_PREFIX "padd", true,
122 PADD_TEXT, PADD_LONGTEXT, false )
125 static const char *const ppsz_filter_options[] = {
126 "width", "height", "aspect", "padd", NULL
131 filter_chain_t *p_chain;
134 static picture_t *video_new( filter_t *p_filter )
136 return filter_NewPicture( p_filter->owner.sys );
139 /*****************************************************************************
141 *****************************************************************************/
142 static int Activate( vlc_object_t *p_this )
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];
151 char *psz_aspect, *psz_parser;
153 unsigned i_fmt_in_aspect;
155 if( !p_filter->b_allow_fmt_out_change )
157 msg_Err( p_filter, "Picture format change isn't allowed" );
161 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
163 msg_Err( p_filter, "Input and output chromas don't match" );
167 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
170 i_canvas_width = var_CreateGetInteger( p_filter, CFG_PREFIX "width" );
171 i_canvas_height = var_CreateGetInteger( p_filter, CFG_PREFIX "height" );
173 if( i_canvas_width == 0 || i_canvas_height == 0 )
175 msg_Err( p_filter, "Width and height options must be set" );
179 if( i_canvas_width & 1 || i_canvas_height & 1 )
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" );
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 *
191 p_filter->fmt_in.video.i_sar_den /
192 p_filter->fmt_in.video.i_visible_height;
194 i_fmt_in_aspect = (int64_t)p_filter->fmt_in.video.i_visible_width *
196 p_filter->fmt_in.video.i_visible_height;
198 psz_aspect = var_CreateGetNonEmptyString( p_filter, CFG_PREFIX "aspect" );
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;
208 if( numerator <= 0 || denominator < 0 )
210 msg_Err( p_filter, "Aspect ratio must be strictly positive" );
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
223 / (i_canvas_height * p_filter->fmt_in.video.i_visible_width);
226 b_padd = var_CreateGetBool( p_filter, CFG_PREFIX "padd" );
228 filter_sys_t *p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
231 p_filter->p_sys = p_sys;
233 filter_owner_t owner = {
236 .buffer_new = video_new,
240 p_sys->p_chain = filter_chain_NewVideo( p_filter, true, &owner );
241 if( !p_sys->p_chain )
243 msg_Err( p_filter, "Could not allocate filter chain" );
248 es_format_Copy( &fmt, &p_filter->fmt_in );
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;
257 if( i_canvas_aspect > i_fmt_in_aspect )
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)
265 fmt.video.i_visible_width = i_canvas_width
268 if( fmt.video.i_visible_width & 1 ) fmt.video.i_visible_width -= 1;
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 );
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
282 if( fmt.video.i_visible_height & 1 ) fmt.video.i_visible_height -= 1;
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 );
293 if( i_canvas_aspect < i_fmt_in_aspect )
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
300 if( fmt.video.i_visible_width & 1 ) fmt.video.i_visible_width -= 1;
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 );
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
314 if( fmt.video.i_visible_height & 1 ) fmt.video.i_visible_height -= 1;
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 );
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
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;
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*/
335 filter_chain_AppendFromString( p_sys->p_chain, psz_croppadd );
337 fmt = *filter_chain_GetFmtOut( p_sys->p_chain );
338 es_format_Copy( &p_filter->fmt_out, &fmt );
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,
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 )
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 );
356 p_filter->pf_video_filter = Filter;
361 /*****************************************************************************
363 *****************************************************************************/
364 static void Destroy( vlc_object_t *p_this )
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 );
371 /*****************************************************************************
373 *****************************************************************************/
374 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
376 return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );