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