]> git.sesse.net Git - vlc/blob - modules/video_filter/antiflicker.c
antiflicker: use atomic variables instead of lock
[vlc] / modules / video_filter / antiflicker.c
1 /*****************************************************************************
2  * antiflicker.c : antiflicker video effect plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2011 the VideoLAN team
5  * $Id:
6  *
7  * Authors: Dharani Prabhu <dharani.prabhu.s@gmail.com>
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_filter.h>
35 #include <vlc_atomic.h>
36 #include "filter_picture.h"
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int GetLuminanceAvg( picture_t * p_pic );
42 static picture_t *Filter( filter_t *, picture_t * );
43 static int AntiFlickerCallback( vlc_object_t *p_this, char const *psz_var,
44                            vlc_value_t oldval, vlc_value_t newval,
45                            void *p_data );
46
47 static int  Create    ( vlc_object_t * );
48 static void Destroy   ( vlc_object_t * );
49
50 #define WINDOW_TEXT N_("Window size")
51 #define WINDOW_LONGTEXT N_("Number of frames (0 to 100)")
52
53 #define SFTN_TEXT N_("Softening value")
54 #define SFTN_LONGTEXT N_("Number of frames consider for smoothening (0 to 30)")
55
56 #define FILTER_PREFIX "antiflicker-"
57
58 #define MAX_WINDOW_SZ 100
59 #define MAX_SOFTENING_SZ 31
60 #define SCENE_CHANGE_THRESHOLD 100
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin ()
66     set_description( N_("antiflicker video filter") )
67     set_shortname( N_( "antiflicker" ))
68     set_capability( "video filter2", 0 )
69     set_category( CAT_VIDEO )
70     set_subcategory( SUBCAT_VIDEO_VFILTER )
71
72     add_integer_with_range( FILTER_PREFIX "window-size", 10, 0, MAX_WINDOW_SZ,
73         WINDOW_TEXT, WINDOW_LONGTEXT, false )
74
75     add_integer_with_range( FILTER_PREFIX "softening-size", 10, 0, MAX_SOFTENING_SZ,
76         SFTN_TEXT, SFTN_LONGTEXT, false )
77
78     add_shortcut( "antiflicker" )
79     set_callbacks( Create, Destroy )
80 vlc_module_end ()
81
82 /*****************************************************************************
83  * filter_sys_t: Distort video output method descriptor
84  *****************************************************************************
85  * This structure is part of the video output thread descriptor.
86  * It describes the Distort specific properties of an output thread.
87  *****************************************************************************/
88 struct filter_sys_t
89 {
90     atomic_int i_window_size;
91     atomic_int i_softening;
92     int ia_luminance_data[MAX_WINDOW_SZ];
93     uint8_t *p_old_data;
94 };
95
96 /*****************************************************************************
97  * Create: allocates Distort video thread output method
98  *****************************************************************************
99  * This function allocates and initializes a Distort vout method.
100  *****************************************************************************/
101 static int Create( vlc_object_t *p_this )
102 {
103     filter_t *p_filter = (filter_t *)p_this;
104
105     switch( p_filter->fmt_in.video.i_chroma )
106     {
107         CASE_PLANAR_YUV
108             break;
109
110         default:
111              msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
112                       (char*)&(p_filter->fmt_in.video.i_chroma) );
113             return VLC_EGENERIC;
114     }
115
116     /* Allocate structure */
117     p_filter->p_sys = malloc( sizeof( *p_filter->p_sys ) );
118     if( p_filter->p_sys == NULL )
119         return VLC_ENOMEM;
120
121     p_filter->pf_video_filter = Filter;
122
123     /* Initialize the arguments */
124     atomic_init( &p_filter->p_sys->i_window_size,
125                 var_CreateGetIntegerCommand( p_filter,
126                                              FILTER_PREFIX"window-size" ) );
127     atomic_init( &p_filter->p_sys->i_softening,
128                  var_CreateGetIntegerCommand( p_filter,
129                                              FILTER_PREFIX"softening-size" ) );
130
131     p_filter->p_sys->p_old_data = calloc( p_filter->fmt_in.video.i_width *
132      (p_filter->fmt_in.video.i_height+1),sizeof(*p_filter->p_sys->p_old_data) );
133
134     if( p_filter->p_sys->p_old_data == NULL )
135         return VLC_ENOMEM;
136
137     memset( p_filter->p_sys->ia_luminance_data, 0,
138                     sizeof(p_filter->p_sys->ia_luminance_data) );
139     p_filter->p_sys->ia_luminance_data[p_filter->p_sys->i_window_size - 1] = 256;
140
141     var_AddCallback(p_filter,FILTER_PREFIX "window-size",
142         AntiFlickerCallback, p_filter->p_sys);
143     var_AddCallback(p_filter,FILTER_PREFIX "softening-size",
144         AntiFlickerCallback, p_filter->p_sys);
145
146     return VLC_SUCCESS;
147 }
148
149 /*****************************************************************************
150  * Destroy: destroy Distort video thread output method
151  *****************************************************************************
152  * Terminate an output method created by DistortCreateOutputMethod
153  *****************************************************************************/
154 static void Destroy( vlc_object_t *p_this )
155 {
156     filter_t *p_filter = (filter_t *)p_this;
157
158     var_DelCallback(p_filter,FILTER_PREFIX "window-size",
159         AntiFlickerCallback, p_filter->p_sys);
160     var_DelCallback(p_filter,FILTER_PREFIX "softening-size",
161         AntiFlickerCallback, p_filter->p_sys);
162     free(p_filter->p_sys->p_old_data);
163     free( p_filter->p_sys );
164 }
165
166 /*****************************************************************************
167  * GetLuminanceAvg : The funtion returns the luminance average for a picture
168  *****************************************************************************/
169 static int GetLuminanceAvg( picture_t *p_pic )
170 {
171     uint8_t *p_yplane_out = p_pic->p[Y_PLANE].p_pixels;
172
173     int i_num_lines = p_pic->p[Y_PLANE].i_visible_lines;
174     int i_num_cols = p_pic->p[Y_PLANE].i_visible_pitch;
175     int i_in_pitch = p_pic->p[Y_PLANE].i_pitch;
176
177     if( i_num_lines == 0 || i_num_cols == 0 )
178         return 0;
179
180     unsigned lum_sum = 0;
181     for( int i_line = 0 ; i_line < i_num_lines ; ++i_line )
182     {
183         for( int i_col = 0 ; i_col < i_num_cols; ++i_col )
184         {
185             lum_sum += p_yplane_out[i_line*i_in_pitch+i_col];
186         }
187     }
188     unsigned div = i_num_lines * i_num_cols;
189     return (lum_sum + (div>>1)) / div;
190 }
191
192 /*****************************************************************************
193  * Filter: adjust the luminance value and renders
194  *****************************************************************************
195  * The function uses moving average of past frames to adjust the luminance
196  * of current frame also applies temporaral smoothening if enabled.
197  *****************************************************************************/
198 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
199 {
200     if( !p_pic ) return NULL;
201
202     picture_t *p_outpic = filter_NewPicture( p_filter );
203     if( !p_outpic )
204     {
205         picture_Release( p_pic );
206         return NULL;
207     }
208
209     /****************** Get variables *************************/
210
211     int i_window_size = atomic_load( &p_filter->p_sys->i_window_size );
212     int i_softening = atomic_load( &p_filter->p_sys->i_softening );
213
214     uint8_t *p_yplane_in = p_pic->p[Y_PLANE].p_pixels;
215     uint8_t *p_yplane_out = p_outpic->p[Y_PLANE].p_pixels;
216     bool scene_changed = false;
217
218     int i_num_lines = p_pic->p[Y_PLANE].i_visible_lines;
219     int i_num_cols = p_pic->p[Y_PLANE].i_visible_pitch;
220     int i_in_pitch = p_pic->p[Y_PLANE].i_pitch;
221     int i_out_pitch = p_outpic->p[Y_PLANE].i_pitch;
222
223     /******** Get the luminance average for the current picture ********/
224     int lum_avg = GetLuminanceAvg(p_pic);
225
226     /*Identify as scene change if the luminance average deviates
227      more than the threshold value or if it is the first frame*/
228
229     if( abs(lum_avg - p_filter->p_sys->
230         ia_luminance_data[i_window_size - 1]) > SCENE_CHANGE_THRESHOLD
231         || p_filter->p_sys->ia_luminance_data[i_window_size - 1] == 256)
232     {
233         scene_changed = true;
234     }
235
236     if ( scene_changed )
237     {
238         //reset the luminance data
239         for (int i = 0; i < i_window_size; ++i)
240             p_filter->p_sys->ia_luminance_data[i] = lum_avg;
241         plane_CopyPixels( &p_outpic->p[Y_PLANE], &p_pic->p[Y_PLANE] );
242     }
243     else
244     {
245         /******* Compute the adjustment factor using moving average ********/
246         for (int i = 0; i < i_window_size-1 ; ++i)
247             p_filter->p_sys->ia_luminance_data[i] =
248                            p_filter->p_sys->ia_luminance_data[i+1];
249
250         p_filter->p_sys->ia_luminance_data[i_window_size - 1] = lum_avg;
251
252         float scale = 1.0;
253         if (lum_avg > 0)
254         {
255              float filt = 0;
256              for (int i = 0; i < i_window_size; i++)
257                   filt += (float) p_filter->p_sys->ia_luminance_data[i];
258              scale = filt/(i_window_size*lum_avg);
259         }
260
261         /******* Apply the adjustment factor to each pixel on Y_PLANE ********/
262         uint8_t shift = 8;
263         int scale_num = __MIN(scale,255) * ( 1 << shift );
264
265         for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
266         {
267             for( int i_col = 0; i_col < i_num_cols  ; i_col++ )
268             {
269                 uint8_t pixel_data = p_yplane_in[i_line*i_in_pitch+i_col];
270                 int pixel_val = ( scale_num * pixel_data +
271                        (1<<(shift -1)) ) >> shift;
272                 p_yplane_out[i_line*i_out_pitch+i_col] =
273                        (pixel_val>255) ? 255:pixel_val;
274             }
275         }
276     }
277
278     /***************** Copy the UV plane as such *****************************/
279     plane_CopyPixels( &p_outpic->p[U_PLANE], &p_pic->p[U_PLANE] );
280     plane_CopyPixels( &p_outpic->p[V_PLANE], &p_pic->p[V_PLANE] );
281
282     if (scene_changed || i_softening == 0)
283     {
284        return CopyInfoAndRelease( p_outpic, p_pic );
285     }
286
287     /******* Temporal softening phase. Adapted from code by Steven Don ******/
288     uint8_t *p_yplane_out_old = p_filter->p_sys->p_old_data;
289     int i_video_width = p_filter->fmt_in.video.i_width;
290
291     for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
292     {
293         for( int i_col = 0; i_col < i_num_cols  ; i_col++ )
294         {
295             uint8_t pixel_data = p_yplane_out[i_line*i_out_pitch+i_col];
296             uint8_t pixel_old = p_yplane_out_old[i_line*i_video_width+i_col];
297             int diff = abs(pixel_data - pixel_old);
298             if (diff < i_softening)
299             {
300                 if (diff > (i_softening >> 1))
301                 {
302                     p_yplane_out_old[i_line*i_video_width+i_col] =
303                         ((pixel_data * 2) + pixel_old) /3;
304                 }
305             }
306             else
307             {
308                 p_yplane_out_old[i_line*i_video_width+i_col] = pixel_data;
309             }
310             p_yplane_out[i_line*i_out_pitch+i_col] =
311                 p_yplane_out_old[i_line*i_video_width+i_col];
312         }
313     }
314
315     return CopyInfoAndRelease( p_outpic, p_pic );
316 }
317
318 /*****************************************************************************
319  * Callback function to set the parameters
320  *****************************************************************************
321  * This function sets the parameters necesscary for the filter
322  *****************************************************************************/
323 static int AntiFlickerCallback( vlc_object_t *p_this, char const *psz_var,
324                            vlc_value_t oldval, vlc_value_t newval,
325                            void *p_data )
326 {
327     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
328     filter_sys_t *p_sys = (filter_sys_t *)p_data;
329
330     if( !strcmp( psz_var, FILTER_PREFIX "window-size" ) )
331         atomic_store( &p_sys->i_window_size, newval.i_int );
332     else if( !strcmp( psz_var, FILTER_PREFIX "softening-size" ) )
333         atomic_store( &p_sys->i_softening, newval.i_int );
334
335     return VLC_SUCCESS;
336 }