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 = p_filter->pf_vout_buffer_new( p_filter );
221 msg_Warn( p_filter, "can't get output picture" );
222 picture_Release( p_pic );
227 i_cont = (int)( p_sys->f_contrast * 255 );
228 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
229 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
230 i_sat = (int)( p_sys->f_saturation * 256 );
231 f_gamma = 1.0 / p_sys->f_gamma;
232 b_thres = p_sys->b_brightness_threshold;
235 * Threshold mode drops out everything about luma, contrast and gamma.
237 if( b_thres != true )
240 /* Contrast is a fast but kludged function, so I put this gap to be
242 i_lum += 128 - i_cont / 2;
244 /* Fill the gamma lookup table */
245 for( i = 0 ; i < 256 ; i++ )
247 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
250 /* Fill the luma lookup table */
251 for( i = 0 ; i < 256 ; i++ )
253 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
259 * We get luma as threshold value: the higher it is, the darker is
260 * the image. Should I reverse this?
262 for( i = 0 ; i < 256 ; i++ )
264 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
268 * Desaturates image to avoid that strange yellow halo...
277 p_in = p_pic->p[Y_PLANE].p_pixels;
278 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
279 * p_pic->p[Y_PLANE].i_pitch - 8;
281 p_out = p_outpic->p[Y_PLANE].p_pixels;
283 for( ; p_in < p_in_end ; )
285 p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
287 for( ; p_in < p_line_end ; )
289 /* Do 8 pixels at a time */
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++ ];
293 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
298 for( ; p_in < p_line_end ; )
300 *p_out++ = pi_luma[ *p_in++ ];
303 p_in += p_pic->p[Y_PLANE].i_pitch
304 - p_pic->p[Y_PLANE].i_visible_pitch;
305 p_out += p_outpic->p[Y_PLANE].i_pitch
306 - p_outpic->p[Y_PLANE].i_visible_pitch;
310 * Do the U and V planes
313 p_in = p_pic->p[U_PLANE].p_pixels;
314 p_in_v = p_pic->p[V_PLANE].p_pixels;
315 p_in_end = p_in + p_pic->p[U_PLANE].i_visible_lines
316 * p_pic->p[U_PLANE].i_pitch - 8;
318 p_out = p_outpic->p[U_PLANE].p_pixels;
319 p_out_v = p_outpic->p[V_PLANE].p_pixels;
321 i_sin = sin(f_hue) * 256;
322 i_cos = cos(f_hue) * 256;
324 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
325 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
329 #define WRITE_UV_CLIP() \
330 i_u = *p_in++ ; i_v = *p_in_v++ ; \
331 *p_out++ = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
332 * i_sat) >> 8) + 128); \
333 *p_out_v++ = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
334 * i_sat) >> 8) + 128)
338 for( ; p_in < p_in_end ; )
340 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
342 for( ; p_in < p_line_end ; )
344 /* Do 8 pixels at a time */
345 WRITE_UV_CLIP(); WRITE_UV_CLIP();
346 WRITE_UV_CLIP(); WRITE_UV_CLIP();
347 WRITE_UV_CLIP(); WRITE_UV_CLIP();
348 WRITE_UV_CLIP(); WRITE_UV_CLIP();
353 for( ; p_in < p_line_end ; )
358 p_in += p_pic->p[U_PLANE].i_pitch
359 - p_pic->p[U_PLANE].i_visible_pitch;
360 p_in_v += p_pic->p[V_PLANE].i_pitch
361 - p_pic->p[V_PLANE].i_visible_pitch;
362 p_out += p_outpic->p[U_PLANE].i_pitch
363 - p_outpic->p[U_PLANE].i_visible_pitch;
364 p_out_v += p_outpic->p[V_PLANE].i_pitch
365 - p_outpic->p[V_PLANE].i_visible_pitch;
372 i_u = *p_in++ ; i_v = *p_in_v++ ; \
373 *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
374 * i_sat) >> 8) + 128; \
375 *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
380 for( ; p_in < p_in_end ; )
382 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
384 for( ; p_in < p_line_end ; )
386 /* Do 8 pixels at a time */
387 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
388 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
393 for( ; p_in < p_line_end ; )
398 p_in += p_pic->p[U_PLANE].i_pitch
399 - p_pic->p[U_PLANE].i_visible_pitch;
400 p_in_v += p_pic->p[V_PLANE].i_pitch
401 - p_pic->p[V_PLANE].i_visible_pitch;
402 p_out += p_outpic->p[U_PLANE].i_pitch
403 - p_outpic->p[U_PLANE].i_visible_pitch;
404 p_out_v += p_outpic->p[V_PLANE].i_pitch
405 - p_outpic->p[V_PLANE].i_visible_pitch;
410 return CopyInfoAndRelease( p_outpic, p_pic );
413 /*****************************************************************************
414 * Run the filter on a Packed YUV picture
415 *****************************************************************************/
416 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
422 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
423 uint8_t *p_out, *p_out_v;
424 int i_y_offset, i_u_offset, i_v_offset;
426 int i_lines, i_visible_lines, i_pitch, i_visible_pitch;
431 int32_t i_cont, i_lum;
432 int i_sat, i_sin, i_cos, i_x, i_y;
435 filter_sys_t *p_sys = p_filter->p_sys;
437 if( !p_pic ) return NULL;
439 i_lines = p_pic->p->i_lines;
440 i_visible_lines = p_pic->p->i_visible_lines;
441 i_pitch = p_pic->p->i_pitch;
442 i_visible_pitch = p_pic->p->i_visible_pitch;
444 if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
445 &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
447 msg_Warn( p_filter, "Unsupported input chroma (%4s)",
448 (char*)&(p_pic->format.i_chroma) );
450 picture_Release( p_pic );
454 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
457 msg_Warn( p_filter, "can't get output picture" );
459 picture_Release( p_pic );
464 i_cont = (int)( p_sys->f_contrast * 255 );
465 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
466 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
467 i_sat = (int)( p_sys->f_saturation * 256 );
468 f_gamma = 1.0 / p_sys->f_gamma;
469 b_thres = p_sys->b_brightness_threshold;
472 * Threshold mode drops out everything about luma, contrast and gamma.
474 if( b_thres != true )
477 /* Contrast is a fast but kludged function, so I put this gap to be
479 i_lum += 128 - i_cont / 2;
481 /* Fill the gamma lookup table */
482 for( i = 0 ; i < 256 ; i++ )
484 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
487 /* Fill the luma lookup table */
488 for( i = 0 ; i < 256 ; i++ )
490 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
496 * We get luma as threshold value: the higher it is, the darker is
497 * the image. Should I reverse this?
499 for( i = 0 ; i < 256 ; i++ )
501 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
505 * Desaturates image to avoid that strange yellow halo...
514 p_in = p_pic->p->p_pixels + i_y_offset;
515 p_in_end = p_in + p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 * 4;
517 p_out = p_outpic->p->p_pixels + i_y_offset;
519 for( ; p_in < p_in_end ; )
521 p_line_end = p_in + i_visible_pitch - 8 * 4;
523 for( ; p_in < p_line_end ; )
525 /* Do 8 pixels at a time */
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;
533 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
538 for( ; p_in < p_line_end ; )
540 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
543 p_in += i_pitch - p_pic->p->i_visible_pitch;
544 p_out += i_pitch - p_outpic->p->i_visible_pitch;
548 * Do the U and V planes
551 p_in = p_pic->p->p_pixels + i_u_offset;
552 p_in_v = p_pic->p->p_pixels + i_v_offset;
553 p_in_end = p_in + i_visible_lines * i_pitch - 8 * 4;
555 p_out = p_outpic->p->p_pixels + i_u_offset;
556 p_out_v = p_outpic->p->p_pixels + i_v_offset;
558 i_sin = sin(f_hue) * 256;
559 i_cos = cos(f_hue) * 256;
561 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
562 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
566 #define WRITE_UV_CLIP() \
567 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
568 *p_out = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
569 * i_sat) >> 8) + 128); \
571 *p_out_v = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
572 * i_sat) >> 8) + 128); \
577 for( ; p_in < p_in_end ; )
579 p_line_end = p_in + i_visible_pitch - 8 * 4;
581 for( ; p_in < p_line_end ; )
583 /* Do 8 pixels at a time */
584 WRITE_UV_CLIP(); WRITE_UV_CLIP();
585 WRITE_UV_CLIP(); WRITE_UV_CLIP();
586 WRITE_UV_CLIP(); WRITE_UV_CLIP();
587 WRITE_UV_CLIP(); WRITE_UV_CLIP();
592 for( ; p_in < p_line_end ; )
597 p_in += i_pitch - i_visible_pitch;
598 p_in_v += i_pitch - i_visible_pitch;
599 p_out += i_pitch - i_visible_pitch;
600 p_out_v += i_pitch - i_visible_pitch;
607 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
608 *p_out = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
609 * i_sat) >> 8) + 128; \
611 *p_out_v = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
612 * i_sat) >> 8) + 128; \
617 for( ; p_in < p_in_end ; )
619 p_line_end = p_in + i_visible_pitch - 8 * 4;
621 for( ; p_in < p_line_end ; )
623 /* Do 8 pixels at a time */
624 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
625 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
630 for( ; p_in < p_line_end ; )
635 p_in += i_pitch - i_visible_pitch;
636 p_in_v += i_pitch - i_visible_pitch;
637 p_out += i_pitch - i_visible_pitch;
638 p_out_v += i_pitch - i_visible_pitch;
643 return CopyInfoAndRelease( p_outpic, p_pic );
646 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
647 vlc_value_t oldval, vlc_value_t newval,
650 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
651 filter_sys_t *p_sys = (filter_sys_t *)p_data;
653 if( !strcmp( psz_var, "contrast" ) )
654 p_sys->f_contrast = newval.f_float;
655 else if( !strcmp( psz_var, "brightness" ) )
656 p_sys->f_brightness = newval.f_float;
657 else if( !strcmp( psz_var, "hue" ) )
658 p_sys->i_hue = newval.i_int;
659 else if( !strcmp( psz_var, "saturation" ) )
660 p_sys->f_saturation = newval.f_float;
661 else if( !strcmp( psz_var, "gamma" ) )
662 p_sys->f_gamma = newval.f_float;
663 else if( !strcmp( psz_var, "brightness-threshold" ) )
664 p_sys->b_brightness_threshold = newval.b_bool;