Revert the so-called whitelisting commits that are actually blacklisting
[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 #include "filter_picture.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 *FilterPlanar( filter_t *, picture_t * );
51 static picture_t *FilterPacked( filter_t *, picture_t * );
52 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
53                            vlc_value_t oldval, vlc_value_t newval,
54                            void *p_data );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59
60 #define THRES_TEXT N_("Brightness threshold")
61 #define THRES_LONGTEXT N_("When this mode is enabled, pixels will be " \
62         "shown as black or white. The threshold value will be the brighness " \
63         "defined below." )
64 #define CONT_TEXT N_("Image contrast (0-2)")
65 #define CONT_LONGTEXT N_("Set the image contrast, between 0 and 2. Defaults to 1.")
66 #define HUE_TEXT N_("Image hue (0-360)")
67 #define HUE_LONGTEXT N_("Set the image hue, between 0 and 360. Defaults to 0.")
68 #define SAT_TEXT N_("Image saturation (0-3)")
69 #define SAT_LONGTEXT N_("Set the image saturation, between 0 and 3. Defaults to 1.")
70 #define LUM_TEXT N_("Image brightness (0-2)")
71 #define LUM_LONGTEXT N_("Set the image brightness, between 0 and 2. Defaults to 1.")
72 #define GAMMA_TEXT N_("Image gamma (0-10)")
73 #define GAMMA_LONGTEXT N_("Set the image gamma, between 0.01 and 10. Defaults to 1.")
74
75 vlc_module_begin();
76     set_description( _("Image properties filter") );
77     set_shortname( _("Image adjust" ));
78     set_category( CAT_VIDEO );
79     set_subcategory( SUBCAT_VIDEO_VFILTER );
80     set_capability( "video filter2", 0 );
81
82     add_float_with_range( "contrast", 1.0, 0.0, 2.0, NULL,
83                           CONT_TEXT, CONT_LONGTEXT, VLC_FALSE );
84     add_float_with_range( "brightness", 1.0, 0.0, 2.0, NULL,
85                            LUM_TEXT, LUM_LONGTEXT, VLC_FALSE );
86     add_integer_with_range( "hue", 0, 0, 360, NULL,
87                             HUE_TEXT, HUE_LONGTEXT, VLC_FALSE );
88     add_float_with_range( "saturation", 1.0, 0.0, 3.0, NULL,
89                           SAT_TEXT, SAT_LONGTEXT, VLC_FALSE );
90     add_float_with_range( "gamma", 1.0, 0.01, 10.0, NULL,
91                           GAMMA_TEXT, GAMMA_LONGTEXT, VLC_FALSE );
92
93     add_bool( "brightness-threshold", 0, NULL,
94               THRES_TEXT, THRES_LONGTEXT, VLC_FALSE );
95
96     add_shortcut( "adjust" );
97     set_callbacks( Create, Destroy );
98 vlc_module_end();
99
100 static const char *ppsz_filter_options[] = {
101     "contrast", "brightness", "hue", "saturation", "gamma",
102     "brightness-threshold", NULL
103 };
104
105 /*****************************************************************************
106  * filter_sys_t: adjust filter method descriptor
107  *****************************************************************************/
108 struct filter_sys_t
109 {
110     double     f_contrast;
111     double     f_brightness;
112     int        i_hue;
113     double     f_saturation;
114     double     f_gamma;
115     vlc_bool_t b_brightness_threshold;
116 };
117
118 /*****************************************************************************
119  * Create: allocates adjust video filter
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     switch( p_filter->fmt_in.video.i_chroma )
127     {
128         CASE_PLANAR_YUV
129             /* Planar YUV */
130             p_filter->pf_video_filter = FilterPlanar;
131             break;
132
133         CASE_PACKED_YUV_422
134             /* Packed YUV 4:2:2 */
135             p_filter->pf_video_filter = FilterPacked;
136             break;
137
138         default:
139             msg_Err( p_filter, "Unsupported input chroma (%4s)",
140                      (char*)&(p_filter->fmt_in.video.i_chroma) );
141             return VLC_EGENERIC;
142     }
143
144     if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
145     {
146         msg_Err( p_filter, "Input and output chromas don't match" );
147         return VLC_EGENERIC;
148     }
149
150     /* Allocate structure */
151     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
152     if( p_filter->p_sys == NULL )
153     {
154         msg_Err( p_filter, "out of memory" );
155         return VLC_ENOMEM;
156     }
157     p_sys = p_filter->p_sys;
158
159     /* needed to get options passed in transcode using the
160      * adjust{name=value} syntax */
161     config_ChainParse( p_filter, "", ppsz_filter_options,
162                    p_filter->p_cfg );
163
164     p_sys->f_contrast = var_CreateGetFloatCommand( p_filter, "contrast" );
165     p_sys->f_brightness = var_CreateGetFloatCommand( p_filter, "brightness" );
166     p_sys->i_hue = var_CreateGetIntegerCommand( p_filter, "hue" );
167     p_sys->f_saturation = var_CreateGetFloatCommand( p_filter, "saturation" );
168     p_sys->f_gamma = var_CreateGetFloatCommand( p_filter, "gamma" );
169     p_sys->b_brightness_threshold =
170         var_CreateGetBoolCommand( p_filter, "brightness-threshold" );
171
172     var_AddCallback( p_filter, "contrast",   AdjustCallback, p_sys );
173     var_AddCallback( p_filter, "brightness", AdjustCallback, p_sys );
174     var_AddCallback( p_filter, "hue",        AdjustCallback, p_sys );
175     var_AddCallback( p_filter, "saturation", AdjustCallback, p_sys );
176     var_AddCallback( p_filter, "gamma",      AdjustCallback, p_sys );
177     var_AddCallback( p_filter, "brightness-threshold",
178                                              AdjustCallback, p_sys );
179
180     return VLC_SUCCESS;
181 }
182
183 /*****************************************************************************
184  * Destroy: destroy adjust video filter
185  *****************************************************************************/
186 static void Destroy( vlc_object_t *p_this )
187 {
188     filter_t *p_filter = (filter_t *)p_this;
189     free( p_filter->p_sys );
190 }
191
192 /*****************************************************************************
193  * Run the filter on a Planar YUV picture
194  *****************************************************************************/
195 static picture_t *FilterPlanar( filter_t *p_filter, picture_t *p_pic )
196 {
197     int pi_luma[256];
198     int pi_gamma[256];
199
200     picture_t *p_outpic;
201     uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
202     uint8_t *p_out, *p_out_v;
203
204     vlc_bool_t b_thres;
205     double  f_hue;
206     double  f_gamma;
207     int32_t i_cont, i_lum;
208     int i_sat, i_sin, i_cos, i_x, i_y;
209     int i;
210
211     filter_sys_t *p_sys = p_filter->p_sys;
212
213     if( !p_pic ) return NULL;
214
215     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
216     if( !p_outpic )
217     {
218         msg_Warn( p_filter, "can't get output picture" );
219         if( p_pic->pf_release )
220             p_pic->pf_release( p_pic );
221         return NULL;
222     }
223
224     /* Getvariables */
225     i_cont = (int)( p_sys->f_contrast * 255 );
226     i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
227     f_hue = (float)( p_sys->i_hue * M_PI / 180 );
228     i_sat = (int)( p_sys->f_saturation * 256 );
229     f_gamma = 1.0 / p_sys->f_gamma;
230     b_thres = p_sys->b_brightness_threshold;
231
232     /*
233      * Threshold mode drops out everything about luma, contrast and gamma.
234      */
235     if( b_thres != VLC_TRUE )
236     {
237
238         /* Contrast is a fast but kludged function, so I put this gap to be
239          * cleaner :) */
240         i_lum += 128 - i_cont / 2;
241
242         /* Fill the gamma lookup table */
243         for( i = 0 ; i < 256 ; i++ )
244         {
245           pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
246         }
247
248         /* Fill the luma lookup table */
249         for( i = 0 ; i < 256 ; i++ )
250         {
251             pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
252         }
253     }
254     else
255     {
256         /*
257          * We get luma as threshold value: the higher it is, the darker is
258          * the image. Should I reverse this?
259          */
260         for( i = 0 ; i < 256 ; i++ )
261         {
262             pi_luma[ i ] = (i < i_lum) ? 0 : 255;
263         }
264
265         /*
266          * Desaturates image to avoid that strange yellow halo...
267          */
268         i_sat = 0;
269     }
270
271     /*
272      * Do the Y plane
273      */
274
275     p_in = p_pic->p[Y_PLANE].p_pixels;
276     p_in_end = p_in + p_pic->p[Y_PLANE].i_visible_lines
277                       * p_pic->p[Y_PLANE].i_pitch - 8;
278
279     p_out = p_outpic->p[Y_PLANE].p_pixels;
280
281     for( ; p_in < p_in_end ; )
282     {
283         p_line_end = p_in + p_pic->p[Y_PLANE].i_visible_pitch - 8;
284
285         for( ; p_in < p_line_end ; )
286         {
287             /* Do 8 pixels at a time */
288             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
289             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
290             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
291             *p_out++ = pi_luma[ *p_in++ ]; *p_out++ = pi_luma[ *p_in++ ];
292         }
293
294         p_line_end += 8;
295
296         for( ; p_in < p_line_end ; )
297         {
298             *p_out++ = pi_luma[ *p_in++ ];
299         }
300
301         p_in += p_pic->p[Y_PLANE].i_pitch
302               - p_pic->p[Y_PLANE].i_visible_pitch;
303         p_out += p_outpic->p[Y_PLANE].i_pitch
304                - p_outpic->p[Y_PLANE].i_visible_pitch;
305     }
306
307     /*
308      * Do the U and V planes
309      */
310
311     p_in = p_pic->p[U_PLANE].p_pixels;
312     p_in_v = p_pic->p[V_PLANE].p_pixels;
313     p_in_end = p_in + p_pic->p[U_PLANE].i_visible_lines
314                       * p_pic->p[U_PLANE].i_pitch - 8;
315
316     p_out = p_outpic->p[U_PLANE].p_pixels;
317     p_out_v = p_outpic->p[V_PLANE].p_pixels;
318
319     i_sin = sin(f_hue) * 256;
320     i_cos = cos(f_hue) * 256;
321
322     i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
323     i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
324
325     if ( i_sat > 256 )
326     {
327 #define WRITE_UV_CLIP() \
328     i_u = *p_in++ ; i_v = *p_in_v++ ; \
329     *p_out++ = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
330                            * i_sat) >> 8) + 128); \
331     *p_out_v++ = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
332                            * i_sat) >> 8) + 128)
333
334         uint8_t i_u, i_v;
335
336         for( ; p_in < p_in_end ; )
337         {
338             p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
339
340             for( ; p_in < p_line_end ; )
341             {
342                 /* Do 8 pixels at a time */
343                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
344                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
345                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
346                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
347             }
348
349             p_line_end += 8;
350
351             for( ; p_in < p_line_end ; )
352             {
353                 WRITE_UV_CLIP();
354             }
355
356             p_in += p_pic->p[U_PLANE].i_pitch
357                   - p_pic->p[U_PLANE].i_visible_pitch;
358             p_in_v += p_pic->p[V_PLANE].i_pitch
359                     - p_pic->p[V_PLANE].i_visible_pitch;
360             p_out += p_outpic->p[U_PLANE].i_pitch
361                    - p_outpic->p[U_PLANE].i_visible_pitch;
362             p_out_v += p_outpic->p[V_PLANE].i_pitch
363                      - p_outpic->p[V_PLANE].i_visible_pitch;
364         }
365 #undef WRITE_UV_CLIP
366     }
367     else
368     {
369 #define WRITE_UV() \
370     i_u = *p_in++ ; i_v = *p_in_v++ ; \
371     *p_out++ = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
372                        * i_sat) >> 8) + 128; \
373     *p_out_v++ = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
374                        * i_sat) >> 8) + 128
375
376         uint8_t i_u, i_v;
377
378         for( ; p_in < p_in_end ; )
379         {
380             p_line_end = p_in + p_pic->p[U_PLANE].i_visible_pitch - 8;
381
382             for( ; p_in < p_line_end ; )
383             {
384                 /* Do 8 pixels at a time */
385                 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
386                 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
387             }
388
389             p_line_end += 8;
390
391             for( ; p_in < p_line_end ; )
392             {
393                 WRITE_UV();
394             }
395
396             p_in += p_pic->p[U_PLANE].i_pitch
397                   - p_pic->p[U_PLANE].i_visible_pitch;
398             p_in_v += p_pic->p[V_PLANE].i_pitch
399                     - p_pic->p[V_PLANE].i_visible_pitch;
400             p_out += p_outpic->p[U_PLANE].i_pitch
401                    - p_outpic->p[U_PLANE].i_visible_pitch;
402             p_out_v += p_outpic->p[V_PLANE].i_pitch
403                      - p_outpic->p[V_PLANE].i_visible_pitch;
404         }
405 #undef WRITE_UV
406     }
407
408     p_outpic->date = p_pic->date;
409     p_outpic->b_force = p_pic->b_force;
410     p_outpic->i_nb_fields = p_pic->i_nb_fields;
411     p_outpic->b_progressive = p_pic->b_progressive;
412     p_outpic->b_top_field_first = p_pic->b_top_field_first;
413
414     if( p_pic->pf_release )
415         p_pic->pf_release( p_pic );
416
417     return p_outpic;
418 }
419
420 /*****************************************************************************
421  * Run the filter on a Packed YUV picture
422  *****************************************************************************/
423 static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
424 {
425     int pi_luma[256];
426     int pi_gamma[256];
427
428     picture_t *p_outpic;
429     uint8_t *p_in, *p_in_v, *p_in_end, *p_line_end;
430     uint8_t *p_out, *p_out_v;
431     int i_y_offset, i_u_offset, i_v_offset;
432
433     int i_lines, i_visible_lines, i_pitch, i_visible_pitch;
434
435     vlc_bool_t b_thres;
436     double  f_hue;
437     double  f_gamma;
438     int32_t i_cont, i_lum;
439     int i_sat, i_sin, i_cos, i_x, i_y;
440     int i;
441
442     filter_sys_t *p_sys = p_filter->p_sys;
443
444     if( !p_pic ) return NULL;
445
446     i_lines = p_pic->p->i_lines;
447     i_visible_lines = p_pic->p->i_visible_lines;
448     i_pitch = p_pic->p->i_pitch;
449     i_visible_pitch = p_pic->p->i_visible_pitch;
450
451     if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
452                              &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
453     {
454         msg_Warn( p_filter, "Unsupported input chroma (%4s)",
455                   (char*)&(p_pic->format.i_chroma) );
456         if( p_pic->pf_release )
457             p_pic->pf_release( p_pic );
458         return NULL;
459     }
460
461     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
462     if( !p_outpic )
463     {
464         msg_Warn( p_filter, "can't get output picture" );
465         if( p_pic->pf_release )
466             p_pic->pf_release( p_pic );
467         return NULL;
468     }
469
470     /* Getvariables */
471     i_cont = (int)( p_sys->f_contrast * 255 );
472     i_lum = (int)( (p_sys->f_brightness - 1.0)*255 );
473     f_hue = (float)( p_sys->i_hue * M_PI / 180 );
474     i_sat = (int)( p_sys->f_saturation * 256 );
475     f_gamma = 1.0 / p_sys->f_gamma;
476     b_thres = p_sys->b_brightness_threshold;
477
478     /*
479      * Threshold mode drops out everything about luma, contrast and gamma.
480      */
481     if( b_thres != VLC_TRUE )
482     {
483
484         /* Contrast is a fast but kludged function, so I put this gap to be
485          * cleaner :) */
486         i_lum += 128 - i_cont / 2;
487
488         /* Fill the gamma lookup table */
489         for( i = 0 ; i < 256 ; i++ )
490         {
491           pi_gamma[ i ] = clip_uint8_vlc( pow(i / 255.0, f_gamma) * 255.0);
492         }
493
494         /* Fill the luma lookup table */
495         for( i = 0 ; i < 256 ; i++ )
496         {
497             pi_luma[ i ] = pi_gamma[clip_uint8_vlc( i_lum + i_cont * i / 256)];
498         }
499     }
500     else
501     {
502         /*
503          * We get luma as threshold value: the higher it is, the darker is
504          * the image. Should I reverse this?
505          */
506         for( i = 0 ; i < 256 ; i++ )
507         {
508             pi_luma[ i ] = (i < i_lum) ? 0 : 255;
509         }
510
511         /*
512          * Desaturates image to avoid that strange yellow halo...
513          */
514         i_sat = 0;
515     }
516
517     /*
518      * Do the Y plane
519      */
520
521     p_in = p_pic->p->p_pixels + i_y_offset;
522     p_in_end = p_in + p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 * 4;
523
524     p_out = p_outpic->p->p_pixels + i_y_offset;
525
526     for( ; p_in < p_in_end ; )
527     {
528         p_line_end = p_in + i_visible_pitch - 8 * 4;
529
530         for( ; p_in < p_line_end ; )
531         {
532             /* Do 8 pixels at a time */
533             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
534             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
535             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
536             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
537             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
538             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
539             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
540             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
541         }
542
543         p_line_end += 8 * 4;
544
545         for( ; p_in < p_line_end ; )
546         {
547             *p_out = pi_luma[ *p_in ]; p_in += 2; p_out += 2;
548         }
549
550         p_in += i_pitch - p_pic->p->i_visible_pitch;
551         p_out += i_pitch - p_outpic->p->i_visible_pitch;
552     }
553
554     /*
555      * Do the U and V planes
556      */
557
558     p_in = p_pic->p->p_pixels + i_u_offset;
559     p_in_v = p_pic->p->p_pixels + i_v_offset;
560     p_in_end = p_in + i_visible_lines * i_pitch - 8 * 4;
561
562     p_out = p_outpic->p->p_pixels + i_u_offset;
563     p_out_v = p_outpic->p->p_pixels + i_v_offset;
564
565     i_sin = sin(f_hue) * 256;
566     i_cos = cos(f_hue) * 256;
567
568     i_x = ( cos(f_hue) + sin(f_hue) ) * 32768;
569     i_y = ( cos(f_hue) - sin(f_hue) ) * 32768;
570
571     if ( i_sat > 256 )
572     {
573 #define WRITE_UV_CLIP() \
574     i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
575     *p_out = clip_uint8_vlc( (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
576                            * i_sat) >> 8) + 128); \
577     p_out += 4; \
578     *p_out_v = clip_uint8_vlc( (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
579                            * i_sat) >> 8) + 128); \
580     p_out_v += 4
581
582         uint8_t i_u, i_v;
583
584         for( ; p_in < p_in_end ; )
585         {
586             p_line_end = p_in + i_visible_pitch - 8 * 4;
587
588             for( ; p_in < p_line_end ; )
589             {
590                 /* Do 8 pixels at a time */
591                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
592                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
593                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
594                 WRITE_UV_CLIP(); WRITE_UV_CLIP();
595             }
596
597             p_line_end += 8 * 4;
598
599             for( ; p_in < p_line_end ; )
600             {
601                 WRITE_UV_CLIP();
602             }
603
604             p_in += i_pitch - i_visible_pitch;
605             p_in_v += i_pitch - i_visible_pitch;
606             p_out += i_pitch - i_visible_pitch;
607             p_out_v += i_pitch - i_visible_pitch;
608         }
609 #undef WRITE_UV_CLIP
610     }
611     else
612     {
613 #define WRITE_UV() \
614     i_u = *p_in; p_in += 4; i_v = *p_in_v; p_in_v += 4; \
615     *p_out = (( ((i_u * i_cos + i_v * i_sin - i_x) >> 8) \
616                        * i_sat) >> 8) + 128; \
617     p_out += 4; \
618     *p_out_v = (( ((i_v * i_cos - i_u * i_sin - i_y) >> 8) \
619                        * i_sat) >> 8) + 128; \
620     p_out_v += 4
621
622         uint8_t i_u, i_v;
623
624         for( ; p_in < p_in_end ; )
625         {
626             p_line_end = p_in + i_visible_pitch - 8 * 4;
627
628             for( ; p_in < p_line_end ; )
629             {
630                 /* Do 8 pixels at a time */
631                 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
632                 WRITE_UV(); WRITE_UV(); WRITE_UV(); WRITE_UV();
633             }
634
635             p_line_end += 8 * 4;
636
637             for( ; p_in < p_line_end ; )
638             {
639                 WRITE_UV();
640             }
641
642             p_in += i_pitch - i_visible_pitch;
643             p_in_v += i_pitch - i_visible_pitch;
644             p_out += i_pitch - i_visible_pitch;
645             p_out_v += i_pitch - i_visible_pitch;
646         }
647 #undef WRITE_UV
648     }
649
650     p_outpic->date = p_pic->date;
651     p_outpic->b_force = p_pic->b_force;
652     p_outpic->i_nb_fields = p_pic->i_nb_fields;
653     p_outpic->b_progressive = p_pic->b_progressive;
654     p_outpic->b_top_field_first = p_pic->b_top_field_first;
655
656     if( p_pic->pf_release )
657         p_pic->pf_release( p_pic );
658
659     return p_outpic;
660 }
661
662 static int AdjustCallback( vlc_object_t *p_this, char const *psz_var,
663                            vlc_value_t oldval, vlc_value_t newval,
664                            void *p_data )
665 {
666     filter_sys_t *p_sys = (filter_sys_t *)p_data;
667
668     if( !strcmp( psz_var, "contrast" ) )
669         p_sys->f_contrast = newval.f_float;
670     else if( !strcmp( psz_var, "brightness" ) )
671         p_sys->f_brightness = newval.f_float;
672     else if( !strcmp( psz_var, "hue" ) )
673         p_sys->i_hue = newval.i_int;
674     else if( !strcmp( psz_var, "saturation" ) )
675         p_sys->f_saturation = newval.f_float;
676     else if( !strcmp( psz_var, "gamma" ) )
677         p_sys->f_gamma = newval.f_float;
678     else if( !strcmp( psz_var, "brightness-threshold" ) )
679         p_sys->b_brightness_threshold = newval.b_bool;
680     else
681         return VLC_EGENERIC;
682     return VLC_SUCCESS;
683 }