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