demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / video_filter / gaussianblur.c
blobde3bcf4c69512ff10b66dd2a816ffd38ff4c9cdd
1 /*****************************************************************************
2 * gaussianblur.c : gaussian blur video filter
3 *****************************************************************************
4 * Copyright (C) 2000-2007 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_memory.h>
35 #include <vlc_filter.h>
36 #include <vlc_picture.h>
37 #include "filter_picture.h"
39 #include <math.h> /* exp(), sqrt() */
41 /*****************************************************************************
42 * Module descriptor
43 *****************************************************************************/
44 static int Create ( vlc_object_t * );
45 static void Destroy ( vlc_object_t * );
47 #define SIGMA_MIN (0.01)
48 #define SIGMA_MAX (4096.0)
50 #define SIGMA_TEXT N_("Gaussian's std deviation")
51 #define SIGMA_LONGTEXT N_( \
52 "Gaussian's standard deviation. The blurring will take " \
53 "into account pixels up to 3*sigma away in any direction.")
55 #define GAUSSIAN_HELP N_("Add a blurring effect")
57 #define FILTER_PREFIX "gaussianblur-"
59 vlc_module_begin ()
60 set_description( N_("Gaussian blur video filter") )
61 set_shortname( N_( "Gaussian Blur" ))
62 set_help(GAUSSIAN_HELP)
63 set_capability( "video filter", 0 )
64 set_category( CAT_VIDEO )
65 set_subcategory( SUBCAT_VIDEO_VFILTER )
67 add_float_with_range( FILTER_PREFIX "sigma", 2., SIGMA_MIN, SIGMA_MAX,
68 SIGMA_TEXT, SIGMA_LONGTEXT,
69 false )
71 set_callbacks( Create, Destroy )
72 vlc_module_end ()
74 /*****************************************************************************
75 * Local prototypes
76 *****************************************************************************/
77 static picture_t *Filter( filter_t *, picture_t * );
79 static const char *const ppsz_filter_options[] = {
80 "sigma", NULL
83 /* Comment this to use floats instead of integers (faster for bigger sigma
84 * values)
85 * For sigma = 2 ints are faster
86 * For sigma = 4 floats are faster
88 #define DONT_USE_FLOATS
90 #ifdef DONT_USE_FLOATS
91 # define type_t int
92 #else
93 # define type_t float
94 #endif
96 struct filter_sys_t
98 double f_sigma;
99 int i_dim;
101 type_t *pt_distribution;
102 type_t *pt_buffer;
103 type_t *pt_scale;
106 static void gaussianblur_InitDistribution( filter_sys_t *p_sys )
108 double f_sigma = p_sys->f_sigma;
109 int i_dim = (int)(3.*f_sigma);
110 type_t *pt_distribution = xmalloc( (2*i_dim+1) * sizeof( type_t ) );
112 for( int x = -i_dim; x <= i_dim; x++ )
114 const float f_distribution = sqrt( exp(-(x*x)/(f_sigma*f_sigma) ) / (2.*M_PI*f_sigma*f_sigma) );
115 #ifdef DONT_USE_FLOATS
116 const float f_factor = 1 << 8;
117 #else
118 const float f_factor = 1;
119 #endif
121 pt_distribution[i_dim+x] = (type_t)( f_distribution * f_factor );
122 //printf("%f\n",(float)pt_distribution[i_dim+x]);
124 p_sys->i_dim = i_dim;
125 p_sys->pt_distribution = pt_distribution;
128 static int Create( vlc_object_t *p_this )
130 filter_t *p_filter = (filter_t *)p_this;
132 if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420
133 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J420
134 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_YV12
136 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_I422
137 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J422
140 /* We only want planar YUV 4:2:0 or 4:2:2 */
141 msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
142 (char*)&(p_filter->fmt_in.video.i_chroma) );
143 return VLC_EGENERIC;
146 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
148 msg_Err( p_filter, "Input and output chromas don't match" );
149 return VLC_EGENERIC;
152 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
153 if( p_filter->p_sys == NULL )
154 return VLC_ENOMEM;
156 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
157 p_filter->p_cfg );
159 p_filter->pf_video_filter = Filter;
161 p_filter->p_sys->f_sigma =
162 var_CreateGetFloat( p_filter, FILTER_PREFIX "sigma" );
163 if( p_filter->p_sys->f_sigma <= 0. )
165 msg_Err( p_filter, "sigma must be greater than zero" );
166 return VLC_EGENERIC;
168 gaussianblur_InitDistribution( p_filter->p_sys );
169 msg_Dbg( p_filter, "gaussian distribution is %d pixels wide",
170 p_filter->p_sys->i_dim*2+1 );
172 p_filter->p_sys->pt_buffer = NULL;
173 p_filter->p_sys->pt_scale = NULL;
175 return VLC_SUCCESS;
178 static void Destroy( vlc_object_t *p_this )
180 filter_t *p_filter = (filter_t *)p_this;
182 free( p_filter->p_sys->pt_distribution );
183 free( p_filter->p_sys->pt_buffer );
184 free( p_filter->p_sys->pt_scale );
186 free( p_filter->p_sys );
189 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
191 picture_t *p_outpic;
192 filter_sys_t *p_sys = p_filter->p_sys;
193 const int i_dim = p_sys->i_dim;
194 type_t *pt_buffer;
195 type_t *pt_scale;
196 const type_t *pt_distribution = p_sys->pt_distribution;
198 if( !p_pic ) return NULL;
200 p_outpic = filter_NewPicture( p_filter );
201 if( !p_outpic )
203 picture_Release( p_pic );
204 return NULL;
206 if( !p_sys->pt_buffer )
208 p_sys->pt_buffer = realloc_or_free( p_sys->pt_buffer,
209 p_pic->p[Y_PLANE].i_visible_lines *
210 p_pic->p[Y_PLANE].i_pitch * sizeof( type_t ) );
213 pt_buffer = p_sys->pt_buffer;
214 if( !p_sys->pt_scale )
216 const int i_visible_lines = p_pic->p[Y_PLANE].i_visible_lines;
217 const int i_visible_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
218 const int i_pitch = p_pic->p[Y_PLANE].i_pitch;
220 p_sys->pt_scale = xmalloc( i_visible_lines * i_pitch * sizeof( type_t ) );
221 pt_scale = p_sys->pt_scale;
223 for( int i_line = 0; i_line < i_visible_lines; i_line++ )
225 for( int i_col = 0; i_col < i_visible_pitch; i_col++ )
227 type_t t_value = 0;
229 for( int y = __MAX( -i_dim, -i_line );
230 y <= __MIN( i_dim, i_visible_lines - i_line - 1 );
231 y++ )
233 for( int x = __MAX( -i_dim, -i_col );
234 x <= __MIN( i_dim, i_visible_pitch - i_col + 1 );
235 x++ )
237 t_value += pt_distribution[y+i_dim] *
238 pt_distribution[x+i_dim];
241 pt_scale[i_line*i_pitch+i_col] = t_value;
246 pt_scale = p_sys->pt_scale;
247 for( int i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
250 uint8_t *p_in = p_pic->p[i_plane].p_pixels;
251 uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
253 const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
254 const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
255 const int i_in_pitch = p_pic->p[i_plane].i_pitch;
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;
260 for( int i_line = 0; i_line < i_visible_lines; i_line++ )
262 for( int i_col = 0; i_col < i_visible_pitch; i_col++ )
264 type_t t_value = 0;
265 const int c = i_line*i_in_pitch+i_col;
266 for( int x = __MAX( -i_dim, -i_col*(x_factor+1) );
267 x <= __MIN( i_dim, (i_visible_pitch - i_col)*(x_factor+1) + 1 );
268 x++ )
270 t_value += pt_distribution[x+i_dim] *
271 p_in[c+(x>>x_factor)];
273 pt_buffer[c] = t_value;
276 for( int i_line = 0; i_line < i_visible_lines; i_line++ )
278 for( int i_col = 0; i_col < i_visible_pitch; i_col++ )
280 type_t t_value = 0;
281 const int c = i_line*i_in_pitch+i_col;
282 for( int y = __MAX( -i_dim, (-i_line)*(y_factor+1) );
283 y <= __MIN( i_dim, (i_visible_lines - i_line)*(y_factor+1) - 1 );
284 y++ )
286 t_value += pt_distribution[y+i_dim] *
287 pt_buffer[c+(y>>y_factor)*i_in_pitch];
290 const type_t t_scale = pt_scale[(i_line<<y_factor)*(i_in_pitch<<x_factor)+(i_col<<x_factor)];
291 p_out[i_line * p_outpic->p[i_plane].i_pitch + i_col] = (uint8_t)(t_value / t_scale); // FIXME wouldn't it be better to round instead of trunc ?
296 return CopyInfoAndRelease( p_outpic, p_pic );