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