a0d39ed96c995caa0dfcd743d25348b3cc385e52
[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
28 #include <vlc/vlc.h>
29 #include <vlc_vout.h>
30
31 #include "vlc_filter.h"
32
33 #include "math.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( filter_t *, 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_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 static inline uint8_t crop( int a )
240 {
241     if( a < 0 ) return 0;
242     if( a > 255 ) return 255;
243     else return (uint8_t)a;
244 }
245
246 #define U 128
247 #define V 128
248
249 static void mmult( double *res, double *a, double *b )
250 {
251     int i, j, k;
252     for( i = 0; i < 3; i++ )
253     {
254         for( j = 0; j < 3; j++ )
255         {
256             res[ i*3 + j ] = 0.;
257             for( k = 0; k < 3; k++ )
258             {
259                 res[ i*3 + j ] += a[ i*3 + k ] * b[ k*3 + j ];
260             }
261         }
262     }
263 }
264 static void make_projection_matrix( filter_t *p_filter, int color, int *matrix )
265 {
266     double left_matrix[9] =
267         {  76.24500,  149.68500,  29.07000,
268           -43.02765,  -84.47235, 127.50000,
269           127.50000, -106.76534, -20.73466 };
270     double right_matrix[9] =
271         { 257.00392,   0.00000,  360.31950,
272           257.00392, -88.44438, -183.53583,
273           257.00392, 455.41095,    0.00000 };
274     double red = ((double)(( 0xFF0000 & color )>>16))/255.;
275     double green = ((double)(( 0x00FF00 & color )>>8))/255.;
276     double blue = ((double)( 0x0000FF & color ))/255.;
277     double norm = sqrt( red*red + green*green + blue*blue );
278     red /= norm;
279     green /= norm;
280     blue /= norm;
281     /* XXX: We might still need to norm the rgb_matrix */
282     double rgb_matrix[9] =
283         { red*red,    red*green,   red*blue,
284           red*green,  green*green, green*blue,
285           red*blue,   green*blue,  blue*blue };
286     double result1[9];
287     double result[9];
288     int i;
289     msg_Dbg( p_filter, "red: %f", red );
290     msg_Dbg( p_filter, "green: %f", green );
291     msg_Dbg( p_filter, "blue: %f", blue );
292     mmult( result1, rgb_matrix, right_matrix );
293     mmult( result, left_matrix, result1 );
294     for( i = 0; i < 9; i++ )
295     {
296         matrix[i] = (int)result[i];
297     }
298     msg_Dbg( p_filter, "Projection matrix:" );
299     msg_Dbg( p_filter, "%6d %6d %6d", matrix[0], matrix[1], matrix[2] );
300     msg_Dbg( p_filter, "%6d %6d %6d", matrix[3], matrix[4], matrix[5] );
301     msg_Dbg( p_filter, "%6d %6d %6d", matrix[6], matrix[7], matrix[8] );
302 }
303
304 static void get_custom_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
305                                     int yp, int up, int vp, int *m )
306 {
307     uint8_t *y1in = p_inpic->p[yp].p_pixels;
308     uint8_t *y2in;
309     uint8_t *uin  = p_inpic->p[up].p_pixels;
310     uint8_t *vin  = p_inpic->p[vp].p_pixels;
311
312     uint8_t *y1out = p_outpic->p[yp].p_pixels;
313     uint8_t *y2out;
314     uint8_t *uout  = p_outpic->p[up].p_pixels;
315     uint8_t *vout  = p_outpic->p[vp].p_pixels;
316
317     const int i_pitch = p_inpic->p[yp].i_pitch;
318     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
319     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
320
321     const int i_uv_pitch = p_inpic->p[up].i_pitch;
322     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
323
324     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
325     while( y1in < yend )
326     {
327         const uint8_t *y1end = y1in + i_visible_pitch;
328         y2in  = y1in + i_pitch;
329         y2out = y1out + i_pitch;
330         while( y1in < y1end )
331         {
332             *uout++ = crop( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
333                       / 65536 + U );
334             *vout++ = crop( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
335                       / 65536 + V );
336             *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
337                        / 65536 );
338             *y1out++ = crop( (*y1in++ * 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             *y2out++ = crop( (*y2in++ * m[0] + (*uin++ - U) * m[1] + (*vin++ -V) * m[2])
343                        / 65536 );
344         }
345         y1in  += 2*i_pitch - i_visible_pitch;
346         y1out += 2*i_pitch - i_visible_pitch;
347         uin   += i_uv_pitch - i_uv_visible_pitch;
348         uout  += i_uv_pitch - i_uv_visible_pitch;
349         vin   += i_uv_pitch - i_uv_visible_pitch;
350         vout  += i_uv_pitch - i_uv_visible_pitch;
351     }
352 }
353 static void get_custom_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
354                                     int yp, int up, int vp, int *m )
355 {
356     uint8_t *y1in = p_inpic->p[yp].p_pixels;
357     uint8_t *uin  = p_inpic->p[up].p_pixels;
358     uint8_t *vin  = p_inpic->p[vp].p_pixels;
359
360     uint8_t *y1out = p_outpic->p[yp].p_pixels;
361     uint8_t *uout  = p_outpic->p[up].p_pixels;
362     uint8_t *vout  = p_outpic->p[vp].p_pixels;
363
364     const int i_pitch = p_inpic->p[yp].i_pitch;
365     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
366     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
367
368     const int i_uv_pitch = p_inpic->p[up].i_pitch;
369     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
370
371     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
372     while( y1in < yend )
373     {
374         const uint8_t *y1end = y1in + i_visible_pitch;
375         while( y1in < y1end )
376         {
377             *uout++ = crop( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
378                       / 65536 + U );
379             *vout++ = crop( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
380                       / 65536 + V );
381             *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
382                        / 65536 );
383             *y1out++ = crop( (*y1in++ * m[0] + (*uin++ -U) * m[1] + (*vin++ -V) * m[2])
384                        / 65536 );
385         }
386         y1in  += i_pitch - i_visible_pitch;
387         y1out += i_pitch - i_visible_pitch;
388         uin   += i_uv_pitch - i_uv_visible_pitch;
389         uout  += i_uv_pitch - i_uv_visible_pitch;
390         vin   += i_uv_pitch - i_uv_visible_pitch;
391         vout  += i_uv_pitch - i_uv_visible_pitch;
392     }
393 }
394
395 static void get_red_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
396                                  int yp, int up, int vp )
397 {
398     uint8_t *y1in = p_inpic->p[yp].p_pixels;
399     uint8_t *y2in;
400     uint8_t *vin  = p_inpic->p[vp].p_pixels;
401
402     uint8_t *y1out = p_outpic->p[yp].p_pixels;
403     uint8_t *y2out;
404     uint8_t *uout  = p_outpic->p[up].p_pixels;
405     uint8_t *vout  = p_outpic->p[vp].p_pixels;
406
407     const int i_pitch = p_inpic->p[yp].i_pitch;
408     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
409     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
410
411     const int i_uv_pitch = p_inpic->p[up].i_pitch;
412     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
413
414     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
415     while( y1in < yend )
416     {
417         const uint8_t *y1end = y1in + i_visible_pitch;
418         y2in  = y1in + i_pitch;
419         y2out = y1out + i_pitch;
420         while( y1in < y1end )
421         {
422 /*
423 19595   0   27473
424 -11058  0   -15504
425 32768   0   45941
426 */
427             *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
428                       / 65536 + U );
429             *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
430                       / 65536 + V );
431             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
432                        / 65536 );
433             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
434                        / 65536 );
435             *y2out++ = crop( (*y2in++ * 19594 + (*vin - V) * 27473)
436                        / 65536 );
437             *y2out++ = crop( (*y2in++ * 19594 + (*vin++ - V) * 27473)
438                        / 65536 );
439         }
440         y1in  += 2*i_pitch - i_visible_pitch;
441         y1out += 2*i_pitch - i_visible_pitch;
442         uout  += i_uv_pitch - i_uv_visible_pitch;
443         vin   += i_uv_pitch - i_uv_visible_pitch;
444         vout  += i_uv_pitch - i_uv_visible_pitch;
445     }
446 }
447
448 static void get_green_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
449                                  int yp, int up, int vp )
450 {
451     uint8_t *y1in = p_inpic->p[yp].p_pixels;
452     uint8_t *y2in;
453     uint8_t *uin  = p_inpic->p[up].p_pixels;
454     uint8_t *vin  = p_inpic->p[vp].p_pixels;
455
456     uint8_t *y1out = p_outpic->p[yp].p_pixels;
457     uint8_t *y2out;
458     uint8_t *uout  = p_outpic->p[up].p_pixels;
459     uint8_t *vout  = p_outpic->p[vp].p_pixels;
460
461     const int i_pitch = p_inpic->p[yp].i_pitch;
462     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
463     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
464
465     const int i_uv_pitch = p_inpic->p[up].i_pitch;
466     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
467
468     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
469     while( y1in < yend )
470     {
471         const uint8_t *y1end = y1in + i_visible_pitch;
472         y2in  = y1in + i_pitch;
473         y2out = y1out + i_pitch;
474         while( y1in < y1end )
475         {
476 /*
477 38470   -13239  -27473
478 -21710  7471    15504
479 -27439  9443    19595
480 */
481             *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
482                       / 65536 + U );
483             *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
484                       / 65536 + V );
485             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
486                        / 65536 );
487             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
488                        / 65536 );
489             *y2out++ = crop( (*y2in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
490                        / 65536 );
491             *y2out++ = crop( (*y2in++ * 38470 + (*uin++ - U) * -13239 + (*vin++ -V) * -27473)
492                        / 65536 );
493         }
494         y1in  += 2*i_pitch - i_visible_pitch;
495         y1out += 2*i_pitch - i_visible_pitch;
496         uin   += i_uv_pitch - i_uv_visible_pitch;
497         uout  += i_uv_pitch - i_uv_visible_pitch;
498         vin   += i_uv_pitch - i_uv_visible_pitch;
499         vout  += i_uv_pitch - i_uv_visible_pitch;
500     }
501 }
502
503 static void get_blue_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
504                                  int yp, int up, int vp )
505 {
506     uint8_t *y1in = p_inpic->p[yp].p_pixels;
507     uint8_t *y2in;
508     uint8_t *uin  = p_inpic->p[up].p_pixels;
509
510     uint8_t *y1out = p_outpic->p[yp].p_pixels;
511     uint8_t *y2out;
512     uint8_t *uout  = p_outpic->p[up].p_pixels;
513     uint8_t *vout  = p_outpic->p[vp].p_pixels;
514
515     const int i_pitch = p_inpic->p[yp].i_pitch;
516     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
517     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
518
519     const int i_uv_pitch = p_inpic->p[up].i_pitch;
520     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
521
522     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
523     while( y1in < yend )
524     {
525         const uint8_t *y1end = y1in + i_visible_pitch;
526         y2in  = y1in + i_pitch;
527         y2out = y1out + i_pitch;
528         while( y1in < y1end )
529         {
530 /*
531 7471    13239   0
532 32768   58065   0
533 -5329   -9443   0
534 */
535             *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
536                       / 65536 + U );
537             *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
538                       / 65536 + V );
539             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
540                        / 65536 );
541             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
542                        / 65536 );
543             *y2out++ = crop( (*y2in++ * 7471 + (*uin - U) * 13239 )
544                        / 65536 );
545             *y2out++ = crop( (*y2in++ * 7471 + (*uin++ - U) * 13239 )
546                        / 65536 );
547         }
548         y1in  += 2*i_pitch - i_visible_pitch;
549         y1out += 2*i_pitch - i_visible_pitch;
550         uin   += i_uv_pitch - i_uv_visible_pitch;
551         uout  += i_uv_pitch - i_uv_visible_pitch;
552         vout  += i_uv_pitch - i_uv_visible_pitch;
553     }
554 }
555
556 static void get_red_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
557                                  int yp, int up, int vp )
558 {
559     uint8_t *y1in = p_inpic->p[yp].p_pixels;
560     uint8_t *vin  = p_inpic->p[vp].p_pixels;
561
562     uint8_t *y1out = p_outpic->p[yp].p_pixels;
563     uint8_t *uout  = p_outpic->p[up].p_pixels;
564     uint8_t *vout  = p_outpic->p[vp].p_pixels;
565
566     const int i_pitch = p_inpic->p[yp].i_pitch;
567     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
568     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
569
570     const int i_uv_pitch = p_inpic->p[up].i_pitch;
571     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
572
573     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
574     while( y1in < yend )
575     {
576         const uint8_t *y1end = y1in + i_visible_pitch;
577         while( y1in < y1end )
578         {
579 /*
580 19595   0   27473
581 -11058  0   -15504
582 32768   0   45941
583 */
584             *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
585                       / 65536 + U );
586             *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
587                       / 65536 + V );
588             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
589                        / 65536 );
590             *y1out++ = crop( (*y1in++ * 19595 + (*vin++ - V) * 27473)
591                        / 65536 );
592         }
593         y1in  += i_pitch - i_visible_pitch;
594         y1out += i_pitch - i_visible_pitch;
595         uout  += i_uv_pitch - i_uv_visible_pitch;
596         vin   += i_uv_pitch - i_uv_visible_pitch;
597         vout  += i_uv_pitch - i_uv_visible_pitch;
598     }
599 }
600
601 static void get_green_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
602                                    int yp, int up, int vp )
603 {
604     uint8_t *y1in = p_inpic->p[yp].p_pixels;
605     uint8_t *uin  = p_inpic->p[up].p_pixels;
606     uint8_t *vin  = p_inpic->p[vp].p_pixels;
607
608     uint8_t *y1out = p_outpic->p[yp].p_pixels;
609     uint8_t *uout  = p_outpic->p[up].p_pixels;
610     uint8_t *vout  = p_outpic->p[vp].p_pixels;
611
612     const int i_pitch = p_inpic->p[yp].i_pitch;
613     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
614     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
615
616     const int i_uv_pitch = p_inpic->p[up].i_pitch;
617     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
618
619     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
620     while( y1in < yend )
621     {
622         const uint8_t *y1end = y1in + i_visible_pitch;
623         while( y1in < y1end )
624         {
625 /*
626 38470   -13239  -27473
627 -21710  7471    15504
628 -27439  9443    19595
629 */
630             *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
631                       / 65536 + U );
632             *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
633                       / 65536 + V );
634             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
635                        / 65536 );
636             *y1out++ = crop( (*y1in++ * 38470 + (*uin++-U) * -13239 + (*vin++-V) * -27473)
637                        / 65536 );
638         }
639         y1in  += i_pitch - i_visible_pitch;
640         y1out += i_pitch - i_visible_pitch;
641         uin   += i_uv_pitch - i_uv_visible_pitch;
642         uout  += i_uv_pitch - i_uv_visible_pitch;
643         vin   += i_uv_pitch - i_uv_visible_pitch;
644         vout  += i_uv_pitch - i_uv_visible_pitch;
645     }
646 }
647
648 static void get_blue_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
649                                  int yp, int up, int vp )
650 {
651     uint8_t *y1in = p_inpic->p[yp].p_pixels;
652     uint8_t *uin  = p_inpic->p[up].p_pixels;
653
654     uint8_t *y1out = p_outpic->p[yp].p_pixels;
655     uint8_t *uout  = p_outpic->p[up].p_pixels;
656     uint8_t *vout  = p_outpic->p[vp].p_pixels;
657
658     const int i_pitch = p_inpic->p[yp].i_pitch;
659     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
660     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
661
662     const int i_uv_pitch = p_inpic->p[up].i_pitch;
663     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
664
665     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
666     while( y1in < yend )
667     {
668         const uint8_t *y1end = y1in + i_visible_pitch;
669         while( y1in < y1end )
670         {
671 /*
672 7471    13239   0
673 32768   58065   0
674 -5329   -9443   0
675 */
676             *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
677                       / 65536 + U );
678             *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
679                       / 65536 + V );
680             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
681                        / 65536 );
682             *y1out++ = crop( (*y1in++ * 7471 + (*uin++ - U) * 13239 )
683                        / 65536 );
684         }
685         y1in  += i_pitch - i_visible_pitch;
686         y1out += i_pitch - i_visible_pitch;
687         uin   += i_uv_pitch - i_uv_visible_pitch;
688         uout  += i_uv_pitch - i_uv_visible_pitch;
689         vout  += i_uv_pitch - i_uv_visible_pitch;
690     }
691 }
692
693 static int ExtractCallback( vlc_object_t *p_this, char const *psz_var,
694                             vlc_value_t oldval, vlc_value_t newval,
695                             void *p_data )
696 {
697     filter_sys_t *p_sys = (filter_sys_t *)p_data;
698
699     if( !strcmp( psz_var, FILTER_PREFIX "component" ) )
700     {
701         p_sys->i_color = newval.i_int;
702         switch( p_sys->i_color )
703         {
704             case RED:
705             case GREEN:
706             case BLUE:
707                 break;
708             default:
709                 make_projection_matrix( (filter_t *)p_this, p_sys->i_color,
710                                         p_sys->projection_matrix );
711                 break;
712         }
713     }
714     else
715     {
716         msg_Warn( p_this, "Unknown callback command." );
717     }
718     return VLC_SUCCESS;
719 }