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