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