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