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
26 #include "libavutil/attributes.h"
27 #include "libavutil/avutil.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/bswap.h"
30 #include "libavutil/cpu.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/pixdesc.h"
37 #include "swscale_internal.h"
39 DECLARE_ALIGNED(8, const uint8_t, ff_dither_2x2_4)[][8] = {
40 { 1, 3, 1, 3, 1, 3, 1, 3, },
41 { 2, 0, 2, 0, 2, 0, 2, 0, },
42 { 1, 3, 1, 3, 1, 3, 1, 3, },
45 DECLARE_ALIGNED(8, const uint8_t, ff_dither_2x2_8)[][8] = {
46 { 6, 2, 6, 2, 6, 2, 6, 2, },
47 { 0, 4, 0, 4, 0, 4, 0, 4, },
48 { 6, 2, 6, 2, 6, 2, 6, 2, },
51 DECLARE_ALIGNED(8, const uint8_t, ff_dither_4x4_16)[][8] = {
52 { 8, 4, 11, 7, 8, 4, 11, 7, },
53 { 2, 14, 1, 13, 2, 14, 1, 13, },
54 { 10, 6, 9, 5, 10, 6, 9, 5, },
55 { 0, 12, 3, 15, 0, 12, 3, 15, },
56 { 8, 4, 11, 7, 8, 4, 11, 7, },
59 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_32)[][8] = {
60 { 17, 9, 23, 15, 16, 8, 22, 14, },
61 { 5, 29, 3, 27, 4, 28, 2, 26, },
62 { 21, 13, 19, 11, 20, 12, 18, 10, },
63 { 0, 24, 6, 30, 1, 25, 7, 31, },
64 { 16, 8, 22, 14, 17, 9, 23, 15, },
65 { 4, 28, 2, 26, 5, 29, 3, 27, },
66 { 20, 12, 18, 10, 21, 13, 19, 11, },
67 { 1, 25, 7, 31, 0, 24, 6, 30, },
68 { 17, 9, 23, 15, 16, 8, 22, 14, },
71 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_73)[][8] = {
72 { 0, 55, 14, 68, 3, 58, 17, 72, },
73 { 37, 18, 50, 32, 40, 22, 54, 35, },
74 { 9, 64, 5, 59, 13, 67, 8, 63, },
75 { 46, 27, 41, 23, 49, 31, 44, 26, },
76 { 2, 57, 16, 71, 1, 56, 15, 70, },
77 { 39, 21, 52, 34, 38, 19, 51, 33, },
78 { 11, 66, 7, 62, 10, 65, 6, 60, },
79 { 48, 30, 43, 25, 47, 29, 42, 24, },
80 { 0, 55, 14, 68, 3, 58, 17, 72, },
84 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
85 {117, 62, 158, 103, 113, 58, 155, 100, },
86 { 34, 199, 21, 186, 31, 196, 17, 182, },
87 {144, 89, 131, 76, 141, 86, 127, 72, },
88 { 0, 165, 41, 206, 10, 175, 52, 217, },
89 {110, 55, 151, 96, 120, 65, 162, 107, },
90 { 28, 193, 14, 179, 38, 203, 24, 189, },
91 {138, 83, 124, 69, 148, 93, 134, 79, },
92 { 7, 172, 48, 213, 3, 168, 45, 210, },
93 {117, 62, 158, 103, 113, 58, 155, 100, },
96 // tries to correct a gamma of 1.5
97 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
98 { 0, 143, 18, 200, 2, 156, 25, 215, },
99 { 78, 28, 125, 64, 89, 36, 138, 74, },
100 { 10, 180, 3, 161, 16, 195, 8, 175, },
101 {109, 51, 93, 38, 121, 60, 105, 47, },
102 { 1, 152, 23, 210, 0, 147, 20, 205, },
103 { 85, 33, 134, 71, 81, 30, 130, 67, },
104 { 14, 190, 6, 171, 12, 185, 5, 166, },
105 {117, 57, 101, 44, 113, 54, 97, 41, },
106 { 0, 143, 18, 200, 2, 156, 25, 215, },
109 // tries to correct a gamma of 2.0
110 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
111 { 0, 124, 8, 193, 0, 140, 12, 213, },
112 { 55, 14, 104, 42, 66, 19, 119, 52, },
113 { 3, 168, 1, 145, 6, 187, 3, 162, },
114 { 86, 31, 70, 21, 99, 39, 82, 28, },
115 { 0, 134, 11, 206, 0, 129, 9, 200, },
116 { 62, 17, 114, 48, 58, 16, 109, 45, },
117 { 5, 181, 2, 157, 4, 175, 1, 151, },
118 { 95, 36, 78, 26, 90, 34, 74, 24, },
119 { 0, 124, 8, 193, 0, 140, 12, 213, },
122 // tries to correct a gamma of 2.5
123 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
124 { 0, 107, 3, 187, 0, 125, 6, 212, },
125 { 39, 7, 86, 28, 49, 11, 102, 36, },
126 { 1, 158, 0, 131, 3, 180, 1, 151, },
127 { 68, 19, 52, 12, 81, 25, 64, 17, },
128 { 0, 119, 5, 203, 0, 113, 4, 195, },
129 { 45, 9, 96, 33, 42, 8, 91, 30, },
130 { 2, 172, 1, 144, 2, 165, 0, 137, },
131 { 77, 23, 60, 15, 72, 21, 56, 14, },
132 { 0, 107, 3, 187, 0, 125, 6, 212, },
136 #define output_pixel(pos, val, bias, signedness) \
138 AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
140 AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
143 static av_always_inline void
144 yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
145 int big_endian, int output_bits)
149 av_assert0(output_bits == 16);
151 for (i = 0; i < dstW; i++) {
152 int val = src[i] + (1 << (shift - 1));
153 output_pixel(&dest[i], val, 0, uint);
157 static av_always_inline void
158 yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
159 const int32_t **src, uint16_t *dest, int dstW,
160 int big_endian, int output_bits)
164 av_assert0(output_bits == 16);
166 for (i = 0; i < dstW; i++) {
167 int val = 1 << (shift - 1);
170 /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
171 * filters (or anything with negative coeffs, the range can be slightly
172 * wider in both directions. To account for this overflow, we subtract
173 * a constant so it always fits in the signed range (assuming a
174 * reasonable filterSize), and re-add that at the end. */
176 for (j = 0; j < filterSize; j++)
177 val += src[j][i] * (unsigned)filter[j];
179 output_pixel(&dest[i], val, 0x8000, int);
183 static void yuv2p016cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
184 const int16_t **chrUSrc, const int16_t **chrVSrc,
185 uint8_t *dest8, int chrDstW)
187 uint16_t *dest = (uint16_t*)dest8;
188 const int32_t **uSrc = (const int32_t **)chrUSrc;
189 const int32_t **vSrc = (const int32_t **)chrVSrc;
191 int big_endian = c->dstFormat == AV_PIX_FMT_P016BE;
194 for (i = 0; i < chrDstW; i++) {
195 int u = 1 << (shift - 1);
196 int v = 1 << (shift - 1);
198 /* See yuv2planeX_16_c_template for details. */
201 for (j = 0; j < chrFilterSize; j++) {
202 u += uSrc[j][i] * (unsigned)chrFilter[j];
203 v += vSrc[j][i] * (unsigned)chrFilter[j];
206 output_pixel(&dest[2*i] , u, 0x8000, int);
207 output_pixel(&dest[2*i+1], v, 0x8000, int);
213 #define output_pixel(pos, val) \
215 AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
217 AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
220 static av_always_inline void
221 yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
222 int big_endian, int output_bits)
225 int shift = 15 - output_bits;
227 for (i = 0; i < dstW; i++) {
228 int val = src[i] + (1 << (shift - 1));
229 output_pixel(&dest[i], val);
233 static av_always_inline void
234 yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
235 const int16_t **src, uint16_t *dest, int dstW,
236 int big_endian, int output_bits)
239 int shift = 11 + 16 - output_bits;
241 for (i = 0; i < dstW; i++) {
242 int val = 1 << (shift - 1);
245 for (j = 0; j < filterSize; j++)
246 val += src[j][i] * filter[j];
248 output_pixel(&dest[i], val);
254 #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
255 static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
256 uint8_t *dest, int dstW, \
257 const uint8_t *dither, int offset)\
259 yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
260 (uint16_t *) dest, dstW, is_be, bits); \
262 static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
263 const int16_t **src, uint8_t *dest, int dstW, \
264 const uint8_t *dither, int offset)\
266 yuv2planeX_## template_size ## _c_template(filter, \
267 filterSize, (const typeX_t **) src, \
268 (uint16_t *) dest, dstW, is_be, bits); \
270 yuv2NBPS( 9, BE, 1, 10, int16_t)
271 yuv2NBPS( 9, LE, 0, 10, int16_t)
272 yuv2NBPS(10, BE, 1, 10, int16_t)
273 yuv2NBPS(10, LE, 0, 10, int16_t)
274 yuv2NBPS(12, BE, 1, 10, int16_t)
275 yuv2NBPS(12, LE, 0, 10, int16_t)
276 yuv2NBPS(14, BE, 1, 10, int16_t)
277 yuv2NBPS(14, LE, 0, 10, int16_t)
278 yuv2NBPS(16, BE, 1, 16, int32_t)
279 yuv2NBPS(16, LE, 0, 16, int32_t)
281 static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
282 const int16_t **src, uint8_t *dest, int dstW,
283 const uint8_t *dither, int offset)
286 for (i=0; i<dstW; i++) {
287 int val = dither[(i + offset) & 7] << 12;
289 for (j=0; j<filterSize; j++)
290 val += src[j][i] * filter[j];
292 dest[i]= av_clip_uint8(val>>19);
296 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
297 const uint8_t *dither, int offset)
300 for (i=0; i<dstW; i++) {
301 int val = (src[i] + dither[(i + offset) & 7]) >> 7;
302 dest[i]= av_clip_uint8(val);
306 static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
307 const int16_t **chrUSrc, const int16_t **chrVSrc,
308 uint8_t *dest, int chrDstW)
310 enum AVPixelFormat dstFormat = c->dstFormat;
311 const uint8_t *chrDither = c->chrDither8;
314 if (dstFormat == AV_PIX_FMT_NV12)
315 for (i=0; i<chrDstW; i++) {
316 int u = chrDither[i & 7] << 12;
317 int v = chrDither[(i + 3) & 7] << 12;
319 for (j=0; j<chrFilterSize; j++) {
320 u += chrUSrc[j][i] * chrFilter[j];
321 v += chrVSrc[j][i] * chrFilter[j];
324 dest[2*i]= av_clip_uint8(u>>19);
325 dest[2*i+1]= av_clip_uint8(v>>19);
328 for (i=0; i<chrDstW; i++) {
329 int u = chrDither[i & 7] << 12;
330 int v = chrDither[(i + 3) & 7] << 12;
332 for (j=0; j<chrFilterSize; j++) {
333 u += chrUSrc[j][i] * chrFilter[j];
334 v += chrVSrc[j][i] * chrFilter[j];
337 dest[2*i]= av_clip_uint8(v>>19);
338 dest[2*i+1]= av_clip_uint8(u>>19);
343 #define output_pixel(pos, val) \
345 AV_WB16(pos, av_clip_uintp2(val >> shift, 10) << 6); \
347 AV_WL16(pos, av_clip_uintp2(val >> shift, 10) << 6); \
350 static void yuv2p010l1_c(const int16_t *src,
351 uint16_t *dest, int dstW,
357 for (i = 0; i < dstW; i++) {
358 int val = src[i] + (1 << (shift - 1));
359 output_pixel(&dest[i], val);
363 static void yuv2p010lX_c(const int16_t *filter, int filterSize,
364 const int16_t **src, uint16_t *dest, int dstW,
370 for (i = 0; i < dstW; i++) {
371 int val = 1 << (shift - 1);
373 for (j = 0; j < filterSize; j++)
374 val += src[j][i] * filter[j];
376 output_pixel(&dest[i], val);
380 static void yuv2p010cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
381 const int16_t **chrUSrc, const int16_t **chrVSrc,
382 uint8_t *dest8, int chrDstW)
384 uint16_t *dest = (uint16_t*)dest8;
386 int big_endian = c->dstFormat == AV_PIX_FMT_P010BE;
389 for (i = 0; i < chrDstW; i++) {
390 int u = 1 << (shift - 1);
391 int v = 1 << (shift - 1);
393 for (j = 0; j < chrFilterSize; j++) {
394 u += chrUSrc[j][i] * chrFilter[j];
395 v += chrVSrc[j][i] * chrFilter[j];
398 output_pixel(&dest[2*i] , u);
399 output_pixel(&dest[2*i+1], v);
403 static void yuv2p010l1_LE_c(const int16_t *src,
404 uint8_t *dest, int dstW,
405 const uint8_t *dither, int offset)
407 yuv2p010l1_c(src, (uint16_t*)dest, dstW, 0);
410 static void yuv2p010l1_BE_c(const int16_t *src,
411 uint8_t *dest, int dstW,
412 const uint8_t *dither, int offset)
414 yuv2p010l1_c(src, (uint16_t*)dest, dstW, 1);
417 static void yuv2p010lX_LE_c(const int16_t *filter, int filterSize,
418 const int16_t **src, uint8_t *dest, int dstW,
419 const uint8_t *dither, int offset)
421 yuv2p010lX_c(filter, filterSize, src, (uint16_t*)dest, dstW, 0);
424 static void yuv2p010lX_BE_c(const int16_t *filter, int filterSize,
425 const int16_t **src, uint8_t *dest, int dstW,
426 const uint8_t *dither, int offset)
428 yuv2p010lX_c(filter, filterSize, src, (uint16_t*)dest, dstW, 1);
434 #define accumulate_bit(acc, val) \
437 #define output_pixel(pos, acc) \
438 if (target == AV_PIX_FMT_MONOBLACK) { \
444 static av_always_inline void
445 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
446 const int16_t **lumSrc, int lumFilterSize,
447 const int16_t *chrFilter, const int16_t **chrUSrc,
448 const int16_t **chrVSrc, int chrFilterSize,
449 const int16_t **alpSrc, uint8_t *dest, int dstW,
450 int y, enum AVPixelFormat target)
452 const uint8_t * const d128 = ff_dither_8x8_220[y&7];
457 for (i = 0; i < dstW; i += 2) {
462 for (j = 0; j < lumFilterSize; j++) {
463 Y1 += lumSrc[j][i] * lumFilter[j];
464 Y2 += lumSrc[j][i+1] * lumFilter[j];
468 if ((Y1 | Y2) & 0x100) {
469 Y1 = av_clip_uint8(Y1);
470 Y2 = av_clip_uint8(Y2);
472 if (c->dither == SWS_DITHER_ED) {
473 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;
474 c->dither_error[0][i] = err;
475 acc = 2*acc + (Y1 >= 128);
478 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);
479 c->dither_error[0][i+1] = Y1;
480 acc = 2*acc + (err >= 128);
483 accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
484 accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
487 output_pixel(*dest++, acc);
490 c->dither_error[0][i] = err;
493 output_pixel(*dest, acc);
497 static av_always_inline void
498 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
499 const int16_t *ubuf[2], const int16_t *vbuf[2],
500 const int16_t *abuf[2], uint8_t *dest, int dstW,
501 int yalpha, int uvalpha, int y,
502 enum AVPixelFormat target)
504 const int16_t *buf0 = buf[0], *buf1 = buf[1];
505 const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
506 int yalpha1 = 4096 - yalpha;
508 av_assert2(yalpha <= 4096U);
510 if (c->dither == SWS_DITHER_ED) {
513 for (i = 0; i < dstW; i +=2) {
516 Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
517 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;
518 c->dither_error[0][i] = err;
519 acc = 2*acc + (Y >= 128);
522 err = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
523 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;
524 c->dither_error[0][i+1] = Y;
525 acc = 2*acc + (err >= 128);
529 output_pixel(*dest++, acc);
531 c->dither_error[0][i] = err;
533 for (i = 0; i < dstW; i += 8) {
536 Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
537 accumulate_bit(acc, Y + d128[0]);
538 Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
539 accumulate_bit(acc, Y + d128[1]);
540 Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
541 accumulate_bit(acc, Y + d128[2]);
542 Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
543 accumulate_bit(acc, Y + d128[3]);
544 Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
545 accumulate_bit(acc, Y + d128[4]);
546 Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
547 accumulate_bit(acc, Y + d128[5]);
548 Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
549 accumulate_bit(acc, Y + d128[6]);
550 Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
551 accumulate_bit(acc, Y + d128[7]);
553 output_pixel(*dest++, acc);
558 static av_always_inline void
559 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
560 const int16_t *ubuf[2], const int16_t *vbuf[2],
561 const int16_t *abuf0, uint8_t *dest, int dstW,
562 int uvalpha, int y, enum AVPixelFormat target)
564 const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
567 if (c->dither == SWS_DITHER_ED) {
570 for (i = 0; i < dstW; i +=2) {
573 Y = ((buf0[i + 0] + 64) >> 7);
574 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;
575 c->dither_error[0][i] = err;
576 acc = 2*acc + (Y >= 128);
579 err = ((buf0[i + 1] + 64) >> 7);
580 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;
581 c->dither_error[0][i+1] = Y;
582 acc = 2*acc + (err >= 128);
586 output_pixel(*dest++, acc);
588 c->dither_error[0][i] = err;
590 for (i = 0; i < dstW; i += 8) {
592 accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
593 accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
594 accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
595 accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
596 accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
597 accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
598 accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
599 accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
601 output_pixel(*dest++, acc);
607 #undef accumulate_bit
609 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
610 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
611 const int16_t **lumSrc, int lumFilterSize, \
612 const int16_t *chrFilter, const int16_t **chrUSrc, \
613 const int16_t **chrVSrc, int chrFilterSize, \
614 const int16_t **alpSrc, uint8_t *dest, int dstW, \
617 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
618 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
619 alpSrc, dest, dstW, y, fmt); \
622 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
623 const int16_t *ubuf[2], const int16_t *vbuf[2], \
624 const int16_t *abuf[2], uint8_t *dest, int dstW, \
625 int yalpha, int uvalpha, int y) \
627 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
628 dest, dstW, yalpha, uvalpha, y, fmt); \
631 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
632 const int16_t *ubuf[2], const int16_t *vbuf[2], \
633 const int16_t *abuf0, uint8_t *dest, int dstW, \
634 int uvalpha, int y) \
636 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
637 abuf0, dest, dstW, uvalpha, \
641 YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
642 YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
644 #define output_pixels(pos, Y1, U, Y2, V) \
645 if (target == AV_PIX_FMT_YUYV422) { \
646 dest[pos + 0] = Y1; \
648 dest[pos + 2] = Y2; \
650 } else if (target == AV_PIX_FMT_YVYU422) { \
651 dest[pos + 0] = Y1; \
653 dest[pos + 2] = Y2; \
655 } else { /* AV_PIX_FMT_UYVY422 */ \
657 dest[pos + 1] = Y1; \
659 dest[pos + 3] = Y2; \
662 static av_always_inline void
663 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
664 const int16_t **lumSrc, int lumFilterSize,
665 const int16_t *chrFilter, const int16_t **chrUSrc,
666 const int16_t **chrVSrc, int chrFilterSize,
667 const int16_t **alpSrc, uint8_t *dest, int dstW,
668 int y, enum AVPixelFormat target)
672 for (i = 0; i < ((dstW + 1) >> 1); i++) {
679 for (j = 0; j < lumFilterSize; j++) {
680 Y1 += lumSrc[j][i * 2] * lumFilter[j];
681 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
683 for (j = 0; j < chrFilterSize; j++) {
684 U += chrUSrc[j][i] * chrFilter[j];
685 V += chrVSrc[j][i] * chrFilter[j];
691 if ((Y1 | Y2 | U | V) & 0x100) {
692 Y1 = av_clip_uint8(Y1);
693 Y2 = av_clip_uint8(Y2);
694 U = av_clip_uint8(U);
695 V = av_clip_uint8(V);
697 output_pixels(4*i, Y1, U, Y2, V);
701 static av_always_inline void
702 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
703 const int16_t *ubuf[2], const int16_t *vbuf[2],
704 const int16_t *abuf[2], uint8_t *dest, int dstW,
705 int yalpha, int uvalpha, int y,
706 enum AVPixelFormat target)
708 const int16_t *buf0 = buf[0], *buf1 = buf[1],
709 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
710 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
711 int yalpha1 = 4096 - yalpha;
712 int uvalpha1 = 4096 - uvalpha;
714 av_assert2(yalpha <= 4096U);
715 av_assert2(uvalpha <= 4096U);
717 for (i = 0; i < ((dstW + 1) >> 1); i++) {
718 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
719 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
720 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
721 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
723 if ((Y1 | Y2 | U | V) & 0x100) {
724 Y1 = av_clip_uint8(Y1);
725 Y2 = av_clip_uint8(Y2);
726 U = av_clip_uint8(U);
727 V = av_clip_uint8(V);
730 output_pixels(i * 4, Y1, U, Y2, V);
734 static av_always_inline void
735 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
736 const int16_t *ubuf[2], const int16_t *vbuf[2],
737 const int16_t *abuf0, uint8_t *dest, int dstW,
738 int uvalpha, int y, enum AVPixelFormat target)
740 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
743 if (uvalpha < 2048) {
744 for (i = 0; i < ((dstW + 1) >> 1); i++) {
745 int Y1 = (buf0[i * 2 ]+64) >> 7;
746 int Y2 = (buf0[i * 2 + 1]+64) >> 7;
747 int U = (ubuf0[i] +64) >> 7;
748 int V = (vbuf0[i] +64) >> 7;
750 if ((Y1 | Y2 | U | V) & 0x100) {
751 Y1 = av_clip_uint8(Y1);
752 Y2 = av_clip_uint8(Y2);
753 U = av_clip_uint8(U);
754 V = av_clip_uint8(V);
757 Y1 = av_clip_uint8(Y1);
758 Y2 = av_clip_uint8(Y2);
759 U = av_clip_uint8(U);
760 V = av_clip_uint8(V);
762 output_pixels(i * 4, Y1, U, Y2, V);
765 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
766 for (i = 0; i < ((dstW + 1) >> 1); i++) {
767 int Y1 = (buf0[i * 2 ] + 64) >> 7;
768 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
769 int U = (ubuf0[i] + ubuf1[i]+128) >> 8;
770 int V = (vbuf0[i] + vbuf1[i]+128) >> 8;
772 if ((Y1 | Y2 | U | V) & 0x100) {
773 Y1 = av_clip_uint8(Y1);
774 Y2 = av_clip_uint8(Y2);
775 U = av_clip_uint8(U);
776 V = av_clip_uint8(V);
779 Y1 = av_clip_uint8(Y1);
780 Y2 = av_clip_uint8(Y2);
781 U = av_clip_uint8(U);
782 V = av_clip_uint8(V);
784 output_pixels(i * 4, Y1, U, Y2, V);
791 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
792 YUV2PACKEDWRAPPER(yuv2, 422, yvyu422, AV_PIX_FMT_YVYU422)
793 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
795 #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE || target == AV_PIX_FMT_RGBA64LE || target == AV_PIX_FMT_RGBA64BE) ? R : B)
796 #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE || target == AV_PIX_FMT_RGBA64LE || target == AV_PIX_FMT_RGBA64BE) ? B : R)
797 #define output_pixel(pos, val) \
798 if (isBE(target)) { \
804 static av_always_inline void
805 yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter,
806 const int32_t **lumSrc, int lumFilterSize,
807 const int16_t *chrFilter, const int32_t **chrUSrc,
808 const int32_t **chrVSrc, int chrFilterSize,
809 const int32_t **alpSrc, uint16_t *dest, int dstW,
810 int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
813 int A1 = 0xffff<<14, A2 = 0xffff<<14;
815 for (i = 0; i < ((dstW + 1) >> 1); i++) {
817 int Y1 = -0x40000000;
818 int Y2 = -0x40000000;
819 int U = -(128 << 23); // 19
820 int V = -(128 << 23);
823 for (j = 0; j < lumFilterSize; j++) {
824 Y1 += lumSrc[j][i * 2] * (unsigned)lumFilter[j];
825 Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
827 for (j = 0; j < chrFilterSize; j++) {;
828 U += chrUSrc[j][i] * (unsigned)chrFilter[j];
829 V += chrVSrc[j][i] * (unsigned)chrFilter[j];
835 for (j = 0; j < lumFilterSize; j++) {
836 A1 += alpSrc[j][i * 2] * (unsigned)lumFilter[j];
837 A2 += alpSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
845 // 8 bits: 12+15=27; 16 bits: 12+19=31
853 // 8 bits: 27 -> 17 bits, 16 bits: 31 - 14 = 17 bits
854 Y1 -= c->yuv2rgb_y_offset;
855 Y2 -= c->yuv2rgb_y_offset;
856 Y1 *= c->yuv2rgb_y_coeff;
857 Y2 *= c->yuv2rgb_y_coeff;
860 // 8 bits: 17 + 13 bits = 30 bits, 16 bits: 17 + 13 bits = 30 bits
862 R = V * c->yuv2rgb_v2r_coeff;
863 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
864 B = U * c->yuv2rgb_u2b_coeff;
866 // 8 bits: 30 - 22 = 8 bits, 16 bits: 30 bits - 14 = 16 bits
867 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
868 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
869 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
871 output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
872 output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
873 output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
874 output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
875 output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
878 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
879 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
880 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
886 static av_always_inline void
887 yuv2rgba64_2_c_template(SwsContext *c, const int32_t *buf[2],
888 const int32_t *ubuf[2], const int32_t *vbuf[2],
889 const int32_t *abuf[2], uint16_t *dest, int dstW,
890 int yalpha, int uvalpha, int y,
891 enum AVPixelFormat target, int hasAlpha, int eightbytes)
893 const int32_t *buf0 = buf[0], *buf1 = buf[1],
894 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
895 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
896 *abuf0 = hasAlpha ? abuf[0] : NULL,
897 *abuf1 = hasAlpha ? abuf[1] : NULL;
898 int yalpha1 = 4096 - yalpha;
899 int uvalpha1 = 4096 - uvalpha;
901 int A1 = 0xffff<<14, A2 = 0xffff<<14;
903 av_assert2(yalpha <= 4096U);
904 av_assert2(uvalpha <= 4096U);
906 for (i = 0; i < ((dstW + 1) >> 1); i++) {
907 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
908 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
909 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha - (128 << 23)) >> 14;
910 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha - (128 << 23)) >> 14;
913 Y1 -= c->yuv2rgb_y_offset;
914 Y2 -= c->yuv2rgb_y_offset;
915 Y1 *= c->yuv2rgb_y_coeff;
916 Y2 *= c->yuv2rgb_y_coeff;
920 R = V * c->yuv2rgb_v2r_coeff;
921 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
922 B = U * c->yuv2rgb_u2b_coeff;
925 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 1;
926 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 1;
932 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
933 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
934 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
936 output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
937 output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
938 output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
939 output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
940 output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
943 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
944 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
945 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
951 static av_always_inline void
952 yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0,
953 const int32_t *ubuf[2], const int32_t *vbuf[2],
954 const int32_t *abuf0, uint16_t *dest, int dstW,
955 int uvalpha, int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
957 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
959 int A1 = 0xffff<<14, A2= 0xffff<<14;
961 if (uvalpha < 2048) {
962 for (i = 0; i < ((dstW + 1) >> 1); i++) {
963 int Y1 = (buf0[i * 2] ) >> 2;
964 int Y2 = (buf0[i * 2 + 1]) >> 2;
965 int U = (ubuf0[i] - (128 << 11)) >> 2;
966 int V = (vbuf0[i] - (128 << 11)) >> 2;
969 Y1 -= c->yuv2rgb_y_offset;
970 Y2 -= c->yuv2rgb_y_offset;
971 Y1 *= c->yuv2rgb_y_coeff;
972 Y2 *= c->yuv2rgb_y_coeff;
977 A1 = abuf0[i * 2 ] << 11;
978 A2 = abuf0[i * 2 + 1] << 11;
984 R = V * c->yuv2rgb_v2r_coeff;
985 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
986 B = U * c->yuv2rgb_u2b_coeff;
988 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
989 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
990 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
992 output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
993 output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
994 output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
995 output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
996 output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
999 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
1000 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
1001 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
1006 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1007 int A1 = 0xffff<<14, A2 = 0xffff<<14;
1008 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1009 int Y1 = (buf0[i * 2] ) >> 2;
1010 int Y2 = (buf0[i * 2 + 1]) >> 2;
1011 int U = (ubuf0[i] + ubuf1[i] - (128 << 12)) >> 3;
1012 int V = (vbuf0[i] + vbuf1[i] - (128 << 12)) >> 3;
1015 Y1 -= c->yuv2rgb_y_offset;
1016 Y2 -= c->yuv2rgb_y_offset;
1017 Y1 *= c->yuv2rgb_y_coeff;
1018 Y2 *= c->yuv2rgb_y_coeff;
1023 A1 = abuf0[i * 2 ] << 11;
1024 A2 = abuf0[i * 2 + 1] << 11;
1030 R = V * c->yuv2rgb_v2r_coeff;
1031 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1032 B = U * c->yuv2rgb_u2b_coeff;
1034 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
1035 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
1036 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
1038 output_pixel(&dest[3], av_clip_uintp2(A1 , 30) >> 14);
1039 output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
1040 output_pixel(&dest[5], av_clip_uintp2( G + Y2, 30) >> 14);
1041 output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
1042 output_pixel(&dest[7], av_clip_uintp2(A2 , 30) >> 14);
1045 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
1046 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
1047 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
1054 static av_always_inline void
1055 yuv2rgba64_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1056 const int32_t **lumSrc, int lumFilterSize,
1057 const int16_t *chrFilter, const int32_t **chrUSrc,
1058 const int32_t **chrVSrc, int chrFilterSize,
1059 const int32_t **alpSrc, uint16_t *dest, int dstW,
1060 int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
1065 for (i = 0; i < dstW; i++) {
1067 int Y = -0x40000000;
1068 int U = -(128 << 23); // 19
1069 int V = -(128 << 23);
1072 for (j = 0; j < lumFilterSize; j++) {
1073 Y += lumSrc[j][i] * (unsigned)lumFilter[j];
1075 for (j = 0; j < chrFilterSize; j++) {;
1076 U += chrUSrc[j][i] * (unsigned)chrFilter[j];
1077 V += chrVSrc[j][i] * (unsigned)chrFilter[j];
1082 for (j = 0; j < lumFilterSize; j++) {
1083 A += alpSrc[j][i] * (unsigned)lumFilter[j];
1089 // 8bit: 12+15=27; 16-bit: 12+19=31
1095 // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
1096 Y -= c->yuv2rgb_y_offset;
1097 Y *= c->yuv2rgb_y_coeff;
1099 // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
1101 R = V * c->yuv2rgb_v2r_coeff;
1102 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1103 B = U * c->yuv2rgb_u2b_coeff;
1105 // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
1106 output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1107 output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14);
1108 output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1110 output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1118 static av_always_inline void
1119 yuv2rgba64_full_2_c_template(SwsContext *c, const int32_t *buf[2],
1120 const int32_t *ubuf[2], const int32_t *vbuf[2],
1121 const int32_t *abuf[2], uint16_t *dest, int dstW,
1122 int yalpha, int uvalpha, int y,
1123 enum AVPixelFormat target, int hasAlpha, int eightbytes)
1125 const int32_t *buf0 = buf[0], *buf1 = buf[1],
1126 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1127 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1128 *abuf0 = hasAlpha ? abuf[0] : NULL,
1129 *abuf1 = hasAlpha ? abuf[1] : NULL;
1130 int yalpha1 = 4096 - yalpha;
1131 int uvalpha1 = 4096 - uvalpha;
1135 av_assert2(yalpha <= 4096U);
1136 av_assert2(uvalpha <= 4096U);
1138 for (i = 0; i < dstW; i++) {
1139 int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 14;
1140 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha - (128 << 23)) >> 14;
1141 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha - (128 << 23)) >> 14;
1144 Y -= c->yuv2rgb_y_offset;
1145 Y *= c->yuv2rgb_y_coeff;
1148 R = V * c->yuv2rgb_v2r_coeff;
1149 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1150 B = U * c->yuv2rgb_u2b_coeff;
1153 A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 1;
1158 output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1159 output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14);
1160 output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1162 output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1170 static av_always_inline void
1171 yuv2rgba64_full_1_c_template(SwsContext *c, const int32_t *buf0,
1172 const int32_t *ubuf[2], const int32_t *vbuf[2],
1173 const int32_t *abuf0, uint16_t *dest, int dstW,
1174 int uvalpha, int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
1176 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1180 if (uvalpha < 2048) {
1181 for (i = 0; i < dstW; i++) {
1182 int Y = (buf0[i]) >> 2;
1183 int U = (ubuf0[i] - (128 << 11)) >> 2;
1184 int V = (vbuf0[i] - (128 << 11)) >> 2;
1187 Y -= c->yuv2rgb_y_offset;
1188 Y *= c->yuv2rgb_y_coeff;
1197 R = V * c->yuv2rgb_v2r_coeff;
1198 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1199 B = U * c->yuv2rgb_u2b_coeff;
1201 output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1202 output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14);
1203 output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1205 output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1212 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1214 for (i = 0; i < dstW; i++) {
1215 int Y = (buf0[i] ) >> 2;
1216 int U = (ubuf0[i] + ubuf1[i] - (128 << 12)) >> 3;
1217 int V = (vbuf0[i] + vbuf1[i] - (128 << 12)) >> 3;
1220 Y -= c->yuv2rgb_y_offset;
1221 Y *= c->yuv2rgb_y_coeff;
1230 R = V * c->yuv2rgb_v2r_coeff;
1231 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1232 B = U * c->yuv2rgb_u2b_coeff;
1234 output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1235 output_pixel(&dest[1], av_clip_uintp2( G + Y, 30) >> 14);
1236 output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1238 output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1251 #define YUV2PACKED16WRAPPER(name, base, ext, fmt, hasAlpha, eightbytes) \
1252 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1253 const int16_t **_lumSrc, int lumFilterSize, \
1254 const int16_t *chrFilter, const int16_t **_chrUSrc, \
1255 const int16_t **_chrVSrc, int chrFilterSize, \
1256 const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
1259 const int32_t **lumSrc = (const int32_t **) _lumSrc, \
1260 **chrUSrc = (const int32_t **) _chrUSrc, \
1261 **chrVSrc = (const int32_t **) _chrVSrc, \
1262 **alpSrc = (const int32_t **) _alpSrc; \
1263 uint16_t *dest = (uint16_t *) _dest; \
1264 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1265 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1266 alpSrc, dest, dstW, y, fmt, hasAlpha, eightbytes); \
1269 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
1270 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
1271 const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
1272 int yalpha, int uvalpha, int y) \
1274 const int32_t **buf = (const int32_t **) _buf, \
1275 **ubuf = (const int32_t **) _ubuf, \
1276 **vbuf = (const int32_t **) _vbuf, \
1277 **abuf = (const int32_t **) _abuf; \
1278 uint16_t *dest = (uint16_t *) _dest; \
1279 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1280 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha, eightbytes); \
1283 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
1284 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
1285 const int16_t *_abuf0, uint8_t *_dest, int dstW, \
1286 int uvalpha, int y) \
1288 const int32_t *buf0 = (const int32_t *) _buf0, \
1289 **ubuf = (const int32_t **) _ubuf, \
1290 **vbuf = (const int32_t **) _vbuf, \
1291 *abuf0 = (const int32_t *) _abuf0; \
1292 uint16_t *dest = (uint16_t *) _dest; \
1293 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1294 dstW, uvalpha, y, fmt, hasAlpha, eightbytes); \
1297 YUV2PACKED16WRAPPER(yuv2, rgba64, rgb48be, AV_PIX_FMT_RGB48BE, 0, 0)
1298 YUV2PACKED16WRAPPER(yuv2, rgba64, rgb48le, AV_PIX_FMT_RGB48LE, 0, 0)
1299 YUV2PACKED16WRAPPER(yuv2, rgba64, bgr48be, AV_PIX_FMT_BGR48BE, 0, 0)
1300 YUV2PACKED16WRAPPER(yuv2, rgba64, bgr48le, AV_PIX_FMT_BGR48LE, 0, 0)
1301 YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64be, AV_PIX_FMT_RGBA64BE, 1, 1)
1302 YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64le, AV_PIX_FMT_RGBA64LE, 1, 1)
1303 YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64be, AV_PIX_FMT_RGBA64BE, 0, 1)
1304 YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64le, AV_PIX_FMT_RGBA64LE, 0, 1)
1305 YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64be, AV_PIX_FMT_BGRA64BE, 1, 1)
1306 YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64le, AV_PIX_FMT_BGRA64LE, 1, 1)
1307 YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64be, AV_PIX_FMT_BGRA64BE, 0, 1)
1308 YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64le, AV_PIX_FMT_BGRA64LE, 0, 1)
1310 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgb48be_full, AV_PIX_FMT_RGB48BE, 0, 0)
1311 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgb48le_full, AV_PIX_FMT_RGB48LE, 0, 0)
1312 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgr48be_full, AV_PIX_FMT_BGR48BE, 0, 0)
1313 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgr48le_full, AV_PIX_FMT_BGR48LE, 0, 0)
1314 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgba64be_full, AV_PIX_FMT_RGBA64BE, 1, 1)
1315 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgba64le_full, AV_PIX_FMT_RGBA64LE, 1, 1)
1316 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbx64be_full, AV_PIX_FMT_RGBA64BE, 0, 1)
1317 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbx64le_full, AV_PIX_FMT_RGBA64LE, 0, 1)
1318 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgra64be_full, AV_PIX_FMT_BGRA64BE, 1, 1)
1319 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgra64le_full, AV_PIX_FMT_BGRA64LE, 1, 1)
1320 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgrx64be_full, AV_PIX_FMT_BGRA64BE, 0, 1)
1321 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgrx64le_full, AV_PIX_FMT_BGRA64LE, 0, 1)
1324 * Write out 2 RGB pixels in the target pixel format. This function takes a
1325 * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
1326 * things like endianness conversion and shifting. The caller takes care of
1327 * setting the correct offset in these tables from the chroma (U/V) values.
1328 * This function then uses the luminance (Y1/Y2) values to write out the
1329 * correct RGB values into the destination buffer.
1331 static av_always_inline void
1332 yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
1333 unsigned A1, unsigned A2,
1334 const void *_r, const void *_g, const void *_b, int y,
1335 enum AVPixelFormat target, int hasAlpha)
1337 if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
1338 target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
1339 uint32_t *dest = (uint32_t *) _dest;
1340 const uint32_t *r = (const uint32_t *) _r;
1341 const uint32_t *g = (const uint32_t *) _g;
1342 const uint32_t *b = (const uint32_t *) _b;
1345 int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
1347 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
1348 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
1351 int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
1353 av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0);
1354 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
1355 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
1357 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
1358 int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
1360 av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0xFF);
1362 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
1363 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
1366 } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
1367 uint8_t *dest = (uint8_t *) _dest;
1368 const uint8_t *r = (const uint8_t *) _r;
1369 const uint8_t *g = (const uint8_t *) _g;
1370 const uint8_t *b = (const uint8_t *) _b;
1372 #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
1373 #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
1375 dest[i * 6 + 0] = r_b[Y1];
1376 dest[i * 6 + 1] = g[Y1];
1377 dest[i * 6 + 2] = b_r[Y1];
1378 dest[i * 6 + 3] = r_b[Y2];
1379 dest[i * 6 + 4] = g[Y2];
1380 dest[i * 6 + 5] = b_r[Y2];
1383 } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
1384 target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
1385 target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
1386 uint16_t *dest = (uint16_t *) _dest;
1387 const uint16_t *r = (const uint16_t *) _r;
1388 const uint16_t *g = (const uint16_t *) _g;
1389 const uint16_t *b = (const uint16_t *) _b;
1390 int dr1, dg1, db1, dr2, dg2, db2;
1392 if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
1393 dr1 = ff_dither_2x2_8[ y & 1 ][0];
1394 dg1 = ff_dither_2x2_4[ y & 1 ][0];
1395 db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
1396 dr2 = ff_dither_2x2_8[ y & 1 ][1];
1397 dg2 = ff_dither_2x2_4[ y & 1 ][1];
1398 db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
1399 } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
1400 dr1 = ff_dither_2x2_8[ y & 1 ][0];
1401 dg1 = ff_dither_2x2_8[ y & 1 ][1];
1402 db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
1403 dr2 = ff_dither_2x2_8[ y & 1 ][1];
1404 dg2 = ff_dither_2x2_8[ y & 1 ][0];
1405 db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
1407 dr1 = ff_dither_4x4_16[ y & 3 ][0];
1408 dg1 = ff_dither_4x4_16[ y & 3 ][1];
1409 db1 = ff_dither_4x4_16[(y & 3) ^ 3][0];
1410 dr2 = ff_dither_4x4_16[ y & 3 ][1];
1411 dg2 = ff_dither_4x4_16[ y & 3 ][0];
1412 db2 = ff_dither_4x4_16[(y & 3) ^ 3][1];
1415 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
1416 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
1417 } else /* 8/4 bits */ {
1418 uint8_t *dest = (uint8_t *) _dest;
1419 const uint8_t *r = (const uint8_t *) _r;
1420 const uint8_t *g = (const uint8_t *) _g;
1421 const uint8_t *b = (const uint8_t *) _b;
1422 int dr1, dg1, db1, dr2, dg2, db2;
1424 if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
1425 const uint8_t * const d64 = ff_dither_8x8_73[y & 7];
1426 const uint8_t * const d32 = ff_dither_8x8_32[y & 7];
1427 dr1 = dg1 = d32[(i * 2 + 0) & 7];
1428 db1 = d64[(i * 2 + 0) & 7];
1429 dr2 = dg2 = d32[(i * 2 + 1) & 7];
1430 db2 = d64[(i * 2 + 1) & 7];
1432 const uint8_t * const d64 = ff_dither_8x8_73 [y & 7];
1433 const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
1434 dr1 = db1 = d128[(i * 2 + 0) & 7];
1435 dg1 = d64[(i * 2 + 0) & 7];
1436 dr2 = db2 = d128[(i * 2 + 1) & 7];
1437 dg2 = d64[(i * 2 + 1) & 7];
1440 if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
1441 dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
1442 ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
1444 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
1445 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
1450 static av_always_inline void
1451 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
1452 const int16_t **lumSrc, int lumFilterSize,
1453 const int16_t *chrFilter, const int16_t **chrUSrc,
1454 const int16_t **chrVSrc, int chrFilterSize,
1455 const int16_t **alpSrc, uint8_t *dest, int dstW,
1456 int y, enum AVPixelFormat target, int hasAlpha)
1460 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1466 const void *r, *g, *b;
1468 for (j = 0; j < lumFilterSize; j++) {
1469 Y1 += lumSrc[j][i * 2] * lumFilter[j];
1470 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
1472 for (j = 0; j < chrFilterSize; j++) {
1473 U += chrUSrc[j][i] * chrFilter[j];
1474 V += chrVSrc[j][i] * chrFilter[j];
1483 for (j = 0; j < lumFilterSize; j++) {
1484 A1 += alpSrc[j][i * 2 ] * lumFilter[j];
1485 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
1489 if ((A1 | A2) & 0x100) {
1490 A1 = av_clip_uint8(A1);
1491 A2 = av_clip_uint8(A2);
1495 r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
1496 g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
1497 b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1499 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1500 r, g, b, y, target, hasAlpha);
1504 static av_always_inline void
1505 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
1506 const int16_t *ubuf[2], const int16_t *vbuf[2],
1507 const int16_t *abuf[2], uint8_t *dest, int dstW,
1508 int yalpha, int uvalpha, int y,
1509 enum AVPixelFormat target, int hasAlpha)
1511 const int16_t *buf0 = buf[0], *buf1 = buf[1],
1512 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1513 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1514 *abuf0 = hasAlpha ? abuf[0] : NULL,
1515 *abuf1 = hasAlpha ? abuf[1] : NULL;
1516 int yalpha1 = 4096 - yalpha;
1517 int uvalpha1 = 4096 - uvalpha;
1519 av_assert2(yalpha <= 4096U);
1520 av_assert2(uvalpha <= 4096U);
1522 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1523 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
1524 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
1525 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
1526 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
1528 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1529 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1530 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1533 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
1534 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1535 A1 = av_clip_uint8(A1);
1536 A2 = av_clip_uint8(A2);
1539 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1540 r, g, b, y, target, hasAlpha);
1544 static av_always_inline void
1545 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1546 const int16_t *ubuf[2], const int16_t *vbuf[2],
1547 const int16_t *abuf0, uint8_t *dest, int dstW,
1548 int uvalpha, int y, enum AVPixelFormat target,
1551 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1554 if (uvalpha < 2048) {
1555 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1556 int Y1 = (buf0[i * 2 ] + 64) >> 7;
1557 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1558 int U = (ubuf0[i] + 64) >> 7;
1559 int V = (vbuf0[i] + 64) >> 7;
1561 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1562 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1563 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1566 A1 = abuf0[i * 2 ] * 255 + 16384 >> 15;
1567 A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
1568 A1 = av_clip_uint8(A1);
1569 A2 = av_clip_uint8(A2);
1572 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1573 r, g, b, y, target, hasAlpha);
1576 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1577 for (i = 0; i < ((dstW + 1) >> 1); i++) {
1578 int Y1 = (buf0[i * 2 ] + 64) >> 7;
1579 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1580 int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
1581 int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
1583 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1584 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1585 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1588 A1 = (abuf0[i * 2 ] + 64) >> 7;
1589 A2 = (abuf0[i * 2 + 1] + 64) >> 7;
1590 A1 = av_clip_uint8(A1);
1591 A2 = av_clip_uint8(A2);
1594 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1595 r, g, b, y, target, hasAlpha);
1600 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1601 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1602 const int16_t **lumSrc, int lumFilterSize, \
1603 const int16_t *chrFilter, const int16_t **chrUSrc, \
1604 const int16_t **chrVSrc, int chrFilterSize, \
1605 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1608 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1609 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1610 alpSrc, dest, dstW, y, fmt, hasAlpha); \
1613 #define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
1614 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1615 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1616 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1617 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1618 int yalpha, int uvalpha, int y) \
1620 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1621 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1624 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1625 YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
1626 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1627 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1628 const int16_t *abuf0, uint8_t *dest, int dstW, \
1629 int uvalpha, int y) \
1631 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1632 dstW, uvalpha, y, fmt, hasAlpha); \
1636 YUV2RGBWRAPPER(yuv2rgb,, 32_1, AV_PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->needAlpha)
1637 YUV2RGBWRAPPER(yuv2rgb,, 32, AV_PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->needAlpha)
1639 #if CONFIG_SWSCALE_ALPHA
1640 YUV2RGBWRAPPER(yuv2rgb,, a32_1, AV_PIX_FMT_RGB32_1, 1)
1641 YUV2RGBWRAPPER(yuv2rgb,, a32, AV_PIX_FMT_RGB32, 1)
1643 YUV2RGBWRAPPER(yuv2rgb,, x32_1, AV_PIX_FMT_RGB32_1, 0)
1644 YUV2RGBWRAPPER(yuv2rgb,, x32, AV_PIX_FMT_RGB32, 0)
1646 YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24, 0)
1647 YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24, 0)
1648 YUV2RGBWRAPPER(yuv2rgb,, 16, AV_PIX_FMT_RGB565, 0)
1649 YUV2RGBWRAPPER(yuv2rgb,, 15, AV_PIX_FMT_RGB555, 0)
1650 YUV2RGBWRAPPER(yuv2rgb,, 12, AV_PIX_FMT_RGB444, 0)
1651 YUV2RGBWRAPPER(yuv2rgb,, 8, AV_PIX_FMT_RGB8, 0)
1652 YUV2RGBWRAPPER(yuv2rgb,, 4, AV_PIX_FMT_RGB4, 0)
1653 YUV2RGBWRAPPER(yuv2rgb,, 4b, AV_PIX_FMT_RGB4_BYTE, 0)
1655 static av_always_inline void yuv2rgb_write_full(SwsContext *c,
1656 uint8_t *dest, int i, int Y, int A, int U, int V,
1657 int y, enum AVPixelFormat target, int hasAlpha, int err[4])
1660 int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
1662 Y -= c->yuv2rgb_y_offset;
1663 Y *= c->yuv2rgb_y_coeff;
1665 R = Y + V*c->yuv2rgb_v2r_coeff;
1666 G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1667 B = Y + U*c->yuv2rgb_u2b_coeff;
1668 if ((R | G | B) & 0xC0000000) {
1669 R = av_clip_uintp2(R, 30);
1670 G = av_clip_uintp2(G, 30);
1671 B = av_clip_uintp2(B, 30);
1675 case AV_PIX_FMT_ARGB:
1676 dest[0] = hasAlpha ? A : 255;
1681 case AV_PIX_FMT_RGB24:
1686 case AV_PIX_FMT_RGBA:
1690 dest[3] = hasAlpha ? A : 255;
1692 case AV_PIX_FMT_ABGR:
1693 dest[0] = hasAlpha ? A : 255;
1698 case AV_PIX_FMT_BGR24:
1703 case AV_PIX_FMT_BGRA:
1707 dest[3] = hasAlpha ? A : 255;
1709 case AV_PIX_FMT_BGR4_BYTE:
1710 case AV_PIX_FMT_RGB4_BYTE:
1711 case AV_PIX_FMT_BGR8:
1712 case AV_PIX_FMT_RGB8:
1716 switch (c->dither) {
1718 case SWS_DITHER_AUTO:
1723 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;
1724 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;
1725 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;
1726 c->dither_error[0][i] = err[0];
1727 c->dither_error[1][i] = err[1];
1728 c->dither_error[2][i] = err[2];
1729 r = R >> (isrgb8 ? 5 : 7);
1730 g = G >> (isrgb8 ? 5 : 6);
1731 b = B >> (isrgb8 ? 6 : 7);
1732 r = av_clip(r, 0, isrgb8 ? 7 : 1);
1733 g = av_clip(g, 0, isrgb8 ? 7 : 3);
1734 b = av_clip(b, 0, isrgb8 ? 3 : 1);
1735 err[0] = R - r*(isrgb8 ? 36 : 255);
1736 err[1] = G - g*(isrgb8 ? 36 : 85);
1737 err[2] = B - b*(isrgb8 ? 85 : 255);
1739 case SWS_DITHER_A_DITHER:
1741 /* see http://pippin.gimp.org/a_dither/ for details/origin */
1742 #define A_DITHER(u,v) (((((u)+((v)*236))*119)&0xff))
1743 r = (((R >> 19) + A_DITHER(i,y) -96)>>8);
1744 g = (((G >> 19) + A_DITHER(i + 17,y) - 96)>>8);
1745 b = (((B >> 20) + A_DITHER(i + 17*2,y) -96)>>8);
1746 r = av_clip_uintp2(r, 3);
1747 g = av_clip_uintp2(g, 3);
1748 b = av_clip_uintp2(b, 2);
1750 r = (((R >> 21) + A_DITHER(i,y)-256)>>8);
1751 g = (((G >> 19) + A_DITHER(i + 17,y)-256)>>8);
1752 b = (((B >> 21) + A_DITHER(i + 17*2,y)-256)>>8);
1753 r = av_clip_uintp2(r, 1);
1754 g = av_clip_uintp2(g, 2);
1755 b = av_clip_uintp2(b, 1);
1758 case SWS_DITHER_X_DITHER:
1760 /* see http://pippin.gimp.org/a_dither/ for details/origin */
1761 #define X_DITHER(u,v) (((((u)^((v)*237))*181)&0x1ff)/2)
1762 r = (((R >> 19) + X_DITHER(i,y) - 96)>>8);
1763 g = (((G >> 19) + X_DITHER(i + 17,y) - 96)>>8);
1764 b = (((B >> 20) + X_DITHER(i + 17*2,y) - 96)>>8);
1765 r = av_clip_uintp2(r, 3);
1766 g = av_clip_uintp2(g, 3);
1767 b = av_clip_uintp2(b, 2);
1769 r = (((R >> 21) + X_DITHER(i,y)-256)>>8);
1770 g = (((G >> 19) + X_DITHER(i + 17,y)-256)>>8);
1771 b = (((B >> 21) + X_DITHER(i + 17*2,y)-256)>>8);
1772 r = av_clip_uintp2(r, 1);
1773 g = av_clip_uintp2(g, 2);
1774 b = av_clip_uintp2(b, 1);
1780 if(target == AV_PIX_FMT_BGR4_BYTE) {
1781 dest[0] = r + 2*g + 8*b;
1782 } else if(target == AV_PIX_FMT_RGB4_BYTE) {
1783 dest[0] = b + 2*g + 8*r;
1784 } else if(target == AV_PIX_FMT_BGR8) {
1785 dest[0] = r + 8*g + 64*b;
1786 } else if(target == AV_PIX_FMT_RGB8) {
1787 dest[0] = b + 4*g + 32*r;
1794 static av_always_inline void
1795 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1796 const int16_t **lumSrc, int lumFilterSize,
1797 const int16_t *chrFilter, const int16_t **chrUSrc,
1798 const int16_t **chrVSrc, int chrFilterSize,
1799 const int16_t **alpSrc, uint8_t *dest,
1800 int dstW, int y, enum AVPixelFormat target, int hasAlpha)
1803 int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1805 int A = 0; //init to silence warning
1807 if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1808 || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
1811 for (i = 0; i < dstW; i++) {
1814 int U = (1<<9)-(128 << 19);
1815 int V = (1<<9)-(128 << 19);
1817 for (j = 0; j < lumFilterSize; j++) {
1818 Y += lumSrc[j][i] * lumFilter[j];
1820 for (j = 0; j < chrFilterSize; j++) {
1821 U += chrUSrc[j][i] * chrFilter[j];
1822 V += chrVSrc[j][i] * chrFilter[j];
1829 for (j = 0; j < lumFilterSize; j++) {
1830 A += alpSrc[j][i] * lumFilter[j];
1834 A = av_clip_uint8(A);
1836 yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1839 c->dither_error[0][i] = err[0];
1840 c->dither_error[1][i] = err[1];
1841 c->dither_error[2][i] = err[2];
1844 static av_always_inline void
1845 yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
1846 const int16_t *ubuf[2], const int16_t *vbuf[2],
1847 const int16_t *abuf[2], uint8_t *dest, int dstW,
1848 int yalpha, int uvalpha, int y,
1849 enum AVPixelFormat target, int hasAlpha)
1851 const int16_t *buf0 = buf[0], *buf1 = buf[1],
1852 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1853 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1854 *abuf0 = hasAlpha ? abuf[0] : NULL,
1855 *abuf1 = hasAlpha ? abuf[1] : NULL;
1856 int yalpha1 = 4096 - yalpha;
1857 int uvalpha1 = 4096 - uvalpha;
1859 int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1861 int A = 0; // init to silcene warning
1863 av_assert2(yalpha <= 4096U);
1864 av_assert2(uvalpha <= 4096U);
1866 if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1867 || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
1870 for (i = 0; i < dstW; i++) {
1871 int Y = ( buf0[i] * yalpha1 + buf1[i] * yalpha ) >> 10; //FIXME rounding
1872 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
1873 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
1876 A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
1878 A = av_clip_uint8(A);
1881 yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1884 c->dither_error[0][i] = err[0];
1885 c->dither_error[1][i] = err[1];
1886 c->dither_error[2][i] = err[2];
1889 static av_always_inline void
1890 yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
1891 const int16_t *ubuf[2], const int16_t *vbuf[2],
1892 const int16_t *abuf0, uint8_t *dest, int dstW,
1893 int uvalpha, int y, enum AVPixelFormat target,
1896 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1898 int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1901 if( target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1902 || target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8)
1905 if (uvalpha < 2048) {
1906 int A = 0; //init to silence warning
1907 for (i = 0; i < dstW; i++) {
1908 int Y = buf0[i] << 2;
1909 int U = (ubuf0[i] - (128<<7)) * 4;
1910 int V = (vbuf0[i] - (128<<7)) * 4;
1913 A = (abuf0[i] + 64) >> 7;
1915 A = av_clip_uint8(A);
1918 yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1922 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1923 int A = 0; //init to silence warning
1924 for (i = 0; i < dstW; i++) {
1925 int Y = buf0[i] << 2;
1926 int U = (ubuf0[i] + ubuf1[i] - (128<<8)) << 1;
1927 int V = (vbuf0[i] + vbuf1[i] - (128<<8)) << 1;
1930 A = (abuf0[i] + 64) >> 7;
1932 A = av_clip_uint8(A);
1935 yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1940 c->dither_error[0][i] = err[0];
1941 c->dither_error[1][i] = err[1];
1942 c->dither_error[2][i] = err[2];
1946 YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->needAlpha)
1947 YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->needAlpha)
1948 YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->needAlpha)
1949 YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->needAlpha)
1951 #if CONFIG_SWSCALE_ALPHA
1952 YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA, 1)
1953 YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR, 1)
1954 YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA, 1)
1955 YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB, 1)
1957 YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA, 0)
1958 YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR, 0)
1959 YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA, 0)
1960 YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB, 0)
1962 YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full, AV_PIX_FMT_BGR24, 0)
1963 YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full, AV_PIX_FMT_RGB24, 0)
1965 YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full, AV_PIX_FMT_BGR4_BYTE, 0)
1966 YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full, AV_PIX_FMT_RGB4_BYTE, 0)
1967 YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full, AV_PIX_FMT_BGR8, 0)
1968 YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full, AV_PIX_FMT_RGB8, 0)
1971 yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
1972 const int16_t **lumSrc, int lumFilterSize,
1973 const int16_t *chrFilter, const int16_t **chrUSrc,
1974 const int16_t **chrVSrc, int chrFilterSize,
1975 const int16_t **alpSrc, uint8_t **dest,
1978 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
1980 int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
1981 uint16_t **dest16 = (uint16_t**)dest;
1982 int SH = 22 + 8 - desc->comp[0].depth;
1983 int A = 0; // init to silence warning
1985 for (i = 0; i < dstW; i++) {
1988 int U = (1 << 9) - (128 << 19);
1989 int V = (1 << 9) - (128 << 19);
1992 for (j = 0; j < lumFilterSize; j++)
1993 Y += lumSrc[j][i] * lumFilter[j];
1995 for (j = 0; j < chrFilterSize; j++) {
1996 U += chrUSrc[j][i] * chrFilter[j];
1997 V += chrVSrc[j][i] * chrFilter[j];
2007 for (j = 0; j < lumFilterSize; j++)
2008 A += alpSrc[j][i] * lumFilter[j];
2011 A = av_clip_uintp2(A, 27);
2014 Y -= c->yuv2rgb_y_offset;
2015 Y *= c->yuv2rgb_y_coeff;
2017 R = Y + V * c->yuv2rgb_v2r_coeff;
2018 G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
2019 B = Y + U * c->yuv2rgb_u2b_coeff;
2021 if ((R | G | B) & 0xC0000000) {
2022 R = av_clip_uintp2(R, 30);
2023 G = av_clip_uintp2(G, 30);
2024 B = av_clip_uintp2(B, 30);
2028 dest16[0][i] = G >> SH;
2029 dest16[1][i] = B >> SH;
2030 dest16[2][i] = R >> SH;
2032 dest16[3][i] = A >> (SH - 3);
2034 dest[0][i] = G >> 22;
2035 dest[1][i] = B >> 22;
2036 dest[2][i] = R >> 22;
2038 dest[3][i] = A >> 19;
2041 if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
2042 for (i = 0; i < dstW; i++) {
2043 dest16[0][i] = av_bswap16(dest16[0][i]);
2044 dest16[1][i] = av_bswap16(dest16[1][i]);
2045 dest16[2][i] = av_bswap16(dest16[2][i]);
2047 dest16[3][i] = av_bswap16(dest16[3][i]);
2053 yuv2gbrp16_full_X_c(SwsContext *c, const int16_t *lumFilter,
2054 const int16_t **lumSrcx, int lumFilterSize,
2055 const int16_t *chrFilter, const int16_t **chrUSrcx,
2056 const int16_t **chrVSrcx, int chrFilterSize,
2057 const int16_t **alpSrcx, uint8_t **dest,
2060 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
2062 int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrcx;
2063 uint16_t **dest16 = (uint16_t**)dest;
2064 const int32_t **lumSrc = (const int32_t**)lumSrcx;
2065 const int32_t **chrUSrc = (const int32_t**)chrUSrcx;
2066 const int32_t **chrVSrc = (const int32_t**)chrVSrcx;
2067 const int32_t **alpSrc = (const int32_t**)alpSrcx;
2069 for (i = 0; i < dstW; i++) {
2071 int Y = -0x40000000;
2072 int U = -(128 << 23);
2073 int V = -(128 << 23);
2076 for (j = 0; j < lumFilterSize; j++)
2077 Y += lumSrc[j][i] * (unsigned)lumFilter[j];
2079 for (j = 0; j < chrFilterSize; j++) {
2080 U += chrUSrc[j][i] * (unsigned)chrFilter[j];
2081 V += chrVSrc[j][i] * (unsigned)chrFilter[j];
2092 for (j = 0; j < lumFilterSize; j++)
2093 A += alpSrc[j][i] * lumFilter[j];
2099 Y -= c->yuv2rgb_y_offset;
2100 Y *= c->yuv2rgb_y_coeff;
2102 R = V * c->yuv2rgb_v2r_coeff;
2103 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
2104 B = U * c->yuv2rgb_u2b_coeff;
2106 R = av_clip_uintp2(Y + R, 30);
2107 G = av_clip_uintp2(Y + G, 30);
2108 B = av_clip_uintp2(Y + B, 30);
2110 dest16[0][i] = G >> 14;
2111 dest16[1][i] = B >> 14;
2112 dest16[2][i] = R >> 14;
2114 dest16[3][i] = av_clip_uintp2(A, 30) >> 14;
2116 if ((!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
2117 for (i = 0; i < dstW; i++) {
2118 dest16[0][i] = av_bswap16(dest16[0][i]);
2119 dest16[1][i] = av_bswap16(dest16[1][i]);
2120 dest16[2][i] = av_bswap16(dest16[2][i]);
2122 dest16[3][i] = av_bswap16(dest16[3][i]);
2128 yuv2ya8_1_c(SwsContext *c, const int16_t *buf0,
2129 const int16_t *ubuf[2], const int16_t *vbuf[2],
2130 const int16_t *abuf0, uint8_t *dest, int dstW,
2133 int hasAlpha = !!abuf0;
2136 for (i = 0; i < dstW; i++) {
2137 int Y = (buf0[i] + 64) >> 7;
2140 Y = av_clip_uint8(Y);
2143 A = (abuf0[i] + 64) >> 7;
2145 A = av_clip_uint8(A);
2149 dest[i * 2 + 1] = hasAlpha ? A : 255;
2154 yuv2ya8_2_c(SwsContext *c, const int16_t *buf[2],
2155 const int16_t *ubuf[2], const int16_t *vbuf[2],
2156 const int16_t *abuf[2], uint8_t *dest, int dstW,
2157 int yalpha, int uvalpha, int y)
2159 int hasAlpha = abuf && abuf[0] && abuf[1];
2160 const int16_t *buf0 = buf[0], *buf1 = buf[1],
2161 *abuf0 = hasAlpha ? abuf[0] : NULL,
2162 *abuf1 = hasAlpha ? abuf[1] : NULL;
2163 int yalpha1 = 4096 - yalpha;
2166 av_assert2(yalpha <= 4096U);
2168 for (i = 0; i < dstW; i++) {
2169 int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 19;
2172 Y = av_clip_uint8(Y);
2175 A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 19;
2176 A = av_clip_uint8(A);
2180 dest[i * 2 + 1] = hasAlpha ? A : 255;
2185 yuv2ya8_X_c(SwsContext *c, const int16_t *lumFilter,
2186 const int16_t **lumSrc, int lumFilterSize,
2187 const int16_t *chrFilter, const int16_t **chrUSrc,
2188 const int16_t **chrVSrc, int chrFilterSize,
2189 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
2191 int hasAlpha = !!alpSrc;
2194 for (i = 0; i < dstW; i++) {
2196 int Y = 1 << 18, A = 1 << 18;
2198 for (j = 0; j < lumFilterSize; j++)
2199 Y += lumSrc[j][i] * lumFilter[j];
2203 Y = av_clip_uint8(Y);
2206 for (j = 0; j < lumFilterSize; j++)
2207 A += alpSrc[j][i] * lumFilter[j];
2212 A = av_clip_uint8(A);
2216 dest[2 * i + 1] = hasAlpha ? A : 255;
2221 yuv2ayuv64le_X_c(SwsContext *c, const int16_t *lumFilter,
2222 const int16_t **_lumSrc, int lumFilterSize,
2223 const int16_t *chrFilter, const int16_t **_chrUSrc,
2224 const int16_t **_chrVSrc, int chrFilterSize,
2225 const int16_t **_alpSrc, uint8_t *dest, int dstW, int y)
2227 const int32_t **lumSrc = (const int32_t **) _lumSrc,
2228 **chrUSrc = (const int32_t **) _chrUSrc,
2229 **chrVSrc = (const int32_t **) _chrVSrc,
2230 **alpSrc = (const int32_t **) _alpSrc;
2231 int hasAlpha = !!alpSrc;
2234 for (i = 0; i < dstW; i++) {
2235 int Y = 1 << 14, U = 1 << 14;
2236 int V = 1 << 14, A = 1 << 14;
2244 for (j = 0; j < lumFilterSize; j++)
2245 Y += lumSrc[j][i] * (unsigned)lumFilter[j];
2247 for (j = 0; j < chrFilterSize; j++)
2248 U += chrUSrc[j][i] * (unsigned)chrFilter[j];
2250 for (j = 0; j < chrFilterSize; j++)
2251 V += chrVSrc[j][i] * (unsigned)chrFilter[j];
2254 for (j = 0; j < lumFilterSize; j++)
2255 A += alpSrc[j][i] * (unsigned)lumFilter[j];
2257 Y = 0x8000 + av_clip_int16(Y >> 15);
2258 U = 0x8000 + av_clip_int16(U >> 15);
2259 V = 0x8000 + av_clip_int16(V >> 15);
2260 A = 0x8000 + av_clip_int16(A >> 15);
2262 AV_WL16(dest + 8 * i, hasAlpha ? A : 65535);
2263 AV_WL16(dest + 8 * i + 2, Y);
2264 AV_WL16(dest + 8 * i + 4, U);
2265 AV_WL16(dest + 8 * i + 6, V);
2269 av_cold void ff_sws_init_output_funcs(SwsContext *c,
2270 yuv2planar1_fn *yuv2plane1,
2271 yuv2planarX_fn *yuv2planeX,
2272 yuv2interleavedX_fn *yuv2nv12cX,
2273 yuv2packed1_fn *yuv2packed1,
2274 yuv2packed2_fn *yuv2packed2,
2275 yuv2packedX_fn *yuv2packedX,
2276 yuv2anyX_fn *yuv2anyX)
2278 enum AVPixelFormat dstFormat = c->dstFormat;
2279 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
2281 if (dstFormat == AV_PIX_FMT_P010LE || dstFormat == AV_PIX_FMT_P010BE) {
2282 *yuv2plane1 = isBE(dstFormat) ? yuv2p010l1_BE_c : yuv2p010l1_LE_c;
2283 *yuv2planeX = isBE(dstFormat) ? yuv2p010lX_BE_c : yuv2p010lX_LE_c;
2284 *yuv2nv12cX = yuv2p010cX_c;
2285 } else if (is16BPS(dstFormat)) {
2286 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
2287 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
2288 if (dstFormat == AV_PIX_FMT_P016LE || dstFormat == AV_PIX_FMT_P016BE) {
2289 *yuv2nv12cX = yuv2p016cX_c;
2291 } else if (isNBPS(dstFormat)) {
2292 if (desc->comp[0].depth == 9) {
2293 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
2294 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
2295 } else if (desc->comp[0].depth == 10) {
2296 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
2297 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
2298 } else if (desc->comp[0].depth == 12) {
2299 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c : yuv2planeX_12LE_c;
2300 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c : yuv2plane1_12LE_c;
2301 } else if (desc->comp[0].depth == 14) {
2302 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c : yuv2planeX_14LE_c;
2303 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c : yuv2plane1_14LE_c;
2307 *yuv2plane1 = yuv2plane1_8_c;
2308 *yuv2planeX = yuv2planeX_8_c;
2309 if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
2310 *yuv2nv12cX = yuv2nv12cX_c;
2313 if(c->flags & SWS_FULL_CHR_H_INT) {
2314 switch (dstFormat) {
2315 case AV_PIX_FMT_RGBA:
2317 *yuv2packedX = yuv2rgba32_full_X_c;
2318 *yuv2packed2 = yuv2rgba32_full_2_c;
2319 *yuv2packed1 = yuv2rgba32_full_1_c;
2321 #if CONFIG_SWSCALE_ALPHA
2323 *yuv2packedX = yuv2rgba32_full_X_c;
2324 *yuv2packed2 = yuv2rgba32_full_2_c;
2325 *yuv2packed1 = yuv2rgba32_full_1_c;
2327 #endif /* CONFIG_SWSCALE_ALPHA */
2329 *yuv2packedX = yuv2rgbx32_full_X_c;
2330 *yuv2packed2 = yuv2rgbx32_full_2_c;
2331 *yuv2packed1 = yuv2rgbx32_full_1_c;
2333 #endif /* !CONFIG_SMALL */
2335 case AV_PIX_FMT_ARGB:
2337 *yuv2packedX = yuv2argb32_full_X_c;
2338 *yuv2packed2 = yuv2argb32_full_2_c;
2339 *yuv2packed1 = yuv2argb32_full_1_c;
2341 #if CONFIG_SWSCALE_ALPHA
2343 *yuv2packedX = yuv2argb32_full_X_c;
2344 *yuv2packed2 = yuv2argb32_full_2_c;
2345 *yuv2packed1 = yuv2argb32_full_1_c;
2347 #endif /* CONFIG_SWSCALE_ALPHA */
2349 *yuv2packedX = yuv2xrgb32_full_X_c;
2350 *yuv2packed2 = yuv2xrgb32_full_2_c;
2351 *yuv2packed1 = yuv2xrgb32_full_1_c;
2353 #endif /* !CONFIG_SMALL */
2355 case AV_PIX_FMT_BGRA:
2357 *yuv2packedX = yuv2bgra32_full_X_c;
2358 *yuv2packed2 = yuv2bgra32_full_2_c;
2359 *yuv2packed1 = yuv2bgra32_full_1_c;
2361 #if CONFIG_SWSCALE_ALPHA
2363 *yuv2packedX = yuv2bgra32_full_X_c;
2364 *yuv2packed2 = yuv2bgra32_full_2_c;
2365 *yuv2packed1 = yuv2bgra32_full_1_c;
2367 #endif /* CONFIG_SWSCALE_ALPHA */
2369 *yuv2packedX = yuv2bgrx32_full_X_c;
2370 *yuv2packed2 = yuv2bgrx32_full_2_c;
2371 *yuv2packed1 = yuv2bgrx32_full_1_c;
2373 #endif /* !CONFIG_SMALL */
2375 case AV_PIX_FMT_ABGR:
2377 *yuv2packedX = yuv2abgr32_full_X_c;
2378 *yuv2packed2 = yuv2abgr32_full_2_c;
2379 *yuv2packed1 = yuv2abgr32_full_1_c;
2381 #if CONFIG_SWSCALE_ALPHA
2383 *yuv2packedX = yuv2abgr32_full_X_c;
2384 *yuv2packed2 = yuv2abgr32_full_2_c;
2385 *yuv2packed1 = yuv2abgr32_full_1_c;
2387 #endif /* CONFIG_SWSCALE_ALPHA */
2389 *yuv2packedX = yuv2xbgr32_full_X_c;
2390 *yuv2packed2 = yuv2xbgr32_full_2_c;
2391 *yuv2packed1 = yuv2xbgr32_full_1_c;
2393 #endif /* !CONFIG_SMALL */
2395 case AV_PIX_FMT_RGBA64LE:
2396 #if CONFIG_SWSCALE_ALPHA
2398 *yuv2packedX = yuv2rgba64le_full_X_c;
2399 *yuv2packed2 = yuv2rgba64le_full_2_c;
2400 *yuv2packed1 = yuv2rgba64le_full_1_c;
2402 #endif /* CONFIG_SWSCALE_ALPHA */
2404 *yuv2packedX = yuv2rgbx64le_full_X_c;
2405 *yuv2packed2 = yuv2rgbx64le_full_2_c;
2406 *yuv2packed1 = yuv2rgbx64le_full_1_c;
2409 case AV_PIX_FMT_RGBA64BE:
2410 #if CONFIG_SWSCALE_ALPHA
2412 *yuv2packedX = yuv2rgba64be_full_X_c;
2413 *yuv2packed2 = yuv2rgba64be_full_2_c;
2414 *yuv2packed1 = yuv2rgba64be_full_1_c;
2416 #endif /* CONFIG_SWSCALE_ALPHA */
2418 *yuv2packedX = yuv2rgbx64be_full_X_c;
2419 *yuv2packed2 = yuv2rgbx64be_full_2_c;
2420 *yuv2packed1 = yuv2rgbx64be_full_1_c;
2423 case AV_PIX_FMT_BGRA64LE:
2424 #if CONFIG_SWSCALE_ALPHA
2426 *yuv2packedX = yuv2bgra64le_full_X_c;
2427 *yuv2packed2 = yuv2bgra64le_full_2_c;
2428 *yuv2packed1 = yuv2bgra64le_full_1_c;
2430 #endif /* CONFIG_SWSCALE_ALPHA */
2432 *yuv2packedX = yuv2bgrx64le_full_X_c;
2433 *yuv2packed2 = yuv2bgrx64le_full_2_c;
2434 *yuv2packed1 = yuv2bgrx64le_full_1_c;
2437 case AV_PIX_FMT_BGRA64BE:
2438 #if CONFIG_SWSCALE_ALPHA
2440 *yuv2packedX = yuv2bgra64be_full_X_c;
2441 *yuv2packed2 = yuv2bgra64be_full_2_c;
2442 *yuv2packed1 = yuv2bgra64be_full_1_c;
2444 #endif /* CONFIG_SWSCALE_ALPHA */
2446 *yuv2packedX = yuv2bgrx64be_full_X_c;
2447 *yuv2packed2 = yuv2bgrx64be_full_2_c;
2448 *yuv2packed1 = yuv2bgrx64be_full_1_c;
2452 case AV_PIX_FMT_RGB24:
2453 *yuv2packedX = yuv2rgb24_full_X_c;
2454 *yuv2packed2 = yuv2rgb24_full_2_c;
2455 *yuv2packed1 = yuv2rgb24_full_1_c;
2457 case AV_PIX_FMT_BGR24:
2458 *yuv2packedX = yuv2bgr24_full_X_c;
2459 *yuv2packed2 = yuv2bgr24_full_2_c;
2460 *yuv2packed1 = yuv2bgr24_full_1_c;
2462 case AV_PIX_FMT_RGB48LE:
2463 *yuv2packedX = yuv2rgb48le_full_X_c;
2464 *yuv2packed2 = yuv2rgb48le_full_2_c;
2465 *yuv2packed1 = yuv2rgb48le_full_1_c;
2467 case AV_PIX_FMT_BGR48LE:
2468 *yuv2packedX = yuv2bgr48le_full_X_c;
2469 *yuv2packed2 = yuv2bgr48le_full_2_c;
2470 *yuv2packed1 = yuv2bgr48le_full_1_c;
2472 case AV_PIX_FMT_RGB48BE:
2473 *yuv2packedX = yuv2rgb48be_full_X_c;
2474 *yuv2packed2 = yuv2rgb48be_full_2_c;
2475 *yuv2packed1 = yuv2rgb48be_full_1_c;
2477 case AV_PIX_FMT_BGR48BE:
2478 *yuv2packedX = yuv2bgr48be_full_X_c;
2479 *yuv2packed2 = yuv2bgr48be_full_2_c;
2480 *yuv2packed1 = yuv2bgr48be_full_1_c;
2482 case AV_PIX_FMT_BGR4_BYTE:
2483 *yuv2packedX = yuv2bgr4_byte_full_X_c;
2484 *yuv2packed2 = yuv2bgr4_byte_full_2_c;
2485 *yuv2packed1 = yuv2bgr4_byte_full_1_c;
2487 case AV_PIX_FMT_RGB4_BYTE:
2488 *yuv2packedX = yuv2rgb4_byte_full_X_c;
2489 *yuv2packed2 = yuv2rgb4_byte_full_2_c;
2490 *yuv2packed1 = yuv2rgb4_byte_full_1_c;
2492 case AV_PIX_FMT_BGR8:
2493 *yuv2packedX = yuv2bgr8_full_X_c;
2494 *yuv2packed2 = yuv2bgr8_full_2_c;
2495 *yuv2packed1 = yuv2bgr8_full_1_c;
2497 case AV_PIX_FMT_RGB8:
2498 *yuv2packedX = yuv2rgb8_full_X_c;
2499 *yuv2packed2 = yuv2rgb8_full_2_c;
2500 *yuv2packed1 = yuv2rgb8_full_1_c;
2502 case AV_PIX_FMT_GBRP:
2503 case AV_PIX_FMT_GBRP9BE:
2504 case AV_PIX_FMT_GBRP9LE:
2505 case AV_PIX_FMT_GBRP10BE:
2506 case AV_PIX_FMT_GBRP10LE:
2507 case AV_PIX_FMT_GBRP12BE:
2508 case AV_PIX_FMT_GBRP12LE:
2509 case AV_PIX_FMT_GBRP14BE:
2510 case AV_PIX_FMT_GBRP14LE:
2511 case AV_PIX_FMT_GBRAP:
2512 case AV_PIX_FMT_GBRAP10BE:
2513 case AV_PIX_FMT_GBRAP10LE:
2514 case AV_PIX_FMT_GBRAP12BE:
2515 case AV_PIX_FMT_GBRAP12LE:
2516 *yuv2anyX = yuv2gbrp_full_X_c;
2518 case AV_PIX_FMT_GBRP16BE:
2519 case AV_PIX_FMT_GBRP16LE:
2520 case AV_PIX_FMT_GBRAP16BE:
2521 case AV_PIX_FMT_GBRAP16LE:
2522 *yuv2anyX = yuv2gbrp16_full_X_c;
2525 if (!*yuv2packedX && !*yuv2anyX)
2529 switch (dstFormat) {
2530 case AV_PIX_FMT_RGBA64LE:
2531 #if CONFIG_SWSCALE_ALPHA
2533 *yuv2packed1 = yuv2rgba64le_1_c;
2534 *yuv2packed2 = yuv2rgba64le_2_c;
2535 *yuv2packedX = yuv2rgba64le_X_c;
2537 #endif /* CONFIG_SWSCALE_ALPHA */
2539 *yuv2packed1 = yuv2rgbx64le_1_c;
2540 *yuv2packed2 = yuv2rgbx64le_2_c;
2541 *yuv2packedX = yuv2rgbx64le_X_c;
2544 case AV_PIX_FMT_RGBA64BE:
2545 #if CONFIG_SWSCALE_ALPHA
2547 *yuv2packed1 = yuv2rgba64be_1_c;
2548 *yuv2packed2 = yuv2rgba64be_2_c;
2549 *yuv2packedX = yuv2rgba64be_X_c;
2551 #endif /* CONFIG_SWSCALE_ALPHA */
2553 *yuv2packed1 = yuv2rgbx64be_1_c;
2554 *yuv2packed2 = yuv2rgbx64be_2_c;
2555 *yuv2packedX = yuv2rgbx64be_X_c;
2558 case AV_PIX_FMT_BGRA64LE:
2559 #if CONFIG_SWSCALE_ALPHA
2561 *yuv2packed1 = yuv2bgra64le_1_c;
2562 *yuv2packed2 = yuv2bgra64le_2_c;
2563 *yuv2packedX = yuv2bgra64le_X_c;
2565 #endif /* CONFIG_SWSCALE_ALPHA */
2567 *yuv2packed1 = yuv2bgrx64le_1_c;
2568 *yuv2packed2 = yuv2bgrx64le_2_c;
2569 *yuv2packedX = yuv2bgrx64le_X_c;
2572 case AV_PIX_FMT_BGRA64BE:
2573 #if CONFIG_SWSCALE_ALPHA
2575 *yuv2packed1 = yuv2bgra64be_1_c;
2576 *yuv2packed2 = yuv2bgra64be_2_c;
2577 *yuv2packedX = yuv2bgra64be_X_c;
2579 #endif /* CONFIG_SWSCALE_ALPHA */
2581 *yuv2packed1 = yuv2bgrx64be_1_c;
2582 *yuv2packed2 = yuv2bgrx64be_2_c;
2583 *yuv2packedX = yuv2bgrx64be_X_c;
2586 case AV_PIX_FMT_RGB48LE:
2587 *yuv2packed1 = yuv2rgb48le_1_c;
2588 *yuv2packed2 = yuv2rgb48le_2_c;
2589 *yuv2packedX = yuv2rgb48le_X_c;
2591 case AV_PIX_FMT_RGB48BE:
2592 *yuv2packed1 = yuv2rgb48be_1_c;
2593 *yuv2packed2 = yuv2rgb48be_2_c;
2594 *yuv2packedX = yuv2rgb48be_X_c;
2596 case AV_PIX_FMT_BGR48LE:
2597 *yuv2packed1 = yuv2bgr48le_1_c;
2598 *yuv2packed2 = yuv2bgr48le_2_c;
2599 *yuv2packedX = yuv2bgr48le_X_c;
2601 case AV_PIX_FMT_BGR48BE:
2602 *yuv2packed1 = yuv2bgr48be_1_c;
2603 *yuv2packed2 = yuv2bgr48be_2_c;
2604 *yuv2packedX = yuv2bgr48be_X_c;
2606 case AV_PIX_FMT_RGB32:
2607 case AV_PIX_FMT_BGR32:
2609 *yuv2packed1 = yuv2rgb32_1_c;
2610 *yuv2packed2 = yuv2rgb32_2_c;
2611 *yuv2packedX = yuv2rgb32_X_c;
2613 #if CONFIG_SWSCALE_ALPHA
2615 *yuv2packed1 = yuv2rgba32_1_c;
2616 *yuv2packed2 = yuv2rgba32_2_c;
2617 *yuv2packedX = yuv2rgba32_X_c;
2619 #endif /* CONFIG_SWSCALE_ALPHA */
2621 *yuv2packed1 = yuv2rgbx32_1_c;
2622 *yuv2packed2 = yuv2rgbx32_2_c;
2623 *yuv2packedX = yuv2rgbx32_X_c;
2625 #endif /* !CONFIG_SMALL */
2627 case AV_PIX_FMT_RGB32_1:
2628 case AV_PIX_FMT_BGR32_1:
2630 *yuv2packed1 = yuv2rgb32_1_1_c;
2631 *yuv2packed2 = yuv2rgb32_1_2_c;
2632 *yuv2packedX = yuv2rgb32_1_X_c;
2634 #if CONFIG_SWSCALE_ALPHA
2636 *yuv2packed1 = yuv2rgba32_1_1_c;
2637 *yuv2packed2 = yuv2rgba32_1_2_c;
2638 *yuv2packedX = yuv2rgba32_1_X_c;
2640 #endif /* CONFIG_SWSCALE_ALPHA */
2642 *yuv2packed1 = yuv2rgbx32_1_1_c;
2643 *yuv2packed2 = yuv2rgbx32_1_2_c;
2644 *yuv2packedX = yuv2rgbx32_1_X_c;
2646 #endif /* !CONFIG_SMALL */
2648 case AV_PIX_FMT_RGB24:
2649 *yuv2packed1 = yuv2rgb24_1_c;
2650 *yuv2packed2 = yuv2rgb24_2_c;
2651 *yuv2packedX = yuv2rgb24_X_c;
2653 case AV_PIX_FMT_BGR24:
2654 *yuv2packed1 = yuv2bgr24_1_c;
2655 *yuv2packed2 = yuv2bgr24_2_c;
2656 *yuv2packedX = yuv2bgr24_X_c;
2658 case AV_PIX_FMT_RGB565LE:
2659 case AV_PIX_FMT_RGB565BE:
2660 case AV_PIX_FMT_BGR565LE:
2661 case AV_PIX_FMT_BGR565BE:
2662 *yuv2packed1 = yuv2rgb16_1_c;
2663 *yuv2packed2 = yuv2rgb16_2_c;
2664 *yuv2packedX = yuv2rgb16_X_c;
2666 case AV_PIX_FMT_RGB555LE:
2667 case AV_PIX_FMT_RGB555BE:
2668 case AV_PIX_FMT_BGR555LE:
2669 case AV_PIX_FMT_BGR555BE:
2670 *yuv2packed1 = yuv2rgb15_1_c;
2671 *yuv2packed2 = yuv2rgb15_2_c;
2672 *yuv2packedX = yuv2rgb15_X_c;
2674 case AV_PIX_FMT_RGB444LE:
2675 case AV_PIX_FMT_RGB444BE:
2676 case AV_PIX_FMT_BGR444LE:
2677 case AV_PIX_FMT_BGR444BE:
2678 *yuv2packed1 = yuv2rgb12_1_c;
2679 *yuv2packed2 = yuv2rgb12_2_c;
2680 *yuv2packedX = yuv2rgb12_X_c;
2682 case AV_PIX_FMT_RGB8:
2683 case AV_PIX_FMT_BGR8:
2684 *yuv2packed1 = yuv2rgb8_1_c;
2685 *yuv2packed2 = yuv2rgb8_2_c;
2686 *yuv2packedX = yuv2rgb8_X_c;
2688 case AV_PIX_FMT_RGB4:
2689 case AV_PIX_FMT_BGR4:
2690 *yuv2packed1 = yuv2rgb4_1_c;
2691 *yuv2packed2 = yuv2rgb4_2_c;
2692 *yuv2packedX = yuv2rgb4_X_c;
2694 case AV_PIX_FMT_RGB4_BYTE:
2695 case AV_PIX_FMT_BGR4_BYTE:
2696 *yuv2packed1 = yuv2rgb4b_1_c;
2697 *yuv2packed2 = yuv2rgb4b_2_c;
2698 *yuv2packedX = yuv2rgb4b_X_c;
2702 switch (dstFormat) {
2703 case AV_PIX_FMT_MONOWHITE:
2704 *yuv2packed1 = yuv2monowhite_1_c;
2705 *yuv2packed2 = yuv2monowhite_2_c;
2706 *yuv2packedX = yuv2monowhite_X_c;
2708 case AV_PIX_FMT_MONOBLACK:
2709 *yuv2packed1 = yuv2monoblack_1_c;
2710 *yuv2packed2 = yuv2monoblack_2_c;
2711 *yuv2packedX = yuv2monoblack_X_c;
2713 case AV_PIX_FMT_YUYV422:
2714 *yuv2packed1 = yuv2yuyv422_1_c;
2715 *yuv2packed2 = yuv2yuyv422_2_c;
2716 *yuv2packedX = yuv2yuyv422_X_c;
2718 case AV_PIX_FMT_YVYU422:
2719 *yuv2packed1 = yuv2yvyu422_1_c;
2720 *yuv2packed2 = yuv2yvyu422_2_c;
2721 *yuv2packedX = yuv2yvyu422_X_c;
2723 case AV_PIX_FMT_UYVY422:
2724 *yuv2packed1 = yuv2uyvy422_1_c;
2725 *yuv2packed2 = yuv2uyvy422_2_c;
2726 *yuv2packedX = yuv2uyvy422_X_c;
2728 case AV_PIX_FMT_YA8:
2729 *yuv2packed1 = yuv2ya8_1_c;
2730 *yuv2packed2 = yuv2ya8_2_c;
2731 *yuv2packedX = yuv2ya8_X_c;
2733 case AV_PIX_FMT_AYUV64LE:
2734 *yuv2packedX = yuv2ayuv64le_X_c;