demux: mkv: handle WAVE_FORMAT_MPEG_ADTS_AAC
[vlc.git] / modules / video_filter / adjust.c
blobe0074e28e310e89270f154ab00897ff39dd4bba3
1 /*****************************************************************************
2 * adjust.c : Contrast/Hue/Saturation/Brightness video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2006 VLC authors and VideoLAN
5 * $Id$
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 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <math.h>
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_filter.h>
39 #include <vlc_picture.h>
40 #include "filter_picture.h"
42 #include "adjust_sat_hue.h"
44 /*****************************************************************************
45 * Local prototypes
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,
54 void *p_data );
56 /*****************************************************************************
57 * Module descriptor
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 brightness " \
63 "defined below." )
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.")
75 vlc_module_begin ()
76 set_description( N_("Image properties filter") )
77 set_shortname( N_("Image adjust" ))
78 set_category( CAT_VIDEO )
79 set_subcategory( SUBCAT_VIDEO_VFILTER )
80 set_capability( "video filter", 0 )
82 add_float_with_range( "contrast", 1.0, 0.0, 2.0,
83 CONT_TEXT, CONT_LONGTEXT, false )
84 change_safe()
85 add_float_with_range( "brightness", 1.0, 0.0, 2.0,
86 LUM_TEXT, LUM_LONGTEXT, false )
87 change_safe()
88 add_float_with_range( "hue", 0, -180., +180.,
89 HUE_TEXT, HUE_LONGTEXT, false )
90 change_safe()
91 add_float_with_range( "saturation", 1.0, 0.0, 3.0,
92 SAT_TEXT, SAT_LONGTEXT, false )
93 change_safe()
94 add_float_with_range( "gamma", 1.0, 0.01, 10.0,
95 GAMMA_TEXT, GAMMA_LONGTEXT, false )
96 change_safe()
97 add_bool( "brightness-threshold", false,
98 THRES_TEXT, THRES_LONGTEXT, false )
99 change_safe()
101 add_shortcut( "adjust" )
102 set_callbacks( Create, Destroy )
103 vlc_module_end ()
105 static const char *const ppsz_filter_options[] = {
106 "contrast", "brightness", "hue", "saturation", "gamma",
107 "brightness-threshold", NULL
110 /*****************************************************************************
111 * filter_sys_t: adjust filter method descriptor
112 *****************************************************************************/
113 struct filter_sys_t
115 vlc_mutex_t lock;
116 float f_contrast;
117 float f_brightness;
118 float f_hue;
119 float f_saturation;
120 float f_gamma;
121 bool b_brightness_threshold;
122 int (*pf_process_sat_hue)( picture_t *, picture_t *, int, int, int,
123 int, int );
124 int (*pf_process_sat_hue_clip)( picture_t *, picture_t *, int, int,
125 int, 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;
134 filter_sys_t *p_sys;
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" );
139 return VLC_EGENERIC;
142 /* Allocate structure */
143 p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
144 if( p_filter->p_sys == NULL )
145 return VLC_ENOMEM;
146 p_sys = p_filter->p_sys;
148 /* Choose Planar/Packed function and pointer to a Hue/Saturation processing
149 * function*/
150 switch( p_filter->fmt_in.video.i_chroma )
152 CASE_PLANAR_YUV
153 /* Planar YUV */
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;
157 break;
159 CASE_PLANAR_YUV10
160 CASE_PLANAR_YUV9
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;
165 break;
167 CASE_PACKED_YUV_422
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;
172 break;
174 default:
175 msg_Dbg( p_filter, "Unsupported input chroma (%4.4s)",
176 (char*)&(p_filter->fmt_in.video.i_chroma) );
177 free(p_sys);
178 return VLC_EGENERIC;
181 vlc_mutex_init( &p_sys->lock );
183 /* needed to get options passed in transcode using the
184 * adjust{name=value} syntax */
185 config_ChainParse( p_filter, "", ppsz_filter_options, p_filter->p_cfg );
187 p_sys->f_contrast = var_CreateGetFloatCommand( p_filter, "contrast" );
188 p_sys->f_brightness = var_CreateGetFloatCommand( p_filter, "brightness" );
189 p_sys->f_hue = var_CreateGetFloatCommand( p_filter, "hue" );
190 p_sys->f_saturation = var_CreateGetFloatCommand( p_filter, "saturation" );
191 p_sys->f_gamma = var_CreateGetFloatCommand( p_filter, "gamma" );
192 p_sys->b_brightness_threshold =
193 var_CreateGetBoolCommand( p_filter, "brightness-threshold" );
195 var_AddCallback( p_filter, "contrast", AdjustCallback, p_sys );
196 var_AddCallback( p_filter, "brightness", AdjustCallback, p_sys );
197 var_AddCallback( p_filter, "hue", AdjustCallback, p_sys );
198 var_AddCallback( p_filter, "saturation", AdjustCallback, p_sys );
199 var_AddCallback( p_filter, "gamma", AdjustCallback, p_sys );
200 var_AddCallback( p_filter, "brightness-threshold",
201 AdjustCallback, p_sys );
203 return VLC_SUCCESS;
206 /*****************************************************************************
207 * Destroy: destroy adjust video filter
208 *****************************************************************************/
209 static void Destroy( vlc_object_t *p_this )
211 filter_t *p_filter = (filter_t *)p_this;
212 filter_sys_t *p_sys = p_filter->p_sys;
214 var_DelCallback( p_filter, "contrast", AdjustCallback, p_sys );
215 var_DelCallback( p_filter, "brightness", AdjustCallback, p_sys );
216 var_DelCallback( p_filter, "hue", AdjustCallback, p_sys );
217 var_DelCallback( p_filter, "saturation", AdjustCallback, p_sys );
218 var_DelCallback( p_filter, "gamma", AdjustCallback, p_sys );
219 var_DelCallback( p_filter, "brightness-threshold",
220 AdjustCallback, p_sys );
222 vlc_mutex_destroy( &p_sys->lock );
223 free( p_sys );
226 /*****************************************************************************
227 * Run the filter on a Planar YUV picture
228 *****************************************************************************/
229 static picture_t *FilterPlanar( filter_t *p_filter, picture_t *p_pic )
231 /* The full range will only be used for 10-bit */
232 int pi_luma[1024];
233 int pi_gamma[1024];
235 picture_t *p_outpic;
237 filter_sys_t *p_sys = p_filter->p_sys;
239 if( !p_pic ) return NULL;
241 p_outpic = filter_NewPicture( p_filter );
242 if( !p_outpic )
244 picture_Release( p_pic );
245 return NULL;
248 bool b_16bit;
249 float f_range;
250 switch( p_filter->fmt_in.video.i_chroma )
252 CASE_PLANAR_YUV10
253 b_16bit = true;
254 f_range = 1024.f;
255 break;
256 CASE_PLANAR_YUV9
257 b_16bit = true;
258 f_range = 512.f;
259 break;
260 default:
261 b_16bit = false;
262 f_range = 256.f;
265 const float f_max = f_range - 1.f;
266 const unsigned i_max = f_max;
267 const int i_range = f_range;
268 const unsigned i_size = i_range;
269 const unsigned i_mid = i_range >> 1;
271 /* Get variables */
272 vlc_mutex_lock( &p_sys->lock );
273 int32_t i_cont = lroundf( p_sys->f_contrast * f_max );
274 int32_t i_lum = lroundf( (p_sys->f_brightness - 1.f) * f_max );
275 float f_hue = p_sys->f_hue * (float)(M_PI / 180.);
276 int i_sat = (int)( p_sys->f_saturation * f_range );
277 float f_gamma = 1.f / p_sys->f_gamma;
278 bool b_thres = p_sys->b_brightness_threshold;
279 vlc_mutex_unlock( &p_sys->lock );
282 * Threshold mode drops out everything about luma, contrast and gamma.
284 if( !b_thres )
287 /* Contrast is a fast but kludged function, so I put this gap to be
288 * cleaner :) */
289 i_lum += i_mid - i_cont / 2;
291 /* Fill the gamma lookup table */
292 for( unsigned i = 0 ; i < i_size; i++ )
294 pi_gamma[ i ] = VLC_CLIP( powf(i / f_max, f_gamma) * f_max, 0, i_max );
297 /* Fill the luma lookup table */
298 for( unsigned i = 0 ; i < i_size; i++ )
300 pi_luma[ i ] = pi_gamma[VLC_CLIP( (int)(i_lum + i_cont * i / i_range), 0, i_max )];
303 else
306 * We get luma as threshold value: the higher it is, the darker is
307 * the image. Should I reverse this?
309 for( int i = 0 ; i < i_range; i++ )
311 pi_luma[ i ] = (i < i_lum) ? 0 : i_max;
315 * Desaturates image to avoid that strange yellow halo...
317 i_sat = 0;
321 * Do the Y plane
323 if ( b_16bit )
325 uint16_t *p_in, *p_in_end, *p_line_end;
326 uint16_t *p_out;
327 p_in = (uint16_t *) p_pic->p[Y_PLANE].p_pixels;
328 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
329 * (p_pic->p[Y_PLANE].i_pitch >> 1) - 8;
331 p_out = (uint16_t *) p_outpic->p[Y_PLANE].p_pixels;
333 for( ; p_in < p_in_end ; )
335 p_line_end = p_in + (p_pic->p[Y_PLANE].i_visible_pitch >> 1) - 8;
337 for( ; p_in < p_line_end ; )
339 /* Do 8 pixels at a time */
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++ ];
343 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
346 p_line_end += 8;
348 for( ; p_in < p_line_end ; )
350 *p_out++ = pi_luma[ *p_in++ ];
353 p_in += (p_pic->p[Y_PLANE].i_pitch >> 1)
354 - (p_pic->p[Y_PLANE].i_visible_pitch >> 1);
355 p_out += (p_outpic->p[Y_PLANE].i_pitch >> 1)
356 - (p_outpic->p[Y_PLANE].i_visible_pitch >> 1);
359 else
361 uint8_t *p_in, *p_in_end, *p_line_end;
362 uint8_t *p_out;
363 p_in = p_pic->p[Y_PLANE].p_pixels;
364 p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
365 * p_pic->p[Y_PLANE].i_pitch - 8;
367 p_out = p_outpic->p[Y_PLANE].p_pixels;
369 for( ; p_in < p_in_end ; )
371 p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
373 for( ; p_in < p_line_end ; )
375 /* Do 8 pixels at a time */
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++ ];
379 *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
382 p_line_end += 8;
384 for( ; p_in < p_line_end ; )
386 *p_out++ = pi_luma[ *p_in++ ];
389 p_in += p_pic->p[Y_PLANE].i_pitch
390 - p_pic->p[Y_PLANE].i_visible_pitch;
391 p_out += p_outpic->p[Y_PLANE].i_pitch
392 - p_outpic->p[Y_PLANE].i_visible_pitch;
397 * Do the U and V planes
400 int i_sin = sinf(f_hue) * f_max;
401 int i_cos = cosf(f_hue) * f_max;
403 /* pow(2, (bpp * 2) - 1) */
404 int i_x = ( cosf(f_hue) + sinf(f_hue) ) * f_range * i_mid;
405 int i_y = ( cosf(f_hue) - sinf(f_hue) ) * f_range * i_mid;
407 if ( i_sat > i_range )
409 /* Currently no errors are implemented in the function, if any are added
410 * check them here */
411 p_sys->pf_process_sat_hue_clip( p_pic, p_outpic, i_sin, i_cos, i_sat,
412 i_x, i_y );
414 else
416 /* Currently no errors are implemented in the function, if any are added
417 * check them here */
418 p_sys->pf_process_sat_hue( p_pic, p_outpic, i_sin, i_cos, i_sat,
419 i_x, i_y );
422 return CopyInfoAndRelease( p_outpic, p_pic );
425 /*****************************************************************************
426 * Run the filter on a Packed YUV picture
427 *****************************************************************************/
428 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
430 int pi_luma[256];
431 int pi_gamma[256];
433 picture_t *p_outpic;
434 uint8_t *p_in, *p_in_end, *p_line_end;
435 uint8_t *p_out;
436 int i_y_offset, i_u_offset, i_v_offset;
438 int i_pitch, i_visible_pitch;
440 bool b_thres;
441 double f_hue;
442 double f_gamma;
443 int32_t i_cont, i_lum;
444 int i_sat, i_sin, i_cos, i_x, i_y;
446 filter_sys_t *p_sys = p_filter->p_sys;
448 if( !p_pic ) return NULL;
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 (%4.4s)",
457 (char*)&(p_pic->format.i_chroma) );
459 picture_Release( p_pic );
460 return NULL;
463 p_outpic = filter_NewPicture( p_filter );
464 if( !p_outpic )
466 msg_Warn( p_filter, "can't get output picture" );
468 picture_Release( p_pic );
469 return NULL;
472 /* Get variables */
473 vlc_mutex_lock( &p_sys->lock );
474 i_cont = (int)( p_sys->f_contrast * 255 );
475 i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
476 f_hue = p_sys->f_hue * (float)(M_PI / 180.);
477 i_sat = (int)( p_sys->f_saturation * 256 );
478 f_gamma = 1.0 / p_sys->f_gamma;
479 b_thres = p_sys->b_brightness_threshold;
480 vlc_mutex_unlock( &p_sys->lock );
483 * Threshold mode drops out everything about luma, contrast and gamma.
485 if( !b_thres )
488 /* Contrast is a fast but kludged function, so I put this gap to be
489 * cleaner :) */
490 i_lum += 128 - i_cont / 2;
492 /* Fill the gamma lookup table */
493 for( int i = 0 ; i < 256 ; i++ )
495 pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
498 /* Fill the luma lookup table */
499 for( int i = 0 ; i < 256 ; i++ )
501 pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
504 else
507 * We get luma as threshold value: the higher it is, the darker is
508 * the image. Should I reverse this?
510 for( int i = 0 ; i < 256 ; i++ )
512 pi_luma[ i ] = (i < i_lum) ? 0 : 255;
516 * Desaturates image to avoid that strange yellow halo...
518 i_sat = 0;
522 * Do the Y plane
525 p_in = p_pic->p->p_pixels + i_y_offset;
526 p_in_end = p_in + p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 * 4;
528 p_out = p_outpic->p->p_pixels + i_y_offset;
530 for( ; p_in < p_in_end ; )
532 p_line_end = p_in + i_visible_pitch - 8 * 4;
534 for( ; p_in < p_line_end ; )
536 /* Do 8 pixels at a time */
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;
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;
547 p_line_end += 8 * 4;
549 for( ; p_in < p_line_end ; )
551 *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
554 p_in += i_pitch - p_pic->p->i_visible_pitch;
555 p_out += i_pitch - p_outpic->p->i_visible_pitch;
559 * Do the U and V planes
562 i_sin = sin(f_hue) * 256;
563 i_cos = cos(f_hue) * 256;
565 i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
566 i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
568 if ( i_sat > 256 )
570 if ( p_sys->pf_process_sat_hue_clip( p_pic, p_outpic, i_sin, i_cos, i_sat,
571 i_x, i_y ) != VLC_SUCCESS )
573 /* Currently only one error can happen in the function, but if there
574 * will be more of them, this message must go away */
575 msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
576 (char*)&(p_pic->format.i_chroma) );
577 picture_Release( p_pic );
578 return NULL;
581 else
583 if ( p_sys->pf_process_sat_hue( p_pic, p_outpic, i_sin, i_cos, i_sat,
584 i_x, i_y ) != VLC_SUCCESS )
586 /* Currently only one error can happen in the function, but if there
587 * will be more of them, this message must go away */
588 msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
589 (char*)&(p_pic->format.i_chroma) );
590 picture_Release( p_pic );
591 return NULL;
595 return CopyInfoAndRelease( p_outpic, p_pic );
598 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
599 vlc_value_t oldval, vlc_value_t newval,
600 void *p_data )
602 VLC_UNUSED(p_this); VLC_UNUSED(oldval);
603 filter_sys_t *p_sys = (filter_sys_t *)p_data;
605 vlc_mutex_lock( &p_sys->lock );
606 if( !strcmp( psz_var, "contrast" ) )
607 p_sys->f_contrast = newval.f_float;
608 else if( !strcmp( psz_var, "brightness" ) )
609 p_sys->f_brightness = newval.f_float;
610 else if( !strcmp( psz_var, "hue" ) )
611 p_sys->f_hue = newval.f_float;
612 else if( !strcmp( psz_var, "saturation" ) )
613 p_sys->f_saturation = newval.f_float;
614 else if( !strcmp( psz_var, "gamma" ) )
615 p_sys->f_gamma = newval.f_float;
616 else if( !strcmp( psz_var, "brightness-threshold" ) )
617 p_sys->b_brightness_threshold = newval.b_bool;
618 vlc_mutex_unlock( &p_sys->lock );
620 return VLC_SUCCESS;