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