1 /*****************************************************************************
2 * antiflicker.c : antiflicker video effect plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2011 VLC authors and VideoLAN
7 * Authors: Dharani Prabhu <dharani.prabhu.s@gmail.com>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
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"
38 /*****************************************************************************
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,
47 static int Create ( vlc_object_t * );
48 static void Destroy ( vlc_object_t * );
50 #define WINDOW_TEXT N_("Window size")
51 #define WINDOW_LONGTEXT N_("Number of frames (0 to 100)")
53 #define SFTN_TEXT N_("Softening value")
54 #define SFTN_LONGTEXT N_("Number of frames consider for smoothening (0 to 30)")
56 #define FILTER_PREFIX "antiflicker-"
58 #define MAX_WINDOW_SZ 100
59 #define MAX_SOFTENING_SZ 31
60 #define SCENE_CHANGE_THRESHOLD 100
62 /*****************************************************************************
64 *****************************************************************************/
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 )
72 add_integer_with_range( FILTER_PREFIX "window-size", 10, 0, MAX_WINDOW_SZ,
73 WINDOW_TEXT, WINDOW_LONGTEXT, false )
75 add_integer_with_range( FILTER_PREFIX "softening-size", 10, 0, MAX_SOFTENING_SZ,
76 SFTN_TEXT, SFTN_LONGTEXT, false )
78 add_shortcut( "antiflicker" )
79 set_callbacks( Create, Destroy )
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 *****************************************************************************/
90 atomic_int i_window_size;
91 atomic_int i_softening;
92 int ia_luminance_data[MAX_WINDOW_SZ];
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 )
103 filter_t *p_filter = (filter_t *)p_this;
105 switch( p_filter->fmt_in.video.i_chroma )
111 msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
112 (char*)&(p_filter->fmt_in.video.i_chroma) );
116 /* Allocate structure */
117 p_filter->p_sys = malloc( sizeof( *p_filter->p_sys ) );
118 if( p_filter->p_sys == NULL )
121 p_filter->pf_video_filter = Filter;
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" ) );
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) );
134 if( p_filter->p_sys->p_old_data == NULL )
136 free( p_filter->p_sys );
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;
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);
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 )
159 filter_t *p_filter = (filter_t *)p_this;
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 );
169 /*****************************************************************************
170 * GetLuminanceAvg : The funtion returns the luminance average for a picture
171 *****************************************************************************/
172 static int GetLuminanceAvg( picture_t *p_pic )
174 uint8_t *p_yplane_out = p_pic->p[Y_PLANE].p_pixels;
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;
180 if( i_num_lines == 0 || i_num_cols == 0 )
183 unsigned lum_sum = 0;
184 for( int i_line = 0 ; i_line < i_num_lines ; ++i_line )
186 for( int i_col = 0 ; i_col < i_num_cols; ++i_col )
188 lum_sum += p_yplane_out[i_line*i_in_pitch+i_col];
191 unsigned div = i_num_lines * i_num_cols;
192 return (lum_sum + (div>>1)) / div;
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 )
203 if( !p_pic ) return NULL;
205 picture_t *p_outpic = filter_NewPicture( p_filter );
208 picture_Release( p_pic );
212 /****************** Get variables *************************/
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 );
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;
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;
226 /******** Get the luminance average for the current picture ********/
227 int lum_avg = GetLuminanceAvg(p_pic);
229 /*Identify as scene change if the luminance average deviates
230 more than the threshold value or if it is the first frame*/
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)
236 scene_changed = true;
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] );
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];
253 p_filter->p_sys->ia_luminance_data[i_window_size - 1] = lum_avg;
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);
264 /******* Apply the adjustment factor to each pixel on Y_PLANE ********/
266 int scale_num = __MIN(scale,255) * ( 1 << shift );
268 for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
270 for( int i_col = 0; i_col < i_num_cols ; i_col++ )
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;
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] );
285 if (scene_changed || i_softening == 0)
287 return CopyInfoAndRelease( p_outpic, p_pic );
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;
294 for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
296 for( int i_col = 0; i_col < i_num_cols ; i_col++ )
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)
303 if (diff > (i_softening >> 1))
305 p_yplane_out_old[i_line*i_video_width+i_col] =
306 ((pixel_data * 2) + pixel_old) /3;
311 p_yplane_out_old[i_line*i_video_width+i_col] = pixel_data;
313 p_yplane_out[i_line*i_out_pitch+i_col] =
314 p_yplane_out_old[i_line*i_video_width+i_col];
318 return CopyInfoAndRelease( p_outpic, p_pic );
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,
330 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
331 filter_sys_t *p_sys = (filter_sys_t *)p_data;
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 );