]> git.sesse.net Git - vlc/blob - modules/video_filter/extract.c
Merge branch 1.0-bugfix
[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 (%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 (%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_pitch = p_inpic->p[yp].i_pitch;
334     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
335     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
336
337     const int i_uv_pitch = p_inpic->p[up].i_pitch;
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_pitch;
341     while( y1in < yend )
342     {
343         const uint8_t *y1end = y1in + i_visible_pitch;
344         y2in  = y1in + i_pitch;
345         y2out = y1out + i_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_pitch - i_visible_pitch;
362         y1out += 2*i_pitch - i_visible_pitch;
363         uin   += i_uv_pitch - i_uv_visible_pitch;
364         uout  += i_uv_pitch - i_uv_visible_pitch;
365         vin   += i_uv_pitch - i_uv_visible_pitch;
366         vout  += i_uv_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_pitch = p_inpic->p[yp].i_pitch;
381     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
382     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
383
384     const int i_uv_pitch = p_inpic->p[up].i_pitch;
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_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_pitch - i_visible_pitch;
403         y1out += i_pitch - i_visible_pitch;
404         uin   += i_uv_pitch - i_uv_visible_pitch;
405         uout  += i_uv_pitch - i_uv_visible_pitch;
406         vin   += i_uv_pitch - i_uv_visible_pitch;
407         vout  += i_uv_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_pitch = p_inpic->p->i_pitch;
429     const int i_visible_pitch = p_inpic->p->i_visible_pitch;
430     const int i_visible_lines = p_inpic->p->i_visible_lines;
431
432     const uint8_t *yend = yin + i_visible_lines * i_pitch;
433     while( yin < yend )
434     {
435         const uint8_t *ylend = yin + i_visible_pitch;
436         while( yin < ylend )
437         {
438             *uout = crop( (*yin * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
439                       / 65536 + U );
440             uout += 4;
441             *vout = crop( (*yin * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
442                      / 65536 + V );
443             vout += 4;
444             *yout = crop( (*yin * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
445                        / 65536 );
446             yin  += 2;
447             yout += 2;
448             *yout = crop( (*yin * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
449                        / 65536 );
450             yin  += 2;
451             yout += 2;
452             uin  += 4;
453             vin  += 4;
454         }
455         yin  += i_pitch - i_visible_pitch;
456         yout += i_pitch - i_visible_pitch;
457         uin  += i_pitch - i_visible_pitch;
458         uout += i_pitch - i_visible_pitch;
459         vin  += i_pitch - i_visible_pitch;
460         vout += i_pitch - i_visible_pitch;
461     }
462 }
463
464 static void get_red_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
465                                  int yp, int up, int vp )
466 {
467     uint8_t *y1in = p_inpic->p[yp].p_pixels;
468     uint8_t *y2in;
469     uint8_t *vin  = p_inpic->p[vp].p_pixels;
470
471     uint8_t *y1out = p_outpic->p[yp].p_pixels;
472     uint8_t *y2out;
473     uint8_t *uout  = p_outpic->p[up].p_pixels;
474     uint8_t *vout  = p_outpic->p[vp].p_pixels;
475
476     const int i_pitch = p_inpic->p[yp].i_pitch;
477     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
478     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
479
480     const int i_uv_pitch = p_inpic->p[up].i_pitch;
481     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
482
483     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
484     while( y1in < yend )
485     {
486         const uint8_t *y1end = y1in + i_visible_pitch;
487         y2in  = y1in + i_pitch;
488         y2out = y1out + i_pitch;
489         while( y1in < y1end )
490         {
491 /*
492 19595   0   27473
493 -11058  0   -15504
494 32768   0   45941
495 */
496             *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
497                       / 65536 + U );
498             *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
499                       / 65536 + V );
500             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
501                        / 65536 );
502             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
503                        / 65536 );
504             *y2out++ = crop( (*y2in++ * 19594 + (*vin - V) * 27473)
505                        / 65536 );
506             *y2out++ = crop( (*y2in++ * 19594 + (*vin++ - V) * 27473)
507                        / 65536 );
508         }
509         y1in  += 2*i_pitch - i_visible_pitch;
510         y1out += 2*i_pitch - i_visible_pitch;
511         uout  += i_uv_pitch - i_uv_visible_pitch;
512         vin   += i_uv_pitch - i_uv_visible_pitch;
513         vout  += i_uv_pitch - i_uv_visible_pitch;
514     }
515 }
516
517 static void get_green_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
518                                  int yp, int up, int vp )
519 {
520     uint8_t *y1in = p_inpic->p[yp].p_pixels;
521     uint8_t *y2in;
522     uint8_t *uin  = p_inpic->p[up].p_pixels;
523     uint8_t *vin  = p_inpic->p[vp].p_pixels;
524
525     uint8_t *y1out = p_outpic->p[yp].p_pixels;
526     uint8_t *y2out;
527     uint8_t *uout  = p_outpic->p[up].p_pixels;
528     uint8_t *vout  = p_outpic->p[vp].p_pixels;
529
530     const int i_pitch = p_inpic->p[yp].i_pitch;
531     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
532     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
533
534     const int i_uv_pitch = p_inpic->p[up].i_pitch;
535     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
536
537     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
538     while( y1in < yend )
539     {
540         const uint8_t *y1end = y1in + i_visible_pitch;
541         y2in  = y1in + i_pitch;
542         y2out = y1out + i_pitch;
543         while( y1in < y1end )
544         {
545 /*
546 38470   -13239  -27473
547 -21710  7471    15504
548 -27439  9443    19595
549 */
550             *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
551                       / 65536 + U );
552             *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
553                       / 65536 + V );
554             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
555                        / 65536 );
556             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
557                        / 65536 );
558             *y2out++ = crop( (*y2in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
559                        / 65536 );
560             *y2out++ = crop( (*y2in++ * 38470 + (*uin++ - U) * -13239 + (*vin++ -V) * -27473)
561                        / 65536 );
562         }
563         y1in  += 2*i_pitch - i_visible_pitch;
564         y1out += 2*i_pitch - i_visible_pitch;
565         uin   += i_uv_pitch - i_uv_visible_pitch;
566         uout  += i_uv_pitch - i_uv_visible_pitch;
567         vin   += i_uv_pitch - i_uv_visible_pitch;
568         vout  += i_uv_pitch - i_uv_visible_pitch;
569     }
570 }
571
572 static void get_blue_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
573                                  int yp, int up, int vp )
574 {
575     uint8_t *y1in = p_inpic->p[yp].p_pixels;
576     uint8_t *y2in;
577     uint8_t *uin  = p_inpic->p[up].p_pixels;
578
579     uint8_t *y1out = p_outpic->p[yp].p_pixels;
580     uint8_t *y2out;
581     uint8_t *uout  = p_outpic->p[up].p_pixels;
582     uint8_t *vout  = p_outpic->p[vp].p_pixels;
583
584     const int i_pitch = p_inpic->p[yp].i_pitch;
585     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
586     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
587
588     const int i_uv_pitch = p_inpic->p[up].i_pitch;
589     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
590
591     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
592     while( y1in < yend )
593     {
594         const uint8_t *y1end = y1in + i_visible_pitch;
595         y2in  = y1in + i_pitch;
596         y2out = y1out + i_pitch;
597         while( y1in < y1end )
598         {
599 /*
600 7471    13239   0
601 32768   58065   0
602 -5329   -9443   0
603 */
604             *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
605                       / 65536 + U );
606             *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
607                       / 65536 + V );
608             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
609                        / 65536 );
610             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
611                        / 65536 );
612             *y2out++ = crop( (*y2in++ * 7471 + (*uin - U) * 13239 )
613                        / 65536 );
614             *y2out++ = crop( (*y2in++ * 7471 + (*uin++ - U) * 13239 )
615                        / 65536 );
616         }
617         y1in  += 2*i_pitch - i_visible_pitch;
618         y1out += 2*i_pitch - i_visible_pitch;
619         uin   += i_uv_pitch - i_uv_visible_pitch;
620         uout  += i_uv_pitch - i_uv_visible_pitch;
621         vout  += i_uv_pitch - i_uv_visible_pitch;
622     }
623 }
624
625 static void get_red_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
626                                  int yp, int up, int vp )
627 {
628     uint8_t *y1in = p_inpic->p[yp].p_pixels;
629     uint8_t *vin  = p_inpic->p[vp].p_pixels;
630
631     uint8_t *y1out = p_outpic->p[yp].p_pixels;
632     uint8_t *uout  = p_outpic->p[up].p_pixels;
633     uint8_t *vout  = p_outpic->p[vp].p_pixels;
634
635     const int i_pitch = p_inpic->p[yp].i_pitch;
636     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
637     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
638
639     const int i_uv_pitch = p_inpic->p[up].i_pitch;
640     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
641
642     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
643     while( y1in < yend )
644     {
645         const uint8_t *y1end = y1in + i_visible_pitch;
646         while( y1in < y1end )
647         {
648 /*
649 19595   0   27473
650 -11058  0   -15504
651 32768   0   45941
652 */
653             *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
654                       / 65536 + U );
655             *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
656                       / 65536 + V );
657             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
658                        / 65536 );
659             *y1out++ = crop( (*y1in++ * 19595 + (*vin++ - V) * 27473)
660                        / 65536 );
661         }
662         y1in  += i_pitch - i_visible_pitch;
663         y1out += i_pitch - i_visible_pitch;
664         uout  += i_uv_pitch - i_uv_visible_pitch;
665         vin   += i_uv_pitch - i_uv_visible_pitch;
666         vout  += i_uv_pitch - i_uv_visible_pitch;
667     }
668 }
669
670 static void get_green_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
671                                    int yp, int up, int vp )
672 {
673     uint8_t *y1in = p_inpic->p[yp].p_pixels;
674     uint8_t *uin  = p_inpic->p[up].p_pixels;
675     uint8_t *vin  = p_inpic->p[vp].p_pixels;
676
677     uint8_t *y1out = p_outpic->p[yp].p_pixels;
678     uint8_t *uout  = p_outpic->p[up].p_pixels;
679     uint8_t *vout  = p_outpic->p[vp].p_pixels;
680
681     const int i_pitch = p_inpic->p[yp].i_pitch;
682     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
683     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
684
685     const int i_uv_pitch = p_inpic->p[up].i_pitch;
686     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
687
688     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
689     while( y1in < yend )
690     {
691         const uint8_t *y1end = y1in + i_visible_pitch;
692         while( y1in < y1end )
693         {
694 /*
695 38470   -13239  -27473
696 -21710  7471    15504
697 -27439  9443    19595
698 */
699             *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
700                       / 65536 + U );
701             *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
702                       / 65536 + V );
703             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
704                        / 65536 );
705             *y1out++ = crop( (*y1in++ * 38470 + (*uin++-U) * -13239 + (*vin++-V) * -27473)
706                        / 65536 );
707         }
708         y1in  += i_pitch - i_visible_pitch;
709         y1out += i_pitch - i_visible_pitch;
710         uin   += i_uv_pitch - i_uv_visible_pitch;
711         uout  += i_uv_pitch - i_uv_visible_pitch;
712         vin   += i_uv_pitch - i_uv_visible_pitch;
713         vout  += i_uv_pitch - i_uv_visible_pitch;
714     }
715 }
716
717 static void get_blue_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
718                                  int yp, int up, int vp )
719 {
720     uint8_t *y1in = p_inpic->p[yp].p_pixels;
721     uint8_t *uin  = p_inpic->p[up].p_pixels;
722
723     uint8_t *y1out = p_outpic->p[yp].p_pixels;
724     uint8_t *uout  = p_outpic->p[up].p_pixels;
725     uint8_t *vout  = p_outpic->p[vp].p_pixels;
726
727     const int i_pitch = p_inpic->p[yp].i_pitch;
728     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
729     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
730
731     const int i_uv_pitch = p_inpic->p[up].i_pitch;
732     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
733
734     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
735     while( y1in < yend )
736     {
737         const uint8_t *y1end = y1in + i_visible_pitch;
738         while( y1in < y1end )
739         {
740 /*
741 7471    13239   0
742 32768   58065   0
743 -5329   -9443   0
744 */
745             *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
746                       / 65536 + U );
747             *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
748                       / 65536 + V );
749             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
750                        / 65536 );
751             *y1out++ = crop( (*y1in++ * 7471 + (*uin++ - U) * 13239 )
752                        / 65536 );
753         }
754         y1in  += i_pitch - i_visible_pitch;
755         y1out += i_pitch - i_visible_pitch;
756         uin   += i_uv_pitch - i_uv_visible_pitch;
757         uout  += i_uv_pitch - i_uv_visible_pitch;
758         vout  += i_uv_pitch - i_uv_visible_pitch;
759     }
760 }
761
762 static int ExtractCallback( vlc_object_t *p_this, char const *psz_var,
763                             vlc_value_t oldval, vlc_value_t newval,
764                             void *p_data )
765 {
766     VLC_UNUSED(oldval);
767     filter_sys_t *p_sys = (filter_sys_t *)p_data;
768
769     vlc_mutex_lock( &p_sys->lock );
770     if( !strcmp( psz_var, FILTER_PREFIX "component" ) )
771     {
772         p_sys->i_color = newval.i_int;
773         /* Matrix won't be used for RED, GREEN or BLUE in planar formats */
774         make_projection_matrix( (filter_t *)p_this, p_sys->i_color,
775                                 p_sys->projection_matrix );
776     }
777     else
778     {
779         msg_Warn( p_this, "Unknown callback command." );
780     }
781     vlc_mutex_unlock( &p_sys->lock );
782     return VLC_SUCCESS;
783 }