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