]> git.sesse.net Git - ffmpeg/blob - libswscale/yuv2rgb.c
Move dither tables from yuv2rgb to swscale, they have been written by me and
[ffmpeg] / libswscale / yuv2rgb.c
1 /*
2  * yuv2rgb.c, Software YUV to RGB converter
3  *
4  *  Copyright (C) 1999, Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5  *
6  *  Functions broken out from display_x11.c and several new modes
7  *  added by HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
8  *
9  *  15 & 16 bpp support by Franck Sicard <Franck.Sicard@solsoft.fr>
10  *
11  *  MMX/MMX2 template stuff (needed for fast movntq support),
12  *  1,4,8bpp support and context / deglobalize stuff
13  *  by Michael Niedermayer (michaelni@gmx.at)
14  *
15  *  This file is part of mpeg2dec, a free MPEG-2 video decoder
16  *
17  *  mpeg2dec is free software; you can redistribute it and/or modify
18  *  it under the terms of the GNU General Public License as published by
19  *  the Free Software Foundation; either version 2, or (at your option)
20  *  any later version.
21  *
22  *  mpeg2dec is distributed in the hope that it will be useful,
23  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *  GNU General Public License for more details.
26  *
27  *  You should have received a copy of the GNU General Public License
28  *  along with mpeg2dec; if not, write to the Free Software
29  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <inttypes.h>
35 #include <assert.h>
36
37 #include "config.h"
38 #include "rgb2rgb.h"
39 #include "swscale.h"
40 #include "swscale_internal.h"
41
42 #define DITHER1XBPP // only for MMX
43
44 extern const uint8_t dither_2x2_4[2][8];
45 extern const uint8_t dither_2x2_8[2][8];
46 extern const uint8_t dither_8x8_32[8][8];
47 extern const uint8_t dither_8x8_73[8][8];
48 extern const uint8_t dither_8x8_220[8][8];
49
50 #ifdef HAVE_MMX
51
52 /* hope these constant values are cache line aligned */
53 DECLARE_ASM_CONST(8, uint64_t, mmx_00ffw)   = 0x00ff00ff00ff00ffULL;
54 DECLARE_ASM_CONST(8, uint64_t, mmx_redmask) = 0xf8f8f8f8f8f8f8f8ULL;
55 DECLARE_ASM_CONST(8, uint64_t, mmx_grnmask) = 0xfcfcfcfcfcfcfcfcULL;
56
57 // The volatile is required because gcc otherwise optimizes some writes away
58 // not knowing that these are read in the ASM block.
59 static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
60 static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
61 static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
62 static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
63
64 #undef HAVE_MMX
65
66 //MMX versions
67 #undef RENAME
68 #define HAVE_MMX
69 #undef HAVE_MMX2
70 #undef HAVE_3DNOW
71 #define RENAME(a) a ## _MMX
72 #include "yuv2rgb_template.c"
73
74 //MMX2 versions
75 #undef RENAME
76 #define HAVE_MMX
77 #define HAVE_MMX2
78 #undef HAVE_3DNOW
79 #define RENAME(a) a ## _MMX2
80 #include "yuv2rgb_template.c"
81
82 #endif /* HAVE_MMX */
83
84 const int32_t Inverse_Table_6_9[8][4] = {
85     {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
86     {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
87     {104597, 132201, 25675, 53279}, /* unspecified */
88     {104597, 132201, 25675, 53279}, /* reserved */
89     {104448, 132798, 24759, 53109}, /* FCC */
90     {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
91     {104597, 132201, 25675, 53279}, /* SMPTE 170M */
92     {117579, 136230, 16907, 35559}  /* SMPTE 240M (1987) */
93 };
94
95 #define RGB(i)                                      \
96     U = pu[i];                                      \
97     V = pv[i];                                      \
98     r = (void *)c->table_rV[V];                     \
99     g = (void *)(c->table_gU[U] + c->table_gV[V]);  \
100     b = (void *)c->table_bU[U];
101
102 #define DST1(i)                         \
103     Y = py_1[2*i];                      \
104     dst_1[2*i] = r[Y] + g[Y] + b[Y];    \
105     Y = py_1[2*i+1];                    \
106     dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
107
108 #define DST2(i)                         \
109     Y = py_2[2*i];                      \
110     dst_2[2*i] = r[Y] + g[Y] + b[Y];    \
111     Y = py_2[2*i+1];                    \
112     dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
113
114 #define DST1RGB(i)                                                \
115     Y = py_1[2*i];                                                \
116     dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y];  \
117     Y = py_1[2*i+1];                                              \
118     dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
119
120 #define DST2RGB(i)                                                \
121     Y = py_2[2*i];                                                \
122     dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y];  \
123     Y = py_2[2*i+1];                                              \
124     dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
125
126 #define DST1BGR(i)                                                \
127     Y = py_1[2*i];                                                \
128     dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y];  \
129     Y = py_1[2*i+1];                                              \
130     dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
131
132 #define DST2BGR(i)                                                \
133     Y = py_2[2*i];                                                \
134     dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y];  \
135     Y = py_2[2*i+1];                                              \
136     dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
137
138 #define PROLOG(func_name, dst_type) \
139 static int func_name(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, \
140                      int srcSliceH, uint8_t* dst[], int dstStride[]){\
141     int y;\
142 \
143     if (c->srcFormat == PIX_FMT_YUV422P){\
144         srcStride[1] *= 2;\
145         srcStride[2] *= 2;\
146     }\
147     for (y=0; y<srcSliceH; y+=2){\
148         dst_type *dst_1= (dst_type*)(dst[0] + (y+srcSliceY  )*dstStride[0]);\
149         dst_type *dst_2= (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
150         dst_type av_unused *r, *b;\
151         dst_type *g;\
152         uint8_t *py_1= src[0] + y*srcStride[0];\
153         uint8_t *py_2= py_1 + srcStride[0];\
154         uint8_t *pu= src[1] + (y>>1)*srcStride[1];\
155         uint8_t *pv= src[2] + (y>>1)*srcStride[2];\
156         unsigned int h_size= c->dstW>>3;\
157         while (h_size--) {\
158             int av_unused U, V;\
159             int Y;\
160
161 #define EPILOG1(dst_delta)\
162             pu += 4;\
163             pv += 4;\
164             py_1 += 8;\
165             py_2 += 8;\
166             dst_1 += dst_delta;\
167             dst_2 += dst_delta;\
168         }\
169         if (c->dstW & 4) {\
170             int av_unused Y, U, V;\
171
172 #define EPILOG2()\
173         }\
174     }\
175     return srcSliceH;\
176 }
177
178 #define EPILOG(dst_delta)\
179     EPILOG1(dst_delta)\
180     EPILOG2()
181
182 PROLOG(yuv2rgb_c_32, uint32_t)
183     RGB(0);
184     DST1(0);
185     DST2(0);
186
187     RGB(1);
188     DST2(1);
189     DST1(1);
190
191     RGB(2);
192     DST1(2);
193     DST2(2);
194
195     RGB(3);
196     DST2(3);
197     DST1(3);
198 EPILOG1(8)
199     RGB(0);
200     DST1(0);
201     DST2(0);
202
203     RGB(1);
204     DST2(1);
205     DST1(1);
206 EPILOG2()
207
208 PROLOG(yuv2rgb_c_24_rgb, uint8_t)
209     RGB(0);
210     DST1RGB(0);
211     DST2RGB(0);
212
213     RGB(1);
214     DST2RGB(1);
215     DST1RGB(1);
216
217     RGB(2);
218     DST1RGB(2);
219     DST2RGB(2);
220
221     RGB(3);
222     DST2RGB(3);
223     DST1RGB(3);
224 EPILOG1(24)
225     RGB(0);
226     DST1RGB(0);
227     DST2RGB(0);
228
229     RGB(1);
230     DST2RGB(1);
231     DST1RGB(1);
232 EPILOG2()
233
234 // only trivial mods from yuv2rgb_c_24_rgb
235 PROLOG(yuv2rgb_c_24_bgr, uint8_t)
236     RGB(0);
237     DST1BGR(0);
238     DST2BGR(0);
239
240     RGB(1);
241     DST2BGR(1);
242     DST1BGR(1);
243
244     RGB(2);
245     DST1BGR(2);
246     DST2BGR(2);
247
248     RGB(3);
249     DST2BGR(3);
250     DST1BGR(3);
251 EPILOG1(24)
252     RGB(0);
253     DST1BGR(0);
254     DST2BGR(0);
255
256     RGB(1);
257     DST2BGR(1);
258     DST1BGR(1);
259 EPILOG2()
260
261 // This is exactly the same code as yuv2rgb_c_32 except for the types of
262 // r, g, b, dst_1, dst_2
263 PROLOG(yuv2rgb_c_16, uint16_t)
264     RGB(0);
265     DST1(0);
266     DST2(0);
267
268     RGB(1);
269     DST2(1);
270     DST1(1);
271
272     RGB(2);
273     DST1(2);
274     DST2(2);
275
276     RGB(3);
277     DST2(3);
278     DST1(3);
279 EPILOG(8)
280
281 // This is exactly the same code as yuv2rgb_c_32 except for the types of
282 // r, g, b, dst_1, dst_2
283 PROLOG(yuv2rgb_c_8, uint8_t)
284     RGB(0);
285     DST1(0);
286     DST2(0);
287
288     RGB(1);
289     DST2(1);
290     DST1(1);
291
292     RGB(2);
293     DST1(2);
294     DST2(2);
295
296     RGB(3);
297     DST2(3);
298     DST1(3);
299 EPILOG(8)
300
301 // r, g, b, dst_1, dst_2
302 PROLOG(yuv2rgb_c_8_ordered_dither, uint8_t)
303     const uint8_t *d32= dither_8x8_32[y&7];
304     const uint8_t *d64= dither_8x8_73[y&7];
305 #define DST1bpp8(i,o)                                               \
306     Y = py_1[2*i];                                                  \
307     dst_1[2*i]   = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]];   \
308     Y = py_1[2*i+1];                                                \
309     dst_1[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
310
311 #define DST2bpp8(i,o)                                               \
312     Y = py_2[2*i];                                                  \
313     dst_2[2*i]   =  r[Y+d32[8+o]] + g[Y+d32[8+o]] + b[Y+d64[8+o]];  \
314     Y = py_2[2*i+1];                                                \
315     dst_2[2*i+1] =  r[Y+d32[9+o]] + g[Y+d32[9+o]] + b[Y+d64[9+o]];
316
317
318     RGB(0);
319     DST1bpp8(0,0);
320     DST2bpp8(0,0);
321
322     RGB(1);
323     DST2bpp8(1,2);
324     DST1bpp8(1,2);
325
326     RGB(2);
327     DST1bpp8(2,4);
328     DST2bpp8(2,4);
329
330     RGB(3);
331     DST2bpp8(3,6);
332     DST1bpp8(3,6);
333 EPILOG(8)
334
335
336 // This is exactly the same code as yuv2rgb_c_32 except for the types of
337 // r, g, b, dst_1, dst_2
338 PROLOG(yuv2rgb_c_4, uint8_t)
339     int acc;
340 #define DST1_4(i)                   \
341     Y = py_1[2*i];                  \
342     acc = r[Y] + g[Y] + b[Y];       \
343     Y = py_1[2*i+1];                \
344     acc |= (r[Y] + g[Y] + b[Y])<<4; \
345     dst_1[i] = acc;
346
347 #define DST2_4(i)                   \
348     Y = py_2[2*i];                  \
349     acc = r[Y] + g[Y] + b[Y];       \
350     Y = py_2[2*i+1];                \
351     acc |= (r[Y] + g[Y] + b[Y])<<4; \
352     dst_2[i] = acc;
353
354     RGB(0);
355     DST1_4(0);
356     DST2_4(0);
357
358     RGB(1);
359     DST2_4(1);
360     DST1_4(1);
361
362     RGB(2);
363     DST1_4(2);
364     DST2_4(2);
365
366     RGB(3);
367     DST2_4(3);
368     DST1_4(3);
369 EPILOG(4)
370
371 PROLOG(yuv2rgb_c_4_ordered_dither, uint8_t)
372     const uint8_t *d64= dither_8x8_73[y&7];
373     const uint8_t *d128=dither_8x8_220[y&7];
374     int acc;
375
376 #define DST1bpp4(i,o)                                             \
377     Y = py_1[2*i];                                                \
378     acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]];        \
379     Y = py_1[2*i+1];                                              \
380     acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4;  \
381     dst_1[i]= acc;
382
383 #define DST2bpp4(i,o)                                             \
384     Y = py_2[2*i];                                                \
385     acc =  r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]];       \
386     Y = py_2[2*i+1];                                              \
387     acc |=  (r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]])<<4; \
388     dst_2[i]= acc;
389
390
391     RGB(0);
392     DST1bpp4(0,0);
393     DST2bpp4(0,0);
394
395     RGB(1);
396     DST2bpp4(1,2);
397     DST1bpp4(1,2);
398
399     RGB(2);
400     DST1bpp4(2,4);
401     DST2bpp4(2,4);
402
403     RGB(3);
404     DST2bpp4(3,6);
405     DST1bpp4(3,6);
406 EPILOG(4)
407
408 // This is exactly the same code as yuv2rgb_c_32 except for the types of
409 // r, g, b, dst_1, dst_2
410 PROLOG(yuv2rgb_c_4b, uint8_t)
411     RGB(0);
412     DST1(0);
413     DST2(0);
414
415     RGB(1);
416     DST2(1);
417     DST1(1);
418
419     RGB(2);
420     DST1(2);
421     DST2(2);
422
423     RGB(3);
424     DST2(3);
425     DST1(3);
426 EPILOG(8)
427
428 PROLOG(yuv2rgb_c_4b_ordered_dither, uint8_t)
429     const uint8_t *d64= dither_8x8_73[y&7];
430     const uint8_t *d128=dither_8x8_220[y&7];
431
432 #define DST1bpp4b(i,o)                                                \
433     Y = py_1[2*i];                                                    \
434     dst_1[2*i]   = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]];   \
435     Y = py_1[2*i+1];                                                  \
436     dst_1[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
437
438 #define DST2bpp4b(i,o)                                                \
439     Y = py_2[2*i];                                                    \
440     dst_2[2*i]   =  r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]];  \
441     Y = py_2[2*i+1];                                                  \
442     dst_2[2*i+1] =  r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]];
443
444
445     RGB(0);
446     DST1bpp4b(0,0);
447     DST2bpp4b(0,0);
448
449     RGB(1);
450     DST2bpp4b(1,2);
451     DST1bpp4b(1,2);
452
453     RGB(2);
454     DST1bpp4b(2,4);
455     DST2bpp4b(2,4);
456
457     RGB(3);
458     DST2bpp4b(3,6);
459     DST1bpp4b(3,6);
460 EPILOG(8)
461
462 PROLOG(yuv2rgb_c_1_ordered_dither, uint8_t)
463         const uint8_t *d128=dither_8x8_220[y&7];
464         char out_1=0, out_2=0;
465         g= c->table_gU[128] + c->table_gV[128];
466
467 #define DST1bpp1(i,o)               \
468     Y = py_1[2*i];                  \
469     out_1+= out_1 + g[Y+d128[0+o]]; \
470     Y = py_1[2*i+1];                \
471     out_1+= out_1 + g[Y+d128[1+o]];
472
473 #define DST2bpp1(i,o)               \
474     Y = py_2[2*i];                  \
475     out_2+= out_2 + g[Y+d128[8+o]]; \
476     Y = py_2[2*i+1];                \
477     out_2+= out_2 + g[Y+d128[9+o]];
478
479     DST1bpp1(0,0);
480     DST2bpp1(0,0);
481
482     DST2bpp1(1,2);
483     DST1bpp1(1,2);
484
485     DST1bpp1(2,4);
486     DST2bpp1(2,4);
487
488     DST2bpp1(3,6);
489     DST1bpp1(3,6);
490
491     dst_1[0]= out_1;
492     dst_2[0]= out_2;
493 EPILOG(1)
494
495 SwsFunc yuv2rgb_get_func_ptr (SwsContext *c)
496 {
497 #if defined(HAVE_MMX2) || defined(HAVE_MMX)
498     if (c->flags & SWS_CPU_CAPS_MMX2){
499         switch(c->dstFormat){
500         case PIX_FMT_RGB32:  return yuv420_rgb32_MMX2;
501         case PIX_FMT_BGR24:  return yuv420_rgb24_MMX2;
502         case PIX_FMT_RGB565: return yuv420_rgb16_MMX2;
503         case PIX_FMT_RGB555: return yuv420_rgb15_MMX2;
504         }
505     }
506     if (c->flags & SWS_CPU_CAPS_MMX){
507         switch(c->dstFormat){
508         case PIX_FMT_RGB32:  return yuv420_rgb32_MMX;
509         case PIX_FMT_BGR24:  return yuv420_rgb24_MMX;
510         case PIX_FMT_RGB565: return yuv420_rgb16_MMX;
511         case PIX_FMT_RGB555: return yuv420_rgb15_MMX;
512         }
513     }
514 #endif
515 #ifdef HAVE_VIS
516     {
517         SwsFunc t= yuv2rgb_init_vis(c);
518         if (t) return t;
519     }
520 #endif
521 #ifdef CONFIG_MLIB
522     {
523         SwsFunc t= yuv2rgb_init_mlib(c);
524         if (t) return t;
525     }
526 #endif
527 #ifdef HAVE_ALTIVEC
528     if (c->flags & SWS_CPU_CAPS_ALTIVEC)
529     {
530         SwsFunc t = yuv2rgb_init_altivec(c);
531         if (t) return t;
532     }
533 #endif
534
535 #ifdef ARCH_BFIN
536     if (c->flags & SWS_CPU_CAPS_BFIN)
537     {
538         SwsFunc t = ff_bfin_yuv2rgb_get_func_ptr (c);
539         if (t) return t;
540     }
541 #endif
542
543     av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found.\n");
544
545     switch(c->dstFormat){
546     case PIX_FMT_BGR32_1:
547     case PIX_FMT_RGB32_1:
548     case PIX_FMT_BGR32:
549     case PIX_FMT_RGB32: return yuv2rgb_c_32;
550     case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
551     case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
552     case PIX_FMT_RGB565:
553     case PIX_FMT_BGR565:
554     case PIX_FMT_RGB555:
555     case PIX_FMT_BGR555: return yuv2rgb_c_16;
556     case PIX_FMT_RGB8:
557     case PIX_FMT_BGR8:  return yuv2rgb_c_8_ordered_dither;
558     case PIX_FMT_RGB4:
559     case PIX_FMT_BGR4:  return yuv2rgb_c_4_ordered_dither;
560     case PIX_FMT_RGB4_BYTE:
561     case PIX_FMT_BGR4_BYTE:  return yuv2rgb_c_4b_ordered_dither;
562     case PIX_FMT_MONOBLACK:  return yuv2rgb_c_1_ordered_dither;
563     default:
564         assert(0);
565     }
566     return NULL;
567 }
568
569 static int div_round (int dividend, int divisor)
570 {
571     if (dividend > 0)
572         return (dividend + (divisor>>1)) / divisor;
573     else
574         return -((-dividend + (divisor>>1)) / divisor);
575 }
576
577 int yuv2rgb_c_init_tables (SwsContext *c, const int inv_table[4], int fullRange, int brightness, int contrast, int saturation)
578 {
579     const int isRgb =      c->dstFormat==PIX_FMT_RGB32
580                         || c->dstFormat==PIX_FMT_RGB32_1
581                         || c->dstFormat==PIX_FMT_BGR24
582                         || c->dstFormat==PIX_FMT_RGB565
583                         || c->dstFormat==PIX_FMT_RGB555
584                         || c->dstFormat==PIX_FMT_RGB8
585                         || c->dstFormat==PIX_FMT_RGB4
586                         || c->dstFormat==PIX_FMT_RGB4_BYTE
587                         || c->dstFormat==PIX_FMT_MONOBLACK;
588     const int bpp = fmt_depth(c->dstFormat);
589     int i, base;
590     uint8_t table_Y[1024];
591     uint32_t *table_32 = 0;
592     uint16_t *table_16 = 0;
593     uint8_t *table_8 = 0;
594     uint8_t *table_332 = 0;
595     uint8_t *table_121 = 0;
596     uint8_t *table_1 = 0;
597     int entry_size = 0;
598     void *table_r = 0, *table_g = 0, *table_b = 0;
599     void *table_start;
600
601     int64_t crv =  inv_table[0];
602     int64_t cbu =  inv_table[1];
603     int64_t cgu = -inv_table[2];
604     int64_t cgv = -inv_table[3];
605     int64_t cy  = 1<<16;
606     int64_t oy  = 0;
607
608 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
609     if (!fullRange){
610         cy= (cy*255) / 219;
611         oy= 16<<16;
612     }else{
613         crv= (crv*224) / 255;
614         cbu= (cbu*224) / 255;
615         cgu= (cgu*224) / 255;
616         cgv= (cgv*224) / 255;
617     }
618
619     cy = (cy *contrast             )>>16;
620     crv= (crv*contrast * saturation)>>32;
621     cbu= (cbu*contrast * saturation)>>32;
622     cgu= (cgu*contrast * saturation)>>32;
623     cgv= (cgv*contrast * saturation)>>32;
624 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
625     oy -= 256*brightness;
626
627     for (i = 0; i < 1024; i++) {
628         int j;
629
630         j= (cy*(((i - 384)<<16) - oy) + (1<<31))>>32;
631         j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
632         table_Y[i] = j;
633     }
634
635     switch (bpp) {
636     case 32:
637         table_start= table_32 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
638         base= (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
639
640         entry_size = sizeof (uint32_t);
641         table_r = table_32 + 197;
642         table_b = table_32 + 197 + 685;
643         table_g = table_32 + 197 + 2*682;
644
645         for (i = -197; i < 256+197; i++)
646             ((uint32_t *)table_r)[i] = table_Y[i+384] << ((isRgb ? 16 : 0) + base);
647         for (i = -132; i < 256+132; i++)
648             ((uint32_t *)table_g)[i] = table_Y[i+384] << (8                + base);
649         for (i = -232; i < 256+232; i++)
650             ((uint32_t *)table_b)[i] = table_Y[i+384] << ((isRgb ? 0 : 16) + base);
651         break;
652
653     case 24:
654         table_start= table_8 = av_malloc ((256 + 2*232) * sizeof (uint8_t));
655
656         entry_size = sizeof (uint8_t);
657         table_r = table_g = table_b = table_8 + 232;
658
659         for (i = -232; i < 256+232; i++)
660             ((uint8_t * )table_b)[i] = table_Y[i+384];
661         break;
662
663     case 15:
664     case 16:
665         table_start= table_16 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
666
667         entry_size = sizeof (uint16_t);
668         table_r = table_16 + 197;
669         table_b = table_16 + 197 + 685;
670         table_g = table_16 + 197 + 2*682;
671
672         for (i = -197; i < 256+197; i++) {
673             int j = table_Y[i+384] >> 3;
674
675             if (isRgb)
676                 j <<= ((bpp==16) ? 11 : 10);
677
678             ((uint16_t *)table_r)[i] = j;
679         }
680         for (i = -132; i < 256+132; i++) {
681             int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
682
683             ((uint16_t *)table_g)[i] = j << 5;
684         }
685         for (i = -232; i < 256+232; i++) {
686             int j = table_Y[i+384] >> 3;
687
688             if (!isRgb)
689                 j <<= ((bpp==16) ? 11 : 10);
690
691             ((uint16_t *)table_b)[i] = j;
692         }
693         break;
694
695     case 8:
696         table_start= table_332 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
697
698         entry_size = sizeof (uint8_t);
699         table_r = table_332 + 197;
700         table_b = table_332 + 197 + 685;
701         table_g = table_332 + 197 + 2*682;
702
703         for (i = -197; i < 256+197; i++) {
704             int j = (table_Y[i+384 - 16] + 18)/36;
705
706             if (isRgb)
707                 j <<= 5;
708
709             ((uint8_t *)table_r)[i] = j;
710         }
711         for (i = -132; i < 256+132; i++) {
712             int j = (table_Y[i+384 - 16] + 18)/36;
713
714             if (!isRgb)
715                 j <<= 1;
716
717             ((uint8_t *)table_g)[i] = j << 2;
718         }
719         for (i = -232; i < 256+232; i++) {
720             int j = (table_Y[i+384 - 37] + 43)/85;
721
722             if (!isRgb)
723                 j <<= 6;
724
725             ((uint8_t *)table_b)[i] = j;
726         }
727         break;
728     case 4:
729     case 4|128:
730         table_start= table_121 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
731
732         entry_size = sizeof (uint8_t);
733         table_r = table_121 + 197;
734         table_b = table_121 + 197 + 685;
735         table_g = table_121 + 197 + 2*682;
736
737         for (i = -197; i < 256+197; i++) {
738             int j = table_Y[i+384 - 110] >> 7;
739
740             if (isRgb)
741                 j <<= 3;
742
743             ((uint8_t *)table_r)[i] = j;
744         }
745         for (i = -132; i < 256+132; i++) {
746             int j = (table_Y[i+384 - 37]+ 43)/85;
747
748             ((uint8_t *)table_g)[i] = j << 1;
749         }
750         for (i = -232; i < 256+232; i++) {
751             int j =table_Y[i+384 - 110] >> 7;
752
753             if (!isRgb)
754                 j <<= 3;
755
756             ((uint8_t *)table_b)[i] = j;
757         }
758         break;
759
760     case 1:
761         table_start= table_1 = av_malloc (256*2 * sizeof (uint8_t));
762
763         entry_size = sizeof (uint8_t);
764         table_g = table_1;
765         table_r = table_b = NULL;
766
767         for (i = 0; i < 256+256; i++) {
768             int j = table_Y[i + 384 - 110]>>7;
769
770             ((uint8_t *)table_g)[i] = j;
771         }
772         break;
773
774     default:
775         table_start= NULL;
776         av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
777         //free mem?
778         return -1;
779     }
780
781     for (i = 0; i < 256; i++) {
782         c->table_rV[i] = (uint8_t *)table_r + entry_size * div_round (crv * (i-128), cy);
783         c->table_gU[i] = (uint8_t *)table_g + entry_size * div_round (cgu * (i-128), cy);
784         c->table_gV[i] = entry_size * div_round (cgv * (i-128), cy);
785         c->table_bU[i] = (uint8_t *)table_b + entry_size * div_round (cbu * (i-128), cy);
786     }
787
788     av_free(c->yuvTable);
789     c->yuvTable= table_start;
790     return 0;
791 }