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 *****************************************************************************/
32 #include <vlc_interface.h>
34 #include "filter_common.h"
36 #define BEST_AUTOCROP 1
38 #define RATIO_MAX 15000 // 10*4/3 for a 360
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Create ( vlc_object_t * );
45 static void Destroy ( vlc_object_t * );
47 static int Init ( vout_thread_t * );
48 static void End ( vout_thread_t * );
49 static int Manage ( vout_thread_t * );
50 static void Render ( vout_thread_t *, picture_t * );
52 static void UpdateStats ( vout_thread_t *, picture_t * );
54 static int SendEvents( vlc_object_t *, char const *,
55 vlc_value_t, vlc_value_t, void * );
58 /*****************************************************************************
60 *****************************************************************************/
61 static int FilterCallback ( vlc_object_t *, char const *,
62 vlc_value_t, vlc_value_t, void * );
65 /*****************************************************************************
67 *****************************************************************************/
68 #define GEOMETRY_TEXT N_("Crop geometry (pixels)")
69 #define GEOMETRY_LONGTEXT N_("Set the geometry of the zone to crop. This is set as <width> x <height> + <left offset> + <top offset>.")
71 #define AUTOCROP_TEXT N_("Automatic cropping")
72 #define AUTOCROP_LONGTEXT N_("Automatically detect black borders and crop them.")
75 #define RATIOMAX_TEXT N_("Ratio max (x 1000)")
76 #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.")
78 #define RATIO_TEXT N_("Manual ratio")
79 #define RATIO_LONGTEXT N_("Force a ratio (0 for automatic). Value is x1000: 1333 means 4/3.")
81 #define TIME_TEXT N_("Number of images for change")
82 #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.")
84 #define DIFF_TEXT N_("Number of lines for change")
85 #define DIFF_LONGTEXT N_("The minimum difference in the number of detected black lines to consider that ratio changed and trigger recrop.")
87 #define NBP_TEXT N_("Number of non black pixels ")
88 #define NBP_LONGTEXT N_("The maximum of non-black pixels in a line to consider"\
89 " that the line is black.")
91 #define SKIP_TEXT N_("Skip percentage (%)")
92 #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.")
94 #define LUM_TEXT N_("Luminance threshold ")
95 #define LUM_LONGTEXT N_("Maximum luminance to consider a pixel as black (0-255).")
99 set_description( _("Crop video filter") );
100 set_shortname( _("Crop" ));
101 set_category( CAT_VIDEO );
102 set_subcategory( SUBCAT_VIDEO_VFILTER );
103 set_capability( "video filter", 0 );
105 add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT,
106 GEOMETRY_LONGTEXT, VLC_FALSE );
108 add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT,
109 AUTOCROP_LONGTEXT, VLC_FALSE );
113 add_integer_with_range( "autocrop-ratio-max", 2405, 0, RATIO_MAX, NULL,
114 RATIOMAX_TEXT, RATIOMAX_LONGTEXT, VLC_TRUE );
117 add_integer_with_range( "crop-ratio", 0, 0, RATIO_MAX, NULL, RATIO_TEXT,
118 RATIO_LONGTEXT, VLC_FALSE );
120 add_integer( "autocrop-time", 25, NULL, TIME_TEXT,
121 TIME_LONGTEXT, VLC_TRUE );
123 add_integer( "autocrop-diff", 16, NULL, DIFF_TEXT,
124 DIFF_LONGTEXT, VLC_TRUE );
127 add_integer( "autocrop-non-black-pixels", 3, NULL,
128 NBP_TEXT, NBP_LONGTEXT, VLC_TRUE );
131 add_integer_with_range( "autocrop-skip-percent", 17, 0, 100, NULL,
132 SKIP_TEXT, SKIP_LONGTEXT, VLC_TRUE );
135 add_integer_with_range( "autocrop-luminance-threshold", 40, 0, 128, NULL,
136 LUM_TEXT, LUM_LONGTEXT, VLC_TRUE );
138 #endif //BEST_AUTOCROP
140 add_shortcut( "crop" );
141 set_callbacks( Create, Destroy );
144 /*****************************************************************************
145 * vout_sys_t: Crop video output method descriptor
146 *****************************************************************************
147 * This structure is part of the video output thread descriptor.
148 * It describes the Crop specific properties of an output thread.
149 *****************************************************************************/
152 vout_thread_t *p_vout;
154 unsigned int i_x, i_y;
155 unsigned int i_width, i_height, i_aspect;
157 vlc_bool_t b_autocrop;
159 /* Autocrop specific variables */
160 unsigned int i_lastchange;
161 vlc_bool_t b_changed;
163 unsigned int i_ratio_max;
164 unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
165 unsigned int i_ratio;
170 /*****************************************************************************
171 * Control: control facility for the vout (forwards to child vout)
172 *****************************************************************************/
173 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
175 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
178 /*****************************************************************************
179 * Create: allocates Crop video thread output method
180 *****************************************************************************
181 * This function allocates and initializes a Crop vout method.
182 *****************************************************************************/
183 static int Create( vlc_object_t *p_this )
185 vout_thread_t *p_vout = (vout_thread_t *)p_this;
187 /* Allocate structure */
188 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
189 if( p_vout->p_sys == NULL )
191 msg_Err( p_vout, "out of memory" );
195 p_vout->pf_init = Init;
196 p_vout->pf_end = End;
197 p_vout->pf_manage = Manage;
198 p_vout->pf_render = Render;
199 p_vout->pf_display = NULL;
200 p_vout->pf_control = Control;
205 /*****************************************************************************
206 * Init: initialize Crop video thread output method
207 *****************************************************************************/
208 static int Init( vout_thread_t *p_vout )
215 I_OUTPUTPICTURES = 0;
216 memset( &fmt, 0, sizeof(video_format_t) );
218 p_vout->p_sys->i_lastchange = 0;
219 p_vout->p_sys->b_changed = VLC_FALSE;
221 /* Initialize the output structure */
222 p_vout->output.i_chroma = p_vout->render.i_chroma;
223 p_vout->output.i_width = p_vout->render.i_width;
224 p_vout->output.i_height = p_vout->render.i_height;
225 p_vout->output.i_aspect = p_vout->render.i_aspect;
226 p_vout->fmt_out = p_vout->fmt_in;
228 /* Shall we use autocrop ? */
229 p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
231 p_vout->p_sys->i_ratio_max = config_GetInt( p_vout, "autocrop-ratio-max" );
232 p_vout->p_sys->i_threshold =
233 config_GetInt( p_vout, "autocrop-luminance-threshold" );
234 p_vout->p_sys->i_skipPercent =
235 config_GetInt( p_vout, "autocrop-skip-percent" );
236 p_vout->p_sys->i_nonBlackPixel =
237 config_GetInt( p_vout, "autocrop-non-black-pixels" );
238 p_vout->p_sys->i_diff = config_GetInt( p_vout, "autocrop-diff" );
239 p_vout->p_sys->i_time = config_GetInt( p_vout, "autocrop-time" );
241 var_Get( p_vout, "ratio-crop", &val );
242 val.psz_string = "0";
243 var_SetString( p_vout, "ratio-crop", val.psz_string);
245 if (p_vout->p_sys->b_autocrop)
246 p_vout->p_sys->i_ratio = 0;
249 p_vout->p_sys->i_ratio = config_GetInt( p_vout, "crop-ratio" );
250 // ratio < width / height => ratio = 0 (unchange ratio)
251 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) / p_vout->output.i_height)
252 p_vout->p_sys->i_ratio = 0;
257 /* Get geometry value from the user */
258 psz_var = config_GetPsz( p_vout, "crop-geometry" );
261 char *psz_parser, *psz_tmp;
263 psz_parser = psz_tmp = psz_var;
264 while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
269 p_vout->p_sys->i_width = atoi( psz_parser );
271 psz_parser = ++psz_tmp;
272 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
277 p_vout->p_sys->i_height = atoi( psz_parser );
279 psz_parser = ++psz_tmp;
280 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
285 p_vout->p_sys->i_x = atoi( psz_parser );
286 p_vout->p_sys->i_y = atoi( ++psz_tmp );
290 p_vout->p_sys->i_x = atoi( psz_parser );
292 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
297 p_vout->p_sys->i_height = atoi( psz_parser );
299 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
301 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
306 p_vout->p_sys->i_width = atoi( psz_parser );
307 p_vout->p_sys->i_height = p_vout->output.i_height;
309 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
311 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
314 /* Check for validity */
315 if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
316 > p_vout->output.i_width )
318 p_vout->p_sys->i_x = 0;
319 if( p_vout->p_sys->i_width > p_vout->output.i_width )
321 p_vout->p_sys->i_width = p_vout->output.i_width;
325 if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
326 > p_vout->output.i_height )
328 p_vout->p_sys->i_y = 0;
329 if( p_vout->p_sys->i_height > p_vout->output.i_height )
331 p_vout->p_sys->i_height = p_vout->output.i_height;
339 if (p_vout->p_sys->i_ratio)
341 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
342 p_vout->p_sys->i_width = p_vout->fmt_out.i_visible_width;
343 p_vout->p_sys->i_height = p_vout->output.i_aspect
344 * p_vout->output.i_height / p_vout->p_sys->i_aspect
345 * p_vout->p_sys->i_width / p_vout->output.i_width;
346 p_vout->p_sys->i_height += p_vout->p_sys->i_height % 2;
347 p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
348 p_vout->p_sys->i_y = (p_vout->output.i_height - p_vout->p_sys->i_height) / 2;
353 p_vout->p_sys->i_width = p_vout->fmt_out.i_visible_width;
354 p_vout->p_sys->i_height = p_vout->fmt_out.i_visible_height;
355 p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
356 p_vout->p_sys->i_y = p_vout->fmt_out.i_y_offset;
359 /* Pheeew. Parsing done. */
360 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
361 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
362 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
363 p_vout->p_sys->b_autocrop ? "" : "not " );
364 /* Set current output image properties */
365 p_vout->p_sys->i_aspect = p_vout->fmt_out.i_aspect
366 * p_vout->fmt_out.i_visible_height / p_vout->p_sys->i_height
367 * p_vout->p_sys->i_width / p_vout->fmt_out.i_visible_width;
370 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
372 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
373 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
374 fmt.i_x_offset = fmt.i_y_offset = 0;
375 fmt.i_chroma = p_vout->render.i_chroma;
376 fmt.i_aspect = p_vout->p_sys->i_aspect;
377 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
378 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
380 /* Try to open the real video output */
381 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
382 if( p_vout->p_sys->p_vout == NULL )
384 msg_Err( p_vout, "failed to create vout" );
385 intf_UserFatal( p_vout, VLC_FALSE, _("Cropping failed"),
386 _("VLC could not open the video output module.") );
390 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
392 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
395 var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
398 ADD_PARENT_CALLBACKS( SendEventsToChild );
403 /*****************************************************************************
404 * End: terminate Crop video thread output method
405 *****************************************************************************/
406 static void End( vout_thread_t *p_vout )
410 /* Free the fake output buffers we allocated */
411 for( i_index = I_OUTPUTPICTURES ; i_index ; )
414 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
418 /*****************************************************************************
419 * Destroy: destroy Crop video thread output method
420 *****************************************************************************
421 * Terminate an output method created by CropCreateOutputMethod
422 *****************************************************************************/
423 static void Destroy( vlc_object_t *p_this )
425 vout_thread_t *p_vout = (vout_thread_t *)p_this;
427 if( p_vout->p_sys->p_vout )
429 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
430 vlc_object_detach( p_vout->p_sys->p_vout );
431 vout_Destroy( p_vout->p_sys->p_vout );
434 DEL_PARENT_CALLBACKS( SendEventsToChild );
436 free( p_vout->p_sys );
439 /*****************************************************************************
440 * Manage: handle Crop events
441 *****************************************************************************
442 * This function should be called regularly by video output thread. It manages
443 * console events. It returns a non null value on error.
444 *****************************************************************************/
445 static int Manage( vout_thread_t *p_vout )
449 if( !p_vout->p_sys->b_changed )
454 memset( &fmt, 0, sizeof(video_format_t) );
457 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
458 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
459 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
460 p_vout->p_sys->b_autocrop ? "" : "not " );
462 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
465 vout_Destroy( p_vout->p_sys->p_vout );
467 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
468 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
469 fmt.i_x_offset = fmt.i_y_offset = 0;
470 fmt.i_chroma = p_vout->render.i_chroma;
471 fmt.i_aspect = p_vout->p_sys->i_aspect;
472 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
473 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
475 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
476 if( p_vout->p_sys->p_vout == NULL )
478 msg_Err( p_vout, "failed to create vout" );
479 intf_UserFatal( p_vout, VLC_FALSE, _("Cropping failed"),
480 _("VLC could not open the video output module.") );
484 p_vout->p_sys->b_changed = VLC_FALSE;
485 p_vout->p_sys->i_lastchange = 0;
490 /*****************************************************************************
491 * Render: display previously rendered output
492 *****************************************************************************
493 * This function sends the currently rendered image to Crop image, waits
494 * until it is displayed and switches the two rendering buffers, preparing next
496 *****************************************************************************/
497 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
499 picture_t *p_outpic = NULL;
502 if( p_vout->p_sys->b_changed )
508 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
511 if( p_vout->b_die || p_vout->b_error )
513 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
517 msleep( VOUT_OUTMEM_SLEEP );
520 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
521 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
523 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
525 uint8_t *p_in, *p_out, *p_out_end;
526 int i_in_pitch = p_pic->p[i_plane].i_pitch;
527 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
528 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
530 p_in = p_pic->p[i_plane].p_pixels
531 /* Skip the right amount of lines */
532 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
533 p_vout->p_sys->i_y / p_vout->output.i_height )
534 /* Skip the right amount of columns */
535 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
537 p_out = p_outpic->p[i_plane].p_pixels;
538 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
540 while( p_out < p_out_end )
542 p_vout->p_libvlc->pf_memcpy( p_out, p_in, i_copy_pitch );
544 p_out += i_out_pitch;
548 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
549 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
551 /* The source image may still be in the cache ... parse it! */
552 if( p_vout->p_sys->b_autocrop )
554 UpdateStats( p_vout, p_pic );
559 static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
560 int i_visible_pitch, int i_lines,
561 int i_lumThreshold, int i_skipCountPercent,
562 int i_nonBlackPixel, int i_chroma)
564 const int i_col = i_line * i_pitch / i_lines;
565 int i_index, i_count = 0;
571 case VLC_FOURCC('I','4','4','4'):
572 case VLC_FOURCC('I','4','2','2'):
573 case VLC_FOURCC('I','4','2','0'):
574 case VLC_FOURCC('Y','V','1','2'):
575 case VLC_FOURCC('I','Y','U','V'):
576 case VLC_FOURCC('I','4','1','1'):
577 case VLC_FOURCC('I','4','1','0'):
578 case VLC_FOURCC('Y','V','U','9'):
579 case VLC_FOURCC('Y','U','V','A'):
580 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
581 for (i_index = i_col/2 + i_skipCount/2;
582 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
585 i_count += (p_in[i_index] > i_lumThreshold);
586 if (i_count > i_nonBlackPixel) break;
590 case VLC_FOURCC('R','G','B','2'): // packed by 1
591 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
592 for (i_index = i_col/2 + i_skipCount/2;
593 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
596 i_count += (p_in[i_index] > i_lumThreshold);
597 if (i_count > i_nonBlackPixel) break;
600 case VLC_FOURCC('R','V','1','5'): // packed by 2
601 case VLC_FOURCC('R','V','1','6'): // packed by 2
602 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
603 for (i_index = i_col/2 + i_skipCount/2 -
604 (i_col/2 + i_skipCount/2) % 2;
605 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
608 i_count += (p_in[i_index] > i_lumThreshold) &&
609 (p_in[i_index + 1] > i_lumThreshold);
610 if (i_count > i_nonBlackPixel) break;
613 case VLC_FOURCC('R','V','2','4'): // packed by 3
614 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
615 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)
617 i_count += (p_in[i_index] > i_lumThreshold) &&
618 (p_in[i_index + 1] > i_lumThreshold) &&
619 (p_in[i_index + 2] > i_lumThreshold);
620 if (i_count > i_nonBlackPixel) break;
623 case VLC_FOURCC('R','V','3','2'): // packed by 4
624 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
625 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)
627 i_count += (uint32_t)(*(p_in + i_index)) > (uint32_t)i_lumThreshold;
628 if (i_count > i_nonBlackPixel) break;
632 case VLC_FOURCC('Y','U','Y','2'): // packed by 2
633 case VLC_FOURCC('Y','U','N','V'): // packed by 2
634 case VLC_FOURCC('U','Y','V','Y'): // packed by 2
635 case VLC_FOURCC('U','Y','N','V'): // packed by 2
636 case VLC_FOURCC('Y','4','2','2'): // packed by 2
637 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
638 for (i_index = (i_col/2 + i_skipCount/2) -
639 (i_col/2 + i_skipCount/2) % 2;
640 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
643 i_count += (p_in[i_index] > i_lumThreshold);
644 if (i_count > i_nonBlackPixel) break;
650 return (i_count > i_nonBlackPixel);
654 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
656 uint8_t *p_in = p_pic->p[0].p_pixels;
657 int i_pitch = p_pic->p[0].i_pitch;
658 int i_visible_pitch = p_pic->p[0].i_visible_pitch;
659 int i_lines = p_pic->p[0].i_visible_lines;
660 int i_firstwhite = -1, i_lastwhite = -1, i;
662 int i_time = p_vout->p_sys->i_time;
663 int i_diff = p_vout->p_sys->i_diff;
665 if (!p_vout->p_sys->i_ratio)
667 /* Determine where black borders are */
668 for( i = 0 ; i < i_lines ; i++)
670 if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
671 p_vout->p_sys->i_threshold,
672 p_vout->p_sys->i_skipPercent,
673 p_vout->p_sys->i_nonBlackPixel,
674 p_vout->output.i_chroma))
677 i_lastwhite = i_lines - i;
683 /* Decide whether it's worth changing the size */
684 if( i_lastwhite == -1 )
686 p_vout->p_sys->i_lastchange = 0;
690 if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
692 p_vout->p_sys->i_lastchange = 0;
696 if (p_vout->output.i_aspect
697 * p_vout->output.i_height /
698 (i_lastwhite - i_firstwhite + 1)
699 * p_vout->p_sys->i_width /
700 p_vout->output.i_width >
701 p_vout->p_sys->i_ratio_max * 432)
703 int i_height = ((p_vout->output.i_aspect / 432) *
704 p_vout->output.i_height * p_vout->p_sys->i_width) /
705 (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
706 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
707 i_lastwhite = p_vout->output.i_height - i_firstwhite;
709 p_vout->p_sys->i_lastchange = 0;
714 if( (i_lastwhite - i_firstwhite) <
715 (int) (p_vout->p_sys->i_height + i_diff)
716 && (i_lastwhite - i_firstwhite + i_diff) >
717 (int) p_vout->p_sys->i_height )
719 p_vout->p_sys->i_lastchange = 0;
723 /* We need at least 'i_time' images to make up our mind */
724 p_vout->p_sys->i_lastchange++;
725 if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
732 if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
734 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
735 int i_height = p_vout->output.i_aspect
736 * p_vout->output.i_height /
737 p_vout->p_sys->i_aspect
738 * p_vout->p_sys->i_width /
739 p_vout->output.i_width;
740 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
741 i_lastwhite = p_vout->output.i_height - i_firstwhite;
750 /* Determine where black borders are */
751 switch( p_vout->output.i_chroma )
753 case VLC_FOURCC('I','4','2','0'):
754 /* XXX: Do not laugh ! I know this is very naive. But it's just a
755 * proof of concept code snippet... */
756 for( i = i_lines ; i-- ; )
758 const int i_col = i * i_pitch / i_lines;
760 if( p_in[i_col/2] > 40
761 && p_in[i_visible_pitch/2] > 40
762 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
764 if( i_lastwhite == -1 )
778 /* Decide whether it's worth changing the size */
779 if( i_lastwhite == -1 )
781 p_vout->p_sys->i_lastchange = 0;
785 if( (unsigned int)(i_lastwhite - i_firstwhite)
786 < p_vout->p_sys->i_height / 2 )
788 p_vout->p_sys->i_lastchange = 0;
792 if( (unsigned int)(i_lastwhite - i_firstwhite)
793 < p_vout->p_sys->i_height + 16
794 && (unsigned int)(i_lastwhite - i_firstwhite + 16)
795 > p_vout->p_sys->i_height )
797 p_vout->p_sys->i_lastchange = 0;
801 /* We need at least 25 images to make up our mind */
802 p_vout->p_sys->i_lastchange++;
803 if( p_vout->p_sys->i_lastchange < 25 )
807 #endif //BEST_AUTOCROP
809 /* Tune a few values */
810 if( i_firstwhite & 1 )
815 if( !(i_lastwhite & 1) )
821 p_vout->p_sys->i_y = i_firstwhite;
822 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
824 // check p_vout->p_sys->i_height <= p_vout->output.i_height
825 if (p_vout->p_sys->i_height > p_vout->output.i_height)
826 p_vout->p_sys->i_height = p_vout->output.i_height;
829 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
830 * p_vout->output.i_height / p_vout->p_sys->i_height
831 * p_vout->p_sys->i_width / p_vout->output.i_width;
833 p_vout->p_sys->b_changed = VLC_TRUE;
836 /*****************************************************************************
837 * SendEvents: forward mouse and keyboard events to the parent p_vout
838 *****************************************************************************/
839 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
840 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
842 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
843 vlc_value_t sentval = newval;
845 /* Translate the mouse coordinates */
846 if( !strcmp( psz_var, "mouse-x" ) )
848 sentval.i_int += p_vout->p_sys->i_x;
850 else if( !strcmp( psz_var, "mouse-y" ) )
852 sentval.i_int += p_vout->p_sys->i_y;
855 var_Set( p_vout, psz_var, sentval );
860 /*****************************************************************************
861 * SendEventsToChild: forward events to the child/children vout
862 *****************************************************************************/
863 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
864 vlc_value_t oldval, vlc_value_t newval, void *p_data )
866 vout_thread_t *p_vout = (vout_thread_t *)p_this;
867 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
872 /*****************************************************************************
873 * FilterCallback: called when changing the ratio on the fly.
874 *****************************************************************************/
875 static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
876 vlc_value_t oldval, vlc_value_t newval,
879 vout_thread_t * p_vout = (vout_thread_t *)p_this;
881 if( !strcmp( psz_var, "ratio-crop" ) )
883 if ( !strcmp( newval.psz_string, "Auto" ) )
884 p_vout->p_sys->i_ratio = 0;
887 p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
888 p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
889 p_vout->p_sys->b_autocrop = VLC_TRUE;
891 if (p_vout->p_sys->i_ratio)
893 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) /
894 p_vout->output.i_height)
895 p_vout->p_sys->i_ratio = (p_vout->output.i_width * 1000) /
896 p_vout->output.i_height;
897 if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
898 p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;