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 *****************************************************************************/
149 vout_thread_t *p_vout;
151 unsigned int i_x, i_y;
152 unsigned int i_width, i_height, i_aspect;
156 /* Autocrop specific variables */
157 unsigned int i_lastchange;
160 unsigned int i_ratio_max;
161 unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
162 unsigned int i_ratio;
167 /*****************************************************************************
168 * Control: control facility for the vout (forwards to child vout)
169 *****************************************************************************/
170 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
172 return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
175 /*****************************************************************************
176 * Create: allocates Crop video thread output method
177 *****************************************************************************
178 * This function allocates and initializes a Crop vout method.
179 *****************************************************************************/
180 static int Create( vlc_object_t *p_this )
182 vout_thread_t *p_vout = (vout_thread_t *)p_this;
184 /* Allocate structure */
185 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
186 if( p_vout->p_sys == NULL )
189 p_vout->pf_init = Init;
190 p_vout->pf_end = End;
191 p_vout->pf_manage = Manage;
192 p_vout->pf_render = Render;
193 p_vout->pf_display = NULL;
194 p_vout->pf_control = Control;
199 /*****************************************************************************
200 * Init: initialize Crop video thread output method
201 *****************************************************************************/
202 static int Init( vout_thread_t *p_vout )
207 I_OUTPUTPICTURES = 0;
208 memset( &fmt, 0, sizeof(video_format_t) );
210 p_vout->p_sys->i_lastchange = 0;
211 p_vout->p_sys->b_changed = false;
213 /* Initialize the output structure */
214 p_vout->output.i_chroma = p_vout->render.i_chroma;
215 p_vout->output.i_width = p_vout->render.i_width;
216 p_vout->output.i_height = p_vout->render.i_height;
217 p_vout->output.i_aspect = p_vout->render.i_aspect;
218 p_vout->fmt_out = p_vout->fmt_in;
220 /* Shall we use autocrop ? */
221 p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
223 p_vout->p_sys->i_ratio_max = config_GetInt( p_vout, "autocrop-ratio-max" );
224 p_vout->p_sys->i_threshold =
225 config_GetInt( p_vout, "autocrop-luminance-threshold" );
226 p_vout->p_sys->i_skipPercent =
227 config_GetInt( p_vout, "autocrop-skip-percent" );
228 p_vout->p_sys->i_nonBlackPixel =
229 config_GetInt( p_vout, "autocrop-non-black-pixels" );
230 p_vout->p_sys->i_diff = config_GetInt( p_vout, "autocrop-diff" );
231 p_vout->p_sys->i_time = config_GetInt( p_vout, "autocrop-time" );
232 var_SetString( p_vout, "ratio-crop", "0" );
234 if (p_vout->p_sys->b_autocrop)
235 p_vout->p_sys->i_ratio = 0;
238 p_vout->p_sys->i_ratio = config_GetInt( p_vout, "crop-ratio" );
239 // ratio < width / height => ratio = 0 (unchange ratio)
240 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) / p_vout->output.i_height)
241 p_vout->p_sys->i_ratio = 0;
246 /* Get geometry value from the user */
247 psz_var = config_GetPsz( p_vout, "crop-geometry" );
250 char *psz_parser, *psz_tmp;
252 psz_parser = psz_tmp = psz_var;
253 while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
258 p_vout->p_sys->i_width = atoi( psz_parser );
260 psz_parser = ++psz_tmp;
261 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
266 p_vout->p_sys->i_height = atoi( psz_parser );
268 psz_parser = ++psz_tmp;
269 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
274 p_vout->p_sys->i_x = atoi( psz_parser );
275 p_vout->p_sys->i_y = atoi( ++psz_tmp );
279 p_vout->p_sys->i_x = atoi( psz_parser );
281 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
286 p_vout->p_sys->i_height = atoi( psz_parser );
288 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
290 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
295 p_vout->p_sys->i_width = atoi( psz_parser );
296 p_vout->p_sys->i_height = p_vout->output.i_height;
298 ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
300 ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
303 /* Check for validity */
304 if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
305 > p_vout->output.i_width )
307 p_vout->p_sys->i_x = 0;
308 if( p_vout->p_sys->i_width > p_vout->output.i_width )
310 p_vout->p_sys->i_width = p_vout->output.i_width;
314 if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
315 > p_vout->output.i_height )
317 p_vout->p_sys->i_y = 0;
318 if( p_vout->p_sys->i_height > p_vout->output.i_height )
320 p_vout->p_sys->i_height = p_vout->output.i_height;
328 if (p_vout->p_sys->i_ratio)
330 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
331 p_vout->p_sys->i_width = p_vout->fmt_out.i_visible_width;
332 p_vout->p_sys->i_height = p_vout->output.i_aspect
333 * p_vout->output.i_height / p_vout->p_sys->i_aspect
334 * p_vout->p_sys->i_width / p_vout->output.i_width;
335 p_vout->p_sys->i_height += p_vout->p_sys->i_height % 2;
336 p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
337 p_vout->p_sys->i_y = (p_vout->output.i_height - p_vout->p_sys->i_height) / 2;
342 p_vout->p_sys->i_width = p_vout->fmt_out.i_visible_width;
343 p_vout->p_sys->i_height = p_vout->fmt_out.i_visible_height;
344 p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
345 p_vout->p_sys->i_y = p_vout->fmt_out.i_y_offset;
348 /* Pheeew. Parsing done. */
349 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
350 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
351 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
352 p_vout->p_sys->b_autocrop ? "" : "not " );
353 /* Set current output image properties */
354 p_vout->p_sys->i_aspect = p_vout->fmt_out.i_aspect
355 * p_vout->fmt_out.i_visible_height / p_vout->p_sys->i_height
356 * p_vout->p_sys->i_width / p_vout->fmt_out.i_visible_width;
359 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
361 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
362 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
363 fmt.i_x_offset = fmt.i_y_offset = 0;
364 fmt.i_chroma = p_vout->render.i_chroma;
365 fmt.i_aspect = p_vout->p_sys->i_aspect;
366 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
367 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
369 /* Try to open the real video output */
370 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
371 if( p_vout->p_sys->p_vout == NULL )
373 msg_Err( p_vout, "failed to create vout" );
374 dialog_Fatal( p_vout, _("Cropping failed"),
375 _("VLC could not open the video output module.") );
380 vlc_mutex_init( &p_vout->p_sys->lock );
381 var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
384 vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
386 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
391 /*****************************************************************************
392 * End: terminate Crop video thread output method
393 *****************************************************************************/
394 static void End( vout_thread_t *p_vout )
396 vout_sys_t *p_sys = p_vout->p_sys;
400 vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
401 vout_CloseAndRelease( p_sys->p_vout );
404 vout_filter_ReleaseDirectBuffers( p_vout );
405 var_DelCallback( p_vout, "ratio-crop", FilterCallback, NULL );
406 vlc_mutex_destroy( &p_sys->lock );
409 /*****************************************************************************
410 * Destroy: destroy Crop video thread output method
411 *****************************************************************************
412 * Terminate an output method created by CropCreateOutputMethod
413 *****************************************************************************/
414 static void Destroy( vlc_object_t *p_this )
416 vout_thread_t *p_vout = (vout_thread_t *)p_this;
418 free( p_vout->p_sys );
421 /*****************************************************************************
422 * Manage: handle Crop events
423 *****************************************************************************
424 * This function should be called regularly by video output thread. It manages
425 * console events. It returns a non null value on error.
426 *****************************************************************************/
427 static int Manage( vout_thread_t *p_vout )
431 if( !p_vout->p_sys->b_changed )
436 memset( &fmt, 0, sizeof(video_format_t) );
439 /* XXX: not thread-safe with FilterCallback */
440 msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
441 p_vout->p_sys->i_width, p_vout->p_sys->i_height,
442 p_vout->p_sys->i_x, p_vout->p_sys->i_y,
443 p_vout->p_sys->b_autocrop ? "" : "not " );
445 msg_Info( p_vout, "ratio %d", p_vout->p_sys->i_aspect / 432);
448 if( p_vout->p_sys->p_vout )
450 vout_filter_DelChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
451 vout_CloseAndRelease( p_vout->p_sys->p_vout );
454 fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
455 fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
456 fmt.i_x_offset = fmt.i_y_offset = 0;
457 fmt.i_chroma = p_vout->render.i_chroma;
458 fmt.i_aspect = p_vout->p_sys->i_aspect;
459 fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
460 fmt.i_sar_den = VOUT_ASPECT_FACTOR;
462 p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
463 if( p_vout->p_sys->p_vout == NULL )
465 msg_Err( p_vout, "failed to create vout" );
466 dialog_Fatal( p_vout, _("Cropping failed"),
467 _("VLC could not open the video output module.") );
470 vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
472 p_vout->p_sys->b_changed = false;
473 vlc_mutex_lock( &p_vout->p_sys->lock );
474 p_vout->p_sys->i_lastchange = 0;
475 vlc_mutex_unlock( &p_vout->p_sys->lock );
480 /*****************************************************************************
481 * Render: display previously rendered output
482 *****************************************************************************
483 * This function sends the currently rendered image to Crop image, waits
484 * until it is displayed and switches the two rendering buffers, preparing next
486 *****************************************************************************/
487 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
489 picture_t *p_outpic = NULL;
492 if( p_vout->p_sys->b_changed )
498 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
501 if( !vlc_object_alive (p_vout) || p_vout->b_error )
503 vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
507 msleep( VOUT_OUTMEM_SLEEP );
510 p_outpic->date = p_pic->date;
511 vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
513 for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
515 uint8_t *p_in, *p_out, *p_out_end;
516 int i_in_pitch = p_pic->p[i_plane].i_pitch;
517 const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
518 const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
520 p_in = p_pic->p[i_plane].p_pixels
521 /* Skip the right amount of lines */
522 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
523 p_vout->p_sys->i_y / p_vout->output.i_height )
524 /* Skip the right amount of columns */
525 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
527 p_out = p_outpic->p[i_plane].p_pixels;
528 p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
530 while( p_out < p_out_end )
532 vlc_memcpy( p_out, p_in, i_copy_pitch );
534 p_out += i_out_pitch;
538 vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
539 vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
541 /* The source image may still be in the cache ... parse it! */
542 vlc_mutex_lock( &p_vout->p_sys->lock );
543 if( p_vout->p_sys->b_autocrop )
544 UpdateStats( p_vout, p_pic );
545 vlc_mutex_unlock( &p_vout->p_sys->lock );
549 static bool NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
550 int i_visible_pitch, int i_lines,
551 int i_lumThreshold, int i_skipCountPercent,
552 int i_nonBlackPixel, int i_chroma)
554 const int i_col = i_line * i_pitch / i_lines;
555 int i_index, i_count = 0;
561 case VLC_FOURCC('I','4','4','4'):
562 case VLC_FOURCC('I','4','2','2'):
563 case VLC_FOURCC('I','4','2','0'):
564 case VLC_FOURCC('Y','V','1','2'):
565 case VLC_FOURCC('I','Y','U','V'):
566 case VLC_FOURCC('I','4','1','1'):
567 case VLC_FOURCC('I','4','1','0'):
568 case VLC_FOURCC('Y','V','U','9'):
569 case VLC_FOURCC('Y','U','V','A'):
570 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
571 for (i_index = i_col/2 + i_skipCount/2;
572 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
575 i_count += (p_in[i_index] > i_lumThreshold);
576 if (i_count > i_nonBlackPixel) break;
580 case VLC_FOURCC('R','G','B','2'): // packed by 1
581 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
582 for (i_index = i_col/2 + i_skipCount/2;
583 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
586 i_count += (p_in[i_index] > i_lumThreshold);
587 if (i_count > i_nonBlackPixel) break;
590 case VLC_FOURCC('R','V','1','5'): // packed by 2
591 case VLC_FOURCC('R','V','1','6'): // packed by 2
592 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
593 for (i_index = i_col/2 + i_skipCount/2 -
594 (i_col/2 + i_skipCount/2) % 2;
595 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
598 i_count += (p_in[i_index] > i_lumThreshold) &&
599 (p_in[i_index + 1] > i_lumThreshold);
600 if (i_count > i_nonBlackPixel) break;
603 case VLC_FOURCC('R','V','2','4'): // packed by 3
604 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
605 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)
607 i_count += (p_in[i_index] > i_lumThreshold) &&
608 (p_in[i_index + 1] > i_lumThreshold) &&
609 (p_in[i_index + 2] > i_lumThreshold);
610 if (i_count > i_nonBlackPixel) break;
613 case VLC_FOURCC('R','V','3','2'): // packed by 4
614 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
615 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)
617 i_count += (uint32_t)(*(p_in + i_index)) > (uint32_t)i_lumThreshold;
618 if (i_count > i_nonBlackPixel) break;
622 case VLC_FOURCC('Y','U','Y','2'): // packed by 2
623 case VLC_FOURCC('Y','U','N','V'): // packed by 2
624 case VLC_FOURCC('U','Y','V','Y'): // packed by 2
625 case VLC_FOURCC('U','Y','N','V'): // packed by 2
626 case VLC_FOURCC('Y','4','2','2'): // packed by 2
627 i_skipCount = (i_pitch * i_skipCountPercent) / 100;
628 for (i_index = (i_col/2 + i_skipCount/2) -
629 (i_col/2 + i_skipCount/2) % 2;
630 i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
633 i_count += (p_in[i_index] > i_lumThreshold);
634 if (i_count > i_nonBlackPixel) break;
640 return (i_count > i_nonBlackPixel);
644 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
646 uint8_t *p_in = p_pic->p[0].p_pixels;
647 int i_pitch = p_pic->p[0].i_pitch;
648 int i_visible_pitch = p_pic->p[0].i_visible_pitch;
649 int i_lines = p_pic->p[0].i_visible_lines;
650 int i_firstwhite = -1, i_lastwhite = -1, i;
652 int i_time = p_vout->p_sys->i_time;
653 int i_diff = p_vout->p_sys->i_diff;
655 if (!p_vout->p_sys->i_ratio)
657 /* Determine where black borders are */
658 for( i = 0 ; i < i_lines ; i++)
660 if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
661 p_vout->p_sys->i_threshold,
662 p_vout->p_sys->i_skipPercent,
663 p_vout->p_sys->i_nonBlackPixel,
664 p_vout->output.i_chroma))
667 i_lastwhite = i_lines - i;
673 /* Decide whether it's worth changing the size */
674 if( i_lastwhite == -1 )
676 p_vout->p_sys->i_lastchange = 0;
680 if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
682 p_vout->p_sys->i_lastchange = 0;
686 if (p_vout->output.i_aspect
687 * p_vout->output.i_height /
688 (i_lastwhite - i_firstwhite + 1)
689 * p_vout->p_sys->i_width /
690 p_vout->output.i_width >
691 p_vout->p_sys->i_ratio_max * 432)
693 int i_height = ((p_vout->output.i_aspect / 432) *
694 p_vout->output.i_height * p_vout->p_sys->i_width) /
695 (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
696 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
697 i_lastwhite = p_vout->output.i_height - i_firstwhite;
699 p_vout->p_sys->i_lastchange = 0;
704 if( (i_lastwhite - i_firstwhite) <
705 (int) (p_vout->p_sys->i_height + i_diff)
706 && (i_lastwhite - i_firstwhite + i_diff) >
707 (int) p_vout->p_sys->i_height )
709 p_vout->p_sys->i_lastchange = 0;
713 /* We need at least 'i_time' images to make up our mind */
714 p_vout->p_sys->i_lastchange++;
715 if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
722 if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
724 p_vout->p_sys->i_aspect = p_vout->p_sys->i_ratio * 432;
725 int i_height = p_vout->output.i_aspect
726 * p_vout->output.i_height /
727 p_vout->p_sys->i_aspect
728 * p_vout->p_sys->i_width /
729 p_vout->output.i_width;
730 i_firstwhite = (p_vout->output.i_height - i_height) / 2;
731 i_lastwhite = p_vout->output.i_height - i_firstwhite;
740 /* Determine where black borders are */
741 switch( p_vout->output.i_chroma )
743 case VLC_FOURCC('I','4','2','0'):
744 /* XXX: Do not laugh ! I know this is very naive. But it's just a
745 * proof of concept code snippet... */
746 for( i = i_lines ; i-- ; )
748 const int i_col = i * i_pitch / i_lines;
750 if( p_in[i_col/2] > 40
751 && p_in[i_visible_pitch/2] > 40
752 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
754 if( i_lastwhite == -1 )
768 /* Decide whether it's worth changing the size */
769 if( i_lastwhite == -1 )
771 p_vout->p_sys->i_lastchange = 0;
775 if( (unsigned int)(i_lastwhite - i_firstwhite)
776 < p_vout->p_sys->i_height / 2 )
778 p_vout->p_sys->i_lastchange = 0;
782 if( (unsigned int)(i_lastwhite - i_firstwhite)
783 < p_vout->p_sys->i_height + 16
784 && (unsigned int)(i_lastwhite - i_firstwhite + 16)
785 > p_vout->p_sys->i_height )
787 p_vout->p_sys->i_lastchange = 0;
791 /* We need at least 25 images to make up our mind */
792 p_vout->p_sys->i_lastchange++;
793 if( p_vout->p_sys->i_lastchange < 25 )
797 #endif //BEST_AUTOCROP
799 /* Tune a few values */
800 if( i_firstwhite & 1 )
805 if( !(i_lastwhite & 1) )
811 p_vout->p_sys->i_y = i_firstwhite;
812 p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
814 // check p_vout->p_sys->i_height <= p_vout->output.i_height
815 if (p_vout->p_sys->i_height > p_vout->output.i_height)
816 p_vout->p_sys->i_height = p_vout->output.i_height;
819 p_vout->p_sys->i_aspect = p_vout->output.i_aspect
820 * p_vout->output.i_height / p_vout->p_sys->i_height
821 * p_vout->p_sys->i_width / p_vout->output.i_width;
823 p_vout->p_sys->b_changed = true;
827 * Forward mouse event with proper conversion.
829 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
830 vlc_value_t oldval, vlc_value_t newval, void *p_data )
832 vout_thread_t *p_vout = p_data;
833 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
835 /* Translate the mouse coordinates
836 * FIXME missing lock */
837 if( !strcmp( psz_var, "mouse-x" ) )
838 newval.i_int += p_vout->p_sys->i_x;
839 else if( !strcmp( psz_var, "mouse-y" ) )
840 newval.i_int += p_vout->p_sys->i_y;
842 return var_Set( p_vout, psz_var, newval );
846 /*****************************************************************************
847 * FilterCallback: called when changing the ratio on the fly.
848 *****************************************************************************/
849 static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
850 vlc_value_t oldval, vlc_value_t newval,
853 VLC_UNUSED(p_data); VLC_UNUSED(oldval);
854 vout_thread_t * p_vout = (vout_thread_t *)p_this;
856 if( !strcmp( psz_var, "ratio-crop" ) )
858 vlc_mutex_lock( &p_vout->p_sys->lock );
859 if ( !strcmp( newval.psz_string, "Auto" ) )
860 p_vout->p_sys->i_ratio = 0;
863 p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
864 p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
865 p_vout->p_sys->b_autocrop = true;
867 if (p_vout->p_sys->i_ratio)
869 if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) /
870 p_vout->output.i_height)
871 p_vout->p_sys->i_ratio = (p_vout->output.i_width * 1000) /
872 p_vout->output.i_height;
873 if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
874 p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;
876 vlc_mutex_unlock( &p_vout->p_sys->lock );