]> git.sesse.net Git - vlc/blob - src/video_output/video_yuv.c
Nouvelle interface, effacement des zones modifi�es d'une image sur 2,
[vlc] / src / video_output / video_yuv.c
1 /*******************************************************************************
2  * video_yuv.c: YUV transformation functions
3  * (c)1999 VideoLAN
4  *******************************************************************************
5  * Provides functions to perform the YUV conversion. The functions provided here
6  * are a complete and portable C implementation, and may be replaced in certain
7  * case by optimized functions.
8  *******************************************************************************/
9
10 /*******************************************************************************
11  * Preamble
12  *******************************************************************************/
13 #include <math.h>
14 #include <errno.h>
15 #include <string.h>
16 #include <stdlib.h>
17
18 #include "common.h"
19 #include "config.h"
20 #include "mtime.h"
21 #include "vlc_thread.h"
22 #include "video.h"
23 #include "video_output.h"
24 #include "video_yuv.h"
25 #include "intf_msg.h"
26
27 /*******************************************************************************
28  * Constants
29  *******************************************************************************/
30
31 /* RGB/YUV inversion matrix (ISO/IEC 13818-2 section 6.3.6, table 6.9) */
32 const int MATRIX_COEFFICIENTS_TABLE[8][4] =
33 {
34   {117504, 138453, 13954, 34903},       /* no sequence_display_extension */
35   {117504, 138453, 13954, 34903},       /* ITU-R Rec. 709 (1990) */
36   {104597, 132201, 25675, 53279},       /* unspecified */
37   {104597, 132201, 25675, 53279},       /* reserved */
38   {104448, 132798, 24759, 53109},       /* FCC */
39   {104597, 132201, 25675, 53279},       /* ITU-R Rec. 624-4 System B, G */
40   {104597, 132201, 25675, 53279},       /* SMPTE 170M */
41   {117579, 136230, 16907, 35559}        /* SMPTE 240M (1987) */
42 };
43
44 /*******************************************************************************
45  * Local prototypes
46  *******************************************************************************/
47 static int      BinaryLog         ( u32 i );
48 static void     MaskToShift       ( int *pi_right, int *pi_left, u32 i_mask );
49 static void     SetGammaTable     ( int *pi_table, double f_gamma );
50 static void     SetTables         ( vout_thread_t *p_vout );
51
52 static void     ConvertY4Gray16   ( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
53                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
54 static void     ConvertY4Gray24   ( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
55                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
56 static void     ConvertY4Gray32   ( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
57                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
58 static void     ConvertYUV420RGB16( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
59                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
60 static void     ConvertYUV422RGB16( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
61                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
62 static void     ConvertYUV444RGB16( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
63                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
64 static void     ConvertYUV420RGB24( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
65                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
66 static void     ConvertYUV422RGB24( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
67                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
68 static void     ConvertYUV444RGB24( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
69                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
70 static void     ConvertYUV420RGB32( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
71                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
72 static void     ConvertYUV422RGB32( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
73                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
74 static void     ConvertYUV444RGB32( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
75                                     int i_width, int i_height, int i_eol, int i_pic_eol, int i_scale, int i_matrix_coefficients );
76
77 /*******************************************************************************
78  * CLIP_BYTE macro: boundary detection
79  *******************************************************************************
80  * Return parameter if between 0 and 255, else return nearest boundary (0 or 
81  * 255). This macro is used to build translations tables.
82  *******************************************************************************/
83 #define CLIP_BYTE( i_val ) ( (i_val < 0) ? 0 : ((i_val > 255) ? 255 : i_val) )
84
85 /*******************************************************************************
86  * LINE_COPY macro: memcopy using 16 pixels blocks
87  *******************************************************************************
88  * Variables:
89  *      p_pic                   destination pointer
90  *      p_pic_src               source pointer
91  *      i_width                 width
92  *      i_x                     index
93  *******************************************************************************/
94 #define LINE_COPY                                                       \
95 for( i_x = 0; i_x < i_width; i_x+=16 )                                  \
96 {                                                                       \
97     *p_pic++ = *p_pic_src++;                                            \
98     *p_pic++ = *p_pic_src++;                                            \
99     *p_pic++ = *p_pic_src++;                                            \
100     *p_pic++ = *p_pic_src++;                                            \
101     *p_pic++ = *p_pic_src++;                                            \
102     *p_pic++ = *p_pic_src++;                                            \
103     *p_pic++ = *p_pic_src++;                                            \
104     *p_pic++ = *p_pic_src++;                                            \
105     *p_pic++ = *p_pic_src++;                                            \
106     *p_pic++ = *p_pic_src++;                                            \
107     *p_pic++ = *p_pic_src++;                                            \
108     *p_pic++ = *p_pic_src++;                                            \
109     *p_pic++ = *p_pic_src++;                                            \
110     *p_pic++ = *p_pic_src++;                                            \
111     *p_pic++ = *p_pic_src++;                                            \
112     *p_pic++ = *p_pic_src++;                                            \
113 }
114
115 /*******************************************************************************
116  * CONVERT_YUV_GRAY macro: grayscale YUV convertion
117  *******************************************************************************
118  * Variables:
119  *      ...see vout_convert_t
120  *      i_x, i_y                coordinates
121  *      i_pic_copy              same type as p_pic
122  *      p_gray                  gray translation table
123  *******************************************************************************/
124 #define CONVERT_YUV_GRAY                                                \
125 for (i_y = 0; i_y < i_height ; i_y++)                                   \
126 {                                                                       \
127     for (i_x = 0; i_x < i_width; i_x += 16)                             \
128     {                                                                   \
129         /* Convert 16 pixels (width is always multiple of 16 */         \
130         *p_pic++ = p_gray[ *p_y++ ];                                    \
131         *p_pic++ = p_gray[ *p_y++ ];                                    \
132         *p_pic++ = p_gray[ *p_y++ ];                                    \
133         *p_pic++ = p_gray[ *p_y++ ];                                    \
134         *p_pic++ = p_gray[ *p_y++ ];                                    \
135         *p_pic++ = p_gray[ *p_y++ ];                                    \
136         *p_pic++ = p_gray[ *p_y++ ];                                    \
137         *p_pic++ = p_gray[ *p_y++ ];                                    \
138         *p_pic++ = p_gray[ *p_y++ ];                                    \
139         *p_pic++ = p_gray[ *p_y++ ];                                    \
140         *p_pic++ = p_gray[ *p_y++ ];                                    \
141         *p_pic++ = p_gray[ *p_y++ ];                                    \
142         *p_pic++ = p_gray[ *p_y++ ];                                    \
143         *p_pic++ = p_gray[ *p_y++ ];                                    \
144         *p_pic++ = p_gray[ *p_y++ ];                                    \
145         *p_pic++ = p_gray[ *p_y++ ];                                    \
146     }                                                                   \
147                                                                         \
148     /* Handle scale factor */                                           \
149     if( i_scale && ! (i_y % i_scale) )                                  \
150     {                                                                   \
151         if( i_scale < 0 )                                               \
152         {                                                               \
153             /* Copy previous line */                                    \
154             p_pic_src = p_pic - i_width;                                \
155             p_pic += i_pic_eol;                                         \
156             LINE_COPY                                                   \
157         }                                                               \
158         else                                                            \
159         {                                                               \
160             /* Ignore next line */                                      \
161             p_y += i_eol + i_width;                                     \
162             i_y++;                                                      \
163         }                                                               \
164     }                                                                   \
165                                                                         \
166     /* Skip until beginning of next line */                             \
167     p_pic += i_pic_eol;                                                 \
168     p_y   += i_eol;                                                     \
169 }
170
171 /*******************************************************************************
172  * CONVERT_YUV_RGB: color YUV convertion
173  *******************************************************************************
174  * Parameters
175  *      CHROMA                          420, 422 or 444
176  * Variables:
177  *      ...see vout_convert_t
178  *      i_x, i_y                        coordinates
179  *      i_uval, i_yval, i_vval          samples
180  *      p_pic_src                       same type as p_pic
181  *      i_chroma_width                  chroma width
182  *      i_chroma_eol                    chroma eol
183  *      p_red                           red translation table
184  *      p_green                         green translation table
185  *      p_blue                          blue translation table
186  *      i_crv, i_cgu, i_cgv, i_cbu      matrix coefficients
187  *******************************************************************************/
188 #define CONVERT_YUV_RGB( CHROMA )                                       \
189 for (i_y = 0; i_y < i_height ; i_y++)                                   \
190 {                                                                       \
191     for (i_x = 0; i_x < i_width; i_x += 2 )                             \
192     {                                                                   \
193         /* First sample (complete) */                                   \
194         i_yval = 76309 * *p_y++ - 1188177;                              \
195         i_uval = *p_u++ - 128;                                          \
196         i_vval = *p_v++ - 128;                                          \
197         *p_pic++ =                                                      \
198             p_red  [(i_yval+i_crv*i_vval)                >>16] |        \
199             p_green[(i_yval-i_cgu*i_uval-i_cgv*i_vval)   >>16] |        \
200             p_blue [(i_yval+i_cbu*i_uval)                >>16];         \
201         i_yval = 76309 * *p_y++ - 1188177;                              \
202         /* Second sample (partial) */                                   \
203         if( CHROMA == 444 )                                             \
204         {                                                               \
205             i_uval = *p_u++ - 128;                                      \
206             i_vval = *p_v++ - 128;                                      \
207         }                                                               \
208         *p_pic++ =                                                      \
209             p_red  [(i_yval+i_crv*i_vval)                >>16] |        \
210             p_green[(i_yval-i_cgu*i_uval-i_cgv*i_vval)   >>16] |        \
211             p_blue [(i_yval+i_cbu*i_uval)                >>16];         \
212     }                                                                   \
213                                                                         \
214     /* Handle scale factor and rewind in 4:2:0 */                       \
215     if( i_scale && ! (i_y % i_scale) )                                  \
216     {                                                                   \
217         if( i_scale < 0 )                                               \
218         {                                                               \
219             /* Copy previous line, rewind if required */                \
220             p_pic_src = p_pic - i_width;                                \
221             p_pic += i_pic_eol;                                         \
222             LINE_COPY                                                   \
223             if( (CHROMA == 420) && !(i_y & 0x1) )                       \
224             {                                                           \
225                 p_u -= i_chroma_width;                                  \
226                 p_v -= i_chroma_width;                                  \
227             }                                                           \
228             else                                                        \
229             {                                                           \
230                 p_u += i_chroma_eol;                                    \
231                 p_v += i_chroma_eol;                                    \
232             }                                                           \
233         }                                                               \
234         else                                                            \
235         {                                                               \
236             /* Ignore next line */                                      \
237             p_y += i_eol + i_width;                                     \
238             p_u += i_chroma_eol;                                        \
239             p_v += i_chroma_eol;                                        \
240             i_y++;                                                      \
241         }                                                               \
242     }                                                                   \
243     else if( (CHROMA == 420) && !(i_y & 0x1) )                          \
244     {                                                                   \
245         p_u -= i_chroma_width;                                          \
246         p_v -= i_chroma_width;                                          \
247     }                                                                   \
248     else                                                                \
249     {                                                                   \
250         p_u += i_chroma_eol;                                            \
251         p_v += i_chroma_eol;                                            \
252     }                                                                   \
253                                                                         \
254     /* Skip until beginning of next line */                             \
255     p_pic += i_pic_eol;                                                 \
256     p_y   += i_eol;                                                     \
257 }
258
259
260 /*******************************************************************************
261  * vout_InitTables: allocate and initialize translations tables
262  *******************************************************************************
263  * This function will allocate memory to store translation tables, depending
264  * of the screen depth.
265  *******************************************************************************/
266 int vout_InitTables( vout_thread_t *p_vout )
267 {
268     size_t      tables_size;                          /* tables size, in bytes */
269     
270     /* Computes tables size */
271     switch( p_vout->i_screen_depth )
272     {
273     case 15:
274     case 16:
275         tables_size = sizeof( u16 ) * 1024 * (p_vout->b_grayscale ? 1 : 3);
276         break;        
277     case 24:        
278     case 32:
279 #ifndef DEBUG
280     default:        
281 #endif
282         tables_size = sizeof( u32 ) * 1024 * (p_vout->b_grayscale ? 1 : 3);        
283         break;        
284 #ifdef DEBUG
285     default:
286         intf_DbgMsg("error: invalid screen depth %d\n", p_vout->i_screen_depth );
287         tables_size = 0;
288         break;        
289 #endif      
290     }
291     
292     /* Allocate memory */
293     p_vout->tables.p_base = malloc( tables_size );
294     if( p_vout->tables.p_base == NULL )
295     {
296         intf_ErrMsg("error: %s\n", strerror(ENOMEM));
297         return( 1 );                
298     }
299
300     /* Initialize tables */
301     SetTables( p_vout );    
302     return( 0 );    
303 }
304
305 /*******************************************************************************
306  * vout_ResetTables: re-initialize translations tables
307  *******************************************************************************
308  * This function will initialize the tables allocated by vout_CreateTables and
309  * set functions pointers.
310  *******************************************************************************/
311 int vout_ResetTables( vout_thread_t *p_vout )
312 {
313     vout_EndTables( p_vout );    
314     return( vout_InitTables( p_vout ) );    
315 }
316
317 /*******************************************************************************
318  * vout_EndTables: destroy translations tables
319  *******************************************************************************
320  * Free memory allocated by vout_CreateTables.
321  *******************************************************************************/
322 void vout_EndTables( vout_thread_t *p_vout )
323 {
324     free( p_vout->tables.p_base );
325 }
326
327 /* following functions are local */
328
329 /*******************************************************************************
330  * BinaryLog: computes the base 2 log of a binary value
331  *******************************************************************************
332  * This functions is used by MaskToShift during tables initialisation, to
333  * get a bit index from a binary value.
334  *******************************************************************************/
335 static int BinaryLog(u32 i)
336 {
337     int i_log;
338
339     i_log = 0;
340     if (i & 0xffff0000) 
341     {        
342         i_log = 16;
343     }    
344     if (i & 0xff00ff00) 
345     {        
346         i_log += 8;
347     }    
348     if (i & 0xf0f0f0f0) 
349     {        
350         i_log += 4;
351     }    
352     if (i & 0xcccccccc) 
353     {        
354         i_log += 2;
355     }    
356     if (i & 0xaaaaaaaa) 
357     {        
358         i_log++;
359     }    
360     if (i != ((u32)1 << i_log))
361     {        
362         intf_ErrMsg("internal error: binary log overflow\n");        
363     }    
364
365     return( i_log );
366 }
367
368 /*******************************************************************************
369  * MaskToShift: Transform a color mask into right and left shifts
370  *******************************************************************************
371  * This function is used during table initialisation. It can return a value
372  *******************************************************************************/
373 static void MaskToShift (int *pi_right, int *pi_left, u32 i_mask)
374 {
375     u32 i_low, i_high;                   /* lower hand higher bits of the mask */
376
377     /* Get bits */
378     i_low =  i_mask & (- i_mask);                     /* lower bit of the mask */
379     i_high = i_mask + i_low;                         /* higher bit of the mask */
380
381     /* Transform bits into an index */
382     i_low =  BinaryLog (i_low);
383     i_high = BinaryLog (i_high);
384
385     /* Update pointers and return */
386     *pi_left =   i_low;
387     *pi_right = (8 - i_high + i_low);
388 }
389
390 /*******************************************************************************
391  * SetGammaTable: return intensity table transformed by gamma curve.
392  *******************************************************************************
393  * pi_table is a table of 256 entries from 0 to 255.
394  *******************************************************************************/
395 static void SetGammaTable( int *pi_table, double f_gamma )
396 {
397     int         i_y;                                         /* base intensity */
398
399     /* Use exp(gamma) instead of gamma */
400     f_gamma = exp(f_gamma );
401
402     /* Build gamma table */
403     for( i_y = 0; i_y < 256; i_y++ )
404     {
405         pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
406     }
407  }
408
409 /*******************************************************************************
410  * SetTables: compute tables and set function pointers
411 + *******************************************************************************/
412 static void SetTables( vout_thread_t *p_vout )
413 {
414     int         pi_gamma[256];                                  /* gamma table */    
415     int         i_index;                                    /* index in tables */
416     int         i_red_right, i_red_left;                         /* red shifts */
417     int         i_green_right, i_green_left;                   /* green shifts */
418     int         i_blue_right, i_blue_left;                      /* blue shifts */
419
420     /* Build gamma table */    
421     SetGammaTable( pi_gamma, p_vout->f_gamma );
422     
423     /*          
424      * Set color masks and shifts
425      */
426     switch( p_vout->i_screen_depth )
427     {
428     case 15:
429         MaskToShift( &i_red_right,   &i_red_left,   0x7c00 );
430         MaskToShift( &i_green_right, &i_green_left, 0x03e0 );
431         MaskToShift( &i_blue_right,  &i_blue_left,  0x001f );        
432         break;        
433     case 16:
434         MaskToShift( &i_red_right,   &i_red_left,   0xf800 );
435         MaskToShift( &i_green_right, &i_green_left, 0x07e0 );
436         MaskToShift( &i_blue_right,  &i_blue_left,  0x001f );
437         break;        
438     case 24:
439     case 32:        
440         MaskToShift( &i_red_right,   &i_red_left,   0x00ff0000 );
441         MaskToShift( &i_green_right, &i_green_left, 0x0000ff00 );
442         MaskToShift( &i_blue_right,  &i_blue_left,  0x000000ff );
443         break;
444 #ifdef DEBUG
445     default:
446         intf_DbgMsg("error: invalid screen depth %d\n", p_vout->i_screen_depth );
447         break;        
448 #endif      
449     }
450
451     /*
452      * Set pointers and build YUV tables
453      */        
454     if( p_vout->b_grayscale )
455     {
456         /* Grayscale: build gray table */
457         switch( p_vout->i_screen_depth )
458         {
459         case 15:
460         case 16:         
461             p_vout->tables.yuv.gray16.p_gray =  (u16 *)p_vout->tables.p_base + 384;
462             for( i_index = -384; i_index < 640; i_index++) 
463             {
464                 p_vout->tables.yuv.gray16.p_gray[ i_index ] = 
465                     ((pi_gamma[CLIP_BYTE( i_index )] >> i_red_right)   << i_red_left)   |
466                     ((pi_gamma[CLIP_BYTE( i_index )] >> i_green_right) << i_green_left) |
467                     ((pi_gamma[CLIP_BYTE( i_index )] >> i_blue_right)  << i_blue_left);                
468             }
469             break;        
470         case 24:
471         case 32:        
472             p_vout->tables.yuv.gray32.p_gray =  (u32 *)p_vout->tables.p_base + 384;
473             for( i_index = -384; i_index < 640; i_index++) 
474             {
475                 p_vout->tables.yuv.gray32.p_gray[ i_index ] = 
476                     ((pi_gamma[CLIP_BYTE( i_index )] >> i_red_right)   << i_red_left)   |
477                     ((pi_gamma[CLIP_BYTE( i_index )] >> i_green_right) << i_green_left) |
478                     ((pi_gamma[CLIP_BYTE( i_index )] >> i_blue_right)  << i_blue_left);                
479             }        
480             break;        
481         }
482     }
483     else
484     {
485         /* Color: build red, green and blue tables */
486         switch( p_vout->i_screen_depth )
487         {
488         case 15:
489         case 16:            
490             p_vout->tables.yuv.rgb16.p_red =    (u16 *)p_vout->tables.p_base +          384;
491             p_vout->tables.yuv.rgb16.p_green =  (u16 *)p_vout->tables.p_base +   1024 + 384;
492             p_vout->tables.yuv.rgb16.p_blue =   (u16 *)p_vout->tables.p_base + 2*1024 + 384;
493             for( i_index = -384; i_index < 640; i_index++) 
494             {
495                 p_vout->tables.yuv.rgb16.p_red[i_index] =   (pi_gamma[CLIP_BYTE(i_index)]>>i_red_right)<<i_red_left;
496                 p_vout->tables.yuv.rgb16.p_green[i_index] = (pi_gamma[CLIP_BYTE(i_index)]>>i_green_right)<<i_green_left;
497                 p_vout->tables.yuv.rgb16.p_blue[i_index] =  (pi_gamma[CLIP_BYTE(i_index)]>>i_blue_right)<<i_blue_left;
498             }
499             break;        
500         case 24:
501         case 32:
502             p_vout->tables.yuv.rgb32.p_red =    (u32 *)p_vout->tables.p_base +          384;
503             p_vout->tables.yuv.rgb32.p_green =  (u32 *)p_vout->tables.p_base +   1024 + 384;
504             p_vout->tables.yuv.rgb32.p_blue =   (u32 *)p_vout->tables.p_base + 2*1024 + 384;
505             for( i_index = -384; i_index < 640; i_index++) 
506             {
507                 p_vout->tables.yuv.rgb32.p_red[i_index] =   (pi_gamma[CLIP_BYTE(i_index)]>>i_red_right)<<i_red_left;
508                 p_vout->tables.yuv.rgb32.p_green[i_index] = (pi_gamma[CLIP_BYTE(i_index)]>>i_green_right)<<i_green_left;
509                 p_vout->tables.yuv.rgb32.p_blue[i_index] =  (pi_gamma[CLIP_BYTE(i_index)]>>i_blue_right)<<i_blue_left;
510             }
511             break;        
512         }
513     }    
514     
515     /*
516      * Set functions pointers
517      */
518     if( p_vout->b_grayscale )
519     {
520         /* Grayscale */
521         switch( p_vout->i_screen_depth )
522         {
523         case 15:
524         case 16:  
525             p_vout->p_ConvertYUV420 = (vout_convert_t *) ConvertY4Gray16;        
526             p_vout->p_ConvertYUV420 = (vout_convert_t *) ConvertY4Gray16;        
527             p_vout->p_ConvertYUV420 = (vout_convert_t *) ConvertY4Gray16;        
528             break;        
529         case 24:
530             p_vout->p_ConvertYUV420 = (vout_convert_t *) ConvertY4Gray24;        
531             p_vout->p_ConvertYUV420 = (vout_convert_t *) ConvertY4Gray24;        
532             p_vout->p_ConvertYUV420 = (vout_convert_t *) ConvertY4Gray24;        
533             break;        
534         case 32:        
535             p_vout->p_ConvertYUV420 = (vout_convert_t *) ConvertY4Gray32;        
536             p_vout->p_ConvertYUV420 = (vout_convert_t *) ConvertY4Gray32;        
537             p_vout->p_ConvertYUV420 = (vout_convert_t *) ConvertY4Gray32;        
538             break;        
539         }        
540     }
541     else
542     {
543         /* Color */
544         switch( p_vout->i_screen_depth )
545         {
546         case 15:
547         case 16:  
548             p_vout->p_ConvertYUV420 =   (vout_convert_t *) ConvertYUV420RGB16;        
549             p_vout->p_ConvertYUV422 =   (vout_convert_t *) ConvertYUV422RGB16;        
550             p_vout->p_ConvertYUV444 =   (vout_convert_t *) ConvertYUV444RGB16;        
551             break;        
552         case 24:
553             p_vout->p_ConvertYUV420 =   (vout_convert_t *) ConvertYUV420RGB24;        
554             p_vout->p_ConvertYUV422 =   (vout_convert_t *) ConvertYUV422RGB24;        
555             p_vout->p_ConvertYUV444 =   (vout_convert_t *) ConvertYUV444RGB24;        
556             break;        
557         case 32:        
558             p_vout->p_ConvertYUV420 =   (vout_convert_t *) ConvertYUV420RGB32;        
559             p_vout->p_ConvertYUV422 =   (vout_convert_t *) ConvertYUV422RGB32;        
560             p_vout->p_ConvertYUV444 =   (vout_convert_t *) ConvertYUV444RGB32;        
561             break;        
562         }
563     }        
564 }
565
566 /*******************************************************************************
567  * ConvertY4Gray16: grayscale YUV 4:x:x to RGB 15 or 16 bpp
568  *******************************************************************************/
569 static void ConvertY4Gray16( p_vout_thread_t p_vout, u16 *p_pic,
570                              yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
571                              int i_width, int i_height, int i_eol, int i_pic_eol,
572                              int i_scale, int i_matrix_coefficients )
573 {
574     u16 *       p_pic_src;                   /* source pointer in case of copy */
575     u16 *       p_gray;                                          /* gray table */    
576     int         i_x, i_y;                               /* picture coordinates */
577     
578     p_gray = p_vout->tables.yuv.gray16.p_gray;
579     CONVERT_YUV_GRAY
580 }
581
582 /*******************************************************************************
583  * ConvertY4Gray24: grayscale YUV 4:x:x to RGB 24 bpp
584  *******************************************************************************/
585 static void ConvertY4Gray24( p_vout_thread_t p_vout, void *p_pic,
586                              yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
587                              int i_width, int i_height, int i_eol, int i_pic_eol,
588                              int i_scale, int i_matrix_coefficients )
589 {
590     //??
591 }
592
593 /*******************************************************************************
594  * ConvertY4Gray32: grayscale YUV 4:x:x to RGB 32 bpp
595  *******************************************************************************/
596 static void ConvertY4Gray32( p_vout_thread_t p_vout, u32 *p_pic,
597                              yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
598                              int i_width, int i_height, int i_eol, int i_pic_eol,
599                              int i_scale, int i_matrix_coefficients )
600 {
601     u32 *       p_pic_src;                   /* source pointer in case of copy */
602     u32 *       p_gray;                                          /* gray table */    
603     int         i_x, i_y;                               /* picture coordinates */
604     
605     p_gray = p_vout->tables.yuv.gray32.p_gray;
606     CONVERT_YUV_GRAY
607 }
608
609 /*******************************************************************************
610  * ConvertYUV420RGB16: color YUV 4:2:0 to RGB 15 or 16 bpp
611  *******************************************************************************/
612 static void ConvertYUV420RGB16( p_vout_thread_t p_vout, u16 *p_pic,
613                                 yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
614                                 int i_width, int i_height, int i_eol, int i_pic_eol,
615                                 int i_scale, int i_matrix_coefficients )
616 {
617 #ifdef HAVE_MMX
618     int         i_chroma_width, i_chroma_eol;      /* width and eol for chroma */
619
620     i_chroma_width =    i_width / 2;
621     i_chroma_eol =      i_eol / 2;
622     ConvertYUV420RGB16MMX( p_y, p_u, p_v, i_width, i_height, 
623                            (i_width + i_eol) * sizeof( yuv_data_t ), 
624                            (i_chroma_width + i_chroma_eol) * sizeof( yuv_data_t),
625                            i_scale, (u8 *)p_pic, 0, 0, (i_width + i_pic_eol) * sizeof( u16 ),
626                            p_vout->i_screen_depth == 15 );    
627 #else
628     u16 *       p_pic_src;                   /* source pointer in case of copy */
629     u16 *       p_red;                                            /* red table */
630     u16 *       p_green;                                        /* green table */
631     u16 *       p_blue;                                          /* blue table */
632     int         i_uval, i_yval, i_vval;                             /* samples */   
633     int         i_x, i_y;                               /* picture coordinates */
634     int         i_chroma_width, i_chroma_eol;      /* width and eol for chroma */
635     int         i_crv, i_cbu, i_cgu, i_cgv;     /* transformation coefficients */
636
637     i_crv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][0];
638     i_cbu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][1];
639     i_cgu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][2];
640     i_cgv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][3];
641     p_red   = p_vout->tables.yuv.rgb16.p_red;
642     p_green = p_vout->tables.yuv.rgb16.p_green;
643     p_blue  = p_vout->tables.yuv.rgb16.p_blue;
644     i_chroma_width =    i_width / 2;
645     i_chroma_eol =      i_eol / 2;
646     CONVERT_YUV_RGB( 420 )
647 #endif
648 }
649
650 /*******************************************************************************
651  * ConvertYUV422RGB16: color YUV 4:2:2 to RGB 15 or 16 bpp
652  *******************************************************************************/
653 static void ConvertYUV422RGB16( p_vout_thread_t p_vout, u16 *p_pic,
654                                 yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
655                                 int i_width, int i_height, int i_eol, int i_pic_eol,
656                                 int i_scale, int i_matrix_coefficients )
657 {
658     u16 *       p_pic_src;                   /* source pointer in case of copy */
659     u16 *       p_red;                                            /* red table */
660     u16 *       p_green;                                        /* green table */
661     u16 *       p_blue;                                          /* blue table */
662     int         i_uval, i_yval, i_vval;                             /* samples */   
663     int         i_x, i_y;                               /* picture coordinates */
664     int         i_chroma_width, i_chroma_eol;      /* width and eol for chroma */
665     int         i_crv, i_cbu, i_cgu, i_cgv;     /* transformation coefficients */
666
667     i_crv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][0];
668     i_cbu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][1];
669     i_cgu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][2];
670     i_cgv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][3];
671     p_red   = p_vout->tables.yuv.rgb16.p_red;
672     p_green = p_vout->tables.yuv.rgb16.p_green;
673     p_blue  = p_vout->tables.yuv.rgb16.p_blue;
674     i_chroma_width =    i_width / 2;
675     i_chroma_eol =      i_eol / 2;
676     CONVERT_YUV_RGB( 422 )
677 }
678
679 /*******************************************************************************
680  * ConvertYUV444RGB16: color YUV 4:4:4 to RGB 15 or 16 bpp
681  *******************************************************************************/
682 static void ConvertYUV444RGB16( p_vout_thread_t p_vout, u16 *p_pic,
683                                 yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
684                                 int i_width, int i_height, int i_eol, int i_pic_eol,
685                                 int i_scale, int i_matrix_coefficients )
686 {
687     u16 *       p_pic_src;                   /* source pointer in case of copy */
688     u16 *       p_red;                                            /* red table */
689     u16 *       p_green;                                        /* green table */
690     u16 *       p_blue;                                          /* blue table */
691     int         i_uval, i_yval, i_vval;                             /* samples */   
692     int         i_x, i_y;                               /* picture coordinates */
693     int         i_chroma_width, i_chroma_eol;      /* width and eol for chroma */
694     int         i_crv, i_cbu, i_cgu, i_cgv;     /* transformation coefficients */
695
696     i_crv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][0];
697     i_cbu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][1];
698     i_cgu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][2];
699     i_cgv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][3];
700     p_red   = p_vout->tables.yuv.rgb16.p_red;
701     p_green = p_vout->tables.yuv.rgb16.p_green;
702     p_blue  = p_vout->tables.yuv.rgb16.p_blue;
703     i_chroma_width =    i_width;
704     i_chroma_eol =      i_eol;
705     CONVERT_YUV_RGB( 444 )
706 }
707
708 /*******************************************************************************
709  * ConvertYUV420RGB24: color YUV 4:2:0 to RGB 24 bpp
710  *******************************************************************************/
711 static void ConvertYUV420RGB24( p_vout_thread_t p_vout, void *p_pic,
712                                 yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
713                                 int i_width, int i_height, int i_eol, int i_pic_eol,
714                                 int i_scale, int i_matrix_coefficients )
715 {
716     //???
717 }
718
719 /*******************************************************************************
720  * ConvertYUV422RGB24: color YUV 4:2:2 to RGB 24 bpp
721  *******************************************************************************/
722 static void ConvertYUV422RGB24( p_vout_thread_t p_vout, void *p_pic,
723                                 yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
724                                 int i_width, int i_height, int i_eol, int i_pic_eol,
725                                 int i_scale, int i_matrix_coefficients )
726 {
727     //???
728 }
729
730 /*******************************************************************************
731  * ConvertYUV444RGB24: color YUV 4:4:4 to RGB 24 bpp
732  *******************************************************************************/
733 static void ConvertYUV444RGB24( p_vout_thread_t p_vout, void *p_pic,
734                                 yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
735                                 int i_width, int i_height, int i_eol, int i_pic_eol,
736                                 int i_scale, int i_matrix_coefficients )
737 {    
738     //???
739 }
740
741 /*******************************************************************************
742  * ConvertYUV420RGB32: color YUV 4:2:0 to RGB 32 bpp
743  *******************************************************************************/
744 static void ConvertYUV420RGB32( p_vout_thread_t p_vout, u32 *p_pic,
745                                 yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
746                                 int i_width, int i_height, int i_eol, int i_pic_eol,
747                                 int i_scale, int i_matrix_coefficients )
748 {
749     u32 *       p_pic_src;                   /* source pointer in case of copy */
750     u32 *       p_red;                                            /* red table */
751     u32 *       p_green;                                        /* green table */
752     u32 *       p_blue;                                          /* blue table */
753     int         i_uval, i_yval, i_vval;                             /* samples */   
754     int         i_x, i_y;                               /* picture coordinates */
755     int         i_chroma_width, i_chroma_eol;      /* width and eol for chroma */
756     int         i_crv, i_cbu, i_cgu, i_cgv;     /* transformation coefficients */
757
758     i_crv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][0];
759     i_cbu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][1];
760     i_cgu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][2];
761     i_cgv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][3];
762     p_red   = p_vout->tables.yuv.rgb32.p_red;
763     p_green = p_vout->tables.yuv.rgb32.p_green;
764     p_blue  = p_vout->tables.yuv.rgb32.p_blue;
765     i_chroma_width =    i_width / 2;
766     i_chroma_eol =      i_eol / 2;
767     CONVERT_YUV_RGB( 420 )
768 }
769
770 /*******************************************************************************
771  * ConvertYUV422RGB32: color YUV 4:2:2 to RGB 32 bpp
772  *******************************************************************************/
773 static void ConvertYUV422RGB32( p_vout_thread_t p_vout, u32 *p_pic,
774                                 yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
775                                 int i_width, int i_height, int i_eol, int i_pic_eol,
776                                 int i_scale, int i_matrix_coefficients )
777 {
778     u32 *       p_pic_src;                   /* source pointer in case of copy */
779     u32 *       p_red;                                            /* red table */
780     u32 *       p_green;                                        /* green table */
781     u32 *       p_blue;                                          /* blue table */
782     int         i_uval, i_yval, i_vval;                             /* samples */   
783     int         i_x, i_y;                               /* picture coordinates */
784     int         i_chroma_width, i_chroma_eol;      /* width and eol for chroma */
785     int         i_crv, i_cbu, i_cgu, i_cgv;     /* transformation coefficients */
786
787     i_crv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][0];
788     i_cbu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][1];
789     i_cgu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][2];
790     i_cgv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][3];
791     p_red   = p_vout->tables.yuv.rgb32.p_red;
792     p_green = p_vout->tables.yuv.rgb32.p_green;
793     p_blue  = p_vout->tables.yuv.rgb32.p_blue;
794     i_chroma_width =    i_width / 2;
795     i_chroma_eol =      i_eol / 2;
796     CONVERT_YUV_RGB( 422 )
797 }
798
799 /*******************************************************************************
800  * ConvertYUV444RGB32: color YUV 4:4:4 to RGB 32 bpp
801  *******************************************************************************/
802 static void ConvertYUV444RGB32( p_vout_thread_t p_vout, u32 *p_pic,
803                                 yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
804                                 int i_width, int i_height, int i_eol, int i_pic_eol,
805                                 int i_scale, int i_matrix_coefficients )
806 {
807     u32 *       p_pic_src;                   /* source pointer in case of copy */
808     u32 *       p_red;                                            /* red table */
809     u32 *       p_green;                                        /* green table */
810     u32 *       p_blue;                                          /* blue table */
811     int         i_uval, i_yval, i_vval;                             /* samples */   
812     int         i_x, i_y;                               /* picture coordinates */
813     int         i_chroma_width, i_chroma_eol;      /* width and eol for chroma */
814     int         i_crv, i_cbu, i_cgu, i_cgv;     /* transformation coefficients */
815
816     i_crv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][0];
817     i_cbu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][1];
818     i_cgu = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][2];
819     i_cgv = MATRIX_COEFFICIENTS_TABLE[i_matrix_coefficients][3];
820     p_red   = p_vout->tables.yuv.rgb32.p_red;
821     p_green = p_vout->tables.yuv.rgb32.p_green;
822     p_blue  = p_vout->tables.yuv.rgb32.p_blue;
823     i_chroma_width =    i_width;
824     i_chroma_eol =      i_eol;
825     CONVERT_YUV_RGB( 444 )
826 }
827
828 //-------------------- walken code follow ---------------------------------------
829
830 /*
831  * YUV to RGB routines.
832  *
833  * these routines calculate r, g and b values from each pixel's y, u and v.
834  * these r, g an b values are then passed thru a table lookup to take the
835  * gamma curve into account and find the corresponding pixel value.
836  *
837  * the table must store more than 3*256 values because of the possibility
838  * of overflow in the yuv->rgb calculation. actually the calculated r,g,b
839  * values are in the following intervals :
840  * -176 to 255+176 for red
841  * -133 to 255+133 for green
842  * -222 to 255+222 for blue
843  *
844  * If the input y,u,v values are right, the r,g,b results are not expected
845  * to move out of the 0 to 255 interval but who knows what will happen in
846  * real use...
847  *
848  * the red, green and blue conversion tables are stored in a single 1935-entry
849  * array. The respective positions of each component in the array have been
850  * calculated to minimize the cache interactions of the 3 tables.
851  */
852
853 int rgbTable16 (short table [1935],
854                        int redMask, int greenMask, int blueMask,
855                        unsigned char gamma[256])
856 {
857     int redRight;
858     int redLeft;
859     int greenRight;
860     int greenLeft;
861     int blueRight;
862     int blueLeft;
863     short * redTable;
864     short * greenTable;
865     short * blueTable;
866     int i;
867     int y;
868
869     MaskToShift (&redRight, &redLeft, redMask);    
870     MaskToShift (&greenRight, &greenLeft, greenMask);    
871     MaskToShift (&blueRight, &blueLeft, blueMask);
872
873     /*
874      * green blue red +- 2 just to be sure
875      * green = 0-525 [151-370]
876      * blue = 594-1297 [834-1053] <834-29>
877      * red = 1323-1934 [1517-1736] <493-712>
878      */
879
880     redTable = table + 1501;
881     greenTable = table + 135;
882     blueTable = table + 818;
883
884     for (i = 0; i < 178; i++) {
885         redTable[i-178] = 0;
886         redTable[i+256] = redMask;
887     }
888     for (i = 0; i < 135; i++) {
889         greenTable[i-135] = 0;
890         greenTable[i+256] = greenMask;
891     }
892     for (i = 0; i < 224; i++) {
893         blueTable[i-224] = 0;
894         blueTable[i+256] = blueMask;
895     }
896
897     for (i = 0; i < 256; i++) {
898         y = gamma[i];
899         redTable[i] = ((y >> redRight) << redLeft);
900         greenTable[i] = ((y >> greenRight) << greenLeft);
901         blueTable[i] = ((y >> blueRight) << blueLeft);
902     }
903
904     return 0;
905 }
906
907 static int rgbTable32 (int table [1935],
908                        int redMask, int greenMask, int blueMask,
909                        unsigned char gamma[256])
910 {
911     int redRight;
912     int redLeft;
913     int greenRight;
914     int greenLeft;
915     int blueRight;
916     int blueLeft;
917     int * redTable;
918     int * greenTable;
919     int * blueTable;
920     int i;
921     int y;
922
923     MaskToShift (&redRight, &redLeft, redMask);
924     MaskToShift (&greenRight, &greenLeft, greenMask);
925     MaskToShift (&blueRight, &blueLeft, blueMask);
926     
927
928     /*
929      * green blue red +- 2 just to be sure
930      * green = 0-525 [151-370]
931      * blue = 594-1297 [834-1053] <834-29>
932      * red = 1323-1934 [1517-1736] <493-712>
933      */
934
935     redTable = table + 1501;
936     greenTable = table + 135;
937     blueTable = table + 818;
938
939     for (i = 0; i < 178; i++) {
940         redTable[i-178] = 0;
941         redTable[i+256] = redMask;
942     }
943     for (i = 0; i < 135; i++) {
944         greenTable[i-135] = 0;
945         greenTable[i+256] = greenMask;
946     }
947     for (i = 0; i < 224; i++) {
948         blueTable[i-224] = 0;
949         blueTable[i+256] = blueMask;
950     }
951
952     for (i = 0; i < 256; i++) {
953         y = gamma[i];
954         redTable[i] = ((y >> redRight) << redLeft);
955         greenTable[i] = ((y >> greenRight) << greenLeft);
956         blueTable[i] = ((y >> blueRight) << blueLeft);
957     }
958
959     return 0;
960 }
961
962 #define SHIFT 20
963 #define U_GREEN_COEF ((int)(-0.391 * (1<<SHIFT) / 1.164))
964 #define U_BLUE_COEF ((int)(2.018 * (1<<SHIFT) / 1.164))
965 #define V_RED_COEF ((int)(1.596 * (1<<SHIFT) / 1.164))
966 #define V_GREEN_COEF ((int)(-0.813 * (1<<SHIFT) / 1.164))
967
968  void yuvToRgb16 (unsigned char * Y,
969                         unsigned char * U, unsigned char * V,
970                         short * dest, short table[1935], int width)
971 {
972     int i;
973     int u;
974     int v;
975     int uvRed;
976     int uvGreen;
977     int uvBlue;
978     short * tableY;
979
980     i = width >> 4;
981     while (i--) {
982         u = *(U++);
983         v = *(V++);
984         uvRed = (V_RED_COEF*v) >> SHIFT;
985         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
986         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
987
988         tableY = table + *(Y++);
989         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
990                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
991                             uvGreen] |
992                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
993
994         tableY = table + *(Y++);
995         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
996                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
997                             uvGreen] |
998                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
999
1000         u = *(U++);
1001         v = *(V++);
1002         uvRed = (V_RED_COEF*v) >> SHIFT;
1003         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1004         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1005
1006         tableY = table + *(Y++);
1007         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1008                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1009                             uvGreen] |
1010                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1011
1012         tableY = table + *(Y++);
1013         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1014                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1015                             uvGreen] |
1016                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1017
1018         u = *(U++);
1019         v = *(V++);
1020         uvRed = (V_RED_COEF*v) >> SHIFT;
1021         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1022         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1023
1024         tableY = table + *(Y++);
1025         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1026                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1027                             uvGreen] |
1028                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1029
1030         tableY = table + *(Y++);
1031         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1032                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1033                             uvGreen] |
1034                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1035
1036         u = *(U++);
1037         v = *(V++);
1038         uvRed = (V_RED_COEF*v) >> SHIFT;
1039         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1040         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1041
1042         tableY = table + *(Y++);
1043         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1044                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1045                             uvGreen] |
1046                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1047
1048         tableY = table + *(Y++);
1049         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1050                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1051                             uvGreen] |
1052                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1053
1054         u = *(U++);
1055         v = *(V++);
1056         uvRed = (V_RED_COEF*v) >> SHIFT;
1057         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1058         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1059
1060         tableY = table + *(Y++);
1061         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1062                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1063                             uvGreen] |
1064                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1065
1066         tableY = table + *(Y++);
1067         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1068                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1069                             uvGreen] |
1070                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1071
1072         u = *(U++);
1073         v = *(V++);
1074         uvRed = (V_RED_COEF*v) >> SHIFT;
1075         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1076         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1077
1078         tableY = table + *(Y++);
1079         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1080                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1081                             uvGreen] |
1082                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1083
1084         tableY = table + *(Y++);
1085         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1086                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1087                             uvGreen] |
1088                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1089
1090         u = *(U++);
1091         v = *(V++);
1092         uvRed = (V_RED_COEF*v) >> SHIFT;
1093         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1094         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1095
1096         tableY = table + *(Y++);
1097         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1098                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1099                             uvGreen] |
1100                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1101
1102         tableY = table + *(Y++);
1103         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1104                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1105                             uvGreen] |
1106                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1107
1108         u = *(U++);
1109         v = *(V++);
1110         uvRed = (V_RED_COEF*v) >> SHIFT;
1111         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1112         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1113
1114         tableY = table + *(Y++);
1115         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1116                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1117                             uvGreen] |
1118                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1119
1120         tableY = table + *(Y++);
1121         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1122                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1123                             uvGreen] |
1124                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1125     }
1126
1127     i = (width & 15) >> 1;
1128     while (i--) {
1129         u = *(U++);
1130         v = *(V++);
1131         uvRed = (V_RED_COEF*v) >> SHIFT;
1132         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1133         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1134
1135         tableY = table + *(Y++);
1136         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1137                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1138                             uvGreen] |
1139                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1140
1141         tableY = table + *(Y++);
1142         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1143                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1144                             uvGreen] |
1145                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1146     }
1147
1148     if (width & 1) {
1149         u = *(U++);
1150         v = *(V++);
1151         uvRed = (V_RED_COEF*v) >> SHIFT;
1152         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1153         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1154
1155         tableY = table + *(Y++);
1156         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1157                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1158                             uvGreen] |
1159                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1160     }
1161 }
1162
1163 static void yuvToRgb24 (unsigned char * Y,
1164                         unsigned char * U, unsigned char * V,
1165                         char * dest, int table[1935], int width)
1166 {
1167     int i;
1168     int u;
1169     int v;
1170     int uvRed;
1171     int uvGreen;
1172     int uvBlue;
1173     int * tableY;
1174     int tmp24;
1175
1176     i = width >> 3;
1177     while (i--) {
1178         u = *(U++);
1179         v = *(V++);
1180         uvRed = (V_RED_COEF*v) >> SHIFT;
1181         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1182         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1183
1184         tableY = table + *(Y++);
1185         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1186                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1187                         uvGreen] |
1188                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1189         *(dest++) = tmp24;
1190         *(dest++) = tmp24 >> 8;
1191         *(dest++) = tmp24 >> 16;
1192
1193         tableY = table + *(Y++);
1194         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1195                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1196                         uvGreen] |
1197                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1198         *(dest++) = tmp24;
1199         *(dest++) = tmp24 >> 8;
1200         *(dest++) = tmp24 >> 16;
1201
1202         u = *(U++);
1203         v = *(V++);
1204         uvRed = (V_RED_COEF*v) >> SHIFT;
1205         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1206         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1207
1208         tableY = table + *(Y++);
1209         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1210                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1211                         uvGreen] |
1212                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1213         *(dest++) = tmp24;
1214         *(dest++) = tmp24 >> 8;
1215         *(dest++) = tmp24 >> 16;
1216
1217         tableY = table + *(Y++);
1218         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1219                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1220                         uvGreen] |
1221                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1222         *(dest++) = tmp24;
1223         *(dest++) = tmp24 >> 8;
1224         *(dest++) = tmp24 >> 16;
1225
1226         u = *(U++);
1227         v = *(V++);
1228         uvRed = (V_RED_COEF*v) >> SHIFT;
1229         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1230         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1231
1232         tableY = table + *(Y++);
1233         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1234                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1235                         uvGreen] |
1236                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1237         *(dest++) = tmp24;
1238         *(dest++) = tmp24 >> 8;
1239         *(dest++) = tmp24 >> 16;
1240
1241         tableY = table + *(Y++);
1242         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1243                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1244                         uvGreen] |
1245                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1246         *(dest++) = tmp24;
1247         *(dest++) = tmp24 >> 8;
1248         *(dest++) = tmp24 >> 16;
1249
1250         u = *(U++);
1251         v = *(V++);
1252         uvRed = (V_RED_COEF*v) >> SHIFT;
1253         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1254         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1255
1256         tableY = table + *(Y++);
1257         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1258                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1259                         uvGreen] |
1260                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1261         *(dest++) = tmp24;
1262         *(dest++) = tmp24 >> 8;
1263         *(dest++) = tmp24 >> 16;
1264
1265         tableY = table + *(Y++);
1266         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1267                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1268                         uvGreen] |
1269                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1270         *(dest++) = tmp24;
1271         *(dest++) = tmp24 >> 8;
1272         *(dest++) = tmp24 >> 16;
1273     }
1274
1275     i = (width & 7) >> 1;
1276     while (i--) {
1277         u = *(U++);
1278         v = *(V++);
1279         uvRed = (V_RED_COEF*v) >> SHIFT;
1280         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1281         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1282
1283         tableY = table + *(Y++);
1284         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1285                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1286                         uvGreen] |
1287                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1288         *(dest++) = tmp24;
1289         *(dest++) = tmp24 >> 8;
1290         *(dest++) = tmp24 >> 16;
1291
1292         tableY = table + *(Y++);
1293         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1294                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1295                         uvGreen] |
1296                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1297         *(dest++) = tmp24;
1298         *(dest++) = tmp24 >> 8;
1299         *(dest++) = tmp24 >> 16;
1300     }
1301
1302     if (width & 1) {
1303         u = *(U++);
1304         v = *(V++);
1305         uvRed = (V_RED_COEF*v) >> SHIFT;
1306         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1307         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1308
1309         tableY = table + *(Y++);
1310         tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1311                  tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1312                         uvGreen] |
1313                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1314         *(dest++) = tmp24;
1315         *(dest++) = tmp24 >> 8;
1316         *(dest++) = tmp24 >> 16;
1317     }
1318 }
1319
1320 static void yuvToRgb32 (unsigned char * Y,
1321                         unsigned char * U, unsigned char * V,
1322                         int * dest, int table[1935], int width)
1323 {
1324     int i;
1325     int u;
1326     int v;
1327     int uvRed;
1328     int uvGreen;
1329     int uvBlue;
1330     int * tableY;
1331
1332     i = width >> 4;
1333     while (i--) {
1334         u = *(U++);
1335         v = *(V++);
1336         uvRed = (V_RED_COEF*v) >> SHIFT;
1337         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1338         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1339
1340         tableY = table + *(Y++);
1341         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1342                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1343                             uvGreen] |
1344                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1345
1346         tableY = table + *(Y++);
1347         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1348                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1349                             uvGreen] |
1350                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1351
1352         u = *(U++);
1353         v = *(V++);
1354         uvRed = (V_RED_COEF*v) >> SHIFT;
1355         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1356         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1357
1358         tableY = table + *(Y++);
1359         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1360                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1361                             uvGreen] |
1362                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1363
1364         tableY = table + *(Y++);
1365         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1366                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1367                             uvGreen] |
1368                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1369
1370         u = *(U++);
1371         v = *(V++);
1372         uvRed = (V_RED_COEF*v) >> SHIFT;
1373         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1374         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1375
1376         tableY = table + *(Y++);
1377         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1378                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1379                             uvGreen] |
1380                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1381
1382         tableY = table + *(Y++);
1383         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1384                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1385                             uvGreen] |
1386                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1387
1388         u = *(U++);
1389         v = *(V++);
1390         uvRed = (V_RED_COEF*v) >> SHIFT;
1391         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1392         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1393
1394         tableY = table + *(Y++);
1395         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1396                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1397                             uvGreen] |
1398                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1399
1400         tableY = table + *(Y++);
1401         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1402                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1403                             uvGreen] |
1404                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1405
1406         u = *(U++);
1407         v = *(V++);
1408         uvRed = (V_RED_COEF*v) >> SHIFT;
1409         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1410         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1411
1412         tableY = table + *(Y++);
1413         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1414                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1415                             uvGreen] |
1416                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1417
1418         tableY = table + *(Y++);
1419         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1420                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1421                             uvGreen] |
1422                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1423
1424         u = *(U++);
1425         v = *(V++);
1426         uvRed = (V_RED_COEF*v) >> SHIFT;
1427         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1428         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1429
1430         tableY = table + *(Y++);
1431         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1432                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1433                             uvGreen] |
1434                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1435
1436         tableY = table + *(Y++);
1437         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1438                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1439                             uvGreen] |
1440                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1441
1442         u = *(U++);
1443         v = *(V++);
1444         uvRed = (V_RED_COEF*v) >> SHIFT;
1445         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1446         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1447
1448         tableY = table + *(Y++);
1449         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1450                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1451                             uvGreen] |
1452                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1453
1454         tableY = table + *(Y++);
1455         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1456                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1457                             uvGreen] |
1458                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1459
1460         u = *(U++);
1461         v = *(V++);
1462         uvRed = (V_RED_COEF*v) >> SHIFT;
1463         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1464         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1465
1466         tableY = table + *(Y++);
1467         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1468                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1469                             uvGreen] |
1470                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1471
1472         tableY = table + *(Y++);
1473         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1474                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1475                             uvGreen] |
1476                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1477     }
1478
1479     i = (width & 15) >> 1;
1480     while (i--) {
1481         u = *(U++);
1482         v = *(V++);
1483         uvRed = (V_RED_COEF*v) >> SHIFT;
1484         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1485         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1486
1487         tableY = table + *(Y++);
1488         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1489                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1490                             uvGreen] |
1491                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1492
1493         tableY = table + *(Y++);
1494         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1495                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1496                             uvGreen] |
1497                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1498     }
1499
1500     if (width & 1) {
1501         u = *(U++);
1502         v = *(V++);
1503         uvRed = (V_RED_COEF*v) >> SHIFT;
1504         uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1505         uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1506
1507         tableY = table + *(Y++);
1508         *(dest++) = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1509                      tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1510                             uvGreen] |
1511                      tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1512     }
1513 }
1514
1515 /* yuv routines with scaling */
1516 /* 4:2:2 i, 16 bpp*/
1517
1518 void yuv422ToRgb16_scaled (unsigned char * Y,
1519                         unsigned char * U, unsigned char * V,
1520                         short * dest, short table[1935], int width , int dest_width,
1521             int height, int dest_height, int skip, int dest_skip,short * buffer)
1522 {
1523     int i, i_hcount, i_vcount, j, k;
1524     int u;
1525     int v;
1526     int uvRed;
1527     int uvGreen;
1528     int uvBlue;
1529     short * tableY;
1530     short pix;
1531
1532     if ( ( width < dest_width ) && ( height < dest_height ) )
1533     {
1534         i_vcount = dest_height;
1535         k = height;
1536         while ( k-- )
1537         {
1538             j = 0;
1539             i = width >> 1;
1540             i_hcount = dest_width;
1541             while ( i-- ) 
1542             {
1543                     u = *(U++);
1544                     v = *(V++);
1545                     uvRed = (V_RED_COEF*v) >> SHIFT;
1546                     uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1547                     uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1548
1549                     tableY = table + *(Y++);
1550                     pix = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1551                                tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)
1552                        >>SHIFT) + uvGreen] |
1553                                tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1554                 while ( ( i_hcount -= width ) >= 0 )
1555                 {
1556                     buffer[j++] = pix;
1557                 }
1558                 i_hcount += dest_width;
1559             
1560                     tableY = table + *(Y++);
1561                     pix = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1562                                tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)
1563                        >>SHIFT) + uvGreen] |
1564                                tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1565                 while ( ( i_hcount -= width ) >= 0 )
1566                 {
1567                     buffer[j++] = pix;
1568                 }
1569                 i_hcount += dest_width;
1570             }
1571             while ( ( i_vcount -= height ) >= 0 )
1572             {
1573             for (j=0; j<dest_width; j+=16)
1574                 {
1575                     dest[j]=buffer[j];
1576                     dest[j+1]=buffer[j+1];
1577                     dest[j+2]=buffer[j+2];
1578                     dest[j+3]=buffer[j+3];
1579                     dest[j+4]=buffer[j+4];
1580                     dest[j+6]=buffer[j+7];
1581                     dest[j+8]=buffer[j+9];
1582                     dest[j+10]=buffer[j+10];
1583                     dest[j+11]=buffer[j+11];
1584                     dest[j+12]=buffer[j+12];
1585                     dest[j+13]=buffer[j+13];
1586                     dest[j+14]=buffer[j+14];
1587                     dest[j+15]=buffer[j+15];
1588                 }
1589                 dest += dest_skip;
1590             }
1591         i_vcount += dest_height;
1592         }
1593     }
1594     else if ( ( width > dest_width ) && ( height < dest_height ) )
1595     {
1596         i_vcount = dest_height;
1597         k = height;
1598         while ( k-- )
1599         {
1600             j = 0;
1601             i_hcount = 0;
1602             i = width >> 1;
1603             while ( i-- ) 
1604             {
1605                 u = *(U++);
1606                 v = *(V++);
1607                 uvRed = (V_RED_COEF*v) >> SHIFT;
1608                 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1609                 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1610                                                                         
1611                     if ( ( i_hcount -= dest_width ) >= 0 )
1612                     Y++;
1613                 else
1614                 {
1615                     tableY = table + *(Y++);
1616                     buffer[j++] = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + 
1617                                    uvRed] | 
1618                                    tableY [135 - 
1619                                    (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1620                                    uvGreen] |
1621                                    tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + 
1622                                    uvBlue]);
1623                     i_hcount += width;
1624                 }
1625                 if ( ( i_hcount -= dest_width ) >= 0 )
1626                     Y++;
1627                 else
1628                 {     
1629                     tableY = table + *(Y++);
1630                     buffer[j++] = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + 
1631                                    uvRed] |
1632                                    tableY [135 - 
1633                                    (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1634                                    uvGreen] |
1635                                    tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + 
1636                                    uvBlue]);
1637                                    i_hcount += width;
1638                 }
1639             }
1640             while ( ( i_vcount -= height ) >= 0 )
1641             {
1642                 for (j=0; j<dest_width; j+=16)
1643                  {
1644                     dest[j]=buffer[j];
1645                     dest[j+1]=buffer[j+1];
1646                     dest[j+2]=buffer[j+2];
1647                     dest[j+3]=buffer[j+3];
1648                     dest[j+4]=buffer[j+4];
1649                     dest[j+6]=buffer[j+7];
1650                     dest[j+8]=buffer[j+9];
1651                     dest[j+10]=buffer[j+10];
1652                     dest[j+11]=buffer[j+11];
1653                     dest[j+12]=buffer[j+12];
1654                     dest[j+13]=buffer[j+13];
1655                     dest[j+14]=buffer[j+14];
1656                     dest[j+15]=buffer[j+15];
1657                 }
1658                 dest += dest_skip;
1659             }
1660             i_vcount += dest_height;
1661         }
1662     }
1663     else if ( ( width < dest_width ) && ( height > dest_height ) )
1664     {
1665         i_vcount = 0;
1666         k = height;
1667         while ( k-- )
1668         {
1669             j = 0;
1670             i = width >> 1;
1671             i_hcount = dest_width;
1672             while ( i-- ) 
1673             {
1674                     u = *(U++);
1675                     v = *(V++);
1676                     uvRed = (V_RED_COEF*v) >> SHIFT;
1677                     uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1678                     uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1679
1680                     tableY = table + *(Y++);
1681                     pix = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1682                                tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)
1683                        >>SHIFT) + uvGreen] |
1684                                tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1685                 while ( ( i_hcount -= width ) >= 0 )
1686                 {
1687                     dest[j++] = pix;
1688                 }
1689                 i_hcount += dest_width;
1690             
1691                     tableY = table + *(Y++);
1692                     pix = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1693                                tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)
1694                        >>SHIFT) + uvGreen] |
1695                                tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1696                 while ( ( i_hcount -= width ) >= 0 )
1697                 {
1698                     dest[j++] = pix;
1699                 }
1700                 i_hcount += dest_width;
1701             }
1702             while ( ( i_vcount -= height ) >= 0 )
1703             {
1704                 Y += skip;
1705                 U += skip >> 1;
1706                 V += skip >> 1;
1707             }
1708             i_vcount += dest_height;
1709         }
1710     }
1711     else if ( ( width > dest_width ) && ( height > dest_height ) )
1712     {
1713         i_vcount = dest_height;
1714         k = height;
1715         while ( k-- )
1716         {
1717             j = 0;
1718             i_hcount = 0;
1719             i = width >> 1;
1720             while ( i-- ) 
1721             {
1722                 u = *(U++);
1723                 v = *(V++);
1724                 uvRed = (V_RED_COEF*v) >> SHIFT;
1725                 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1726                 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1727                                                                         
1728                     if ( ( i_hcount -= dest_width ) >= 0 )
1729                     Y++;
1730                 else
1731                 {
1732                     tableY = table + *(Y++);
1733                     dest[j++] = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + 
1734                                  uvRed] | 
1735                                  tableY [135 - 
1736                                  (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1737                                  uvGreen] |
1738                                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + 
1739                                  uvBlue]);
1740                     i_hcount += width;
1741                 }
1742                 if ( ( i_hcount -= dest_width ) >= 0 )
1743                     Y++;
1744                 else
1745                 {     
1746                     tableY = table + *(Y++);
1747                     dest[j++] = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + 
1748                                  uvRed] |
1749                                  tableY [135 - 
1750                                  (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1751                                  uvGreen] |
1752                                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + 
1753                                  uvBlue]);
1754                                  i_hcount += width;
1755                 }
1756             }
1757             while ( ( i_vcount -= height ) >= 0 )
1758             {
1759                 Y += skip;
1760                 U += skip >> 1;
1761                 V += skip >> 1;
1762             }
1763             i_vcount += dest_height;
1764         }
1765     }
1766 }
1767
1768 /* yuv routines with scaling */
1769 /* 4:2:0 i, 16 bpp*/
1770
1771 void yuv420ToRgb16_scaled (unsigned char * Y,
1772                         unsigned char * U, unsigned char * V,
1773                         short * dest, short table[1935], int width , int dest_width,
1774             int height, int dest_height, int skip, int dest_skip,short * buffer)
1775 {
1776     int i, i_hcount, i_vcount, j, k;
1777     int u;
1778     int v;
1779     int uvRed;
1780     int uvGreen;
1781     int uvBlue;
1782     short * tableY;
1783     short pix;
1784
1785     if ( ( width < dest_width ) && ( height < dest_height ) )
1786     {
1787         i_vcount = dest_height;
1788         k = height >> 1;
1789         while ( k-- )
1790         {
1791             j = 0;
1792             i = width >> 1;
1793             i_hcount = dest_width;
1794             while ( i-- ) 
1795             {
1796                     u = *(U++);
1797                     v = *(V++);
1798                     uvRed = (V_RED_COEF*v) >> SHIFT;
1799                     uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1800                     uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1801
1802                     tableY = table + *(Y++);
1803                     pix = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1804                                tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)
1805                        >>SHIFT) + uvGreen] |
1806                                tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1807                 while ( ( i_hcount -= width ) >= 0 )
1808                 {
1809                     buffer[j++] = pix;
1810                 }
1811                 i_hcount += dest_width;
1812             
1813                     tableY = table + *(Y++);
1814                     pix = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1815                                tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)
1816                        >>SHIFT) + uvGreen] |
1817                                tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1818                 while ( ( i_hcount -= width ) >= 0 )
1819                 {
1820                     buffer[j++] = pix;
1821                 }
1822                 i_hcount += dest_width;
1823             }
1824             while ( ( i_vcount -= height ) >= 0 )
1825             {
1826                 for (j=0; j<dest_width; j+=16)
1827                     {
1828                         dest[j]=buffer[j];
1829                         dest[j+1]=buffer[j+1];
1830                         dest[j+2]=buffer[j+2];
1831                         dest[j+3]=buffer[j+3];
1832                         dest[j+4]=buffer[j+4];
1833                         dest[j+6]=buffer[j+7];
1834                         dest[j+8]=buffer[j+9];
1835                         dest[j+10]=buffer[j+10];
1836                         dest[j+11]=buffer[j+11];
1837                         dest[j+12]=buffer[j+12];
1838                         dest[j+13]=buffer[j+13];
1839                         dest[j+14]=buffer[j+14];
1840                         dest[j+15]=buffer[j+15];
1841                     }
1842                 dest += dest_skip;
1843             }
1844             i_vcount += dest_height;
1845             U -= skip >> 1;
1846             V -= skip >> 1;
1847             j = 0;
1848             i = width >> 1;
1849             i_hcount = dest_width;
1850             while ( i-- ) 
1851             {
1852                     u = *(U++);
1853                     v = *(V++);
1854                     uvRed = (V_RED_COEF*v) >> SHIFT;
1855                     uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1856                     uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1857
1858                     tableY = table + *(Y++);
1859                     pix = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1860                                tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)
1861                        >>SHIFT) + uvGreen] |
1862                                tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1863                 while ( ( i_hcount -= width ) >= 0 )
1864                 {
1865                     buffer[j++] = pix;
1866                 }
1867                 i_hcount += dest_width;
1868             
1869                     tableY = table + *(Y++);
1870                     pix = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1871                                tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)
1872                        >>SHIFT) + uvGreen] |
1873                                tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1874                 while ( ( i_hcount -= width ) >= 0 )
1875                 {
1876                     buffer[j++] = pix;
1877                 }
1878                 i_hcount += dest_width;
1879             }
1880             while ( ( i_vcount -= height ) >= 0 )
1881             {
1882                 for (j=0; j<dest_width; j+=16)
1883                     {
1884                         dest[j]=buffer[j];
1885                         dest[j+1]=buffer[j+1];
1886                         dest[j+2]=buffer[j+2];
1887                         dest[j+3]=buffer[j+3];
1888                         dest[j+4]=buffer[j+4];
1889                         dest[j+6]=buffer[j+7];
1890                         dest[j+8]=buffer[j+9];
1891                         dest[j+10]=buffer[j+10];
1892                         dest[j+11]=buffer[j+11];
1893                         dest[j+12]=buffer[j+12];
1894                         dest[j+13]=buffer[j+13];
1895                         dest[j+14]=buffer[j+14];
1896                         dest[j+15]=buffer[j+15];
1897                     }
1898                 dest += dest_skip;
1899             }
1900             i_vcount += dest_height;
1901         }
1902     }
1903     else if ( ( width > dest_width ) && ( height < dest_height ) )
1904     {
1905         i_vcount = dest_height;
1906         k = height;
1907         while ( k-- )
1908         {
1909             j = 0;
1910             i_hcount = 0;
1911             i = width >> 1;
1912             while ( i-- ) 
1913             {
1914                 u = *(U++);
1915                 v = *(V++);
1916                 uvRed = (V_RED_COEF*v) >> SHIFT;
1917                 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1918                 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1919                                                                         
1920                     if ( ( i_hcount -= dest_width ) >= 0 )
1921                     Y++;
1922                 else
1923                 {
1924                     tableY = table + *(Y++);
1925                     buffer[j++] = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + 
1926                                    uvRed] | 
1927                                    tableY [135 - 
1928                                    (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1929                                    uvGreen] |
1930                                    tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + 
1931                                    uvBlue]);
1932                     i_hcount += width;
1933                 }
1934                 if ( ( i_hcount -= dest_width ) >= 0 )
1935                     Y++;
1936                 else
1937                 {     
1938                     tableY = table + *(Y++);
1939                     buffer[j++] = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + 
1940                                    uvRed] |
1941                                    tableY [135 - 
1942                                    (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1943                                    uvGreen] |
1944                                    tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + 
1945                                    uvBlue]);
1946                                    i_hcount += width;
1947                 }
1948             }
1949             while ( ( i_vcount -= height ) >= 0 )
1950             {
1951                 for (j=0; j<dest_width; j+=16)
1952                  {
1953                     dest[j]=buffer[j];
1954                     dest[j+1]=buffer[j+1];
1955                     dest[j+2]=buffer[j+2];
1956                     dest[j+3]=buffer[j+3];
1957                     dest[j+4]=buffer[j+4];
1958                     dest[j+6]=buffer[j+7];
1959                     dest[j+8]=buffer[j+9];
1960                     dest[j+10]=buffer[j+10];
1961                     dest[j+11]=buffer[j+11];
1962                     dest[j+12]=buffer[j+12];
1963                     dest[j+13]=buffer[j+13];
1964                     dest[j+14]=buffer[j+14];
1965                     dest[j+15]=buffer[j+15];
1966                 }
1967                 dest += dest_skip;
1968             }
1969             i_vcount += dest_height;
1970             U -= skip >> 1;
1971             V -= skip >> 1;
1972             j = 0;
1973             i_hcount = 0;
1974             i = width >> 1;
1975             while ( i-- ) 
1976             {
1977                 u = *(U++);
1978                 v = *(V++);
1979                 uvRed = (V_RED_COEF*v) >> SHIFT;
1980                 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1981                 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1982                                                                         
1983                     if ( ( i_hcount -= dest_width ) >= 0 )
1984                     Y++;
1985                 else
1986                 {
1987                     tableY = table + *(Y++);
1988                     buffer[j++] = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + 
1989                                    uvRed] | 
1990                                    tableY [135 - 
1991                                    (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1992                                    uvGreen] |
1993                                    tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + 
1994                                    uvBlue]);
1995                     i_hcount += width;
1996                 }
1997                 if ( ( i_hcount -= dest_width ) >= 0 )
1998                     Y++;
1999                 else
2000                 {     
2001                     tableY = table + *(Y++);
2002                     buffer[j++] = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + 
2003                                    uvRed] |
2004                                    tableY [135 - 
2005                                    (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
2006                                    uvGreen] |
2007                                    tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + 
2008                                    uvBlue]);
2009                                    i_hcount += width;
2010                 }
2011             }
2012             while ( ( i_vcount -= height ) >= 0 )
2013             {
2014                 for (j=0; j<dest_width; j+=16)
2015                  {
2016                     dest[j]=buffer[j];
2017                     dest[j+1]=buffer[j+1];
2018                     dest[j+2]=buffer[j+2];
2019                     dest[j+3]=buffer[j+3];
2020                     dest[j+4]=buffer[j+4];
2021                     dest[j+6]=buffer[j+7];
2022                     dest[j+8]=buffer[j+9];
2023                     dest[j+10]=buffer[j+10];
2024                     dest[j+11]=buffer[j+11];
2025                     dest[j+12]=buffer[j+12];
2026                     dest[j+13]=buffer[j+13];
2027                     dest[j+14]=buffer[j+14];
2028                     dest[j+15]=buffer[j+15];
2029                 }
2030                 dest += dest_skip;
2031             }
2032             i_vcount += dest_height;
2033         }
2034     }
2035     else if ( ( width < dest_width ) && ( height > dest_height ) )
2036     {
2037         i_vcount = 0;
2038         k = height;
2039         while ( k-- )
2040         {
2041             j = 0;
2042             i = width >> 1;
2043             i_hcount = dest_width;
2044             while ( i-- ) 
2045             {
2046                     u = *(U++);
2047                     v = *(V++);
2048                     uvRed = (V_RED_COEF*v) >> SHIFT;
2049                     uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
2050                     uvBlue = (U_BLUE_COEF*u) >> SHIFT;
2051
2052                     tableY = table + *(Y++);
2053                     pix = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
2054                                tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)
2055                        >>SHIFT) + uvGreen] |
2056                                tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
2057                 while ( ( i_hcount -= width ) >= 0 )
2058                 {
2059                     dest[j++] = pix;
2060                 }
2061                 i_hcount += dest_width;
2062             
2063                     tableY = table + *(Y++);
2064                     pix = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
2065                                tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)
2066                        >>SHIFT) + uvGreen] |
2067                                tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
2068                 while ( ( i_hcount -= width ) >= 0 )
2069                 {
2070                     dest[j++] = pix;
2071                 }
2072                 i_hcount += dest_width;
2073             }
2074             j = 0;
2075             while ( ( i_vcount -= height ) >= 0 )
2076             {
2077                 Y += skip;
2078                 j++;
2079             }
2080             U += skip * ( j >> 1 );
2081             V += skip * ( j >> 1 );
2082             i_vcount += dest_height;
2083         }
2084     }
2085     else if ( ( width > dest_width ) && ( height > dest_height ) )
2086     {
2087         i_vcount = dest_height;
2088         k = height;
2089         while ( k-- )
2090         {
2091             j = 0;
2092             i_hcount = 0;
2093             i = width >> 1;
2094             while ( i-- ) 
2095             {
2096                 u = *(U++);
2097                 v = *(V++);
2098                 uvRed = (V_RED_COEF*v) >> SHIFT;
2099                 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
2100                 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
2101                                                                         
2102                     if ( ( i_hcount -= dest_width ) >= 0 )
2103                     Y++;
2104                 else
2105                 {
2106                     tableY = table + *(Y++);
2107                     dest[j++] = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + 
2108                                  uvRed] | 
2109                                  tableY [135 - 
2110                                  (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
2111                                  uvGreen] |
2112                                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + 
2113                                  uvBlue]);
2114                     i_hcount += width;
2115                 }
2116                 if ( ( i_hcount -= dest_width ) >= 0 )
2117                     Y++;
2118                 else
2119                 {     
2120                     tableY = table + *(Y++);
2121                     dest[j++] = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + 
2122                                  uvRed] |
2123                                  tableY [135 - 
2124                                  (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
2125                                  uvGreen] |
2126                                  tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + 
2127                                  uvBlue]);
2128                                  i_hcount += width;
2129                 }
2130             }
2131             j = 0;
2132             while ( ( i_vcount -= height ) >= 0 )
2133             {
2134                 Y += skip;
2135                 j++;
2136             }
2137             U += skip * ( j >> 1 );
2138             V += skip * ( j >> 1 );
2139             i_vcount += dest_height;
2140         }
2141     }
2142 }
2143