]> git.sesse.net Git - vlc/blob - modules/video_filter/crop.c
Merge branch 1.0-bugfix
[vlc] / modules / video_filter / crop.c
1 /*****************************************************************************
2  * crop.c : Crop video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002, 2003 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          mod by Cedric Cocquebert <Cedric.Cocquebert@supelec.fr>
9  *          based of DScaler idea (M. Samblanet)
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout.h>
37 #include <vlc_dialog.h>
38
39 #include "filter_common.h"
40
41 #define BEST_AUTOCROP 1
42 #ifdef BEST_AUTOCROP
43     #define RATIO_MAX 15000  // 10*4/3 for a 360
44 #endif
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49 static int  Create    ( vlc_object_t * );
50 static void Destroy   ( vlc_object_t * );
51
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 * );
56
57 static void UpdateStats    ( vout_thread_t *, picture_t * );
58
59 static int  MouseEvent( vlc_object_t *, char const *,
60                         vlc_value_t, vlc_value_t, void * );
61
62 #ifdef BEST_AUTOCROP
63 /*****************************************************************************
64  * Callback prototypes
65  *****************************************************************************/
66 static int FilterCallback ( vlc_object_t *, char const *,
67                             vlc_value_t, vlc_value_t, void * );
68 #endif
69
70 /*****************************************************************************
71  * Module descriptor
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>.")
75
76 #define AUTOCROP_TEXT N_("Automatic cropping")
77 #define AUTOCROP_LONGTEXT N_("Automatically detect black borders and crop them.")
78
79 #ifdef BEST_AUTOCROP
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.")
82
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.")
85
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.")
88
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.")
91
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.")
95
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.")
98
99 #define LUM_TEXT N_("Luminance threshold ")
100 #define LUM_LONGTEXT N_("Maximum luminance to consider a pixel as black (0-255).")
101 #endif
102
103 vlc_module_begin ()
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 )
109
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 )
114
115 #ifdef BEST_AUTOCROP
116     add_integer_with_range( "autocrop-ratio-max", 2405, 0, RATIO_MAX, NULL,
117                             RATIOMAX_TEXT, RATIOMAX_LONGTEXT, true )
118
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 )
125
126     add_integer( "autocrop-non-black-pixels", 3, NULL,
127                  NBP_TEXT, NBP_LONGTEXT, true )
128
129     add_integer_with_range( "autocrop-skip-percent", 17, 0, 100, NULL,
130                             SKIP_TEXT, SKIP_LONGTEXT, true )
131
132     add_integer_with_range( "autocrop-luminance-threshold", 40, 0, 128, NULL,
133                             LUM_TEXT, LUM_LONGTEXT, true )
134 #endif //BEST_AUTOCROP
135
136     add_shortcut( "crop" )
137     set_callbacks( Create, Destroy )
138 vlc_module_end ()
139
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  *****************************************************************************/
146 struct vout_sys_t
147 {
148     vlc_mutex_t lock;
149     vout_thread_t *p_vout;
150
151     unsigned int i_x, i_y;
152     unsigned int i_width, i_height, i_aspect;
153
154     bool b_autocrop;
155
156     /* Autocrop specific variables */
157     unsigned int i_lastchange;
158     bool   b_changed;
159 #ifdef BEST_AUTOCROP
160     unsigned int i_ratio_max;
161     unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
162     unsigned int i_ratio;
163 #endif
164
165 };
166
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 )
171 {
172     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
173 }
174
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 )
181 {
182     vout_thread_t *p_vout = (vout_thread_t *)p_this;
183
184     /* Allocate structure */
185     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
186     if( p_vout->p_sys == NULL )
187         return VLC_ENOMEM;
188
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;
195
196     return VLC_SUCCESS;
197 }
198
199 /*****************************************************************************
200  * Init: initialize Crop video thread output method
201  *****************************************************************************/
202 static int Init( vout_thread_t *p_vout )
203 {
204     char *psz_var;
205     video_format_t fmt;
206
207     I_OUTPUTPICTURES = 0;
208     memset( &fmt, 0, sizeof(video_format_t) );
209
210     p_vout->p_sys->i_lastchange = 0;
211     p_vout->p_sys->b_changed = false;
212
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;
219
220     /* Shall we use autocrop ? */
221     p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
222 #ifdef BEST_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" );
233
234     if (p_vout->p_sys->b_autocrop)
235         p_vout->p_sys->i_ratio = 0;
236     else
237     {
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;
242     }
243 #endif
244
245
246     /* Get geometry value from the user */
247     psz_var = config_GetPsz( p_vout, "crop-geometry" );
248     if( psz_var )
249     {
250         char *psz_parser, *psz_tmp;
251
252         psz_parser = psz_tmp = psz_var;
253         while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
254
255         if( *psz_tmp )
256         {
257             psz_tmp[0] = '\0';
258             p_vout->p_sys->i_width = atoi( psz_parser );
259
260             psz_parser = ++psz_tmp;
261             while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
262
263             if( *psz_tmp )
264             {
265                 psz_tmp[0] = '\0';
266                 p_vout->p_sys->i_height = atoi( psz_parser );
267
268                 psz_parser = ++psz_tmp;
269                 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
270
271                 if( *psz_tmp )
272                 {
273                     psz_tmp[0] = '\0';
274                     p_vout->p_sys->i_x = atoi( psz_parser );
275                     p_vout->p_sys->i_y = atoi( ++psz_tmp );
276                 }
277                 else
278                 {
279                     p_vout->p_sys->i_x = atoi( psz_parser );
280                     p_vout->p_sys->i_y =
281                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
282                 }
283             }
284             else
285             {
286                 p_vout->p_sys->i_height = atoi( psz_parser );
287                 p_vout->p_sys->i_x =
288                      ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
289                 p_vout->p_sys->i_y =
290                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
291             }
292         }
293         else
294         {
295             p_vout->p_sys->i_width = atoi( psz_parser );
296             p_vout->p_sys->i_height = p_vout->output.i_height;
297             p_vout->p_sys->i_x =
298                      ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
299             p_vout->p_sys->i_y =
300                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
301         }
302
303         /* Check for validity */
304         if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
305                                                    > p_vout->output.i_width )
306         {
307             p_vout->p_sys->i_x = 0;
308             if( p_vout->p_sys->i_width > p_vout->output.i_width )
309             {
310                 p_vout->p_sys->i_width = p_vout->output.i_width;
311             }
312         }
313
314         if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
315                                                    > p_vout->output.i_height )
316         {
317             p_vout->p_sys->i_y = 0;
318             if( p_vout->p_sys->i_height > p_vout->output.i_height )
319             {
320                 p_vout->p_sys->i_height = p_vout->output.i_height;
321             }
322         }
323
324         free( psz_var );
325     }
326     else
327 #ifdef BEST_AUTOCROP
328     if (p_vout->p_sys->i_ratio)
329     {
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;
338     }
339     else
340 #endif
341     {
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;
346     }
347
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;
357
358 #ifdef BEST_AUTOCROP
359     msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
360 #endif
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;
368
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 )
372     {
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.") );
376         return VLC_EGENERIC;
377     }
378
379 #ifdef BEST_AUTOCROP
380     vlc_mutex_init( &p_vout->p_sys->lock );
381     var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
382 #endif
383
384     vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
385
386     vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
387
388     return VLC_SUCCESS;
389 }
390
391 /*****************************************************************************
392  * End: terminate Crop video thread output method
393  *****************************************************************************/
394 static void End( vout_thread_t *p_vout )
395 {
396     vout_sys_t *p_sys = p_vout->p_sys;
397
398     if( p_sys->p_vout )
399     {
400         vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
401         vout_CloseAndRelease( p_sys->p_vout );
402     }
403
404     vout_filter_ReleaseDirectBuffers( p_vout );
405     var_DelCallback( p_vout, "ratio-crop", FilterCallback, NULL );
406     vlc_mutex_destroy( &p_sys->lock );
407 }
408
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 )
415 {
416     vout_thread_t *p_vout = (vout_thread_t *)p_this;
417
418     free( p_vout->p_sys );
419 }
420
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 )
428 {
429     video_format_t fmt;
430
431     if( !p_vout->p_sys->b_changed )
432     {
433         return VLC_SUCCESS;
434     }
435
436     memset( &fmt, 0, sizeof(video_format_t) );
437
438 #ifdef BEST_AUTOCROP
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 " );
444
445     msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
446 #endif
447
448     if( p_vout->p_sys->p_vout )
449     {
450         vout_filter_DelChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
451         vout_CloseAndRelease( p_vout->p_sys->p_vout );
452     }
453
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;
461
462     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
463     if( p_vout->p_sys->p_vout == NULL )
464     {
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.") );
468         return VLC_EGENERIC;
469     }
470     vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
471
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 );
476
477     return VLC_SUCCESS;
478 }
479
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
485  * frame.
486  *****************************************************************************/
487 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
488 {
489     picture_t *p_outpic = NULL;
490     int i_plane;
491
492     if( p_vout->p_sys->b_changed )
493     {
494         return;
495     }
496
497     while( ( p_outpic =
498                  vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
499            ) == NULL )
500     {
501         if( !vlc_object_alive (p_vout) || p_vout->b_error )
502         {
503             vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
504             return;
505         }
506
507         msleep( VOUT_OUTMEM_SLEEP );
508     }
509
510     p_outpic->date = p_pic->date;
511     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
512
513     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
514     {
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;
519
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;
526
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;
529
530         while( p_out < p_out_end )
531         {
532             vlc_memcpy( p_out, p_in, i_copy_pitch );
533             p_in += i_in_pitch;
534             p_out += i_out_pitch;
535         }
536     }
537
538     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
539     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
540
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 );
546 }
547
548 #ifdef BEST_AUTOCROP
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)
553 {
554     const int i_col = i_line * i_pitch / i_lines;
555     int i_index, i_count = 0;
556     int i_skipCount = 0;
557
558     switch(i_chroma)
559     {
560     // planar YUV
561         case VLC_CODEC_I444:
562         case VLC_CODEC_I422:
563         case VLC_CODEC_I420:
564         case VLC_CODEC_YV12:
565         case VLC_CODEC_I411:
566         case VLC_CODEC_I410:
567         case VLC_CODEC_YUVA:
568             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
569             for (i_index = i_col/2 + i_skipCount/2;
570                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
571                  i_index++)
572             {
573                  i_count += (p_in[i_index] > i_lumThreshold);
574             if (i_count > i_nonBlackPixel) break;
575             }
576             break;
577     // packed RGB
578         case VLC_CODEC_RGB8:    // packed by 1
579             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
580             for (i_index = i_col/2 + i_skipCount/2;
581                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
582                  i_index++)
583             {
584                  i_count += (p_in[i_index] > i_lumThreshold);
585             if (i_count > i_nonBlackPixel) break;
586             }
587             break;
588         case VLC_CODEC_RGB15:    // packed by 2
589         case VLC_CODEC_RGB16:    // packed by 2
590             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
591             for (i_index = i_col/2 + i_skipCount/2 -
592                                 (i_col/2 + i_skipCount/2) % 2;
593                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
594                  i_index+=2)
595             {
596                  i_count += (p_in[i_index] > i_lumThreshold) &&
597                             (p_in[i_index + 1] > i_lumThreshold);
598             if (i_count > i_nonBlackPixel) break;
599             }
600             break;
601         case VLC_CODEC_RGB24:    // packed by 3
602             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
603             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)
604             {
605                  i_count += (p_in[i_index] > i_lumThreshold) &&
606                             (p_in[i_index + 1] > i_lumThreshold) &&
607                             (p_in[i_index + 2] > i_lumThreshold);
608             if (i_count > i_nonBlackPixel) break;
609             }
610             break;
611         case VLC_CODEC_RGB32:    // packed by 4
612             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
613             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)
614             {
615                  i_count += (uint32_t)(*(p_in + i_index)) > (uint32_t)i_lumThreshold;
616             if (i_count > i_nonBlackPixel) break;
617             }
618             break;
619     // packed YUV
620         case VLC_CODEC_YUYV:    // packed by 2
621         case VLC_CODEC_UYVY:    // packed by 2
622             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
623             for (i_index = (i_col/2 + i_skipCount/2) -
624                            (i_col/2 + i_skipCount/2) % 2;
625                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
626                  i_index+=2)
627             {
628                  i_count += (p_in[i_index] > i_lumThreshold);
629             if (i_count > i_nonBlackPixel) break;
630             }
631             break;
632         default :
633             break;
634     }
635     return (i_count > i_nonBlackPixel);
636 }
637 #endif
638
639 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
640 {
641    uint8_t *p_in = p_pic->p[0].p_pixels;
642     int i_pitch = p_pic->p[0].i_pitch;
643     int i_visible_pitch = p_pic->p[0].i_visible_pitch;
644     int i_lines = p_pic->p[0].i_visible_lines;
645     int i_firstwhite = -1, i_lastwhite = -1, i;
646 #ifdef BEST_AUTOCROP
647     int i_time = p_vout->p_sys->i_time;
648     int i_diff = p_vout->p_sys->i_diff;
649
650     if (!p_vout->p_sys->i_ratio)
651     {
652         /* Determine where black borders are */
653         for( i = 0 ; i < i_lines ; i++)
654         {
655                    if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
656                             p_vout->p_sys->i_threshold,
657                             p_vout->p_sys->i_skipPercent,
658                             p_vout->p_sys->i_nonBlackPixel,
659                             p_vout->output.i_chroma))
660                 {
661                     i_firstwhite = i;
662                     i_lastwhite = i_lines - i;
663                     break;
664                 }
665                 p_in += i_pitch;
666         }
667
668         /* Decide whether it's worth changing the size */
669         if( i_lastwhite == -1 )
670         {
671             p_vout->p_sys->i_lastchange = 0;
672             return;
673         }
674
675         if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
676         {
677             p_vout->p_sys->i_lastchange = 0;
678             return;
679         }
680
681         if (p_vout->output.i_aspect
682                             * p_vout->output.i_height /
683                                 (i_lastwhite - i_firstwhite + 1)
684                             * p_vout->p_sys->i_width /
685                                p_vout->output.i_width >
686                                     p_vout->p_sys->i_ratio_max * 432)
687         {
688             int i_height = ((p_vout->output.i_aspect / 432) *
689                            p_vout->output.i_height * p_vout->p_sys->i_width) /
690                           (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
691             i_firstwhite = (p_vout->output.i_height - i_height) / 2;
692             i_lastwhite =  p_vout->output.i_height - i_firstwhite;
693 /*
694             p_vout->p_sys->i_lastchange = 0;
695             return;
696 */
697         }
698
699         if( (i_lastwhite - i_firstwhite) <
700                         (int) (p_vout->p_sys->i_height + i_diff)
701              && (i_lastwhite - i_firstwhite + i_diff) >
702                         (int) p_vout->p_sys->i_height )
703         {
704             p_vout->p_sys->i_lastchange = 0;
705             return;
706         }
707
708         /* We need at least 'i_time' images to make up our mind */
709         p_vout->p_sys->i_lastchange++;
710         if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
711         {
712             return;
713         }
714     }
715     else
716     {
717         if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
718         {
719             p_vout->p_sys->i_aspect    =  p_vout->p_sys->i_ratio * 432;
720             int i_height = p_vout->output.i_aspect
721                                     * p_vout->output.i_height /
722                                         p_vout->p_sys->i_aspect
723                                     * p_vout->p_sys->i_width /
724                                         p_vout->output.i_width;
725             i_firstwhite = (p_vout->output.i_height - i_height) / 2;
726             i_lastwhite =  p_vout->output.i_height - i_firstwhite;
727         }
728         else
729         {
730             return;
731         }
732     }
733
734 #else
735     /* Determine where black borders are */
736     switch( p_vout->output.i_chroma )
737     {
738     case VLC_CODEC_I420:
739         /* XXX: Do not laugh ! I know this is very naive. But it's just a
740          *      proof of concept code snippet... */
741         for( i = i_lines ; i-- ; )
742         {
743             const int i_col = i * i_pitch / i_lines;
744
745             if( p_in[i_col/2] > 40
746                  && p_in[i_visible_pitch/2] > 40
747                  && p_in[i_visible_pitch/2 + i_col/2] > 40 )
748             {
749                 if( i_lastwhite == -1 )
750                 {
751                     i_lastwhite = i;
752                 }
753                 i_firstwhite = i;
754             }
755             p_in += i_pitch;
756         }
757         break;
758
759     default:
760         break;
761     }
762
763     /* Decide whether it's worth changing the size */
764     if( i_lastwhite == -1 )
765     {
766         p_vout->p_sys->i_lastchange = 0;
767         return;
768     }
769
770     if( (unsigned int)(i_lastwhite - i_firstwhite)
771                                            < p_vout->p_sys->i_height / 2 )
772     {
773         p_vout->p_sys->i_lastchange = 0;
774         return;
775     }
776
777     if( (unsigned int)(i_lastwhite - i_firstwhite)
778                                           < p_vout->p_sys->i_height + 16
779          && (unsigned int)(i_lastwhite - i_firstwhite + 16)
780                                                 > p_vout->p_sys->i_height )
781     {
782         p_vout->p_sys->i_lastchange = 0;
783         return;
784     }
785
786     /* We need at least 25 images to make up our mind */
787     p_vout->p_sys->i_lastchange++;
788     if( p_vout->p_sys->i_lastchange < 25 )
789     {
790         return;
791     }
792 #endif //BEST_AUTOCROP
793
794     /* Tune a few values */
795     if( i_firstwhite & 1 )
796     {
797         i_firstwhite--;
798     }
799
800     if( !(i_lastwhite & 1) )
801     {
802         i_lastwhite++;
803     }
804
805     /* Change size */
806     p_vout->p_sys->i_y = i_firstwhite;
807     p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
808 #ifdef BEST_AUTOCROP
809     // check p_vout->p_sys->i_height <= p_vout->output.i_height
810     if (p_vout->p_sys->i_height > p_vout->output.i_height)
811         p_vout->p_sys->i_height = p_vout->output.i_height;
812 #endif
813
814     p_vout->p_sys->i_aspect = p_vout->output.i_aspect
815                             * p_vout->output.i_height / p_vout->p_sys->i_height
816                             * p_vout->p_sys->i_width / p_vout->output.i_width;
817
818     p_vout->p_sys->b_changed = true;
819 }
820
821 /**
822  * Forward mouse event with proper conversion.
823  */
824 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
825                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
826 {
827     vout_thread_t *p_vout = p_data;
828     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
829
830     /* Translate the mouse coordinates
831      * FIXME missing lock */
832     if( !strcmp( psz_var, "mouse-x" ) )
833         newval.i_int += p_vout->p_sys->i_x;
834     else if( !strcmp( psz_var, "mouse-y" ) )
835         newval.i_int += p_vout->p_sys->i_y;
836
837     return var_Set( p_vout, psz_var, newval );
838 }
839
840 #ifdef BEST_AUTOCROP
841 /*****************************************************************************
842  * FilterCallback: called when changing the ratio on the fly.
843  *****************************************************************************/
844 static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
845                            vlc_value_t oldval, vlc_value_t newval,
846                            void *p_data )
847 {
848     VLC_UNUSED(p_data); VLC_UNUSED(oldval);
849     vout_thread_t * p_vout = (vout_thread_t *)p_this;
850
851     if( !strcmp( psz_var, "ratio-crop" ) )
852     {
853         vlc_mutex_lock( &p_vout->p_sys->lock );
854         if ( !strcmp( newval.psz_string, "Auto" ) )
855             p_vout->p_sys->i_ratio = 0;
856         else
857         {
858             p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
859             p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
860             p_vout->p_sys->b_autocrop = true;
861         }
862         if (p_vout->p_sys->i_ratio)
863         {
864             if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) /
865                                     p_vout->output.i_height)
866                 p_vout->p_sys->i_ratio = (p_vout->output.i_width * 1000) /
867                                     p_vout->output.i_height;
868             if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
869                 p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;
870         }
871         vlc_mutex_unlock( &p_vout->p_sys->lock );
872      }
873     return VLC_SUCCESS;
874 }
875 #endif