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() */
35 #include <vlc/decoder.h>
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 * );
53 /*****************************************************************************
55 *****************************************************************************/
57 #define THRES_TEXT N_("Brightness threshold")
58 #define THRES_LONGTEXT N_("When this mode is enabled, pixels will be " \
59 "shown as black or white. The threshold value will be the brighness " \
61 #define CONT_TEXT N_("Image contrast (0-2)")
62 #define CONT_LONGTEXT N_("Set the image contrast, between 0 and 2. Defaults to 1.")
63 #define HUE_TEXT N_("Image hue (0-360)")
64 #define HUE_LONGTEXT N_("Set the image hue, between 0 and 360. Defaults to 0.")
65 #define SAT_TEXT N_("Image saturation (0-3)")
66 #define SAT_LONGTEXT N_("Set the image saturation, between 0 and 3. Defaults to 1.")
67 #define LUM_TEXT N_("Image brightness (0-2)")
68 #define LUM_LONGTEXT N_("Set the image brightness, between 0 and 2. Defaults to 1.")
69 #define GAMMA_TEXT N_("Image gamma (0-10)")
70 #define GAMMA_LONGTEXT N_("Set the image gamma, between 0.01 and 10. Defaults to 1.")
73 set_description( _("Image properties filter") );
74 set_shortname( N_("Image adjust" ));
75 set_category( CAT_VIDEO );
76 set_subcategory( SUBCAT_VIDEO_VFILTER2 );
77 set_capability( "video filter2", 0 );
79 add_float_with_range( "contrast", 1.0, 0.0, 2.0, NULL,
80 CONT_TEXT, CONT_LONGTEXT, VLC_FALSE );
81 add_float_with_range( "brightness", 1.0, 0.0, 2.0, NULL,
82 LUM_TEXT, LUM_LONGTEXT, VLC_FALSE );
83 add_integer_with_range( "hue", 0, 0, 360, NULL,
84 HUE_TEXT, HUE_LONGTEXT, VLC_FALSE );
85 add_float_with_range( "saturation", 1.0, 0.0, 3.0, NULL,
86 SAT_TEXT, SAT_LONGTEXT, VLC_FALSE );
87 add_float_with_range( "gamma", 1.0, 0.01, 10.0, NULL,
88 GAMMA_TEXT, GAMMA_LONGTEXT, VLC_FALSE );
90 add_bool( "brightness-threshold", 0, NULL,
91 THRES_TEXT, THRES_LONGTEXT, VLC_FALSE );
93 add_shortcut( "adjust" );
94 set_callbacks( Create, Destroy );
97 static const char *ppsz_filter_options[] = {
98 "contrast", "brightness", "hue", "saturation", "gamma",
99 "brightness-threshold", NULL
102 /*****************************************************************************
103 * filter_sys_t: adjust filter method descriptor
104 *****************************************************************************/
109 inline static int32_t clip( int32_t a )
111 return (a > 255) ? 255 : (a < 0) ? 0 : a;
114 /*****************************************************************************
115 * Create: allocates adjust video thread output method
116 *****************************************************************************
117 * This function allocates and initializes a adjust vout method.
118 *****************************************************************************/
119 static int Create( vlc_object_t *p_this )
121 filter_t *p_filter = (filter_t *)p_this;
123 /* XXX: we might need to add/remove some FOURCCs ... */
124 if( p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0')
125 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V')
126 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','0')
127 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2')
128 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','U','Y','V')
129 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('U','Y','V','Y')
130 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('U','Y','N','V')
131 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','4','2','2')
132 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('c','y','u','v')
133 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','Y','2')
134 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','N','V')
135 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','Y','U')
136 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','1','1')
137 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','1','0')
138 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','U','9')
139 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','M','G','A')
140 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','2')
141 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','2')
142 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','4','4')
143 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','4','4')
144 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P')
145 && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') )
147 msg_Err( p_filter, "Unsupported input chroma (%4s)",
148 (char*)&(p_filter->fmt_in.video.i_chroma) );
152 if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
154 msg_Err( p_filter, "Input and output chromas don't match" );
158 /* Allocate structure */
159 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
160 if( p_filter->p_sys == NULL )
162 msg_Err( p_filter, "out of memory" );
166 p_filter->pf_video_filter = Filter;
168 /* needed to get options passed in transcode using the
169 * adjust{name=value} syntax */
170 sout_CfgParse( p_filter, "", ppsz_filter_options,
173 var_Create( p_filter, "contrast",
174 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
175 var_Create( p_filter, "brightness",
176 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
177 var_Create( p_filter, "hue",
178 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
179 var_Create( p_filter, "saturation",
180 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
181 var_Create( p_filter, "gamma",
182 VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
183 var_Create( p_filter, "brightness-threshold",
184 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
189 /*****************************************************************************
190 * Destroy: destroy adjust video thread output method
191 *****************************************************************************
192 * Terminate an output method created by adjustCreateOutputMethod
193 *****************************************************************************/
194 static void Destroy( vlc_object_t *p_this )
196 filter_t *p_filter = (filter_t *)p_this;
197 free( p_filter->p_sys );
200 /*****************************************************************************
201 * Render: displays previously rendered output
202 *****************************************************************************
203 * This function send the currently rendered image to adjust modified image,
204 * waits until it is displayed and switch the two rendering buffers, preparing
206 *****************************************************************************/
207 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
213 uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
214 uint8_t *p_out, *p_out_v;
219 int32_t i_cont, i_lum;
220 int i_sat, i_sin, i_cos, i_x, i_y;
224 if( !p_pic ) return NULL;
226 p_outpic = p_filter->pf_vout_buffer_new( p_filter );
229 msg_Warn( p_filter, "can't get output picture" );
230 if( p_pic->pf_release )
231 p_pic->pf_release( p_pic );
236 var_Get( p_filter, "contrast", &val );
237 i_cont = (int) ( val.f_float * 255 );
238 var_Get( p_filter, "brightness", &val );
239 i_lum = (int) (( val.f_float - 1.0 ) * 255 );
240 var_Get( p_filter, "hue", &val );
241 f_hue = (float) ( val.i_int * M_PI / 180 );
242 var_Get( p_filter, "saturation", &val );
243 i_sat = (int) (val.f_float * 256 );
244 var_Get( p_filter, "gamma", &val );
245 f_gamma = 1.0 / val.f_float;
246 var_Get( p_filter, "brightness-threshold", &val );
247 b_thres = (vlc_bool_t) ( val.b_bool );
250 * Threshold mode drops out everything about luma, contrast and gamma.
252 if( b_thres != VLC_TRUE )
255 /* Contrast is a fast but kludged function, so I put this gap to be
257 i_lum += 128 - i_cont / 2;
259 /* Fill the gamma lookup table */
260 for( i = 0 ; i < 256 ; i++ )
262 pi_gamma[ i ] = clip( pow(i / 255.0, f_gamma) * 255.0);
265 /* Fill the luma lookup table */
266 for( i = 0 ; i < 256 ; i++ )
268 pi_luma[ i ] = pi_gamma[clip( i_lum + i_cont * i / 256)];
274 * We get luma as threshold value: the higher it is, the darker is
275 * the image. Should I reverse this?
277 for( i = 0 ; i < 256 ; i++ )
279 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
283 * Desaturates image to avoid that strange yellow halo...
292 p_in = p_pic->p[Y_PLANE].p_pixels;
293 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
294 * p_pic->p[Y_PLANE].i_pitch - 8;
296 p_out = p_outpic->p[Y_PLANE].p_pixels;
298 for( ; p_in < p_in_end ; )
300 p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
302 for( ; p_in < p_line_end ; )
304 /* Do 8 pixels at a time */
305 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
306 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
307 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
308 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
313 for( ; p_in < p_line_end ; )
315 *p_out++ = pi_luma[ *p_in++ ];
318 p_in += p_pic->p[Y_PLANE].i_pitch
319 - p_pic->p[Y_PLANE].i_visible_pitch;
320 p_out += p_outpic->p[Y_PLANE].i_pitch
321 - p_outpic->p[Y_PLANE].i_visible_pitch;
325 * Do the U and V planes
328 p_in = p_pic->p[U_PLANE].p_pixels;
329 p_in_v = p_pic->p[V_PLANE].p_pixels;
330 p_in_end = p_in + p_pic->p[U_PLANE].i_visible_lines
331 * p_pic->p[U_PLANE].i_pitch - 8;
333 p_out = p_outpic->p[U_PLANE].p_pixels;
334 p_out_v = p_outpic->p[V_PLANE].p_pixels;
336 i_sin = sin(f_hue) * 256;
337 i_cos = cos(f_hue) * 256;
339 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
340 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
344 #define WRITE_UV_CLIP() \
345 i_u = *p_in++ ; i_v = *p_in_v++ ; \
346 *p_out++ = clip( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
347 * i_sat) >> 8) + 128); \
348 *p_out_v++ = clip( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
349 * i_sat) >> 8) + 128)
353 for( ; p_in < p_in_end ; )
355 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
357 for( ; p_in < p_line_end ; )
359 /* Do 8 pixels at a time */
360 WRITE_UV_CLIP(); WRITE_UV_CLIP();
361 WRITE_UV_CLIP(); WRITE_UV_CLIP();
362 WRITE_UV_CLIP(); WRITE_UV_CLIP();
363 WRITE_UV_CLIP(); WRITE_UV_CLIP();
368 for( ; p_in < p_line_end ; )
373 p_in += p_pic->p[U_PLANE].i_pitch
374 - p_pic->p[U_PLANE].i_visible_pitch;
375 p_in_v += p_pic->p[V_PLANE].i_pitch
376 - p_pic->p[V_PLANE].i_visible_pitch;
377 p_out += p_outpic->p[U_PLANE].i_pitch
378 - p_outpic->p[U_PLANE].i_visible_pitch;
379 p_out_v += p_outpic->p[V_PLANE].i_pitch
380 - p_outpic->p[V_PLANE].i_visible_pitch;
386 i_u = *p_in++ ; i_v = *p_in_v++ ; \
387 *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
388 * i_sat) >> 8) + 128; \
389 *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
394 for( ; p_in < p_in_end ; )
396 p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
398 for( ; p_in < p_line_end ; )
400 /* Do 8 pixels at a time */
401 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
402 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
407 for( ; p_in < p_line_end ; )
412 p_in += p_pic->p[U_PLANE].i_pitch
413 - p_pic->p[U_PLANE].i_visible_pitch;
414 p_in_v += p_pic->p[V_PLANE].i_pitch
415 - p_pic->p[V_PLANE].i_visible_pitch;
416 p_out += p_outpic->p[U_PLANE].i_pitch
417 - p_outpic->p[U_PLANE].i_visible_pitch;
418 p_out_v += p_outpic->p[V_PLANE].i_pitch
419 - p_outpic->p[V_PLANE].i_visible_pitch;
423 p_outpic->date = p_pic->date;
424 p_outpic->b_force = p_pic->b_force;
425 p_outpic->i_nb_fields = p_pic->i_nb_fields;
426 p_outpic->b_progressive = p_pic->b_progressive;
427 p_outpic->b_top_field_first = p_pic->b_top_field_first;
429 if( p_pic->pf_release )
430 p_pic->pf_release( p_pic );