A little more detail regarding using my github copies of the code with where it's...
[vlc/adversarial.git] / modules / video_filter / canvas.c
blobb81a1877a2a5969f6c489d9b89994900de23589a
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$
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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <limits.h>
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}.
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)
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
63 * the canvas.
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)
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
77 * the canvas.
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 /*****************************************************************************
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 )
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 )
123 vlc_module_end ()
125 static const char *const ppsz_filter_options[] = {
126 "width", "height", "aspect", "padd", NULL
129 struct filter_sys_t
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 static void video_del( filter_t *p_filter, picture_t *p_pic )
141 return filter_DeletePicture( p_filter->owner.sys, p_pic );
144 /*****************************************************************************
146 *****************************************************************************/
147 static int Activate( vlc_object_t *p_this )
149 filter_t *p_filter = (filter_t *)p_this;
150 unsigned i_canvas_width; /* visible width of output canvas */
151 unsigned i_canvas_height; /* visible height of output canvas */
152 unsigned i_canvas_aspect; /* canvas PictureAspectRatio */
153 es_format_t fmt; /* target format after up/down conversion */
154 char psz_croppadd[100];
155 int i_padd,i_offset;
156 char *psz_aspect, *psz_parser;
157 bool b_padd;
158 unsigned i_fmt_in_aspect;
160 if( !p_filter->b_allow_fmt_out_change )
162 msg_Err( p_filter, "Picture format change isn't allowed" );
163 return VLC_EGENERIC;
166 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
168 msg_Err( p_filter, "Input and output chromas don't match" );
169 return VLC_EGENERIC;
172 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
173 p_filter->p_cfg );
175 i_canvas_width = var_CreateGetInteger( p_filter, CFG_PREFIX "width" );
176 i_canvas_height = var_CreateGetInteger( p_filter, CFG_PREFIX "height" );
178 if( i_canvas_width == 0 || i_canvas_height == 0 )
180 msg_Err( p_filter, "Width and height options must be set" );
181 return VLC_EGENERIC;
184 if( i_canvas_width & 1 || i_canvas_height & 1 )
186 /* If this restriction were ever relaxed, it is very important to
187 * get the field polatiry correct */
188 msg_Err( p_filter, "Width and height options must be even integers" );
189 return VLC_EGENERIC;
192 if( p_filter->fmt_in.video.i_sar_num )
193 i_fmt_in_aspect = (int64_t)p_filter->fmt_in.video.i_sar_num *
194 p_filter->fmt_in.video.i_visible_width *
195 VOUT_ASPECT_FACTOR /
196 p_filter->fmt_in.video.i_sar_den /
197 p_filter->fmt_in.video.i_visible_height;
198 else
199 i_fmt_in_aspect = (int64_t)p_filter->fmt_in.video.i_visible_width *
200 VOUT_ASPECT_FACTOR /
201 p_filter->fmt_in.video.i_visible_height;
203 psz_aspect = var_CreateGetNonEmptyString( p_filter, CFG_PREFIX "aspect" );
204 if( psz_aspect )
206 psz_parser = strchr( psz_aspect, ':' );
207 int numerator = atoi( psz_aspect );
208 int denominator = psz_parser ? atoi( psz_parser+1 ) : 0;
209 denominator = denominator == 0 ? 1 : denominator;
210 i_canvas_aspect = numerator * VOUT_ASPECT_FACTOR / denominator;
211 free( psz_aspect );
213 if( numerator <= 0 || denominator < 0 )
215 msg_Err( p_filter, "Aspect ratio must be strictly positive" );
216 return VLC_EGENERIC;
219 else
221 /* if there is no user supplied aspect ratio, assume the canvas
222 * has the same sample aspect ratio as the subpicture */
223 /* aspect = subpic_sar * canvas_width / canvas_height
224 * where subpic_sar = subpic_ph * subpic_par / subpic_pw */
225 i_canvas_aspect = (uint64_t) p_filter->fmt_in.video.i_visible_height
226 * i_fmt_in_aspect
227 * i_canvas_width
228 / (i_canvas_height * p_filter->fmt_in.video.i_visible_width);
231 b_padd = var_CreateGetBool( p_filter, CFG_PREFIX "padd" );
233 filter_sys_t *p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
234 if( !p_sys )
235 return VLC_ENOMEM;
236 p_filter->p_sys = p_sys;
238 filter_owner_t owner = {
239 .sys = p_filter,
240 .video = {
241 .buffer_new = video_new,
242 .buffer_del = video_del,
246 p_sys->p_chain = filter_chain_NewVideo( p_filter, true, &owner );
247 if( !p_sys->p_chain )
249 msg_Err( p_filter, "Could not allocate filter chain" );
250 free( p_sys );
251 return VLC_EGENERIC;
254 es_format_Copy( &fmt, &p_filter->fmt_in );
256 /* one dimension will end up with one of the following: */
257 fmt.video.i_visible_width = i_canvas_width;
258 fmt.video.i_visible_height = i_canvas_height;
260 if( b_padd )
262 /* Padd */
263 if( i_canvas_aspect > i_fmt_in_aspect )
265 /* The canvas has a wider aspect than the subpicture:
266 * ie, pillarbox the [scaled] subpicture */
267 /* The following is derived form:
268 * width = upconverted_subpic_height * subpic_par / canvas_sar
269 * where canvas_sar = canvas_width / (canvas_height * canvas_par)
270 * then simplify */
271 fmt.video.i_visible_width = i_canvas_width
272 * i_fmt_in_aspect
273 / i_canvas_aspect;
274 if( fmt.video.i_visible_width & 1 ) fmt.video.i_visible_width -= 1;
276 i_padd = (i_canvas_width - fmt.video.i_visible_width) / 2;
277 i_offset = (i_padd & 1);
278 snprintf( psz_croppadd, 100, "croppadd{paddleft=%d,paddright=%d}",
279 i_padd - i_offset, i_padd + i_offset );
281 else
283 /* The canvas has a taller aspect than the subpicture:
284 * ie, letterbox the [scaled] subpicture */
285 fmt.video.i_visible_height = i_canvas_height
286 * i_canvas_aspect
287 / i_fmt_in_aspect;
288 if( fmt.video.i_visible_height & 1 ) fmt.video.i_visible_height -= 1;
290 i_padd = (i_canvas_height - fmt.video.i_visible_height ) / 2;
291 i_offset = (i_padd & 1);
292 snprintf( psz_croppadd, 100, "croppadd{paddtop=%d,paddbottom=%d}",
293 i_padd - i_offset, i_padd + i_offset );
296 else
298 /* Crop */
299 if( i_canvas_aspect < i_fmt_in_aspect )
301 /* The canvas has a narrower aspect than the subpicture:
302 * ie, crop the [scaled] subpicture horizontally */
303 fmt.video.i_visible_width = i_canvas_width
304 * i_fmt_in_aspect
305 / i_canvas_aspect;
306 if( fmt.video.i_visible_width & 1 ) fmt.video.i_visible_width -= 1;
308 i_padd = (fmt.video.i_visible_width - i_canvas_width) / 2;
309 i_offset = (i_padd & 1);
310 snprintf( psz_croppadd, 100, "croppadd{cropleft=%d,cropright=%d}",
311 i_padd - i_offset, i_padd + i_offset );
313 else
315 /* The canvas has a shorter aspect than the subpicture:
316 * ie, crop the [scaled] subpicture vertically */
317 fmt.video.i_visible_height = i_canvas_height
318 * i_canvas_aspect
319 / i_fmt_in_aspect;
320 if( fmt.video.i_visible_height & 1 ) fmt.video.i_visible_height -= 1;
322 i_padd = (fmt.video.i_visible_height - i_canvas_height) / 2;
323 i_offset = (i_padd & 1);
324 snprintf( psz_croppadd, 100, "croppadd{croptop=%d,cropbottom=%d}",
325 i_padd - i_offset, i_padd + i_offset );
329 /* xxx, should the clean area include the letter-boxing?
330 * probably not, as some codecs can make use of that information
331 * and it should be a scaled version of the input clean area
332 * -- davidf */
333 fmt.video.i_width = p_filter->fmt_in.video.i_width * fmt.video.i_visible_width / p_filter->fmt_in.video.i_visible_width;
334 fmt.video.i_height = p_filter->fmt_in.video.i_height * fmt.video.i_visible_height / p_filter->fmt_in.video.i_visible_height;
336 filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &fmt );
337 /* Append scaling module */
338 filter_chain_AppendFilter( p_sys->p_chain, NULL, NULL, NULL, NULL );
339 /* Append padding module */
340 filter_chain_AppendFromString( p_sys->p_chain, psz_croppadd );
342 fmt = *filter_chain_GetFmtOut( p_sys->p_chain );
343 es_format_Copy( &p_filter->fmt_out, &fmt );
345 vlc_ureduce( &p_filter->fmt_out.video.i_sar_num,
346 &p_filter->fmt_out.video.i_sar_den,
347 i_canvas_aspect * p_filter->fmt_out.video.i_visible_height,
348 VOUT_ASPECT_FACTOR * p_filter->fmt_out.video.i_visible_width,
351 if( p_filter->fmt_out.video.i_visible_width != i_canvas_width
352 || p_filter->fmt_out.video.i_visible_height != i_canvas_height )
354 msg_Warn( p_filter, "Looks like something went wrong. "
355 "Output size is %dx%d while we asked for %dx%d",
356 p_filter->fmt_out.video.i_visible_width,
357 p_filter->fmt_out.video.i_visible_height,
358 i_canvas_width, i_canvas_height );
361 p_filter->pf_video_filter = Filter;
363 return VLC_SUCCESS;
366 /*****************************************************************************
368 *****************************************************************************/
369 static void Destroy( vlc_object_t *p_this )
371 filter_t *p_filter = (filter_t *)p_this;
372 filter_chain_Delete( p_filter->p_sys->p_chain );
373 free( p_filter->p_sys );
376 /*****************************************************************************
378 *****************************************************************************/
379 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
381 return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );