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