f17b2d61f1aca33dc046a9404cfb3cd271cce367
[vlc.git] / 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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>
29 #include <math.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc_sout.h>
33 #include <vlc_vout.h>
34
35 #include "vlc_filter.h"
36
37 #ifndef M_PI
38 #   define M_PI 3.14159265358979323846
39 #endif
40
41 #define eight_times( x )    x x x x x x x x
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  Create    ( vlc_object_t * );
47 static void Destroy   ( vlc_object_t * );
48
49 static picture_t *Filter( filter_t *, picture_t * );
50 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
51                            vlc_value_t oldval, vlc_value_t newval,
52                            void *p_data );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57
58 #define THRES_TEXT N_("Brightness threshold")
59 #define THRES_LONGTEXT N_("When this mode is enabled, pixels will be " \
60         "shown as black or white. The threshold value will be the brighness " \
61         "defined below." )
62 #define CONT_TEXT N_("Image contrast (0-2)")
63 #define CONT_LONGTEXT N_("Set the image contrast, between 0 and 2. Defaults to 1.")
64 #define HUE_TEXT N_("Image hue (0-360)")
65 #define HUE_LONGTEXT N_("Set the image hue, between 0 and 360. Defaults to 0.")
66 #define SAT_TEXT N_("Image saturation (0-3)")
67 #define SAT_LONGTEXT N_("Set the image saturation, between 0 and 3. Defaults to 1.")
68 #define LUM_TEXT N_("Image brightness (0-2)")
69 #define LUM_LONGTEXT N_("Set the image brightness, between 0 and 2. Defaults to 1.")
70 #define GAMMA_TEXT N_("Image gamma (0-10)")
71 #define GAMMA_LONGTEXT N_("Set the image gamma, between 0.01 and 10. Defaults to 1.")
72
73 vlc_module_begin();
74     set_description( _("Image properties filter") );
75     set_shortname( _("Image adjust" ));
76     set_category( CAT_VIDEO );
77     set_subcategory( SUBCAT_VIDEO_VFILTER );
78     set_capability( "video filter2", 0 );
79
80     add_float_with_range( "contrast", 1.0, 0.0, 2.0, NULL,
81                           CONT_TEXT, CONT_LONGTEXT, VLC_FALSE );
82     add_float_with_range( "brightness", 1.0, 0.0, 2.0, NULL,
83                            LUM_TEXT, LUM_LONGTEXT, VLC_FALSE );
84     add_integer_with_range( "hue", 0, 0, 360, NULL,
85                             HUE_TEXT, HUE_LONGTEXT, VLC_FALSE );
86     add_float_with_range( "saturation", 1.0, 0.0, 3.0, NULL,
87                           SAT_TEXT, SAT_LONGTEXT, VLC_FALSE );
88     add_float_with_range( "gamma", 1.0, 0.01, 10.0, NULL,
89                           GAMMA_TEXT, GAMMA_LONGTEXT, VLC_FALSE );
90
91     add_bool( "brightness-threshold", 0, NULL,
92               THRES_TEXT, THRES_LONGTEXT, VLC_FALSE );
93
94     add_shortcut( "adjust" );
95     set_callbacks( Create, Destroy );
96 vlc_module_end();
97
98 static const char *ppsz_filter_options[] = {
99     "contrast", "brightness", "hue", "saturation", "gamma",
100     "brightness-threshold", NULL
101 };
102
103 /*****************************************************************************
104  * filter_sys_t: adjust filter method descriptor
105  *****************************************************************************/
106 struct filter_sys_t
107 {
108     double     f_contrast;
109     double     f_brightness;
110     int        i_hue;
111     double     f_saturation;
112     double     f_gamma;
113     vlc_bool_t b_brightness_threshold;
114 };
115
116 /*****************************************************************************
117  * Create: allocates adjust video thread output method
118  *****************************************************************************
119  * This function allocates and initializes a adjust vout method.
120  *****************************************************************************/
121 static int Create( vlc_object_t *p_this )
122 {
123     filter_t *p_filter = (filter_t *)p_this;
124     filter_sys_t *p_sys;
125
126     /* XXX: we might need to add/remove some FOURCCs ... */
127     if(   p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','0')
128        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','Y','U','V')
129        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','0')
130        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','1','2')
131        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','U','Y','V')
132        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('U','Y','V','Y')
133        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('U','Y','N','V')
134        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','4','2','2')
135        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('c','y','u','v')
136        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','Y','2')
137        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','N','V')
138        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','Y','U')
139        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','1','1')
140        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','1','0')
141        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','V','U','9')
142        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','M','G','A')
143        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','2','2')
144        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','2','2')
145        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('I','4','4','4')
146        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('J','4','4','4')
147        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','P')
148        && p_filter->fmt_in.video.i_chroma != VLC_FOURCC('Y','U','V','A') )
149     {
150         msg_Err( p_filter, "Unsupported input chroma (%4s)",
151                  (char*)&(p_filter->fmt_in.video.i_chroma) );
152         return VLC_EGENERIC;
153     }
154
155     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
156     {
157         msg_Err( p_filter, "Input and output chromas don't match" );
158         return VLC_EGENERIC;
159     }
160
161     /* Allocate structure */
162     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
163     if( p_filter->p_sys == NULL )
164     {
165         msg_Err( p_filter, "out of memory" );
166         return VLC_ENOMEM;
167     }
168     p_sys = p_filter->p_sys;
169
170     p_filter->pf_video_filter = Filter;
171
172     /* needed to get options passed in transcode using the
173      * adjust{name=value} syntax */
174     config_ChainParse( p_filter, "", ppsz_filter_options,
175                    p_filter->p_cfg );
176
177     p_sys->f_contrast = var_CreateGetFloatCommand( p_filter, "contrast" );
178     p_sys->f_brightness = var_CreateGetFloatCommand( p_filter, "brightness" );
179     p_sys->i_hue = var_CreateGetIntegerCommand( p_filter, "hue" );
180     p_sys->f_saturation = var_CreateGetFloatCommand( p_filter, "saturation" );
181     p_sys->f_gamma = var_CreateGetFloatCommand( p_filter, "gamma" );
182     p_sys->b_brightness_threshold =
183         var_CreateGetBoolCommand( p_filter, "brightness-threshold" );
184
185     var_AddCallback( p_filter, "contrast",   AdjustCallback, p_sys );
186     var_AddCallback( p_filter, "brightness", AdjustCallback, p_sys );
187     var_AddCallback( p_filter, "hue",        AdjustCallback, p_sys );
188     var_AddCallback( p_filter, "saturation", AdjustCallback, p_sys );
189     var_AddCallback( p_filter, "gamma",      AdjustCallback, p_sys );
190     var_AddCallback( p_filter, "brightness-threshold",
191                                              AdjustCallback, p_sys );
192
193     return VLC_SUCCESS;
194 }
195
196 /*****************************************************************************
197  * Destroy: destroy adjust video thread output method
198  *****************************************************************************
199  * Terminate an output method created by adjustCreateOutputMethod
200  *****************************************************************************/
201 static void Destroy( vlc_object_t *p_this )
202 {
203     filter_t *p_filter = (filter_t *)p_this;
204     free( p_filter->p_sys );
205 }
206
207 /*****************************************************************************
208  * Render: displays previously rendered output
209  *****************************************************************************
210  * This function send the currently rendered image to adjust modified image,
211  * waits until it is displayed and switch the two rendering buffers, preparing
212  * next frame.
213  *****************************************************************************/
214 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
215 {
216     int pi_luma[256];
217     int pi_gamma[256];
218
219     picture_t *p_outpic;
220     uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
221     uint8_t *p_out, *p_out_v;
222
223     vlc_bool_t b_thres;
224     double  f_hue;
225     double  f_gamma;
226     int32_t i_cont, i_lum;
227     int i_sat, i_sin, i_cos, i_x, i_y;
228     int i;
229
230     filter_sys_t *p_sys = p_filter->p_sys;
231
232     if( !p_pic ) return NULL;
233
234     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
235     if( !p_outpic )
236     {
237         msg_Warn( p_filter, "can't get output picture" );
238         if( p_pic->pf_release )
239             p_pic->pf_release( p_pic );
240         return NULL;
241     }
242
243     /* Getvariables */
244     i_cont = (int)( p_sys->f_contrast * 255 );
245     i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
246     f_hue = (float)( p_sys->i_hue * M_PI / 180 );
247     i_sat = (int)( p_sys->f_saturation * 256 );
248     f_gamma = 1.0 / p_sys->f_gamma;
249     b_thres = p_sys->b_brightness_threshold;
250
251     /*
252      * Threshold mode drops out everything about luma, contrast and gamma.
253      */
254     if( b_thres != VLC_TRUE )
255     {
256
257         /* Contrast is a fast but kludged function, so I put this gap to be
258          * cleaner :) */
259         i_lum += 128 - i_cont / 2;
260
261         /* Fill the gamma lookup table */
262         for( i = 0 ; i < 256 ; i++ )
263         {
264           pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
265         }
266
267         /* Fill the luma lookup table */
268         for( i = 0 ; i < 256 ; i++ )
269         {
270             pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
271         }
272     }
273     else
274     {
275         /*
276          * We get luma as threshold value: the higher it is, the darker is
277          * the image. Should I reverse this?
278          */
279         for( i = 0 ; i < 256 ; i++ )
280         {
281             pi_luma[ i ] = (i < i_lum) ? 0 : 255;
282         }
283
284         /*
285          * Desaturates image to avoid that strange yellow halo...
286          */
287         i_sat = 0;
288     }
289
290     /*
291      * Do the Y plane
292      */
293
294     p_in = p_pic->p[Y_PLANE].p_pixels;
295     p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
296                       * p_pic->p[Y_PLANE].i_pitch - 8;
297
298     p_out = p_outpic->p[Y_PLANE].p_pixels;
299
300     for( ; p_in < p_in_end ; )
301     {
302         p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
303
304         for( ; p_in < p_line_end ; )
305         {
306             /* Do 8 pixels at a time */
307             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
308             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
309             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
310             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
311         }
312
313         p_line_end += 8;
314
315         for( ; p_in < p_line_end ; )
316         {
317             *p_out++ = pi_luma[ *p_in++ ];
318         }
319
320         p_in += p_pic->p[Y_PLANE].i_pitch
321               - p_pic->p[Y_PLANE].i_visible_pitch;
322         p_out += p_outpic->p[Y_PLANE].i_pitch
323                - p_outpic->p[Y_PLANE].i_visible_pitch;
324     }
325
326     /*
327      * Do the U and V planes
328      */
329
330     p_in = p_pic->p[U_PLANE].p_pixels;
331     p_in_v = p_pic->p[V_PLANE].p_pixels;
332     p_in_end = p_in + p_pic->p[U_PLANE].i_visible_lines
333                       * p_pic->p[U_PLANE].i_pitch - 8;
334
335     p_out = p_outpic->p[U_PLANE].p_pixels;
336     p_out_v = p_outpic->p[V_PLANE].p_pixels;
337
338     i_sin = sin(f_hue) * 256;
339     i_cos = cos(f_hue) * 256;
340
341     i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
342     i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
343
344     if ( i_sat > 256 )
345     {
346 #define WRITE_UV_CLIP() \
347     i_u = *p_in++ ; i_v = *p_in_v++ ; \
348     *p_out++ = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
349                            * i_sat) >> 8) + 128); \
350     *p_out_v++ = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
351                            * i_sat) >> 8) + 128)
352
353         uint8_t i_u, i_v;
354
355         for( ; p_in < p_in_end ; )
356         {
357             p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
358
359             for( ; p_in < p_line_end ; )
360             {
361                 /* Do 8 pixels at a time */
362                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
363                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
364                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
365                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
366             }
367
368             p_line_end += 8;
369
370             for( ; p_in < p_line_end ; )
371             {
372                 WRITE_UV_CLIP();
373             }
374
375             p_in += p_pic->p[U_PLANE].i_pitch
376                   - p_pic->p[U_PLANE].i_visible_pitch;
377             p_in_v += p_pic->p[V_PLANE].i_pitch
378                     - p_pic->p[V_PLANE].i_visible_pitch;
379             p_out += p_outpic->p[U_PLANE].i_pitch
380                    - p_outpic->p[U_PLANE].i_visible_pitch;
381             p_out_v += p_outpic->p[V_PLANE].i_pitch
382                      - p_outpic->p[V_PLANE].i_visible_pitch;
383         }
384     }
385     else
386     {
387 #define WRITE_UV() \
388     i_u = *p_in++ ; i_v = *p_in_v++ ; \
389     *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
390                        * i_sat) >> 8) + 128; \
391     *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
392                        * i_sat) >> 8) + 128
393
394         uint8_t i_u, i_v;
395
396         for( ; p_in < p_in_end ; )
397         {
398             p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
399
400             for( ; p_in < p_line_end ; )
401             {
402                 /* Do 8 pixels at a time */
403                 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
404                 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
405             }
406
407             p_line_end += 8;
408
409             for( ; p_in < p_line_end ; )
410             {
411                 WRITE_UV();
412             }
413
414             p_in += p_pic->p[U_PLANE].i_pitch
415                   - p_pic->p[U_PLANE].i_visible_pitch;
416             p_in_v += p_pic->p[V_PLANE].i_pitch
417                     - p_pic->p[V_PLANE].i_visible_pitch;
418             p_out += p_outpic->p[U_PLANE].i_pitch
419                    - p_outpic->p[U_PLANE].i_visible_pitch;
420             p_out_v += p_outpic->p[V_PLANE].i_pitch
421                      - p_outpic->p[V_PLANE].i_visible_pitch;
422         }
423     }
424
425     p_outpic->date = p_pic->date;
426     p_outpic->b_force = p_pic->b_force;
427     p_outpic->i_nb_fields = p_pic->i_nb_fields;
428     p_outpic->b_progressive = p_pic->b_progressive;
429     p_outpic->b_top_field_first = p_pic->b_top_field_first;
430
431     if( p_pic->pf_release )
432         p_pic->pf_release( p_pic );
433
434     return p_outpic;
435 }
436
437 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
438                            vlc_value_t oldval, vlc_value_t newval,
439                            void *p_data )
440 {
441     filter_sys_t *p_sys = (filter_sys_t *)p_data;
442
443     if( !strcmp( psz_var, "contrast" ) )
444         p_sys->f_contrast = newval.f_float;
445     else if( !strcmp( psz_var, "brightness" ) )
446         p_sys->f_brightness = newval.f_float;
447     else if( !strcmp( psz_var, "hue" ) )
448         p_sys->i_hue = newval.i_int;
449     else if( !strcmp( psz_var, "saturation" ) )
450         p_sys->f_saturation = newval.f_float;
451     else if( !strcmp( psz_var, "gamma" ) )
452         p_sys->f_gamma = newval.f_float;
453     else if( !strcmp( psz_var, "brightness-threshold" ) )
454         p_sys->b_brightness_threshold = newval.b_bool;
455     else
456         return VLC_EGENERIC;
457     return VLC_SUCCESS;
458 }