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];
338 for (i = 0; i < dstW; i += 2) {
343 for (j = 0; j < lumFilterSize; j++) {
344 Y1 += lumSrc[j][i] * lumFilter[j];
345 Y2 += lumSrc[j][i+1] * lumFilter[j];
349 if ((Y1 | Y2) & 0x100) {
350 Y1 = av_clip_uint8(Y1);
351 Y2 = av_clip_uint8(Y2);
353 if (c->flags & SWS_ERROR_DIFFUSION) {
354 Y1 += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
355 c->dither_error[0][i] = err;
356 acc = 2*acc + (Y1 >= 128);
359 err = Y2 + ((7*Y1 + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4);
360 c->dither_error[0][i+1] = Y1;
361 acc = 2*acc + (err >= 128);
364 accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
365 accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
368 output_pixel(*dest++, acc);
371 c->dither_error[0][i] = err;
374 output_pixel(*dest, acc);
378 static av_always_inline void
379 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
380 const int16_t *ubuf[2], const int16_t *vbuf[2],
381 const int16_t *abuf[2], uint8_t *dest, int dstW,
382 int yalpha, int uvalpha, int y,
383 enum AVPixelFormat target)
385 const int16_t *buf0 = buf[0], *buf1 = buf[1];
386 const uint8_t * const d128 = dither_8x8_220[y & 7];
387 int yalpha1 = 4096 - yalpha;
390 if (c->flags & SWS_ERROR_DIFFUSION) {
393 for (i = 0; i < dstW; i +=2) {
396 Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
397 Y += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
398 c->dither_error[0][i] = err;
399 acc = 2*acc + (Y >= 128);
402 err = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
403 err += (7*Y + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4;
404 c->dither_error[0][i+1] = Y;
405 acc = 2*acc + (err >= 128);
409 output_pixel(*dest++, acc);
411 c->dither_error[0][i] = err;
413 for (i = 0; i < dstW; i += 8) {
416 Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
417 accumulate_bit(acc, Y + d128[0]);
418 Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
419 accumulate_bit(acc, Y + d128[1]);
420 Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
421 accumulate_bit(acc, Y + d128[2]);
422 Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
423 accumulate_bit(acc, Y + d128[3]);
424 Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
425 accumulate_bit(acc, Y + d128[4]);
426 Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
427 accumulate_bit(acc, Y + d128[5]);
428 Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
429 accumulate_bit(acc, Y + d128[6]);
430 Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
431 accumulate_bit(acc, Y + d128[7]);
433 output_pixel(*dest++, acc);
438 static av_always_inline void
439 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
440 const int16_t *ubuf[2], const int16_t *vbuf[2],
441 const int16_t *abuf0, uint8_t *dest, int dstW,
442 int uvalpha, int y, enum AVPixelFormat target)
444 const uint8_t * const d128 = dither_8x8_220[y & 7];
447 if (c->flags & SWS_ERROR_DIFFUSION) {
450 for (i = 0; i < dstW; i +=2) {
453 Y = ((buf0[i + 0] + 64) >> 7);
454 Y += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
455 c->dither_error[0][i] = err;
456 acc = 2*acc + (Y >= 128);
459 err = ((buf0[i + 1] + 64) >> 7);
460 err += (7*Y + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4;
461 c->dither_error[0][i+1] = Y;
462 acc = 2*acc + (err >= 128);
466 output_pixel(*dest++, acc);
468 c->dither_error[0][i] = err;
470 for (i = 0; i < dstW; i += 8) {
472 accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
473 accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
474 accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
475 accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
476 accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
477 accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
478 accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
479 accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
481 output_pixel(*dest++, acc);
487 #undef accumulate_bit
489 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
490 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
491 const int16_t **lumSrc, int lumFilterSize, \
492 const int16_t *chrFilter, const int16_t **chrUSrc, \
493 const int16_t **chrVSrc, int chrFilterSize, \
494 const int16_t **alpSrc, uint8_t *dest, int dstW, \
497 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
498 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
499 alpSrc, dest, dstW, y, fmt); \
502 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
503 const int16_t *ubuf[2], const int16_t *vbuf[2], \
504 const int16_t *abuf[2], uint8_t *dest, int dstW, \
505 int yalpha, int uvalpha, int y) \
507 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
508 dest, dstW, yalpha, uvalpha, y, fmt); \
511 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
512 const int16_t *ubuf[2], const int16_t *vbuf[2], \
513 const int16_t *abuf0, uint8_t *dest, int dstW, \
514 int uvalpha, int y) \
516 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
517 abuf0, dest, dstW, uvalpha, \
521 YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
522 YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
524 #define output_pixels(pos, Y1, U, Y2, V) \
525 if (target == AV_PIX_FMT_YUYV422) { \
526 dest[pos + 0] = Y1; \
528 dest[pos + 2] = Y2; \
532 dest[pos + 1] = Y1; \
534 dest[pos + 3] = Y2; \
537 static av_always_inline void
538 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
539 const int16_t **lumSrc, int lumFilterSize,
540 const int16_t *chrFilter, const int16_t **chrUSrc,
541 const int16_t **chrVSrc, int chrFilterSize,
542 const int16_t **alpSrc, uint8_t *dest, int dstW,
543 int y, enum AVPixelFormat target)
547 for (i = 0; i < ((dstW + 1) >> 1); i++) {
554 for (j = 0; j < lumFilterSize; j++) {
555 Y1 += lumSrc[j][i * 2] * lumFilter[j];
556 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
558 for (j = 0; j < chrFilterSize; j++) {
559 U += chrUSrc[j][i] * chrFilter[j];
560 V += chrVSrc[j][i] * chrFilter[j];
566 if ((Y1 | Y2 | U | V) & 0x100) {
567 Y1 = av_clip_uint8(Y1);
568 Y2 = av_clip_uint8(Y2);
569 U = av_clip_uint8(U);
570 V = av_clip_uint8(V);
572 output_pixels(4*i, Y1, U, Y2, V);
576 static av_always_inline void
577 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
578 const int16_t *ubuf[2], const int16_t *vbuf[2],
579 const int16_t *abuf[2], uint8_t *dest, int dstW,
580 int yalpha, int uvalpha, int y,
581 enum AVPixelFormat target)
583 const int16_t *buf0 = buf[0], *buf1 = buf[1],
584 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
585 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
586 int yalpha1 = 4096 - yalpha;
587 int uvalpha1 = 4096 - uvalpha;
590 for (i = 0; i < ((dstW + 1) >> 1); i++) {
591 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
592 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
593 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
594 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
596 if ((Y1 | Y2 | U | V) & 0x100) {
597 Y1 = av_clip_uint8(Y1);
598 Y2 = av_clip_uint8(Y2);
599 U = av_clip_uint8(U);
600 V = av_clip_uint8(V);
603 output_pixels(i * 4, Y1, U, Y2, V);
607 static av_always_inline void
608 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
609 const int16_t *ubuf[2], const int16_t *vbuf[2],
610 const int16_t *abuf0, uint8_t *dest, int dstW,
611 int uvalpha, int y, enum AVPixelFormat target)
613 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
616 if (uvalpha < 2048) {
617 for (i = 0; i < ((dstW + 1) >> 1); i++) {
618 int Y1 = (buf0[i * 2 ]+64) >> 7;
619 int Y2 = (buf0[i * 2 + 1]+64) >> 7;
620 int U = (ubuf0[i] +64) >> 7;
621 int V = (vbuf0[i] +64) >> 7;
623 if ((Y1 | Y2 | U | V) & 0x100) {
624 Y1 = av_clip_uint8(Y1);
625 Y2 = av_clip_uint8(Y2);
626 U = av_clip_uint8(U);
627 V = av_clip_uint8(V);
630 Y1 = av_clip_uint8(Y1);
631 Y2 = av_clip_uint8(Y2);
632 U = av_clip_uint8(U);
633 V = av_clip_uint8(V);
635 output_pixels(i * 4, Y1, U, Y2, V);
638 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
639 for (i = 0; i < ((dstW + 1) >> 1); i++) {
640 int Y1 = (buf0[i * 2 ] + 64) >> 7;
641 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
642 int U = (ubuf0[i] + ubuf1[i]+128) >> 8;
643 int V = (vbuf0[i] + vbuf1[i]+128) >> 8;
645 if ((Y1 | Y2 | U | V) & 0x100) {
646 Y1 = av_clip_uint8(Y1);
647 Y2 = av_clip_uint8(Y2);
648 U = av_clip_uint8(U);
649 V = av_clip_uint8(V);
652 Y1 = av_clip_uint8(Y1);
653 Y2 = av_clip_uint8(Y2);
654 U = av_clip_uint8(U);
655 V = av_clip_uint8(V);
657 output_pixels(i * 4, Y1, U, Y2, V);
664 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
665 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
667 #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? R : B)
668 #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? B : R)
669 #define output_pixel(pos, val) \
670 if (isBE(target)) { \
676 static av_always_inline void
677 yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter,
678 const int32_t **lumSrc, int lumFilterSize,
679 const int16_t *chrFilter, const int32_t **chrUSrc,
680 const int32_t **chrVSrc, int chrFilterSize,
681 const int32_t **alpSrc, uint16_t *dest, int dstW,
682 int y, enum AVPixelFormat target, int hasAlpha)
685 int A1 = 0xffff<<14, A2 = 0xffff<<14;
687 for (i = 0; i < ((dstW + 1) >> 1); i++) {
689 int Y1 = -0x40000000;
690 int Y2 = -0x40000000;
691 int U = -128 << 23; // 19
695 for (j = 0; j < lumFilterSize; j++) {
696 Y1 += lumSrc[j][i * 2] * (unsigned)lumFilter[j];
697 Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
699 for (j = 0; j < chrFilterSize; j++) {;
700 U += chrUSrc[j][i] * (unsigned)chrFilter[j];
701 V += chrVSrc[j][i] * (unsigned)chrFilter[j];
707 for (j = 0; j < lumFilterSize; j++) {
708 A1 += alpSrc[j][i * 2] * (unsigned)lumFilter[j];
709 A2 += alpSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
717 // 8bit: 12+15=27; 16-bit: 12+19=31
725 // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
726 Y1 -= c->yuv2rgb_y_offset;
727 Y2 -= c->yuv2rgb_y_offset;
728 Y1 *= c->yuv2rgb_y_coeff;
729 Y2 *= c->yuv2rgb_y_coeff;
732 // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
734 R = V * c->yuv2rgb_v2r_coeff;
735 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
736 B = U * c->yuv2rgb_u2b_coeff;
738 // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
739 output_pixel(&dest[0], av_clip_uintp2(B_R + Y1, 30) >> 14);
740 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
741 output_pixel(&dest[2], av_clip_uintp2(R_B + Y1, 30) >> 14);
742 output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
743 output_pixel(&dest[4], av_clip_uintp2(B_R + Y2, 30) >> 14);
744 output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
745 output_pixel(&dest[6], av_clip_uintp2(R_B + Y2, 30) >> 14);
746 output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
751 static av_always_inline void
752 yuv2rgba64_2_c_template(SwsContext *c, const int32_t *buf[2],
753 const int32_t *ubuf[2], const int32_t *vbuf[2],
754 const int32_t *abuf[2], uint16_t *dest, int dstW,
755 int yalpha, int uvalpha, int y,
756 enum AVPixelFormat target, int hasAlpha)
758 const int32_t *buf0 = buf[0], *buf1 = buf[1],
759 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
760 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
761 *abuf0 = hasAlpha ? abuf[0] : NULL,
762 *abuf1 = hasAlpha ? abuf[1] : NULL;
763 int yalpha1 = 4096 - yalpha;
764 int uvalpha1 = 4096 - uvalpha;
766 int A1 = 0xffff<<14, A2 = 0xffff<<14;
768 for (i = 0; i < ((dstW + 1) >> 1); i++) {
769 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
770 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
771 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
772 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
775 Y1 -= c->yuv2rgb_y_offset;
776 Y2 -= c->yuv2rgb_y_offset;
777 Y1 *= c->yuv2rgb_y_coeff;
778 Y2 *= c->yuv2rgb_y_coeff;
782 R = V * c->yuv2rgb_v2r_coeff;
783 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
784 B = U * c->yuv2rgb_u2b_coeff;
787 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 1;
788 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 1;
794 output_pixel(&dest[0], av_clip_uintp2(B_R + Y1, 30) >> 14);
795 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
796 output_pixel(&dest[2], av_clip_uintp2(R_B + Y1, 30) >> 14);
797 output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
798 output_pixel(&dest[4], av_clip_uintp2(B_R + Y2, 30) >> 14);
799 output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
800 output_pixel(&dest[6], av_clip_uintp2(R_B + Y2, 30) >> 14);
801 output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
806 static av_always_inline void
807 yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0,
808 const int32_t *ubuf[2], const int32_t *vbuf[2],
809 const int32_t *abuf0, uint16_t *dest, int dstW,
810 int uvalpha, int y, enum AVPixelFormat target, int hasAlpha)
812 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
814 int A1 = 0xffff<<14, A2= 0xffff<<14;
816 if (uvalpha < 2048) {
817 for (i = 0; i < ((dstW + 1) >> 1); i++) {
818 int Y1 = (buf0[i * 2] ) >> 2;
819 int Y2 = (buf0[i * 2 + 1]) >> 2;
820 int U = (ubuf0[i] + (-128 << 11)) >> 2;
821 int V = (vbuf0[i] + (-128 << 11)) >> 2;
824 Y1 -= c->yuv2rgb_y_offset;
825 Y2 -= c->yuv2rgb_y_offset;
826 Y1 *= c->yuv2rgb_y_coeff;
827 Y2 *= c->yuv2rgb_y_coeff;
832 A1 = abuf0[i * 2 ] << 11;
833 A2 = abuf0[i * 2 + 1] << 11;
839 R = V * c->yuv2rgb_v2r_coeff;
840 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
841 B = U * c->yuv2rgb_u2b_coeff;
843 output_pixel(&dest[0], av_clip_uintp2(B_R + Y1, 30) >> 14);
844 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
845 output_pixel(&dest[2], av_clip_uintp2(R_B + Y1, 30) >> 14);
846 output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
847 output_pixel(&dest[4], av_clip_uintp2(B_R + Y2, 30) >> 14);
848 output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
849 output_pixel(&dest[6], av_clip_uintp2(R_B + Y2, 30) >> 14);
850 output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
854 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
855 int A1 = 0xffff<<14, A2 = 0xffff<<14;
856 for (i = 0; i < ((dstW + 1) >> 1); i++) {
857 int Y1 = (buf0[i * 2] ) >> 2;
858 int Y2 = (buf0[i * 2 + 1]) >> 2;
859 int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
860 int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
863 Y1 -= c->yuv2rgb_y_offset;
864 Y2 -= c->yuv2rgb_y_offset;
865 Y1 *= c->yuv2rgb_y_coeff;
866 Y2 *= c->yuv2rgb_y_coeff;
871 A1 = abuf0[i * 2 ] << 11;
872 A2 = abuf0[i * 2 + 1] << 11;
878 R = V * c->yuv2rgb_v2r_coeff;
879 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
880 B = U * c->yuv2rgb_u2b_coeff;
882 output_pixel(&dest[0], av_clip_uintp2(B_R + Y1, 30) >> 14);
883 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
884 output_pixel(&dest[2], av_clip_uintp2(R_B + Y1, 30) >> 14);
885 output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
886 output_pixel(&dest[4], av_clip_uintp2(B_R + Y2, 30) >> 14);
887 output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
888 output_pixel(&dest[6], av_clip_uintp2(R_B + Y2, 30) >> 14);
889 output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
895 static av_always_inline void
896 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
897 const int32_t **lumSrc, int lumFilterSize,
898 const int16_t *chrFilter, const int32_t **chrUSrc,
899 const int32_t **chrVSrc, int chrFilterSize,
900 const int32_t **alpSrc, uint16_t *dest, int dstW,
901 int y, enum AVPixelFormat target, int hasAlpha)
905 for (i = 0; i < ((dstW + 1) >> 1); i++) {
907 int Y1 = -0x40000000;
908 int Y2 = -0x40000000;
909 int U = -128 << 23; // 19
913 for (j = 0; j < lumFilterSize; j++) {
914 Y1 += lumSrc[j][i * 2] * (unsigned)lumFilter[j];
915 Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
917 for (j = 0; j < chrFilterSize; j++) {;
918 U += chrUSrc[j][i] * (unsigned)chrFilter[j];
919 V += chrVSrc[j][i] * (unsigned)chrFilter[j];
922 // 8bit: 12+15=27; 16-bit: 12+19=31
930 // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
931 Y1 -= c->yuv2rgb_y_offset;
932 Y2 -= c->yuv2rgb_y_offset;
933 Y1 *= c->yuv2rgb_y_coeff;
934 Y2 *= c->yuv2rgb_y_coeff;
937 // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
939 R = V * c->yuv2rgb_v2r_coeff;
940 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
941 B = U * c->yuv2rgb_u2b_coeff;
943 // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
944 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
945 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
946 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
947 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
948 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
949 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
954 static av_always_inline void
955 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
956 const int32_t *ubuf[2], const int32_t *vbuf[2],
957 const int32_t *abuf[2], uint16_t *dest, int dstW,
958 int yalpha, int uvalpha, int y,
959 enum AVPixelFormat target, int hasAlpha)
961 const int32_t *buf0 = buf[0], *buf1 = buf[1],
962 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
963 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
964 int yalpha1 = 4096 - yalpha;
965 int uvalpha1 = 4096 - uvalpha;
968 for (i = 0; i < ((dstW + 1) >> 1); i++) {
969 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
970 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
971 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
972 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
975 Y1 -= c->yuv2rgb_y_offset;
976 Y2 -= c->yuv2rgb_y_offset;
977 Y1 *= c->yuv2rgb_y_coeff;
978 Y2 *= c->yuv2rgb_y_coeff;
982 R = V * c->yuv2rgb_v2r_coeff;
983 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
984 B = U * c->yuv2rgb_u2b_coeff;
986 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
987 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
988 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
989 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
990 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
991 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
996 static av_always_inline void
997 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
998 const int32_t *ubuf[2], const int32_t *vbuf[2],
999 const int32_t *abuf0, uint16_t *dest, int dstW,
1000 int uvalpha, int y, enum AVPixelFormat target, int hasAlpha)
1002 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1005 if (uvalpha < 2048) {
1006 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1007 int Y1 = (buf0[i * 2] ) >> 2;
1008 int Y2 = (buf0[i * 2 + 1]) >> 2;
1009 int U = (ubuf0[i] + (-128 << 11)) >> 2;
1010 int V = (vbuf0[i] + (-128 << 11)) >> 2;
1013 Y1 -= c->yuv2rgb_y_offset;
1014 Y2 -= c->yuv2rgb_y_offset;
1015 Y1 *= c->yuv2rgb_y_coeff;
1016 Y2 *= c->yuv2rgb_y_coeff;
1020 R = V * c->yuv2rgb_v2r_coeff;
1021 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1022 B = U * c->yuv2rgb_u2b_coeff;
1024 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
1025 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
1026 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
1027 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
1028 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
1029 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
1033 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1034 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1035 int Y1 = (buf0[i * 2] ) >> 2;
1036 int Y2 = (buf0[i * 2 + 1]) >> 2;
1037 int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
1038 int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
1041 Y1 -= c->yuv2rgb_y_offset;
1042 Y2 -= c->yuv2rgb_y_offset;
1043 Y1 *= c->yuv2rgb_y_coeff;
1044 Y2 *= c->yuv2rgb_y_coeff;
1048 R = V * c->yuv2rgb_v2r_coeff;
1049 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1050 B = U * c->yuv2rgb_u2b_coeff;
1052 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
1053 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
1054 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
1055 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
1056 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
1057 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
1067 #define YUV2PACKED16WRAPPER(name, base, ext, fmt, hasAlpha) \
1068 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1069 const int16_t **_lumSrc, int lumFilterSize, \
1070 const int16_t *chrFilter, const int16_t **_chrUSrc, \
1071 const int16_t **_chrVSrc, int chrFilterSize, \
1072 const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
1075 const int32_t **lumSrc = (const int32_t **) _lumSrc, \
1076 **chrUSrc = (const int32_t **) _chrUSrc, \
1077 **chrVSrc = (const int32_t **) _chrVSrc, \
1078 **alpSrc = (const int32_t **) _alpSrc; \
1079 uint16_t *dest = (uint16_t *) _dest; \
1080 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1081 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1082 alpSrc, dest, dstW, y, fmt, hasAlpha); \
1085 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
1086 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
1087 const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
1088 int yalpha, int uvalpha, int y) \
1090 const int32_t **buf = (const int32_t **) _buf, \
1091 **ubuf = (const int32_t **) _ubuf, \
1092 **vbuf = (const int32_t **) _vbuf, \
1093 **abuf = (const int32_t **) _abuf; \
1094 uint16_t *dest = (uint16_t *) _dest; \
1095 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1096 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1099 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
1100 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
1101 const int16_t *_abuf0, uint8_t *_dest, int dstW, \
1102 int uvalpha, int y) \
1104 const int32_t *buf0 = (const int32_t *) _buf0, \
1105 **ubuf = (const int32_t **) _ubuf, \
1106 **vbuf = (const int32_t **) _vbuf, \
1107 *abuf0 = (const int32_t *) _abuf0; \
1108 uint16_t *dest = (uint16_t *) _dest; \
1109 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1110 dstW, uvalpha, y, fmt, hasAlpha); \
1113 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, AV_PIX_FMT_RGB48BE, 0)
1114 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, AV_PIX_FMT_RGB48LE, 0)
1115 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, AV_PIX_FMT_BGR48BE, 0)
1116 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, AV_PIX_FMT_BGR48LE, 0)
1117 YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64be, AV_PIX_FMT_RGBA64BE, 1)
1118 YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64le, AV_PIX_FMT_RGBA64LE, 1)
1119 YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64be, AV_PIX_FMT_RGBA64BE, 0)
1120 YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64le, AV_PIX_FMT_RGBA64LE, 0)
1123 * Write out 2 RGB pixels in the target pixel format. This function takes a
1124 * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
1125 * things like endianness conversion and shifting. The caller takes care of
1126 * setting the correct offset in these tables from the chroma (U/V) values.
1127 * This function then uses the luminance (Y1/Y2) values to write out the
1128 * correct RGB values into the destination buffer.
1130 static av_always_inline void
1131 yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
1132 unsigned A1, unsigned A2,
1133 const void *_r, const void *_g, const void *_b, int y,
1134 enum AVPixelFormat target, int hasAlpha)
1136 if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
1137 target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
1138 uint32_t *dest = (uint32_t *) _dest;
1139 const uint32_t *r = (const uint32_t *) _r;
1140 const uint32_t *g = (const uint32_t *) _g;
1141 const uint32_t *b = (const uint32_t *) _b;
1144 int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
1146 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
1147 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
1150 int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
1152 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
1153 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
1155 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
1156 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
1159 } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
1160 uint8_t *dest = (uint8_t *) _dest;
1161 const uint8_t *r = (const uint8_t *) _r;
1162 const uint8_t *g = (const uint8_t *) _g;
1163 const uint8_t *b = (const uint8_t *) _b;
1165 #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
1166 #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
1168 dest[i * 6 + 0] = r_b[Y1];
1169 dest[i * 6 + 1] = g[Y1];
1170 dest[i * 6 + 2] = b_r[Y1];
1171 dest[i * 6 + 3] = r_b[Y2];
1172 dest[i * 6 + 4] = g[Y2];
1173 dest[i * 6 + 5] = b_r[Y2];
1176 } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
1177 target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
1178 target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
1179 uint16_t *dest = (uint16_t *) _dest;
1180 const uint16_t *r = (const uint16_t *) _r;
1181 const uint16_t *g = (const uint16_t *) _g;
1182 const uint16_t *b = (const uint16_t *) _b;
1183 int dr1, dg1, db1, dr2, dg2, db2;
1185 if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
1186 dr1 = dither_2x2_8[ y & 1 ][0];
1187 dg1 = dither_2x2_4[ y & 1 ][0];
1188 db1 = dither_2x2_8[(y & 1) ^ 1][0];
1189 dr2 = dither_2x2_8[ y & 1 ][1];
1190 dg2 = dither_2x2_4[ y & 1 ][1];
1191 db2 = dither_2x2_8[(y & 1) ^ 1][1];
1192 } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
1193 dr1 = dither_2x2_8[ y & 1 ][0];
1194 dg1 = dither_2x2_8[ y & 1 ][1];
1195 db1 = dither_2x2_8[(y & 1) ^ 1][0];
1196 dr2 = dither_2x2_8[ y & 1 ][1];
1197 dg2 = dither_2x2_8[ y & 1 ][0];
1198 db2 = dither_2x2_8[(y & 1) ^ 1][1];
1200 dr1 = dither_4x4_16[ y & 3 ][0];
1201 dg1 = dither_4x4_16[ y & 3 ][1];
1202 db1 = dither_4x4_16[(y & 3) ^ 3][0];
1203 dr2 = dither_4x4_16[ y & 3 ][1];
1204 dg2 = dither_4x4_16[ y & 3 ][0];
1205 db2 = dither_4x4_16[(y & 3) ^ 3][1];
1208 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
1209 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
1210 } else /* 8/4-bit */ {
1211 uint8_t *dest = (uint8_t *) _dest;
1212 const uint8_t *r = (const uint8_t *) _r;
1213 const uint8_t *g = (const uint8_t *) _g;
1214 const uint8_t *b = (const uint8_t *) _b;
1215 int dr1, dg1, db1, dr2, dg2, db2;
1217 if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
1218 const uint8_t * const d64 = dither_8x8_73[y & 7];
1219 const uint8_t * const d32 = dither_8x8_32[y & 7];
1220 dr1 = dg1 = d32[(i * 2 + 0) & 7];
1221 db1 = d64[(i * 2 + 0) & 7];
1222 dr2 = dg2 = d32[(i * 2 + 1) & 7];
1223 db2 = d64[(i * 2 + 1) & 7];
1225 const uint8_t * const d64 = dither_8x8_73 [y & 7];
1226 const uint8_t * const d128 = dither_8x8_220[y & 7];
1227 dr1 = db1 = d128[(i * 2 + 0) & 7];
1228 dg1 = d64[(i * 2 + 0) & 7];
1229 dr2 = db2 = d128[(i * 2 + 1) & 7];
1230 dg2 = d64[(i * 2 + 1) & 7];
1233 if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
1234 dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
1235 ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
1237 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
1238 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
1243 static av_always_inline void
1244 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
1245 const int16_t **lumSrc, int lumFilterSize,
1246 const int16_t *chrFilter, const int16_t **chrUSrc,
1247 const int16_t **chrVSrc, int chrFilterSize,
1248 const int16_t **alpSrc, uint8_t *dest, int dstW,
1249 int y, enum AVPixelFormat target, int hasAlpha)
1253 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1259 const void *r, *g, *b;
1261 for (j = 0; j < lumFilterSize; j++) {
1262 Y1 += lumSrc[j][i * 2] * lumFilter[j];
1263 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
1265 for (j = 0; j < chrFilterSize; j++) {
1266 U += chrUSrc[j][i] * chrFilter[j];
1267 V += chrVSrc[j][i] * chrFilter[j];
1276 for (j = 0; j < lumFilterSize; j++) {
1277 A1 += alpSrc[j][i * 2 ] * lumFilter[j];
1278 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
1282 if ((A1 | A2) & 0x100) {
1283 A1 = av_clip_uint8(A1);
1284 A2 = av_clip_uint8(A2);
1288 r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
1289 g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
1290 b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1292 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1293 r, g, b, y, target, hasAlpha);
1297 static av_always_inline void
1298 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
1299 const int16_t *ubuf[2], const int16_t *vbuf[2],
1300 const int16_t *abuf[2], uint8_t *dest, int dstW,
1301 int yalpha, int uvalpha, int y,
1302 enum AVPixelFormat target, int hasAlpha)
1304 const int16_t *buf0 = buf[0], *buf1 = buf[1],
1305 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1306 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1307 *abuf0 = hasAlpha ? abuf[0] : NULL,
1308 *abuf1 = hasAlpha ? abuf[1] : NULL;
1309 int yalpha1 = 4096 - yalpha;
1310 int uvalpha1 = 4096 - uvalpha;
1313 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1314 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
1315 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
1316 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
1317 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
1319 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1320 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1321 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1324 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
1325 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1326 A1 = av_clip_uint8(A1);
1327 A2 = av_clip_uint8(A2);
1330 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1331 r, g, b, y, target, hasAlpha);
1335 static av_always_inline void
1336 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1337 const int16_t *ubuf[2], const int16_t *vbuf[2],
1338 const int16_t *abuf0, uint8_t *dest, int dstW,
1339 int uvalpha, int y, enum AVPixelFormat target,
1342 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1345 if (uvalpha < 2048) {
1346 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1347 int Y1 = (buf0[i * 2 ] + 64) >> 7;
1348 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1349 int U = (ubuf0[i] + 64) >> 7;
1350 int V = (vbuf0[i] + 64) >> 7;
1352 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1353 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1354 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1357 A1 = abuf0[i * 2 ] * 255 + 16384 >> 15;
1358 A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
1359 A1 = av_clip_uint8(A1);
1360 A2 = av_clip_uint8(A2);
1363 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1364 r, g, b, y, target, hasAlpha);
1367 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1368 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1369 int Y1 = (buf0[i * 2 ] + 64) >> 7;
1370 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1371 int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
1372 int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
1374 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1375 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1376 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1379 A1 = (abuf0[i * 2 ] + 64) >> 7;
1380 A2 = (abuf0[i * 2 + 1] + 64) >> 7;
1381 A1 = av_clip_uint8(A1);
1382 A2 = av_clip_uint8(A2);
1385 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1386 r, g, b, y, target, hasAlpha);
1391 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1392 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1393 const int16_t **lumSrc, int lumFilterSize, \
1394 const int16_t *chrFilter, const int16_t **chrUSrc, \
1395 const int16_t **chrVSrc, int chrFilterSize, \
1396 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1399 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1400 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1401 alpSrc, dest, dstW, y, fmt, hasAlpha); \
1404 #define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
1405 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1406 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1407 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1408 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1409 int yalpha, int uvalpha, int y) \
1411 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1412 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1415 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1416 YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
1417 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1418 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1419 const int16_t *abuf0, uint8_t *dest, int dstW, \
1420 int uvalpha, int y) \
1422 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1423 dstW, uvalpha, y, fmt, hasAlpha); \
1427 YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1428 YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1430 #if CONFIG_SWSCALE_ALPHA
1431 YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
1432 YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
1434 YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
1435 YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
1437 YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
1438 YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
1439 YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
1440 YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
1441 YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
1442 YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
1443 YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
1444 YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
1446 static av_always_inline void yuv2rgb_write_full(SwsContext *c,
1447 uint8_t *dest, int i, int Y, int A, int U, int V,
1448 int y, enum AVPixelFormat target, int hasAlpha, int err[4])
1451 int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
1453 Y -= c->yuv2rgb_y_offset;
1454 Y *= c->yuv2rgb_y_coeff;
1456 R = Y + V*c->yuv2rgb_v2r_coeff;
1457 G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1458 B = Y + U*c->yuv2rgb_u2b_coeff;
1459 if ((R | G | B) & 0xC0000000) {
1460 R = av_clip_uintp2(R, 30);
1461 G = av_clip_uintp2(G, 30);
1462 B = av_clip_uintp2(B, 30);
1466 case AV_PIX_FMT_ARGB:
1467 dest[0] = hasAlpha ? A : 255;
1472 case AV_PIX_FMT_RGB24:
1477 case AV_PIX_FMT_RGBA:
1481 dest[3] = hasAlpha ? A : 255;
1483 case AV_PIX_FMT_ABGR:
1484 dest[0] = hasAlpha ? A : 255;
1489 case AV_PIX_FMT_BGR24:
1494 case AV_PIX_FMT_BGRA:
1498 dest[3] = hasAlpha ? A : 255;
1500 case AV_PIX_FMT_BGR4_BYTE:
1501 case AV_PIX_FMT_RGB4_BYTE:
1502 case AV_PIX_FMT_BGR8:
1503 case AV_PIX_FMT_RGB8:
1509 R += (7*err[0] + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2])>>4;
1510 G += (7*err[1] + 1*c->dither_error[1][i] + 5*c->dither_error[1][i+1] + 3*c->dither_error[1][i+2])>>4;
1511 B += (7*err[2] + 1*c->dither_error[2][i] + 5*c->dither_error[2][i+1] + 3*c->dither_error[2][i+2])>>4;
1512 c->dither_error[0][i] = err[0];
1513 c->dither_error[1][i] = err[1];
1514 c->dither_error[2][i] = err[2];
1515 r = R >> (isrgb8 ? 5 : 7);
1516 g = G >> (isrgb8 ? 5 : 6);
1517 b = B >> (isrgb8 ? 6 : 7);
1518 r = av_clip(r, 0, isrgb8 ? 7 : 1);
1519 g = av_clip(g, 0, isrgb8 ? 7 : 3);
1520 b = av_clip(b, 0, isrgb8 ? 3 : 1);
1521 err[0] = R - r*(isrgb8 ? 36 : 255);
1522 err[1] = G - g*(isrgb8 ? 36 : 85);
1523 err[2] = B - b*(isrgb8 ? 85 : 255);
1524 if(target == AV_PIX_FMT_BGR4_BYTE) {
1525 dest[0] = r + 2*g + 8*b;
1526 } else if(target == AV_PIX_FMT_RGB4_BYTE) {
1527 dest[0] = b + 2*g + 8*r;
1528 } else if(target == AV_PIX_FMT_BGR8) {
1529 dest[0] = r + 8*g + 64*b;
1530 } else if(target == AV_PIX_FMT_RGB8) {
1531 dest[0] = b + 4*g + 32*r;
1538 static av_always_inline void
1539 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1540 const int16_t **lumSrc, int lumFilterSize,
1541 const int16_t *chrFilter, const int16_t **chrUSrc,
1542 const int16_t **chrVSrc, int chrFilterSize,
1543 const int16_t **alpSrc, uint8_t *dest,
1544 int dstW, int y, enum AVPixelFormat target, int hasAlpha)
1547 int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1549 int A = 0; //init to silence warning
1551 if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1552 || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
1555 for (i = 0; i < dstW; i++) {
1558 int U = (1<<9)-(128 << 19);
1559 int V = (1<<9)-(128 << 19);
1561 for (j = 0; j < lumFilterSize; j++) {
1562 Y += lumSrc[j][i] * lumFilter[j];
1564 for (j = 0; j < chrFilterSize; j++) {
1565 U += chrUSrc[j][i] * chrFilter[j];
1566 V += chrVSrc[j][i] * chrFilter[j];
1573 for (j = 0; j < lumFilterSize; j++) {
1574 A += alpSrc[j][i] * lumFilter[j];
1578 A = av_clip_uint8(A);
1580 yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1583 c->dither_error[0][i] = err[0];
1584 c->dither_error[1][i] = err[1];
1585 c->dither_error[2][i] = err[2];
1588 static av_always_inline void
1589 yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
1590 const int16_t *ubuf[2], const int16_t *vbuf[2],
1591 const int16_t *abuf[2], uint8_t *dest, int dstW,
1592 int yalpha, int uvalpha, int y,
1593 enum AVPixelFormat target, int hasAlpha)
1595 const int16_t *buf0 = buf[0], *buf1 = buf[1],
1596 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1597 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1598 *abuf0 = hasAlpha ? abuf[0] : NULL,
1599 *abuf1 = hasAlpha ? abuf[1] : NULL;
1600 int yalpha1 = 4096 - yalpha;
1601 int uvalpha1 = 4096 - uvalpha;
1603 int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1605 int A = 0; // init to silcene warning
1607 if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1608 || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
1611 for (i = 0; i < dstW; i++) {
1612 int Y = ( buf0[i] * yalpha1 + buf1[i] * yalpha ) >> 10; //FIXME rounding
1613 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
1614 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
1617 A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
1619 A = av_clip_uint8(A);
1622 yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1625 c->dither_error[0][i] = err[0];
1626 c->dither_error[1][i] = err[1];
1627 c->dither_error[2][i] = err[2];
1630 static av_always_inline void
1631 yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
1632 const int16_t *ubuf[2], const int16_t *vbuf[2],
1633 const int16_t *abuf0, uint8_t *dest, int dstW,
1634 int uvalpha, int y, enum AVPixelFormat target,
1637 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1639 int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1642 if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1643 || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
1646 if (uvalpha < 2048) {
1647 int A = 0; //init to silence warning
1648 for (i = 0; i < dstW; i++) {
1649 int Y = buf0[i] << 2;
1650 int U = (ubuf0[i] - (128<<7)) << 2;
1651 int V = (vbuf0[i] - (128<<7)) << 2;
1654 A = (abuf0[i] + 64) >> 7;
1656 A = av_clip_uint8(A);
1659 yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1663 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1664 int A = 0; //init to silence warning
1665 for (i = 0; i < dstW; i++) {
1666 int Y = buf0[i] << 2;
1667 int U = (ubuf0[i] + ubuf1[i] - (128<<8)) << 1;
1668 int V = (vbuf0[i] + vbuf1[i] - (128<<8)) << 1;
1671 A = (abuf0[i] + 64) >> 7;
1673 A = av_clip_uint8(A);
1676 yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1681 c->dither_error[0][i] = err[0];
1682 c->dither_error[1][i] = err[1];
1683 c->dither_error[2][i] = err[2];
1687 YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1688 YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1689 YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1690 YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1692 #if CONFIG_SWSCALE_ALPHA
1693 YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
1694 YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
1695 YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
1696 YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
1698 YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
1699 YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
1700 YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
1701 YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
1703 YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
1704 YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
1706 YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full, AV_PIX_FMT_BGR4_BYTE, 0)
1707 YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full, AV_PIX_FMT_RGB4_BYTE, 0)
1708 YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full, AV_PIX_FMT_BGR8, 0)
1709 YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full, AV_PIX_FMT_RGB8, 0)
1712 yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
1713 const int16_t **lumSrc, int lumFilterSize,
1714 const int16_t *chrFilter, const int16_t **chrUSrc,
1715 const int16_t **chrVSrc, int chrFilterSize,
1716 const int16_t **alpSrc, uint8_t **dest,
1719 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
1721 int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
1722 uint16_t **dest16 = (uint16_t**)dest;
1723 int SH = 22 + 7 - desc->comp[0].depth_minus1;
1724 int A = 0; // init to silence warning
1726 for (i = 0; i < dstW; i++) {
1729 int U = (1 << 9) - (128 << 19);
1730 int V = (1 << 9) - (128 << 19);
1733 for (j = 0; j < lumFilterSize; j++)
1734 Y += lumSrc[j][i] * lumFilter[j];
1736 for (j = 0; j < chrFilterSize; j++) {
1737 U += chrUSrc[j][i] * chrFilter[j];
1738 V += chrVSrc[j][i] * chrFilter[j];
1748 for (j = 0; j < lumFilterSize; j++)
1749 A += alpSrc[j][i] * lumFilter[j];
1754 A = av_clip_uint8(A);
1757 Y -= c->yuv2rgb_y_offset;
1758 Y *= c->yuv2rgb_y_coeff;
1760 R = Y + V * c->yuv2rgb_v2r_coeff;
1761 G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1762 B = Y + U * c->yuv2rgb_u2b_coeff;
1764 if ((R | G | B) & 0xC0000000) {
1765 R = av_clip_uintp2(R, 30);
1766 G = av_clip_uintp2(G, 30);
1767 B = av_clip_uintp2(B, 30);
1771 dest16[0][i] = G >> SH;
1772 dest16[1][i] = B >> SH;
1773 dest16[2][i] = R >> SH;
1777 dest[0][i] = G >> 22;
1778 dest[1][i] = B >> 22;
1779 dest[2][i] = R >> 22;
1784 if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
1785 for (i = 0; i < dstW; i++) {
1786 dest16[0][i] = av_bswap16(dest16[0][i]);
1787 dest16[1][i] = av_bswap16(dest16[1][i]);
1788 dest16[2][i] = av_bswap16(dest16[2][i]);
1790 dest16[3][i] = av_bswap16(dest16[3][i]);
1795 av_cold void ff_sws_init_output_funcs(SwsContext *c,
1796 yuv2planar1_fn *yuv2plane1,
1797 yuv2planarX_fn *yuv2planeX,
1798 yuv2interleavedX_fn *yuv2nv12cX,
1799 yuv2packed1_fn *yuv2packed1,
1800 yuv2packed2_fn *yuv2packed2,
1801 yuv2packedX_fn *yuv2packedX,
1802 yuv2anyX_fn *yuv2anyX)
1804 enum AVPixelFormat dstFormat = c->dstFormat;
1805 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
1807 if (is16BPS(dstFormat)) {
1808 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
1809 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
1810 } else if (is9_OR_10BPS(dstFormat)) {
1811 if (desc->comp[0].depth_minus1 == 8) {
1812 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
1813 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
1814 } else if (desc->comp[0].depth_minus1 == 9) {
1815 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
1816 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
1817 } else if (desc->comp[0].depth_minus1 == 11) {
1818 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c : yuv2planeX_12LE_c;
1819 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c : yuv2plane1_12LE_c;
1820 } else if (desc->comp[0].depth_minus1 == 13) {
1821 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c : yuv2planeX_14LE_c;
1822 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c : yuv2plane1_14LE_c;
1826 *yuv2plane1 = yuv2plane1_8_c;
1827 *yuv2planeX = yuv2planeX_8_c;
1828 if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
1829 *yuv2nv12cX = yuv2nv12cX_c;
1832 if(c->flags & SWS_FULL_CHR_H_INT) {
1833 switch (dstFormat) {
1834 case AV_PIX_FMT_RGBA:
1836 *yuv2packedX = yuv2rgba32_full_X_c;
1837 *yuv2packed2 = yuv2rgba32_full_2_c;
1838 *yuv2packed1 = yuv2rgba32_full_1_c;
1840 #if CONFIG_SWSCALE_ALPHA
1842 *yuv2packedX = yuv2rgba32_full_X_c;
1843 *yuv2packed2 = yuv2rgba32_full_2_c;
1844 *yuv2packed1 = yuv2rgba32_full_1_c;
1846 #endif /* CONFIG_SWSCALE_ALPHA */
1848 *yuv2packedX = yuv2rgbx32_full_X_c;
1849 *yuv2packed2 = yuv2rgbx32_full_2_c;
1850 *yuv2packed1 = yuv2rgbx32_full_1_c;
1852 #endif /* !CONFIG_SMALL */
1854 case AV_PIX_FMT_ARGB:
1856 *yuv2packedX = yuv2argb32_full_X_c;
1857 *yuv2packed2 = yuv2argb32_full_2_c;
1858 *yuv2packed1 = yuv2argb32_full_1_c;
1860 #if CONFIG_SWSCALE_ALPHA
1862 *yuv2packedX = yuv2argb32_full_X_c;
1863 *yuv2packed2 = yuv2argb32_full_2_c;
1864 *yuv2packed1 = yuv2argb32_full_1_c;
1866 #endif /* CONFIG_SWSCALE_ALPHA */
1868 *yuv2packedX = yuv2xrgb32_full_X_c;
1869 *yuv2packed2 = yuv2xrgb32_full_2_c;
1870 *yuv2packed1 = yuv2xrgb32_full_1_c;
1872 #endif /* !CONFIG_SMALL */
1874 case AV_PIX_FMT_BGRA:
1876 *yuv2packedX = yuv2bgra32_full_X_c;
1877 *yuv2packed2 = yuv2bgra32_full_2_c;
1878 *yuv2packed1 = yuv2bgra32_full_1_c;
1880 #if CONFIG_SWSCALE_ALPHA
1882 *yuv2packedX = yuv2bgra32_full_X_c;
1883 *yuv2packed2 = yuv2bgra32_full_2_c;
1884 *yuv2packed1 = yuv2bgra32_full_1_c;
1886 #endif /* CONFIG_SWSCALE_ALPHA */
1888 *yuv2packedX = yuv2bgrx32_full_X_c;
1889 *yuv2packed2 = yuv2bgrx32_full_2_c;
1890 *yuv2packed1 = yuv2bgrx32_full_1_c;
1892 #endif /* !CONFIG_SMALL */
1894 case AV_PIX_FMT_ABGR:
1896 *yuv2packedX = yuv2abgr32_full_X_c;
1897 *yuv2packed2 = yuv2abgr32_full_2_c;
1898 *yuv2packed1 = yuv2abgr32_full_1_c;
1900 #if CONFIG_SWSCALE_ALPHA
1902 *yuv2packedX = yuv2abgr32_full_X_c;
1903 *yuv2packed2 = yuv2abgr32_full_2_c;
1904 *yuv2packed1 = yuv2abgr32_full_1_c;
1906 #endif /* CONFIG_SWSCALE_ALPHA */
1908 *yuv2packedX = yuv2xbgr32_full_X_c;
1909 *yuv2packed2 = yuv2xbgr32_full_2_c;
1910 *yuv2packed1 = yuv2xbgr32_full_1_c;
1912 #endif /* !CONFIG_SMALL */
1914 case AV_PIX_FMT_RGB24:
1915 *yuv2packedX = yuv2rgb24_full_X_c;
1916 *yuv2packed2 = yuv2rgb24_full_2_c;
1917 *yuv2packed1 = yuv2rgb24_full_1_c;
1919 case AV_PIX_FMT_BGR24:
1920 *yuv2packedX = yuv2bgr24_full_X_c;
1921 *yuv2packed2 = yuv2bgr24_full_2_c;
1922 *yuv2packed1 = yuv2bgr24_full_1_c;
1924 case AV_PIX_FMT_BGR4_BYTE:
1925 *yuv2packedX = yuv2bgr4_byte_full_X_c;
1926 *yuv2packed2 = yuv2bgr4_byte_full_2_c;
1927 *yuv2packed1 = yuv2bgr4_byte_full_1_c;
1929 case AV_PIX_FMT_RGB4_BYTE:
1930 *yuv2packedX = yuv2rgb4_byte_full_X_c;
1931 *yuv2packed2 = yuv2rgb4_byte_full_2_c;
1932 *yuv2packed1 = yuv2rgb4_byte_full_1_c;
1934 case AV_PIX_FMT_BGR8:
1935 *yuv2packedX = yuv2bgr8_full_X_c;
1936 *yuv2packed2 = yuv2bgr8_full_2_c;
1937 *yuv2packed1 = yuv2bgr8_full_1_c;
1939 case AV_PIX_FMT_RGB8:
1940 *yuv2packedX = yuv2rgb8_full_X_c;
1941 *yuv2packed2 = yuv2rgb8_full_2_c;
1942 *yuv2packed1 = yuv2rgb8_full_1_c;
1944 case AV_PIX_FMT_GBRP:
1945 case AV_PIX_FMT_GBRP9BE:
1946 case AV_PIX_FMT_GBRP9LE:
1947 case AV_PIX_FMT_GBRP10BE:
1948 case AV_PIX_FMT_GBRP10LE:
1949 case AV_PIX_FMT_GBRP12BE:
1950 case AV_PIX_FMT_GBRP12LE:
1951 case AV_PIX_FMT_GBRP14BE:
1952 case AV_PIX_FMT_GBRP14LE:
1953 case AV_PIX_FMT_GBRP16BE:
1954 case AV_PIX_FMT_GBRP16LE:
1955 case AV_PIX_FMT_GBRAP:
1956 *yuv2anyX = yuv2gbrp_full_X_c;
1959 if (!*yuv2packedX && !*yuv2anyX)
1963 switch (dstFormat) {
1964 case AV_PIX_FMT_RGBA64LE:
1965 #if CONFIG_SWSCALE_ALPHA
1967 *yuv2packed1 = yuv2rgba64le_1_c;
1968 *yuv2packed2 = yuv2rgba64le_2_c;
1969 *yuv2packedX = yuv2rgba64le_X_c;
1971 #endif /* CONFIG_SWSCALE_ALPHA */
1973 *yuv2packed1 = yuv2rgbx64le_1_c;
1974 *yuv2packed2 = yuv2rgbx64le_2_c;
1975 *yuv2packedX = yuv2rgbx64le_X_c;
1978 case AV_PIX_FMT_RGBA64BE:
1979 #if CONFIG_SWSCALE_ALPHA
1981 *yuv2packed1 = yuv2rgba64be_1_c;
1982 *yuv2packed2 = yuv2rgba64be_2_c;
1983 *yuv2packedX = yuv2rgba64be_X_c;
1985 #endif /* CONFIG_SWSCALE_ALPHA */
1987 *yuv2packed1 = yuv2rgbx64be_1_c;
1988 *yuv2packed2 = yuv2rgbx64be_2_c;
1989 *yuv2packedX = yuv2rgbx64be_X_c;
1992 case AV_PIX_FMT_RGB48LE:
1993 *yuv2packed1 = yuv2rgb48le_1_c;
1994 *yuv2packed2 = yuv2rgb48le_2_c;
1995 *yuv2packedX = yuv2rgb48le_X_c;
1997 case AV_PIX_FMT_RGB48BE:
1998 *yuv2packed1 = yuv2rgb48be_1_c;
1999 *yuv2packed2 = yuv2rgb48be_2_c;
2000 *yuv2packedX = yuv2rgb48be_X_c;
2002 case AV_PIX_FMT_BGR48LE:
2003 *yuv2packed1 = yuv2bgr48le_1_c;
2004 *yuv2packed2 = yuv2bgr48le_2_c;
2005 *yuv2packedX = yuv2bgr48le_X_c;
2007 case AV_PIX_FMT_BGR48BE:
2008 *yuv2packed1 = yuv2bgr48be_1_c;
2009 *yuv2packed2 = yuv2bgr48be_2_c;
2010 *yuv2packedX = yuv2bgr48be_X_c;
2012 case AV_PIX_FMT_RGB32:
2013 case AV_PIX_FMT_BGR32:
2015 *yuv2packed1 = yuv2rgb32_1_c;
2016 *yuv2packed2 = yuv2rgb32_2_c;
2017 *yuv2packedX = yuv2rgb32_X_c;
2019 #if CONFIG_SWSCALE_ALPHA
2021 *yuv2packed1 = yuv2rgba32_1_c;
2022 *yuv2packed2 = yuv2rgba32_2_c;
2023 *yuv2packedX = yuv2rgba32_X_c;
2025 #endif /* CONFIG_SWSCALE_ALPHA */
2027 *yuv2packed1 = yuv2rgbx32_1_c;
2028 *yuv2packed2 = yuv2rgbx32_2_c;
2029 *yuv2packedX = yuv2rgbx32_X_c;
2031 #endif /* !CONFIG_SMALL */
2033 case AV_PIX_FMT_RGB32_1:
2034 case AV_PIX_FMT_BGR32_1:
2036 *yuv2packed1 = yuv2rgb32_1_1_c;
2037 *yuv2packed2 = yuv2rgb32_1_2_c;
2038 *yuv2packedX = yuv2rgb32_1_X_c;
2040 #if CONFIG_SWSCALE_ALPHA
2042 *yuv2packed1 = yuv2rgba32_1_1_c;
2043 *yuv2packed2 = yuv2rgba32_1_2_c;
2044 *yuv2packedX = yuv2rgba32_1_X_c;
2046 #endif /* CONFIG_SWSCALE_ALPHA */
2048 *yuv2packed1 = yuv2rgbx32_1_1_c;
2049 *yuv2packed2 = yuv2rgbx32_1_2_c;
2050 *yuv2packedX = yuv2rgbx32_1_X_c;
2052 #endif /* !CONFIG_SMALL */
2054 case AV_PIX_FMT_RGB24:
2055 *yuv2packed1 = yuv2rgb24_1_c;
2056 *yuv2packed2 = yuv2rgb24_2_c;
2057 *yuv2packedX = yuv2rgb24_X_c;
2059 case AV_PIX_FMT_BGR24:
2060 *yuv2packed1 = yuv2bgr24_1_c;
2061 *yuv2packed2 = yuv2bgr24_2_c;
2062 *yuv2packedX = yuv2bgr24_X_c;
2064 case AV_PIX_FMT_RGB565LE:
2065 case AV_PIX_FMT_RGB565BE:
2066 case AV_PIX_FMT_BGR565LE:
2067 case AV_PIX_FMT_BGR565BE:
2068 *yuv2packed1 = yuv2rgb16_1_c;
2069 *yuv2packed2 = yuv2rgb16_2_c;
2070 *yuv2packedX = yuv2rgb16_X_c;
2072 case AV_PIX_FMT_RGB555LE:
2073 case AV_PIX_FMT_RGB555BE:
2074 case AV_PIX_FMT_BGR555LE:
2075 case AV_PIX_FMT_BGR555BE:
2076 *yuv2packed1 = yuv2rgb15_1_c;
2077 *yuv2packed2 = yuv2rgb15_2_c;
2078 *yuv2packedX = yuv2rgb15_X_c;
2080 case AV_PIX_FMT_RGB444LE:
2081 case AV_PIX_FMT_RGB444BE:
2082 case AV_PIX_FMT_BGR444LE:
2083 case AV_PIX_FMT_BGR444BE:
2084 *yuv2packed1 = yuv2rgb12_1_c;
2085 *yuv2packed2 = yuv2rgb12_2_c;
2086 *yuv2packedX = yuv2rgb12_X_c;
2088 case AV_PIX_FMT_RGB8:
2089 case AV_PIX_FMT_BGR8:
2090 *yuv2packed1 = yuv2rgb8_1_c;
2091 *yuv2packed2 = yuv2rgb8_2_c;
2092 *yuv2packedX = yuv2rgb8_X_c;
2094 case AV_PIX_FMT_RGB4:
2095 case AV_PIX_FMT_BGR4:
2096 *yuv2packed1 = yuv2rgb4_1_c;
2097 *yuv2packed2 = yuv2rgb4_2_c;
2098 *yuv2packedX = yuv2rgb4_X_c;
2100 case AV_PIX_FMT_RGB4_BYTE:
2101 case AV_PIX_FMT_BGR4_BYTE:
2102 *yuv2packed1 = yuv2rgb4b_1_c;
2103 *yuv2packed2 = yuv2rgb4b_2_c;
2104 *yuv2packedX = yuv2rgb4b_X_c;
2108 switch (dstFormat) {
2109 case AV_PIX_FMT_MONOWHITE:
2110 *yuv2packed1 = yuv2monowhite_1_c;
2111 *yuv2packed2 = yuv2monowhite_2_c;
2112 *yuv2packedX = yuv2monowhite_X_c;
2114 case AV_PIX_FMT_MONOBLACK:
2115 *yuv2packed1 = yuv2monoblack_1_c;
2116 *yuv2packed2 = yuv2monoblack_2_c;
2117 *yuv2packedX = yuv2monoblack_X_c;
2119 case AV_PIX_FMT_YUYV422:
2120 *yuv2packed1 = yuv2yuyv422_1_c;
2121 *yuv2packed2 = yuv2yuyv422_2_c;
2122 *yuv2packedX = yuv2yuyv422_X_c;
2124 case AV_PIX_FMT_UYVY422:
2125 *yuv2packed1 = yuv2uyvy422_1_c;
2126 *yuv2packed2 = yuv2uyvy422_2_c;
2127 *yuv2packedX = yuv2uyvy422_X_c;