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