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 *****************************************************************************/
121 bool b_brightness_threshold;
124 /*****************************************************************************
125 * Create: allocates adjust video filter
126 *****************************************************************************/
127 static int Create( vlc_object_t *p_this )
129 filter_t *p_filter = (filter_t *)p_this;
132 switch( p_filter->fmt_in.video.i_chroma )
136 p_filter->pf_video_filter = FilterPlanar;
140 /* Packed YUV 4:2:2 */
141 p_filter->pf_video_filter = FilterPacked;
145 msg_Err( p_filter, "Unsupported input chroma (%4s)",
146 (char*)&(p_filter->fmt_in.video.i_chroma) );
150 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
152 msg_Err( p_filter, "Input and output chromas don't match" );
156 /* Allocate structure */
157 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
158 if( p_filter->p_sys == NULL )
160 p_sys = p_filter->p_sys;
162 /* needed to get options passed in transcode using the
163 * adjust{name=value} syntax */
164 config_ChainParse( p_filter, "", ppsz_filter_options,
167 p_sys->f_contrast = var_CreateGetFloatCommand( p_filter, "contrast" );
168 p_sys->f_brightness = var_CreateGetFloatCommand( p_filter, "brightness" );
169 p_sys->i_hue = var_CreateGetIntegerCommand( p_filter, "hue" );
170 p_sys->f_saturation = var_CreateGetFloatCommand( p_filter, "saturation" );
171 p_sys->f_gamma = var_CreateGetFloatCommand( p_filter, "gamma" );
172 p_sys->b_brightness_threshold =
173 var_CreateGetBoolCommand( p_filter, "brightness-threshold" );
175 var_AddCallback( p_filter, "contrast", AdjustCallback, p_sys );
176 var_AddCallback( p_filter, "brightness", AdjustCallback, p_sys );
177 var_AddCallback( p_filter, "hue", AdjustCallback, p_sys );
178 var_AddCallback( p_filter, "saturation", AdjustCallback, p_sys );
179 var_AddCallback( p_filter, "gamma", AdjustCallback, p_sys );
180 var_AddCallback( p_filter, "brightness-threshold",
181 AdjustCallback, p_sys );
186 /*****************************************************************************
187 * Destroy: destroy adjust video filter
188 *****************************************************************************/
189 static void Destroy( vlc_object_t *p_this )
191 filter_t *p_filter = (filter_t *)p_this;
192 free( p_filter->p_sys );
195 /*****************************************************************************
196 * Run the filter on a Planar YUV picture
197 *****************************************************************************/
198 static picture_t *FilterPlanar( filter_t *p_filter, picture_t *p_pic )
204 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
205 uint8_t *p_out, *p_out_v;
210 int32_t i_cont, i_lum;
211 int i_sat, i_sin, i_cos, i_x, i_y;
214 filter_sys_t *p_sys = p_filter->p_sys;
216 if( !p_pic ) return NULL;
218 p_outpic = filter_NewPicture( p_filter );
221 picture_Release( p_pic );
226 i_cont = (int)( p_sys->f_contrast * 255 );
227 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
228 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
229 i_sat = (int)( p_sys->f_saturation * 256 );
230 f_gamma = 1.0 / p_sys->f_gamma;
231 b_thres = p_sys->b_brightness_threshold;
234 * Threshold mode drops out everything about luma, contrast and gamma.
236 if( b_thres != true )
239 /* Contrast is a fast but kludged function, so I put this gap to be
241 i_lum += 128 - i_cont / 2;
243 /* Fill the gamma lookup table */
244 for( i = 0 ; i < 256 ; i++ )
246 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
249 /* Fill the luma lookup table */
250 for( i = 0 ; i < 256 ; i++ )
252 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
258 * We get luma as threshold value: the higher it is, the darker is
259 * the image. Should I reverse this?
261 for( i = 0 ; i < 256 ; i++ )
263 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
267 * Desaturates image to avoid that strange yellow halo...
276 p_in = p_pic->p[Y_PLANE].p_pixels;
277 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
278 * p_pic->p[Y_PLANE].i_pitch - 8;
280 p_out = p_outpic->p[Y_PLANE].p_pixels;
282 for( ; p_in < p_in_end ; )
284 p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
286 for( ; p_in < p_line_end ; )
288 /* Do 8 pixels at a time */
289 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
290 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
291 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
292 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
297 for( ; p_in < p_line_end ; )
299 *p_out++ = pi_luma[ *p_in++ ];
302 p_in += p_pic->p[Y_PLANE].i_pitch
303 - p_pic->p[Y_PLANE].i_visible_pitch;
304 p_out += p_outpic->p[Y_PLANE].i_pitch
305 - p_outpic->p[Y_PLANE].i_visible_pitch;
309 * Do the U and V planes
312 p_in = p_pic->p[U_PLANE].p_pixels;
313 p_in_v = p_pic->p[V_PLANE].p_pixels;
314 p_in_end = p_in + p_pic->p[U_PLANE].i_visible_lines
315 * p_pic->p[U_PLANE].i_pitch - 8;
317 p_out = p_outpic->p[U_PLANE].p_pixels;
318 p_out_v = p_outpic->p[V_PLANE].p_pixels;
320 i_sin = sin(f_hue) * 256;
321 i_cos = cos(f_hue) * 256;
323 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
324 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
328 #define WRITE_UV_CLIP() \
329 i_u = *p_in++ ; i_v = *p_in_v++ ; \
330 *p_out++ = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
331 * i_sat) >> 8) + 128); \
332 *p_out_v++ = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
333 * i_sat) >> 8) + 128)
337 for( ; p_in < p_in_end ; )
339 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
341 for( ; p_in < p_line_end ; )
343 /* Do 8 pixels at a time */
344 WRITE_UV_CLIP(); WRITE_UV_CLIP();
345 WRITE_UV_CLIP(); WRITE_UV_CLIP();
346 WRITE_UV_CLIP(); WRITE_UV_CLIP();
347 WRITE_UV_CLIP(); WRITE_UV_CLIP();
352 for( ; p_in < p_line_end ; )
357 p_in += p_pic->p[U_PLANE].i_pitch
358 - p_pic->p[U_PLANE].i_visible_pitch;
359 p_in_v += p_pic->p[V_PLANE].i_pitch
360 - p_pic->p[V_PLANE].i_visible_pitch;
361 p_out += p_outpic->p[U_PLANE].i_pitch
362 - p_outpic->p[U_PLANE].i_visible_pitch;
363 p_out_v += p_outpic->p[V_PLANE].i_pitch
364 - p_outpic->p[V_PLANE].i_visible_pitch;
371 i_u = *p_in++ ; i_v = *p_in_v++ ; \
372 *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
373 * i_sat) >> 8) + 128; \
374 *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
379 for( ; p_in < p_in_end ; )
381 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
383 for( ; p_in < p_line_end ; )
385 /* Do 8 pixels at a time */
386 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
387 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
392 for( ; p_in < p_line_end ; )
397 p_in += p_pic->p[U_PLANE].i_pitch
398 - p_pic->p[U_PLANE].i_visible_pitch;
399 p_in_v += p_pic->p[V_PLANE].i_pitch
400 - p_pic->p[V_PLANE].i_visible_pitch;
401 p_out += p_outpic->p[U_PLANE].i_pitch
402 - p_outpic->p[U_PLANE].i_visible_pitch;
403 p_out_v += p_outpic->p[V_PLANE].i_pitch
404 - p_outpic->p[V_PLANE].i_visible_pitch;
409 return CopyInfoAndRelease( p_outpic, p_pic );
412 /*****************************************************************************
413 * Run the filter on a Packed YUV picture
414 *****************************************************************************/
415 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
421 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
422 uint8_t *p_out, *p_out_v;
423 int i_y_offset, i_u_offset, i_v_offset;
425 int i_lines, i_visible_lines, i_pitch, i_visible_pitch;
430 int32_t i_cont, i_lum;
431 int i_sat, i_sin, i_cos, i_x, i_y;
434 filter_sys_t *p_sys = p_filter->p_sys;
436 if( !p_pic ) return NULL;
438 i_lines = p_pic->p->i_lines;
439 i_visible_lines = p_pic->p->i_visible_lines;
440 i_pitch = p_pic->p->i_pitch;
441 i_visible_pitch = p_pic->p->i_visible_pitch;
443 if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
444 &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
446 msg_Warn( p_filter, "Unsupported input chroma (%4s)",
447 (char*)&(p_pic->format.i_chroma) );
449 picture_Release( p_pic );
453 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
456 msg_Warn( p_filter, "can't get output picture" );
458 picture_Release( p_pic );
463 i_cont = (int)( p_sys->f_contrast * 255 );
464 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
465 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
466 i_sat = (int)( p_sys->f_saturation * 256 );
467 f_gamma = 1.0 / p_sys->f_gamma;
468 b_thres = p_sys->b_brightness_threshold;
471 * Threshold mode drops out everything about luma, contrast and gamma.
473 if( b_thres != true )
476 /* Contrast is a fast but kludged function, so I put this gap to be
478 i_lum += 128 - i_cont / 2;
480 /* Fill the gamma lookup table */
481 for( i = 0 ; i < 256 ; i++ )
483 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
486 /* Fill the luma lookup table */
487 for( i = 0 ; i < 256 ; i++ )
489 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
495 * We get luma as threshold value: the higher it is, the darker is
496 * the image. Should I reverse this?
498 for( i = 0 ; i < 256 ; i++ )
500 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
504 * Desaturates image to avoid that strange yellow halo...
513 p_in = p_pic->p->p_pixels + i_y_offset;
514 p_in_end = p_in + p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 * 4;
516 p_out = p_outpic->p->p_pixels + i_y_offset;
518 for( ; p_in < p_in_end ; )
520 p_line_end = p_in + i_visible_pitch - 8 * 4;
522 for( ; p_in < p_line_end ; )
524 /* Do 8 pixels at a time */
525 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
526 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
527 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
528 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
529 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
530 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
531 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
532 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
537 for( ; p_in < p_line_end ; )
539 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
542 p_in += i_pitch - p_pic->p->i_visible_pitch;
543 p_out += i_pitch - p_outpic->p->i_visible_pitch;
547 * Do the U and V planes
550 p_in = p_pic->p->p_pixels + i_u_offset;
551 p_in_v = p_pic->p->p_pixels + i_v_offset;
552 p_in_end = p_in + i_visible_lines * i_pitch - 8 * 4;
554 p_out = p_outpic->p->p_pixels + i_u_offset;
555 p_out_v = p_outpic->p->p_pixels + i_v_offset;
557 i_sin = sin(f_hue) * 256;
558 i_cos = cos(f_hue) * 256;
560 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
561 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
565 #define WRITE_UV_CLIP() \
566 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
567 *p_out = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
568 * i_sat) >> 8) + 128); \
570 *p_out_v = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
571 * i_sat) >> 8) + 128); \
576 for( ; p_in < p_in_end ; )
578 p_line_end = p_in + i_visible_pitch - 8 * 4;
580 for( ; p_in < p_line_end ; )
582 /* Do 8 pixels at a time */
583 WRITE_UV_CLIP(); WRITE_UV_CLIP();
584 WRITE_UV_CLIP(); WRITE_UV_CLIP();
585 WRITE_UV_CLIP(); WRITE_UV_CLIP();
586 WRITE_UV_CLIP(); WRITE_UV_CLIP();
591 for( ; p_in < p_line_end ; )
596 p_in += i_pitch - i_visible_pitch;
597 p_in_v += i_pitch - i_visible_pitch;
598 p_out += i_pitch - i_visible_pitch;
599 p_out_v += i_pitch - i_visible_pitch;
606 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
607 *p_out = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
608 * i_sat) >> 8) + 128; \
610 *p_out_v = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
611 * i_sat) >> 8) + 128; \
616 for( ; p_in < p_in_end ; )
618 p_line_end = p_in + i_visible_pitch - 8 * 4;
620 for( ; p_in < p_line_end ; )
622 /* Do 8 pixels at a time */
623 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
624 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
629 for( ; p_in < p_line_end ; )
634 p_in += i_pitch - i_visible_pitch;
635 p_in_v += i_pitch - i_visible_pitch;
636 p_out += i_pitch - i_visible_pitch;
637 p_out_v += i_pitch - i_visible_pitch;
642 return CopyInfoAndRelease( p_outpic, p_pic );
645 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
646 vlc_value_t oldval, vlc_value_t newval,
649 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
650 filter_sys_t *p_sys = (filter_sys_t *)p_data;
652 if( !strcmp( psz_var, "contrast" ) )
653 p_sys->f_contrast = newval.f_float;
654 else if( !strcmp( psz_var, "brightness" ) )
655 p_sys->f_brightness = newval.f_float;
656 else if( !strcmp( psz_var, "hue" ) )
657 p_sys->i_hue = newval.i_int;
658 else if( !strcmp( psz_var, "saturation" ) )
659 p_sys->f_saturation = newval.f_float;
660 else if( !strcmp( psz_var, "gamma" ) )
661 p_sys->f_gamma = newval.f_float;
662 else if( !strcmp( psz_var, "brightness-threshold" ) )
663 p_sys->b_brightness_threshold = newval.b_bool;