]> git.sesse.net Git - vlc/blob - modules/video_filter/gaussianblur.c
Use var_InheritString for --decklink-video-connection.
[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_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_TEXT N_("Gaussian's std deviation")
48 #define SIGMA_LONGTEXT N_( \
49     "Gaussian's standard deviation. The bluring will take " \
50     "into account pixels up to 3*sigma away in any direction.")
51
52 #define GAUSSIAN_HELP N_("Add a blurring effect")
53
54 #define FILTER_PREFIX "gaussianblur-"
55
56 vlc_module_begin ()
57     set_description( N_("Gaussian blur video filter") )
58     set_shortname( N_( "Gaussian Blur" ))
59     set_help(GAUSSIAN_HELP)
60     set_capability( "video filter2", 0 )
61     set_category( CAT_VIDEO )
62     set_subcategory( SUBCAT_VIDEO_VFILTER )
63
64     add_float( FILTER_PREFIX "sigma", 2., NULL, SIGMA_TEXT, SIGMA_LONGTEXT,
65                false )
66
67     set_callbacks( Create, Destroy )
68 vlc_module_end ()
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static picture_t *Filter( filter_t *, picture_t * );
74
75 static const char *const ppsz_filter_options[] = {
76     "sigma", NULL
77 };
78
79 /* Comment this to use floats instead of integers (faster for bigger sigma
80  * values)
81  * For sigma = 2 ints are faster
82  * For sigma = 4 floats are faster
83  */
84 #define DONT_USE_FLOATS
85
86 #ifdef DONT_USE_FLOATS
87 #   define type_t int
88 #else
89 #   define type_t float
90 #endif
91
92 struct filter_sys_t
93 {
94     double f_sigma;
95     int i_dim;
96
97     type_t *pt_distribution;
98     type_t *pt_buffer;
99     type_t *pt_scale;
100 };
101
102 static void gaussianblur_InitDistribution( filter_sys_t *p_sys )
103 {
104     double f_sigma = p_sys->f_sigma;
105     int i_dim = (int)(3.*f_sigma);
106     type_t *pt_distribution = xmalloc( (2*i_dim+1) * sizeof( type_t ) );
107     int x;
108
109     for( x = -i_dim; x <= i_dim; x++ )
110     {
111         const float f_distribution = sqrt( exp(-(x*x)/(f_sigma*f_sigma) ) / (2.*M_PI*f_sigma*f_sigma) );
112 #ifdef DONT_USE_FLOATS
113         const float f_factor = 1 << 8;
114 #else
115         const float f_factor = 1;
116 #endif
117
118         pt_distribution[i_dim+x] = (type_t)( f_distribution * f_factor );
119         //printf("%f\n",(float)pt_distribution[i_dim+x]);
120     }
121     p_sys->i_dim = i_dim;
122     p_sys->pt_distribution = pt_distribution;
123 }
124
125 static int Create( vlc_object_t *p_this )
126 {
127     filter_t *p_filter = (filter_t *)p_this;
128
129     if(   p_filter->fmt_in.video.i_chroma != VLC_CODEC_I420
130        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J420
131        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_YV12
132
133        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_I422
134        && p_filter->fmt_in.video.i_chroma != VLC_CODEC_J422
135       )
136     {
137         /* We only want planar YUV 4:2:0 or 4:2:2 */
138         msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
139                  (char*)&(p_filter->fmt_in.video.i_chroma) );
140         return VLC_EGENERIC;
141     }
142
143     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
144     {
145         msg_Err( p_filter, "Input and output chromas don't match" );
146         return VLC_EGENERIC;
147     }
148
149     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
150     if( p_filter->p_sys == NULL )
151         return VLC_ENOMEM;
152
153     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
154                        p_filter->p_cfg );
155
156     p_filter->pf_video_filter = Filter;
157
158     p_filter->p_sys->f_sigma =
159         var_CreateGetFloat( p_filter, FILTER_PREFIX "sigma" );
160     if( p_filter->p_sys->f_sigma <= 0. )
161     {
162         msg_Err( p_filter, "sigma must be positive" );
163         return VLC_EGENERIC;
164     }
165     gaussianblur_InitDistribution( p_filter->p_sys );
166     msg_Dbg( p_filter, "gaussian distribution is %d pixels wide",
167              p_filter->p_sys->i_dim*2+1 );
168
169     p_filter->p_sys->pt_buffer = NULL;
170     p_filter->p_sys->pt_scale = NULL;
171
172     return VLC_SUCCESS;
173 }
174
175 static void Destroy( vlc_object_t *p_this )
176 {
177     filter_t *p_filter = (filter_t *)p_this;
178
179     free( p_filter->p_sys->pt_distribution );
180     free( p_filter->p_sys->pt_buffer );
181     free( p_filter->p_sys->pt_scale );
182
183     free( p_filter->p_sys );
184 }
185
186 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
187 {
188     picture_t *p_outpic;
189     filter_sys_t *p_sys = p_filter->p_sys;
190     int i_plane;
191     const int i_dim = p_sys->i_dim;
192     type_t *pt_buffer;
193     type_t *pt_scale;
194     const type_t *pt_distribution = p_sys->pt_distribution;
195
196     if( !p_pic ) return NULL;
197
198     p_outpic = filter_NewPicture( p_filter );
199     if( !p_outpic )
200     {
201         picture_Release( p_pic );
202         return NULL;
203     }
204     if( !p_sys->pt_buffer )
205     {
206         p_sys->pt_buffer = realloc_or_free( p_sys->pt_buffer,
207                                p_pic->p[Y_PLANE].i_visible_lines *
208                                p_pic->p[Y_PLANE].i_pitch * sizeof( type_t ) );
209     }
210
211     pt_buffer = p_sys->pt_buffer;
212     if( !p_sys->pt_scale )
213     {
214         const int i_visible_lines = p_pic->p[Y_PLANE].i_visible_lines;
215         const int i_visible_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
216         const int i_pitch = p_pic->p[Y_PLANE].i_pitch;
217         int i_col, i_line;
218
219         p_sys->pt_scale = xmalloc( i_visible_lines * i_pitch * sizeof( type_t ) );
220         pt_scale = p_sys->pt_scale;
221
222         for( i_line = 0 ; i_line < i_visible_lines ; i_line++ )
223         {
224             for( i_col = 0; i_col < i_visible_pitch ; i_col++ )
225             {
226                 int x, y;
227                 type_t t_value = 0;
228
229                 for( y = __MAX( -i_dim, -i_line );
230                      y <= __MIN( i_dim, i_visible_lines - i_line - 1 );
231                      y++ )
232                 {
233                     for( 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( 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         int i_line, i_col;
258         const int x_factor = p_pic->p[Y_PLANE].i_visible_pitch/i_visible_pitch-1;
259         const int y_factor = p_pic->p[Y_PLANE].i_visible_lines/i_visible_lines-1;
260
261         for( i_line = 0 ; i_line < i_visible_lines ; i_line++ )
262         {
263             for( i_col = 0; i_col < i_visible_pitch ; i_col++ )
264             {
265                 type_t t_value = 0;
266                 int x;
267                 const int c = i_line*i_in_pitch+i_col;
268                 for( x = __MAX( -i_dim, -i_col*(x_factor+1) );
269                      x <= __MIN( i_dim, (i_visible_pitch - i_col)*(x_factor+1) + 1 );
270                      x++ )
271                 {
272                     t_value += pt_distribution[x+i_dim] *
273                                p_in[c+(x>>x_factor)];
274                 }
275                 pt_buffer[c] = t_value;
276             }
277         }
278         for( i_line = 0 ; i_line < i_visible_lines ; i_line++ )
279         {
280             for( i_col = 0; i_col < i_visible_pitch ; i_col++ )
281             {
282                 type_t t_value = 0;
283                 int y;
284                 const int c = i_line*i_in_pitch+i_col;
285                 for( y = __MAX( -i_dim, (-i_line)*(y_factor+1) );
286                      y <= __MIN( i_dim, (i_visible_lines - i_line)*(y_factor+1) - 1 );
287                      y++ )
288                 {
289                     t_value += pt_distribution[y+i_dim] *
290                                pt_buffer[c+(y>>y_factor)*i_in_pitch];
291                 }
292
293                 const type_t t_scale = pt_scale[(i_line<<y_factor)*(i_in_pitch<<x_factor)+(i_col<<x_factor)];
294                 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 ?
295             }
296         }
297     }
298
299     return CopyInfoAndRelease( p_outpic, p_pic );
300 }