]> git.sesse.net Git - vlc/blob - modules/video_filter/extract.c
mediacodec: don't loop in GetOutput
[vlc] / modules / video_filter / extract.c
1 /*****************************************************************************
2  * extract.c : Extract RGB components
3  *****************************************************************************
4  * Copyright (C) 2000-2006 VLC authors and VideoLAN
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 it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * 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 #define U 128
256 #define V 128
257
258 static void mmult( double *res, double *a, double *b )
259 {
260     int i, j, k;
261     for( i = 0; i < 3; i++ )
262     {
263         for( j = 0; j < 3; j++ )
264         {
265             res[ i*3 + j ] = 0.;
266             for( k = 0; k < 3; k++ )
267             {
268                 res[ i*3 + j ] += a[ i*3 + k ] * b[ k*3 + j ];
269             }
270         }
271     }
272 }
273 static void make_projection_matrix( filter_t *p_filter, int color, int *matrix )
274 {
275     double left_matrix[9] =
276         {  76.24500,  149.68500,  29.07000,
277           -43.02765,  -84.47235, 127.50000,
278           127.50000, -106.76534, -20.73466 };
279     double right_matrix[9] =
280         { 257.00392,   0.00000,  360.31950,
281           257.00392, -88.44438, -183.53583,
282           257.00392, 455.41095,    0.00000 };
283     double red = ((double)(( 0xFF0000 & color )>>16))/255.;
284     double green = ((double)(( 0x00FF00 & color )>>8))/255.;
285     double blue = ((double)( 0x0000FF & color ))/255.;
286     double norm = sqrt( red*red + green*green + blue*blue );
287     if( norm > 0 )
288     {
289         red /= norm;
290         green /= norm;
291         blue /= norm;
292     }
293     /* XXX: We might still need to norm the rgb_matrix */
294     double rgb_matrix[9] =
295         { red*red,    red*green,   red*blue,
296           red*green,  green*green, green*blue,
297           red*blue,   green*blue,  blue*blue };
298     double result1[9];
299     double result[9];
300     int i;
301     msg_Dbg( p_filter, "red: %f", red );
302     msg_Dbg( p_filter, "green: %f", green );
303     msg_Dbg( p_filter, "blue: %f", blue );
304     mmult( result1, rgb_matrix, right_matrix );
305     mmult( result, left_matrix, result1 );
306     for( i = 0; i < 9; i++ )
307     {
308         matrix[i] = (int)result[i];
309     }
310     msg_Dbg( p_filter, "Projection matrix:" );
311     msg_Dbg( p_filter, "%6d %6d %6d", matrix[0], matrix[1], matrix[2] );
312     msg_Dbg( p_filter, "%6d %6d %6d", matrix[3], matrix[4], matrix[5] );
313     msg_Dbg( p_filter, "%6d %6d %6d", matrix[6], matrix[7], matrix[8] );
314 }
315
316 static void get_custom_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
317                                     int yp, int up, int vp, int *m )
318 {
319     uint8_t *y1in = p_inpic->p[yp].p_pixels;
320     uint8_t *y2in;
321     uint8_t *uin  = p_inpic->p[up].p_pixels;
322     uint8_t *vin  = p_inpic->p[vp].p_pixels;
323
324     uint8_t *y1out = p_outpic->p[yp].p_pixels;
325     uint8_t *y2out;
326     uint8_t *uout  = p_outpic->p[up].p_pixels;
327     uint8_t *vout  = p_outpic->p[vp].p_pixels;
328
329     const int i_in_pitch  = p_inpic->p[yp].i_pitch;
330     const int i_out_pitch = p_outpic->p[yp].i_pitch;
331
332     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
333     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
334     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
335
336     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
337     while( y1in < yend )
338     {
339         const uint8_t *y1end = y1in + i_visible_pitch;
340         y2in  = y1in + i_in_pitch;
341         y2out = y1out + i_out_pitch;
342         while( y1in < y1end )
343         {
344             *uout++ = vlc_uint8( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
345                       / 65536 + U );
346             *vout++ = vlc_uint8( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
347                       / 65536 + V );
348             *y1out++ = vlc_uint8( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
349                        / 65536 );
350             *y1out++ = vlc_uint8( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
351                        / 65536 );
352             *y2out++ = vlc_uint8( (*y2in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
353                        / 65536 );
354             *y2out++ = vlc_uint8( (*y2in++ * m[0] + (*uin++ - U) * m[1] + (*vin++ -V) * m[2])
355                        / 65536 );
356         }
357         y1in  += 2*i_in_pitch  - i_visible_pitch;
358         y1out += 2*i_out_pitch - i_visible_pitch;
359         uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
360         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
361         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
362         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
363     }
364 }
365 static void get_custom_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
366                                     int yp, int up, int vp, int *m )
367 {
368     uint8_t *y1in = p_inpic->p[yp].p_pixels;
369     uint8_t *uin  = p_inpic->p[up].p_pixels;
370     uint8_t *vin  = p_inpic->p[vp].p_pixels;
371
372     uint8_t *y1out = p_outpic->p[yp].p_pixels;
373     uint8_t *uout  = p_outpic->p[up].p_pixels;
374     uint8_t *vout  = p_outpic->p[vp].p_pixels;
375
376     const int i_in_pitch  = p_inpic->p[yp].i_pitch;
377     const int i_out_pitch = p_outpic->p[yp].i_pitch;
378
379     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
380     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
381     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
382
383     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
384     while( y1in < yend )
385     {
386         const uint8_t *y1end = y1in + i_visible_pitch;
387         while( y1in < y1end )
388         {
389             *uout++ = vlc_uint8( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
390                       / 65536 + U );
391             *vout++ = vlc_uint8( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
392                       / 65536 + V );
393             *y1out++ = vlc_uint8( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
394                        / 65536 );
395             *y1out++ = vlc_uint8( (*y1in++ * m[0] + (*uin++ -U) * m[1] + (*vin++ -V) * m[2])
396                        / 65536 );
397         }
398         y1in  += i_in_pitch  - i_visible_pitch;
399         y1out += i_out_pitch - i_visible_pitch;
400         uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
401         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
402         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
403         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
404     }
405 }
406
407 static void get_custom_from_packedyuv422( picture_t *p_inpic,
408                                           picture_t *p_outpic,
409                                           int *m )
410 {
411     int i_y_offset, i_u_offset, i_v_offset;
412     if( GetPackedYuvOffsets( p_inpic->format.i_chroma, &i_y_offset,
413                          &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
414         return;
415
416     uint8_t *yin = p_inpic->p->p_pixels + i_y_offset;
417     uint8_t *uin = p_inpic->p->p_pixels + i_u_offset;
418     uint8_t *vin = p_inpic->p->p_pixels + i_v_offset;
419
420     uint8_t *yout = p_outpic->p->p_pixels + i_y_offset;
421     uint8_t *uout = p_outpic->p->p_pixels + i_u_offset;
422     uint8_t *vout = p_outpic->p->p_pixels + i_v_offset;
423
424     const int i_in_pitch  = p_inpic->p->i_pitch;
425     const int i_out_pitch = p_outpic->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_in_pitch;
430     while( yin < yend )
431     {
432         const uint8_t *ylend = yin + i_visible_pitch;
433         while( yin < ylend )
434         {
435             *uout = vlc_uint8( (*yin * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
436                       / 65536 + U );
437             uout += 4;
438             *vout = vlc_uint8( (*yin * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
439                      / 65536 + V );
440             vout += 4;
441             *yout = vlc_uint8( (*yin * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
442                        / 65536 );
443             yin  += 2;
444             yout += 2;
445             *yout = vlc_uint8( (*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_in_pitch  - i_visible_pitch;
453         yout += i_out_pitch - i_visible_pitch;
454         uin  += i_in_pitch  - i_visible_pitch;
455         uout += i_out_pitch - i_visible_pitch;
456         vin  += i_in_pitch  - i_visible_pitch;
457         vout += i_out_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_in_pitch  = p_inpic->p[yp].i_pitch;
474     const int i_out_pitch = p_outpic->p[yp].i_pitch;
475
476     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
477     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
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_in_pitch;
481     while( y1in < yend )
482     {
483         const uint8_t *y1end = y1in + i_visible_pitch;
484         y2in  = y1in + i_in_pitch;
485         y2out = y1out + i_out_pitch;
486         while( y1in < y1end )
487         {
488 /*
489 19595   0   27473
490 -11058  0   -15504
491 32768   0   45941
492 */
493             *uout++ = vlc_uint8( (*y1in * -11058 + (*vin - V) * -15504)
494                       / 65536 + U );
495             *vout++ = vlc_uint8( (*y1in * 32768 + (*vin - V) * 45941)
496                       / 65536 + V );
497             *y1out++ = vlc_uint8( (*y1in++ * 19595 + (*vin - V) * 27473)
498                        / 65536 );
499             *y1out++ = vlc_uint8( (*y1in++ * 19595 + (*vin - V) * 27473)
500                        / 65536 );
501             *y2out++ = vlc_uint8( (*y2in++ * 19594 + (*vin - V) * 27473)
502                        / 65536 );
503             *y2out++ = vlc_uint8( (*y2in++ * 19594 + (*vin++ - V) * 27473)
504                        / 65536 );
505         }
506         y1in  += 2*i_in_pitch  - i_visible_pitch;
507         y1out += 2*i_out_pitch - i_visible_pitch;
508         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
509         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
510         vout  += p_outpic->p[vp].i_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_in_pitch  = p_inpic->p[yp].i_pitch;
528     const int i_out_pitch = p_outpic->p[yp].i_pitch;
529
530     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
531     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
532
533     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
534
535     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
536     while( y1in < yend )
537     {
538         const uint8_t *y1end = y1in + i_visible_pitch;
539         y2in  = y1in + i_in_pitch;
540         y2out = y1out + i_out_pitch;
541         while( y1in < y1end )
542         {
543 /*
544 38470   -13239  -27473
545 -21710  7471    15504
546 -27439  9443    19595
547 */
548             *uout++ = vlc_uint8( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
549                       / 65536 + U );
550             *vout++ = vlc_uint8( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
551                       / 65536 + V );
552             *y1out++ = vlc_uint8( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
553                        / 65536 );
554             *y1out++ = vlc_uint8( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
555                        / 65536 );
556             *y2out++ = vlc_uint8( (*y2in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
557                        / 65536 );
558             *y2out++ = vlc_uint8( (*y2in++ * 38470 + (*uin++ - U) * -13239 + (*vin++ -V) * -27473)
559                        / 65536 );
560         }
561         y1in  += 2*i_in_pitch  - i_visible_pitch;
562         y1out += 2*i_out_pitch - i_visible_pitch;
563         uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
564         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
565         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
566         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
567     }
568 }
569
570 static void get_blue_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
571                                  int yp, int up, int vp )
572 {
573     uint8_t *y1in = p_inpic->p[yp].p_pixels;
574     uint8_t *y2in;
575     uint8_t *uin  = p_inpic->p[up].p_pixels;
576
577     uint8_t *y1out = p_outpic->p[yp].p_pixels;
578     uint8_t *y2out;
579     uint8_t *uout  = p_outpic->p[up].p_pixels;
580     uint8_t *vout  = p_outpic->p[vp].p_pixels;
581
582     const int i_in_pitch  = p_inpic->p[yp].i_pitch;
583     const int i_out_pitch = p_outpic->p[yp].i_pitch;
584
585     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
586     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
587     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
588
589     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
590     while( y1in < yend )
591     {
592         const uint8_t *y1end = y1in + i_visible_pitch;
593         y2in  = y1in + i_in_pitch;
594         y2out = y1out + i_out_pitch;
595         while( y1in < y1end )
596         {
597 /*
598 7471    13239   0
599 32768   58065   0
600 -5329   -9443   0
601 */
602             *uout++ = vlc_uint8( (*y1in* 32768 + (*uin - U) * 58065 )
603                       / 65536 + U );
604             *vout++ = vlc_uint8( (*y1in * -5329 + (*uin - U) * -9443 )
605                       / 65536 + V );
606             *y1out++ = vlc_uint8( (*y1in++ * 7471 + (*uin - U) * 13239 )
607                        / 65536 );
608             *y1out++ = vlc_uint8( (*y1in++ * 7471 + (*uin - U) * 13239 )
609                        / 65536 );
610             *y2out++ = vlc_uint8( (*y2in++ * 7471 + (*uin - U) * 13239 )
611                        / 65536 );
612             *y2out++ = vlc_uint8( (*y2in++ * 7471 + (*uin++ - U) * 13239 )
613                        / 65536 );
614         }
615         y1in  += 2*i_in_pitch  - i_visible_pitch;
616         y1out += 2*i_out_pitch - i_visible_pitch;
617         uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
618         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
619         vout  += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
620     }
621 }
622
623 static void get_red_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
624                                  int yp, int up, int vp )
625 {
626     uint8_t *y1in = p_inpic->p[yp].p_pixels;
627     uint8_t *vin  = p_inpic->p[vp].p_pixels;
628
629     uint8_t *y1out = p_outpic->p[yp].p_pixels;
630     uint8_t *uout  = p_outpic->p[up].p_pixels;
631     uint8_t *vout  = p_outpic->p[vp].p_pixels;
632
633     const int i_in_pitch = p_inpic->p[yp].i_pitch;
634     const int i_out_pitch = p_inpic->p[yp].i_pitch;
635
636     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
637     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
638     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
639
640     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
641     while( y1in < yend )
642     {
643         const uint8_t *y1end = y1in + i_visible_pitch;
644         while( y1in < y1end )
645         {
646 /*
647 19595   0   27473
648 -11058  0   -15504
649 32768   0   45941
650 */
651             *uout++ = vlc_uint8( (*y1in * -11058 + (*vin - V) * -15504)
652                       / 65536 + U );
653             *vout++ = vlc_uint8( (*y1in * 32768 + (*vin - V) * 45941)
654                       / 65536 + V );
655             *y1out++ = vlc_uint8( (*y1in++ * 19595 + (*vin - V) * 27473)
656                        / 65536 );
657             *y1out++ = vlc_uint8( (*y1in++ * 19595 + (*vin++ - V) * 27473)
658                        / 65536 );
659         }
660         y1in  += i_in_pitch  - i_visible_pitch;
661         y1out += i_out_pitch - i_visible_pitch;
662         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
663         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
664         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
665     }
666 }
667
668 static void get_green_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
669                                    int yp, int up, int vp )
670 {
671     uint8_t *y1in = p_inpic->p[yp].p_pixels;
672     uint8_t *uin  = p_inpic->p[up].p_pixels;
673     uint8_t *vin  = p_inpic->p[vp].p_pixels;
674
675     uint8_t *y1out = p_outpic->p[yp].p_pixels;
676     uint8_t *uout  = p_outpic->p[up].p_pixels;
677     uint8_t *vout  = p_outpic->p[vp].p_pixels;
678
679     const int i_in_pitch  = p_inpic->p[yp].i_pitch;
680     const int i_out_pitch = p_outpic->p[yp].i_pitch;
681
682     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
683     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
684     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
685
686     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
687     while( y1in < yend )
688     {
689         const uint8_t *y1end = y1in + i_visible_pitch;
690         while( y1in < y1end )
691         {
692 /*
693 38470   -13239  -27473
694 -21710  7471    15504
695 -27439  9443    19595
696 */
697             *uout++ = vlc_uint8( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
698                       / 65536 + U );
699             *vout++ = vlc_uint8( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
700                       / 65536 + V );
701             *y1out++ = vlc_uint8( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
702                        / 65536 );
703             *y1out++ = vlc_uint8( (*y1in++ * 38470 + (*uin++-U) * -13239 + (*vin++-V) * -27473)
704                        / 65536 );
705         }
706         y1in  += i_in_pitch  - i_visible_pitch;
707         y1out += i_out_pitch - i_visible_pitch;
708         uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
709         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
710         vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
711         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
712     }
713 }
714
715 static void get_blue_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
716                                  int yp, int up, int vp )
717 {
718     uint8_t *y1in = p_inpic->p[yp].p_pixels;
719     uint8_t *uin  = p_inpic->p[up].p_pixels;
720
721     uint8_t *y1out = p_outpic->p[yp].p_pixels;
722     uint8_t *uout  = p_outpic->p[up].p_pixels;
723     uint8_t *vout  = p_outpic->p[vp].p_pixels;
724
725     const int i_in_pitch  = p_inpic->p[yp].i_pitch;
726     const int i_out_pitch = p_outpic->p[yp].i_pitch;
727
728     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
729     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
730     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
731
732     const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
733     while( y1in < yend )
734     {
735         const uint8_t *y1end = y1in + i_visible_pitch;
736         while( y1in < y1end )
737         {
738 /*
739 7471    13239   0
740 32768   58065   0
741 -5329   -9443   0
742 */
743             *uout++ = vlc_uint8( (*y1in* 32768 + (*uin - U) * 58065 )
744                       / 65536 + U );
745             *vout++ = vlc_uint8( (*y1in * -5329 + (*uin - U) * -9443 )
746                       / 65536 + V );
747             *y1out++ = vlc_uint8( (*y1in++ * 7471 + (*uin - U) * 13239 )
748                        / 65536 );
749             *y1out++ = vlc_uint8( (*y1in++ * 7471 + (*uin++ - U) * 13239 )
750                        / 65536 );
751         }
752         y1in  += i_in_pitch  - i_visible_pitch;
753         y1out += i_out_pitch - i_visible_pitch;
754         uin   += p_inpic->p[up].i_pitch - i_uv_visible_pitch;
755         uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
756         vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
757     }
758 }
759
760 static int ExtractCallback( vlc_object_t *p_this, char const *psz_var,
761                             vlc_value_t oldval, vlc_value_t newval,
762                             void *p_data )
763 {
764     VLC_UNUSED(oldval);
765     filter_sys_t *p_sys = (filter_sys_t *)p_data;
766
767     vlc_mutex_lock( &p_sys->lock );
768     if( !strcmp( psz_var, FILTER_PREFIX "component" ) )
769     {
770         p_sys->i_color = newval.i_int;
771         /* Matrix won't be used for RED, GREEN or BLUE in planar formats */
772         make_projection_matrix( (filter_t *)p_this, p_sys->i_color,
773                                 p_sys->projection_matrix );
774     }
775     else
776     {
777         msg_Warn( p_this, "Unknown callback command." );
778     }
779     vlc_mutex_unlock( &p_sys->lock );
780     return VLC_SUCCESS;
781 }