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/bswap.h"
34 #include "swscale_internal.h"
35 #include "libavutil/pixdesc.h"
37 /* Color space conversion coefficients for YCbCr -> RGB mapping.
39 * Entries are {crv, cbu, cgu, cgv}
41 * crv = (255 / 224) * 65536 * (1 - cr) / 0.5
42 * cbu = (255 / 224) * 65536 * (1 - cb) / 0.5
43 * cgu = (255 / 224) * 65536 * (cb / cg) * (1 - cb) / 0.5
44 * cgv = (255 / 224) * 65536 * (cr / cg) * (1 - cr) / 0.5
46 * where Y = cr * R + cg * G + cb * B and cr + cg + cb = 1.
48 const int32_t ff_yuv2rgb_coeffs[11][4] = {
49 { 117489, 138438, 13975, 34925 }, /* no sequence_display_extension */
50 { 117489, 138438, 13975, 34925 }, /* ITU-R Rec. 709 (1990) */
51 { 104597, 132201, 25675, 53279 }, /* unspecified */
52 { 104597, 132201, 25675, 53279 }, /* reserved */
53 { 104448, 132798, 24759, 53109 }, /* FCC */
54 { 104597, 132201, 25675, 53279 }, /* ITU-R Rec. 624-4 System B, G */
55 { 104597, 132201, 25675, 53279 }, /* SMPTE 170M */
56 { 117579, 136230, 16907, 35559 }, /* SMPTE 240M (1987) */
58 { 110013, 140363, 12277, 42626 }, /* Bt-2020-NCL */
59 { 110013, 140363, 12277, 42626 }, /* Bt-2020-CL */
62 const int *sws_getCoefficients(int colorspace)
64 if (colorspace > 10 || colorspace < 0 || colorspace == 8)
65 colorspace = SWS_CS_DEFAULT;
66 return ff_yuv2rgb_coeffs[colorspace];
69 #define LOADCHROMA(i) \
72 r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
73 g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
74 b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
76 #define PUTRGB(dst, src, i) \
78 dst[2 * i] = r[Y] + g[Y] + b[Y]; \
80 dst[2 * i + 1] = r[Y] + g[Y] + b[Y];
82 #define PUTRGB24(dst, src, i) \
84 dst[6 * i + 0] = r[Y]; \
85 dst[6 * i + 1] = g[Y]; \
86 dst[6 * i + 2] = b[Y]; \
88 dst[6 * i + 3] = r[Y]; \
89 dst[6 * i + 4] = g[Y]; \
90 dst[6 * i + 5] = b[Y];
92 #define PUTBGR24(dst, src, i) \
94 dst[6 * i + 0] = b[Y]; \
95 dst[6 * i + 1] = g[Y]; \
96 dst[6 * i + 2] = r[Y]; \
98 dst[6 * i + 3] = b[Y]; \
99 dst[6 * i + 4] = g[Y]; \
100 dst[6 * i + 5] = r[Y];
102 #define PUTRGBA(dst, ysrc, asrc, i, s) \
104 dst[2 * i] = r[Y] + g[Y] + b[Y] + (asrc[2 * i] << s); \
105 Y = ysrc[2 * i + 1]; \
106 dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + (asrc[2 * i + 1] << s);
108 #define PUTRGB48(dst, src, i) \
110 dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
111 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
112 dst[12 * i + 4] = dst[12 * i + 5] = b[Y]; \
113 Y = src[ 2 * i + 1]; \
114 dst[12 * i + 6] = dst[12 * i + 7] = r[Y]; \
115 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
116 dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
118 #define PUTBGR48(dst, src, i) \
120 dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
121 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
122 dst[12 * i + 4] = dst[12 * i + 5] = r[Y]; \
123 Y = src[2 * i + 1]; \
124 dst[12 * i + 6] = dst[12 * i + 7] = b[Y]; \
125 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
126 dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
128 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
129 static int func_name(SwsContext *c, const uint8_t *src[], \
130 int srcStride[], int srcSliceY, int srcSliceH, \
131 uint8_t *dst[], int dstStride[]) \
135 if (!alpha && c->srcFormat == AV_PIX_FMT_YUV422P) { \
139 for (y = 0; y < srcSliceH; y += 2) { \
140 int yd = y + srcSliceY; \
142 (dst_type *)(dst[0] + (yd) * dstStride[0]); \
144 (dst_type *)(dst[0] + (yd + 1) * dstStride[0]); \
145 dst_type av_unused *r, *g, *b; \
146 const uint8_t *py_1 = src[0] + y * srcStride[0]; \
147 const uint8_t *py_2 = py_1 + srcStride[0]; \
148 const uint8_t av_unused *pu = src[1] + (y >> 1) * srcStride[1]; \
149 const uint8_t av_unused *pv = src[2] + (y >> 1) * srcStride[2]; \
150 const uint8_t av_unused *pa_1, *pa_2; \
151 unsigned int h_size = c->dstW >> 3; \
153 pa_1 = src[3] + y * srcStride[3]; \
154 pa_2 = pa_1 + srcStride[3]; \
157 int av_unused U, V, Y; \
159 #define ENDYUV2RGBLINE(dst_delta, ss) \
164 dst_1 += dst_delta >> ss; \
165 dst_2 += dst_delta >> ss; \
167 if (c->dstW & (4 >> ss)) { \
168 int av_unused Y, U, V; \
170 #define ENDYUV2RGBFUNC() \
176 #define CLOSEYUV2RGBFUNC(dst_delta) \
177 ENDYUV2RGBLINE(dst_delta, 0) \
180 YUV2RGBFUNC(yuv2rgb_c_48, uint8_t, 0)
182 PUTRGB48(dst_1, py_1, 0);
183 PUTRGB48(dst_2, py_2, 0);
186 PUTRGB48(dst_2, py_2, 1);
187 PUTRGB48(dst_1, py_1, 1);
190 PUTRGB48(dst_1, py_1, 2);
191 PUTRGB48(dst_2, py_2, 2);
194 PUTRGB48(dst_2, py_2, 3);
195 PUTRGB48(dst_1, py_1, 3);
196 ENDYUV2RGBLINE(48, 0)
198 PUTRGB48(dst_1, py_1, 0);
199 PUTRGB48(dst_2, py_2, 0);
202 PUTRGB48(dst_2, py_2, 1);
203 PUTRGB48(dst_1, py_1, 1);
204 ENDYUV2RGBLINE(48, 1)
206 PUTRGB48(dst_1, py_1, 0);
207 PUTRGB48(dst_2, py_2, 0);
210 YUV2RGBFUNC(yuv2rgb_c_bgr48, uint8_t, 0)
212 PUTBGR48(dst_1, py_1, 0);
213 PUTBGR48(dst_2, py_2, 0);
216 PUTBGR48(dst_2, py_2, 1);
217 PUTBGR48(dst_1, py_1, 1);
220 PUTBGR48(dst_1, py_1, 2);
221 PUTBGR48(dst_2, py_2, 2);
224 PUTBGR48(dst_2, py_2, 3);
225 PUTBGR48(dst_1, py_1, 3);
226 ENDYUV2RGBLINE(48, 0)
228 PUTBGR48(dst_1, py_1, 0);
229 PUTBGR48(dst_2, py_2, 0);
232 PUTBGR48(dst_2, py_2, 1);
233 PUTBGR48(dst_1, py_1, 1);
234 ENDYUV2RGBLINE(48, 1)
236 PUTBGR48(dst_1, py_1, 0);
237 PUTBGR48(dst_2, py_2, 0);
240 YUV2RGBFUNC(yuv2rgb_c_32, uint32_t, 0)
242 PUTRGB(dst_1, py_1, 0);
243 PUTRGB(dst_2, py_2, 0);
246 PUTRGB(dst_2, py_2, 1);
247 PUTRGB(dst_1, py_1, 1);
250 PUTRGB(dst_1, py_1, 2);
251 PUTRGB(dst_2, py_2, 2);
254 PUTRGB(dst_2, py_2, 3);
255 PUTRGB(dst_1, py_1, 3);
258 PUTRGB(dst_1, py_1, 0);
259 PUTRGB(dst_2, py_2, 0);
262 PUTRGB(dst_2, py_2, 1);
263 PUTRGB(dst_1, py_1, 1);
266 PUTRGB(dst_1, py_1, 0);
267 PUTRGB(dst_2, py_2, 0);
271 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
273 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
276 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
277 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
280 PUTRGBA(dst_2, py_2, pa_2, 1, 24);
281 PUTRGBA(dst_1, py_1, pa_1, 1, 24);
284 PUTRGBA(dst_1, py_1, pa_1, 2, 24);
285 PUTRGBA(dst_2, py_2, pa_2, 2, 24);
288 PUTRGBA(dst_2, py_2, pa_2, 3, 24);
289 PUTRGBA(dst_1, py_1, pa_1, 3, 24);
294 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
295 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
298 PUTRGBA(dst_2, py_2, pa_2, 1, 24);
299 PUTRGBA(dst_1, py_1, pa_1, 1, 24);
304 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
305 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
309 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
311 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
314 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
315 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
318 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
319 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
322 PUTRGBA(dst_1, py_1, pa_1, 2, 0);
323 PUTRGBA(dst_2, py_2, pa_2, 2, 0);
326 PUTRGBA(dst_2, py_2, pa_2, 3, 0);
327 PUTRGBA(dst_1, py_1, pa_1, 3, 0);
332 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
333 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
336 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
337 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
342 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
343 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
346 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
348 PUTRGB24(dst_1, py_1, 0);
349 PUTRGB24(dst_2, py_2, 0);
352 PUTRGB24(dst_2, py_2, 1);
353 PUTRGB24(dst_1, py_1, 1);
356 PUTRGB24(dst_1, py_1, 2);
357 PUTRGB24(dst_2, py_2, 2);
360 PUTRGB24(dst_2, py_2, 3);
361 PUTRGB24(dst_1, py_1, 3);
362 ENDYUV2RGBLINE(24, 0)
364 PUTRGB24(dst_1, py_1, 0);
365 PUTRGB24(dst_2, py_2, 0);
368 PUTRGB24(dst_2, py_2, 1);
369 PUTRGB24(dst_1, py_1, 1);
370 ENDYUV2RGBLINE(24, 1)
372 PUTRGB24(dst_1, py_1, 0);
373 PUTRGB24(dst_2, py_2, 0);
376 // only trivial mods from yuv2rgb_c_24_rgb
377 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
379 PUTBGR24(dst_1, py_1, 0);
380 PUTBGR24(dst_2, py_2, 0);
383 PUTBGR24(dst_2, py_2, 1);
384 PUTBGR24(dst_1, py_1, 1);
387 PUTBGR24(dst_1, py_1, 2);
388 PUTBGR24(dst_2, py_2, 2);
391 PUTBGR24(dst_2, py_2, 3);
392 PUTBGR24(dst_1, py_1, 3);
393 ENDYUV2RGBLINE(24, 0)
395 PUTBGR24(dst_1, py_1, 0);
396 PUTBGR24(dst_2, py_2, 0);
399 PUTBGR24(dst_2, py_2, 1);
400 PUTBGR24(dst_1, py_1, 1);
401 ENDYUV2RGBLINE(24, 1)
403 PUTBGR24(dst_1, py_1, 0);
404 PUTBGR24(dst_2, py_2, 0);
407 YUV2RGBFUNC(yuv2rgb_c_16_ordered_dither, uint16_t, 0)
408 const uint8_t *d16 = ff_dither_2x2_8[y & 1];
409 const uint8_t *e16 = ff_dither_2x2_4[y & 1];
410 const uint8_t *f16 = ff_dither_2x2_8[(y & 1)^1];
412 #define PUTRGB16(dst, src, i, o) \
414 dst[2 * i] = r[Y + d16[0 + o]] + \
415 g[Y + e16[0 + o]] + \
417 Y = src[2 * i + 1]; \
418 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
419 g[Y + e16[1 + o]] + \
422 PUTRGB16(dst_1, py_1, 0, 0);
423 PUTRGB16(dst_2, py_2, 0, 0 + 8);
426 PUTRGB16(dst_2, py_2, 1, 2 + 8);
427 PUTRGB16(dst_1, py_1, 1, 2);
430 PUTRGB16(dst_1, py_1, 2, 4);
431 PUTRGB16(dst_2, py_2, 2, 4 + 8);
434 PUTRGB16(dst_2, py_2, 3, 6 + 8);
435 PUTRGB16(dst_1, py_1, 3, 6);
438 YUV2RGBFUNC(yuv2rgb_c_15_ordered_dither, uint16_t, 0)
439 const uint8_t *d16 = ff_dither_2x2_8[y & 1];
440 const uint8_t *e16 = ff_dither_2x2_8[(y & 1)^1];
442 #define PUTRGB15(dst, src, i, o) \
444 dst[2 * i] = r[Y + d16[0 + o]] + \
445 g[Y + d16[1 + o]] + \
447 Y = src[2 * i + 1]; \
448 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
449 g[Y + d16[0 + o]] + \
452 PUTRGB15(dst_1, py_1, 0, 0);
453 PUTRGB15(dst_2, py_2, 0, 0 + 8);
456 PUTRGB15(dst_2, py_2, 1, 2 + 8);
457 PUTRGB15(dst_1, py_1, 1, 2);
460 PUTRGB15(dst_1, py_1, 2, 4);
461 PUTRGB15(dst_2, py_2, 2, 4 + 8);
464 PUTRGB15(dst_2, py_2, 3, 6 + 8);
465 PUTRGB15(dst_1, py_1, 3, 6);
468 // r, g, b, dst_1, dst_2
469 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
470 const uint8_t *d16 = ff_dither_4x4_16[y & 3];
472 #define PUTRGB12(dst, src, i, o) \
474 dst[2 * i] = r[Y + d16[0 + o]] + \
475 g[Y + d16[0 + o]] + \
477 Y = src[2 * i + 1]; \
478 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
479 g[Y + d16[1 + o]] + \
483 PUTRGB12(dst_1, py_1, 0, 0);
484 PUTRGB12(dst_2, py_2, 0, 0 + 8);
487 PUTRGB12(dst_2, py_2, 1, 2 + 8);
488 PUTRGB12(dst_1, py_1, 1, 2);
491 PUTRGB12(dst_1, py_1, 2, 4);
492 PUTRGB12(dst_2, py_2, 2, 4 + 8);
495 PUTRGB12(dst_2, py_2, 3, 6 + 8);
496 PUTRGB12(dst_1, py_1, 3, 6);
499 // r, g, b, dst_1, dst_2
500 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
501 const uint8_t *d32 = ff_dither_8x8_32[yd & 7];
502 const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
504 #define PUTRGB8(dst, src, i, o) \
506 dst[2 * i] = r[Y + d32[0 + o]] + \
507 g[Y + d32[0 + o]] + \
509 Y = src[2 * i + 1]; \
510 dst[2 * i + 1] = r[Y + d32[1 + o]] + \
511 g[Y + d32[1 + o]] + \
515 PUTRGB8(dst_1, py_1, 0, 0);
516 PUTRGB8(dst_2, py_2, 0, 0 + 8);
519 PUTRGB8(dst_2, py_2, 1, 2 + 8);
520 PUTRGB8(dst_1, py_1, 1, 2);
523 PUTRGB8(dst_1, py_1, 2, 4);
524 PUTRGB8(dst_2, py_2, 2, 4 + 8);
527 PUTRGB8(dst_2, py_2, 3, 6 + 8);
528 PUTRGB8(dst_1, py_1, 3, 6);
531 const uint8_t *d32 = ff_dither_8x8_32[yd & 7];
532 const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
534 PUTRGB8(dst_1, py_1, 0, 0);
535 PUTRGB8(dst_2, py_2, 0, 0 + 8);
538 PUTRGB8(dst_2, py_2, 1, 2 + 8);
539 PUTRGB8(dst_1, py_1, 1, 2);
542 const uint8_t *d32 = ff_dither_8x8_32[yd & 7];
543 const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
545 PUTRGB8(dst_1, py_1, 0, 0);
546 PUTRGB8(dst_2, py_2, 0, 0 + 8);
551 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
552 const uint8_t * d64 = ff_dither_8x8_73[yd & 7];
553 const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
556 #define PUTRGB4D(dst, src, i, o) \
558 acc = r[Y + d128[0 + o]] + \
559 g[Y + d64[0 + o]] + \
560 b[Y + d128[0 + o]]; \
561 Y = src[2 * i + 1]; \
562 acc |= (r[Y + d128[1 + o]] + \
563 g[Y + d64[1 + o]] + \
564 b[Y + d128[1 + o]]) << 4; \
568 PUTRGB4D(dst_1, py_1, 0, 0);
569 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
572 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
573 PUTRGB4D(dst_1, py_1, 1, 2);
576 PUTRGB4D(dst_1, py_1, 2, 4);
577 PUTRGB4D(dst_2, py_2, 2, 4 + 8);
580 PUTRGB4D(dst_2, py_2, 3, 6 + 8);
581 PUTRGB4D(dst_1, py_1, 3, 6);
584 const uint8_t * d64 = ff_dither_8x8_73[yd & 7];
585 const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
588 PUTRGB4D(dst_1, py_1, 0, 0);
589 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
592 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
593 PUTRGB4D(dst_1, py_1, 1, 2);
596 const uint8_t * d64 = ff_dither_8x8_73[yd & 7];
597 const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
600 PUTRGB4D(dst_1, py_1, 0, 0);
601 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
604 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
605 const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
606 const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
608 #define PUTRGB4DB(dst, src, i, o) \
610 dst[2 * i] = r[Y + d128[0 + o]] + \
611 g[Y + d64[0 + o]] + \
612 b[Y + d128[0 + o]]; \
613 Y = src[2 * i + 1]; \
614 dst[2 * i + 1] = r[Y + d128[1 + o]] + \
615 g[Y + d64[1 + o]] + \
619 PUTRGB4DB(dst_1, py_1, 0, 0);
620 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
623 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
624 PUTRGB4DB(dst_1, py_1, 1, 2);
627 PUTRGB4DB(dst_1, py_1, 2, 4);
628 PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
631 PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
632 PUTRGB4DB(dst_1, py_1, 3, 6);
634 const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
635 const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
637 PUTRGB4DB(dst_1, py_1, 0, 0);
638 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
641 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
642 PUTRGB4DB(dst_1, py_1, 1, 2);
644 const uint8_t *d64 = ff_dither_8x8_73[yd & 7];
645 const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
647 PUTRGB4DB(dst_1, py_1, 0, 0);
648 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
651 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
652 const uint8_t *d128 = ff_dither_8x8_220[yd & 7];
653 char out_1 = 0, out_2 = 0;
654 g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
656 #define PUTRGB1(out, src, i, o) \
658 out += out + g[Y + d128[0 + o]]; \
659 Y = src[2 * i + 1]; \
660 out += out + g[Y + d128[1 + o]];
662 PUTRGB1(out_1, py_1, 0, 0);
663 PUTRGB1(out_2, py_2, 0, 0 + 8);
665 PUTRGB1(out_2, py_2, 1, 2 + 8);
666 PUTRGB1(out_1, py_1, 1, 2);
668 PUTRGB1(out_1, py_1, 2, 4);
669 PUTRGB1(out_2, py_2, 2, 4 + 8);
671 PUTRGB1(out_2, py_2, 3, 6 + 8);
672 PUTRGB1(out_1, py_1, 3, 6);
678 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
683 t = ff_yuv2rgb_init_ppc(c);
685 t = ff_yuv2rgb_init_x86(c);
686 #elif ARCH_LOONGARCH64
687 t = ff_yuv2rgb_init_loongarch(c);
693 av_log(c, AV_LOG_WARNING,
694 "No accelerated colorspace conversion found from %s to %s.\n",
695 av_get_pix_fmt_name(c->srcFormat), av_get_pix_fmt_name(c->dstFormat));
697 switch (c->dstFormat) {
698 case AV_PIX_FMT_BGR48BE:
699 case AV_PIX_FMT_BGR48LE:
700 return yuv2rgb_c_bgr48;
701 case AV_PIX_FMT_RGB48BE:
702 case AV_PIX_FMT_RGB48LE:
704 case AV_PIX_FMT_ARGB:
705 case AV_PIX_FMT_ABGR:
706 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat))
708 case AV_PIX_FMT_RGBA:
709 case AV_PIX_FMT_BGRA:
710 return (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat)) ? yuva2rgba_c : yuv2rgb_c_32;
711 case AV_PIX_FMT_RGB24:
712 return yuv2rgb_c_24_rgb;
713 case AV_PIX_FMT_BGR24:
714 return yuv2rgb_c_24_bgr;
715 case AV_PIX_FMT_RGB565:
716 case AV_PIX_FMT_BGR565:
717 return yuv2rgb_c_16_ordered_dither;
718 case AV_PIX_FMT_RGB555:
719 case AV_PIX_FMT_BGR555:
720 return yuv2rgb_c_15_ordered_dither;
721 case AV_PIX_FMT_RGB444:
722 case AV_PIX_FMT_BGR444:
723 return yuv2rgb_c_12_ordered_dither;
724 case AV_PIX_FMT_RGB8:
725 case AV_PIX_FMT_BGR8:
726 return yuv2rgb_c_8_ordered_dither;
727 case AV_PIX_FMT_RGB4:
728 case AV_PIX_FMT_BGR4:
729 return yuv2rgb_c_4_ordered_dither;
730 case AV_PIX_FMT_RGB4_BYTE:
731 case AV_PIX_FMT_BGR4_BYTE:
732 return yuv2rgb_c_4b_ordered_dither;
733 case AV_PIX_FMT_MONOBLACK:
734 return yuv2rgb_c_1_ordered_dither;
739 static void fill_table(uint8_t* table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize,
740 const int64_t inc, void *y_tab)
743 uint8_t *y_table = y_tab;
745 y_table -= elemsize * (inc >> 9);
747 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
748 int64_t cb = av_clip_uint8(i-YUVRGB_TABLE_HEADROOM)*inc;
749 table[i] = y_table + elemsize * (cb >> 16);
753 static void fill_gv_table(int table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize, const int64_t inc)
756 int off = -(inc >> 9);
758 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
759 int64_t cb = av_clip_uint8(i-YUVRGB_TABLE_HEADROOM)*inc;
760 table[i] = elemsize * (off + (cb >> 16));
764 static uint16_t roundToInt16(int64_t f)
766 int r = (f + (1 << 15)) >> 16;
776 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
777 int fullRange, int brightness,
778 int contrast, int saturation)
780 const int isRgb = c->dstFormat == AV_PIX_FMT_RGB32 ||
781 c->dstFormat == AV_PIX_FMT_RGB32_1 ||
782 c->dstFormat == AV_PIX_FMT_BGR24 ||
783 c->dstFormat == AV_PIX_FMT_RGB565BE ||
784 c->dstFormat == AV_PIX_FMT_RGB565LE ||
785 c->dstFormat == AV_PIX_FMT_RGB555BE ||
786 c->dstFormat == AV_PIX_FMT_RGB555LE ||
787 c->dstFormat == AV_PIX_FMT_RGB444BE ||
788 c->dstFormat == AV_PIX_FMT_RGB444LE ||
789 c->dstFormat == AV_PIX_FMT_X2RGB10BE ||
790 c->dstFormat == AV_PIX_FMT_X2RGB10LE ||
791 c->dstFormat == AV_PIX_FMT_RGB8 ||
792 c->dstFormat == AV_PIX_FMT_RGB4 ||
793 c->dstFormat == AV_PIX_FMT_RGB4_BYTE ||
794 c->dstFormat == AV_PIX_FMT_MONOBLACK;
795 const int isNotNe = c->dstFormat == AV_PIX_FMT_NE(RGB565LE, RGB565BE) ||
796 c->dstFormat == AV_PIX_FMT_NE(RGB555LE, RGB555BE) ||
797 c->dstFormat == AV_PIX_FMT_NE(RGB444LE, RGB444BE) ||
798 c->dstFormat == AV_PIX_FMT_NE(BGR565LE, BGR565BE) ||
799 c->dstFormat == AV_PIX_FMT_NE(BGR555LE, BGR555BE) ||
800 c->dstFormat == AV_PIX_FMT_NE(BGR444LE, BGR444BE) ||
801 c->dstFormat == AV_PIX_FMT_NE(X2RGB10LE, X2RGB10BE) ||
802 c->dstFormat == AV_PIX_FMT_NE(X2BGR10LE, X2BGR10BE);
803 const int bpp = c->dstFormatBpp;
807 int i, base, rbase, gbase, bbase, av_uninit(abase), needAlpha;
808 const int yoffs = (fullRange ? 384 : 326) + YUVRGB_TABLE_LUMA_HEADROOM;
809 const int table_plane_size = 1024 + 2*YUVRGB_TABLE_LUMA_HEADROOM;
811 int64_t crv = inv_table[0];
812 int64_t cbu = inv_table[1];
813 int64_t cgu = -inv_table[2];
814 int64_t cgv = -inv_table[3];
815 int64_t cy = 1 << 16;
820 cy = (cy * 255) / 219;
823 crv = (crv * 224) / 255;
824 cbu = (cbu * 224) / 255;
825 cgu = (cgu * 224) / 255;
826 cgv = (cgv * 224) / 255;
829 cy = (cy * contrast) >> 16;
830 crv = (crv * contrast * saturation) >> 32;
831 cbu = (cbu * contrast * saturation) >> 32;
832 cgu = (cgu * contrast * saturation) >> 32;
833 cgv = (cgv * contrast * saturation) >> 32;
834 oy -= 256 * brightness;
836 c->uOffset = 0x0400040004000400LL;
837 c->vOffset = 0x0400040004000400LL;
838 c->yCoeff = roundToInt16(cy * (1 << 13)) * 0x0001000100010001ULL;
839 c->vrCoeff = roundToInt16(crv * (1 << 13)) * 0x0001000100010001ULL;
840 c->ubCoeff = roundToInt16(cbu * (1 << 13)) * 0x0001000100010001ULL;
841 c->vgCoeff = roundToInt16(cgv * (1 << 13)) * 0x0001000100010001ULL;
842 c->ugCoeff = roundToInt16(cgu * (1 << 13)) * 0x0001000100010001ULL;
843 c->yOffset = roundToInt16(oy * (1 << 3)) * 0x0001000100010001ULL;
845 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy * (1 << 13));
846 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy * (1 << 9));
847 c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv * (1 << 13));
848 c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv * (1 << 13));
849 c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu * (1 << 13));
850 c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu * (1 << 13));
852 //scale coefficients by cy
853 crv = ((crv * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
854 cbu = ((cbu * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
855 cgu = ((cgu * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
856 cgv = ((cgv * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
858 av_freep(&c->yuvTable);
860 #define ALLOC_YUV_TABLE(x) \
861 c->yuvTable = av_malloc(x); \
863 return AVERROR(ENOMEM);
866 ALLOC_YUV_TABLE(table_plane_size);
867 y_table = c->yuvTable;
868 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
869 for (i = 0; i < table_plane_size - 110; i++) {
870 y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
873 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
874 fill_gv_table(c->table_gV, 1, cgv);
878 rbase = isRgb ? 3 : 0;
880 bbase = isRgb ? 0 : 3;
881 ALLOC_YUV_TABLE(table_plane_size * 3);
882 y_table = c->yuvTable;
883 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
884 for (i = 0; i < table_plane_size - 110; i++) {
885 int yval = av_clip_uint8((yb + 0x8000) >> 16);
886 y_table[i + 110] = (yval >> 7) << rbase;
887 y_table[i + 37 + table_plane_size] = ((yval + 43) / 85) << gbase;
888 y_table[i + 110 + 2*table_plane_size] = (yval >> 7) << bbase;
891 fill_table(c->table_rV, 1, crv, y_table + yoffs);
892 fill_table(c->table_gU, 1, cgu, y_table + yoffs + table_plane_size);
893 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2*table_plane_size);
894 fill_gv_table(c->table_gV, 1, cgv);
897 rbase = isRgb ? 5 : 0;
898 gbase = isRgb ? 2 : 3;
899 bbase = isRgb ? 0 : 6;
900 ALLOC_YUV_TABLE(table_plane_size * 3);
901 y_table = c->yuvTable;
902 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
903 for (i = 0; i < table_plane_size - 38; i++) {
904 int yval = av_clip_uint8((yb + 0x8000) >> 16);
905 y_table[i + 16] = ((yval + 18) / 36) << rbase;
906 y_table[i + 16 + table_plane_size] = ((yval + 18) / 36) << gbase;
907 y_table[i + 37 + 2*table_plane_size] = ((yval + 43) / 85) << bbase;
910 fill_table(c->table_rV, 1, crv, y_table + yoffs);
911 fill_table(c->table_gU, 1, cgu, y_table + yoffs + table_plane_size);
912 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2*table_plane_size);
913 fill_gv_table(c->table_gV, 1, cgv);
916 rbase = isRgb ? 8 : 0;
918 bbase = isRgb ? 0 : 8;
919 ALLOC_YUV_TABLE(table_plane_size * 3 * 2);
920 y_table16 = c->yuvTable;
921 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
922 for (i = 0; i < table_plane_size; i++) {
923 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
924 y_table16[i] = (yval >> 4) << rbase;
925 y_table16[i + table_plane_size] = (yval >> 4) << gbase;
926 y_table16[i + 2*table_plane_size] = (yval >> 4) << bbase;
930 for (i = 0; i < table_plane_size * 3; i++)
931 y_table16[i] = av_bswap16(y_table16[i]);
932 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
933 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + table_plane_size);
934 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2*table_plane_size);
935 fill_gv_table(c->table_gV, 2, cgv);
939 rbase = isRgb ? bpp - 5 : 0;
941 bbase = isRgb ? 0 : (bpp - 5);
942 ALLOC_YUV_TABLE(table_plane_size * 3 * 2);
943 y_table16 = c->yuvTable;
944 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
945 for (i = 0; i < table_plane_size; i++) {
946 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
947 y_table16[i] = (yval >> 3) << rbase;
948 y_table16[i + table_plane_size] = (yval >> (18 - bpp)) << gbase;
949 y_table16[i + 2*table_plane_size] = (yval >> 3) << bbase;
953 for (i = 0; i < table_plane_size * 3; i++)
954 y_table16[i] = av_bswap16(y_table16[i]);
955 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
956 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + table_plane_size);
957 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2*table_plane_size);
958 fill_gv_table(c->table_gV, 2, cgv);
962 ALLOC_YUV_TABLE(table_plane_size);
963 y_table = c->yuvTable;
964 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
965 for (i = 0; i < table_plane_size; i++) {
966 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
969 fill_table(c->table_rV, 1, crv, y_table + yoffs);
970 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
971 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
972 fill_gv_table(c->table_gV, 1, cgv);
975 rbase = isRgb ? 20 : 0;
977 bbase = isRgb ? 0 : 20;
978 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
981 ALLOC_YUV_TABLE(table_plane_size * 3 * 4);
982 y_table32 = c->yuvTable;
983 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
984 for (i = 0; i < table_plane_size; i++) {
985 unsigned yval = av_clip_uintp2((yb + 0x8000) >> 14, 10);
986 y_table32[i]= (yval << rbase) + (needAlpha ? 0 : (255u << abase));
987 y_table32[i + table_plane_size] = yval << gbase;
988 y_table32[i + 2 * table_plane_size] = yval << bbase;
992 for (i = 0; i < table_plane_size * 3; i++)
993 y_table32[i] = av_bswap32(y_table32[i]);
995 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
996 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + table_plane_size);
997 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2 * table_plane_size);
998 fill_gv_table(c->table_gV, 4, cgv);
1002 base = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
1003 c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
1004 rbase = base + (isRgb ? 16 : 0);
1006 bbase = base + (isRgb ? 0 : 16);
1007 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
1009 abase = (base + 24) & 31;
1010 ALLOC_YUV_TABLE(table_plane_size * 3 * 4);
1011 y_table32 = c->yuvTable;
1012 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
1013 for (i = 0; i < table_plane_size; i++) {
1014 unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
1015 y_table32[i] = (yval << rbase) +
1016 (needAlpha ? 0 : (255u << abase));
1017 y_table32[i + table_plane_size] = yval << gbase;
1018 y_table32[i + 2*table_plane_size] = yval << bbase;
1021 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
1022 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + table_plane_size);
1023 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2*table_plane_size);
1024 fill_gv_table(c->table_gV, 4, cgv);
1027 if(!isPlanar(c->dstFormat) || bpp <= 24)
1028 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);
1029 return AVERROR(EINVAL);