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 );
85 add_float_with_range( "brightness", 1.0, 0.0, 2.0, NULL,
86 LUM_TEXT, LUM_LONGTEXT, VLC_FALSE );
88 add_integer_with_range( "hue", 0, 0, 360, NULL,
89 HUE_TEXT, HUE_LONGTEXT, VLC_FALSE );
91 add_float_with_range( "saturation", 1.0, 0.0, 3.0, NULL,
92 SAT_TEXT, SAT_LONGTEXT, VLC_FALSE );
94 add_float_with_range( "gamma", 1.0, 0.01, 10.0, NULL,
95 GAMMA_TEXT, GAMMA_LONGTEXT, VLC_FALSE );
98 add_bool( "brightness-threshold", 0, NULL,
99 THRES_TEXT, THRES_LONGTEXT, VLC_FALSE );
102 add_shortcut( "adjust" );
103 set_callbacks( Create, Destroy );
106 static const char *ppsz_filter_options[] = {
107 "contrast", "brightness", "hue", "saturation", "gamma",
108 "brightness-threshold", NULL
111 /*****************************************************************************
112 * filter_sys_t: adjust filter method descriptor
113 *****************************************************************************/
121 vlc_bool_t 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 msg_Err( p_filter, "out of memory" );
163 p_sys = p_filter->p_sys;
165 /* needed to get options passed in transcode using the
166 * adjust{name=value} syntax */
167 config_ChainParse( p_filter, "", ppsz_filter_options,
170 p_sys->f_contrast = var_CreateGetFloatCommand( p_filter, "contrast" );
171 p_sys->f_brightness = var_CreateGetFloatCommand( p_filter, "brightness" );
172 p_sys->i_hue = var_CreateGetIntegerCommand( p_filter, "hue" );
173 p_sys->f_saturation = var_CreateGetFloatCommand( p_filter, "saturation" );
174 p_sys->f_gamma = var_CreateGetFloatCommand( p_filter, "gamma" );
175 p_sys->b_brightness_threshold =
176 var_CreateGetBoolCommand( p_filter, "brightness-threshold" );
178 var_AddCallback( p_filter, "contrast", AdjustCallback, p_sys );
179 var_AddCallback( p_filter, "brightness", AdjustCallback, p_sys );
180 var_AddCallback( p_filter, "hue", AdjustCallback, p_sys );
181 var_AddCallback( p_filter, "saturation", AdjustCallback, p_sys );
182 var_AddCallback( p_filter, "gamma", AdjustCallback, p_sys );
183 var_AddCallback( p_filter, "brightness-threshold",
184 AdjustCallback, p_sys );
189 /*****************************************************************************
190 * Destroy: destroy adjust video filter
191 *****************************************************************************/
192 static void Destroy( vlc_object_t *p_this )
194 filter_t *p_filter = (filter_t *)p_this;
195 free( p_filter->p_sys );
198 /*****************************************************************************
199 * Run the filter on a Planar YUV picture
200 *****************************************************************************/
201 static picture_t *FilterPlanar( filter_t *p_filter, picture_t *p_pic )
207 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
208 uint8_t *p_out, *p_out_v;
213 int32_t i_cont, i_lum;
214 int i_sat, i_sin, i_cos, i_x, i_y;
217 filter_sys_t *p_sys = p_filter->p_sys;
219 if( !p_pic ) return NULL;
221 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
224 msg_Warn( p_filter, "can't get output picture" );
225 if( p_pic->pf_release )
226 p_pic->pf_release( p_pic );
231 i_cont = (int)( p_sys->f_contrast * 255 );
232 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
233 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
234 i_sat = (int)( p_sys->f_saturation * 256 );
235 f_gamma = 1.0 / p_sys->f_gamma;
236 b_thres = p_sys->b_brightness_threshold;
239 * Threshold mode drops out everything about luma, contrast and gamma.
241 if( b_thres != VLC_TRUE )
244 /* Contrast is a fast but kludged function, so I put this gap to be
246 i_lum += 128 - i_cont / 2;
248 /* Fill the gamma lookup table */
249 for( i = 0 ; i < 256 ; i++ )
251 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
254 /* Fill the luma lookup table */
255 for( i = 0 ; i < 256 ; i++ )
257 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
263 * We get luma as threshold value: the higher it is, the darker is
264 * the image. Should I reverse this?
266 for( i = 0 ; i < 256 ; i++ )
268 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
272 * Desaturates image to avoid that strange yellow halo...
281 p_in = p_pic->p[Y_PLANE].p_pixels;
282 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
283 * p_pic->p[Y_PLANE].i_pitch - 8;
285 p_out = p_outpic->p[Y_PLANE].p_pixels;
287 for( ; p_in < p_in_end ; )
289 p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
291 for( ; p_in < p_line_end ; )
293 /* Do 8 pixels at a time */
294 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
295 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
296 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
297 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
302 for( ; p_in < p_line_end ; )
304 *p_out++ = pi_luma[ *p_in++ ];
307 p_in += p_pic->p[Y_PLANE].i_pitch
308 - p_pic->p[Y_PLANE].i_visible_pitch;
309 p_out += p_outpic->p[Y_PLANE].i_pitch
310 - p_outpic->p[Y_PLANE].i_visible_pitch;
314 * Do the U and V planes
317 p_in = p_pic->p[U_PLANE].p_pixels;
318 p_in_v = p_pic->p[V_PLANE].p_pixels;
319 p_in_end = p_in + p_pic->p[U_PLANE].i_visible_lines
320 * p_pic->p[U_PLANE].i_pitch - 8;
322 p_out = p_outpic->p[U_PLANE].p_pixels;
323 p_out_v = p_outpic->p[V_PLANE].p_pixels;
325 i_sin = sin(f_hue) * 256;
326 i_cos = cos(f_hue) * 256;
328 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
329 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
333 #define WRITE_UV_CLIP() \
334 i_u = *p_in++ ; i_v = *p_in_v++ ; \
335 *p_out++ = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
336 * i_sat) >> 8) + 128); \
337 *p_out_v++ = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
338 * i_sat) >> 8) + 128)
342 for( ; p_in < p_in_end ; )
344 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
346 for( ; p_in < p_line_end ; )
348 /* Do 8 pixels at a time */
349 WRITE_UV_CLIP(); WRITE_UV_CLIP();
350 WRITE_UV_CLIP(); WRITE_UV_CLIP();
351 WRITE_UV_CLIP(); WRITE_UV_CLIP();
352 WRITE_UV_CLIP(); WRITE_UV_CLIP();
357 for( ; p_in < p_line_end ; )
362 p_in += p_pic->p[U_PLANE].i_pitch
363 - p_pic->p[U_PLANE].i_visible_pitch;
364 p_in_v += p_pic->p[V_PLANE].i_pitch
365 - p_pic->p[V_PLANE].i_visible_pitch;
366 p_out += p_outpic->p[U_PLANE].i_pitch
367 - p_outpic->p[U_PLANE].i_visible_pitch;
368 p_out_v += p_outpic->p[V_PLANE].i_pitch
369 - p_outpic->p[V_PLANE].i_visible_pitch;
376 i_u = *p_in++ ; i_v = *p_in_v++ ; \
377 *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
378 * i_sat) >> 8) + 128; \
379 *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
384 for( ; p_in < p_in_end ; )
386 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
388 for( ; p_in < p_line_end ; )
390 /* Do 8 pixels at a time */
391 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
392 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
397 for( ; p_in < p_line_end ; )
402 p_in += p_pic->p[U_PLANE].i_pitch
403 - p_pic->p[U_PLANE].i_visible_pitch;
404 p_in_v += p_pic->p[V_PLANE].i_pitch
405 - p_pic->p[V_PLANE].i_visible_pitch;
406 p_out += p_outpic->p[U_PLANE].i_pitch
407 - p_outpic->p[U_PLANE].i_visible_pitch;
408 p_out_v += p_outpic->p[V_PLANE].i_pitch
409 - p_outpic->p[V_PLANE].i_visible_pitch;
414 p_outpic->date = p_pic->date;
415 p_outpic->b_force = p_pic->b_force;
416 p_outpic->i_nb_fields = p_pic->i_nb_fields;
417 p_outpic->b_progressive = p_pic->b_progressive;
418 p_outpic->b_top_field_first = p_pic->b_top_field_first;
420 if( p_pic->pf_release )
421 p_pic->pf_release( p_pic );
426 /*****************************************************************************
427 * Run the filter on a Packed YUV picture
428 *****************************************************************************/
429 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
435 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
436 uint8_t *p_out, *p_out_v;
437 int i_y_offset, i_u_offset, i_v_offset;
439 int i_lines, i_visible_lines, i_pitch, i_visible_pitch;
444 int32_t i_cont, i_lum;
445 int i_sat, i_sin, i_cos, i_x, i_y;
448 filter_sys_t *p_sys = p_filter->p_sys;
450 if( !p_pic ) return NULL;
452 i_lines = p_pic->p->i_lines;
453 i_visible_lines = p_pic->p->i_visible_lines;
454 i_pitch = p_pic->p->i_pitch;
455 i_visible_pitch = p_pic->p->i_visible_pitch;
457 if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
458 &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
460 msg_Warn( p_filter, "Unsupported input chroma (%4s)",
461 (char*)&(p_pic->format.i_chroma) );
462 if( p_pic->pf_release )
463 p_pic->pf_release( p_pic );
467 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
470 msg_Warn( p_filter, "can't get output picture" );
471 if( p_pic->pf_release )
472 p_pic->pf_release( p_pic );
477 i_cont = (int)( p_sys->f_contrast * 255 );
478 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
479 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
480 i_sat = (int)( p_sys->f_saturation * 256 );
481 f_gamma = 1.0 / p_sys->f_gamma;
482 b_thres = p_sys->b_brightness_threshold;
485 * Threshold mode drops out everything about luma, contrast and gamma.
487 if( b_thres != VLC_TRUE )
490 /* Contrast is a fast but kludged function, so I put this gap to be
492 i_lum += 128 - i_cont / 2;
494 /* Fill the gamma lookup table */
495 for( i = 0 ; i < 256 ; i++ )
497 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
500 /* Fill the luma lookup table */
501 for( i = 0 ; i < 256 ; i++ )
503 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
509 * We get luma as threshold value: the higher it is, the darker is
510 * the image. Should I reverse this?
512 for( i = 0 ; i < 256 ; i++ )
514 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
518 * Desaturates image to avoid that strange yellow halo...
527 p_in = p_pic->p->p_pixels + i_y_offset;
528 p_in_end = p_in + p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 * 4;
530 p_out = p_outpic->p->p_pixels + i_y_offset;
532 for( ; p_in < p_in_end ; )
534 p_line_end = p_in + i_visible_pitch - 8 * 4;
536 for( ; p_in < p_line_end ; )
538 /* Do 8 pixels at a time */
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;
541 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
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;
551 for( ; p_in < p_line_end ; )
553 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
556 p_in += i_pitch - p_pic->p->i_visible_pitch;
557 p_out += i_pitch - p_outpic->p->i_visible_pitch;
561 * Do the U and V planes
564 p_in = p_pic->p->p_pixels + i_u_offset;
565 p_in_v = p_pic->p->p_pixels + i_v_offset;
566 p_in_end = p_in + i_visible_lines * i_pitch - 8 * 4;
568 p_out = p_outpic->p->p_pixels + i_u_offset;
569 p_out_v = p_outpic->p->p_pixels + i_v_offset;
571 i_sin = sin(f_hue) * 256;
572 i_cos = cos(f_hue) * 256;
574 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
575 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
579 #define WRITE_UV_CLIP() \
580 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
581 *p_out = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
582 * i_sat) >> 8) + 128); \
584 *p_out_v = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
585 * i_sat) >> 8) + 128); \
590 for( ; p_in < p_in_end ; )
592 p_line_end = p_in + i_visible_pitch - 8 * 4;
594 for( ; p_in < p_line_end ; )
596 /* Do 8 pixels at a time */
597 WRITE_UV_CLIP(); WRITE_UV_CLIP();
598 WRITE_UV_CLIP(); WRITE_UV_CLIP();
599 WRITE_UV_CLIP(); WRITE_UV_CLIP();
600 WRITE_UV_CLIP(); WRITE_UV_CLIP();
605 for( ; p_in < p_line_end ; )
610 p_in += i_pitch - i_visible_pitch;
611 p_in_v += i_pitch - i_visible_pitch;
612 p_out += i_pitch - i_visible_pitch;
613 p_out_v += i_pitch - i_visible_pitch;
620 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
621 *p_out = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
622 * i_sat) >> 8) + 128; \
624 *p_out_v = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
625 * i_sat) >> 8) + 128; \
630 for( ; p_in < p_in_end ; )
632 p_line_end = p_in + i_visible_pitch - 8 * 4;
634 for( ; p_in < p_line_end ; )
636 /* Do 8 pixels at a time */
637 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
638 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
643 for( ; p_in < p_line_end ; )
648 p_in += i_pitch - i_visible_pitch;
649 p_in_v += i_pitch - i_visible_pitch;
650 p_out += i_pitch - i_visible_pitch;
651 p_out_v += i_pitch - i_visible_pitch;
656 p_outpic->date = p_pic->date;
657 p_outpic->b_force = p_pic->b_force;
658 p_outpic->i_nb_fields = p_pic->i_nb_fields;
659 p_outpic->b_progressive = p_pic->b_progressive;
660 p_outpic->b_top_field_first = p_pic->b_top_field_first;
662 if( p_pic->pf_release )
663 p_pic->pf_release( p_pic );
668 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
669 vlc_value_t oldval, vlc_value_t newval,
672 filter_sys_t *p_sys = (filter_sys_t *)p_data;
674 if( !strcmp( psz_var, "contrast" ) )
675 p_sys->f_contrast = newval.f_float;
676 else if( !strcmp( psz_var, "brightness" ) )
677 p_sys->f_brightness = newval.f_float;
678 else if( !strcmp( psz_var, "hue" ) )
679 p_sys->i_hue = newval.i_int;
680 else if( !strcmp( psz_var, "saturation" ) )
681 p_sys->f_saturation = newval.f_float;
682 else if( !strcmp( psz_var, "gamma" ) )
683 p_sys->f_gamma = newval.f_float;
684 else if( !strcmp( psz_var, "brightness-threshold" ) )
685 p_sys->b_brightness_threshold = newval.b_bool;