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