qt: playlist: use item title if available
[vlc.git] / modules / video_filter / gaussianblur.c
blobfd2c900bc918cc21f4cfb7209e3aa93d3feda8cb
1 /*****************************************************************************
2 * gaussianblur.c : gaussian blur video filter
3 *****************************************************************************
4 * Copyright (C) 2000-2007 VLC authors and VideoLAN
6 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_filter.h>
34 #include <vlc_picture.h>
36 #include <math.h> /* exp(), sqrt() */
38 /*****************************************************************************
39 * Module descriptor
40 *****************************************************************************/
41 static int Create ( filter_t * );
43 #define SIGMA_MIN (0.01)
44 #define SIGMA_MAX (4096.0)
46 #define SIGMA_TEXT N_("Gaussian's std deviation")
47 #define SIGMA_LONGTEXT N_( \
48 "Gaussian's standard deviation. The blurring will take " \
49 "into account pixels up to 3*sigma away in any direction.")
51 #define GAUSSIAN_HELP N_("Add a blurring effect")
53 #define FILTER_PREFIX "gaussianblur-"
55 vlc_module_begin ()
56 set_description( N_("Gaussian blur video filter") )
57 set_shortname( N_( "Gaussian Blur" ))
58 set_help(GAUSSIAN_HELP)
59 set_category( CAT_VIDEO )
60 set_subcategory( SUBCAT_VIDEO_VFILTER )
62 add_float_with_range( FILTER_PREFIX "sigma", 2., SIGMA_MIN, SIGMA_MAX,
63 SIGMA_TEXT, SIGMA_LONGTEXT,
64 false )
66 set_callback_video_filter( Create )
67 vlc_module_end ()
69 /*****************************************************************************
70 * Local prototypes
71 *****************************************************************************/
72 VIDEO_FILTER_WRAPPER_CLOSE(Filter, Destroy)
74 static const char *const ppsz_filter_options[] = {
75 "sigma", NULL
78 /* Comment this to use floats instead of integers (faster for bigger sigma
79 * values)
80 * For sigma = 2 ints are faster
81 * For sigma = 4 floats are faster
83 #define DONT_USE_FLOATS
85 #ifdef DONT_USE_FLOATS
86 # define type_t int
87 #else
88 # define type_t float
89 #endif
91 typedef struct
93 double f_sigma;
94 int i_dim;
96 type_t *pt_distribution;
97 type_t *pt_buffer;
98 type_t *pt_scale;
99 } filter_sys_t;
101 static void gaussianblur_InitDistribution( filter_sys_t *p_sys )
103 double f_sigma = p_sys->f_sigma;
104 int i_dim = (int)(3.*f_sigma);
105 type_t *pt_distribution = xmalloc( (2*i_dim+1) * sizeof( type_t ) );
107 for( int x = -i_dim; x <= i_dim; x++ )
109 const float f_distribution = sqrt( exp(-(x*x)/(f_sigma*f_sigma) ) / (2.*M_PI*f_sigma*f_sigma) );
110 #ifdef DONT_USE_FLOATS
111 const float f_factor = 1 << 8;
112 #else
113 const float f_factor = 1;
114 #endif
116 pt_distribution[i_dim+x] = (type_t)( f_distribution * f_factor );
117 //printf("%f\n",(float)pt_distribution[i_dim+x]);
119 p_sys->i_dim = i_dim;
120 p_sys->pt_distribution = pt_distribution;
123 static int Create( filter_t *p_filter )
125 if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420
126 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J420
127 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_YV12
129 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_I422
130 && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J422
133 /* We only want planar YUV 4:2:0 or 4:2:2 */
134 msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
135 (char*)&(p_filter->fmt_in.video.i_chroma) );
136 return VLC_EGENERIC;
139 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
141 msg_Err( p_filter, "Input and output chromas don't match" );
142 return VLC_EGENERIC;
145 filter_sys_t *p_sys = malloc( sizeof( filter_sys_t ) );
146 if( p_sys == NULL )
147 return VLC_ENOMEM;
148 p_filter->p_sys = p_sys;
150 config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
151 p_filter->p_cfg );
153 p_filter->ops = &Filter_ops;
155 p_sys->f_sigma =
156 var_CreateGetFloat( p_filter, FILTER_PREFIX "sigma" );
157 if( p_sys->f_sigma <= 0. )
159 msg_Err( p_filter, "sigma must be greater than zero" );
160 return VLC_EGENERIC;
162 gaussianblur_InitDistribution( p_sys );
163 msg_Dbg( p_filter, "gaussian distribution is %d pixels wide",
164 p_sys->i_dim*2+1 );
166 p_sys->pt_buffer = NULL;
167 p_sys->pt_scale = NULL;
169 return VLC_SUCCESS;
172 static void Destroy( filter_t *p_filter )
174 filter_sys_t *p_sys = p_filter->p_sys;
176 free( p_sys->pt_distribution );
177 free( p_sys->pt_buffer );
178 free( p_sys->pt_scale );
180 free( p_sys );
183 static void Filter( filter_t *p_filter, picture_t *p_pic, picture_t *p_outpic )
185 filter_sys_t *p_sys = p_filter->p_sys;
186 const int i_dim = p_sys->i_dim;
187 type_t *pt_buffer;
188 type_t *pt_scale;
189 const type_t *pt_distribution = p_sys->pt_distribution;
191 if( !p_sys->pt_buffer )
193 p_sys->pt_buffer = realloc_or_free( p_sys->pt_buffer,
194 p_pic->p[Y_PLANE].i_visible_lines *
195 p_pic->p[Y_PLANE].i_pitch * sizeof( type_t ) );
198 pt_buffer = p_sys->pt_buffer;
199 if( !p_sys->pt_scale )
201 const int i_visible_lines = p_pic->p[Y_PLANE].i_visible_lines;
202 const int i_visible_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
203 const int i_pitch = p_pic->p[Y_PLANE].i_pitch;
205 p_sys->pt_scale = xmalloc( i_visible_lines * i_pitch * sizeof( type_t ) );
206 pt_scale = p_sys->pt_scale;
208 for( int i_line = 0; i_line < i_visible_lines; i_line++ )
210 for( int i_col = 0; i_col < i_visible_pitch; i_col++ )
212 type_t t_value = 0;
214 for( int y = __MAX( -i_dim, -i_line );
215 y <= __MIN( i_dim, i_visible_lines - i_line - 1 );
216 y++ )
218 for( int x = __MAX( -i_dim, -i_col );
219 x <= __MIN( i_dim, i_visible_pitch - i_col + 1 );
220 x++ )
222 t_value += pt_distribution[y+i_dim] *
223 pt_distribution[x+i_dim];
226 pt_scale[i_line*i_pitch+i_col] = t_value;
231 pt_scale = p_sys->pt_scale;
232 for( int i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
235 uint8_t *p_in = p_pic->p[i_plane].p_pixels;
236 uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
238 const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
239 const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
240 const int i_in_pitch = p_pic->p[i_plane].i_pitch;
242 const int x_factor = p_pic->p[Y_PLANE].i_visible_pitch/i_visible_pitch-1;
243 const int y_factor = p_pic->p[Y_PLANE].i_visible_lines/i_visible_lines-1;
245 for( int i_line = 0; i_line < i_visible_lines; i_line++ )
247 for( int i_col = 0; i_col < i_visible_pitch; i_col++ )
249 type_t t_value = 0;
250 const int c = i_line*i_in_pitch+i_col;
251 for( int x = __MAX( -i_dim, -i_col*(x_factor+1) );
252 x <= __MIN( i_dim, (i_visible_pitch - i_col)*(x_factor+1) + 1 );
253 x++ )
255 t_value += pt_distribution[x+i_dim] *
256 p_in[c+(x>>x_factor)];
258 pt_buffer[c] = t_value;
261 for( int i_line = 0; i_line < i_visible_lines; i_line++ )
263 for( int i_col = 0; i_col < i_visible_pitch; i_col++ )
265 type_t t_value = 0;
266 const int c = i_line*i_in_pitch+i_col;
267 for( int y = __MAX( -i_dim, (-i_line)*(y_factor+1) );
268 y <= __MIN( i_dim, (i_visible_lines - i_line)*(y_factor+1) - 1 );
269 y++ )
271 t_value += pt_distribution[y+i_dim] *
272 pt_buffer[c+(y>>y_factor)*i_in_pitch];
275 const type_t t_scale = pt_scale[(i_line<<y_factor)*(i_in_pitch<<x_factor)+(i_col<<x_factor)];
276 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 ?