1 /*****************************************************************************
2 * adjust.c : Contrast/Hue/Saturation/Brightness video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2006 the VideoLAN team
7 * Authors: Simon Latapie <garf@via.ecp.fr>
8 * Antoine Cellerier <dionoea -at- videolan d0t org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
41 #include "vlc_filter.h"
42 #include "filter_picture.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 brighness " \
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, NULL,
89 CONT_TEXT, CONT_LONGTEXT, false )
90 add_float_with_range( "brightness", 1.0, 0.0, 2.0, NULL,
91 LUM_TEXT, LUM_LONGTEXT, false )
92 add_integer_with_range( "hue", 0, 0, 360, NULL,
93 HUE_TEXT, HUE_LONGTEXT, false )
94 add_float_with_range( "saturation", 1.0, 0.0, 3.0, NULL,
95 SAT_TEXT, SAT_LONGTEXT, false )
96 add_float_with_range( "gamma", 1.0, 0.01, 10.0, NULL,
97 GAMMA_TEXT, GAMMA_LONGTEXT, false )
99 add_bool( "brightness-threshold", 0, NULL,
100 THRES_TEXT, THRES_LONGTEXT, false )
102 add_shortcut( "adjust" )
103 set_callbacks( Create, Destroy )
106 static const char *const ppsz_filter_options[] = {
107 "contrast", "brightness", "hue", "saturation", "gamma",
108 "brightness-threshold", NULL
111 /*****************************************************************************
112 * filter_sys_t: adjust filter method descriptor
113 *****************************************************************************/
122 bool b_brightness_threshold;
125 /*****************************************************************************
126 * Create: allocates adjust video filter
127 *****************************************************************************/
128 static int Create( vlc_object_t *p_this )
130 filter_t *p_filter = (filter_t *)p_this;
133 switch( p_filter->fmt_in.video.i_chroma )
137 p_filter->pf_video_filter = FilterPlanar;
141 /* Packed YUV 4:2:2 */
142 p_filter->pf_video_filter = FilterPacked;
146 msg_Err( p_filter, "Unsupported input chroma (%4s)",
147 (char*)&(p_filter->fmt_in.video.i_chroma) );
151 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
153 msg_Err( p_filter, "Input and output chromas don't match" );
157 /* Allocate structure */
158 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
159 if( p_filter->p_sys == NULL )
161 p_sys = p_filter->p_sys;
163 /* needed to get options passed in transcode using the
164 * adjust{name=value} syntax */
165 config_ChainParse( p_filter, "", ppsz_filter_options,
168 p_sys->f_contrast = var_CreateGetFloatCommand( p_filter, "contrast" );
169 p_sys->f_brightness = var_CreateGetFloatCommand( p_filter, "brightness" );
170 p_sys->i_hue = var_CreateGetIntegerCommand( p_filter, "hue" );
171 p_sys->f_saturation = var_CreateGetFloatCommand( p_filter, "saturation" );
172 p_sys->f_gamma = var_CreateGetFloatCommand( p_filter, "gamma" );
173 p_sys->b_brightness_threshold =
174 var_CreateGetBoolCommand( p_filter, "brightness-threshold" );
176 vlc_mutex_init( &p_sys->lock );
177 var_AddCallback( p_filter, "contrast", AdjustCallback, p_sys );
178 var_AddCallback( p_filter, "brightness", AdjustCallback, p_sys );
179 var_AddCallback( p_filter, "hue", AdjustCallback, p_sys );
180 var_AddCallback( p_filter, "saturation", AdjustCallback, p_sys );
181 var_AddCallback( p_filter, "gamma", AdjustCallback, p_sys );
182 var_AddCallback( p_filter, "brightness-threshold",
183 AdjustCallback, p_sys );
188 /*****************************************************************************
189 * Destroy: destroy adjust video filter
190 *****************************************************************************/
191 static void Destroy( vlc_object_t *p_this )
193 filter_t *p_filter = (filter_t *)p_this;
194 filter_sys_t *p_sys = p_filter->p_sys;
196 var_DelCallback( p_filter, "contrast", AdjustCallback, p_sys );
197 var_DelCallback( p_filter, "brightness", AdjustCallback, p_sys );
198 var_DelCallback( p_filter, "hue", AdjustCallback, p_sys );
199 var_DelCallback( p_filter, "saturation", AdjustCallback, p_sys );
200 var_DelCallback( p_filter, "gamma", AdjustCallback, p_sys );
201 var_DelCallback( p_filter, "brightness-threshold",
202 AdjustCallback, p_sys );
204 vlc_mutex_destroy( &p_sys->lock );
208 /*****************************************************************************
209 * Run the filter on a Planar YUV picture
210 *****************************************************************************/
211 static picture_t *FilterPlanar( filter_t *p_filter, picture_t *p_pic )
217 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
218 uint8_t *p_out, *p_out_v;
223 int32_t i_cont, i_lum;
224 int i_sat, i_sin, i_cos, i_x, i_y;
227 filter_sys_t *p_sys = p_filter->p_sys;
229 if( !p_pic ) return NULL;
231 p_outpic = filter_NewPicture( p_filter );
234 picture_Release( p_pic );
239 vlc_mutex_lock( &p_sys->lock );
240 i_cont = (int)( p_sys->f_contrast * 255 );
241 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
242 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
243 i_sat = (int)( p_sys->f_saturation * 256 );
244 f_gamma = 1.0 / p_sys->f_gamma;
245 b_thres = p_sys->b_brightness_threshold;
246 vlc_mutex_unlock( &p_sys->lock );
249 * Threshold mode drops out everything about luma, contrast and gamma.
251 if( b_thres != true )
254 /* Contrast is a fast but kludged function, so I put this gap to be
256 i_lum += 128 - i_cont / 2;
258 /* Fill the gamma lookup table */
259 for( i = 0 ; i < 256 ; i++ )
261 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
264 /* Fill the luma lookup table */
265 for( i = 0 ; i < 256 ; i++ )
267 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
273 * We get luma as threshold value: the higher it is, the darker is
274 * the image. Should I reverse this?
276 for( i = 0 ; i < 256 ; i++ )
278 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
282 * Desaturates image to avoid that strange yellow halo...
291 p_in = p_pic->p[Y_PLANE].p_pixels;
292 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
293 * p_pic->p[Y_PLANE].i_pitch - 8;
295 p_out = p_outpic->p[Y_PLANE].p_pixels;
297 for( ; p_in < p_in_end ; )
299 p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
301 for( ; p_in < p_line_end ; )
303 /* Do 8 pixels at a time */
304 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
305 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
306 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
307 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
312 for( ; p_in < p_line_end ; )
314 *p_out++ = pi_luma[ *p_in++ ];
317 p_in += p_pic->p[Y_PLANE].i_pitch
318 - p_pic->p[Y_PLANE].i_visible_pitch;
319 p_out += p_outpic->p[Y_PLANE].i_pitch
320 - p_outpic->p[Y_PLANE].i_visible_pitch;
324 * Do the U and V planes
327 p_in = p_pic->p[U_PLANE].p_pixels;
328 p_in_v = p_pic->p[V_PLANE].p_pixels;
329 p_in_end = p_in + p_pic->p[U_PLANE].i_visible_lines
330 * p_pic->p[U_PLANE].i_pitch - 8;
332 p_out = p_outpic->p[U_PLANE].p_pixels;
333 p_out_v = p_outpic->p[V_PLANE].p_pixels;
335 i_sin = sin(f_hue) * 256;
336 i_cos = cos(f_hue) * 256;
338 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
339 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
343 #define WRITE_UV_CLIP() \
344 i_u = *p_in++ ; i_v = *p_in_v++ ; \
345 *p_out++ = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
346 * i_sat) >> 8) + 128); \
347 *p_out_v++ = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
348 * i_sat) >> 8) + 128)
352 for( ; p_in < p_in_end ; )
354 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
356 for( ; p_in < p_line_end ; )
358 /* Do 8 pixels at a time */
359 WRITE_UV_CLIP(); WRITE_UV_CLIP();
360 WRITE_UV_CLIP(); WRITE_UV_CLIP();
361 WRITE_UV_CLIP(); WRITE_UV_CLIP();
362 WRITE_UV_CLIP(); WRITE_UV_CLIP();
367 for( ; p_in < p_line_end ; )
372 p_in += p_pic->p[U_PLANE].i_pitch
373 - p_pic->p[U_PLANE].i_visible_pitch;
374 p_in_v += p_pic->p[V_PLANE].i_pitch
375 - p_pic->p[V_PLANE].i_visible_pitch;
376 p_out += p_outpic->p[U_PLANE].i_pitch
377 - p_outpic->p[U_PLANE].i_visible_pitch;
378 p_out_v += p_outpic->p[V_PLANE].i_pitch
379 - p_outpic->p[V_PLANE].i_visible_pitch;
386 i_u = *p_in++ ; i_v = *p_in_v++ ; \
387 *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
388 * i_sat) >> 8) + 128; \
389 *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
394 for( ; p_in < p_in_end ; )
396 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
398 for( ; p_in < p_line_end ; )
400 /* Do 8 pixels at a time */
401 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
402 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
407 for( ; p_in < p_line_end ; )
412 p_in += p_pic->p[U_PLANE].i_pitch
413 - p_pic->p[U_PLANE].i_visible_pitch;
414 p_in_v += p_pic->p[V_PLANE].i_pitch
415 - p_pic->p[V_PLANE].i_visible_pitch;
416 p_out += p_outpic->p[U_PLANE].i_pitch
417 - p_outpic->p[U_PLANE].i_visible_pitch;
418 p_out_v += p_outpic->p[V_PLANE].i_pitch
419 - p_outpic->p[V_PLANE].i_visible_pitch;
424 return CopyInfoAndRelease( p_outpic, p_pic );
427 /*****************************************************************************
428 * Run the filter on a Packed YUV picture
429 *****************************************************************************/
430 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
436 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
437 uint8_t *p_out, *p_out_v;
438 int i_y_offset, i_u_offset, i_v_offset;
440 int i_lines, i_visible_lines, i_pitch, i_visible_pitch;
445 int32_t i_cont, i_lum;
446 int i_sat, i_sin, i_cos, i_x, i_y;
449 filter_sys_t *p_sys = p_filter->p_sys;
451 if( !p_pic ) return NULL;
453 i_lines = p_pic->p->i_lines;
454 i_visible_lines = p_pic->p->i_visible_lines;
455 i_pitch = p_pic->p->i_pitch;
456 i_visible_pitch = p_pic->p->i_visible_pitch;
458 if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
459 &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
461 msg_Warn( p_filter, "Unsupported input chroma (%4s)",
462 (char*)&(p_pic->format.i_chroma) );
464 picture_Release( p_pic );
468 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
471 msg_Warn( p_filter, "can't get output picture" );
473 picture_Release( p_pic );
478 vlc_mutex_lock( &p_sys->lock );
479 i_cont = (int)( p_sys->f_contrast * 255 );
480 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
481 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
482 i_sat = (int)( p_sys->f_saturation * 256 );
483 f_gamma = 1.0 / p_sys->f_gamma;
484 b_thres = p_sys->b_brightness_threshold;
485 vlc_mutex_unlock( &p_sys->lock );
488 * Threshold mode drops out everything about luma, contrast and gamma.
490 if( b_thres != true )
493 /* Contrast is a fast but kludged function, so I put this gap to be
495 i_lum += 128 - i_cont / 2;
497 /* Fill the gamma lookup table */
498 for( i = 0 ; i < 256 ; i++ )
500 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
503 /* Fill the luma lookup table */
504 for( i = 0 ; i < 256 ; i++ )
506 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
512 * We get luma as threshold value: the higher it is, the darker is
513 * the image. Should I reverse this?
515 for( i = 0 ; i < 256 ; i++ )
517 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
521 * Desaturates image to avoid that strange yellow halo...
530 p_in = p_pic->p->p_pixels + i_y_offset;
531 p_in_end = p_in + p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 * 4;
533 p_out = p_outpic->p->p_pixels + i_y_offset;
535 for( ; p_in < p_in_end ; )
537 p_line_end = p_in + i_visible_pitch - 8 * 4;
539 for( ; p_in < p_line_end ; )
541 /* Do 8 pixels at a time */
542 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
543 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
544 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
545 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
546 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
547 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
548 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
549 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
554 for( ; p_in < p_line_end ; )
556 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
559 p_in += i_pitch - p_pic->p->i_visible_pitch;
560 p_out += i_pitch - p_outpic->p->i_visible_pitch;
564 * Do the U and V planes
567 p_in = p_pic->p->p_pixels + i_u_offset;
568 p_in_v = p_pic->p->p_pixels + i_v_offset;
569 p_in_end = p_in + i_visible_lines * i_pitch - 8 * 4;
571 p_out = p_outpic->p->p_pixels + i_u_offset;
572 p_out_v = p_outpic->p->p_pixels + i_v_offset;
574 i_sin = sin(f_hue) * 256;
575 i_cos = cos(f_hue) * 256;
577 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
578 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
582 #define WRITE_UV_CLIP() \
583 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
584 *p_out = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
585 * i_sat) >> 8) + 128); \
587 *p_out_v = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
588 * i_sat) >> 8) + 128); \
593 for( ; p_in < p_in_end ; )
595 p_line_end = p_in + i_visible_pitch - 8 * 4;
597 for( ; p_in < p_line_end ; )
599 /* Do 8 pixels at a time */
600 WRITE_UV_CLIP(); WRITE_UV_CLIP();
601 WRITE_UV_CLIP(); WRITE_UV_CLIP();
602 WRITE_UV_CLIP(); WRITE_UV_CLIP();
603 WRITE_UV_CLIP(); WRITE_UV_CLIP();
608 for( ; p_in < p_line_end ; )
613 p_in += i_pitch - i_visible_pitch;
614 p_in_v += i_pitch - i_visible_pitch;
615 p_out += i_pitch - i_visible_pitch;
616 p_out_v += i_pitch - i_visible_pitch;
623 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
624 *p_out = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
625 * i_sat) >> 8) + 128; \
627 *p_out_v = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
628 * i_sat) >> 8) + 128; \
633 for( ; p_in < p_in_end ; )
635 p_line_end = p_in + i_visible_pitch - 8 * 4;
637 for( ; p_in < p_line_end ; )
639 /* Do 8 pixels at a time */
640 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
641 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
646 for( ; p_in < p_line_end ; )
651 p_in += i_pitch - i_visible_pitch;
652 p_in_v += i_pitch - i_visible_pitch;
653 p_out += i_pitch - i_visible_pitch;
654 p_out_v += i_pitch - i_visible_pitch;
659 return CopyInfoAndRelease( p_outpic, p_pic );
662 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
663 vlc_value_t oldval, vlc_value_t newval,
666 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
667 filter_sys_t *p_sys = (filter_sys_t *)p_data;
669 vlc_mutex_lock( &p_sys->lock );
670 if( !strcmp( psz_var, "contrast" ) )
671 p_sys->f_contrast = newval.f_float;
672 else if( !strcmp( psz_var, "brightness" ) )
673 p_sys->f_brightness = newval.f_float;
674 else if( !strcmp( psz_var, "hue" ) )
675 p_sys->i_hue = newval.i_int;
676 else if( !strcmp( psz_var, "saturation" ) )
677 p_sys->f_saturation = newval.f_float;
678 else if( !strcmp( psz_var, "gamma" ) )
679 p_sys->f_gamma = newval.f_float;
680 else if( !strcmp( psz_var, "brightness-threshold" ) )
681 p_sys->b_brightness_threshold = newval.b_bool;
682 vlc_mutex_unlock( &p_sys->lock );