spatialaudio: remove pause hack
[vlc.git] / modules / video_filter / gaussianblur.c
1 /*****************************************************************************
2  * gaussianblur.c : gaussian blur video filter
3  *****************************************************************************
4  * Copyright (C) 2000-2007 VLC authors and VideoLAN
5  *
6  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_filter.h>
34 #include <vlc_picture.h>
35 #include "filter_picture.h"
36
37 #include <math.h>                                          /* exp(), sqrt() */
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Create    ( vlc_object_t * );
43 static void Destroy   ( vlc_object_t * );
44
45 #define SIGMA_MIN (0.01)
46 #define SIGMA_MAX (4096.0)
47
48 #define SIGMA_TEXT N_("Gaussian's std deviation")
49 #define SIGMA_LONGTEXT N_( \
50     "Gaussian's standard deviation. The blurring will take " \
51     "into account pixels up to 3*sigma away in any direction.")
52
53 #define GAUSSIAN_HELP N_("Add a blurring effect")
54
55 #define FILTER_PREFIX "gaussianblur-"
56
57 vlc_module_begin ()
58     set_description( N_("Gaussian blur video filter") )
59     set_shortname( N_( "Gaussian Blur" ))
60     set_help(GAUSSIAN_HELP)
61     set_capability( "video filter", 0 )
62     set_category( CAT_VIDEO )
63     set_subcategory( SUBCAT_VIDEO_VFILTER )
64
65     add_float_with_range( FILTER_PREFIX "sigma", 2., SIGMA_MIN, SIGMA_MAX,
66                           SIGMA_TEXT, SIGMA_LONGTEXT,
67                           false )
68
69     set_callbacks( Create, Destroy )
70 vlc_module_end ()
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 static picture_t *Filter( filter_t *, picture_t * );
76
77 static const char *const ppsz_filter_options[] = {
78     "sigma", NULL
79 };
80
81 /* Comment this to use floats instead of integers (faster for bigger sigma
82  * values)
83  * For sigma = 2 ints are faster
84  * For sigma = 4 floats are faster
85  */
86 #define DONT_USE_FLOATS
87
88 #ifdef DONT_USE_FLOATS
89 #   define type_t int
90 #else
91 #   define type_t float
92 #endif
93
94 typedef struct
95 {
96     double f_sigma;
97     int i_dim;
98
99     type_t *pt_distribution;
100     type_t *pt_buffer;
101     type_t *pt_scale;
102 } filter_sys_t;
103
104 static void gaussianblur_InitDistribution( filter_sys_t *p_sys )
105 {
106     double f_sigma = p_sys->f_sigma;
107     int i_dim = (int)(3.*f_sigma);
108     type_t *pt_distribution = xmalloc( (2*i_dim+1) * sizeof( type_t ) );
109
110     for( int x = -i_dim; x <= i_dim; x++ )
111     {
112         const float f_distribution = sqrt( exp(-(x*x)/(f_sigma*f_sigma) ) / (2.*M_PI*f_sigma*f_sigma) );
113 #ifdef DONT_USE_FLOATS
114         const float f_factor = 1 << 8;
115 #else
116         const float f_factor = 1;
117 #endif
118
119         pt_distribution[i_dim+x] = (type_t)( f_distribution * f_factor );
120         //printf("%f\n",(float)pt_distribution[i_dim+x]);
121     }
122     p_sys->i_dim = i_dim;
123     p_sys->pt_distribution = pt_distribution;
124 }
125
126 static int Create( vlc_object_t *p_this )
127 {
128     filter_t *p_filter = (filter_t *)p_this;
129
130     if(   p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420
131        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J420
132        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_YV12
133
134        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_I422
135        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J422
136       )
137     {
138         /* We only want planar YUV 4:2:0 or 4:2:2 */
139         msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
140                  (char*)&(p_filter->fmt_in.video.i_chroma) );
141         return VLC_EGENERIC;
142     }
143
144     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
145     {
146         msg_Err( p_filter, "Input and output chromas don't match" );
147         return VLC_EGENERIC;
148     }
149
150     filter_sys_t *p_sys = malloc( sizeof( filter_sys_t ) );
151     if( p_sys == NULL )
152         return VLC_ENOMEM;
153     p_filter->p_sys = p_sys;
154
155     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
156                        p_filter->p_cfg );
157
158     p_filter->pf_video_filter = Filter;
159
160     p_sys->f_sigma =
161         var_CreateGetFloat( p_filter, FILTER_PREFIX "sigma" );
162     if( p_sys->f_sigma <= 0. )
163     {
164         msg_Err( p_filter, "sigma must be greater than zero" );
165         return VLC_EGENERIC;
166     }
167     gaussianblur_InitDistribution( p_sys );
168     msg_Dbg( p_filter, "gaussian distribution is %d pixels wide",
169              p_sys->i_dim*2+1 );
170
171     p_sys->pt_buffer = NULL;
172     p_sys->pt_scale = NULL;
173
174     return VLC_SUCCESS;
175 }
176
177 static void Destroy( vlc_object_t *p_this )
178 {
179     filter_t *p_filter = (filter_t *)p_this;
180     filter_sys_t *p_sys = p_filter->p_sys;
181
182     free( p_sys->pt_distribution );
183     free( p_sys->pt_buffer );
184     free( p_sys->pt_scale );
185
186     free( p_sys );
187 }
188
189 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
190 {
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;
197
198     if( !p_pic ) return NULL;
199
200     p_outpic = filter_NewPicture( p_filter );
201     if( !p_outpic )
202     {
203         picture_Release( p_pic );
204         return NULL;
205     }
206     if( !p_sys->pt_buffer )
207     {
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 ) );
211     }
212
213     pt_buffer = p_sys->pt_buffer;
214     if( !p_sys->pt_scale )
215     {
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;
219
220         p_sys->pt_scale = xmalloc( i_visible_lines * i_pitch * sizeof( type_t ) );
221         pt_scale = p_sys->pt_scale;
222
223         for( int i_line = 0; i_line < i_visible_lines; i_line++ )
224         {
225             for( int i_col = 0; i_col < i_visible_pitch; i_col++ )
226             {
227                 type_t t_value = 0;
228
229                 for( int y = __MAX( -i_dim, -i_line );
230                      y <= __MIN( i_dim, i_visible_lines - i_line - 1 );
231                      y++ )
232                 {
233                     for( int x = __MAX( -i_dim, -i_col );
234                          x <= __MIN( i_dim, i_visible_pitch - i_col + 1 );
235                          x++ )
236                     {
237                         t_value += pt_distribution[y+i_dim] *
238                                    pt_distribution[x+i_dim];
239                     }
240                 }
241                 pt_scale[i_line*i_pitch+i_col] = t_value;
242             }
243         }
244     }
245
246     pt_scale = p_sys->pt_scale;
247     for( int i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
248     {
249
250         uint8_t *p_in = p_pic->p[i_plane].p_pixels;
251         uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
252
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;
256
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( int i_line = 0; i_line < i_visible_lines; i_line++ )
261         {
262             for( int i_col = 0; i_col < i_visible_pitch; i_col++ )
263             {
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++ )
269                 {
270                     t_value += pt_distribution[x+i_dim] *
271                                p_in[c+(x>>x_factor)];
272                 }
273                 pt_buffer[c] = t_value;
274             }
275         }
276         for( int i_line = 0; i_line < i_visible_lines; i_line++ )
277         {
278             for( int i_col = 0; i_col < i_visible_pitch; i_col++ )
279             {
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++ )
285                 {
286                     t_value += pt_distribution[y+i_dim] *
287                                pt_buffer[c+(y>>y_factor)*i_in_pitch];
288                 }
289
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 ?
292             }
293         }
294     }
295
296     return CopyInfoAndRelease( p_outpic, p_pic );
297 }