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