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