1 /*****************************************************************************
2 * crop.c : Crop video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: crop.c,v 1.3 2002/11/23 02:40:30 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
33 #include "filter_common.h"
35 /*****************************************************************************
37 *****************************************************************************/
38 static int Create ( vlc_object_t * );
39 static void Destroy ( vlc_object_t * );
41 static int Init ( vout_thread_t * );
42 static void End ( vout_thread_t * );
43 static int Manage ( vout_thread_t * );
44 static void Render ( vout_thread_t *, picture_t * );
46 static void UpdateStats ( vout_thread_t *, picture_t * );
48 /*****************************************************************************
50 *****************************************************************************/
51 #define GEOMETRY_TEXT N_("crop geometry")
52 #define GEOMETRY_LONGTEXT N_("Set the geometry of the zone to crop")
54 #define AUTOCROP_TEXT N_("automatic cropping")
55 #define AUTOCROP_LONGTEXT N_("Activate automatic black border cropping")
58 add_category_hint( N_("Miscellaneous"), NULL );
59 add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT, GEOMETRY_LONGTEXT );
60 add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT, AUTOCROP_LONGTEXT );
61 set_description( _("image crop video module") );
62 set_capability( "video filter", 0 );
63 add_shortcut( "crop" );
64 set_callbacks( Create, Destroy );
67 /*****************************************************************************
68 * vout_sys_t: Crop video output method descriptor
69 *****************************************************************************
70 * This structure is part of the video output thread descriptor.
71 * It describes the Crop specific properties of an output thread.
72 *****************************************************************************/
75 vout_thread_t *p_vout;
77 unsigned int i_x, i_y;
78 unsigned int i_width, i_height, i_aspect;
80 vlc_bool_t b_autocrop;
82 /* Autocrop specific variables */
83 unsigned int i_lastchange;
87 /*****************************************************************************
88 * Create: allocates Crop video thread output method
89 *****************************************************************************
90 * This function allocates and initializes a Crop vout method.
91 *****************************************************************************/
92 static int Create( vlc_object_t *p_this )
94 vout_thread_t *p_vout = (vout_thread_t *)p_this;
96 /* Allocate structure */
97 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
98 if( p_vout->p_sys == NULL )
100 msg_Err( p_vout, "out of memory" );
104 p_vout->pf_init = Init;
105 p_vout->pf_end = End;
106 p_vout->pf_manage = Manage;
107 p_vout->pf_render = Render;
108 p_vout->pf_display = NULL;
113 /*****************************************************************************
114 * Init: initialize Crop video thread output method
115 *****************************************************************************/
116 static int Init( vout_thread_t *p_vout )
122 I_OUTPUTPICTURES = 0;
124 p_vout->p_sys->i_lastchange = 0;
125 p_vout->p_sys->b_changed = 0;
127 /* Initialize the output structure */
128 p_vout->output.i_chroma = p_vout->render.i_chroma;
129 p_vout->output.i_width = p_vout->render.i_width;
130 p_vout->output.i_height = p_vout->render.i_height;
131 p_vout->output.i_aspect = p_vout->render.i_aspect;
133 /* Shall we use autocrop ? */
134 p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
136 /* Get geometry value from the user */
137 psz_var = config_GetPsz( p_vout, "crop-geometry" );
140 char *psz_parser, *psz_tmp;
142 psz_parser = psz_tmp = psz_var;
143 while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
148 p_vout->p_sys->i_width = atoi( psz_parser );
150 psz_parser = ++psz_tmp;
151 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
156 p_vout->p_sys->i_height = atoi( psz_parser );
158 psz_parser = ++psz_tmp;
159 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
164 p_vout->p_sys->i_x = atoi( psz_parser );
165 p_vout->p_sys->i_y = atoi( ++psz_tmp );
169 p_vout->p_sys->i_x = atoi( psz_parser );
171 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
176 p_vout->p_sys->i_height = atoi( psz_parser );
178 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
180 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
185 p_vout->p_sys->i_width = atoi( psz_parser );
186 p_vout->p_sys->i_height = p_vout->output.i_height;
188 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
190 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
193 /* Check for validity */
194 if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
195 > p_vout->output.i_width )
197 p_vout->p_sys->i_x = 0;
198 if( p_vout->p_sys->i_width > p_vout->output.i_width )
200 p_vout->p_sys->i_width = p_vout->output.i_width;
204 if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
205 > p_vout->output.i_height )
207 p_vout->p_sys->i_y = 0;
208 if( p_vout->p_sys->i_height > p_vout->output.i_height )
210 p_vout->p_sys->i_height = p_vout->output.i_height;
218 p_vout->p_sys->i_width = p_vout->output.i_width;
219 p_vout->p_sys->i_height = p_vout->output.i_height;
220 p_vout->p_sys->i_x = p_vout->p_sys->i_y = 0;
223 /* Pheeew. Parsing done. */
224 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
225 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
226 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
227 p_vout->p_sys->b_autocrop ? "" : "not " );
229 /* Set current output image properties */
230 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
231 * p_vout->output.i_height / p_vout->p_sys->i_height
232 * p_vout->p_sys->i_width / p_vout->output.i_width;
234 /* Try to open the real video output */
235 p_vout->p_sys->p_vout =
236 vout_CreateThread( p_vout,
237 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
238 p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
239 if( p_vout->p_sys->p_vout == NULL )
241 msg_Err( p_vout, "failed to create vout" );
245 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
250 /*****************************************************************************
251 * End: terminate Crop video thread output method
252 *****************************************************************************/
253 static void End( vout_thread_t *p_vout )
257 /* Free the fake output buffers we allocated */
258 for( i_index = I_OUTPUTPICTURES ; i_index ; )
261 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
265 /*****************************************************************************
266 * Destroy: destroy Crop video thread output method
267 *****************************************************************************
268 * Terminate an output method created by CropCreateOutputMethod
269 *****************************************************************************/
270 static void Destroy( vlc_object_t *p_this )
272 vout_thread_t *p_vout = (vout_thread_t *)p_this;
274 vout_DestroyThread( p_vout->p_sys->p_vout );
275 free( p_vout->p_sys );
278 /*****************************************************************************
279 * Manage: handle Crop events
280 *****************************************************************************
281 * This function should be called regularly by video output thread. It manages
282 * console events. It returns a non null value on error.
283 *****************************************************************************/
284 static int Manage( vout_thread_t *p_vout )
286 if( !p_vout->p_sys->b_changed )
291 vout_DestroyThread( p_vout->p_sys->p_vout );
293 p_vout->p_sys->p_vout =
294 vout_CreateThread( p_vout,
295 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
296 p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
297 if( p_vout->p_sys->p_vout == NULL )
299 msg_Err( p_vout, "failed to create vout" );
303 p_vout->p_sys->b_changed = 0;
304 p_vout->p_sys->i_lastchange = 0;
309 /*****************************************************************************
310 * Render: display previously rendered output
311 *****************************************************************************
312 * This function sends the currently rendered image to Crop image, waits
313 * until it is displayed and switches the two rendering buffers, preparing next
315 *****************************************************************************/
316 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
318 picture_t *p_outpic = NULL;
321 if( p_vout->p_sys->b_changed )
327 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
330 if( p_vout->b_die || p_vout->b_error )
332 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
336 msleep( VOUT_OUTMEM_SLEEP );
339 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
340 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
342 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
344 u8 *p_in, *p_out, *p_out_end;
345 int i_in_pitch = p_pic->p[i_plane].i_pitch;
346 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
348 p_in = p_pic->p[i_plane].p_pixels
349 /* Skip the right amount of lines */
350 + i_in_pitch * ( p_pic->p[i_plane].i_lines * p_vout->p_sys->i_y
351 / p_vout->output.i_height )
352 /* Skip the right amount of columns */
353 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
355 p_out = p_outpic->p[i_plane].p_pixels;
356 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_lines;
358 while( p_out < p_out_end )
360 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_out_pitch );
362 p_out += i_out_pitch;
366 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
367 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
369 /* The source image may still be in the cache ... parse it! */
370 if( !p_vout->p_sys->b_autocrop )
375 UpdateStats( p_vout, p_pic );
378 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
380 u8 *p_in = p_pic->p[0].p_pixels;
381 int i_pitch = p_pic->p[0].i_pitch;
382 int i_lines = p_pic->p[0].i_lines;
383 int i_firstwhite = -1, i_lastwhite = -1, i;
385 /* Determine where black borders are */
386 switch( p_vout->output.i_chroma )
388 case VLC_FOURCC('I','4','2','0'):
389 /* XXX: Do not laugh ! I know this is very naive. But it's just a
390 * proof of concept code snippet... */
391 for( i = i_lines ; i-- ; )
393 const int i_col = i * i_pitch / i_lines;
395 if( p_in[i_col/2] > 40
396 && p_in[i_pitch / 2] > 40
397 && p_in[i_pitch/2 + i_col/2] > 40 )
399 if( i_lastwhite == -1 )
413 /* Decide whether it's worth changing the size */
414 if( i_lastwhite == -1 )
416 p_vout->p_sys->i_lastchange = 0;
420 if( i_lastwhite - i_firstwhite < p_vout->p_sys->i_height / 2 )
422 p_vout->p_sys->i_lastchange = 0;
426 if( i_lastwhite - i_firstwhite < p_vout->p_sys->i_height + 16
427 && i_lastwhite - i_firstwhite + 16 > p_vout->p_sys->i_height )
429 p_vout->p_sys->i_lastchange = 0;
433 /* We need at least 25 images to make up our mind */
434 p_vout->p_sys->i_lastchange++;
435 if( p_vout->p_sys->i_lastchange < 25 )
440 /* Tune a few values */
441 if( i_firstwhite & 1 )
446 if( !(i_lastwhite & 1) )
452 p_vout->p_sys->i_y = i_firstwhite;
453 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
455 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
456 * p_vout->output.i_height / p_vout->p_sys->i_height
457 * p_vout->p_sys->i_width / p_vout->output.i_width;
459 p_vout->p_sys->b_changed = 1;