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