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 *****************************************************************************/
36 #include <vlc_interface.h>
38 #include "filter_common.h"
40 #define BEST_AUTOCROP 1
42 #define RATIO_MAX 15000 // 10*4/3 for a 360
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Create ( vlc_object_t * );
49 static void Destroy ( vlc_object_t * );
51 static int Init ( vout_thread_t * );
52 static void End ( vout_thread_t * );
53 static int Manage ( vout_thread_t * );
54 static void Render ( vout_thread_t *, picture_t * );
56 static void UpdateStats ( vout_thread_t *, picture_t * );
58 static int SendEvents( vlc_object_t *, char const *,
59 vlc_value_t, vlc_value_t, void * );
62 /*****************************************************************************
64 *****************************************************************************/
65 static int FilterCallback ( vlc_object_t *, char const *,
66 vlc_value_t, vlc_value_t, void * );
69 /*****************************************************************************
71 *****************************************************************************/
72 #define GEOMETRY_TEXT N_("Crop geometry (pixels)")
73 #define GEOMETRY_LONGTEXT N_("Set the geometry of the zone to crop. This is set as <width> x <height> + <left offset> + <top offset>.")
75 #define AUTOCROP_TEXT N_("Automatic cropping")
76 #define AUTOCROP_LONGTEXT N_("Automatically detect black borders and crop them.")
79 #define RATIOMAX_TEXT N_("Ratio max (x 1000)")
80 #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.")
82 #define RATIO_TEXT N_("Manual ratio")
83 #define RATIO_LONGTEXT N_("Force a ratio (0 for automatic). Value is x1000: 1333 means 4/3.")
85 #define TIME_TEXT N_("Number of images for change")
86 #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.")
88 #define DIFF_TEXT N_("Number of lines for change")
89 #define DIFF_LONGTEXT N_("The minimum difference in the number of detected black lines to consider that ratio changed and trigger recrop.")
91 #define NBP_TEXT N_("Number of non black pixels ")
92 #define NBP_LONGTEXT N_("The maximum of non-black pixels in a line to consider"\
93 " that the line is black.")
95 #define SKIP_TEXT N_("Skip percentage (%)")
96 #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.")
98 #define LUM_TEXT N_("Luminance threshold ")
99 #define LUM_LONGTEXT N_("Maximum luminance to consider a pixel as black (0-255).")
103 set_description( _("Crop video filter") );
104 set_shortname( _("Crop" ));
105 set_category( CAT_VIDEO );
106 set_subcategory( SUBCAT_VIDEO_VFILTER );
107 set_capability( "video filter", 0 );
109 add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT,
110 GEOMETRY_LONGTEXT, VLC_FALSE );
111 add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT,
112 AUTOCROP_LONGTEXT, VLC_FALSE );
115 add_integer_with_range( "autocrop-ratio-max", 2405, 0, RATIO_MAX, NULL,
116 RATIOMAX_TEXT, RATIOMAX_LONGTEXT, VLC_TRUE );
118 add_integer_with_range( "crop-ratio", 0, 0, RATIO_MAX, NULL, RATIO_TEXT,
119 RATIO_LONGTEXT, VLC_FALSE );
120 add_integer( "autocrop-time", 25, NULL, TIME_TEXT,
121 TIME_LONGTEXT, VLC_TRUE );
122 add_integer( "autocrop-diff", 16, NULL, DIFF_TEXT,
123 DIFF_LONGTEXT, VLC_TRUE );
125 add_integer( "autocrop-non-black-pixels", 3, NULL,
126 NBP_TEXT, NBP_LONGTEXT, VLC_TRUE );
128 add_integer_with_range( "autocrop-skip-percent", 17, 0, 100, NULL,
129 SKIP_TEXT, SKIP_LONGTEXT, VLC_TRUE );
131 add_integer_with_range( "autocrop-luminance-threshold", 40, 0, 128, NULL,
132 LUM_TEXT, LUM_LONGTEXT, VLC_TRUE );
133 #endif //BEST_AUTOCROP
135 add_shortcut( "crop" );
136 set_callbacks( Create, Destroy );
139 /*****************************************************************************
140 * vout_sys_t: Crop video output method descriptor
141 *****************************************************************************
142 * This structure is part of the video output thread descriptor.
143 * It describes the Crop specific properties of an output thread.
144 *****************************************************************************/
147 vout_thread_t *p_vout;
149 unsigned int i_x, i_y;
150 unsigned int i_width, i_height, i_aspect;
152 vlc_bool_t b_autocrop;
154 /* Autocrop specific variables */
155 unsigned int i_lastchange;
156 vlc_bool_t b_changed;
158 unsigned int i_ratio_max;
159 unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
160 unsigned int i_ratio;
165 /*****************************************************************************
166 * Control: control facility for the vout (forwards to child vout)
167 *****************************************************************************/
168 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
170 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
173 /*****************************************************************************
174 * Create: allocates Crop video thread output method
175 *****************************************************************************
176 * This function allocates and initializes a Crop vout method.
177 *****************************************************************************/
178 static int Create( vlc_object_t *p_this )
180 vout_thread_t *p_vout = (vout_thread_t *)p_this;
182 /* Allocate structure */
183 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
184 if( p_vout->p_sys == NULL )
186 msg_Err( p_vout, "out of memory" );
190 p_vout->pf_init = Init;
191 p_vout->pf_end = End;
192 p_vout->pf_manage = Manage;
193 p_vout->pf_render = Render;
194 p_vout->pf_display = NULL;
195 p_vout->pf_control = Control;
200 /*****************************************************************************
201 * Init: initialize Crop video thread output method
202 *****************************************************************************/
203 static int Init( vout_thread_t *p_vout )
210 I_OUTPUTPICTURES = 0;
211 memset( &fmt, 0, sizeof(video_format_t) );
213 p_vout->p_sys->i_lastchange = 0;
214 p_vout->p_sys->b_changed = VLC_FALSE;
216 /* Initialize the output structure */
217 p_vout->output.i_chroma = p_vout->render.i_chroma;
218 p_vout->output.i_width = p_vout->render.i_width;
219 p_vout->output.i_height = p_vout->render.i_height;
220 p_vout->output.i_aspect = p_vout->render.i_aspect;
221 p_vout->fmt_out = p_vout->fmt_in;
223 /* Shall we use autocrop ? */
224 p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
226 p_vout->p_sys->i_ratio_max = config_GetInt( p_vout, "autocrop-ratio-max" );
227 p_vout->p_sys->i_threshold =
228 config_GetInt( p_vout, "autocrop-luminance-threshold" );
229 p_vout->p_sys->i_skipPercent =
230 config_GetInt( p_vout, "autocrop-skip-percent" );
231 p_vout->p_sys->i_nonBlackPixel =
232 config_GetInt( p_vout, "autocrop-non-black-pixels" );
233 p_vout->p_sys->i_diff = config_GetInt( p_vout, "autocrop-diff" );
234 p_vout->p_sys->i_time = config_GetInt( p_vout, "autocrop-time" );
236 var_Get( p_vout, "ratio-crop", &val );
237 val.psz_string = "0";
238 var_SetString( p_vout, "ratio-crop", val.psz_string);
240 if (p_vout->p_sys->b_autocrop)
241 p_vout->p_sys->i_ratio = 0;
244 p_vout->p_sys->i_ratio = config_GetInt( p_vout, "crop-ratio" );
245 // ratio < width / height => ratio = 0 (unchange ratio)
246 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) / p_vout->output.i_height)
247 p_vout->p_sys->i_ratio = 0;
252 /* Get geometry value from the user */
253 psz_var = config_GetPsz( p_vout, "crop-geometry" );
256 char *psz_parser, *psz_tmp;
258 psz_parser = psz_tmp = psz_var;
259 while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
264 p_vout->p_sys->i_width = atoi( psz_parser );
266 psz_parser = ++psz_tmp;
267 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
272 p_vout->p_sys->i_height = atoi( psz_parser );
274 psz_parser = ++psz_tmp;
275 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
280 p_vout->p_sys->i_x = atoi( psz_parser );
281 p_vout->p_sys->i_y = atoi( ++psz_tmp );
285 p_vout->p_sys->i_x = atoi( psz_parser );
287 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
292 p_vout->p_sys->i_height = atoi( psz_parser );
294 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
296 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
301 p_vout->p_sys->i_width = atoi( psz_parser );
302 p_vout->p_sys->i_height = p_vout->output.i_height;
304 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
306 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
309 /* Check for validity */
310 if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
311 > p_vout->output.i_width )
313 p_vout->p_sys->i_x = 0;
314 if( p_vout->p_sys->i_width > p_vout->output.i_width )
316 p_vout->p_sys->i_width = p_vout->output.i_width;
320 if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
321 > p_vout->output.i_height )
323 p_vout->p_sys->i_y = 0;
324 if( p_vout->p_sys->i_height > p_vout->output.i_height )
326 p_vout->p_sys->i_height = p_vout->output.i_height;
334 if (p_vout->p_sys->i_ratio)
336 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
337 p_vout->p_sys->i_width = p_vout->fmt_out.i_visible_width;
338 p_vout->p_sys->i_height = p_vout->output.i_aspect
339 * p_vout->output.i_height / p_vout->p_sys->i_aspect
340 * p_vout->p_sys->i_width / p_vout->output.i_width;
341 p_vout->p_sys->i_height += p_vout->p_sys->i_height % 2;
342 p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
343 p_vout->p_sys->i_y = (p_vout->output.i_height - p_vout->p_sys->i_height) / 2;
348 p_vout->p_sys->i_width = p_vout->fmt_out.i_visible_width;
349 p_vout->p_sys->i_height = p_vout->fmt_out.i_visible_height;
350 p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
351 p_vout->p_sys->i_y = p_vout->fmt_out.i_y_offset;
354 /* Pheeew. Parsing done. */
355 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
356 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
357 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
358 p_vout->p_sys->b_autocrop ? "" : "not " );
359 /* Set current output image properties */
360 p_vout->p_sys->i_aspect = p_vout->fmt_out.i_aspect
361 * p_vout->fmt_out.i_visible_height / p_vout->p_sys->i_height
362 * p_vout->p_sys->i_width / p_vout->fmt_out.i_visible_width;
365 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
367 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
368 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
369 fmt.i_x_offset = fmt.i_y_offset = 0;
370 fmt.i_chroma = p_vout->render.i_chroma;
371 fmt.i_aspect = p_vout->p_sys->i_aspect;
372 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
373 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
375 /* Try to open the real video output */
376 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
377 if( p_vout->p_sys->p_vout == NULL )
379 msg_Err( p_vout, "failed to create vout" );
380 intf_UserFatal( p_vout, VLC_FALSE, _("Cropping failed"),
381 _("VLC could not open the video output module.") );
385 ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
387 ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
390 var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
393 ADD_PARENT_CALLBACKS( SendEventsToChild );
398 /*****************************************************************************
399 * End: terminate Crop video thread output method
400 *****************************************************************************/
401 static void End( vout_thread_t *p_vout )
405 /* Free the fake output buffers we allocated */
406 for( i_index = I_OUTPUTPICTURES ; i_index ; )
409 free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
413 /*****************************************************************************
414 * Destroy: destroy Crop video thread output method
415 *****************************************************************************
416 * Terminate an output method created by CropCreateOutputMethod
417 *****************************************************************************/
418 static void Destroy( vlc_object_t *p_this )
420 vout_thread_t *p_vout = (vout_thread_t *)p_this;
422 if( p_vout->p_sys->p_vout )
424 DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
425 vlc_object_detach( p_vout->p_sys->p_vout );
426 vout_Destroy( p_vout->p_sys->p_vout );
429 DEL_PARENT_CALLBACKS( SendEventsToChild );
431 free( p_vout->p_sys );
434 /*****************************************************************************
435 * Manage: handle Crop events
436 *****************************************************************************
437 * This function should be called regularly by video output thread. It manages
438 * console events. It returns a non null value on error.
439 *****************************************************************************/
440 static int Manage( vout_thread_t *p_vout )
444 if( !p_vout->p_sys->b_changed )
449 memset( &fmt, 0, sizeof(video_format_t) );
452 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
453 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
454 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
455 p_vout->p_sys->b_autocrop ? "" : "not " );
457 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
460 vout_Destroy( p_vout->p_sys->p_vout );
462 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
463 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
464 fmt.i_x_offset = fmt.i_y_offset = 0;
465 fmt.i_chroma = p_vout->render.i_chroma;
466 fmt.i_aspect = p_vout->p_sys->i_aspect;
467 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
468 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
470 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
471 if( p_vout->p_sys->p_vout == NULL )
473 msg_Err( p_vout, "failed to create vout" );
474 intf_UserFatal( p_vout, VLC_FALSE, _("Cropping failed"),
475 _("VLC could not open the video output module.") );
479 p_vout->p_sys->b_changed = VLC_FALSE;
480 p_vout->p_sys->i_lastchange = 0;
485 /*****************************************************************************
486 * Render: display previously rendered output
487 *****************************************************************************
488 * This function sends the currently rendered image to Crop image, waits
489 * until it is displayed and switches the two rendering buffers, preparing next
491 *****************************************************************************/
492 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
494 picture_t *p_outpic = NULL;
497 if( p_vout->p_sys->b_changed )
503 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
506 if( p_vout->b_die || p_vout->b_error )
508 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
512 msleep( VOUT_OUTMEM_SLEEP );
515 vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
516 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
518 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
520 uint8_t *p_in, *p_out, *p_out_end;
521 int i_in_pitch = p_pic->p[i_plane].i_pitch;
522 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
523 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
525 p_in = p_pic->p[i_plane].p_pixels
526 /* Skip the right amount of lines */
527 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
528 p_vout->p_sys->i_y / p_vout->output.i_height )
529 /* Skip the right amount of columns */
530 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
532 p_out = p_outpic->p[i_plane].p_pixels;
533 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
535 while( p_out < p_out_end )
537 p_vout->p_libvlc->pf_memcpy( p_out, p_in, i_copy_pitch );
539 p_out += i_out_pitch;
543 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
544 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
546 /* The source image may still be in the cache ... parse it! */
547 if( p_vout->p_sys->b_autocrop )
549 UpdateStats( p_vout, p_pic );
554 static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
555 int i_visible_pitch, int i_lines,
556 int i_lumThreshold, int i_skipCountPercent,
557 int i_nonBlackPixel, int i_chroma)
559 const int i_col = i_line * i_pitch / i_lines;
560 int i_index, i_count = 0;
566 case VLC_FOURCC('I','4','4','4'):
567 case VLC_FOURCC('I','4','2','2'):
568 case VLC_FOURCC('I','4','2','0'):
569 case VLC_FOURCC('Y','V','1','2'):
570 case VLC_FOURCC('I','Y','U','V'):
571 case VLC_FOURCC('I','4','1','1'):
572 case VLC_FOURCC('I','4','1','0'):
573 case VLC_FOURCC('Y','V','U','9'):
574 case VLC_FOURCC('Y','U','V','A'):
575 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
576 for (i_index = i_col/2 + i_skipCount/2;
577 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
580 i_count += (p_in[i_index] > i_lumThreshold);
581 if (i_count > i_nonBlackPixel) break;
585 case VLC_FOURCC('R','G','B','2'): // packed by 1
586 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
587 for (i_index = i_col/2 + i_skipCount/2;
588 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
591 i_count += (p_in[i_index] > i_lumThreshold);
592 if (i_count > i_nonBlackPixel) break;
595 case VLC_FOURCC('R','V','1','5'): // packed by 2
596 case VLC_FOURCC('R','V','1','6'): // packed by 2
597 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
598 for (i_index = i_col/2 + i_skipCount/2 -
599 (i_col/2 + i_skipCount/2) % 2;
600 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
603 i_count += (p_in[i_index] > i_lumThreshold) &&
604 (p_in[i_index + 1] > i_lumThreshold);
605 if (i_count > i_nonBlackPixel) break;
608 case VLC_FOURCC('R','V','2','4'): // packed by 3
609 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
610 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)
612 i_count += (p_in[i_index] > i_lumThreshold) &&
613 (p_in[i_index + 1] > i_lumThreshold) &&
614 (p_in[i_index + 2] > i_lumThreshold);
615 if (i_count > i_nonBlackPixel) break;
618 case VLC_FOURCC('R','V','3','2'): // packed by 4
619 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
620 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)
622 i_count += (uint32_t)(*(p_in + i_index)) > (uint32_t)i_lumThreshold;
623 if (i_count > i_nonBlackPixel) break;
627 case VLC_FOURCC('Y','U','Y','2'): // packed by 2
628 case VLC_FOURCC('Y','U','N','V'): // packed by 2
629 case VLC_FOURCC('U','Y','V','Y'): // packed by 2
630 case VLC_FOURCC('U','Y','N','V'): // packed by 2
631 case VLC_FOURCC('Y','4','2','2'): // packed by 2
632 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
633 for (i_index = (i_col/2 + i_skipCount/2) -
634 (i_col/2 + i_skipCount/2) % 2;
635 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
638 i_count += (p_in[i_index] > i_lumThreshold);
639 if (i_count > i_nonBlackPixel) break;
645 return (i_count > i_nonBlackPixel);
649 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
651 uint8_t *p_in = p_pic->p[0].p_pixels;
652 int i_pitch = p_pic->p[0].i_pitch;
653 int i_visible_pitch = p_pic->p[0].i_visible_pitch;
654 int i_lines = p_pic->p[0].i_visible_lines;
655 int i_firstwhite = -1, i_lastwhite = -1, i;
657 int i_time = p_vout->p_sys->i_time;
658 int i_diff = p_vout->p_sys->i_diff;
660 if (!p_vout->p_sys->i_ratio)
662 /* Determine where black borders are */
663 for( i = 0 ; i < i_lines ; i++)
665 if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
666 p_vout->p_sys->i_threshold,
667 p_vout->p_sys->i_skipPercent,
668 p_vout->p_sys->i_nonBlackPixel,
669 p_vout->output.i_chroma))
672 i_lastwhite = i_lines - i;
678 /* Decide whether it's worth changing the size */
679 if( i_lastwhite == -1 )
681 p_vout->p_sys->i_lastchange = 0;
685 if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
687 p_vout->p_sys->i_lastchange = 0;
691 if (p_vout->output.i_aspect
692 * p_vout->output.i_height /
693 (i_lastwhite - i_firstwhite + 1)
694 * p_vout->p_sys->i_width /
695 p_vout->output.i_width >
696 p_vout->p_sys->i_ratio_max * 432)
698 int i_height = ((p_vout->output.i_aspect / 432) *
699 p_vout->output.i_height * p_vout->p_sys->i_width) /
700 (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
701 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
702 i_lastwhite = p_vout->output.i_height - i_firstwhite;
704 p_vout->p_sys->i_lastchange = 0;
709 if( (i_lastwhite - i_firstwhite) <
710 (int) (p_vout->p_sys->i_height + i_diff)
711 && (i_lastwhite - i_firstwhite + i_diff) >
712 (int) p_vout->p_sys->i_height )
714 p_vout->p_sys->i_lastchange = 0;
718 /* We need at least 'i_time' images to make up our mind */
719 p_vout->p_sys->i_lastchange++;
720 if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
727 if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
729 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
730 int i_height = p_vout->output.i_aspect
731 * p_vout->output.i_height /
732 p_vout->p_sys->i_aspect
733 * p_vout->p_sys->i_width /
734 p_vout->output.i_width;
735 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
736 i_lastwhite = p_vout->output.i_height - i_firstwhite;
745 /* Determine where black borders are */
746 switch( p_vout->output.i_chroma )
748 case VLC_FOURCC('I','4','2','0'):
749 /* XXX: Do not laugh ! I know this is very naive. But it's just a
750 * proof of concept code snippet... */
751 for( i = i_lines ; i-- ; )
753 const int i_col = i * i_pitch / i_lines;
755 if( p_in[i_col/2] > 40
756 && p_in[i_visible_pitch/2] > 40
757 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
759 if( i_lastwhite == -1 )
773 /* Decide whether it's worth changing the size */
774 if( i_lastwhite == -1 )
776 p_vout->p_sys->i_lastchange = 0;
780 if( (unsigned int)(i_lastwhite - i_firstwhite)
781 < p_vout->p_sys->i_height / 2 )
783 p_vout->p_sys->i_lastchange = 0;
787 if( (unsigned int)(i_lastwhite - i_firstwhite)
788 < p_vout->p_sys->i_height + 16
789 && (unsigned int)(i_lastwhite - i_firstwhite + 16)
790 > p_vout->p_sys->i_height )
792 p_vout->p_sys->i_lastchange = 0;
796 /* We need at least 25 images to make up our mind */
797 p_vout->p_sys->i_lastchange++;
798 if( p_vout->p_sys->i_lastchange < 25 )
802 #endif //BEST_AUTOCROP
804 /* Tune a few values */
805 if( i_firstwhite & 1 )
810 if( !(i_lastwhite & 1) )
816 p_vout->p_sys->i_y = i_firstwhite;
817 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
819 // check p_vout->p_sys->i_height <= p_vout->output.i_height
820 if (p_vout->p_sys->i_height > p_vout->output.i_height)
821 p_vout->p_sys->i_height = p_vout->output.i_height;
824 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
825 * p_vout->output.i_height / p_vout->p_sys->i_height
826 * p_vout->p_sys->i_width / p_vout->output.i_width;
828 p_vout->p_sys->b_changed = VLC_TRUE;
831 /*****************************************************************************
832 * SendEvents: forward mouse and keyboard events to the parent p_vout
833 *****************************************************************************/
834 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
835 vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
837 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
838 vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
839 vlc_value_t sentval = newval;
841 /* Translate the mouse coordinates */
842 if( !strcmp( psz_var, "mouse-x" ) )
844 sentval.i_int += p_vout->p_sys->i_x;
846 else if( !strcmp( psz_var, "mouse-y" ) )
848 sentval.i_int += p_vout->p_sys->i_y;
851 var_Set( p_vout, psz_var, sentval );
856 /*****************************************************************************
857 * SendEventsToChild: forward events to the child/children vout
858 *****************************************************************************/
859 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
860 vlc_value_t oldval, vlc_value_t newval, void *p_data )
862 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
863 vout_thread_t *p_vout = (vout_thread_t *)p_this;
864 var_Set( p_vout->p_sys->p_vout, psz_var, newval );
869 /*****************************************************************************
870 * FilterCallback: called when changing the ratio on the fly.
871 *****************************************************************************/
872 static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
873 vlc_value_t oldval, vlc_value_t newval,
876 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
877 vout_thread_t * p_vout = (vout_thread_t *)p_this;
879 if( !strcmp( psz_var, "ratio-crop" ) )
881 if ( !strcmp( newval.psz_string, "Auto" ) )
882 p_vout->p_sys->i_ratio = 0;
885 p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
886 p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
887 p_vout->p_sys->b_autocrop = VLC_TRUE;
889 if (p_vout->p_sys->i_ratio)
891 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) /
892 p_vout->output.i_height)
893 p_vout->p_sys->i_ratio = (p_vout->output.i_width * 1000) /
894 p_vout->output.i_height;
895 if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
896 p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;