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 Libav.
11 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "libavutil/cpu.h"
32 #include "libavutil/bswap.h"
36 #include "swscale_internal.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]; \
77 dst[6 * i + 1] = g[Y]; \
78 dst[6 * i + 2] = b[Y]; \
80 dst[6 * i + 3] = r[Y]; \
81 dst[6 * i + 4] = g[Y]; \
82 dst[6 * i + 5] = b[Y];
84 #define PUTBGR24(dst, src, i) \
86 dst[6 * i + 0] = b[Y]; \
87 dst[6 * i + 1] = g[Y]; \
88 dst[6 * i + 2] = r[Y]; \
90 dst[6 * i + 3] = b[Y]; \
91 dst[6 * i + 4] = g[Y]; \
92 dst[6 * i + 5] = r[Y];
94 #define PUTRGBA(dst, ysrc, asrc, i, s) \
96 dst[2 * i] = r[Y] + g[Y] + b[Y] + (asrc[2 * i] << s); \
97 Y = ysrc[2 * i + 1]; \
98 dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + (asrc[2 * i + 1] << s);
100 #define PUTRGB48(dst, src, i) \
102 dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
103 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
104 dst[12 * i + 4] = dst[12 * i + 5] = b[Y]; \
105 Y = src[ 2 * i + 1]; \
106 dst[12 * i + 6] = dst[12 * i + 7] = r[Y]; \
107 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
108 dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
110 #define PUTBGR48(dst, src, i) \
112 dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
113 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
114 dst[12 * i + 4] = dst[12 * i + 5] = r[Y]; \
115 Y = src[2 * i + 1]; \
116 dst[12 * i + 6] = dst[12 * i + 7] = b[Y]; \
117 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
118 dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
120 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
121 static int func_name(SwsContext *c, const uint8_t *src[], \
122 int srcStride[], int srcSliceY, int srcSliceH, \
123 uint8_t *dst[], int dstStride[]) \
127 if (!alpha && c->srcFormat == PIX_FMT_YUV422P) { \
131 for (y = 0; y < srcSliceH; y += 2) { \
133 (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]); \
135 (dst_type *)(dst[0] + (y + srcSliceY + 1) * dstStride[0]); \
136 dst_type av_unused *r, *g, *b; \
137 const uint8_t *py_1 = src[0] + y * srcStride[0]; \
138 const uint8_t *py_2 = py_1 + srcStride[0]; \
139 const uint8_t *pu = src[1] + (y >> 1) * srcStride[1]; \
140 const uint8_t *pv = src[2] + (y >> 1) * srcStride[2]; \
141 const uint8_t av_unused *pa_1, *pa_2; \
142 unsigned int h_size = c->dstW >> 3; \
144 pa_1 = src[3] + y * srcStride[3]; \
145 pa_2 = pa_1 + srcStride[3]; \
148 int av_unused U, V, Y; \
150 #define ENDYUV2RGBLINE(dst_delta) \
155 dst_1 += dst_delta; \
156 dst_2 += dst_delta; \
159 int av_unused Y, U, V; \
161 #define ENDYUV2RGBFUNC() \
167 #define CLOSEYUV2RGBFUNC(dst_delta) \
168 ENDYUV2RGBLINE(dst_delta) \
171 YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
173 PUTRGB48(dst_1, py_1, 0);
174 PUTRGB48(dst_2, py_2, 0);
177 PUTRGB48(dst_2, py_2, 1);
178 PUTRGB48(dst_1, py_1, 1);
181 PUTRGB48(dst_1, py_1, 2);
182 PUTRGB48(dst_2, py_2, 2);
185 PUTRGB48(dst_2, py_2, 3);
186 PUTRGB48(dst_1, py_1, 3);
189 PUTRGB48(dst_1, py_1, 0);
190 PUTRGB48(dst_2, py_2, 0);
193 PUTRGB48(dst_2, py_2, 1);
194 PUTRGB48(dst_1, py_1, 1);
197 YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
199 PUTBGR48(dst_1, py_1, 0);
200 PUTBGR48(dst_2, py_2, 0);
203 PUTBGR48(dst_2, py_2, 1);
204 PUTBGR48(dst_1, py_1, 1);
207 PUTBGR48(dst_1, py_1, 2);
208 PUTBGR48(dst_2, py_2, 2);
211 PUTBGR48(dst_2, py_2, 3);
212 PUTBGR48(dst_1, py_1, 3);
215 PUTBGR48(dst_1, py_1, 0);
216 PUTBGR48(dst_2, py_2, 0);
219 PUTBGR48(dst_2, py_2, 1);
220 PUTBGR48(dst_1, py_1, 1);
223 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
225 PUTRGB(dst_1, py_1, 0);
226 PUTRGB(dst_2, py_2, 0);
229 PUTRGB(dst_2, py_2, 1);
230 PUTRGB(dst_1, py_1, 1);
233 PUTRGB(dst_1, py_1, 2);
234 PUTRGB(dst_2, py_2, 2);
237 PUTRGB(dst_2, py_2, 3);
238 PUTRGB(dst_1, py_1, 3);
241 PUTRGB(dst_1, py_1, 0);
242 PUTRGB(dst_2, py_2, 0);
245 PUTRGB(dst_2, py_2, 1);
246 PUTRGB(dst_1, py_1, 1);
249 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
251 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
252 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
255 PUTRGBA(dst_2, py_2, pa_1, 1, 24);
256 PUTRGBA(dst_1, py_1, pa_2, 1, 24);
259 PUTRGBA(dst_1, py_1, pa_1, 2, 24);
260 PUTRGBA(dst_2, py_2, pa_2, 2, 24);
263 PUTRGBA(dst_2, py_2, pa_1, 3, 24);
264 PUTRGBA(dst_1, py_1, pa_2, 3, 24);
269 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
270 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
273 PUTRGBA(dst_2, py_2, pa_1, 1, 24);
274 PUTRGBA(dst_1, py_1, pa_2, 1, 24);
277 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
279 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
280 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
283 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
284 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
287 PUTRGBA(dst_1, py_1, pa_1, 2, 0);
288 PUTRGBA(dst_2, py_2, pa_2, 2, 0);
291 PUTRGBA(dst_2, py_2, pa_2, 3, 0);
292 PUTRGBA(dst_1, py_1, pa_1, 3, 0);
297 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
298 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
301 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
302 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
305 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
307 PUTRGB24(dst_1, py_1, 0);
308 PUTRGB24(dst_2, py_2, 0);
311 PUTRGB24(dst_2, py_2, 1);
312 PUTRGB24(dst_1, py_1, 1);
315 PUTRGB24(dst_1, py_1, 2);
316 PUTRGB24(dst_2, py_2, 2);
319 PUTRGB24(dst_2, py_2, 3);
320 PUTRGB24(dst_1, py_1, 3);
323 PUTRGB24(dst_1, py_1, 0);
324 PUTRGB24(dst_2, py_2, 0);
327 PUTRGB24(dst_2, py_2, 1);
328 PUTRGB24(dst_1, py_1, 1);
331 // only trivial mods from yuv2rgb_c_24_rgb
332 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
334 PUTBGR24(dst_1, py_1, 0);
335 PUTBGR24(dst_2, py_2, 0);
338 PUTBGR24(dst_2, py_2, 1);
339 PUTBGR24(dst_1, py_1, 1);
342 PUTBGR24(dst_1, py_1, 2);
343 PUTBGR24(dst_2, py_2, 2);
346 PUTBGR24(dst_2, py_2, 3);
347 PUTBGR24(dst_1, py_1, 3);
350 PUTBGR24(dst_1, py_1, 0);
351 PUTBGR24(dst_2, py_2, 0);
354 PUTBGR24(dst_2, py_2, 1);
355 PUTBGR24(dst_1, py_1, 1);
358 // This is exactly the same code as yuv2rgb_c_32 except for the types of
359 // r, g, b, dst_1, dst_2
360 YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
362 PUTRGB(dst_1, py_1, 0);
363 PUTRGB(dst_2, py_2, 0);
366 PUTRGB(dst_2, py_2, 1);
367 PUTRGB(dst_1, py_1, 1);
370 PUTRGB(dst_1, py_1, 2);
371 PUTRGB(dst_2, py_2, 2);
374 PUTRGB(dst_2, py_2, 3);
375 PUTRGB(dst_1, py_1, 3);
378 // r, g, b, dst_1, dst_2
379 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
380 const uint8_t *d16 = dither_4x4_16[y & 3];
382 #define PUTRGB12(dst, src, i, o) \
384 dst[2 * i] = r[Y + d16[0 + o]] + \
385 g[Y + d16[0 + o]] + \
387 Y = src[2 * i + 1]; \
388 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
389 g[Y + d16[1 + o]] + \
393 PUTRGB12(dst_1, py_1, 0, 0);
394 PUTRGB12(dst_2, py_2, 0, 0 + 8);
397 PUTRGB12(dst_2, py_2, 1, 2 + 8);
398 PUTRGB12(dst_1, py_1, 1, 2);
401 PUTRGB12(dst_1, py_1, 2, 4);
402 PUTRGB12(dst_2, py_2, 2, 4 + 8);
405 PUTRGB12(dst_2, py_2, 3, 6 + 8);
406 PUTRGB12(dst_1, py_1, 3, 6);
409 // r, g, b, dst_1, dst_2
410 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
411 const uint8_t *d32 = dither_8x8_32[y & 7];
412 const uint8_t *d64 = dither_8x8_73[y & 7];
414 #define PUTRGB8(dst, src, i, o) \
416 dst[2 * i] = r[Y + d32[0 + o]] + \
417 g[Y + d32[0 + o]] + \
419 Y = src[2 * i + 1]; \
420 dst[2 * i + 1] = r[Y + d32[1 + o]] + \
421 g[Y + d32[1 + o]] + \
425 PUTRGB8(dst_1, py_1, 0, 0);
426 PUTRGB8(dst_2, py_2, 0, 0 + 8);
429 PUTRGB8(dst_2, py_2, 1, 2 + 8);
430 PUTRGB8(dst_1, py_1, 1, 2);
433 PUTRGB8(dst_1, py_1, 2, 4);
434 PUTRGB8(dst_2, py_2, 2, 4 + 8);
437 PUTRGB8(dst_2, py_2, 3, 6 + 8);
438 PUTRGB8(dst_1, py_1, 3, 6);
441 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
442 const uint8_t * d64 = dither_8x8_73[y & 7];
443 const uint8_t *d128 = dither_8x8_220[y & 7];
446 #define PUTRGB4D(dst, src, i, o) \
448 acc = r[Y + d128[0 + o]] + \
449 g[Y + d64[0 + o]] + \
450 b[Y + d128[0 + o]]; \
451 Y = src[2 * i + 1]; \
452 acc |= (r[Y + d128[1 + o]] + \
453 g[Y + d64[1 + o]] + \
454 b[Y + d128[1 + o]]) << 4; \
458 PUTRGB4D(dst_1, py_1, 0, 0);
459 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
462 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
463 PUTRGB4D(dst_1, py_1, 1, 2);
466 PUTRGB4D(dst_1, py_1, 2, 4);
467 PUTRGB4D(dst_2, py_2, 2, 4 + 8);
470 PUTRGB4D(dst_2, py_2, 3, 6 + 8);
471 PUTRGB4D(dst_1, py_1, 3, 6);
474 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
475 const uint8_t *d64 = dither_8x8_73[y & 7];
476 const uint8_t *d128 = dither_8x8_220[y & 7];
478 #define PUTRGB4DB(dst, src, i, o) \
480 dst[2 * i] = r[Y + d128[0 + o]] + \
481 g[Y + d64[0 + o]] + \
482 b[Y + d128[0 + o]]; \
483 Y = src[2 * i + 1]; \
484 dst[2 * i + 1] = r[Y + d128[1 + o]] + \
485 g[Y + d64[1 + o]] + \
489 PUTRGB4DB(dst_1, py_1, 0, 0);
490 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
493 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
494 PUTRGB4DB(dst_1, py_1, 1, 2);
497 PUTRGB4DB(dst_1, py_1, 2, 4);
498 PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
501 PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
502 PUTRGB4DB(dst_1, py_1, 3, 6);
505 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
506 const uint8_t *d128 = dither_8x8_220[y & 7];
507 char out_1 = 0, out_2 = 0;
508 g = c->table_gU[128] + c->table_gV[128];
510 #define PUTRGB1(out, src, i, o) \
512 out += out + g[Y + d128[0 + o]]; \
513 Y = src[2 * i + 1]; \
514 out += out + g[Y + d128[1 + o]];
516 PUTRGB1(out_1, py_1, 0, 0);
517 PUTRGB1(out_2, py_2, 0, 0 + 8);
519 PUTRGB1(out_2, py_2, 1, 2 + 8);
520 PUTRGB1(out_1, py_1, 1, 2);
522 PUTRGB1(out_1, py_1, 2, 4);
523 PUTRGB1(out_2, py_2, 2, 4 + 8);
525 PUTRGB1(out_2, py_2, 3, 6 + 8);
526 PUTRGB1(out_1, py_1, 3, 6);
532 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
537 t = ff_yuv2rgb_init_mmx(c);
539 t = ff_yuv2rgb_init_vis(c);
540 else if (HAVE_ALTIVEC)
541 t = ff_yuv2rgb_init_altivec(c);
543 t = ff_yuv2rgb_get_func_ptr_bfin(c);
548 av_log(c, AV_LOG_WARNING,
549 "No accelerated colorspace conversion found from %s to %s.\n",
550 sws_format_name(c->srcFormat), sws_format_name(c->dstFormat));
552 switch (c->dstFormat) {
553 case PIX_FMT_BGR48BE:
554 case PIX_FMT_BGR48LE:
555 return yuv2rgb_c_bgr48;
556 case PIX_FMT_RGB48BE:
557 case PIX_FMT_RGB48LE:
561 if (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P)
565 if (CONFIG_SWSCALE_ALPHA && c->srcFormat == PIX_FMT_YUVA420P)
570 return yuv2rgb_c_24_rgb;
572 return yuv2rgb_c_24_bgr;
580 return yuv2rgb_c_12_ordered_dither;
583 return yuv2rgb_c_8_ordered_dither;
586 return yuv2rgb_c_4_ordered_dither;
587 case PIX_FMT_RGB4_BYTE:
588 case PIX_FMT_BGR4_BYTE:
589 return yuv2rgb_c_4b_ordered_dither;
590 case PIX_FMT_MONOBLACK:
591 return yuv2rgb_c_1_ordered_dither;
598 static void fill_table(uint8_t *table[256], const int elemsize,
599 const int inc, void *y_tab)
603 uint8_t *y_table = y_tab;
605 y_table -= elemsize * (inc >> 9);
607 for (i = 0; i < 256; i++) {
608 table[i] = y_table + elemsize * (cb >> 16);
613 static void fill_gv_table(int table[256], const int elemsize, const int inc)
617 int off = -(inc >> 9);
619 for (i = 0; i < 256; i++) {
620 table[i] = elemsize * (off + (cb >> 16));
625 static uint16_t roundToInt16(int64_t f)
627 int r = (f + (1 << 15)) >> 16;
637 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
638 int fullRange, int brightness,
639 int contrast, int saturation)
641 const int isRgb = c->dstFormat == PIX_FMT_RGB32 ||
642 c->dstFormat == PIX_FMT_RGB32_1 ||
643 c->dstFormat == PIX_FMT_BGR24 ||
644 c->dstFormat == PIX_FMT_RGB565BE ||
645 c->dstFormat == PIX_FMT_RGB565LE ||
646 c->dstFormat == PIX_FMT_RGB555BE ||
647 c->dstFormat == PIX_FMT_RGB555LE ||
648 c->dstFormat == PIX_FMT_RGB444BE ||
649 c->dstFormat == PIX_FMT_RGB444LE ||
650 c->dstFormat == PIX_FMT_RGB8 ||
651 c->dstFormat == PIX_FMT_RGB4 ||
652 c->dstFormat == PIX_FMT_RGB4_BYTE ||
653 c->dstFormat == PIX_FMT_MONOBLACK;
654 const int isNotNe = c->dstFormat == PIX_FMT_NE(RGB565LE, RGB565BE) ||
655 c->dstFormat == PIX_FMT_NE(RGB555LE, RGB555BE) ||
656 c->dstFormat == PIX_FMT_NE(RGB444LE, RGB444BE) ||
657 c->dstFormat == PIX_FMT_NE(BGR565LE, BGR565BE) ||
658 c->dstFormat == PIX_FMT_NE(BGR555LE, BGR555BE) ||
659 c->dstFormat == PIX_FMT_NE(BGR444LE, BGR444BE);
660 const int bpp = c->dstFormatBpp;
664 int i, base, rbase, gbase, bbase, abase, needAlpha;
665 const int yoffs = fullRange ? 384 : 326;
667 int64_t crv = inv_table[0];
668 int64_t cbu = inv_table[1];
669 int64_t cgu = -inv_table[2];
670 int64_t cgv = -inv_table[3];
671 int64_t cy = 1 << 16;
676 cy = (cy * 255) / 219;
679 crv = (crv * 224) / 255;
680 cbu = (cbu * 224) / 255;
681 cgu = (cgu * 224) / 255;
682 cgv = (cgv * 224) / 255;
685 cy = (cy * contrast) >> 16;
686 crv = (crv * contrast * saturation) >> 32;
687 cbu = (cbu * contrast * saturation) >> 32;
688 cgu = (cgu * contrast * saturation) >> 32;
689 cgv = (cgv * contrast * saturation) >> 32;
690 oy -= 256 * brightness;
692 c->uOffset = 0x0400040004000400LL;
693 c->vOffset = 0x0400040004000400LL;
694 c->yCoeff = roundToInt16(cy * 8192) * 0x0001000100010001ULL;
695 c->vrCoeff = roundToInt16(crv * 8192) * 0x0001000100010001ULL;
696 c->ubCoeff = roundToInt16(cbu * 8192) * 0x0001000100010001ULL;
697 c->vgCoeff = roundToInt16(cgv * 8192) * 0x0001000100010001ULL;
698 c->ugCoeff = roundToInt16(cgu * 8192) * 0x0001000100010001ULL;
699 c->yOffset = roundToInt16(oy * 8) * 0x0001000100010001ULL;
701 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy << 13);
702 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
703 c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv << 13);
704 c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv << 13);
705 c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu << 13);
706 c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu << 13);
708 //scale coefficients by cy
709 crv = ((crv << 16) + 0x8000) / cy;
710 cbu = ((cbu << 16) + 0x8000) / cy;
711 cgu = ((cgu << 16) + 0x8000) / cy;
712 cgv = ((cgv << 16) + 0x8000) / cy;
714 av_free(c->yuvTable);
718 c->yuvTable = av_malloc(1024);
719 y_table = c->yuvTable;
720 yb = -(384 << 16) - oy;
721 for (i = 0; i < 1024 - 110; i++) {
722 y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
725 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
726 fill_gv_table(c->table_gV, 1, cgv);
730 rbase = isRgb ? 3 : 0;
732 bbase = isRgb ? 0 : 3;
733 c->yuvTable = av_malloc(1024 * 3);
734 y_table = c->yuvTable;
735 yb = -(384 << 16) - oy;
736 for (i = 0; i < 1024 - 110; i++) {
737 int yval = av_clip_uint8((yb + 0x8000) >> 16);
738 y_table[i + 110] = (yval >> 7) << rbase;
739 y_table[i + 37 + 1024] = ((yval + 43) / 85) << gbase;
740 y_table[i + 110 + 2048] = (yval >> 7) << bbase;
743 fill_table(c->table_rV, 1, crv, y_table + yoffs);
744 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
745 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
746 fill_gv_table(c->table_gV, 1, cgv);
749 rbase = isRgb ? 5 : 0;
750 gbase = isRgb ? 2 : 3;
751 bbase = isRgb ? 0 : 6;
752 c->yuvTable = av_malloc(1024 * 3);
753 y_table = c->yuvTable;
754 yb = -(384 << 16) - oy;
755 for (i = 0; i < 1024 - 38; i++) {
756 int yval = av_clip_uint8((yb + 0x8000) >> 16);
757 y_table[i + 16] = ((yval + 18) / 36) << rbase;
758 y_table[i + 16 + 1024] = ((yval + 18) / 36) << gbase;
759 y_table[i + 37 + 2048] = ((yval + 43) / 85) << bbase;
762 fill_table(c->table_rV, 1, crv, y_table + yoffs);
763 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
764 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
765 fill_gv_table(c->table_gV, 1, cgv);
768 rbase = isRgb ? 8 : 0;
770 bbase = isRgb ? 0 : 8;
771 c->yuvTable = av_malloc(1024 * 3 * 2);
772 y_table16 = c->yuvTable;
773 yb = -(384 << 16) - oy;
774 for (i = 0; i < 1024; i++) {
775 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
776 y_table16[i] = (yval >> 4) << rbase;
777 y_table16[i + 1024] = (yval >> 4) << gbase;
778 y_table16[i + 2048] = (yval >> 4) << bbase;
782 for (i = 0; i < 1024 * 3; i++)
783 y_table16[i] = av_bswap16(y_table16[i]);
784 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
785 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
786 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
787 fill_gv_table(c->table_gV, 2, cgv);
791 rbase = isRgb ? bpp - 5 : 0;
793 bbase = isRgb ? 0 : (bpp - 5);
794 c->yuvTable = av_malloc(1024 * 3 * 2);
795 y_table16 = c->yuvTable;
796 yb = -(384 << 16) - oy;
797 for (i = 0; i < 1024; i++) {
798 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
799 y_table16[i] = (yval >> 3) << rbase;
800 y_table16[i + 1024] = (yval >> (18 - bpp)) << gbase;
801 y_table16[i + 2048] = (yval >> 3) << bbase;
805 for (i = 0; i < 1024 * 3; i++)
806 y_table16[i] = av_bswap16(y_table16[i]);
807 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
808 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
809 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
810 fill_gv_table(c->table_gV, 2, cgv);
814 c->yuvTable = av_malloc(1024);
815 y_table = c->yuvTable;
816 yb = -(384 << 16) - oy;
817 for (i = 0; i < 1024; i++) {
818 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
821 fill_table(c->table_rV, 1, crv, y_table + yoffs);
822 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
823 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
824 fill_gv_table(c->table_gV, 1, cgv);
827 base = (c->dstFormat == PIX_FMT_RGB32_1 ||
828 c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
829 rbase = base + (isRgb ? 16 : 0);
831 bbase = base + (isRgb ? 0 : 16);
832 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
834 abase = (base + 24) & 31;
835 c->yuvTable = av_malloc(1024 * 3 * 4);
836 y_table32 = c->yuvTable;
837 yb = -(384 << 16) - oy;
838 for (i = 0; i < 1024; i++) {
839 unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
840 y_table32[i] = (yval << rbase) +
841 (needAlpha ? 0 : (255u << abase));
842 y_table32[i + 1024] = yval << gbase;
843 y_table32[i + 2048] = yval << bbase;
846 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
847 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
848 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
849 fill_gv_table(c->table_gV, 4, cgv);
853 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);