1 /*****************************************************************************
2 * croppadd.c: Crop/Padd image filter
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
7 * Authors: Antoine Cellerier <dionoea @t videolan dot org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <limits.h> /* INT_MAX */
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_filter.h>
36 #include <vlc_picture.h>
37 #include "filter_picture.h"
39 /****************************************************************************
41 ****************************************************************************/
42 static int OpenFilter ( vlc_object_t
* );
43 static void CloseFilter( vlc_object_t
* );
45 static picture_t
*Filter( filter_t
*, picture_t
* );
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." )
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." )
73 #define CFG_PREFIX "croppadd-"
75 /*****************************************************************************
77 *****************************************************************************/
79 set_shortname( N_("Croppadd") )
80 set_description( N_("Video cropping filter") )
81 set_capability( "video filter", 0 )
82 set_callbacks( OpenFilter
, CloseFilter
)
84 set_category( CAT_VIDEO
)
85 set_subcategory( SUBCAT_VIDEO_VFILTER
);
87 set_section( N_("Crop"), NULL
)
88 add_integer_with_range( CFG_PREFIX
"croptop", 0, 0, INT_MAX
,
89 CROPTOP_TEXT
, CROPTOP_LONGTEXT
, false )
90 add_integer_with_range( CFG_PREFIX
"cropbottom", 0, 0, INT_MAX
,
91 CROPBOTTOM_TEXT
, CROPBOTTOM_LONGTEXT
, false )
92 add_integer_with_range( CFG_PREFIX
"cropleft", 0, 0, INT_MAX
,
93 CROPLEFT_TEXT
, CROPLEFT_LONGTEXT
, false )
94 add_integer_with_range( CFG_PREFIX
"cropright", 0, 0, INT_MAX
,
95 CROPRIGHT_TEXT
, CROPRIGHT_LONGTEXT
, false )
97 set_section( N_("Padd"), NULL
)
98 add_integer_with_range( CFG_PREFIX
"paddtop", 0, 0, INT_MAX
,
99 PADDTOP_TEXT
, PADDTOP_LONGTEXT
, false )
100 add_integer_with_range( CFG_PREFIX
"paddbottom", 0, 0, INT_MAX
,
101 PADDBOTTOM_TEXT
, PADDBOTTOM_LONGTEXT
, false )
102 add_integer_with_range( CFG_PREFIX
"paddleft", 0, 0, INT_MAX
,
103 PADDLEFT_TEXT
, PADDLEFT_LONGTEXT
, false )
104 add_integer_with_range( CFG_PREFIX
"paddright", 0, 0, INT_MAX
,
105 PADDRIGHT_TEXT
, PADDRIGHT_LONGTEXT
, false )
108 static const char *const ppsz_filter_options
[] = {
109 "croptop", "cropbottom", "cropleft", "cropright",
110 "paddtop", "paddbottom", "paddleft", "paddright",
126 /*****************************************************************************
127 * OpenFilter: probe the filter and return score
128 *****************************************************************************/
129 static int OpenFilter( vlc_object_t
*p_this
)
131 filter_t
*p_filter
= (filter_t
*)p_this
;
134 if( !p_filter
->b_allow_fmt_out_change
)
136 msg_Err( p_filter
, "Picture format change isn't allowed" );
140 if( p_filter
->fmt_in
.video
.i_chroma
!= p_filter
->fmt_out
.video
.i_chroma
)
142 msg_Err( p_filter
, "Input and output chromas don't match" );
143 /* In fact we don't really care about this since we're allowed
144 * to change the output format ... FIXME? */
148 const vlc_chroma_description_t
*p_chroma
=
149 vlc_fourcc_GetChromaDescription( p_filter
->fmt_in
.video
.i_chroma
);
150 if( p_chroma
== NULL
|| p_chroma
->plane_count
== 0 )
152 msg_Err( p_filter
, "Unknown input chroma %4.4s", p_filter
->fmt_in
.video
.i_chroma
?
153 (const char*)&p_filter
->fmt_in
.video
.i_chroma
: "xxxx" );
157 p_filter
->p_sys
= (filter_sys_t
*)malloc( sizeof( filter_sys_t
) );
158 if( !p_filter
->p_sys
) return VLC_ENOMEM
;
160 config_ChainParse( p_filter
, CFG_PREFIX
, ppsz_filter_options
,
163 p_sys
= p_filter
->p_sys
;
164 #define GET_OPTION( name ) \
165 p_sys->i_ ## name = var_CreateGetInteger( p_filter, CFG_PREFIX #name ); \
166 if( p_sys->i_ ## name & 1 ) \
167 msg_Warn( p_filter, "Using even values for `" #name "' is recommended" );
168 GET_OPTION( croptop
)
169 GET_OPTION( cropbottom
)
170 GET_OPTION( cropleft
)
171 GET_OPTION( cropright
)
172 GET_OPTION( paddtop
)
173 GET_OPTION( paddbottom
)
174 GET_OPTION( paddleft
)
175 GET_OPTION( paddright
)
177 p_filter
->fmt_out
.video
.i_height
=
178 p_filter
->fmt_out
.video
.i_visible_height
=
179 p_filter
->fmt_in
.video
.i_visible_height
180 - p_sys
->i_croptop
- p_sys
->i_cropbottom
181 + p_sys
->i_paddtop
+ p_sys
->i_paddbottom
;
183 p_filter
->fmt_out
.video
.i_width
=
184 p_filter
->fmt_out
.video
.i_visible_width
=
185 p_filter
->fmt_in
.video
.i_visible_width
186 - p_sys
->i_cropleft
- p_sys
->i_cropright
187 + p_sys
->i_paddleft
+ p_sys
->i_paddright
;
189 p_filter
->pf_video_filter
= Filter
;
191 msg_Dbg( p_filter
, "Crop: Top: %d, Bottom: %d, Left: %d, Right: %d",
192 p_sys
->i_croptop
, p_sys
->i_cropbottom
, p_sys
->i_cropleft
,
193 p_sys
->i_cropright
);
194 msg_Dbg( p_filter
, "Padd: Top: %d, Bottom: %d, Left: %d, Right: %d",
195 p_sys
->i_paddtop
, p_sys
->i_paddbottom
, p_sys
->i_paddleft
,
196 p_sys
->i_paddright
);
197 msg_Dbg( p_filter
, "%dx%d -> %dx%d",
198 p_filter
->fmt_in
.video
.i_width
,
199 p_filter
->fmt_in
.video
.i_height
,
200 p_filter
->fmt_out
.video
.i_width
,
201 p_filter
->fmt_out
.video
.i_height
);
206 /*****************************************************************************
207 * CloseFilter: clean up the filter
208 *****************************************************************************/
209 static void CloseFilter( vlc_object_t
*p_this
)
211 filter_t
*p_filter
= (filter_t
*)p_this
;
212 free( p_filter
->p_sys
);
215 /****************************************************************************
216 * Filter: the whole thing
217 ****************************************************************************/
218 static picture_t
*Filter( filter_t
*p_filter
, picture_t
*p_pic
)
220 filter_sys_t
*p_sys
= p_filter
->p_sys
;
222 int i_width
, i_height
, i_xcrop
, i_ycrop
,
223 i_outwidth
, i_outheight
, i_xpadd
, i_ypadd
;
225 const int p_padd_color
[] = { 0x00, 0x80, 0x80, 0xff };
227 if( !p_pic
) return NULL
;
229 /* Request output picture */
230 p_outpic
= filter_NewPicture( p_filter
);
233 picture_Release( p_pic
);
237 for( int i_plane
= 0; i_plane
< p_pic
->i_planes
; i_plane
++ )
238 /* p_pic and p_outpic have the same chroma/number of planes but that's
241 plane_t
*p_plane
= p_pic
->p
+i_plane
;
242 plane_t
*p_outplane
= p_outpic
->p
+i_plane
;
243 uint8_t *p_in
= p_plane
->p_pixels
;
244 uint8_t *p_out
= p_outplane
->p_pixels
;
245 int i_pixel_pitch
= p_plane
->i_pixel_pitch
;
246 int i_padd_color
= i_plane
> 3 ? p_padd_color
[0]
247 : p_padd_color
[i_plane
];
249 /* These assignments assume that the first plane always has
250 * a width and height equal to the picture's */
251 i_width
= ( ( p_filter
->fmt_in
.video
.i_visible_width
252 - p_sys
->i_cropleft
- p_sys
->i_cropright
)
253 * p_plane
->i_visible_pitch
)
254 / p_pic
->p
->i_visible_pitch
;
255 i_height
= ( ( p_filter
->fmt_in
.video
.i_visible_height
256 - p_sys
->i_croptop
- p_sys
->i_cropbottom
)
257 * p_plane
->i_visible_lines
)
258 / p_pic
->p
->i_visible_lines
;
259 i_xcrop
= ( p_sys
->i_cropleft
* p_plane
->i_visible_pitch
)
260 / p_pic
->p
->i_visible_pitch
;
261 i_ycrop
= ( p_sys
->i_croptop
* p_plane
->i_visible_lines
)
262 / p_pic
->p
->i_visible_lines
;
263 i_outwidth
= ( p_filter
->fmt_out
.video
.i_visible_width
264 * p_outplane
->i_visible_pitch
)
265 / p_outpic
->p
->i_visible_pitch
;
266 i_outheight
= ( p_filter
->fmt_out
.video
.i_visible_height
267 * p_outplane
->i_visible_lines
)
268 / p_outpic
->p
->i_visible_lines
;
269 i_xpadd
= ( p_sys
->i_paddleft
* p_outplane
->i_visible_pitch
)
270 / p_outpic
->p
->i_visible_pitch
;
271 i_ypadd
= ( p_sys
->i_paddtop
* p_outplane
->i_visible_lines
)
272 / p_outpic
->p
->i_visible_lines
;
275 p_in
+= i_ycrop
* p_plane
->i_pitch
;
277 /* Padd on the top */
278 memset( p_out
, i_padd_color
, i_ypadd
* p_outplane
->i_pitch
);
279 p_out
+= i_ypadd
* p_outplane
->i_pitch
;
281 for( int i_line
= 0; i_line
< i_height
; i_line
++ )
283 uint8_t *p_in_next
= p_in
+ p_plane
->i_pitch
;
284 uint8_t *p_out_next
= p_out
+ p_outplane
->i_pitch
;
286 /* Crop on the left */
287 p_in
+= i_xcrop
* i_pixel_pitch
;
289 /* Padd on the left */
290 memset( p_out
, i_padd_color
, i_xpadd
* i_pixel_pitch
);
291 p_out
+= i_xpadd
* i_pixel_pitch
;
293 /* Copy the image and crop on the right */
294 memcpy( p_out
, p_in
, i_width
* i_pixel_pitch
);
295 p_out
+= i_width
* i_pixel_pitch
;
296 p_in
+= i_width
* i_pixel_pitch
;
298 /* Padd on the right */
299 memset( p_out
, i_padd_color
,
300 ( i_outwidth
- i_xpadd
- i_width
) * i_pixel_pitch
);
302 /* Got to begining of the next line */
307 /* Padd on the bottom */
308 memset( p_out
, i_padd_color
,
309 ( i_outheight
- i_ypadd
- i_height
) * p_outplane
->i_pitch
);
312 return CopyInfoAndRelease( p_outpic
, p_pic
);