]> git.sesse.net Git - vlc/blob - modules/video_filter/extract.c
Fixed extract video filter when requesting black color.
[vlc] / modules / video_filter / extract.c
1 /*****************************************************************************
2  * extract.c : Extract RGB components
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea .t videolan d@t org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34
35 #include <vlc_filter.h>
36 #include "filter_picture.h"
37
38 #include "math.h"
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create      ( vlc_object_t * );
44 static void Destroy     ( vlc_object_t * );
45
46 static picture_t *Filter( filter_t *, picture_t * );
47 static int ExtractCallback( vlc_object_t *, char const *,
48                             vlc_value_t, vlc_value_t, void * );
49
50 static void get_red_from_yuv420( picture_t *, picture_t *, int, int, int );
51 static void get_green_from_yuv420( picture_t *, picture_t *, int, int, int );
52 static void get_blue_from_yuv420( picture_t *, picture_t *, int, int, int );
53 static void get_red_from_yuv422( picture_t *, picture_t *, int, int, int );
54 static void get_green_from_yuv422( picture_t *, picture_t *, int, int, int );
55 static void get_blue_from_yuv422( picture_t *, picture_t *, int, int, int );
56 static void make_projection_matrix( filter_t *, int color, int *matrix );
57 static void get_custom_from_yuv420( picture_t *, picture_t *, int, int, int, int * );
58 static void get_custom_from_yuv422( picture_t *, picture_t *, int, int, int, int * );
59 static void get_custom_from_packedyuv422( picture_t *, picture_t *, int * );
60
61
62 #define COMPONENT_TEXT N_("RGB component to extract")
63 #define COMPONENT_LONGTEXT N_("RGB component to extract. 0 for Red, 1 for Green and 2 for Blue.")
64 #define FILTER_PREFIX "extract-"
65
66 static const int pi_component_values[] = { 0xFF0000, 0x00FF00, 0x0000FF };
67 static const char *const ppsz_component_descriptions[] = {
68     "Red", "Green", "Blue" };
69
70 /*****************************************************************************
71  * Module descriptor
72  *****************************************************************************/
73 vlc_module_begin ()
74     set_description( N_("Extract RGB component video filter") )
75     set_shortname( N_("Extract" ))
76     set_category( CAT_VIDEO )
77     set_subcategory( SUBCAT_VIDEO_VFILTER )
78     set_capability( "video filter2", 0 )
79     add_shortcut( "extract" )
80
81     add_integer_with_range( FILTER_PREFIX "component", 0xFF0000, 1, 0xFFFFFF,
82                             COMPONENT_TEXT, COMPONENT_LONGTEXT, false )
83         change_integer_list( pi_component_values, ppsz_component_descriptions )
84
85     set_callbacks( Create, Destroy )
86 vlc_module_end ()
87
88 static const char *const ppsz_filter_options[] = {
89     "component", NULL
90 };
91
92 enum { RED=0xFF0000, GREEN=0x00FF00, BLUE=0x0000FF };
93 struct filter_sys_t
94 {
95     vlc_mutex_t lock;
96     int *projection_matrix;
97     uint32_t i_color;
98 };
99
100 /*****************************************************************************
101  * Create
102  *****************************************************************************/
103 static int Create( vlc_object_t *p_this )
104 {
105     filter_t *p_filter = (filter_t *)p_this;
106
107     switch( p_filter->fmt_in.video.i_chroma )
108     {
109         case VLC_CODEC_I420:
110         case VLC_CODEC_J420:
111         case VLC_CODEC_YV12:
112
113         case VLC_CODEC_I422:
114         case VLC_CODEC_J422:
115
116         CASE_PACKED_YUV_422
117             break;
118
119         default:
120             /* We only want planar YUV 4:2:0 or 4:2:2 */
121             msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
122                      (char*)&(p_filter->fmt_in.video.i_chroma) );
123             return VLC_EGENERIC;
124     }
125
126     /* Allocate structure */
127     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
128     if( p_filter->p_sys == NULL )
129         return VLC_ENOMEM;
130     p_filter->p_sys->projection_matrix = malloc( 9 * sizeof( int ) );
131     if( !p_filter->p_sys->projection_matrix )
132     {
133         free( p_filter->p_sys );
134         return VLC_ENOMEM;
135     }
136
137     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
138                        p_filter->p_cfg );
139
140     p_filter->p_sys->i_color = var_CreateGetIntegerCommand( p_filter,
141                                                FILTER_PREFIX "component" );
142     /* Matrix won't be used for RED, GREEN or BLUE in planar formats */
143     make_projection_matrix( p_filter, p_filter->p_sys->i_color,
144                             p_filter->p_sys->projection_matrix );
145     vlc_mutex_init( &p_filter->p_sys->lock );
146     var_AddCallback( p_filter, FILTER_PREFIX "component",
147                      ExtractCallback, p_filter->p_sys );
148
149     p_filter->pf_video_filter = Filter;
150
151     return VLC_SUCCESS;
152 }
153
154 /*****************************************************************************
155  * Destroy
156  *****************************************************************************/
157 static void Destroy( vlc_object_t *p_this )
158 {
159     filter_t *p_filter = (filter_t *)p_this;
160     filter_sys_t *p_sys = p_filter->p_sys;
161
162     var_DelCallback( p_filter, FILTER_PREFIX "component", ExtractCallback,
163                      p_sys );
164     vlc_mutex_destroy( &p_sys->lock );
165     free( p_sys->projection_matrix );
166     free( p_sys );
167 }
168
169 /*****************************************************************************
170  * Render
171  *****************************************************************************/
172 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
173 {
174     picture_t *p_outpic;
175     filter_sys_t *p_sys = p_filter->p_sys;
176
177     if( !p_pic ) return NULL;
178
179     p_outpic = filter_NewPicture( p_filter );
180     if( !p_outpic )
181     {
182         picture_Release( p_pic );
183         return NULL;
184     }
185
186     vlc_mutex_lock( &p_sys->lock );
187     switch( p_pic->format.i_chroma )
188     {
189         case VLC_CODEC_I420:
190         case VLC_CODEC_J420:
191         case VLC_CODEC_YV12:
192             switch( p_sys->i_color )
193             {
194                 case RED:
195                     get_red_from_yuv420( p_pic, p_outpic,
196                                          Y_PLANE, U_PLANE, V_PLANE );
197                     break;
198                 case GREEN:
199                     get_green_from_yuv420( p_pic, p_outpic,
200                                            Y_PLANE, U_PLANE, V_PLANE );
201                     break;
202                 case BLUE:
203                     get_blue_from_yuv420( p_pic, p_outpic,
204                                           Y_PLANE, U_PLANE, V_PLANE );
205                     break;
206                 default:
207                     get_custom_from_yuv420( p_pic, p_outpic,
208                                             Y_PLANE, U_PLANE, V_PLANE,
209                                             p_sys->projection_matrix);
210                     break;
211             }
212             break;
213
214         case VLC_CODEC_I422:
215         case VLC_CODEC_J422:
216             switch( p_filter->p_sys->i_color )
217             {
218                 case RED:
219                     get_red_from_yuv422( p_pic, p_outpic,
220                                          Y_PLANE, U_PLANE, V_PLANE );
221                     break;
222                 case GREEN:
223                     get_green_from_yuv422( p_pic, p_outpic,
224                                            Y_PLANE, U_PLANE, V_PLANE );
225                     break;
226                 case BLUE:
227                     get_blue_from_yuv422( p_pic, p_outpic,
228                                           Y_PLANE, U_PLANE, V_PLANE );
229                     break;
230                 default:
231                     get_custom_from_yuv422( p_pic, p_outpic,
232                                             Y_PLANE, U_PLANE, V_PLANE,
233                                             p_sys->projection_matrix);
234                     break;
235             }
236             break;
237
238         CASE_PACKED_YUV_422
239             get_custom_from_packedyuv422( p_pic, p_outpic,
240                                           p_sys->projection_matrix );
241             break;
242
243         default:
244             vlc_mutex_unlock( &p_sys->lock );
245             msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
246                       (char*)&(p_pic->format.i_chroma) );
247             picture_Release( p_pic );
248             return NULL;
249     }
250     vlc_mutex_unlock( &p_sys->lock );
251
252     return CopyInfoAndRelease( p_outpic, p_pic );
253 }
254
255 static inline uint8_t crop( int a )
256 {
257     if( a < 0 ) return 0;
258     if( a > 255 ) return 255;
259     else return (uint8_t)a;
260 }
261
262 #define U 128
263 #define V 128
264
265 static void mmult( double *res, double *a, double *b )
266 {
267     int i, j, k;
268     for( i = 0; i < 3; i++ )
269     {
270         for( j = 0; j < 3; j++ )
271         {
272             res[ i*3 + j ] = 0.;
273             for( k = 0; k < 3; k++ )
274             {
275                 res[ i*3 + j ] += a[ i*3 + k ] * b[ k*3 + j ];
276             }
277         }
278     }
279 }
280 static void make_projection_matrix( filter_t *p_filter, int color, int *matrix )
281 {
282     double left_matrix[9] =
283         {  76.24500,  149.68500,  29.07000,
284           -43.02765,  -84.47235, 127.50000,
285           127.50000, -106.76534, -20.73466 };
286     double right_matrix[9] =
287         { 257.00392,   0.00000,  360.31950,
288           257.00392, -88.44438, -183.53583,
289           257.00392, 455.41095,    0.00000 };
290     double red = ((double)(( 0xFF0000 & color )>>16))/255.;
291     double green = ((double)(( 0x00FF00 & color )>>8))/255.;
292     double blue = ((double)( 0x0000FF & color ))/255.;
293     double norm = sqrt( red*red + green*green + blue*blue );
294     if( norm > 0 )
295     {
296         red /= norm;
297         green /= norm;
298         blue /= norm;
299     }
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_in_pitch  = p_inpic->p[yp].i_pitch;
337     const int i_out_pitch = p_outpic->p[yp].i_pitch;
338
339     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
340     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
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_in_pitch;
344     while( y1in < yend )
345     {
346         const uint8_t *y1end = y1in + i_visible_pitch;
347         y2in  = y1in + i_in_pitch;
348         y2out = y1out + i_out_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_in_pitch  - i_visible_pitch;
365         y1out += 2*i_out_pitch - i_visible_pitch;
366         uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
367         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
368         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
369         vout  += p_outpic->p[vp].i_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_in_pitch  = p_inpic->p[yp].i_pitch;
384     const int i_out_pitch = p_outpic->p[yp].i_pitch;
385
386     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
387     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
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_in_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_in_pitch  - i_visible_pitch;
406         y1out += i_out_pitch - i_visible_pitch;
407         uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
408         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
409         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
410         vout  += p_outpic->p[vp].i_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_in_pitch  = p_inpic->p->i_pitch;
432     const int i_out_pitch = p_outpic->p->i_pitch;
433     const int i_visible_pitch = p_inpic->p->i_visible_pitch;
434     const int i_visible_lines = p_inpic->p->i_visible_lines;
435
436     const uint8_t *yend = yin + i_visible_lines * i_in_pitch;
437     while( yin < yend )
438     {
439         const uint8_t *ylend = yin + i_visible_pitch;
440         while( yin < ylend )
441         {
442             *uout = crop( (*yin * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
443                       / 65536 + U );
444             uout += 4;
445             *vout = crop( (*yin * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
446                      / 65536 + V );
447             vout += 4;
448             *yout = crop( (*yin * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
449                        / 65536 );
450             yin  += 2;
451             yout += 2;
452             *yout = crop( (*yin * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
453                        / 65536 );
454             yin  += 2;
455             yout += 2;
456             uin  += 4;
457             vin  += 4;
458         }
459         yin  += i_in_pitch  - i_visible_pitch;
460         yout += i_out_pitch - i_visible_pitch;
461         uin  += i_in_pitch  - i_visible_pitch;
462         uout += i_out_pitch - i_visible_pitch;
463         vin  += i_in_pitch  - i_visible_pitch;
464         vout += i_out_pitch - i_visible_pitch;
465     }
466 }
467
468 static void get_red_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
469                                  int yp, int up, int vp )
470 {
471     uint8_t *y1in = p_inpic->p[yp].p_pixels;
472     uint8_t *y2in;
473     uint8_t *vin  = p_inpic->p[vp].p_pixels;
474
475     uint8_t *y1out = p_outpic->p[yp].p_pixels;
476     uint8_t *y2out;
477     uint8_t *uout  = p_outpic->p[up].p_pixels;
478     uint8_t *vout  = p_outpic->p[vp].p_pixels;
479
480     const int i_in_pitch  = p_inpic->p[yp].i_pitch;
481     const int i_out_pitch = p_outpic->p[yp].i_pitch;
482
483     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
484     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
485     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
486
487     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
488     while( y1in < yend )
489     {
490         const uint8_t *y1end = y1in + i_visible_pitch;
491         y2in  = y1in + i_in_pitch;
492         y2out = y1out + i_out_pitch;
493         while( y1in < y1end )
494         {
495 /*
496 19595   0   27473
497 -11058  0   -15504
498 32768   0   45941
499 */
500             *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
501                       / 65536 + U );
502             *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
503                       / 65536 + V );
504             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
505                        / 65536 );
506             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
507                        / 65536 );
508             *y2out++ = crop( (*y2in++ * 19594 + (*vin - V) * 27473)
509                        / 65536 );
510             *y2out++ = crop( (*y2in++ * 19594 + (*vin++ - V) * 27473)
511                        / 65536 );
512         }
513         y1in  += 2*i_in_pitch  - i_visible_pitch;
514         y1out += 2*i_out_pitch - i_visible_pitch;
515         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
516         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
517         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
518     }
519 }
520
521 static void get_green_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
522                                  int yp, int up, int vp )
523 {
524     uint8_t *y1in = p_inpic->p[yp].p_pixels;
525     uint8_t *y2in;
526     uint8_t *uin  = p_inpic->p[up].p_pixels;
527     uint8_t *vin  = p_inpic->p[vp].p_pixels;
528
529     uint8_t *y1out = p_outpic->p[yp].p_pixels;
530     uint8_t *y2out;
531     uint8_t *uout  = p_outpic->p[up].p_pixels;
532     uint8_t *vout  = p_outpic->p[vp].p_pixels;
533
534     const int i_in_pitch  = p_inpic->p[yp].i_pitch;
535     const int i_out_pitch = p_outpic->p[yp].i_pitch;
536
537     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
538     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
539
540     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
541
542     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
543     while( y1in < yend )
544     {
545         const uint8_t *y1end = y1in + i_visible_pitch;
546         y2in  = y1in + i_in_pitch;
547         y2out = y1out + i_out_pitch;
548         while( y1in < y1end )
549         {
550 /*
551 38470   -13239  -27473
552 -21710  7471    15504
553 -27439  9443    19595
554 */
555             *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
556                       / 65536 + U );
557             *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
558                       / 65536 + V );
559             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
560                        / 65536 );
561             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
562                        / 65536 );
563             *y2out++ = crop( (*y2in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
564                        / 65536 );
565             *y2out++ = crop( (*y2in++ * 38470 + (*uin++ - U) * -13239 + (*vin++ -V) * -27473)
566                        / 65536 );
567         }
568         y1in  += 2*i_in_pitch  - i_visible_pitch;
569         y1out += 2*i_out_pitch - i_visible_pitch;
570         uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
571         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
572         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
573         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
574     }
575 }
576
577 static void get_blue_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
578                                  int yp, int up, int vp )
579 {
580     uint8_t *y1in = p_inpic->p[yp].p_pixels;
581     uint8_t *y2in;
582     uint8_t *uin  = p_inpic->p[up].p_pixels;
583
584     uint8_t *y1out = p_outpic->p[yp].p_pixels;
585     uint8_t *y2out;
586     uint8_t *uout  = p_outpic->p[up].p_pixels;
587     uint8_t *vout  = p_outpic->p[vp].p_pixels;
588
589     const int i_in_pitch  = p_inpic->p[yp].i_pitch;
590     const int i_out_pitch = p_outpic->p[yp].i_pitch;
591
592     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
593     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
594     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
595
596     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
597     while( y1in < yend )
598     {
599         const uint8_t *y1end = y1in + i_visible_pitch;
600         y2in  = y1in + i_in_pitch;
601         y2out = y1out + i_out_pitch;
602         while( y1in < y1end )
603         {
604 /*
605 7471    13239   0
606 32768   58065   0
607 -5329   -9443   0
608 */
609             *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
610                       / 65536 + U );
611             *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
612                       / 65536 + V );
613             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
614                        / 65536 );
615             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
616                        / 65536 );
617             *y2out++ = crop( (*y2in++ * 7471 + (*uin - U) * 13239 )
618                        / 65536 );
619             *y2out++ = crop( (*y2in++ * 7471 + (*uin++ - U) * 13239 )
620                        / 65536 );
621         }
622         y1in  += 2*i_in_pitch  - i_visible_pitch;
623         y1out += 2*i_out_pitch - i_visible_pitch;
624         uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
625         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
626         vout  += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
627     }
628 }
629
630 static void get_red_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
631                                  int yp, int up, int vp )
632 {
633     uint8_t *y1in = p_inpic->p[yp].p_pixels;
634     uint8_t *vin  = p_inpic->p[vp].p_pixels;
635
636     uint8_t *y1out = p_outpic->p[yp].p_pixels;
637     uint8_t *uout  = p_outpic->p[up].p_pixels;
638     uint8_t *vout  = p_outpic->p[vp].p_pixels;
639
640     const int i_in_pitch = p_inpic->p[yp].i_pitch;
641     const int i_out_pitch = p_inpic->p[yp].i_pitch;
642
643     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
644     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
645     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
646
647     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
648     while( y1in < yend )
649     {
650         const uint8_t *y1end = y1in + i_visible_pitch;
651         while( y1in < y1end )
652         {
653 /*
654 19595   0   27473
655 -11058  0   -15504
656 32768   0   45941
657 */
658             *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
659                       / 65536 + U );
660             *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
661                       / 65536 + V );
662             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
663                        / 65536 );
664             *y1out++ = crop( (*y1in++ * 19595 + (*vin++ - V) * 27473)
665                        / 65536 );
666         }
667         y1in  += i_in_pitch  - i_visible_pitch;
668         y1out += i_out_pitch - i_visible_pitch;
669         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
670         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
671         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
672     }
673 }
674
675 static void get_green_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
676                                    int yp, int up, int vp )
677 {
678     uint8_t *y1in = p_inpic->p[yp].p_pixels;
679     uint8_t *uin  = p_inpic->p[up].p_pixels;
680     uint8_t *vin  = p_inpic->p[vp].p_pixels;
681
682     uint8_t *y1out = p_outpic->p[yp].p_pixels;
683     uint8_t *uout  = p_outpic->p[up].p_pixels;
684     uint8_t *vout  = p_outpic->p[vp].p_pixels;
685
686     const int i_in_pitch  = p_inpic->p[yp].i_pitch;
687     const int i_out_pitch = p_outpic->p[yp].i_pitch;
688
689     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
690     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
691     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
692
693     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
694     while( y1in < yend )
695     {
696         const uint8_t *y1end = y1in + i_visible_pitch;
697         while( y1in < y1end )
698         {
699 /*
700 38470   -13239  -27473
701 -21710  7471    15504
702 -27439  9443    19595
703 */
704             *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
705                       / 65536 + U );
706             *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
707                       / 65536 + V );
708             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
709                        / 65536 );
710             *y1out++ = crop( (*y1in++ * 38470 + (*uin++-U) * -13239 + (*vin++-V) * -27473)
711                        / 65536 );
712         }
713         y1in  += i_in_pitch  - i_visible_pitch;
714         y1out += i_out_pitch - i_visible_pitch;
715         uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
716         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
717         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
718         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
719     }
720 }
721
722 static void get_blue_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
723                                  int yp, int up, int vp )
724 {
725     uint8_t *y1in = p_inpic->p[yp].p_pixels;
726     uint8_t *uin  = p_inpic->p[up].p_pixels;
727
728     uint8_t *y1out = p_outpic->p[yp].p_pixels;
729     uint8_t *uout  = p_outpic->p[up].p_pixels;
730     uint8_t *vout  = p_outpic->p[vp].p_pixels;
731
732     const int i_in_pitch  = p_inpic->p[yp].i_pitch;
733     const int i_out_pitch = p_outpic->p[yp].i_pitch;
734
735     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
736     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
737     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
738
739     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
740     while( y1in < yend )
741     {
742         const uint8_t *y1end = y1in + i_visible_pitch;
743         while( y1in < y1end )
744         {
745 /*
746 7471    13239   0
747 32768   58065   0
748 -5329   -9443   0
749 */
750             *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
751                       / 65536 + U );
752             *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
753                       / 65536 + V );
754             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
755                        / 65536 );
756             *y1out++ = crop( (*y1in++ * 7471 + (*uin++ - U) * 13239 )
757                        / 65536 );
758         }
759         y1in  += i_in_pitch  - i_visible_pitch;
760         y1out += i_out_pitch - i_visible_pitch;
761         uin   += p_inpic->p[up].i_pitch - i_uv_visible_pitch;
762         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
763         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
764     }
765 }
766
767 static int ExtractCallback( vlc_object_t *p_this, char const *psz_var,
768                             vlc_value_t oldval, vlc_value_t newval,
769                             void *p_data )
770 {
771     VLC_UNUSED(oldval);
772     filter_sys_t *p_sys = (filter_sys_t *)p_data;
773
774     vlc_mutex_lock( &p_sys->lock );
775     if( !strcmp( psz_var, FILTER_PREFIX "component" ) )
776     {
777         p_sys->i_color = newval.i_int;
778         /* Matrix won't be used for RED, GREEN or BLUE in planar formats */
779         make_projection_matrix( (filter_t *)p_this, p_sys->i_color,
780                                 p_sys->projection_matrix );
781     }
782     else
783     {
784         msg_Warn( p_this, "Unknown callback command." );
785     }
786     vlc_mutex_unlock( &p_sys->lock );
787     return VLC_SUCCESS;
788 }