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