FSF address change.
[vlc.git] / 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  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include "filter_common.h"
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Create    ( vlc_object_t * );
39 static void Destroy   ( vlc_object_t * );
40
41 static int  Init      ( vout_thread_t * );
42 static void End       ( vout_thread_t * );
43 static int  Manage    ( vout_thread_t * );
44 static void Render    ( vout_thread_t *, picture_t * );
45
46 static void UpdateStats    ( vout_thread_t *, picture_t * );
47
48 static int  SendEvents( vlc_object_t *, char const *,
49                         vlc_value_t, vlc_value_t, void * );
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 #define GEOMETRY_TEXT N_("Crop geometry (pixels)")
55 #define GEOMETRY_LONGTEXT N_("Set the geometry of the zone to crop. This is set as <width> x <height> + <left offset> + <top offset>.")
56
57 #define AUTOCROP_TEXT N_("Automatic cropping")
58 #define AUTOCROP_LONGTEXT N_("Activate automatic black border cropping.")
59
60 vlc_module_begin();
61     set_description( _("Crop video filter") );
62     set_shortname( N_("Crop" ));
63     set_category( CAT_VIDEO );
64     set_subcategory( SUBCAT_VIDEO_VFILTER );
65     set_capability( "video filter", 0 );
66
67     add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT, GEOMETRY_LONGTEXT, VLC_FALSE );
68     add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT, AUTOCROP_LONGTEXT, VLC_FALSE );
69
70     add_shortcut( "crop" );
71     set_callbacks( Create, Destroy );
72 vlc_module_end();
73
74 /*****************************************************************************
75  * vout_sys_t: Crop video output method descriptor
76  *****************************************************************************
77  * This structure is part of the video output thread descriptor.
78  * It describes the Crop specific properties of an output thread.
79  *****************************************************************************/
80 struct vout_sys_t
81 {
82     vout_thread_t *p_vout;
83
84     unsigned int i_x, i_y;
85     unsigned int i_width, i_height, i_aspect;
86
87     vlc_bool_t b_autocrop;
88
89     /* Autocrop specific variables */
90     unsigned int i_lastchange;
91     vlc_bool_t   b_changed;
92 };
93
94 /*****************************************************************************
95  * Control: control facility for the vout (forwards to child vout)
96  *****************************************************************************/
97 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
98 {
99     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
100 }
101
102 /*****************************************************************************
103  * Create: allocates Crop video thread output method
104  *****************************************************************************
105  * This function allocates and initializes a Crop vout method.
106  *****************************************************************************/
107 static int Create( vlc_object_t *p_this )
108 {
109     vout_thread_t *p_vout = (vout_thread_t *)p_this;
110
111     /* Allocate structure */
112     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
113     if( p_vout->p_sys == NULL )
114     {
115         msg_Err( p_vout, "out of memory" );
116         return VLC_ENOMEM;
117     }
118
119     p_vout->pf_init = Init;
120     p_vout->pf_end = End;
121     p_vout->pf_manage = Manage;
122     p_vout->pf_render = Render;
123     p_vout->pf_display = NULL;
124     p_vout->pf_control = Control;
125
126     return VLC_SUCCESS;
127 }
128
129 /*****************************************************************************
130  * Init: initialize Crop video thread output method
131  *****************************************************************************/
132 static int Init( vout_thread_t *p_vout )
133 {
134     int   i_index;
135     char *psz_var;
136     picture_t *p_pic;
137     video_format_t fmt = {0};
138
139     I_OUTPUTPICTURES = 0;
140
141     p_vout->p_sys->i_lastchange = 0;
142     p_vout->p_sys->b_changed = VLC_FALSE;
143
144     /* Initialize the output structure */
145     p_vout->output.i_chroma = p_vout->render.i_chroma;
146     p_vout->output.i_width  = p_vout->render.i_width;
147     p_vout->output.i_height = p_vout->render.i_height;
148     p_vout->output.i_aspect = p_vout->render.i_aspect;
149     p_vout->fmt_out = p_vout->fmt_in;
150
151     /* Shall we use autocrop ? */
152     p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
153
154     /* Get geometry value from the user */
155     psz_var = config_GetPsz( p_vout, "crop-geometry" );
156     if( psz_var )
157     {
158         char *psz_parser, *psz_tmp;
159
160         psz_parser = psz_tmp = psz_var;
161         while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
162
163         if( *psz_tmp )
164         {
165             psz_tmp[0] = '\0';
166             p_vout->p_sys->i_width = atoi( psz_parser );
167
168             psz_parser = ++psz_tmp;
169             while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
170
171             if( *psz_tmp )
172             {
173                 psz_tmp[0] = '\0';
174                 p_vout->p_sys->i_height = atoi( psz_parser );
175
176                 psz_parser = ++psz_tmp;
177                 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
178
179                 if( *psz_tmp )
180                 {
181                     psz_tmp[0] = '\0';
182                     p_vout->p_sys->i_x = atoi( psz_parser );
183                     p_vout->p_sys->i_y = atoi( ++psz_tmp );
184                 }
185                 else
186                 {
187                     p_vout->p_sys->i_x = atoi( psz_parser );
188                     p_vout->p_sys->i_y =
189                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
190                 }
191             }
192             else
193             {
194                 p_vout->p_sys->i_height = atoi( psz_parser );
195                 p_vout->p_sys->i_x =
196                      ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
197                 p_vout->p_sys->i_y =
198                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
199             }
200         }
201         else
202         {
203             p_vout->p_sys->i_width = atoi( psz_parser );
204             p_vout->p_sys->i_height = p_vout->output.i_height;
205             p_vout->p_sys->i_x =
206                      ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
207             p_vout->p_sys->i_y =
208                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
209         }
210
211         /* Check for validity */
212         if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
213                                                    > p_vout->output.i_width )
214         {
215             p_vout->p_sys->i_x = 0;
216             if( p_vout->p_sys->i_width > p_vout->output.i_width )
217             {
218                 p_vout->p_sys->i_width = p_vout->output.i_width;
219             }
220         }
221
222         if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
223                                                    > p_vout->output.i_height )
224         {
225             p_vout->p_sys->i_y = 0;
226             if( p_vout->p_sys->i_height > p_vout->output.i_height )
227             {
228                 p_vout->p_sys->i_height = p_vout->output.i_height;
229             }
230         }
231
232         free( psz_var );
233     }
234     else
235     {
236         p_vout->p_sys->i_width  = p_vout->fmt_out.i_visible_width;
237         p_vout->p_sys->i_height = p_vout->fmt_out.i_visible_height;
238         p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
239         p_vout->p_sys->i_y = p_vout->fmt_out.i_y_offset;
240     }
241
242     /* Pheeew. Parsing done. */
243     msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
244                      p_vout->p_sys->i_width, p_vout->p_sys->i_height,
245                      p_vout->p_sys->i_x, p_vout->p_sys->i_y,
246                      p_vout->p_sys->b_autocrop ? "" : "not " );
247
248     /* Set current output image properties */
249     p_vout->p_sys->i_aspect = p_vout->fmt_out.i_aspect
250            * p_vout->fmt_out.i_visible_height / p_vout->p_sys->i_height
251            * p_vout->p_sys->i_width / p_vout->fmt_out.i_visible_width;
252
253     fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
254     fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
255     fmt.i_x_offset = fmt.i_y_offset = 0;
256     fmt.i_chroma = p_vout->render.i_chroma;
257     fmt.i_aspect = p_vout->p_sys->i_aspect;
258     fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
259     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
260
261     /* Try to open the real video output */
262     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
263     if( p_vout->p_sys->p_vout == NULL )
264     {
265         msg_Err( p_vout, "failed to create vout" );
266         return VLC_EGENERIC;
267     }
268
269     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
270
271     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
272
273     ADD_PARENT_CALLBACKS( SendEventsToChild );
274
275     return VLC_SUCCESS;
276 }
277
278 /*****************************************************************************
279  * End: terminate Crop video thread output method
280  *****************************************************************************/
281 static void End( vout_thread_t *p_vout )
282 {
283     int i_index;
284
285     /* Free the fake output buffers we allocated */
286     for( i_index = I_OUTPUTPICTURES ; i_index ; )
287     {
288         i_index--;
289         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
290     }
291 }
292
293 /*****************************************************************************
294  * Destroy: destroy Crop video thread output method
295  *****************************************************************************
296  * Terminate an output method created by CropCreateOutputMethod
297  *****************************************************************************/
298 static void Destroy( vlc_object_t *p_this )
299 {
300     vout_thread_t *p_vout = (vout_thread_t *)p_this;
301
302     if( p_vout->p_sys->p_vout )
303     {
304         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
305         vlc_object_detach( p_vout->p_sys->p_vout );
306         vout_Destroy( p_vout->p_sys->p_vout );
307     }
308
309     DEL_PARENT_CALLBACKS( SendEventsToChild );
310
311     free( p_vout->p_sys );
312 }
313
314 /*****************************************************************************
315  * Manage: handle Crop events
316  *****************************************************************************
317  * This function should be called regularly by video output thread. It manages
318  * console events. It returns a non null value on error.
319  *****************************************************************************/
320 static int Manage( vout_thread_t *p_vout )
321 {
322     video_format_t fmt = {0};
323
324     if( !p_vout->p_sys->b_changed )
325     {
326         return VLC_SUCCESS;
327     }
328
329     vout_Destroy( p_vout->p_sys->p_vout );
330
331     fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
332     fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
333     fmt.i_x_offset = fmt.i_y_offset = 0;
334     fmt.i_chroma = p_vout->render.i_chroma;
335     fmt.i_aspect = p_vout->p_sys->i_aspect;
336     fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
337     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
338
339     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
340     if( p_vout->p_sys->p_vout == NULL )
341     {
342         msg_Err( p_vout, "failed to create vout" );
343         return VLC_EGENERIC;
344     }
345
346     p_vout->p_sys->b_changed = VLC_FALSE;
347     p_vout->p_sys->i_lastchange = 0;
348
349     return VLC_SUCCESS;
350 }
351
352 /*****************************************************************************
353  * Render: display previously rendered output
354  *****************************************************************************
355  * This function sends the currently rendered image to Crop image, waits
356  * until it is displayed and switches the two rendering buffers, preparing next
357  * frame.
358  *****************************************************************************/
359 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
360 {
361     picture_t *p_outpic = NULL;
362     int i_plane;
363
364     if( p_vout->p_sys->b_changed )
365     {
366         return;
367     }
368
369     while( ( p_outpic =
370                  vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
371            ) == NULL )
372     {
373         if( p_vout->b_die || p_vout->b_error )
374         {
375             vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
376             return;
377         }
378
379         msleep( VOUT_OUTMEM_SLEEP );
380     }
381
382     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
383     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
384
385     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
386     {
387         uint8_t *p_in, *p_out, *p_out_end;
388         int i_in_pitch = p_pic->p[i_plane].i_pitch;
389         const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
390         const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
391
392         p_in = p_pic->p[i_plane].p_pixels
393                 /* Skip the right amount of lines */
394                 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
395                                  p_vout->p_sys->i_y / p_vout->output.i_height )
396                 /* Skip the right amount of columns */
397                 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
398
399         p_out = p_outpic->p[i_plane].p_pixels;
400         p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
401
402         while( p_out < p_out_end )
403         {
404             p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
405             p_in += i_in_pitch;
406             p_out += i_out_pitch;
407         }
408     }
409
410     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
411     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
412
413     /* The source image may still be in the cache ... parse it! */
414     if( p_vout->p_sys->b_autocrop )
415     {
416         UpdateStats( p_vout, p_pic );
417     }
418 }
419
420 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
421 {
422     uint8_t *p_in = p_pic->p[0].p_pixels;
423     int i_pitch = p_pic->p[0].i_pitch;
424     int i_visible_pitch = p_pic->p[0].i_visible_pitch;
425     int i_lines = p_pic->p[0].i_visible_lines;
426     int i_firstwhite = -1, i_lastwhite = -1, i;
427
428     /* Determine where black borders are */
429     switch( p_vout->output.i_chroma )
430     {
431     case VLC_FOURCC('I','4','2','0'):
432         /* XXX: Do not laugh ! I know this is very naive. But it's just a
433          *      proof of concept code snippet... */
434         for( i = i_lines ; i-- ; )
435         {
436             const int i_col = i * i_pitch / i_lines;
437
438             if( p_in[i_col/2] > 40
439                  && p_in[i_visible_pitch/2] > 40
440                  && p_in[i_visible_pitch/2 + i_col/2] > 40 )
441             {
442                 if( i_lastwhite == -1 )
443                 {
444                     i_lastwhite = i;
445                 }
446                 i_firstwhite = i;
447             }
448             p_in += i_pitch;
449         }
450         break;
451
452     default:
453         break;
454     }
455
456     /* Decide whether it's worth changing the size */
457     if( i_lastwhite == -1 )
458     {
459         p_vout->p_sys->i_lastchange = 0;
460         return;
461     }
462
463     if( (unsigned int)(i_lastwhite - i_firstwhite)
464                                            < p_vout->p_sys->i_height / 2 )
465     {
466         p_vout->p_sys->i_lastchange = 0;
467         return;
468     }
469
470     if( (unsigned int)(i_lastwhite - i_firstwhite)
471                                           < p_vout->p_sys->i_height + 16
472          && (unsigned int)(i_lastwhite - i_firstwhite + 16)
473                                                 > p_vout->p_sys->i_height )
474     {
475         p_vout->p_sys->i_lastchange = 0;
476         return;
477     }
478
479     /* We need at least 25 images to make up our mind */
480     p_vout->p_sys->i_lastchange++;
481     if( p_vout->p_sys->i_lastchange < 25 )
482     {
483         return;
484     }
485
486     /* Tune a few values */
487     if( i_firstwhite & 1 )
488     {
489         i_firstwhite--;
490     }
491
492     if( !(i_lastwhite & 1) )
493     {
494         i_lastwhite++;
495     }
496
497     /* Change size */
498     p_vout->p_sys->i_y = i_firstwhite;
499     p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
500
501     p_vout->p_sys->i_aspect = p_vout->output.i_aspect
502                             * p_vout->output.i_height / p_vout->p_sys->i_height
503                             * p_vout->p_sys->i_width / p_vout->output.i_width;
504
505     p_vout->p_sys->b_changed = VLC_TRUE;
506 }
507
508 /*****************************************************************************
509  * SendEvents: forward mouse and keyboard events to the parent p_vout
510  *****************************************************************************/
511 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
512                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
513 {
514     vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
515     vlc_value_t sentval = newval;
516
517     /* Translate the mouse coordinates */
518     if( !strcmp( psz_var, "mouse-x" ) )
519     {
520         sentval.i_int += p_vout->p_sys->i_x;
521     }
522     else if( !strcmp( psz_var, "mouse-y" ) )
523     {
524         sentval.i_int += p_vout->p_sys->i_y;
525     }
526
527     var_Set( p_vout, psz_var, sentval );
528
529     return VLC_SUCCESS;
530 }
531
532 /*****************************************************************************
533  * SendEventsToChild: forward events to the child/children vout
534  *****************************************************************************/
535 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
536                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
537 {
538     vout_thread_t *p_vout = (vout_thread_t *)p_this;
539     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
540     return VLC_SUCCESS;
541 }