upnp: remove m_server_list_lock, p_server_list is already protected by s_lock
[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  * $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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_memory.h>
35
36 #include <vlc_filter.h>
37 #include "filter_picture.h"
38
39 #include <math.h>                                          /* exp(), sqrt() */
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Create    ( vlc_object_t * );
45 static void Destroy   ( vlc_object_t * );
46
47 #define SIGMA_MIN (0.01)
48 #define SIGMA_MAX (4096.0)
49
50 #define SIGMA_TEXT N_("Gaussian's std deviation")
51 #define SIGMA_LONGTEXT N_( \
52     "Gaussian's standard deviation. The blurring will take " \
53     "into account pixels up to 3*sigma away in any direction.")
54
55 #define GAUSSIAN_HELP N_("Add a blurring effect")
56
57 #define FILTER_PREFIX "gaussianblur-"
58
59 vlc_module_begin ()
60     set_description( N_("Gaussian blur video filter") )
61     set_shortname( N_( "Gaussian Blur" ))
62     set_help(GAUSSIAN_HELP)
63     set_capability( "video filter2", 0 )
64     set_category( CAT_VIDEO )
65     set_subcategory( SUBCAT_VIDEO_VFILTER )
66
67     add_float_with_range( FILTER_PREFIX "sigma", 2., SIGMA_MIN, SIGMA_MAX,
68                           SIGMA_TEXT, SIGMA_LONGTEXT,
69                           false )
70
71     set_callbacks( Create, Destroy )
72 vlc_module_end ()
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77 static picture_t *Filter( filter_t *, picture_t * );
78
79 static const char *const ppsz_filter_options[] = {
80     "sigma", NULL
81 };
82
83 /* Comment this to use floats instead of integers (faster for bigger sigma
84  * values)
85  * For sigma = 2 ints are faster
86  * For sigma = 4 floats are faster
87  */
88 #define DONT_USE_FLOATS
89
90 #ifdef DONT_USE_FLOATS
91 #   define type_t int
92 #else
93 #   define type_t float
94 #endif
95
96 struct filter_sys_t
97 {
98     double f_sigma;
99     int i_dim;
100
101     type_t *pt_distribution;
102     type_t *pt_buffer;
103     type_t *pt_scale;
104 };
105
106 static void gaussianblur_InitDistribution( filter_sys_t *p_sys )
107 {
108     double f_sigma = p_sys->f_sigma;
109     int i_dim = (int)(3.*f_sigma);
110     type_t *pt_distribution = xmalloc( (2*i_dim+1) * sizeof( type_t ) );
111
112     for( int x = -i_dim; x <= i_dim; x++ )
113     {
114         const float f_distribution = sqrt( exp(-(x*x)/(f_sigma*f_sigma) ) / (2.*M_PI*f_sigma*f_sigma) );
115 #ifdef DONT_USE_FLOATS
116         const float f_factor = 1 << 8;
117 #else
118         const float f_factor = 1;
119 #endif
120
121         pt_distribution[i_dim+x] = (type_t)( f_distribution * f_factor );
122         //printf("%f\n",(float)pt_distribution[i_dim+x]);
123     }
124     p_sys->i_dim = i_dim;
125     p_sys->pt_distribution = pt_distribution;
126 }
127
128 static int Create( vlc_object_t *p_this )
129 {
130     filter_t *p_filter = (filter_t *)p_this;
131
132     if(   p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420
133        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J420
134        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_YV12
135
136        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_I422
137        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J422
138       )
139     {
140         /* We only want planar YUV 4:2:0 or 4:2:2 */
141         msg_Err( p_filter, "Unsupported input chroma (%4.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         return VLC_ENOMEM;
155
156     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
157                        p_filter->p_cfg );
158
159     p_filter->pf_video_filter = Filter;
160
161     p_filter->p_sys->f_sigma =
162         var_CreateGetFloat( p_filter, FILTER_PREFIX "sigma" );
163     if( p_filter->p_sys->f_sigma <= 0. )
164     {
165         msg_Err( p_filter, "sigma must be greater than zero" );
166         return VLC_EGENERIC;
167     }
168     gaussianblur_InitDistribution( p_filter->p_sys );
169     msg_Dbg( p_filter, "gaussian distribution is %d pixels wide",
170              p_filter->p_sys->i_dim*2+1 );
171
172     p_filter->p_sys->pt_buffer = NULL;
173     p_filter->p_sys->pt_scale = NULL;
174
175     return VLC_SUCCESS;
176 }
177
178 static void Destroy( vlc_object_t *p_this )
179 {
180     filter_t *p_filter = (filter_t *)p_this;
181
182     free( p_filter->p_sys->pt_distribution );
183     free( p_filter->p_sys->pt_buffer );
184     free( p_filter->p_sys->pt_scale );
185
186     free( p_filter->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 }