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 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
37 #include <vlc_dialog.h>
39 #include "filter_common.h"
41 #define BEST_AUTOCROP 1
43 #define RATIO_MAX 15000 // 10*4/3 for a 360
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Create ( vlc_object_t * );
50 static void Destroy ( vlc_object_t * );
52 static int Init ( vout_thread_t * );
53 static void End ( vout_thread_t * );
54 static int Manage ( vout_thread_t * );
55 static void Render ( vout_thread_t *, picture_t * );
57 static void UpdateStats ( vout_thread_t *, picture_t * );
59 static int MouseEvent( vlc_object_t *, char const *,
60 vlc_value_t, vlc_value_t, void * );
63 /*****************************************************************************
65 *****************************************************************************/
66 static int FilterCallback ( vlc_object_t *, char const *,
67 vlc_value_t, vlc_value_t, void * );
70 /*****************************************************************************
72 *****************************************************************************/
73 #define GEOMETRY_TEXT N_("Crop geometry (pixels)")
74 #define GEOMETRY_LONGTEXT N_("Set the geometry of the zone to crop. This is set as <width> x <height> + <left offset> + <top offset>.")
76 #define AUTOCROP_TEXT N_("Automatic cropping")
77 #define AUTOCROP_LONGTEXT N_("Automatically detect black borders and crop them.")
79 #define CROP_HELP N_("Remove borders of the video and replace them by black borders")
82 #define RATIOMAX_TEXT N_("Ratio max (x 1000)")
83 #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.")
85 #define RATIO_TEXT N_("Manual ratio")
86 #define RATIO_LONGTEXT N_("Force a ratio (0 for automatic). Value is x1000: 1333 means 4/3.")
88 #define TIME_TEXT N_("Number of images for change")
89 #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.")
91 #define DIFF_TEXT N_("Number of lines for change")
92 #define DIFF_LONGTEXT N_("The minimum difference in the number of detected black lines to consider that ratio changed and trigger recrop.")
94 #define NBP_TEXT N_("Number of non black pixels ")
95 #define NBP_LONGTEXT N_("The maximum of non-black pixels in a line to consider"\
96 " that the line is black.")
98 #define SKIP_TEXT N_("Skip percentage (%)")
99 #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.")
101 #define LUM_TEXT N_("Luminance threshold ")
102 #define LUM_LONGTEXT N_("Maximum luminance to consider a pixel as black (0-255).")
106 set_description( N_("Crop video filter") )
107 set_shortname( N_("Crop" ))
109 set_category( CAT_VIDEO )
110 set_subcategory( SUBCAT_VIDEO_VFILTER )
111 set_capability( "video filter", 0 )
113 add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT,
114 GEOMETRY_LONGTEXT, false )
115 add_bool( "autocrop", false, NULL, AUTOCROP_TEXT,
116 AUTOCROP_LONGTEXT, false )
119 add_integer_with_range( "autocrop-ratio-max", 2405, 0, RATIO_MAX, NULL,
120 RATIOMAX_TEXT, RATIOMAX_LONGTEXT, true )
122 add_integer_with_range( "crop-ratio", 0, 0, RATIO_MAX, NULL, RATIO_TEXT,
123 RATIO_LONGTEXT, false )
124 add_integer( "autocrop-time", 25, NULL, TIME_TEXT,
125 TIME_LONGTEXT, true )
126 add_integer( "autocrop-diff", 16, NULL, DIFF_TEXT,
127 DIFF_LONGTEXT, true )
129 add_integer( "autocrop-non-black-pixels", 3, NULL,
130 NBP_TEXT, NBP_LONGTEXT, true )
132 add_integer_with_range( "autocrop-skip-percent", 17, 0, 100, NULL,
133 SKIP_TEXT, SKIP_LONGTEXT, true )
135 add_integer_with_range( "autocrop-luminance-threshold", 40, 0, 128, NULL,
136 LUM_TEXT, LUM_LONGTEXT, true )
137 #endif //BEST_AUTOCROP
139 add_shortcut( "crop" )
140 set_callbacks( Create, Destroy )
143 /*****************************************************************************
144 * vout_sys_t: Crop video output method descriptor
145 *****************************************************************************
146 * This structure is part of the video output thread descriptor.
147 * It describes the Crop specific properties of an output thread.
148 *****************************************************************************/
152 vout_thread_t *p_vout;
154 unsigned int i_x, i_y;
155 unsigned int i_width, i_height, i_aspect;
159 /* Autocrop specific variables */
160 unsigned int i_lastchange;
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 )
192 p_vout->pf_init = Init;
193 p_vout->pf_end = End;
194 p_vout->pf_manage = Manage;
195 p_vout->pf_render = Render;
196 p_vout->pf_display = NULL;
197 p_vout->pf_control = Control;
202 /*****************************************************************************
203 * Init: initialize Crop video thread output method
204 *****************************************************************************/
205 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 = 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" );
235 var_SetString( p_vout, "ratio-crop", "0" );
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 = (int64_t)VOUT_ASPECT_FACTOR *
358 p_vout->fmt_out.i_sar_num * p_vout->p_sys->i_width /
359 (p_vout->fmt_out.i_sar_den * p_vout->p_sys->i_height);
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_sar_num = p_vout->p_sys->i_aspect * fmt.i_height;
369 fmt.i_sar_den = VOUT_ASPECT_FACTOR * fmt.i_width;
371 /* Try to open the real video output */
372 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
373 if( p_vout->p_sys->p_vout == NULL )
375 msg_Err( p_vout, "failed to create vout" );
376 dialog_Fatal( p_vout, _("Cropping failed"), "%s",
377 _("VLC could not open the video output module.") );
381 vlc_mutex_init( &p_vout->p_sys->lock );
383 var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
386 vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
388 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
393 /*****************************************************************************
394 * End: terminate Crop video thread output method
395 *****************************************************************************/
396 static void End( vout_thread_t *p_vout )
398 vout_sys_t *p_sys = p_vout->p_sys;
402 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
403 vout_CloseAndRelease( p_sys->p_vout );
406 vout_filter_ReleaseDirectBuffers( p_vout );
407 var_DelCallback( p_vout, "ratio-crop", FilterCallback, NULL );
408 vlc_mutex_destroy( &p_sys->lock );
411 /*****************************************************************************
412 * Destroy: destroy Crop video thread output method
413 *****************************************************************************
414 * Terminate an output method created by CropCreateOutputMethod
415 *****************************************************************************/
416 static void Destroy( vlc_object_t *p_this )
418 vout_thread_t *p_vout = (vout_thread_t *)p_this;
420 free( p_vout->p_sys );
423 /*****************************************************************************
424 * Manage: handle Crop events
425 *****************************************************************************
426 * This function should be called regularly by video output thread. It manages
427 * console events. It returns a non null value on error.
428 *****************************************************************************/
429 static int Manage( vout_thread_t *p_vout )
433 if( !p_vout->p_sys->b_changed )
438 memset( &fmt, 0, sizeof(video_format_t) );
441 /* XXX: not thread-safe with FilterCallback */
442 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
443 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
444 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
445 p_vout->p_sys->b_autocrop ? "" : "not " );
447 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
450 if( p_vout->p_sys->p_vout )
452 vout_filter_DelChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
453 vout_CloseAndRelease( p_vout->p_sys->p_vout );
456 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
457 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
458 fmt.i_x_offset = fmt.i_y_offset = 0;
459 fmt.i_chroma = p_vout->render.i_chroma;
460 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
461 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
463 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
464 if( p_vout->p_sys->p_vout == NULL )
466 msg_Err( p_vout, "failed to create vout" );
467 dialog_Fatal( p_vout, _("Cropping failed"), "%s",
468 _("VLC could not open the video output module.") );
471 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
473 p_vout->p_sys->b_changed = false;
474 vlc_mutex_lock( &p_vout->p_sys->lock );
475 p_vout->p_sys->i_lastchange = 0;
476 vlc_mutex_unlock( &p_vout->p_sys->lock );
481 /*****************************************************************************
482 * Render: display previously rendered output
483 *****************************************************************************
484 * This function sends the currently rendered image to Crop image, waits
485 * until it is displayed and switches the two rendering buffers, preparing next
487 *****************************************************************************/
488 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
490 picture_t *p_outpic = NULL;
493 if( p_vout->p_sys->b_changed )
499 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
502 if( !vlc_object_alive (p_vout) || p_vout->b_error )
504 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
508 msleep( VOUT_OUTMEM_SLEEP );
511 p_outpic->date = p_pic->date;
512 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
514 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
516 uint8_t *p_in, *p_out, *p_out_end;
517 int i_in_pitch = p_pic->p[i_plane].i_pitch;
518 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
519 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
521 p_in = p_pic->p[i_plane].p_pixels
522 /* Skip the right amount of lines */
523 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
524 p_vout->p_sys->i_y / p_vout->output.i_height )
525 /* Skip the right amount of columns */
526 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
528 p_out = p_outpic->p[i_plane].p_pixels;
529 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
531 while( p_out < p_out_end )
533 vlc_memcpy( p_out, p_in, i_copy_pitch );
535 p_out += i_out_pitch;
539 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
540 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
542 /* The source image may still be in the cache ... parse it! */
543 vlc_mutex_lock( &p_vout->p_sys->lock );
544 if( p_vout->p_sys->b_autocrop )
545 UpdateStats( p_vout, p_pic );
546 vlc_mutex_unlock( &p_vout->p_sys->lock );
550 static bool NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
551 int i_visible_pitch, int i_lines,
552 int i_lumThreshold, int i_skipCountPercent,
553 int i_nonBlackPixel, int i_chroma)
555 const int i_col = i_line * i_pitch / i_lines;
556 int i_index, i_count = 0;
569 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
570 for (i_index = i_col/2 + i_skipCount/2;
571 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
574 i_count += (p_in[i_index] > i_lumThreshold);
575 if (i_count > i_nonBlackPixel) break;
579 case VLC_CODEC_RGB8: // packed by 1
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;
589 case VLC_CODEC_RGB15: // packed by 2
590 case VLC_CODEC_RGB16: // packed by 2
591 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
592 for (i_index = i_col/2 + i_skipCount/2 -
593 (i_col/2 + i_skipCount/2) % 2;
594 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
597 i_count += (p_in[i_index] > i_lumThreshold) &&
598 (p_in[i_index + 1] > i_lumThreshold);
599 if (i_count > i_nonBlackPixel) break;
602 case VLC_CODEC_RGB24: // packed by 3
603 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
604 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)
606 i_count += (p_in[i_index] > i_lumThreshold) &&
607 (p_in[i_index + 1] > i_lumThreshold) &&
608 (p_in[i_index + 2] > i_lumThreshold);
609 if (i_count > i_nonBlackPixel) break;
612 case VLC_CODEC_RGB32: // packed by 4
613 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
614 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)
616 i_count += (uint32_t)(*(p_in + i_index)) > (uint32_t)i_lumThreshold;
617 if (i_count > i_nonBlackPixel) break;
621 case VLC_CODEC_YUYV: // packed by 2
622 case VLC_CODEC_UYVY: // packed by 2
623 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
624 for (i_index = (i_col/2 + i_skipCount/2) -
625 (i_col/2 + i_skipCount/2) % 2;
626 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
629 i_count += (p_in[i_index] > i_lumThreshold);
630 if (i_count > i_nonBlackPixel) break;
636 return (i_count > i_nonBlackPixel);
640 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
642 uint8_t *p_in = p_pic->p[0].p_pixels;
643 int i_pitch = p_pic->p[0].i_pitch;
644 int i_visible_pitch = p_pic->p[0].i_visible_pitch;
645 int i_lines = p_pic->p[0].i_visible_lines;
646 int i_firstwhite = -1, i_lastwhite = -1, i;
648 int i_time = p_vout->p_sys->i_time;
649 int i_diff = p_vout->p_sys->i_diff;
651 if (!p_vout->p_sys->i_ratio)
653 /* Determine where black borders are */
654 for( i = 0 ; i < i_lines ; i++)
656 if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
657 p_vout->p_sys->i_threshold,
658 p_vout->p_sys->i_skipPercent,
659 p_vout->p_sys->i_nonBlackPixel,
660 p_vout->output.i_chroma))
663 i_lastwhite = i_lines - i;
669 /* Decide whether it's worth changing the size */
670 if( i_lastwhite == -1 )
672 p_vout->p_sys->i_lastchange = 0;
676 if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
678 p_vout->p_sys->i_lastchange = 0;
682 if (p_vout->output.i_aspect
683 * p_vout->output.i_height /
684 (i_lastwhite - i_firstwhite + 1)
685 * p_vout->p_sys->i_width /
686 p_vout->output.i_width >
687 p_vout->p_sys->i_ratio_max * 432)
689 int i_height = ((p_vout->output.i_aspect / 432) *
690 p_vout->output.i_height * p_vout->p_sys->i_width) /
691 (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
692 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
693 i_lastwhite = p_vout->output.i_height - i_firstwhite;
695 p_vout->p_sys->i_lastchange = 0;
700 if( (i_lastwhite - i_firstwhite) <
701 (int) (p_vout->p_sys->i_height + i_diff)
702 && (i_lastwhite - i_firstwhite + i_diff) >
703 (int) p_vout->p_sys->i_height )
705 p_vout->p_sys->i_lastchange = 0;
709 /* We need at least 'i_time' images to make up our mind */
710 p_vout->p_sys->i_lastchange++;
711 if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
718 if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
720 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
721 int i_height = p_vout->output.i_aspect
722 * p_vout->output.i_height /
723 p_vout->p_sys->i_aspect
724 * p_vout->p_sys->i_width /
725 p_vout->output.i_width;
726 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
727 i_lastwhite = p_vout->output.i_height - i_firstwhite;
736 /* Determine where black borders are */
737 switch( p_vout->output.i_chroma )
740 /* XXX: Do not laugh ! I know this is very naive. But it's just a
741 * proof of concept code snippet... */
742 for( i = i_lines ; i-- ; )
744 const int i_col = i * i_pitch / i_lines;
746 if( p_in[i_col/2] > 40
747 && p_in[i_visible_pitch/2] > 40
748 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
750 if( i_lastwhite == -1 )
764 /* Decide whether it's worth changing the size */
765 if( i_lastwhite == -1 )
767 p_vout->p_sys->i_lastchange = 0;
771 if( (unsigned int)(i_lastwhite - i_firstwhite)
772 < p_vout->p_sys->i_height / 2 )
774 p_vout->p_sys->i_lastchange = 0;
778 if( (unsigned int)(i_lastwhite - i_firstwhite)
779 < p_vout->p_sys->i_height + 16
780 && (unsigned int)(i_lastwhite - i_firstwhite + 16)
781 > p_vout->p_sys->i_height )
783 p_vout->p_sys->i_lastchange = 0;
787 /* We need at least 25 images to make up our mind */
788 p_vout->p_sys->i_lastchange++;
789 if( p_vout->p_sys->i_lastchange < 25 )
793 #endif //BEST_AUTOCROP
795 /* Tune a few values */
796 if( i_firstwhite & 1 )
801 if( !(i_lastwhite & 1) )
807 p_vout->p_sys->i_y = i_firstwhite;
808 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
810 // check p_vout->p_sys->i_height <= p_vout->output.i_height
811 if (p_vout->p_sys->i_height > p_vout->output.i_height)
812 p_vout->p_sys->i_height = p_vout->output.i_height;
815 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
816 * p_vout->output.i_height / p_vout->p_sys->i_height
817 * p_vout->p_sys->i_width / p_vout->output.i_width;
819 p_vout->p_sys->b_changed = true;
823 * Forward mouse event with proper conversion.
825 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
826 vlc_value_t oldval, vlc_value_t newval, void *p_data )
828 vout_thread_t *p_vout = p_data;
829 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
831 /* Translate the mouse coordinates
832 * FIXME missing lock */
833 if( !strcmp( psz_var, "mouse-x" ) )
834 newval.i_int += p_vout->p_sys->i_x;
835 else if( !strcmp( psz_var, "mouse-y" ) )
836 newval.i_int += p_vout->p_sys->i_y;
838 return var_Set( p_vout, psz_var, newval );
842 /*****************************************************************************
843 * FilterCallback: called when changing the ratio on the fly.
844 *****************************************************************************/
845 static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
846 vlc_value_t oldval, vlc_value_t newval,
849 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
850 vout_thread_t * p_vout = (vout_thread_t *)p_this;
852 if( !strcmp( psz_var, "ratio-crop" ) )
854 vlc_mutex_lock( &p_vout->p_sys->lock );
855 if ( !strcmp( newval.psz_string, "Auto" ) )
856 p_vout->p_sys->i_ratio = 0;
859 p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
860 p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
861 p_vout->p_sys->b_autocrop = true;
863 if (p_vout->p_sys->i_ratio)
865 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) /
866 p_vout->output.i_height)
867 p_vout->p_sys->i_ratio = (p_vout->output.i_width * 1000) /
868 p_vout->output.i_height;
869 if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
870 p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;
872 vlc_mutex_unlock( &p_vout->p_sys->lock );