]> git.sesse.net Git - vlc/blob - modules/video_filter/canvas.c
Merge branch 1.0-bugfix
[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 #define WIDTH_TEXT N_( "Image width" )
47 #define WIDTH_LONGTEXT N_( \
48     "Image width" )
49 #define HEIGHT_TEXT N_( "Image height" )
50 #define HEIGHT_LONGTEXT N_( \
51     "Image height" )
52 #define ASPECT_TEXT N_( "Aspect ratio" )
53 #define ASPECT_LONGTEXT N_( \
54     "Set aspect (like 4:3) of the video canvas" )
55 #define PADD_TEXT N_( "Padd video" )
56 #define PADD_LONGTEXT N_( \
57     "If enabled, video will be padded to fit in canvas after scaling. " \
58     "Otherwise, video will be cropped to fix in canvas after scaling." )
59
60 #define CFG_PREFIX "canvas-"
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin ()
66     set_description( N_("Automatically resize and padd a video") )
67     set_capability( "video filter2", 0 )
68     set_callbacks( Activate, Destroy )
69
70     add_integer_with_range( CFG_PREFIX "width", 0, 0, INT_MAX, NULL,
71                             WIDTH_TEXT, WIDTH_LONGTEXT, false )
72     add_integer_with_range( CFG_PREFIX "height", 0, 0, INT_MAX, NULL,
73                             HEIGHT_TEXT, HEIGHT_LONGTEXT, false )
74
75     add_string( CFG_PREFIX "aspect", "4:3", NULL,
76                 ASPECT_TEXT, ASPECT_LONGTEXT, false )
77
78     add_bool( CFG_PREFIX "padd", true, NULL,
79               PADD_TEXT, PADD_LONGTEXT, false )
80 vlc_module_end ()
81
82 static const char *const ppsz_filter_options[] = {
83     "width", "height", "aspect", "padd", NULL
84 };
85
86 struct filter_sys_t
87 {
88     filter_chain_t *p_chain;
89 };
90
91 /*****************************************************************************
92  *
93  *****************************************************************************/
94 static int Activate( vlc_object_t *p_this )
95 {
96     filter_t *p_filter = (filter_t *)p_this;
97     unsigned int i_width, i_height;
98     es_format_t fmt;
99     char psz_croppadd[100];
100     int i_padd,i_offset;
101     char *psz_aspect, *psz_parser;
102     int i_aspect;
103     bool b_padd;
104
105     if( !p_filter->b_allow_fmt_out_change )
106     {
107         msg_Err( p_filter, "Picture format change isn't allowed" );
108         return VLC_EGENERIC;
109     }
110
111     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
112     {
113         msg_Err( p_filter, "Input and output chromas don't match" );
114         return VLC_EGENERIC;
115     }
116
117     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
118                        p_filter->p_cfg );
119
120     i_width = var_CreateGetInteger( p_filter, CFG_PREFIX "width" );
121     i_height = var_CreateGetInteger( p_filter, CFG_PREFIX "height" );
122
123     if( i_width == 0 || i_height == 0 )
124     {
125         msg_Err( p_filter, "Width and height options must be set" );
126         return VLC_EGENERIC;
127     }
128
129     if( i_width & 1 || i_height & 1 )
130     {
131         msg_Err( p_filter, "Width and height options must be even integers" );
132         return VLC_EGENERIC;
133     }
134
135     psz_aspect = var_CreateGetNonEmptyString( p_filter, CFG_PREFIX "aspect" );
136     if( !psz_aspect )
137     {
138         msg_Err( p_filter, "Aspect ratio must be set" );
139         return VLC_EGENERIC;
140     }
141     psz_parser = strchr( psz_aspect, ':' );
142     if( psz_parser ) psz_parser++;
143     if( psz_parser && atoi( psz_parser ) > 0 )
144         i_aspect = atoi( psz_aspect ) * VOUT_ASPECT_FACTOR / atoi( psz_parser );
145     else
146         i_aspect = atof( psz_aspect ) * VOUT_ASPECT_FACTOR;
147     free( psz_aspect );
148
149     if( i_aspect <= 0 )
150     {
151         msg_Err( p_filter, "Aspect ratio must be strictly positive" );
152         return VLC_EGENERIC;
153     }
154
155     b_padd = var_CreateGetBool( p_filter, CFG_PREFIX "padd" );
156
157     filter_sys_t *p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
158     if( !p_sys )
159         return VLC_ENOMEM;
160     p_filter->p_sys = p_sys;
161
162     p_sys->p_chain = filter_chain_New( p_filter, "video filter2", true,
163                                        alloc_init, NULL, p_filter );
164     if( !p_sys->p_chain )
165     {
166         msg_Err( p_filter, "Could not allocate filter chain" );
167         free( p_sys );
168         return VLC_EGENERIC;
169     }
170
171     es_format_Copy( &fmt, &p_filter->fmt_in );
172
173     fmt.video.i_width = i_width;
174     fmt.video.i_height = ( p_filter->fmt_in.video.i_height * i_width )
175                          / p_filter->fmt_in.video.i_width;
176     fmt.video.i_height = ( fmt.video.i_height * i_aspect )
177                          / p_filter->fmt_in.video.i_aspect;
178
179     if( b_padd )
180     {
181         /* Padd */
182         if( fmt.video.i_height > i_height )
183         {
184             fmt.video.i_height = i_height;
185             fmt.video.i_width = ( p_filter->fmt_in.video.i_width * i_height )
186                                 / p_filter->fmt_in.video.i_height;
187             fmt.video.i_width = ( fmt.video.i_width * p_filter->fmt_in.video.i_aspect )
188                                 / i_aspect;
189             if( fmt.video.i_width & 1 ) fmt.video.i_width -= 1;
190
191             i_padd = (i_width - fmt.video.i_width) / 2;
192             i_offset = (i_padd & 1);
193             /* Gruik */
194             snprintf( psz_croppadd, 100, "croppadd{paddleft=%d,paddright=%d}",
195                       i_padd - i_offset, i_padd + i_offset );
196         }
197         else
198         {
199             if( fmt.video.i_height & 1 ) fmt.video.i_height -= 1;
200             i_padd = (i_height - fmt.video.i_height ) / 2;
201             i_offset = (i_padd & 1);
202             /* Gruik */
203             snprintf( psz_croppadd, 100, "croppadd{paddtop=%d,paddbottom=%d}",
204                       i_padd - i_offset, i_padd + i_offset );
205         }
206     }
207     else
208     {
209         /* Crop */
210         if( fmt.video.i_height < i_height )
211         {
212             fmt.video.i_height = i_height;
213             fmt.video.i_width = ( p_filter->fmt_in.video.i_width * i_height )
214                                 / p_filter->fmt_in.video.i_height;
215             fmt.video.i_width = ( fmt.video.i_width * p_filter->fmt_in.video.i_aspect )
216                                 / i_aspect;
217             if( fmt.video.i_width & 1 ) fmt.video.i_width -= 1;
218
219             i_padd = (fmt.video.i_width - i_width) / 2;
220             i_offset =  (i_padd & 1);
221             /* Gruik */
222             snprintf( psz_croppadd, 100, "croppadd{cropleft=%d,cropright=%d}",
223                       i_padd - i_offset, i_padd + i_offset );
224         }
225         else
226         {
227             if( fmt.video.i_height & 1 ) fmt.video.i_height -= 1;
228             i_padd = (fmt.video.i_height - i_height) / 2;
229             i_offset = (i_padd & 1);
230             /* Gruik */
231             snprintf( psz_croppadd, 100, "croppadd{croptop=%d,cropbottom=%d}",
232                       i_padd - i_offset, i_padd + i_offset );
233         }
234     }
235
236     fmt.video.i_visible_width = fmt.video.i_width;
237     fmt.video.i_visible_height = fmt.video.i_height;
238
239     filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &fmt );
240     /* Append scaling module */
241     filter_chain_AppendFilter( p_sys->p_chain, NULL, NULL, NULL, NULL );
242     /* Append padding module */
243     filter_chain_AppendFromString( p_sys->p_chain, psz_croppadd );
244
245     fmt = *filter_chain_GetFmtOut( p_sys->p_chain );
246     es_format_Copy( &p_filter->fmt_out, &fmt );
247
248     p_filter->fmt_out.video.i_aspect = i_aspect * i_width / i_height;
249
250     if( p_filter->fmt_out.video.i_width != i_width
251      || p_filter->fmt_out.video.i_height != i_height )
252     {
253         msg_Warn( p_filter, "Looks like something went wrong. "
254                   "Output size is %dx%d while we asked for %dx%d",
255                   p_filter->fmt_out.video.i_width,
256                   p_filter->fmt_out.video.i_height,
257                   i_width, i_height );
258     }
259
260     p_filter->pf_video_filter = Filter;
261
262     return VLC_SUCCESS;
263 }
264
265 /*****************************************************************************
266  *
267  *****************************************************************************/
268 static void Destroy( vlc_object_t *p_this )
269 {
270     filter_t *p_filter = (filter_t *)p_this;
271     filter_chain_Delete( p_filter->p_sys->p_chain );
272     free( p_filter->p_sys );
273 }
274
275 /*****************************************************************************
276  *
277  *****************************************************************************/
278 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
279 {
280     return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );
281 }
282
283 /*****************************************************************************
284  *
285  *****************************************************************************/
286 static picture_t *video_new( filter_t *p_filter )
287 {
288     return filter_NewPicture( (filter_t*)p_filter->p_owner );
289 }
290
291 static void video_del( filter_t *p_filter, picture_t *p_pic )
292 {
293     return filter_DeletePicture( (filter_t*)p_filter->p_owner, p_pic );
294 }
295
296 static int alloc_init( filter_t *p_filter, void *p_data )
297 {
298     p_filter->p_owner = p_data;
299     p_filter->pf_vout_buffer_new = video_new;
300     p_filter->pf_vout_buffer_del = video_del;
301     return VLC_SUCCESS;
302 }