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 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
37 #include "vlc_filter.h"
40 # define M_PI 3.14159265358979323846
43 #define eight_times( x ) x x x x x x x x
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Create ( vlc_object_t * );
49 static void Destroy ( vlc_object_t * );
51 static picture_t *Filter( 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 thread output method
120 *****************************************************************************
121 * This function allocates and initializes a adjust vout method.
122 *****************************************************************************/
123 static int Create( vlc_object_t *p_this )
125 filter_t *p_filter = (filter_t *)p_this;
128 /* XXX: we might need to add/remove some FOURCCs ... */
129 if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0')
130 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V')
131 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','0')
132 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2')
133 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','U','Y','V')
134 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('U','Y','V','Y')
135 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('U','Y','N','V')
136 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','4','2','2')
137 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('c','y','u','v')
138 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','Y','2')
139 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','N','V')
140 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','Y','U')
141 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','1','1')
142 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','1','0')
143 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','U','9')
144 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','M','G','A')
145 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','2')
146 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','2')
147 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','4','4')
148 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','4','4')
149 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P')
150 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') )
152 msg_Err( p_filter, "Unsupported input chroma (%4s)",
153 (char*)&(p_filter->fmt_in.video.i_chroma) );
157 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
159 msg_Err( p_filter, "Input and output chromas don't match" );
163 /* Allocate structure */
164 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
165 if( p_filter->p_sys == NULL )
167 msg_Err( p_filter, "out of memory" );
170 p_sys = p_filter->p_sys;
172 p_filter->pf_video_filter = Filter;
174 /* needed to get options passed in transcode using the
175 * adjust{name=value} syntax */
176 config_ChainParse( p_filter, "", ppsz_filter_options,
179 p_sys->f_contrast = var_CreateGetFloatCommand( p_filter, "contrast" );
180 p_sys->f_brightness = var_CreateGetFloatCommand( p_filter, "brightness" );
181 p_sys->i_hue = var_CreateGetIntegerCommand( p_filter, "hue" );
182 p_sys->f_saturation = var_CreateGetFloatCommand( p_filter, "saturation" );
183 p_sys->f_gamma = var_CreateGetFloatCommand( p_filter, "gamma" );
184 p_sys->b_brightness_threshold =
185 var_CreateGetBoolCommand( p_filter, "brightness-threshold" );
187 var_AddCallback( p_filter, "contrast", AdjustCallback, p_sys );
188 var_AddCallback( p_filter, "brightness", AdjustCallback, p_sys );
189 var_AddCallback( p_filter, "hue", AdjustCallback, p_sys );
190 var_AddCallback( p_filter, "saturation", AdjustCallback, p_sys );
191 var_AddCallback( p_filter, "gamma", AdjustCallback, p_sys );
192 var_AddCallback( p_filter, "brightness-threshold",
193 AdjustCallback, p_sys );
198 /*****************************************************************************
199 * Destroy: destroy adjust video thread output method
200 *****************************************************************************
201 * Terminate an output method created by adjustCreateOutputMethod
202 *****************************************************************************/
203 static void Destroy( vlc_object_t *p_this )
205 filter_t *p_filter = (filter_t *)p_this;
206 free( p_filter->p_sys );
209 /*****************************************************************************
210 * Render: displays previously rendered output
211 *****************************************************************************
212 * This function send the currently rendered image to adjust modified image,
213 * waits until it is displayed and switch the two rendering buffers, preparing
215 *****************************************************************************/
216 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
222 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
223 uint8_t *p_out, *p_out_v;
228 int32_t i_cont, i_lum;
229 int i_sat, i_sin, i_cos, i_x, i_y;
232 filter_sys_t *p_sys = p_filter->p_sys;
234 if( !p_pic ) return NULL;
236 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
239 msg_Warn( p_filter, "can't get output picture" );
240 if( p_pic->pf_release )
241 p_pic->pf_release( p_pic );
246 i_cont = (int)( p_sys->f_contrast * 255 );
247 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
248 f_hue = (float)( p_sys->i_hue * M_PI / 180 );
249 i_sat = (int)( p_sys->f_saturation * 256 );
250 f_gamma = 1.0 / p_sys->f_gamma;
251 b_thres = p_sys->b_brightness_threshold;
254 * Threshold mode drops out everything about luma, contrast and gamma.
256 if( b_thres != VLC_TRUE )
259 /* Contrast is a fast but kludged function, so I put this gap to be
261 i_lum += 128 - i_cont / 2;
263 /* Fill the gamma lookup table */
264 for( i = 0 ; i < 256 ; i++ )
266 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
269 /* Fill the luma lookup table */
270 for( i = 0 ; i < 256 ; i++ )
272 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
278 * We get luma as threshold value: the higher it is, the darker is
279 * the image. Should I reverse this?
281 for( i = 0 ; i < 256 ; i++ )
283 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
287 * Desaturates image to avoid that strange yellow halo...
296 p_in = p_pic->p[Y_PLANE].p_pixels;
297 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
298 * p_pic->p[Y_PLANE].i_pitch - 8;
300 p_out = p_outpic->p[Y_PLANE].p_pixels;
302 for( ; p_in < p_in_end ; )
304 p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
306 for( ; p_in < p_line_end ; )
308 /* Do 8 pixels at a time */
309 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
310 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
311 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
312 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
317 for( ; p_in < p_line_end ; )
319 *p_out++ = pi_luma[ *p_in++ ];
322 p_in += p_pic->p[Y_PLANE].i_pitch
323 - p_pic->p[Y_PLANE].i_visible_pitch;
324 p_out += p_outpic->p[Y_PLANE].i_pitch
325 - p_outpic->p[Y_PLANE].i_visible_pitch;
329 * Do the U and V planes
332 p_in = p_pic->p[U_PLANE].p_pixels;
333 p_in_v = p_pic->p[V_PLANE].p_pixels;
334 p_in_end = p_in + p_pic->p[U_PLANE].i_visible_lines
335 * p_pic->p[U_PLANE].i_pitch - 8;
337 p_out = p_outpic->p[U_PLANE].p_pixels;
338 p_out_v = p_outpic->p[V_PLANE].p_pixels;
340 i_sin = sin(f_hue) * 256;
341 i_cos = cos(f_hue) * 256;
343 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
344 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
348 #define WRITE_UV_CLIP() \
349 i_u = *p_in++ ; i_v = *p_in_v++ ; \
350 *p_out++ = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
351 * i_sat) >> 8) + 128); \
352 *p_out_v++ = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
353 * i_sat) >> 8) + 128)
357 for( ; p_in < p_in_end ; )
359 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
361 for( ; p_in < p_line_end ; )
363 /* Do 8 pixels at a time */
364 WRITE_UV_CLIP(); WRITE_UV_CLIP();
365 WRITE_UV_CLIP(); WRITE_UV_CLIP();
366 WRITE_UV_CLIP(); WRITE_UV_CLIP();
367 WRITE_UV_CLIP(); WRITE_UV_CLIP();
372 for( ; p_in < p_line_end ; )
377 p_in += p_pic->p[U_PLANE].i_pitch
378 - p_pic->p[U_PLANE].i_visible_pitch;
379 p_in_v += p_pic->p[V_PLANE].i_pitch
380 - p_pic->p[V_PLANE].i_visible_pitch;
381 p_out += p_outpic->p[U_PLANE].i_pitch
382 - p_outpic->p[U_PLANE].i_visible_pitch;
383 p_out_v += p_outpic->p[V_PLANE].i_pitch
384 - p_outpic->p[V_PLANE].i_visible_pitch;
390 i_u = *p_in++ ; i_v = *p_in_v++ ; \
391 *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
392 * i_sat) >> 8) + 128; \
393 *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
398 for( ; p_in < p_in_end ; )
400 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
402 for( ; p_in < p_line_end ; )
404 /* Do 8 pixels at a time */
405 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
406 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
411 for( ; p_in < p_line_end ; )
416 p_in += p_pic->p[U_PLANE].i_pitch
417 - p_pic->p[U_PLANE].i_visible_pitch;
418 p_in_v += p_pic->p[V_PLANE].i_pitch
419 - p_pic->p[V_PLANE].i_visible_pitch;
420 p_out += p_outpic->p[U_PLANE].i_pitch
421 - p_outpic->p[U_PLANE].i_visible_pitch;
422 p_out_v += p_outpic->p[V_PLANE].i_pitch
423 - p_outpic->p[V_PLANE].i_visible_pitch;
427 p_outpic->date = p_pic->date;
428 p_outpic->b_force = p_pic->b_force;
429 p_outpic->i_nb_fields = p_pic->i_nb_fields;
430 p_outpic->b_progressive = p_pic->b_progressive;
431 p_outpic->b_top_field_first = p_pic->b_top_field_first;
433 if( p_pic->pf_release )
434 p_pic->pf_release( p_pic );
439 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
440 vlc_value_t oldval, vlc_value_t newval,
443 filter_sys_t *p_sys = (filter_sys_t *)p_data;
445 if( !strcmp( psz_var, "contrast" ) )
446 p_sys->f_contrast = newval.f_float;
447 else if( !strcmp( psz_var, "brightness" ) )
448 p_sys->f_brightness = newval.f_float;
449 else if( !strcmp( psz_var, "hue" ) )
450 p_sys->i_hue = newval.i_int;
451 else if( !strcmp( psz_var, "saturation" ) )
452 p_sys->f_saturation = newval.f_float;
453 else if( !strcmp( psz_var, "gamma" ) )
454 p_sys->f_gamma = newval.f_float;
455 else if( !strcmp( psz_var, "brightness-threshold" ) )
456 p_sys->b_brightness_threshold = newval.b_bool;