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_2x2_4[2][8];
39 extern const uint8_t dither_2x2_8[2][8];
40 extern const uint8_t dither_4x4_16[4][8];
41 extern const uint8_t dither_8x8_32[8][8];
42 extern const uint8_t dither_8x8_73[8][8];
43 extern const uint8_t dither_8x8_220[8][8];
45 const int32_t ff_yuv2rgb_coeffs[8][4] = {
46 { 117504, 138453, 13954, 34903 }, /* no sequence_display_extension */
47 { 117504, 138453, 13954, 34903 }, /* ITU-R Rec. 709 (1990) */
48 { 104597, 132201, 25675, 53279 }, /* unspecified */
49 { 104597, 132201, 25675, 53279 }, /* reserved */
50 { 104448, 132798, 24759, 53109 }, /* FCC */
51 { 104597, 132201, 25675, 53279 }, /* ITU-R Rec. 624-4 System B, G */
52 { 104597, 132201, 25675, 53279 }, /* SMPTE 170M */
53 { 117579, 136230, 16907, 35559 } /* SMPTE 240M (1987) */
56 const int *sws_getCoefficients(int colorspace)
58 if (colorspace > 7 || colorspace < 0)
59 colorspace = SWS_CS_DEFAULT;
60 return ff_yuv2rgb_coeffs[colorspace];
63 #define LOADCHROMA(i) \
66 r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
67 g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
68 b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
70 #define PUTRGB(dst, src, i) \
72 dst[2 * i] = r[Y] + g[Y] + b[Y]; \
74 dst[2 * i + 1] = r[Y] + g[Y] + b[Y];
76 #define PUTRGB24(dst, src, i) \
78 dst[6 * i + 0] = r[Y]; \
79 dst[6 * i + 1] = g[Y]; \
80 dst[6 * i + 2] = b[Y]; \
82 dst[6 * i + 3] = r[Y]; \
83 dst[6 * i + 4] = g[Y]; \
84 dst[6 * i + 5] = b[Y];
86 #define PUTBGR24(dst, src, i) \
88 dst[6 * i + 0] = b[Y]; \
89 dst[6 * i + 1] = g[Y]; \
90 dst[6 * i + 2] = r[Y]; \
92 dst[6 * i + 3] = b[Y]; \
93 dst[6 * i + 4] = g[Y]; \
94 dst[6 * i + 5] = r[Y];
96 #define PUTRGBA(dst, ysrc, asrc, i, s) \
98 dst[2 * i] = r[Y] + g[Y] + b[Y] + (asrc[2 * i] << s); \
99 Y = ysrc[2 * i + 1]; \
100 dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + (asrc[2 * i + 1] << s);
102 #define PUTRGB48(dst, src, i) \
104 dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
105 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
106 dst[12 * i + 4] = dst[12 * i + 5] = b[Y]; \
107 Y = src[ 2 * i + 1]; \
108 dst[12 * i + 6] = dst[12 * i + 7] = r[Y]; \
109 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
110 dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
112 #define PUTBGR48(dst, src, i) \
114 dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
115 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
116 dst[12 * i + 4] = dst[12 * i + 5] = r[Y]; \
117 Y = src[2 * i + 1]; \
118 dst[12 * i + 6] = dst[12 * i + 7] = b[Y]; \
119 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
120 dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
122 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
123 static int func_name(SwsContext *c, const uint8_t *src[], \
124 int srcStride[], int srcSliceY, int srcSliceH, \
125 uint8_t *dst[], int dstStride[]) \
129 if (!alpha && c->srcFormat == PIX_FMT_YUV422P) { \
133 for (y = 0; y < srcSliceH; y += 2) { \
135 (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]); \
137 (dst_type *)(dst[0] + (y + srcSliceY + 1) * dstStride[0]); \
138 dst_type av_unused *r, *g, *b; \
139 const uint8_t *py_1 = src[0] + y * srcStride[0]; \
140 const uint8_t *py_2 = py_1 + srcStride[0]; \
141 const uint8_t *pu = src[1] + (y >> 1) * srcStride[1]; \
142 const uint8_t *pv = src[2] + (y >> 1) * srcStride[2]; \
143 const uint8_t av_unused *pa_1, *pa_2; \
144 unsigned int h_size = c->dstW >> 3; \
146 pa_1 = src[3] + y * srcStride[3]; \
147 pa_2 = pa_1 + srcStride[3]; \
150 int av_unused U, V, Y; \
152 #define ENDYUV2RGBLINE(dst_delta) \
157 dst_1 += dst_delta; \
158 dst_2 += dst_delta; \
161 int av_unused Y, U, V; \
163 #define ENDYUV2RGBFUNC() \
169 #define CLOSEYUV2RGBFUNC(dst_delta) \
170 ENDYUV2RGBLINE(dst_delta) \
173 YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
175 PUTRGB48(dst_1, py_1, 0);
176 PUTRGB48(dst_2, py_2, 0);
179 PUTRGB48(dst_2, py_2, 1);
180 PUTRGB48(dst_1, py_1, 1);
183 PUTRGB48(dst_1, py_1, 2);
184 PUTRGB48(dst_2, py_2, 2);
187 PUTRGB48(dst_2, py_2, 3);
188 PUTRGB48(dst_1, py_1, 3);
191 PUTRGB48(dst_1, py_1, 0);
192 PUTRGB48(dst_2, py_2, 0);
195 PUTRGB48(dst_2, py_2, 1);
196 PUTRGB48(dst_1, py_1, 1);
199 YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
201 PUTBGR48(dst_1, py_1, 0);
202 PUTBGR48(dst_2, py_2, 0);
205 PUTBGR48(dst_2, py_2, 1);
206 PUTBGR48(dst_1, py_1, 1);
209 PUTBGR48(dst_1, py_1, 2);
210 PUTBGR48(dst_2, py_2, 2);
213 PUTBGR48(dst_2, py_2, 3);
214 PUTBGR48(dst_1, py_1, 3);
217 PUTBGR48(dst_1, py_1, 0);
218 PUTBGR48(dst_2, py_2, 0);
221 PUTBGR48(dst_2, py_2, 1);
222 PUTBGR48(dst_1, py_1, 1);
225 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
227 PUTRGB(dst_1, py_1, 0);
228 PUTRGB(dst_2, py_2, 0);
231 PUTRGB(dst_2, py_2, 1);
232 PUTRGB(dst_1, py_1, 1);
235 PUTRGB(dst_1, py_1, 2);
236 PUTRGB(dst_2, py_2, 2);
239 PUTRGB(dst_2, py_2, 3);
240 PUTRGB(dst_1, py_1, 3);
243 PUTRGB(dst_1, py_1, 0);
244 PUTRGB(dst_2, py_2, 0);
247 PUTRGB(dst_2, py_2, 1);
248 PUTRGB(dst_1, py_1, 1);
251 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
253 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
254 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
257 PUTRGBA(dst_2, py_2, pa_1, 1, 24);
258 PUTRGBA(dst_1, py_1, pa_2, 1, 24);
261 PUTRGBA(dst_1, py_1, pa_1, 2, 24);
262 PUTRGBA(dst_2, py_2, pa_2, 2, 24);
265 PUTRGBA(dst_2, py_2, pa_1, 3, 24);
266 PUTRGBA(dst_1, py_1, pa_2, 3, 24);
271 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
272 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
275 PUTRGBA(dst_2, py_2, pa_1, 1, 24);
276 PUTRGBA(dst_1, py_1, pa_2, 1, 24);
279 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
281 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
282 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
285 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
286 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
289 PUTRGBA(dst_1, py_1, pa_1, 2, 0);
290 PUTRGBA(dst_2, py_2, pa_2, 2, 0);
293 PUTRGBA(dst_2, py_2, pa_2, 3, 0);
294 PUTRGBA(dst_1, py_1, pa_1, 3, 0);
299 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
300 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
303 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
304 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
307 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
309 PUTRGB24(dst_1, py_1, 0);
310 PUTRGB24(dst_2, py_2, 0);
313 PUTRGB24(dst_2, py_2, 1);
314 PUTRGB24(dst_1, py_1, 1);
317 PUTRGB24(dst_1, py_1, 2);
318 PUTRGB24(dst_2, py_2, 2);
321 PUTRGB24(dst_2, py_2, 3);
322 PUTRGB24(dst_1, py_1, 3);
325 PUTRGB24(dst_1, py_1, 0);
326 PUTRGB24(dst_2, py_2, 0);
329 PUTRGB24(dst_2, py_2, 1);
330 PUTRGB24(dst_1, py_1, 1);
333 // only trivial mods from yuv2rgb_c_24_rgb
334 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
336 PUTBGR24(dst_1, py_1, 0);
337 PUTBGR24(dst_2, py_2, 0);
340 PUTBGR24(dst_2, py_2, 1);
341 PUTBGR24(dst_1, py_1, 1);
344 PUTBGR24(dst_1, py_1, 2);
345 PUTBGR24(dst_2, py_2, 2);
348 PUTBGR24(dst_2, py_2, 3);
349 PUTBGR24(dst_1, py_1, 3);
352 PUTBGR24(dst_1, py_1, 0);
353 PUTBGR24(dst_2, py_2, 0);
356 PUTBGR24(dst_2, py_2, 1);
357 PUTBGR24(dst_1, py_1, 1);
360 YUV2RGBFUNC(yuv2rgb_c_16_ordered_dither, uint16_t, 0)
361 const uint8_t *d16 = dither_2x2_8[y & 1];
362 const uint8_t *e16 = dither_2x2_4[y & 1];
363 const uint8_t *f16 = dither_2x2_8[(y & 1)^1];
365 #define PUTRGB16(dst, src, i, o) \
367 dst[2 * i] = r[Y + d16[0 + o]] + \
368 g[Y + e16[0 + o]] + \
370 Y = src[2 * i + 1]; \
371 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
372 g[Y + e16[1 + o]] + \
375 PUTRGB16(dst_1, py_1, 0, 0);
376 PUTRGB16(dst_2, py_2, 0, 0 + 8);
379 PUTRGB16(dst_2, py_2, 1, 2 + 8);
380 PUTRGB16(dst_1, py_1, 1, 2);
383 PUTRGB16(dst_1, py_1, 2, 4);
384 PUTRGB16(dst_2, py_2, 2, 4 + 8);
387 PUTRGB16(dst_2, py_2, 3, 6 + 8);
388 PUTRGB16(dst_1, py_1, 3, 6);
391 YUV2RGBFUNC(yuv2rgb_c_15_ordered_dither, uint16_t, 0)
392 const uint8_t *d16 = dither_2x2_8[y & 1];
393 const uint8_t *e16 = dither_2x2_8[(y & 1)^1];
395 #define PUTRGB15(dst, src, i, o) \
397 dst[2 * i] = r[Y + d16[0 + o]] + \
398 g[Y + d16[1 + o]] + \
400 Y = src[2 * i + 1]; \
401 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
402 g[Y + d16[0 + o]] + \
405 PUTRGB15(dst_1, py_1, 0, 0);
406 PUTRGB15(dst_2, py_2, 0, 0 + 8);
409 PUTRGB15(dst_2, py_2, 1, 2 + 8);
410 PUTRGB15(dst_1, py_1, 1, 2);
413 PUTRGB15(dst_1, py_1, 2, 4);
414 PUTRGB15(dst_2, py_2, 2, 4 + 8);
417 PUTRGB15(dst_2, py_2, 3, 6 + 8);
418 PUTRGB15(dst_1, py_1, 3, 6);
421 // r, g, b, dst_1, dst_2
422 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
423 const uint8_t *d16 = dither_4x4_16[y & 3];
425 #define PUTRGB12(dst, src, i, o) \
427 dst[2 * i] = r[Y + d16[0 + o]] + \
428 g[Y + d16[0 + o]] + \
430 Y = src[2 * i + 1]; \
431 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
432 g[Y + d16[1 + o]] + \
436 PUTRGB12(dst_1, py_1, 0, 0);
437 PUTRGB12(dst_2, py_2, 0, 0 + 8);
440 PUTRGB12(dst_2, py_2, 1, 2 + 8);
441 PUTRGB12(dst_1, py_1, 1, 2);
444 PUTRGB12(dst_1, py_1, 2, 4);
445 PUTRGB12(dst_2, py_2, 2, 4 + 8);
448 PUTRGB12(dst_2, py_2, 3, 6 + 8);
449 PUTRGB12(dst_1, py_1, 3, 6);
452 // r, g, b, dst_1, dst_2
453 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
454 const uint8_t *d32 = dither_8x8_32[y & 7];
455 const uint8_t *d64 = dither_8x8_73[y & 7];
457 #define PUTRGB8(dst, src, i, o) \
459 dst[2 * i] = r[Y + d32[0 + o]] + \
460 g[Y + d32[0 + o]] + \
462 Y = src[2 * i + 1]; \
463 dst[2 * i + 1] = r[Y + d32[1 + o]] + \
464 g[Y + d32[1 + o]] + \
468 PUTRGB8(dst_1, py_1, 0, 0);
469 PUTRGB8(dst_2, py_2, 0, 0 + 8);
472 PUTRGB8(dst_2, py_2, 1, 2 + 8);
473 PUTRGB8(dst_1, py_1, 1, 2);
476 PUTRGB8(dst_1, py_1, 2, 4);
477 PUTRGB8(dst_2, py_2, 2, 4 + 8);
480 PUTRGB8(dst_2, py_2, 3, 6 + 8);
481 PUTRGB8(dst_1, py_1, 3, 6);
484 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
485 const uint8_t * d64 = dither_8x8_73[y & 7];
486 const uint8_t *d128 = dither_8x8_220[y & 7];
489 #define PUTRGB4D(dst, src, i, o) \
491 acc = r[Y + d128[0 + o]] + \
492 g[Y + d64[0 + o]] + \
493 b[Y + d128[0 + o]]; \
494 Y = src[2 * i + 1]; \
495 acc |= (r[Y + d128[1 + o]] + \
496 g[Y + d64[1 + o]] + \
497 b[Y + d128[1 + o]]) << 4; \
501 PUTRGB4D(dst_1, py_1, 0, 0);
502 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
505 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
506 PUTRGB4D(dst_1, py_1, 1, 2);
509 PUTRGB4D(dst_1, py_1, 2, 4);
510 PUTRGB4D(dst_2, py_2, 2, 4 + 8);
513 PUTRGB4D(dst_2, py_2, 3, 6 + 8);
514 PUTRGB4D(dst_1, py_1, 3, 6);
517 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
518 const uint8_t *d64 = dither_8x8_73[y & 7];
519 const uint8_t *d128 = dither_8x8_220[y & 7];
521 #define PUTRGB4DB(dst, src, i, o) \
523 dst[2 * i] = r[Y + d128[0 + o]] + \
524 g[Y + d64[0 + o]] + \
525 b[Y + d128[0 + o]]; \
526 Y = src[2 * i + 1]; \
527 dst[2 * i + 1] = r[Y + d128[1 + o]] + \
528 g[Y + d64[1 + o]] + \
532 PUTRGB4DB(dst_1, py_1, 0, 0);
533 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
536 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
537 PUTRGB4DB(dst_1, py_1, 1, 2);
540 PUTRGB4DB(dst_1, py_1, 2, 4);
541 PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
544 PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
545 PUTRGB4DB(dst_1, py_1, 3, 6);
548 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
549 const uint8_t *d128 = dither_8x8_220[y & 7];
550 char out_1 = 0, out_2 = 0;
551 g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
553 #define PUTRGB1(out, src, i, o) \
555 out += out + g[Y + d128[0 + o]]; \
556 Y = src[2 * i + 1]; \
557 out += out + g[Y + d128[1 + o]];
559 PUTRGB1(out_1, py_1, 0, 0);
560 PUTRGB1(out_2, py_2, 0, 0 + 8);
562 PUTRGB1(out_2, py_2, 1, 2 + 8);
563 PUTRGB1(out_1, py_1, 1, 2);
565 PUTRGB1(out_1, py_1, 2, 4);
566 PUTRGB1(out_2, py_2, 2, 4 + 8);
568 PUTRGB1(out_2, py_2, 3, 6 + 8);
569 PUTRGB1(out_1, py_1, 3, 6);
575 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
580 t = ff_yuv2rgb_init_mmx(c);
582 t = ff_yuv2rgb_init_vis(c);
583 else if (HAVE_ALTIVEC)
584 t = ff_yuv2rgb_init_altivec(c);
586 t = ff_yuv2rgb_get_func_ptr_bfin(c);
591 av_log(c, AV_LOG_WARNING,
592 "No accelerated colorspace conversion found from %s to %s.\n",
593 av_get_pix_fmt_name(c->srcFormat), av_get_pix_fmt_name(c->dstFormat));
595 switch (c->dstFormat) {
596 case PIX_FMT_BGR48BE:
597 case PIX_FMT_BGR48LE:
598 return yuv2rgb_c_bgr48;
599 case PIX_FMT_RGB48BE:
600 case PIX_FMT_RGB48LE:
604 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat))
608 return (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat)) ? yuva2rgba_c : yuv2rgb_c_32;
610 return yuv2rgb_c_24_rgb;
612 return yuv2rgb_c_24_bgr;
615 return yuv2rgb_c_16_ordered_dither;
618 return yuv2rgb_c_15_ordered_dither;
621 return yuv2rgb_c_12_ordered_dither;
624 return yuv2rgb_c_8_ordered_dither;
627 return yuv2rgb_c_4_ordered_dither;
628 case PIX_FMT_RGB4_BYTE:
629 case PIX_FMT_BGR4_BYTE:
630 return yuv2rgb_c_4b_ordered_dither;
631 case PIX_FMT_MONOBLACK:
632 return yuv2rgb_c_1_ordered_dither;
637 static void fill_table(uint8_t* table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize,
638 const int inc, void *y_tab)
641 uint8_t *y_table = y_tab;
643 y_table -= elemsize * (inc >> 9);
645 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
646 int64_t cb = av_clip(i-YUVRGB_TABLE_HEADROOM, 0, 255)*inc;
647 table[i] = y_table + elemsize * (cb >> 16);
651 static void fill_gv_table(int table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize, const int inc)
654 int off = -(inc >> 9);
656 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
657 int64_t cb = av_clip(i-YUVRGB_TABLE_HEADROOM, 0, 255)*inc;
658 table[i] = elemsize * (off + (cb >> 16));
662 static uint16_t roundToInt16(int64_t f)
664 int r = (f + (1 << 15)) >> 16;
674 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
675 int fullRange, int brightness,
676 int contrast, int saturation)
678 const int isRgb = c->dstFormat == PIX_FMT_RGB32 ||
679 c->dstFormat == PIX_FMT_RGB32_1 ||
680 c->dstFormat == PIX_FMT_BGR24 ||
681 c->dstFormat == PIX_FMT_RGB565BE ||
682 c->dstFormat == PIX_FMT_RGB565LE ||
683 c->dstFormat == PIX_FMT_RGB555BE ||
684 c->dstFormat == PIX_FMT_RGB555LE ||
685 c->dstFormat == PIX_FMT_RGB444BE ||
686 c->dstFormat == PIX_FMT_RGB444LE ||
687 c->dstFormat == PIX_FMT_RGB8 ||
688 c->dstFormat == PIX_FMT_RGB4 ||
689 c->dstFormat == PIX_FMT_RGB4_BYTE ||
690 c->dstFormat == PIX_FMT_MONOBLACK;
691 const int isNotNe = c->dstFormat == PIX_FMT_NE(RGB565LE, RGB565BE) ||
692 c->dstFormat == PIX_FMT_NE(RGB555LE, RGB555BE) ||
693 c->dstFormat == PIX_FMT_NE(RGB444LE, RGB444BE) ||
694 c->dstFormat == PIX_FMT_NE(BGR565LE, BGR565BE) ||
695 c->dstFormat == PIX_FMT_NE(BGR555LE, BGR555BE) ||
696 c->dstFormat == PIX_FMT_NE(BGR444LE, BGR444BE);
697 const int bpp = c->dstFormatBpp;
701 int i, base, rbase, gbase, bbase, av_uninit(abase), needAlpha;
702 const int yoffs = fullRange ? 384 : 326;
704 int64_t crv = inv_table[0];
705 int64_t cbu = inv_table[1];
706 int64_t cgu = -inv_table[2];
707 int64_t cgv = -inv_table[3];
708 int64_t cy = 1 << 16;
713 cy = (cy * 255) / 219;
716 crv = (crv * 224) / 255;
717 cbu = (cbu * 224) / 255;
718 cgu = (cgu * 224) / 255;
719 cgv = (cgv * 224) / 255;
722 cy = (cy * contrast) >> 16;
723 crv = (crv * contrast * saturation) >> 32;
724 cbu = (cbu * contrast * saturation) >> 32;
725 cgu = (cgu * contrast * saturation) >> 32;
726 cgv = (cgv * contrast * saturation) >> 32;
727 oy -= 256 * brightness;
729 c->uOffset = 0x0400040004000400LL;
730 c->vOffset = 0x0400040004000400LL;
731 c->yCoeff = roundToInt16(cy * 8192) * 0x0001000100010001ULL;
732 c->vrCoeff = roundToInt16(crv * 8192) * 0x0001000100010001ULL;
733 c->ubCoeff = roundToInt16(cbu * 8192) * 0x0001000100010001ULL;
734 c->vgCoeff = roundToInt16(cgv * 8192) * 0x0001000100010001ULL;
735 c->ugCoeff = roundToInt16(cgu * 8192) * 0x0001000100010001ULL;
736 c->yOffset = roundToInt16(oy * 8) * 0x0001000100010001ULL;
738 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy << 13);
739 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
740 c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv << 13);
741 c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv << 13);
742 c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu << 13);
743 c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu << 13);
745 //scale coefficients by cy
746 crv = ((crv << 16) + 0x8000) / cy;
747 cbu = ((cbu << 16) + 0x8000) / cy;
748 cgu = ((cgu << 16) + 0x8000) / cy;
749 cgv = ((cgv << 16) + 0x8000) / cy;
751 av_free(c->yuvTable);
755 c->yuvTable = av_malloc(1024);
756 y_table = c->yuvTable;
757 yb = -(384 << 16) - oy;
758 for (i = 0; i < 1024 - 110; i++) {
759 y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
762 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
763 fill_gv_table(c->table_gV, 1, cgv);
767 rbase = isRgb ? 3 : 0;
769 bbase = isRgb ? 0 : 3;
770 c->yuvTable = av_malloc(1024 * 3);
771 y_table = c->yuvTable;
772 yb = -(384 << 16) - oy;
773 for (i = 0; i < 1024 - 110; i++) {
774 int yval = av_clip_uint8((yb + 0x8000) >> 16);
775 y_table[i + 110] = (yval >> 7) << rbase;
776 y_table[i + 37 + 1024] = ((yval + 43) / 85) << gbase;
777 y_table[i + 110 + 2048] = (yval >> 7) << bbase;
780 fill_table(c->table_rV, 1, crv, y_table + yoffs);
781 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
782 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
783 fill_gv_table(c->table_gV, 1, cgv);
786 rbase = isRgb ? 5 : 0;
787 gbase = isRgb ? 2 : 3;
788 bbase = isRgb ? 0 : 6;
789 c->yuvTable = av_malloc(1024 * 3);
790 y_table = c->yuvTable;
791 yb = -(384 << 16) - oy;
792 for (i = 0; i < 1024 - 38; i++) {
793 int yval = av_clip_uint8((yb + 0x8000) >> 16);
794 y_table[i + 16] = ((yval + 18) / 36) << rbase;
795 y_table[i + 16 + 1024] = ((yval + 18) / 36) << gbase;
796 y_table[i + 37 + 2048] = ((yval + 43) / 85) << bbase;
799 fill_table(c->table_rV, 1, crv, y_table + yoffs);
800 fill_table(c->table_gU, 1, cgu, y_table + yoffs + 1024);
801 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2048);
802 fill_gv_table(c->table_gV, 1, cgv);
805 rbase = isRgb ? 8 : 0;
807 bbase = isRgb ? 0 : 8;
808 c->yuvTable = av_malloc(1024 * 3 * 2);
809 y_table16 = c->yuvTable;
810 yb = -(384 << 16) - oy;
811 for (i = 0; i < 1024; i++) {
812 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
813 y_table16[i] = (yval >> 4) << rbase;
814 y_table16[i + 1024] = (yval >> 4) << gbase;
815 y_table16[i + 2048] = (yval >> 4) << bbase;
819 for (i = 0; i < 1024 * 3; i++)
820 y_table16[i] = av_bswap16(y_table16[i]);
821 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
822 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
823 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
824 fill_gv_table(c->table_gV, 2, cgv);
828 rbase = isRgb ? bpp - 5 : 0;
830 bbase = isRgb ? 0 : (bpp - 5);
831 c->yuvTable = av_malloc(1024 * 3 * 2);
832 y_table16 = c->yuvTable;
833 yb = -(384 << 16) - oy;
834 for (i = 0; i < 1024; i++) {
835 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
836 y_table16[i] = (yval >> 3) << rbase;
837 y_table16[i + 1024] = (yval >> (18 - bpp)) << gbase;
838 y_table16[i + 2048] = (yval >> 3) << bbase;
842 for (i = 0; i < 1024 * 3; i++)
843 y_table16[i] = av_bswap16(y_table16[i]);
844 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
845 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + 1024);
846 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2048);
847 fill_gv_table(c->table_gV, 2, cgv);
851 c->yuvTable = av_malloc(1024);
852 y_table = c->yuvTable;
853 yb = -(384 << 16) - oy;
854 for (i = 0; i < 1024; i++) {
855 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
858 fill_table(c->table_rV, 1, crv, y_table + yoffs);
859 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
860 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
861 fill_gv_table(c->table_gV, 1, cgv);
864 base = (c->dstFormat == PIX_FMT_RGB32_1 ||
865 c->dstFormat == PIX_FMT_BGR32_1) ? 8 : 0;
866 rbase = base + (isRgb ? 16 : 0);
868 bbase = base + (isRgb ? 0 : 16);
869 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
871 abase = (base + 24) & 31;
872 c->yuvTable = av_malloc(1024 * 3 * 4);
873 y_table32 = c->yuvTable;
874 yb = -(384 << 16) - oy;
875 for (i = 0; i < 1024; i++) {
876 unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
877 y_table32[i] = (yval << rbase) +
878 (needAlpha ? 0 : (255u << abase));
879 y_table32[i + 1024] = yval << gbase;
880 y_table32[i + 2048] = yval << bbase;
883 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
884 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + 1024);
885 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2048);
886 fill_gv_table(c->table_gV, 4, cgv);
890 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);