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.")
80 #define RATIOMAX_TEXT N_("Ratio max (x 1000)")
81 #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.")
83 #define RATIO_TEXT N_("Manual ratio")
84 #define RATIO_LONGTEXT N_("Force a ratio (0 for automatic). Value is x1000: 1333 means 4/3.")
86 #define TIME_TEXT N_("Number of images for change")
87 #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.")
89 #define DIFF_TEXT N_("Number of lines for change")
90 #define DIFF_LONGTEXT N_("The minimum difference in the number of detected black lines to consider that ratio changed and trigger recrop.")
92 #define NBP_TEXT N_("Number of non black pixels ")
93 #define NBP_LONGTEXT N_("The maximum of non-black pixels in a line to consider"\
94 " that the line is black.")
96 #define SKIP_TEXT N_("Skip percentage (%)")
97 #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.")
99 #define LUM_TEXT N_("Luminance threshold ")
100 #define LUM_LONGTEXT N_("Maximum luminance to consider a pixel as black (0-255).")
104 set_description( N_("Crop video filter") )
105 set_shortname( N_("Crop" ))
106 set_category( CAT_VIDEO )
107 set_subcategory( SUBCAT_VIDEO_VFILTER )
108 set_capability( "video filter", 0 )
110 add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT,
111 GEOMETRY_LONGTEXT, false )
112 add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT,
113 AUTOCROP_LONGTEXT, false )
116 add_integer_with_range( "autocrop-ratio-max", 2405, 0, RATIO_MAX, NULL,
117 RATIOMAX_TEXT, RATIOMAX_LONGTEXT, true )
119 add_integer_with_range( "crop-ratio", 0, 0, RATIO_MAX, NULL, RATIO_TEXT,
120 RATIO_LONGTEXT, false )
121 add_integer( "autocrop-time", 25, NULL, TIME_TEXT,
122 TIME_LONGTEXT, true )
123 add_integer( "autocrop-diff", 16, NULL, DIFF_TEXT,
124 DIFF_LONGTEXT, true )
126 add_integer( "autocrop-non-black-pixels", 3, NULL,
127 NBP_TEXT, NBP_LONGTEXT, true )
129 add_integer_with_range( "autocrop-skip-percent", 17, 0, 100, NULL,
130 SKIP_TEXT, SKIP_LONGTEXT, true )
132 add_integer_with_range( "autocrop-luminance-threshold", 40, 0, 128, NULL,
133 LUM_TEXT, LUM_LONGTEXT, true )
134 #endif //BEST_AUTOCROP
136 add_shortcut( "crop" )
137 set_callbacks( Create, Destroy )
140 /*****************************************************************************
141 * vout_sys_t: Crop video output method descriptor
142 *****************************************************************************
143 * This structure is part of the video output thread descriptor.
144 * It describes the Crop specific properties of an output thread.
145 *****************************************************************************/
148 vout_thread_t *p_vout;
150 unsigned int i_x, i_y;
151 unsigned int i_width, i_height, i_aspect;
155 /* Autocrop specific variables */
156 unsigned int i_lastchange;
159 unsigned int i_ratio_max;
160 unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
161 unsigned int i_ratio;
166 /*****************************************************************************
167 * Control: control facility for the vout (forwards to child vout)
168 *****************************************************************************/
169 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
171 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
174 /*****************************************************************************
175 * Create: allocates Crop video thread output method
176 *****************************************************************************
177 * This function allocates and initializes a Crop vout method.
178 *****************************************************************************/
179 static int Create( vlc_object_t *p_this )
181 vout_thread_t *p_vout = (vout_thread_t *)p_this;
183 /* Allocate structure */
184 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
185 if( p_vout->p_sys == NULL )
188 p_vout->pf_init = Init;
189 p_vout->pf_end = End;
190 p_vout->pf_manage = Manage;
191 p_vout->pf_render = Render;
192 p_vout->pf_display = NULL;
193 p_vout->pf_control = Control;
198 /*****************************************************************************
199 * Init: initialize Crop video thread output method
200 *****************************************************************************/
201 static int Init( vout_thread_t *p_vout )
206 I_OUTPUTPICTURES = 0;
207 memset( &fmt, 0, sizeof(video_format_t) );
209 p_vout->p_sys->i_lastchange = 0;
210 p_vout->p_sys->b_changed = false;
212 /* Initialize the output structure */
213 p_vout->output.i_chroma = p_vout->render.i_chroma;
214 p_vout->output.i_width = p_vout->render.i_width;
215 p_vout->output.i_height = p_vout->render.i_height;
216 p_vout->output.i_aspect = p_vout->render.i_aspect;
217 p_vout->fmt_out = p_vout->fmt_in;
219 /* Shall we use autocrop ? */
220 p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
222 p_vout->p_sys->i_ratio_max = config_GetInt( p_vout, "autocrop-ratio-max" );
223 p_vout->p_sys->i_threshold =
224 config_GetInt( p_vout, "autocrop-luminance-threshold" );
225 p_vout->p_sys->i_skipPercent =
226 config_GetInt( p_vout, "autocrop-skip-percent" );
227 p_vout->p_sys->i_nonBlackPixel =
228 config_GetInt( p_vout, "autocrop-non-black-pixels" );
229 p_vout->p_sys->i_diff = config_GetInt( p_vout, "autocrop-diff" );
230 p_vout->p_sys->i_time = config_GetInt( p_vout, "autocrop-time" );
231 var_SetString( p_vout, "ratio-crop", "0" );
233 if (p_vout->p_sys->b_autocrop)
234 p_vout->p_sys->i_ratio = 0;
237 p_vout->p_sys->i_ratio = config_GetInt( p_vout, "crop-ratio" );
238 // ratio < width / height => ratio = 0 (unchange ratio)
239 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) / p_vout->output.i_height)
240 p_vout->p_sys->i_ratio = 0;
245 /* Get geometry value from the user */
246 psz_var = config_GetPsz( p_vout, "crop-geometry" );
249 char *psz_parser, *psz_tmp;
251 psz_parser = psz_tmp = psz_var;
252 while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
257 p_vout->p_sys->i_width = atoi( psz_parser );
259 psz_parser = ++psz_tmp;
260 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
265 p_vout->p_sys->i_height = atoi( psz_parser );
267 psz_parser = ++psz_tmp;
268 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
273 p_vout->p_sys->i_x = atoi( psz_parser );
274 p_vout->p_sys->i_y = atoi( ++psz_tmp );
278 p_vout->p_sys->i_x = atoi( psz_parser );
280 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
285 p_vout->p_sys->i_height = atoi( psz_parser );
287 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
289 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
294 p_vout->p_sys->i_width = atoi( psz_parser );
295 p_vout->p_sys->i_height = p_vout->output.i_height;
297 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
299 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
302 /* Check for validity */
303 if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
304 > p_vout->output.i_width )
306 p_vout->p_sys->i_x = 0;
307 if( p_vout->p_sys->i_width > p_vout->output.i_width )
309 p_vout->p_sys->i_width = p_vout->output.i_width;
313 if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
314 > p_vout->output.i_height )
316 p_vout->p_sys->i_y = 0;
317 if( p_vout->p_sys->i_height > p_vout->output.i_height )
319 p_vout->p_sys->i_height = p_vout->output.i_height;
327 if (p_vout->p_sys->i_ratio)
329 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
330 p_vout->p_sys->i_width = p_vout->fmt_out.i_visible_width;
331 p_vout->p_sys->i_height = p_vout->output.i_aspect
332 * p_vout->output.i_height / p_vout->p_sys->i_aspect
333 * p_vout->p_sys->i_width / p_vout->output.i_width;
334 p_vout->p_sys->i_height += p_vout->p_sys->i_height % 2;
335 p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
336 p_vout->p_sys->i_y = (p_vout->output.i_height - p_vout->p_sys->i_height) / 2;
341 p_vout->p_sys->i_width = p_vout->fmt_out.i_visible_width;
342 p_vout->p_sys->i_height = p_vout->fmt_out.i_visible_height;
343 p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
344 p_vout->p_sys->i_y = p_vout->fmt_out.i_y_offset;
347 /* Pheeew. Parsing done. */
348 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
349 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
350 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
351 p_vout->p_sys->b_autocrop ? "" : "not " );
352 /* Set current output image properties */
353 p_vout->p_sys->i_aspect = p_vout->fmt_out.i_aspect
354 * p_vout->fmt_out.i_visible_height / p_vout->p_sys->i_height
355 * p_vout->p_sys->i_width / p_vout->fmt_out.i_visible_width;
358 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
360 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
361 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
362 fmt.i_x_offset = fmt.i_y_offset = 0;
363 fmt.i_chroma = p_vout->render.i_chroma;
364 fmt.i_aspect = p_vout->p_sys->i_aspect;
365 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
366 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
368 /* Try to open the real video output */
369 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
370 if( p_vout->p_sys->p_vout == NULL )
372 msg_Err( p_vout, "failed to create vout" );
373 dialog_Fatal( p_vout, _("Cropping failed"),
374 _("VLC could not open the video output module.") );
379 var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
382 vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
384 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
389 /*****************************************************************************
390 * End: terminate Crop video thread output method
391 *****************************************************************************/
392 static void End( vout_thread_t *p_vout )
394 vout_sys_t *p_sys = p_vout->p_sys;
398 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
399 vout_CloseAndRelease( p_sys->p_vout );
402 vout_filter_ReleaseDirectBuffers( p_vout );
405 /*****************************************************************************
406 * Destroy: destroy Crop video thread output method
407 *****************************************************************************
408 * Terminate an output method created by CropCreateOutputMethod
409 *****************************************************************************/
410 static void Destroy( vlc_object_t *p_this )
412 vout_thread_t *p_vout = (vout_thread_t *)p_this;
414 free( p_vout->p_sys );
417 /*****************************************************************************
418 * Manage: handle Crop events
419 *****************************************************************************
420 * This function should be called regularly by video output thread. It manages
421 * console events. It returns a non null value on error.
422 *****************************************************************************/
423 static int Manage( vout_thread_t *p_vout )
427 if( !p_vout->p_sys->b_changed )
432 memset( &fmt, 0, sizeof(video_format_t) );
435 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
436 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
437 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
438 p_vout->p_sys->b_autocrop ? "" : "not " );
440 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
443 if( p_vout->p_sys->p_vout )
445 vout_filter_DelChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
446 vout_CloseAndRelease( p_vout->p_sys->p_vout );
449 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
450 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
451 fmt.i_x_offset = fmt.i_y_offset = 0;
452 fmt.i_chroma = p_vout->render.i_chroma;
453 fmt.i_aspect = p_vout->p_sys->i_aspect;
454 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
455 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
457 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
458 if( p_vout->p_sys->p_vout == NULL )
460 msg_Err( p_vout, "failed to create vout" );
461 dialog_Fatal( p_vout, _("Cropping failed"),
462 _("VLC could not open the video output module.") );
465 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
467 p_vout->p_sys->b_changed = false;
468 p_vout->p_sys->i_lastchange = 0;
473 /*****************************************************************************
474 * Render: display previously rendered output
475 *****************************************************************************
476 * This function sends the currently rendered image to Crop image, waits
477 * until it is displayed and switches the two rendering buffers, preparing next
479 *****************************************************************************/
480 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
482 picture_t *p_outpic = NULL;
485 if( p_vout->p_sys->b_changed )
491 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
494 if( !vlc_object_alive (p_vout) || p_vout->b_error )
496 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
500 msleep( VOUT_OUTMEM_SLEEP );
503 p_outpic->date = p_pic->date;
504 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
506 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
508 uint8_t *p_in, *p_out, *p_out_end;
509 int i_in_pitch = p_pic->p[i_plane].i_pitch;
510 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
511 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
513 p_in = p_pic->p[i_plane].p_pixels
514 /* Skip the right amount of lines */
515 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
516 p_vout->p_sys->i_y / p_vout->output.i_height )
517 /* Skip the right amount of columns */
518 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
520 p_out = p_outpic->p[i_plane].p_pixels;
521 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
523 while( p_out < p_out_end )
525 vlc_memcpy( p_out, p_in, i_copy_pitch );
527 p_out += i_out_pitch;
531 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
532 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
534 /* The source image may still be in the cache ... parse it! */
535 if( p_vout->p_sys->b_autocrop )
537 UpdateStats( p_vout, p_pic );
542 static bool NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
543 int i_visible_pitch, int i_lines,
544 int i_lumThreshold, int i_skipCountPercent,
545 int i_nonBlackPixel, int i_chroma)
547 const int i_col = i_line * i_pitch / i_lines;
548 int i_index, i_count = 0;
554 case VLC_FOURCC('I','4','4','4'):
555 case VLC_FOURCC('I','4','2','2'):
556 case VLC_FOURCC('I','4','2','0'):
557 case VLC_FOURCC('Y','V','1','2'):
558 case VLC_FOURCC('I','Y','U','V'):
559 case VLC_FOURCC('I','4','1','1'):
560 case VLC_FOURCC('I','4','1','0'):
561 case VLC_FOURCC('Y','V','U','9'):
562 case VLC_FOURCC('Y','U','V','A'):
563 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
564 for (i_index = i_col/2 + i_skipCount/2;
565 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
568 i_count += (p_in[i_index] > i_lumThreshold);
569 if (i_count > i_nonBlackPixel) break;
573 case VLC_FOURCC('R','G','B','2'): // packed by 1
574 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
575 for (i_index = i_col/2 + i_skipCount/2;
576 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
579 i_count += (p_in[i_index] > i_lumThreshold);
580 if (i_count > i_nonBlackPixel) break;
583 case VLC_FOURCC('R','V','1','5'): // packed by 2
584 case VLC_FOURCC('R','V','1','6'): // packed by 2
585 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
586 for (i_index = i_col/2 + i_skipCount/2 -
587 (i_col/2 + i_skipCount/2) % 2;
588 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
591 i_count += (p_in[i_index] > i_lumThreshold) &&
592 (p_in[i_index + 1] > i_lumThreshold);
593 if (i_count > i_nonBlackPixel) break;
596 case VLC_FOURCC('R','V','2','4'): // packed by 3
597 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
598 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)
600 i_count += (p_in[i_index] > i_lumThreshold) &&
601 (p_in[i_index + 1] > i_lumThreshold) &&
602 (p_in[i_index + 2] > i_lumThreshold);
603 if (i_count > i_nonBlackPixel) break;
606 case VLC_FOURCC('R','V','3','2'): // packed by 4
607 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
608 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)
610 i_count += (uint32_t)(*(p_in + i_index)) > (uint32_t)i_lumThreshold;
611 if (i_count > i_nonBlackPixel) break;
615 case VLC_FOURCC('Y','U','Y','2'): // packed by 2
616 case VLC_FOURCC('Y','U','N','V'): // packed by 2
617 case VLC_FOURCC('U','Y','V','Y'): // packed by 2
618 case VLC_FOURCC('U','Y','N','V'): // packed by 2
619 case VLC_FOURCC('Y','4','2','2'): // packed by 2
620 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
621 for (i_index = (i_col/2 + i_skipCount/2) -
622 (i_col/2 + i_skipCount/2) % 2;
623 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
626 i_count += (p_in[i_index] > i_lumThreshold);
627 if (i_count > i_nonBlackPixel) break;
633 return (i_count > i_nonBlackPixel);
637 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
639 uint8_t *p_in = p_pic->p[0].p_pixels;
640 int i_pitch = p_pic->p[0].i_pitch;
641 int i_visible_pitch = p_pic->p[0].i_visible_pitch;
642 int i_lines = p_pic->p[0].i_visible_lines;
643 int i_firstwhite = -1, i_lastwhite = -1, i;
645 int i_time = p_vout->p_sys->i_time;
646 int i_diff = p_vout->p_sys->i_diff;
648 if (!p_vout->p_sys->i_ratio)
650 /* Determine where black borders are */
651 for( i = 0 ; i < i_lines ; i++)
653 if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
654 p_vout->p_sys->i_threshold,
655 p_vout->p_sys->i_skipPercent,
656 p_vout->p_sys->i_nonBlackPixel,
657 p_vout->output.i_chroma))
660 i_lastwhite = i_lines - i;
666 /* Decide whether it's worth changing the size */
667 if( i_lastwhite == -1 )
669 p_vout->p_sys->i_lastchange = 0;
673 if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
675 p_vout->p_sys->i_lastchange = 0;
679 if (p_vout->output.i_aspect
680 * p_vout->output.i_height /
681 (i_lastwhite - i_firstwhite + 1)
682 * p_vout->p_sys->i_width /
683 p_vout->output.i_width >
684 p_vout->p_sys->i_ratio_max * 432)
686 int i_height = ((p_vout->output.i_aspect / 432) *
687 p_vout->output.i_height * p_vout->p_sys->i_width) /
688 (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
689 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
690 i_lastwhite = p_vout->output.i_height - i_firstwhite;
692 p_vout->p_sys->i_lastchange = 0;
697 if( (i_lastwhite - i_firstwhite) <
698 (int) (p_vout->p_sys->i_height + i_diff)
699 && (i_lastwhite - i_firstwhite + i_diff) >
700 (int) p_vout->p_sys->i_height )
702 p_vout->p_sys->i_lastchange = 0;
706 /* We need at least 'i_time' images to make up our mind */
707 p_vout->p_sys->i_lastchange++;
708 if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
715 if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
717 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
718 int i_height = p_vout->output.i_aspect
719 * p_vout->output.i_height /
720 p_vout->p_sys->i_aspect
721 * p_vout->p_sys->i_width /
722 p_vout->output.i_width;
723 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
724 i_lastwhite = p_vout->output.i_height - i_firstwhite;
733 /* Determine where black borders are */
734 switch( p_vout->output.i_chroma )
736 case VLC_FOURCC('I','4','2','0'):
737 /* XXX: Do not laugh ! I know this is very naive. But it's just a
738 * proof of concept code snippet... */
739 for( i = i_lines ; i-- ; )
741 const int i_col = i * i_pitch / i_lines;
743 if( p_in[i_col/2] > 40
744 && p_in[i_visible_pitch/2] > 40
745 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
747 if( i_lastwhite == -1 )
761 /* Decide whether it's worth changing the size */
762 if( i_lastwhite == -1 )
764 p_vout->p_sys->i_lastchange = 0;
768 if( (unsigned int)(i_lastwhite - i_firstwhite)
769 < p_vout->p_sys->i_height / 2 )
771 p_vout->p_sys->i_lastchange = 0;
775 if( (unsigned int)(i_lastwhite - i_firstwhite)
776 < p_vout->p_sys->i_height + 16
777 && (unsigned int)(i_lastwhite - i_firstwhite + 16)
778 > p_vout->p_sys->i_height )
780 p_vout->p_sys->i_lastchange = 0;
784 /* We need at least 25 images to make up our mind */
785 p_vout->p_sys->i_lastchange++;
786 if( p_vout->p_sys->i_lastchange < 25 )
790 #endif //BEST_AUTOCROP
792 /* Tune a few values */
793 if( i_firstwhite & 1 )
798 if( !(i_lastwhite & 1) )
804 p_vout->p_sys->i_y = i_firstwhite;
805 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
807 // check p_vout->p_sys->i_height <= p_vout->output.i_height
808 if (p_vout->p_sys->i_height > p_vout->output.i_height)
809 p_vout->p_sys->i_height = p_vout->output.i_height;
812 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
813 * p_vout->output.i_height / p_vout->p_sys->i_height
814 * p_vout->p_sys->i_width / p_vout->output.i_width;
816 p_vout->p_sys->b_changed = true;
820 * Forward mouse event with proper conversion.
822 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
823 vlc_value_t oldval, vlc_value_t newval, void *p_data )
825 vout_thread_t *p_vout = p_data;
826 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
828 /* Translate the mouse coordinates
829 * FIXME missing lock */
830 if( !strcmp( psz_var, "mouse-x" ) )
831 newval.i_int += p_vout->p_sys->i_x;
832 else if( !strcmp( psz_var, "mouse-y" ) )
833 newval.i_int += p_vout->p_sys->i_y;
835 return var_Set( p_vout, psz_var, newval );
839 /*****************************************************************************
840 * FilterCallback: called when changing the ratio on the fly.
841 *****************************************************************************/
842 static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
843 vlc_value_t oldval, vlc_value_t newval,
846 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
847 vout_thread_t * p_vout = (vout_thread_t *)p_this;
849 if( !strcmp( psz_var, "ratio-crop" ) )
851 if ( !strcmp( newval.psz_string, "Auto" ) )
852 p_vout->p_sys->i_ratio = 0;
855 p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
856 p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
857 p_vout->p_sys->b_autocrop = true;
859 if (p_vout->p_sys->i_ratio)
861 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) /
862 p_vout->output.i_height)
863 p_vout->p_sys->i_ratio = (p_vout->output.i_width * 1000) /
864 p_vout->output.i_height;
865 if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
866 p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;