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_( \
54 #define CFG_PREFIX "canvas-"
56 /*****************************************************************************
58 *****************************************************************************/
60 set_description( N_("Automatically resize and padd a video") );
61 set_capability( "video filter2", 0 );
62 set_callbacks( Activate, Destroy );
64 add_integer_with_range( CFG_PREFIX "width", 0, 0, INT_MAX, NULL,
65 WIDTH_TEXT, WIDTH_LONGTEXT, false );
66 add_integer_with_range( CFG_PREFIX "height", 0, 0, INT_MAX, NULL,
67 HEIGHT_TEXT, HEIGHT_LONGTEXT, false );
70 static const char *const ppsz_filter_options[] = {
71 "width", "height", NULL
76 filter_chain_t *p_chain;
79 /*****************************************************************************
81 *****************************************************************************/
82 static int Activate( vlc_object_t *p_this )
84 filter_t *p_filter = (filter_t *)p_this;
85 unsigned int i_width, i_height;
87 char psz_croppadd[100];
90 if( !p_filter->b_allow_fmt_out_change )
92 msg_Err( p_filter, "Picture format change isn't allowed" );
96 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
98 msg_Err( p_filter, "Input and output chromas don't match" );
102 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
105 i_width = var_CreateGetInteger( p_filter, CFG_PREFIX "width" );
106 i_height = var_CreateGetInteger( p_filter, CFG_PREFIX "height" );
108 if( i_width == 0 || i_height == 0 )
110 msg_Err( p_filter, "Width and height options must be set" );
114 if( i_width & 1 || i_height & 1 )
116 msg_Err( p_filter, "Width and height options must be even integers" );
120 filter_sys_t *p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
123 p_filter->p_sys = p_sys;
125 p_sys->p_chain = filter_chain_New( p_filter, "video filter2", true,
126 alloc_init, NULL, p_filter );
127 if( !p_sys->p_chain )
129 msg_Err( p_filter, "Could not allocate filter chain" );
134 es_format_Copy( &fmt, &p_filter->fmt_in );
136 fmt.video.i_width = i_width;
137 fmt.video.i_height = ( p_filter->fmt_in.video.i_height * i_width )
138 / p_filter->fmt_in.video.i_width;
139 if( fmt.video.i_height > i_height )
141 fmt.video.i_height = i_height;
142 fmt.video.i_width = ( p_filter->fmt_in.video.i_width * i_height )
143 / p_filter->fmt_in.video.i_height;
144 if( fmt.video.i_width & 1 ) fmt.video.i_width -= 1;
145 i_padd = i_width - fmt.video.i_width;
147 snprintf( psz_croppadd, 100, "croppadd{paddleft=%d,paddright=%d}",
148 i_padd/2, (i_padd+1)/2 );
152 if( fmt.video.i_height & 1 ) fmt.video.i_height -= 1;
153 i_padd = i_height - fmt.video.i_height;
155 snprintf( psz_croppadd, 100, "croppadd{paddtop=%d,paddbottom=%d}",
156 i_padd/2, (i_padd+1)/2 );
159 fmt.video.i_visible_width = fmt.video.i_width;
160 fmt.video.i_visible_height = fmt.video.i_height;
162 filter_chain_Reset( p_sys->p_chain, &p_filter->fmt_in, &fmt );
163 /* Append scaling module */
164 filter_chain_AppendFilter( p_sys->p_chain, NULL, NULL, NULL, NULL );
165 /* Append padding module */
166 filter_chain_AppendFromString( p_sys->p_chain, psz_croppadd );
168 fmt = *filter_chain_GetFmtOut( p_sys->p_chain );
169 es_format_Copy( &p_filter->fmt_out, &fmt );
171 if( p_filter->fmt_out.video.i_width != i_width
172 || p_filter->fmt_out.video.i_height != i_height )
174 msg_Warn( p_filter, "Looks like something went wrong. "
175 "Output size is %dx%d while we asked for %dx%d",
176 p_filter->fmt_out.video.i_width,
177 p_filter->fmt_out.video.i_height,
181 p_filter->pf_video_filter = Filter;
186 /*****************************************************************************
188 *****************************************************************************/
189 static void Destroy( vlc_object_t *p_this )
191 filter_t *p_filter = (filter_t *)p_this;
192 filter_chain_Delete( p_filter->p_sys->p_chain );
193 free( p_filter->p_sys );
196 /*****************************************************************************
198 *****************************************************************************/
199 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
201 return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );
204 /*****************************************************************************
206 *****************************************************************************/
207 static picture_t *video_new( filter_t *p_filter )
209 return ((filter_t*)p_filter->p_owner)->pf_vout_buffer_new( (filter_t*)p_filter->p_owner );
212 static void video_del( filter_t *p_filter, picture_t *p_pic )
214 if( ((filter_t*)p_filter->p_owner)->pf_vout_buffer_del )
215 ((filter_t*)p_filter->p_owner)->pf_vout_buffer_del( (filter_t*)p_filter->p_owner, p_pic );
218 static int alloc_init( filter_t *p_filter, void *p_data )
220 p_filter->p_owner = p_data;
221 p_filter->pf_vout_buffer_new = video_new;
222 p_filter->pf_vout_buffer_del = video_del;