]> git.sesse.net Git - vlc/blob - modules/video_filter/gaussianblur.c
c1e920686d93f0ef1b82db806217b6195d83a34c
[vlc] / modules / video_filter / gaussianblur.c
1 /*****************************************************************************
2  * gaussianblur.c : gaussian blur video filter
3  *****************************************************************************
4  * Copyright (C) 2000-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_vout.h>
32
33 #include "vlc_filter.h"
34
35 #include <math.h>                                          /* exp(), sqrt() */
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  Create    ( vlc_object_t * );
41 static void Destroy   ( vlc_object_t * );
42
43 static picture_t *Filter( filter_t *, picture_t * );
44
45 #define SIGMA_TEXT N_("Gaussian's std deviation")
46 #define SIGMA_LONGTEXT N_( \
47     "Gaussian's standard deviation. The bluring will take " \
48     "into account pixels up to 3*sigma away in any direction.")
49
50 #define FILTER_PREFIX "gaussianblur-"
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 vlc_module_begin();
56     set_description( _("Gaussian blur video filter") );
57     set_shortname( _( "Gaussian Blur" ));
58     set_capability( "video filter2", 0 );
59     set_category( CAT_VIDEO );
60     set_subcategory( SUBCAT_VIDEO_VFILTER );
61
62     add_float( FILTER_PREFIX "sigma", 2., NULL, SIGMA_TEXT, SIGMA_LONGTEXT,
63                VLC_FALSE );
64
65     set_callbacks( Create, Destroy );
66 vlc_module_end();
67
68 static const char *ppsz_filter_options[] = {
69     "sigma", NULL
70 };
71
72 /* Comment this to use floats instead of integers (faster for bigger sigma
73  * values)
74  * For sigma = 2 ints are faster
75  * For sigma = 4 floats are faster
76  */
77 #define DONT_USE_FLOATS
78 struct filter_sys_t
79 {
80     double f_sigma;
81     int i_dim;
82 #ifdef DONT_USE_FLOATS
83     int *pi_distribution;
84     int *pi_buffer;
85     int *pi_scale;
86 #else
87     float *pf_distribution;
88     float *pf_buffer;
89     float *pf_scale;
90 #endif
91 };
92
93 static void gaussianblur_InitDistribution( filter_sys_t *p_sys )
94 {
95     double f_sigma = p_sys->f_sigma;
96     int i_dim = (int)(3.*f_sigma);
97 #ifdef DONT_USE_FLOATS
98     int *pi_distribution = (int*)malloc( (2*i_dim+1) * sizeof( int ) );
99 #else
100     float *pf_distribution = (float*)malloc( (2*i_dim+1) * sizeof( float ) );
101 #endif
102     int x;
103     for( x = -i_dim; x <= i_dim; x++ )
104     {
105 #ifdef DONT_USE_FLOATS
106         pi_distribution[i_dim+x] =
107             (int)( sqrt( exp(-(x*x)/(f_sigma*f_sigma) )
108                  / (2.*M_PI*f_sigma*f_sigma) )  * (double)(1<<8) );
109         printf("%d\n",pi_distribution[i_dim+x]);
110 #else
111         pf_distribution[i_dim+x] = (float)
112             sqrt( exp(-(x*x)/(f_sigma*f_sigma) ) / (2.*M_PI*f_sigma*f_sigma) );
113         printf("%f\n",pf_distribution[i_dim+x]);
114 #endif
115     }
116     p_sys->i_dim = i_dim;
117 #ifdef DONT_USE_FLOATS
118     p_sys->pi_distribution = pi_distribution;
119 #else
120     p_sys->pf_distribution = pf_distribution;
121 #endif
122 }
123
124 static int Create( vlc_object_t *p_this )
125 {
126     filter_t *p_filter = (filter_t *)p_this;
127
128     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
129     if( p_filter->p_sys == NULL )
130     {
131         msg_Err( p_filter, "out of memory" );
132         return VLC_ENOMEM;
133     }
134
135     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
136                        p_filter->p_cfg );
137
138     p_filter->pf_video_filter = Filter;
139
140     p_filter->p_sys->f_sigma =
141         var_CreateGetFloat( p_filter, FILTER_PREFIX "sigma" );
142     if( p_filter->p_sys->f_sigma <= 0. )
143     {
144         msg_Err( p_filter, "sigma must be positive" );
145         return VLC_EGENERIC;
146     }
147     gaussianblur_InitDistribution( p_filter->p_sys );
148     msg_Dbg( p_filter, "gaussian distribution is %d pixels wide",
149              p_filter->p_sys->i_dim*2+1 );
150 #ifdef DONT_USE_FLOATS
151     p_filter->p_sys->pi_buffer = NULL;
152     p_filter->p_sys->pi_scale = NULL;
153 #else
154     p_filter->p_sys->pf_buffer = NULL;
155     p_filter->p_sys->pf_scale = NULL;
156 #endif
157
158     return VLC_SUCCESS;
159 }
160
161 static void Destroy( vlc_object_t *p_this )
162 {
163     filter_t *p_filter = (filter_t *)p_this;
164 #ifdef DONT_USE_FLOATS
165     free( p_filter->p_sys->pi_distribution );
166     free( p_filter->p_sys->pi_buffer );
167     free( p_filter->p_sys->pi_scale );
168 #else
169     free( p_filter->p_sys->pf_distribution );
170     free( p_filter->p_sys->pf_buffer );
171     free( p_filter->p_sys->pf_scale );
172 #endif
173     free( p_filter->p_sys );
174 }
175
176 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
177 {
178     picture_t *p_outpic;
179     filter_sys_t *p_sys = p_filter->p_sys;
180     int i_plane;
181     const int i_dim = p_sys->i_dim;
182 #ifdef DONT_USE_FLOATS
183     int *pi_buffer;
184     int *pi_scale;
185     const int *pi_distribution = p_sys->pi_distribution;
186 #else
187     float *pf_buffer;
188     float *pf_scale;
189     const float *pf_distribution = p_sys->pf_distribution;
190 #endif
191     if( !p_pic ) return NULL;
192
193     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
194     if( !p_outpic )
195     {
196         msg_Warn( p_filter, "can't get output picture" );
197         if( p_pic->pf_release )
198             p_pic->pf_release( p_pic );
199         return NULL;
200     }
201 #ifdef DONT_USE_FLOATS
202     if( !p_sys->pi_buffer )
203     {
204         p_sys->pi_buffer = (int*)realloc( p_sys->pi_buffer,
205                                           p_pic->p[Y_PLANE].i_visible_lines
206                                           * p_pic->p[Y_PLANE].i_pitch
207                                           * sizeof( int ) );
208     }
209     pi_buffer = p_sys->pi_buffer;
210 #else
211     if( !p_sys->pf_buffer )
212     {
213         p_sys->pf_buffer = (float*)realloc( p_sys->pf_buffer,
214                                             p_pic->p[Y_PLANE].i_visible_lines
215                                             * p_pic->p[Y_PLANE].i_pitch
216                                             * sizeof( float ) );
217     }
218     pf_buffer = p_sys->pf_buffer;
219 #endif
220 #ifdef DONT_USE_FLOATS
221     if( !p_sys->pi_scale )
222 #else
223     if( !p_sys->pf_scale )
224 #endif
225     {
226         const int i_visible_lines = p_pic->p[Y_PLANE].i_visible_lines;
227         const int i_visible_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
228         const int i_pitch = p_pic->p[Y_PLANE].i_pitch;
229         int i_col, i_line;
230 #ifdef DONT_USE_FLOATS
231         p_sys->pi_scale = (int*)malloc( i_visible_lines * i_pitch
232                                         * sizeof( int ) );
233         pi_scale = p_sys->pi_scale;
234 #else
235         p_sys->pf_scale = (float*)malloc( i_visible_lines * i_pitch
236                                           * sizeof( float ) );
237         pf_scale = p_sys->pf_scale;
238 #endif
239         for( i_line = 0 ; i_line < i_visible_lines ; i_line++ )
240         {
241             for( i_col = 0; i_col < i_visible_pitch ; i_col++ )
242             {
243                 int x, y;
244 #ifdef DONT_USE_FLOATS
245                 int value = 0;
246 #else
247                 double value = 0.;
248 #endif
249                 for( y = __MAX( -i_dim, -i_line );
250                      y <= __MIN( i_dim, i_visible_lines - i_line - 1 );
251                      y++ )
252                 {
253                     for( x = __MAX( -i_dim, -i_col );
254                          x <= __MIN( i_dim, i_visible_pitch - i_col + 1 );
255                          x++ )
256                     {
257 #ifdef DONT_USE_FLOATS
258                         value += pi_distribution[y+i_dim]
259                                * pi_distribution[x+i_dim];
260 #else
261                         value += ((double)pf_distribution[y+i_dim])
262                                * ((double)pf_distribution[x+i_dim]);
263 #endif
264                     }
265                 }
266 #ifdef DONT_USE_FLOATS
267                 pi_scale[i_line*i_pitch+i_col] = value;
268 #else
269                 pf_scale[i_line*i_pitch+i_col] = (float)(1./value);
270 #endif
271             }
272         }
273     }
274 #ifdef DONT_USE_FLOATS
275     pi_scale = p_sys->pi_scale;
276 #else
277     pf_scale = p_sys->pf_scale;
278 #endif
279
280     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
281     {
282
283         uint8_t *p_in = p_pic->p[i_plane].p_pixels;
284         uint8_t *p_out = p_outpic->p[i_plane].p_pixels;
285
286         const int i_visible_lines = p_pic->p[i_plane].i_visible_lines;
287         const int i_visible_pitch = p_pic->p[i_plane].i_visible_pitch;
288         const int i_pitch = p_pic->p[i_plane].i_pitch;
289
290         int i_line, i_col;
291         const int factor = i_plane ? 1 : 0;
292
293         for( i_line = 0 ; i_line < i_visible_lines ; i_line++ )
294         {
295             for( i_col = 0; i_col < i_visible_pitch ; i_col++ )
296             {
297 #ifdef DONT_USE_FLOATS
298                 int value = 0;
299 #else
300                 float value = 0.;
301 #endif
302                 int x;
303                 const int c = i_line*i_pitch+i_col;
304                 for( x = __MAX( -i_dim, -i_col*(factor+1) );
305                      x <= __MIN( i_dim, (i_visible_pitch - i_col)*(factor+1) + 1 );
306                      x++ )
307                 {
308 #ifdef DONT_USE_FLOATS
309                     value += pi_distribution[x+i_dim]
310                            * p_in[c+(x>>factor)];
311 #else
312                     value += pf_distribution[x+i_dim]
313                            * (float)p_in[c+(x>>factor)];
314 #endif
315                 }
316 #ifdef DONT_USE_FLOATS
317                 pi_buffer[c] = value;
318 #else
319                 pf_buffer[c] = value;
320 #endif
321             }
322         }
323         for( i_line = 0 ; i_line < i_visible_lines ; i_line++ )
324         {
325             for( i_col = 0; i_col < i_visible_pitch ; i_col++ )
326             {
327 #ifdef DONT_USE_FLOATS
328                 int value = 0;
329 #else
330                 float value = 0.;
331 #endif
332                 int y;
333                 const int c = i_line*i_pitch+i_col;
334                 for( y = __MAX( -i_dim, (-i_line)*(factor+1) );
335                      y <= __MIN( i_dim, (i_visible_lines - i_line)*(factor+1) - 1 );
336                      y++ )
337                 {
338 #ifdef DONT_USE_FLOATS
339                     value += pi_distribution[y+i_dim]
340                            * pi_buffer[c+(y>>factor)*i_pitch];
341 #else
342                     value += pf_distribution[y+i_dim]
343                            * pf_buffer[c+(y>>factor)*i_pitch];
344 #endif
345                 }
346 #ifdef DONT_USE_FLOATS
347                 p_out[c] = (uint8_t)(value/pi_scale[(i_line<<factor)*(i_pitch<<factor)+(i_col<<factor)]);
348 #else
349                 p_out[c] = (uint8_t)(value*pf_scale[(i_line<<factor)*(i_pitch<<factor)+(i_col<<factor)]);
350 #endif
351             }
352         }
353     }
354
355     p_outpic->date = p_pic->date;
356     p_outpic->b_force = p_pic->b_force;
357     p_outpic->i_nb_fields = p_pic->i_nb_fields;
358     p_outpic->b_progressive = p_pic->b_progressive;
359     p_outpic->b_top_field_first = p_pic->b_top_field_first;
360
361     if( p_pic->pf_release )
362         p_pic->pf_release( p_pic );
363
364     return p_outpic;
365 }