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