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 /* Color space conversion coefficients for YCbCr -> RGB mapping.
40 * Entries are {crv, cbu, cgu, cgv}
42 * crv = (255 / 224) * 65536 * (1 - cr) / 0.5
43 * cbu = (255 / 224) * 65536 * (1 - cb) / 0.5
44 * cgu = (255 / 224) * 65536 * (cb / cg) * (1 - cb) / 0.5
45 * cgv = (255 / 224) * 65536 * (cr / cg) * (1 - cr) / 0.5
47 * where Y = cr * R + cg * G + cb * B and cr + cg + cb = 1.
49 const int32_t ff_yuv2rgb_coeffs[11][4] = {
50 { 117489, 138438, 13975, 34925 }, /* no sequence_display_extension */
51 { 117489, 138438, 13975, 34925 }, /* ITU-R Rec. 709 (1990) */
52 { 104597, 132201, 25675, 53279 }, /* unspecified */
53 { 104597, 132201, 25675, 53279 }, /* reserved */
54 { 104448, 132798, 24759, 53109 }, /* FCC */
55 { 104597, 132201, 25675, 53279 }, /* ITU-R Rec. 624-4 System B, G */
56 { 104597, 132201, 25675, 53279 }, /* SMPTE 170M */
57 { 117579, 136230, 16907, 35559 }, /* SMPTE 240M (1987) */
59 { 110013, 140363, 12277, 42626 }, /* Bt-2020-NCL */
60 { 110013, 140363, 12277, 42626 }, /* Bt-2020-CL */
63 const int *sws_getCoefficients(int colorspace)
65 if (colorspace > 10 || colorspace < 0 || colorspace == 8)
66 colorspace = SWS_CS_DEFAULT;
67 return ff_yuv2rgb_coeffs[colorspace];
70 #define LOADCHROMA(i) \
73 r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
74 g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
75 b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
77 #define PUTRGB(dst, src, i) \
79 dst[2 * i] = r[Y] + g[Y] + b[Y]; \
81 dst[2 * i + 1] = r[Y] + g[Y] + b[Y];
83 #define PUTRGB24(dst, src, i) \
85 dst[6 * i + 0] = r[Y]; \
86 dst[6 * i + 1] = g[Y]; \
87 dst[6 * i + 2] = b[Y]; \
89 dst[6 * i + 3] = r[Y]; \
90 dst[6 * i + 4] = g[Y]; \
91 dst[6 * i + 5] = b[Y];
93 #define PUTBGR24(dst, src, i) \
95 dst[6 * i + 0] = b[Y]; \
96 dst[6 * i + 1] = g[Y]; \
97 dst[6 * i + 2] = r[Y]; \
99 dst[6 * i + 3] = b[Y]; \
100 dst[6 * i + 4] = g[Y]; \
101 dst[6 * i + 5] = r[Y];
103 #define PUTRGBA(dst, ysrc, asrc, i, s) \
105 dst[2 * i] = r[Y] + g[Y] + b[Y] + (asrc[2 * i] << s); \
106 Y = ysrc[2 * i + 1]; \
107 dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + (asrc[2 * i + 1] << s);
109 #define PUTRGB48(dst, src, i) \
111 dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
112 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
113 dst[12 * i + 4] = dst[12 * i + 5] = b[Y]; \
114 Y = src[ 2 * i + 1]; \
115 dst[12 * i + 6] = dst[12 * i + 7] = r[Y]; \
116 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
117 dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
119 #define PUTBGR48(dst, src, i) \
121 dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
122 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
123 dst[12 * i + 4] = dst[12 * i + 5] = r[Y]; \
124 Y = src[2 * i + 1]; \
125 dst[12 * i + 6] = dst[12 * i + 7] = b[Y]; \
126 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
127 dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
129 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
130 static int func_name(SwsContext *c, const uint8_t *src[], \
131 int srcStride[], int srcSliceY, int srcSliceH, \
132 uint8_t *dst[], int dstStride[]) \
136 if (!alpha && c->srcFormat == AV_PIX_FMT_YUV422P) { \
140 for (y = 0; y < srcSliceH; y += 2) { \
142 (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]); \
144 (dst_type *)(dst[0] + (y + srcSliceY + 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 *pu = src[1] + (y >> 1) * srcStride[1]; \
149 const uint8_t *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);
270 YUV2RGBFUNC(yuva2rgba_c, uint32_t, 1)
272 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
273 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
276 PUTRGBA(dst_2, py_2, pa_2, 1, 24);
277 PUTRGBA(dst_1, py_1, pa_1, 1, 24);
280 PUTRGBA(dst_1, py_1, pa_1, 2, 24);
281 PUTRGBA(dst_2, py_2, pa_2, 2, 24);
284 PUTRGBA(dst_2, py_2, pa_2, 3, 24);
285 PUTRGBA(dst_1, py_1, pa_1, 3, 24);
290 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
291 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
294 PUTRGBA(dst_2, py_2, pa_2, 1, 24);
295 PUTRGBA(dst_1, py_1, pa_1, 1, 24);
300 PUTRGBA(dst_1, py_1, pa_1, 0, 24);
301 PUTRGBA(dst_2, py_2, pa_2, 0, 24);
304 YUV2RGBFUNC(yuva2argb_c, uint32_t, 1)
306 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
307 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
310 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
311 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
314 PUTRGBA(dst_1, py_1, pa_1, 2, 0);
315 PUTRGBA(dst_2, py_2, pa_2, 2, 0);
318 PUTRGBA(dst_2, py_2, pa_2, 3, 0);
319 PUTRGBA(dst_1, py_1, pa_1, 3, 0);
324 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
325 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
328 PUTRGBA(dst_2, py_2, pa_2, 1, 0);
329 PUTRGBA(dst_1, py_1, pa_1, 1, 0);
334 PUTRGBA(dst_1, py_1, pa_1, 0, 0);
335 PUTRGBA(dst_2, py_2, pa_2, 0, 0);
338 YUV2RGBFUNC(yuv2rgb_c_24_rgb, uint8_t, 0)
340 PUTRGB24(dst_1, py_1, 0);
341 PUTRGB24(dst_2, py_2, 0);
344 PUTRGB24(dst_2, py_2, 1);
345 PUTRGB24(dst_1, py_1, 1);
348 PUTRGB24(dst_1, py_1, 2);
349 PUTRGB24(dst_2, py_2, 2);
352 PUTRGB24(dst_2, py_2, 3);
353 PUTRGB24(dst_1, py_1, 3);
354 ENDYUV2RGBLINE(24, 0)
356 PUTRGB24(dst_1, py_1, 0);
357 PUTRGB24(dst_2, py_2, 0);
360 PUTRGB24(dst_2, py_2, 1);
361 PUTRGB24(dst_1, py_1, 1);
362 ENDYUV2RGBLINE(24, 1)
364 PUTRGB24(dst_1, py_1, 0);
365 PUTRGB24(dst_2, py_2, 0);
368 // only trivial mods from yuv2rgb_c_24_rgb
369 YUV2RGBFUNC(yuv2rgb_c_24_bgr, uint8_t, 0)
371 PUTBGR24(dst_1, py_1, 0);
372 PUTBGR24(dst_2, py_2, 0);
375 PUTBGR24(dst_2, py_2, 1);
376 PUTBGR24(dst_1, py_1, 1);
379 PUTBGR24(dst_1, py_1, 2);
380 PUTBGR24(dst_2, py_2, 2);
383 PUTBGR24(dst_2, py_2, 3);
384 PUTBGR24(dst_1, py_1, 3);
385 ENDYUV2RGBLINE(24, 0)
387 PUTBGR24(dst_1, py_1, 0);
388 PUTBGR24(dst_2, py_2, 0);
391 PUTBGR24(dst_2, py_2, 1);
392 PUTBGR24(dst_1, py_1, 1);
393 ENDYUV2RGBLINE(24, 1)
395 PUTBGR24(dst_1, py_1, 0);
396 PUTBGR24(dst_2, py_2, 0);
399 YUV2RGBFUNC(yuv2rgb_c_16_ordered_dither, uint16_t, 0)
400 const uint8_t *d16 = ff_dither_2x2_8[y & 1];
401 const uint8_t *e16 = ff_dither_2x2_4[y & 1];
402 const uint8_t *f16 = ff_dither_2x2_8[(y & 1)^1];
404 #define PUTRGB16(dst, src, i, o) \
406 dst[2 * i] = r[Y + d16[0 + o]] + \
407 g[Y + e16[0 + o]] + \
409 Y = src[2 * i + 1]; \
410 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
411 g[Y + e16[1 + o]] + \
414 PUTRGB16(dst_1, py_1, 0, 0);
415 PUTRGB16(dst_2, py_2, 0, 0 + 8);
418 PUTRGB16(dst_2, py_2, 1, 2 + 8);
419 PUTRGB16(dst_1, py_1, 1, 2);
422 PUTRGB16(dst_1, py_1, 2, 4);
423 PUTRGB16(dst_2, py_2, 2, 4 + 8);
426 PUTRGB16(dst_2, py_2, 3, 6 + 8);
427 PUTRGB16(dst_1, py_1, 3, 6);
430 YUV2RGBFUNC(yuv2rgb_c_15_ordered_dither, uint16_t, 0)
431 const uint8_t *d16 = ff_dither_2x2_8[y & 1];
432 const uint8_t *e16 = ff_dither_2x2_8[(y & 1)^1];
434 #define PUTRGB15(dst, src, i, o) \
436 dst[2 * i] = r[Y + d16[0 + o]] + \
437 g[Y + d16[1 + o]] + \
439 Y = src[2 * i + 1]; \
440 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
441 g[Y + d16[0 + o]] + \
444 PUTRGB15(dst_1, py_1, 0, 0);
445 PUTRGB15(dst_2, py_2, 0, 0 + 8);
448 PUTRGB15(dst_2, py_2, 1, 2 + 8);
449 PUTRGB15(dst_1, py_1, 1, 2);
452 PUTRGB15(dst_1, py_1, 2, 4);
453 PUTRGB15(dst_2, py_2, 2, 4 + 8);
456 PUTRGB15(dst_2, py_2, 3, 6 + 8);
457 PUTRGB15(dst_1, py_1, 3, 6);
460 // r, g, b, dst_1, dst_2
461 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither, uint16_t, 0)
462 const uint8_t *d16 = ff_dither_4x4_16[y & 3];
464 #define PUTRGB12(dst, src, i, o) \
466 dst[2 * i] = r[Y + d16[0 + o]] + \
467 g[Y + d16[0 + o]] + \
469 Y = src[2 * i + 1]; \
470 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
471 g[Y + d16[1 + o]] + \
475 PUTRGB12(dst_1, py_1, 0, 0);
476 PUTRGB12(dst_2, py_2, 0, 0 + 8);
479 PUTRGB12(dst_2, py_2, 1, 2 + 8);
480 PUTRGB12(dst_1, py_1, 1, 2);
483 PUTRGB12(dst_1, py_1, 2, 4);
484 PUTRGB12(dst_2, py_2, 2, 4 + 8);
487 PUTRGB12(dst_2, py_2, 3, 6 + 8);
488 PUTRGB12(dst_1, py_1, 3, 6);
491 // r, g, b, dst_1, dst_2
492 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither, uint8_t, 0)
493 const uint8_t *d32 = ff_dither_8x8_32[y & 7];
494 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
496 #define PUTRGB8(dst, src, i, o) \
498 dst[2 * i] = r[Y + d32[0 + o]] + \
499 g[Y + d32[0 + o]] + \
501 Y = src[2 * i + 1]; \
502 dst[2 * i + 1] = r[Y + d32[1 + o]] + \
503 g[Y + d32[1 + o]] + \
507 PUTRGB8(dst_1, py_1, 0, 0);
508 PUTRGB8(dst_2, py_2, 0, 0 + 8);
511 PUTRGB8(dst_2, py_2, 1, 2 + 8);
512 PUTRGB8(dst_1, py_1, 1, 2);
515 PUTRGB8(dst_1, py_1, 2, 4);
516 PUTRGB8(dst_2, py_2, 2, 4 + 8);
519 PUTRGB8(dst_2, py_2, 3, 6 + 8);
520 PUTRGB8(dst_1, py_1, 3, 6);
523 const uint8_t *d32 = ff_dither_8x8_32[y & 7];
524 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
526 PUTRGB8(dst_1, py_1, 0, 0);
527 PUTRGB8(dst_2, py_2, 0, 0 + 8);
530 PUTRGB8(dst_2, py_2, 1, 2 + 8);
531 PUTRGB8(dst_1, py_1, 1, 2);
534 const uint8_t *d32 = ff_dither_8x8_32[y & 7];
535 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
537 PUTRGB8(dst_1, py_1, 0, 0);
538 PUTRGB8(dst_2, py_2, 0, 0 + 8);
543 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither, uint8_t, 0)
544 const uint8_t * d64 = ff_dither_8x8_73[y & 7];
545 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
548 #define PUTRGB4D(dst, src, i, o) \
550 acc = r[Y + d128[0 + o]] + \
551 g[Y + d64[0 + o]] + \
552 b[Y + d128[0 + o]]; \
553 Y = src[2 * i + 1]; \
554 acc |= (r[Y + d128[1 + o]] + \
555 g[Y + d64[1 + o]] + \
556 b[Y + d128[1 + o]]) << 4; \
560 PUTRGB4D(dst_1, py_1, 0, 0);
561 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
564 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
565 PUTRGB4D(dst_1, py_1, 1, 2);
568 PUTRGB4D(dst_1, py_1, 2, 4);
569 PUTRGB4D(dst_2, py_2, 2, 4 + 8);
572 PUTRGB4D(dst_2, py_2, 3, 6 + 8);
573 PUTRGB4D(dst_1, py_1, 3, 6);
576 const uint8_t * d64 = ff_dither_8x8_73[y & 7];
577 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
580 PUTRGB4D(dst_1, py_1, 0, 0);
581 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
584 PUTRGB4D(dst_2, py_2, 1, 2 + 8);
585 PUTRGB4D(dst_1, py_1, 1, 2);
588 const uint8_t * d64 = ff_dither_8x8_73[y & 7];
589 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
592 PUTRGB4D(dst_1, py_1, 0, 0);
593 PUTRGB4D(dst_2, py_2, 0, 0 + 8);
596 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither, uint8_t, 0)
597 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
598 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
600 #define PUTRGB4DB(dst, src, i, o) \
602 dst[2 * i] = r[Y + d128[0 + o]] + \
603 g[Y + d64[0 + o]] + \
604 b[Y + d128[0 + o]]; \
605 Y = src[2 * i + 1]; \
606 dst[2 * i + 1] = r[Y + d128[1 + o]] + \
607 g[Y + d64[1 + o]] + \
611 PUTRGB4DB(dst_1, py_1, 0, 0);
612 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
615 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
616 PUTRGB4DB(dst_1, py_1, 1, 2);
619 PUTRGB4DB(dst_1, py_1, 2, 4);
620 PUTRGB4DB(dst_2, py_2, 2, 4 + 8);
623 PUTRGB4DB(dst_2, py_2, 3, 6 + 8);
624 PUTRGB4DB(dst_1, py_1, 3, 6);
626 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
627 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
629 PUTRGB4DB(dst_1, py_1, 0, 0);
630 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
633 PUTRGB4DB(dst_2, py_2, 1, 2 + 8);
634 PUTRGB4DB(dst_1, py_1, 1, 2);
636 const uint8_t *d64 = ff_dither_8x8_73[y & 7];
637 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
639 PUTRGB4DB(dst_1, py_1, 0, 0);
640 PUTRGB4DB(dst_2, py_2, 0, 0 + 8);
643 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither, uint8_t, 0)
644 const uint8_t *d128 = ff_dither_8x8_220[y & 7];
645 char out_1 = 0, out_2 = 0;
646 g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
648 #define PUTRGB1(out, src, i, o) \
650 out += out + g[Y + d128[0 + o]]; \
651 Y = src[2 * i + 1]; \
652 out += out + g[Y + d128[1 + o]];
654 PUTRGB1(out_1, py_1, 0, 0);
655 PUTRGB1(out_2, py_2, 0, 0 + 8);
657 PUTRGB1(out_2, py_2, 1, 2 + 8);
658 PUTRGB1(out_1, py_1, 1, 2);
660 PUTRGB1(out_1, py_1, 2, 4);
661 PUTRGB1(out_2, py_2, 2, 4 + 8);
663 PUTRGB1(out_2, py_2, 3, 6 + 8);
664 PUTRGB1(out_1, py_1, 3, 6);
670 SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c)
675 t = ff_yuv2rgb_init_ppc(c);
677 t = ff_yuv2rgb_init_x86(c);
682 av_log(c, AV_LOG_WARNING,
683 "No accelerated colorspace conversion found from %s to %s.\n",
684 av_get_pix_fmt_name(c->srcFormat), av_get_pix_fmt_name(c->dstFormat));
686 switch (c->dstFormat) {
687 case AV_PIX_FMT_BGR48BE:
688 case AV_PIX_FMT_BGR48LE:
689 return yuv2rgb_c_bgr48;
690 case AV_PIX_FMT_RGB48BE:
691 case AV_PIX_FMT_RGB48LE:
693 case AV_PIX_FMT_ARGB:
694 case AV_PIX_FMT_ABGR:
695 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat))
697 case AV_PIX_FMT_RGBA:
698 case AV_PIX_FMT_BGRA:
699 return (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat)) ? yuva2rgba_c : yuv2rgb_c_32;
700 case AV_PIX_FMT_RGB24:
701 return yuv2rgb_c_24_rgb;
702 case AV_PIX_FMT_BGR24:
703 return yuv2rgb_c_24_bgr;
704 case AV_PIX_FMT_RGB565:
705 case AV_PIX_FMT_BGR565:
706 return yuv2rgb_c_16_ordered_dither;
707 case AV_PIX_FMT_RGB555:
708 case AV_PIX_FMT_BGR555:
709 return yuv2rgb_c_15_ordered_dither;
710 case AV_PIX_FMT_RGB444:
711 case AV_PIX_FMT_BGR444:
712 return yuv2rgb_c_12_ordered_dither;
713 case AV_PIX_FMT_RGB8:
714 case AV_PIX_FMT_BGR8:
715 return yuv2rgb_c_8_ordered_dither;
716 case AV_PIX_FMT_RGB4:
717 case AV_PIX_FMT_BGR4:
718 return yuv2rgb_c_4_ordered_dither;
719 case AV_PIX_FMT_RGB4_BYTE:
720 case AV_PIX_FMT_BGR4_BYTE:
721 return yuv2rgb_c_4b_ordered_dither;
722 case AV_PIX_FMT_MONOBLACK:
723 return yuv2rgb_c_1_ordered_dither;
728 static void fill_table(uint8_t* table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize,
729 const int64_t inc, void *y_tab)
732 uint8_t *y_table = y_tab;
734 y_table -= elemsize * (inc >> 9);
736 for (i = 0; i < 256 + 2*YUVRGB_TABLE_HEADROOM; i++) {
737 int64_t cb = av_clip_uint8(i-YUVRGB_TABLE_HEADROOM)*inc;
738 table[i] = y_table + elemsize * (cb >> 16);
742 static void fill_gv_table(int table[256 + 2*YUVRGB_TABLE_HEADROOM], const int elemsize, const int64_t inc)
745 int off = -(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] = elemsize * (off + (cb >> 16));
753 static uint16_t roundToInt16(int64_t f)
755 int r = (f + (1 << 15)) >> 16;
765 av_cold int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
766 int fullRange, int brightness,
767 int contrast, int saturation)
769 const int isRgb = c->dstFormat == AV_PIX_FMT_RGB32 ||
770 c->dstFormat == AV_PIX_FMT_RGB32_1 ||
771 c->dstFormat == AV_PIX_FMT_BGR24 ||
772 c->dstFormat == AV_PIX_FMT_RGB565BE ||
773 c->dstFormat == AV_PIX_FMT_RGB565LE ||
774 c->dstFormat == AV_PIX_FMT_RGB555BE ||
775 c->dstFormat == AV_PIX_FMT_RGB555LE ||
776 c->dstFormat == AV_PIX_FMT_RGB444BE ||
777 c->dstFormat == AV_PIX_FMT_RGB444LE ||
778 c->dstFormat == AV_PIX_FMT_RGB8 ||
779 c->dstFormat == AV_PIX_FMT_RGB4 ||
780 c->dstFormat == AV_PIX_FMT_RGB4_BYTE ||
781 c->dstFormat == AV_PIX_FMT_MONOBLACK;
782 const int isNotNe = c->dstFormat == AV_PIX_FMT_NE(RGB565LE, RGB565BE) ||
783 c->dstFormat == AV_PIX_FMT_NE(RGB555LE, RGB555BE) ||
784 c->dstFormat == AV_PIX_FMT_NE(RGB444LE, RGB444BE) ||
785 c->dstFormat == AV_PIX_FMT_NE(BGR565LE, BGR565BE) ||
786 c->dstFormat == AV_PIX_FMT_NE(BGR555LE, BGR555BE) ||
787 c->dstFormat == AV_PIX_FMT_NE(BGR444LE, BGR444BE);
788 const int bpp = c->dstFormatBpp;
792 int i, base, rbase, gbase, bbase, av_uninit(abase), needAlpha;
793 const int yoffs = (fullRange ? 384 : 326) + YUVRGB_TABLE_LUMA_HEADROOM;
794 const int table_plane_size = 1024 + 2*YUVRGB_TABLE_LUMA_HEADROOM;
796 int64_t crv = inv_table[0];
797 int64_t cbu = inv_table[1];
798 int64_t cgu = -inv_table[2];
799 int64_t cgv = -inv_table[3];
800 int64_t cy = 1 << 16;
805 cy = (cy * 255) / 219;
808 crv = (crv * 224) / 255;
809 cbu = (cbu * 224) / 255;
810 cgu = (cgu * 224) / 255;
811 cgv = (cgv * 224) / 255;
814 cy = (cy * contrast) >> 16;
815 crv = (crv * contrast * saturation) >> 32;
816 cbu = (cbu * contrast * saturation) >> 32;
817 cgu = (cgu * contrast * saturation) >> 32;
818 cgv = (cgv * contrast * saturation) >> 32;
819 oy -= 256 * brightness;
821 c->uOffset = 0x0400040004000400LL;
822 c->vOffset = 0x0400040004000400LL;
823 c->yCoeff = roundToInt16(cy * (1 << 13)) * 0x0001000100010001ULL;
824 c->vrCoeff = roundToInt16(crv * (1 << 13)) * 0x0001000100010001ULL;
825 c->ubCoeff = roundToInt16(cbu * (1 << 13)) * 0x0001000100010001ULL;
826 c->vgCoeff = roundToInt16(cgv * (1 << 13)) * 0x0001000100010001ULL;
827 c->ugCoeff = roundToInt16(cgu * (1 << 13)) * 0x0001000100010001ULL;
828 c->yOffset = roundToInt16(oy * (1 << 3)) * 0x0001000100010001ULL;
830 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy * (1 << 13));
831 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy * (1 << 9));
832 c->yuv2rgb_v2r_coeff = (int16_t)roundToInt16(crv * (1 << 13));
833 c->yuv2rgb_v2g_coeff = (int16_t)roundToInt16(cgv * (1 << 13));
834 c->yuv2rgb_u2g_coeff = (int16_t)roundToInt16(cgu * (1 << 13));
835 c->yuv2rgb_u2b_coeff = (int16_t)roundToInt16(cbu * (1 << 13));
837 //scale coefficients by cy
838 crv = ((crv * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
839 cbu = ((cbu * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
840 cgu = ((cgu * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
841 cgv = ((cgv * (1 << 16)) + 0x8000) / FFMAX(cy, 1);
843 av_freep(&c->yuvTable);
845 #define ALLOC_YUV_TABLE(x) \
846 c->yuvTable = av_malloc(x); \
848 return AVERROR(ENOMEM);
851 ALLOC_YUV_TABLE(table_plane_size);
852 y_table = c->yuvTable;
853 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
854 for (i = 0; i < table_plane_size - 110; i++) {
855 y_table[i + 110] = av_clip_uint8((yb + 0x8000) >> 16) >> 7;
858 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
859 fill_gv_table(c->table_gV, 1, cgv);
863 rbase = isRgb ? 3 : 0;
865 bbase = isRgb ? 0 : 3;
866 ALLOC_YUV_TABLE(table_plane_size * 3);
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 int yval = av_clip_uint8((yb + 0x8000) >> 16);
871 y_table[i + 110] = (yval >> 7) << rbase;
872 y_table[i + 37 + table_plane_size] = ((yval + 43) / 85) << gbase;
873 y_table[i + 110 + 2*table_plane_size] = (yval >> 7) << bbase;
876 fill_table(c->table_rV, 1, crv, y_table + yoffs);
877 fill_table(c->table_gU, 1, cgu, y_table + yoffs + table_plane_size);
878 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2*table_plane_size);
879 fill_gv_table(c->table_gV, 1, cgv);
882 rbase = isRgb ? 5 : 0;
883 gbase = isRgb ? 2 : 3;
884 bbase = isRgb ? 0 : 6;
885 ALLOC_YUV_TABLE(table_plane_size * 3);
886 y_table = c->yuvTable;
887 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
888 for (i = 0; i < table_plane_size - 38; i++) {
889 int yval = av_clip_uint8((yb + 0x8000) >> 16);
890 y_table[i + 16] = ((yval + 18) / 36) << rbase;
891 y_table[i + 16 + table_plane_size] = ((yval + 18) / 36) << gbase;
892 y_table[i + 37 + 2*table_plane_size] = ((yval + 43) / 85) << bbase;
895 fill_table(c->table_rV, 1, crv, y_table + yoffs);
896 fill_table(c->table_gU, 1, cgu, y_table + yoffs + table_plane_size);
897 fill_table(c->table_bU, 1, cbu, y_table + yoffs + 2*table_plane_size);
898 fill_gv_table(c->table_gV, 1, cgv);
901 rbase = isRgb ? 8 : 0;
903 bbase = isRgb ? 0 : 8;
904 ALLOC_YUV_TABLE(table_plane_size * 3 * 2);
905 y_table16 = c->yuvTable;
906 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
907 for (i = 0; i < table_plane_size; i++) {
908 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
909 y_table16[i] = (yval >> 4) << rbase;
910 y_table16[i + table_plane_size] = (yval >> 4) << gbase;
911 y_table16[i + 2*table_plane_size] = (yval >> 4) << bbase;
915 for (i = 0; i < table_plane_size * 3; i++)
916 y_table16[i] = av_bswap16(y_table16[i]);
917 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
918 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + table_plane_size);
919 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2*table_plane_size);
920 fill_gv_table(c->table_gV, 2, cgv);
924 rbase = isRgb ? bpp - 5 : 0;
926 bbase = isRgb ? 0 : (bpp - 5);
927 ALLOC_YUV_TABLE(table_plane_size * 3 * 2);
928 y_table16 = c->yuvTable;
929 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
930 for (i = 0; i < table_plane_size; i++) {
931 uint8_t yval = av_clip_uint8((yb + 0x8000) >> 16);
932 y_table16[i] = (yval >> 3) << rbase;
933 y_table16[i + table_plane_size] = (yval >> (18 - bpp)) << gbase;
934 y_table16[i + 2*table_plane_size] = (yval >> 3) << bbase;
938 for (i = 0; i < table_plane_size * 3; i++)
939 y_table16[i] = av_bswap16(y_table16[i]);
940 fill_table(c->table_rV, 2, crv, y_table16 + yoffs);
941 fill_table(c->table_gU, 2, cgu, y_table16 + yoffs + table_plane_size);
942 fill_table(c->table_bU, 2, cbu, y_table16 + yoffs + 2*table_plane_size);
943 fill_gv_table(c->table_gV, 2, cgv);
947 ALLOC_YUV_TABLE(table_plane_size);
948 y_table = c->yuvTable;
949 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
950 for (i = 0; i < table_plane_size; i++) {
951 y_table[i] = av_clip_uint8((yb + 0x8000) >> 16);
954 fill_table(c->table_rV, 1, crv, y_table + yoffs);
955 fill_table(c->table_gU, 1, cgu, y_table + yoffs);
956 fill_table(c->table_bU, 1, cbu, y_table + yoffs);
957 fill_gv_table(c->table_gV, 1, cgv);
961 base = (c->dstFormat == AV_PIX_FMT_RGB32_1 ||
962 c->dstFormat == AV_PIX_FMT_BGR32_1) ? 8 : 0;
963 rbase = base + (isRgb ? 16 : 0);
965 bbase = base + (isRgb ? 0 : 16);
966 needAlpha = CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat);
968 abase = (base + 24) & 31;
969 ALLOC_YUV_TABLE(table_plane_size * 3 * 4);
970 y_table32 = c->yuvTable;
971 yb = -(384 << 16) - YUVRGB_TABLE_LUMA_HEADROOM*cy - oy;
972 for (i = 0; i < table_plane_size; i++) {
973 unsigned yval = av_clip_uint8((yb + 0x8000) >> 16);
974 y_table32[i] = (yval << rbase) +
975 (needAlpha ? 0 : (255u << abase));
976 y_table32[i + table_plane_size] = yval << gbase;
977 y_table32[i + 2*table_plane_size] = yval << bbase;
980 fill_table(c->table_rV, 4, crv, y_table32 + yoffs);
981 fill_table(c->table_gU, 4, cgu, y_table32 + yoffs + table_plane_size);
982 fill_table(c->table_bU, 4, cbu, y_table32 + yoffs + 2*table_plane_size);
983 fill_gv_table(c->table_gV, 4, cgv);
986 if(!isPlanar(c->dstFormat) || bpp <= 24)
987 av_log(c, AV_LOG_ERROR, "%ibpp not supported by yuv2rgb\n", bpp);