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 "filter_picture.h"
37 /*****************************************************************************
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,
46 static int Create ( vlc_object_t * );
47 static void Destroy ( vlc_object_t * );
49 #define WINDOW_TEXT N_("Window size")
50 #define WINDOW_LONGTEXT N_("Number of frames (0 to 100)")
52 #define SFTN_TEXT N_("Softening value")
53 #define SFTN_LONGTEXT N_("Number of frames consider for smoothening (0 to 30)")
55 #define FILTER_PREFIX "antiflicker-"
57 #define MAX_WINDOW_SZ 100
58 #define MAX_SOFTENING_SZ 31
59 #define SCENE_CHANGE_THRESHOLD 100
61 /*****************************************************************************
63 *****************************************************************************/
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 )
71 add_integer_with_range( FILTER_PREFIX "window-size", 10, 0, MAX_WINDOW_SZ,
72 WINDOW_TEXT, WINDOW_LONGTEXT, false )
74 add_integer_with_range( FILTER_PREFIX "softening-size", 10, 0, MAX_SOFTENING_SZ,
75 SFTN_TEXT, SFTN_LONGTEXT, false )
77 add_shortcut( "antiflicker" )
78 set_callbacks( Create, Destroy )
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 *****************************************************************************/
89 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 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" );
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) );
132 if( p_filter->p_sys->p_old_data == NULL )
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;
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);
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 )
155 filter_t *p_filter = (filter_t *)p_this;
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 );
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 *************************/
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 );
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;
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;
228 /******** Get the luminance average for the current picture ********/
229 int lum_avg = GetLuminanceAvg(p_pic);
231 /*Identify as scene change if the luminance average deviates
232 more than the threshold value or if it is the first frame*/
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)
238 scene_changed = true;
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] );
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];
255 p_filter->p_sys->ia_luminance_data[i_window_size - 1] = lum_avg;
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);
266 /******* Apply the adjustment factor to each pixel on Y_PLANE ********/
268 int scale_num = __MIN(scale,255) * ( 1 << shift );
270 for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
272 for( int i_col = 0; i_col < i_num_cols ; i_col++ )
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;
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] );
287 if (scene_changed || i_softening == 0)
289 return CopyInfoAndRelease( p_outpic, p_pic );
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;
296 for( int i_line = 0 ; i_line < i_num_lines ; i_line++ )
298 for( int i_col = 0; i_col < i_num_cols ; i_col++ )
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)
305 if (diff > (i_softening >> 1))
307 p_yplane_out_old[i_line*i_video_width+i_col] =
308 ((pixel_data * 2) + pixel_old) /3;
313 p_yplane_out_old[i_line*i_video_width+i_col] = pixel_data;
315 p_yplane_out[i_line*i_out_pitch+i_col] =
316 p_yplane_out_old[i_line*i_video_width+i_col];
320 return CopyInfoAndRelease( p_outpic, p_pic );
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,
332 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
333 filter_sys_t *p_sys = (filter_sys_t *)p_data;
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 );