1 /*****************************************************************************
2 * crop.c : Crop video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
5 * $Id: crop.c,v 1.14 2004/01/25 03:28:41 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 add_category_hint( N_("Crop"), NULL, VLC_FALSE );
62 add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT, GEOMETRY_LONGTEXT, VLC_FALSE );
63 add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT, AUTOCROP_LONGTEXT, VLC_FALSE );
64 set_description( _("Crop video filter") );
65 set_capability( "video filter", 0 );
66 add_shortcut( "crop" );
67 set_callbacks( Create, Destroy );
70 /*****************************************************************************
71 * vout_sys_t: Crop video output method descriptor
72 *****************************************************************************
73 * This structure is part of the video output thread descriptor.
74 * It describes the Crop specific properties of an output thread.
75 *****************************************************************************/
78 vout_thread_t *p_vout;
80 unsigned int i_x, i_y;
81 unsigned int i_width, i_height, i_aspect;
83 vlc_bool_t b_autocrop;
85 /* Autocrop specific variables */
86 unsigned int i_lastchange;
90 /*****************************************************************************
91 * Create: allocates Crop video thread output method
92 *****************************************************************************
93 * This function allocates and initializes a Crop vout method.
94 *****************************************************************************/
95 static int Create( vlc_object_t *p_this )
97 vout_thread_t *p_vout = (vout_thread_t *)p_this;
99 /* Allocate structure */
100 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
101 if( p_vout->p_sys == NULL )
103 msg_Err( p_vout, "out of memory" );
107 p_vout->pf_init = Init;
108 p_vout->pf_end = End;
109 p_vout->pf_manage = Manage;
110 p_vout->pf_render = Render;
111 p_vout->pf_display = NULL;
116 /*****************************************************************************
117 * Init: initialize Crop video thread output method
118 *****************************************************************************/
119 static int Init( vout_thread_t *p_vout )
125 I_OUTPUTPICTURES = 0;
127 p_vout->p_sys->i_lastchange = 0;
128 p_vout->p_sys->b_changed = VLC_FALSE;
130 /* Initialize the output structure */
131 p_vout->output.i_chroma = p_vout->render.i_chroma;
132 p_vout->output.i_width = p_vout->render.i_width;
133 p_vout->output.i_height = p_vout->render.i_height;
134 p_vout->output.i_aspect = p_vout->render.i_aspect;
136 /* Shall we use autocrop ? */
137 p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
139 /* Get geometry value from the user */
140 psz_var = config_GetPsz( p_vout, "crop-geometry" );
143 char *psz_parser, *psz_tmp;
145 psz_parser = psz_tmp = psz_var;
146 while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
151 p_vout->p_sys->i_width = atoi( psz_parser );
153 psz_parser = ++psz_tmp;
154 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
159 p_vout->p_sys->i_height = atoi( psz_parser );
161 psz_parser = ++psz_tmp;
162 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
167 p_vout->p_sys->i_x = atoi( psz_parser );
168 p_vout->p_sys->i_y = atoi( ++psz_tmp );
172 p_vout->p_sys->i_x = atoi( psz_parser );
174 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
179 p_vout->p_sys->i_height = atoi( psz_parser );
181 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
183 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
188 p_vout->p_sys->i_width = atoi( psz_parser );
189 p_vout->p_sys->i_height = p_vout->output.i_height;
191 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
193 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
196 /* Check for validity */
197 if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
198 > p_vout->output.i_width )
200 p_vout->p_sys->i_x = 0;
201 if( p_vout->p_sys->i_width > p_vout->output.i_width )
203 p_vout->p_sys->i_width = p_vout->output.i_width;
207 if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
208 > p_vout->output.i_height )
210 p_vout->p_sys->i_y = 0;
211 if( p_vout->p_sys->i_height > p_vout->output.i_height )
213 p_vout->p_sys->i_height = p_vout->output.i_height;
221 p_vout->p_sys->i_width = p_vout->output.i_width;
222 p_vout->p_sys->i_height = p_vout->output.i_height;
223 p_vout->p_sys->i_x = p_vout->p_sys->i_y = 0;
226 /* Pheeew. Parsing done. */
227 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
228 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
229 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
230 p_vout->p_sys->b_autocrop ? "" : "not " );
232 /* Set current output image properties */
233 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
234 * p_vout->output.i_height / p_vout->p_sys->i_height
235 * p_vout->p_sys->i_width / p_vout->output.i_width;
237 /* Try to open the real video output */
238 p_vout->p_sys->p_vout = vout_Create( p_vout,
239 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
240 p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
241 if( p_vout->p_sys->p_vout == NULL )
243 msg_Err( p_vout, "failed to create vout" );
247 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
249 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
251 ADD_PARENT_CALLBACKS( SendEventsToChild );
256 /*****************************************************************************
257 * End: terminate Crop video thread output method
258 *****************************************************************************/
259 static void End( vout_thread_t *p_vout )
263 /* Free the fake output buffers we allocated */
264 for( i_index = I_OUTPUTPICTURES ; i_index ; )
267 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
271 /*****************************************************************************
272 * Destroy: destroy Crop video thread output method
273 *****************************************************************************
274 * Terminate an output method created by CropCreateOutputMethod
275 *****************************************************************************/
276 static void Destroy( vlc_object_t *p_this )
278 vout_thread_t *p_vout = (vout_thread_t *)p_this;
280 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
281 vlc_object_detach( p_vout->p_sys->p_vout );
282 vout_Destroy( p_vout->p_sys->p_vout );
284 DEL_PARENT_CALLBACKS( SendEventsToChild );
286 free( p_vout->p_sys );
289 /*****************************************************************************
290 * Manage: handle Crop events
291 *****************************************************************************
292 * This function should be called regularly by video output thread. It manages
293 * console events. It returns a non null value on error.
294 *****************************************************************************/
295 static int Manage( vout_thread_t *p_vout )
297 if( !p_vout->p_sys->b_changed )
302 vout_Destroy( p_vout->p_sys->p_vout );
304 p_vout->p_sys->p_vout = vout_Create( p_vout,
305 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
306 p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
307 if( p_vout->p_sys->p_vout == NULL )
309 msg_Err( p_vout, "failed to create vout" );
313 p_vout->p_sys->b_changed = VLC_FALSE;
314 p_vout->p_sys->i_lastchange = 0;
319 /*****************************************************************************
320 * Render: display previously rendered output
321 *****************************************************************************
322 * This function sends the currently rendered image to Crop image, waits
323 * until it is displayed and switches the two rendering buffers, preparing next
325 *****************************************************************************/
326 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
328 picture_t *p_outpic = NULL;
331 if( p_vout->p_sys->b_changed )
337 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
340 if( p_vout->b_die || p_vout->b_error )
342 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
346 msleep( VOUT_OUTMEM_SLEEP );
349 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
350 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
352 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
354 uint8_t *p_in, *p_out, *p_out_end;
355 int i_in_pitch = p_pic->p[i_plane].i_pitch;
356 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
357 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
359 p_in = p_pic->p[i_plane].p_pixels
360 /* Skip the right amount of lines */
361 + i_in_pitch * ( p_pic->p[i_plane].i_lines * p_vout->p_sys->i_y
362 / p_vout->output.i_height )
363 /* Skip the right amount of columns */
364 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
366 p_out = p_outpic->p[i_plane].p_pixels;
367 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_lines;
369 while( p_out < p_out_end )
371 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
373 p_out += i_out_pitch;
377 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
378 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
380 /* The source image may still be in the cache ... parse it! */
381 if( p_vout->p_sys->b_autocrop )
383 UpdateStats( p_vout, p_pic );
387 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
389 uint8_t *p_in = p_pic->p[0].p_pixels;
390 int i_pitch = p_pic->p[0].i_pitch;
391 int i_visible_pitch = p_pic->p[0].i_visible_pitch;
392 int i_lines = p_pic->p[0].i_lines;
393 int i_firstwhite = -1, i_lastwhite = -1, i;
395 /* Determine where black borders are */
396 switch( p_vout->output.i_chroma )
398 case VLC_FOURCC('I','4','2','0'):
399 /* XXX: Do not laugh ! I know this is very naive. But it's just a
400 * proof of concept code snippet... */
401 for( i = i_lines ; i-- ; )
403 const int i_col = i * i_pitch / i_lines;
405 if( p_in[i_col/2] > 40
406 && p_in[i_visible_pitch/2] > 40
407 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
409 if( i_lastwhite == -1 )
423 /* Decide whether it's worth changing the size */
424 if( i_lastwhite == -1 )
426 p_vout->p_sys->i_lastchange = 0;
430 if( (unsigned int)(i_lastwhite - i_firstwhite)
431 < p_vout->p_sys->i_height / 2 )
433 p_vout->p_sys->i_lastchange = 0;
437 if( (unsigned int)(i_lastwhite - i_firstwhite)
438 < p_vout->p_sys->i_height + 16
439 && (unsigned int)(i_lastwhite - i_firstwhite + 16)
440 > p_vout->p_sys->i_height )
442 p_vout->p_sys->i_lastchange = 0;
446 /* We need at least 25 images to make up our mind */
447 p_vout->p_sys->i_lastchange++;
448 if( p_vout->p_sys->i_lastchange < 25 )
453 /* Tune a few values */
454 if( i_firstwhite & 1 )
459 if( !(i_lastwhite & 1) )
465 p_vout->p_sys->i_y = i_firstwhite;
466 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
468 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
469 * p_vout->output.i_height / p_vout->p_sys->i_height
470 * p_vout->p_sys->i_width / p_vout->output.i_width;
472 p_vout->p_sys->b_changed = VLC_TRUE;
475 /*****************************************************************************
476 * SendEvents: forward mouse and keyboard events to the parent p_vout
477 *****************************************************************************/
478 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
479 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
481 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
482 vlc_value_t sentval = newval;
484 /* Translate the mouse coordinates */
485 if( !strcmp( psz_var, "mouse-x" ) )
487 sentval.i_int += p_vout->p_sys->i_x;
489 else if( !strcmp( psz_var, "mouse-y" ) )
491 sentval.i_int += p_vout->p_sys->i_y;
494 var_Set( p_vout, psz_var, sentval );
499 /*****************************************************************************
500 * SendEventsToChild: forward events to the child/children vout
501 *****************************************************************************/
502 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
503 vlc_value_t oldval, vlc_value_t newval, void *p_data )
505 vout_thread_t *p_vout = (vout_thread_t *)p_this;
506 var_Set( p_vout->p_sys->p_vout, psz_var, newval );