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