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