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