1 /*****************************************************************************
2 * canvas.c : automatically resize and padd a video to fit in canvas
3 *****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan dot org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_filter.h>
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 * );
47 #define WIDTH_TEXT N_( "Image width" )
48 #define WIDTH_LONGTEXT N_( \
50 #define HEIGHT_TEXT N_( "Image height" )
51 #define HEIGHT_LONGTEXT N_( \
53 #define ASPECT_TEXT N_( "Aspect ratio" )
54 #define ASPECT_LONGTEXT N_( \
55 "Set aspect (like 4:3) of the video canvas" )
57 #define CFG_PREFIX "canvas-"
59 /*****************************************************************************
61 *****************************************************************************/
63 set_description( N_("Automatically resize and padd a video") );
64 set_capability( "video filter2", 0 );
65 set_callbacks( Activate, Destroy );
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 );
72 add_string( CFG_PREFIX "aspect", "4:3", NULL,
73 ASPECT_TEXT, ASPECT_LONGTEXT, false );
76 static const char *const ppsz_filter_options[] = {
77 "width", "height", "aspect", NULL
82 filter_chain_t *p_chain;
85 /*****************************************************************************
87 *****************************************************************************/
88 static int Activate( vlc_object_t *p_this )
90 filter_t *p_filter = (filter_t *)p_this;
91 unsigned int i_width, i_height;
93 char psz_croppadd[100];
95 char *psz_aspect, *psz_parser;
98 if( !p_filter->b_allow_fmt_out_change )
100 msg_Err( p_filter, "Picture format change isn't allowed" );
104 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
106 msg_Err( p_filter, "Input and output chromas don't match" );
110 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
113 i_width = var_CreateGetInteger( p_filter, CFG_PREFIX "width" );
114 i_height = var_CreateGetInteger( p_filter, CFG_PREFIX "height" );
116 if( i_width == 0 || i_height == 0 )
118 msg_Err( p_filter, "Width and height options must be set" );
122 if( i_width & 1 || i_height & 1 )
124 msg_Err( p_filter, "Width and height options must be even integers" );
128 psz_aspect = var_CreateGetNonEmptyString( p_filter, CFG_PREFIX "aspect" );
131 msg_Err( p_filter, "Aspect ratio must be set" );
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 );
139 i_aspect = atof( psz_aspect ) * VOUT_ASPECT_FACTOR;
144 msg_Err( p_filter, "Aspect ratio must be strictly positive" );
148 filter_sys_t *p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
151 p_filter->p_sys = p_sys;
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 )
157 msg_Err( p_filter, "Could not allocate filter chain" );
162 es_format_Copy( &fmt, &p_filter->fmt_in );
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 )
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) / 2;
174 i_offset = (i_padd & 1);
176 snprintf( psz_croppadd, 100, "croppadd{paddleft=%d,paddright=%d}",
177 i_padd - i_offset, i_padd + i_offset );
181 if( fmt.video.i_height & 1 ) fmt.video.i_height -= 1;
182 i_padd = (i_height - fmt.video.i_height ) / 2;
183 i_offset = (i_padd & 1);
185 snprintf( psz_croppadd, 100, "croppadd{paddtop=%d,paddbottom=%d}",
186 i_padd - i_offset, i_padd + i_offset );
189 fmt.video.i_visible_width = fmt.video.i_width;
190 fmt.video.i_visible_height = fmt.video.i_height;
192 filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &fmt );
193 /* Append scaling module */
194 filter_chain_AppendFilter( p_sys->p_chain, NULL, NULL, NULL, NULL );
195 /* Append padding module */
196 filter_chain_AppendFromString( p_sys->p_chain, psz_croppadd );
198 fmt = *filter_chain_GetFmtOut( p_sys->p_chain );
199 es_format_Copy( &p_filter->fmt_out, &fmt );
201 p_filter->fmt_out.video.i_aspect = i_aspect * i_width / i_height;
203 if( p_filter->fmt_out.video.i_width != i_width
204 || p_filter->fmt_out.video.i_height != i_height )
206 msg_Warn( p_filter, "Looks like something went wrong. "
207 "Output size is %dx%d while we asked for %dx%d",
208 p_filter->fmt_out.video.i_width,
209 p_filter->fmt_out.video.i_height,
213 p_filter->pf_video_filter = Filter;
218 /*****************************************************************************
220 *****************************************************************************/
221 static void Destroy( vlc_object_t *p_this )
223 filter_t *p_filter = (filter_t *)p_this;
224 filter_chain_Delete( p_filter->p_sys->p_chain );
225 free( p_filter->p_sys );
228 /*****************************************************************************
230 *****************************************************************************/
231 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
233 return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );
236 /*****************************************************************************
238 *****************************************************************************/
239 static picture_t *video_new( filter_t *p_filter )
241 return ((filter_t*)p_filter->p_owner)->pf_vout_buffer_new( (filter_t*)p_filter->p_owner );
244 static void video_del( filter_t *p_filter, picture_t *p_pic )
246 if( ((filter_t*)p_filter->p_owner)->pf_vout_buffer_del )
247 ((filter_t*)p_filter->p_owner)->pf_vout_buffer_del( (filter_t*)p_filter->p_owner, p_pic );
250 static int alloc_init( filter_t *p_filter, void *p_data )
252 p_filter->p_owner = p_data;
253 p_filter->pf_vout_buffer_new = video_new;
254 p_filter->pf_vout_buffer_del = video_del;