]> git.sesse.net Git - vlc/blob - modules/video_filter/erase.c
Remove stdlib.h
[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 <string.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc_sout.h>
31 #include <vlc_vout.h>
32 #include "vlc_image.h"
33
34 #include "vlc_filter.h"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Create    ( vlc_object_t * );
40 static void Destroy   ( vlc_object_t * );
41
42 static picture_t *Filter( filter_t *, picture_t * );
43 static void FilterErase( filter_t *, picture_t *, picture_t * );
44 static int EraseCallback( vlc_object_t *, char const *,
45                           vlc_value_t, vlc_value_t, void * );
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 #define MASK_TEXT N_("Image mask")
51 #define MASK_LONGTEXT N_("Image mask. Pixels with an alpha value greater than 50% will be erased.")
52
53 #define POSX_TEXT N_("X coordinate")
54 #define POSX_LONGTEXT N_("X coordinate of the mask.")
55 #define POSY_TEXT N_("Y coordinate")
56 #define POSY_LONGTEXT N_("Y coordinate of the mask.")
57
58 #define CFG_PREFIX "erase-"
59
60 vlc_module_begin();
61     set_description( _("Erase video filter") );
62     set_shortname( _( "Erase" ));
63     set_capability( "video filter2", 0 );
64     set_category( CAT_VIDEO );
65     set_subcategory( SUBCAT_VIDEO_VFILTER );
66
67     add_file( CFG_PREFIX "mask", NULL, NULL,
68               MASK_TEXT, MASK_LONGTEXT, VLC_FALSE );
69     add_integer( CFG_PREFIX "x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
70     add_integer( CFG_PREFIX "y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
71
72     add_shortcut( "erase" );
73     set_callbacks( Create, Destroy );
74 vlc_module_end();
75
76 static const char *ppsz_filter_options[] = {
77     "mask", "x", "y", NULL
78 };
79
80 /*****************************************************************************
81  * filter_sys_t
82  *****************************************************************************/
83 struct filter_sys_t
84 {
85     int i_x;
86     int i_y;
87     picture_t *p_mask;
88     vlc_mutex_t lock;
89 };
90
91 static void LoadMask( filter_t *p_filter, const char *psz_filename )
92 {
93     image_handler_t *p_image;
94     video_format_t fmt_in, fmt_out;
95     picture_t *p_old_mask = p_filter->p_sys->p_mask;
96     memset( &fmt_in, 0, sizeof( video_format_t ) );
97     memset( &fmt_out, 0, sizeof( video_format_t ) );
98     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
99     p_image = image_HandlerCreate( p_filter );
100     p_filter->p_sys->p_mask =
101         image_ReadUrl( p_image, psz_filename, &fmt_in, &fmt_out );
102     if( p_filter->p_sys->p_mask )
103     {
104         if( p_old_mask )
105             p_old_mask->pf_release( p_old_mask );
106     }
107     else if( p_old_mask )
108     {
109         p_filter->p_sys->p_mask = p_old_mask;
110         msg_Err( p_filter, "Error while loading new mask. Keeping old mask." );
111     }
112     image_HandlerDelete( p_image );
113 }
114
115 /*****************************************************************************
116  * Create
117  *****************************************************************************/
118 static int Create( vlc_object_t *p_this )
119 {
120     filter_t *p_filter = (filter_t *)p_this;
121     filter_sys_t *p_sys;
122     char *psz_filename;
123
124     /* Allocate structure */
125     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
126     if( p_filter->p_sys == NULL )
127     {
128         msg_Err( p_filter, "out of memory" );
129         return VLC_ENOMEM;
130     }
131     p_sys = p_filter->p_sys;
132
133     p_filter->pf_video_filter = Filter;
134
135     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
136                        p_filter->p_cfg );
137
138     psz_filename =
139         var_CreateGetNonEmptyStringCommand( p_filter, CFG_PREFIX "mask" );
140
141     if( !psz_filename )
142     {
143         msg_Err( p_filter, "Missing 'mask' option value." );
144         return VLC_EGENERIC;
145     }
146
147     p_sys->p_mask = NULL;
148     LoadMask( p_filter, psz_filename );
149     free( psz_filename );
150     p_sys->i_x = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "x" );
151     p_sys->i_y = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "y" );
152
153     var_AddCallback( p_filter, CFG_PREFIX "x", EraseCallback, p_sys );
154     var_AddCallback( p_filter, CFG_PREFIX "y", EraseCallback, p_sys );
155     var_AddCallback( p_filter, CFG_PREFIX "mask", EraseCallback, p_sys );
156
157     vlc_mutex_init( p_filter, &p_sys->lock );
158
159     return VLC_SUCCESS;
160 }
161
162 /*****************************************************************************
163  * Destroy
164  *****************************************************************************/
165 static void Destroy( vlc_object_t *p_this )
166 {
167     filter_t *p_filter = (filter_t *)p_this;
168     filter_sys_t *p_sys = p_filter->p_sys;
169     if( p_sys->p_mask )
170         p_sys->p_mask->pf_release( p_sys->p_mask );
171
172     vlc_mutex_destroy( &p_sys->lock );
173
174     free( p_filter->p_sys );
175 }
176
177 /*****************************************************************************
178  * Filter
179  *****************************************************************************/
180 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
181 {
182     picture_t *p_outpic;
183
184     if( !p_pic ) return NULL;
185     switch( p_pic->format.i_chroma )
186     {
187         case VLC_FOURCC('I','4','2','0'):
188         case VLC_FOURCC('I','Y','U','V'):
189         case VLC_FOURCC('J','4','2','0'):
190         case VLC_FOURCC('Y','V','1','2'):
191
192         case VLC_FOURCC('I','4','2','2'):
193         case VLC_FOURCC('J','4','2','2'):
194             break;
195         default:
196             msg_Warn( p_filter, "Unsupported input chroma (%4s)",
197                       (char*)&(p_pic->format.i_chroma) );
198             if( p_pic->pf_release )
199                 p_pic->pf_release( p_pic );
200             return NULL;
201     }
202
203     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
204     if( !p_outpic )
205     {
206         msg_Warn( p_filter, "can't get output picture" );
207         if( p_pic->pf_release )
208             p_pic->pf_release( p_pic );
209         return NULL;
210     }
211
212     /* Here */
213     FilterErase( p_filter, p_pic, p_outpic );
214
215     p_outpic->date = p_pic->date;
216     p_outpic->b_force = p_pic->b_force;
217     p_outpic->i_nb_fields = p_pic->i_nb_fields;
218     p_outpic->b_progressive = p_pic->b_progressive;
219     p_outpic->b_top_field_first = p_pic->b_top_field_first;
220
221     if( p_pic->pf_release )
222         p_pic->pf_release( p_pic );
223
224     return p_outpic;
225 }
226
227 /*****************************************************************************
228  * FilterErase
229  *****************************************************************************/
230 static void FilterErase( filter_t *p_filter, picture_t *p_inpic,
231                                              picture_t *p_outpic )
232 {
233     filter_sys_t *p_sys = p_filter->p_sys;
234
235     int i_plane;
236
237     const int i_mask_pitch = p_sys->p_mask->A_PITCH;
238     const int i_mask_visible_pitch = p_sys->p_mask->p[A_PLANE].i_visible_pitch;
239     const int i_mask_visible_lines = p_sys->p_mask->p[A_PLANE].i_visible_lines;
240
241     for( i_plane = 0; i_plane < p_inpic->i_planes; i_plane++ )
242     {
243         const int i_pitch = p_inpic->p[i_plane].i_pitch;
244         const int i_2pitch = i_pitch<<1;
245         const int i_visible_pitch = p_inpic->p[i_plane].i_visible_pitch;
246         const int i_lines = p_inpic->p[i_plane].i_lines;
247         const int i_visible_lines = p_inpic->p[i_plane].i_visible_lines;
248
249         uint8_t *p_inpix = p_inpic->p[i_plane].p_pixels;
250         uint8_t *p_outpix = p_outpic->p[i_plane].p_pixels;
251         uint8_t *p_mask = p_sys->p_mask->A_PIXELS;
252
253         int i_x = p_sys->i_x,
254             i_y = p_sys->i_y;
255         int x, y;
256         int i_height = i_mask_visible_lines;
257         int i_width  = i_mask_visible_pitch;
258
259         const vlc_bool_t b_line_factor = ( i_plane /* U_PLANE or V_PLANE */ &&
260             !( p_inpic->format.i_chroma == VLC_FOURCC('I','4','2','2')
261             || p_inpic->format.i_chroma == VLC_FOURCC('J','4','2','2') ) );
262
263         if( i_plane ) /* U_PLANE or V_PLANE */
264         {
265             i_width  >>= 1;
266             i_x      >>= 1;
267         }
268         if( b_line_factor )
269         {
270             i_height >>= 1;
271             i_y      >>= 1;
272         }
273         i_height = __MIN( i_visible_lines - i_y, i_height );
274         i_width  = __MIN( i_visible_pitch - i_x, i_width  );
275
276         /* Copy original pixel buffer */
277         p_filter->p_libvlc->pf_memcpy( p_outpix, p_inpix, i_pitch * i_lines );
278
279         /* Horizontal linear interpolation of masked areas */
280         p_outpix = p_outpic->p[i_plane].p_pixels + i_y*i_pitch + i_x;
281         for( y = 0; y < i_height;
282              y++, p_mask += i_mask_pitch, p_outpix += i_pitch )
283         {
284             uint8_t prev, next = 0;
285             int prev_x = -1, next_x = -2;
286             int quot = 0;
287
288             /* Find a suitable value for the previous color to use when
289              * interpoling a masked pixel's value */
290             if( i_x )
291             {
292                 /* There are pixels before current position on the same line.
293                  * Use those */
294                 prev = *(p_outpix-1);
295             }
296             else if( y || i_y )
297             {
298                 /* This is the first pixel on a line but there other lines
299                  * above us. Use the pixel right above */
300                 prev = *(p_outpix-i_pitch);
301             }
302             else
303             {
304                 /* We're in the upper left corner. This sucks. We can't use
305                  * any previous value, so we'll use a dummy one. In most
306                  * cases this dummy value will be fixed later on in the
307                  * algorithm */
308                 prev = 0xff;
309             }
310
311             for( x = 0; x < i_width; x++ )
312             {
313                 if( p_mask[i_plane?x<<1:x] > 127 )
314                 {
315                     /* This is a masked pixel */
316                     if( next_x <= prev_x )
317                     {
318                         int x0;
319                         /* Look for the next non masked pixel on the same
320                          * line (inside the mask's bounding box) */
321                         for( x0 = x; x0 < i_width; x0++ )
322                         {
323                             if( p_mask[i_plane?x0<<1:x0] <= 127 )
324                             {
325                                 /* We found an unmasked pixel. Victory! */
326                                 next_x = x0;
327                                 next = p_outpix[x0];
328                                 break;
329                             }
330                         }
331                         if( next_x <= prev_x )
332                         {
333                             /* We didn't find an unmasked pixel yet. Try
334                              * harder */
335                             if( x0 == x ) x0++;
336                             if( x0 < i_visible_pitch )
337                             {
338                                 /* If we didn't find a non masked pixel on the
339                                  * same line inside the mask's bounding box,
340                                  * use the next pixel on the line (except if
341                                  * it doesn't exist) */
342                                 next_x = x0;
343                                 next = p_outpix[x0];
344                             }
345                             else
346                             {
347                                 /* The last pixel on the line is masked,
348                                  * so we'll use the "prev" value. A better
349                                  * approach would be to use unmasked pixels
350                                  * at the end of adjacent lines */
351                                 next_x = x0;
352                                 next = prev;
353                             }
354                         }
355                         if( !( i_x || y || i_y ) )
356                             /* We were unable to find a suitable value for
357                              * the previous color (which means that we are
358                              * on the first line in the upper left corner)
359                              */
360                             prev = next;
361
362                         /* Divide only once instead of next_x-prev_x-1 times */
363                         quot = ((next-prev)<<16)/(next_x-prev_x);
364                     }
365                     /* Interpolate new value, and round correctly */
366                     p_outpix[x] = prev + (((x-prev_x)*quot+(1<<16))>>16);
367                 }
368                 else
369                 {
370                     /* This pixel isn't masked. It's thus suitable as a
371                      * previous color for the next interpolation */
372                     prev = p_outpix[x];
373                     prev_x = x;
374                 }
375             }
376         }
377
378         /* Vertical bluring */
379         p_mask = p_sys->p_mask->A_PIXELS;
380         i_height = b_line_factor ? i_mask_visible_lines>>1
381                                  : i_mask_visible_lines;
382         /* Make sure that we stop at least 2 lines before the picture's end
383          * (since our bluring algorithm uses the 2 next lines) */
384         i_height = __MIN( i_visible_lines - i_y - 2, i_height );
385         /* Make sure that we start at least 2 lines from the top (since our
386          * bluring algorithm uses the 2 previous lines) */
387         y = __MAX(i_y-2,0);
388         p_outpix = p_outpic->p[i_plane].p_pixels + (i_y+y)*i_pitch + i_x;
389         for( ; y < i_height; y++, p_mask += i_mask_pitch, p_outpix += i_pitch )
390         {
391             for( x = 0; x < i_width; x++ )
392             {
393                 if( p_mask[i_plane?x<<1:x] > 127 )
394                 {
395                     /* Ugly bluring function */
396                     p_outpix[x] =
397                         ( (p_outpix[x-i_2pitch]<<1)       /* 2 */
398                         + (p_outpix[x-i_pitch ]<<2)       /* 4 */
399                         + (p_outpix[x         ]<<2)       /* 4 */
400                         + (p_outpix[x+i_pitch ]<<2)       /* 4 */
401                         + (p_outpix[x+i_2pitch]<<1) )>>4; /* 2 */
402                 }
403             }
404         }
405     }
406 }
407
408 static int EraseCallback( vlc_object_t *p_this, char const *psz_var,
409                           vlc_value_t oldval, vlc_value_t newval, void *p_data )
410 {
411     filter_sys_t *p_sys = (filter_sys_t *)p_data;
412
413     if( !strcmp( psz_var, CFG_PREFIX "x" ) )
414     {
415         vlc_mutex_lock( &p_sys->lock );
416         p_sys->i_x = newval.i_int;
417         vlc_mutex_unlock( &p_sys->lock );
418     }
419     else if( !strcmp( psz_var, CFG_PREFIX "y" ) )
420     {
421         vlc_mutex_lock( &p_sys->lock );
422         p_sys->i_y = newval.i_int;
423         vlc_mutex_unlock( &p_sys->lock );
424     }
425     else if( !strcmp( psz_var, CFG_PREFIX "mask" ) )
426     {
427         vlc_mutex_lock( &p_sys->lock );
428         LoadMask( (filter_t*)p_this, newval.psz_string );
429         vlc_mutex_unlock( &p_sys->lock );
430     }
431     else
432     {
433         msg_Warn( p_this, "Unknown callback command." );
434     }
435     return VLC_SUCCESS;
436 }