2 * software YUV to RGB converter
4 * Copyright (C) 2009 Konstantin Shishkov
6 * 1,4,8bpp support and context / deglobalize stuff
7 * by Michael Niedermayer (michaelni@gmx.at)
9 * This file is part of FFmpeg.
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "swscale_internal.h"
35 #include "libavutil/x86_cpu.h"
36 #include "libavutil/bswap.h"
38 extern const uint8_t dither_4x4_16[4][8];
39 extern const uint8_t dither_8x8_32[8][8];
40 extern const uint8_t dither_8x8_73[8][8];
41 extern const uint8_t dither_8x8_220[8][8];
43 const int32_t ff_yuv2rgb_coeffs[8][4] = {
44 {117504, 138453, 13954, 34903}, /* no sequence_display_extension */
45 {117504, 138453, 13954, 34903}, /* ITU-R Rec. 709 (1990) */
46 {104597, 132201, 25675, 53279}, /* unspecified */
47 {104597, 132201, 25675, 53279}, /* reserved */
48 {104448, 132798, 24759, 53109}, /* FCC */
49 {104597, 132201, 25675, 53279}, /* ITU-R Rec. 624-4 System B, G */
50 {104597, 132201, 25675, 53279}, /* SMPTE 170M */
51 {117579, 136230, 16907, 35559} /* SMPTE 240M (1987) */
54 const int *sws_getCoefficients(int colorspace)
56 if (colorspace > 7 || colorspace < 0)
57 colorspace = SWS_CS_DEFAULT;
58 return ff_yuv2rgb_coeffs[colorspace];
61 #define LOADCHROMA(i) \
64 r = (void *)c->table_rV[V]; \
65 g = (void *)(c->table_gU[U] + c->table_gV[V]); \
66 b = (void *)c->table_bU[U];
68 #define PUTRGB(dst,src,i) \
70 dst[2*i ] = r[Y] + g[Y] + b[Y]; \
72 dst[2*i+1] = r[Y] + g[Y] + b[Y];
74 #define PUTRGB24(dst,src,i) \
76 dst[6*i+0] = r[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = b[Y]; \
78 dst[6*i+3] = r[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = b[Y];
80 #define PUTBGR24(dst,src,i) \
82 dst[6*i+0] = b[Y]; dst[6*i+1] = g[Y]; dst[6*i+2] = r[Y]; \
84 dst[6*i+3] = b[Y]; dst[6*i+4] = g[Y]; dst[6*i+5] = r[Y];
86 #define PUTRGBA(dst,ysrc,asrc,i,s) \
88 dst[2*i ] = r[Y] + g[Y] + b[Y] + (asrc[2*i ]<<s); \
90 dst[2*i+1] = r[Y] + g[Y] + b[Y] + (asrc[2*i+1]<<s);
92 #define PUTRGB48(dst,src,i) \
94 dst[12*i+ 0] = dst[12*i+ 1] = r[Y]; \
95 dst[12*i+ 2] = dst[12*i+ 3] = g[Y]; \
96 dst[12*i+ 4] = dst[12*i+ 5] = b[Y]; \
98 dst[12*i+ 6] = dst[12*i+ 7] = r[Y]; \
99 dst[12*i+ 8] = dst[12*i+ 9] = g[Y]; \
100 dst[12*i+10] = dst[12*i+11] = b[Y];
102 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
103 static int func_name(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY, \
104 int srcSliceH, uint8_t* dst[], int dstStride[]) \
108 if (!alpha && c->srcFormat == PIX_FMT_YUV422P) {\
112 for (y=0; y<srcSliceH; y+=2) {\
113 dst_type *dst_1 = (dst_type*)(dst[0] + (y+srcSliceY )*dstStride[0]);\
114 dst_type *dst_2 = (dst_type*)(dst[0] + (y+srcSliceY+1)*dstStride[0]);\
115 dst_type av_unused *r, *b;\
117 const uint8_t *py_1 = src[0] + y*srcStride[0];\
118 const uint8_t *py_2 = py_1 + srcStride[0];\
119 const uint8_t *pu = src[1] + (y>>1)*srcStride[1];\
120 const uint8_t *pv = src[2] + (y>>1)*srcStride[2];\
121 const uint8_t av_unused *pa_1, *pa_2;\
122 unsigned int h_size = c->dstW>>3;\
124 pa_1 = src[3] + y*srcStride[3];\
125 pa_2 = pa_1 + srcStride[3];\
131 #define ENDYUV2RGBLINE(dst_delta)\
140 int av_unused Y, U, V;\
142 #define ENDYUV2RGBFUNC()\
148 #define CLOSEYUV2RGBFUNC(dst_delta)\
149 ENDYUV2RGBLINE(dst_delta)\
152 YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
154 PUTRGB48(dst_1,py_1,0);
155 PUTRGB48(dst_2,py_2,0);
158 PUTRGB48(dst_2,py_2,1);
159 PUTRGB48(dst_1,py_1,1);
162 PUTRGB48(dst_1,py_1,2);
163 PUTRGB48(dst_2,py_2,2);
166 PUTRGB48(dst_2,py_2,3);
167 PUTRGB48(dst_1,py_1,3);
170 PUTRGB48(dst_1,py_1,0);
171 PUTRGB48(dst_2,py_2,0);
174 PUTRGB48(dst_2,py_2,1);
175 PUTRGB48(dst_1,py_1,1);
178 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
180 PUTRGB(dst_1,py_1,0);
181 PUTRGB(dst_2,py_2,0);
184 PUTRGB(dst_2,py_2,1);
185 PUTRGB(dst_1,py_1,1);
188 PUTRGB(dst_1,py_1,2);
189 PUTRGB(dst_2,py_2,2);
192 PUTRGB(dst_2,py_2,3);
193 PUTRGB(dst_1,py_1,3);
196 PUTRGB(dst_1,py_1,0);
197 PUTRGB(dst_2,py_2,0);
200 PUTRGB(dst_2,py_2,1);
201 PUTRGB(dst_1,py_1,1);
204 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
206 PUTRGBA(dst_1,py_1,pa_1,0,24);
207 PUTRGBA(dst_2,py_2,pa_2,0,24);
210 PUTRGBA(dst_2,py_2,pa_1,1,24);
211 PUTRGBA(dst_1,py_1,pa_2,1,24);
214 PUTRGBA(dst_1,py_1,pa_1,2,24);
215 PUTRGBA(dst_2,py_2,pa_2,2,24);
218 PUTRGBA(dst_2,py_2,pa_1,3,24);
219 PUTRGBA(dst_1,py_1,pa_2,3,24);
224 PUTRGBA(dst_1,py_1,pa_1,0,24);
225 PUTRGBA(dst_2,py_2,pa_2,0,24);
228 PUTRGBA(dst_2,py_2,pa_1,1,24);
229 PUTRGBA(dst_1,py_1,pa_2,1,24);
232 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
234 PUTRGBA(dst_1,py_1,pa_1,0,0);
235 PUTRGBA(dst_2,py_2,pa_2,0,0);
238 PUTRGBA(dst_2,py_2,pa_2,1,0);
239 PUTRGBA(dst_1,py_1,pa_1,1,0);
242 PUTRGBA(dst_1,py_1,pa_1,2,0);
243 PUTRGBA(dst_2,py_2,pa_2,2,0);
246 PUTRGBA(dst_2,py_2,pa_2,3,0);
247 PUTRGBA(dst_1,py_1,pa_1,3,0);
252 PUTRGBA(dst_1,py_1,pa_1,0,0);
253 PUTRGBA(dst_2,py_2,pa_2,0,0);
256 PUTRGBA(dst_2,py_2,pa_2,1,0);
257 PUTRGBA(dst_1,py_1,pa_1,1,0);
260 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
262 PUTRGB24(dst_1,py_1,0);
263 PUTRGB24(dst_2,py_2,0);
266 PUTRGB24(dst_2,py_2,1);
267 PUTRGB24(dst_1,py_1,1);
270 PUTRGB24(dst_1,py_1,2);
271 PUTRGB24(dst_2,py_2,2);
274 PUTRGB24(dst_2,py_2,3);
275 PUTRGB24(dst_1,py_1,3);
278 PUTRGB24(dst_1,py_1,0);
279 PUTRGB24(dst_2,py_2,0);
282 PUTRGB24(dst_2,py_2,1);
283 PUTRGB24(dst_1,py_1,1);
286 // only trivial mods from yuv2rgb_c_24_rgb
287 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
289 PUTBGR24(dst_1,py_1,0);
290 PUTBGR24(dst_2,py_2,0);
293 PUTBGR24(dst_2,py_2,1);
294 PUTBGR24(dst_1,py_1,1);
297 PUTBGR24(dst_1,py_1,2);
298 PUTBGR24(dst_2,py_2,2);
301 PUTBGR24(dst_2,py_2,3);
302 PUTBGR24(dst_1,py_1,3);
305 PUTBGR24(dst_1,py_1,0);
306 PUTBGR24(dst_2,py_2,0);
309 PUTBGR24(dst_2,py_2,1);
310 PUTBGR24(dst_1,py_1,1);
313 // This is exactly the same code as yuv2rgb_c_32 except for the types of
314 // r, g, b, dst_1, dst_2
315 YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
317 PUTRGB(dst_1,py_1,0);
318 PUTRGB(dst_2,py_2,0);
321 PUTRGB(dst_2,py_2,1);
322 PUTRGB(dst_1,py_1,1);
325 PUTRGB(dst_1,py_1,2);
326 PUTRGB(dst_2,py_2,2);
329 PUTRGB(dst_2,py_2,3);
330 PUTRGB(dst_1,py_1,3);
333 #if 0 // Currently unused
334 // This is exactly the same code as yuv2rgb_c_32 except for the types of
335 // r, g, b, dst_1, dst_2
336 YUV2RGBFUNC(yuv2rgb_c_8, uint8_t, 0)
338 PUTRGB(dst_1,py_1,0);
339 PUTRGB(dst_2,py_2,0);
342 PUTRGB(dst_2,py_2,1);
343 PUTRGB(dst_1,py_1,1);
346 PUTRGB(dst_1,py_1,2);
347 PUTRGB(dst_2,py_2,2);
350 PUTRGB(dst_2,py_2,3);
351 PUTRGB(dst_1,py_1,3);
355 // r, g, b, dst_1, dst_2
356 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
357 const uint8_t *d16 = dither_4x4_16[y&3];
358 #define PUTRGB12(dst,src,i,o) \
360 dst[2*i] = r[Y+d16[0+o]] + g[Y+d16[0+o]] + b[Y+d16[0+o]]; \
362 dst[2*i+1] = r[Y+d16[1+o]] + g[Y+d16[1+o]] + b[Y+d16[1+o]];
365 PUTRGB12(dst_1,py_1,0,0);
366 PUTRGB12(dst_2,py_2,0,0+8);
369 PUTRGB12(dst_2,py_2,1,2+8);
370 PUTRGB12(dst_1,py_1,1,2);
373 PUTRGB12(dst_1,py_1,2,4);
374 PUTRGB12(dst_2,py_2,2,4+8);
377 PUTRGB12(dst_2,py_2,3,6+8);
378 PUTRGB12(dst_1,py_1,3,6);
381 // r, g, b, dst_1, dst_2
382 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
383 const uint8_t *d32 = dither_8x8_32[y&7];
384 const uint8_t *d64 = dither_8x8_73[y&7];
385 #define PUTRGB8(dst,src,i,o) \
387 dst[2*i] = r[Y+d32[0+o]] + g[Y+d32[0+o]] + b[Y+d64[0+o]]; \
389 dst[2*i+1] = r[Y+d32[1+o]] + g[Y+d32[1+o]] + b[Y+d64[1+o]];
392 PUTRGB8(dst_1,py_1,0,0);
393 PUTRGB8(dst_2,py_2,0,0+8);
396 PUTRGB8(dst_2,py_2,1,2+8);
397 PUTRGB8(dst_1,py_1,1,2);
400 PUTRGB8(dst_1,py_1,2,4);
401 PUTRGB8(dst_2,py_2,2,4+8);
404 PUTRGB8(dst_2,py_2,3,6+8);
405 PUTRGB8(dst_1,py_1,3,6);
408 #if 0 // Currently unused
409 // This is exactly the same code as yuv2rgb_c_32 except for the types of
410 // r, g, b, dst_1, dst_2
411 YUV2RGBFUNC(yuv2rgb_c_4, uint8_t, 0)
413 #define PUTRGB4(dst,src,i) \
415 acc = r[Y] + g[Y] + b[Y]; \
417 acc |= (r[Y] + g[Y] + b[Y])<<4; \
421 PUTRGB4(dst_1,py_1,0);
422 PUTRGB4(dst_2,py_2,0);
425 PUTRGB4(dst_2,py_2,1);
426 PUTRGB4(dst_1,py_1,1);
429 PUTRGB4(dst_1,py_1,2);
430 PUTRGB4(dst_2,py_2,2);
433 PUTRGB4(dst_2,py_2,3);
434 PUTRGB4(dst_1,py_1,3);
438 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
439 const uint8_t *d64 = dither_8x8_73[y&7];
440 const uint8_t *d128 = dither_8x8_220[y&7];
443 #define PUTRGB4D(dst,src,i,o) \
445 acc = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
447 acc |= (r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]])<<4; \
451 PUTRGB4D(dst_1,py_1,0,0);
452 PUTRGB4D(dst_2,py_2,0,0+8);
455 PUTRGB4D(dst_2,py_2,1,2+8);
456 PUTRGB4D(dst_1,py_1,1,2);
459 PUTRGB4D(dst_1,py_1,2,4);
460 PUTRGB4D(dst_2,py_2,2,4+8);
463 PUTRGB4D(dst_2,py_2,3,6+8);
464 PUTRGB4D(dst_1,py_1,3,6);
467 #if 0 // Currently unused
468 // This is exactly the same code as yuv2rgb_c_32 except for the types of
469 // r, g, b, dst_1, dst_2
470 YUV2RGBFUNC(yuv2rgb_c_4b, uint8_t, 0)
472 PUTRGB(dst_1,py_1,0);
473 PUTRGB(dst_2,py_2,0);
476 PUTRGB(dst_2,py_2,1);
477 PUTRGB(dst_1,py_1,1);
480 PUTRGB(dst_1,py_1,2);
481 PUTRGB(dst_2,py_2,2);
484 PUTRGB(dst_2,py_2,3);
485 PUTRGB(dst_1,py_1,3);
489 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
490 const uint8_t *d64 = dither_8x8_73[y&7];
491 const uint8_t *d128 = dither_8x8_220[y&7];
493 #define PUTRGB4DB(dst,src,i,o) \
495 dst[2*i] = r[Y+d128[0+o]] + g[Y+d64[0+o]] + b[Y+d128[0+o]]; \
497 dst[2*i+1] = r[Y+d128[1+o]] + g[Y+d64[1+o]] + b[Y+d128[1+o]];
500 PUTRGB4DB(dst_1,py_1,0,0);
501 PUTRGB4DB(dst_2,py_2,0,0+8);
504 PUTRGB4DB(dst_2,py_2,1,2+8);
505 PUTRGB4DB(dst_1,py_1,1,2);
508 PUTRGB4DB(dst_1,py_1,2,4);
509 PUTRGB4DB(dst_2,py_2,2,4+8);
512 PUTRGB4DB(dst_2,py_2,3,6+8);
513 PUTRGB4DB(dst_1,py_1,3,6);
516 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
517 const uint8_t *d128 = dither_8x8_220[y&7];
518 char out_1 = 0, out_2 = 0;
519 g= c->table_gU[128] + c->table_gV[128];
521 #define PUTRGB1(out,src,i,o) \
523 out+= out + g[Y+d128[0+o]]; \
525 out+= out + g[Y+d128[1+o]];
527 PUTRGB1(out_1,py_1,0,0);
528 PUTRGB1(out_2,py_2,0,0+8);
530 PUTRGB1(out_2,py_2,1,2+8);
531 PUTRGB1(out_1,py_1,1,2);
533 PUTRGB1(out_1,py_1,2,4);
534 PUTRGB1(out_2,py_2,2,4+8);
536 PUTRGB1(out_2,py_2,3,6+8);
537 PUTRGB1(out_1,py_1,3,6);
543 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
547 t = ff_yuv2rgb_init_mmx(c);
550 t = ff_yuv2rgb_init_vis(c);
553 t = ff_yuv2rgb_init_mlib(c);
556 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
557 t = ff_yuv2rgb_init_altivec(c);
561 if (c->flags & SWS_CPU_CAPS_BFIN)
562 t = ff_yuv2rgb_get_func_ptr_bfin(c);
568 av_log(c, AV_LOG_WARNING, "No accelerated colorspace conversion found from %s to %s.\n", sws_format_name(c->srcFormat), sws_format_name(c->dstFormat));
570 switch (c->dstFormat) {
571 case PIX_FMT_RGB48BE:
572 case PIX_FMT_RGB48LE: return yuv2rgb_c_48;
574 case PIX_FMT_ABGR: if (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) return yuva2argb_c;
576 case PIX_FMT_BGRA: return (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P) ? yuva2rgba_c : yuv2rgb_c_32;
577 case PIX_FMT_RGB24: return yuv2rgb_c_24_rgb;
578 case PIX_FMT_BGR24: return yuv2rgb_c_24_bgr;
582 case PIX_FMT_BGR555: return yuv2rgb_c_16;
584 case PIX_FMT_BGR444: return yuv2rgb_c_12_ordered_dither;
586 case PIX_FMT_BGR8: return yuv2rgb_c_8_ordered_dither;
588 case PIX_FMT_BGR4: return yuv2rgb_c_4_ordered_dither;
589 case PIX_FMT_RGB4_BYTE:
590 case PIX_FMT_BGR4_BYTE: return yuv2rgb_c_4b_ordered_dither;
591 case PIX_FMT_MONOBLACK: return yuv2rgb_c_1_ordered_dither;
598 static void fill_table(uint8_t* table[256], const int elemsize, const int inc, uint8_t *y_table)
603 y_table -= elemsize * (inc >> 9);
605 for (i = 0; i < 256; i++) {
606 table[i] = y_table + elemsize * (cb >> 16);
611 static void fill_gv_table(int table[256], const int elemsize, const int inc)
615 int off = -(inc >> 9);
617 for (i = 0; i < 256; i++) {
618 table[i] = elemsize * (off + (cb >> 16));
623 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4], int fullRange,
624 int brightness, int contrast, int saturation)
626 const int isRgb = c->dstFormat==PIX_FMT_RGB32
627 || c->dstFormat==PIX_FMT_RGB32_1
628 || c->dstFormat==PIX_FMT_BGR24
629 || c->dstFormat==PIX_FMT_RGB565BE
630 || c->dstFormat==PIX_FMT_RGB565LE
631 || c->dstFormat==PIX_FMT_RGB555BE
632 || c->dstFormat==PIX_FMT_RGB555LE
633 || c->dstFormat==PIX_FMT_RGB444BE
634 || c->dstFormat==PIX_FMT_RGB444LE
635 || c->dstFormat==PIX_FMT_RGB8
636 || c->dstFormat==PIX_FMT_RGB4
637 || c->dstFormat==PIX_FMT_RGB4_BYTE
638 || c->dstFormat==PIX_FMT_MONOBLACK;
639 const int isNotNe = c->dstFormat==PIX_FMT_NE(RGB565LE,RGB565BE)
640 || c->dstFormat==PIX_FMT_NE(RGB555LE,RGB555BE)
641 || c->dstFormat==PIX_FMT_NE(RGB444LE,RGB444BE)
642 || c->dstFormat==PIX_FMT_NE(BGR565LE,BGR565BE)
643 || c->dstFormat==PIX_FMT_NE(BGR555LE,BGR555BE)
644 || c->dstFormat==PIX_FMT_NE(BGR444LE,BGR444BE);
645 const int bpp = c->dstFormatBpp;
649 int i, base, rbase, gbase, bbase, abase, needAlpha;
650 const int yoffs = fullRange ? 384 : 326;
652 int64_t crv = inv_table[0];
653 int64_t cbu = inv_table[1];
654 int64_t cgu = -inv_table[2];
655 int64_t cgv = -inv_table[3];
665 crv = (crv*224) / 255;
666 cbu = (cbu*224) / 255;
667 cgu = (cgu*224) / 255;
668 cgv = (cgv*224) / 255;
671 cy = (cy *contrast ) >> 16;
672 crv = (crv*contrast * saturation) >> 32;
673 cbu = (cbu*contrast * saturation) >> 32;
674 cgu = (cgu*contrast * saturation) >> 32;
675 cgv = (cgv*contrast * saturation) >> 32;
676 oy -= 256*brightness;
678 //scale coefficients by cy
679 crv = ((crv << 16) + 0x8000) / cy;
680 cbu = ((cbu << 16) + 0x8000) / cy;
681 cgu = ((cgu << 16) + 0x8000) / cy;
682 cgv = ((cgv << 16) + 0x8000) / cy;
684 av_free(c->yuvTable);
688 c->yuvTable = av_malloc(1024);
689 y_table = c->yuvTable;
690 yb = -(384<<16) - oy;
691 for (i = 0; i < 1024-110; i++) {
692 y_table[i+110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
695 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
696 fill_gv_table(c->table_gV, 1, cgv);
700 rbase = isRgb ? 3 : 0;
702 bbase = isRgb ? 0 : 3;
703 c->yuvTable = av_malloc(1024*3);
704 y_table = c->yuvTable;
705 yb = -(384<<16) - oy;
706 for (i = 0; i < 1024-110; i++) {
707 int yval = av_clip_uint8((yb + 0x8000) >> 16);
708 y_table[i+110 ] = (yval >> 7) << rbase;
709 y_table[i+ 37+1024] = ((yval + 43) / 85) << gbase;
710 y_table[i+110+2048] = (yval >> 7) << bbase;
713 fill_table(c->table_rV, 1, crv, y_table + yoffs);
714 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
715 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
716 fill_gv_table(c->table_gV, 1, cgv);
719 rbase = isRgb ? 5 : 0;
720 gbase = isRgb ? 2 : 3;
721 bbase = isRgb ? 0 : 6;
722 c->yuvTable = av_malloc(1024*3);
723 y_table = c->yuvTable;
724 yb = -(384<<16) - oy;
725 for (i = 0; i < 1024-38; i++) {
726 int yval = av_clip_uint8((yb + 0x8000) >> 16);
727 y_table[i+16 ] = ((yval + 18) / 36) << rbase;
728 y_table[i+16+1024] = ((yval + 18) / 36) << gbase;
729 y_table[i+37+2048] = ((yval + 43) / 85) << bbase;
732 fill_table(c->table_rV, 1, crv, y_table + yoffs);
733 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
734 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
735 fill_gv_table(c->table_gV, 1, cgv);
738 rbase = isRgb ? 8 : 0;
740 bbase = isRgb ? 0 : 8;
741 c->yuvTable = av_malloc(1024*3*2);
742 y_table16 = c->yuvTable;
743 yb = -(384<<16) - oy;
744 for (i = 0; i < 1024; i++) {
745 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
746 y_table16[i ] = (yval >> 4) << rbase;
747 y_table16[i+1024] = (yval >> 4) << gbase;
748 y_table16[i+2048] = (yval >> 4) << bbase;
752 for (i = 0; i < 1024*3; i++)
753 y_table16[i] = bswap_16(y_table16[i]);
754 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
755 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
756 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
757 fill_gv_table(c->table_gV, 2, cgv);
761 rbase = isRgb ? bpp - 5 : 0;
763 bbase = isRgb ? 0 : (bpp - 5);
764 c->yuvTable = av_malloc(1024*3*2);
765 y_table16 = c->yuvTable;
766 yb = -(384<<16) - oy;
767 for (i = 0; i < 1024; i++) {
768 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
769 y_table16[i ] = (yval >> 3) << rbase;
770 y_table16[i+1024] = (yval >> (18 - bpp)) << gbase;
771 y_table16[i+2048] = (yval >> 3) << bbase;
775 for (i = 0; i < 1024*3; i++)
776 y_table16[i] = bswap_16(y_table16[i]);
777 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
778 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
779 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
780 fill_gv_table(c->table_gV, 2, cgv);
784 c->yuvTable = av_malloc(1024);
785 y_table = c->yuvTable;
786 yb = -(384<<16) - oy;
787 for (i = 0; i < 1024; i++) {
788 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
791 fill_table(c->table_rV, 1, crv, y_table + yoffs);
792 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
793 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
794 fill_gv_table(c->table_gV, 1, cgv);
797 base = (c->dstFormat == PIX_FMT_RGB32_1 || c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
798 rbase = base + (isRgb ? 16 : 0);
800 bbase = base + (isRgb ? 0 : 16);
801 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
803 abase = (base + 24) & 31;
804 c->yuvTable = av_malloc(1024*3*4);
805 y_table32 = c->yuvTable;
806 yb = -(384<<16) - oy;
807 for (i = 0; i < 1024; i++) {
808 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
809 y_table32[i ] = (yval << rbase) + (needAlpha ? 0 : (255 << abase));
810 y_table32[i+1024] = yval << gbase;
811 y_table32[i+2048] = yval << bbase;
814 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
815 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
816 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
817 fill_gv_table(c->table_gV, 4, cgv);
821 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);