2 * Copyright (C) 2001-2012 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/attributes.h"
28 #include "libavutil/avutil.h"
29 #include "libavutil/avassert.h"
30 #include "libavutil/bswap.h"
31 #include "libavutil/cpu.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/pixdesc.h"
38 #include "swscale_internal.h"
40 DECLARE_ALIGNED(8, const uint8_t, dither_2x2_4)[][8]={
41 { 1, 3, 1, 3, 1, 3, 1, 3, },
42 { 2, 0, 2, 0, 2, 0, 2, 0, },
43 { 1, 3, 1, 3, 1, 3, 1, 3, },
46 DECLARE_ALIGNED(8, const uint8_t, dither_2x2_8)[][8]={
47 { 6, 2, 6, 2, 6, 2, 6, 2, },
48 { 0, 4, 0, 4, 0, 4, 0, 4, },
49 { 6, 2, 6, 2, 6, 2, 6, 2, },
52 DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[][8]={
53 { 8, 4, 11, 7, 8, 4, 11, 7, },
54 { 2, 14, 1, 13, 2, 14, 1, 13, },
55 { 10, 6, 9, 5, 10, 6, 9, 5, },
56 { 0, 12, 3, 15, 0, 12, 3, 15, },
57 { 8, 4, 11, 7, 8, 4, 11, 7, },
60 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[][8]={
61 { 17, 9, 23, 15, 16, 8, 22, 14, },
62 { 5, 29, 3, 27, 4, 28, 2, 26, },
63 { 21, 13, 19, 11, 20, 12, 18, 10, },
64 { 0, 24, 6, 30, 1, 25, 7, 31, },
65 { 16, 8, 22, 14, 17, 9, 23, 15, },
66 { 4, 28, 2, 26, 5, 29, 3, 27, },
67 { 20, 12, 18, 10, 21, 13, 19, 11, },
68 { 1, 25, 7, 31, 0, 24, 6, 30, },
69 { 17, 9, 23, 15, 16, 8, 22, 14, },
72 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[][8]={
73 { 0, 55, 14, 68, 3, 58, 17, 72, },
74 { 37, 18, 50, 32, 40, 22, 54, 35, },
75 { 9, 64, 5, 59, 13, 67, 8, 63, },
76 { 46, 27, 41, 23, 49, 31, 44, 26, },
77 { 2, 57, 16, 71, 1, 56, 15, 70, },
78 { 39, 21, 52, 34, 38, 19, 51, 33, },
79 { 11, 66, 7, 62, 10, 65, 6, 60, },
80 { 48, 30, 43, 25, 47, 29, 42, 24, },
81 { 0, 55, 14, 68, 3, 58, 17, 72, },
85 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[][8]={
86 {117, 62, 158, 103, 113, 58, 155, 100, },
87 { 34, 199, 21, 186, 31, 196, 17, 182, },
88 {144, 89, 131, 76, 141, 86, 127, 72, },
89 { 0, 165, 41, 206, 10, 175, 52, 217, },
90 {110, 55, 151, 96, 120, 65, 162, 107, },
91 { 28, 193, 14, 179, 38, 203, 24, 189, },
92 {138, 83, 124, 69, 148, 93, 134, 79, },
93 { 7, 172, 48, 213, 3, 168, 45, 210, },
94 {117, 62, 158, 103, 113, 58, 155, 100, },
97 // tries to correct a gamma of 1.5
98 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[][8]={
99 { 0, 143, 18, 200, 2, 156, 25, 215, },
100 { 78, 28, 125, 64, 89, 36, 138, 74, },
101 { 10, 180, 3, 161, 16, 195, 8, 175, },
102 {109, 51, 93, 38, 121, 60, 105, 47, },
103 { 1, 152, 23, 210, 0, 147, 20, 205, },
104 { 85, 33, 134, 71, 81, 30, 130, 67, },
105 { 14, 190, 6, 171, 12, 185, 5, 166, },
106 {117, 57, 101, 44, 113, 54, 97, 41, },
107 { 0, 143, 18, 200, 2, 156, 25, 215, },
110 // tries to correct a gamma of 2.0
111 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[][8]={
112 { 0, 124, 8, 193, 0, 140, 12, 213, },
113 { 55, 14, 104, 42, 66, 19, 119, 52, },
114 { 3, 168, 1, 145, 6, 187, 3, 162, },
115 { 86, 31, 70, 21, 99, 39, 82, 28, },
116 { 0, 134, 11, 206, 0, 129, 9, 200, },
117 { 62, 17, 114, 48, 58, 16, 109, 45, },
118 { 5, 181, 2, 157, 4, 175, 1, 151, },
119 { 95, 36, 78, 26, 90, 34, 74, 24, },
120 { 0, 124, 8, 193, 0, 140, 12, 213, },
123 // tries to correct a gamma of 2.5
124 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[][8]={
125 { 0, 107, 3, 187, 0, 125, 6, 212, },
126 { 39, 7, 86, 28, 49, 11, 102, 36, },
127 { 1, 158, 0, 131, 3, 180, 1, 151, },
128 { 68, 19, 52, 12, 81, 25, 64, 17, },
129 { 0, 119, 5, 203, 0, 113, 4, 195, },
130 { 45, 9, 96, 33, 42, 8, 91, 30, },
131 { 2, 172, 1, 144, 2, 165, 0, 137, },
132 { 77, 23, 60, 15, 72, 21, 56, 14, },
133 { 0, 107, 3, 187, 0, 125, 6, 212, },
137 #define output_pixel(pos, val, bias, signedness) \
139 AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
141 AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
144 static av_always_inline void
145 yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
146 int big_endian, int output_bits)
150 av_assert0(output_bits == 16);
152 for (i = 0; i < dstW; i++) {
153 int val = src[i] + (1 << (shift - 1));
154 output_pixel(&dest[i], val, 0, uint);
158 static av_always_inline void
159 yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
160 const int32_t **src, uint16_t *dest, int dstW,
161 int big_endian, int output_bits)
165 av_assert0(output_bits == 16);
167 for (i = 0; i < dstW; i++) {
168 int val = 1 << (shift - 1);
171 /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
172 * filters (or anything with negative coeffs, the range can be slightly
173 * wider in both directions. To account for this overflow, we subtract
174 * a constant so it always fits in the signed range (assuming a
175 * reasonable filterSize), and re-add that at the end. */
177 for (j = 0; j < filterSize; j++)
178 val += src[j][i] * (unsigned)filter[j];
180 output_pixel(&dest[i], val, 0x8000, int);
186 #define output_pixel(pos, val) \
188 AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
190 AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
193 static av_always_inline void
194 yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
195 int big_endian, int output_bits)
198 int shift = 15 - output_bits;
200 for (i = 0; i < dstW; i++) {
201 int val = src[i] + (1 << (shift - 1));
202 output_pixel(&dest[i], val);
206 static av_always_inline void
207 yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
208 const int16_t **src, uint16_t *dest, int dstW,
209 int big_endian, int output_bits)
212 int shift = 11 + 16 - output_bits;
214 for (i = 0; i < dstW; i++) {
215 int val = 1 << (shift - 1);
218 for (j = 0; j < filterSize; j++)
219 val += src[j][i] * filter[j];
221 output_pixel(&dest[i], val);
227 #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
228 static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
229 uint8_t *dest, int dstW, \
230 const uint8_t *dither, int offset)\
232 yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
233 (uint16_t *) dest, dstW, is_be, bits); \
235 static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
236 const int16_t **src, uint8_t *dest, int dstW, \
237 const uint8_t *dither, int offset)\
239 yuv2planeX_## template_size ## _c_template(filter, \
240 filterSize, (const typeX_t **) src, \
241 (uint16_t *) dest, dstW, is_be, bits); \
243 yuv2NBPS( 9, BE, 1, 10, int16_t)
244 yuv2NBPS( 9, LE, 0, 10, int16_t)
245 yuv2NBPS(10, BE, 1, 10, int16_t)
246 yuv2NBPS(10, LE, 0, 10, int16_t)
247 yuv2NBPS(12, BE, 1, 10, int16_t)
248 yuv2NBPS(12, LE, 0, 10, int16_t)
249 yuv2NBPS(14, BE, 1, 10, int16_t)
250 yuv2NBPS(14, LE, 0, 10, int16_t)
251 yuv2NBPS(16, BE, 1, 16, int32_t)
252 yuv2NBPS(16, LE, 0, 16, int32_t)
254 static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
255 const int16_t **src, uint8_t *dest, int dstW,
256 const uint8_t *dither, int offset)
259 for (i=0; i<dstW; i++) {
260 int val = dither[(i + offset) & 7] << 12;
262 for (j=0; j<filterSize; j++)
263 val += src[j][i] * filter[j];
265 dest[i]= av_clip_uint8(val>>19);
269 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
270 const uint8_t *dither, int offset)
273 for (i=0; i<dstW; i++) {
274 int val = (src[i] + dither[(i + offset) & 7]) >> 7;
275 dest[i]= av_clip_uint8(val);
279 static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
280 const int16_t **chrUSrc, const int16_t **chrVSrc,
281 uint8_t *dest, int chrDstW)
283 enum AVPixelFormat dstFormat = c->dstFormat;
284 const uint8_t *chrDither = c->chrDither8;
287 if (dstFormat == AV_PIX_FMT_NV12)
288 for (i=0; i<chrDstW; i++) {
289 int u = chrDither[i & 7] << 12;
290 int v = chrDither[(i + 3) & 7] << 12;
292 for (j=0; j<chrFilterSize; j++) {
293 u += chrUSrc[j][i] * chrFilter[j];
294 v += chrVSrc[j][i] * chrFilter[j];
297 dest[2*i]= av_clip_uint8(u>>19);
298 dest[2*i+1]= av_clip_uint8(v>>19);
301 for (i=0; i<chrDstW; i++) {
302 int u = chrDither[i & 7] << 12;
303 int v = chrDither[(i + 3) & 7] << 12;
305 for (j=0; j<chrFilterSize; j++) {
306 u += chrUSrc[j][i] * chrFilter[j];
307 v += chrVSrc[j][i] * chrFilter[j];
310 dest[2*i]= av_clip_uint8(v>>19);
311 dest[2*i+1]= av_clip_uint8(u>>19);
315 #define accumulate_bit(acc, val) \
317 acc |= (val) >= (128 + 110)
318 #define output_pixel(pos, acc) \
319 if (target == AV_PIX_FMT_MONOBLACK) { \
325 static av_always_inline void
326 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
327 const int16_t **lumSrc, int lumFilterSize,
328 const int16_t *chrFilter, const int16_t **chrUSrc,
329 const int16_t **chrVSrc, int chrFilterSize,
330 const int16_t **alpSrc, uint8_t *dest, int dstW,
331 int y, enum AVPixelFormat target)
333 const uint8_t * const d128=dither_8x8_220[y&7];
337 for (i = 0; i < dstW; i += 2) {
342 for (j = 0; j < lumFilterSize; j++) {
343 Y1 += lumSrc[j][i] * lumFilter[j];
344 Y2 += lumSrc[j][i+1] * lumFilter[j];
348 if ((Y1 | Y2) & 0x100) {
349 Y1 = av_clip_uint8(Y1);
350 Y2 = av_clip_uint8(Y2);
352 accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
353 accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
355 output_pixel(*dest++, acc);
360 output_pixel(*dest, acc);
364 static av_always_inline void
365 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
366 const int16_t *ubuf[2], const int16_t *vbuf[2],
367 const int16_t *abuf[2], uint8_t *dest, int dstW,
368 int yalpha, int uvalpha, int y,
369 enum AVPixelFormat target)
371 const int16_t *buf0 = buf[0], *buf1 = buf[1];
372 const uint8_t * const d128 = dither_8x8_220[y & 7];
373 int yalpha1 = 4096 - yalpha;
376 for (i = 0; i < dstW; i += 8) {
379 Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
380 accumulate_bit(acc, Y + d128[0]);
381 Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
382 accumulate_bit(acc, Y + d128[1]);
383 Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
384 accumulate_bit(acc, Y + d128[2]);
385 Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
386 accumulate_bit(acc, Y + d128[3]);
387 Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
388 accumulate_bit(acc, Y + d128[4]);
389 Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
390 accumulate_bit(acc, Y + d128[5]);
391 Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
392 accumulate_bit(acc, Y + d128[6]);
393 Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
394 accumulate_bit(acc, Y + d128[7]);
396 output_pixel(*dest++, acc);
400 static av_always_inline void
401 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
402 const int16_t *ubuf[2], const int16_t *vbuf[2],
403 const int16_t *abuf0, uint8_t *dest, int dstW,
404 int uvalpha, int y, enum AVPixelFormat target)
406 const uint8_t * const d128 = dither_8x8_220[y & 7];
409 for (i = 0; i < dstW; i += 8) {
412 accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
413 accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
414 accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
415 accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
416 accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
417 accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
418 accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
419 accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
421 output_pixel(*dest++, acc);
426 #undef accumulate_bit
428 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
429 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
430 const int16_t **lumSrc, int lumFilterSize, \
431 const int16_t *chrFilter, const int16_t **chrUSrc, \
432 const int16_t **chrVSrc, int chrFilterSize, \
433 const int16_t **alpSrc, uint8_t *dest, int dstW, \
436 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
437 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
438 alpSrc, dest, dstW, y, fmt); \
441 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
442 const int16_t *ubuf[2], const int16_t *vbuf[2], \
443 const int16_t *abuf[2], uint8_t *dest, int dstW, \
444 int yalpha, int uvalpha, int y) \
446 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
447 dest, dstW, yalpha, uvalpha, y, fmt); \
450 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
451 const int16_t *ubuf[2], const int16_t *vbuf[2], \
452 const int16_t *abuf0, uint8_t *dest, int dstW, \
453 int uvalpha, int y) \
455 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
456 abuf0, dest, dstW, uvalpha, \
460 YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
461 YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
463 #define output_pixels(pos, Y1, U, Y2, V) \
464 if (target == AV_PIX_FMT_YUYV422) { \
465 dest[pos + 0] = Y1; \
467 dest[pos + 2] = Y2; \
471 dest[pos + 1] = Y1; \
473 dest[pos + 3] = Y2; \
476 static av_always_inline void
477 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
478 const int16_t **lumSrc, int lumFilterSize,
479 const int16_t *chrFilter, const int16_t **chrUSrc,
480 const int16_t **chrVSrc, int chrFilterSize,
481 const int16_t **alpSrc, uint8_t *dest, int dstW,
482 int y, enum AVPixelFormat target)
486 for (i = 0; i < ((dstW + 1) >> 1); i++) {
493 for (j = 0; j < lumFilterSize; j++) {
494 Y1 += lumSrc[j][i * 2] * lumFilter[j];
495 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
497 for (j = 0; j < chrFilterSize; j++) {
498 U += chrUSrc[j][i] * chrFilter[j];
499 V += chrVSrc[j][i] * chrFilter[j];
505 if ((Y1 | Y2 | U | V) & 0x100) {
506 Y1 = av_clip_uint8(Y1);
507 Y2 = av_clip_uint8(Y2);
508 U = av_clip_uint8(U);
509 V = av_clip_uint8(V);
511 output_pixels(4*i, Y1, U, Y2, V);
515 static av_always_inline void
516 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
517 const int16_t *ubuf[2], const int16_t *vbuf[2],
518 const int16_t *abuf[2], uint8_t *dest, int dstW,
519 int yalpha, int uvalpha, int y,
520 enum AVPixelFormat target)
522 const int16_t *buf0 = buf[0], *buf1 = buf[1],
523 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
524 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
525 int yalpha1 = 4096 - yalpha;
526 int uvalpha1 = 4096 - uvalpha;
529 for (i = 0; i < ((dstW + 1) >> 1); i++) {
530 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
531 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
532 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
533 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
535 if ((Y1 | Y2 | U | V) & 0x100) {
536 Y1 = av_clip_uint8(Y1);
537 Y2 = av_clip_uint8(Y2);
538 U = av_clip_uint8(U);
539 V = av_clip_uint8(V);
542 output_pixels(i * 4, Y1, U, Y2, V);
546 static av_always_inline void
547 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
548 const int16_t *ubuf[2], const int16_t *vbuf[2],
549 const int16_t *abuf0, uint8_t *dest, int dstW,
550 int uvalpha, int y, enum AVPixelFormat target)
552 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
555 if (uvalpha < 2048) {
556 for (i = 0; i < ((dstW + 1) >> 1); i++) {
557 int Y1 = (buf0[i * 2 ]+64) >> 7;
558 int Y2 = (buf0[i * 2 + 1]+64) >> 7;
559 int U = (ubuf0[i] +64) >> 7;
560 int V = (vbuf0[i] +64) >> 7;
562 if ((Y1 | Y2 | U | V) & 0x100) {
563 Y1 = av_clip_uint8(Y1);
564 Y2 = av_clip_uint8(Y2);
565 U = av_clip_uint8(U);
566 V = av_clip_uint8(V);
569 Y1 = av_clip_uint8(Y1);
570 Y2 = av_clip_uint8(Y2);
571 U = av_clip_uint8(U);
572 V = av_clip_uint8(V);
574 output_pixels(i * 4, Y1, U, Y2, V);
577 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
578 for (i = 0; i < ((dstW + 1) >> 1); i++) {
579 int Y1 = (buf0[i * 2 ] + 64) >> 7;
580 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
581 int U = (ubuf0[i] + ubuf1[i]+128) >> 8;
582 int V = (vbuf0[i] + vbuf1[i]+128) >> 8;
584 if ((Y1 | Y2 | U | V) & 0x100) {
585 Y1 = av_clip_uint8(Y1);
586 Y2 = av_clip_uint8(Y2);
587 U = av_clip_uint8(U);
588 V = av_clip_uint8(V);
591 Y1 = av_clip_uint8(Y1);
592 Y2 = av_clip_uint8(Y2);
593 U = av_clip_uint8(U);
594 V = av_clip_uint8(V);
596 output_pixels(i * 4, Y1, U, Y2, V);
603 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
604 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
606 #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? R : B)
607 #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? B : R)
608 #define output_pixel(pos, val) \
609 if (isBE(target)) { \
615 static av_always_inline void
616 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
617 const int32_t **lumSrc, int lumFilterSize,
618 const int16_t *chrFilter, const int32_t **chrUSrc,
619 const int32_t **chrVSrc, int chrFilterSize,
620 const int32_t **alpSrc, uint16_t *dest, int dstW,
621 int y, enum AVPixelFormat target)
625 for (i = 0; i < ((dstW + 1) >> 1); i++) {
627 int Y1 = -0x40000000;
628 int Y2 = -0x40000000;
629 int U = -128 << 23; // 19
633 for (j = 0; j < lumFilterSize; j++) {
634 Y1 += lumSrc[j][i * 2] * (unsigned)lumFilter[j];
635 Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
637 for (j = 0; j < chrFilterSize; j++) {;
638 U += chrUSrc[j][i] * (unsigned)chrFilter[j];
639 V += chrVSrc[j][i] * (unsigned)chrFilter[j];
642 // 8bit: 12+15=27; 16-bit: 12+19=31
650 // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
651 Y1 -= c->yuv2rgb_y_offset;
652 Y2 -= c->yuv2rgb_y_offset;
653 Y1 *= c->yuv2rgb_y_coeff;
654 Y2 *= c->yuv2rgb_y_coeff;
657 // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
659 R = V * c->yuv2rgb_v2r_coeff;
660 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
661 B = U * c->yuv2rgb_u2b_coeff;
663 // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
664 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
665 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
666 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
667 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
668 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
669 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
674 static av_always_inline void
675 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
676 const int32_t *ubuf[2], const int32_t *vbuf[2],
677 const int32_t *abuf[2], uint16_t *dest, int dstW,
678 int yalpha, int uvalpha, int y,
679 enum AVPixelFormat target)
681 const int32_t *buf0 = buf[0], *buf1 = buf[1],
682 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
683 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
684 int yalpha1 = 4096 - yalpha;
685 int uvalpha1 = 4096 - uvalpha;
688 for (i = 0; i < ((dstW + 1) >> 1); i++) {
689 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
690 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
691 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
692 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
695 Y1 -= c->yuv2rgb_y_offset;
696 Y2 -= c->yuv2rgb_y_offset;
697 Y1 *= c->yuv2rgb_y_coeff;
698 Y2 *= c->yuv2rgb_y_coeff;
702 R = V * c->yuv2rgb_v2r_coeff;
703 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
704 B = U * c->yuv2rgb_u2b_coeff;
706 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
707 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
708 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
709 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
710 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
711 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
716 static av_always_inline void
717 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
718 const int32_t *ubuf[2], const int32_t *vbuf[2],
719 const int32_t *abuf0, uint16_t *dest, int dstW,
720 int uvalpha, int y, enum AVPixelFormat target)
722 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
725 if (uvalpha < 2048) {
726 for (i = 0; i < ((dstW + 1) >> 1); i++) {
727 int Y1 = (buf0[i * 2] ) >> 2;
728 int Y2 = (buf0[i * 2 + 1]) >> 2;
729 int U = (ubuf0[i] + (-128 << 11)) >> 2;
730 int V = (vbuf0[i] + (-128 << 11)) >> 2;
733 Y1 -= c->yuv2rgb_y_offset;
734 Y2 -= c->yuv2rgb_y_offset;
735 Y1 *= c->yuv2rgb_y_coeff;
736 Y2 *= c->yuv2rgb_y_coeff;
740 R = V * c->yuv2rgb_v2r_coeff;
741 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
742 B = U * c->yuv2rgb_u2b_coeff;
744 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
745 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
746 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
747 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
748 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
749 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
753 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
754 for (i = 0; i < ((dstW + 1) >> 1); i++) {
755 int Y1 = (buf0[i * 2] ) >> 2;
756 int Y2 = (buf0[i * 2 + 1]) >> 2;
757 int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
758 int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
761 Y1 -= c->yuv2rgb_y_offset;
762 Y2 -= c->yuv2rgb_y_offset;
763 Y1 *= c->yuv2rgb_y_coeff;
764 Y2 *= c->yuv2rgb_y_coeff;
768 R = V * c->yuv2rgb_v2r_coeff;
769 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
770 B = U * c->yuv2rgb_u2b_coeff;
772 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
773 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
774 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
775 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
776 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
777 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
787 #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
788 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
789 const int16_t **_lumSrc, int lumFilterSize, \
790 const int16_t *chrFilter, const int16_t **_chrUSrc, \
791 const int16_t **_chrVSrc, int chrFilterSize, \
792 const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
795 const int32_t **lumSrc = (const int32_t **) _lumSrc, \
796 **chrUSrc = (const int32_t **) _chrUSrc, \
797 **chrVSrc = (const int32_t **) _chrVSrc, \
798 **alpSrc = (const int32_t **) _alpSrc; \
799 uint16_t *dest = (uint16_t *) _dest; \
800 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
801 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
802 alpSrc, dest, dstW, y, fmt); \
805 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
806 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
807 const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
808 int yalpha, int uvalpha, int y) \
810 const int32_t **buf = (const int32_t **) _buf, \
811 **ubuf = (const int32_t **) _ubuf, \
812 **vbuf = (const int32_t **) _vbuf, \
813 **abuf = (const int32_t **) _abuf; \
814 uint16_t *dest = (uint16_t *) _dest; \
815 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
816 dest, dstW, yalpha, uvalpha, y, fmt); \
819 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
820 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
821 const int16_t *_abuf0, uint8_t *_dest, int dstW, \
822 int uvalpha, int y) \
824 const int32_t *buf0 = (const int32_t *) _buf0, \
825 **ubuf = (const int32_t **) _ubuf, \
826 **vbuf = (const int32_t **) _vbuf, \
827 *abuf0 = (const int32_t *) _abuf0; \
828 uint16_t *dest = (uint16_t *) _dest; \
829 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
830 dstW, uvalpha, y, fmt); \
833 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, AV_PIX_FMT_RGB48BE)
834 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, AV_PIX_FMT_RGB48LE)
835 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, AV_PIX_FMT_BGR48BE)
836 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, AV_PIX_FMT_BGR48LE)
839 * Write out 2 RGB pixels in the target pixel format. This function takes a
840 * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
841 * things like endianness conversion and shifting. The caller takes care of
842 * setting the correct offset in these tables from the chroma (U/V) values.
843 * This function then uses the luminance (Y1/Y2) values to write out the
844 * correct RGB values into the destination buffer.
846 static av_always_inline void
847 yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
848 unsigned A1, unsigned A2,
849 const void *_r, const void *_g, const void *_b, int y,
850 enum AVPixelFormat target, int hasAlpha)
852 if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
853 target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
854 uint32_t *dest = (uint32_t *) _dest;
855 const uint32_t *r = (const uint32_t *) _r;
856 const uint32_t *g = (const uint32_t *) _g;
857 const uint32_t *b = (const uint32_t *) _b;
860 int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
862 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
863 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
866 int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
868 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
869 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
871 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
872 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
875 } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
876 uint8_t *dest = (uint8_t *) _dest;
877 const uint8_t *r = (const uint8_t *) _r;
878 const uint8_t *g = (const uint8_t *) _g;
879 const uint8_t *b = (const uint8_t *) _b;
881 #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
882 #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
884 dest[i * 6 + 0] = r_b[Y1];
885 dest[i * 6 + 1] = g[Y1];
886 dest[i * 6 + 2] = b_r[Y1];
887 dest[i * 6 + 3] = r_b[Y2];
888 dest[i * 6 + 4] = g[Y2];
889 dest[i * 6 + 5] = b_r[Y2];
892 } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
893 target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
894 target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
895 uint16_t *dest = (uint16_t *) _dest;
896 const uint16_t *r = (const uint16_t *) _r;
897 const uint16_t *g = (const uint16_t *) _g;
898 const uint16_t *b = (const uint16_t *) _b;
899 int dr1, dg1, db1, dr2, dg2, db2;
901 if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
902 dr1 = dither_2x2_8[ y & 1 ][0];
903 dg1 = dither_2x2_4[ y & 1 ][0];
904 db1 = dither_2x2_8[(y & 1) ^ 1][0];
905 dr2 = dither_2x2_8[ y & 1 ][1];
906 dg2 = dither_2x2_4[ y & 1 ][1];
907 db2 = dither_2x2_8[(y & 1) ^ 1][1];
908 } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
909 dr1 = dither_2x2_8[ y & 1 ][0];
910 dg1 = dither_2x2_8[ y & 1 ][1];
911 db1 = dither_2x2_8[(y & 1) ^ 1][0];
912 dr2 = dither_2x2_8[ y & 1 ][1];
913 dg2 = dither_2x2_8[ y & 1 ][0];
914 db2 = dither_2x2_8[(y & 1) ^ 1][1];
916 dr1 = dither_4x4_16[ y & 3 ][0];
917 dg1 = dither_4x4_16[ y & 3 ][1];
918 db1 = dither_4x4_16[(y & 3) ^ 3][0];
919 dr2 = dither_4x4_16[ y & 3 ][1];
920 dg2 = dither_4x4_16[ y & 3 ][0];
921 db2 = dither_4x4_16[(y & 3) ^ 3][1];
924 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
925 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
926 } else /* 8/4-bit */ {
927 uint8_t *dest = (uint8_t *) _dest;
928 const uint8_t *r = (const uint8_t *) _r;
929 const uint8_t *g = (const uint8_t *) _g;
930 const uint8_t *b = (const uint8_t *) _b;
931 int dr1, dg1, db1, dr2, dg2, db2;
933 if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
934 const uint8_t * const d64 = dither_8x8_73[y & 7];
935 const uint8_t * const d32 = dither_8x8_32[y & 7];
936 dr1 = dg1 = d32[(i * 2 + 0) & 7];
937 db1 = d64[(i * 2 + 0) & 7];
938 dr2 = dg2 = d32[(i * 2 + 1) & 7];
939 db2 = d64[(i * 2 + 1) & 7];
941 const uint8_t * const d64 = dither_8x8_73 [y & 7];
942 const uint8_t * const d128 = dither_8x8_220[y & 7];
943 dr1 = db1 = d128[(i * 2 + 0) & 7];
944 dg1 = d64[(i * 2 + 0) & 7];
945 dr2 = db2 = d128[(i * 2 + 1) & 7];
946 dg2 = d64[(i * 2 + 1) & 7];
949 if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
950 dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
951 ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
953 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
954 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
959 static av_always_inline void
960 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
961 const int16_t **lumSrc, int lumFilterSize,
962 const int16_t *chrFilter, const int16_t **chrUSrc,
963 const int16_t **chrVSrc, int chrFilterSize,
964 const int16_t **alpSrc, uint8_t *dest, int dstW,
965 int y, enum AVPixelFormat target, int hasAlpha)
969 for (i = 0; i < ((dstW + 1) >> 1); i++) {
975 const void *r, *g, *b;
977 for (j = 0; j < lumFilterSize; j++) {
978 Y1 += lumSrc[j][i * 2] * lumFilter[j];
979 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
981 for (j = 0; j < chrFilterSize; j++) {
982 U += chrUSrc[j][i] * chrFilter[j];
983 V += chrVSrc[j][i] * chrFilter[j];
992 for (j = 0; j < lumFilterSize; j++) {
993 A1 += alpSrc[j][i * 2 ] * lumFilter[j];
994 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
998 if ((A1 | A2) & 0x100) {
999 A1 = av_clip_uint8(A1);
1000 A2 = av_clip_uint8(A2);
1004 r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
1005 g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
1006 b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1008 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1009 r, g, b, y, target, hasAlpha);
1013 static av_always_inline void
1014 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
1015 const int16_t *ubuf[2], const int16_t *vbuf[2],
1016 const int16_t *abuf[2], uint8_t *dest, int dstW,
1017 int yalpha, int uvalpha, int y,
1018 enum AVPixelFormat target, int hasAlpha)
1020 const int16_t *buf0 = buf[0], *buf1 = buf[1],
1021 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1022 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1023 *abuf0 = hasAlpha ? abuf[0] : NULL,
1024 *abuf1 = hasAlpha ? abuf[1] : NULL;
1025 int yalpha1 = 4096 - yalpha;
1026 int uvalpha1 = 4096 - uvalpha;
1029 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1030 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
1031 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
1032 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
1033 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
1035 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1036 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1037 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1040 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
1041 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1042 A1 = av_clip_uint8(A1);
1043 A2 = av_clip_uint8(A2);
1046 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1047 r, g, b, y, target, hasAlpha);
1051 static av_always_inline void
1052 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1053 const int16_t *ubuf[2], const int16_t *vbuf[2],
1054 const int16_t *abuf0, uint8_t *dest, int dstW,
1055 int uvalpha, int y, enum AVPixelFormat target,
1058 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1061 if (uvalpha < 2048) {
1062 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1063 int Y1 = (buf0[i * 2 ] + 64) >> 7;
1064 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1065 int U = (ubuf0[i] + 64) >> 7;
1066 int V = (vbuf0[i] + 64) >> 7;
1068 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1069 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1070 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1073 A1 = abuf0[i * 2 ] * 255 + 16384 >> 15;
1074 A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
1075 A1 = av_clip_uint8(A1);
1076 A2 = av_clip_uint8(A2);
1079 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1080 r, g, b, y, target, hasAlpha);
1083 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1084 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1085 int Y1 = (buf0[i * 2 ] + 64) >> 7;
1086 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1087 int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
1088 int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
1090 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1091 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1092 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1095 A1 = (abuf0[i * 2 ] + 64) >> 7;
1096 A2 = (abuf0[i * 2 + 1] + 64) >> 7;
1097 A1 = av_clip_uint8(A1);
1098 A2 = av_clip_uint8(A2);
1101 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1102 r, g, b, y, target, hasAlpha);
1107 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1108 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1109 const int16_t **lumSrc, int lumFilterSize, \
1110 const int16_t *chrFilter, const int16_t **chrUSrc, \
1111 const int16_t **chrVSrc, int chrFilterSize, \
1112 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1115 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1116 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1117 alpSrc, dest, dstW, y, fmt, hasAlpha); \
1119 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1120 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1121 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1122 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1123 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1124 int yalpha, int uvalpha, int y) \
1126 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1127 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1130 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1131 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1132 const int16_t *abuf0, uint8_t *dest, int dstW, \
1133 int uvalpha, int y) \
1135 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1136 dstW, uvalpha, y, fmt, hasAlpha); \
1140 YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1141 YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1143 #if CONFIG_SWSCALE_ALPHA
1144 YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
1145 YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
1147 YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
1148 YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
1150 YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
1151 YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
1152 YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
1153 YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
1154 YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
1155 YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
1156 YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
1157 YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
1159 static av_always_inline void
1160 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1161 const int16_t **lumSrc, int lumFilterSize,
1162 const int16_t *chrFilter, const int16_t **chrUSrc,
1163 const int16_t **chrVSrc, int chrFilterSize,
1164 const int16_t **alpSrc, uint8_t *dest,
1165 int dstW, int y, enum AVPixelFormat target, int hasAlpha)
1168 int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1170 for (i = 0; i < dstW; i++) {
1173 int U = (1<<9)-(128 << 19);
1174 int V = (1<<9)-(128 << 19);
1177 for (j = 0; j < lumFilterSize; j++) {
1178 Y += lumSrc[j][i] * lumFilter[j];
1180 for (j = 0; j < chrFilterSize; j++) {
1181 U += chrUSrc[j][i] * chrFilter[j];
1182 V += chrVSrc[j][i] * chrFilter[j];
1189 for (j = 0; j < lumFilterSize; j++) {
1190 A += alpSrc[j][i] * lumFilter[j];
1194 A = av_clip_uint8(A);
1196 Y -= c->yuv2rgb_y_offset;
1197 Y *= c->yuv2rgb_y_coeff;
1199 R = Y + V*c->yuv2rgb_v2r_coeff;
1200 G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1201 B = Y + U*c->yuv2rgb_u2b_coeff;
1202 if ((R | G | B) & 0xC0000000) {
1203 R = av_clip_uintp2(R, 30);
1204 G = av_clip_uintp2(G, 30);
1205 B = av_clip_uintp2(B, 30);
1209 case AV_PIX_FMT_ARGB:
1210 dest[0] = hasAlpha ? A : 255;
1215 case AV_PIX_FMT_RGB24:
1220 case AV_PIX_FMT_RGBA:
1224 dest[3] = hasAlpha ? A : 255;
1226 case AV_PIX_FMT_ABGR:
1227 dest[0] = hasAlpha ? A : 255;
1232 case AV_PIX_FMT_BGR24:
1237 case AV_PIX_FMT_BGRA:
1241 dest[3] = hasAlpha ? A : 255;
1249 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1250 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1251 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1252 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1254 #if CONFIG_SWSCALE_ALPHA
1255 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
1256 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
1257 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
1258 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
1260 YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
1261 YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
1262 YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
1263 YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
1265 YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
1266 YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
1268 av_cold void ff_sws_init_output_funcs(SwsContext *c,
1269 yuv2planar1_fn *yuv2plane1,
1270 yuv2planarX_fn *yuv2planeX,
1271 yuv2interleavedX_fn *yuv2nv12cX,
1272 yuv2packed1_fn *yuv2packed1,
1273 yuv2packed2_fn *yuv2packed2,
1274 yuv2packedX_fn *yuv2packedX)
1276 enum AVPixelFormat dstFormat = c->dstFormat;
1277 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
1279 if (is16BPS(dstFormat)) {
1280 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
1281 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
1282 } else if (is9_OR_10BPS(dstFormat)) {
1283 if (desc->comp[0].depth_minus1 == 8) {
1284 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
1285 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
1286 } else if (desc->comp[0].depth_minus1 == 9) {
1287 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
1288 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
1289 } else if (desc->comp[0].depth_minus1 == 11) {
1290 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c : yuv2planeX_12LE_c;
1291 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c : yuv2plane1_12LE_c;
1292 } else if (desc->comp[0].depth_minus1 == 13) {
1293 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c : yuv2planeX_14LE_c;
1294 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c : yuv2plane1_14LE_c;
1298 *yuv2plane1 = yuv2plane1_8_c;
1299 *yuv2planeX = yuv2planeX_8_c;
1300 if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
1301 *yuv2nv12cX = yuv2nv12cX_c;
1304 if(c->flags & SWS_FULL_CHR_H_INT) {
1305 switch (dstFormat) {
1306 case AV_PIX_FMT_RGBA:
1308 *yuv2packedX = yuv2rgba32_full_X_c;
1310 #if CONFIG_SWSCALE_ALPHA
1312 *yuv2packedX = yuv2rgba32_full_X_c;
1314 #endif /* CONFIG_SWSCALE_ALPHA */
1316 *yuv2packedX = yuv2rgbx32_full_X_c;
1318 #endif /* !CONFIG_SMALL */
1320 case AV_PIX_FMT_ARGB:
1322 *yuv2packedX = yuv2argb32_full_X_c;
1324 #if CONFIG_SWSCALE_ALPHA
1326 *yuv2packedX = yuv2argb32_full_X_c;
1328 #endif /* CONFIG_SWSCALE_ALPHA */
1330 *yuv2packedX = yuv2xrgb32_full_X_c;
1332 #endif /* !CONFIG_SMALL */
1334 case AV_PIX_FMT_BGRA:
1336 *yuv2packedX = yuv2bgra32_full_X_c;
1338 #if CONFIG_SWSCALE_ALPHA
1340 *yuv2packedX = yuv2bgra32_full_X_c;
1342 #endif /* CONFIG_SWSCALE_ALPHA */
1344 *yuv2packedX = yuv2bgrx32_full_X_c;
1346 #endif /* !CONFIG_SMALL */
1348 case AV_PIX_FMT_ABGR:
1350 *yuv2packedX = yuv2abgr32_full_X_c;
1352 #if CONFIG_SWSCALE_ALPHA
1354 *yuv2packedX = yuv2abgr32_full_X_c;
1356 #endif /* CONFIG_SWSCALE_ALPHA */
1358 *yuv2packedX = yuv2xbgr32_full_X_c;
1360 #endif /* !CONFIG_SMALL */
1362 case AV_PIX_FMT_RGB24:
1363 *yuv2packedX = yuv2rgb24_full_X_c;
1365 case AV_PIX_FMT_BGR24:
1366 *yuv2packedX = yuv2bgr24_full_X_c;
1373 switch (dstFormat) {
1374 case AV_PIX_FMT_RGB48LE:
1375 *yuv2packed1 = yuv2rgb48le_1_c;
1376 *yuv2packed2 = yuv2rgb48le_2_c;
1377 *yuv2packedX = yuv2rgb48le_X_c;
1379 case AV_PIX_FMT_RGB48BE:
1380 *yuv2packed1 = yuv2rgb48be_1_c;
1381 *yuv2packed2 = yuv2rgb48be_2_c;
1382 *yuv2packedX = yuv2rgb48be_X_c;
1384 case AV_PIX_FMT_BGR48LE:
1385 *yuv2packed1 = yuv2bgr48le_1_c;
1386 *yuv2packed2 = yuv2bgr48le_2_c;
1387 *yuv2packedX = yuv2bgr48le_X_c;
1389 case AV_PIX_FMT_BGR48BE:
1390 *yuv2packed1 = yuv2bgr48be_1_c;
1391 *yuv2packed2 = yuv2bgr48be_2_c;
1392 *yuv2packedX = yuv2bgr48be_X_c;
1394 case AV_PIX_FMT_RGB32:
1395 case AV_PIX_FMT_BGR32:
1397 *yuv2packed1 = yuv2rgb32_1_c;
1398 *yuv2packed2 = yuv2rgb32_2_c;
1399 *yuv2packedX = yuv2rgb32_X_c;
1401 #if CONFIG_SWSCALE_ALPHA
1403 *yuv2packed1 = yuv2rgba32_1_c;
1404 *yuv2packed2 = yuv2rgba32_2_c;
1405 *yuv2packedX = yuv2rgba32_X_c;
1407 #endif /* CONFIG_SWSCALE_ALPHA */
1409 *yuv2packed1 = yuv2rgbx32_1_c;
1410 *yuv2packed2 = yuv2rgbx32_2_c;
1411 *yuv2packedX = yuv2rgbx32_X_c;
1413 #endif /* !CONFIG_SMALL */
1415 case AV_PIX_FMT_RGB32_1:
1416 case AV_PIX_FMT_BGR32_1:
1418 *yuv2packed1 = yuv2rgb32_1_1_c;
1419 *yuv2packed2 = yuv2rgb32_1_2_c;
1420 *yuv2packedX = yuv2rgb32_1_X_c;
1422 #if CONFIG_SWSCALE_ALPHA
1424 *yuv2packed1 = yuv2rgba32_1_1_c;
1425 *yuv2packed2 = yuv2rgba32_1_2_c;
1426 *yuv2packedX = yuv2rgba32_1_X_c;
1428 #endif /* CONFIG_SWSCALE_ALPHA */
1430 *yuv2packed1 = yuv2rgbx32_1_1_c;
1431 *yuv2packed2 = yuv2rgbx32_1_2_c;
1432 *yuv2packedX = yuv2rgbx32_1_X_c;
1434 #endif /* !CONFIG_SMALL */
1436 case AV_PIX_FMT_RGB24:
1437 *yuv2packed1 = yuv2rgb24_1_c;
1438 *yuv2packed2 = yuv2rgb24_2_c;
1439 *yuv2packedX = yuv2rgb24_X_c;
1441 case AV_PIX_FMT_BGR24:
1442 *yuv2packed1 = yuv2bgr24_1_c;
1443 *yuv2packed2 = yuv2bgr24_2_c;
1444 *yuv2packedX = yuv2bgr24_X_c;
1446 case AV_PIX_FMT_RGB565LE:
1447 case AV_PIX_FMT_RGB565BE:
1448 case AV_PIX_FMT_BGR565LE:
1449 case AV_PIX_FMT_BGR565BE:
1450 *yuv2packed1 = yuv2rgb16_1_c;
1451 *yuv2packed2 = yuv2rgb16_2_c;
1452 *yuv2packedX = yuv2rgb16_X_c;
1454 case AV_PIX_FMT_RGB555LE:
1455 case AV_PIX_FMT_RGB555BE:
1456 case AV_PIX_FMT_BGR555LE:
1457 case AV_PIX_FMT_BGR555BE:
1458 *yuv2packed1 = yuv2rgb15_1_c;
1459 *yuv2packed2 = yuv2rgb15_2_c;
1460 *yuv2packedX = yuv2rgb15_X_c;
1462 case AV_PIX_FMT_RGB444LE:
1463 case AV_PIX_FMT_RGB444BE:
1464 case AV_PIX_FMT_BGR444LE:
1465 case AV_PIX_FMT_BGR444BE:
1466 *yuv2packed1 = yuv2rgb12_1_c;
1467 *yuv2packed2 = yuv2rgb12_2_c;
1468 *yuv2packedX = yuv2rgb12_X_c;
1470 case AV_PIX_FMT_RGB8:
1471 case AV_PIX_FMT_BGR8:
1472 *yuv2packed1 = yuv2rgb8_1_c;
1473 *yuv2packed2 = yuv2rgb8_2_c;
1474 *yuv2packedX = yuv2rgb8_X_c;
1476 case AV_PIX_FMT_RGB4:
1477 case AV_PIX_FMT_BGR4:
1478 *yuv2packed1 = yuv2rgb4_1_c;
1479 *yuv2packed2 = yuv2rgb4_2_c;
1480 *yuv2packedX = yuv2rgb4_X_c;
1482 case AV_PIX_FMT_RGB4_BYTE:
1483 case AV_PIX_FMT_BGR4_BYTE:
1484 *yuv2packed1 = yuv2rgb4b_1_c;
1485 *yuv2packed2 = yuv2rgb4b_2_c;
1486 *yuv2packedX = yuv2rgb4b_X_c;
1490 switch (dstFormat) {
1491 case AV_PIX_FMT_MONOWHITE:
1492 *yuv2packed1 = yuv2monowhite_1_c;
1493 *yuv2packed2 = yuv2monowhite_2_c;
1494 *yuv2packedX = yuv2monowhite_X_c;
1496 case AV_PIX_FMT_MONOBLACK:
1497 *yuv2packed1 = yuv2monoblack_1_c;
1498 *yuv2packed2 = yuv2monoblack_2_c;
1499 *yuv2packedX = yuv2monoblack_X_c;
1501 case AV_PIX_FMT_YUYV422:
1502 *yuv2packed1 = yuv2yuyv422_1_c;
1503 *yuv2packed2 = yuv2yuyv422_2_c;
1504 *yuv2packedX = yuv2yuyv422_X_c;
1506 case AV_PIX_FMT_UYVY422:
1507 *yuv2packed1 = yuv2uyvy422_1_c;
1508 *yuv2packed2 = yuv2uyvy422_2_c;
1509 *yuv2packedX = yuv2uyvy422_X_c;