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