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