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