]> git.sesse.net Git - vlc/blob - modules/video_filter/crop.c
Used VLC_CODEC_* and vlc_fourcc_GetCodec when suitable.
[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     vout_thread_t *p_vout;
149
150     unsigned int i_x, i_y;
151     unsigned int i_width, i_height, i_aspect;
152
153     bool b_autocrop;
154
155     /* Autocrop specific variables */
156     unsigned int i_lastchange;
157     bool   b_changed;
158 #ifdef BEST_AUTOCROP
159     unsigned int i_ratio_max;
160     unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
161     unsigned int i_ratio;
162 #endif
163
164 };
165
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 )
170 {
171     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
172 }
173
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 )
180 {
181     vout_thread_t *p_vout = (vout_thread_t *)p_this;
182
183     /* Allocate structure */
184     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
185     if( p_vout->p_sys == NULL )
186         return VLC_ENOMEM;
187
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;
194
195     return VLC_SUCCESS;
196 }
197
198 /*****************************************************************************
199  * Init: initialize Crop video thread output method
200  *****************************************************************************/
201 static int Init( vout_thread_t *p_vout )
202 {
203     char *psz_var;
204     video_format_t fmt;
205
206     I_OUTPUTPICTURES = 0;
207     memset( &fmt, 0, sizeof(video_format_t) );
208
209     p_vout->p_sys->i_lastchange = 0;
210     p_vout->p_sys->b_changed = false;
211
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;
218
219     /* Shall we use autocrop ? */
220     p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
221 #ifdef BEST_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     vlc_value_t val={0};
232     var_Get( p_vout, "ratio-crop", &val );
233     val.psz_string = "0";
234     var_SetString( p_vout, "ratio-crop", val.psz_string);
235
236     if (p_vout->p_sys->b_autocrop)
237         p_vout->p_sys->i_ratio = 0;
238     else
239     {
240         p_vout->p_sys->i_ratio = config_GetInt( p_vout, "crop-ratio" );
241         // ratio < width / height => ratio = 0 (unchange ratio)
242         if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) / p_vout->output.i_height)
243             p_vout->p_sys->i_ratio = 0;
244     }
245 #endif
246
247
248     /* Get geometry value from the user */
249     psz_var = config_GetPsz( p_vout, "crop-geometry" );
250     if( psz_var )
251     {
252         char *psz_parser, *psz_tmp;
253
254         psz_parser = psz_tmp = psz_var;
255         while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
256
257         if( *psz_tmp )
258         {
259             psz_tmp[0] = '\0';
260             p_vout->p_sys->i_width = atoi( psz_parser );
261
262             psz_parser = ++psz_tmp;
263             while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
264
265             if( *psz_tmp )
266             {
267                 psz_tmp[0] = '\0';
268                 p_vout->p_sys->i_height = atoi( psz_parser );
269
270                 psz_parser = ++psz_tmp;
271                 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
272
273                 if( *psz_tmp )
274                 {
275                     psz_tmp[0] = '\0';
276                     p_vout->p_sys->i_x = atoi( psz_parser );
277                     p_vout->p_sys->i_y = atoi( ++psz_tmp );
278                 }
279                 else
280                 {
281                     p_vout->p_sys->i_x = atoi( psz_parser );
282                     p_vout->p_sys->i_y =
283                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
284                 }
285             }
286             else
287             {
288                 p_vout->p_sys->i_height = atoi( psz_parser );
289                 p_vout->p_sys->i_x =
290                      ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
291                 p_vout->p_sys->i_y =
292                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
293             }
294         }
295         else
296         {
297             p_vout->p_sys->i_width = atoi( psz_parser );
298             p_vout->p_sys->i_height = p_vout->output.i_height;
299             p_vout->p_sys->i_x =
300                      ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
301             p_vout->p_sys->i_y =
302                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
303         }
304
305         /* Check for validity */
306         if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
307                                                    > p_vout->output.i_width )
308         {
309             p_vout->p_sys->i_x = 0;
310             if( p_vout->p_sys->i_width > p_vout->output.i_width )
311             {
312                 p_vout->p_sys->i_width = p_vout->output.i_width;
313             }
314         }
315
316         if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
317                                                    > p_vout->output.i_height )
318         {
319             p_vout->p_sys->i_y = 0;
320             if( p_vout->p_sys->i_height > p_vout->output.i_height )
321             {
322                 p_vout->p_sys->i_height = p_vout->output.i_height;
323             }
324         }
325
326         free( psz_var );
327     }
328     else
329 #ifdef BEST_AUTOCROP
330     if (p_vout->p_sys->i_ratio)
331     {
332         p_vout->p_sys->i_aspect    =  p_vout->p_sys->i_ratio * 432;
333         p_vout->p_sys->i_width  = p_vout->fmt_out.i_visible_width;
334         p_vout->p_sys->i_height = p_vout->output.i_aspect
335                                 * p_vout->output.i_height / p_vout->p_sys->i_aspect
336                                 * p_vout->p_sys->i_width / p_vout->output.i_width;
337         p_vout->p_sys->i_height += p_vout->p_sys->i_height % 2;
338         p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
339         p_vout->p_sys->i_y = (p_vout->output.i_height - p_vout->p_sys->i_height) / 2;
340     }
341     else
342 #endif
343     {
344         p_vout->p_sys->i_width  = p_vout->fmt_out.i_visible_width;
345         p_vout->p_sys->i_height = p_vout->fmt_out.i_visible_height;
346         p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
347         p_vout->p_sys->i_y = p_vout->fmt_out.i_y_offset;
348     }
349
350     /* Pheeew. Parsing done. */
351     msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
352                      p_vout->p_sys->i_width, p_vout->p_sys->i_height,
353                      p_vout->p_sys->i_x, p_vout->p_sys->i_y,
354                      p_vout->p_sys->b_autocrop ? "" : "not " );
355     /* Set current output image properties */
356     p_vout->p_sys->i_aspect = p_vout->fmt_out.i_aspect
357            * p_vout->fmt_out.i_visible_height / p_vout->p_sys->i_height
358            * p_vout->p_sys->i_width / p_vout->fmt_out.i_visible_width;
359
360 #ifdef BEST_AUTOCROP
361     msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
362 #endif
363     fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
364     fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
365     fmt.i_x_offset = fmt.i_y_offset = 0;
366     fmt.i_chroma = p_vout->render.i_chroma;
367     fmt.i_aspect = p_vout->p_sys->i_aspect;
368     fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
369     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
370
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 )
374     {
375         msg_Err( p_vout, "failed to create vout" );
376         dialog_Fatal( p_vout, _("Cropping failed"),
377                         _("VLC could not open the video output module.") );
378         return VLC_EGENERIC;
379     }
380
381 #ifdef BEST_AUTOCROP
382     var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
383 #endif
384
385     vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
386
387     vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
388
389     return VLC_SUCCESS;
390 }
391
392 /*****************************************************************************
393  * End: terminate Crop video thread output method
394  *****************************************************************************/
395 static void End( vout_thread_t *p_vout )
396 {
397     vout_sys_t *p_sys = p_vout->p_sys;
398
399     if( p_sys->p_vout )
400     {
401         vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
402         vout_CloseAndRelease( p_sys->p_vout );
403     }
404
405     vout_filter_ReleaseDirectBuffers( p_vout );
406 }
407
408 /*****************************************************************************
409  * Destroy: destroy Crop video thread output method
410  *****************************************************************************
411  * Terminate an output method created by CropCreateOutputMethod
412  *****************************************************************************/
413 static void Destroy( vlc_object_t *p_this )
414 {
415     vout_thread_t *p_vout = (vout_thread_t *)p_this;
416
417     free( p_vout->p_sys );
418 }
419
420 /*****************************************************************************
421  * Manage: handle Crop events
422  *****************************************************************************
423  * This function should be called regularly by video output thread. It manages
424  * console events. It returns a non null value on error.
425  *****************************************************************************/
426 static int Manage( vout_thread_t *p_vout )
427 {
428     video_format_t fmt;
429
430     if( !p_vout->p_sys->b_changed )
431     {
432         return VLC_SUCCESS;
433     }
434
435     memset( &fmt, 0, sizeof(video_format_t) );
436
437 #ifdef BEST_AUTOCROP
438     msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
439                      p_vout->p_sys->i_width, p_vout->p_sys->i_height,
440                      p_vout->p_sys->i_x, p_vout->p_sys->i_y,
441                      p_vout->p_sys->b_autocrop ? "" : "not " );
442
443     msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
444 #endif
445
446     if( p_vout->p_sys->p_vout )
447     {
448         vout_filter_DelChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
449         vout_CloseAndRelease( p_vout->p_sys->p_vout );
450     }
451
452     fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
453     fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
454     fmt.i_x_offset = fmt.i_y_offset = 0;
455     fmt.i_chroma = p_vout->render.i_chroma;
456     fmt.i_aspect = p_vout->p_sys->i_aspect;
457     fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
458     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
459
460     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
461     if( p_vout->p_sys->p_vout == NULL )
462     {
463         msg_Err( p_vout, "failed to create vout" );
464         dialog_Fatal( p_vout, _("Cropping failed"),
465                         _("VLC could not open the video output module.") );
466         return VLC_EGENERIC;
467     }
468     vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
469
470     p_vout->p_sys->b_changed = false;
471     p_vout->p_sys->i_lastchange = 0;
472
473     return VLC_SUCCESS;
474 }
475
476 /*****************************************************************************
477  * Render: display previously rendered output
478  *****************************************************************************
479  * This function sends the currently rendered image to Crop image, waits
480  * until it is displayed and switches the two rendering buffers, preparing next
481  * frame.
482  *****************************************************************************/
483 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
484 {
485     picture_t *p_outpic = NULL;
486     int i_plane;
487
488     if( p_vout->p_sys->b_changed )
489     {
490         return;
491     }
492
493     while( ( p_outpic =
494                  vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
495            ) == NULL )
496     {
497         if( !vlc_object_alive (p_vout) || p_vout->b_error )
498         {
499             vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
500             return;
501         }
502
503         msleep( VOUT_OUTMEM_SLEEP );
504     }
505
506     p_outpic->date = p_pic->date;
507     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
508
509     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
510     {
511         uint8_t *p_in, *p_out, *p_out_end;
512         int i_in_pitch = p_pic->p[i_plane].i_pitch;
513         const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
514         const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
515
516         p_in = p_pic->p[i_plane].p_pixels
517                 /* Skip the right amount of lines */
518                 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
519                                  p_vout->p_sys->i_y / p_vout->output.i_height )
520                 /* Skip the right amount of columns */
521                 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
522
523         p_out = p_outpic->p[i_plane].p_pixels;
524         p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
525
526         while( p_out < p_out_end )
527         {
528             vlc_memcpy( p_out, p_in, i_copy_pitch );
529             p_in += i_in_pitch;
530             p_out += i_out_pitch;
531         }
532     }
533
534     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
535     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
536
537     /* The source image may still be in the cache ... parse it! */
538     if( p_vout->p_sys->b_autocrop )
539     {
540         UpdateStats( p_vout, p_pic );
541     }
542 }
543
544 #ifdef BEST_AUTOCROP
545 static bool NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
546                                int i_visible_pitch, int i_lines,
547                                int i_lumThreshold, int i_skipCountPercent,
548                                int i_nonBlackPixel, int i_chroma)
549 {
550     const int i_col = i_line * i_pitch / i_lines;
551     int i_index, i_count = 0;
552     int i_skipCount = 0;
553
554     switch(i_chroma)
555     {
556     // planar YUV
557         case VLC_CODEC_I444:
558         case VLC_CODEC_I422:
559         case VLC_CODEC_I420:
560         case VLC_CODEC_YV12:
561         case VLC_CODEC_I411:
562         case VLC_CODEC_I410:
563         case VLC_CODEC_YUVA:
564             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
565             for (i_index = i_col/2 + i_skipCount/2;
566                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
567                  i_index++)
568             {
569                  i_count += (p_in[i_index] > i_lumThreshold);
570             if (i_count > i_nonBlackPixel) break;
571             }
572             break;
573     // packed RGB
574         case VLC_CODEC_RGB8:    // packed by 1
575             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
576             for (i_index = i_col/2 + i_skipCount/2;
577                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
578                  i_index++)
579             {
580                  i_count += (p_in[i_index] > i_lumThreshold);
581             if (i_count > i_nonBlackPixel) break;
582             }
583             break;
584         case VLC_CODEC_RGB15:    // packed by 2
585         case VLC_CODEC_RGB16:    // packed by 2
586             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
587             for (i_index = i_col/2 + i_skipCount/2 -
588                                 (i_col/2 + i_skipCount/2) % 2;
589                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
590                  i_index+=2)
591             {
592                  i_count += (p_in[i_index] > i_lumThreshold) &&
593                             (p_in[i_index + 1] > i_lumThreshold);
594             if (i_count > i_nonBlackPixel) break;
595             }
596             break;
597         case VLC_CODEC_RGB24:    // packed by 3
598             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
599             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             {
601                  i_count += (p_in[i_index] > i_lumThreshold) &&
602                             (p_in[i_index + 1] > i_lumThreshold) &&
603                             (p_in[i_index + 2] > i_lumThreshold);
604             if (i_count > i_nonBlackPixel) break;
605             }
606             break;
607         case VLC_CODEC_RGB32:    // packed by 4
608             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
609             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             {
611                  i_count += (uint32_t)(*(p_in + i_index)) > (uint32_t)i_lumThreshold;
612             if (i_count > i_nonBlackPixel) break;
613             }
614             break;
615     // packed YUV
616         case VLC_CODEC_YUYV:    // packed by 2
617         case VLC_CODEC_UYVY:    // packed by 2
618             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
619             for (i_index = (i_col/2 + i_skipCount/2) -
620                            (i_col/2 + i_skipCount/2) % 2;
621                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
622                  i_index+=2)
623             {
624                  i_count += (p_in[i_index] > i_lumThreshold);
625             if (i_count > i_nonBlackPixel) break;
626             }
627             break;
628         default :
629             break;
630     }
631     return (i_count > i_nonBlackPixel);
632 }
633 #endif
634
635 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
636 {
637    uint8_t *p_in = p_pic->p[0].p_pixels;
638     int i_pitch = p_pic->p[0].i_pitch;
639     int i_visible_pitch = p_pic->p[0].i_visible_pitch;
640     int i_lines = p_pic->p[0].i_visible_lines;
641     int i_firstwhite = -1, i_lastwhite = -1, i;
642 #ifdef BEST_AUTOCROP
643     int i_time = p_vout->p_sys->i_time;
644     int i_diff = p_vout->p_sys->i_diff;
645
646     if (!p_vout->p_sys->i_ratio)
647     {
648         /* Determine where black borders are */
649         for( i = 0 ; i < i_lines ; i++)
650         {
651                    if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
652                             p_vout->p_sys->i_threshold,
653                             p_vout->p_sys->i_skipPercent,
654                             p_vout->p_sys->i_nonBlackPixel,
655                             p_vout->output.i_chroma))
656                 {
657                     i_firstwhite = i;
658                     i_lastwhite = i_lines - i;
659                     break;
660                 }
661                 p_in += i_pitch;
662         }
663
664         /* Decide whether it's worth changing the size */
665         if( i_lastwhite == -1 )
666         {
667             p_vout->p_sys->i_lastchange = 0;
668             return;
669         }
670
671         if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
672         {
673             p_vout->p_sys->i_lastchange = 0;
674             return;
675         }
676
677         if (p_vout->output.i_aspect
678                             * p_vout->output.i_height /
679                                 (i_lastwhite - i_firstwhite + 1)
680                             * p_vout->p_sys->i_width /
681                                p_vout->output.i_width >
682                                     p_vout->p_sys->i_ratio_max * 432)
683         {
684             int i_height = ((p_vout->output.i_aspect / 432) *
685                            p_vout->output.i_height * p_vout->p_sys->i_width) /
686                           (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
687             i_firstwhite = (p_vout->output.i_height - i_height) / 2;
688             i_lastwhite =  p_vout->output.i_height - i_firstwhite;
689 /*
690             p_vout->p_sys->i_lastchange = 0;
691             return;
692 */
693         }
694
695         if( (i_lastwhite - i_firstwhite) <
696                         (int) (p_vout->p_sys->i_height + i_diff)
697              && (i_lastwhite - i_firstwhite + i_diff) >
698                         (int) p_vout->p_sys->i_height )
699         {
700             p_vout->p_sys->i_lastchange = 0;
701             return;
702         }
703
704         /* We need at least 'i_time' images to make up our mind */
705         p_vout->p_sys->i_lastchange++;
706         if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
707         {
708             return;
709         }
710     }
711     else
712     {
713         if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
714         {
715             p_vout->p_sys->i_aspect    =  p_vout->p_sys->i_ratio * 432;
716             int i_height = p_vout->output.i_aspect
717                                     * p_vout->output.i_height /
718                                         p_vout->p_sys->i_aspect
719                                     * p_vout->p_sys->i_width /
720                                         p_vout->output.i_width;
721             i_firstwhite = (p_vout->output.i_height - i_height) / 2;
722             i_lastwhite =  p_vout->output.i_height - i_firstwhite;
723         }
724         else
725         {
726             return;
727         }
728     }
729
730 #else
731     /* Determine where black borders are */
732     switch( p_vout->output.i_chroma )
733     {
734     case VLC_CODEC_I420:
735         /* XXX: Do not laugh ! I know this is very naive. But it's just a
736          *      proof of concept code snippet... */
737         for( i = i_lines ; i-- ; )
738         {
739             const int i_col = i * i_pitch / i_lines;
740
741             if( p_in[i_col/2] > 40
742                  && p_in[i_visible_pitch/2] > 40
743                  && p_in[i_visible_pitch/2 + i_col/2] > 40 )
744             {
745                 if( i_lastwhite == -1 )
746                 {
747                     i_lastwhite = i;
748                 }
749                 i_firstwhite = i;
750             }
751             p_in += i_pitch;
752         }
753         break;
754
755     default:
756         break;
757     }
758
759     /* Decide whether it's worth changing the size */
760     if( i_lastwhite == -1 )
761     {
762         p_vout->p_sys->i_lastchange = 0;
763         return;
764     }
765
766     if( (unsigned int)(i_lastwhite - i_firstwhite)
767                                            < p_vout->p_sys->i_height / 2 )
768     {
769         p_vout->p_sys->i_lastchange = 0;
770         return;
771     }
772
773     if( (unsigned int)(i_lastwhite - i_firstwhite)
774                                           < p_vout->p_sys->i_height + 16
775          && (unsigned int)(i_lastwhite - i_firstwhite + 16)
776                                                 > p_vout->p_sys->i_height )
777     {
778         p_vout->p_sys->i_lastchange = 0;
779         return;
780     }
781
782     /* We need at least 25 images to make up our mind */
783     p_vout->p_sys->i_lastchange++;
784     if( p_vout->p_sys->i_lastchange < 25 )
785     {
786         return;
787     }
788 #endif //BEST_AUTOCROP
789
790     /* Tune a few values */
791     if( i_firstwhite & 1 )
792     {
793         i_firstwhite--;
794     }
795
796     if( !(i_lastwhite & 1) )
797     {
798         i_lastwhite++;
799     }
800
801     /* Change size */
802     p_vout->p_sys->i_y = i_firstwhite;
803     p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
804 #ifdef BEST_AUTOCROP
805     // check p_vout->p_sys->i_height <= p_vout->output.i_height
806     if (p_vout->p_sys->i_height > p_vout->output.i_height)
807         p_vout->p_sys->i_height = p_vout->output.i_height;
808 #endif
809
810     p_vout->p_sys->i_aspect = p_vout->output.i_aspect
811                             * p_vout->output.i_height / p_vout->p_sys->i_height
812                             * p_vout->p_sys->i_width / p_vout->output.i_width;
813
814     p_vout->p_sys->b_changed = true;
815 }
816
817 /**
818  * Forward mouse event with proper conversion.
819  */
820 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
821                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
822 {
823     vout_thread_t *p_vout = p_data;
824     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
825
826     /* Translate the mouse coordinates
827      * FIXME missing lock */
828     if( !strcmp( psz_var, "mouse-x" ) )
829         newval.i_int += p_vout->p_sys->i_x;
830     else if( !strcmp( psz_var, "mouse-y" ) )
831         newval.i_int += p_vout->p_sys->i_y;
832
833     return var_Set( p_vout, psz_var, newval );
834 }
835
836 #ifdef BEST_AUTOCROP
837 /*****************************************************************************
838  * FilterCallback: called when changing the ratio on the fly.
839  *****************************************************************************/
840 static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
841                            vlc_value_t oldval, vlc_value_t newval,
842                            void *p_data )
843 {
844     VLC_UNUSED(p_data); VLC_UNUSED(oldval);
845     vout_thread_t * p_vout = (vout_thread_t *)p_this;
846
847     if( !strcmp( psz_var, "ratio-crop" ) )
848     {
849         if ( !strcmp( newval.psz_string, "Auto" ) )
850             p_vout->p_sys->i_ratio = 0;
851         else
852         {
853             p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
854             p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
855             p_vout->p_sys->b_autocrop = true;
856         }
857         if (p_vout->p_sys->i_ratio)
858         {
859             if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) /
860                                     p_vout->output.i_height)
861                 p_vout->p_sys->i_ratio = (p_vout->output.i_width * 1000) /
862                                     p_vout->output.i_height;
863             if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
864                 p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;
865         }
866      }
867     return VLC_SUCCESS;
868 }
869 #endif