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 filter_sys_t *p_sys = p_filter->p_sys;
194 var_DelCallback( p_filter, "contrast", AdjustCallback, p_sys );
195 var_DelCallback( p_filter, "brightness", AdjustCallback, p_sys );
196 var_DelCallback( p_filter, "hue", AdjustCallback, p_sys );
197 var_DelCallback( p_filter, "saturation", AdjustCallback, p_sys );
198 var_DelCallback( p_filter, "gamma", AdjustCallback, p_sys );
199 var_DelCallback( p_filter, "brightness-threshold",
200 AdjustCallback, p_sys );
205 /*****************************************************************************
206 * Run the filter on a Planar YUV picture
207 *****************************************************************************/
208 static picture_t *FilterPlanar( filter_t *p_filter, picture_t *p_pic )
214 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
215 uint8_t *p_out, *p_out_v;
220 int32_t i_cont, i_lum;
221 int i_sat, i_sin, i_cos, i_x, i_y;
224 filter_sys_t *p_sys = p_filter->p_sys;
226 if( !p_pic ) return NULL;
228 p_outpic = filter_NewPicture( p_filter );
231 picture_Release( p_pic );
236 i_cont = (int)( p_sys->f_contrast * 255 );
237 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
238 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
239 i_sat = (int)( p_sys->f_saturation * 256 );
240 f_gamma = 1.0 / p_sys->f_gamma;
241 b_thres = p_sys->b_brightness_threshold;
244 * Threshold mode drops out everything about luma, contrast and gamma.
246 if( b_thres != true )
249 /* Contrast is a fast but kludged function, so I put this gap to be
251 i_lum += 128 - i_cont / 2;
253 /* Fill the gamma lookup table */
254 for( i = 0 ; i < 256 ; i++ )
256 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
259 /* Fill the luma lookup table */
260 for( i = 0 ; i < 256 ; i++ )
262 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
268 * We get luma as threshold value: the higher it is, the darker is
269 * the image. Should I reverse this?
271 for( i = 0 ; i < 256 ; i++ )
273 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
277 * Desaturates image to avoid that strange yellow halo...
286 p_in = p_pic->p[Y_PLANE].p_pixels;
287 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
288 * p_pic->p[Y_PLANE].i_pitch - 8;
290 p_out = p_outpic->p[Y_PLANE].p_pixels;
292 for( ; p_in < p_in_end ; )
294 p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
296 for( ; p_in < p_line_end ; )
298 /* Do 8 pixels at a time */
299 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
300 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
301 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
302 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
307 for( ; p_in < p_line_end ; )
309 *p_out++ = pi_luma[ *p_in++ ];
312 p_in += p_pic->p[Y_PLANE].i_pitch
313 - p_pic->p[Y_PLANE].i_visible_pitch;
314 p_out += p_outpic->p[Y_PLANE].i_pitch
315 - p_outpic->p[Y_PLANE].i_visible_pitch;
319 * Do the U and V planes
322 p_in = p_pic->p[U_PLANE].p_pixels;
323 p_in_v = p_pic->p[V_PLANE].p_pixels;
324 p_in_end = p_in + p_pic->p[U_PLANE].i_visible_lines
325 * p_pic->p[U_PLANE].i_pitch - 8;
327 p_out = p_outpic->p[U_PLANE].p_pixels;
328 p_out_v = p_outpic->p[V_PLANE].p_pixels;
330 i_sin = sin(f_hue) * 256;
331 i_cos = cos(f_hue) * 256;
333 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
334 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
338 #define WRITE_UV_CLIP() \
339 i_u = *p_in++ ; i_v = *p_in_v++ ; \
340 *p_out++ = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
341 * i_sat) >> 8) + 128); \
342 *p_out_v++ = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
343 * i_sat) >> 8) + 128)
347 for( ; p_in < p_in_end ; )
349 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
351 for( ; p_in < p_line_end ; )
353 /* Do 8 pixels at a time */
354 WRITE_UV_CLIP(); WRITE_UV_CLIP();
355 WRITE_UV_CLIP(); WRITE_UV_CLIP();
356 WRITE_UV_CLIP(); WRITE_UV_CLIP();
357 WRITE_UV_CLIP(); WRITE_UV_CLIP();
362 for( ; p_in < p_line_end ; )
367 p_in += p_pic->p[U_PLANE].i_pitch
368 - p_pic->p[U_PLANE].i_visible_pitch;
369 p_in_v += p_pic->p[V_PLANE].i_pitch
370 - p_pic->p[V_PLANE].i_visible_pitch;
371 p_out += p_outpic->p[U_PLANE].i_pitch
372 - p_outpic->p[U_PLANE].i_visible_pitch;
373 p_out_v += p_outpic->p[V_PLANE].i_pitch
374 - p_outpic->p[V_PLANE].i_visible_pitch;
381 i_u = *p_in++ ; i_v = *p_in_v++ ; \
382 *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
383 * i_sat) >> 8) + 128; \
384 *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
389 for( ; p_in < p_in_end ; )
391 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
393 for( ; p_in < p_line_end ; )
395 /* Do 8 pixels at a time */
396 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
397 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
402 for( ; p_in < p_line_end ; )
407 p_in += p_pic->p[U_PLANE].i_pitch
408 - p_pic->p[U_PLANE].i_visible_pitch;
409 p_in_v += p_pic->p[V_PLANE].i_pitch
410 - p_pic->p[V_PLANE].i_visible_pitch;
411 p_out += p_outpic->p[U_PLANE].i_pitch
412 - p_outpic->p[U_PLANE].i_visible_pitch;
413 p_out_v += p_outpic->p[V_PLANE].i_pitch
414 - p_outpic->p[V_PLANE].i_visible_pitch;
419 return CopyInfoAndRelease( p_outpic, p_pic );
422 /*****************************************************************************
423 * Run the filter on a Packed YUV picture
424 *****************************************************************************/
425 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
431 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
432 uint8_t *p_out, *p_out_v;
433 int i_y_offset, i_u_offset, i_v_offset;
435 int i_lines, i_visible_lines, i_pitch, i_visible_pitch;
440 int32_t i_cont, i_lum;
441 int i_sat, i_sin, i_cos, i_x, i_y;
444 filter_sys_t *p_sys = p_filter->p_sys;
446 if( !p_pic ) return NULL;
448 i_lines = p_pic->p->i_lines;
449 i_visible_lines = p_pic->p->i_visible_lines;
450 i_pitch = p_pic->p->i_pitch;
451 i_visible_pitch = p_pic->p->i_visible_pitch;
453 if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
454 &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
456 msg_Warn( p_filter, "Unsupported input chroma (%4s)",
457 (char*)&(p_pic->format.i_chroma) );
459 picture_Release( p_pic );
463 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
466 msg_Warn( p_filter, "can't get output picture" );
468 picture_Release( p_pic );
473 i_cont = (int)( p_sys->f_contrast * 255 );
474 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
475 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
476 i_sat = (int)( p_sys->f_saturation * 256 );
477 f_gamma = 1.0 / p_sys->f_gamma;
478 b_thres = p_sys->b_brightness_threshold;
481 * Threshold mode drops out everything about luma, contrast and gamma.
483 if( b_thres != true )
486 /* Contrast is a fast but kludged function, so I put this gap to be
488 i_lum += 128 - i_cont / 2;
490 /* Fill the gamma lookup table */
491 for( i = 0 ; i < 256 ; i++ )
493 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
496 /* Fill the luma lookup table */
497 for( i = 0 ; i < 256 ; i++ )
499 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
505 * We get luma as threshold value: the higher it is, the darker is
506 * the image. Should I reverse this?
508 for( i = 0 ; i < 256 ; i++ )
510 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
514 * Desaturates image to avoid that strange yellow halo...
523 p_in = p_pic->p->p_pixels + i_y_offset;
524 p_in_end = p_in + p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 * 4;
526 p_out = p_outpic->p->p_pixels + i_y_offset;
528 for( ; p_in < p_in_end ; )
530 p_line_end = p_in + i_visible_pitch - 8 * 4;
532 for( ; p_in < p_line_end ; )
534 /* Do 8 pixels at a time */
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;
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;
547 for( ; p_in < p_line_end ; )
549 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
552 p_in += i_pitch - p_pic->p->i_visible_pitch;
553 p_out += i_pitch - p_outpic->p->i_visible_pitch;
557 * Do the U and V planes
560 p_in = p_pic->p->p_pixels + i_u_offset;
561 p_in_v = p_pic->p->p_pixels + i_v_offset;
562 p_in_end = p_in + i_visible_lines * i_pitch - 8 * 4;
564 p_out = p_outpic->p->p_pixels + i_u_offset;
565 p_out_v = p_outpic->p->p_pixels + i_v_offset;
567 i_sin = sin(f_hue) * 256;
568 i_cos = cos(f_hue) * 256;
570 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
571 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
575 #define WRITE_UV_CLIP() \
576 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
577 *p_out = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
578 * i_sat) >> 8) + 128); \
580 *p_out_v = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
581 * i_sat) >> 8) + 128); \
586 for( ; p_in < p_in_end ; )
588 p_line_end = p_in + i_visible_pitch - 8 * 4;
590 for( ; p_in < p_line_end ; )
592 /* Do 8 pixels at a time */
593 WRITE_UV_CLIP(); WRITE_UV_CLIP();
594 WRITE_UV_CLIP(); WRITE_UV_CLIP();
595 WRITE_UV_CLIP(); WRITE_UV_CLIP();
596 WRITE_UV_CLIP(); WRITE_UV_CLIP();
601 for( ; p_in < p_line_end ; )
606 p_in += i_pitch - i_visible_pitch;
607 p_in_v += i_pitch - i_visible_pitch;
608 p_out += i_pitch - i_visible_pitch;
609 p_out_v += i_pitch - i_visible_pitch;
616 i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
617 *p_out = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
618 * i_sat) >> 8) + 128; \
620 *p_out_v = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
621 * i_sat) >> 8) + 128; \
626 for( ; p_in < p_in_end ; )
628 p_line_end = p_in + i_visible_pitch - 8 * 4;
630 for( ; p_in < p_line_end ; )
632 /* Do 8 pixels at a time */
633 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
634 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
639 for( ; p_in < p_line_end ; )
644 p_in += i_pitch - i_visible_pitch;
645 p_in_v += i_pitch - i_visible_pitch;
646 p_out += i_pitch - i_visible_pitch;
647 p_out_v += i_pitch - i_visible_pitch;
652 return CopyInfoAndRelease( p_outpic, p_pic );
655 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
656 vlc_value_t oldval, vlc_value_t newval,
659 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
660 filter_sys_t *p_sys = (filter_sys_t *)p_data;
662 if( !strcmp( psz_var, "contrast" ) )
663 p_sys->f_contrast = newval.f_float;
664 else if( !strcmp( psz_var, "brightness" ) )
665 p_sys->f_brightness = newval.f_float;
666 else if( !strcmp( psz_var, "hue" ) )
667 p_sys->i_hue = newval.i_int;
668 else if( !strcmp( psz_var, "saturation" ) )
669 p_sys->f_saturation = newval.f_float;
670 else if( !strcmp( psz_var, "gamma" ) )
671 p_sys->f_gamma = newval.f_float;
672 else if( !strcmp( psz_var, "brightness-threshold" ) )
673 p_sys->b_brightness_threshold = newval.b_bool;