Make it possible to change the extracted color once the module is launched.
[vlc.git] / modules / video_filter / extract.c
1 /*****************************************************************************
2  * extract.c : Extract RGB components
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea .t videolan d@t org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_vout.h>
32
33 #include "vlc_filter.h"
34
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38 static int  Create      ( vlc_object_t * );
39 static void Destroy     ( vlc_object_t * );
40
41 static picture_t *Filter( filter_t *, picture_t * );
42 static int ExtractCallback( vlc_object_t *, char const *,
43                             vlc_value_t, vlc_value_t, void * );
44
45 static void get_red_from_yuv420( picture_t *, picture_t *, int, int, int );
46 static void get_green_from_yuv420( picture_t *, picture_t *, int, int, int );
47 static void get_blue_from_yuv420( picture_t *, picture_t *, int, int, int );
48 static void get_red_from_yuv422( picture_t *, picture_t *, int, int, int );
49 static void get_green_from_yuv422( picture_t *, picture_t *, int, int, int );
50 static void get_blue_from_yuv422( picture_t *, picture_t *, int, int, int );
51 static void make_projection_matrix( int color, int *matrix );
52 static void get_custom_from_yuv420( picture_t *, picture_t *, int, int, int, int * );
53 static void get_custom_from_yuv422( picture_t *, picture_t *, int, int, int, int * );
54
55
56 #define COMPONENT_TEXT N_("RGB component to extract")
57 #define COMPONENT_LONGTEXT N_("RGB component to extract. 0 for Red, 1 for Green and 2 for Blue.")
58 #define FILTER_PREFIX "extract-"
59
60 static int pi_component_values[] = { 0xFF0000, 0x00FF00, 0x0000FF };
61 static const char *ppsz_component_descriptions[] = { "Red", "Green", "Blue" };
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 vlc_module_begin();
67     set_description( _("Extract RGB component video filter") );
68     set_shortname( _("Extract" ));
69     set_category( CAT_VIDEO );
70     set_subcategory( SUBCAT_VIDEO_VFILTER );
71     set_capability( "video filter2", 0 );
72     add_shortcut( "extract" );
73
74     add_integer_with_range( FILTER_PREFIX "component", 0xFF0000, 1, 0xFFFFFF,
75                  NULL, COMPONENT_TEXT, COMPONENT_LONGTEXT, VLC_FALSE );
76         change_integer_list( pi_component_values, ppsz_component_descriptions, 0 );
77
78     set_callbacks( Create, Destroy );
79 vlc_module_end();
80
81 static const char *ppsz_filter_options[] = {
82     "component", NULL
83 };
84
85 enum { RED=0xFF0000, GREEN=0x00FF00, BLUE=0x0000FF };
86 struct filter_sys_t
87 {
88     int i_color;
89     int *projection_matrix;
90 };
91
92 /*****************************************************************************
93  * Create
94  *****************************************************************************/
95 static int Create( vlc_object_t *p_this )
96 {
97     filter_t *p_filter = (filter_t *)p_this;
98
99     /* Allocate structure */
100     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
101     if( p_filter->p_sys == NULL )
102     {
103         msg_Err( p_filter, "out of memory" );
104         return VLC_ENOMEM;
105     }
106     p_filter->p_sys->projection_matrix = malloc( 9 * sizeof( int ) );
107     if( !p_filter->p_sys->projection_matrix )
108     {
109         free( p_filter->p_sys );
110         msg_Err( p_filter, "out of memory" );
111         return VLC_ENOMEM;
112     }
113
114     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
115                        p_filter->p_cfg );
116
117     p_filter->p_sys->i_color = var_CreateGetIntegerCommand( p_filter,
118                                                FILTER_PREFIX "component" );
119     var_AddCallback( p_filter, FILTER_PREFIX "component",
120                      ExtractCallback, p_filter->p_sys );
121     switch( p_filter->p_sys->i_color )
122     {
123         case RED:
124         case GREEN:
125         case BLUE:
126             break;
127         default:
128             make_projection_matrix( p_filter->p_sys->i_color,
129                                     p_filter->p_sys->projection_matrix );
130             break;
131     }
132
133     p_filter->pf_video_filter = Filter;
134
135     return VLC_SUCCESS;
136 }
137
138 /*****************************************************************************
139  * Destroy
140  *****************************************************************************/
141 static void Destroy( vlc_object_t *p_this )
142 {
143     filter_t *p_filter = (filter_t *)p_this;
144
145     free( p_filter->p_sys->projection_matrix );
146     free( p_filter->p_sys );
147 }
148
149 /*****************************************************************************
150  * Render
151  *****************************************************************************/
152 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
153 {
154     picture_t *p_outpic;
155
156     if( !p_pic ) return NULL;
157
158     p_outpic = p_filter->pf_vout_buffer_new( p_filter );
159     if( !p_outpic )
160     {
161         msg_Warn( p_filter, "can't get output picture" );
162         if( p_pic->pf_release )
163             p_pic->pf_release( p_pic );
164         return NULL;
165     }
166
167     switch( p_pic->format.i_chroma )
168     {
169         case VLC_FOURCC('I','4','2','0'):
170         case VLC_FOURCC('I','Y','U','V'):
171         case VLC_FOURCC('J','4','2','0'):
172         case VLC_FOURCC('Y','V','1','2'):
173             switch( p_filter->p_sys->i_color )
174             {
175                 case RED:
176                     get_red_from_yuv420( p_pic, p_outpic,
177                                          Y_PLANE, U_PLANE, V_PLANE );
178                     break;
179                 case GREEN:
180                     get_green_from_yuv420( p_pic, p_outpic,
181                                            Y_PLANE, U_PLANE, V_PLANE );
182                     break;
183                 case BLUE:
184                     get_blue_from_yuv420( p_pic, p_outpic,
185                                           Y_PLANE, U_PLANE, V_PLANE );
186                     break;
187                 default:
188                     get_custom_from_yuv420( p_pic, p_outpic,
189                                             Y_PLANE, U_PLANE, V_PLANE,
190                                             p_filter->p_sys->projection_matrix);
191                     break;
192             }
193             break;
194
195         case VLC_FOURCC('I','4','2','2'):
196         case VLC_FOURCC('J','4','2','2'):
197             switch( p_filter->p_sys->i_color )
198             {
199                 case RED:
200                     get_red_from_yuv422( p_pic, p_outpic,
201                                          Y_PLANE, U_PLANE, V_PLANE );
202                     break;
203                 case GREEN:
204                     get_green_from_yuv422( p_pic, p_outpic,
205                                            Y_PLANE, U_PLANE, V_PLANE );
206                     break;
207                 case BLUE:
208                     get_blue_from_yuv422( p_pic, p_outpic,
209                                           Y_PLANE, U_PLANE, V_PLANE );
210                     break;
211                 default:
212                     get_custom_from_yuv422( p_pic, p_outpic,
213                                             Y_PLANE, U_PLANE, V_PLANE,
214                                             p_filter->p_sys->projection_matrix);
215                     break;
216             }
217             break;
218
219         default:
220             msg_Warn( p_filter, "Unsupported input chroma (%4s)",
221                       (char*)&(p_pic->format.i_chroma) );
222             if( p_pic->pf_release )
223                 p_pic->pf_release( p_pic );
224             return NULL;
225     }
226
227     p_outpic->date = p_pic->date;
228     p_outpic->b_force = p_pic->b_force;
229     p_outpic->i_nb_fields = p_pic->i_nb_fields;
230     p_outpic->b_progressive = p_pic->b_progressive;
231     p_outpic->b_top_field_first = p_pic->b_top_field_first;
232
233     if( p_pic->pf_release )
234         p_pic->pf_release( p_pic );
235
236     return p_outpic;
237 }
238
239 inline uint8_t crop( int a );
240 inline uint8_t crop( int a )
241 {
242     if( a < 0 ) return 0;
243     if( a > 255 ) return 255;
244     else return (uint8_t)a;
245 }
246
247 #define U 128
248 #define V 128
249
250 static void mmult( double *res, double *a, double *b )
251 {
252     int i, j, k;
253     for( i = 0; i < 3; i++ )
254     {
255         for( j = 0; j < 3; j++ )
256         {
257             res[ i*3 + j ] = 0.;
258             for( k = 0; k < 3; k++ )
259             {
260                 res[ i*3 + j ] += a[ i*3 + k ] * b[ k*3 + j ];
261             }
262         }
263     }
264 }
265 static void make_projection_matrix( int color, int *matrix )
266 {
267     double left_matrix[9] =
268         {  76.24500,  149.68500,  29.07000,
269           -43.02765,  -84.47235, 127.50000,
270           127.50000, -106.76534, -20.73466 };
271     double right_matrix[9] =
272         { 257.00392,   0.00000,  360.31950,
273           257.00392, -88.44438, -183.53583,
274           257.00392, 455.41095,    0.00000 };
275     double red = ((double)(( 0xFF0000 & color )>>16))/255.;
276     double green = ((double)(( 0x00FF00 & color )>>8))/255.;
277     double blue = ((double)( 0x0000FF & color ))/255.;
278     double norm = sqrt( red*red + green*green + blue*blue );
279     red /= norm;
280     green /= norm;
281     blue /= norm;
282     /* XXX: We might still need to norm the rgb_matrix */
283     double rgb_matrix[9] =
284         { red*red,    red*green,   red*blue,
285           red*green,  green*green, green*blue,
286           red*blue,   green*blue,  blue*blue };
287     double result1[9];
288     double result[9];
289     int i;
290     printf( "%f\n", red );
291     printf( "%f\n", green );
292     printf( "%f\n", blue );
293     mmult( result1, rgb_matrix, right_matrix );
294     mmult( result, left_matrix, result1 );
295     for( i = 0; i < 9; i++ )
296     {
297         matrix[i] = (int)result[i];
298         printf( "%d ", matrix[i] );
299         if( i%3 == 2 ) printf( "\n" );
300     }
301 }
302 static void get_custom_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
303                                     int yp, int up, int vp, int *m )
304 {
305     uint8_t *y1in = p_inpic->p[yp].p_pixels;
306     uint8_t *y2in;
307     uint8_t *uin  = p_inpic->p[up].p_pixels;
308     uint8_t *vin  = p_inpic->p[vp].p_pixels;
309
310     uint8_t *y1out = p_outpic->p[yp].p_pixels;
311     uint8_t *y2out;
312     uint8_t *uout  = p_outpic->p[up].p_pixels;
313     uint8_t *vout  = p_outpic->p[vp].p_pixels;
314
315     const int i_pitch = p_inpic->p[yp].i_pitch;
316     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
317     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
318
319     const int i_uv_pitch = p_inpic->p[up].i_pitch;
320     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
321
322     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
323     while( y1in < yend )
324     {
325         const uint8_t *y1end = y1in + i_visible_pitch;
326         y2in  = y1in + i_pitch;
327         y2out = y1out + i_pitch;
328         while( y1in < y1end )
329         {
330             *uout++ = crop( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
331                       / 65536 + U );
332             *vout++ = crop( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
333                       / 65536 + V );
334             *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
335                        / 65536 );
336             *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
337                        / 65536 );
338             *y2out++ = crop( (*y2in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
339                        / 65536 );
340             *y2out++ = crop( (*y2in++ * m[0] + (*uin++ - U) * m[1] + (*vin++ -V) * m[2])
341                        / 65536 );
342         }
343         y1in  += 2*i_pitch - i_visible_pitch;
344         y1out += 2*i_pitch - i_visible_pitch;
345         uin   += i_uv_pitch - i_uv_visible_pitch;
346         uout  += i_uv_pitch - i_uv_visible_pitch;
347         vin   += i_uv_pitch - i_uv_visible_pitch;
348         vout  += i_uv_pitch - i_uv_visible_pitch;
349     }
350 }
351 static void get_custom_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
352                                     int yp, int up, int vp, int *m )
353 {
354     uint8_t *y1in = p_inpic->p[yp].p_pixels;
355     uint8_t *uin  = p_inpic->p[up].p_pixels;
356     uint8_t *vin  = p_inpic->p[vp].p_pixels;
357
358     uint8_t *y1out = p_outpic->p[yp].p_pixels;
359     uint8_t *uout  = p_outpic->p[up].p_pixels;
360     uint8_t *vout  = p_outpic->p[vp].p_pixels;
361
362     const int i_pitch = p_inpic->p[yp].i_pitch;
363     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
364     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
365
366     const int i_uv_pitch = p_inpic->p[up].i_pitch;
367     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
368
369     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
370     while( y1in < yend )
371     {
372         const uint8_t *y1end = y1in + i_visible_pitch;
373         while( y1in < y1end )
374         {
375             *uout++ = crop( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
376                       / 65536 + U );
377             *vout++ = crop( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
378                       / 65536 + V );
379             *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
380                        / 65536 );
381             *y1out++ = crop( (*y1in++ * m[0] + (*uin++ -U) * m[1] + (*vin++ -V) * m[2])
382                        / 65536 );
383         }
384         y1in  += i_pitch - i_visible_pitch;
385         y1out += i_pitch - i_visible_pitch;
386         uin   += i_uv_pitch - i_uv_visible_pitch;
387         uout  += i_uv_pitch - i_uv_visible_pitch;
388         vin   += i_uv_pitch - i_uv_visible_pitch;
389         vout  += i_uv_pitch - i_uv_visible_pitch;
390     }
391 }
392
393 static void get_red_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
394                                  int yp, int up, int vp )
395 {
396     uint8_t *y1in = p_inpic->p[yp].p_pixels;
397     uint8_t *y2in;
398     uint8_t *vin  = p_inpic->p[vp].p_pixels;
399
400     uint8_t *y1out = p_outpic->p[yp].p_pixels;
401     uint8_t *y2out;
402     uint8_t *uout  = p_outpic->p[up].p_pixels;
403     uint8_t *vout  = p_outpic->p[vp].p_pixels;
404
405     const int i_pitch = p_inpic->p[yp].i_pitch;
406     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
407     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
408
409     const int i_uv_pitch = p_inpic->p[up].i_pitch;
410     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
411
412     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
413     while( y1in < yend )
414     {
415         const uint8_t *y1end = y1in + i_visible_pitch;
416         y2in  = y1in + i_pitch;
417         y2out = y1out + i_pitch;
418         while( y1in < y1end )
419         {
420 /*
421 19595   0   27473
422 -11058  0   -15504
423 32768   0   45941
424 */
425             *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
426                       / 65536 + U );
427             *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
428                       / 65536 + V );
429             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
430                        / 65536 );
431             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
432                        / 65536 );
433             *y2out++ = crop( (*y2in++ * 19594 + (*vin - V) * 27473)
434                        / 65536 );
435             *y2out++ = crop( (*y2in++ * 19594 + (*vin++ - V) * 27473)
436                        / 65536 );
437         }
438         y1in  += 2*i_pitch - i_visible_pitch;
439         y1out += 2*i_pitch - i_visible_pitch;
440         uout  += i_uv_pitch - i_uv_visible_pitch;
441         vin   += i_uv_pitch - i_uv_visible_pitch;
442         vout  += i_uv_pitch - i_uv_visible_pitch;
443     }
444 }
445
446 static void get_green_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
447                                  int yp, int up, int vp )
448 {
449     uint8_t *y1in = p_inpic->p[yp].p_pixels;
450     uint8_t *y2in;
451     uint8_t *uin  = p_inpic->p[up].p_pixels;
452     uint8_t *vin  = p_inpic->p[vp].p_pixels;
453
454     uint8_t *y1out = p_outpic->p[yp].p_pixels;
455     uint8_t *y2out;
456     uint8_t *uout  = p_outpic->p[up].p_pixels;
457     uint8_t *vout  = p_outpic->p[vp].p_pixels;
458
459     const int i_pitch = p_inpic->p[yp].i_pitch;
460     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
461     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
462
463     const int i_uv_pitch = p_inpic->p[up].i_pitch;
464     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
465
466     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
467     while( y1in < yend )
468     {
469         const uint8_t *y1end = y1in + i_visible_pitch;
470         y2in  = y1in + i_pitch;
471         y2out = y1out + i_pitch;
472         while( y1in < y1end )
473         {
474 /*
475 38470   -13239  -27473
476 -21710  7471    15504
477 -27439  9443    19595
478 */
479             *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
480                       / 65536 + U );
481             *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
482                       / 65536 + V );
483             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
484                        / 65536 );
485             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
486                        / 65536 );
487             *y2out++ = crop( (*y2in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
488                        / 65536 );
489             *y2out++ = crop( (*y2in++ * 38470 + (*uin++ - U) * -13239 + (*vin++ -V) * -27473)
490                        / 65536 );
491         }
492         y1in  += 2*i_pitch - i_visible_pitch;
493         y1out += 2*i_pitch - i_visible_pitch;
494         uin   += i_uv_pitch - i_uv_visible_pitch;
495         uout  += i_uv_pitch - i_uv_visible_pitch;
496         vin   += i_uv_pitch - i_uv_visible_pitch;
497         vout  += i_uv_pitch - i_uv_visible_pitch;
498     }
499 }
500
501 static void get_blue_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
502                                  int yp, int up, int vp )
503 {
504     uint8_t *y1in = p_inpic->p[yp].p_pixels;
505     uint8_t *y2in;
506     uint8_t *uin  = p_inpic->p[up].p_pixels;
507
508     uint8_t *y1out = p_outpic->p[yp].p_pixels;
509     uint8_t *y2out;
510     uint8_t *uout  = p_outpic->p[up].p_pixels;
511     uint8_t *vout  = p_outpic->p[vp].p_pixels;
512
513     const int i_pitch = p_inpic->p[yp].i_pitch;
514     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
515     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
516
517     const int i_uv_pitch = p_inpic->p[up].i_pitch;
518     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
519
520     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
521     while( y1in < yend )
522     {
523         const uint8_t *y1end = y1in + i_visible_pitch;
524         y2in  = y1in + i_pitch;
525         y2out = y1out + i_pitch;
526         while( y1in < y1end )
527         {
528 /*
529 7471    13239   0
530 32768   58065   0
531 -5329   -9443   0
532 */
533             *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
534                       / 65536 + U );
535             *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
536                       / 65536 + V );
537             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
538                        / 65536 );
539             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
540                        / 65536 );
541             *y2out++ = crop( (*y2in++ * 7471 + (*uin - U) * 13239 )
542                        / 65536 );
543             *y2out++ = crop( (*y2in++ * 7471 + (*uin++ - U) * 13239 )
544                        / 65536 );
545         }
546         y1in  += 2*i_pitch - i_visible_pitch;
547         y1out += 2*i_pitch - i_visible_pitch;
548         uin   += i_uv_pitch - i_uv_visible_pitch;
549         uout  += i_uv_pitch - i_uv_visible_pitch;
550         vout  += i_uv_pitch - i_uv_visible_pitch;
551     }
552 }
553
554 static void get_red_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
555                                  int yp, int up, int vp )
556 {
557     uint8_t *y1in = p_inpic->p[yp].p_pixels;
558     uint8_t *vin  = p_inpic->p[vp].p_pixels;
559
560     uint8_t *y1out = p_outpic->p[yp].p_pixels;
561     uint8_t *uout  = p_outpic->p[up].p_pixels;
562     uint8_t *vout  = p_outpic->p[vp].p_pixels;
563
564     const int i_pitch = p_inpic->p[yp].i_pitch;
565     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
566     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
567
568     const int i_uv_pitch = p_inpic->p[up].i_pitch;
569     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
570
571     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
572     while( y1in < yend )
573     {
574         const uint8_t *y1end = y1in + i_visible_pitch;
575         while( y1in < y1end )
576         {
577 /*
578 19595   0   27473
579 -11058  0   -15504
580 32768   0   45941
581 */
582             *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
583                       / 65536 + U );
584             *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
585                       / 65536 + V );
586             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
587                        / 65536 );
588             *y1out++ = crop( (*y1in++ * 19595 + (*vin++ - V) * 27473)
589                        / 65536 );
590         }
591         y1in  += i_pitch - i_visible_pitch;
592         y1out += i_pitch - i_visible_pitch;
593         uout  += i_uv_pitch - i_uv_visible_pitch;
594         vin   += i_uv_pitch - i_uv_visible_pitch;
595         vout  += i_uv_pitch - i_uv_visible_pitch;
596     }
597 }
598
599 static void get_green_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
600                                    int yp, int up, int vp )
601 {
602     uint8_t *y1in = p_inpic->p[yp].p_pixels;
603     uint8_t *uin  = p_inpic->p[up].p_pixels;
604     uint8_t *vin  = p_inpic->p[vp].p_pixels;
605
606     uint8_t *y1out = p_outpic->p[yp].p_pixels;
607     uint8_t *uout  = p_outpic->p[up].p_pixels;
608     uint8_t *vout  = p_outpic->p[vp].p_pixels;
609
610     const int i_pitch = p_inpic->p[yp].i_pitch;
611     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
612     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
613
614     const int i_uv_pitch = p_inpic->p[up].i_pitch;
615     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
616
617     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
618     while( y1in < yend )
619     {
620         const uint8_t *y1end = y1in + i_visible_pitch;
621         while( y1in < y1end )
622         {
623 /*
624 38470   -13239  -27473
625 -21710  7471    15504
626 -27439  9443    19595
627 */
628             *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
629                       / 65536 + U );
630             *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
631                       / 65536 + V );
632             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
633                        / 65536 );
634             *y1out++ = crop( (*y1in++ * 38470 + (*uin++-U) * -13239 + (*vin++-V) * -27473)
635                        / 65536 );
636         }
637         y1in  += i_pitch - i_visible_pitch;
638         y1out += i_pitch - i_visible_pitch;
639         uin   += i_uv_pitch - i_uv_visible_pitch;
640         uout  += i_uv_pitch - i_uv_visible_pitch;
641         vin   += i_uv_pitch - i_uv_visible_pitch;
642         vout  += i_uv_pitch - i_uv_visible_pitch;
643     }
644 }
645
646 static void get_blue_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
647                                  int yp, int up, int vp )
648 {
649     uint8_t *y1in = p_inpic->p[yp].p_pixels;
650     uint8_t *uin  = p_inpic->p[up].p_pixels;
651
652     uint8_t *y1out = p_outpic->p[yp].p_pixels;
653     uint8_t *uout  = p_outpic->p[up].p_pixels;
654     uint8_t *vout  = p_outpic->p[vp].p_pixels;
655
656     const int i_pitch = p_inpic->p[yp].i_pitch;
657     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
658     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
659
660     const int i_uv_pitch = p_inpic->p[up].i_pitch;
661     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
662
663     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
664     while( y1in < yend )
665     {
666         const uint8_t *y1end = y1in + i_visible_pitch;
667         while( y1in < y1end )
668         {
669 /*
670 7471    13239   0
671 32768   58065   0
672 -5329   -9443   0
673 */
674             *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
675                       / 65536 + U );
676             *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
677                       / 65536 + V );
678             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
679                        / 65536 );
680             *y1out++ = crop( (*y1in++ * 7471 + (*uin++ - U) * 13239 )
681                        / 65536 );
682         }
683         y1in  += i_pitch - i_visible_pitch;
684         y1out += i_pitch - i_visible_pitch;
685         uin   += i_uv_pitch - i_uv_visible_pitch;
686         uout  += i_uv_pitch - i_uv_visible_pitch;
687         vout  += i_uv_pitch - i_uv_visible_pitch;
688     }
689 }
690
691 static int ExtractCallback( vlc_object_t *p_this, char const *psz_var,
692                             vlc_value_t oldval, vlc_value_t newval,
693                             void *p_data )
694 {
695     filter_sys_t *p_sys = (filter_sys_t *)p_data;
696
697     if( !strcmp( psz_var, FILTER_PREFIX "component" ) )
698     {
699         p_sys->i_color = newval.i_int;
700         switch( p_sys->i_color )
701         {
702             case RED:
703             case GREEN:
704             case BLUE:
705                 break;
706             default:
707                 make_projection_matrix( p_sys->i_color,
708                                         p_sys->projection_matrix );
709                 break;
710         }
711     }
712     else
713     {
714         msg_Warn( p_this, "Unknown callback command." );
715     }
716     return VLC_SUCCESS;
717 }