]> git.sesse.net Git - vlc/blob - modules/video_filter/extract.c
* Make it possible to extract any color component from the video. (Red, Green and...
[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 #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 /*
329 38470   -13239  -27473
330 -21710  7471    15504
331 -27439  9443    19595
332 */
333             *uout++ = crop( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
334                       / 65536 + U );
335             *vout++ = crop( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
336                       / 65536 + V );
337             *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
338                        / 65536 );
339             *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
340                        / 65536 );
341             *y2out++ = crop( (*y2in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
342                        / 65536 );
343             *y2out++ = crop( (*y2in++ * m[0] + (*uin++ - U) * m[1] + (*vin++ -V) * m[2])
344                        / 65536 );
345         }
346         y1in  += 2*i_pitch - i_visible_pitch;
347         y1out += 2*i_pitch - i_visible_pitch;
348         uin   += i_uv_pitch - i_uv_visible_pitch;
349         uout  += i_uv_pitch - i_uv_visible_pitch;
350         vin   += i_uv_pitch - i_uv_visible_pitch;
351         vout  += i_uv_pitch - i_uv_visible_pitch;
352     }
353 }
354 static void get_custom_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
355                                     int yp, int up, int vp, int *m )
356 {
357     uint8_t *y1in = p_inpic->p[yp].p_pixels;
358     uint8_t *uin  = p_inpic->p[up].p_pixels;
359     uint8_t *vin  = p_inpic->p[vp].p_pixels;
360
361     uint8_t *y1out = p_outpic->p[yp].p_pixels;
362     uint8_t *uout  = p_outpic->p[up].p_pixels;
363     uint8_t *vout  = p_outpic->p[vp].p_pixels;
364
365     const int i_pitch = p_inpic->p[yp].i_pitch;
366     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
367     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
368
369     const int i_uv_pitch = p_inpic->p[up].i_pitch;
370     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
371
372     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
373     while( y1in < yend )
374     {
375         const uint8_t *y1end = y1in + i_visible_pitch;
376         while( y1in < y1end )
377         {
378 /*
379 38470   -13239  -27473
380 -21710  7471    15504
381 -27439  9443    19595
382 */
383             *uout++ = crop( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
384                       / 65536 + U );
385             *vout++ = crop( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
386                       / 65536 + V );
387             *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
388                        / 65536 );
389             *y1out++ = crop( (*y1in++ * m[0] + (*uin++ -U) * m[1] + (*vin++ -V) * m[2])
390                        / 65536 );
391         }
392         y1in  += 2*i_pitch - i_visible_pitch;
393         y1out += 2*i_pitch - i_visible_pitch;
394         uin   += i_uv_pitch - i_uv_visible_pitch;
395         uout  += i_uv_pitch - i_uv_visible_pitch;
396         vin   += i_uv_pitch - i_uv_visible_pitch;
397         vout  += i_uv_pitch - i_uv_visible_pitch;
398     }
399 }
400
401 static void get_red_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
402                                  int yp, int up, int vp )
403 {
404     uint8_t *y1in = p_inpic->p[yp].p_pixels;
405     uint8_t *y2in;
406     uint8_t *vin  = p_inpic->p[vp].p_pixels;
407
408     uint8_t *y1out = p_outpic->p[yp].p_pixels;
409     uint8_t *y2out;
410     uint8_t *uout  = p_outpic->p[up].p_pixels;
411     uint8_t *vout  = p_outpic->p[vp].p_pixels;
412
413     const int i_pitch = p_inpic->p[yp].i_pitch;
414     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
415     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
416
417     const int i_uv_pitch = p_inpic->p[up].i_pitch;
418     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
419
420     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
421     while( y1in < yend )
422     {
423         const uint8_t *y1end = y1in + i_visible_pitch;
424         y2in  = y1in + i_pitch;
425         y2out = y1out + i_pitch;
426         while( y1in < y1end )
427         {
428 /*
429 19595   0   27473
430 -11058  0   -15504
431 32768   0   45941
432 */
433             *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
434                       / 65536 + U );
435             *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
436                       / 65536 + V );
437             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
438                        / 65536 );
439             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
440                        / 65536 );
441             *y2out++ = crop( (*y2in++ * 19594 + (*vin - V) * 27473)
442                        / 65536 );
443             *y2out++ = crop( (*y2in++ * 19594 + (*vin++ - V) * 27473)
444                        / 65536 );
445         }
446         y1in  += 2*i_pitch - i_visible_pitch;
447         y1out += 2*i_pitch - i_visible_pitch;
448         uout  += i_uv_pitch - i_uv_visible_pitch;
449         vin   += i_uv_pitch - i_uv_visible_pitch;
450         vout  += i_uv_pitch - i_uv_visible_pitch;
451     }
452 }
453
454 static void get_green_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
455                                  int yp, int up, int vp )
456 {
457     uint8_t *y1in = p_inpic->p[yp].p_pixels;
458     uint8_t *y2in;
459     uint8_t *uin  = p_inpic->p[up].p_pixels;
460     uint8_t *vin  = p_inpic->p[vp].p_pixels;
461
462     uint8_t *y1out = p_outpic->p[yp].p_pixels;
463     uint8_t *y2out;
464     uint8_t *uout  = p_outpic->p[up].p_pixels;
465     uint8_t *vout  = p_outpic->p[vp].p_pixels;
466
467     const int i_pitch = p_inpic->p[yp].i_pitch;
468     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
469     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
470
471     const int i_uv_pitch = p_inpic->p[up].i_pitch;
472     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
473
474     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
475     while( y1in < yend )
476     {
477         const uint8_t *y1end = y1in + i_visible_pitch;
478         y2in  = y1in + i_pitch;
479         y2out = y1out + i_pitch;
480         while( y1in < y1end )
481         {
482 /*
483 38470   -13239  -27473
484 -21710  7471    15504
485 -27439  9443    19595
486 */
487             *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
488                       / 65536 + U );
489             *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
490                       / 65536 + V );
491             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
492                        / 65536 );
493             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
494                        / 65536 );
495             *y2out++ = crop( (*y2in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
496                        / 65536 );
497             *y2out++ = crop( (*y2in++ * 38470 + (*uin++ - U) * -13239 + (*vin++ -V) * -27473)
498                        / 65536 );
499         }
500         y1in  += 2*i_pitch - i_visible_pitch;
501         y1out += 2*i_pitch - i_visible_pitch;
502         uin   += i_uv_pitch - i_uv_visible_pitch;
503         uout  += i_uv_pitch - i_uv_visible_pitch;
504         vin   += i_uv_pitch - i_uv_visible_pitch;
505         vout  += i_uv_pitch - i_uv_visible_pitch;
506     }
507 }
508
509 static void get_blue_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
510                                  int yp, int up, int vp )
511 {
512     uint8_t *y1in = p_inpic->p[yp].p_pixels;
513     uint8_t *y2in;
514     uint8_t *uin  = p_inpic->p[up].p_pixels;
515
516     uint8_t *y1out = p_outpic->p[yp].p_pixels;
517     uint8_t *y2out;
518     uint8_t *uout  = p_outpic->p[up].p_pixels;
519     uint8_t *vout  = p_outpic->p[vp].p_pixels;
520
521     const int i_pitch = p_inpic->p[yp].i_pitch;
522     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
523     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
524
525     const int i_uv_pitch = p_inpic->p[up].i_pitch;
526     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
527
528     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
529     while( y1in < yend )
530     {
531         const uint8_t *y1end = y1in + i_visible_pitch;
532         y2in  = y1in + i_pitch;
533         y2out = y1out + i_pitch;
534         while( y1in < y1end )
535         {
536 /*
537 7471    13239   0
538 32768   58065   0
539 -5329   -9443   0
540 */
541             *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
542                       / 65536 + U );
543             *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
544                       / 65536 + V );
545             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
546                        / 65536 );
547             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
548                        / 65536 );
549             *y2out++ = crop( (*y2in++ * 7471 + (*uin - U) * 13239 )
550                        / 65536 );
551             *y2out++ = crop( (*y2in++ * 7471 + (*uin++ - U) * 13239 )
552                        / 65536 );
553         }
554         y1in  += 2*i_pitch - i_visible_pitch;
555         y1out += 2*i_pitch - i_visible_pitch;
556         uin   += i_uv_pitch - i_uv_visible_pitch;
557         uout  += i_uv_pitch - i_uv_visible_pitch;
558         vout  += i_uv_pitch - i_uv_visible_pitch;
559     }
560 }
561
562 static void get_red_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
563                                  int yp, int up, int vp )
564 {
565     uint8_t *y1in = p_inpic->p[yp].p_pixels;
566     uint8_t *vin  = p_inpic->p[vp].p_pixels;
567
568     uint8_t *y1out = p_outpic->p[yp].p_pixels;
569     uint8_t *uout  = p_outpic->p[up].p_pixels;
570     uint8_t *vout  = p_outpic->p[vp].p_pixels;
571
572     const int i_pitch = p_inpic->p[yp].i_pitch;
573     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
574     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
575
576     const int i_uv_pitch = p_inpic->p[up].i_pitch;
577     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
578
579     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
580     while( y1in < yend )
581     {
582         const uint8_t *y1end = y1in + i_visible_pitch;
583         while( y1in < y1end )
584         {
585 /*
586 19595   0   27473
587 -11058  0   -15504
588 32768   0   45941
589 */
590             *uout++ = crop( (*y1in * -11058 + (*vin - V) * -15504)
591                       / 65536 + U );
592             *vout++ = crop( (*y1in * 32768 + (*vin - V) * 45941)
593                       / 65536 + V );
594             *y1out++ = crop( (*y1in++ * 19595 + (*vin - V) * 27473)
595                        / 65536 );
596             *y1out++ = crop( (*y1in++ * 19595 + (*vin++ - V) * 27473)
597                        / 65536 );
598         }
599         y1in  += i_pitch - i_visible_pitch;
600         y1out += i_pitch - i_visible_pitch;
601         uout  += i_uv_pitch - i_uv_visible_pitch;
602         vin   += i_uv_pitch - i_uv_visible_pitch;
603         vout  += i_uv_pitch - i_uv_visible_pitch;
604     }
605 }
606
607 static void get_green_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
608                                    int yp, int up, int vp )
609 {
610     uint8_t *y1in = p_inpic->p[yp].p_pixels;
611     uint8_t *uin  = p_inpic->p[up].p_pixels;
612     uint8_t *vin  = p_inpic->p[vp].p_pixels;
613
614     uint8_t *y1out = p_outpic->p[yp].p_pixels;
615     uint8_t *uout  = p_outpic->p[up].p_pixels;
616     uint8_t *vout  = p_outpic->p[vp].p_pixels;
617
618     const int i_pitch = p_inpic->p[yp].i_pitch;
619     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
620     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
621
622     const int i_uv_pitch = p_inpic->p[up].i_pitch;
623     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
624
625     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
626     while( y1in < yend )
627     {
628         const uint8_t *y1end = y1in + i_visible_pitch;
629         while( y1in < y1end )
630         {
631 /*
632 38470   -13239  -27473
633 -21710  7471    15504
634 -27439  9443    19595
635 */
636             *uout++ = crop( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
637                       / 65536 + U );
638             *vout++ = crop( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
639                       / 65536 + V );
640             *y1out++ = crop( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
641                        / 65536 );
642             *y1out++ = crop( (*y1in++ * 38470 + (*uin++-U) * -13239 + (*vin++-V) * -27473)
643                        / 65536 );
644         }
645         y1in  += i_pitch - i_visible_pitch;
646         y1out += i_pitch - i_visible_pitch;
647         uin   += i_uv_pitch - i_uv_visible_pitch;
648         uout  += i_uv_pitch - i_uv_visible_pitch;
649         vin   += i_uv_pitch - i_uv_visible_pitch;
650         vout  += i_uv_pitch - i_uv_visible_pitch;
651     }
652 }
653
654 static void get_blue_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
655                                  int yp, int up, int vp )
656 {
657     uint8_t *y1in = p_inpic->p[yp].p_pixels;
658     uint8_t *uin  = p_inpic->p[up].p_pixels;
659
660     uint8_t *y1out = p_outpic->p[yp].p_pixels;
661     uint8_t *uout  = p_outpic->p[up].p_pixels;
662     uint8_t *vout  = p_outpic->p[vp].p_pixels;
663
664     const int i_pitch = p_inpic->p[yp].i_pitch;
665     const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
666     const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
667
668     const int i_uv_pitch = p_inpic->p[up].i_pitch;
669     const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
670
671     const uint8_t *yend = y1in + i_visible_lines * i_pitch;
672     while( y1in < yend )
673     {
674         const uint8_t *y1end = y1in + i_visible_pitch;
675         while( y1in < y1end )
676         {
677 /*
678 7471    13239   0
679 32768   58065   0
680 -5329   -9443   0
681 */
682             *uout++ = crop( (*y1in* 32768 + (*uin - U) * 58065 )
683                       / 65536 + U );
684             *vout++ = crop( (*y1in * -5329 + (*uin - U) * -9443 )
685                       / 65536 + V );
686             *y1out++ = crop( (*y1in++ * 7471 + (*uin - U) * 13239 )
687                        / 65536 );
688             *y1out++ = crop( (*y1in++ * 7471 + (*uin++ - U) * 13239 )
689                        / 65536 );
690         }
691         y1in  += i_pitch - i_visible_pitch;
692         y1out += i_pitch - i_visible_pitch;
693         uin   += i_uv_pitch - i_uv_visible_pitch;
694         uout  += i_uv_pitch - i_uv_visible_pitch;
695         vout  += i_uv_pitch - i_uv_visible_pitch;
696     }
697 }