]> git.sesse.net Git - vlc/blob - modules/video_filter/crop.c
Remove stdlib.h
[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 #include <string.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc_vout.h>
33 #include <vlc_interface.h>
34
35 #include "filter_common.h"
36
37 #define BEST_AUTOCROP 1
38 #ifdef BEST_AUTOCROP
39     #define RATIO_MAX 15000  // 10*4/3 for a 360
40 #endif
41
42 /*****************************************************************************
43  * Local prototypes
44  *****************************************************************************/
45 static int  Create    ( vlc_object_t * );
46 static void Destroy   ( vlc_object_t * );
47
48 static int  Init      ( vout_thread_t * );
49 static void End       ( vout_thread_t * );
50 static int  Manage    ( vout_thread_t * );
51 static void Render    ( vout_thread_t *, picture_t * );
52
53 static void UpdateStats    ( vout_thread_t *, picture_t * );
54
55 static int  SendEvents( vlc_object_t *, char const *,
56                         vlc_value_t, vlc_value_t, void * );
57
58 #ifdef BEST_AUTOCROP
59 /*****************************************************************************
60  * Callback prototypes
61  *****************************************************************************/
62 static int FilterCallback ( vlc_object_t *, char const *,
63                             vlc_value_t, vlc_value_t, void * );
64 #endif
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 #define GEOMETRY_TEXT N_("Crop geometry (pixels)")
70 #define GEOMETRY_LONGTEXT N_("Set the geometry of the zone to crop. This is set as <width> x <height> + <left offset> + <top offset>.")
71
72 #define AUTOCROP_TEXT N_("Automatic cropping")
73 #define AUTOCROP_LONGTEXT N_("Automatically detect black borders and crop them.")
74
75 #ifdef BEST_AUTOCROP
76 #define RATIOMAX_TEXT N_("Ratio max (x 1000)")
77 #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.")
78
79 #define RATIO_TEXT N_("Manual ratio")
80 #define RATIO_LONGTEXT N_("Force a ratio (0 for automatic). Value is x1000: 1333 means 4/3.")
81
82 #define TIME_TEXT N_("Number of images for change")
83 #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.")
84
85 #define DIFF_TEXT N_("Number of lines for change")
86 #define DIFF_LONGTEXT N_("The minimum difference in the number of detected black lines to consider that ratio changed and trigger recrop.")
87
88 #define NBP_TEXT N_("Number of non black pixels ")
89 #define NBP_LONGTEXT N_("The maximum of non-black pixels in a line to consider"\
90                         " that the line is black.")
91
92 #define SKIP_TEXT N_("Skip percentage (%)")
93 #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.")
94
95 #define LUM_TEXT N_("Luminance threshold ")
96 #define LUM_LONGTEXT N_("Maximum luminance to consider a pixel as black (0-255).")
97 #endif
98
99 vlc_module_begin();
100     set_description( _("Crop video filter") );
101     set_shortname( _("Crop" ));
102     set_category( CAT_VIDEO );
103     set_subcategory( SUBCAT_VIDEO_VFILTER );
104     set_capability( "video filter", 0 );
105
106     add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT,
107                                              GEOMETRY_LONGTEXT, VLC_FALSE );
108     add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT,
109                                    AUTOCROP_LONGTEXT, VLC_FALSE );
110
111 #ifdef BEST_AUTOCROP
112     add_integer_with_range( "autocrop-ratio-max", 2405, 0, RATIO_MAX, NULL,
113                             RATIOMAX_TEXT, RATIOMAX_LONGTEXT, VLC_TRUE );
114
115     add_integer_with_range( "crop-ratio", 0, 0, RATIO_MAX, NULL, RATIO_TEXT,
116                             RATIO_LONGTEXT, VLC_FALSE );
117     add_integer( "autocrop-time", 25, NULL, TIME_TEXT,
118                  TIME_LONGTEXT, VLC_TRUE );
119     add_integer( "autocrop-diff", 16, NULL, DIFF_TEXT,
120                                             DIFF_LONGTEXT, VLC_TRUE );
121
122     add_integer( "autocrop-non-black-pixels", 3, NULL,
123                  NBP_TEXT, NBP_LONGTEXT, VLC_TRUE );
124
125     add_integer_with_range( "autocrop-skip-percent", 17, 0, 100, NULL,
126                             SKIP_TEXT, SKIP_LONGTEXT, VLC_TRUE );
127
128     add_integer_with_range( "autocrop-luminance-threshold", 40, 0, 128, NULL,
129                             LUM_TEXT, LUM_LONGTEXT, VLC_TRUE );
130 #endif //BEST_AUTOCROP
131
132     add_shortcut( "crop" );
133     set_callbacks( Create, Destroy );
134 vlc_module_end();
135
136 /*****************************************************************************
137  * vout_sys_t: Crop video output method descriptor
138  *****************************************************************************
139  * This structure is part of the video output thread descriptor.
140  * It describes the Crop specific properties of an output thread.
141  *****************************************************************************/
142 struct vout_sys_t
143 {
144     vout_thread_t *p_vout;
145
146     unsigned int i_x, i_y;
147     unsigned int i_width, i_height, i_aspect;
148
149     vlc_bool_t b_autocrop;
150
151     /* Autocrop specific variables */
152     unsigned int i_lastchange;
153     vlc_bool_t   b_changed;
154 #ifdef BEST_AUTOCROP
155     unsigned int i_ratio_max;
156     unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
157     unsigned int i_ratio;
158 #endif
159
160 };
161
162 /*****************************************************************************
163  * Control: control facility for the vout (forwards to child vout)
164  *****************************************************************************/
165 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
166 {
167     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
168 }
169
170 /*****************************************************************************
171  * Create: allocates Crop video thread output method
172  *****************************************************************************
173  * This function allocates and initializes a Crop vout method.
174  *****************************************************************************/
175 static int Create( vlc_object_t *p_this )
176 {
177     vout_thread_t *p_vout = (vout_thread_t *)p_this;
178
179     /* Allocate structure */
180     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
181     if( p_vout->p_sys == NULL )
182     {
183         msg_Err( p_vout, "out of memory" );
184         return VLC_ENOMEM;
185     }
186
187     p_vout->pf_init = Init;
188     p_vout->pf_end = End;
189     p_vout->pf_manage = Manage;
190     p_vout->pf_render = Render;
191     p_vout->pf_display = NULL;
192     p_vout->pf_control = Control;
193
194     return VLC_SUCCESS;
195 }
196
197 /*****************************************************************************
198  * Init: initialize Crop video thread output method
199  *****************************************************************************/
200 static int Init( vout_thread_t *p_vout )
201 {
202     int   i_index;
203     char *psz_var;
204     picture_t *p_pic;
205     video_format_t fmt;
206
207     I_OUTPUTPICTURES = 0;
208     memset( &fmt, 0, sizeof(video_format_t) );
209
210     p_vout->p_sys->i_lastchange = 0;
211     p_vout->p_sys->b_changed = VLC_FALSE;
212
213     /* Initialize the output structure */
214     p_vout->output.i_chroma = p_vout->render.i_chroma;
215     p_vout->output.i_width  = p_vout->render.i_width;
216     p_vout->output.i_height = p_vout->render.i_height;
217     p_vout->output.i_aspect = p_vout->render.i_aspect;
218     p_vout->fmt_out = p_vout->fmt_in;
219
220     /* Shall we use autocrop ? */
221     p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
222 #ifdef BEST_AUTOCROP
223     p_vout->p_sys->i_ratio_max = config_GetInt( p_vout, "autocrop-ratio-max" );
224     p_vout->p_sys->i_threshold =
225                     config_GetInt( p_vout, "autocrop-luminance-threshold" );
226     p_vout->p_sys->i_skipPercent =
227                     config_GetInt( p_vout, "autocrop-skip-percent" );
228     p_vout->p_sys->i_nonBlackPixel =
229                     config_GetInt( p_vout, "autocrop-non-black-pixels" );
230     p_vout->p_sys->i_diff = config_GetInt( p_vout, "autocrop-diff" );
231     p_vout->p_sys->i_time = config_GetInt( p_vout, "autocrop-time" );
232     vlc_value_t val={0};
233     var_Get( p_vout, "ratio-crop", &val );
234     val.psz_string = "0";
235     var_SetString( p_vout, "ratio-crop", val.psz_string);
236
237     if (p_vout->p_sys->b_autocrop)
238         p_vout->p_sys->i_ratio = 0;
239     else
240     {
241         p_vout->p_sys->i_ratio = config_GetInt( p_vout, "crop-ratio" );
242         // ratio < width / height => ratio = 0 (unchange ratio)
243         if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) / p_vout->output.i_height)
244             p_vout->p_sys->i_ratio = 0;
245     }
246 #endif
247
248
249     /* Get geometry value from the user */
250     psz_var = config_GetPsz( p_vout, "crop-geometry" );
251     if( psz_var )
252     {
253         char *psz_parser, *psz_tmp;
254
255         psz_parser = psz_tmp = psz_var;
256         while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
257
258         if( *psz_tmp )
259         {
260             psz_tmp[0] = '\0';
261             p_vout->p_sys->i_width = atoi( psz_parser );
262
263             psz_parser = ++psz_tmp;
264             while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
265
266             if( *psz_tmp )
267             {
268                 psz_tmp[0] = '\0';
269                 p_vout->p_sys->i_height = atoi( psz_parser );
270
271                 psz_parser = ++psz_tmp;
272                 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
273
274                 if( *psz_tmp )
275                 {
276                     psz_tmp[0] = '\0';
277                     p_vout->p_sys->i_x = atoi( psz_parser );
278                     p_vout->p_sys->i_y = atoi( ++psz_tmp );
279                 }
280                 else
281                 {
282                     p_vout->p_sys->i_x = atoi( psz_parser );
283                     p_vout->p_sys->i_y =
284                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
285                 }
286             }
287             else
288             {
289                 p_vout->p_sys->i_height = atoi( psz_parser );
290                 p_vout->p_sys->i_x =
291                      ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
292                 p_vout->p_sys->i_y =
293                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
294             }
295         }
296         else
297         {
298             p_vout->p_sys->i_width = atoi( psz_parser );
299             p_vout->p_sys->i_height = p_vout->output.i_height;
300             p_vout->p_sys->i_x =
301                      ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
302             p_vout->p_sys->i_y =
303                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
304         }
305
306         /* Check for validity */
307         if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
308                                                    > p_vout->output.i_width )
309         {
310             p_vout->p_sys->i_x = 0;
311             if( p_vout->p_sys->i_width > p_vout->output.i_width )
312             {
313                 p_vout->p_sys->i_width = p_vout->output.i_width;
314             }
315         }
316
317         if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
318                                                    > p_vout->output.i_height )
319         {
320             p_vout->p_sys->i_y = 0;
321             if( p_vout->p_sys->i_height > p_vout->output.i_height )
322             {
323                 p_vout->p_sys->i_height = p_vout->output.i_height;
324             }
325         }
326
327         free( psz_var );
328     }
329     else
330 #ifdef BEST_AUTOCROP
331     if (p_vout->p_sys->i_ratio)
332     {
333         p_vout->p_sys->i_aspect    =  p_vout->p_sys->i_ratio * 432;
334         p_vout->p_sys->i_width  = p_vout->fmt_out.i_visible_width;
335         p_vout->p_sys->i_height = p_vout->output.i_aspect
336                                 * p_vout->output.i_height / p_vout->p_sys->i_aspect
337                                 * p_vout->p_sys->i_width / p_vout->output.i_width;
338         p_vout->p_sys->i_height += p_vout->p_sys->i_height % 2;
339         p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
340         p_vout->p_sys->i_y = (p_vout->output.i_height - p_vout->p_sys->i_height) / 2;
341     }
342     else
343 #endif
344     {
345         p_vout->p_sys->i_width  = p_vout->fmt_out.i_visible_width;
346         p_vout->p_sys->i_height = p_vout->fmt_out.i_visible_height;
347         p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
348         p_vout->p_sys->i_y = p_vout->fmt_out.i_y_offset;
349     }
350
351     /* Pheeew. Parsing done. */
352     msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
353                      p_vout->p_sys->i_width, p_vout->p_sys->i_height,
354                      p_vout->p_sys->i_x, p_vout->p_sys->i_y,
355                      p_vout->p_sys->b_autocrop ? "" : "not " );
356     /* Set current output image properties */
357     p_vout->p_sys->i_aspect = p_vout->fmt_out.i_aspect
358            * p_vout->fmt_out.i_visible_height / p_vout->p_sys->i_height
359            * p_vout->p_sys->i_width / p_vout->fmt_out.i_visible_width;
360
361 #ifdef BEST_AUTOCROP
362     msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
363 #endif
364     fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
365     fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
366     fmt.i_x_offset = fmt.i_y_offset = 0;
367     fmt.i_chroma = p_vout->render.i_chroma;
368     fmt.i_aspect = p_vout->p_sys->i_aspect;
369     fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
370     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
371
372     /* Try to open the real video output */
373     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
374     if( p_vout->p_sys->p_vout == NULL )
375     {
376         msg_Err( p_vout, "failed to create vout" );
377         intf_UserFatal( p_vout, VLC_FALSE, _("Cropping failed"),
378                         _("VLC could not open the video output module.") );
379         return VLC_EGENERIC;
380     }
381
382     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
383
384     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
385
386 #ifdef BEST_AUTOCROP
387     var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
388 #endif
389
390     ADD_PARENT_CALLBACKS( SendEventsToChild );
391
392     return VLC_SUCCESS;
393 }
394
395 /*****************************************************************************
396  * End: terminate Crop video thread output method
397  *****************************************************************************/
398 static void End( vout_thread_t *p_vout )
399 {
400     int i_index;
401
402     /* Free the fake output buffers we allocated */
403     for( i_index = I_OUTPUTPICTURES ; i_index ; )
404     {
405         i_index--;
406         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
407     }
408 }
409
410 /*****************************************************************************
411  * Destroy: destroy Crop video thread output method
412  *****************************************************************************
413  * Terminate an output method created by CropCreateOutputMethod
414  *****************************************************************************/
415 static void Destroy( vlc_object_t *p_this )
416 {
417     vout_thread_t *p_vout = (vout_thread_t *)p_this;
418
419     if( p_vout->p_sys->p_vout )
420     {
421         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
422         vlc_object_detach( p_vout->p_sys->p_vout );
423         vout_Destroy( p_vout->p_sys->p_vout );
424     }
425
426     DEL_PARENT_CALLBACKS( SendEventsToChild );
427
428     free( p_vout->p_sys );
429 }
430
431 /*****************************************************************************
432  * Manage: handle Crop events
433  *****************************************************************************
434  * This function should be called regularly by video output thread. It manages
435  * console events. It returns a non null value on error.
436  *****************************************************************************/
437 static int Manage( vout_thread_t *p_vout )
438 {
439     video_format_t fmt;
440
441     if( !p_vout->p_sys->b_changed )
442     {
443         return VLC_SUCCESS;
444     }
445
446     memset( &fmt, 0, sizeof(video_format_t) );
447
448 #ifdef BEST_AUTOCROP
449     msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
450                      p_vout->p_sys->i_width, p_vout->p_sys->i_height,
451                      p_vout->p_sys->i_x, p_vout->p_sys->i_y,
452                      p_vout->p_sys->b_autocrop ? "" : "not " );
453
454     msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
455 #endif
456
457     vout_Destroy( p_vout->p_sys->p_vout );
458
459     fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
460     fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
461     fmt.i_x_offset = fmt.i_y_offset = 0;
462     fmt.i_chroma = p_vout->render.i_chroma;
463     fmt.i_aspect = p_vout->p_sys->i_aspect;
464     fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
465     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
466
467     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
468     if( p_vout->p_sys->p_vout == NULL )
469     {
470         msg_Err( p_vout, "failed to create vout" );
471         intf_UserFatal( p_vout, VLC_FALSE, _("Cropping failed"),
472                         _("VLC could not open the video output module.") );
473         return VLC_EGENERIC;
474     }
475
476     p_vout->p_sys->b_changed = VLC_FALSE;
477     p_vout->p_sys->i_lastchange = 0;
478
479     return VLC_SUCCESS;
480 }
481
482 /*****************************************************************************
483  * Render: display previously rendered output
484  *****************************************************************************
485  * This function sends the currently rendered image to Crop image, waits
486  * until it is displayed and switches the two rendering buffers, preparing next
487  * frame.
488  *****************************************************************************/
489 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
490 {
491     picture_t *p_outpic = NULL;
492     int i_plane;
493
494     if( p_vout->p_sys->b_changed )
495     {
496         return;
497     }
498
499     while( ( p_outpic =
500                  vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
501            ) == NULL )
502     {
503         if( p_vout->b_die || p_vout->b_error )
504         {
505             vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
506             return;
507         }
508
509         msleep( VOUT_OUTMEM_SLEEP );
510     }
511
512     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
513     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
514
515     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
516     {
517         uint8_t *p_in, *p_out, *p_out_end;
518         int i_in_pitch = p_pic->p[i_plane].i_pitch;
519         const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
520         const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
521
522         p_in = p_pic->p[i_plane].p_pixels
523                 /* Skip the right amount of lines */
524                 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
525                                  p_vout->p_sys->i_y / p_vout->output.i_height )
526                 /* Skip the right amount of columns */
527                 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
528
529         p_out = p_outpic->p[i_plane].p_pixels;
530         p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
531
532         while( p_out < p_out_end )
533         {
534             p_vout->p_libvlc->pf_memcpy( p_out, p_in, i_copy_pitch );
535             p_in += i_in_pitch;
536             p_out += i_out_pitch;
537         }
538     }
539
540     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
541     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
542
543     /* The source image may still be in the cache ... parse it! */
544     if( p_vout->p_sys->b_autocrop )
545     {
546         UpdateStats( p_vout, p_pic );
547     }
548 }
549
550 #ifdef BEST_AUTOCROP
551 static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
552                                int i_visible_pitch, int i_lines,
553                                int i_lumThreshold, int i_skipCountPercent,
554                                int i_nonBlackPixel, int i_chroma)
555 {
556     const int i_col = i_line * i_pitch / i_lines;
557     int i_index, i_count = 0;
558     int i_skipCount = 0;
559
560     switch(i_chroma)
561     {
562     // planar YUV
563         case VLC_FOURCC('I','4','4','4'):
564         case VLC_FOURCC('I','4','2','2'):
565         case VLC_FOURCC('I','4','2','0'):
566         case VLC_FOURCC('Y','V','1','2'):
567         case VLC_FOURCC('I','Y','U','V'):
568         case VLC_FOURCC('I','4','1','1'):
569         case VLC_FOURCC('I','4','1','0'):
570         case VLC_FOURCC('Y','V','U','9'):
571         case VLC_FOURCC('Y','U','V','A'):
572             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
573             for (i_index = i_col/2 + i_skipCount/2;
574                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
575                  i_index++)
576             {
577                  i_count += (p_in[i_index] > i_lumThreshold);
578             if (i_count > i_nonBlackPixel) break;
579             }
580             break;
581     // packed RGB
582         case VLC_FOURCC('R','G','B','2'):    // packed by 1
583             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
584             for (i_index = i_col/2 + i_skipCount/2;
585                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
586                  i_index++)
587             {
588                  i_count += (p_in[i_index] > i_lumThreshold);
589             if (i_count > i_nonBlackPixel) break;
590             }
591             break;
592         case VLC_FOURCC('R','V','1','5'):    // packed by 2
593         case VLC_FOURCC('R','V','1','6'):    // packed by 2
594             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
595             for (i_index = i_col/2 + i_skipCount/2 -
596                                 (i_col/2 + i_skipCount/2) % 2;
597                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
598                  i_index+=2)
599             {
600                  i_count += (p_in[i_index] > i_lumThreshold) &&
601                             (p_in[i_index + 1] > i_lumThreshold);
602             if (i_count > i_nonBlackPixel) break;
603             }
604             break;
605         case VLC_FOURCC('R','V','2','4'):    // packed by 3
606             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
607             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)
608             {
609                  i_count += (p_in[i_index] > i_lumThreshold) &&
610                             (p_in[i_index + 1] > i_lumThreshold) &&
611                             (p_in[i_index + 2] > i_lumThreshold);
612             if (i_count > i_nonBlackPixel) break;
613             }
614             break;
615         case VLC_FOURCC('R','V','3','2'):    // packed by 4
616             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
617             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)
618             {
619                  i_count += (uint32_t)(*(p_in + i_index)) > (uint32_t)i_lumThreshold;
620             if (i_count > i_nonBlackPixel) break;
621             }
622             break;
623     // packed YUV
624         case VLC_FOURCC('Y','U','Y','2'):    // packed by 2
625         case VLC_FOURCC('Y','U','N','V'):    // packed by 2
626         case VLC_FOURCC('U','Y','V','Y'):    // packed by 2
627         case VLC_FOURCC('U','Y','N','V'):    // packed by 2
628         case VLC_FOURCC('Y','4','2','2'):    // packed by 2
629             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
630             for (i_index = (i_col/2 + i_skipCount/2) -
631                            (i_col/2 + i_skipCount/2) % 2;
632                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
633                  i_index+=2)
634             {
635                  i_count += (p_in[i_index] > i_lumThreshold);
636             if (i_count > i_nonBlackPixel) break;
637             }
638             break;
639         default :
640             break;
641     }
642     return (i_count > i_nonBlackPixel);
643 }
644 #endif
645
646 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
647 {
648    uint8_t *p_in = p_pic->p[0].p_pixels;
649     int i_pitch = p_pic->p[0].i_pitch;
650     int i_visible_pitch = p_pic->p[0].i_visible_pitch;
651     int i_lines = p_pic->p[0].i_visible_lines;
652     int i_firstwhite = -1, i_lastwhite = -1, i;
653 #ifdef BEST_AUTOCROP
654     int i_time = p_vout->p_sys->i_time;
655     int i_diff = p_vout->p_sys->i_diff;
656
657     if (!p_vout->p_sys->i_ratio)
658     {
659         /* Determine where black borders are */
660         for( i = 0 ; i < i_lines ; i++)
661         {
662                    if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
663                             p_vout->p_sys->i_threshold,
664                             p_vout->p_sys->i_skipPercent,
665                             p_vout->p_sys->i_nonBlackPixel,
666                             p_vout->output.i_chroma))
667                 {
668                     i_firstwhite = i;
669                     i_lastwhite = i_lines - i;
670                     break;
671                 }
672                 p_in += i_pitch;
673         }
674
675         /* Decide whether it's worth changing the size */
676         if( i_lastwhite == -1 )
677         {
678             p_vout->p_sys->i_lastchange = 0;
679             return;
680         }
681
682         if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
683         {
684             p_vout->p_sys->i_lastchange = 0;
685             return;
686         }
687
688         if (p_vout->output.i_aspect
689                             * p_vout->output.i_height /
690                                 (i_lastwhite - i_firstwhite + 1)
691                             * p_vout->p_sys->i_width /
692                                p_vout->output.i_width >
693                                     p_vout->p_sys->i_ratio_max * 432)
694         {
695             int i_height = ((p_vout->output.i_aspect / 432) *
696                            p_vout->output.i_height * p_vout->p_sys->i_width) /
697                           (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
698             i_firstwhite = (p_vout->output.i_height - i_height) / 2;
699             i_lastwhite =  p_vout->output.i_height - i_firstwhite;
700 /*
701             p_vout->p_sys->i_lastchange = 0;
702             return;
703 */
704         }
705
706         if( (i_lastwhite - i_firstwhite) <
707                         (int) (p_vout->p_sys->i_height + i_diff)
708              && (i_lastwhite - i_firstwhite + i_diff) >
709                         (int) p_vout->p_sys->i_height )
710         {
711             p_vout->p_sys->i_lastchange = 0;
712             return;
713         }
714
715         /* We need at least 'i_time' images to make up our mind */
716         p_vout->p_sys->i_lastchange++;
717         if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
718         {
719             return;
720         }
721     }
722     else
723     {
724         if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
725         {
726             p_vout->p_sys->i_aspect    =  p_vout->p_sys->i_ratio * 432;
727             int i_height = p_vout->output.i_aspect
728                                     * p_vout->output.i_height /
729                                         p_vout->p_sys->i_aspect
730                                     * p_vout->p_sys->i_width /
731                                         p_vout->output.i_width;
732             i_firstwhite = (p_vout->output.i_height - i_height) / 2;
733             i_lastwhite =  p_vout->output.i_height - i_firstwhite;
734         }
735         else
736         {
737             return;
738         }
739     }
740
741 #else
742     /* Determine where black borders are */
743     switch( p_vout->output.i_chroma )
744     {
745     case VLC_FOURCC('I','4','2','0'):
746         /* XXX: Do not laugh ! I know this is very naive. But it's just a
747          *      proof of concept code snippet... */
748         for( i = i_lines ; i-- ; )
749         {
750             const int i_col = i * i_pitch / i_lines;
751
752             if( p_in[i_col/2] > 40
753                  && p_in[i_visible_pitch/2] > 40
754                  && p_in[i_visible_pitch/2 + i_col/2] > 40 )
755             {
756                 if( i_lastwhite == -1 )
757                 {
758                     i_lastwhite = i;
759                 }
760                 i_firstwhite = i;
761             }
762             p_in += i_pitch;
763         }
764         break;
765
766     default:
767         break;
768     }
769
770     /* Decide whether it's worth changing the size */
771     if( i_lastwhite == -1 )
772     {
773         p_vout->p_sys->i_lastchange = 0;
774         return;
775     }
776
777     if( (unsigned int)(i_lastwhite - i_firstwhite)
778                                            < p_vout->p_sys->i_height / 2 )
779     {
780         p_vout->p_sys->i_lastchange = 0;
781         return;
782     }
783
784     if( (unsigned int)(i_lastwhite - i_firstwhite)
785                                           < p_vout->p_sys->i_height + 16
786          && (unsigned int)(i_lastwhite - i_firstwhite + 16)
787                                                 > p_vout->p_sys->i_height )
788     {
789         p_vout->p_sys->i_lastchange = 0;
790         return;
791     }
792
793     /* We need at least 25 images to make up our mind */
794     p_vout->p_sys->i_lastchange++;
795     if( p_vout->p_sys->i_lastchange < 25 )
796     {
797         return;
798     }
799 #endif //BEST_AUTOCROP
800
801     /* Tune a few values */
802     if( i_firstwhite & 1 )
803     {
804         i_firstwhite--;
805     }
806
807     if( !(i_lastwhite & 1) )
808     {
809         i_lastwhite++;
810     }
811
812     /* Change size */
813     p_vout->p_sys->i_y = i_firstwhite;
814     p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
815 #ifdef BEST_AUTOCROP
816     // check p_vout->p_sys->i_height <= p_vout->output.i_height
817     if (p_vout->p_sys->i_height > p_vout->output.i_height)
818         p_vout->p_sys->i_height = p_vout->output.i_height;
819 #endif
820
821     p_vout->p_sys->i_aspect = p_vout->output.i_aspect
822                             * p_vout->output.i_height / p_vout->p_sys->i_height
823                             * p_vout->p_sys->i_width / p_vout->output.i_width;
824
825     p_vout->p_sys->b_changed = VLC_TRUE;
826 }
827
828 /*****************************************************************************
829  * SendEvents: forward mouse and keyboard events to the parent p_vout
830  *****************************************************************************/
831 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
832                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
833 {
834     vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
835     vlc_value_t sentval = newval;
836
837     /* Translate the mouse coordinates */
838     if( !strcmp( psz_var, "mouse-x" ) )
839     {
840         sentval.i_int += p_vout->p_sys->i_x;
841     }
842     else if( !strcmp( psz_var, "mouse-y" ) )
843     {
844         sentval.i_int += p_vout->p_sys->i_y;
845     }
846
847     var_Set( p_vout, psz_var, sentval );
848
849     return VLC_SUCCESS;
850 }
851
852 /*****************************************************************************
853  * SendEventsToChild: forward events to the child/children vout
854  *****************************************************************************/
855 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
856                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
857 {
858     vout_thread_t *p_vout = (vout_thread_t *)p_this;
859     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
860     return VLC_SUCCESS;
861 }
862
863 #ifdef BEST_AUTOCROP
864 /*****************************************************************************
865  * FilterCallback: called when changing the ratio on the fly.
866  *****************************************************************************/
867 static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
868                            vlc_value_t oldval, vlc_value_t newval,
869                            void *p_data )
870 {
871     vout_thread_t * p_vout = (vout_thread_t *)p_this;
872
873     if( !strcmp( psz_var, "ratio-crop" ) )
874     {
875         if ( !strcmp( newval.psz_string, "Auto" ) )
876             p_vout->p_sys->i_ratio = 0;
877         else
878         {
879             p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
880             p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
881             p_vout->p_sys->b_autocrop = VLC_TRUE;
882         }
883         if (p_vout->p_sys->i_ratio)
884         {
885             if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) /
886                                     p_vout->output.i_height)
887                 p_vout->p_sys->i_ratio = (p_vout->output.i_width * 1000) /
888                                     p_vout->output.i_height;
889             if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
890                 p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;
891         }
892      }
893     return VLC_SUCCESS;
894 }
895 #endif