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