Check the input chroma. (We only want to work on I420 for the moment)
[vlc.git] / modules / video_filter / erase.c
1 /*****************************************************************************
2  * erase.c : logo erase video filter
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <math.h>                                            /* sin(), cos() */
31
32 #include <vlc/vlc.h>
33 #include <vlc_sout.h>
34 #include <vlc_vout.h>
35 #include "vlc_image.h"
36
37 #include "vlc_filter.h"
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  Create    ( vlc_object_t * );
43 static void Destroy   ( vlc_object_t * );
44
45 static picture_t *Filter( filter_t *, picture_t * );
46 static void FilterErase( filter_t *, picture_t *, picture_t * );
47 static int EraseCallback( vlc_object_t *, char const *,
48                           vlc_value_t, vlc_value_t, void * );
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 #define MASK_TEXT N_("Image mask")
54 #define MASK_LONGTEXT N_("Image mask. Pixels with an alpha value greater than 50% will be erased.")
55
56 #define POSX_TEXT N_("X coordinate")
57 #define POSX_LONGTEXT N_("X coordinate of the mask.")
58 #define POSY_TEXT N_("Y coordinate")
59 #define POSY_LONGTEXT N_("Y coordinate of the mask.")
60
61 #define CFG_PREFIX "erase-"
62
63 vlc_module_begin();
64     set_description( _("Erase video filter") );
65     set_shortname( _( "Erase" ));
66     set_capability( "video filter2", 0 );
67     set_category( CAT_VIDEO );
68     set_subcategory( SUBCAT_VIDEO_VFILTER );
69
70     add_file( CFG_PREFIX "mask", NULL, NULL,
71               MASK_TEXT, MASK_LONGTEXT, VLC_FALSE );
72     add_integer( CFG_PREFIX "x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
73     add_integer( CFG_PREFIX "y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
74
75     add_shortcut( "erase" );
76     set_callbacks( Create, Destroy );
77 vlc_module_end();
78
79 static const char *ppsz_filter_options[] = {
80     "mask", "x", "y", NULL
81 };
82
83 /*****************************************************************************
84  * filter_sys_t
85  *****************************************************************************/
86 struct filter_sys_t
87 {
88     int i_x;
89     int i_y;
90     picture_t *p_mask;
91     vlc_mutex_t lock;
92 };
93
94 static void LoadMask( filter_t *p_filter, const char *psz_filename )
95 {
96     image_handler_t *p_image;
97     video_format_t fmt_in = {0}, fmt_out = {0};
98     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
99     if( p_filter->p_sys->p_mask )
100         p_filter->p_sys->p_mask->pf_release( p_filter->p_sys->p_mask );
101     p_image = image_HandlerCreate( p_filter );
102     p_filter->p_sys->p_mask =
103         image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
104     image_HandlerDelete( p_image );
105 }
106
107 /*****************************************************************************
108  * Create
109  *****************************************************************************/
110 static int Create( vlc_object_t *p_this )
111 {
112     filter_t *p_filter = (filter_t *)p_this;
113     filter_sys_t *p_sys;
114     char *psz_filename;
115
116     /* Allocate structure */
117     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
118     if( p_filter->p_sys == NULL )
119     {
120         msg_Err( p_filter, "out of memory" );
121         return VLC_ENOMEM;
122     }
123     p_sys = p_filter->p_sys;
124
125     p_filter->pf_video_filter = Filter;
126
127     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
128                        p_filter->p_cfg );
129
130     psz_filename =
131         var_CreateGetNonEmptyStringCommand( p_filter, CFG_PREFIX "mask" );
132
133     if( !psz_filename )
134     {
135         msg_Err( p_filter, "Missing 'mask' option value." );
136         return VLC_EGENERIC;
137     }
138
139     p_sys->p_mask = NULL;
140     LoadMask( p_filter, psz_filename );
141     free( psz_filename );
142     p_sys->i_x = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "x" );
143     p_sys->i_y = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "y" );
144
145     var_AddCallback( p_filter, CFG_PREFIX "x", EraseCallback, p_sys );
146     var_AddCallback( p_filter, CFG_PREFIX "y", EraseCallback, p_sys );
147     var_AddCallback( p_filter, CFG_PREFIX "mask", EraseCallback, p_sys );
148
149     vlc_mutex_init( p_filter, &p_sys->lock );
150
151     return VLC_SUCCESS;
152 }
153
154 /*****************************************************************************
155  * Destroy
156  *****************************************************************************/
157 static void Destroy( vlc_object_t *p_this )
158 {
159     filter_t *p_filter = (filter_t *)p_this;
160     filter_sys_t *p_sys = p_filter->p_sys;
161     if( p_sys->p_mask )
162         p_sys->p_mask->pf_release( p_sys->p_mask );
163
164     vlc_mutex_destroy( &p_sys->lock );
165
166     free( p_filter->p_sys );
167 }
168
169 /*****************************************************************************
170  * Filter
171  *****************************************************************************/
172 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
173 {
174     picture_t *p_outpic;
175
176     if( !p_pic ) return NULL;
177     switch( p_pic->format.i_chroma )
178     {
179         case VLC_FOURCC('I','4','2','0'):
180         case VLC_FOURCC('I','Y','U','V'):
181         case VLC_FOURCC('J','4','2','0'):
182         case VLC_FOURCC('Y','V','1','2'):
183             break;
184         default:
185             msg_Warn( p_filter, "Unsupported input chroma (%4s)",
186                       (char*)&(p_pic->format.i_chroma) );
187             if( p_pic->pf_release )
188                 p_pic->pf_release( p_pic );
189             return NULL;
190     }
191
192     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
193     if( !p_outpic )
194     {
195         msg_Warn( p_filter, "can't get output picture" );
196         if( p_pic->pf_release )
197             p_pic->pf_release( p_pic );
198         return NULL;
199     }
200
201     /* Here */
202     FilterErase( p_filter, p_pic, p_outpic );
203
204     p_outpic->date = p_pic->date;
205     p_outpic->b_force = p_pic->b_force;
206     p_outpic->i_nb_fields = p_pic->i_nb_fields;
207     p_outpic->b_progressive = p_pic->b_progressive;
208     p_outpic->b_top_field_first = p_pic->b_top_field_first;
209
210     if( p_pic->pf_release )
211         p_pic->pf_release( p_pic );
212
213     return p_outpic;
214 }
215
216 /*****************************************************************************
217  * FilterErase
218  *****************************************************************************/
219 static void FilterErase( filter_t *p_filter, picture_t *p_inpic,
220                                              picture_t *p_outpic )
221 {
222     filter_sys_t *p_sys = p_filter->p_sys;
223
224     int i_plane;
225
226     const int i_mask_pitch = p_sys->p_mask->A_PITCH;
227     const int i_mask_visible_pitch = p_sys->p_mask->p[A_PLANE].i_visible_pitch;
228     const int i_mask_visible_lines = p_sys->p_mask->p[A_PLANE].i_visible_lines;
229
230     for( i_plane = 0; i_plane < p_inpic->i_planes; i_plane++ )
231     {
232         const int i_pitch = p_inpic->p[i_plane].i_pitch;
233         const int i_visible_pitch = p_inpic->p[i_plane].i_visible_pitch;
234         const int i_lines = p_inpic->p[i_plane].i_lines;
235         const int i_visible_lines = p_inpic->p[i_plane].i_visible_lines;
236
237         uint8_t *p_inpix = p_inpic->p[i_plane].p_pixels;
238         uint8_t *p_outpix = p_outpic->p[i_plane].p_pixels;
239         uint8_t *p_mask = p_sys->p_mask->A_PIXELS;
240
241         int x, y;
242         int i_height = i_mask_visible_lines;
243         int i_width = i_mask_visible_pitch;
244         if( i_plane )
245         {
246             i_height /= 2;
247             i_width /= 2;
248         }
249         i_height = __MIN( i_visible_lines - (i_plane?p_sys->i_y/2:p_sys->i_y), i_height );
250         i_width = __MIN( i_visible_pitch - (i_plane?p_sys->i_x/2:p_sys->i_x), i_width );
251
252         p_filter->p_libvlc->pf_memcpy( p_outpix, p_inpix, i_pitch * i_lines );
253
254         for( y = 0; y < i_height;
255              y++, p_mask += i_plane ? 2*i_mask_pitch : i_mask_pitch )
256         {
257             uint8_t prev, next;
258             int prev_x = 0, next_x = -1;
259             p_outpix = i_plane ?
260             p_outpic->p[i_plane].p_pixels
261                        + (p_sys->i_y/2+y)*i_pitch + (p_sys->i_x/2)
262             :
263             p_outpic->p[i_plane].p_pixels
264                        + (p_sys->i_y+y)*i_pitch + p_sys->i_x;
265             if( p_sys->i_x )
266             {
267                 prev = *(p_outpix-1);
268             }
269             else if( y || p_sys->i_y )
270             {
271                 prev = *(p_outpix-i_pitch);
272             }
273             else
274             {
275                 prev = 0xff;
276             }
277             for( x = 0; x < i_width; x++ )
278             {
279                 if( p_mask[i_plane?2*x:x] > 127 )
280                 {
281                     if( next_x <= prev_x )
282                     {
283                         int x0;
284                         for( x0 = x; x0 < i_width; x0++ )
285                         {
286                             if( p_mask[i_plane?2*x0:x0] <= 127 )
287                             {
288                                 next_x = x0;
289                                 next = p_outpix[x0];
290                                 break;
291                             }
292                         }
293                         if( next_x <= prev_x )
294                         {
295                             if( x0 == x ) x0++;
296                             if( (i_plane?p_sys->i_x/2:p_sys->i_x)+i_width >= i_visible_pitch )
297                             {
298                                 next_x = x0;
299                                 next = prev;
300                             }
301                             else
302                             {
303                                 next_x = x0;
304                                 next = p_outpix[x0];
305                             }
306                         }
307                     }
308                     /* interpolate new value */
309                     p_outpix[x] = prev + (x-prev_x)*(next-prev)/(next_x-prev_x);
310                 }
311                 else
312                 {
313                     prev = p_outpix[x];
314                     prev_x = x;
315                 }
316             }
317         }
318     }
319 }
320
321 static int EraseCallback( vlc_object_t *p_this, char const *psz_var,
322                           vlc_value_t oldval, vlc_value_t newval, void *p_data )
323 {
324     filter_sys_t *p_sys = (filter_sys_t *)p_data;
325
326     if( !strcmp( psz_var, CFG_PREFIX "x" ) )
327     {
328         vlc_mutex_lock( &p_sys->lock );
329         p_sys->i_x = newval.i_int;
330         vlc_mutex_unlock( &p_sys->lock );
331     }
332     else if( !strcmp( psz_var, CFG_PREFIX "y" ) )
333     {
334         vlc_mutex_lock( &p_sys->lock );
335         p_sys->i_y = newval.i_int;
336         vlc_mutex_unlock( &p_sys->lock );
337     }
338     else if( !strcmp( psz_var, CFG_PREFIX "mask" ) )
339     {
340         vlc_mutex_lock( &p_sys->lock );
341         LoadMask( (filter_t*)p_this, newval.psz_string );
342         vlc_mutex_unlock( &p_sys->lock );
343     }
344     else
345     {
346         msg_Warn( p_this, "Unknown callback command." );
347     }
348     return VLC_SUCCESS;
349 }