]> git.sesse.net Git - vlc/blob - modules/video_filter/erase.c
Oops.
[vlc] / 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, fmt_out;
98     memset( &fmt_in, 0, sizeof( video_format_t ) );
99     memset( &fmt_out, 0, sizeof( video_format_t ) );
100     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
101     if( p_filter->p_sys->p_mask )
102         p_filter->p_sys->p_mask->pf_release( p_filter->p_sys->p_mask );
103     p_image = image_HandlerCreate( p_filter );
104     p_filter->p_sys->p_mask =
105         image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
106     image_HandlerDelete( p_image );
107 }
108
109 /*****************************************************************************
110  * Create
111  *****************************************************************************/
112 static int Create( vlc_object_t *p_this )
113 {
114     filter_t *p_filter = (filter_t *)p_this;
115     filter_sys_t *p_sys;
116     char *psz_filename;
117
118     /* Allocate structure */
119     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
120     if( p_filter->p_sys == NULL )
121     {
122         msg_Err( p_filter, "out of memory" );
123         return VLC_ENOMEM;
124     }
125     p_sys = p_filter->p_sys;
126
127     p_filter->pf_video_filter = Filter;
128
129     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
130                        p_filter->p_cfg );
131
132     psz_filename =
133         var_CreateGetNonEmptyStringCommand( p_filter, CFG_PREFIX "mask" );
134
135     if( !psz_filename )
136     {
137         msg_Err( p_filter, "Missing 'mask' option value." );
138         return VLC_EGENERIC;
139     }
140
141     p_sys->p_mask = NULL;
142     LoadMask( p_filter, psz_filename );
143     free( psz_filename );
144     p_sys->i_x = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "x" );
145     p_sys->i_y = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "y" );
146
147     var_AddCallback( p_filter, CFG_PREFIX "x", EraseCallback, p_sys );
148     var_AddCallback( p_filter, CFG_PREFIX "y", EraseCallback, p_sys );
149     var_AddCallback( p_filter, CFG_PREFIX "mask", EraseCallback, p_sys );
150
151     vlc_mutex_init( p_filter, &p_sys->lock );
152
153     return VLC_SUCCESS;
154 }
155
156 /*****************************************************************************
157  * Destroy
158  *****************************************************************************/
159 static void Destroy( vlc_object_t *p_this )
160 {
161     filter_t *p_filter = (filter_t *)p_this;
162     filter_sys_t *p_sys = p_filter->p_sys;
163     if( p_sys->p_mask )
164         p_sys->p_mask->pf_release( p_sys->p_mask );
165
166     vlc_mutex_destroy( &p_sys->lock );
167
168     free( p_filter->p_sys );
169 }
170
171 /*****************************************************************************
172  * Filter
173  *****************************************************************************/
174 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
175 {
176     picture_t *p_outpic;
177
178     if( !p_pic ) return NULL;
179     switch( p_pic->format.i_chroma )
180     {
181         case VLC_FOURCC('I','4','2','0'):
182         case VLC_FOURCC('I','Y','U','V'):
183         case VLC_FOURCC('J','4','2','0'):
184         case VLC_FOURCC('Y','V','1','2'):
185             break;
186         default:
187             msg_Warn( p_filter, "Unsupported input chroma (%4s)",
188                       (char*)&(p_pic->format.i_chroma) );
189             if( p_pic->pf_release )
190                 p_pic->pf_release( p_pic );
191             return NULL;
192     }
193
194     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
195     if( !p_outpic )
196     {
197         msg_Warn( p_filter, "can't get output picture" );
198         if( p_pic->pf_release )
199             p_pic->pf_release( p_pic );
200         return NULL;
201     }
202
203     /* Here */
204     FilterErase( p_filter, p_pic, p_outpic );
205
206     p_outpic->date = p_pic->date;
207     p_outpic->b_force = p_pic->b_force;
208     p_outpic->i_nb_fields = p_pic->i_nb_fields;
209     p_outpic->b_progressive = p_pic->b_progressive;
210     p_outpic->b_top_field_first = p_pic->b_top_field_first;
211
212     if( p_pic->pf_release )
213         p_pic->pf_release( p_pic );
214
215     return p_outpic;
216 }
217
218 /*****************************************************************************
219  * FilterErase
220  *****************************************************************************/
221 static void FilterErase( filter_t *p_filter, picture_t *p_inpic,
222                                              picture_t *p_outpic )
223 {
224     filter_sys_t *p_sys = p_filter->p_sys;
225
226     int i_plane;
227
228     const int i_mask_pitch = p_sys->p_mask->A_PITCH;
229     const int i_mask_visible_pitch = p_sys->p_mask->p[A_PLANE].i_visible_pitch;
230     const int i_mask_visible_lines = p_sys->p_mask->p[A_PLANE].i_visible_lines;
231
232     for( i_plane = 0; i_plane < p_inpic->i_planes; i_plane++ )
233     {
234         const int i_pitch = p_inpic->p[i_plane].i_pitch;
235         const int i_visible_pitch = p_inpic->p[i_plane].i_visible_pitch;
236         const int i_lines = p_inpic->p[i_plane].i_lines;
237         const int i_visible_lines = p_inpic->p[i_plane].i_visible_lines;
238
239         uint8_t *p_inpix = p_inpic->p[i_plane].p_pixels;
240         uint8_t *p_outpix = p_outpic->p[i_plane].p_pixels;
241         uint8_t *p_mask = p_sys->p_mask->A_PIXELS;
242
243         int i_x = p_sys->i_x, i_y = p_sys->i_y;
244         int x, y;
245         int i_height = i_mask_visible_lines;
246         int i_width = i_mask_visible_pitch;
247         if( i_plane ) /* U_PLANE or V_PLANE */
248         {
249             i_width       /= 2;
250             i_height      /= 2;
251             i_x           /= 2;
252             i_y           /= 2;
253         }
254         i_height = __MIN( i_visible_lines - i_y, i_height );
255         i_width  = __MIN( i_visible_pitch - i_x, i_width  );
256
257         p_filter->p_libvlc->pf_memcpy( p_outpix, p_inpix, i_pitch * i_lines );
258
259         for( y = 0; y < i_height; y++, p_mask += i_mask_pitch )
260         {
261             uint8_t prev, next = 0;
262             int prev_x = -1, next_x = -2;
263             p_outpix = p_outpic->p[i_plane].p_pixels + (i_y+y)*i_pitch + i_x;
264             if( i_x )
265             {
266                 prev = *(p_outpix-1);
267             }
268             else if( y || i_y )
269             {
270                 prev = *(p_outpix-i_pitch);
271             }
272             else
273             {
274                 prev = 0xff;
275             }
276             for( x = 0; x < i_width; x++ )
277             {
278                 if( p_mask[i_plane?2*x:x] > 127 )
279                 {
280                     if( next_x <= prev_x )
281                     {
282                         int x0;
283                         for( x0 = x; x0 < i_width; x0++ )
284                         {
285                             if( p_mask[i_plane?2*x0:x0] <= 127 )
286                             {
287                                 next_x = x0;
288                                 next = p_outpix[x0];
289                                 break;
290                             }
291                         }
292                         if( next_x <= prev_x )
293                         {
294                             if( x0 == x ) x0++;
295                             if( x0 >= i_visible_pitch )
296                             {
297                                 next_x = x0;
298                                 next = prev;
299                             }
300                             else
301                             {
302                                 next_x = x0;
303                                 next = p_outpix[x0];
304                             }
305                         }
306                         if( !( i_x || y || i_y ) )
307                             prev = next;
308                     }
309                     /* interpolate new value */
310                     p_outpix[x] = prev + (x-prev_x)*(next-prev)/(next_x-prev_x);
311                 }
312                 else
313                 {
314                     prev = p_outpix[x];
315                     prev_x = x;
316                 }
317             }
318         }
319
320         /* Vertical bluring */
321         p_mask = p_sys->p_mask->A_PIXELS;
322         i_height = i_mask_visible_lines / (i_plane?2:1);
323         i_height = __MIN( i_visible_lines - i_y - 2, i_height );
324         for( y = __MAX(i_y-2,0); y < i_height;
325              y++, p_mask += i_mask_pitch )
326         {
327             p_outpix = p_outpic->p[i_plane].p_pixels + (i_y+y)*i_pitch + i_x;
328             for( x = 0; x < i_width; x++ )
329             {
330                 if( p_mask[i_plane?2*x:x] > 127 )
331                 {
332                     p_outpix[x] =
333                         ( (p_outpix[x-2*i_pitch]<<1)       /* 2 */
334                         + (p_outpix[x-i_pitch]<<2)         /* 4 */
335                         + (p_outpix[x]<<2)                 /* 4 */
336                         + (p_outpix[x+i_pitch]<<2)         /* 4 */
337                         + (p_outpix[x+2*i_pitch]<<1) )>>4; /* 2 */
338                 }
339             }
340         }
341
342     }
343 }
344
345 static int EraseCallback( vlc_object_t *p_this, char const *psz_var,
346                           vlc_value_t oldval, vlc_value_t newval, void *p_data )
347 {
348     filter_sys_t *p_sys = (filter_sys_t *)p_data;
349
350     if( !strcmp( psz_var, CFG_PREFIX "x" ) )
351     {
352         vlc_mutex_lock( &p_sys->lock );
353         p_sys->i_x = newval.i_int;
354         vlc_mutex_unlock( &p_sys->lock );
355     }
356     else if( !strcmp( psz_var, CFG_PREFIX "y" ) )
357     {
358         vlc_mutex_lock( &p_sys->lock );
359         p_sys->i_y = newval.i_int;
360         vlc_mutex_unlock( &p_sys->lock );
361     }
362     else if( !strcmp( psz_var, CFG_PREFIX "mask" ) )
363     {
364         vlc_mutex_lock( &p_sys->lock );
365         LoadMask( (filter_t*)p_this, newval.psz_string );
366         vlc_mutex_unlock( &p_sys->lock );
367     }
368     else
369     {
370         msg_Warn( p_this, "Unknown callback command." );
371     }
372     return VLC_SUCCESS;
373 }