2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/attributes.h"
27 #include "libavutil/avutil.h"
28 #include "libavutil/bswap.h"
29 #include "libavutil/cpu.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
36 #include "swscale_internal.h"
38 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
39 { 1, 3, 1, 3, 1, 3, 1, 3, },
40 { 2, 0, 2, 0, 2, 0, 2, 0, },
43 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
44 { 6, 2, 6, 2, 6, 2, 6, 2, },
45 { 0, 4, 0, 4, 0, 4, 0, 4, },
48 DECLARE_ALIGNED(8, const uint8_t, ff_dither_4x4_16)[4][8] = {
49 { 8, 4, 11, 7, 8, 4, 11, 7, },
50 { 2, 14, 1, 13, 2, 14, 1, 13, },
51 { 10, 6, 9, 5, 10, 6, 9, 5, },
52 { 0, 12, 3, 15, 0, 12, 3, 15, },
55 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_32)[8][8] = {
56 { 17, 9, 23, 15, 16, 8, 22, 14, },
57 { 5, 29, 3, 27, 4, 28, 2, 26, },
58 { 21, 13, 19, 11, 20, 12, 18, 10, },
59 { 0, 24, 6, 30, 1, 25, 7, 31, },
60 { 16, 8, 22, 14, 17, 9, 23, 15, },
61 { 4, 28, 2, 26, 5, 29, 3, 27, },
62 { 20, 12, 18, 10, 21, 13, 19, 11, },
63 { 1, 25, 7, 31, 0, 24, 6, 30, },
66 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_73)[8][8] = {
67 { 0, 55, 14, 68, 3, 58, 17, 72, },
68 { 37, 18, 50, 32, 40, 22, 54, 35, },
69 { 9, 64, 5, 59, 13, 67, 8, 63, },
70 { 46, 27, 41, 23, 49, 31, 44, 26, },
71 { 2, 57, 16, 71, 1, 56, 15, 70, },
72 { 39, 21, 52, 34, 38, 19, 51, 33, },
73 { 11, 66, 7, 62, 10, 65, 6, 60, },
74 { 48, 30, 43, 25, 47, 29, 42, 24, },
78 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
79 {117, 62, 158, 103, 113, 58, 155, 100, },
80 { 34, 199, 21, 186, 31, 196, 17, 182, },
81 {144, 89, 131, 76, 141, 86, 127, 72, },
82 { 0, 165, 41, 206, 10, 175, 52, 217, },
83 {110, 55, 151, 96, 120, 65, 162, 107, },
84 { 28, 193, 14, 179, 38, 203, 24, 189, },
85 {138, 83, 124, 69, 148, 93, 134, 79, },
86 { 7, 172, 48, 213, 3, 168, 45, 210, },
89 // tries to correct a gamma of 1.5
90 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
91 { 0, 143, 18, 200, 2, 156, 25, 215, },
92 { 78, 28, 125, 64, 89, 36, 138, 74, },
93 { 10, 180, 3, 161, 16, 195, 8, 175, },
94 {109, 51, 93, 38, 121, 60, 105, 47, },
95 { 1, 152, 23, 210, 0, 147, 20, 205, },
96 { 85, 33, 134, 71, 81, 30, 130, 67, },
97 { 14, 190, 6, 171, 12, 185, 5, 166, },
98 {117, 57, 101, 44, 113, 54, 97, 41, },
101 // tries to correct a gamma of 2.0
102 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
103 { 0, 124, 8, 193, 0, 140, 12, 213, },
104 { 55, 14, 104, 42, 66, 19, 119, 52, },
105 { 3, 168, 1, 145, 6, 187, 3, 162, },
106 { 86, 31, 70, 21, 99, 39, 82, 28, },
107 { 0, 134, 11, 206, 0, 129, 9, 200, },
108 { 62, 17, 114, 48, 58, 16, 109, 45, },
109 { 5, 181, 2, 157, 4, 175, 1, 151, },
110 { 95, 36, 78, 26, 90, 34, 74, 24, },
113 // tries to correct a gamma of 2.5
114 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
115 { 0, 107, 3, 187, 0, 125, 6, 212, },
116 { 39, 7, 86, 28, 49, 11, 102, 36, },
117 { 1, 158, 0, 131, 3, 180, 1, 151, },
118 { 68, 19, 52, 12, 81, 25, 64, 17, },
119 { 0, 119, 5, 203, 0, 113, 4, 195, },
120 { 45, 9, 96, 33, 42, 8, 91, 30, },
121 { 2, 172, 1, 144, 2, 165, 0, 137, },
122 { 77, 23, 60, 15, 72, 21, 56, 14, },
126 #define output_pixel(pos, val, bias, signedness) \
128 AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
130 AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
133 static av_always_inline void
134 yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
135 int big_endian, int output_bits)
138 int shift = 19 - output_bits;
140 for (i = 0; i < dstW; i++) {
141 int val = src[i] + (1 << (shift - 1));
142 output_pixel(&dest[i], val, 0, uint);
146 static av_always_inline void
147 yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
148 const int32_t **src, uint16_t *dest, int dstW,
149 int big_endian, int output_bits)
152 int shift = 15 + 16 - output_bits;
154 for (i = 0; i < dstW; i++) {
155 int val = 1 << (30-output_bits);
158 /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
159 * filters (or anything with negative coeffs, the range can be slightly
160 * wider in both directions. To account for this overflow, we subtract
161 * a constant so it always fits in the signed range (assuming a
162 * reasonable filterSize), and re-add that at the end. */
164 for (j = 0; j < filterSize; j++)
165 val += src[j][i] * filter[j];
167 output_pixel(&dest[i], val, 0x8000, int);
173 #define output_pixel(pos, val) \
175 AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
177 AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
180 static av_always_inline void
181 yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
182 int big_endian, int output_bits)
185 int shift = 15 - output_bits;
187 for (i = 0; i < dstW; i++) {
188 int val = src[i] + (1 << (shift - 1));
189 output_pixel(&dest[i], val);
193 static av_always_inline void
194 yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
195 const int16_t **src, uint16_t *dest, int dstW,
196 int big_endian, int output_bits)
199 int shift = 11 + 16 - output_bits;
201 for (i = 0; i < dstW; i++) {
202 int val = 1 << (26-output_bits);
205 for (j = 0; j < filterSize; j++)
206 val += src[j][i] * filter[j];
208 output_pixel(&dest[i], val);
214 #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
215 static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
216 uint8_t *dest, int dstW, \
217 const uint8_t *dither, int offset)\
219 yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
220 (uint16_t *) dest, dstW, is_be, bits); \
222 static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
223 const int16_t **src, uint8_t *dest, int dstW, \
224 const uint8_t *dither, int offset)\
226 yuv2planeX_## template_size ## _c_template(filter, \
227 filterSize, (const typeX_t **) src, \
228 (uint16_t *) dest, dstW, is_be, bits); \
230 yuv2NBPS( 9, BE, 1, 10, int16_t)
231 yuv2NBPS( 9, LE, 0, 10, int16_t)
232 yuv2NBPS(10, BE, 1, 10, int16_t)
233 yuv2NBPS(10, LE, 0, 10, int16_t)
234 yuv2NBPS(12, BE, 1, 10, int16_t)
235 yuv2NBPS(12, LE, 0, 10, int16_t)
236 yuv2NBPS(16, BE, 1, 16, int32_t)
237 yuv2NBPS(16, LE, 0, 16, int32_t)
239 static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
240 const int16_t **src, uint8_t *dest, int dstW,
241 const uint8_t *dither, int offset)
244 for (i=0; i<dstW; i++) {
245 int val = dither[(i + offset) & 7] << 12;
247 for (j=0; j<filterSize; j++)
248 val += src[j][i] * filter[j];
250 dest[i]= av_clip_uint8(val>>19);
254 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
255 const uint8_t *dither, int offset)
258 for (i=0; i<dstW; i++) {
259 int val = (src[i] + dither[(i + offset) & 7]) >> 7;
260 dest[i]= av_clip_uint8(val);
264 static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
265 const int16_t **chrUSrc, const int16_t **chrVSrc,
266 uint8_t *dest, int chrDstW)
268 enum AVPixelFormat dstFormat = c->dstFormat;
269 const uint8_t *chrDither = c->chrDither8;
272 if (dstFormat == AV_PIX_FMT_NV12)
273 for (i=0; i<chrDstW; i++) {
274 int u = chrDither[i & 7] << 12;
275 int v = chrDither[(i + 3) & 7] << 12;
277 for (j=0; j<chrFilterSize; j++) {
278 u += chrUSrc[j][i] * chrFilter[j];
279 v += chrVSrc[j][i] * chrFilter[j];
282 dest[2*i]= av_clip_uint8(u>>19);
283 dest[2*i+1]= av_clip_uint8(v>>19);
286 for (i=0; i<chrDstW; i++) {
287 int u = chrDither[i & 7] << 12;
288 int v = chrDither[(i + 3) & 7] << 12;
290 for (j=0; j<chrFilterSize; j++) {
291 u += chrUSrc[j][i] * chrFilter[j];
292 v += chrVSrc[j][i] * chrFilter[j];
295 dest[2*i]= av_clip_uint8(v>>19);
296 dest[2*i+1]= av_clip_uint8(u>>19);
300 #define accumulate_bit(acc, val) \
302 acc |= (val) >= (128 + 110)
303 #define output_pixel(pos, acc) \
304 if (target == AV_PIX_FMT_MONOBLACK) { \
310 static av_always_inline void
311 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
312 const int16_t **lumSrc, int lumFilterSize,
313 const int16_t *chrFilter, const int16_t **chrUSrc,
314 const int16_t **chrVSrc, int chrFilterSize,
315 const int16_t **alpSrc, uint8_t *dest, int dstW,
316 int y, enum AVPixelFormat target)
318 const uint8_t * const d128 = ff_dither_8x8_220[y&7];
322 for (i = 0; i < dstW; i += 2) {
327 for (j = 0; j < lumFilterSize; j++) {
328 Y1 += lumSrc[j][i] * lumFilter[j];
329 Y2 += lumSrc[j][i+1] * lumFilter[j];
333 if ((Y1 | Y2) & 0x100) {
334 Y1 = av_clip_uint8(Y1);
335 Y2 = av_clip_uint8(Y2);
337 accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
338 accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
340 output_pixel(*dest++, acc);
345 output_pixel(*dest, acc);
349 static av_always_inline void
350 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
351 const int16_t *ubuf[2], const int16_t *vbuf[2],
352 const int16_t *abuf[2], uint8_t *dest, int dstW,
353 int yalpha, int uvalpha, int y,
354 enum AVPixelFormat target)
356 const int16_t *buf0 = buf[0], *buf1 = buf[1];
357 const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
358 int yalpha1 = 4096 - yalpha;
361 for (i = 0; i < dstW; i += 8) {
364 Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
365 accumulate_bit(acc, Y + d128[0]);
366 Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
367 accumulate_bit(acc, Y + d128[1]);
368 Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
369 accumulate_bit(acc, Y + d128[2]);
370 Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
371 accumulate_bit(acc, Y + d128[3]);
372 Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
373 accumulate_bit(acc, Y + d128[4]);
374 Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
375 accumulate_bit(acc, Y + d128[5]);
376 Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
377 accumulate_bit(acc, Y + d128[6]);
378 Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
379 accumulate_bit(acc, Y + d128[7]);
381 output_pixel(*dest++, acc);
385 static av_always_inline void
386 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
387 const int16_t *ubuf[2], const int16_t *vbuf[2],
388 const int16_t *abuf0, uint8_t *dest, int dstW,
389 int uvalpha, int y, enum AVPixelFormat target)
391 const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
394 for (i = 0; i < dstW; i += 8) {
397 accumulate_bit(acc, (buf0[i + 0] >> 7) + d128[0]);
398 accumulate_bit(acc, (buf0[i + 1] >> 7) + d128[1]);
399 accumulate_bit(acc, (buf0[i + 2] >> 7) + d128[2]);
400 accumulate_bit(acc, (buf0[i + 3] >> 7) + d128[3]);
401 accumulate_bit(acc, (buf0[i + 4] >> 7) + d128[4]);
402 accumulate_bit(acc, (buf0[i + 5] >> 7) + d128[5]);
403 accumulate_bit(acc, (buf0[i + 6] >> 7) + d128[6]);
404 accumulate_bit(acc, (buf0[i + 7] >> 7) + d128[7]);
406 output_pixel(*dest++, acc);
411 #undef accumulate_bit
413 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
414 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
415 const int16_t **lumSrc, int lumFilterSize, \
416 const int16_t *chrFilter, const int16_t **chrUSrc, \
417 const int16_t **chrVSrc, int chrFilterSize, \
418 const int16_t **alpSrc, uint8_t *dest, int dstW, \
421 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
422 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
423 alpSrc, dest, dstW, y, fmt); \
426 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
427 const int16_t *ubuf[2], const int16_t *vbuf[2], \
428 const int16_t *abuf[2], uint8_t *dest, int dstW, \
429 int yalpha, int uvalpha, int y) \
431 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
432 dest, dstW, yalpha, uvalpha, y, fmt); \
435 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
436 const int16_t *ubuf[2], const int16_t *vbuf[2], \
437 const int16_t *abuf0, uint8_t *dest, int dstW, \
438 int uvalpha, int y) \
440 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
441 abuf0, dest, dstW, uvalpha, \
445 YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
446 YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
448 #define output_pixels(pos, Y1, U, Y2, V) \
449 if (target == AV_PIX_FMT_YUYV422) { \
450 dest[pos + 0] = Y1; \
452 dest[pos + 2] = Y2; \
454 } else if (target == AV_PIX_FMT_YVYU422) { \
455 dest[pos + 0] = Y1; \
457 dest[pos + 2] = Y2; \
459 } else { /* AV_PIX_FMT_UYVY422 */ \
461 dest[pos + 1] = Y1; \
463 dest[pos + 3] = Y2; \
466 static av_always_inline void
467 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
468 const int16_t **lumSrc, int lumFilterSize,
469 const int16_t *chrFilter, const int16_t **chrUSrc,
470 const int16_t **chrVSrc, int chrFilterSize,
471 const int16_t **alpSrc, uint8_t *dest, int dstW,
472 int y, enum AVPixelFormat target)
476 for (i = 0; i < ((dstW + 1) >> 1); i++) {
483 for (j = 0; j < lumFilterSize; j++) {
484 Y1 += lumSrc[j][i * 2] * lumFilter[j];
485 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
487 for (j = 0; j < chrFilterSize; j++) {
488 U += chrUSrc[j][i] * chrFilter[j];
489 V += chrVSrc[j][i] * chrFilter[j];
495 if ((Y1 | Y2 | U | V) & 0x100) {
496 Y1 = av_clip_uint8(Y1);
497 Y2 = av_clip_uint8(Y2);
498 U = av_clip_uint8(U);
499 V = av_clip_uint8(V);
501 output_pixels(4*i, Y1, U, Y2, V);
505 static av_always_inline void
506 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
507 const int16_t *ubuf[2], const int16_t *vbuf[2],
508 const int16_t *abuf[2], uint8_t *dest, int dstW,
509 int yalpha, int uvalpha, int y,
510 enum AVPixelFormat target)
512 const int16_t *buf0 = buf[0], *buf1 = buf[1],
513 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
514 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
515 int yalpha1 = 4096 - yalpha;
516 int uvalpha1 = 4096 - uvalpha;
519 for (i = 0; i < ((dstW + 1) >> 1); i++) {
520 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
521 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
522 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
523 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
525 Y1 = av_clip_uint8(Y1);
526 Y2 = av_clip_uint8(Y2);
527 U = av_clip_uint8(U);
528 V = av_clip_uint8(V);
530 output_pixels(i * 4, Y1, U, Y2, V);
534 static av_always_inline void
535 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
536 const int16_t *ubuf[2], const int16_t *vbuf[2],
537 const int16_t *abuf0, uint8_t *dest, int dstW,
538 int uvalpha, int y, enum AVPixelFormat target)
540 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
543 if (uvalpha < 2048) {
544 for (i = 0; i < ((dstW + 1) >> 1); i++) {
545 int Y1 = buf0[i * 2] >> 7;
546 int Y2 = buf0[i * 2 + 1] >> 7;
547 int U = ubuf0[i] >> 7;
548 int V = vbuf0[i] >> 7;
550 Y1 = av_clip_uint8(Y1);
551 Y2 = av_clip_uint8(Y2);
552 U = av_clip_uint8(U);
553 V = av_clip_uint8(V);
555 output_pixels(i * 4, Y1, U, Y2, V);
558 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
559 for (i = 0; i < ((dstW + 1) >> 1); i++) {
560 int Y1 = buf0[i * 2] >> 7;
561 int Y2 = buf0[i * 2 + 1] >> 7;
562 int U = (ubuf0[i] + ubuf1[i]) >> 8;
563 int V = (vbuf0[i] + vbuf1[i]) >> 8;
565 Y1 = av_clip_uint8(Y1);
566 Y2 = av_clip_uint8(Y2);
567 U = av_clip_uint8(U);
568 V = av_clip_uint8(V);
570 output_pixels(i * 4, Y1, U, Y2, V);
577 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
578 YUV2PACKEDWRAPPER(yuv2, 422, yvyu422, AV_PIX_FMT_YVYU422)
579 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
581 #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? R : B)
582 #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? B : R)
583 #define output_pixel(pos, val) \
584 if (isBE(target)) { \
590 static av_always_inline void
591 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
592 const int32_t **lumSrc, int lumFilterSize,
593 const int16_t *chrFilter, const int32_t **chrUSrc,
594 const int32_t **chrVSrc, int chrFilterSize,
595 const int32_t **alpSrc, uint16_t *dest, int dstW,
596 int y, enum AVPixelFormat target)
600 for (i = 0; i < ((dstW + 1) >> 1); i++) {
602 int Y1 = -0x40000000;
603 int Y2 = -0x40000000;
604 int U = -128 << 23; // 19
608 for (j = 0; j < lumFilterSize; j++) {
609 Y1 += lumSrc[j][i * 2] * lumFilter[j];
610 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
612 for (j = 0; j < chrFilterSize; j++) {
613 U += chrUSrc[j][i] * chrFilter[j];
614 V += chrVSrc[j][i] * chrFilter[j];
617 // 8 bits: 12+15=27; 16 bits: 12+19=31
625 // 8 bits: 27 -> 17 bits, 16 bits: 31 - 14 = 17 bits
626 Y1 -= c->yuv2rgb_y_offset;
627 Y2 -= c->yuv2rgb_y_offset;
628 Y1 *= c->yuv2rgb_y_coeff;
629 Y2 *= c->yuv2rgb_y_coeff;
632 // 8 bits: 17 + 13 bits = 30 bits, 16 bits: 17 + 13 bits = 30 bits
634 R = V * c->yuv2rgb_v2r_coeff;
635 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
636 B = U * c->yuv2rgb_u2b_coeff;
638 // 8 bits: 30 - 22 = 8 bits, 16 bits: 30 bits - 14 = 16 bits
639 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
640 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
641 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
642 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
643 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
644 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
649 static av_always_inline void
650 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
651 const int32_t *ubuf[2], const int32_t *vbuf[2],
652 const int32_t *abuf[2], uint16_t *dest, int dstW,
653 int yalpha, int uvalpha, int y,
654 enum AVPixelFormat target)
656 const int32_t *buf0 = buf[0], *buf1 = buf[1],
657 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
658 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
659 int yalpha1 = 4096 - yalpha;
660 int uvalpha1 = 4096 - uvalpha;
663 for (i = 0; i < ((dstW + 1) >> 1); i++) {
664 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
665 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
666 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
667 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
670 Y1 -= c->yuv2rgb_y_offset;
671 Y2 -= c->yuv2rgb_y_offset;
672 Y1 *= c->yuv2rgb_y_coeff;
673 Y2 *= c->yuv2rgb_y_coeff;
677 R = V * c->yuv2rgb_v2r_coeff;
678 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
679 B = U * c->yuv2rgb_u2b_coeff;
681 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
682 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
683 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
684 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
685 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
686 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
691 static av_always_inline void
692 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
693 const int32_t *ubuf[2], const int32_t *vbuf[2],
694 const int32_t *abuf0, uint16_t *dest, int dstW,
695 int uvalpha, int y, enum AVPixelFormat target)
697 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
700 if (uvalpha < 2048) {
701 for (i = 0; i < ((dstW + 1) >> 1); i++) {
702 int Y1 = (buf0[i * 2] ) >> 2;
703 int Y2 = (buf0[i * 2 + 1]) >> 2;
704 int U = (ubuf0[i] + (-128 << 11)) >> 2;
705 int V = (vbuf0[i] + (-128 << 11)) >> 2;
708 Y1 -= c->yuv2rgb_y_offset;
709 Y2 -= c->yuv2rgb_y_offset;
710 Y1 *= c->yuv2rgb_y_coeff;
711 Y2 *= c->yuv2rgb_y_coeff;
715 R = V * c->yuv2rgb_v2r_coeff;
716 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
717 B = U * c->yuv2rgb_u2b_coeff;
719 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
720 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
721 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
722 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
723 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
724 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
728 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
729 for (i = 0; i < ((dstW + 1) >> 1); i++) {
730 int Y1 = (buf0[i * 2] ) >> 2;
731 int Y2 = (buf0[i * 2 + 1]) >> 2;
732 int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
733 int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
736 Y1 -= c->yuv2rgb_y_offset;
737 Y2 -= c->yuv2rgb_y_offset;
738 Y1 *= c->yuv2rgb_y_coeff;
739 Y2 *= c->yuv2rgb_y_coeff;
743 R = V * c->yuv2rgb_v2r_coeff;
744 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
745 B = U * c->yuv2rgb_u2b_coeff;
747 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
748 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
749 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
750 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
751 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
752 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
762 #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
763 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
764 const int16_t **_lumSrc, int lumFilterSize, \
765 const int16_t *chrFilter, const int16_t **_chrUSrc, \
766 const int16_t **_chrVSrc, int chrFilterSize, \
767 const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
770 const int32_t **lumSrc = (const int32_t **) _lumSrc, \
771 **chrUSrc = (const int32_t **) _chrUSrc, \
772 **chrVSrc = (const int32_t **) _chrVSrc, \
773 **alpSrc = (const int32_t **) _alpSrc; \
774 uint16_t *dest = (uint16_t *) _dest; \
775 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
776 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
777 alpSrc, dest, dstW, y, fmt); \
780 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
781 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
782 const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
783 int yalpha, int uvalpha, int y) \
785 const int32_t **buf = (const int32_t **) _buf, \
786 **ubuf = (const int32_t **) _ubuf, \
787 **vbuf = (const int32_t **) _vbuf, \
788 **abuf = (const int32_t **) _abuf; \
789 uint16_t *dest = (uint16_t *) _dest; \
790 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
791 dest, dstW, yalpha, uvalpha, y, fmt); \
794 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
795 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
796 const int16_t *_abuf0, uint8_t *_dest, int dstW, \
797 int uvalpha, int y) \
799 const int32_t *buf0 = (const int32_t *) _buf0, \
800 **ubuf = (const int32_t **) _ubuf, \
801 **vbuf = (const int32_t **) _vbuf, \
802 *abuf0 = (const int32_t *) _abuf0; \
803 uint16_t *dest = (uint16_t *) _dest; \
804 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
805 dstW, uvalpha, y, fmt); \
808 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, AV_PIX_FMT_RGB48BE)
809 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, AV_PIX_FMT_RGB48LE)
810 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, AV_PIX_FMT_BGR48BE)
811 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, AV_PIX_FMT_BGR48LE)
814 * Write out 2 RGB pixels in the target pixel format. This function takes a
815 * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
816 * things like endianness conversion and shifting. The caller takes care of
817 * setting the correct offset in these tables from the chroma (U/V) values.
818 * This function then uses the luminance (Y1/Y2) values to write out the
819 * correct RGB values into the destination buffer.
821 static av_always_inline void
822 yuv2rgb_write(uint8_t *_dest, int i, unsigned Y1, unsigned Y2,
823 unsigned A1, unsigned A2,
824 const void *_r, const void *_g, const void *_b, int y,
825 enum AVPixelFormat target, int hasAlpha)
827 if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
828 target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
829 uint32_t *dest = (uint32_t *) _dest;
830 const uint32_t *r = (const uint32_t *) _r;
831 const uint32_t *g = (const uint32_t *) _g;
832 const uint32_t *b = (const uint32_t *) _b;
835 int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
837 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
838 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
841 int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
843 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
844 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
846 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
847 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
850 } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
851 uint8_t *dest = (uint8_t *) _dest;
852 const uint8_t *r = (const uint8_t *) _r;
853 const uint8_t *g = (const uint8_t *) _g;
854 const uint8_t *b = (const uint8_t *) _b;
856 #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
857 #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
858 dest[i * 6 + 0] = r_b[Y1];
859 dest[i * 6 + 1] = g[Y1];
860 dest[i * 6 + 2] = b_r[Y1];
861 dest[i * 6 + 3] = r_b[Y2];
862 dest[i * 6 + 4] = g[Y2];
863 dest[i * 6 + 5] = b_r[Y2];
866 } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
867 target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
868 target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
869 uint16_t *dest = (uint16_t *) _dest;
870 const uint16_t *r = (const uint16_t *) _r;
871 const uint16_t *g = (const uint16_t *) _g;
872 const uint16_t *b = (const uint16_t *) _b;
873 int dr1, dg1, db1, dr2, dg2, db2;
875 if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
876 dr1 = dither_2x2_8[ y & 1 ][0];
877 dg1 = dither_2x2_4[ y & 1 ][0];
878 db1 = dither_2x2_8[(y & 1) ^ 1][0];
879 dr2 = dither_2x2_8[ y & 1 ][1];
880 dg2 = dither_2x2_4[ y & 1 ][1];
881 db2 = dither_2x2_8[(y & 1) ^ 1][1];
882 } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
883 dr1 = dither_2x2_8[ y & 1 ][0];
884 dg1 = dither_2x2_8[ y & 1 ][1];
885 db1 = dither_2x2_8[(y & 1) ^ 1][0];
886 dr2 = dither_2x2_8[ y & 1 ][1];
887 dg2 = dither_2x2_8[ y & 1 ][0];
888 db2 = dither_2x2_8[(y & 1) ^ 1][1];
890 dr1 = ff_dither_4x4_16[ y & 3 ][0];
891 dg1 = ff_dither_4x4_16[ y & 3 ][1];
892 db1 = ff_dither_4x4_16[(y & 3) ^ 3][0];
893 dr2 = ff_dither_4x4_16[ y & 3 ][1];
894 dg2 = ff_dither_4x4_16[ y & 3 ][0];
895 db2 = ff_dither_4x4_16[(y & 3) ^ 3][1];
898 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
899 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
900 } else /* 8/4 bits */ {
901 uint8_t *dest = (uint8_t *) _dest;
902 const uint8_t *r = (const uint8_t *) _r;
903 const uint8_t *g = (const uint8_t *) _g;
904 const uint8_t *b = (const uint8_t *) _b;
905 int dr1, dg1, db1, dr2, dg2, db2;
907 if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
908 const uint8_t * const d64 = ff_dither_8x8_73[y & 7];
909 const uint8_t * const d32 = ff_dither_8x8_32[y & 7];
910 dr1 = dg1 = d32[(i * 2 + 0) & 7];
911 db1 = d64[(i * 2 + 0) & 7];
912 dr2 = dg2 = d32[(i * 2 + 1) & 7];
913 db2 = d64[(i * 2 + 1) & 7];
915 const uint8_t * const d64 = ff_dither_8x8_73 [y & 7];
916 const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
917 dr1 = db1 = d128[(i * 2 + 0) & 7];
918 dg1 = d64[(i * 2 + 0) & 7];
919 dr2 = db2 = d128[(i * 2 + 1) & 7];
920 dg2 = d64[(i * 2 + 1) & 7];
923 if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
924 dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
925 ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
927 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
928 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
933 static av_always_inline void
934 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
935 const int16_t **lumSrc, int lumFilterSize,
936 const int16_t *chrFilter, const int16_t **chrUSrc,
937 const int16_t **chrVSrc, int chrFilterSize,
938 const int16_t **alpSrc, uint8_t *dest, int dstW,
939 int y, enum AVPixelFormat target, int hasAlpha)
943 for (i = 0; i < ((dstW + 1) >> 1); i++) {
949 const void *r, *g, *b;
951 for (j = 0; j < lumFilterSize; j++) {
952 Y1 += lumSrc[j][i * 2] * lumFilter[j];
953 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
955 for (j = 0; j < chrFilterSize; j++) {
956 U += chrUSrc[j][i] * chrFilter[j];
957 V += chrVSrc[j][i] * chrFilter[j];
963 if ((Y1 | Y2 | U | V) & 0x100) {
964 Y1 = av_clip_uint8(Y1);
965 Y2 = av_clip_uint8(Y2);
966 U = av_clip_uint8(U);
967 V = av_clip_uint8(V);
972 for (j = 0; j < lumFilterSize; j++) {
973 A1 += alpSrc[j][i * 2 ] * lumFilter[j];
974 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
978 if ((A1 | A2) & 0x100) {
979 A1 = av_clip_uint8(A1);
980 A2 = av_clip_uint8(A2);
984 /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
986 g = (c->table_gU[U] + c->table_gV[V]);
989 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
990 r, g, b, y, target, hasAlpha);
994 static av_always_inline void
995 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
996 const int16_t *ubuf[2], const int16_t *vbuf[2],
997 const int16_t *abuf[2], uint8_t *dest, int dstW,
998 int yalpha, int uvalpha, int y,
999 enum AVPixelFormat target, int hasAlpha)
1001 const int16_t *buf0 = buf[0], *buf1 = buf[1],
1002 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1003 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1004 *abuf0 = hasAlpha ? abuf[0] : NULL,
1005 *abuf1 = hasAlpha ? abuf[1] : NULL;
1006 int yalpha1 = 4096 - yalpha;
1007 int uvalpha1 = 4096 - uvalpha;
1010 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1011 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
1012 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
1013 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
1014 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
1016 const void *r, *g, *b;
1018 Y1 = av_clip_uint8(Y1);
1019 Y2 = av_clip_uint8(Y2);
1020 U = av_clip_uint8(U);
1021 V = av_clip_uint8(V);
1024 g = (c->table_gU[U] + c->table_gV[V]);
1028 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
1029 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1030 A1 = av_clip_uint8(A1);
1031 A2 = av_clip_uint8(A2);
1034 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1035 r, g, b, y, target, hasAlpha);
1039 static av_always_inline void
1040 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1041 const int16_t *ubuf[2], const int16_t *vbuf[2],
1042 const int16_t *abuf0, uint8_t *dest, int dstW,
1043 int uvalpha, int y, enum AVPixelFormat target,
1046 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1049 if (uvalpha < 2048) {
1050 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1051 int Y1 = buf0[i * 2] >> 7;
1052 int Y2 = buf0[i * 2 + 1] >> 7;
1053 int U = ubuf0[i] >> 7;
1054 int V = vbuf0[i] >> 7;
1056 const void *r, *g, *b;
1058 Y1 = av_clip_uint8(Y1);
1059 Y2 = av_clip_uint8(Y2);
1060 U = av_clip_uint8(U);
1061 V = av_clip_uint8(V);
1064 g = (c->table_gU[U] + c->table_gV[V]);
1068 A1 = abuf0[i * 2 ] >> 7;
1069 A2 = abuf0[i * 2 + 1] >> 7;
1070 A1 = av_clip_uint8(A1);
1071 A2 = av_clip_uint8(A2);
1074 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1075 r, g, b, y, target, hasAlpha);
1078 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1079 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1080 int Y1 = buf0[i * 2] >> 7;
1081 int Y2 = buf0[i * 2 + 1] >> 7;
1082 int U = (ubuf0[i] + ubuf1[i]) >> 8;
1083 int V = (vbuf0[i] + vbuf1[i]) >> 8;
1085 const void *r, *g, *b;
1087 Y1 = av_clip_uint8(Y1);
1088 Y2 = av_clip_uint8(Y2);
1089 U = av_clip_uint8(U);
1090 V = av_clip_uint8(V);
1093 g = (c->table_gU[U] + c->table_gV[V]);
1097 A1 = abuf0[i * 2 ] >> 7;
1098 A2 = abuf0[i * 2 + 1] >> 7;
1099 A1 = av_clip_uint8(A1);
1100 A2 = av_clip_uint8(A2);
1103 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1104 r, g, b, y, target, hasAlpha);
1109 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1110 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1111 const int16_t **lumSrc, int lumFilterSize, \
1112 const int16_t *chrFilter, const int16_t **chrUSrc, \
1113 const int16_t **chrVSrc, int chrFilterSize, \
1114 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1117 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1118 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1119 alpSrc, dest, dstW, y, fmt, hasAlpha); \
1121 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1122 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1123 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1124 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1125 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1126 int yalpha, int uvalpha, int y) \
1128 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1129 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1132 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1133 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1134 const int16_t *abuf0, uint8_t *dest, int dstW, \
1135 int uvalpha, int y) \
1137 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1138 dstW, uvalpha, y, fmt, hasAlpha); \
1142 YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1143 YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1145 #if CONFIG_SWSCALE_ALPHA
1146 YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
1147 YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
1149 YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
1150 YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
1152 YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
1153 YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
1154 YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
1155 YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
1156 YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
1157 YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
1158 YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
1159 YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
1161 static av_always_inline void
1162 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1163 const int16_t **lumSrc, int lumFilterSize,
1164 const int16_t *chrFilter, const int16_t **chrUSrc,
1165 const int16_t **chrVSrc, int chrFilterSize,
1166 const int16_t **alpSrc, uint8_t *dest,
1167 int dstW, int y, enum AVPixelFormat target, int hasAlpha)
1170 int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1172 for (i = 0; i < dstW; i++) {
1179 for (j = 0; j < lumFilterSize; j++) {
1180 Y += lumSrc[j][i] * lumFilter[j];
1182 for (j = 0; j < chrFilterSize; j++) {
1183 U += chrUSrc[j][i] * chrFilter[j];
1184 V += chrVSrc[j][i] * chrFilter[j];
1191 for (j = 0; j < lumFilterSize; j++) {
1192 A += alpSrc[j][i] * lumFilter[j];
1196 A = av_clip_uint8(A);
1198 Y -= c->yuv2rgb_y_offset;
1199 Y *= c->yuv2rgb_y_coeff;
1201 R = Y + V*c->yuv2rgb_v2r_coeff;
1202 G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1203 B = Y + U*c->yuv2rgb_u2b_coeff;
1204 if ((R | G | B) & 0xC0000000) {
1205 R = av_clip_uintp2(R, 30);
1206 G = av_clip_uintp2(G, 30);
1207 B = av_clip_uintp2(B, 30);
1211 case AV_PIX_FMT_ARGB:
1212 dest[0] = hasAlpha ? A : 255;
1217 case AV_PIX_FMT_RGB24:
1222 case AV_PIX_FMT_RGBA:
1226 dest[3] = hasAlpha ? A : 255;
1228 case AV_PIX_FMT_ABGR:
1229 dest[0] = hasAlpha ? A : 255;
1235 case AV_PIX_FMT_BGR24:
1240 case AV_PIX_FMT_BGRA:
1244 dest[3] = hasAlpha ? A : 255;
1252 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1253 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1254 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1255 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1257 #if CONFIG_SWSCALE_ALPHA
1258 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
1259 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
1260 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
1261 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
1263 YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
1264 YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
1265 YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
1266 YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
1268 YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
1269 YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
1272 yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
1273 const int16_t **lumSrc, int lumFilterSize,
1274 const int16_t *chrFilter, const int16_t **chrUSrc,
1275 const int16_t **chrVSrc, int chrFilterSize,
1276 const int16_t **alpSrc, uint8_t **dest,
1279 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
1281 int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
1282 uint16_t **dest16 = (uint16_t**)dest;
1283 int SH = 22 + 8 - desc->comp[0].depth;
1285 for (i = 0; i < dstW; i++) {
1288 int U = (1 << 9) - (128 << 19);
1289 int V = (1 << 9) - (128 << 19);
1292 for (j = 0; j < lumFilterSize; j++)
1293 Y += lumSrc[j][i] * lumFilter[j];
1295 for (j = 0; j < chrFilterSize; j++) {
1296 U += chrUSrc[j][i] * chrFilter[j];
1297 V += chrVSrc[j][i] * chrFilter[j];
1307 for (j = 0; j < lumFilterSize; j++)
1308 A += alpSrc[j][i] * lumFilter[j];
1313 A = av_clip_uint8(A);
1316 Y -= c->yuv2rgb_y_offset;
1317 Y *= c->yuv2rgb_y_coeff;
1319 R = Y + V * c->yuv2rgb_v2r_coeff;
1320 G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1321 B = Y + U * c->yuv2rgb_u2b_coeff;
1323 if ((R | G | B) & 0xC0000000) {
1324 R = av_clip_uintp2(R, 30);
1325 G = av_clip_uintp2(G, 30);
1326 B = av_clip_uintp2(B, 30);
1330 dest16[0][i] = G >> SH;
1331 dest16[1][i] = B >> SH;
1332 dest16[2][i] = R >> SH;
1336 dest[0][i] = G >> 22;
1337 dest[1][i] = B >> 22;
1338 dest[2][i] = R >> 22;
1343 if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
1344 for (i = 0; i < dstW; i++) {
1345 dest16[0][i] = av_bswap16(dest16[0][i]);
1346 dest16[1][i] = av_bswap16(dest16[1][i]);
1347 dest16[2][i] = av_bswap16(dest16[2][i]);
1349 dest16[3][i] = av_bswap16(dest16[3][i]);
1354 av_cold void ff_sws_init_output_funcs(SwsContext *c,
1355 yuv2planar1_fn *yuv2plane1,
1356 yuv2planarX_fn *yuv2planeX,
1357 yuv2interleavedX_fn *yuv2nv12cX,
1358 yuv2packed1_fn *yuv2packed1,
1359 yuv2packed2_fn *yuv2packed2,
1360 yuv2packedX_fn *yuv2packedX,
1361 yuv2anyX_fn *yuv2anyX)
1363 enum AVPixelFormat dstFormat = c->dstFormat;
1364 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
1366 if (is16BPS(dstFormat)) {
1367 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
1368 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
1369 } else if (is9_15BPS(dstFormat)) {
1370 if (desc->comp[0].depth == 9) {
1371 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
1372 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
1373 } else if (desc->comp[0].depth == 10) {
1374 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
1375 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
1376 } else if (desc->comp[0].depth == 12) {
1377 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c : yuv2planeX_12LE_c;
1378 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c : yuv2plane1_12LE_c;
1383 *yuv2plane1 = yuv2plane1_8_c;
1384 *yuv2planeX = yuv2planeX_8_c;
1385 if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
1386 *yuv2nv12cX = yuv2nv12cX_c;
1389 if(c->flags & SWS_FULL_CHR_H_INT) {
1390 switch (dstFormat) {
1391 case AV_PIX_FMT_RGBA:
1393 *yuv2packedX = yuv2rgba32_full_X_c;
1395 #if CONFIG_SWSCALE_ALPHA
1397 *yuv2packedX = yuv2rgba32_full_X_c;
1399 #endif /* CONFIG_SWSCALE_ALPHA */
1401 *yuv2packedX = yuv2rgbx32_full_X_c;
1403 #endif /* !CONFIG_SMALL */
1405 case AV_PIX_FMT_ARGB:
1407 *yuv2packedX = yuv2argb32_full_X_c;
1409 #if CONFIG_SWSCALE_ALPHA
1411 *yuv2packedX = yuv2argb32_full_X_c;
1413 #endif /* CONFIG_SWSCALE_ALPHA */
1415 *yuv2packedX = yuv2xrgb32_full_X_c;
1417 #endif /* !CONFIG_SMALL */
1419 case AV_PIX_FMT_BGRA:
1421 *yuv2packedX = yuv2bgra32_full_X_c;
1423 #if CONFIG_SWSCALE_ALPHA
1425 *yuv2packedX = yuv2bgra32_full_X_c;
1427 #endif /* CONFIG_SWSCALE_ALPHA */
1429 *yuv2packedX = yuv2bgrx32_full_X_c;
1431 #endif /* !CONFIG_SMALL */
1433 case AV_PIX_FMT_ABGR:
1435 *yuv2packedX = yuv2abgr32_full_X_c;
1437 #if CONFIG_SWSCALE_ALPHA
1439 *yuv2packedX = yuv2abgr32_full_X_c;
1441 #endif /* CONFIG_SWSCALE_ALPHA */
1443 *yuv2packedX = yuv2xbgr32_full_X_c;
1445 #endif /* !CONFIG_SMALL */
1447 case AV_PIX_FMT_RGB24:
1448 *yuv2packedX = yuv2rgb24_full_X_c;
1450 case AV_PIX_FMT_BGR24:
1451 *yuv2packedX = yuv2bgr24_full_X_c;
1453 case AV_PIX_FMT_GBRP:
1454 case AV_PIX_FMT_GBRP9BE:
1455 case AV_PIX_FMT_GBRP9LE:
1456 case AV_PIX_FMT_GBRP10BE:
1457 case AV_PIX_FMT_GBRP10LE:
1458 case AV_PIX_FMT_GBRP12BE:
1459 case AV_PIX_FMT_GBRP12LE:
1460 case AV_PIX_FMT_GBRAP12BE:
1461 case AV_PIX_FMT_GBRAP12LE:
1462 case AV_PIX_FMT_GBRP16BE:
1463 case AV_PIX_FMT_GBRP16LE:
1464 case AV_PIX_FMT_GBRAP:
1465 *yuv2anyX = yuv2gbrp_full_X_c;
1469 switch (dstFormat) {
1470 case AV_PIX_FMT_RGB48LE:
1471 *yuv2packed1 = yuv2rgb48le_1_c;
1472 *yuv2packed2 = yuv2rgb48le_2_c;
1473 *yuv2packedX = yuv2rgb48le_X_c;
1475 case AV_PIX_FMT_RGB48BE:
1476 *yuv2packed1 = yuv2rgb48be_1_c;
1477 *yuv2packed2 = yuv2rgb48be_2_c;
1478 *yuv2packedX = yuv2rgb48be_X_c;
1480 case AV_PIX_FMT_BGR48LE:
1481 *yuv2packed1 = yuv2bgr48le_1_c;
1482 *yuv2packed2 = yuv2bgr48le_2_c;
1483 *yuv2packedX = yuv2bgr48le_X_c;
1485 case AV_PIX_FMT_BGR48BE:
1486 *yuv2packed1 = yuv2bgr48be_1_c;
1487 *yuv2packed2 = yuv2bgr48be_2_c;
1488 *yuv2packedX = yuv2bgr48be_X_c;
1490 case AV_PIX_FMT_RGB32:
1491 case AV_PIX_FMT_BGR32:
1493 *yuv2packed1 = yuv2rgb32_1_c;
1494 *yuv2packed2 = yuv2rgb32_2_c;
1495 *yuv2packedX = yuv2rgb32_X_c;
1497 #if CONFIG_SWSCALE_ALPHA
1499 *yuv2packed1 = yuv2rgba32_1_c;
1500 *yuv2packed2 = yuv2rgba32_2_c;
1501 *yuv2packedX = yuv2rgba32_X_c;
1503 #endif /* CONFIG_SWSCALE_ALPHA */
1505 *yuv2packed1 = yuv2rgbx32_1_c;
1506 *yuv2packed2 = yuv2rgbx32_2_c;
1507 *yuv2packedX = yuv2rgbx32_X_c;
1509 #endif /* !CONFIG_SMALL */
1511 case AV_PIX_FMT_RGB32_1:
1512 case AV_PIX_FMT_BGR32_1:
1514 *yuv2packed1 = yuv2rgb32_1_1_c;
1515 *yuv2packed2 = yuv2rgb32_1_2_c;
1516 *yuv2packedX = yuv2rgb32_1_X_c;
1518 #if CONFIG_SWSCALE_ALPHA
1520 *yuv2packed1 = yuv2rgba32_1_1_c;
1521 *yuv2packed2 = yuv2rgba32_1_2_c;
1522 *yuv2packedX = yuv2rgba32_1_X_c;
1524 #endif /* CONFIG_SWSCALE_ALPHA */
1526 *yuv2packed1 = yuv2rgbx32_1_1_c;
1527 *yuv2packed2 = yuv2rgbx32_1_2_c;
1528 *yuv2packedX = yuv2rgbx32_1_X_c;
1530 #endif /* !CONFIG_SMALL */
1532 case AV_PIX_FMT_RGB24:
1533 *yuv2packed1 = yuv2rgb24_1_c;
1534 *yuv2packed2 = yuv2rgb24_2_c;
1535 *yuv2packedX = yuv2rgb24_X_c;
1537 case AV_PIX_FMT_BGR24:
1538 *yuv2packed1 = yuv2bgr24_1_c;
1539 *yuv2packed2 = yuv2bgr24_2_c;
1540 *yuv2packedX = yuv2bgr24_X_c;
1542 case AV_PIX_FMT_RGB565LE:
1543 case AV_PIX_FMT_RGB565BE:
1544 case AV_PIX_FMT_BGR565LE:
1545 case AV_PIX_FMT_BGR565BE:
1546 *yuv2packed1 = yuv2rgb16_1_c;
1547 *yuv2packed2 = yuv2rgb16_2_c;
1548 *yuv2packedX = yuv2rgb16_X_c;
1550 case AV_PIX_FMT_RGB555LE:
1551 case AV_PIX_FMT_RGB555BE:
1552 case AV_PIX_FMT_BGR555LE:
1553 case AV_PIX_FMT_BGR555BE:
1554 *yuv2packed1 = yuv2rgb15_1_c;
1555 *yuv2packed2 = yuv2rgb15_2_c;
1556 *yuv2packedX = yuv2rgb15_X_c;
1558 case AV_PIX_FMT_RGB444LE:
1559 case AV_PIX_FMT_RGB444BE:
1560 case AV_PIX_FMT_BGR444LE:
1561 case AV_PIX_FMT_BGR444BE:
1562 *yuv2packed1 = yuv2rgb12_1_c;
1563 *yuv2packed2 = yuv2rgb12_2_c;
1564 *yuv2packedX = yuv2rgb12_X_c;
1566 case AV_PIX_FMT_RGB8:
1567 case AV_PIX_FMT_BGR8:
1568 *yuv2packed1 = yuv2rgb8_1_c;
1569 *yuv2packed2 = yuv2rgb8_2_c;
1570 *yuv2packedX = yuv2rgb8_X_c;
1572 case AV_PIX_FMT_RGB4:
1573 case AV_PIX_FMT_BGR4:
1574 *yuv2packed1 = yuv2rgb4_1_c;
1575 *yuv2packed2 = yuv2rgb4_2_c;
1576 *yuv2packedX = yuv2rgb4_X_c;
1578 case AV_PIX_FMT_RGB4_BYTE:
1579 case AV_PIX_FMT_BGR4_BYTE:
1580 *yuv2packed1 = yuv2rgb4b_1_c;
1581 *yuv2packed2 = yuv2rgb4b_2_c;
1582 *yuv2packedX = yuv2rgb4b_X_c;
1586 switch (dstFormat) {
1587 case AV_PIX_FMT_MONOWHITE:
1588 *yuv2packed1 = yuv2monowhite_1_c;
1589 *yuv2packed2 = yuv2monowhite_2_c;
1590 *yuv2packedX = yuv2monowhite_X_c;
1592 case AV_PIX_FMT_MONOBLACK:
1593 *yuv2packed1 = yuv2monoblack_1_c;
1594 *yuv2packed2 = yuv2monoblack_2_c;
1595 *yuv2packedX = yuv2monoblack_X_c;
1597 case AV_PIX_FMT_YUYV422:
1598 *yuv2packed1 = yuv2yuyv422_1_c;
1599 *yuv2packed2 = yuv2yuyv422_2_c;
1600 *yuv2packedX = yuv2yuyv422_X_c;
1602 case AV_PIX_FMT_YVYU422:
1603 *yuv2packed1 = yuv2yvyu422_1_c;
1604 *yuv2packed2 = yuv2yvyu422_2_c;
1605 *yuv2packedX = yuv2yvyu422_X_c;
1607 case AV_PIX_FMT_UYVY422:
1608 *yuv2packed1 = yuv2uyvy422_1_c;
1609 *yuv2packed2 = yuv2uyvy422_2_c;
1610 *yuv2packedX = yuv2uyvy422_X_c;