]> git.sesse.net Git - vlc/blob - modules/video_filter/croppadd.c
Fix croppadd out of bounds write (we were padding 'paddleft+paddright' pixels on...
[vlc] / modules / video_filter / croppadd.c
1 /*****************************************************************************
2  * croppadd.c: Crop/Padd image filter
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea @t 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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <limits.h> /* INT_MAX */
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_vout.h>
36 #include "vlc_filter.h"
37 #include "filter_picture.h"
38
39 /****************************************************************************
40  * Local prototypes
41  ****************************************************************************/
42 static int  OpenFilter ( vlc_object_t * );
43 static void CloseFilter( vlc_object_t * );
44
45 static picture_t *Filter( filter_t *, picture_t * );
46
47 #define CROPTOP_TEXT N_( "Pixels to crop from top" )
48 #define CROPTOP_LONGTEXT N_( \
49     "Number of pixels to crop from the top of the image." )
50 #define CROPBOTTOM_TEXT N_( "Pixels to crop from bottom" )
51 #define CROPBOTTOM_LONGTEXT N_( \
52     "Number of pixels to crop from the bottom of the image." )
53 #define CROPLEFT_TEXT N_( "Pixels to crop from left" )
54 #define CROPLEFT_LONGTEXT N_( \
55     "Number of pixels to crop from the left of the image." )
56 #define CROPRIGHT_TEXT N_( "Pixels to crop from right" )
57 #define CROPRIGHT_LONGTEXT N_( \
58     "Number of pixels to crop from the right of the image." )
59
60 #define PADDTOP_TEXT N_( "Pixels to padd to top" )
61 #define PADDTOP_LONGTEXT N_( \
62     "Number of pixels to padd to the top of the image after cropping." )
63 #define PADDBOTTOM_TEXT N_( "Pixels to padd to bottom" )
64 #define PADDBOTTOM_LONGTEXT N_( \
65     "Number of pixels to padd to the bottom of the image after cropping." )
66 #define PADDLEFT_TEXT N_( "Pixels to padd to left" )
67 #define PADDLEFT_LONGTEXT N_( \
68     "Number of pixels to padd to the left of the image after cropping." )
69 #define PADDRIGHT_TEXT N_( "Pixels to padd to right" )
70 #define PADDRIGHT_LONGTEXT N_( \
71     "Number of pixels to padd to the right of the image after cropping." )
72
73 #define CFG_PREFIX "croppadd-"
74
75 /*****************************************************************************
76  * Module descriptor
77  *****************************************************************************/
78 vlc_module_begin();
79     set_description( N_("Video scaling filter") );
80     set_capability( "video filter2", 0 );
81     set_callbacks( OpenFilter, CloseFilter );
82
83     set_section( N_("Crop"), NULL );
84         add_integer_with_range( CFG_PREFIX "croptop", 0, 0, INT_MAX, NULL,
85                                 CROPTOP_TEXT, CROPTOP_LONGTEXT, false );
86         add_integer_with_range( CFG_PREFIX "cropbottom", 0, 0, INT_MAX, NULL,
87                                 CROPBOTTOM_TEXT, CROPBOTTOM_LONGTEXT, false );
88         add_integer_with_range( CFG_PREFIX "cropleft", 0, 0, INT_MAX, NULL,
89                                 CROPLEFT_TEXT, CROPLEFT_LONGTEXT, false );
90         add_integer_with_range( CFG_PREFIX "cropright", 0, 0, INT_MAX, NULL,
91                                 CROPRIGHT_TEXT, CROPRIGHT_LONGTEXT, false );
92
93     set_section( N_("Padd"), NULL );
94         add_integer_with_range( CFG_PREFIX "paddtop", 0, 0, INT_MAX, NULL,
95                                 PADDTOP_TEXT, PADDTOP_LONGTEXT, false );
96         add_integer_with_range( CFG_PREFIX "paddbottom", 0, 0, INT_MAX, NULL,
97                                 PADDBOTTOM_TEXT, PADDBOTTOM_LONGTEXT, false );
98         add_integer_with_range( CFG_PREFIX "paddleft", 0, 0, INT_MAX, NULL,
99                                 PADDLEFT_TEXT, PADDLEFT_LONGTEXT, false );
100         add_integer_with_range( CFG_PREFIX "paddright", 0, 0, INT_MAX, NULL,
101                                 PADDRIGHT_TEXT, PADDRIGHT_LONGTEXT, false );
102 vlc_module_end();
103
104 static const char *const ppsz_filter_options[] = {
105     "croptop", "cropbottom", "cropleft", "cropright",
106     "paddtop", "paddbottom", "paddleft", "paddright",
107     NULL
108 };
109
110 struct filter_sys_t
111 {
112     int i_croptop;
113     int i_cropbottom;
114     int i_cropleft;
115     int i_cropright;
116     int i_paddtop;
117     int i_paddbottom;
118     int i_paddleft;
119     int i_paddright;
120 };
121
122 /*****************************************************************************
123  * OpenFilter: probe the filter and return score
124  *****************************************************************************/
125 static int OpenFilter( vlc_object_t *p_this )
126 {
127     filter_t *p_filter = (filter_t*)p_this;
128     filter_sys_t *p_sys;
129
130     if( !p_filter->b_allow_fmt_out_change )
131     {
132         msg_Err( p_filter, "Picture format change isn't allowed" );
133         return VLC_EGENERIC;
134     }
135
136     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
137     {
138         msg_Err( p_filter, "Input and output chromas don't match" );
139         /* In fact we don't really care about this since we're allowed
140          * to change the output format ... FIXME? */
141         return VLC_EGENERIC;
142     }
143
144     p_filter->p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
145     if( !p_filter->p_sys ) return VLC_ENOMEM;
146
147     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
148                        p_filter->p_cfg );
149
150     p_sys = p_filter->p_sys;
151 #define GET_OPTION( name ) \
152     p_sys->i_ ## name = var_CreateGetInteger( p_filter, CFG_PREFIX #name ); \
153     if( p_sys->i_ ## name & 1 ) \
154         msg_Warn( p_filter, "Using even values for `" #name "' is recommended" );
155     GET_OPTION( croptop )
156     GET_OPTION( cropbottom )
157     GET_OPTION( cropleft )
158     GET_OPTION( cropright )
159     GET_OPTION( paddtop )
160     GET_OPTION( paddbottom )
161     GET_OPTION( paddleft )
162     GET_OPTION( paddright )
163
164     p_filter->fmt_out.video.i_height =
165     p_filter->fmt_out.video.i_visible_height =
166         p_filter->fmt_in.video.i_visible_height
167         - p_sys->i_croptop - p_sys->i_cropbottom
168         + p_sys->i_paddtop + p_sys->i_paddbottom;
169
170     p_filter->fmt_out.video.i_width =
171     p_filter->fmt_out.video.i_visible_width =
172         p_filter->fmt_in.video.i_visible_width
173         - p_sys->i_cropleft - p_sys->i_cropright
174         + p_sys->i_paddleft + p_sys->i_paddright;
175
176     p_filter->pf_video_filter = Filter;
177
178     msg_Dbg( p_filter, "Crop: Top: %d, Bottom: %d, Left: %d, Right: %d",
179              p_sys->i_croptop, p_sys->i_cropbottom, p_sys->i_cropleft,
180              p_sys->i_cropright );
181     msg_Dbg( p_filter, "Padd: Top: %d, Bottom: %d, Left: %d, Right: %d",
182              p_sys->i_paddtop, p_sys->i_paddbottom, p_sys->i_paddleft,
183              p_sys->i_paddright );
184     msg_Dbg( p_filter, "%dx%d -> %dx%d",
185              p_filter->fmt_in.video.i_width,
186              p_filter->fmt_in.video.i_height,
187              p_filter->fmt_out.video.i_width,
188              p_filter->fmt_out.video.i_height );
189
190     return VLC_SUCCESS;
191 }
192
193 /*****************************************************************************
194  * CloseFilter: clean up the filter
195  *****************************************************************************/
196 static void CloseFilter( vlc_object_t *p_this )
197 {
198     filter_t *p_filter = (filter_t *)p_this;
199     free( p_filter->p_sys );
200 }
201
202 /****************************************************************************
203  * Filter: the whole thing
204  ****************************************************************************/
205 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
206 {
207     filter_sys_t *p_sys = p_filter->p_sys;
208     picture_t *p_outpic;
209     int i_plane;
210     int i_width, i_height, i_xcrop, i_ycrop,
211         i_outwidth, i_outheight, i_xpadd, i_ypadd;
212
213     const int p_padd_color[] = { 0x00, 0x80, 0x80, 0xff };
214
215     if( !p_pic ) return NULL;
216
217     /* Request output picture */
218     p_outpic = filter_NewPicture( p_filter );
219     if( !p_outpic )
220     {
221         picture_Release( p_pic );
222         return NULL;
223     }
224
225     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
226     /* p_pic and p_outpic have the same chroma/number of planes but that's
227      * about it. */
228     {
229         plane_t *p_plane = p_pic->p+i_plane;
230         plane_t *p_outplane = p_outpic->p+i_plane;
231         uint8_t *p_in = p_plane->p_pixels;
232         uint8_t *p_out = p_outplane->p_pixels;
233         int i_pixel_pitch = p_plane->i_pixel_pitch;
234         int i_padd_color = i_plane > 3 ? p_padd_color[0]
235                                        : p_padd_color[i_plane];
236
237         /* These assignments assume that the first plane always has
238          * a width and height equal to the picture's */
239         i_width =     ( ( p_filter->fmt_in.video.i_visible_width
240                           - p_sys->i_cropleft - p_sys->i_cropright )
241                         * p_plane->i_visible_pitch )
242                       / p_pic->p->i_visible_pitch;
243         i_height =    ( ( p_filter->fmt_in.video.i_visible_height
244                           - p_sys->i_croptop - p_sys->i_cropbottom )
245                         * p_plane->i_visible_lines )
246                       / p_pic->p->i_visible_lines;
247         i_xcrop =     ( p_sys->i_cropleft * p_plane->i_visible_pitch)
248                       / p_pic->p->i_visible_pitch;
249         i_ycrop =     ( p_sys->i_croptop * p_plane->i_visible_lines)
250                       / p_pic->p->i_visible_lines;
251         i_outwidth =  ( p_filter->fmt_out.video.i_visible_width
252                         * p_outplane->i_visible_pitch )
253                       / p_outpic->p->i_visible_pitch;
254         i_outheight = ( p_filter->fmt_out.video.i_visible_height
255                         * p_outplane->i_visible_lines )
256                       / p_outpic->p->i_visible_lines;
257         i_xpadd =     ( p_sys->i_paddleft * p_outplane->i_visible_pitch )
258                       / p_outpic->p->i_visible_pitch;
259         i_ypadd =     ( p_sys->i_paddtop * p_outplane->i_visible_lines )
260                        / p_outpic->p->i_visible_lines;
261
262         /* Crop the top */
263         p_in += i_ycrop * p_plane->i_pitch;
264
265         /* Padd on the top */
266         vlc_memset( p_out, i_padd_color, i_ypadd * p_outplane->i_pitch );
267         p_out += i_ypadd * p_outplane->i_pitch;
268
269         int i_line;
270         for( i_line = 0; i_line < i_height; i_line++ )
271         {
272             uint8_t *p_in_next = p_in + p_plane->i_pitch;
273             uint8_t *p_out_next = p_out + p_outplane->i_pitch;
274
275             /* Crop on the left */
276             p_in += i_xcrop * i_pixel_pitch;
277
278             /* Padd on the left */
279             vlc_memset( p_out, i_padd_color, i_xpadd * i_pixel_pitch );
280             p_out += i_xpadd * i_pixel_pitch;
281
282             /* Copy the image and crop on the right */
283             vlc_memcpy( p_out, p_in, i_width * i_pixel_pitch );
284             p_out += i_width * i_pixel_pitch;
285             p_in += i_width * i_pixel_pitch;
286
287             /* Padd on the right */
288             vlc_memset( p_out, i_padd_color,
289                         ( i_outwidth - i_xpadd - i_width ) * i_pixel_pitch );
290
291             /* Got to begining of the next line */
292             p_in = p_in_next;
293             p_out = p_out_next;
294         }
295
296         /* Padd on the bottom */
297         vlc_memset( p_out, i_padd_color,
298                  ( i_outheight - i_ypadd - i_height ) * p_outplane->i_pitch );
299     }
300
301     return CopyInfoAndRelease( p_outpic, p_pic );
302 }