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 const int32_t ff_yuv2rgb_coeffs
[8][4] = {
39 { 117504, 138453, 13954, 34903 }, /* no sequence_display_extension */
40 { 117504, 138453, 13954, 34903 }, /* ITU-R Rec. 709 (1990) */
41 { 104597, 132201, 25675, 53279 }, /* unspecified */
42 { 104597, 132201, 25675, 53279 }, /* reserved */
43 { 104448, 132798, 24759, 53109 }, /* FCC */
44 { 104597, 132201, 25675, 53279 }, /* ITU-R Rec. 624-4 System B, G */
45 { 104597, 132201, 25675, 53279 }, /* SMPTE 170M */
46 { 117579, 136230, 16907, 35559 } /* SMPTE 240M (1987) */
49 const int *sws_getCoefficients(int colorspace
)
51 if (colorspace
> 7 || colorspace
< 0)
52 colorspace
= SWS_CS_DEFAULT
;
53 return ff_yuv2rgb_coeffs
[colorspace
];
56 #define LOADCHROMA(i) \
59 r = (void *)c->table_rV[V+YUVRGB_TABLE_HEADROOM]; \
60 g = (void *)(c->table_gU[U+YUVRGB_TABLE_HEADROOM] + c->table_gV[V+YUVRGB_TABLE_HEADROOM]); \
61 b = (void *)c->table_bU[U+YUVRGB_TABLE_HEADROOM];
63 #define PUTRGB(dst, src, i) \
65 dst[2 * i] = r[Y] + g[Y] + b[Y]; \
67 dst[2 * i + 1] = r[Y] + g[Y] + b[Y];
69 #define PUTRGB24(dst, src, i) \
71 dst[6 * i + 0] = r[Y]; \
72 dst[6 * i + 1] = g[Y]; \
73 dst[6 * i + 2] = b[Y]; \
75 dst[6 * i + 3] = r[Y]; \
76 dst[6 * i + 4] = g[Y]; \
77 dst[6 * i + 5] = b[Y];
79 #define PUTBGR24(dst, src, i) \
81 dst[6 * i + 0] = b[Y]; \
82 dst[6 * i + 1] = g[Y]; \
83 dst[6 * i + 2] = r[Y]; \
85 dst[6 * i + 3] = b[Y]; \
86 dst[6 * i + 4] = g[Y]; \
87 dst[6 * i + 5] = r[Y];
89 #define PUTRGBA(dst, ysrc, asrc, i, s) \
91 dst[2 * i] = r[Y] + g[Y] + b[Y] + (asrc[2 * i] << s); \
92 Y = ysrc[2 * i + 1]; \
93 dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + (asrc[2 * i + 1] << s);
95 #define PUTRGB48(dst, src, i) \
97 dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
98 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
99 dst[12 * i + 4] = dst[12 * i + 5] = b[Y]; \
100 Y = src[ 2 * i + 1]; \
101 dst[12 * i + 6] = dst[12 * i + 7] = r[Y]; \
102 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
103 dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
105 #define PUTBGR48(dst, src, i) \
107 dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
108 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
109 dst[12 * i + 4] = dst[12 * i + 5] = r[Y]; \
110 Y = src[2 * i + 1]; \
111 dst[12 * i + 6] = dst[12 * i + 7] = b[Y]; \
112 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
113 dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
115 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
116 static int func_name(SwsContext *c, const uint8_t *src[], \
117 int srcStride[], int srcSliceY, int srcSliceH, \
118 uint8_t *dst[], int dstStride[]) \
122 if (!alpha && c->srcFormat == AV_PIX_FMT_YUV422P) { \
126 for (y = 0; y < srcSliceH; y += 2) { \
128 (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]); \
130 (dst_type *)(dst[0] + (y + srcSliceY + 1) * dstStride[0]); \
131 dst_type av_unused *r, *g, *b; \
132 const uint8_t *py_1 = src[0] + y * srcStride[0]; \
133 const uint8_t *py_2 = py_1 + srcStride[0]; \
134 const uint8_t *pu = src[1] + (y >> 1) * srcStride[1]; \
135 const uint8_t *pv = src[2] + (y >> 1) * srcStride[2]; \
136 const uint8_t av_unused *pa_1, *pa_2; \
137 unsigned int h_size = c->dstW >> 3; \
139 pa_1 = src[3] + y * srcStride[3]; \
140 pa_2 = pa_1 + srcStride[3]; \
143 int av_unused U, V, Y; \
145 #define ENDYUV2RGBLINE(dst_delta, ss) \
150 dst_1 += dst_delta >> ss; \
151 dst_2 += dst_delta >> ss; \
153 if (c->dstW & (4 >> ss)) { \
154 int av_unused Y, U, V; \
156 #define ENDYUV2RGBFUNC() \
162 #define CLOSEYUV2RGBFUNC(dst_delta) \
163 ENDYUV2RGBLINE(dst_delta, 0) \
166 YUV2RGBFUNC(yuv2rgb_c_48
, uint8_t, 0)
168 PUTRGB48(dst_1
, py_1
, 0);
169 PUTRGB48(dst_2
, py_2
, 0);
172 PUTRGB48(dst_2
, py_2
, 1);
173 PUTRGB48(dst_1
, py_1
, 1);
176 PUTRGB48(dst_1
, py_1
, 2);
177 PUTRGB48(dst_2
, py_2
, 2);
180 PUTRGB48(dst_2
, py_2
, 3);
181 PUTRGB48(dst_1
, py_1
, 3);
182 ENDYUV2RGBLINE(48, 0)
184 PUTRGB48(dst_1
, py_1
, 0);
185 PUTRGB48(dst_2
, py_2
, 0);
188 PUTRGB48(dst_2
, py_2
, 1);
189 PUTRGB48(dst_1
, py_1
, 1);
190 ENDYUV2RGBLINE(48, 1)
192 PUTRGB48(dst_1
, py_1
, 0);
193 PUTRGB48(dst_2
, py_2
, 0);
196 YUV2RGBFUNC(yuv2rgb_c_bgr48
, uint8_t, 0)
198 PUTBGR48(dst_1
, py_1
, 0);
199 PUTBGR48(dst_2
, py_2
, 0);
202 PUTBGR48(dst_2
, py_2
, 1);
203 PUTBGR48(dst_1
, py_1
, 1);
206 PUTBGR48(dst_1
, py_1
, 2);
207 PUTBGR48(dst_2
, py_2
, 2);
210 PUTBGR48(dst_2
, py_2
, 3);
211 PUTBGR48(dst_1
, py_1
, 3);
212 ENDYUV2RGBLINE(48, 0)
214 PUTBGR48(dst_1
, py_1
, 0);
215 PUTBGR48(dst_2
, py_2
, 0);
218 PUTBGR48(dst_2
, py_2
, 1);
219 PUTBGR48(dst_1
, py_1
, 1);
220 ENDYUV2RGBLINE(48, 1)
222 PUTBGR48(dst_1
, py_1
, 0);
223 PUTBGR48(dst_2
, py_2
, 0);
226 YUV2RGBFUNC(yuv2rgb_c_32
, uint32_t, 0)
228 PUTRGB(dst_1
, py_1
, 0);
229 PUTRGB(dst_2
, py_2
, 0);
232 PUTRGB(dst_2
, py_2
, 1);
233 PUTRGB(dst_1
, py_1
, 1);
236 PUTRGB(dst_1
, py_1
, 2);
237 PUTRGB(dst_2
, py_2
, 2);
240 PUTRGB(dst_2
, py_2
, 3);
241 PUTRGB(dst_1
, py_1
, 3);
244 PUTRGB(dst_1
, py_1
, 0);
245 PUTRGB(dst_2
, py_2
, 0);
248 PUTRGB(dst_2
, py_2
, 1);
249 PUTRGB(dst_1
, py_1
, 1);
252 PUTRGB(dst_1
, py_1
, 0);
253 PUTRGB(dst_2
, py_2
, 0);
256 YUV2RGBFUNC(yuva2rgba_c
, uint32_t, 1)
258 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 24);
259 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 24);
262 PUTRGBA(dst_2
, py_2
, pa_2
, 1, 24);
263 PUTRGBA(dst_1
, py_1
, pa_1
, 1, 24);
266 PUTRGBA(dst_1
, py_1
, pa_1
, 2, 24);
267 PUTRGBA(dst_2
, py_2
, pa_2
, 2, 24);
270 PUTRGBA(dst_2
, py_2
, pa_2
, 3, 24);
271 PUTRGBA(dst_1
, py_1
, pa_1
, 3, 24);
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);
286 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 24);
287 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 24);
290 YUV2RGBFUNC(yuva2argb_c
, uint32_t, 1)
292 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 0);
293 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 0);
296 PUTRGBA(dst_2
, py_2
, pa_2
, 1, 0);
297 PUTRGBA(dst_1
, py_1
, pa_1
, 1, 0);
300 PUTRGBA(dst_1
, py_1
, pa_1
, 2, 0);
301 PUTRGBA(dst_2
, py_2
, pa_2
, 2, 0);
304 PUTRGBA(dst_2
, py_2
, pa_2
, 3, 0);
305 PUTRGBA(dst_1
, py_1
, pa_1
, 3, 0);
310 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 0);
311 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 0);
314 PUTRGBA(dst_2
, py_2
, pa_2
, 1, 0);
315 PUTRGBA(dst_1
, py_1
, pa_1
, 1, 0);
320 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 0);
321 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 0);
324 YUV2RGBFUNC(yuv2rgb_c_24_rgb
, uint8_t, 0)
326 PUTRGB24(dst_1
, py_1
, 0);
327 PUTRGB24(dst_2
, py_2
, 0);
330 PUTRGB24(dst_2
, py_2
, 1);
331 PUTRGB24(dst_1
, py_1
, 1);
334 PUTRGB24(dst_1
, py_1
, 2);
335 PUTRGB24(dst_2
, py_2
, 2);
338 PUTRGB24(dst_2
, py_2
, 3);
339 PUTRGB24(dst_1
, py_1
, 3);
340 ENDYUV2RGBLINE(24, 0)
342 PUTRGB24(dst_1
, py_1
, 0);
343 PUTRGB24(dst_2
, py_2
, 0);
346 PUTRGB24(dst_2
, py_2
, 1);
347 PUTRGB24(dst_1
, py_1
, 1);
348 ENDYUV2RGBLINE(24, 1)
350 PUTRGB24(dst_1
, py_1
, 0);
351 PUTRGB24(dst_2
, py_2
, 0);
354 // only trivial mods from yuv2rgb_c_24_rgb
355 YUV2RGBFUNC(yuv2rgb_c_24_bgr
, uint8_t, 0)
357 PUTBGR24(dst_1
, py_1
, 0);
358 PUTBGR24(dst_2
, py_2
, 0);
361 PUTBGR24(dst_2
, py_2
, 1);
362 PUTBGR24(dst_1
, py_1
, 1);
365 PUTBGR24(dst_1
, py_1
, 2);
366 PUTBGR24(dst_2
, py_2
, 2);
369 PUTBGR24(dst_2
, py_2
, 3);
370 PUTBGR24(dst_1
, py_1
, 3);
371 ENDYUV2RGBLINE(24, 0)
373 PUTBGR24(dst_1
, py_1
, 0);
374 PUTBGR24(dst_2
, py_2
, 0);
377 PUTBGR24(dst_2
, py_2
, 1);
378 PUTBGR24(dst_1
, py_1
, 1);
379 ENDYUV2RGBLINE(24, 1)
381 PUTBGR24(dst_1
, py_1
, 0);
382 PUTBGR24(dst_2
, py_2
, 0);
385 YUV2RGBFUNC(yuv2rgb_c_16_ordered_dither
, uint16_t, 0)
386 const uint8_t *d16
= ff_dither_2x2_8
[y
& 1];
387 const uint8_t *e16
= ff_dither_2x2_4
[y
& 1];
388 const uint8_t *f16
= ff_dither_2x2_8
[(y
& 1)^1];
390 #define PUTRGB16(dst, src, i, o) \
392 dst[2 * i] = r[Y + d16[0 + o]] + \
393 g[Y + e16[0 + o]] + \
395 Y = src[2 * i + 1]; \
396 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
397 g[Y + e16[1 + o]] + \
400 PUTRGB16(dst_1
, py_1
, 0, 0);
401 PUTRGB16(dst_2
, py_2
, 0, 0 + 8);
404 PUTRGB16(dst_2
, py_2
, 1, 2 + 8);
405 PUTRGB16(dst_1
, py_1
, 1, 2);
408 PUTRGB16(dst_1
, py_1
, 2, 4);
409 PUTRGB16(dst_2
, py_2
, 2, 4 + 8);
412 PUTRGB16(dst_2
, py_2
, 3, 6 + 8);
413 PUTRGB16(dst_1
, py_1
, 3, 6);
416 YUV2RGBFUNC(yuv2rgb_c_15_ordered_dither
, uint16_t, 0)
417 const uint8_t *d16
= ff_dither_2x2_8
[y
& 1];
418 const uint8_t *e16
= ff_dither_2x2_8
[(y
& 1)^1];
420 #define PUTRGB15(dst, src, i, o) \
422 dst[2 * i] = r[Y + d16[0 + o]] + \
423 g[Y + d16[1 + o]] + \
425 Y = src[2 * i + 1]; \
426 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
427 g[Y + d16[0 + o]] + \
430 PUTRGB15(dst_1
, py_1
, 0, 0);
431 PUTRGB15(dst_2
, py_2
, 0, 0 + 8);
434 PUTRGB15(dst_2
, py_2
, 1, 2 + 8);
435 PUTRGB15(dst_1
, py_1
, 1, 2);
438 PUTRGB15(dst_1
, py_1
, 2, 4);
439 PUTRGB15(dst_2
, py_2
, 2, 4 + 8);
442 PUTRGB15(dst_2
, py_2
, 3, 6 + 8);
443 PUTRGB15(dst_1
, py_1
, 3, 6);
446 // r, g, b, dst_1, dst_2
447 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither
, uint16_t, 0)
448 const uint8_t *d16
= ff_dither_4x4_16
[y
& 3];
450 #define PUTRGB12(dst, src, i, o) \
452 dst[2 * i] = r[Y + d16[0 + o]] + \
453 g[Y + d16[0 + o]] + \
455 Y = src[2 * i + 1]; \
456 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
457 g[Y + d16[1 + o]] + \
461 PUTRGB12(dst_1
, py_1
, 0, 0);
462 PUTRGB12(dst_2
, py_2
, 0, 0 + 8);
465 PUTRGB12(dst_2
, py_2
, 1, 2 + 8);
466 PUTRGB12(dst_1
, py_1
, 1, 2);
469 PUTRGB12(dst_1
, py_1
, 2, 4);
470 PUTRGB12(dst_2
, py_2
, 2, 4 + 8);
473 PUTRGB12(dst_2
, py_2
, 3, 6 + 8);
474 PUTRGB12(dst_1
, py_1
, 3, 6);
477 // r, g, b, dst_1, dst_2
478 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither
, uint8_t, 0)
479 const uint8_t *d32
= ff_dither_8x8_32
[y
& 7];
480 const uint8_t *d64
= ff_dither_8x8_73
[y
& 7];
482 #define PUTRGB8(dst, src, i, o) \
484 dst[2 * i] = r[Y + d32[0 + o]] + \
485 g[Y + d32[0 + o]] + \
487 Y = src[2 * i + 1]; \
488 dst[2 * i + 1] = r[Y + d32[1 + o]] + \
489 g[Y + d32[1 + o]] + \
493 PUTRGB8(dst_1
, py_1
, 0, 0);
494 PUTRGB8(dst_2
, py_2
, 0, 0 + 8);
497 PUTRGB8(dst_2
, py_2
, 1, 2 + 8);
498 PUTRGB8(dst_1
, py_1
, 1, 2);
501 PUTRGB8(dst_1
, py_1
, 2, 4);
502 PUTRGB8(dst_2
, py_2
, 2, 4 + 8);
505 PUTRGB8(dst_2
, py_2
, 3, 6 + 8);
506 PUTRGB8(dst_1
, py_1
, 3, 6);
509 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither
, uint8_t, 0)
510 const uint8_t * d64
= ff_dither_8x8_73
[y
& 7];
511 const uint8_t *d128
= ff_dither_8x8_220
[y
& 7];
514 #define PUTRGB4D(dst, src, i, o) \
516 acc = r[Y + d128[0 + o]] + \
517 g[Y + d64[0 + o]] + \
518 b[Y + d128[0 + o]]; \
519 Y = src[2 * i + 1]; \
520 acc |= (r[Y + d128[1 + o]] + \
521 g[Y + d64[1 + o]] + \
522 b[Y + d128[1 + o]]) << 4; \
526 PUTRGB4D(dst_1
, py_1
, 0, 0);
527 PUTRGB4D(dst_2
, py_2
, 0, 0 + 8);
530 PUTRGB4D(dst_2
, py_2
, 1, 2 + 8);
531 PUTRGB4D(dst_1
, py_1
, 1, 2);
534 PUTRGB4D(dst_1
, py_1
, 2, 4);
535 PUTRGB4D(dst_2
, py_2
, 2, 4 + 8);
538 PUTRGB4D(dst_2
, py_2
, 3, 6 + 8);
539 PUTRGB4D(dst_1
, py_1
, 3, 6);
542 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither
, uint8_t, 0)
543 const uint8_t *d64
= ff_dither_8x8_73
[y
& 7];
544 const uint8_t *d128
= ff_dither_8x8_220
[y
& 7];
546 #define PUTRGB4DB(dst, src, i, o) \
548 dst[2 * i] = r[Y + d128[0 + o]] + \
549 g[Y + d64[0 + o]] + \
550 b[Y + d128[0 + o]]; \
551 Y = src[2 * i + 1]; \
552 dst[2 * i + 1] = r[Y + d128[1 + o]] + \
553 g[Y + d64[1 + o]] + \
557 PUTRGB4DB(dst_1
, py_1
, 0, 0);
558 PUTRGB4DB(dst_2
, py_2
, 0, 0 + 8);
561 PUTRGB4DB(dst_2
, py_2
, 1, 2 + 8);
562 PUTRGB4DB(dst_1
, py_1
, 1, 2);
565 PUTRGB4DB(dst_1
, py_1
, 2, 4);
566 PUTRGB4DB(dst_2
, py_2
, 2, 4 + 8);
569 PUTRGB4DB(dst_2
, py_2
, 3, 6 + 8);
570 PUTRGB4DB(dst_1
, py_1
, 3, 6);
573 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither
, uint8_t, 0)
574 const uint8_t *d128
= ff_dither_8x8_220
[y
& 7];
575 char out_1
= 0, out_2
= 0;
576 g
= c
->table_gU
[128 + YUVRGB_TABLE_HEADROOM
] + c
->table_gV
[128 + YUVRGB_TABLE_HEADROOM
];
578 #define PUTRGB1(out, src, i, o) \
580 out += out + g[Y + d128[0 + o]]; \
581 Y = src[2 * i + 1]; \
582 out += out + g[Y + d128[1 + o]];
584 PUTRGB1(out_1
, py_1
, 0, 0);
585 PUTRGB1(out_2
, py_2
, 0, 0 + 8);
587 PUTRGB1(out_2
, py_2
, 1, 2 + 8);
588 PUTRGB1(out_1
, py_1
, 1, 2);
590 PUTRGB1(out_1
, py_1
, 2, 4);
591 PUTRGB1(out_2
, py_2
, 2, 4 + 8);
593 PUTRGB1(out_2
, py_2
, 3, 6 + 8);
594 PUTRGB1(out_1
, py_1
, 3, 6);
600 SwsFunc
ff_yuv2rgb_get_func_ptr(SwsContext
*c
)
605 t
= ff_yuv2rgb_init_ppc(c
);
607 t
= ff_yuv2rgb_init_x86(c
);
612 av_log(c
, AV_LOG_WARNING
,
613 "No accelerated colorspace conversion found from %s to %s.\n",
614 av_get_pix_fmt_name(c
->srcFormat
), av_get_pix_fmt_name(c
->dstFormat
));
616 switch (c
->dstFormat
) {
617 case AV_PIX_FMT_BGR48BE
:
618 case AV_PIX_FMT_BGR48LE
:
619 return yuv2rgb_c_bgr48
;
620 case AV_PIX_FMT_RGB48BE
:
621 case AV_PIX_FMT_RGB48LE
:
623 case AV_PIX_FMT_ARGB
:
624 case AV_PIX_FMT_ABGR
:
625 if (CONFIG_SWSCALE_ALPHA
&& isALPHA(c
->srcFormat
))
627 case AV_PIX_FMT_RGBA
:
628 case AV_PIX_FMT_BGRA
:
629 return (CONFIG_SWSCALE_ALPHA
&& isALPHA(c
->srcFormat
)) ? yuva2rgba_c
: yuv2rgb_c_32
;
630 case AV_PIX_FMT_RGB24
:
631 return yuv2rgb_c_24_rgb
;
632 case AV_PIX_FMT_BGR24
:
633 return yuv2rgb_c_24_bgr
;
634 case AV_PIX_FMT_RGB565
:
635 case AV_PIX_FMT_BGR565
:
636 return yuv2rgb_c_16_ordered_dither
;
637 case AV_PIX_FMT_RGB555
:
638 case AV_PIX_FMT_BGR555
:
639 return yuv2rgb_c_15_ordered_dither
;
640 case AV_PIX_FMT_RGB444
:
641 case AV_PIX_FMT_BGR444
:
642 return yuv2rgb_c_12_ordered_dither
;
643 case AV_PIX_FMT_RGB8
:
644 case AV_PIX_FMT_BGR8
:
645 return yuv2rgb_c_8_ordered_dither
;
646 case AV_PIX_FMT_RGB4
:
647 case AV_PIX_FMT_BGR4
:
648 return yuv2rgb_c_4_ordered_dither
;
649 case AV_PIX_FMT_RGB4_BYTE
:
650 case AV_PIX_FMT_BGR4_BYTE
:
651 return yuv2rgb_c_4b_ordered_dither
;
652 case AV_PIX_FMT_MONOBLACK
:
653 return yuv2rgb_c_1_ordered_dither
;
658 static void fill_table(uint8_t* table
[256 + 2*YUVRGB_TABLE_HEADROOM
], const int elemsize
,
659 const int64_t inc
, void *y_tab
)
662 uint8_t *y_table
= y_tab
;
664 y_table
-= elemsize
* (inc
>> 9);
666 for (i
= 0; i
< 256 + 2*YUVRGB_TABLE_HEADROOM
; i
++) {
667 int64_t cb
= av_clip(i
-YUVRGB_TABLE_HEADROOM
, 0, 255)*inc
;
668 table
[i
] = y_table
+ elemsize
* (cb
>> 16);
672 static void fill_gv_table(int table
[256 + 2*YUVRGB_TABLE_HEADROOM
], const int elemsize
, const int64_t inc
)
675 int off
= -(inc
>> 9);
677 for (i
= 0; i
< 256 + 2*YUVRGB_TABLE_HEADROOM
; i
++) {
678 int64_t cb
= av_clip(i
-YUVRGB_TABLE_HEADROOM
, 0, 255)*inc
;
679 table
[i
] = elemsize
* (off
+ (cb
>> 16));
683 static uint16_t roundToInt16(int64_t f
)
685 int r
= (f
+ (1 << 15)) >> 16;
695 av_cold
int ff_yuv2rgb_c_init_tables(SwsContext
*c
, const int inv_table
[4],
696 int fullRange
, int brightness
,
697 int contrast
, int saturation
)
699 const int isRgb
= c
->dstFormat
== AV_PIX_FMT_RGB32
||
700 c
->dstFormat
== AV_PIX_FMT_RGB32_1
||
701 c
->dstFormat
== AV_PIX_FMT_BGR24
||
702 c
->dstFormat
== AV_PIX_FMT_RGB565BE
||
703 c
->dstFormat
== AV_PIX_FMT_RGB565LE
||
704 c
->dstFormat
== AV_PIX_FMT_RGB555BE
||
705 c
->dstFormat
== AV_PIX_FMT_RGB555LE
||
706 c
->dstFormat
== AV_PIX_FMT_RGB444BE
||
707 c
->dstFormat
== AV_PIX_FMT_RGB444LE
||
708 c
->dstFormat
== AV_PIX_FMT_RGB8
||
709 c
->dstFormat
== AV_PIX_FMT_RGB4
||
710 c
->dstFormat
== AV_PIX_FMT_RGB4_BYTE
||
711 c
->dstFormat
== AV_PIX_FMT_MONOBLACK
;
712 const int isNotNe
= c
->dstFormat
== AV_PIX_FMT_NE(RGB565LE
, RGB565BE
) ||
713 c
->dstFormat
== AV_PIX_FMT_NE(RGB555LE
, RGB555BE
) ||
714 c
->dstFormat
== AV_PIX_FMT_NE(RGB444LE
, RGB444BE
) ||
715 c
->dstFormat
== AV_PIX_FMT_NE(BGR565LE
, BGR565BE
) ||
716 c
->dstFormat
== AV_PIX_FMT_NE(BGR555LE
, BGR555BE
) ||
717 c
->dstFormat
== AV_PIX_FMT_NE(BGR444LE
, BGR444BE
);
718 const int bpp
= c
->dstFormatBpp
;
722 int i
, base
, rbase
, gbase
, bbase
, av_uninit(abase
), needAlpha
;
723 const int yoffs
= fullRange
? 384 : 326;
725 int64_t crv
= inv_table
[0];
726 int64_t cbu
= inv_table
[1];
727 int64_t cgu
= -inv_table
[2];
728 int64_t cgv
= -inv_table
[3];
729 int64_t cy
= 1 << 16;
734 cy
= (cy
* 255) / 219;
737 crv
= (crv
* 224) / 255;
738 cbu
= (cbu
* 224) / 255;
739 cgu
= (cgu
* 224) / 255;
740 cgv
= (cgv
* 224) / 255;
743 cy
= (cy
* contrast
) >> 16;
744 crv
= (crv
* contrast
* saturation
) >> 32;
745 cbu
= (cbu
* contrast
* saturation
) >> 32;
746 cgu
= (cgu
* contrast
* saturation
) >> 32;
747 cgv
= (cgv
* contrast
* saturation
) >> 32;
748 oy
-= 256 * brightness
;
750 c
->uOffset
= 0x0400040004000400LL
;
751 c
->vOffset
= 0x0400040004000400LL
;
752 c
->yCoeff
= roundToInt16(cy
* 8192) * 0x0001000100010001ULL
;
753 c
->vrCoeff
= roundToInt16(crv
* 8192) * 0x0001000100010001ULL
;
754 c
->ubCoeff
= roundToInt16(cbu
* 8192) * 0x0001000100010001ULL
;
755 c
->vgCoeff
= roundToInt16(cgv
* 8192) * 0x0001000100010001ULL
;
756 c
->ugCoeff
= roundToInt16(cgu
* 8192) * 0x0001000100010001ULL
;
757 c
->yOffset
= roundToInt16(oy
* 8) * 0x0001000100010001ULL
;
759 c
->yuv2rgb_y_coeff
= (int16_t)roundToInt16(cy
<< 13);
760 c
->yuv2rgb_y_offset
= (int16_t)roundToInt16(oy
<< 9);
761 c
->yuv2rgb_v2r_coeff
= (int16_t)roundToInt16(crv
<< 13);
762 c
->yuv2rgb_v2g_coeff
= (int16_t)roundToInt16(cgv
<< 13);
763 c
->yuv2rgb_u2g_coeff
= (int16_t)roundToInt16(cgu
<< 13);
764 c
->yuv2rgb_u2b_coeff
= (int16_t)roundToInt16(cbu
<< 13);
766 //scale coefficients by cy
767 crv
= ((crv
<< 16) + 0x8000) / FFMAX(cy
, 1);
768 cbu
= ((cbu
<< 16) + 0x8000) / FFMAX(cy
, 1);
769 cgu
= ((cgu
<< 16) + 0x8000) / FFMAX(cy
, 1);
770 cgv
= ((cgv
<< 16) + 0x8000) / FFMAX(cy
, 1);
772 av_freep(&c
->yuvTable
);
776 c
->yuvTable
= av_malloc(1024);
777 y_table
= c
->yuvTable
;
778 yb
= -(384 << 16) - oy
;
779 for (i
= 0; i
< 1024 - 110; i
++) {
780 y_table
[i
+ 110] = av_clip_uint8((yb
+ 0x8000) >> 16) >> 7;
783 fill_table(c
->table_gU
, 1, cgu
, y_table
+ yoffs
);
784 fill_gv_table(c
->table_gV
, 1, cgv
);
788 rbase
= isRgb
? 3 : 0;
790 bbase
= isRgb
? 0 : 3;
791 c
->yuvTable
= av_malloc(1024 * 3);
792 y_table
= c
->yuvTable
;
793 yb
= -(384 << 16) - oy
;
794 for (i
= 0; i
< 1024 - 110; i
++) {
795 int yval
= av_clip_uint8((yb
+ 0x8000) >> 16);
796 y_table
[i
+ 110] = (yval
>> 7) << rbase
;
797 y_table
[i
+ 37 + 1024] = ((yval
+ 43) / 85) << gbase
;
798 y_table
[i
+ 110 + 2048] = (yval
>> 7) << bbase
;
801 fill_table(c
->table_rV
, 1, crv
, y_table
+ yoffs
);
802 fill_table(c
->table_gU
, 1, cgu
, y_table
+ yoffs
+ 1024);
803 fill_table(c
->table_bU
, 1, cbu
, y_table
+ yoffs
+ 2048);
804 fill_gv_table(c
->table_gV
, 1, cgv
);
807 rbase
= isRgb
? 5 : 0;
808 gbase
= isRgb
? 2 : 3;
809 bbase
= isRgb
? 0 : 6;
810 c
->yuvTable
= av_malloc(1024 * 3);
811 y_table
= c
->yuvTable
;
812 yb
= -(384 << 16) - oy
;
813 for (i
= 0; i
< 1024 - 38; i
++) {
814 int yval
= av_clip_uint8((yb
+ 0x8000) >> 16);
815 y_table
[i
+ 16] = ((yval
+ 18) / 36) << rbase
;
816 y_table
[i
+ 16 + 1024] = ((yval
+ 18) / 36) << gbase
;
817 y_table
[i
+ 37 + 2048] = ((yval
+ 43) / 85) << bbase
;
820 fill_table(c
->table_rV
, 1, crv
, y_table
+ yoffs
);
821 fill_table(c
->table_gU
, 1, cgu
, y_table
+ yoffs
+ 1024);
822 fill_table(c
->table_bU
, 1, cbu
, y_table
+ yoffs
+ 2048);
823 fill_gv_table(c
->table_gV
, 1, cgv
);
826 rbase
= isRgb
? 8 : 0;
828 bbase
= isRgb
? 0 : 8;
829 c
->yuvTable
= av_malloc(1024 * 3 * 2);
830 y_table16
= c
->yuvTable
;
831 yb
= -(384 << 16) - oy
;
832 for (i
= 0; i
< 1024; i
++) {
833 uint8_t yval
= av_clip_uint8((yb
+ 0x8000) >> 16);
834 y_table16
[i
] = (yval
>> 4) << rbase
;
835 y_table16
[i
+ 1024] = (yval
>> 4) << gbase
;
836 y_table16
[i
+ 2048] = (yval
>> 4) << bbase
;
840 for (i
= 0; i
< 1024 * 3; i
++)
841 y_table16
[i
] = av_bswap16(y_table16
[i
]);
842 fill_table(c
->table_rV
, 2, crv
, y_table16
+ yoffs
);
843 fill_table(c
->table_gU
, 2, cgu
, y_table16
+ yoffs
+ 1024);
844 fill_table(c
->table_bU
, 2, cbu
, y_table16
+ yoffs
+ 2048);
845 fill_gv_table(c
->table_gV
, 2, cgv
);
849 rbase
= isRgb
? bpp
- 5 : 0;
851 bbase
= isRgb
? 0 : (bpp
- 5);
852 c
->yuvTable
= av_malloc(1024 * 3 * 2);
853 y_table16
= c
->yuvTable
;
854 yb
= -(384 << 16) - oy
;
855 for (i
= 0; i
< 1024; i
++) {
856 uint8_t yval
= av_clip_uint8((yb
+ 0x8000) >> 16);
857 y_table16
[i
] = (yval
>> 3) << rbase
;
858 y_table16
[i
+ 1024] = (yval
>> (18 - bpp
)) << gbase
;
859 y_table16
[i
+ 2048] = (yval
>> 3) << bbase
;
863 for (i
= 0; i
< 1024 * 3; i
++)
864 y_table16
[i
] = av_bswap16(y_table16
[i
]);
865 fill_table(c
->table_rV
, 2, crv
, y_table16
+ yoffs
);
866 fill_table(c
->table_gU
, 2, cgu
, y_table16
+ yoffs
+ 1024);
867 fill_table(c
->table_bU
, 2, cbu
, y_table16
+ yoffs
+ 2048);
868 fill_gv_table(c
->table_gV
, 2, cgv
);
872 c
->yuvTable
= av_malloc(1024);
873 y_table
= c
->yuvTable
;
874 yb
= -(384 << 16) - oy
;
875 for (i
= 0; i
< 1024; i
++) {
876 y_table
[i
] = av_clip_uint8((yb
+ 0x8000) >> 16);
879 fill_table(c
->table_rV
, 1, crv
, y_table
+ yoffs
);
880 fill_table(c
->table_gU
, 1, cgu
, y_table
+ yoffs
);
881 fill_table(c
->table_bU
, 1, cbu
, y_table
+ yoffs
);
882 fill_gv_table(c
->table_gV
, 1, cgv
);
886 base
= (c
->dstFormat
== AV_PIX_FMT_RGB32_1
||
887 c
->dstFormat
== AV_PIX_FMT_BGR32_1
) ? 8 : 0;
888 rbase
= base
+ (isRgb
? 16 : 0);
890 bbase
= base
+ (isRgb
? 0 : 16);
891 needAlpha
= CONFIG_SWSCALE_ALPHA
&& isALPHA(c
->srcFormat
);
893 abase
= (base
+ 24) & 31;
894 c
->yuvTable
= av_malloc(1024 * 3 * 4);
895 y_table32
= c
->yuvTable
;
896 yb
= -(384 << 16) - oy
;
897 for (i
= 0; i
< 1024; i
++) {
898 unsigned yval
= av_clip_uint8((yb
+ 0x8000) >> 16);
899 y_table32
[i
] = (yval
<< rbase
) +
900 (needAlpha
? 0 : (255u << abase
));
901 y_table32
[i
+ 1024] = yval
<< gbase
;
902 y_table32
[i
+ 2048] = yval
<< bbase
;
905 fill_table(c
->table_rV
, 4, crv
, y_table32
+ yoffs
);
906 fill_table(c
->table_gU
, 4, cgu
, y_table32
+ yoffs
+ 1024);
907 fill_table(c
->table_bU
, 4, cbu
, y_table32
+ yoffs
+ 2048);
908 fill_gv_table(c
->table_gV
, 4, cgv
);
911 if(!isPlanar(c
->dstFormat
) || bpp
<= 24)
912 av_log(c
, AV_LOG_ERROR
, "%ibpp not supported by yuv2rgb\n", bpp
);