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