1 /*****************************************************************************
2 * antiflicker.c : antiflicker video effect plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2011 the VideoLAN team
7 * Authors: Dharani Prabhu <dharani.prabhu.s@gmail.com>
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.
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.
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 *****************************************************************************/
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 )
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;
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);
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 )
156 filter_t *p_filter = (filter_t *)p_this;
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 );
166 /*****************************************************************************
167 * GetLuminanceAvg : The funtion returns the luminance average for a picture
168 *****************************************************************************/
169 static int GetLuminanceAvg( picture_t *p_pic )
171 uint8_t *p_yplane_out = p_pic->p[Y_PLANE].p_pixels;
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;
177 if( i_num_lines == 0 || i_num_cols == 0 )
180 unsigned lum_sum = 0;
181 for( int i_line = 0 ; i_line < i_num_lines ; ++i_line )
183 for( int i_col = 0 ; i_col < i_num_cols; ++i_col )
185 lum_sum += p_yplane_out[i_line*i_in_pitch+i_col];
188 unsigned div = i_num_lines * i_num_cols;
189 return (lum_sum + (div>>1)) / div;
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 )
200 if( !p_pic ) return NULL;
202 picture_t *p_outpic = filter_NewPicture( p_filter );
205 picture_Release( p_pic );
209 /****************** Get variables *************************/
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 );
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;
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;
223 /******** Get the luminance average for the current picture ********/
224 int lum_avg = GetLuminanceAvg(p_pic);
226 /*Identify as scene change if the luminance average deviates
227 more than the threshold value or if it is the first frame*/
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)
233 scene_changed = true;
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] );
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];
250 p_filter->p_sys->ia_luminance_data[i_window_size - 1] = lum_avg;
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);
261 /******* Apply the adjustment factor to each pixel on Y_PLANE ********/
263 int scale_num = __MIN(scale,255) * ( 1 << shift );
265 for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
267 for( int i_col = 0; i_col < i_num_cols ; i_col++ )
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;
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] );
282 if (scene_changed || i_softening == 0)
284 return CopyInfoAndRelease( p_outpic, p_pic );
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;
291 for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
293 for( int i_col = 0; i_col < i_num_cols ; i_col++ )
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)
300 if (diff > (i_softening >> 1))
302 p_yplane_out_old[i_line*i_video_width+i_col] =
303 ((pixel_data * 2) + pixel_old) /3;
308 p_yplane_out_old[i_line*i_video_width+i_col] = pixel_data;
310 p_yplane_out[i_line*i_out_pitch+i_col] =
311 p_yplane_out_old[i_line*i_video_width+i_col];
315 return CopyInfoAndRelease( p_outpic, p_pic );
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,
327 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
328 filter_sys_t *p_sys = (filter_sys_t *)p_data;
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 );