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