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