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