2 * software YUV to RGB converter
4 * Copyright (C) 2009 Konstantin Shishkov
6 * 1,4,8bpp support and context / deglobalize stuff
7 * by Michael Niedermayer (michaelni@gmx.at)
9 * This file is part of Libav.
11 * Libav is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * Libav is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with Libav; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "libavutil/cpu.h"
32 #include "libavutil/bswap.h"
36 #include "swscale_internal.h"
38 /* 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
[8][4] = {
50 { 117504, 138453, 13954, 34903 }, /* no sequence_display_extension */
51 { 117504, 138453, 13954, 34903 }, /* 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) */
60 const int *sws_getCoefficients(int colorspace
)
62 if (colorspace
> 7 || colorspace
< 0)
63 colorspace
= SWS_CS_DEFAULT
;
64 return ff_yuv2rgb_coeffs
[colorspace
];
67 #define LOADCHROMA(i) \
70 r = (void *)c->table_rV[V]; \
71 g = (void *)(c->table_gU[U] + c->table_gV[V]); \
72 b = (void *)c->table_bU[U];
74 #define PUTRGB(dst, src, i) \
76 dst[2 * i] = r[Y] + g[Y] + b[Y]; \
78 dst[2 * i + 1] = r[Y] + g[Y] + b[Y];
80 #define PUTRGB24(dst, src, i) \
82 dst[6 * i + 0] = r[Y]; \
83 dst[6 * i + 1] = g[Y]; \
84 dst[6 * i + 2] = b[Y]; \
86 dst[6 * i + 3] = r[Y]; \
87 dst[6 * i + 4] = g[Y]; \
88 dst[6 * i + 5] = b[Y];
90 #define PUTBGR24(dst, src, i) \
92 dst[6 * i + 0] = b[Y]; \
93 dst[6 * i + 1] = g[Y]; \
94 dst[6 * i + 2] = r[Y]; \
96 dst[6 * i + 3] = b[Y]; \
97 dst[6 * i + 4] = g[Y]; \
98 dst[6 * i + 5] = r[Y];
100 #define PUTRGBA(dst, ysrc, asrc, i, s) \
102 dst[2 * i] = r[Y] + g[Y] + b[Y] + (asrc[2 * i] << s); \
103 Y = ysrc[2 * i + 1]; \
104 dst[2 * i + 1] = r[Y] + g[Y] + b[Y] + (asrc[2 * i + 1] << s);
106 #define PUTRGB48(dst, src, i) \
108 dst[12 * i + 0] = dst[12 * i + 1] = r[Y]; \
109 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
110 dst[12 * i + 4] = dst[12 * i + 5] = b[Y]; \
111 Y = src[ 2 * i + 1]; \
112 dst[12 * i + 6] = dst[12 * i + 7] = r[Y]; \
113 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
114 dst[12 * i + 10] = dst[12 * i + 11] = b[Y];
116 #define PUTBGR48(dst, src, i) \
118 dst[12 * i + 0] = dst[12 * i + 1] = b[Y]; \
119 dst[12 * i + 2] = dst[12 * i + 3] = g[Y]; \
120 dst[12 * i + 4] = dst[12 * i + 5] = r[Y]; \
121 Y = src[2 * i + 1]; \
122 dst[12 * i + 6] = dst[12 * i + 7] = b[Y]; \
123 dst[12 * i + 8] = dst[12 * i + 9] = g[Y]; \
124 dst[12 * i + 10] = dst[12 * i + 11] = r[Y];
126 #define YUV2RGBFUNC(func_name, dst_type, alpha) \
127 static int func_name(SwsContext *c, const uint8_t *src[], \
128 int srcStride[], int srcSliceY, int srcSliceH, \
129 uint8_t *dst[], int dstStride[]) \
133 if (!alpha && c->srcFormat == AV_PIX_FMT_YUV422P) { \
137 for (y = 0; y < srcSliceH; y += 2) { \
139 (dst_type *)(dst[0] + (y + srcSliceY) * dstStride[0]); \
141 (dst_type *)(dst[0] + (y + srcSliceY + 1) * dstStride[0]); \
142 dst_type av_unused *r, *g, *b; \
143 const uint8_t *py_1 = src[0] + y * srcStride[0]; \
144 const uint8_t *py_2 = py_1 + srcStride[0]; \
145 const uint8_t *pu = src[1] + (y >> 1) * srcStride[1]; \
146 const uint8_t *pv = src[2] + (y >> 1) * srcStride[2]; \
147 const uint8_t av_unused *pa_1, *pa_2; \
148 unsigned int h_size = c->dstW >> 3; \
150 pa_1 = src[3] + y * srcStride[3]; \
151 pa_2 = pa_1 + srcStride[3]; \
154 int av_unused U, V, Y; \
156 #define ENDYUV2RGBLINE(dst_delta, ss) \
161 dst_1 += dst_delta >> ss; \
162 dst_2 += dst_delta >> ss; \
164 if (c->dstW & (4 >> ss)) { \
165 int av_unused Y, U, V; \
167 #define ENDYUV2RGBFUNC() \
173 #define CLOSEYUV2RGBFUNC(dst_delta) \
174 ENDYUV2RGBLINE(dst_delta, 0) \
177 YUV2RGBFUNC(yuv2rgb_c_48
, uint8_t, 0)
179 PUTRGB48(dst_1
, py_1
, 0);
180 PUTRGB48(dst_2
, py_2
, 0);
183 PUTRGB48(dst_2
, py_2
, 1);
184 PUTRGB48(dst_1
, py_1
, 1);
187 PUTRGB48(dst_1
, py_1
, 2);
188 PUTRGB48(dst_2
, py_2
, 2);
191 PUTRGB48(dst_2
, py_2
, 3);
192 PUTRGB48(dst_1
, py_1
, 3);
193 ENDYUV2RGBLINE(48, 0)
195 PUTRGB48(dst_1
, py_1
, 0);
196 PUTRGB48(dst_2
, py_2
, 0);
199 PUTRGB48(dst_2
, py_2
, 1);
200 PUTRGB48(dst_1
, py_1
, 1);
201 ENDYUV2RGBLINE(48, 1)
203 PUTRGB48(dst_1
, py_1
, 0);
204 PUTRGB48(dst_2
, py_2
, 0);
207 YUV2RGBFUNC(yuv2rgb_c_bgr48
, uint8_t, 0)
209 PUTBGR48(dst_1
, py_1
, 0);
210 PUTBGR48(dst_2
, py_2
, 0);
213 PUTBGR48(dst_2
, py_2
, 1);
214 PUTBGR48(dst_1
, py_1
, 1);
217 PUTBGR48(dst_1
, py_1
, 2);
218 PUTBGR48(dst_2
, py_2
, 2);
221 PUTBGR48(dst_2
, py_2
, 3);
222 PUTBGR48(dst_1
, py_1
, 3);
223 ENDYUV2RGBLINE(48, 0)
225 PUTBGR48(dst_1
, py_1
, 0);
226 PUTBGR48(dst_2
, py_2
, 0);
229 PUTBGR48(dst_2
, py_2
, 1);
230 PUTBGR48(dst_1
, py_1
, 1);
231 ENDYUV2RGBLINE(48, 1)
233 PUTBGR48(dst_1
, py_1
, 0);
234 PUTBGR48(dst_2
, py_2
, 0);
237 YUV2RGBFUNC(yuv2rgb_c_32
, uint32_t, 0)
239 PUTRGB(dst_1
, py_1
, 0);
240 PUTRGB(dst_2
, py_2
, 0);
243 PUTRGB(dst_2
, py_2
, 1);
244 PUTRGB(dst_1
, py_1
, 1);
247 PUTRGB(dst_1
, py_1
, 2);
248 PUTRGB(dst_2
, py_2
, 2);
251 PUTRGB(dst_2
, py_2
, 3);
252 PUTRGB(dst_1
, py_1
, 3);
255 PUTRGB(dst_1
, py_1
, 0);
256 PUTRGB(dst_2
, py_2
, 0);
259 PUTRGB(dst_2
, py_2
, 1);
260 PUTRGB(dst_1
, py_1
, 1);
263 PUTRGB(dst_1
, py_1
, 0);
264 PUTRGB(dst_2
, py_2
, 0);
267 YUV2RGBFUNC(yuva2rgba_c
, uint32_t, 1)
269 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 24);
270 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 24);
273 PUTRGBA(dst_2
, py_2
, pa_2
, 1, 24);
274 PUTRGBA(dst_1
, py_1
, pa_1
, 1, 24);
277 PUTRGBA(dst_1
, py_1
, pa_1
, 2, 24);
278 PUTRGBA(dst_2
, py_2
, pa_2
, 2, 24);
281 PUTRGBA(dst_2
, py_2
, pa_2
, 3, 24);
282 PUTRGBA(dst_1
, py_1
, pa_1
, 3, 24);
287 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 24);
288 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 24);
291 PUTRGBA(dst_2
, py_2
, pa_2
, 1, 24);
292 PUTRGBA(dst_1
, py_1
, pa_1
, 1, 24);
297 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 24);
298 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 24);
301 YUV2RGBFUNC(yuva2argb_c
, uint32_t, 1)
303 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 0);
304 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 0);
307 PUTRGBA(dst_2
, py_2
, pa_2
, 1, 0);
308 PUTRGBA(dst_1
, py_1
, pa_1
, 1, 0);
311 PUTRGBA(dst_1
, py_1
, pa_1
, 2, 0);
312 PUTRGBA(dst_2
, py_2
, pa_2
, 2, 0);
315 PUTRGBA(dst_2
, py_2
, pa_2
, 3, 0);
316 PUTRGBA(dst_1
, py_1
, pa_1
, 3, 0);
321 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 0);
322 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 0);
325 PUTRGBA(dst_2
, py_2
, pa_2
, 1, 0);
326 PUTRGBA(dst_1
, py_1
, pa_1
, 1, 0);
331 PUTRGBA(dst_1
, py_1
, pa_1
, 0, 0);
332 PUTRGBA(dst_2
, py_2
, pa_2
, 0, 0);
335 YUV2RGBFUNC(yuv2rgb_c_24_rgb
, uint8_t, 0)
337 PUTRGB24(dst_1
, py_1
, 0);
338 PUTRGB24(dst_2
, py_2
, 0);
341 PUTRGB24(dst_2
, py_2
, 1);
342 PUTRGB24(dst_1
, py_1
, 1);
345 PUTRGB24(dst_1
, py_1
, 2);
346 PUTRGB24(dst_2
, py_2
, 2);
349 PUTRGB24(dst_2
, py_2
, 3);
350 PUTRGB24(dst_1
, py_1
, 3);
351 ENDYUV2RGBLINE(24, 0)
353 PUTRGB24(dst_1
, py_1
, 0);
354 PUTRGB24(dst_2
, py_2
, 0);
357 PUTRGB24(dst_2
, py_2
, 1);
358 PUTRGB24(dst_1
, py_1
, 1);
359 ENDYUV2RGBLINE(24, 1)
361 PUTRGB24(dst_1
, py_1
, 0);
362 PUTRGB24(dst_2
, py_2
, 0);
365 // only trivial mods from yuv2rgb_c_24_rgb
366 YUV2RGBFUNC(yuv2rgb_c_24_bgr
, uint8_t, 0)
368 PUTBGR24(dst_1
, py_1
, 0);
369 PUTBGR24(dst_2
, py_2
, 0);
372 PUTBGR24(dst_2
, py_2
, 1);
373 PUTBGR24(dst_1
, py_1
, 1);
376 PUTBGR24(dst_1
, py_1
, 2);
377 PUTBGR24(dst_2
, py_2
, 2);
380 PUTBGR24(dst_2
, py_2
, 3);
381 PUTBGR24(dst_1
, py_1
, 3);
382 ENDYUV2RGBLINE(24, 0)
384 PUTBGR24(dst_1
, py_1
, 0);
385 PUTBGR24(dst_2
, py_2
, 0);
388 PUTBGR24(dst_2
, py_2
, 1);
389 PUTBGR24(dst_1
, py_1
, 1);
390 ENDYUV2RGBLINE(24, 1)
392 PUTBGR24(dst_1
, py_1
, 0);
393 PUTBGR24(dst_2
, py_2
, 0);
396 // This is exactly the same code as yuv2rgb_c_32 except for the types of
397 // r, g, b, dst_1, dst_2
398 YUV2RGBFUNC(yuv2rgb_c_16
, uint16_t, 0)
400 PUTRGB(dst_1
, py_1
, 0);
401 PUTRGB(dst_2
, py_2
, 0);
404 PUTRGB(dst_2
, py_2
, 1);
405 PUTRGB(dst_1
, py_1
, 1);
408 PUTRGB(dst_1
, py_1
, 2);
409 PUTRGB(dst_2
, py_2
, 2);
412 PUTRGB(dst_2
, py_2
, 3);
413 PUTRGB(dst_1
, py_1
, 3);
416 // r, g, b, dst_1, dst_2
417 YUV2RGBFUNC(yuv2rgb_c_12_ordered_dither
, uint16_t, 0)
418 const uint8_t *d16
= ff_dither_4x4_16
[y
& 3];
420 #define PUTRGB12(dst, src, i, o) \
422 dst[2 * i] = r[Y + d16[0 + o]] + \
423 g[Y + d16[0 + o]] + \
425 Y = src[2 * i + 1]; \
426 dst[2 * i + 1] = r[Y + d16[1 + o]] + \
427 g[Y + d16[1 + o]] + \
431 PUTRGB12(dst_1
, py_1
, 0, 0);
432 PUTRGB12(dst_2
, py_2
, 0, 0 + 8);
435 PUTRGB12(dst_2
, py_2
, 1, 2 + 8);
436 PUTRGB12(dst_1
, py_1
, 1, 2);
439 PUTRGB12(dst_1
, py_1
, 2, 4);
440 PUTRGB12(dst_2
, py_2
, 2, 4 + 8);
443 PUTRGB12(dst_2
, py_2
, 3, 6 + 8);
444 PUTRGB12(dst_1
, py_1
, 3, 6);
447 // r, g, b, dst_1, dst_2
448 YUV2RGBFUNC(yuv2rgb_c_8_ordered_dither
, uint8_t, 0)
449 const uint8_t *d32
= ff_dither_8x8_32
[y
& 7];
450 const uint8_t *d64
= ff_dither_8x8_73
[y
& 7];
452 #define PUTRGB8(dst, src, i, o) \
454 dst[2 * i] = r[Y + d32[0 + o]] + \
455 g[Y + d32[0 + o]] + \
457 Y = src[2 * i + 1]; \
458 dst[2 * i + 1] = r[Y + d32[1 + o]] + \
459 g[Y + d32[1 + o]] + \
463 PUTRGB8(dst_1
, py_1
, 0, 0);
464 PUTRGB8(dst_2
, py_2
, 0, 0 + 8);
467 PUTRGB8(dst_2
, py_2
, 1, 2 + 8);
468 PUTRGB8(dst_1
, py_1
, 1, 2);
471 PUTRGB8(dst_1
, py_1
, 2, 4);
472 PUTRGB8(dst_2
, py_2
, 2, 4 + 8);
475 PUTRGB8(dst_2
, py_2
, 3, 6 + 8);
476 PUTRGB8(dst_1
, py_1
, 3, 6);
479 YUV2RGBFUNC(yuv2rgb_c_4_ordered_dither
, uint8_t, 0)
480 const uint8_t * d64
= ff_dither_8x8_73
[y
& 7];
481 const uint8_t *d128
= ff_dither_8x8_220
[y
& 7];
484 #define PUTRGB4D(dst, src, i, o) \
486 acc = r[Y + d128[0 + o]] + \
487 g[Y + d64[0 + o]] + \
488 b[Y + d128[0 + o]]; \
489 Y = src[2 * i + 1]; \
490 acc |= (r[Y + d128[1 + o]] + \
491 g[Y + d64[1 + o]] + \
492 b[Y + d128[1 + o]]) << 4; \
496 PUTRGB4D(dst_1
, py_1
, 0, 0);
497 PUTRGB4D(dst_2
, py_2
, 0, 0 + 8);
500 PUTRGB4D(dst_2
, py_2
, 1, 2 + 8);
501 PUTRGB4D(dst_1
, py_1
, 1, 2);
504 PUTRGB4D(dst_1
, py_1
, 2, 4);
505 PUTRGB4D(dst_2
, py_2
, 2, 4 + 8);
508 PUTRGB4D(dst_2
, py_2
, 3, 6 + 8);
509 PUTRGB4D(dst_1
, py_1
, 3, 6);
512 YUV2RGBFUNC(yuv2rgb_c_4b_ordered_dither
, uint8_t, 0)
513 const uint8_t *d64
= ff_dither_8x8_73
[y
& 7];
514 const uint8_t *d128
= ff_dither_8x8_220
[y
& 7];
516 #define PUTRGB4DB(dst, src, i, o) \
518 dst[2 * i] = r[Y + d128[0 + o]] + \
519 g[Y + d64[0 + o]] + \
520 b[Y + d128[0 + o]]; \
521 Y = src[2 * i + 1]; \
522 dst[2 * i + 1] = r[Y + d128[1 + o]] + \
523 g[Y + d64[1 + o]] + \
527 PUTRGB4DB(dst_1
, py_1
, 0, 0);
528 PUTRGB4DB(dst_2
, py_2
, 0, 0 + 8);
531 PUTRGB4DB(dst_2
, py_2
, 1, 2 + 8);
532 PUTRGB4DB(dst_1
, py_1
, 1, 2);
535 PUTRGB4DB(dst_1
, py_1
, 2, 4);
536 PUTRGB4DB(dst_2
, py_2
, 2, 4 + 8);
539 PUTRGB4DB(dst_2
, py_2
, 3, 6 + 8);
540 PUTRGB4DB(dst_1
, py_1
, 3, 6);
543 YUV2RGBFUNC(yuv2rgb_c_1_ordered_dither
, uint8_t, 0)
544 const uint8_t *d128
= ff_dither_8x8_220
[y
& 7];
545 char out_1
= 0, out_2
= 0;
546 g
= c
->table_gU
[128] + c
->table_gV
[128];
548 #define PUTRGB1(out, src, i, o) \
550 out += out + g[Y + d128[0 + o]]; \
551 Y = src[2 * i + 1]; \
552 out += out + g[Y + d128[1 + o]];
554 PUTRGB1(out_1
, py_1
, 0, 0);
555 PUTRGB1(out_2
, py_2
, 0, 0 + 8);
557 PUTRGB1(out_2
, py_2
, 1, 2 + 8);
558 PUTRGB1(out_1
, py_1
, 1, 2);
560 PUTRGB1(out_1
, py_1
, 2, 4);
561 PUTRGB1(out_2
, py_2
, 2, 4 + 8);
563 PUTRGB1(out_2
, py_2
, 3, 6 + 8);
564 PUTRGB1(out_1
, py_1
, 3, 6);
570 SwsFunc
ff_yuv2rgb_get_func_ptr(SwsContext
*c
)
575 t
= ff_yuv2rgb_init_ppc(c
);
577 t
= ff_yuv2rgb_init_x86(c
);
582 av_log(c
, AV_LOG_WARNING
,
583 "No accelerated colorspace conversion found from %s to %s.\n",
584 sws_format_name(c
->srcFormat
), sws_format_name(c
->dstFormat
));
586 switch (c
->dstFormat
) {
587 case AV_PIX_FMT_BGR48BE
:
588 case AV_PIX_FMT_BGR48LE
:
589 return yuv2rgb_c_bgr48
;
590 case AV_PIX_FMT_RGB48BE
:
591 case AV_PIX_FMT_RGB48LE
:
593 case AV_PIX_FMT_ARGB
:
594 case AV_PIX_FMT_ABGR
:
595 if (CONFIG_SWSCALE_ALPHA
&& c
->srcFormat
== AV_PIX_FMT_YUVA420P
)
597 case AV_PIX_FMT_RGBA
:
598 case AV_PIX_FMT_BGRA
:
599 if (CONFIG_SWSCALE_ALPHA
&& c
->srcFormat
== AV_PIX_FMT_YUVA420P
)
603 case AV_PIX_FMT_RGB24
:
604 return yuv2rgb_c_24_rgb
;
605 case AV_PIX_FMT_BGR24
:
606 return yuv2rgb_c_24_bgr
;
607 case AV_PIX_FMT_RGB565
:
608 case AV_PIX_FMT_BGR565
:
609 case AV_PIX_FMT_RGB555
:
610 case AV_PIX_FMT_BGR555
:
612 case AV_PIX_FMT_RGB444
:
613 case AV_PIX_FMT_BGR444
:
614 return yuv2rgb_c_12_ordered_dither
;
615 case AV_PIX_FMT_RGB8
:
616 case AV_PIX_FMT_BGR8
:
617 return yuv2rgb_c_8_ordered_dither
;
618 case AV_PIX_FMT_RGB4
:
619 case AV_PIX_FMT_BGR4
:
620 return yuv2rgb_c_4_ordered_dither
;
621 case AV_PIX_FMT_RGB4_BYTE
:
622 case AV_PIX_FMT_BGR4_BYTE
:
623 return yuv2rgb_c_4b_ordered_dither
;
624 case AV_PIX_FMT_MONOBLACK
:
625 return yuv2rgb_c_1_ordered_dither
;
632 static void fill_table(uint8_t *table
[256], const int elemsize
,
633 const int inc
, void *y_tab
)
637 uint8_t *y_table
= y_tab
;
639 y_table
-= elemsize
* (inc
>> 9);
641 for (i
= 0; i
< 256; i
++) {
642 table
[i
] = y_table
+ elemsize
* (cb
>> 16);
647 static void fill_gv_table(int table
[256], const int elemsize
, const int inc
)
651 int off
= -(inc
>> 9);
653 for (i
= 0; i
< 256; i
++) {
654 table
[i
] = elemsize
* (off
+ (cb
>> 16));
659 static uint16_t roundToInt16(int64_t f
)
661 int r
= (f
+ (1 << 15)) >> 16;
671 av_cold
int ff_yuv2rgb_c_init_tables(SwsContext
*c
, const int inv_table
[4],
672 int fullRange
, int brightness
,
673 int contrast
, int saturation
)
675 const int isRgb
= c
->dstFormat
== AV_PIX_FMT_RGB32
||
676 c
->dstFormat
== AV_PIX_FMT_RGB32_1
||
677 c
->dstFormat
== AV_PIX_FMT_BGR24
||
678 c
->dstFormat
== AV_PIX_FMT_RGB565BE
||
679 c
->dstFormat
== AV_PIX_FMT_RGB565LE
||
680 c
->dstFormat
== AV_PIX_FMT_RGB555BE
||
681 c
->dstFormat
== AV_PIX_FMT_RGB555LE
||
682 c
->dstFormat
== AV_PIX_FMT_RGB444BE
||
683 c
->dstFormat
== AV_PIX_FMT_RGB444LE
||
684 c
->dstFormat
== AV_PIX_FMT_RGB8
||
685 c
->dstFormat
== AV_PIX_FMT_RGB4
||
686 c
->dstFormat
== AV_PIX_FMT_RGB4_BYTE
||
687 c
->dstFormat
== AV_PIX_FMT_MONOBLACK
;
688 const int isNotNe
= c
->dstFormat
== AV_PIX_FMT_NE(RGB565LE
, RGB565BE
) ||
689 c
->dstFormat
== AV_PIX_FMT_NE(RGB555LE
, RGB555BE
) ||
690 c
->dstFormat
== AV_PIX_FMT_NE(RGB444LE
, RGB444BE
) ||
691 c
->dstFormat
== AV_PIX_FMT_NE(BGR565LE
, BGR565BE
) ||
692 c
->dstFormat
== AV_PIX_FMT_NE(BGR555LE
, BGR555BE
) ||
693 c
->dstFormat
== AV_PIX_FMT_NE(BGR444LE
, BGR444BE
);
694 const int bpp
= c
->dstFormatBpp
;
698 int i
, base
, rbase
, gbase
, bbase
, abase
, needAlpha
;
699 const int yoffs
= fullRange
? 384 : 326;
701 int64_t crv
= inv_table
[0];
702 int64_t cbu
= inv_table
[1];
703 int64_t cgu
= -inv_table
[2];
704 int64_t cgv
= -inv_table
[3];
705 int64_t cy
= 1 << 16;
710 cy
= (cy
* 255) / 219;
713 crv
= (crv
* 224) / 255;
714 cbu
= (cbu
* 224) / 255;
715 cgu
= (cgu
* 224) / 255;
716 cgv
= (cgv
* 224) / 255;
719 cy
= (cy
* contrast
) >> 16;
720 crv
= (crv
* contrast
* saturation
) >> 32;
721 cbu
= (cbu
* contrast
* saturation
) >> 32;
722 cgu
= (cgu
* contrast
* saturation
) >> 32;
723 cgv
= (cgv
* contrast
* saturation
) >> 32;
724 oy
-= 256 * brightness
;
726 c
->uOffset
= 0x0400040004000400LL
;
727 c
->vOffset
= 0x0400040004000400LL
;
728 c
->yCoeff
= roundToInt16(cy
* 8192) * 0x0001000100010001ULL
;
729 c
->vrCoeff
= roundToInt16(crv
* 8192) * 0x0001000100010001ULL
;
730 c
->ubCoeff
= roundToInt16(cbu
* 8192) * 0x0001000100010001ULL
;
731 c
->vgCoeff
= roundToInt16(cgv
* 8192) * 0x0001000100010001ULL
;
732 c
->ugCoeff
= roundToInt16(cgu
* 8192) * 0x0001000100010001ULL
;
733 c
->yOffset
= roundToInt16(oy
* 8) * 0x0001000100010001ULL
;
735 c
->yuv2rgb_y_coeff
= (int16_t)roundToInt16(cy
<< 13);
736 c
->yuv2rgb_y_offset
= (int16_t)roundToInt16(oy
<< 9);
737 c
->yuv2rgb_v2r_coeff
= (int16_t)roundToInt16(crv
<< 13);
738 c
->yuv2rgb_v2g_coeff
= (int16_t)roundToInt16(cgv
<< 13);
739 c
->yuv2rgb_u2g_coeff
= (int16_t)roundToInt16(cgu
<< 13);
740 c
->yuv2rgb_u2b_coeff
= (int16_t)roundToInt16(cbu
<< 13);
742 //scale coefficients by cy
743 crv
= ((crv
<< 16) + 0x8000) / cy
;
744 cbu
= ((cbu
<< 16) + 0x8000) / cy
;
745 cgu
= ((cgu
<< 16) + 0x8000) / cy
;
746 cgv
= ((cgv
<< 16) + 0x8000) / cy
;
748 av_free(c
->yuvTable
);
750 #define ALLOC_YUV_TABLE(x) \
751 c->yuvTable = av_malloc(x); \
753 return AVERROR(ENOMEM);
756 ALLOC_YUV_TABLE(1024);
757 y_table
= c
->yuvTable
;
758 yb
= -(384 << 16) - oy
;
759 for (i
= 0; i
< 1024 - 110; i
++) {
760 y_table
[i
+ 110] = av_clip_uint8((yb
+ 0x8000) >> 16) >> 7;
763 fill_table(c
->table_gU
, 1, cgu
, y_table
+ yoffs
);
764 fill_gv_table(c
->table_gV
, 1, cgv
);
768 rbase
= isRgb
? 3 : 0;
770 bbase
= isRgb
? 0 : 3;
771 ALLOC_YUV_TABLE(1024 * 3);
772 y_table
= c
->yuvTable
;
773 yb
= -(384 << 16) - oy
;
774 for (i
= 0; i
< 1024 - 110; i
++) {
775 int yval
= av_clip_uint8((yb
+ 0x8000) >> 16);
776 y_table
[i
+ 110] = (yval
>> 7) << rbase
;
777 y_table
[i
+ 37 + 1024] = ((yval
+ 43) / 85) << gbase
;
778 y_table
[i
+ 110 + 2048] = (yval
>> 7) << bbase
;
781 fill_table(c
->table_rV
, 1, crv
, y_table
+ yoffs
);
782 fill_table(c
->table_gU
, 1, cgu
, y_table
+ yoffs
+ 1024);
783 fill_table(c
->table_bU
, 1, cbu
, y_table
+ yoffs
+ 2048);
784 fill_gv_table(c
->table_gV
, 1, cgv
);
787 rbase
= isRgb
? 5 : 0;
788 gbase
= isRgb
? 2 : 3;
789 bbase
= isRgb
? 0 : 6;
790 ALLOC_YUV_TABLE(1024 * 3);
791 y_table
= c
->yuvTable
;
792 yb
= -(384 << 16) - oy
;
793 for (i
= 0; i
< 1024 - 38; i
++) {
794 int yval
= av_clip_uint8((yb
+ 0x8000) >> 16);
795 y_table
[i
+ 16] = ((yval
+ 18) / 36) << rbase
;
796 y_table
[i
+ 16 + 1024] = ((yval
+ 18) / 36) << gbase
;
797 y_table
[i
+ 37 + 2048] = ((yval
+ 43) / 85) << bbase
;
800 fill_table(c
->table_rV
, 1, crv
, y_table
+ yoffs
);
801 fill_table(c
->table_gU
, 1, cgu
, y_table
+ yoffs
+ 1024);
802 fill_table(c
->table_bU
, 1, cbu
, y_table
+ yoffs
+ 2048);
803 fill_gv_table(c
->table_gV
, 1, cgv
);
806 rbase
= isRgb
? 8 : 0;
808 bbase
= isRgb
? 0 : 8;
809 ALLOC_YUV_TABLE(1024 * 3 * 2);
810 y_table16
= c
->yuvTable
;
811 yb
= -(384 << 16) - oy
;
812 for (i
= 0; i
< 1024; i
++) {
813 uint8_t yval
= av_clip_uint8((yb
+ 0x8000) >> 16);
814 y_table16
[i
] = (yval
>> 4) << rbase
;
815 y_table16
[i
+ 1024] = (yval
>> 4) << gbase
;
816 y_table16
[i
+ 2048] = (yval
>> 4) << bbase
;
820 for (i
= 0; i
< 1024 * 3; i
++)
821 y_table16
[i
] = av_bswap16(y_table16
[i
]);
822 fill_table(c
->table_rV
, 2, crv
, y_table16
+ yoffs
);
823 fill_table(c
->table_gU
, 2, cgu
, y_table16
+ yoffs
+ 1024);
824 fill_table(c
->table_bU
, 2, cbu
, y_table16
+ yoffs
+ 2048);
825 fill_gv_table(c
->table_gV
, 2, cgv
);
829 rbase
= isRgb
? bpp
- 5 : 0;
831 bbase
= isRgb
? 0 : (bpp
- 5);
832 ALLOC_YUV_TABLE(1024 * 3 * 2);
833 y_table16
= c
->yuvTable
;
834 yb
= -(384 << 16) - oy
;
835 for (i
= 0; i
< 1024; i
++) {
836 uint8_t yval
= av_clip_uint8((yb
+ 0x8000) >> 16);
837 y_table16
[i
] = (yval
>> 3) << rbase
;
838 y_table16
[i
+ 1024] = (yval
>> (18 - bpp
)) << gbase
;
839 y_table16
[i
+ 2048] = (yval
>> 3) << bbase
;
843 for (i
= 0; i
< 1024 * 3; i
++)
844 y_table16
[i
] = av_bswap16(y_table16
[i
]);
845 fill_table(c
->table_rV
, 2, crv
, y_table16
+ yoffs
);
846 fill_table(c
->table_gU
, 2, cgu
, y_table16
+ yoffs
+ 1024);
847 fill_table(c
->table_bU
, 2, cbu
, y_table16
+ yoffs
+ 2048);
848 fill_gv_table(c
->table_gV
, 2, cgv
);
852 ALLOC_YUV_TABLE(1024);
853 y_table
= c
->yuvTable
;
854 yb
= -(384 << 16) - oy
;
855 for (i
= 0; i
< 1024; i
++) {
856 y_table
[i
] = av_clip_uint8((yb
+ 0x8000) >> 16);
859 fill_table(c
->table_rV
, 1, crv
, y_table
+ yoffs
);
860 fill_table(c
->table_gU
, 1, cgu
, y_table
+ yoffs
);
861 fill_table(c
->table_bU
, 1, cbu
, y_table
+ yoffs
);
862 fill_gv_table(c
->table_gV
, 1, cgv
);
865 base
= (c
->dstFormat
== AV_PIX_FMT_RGB32_1
||
866 c
->dstFormat
== AV_PIX_FMT_BGR32_1
) ? 8 : 0;
867 rbase
= base
+ (isRgb
? 16 : 0);
869 bbase
= base
+ (isRgb
? 0 : 16);
870 needAlpha
= CONFIG_SWSCALE_ALPHA
&& isALPHA(c
->srcFormat
);
872 abase
= (base
+ 24) & 31;
873 ALLOC_YUV_TABLE(1024 * 3 * 4);
874 y_table32
= c
->yuvTable
;
875 yb
= -(384 << 16) - oy
;
876 for (i
= 0; i
< 1024; i
++) {
877 unsigned yval
= av_clip_uint8((yb
+ 0x8000) >> 16);
878 y_table32
[i
] = (yval
<< rbase
) +
879 (needAlpha
? 0 : (255u << abase
));
880 y_table32
[i
+ 1024] = yval
<< gbase
;
881 y_table32
[i
+ 2048] = yval
<< bbase
;
884 fill_table(c
->table_rV
, 4, crv
, y_table32
+ yoffs
);
885 fill_table(c
->table_gU
, 4, cgu
, y_table32
+ yoffs
+ 1024);
886 fill_table(c
->table_bU
, 4, cbu
, y_table32
+ yoffs
+ 2048);
887 fill_gv_table(c
->table_gV
, 4, cgv
);
891 if(!isPlanar(c
->dstFormat
) || bpp
<= 24)
892 av_log(c
, AV_LOG_ERROR
, "%ibpp not supported by yuv2rgb\n", bpp
);