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