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] * 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(16, BE, 1, 16, int32_t)
248 yuv2NBPS(16, LE, 0, 16, int32_t)
250 static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
251 const int16_t **src, uint8_t *dest, int dstW,
252 const uint8_t *dither, int offset)
255 for (i=0; i<dstW; i++) {
256 int val = dither[(i + offset) & 7] << 12;
258 for (j=0; j<filterSize; j++)
259 val += src[j][i] * filter[j];
261 dest[i]= av_clip_uint8(val>>19);
265 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
266 const uint8_t *dither, int offset)
269 for (i=0; i<dstW; i++) {
270 int val = (src[i] + dither[(i + offset) & 7]) >> 7;
271 dest[i]= av_clip_uint8(val);
275 static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
276 const int16_t **chrUSrc, const int16_t **chrVSrc,
277 uint8_t *dest, int chrDstW)
279 enum PixelFormat dstFormat = c->dstFormat;
280 const uint8_t *chrDither = c->chrDither8;
283 if (dstFormat == PIX_FMT_NV12)
284 for (i=0; i<chrDstW; i++) {
285 int u = chrDither[i & 7] << 12;
286 int v = chrDither[(i + 3) & 7] << 12;
288 for (j=0; j<chrFilterSize; j++) {
289 u += chrUSrc[j][i] * chrFilter[j];
290 v += chrVSrc[j][i] * chrFilter[j];
293 dest[2*i]= av_clip_uint8(u>>19);
294 dest[2*i+1]= av_clip_uint8(v>>19);
297 for (i=0; i<chrDstW; i++) {
298 int u = chrDither[i & 7] << 12;
299 int v = chrDither[(i + 3) & 7] << 12;
301 for (j=0; j<chrFilterSize; j++) {
302 u += chrUSrc[j][i] * chrFilter[j];
303 v += chrVSrc[j][i] * chrFilter[j];
306 dest[2*i]= av_clip_uint8(v>>19);
307 dest[2*i+1]= av_clip_uint8(u>>19);
311 #define accumulate_bit(acc, val) \
313 acc |= (val) >= (128 + 110)
314 #define output_pixel(pos, acc) \
315 if (target == PIX_FMT_MONOBLACK) { \
321 static av_always_inline void
322 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
323 const int16_t **lumSrc, int lumFilterSize,
324 const int16_t *chrFilter, const int16_t **chrUSrc,
325 const int16_t **chrVSrc, int chrFilterSize,
326 const int16_t **alpSrc, uint8_t *dest, int dstW,
327 int y, enum PixelFormat target)
329 const uint8_t * const d128=dither_8x8_220[y&7];
333 for (i = 0; i < dstW; i += 2) {
338 for (j = 0; j < lumFilterSize; j++) {
339 Y1 += lumSrc[j][i] * lumFilter[j];
340 Y2 += lumSrc[j][i+1] * lumFilter[j];
344 if ((Y1 | Y2) & 0x100) {
345 Y1 = av_clip_uint8(Y1);
346 Y2 = av_clip_uint8(Y2);
348 accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
349 accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
351 output_pixel(*dest++, acc);
356 output_pixel(*dest, acc);
360 static av_always_inline void
361 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
362 const int16_t *ubuf[2], const int16_t *vbuf[2],
363 const int16_t *abuf[2], uint8_t *dest, int dstW,
364 int yalpha, int uvalpha, int y,
365 enum PixelFormat target)
367 const int16_t *buf0 = buf[0], *buf1 = buf[1];
368 const uint8_t * const d128 = dither_8x8_220[y & 7];
369 int yalpha1 = 4096 - yalpha;
372 for (i = 0; i < dstW; i += 8) {
375 Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
376 accumulate_bit(acc, Y + d128[0]);
377 Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
378 accumulate_bit(acc, Y + d128[1]);
379 Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
380 accumulate_bit(acc, Y + d128[2]);
381 Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
382 accumulate_bit(acc, Y + d128[3]);
383 Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
384 accumulate_bit(acc, Y + d128[4]);
385 Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
386 accumulate_bit(acc, Y + d128[5]);
387 Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
388 accumulate_bit(acc, Y + d128[6]);
389 Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
390 accumulate_bit(acc, Y + d128[7]);
392 output_pixel(*dest++, acc);
396 static av_always_inline void
397 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
398 const int16_t *ubuf[2], const int16_t *vbuf[2],
399 const int16_t *abuf0, uint8_t *dest, int dstW,
400 int uvalpha, int y, enum PixelFormat target)
402 const uint8_t * const d128 = dither_8x8_220[y & 7];
405 for (i = 0; i < dstW; i += 8) {
408 accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
409 accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
410 accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
411 accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
412 accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
413 accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
414 accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
415 accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
417 output_pixel(*dest++, acc);
422 #undef accumulate_bit
424 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
425 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
426 const int16_t **lumSrc, int lumFilterSize, \
427 const int16_t *chrFilter, const int16_t **chrUSrc, \
428 const int16_t **chrVSrc, int chrFilterSize, \
429 const int16_t **alpSrc, uint8_t *dest, int dstW, \
432 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
433 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
434 alpSrc, dest, dstW, y, fmt); \
437 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
438 const int16_t *ubuf[2], const int16_t *vbuf[2], \
439 const int16_t *abuf[2], uint8_t *dest, int dstW, \
440 int yalpha, int uvalpha, int y) \
442 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
443 dest, dstW, yalpha, uvalpha, y, fmt); \
446 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
447 const int16_t *ubuf[2], const int16_t *vbuf[2], \
448 const int16_t *abuf0, uint8_t *dest, int dstW, \
449 int uvalpha, int y) \
451 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
452 abuf0, dest, dstW, uvalpha, \
456 YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE)
457 YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK)
459 #define output_pixels(pos, Y1, U, Y2, V) \
460 if (target == PIX_FMT_YUYV422) { \
461 dest[pos + 0] = Y1; \
463 dest[pos + 2] = Y2; \
467 dest[pos + 1] = Y1; \
469 dest[pos + 3] = Y2; \
472 static av_always_inline void
473 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
474 const int16_t **lumSrc, int lumFilterSize,
475 const int16_t *chrFilter, const int16_t **chrUSrc,
476 const int16_t **chrVSrc, int chrFilterSize,
477 const int16_t **alpSrc, uint8_t *dest, int dstW,
478 int y, enum PixelFormat target)
482 for (i = 0; i < ((dstW + 1) >> 1); i++) {
489 for (j = 0; j < lumFilterSize; j++) {
490 Y1 += lumSrc[j][i * 2] * lumFilter[j];
491 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
493 for (j = 0; j < chrFilterSize; j++) {
494 U += chrUSrc[j][i] * chrFilter[j];
495 V += chrVSrc[j][i] * chrFilter[j];
501 if ((Y1 | Y2 | U | V) & 0x100) {
502 Y1 = av_clip_uint8(Y1);
503 Y2 = av_clip_uint8(Y2);
504 U = av_clip_uint8(U);
505 V = av_clip_uint8(V);
507 output_pixels(4*i, Y1, U, Y2, V);
511 static av_always_inline void
512 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
513 const int16_t *ubuf[2], const int16_t *vbuf[2],
514 const int16_t *abuf[2], uint8_t *dest, int dstW,
515 int yalpha, int uvalpha, int y,
516 enum PixelFormat target)
518 const int16_t *buf0 = buf[0], *buf1 = buf[1],
519 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
520 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
521 int yalpha1 = 4096 - yalpha;
522 int uvalpha1 = 4096 - uvalpha;
525 for (i = 0; i < ((dstW + 1) >> 1); i++) {
526 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
527 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
528 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
529 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
531 if ((Y1 | Y2 | U | V) & 0x100) {
532 Y1 = av_clip_uint8(Y1);
533 Y2 = av_clip_uint8(Y2);
534 U = av_clip_uint8(U);
535 V = av_clip_uint8(V);
538 output_pixels(i * 4, Y1, U, Y2, V);
542 static av_always_inline void
543 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
544 const int16_t *ubuf[2], const int16_t *vbuf[2],
545 const int16_t *abuf0, uint8_t *dest, int dstW,
546 int uvalpha, int y, enum PixelFormat target)
548 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
551 if (uvalpha < 2048) {
552 for (i = 0; i < ((dstW + 1) >> 1); i++) {
553 int Y1 = (buf0[i * 2 ]+64) >> 7;
554 int Y2 = (buf0[i * 2 + 1]+64) >> 7;
555 int U = (ubuf0[i] +64) >> 7;
556 int V = (vbuf0[i] +64) >> 7;
558 if ((Y1 | Y2 | U | V) & 0x100) {
559 Y1 = av_clip_uint8(Y1);
560 Y2 = av_clip_uint8(Y2);
561 U = av_clip_uint8(U);
562 V = av_clip_uint8(V);
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);
573 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
574 for (i = 0; i < ((dstW + 1) >> 1); i++) {
575 int Y1 = (buf0[i * 2 ] + 64) >> 7;
576 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
577 int U = (ubuf0[i] + ubuf1[i]+128) >> 8;
578 int V = (vbuf0[i] + vbuf1[i]+128) >> 8;
580 if ((Y1 | Y2 | U | V) & 0x100) {
581 Y1 = av_clip_uint8(Y1);
582 Y2 = av_clip_uint8(Y2);
583 U = av_clip_uint8(U);
584 V = av_clip_uint8(V);
587 Y1 = av_clip_uint8(Y1);
588 Y2 = av_clip_uint8(Y2);
589 U = av_clip_uint8(U);
590 V = av_clip_uint8(V);
592 output_pixels(i * 4, Y1, U, Y2, V);
599 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422)
600 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422)
602 #define R_B ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? R : B)
603 #define B_R ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? B : R)
604 #define output_pixel(pos, val) \
605 if (isBE(target)) { \
611 static av_always_inline void
612 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
613 const int32_t **lumSrc, int lumFilterSize,
614 const int16_t *chrFilter, const int32_t **chrUSrc,
615 const int32_t **chrVSrc, int chrFilterSize,
616 const int32_t **alpSrc, uint16_t *dest, int dstW,
617 int y, enum PixelFormat target)
621 for (i = 0; i < ((dstW + 1) >> 1); i++) {
623 int Y1 = -0x40000000;
624 int Y2 = -0x40000000;
625 int U = -128 << 23; // 19
629 for (j = 0; j < lumFilterSize; j++) {
630 Y1 += lumSrc[j][i * 2] * lumFilter[j];
631 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
633 for (j = 0; j < chrFilterSize; j++) {
634 U += chrUSrc[j][i] * chrFilter[j];
635 V += chrVSrc[j][i] * chrFilter[j];
638 // 8bit: 12+15=27; 16-bit: 12+19=31
646 // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
647 Y1 -= c->yuv2rgb_y_offset;
648 Y2 -= c->yuv2rgb_y_offset;
649 Y1 *= c->yuv2rgb_y_coeff;
650 Y2 *= c->yuv2rgb_y_coeff;
653 // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
655 R = V * c->yuv2rgb_v2r_coeff;
656 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
657 B = U * c->yuv2rgb_u2b_coeff;
659 // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
660 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
661 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
662 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
663 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
664 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
665 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
670 static av_always_inline void
671 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
672 const int32_t *ubuf[2], const int32_t *vbuf[2],
673 const int32_t *abuf[2], uint16_t *dest, int dstW,
674 int yalpha, int uvalpha, int y,
675 enum PixelFormat target)
677 const int32_t *buf0 = buf[0], *buf1 = buf[1],
678 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
679 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
680 int yalpha1 = 4096 - yalpha;
681 int uvalpha1 = 4096 - uvalpha;
684 for (i = 0; i < ((dstW + 1) >> 1); i++) {
685 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
686 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
687 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
688 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
691 Y1 -= c->yuv2rgb_y_offset;
692 Y2 -= c->yuv2rgb_y_offset;
693 Y1 *= c->yuv2rgb_y_coeff;
694 Y2 *= c->yuv2rgb_y_coeff;
698 R = V * c->yuv2rgb_v2r_coeff;
699 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
700 B = U * c->yuv2rgb_u2b_coeff;
702 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
703 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
704 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
705 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
706 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
707 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
712 static av_always_inline void
713 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
714 const int32_t *ubuf[2], const int32_t *vbuf[2],
715 const int32_t *abuf0, uint16_t *dest, int dstW,
716 int uvalpha, int y, enum PixelFormat target)
718 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
721 if (uvalpha < 2048) {
722 for (i = 0; i < ((dstW + 1) >> 1); i++) {
723 int Y1 = (buf0[i * 2] ) >> 2;
724 int Y2 = (buf0[i * 2 + 1]) >> 2;
725 int U = (ubuf0[i] + (-128 << 11)) >> 2;
726 int V = (vbuf0[i] + (-128 << 11)) >> 2;
729 Y1 -= c->yuv2rgb_y_offset;
730 Y2 -= c->yuv2rgb_y_offset;
731 Y1 *= c->yuv2rgb_y_coeff;
732 Y2 *= c->yuv2rgb_y_coeff;
736 R = V * c->yuv2rgb_v2r_coeff;
737 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
738 B = U * c->yuv2rgb_u2b_coeff;
740 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
741 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
742 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
743 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
744 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
745 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
749 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
750 for (i = 0; i < ((dstW + 1) >> 1); i++) {
751 int Y1 = (buf0[i * 2] ) >> 2;
752 int Y2 = (buf0[i * 2 + 1]) >> 2;
753 int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
754 int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
757 Y1 -= c->yuv2rgb_y_offset;
758 Y2 -= c->yuv2rgb_y_offset;
759 Y1 *= c->yuv2rgb_y_coeff;
760 Y2 *= c->yuv2rgb_y_coeff;
764 R = V * c->yuv2rgb_v2r_coeff;
765 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
766 B = U * c->yuv2rgb_u2b_coeff;
768 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
769 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
770 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
771 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
772 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
773 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
783 #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
784 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
785 const int16_t **_lumSrc, int lumFilterSize, \
786 const int16_t *chrFilter, const int16_t **_chrUSrc, \
787 const int16_t **_chrVSrc, int chrFilterSize, \
788 const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
791 const int32_t **lumSrc = (const int32_t **) _lumSrc, \
792 **chrUSrc = (const int32_t **) _chrUSrc, \
793 **chrVSrc = (const int32_t **) _chrVSrc, \
794 **alpSrc = (const int32_t **) _alpSrc; \
795 uint16_t *dest = (uint16_t *) _dest; \
796 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
797 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
798 alpSrc, dest, dstW, y, fmt); \
801 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
802 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
803 const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
804 int yalpha, int uvalpha, int y) \
806 const int32_t **buf = (const int32_t **) _buf, \
807 **ubuf = (const int32_t **) _ubuf, \
808 **vbuf = (const int32_t **) _vbuf, \
809 **abuf = (const int32_t **) _abuf; \
810 uint16_t *dest = (uint16_t *) _dest; \
811 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
812 dest, dstW, yalpha, uvalpha, y, fmt); \
815 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
816 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
817 const int16_t *_abuf0, uint8_t *_dest, int dstW, \
818 int uvalpha, int y) \
820 const int32_t *buf0 = (const int32_t *) _buf0, \
821 **ubuf = (const int32_t **) _ubuf, \
822 **vbuf = (const int32_t **) _vbuf, \
823 *abuf0 = (const int32_t *) _abuf0; \
824 uint16_t *dest = (uint16_t *) _dest; \
825 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
826 dstW, uvalpha, y, fmt); \
829 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, PIX_FMT_RGB48BE)
830 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, PIX_FMT_RGB48LE)
831 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, PIX_FMT_BGR48BE)
832 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, PIX_FMT_BGR48LE)
835 * Write out 2 RGB pixels in the target pixel format. This function takes a
836 * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
837 * things like endianness conversion and shifting. The caller takes care of
838 * setting the correct offset in these tables from the chroma (U/V) values.
839 * This function then uses the luminance (Y1/Y2) values to write out the
840 * correct RGB values into the destination buffer.
842 static av_always_inline void
843 yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
844 unsigned A1, unsigned A2,
845 const void *_r, const void *_g, const void *_b, int y,
846 enum PixelFormat target, int hasAlpha)
848 if (target == PIX_FMT_ARGB || target == PIX_FMT_RGBA ||
849 target == PIX_FMT_ABGR || target == PIX_FMT_BGRA) {
850 uint32_t *dest = (uint32_t *) _dest;
851 const uint32_t *r = (const uint32_t *) _r;
852 const uint32_t *g = (const uint32_t *) _g;
853 const uint32_t *b = (const uint32_t *) _b;
856 int sh = hasAlpha ? ((target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24) : 0;
858 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
859 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
862 int sh = (target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24;
864 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
865 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
867 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
868 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
871 } else if (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) {
872 uint8_t *dest = (uint8_t *) _dest;
873 const uint8_t *r = (const uint8_t *) _r;
874 const uint8_t *g = (const uint8_t *) _g;
875 const uint8_t *b = (const uint8_t *) _b;
877 #define r_b ((target == PIX_FMT_RGB24) ? r : b)
878 #define b_r ((target == PIX_FMT_RGB24) ? b : r)
880 dest[i * 6 + 0] = r_b[Y1];
881 dest[i * 6 + 1] = g[Y1];
882 dest[i * 6 + 2] = b_r[Y1];
883 dest[i * 6 + 3] = r_b[Y2];
884 dest[i * 6 + 4] = g[Y2];
885 dest[i * 6 + 5] = b_r[Y2];
888 } else if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565 ||
889 target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555 ||
890 target == PIX_FMT_RGB444 || target == PIX_FMT_BGR444) {
891 uint16_t *dest = (uint16_t *) _dest;
892 const uint16_t *r = (const uint16_t *) _r;
893 const uint16_t *g = (const uint16_t *) _g;
894 const uint16_t *b = (const uint16_t *) _b;
895 int dr1, dg1, db1, dr2, dg2, db2;
897 if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565) {
898 dr1 = dither_2x2_8[ y & 1 ][0];
899 dg1 = dither_2x2_4[ y & 1 ][0];
900 db1 = dither_2x2_8[(y & 1) ^ 1][0];
901 dr2 = dither_2x2_8[ y & 1 ][1];
902 dg2 = dither_2x2_4[ y & 1 ][1];
903 db2 = dither_2x2_8[(y & 1) ^ 1][1];
904 } else if (target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555) {
905 dr1 = dither_2x2_8[ y & 1 ][0];
906 dg1 = dither_2x2_8[ y & 1 ][1];
907 db1 = dither_2x2_8[(y & 1) ^ 1][0];
908 dr2 = dither_2x2_8[ y & 1 ][1];
909 dg2 = dither_2x2_8[ y & 1 ][0];
910 db2 = dither_2x2_8[(y & 1) ^ 1][1];
912 dr1 = dither_4x4_16[ y & 3 ][0];
913 dg1 = dither_4x4_16[ y & 3 ][1];
914 db1 = dither_4x4_16[(y & 3) ^ 3][0];
915 dr2 = dither_4x4_16[ y & 3 ][1];
916 dg2 = dither_4x4_16[ y & 3 ][0];
917 db2 = dither_4x4_16[(y & 3) ^ 3][1];
920 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
921 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
922 } else /* 8/4-bit */ {
923 uint8_t *dest = (uint8_t *) _dest;
924 const uint8_t *r = (const uint8_t *) _r;
925 const uint8_t *g = (const uint8_t *) _g;
926 const uint8_t *b = (const uint8_t *) _b;
927 int dr1, dg1, db1, dr2, dg2, db2;
929 if (target == PIX_FMT_RGB8 || target == PIX_FMT_BGR8) {
930 const uint8_t * const d64 = dither_8x8_73[y & 7];
931 const uint8_t * const d32 = dither_8x8_32[y & 7];
932 dr1 = dg1 = d32[(i * 2 + 0) & 7];
933 db1 = d64[(i * 2 + 0) & 7];
934 dr2 = dg2 = d32[(i * 2 + 1) & 7];
935 db2 = d64[(i * 2 + 1) & 7];
937 const uint8_t * const d64 = dither_8x8_73 [y & 7];
938 const uint8_t * const d128 = dither_8x8_220[y & 7];
939 dr1 = db1 = d128[(i * 2 + 0) & 7];
940 dg1 = d64[(i * 2 + 0) & 7];
941 dr2 = db2 = d128[(i * 2 + 1) & 7];
942 dg2 = d64[(i * 2 + 1) & 7];
945 if (target == PIX_FMT_RGB4 || target == PIX_FMT_BGR4) {
946 dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
947 ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
949 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
950 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
955 static av_always_inline void
956 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
957 const int16_t **lumSrc, int lumFilterSize,
958 const int16_t *chrFilter, const int16_t **chrUSrc,
959 const int16_t **chrVSrc, int chrFilterSize,
960 const int16_t **alpSrc, uint8_t *dest, int dstW,
961 int y, enum PixelFormat target, int hasAlpha)
965 for (i = 0; i < ((dstW + 1) >> 1); i++) {
971 const void *r, *g, *b;
973 for (j = 0; j < lumFilterSize; j++) {
974 Y1 += lumSrc[j][i * 2] * lumFilter[j];
975 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
977 for (j = 0; j < chrFilterSize; j++) {
978 U += chrUSrc[j][i] * chrFilter[j];
979 V += chrVSrc[j][i] * chrFilter[j];
988 for (j = 0; j < lumFilterSize; j++) {
989 A1 += alpSrc[j][i * 2 ] * lumFilter[j];
990 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
994 if ((A1 | A2) & 0x100) {
995 A1 = av_clip_uint8(A1);
996 A2 = av_clip_uint8(A2);
1000 r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
1001 g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
1002 b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1004 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1005 r, g, b, y, target, hasAlpha);
1009 static av_always_inline void
1010 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
1011 const int16_t *ubuf[2], const int16_t *vbuf[2],
1012 const int16_t *abuf[2], uint8_t *dest, int dstW,
1013 int yalpha, int uvalpha, int y,
1014 enum PixelFormat target, int hasAlpha)
1016 const int16_t *buf0 = buf[0], *buf1 = buf[1],
1017 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1018 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1019 *abuf0 = hasAlpha ? abuf[0] : NULL,
1020 *abuf1 = hasAlpha ? abuf[1] : NULL;
1021 int yalpha1 = 4096 - yalpha;
1022 int uvalpha1 = 4096 - uvalpha;
1025 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1026 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
1027 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
1028 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
1029 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
1031 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1032 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1033 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1036 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
1037 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1038 A1 = av_clip_uint8(A1);
1039 A2 = av_clip_uint8(A2);
1042 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1043 r, g, b, y, target, hasAlpha);
1047 static av_always_inline void
1048 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1049 const int16_t *ubuf[2], const int16_t *vbuf[2],
1050 const int16_t *abuf0, uint8_t *dest, int dstW,
1051 int uvalpha, int y, enum PixelFormat target,
1054 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1057 if (uvalpha < 2048) {
1058 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1059 int Y1 = (buf0[i * 2 ] + 64) >> 7;
1060 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1061 int U = (ubuf0[i] + 64) >> 7;
1062 int V = (vbuf0[i] + 64) >> 7;
1064 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1065 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1066 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1069 A1 = abuf0[i * 2 ] * 255 + 16384 >> 15;
1070 A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
1071 A1 = av_clip_uint8(A1);
1072 A2 = av_clip_uint8(A2);
1075 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1076 r, g, b, y, target, hasAlpha);
1079 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1080 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1081 int Y1 = (buf0[i * 2 ] + 64) >> 7;
1082 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1083 int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
1084 int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
1086 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1087 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1088 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1091 A1 = (abuf0[i * 2 ] + 64) >> 7;
1092 A2 = (abuf0[i * 2 + 1] + 64) >> 7;
1093 A1 = av_clip_uint8(A1);
1094 A2 = av_clip_uint8(A2);
1097 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1098 r, g, b, y, target, hasAlpha);
1103 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1104 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1105 const int16_t **lumSrc, int lumFilterSize, \
1106 const int16_t *chrFilter, const int16_t **chrUSrc, \
1107 const int16_t **chrVSrc, int chrFilterSize, \
1108 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1111 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1112 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1113 alpSrc, dest, dstW, y, fmt, hasAlpha); \
1115 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1116 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1117 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1118 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1119 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1120 int yalpha, int uvalpha, int y) \
1122 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1123 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1126 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1127 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1128 const int16_t *abuf0, uint8_t *dest, int dstW, \
1129 int uvalpha, int y) \
1131 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1132 dstW, uvalpha, y, fmt, hasAlpha); \
1136 YUV2RGBWRAPPER(yuv2rgb,, 32_1, PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1137 YUV2RGBWRAPPER(yuv2rgb,, 32, PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1139 #if CONFIG_SWSCALE_ALPHA
1140 YUV2RGBWRAPPER(yuv2rgb,, a32_1, PIX_FMT_RGB32_1, 1)
1141 YUV2RGBWRAPPER(yuv2rgb,, a32, PIX_FMT_RGB32, 1)
1143 YUV2RGBWRAPPER(yuv2rgb,, x32_1, PIX_FMT_RGB32_1, 0)
1144 YUV2RGBWRAPPER(yuv2rgb,, x32, PIX_FMT_RGB32, 0)
1146 YUV2RGBWRAPPER(yuv2, rgb, rgb24, PIX_FMT_RGB24, 0)
1147 YUV2RGBWRAPPER(yuv2, rgb, bgr24, PIX_FMT_BGR24, 0)
1148 YUV2RGBWRAPPER(yuv2rgb,, 16, PIX_FMT_RGB565, 0)
1149 YUV2RGBWRAPPER(yuv2rgb,, 15, PIX_FMT_RGB555, 0)
1150 YUV2RGBWRAPPER(yuv2rgb,, 12, PIX_FMT_RGB444, 0)
1151 YUV2RGBWRAPPER(yuv2rgb,, 8, PIX_FMT_RGB8, 0)
1152 YUV2RGBWRAPPER(yuv2rgb,, 4, PIX_FMT_RGB4, 0)
1153 YUV2RGBWRAPPER(yuv2rgb,, 4b, PIX_FMT_RGB4_BYTE, 0)
1155 static av_always_inline void
1156 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1157 const int16_t **lumSrc, int lumFilterSize,
1158 const int16_t *chrFilter, const int16_t **chrUSrc,
1159 const int16_t **chrVSrc, int chrFilterSize,
1160 const int16_t **alpSrc, uint8_t *dest,
1161 int dstW, int y, enum PixelFormat target, int hasAlpha)
1164 int step = (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) ? 3 : 4;
1166 for (i = 0; i < dstW; i++) {
1169 int U = (1<<9)-(128 << 19);
1170 int V = (1<<9)-(128 << 19);
1173 for (j = 0; j < lumFilterSize; j++) {
1174 Y += lumSrc[j][i] * lumFilter[j];
1176 for (j = 0; j < chrFilterSize; j++) {
1177 U += chrUSrc[j][i] * chrFilter[j];
1178 V += chrVSrc[j][i] * chrFilter[j];
1185 for (j = 0; j < lumFilterSize; j++) {
1186 A += alpSrc[j][i] * lumFilter[j];
1190 A = av_clip_uint8(A);
1192 Y -= c->yuv2rgb_y_offset;
1193 Y *= c->yuv2rgb_y_coeff;
1195 R = Y + V*c->yuv2rgb_v2r_coeff;
1196 G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1197 B = Y + U*c->yuv2rgb_u2b_coeff;
1198 if ((R | G | B) & 0xC0000000) {
1199 R = av_clip_uintp2(R, 30);
1200 G = av_clip_uintp2(G, 30);
1201 B = av_clip_uintp2(B, 30);
1206 dest[0] = hasAlpha ? A : 255;
1220 dest[3] = hasAlpha ? A : 255;
1223 dest[0] = hasAlpha ? A : 255;
1237 dest[3] = hasAlpha ? A : 255;
1245 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1246 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1247 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1248 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1250 #if CONFIG_SWSCALE_ALPHA
1251 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, 1)
1252 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, 1)
1253 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, 1)
1254 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, 1)
1256 YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, PIX_FMT_BGRA, 0)
1257 YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, PIX_FMT_ABGR, 0)
1258 YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, PIX_FMT_RGBA, 0)
1259 YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, PIX_FMT_ARGB, 0)
1261 YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, PIX_FMT_BGR24, 0)
1262 YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, PIX_FMT_RGB24, 0)
1264 av_cold void ff_sws_init_output_funcs(SwsContext *c,
1265 yuv2planar1_fn *yuv2plane1,
1266 yuv2planarX_fn *yuv2planeX,
1267 yuv2interleavedX_fn *yuv2nv12cX,
1268 yuv2packed1_fn *yuv2packed1,
1269 yuv2packed2_fn *yuv2packed2,
1270 yuv2packedX_fn *yuv2packedX)
1272 enum PixelFormat dstFormat = c->dstFormat;
1274 if (is16BPS(dstFormat)) {
1275 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
1276 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
1277 } else if (is9_OR_10BPS(dstFormat)) {
1278 if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
1279 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
1280 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
1282 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
1283 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
1286 *yuv2plane1 = yuv2plane1_8_c;
1287 *yuv2planeX = yuv2planeX_8_c;
1288 if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)
1289 *yuv2nv12cX = yuv2nv12cX_c;
1292 if(c->flags & SWS_FULL_CHR_H_INT) {
1293 switch (dstFormat) {
1296 *yuv2packedX = yuv2rgba32_full_X_c;
1298 #if CONFIG_SWSCALE_ALPHA
1300 *yuv2packedX = yuv2rgba32_full_X_c;
1302 #endif /* CONFIG_SWSCALE_ALPHA */
1304 *yuv2packedX = yuv2rgbx32_full_X_c;
1306 #endif /* !CONFIG_SMALL */
1310 *yuv2packedX = yuv2argb32_full_X_c;
1312 #if CONFIG_SWSCALE_ALPHA
1314 *yuv2packedX = yuv2argb32_full_X_c;
1316 #endif /* CONFIG_SWSCALE_ALPHA */
1318 *yuv2packedX = yuv2xrgb32_full_X_c;
1320 #endif /* !CONFIG_SMALL */
1324 *yuv2packedX = yuv2bgra32_full_X_c;
1326 #if CONFIG_SWSCALE_ALPHA
1328 *yuv2packedX = yuv2bgra32_full_X_c;
1330 #endif /* CONFIG_SWSCALE_ALPHA */
1332 *yuv2packedX = yuv2bgrx32_full_X_c;
1334 #endif /* !CONFIG_SMALL */
1338 *yuv2packedX = yuv2abgr32_full_X_c;
1340 #if CONFIG_SWSCALE_ALPHA
1342 *yuv2packedX = yuv2abgr32_full_X_c;
1344 #endif /* CONFIG_SWSCALE_ALPHA */
1346 *yuv2packedX = yuv2xbgr32_full_X_c;
1348 #endif /* !CONFIG_SMALL */
1351 *yuv2packedX = yuv2rgb24_full_X_c;
1354 *yuv2packedX = yuv2bgr24_full_X_c;
1361 switch (dstFormat) {
1362 case PIX_FMT_RGB48LE:
1363 *yuv2packed1 = yuv2rgb48le_1_c;
1364 *yuv2packed2 = yuv2rgb48le_2_c;
1365 *yuv2packedX = yuv2rgb48le_X_c;
1367 case PIX_FMT_RGB48BE:
1368 *yuv2packed1 = yuv2rgb48be_1_c;
1369 *yuv2packed2 = yuv2rgb48be_2_c;
1370 *yuv2packedX = yuv2rgb48be_X_c;
1372 case PIX_FMT_BGR48LE:
1373 *yuv2packed1 = yuv2bgr48le_1_c;
1374 *yuv2packed2 = yuv2bgr48le_2_c;
1375 *yuv2packedX = yuv2bgr48le_X_c;
1377 case PIX_FMT_BGR48BE:
1378 *yuv2packed1 = yuv2bgr48be_1_c;
1379 *yuv2packed2 = yuv2bgr48be_2_c;
1380 *yuv2packedX = yuv2bgr48be_X_c;
1385 *yuv2packed1 = yuv2rgb32_1_c;
1386 *yuv2packed2 = yuv2rgb32_2_c;
1387 *yuv2packedX = yuv2rgb32_X_c;
1389 #if CONFIG_SWSCALE_ALPHA
1391 *yuv2packed1 = yuv2rgba32_1_c;
1392 *yuv2packed2 = yuv2rgba32_2_c;
1393 *yuv2packedX = yuv2rgba32_X_c;
1395 #endif /* CONFIG_SWSCALE_ALPHA */
1397 *yuv2packed1 = yuv2rgbx32_1_c;
1398 *yuv2packed2 = yuv2rgbx32_2_c;
1399 *yuv2packedX = yuv2rgbx32_X_c;
1401 #endif /* !CONFIG_SMALL */
1403 case PIX_FMT_RGB32_1:
1404 case PIX_FMT_BGR32_1:
1406 *yuv2packed1 = yuv2rgb32_1_1_c;
1407 *yuv2packed2 = yuv2rgb32_1_2_c;
1408 *yuv2packedX = yuv2rgb32_1_X_c;
1410 #if CONFIG_SWSCALE_ALPHA
1412 *yuv2packed1 = yuv2rgba32_1_1_c;
1413 *yuv2packed2 = yuv2rgba32_1_2_c;
1414 *yuv2packedX = yuv2rgba32_1_X_c;
1416 #endif /* CONFIG_SWSCALE_ALPHA */
1418 *yuv2packed1 = yuv2rgbx32_1_1_c;
1419 *yuv2packed2 = yuv2rgbx32_1_2_c;
1420 *yuv2packedX = yuv2rgbx32_1_X_c;
1422 #endif /* !CONFIG_SMALL */
1425 *yuv2packed1 = yuv2rgb24_1_c;
1426 *yuv2packed2 = yuv2rgb24_2_c;
1427 *yuv2packedX = yuv2rgb24_X_c;
1430 *yuv2packed1 = yuv2bgr24_1_c;
1431 *yuv2packed2 = yuv2bgr24_2_c;
1432 *yuv2packedX = yuv2bgr24_X_c;
1434 case PIX_FMT_RGB565LE:
1435 case PIX_FMT_RGB565BE:
1436 case PIX_FMT_BGR565LE:
1437 case PIX_FMT_BGR565BE:
1438 *yuv2packed1 = yuv2rgb16_1_c;
1439 *yuv2packed2 = yuv2rgb16_2_c;
1440 *yuv2packedX = yuv2rgb16_X_c;
1442 case PIX_FMT_RGB555LE:
1443 case PIX_FMT_RGB555BE:
1444 case PIX_FMT_BGR555LE:
1445 case PIX_FMT_BGR555BE:
1446 *yuv2packed1 = yuv2rgb15_1_c;
1447 *yuv2packed2 = yuv2rgb15_2_c;
1448 *yuv2packedX = yuv2rgb15_X_c;
1450 case PIX_FMT_RGB444LE:
1451 case PIX_FMT_RGB444BE:
1452 case PIX_FMT_BGR444LE:
1453 case PIX_FMT_BGR444BE:
1454 *yuv2packed1 = yuv2rgb12_1_c;
1455 *yuv2packed2 = yuv2rgb12_2_c;
1456 *yuv2packedX = yuv2rgb12_X_c;
1460 *yuv2packed1 = yuv2rgb8_1_c;
1461 *yuv2packed2 = yuv2rgb8_2_c;
1462 *yuv2packedX = yuv2rgb8_X_c;
1466 *yuv2packed1 = yuv2rgb4_1_c;
1467 *yuv2packed2 = yuv2rgb4_2_c;
1468 *yuv2packedX = yuv2rgb4_X_c;
1470 case PIX_FMT_RGB4_BYTE:
1471 case PIX_FMT_BGR4_BYTE:
1472 *yuv2packed1 = yuv2rgb4b_1_c;
1473 *yuv2packed2 = yuv2rgb4b_2_c;
1474 *yuv2packedX = yuv2rgb4b_X_c;
1478 switch (dstFormat) {
1479 case PIX_FMT_MONOWHITE:
1480 *yuv2packed1 = yuv2monowhite_1_c;
1481 *yuv2packed2 = yuv2monowhite_2_c;
1482 *yuv2packedX = yuv2monowhite_X_c;
1484 case PIX_FMT_MONOBLACK:
1485 *yuv2packed1 = yuv2monoblack_1_c;
1486 *yuv2packed2 = yuv2monoblack_2_c;
1487 *yuv2packedX = yuv2monoblack_X_c;
1489 case PIX_FMT_YUYV422:
1490 *yuv2packed1 = yuv2yuyv422_1_c;
1491 *yuv2packed2 = yuv2yuyv422_2_c;
1492 *yuv2packedX = yuv2yuyv422_X_c;
1494 case PIX_FMT_UYVY422:
1495 *yuv2packed1 = yuv2uyvy422_1_c;
1496 *yuv2packed2 = yuv2uyvy422_2_c;
1497 *yuv2packedX = yuv2uyvy422_X_c;