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