]> git.sesse.net Git - vlc/blob - modules/video_filter/adjust.c
Cast or convert <ctype.h> functions it parameters to unsigned char
[vlc] / modules / video_filter / adjust.c
1 /*****************************************************************************
2  * adjust.c : Contrast/Hue/Saturation/Brightness video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Simon Latapie <garf@via.ecp.fr>
8  *          Antoine Cellerier <dionoea -at- videolan d0t org>
9  *          Martin Briza <gamajun@seznam.cz> (SSE)
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
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 General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <math.h>
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38
39 #include <vlc_filter.h>
40 #include "filter_picture.h"
41
42 #include "adjust_sat_hue.h"
43
44 #ifndef M_PI
45 #   define M_PI 3.14159265358979323846
46 #endif
47
48 #define eight_times( x )    x x x x x x x x
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  Create    ( vlc_object_t * );
54 static void Destroy   ( vlc_object_t * );
55
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,
60                            void *p_data );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65
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 brightness " \
69         "defined below." )
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.")
80
81 vlc_module_begin ()
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 )
87
88     add_float_with_range( "contrast", 1.0, 0.0, 2.0,
89                           CONT_TEXT, CONT_LONGTEXT, false )
90     add_float_with_range( "brightness", 1.0, 0.0, 2.0,
91                            LUM_TEXT, LUM_LONGTEXT, false )
92     add_integer_with_range( "hue", 0, 0, 360,
93                             HUE_TEXT, HUE_LONGTEXT, false )
94     add_float_with_range( "saturation", 1.0, 0.0, 3.0,
95                           SAT_TEXT, SAT_LONGTEXT, false )
96     add_float_with_range( "gamma", 1.0, 0.01, 10.0,
97                           GAMMA_TEXT, GAMMA_LONGTEXT, false )
98
99     add_bool( "brightness-threshold", false,
100               THRES_TEXT, THRES_LONGTEXT, false )
101
102     add_shortcut( "adjust" )
103     set_callbacks( Create, Destroy )
104 vlc_module_end ()
105
106 static const char *const ppsz_filter_options[] = {
107     "contrast", "brightness", "hue", "saturation", "gamma",
108     "brightness-threshold", NULL
109 };
110
111 /*****************************************************************************
112  * filter_sys_t: adjust filter method descriptor
113  *****************************************************************************/
114 struct filter_sys_t
115 {
116     vlc_mutex_t lock;
117     double     f_contrast;
118     double     f_brightness;
119     int        i_hue;
120     double     f_saturation;
121     double     f_gamma;
122     bool       b_brightness_threshold;
123     int        (* pf_process_sat_hue)( picture_t *, picture_t *, int, int, int,
124                                        int, int );
125     int        (* pf_process_sat_hue_clip)( picture_t *, picture_t *, int, int,
126                                             int, int, int );
127 };
128
129 /*****************************************************************************
130  * Create: allocates adjust video filter
131  *****************************************************************************/
132 static int Create( vlc_object_t *p_this )
133 {
134     filter_t *p_filter = (filter_t *)p_this;
135     filter_sys_t *p_sys;
136
137     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
138     {
139         msg_Err( p_filter, "Input and output chromas don't match" );
140         return VLC_EGENERIC;
141     }
142
143     /* Allocate structure */
144     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
145     if( p_filter->p_sys == NULL )
146         return VLC_ENOMEM;
147     p_sys = p_filter->p_sys;
148
149     /* needed to get options passed in transcode using the
150      * adjust{name=value} syntax */
151     config_ChainParse( p_filter, "", ppsz_filter_options,
152                    p_filter->p_cfg );
153
154     p_sys->f_contrast = var_CreateGetFloatCommand( p_filter, "contrast" );
155     p_sys->f_brightness = var_CreateGetFloatCommand( p_filter, "brightness" );
156     p_sys->i_hue = var_CreateGetIntegerCommand( p_filter, "hue" );
157     p_sys->f_saturation = var_CreateGetFloatCommand( p_filter, "saturation" );
158     p_sys->f_gamma = var_CreateGetFloatCommand( p_filter, "gamma" );
159     p_sys->b_brightness_threshold =
160         var_CreateGetBoolCommand( p_filter, "brightness-threshold" );
161
162     /* Choose Planar/Packed function and pointer to a Hue/Saturation processing
163      * function*/
164     switch( p_filter->fmt_in.video.i_chroma )
165     {
166         CASE_PLANAR_YUV
167             /* Planar YUV */
168             p_filter->pf_video_filter = FilterPlanar;
169             p_sys->pf_process_sat_hue_clip = planar_sat_hue_clip_C;
170             p_sys->pf_process_sat_hue = planar_sat_hue_C;
171             break;
172
173         CASE_PACKED_YUV_422
174             /* Packed YUV 4:2:2 */
175             p_filter->pf_video_filter = FilterPacked;
176             p_sys->pf_process_sat_hue_clip = packed_sat_hue_clip_C;
177             p_sys->pf_process_sat_hue = packed_sat_hue_C;
178             break;
179
180         default:
181             msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
182                      (char*)&(p_filter->fmt_in.video.i_chroma) );
183             return VLC_EGENERIC;
184     }
185
186     vlc_mutex_init( &p_sys->lock );
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 );
194
195     return VLC_SUCCESS;
196 }
197
198 /*****************************************************************************
199  * Destroy: destroy adjust video filter
200  *****************************************************************************/
201 static void Destroy( vlc_object_t *p_this )
202 {
203     filter_t *p_filter = (filter_t *)p_this;
204     filter_sys_t *p_sys = p_filter->p_sys;
205
206     var_DelCallback( p_filter, "contrast",   AdjustCallback, p_sys );
207     var_DelCallback( p_filter, "brightness", AdjustCallback, p_sys );
208     var_DelCallback( p_filter, "hue",        AdjustCallback, p_sys );
209     var_DelCallback( p_filter, "saturation", AdjustCallback, p_sys );
210     var_DelCallback( p_filter, "gamma",      AdjustCallback, p_sys );
211     var_DelCallback( p_filter, "brightness-threshold",
212                                              AdjustCallback, p_sys );
213
214     vlc_mutex_destroy( &p_sys->lock );
215     free( p_sys );
216 }
217
218 /*****************************************************************************
219  * Run the filter on a Planar YUV picture
220  *****************************************************************************/
221 static picture_t *FilterPlanar( filter_t *p_filter, picture_t *p_pic )
222 {
223     int pi_luma[256];
224     int pi_gamma[256];
225
226     picture_t *p_outpic;
227     uint8_t *p_in, *p_in_end, *p_line_end;
228     uint8_t *p_out;
229
230     bool b_thres;
231     double  f_hue;
232     double  f_gamma;
233     int32_t i_cont, i_lum;
234     int i_sat, i_sin, i_cos, i_x, i_y;
235     int i;
236
237     filter_sys_t *p_sys = p_filter->p_sys;
238
239     if( !p_pic ) return NULL;
240
241     p_outpic = filter_NewPicture( p_filter );
242     if( !p_outpic )
243     {
244         picture_Release( p_pic );
245         return NULL;
246     }
247
248     /* Get variables */
249     vlc_mutex_lock( &p_sys->lock );
250     i_cont = (int)( p_sys->f_contrast * 255 );
251     i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
252     f_hue = (float)( p_sys->i_hue * M_PI / 180 );
253     i_sat = (int)( p_sys->f_saturation * 256 );
254     f_gamma = 1.0 / p_sys->f_gamma;
255     b_thres = p_sys->b_brightness_threshold;
256     vlc_mutex_unlock( &p_sys->lock );
257
258     /*
259      * Threshold mode drops out everything about luma, contrast and gamma.
260      */
261     if( !b_thres )
262     {
263
264         /* Contrast is a fast but kludged function, so I put this gap to be
265          * cleaner :) */
266         i_lum += 128 - i_cont / 2;
267
268         /* Fill the gamma lookup table */
269         for( i = 0 ; i < 256 ; i++ )
270         {
271             pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
272         }
273
274         /* Fill the luma lookup table */
275         for( i = 0 ; i < 256 ; i++ )
276         {
277             pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
278         }
279     }
280     else
281     {
282         /*
283          * We get luma as threshold value: the higher it is, the darker is
284          * the image. Should I reverse this?
285          */
286         for( i = 0 ; i < 256 ; i++ )
287         {
288             pi_luma[ i ] = (i < i_lum) ? 0 : 255;
289         }
290
291         /*
292          * Desaturates image to avoid that strange yellow halo...
293          */
294         i_sat = 0;
295     }
296
297     /*
298      * Do the Y plane
299      */
300
301     p_in = p_pic->p[Y_PLANE].p_pixels;
302     p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
303                       * p_pic->p[Y_PLANE].i_pitch - 8;
304
305     p_out = p_outpic->p[Y_PLANE].p_pixels;
306
307     for( ; p_in < p_in_end ; )
308     {
309         p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
310
311         for( ; p_in < p_line_end ; )
312         {
313             /* Do 8 pixels at a time */
314             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
315             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
316             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
317             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
318         }
319
320         p_line_end += 8;
321
322         for( ; p_in < p_line_end ; )
323         {
324             *p_out++ = pi_luma[ *p_in++ ];
325         }
326
327         p_in += p_pic->p[Y_PLANE].i_pitch
328               - p_pic->p[Y_PLANE].i_visible_pitch;
329         p_out += p_outpic->p[Y_PLANE].i_pitch
330                - p_outpic->p[Y_PLANE].i_visible_pitch;
331     }
332
333     /*
334      * Do the U and V planes
335      */
336
337     i_sin = sin(f_hue) * 256;
338     i_cos = cos(f_hue) * 256;
339
340     i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
341     i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
342
343     if ( i_sat > 256 )
344     {
345         /* Currently no errors are implemented in the function, if any are added
346          * check them here */
347         p_sys->pf_process_sat_hue_clip( p_pic, p_outpic, i_sin, i_cos, i_sat,
348                                         i_x, i_y );
349     }
350     else
351     {
352         /* Currently no errors are implemented in the function, if any are added
353          * check them here */
354         p_sys->pf_process_sat_hue( p_pic, p_outpic, i_sin, i_cos, i_sat,
355                                         i_x, i_y );
356     }
357
358     return CopyInfoAndRelease( p_outpic, p_pic );
359 }
360
361 /*****************************************************************************
362  * Run the filter on a Packed YUV picture
363  *****************************************************************************/
364 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
365 {
366     int pi_luma[256];
367     int pi_gamma[256];
368
369     picture_t *p_outpic;
370     uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
371     uint8_t *p_out, *p_out_v;
372     int i_y_offset, i_u_offset, i_v_offset;
373
374     int i_visible_lines, i_pitch, i_visible_pitch;
375
376     bool b_thres;
377     double  f_hue;
378     double  f_gamma;
379     int32_t i_cont, i_lum;
380     int i_sat, i_sin, i_cos, i_x, i_y;
381     int i;
382
383     filter_sys_t *p_sys = p_filter->p_sys;
384
385     if( !p_pic ) return NULL;
386
387     i_visible_lines = p_pic->p->i_visible_lines;
388     i_pitch = p_pic->p->i_pitch;
389     i_visible_pitch = p_pic->p->i_visible_pitch;
390
391     if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
392                              &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
393     {
394         msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
395                   (char*)&(p_pic->format.i_chroma) );
396
397         picture_Release( p_pic );
398         return NULL;
399     }
400
401     p_outpic = filter_NewPicture( p_filter );
402     if( !p_outpic )
403     {
404         msg_Warn( p_filter, "can't get output picture" );
405
406         picture_Release( p_pic );
407         return NULL;
408     }
409
410     /* Get variables */
411     vlc_mutex_lock( &p_sys->lock );
412     i_cont = (int)( p_sys->f_contrast * 255 );
413     i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
414     f_hue = (float)( p_sys->i_hue * M_PI / 180 );
415     i_sat = (int)( p_sys->f_saturation * 256 );
416     f_gamma = 1.0 / p_sys->f_gamma;
417     b_thres = p_sys->b_brightness_threshold;
418     vlc_mutex_unlock( &p_sys->lock );
419
420     /*
421      * Threshold mode drops out everything about luma, contrast and gamma.
422      */
423     if( !b_thres )
424     {
425
426         /* Contrast is a fast but kludged function, so I put this gap to be
427          * cleaner :) */
428         i_lum += 128 - i_cont / 2;
429
430         /* Fill the gamma lookup table */
431         for( i = 0 ; i < 256 ; i++ )
432         {
433           pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
434         }
435
436         /* Fill the luma lookup table */
437         for( i = 0 ; i < 256 ; i++ )
438         {
439             pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
440         }
441     }
442     else
443     {
444         /*
445          * We get luma as threshold value: the higher it is, the darker is
446          * the image. Should I reverse this?
447          */
448         for( i = 0 ; i < 256 ; i++ )
449         {
450             pi_luma[ i ] = (i < i_lum) ? 0 : 255;
451         }
452
453         /*
454          * Desaturates image to avoid that strange yellow halo...
455          */
456         i_sat = 0;
457     }
458
459     /*
460      * Do the Y plane
461      */
462
463     p_in = p_pic->p->p_pixels + i_y_offset;
464     p_in_end = p_in + p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 * 4;
465
466     p_out = p_outpic->p->p_pixels + i_y_offset;
467
468     for( ; p_in < p_in_end ; )
469     {
470         p_line_end = p_in + i_visible_pitch - 8 * 4;
471
472         for( ; p_in < p_line_end ; )
473         {
474             /* Do 8 pixels at a time */
475             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
476             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
477             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
478             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
479             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
480             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
481             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
482             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
483         }
484
485         p_line_end += 8 * 4;
486
487         for( ; p_in < p_line_end ; )
488         {
489             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
490         }
491
492         p_in += i_pitch - p_pic->p->i_visible_pitch;
493         p_out += i_pitch - p_outpic->p->i_visible_pitch;
494     }
495
496     /*
497      * Do the U and V planes
498      */
499
500     p_in = p_pic->p->p_pixels + i_u_offset;
501     p_in_v = p_pic->p->p_pixels + i_v_offset;
502     p_in_end = p_in + i_visible_lines * i_pitch - 8 * 4;
503
504     p_out = p_outpic->p->p_pixels + i_u_offset;
505     p_out_v = p_outpic->p->p_pixels + i_v_offset;
506
507     i_sin = sin(f_hue) * 256;
508     i_cos = cos(f_hue) * 256;
509
510     i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
511     i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
512
513     if ( i_sat > 256 )
514     {
515         if ( p_sys->pf_process_sat_hue_clip( p_pic, p_outpic, i_sin, i_cos, i_sat,
516                                              i_x, i_y ) != VLC_SUCCESS )
517         {
518             /* Currently only one error can happen in the function, but if there
519              * will be more of them, this message must go away */
520             msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
521                       (char*)&(p_pic->format.i_chroma) );
522             picture_Release( p_pic );
523             return NULL;
524         }
525     }
526     else
527     {
528         if ( p_sys->pf_process_sat_hue( p_pic, p_outpic, i_sin, i_cos, i_sat,
529                                         i_x, i_y ) != VLC_SUCCESS )
530         {
531             /* Currently only one error can happen in the function, but if there
532              * will be more of them, this message must go away */
533             msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
534                       (char*)&(p_pic->format.i_chroma) );
535             picture_Release( p_pic );
536             return NULL;
537         }
538     }
539
540     return CopyInfoAndRelease( p_outpic, p_pic );
541 }
542
543 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
544                            vlc_value_t oldval, vlc_value_t newval,
545                            void *p_data )
546 {
547     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
548     filter_sys_t *p_sys = (filter_sys_t *)p_data;
549
550     vlc_mutex_lock( &p_sys->lock );
551     if( !strcmp( psz_var, "contrast" ) )
552         p_sys->f_contrast = newval.f_float;
553     else if( !strcmp( psz_var, "brightness" ) )
554         p_sys->f_brightness = newval.f_float;
555     else if( !strcmp( psz_var, "hue" ) )
556         p_sys->i_hue = newval.i_int;
557     else if( !strcmp( psz_var, "saturation" ) )
558         p_sys->f_saturation = newval.f_float;
559     else if( !strcmp( psz_var, "gamma" ) )
560         p_sys->f_gamma = newval.f_float;
561     else if( !strcmp( psz_var, "brightness-threshold" ) )
562         p_sys->b_brightness_threshold = newval.b_bool;
563     vlc_mutex_unlock( &p_sys->lock );
564
565     return VLC_SUCCESS;
566 }