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