1 /*****************************************************************************
2 * erase.c : logo erase video filter
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <math.h> /* sin(), cos() */
35 #include "vlc_image.h"
37 #include "vlc_filter.h"
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Create ( vlc_object_t * );
43 static void Destroy ( vlc_object_t * );
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 * );
50 /*****************************************************************************
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.")
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.")
61 #define CFG_PREFIX "erase-"
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 );
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 );
75 add_shortcut( "erase" );
76 set_callbacks( Create, Destroy );
79 static const char *ppsz_filter_options[] = {
80 "mask", "x", "y", NULL
83 /*****************************************************************************
85 *****************************************************************************/
94 static void LoadMask( filter_t *p_filter, const char *psz_filename )
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 );
109 /*****************************************************************************
111 *****************************************************************************/
112 static int Create( vlc_object_t *p_this )
114 filter_t *p_filter = (filter_t *)p_this;
118 /* Allocate structure */
119 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
120 if( p_filter->p_sys == NULL )
122 msg_Err( p_filter, "out of memory" );
125 p_sys = p_filter->p_sys;
127 p_filter->pf_video_filter = Filter;
129 config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
133 var_CreateGetNonEmptyStringCommand( p_filter, CFG_PREFIX "mask" );
137 msg_Err( p_filter, "Missing 'mask' option value." );
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" );
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 );
151 vlc_mutex_init( p_filter, &p_sys->lock );
156 /*****************************************************************************
158 *****************************************************************************/
159 static void Destroy( vlc_object_t *p_this )
161 filter_t *p_filter = (filter_t *)p_this;
162 filter_sys_t *p_sys = p_filter->p_sys;
164 p_sys->p_mask->pf_release( p_sys->p_mask );
166 vlc_mutex_destroy( &p_sys->lock );
168 free( p_filter->p_sys );
171 /*****************************************************************************
173 *****************************************************************************/
174 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
178 if( !p_pic ) return NULL;
179 switch( p_pic->format.i_chroma )
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'):
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 );
194 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
197 msg_Warn( p_filter, "can't get output picture" );
198 if( p_pic->pf_release )
199 p_pic->pf_release( p_pic );
204 FilterErase( p_filter, p_pic, p_outpic );
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;
212 if( p_pic->pf_release )
213 p_pic->pf_release( p_pic );
218 /*****************************************************************************
220 *****************************************************************************/
221 static void FilterErase( filter_t *p_filter, picture_t *p_inpic,
222 picture_t *p_outpic )
224 filter_sys_t *p_sys = p_filter->p_sys;
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;
232 for( i_plane = 0; i_plane < p_inpic->i_planes; i_plane++ )
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;
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;
243 int i_x = p_sys->i_x, i_y = p_sys->i_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 */
254 i_height = __MIN( i_visible_lines - i_y, i_height );
255 i_width = __MIN( i_visible_pitch - i_x, i_width );
257 p_filter->p_libvlc->pf_memcpy( p_outpix, p_inpix, i_pitch * i_lines );
259 for( y = 0; y < i_height; y++, p_mask += i_mask_pitch )
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;
266 prev = *(p_outpix-1);
270 prev = *(p_outpix-i_pitch);
276 for( x = 0; x < i_width; x++ )
278 if( p_mask[i_plane?2*x:x] > 127 )
280 if( next_x <= prev_x )
283 for( x0 = x; x0 < i_width; x0++ )
285 if( p_mask[i_plane?2*x0:x0] <= 127 )
292 if( next_x <= prev_x )
295 if( x0 >= i_visible_pitch )
306 if( !( i_x || y || i_y ) )
309 /* interpolate new value */
310 p_outpix[x] = prev;// + (x-prev_x)*(next-prev)/(next_x-prev_x);
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 )
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++ )
330 if( p_mask[i_plane?2*x:x] > 127 )
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 */
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 )
348 filter_sys_t *p_sys = (filter_sys_t *)p_data;
350 if( !strcmp( psz_var, CFG_PREFIX "x" ) )
352 vlc_mutex_lock( &p_sys->lock );
353 p_sys->i_x = newval.i_int;
354 vlc_mutex_unlock( &p_sys->lock );
356 else if( !strcmp( psz_var, CFG_PREFIX "y" ) )
358 vlc_mutex_lock( &p_sys->lock );
359 p_sys->i_y = newval.i_int;
360 vlc_mutex_unlock( &p_sys->lock );
362 else if( !strcmp( psz_var, CFG_PREFIX "mask" ) )
364 vlc_mutex_lock( &p_sys->lock );
365 LoadMask( (filter_t*)p_this, newval.psz_string );
366 vlc_mutex_unlock( &p_sys->lock );
370 msg_Warn( p_this, "Unknown callback command." );