1 /*****************************************************************************
2 * crop.c : Crop video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: crop.c,v 1.6 2003/01/09 17:47:05 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 = VLC_FALSE;
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 = vout_Create( p_vout,
236 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
237 p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
238 if( p_vout->p_sys->p_vout == NULL )
240 msg_Err( p_vout, "failed to create vout" );
244 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
249 /*****************************************************************************
250 * End: terminate Crop video thread output method
251 *****************************************************************************/
252 static void End( vout_thread_t *p_vout )
256 /* Free the fake output buffers we allocated */
257 for( i_index = I_OUTPUTPICTURES ; i_index ; )
260 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
264 /*****************************************************************************
265 * Destroy: destroy Crop video thread output method
266 *****************************************************************************
267 * Terminate an output method created by CropCreateOutputMethod
268 *****************************************************************************/
269 static void Destroy( vlc_object_t *p_this )
271 vout_thread_t *p_vout = (vout_thread_t *)p_this;
273 vout_Destroy( p_vout->p_sys->p_vout );
274 free( p_vout->p_sys );
277 /*****************************************************************************
278 * Manage: handle Crop events
279 *****************************************************************************
280 * This function should be called regularly by video output thread. It manages
281 * console events. It returns a non null value on error.
282 *****************************************************************************/
283 static int Manage( vout_thread_t *p_vout )
285 if( !p_vout->p_sys->b_changed )
290 vout_Destroy( p_vout->p_sys->p_vout );
292 p_vout->p_sys->p_vout = vout_Create( p_vout,
293 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
294 p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
295 if( p_vout->p_sys->p_vout == NULL )
297 msg_Err( p_vout, "failed to create vout" );
301 p_vout->p_sys->b_changed = VLC_FALSE;
302 p_vout->p_sys->i_lastchange = 0;
307 /*****************************************************************************
308 * Render: display previously rendered output
309 *****************************************************************************
310 * This function sends the currently rendered image to Crop image, waits
311 * until it is displayed and switches the two rendering buffers, preparing next
313 *****************************************************************************/
314 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
316 picture_t *p_outpic = NULL;
319 if( p_vout->p_sys->b_changed )
325 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
328 if( p_vout->b_die || p_vout->b_error )
330 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
334 msleep( VOUT_OUTMEM_SLEEP );
337 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
338 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
340 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
342 uint8_t *p_in, *p_out, *p_out_end;
343 int i_in_pitch = p_pic->p[i_plane].i_pitch;
344 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
345 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
347 p_in = p_pic->p[i_plane].p_pixels
348 /* Skip the right amount of lines */
349 + i_in_pitch * ( p_pic->p[i_plane].i_lines * p_vout->p_sys->i_y
350 / p_vout->output.i_height )
351 /* Skip the right amount of columns */
352 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
354 p_out = p_outpic->p[i_plane].p_pixels;
355 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_lines;
357 while( p_out < p_out_end )
359 p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
361 p_out += i_out_pitch;
365 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
366 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
368 /* The source image may still be in the cache ... parse it! */
369 if( p_vout->p_sys->b_autocrop )
371 UpdateStats( p_vout, p_pic );
375 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
377 uint8_t *p_in = p_pic->p[0].p_pixels;
378 int i_pitch = p_pic->p[0].i_pitch;
379 int i_visible_pitch = p_pic->p[0].i_visible_pitch;
380 int i_lines = p_pic->p[0].i_lines;
381 int i_firstwhite = -1, i_lastwhite = -1, i;
383 /* Determine where black borders are */
384 switch( p_vout->output.i_chroma )
386 case VLC_FOURCC('I','4','2','0'):
387 /* XXX: Do not laugh ! I know this is very naive. But it's just a
388 * proof of concept code snippet... */
389 for( i = i_lines ; i-- ; )
391 const int i_col = i * i_pitch / i_lines;
393 if( p_in[i_col/2] > 40
394 && p_in[i_visible_pitch/2] > 40
395 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
397 if( i_lastwhite == -1 )
411 /* Decide whether it's worth changing the size */
412 if( i_lastwhite == -1 )
414 p_vout->p_sys->i_lastchange = 0;
418 if( (unsigned int)(i_lastwhite - i_firstwhite)
419 < p_vout->p_sys->i_height / 2 )
421 p_vout->p_sys->i_lastchange = 0;
425 if( (unsigned int)(i_lastwhite - i_firstwhite)
426 < p_vout->p_sys->i_height + 16
427 && (unsigned int)(i_lastwhite - i_firstwhite + 16)
428 > p_vout->p_sys->i_height )
430 p_vout->p_sys->i_lastchange = 0;
434 /* We need at least 25 images to make up our mind */
435 p_vout->p_sys->i_lastchange++;
436 if( p_vout->p_sys->i_lastchange < 25 )
441 /* Tune a few values */
442 if( i_firstwhite & 1 )
447 if( !(i_lastwhite & 1) )
453 p_vout->p_sys->i_y = i_firstwhite;
454 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
456 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
457 * p_vout->output.i_height / p_vout->p_sys->i_height
458 * p_vout->p_sys->i_width / p_vout->output.i_width;
460 p_vout->p_sys->b_changed = VLC_TRUE;