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 == AV_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, ss) \
155 dst_1 += dst_delta >> ss; \
156 dst_2 += dst_delta >> ss; \
158 if (c->dstW & (4 >> ss)) { \
159 int av_unused Y, U, V; \
161 #define ENDYUV2RGBFUNC() \
167 #define CLOSEYUV2RGBFUNC(dst_delta) \
168 ENDYUV2RGBLINE(dst_delta, 0) \
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);
187 ENDYUV2RGBLINE(48, 0)
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);
195 ENDYUV2RGBLINE(48, 1)
197 PUTRGB48(dst_1, py_1, 0);
198 PUTRGB48(dst_2, py_2, 0);
201 YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
203 PUTBGR48(dst_1, py_1, 0);
204 PUTBGR48(dst_2, py_2, 0);
207 PUTBGR48(dst_2, py_2, 1);
208 PUTBGR48(dst_1, py_1, 1);
211 PUTBGR48(dst_1, py_1, 2);
212 PUTBGR48(dst_2, py_2, 2);
215 PUTBGR48(dst_2, py_2, 3);
216 PUTBGR48(dst_1, py_1, 3);
217 ENDYUV2RGBLINE(48, 0)
219 PUTBGR48(dst_1, py_1, 0);
220 PUTBGR48(dst_2, py_2, 0);
223 PUTBGR48(dst_2, py_2, 1);
224 PUTBGR48(dst_1, py_1, 1);
225 ENDYUV2RGBLINE(48, 1)
227 PUTBGR48(dst_1, py_1, 0);
228 PUTBGR48(dst_2, py_2, 0);
231 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
233 PUTRGB(dst_1, py_1, 0);
234 PUTRGB(dst_2, py_2, 0);
237 PUTRGB(dst_2, py_2, 1);
238 PUTRGB(dst_1, py_1, 1);
241 PUTRGB(dst_1, py_1, 2);
242 PUTRGB(dst_2, py_2, 2);
245 PUTRGB(dst_2, py_2, 3);
246 PUTRGB(dst_1, py_1, 3);
249 PUTRGB(dst_1, py_1, 0);
250 PUTRGB(dst_2, py_2, 0);
253 PUTRGB(dst_2, py_2, 1);
254 PUTRGB(dst_1, py_1, 1);
257 PUTRGB(dst_1, py_1, 0);
258 PUTRGB(dst_2, py_2, 0);
261 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
263 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
264 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
267 PUTRGBA(dst_2, py_2, pa_1, 1, 24);
268 PUTRGBA(dst_1, py_1, pa_2, 1, 24);
271 PUTRGBA(dst_1, py_1, pa_1, 2, 24);
272 PUTRGBA(dst_2, py_2, pa_2, 2, 24);
275 PUTRGBA(dst_2, py_2, pa_1, 3, 24);
276 PUTRGBA(dst_1, py_1, pa_2, 3, 24);
281 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
282 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
285 PUTRGBA(dst_2, py_2, pa_1, 1, 24);
286 PUTRGBA(dst_1, py_1, pa_2, 1, 24);
291 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
292 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
295 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
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 PUTRGBA(dst_1, py_1, pa_1, 2, 0);
306 PUTRGBA(dst_2, py_2, pa_2, 2, 0);
309 PUTRGBA(dst_2, py_2, pa_2, 3, 0);
310 PUTRGBA(dst_1, py_1, pa_1, 3, 0);
315 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
316 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
319 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
320 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
325 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
326 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
329 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
331 PUTRGB24(dst_1, py_1, 0);
332 PUTRGB24(dst_2, py_2, 0);
335 PUTRGB24(dst_2, py_2, 1);
336 PUTRGB24(dst_1, py_1, 1);
339 PUTRGB24(dst_1, py_1, 2);
340 PUTRGB24(dst_2, py_2, 2);
343 PUTRGB24(dst_2, py_2, 3);
344 PUTRGB24(dst_1, py_1, 3);
345 ENDYUV2RGBLINE(24, 0)
347 PUTRGB24(dst_1, py_1, 0);
348 PUTRGB24(dst_2, py_2, 0);
351 PUTRGB24(dst_2, py_2, 1);
352 PUTRGB24(dst_1, py_1, 1);
353 ENDYUV2RGBLINE(24, 1)
355 PUTRGB24(dst_1, py_1, 0);
356 PUTRGB24(dst_2, py_2, 0);
359 // only trivial mods from yuv2rgb_c_24_rgb
360 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
362 PUTBGR24(dst_1, py_1, 0);
363 PUTBGR24(dst_2, py_2, 0);
366 PUTBGR24(dst_2, py_2, 1);
367 PUTBGR24(dst_1, py_1, 1);
370 PUTBGR24(dst_1, py_1, 2);
371 PUTBGR24(dst_2, py_2, 2);
374 PUTBGR24(dst_2, py_2, 3);
375 PUTBGR24(dst_1, py_1, 3);
376 ENDYUV2RGBLINE(24, 0)
378 PUTBGR24(dst_1, py_1, 0);
379 PUTBGR24(dst_2, py_2, 0);
382 PUTBGR24(dst_2, py_2, 1);
383 PUTBGR24(dst_1, py_1, 1);
384 ENDYUV2RGBLINE(24, 1)
386 PUTBGR24(dst_1, py_1, 0);
387 PUTBGR24(dst_2, py_2, 0);
390 // This is exactly the same code as yuv2rgb_c_32 except for the types of
391 // r, g, b, dst_1, dst_2
392 YUV2RGBFUNC(yuv2rgb_c_16, uint16_t, 0)
394 PUTRGB(dst_1, py_1, 0);
395 PUTRGB(dst_2, py_2, 0);
398 PUTRGB(dst_2, py_2, 1);
399 PUTRGB(dst_1, py_1, 1);
402 PUTRGB(dst_1, py_1, 2);
403 PUTRGB(dst_2, py_2, 2);
406 PUTRGB(dst_2, py_2, 3);
407 PUTRGB(dst_1, py_1, 3);
410 // r, g, b, dst_1, dst_2
411 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
412 const uint8_t *d16 = dither_4x4_16[y & 3];
414 #define PUTRGB12(dst, src, i, o) \
416 dst[2 * i] = r[Y + d16[0 + o]] + \
417 g[Y + d16[0 + o]] + \
419 Y = src[2 * i + 1]; \
420 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
421 g[Y + d16[1 + o]] + \
425 PUTRGB12(dst_1, py_1, 0, 0);
426 PUTRGB12(dst_2, py_2, 0, 0 + 8);
429 PUTRGB12(dst_2, py_2, 1, 2 + 8);
430 PUTRGB12(dst_1, py_1, 1, 2);
433 PUTRGB12(dst_1, py_1, 2, 4);
434 PUTRGB12(dst_2, py_2, 2, 4 + 8);
437 PUTRGB12(dst_2, py_2, 3, 6 + 8);
438 PUTRGB12(dst_1, py_1, 3, 6);
441 // r, g, b, dst_1, dst_2
442 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
443 const uint8_t *d32 = dither_8x8_32[y & 7];
444 const uint8_t *d64 = dither_8x8_73[y & 7];
446 #define PUTRGB8(dst, src, i, o) \
448 dst[2 * i] = r[Y + d32[0 + o]] + \
449 g[Y + d32[0 + o]] + \
451 Y = src[2 * i + 1]; \
452 dst[2 * i + 1] = r[Y + d32[1 + o]] + \
453 g[Y + d32[1 + o]] + \
457 PUTRGB8(dst_1, py_1, 0, 0);
458 PUTRGB8(dst_2, py_2, 0, 0 + 8);
461 PUTRGB8(dst_2, py_2, 1, 2 + 8);
462 PUTRGB8(dst_1, py_1, 1, 2);
465 PUTRGB8(dst_1, py_1, 2, 4);
466 PUTRGB8(dst_2, py_2, 2, 4 + 8);
469 PUTRGB8(dst_2, py_2, 3, 6 + 8);
470 PUTRGB8(dst_1, py_1, 3, 6);
473 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
474 const uint8_t * d64 = dither_8x8_73[y & 7];
475 const uint8_t *d128 = dither_8x8_220[y & 7];
478 #define PUTRGB4D(dst, src, i, o) \
480 acc = r[Y + d128[0 + o]] + \
481 g[Y + d64[0 + o]] + \
482 b[Y + d128[0 + o]]; \
483 Y = src[2 * i + 1]; \
484 acc |= (r[Y + d128[1 + o]] + \
485 g[Y + d64[1 + o]] + \
486 b[Y + d128[1 + o]]) << 4; \
490 PUTRGB4D(dst_1, py_1, 0, 0);
491 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
494 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
495 PUTRGB4D(dst_1, py_1, 1, 2);
498 PUTRGB4D(dst_1, py_1, 2, 4);
499 PUTRGB4D(dst_2, py_2, 2, 4 + 8);
502 PUTRGB4D(dst_2, py_2, 3, 6 + 8);
503 PUTRGB4D(dst_1, py_1, 3, 6);
506 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
507 const uint8_t *d64 = dither_8x8_73[y & 7];
508 const uint8_t *d128 = dither_8x8_220[y & 7];
510 #define PUTRGB4DB(dst, src, i, o) \
512 dst[2 * i] = r[Y + d128[0 + o]] + \
513 g[Y + d64[0 + o]] + \
514 b[Y + d128[0 + o]]; \
515 Y = src[2 * i + 1]; \
516 dst[2 * i + 1] = r[Y + d128[1 + o]] + \
517 g[Y + d64[1 + o]] + \
521 PUTRGB4DB(dst_1, py_1, 0, 0);
522 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
525 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
526 PUTRGB4DB(dst_1, py_1, 1, 2);
529 PUTRGB4DB(dst_1, py_1, 2, 4);
530 PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
533 PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
534 PUTRGB4DB(dst_1, py_1, 3, 6);
537 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
538 const uint8_t *d128 = dither_8x8_220[y & 7];
539 char out_1 = 0, out_2 = 0;
540 g = c->table_gU[128] + c->table_gV[128];
542 #define PUTRGB1(out, src, i, o) \
544 out += out + g[Y + d128[0 + o]]; \
545 Y = src[2 * i + 1]; \
546 out += out + g[Y + d128[1 + o]];
548 PUTRGB1(out_1, py_1, 0, 0);
549 PUTRGB1(out_2, py_2, 0, 0 + 8);
551 PUTRGB1(out_2, py_2, 1, 2 + 8);
552 PUTRGB1(out_1, py_1, 1, 2);
554 PUTRGB1(out_1, py_1, 2, 4);
555 PUTRGB1(out_2, py_2, 2, 4 + 8);
557 PUTRGB1(out_2, py_2, 3, 6 + 8);
558 PUTRGB1(out_1, py_1, 3, 6);
564 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
569 t = ff_yuv2rgb_init_mmx(c);
571 t = ff_yuv2rgb_init_vis(c);
572 else if (HAVE_ALTIVEC)
573 t = ff_yuv2rgb_init_altivec(c);
575 t = ff_yuv2rgb_get_func_ptr_bfin(c);
580 av_log(c, AV_LOG_WARNING,
581 "No accelerated colorspace conversion found from %s to %s.\n",
582 sws_format_name(c->srcFormat), sws_format_name(c->dstFormat));
584 switch (c->dstFormat) {
585 case AV_PIX_FMT_BGR48BE:
586 case AV_PIX_FMT_BGR48LE:
587 return yuv2rgb_c_bgr48;
588 case AV_PIX_FMT_RGB48BE:
589 case AV_PIX_FMT_RGB48LE:
591 case AV_PIX_FMT_ARGB:
592 case AV_PIX_FMT_ABGR:
593 if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
595 case AV_PIX_FMT_RGBA:
596 case AV_PIX_FMT_BGRA:
597 if (CONFIG_SWSCALE_ALPHA && c->srcFormat == AV_PIX_FMT_YUVA420P)
601 case AV_PIX_FMT_RGB24:
602 return yuv2rgb_c_24_rgb;
603 case AV_PIX_FMT_BGR24:
604 return yuv2rgb_c_24_bgr;
605 case AV_PIX_FMT_RGB565:
606 case AV_PIX_FMT_BGR565:
607 case AV_PIX_FMT_RGB555:
608 case AV_PIX_FMT_BGR555:
610 case AV_PIX_FMT_RGB444:
611 case AV_PIX_FMT_BGR444:
612 return yuv2rgb_c_12_ordered_dither;
613 case AV_PIX_FMT_RGB8:
614 case AV_PIX_FMT_BGR8:
615 return yuv2rgb_c_8_ordered_dither;
616 case AV_PIX_FMT_RGB4:
617 case AV_PIX_FMT_BGR4:
618 return yuv2rgb_c_4_ordered_dither;
619 case AV_PIX_FMT_RGB4_BYTE:
620 case AV_PIX_FMT_BGR4_BYTE:
621 return yuv2rgb_c_4b_ordered_dither;
622 case AV_PIX_FMT_MONOBLACK:
623 return yuv2rgb_c_1_ordered_dither;
630 static void fill_table(uint8_t *table[256], const int elemsize,
631 const int inc, void *y_tab)
635 uint8_t *y_table = y_tab;
637 y_table -= elemsize * (inc >> 9);
639 for (i = 0; i < 256; i++) {
640 table[i] = y_table + elemsize * (cb >> 16);
645 static void fill_gv_table(int table[256], const int elemsize, const int inc)
649 int off = -(inc >> 9);
651 for (i = 0; i < 256; i++) {
652 table[i] = elemsize * (off + (cb >> 16));
657 static uint16_t roundToInt16(int64_t f)
659 int r = (f + (1 << 15)) >> 16;
669 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
670 int fullRange, int brightness,
671 int contrast, int saturation)
673 const int isRgb = c->dstFormat == AV_PIX_FMT_RGB32 ||
674 c->dstFormat == AV_PIX_FMT_RGB32_1 ||
675 c->dstFormat == AV_PIX_FMT_BGR24 ||
676 c->dstFormat == AV_PIX_FMT_RGB565BE ||
677 c->dstFormat == AV_PIX_FMT_RGB565LE ||
678 c->dstFormat == AV_PIX_FMT_RGB555BE ||
679 c->dstFormat == AV_PIX_FMT_RGB555LE ||
680 c->dstFormat == AV_PIX_FMT_RGB444BE ||
681 c->dstFormat == AV_PIX_FMT_RGB444LE ||
682 c->dstFormat == AV_PIX_FMT_RGB8 ||
683 c->dstFormat == AV_PIX_FMT_RGB4 ||
684 c->dstFormat == AV_PIX_FMT_RGB4_BYTE ||
685 c->dstFormat == AV_PIX_FMT_MONOBLACK;
686 const int isNotNe = c->dstFormat == AV_PIX_FMT_NE(RGB565LE, RGB565BE) ||
687 c->dstFormat == AV_PIX_FMT_NE(RGB555LE, RGB555BE) ||
688 c->dstFormat == AV_PIX_FMT_NE(RGB444LE, RGB444BE) ||
689 c->dstFormat == AV_PIX_FMT_NE(BGR565LE, BGR565BE) ||
690 c->dstFormat == AV_PIX_FMT_NE(BGR555LE, BGR555BE) ||
691 c->dstFormat == AV_PIX_FMT_NE(BGR444LE, BGR444BE);
692 const int bpp = c->dstFormatBpp;
696 int i, base, rbase, gbase, bbase, abase, needAlpha;
697 const int yoffs = fullRange ? 384 : 326;
699 int64_t crv = inv_table[0];
700 int64_t cbu = inv_table[1];
701 int64_t cgu = -inv_table[2];
702 int64_t cgv = -inv_table[3];
703 int64_t cy = 1 << 16;
708 cy = (cy * 255) / 219;
711 crv = (crv * 224) / 255;
712 cbu = (cbu * 224) / 255;
713 cgu = (cgu * 224) / 255;
714 cgv = (cgv * 224) / 255;
717 cy = (cy * contrast) >> 16;
718 crv = (crv * contrast * saturation) >> 32;
719 cbu = (cbu * contrast * saturation) >> 32;
720 cgu = (cgu * contrast * saturation) >> 32;
721 cgv = (cgv * contrast * saturation) >> 32;
722 oy -= 256 * brightness;
724 c->uOffset = 0x0400040004000400LL;
725 c->vOffset = 0x0400040004000400LL;
726 c->yCoeff = roundToInt16(cy * 8192) * 0x0001000100010001ULL;
727 c->vrCoeff = roundToInt16(crv * 8192) * 0x0001000100010001ULL;
728 c->ubCoeff = roundToInt16(cbu * 8192) * 0x0001000100010001ULL;
729 c->vgCoeff = roundToInt16(cgv * 8192) * 0x0001000100010001ULL;
730 c->ugCoeff = roundToInt16(cgu * 8192) * 0x0001000100010001ULL;
731 c->yOffset = roundToInt16(oy * 8) * 0x0001000100010001ULL;
733 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy << 13);
734 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
735 c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv << 13);
736 c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv << 13);
737 c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu << 13);
738 c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu << 13);
740 //scale coefficients by cy
741 crv = ((crv << 16) + 0x8000) / cy;
742 cbu = ((cbu << 16) + 0x8000) / cy;
743 cgu = ((cgu << 16) + 0x8000) / cy;
744 cgv = ((cgv << 16) + 0x8000) / cy;
746 av_free(c->yuvTable);
750 c->yuvTable = av_malloc(1024);
751 y_table = c->yuvTable;
752 yb = -(384 << 16) - oy;
753 for (i = 0; i < 1024 - 110; i++) {
754 y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
757 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
758 fill_gv_table(c->table_gV, 1, cgv);
762 rbase = isRgb ? 3 : 0;
764 bbase = isRgb ? 0 : 3;
765 c->yuvTable = av_malloc(1024 * 3);
766 y_table = c->yuvTable;
767 yb = -(384 << 16) - oy;
768 for (i = 0; i < 1024 - 110; i++) {
769 int yval = av_clip_uint8((yb + 0x8000) >> 16);
770 y_table[i + 110] = (yval >> 7) << rbase;
771 y_table[i + 37 + 1024] = ((yval + 43) / 85) << gbase;
772 y_table[i + 110 + 2048] = (yval >> 7) << bbase;
775 fill_table(c->table_rV, 1, crv, y_table + yoffs);
776 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
777 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
778 fill_gv_table(c->table_gV, 1, cgv);
781 rbase = isRgb ? 5 : 0;
782 gbase = isRgb ? 2 : 3;
783 bbase = isRgb ? 0 : 6;
784 c->yuvTable = av_malloc(1024 * 3);
785 y_table = c->yuvTable;
786 yb = -(384 << 16) - oy;
787 for (i = 0; i < 1024 - 38; i++) {
788 int yval = av_clip_uint8((yb + 0x8000) >> 16);
789 y_table[i + 16] = ((yval + 18) / 36) << rbase;
790 y_table[i + 16 + 1024] = ((yval + 18) / 36) << gbase;
791 y_table[i + 37 + 2048] = ((yval + 43) / 85) << bbase;
794 fill_table(c->table_rV, 1, crv, y_table + yoffs);
795 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
796 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
797 fill_gv_table(c->table_gV, 1, cgv);
800 rbase = isRgb ? 8 : 0;
802 bbase = isRgb ? 0 : 8;
803 c->yuvTable = av_malloc(1024 * 3 * 2);
804 y_table16 = c->yuvTable;
805 yb = -(384 << 16) - oy;
806 for (i = 0; i < 1024; i++) {
807 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
808 y_table16[i] = (yval >> 4) << rbase;
809 y_table16[i + 1024] = (yval >> 4) << gbase;
810 y_table16[i + 2048] = (yval >> 4) << bbase;
814 for (i = 0; i < 1024 * 3; i++)
815 y_table16[i] = av_bswap16(y_table16[i]);
816 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
817 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
818 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
819 fill_gv_table(c->table_gV, 2, cgv);
823 rbase = isRgb ? bpp - 5 : 0;
825 bbase = isRgb ? 0 : (bpp - 5);
826 c->yuvTable = av_malloc(1024 * 3 * 2);
827 y_table16 = c->yuvTable;
828 yb = -(384 << 16) - oy;
829 for (i = 0; i < 1024; i++) {
830 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
831 y_table16[i] = (yval >> 3) << rbase;
832 y_table16[i + 1024] = (yval >> (18 - bpp)) << gbase;
833 y_table16[i + 2048] = (yval >> 3) << bbase;
837 for (i = 0; i < 1024 * 3; i++)
838 y_table16[i] = av_bswap16(y_table16[i]);
839 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
840 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
841 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
842 fill_gv_table(c->table_gV, 2, cgv);
846 c->yuvTable = av_malloc(1024);
847 y_table = c->yuvTable;
848 yb = -(384 << 16) - oy;
849 for (i = 0; i < 1024; i++) {
850 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
853 fill_table(c->table_rV, 1, crv, y_table + yoffs);
854 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
855 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
856 fill_gv_table(c->table_gV, 1, cgv);
859 base = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
860 c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
861 rbase = base + (isRgb ? 16 : 0);
863 bbase = base + (isRgb ? 0 : 16);
864 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
866 abase = (base + 24) & 31;
867 c->yuvTable = av_malloc(1024 * 3 * 4);
868 y_table32 = c->yuvTable;
869 yb = -(384 << 16) - oy;
870 for (i = 0; i < 1024; i++) {
871 unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
872 y_table32[i] = (yval << rbase) +
873 (needAlpha ? 0 : (255u << abase));
874 y_table32[i + 1024] = yval << gbase;
875 y_table32[i + 2048] = yval << bbase;
878 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
879 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
880 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
881 fill_gv_table(c->table_gV, 4, cgv);
885 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);