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