2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "libavutil/attributes.h"
27 #include "libavutil/avutil.h"
28 #include "libavutil/bswap.h"
29 #include "libavutil/cpu.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
36 #include "swscale_internal.h"
38 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
39 { 1, 3, 1, 3, 1, 3, 1, 3, },
40 { 2, 0, 2, 0, 2, 0, 2, 0, },
43 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
44 { 6, 2, 6, 2, 6, 2, 6, 2, },
45 { 0, 4, 0, 4, 0, 4, 0, 4, },
48 DECLARE_ALIGNED(8, const uint8_t, ff_dither_4x4_16)[4][8] = {
49 { 8, 4, 11, 7, 8, 4, 11, 7, },
50 { 2, 14, 1, 13, 2, 14, 1, 13, },
51 { 10, 6, 9, 5, 10, 6, 9, 5, },
52 { 0, 12, 3, 15, 0, 12, 3, 15, },
55 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_32)[8][8] = {
56 { 17, 9, 23, 15, 16, 8, 22, 14, },
57 { 5, 29, 3, 27, 4, 28, 2, 26, },
58 { 21, 13, 19, 11, 20, 12, 18, 10, },
59 { 0, 24, 6, 30, 1, 25, 7, 31, },
60 { 16, 8, 22, 14, 17, 9, 23, 15, },
61 { 4, 28, 2, 26, 5, 29, 3, 27, },
62 { 20, 12, 18, 10, 21, 13, 19, 11, },
63 { 1, 25, 7, 31, 0, 24, 6, 30, },
66 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_73)[8][8] = {
67 { 0, 55, 14, 68, 3, 58, 17, 72, },
68 { 37, 18, 50, 32, 40, 22, 54, 35, },
69 { 9, 64, 5, 59, 13, 67, 8, 63, },
70 { 46, 27, 41, 23, 49, 31, 44, 26, },
71 { 2, 57, 16, 71, 1, 56, 15, 70, },
72 { 39, 21, 52, 34, 38, 19, 51, 33, },
73 { 11, 66, 7, 62, 10, 65, 6, 60, },
74 { 48, 30, 43, 25, 47, 29, 42, 24, },
78 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
79 {117, 62, 158, 103, 113, 58, 155, 100, },
80 { 34, 199, 21, 186, 31, 196, 17, 182, },
81 {144, 89, 131, 76, 141, 86, 127, 72, },
82 { 0, 165, 41, 206, 10, 175, 52, 217, },
83 {110, 55, 151, 96, 120, 65, 162, 107, },
84 { 28, 193, 14, 179, 38, 203, 24, 189, },
85 {138, 83, 124, 69, 148, 93, 134, 79, },
86 { 7, 172, 48, 213, 3, 168, 45, 210, },
89 // tries to correct a gamma of 1.5
90 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
91 { 0, 143, 18, 200, 2, 156, 25, 215, },
92 { 78, 28, 125, 64, 89, 36, 138, 74, },
93 { 10, 180, 3, 161, 16, 195, 8, 175, },
94 {109, 51, 93, 38, 121, 60, 105, 47, },
95 { 1, 152, 23, 210, 0, 147, 20, 205, },
96 { 85, 33, 134, 71, 81, 30, 130, 67, },
97 { 14, 190, 6, 171, 12, 185, 5, 166, },
98 {117, 57, 101, 44, 113, 54, 97, 41, },
101 // tries to correct a gamma of 2.0
102 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
103 { 0, 124, 8, 193, 0, 140, 12, 213, },
104 { 55, 14, 104, 42, 66, 19, 119, 52, },
105 { 3, 168, 1, 145, 6, 187, 3, 162, },
106 { 86, 31, 70, 21, 99, 39, 82, 28, },
107 { 0, 134, 11, 206, 0, 129, 9, 200, },
108 { 62, 17, 114, 48, 58, 16, 109, 45, },
109 { 5, 181, 2, 157, 4, 175, 1, 151, },
110 { 95, 36, 78, 26, 90, 34, 74, 24, },
113 // tries to correct a gamma of 2.5
114 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
115 { 0, 107, 3, 187, 0, 125, 6, 212, },
116 { 39, 7, 86, 28, 49, 11, 102, 36, },
117 { 1, 158, 0, 131, 3, 180, 1, 151, },
118 { 68, 19, 52, 12, 81, 25, 64, 17, },
119 { 0, 119, 5, 203, 0, 113, 4, 195, },
120 { 45, 9, 96, 33, 42, 8, 91, 30, },
121 { 2, 172, 1, 144, 2, 165, 0, 137, },
122 { 77, 23, 60, 15, 72, 21, 56, 14, },
126 #define output_pixel(pos, val, bias, signedness) \
128 AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
130 AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
133 static av_always_inline void
134 yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
135 int big_endian, int output_bits)
138 int shift = 19 - output_bits;
140 for (i = 0; i < dstW; i++) {
141 int val = src[i] + (1 << (shift - 1));
142 output_pixel(&dest[i], val, 0, uint);
146 static av_always_inline void
147 yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
148 const int32_t **src, uint16_t *dest, int dstW,
149 int big_endian, int output_bits)
152 int shift = 15 + 16 - output_bits;
154 for (i = 0; i < dstW; i++) {
155 int val = 1 << (30-output_bits);
158 /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
159 * filters (or anything with negative coeffs, the range can be slightly
160 * wider in both directions. To account for this overflow, we subtract
161 * a constant so it always fits in the signed range (assuming a
162 * reasonable filterSize), and re-add that at the end. */
164 for (j = 0; j < filterSize; j++)
165 val += src[j][i] * filter[j];
167 output_pixel(&dest[i], val, 0x8000, int);
173 #define output_pixel(pos, val) \
175 AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
177 AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
180 static av_always_inline void
181 yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
182 int big_endian, int output_bits)
185 int shift = 15 - output_bits;
187 for (i = 0; i < dstW; i++) {
188 int val = src[i] + (1 << (shift - 1));
189 output_pixel(&dest[i], val);
193 static av_always_inline void
194 yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
195 const int16_t **src, uint16_t *dest, int dstW,
196 int big_endian, int output_bits)
199 int shift = 11 + 16 - output_bits;
201 for (i = 0; i < dstW; i++) {
202 int val = 1 << (26-output_bits);
205 for (j = 0; j < filterSize; j++)
206 val += src[j][i] * filter[j];
208 output_pixel(&dest[i], val);
214 #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
215 static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
216 uint8_t *dest, int dstW, \
217 const uint8_t *dither, int offset)\
219 yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
220 (uint16_t *) dest, dstW, is_be, bits); \
222 static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
223 const int16_t **src, uint8_t *dest, int dstW, \
224 const uint8_t *dither, int offset)\
226 yuv2planeX_## template_size ## _c_template(filter, \
227 filterSize, (const typeX_t **) src, \
228 (uint16_t *) dest, dstW, is_be, bits); \
230 yuv2NBPS( 9, BE, 1, 10, int16_t)
231 yuv2NBPS( 9, LE, 0, 10, int16_t)
232 yuv2NBPS(10, BE, 1, 10, int16_t)
233 yuv2NBPS(10, LE, 0, 10, int16_t)
234 yuv2NBPS(16, BE, 1, 16, int32_t)
235 yuv2NBPS(16, LE, 0, 16, int32_t)
237 static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
238 const int16_t **src, uint8_t *dest, int dstW,
239 const uint8_t *dither, int offset)
242 for (i=0; i<dstW; i++) {
243 int val = dither[(i + offset) & 7] << 12;
245 for (j=0; j<filterSize; j++)
246 val += src[j][i] * filter[j];
248 dest[i]= av_clip_uint8(val>>19);
252 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
253 const uint8_t *dither, int offset)
256 for (i=0; i<dstW; i++) {
257 int val = (src[i] + dither[(i + offset) & 7]) >> 7;
258 dest[i]= av_clip_uint8(val);
262 static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
263 const int16_t **chrUSrc, const int16_t **chrVSrc,
264 uint8_t *dest, int chrDstW)
266 enum AVPixelFormat dstFormat = c->dstFormat;
267 const uint8_t *chrDither = c->chrDither8;
270 if (dstFormat == AV_PIX_FMT_NV12)
271 for (i=0; i<chrDstW; i++) {
272 int u = chrDither[i & 7] << 12;
273 int v = chrDither[(i + 3) & 7] << 12;
275 for (j=0; j<chrFilterSize; j++) {
276 u += chrUSrc[j][i] * chrFilter[j];
277 v += chrVSrc[j][i] * chrFilter[j];
280 dest[2*i]= av_clip_uint8(u>>19);
281 dest[2*i+1]= av_clip_uint8(v>>19);
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(v>>19);
294 dest[2*i+1]= av_clip_uint8(u>>19);
298 #define accumulate_bit(acc, val) \
300 acc |= (val) >= (128 + 110)
301 #define output_pixel(pos, acc) \
302 if (target == AV_PIX_FMT_MONOBLACK) { \
308 static av_always_inline void
309 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
310 const int16_t **lumSrc, int lumFilterSize,
311 const int16_t *chrFilter, const int16_t **chrUSrc,
312 const int16_t **chrVSrc, int chrFilterSize,
313 const int16_t **alpSrc, uint8_t *dest, int dstW,
314 int y, enum AVPixelFormat target)
316 const uint8_t * const d128 = ff_dither_8x8_220[y&7];
320 for (i = 0; i < dstW; i += 2) {
325 for (j = 0; j < lumFilterSize; j++) {
326 Y1 += lumSrc[j][i] * lumFilter[j];
327 Y2 += lumSrc[j][i+1] * lumFilter[j];
331 if ((Y1 | Y2) & 0x100) {
332 Y1 = av_clip_uint8(Y1);
333 Y2 = av_clip_uint8(Y2);
335 accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
336 accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
338 output_pixel(*dest++, acc);
343 output_pixel(*dest, acc);
347 static av_always_inline void
348 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
349 const int16_t *ubuf[2], const int16_t *vbuf[2],
350 const int16_t *abuf[2], uint8_t *dest, int dstW,
351 int yalpha, int uvalpha, int y,
352 enum AVPixelFormat target)
354 const int16_t *buf0 = buf[0], *buf1 = buf[1];
355 const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
356 int yalpha1 = 4096 - yalpha;
359 for (i = 0; i < dstW; i += 8) {
362 Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
363 accumulate_bit(acc, Y + d128[0]);
364 Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
365 accumulate_bit(acc, Y + d128[1]);
366 Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
367 accumulate_bit(acc, Y + d128[2]);
368 Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
369 accumulate_bit(acc, Y + d128[3]);
370 Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
371 accumulate_bit(acc, Y + d128[4]);
372 Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
373 accumulate_bit(acc, Y + d128[5]);
374 Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
375 accumulate_bit(acc, Y + d128[6]);
376 Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
377 accumulate_bit(acc, Y + d128[7]);
379 output_pixel(*dest++, acc);
383 static av_always_inline void
384 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
385 const int16_t *ubuf[2], const int16_t *vbuf[2],
386 const int16_t *abuf0, uint8_t *dest, int dstW,
387 int uvalpha, int y, enum AVPixelFormat target)
389 const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
392 for (i = 0; i < dstW; i += 8) {
395 accumulate_bit(acc, (buf0[i + 0] >> 7) + d128[0]);
396 accumulate_bit(acc, (buf0[i + 1] >> 7) + d128[1]);
397 accumulate_bit(acc, (buf0[i + 2] >> 7) + d128[2]);
398 accumulate_bit(acc, (buf0[i + 3] >> 7) + d128[3]);
399 accumulate_bit(acc, (buf0[i + 4] >> 7) + d128[4]);
400 accumulate_bit(acc, (buf0[i + 5] >> 7) + d128[5]);
401 accumulate_bit(acc, (buf0[i + 6] >> 7) + d128[6]);
402 accumulate_bit(acc, (buf0[i + 7] >> 7) + d128[7]);
404 output_pixel(*dest++, acc);
409 #undef accumulate_bit
411 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
412 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
413 const int16_t **lumSrc, int lumFilterSize, \
414 const int16_t *chrFilter, const int16_t **chrUSrc, \
415 const int16_t **chrVSrc, int chrFilterSize, \
416 const int16_t **alpSrc, uint8_t *dest, int dstW, \
419 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
420 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
421 alpSrc, dest, dstW, y, fmt); \
424 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
425 const int16_t *ubuf[2], const int16_t *vbuf[2], \
426 const int16_t *abuf[2], uint8_t *dest, int dstW, \
427 int yalpha, int uvalpha, int y) \
429 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
430 dest, dstW, yalpha, uvalpha, y, fmt); \
433 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
434 const int16_t *ubuf[2], const int16_t *vbuf[2], \
435 const int16_t *abuf0, uint8_t *dest, int dstW, \
436 int uvalpha, int y) \
438 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
439 abuf0, dest, dstW, uvalpha, \
443 YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
444 YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
446 #define output_pixels(pos, Y1, U, Y2, V) \
447 if (target == AV_PIX_FMT_YUYV422) { \
448 dest[pos + 0] = Y1; \
450 dest[pos + 2] = Y2; \
452 } else if (target == AV_PIX_FMT_YVYU422) { \
453 dest[pos + 0] = Y1; \
455 dest[pos + 2] = Y2; \
457 } else { /* AV_PIX_FMT_UYVY422 */ \
459 dest[pos + 1] = Y1; \
461 dest[pos + 3] = Y2; \
464 static av_always_inline void
465 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
466 const int16_t **lumSrc, int lumFilterSize,
467 const int16_t *chrFilter, const int16_t **chrUSrc,
468 const int16_t **chrVSrc, int chrFilterSize,
469 const int16_t **alpSrc, uint8_t *dest, int dstW,
470 int y, enum AVPixelFormat target)
474 for (i = 0; i < ((dstW + 1) >> 1); i++) {
481 for (j = 0; j < lumFilterSize; j++) {
482 Y1 += lumSrc[j][i * 2] * lumFilter[j];
483 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
485 for (j = 0; j < chrFilterSize; j++) {
486 U += chrUSrc[j][i] * chrFilter[j];
487 V += chrVSrc[j][i] * chrFilter[j];
493 if ((Y1 | Y2 | U | V) & 0x100) {
494 Y1 = av_clip_uint8(Y1);
495 Y2 = av_clip_uint8(Y2);
496 U = av_clip_uint8(U);
497 V = av_clip_uint8(V);
499 output_pixels(4*i, Y1, U, Y2, V);
503 static av_always_inline void
504 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
505 const int16_t *ubuf[2], const int16_t *vbuf[2],
506 const int16_t *abuf[2], uint8_t *dest, int dstW,
507 int yalpha, int uvalpha, int y,
508 enum AVPixelFormat target)
510 const int16_t *buf0 = buf[0], *buf1 = buf[1],
511 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
512 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
513 int yalpha1 = 4096 - yalpha;
514 int uvalpha1 = 4096 - uvalpha;
517 for (i = 0; i < ((dstW + 1) >> 1); i++) {
518 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
519 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
520 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
521 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
523 Y1 = av_clip_uint8(Y1);
524 Y2 = av_clip_uint8(Y2);
525 U = av_clip_uint8(U);
526 V = av_clip_uint8(V);
528 output_pixels(i * 4, Y1, U, Y2, V);
532 static av_always_inline void
533 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
534 const int16_t *ubuf[2], const int16_t *vbuf[2],
535 const int16_t *abuf0, uint8_t *dest, int dstW,
536 int uvalpha, int y, enum AVPixelFormat target)
538 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
541 if (uvalpha < 2048) {
542 for (i = 0; i < ((dstW + 1) >> 1); i++) {
543 int Y1 = buf0[i * 2] >> 7;
544 int Y2 = buf0[i * 2 + 1] >> 7;
545 int U = ubuf0[i] >> 7;
546 int V = vbuf0[i] >> 7;
548 Y1 = av_clip_uint8(Y1);
549 Y2 = av_clip_uint8(Y2);
550 U = av_clip_uint8(U);
551 V = av_clip_uint8(V);
553 output_pixels(i * 4, Y1, U, Y2, V);
556 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
557 for (i = 0; i < ((dstW + 1) >> 1); i++) {
558 int Y1 = buf0[i * 2] >> 7;
559 int Y2 = buf0[i * 2 + 1] >> 7;
560 int U = (ubuf0[i] + ubuf1[i]) >> 8;
561 int V = (vbuf0[i] + vbuf1[i]) >> 8;
563 Y1 = av_clip_uint8(Y1);
564 Y2 = av_clip_uint8(Y2);
565 U = av_clip_uint8(U);
566 V = av_clip_uint8(V);
568 output_pixels(i * 4, Y1, U, Y2, V);
575 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
576 YUV2PACKEDWRAPPER(yuv2, 422, yvyu422, AV_PIX_FMT_YVYU422)
577 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
579 #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? R : B)
580 #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? B : R)
581 #define output_pixel(pos, val) \
582 if (isBE(target)) { \
588 static av_always_inline void
589 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
590 const int32_t **lumSrc, int lumFilterSize,
591 const int16_t *chrFilter, const int32_t **chrUSrc,
592 const int32_t **chrVSrc, int chrFilterSize,
593 const int32_t **alpSrc, uint16_t *dest, int dstW,
594 int y, enum AVPixelFormat target)
598 for (i = 0; i < ((dstW + 1) >> 1); i++) {
600 int Y1 = -0x40000000;
601 int Y2 = -0x40000000;
602 int U = -128 << 23; // 19
606 for (j = 0; j < lumFilterSize; j++) {
607 Y1 += lumSrc[j][i * 2] * lumFilter[j];
608 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
610 for (j = 0; j < chrFilterSize; j++) {
611 U += chrUSrc[j][i] * chrFilter[j];
612 V += chrVSrc[j][i] * chrFilter[j];
615 // 8 bits: 12+15=27; 16 bits: 12+19=31
623 // 8 bits: 27 -> 17 bits, 16 bits: 31 - 14 = 17 bits
624 Y1 -= c->yuv2rgb_y_offset;
625 Y2 -= c->yuv2rgb_y_offset;
626 Y1 *= c->yuv2rgb_y_coeff;
627 Y2 *= c->yuv2rgb_y_coeff;
630 // 8 bits: 17 + 13 bits = 30 bits, 16 bits: 17 + 13 bits = 30 bits
632 R = V * c->yuv2rgb_v2r_coeff;
633 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
634 B = U * c->yuv2rgb_u2b_coeff;
636 // 8 bits: 30 - 22 = 8 bits, 16 bits: 30 bits - 14 = 16 bits
637 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
638 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
639 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
640 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
641 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
642 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
647 static av_always_inline void
648 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
649 const int32_t *ubuf[2], const int32_t *vbuf[2],
650 const int32_t *abuf[2], uint16_t *dest, int dstW,
651 int yalpha, int uvalpha, int y,
652 enum AVPixelFormat target)
654 const int32_t *buf0 = buf[0], *buf1 = buf[1],
655 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
656 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
657 int yalpha1 = 4096 - yalpha;
658 int uvalpha1 = 4096 - uvalpha;
661 for (i = 0; i < ((dstW + 1) >> 1); i++) {
662 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
663 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
664 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
665 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
668 Y1 -= c->yuv2rgb_y_offset;
669 Y2 -= c->yuv2rgb_y_offset;
670 Y1 *= c->yuv2rgb_y_coeff;
671 Y2 *= c->yuv2rgb_y_coeff;
675 R = V * c->yuv2rgb_v2r_coeff;
676 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
677 B = U * c->yuv2rgb_u2b_coeff;
679 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
680 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
681 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
682 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
683 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
684 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
689 static av_always_inline void
690 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
691 const int32_t *ubuf[2], const int32_t *vbuf[2],
692 const int32_t *abuf0, uint16_t *dest, int dstW,
693 int uvalpha, int y, enum AVPixelFormat target)
695 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
698 if (uvalpha < 2048) {
699 for (i = 0; i < ((dstW + 1) >> 1); i++) {
700 int Y1 = (buf0[i * 2] ) >> 2;
701 int Y2 = (buf0[i * 2 + 1]) >> 2;
702 int U = (ubuf0[i] + (-128 << 11)) >> 2;
703 int V = (vbuf0[i] + (-128 << 11)) >> 2;
706 Y1 -= c->yuv2rgb_y_offset;
707 Y2 -= c->yuv2rgb_y_offset;
708 Y1 *= c->yuv2rgb_y_coeff;
709 Y2 *= c->yuv2rgb_y_coeff;
713 R = V * c->yuv2rgb_v2r_coeff;
714 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
715 B = U * c->yuv2rgb_u2b_coeff;
717 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
718 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
719 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
720 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
721 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
722 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
726 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
727 for (i = 0; i < ((dstW + 1) >> 1); i++) {
728 int Y1 = (buf0[i * 2] ) >> 2;
729 int Y2 = (buf0[i * 2 + 1]) >> 2;
730 int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
731 int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
734 Y1 -= c->yuv2rgb_y_offset;
735 Y2 -= c->yuv2rgb_y_offset;
736 Y1 *= c->yuv2rgb_y_coeff;
737 Y2 *= c->yuv2rgb_y_coeff;
741 R = V * c->yuv2rgb_v2r_coeff;
742 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
743 B = U * c->yuv2rgb_u2b_coeff;
745 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
746 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
747 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
748 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
749 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
750 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
760 #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
761 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
762 const int16_t **_lumSrc, int lumFilterSize, \
763 const int16_t *chrFilter, const int16_t **_chrUSrc, \
764 const int16_t **_chrVSrc, int chrFilterSize, \
765 const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
768 const int32_t **lumSrc = (const int32_t **) _lumSrc, \
769 **chrUSrc = (const int32_t **) _chrUSrc, \
770 **chrVSrc = (const int32_t **) _chrVSrc, \
771 **alpSrc = (const int32_t **) _alpSrc; \
772 uint16_t *dest = (uint16_t *) _dest; \
773 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
774 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
775 alpSrc, dest, dstW, y, fmt); \
778 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
779 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
780 const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
781 int yalpha, int uvalpha, int y) \
783 const int32_t **buf = (const int32_t **) _buf, \
784 **ubuf = (const int32_t **) _ubuf, \
785 **vbuf = (const int32_t **) _vbuf, \
786 **abuf = (const int32_t **) _abuf; \
787 uint16_t *dest = (uint16_t *) _dest; \
788 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
789 dest, dstW, yalpha, uvalpha, y, fmt); \
792 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
793 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
794 const int16_t *_abuf0, uint8_t *_dest, int dstW, \
795 int uvalpha, int y) \
797 const int32_t *buf0 = (const int32_t *) _buf0, \
798 **ubuf = (const int32_t **) _ubuf, \
799 **vbuf = (const int32_t **) _vbuf, \
800 *abuf0 = (const int32_t *) _abuf0; \
801 uint16_t *dest = (uint16_t *) _dest; \
802 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
803 dstW, uvalpha, y, fmt); \
806 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, AV_PIX_FMT_RGB48BE)
807 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, AV_PIX_FMT_RGB48LE)
808 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, AV_PIX_FMT_BGR48BE)
809 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, AV_PIX_FMT_BGR48LE)
812 * Write out 2 RGB pixels in the target pixel format. This function takes a
813 * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
814 * things like endianness conversion and shifting. The caller takes care of
815 * setting the correct offset in these tables from the chroma (U/V) values.
816 * This function then uses the luminance (Y1/Y2) values to write out the
817 * correct RGB values into the destination buffer.
819 static av_always_inline void
820 yuv2rgb_write(uint8_t *_dest, int i, unsigned Y1, unsigned Y2,
821 unsigned A1, unsigned A2,
822 const void *_r, const void *_g, const void *_b, int y,
823 enum AVPixelFormat target, int hasAlpha)
825 if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
826 target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
827 uint32_t *dest = (uint32_t *) _dest;
828 const uint32_t *r = (const uint32_t *) _r;
829 const uint32_t *g = (const uint32_t *) _g;
830 const uint32_t *b = (const uint32_t *) _b;
833 int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
835 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
836 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
839 int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
841 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
842 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
844 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
845 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
848 } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
849 uint8_t *dest = (uint8_t *) _dest;
850 const uint8_t *r = (const uint8_t *) _r;
851 const uint8_t *g = (const uint8_t *) _g;
852 const uint8_t *b = (const uint8_t *) _b;
854 #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
855 #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
856 dest[i * 6 + 0] = r_b[Y1];
857 dest[i * 6 + 1] = g[Y1];
858 dest[i * 6 + 2] = b_r[Y1];
859 dest[i * 6 + 3] = r_b[Y2];
860 dest[i * 6 + 4] = g[Y2];
861 dest[i * 6 + 5] = b_r[Y2];
864 } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
865 target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
866 target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
867 uint16_t *dest = (uint16_t *) _dest;
868 const uint16_t *r = (const uint16_t *) _r;
869 const uint16_t *g = (const uint16_t *) _g;
870 const uint16_t *b = (const uint16_t *) _b;
871 int dr1, dg1, db1, dr2, dg2, db2;
873 if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
874 dr1 = dither_2x2_8[ y & 1 ][0];
875 dg1 = dither_2x2_4[ y & 1 ][0];
876 db1 = dither_2x2_8[(y & 1) ^ 1][0];
877 dr2 = dither_2x2_8[ y & 1 ][1];
878 dg2 = dither_2x2_4[ y & 1 ][1];
879 db2 = dither_2x2_8[(y & 1) ^ 1][1];
880 } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
881 dr1 = dither_2x2_8[ y & 1 ][0];
882 dg1 = dither_2x2_8[ y & 1 ][1];
883 db1 = dither_2x2_8[(y & 1) ^ 1][0];
884 dr2 = dither_2x2_8[ y & 1 ][1];
885 dg2 = dither_2x2_8[ y & 1 ][0];
886 db2 = dither_2x2_8[(y & 1) ^ 1][1];
888 dr1 = ff_dither_4x4_16[ y & 3 ][0];
889 dg1 = ff_dither_4x4_16[ y & 3 ][1];
890 db1 = ff_dither_4x4_16[(y & 3) ^ 3][0];
891 dr2 = ff_dither_4x4_16[ y & 3 ][1];
892 dg2 = ff_dither_4x4_16[ y & 3 ][0];
893 db2 = ff_dither_4x4_16[(y & 3) ^ 3][1];
896 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
897 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
898 } else /* 8/4 bits */ {
899 uint8_t *dest = (uint8_t *) _dest;
900 const uint8_t *r = (const uint8_t *) _r;
901 const uint8_t *g = (const uint8_t *) _g;
902 const uint8_t *b = (const uint8_t *) _b;
903 int dr1, dg1, db1, dr2, dg2, db2;
905 if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
906 const uint8_t * const d64 = ff_dither_8x8_73[y & 7];
907 const uint8_t * const d32 = ff_dither_8x8_32[y & 7];
908 dr1 = dg1 = d32[(i * 2 + 0) & 7];
909 db1 = d64[(i * 2 + 0) & 7];
910 dr2 = dg2 = d32[(i * 2 + 1) & 7];
911 db2 = d64[(i * 2 + 1) & 7];
913 const uint8_t * const d64 = ff_dither_8x8_73 [y & 7];
914 const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
915 dr1 = db1 = d128[(i * 2 + 0) & 7];
916 dg1 = d64[(i * 2 + 0) & 7];
917 dr2 = db2 = d128[(i * 2 + 1) & 7];
918 dg2 = d64[(i * 2 + 1) & 7];
921 if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
922 dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
923 ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
925 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
926 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
931 static av_always_inline void
932 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
933 const int16_t **lumSrc, int lumFilterSize,
934 const int16_t *chrFilter, const int16_t **chrUSrc,
935 const int16_t **chrVSrc, int chrFilterSize,
936 const int16_t **alpSrc, uint8_t *dest, int dstW,
937 int y, enum AVPixelFormat target, int hasAlpha)
941 for (i = 0; i < ((dstW + 1) >> 1); i++) {
947 const void *r, *g, *b;
949 for (j = 0; j < lumFilterSize; j++) {
950 Y1 += lumSrc[j][i * 2] * lumFilter[j];
951 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
953 for (j = 0; j < chrFilterSize; j++) {
954 U += chrUSrc[j][i] * chrFilter[j];
955 V += chrVSrc[j][i] * chrFilter[j];
961 if ((Y1 | Y2 | U | V) & 0x100) {
962 Y1 = av_clip_uint8(Y1);
963 Y2 = av_clip_uint8(Y2);
964 U = av_clip_uint8(U);
965 V = av_clip_uint8(V);
970 for (j = 0; j < lumFilterSize; j++) {
971 A1 += alpSrc[j][i * 2 ] * lumFilter[j];
972 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
976 if ((A1 | A2) & 0x100) {
977 A1 = av_clip_uint8(A1);
978 A2 = av_clip_uint8(A2);
982 /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
984 g = (c->table_gU[U] + c->table_gV[V]);
987 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
988 r, g, b, y, target, hasAlpha);
992 static av_always_inline void
993 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
994 const int16_t *ubuf[2], const int16_t *vbuf[2],
995 const int16_t *abuf[2], uint8_t *dest, int dstW,
996 int yalpha, int uvalpha, int y,
997 enum AVPixelFormat target, int hasAlpha)
999 const int16_t *buf0 = buf[0], *buf1 = buf[1],
1000 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1001 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1002 *abuf0 = hasAlpha ? abuf[0] : NULL,
1003 *abuf1 = hasAlpha ? abuf[1] : NULL;
1004 int yalpha1 = 4096 - yalpha;
1005 int uvalpha1 = 4096 - uvalpha;
1008 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1009 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
1010 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
1011 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
1012 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
1014 const void *r, *g, *b;
1016 Y1 = av_clip_uint8(Y1);
1017 Y2 = av_clip_uint8(Y2);
1018 U = av_clip_uint8(U);
1019 V = av_clip_uint8(V);
1022 g = (c->table_gU[U] + c->table_gV[V]);
1026 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
1027 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1028 A1 = av_clip_uint8(A1);
1029 A2 = av_clip_uint8(A2);
1032 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1033 r, g, b, y, target, hasAlpha);
1037 static av_always_inline void
1038 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1039 const int16_t *ubuf[2], const int16_t *vbuf[2],
1040 const int16_t *abuf0, uint8_t *dest, int dstW,
1041 int uvalpha, int y, enum AVPixelFormat target,
1044 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1047 if (uvalpha < 2048) {
1048 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1049 int Y1 = buf0[i * 2] >> 7;
1050 int Y2 = buf0[i * 2 + 1] >> 7;
1051 int U = ubuf0[i] >> 7;
1052 int V = vbuf0[i] >> 7;
1054 const void *r, *g, *b;
1056 Y1 = av_clip_uint8(Y1);
1057 Y2 = av_clip_uint8(Y2);
1058 U = av_clip_uint8(U);
1059 V = av_clip_uint8(V);
1062 g = (c->table_gU[U] + c->table_gV[V]);
1066 A1 = abuf0[i * 2 ] >> 7;
1067 A2 = abuf0[i * 2 + 1] >> 7;
1068 A1 = av_clip_uint8(A1);
1069 A2 = av_clip_uint8(A2);
1072 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1073 r, g, b, y, target, hasAlpha);
1076 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1077 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1078 int Y1 = buf0[i * 2] >> 7;
1079 int Y2 = buf0[i * 2 + 1] >> 7;
1080 int U = (ubuf0[i] + ubuf1[i]) >> 8;
1081 int V = (vbuf0[i] + vbuf1[i]) >> 8;
1083 const void *r, *g, *b;
1085 Y1 = av_clip_uint8(Y1);
1086 Y2 = av_clip_uint8(Y2);
1087 U = av_clip_uint8(U);
1088 V = av_clip_uint8(V);
1091 g = (c->table_gU[U] + c->table_gV[V]);
1095 A1 = abuf0[i * 2 ] >> 7;
1096 A2 = abuf0[i * 2 + 1] >> 7;
1097 A1 = av_clip_uint8(A1);
1098 A2 = av_clip_uint8(A2);
1101 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1102 r, g, b, y, target, hasAlpha);
1107 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1108 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1109 const int16_t **lumSrc, int lumFilterSize, \
1110 const int16_t *chrFilter, const int16_t **chrUSrc, \
1111 const int16_t **chrVSrc, int chrFilterSize, \
1112 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1115 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1116 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1117 alpSrc, dest, dstW, y, fmt, hasAlpha); \
1119 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1120 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1121 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1122 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1123 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1124 int yalpha, int uvalpha, int y) \
1126 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1127 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1130 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1131 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1132 const int16_t *abuf0, uint8_t *dest, int dstW, \
1133 int uvalpha, int y) \
1135 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1136 dstW, uvalpha, y, fmt, hasAlpha); \
1140 YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1141 YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1143 #if CONFIG_SWSCALE_ALPHA
1144 YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
1145 YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
1147 YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
1148 YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
1150 YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
1151 YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
1152 YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
1153 YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
1154 YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
1155 YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
1156 YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
1157 YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
1159 static av_always_inline void
1160 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1161 const int16_t **lumSrc, int lumFilterSize,
1162 const int16_t *chrFilter, const int16_t **chrUSrc,
1163 const int16_t **chrVSrc, int chrFilterSize,
1164 const int16_t **alpSrc, uint8_t *dest,
1165 int dstW, int y, enum AVPixelFormat target, int hasAlpha)
1168 int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1170 for (i = 0; i < dstW; i++) {
1177 for (j = 0; j < lumFilterSize; j++) {
1178 Y += lumSrc[j][i] * lumFilter[j];
1180 for (j = 0; j < chrFilterSize; j++) {
1181 U += chrUSrc[j][i] * chrFilter[j];
1182 V += chrVSrc[j][i] * chrFilter[j];
1189 for (j = 0; j < lumFilterSize; j++) {
1190 A += alpSrc[j][i] * lumFilter[j];
1194 A = av_clip_uint8(A);
1196 Y -= c->yuv2rgb_y_offset;
1197 Y *= c->yuv2rgb_y_coeff;
1199 R = Y + V*c->yuv2rgb_v2r_coeff;
1200 G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1201 B = Y + U*c->yuv2rgb_u2b_coeff;
1202 if ((R | G | B) & 0xC0000000) {
1203 R = av_clip_uintp2(R, 30);
1204 G = av_clip_uintp2(G, 30);
1205 B = av_clip_uintp2(B, 30);
1209 case AV_PIX_FMT_ARGB:
1210 dest[0] = hasAlpha ? A : 255;
1215 case AV_PIX_FMT_RGB24:
1220 case AV_PIX_FMT_RGBA:
1224 dest[3] = hasAlpha ? A : 255;
1226 case AV_PIX_FMT_ABGR:
1227 dest[0] = hasAlpha ? A : 255;
1233 case AV_PIX_FMT_BGR24:
1238 case AV_PIX_FMT_BGRA:
1242 dest[3] = hasAlpha ? A : 255;
1250 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1251 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1252 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1253 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1255 #if CONFIG_SWSCALE_ALPHA
1256 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
1257 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
1258 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
1259 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
1261 YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
1262 YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
1263 YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
1264 YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
1266 YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
1267 YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
1270 yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
1271 const int16_t **lumSrc, int lumFilterSize,
1272 const int16_t *chrFilter, const int16_t **chrUSrc,
1273 const int16_t **chrVSrc, int chrFilterSize,
1274 const int16_t **alpSrc, uint8_t **dest,
1277 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
1279 int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
1280 uint16_t **dest16 = (uint16_t**)dest;
1281 int SH = 22 + 8 - desc->comp[0].depth;
1283 for (i = 0; i < dstW; i++) {
1286 int U = (1 << 9) - (128 << 19);
1287 int V = (1 << 9) - (128 << 19);
1290 for (j = 0; j < lumFilterSize; j++)
1291 Y += lumSrc[j][i] * lumFilter[j];
1293 for (j = 0; j < chrFilterSize; j++) {
1294 U += chrUSrc[j][i] * chrFilter[j];
1295 V += chrVSrc[j][i] * chrFilter[j];
1305 for (j = 0; j < lumFilterSize; j++)
1306 A += alpSrc[j][i] * lumFilter[j];
1311 A = av_clip_uint8(A);
1314 Y -= c->yuv2rgb_y_offset;
1315 Y *= c->yuv2rgb_y_coeff;
1317 R = Y + V * c->yuv2rgb_v2r_coeff;
1318 G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1319 B = Y + U * c->yuv2rgb_u2b_coeff;
1321 if ((R | G | B) & 0xC0000000) {
1322 R = av_clip_uintp2(R, 30);
1323 G = av_clip_uintp2(G, 30);
1324 B = av_clip_uintp2(B, 30);
1328 dest16[0][i] = G >> SH;
1329 dest16[1][i] = B >> SH;
1330 dest16[2][i] = R >> SH;
1334 dest[0][i] = G >> 22;
1335 dest[1][i] = B >> 22;
1336 dest[2][i] = R >> 22;
1341 if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
1342 for (i = 0; i < dstW; i++) {
1343 dest16[0][i] = av_bswap16(dest16[0][i]);
1344 dest16[1][i] = av_bswap16(dest16[1][i]);
1345 dest16[2][i] = av_bswap16(dest16[2][i]);
1347 dest16[3][i] = av_bswap16(dest16[3][i]);
1352 av_cold void ff_sws_init_output_funcs(SwsContext *c,
1353 yuv2planar1_fn *yuv2plane1,
1354 yuv2planarX_fn *yuv2planeX,
1355 yuv2interleavedX_fn *yuv2nv12cX,
1356 yuv2packed1_fn *yuv2packed1,
1357 yuv2packed2_fn *yuv2packed2,
1358 yuv2packedX_fn *yuv2packedX,
1359 yuv2anyX_fn *yuv2anyX)
1361 enum AVPixelFormat dstFormat = c->dstFormat;
1362 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
1364 if (is16BPS(dstFormat)) {
1365 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
1366 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
1367 } else if (is9_OR_10BPS(dstFormat)) {
1368 if (desc->comp[0].depth == 9) {
1369 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
1370 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
1372 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
1373 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
1376 *yuv2plane1 = yuv2plane1_8_c;
1377 *yuv2planeX = yuv2planeX_8_c;
1378 if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
1379 *yuv2nv12cX = yuv2nv12cX_c;
1382 if(c->flags & SWS_FULL_CHR_H_INT) {
1383 switch (dstFormat) {
1384 case AV_PIX_FMT_RGBA:
1386 *yuv2packedX = yuv2rgba32_full_X_c;
1388 #if CONFIG_SWSCALE_ALPHA
1390 *yuv2packedX = yuv2rgba32_full_X_c;
1392 #endif /* CONFIG_SWSCALE_ALPHA */
1394 *yuv2packedX = yuv2rgbx32_full_X_c;
1396 #endif /* !CONFIG_SMALL */
1398 case AV_PIX_FMT_ARGB:
1400 *yuv2packedX = yuv2argb32_full_X_c;
1402 #if CONFIG_SWSCALE_ALPHA
1404 *yuv2packedX = yuv2argb32_full_X_c;
1406 #endif /* CONFIG_SWSCALE_ALPHA */
1408 *yuv2packedX = yuv2xrgb32_full_X_c;
1410 #endif /* !CONFIG_SMALL */
1412 case AV_PIX_FMT_BGRA:
1414 *yuv2packedX = yuv2bgra32_full_X_c;
1416 #if CONFIG_SWSCALE_ALPHA
1418 *yuv2packedX = yuv2bgra32_full_X_c;
1420 #endif /* CONFIG_SWSCALE_ALPHA */
1422 *yuv2packedX = yuv2bgrx32_full_X_c;
1424 #endif /* !CONFIG_SMALL */
1426 case AV_PIX_FMT_ABGR:
1428 *yuv2packedX = yuv2abgr32_full_X_c;
1430 #if CONFIG_SWSCALE_ALPHA
1432 *yuv2packedX = yuv2abgr32_full_X_c;
1434 #endif /* CONFIG_SWSCALE_ALPHA */
1436 *yuv2packedX = yuv2xbgr32_full_X_c;
1438 #endif /* !CONFIG_SMALL */
1440 case AV_PIX_FMT_RGB24:
1441 *yuv2packedX = yuv2rgb24_full_X_c;
1443 case AV_PIX_FMT_BGR24:
1444 *yuv2packedX = yuv2bgr24_full_X_c;
1446 case AV_PIX_FMT_GBRP:
1447 case AV_PIX_FMT_GBRP9BE:
1448 case AV_PIX_FMT_GBRP9LE:
1449 case AV_PIX_FMT_GBRP10BE:
1450 case AV_PIX_FMT_GBRP10LE:
1451 case AV_PIX_FMT_GBRP16BE:
1452 case AV_PIX_FMT_GBRP16LE:
1453 case AV_PIX_FMT_GBRAP:
1454 *yuv2anyX = yuv2gbrp_full_X_c;
1458 switch (dstFormat) {
1459 case AV_PIX_FMT_RGB48LE:
1460 *yuv2packed1 = yuv2rgb48le_1_c;
1461 *yuv2packed2 = yuv2rgb48le_2_c;
1462 *yuv2packedX = yuv2rgb48le_X_c;
1464 case AV_PIX_FMT_RGB48BE:
1465 *yuv2packed1 = yuv2rgb48be_1_c;
1466 *yuv2packed2 = yuv2rgb48be_2_c;
1467 *yuv2packedX = yuv2rgb48be_X_c;
1469 case AV_PIX_FMT_BGR48LE:
1470 *yuv2packed1 = yuv2bgr48le_1_c;
1471 *yuv2packed2 = yuv2bgr48le_2_c;
1472 *yuv2packedX = yuv2bgr48le_X_c;
1474 case AV_PIX_FMT_BGR48BE:
1475 *yuv2packed1 = yuv2bgr48be_1_c;
1476 *yuv2packed2 = yuv2bgr48be_2_c;
1477 *yuv2packedX = yuv2bgr48be_X_c;
1479 case AV_PIX_FMT_RGB32:
1480 case AV_PIX_FMT_BGR32:
1482 *yuv2packed1 = yuv2rgb32_1_c;
1483 *yuv2packed2 = yuv2rgb32_2_c;
1484 *yuv2packedX = yuv2rgb32_X_c;
1486 #if CONFIG_SWSCALE_ALPHA
1488 *yuv2packed1 = yuv2rgba32_1_c;
1489 *yuv2packed2 = yuv2rgba32_2_c;
1490 *yuv2packedX = yuv2rgba32_X_c;
1492 #endif /* CONFIG_SWSCALE_ALPHA */
1494 *yuv2packed1 = yuv2rgbx32_1_c;
1495 *yuv2packed2 = yuv2rgbx32_2_c;
1496 *yuv2packedX = yuv2rgbx32_X_c;
1498 #endif /* !CONFIG_SMALL */
1500 case AV_PIX_FMT_RGB32_1:
1501 case AV_PIX_FMT_BGR32_1:
1503 *yuv2packed1 = yuv2rgb32_1_1_c;
1504 *yuv2packed2 = yuv2rgb32_1_2_c;
1505 *yuv2packedX = yuv2rgb32_1_X_c;
1507 #if CONFIG_SWSCALE_ALPHA
1509 *yuv2packed1 = yuv2rgba32_1_1_c;
1510 *yuv2packed2 = yuv2rgba32_1_2_c;
1511 *yuv2packedX = yuv2rgba32_1_X_c;
1513 #endif /* CONFIG_SWSCALE_ALPHA */
1515 *yuv2packed1 = yuv2rgbx32_1_1_c;
1516 *yuv2packed2 = yuv2rgbx32_1_2_c;
1517 *yuv2packedX = yuv2rgbx32_1_X_c;
1519 #endif /* !CONFIG_SMALL */
1521 case AV_PIX_FMT_RGB24:
1522 *yuv2packed1 = yuv2rgb24_1_c;
1523 *yuv2packed2 = yuv2rgb24_2_c;
1524 *yuv2packedX = yuv2rgb24_X_c;
1526 case AV_PIX_FMT_BGR24:
1527 *yuv2packed1 = yuv2bgr24_1_c;
1528 *yuv2packed2 = yuv2bgr24_2_c;
1529 *yuv2packedX = yuv2bgr24_X_c;
1531 case AV_PIX_FMT_RGB565LE:
1532 case AV_PIX_FMT_RGB565BE:
1533 case AV_PIX_FMT_BGR565LE:
1534 case AV_PIX_FMT_BGR565BE:
1535 *yuv2packed1 = yuv2rgb16_1_c;
1536 *yuv2packed2 = yuv2rgb16_2_c;
1537 *yuv2packedX = yuv2rgb16_X_c;
1539 case AV_PIX_FMT_RGB555LE:
1540 case AV_PIX_FMT_RGB555BE:
1541 case AV_PIX_FMT_BGR555LE:
1542 case AV_PIX_FMT_BGR555BE:
1543 *yuv2packed1 = yuv2rgb15_1_c;
1544 *yuv2packed2 = yuv2rgb15_2_c;
1545 *yuv2packedX = yuv2rgb15_X_c;
1547 case AV_PIX_FMT_RGB444LE:
1548 case AV_PIX_FMT_RGB444BE:
1549 case AV_PIX_FMT_BGR444LE:
1550 case AV_PIX_FMT_BGR444BE:
1551 *yuv2packed1 = yuv2rgb12_1_c;
1552 *yuv2packed2 = yuv2rgb12_2_c;
1553 *yuv2packedX = yuv2rgb12_X_c;
1555 case AV_PIX_FMT_RGB8:
1556 case AV_PIX_FMT_BGR8:
1557 *yuv2packed1 = yuv2rgb8_1_c;
1558 *yuv2packed2 = yuv2rgb8_2_c;
1559 *yuv2packedX = yuv2rgb8_X_c;
1561 case AV_PIX_FMT_RGB4:
1562 case AV_PIX_FMT_BGR4:
1563 *yuv2packed1 = yuv2rgb4_1_c;
1564 *yuv2packed2 = yuv2rgb4_2_c;
1565 *yuv2packedX = yuv2rgb4_X_c;
1567 case AV_PIX_FMT_RGB4_BYTE:
1568 case AV_PIX_FMT_BGR4_BYTE:
1569 *yuv2packed1 = yuv2rgb4b_1_c;
1570 *yuv2packed2 = yuv2rgb4b_2_c;
1571 *yuv2packedX = yuv2rgb4b_X_c;
1575 switch (dstFormat) {
1576 case AV_PIX_FMT_MONOWHITE:
1577 *yuv2packed1 = yuv2monowhite_1_c;
1578 *yuv2packed2 = yuv2monowhite_2_c;
1579 *yuv2packedX = yuv2monowhite_X_c;
1581 case AV_PIX_FMT_MONOBLACK:
1582 *yuv2packed1 = yuv2monoblack_1_c;
1583 *yuv2packed2 = yuv2monoblack_2_c;
1584 *yuv2packedX = yuv2monoblack_X_c;
1586 case AV_PIX_FMT_YUYV422:
1587 *yuv2packed1 = yuv2yuyv422_1_c;
1588 *yuv2packed2 = yuv2yuyv422_2_c;
1589 *yuv2packedX = yuv2yuyv422_X_c;
1591 case AV_PIX_FMT_YVYU422:
1592 *yuv2packed1 = yuv2yvyu422_1_c;
1593 *yuv2packed2 = yuv2yvyu422_2_c;
1594 *yuv2packedX = yuv2yvyu422_X_c;
1596 case AV_PIX_FMT_UYVY422:
1597 *yuv2packed1 = yuv2uyvy422_1_c;
1598 *yuv2packed2 = yuv2uyvy422_2_c;
1599 *yuv2packedX = yuv2uyvy422_X_c;