1 /*****************************************************************************
2 * crop.c : Crop video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
5 * $Id: crop.c,v 1.15 2004/01/25 20:05:28 hartman 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 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 heigth + 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_capability( "video filter", 0 );
64 add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT, GEOMETRY_LONGTEXT, VLC_FALSE );
65 add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT, AUTOCROP_LONGTEXT, VLC_FALSE );
67 add_shortcut( "crop" );
68 set_callbacks( Create, Destroy );
71 /*****************************************************************************
72 * vout_sys_t: Crop video output method descriptor
73 *****************************************************************************
74 * This structure is part of the video output thread descriptor.
75 * It describes the Crop specific properties of an output thread.
76 *****************************************************************************/
79 vout_thread_t *p_vout;
81 unsigned int i_x, i_y;
82 unsigned int i_width, i_height, i_aspect;
84 vlc_bool_t b_autocrop;
86 /* Autocrop specific variables */
87 unsigned int i_lastchange;
91 /*****************************************************************************
92 * Create: allocates Crop video thread output method
93 *****************************************************************************
94 * This function allocates and initializes a Crop vout method.
95 *****************************************************************************/
96 static int Create( vlc_object_t *p_this )
98 vout_thread_t *p_vout = (vout_thread_t *)p_this;
100 /* Allocate structure */
101 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
102 if( p_vout->p_sys == NULL )
104 msg_Err( p_vout, "out of memory" );
108 p_vout->pf_init = Init;
109 p_vout->pf_end = End;
110 p_vout->pf_manage = Manage;
111 p_vout->pf_render = Render;
112 p_vout->pf_display = NULL;
117 /*****************************************************************************
118 * Init: initialize Crop video thread output method
119 *****************************************************************************/
120 static int Init( vout_thread_t *p_vout )
126 I_OUTPUTPICTURES = 0;
128 p_vout->p_sys->i_lastchange = 0;
129 p_vout->p_sys->b_changed = VLC_FALSE;
131 /* Initialize the output structure */
132 p_vout->output.i_chroma = p_vout->render.i_chroma;
133 p_vout->output.i_width = p_vout->render.i_width;
134 p_vout->output.i_height = p_vout->render.i_height;
135 p_vout->output.i_aspect = p_vout->render.i_aspect;
137 /* Shall we use autocrop ? */
138 p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
140 /* Get geometry value from the user */
141 psz_var = config_GetPsz( p_vout, "crop-geometry" );
144 char *psz_parser, *psz_tmp;
146 psz_parser = psz_tmp = psz_var;
147 while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
152 p_vout->p_sys->i_width = atoi( psz_parser );
154 psz_parser = ++psz_tmp;
155 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
160 p_vout->p_sys->i_height = atoi( psz_parser );
162 psz_parser = ++psz_tmp;
163 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
168 p_vout->p_sys->i_x = atoi( psz_parser );
169 p_vout->p_sys->i_y = atoi( ++psz_tmp );
173 p_vout->p_sys->i_x = atoi( psz_parser );
175 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
180 p_vout->p_sys->i_height = atoi( psz_parser );
182 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
184 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
189 p_vout->p_sys->i_width = atoi( psz_parser );
190 p_vout->p_sys->i_height = p_vout->output.i_height;
192 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
194 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
197 /* Check for validity */
198 if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
199 > p_vout->output.i_width )
201 p_vout->p_sys->i_x = 0;
202 if( p_vout->p_sys->i_width > p_vout->output.i_width )
204 p_vout->p_sys->i_width = p_vout->output.i_width;
208 if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
209 > p_vout->output.i_height )
211 p_vout->p_sys->i_y = 0;
212 if( p_vout->p_sys->i_height > p_vout->output.i_height )
214 p_vout->p_sys->i_height = p_vout->output.i_height;
222 p_vout->p_sys->i_width = p_vout->output.i_width;
223 p_vout->p_sys->i_height = p_vout->output.i_height;
224 p_vout->p_sys->i_x = p_vout->p_sys->i_y = 0;
227 /* Pheeew. Parsing done. */
228 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
229 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
230 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
231 p_vout->p_sys->b_autocrop ? "" : "not " );
233 /* Set current output image properties */
234 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
235 * p_vout->output.i_height / p_vout->p_sys->i_height
236 * p_vout->p_sys->i_width / p_vout->output.i_width;
238 /* Try to open the real video output */
239 p_vout->p_sys->p_vout = vout_Create( p_vout,
240 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
241 p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
242 if( p_vout->p_sys->p_vout == NULL )
244 msg_Err( p_vout, "failed to create vout" );
248 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
250 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
252 ADD_PARENT_CALLBACKS( SendEventsToChild );
257 /*****************************************************************************
258 * End: terminate Crop video thread output method
259 *****************************************************************************/
260 static void End( vout_thread_t *p_vout )
264 /* Free the fake output buffers we allocated */
265 for( i_index = I_OUTPUTPICTURES ; i_index ; )
268 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
272 /*****************************************************************************
273 * Destroy: destroy Crop video thread output method
274 *****************************************************************************
275 * Terminate an output method created by CropCreateOutputMethod
276 *****************************************************************************/
277 static void Destroy( vlc_object_t *p_this )
279 vout_thread_t *p_vout = (vout_thread_t *)p_this;
281 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
282 vlc_object_detach( p_vout->p_sys->p_vout );
283 vout_Destroy( p_vout->p_sys->p_vout );
285 DEL_PARENT_CALLBACKS( SendEventsToChild );
287 free( p_vout->p_sys );
290 /*****************************************************************************
291 * Manage: handle Crop events
292 *****************************************************************************
293 * This function should be called regularly by video output thread. It manages
294 * console events. It returns a non null value on error.
295 *****************************************************************************/
296 static int Manage( vout_thread_t *p_vout )
298 if( !p_vout->p_sys->b_changed )
303 vout_Destroy( p_vout->p_sys->p_vout );
305 p_vout->p_sys->p_vout = vout_Create( p_vout,
306 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
307 p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
308 if( p_vout->p_sys->p_vout == NULL )
310 msg_Err( p_vout, "failed to create vout" );
314 p_vout->p_sys->b_changed = VLC_FALSE;
315 p_vout->p_sys->i_lastchange = 0;
320 /*****************************************************************************
321 * Render: display previously rendered output
322 *****************************************************************************
323 * This function sends the currently rendered image to Crop image, waits
324 * until it is displayed and switches the two rendering buffers, preparing next
326 *****************************************************************************/
327 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
329 picture_t *p_outpic = NULL;
332 if( p_vout->p_sys->b_changed )
338 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
341 if( p_vout->b_die || p_vout->b_error )
343 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
347 msleep( VOUT_OUTMEM_SLEEP );
350 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
351 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
353 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
355 uint8_t *p_in, *p_out, *p_out_end;
356 int i_in_pitch = p_pic->p[i_plane].i_pitch;
357 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
358 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
360 p_in = p_pic->p[i_plane].p_pixels
361 /* Skip the right amount of lines */
362 + i_in_pitch * ( p_pic->p[i_plane].i_lines * p_vout->p_sys->i_y
363 / p_vout->output.i_height )
364 /* Skip the right amount of columns */
365 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
367 p_out = p_outpic->p[i_plane].p_pixels;
368 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_lines;
370 while( p_out < p_out_end )
372 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
374 p_out += i_out_pitch;
378 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
379 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
381 /* The source image may still be in the cache ... parse it! */
382 if( p_vout->p_sys->b_autocrop )
384 UpdateStats( p_vout, p_pic );
388 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
390 uint8_t *p_in = p_pic->p[0].p_pixels;
391 int i_pitch = p_pic->p[0].i_pitch;
392 int i_visible_pitch = p_pic->p[0].i_visible_pitch;
393 int i_lines = p_pic->p[0].i_lines;
394 int i_firstwhite = -1, i_lastwhite = -1, i;
396 /* Determine where black borders are */
397 switch( p_vout->output.i_chroma )
399 case VLC_FOURCC('I','4','2','0'):
400 /* XXX: Do not laugh ! I know this is very naive. But it's just a
401 * proof of concept code snippet... */
402 for( i = i_lines ; i-- ; )
404 const int i_col = i * i_pitch / i_lines;
406 if( p_in[i_col/2] > 40
407 && p_in[i_visible_pitch/2] > 40
408 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
410 if( i_lastwhite == -1 )
424 /* Decide whether it's worth changing the size */
425 if( i_lastwhite == -1 )
427 p_vout->p_sys->i_lastchange = 0;
431 if( (unsigned int)(i_lastwhite - i_firstwhite)
432 < p_vout->p_sys->i_height / 2 )
434 p_vout->p_sys->i_lastchange = 0;
438 if( (unsigned int)(i_lastwhite - i_firstwhite)
439 < p_vout->p_sys->i_height + 16
440 && (unsigned int)(i_lastwhite - i_firstwhite + 16)
441 > p_vout->p_sys->i_height )
443 p_vout->p_sys->i_lastchange = 0;
447 /* We need at least 25 images to make up our mind */
448 p_vout->p_sys->i_lastchange++;
449 if( p_vout->p_sys->i_lastchange < 25 )
454 /* Tune a few values */
455 if( i_firstwhite & 1 )
460 if( !(i_lastwhite & 1) )
466 p_vout->p_sys->i_y = i_firstwhite;
467 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
469 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
470 * p_vout->output.i_height / p_vout->p_sys->i_height
471 * p_vout->p_sys->i_width / p_vout->output.i_width;
473 p_vout->p_sys->b_changed = VLC_TRUE;
476 /*****************************************************************************
477 * SendEvents: forward mouse and keyboard events to the parent p_vout
478 *****************************************************************************/
479 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
480 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
482 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
483 vlc_value_t sentval = newval;
485 /* Translate the mouse coordinates */
486 if( !strcmp( psz_var, "mouse-x" ) )
488 sentval.i_int += p_vout->p_sys->i_x;
490 else if( !strcmp( psz_var, "mouse-y" ) )
492 sentval.i_int += p_vout->p_sys->i_y;
495 var_Set( p_vout, psz_var, sentval );
500 /*****************************************************************************
501 * SendEventsToChild: forward events to the child/children vout
502 *****************************************************************************/
503 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
504 vlc_value_t oldval, vlc_value_t newval, void *p_data )
506 vout_thread_t *p_vout = (vout_thread_t *)p_this;
507 var_Set( p_vout->p_sys->p_vout, psz_var, newval );