Copyright fixes
[vlc.git] / modules / video_filter / crop.c
1 /*****************************************************************************
2  * crop.c : Crop video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2002, 2003 VideoLAN (Centrale Réseaux) and its contributors
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., 59 Temple Place - Suite 330, Boston, MA  02111, 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
150     /* Shall we use autocrop ? */
151     p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
152
153     /* Get geometry value from the user */
154     psz_var = config_GetPsz( p_vout, "crop-geometry" );
155     if( psz_var )
156     {
157         char *psz_parser, *psz_tmp;
158
159         psz_parser = psz_tmp = psz_var;
160         while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
161
162         if( *psz_tmp )
163         {
164             psz_tmp[0] = '\0';
165             p_vout->p_sys->i_width = atoi( psz_parser );
166
167             psz_parser = ++psz_tmp;
168             while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
169
170             if( *psz_tmp )
171             {
172                 psz_tmp[0] = '\0';
173                 p_vout->p_sys->i_height = atoi( psz_parser );
174
175                 psz_parser = ++psz_tmp;
176                 while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
177
178                 if( *psz_tmp )
179                 {
180                     psz_tmp[0] = '\0';
181                     p_vout->p_sys->i_x = atoi( psz_parser );
182                     p_vout->p_sys->i_y = atoi( ++psz_tmp );
183                 }
184                 else
185                 {
186                     p_vout->p_sys->i_x = atoi( psz_parser );
187                     p_vout->p_sys->i_y =
188                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
189                 }
190             }
191             else
192             {
193                 p_vout->p_sys->i_height = atoi( psz_parser );
194                 p_vout->p_sys->i_x =
195                      ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
196                 p_vout->p_sys->i_y =
197                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
198             }
199         }
200         else
201         {
202             p_vout->p_sys->i_width = atoi( psz_parser );
203             p_vout->p_sys->i_height = p_vout->output.i_height;
204             p_vout->p_sys->i_x =
205                      ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
206             p_vout->p_sys->i_y =
207                      ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
208         }
209
210         /* Check for validity */
211         if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
212                                                    > p_vout->output.i_width )
213         {
214             p_vout->p_sys->i_x = 0;
215             if( p_vout->p_sys->i_width > p_vout->output.i_width )
216             {
217                 p_vout->p_sys->i_width = p_vout->output.i_width;
218             }
219         }
220
221         if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
222                                                    > p_vout->output.i_height )
223         {
224             p_vout->p_sys->i_y = 0;
225             if( p_vout->p_sys->i_height > p_vout->output.i_height )
226             {
227                 p_vout->p_sys->i_height = p_vout->output.i_height;
228             }
229         }
230
231         free( psz_var );
232     }
233     else
234     {
235         p_vout->p_sys->i_width  = p_vout->output.i_width;
236         p_vout->p_sys->i_height = p_vout->output.i_height;
237         p_vout->p_sys->i_x = p_vout->p_sys->i_y = 0;
238     }
239
240     /* Pheeew. Parsing done. */
241     msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
242                      p_vout->p_sys->i_width, p_vout->p_sys->i_height,
243                      p_vout->p_sys->i_x, p_vout->p_sys->i_y,
244                      p_vout->p_sys->b_autocrop ? "" : "not " );
245
246     /* Set current output image properties */
247     p_vout->p_sys->i_aspect = p_vout->output.i_aspect
248                             * p_vout->output.i_height / p_vout->p_sys->i_height
249                             * p_vout->p_sys->i_width / p_vout->output.i_width;
250
251     fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
252     fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
253     fmt.i_x_offset = fmt.i_y_offset = 0;
254     fmt.i_chroma = p_vout->render.i_chroma;
255     fmt.i_aspect = p_vout->p_sys->i_aspect;
256     fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
257     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
258
259     /* Try to open the real video output */
260     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
261     if( p_vout->p_sys->p_vout == NULL )
262     {
263         msg_Err( p_vout, "failed to create vout" );
264         return VLC_EGENERIC;
265     }
266
267     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
268
269     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
270
271     ADD_PARENT_CALLBACKS( SendEventsToChild );
272
273     return VLC_SUCCESS;
274 }
275
276 /*****************************************************************************
277  * End: terminate Crop video thread output method
278  *****************************************************************************/
279 static void End( vout_thread_t *p_vout )
280 {
281     int i_index;
282
283     /* Free the fake output buffers we allocated */
284     for( i_index = I_OUTPUTPICTURES ; i_index ; )
285     {
286         i_index--;
287         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
288     }
289 }
290
291 /*****************************************************************************
292  * Destroy: destroy Crop video thread output method
293  *****************************************************************************
294  * Terminate an output method created by CropCreateOutputMethod
295  *****************************************************************************/
296 static void Destroy( vlc_object_t *p_this )
297 {
298     vout_thread_t *p_vout = (vout_thread_t *)p_this;
299
300     if( p_vout->p_sys->p_vout )
301     {
302         DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
303         vlc_object_detach( p_vout->p_sys->p_vout );
304         vout_Destroy( p_vout->p_sys->p_vout );
305     }
306
307     DEL_PARENT_CALLBACKS( SendEventsToChild );
308
309     free( p_vout->p_sys );
310 }
311
312 /*****************************************************************************
313  * Manage: handle Crop events
314  *****************************************************************************
315  * This function should be called regularly by video output thread. It manages
316  * console events. It returns a non null value on error.
317  *****************************************************************************/
318 static int Manage( vout_thread_t *p_vout )
319 {
320     video_format_t fmt = {0};
321
322     if( !p_vout->p_sys->b_changed )
323     {
324         return VLC_SUCCESS;
325     }
326
327     vout_Destroy( p_vout->p_sys->p_vout );
328
329     fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
330     fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
331     fmt.i_x_offset = fmt.i_y_offset = 0;
332     fmt.i_chroma = p_vout->render.i_chroma;
333     fmt.i_aspect = p_vout->p_sys->i_aspect;
334     fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
335     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
336
337     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
338     if( p_vout->p_sys->p_vout == NULL )
339     {
340         msg_Err( p_vout, "failed to create vout" );
341         return VLC_EGENERIC;
342     }
343
344     p_vout->p_sys->b_changed = VLC_FALSE;
345     p_vout->p_sys->i_lastchange = 0;
346
347     return VLC_SUCCESS;
348 }
349
350 /*****************************************************************************
351  * Render: display previously rendered output
352  *****************************************************************************
353  * This function sends the currently rendered image to Crop image, waits
354  * until it is displayed and switches the two rendering buffers, preparing next
355  * frame.
356  *****************************************************************************/
357 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
358 {
359     picture_t *p_outpic = NULL;
360     int i_plane;
361
362     if( p_vout->p_sys->b_changed )
363     {
364         return;
365     }
366
367     while( ( p_outpic =
368                  vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
369            ) == NULL )
370     {
371         if( p_vout->b_die || p_vout->b_error )
372         {
373             vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
374             return;
375         }
376
377         msleep( VOUT_OUTMEM_SLEEP );
378     }
379
380     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
381     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
382
383     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
384     {
385         uint8_t *p_in, *p_out, *p_out_end;
386         int i_in_pitch = p_pic->p[i_plane].i_pitch;
387         const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
388         const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
389
390         p_in = p_pic->p[i_plane].p_pixels
391                 /* Skip the right amount of lines */
392                 + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
393                                  p_vout->p_sys->i_y / p_vout->output.i_height )
394                 /* Skip the right amount of columns */
395                 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
396
397         p_out = p_outpic->p[i_plane].p_pixels;
398         p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
399
400         while( p_out < p_out_end )
401         {
402             p_vout->p_vlc->pf_memcpy( p_out, p_in, i_copy_pitch );
403             p_in += i_in_pitch;
404             p_out += i_out_pitch;
405         }
406     }
407
408     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
409     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
410
411     /* The source image may still be in the cache ... parse it! */
412     if( p_vout->p_sys->b_autocrop )
413     {
414         UpdateStats( p_vout, p_pic );
415     }
416 }
417
418 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
419 {
420     uint8_t *p_in = p_pic->p[0].p_pixels;
421     int i_pitch = p_pic->p[0].i_pitch;
422     int i_visible_pitch = p_pic->p[0].i_visible_pitch;
423     int i_lines = p_pic->p[0].i_visible_lines;
424     int i_firstwhite = -1, i_lastwhite = -1, i;
425
426     /* Determine where black borders are */
427     switch( p_vout->output.i_chroma )
428     {
429     case VLC_FOURCC('I','4','2','0'):
430         /* XXX: Do not laugh ! I know this is very naive. But it's just a
431          *      proof of concept code snippet... */
432         for( i = i_lines ; i-- ; )
433         {
434             const int i_col = i * i_pitch / i_lines;
435
436             if( p_in[i_col/2] > 40
437                  && p_in[i_visible_pitch/2] > 40
438                  && p_in[i_visible_pitch/2 + i_col/2] > 40 )
439             {
440                 if( i_lastwhite == -1 )
441                 {
442                     i_lastwhite = i;
443                 }
444                 i_firstwhite = i;
445             }
446             p_in += i_pitch;
447         }
448         break;
449
450     default:
451         break;
452     }
453
454     /* Decide whether it's worth changing the size */
455     if( i_lastwhite == -1 )
456     {
457         p_vout->p_sys->i_lastchange = 0;
458         return;
459     }
460
461     if( (unsigned int)(i_lastwhite - i_firstwhite)
462                                            < p_vout->p_sys->i_height / 2 )
463     {
464         p_vout->p_sys->i_lastchange = 0;
465         return;
466     }
467
468     if( (unsigned int)(i_lastwhite - i_firstwhite)
469                                           < p_vout->p_sys->i_height + 16
470          && (unsigned int)(i_lastwhite - i_firstwhite + 16)
471                                                 > p_vout->p_sys->i_height )
472     {
473         p_vout->p_sys->i_lastchange = 0;
474         return;
475     }
476
477     /* We need at least 25 images to make up our mind */
478     p_vout->p_sys->i_lastchange++;
479     if( p_vout->p_sys->i_lastchange < 25 )
480     {
481         return;
482     }
483
484     /* Tune a few values */
485     if( i_firstwhite & 1 )
486     {
487         i_firstwhite--;
488     }
489
490     if( !(i_lastwhite & 1) )
491     {
492         i_lastwhite++;
493     }
494
495     /* Change size */
496     p_vout->p_sys->i_y = i_firstwhite;
497     p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
498
499     p_vout->p_sys->i_aspect = p_vout->output.i_aspect
500                             * p_vout->output.i_height / p_vout->p_sys->i_height
501                             * p_vout->p_sys->i_width / p_vout->output.i_width;
502
503     p_vout->p_sys->b_changed = VLC_TRUE;
504 }
505
506 /*****************************************************************************
507  * SendEvents: forward mouse and keyboard events to the parent p_vout
508  *****************************************************************************/
509 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
510                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
511 {
512     vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
513     vlc_value_t sentval = newval;
514
515     /* Translate the mouse coordinates */
516     if( !strcmp( psz_var, "mouse-x" ) )
517     {
518         sentval.i_int += p_vout->p_sys->i_x;
519     }
520     else if( !strcmp( psz_var, "mouse-y" ) )
521     {
522         sentval.i_int += p_vout->p_sys->i_y;
523     }
524
525     var_Set( p_vout, psz_var, sentval );
526
527     return VLC_SUCCESS;
528 }
529
530 /*****************************************************************************
531  * SendEventsToChild: forward events to the child/children vout
532  *****************************************************************************/
533 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
534                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
535 {
536     vout_thread_t *p_vout = (vout_thread_t *)p_this;
537     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
538     return VLC_SUCCESS;
539 }