Update outdated availability note for -mouse-movements
[mplayer/glamo.git] / libswscale / yuv2rgb.c
blobd19430fd17d67a9953d23bc5e947e22aefb6f242
1 /*
2 * yuv2rgb.c, Software YUV to RGB converter
4 * Copyright (C) 1999, Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * Functions broken out from display_x11.c and several new modes
7 * added by HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
9 * 15 & 16 bpp support by Franck Sicard <Franck.Sicard@solsoft.fr>
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)
15 * This file is part of mpeg2dec, a free MPEG-2 video decoder
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.
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.
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
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <inttypes.h>
35 #include <assert.h>
37 #include "config.h"
38 #include "rgb2rgb.h"
39 #include "swscale.h"
40 #include "swscale_internal.h"
42 #define DITHER1XBPP // only for MMX
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];
48 #ifdef HAVE_MMX
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;
55 #undef HAVE_MMX
57 //MMX versions
58 #undef RENAME
59 #define HAVE_MMX
60 #undef HAVE_MMX2
61 #undef HAVE_3DNOW
62 #define RENAME(a) a ## _MMX
63 #include "yuv2rgb_template.c"
65 //MMX2 versions
66 #undef RENAME
67 #define HAVE_MMX
68 #define HAVE_MMX2
69 #undef HAVE_3DNOW
70 #define RENAME(a) a ## _MMX2
71 #include "yuv2rgb_template.c"
73 #endif /* HAVE_MMX */
75 const int32_t Inverse_Table_6_9[8][4] = {
76 {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
77 {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
78 {104597, 132201, 25675, 53279}, /* unspecified */
79 {104597, 132201, 25675, 53279}, /* reserved */
80 {104448, 132798, 24759, 53109}, /* FCC */
81 {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
82 {104597, 132201, 25675, 53279}, /* SMPTE 170M */
83 {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
86 #define RGB(i) \
87 U = pu[i]; \
88 V = pv[i]; \
89 r = (void *)c->table_rV[V]; \
90 g = (void *)(c->table_gU[U] + c->table_gV[V]); \
91 b = (void *)c->table_bU[U];
93 #define DST1(i) \
94 Y = py_1[2*i]; \
95 dst_1[2*i] = r[Y] + g[Y] + b[Y]; \
96 Y = py_1[2*i+1]; \
97 dst_1[2*i+1] = r[Y] + g[Y] + b[Y];
99 #define DST2(i) \
100 Y = py_2[2*i]; \
101 dst_2[2*i] = r[Y] + g[Y] + b[Y]; \
102 Y = py_2[2*i+1]; \
103 dst_2[2*i+1] = r[Y] + g[Y] + b[Y];
105 #define DST1RGB(i) \
106 Y = py_1[2*i]; \
107 dst_1[6*i] = r[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = b[Y]; \
108 Y = py_1[2*i+1]; \
109 dst_1[6*i+3] = r[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = b[Y];
111 #define DST2RGB(i) \
112 Y = py_2[2*i]; \
113 dst_2[6*i] = r[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = b[Y]; \
114 Y = py_2[2*i+1]; \
115 dst_2[6*i+3] = r[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = b[Y];
117 #define DST1BGR(i) \
118 Y = py_1[2*i]; \
119 dst_1[6*i] = b[Y]; dst_1[6*i+1] = g[Y]; dst_1[6*i+2] = r[Y]; \
120 Y = py_1[2*i+1]; \
121 dst_1[6*i+3] = b[Y]; dst_1[6*i+4] = g[Y]; dst_1[6*i+5] = r[Y];
123 #define DST2BGR(i) \
124 Y = py_2[2*i]; \
125 dst_2[6*i] = b[Y]; dst_2[6*i+1] = g[Y]; dst_2[6*i+2] = r[Y]; \
126 Y = py_2[2*i+1]; \
127 dst_2[6*i+3] = b[Y]; dst_2[6*i+4] = g[Y]; dst_2[6*i+5] = r[Y];
129 #define PROLOG(func_name, dst_type) \
130 static int func_name(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY, \
131 int srcSliceH, uint8_t* dst[], int dstStride[]){\
132 int y;\
134 if (c->srcFormat == PIX_FMT_YUV422P){\
135 srcStride[1] *= 2;\
136 srcStride[2] *= 2;\
138 for (y=0; y<srcSliceH; y+=2){\
139 dst_type *dst_1= (dst_type*)(dst[0] + (y+srcSliceY )*dstStride[0]);\
140 dst_type *dst_2= (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
141 dst_type av_unused *r, *b;\
142 dst_type *g;\
143 uint8_t *py_1= src[0] + y*srcStride[0];\
144 uint8_t *py_2= py_1 + srcStride[0];\
145 uint8_t *pu= src[1] + (y>>1)*srcStride[1];\
146 uint8_t *pv= src[2] + (y>>1)*srcStride[2];\
147 unsigned int h_size= c->dstW>>3;\
148 while (h_size--) {\
149 int av_unused U, V;\
150 int Y;\
152 #define EPILOG1(dst_delta)\
153 pu += 4;\
154 pv += 4;\
155 py_1 += 8;\
156 py_2 += 8;\
157 dst_1 += dst_delta;\
158 dst_2 += dst_delta;\
160 if (c->dstW & 4) {\
161 int av_unused Y, U, V;\
163 #define EPILOG2()\
166 return srcSliceH;\
169 #define EPILOG(dst_delta)\
170 EPILOG1(dst_delta)\
171 EPILOG2()
173 PROLOG(yuv2rgb_c_32, uint32_t)
174 RGB(0);
175 DST1(0);
176 DST2(0);
178 RGB(1);
179 DST2(1);
180 DST1(1);
182 RGB(2);
183 DST1(2);
184 DST2(2);
186 RGB(3);
187 DST2(3);
188 DST1(3);
189 EPILOG1(8)
190 RGB(0);
191 DST1(0);
192 DST2(0);
194 RGB(1);
195 DST2(1);
196 DST1(1);
197 EPILOG2()
199 PROLOG(yuv2rgb_c_24_rgb, uint8_t)
200 RGB(0);
201 DST1RGB(0);
202 DST2RGB(0);
204 RGB(1);
205 DST2RGB(1);
206 DST1RGB(1);
208 RGB(2);
209 DST1RGB(2);
210 DST2RGB(2);
212 RGB(3);
213 DST2RGB(3);
214 DST1RGB(3);
215 EPILOG1(24)
216 RGB(0);
217 DST1RGB(0);
218 DST2RGB(0);
220 RGB(1);
221 DST2RGB(1);
222 DST1RGB(1);
223 EPILOG2()
225 // only trivial mods from yuv2rgb_c_24_rgb
226 PROLOG(yuv2rgb_c_24_bgr, uint8_t)
227 RGB(0);
228 DST1BGR(0);
229 DST2BGR(0);
231 RGB(1);
232 DST2BGR(1);
233 DST1BGR(1);
235 RGB(2);
236 DST1BGR(2);
237 DST2BGR(2);
239 RGB(3);
240 DST2BGR(3);
241 DST1BGR(3);
242 EPILOG1(24)
243 RGB(0);
244 DST1BGR(0);
245 DST2BGR(0);
247 RGB(1);
248 DST2BGR(1);
249 DST1BGR(1);
250 EPILOG2()
252 // This is exactly the same code as yuv2rgb_c_32 except for the types of
253 // r, g, b, dst_1, dst_2
254 PROLOG(yuv2rgb_c_16, uint16_t)
255 RGB(0);
256 DST1(0);
257 DST2(0);
259 RGB(1);
260 DST2(1);
261 DST1(1);
263 RGB(2);
264 DST1(2);
265 DST2(2);
267 RGB(3);
268 DST2(3);
269 DST1(3);
270 EPILOG(8)
272 // This is exactly the same code as yuv2rgb_c_32 except for the types of
273 // r, g, b, dst_1, dst_2
274 PROLOG(yuv2rgb_c_8, uint8_t)
275 RGB(0);
276 DST1(0);
277 DST2(0);
279 RGB(1);
280 DST2(1);
281 DST1(1);
283 RGB(2);
284 DST1(2);
285 DST2(2);
287 RGB(3);
288 DST2(3);
289 DST1(3);
290 EPILOG(8)
292 // r, g, b, dst_1, dst_2
293 PROLOG(yuv2rgb_c_8_ordered_dither, uint8_t)
294 const uint8_t *d32= dither_8x8_32[y&7];
295 const uint8_t *d64= dither_8x8_73[y&7];
296 #define DST1bpp8(i,o) \
297 Y = py_1[2*i]; \
298 dst_1[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
299 Y = py_1[2*i+1]; \
300 dst_1[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
302 #define DST2bpp8(i,o) \
303 Y = py_2[2*i]; \
304 dst_2[2*i] = r[Y+d32[8+o]] + g[Y+d32[8+o]] + b[Y+d64[8+o]]; \
305 Y = py_2[2*i+1]; \
306 dst_2[2*i+1] = r[Y+d32[9+o]] + g[Y+d32[9+o]] + b[Y+d64[9+o]];
309 RGB(0);
310 DST1bpp8(0,0);
311 DST2bpp8(0,0);
313 RGB(1);
314 DST2bpp8(1,2);
315 DST1bpp8(1,2);
317 RGB(2);
318 DST1bpp8(2,4);
319 DST2bpp8(2,4);
321 RGB(3);
322 DST2bpp8(3,6);
323 DST1bpp8(3,6);
324 EPILOG(8)
327 // This is exactly the same code as yuv2rgb_c_32 except for the types of
328 // r, g, b, dst_1, dst_2
329 PROLOG(yuv2rgb_c_4, uint8_t)
330 int acc;
331 #define DST1_4(i) \
332 Y = py_1[2*i]; \
333 acc = r[Y] + g[Y] + b[Y]; \
334 Y = py_1[2*i+1]; \
335 acc |= (r[Y] + g[Y] + b[Y])<<4; \
336 dst_1[i] = acc;
338 #define DST2_4(i) \
339 Y = py_2[2*i]; \
340 acc = r[Y] + g[Y] + b[Y]; \
341 Y = py_2[2*i+1]; \
342 acc |= (r[Y] + g[Y] + b[Y])<<4; \
343 dst_2[i] = acc;
345 RGB(0);
346 DST1_4(0);
347 DST2_4(0);
349 RGB(1);
350 DST2_4(1);
351 DST1_4(1);
353 RGB(2);
354 DST1_4(2);
355 DST2_4(2);
357 RGB(3);
358 DST2_4(3);
359 DST1_4(3);
360 EPILOG(4)
362 PROLOG(yuv2rgb_c_4_ordered_dither, uint8_t)
363 const uint8_t *d64= dither_8x8_73[y&7];
364 const uint8_t *d128=dither_8x8_220[y&7];
365 int acc;
367 #define DST1bpp4(i,o) \
368 Y = py_1[2*i]; \
369 acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
370 Y = py_1[2*i+1]; \
371 acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4; \
372 dst_1[i]= acc;
374 #define DST2bpp4(i,o) \
375 Y = py_2[2*i]; \
376 acc = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
377 Y = py_2[2*i+1]; \
378 acc |= (r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]])<<4; \
379 dst_2[i]= acc;
382 RGB(0);
383 DST1bpp4(0,0);
384 DST2bpp4(0,0);
386 RGB(1);
387 DST2bpp4(1,2);
388 DST1bpp4(1,2);
390 RGB(2);
391 DST1bpp4(2,4);
392 DST2bpp4(2,4);
394 RGB(3);
395 DST2bpp4(3,6);
396 DST1bpp4(3,6);
397 EPILOG(4)
399 // This is exactly the same code as yuv2rgb_c_32 except for the types of
400 // r, g, b, dst_1, dst_2
401 PROLOG(yuv2rgb_c_4b, uint8_t)
402 RGB(0);
403 DST1(0);
404 DST2(0);
406 RGB(1);
407 DST2(1);
408 DST1(1);
410 RGB(2);
411 DST1(2);
412 DST2(2);
414 RGB(3);
415 DST2(3);
416 DST1(3);
417 EPILOG(8)
419 PROLOG(yuv2rgb_c_4b_ordered_dither, uint8_t)
420 const uint8_t *d64= dither_8x8_73[y&7];
421 const uint8_t *d128=dither_8x8_220[y&7];
423 #define DST1bpp4b(i,o) \
424 Y = py_1[2*i]; \
425 dst_1[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
426 Y = py_1[2*i+1]; \
427 dst_1[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
429 #define DST2bpp4b(i,o) \
430 Y = py_2[2*i]; \
431 dst_2[2*i] = r[Y+d128[8+o]] + g[Y+d64[8+o]] + b[Y+d128[8+o]]; \
432 Y = py_2[2*i+1]; \
433 dst_2[2*i+1] = r[Y+d128[9+o]] + g[Y+d64[9+o]] + b[Y+d128[9+o]];
436 RGB(0);
437 DST1bpp4b(0,0);
438 DST2bpp4b(0,0);
440 RGB(1);
441 DST2bpp4b(1,2);
442 DST1bpp4b(1,2);
444 RGB(2);
445 DST1bpp4b(2,4);
446 DST2bpp4b(2,4);
448 RGB(3);
449 DST2bpp4b(3,6);
450 DST1bpp4b(3,6);
451 EPILOG(8)
453 PROLOG(yuv2rgb_c_1_ordered_dither, uint8_t)
454 const uint8_t *d128=dither_8x8_220[y&7];
455 char out_1=0, out_2=0;
456 g= c->table_gU[128] + c->table_gV[128];
458 #define DST1bpp1(i,o) \
459 Y = py_1[2*i]; \
460 out_1+= out_1 + g[Y+d128[0+o]]; \
461 Y = py_1[2*i+1]; \
462 out_1+= out_1 + g[Y+d128[1+o]];
464 #define DST2bpp1(i,o) \
465 Y = py_2[2*i]; \
466 out_2+= out_2 + g[Y+d128[8+o]]; \
467 Y = py_2[2*i+1]; \
468 out_2+= out_2 + g[Y+d128[9+o]];
470 DST1bpp1(0,0);
471 DST2bpp1(0,0);
473 DST2bpp1(1,2);
474 DST1bpp1(1,2);
476 DST1bpp1(2,4);
477 DST2bpp1(2,4);
479 DST2bpp1(3,6);
480 DST1bpp1(3,6);
482 dst_1[0]= out_1;
483 dst_2[0]= out_2;
484 EPILOG(1)
486 SwsFunc yuv2rgb_get_func_ptr (SwsContext *c)
488 #if defined(HAVE_MMX2) || defined(HAVE_MMX)
489 if (c->flags & SWS_CPU_CAPS_MMX2){
490 switch(c->dstFormat){
491 case PIX_FMT_RGB32: return yuv420_rgb32_MMX2;
492 case PIX_FMT_BGR24: return yuv420_rgb24_MMX2;
493 case PIX_FMT_RGB565: return yuv420_rgb16_MMX2;
494 case PIX_FMT_RGB555: return yuv420_rgb15_MMX2;
497 if (c->flags & SWS_CPU_CAPS_MMX){
498 switch(c->dstFormat){
499 case PIX_FMT_RGB32: return yuv420_rgb32_MMX;
500 case PIX_FMT_BGR24: return yuv420_rgb24_MMX;
501 case PIX_FMT_RGB565: return yuv420_rgb16_MMX;
502 case PIX_FMT_RGB555: return yuv420_rgb15_MMX;
505 #endif
506 #ifdef HAVE_VIS
508 SwsFunc t= yuv2rgb_init_vis(c);
509 if (t) return t;
511 #endif
512 #ifdef CONFIG_MLIB
514 SwsFunc t= yuv2rgb_init_mlib(c);
515 if (t) return t;
517 #endif
518 #ifdef HAVE_ALTIVEC
519 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
521 SwsFunc t = yuv2rgb_init_altivec(c);
522 if (t) return t;
524 #endif
526 #ifdef ARCH_BFIN
527 if (c->flags & SWS_CPU_CAPS_BFIN)
529 SwsFunc t = ff_bfin_yuv2rgb_get_func_ptr (c);
530 if (t) return t;
532 #endif
534 av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found.\n");
536 switch(c->dstFormat){
537 case PIX_FMT_BGR32_1:
538 case PIX_FMT_RGB32_1:
539 case PIX_FMT_BGR32:
540 case PIX_FMT_RGB32: return yuv2rgb_c_32;
541 case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
542 case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
543 case PIX_FMT_RGB565:
544 case PIX_FMT_BGR565:
545 case PIX_FMT_RGB555:
546 case PIX_FMT_BGR555: return yuv2rgb_c_16;
547 case PIX_FMT_RGB8:
548 case PIX_FMT_BGR8: return yuv2rgb_c_8_ordered_dither;
549 case PIX_FMT_RGB4:
550 case PIX_FMT_BGR4: return yuv2rgb_c_4_ordered_dither;
551 case PIX_FMT_RGB4_BYTE:
552 case PIX_FMT_BGR4_BYTE: return yuv2rgb_c_4b_ordered_dither;
553 case PIX_FMT_MONOBLACK: return yuv2rgb_c_1_ordered_dither;
554 default:
555 assert(0);
557 return NULL;
560 static int div_round (int dividend, int divisor)
562 if (dividend > 0)
563 return (dividend + (divisor>>1)) / divisor;
564 else
565 return -((-dividend + (divisor>>1)) / divisor);
568 int yuv2rgb_c_init_tables (SwsContext *c, const int inv_table[4], int fullRange, int brightness, int contrast, int saturation)
570 const int isRgb = c->dstFormat==PIX_FMT_RGB32
571 || c->dstFormat==PIX_FMT_RGB32_1
572 || c->dstFormat==PIX_FMT_BGR24
573 || c->dstFormat==PIX_FMT_RGB565
574 || c->dstFormat==PIX_FMT_RGB555
575 || c->dstFormat==PIX_FMT_RGB8
576 || c->dstFormat==PIX_FMT_RGB4
577 || c->dstFormat==PIX_FMT_RGB4_BYTE
578 || c->dstFormat==PIX_FMT_MONOBLACK;
579 const int bpp = fmt_depth(c->dstFormat);
580 int i, base;
581 uint8_t table_Y[1024];
582 uint32_t *table_32 = 0;
583 uint16_t *table_16 = 0;
584 uint8_t *table_8 = 0;
585 uint8_t *table_332 = 0;
586 uint8_t *table_121 = 0;
587 uint8_t *table_1 = 0;
588 int entry_size = 0;
589 void *table_r = 0, *table_g = 0, *table_b = 0;
590 void *table_start;
592 int64_t crv = inv_table[0];
593 int64_t cbu = inv_table[1];
594 int64_t cgu = -inv_table[2];
595 int64_t cgv = -inv_table[3];
596 int64_t cy = 1<<16;
597 int64_t oy = 0;
599 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
600 if (!fullRange){
601 cy= (cy*255) / 219;
602 oy= 16<<16;
603 }else{
604 crv= (crv*224) / 255;
605 cbu= (cbu*224) / 255;
606 cgu= (cgu*224) / 255;
607 cgv= (cgv*224) / 255;
610 cy = (cy *contrast )>>16;
611 crv= (crv*contrast * saturation)>>32;
612 cbu= (cbu*contrast * saturation)>>32;
613 cgu= (cgu*contrast * saturation)>>32;
614 cgv= (cgv*contrast * saturation)>>32;
615 //printf("%lld %lld %lld %lld %lld\n", cy, crv, cbu, cgu, cgv);
616 oy -= 256*brightness;
618 for (i = 0; i < 1024; i++) {
619 int j;
621 j= (cy*(((i - 384)<<16) - oy) + (1<<31))>>32;
622 j = (j < 0) ? 0 : ((j > 255) ? 255 : j);
623 table_Y[i] = j;
626 switch (bpp) {
627 case 32:
628 table_start= table_32 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint32_t));
629 base= (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
631 entry_size = sizeof (uint32_t);
632 table_r = table_32 + 197;
633 table_b = table_32 + 197 + 685;
634 table_g = table_32 + 197 + 2*682;
636 for (i = -197; i < 256+197; i++)
637 ((uint32_t *)table_r)[i] = table_Y[i+384] << ((isRgb ? 16 : 0) + base);
638 for (i = -132; i < 256+132; i++)
639 ((uint32_t *)table_g)[i] = table_Y[i+384] << (8 + base);
640 for (i = -232; i < 256+232; i++)
641 ((uint32_t *)table_b)[i] = table_Y[i+384] << ((isRgb ? 0 : 16) + base);
642 break;
644 case 24:
645 table_start= table_8 = av_malloc ((256 + 2*232) * sizeof (uint8_t));
647 entry_size = sizeof (uint8_t);
648 table_r = table_g = table_b = table_8 + 232;
650 for (i = -232; i < 256+232; i++)
651 ((uint8_t * )table_b)[i] = table_Y[i+384];
652 break;
654 case 15:
655 case 16:
656 table_start= table_16 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint16_t));
658 entry_size = sizeof (uint16_t);
659 table_r = table_16 + 197;
660 table_b = table_16 + 197 + 685;
661 table_g = table_16 + 197 + 2*682;
663 for (i = -197; i < 256+197; i++) {
664 int j = table_Y[i+384] >> 3;
666 if (isRgb)
667 j <<= ((bpp==16) ? 11 : 10);
669 ((uint16_t *)table_r)[i] = j;
671 for (i = -132; i < 256+132; i++) {
672 int j = table_Y[i+384] >> ((bpp==16) ? 2 : 3);
674 ((uint16_t *)table_g)[i] = j << 5;
676 for (i = -232; i < 256+232; i++) {
677 int j = table_Y[i+384] >> 3;
679 if (!isRgb)
680 j <<= ((bpp==16) ? 11 : 10);
682 ((uint16_t *)table_b)[i] = j;
684 break;
686 case 8:
687 table_start= table_332 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
689 entry_size = sizeof (uint8_t);
690 table_r = table_332 + 197;
691 table_b = table_332 + 197 + 685;
692 table_g = table_332 + 197 + 2*682;
694 for (i = -197; i < 256+197; i++) {
695 int j = (table_Y[i+384 - 16] + 18)/36;
697 if (isRgb)
698 j <<= 5;
700 ((uint8_t *)table_r)[i] = j;
702 for (i = -132; i < 256+132; i++) {
703 int j = (table_Y[i+384 - 16] + 18)/36;
705 if (!isRgb)
706 j <<= 1;
708 ((uint8_t *)table_g)[i] = j << 2;
710 for (i = -232; i < 256+232; i++) {
711 int j = (table_Y[i+384 - 37] + 43)/85;
713 if (!isRgb)
714 j <<= 6;
716 ((uint8_t *)table_b)[i] = j;
718 break;
719 case 4:
720 case 4|128:
721 table_start= table_121 = av_malloc ((197 + 2*682 + 256 + 132) * sizeof (uint8_t));
723 entry_size = sizeof (uint8_t);
724 table_r = table_121 + 197;
725 table_b = table_121 + 197 + 685;
726 table_g = table_121 + 197 + 2*682;
728 for (i = -197; i < 256+197; i++) {
729 int j = table_Y[i+384 - 110] >> 7;
731 if (isRgb)
732 j <<= 3;
734 ((uint8_t *)table_r)[i] = j;
736 for (i = -132; i < 256+132; i++) {
737 int j = (table_Y[i+384 - 37]+ 43)/85;
739 ((uint8_t *)table_g)[i] = j << 1;
741 for (i = -232; i < 256+232; i++) {
742 int j =table_Y[i+384 - 110] >> 7;
744 if (!isRgb)
745 j <<= 3;
747 ((uint8_t *)table_b)[i] = j;
749 break;
751 case 1:
752 table_start= table_1 = av_malloc (256*2 * sizeof (uint8_t));
754 entry_size = sizeof (uint8_t);
755 table_g = table_1;
756 table_r = table_b = NULL;
758 for (i = 0; i < 256+256; i++) {
759 int j = table_Y[i + 384 - 110]>>7;
761 ((uint8_t *)table_g)[i] = j;
763 break;
765 default:
766 table_start= NULL;
767 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
768 //free mem?
769 return -1;
772 for (i = 0; i < 256; i++) {
773 c->table_rV[i] = (uint8_t *)table_r + entry_size * div_round (crv * (i-128), cy);
774 c->table_gU[i] = (uint8_t *)table_g + entry_size * div_round (cgu * (i-128), cy);
775 c->table_gV[i] = entry_size * div_round (cgv * (i-128), cy);
776 c->table_bU[i] = (uint8_t *)table_b + entry_size * div_round (cbu * (i-128), cy);
779 av_free(c->yuvTable);
780 c->yuvTable= table_start;
781 return 0;