]> git.sesse.net Git - vlc/blob - modules/video_filter/canvas.c
Removed es_format_t::i_aspect.
[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 the VideoLAN team
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
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, 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 static int alloc_init( filter_t *, void * );
45
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.
49  *
50  * NB, all of the following operatons take into account aspect ratio
51  *
52  * A canvas is of canvas_{width,height}.
53  * In Pad mode:
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)
58  *
59  *   Ie, The subpicture's largest dimension is made equal to the
60  *   equivalent canvas dimension.
61  *
62  *  - The upconverted subpicture's smallest dimension is then padded
63  *    to make the upconverted subpicture have the same dimensions of
64  *    the canvas.
65  *
66  * In Crop mode:
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)
71  *
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)
75  *
76  *  - The upconverted subpicture's largest dimension is then cropped
77  *    to make the upconverted subpicture have the same dimensions of
78  *    the canvas.
79  */
80
81 /* NB, use of `padd' in this module is a 16-17th Century spelling of `pad' */
82
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
98 #define CFG_PREFIX "canvas-"
99
100 /*****************************************************************************
101  * Module descriptor
102  *****************************************************************************/
103 vlc_module_begin ()
104     set_shortname( N_("Canvas") )
105     set_description( N_("Automatically resize and pad a video") )
106     set_capability( "video filter2", 0 )
107     set_callbacks( Activate, Destroy )
108
109     set_category( CAT_VIDEO )
110     set_subcategory( SUBCAT_VIDEO_VFILTER )
111
112     add_integer_with_range( CFG_PREFIX "width", 0, 0, INT_MAX, NULL,
113                             WIDTH_TEXT, WIDTH_LONGTEXT, false )
114     add_integer_with_range( CFG_PREFIX "height", 0, 0, INT_MAX, NULL,
115                             HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
116
117     add_string( CFG_PREFIX "aspect", NULL, NULL,
118                 ASPECT_TEXT, ASPECT_LONGTEXT, false )
119
120     add_bool( CFG_PREFIX "padd", true, NULL,
121               PADD_TEXT, PADD_LONGTEXT, false )
122 vlc_module_end ()
123
124 static const char *const ppsz_filter_options[] = {
125     "width", "height", "aspect", "padd", NULL
126 };
127
128 struct filter_sys_t
129 {
130     filter_chain_t *p_chain;
131 };
132
133 /*****************************************************************************
134  *
135  *****************************************************************************/
136 static int Activate( vlc_object_t *p_this )
137 {
138     filter_t *p_filter = (filter_t *)p_this;
139     unsigned i_canvas_width; /* width of output canvas */
140     unsigned i_canvas_height; /* height of output canvas */
141     unsigned i_canvas_aspect; /* canvas PictureAspectRatio */
142     es_format_t fmt; /* target format after up/down conversion */
143     char psz_croppadd[100];
144     int i_padd,i_offset;
145     char *psz_aspect, *psz_parser;
146     bool b_padd;
147     unsigned i_fmt_in_aspect;
148
149     if( !p_filter->b_allow_fmt_out_change )
150     {
151         msg_Err( p_filter, "Picture format change isn't allowed" );
152         return VLC_EGENERIC;
153     }
154
155     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
156     {
157         msg_Err( p_filter, "Input and output chromas don't match" );
158         return VLC_EGENERIC;
159     }
160
161     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
162                        p_filter->p_cfg );
163
164     i_canvas_width = var_CreateGetInteger( p_filter, CFG_PREFIX "width" );
165     i_canvas_height = var_CreateGetInteger( p_filter, CFG_PREFIX "height" );
166
167     if( i_canvas_width == 0 || i_canvas_height == 0 )
168     {
169         msg_Err( p_filter, "Width and height options must be set" );
170         return VLC_EGENERIC;
171     }
172
173     if( i_canvas_width & 1 || i_canvas_height & 1 )
174     {
175         /* If this restriction were ever relaxed, it is very important to
176          * get the field polatiry correct */
177         msg_Err( p_filter, "Width and height options must be even integers" );
178         return VLC_EGENERIC;
179     }
180
181     i_fmt_in_aspect = (int64_t)p_filter->fmt_in.video.i_sar_num *
182                       p_filter->fmt_in.video.i_width *
183                       VOUT_ASPECT_FACTOR /
184                       p_filter->fmt_in.video.i_sar_den /
185                       p_filter->fmt_in.video.i_height;
186     psz_aspect = var_CreateGetNonEmptyString( p_filter, CFG_PREFIX "aspect" );
187     if( psz_aspect )
188     {
189         psz_parser = strchr( psz_aspect, ':' );
190         int numerator = atoi( psz_aspect );
191         int denominator = psz_parser ? atoi( psz_parser+1 ) : 0;
192         denominator = denominator == 0 ? 1 : denominator;
193         i_canvas_aspect = numerator * VOUT_ASPECT_FACTOR / denominator;
194         free( psz_aspect );
195
196         if( numerator <= 0 || denominator < 0 )
197         {
198             msg_Err( p_filter, "Aspect ratio must be strictly positive" );
199             return VLC_EGENERIC;
200         }
201     }
202     else
203     {
204         /* if there is no user supplied aspect ratio, assume the canvas
205          * has the same sample aspect ratio as the subpicture */
206         /* aspect = subpic_sar * canvas_width / canvas_height
207          *  where subpic_sar = subpic_ph * subpic_par / subpic_pw */
208         i_canvas_aspect = (uint64_t) p_filter->fmt_in.video.i_height
209                         * i_fmt_in_aspect
210                         * i_canvas_width
211                         / (i_canvas_height * p_filter->fmt_in.video.i_width);
212     }
213
214     b_padd = var_CreateGetBool( p_filter, CFG_PREFIX "padd" );
215
216     filter_sys_t *p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
217     if( !p_sys )
218         return VLC_ENOMEM;
219     p_filter->p_sys = p_sys;
220
221     p_sys->p_chain = filter_chain_New( p_filter, "video filter2", true,
222                                        alloc_init, NULL, p_filter );
223     if( !p_sys->p_chain )
224     {
225         msg_Err( p_filter, "Could not allocate filter chain" );
226         free( p_sys );
227         return VLC_EGENERIC;
228     }
229
230     es_format_Copy( &fmt, &p_filter->fmt_in );
231
232     /* one dimension will end up with one of the following: */
233     fmt.video.i_width = i_canvas_width;
234     fmt.video.i_height = i_canvas_height;
235
236     if( b_padd )
237     {
238         /* Padd */
239         if( i_canvas_aspect > i_fmt_in_aspect )
240         {
241             /* The canvas has a wider aspect than the subpicture:
242              *  ie, pillarbox the [scaled] subpicture */
243             /* The following is derived form:
244              * width = upconverted_subpic_height * subpic_par / canvas_sar
245              *  where canvas_sar = canvas_width / (canvas_height * canvas_par)
246              * then simplify */
247             fmt.video.i_width = i_canvas_width
248                               * i_fmt_in_aspect
249                               / i_canvas_aspect;
250             if( fmt.video.i_width & 1 ) fmt.video.i_width -= 1;
251
252             i_padd = (i_canvas_width - fmt.video.i_width) / 2;
253             i_offset = (i_padd & 1);
254             snprintf( psz_croppadd, 100, "croppadd{paddleft=%d,paddright=%d}",
255                       i_padd - i_offset, i_padd + i_offset );
256         }
257         else
258         {
259             /* The canvas has a taller aspect than the subpicture:
260              *  ie, letterbox the [scaled] subpicture */
261             fmt.video.i_height = i_canvas_height
262                                * i_canvas_aspect
263                                / i_fmt_in_aspect;
264             if( fmt.video.i_height & 1 ) fmt.video.i_height -= 1;
265
266             i_padd = (i_canvas_height - fmt.video.i_height ) / 2;
267             i_offset = (i_padd & 1);
268             snprintf( psz_croppadd, 100, "croppadd{paddtop=%d,paddbottom=%d}",
269                       i_padd - i_offset, i_padd + i_offset );
270         }
271     }
272     else
273     {
274         /* Crop */
275         if( i_canvas_aspect < i_fmt_in_aspect )
276         {
277             /* The canvas has a narrower aspect than the subpicture:
278              *  ie, crop the [scaled] subpicture horizontally */
279             fmt.video.i_width = i_canvas_width
280                               * i_fmt_in_aspect
281                               / i_canvas_aspect;
282             if( fmt.video.i_width & 1 ) fmt.video.i_width -= 1;
283
284             i_padd = (fmt.video.i_width - i_canvas_width) / 2;
285             i_offset = (i_padd & 1);
286             snprintf( psz_croppadd, 100, "croppadd{cropleft=%d,cropright=%d}",
287                       i_padd - i_offset, i_padd + i_offset );
288         }
289         else
290         {
291             /* The canvas has a shorter aspect than the subpicture:
292              *  ie, crop the [scaled] subpicture vertically */
293             fmt.video.i_height = i_canvas_height
294                                * i_canvas_aspect
295                                / i_fmt_in_aspect;
296             if( fmt.video.i_height & 1 ) fmt.video.i_height -= 1;
297
298             i_padd = (fmt.video.i_height - i_canvas_height) / 2;
299             i_offset = (i_padd & 1);
300             snprintf( psz_croppadd, 100, "croppadd{croptop=%d,cropbottom=%d}",
301                       i_padd - i_offset, i_padd + i_offset );
302         }
303     }
304
305     /* xxx, should the clean area include the letter-boxing?
306      *  probably not, as some codecs can make use of that information
307      *  and it should be a scaled version of the input clean area
308      *   -- davidf */
309     fmt.video.i_visible_width = fmt.video.i_width;
310     fmt.video.i_visible_height = fmt.video.i_height;
311
312     filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &fmt );
313     /* Append scaling module */
314     filter_chain_AppendFilter( p_sys->p_chain, NULL, NULL, NULL, NULL );
315     /* Append padding module */
316     filter_chain_AppendFromString( p_sys->p_chain, psz_croppadd );
317
318     fmt = *filter_chain_GetFmtOut( p_sys->p_chain );
319     es_format_Copy( &p_filter->fmt_out, &fmt );
320
321     p_filter->fmt_out.video.i_sar_num =
322         i_canvas_aspect    * p_filter->fmt_out.video.i_height;
323     p_filter->fmt_out.video.i_sar_den =
324         VOUT_ASPECT_FACTOR * p_filter->fmt_out.video.i_width;
325
326     if( p_filter->fmt_out.video.i_width != i_canvas_width
327      || p_filter->fmt_out.video.i_height != i_canvas_height )
328     {
329         msg_Warn( p_filter, "Looks like something went wrong. "
330                   "Output size is %dx%d while we asked for %dx%d",
331                   p_filter->fmt_out.video.i_width,
332                   p_filter->fmt_out.video.i_height,
333                   i_canvas_width, i_canvas_height );
334     }
335
336     p_filter->pf_video_filter = Filter;
337
338     return VLC_SUCCESS;
339 }
340
341 /*****************************************************************************
342  *
343  *****************************************************************************/
344 static void Destroy( vlc_object_t *p_this )
345 {
346     filter_t *p_filter = (filter_t *)p_this;
347     filter_chain_Delete( p_filter->p_sys->p_chain );
348     free( p_filter->p_sys );
349 }
350
351 /*****************************************************************************
352  *
353  *****************************************************************************/
354 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
355 {
356     return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );
357 }
358
359 /*****************************************************************************
360  *
361  *****************************************************************************/
362 static picture_t *video_new( filter_t *p_filter )
363 {
364     return filter_NewPicture( (filter_t*)p_filter->p_owner );
365 }
366
367 static void video_del( filter_t *p_filter, picture_t *p_pic )
368 {
369     return filter_DeletePicture( (filter_t*)p_filter->p_owner, p_pic );
370 }
371
372 static int alloc_init( filter_t *p_filter, void *p_data )
373 {
374     p_filter->p_owner = p_data;
375     p_filter->pf_vout_buffer_new = video_new;
376     p_filter->pf_vout_buffer_del = video_del;
377     return VLC_SUCCESS;
378 }