1 /*****************************************************************************
2 * adjust.c : Contrast/Hue/Saturation/Brightness video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2006 VLC authors and VideoLAN
7 * Authors: Simon Latapie <garf@via.ecp.fr>
8 * Antoine Cellerier <dionoea -at- videolan d0t org>
9 * Martin Briza <gamajun@seznam.cz> (SSE)
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
39 #include <vlc_filter.h>
40 #include "filter_picture.h"
42 #include "adjust_sat_hue.h"
45 # define M_PI 3.14159265358979323846
48 #define eight_times( x ) x x x x x x x x
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Create ( vlc_object_t * );
54 static void Destroy ( vlc_object_t * );
56 static picture_t *FilterPlanar( filter_t *, picture_t * );
57 static picture_t *FilterPacked( filter_t *, picture_t * );
58 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
59 vlc_value_t oldval, vlc_value_t newval,
62 /*****************************************************************************
64 *****************************************************************************/
66 #define THRES_TEXT N_("Brightness threshold")
67 #define THRES_LONGTEXT N_("When this mode is enabled, pixels will be " \
68 "shown as black or white. The threshold value will be the brightness " \
70 #define CONT_TEXT N_("Image contrast (0-2)")
71 #define CONT_LONGTEXT N_("Set the image contrast, between 0 and 2. Defaults to 1.")
72 #define HUE_TEXT N_("Image hue (0-360)")
73 #define HUE_LONGTEXT N_("Set the image hue, between 0 and 360. Defaults to 0.")
74 #define SAT_TEXT N_("Image saturation (0-3)")
75 #define SAT_LONGTEXT N_("Set the image saturation, between 0 and 3. Defaults to 1.")
76 #define LUM_TEXT N_("Image brightness (0-2)")
77 #define LUM_LONGTEXT N_("Set the image brightness, between 0 and 2. Defaults to 1.")
78 #define GAMMA_TEXT N_("Image gamma (0-10)")
79 #define GAMMA_LONGTEXT N_("Set the image gamma, between 0.01 and 10. Defaults to 1.")
82 set_description( N_("Image properties filter") )
83 set_shortname( N_("Image adjust" ))
84 set_category( CAT_VIDEO )
85 set_subcategory( SUBCAT_VIDEO_VFILTER )
86 set_capability( "video filter2", 0 )
88 add_float_with_range( "contrast", 1.0, 0.0, 2.0,
89 CONT_TEXT, CONT_LONGTEXT, false )
91 add_float_with_range( "brightness", 1.0, 0.0, 2.0,
92 LUM_TEXT, LUM_LONGTEXT, false )
94 add_float_with_range( "hue", 0, -180., +180.,
95 HUE_TEXT, HUE_LONGTEXT, false )
97 add_float_with_range( "saturation", 1.0, 0.0, 3.0,
98 SAT_TEXT, SAT_LONGTEXT, false )
100 add_float_with_range( "gamma", 1.0, 0.01, 10.0,
101 GAMMA_TEXT, GAMMA_LONGTEXT, false )
103 add_bool( "brightness-threshold", false,
104 THRES_TEXT, THRES_LONGTEXT, false )
107 add_shortcut( "adjust" )
108 set_callbacks( Create, Destroy )
111 static const char *const ppsz_filter_options[] = {
112 "contrast", "brightness", "hue", "saturation", "gamma",
113 "brightness-threshold", NULL
116 /*****************************************************************************
117 * filter_sys_t: adjust filter method descriptor
118 *****************************************************************************/
127 bool b_brightness_threshold;
128 int (*pf_process_sat_hue)( picture_t *, picture_t *, int, int, int,
130 int (*pf_process_sat_hue_clip)( picture_t *, picture_t *, int, int,
134 /*****************************************************************************
135 * Create: allocates adjust video filter
136 *****************************************************************************/
137 static int Create( vlc_object_t *p_this )
139 filter_t *p_filter = (filter_t *)p_this;
142 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
144 msg_Err( p_filter, "Input and output chromas don't match" );
148 /* Allocate structure */
149 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
150 if( p_filter->p_sys == NULL )
152 p_sys = p_filter->p_sys;
154 /* needed to get options passed in transcode using the
155 * adjust{name=value} syntax */
156 config_ChainParse( p_filter, "", ppsz_filter_options,
159 p_sys->f_contrast = var_CreateGetFloatCommand( p_filter, "contrast" );
160 p_sys->f_brightness = var_CreateGetFloatCommand( p_filter, "brightness" );
161 p_sys->f_hue = var_CreateGetFloatCommand( p_filter, "hue" );
162 p_sys->f_saturation = var_CreateGetFloatCommand( p_filter, "saturation" );
163 p_sys->f_gamma = var_CreateGetFloatCommand( p_filter, "gamma" );
164 p_sys->b_brightness_threshold =
165 var_CreateGetBoolCommand( p_filter, "brightness-threshold" );
167 /* Choose Planar/Packed function and pointer to a Hue/Saturation processing
169 switch( p_filter->fmt_in.video.i_chroma )
173 p_filter->pf_video_filter = FilterPlanar;
174 p_sys->pf_process_sat_hue_clip = planar_sat_hue_clip_C;
175 p_sys->pf_process_sat_hue = planar_sat_hue_C;
179 /* Packed YUV 4:2:2 */
180 p_filter->pf_video_filter = FilterPacked;
181 p_sys->pf_process_sat_hue_clip = packed_sat_hue_clip_C;
182 p_sys->pf_process_sat_hue = packed_sat_hue_C;
186 msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
187 (char*)&(p_filter->fmt_in.video.i_chroma) );
191 vlc_mutex_init( &p_sys->lock );
192 var_AddCallback( p_filter, "contrast", AdjustCallback, p_sys );
193 var_AddCallback( p_filter, "brightness", AdjustCallback, p_sys );
194 var_AddCallback( p_filter, "hue", AdjustCallback, p_sys );
195 var_AddCallback( p_filter, "saturation", AdjustCallback, p_sys );
196 var_AddCallback( p_filter, "gamma", AdjustCallback, p_sys );
197 var_AddCallback( p_filter, "brightness-threshold",
198 AdjustCallback, p_sys );
203 /*****************************************************************************
204 * Destroy: destroy adjust video filter
205 *****************************************************************************/
206 static void Destroy( vlc_object_t *p_this )
208 filter_t *p_filter = (filter_t *)p_this;
209 filter_sys_t *p_sys = p_filter->p_sys;
211 var_DelCallback( p_filter, "contrast", AdjustCallback, p_sys );
212 var_DelCallback( p_filter, "brightness", AdjustCallback, p_sys );
213 var_DelCallback( p_filter, "hue", AdjustCallback, p_sys );
214 var_DelCallback( p_filter, "saturation", AdjustCallback, p_sys );
215 var_DelCallback( p_filter, "gamma", AdjustCallback, p_sys );
216 var_DelCallback( p_filter, "brightness-threshold",
217 AdjustCallback, p_sys );
219 vlc_mutex_destroy( &p_sys->lock );
223 /*****************************************************************************
224 * Run the filter on a Planar YUV picture
225 *****************************************************************************/
226 static picture_t *FilterPlanar( filter_t *p_filter, picture_t *p_pic )
232 uint8_t *p_in, *p_in_end, *p_line_end;
235 filter_sys_t *p_sys = p_filter->p_sys;
237 if( !p_pic ) return NULL;
239 p_outpic = filter_NewPicture( p_filter );
242 picture_Release( p_pic );
247 vlc_mutex_lock( &p_sys->lock );
248 int32_t i_cont = lroundf( p_sys->f_contrast * 255.f );
249 int32_t i_lum = lroundf( (p_sys->f_brightness - 1.f) * 255.f );
250 float f_hue = p_sys->f_hue * (float)(M_PI / 180.);
251 int i_sat = (int)( p_sys->f_saturation * 256.f );
252 float f_gamma = 1.f / p_sys->f_gamma;
253 bool b_thres = p_sys->b_brightness_threshold;
254 vlc_mutex_unlock( &p_sys->lock );
257 * Threshold mode drops out everything about luma, contrast and gamma.
262 /* Contrast is a fast but kludged function, so I put this gap to be
264 i_lum += 128 - i_cont / 2;
266 /* Fill the gamma lookup table */
267 for( unsigned i = 0 ; i < 256 ; i++ )
269 pi_gamma[ i ] = clip_uint8_vlc( powf(i / 255.f, f_gamma) * 255.f);
272 /* Fill the luma lookup table */
273 for( unsigned i = 0 ; i < 256 ; i++ )
275 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
281 * We get luma as threshold value: the higher it is, the darker is
282 * the image. Should I reverse this?
284 for( int i = 0 ; i < 256 ; i++ )
286 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
290 * Desaturates image to avoid that strange yellow halo...
299 p_in = p_pic->p[Y_PLANE].p_pixels;
300 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
301 * p_pic->p[Y_PLANE].i_pitch - 8;
303 p_out = p_outpic->p[Y_PLANE].p_pixels;
305 for( ; p_in < p_in_end ; )
307 p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
309 for( ; p_in < p_line_end ; )
311 /* Do 8 pixels at a time */
312 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
313 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
314 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
315 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
320 for( ; p_in < p_line_end ; )
322 *p_out++ = pi_luma[ *p_in++ ];
325 p_in += p_pic->p[Y_PLANE].i_pitch
326 - p_pic->p[Y_PLANE].i_visible_pitch;
327 p_out += p_outpic->p[Y_PLANE].i_pitch
328 - p_outpic->p[Y_PLANE].i_visible_pitch;
332 * Do the U and V planes
335 int i_sin = sinf(f_hue) * 256.f;
336 int i_cos = cosf(f_hue) * 256.f;
338 int i_x = ( cosf(f_hue) + sinf(f_hue) ) * 32768.f;
339 int i_y = ( cosf(f_hue) - sinf(f_hue) ) * 32768.f;
343 /* Currently no errors are implemented in the function, if any are added
345 p_sys->pf_process_sat_hue_clip( p_pic, p_outpic, i_sin, i_cos, i_sat,
350 /* Currently no errors are implemented in the function, if any are added
352 p_sys->pf_process_sat_hue( p_pic, p_outpic, i_sin, i_cos, i_sat,
356 return CopyInfoAndRelease( p_outpic, p_pic );
359 /*****************************************************************************
360 * Run the filter on a Packed YUV picture
361 *****************************************************************************/
362 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
368 uint8_t *p_in, *p_in_end, *p_line_end;
370 int i_y_offset, i_u_offset, i_v_offset;
372 int i_pitch, i_visible_pitch;
377 int32_t i_cont, i_lum;
378 int i_sat, i_sin, i_cos, i_x, i_y;
381 filter_sys_t *p_sys = p_filter->p_sys;
383 if( !p_pic ) return NULL;
385 i_pitch = p_pic->p->i_pitch;
386 i_visible_pitch = p_pic->p->i_visible_pitch;
388 if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
389 &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
391 msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
392 (char*)&(p_pic->format.i_chroma) );
394 picture_Release( p_pic );
398 p_outpic = filter_NewPicture( p_filter );
401 msg_Warn( p_filter, "can't get output picture" );
403 picture_Release( p_pic );
408 vlc_mutex_lock( &p_sys->lock );
409 i_cont = (int)( p_sys->f_contrast * 255 );
410 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
411 f_hue = p_sys->f_hue * (float)(M_PI / 180.);
412 i_sat = (int)( p_sys->f_saturation * 256 );
413 f_gamma = 1.0 / p_sys->f_gamma;
414 b_thres = p_sys->b_brightness_threshold;
415 vlc_mutex_unlock( &p_sys->lock );
418 * Threshold mode drops out everything about luma, contrast and gamma.
423 /* Contrast is a fast but kludged function, so I put this gap to be
425 i_lum += 128 - i_cont / 2;
427 /* Fill the gamma lookup table */
428 for( i = 0 ; i < 256 ; i++ )
430 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
433 /* Fill the luma lookup table */
434 for( i = 0 ; i < 256 ; i++ )
436 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
442 * We get luma as threshold value: the higher it is, the darker is
443 * the image. Should I reverse this?
445 for( i = 0 ; i < 256 ; i++ )
447 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
451 * Desaturates image to avoid that strange yellow halo...
460 p_in = p_pic->p->p_pixels + i_y_offset;
461 p_in_end = p_in + p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 * 4;
463 p_out = p_outpic->p->p_pixels + i_y_offset;
465 for( ; p_in < p_in_end ; )
467 p_line_end = p_in + i_visible_pitch - 8 * 4;
469 for( ; p_in < p_line_end ; )
471 /* Do 8 pixels at a time */
472 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
473 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
474 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
475 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
476 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
477 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
478 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
479 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
484 for( ; p_in < p_line_end ; )
486 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
489 p_in += i_pitch - p_pic->p->i_visible_pitch;
490 p_out += i_pitch - p_outpic->p->i_visible_pitch;
494 * Do the U and V planes
497 i_sin = sin(f_hue) * 256;
498 i_cos = cos(f_hue) * 256;
500 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
501 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
505 if ( p_sys->pf_process_sat_hue_clip( p_pic, p_outpic, i_sin, i_cos, i_sat,
506 i_x, i_y ) != VLC_SUCCESS )
508 /* Currently only one error can happen in the function, but if there
509 * will be more of them, this message must go away */
510 msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
511 (char*)&(p_pic->format.i_chroma) );
512 picture_Release( p_pic );
518 if ( p_sys->pf_process_sat_hue( p_pic, p_outpic, i_sin, i_cos, i_sat,
519 i_x, i_y ) != VLC_SUCCESS )
521 /* Currently only one error can happen in the function, but if there
522 * will be more of them, this message must go away */
523 msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
524 (char*)&(p_pic->format.i_chroma) );
525 picture_Release( p_pic );
530 return CopyInfoAndRelease( p_outpic, p_pic );
533 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
534 vlc_value_t oldval, vlc_value_t newval,
537 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
538 filter_sys_t *p_sys = (filter_sys_t *)p_data;
540 vlc_mutex_lock( &p_sys->lock );
541 if( !strcmp( psz_var, "contrast" ) )
542 p_sys->f_contrast = newval.f_float;
543 else if( !strcmp( psz_var, "brightness" ) )
544 p_sys->f_brightness = newval.f_float;
545 else if( !strcmp( psz_var, "hue" ) )
546 p_sys->f_hue = newval.f_float;
547 else if( !strcmp( psz_var, "saturation" ) )
548 p_sys->f_saturation = newval.f_float;
549 else if( !strcmp( psz_var, "gamma" ) )
550 p_sys->f_gamma = newval.f_float;
551 else if( !strcmp( psz_var, "brightness-threshold" ) )
552 p_sys->b_brightness_threshold = newval.b_bool;
553 vlc_mutex_unlock( &p_sys->lock );