add_float: remove callback parameter
[vlc/asuraparaju-public.git] / modules / video_filter / gaussianblur.c
blob9ae25b12a042a2ba281a4ccc8f92173f6bf50a10
1 /*****************************************************************************
2 * gaussianblur.c : gaussian blur video filter
3 *****************************************************************************
4 * Copyright (C) 2000-2007 the VideoLAN team
5 * $Id$
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
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.
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.
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 *****************************************************************************/
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>
36 #include <vlc_filter.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_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.")
52 #define GAUSSIAN_HELP N_("Add a blurring effect")
54 #define FILTER_PREFIX "gaussianblur-"
56 vlc_module_begin ()
57 set_description( N_("Gaussian blur video filter") )
58 set_shortname( N_( "Gaussian Blur" ))
59 set_help(GAUSSIAN_HELP)
60 set_capability( "video filter2", 0 )
61 set_category( CAT_VIDEO )
62 set_subcategory( SUBCAT_VIDEO_VFILTER )
64 add_float( FILTER_PREFIX "sigma", 2., SIGMA_TEXT, SIGMA_LONGTEXT,
65 false )
67 set_callbacks( Create, Destroy )
68 vlc_module_end ()
70 /*****************************************************************************
71 * Local prototypes
72 *****************************************************************************/
73 static picture_t *Filter( filter_t *, picture_t * );
75 static const char *const ppsz_filter_options[] = {
76 "sigma", NULL
79 /* Comment this to use floats instead of integers (faster for bigger sigma
80 * values)
81 * For sigma = 2 ints are faster
82 * For sigma = 4 floats are faster
84 #define DONT_USE_FLOATS
86 #ifdef DONT_USE_FLOATS
87 # define type_t int
88 #else
89 # define type_t float
90 #endif
92 struct filter_sys_t
94 double f_sigma;
95 int i_dim;
97 type_t *pt_distribution;
98 type_t *pt_buffer;
99 type_t *pt_scale;
102 static void gaussianblur_InitDistribution( filter_sys_t *p_sys )
104 double f_sigma = p_sys->f_sigma;
105 int i_dim = (int)(3.*f_sigma);
106 type_t *pt_distribution = xmalloc( (2*i_dim+1) * sizeof( type_t ) );
107 int x;
109 for( x = -i_dim; x <= i_dim; x++ )
111 const float f_distribution = sqrt( exp(-(x*x)/(f_sigma*f_sigma) ) / (2.*M_PI*f_sigma*f_sigma) );
112 #ifdef DONT_USE_FLOATS
113 const float f_factor = 1 << 8;
114 #else
115 const float f_factor = 1;
116 #endif
118 pt_distribution[i_dim+x] = (type_t)( f_distribution * f_factor );
119 //printf("%f\n",(float)pt_distribution[i_dim+x]);
121 p_sys->i_dim = i_dim;
122 p_sys->pt_distribution = pt_distribution;
125 static int Create( vlc_object_t *p_this )
127 filter_t *p_filter = (filter_t *)p_this;
129 if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420
130 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J420
131 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_YV12
133 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_I422
134 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J422
137 /* We only want planar YUV 4:2:0 or 4:2:2 */
138 msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
139 (char*)&(p_filter->fmt_in.video.i_chroma) );
140 return VLC_EGENERIC;
143 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
145 msg_Err( p_filter, "Input and output chromas don't match" );
146 return VLC_EGENERIC;
149 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
150 if( p_filter->p_sys == NULL )
151 return VLC_ENOMEM;
153 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
154 p_filter->p_cfg );
156 p_filter->pf_video_filter = Filter;
158 p_filter->p_sys->f_sigma =
159 var_CreateGetFloat( p_filter, FILTER_PREFIX "sigma" );
160 if( p_filter->p_sys->f_sigma <= 0. )
162 msg_Err( p_filter, "sigma must be positive" );
163 return VLC_EGENERIC;
165 gaussianblur_InitDistribution( p_filter->p_sys );
166 msg_Dbg( p_filter, "gaussian distribution is %d pixels wide",
167 p_filter->p_sys->i_dim*2+1 );
169 p_filter->p_sys->pt_buffer = NULL;
170 p_filter->p_sys->pt_scale = NULL;
172 return VLC_SUCCESS;
175 static void Destroy( vlc_object_t *p_this )
177 filter_t *p_filter = (filter_t *)p_this;
179 free( p_filter->p_sys->pt_distribution );
180 free( p_filter->p_sys->pt_buffer );
181 free( p_filter->p_sys->pt_scale );
183 free( p_filter->p_sys );
186 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
188 picture_t *p_outpic;
189 filter_sys_t *p_sys = p_filter->p_sys;
190 int i_plane;
191 const int i_dim = p_sys->i_dim;
192 type_t *pt_buffer;
193 type_t *pt_scale;
194 const type_t *pt_distribution = p_sys->pt_distribution;
196 if( !p_pic ) return NULL;
198 p_outpic = filter_NewPicture( p_filter );
199 if( !p_outpic )
201 picture_Release( p_pic );
202 return NULL;
204 if( !p_sys->pt_buffer )
206 p_sys->pt_buffer = realloc_or_free( p_sys->pt_buffer,
207 p_pic->p[Y_PLANE].i_visible_lines *
208 p_pic->p[Y_PLANE].i_pitch * sizeof( type_t ) );
211 pt_buffer = p_sys->pt_buffer;
212 if( !p_sys->pt_scale )
214 const int i_visible_lines = p_pic->p[Y_PLANE].i_visible_lines;
215 const int i_visible_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
216 const int i_pitch = p_pic->p[Y_PLANE].i_pitch;
217 int i_col, i_line;
219 p_sys->pt_scale = xmalloc( i_visible_lines * i_pitch * sizeof( type_t ) );
220 pt_scale = p_sys->pt_scale;
222 for( i_line = 0 ; i_line < i_visible_lines ; i_line++ )
224 for( i_col = 0; i_col < i_visible_pitch ; i_col++ )
226 int x, y;
227 type_t t_value = 0;
229 for( y = __MAX( -i_dim, -i_line );
230 y <= __MIN( i_dim, i_visible_lines - i_line - 1 );
231 y++ )
233 for( 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( 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 int i_line, i_col;
258 const int x_factor = p_pic->p[Y_PLANE].i_visible_pitch/i_visible_pitch-1;
259 const int y_factor = p_pic->p[Y_PLANE].i_visible_lines/i_visible_lines-1;
261 for( i_line = 0 ; i_line < i_visible_lines ; i_line++ )
263 for( i_col = 0; i_col < i_visible_pitch ; i_col++ )
265 type_t t_value = 0;
266 int x;
267 const int c = i_line*i_in_pitch+i_col;
268 for( x = __MAX( -i_dim, -i_col*(x_factor+1) );
269 x <= __MIN( i_dim, (i_visible_pitch - i_col)*(x_factor+1) + 1 );
270 x++ )
272 t_value += pt_distribution[x+i_dim] *
273 p_in[c+(x>>x_factor)];
275 pt_buffer[c] = t_value;
278 for( i_line = 0 ; i_line < i_visible_lines ; i_line++ )
280 for( i_col = 0; i_col < i_visible_pitch ; i_col++ )
282 type_t t_value = 0;
283 int y;
284 const int c = i_line*i_in_pitch+i_col;
285 for( y = __MAX( -i_dim, (-i_line)*(y_factor+1) );
286 y <= __MIN( i_dim, (i_visible_lines - i_line)*(y_factor+1) - 1 );
287 y++ )
289 t_value += pt_distribution[y+i_dim] *
290 pt_buffer[c+(y>>y_factor)*i_in_pitch];
293 const type_t t_scale = pt_scale[(i_line<<y_factor)*(i_in_pitch<<x_factor)+(i_col<<x_factor)];
294 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 ?
299 return CopyInfoAndRelease( p_outpic, p_pic );