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