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