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
30 #include "libavutil/cpu.h"
31 #include "libavutil/bswap.h"
35 #include "swscale_internal.h"
36 #include "libavutil/pixdesc.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+YUVRGB_TABLE_HEADROOM]; \
65 g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
66 b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
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 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
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 av_get_pix_fmt_name(c->srcFormat), av_get_pix_fmt_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 && isALPHA(c->srcFormat))
565 return (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat)) ? yuva2rgba_c : yuv2rgb_c_32;
567 return yuv2rgb_c_24_rgb;
569 return yuv2rgb_c_24_bgr;
577 return yuv2rgb_c_12_ordered_dither;
580 return yuv2rgb_c_8_ordered_dither;
583 return yuv2rgb_c_4_ordered_dither;
584 case PIX_FMT_RGB4_BYTE:
585 case PIX_FMT_BGR4_BYTE:
586 return yuv2rgb_c_4b_ordered_dither;
587 case PIX_FMT_MONOBLACK:
588 return yuv2rgb_c_1_ordered_dither;
593 static void fill_table(uint8_t* table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize,
594 const int inc, void *y_tab)
597 uint8_t *y_table = y_tab;
599 y_table -= elemsize * (inc >> 9);
601 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
602 int64_t cb = av_clip(i-YUVRGB_TABLE_HEADROOM, 0, 255)*inc;
603 table[i] = y_table + elemsize * (cb >> 16);
607 static void fill_gv_table(int table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize, const int inc)
610 int off = -(inc >> 9);
612 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
613 int64_t cb = av_clip(i-YUVRGB_TABLE_HEADROOM, 0, 255)*inc;
614 table[i] = elemsize * (off + (cb >> 16));
618 static uint16_t roundToInt16(int64_t f)
620 int r = (f + (1 << 15)) >> 16;
630 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
631 int fullRange, int brightness,
632 int contrast, int saturation)
634 const int isRgb = c->dstFormat == PIX_FMT_RGB32 ||
635 c->dstFormat == PIX_FMT_RGB32_1 ||
636 c->dstFormat == PIX_FMT_BGR24 ||
637 c->dstFormat == PIX_FMT_RGB565BE ||
638 c->dstFormat == PIX_FMT_RGB565LE ||
639 c->dstFormat == PIX_FMT_RGB555BE ||
640 c->dstFormat == PIX_FMT_RGB555LE ||
641 c->dstFormat == PIX_FMT_RGB444BE ||
642 c->dstFormat == PIX_FMT_RGB444LE ||
643 c->dstFormat == PIX_FMT_RGB8 ||
644 c->dstFormat == PIX_FMT_RGB4 ||
645 c->dstFormat == PIX_FMT_RGB4_BYTE ||
646 c->dstFormat == PIX_FMT_MONOBLACK;
647 const int isNotNe = c->dstFormat == PIX_FMT_NE(RGB565LE, RGB565BE) ||
648 c->dstFormat == PIX_FMT_NE(RGB555LE, RGB555BE) ||
649 c->dstFormat == PIX_FMT_NE(RGB444LE, RGB444BE) ||
650 c->dstFormat == PIX_FMT_NE(BGR565LE, BGR565BE) ||
651 c->dstFormat == PIX_FMT_NE(BGR555LE, BGR555BE) ||
652 c->dstFormat == PIX_FMT_NE(BGR444LE, BGR444BE);
653 const int bpp = c->dstFormatBpp;
657 int i, base, rbase, gbase, bbase, av_uninit(abase), needAlpha;
658 const int yoffs = fullRange ? 384 : 326;
660 int64_t crv = inv_table[0];
661 int64_t cbu = inv_table[1];
662 int64_t cgu = -inv_table[2];
663 int64_t cgv = -inv_table[3];
664 int64_t cy = 1 << 16;
669 cy = (cy * 255) / 219;
672 crv = (crv * 224) / 255;
673 cbu = (cbu * 224) / 255;
674 cgu = (cgu * 224) / 255;
675 cgv = (cgv * 224) / 255;
678 cy = (cy * contrast) >> 16;
679 crv = (crv * contrast * saturation) >> 32;
680 cbu = (cbu * contrast * saturation) >> 32;
681 cgu = (cgu * contrast * saturation) >> 32;
682 cgv = (cgv * contrast * saturation) >> 32;
683 oy -= 256 * brightness;
685 c->uOffset = 0x0400040004000400LL;
686 c->vOffset = 0x0400040004000400LL;
687 c->yCoeff = roundToInt16(cy * 8192) * 0x0001000100010001ULL;
688 c->vrCoeff = roundToInt16(crv * 8192) * 0x0001000100010001ULL;
689 c->ubCoeff = roundToInt16(cbu * 8192) * 0x0001000100010001ULL;
690 c->vgCoeff = roundToInt16(cgv * 8192) * 0x0001000100010001ULL;
691 c->ugCoeff = roundToInt16(cgu * 8192) * 0x0001000100010001ULL;
692 c->yOffset = roundToInt16(oy * 8) * 0x0001000100010001ULL;
694 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy << 13);
695 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
696 c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv << 13);
697 c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv << 13);
698 c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu << 13);
699 c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu << 13);
701 //scale coefficients by cy
702 crv = ((crv << 16) + 0x8000) / cy;
703 cbu = ((cbu << 16) + 0x8000) / cy;
704 cgu = ((cgu << 16) + 0x8000) / cy;
705 cgv = ((cgv << 16) + 0x8000) / cy;
707 av_free(c->yuvTable);
711 c->yuvTable = av_malloc(1024);
712 y_table = c->yuvTable;
713 yb = -(384 << 16) - oy;
714 for (i = 0; i < 1024 - 110; i++) {
715 y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
718 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
719 fill_gv_table(c->table_gV, 1, cgv);
723 rbase = isRgb ? 3 : 0;
725 bbase = isRgb ? 0 : 3;
726 c->yuvTable = av_malloc(1024 * 3);
727 y_table = c->yuvTable;
728 yb = -(384 << 16) - oy;
729 for (i = 0; i < 1024 - 110; i++) {
730 int yval = av_clip_uint8((yb + 0x8000) >> 16);
731 y_table[i + 110] = (yval >> 7) << rbase;
732 y_table[i + 37 + 1024] = ((yval + 43) / 85) << gbase;
733 y_table[i + 110 + 2048] = (yval >> 7) << bbase;
736 fill_table(c->table_rV, 1, crv, y_table + yoffs);
737 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
738 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
739 fill_gv_table(c->table_gV, 1, cgv);
742 rbase = isRgb ? 5 : 0;
743 gbase = isRgb ? 2 : 3;
744 bbase = isRgb ? 0 : 6;
745 c->yuvTable = av_malloc(1024 * 3);
746 y_table = c->yuvTable;
747 yb = -(384 << 16) - oy;
748 for (i = 0; i < 1024 - 38; i++) {
749 int yval = av_clip_uint8((yb + 0x8000) >> 16);
750 y_table[i + 16] = ((yval + 18) / 36) << rbase;
751 y_table[i + 16 + 1024] = ((yval + 18) / 36) << gbase;
752 y_table[i + 37 + 2048] = ((yval + 43) / 85) << bbase;
755 fill_table(c->table_rV, 1, crv, y_table + yoffs);
756 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
757 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
758 fill_gv_table(c->table_gV, 1, cgv);
761 rbase = isRgb ? 8 : 0;
763 bbase = isRgb ? 0 : 8;
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 >> 4) << rbase;
770 y_table16[i + 1024] = (yval >> 4) << gbase;
771 y_table16[i + 2048] = (yval >> 4) << bbase;
775 for (i = 0; i < 1024 * 3; i++)
776 y_table16[i] = av_bswap16(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 rbase = isRgb ? bpp - 5 : 0;
786 bbase = isRgb ? 0 : (bpp - 5);
787 c->yuvTable = av_malloc(1024 * 3 * 2);
788 y_table16 = c->yuvTable;
789 yb = -(384 << 16) - oy;
790 for (i = 0; i < 1024; i++) {
791 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
792 y_table16[i] = (yval >> 3) << rbase;
793 y_table16[i + 1024] = (yval >> (18 - bpp)) << gbase;
794 y_table16[i + 2048] = (yval >> 3) << bbase;
798 for (i = 0; i < 1024 * 3; i++)
799 y_table16[i] = av_bswap16(y_table16[i]);
800 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
801 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
802 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
803 fill_gv_table(c->table_gV, 2, cgv);
807 c->yuvTable = av_malloc(1024);
808 y_table = c->yuvTable;
809 yb = -(384 << 16) - oy;
810 for (i = 0; i < 1024; i++) {
811 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
814 fill_table(c->table_rV, 1, crv, y_table + yoffs);
815 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
816 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
817 fill_gv_table(c->table_gV, 1, cgv);
820 base = (c->dstFormat == PIX_FMT_RGB32_1 ||
821 c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
822 rbase = base + (isRgb ? 16 : 0);
824 bbase = base + (isRgb ? 0 : 16);
825 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
827 abase = (base + 24) & 31;
828 c->yuvTable = av_malloc(1024 * 3 * 4);
829 y_table32 = c->yuvTable;
830 yb = -(384 << 16) - oy;
831 for (i = 0; i < 1024; i++) {
832 unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
833 y_table32[i] = (yval << rbase) +
834 (needAlpha ? 0 : (255u << abase));
835 y_table32[i + 1024] = yval << gbase;
836 y_table32[i + 2048] = yval << bbase;
839 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
840 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
841 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
842 fill_gv_table(c->table_gV, 4, cgv);
846 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);