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