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 *****************************************************************************/
35 #include "vlc_filter.h"
36 #include "filter_picture.h"
39 # define M_PI 3.14159265358979323846
42 #define eight_times( x ) x x x x x x x x
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Create ( vlc_object_t * );
48 static void Destroy ( vlc_object_t * );
50 static picture_t *FilterPlanar( filter_t *, picture_t * );
51 static picture_t *FilterPacked( filter_t *, picture_t * );
52 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
53 vlc_value_t oldval, vlc_value_t newval,
56 /*****************************************************************************
58 *****************************************************************************/
60 #define THRES_TEXT N_("Brightness threshold")
61 #define THRES_LONGTEXT N_("When this mode is enabled, pixels will be " \
62 "shown as black or white. The threshold value will be the brighness " \
64 #define CONT_TEXT N_("Image contrast (0-2)")
65 #define CONT_LONGTEXT N_("Set the image contrast, between 0 and 2. Defaults to 1.")
66 #define HUE_TEXT N_("Image hue (0-360)")
67 #define HUE_LONGTEXT N_("Set the image hue, between 0 and 360. Defaults to 0.")
68 #define SAT_TEXT N_("Image saturation (0-3)")
69 #define SAT_LONGTEXT N_("Set the image saturation, between 0 and 3. Defaults to 1.")
70 #define LUM_TEXT N_("Image brightness (0-2)")
71 #define LUM_LONGTEXT N_("Set the image brightness, between 0 and 2. Defaults to 1.")
72 #define GAMMA_TEXT N_("Image gamma (0-10)")
73 #define GAMMA_LONGTEXT N_("Set the image gamma, between 0.01 and 10. Defaults to 1.")
76 set_description( _("Image properties filter") );
77 set_shortname( _("Image adjust" ));
78 set_category( CAT_VIDEO );
79 set_subcategory( SUBCAT_VIDEO_VFILTER );
80 set_capability( "video filter2", 0 );
82 add_float_with_range( "contrast", 1.0, 0.0, 2.0, NULL,
83 CONT_TEXT, CONT_LONGTEXT, VLC_FALSE );
84 add_float_with_range( "brightness", 1.0, 0.0, 2.0, NULL,
85 LUM_TEXT, LUM_LONGTEXT, VLC_FALSE );
86 add_integer_with_range( "hue", 0, 0, 360, NULL,
87 HUE_TEXT, HUE_LONGTEXT, VLC_FALSE );
88 add_float_with_range( "saturation", 1.0, 0.0, 3.0, NULL,
89 SAT_TEXT, SAT_LONGTEXT, VLC_FALSE );
90 add_float_with_range( "gamma", 1.0, 0.01, 10.0, NULL,
91 GAMMA_TEXT, GAMMA_LONGTEXT, VLC_FALSE );
93 add_bool( "brightness-threshold", 0, NULL,
94 THRES_TEXT, THRES_LONGTEXT, VLC_FALSE );
96 add_shortcut( "adjust" );
97 set_callbacks( Create, Destroy );
100 static const char *ppsz_filter_options[] = {
101 "contrast", "brightness", "hue", "saturation", "gamma",
102 "brightness-threshold", NULL
105 /*****************************************************************************
106 * filter_sys_t: adjust filter method descriptor
107 *****************************************************************************/
115 vlc_bool_t b_brightness_threshold;
118 /*****************************************************************************
119 * Create: allocates adjust video filter
120 *****************************************************************************/
121 static int Create( vlc_object_t *p_this )
123 filter_t *p_filter = (filter_t *)p_this;
126 switch( p_filter->fmt_in.video.i_chroma )
130 p_filter->pf_video_filter = FilterPlanar;
134 /* Packed YUV 4:2:2 */
135 p_filter->pf_video_filter = FilterPacked;
139 msg_Err( p_filter, "Unsupported input chroma (%4s)",
140 (char*)&(p_filter->fmt_in.video.i_chroma) );
144 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
146 msg_Err( p_filter, "Input and output chromas don't match" );
150 /* Allocate structure */
151 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
152 if( p_filter->p_sys == NULL )
154 msg_Err( p_filter, "out of memory" );
157 p_sys = p_filter->p_sys;
159 /* needed to get options passed in transcode using the
160 * adjust{name=value} syntax */
161 config_ChainParse( p_filter, "", ppsz_filter_options,
164 p_sys->f_contrast = var_CreateGetFloatCommand( p_filter, "contrast" );
165 p_sys->f_brightness = var_CreateGetFloatCommand( p_filter, "brightness" );
166 p_sys->i_hue = var_CreateGetIntegerCommand( p_filter, "hue" );
167 p_sys->f_saturation = var_CreateGetFloatCommand( p_filter, "saturation" );
168 p_sys->f_gamma = var_CreateGetFloatCommand( p_filter, "gamma" );
169 p_sys->b_brightness_threshold =
170 var_CreateGetBoolCommand( p_filter, "brightness-threshold" );
172 var_AddCallback( p_filter, "contrast", AdjustCallback, p_sys );
173 var_AddCallback( p_filter, "brightness", AdjustCallback, p_sys );
174 var_AddCallback( p_filter, "hue", AdjustCallback, p_sys );
175 var_AddCallback( p_filter, "saturation", AdjustCallback, p_sys );
176 var_AddCallback( p_filter, "gamma", AdjustCallback, p_sys );
177 var_AddCallback( p_filter, "brightness-threshold",
178 AdjustCallback, p_sys );
183 /*****************************************************************************
184 * Destroy: destroy adjust video filter
185 *****************************************************************************/
186 static void Destroy( vlc_object_t *p_this )
188 filter_t *p_filter = (filter_t *)p_this;
189 free( p_filter->p_sys );
192 /*****************************************************************************
193 * Run the filter on a Planar YUV picture
194 *****************************************************************************/
195 static picture_t *FilterPlanar( filter_t *p_filter, picture_t *p_pic )
201 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
202 uint8_t *p_out, *p_out_v;
207 int32_t i_cont, i_lum;
208 int i_sat, i_sin, i_cos, i_x, i_y;
211 filter_sys_t *p_sys = p_filter->p_sys;
213 if( !p_pic ) return NULL;
215 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
218 msg_Warn( p_filter, "can't get output picture" );
219 if( p_pic->pf_release )
220 p_pic->pf_release( p_pic );
225 i_cont = (int)( p_sys->f_contrast * 255 );
226 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
227 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
228 i_sat = (int)( p_sys->f_saturation * 256 );
229 f_gamma = 1.0 / p_sys->f_gamma;
230 b_thres = p_sys->b_brightness_threshold;
233 * Threshold mode drops out everything about luma, contrast and gamma.
235 if( b_thres != VLC_TRUE )
238 /* Contrast is a fast but kludged function, so I put this gap to be
240 i_lum += 128 - i_cont / 2;
242 /* Fill the gamma lookup table */
243 for( i = 0 ; i < 256 ; i++ )
245 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
248 /* Fill the luma lookup table */
249 for( i = 0 ; i < 256 ; i++ )
251 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
257 * We get luma as threshold value: the higher it is, the darker is
258 * the image. Should I reverse this?
260 for( i = 0 ; i < 256 ; i++ )
262 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
266 * Desaturates image to avoid that strange yellow halo...
275 p_in = p_pic->p[Y_PLANE].p_pixels;
276 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
277 * p_pic->p[Y_PLANE].i_pitch - 8;
279 p_out = p_outpic->p[Y_PLANE].p_pixels;
281 for( ; p_in < p_in_end ; )
283 p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
285 for( ; p_in < p_line_end ; )
287 /* Do 8 pixels at a time */
288 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
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++ ];
296 for( ; p_in < p_line_end ; )
298 *p_out++ = pi_luma[ *p_in++ ];
301 p_in += p_pic->p[Y_PLANE].i_pitch
302 - p_pic->p[Y_PLANE].i_visible_pitch;
303 p_out += p_outpic->p[Y_PLANE].i_pitch
304 - p_outpic->p[Y_PLANE].i_visible_pitch;
308 * Do the U and V planes
311 p_in = p_pic->p[U_PLANE].p_pixels;
312 p_in_v = p_pic->p[V_PLANE].p_pixels;
313 p_in_end = p_in + p_pic->p[U_PLANE].i_visible_lines
314 * p_pic->p[U_PLANE].i_pitch - 8;
316 p_out = p_outpic->p[U_PLANE].p_pixels;
317 p_out_v = p_outpic->p[V_PLANE].p_pixels;
319 i_sin = sin(f_hue) * 256;
320 i_cos = cos(f_hue) * 256;
322 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
323 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
327 #define WRITE_UV_CLIP() \
328 i_u = *p_in++ ; i_v = *p_in_v++ ; \
329 *p_out++ = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
330 * i_sat) >> 8) + 128); \
331 *p_out_v++ = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
332 * i_sat) >> 8) + 128)
336 for( ; p_in < p_in_end ; )
338 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
340 for( ; p_in < p_line_end ; )
342 /* Do 8 pixels at a time */
343 WRITE_UV_CLIP(); WRITE_UV_CLIP();
344 WRITE_UV_CLIP(); WRITE_UV_CLIP();
345 WRITE_UV_CLIP(); WRITE_UV_CLIP();
346 WRITE_UV_CLIP(); WRITE_UV_CLIP();
351 for( ; p_in < p_line_end ; )
356 p_in += p_pic->p[U_PLANE].i_pitch
357 - p_pic->p[U_PLANE].i_visible_pitch;
358 p_in_v += p_pic->p[V_PLANE].i_pitch
359 - p_pic->p[V_PLANE].i_visible_pitch;
360 p_out += p_outpic->p[U_PLANE].i_pitch
361 - p_outpic->p[U_PLANE].i_visible_pitch;
362 p_out_v += p_outpic->p[V_PLANE].i_pitch
363 - p_outpic->p[V_PLANE].i_visible_pitch;
370 i_u = *p_in++ ; i_v = *p_in_v++ ; \
371 *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
372 * i_sat) >> 8) + 128; \
373 *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
378 for( ; p_in < p_in_end ; )
380 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
382 for( ; p_in < p_line_end ; )
384 /* Do 8 pixels at a time */
385 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
386 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
391 for( ; p_in < p_line_end ; )
396 p_in += p_pic->p[U_PLANE].i_pitch
397 - p_pic->p[U_PLANE].i_visible_pitch;
398 p_in_v += p_pic->p[V_PLANE].i_pitch
399 - p_pic->p[V_PLANE].i_visible_pitch;
400 p_out += p_outpic->p[U_PLANE].i_pitch
401 - p_outpic->p[U_PLANE].i_visible_pitch;
402 p_out_v += p_outpic->p[V_PLANE].i_pitch
403 - p_outpic->p[V_PLANE].i_visible_pitch;
408 p_outpic->date = p_pic->date;
409 p_outpic->b_force = p_pic->b_force;
410 p_outpic->i_nb_fields = p_pic->i_nb_fields;
411 p_outpic->b_progressive = p_pic->b_progressive;
412 p_outpic->b_top_field_first = p_pic->b_top_field_first;
414 if( p_pic->pf_release )
415 p_pic->pf_release( p_pic );
420 /*****************************************************************************
421 * Run the filter on a Packed YUV picture
422 *****************************************************************************/
423 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
429 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
430 uint8_t *p_out, *p_out_v;
431 int i_y_offset, i_u_offset, i_v_offset;
433 int i_lines, i_visible_lines, i_pitch, i_visible_pitch;
438 int32_t i_cont, i_lum;
439 int i_sat, i_sin, i_cos, i_x, i_y;
442 filter_sys_t *p_sys = p_filter->p_sys;
444 if( !p_pic ) return NULL;
446 i_lines = p_pic->p->i_lines;
447 i_visible_lines = p_pic->p->i_visible_lines;
448 i_pitch = p_pic->p->i_pitch;
449 i_visible_pitch = p_pic->p->i_visible_pitch;
451 if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
452 &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
454 msg_Warn( p_filter, "Unsupported input chroma (%4s)",
455 (char*)&(p_pic->format.i_chroma) );
456 if( p_pic->pf_release )
457 p_pic->pf_release( p_pic );
461 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
464 msg_Warn( p_filter, "can't get output picture" );
465 if( p_pic->pf_release )
466 p_pic->pf_release( p_pic );
471 i_cont = (int)( p_sys->f_contrast * 255 );
472 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
473 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
474 i_sat = (int)( p_sys->f_saturation * 256 );
475 f_gamma = 1.0 / p_sys->f_gamma;
476 b_thres = p_sys->b_brightness_threshold;
479 * Threshold mode drops out everything about luma, contrast and gamma.
481 if( b_thres != VLC_TRUE )
484 /* Contrast is a fast but kludged function, so I put this gap to be
486 i_lum += 128 - i_cont / 2;
488 /* Fill the gamma lookup table */
489 for( i = 0 ; i < 256 ; i++ )
491 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
494 /* Fill the luma lookup table */
495 for( i = 0 ; i < 256 ; i++ )
497 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
503 * We get luma as threshold value: the higher it is, the darker is
504 * the image. Should I reverse this?
506 for( i = 0 ; i < 256 ; i++ )
508 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
512 * Desaturates image to avoid that strange yellow halo...
521 p_in = p_pic->p->p_pixels + i_y_offset;
522 p_in_end = p_in + p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 * 4;
524 p_out = p_outpic->p->p_pixels + i_y_offset;
526 for( ; p_in < p_in_end ; )
528 p_line_end = p_in + i_visible_pitch - 8 * 4;
530 for( ; p_in < p_line_end ; )
532 /* Do 8 pixels at a time */
533 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
534 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
535 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
536 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
537 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
538 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
539 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
540 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
545 for( ; p_in < p_line_end ; )
547 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
550 p_in += i_pitch - p_pic->p->i_visible_pitch;
551 p_out += i_pitch - p_outpic->p->i_visible_pitch;
555 * Do the U and V planes
558 p_in = p_pic->p->p_pixels + i_u_offset;
559 p_in_v = p_pic->p->p_pixels + i_v_offset;
560 p_in_end = p_in + i_visible_lines * i_pitch - 8 * 4;
562 p_out = p_outpic->p->p_pixels + i_u_offset;
563 p_out_v = p_outpic->p->p_pixels + i_v_offset;
565 i_sin = sin(f_hue) * 256;
566 i_cos = cos(f_hue) * 256;
568 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
569 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
573 #define WRITE_UV_CLIP() \
574 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
575 *p_out = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
576 * i_sat) >> 8) + 128); \
578 *p_out_v = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
579 * i_sat) >> 8) + 128); \
584 for( ; p_in < p_in_end ; )
586 p_line_end = p_in + i_visible_pitch - 8 * 4;
588 for( ; p_in < p_line_end ; )
590 /* Do 8 pixels at a time */
591 WRITE_UV_CLIP(); WRITE_UV_CLIP();
592 WRITE_UV_CLIP(); WRITE_UV_CLIP();
593 WRITE_UV_CLIP(); WRITE_UV_CLIP();
594 WRITE_UV_CLIP(); WRITE_UV_CLIP();
599 for( ; p_in < p_line_end ; )
604 p_in += i_pitch - i_visible_pitch;
605 p_in_v += i_pitch - i_visible_pitch;
606 p_out += i_pitch - i_visible_pitch;
607 p_out_v += i_pitch - i_visible_pitch;
614 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
615 *p_out = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
616 * i_sat) >> 8) + 128; \
618 *p_out_v = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
619 * i_sat) >> 8) + 128; \
624 for( ; p_in < p_in_end ; )
626 p_line_end = p_in + i_visible_pitch - 8 * 4;
628 for( ; p_in < p_line_end ; )
630 /* Do 8 pixels at a time */
631 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
632 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
637 for( ; p_in < p_line_end ; )
642 p_in += i_pitch - i_visible_pitch;
643 p_in_v += i_pitch - i_visible_pitch;
644 p_out += i_pitch - i_visible_pitch;
645 p_out_v += i_pitch - i_visible_pitch;
650 p_outpic->date = p_pic->date;
651 p_outpic->b_force = p_pic->b_force;
652 p_outpic->i_nb_fields = p_pic->i_nb_fields;
653 p_outpic->b_progressive = p_pic->b_progressive;
654 p_outpic->b_top_field_first = p_pic->b_top_field_first;
656 if( p_pic->pf_release )
657 p_pic->pf_release( 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 filter_sys_t *p_sys = (filter_sys_t *)p_data;
668 if( !strcmp( psz_var, "contrast" ) )
669 p_sys->f_contrast = newval.f_float;
670 else if( !strcmp( psz_var, "brightness" ) )
671 p_sys->f_brightness = newval.f_float;
672 else if( !strcmp( psz_var, "hue" ) )
673 p_sys->i_hue = newval.i_int;
674 else if( !strcmp( psz_var, "saturation" ) )
675 p_sys->f_saturation = newval.f_float;
676 else if( !strcmp( psz_var, "gamma" ) )
677 p_sys->f_gamma = newval.f_float;
678 else if( !strcmp( psz_var, "brightness-threshold" ) )
679 p_sys->b_brightness_threshold = newval.b_bool;