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