1 /*****************************************************************************
2 * crop.c : Crop video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
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 static int SendEvents( vlc_object_t *, char const *,
49 vlc_value_t, vlc_value_t, void * );
51 /*****************************************************************************
53 *****************************************************************************/
54 #define GEOMETRY_TEXT N_("Crop geometry (pixels)")
55 #define GEOMETRY_LONGTEXT N_("Set the geometry of the zone to crop. This is set as <width> x <height> + <left offset> + <top offset>.")
57 #define AUTOCROP_TEXT N_("Automatic cropping")
58 #define AUTOCROP_LONGTEXT N_("Activate automatic black border cropping.")
61 set_description( _("Crop video filter") );
62 set_shortname( N_("Crop" ));
63 set_category( CAT_VIDEO );
64 set_subcategory( SUBCAT_VIDEO_VFILTER );
65 set_capability( "video filter", 0 );
67 add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT, GEOMETRY_LONGTEXT, VLC_FALSE );
68 add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT, AUTOCROP_LONGTEXT, VLC_FALSE );
70 add_shortcut( "crop" );
71 set_callbacks( Create, Destroy );
74 /*****************************************************************************
75 * vout_sys_t: Crop video output method descriptor
76 *****************************************************************************
77 * This structure is part of the video output thread descriptor.
78 * It describes the Crop specific properties of an output thread.
79 *****************************************************************************/
82 vout_thread_t *p_vout;
84 unsigned int i_x, i_y;
85 unsigned int i_width, i_height, i_aspect;
87 vlc_bool_t b_autocrop;
89 /* Autocrop specific variables */
90 unsigned int i_lastchange;
94 /*****************************************************************************
95 * Control: control facility for the vout (forwards to child vout)
96 *****************************************************************************/
97 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
99 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
102 /*****************************************************************************
103 * Create: allocates Crop video thread output method
104 *****************************************************************************
105 * This function allocates and initializes a Crop vout method.
106 *****************************************************************************/
107 static int Create( vlc_object_t *p_this )
109 vout_thread_t *p_vout = (vout_thread_t *)p_this;
111 /* Allocate structure */
112 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
113 if( p_vout->p_sys == NULL )
115 msg_Err( p_vout, "out of memory" );
119 p_vout->pf_init = Init;
120 p_vout->pf_end = End;
121 p_vout->pf_manage = Manage;
122 p_vout->pf_render = Render;
123 p_vout->pf_display = NULL;
124 p_vout->pf_control = Control;
129 /*****************************************************************************
130 * Init: initialize Crop video thread output method
131 *****************************************************************************/
132 static int Init( vout_thread_t *p_vout )
138 I_OUTPUTPICTURES = 0;
140 p_vout->p_sys->i_lastchange = 0;
141 p_vout->p_sys->b_changed = VLC_FALSE;
143 /* Initialize the output structure */
144 p_vout->output.i_chroma = p_vout->render.i_chroma;
145 p_vout->output.i_width = p_vout->render.i_width;
146 p_vout->output.i_height = p_vout->render.i_height;
147 p_vout->output.i_aspect = p_vout->render.i_aspect;
149 /* Shall we use autocrop ? */
150 p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
152 /* Get geometry value from the user */
153 psz_var = config_GetPsz( p_vout, "crop-geometry" );
156 char *psz_parser, *psz_tmp;
158 psz_parser = psz_tmp = psz_var;
159 while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
164 p_vout->p_sys->i_width = atoi( psz_parser );
166 psz_parser = ++psz_tmp;
167 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
172 p_vout->p_sys->i_height = atoi( psz_parser );
174 psz_parser = ++psz_tmp;
175 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
180 p_vout->p_sys->i_x = atoi( psz_parser );
181 p_vout->p_sys->i_y = atoi( ++psz_tmp );
185 p_vout->p_sys->i_x = atoi( psz_parser );
187 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
192 p_vout->p_sys->i_height = atoi( psz_parser );
194 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
196 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
201 p_vout->p_sys->i_width = atoi( psz_parser );
202 p_vout->p_sys->i_height = p_vout->output.i_height;
204 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
206 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
209 /* Check for validity */
210 if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
211 > p_vout->output.i_width )
213 p_vout->p_sys->i_x = 0;
214 if( p_vout->p_sys->i_width > p_vout->output.i_width )
216 p_vout->p_sys->i_width = p_vout->output.i_width;
220 if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
221 > p_vout->output.i_height )
223 p_vout->p_sys->i_y = 0;
224 if( p_vout->p_sys->i_height > p_vout->output.i_height )
226 p_vout->p_sys->i_height = p_vout->output.i_height;
234 p_vout->p_sys->i_width = p_vout->output.i_width;
235 p_vout->p_sys->i_height = p_vout->output.i_height;
236 p_vout->p_sys->i_x = p_vout->p_sys->i_y = 0;
239 /* Pheeew. Parsing done. */
240 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
241 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
242 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
243 p_vout->p_sys->b_autocrop ? "" : "not " );
245 /* Set current output image properties */
246 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
247 * p_vout->output.i_height / p_vout->p_sys->i_height
248 * p_vout->p_sys->i_width / p_vout->output.i_width;
250 /* Try to open the real video output */
251 p_vout->p_sys->p_vout = vout_Create( p_vout,
252 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
253 p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
254 if( p_vout->p_sys->p_vout == NULL )
256 msg_Err( p_vout, "failed to create vout" );
260 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
262 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
264 ADD_PARENT_CALLBACKS( SendEventsToChild );
269 /*****************************************************************************
270 * End: terminate Crop video thread output method
271 *****************************************************************************/
272 static void End( vout_thread_t *p_vout )
276 /* Free the fake output buffers we allocated */
277 for( i_index = I_OUTPUTPICTURES ; i_index ; )
280 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
284 /*****************************************************************************
285 * Destroy: destroy Crop video thread output method
286 *****************************************************************************
287 * Terminate an output method created by CropCreateOutputMethod
288 *****************************************************************************/
289 static void Destroy( vlc_object_t *p_this )
291 vout_thread_t *p_vout = (vout_thread_t *)p_this;
293 if( p_vout->p_sys->p_vout )
295 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
296 vlc_object_detach( p_vout->p_sys->p_vout );
297 vout_Destroy( p_vout->p_sys->p_vout );
300 DEL_PARENT_CALLBACKS( SendEventsToChild );
302 free( p_vout->p_sys );
305 /*****************************************************************************
306 * Manage: handle Crop events
307 *****************************************************************************
308 * This function should be called regularly by video output thread. It manages
309 * console events. It returns a non null value on error.
310 *****************************************************************************/
311 static int Manage( vout_thread_t *p_vout )
313 if( !p_vout->p_sys->b_changed )
318 vout_Destroy( p_vout->p_sys->p_vout );
320 p_vout->p_sys->p_vout = vout_Create( p_vout,
321 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
322 p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
323 if( p_vout->p_sys->p_vout == NULL )
325 msg_Err( p_vout, "failed to create vout" );
329 p_vout->p_sys->b_changed = VLC_FALSE;
330 p_vout->p_sys->i_lastchange = 0;
335 /*****************************************************************************
336 * Render: display previously rendered output
337 *****************************************************************************
338 * This function sends the currently rendered image to Crop image, waits
339 * until it is displayed and switches the two rendering buffers, preparing next
341 *****************************************************************************/
342 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
344 picture_t *p_outpic = NULL;
347 if( p_vout->p_sys->b_changed )
353 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
356 if( p_vout->b_die || p_vout->b_error )
358 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
362 msleep( VOUT_OUTMEM_SLEEP );
365 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
366 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
368 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
370 uint8_t *p_in, *p_out, *p_out_end;
371 int i_in_pitch = p_pic->p[i_plane].i_pitch;
372 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
373 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
375 p_in = p_pic->p[i_plane].p_pixels
376 /* Skip the right amount of lines */
377 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
378 p_vout->p_sys->i_y / p_vout->output.i_height )
379 /* Skip the right amount of columns */
380 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
382 p_out = p_outpic->p[i_plane].p_pixels;
383 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
385 while( p_out < p_out_end )
387 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
389 p_out += i_out_pitch;
393 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
394 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
396 /* The source image may still be in the cache ... parse it! */
397 if( p_vout->p_sys->b_autocrop )
399 UpdateStats( p_vout, p_pic );
403 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
405 uint8_t *p_in = p_pic->p[0].p_pixels;
406 int i_pitch = p_pic->p[0].i_pitch;
407 int i_visible_pitch = p_pic->p[0].i_visible_pitch;
408 int i_lines = p_pic->p[0].i_visible_lines;
409 int i_firstwhite = -1, i_lastwhite = -1, i;
411 /* Determine where black borders are */
412 switch( p_vout->output.i_chroma )
414 case VLC_FOURCC('I','4','2','0'):
415 /* XXX: Do not laugh ! I know this is very naive. But it's just a
416 * proof of concept code snippet... */
417 for( i = i_lines ; i-- ; )
419 const int i_col = i * i_pitch / i_lines;
421 if( p_in[i_col/2] > 40
422 && p_in[i_visible_pitch/2] > 40
423 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
425 if( i_lastwhite == -1 )
439 /* Decide whether it's worth changing the size */
440 if( i_lastwhite == -1 )
442 p_vout->p_sys->i_lastchange = 0;
446 if( (unsigned int)(i_lastwhite - i_firstwhite)
447 < p_vout->p_sys->i_height / 2 )
449 p_vout->p_sys->i_lastchange = 0;
453 if( (unsigned int)(i_lastwhite - i_firstwhite)
454 < p_vout->p_sys->i_height + 16
455 && (unsigned int)(i_lastwhite - i_firstwhite + 16)
456 > p_vout->p_sys->i_height )
458 p_vout->p_sys->i_lastchange = 0;
462 /* We need at least 25 images to make up our mind */
463 p_vout->p_sys->i_lastchange++;
464 if( p_vout->p_sys->i_lastchange < 25 )
469 /* Tune a few values */
470 if( i_firstwhite & 1 )
475 if( !(i_lastwhite & 1) )
481 p_vout->p_sys->i_y = i_firstwhite;
482 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
484 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
485 * p_vout->output.i_height / p_vout->p_sys->i_height
486 * p_vout->p_sys->i_width / p_vout->output.i_width;
488 p_vout->p_sys->b_changed = VLC_TRUE;
491 /*****************************************************************************
492 * SendEvents: forward mouse and keyboard events to the parent p_vout
493 *****************************************************************************/
494 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
495 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
497 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
498 vlc_value_t sentval = newval;
500 /* Translate the mouse coordinates */
501 if( !strcmp( psz_var, "mouse-x" ) )
503 sentval.i_int += p_vout->p_sys->i_x;
505 else if( !strcmp( psz_var, "mouse-y" ) )
507 sentval.i_int += p_vout->p_sys->i_y;
510 var_Set( p_vout, psz_var, sentval );
515 /*****************************************************************************
516 * SendEventsToChild: forward events to the child/children vout
517 *****************************************************************************/
518 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
519 vlc_value_t oldval, vlc_value_t newval, void *p_data )
521 vout_thread_t *p_vout = (vout_thread_t *)p_this;
522 var_Set( p_vout->p_sys->p_vout, psz_var, newval );