* ./modules/video_output/wingdi.c: the GDI video output now properly sets
[vlc.git] / 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.3 2002/11/23 02:40:30 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 1;
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 0;
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 = 0;
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 =
236         vout_CreateThread( p_vout,
237                     p_vout->p_sys->i_width, p_vout->p_sys->i_height,
238                     p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
239     if( p_vout->p_sys->p_vout == NULL )
240     {
241         msg_Err( p_vout, "failed to create vout" );
242         return 0;
243     }
244
245     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
246
247     return 0;
248 }
249
250 /*****************************************************************************
251  * End: terminate Crop video thread output method
252  *****************************************************************************/
253 static void End( vout_thread_t *p_vout )
254 {
255     int i_index;
256
257     /* Free the fake output buffers we allocated */
258     for( i_index = I_OUTPUTPICTURES ; i_index ; )
259     {
260         i_index--;
261         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
262     }
263 }
264
265 /*****************************************************************************
266  * Destroy: destroy Crop video thread output method
267  *****************************************************************************
268  * Terminate an output method created by CropCreateOutputMethod
269  *****************************************************************************/
270 static void Destroy( vlc_object_t *p_this )
271 {
272     vout_thread_t *p_vout = (vout_thread_t *)p_this;
273
274     vout_DestroyThread( p_vout->p_sys->p_vout );
275     free( p_vout->p_sys );
276 }
277
278 /*****************************************************************************
279  * Manage: handle Crop events
280  *****************************************************************************
281  * This function should be called regularly by video output thread. It manages
282  * console events. It returns a non null value on error.
283  *****************************************************************************/
284 static int Manage( vout_thread_t *p_vout )
285 {
286     if( !p_vout->p_sys->b_changed )
287     {
288         return 0;
289     }
290
291     vout_DestroyThread( p_vout->p_sys->p_vout );
292
293     p_vout->p_sys->p_vout =
294         vout_CreateThread( p_vout,
295                     p_vout->p_sys->i_width, p_vout->p_sys->i_height,
296                     p_vout->render.i_chroma, p_vout->p_sys->i_aspect );
297     if( p_vout->p_sys->p_vout == NULL )
298     {
299         msg_Err( p_vout, "failed to create vout" );
300         return 1;
301     }
302
303     p_vout->p_sys->b_changed = 0;
304     p_vout->p_sys->i_lastchange = 0;
305
306     return 0;
307 }
308
309 /*****************************************************************************
310  * Render: display previously rendered output
311  *****************************************************************************
312  * This function sends the currently rendered image to Crop image, waits
313  * until it is displayed and switches the two rendering buffers, preparing next
314  * frame.
315  *****************************************************************************/
316 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
317 {
318     picture_t *p_outpic = NULL;
319     int i_plane;
320
321     if( p_vout->p_sys->b_changed )
322     {
323         return;
324     }
325
326     while( ( p_outpic =
327                  vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
328            ) == NULL )
329     {
330         if( p_vout->b_die || p_vout->b_error )
331         {
332             vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
333             return;
334         }
335
336         msleep( VOUT_OUTMEM_SLEEP );
337     }
338
339     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
340     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
341
342     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
343     {
344         u8 *p_in, *p_out, *p_out_end;
345         int i_in_pitch = p_pic->p[i_plane].i_pitch;
346         const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
347
348         p_in = p_pic->p[i_plane].p_pixels
349                 /* Skip the right amount of lines */
350                 + i_in_pitch * ( p_pic->p[i_plane].i_lines * p_vout->p_sys->i_y
351                                   / p_vout->output.i_height )
352                 /* Skip the right amount of columns */
353                 + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
354
355         p_out = p_outpic->p[i_plane].p_pixels;
356         p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_lines;
357
358         while( p_out < p_out_end )
359         {
360             p_vout->p_vlc->pf_memcpy( p_out, p_in, i_out_pitch );
361             p_in += i_in_pitch;
362             p_out += i_out_pitch;
363         }
364     }
365
366     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
367     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
368
369     /* The source image may still be in the cache ... parse it! */
370     if( !p_vout->p_sys->b_autocrop )
371     {
372         return;
373     }
374
375     UpdateStats( p_vout, p_pic );
376 }
377
378 static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
379 {
380     u8 *p_in = p_pic->p[0].p_pixels;
381     int i_pitch = p_pic->p[0].i_pitch;
382     int i_lines = p_pic->p[0].i_lines;
383     int i_firstwhite = -1, i_lastwhite = -1, i;
384
385     /* Determine where black borders are */
386     switch( p_vout->output.i_chroma )
387     {
388     case VLC_FOURCC('I','4','2','0'):
389         /* XXX: Do not laugh ! I know this is very naive. But it's just a
390          *      proof of concept code snippet... */
391         for( i = i_lines ; i-- ; )
392         {
393             const int i_col = i * i_pitch / i_lines;
394
395             if( p_in[i_col/2] > 40
396                  && p_in[i_pitch / 2] > 40
397                  && p_in[i_pitch/2 + i_col/2] > 40 )
398             {
399                 if( i_lastwhite == -1 )
400                 {
401                     i_lastwhite = i;
402                 }
403                 i_firstwhite = i;
404             }
405             p_in += i_pitch;
406         }
407         break;
408
409     default:
410         break;
411     }
412
413     /* Decide whether it's worth changing the size */
414     if( i_lastwhite == -1 )
415     {
416         p_vout->p_sys->i_lastchange = 0;
417         return;
418     }
419
420     if( i_lastwhite - i_firstwhite < p_vout->p_sys->i_height / 2 )
421     {
422         p_vout->p_sys->i_lastchange = 0;
423         return;
424     }
425
426     if( i_lastwhite - i_firstwhite < p_vout->p_sys->i_height + 16
427          && i_lastwhite - i_firstwhite + 16 > p_vout->p_sys->i_height )
428     {
429         p_vout->p_sys->i_lastchange = 0;
430         return;
431     }
432
433     /* We need at least 25 images to make up our mind */
434     p_vout->p_sys->i_lastchange++;
435     if( p_vout->p_sys->i_lastchange < 25 )
436     {
437         return;
438     }
439
440     /* Tune a few values */
441     if( i_firstwhite & 1 )
442     {
443         i_firstwhite--;
444     }
445
446     if( !(i_lastwhite & 1) )
447     {
448         i_lastwhite++;
449     }
450
451     /* Change size */
452     p_vout->p_sys->i_y = i_firstwhite;
453     p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
454
455     p_vout->p_sys->i_aspect = p_vout->output.i_aspect
456                             * p_vout->output.i_height / p_vout->p_sys->i_height
457                             * p_vout->p_sys->i_width / p_vout->output.i_width;
458
459     p_vout->p_sys->b_changed = 1;
460 }