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