]> git.sesse.net Git - vlc/blob - modules/video_filter/antiflicker.c
decoder: fix data race in input_DecoderChangePause()
[vlc] / modules / video_filter / antiflicker.c
1 /*****************************************************************************
2  * antiflicker.c : antiflicker video effect plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2011 VLC authors and VideoLAN
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 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_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     {
136         free( p_filter->p_sys );
137         return VLC_ENOMEM;
138     }
139
140     memset( p_filter->p_sys->ia_luminance_data, 0,
141                     sizeof(p_filter->p_sys->ia_luminance_data) );
142     p_filter->p_sys->ia_luminance_data[p_filter->p_sys->i_window_size - 1] = 256;
143
144     var_AddCallback(p_filter,FILTER_PREFIX "window-size",
145         AntiFlickerCallback, p_filter->p_sys);
146     var_AddCallback(p_filter,FILTER_PREFIX "softening-size",
147         AntiFlickerCallback, p_filter->p_sys);
148
149     return VLC_SUCCESS;
150 }
151
152 /*****************************************************************************
153  * Destroy: destroy Distort video thread output method
154  *****************************************************************************
155  * Terminate an output method created by DistortCreateOutputMethod
156  *****************************************************************************/
157 static void Destroy( vlc_object_t *p_this )
158 {
159     filter_t *p_filter = (filter_t *)p_this;
160
161     var_DelCallback(p_filter,FILTER_PREFIX "window-size",
162         AntiFlickerCallback, p_filter->p_sys);
163     var_DelCallback(p_filter,FILTER_PREFIX "softening-size",
164         AntiFlickerCallback, p_filter->p_sys);
165     free(p_filter->p_sys->p_old_data);
166     free( p_filter->p_sys );
167 }
168
169 /*****************************************************************************
170  * GetLuminanceAvg : The funtion returns the luminance average for a picture
171  *****************************************************************************/
172 static int GetLuminanceAvg( picture_t *p_pic )
173 {
174     uint8_t *p_yplane_out = p_pic->p[Y_PLANE].p_pixels;
175
176     int i_num_lines = p_pic->p[Y_PLANE].i_visible_lines;
177     int i_num_cols = p_pic->p[Y_PLANE].i_visible_pitch;
178     int i_in_pitch = p_pic->p[Y_PLANE].i_pitch;
179
180     if( i_num_lines == 0 || i_num_cols == 0 )
181         return 0;
182
183     unsigned lum_sum = 0;
184     for( int i_line = 0 ; i_line < i_num_lines ; ++i_line )
185     {
186         for( int i_col = 0 ; i_col < i_num_cols; ++i_col )
187         {
188             lum_sum += p_yplane_out[i_line*i_in_pitch+i_col];
189         }
190     }
191     unsigned div = i_num_lines * i_num_cols;
192     return (lum_sum + (div>>1)) / div;
193 }
194
195 /*****************************************************************************
196  * Filter: adjust the luminance value and renders
197  *****************************************************************************
198  * The function uses moving average of past frames to adjust the luminance
199  * of current frame also applies temporaral smoothening if enabled.
200  *****************************************************************************/
201 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
202 {
203     if( !p_pic ) return NULL;
204
205     picture_t *p_outpic = filter_NewPicture( p_filter );
206     if( !p_outpic )
207     {
208         picture_Release( p_pic );
209         return NULL;
210     }
211
212     /****************** Get variables *************************/
213
214     int i_window_size = atomic_load( &p_filter->p_sys->i_window_size );
215     int i_softening = atomic_load( &p_filter->p_sys->i_softening );
216
217     uint8_t *p_yplane_in = p_pic->p[Y_PLANE].p_pixels;
218     uint8_t *p_yplane_out = p_outpic->p[Y_PLANE].p_pixels;
219     bool scene_changed = false;
220
221     int i_num_lines = p_pic->p[Y_PLANE].i_visible_lines;
222     int i_num_cols = p_pic->p[Y_PLANE].i_visible_pitch;
223     int i_in_pitch = p_pic->p[Y_PLANE].i_pitch;
224     int i_out_pitch = p_outpic->p[Y_PLANE].i_pitch;
225
226     /******** Get the luminance average for the current picture ********/
227     int lum_avg = GetLuminanceAvg(p_pic);
228
229     /*Identify as scene change if the luminance average deviates
230      more than the threshold value or if it is the first frame*/
231
232     if( abs(lum_avg - p_filter->p_sys->
233         ia_luminance_data[i_window_size - 1]) > SCENE_CHANGE_THRESHOLD
234         || p_filter->p_sys->ia_luminance_data[i_window_size - 1] == 256)
235     {
236         scene_changed = true;
237     }
238
239     if ( scene_changed )
240     {
241         //reset the luminance data
242         for (int i = 0; i < i_window_size; ++i)
243             p_filter->p_sys->ia_luminance_data[i] = lum_avg;
244         plane_CopyPixels( &p_outpic->p[Y_PLANE], &p_pic->p[Y_PLANE] );
245     }
246     else
247     {
248         /******* Compute the adjustment factor using moving average ********/
249         for (int i = 0; i < i_window_size-1 ; ++i)
250             p_filter->p_sys->ia_luminance_data[i] =
251                            p_filter->p_sys->ia_luminance_data[i+1];
252
253         p_filter->p_sys->ia_luminance_data[i_window_size - 1] = lum_avg;
254
255         float scale = 1.0;
256         if (lum_avg > 0)
257         {
258              float filt = 0;
259              for (int i = 0; i < i_window_size; i++)
260                   filt += (float) p_filter->p_sys->ia_luminance_data[i];
261              scale = filt/(i_window_size*lum_avg);
262         }
263
264         /******* Apply the adjustment factor to each pixel on Y_PLANE ********/
265         uint8_t shift = 8;
266         int scale_num = __MIN(scale,255) * ( 1 << shift );
267
268         for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
269         {
270             for( int i_col = 0; i_col < i_num_cols  ; i_col++ )
271             {
272                 uint8_t pixel_data = p_yplane_in[i_line*i_in_pitch+i_col];
273                 int pixel_val = ( scale_num * pixel_data +
274                        (1<<(shift -1)) ) >> shift;
275                 p_yplane_out[i_line*i_out_pitch+i_col] =
276                        (pixel_val>255) ? 255:pixel_val;
277             }
278         }
279     }
280
281     /***************** Copy the UV plane as such *****************************/
282     plane_CopyPixels( &p_outpic->p[U_PLANE], &p_pic->p[U_PLANE] );
283     plane_CopyPixels( &p_outpic->p[V_PLANE], &p_pic->p[V_PLANE] );
284
285     if (scene_changed || i_softening == 0)
286     {
287        return CopyInfoAndRelease( p_outpic, p_pic );
288     }
289
290     /******* Temporal softening phase. Adapted from code by Steven Don ******/
291     uint8_t *p_yplane_out_old = p_filter->p_sys->p_old_data;
292     int i_video_width = p_filter->fmt_in.video.i_width;
293
294     for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
295     {
296         for( int i_col = 0; i_col < i_num_cols  ; i_col++ )
297         {
298             uint8_t pixel_data = p_yplane_out[i_line*i_out_pitch+i_col];
299             uint8_t pixel_old = p_yplane_out_old[i_line*i_video_width+i_col];
300             int diff = abs(pixel_data - pixel_old);
301             if (diff < i_softening)
302             {
303                 if (diff > (i_softening >> 1))
304                 {
305                     p_yplane_out_old[i_line*i_video_width+i_col] =
306                         ((pixel_data * 2) + pixel_old) /3;
307                 }
308             }
309             else
310             {
311                 p_yplane_out_old[i_line*i_video_width+i_col] = pixel_data;
312             }
313             p_yplane_out[i_line*i_out_pitch+i_col] =
314                 p_yplane_out_old[i_line*i_video_width+i_col];
315         }
316     }
317
318     return CopyInfoAndRelease( p_outpic, p_pic );
319 }
320
321 /*****************************************************************************
322  * Callback function to set the parameters
323  *****************************************************************************
324  * This function sets the parameters necesscary for the filter
325  *****************************************************************************/
326 static int AntiFlickerCallback( vlc_object_t *p_this, char const *psz_var,
327                            vlc_value_t oldval, vlc_value_t newval,
328                            void *p_data )
329 {
330     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
331     filter_sys_t *p_sys = (filter_sys_t *)p_data;
332
333     if( !strcmp( psz_var, FILTER_PREFIX "window-size" ) )
334         atomic_store( &p_sys->i_window_size, newval.i_int );
335     else if( !strcmp( psz_var, FILTER_PREFIX "softening-size" ) )
336         atomic_store( &p_sys->i_softening, newval.i_int );
337
338     return VLC_SUCCESS;
339 }