1 /*****************************************************************************
2 * adjust.c : Contrast/Hue/Saturation/Brightness video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2006 VLC authors and VideoLAN
7 * Authors: Simon Latapie <garf@via.ecp.fr>
8 * Antoine Cellerier <dionoea -at- videolan d0t org>
9 * Martin Briza <gamajun@seznam.cz> (SSE)
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_atomic.h>
38 #include <vlc_plugin.h>
39 #include <vlc_filter.h>
40 #include <vlc_picture.h>
41 #include "filter_picture.h"
43 #include "adjust_sat_hue.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Create ( vlc_object_t
* );
49 static void Destroy ( vlc_object_t
* );
51 static picture_t
*FilterPlanar( filter_t
*, picture_t
* );
52 static picture_t
*FilterPacked( filter_t
*, picture_t
* );
53 static int AdjustCallback( vlc_object_t
*p_this
, char const *psz_var
,
54 vlc_value_t oldval
, vlc_value_t newval
,
57 /*****************************************************************************
59 *****************************************************************************/
61 #define THRES_TEXT N_("Brightness threshold")
62 #define THRES_LONGTEXT N_("When this mode is enabled, pixels will be " \
63 "shown as black or white. The threshold value will be the brightness " \
65 #define CONT_TEXT N_("Image contrast (0-2)")
66 #define CONT_LONGTEXT N_("Set the image contrast, between 0 and 2. Defaults to 1.")
67 #define HUE_TEXT N_("Image hue (0-360)")
68 #define HUE_LONGTEXT N_("Set the image hue, between 0 and 360. Defaults to 0.")
69 #define SAT_TEXT N_("Image saturation (0-3)")
70 #define SAT_LONGTEXT N_("Set the image saturation, between 0 and 3. Defaults to 1.")
71 #define LUM_TEXT N_("Image brightness (0-2)")
72 #define LUM_LONGTEXT N_("Set the image brightness, between 0 and 2. Defaults to 1.")
73 #define GAMMA_TEXT N_("Image gamma (0-10)")
74 #define GAMMA_LONGTEXT N_("Set the image gamma, between 0.01 and 10. Defaults to 1.")
77 set_description( N_("Image properties filter") )
78 set_shortname( N_("Image adjust" ))
79 set_category( CAT_VIDEO
)
80 set_subcategory( SUBCAT_VIDEO_VFILTER
)
81 set_capability( "video filter", 0 )
83 add_float_with_range( "contrast", 1.0, 0.0, 2.0,
84 CONT_TEXT
, CONT_LONGTEXT
, false )
86 add_float_with_range( "brightness", 1.0, 0.0, 2.0,
87 LUM_TEXT
, LUM_LONGTEXT
, false )
89 add_float_with_range( "hue", 0, -180., +180.,
90 HUE_TEXT
, HUE_LONGTEXT
, false )
92 add_float_with_range( "saturation", 1.0, 0.0, 3.0,
93 SAT_TEXT
, SAT_LONGTEXT
, false )
95 add_float_with_range( "gamma", 1.0, 0.01, 10.0,
96 GAMMA_TEXT
, GAMMA_LONGTEXT
, false )
98 add_bool( "brightness-threshold", false,
99 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 *****************************************************************************/
116 vlc_atomic_float f_contrast
;
117 vlc_atomic_float f_brightness
;
118 vlc_atomic_float f_hue
;
119 vlc_atomic_float f_saturation
;
120 vlc_atomic_float f_gamma
;
121 atomic_bool b_brightness_threshold
;
122 int (*pf_process_sat_hue
)( picture_t
*, picture_t
*, int, int, int,
124 int (*pf_process_sat_hue_clip
)( picture_t
*, picture_t
*, int, int,
128 /*****************************************************************************
129 * Create: allocates adjust video filter
130 *****************************************************************************/
131 static int Create( vlc_object_t
*p_this
)
133 filter_t
*p_filter
= (filter_t
*)p_this
;
136 if( p_filter
->fmt_in
.video
.i_chroma
!= p_filter
->fmt_out
.video
.i_chroma
)
138 msg_Err( p_filter
, "Input and output chromas don't match" );
142 /* Allocate structure */
143 p_filter
->p_sys
= malloc( sizeof( filter_sys_t
) );
144 if( p_filter
->p_sys
== NULL
)
146 p_sys
= p_filter
->p_sys
;
148 /* Choose Planar/Packed function and pointer to a Hue/Saturation processing
150 switch( p_filter
->fmt_in
.video
.i_chroma
)
154 p_filter
->pf_video_filter
= FilterPlanar
;
155 p_sys
->pf_process_sat_hue_clip
= planar_sat_hue_clip_C
;
156 p_sys
->pf_process_sat_hue
= planar_sat_hue_C
;
161 /* Planar YUV 9-bit or 10-bit */
162 p_filter
->pf_video_filter
= FilterPlanar
;
163 p_sys
->pf_process_sat_hue_clip
= planar_sat_hue_clip_C_16
;
164 p_sys
->pf_process_sat_hue
= planar_sat_hue_C_16
;
168 /* Packed YUV 4:2:2 */
169 p_filter
->pf_video_filter
= FilterPacked
;
170 p_sys
->pf_process_sat_hue_clip
= packed_sat_hue_clip_C
;
171 p_sys
->pf_process_sat_hue
= packed_sat_hue_C
;
175 msg_Dbg( p_filter
, "Unsupported input chroma (%4.4s)",
176 (char*)&(p_filter
->fmt_in
.video
.i_chroma
) );
181 /* needed to get options passed in transcode using the
182 * adjust{name=value} syntax */
183 config_ChainParse( p_filter
, "", ppsz_filter_options
, p_filter
->p_cfg
);
185 vlc_atomic_init_float( &p_sys
->f_contrast
,
186 var_CreateGetFloatCommand( p_filter
, "contrast" ) );
187 vlc_atomic_init_float( &p_sys
->f_brightness
,
188 var_CreateGetFloatCommand( p_filter
, "brightness" ) );
189 vlc_atomic_init_float( &p_sys
->f_hue
,
190 var_CreateGetFloatCommand( p_filter
, "hue" ) );
191 vlc_atomic_init_float( &p_sys
->f_saturation
,
192 var_CreateGetFloatCommand( p_filter
, "saturation" ) );
193 vlc_atomic_init_float( &p_sys
->f_gamma
,
194 var_CreateGetFloatCommand( p_filter
, "gamma" ) );
195 atomic_init( &p_sys
->b_brightness_threshold
,
196 var_CreateGetBoolCommand( p_filter
, "brightness-threshold" ) );
198 var_AddCallback( p_filter
, "contrast", AdjustCallback
, p_sys
);
199 var_AddCallback( p_filter
, "brightness", AdjustCallback
, p_sys
);
200 var_AddCallback( p_filter
, "hue", AdjustCallback
, p_sys
);
201 var_AddCallback( p_filter
, "saturation", AdjustCallback
, p_sys
);
202 var_AddCallback( p_filter
, "gamma", AdjustCallback
, p_sys
);
203 var_AddCallback( p_filter
, "brightness-threshold",
204 AdjustCallback
, p_sys
);
209 /*****************************************************************************
210 * Destroy: destroy adjust video filter
211 *****************************************************************************/
212 static void Destroy( vlc_object_t
*p_this
)
214 filter_t
*p_filter
= (filter_t
*)p_this
;
215 filter_sys_t
*p_sys
= p_filter
->p_sys
;
217 var_DelCallback( p_filter
, "contrast", AdjustCallback
, p_sys
);
218 var_DelCallback( p_filter
, "brightness", AdjustCallback
, p_sys
);
219 var_DelCallback( p_filter
, "hue", AdjustCallback
, p_sys
);
220 var_DelCallback( p_filter
, "saturation", AdjustCallback
, p_sys
);
221 var_DelCallback( p_filter
, "gamma", AdjustCallback
, p_sys
);
222 var_DelCallback( p_filter
, "brightness-threshold",
223 AdjustCallback
, p_sys
);
228 /*****************************************************************************
229 * Run the filter on a Planar YUV picture
230 *****************************************************************************/
231 static picture_t
*FilterPlanar( filter_t
*p_filter
, picture_t
*p_pic
)
233 /* The full range will only be used for 10-bit */
239 filter_sys_t
*p_sys
= p_filter
->p_sys
;
241 if( !p_pic
) return NULL
;
243 p_outpic
= filter_NewPicture( p_filter
);
246 picture_Release( p_pic
);
252 switch( p_filter
->fmt_in
.video
.i_chroma
)
267 const float f_max
= f_range
- 1.f
;
268 const unsigned i_max
= f_max
;
269 const int i_range
= f_range
;
270 const unsigned i_size
= i_range
;
271 const unsigned i_mid
= i_range
>> 1;
274 int32_t i_cont
= lroundf( vlc_atomic_load_float( &p_sys
->f_contrast
) * f_max
);
275 int32_t i_lum
= lroundf( (vlc_atomic_load_float( &p_sys
->f_brightness
) - 1.f
) * f_max
);
276 float f_hue
= vlc_atomic_load_float( &p_sys
->f_hue
) * (float)(M_PI
/ 180.);
277 int i_sat
= (int)( vlc_atomic_load_float( &p_sys
->f_saturation
) * f_range
);
278 float f_gamma
= 1.f
/ vlc_atomic_load_float( &p_sys
->f_gamma
);
281 * Threshold mode drops out everything about luma, contrast and gamma.
283 if( !atomic_load( &p_sys
->b_brightness_threshold
) )
286 /* Contrast is a fast but kludged function, so I put this gap to be
288 i_lum
+= i_mid
- i_cont
/ 2;
290 /* Fill the gamma lookup table */
291 for( unsigned i
= 0 ; i
< i_size
; i
++ )
293 pi_gamma
[ i
] = VLC_CLIP( powf(i
/ f_max
, f_gamma
) * f_max
, 0, i_max
);
296 /* Fill the luma lookup table */
297 for( unsigned i
= 0 ; i
< i_size
; i
++ )
299 pi_luma
[ i
] = pi_gamma
[VLC_CLIP( (int)(i_lum
+ i_cont
* i
/ i_range
), 0, i_max
)];
305 * We get luma as threshold value: the higher it is, the darker is
306 * the image. Should I reverse this?
308 for( int i
= 0 ; i
< i_range
; i
++ )
310 pi_luma
[ i
] = (i
< i_lum
) ? 0 : i_max
;
314 * Desaturates image to avoid that strange yellow halo...
324 uint16_t *p_in
, *p_in_end
, *p_line_end
;
326 p_in
= (uint16_t *) p_pic
->p
[Y_PLANE
].p_pixels
;
327 p_in_end
= p_in
+ p_pic
->p
[Y_PLANE
].i_visible_lines
328 * (p_pic
->p
[Y_PLANE
].i_pitch
>> 1) - 8;
330 p_out
= (uint16_t *) p_outpic
->p
[Y_PLANE
].p_pixels
;
332 for( ; p_in
< p_in_end
; )
334 p_line_end
= p_in
+ (p_pic
->p
[Y_PLANE
].i_visible_pitch
>> 1) - 8;
336 for( ; p_in
< p_line_end
; )
338 /* Do 8 pixels at a time */
339 *p_out
++ = pi_luma
[ *p_in
++ ]; *p_out
++ = pi_luma
[ *p_in
++ ];
340 *p_out
++ = pi_luma
[ *p_in
++ ]; *p_out
++ = pi_luma
[ *p_in
++ ];
341 *p_out
++ = pi_luma
[ *p_in
++ ]; *p_out
++ = pi_luma
[ *p_in
++ ];
342 *p_out
++ = pi_luma
[ *p_in
++ ]; *p_out
++ = pi_luma
[ *p_in
++ ];
347 for( ; p_in
< p_line_end
; )
349 *p_out
++ = pi_luma
[ *p_in
++ ];
352 p_in
+= (p_pic
->p
[Y_PLANE
].i_pitch
>> 1)
353 - (p_pic
->p
[Y_PLANE
].i_visible_pitch
>> 1);
354 p_out
+= (p_outpic
->p
[Y_PLANE
].i_pitch
>> 1)
355 - (p_outpic
->p
[Y_PLANE
].i_visible_pitch
>> 1);
360 uint8_t *p_in
, *p_in_end
, *p_line_end
;
362 p_in
= p_pic
->p
[Y_PLANE
].p_pixels
;
363 p_in_end
= p_in
+ p_pic
->p
[Y_PLANE
].i_visible_lines
364 * p_pic
->p
[Y_PLANE
].i_pitch
- 8;
366 p_out
= p_outpic
->p
[Y_PLANE
].p_pixels
;
368 for( ; p_in
< p_in_end
; )
370 p_line_end
= p_in
+ p_pic
->p
[Y_PLANE
].i_visible_pitch
- 8;
372 for( ; p_in
< p_line_end
; )
374 /* Do 8 pixels at a time */
375 *p_out
++ = pi_luma
[ *p_in
++ ]; *p_out
++ = pi_luma
[ *p_in
++ ];
376 *p_out
++ = pi_luma
[ *p_in
++ ]; *p_out
++ = pi_luma
[ *p_in
++ ];
377 *p_out
++ = pi_luma
[ *p_in
++ ]; *p_out
++ = pi_luma
[ *p_in
++ ];
378 *p_out
++ = pi_luma
[ *p_in
++ ]; *p_out
++ = pi_luma
[ *p_in
++ ];
383 for( ; p_in
< p_line_end
; )
385 *p_out
++ = pi_luma
[ *p_in
++ ];
388 p_in
+= p_pic
->p
[Y_PLANE
].i_pitch
389 - p_pic
->p
[Y_PLANE
].i_visible_pitch
;
390 p_out
+= p_outpic
->p
[Y_PLANE
].i_pitch
391 - p_outpic
->p
[Y_PLANE
].i_visible_pitch
;
396 * Do the U and V planes
399 int i_sin
= sinf(f_hue
) * f_max
;
400 int i_cos
= cosf(f_hue
) * f_max
;
402 /* pow(2, (bpp * 2) - 1) */
403 int i_x
= ( cosf(f_hue
) + sinf(f_hue
) ) * f_range
* i_mid
;
404 int i_y
= ( cosf(f_hue
) - sinf(f_hue
) ) * f_range
* i_mid
;
406 if ( i_sat
> i_range
)
408 /* Currently no errors are implemented in the function, if any are added
410 p_sys
->pf_process_sat_hue_clip( p_pic
, p_outpic
, i_sin
, i_cos
, i_sat
,
415 /* Currently no errors are implemented in the function, if any are added
417 p_sys
->pf_process_sat_hue( p_pic
, p_outpic
, i_sin
, i_cos
, i_sat
,
421 return CopyInfoAndRelease( p_outpic
, p_pic
);
424 /*****************************************************************************
425 * Run the filter on a Packed YUV picture
426 *****************************************************************************/
427 static picture_t
*FilterPacked( filter_t
*p_filter
, picture_t
*p_pic
)
433 uint8_t *p_in
, *p_in_end
, *p_line_end
;
435 int i_y_offset
, i_u_offset
, i_v_offset
;
437 int i_pitch
, i_visible_pitch
;
441 int32_t i_cont
, i_lum
;
442 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_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 (%4.4s)",
455 (char*)&(p_pic
->format
.i_chroma
) );
457 picture_Release( p_pic
);
461 p_outpic
= filter_NewPicture( p_filter
);
464 msg_Warn( p_filter
, "can't get output picture" );
466 picture_Release( p_pic
);
471 i_cont
= (int)( vlc_atomic_load_float( &p_sys
->f_contrast
) * 255 );
472 i_lum
= (int)( (vlc_atomic_load_float( &p_sys
->f_brightness
) - 1.0)*255 );
473 f_hue
= vlc_atomic_load_float( &p_sys
->f_hue
) * (float)(M_PI
/ 180.);
474 i_sat
= (int)( vlc_atomic_load_float( &p_sys
->f_saturation
) * 256 );
475 f_gamma
= 1.0 / vlc_atomic_load_float( &p_sys
->f_gamma
);
478 * Threshold mode drops out everything about luma, contrast and gamma.
480 if( !atomic_load( &p_sys
->b_brightness_threshold
) )
483 /* Contrast is a fast but kludged function, so I put this gap to be
485 i_lum
+= 128 - i_cont
/ 2;
487 /* Fill the gamma lookup table */
488 for( int i
= 0 ; i
< 256 ; i
++ )
490 pi_gamma
[ i
] = clip_uint8_vlc( pow(i
/ 255.0, f_gamma
) * 255.0);
493 /* Fill the luma lookup table */
494 for( int i
= 0 ; i
< 256 ; i
++ )
496 pi_luma
[ i
] = pi_gamma
[clip_uint8_vlc( i_lum
+ i_cont
* i
/ 256)];
502 * We get luma as threshold value: the higher it is, the darker is
503 * the image. Should I reverse this?
505 for( int i
= 0 ; i
< 256 ; i
++ )
507 pi_luma
[ i
] = (i
< i_lum
) ? 0 : 255;
511 * Desaturates image to avoid that strange yellow halo...
520 p_in
= p_pic
->p
->p_pixels
+ i_y_offset
;
521 p_in_end
= p_in
+ p_pic
->p
->i_visible_lines
* p_pic
->p
->i_pitch
- 8 * 4;
523 p_out
= p_outpic
->p
->p_pixels
+ i_y_offset
;
525 for( ; p_in
< p_in_end
; )
527 p_line_end
= p_in
+ i_visible_pitch
- 8 * 4;
529 for( ; p_in
< p_line_end
; )
531 /* Do 8 pixels at a time */
532 *p_out
= pi_luma
[ *p_in
]; p_in
+= 2; p_out
+= 2;
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;
544 for( ; p_in
< p_line_end
; )
546 *p_out
= pi_luma
[ *p_in
]; p_in
+= 2; p_out
+= 2;
549 p_in
+= i_pitch
- p_pic
->p
->i_visible_pitch
;
550 p_out
+= i_pitch
- p_outpic
->p
->i_visible_pitch
;
554 * Do the U and V planes
557 i_sin
= sin(f_hue
) * 256;
558 i_cos
= cos(f_hue
) * 256;
560 i_x
= ( cos(f_hue
) + sin(f_hue
) ) * 32768;
561 i_y
= ( cos(f_hue
) - sin(f_hue
) ) * 32768;
565 if ( p_sys
->pf_process_sat_hue_clip( p_pic
, p_outpic
, i_sin
, i_cos
, i_sat
,
566 i_x
, i_y
) != VLC_SUCCESS
)
568 /* Currently only one error can happen in the function, but if there
569 * will be more of them, this message must go away */
570 msg_Warn( p_filter
, "Unsupported input chroma (%4.4s)",
571 (char*)&(p_pic
->format
.i_chroma
) );
572 picture_Release( p_pic
);
578 if ( p_sys
->pf_process_sat_hue( p_pic
, p_outpic
, i_sin
, i_cos
, i_sat
,
579 i_x
, i_y
) != VLC_SUCCESS
)
581 /* Currently only one error can happen in the function, but if there
582 * will be more of them, this message must go away */
583 msg_Warn( p_filter
, "Unsupported input chroma (%4.4s)",
584 (char*)&(p_pic
->format
.i_chroma
) );
585 picture_Release( p_pic
);
590 return CopyInfoAndRelease( p_outpic
, p_pic
);
593 static int AdjustCallback( vlc_object_t
*p_this
, char const *psz_var
,
594 vlc_value_t oldval
, vlc_value_t newval
,
597 VLC_UNUSED(p_this
); VLC_UNUSED(oldval
);
598 filter_sys_t
*p_sys
= (filter_sys_t
*)p_data
;
600 if( !strcmp( psz_var
, "contrast" ) )
601 vlc_atomic_store_float( &p_sys
->f_contrast
, newval
.f_float
);
602 else if( !strcmp( psz_var
, "brightness" ) )
603 vlc_atomic_store_float( &p_sys
->f_brightness
, newval
.f_float
);
604 else if( !strcmp( psz_var
, "hue" ) )
605 vlc_atomic_store_float( &p_sys
->f_hue
, newval
.f_float
);
606 else if( !strcmp( psz_var
, "saturation" ) )
607 vlc_atomic_store_float( &p_sys
->f_saturation
, newval
.f_float
);
608 else if( !strcmp( psz_var
, "gamma" ) )
609 vlc_atomic_store_float( &p_sys
->f_gamma
, newval
.f_float
);
610 else if( !strcmp( psz_var
, "brightness-threshold" ) )
611 atomic_store( &p_sys
->b_brightness_threshold
, newval
.b_bool
);