1 /*****************************************************************************
2 * crop.c : Crop video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 the VideoLAN team
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * mod by Cedric Cocquebert <Cedric.Cocquebert@supelec.fr>
9 * based of DScaler idea (M. Samblanet)
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
33 #include <vlc_interface.h>
35 #include "filter_common.h"
37 #define BEST_AUTOCROP 1
39 #define RATIO_MAX 15000 // 10*4/3 for a 360
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Create ( vlc_object_t * );
46 static void Destroy ( vlc_object_t * );
48 static int Init ( vout_thread_t * );
49 static void End ( vout_thread_t * );
50 static int Manage ( vout_thread_t * );
51 static void Render ( vout_thread_t *, picture_t * );
53 static void UpdateStats ( vout_thread_t *, picture_t * );
55 static int SendEvents( vlc_object_t *, char const *,
56 vlc_value_t, vlc_value_t, void * );
59 /*****************************************************************************
61 *****************************************************************************/
62 static int FilterCallback ( vlc_object_t *, char const *,
63 vlc_value_t, vlc_value_t, void * );
66 /*****************************************************************************
68 *****************************************************************************/
69 #define GEOMETRY_TEXT N_("Crop geometry (pixels)")
70 #define GEOMETRY_LONGTEXT N_("Set the geometry of the zone to crop. This is set as <width> x <height> + <left offset> + <top offset>.")
72 #define AUTOCROP_TEXT N_("Automatic cropping")
73 #define AUTOCROP_LONGTEXT N_("Automatically detect black borders and crop them.")
76 #define RATIOMAX_TEXT N_("Ratio max (x 1000)")
77 #define RATIOMAX_LONGTEXT N_("Maximum image ratio. The crop plugin will never automatically crop to a higher ratio (ie, to a more \"flat\" image). The value is x1000: 1333 means 4/3.")
79 #define RATIO_TEXT N_("Manual ratio")
80 #define RATIO_LONGTEXT N_("Force a ratio (0 for automatic). Value is x1000: 1333 means 4/3.")
82 #define TIME_TEXT N_("Number of images for change")
83 #define TIME_LONGTEXT N_("The number of consecutive images with the same detected ratio (different from the previously detected ratio) to consider that ratio chnged and trigger recrop.")
85 #define DIFF_TEXT N_("Number of lines for change")
86 #define DIFF_LONGTEXT N_("The minimum difference in the number of detected black lines to consider that ratio changed and trigger recrop.")
88 #define NBP_TEXT N_("Number of non black pixels ")
89 #define NBP_LONGTEXT N_("The maximum of non-black pixels in a line to consider"\
90 " that the line is black.")
92 #define SKIP_TEXT N_("Skip percentage (%)")
93 #define SKIP_LONGTEXT N_("Percentage of the line to consider while checking for black lines. This allows to skip logos in black borders and crop them anyway.")
95 #define LUM_TEXT N_("Luminance threshold ")
96 #define LUM_LONGTEXT N_("Maximum luminance to consider a pixel as black (0-255).")
100 set_description( _("Crop video filter") );
101 set_shortname( _("Crop" ));
102 set_category( CAT_VIDEO );
103 set_subcategory( SUBCAT_VIDEO_VFILTER );
104 set_capability( "video filter", 0 );
106 add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT,
107 GEOMETRY_LONGTEXT, VLC_FALSE );
108 add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT,
109 AUTOCROP_LONGTEXT, VLC_FALSE );
112 add_integer_with_range( "autocrop-ratio-max", 2405, 0, RATIO_MAX, NULL,
113 RATIOMAX_TEXT, RATIOMAX_LONGTEXT, VLC_TRUE );
115 add_integer_with_range( "crop-ratio", 0, 0, RATIO_MAX, NULL, RATIO_TEXT,
116 RATIO_LONGTEXT, VLC_FALSE );
117 add_integer( "autocrop-time", 25, NULL, TIME_TEXT,
118 TIME_LONGTEXT, VLC_TRUE );
119 add_integer( "autocrop-diff", 16, NULL, DIFF_TEXT,
120 DIFF_LONGTEXT, VLC_TRUE );
122 add_integer( "autocrop-non-black-pixels", 3, NULL,
123 NBP_TEXT, NBP_LONGTEXT, VLC_TRUE );
125 add_integer_with_range( "autocrop-skip-percent", 17, 0, 100, NULL,
126 SKIP_TEXT, SKIP_LONGTEXT, VLC_TRUE );
128 add_integer_with_range( "autocrop-luminance-threshold", 40, 0, 128, NULL,
129 LUM_TEXT, LUM_LONGTEXT, VLC_TRUE );
130 #endif //BEST_AUTOCROP
132 add_shortcut( "crop" );
133 set_callbacks( Create, Destroy );
136 /*****************************************************************************
137 * vout_sys_t: Crop video output method descriptor
138 *****************************************************************************
139 * This structure is part of the video output thread descriptor.
140 * It describes the Crop specific properties of an output thread.
141 *****************************************************************************/
144 vout_thread_t *p_vout;
146 unsigned int i_x, i_y;
147 unsigned int i_width, i_height, i_aspect;
149 vlc_bool_t b_autocrop;
151 /* Autocrop specific variables */
152 unsigned int i_lastchange;
153 vlc_bool_t b_changed;
155 unsigned int i_ratio_max;
156 unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
157 unsigned int i_ratio;
162 /*****************************************************************************
163 * Control: control facility for the vout (forwards to child vout)
164 *****************************************************************************/
165 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
167 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
170 /*****************************************************************************
171 * Create: allocates Crop video thread output method
172 *****************************************************************************
173 * This function allocates and initializes a Crop vout method.
174 *****************************************************************************/
175 static int Create( vlc_object_t *p_this )
177 vout_thread_t *p_vout = (vout_thread_t *)p_this;
179 /* Allocate structure */
180 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
181 if( p_vout->p_sys == NULL )
183 msg_Err( p_vout, "out of memory" );
187 p_vout->pf_init = Init;
188 p_vout->pf_end = End;
189 p_vout->pf_manage = Manage;
190 p_vout->pf_render = Render;
191 p_vout->pf_display = NULL;
192 p_vout->pf_control = Control;
197 /*****************************************************************************
198 * Init: initialize Crop video thread output method
199 *****************************************************************************/
200 static int Init( vout_thread_t *p_vout )
207 I_OUTPUTPICTURES = 0;
208 memset( &fmt, 0, sizeof(video_format_t) );
210 p_vout->p_sys->i_lastchange = 0;
211 p_vout->p_sys->b_changed = VLC_FALSE;
213 /* Initialize the output structure */
214 p_vout->output.i_chroma = p_vout->render.i_chroma;
215 p_vout->output.i_width = p_vout->render.i_width;
216 p_vout->output.i_height = p_vout->render.i_height;
217 p_vout->output.i_aspect = p_vout->render.i_aspect;
218 p_vout->fmt_out = p_vout->fmt_in;
220 /* Shall we use autocrop ? */
221 p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
223 p_vout->p_sys->i_ratio_max = config_GetInt( p_vout, "autocrop-ratio-max" );
224 p_vout->p_sys->i_threshold =
225 config_GetInt( p_vout, "autocrop-luminance-threshold" );
226 p_vout->p_sys->i_skipPercent =
227 config_GetInt( p_vout, "autocrop-skip-percent" );
228 p_vout->p_sys->i_nonBlackPixel =
229 config_GetInt( p_vout, "autocrop-non-black-pixels" );
230 p_vout->p_sys->i_diff = config_GetInt( p_vout, "autocrop-diff" );
231 p_vout->p_sys->i_time = config_GetInt( p_vout, "autocrop-time" );
233 var_Get( p_vout, "ratio-crop", &val );
234 val.psz_string = "0";
235 var_SetString( p_vout, "ratio-crop", val.psz_string);
237 if (p_vout->p_sys->b_autocrop)
238 p_vout->p_sys->i_ratio = 0;
241 p_vout->p_sys->i_ratio = config_GetInt( p_vout, "crop-ratio" );
242 // ratio < width / height => ratio = 0 (unchange ratio)
243 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) / p_vout->output.i_height)
244 p_vout->p_sys->i_ratio = 0;
249 /* Get geometry value from the user */
250 psz_var = config_GetPsz( p_vout, "crop-geometry" );
253 char *psz_parser, *psz_tmp;
255 psz_parser = psz_tmp = psz_var;
256 while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
261 p_vout->p_sys->i_width = atoi( psz_parser );
263 psz_parser = ++psz_tmp;
264 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
269 p_vout->p_sys->i_height = atoi( psz_parser );
271 psz_parser = ++psz_tmp;
272 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
277 p_vout->p_sys->i_x = atoi( psz_parser );
278 p_vout->p_sys->i_y = atoi( ++psz_tmp );
282 p_vout->p_sys->i_x = atoi( psz_parser );
284 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
289 p_vout->p_sys->i_height = atoi( psz_parser );
291 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
293 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
298 p_vout->p_sys->i_width = atoi( psz_parser );
299 p_vout->p_sys->i_height = p_vout->output.i_height;
301 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
303 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
306 /* Check for validity */
307 if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
308 > p_vout->output.i_width )
310 p_vout->p_sys->i_x = 0;
311 if( p_vout->p_sys->i_width > p_vout->output.i_width )
313 p_vout->p_sys->i_width = p_vout->output.i_width;
317 if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
318 > p_vout->output.i_height )
320 p_vout->p_sys->i_y = 0;
321 if( p_vout->p_sys->i_height > p_vout->output.i_height )
323 p_vout->p_sys->i_height = p_vout->output.i_height;
331 if (p_vout->p_sys->i_ratio)
333 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
334 p_vout->p_sys->i_width = p_vout->fmt_out.i_visible_width;
335 p_vout->p_sys->i_height = p_vout->output.i_aspect
336 * p_vout->output.i_height / p_vout->p_sys->i_aspect
337 * p_vout->p_sys->i_width / p_vout->output.i_width;
338 p_vout->p_sys->i_height += p_vout->p_sys->i_height % 2;
339 p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
340 p_vout->p_sys->i_y = (p_vout->output.i_height - p_vout->p_sys->i_height) / 2;
345 p_vout->p_sys->i_width = p_vout->fmt_out.i_visible_width;
346 p_vout->p_sys->i_height = p_vout->fmt_out.i_visible_height;
347 p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
348 p_vout->p_sys->i_y = p_vout->fmt_out.i_y_offset;
351 /* Pheeew. Parsing done. */
352 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
353 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
354 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
355 p_vout->p_sys->b_autocrop ? "" : "not " );
356 /* Set current output image properties */
357 p_vout->p_sys->i_aspect = p_vout->fmt_out.i_aspect
358 * p_vout->fmt_out.i_visible_height / p_vout->p_sys->i_height
359 * p_vout->p_sys->i_width / p_vout->fmt_out.i_visible_width;
362 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
364 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
365 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
366 fmt.i_x_offset = fmt.i_y_offset = 0;
367 fmt.i_chroma = p_vout->render.i_chroma;
368 fmt.i_aspect = p_vout->p_sys->i_aspect;
369 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
370 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
372 /* Try to open the real video output */
373 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
374 if( p_vout->p_sys->p_vout == NULL )
376 msg_Err( p_vout, "failed to create vout" );
377 intf_UserFatal( p_vout, VLC_FALSE, _("Cropping failed"),
378 _("VLC could not open the video output module.") );
382 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
384 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
387 var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
390 ADD_PARENT_CALLBACKS( SendEventsToChild );
395 /*****************************************************************************
396 * End: terminate Crop video thread output method
397 *****************************************************************************/
398 static void End( vout_thread_t *p_vout )
402 /* Free the fake output buffers we allocated */
403 for( i_index = I_OUTPUTPICTURES ; i_index ; )
406 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
410 /*****************************************************************************
411 * Destroy: destroy Crop video thread output method
412 *****************************************************************************
413 * Terminate an output method created by CropCreateOutputMethod
414 *****************************************************************************/
415 static void Destroy( vlc_object_t *p_this )
417 vout_thread_t *p_vout = (vout_thread_t *)p_this;
419 if( p_vout->p_sys->p_vout )
421 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
422 vlc_object_detach( p_vout->p_sys->p_vout );
423 vout_Destroy( p_vout->p_sys->p_vout );
426 DEL_PARENT_CALLBACKS( SendEventsToChild );
428 free( p_vout->p_sys );
431 /*****************************************************************************
432 * Manage: handle Crop events
433 *****************************************************************************
434 * This function should be called regularly by video output thread. It manages
435 * console events. It returns a non null value on error.
436 *****************************************************************************/
437 static int Manage( vout_thread_t *p_vout )
441 if( !p_vout->p_sys->b_changed )
446 memset( &fmt, 0, sizeof(video_format_t) );
449 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
450 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
451 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
452 p_vout->p_sys->b_autocrop ? "" : "not " );
454 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
457 vout_Destroy( p_vout->p_sys->p_vout );
459 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
460 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
461 fmt.i_x_offset = fmt.i_y_offset = 0;
462 fmt.i_chroma = p_vout->render.i_chroma;
463 fmt.i_aspect = p_vout->p_sys->i_aspect;
464 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
465 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
467 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
468 if( p_vout->p_sys->p_vout == NULL )
470 msg_Err( p_vout, "failed to create vout" );
471 intf_UserFatal( p_vout, VLC_FALSE, _("Cropping failed"),
472 _("VLC could not open the video output module.") );
476 p_vout->p_sys->b_changed = VLC_FALSE;
477 p_vout->p_sys->i_lastchange = 0;
482 /*****************************************************************************
483 * Render: display previously rendered output
484 *****************************************************************************
485 * This function sends the currently rendered image to Crop image, waits
486 * until it is displayed and switches the two rendering buffers, preparing next
488 *****************************************************************************/
489 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
491 picture_t *p_outpic = NULL;
494 if( p_vout->p_sys->b_changed )
500 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
503 if( p_vout->b_die || p_vout->b_error )
505 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
509 msleep( VOUT_OUTMEM_SLEEP );
512 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
513 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
515 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
517 uint8_t *p_in, *p_out, *p_out_end;
518 int i_in_pitch = p_pic->p[i_plane].i_pitch;
519 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
520 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
522 p_in = p_pic->p[i_plane].p_pixels
523 /* Skip the right amount of lines */
524 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
525 p_vout->p_sys->i_y / p_vout->output.i_height )
526 /* Skip the right amount of columns */
527 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
529 p_out = p_outpic->p[i_plane].p_pixels;
530 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
532 while( p_out < p_out_end )
534 p_vout->p_libvlc->pf_memcpy( p_out, p_in, i_copy_pitch );
536 p_out += i_out_pitch;
540 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
541 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
543 /* The source image may still be in the cache ... parse it! */
544 if( p_vout->p_sys->b_autocrop )
546 UpdateStats( p_vout, p_pic );
551 static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
552 int i_visible_pitch, int i_lines,
553 int i_lumThreshold, int i_skipCountPercent,
554 int i_nonBlackPixel, int i_chroma)
556 const int i_col = i_line * i_pitch / i_lines;
557 int i_index, i_count = 0;
563 case VLC_FOURCC('I','4','4','4'):
564 case VLC_FOURCC('I','4','2','2'):
565 case VLC_FOURCC('I','4','2','0'):
566 case VLC_FOURCC('Y','V','1','2'):
567 case VLC_FOURCC('I','Y','U','V'):
568 case VLC_FOURCC('I','4','1','1'):
569 case VLC_FOURCC('I','4','1','0'):
570 case VLC_FOURCC('Y','V','U','9'):
571 case VLC_FOURCC('Y','U','V','A'):
572 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
573 for (i_index = i_col/2 + i_skipCount/2;
574 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
577 i_count += (p_in[i_index] > i_lumThreshold);
578 if (i_count > i_nonBlackPixel) break;
582 case VLC_FOURCC('R','G','B','2'): // packed by 1
583 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
584 for (i_index = i_col/2 + i_skipCount/2;
585 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
588 i_count += (p_in[i_index] > i_lumThreshold);
589 if (i_count > i_nonBlackPixel) break;
592 case VLC_FOURCC('R','V','1','5'): // packed by 2
593 case VLC_FOURCC('R','V','1','6'): // packed by 2
594 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
595 for (i_index = i_col/2 + i_skipCount/2 -
596 (i_col/2 + i_skipCount/2) % 2;
597 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
600 i_count += (p_in[i_index] > i_lumThreshold) &&
601 (p_in[i_index + 1] > i_lumThreshold);
602 if (i_count > i_nonBlackPixel) break;
605 case VLC_FOURCC('R','V','2','4'): // packed by 3
606 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
607 for (i_index = i_col/2 + i_skipCount/2 - (i_col/2 + i_skipCount/2) % 3; i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2; i_index+=3)
609 i_count += (p_in[i_index] > i_lumThreshold) &&
610 (p_in[i_index + 1] > i_lumThreshold) &&
611 (p_in[i_index + 2] > i_lumThreshold);
612 if (i_count > i_nonBlackPixel) break;
615 case VLC_FOURCC('R','V','3','2'): // packed by 4
616 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
617 for (i_index = i_col/2 + i_skipCount/2 - (i_col/2 + i_skipCount/2) % 4; i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2; i_index+=4)
619 i_count += (uint32_t)(*(p_in + i_index)) > (uint32_t)i_lumThreshold;
620 if (i_count > i_nonBlackPixel) break;
624 case VLC_FOURCC('Y','U','Y','2'): // packed by 2
625 case VLC_FOURCC('Y','U','N','V'): // packed by 2
626 case VLC_FOURCC('U','Y','V','Y'): // packed by 2
627 case VLC_FOURCC('U','Y','N','V'): // packed by 2
628 case VLC_FOURCC('Y','4','2','2'): // packed by 2
629 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
630 for (i_index = (i_col/2 + i_skipCount/2) -
631 (i_col/2 + i_skipCount/2) % 2;
632 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
635 i_count += (p_in[i_index] > i_lumThreshold);
636 if (i_count > i_nonBlackPixel) break;
642 return (i_count > i_nonBlackPixel);
646 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
648 uint8_t *p_in = p_pic->p[0].p_pixels;
649 int i_pitch = p_pic->p[0].i_pitch;
650 int i_visible_pitch = p_pic->p[0].i_visible_pitch;
651 int i_lines = p_pic->p[0].i_visible_lines;
652 int i_firstwhite = -1, i_lastwhite = -1, i;
654 int i_time = p_vout->p_sys->i_time;
655 int i_diff = p_vout->p_sys->i_diff;
657 if (!p_vout->p_sys->i_ratio)
659 /* Determine where black borders are */
660 for( i = 0 ; i < i_lines ; i++)
662 if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
663 p_vout->p_sys->i_threshold,
664 p_vout->p_sys->i_skipPercent,
665 p_vout->p_sys->i_nonBlackPixel,
666 p_vout->output.i_chroma))
669 i_lastwhite = i_lines - i;
675 /* Decide whether it's worth changing the size */
676 if( i_lastwhite == -1 )
678 p_vout->p_sys->i_lastchange = 0;
682 if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
684 p_vout->p_sys->i_lastchange = 0;
688 if (p_vout->output.i_aspect
689 * p_vout->output.i_height /
690 (i_lastwhite - i_firstwhite + 1)
691 * p_vout->p_sys->i_width /
692 p_vout->output.i_width >
693 p_vout->p_sys->i_ratio_max * 432)
695 int i_height = ((p_vout->output.i_aspect / 432) *
696 p_vout->output.i_height * p_vout->p_sys->i_width) /
697 (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
698 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
699 i_lastwhite = p_vout->output.i_height - i_firstwhite;
701 p_vout->p_sys->i_lastchange = 0;
706 if( (i_lastwhite - i_firstwhite) <
707 (int) (p_vout->p_sys->i_height + i_diff)
708 && (i_lastwhite - i_firstwhite + i_diff) >
709 (int) p_vout->p_sys->i_height )
711 p_vout->p_sys->i_lastchange = 0;
715 /* We need at least 'i_time' images to make up our mind */
716 p_vout->p_sys->i_lastchange++;
717 if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
724 if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
726 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
727 int i_height = p_vout->output.i_aspect
728 * p_vout->output.i_height /
729 p_vout->p_sys->i_aspect
730 * p_vout->p_sys->i_width /
731 p_vout->output.i_width;
732 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
733 i_lastwhite = p_vout->output.i_height - i_firstwhite;
742 /* Determine where black borders are */
743 switch( p_vout->output.i_chroma )
745 case VLC_FOURCC('I','4','2','0'):
746 /* XXX: Do not laugh ! I know this is very naive. But it's just a
747 * proof of concept code snippet... */
748 for( i = i_lines ; i-- ; )
750 const int i_col = i * i_pitch / i_lines;
752 if( p_in[i_col/2] > 40
753 && p_in[i_visible_pitch/2] > 40
754 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
756 if( i_lastwhite == -1 )
770 /* Decide whether it's worth changing the size */
771 if( i_lastwhite == -1 )
773 p_vout->p_sys->i_lastchange = 0;
777 if( (unsigned int)(i_lastwhite - i_firstwhite)
778 < p_vout->p_sys->i_height / 2 )
780 p_vout->p_sys->i_lastchange = 0;
784 if( (unsigned int)(i_lastwhite - i_firstwhite)
785 < p_vout->p_sys->i_height + 16
786 && (unsigned int)(i_lastwhite - i_firstwhite + 16)
787 > p_vout->p_sys->i_height )
789 p_vout->p_sys->i_lastchange = 0;
793 /* We need at least 25 images to make up our mind */
794 p_vout->p_sys->i_lastchange++;
795 if( p_vout->p_sys->i_lastchange < 25 )
799 #endif //BEST_AUTOCROP
801 /* Tune a few values */
802 if( i_firstwhite & 1 )
807 if( !(i_lastwhite & 1) )
813 p_vout->p_sys->i_y = i_firstwhite;
814 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
816 // check p_vout->p_sys->i_height <= p_vout->output.i_height
817 if (p_vout->p_sys->i_height > p_vout->output.i_height)
818 p_vout->p_sys->i_height = p_vout->output.i_height;
821 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
822 * p_vout->output.i_height / p_vout->p_sys->i_height
823 * p_vout->p_sys->i_width / p_vout->output.i_width;
825 p_vout->p_sys->b_changed = VLC_TRUE;
828 /*****************************************************************************
829 * SendEvents: forward mouse and keyboard events to the parent p_vout
830 *****************************************************************************/
831 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
832 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
834 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
835 vlc_value_t sentval = newval;
837 /* Translate the mouse coordinates */
838 if( !strcmp( psz_var, "mouse-x" ) )
840 sentval.i_int += p_vout->p_sys->i_x;
842 else if( !strcmp( psz_var, "mouse-y" ) )
844 sentval.i_int += p_vout->p_sys->i_y;
847 var_Set( p_vout, psz_var, sentval );
852 /*****************************************************************************
853 * SendEventsToChild: forward events to the child/children vout
854 *****************************************************************************/
855 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
856 vlc_value_t oldval, vlc_value_t newval, void *p_data )
858 vout_thread_t *p_vout = (vout_thread_t *)p_this;
859 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
864 /*****************************************************************************
865 * FilterCallback: called when changing the ratio on the fly.
866 *****************************************************************************/
867 static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
868 vlc_value_t oldval, vlc_value_t newval,
871 vout_thread_t * p_vout = (vout_thread_t *)p_this;
873 if( !strcmp( psz_var, "ratio-crop" ) )
875 if ( !strcmp( newval.psz_string, "Auto" ) )
876 p_vout->p_sys->i_ratio = 0;
879 p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
880 p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
881 p_vout->p_sys->b_autocrop = VLC_TRUE;
883 if (p_vout->p_sys->i_ratio)
885 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) /
886 p_vout->output.i_height)
887 p_vout->p_sys->i_ratio = (p_vout->output.i_width * 1000) /
888 p_vout->output.i_height;
889 if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
890 p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;