]> git.sesse.net Git - vlc/blob - modules/video_filter/canvas.c
New canvas video filter.
[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
54 #define CFG_PREFIX "canvas-"
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59 vlc_module_begin();
60     set_description( N_("Automatically resize and padd a video") );
61     set_capability( "video filter2", 0 );
62     set_callbacks( Activate, Destroy );
63
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 );
68 vlc_module_end();
69
70 static const char *const ppsz_filter_options[] = {
71     "width", "height", NULL
72 };
73
74 struct filter_sys_t
75 {
76     filter_chain_t *p_chain;
77 };
78
79 /*****************************************************************************
80  *
81  *****************************************************************************/
82 static int Activate( vlc_object_t *p_this )
83 {
84     filter_t *p_filter = (filter_t *)p_this;
85     unsigned int i_width, i_height;
86     es_format_t fmt;
87     char psz_croppadd[100];
88     int i_padd;
89
90     if( !p_filter->b_allow_fmt_out_change )
91     {
92         msg_Err( p_filter, "Picture format change isn't allowed" );
93         return VLC_EGENERIC;
94     }
95
96     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
97     {
98         msg_Err( p_filter, "Input and output chromas don't match" );
99         return VLC_EGENERIC;
100     }
101
102     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
103                        p_filter->p_cfg );
104
105     i_width = var_CreateGetInteger( p_filter, CFG_PREFIX "width" );
106     i_height = var_CreateGetInteger( p_filter, CFG_PREFIX "height" );
107
108     if( i_width == 0 || i_height == 0 )
109     {
110         msg_Err( p_filter, "Width and height options must be set" );
111         return VLC_EGENERIC;
112     }
113
114     if( i_width & 1 || i_height & 1 )
115     {
116         msg_Err( p_filter, "Width and height options must be even integers" );
117         return VLC_EGENERIC;
118     }
119
120     filter_sys_t *p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
121     if( !p_sys )
122         return VLC_ENOMEM;
123     p_filter->p_sys = p_sys;
124
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 )
128     {
129         msg_Err( p_filter, "Could not allocate filter chain" );
130         free( p_sys );
131         return VLC_EGENERIC;
132     }
133
134     es_format_Copy( &fmt, &p_filter->fmt_in );
135
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 )
140     {
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;
146         /* Gruik */
147         snprintf( psz_croppadd, 100, "croppadd{paddleft=%d,paddright=%d}",
148                   i_padd/2, (i_padd+1)/2 );
149     }
150     else
151     {
152         if( fmt.video.i_height & 1 ) fmt.video.i_height -= 1;
153         i_padd = i_height - fmt.video.i_height;
154         /* Gruik */
155         snprintf( psz_croppadd, 100, "croppadd{paddtop=%d,paddbottom=%d}",
156                   i_padd/2, (i_padd+1)/2 );
157     }
158
159     fmt.video.i_visible_width = fmt.video.i_width;
160     fmt.video.i_visible_height = fmt.video.i_height;
161
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 );
167
168     fmt = *filter_chain_GetFmtOut( p_sys->p_chain );
169     es_format_Copy( &p_filter->fmt_out, &fmt );
170
171     if( p_filter->fmt_out.video.i_width != i_width
172      || p_filter->fmt_out.video.i_height != i_height )
173     {
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,
178                   i_width, i_height );
179     }
180
181     p_filter->pf_video_filter = Filter;
182
183     return VLC_SUCCESS;
184 }
185
186 /*****************************************************************************
187  *
188  *****************************************************************************/
189 static void Destroy( vlc_object_t *p_this )
190 {
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 );
194 }
195
196 /*****************************************************************************
197  *
198  *****************************************************************************/
199 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
200 {
201     return filter_chain_VideoFilter( p_filter->p_sys->p_chain, p_pic );
202 }
203
204 /*****************************************************************************
205  *
206  *****************************************************************************/
207 static picture_t *video_new( filter_t *p_filter )
208 {
209     return ((filter_t*)p_filter->p_owner)->pf_vout_buffer_new( (filter_t*)p_filter->p_owner );
210 }
211
212 static void video_del( filter_t *p_filter, picture_t *p_pic )
213 {
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 );
216 }
217
218 static int alloc_init( filter_t *p_filter, void *p_data )
219 {
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;
223     return VLC_SUCCESS;
224 }