]> git.sesse.net Git - vlc/blob - modules/video_filter/bluescreen.c
Replace argument = realloc( argument, size ); with realloc_or_free() in modules/...
[vlc] / modules / video_filter / bluescreen.c
1 /*****************************************************************************
2  * bluescreen.c : Bluescreen (weather channel like) video filter for vlc
3  *****************************************************************************
4  * Copyright (C) 2005-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod 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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <assert.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_filter.h>
37 #include <vlc_memory.h>
38
39 #define BLUESCREEN_HELP N_( \
40     "This effect, also known as \"greenscreen\" or \"chroma key\" blends " \
41     "the \"blue parts\" of the foreground image of the mosaic on the " \
42     "background (like weather forecasts). You can choose the \"key\" " \
43     "color for blending (blue by default)." )
44
45 #define BLUESCREENU_TEXT N_("Bluescreen U value")
46 #define BLUESCREENU_LONGTEXT N_( \
47         "\"U\" value for the bluescreen key color " \
48         "(in YUV values). From 0 to 255. Defaults to 120 for blue." )
49 #define BLUESCREENV_TEXT N_("Bluescreen V value")
50 #define BLUESCREENV_LONGTEXT N_( \
51         "\"V\" value for the bluescreen key color " \
52         "(in YUV values). From 0 to 255. Defaults to 90 for blue." )
53 #define BLUESCREENUTOL_TEXT N_("Bluescreen U tolerance")
54 #define BLUESCREENUTOL_LONGTEXT N_( \
55         "Tolerance of the bluescreen blender " \
56         "on color variations for the U plane. A value between 10 and 20 " \
57         "seems sensible." )
58 #define BLUESCREENVTOL_TEXT N_("Bluescreen V tolerance")
59 #define BLUESCREENVTOL_LONGTEXT N_( \
60         "Tolerance of the bluescreen blender " \
61         "on color variations for the V plane. A value between 10 and 20 " \
62         "seems sensible." )
63
64 #define CFG_PREFIX "bluescreen-"
65
66 /*****************************************************************************
67  * Local prototypes
68  *****************************************************************************/
69 static int  Create      ( vlc_object_t * );
70 static void Destroy     ( vlc_object_t * );
71
72 static picture_t *Filter( filter_t *, picture_t * );
73 static int BluescreenCallback( vlc_object_t *, char const *,
74                                vlc_value_t, vlc_value_t, void * );
75
76 /*****************************************************************************
77  * Module descriptor
78  *****************************************************************************/
79 vlc_module_begin ()
80     set_description( N_("Bluescreen video filter") )
81     set_shortname( N_("Bluescreen" ))
82     set_help( BLUESCREEN_HELP )
83     set_category( CAT_VIDEO )
84     set_subcategory( SUBCAT_VIDEO_VFILTER )
85     set_capability( "video filter2", 0 )
86     add_shortcut( "bluescreen" )
87     set_callbacks( Create, Destroy )
88
89     add_integer_with_range( CFG_PREFIX "u", 120, 0, 255, NULL,
90                             BLUESCREENU_TEXT, BLUESCREENU_LONGTEXT, false )
91     add_integer_with_range( CFG_PREFIX "v", 90, 0, 255, NULL,
92                             BLUESCREENV_TEXT, BLUESCREENV_LONGTEXT, false )
93     add_integer_with_range( CFG_PREFIX "ut", 17, 0, 255, NULL,
94                             BLUESCREENUTOL_TEXT, BLUESCREENUTOL_LONGTEXT,
95                             false )
96     add_integer_with_range( CFG_PREFIX "vt", 17, 0, 255, NULL,
97                             BLUESCREENVTOL_TEXT, BLUESCREENVTOL_LONGTEXT,
98                             false )
99 vlc_module_end ()
100
101 static const char *const ppsz_filter_options[] = {
102     "u", "v", "ut", "vt", NULL
103 };
104
105 struct filter_sys_t
106 {
107     vlc_mutex_t lock;
108     int i_u, i_v, i_ut, i_vt;
109     uint8_t *p_at;
110 };
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
117     if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVA )
118     {
119         msg_Err( p_filter,
120                  "Unsupported input chroma \"%4s\". "
121                  "Bluescreen can only use \"YUVA\".",
122                  (char*)&p_filter->fmt_in.video.i_chroma );
123         return VLC_EGENERIC;
124     }
125
126     /* Allocate structure */
127     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
128     if( p_filter->p_sys == NULL )
129         return VLC_ENOMEM;
130     p_sys = p_filter->p_sys;
131
132     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
133                        p_filter->p_cfg );
134
135     int val;
136     vlc_mutex_init( &p_sys->lock );
137 #define GET_VAR( name, min, max )                                           \
138     val = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX #name );        \
139     p_sys->i_##name = __MIN( max, __MAX( min, val ) );                      \
140     var_AddCallback( p_filter, CFG_PREFIX #name, BluescreenCallback, p_sys );
141
142     GET_VAR( u, 0x00, 0xff );
143     GET_VAR( v, 0x00, 0xff );
144     GET_VAR( ut, 0x00, 0xff );
145     GET_VAR( vt, 0x00, 0xff );
146     p_sys->p_at = NULL;
147 #undef GET_VAR
148
149     p_filter->pf_video_filter = Filter;
150
151     return VLC_SUCCESS;
152 }
153
154 static void Destroy( vlc_object_t *p_this )
155 {
156     filter_t *p_filter = (filter_t *)p_this;
157     filter_sys_t *p_sys = p_filter->p_sys;
158
159     var_DelCallback( p_filter, CFG_PREFIX "u", BluescreenCallback, p_sys );
160     var_DelCallback( p_filter, CFG_PREFIX "v", BluescreenCallback, p_sys );
161     var_DelCallback( p_filter, CFG_PREFIX "ut", BluescreenCallback, p_sys );
162     var_DelCallback( p_filter, CFG_PREFIX "vt", BluescreenCallback, p_sys );
163
164     free( p_sys->p_at );
165     vlc_mutex_destroy( &p_sys->lock );
166     free( p_sys );
167 }
168
169 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
170 {
171     filter_sys_t *p_sys = p_filter->p_sys;
172
173     int i,j;
174     int i_lines = p_pic->p[ A_PLANE ].i_lines;
175     int i_pitch = p_pic->p[ A_PLANE ].i_pitch;
176     uint8_t *p_a = p_pic->p[ A_PLANE ].p_pixels;
177     uint8_t *p_at;
178     uint8_t *p_u = p_pic->p[ U_PLANE ].p_pixels;
179     uint8_t *p_v = p_pic->p[ V_PLANE ].p_pixels;
180     uint8_t umin, umax, vmin, vmax;
181
182     if( p_pic->format.i_chroma != VLC_CODEC_YUVA )
183     {
184         msg_Err( p_filter,
185                  "Unsupported input chroma \"%4s\". "
186                  "Bluescreen can only use \"YUVA\".",
187                  (char*)&p_pic->format.i_chroma );
188         return NULL;
189     }
190
191     p_sys->p_at = realloc_or_free( p_sys->p_at,
192                                    i_lines * i_pitch * sizeof( uint8_t ) );
193     assert( p_sys->p_at );
194     p_at = p_sys->p_at;
195
196     vlc_mutex_lock( &p_sys->lock );
197     umin = p_sys->i_u - p_sys->i_ut >= 0x00 ? p_sys->i_u - p_sys->i_ut : 0x00;
198     umax = p_sys->i_u + p_sys->i_ut <= 0xff ? p_sys->i_u + p_sys->i_ut : 0xff;
199     vmin = p_sys->i_v - p_sys->i_vt >= 0x00 ? p_sys->i_v - p_sys->i_vt : 0x00;
200     vmax = p_sys->i_v + p_sys->i_vt <= 0xff ? p_sys->i_v + p_sys->i_vt : 0xff;
201     vlc_mutex_unlock( &p_sys->lock );
202
203     for( i = 0; i < i_lines*i_pitch; i++ )
204     {
205         if(    p_u[i] < umax && p_u[i] > umin
206             && p_v[i] < vmax && p_v[i] > vmin )
207         {
208             p_at[i] = 0x00;
209         }
210         else
211         {
212             p_at[i] = 0xff;
213         }
214     }
215     /* Gaussian convolution to make it look cleaner */
216     vlc_memset( p_a, 0, 2 * i_pitch );
217     for( i = 2; i < i_lines - 2; i++ )
218     {
219         p_a[i*i_pitch] = 0x00;
220         p_a[i*i_pitch+1] = 0x00;
221         for( j = 2; j < i_pitch - 2; j ++ )
222         {
223             p_a[i*i_pitch+j] = (uint8_t)((
224               /* 2 rows up */
225                 ( p_at[(i-2)*i_pitch+j-2]<<1 )
226               + ( p_at[(i-2)*i_pitch+j-1]<<2 )
227               + ( p_at[(i-2)*i_pitch+j]<<2 )
228               + ( p_at[(i-2)*i_pitch+j+1]<<2 )
229               + ( p_at[(i-2)*i_pitch+j+2]<<1 )
230               /* 1 row up */
231               + ( p_at[(i-1)*i_pitch+j-2]<<2 )
232               + ( p_at[(i-1)*i_pitch+j-1]<<3 )
233               + ( p_at[(i-1)*i_pitch+j]*12 )
234               + ( p_at[(i-1)*i_pitch+j+1]<<3 )
235               + ( p_at[(i-1)*i_pitch+j+2]<<2 )
236               /* */
237               + ( p_at[i*i_pitch+j-2]<<2 )
238               + ( p_at[i*i_pitch+j-1]*12 )
239               + ( p_at[i*i_pitch+j]<<4 )
240               + ( p_at[i*i_pitch+j+1]*12 )
241               + ( p_at[i*i_pitch+j+2]<<2 )
242               /* 1 row down */
243               + ( p_at[(i+1)*i_pitch+j-2]<<2 )
244               + ( p_at[(i+1)*i_pitch+j-1]<<3 )
245               + ( p_at[(i+1)*i_pitch+j]*12 )
246               + ( p_at[(i+1)*i_pitch+j+1]<<3 )
247               + ( p_at[(i+1)*i_pitch+j+2]<<2 )
248               /* 2 rows down */
249               + ( p_at[(i+2)*i_pitch+j-2]<<1 )
250               + ( p_at[(i+2)*i_pitch+j-1]<<2 )
251               + ( p_at[(i+2)*i_pitch+j]<<2 )
252               + ( p_at[(i+2)*i_pitch+j+1]<<2 )
253               + ( p_at[(i+2)*i_pitch+j+2]<<1 )
254               )/152);
255               if( p_a[i*i_pitch+j] < 0xbf ) p_a[i*i_pitch+j] = 0x00;
256         }
257     }
258     return p_pic;
259 }
260
261 /*****************************************************************************
262 * Callback to update params on the fly
263 *****************************************************************************/
264 static int BluescreenCallback( vlc_object_t *p_this, char const *psz_var,
265                                vlc_value_t oldval, vlc_value_t newval,
266                                void *p_data )
267 {
268     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
269     filter_sys_t *p_sys = (filter_sys_t *) p_data;
270
271     vlc_mutex_lock( &p_sys->lock );
272 #define VAR_IS( a ) !strcmp( psz_var, CFG_PREFIX a )
273     if( VAR_IS( "u" ) )
274         p_sys->i_u = __MAX( 0, __MIN( 255, newval.i_int ) );
275     else if( VAR_IS( "v" ) )
276         p_sys->i_v = __MAX( 0, __MIN( 255, newval.i_int ) );
277     else if( VAR_IS( "ut" ) )
278         p_sys->i_ut = __MAX( 0, __MIN( 255, newval.i_int ) );
279     else if( VAR_IS( "vt" ) )
280         p_sys->i_vt = __MAX( 0, __MIN( 255, newval.i_int ) );
281     vlc_mutex_unlock( &p_sys->lock );
282
283     return VLC_SUCCESS;
284 }