]> git.sesse.net Git - vlc/blob - modules/video_filter/antiflicker.c
OpenCV example: drop an unused variable
[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 "filter_picture.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int GetLuminanceAvg( picture_t * p_pic );
41 static picture_t *Filter( filter_t *, picture_t * );
42 static int AntiFlickerCallback( vlc_object_t *p_this, char const *psz_var,
43                            vlc_value_t oldval, vlc_value_t newval,
44                            void *p_data );
45
46 static int  Create    ( vlc_object_t * );
47 static void Destroy   ( vlc_object_t * );
48
49 #define WINDOW_TEXT N_("Window size")
50 #define WINDOW_LONGTEXT N_("Number of frames (0 to 100)")
51
52 #define SFTN_TEXT N_("Softening value")
53 #define SFTN_LONGTEXT N_("Number of frames consider for smoothening (0 to 30)")
54
55 #define FILTER_PREFIX "antiflicker-"
56
57 #define MAX_WINDOW_SZ 100
58 #define MAX_SOFTENING_SZ 31
59 #define SCENE_CHANGE_THRESHOLD 100
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 vlc_module_begin ()
65     set_description( N_("antiflicker video filter") )
66     set_shortname( N_( "antiflicker" ))
67     set_capability( "video filter2", 0 )
68     set_category( CAT_VIDEO )
69     set_subcategory( SUBCAT_VIDEO_VFILTER )
70
71     add_integer_with_range( FILTER_PREFIX "window-size", 10, 0, MAX_WINDOW_SZ,
72         WINDOW_TEXT, WINDOW_LONGTEXT, false )
73
74     add_integer_with_range( FILTER_PREFIX "softening-size", 10, 0, MAX_SOFTENING_SZ,
75         SFTN_TEXT, SFTN_LONGTEXT, false )
76
77     add_shortcut( "antiflicker" )
78     set_callbacks( Create, Destroy )
79 vlc_module_end ()
80
81 /*****************************************************************************
82  * filter_sys_t: Distort video output method descriptor
83  *****************************************************************************
84  * This structure is part of the video output thread descriptor.
85  * It describes the Distort specific properties of an output thread.
86  *****************************************************************************/
87 struct filter_sys_t
88 {
89     int ia_luminance_data[MAX_WINDOW_SZ];
90     vlc_mutex_t lock;
91     int i_window_size;
92     int i_softening;
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     p_filter->p_sys->i_window_size = var_CreateGetIntegerCommand( p_filter,
125                                                FILTER_PREFIX "window-size" );
126     p_filter->p_sys->i_softening = var_CreateGetIntegerCommand( p_filter,
127                                                FILTER_PREFIX "softening-size" );
128
129     p_filter->p_sys->p_old_data = calloc( p_filter->fmt_in.video.i_width *
130      (p_filter->fmt_in.video.i_height+1),sizeof(*p_filter->p_sys->p_old_data) );
131
132     if( p_filter->p_sys->p_old_data == NULL )
133         return VLC_ENOMEM;
134
135     memset( p_filter->p_sys->ia_luminance_data, 0,
136                     sizeof(p_filter->p_sys->ia_luminance_data) );
137     p_filter->p_sys->ia_luminance_data[p_filter->p_sys->i_window_size - 1] = 256;
138
139     vlc_mutex_init( &p_filter->p_sys->lock );
140     var_AddCallback(p_filter,FILTER_PREFIX "window-size",
141         AntiFlickerCallback, p_filter->p_sys);
142     var_AddCallback(p_filter,FILTER_PREFIX "softening-size",
143         AntiFlickerCallback, p_filter->p_sys);
144
145     return VLC_SUCCESS;
146 }
147
148 /*****************************************************************************
149  * Destroy: destroy Distort video thread output method
150  *****************************************************************************
151  * Terminate an output method created by DistortCreateOutputMethod
152  *****************************************************************************/
153 static void Destroy( vlc_object_t *p_this )
154 {
155     filter_t *p_filter = (filter_t *)p_this;
156
157     var_DelCallback(p_filter,FILTER_PREFIX "window-size",
158         AntiFlickerCallback, p_filter->p_sys);
159     var_DelCallback(p_filter,FILTER_PREFIX "softening-size",
160         AntiFlickerCallback, p_filter->p_sys);
161     vlc_mutex_destroy( &p_filter->p_sys->lock );
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;
212     int i_softening;
213
214     vlc_mutex_lock( &p_filter->p_sys->lock );
215     i_window_size = p_filter->p_sys->i_window_size;
216     i_softening = p_filter->p_sys->i_softening;
217     vlc_mutex_unlock( &p_filter->p_sys->lock );
218
219     uint8_t *p_yplane_in = p_pic->p[Y_PLANE].p_pixels;
220     uint8_t *p_yplane_out = p_outpic->p[Y_PLANE].p_pixels;
221     bool scene_changed = false;
222
223     int i_num_lines = p_pic->p[Y_PLANE].i_visible_lines;
224     int i_num_cols = p_pic->p[Y_PLANE].i_visible_pitch;
225     int i_in_pitch = p_pic->p[Y_PLANE].i_pitch;
226     int i_out_pitch = p_outpic->p[Y_PLANE].i_pitch;
227
228     /******** Get the luminance average for the current picture ********/
229     int lum_avg = GetLuminanceAvg(p_pic);
230
231     /*Identify as scene change if the luminance average deviates
232      more than the threshold value or if it is the first frame*/
233
234     if( abs(lum_avg - p_filter->p_sys->
235         ia_luminance_data[i_window_size - 1]) > SCENE_CHANGE_THRESHOLD
236         || p_filter->p_sys->ia_luminance_data[i_window_size - 1] == 256)
237     {
238         scene_changed = true;
239     }
240
241     if ( scene_changed )
242     {
243         //reset the luminance data
244         for (int i = 0; i < i_window_size; ++i)
245             p_filter->p_sys->ia_luminance_data[i] = lum_avg;
246         plane_CopyPixels( &p_outpic->p[Y_PLANE], &p_pic->p[Y_PLANE] );
247     }
248     else
249     {
250         /******* Compute the adjustment factor using moving average ********/
251         for (int i = 0; i < i_window_size-1 ; ++i)
252             p_filter->p_sys->ia_luminance_data[i] =
253                            p_filter->p_sys->ia_luminance_data[i+1];
254
255         p_filter->p_sys->ia_luminance_data[i_window_size - 1] = lum_avg;
256
257         float scale = 1.0;
258         if (lum_avg > 0)
259         {
260              float filt = 0;
261              for (int i = 0; i < i_window_size; i++)
262                   filt += (float) p_filter->p_sys->ia_luminance_data[i];
263              scale = filt/(i_window_size*lum_avg);
264         }
265
266         /******* Apply the adjustment factor to each pixel on Y_PLANE ********/
267         uint8_t shift = 8;
268         int scale_num = __MIN(scale,255) * ( 1 << shift );
269
270         for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
271         {
272             for( int i_col = 0; i_col < i_num_cols  ; i_col++ )
273             {
274                 uint8_t pixel_data = p_yplane_in[i_line*i_in_pitch+i_col];
275                 int pixel_val = ( scale_num * pixel_data +
276                        (1<<(shift -1)) ) >> shift;
277                 p_yplane_out[i_line*i_out_pitch+i_col] =
278                        (pixel_val>255) ? 255:pixel_val;
279             }
280         }
281     }
282
283     /***************** Copy the UV plane as such *****************************/
284     plane_CopyPixels( &p_outpic->p[U_PLANE], &p_pic->p[U_PLANE] );
285     plane_CopyPixels( &p_outpic->p[V_PLANE], &p_pic->p[V_PLANE] );
286
287     if (scene_changed || i_softening == 0)
288     {
289        return CopyInfoAndRelease( p_outpic, p_pic );
290     }
291
292     /******* Temporal softening phase. Adapted from code by Steven Don ******/
293     uint8_t *p_yplane_out_old = p_filter->p_sys->p_old_data;
294     int i_video_width = p_filter->fmt_in.video.i_width;
295
296     for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
297     {
298         for( int i_col = 0; i_col < i_num_cols  ; i_col++ )
299         {
300             uint8_t pixel_data = p_yplane_out[i_line*i_out_pitch+i_col];
301             uint8_t pixel_old = p_yplane_out_old[i_line*i_video_width+i_col];
302             int diff = abs(pixel_data - pixel_old);
303             if (diff < i_softening)
304             {
305                 if (diff > (i_softening >> 1))
306                 {
307                     p_yplane_out_old[i_line*i_video_width+i_col] =
308                         ((pixel_data * 2) + pixel_old) /3;
309                 }
310             }
311             else
312             {
313                 p_yplane_out_old[i_line*i_video_width+i_col] = pixel_data;
314             }
315             p_yplane_out[i_line*i_out_pitch+i_col] =
316                 p_yplane_out_old[i_line*i_video_width+i_col];
317         }
318     }
319
320     return CopyInfoAndRelease( p_outpic, p_pic );
321 }
322
323 /*****************************************************************************
324  * Callback function to set the parameters
325  *****************************************************************************
326  * This function sets the parameters necesscary for the filter
327  *****************************************************************************/
328 static int AntiFlickerCallback( vlc_object_t *p_this, char const *psz_var,
329                            vlc_value_t oldval, vlc_value_t newval,
330                            void *p_data )
331 {
332     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
333     filter_sys_t *p_sys = (filter_sys_t *)p_data;
334
335     vlc_mutex_lock( &p_sys->lock );
336     if( !strcmp( psz_var, FILTER_PREFIX "window-size" ) )
337         p_sys->i_window_size = newval.i_int;
338     else if( !strcmp( psz_var, FILTER_PREFIX "softening-size" ) )
339         p_sys->i_softening = newval.i_int;
340     vlc_mutex_unlock( &p_sys->lock );
341
342     return VLC_SUCCESS;
343 }