| 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 |
#include "filter_picture.h" |
|---|
| 38 |
|
|---|
| 39 |
#include <math.h> |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
static int Create ( vlc_object_t * ); |
|---|
| 45 |
static void Destroy ( vlc_object_t * ); |
|---|
| 46 |
|
|---|
| 47 |
#define SIGMA_TEXT N_("Gaussian's std deviation") |
|---|
| 48 |
#define SIGMA_LONGTEXT N_( \ |
|---|
| 49 |
"Gaussian's standard deviation. The bluring will take " \ |
|---|
| 50 |
"into account pixels up to 3*sigma away in any direction.") |
|---|
| 51 |
|
|---|
| 52 |
#define FILTER_PREFIX "gaussianblur-" |
|---|
| 53 |
|
|---|
| 54 |
vlc_module_begin(); |
|---|
| 55 |
set_description( N_("Gaussian blur video filter") ); |
|---|
| 56 |
set_shortname( N_( "Gaussian Blur" )); |
|---|
| 57 |
set_capability( "video filter2", 0 ); |
|---|
| 58 |
set_category( CAT_VIDEO ); |
|---|
| 59 |
set_subcategory( SUBCAT_VIDEO_VFILTER ); |
|---|
| 60 |
|
|---|
| 61 |
add_float( FILTER_PREFIX "sigma", 2., NULL, SIGMA_TEXT, SIGMA_LONGTEXT, |
|---|
| 62 |
false ); |
|---|
| 63 |
|
|---|
| 64 |
set_callbacks( Create, Destroy ); |
|---|
| 65 |
vlc_module_end(); |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
static picture_t *Filter( filter_t *, picture_t * ); |
|---|
| 71 |
|
|---|
| 72 |
static const char *const ppsz_filter_options[] = { |
|---|
| 73 |
"sigma", NULL |
|---|
| 74 |
}; |
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
#define DONT_USE_FLOATS |
|---|
| 82 |
|
|---|
| 83 |
#ifdef DONT_USE_FLOATS |
|---|
| 84 |
# define type_t int |
|---|
| 85 |
#else |
|---|
| 86 |
# define type_t float |
|---|
| 87 |
#endif |
|---|
| 88 |
|
|---|
| 89 |
struct filter_sys_t |
|---|
| 90 |
{ |
|---|
| 91 |
double f_sigma; |
|---|
| 92 |
int i_dim; |
|---|
| 93 |
|
|---|
| 94 |
type_t *pt_distribution; |
|---|
| 95 |
type_t *pt_buffer; |
|---|
| 96 |
type_t *pt_scale; |
|---|
| 97 |
}; |
|---|
| 98 |
|
|---|
| 99 |
static void gaussianblur_InitDistribution( filter_sys_t *p_sys ) |
|---|
| 100 |
{ |
|---|
| 101 |
double f_sigma = p_sys->f_sigma; |
|---|
| 102 |
int i_dim = (int)(3.*f_sigma); |
|---|
| 103 |
type_t *pt_distribution = malloc( (2*i_dim+1) * sizeof( type_t ) ); |
|---|
| 104 |
int x; |
|---|
| 105 |
|
|---|
| 106 |
for( x = -i_dim; x <= i_dim; x++ ) |
|---|
| 107 |
{ |
|---|
| 108 |
const float f_distribution = sqrt( exp(-(x*x)/(f_sigma*f_sigma) ) / (2.*M_PI*f_sigma*f_sigma) ); |
|---|
| 109 |
#ifdef DONT_USE_FLOATS |
|---|
| 110 |
const float f_factor = 1 << 8; |
|---|
| 111 |
#else |
|---|
| 112 |
const float f_factor = 1; |
|---|
| 113 |
#endif |
|---|
| 114 |
|
|---|
| 115 |
pt_distribution[i_dim+x] = (type_t)( f_distribution * f_factor ); |
|---|
| 116 |
|
|---|
| 117 |
} |
|---|
| 118 |
p_sys->i_dim = i_dim; |
|---|
| 119 |
p_sys->pt_distribution = pt_distribution; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
static int Create( vlc_object_t *p_this ) |
|---|
| 123 |
{ |
|---|
| 124 |
filter_t *p_filter = (filter_t *)p_this; |
|---|
| 125 |
|
|---|
| 126 |
if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0') |
|---|
| 127 |
&& p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V') |
|---|
| 128 |
&& p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','0') |
|---|
| 129 |
&& p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2') |
|---|
| 130 |
|
|---|
| 131 |
&& p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','2') |
|---|
| 132 |
&& p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','2') |
|---|
| 133 |
) |
|---|
| 134 |
{ |
|---|
| 135 |
|
|---|
| 136 |
msg_Err( p_filter, "Unsupported input chroma (%4s)", |
|---|
| 137 |
(char*)&(p_filter->fmt_in.video.i_chroma) ); |
|---|
| 138 |
return VLC_EGENERIC; |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma ) |
|---|
| 142 |
{ |
|---|
| 143 |
msg_Err( p_filter, "Input and output chromas don't match" ); |
|---|
| 144 |
return VLC_EGENERIC; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
p_filter->p_sys = malloc( sizeof( filter_sys_t ) ); |
|---|
| 148 |
if( p_filter->p_sys == NULL ) |
|---|
| 149 |
return VLC_ENOMEM; |
|---|
| 150 |
|
|---|
| 151 |
config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options, |
|---|
| 152 |
p_filter->p_cfg ); |
|---|
| 153 |
|
|---|
| 154 |
p_filter->pf_video_filter = Filter; |
|---|
| 155 |
|
|---|
| 156 |
p_filter->p_sys->f_sigma = |
|---|
| 157 |
var_CreateGetFloat( p_filter, FILTER_PREFIX "sigma" ); |
|---|
| 158 |
if( p_filter->p_sys->f_sigma <= 0. ) |
|---|
| 159 |
{ |
|---|
| 160 |
msg_Err( p_filter, "sigma must be positive" ); |
|---|
| 161 |
return VLC_EGENERIC; |
|---|
| 162 |
} |
|---|
| 163 |
gaussianblur_InitDistribution( p_filter->p_sys ); |
|---|
| 164 |
msg_Dbg( p_filter, "gaussian distribution is %d pixels wide", |
|---|
| 165 |
p_filter->p_sys->i_dim*2+1 ); |
|---|
| 166 |
|
|---|
| 167 |
p_filter->p_sys->pt_buffer = NULL; |
|---|
| 168 |
p_filter->p_sys->pt_scale = NULL; |
|---|
| 169 |
|
|---|
| 170 |
return VLC_SUCCESS; |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
static void Destroy( vlc_object_t *p_this ) |
|---|
| 174 |
{ |
|---|
| 175 |
filter_t *p_filter = (filter_t *)p_this; |
|---|
| 176 |
|
|---|
| 177 |
free( p_filter->p_sys->pt_distribution ); |
|---|
| 178 |
free( p_filter->p_sys->pt_buffer ); |
|---|
| 179 |
free( p_filter->p_sys->pt_scale ); |
|---|
| 180 |
|
|---|
| 181 |
free( p_filter->p_sys ); |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic ) |
|---|
| 185 |
{ |
|---|
| 186 |
picture_t *p_outpic; |
|---|
| 187 |
filter_sys_t *p_sys = p_filter->p_sys; |
|---|
| 188 |
int i_plane; |
|---|
| 189 |
const int i_dim = p_sys->i_dim; |
|---|
| 190 |
type_t *pt_buffer; |
|---|
| 191 |
type_t *pt_scale; |
|---|
| 192 |
const type_t *pt_distribution = p_sys->pt_distribution; |
|---|
| 193 |
|
|---|
| 194 |
if( !p_pic ) return NULL; |
|---|
| 195 |
|
|---|
| 196 |
p_outpic = filter_NewPicture( p_filter ); |
|---|
| 197 |
if( !p_outpic ) |
|---|
| 198 |
{ |
|---|
| 199 |
picture_Release( p_pic ); |
|---|
| 200 |
return NULL; |
|---|
| 201 |
} |
|---|
| 202 |
if( !p_sys->pt_buffer ) |
|---|
| 203 |
{ |
|---|
| 204 |
p_sys->pt_buffer = realloc( p_sys->pt_buffer, |
|---|
| 205 |
p_pic->p[Y_PLANE].i_visible_lines * |
|---|
| 206 |
p_pic->p[Y_PLANE].i_pitch * |
|---|
| 207 |
sizeof( type_t ) ); |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
pt_buffer = p_sys->pt_buffer; |
|---|
| 211 |
if( !p_sys->pt_scale ) |
|---|
| 212 |
{ |
|---|
| 213 |
const int i_visible_lines = p_pic->p[Y_PLANE].i_visible_lines; |
|---|
| 214 |
const int i_visible_pitch = p_pic->p[Y_PLANE].i_visible_pitch; |
|---|
| 215 |
const int i_pitch = p_pic->p[Y_PLANE].i_pitch; |
|---|
| 216 |
int i_col, i_line; |
|---|
| 217 |
|
|---|
| 218 |
p_sys->pt_scale = malloc( i_visible_lines * i_pitch * sizeof( type_t ) ); |
|---|
| 219 |
pt_scale = p_sys->pt_scale; |
|---|
| 220 |
|
|---|
| 221 |
for( i_line = 0 ; i_line < i_visible_lines ; i_line++ ) |
|---|
| 222 |
{ |
|---|
| 223 |
for( i_col = 0; i_col < i_visible_pitch ; i_col++ ) |
|---|
| 224 |
{ |
|---|
| 225 |
int x, y; |
|---|
| 226 |
type_t t_value = 0; |
|---|
| 227 |
|
|---|
| 228 |
for( y = __MAX( -i_dim, -i_line ); |
|---|
| 229 |
y <= __MIN( i_dim, i_visible_lines - i_line - 1 ); |
|---|
| 230 |
y++ ) |
|---|
| 231 |
{ |
|---|
| 232 |
for( x = __MAX( -i_dim, -i_col ); |
|---|
| 233 |
x <= __MIN( i_dim, i_visible_pitch - i_col + 1 ); |
|---|
| 234 |
x++ ) |
|---|
| 235 |
{ |
|---|
| 236 |
t_value += pt_distribution[y+i_dim] * |
|---|
| 237 |
pt_distribution[x+i_dim]; |
|---|
| 238 |
} |
|---|
| 239 |
} |
|---|
| 240 |
pt_scale[i_line*i_pitch+i_col] = t_value; |
|---|
| 241 |
} |
|---|
| 242 |
} |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
pt_scale = p_sys->pt_scale; |
|---|
| 246 |
for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ ) |
|---|
| 247 |
{ |
|---|
| 248 |
|
|---|
| 249 |
uint8_t *p_in = p_pic->p[i_plane].p_pixels; |
|---|
| 250 |
uint8_t *p_out = p_outpic->p[i_plane].p_pixels; |
|---|
| 251 |
|
|---|
| 252 |
const int i_visible_lines = p_pic->p[i_plane].i_visible_lines; |
|---|
| 253 |
const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch; |
|---|
| 254 |
const int i_pitch = p_pic->p[i_plane].i_pitch; |
|---|
| 255 |
|
|---|
| 256 |
int i_line, i_col; |
|---|
| 257 |
const int x_factor = p_pic->p[Y_PLANE].i_visible_pitch/i_visible_pitch-1; |
|---|
| 258 |
const int y_factor = p_pic->p[Y_PLANE].i_visible_lines/i_visible_lines-1; |
|---|
| 259 |
|
|---|
| 260 |
for( i_line = 0 ; i_line < i_visible_lines ; i_line++ ) |
|---|
| 261 |
{ |
|---|
| 262 |
for( i_col = 0; i_col < i_visible_pitch ; i_col++ ) |
|---|
| 263 |
{ |
|---|
| 264 |
type_t t_value = 0; |
|---|
| 265 |
int x; |
|---|
| 266 |
const int c = i_line*i_pitch+i_col; |
|---|
| 267 |
for( x = __MAX( -i_dim, -i_col*(x_factor+1) ); |
|---|
| 268 |
x <= __MIN( i_dim, (i_visible_pitch - i_col)*(x_factor+1) + 1 ); |
|---|
| 269 |
x++ ) |
|---|
| 270 |
{ |
|---|
| 271 |
t_value += pt_distribution[x+i_dim] * |
|---|
| 272 |
p_in[c+(x>>x_factor)]; |
|---|
| 273 |
} |
|---|
| 274 |
pt_buffer[c] = t_value; |
|---|
| 275 |
} |
|---|
| 276 |
} |
|---|
| 277 |
for( i_line = 0 ; i_line < i_visible_lines ; i_line++ ) |
|---|
| 278 |
{ |
|---|
| 279 |
for( i_col = 0; i_col < i_visible_pitch ; i_col++ ) |
|---|
| 280 |
{ |
|---|
| 281 |
type_t t_value = 0; |
|---|
| 282 |
int y; |
|---|
| 283 |
const int c = i_line*i_pitch+i_col; |
|---|
| 284 |
for( y = __MAX( -i_dim, (-i_line)*(y_factor+1) ); |
|---|
| 285 |
y <= __MIN( i_dim, (i_visible_lines - i_line)*(y_factor+1) - 1 ); |
|---|
| 286 |
y++ ) |
|---|
| 287 |
{ |
|---|
| 288 |
t_value += pt_distribution[y+i_dim] * |
|---|
| 289 |
pt_buffer[c+(y>>y_factor)*i_pitch]; |
|---|
| 290 |
} |
|---|
| 291 |
|
|---|
| 292 |
const type_t t_scale = pt_scale[(i_line<<y_factor)*(i_pitch<<x_factor)+(i_col<<x_factor)]; |
|---|
| 293 |
p_out[c] = (uint8_t)(t_value / t_scale); |
|---|
| 294 |
} |
|---|
| 295 |
} |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
return CopyInfoAndRelease( p_outpic, p_pic ); |
|---|
| 299 |
} |
|---|