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>
37 #include <vlc_picture.h>
39 /*****************************************************************************
40 * Local and extern prototypes.
41 *****************************************************************************/
42 static int Activate( vlc_object_t
* );
43 static void Destroy( vlc_object_t
* );
44 static picture_t
*Filter( filter_t
*, picture_t
* );
46 /* This module effectively implements a form of picture-in-picture.
47 * - The outer picture is called the canvas.
48 * - The innter picture is callsed the subpicture.
50 * NB, all of the following operatons take into account aspect ratio
52 * A canvas is of canvas_{width,height}.
54 * - The subpicture is upconverted with a inverse scalefactor of:
55 * (The size of subpicture's largest dimension)
56 * --------------------------------------------
57 * (The size of canvas's equivalent dimension)
59 * Ie, The subpicture's largest dimension is made equal to the
60 * equivalent canvas dimension.
62 * - The upconverted subpicture's smallest dimension is then padded
63 * to make the upconverted subpicture have the same dimensions of
67 * - The subpicture is upconverted with an inverse scalefactor of:
68 * (The size of subpicture's smallest dimension)
69 * --------------------------------------------
70 * (The size of canvas's equivalent dimension)
72 * Ie, The subpicture's smallest dimension is made equal to the
73 * equivalent canvas dimension. (The subpicture will then be
74 * larger than the canvas)
76 * - The upconverted subpicture's largest dimension is then cropped
77 * to make the upconverted subpicture have the same dimensions of
81 /* NB, use of `padd' in this module is a 16-17th Century spelling of `pad' */
83 #define WIDTH_TEXT N_( "Output width" )
84 #define WIDTH_LONGTEXT N_( \
85 "Output (canvas) image width" )
86 #define HEIGHT_TEXT N_( "Output height" )
87 #define HEIGHT_LONGTEXT N_( \
88 "Output (canvas) image height" )
89 #define ASPECT_TEXT N_( "Output picture aspect ratio" )
90 #define ASPECT_LONGTEXT N_( \
91 "Set the canvas' picture aspect ratio. " \
92 "If omitted, the canvas is assumed to have the same SAR as the input." )
93 #define PADD_TEXT N_( "Pad video" )
94 #define PADD_LONGTEXT N_( \
95 "If enabled, video will be padded to fit in canvas after scaling. " \
96 "Otherwise, video will be cropped to fix in canvas after scaling." )
97 #define CANVAS_HELP N_( "Automatically resize and pad a video" )
99 #define CFG_PREFIX "canvas-"
101 /*****************************************************************************
103 *****************************************************************************/
105 set_shortname( N_("Canvas") )
106 set_description( N_("Canvas video filter") )
107 set_capability( "video filter", 0 )
108 set_help( CANVAS_HELP
)
109 set_callbacks( Activate
, Destroy
)
111 set_category( CAT_VIDEO
)
112 set_subcategory( SUBCAT_VIDEO_VFILTER
)
114 add_integer_with_range( CFG_PREFIX
"width", 0, 0, INT_MAX
,
115 WIDTH_TEXT
, WIDTH_LONGTEXT
, false )
116 add_integer_with_range( CFG_PREFIX
"height", 0, 0, INT_MAX
,
117 HEIGHT_TEXT
, HEIGHT_LONGTEXT
, false )
119 add_string( CFG_PREFIX
"aspect", NULL
,
120 ASPECT_TEXT
, ASPECT_LONGTEXT
, false )
122 add_bool( CFG_PREFIX
"padd", true,
123 PADD_TEXT
, PADD_LONGTEXT
, false )
126 static const char *const ppsz_filter_options
[] = {
127 "width", "height", "aspect", "padd", NULL
132 filter_chain_t
*p_chain
;
135 static picture_t
*video_new( filter_t
*p_filter
)
137 return filter_NewPicture( p_filter
->owner
.sys
);
140 /*****************************************************************************
142 *****************************************************************************/
143 static int Activate( vlc_object_t
*p_this
)
145 filter_t
*p_filter
= (filter_t
*)p_this
;
146 unsigned i_canvas_width
; /* visible width of output canvas */
147 unsigned i_canvas_height
; /* visible height of output canvas */
148 unsigned i_canvas_aspect
; /* canvas PictureAspectRatio */
149 es_format_t fmt
; /* target format after up/down conversion */
150 char psz_croppadd
[100];
152 char *psz_aspect
, *psz_parser
;
154 unsigned i_fmt_in_aspect
;
156 if( !p_filter
->b_allow_fmt_out_change
)
158 msg_Err( p_filter
, "Picture format change isn't allowed" );
162 if( p_filter
->fmt_in
.video
.i_chroma
!= p_filter
->fmt_out
.video
.i_chroma
)
164 msg_Err( p_filter
, "Input and output chromas don't match" );
168 config_ChainParse( p_filter
, CFG_PREFIX
, ppsz_filter_options
,
171 i_canvas_width
= var_CreateGetInteger( p_filter
, CFG_PREFIX
"width" );
172 i_canvas_height
= var_CreateGetInteger( p_filter
, CFG_PREFIX
"height" );
174 if( i_canvas_width
== 0 || i_canvas_height
== 0 )
176 msg_Err( p_filter
, "Width and height options must be set" );
180 if( i_canvas_width
& 1 || i_canvas_height
& 1 )
182 /* If this restriction were ever relaxed, it is very important to
183 * get the field polatiry correct */
184 msg_Err( p_filter
, "Width and height options must be even integers" );
188 if( p_filter
->fmt_in
.video
.i_sar_num
)
189 i_fmt_in_aspect
= (int64_t)p_filter
->fmt_in
.video
.i_sar_num
*
190 p_filter
->fmt_in
.video
.i_visible_width
*
192 p_filter
->fmt_in
.video
.i_sar_den
/
193 p_filter
->fmt_in
.video
.i_visible_height
;
195 i_fmt_in_aspect
= (int64_t)p_filter
->fmt_in
.video
.i_visible_width
*
197 p_filter
->fmt_in
.video
.i_visible_height
;
199 psz_aspect
= var_CreateGetNonEmptyString( p_filter
, CFG_PREFIX
"aspect" );
202 psz_parser
= strchr( psz_aspect
, ':' );
203 int numerator
= atoi( psz_aspect
);
204 int denominator
= psz_parser
? atoi( psz_parser
+1 ) : 0;
205 denominator
= denominator
== 0 ? 1 : denominator
;
206 i_canvas_aspect
= numerator
* VOUT_ASPECT_FACTOR
/ denominator
;
209 if( numerator
<= 0 || denominator
< 0 )
211 msg_Err( p_filter
, "Aspect ratio must be strictly positive" );
217 /* if there is no user supplied aspect ratio, assume the canvas
218 * has the same sample aspect ratio as the subpicture */
219 /* aspect = subpic_sar * canvas_width / canvas_height
220 * where subpic_sar = subpic_ph * subpic_par / subpic_pw */
221 i_canvas_aspect
= (uint64_t) p_filter
->fmt_in
.video
.i_visible_height
224 / (i_canvas_height
* p_filter
->fmt_in
.video
.i_visible_width
);
227 b_padd
= var_CreateGetBool( p_filter
, CFG_PREFIX
"padd" );
229 filter_sys_t
*p_sys
= (filter_sys_t
*)malloc( sizeof( filter_sys_t
) );
232 p_filter
->p_sys
= p_sys
;
234 filter_owner_t owner
= {
237 .buffer_new
= video_new
,
241 p_sys
->p_chain
= filter_chain_NewVideo( p_filter
, true, &owner
);
242 if( !p_sys
->p_chain
)
244 msg_Err( p_filter
, "Could not allocate filter chain" );
249 es_format_Copy( &fmt
, &p_filter
->fmt_in
);
251 /* one dimension will end up with one of the following: */
252 fmt
.video
.i_visible_width
= i_canvas_width
;
253 fmt
.video
.i_visible_height
= i_canvas_height
;
258 if( i_canvas_aspect
> i_fmt_in_aspect
)
260 /* The canvas has a wider aspect than the subpicture:
261 * ie, pillarbox the [scaled] subpicture */
262 /* The following is derived form:
263 * width = upconverted_subpic_height * subpic_par / canvas_sar
264 * where canvas_sar = canvas_width / (canvas_height * canvas_par)
266 fmt
.video
.i_visible_width
= i_canvas_width
269 if( fmt
.video
.i_visible_width
& 1 ) fmt
.video
.i_visible_width
-= 1;
271 i_padd
= (i_canvas_width
- fmt
.video
.i_visible_width
) / 2;
272 i_offset
= (i_padd
& 1);
273 snprintf( psz_croppadd
, 100, "croppadd{paddleft=%d,paddright=%d}",
274 i_padd
- i_offset
, i_padd
+ i_offset
);
278 /* The canvas has a taller aspect than the subpicture:
279 * ie, letterbox the [scaled] subpicture */
280 fmt
.video
.i_visible_height
= i_canvas_height
283 if( fmt
.video
.i_visible_height
& 1 ) fmt
.video
.i_visible_height
-= 1;
285 i_padd
= (i_canvas_height
- fmt
.video
.i_visible_height
) / 2;
286 i_offset
= (i_padd
& 1);
287 snprintf( psz_croppadd
, 100, "croppadd{paddtop=%d,paddbottom=%d}",
288 i_padd
- i_offset
, i_padd
+ i_offset
);
294 if( i_canvas_aspect
< i_fmt_in_aspect
)
296 /* The canvas has a narrower aspect than the subpicture:
297 * ie, crop the [scaled] subpicture horizontally */
298 fmt
.video
.i_visible_width
= i_canvas_width
301 if( fmt
.video
.i_visible_width
& 1 ) fmt
.video
.i_visible_width
-= 1;
303 i_padd
= (fmt
.video
.i_visible_width
- i_canvas_width
) / 2;
304 i_offset
= (i_padd
& 1);
305 snprintf( psz_croppadd
, 100, "croppadd{cropleft=%d,cropright=%d}",
306 i_padd
- i_offset
, i_padd
+ i_offset
);
310 /* The canvas has a shorter aspect than the subpicture:
311 * ie, crop the [scaled] subpicture vertically */
312 fmt
.video
.i_visible_height
= i_canvas_height
315 if( fmt
.video
.i_visible_height
& 1 ) fmt
.video
.i_visible_height
-= 1;
317 i_padd
= (fmt
.video
.i_visible_height
- i_canvas_height
) / 2;
318 i_offset
= (i_padd
& 1);
319 snprintf( psz_croppadd
, 100, "croppadd{croptop=%d,cropbottom=%d}",
320 i_padd
- i_offset
, i_padd
+ i_offset
);
324 /* xxx, should the clean area include the letter-boxing?
325 * probably not, as some codecs can make use of that information
326 * and it should be a scaled version of the input clean area
328 fmt
.video
.i_width
= p_filter
->fmt_in
.video
.i_width
* fmt
.video
.i_visible_width
/ p_filter
->fmt_in
.video
.i_visible_width
;
329 fmt
.video
.i_height
= p_filter
->fmt_in
.video
.i_height
* fmt
.video
.i_visible_height
/ p_filter
->fmt_in
.video
.i_visible_height
;
331 filter_chain_Reset( p_sys
->p_chain
, &p_filter
->fmt_in
, &fmt
);
332 /* Append scaling module */
333 if ( !filter_chain_AppendConverter( p_sys
->p_chain
, NULL
, NULL
) )
335 msg_Err( p_filter
, "Could not append scaling filter" );
340 /* Append croppadd module if we actually do cropping or padding instead of just scaling*/
343 if ( !filter_chain_AppendFromString( p_sys
->p_chain
, psz_croppadd
) )
345 msg_Err( p_filter
, "Could not append cropadd filter" );
346 filter_chain_Delete( p_sys
->p_chain
);
352 es_format_Copy( &p_filter
->fmt_out
,
353 filter_chain_GetFmtOut( p_sys
->p_chain
) );
355 vlc_ureduce( &p_filter
->fmt_out
.video
.i_sar_num
,
356 &p_filter
->fmt_out
.video
.i_sar_den
,
357 i_canvas_aspect
* p_filter
->fmt_out
.video
.i_visible_height
,
358 VOUT_ASPECT_FACTOR
* p_filter
->fmt_out
.video
.i_visible_width
,
361 if( p_filter
->fmt_out
.video
.i_visible_width
!= i_canvas_width
362 || p_filter
->fmt_out
.video
.i_visible_height
!= i_canvas_height
)
364 msg_Warn( p_filter
, "Looks like something went wrong. "
365 "Output size is %dx%d while we asked for %dx%d",
366 p_filter
->fmt_out
.video
.i_visible_width
,
367 p_filter
->fmt_out
.video
.i_visible_height
,
368 i_canvas_width
, i_canvas_height
);
371 p_filter
->pf_video_filter
= Filter
;
376 /*****************************************************************************
378 *****************************************************************************/
379 static void Destroy( vlc_object_t
*p_this
)
381 filter_t
*p_filter
= (filter_t
*)p_this
;
382 filter_chain_Delete( p_filter
->p_sys
->p_chain
);
383 free( p_filter
->p_sys
);
386 /*****************************************************************************
388 *****************************************************************************/
389 static picture_t
*Filter( filter_t
*p_filter
, picture_t
*p_pic
)
391 return filter_chain_VideoFilter( p_filter
->p_sys
->p_chain
, p_pic
);