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
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, 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, 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, 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, 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, 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, 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, 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 PixelFormat dstFormat = c->dstFormat;
267 const uint8_t *chrDither = c->chrDither8;
270 if (dstFormat == 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 output_pixel(pos, acc) \
299 if (target == PIX_FMT_MONOBLACK) { \
305 static av_always_inline void
306 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
307 const int16_t **lumSrc, int lumFilterSize,
308 const int16_t *chrFilter, const int16_t **chrUSrc,
309 const int16_t **chrVSrc, int chrFilterSize,
310 const int16_t **alpSrc, uint8_t *dest, int dstW,
311 int y, enum PixelFormat target)
313 const uint8_t * const d128=dither_8x8_220[y&7];
314 uint8_t *g = c->table_gU[128] + c->table_gV[128];
318 for (i = 0; i < dstW - 1; i += 2) {
323 for (j = 0; j < lumFilterSize; j++) {
324 Y1 += lumSrc[j][i] * lumFilter[j];
325 Y2 += lumSrc[j][i+1] * lumFilter[j];
329 if ((Y1 | Y2) & 0x100) {
330 Y1 = av_clip_uint8(Y1);
331 Y2 = av_clip_uint8(Y2);
333 acc += acc + g[Y1 + d128[(i + 0) & 7]];
334 acc += acc + g[Y2 + d128[(i + 1) & 7]];
336 output_pixel(*dest++, acc);
341 static av_always_inline void
342 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
343 const int16_t *ubuf[2], const int16_t *vbuf[2],
344 const int16_t *abuf[2], uint8_t *dest, int dstW,
345 int yalpha, int uvalpha, int y,
346 enum PixelFormat target)
348 const int16_t *buf0 = buf[0], *buf1 = buf[1];
349 const uint8_t * const d128 = dither_8x8_220[y & 7];
350 uint8_t *g = c->table_gU[128] + c->table_gV[128];
351 int yalpha1 = 4095 - yalpha;
354 for (i = 0; i < dstW - 7; i += 8) {
355 int acc = g[((buf0[i ] * yalpha1 + buf1[i ] * yalpha) >> 19) + d128[0]];
356 acc += acc + g[((buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19) + d128[1]];
357 acc += acc + g[((buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19) + d128[2]];
358 acc += acc + g[((buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19) + d128[3]];
359 acc += acc + g[((buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19) + d128[4]];
360 acc += acc + g[((buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19) + d128[5]];
361 acc += acc + g[((buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19) + d128[6]];
362 acc += acc + g[((buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19) + d128[7]];
363 output_pixel(*dest++, acc);
367 static av_always_inline void
368 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
369 const int16_t *ubuf[2], const int16_t *vbuf[2],
370 const int16_t *abuf0, uint8_t *dest, int dstW,
371 int uvalpha, int y, enum PixelFormat target)
373 const uint8_t * const d128 = dither_8x8_220[y & 7];
374 uint8_t *g = c->table_gU[128] + c->table_gV[128];
377 for (i = 0; i < dstW - 7; i += 8) {
378 int acc = g[(buf0[i ] >> 7) + d128[0]];
379 acc += acc + g[(buf0[i + 1] >> 7) + d128[1]];
380 acc += acc + g[(buf0[i + 2] >> 7) + d128[2]];
381 acc += acc + g[(buf0[i + 3] >> 7) + d128[3]];
382 acc += acc + g[(buf0[i + 4] >> 7) + d128[4]];
383 acc += acc + g[(buf0[i + 5] >> 7) + d128[5]];
384 acc += acc + g[(buf0[i + 6] >> 7) + d128[6]];
385 acc += acc + g[(buf0[i + 7] >> 7) + d128[7]];
386 output_pixel(*dest++, acc);
392 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
393 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
394 const int16_t **lumSrc, int lumFilterSize, \
395 const int16_t *chrFilter, const int16_t **chrUSrc, \
396 const int16_t **chrVSrc, int chrFilterSize, \
397 const int16_t **alpSrc, uint8_t *dest, int dstW, \
400 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
401 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
402 alpSrc, dest, dstW, y, fmt); \
405 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
406 const int16_t *ubuf[2], const int16_t *vbuf[2], \
407 const int16_t *abuf[2], uint8_t *dest, int dstW, \
408 int yalpha, int uvalpha, int y) \
410 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
411 dest, dstW, yalpha, uvalpha, y, fmt); \
414 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
415 const int16_t *ubuf[2], const int16_t *vbuf[2], \
416 const int16_t *abuf0, uint8_t *dest, int dstW, \
417 int uvalpha, int y) \
419 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
420 abuf0, dest, dstW, uvalpha, \
424 YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE)
425 YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK)
427 #define output_pixels(pos, Y1, U, Y2, V) \
428 if (target == PIX_FMT_YUYV422) { \
429 dest[pos + 0] = Y1; \
431 dest[pos + 2] = Y2; \
435 dest[pos + 1] = Y1; \
437 dest[pos + 3] = Y2; \
440 static av_always_inline void
441 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
442 const int16_t **lumSrc, int lumFilterSize,
443 const int16_t *chrFilter, const int16_t **chrUSrc,
444 const int16_t **chrVSrc, int chrFilterSize,
445 const int16_t **alpSrc, uint8_t *dest, int dstW,
446 int y, enum PixelFormat target)
450 for (i = 0; i < (dstW >> 1); i++) {
457 for (j = 0; j < lumFilterSize; j++) {
458 Y1 += lumSrc[j][i * 2] * lumFilter[j];
459 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
461 for (j = 0; j < chrFilterSize; j++) {
462 U += chrUSrc[j][i] * chrFilter[j];
463 V += chrVSrc[j][i] * chrFilter[j];
469 if ((Y1 | Y2 | U | V) & 0x100) {
470 Y1 = av_clip_uint8(Y1);
471 Y2 = av_clip_uint8(Y2);
472 U = av_clip_uint8(U);
473 V = av_clip_uint8(V);
475 output_pixels(4*i, Y1, U, Y2, V);
479 static av_always_inline void
480 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
481 const int16_t *ubuf[2], const int16_t *vbuf[2],
482 const int16_t *abuf[2], uint8_t *dest, int dstW,
483 int yalpha, int uvalpha, int y,
484 enum PixelFormat target)
486 const int16_t *buf0 = buf[0], *buf1 = buf[1],
487 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
488 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
489 int yalpha1 = 4095 - yalpha;
490 int uvalpha1 = 4095 - uvalpha;
493 for (i = 0; i < (dstW >> 1); i++) {
494 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
495 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
496 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
497 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
499 output_pixels(i * 4, Y1, U, Y2, V);
503 static av_always_inline void
504 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
505 const int16_t *ubuf[2], const int16_t *vbuf[2],
506 const int16_t *abuf0, uint8_t *dest, int dstW,
507 int uvalpha, int y, enum PixelFormat target)
509 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
512 if (uvalpha < 2048) {
513 for (i = 0; i < (dstW >> 1); i++) {
514 int Y1 = buf0[i * 2] >> 7;
515 int Y2 = buf0[i * 2 + 1] >> 7;
516 int U = ubuf0[i] >> 7;
517 int V = vbuf0[i] >> 7;
519 output_pixels(i * 4, Y1, U, Y2, V);
522 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
523 for (i = 0; i < (dstW >> 1); i++) {
524 int Y1 = buf0[i * 2] >> 7;
525 int Y2 = buf0[i * 2 + 1] >> 7;
526 int U = (ubuf0[i] + ubuf1[i]) >> 8;
527 int V = (vbuf0[i] + vbuf1[i]) >> 8;
529 output_pixels(i * 4, Y1, U, Y2, V);
536 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422)
537 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422)
539 #define R_B ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? R : B)
540 #define B_R ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? B : R)
541 #define output_pixel(pos, val) \
542 if (isBE(target)) { \
548 static av_always_inline void
549 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
550 const int32_t **lumSrc, int lumFilterSize,
551 const int16_t *chrFilter, const int32_t **chrUSrc,
552 const int32_t **chrVSrc, int chrFilterSize,
553 const int32_t **alpSrc, uint16_t *dest, int dstW,
554 int y, enum PixelFormat target)
558 for (i = 0; i < (dstW >> 1); i++) {
560 int Y1 = -0x40000000;
561 int Y2 = -0x40000000;
562 int U = -128 << 23; // 19
566 for (j = 0; j < lumFilterSize; j++) {
567 Y1 += lumSrc[j][i * 2] * lumFilter[j];
568 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
570 for (j = 0; j < chrFilterSize; j++) {
571 U += chrUSrc[j][i] * chrFilter[j];
572 V += chrVSrc[j][i] * chrFilter[j];
575 // 8bit: 12+15=27; 16-bit: 12+19=31
583 // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
584 Y1 -= c->yuv2rgb_y_offset;
585 Y2 -= c->yuv2rgb_y_offset;
586 Y1 *= c->yuv2rgb_y_coeff;
587 Y2 *= c->yuv2rgb_y_coeff;
590 // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
592 R = V * c->yuv2rgb_v2r_coeff;
593 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
594 B = U * c->yuv2rgb_u2b_coeff;
596 // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
597 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
598 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
599 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
600 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
601 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
602 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
607 static av_always_inline void
608 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
609 const int32_t *ubuf[2], const int32_t *vbuf[2],
610 const int32_t *abuf[2], uint16_t *dest, int dstW,
611 int yalpha, int uvalpha, int y,
612 enum PixelFormat target)
614 const int32_t *buf0 = buf[0], *buf1 = buf[1],
615 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
616 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
617 int yalpha1 = 4095 - yalpha;
618 int uvalpha1 = 4095 - uvalpha;
621 for (i = 0; i < (dstW >> 1); i++) {
622 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
623 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
624 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
625 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
628 Y1 -= c->yuv2rgb_y_offset;
629 Y2 -= c->yuv2rgb_y_offset;
630 Y1 *= c->yuv2rgb_y_coeff;
631 Y2 *= c->yuv2rgb_y_coeff;
635 R = V * c->yuv2rgb_v2r_coeff;
636 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
637 B = U * c->yuv2rgb_u2b_coeff;
639 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
640 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
641 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
642 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
643 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
644 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
649 static av_always_inline void
650 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
651 const int32_t *ubuf[2], const int32_t *vbuf[2],
652 const int32_t *abuf0, uint16_t *dest, int dstW,
653 int uvalpha, int y, enum PixelFormat target)
655 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
658 if (uvalpha < 2048) {
659 for (i = 0; i < (dstW >> 1); i++) {
660 int Y1 = (buf0[i * 2] ) >> 2;
661 int Y2 = (buf0[i * 2 + 1]) >> 2;
662 int U = (ubuf0[i] + (-128 << 11)) >> 2;
663 int V = (vbuf0[i] + (-128 << 11)) >> 2;
666 Y1 -= c->yuv2rgb_y_offset;
667 Y2 -= c->yuv2rgb_y_offset;
668 Y1 *= c->yuv2rgb_y_coeff;
669 Y2 *= c->yuv2rgb_y_coeff;
673 R = V * c->yuv2rgb_v2r_coeff;
674 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
675 B = U * c->yuv2rgb_u2b_coeff;
677 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
678 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
679 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
680 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
681 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
682 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
686 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
687 for (i = 0; i < (dstW >> 1); i++) {
688 int Y1 = (buf0[i * 2] ) >> 2;
689 int Y2 = (buf0[i * 2 + 1]) >> 2;
690 int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
691 int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
694 Y1 -= c->yuv2rgb_y_offset;
695 Y2 -= c->yuv2rgb_y_offset;
696 Y1 *= c->yuv2rgb_y_coeff;
697 Y2 *= c->yuv2rgb_y_coeff;
701 R = V * c->yuv2rgb_v2r_coeff;
702 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
703 B = U * c->yuv2rgb_u2b_coeff;
705 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
706 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
707 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
708 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
709 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
710 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
720 #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
721 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
722 const int16_t **_lumSrc, int lumFilterSize, \
723 const int16_t *chrFilter, const int16_t **_chrUSrc, \
724 const int16_t **_chrVSrc, int chrFilterSize, \
725 const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
728 const int32_t **lumSrc = (const int32_t **) _lumSrc, \
729 **chrUSrc = (const int32_t **) _chrUSrc, \
730 **chrVSrc = (const int32_t **) _chrVSrc, \
731 **alpSrc = (const int32_t **) _alpSrc; \
732 uint16_t *dest = (uint16_t *) _dest; \
733 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
734 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
735 alpSrc, dest, dstW, y, fmt); \
738 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
739 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
740 const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
741 int yalpha, int uvalpha, int y) \
743 const int32_t **buf = (const int32_t **) _buf, \
744 **ubuf = (const int32_t **) _ubuf, \
745 **vbuf = (const int32_t **) _vbuf, \
746 **abuf = (const int32_t **) _abuf; \
747 uint16_t *dest = (uint16_t *) _dest; \
748 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
749 dest, dstW, yalpha, uvalpha, y, fmt); \
752 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
753 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
754 const int16_t *_abuf0, uint8_t *_dest, int dstW, \
755 int uvalpha, int y) \
757 const int32_t *buf0 = (const int32_t *) _buf0, \
758 **ubuf = (const int32_t **) _ubuf, \
759 **vbuf = (const int32_t **) _vbuf, \
760 *abuf0 = (const int32_t *) _abuf0; \
761 uint16_t *dest = (uint16_t *) _dest; \
762 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
763 dstW, uvalpha, y, fmt); \
766 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, PIX_FMT_RGB48BE)
767 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, PIX_FMT_RGB48LE)
768 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, PIX_FMT_BGR48BE)
769 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, PIX_FMT_BGR48LE)
772 * Write out 2 RGB pixels in the target pixel format. This function takes a
773 * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
774 * things like endianness conversion and shifting. The caller takes care of
775 * setting the correct offset in these tables from the chroma (U/V) values.
776 * This function then uses the luminance (Y1/Y2) values to write out the
777 * correct RGB values into the destination buffer.
779 static av_always_inline void
780 yuv2rgb_write(uint8_t *_dest, int i, unsigned Y1, unsigned Y2,
781 unsigned A1, unsigned A2,
782 const void *_r, const void *_g, const void *_b, int y,
783 enum PixelFormat target, int hasAlpha)
785 if (target == PIX_FMT_ARGB || target == PIX_FMT_RGBA ||
786 target == PIX_FMT_ABGR || target == PIX_FMT_BGRA) {
787 uint32_t *dest = (uint32_t *) _dest;
788 const uint32_t *r = (const uint32_t *) _r;
789 const uint32_t *g = (const uint32_t *) _g;
790 const uint32_t *b = (const uint32_t *) _b;
793 int sh = hasAlpha ? ((target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24) : 0;
795 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
796 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
799 int sh = (target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24;
801 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
802 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
804 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
805 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
808 } else if (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) {
809 uint8_t *dest = (uint8_t *) _dest;
810 const uint8_t *r = (const uint8_t *) _r;
811 const uint8_t *g = (const uint8_t *) _g;
812 const uint8_t *b = (const uint8_t *) _b;
814 #define r_b ((target == PIX_FMT_RGB24) ? r : b)
815 #define b_r ((target == PIX_FMT_RGB24) ? b : r)
816 dest[i * 6 + 0] = r_b[Y1];
817 dest[i * 6 + 1] = g[Y1];
818 dest[i * 6 + 2] = b_r[Y1];
819 dest[i * 6 + 3] = r_b[Y2];
820 dest[i * 6 + 4] = g[Y2];
821 dest[i * 6 + 5] = b_r[Y2];
824 } else if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565 ||
825 target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555 ||
826 target == PIX_FMT_RGB444 || target == PIX_FMT_BGR444) {
827 uint16_t *dest = (uint16_t *) _dest;
828 const uint16_t *r = (const uint16_t *) _r;
829 const uint16_t *g = (const uint16_t *) _g;
830 const uint16_t *b = (const uint16_t *) _b;
831 int dr1, dg1, db1, dr2, dg2, db2;
833 if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565) {
834 dr1 = dither_2x2_8[ y & 1 ][0];
835 dg1 = dither_2x2_4[ y & 1 ][0];
836 db1 = dither_2x2_8[(y & 1) ^ 1][0];
837 dr2 = dither_2x2_8[ y & 1 ][1];
838 dg2 = dither_2x2_4[ y & 1 ][1];
839 db2 = dither_2x2_8[(y & 1) ^ 1][1];
840 } else if (target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555) {
841 dr1 = dither_2x2_8[ y & 1 ][0];
842 dg1 = dither_2x2_8[ y & 1 ][1];
843 db1 = dither_2x2_8[(y & 1) ^ 1][0];
844 dr2 = dither_2x2_8[ y & 1 ][1];
845 dg2 = dither_2x2_8[ y & 1 ][0];
846 db2 = dither_2x2_8[(y & 1) ^ 1][1];
848 dr1 = dither_4x4_16[ y & 3 ][0];
849 dg1 = dither_4x4_16[ y & 3 ][1];
850 db1 = dither_4x4_16[(y & 3) ^ 3][0];
851 dr2 = dither_4x4_16[ y & 3 ][1];
852 dg2 = dither_4x4_16[ y & 3 ][0];
853 db2 = dither_4x4_16[(y & 3) ^ 3][1];
856 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
857 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
858 } else /* 8/4-bit */ {
859 uint8_t *dest = (uint8_t *) _dest;
860 const uint8_t *r = (const uint8_t *) _r;
861 const uint8_t *g = (const uint8_t *) _g;
862 const uint8_t *b = (const uint8_t *) _b;
863 int dr1, dg1, db1, dr2, dg2, db2;
865 if (target == PIX_FMT_RGB8 || target == PIX_FMT_BGR8) {
866 const uint8_t * const d64 = dither_8x8_73[y & 7];
867 const uint8_t * const d32 = dither_8x8_32[y & 7];
868 dr1 = dg1 = d32[(i * 2 + 0) & 7];
869 db1 = d64[(i * 2 + 0) & 7];
870 dr2 = dg2 = d32[(i * 2 + 1) & 7];
871 db2 = d64[(i * 2 + 1) & 7];
873 const uint8_t * const d64 = dither_8x8_73 [y & 7];
874 const uint8_t * const d128 = dither_8x8_220[y & 7];
875 dr1 = db1 = d128[(i * 2 + 0) & 7];
876 dg1 = d64[(i * 2 + 0) & 7];
877 dr2 = db2 = d128[(i * 2 + 1) & 7];
878 dg2 = d64[(i * 2 + 1) & 7];
881 if (target == PIX_FMT_RGB4 || target == PIX_FMT_BGR4) {
882 dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
883 ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
885 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
886 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
891 static av_always_inline void
892 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
893 const int16_t **lumSrc, int lumFilterSize,
894 const int16_t *chrFilter, const int16_t **chrUSrc,
895 const int16_t **chrVSrc, int chrFilterSize,
896 const int16_t **alpSrc, uint8_t *dest, int dstW,
897 int y, enum PixelFormat target, int hasAlpha)
901 for (i = 0; i < (dstW >> 1); i++) {
907 const void *r, *g, *b;
909 for (j = 0; j < lumFilterSize; j++) {
910 Y1 += lumSrc[j][i * 2] * lumFilter[j];
911 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
913 for (j = 0; j < chrFilterSize; j++) {
914 U += chrUSrc[j][i] * chrFilter[j];
915 V += chrVSrc[j][i] * chrFilter[j];
921 if ((Y1 | Y2 | U | V) & 0x100) {
922 Y1 = av_clip_uint8(Y1);
923 Y2 = av_clip_uint8(Y2);
924 U = av_clip_uint8(U);
925 V = av_clip_uint8(V);
930 for (j = 0; j < lumFilterSize; j++) {
931 A1 += alpSrc[j][i * 2 ] * lumFilter[j];
932 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
936 if ((A1 | A2) & 0x100) {
937 A1 = av_clip_uint8(A1);
938 A2 = av_clip_uint8(A2);
942 /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
944 g = (c->table_gU[U] + c->table_gV[V]);
947 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
948 r, g, b, y, target, hasAlpha);
952 static av_always_inline void
953 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
954 const int16_t *ubuf[2], const int16_t *vbuf[2],
955 const int16_t *abuf[2], uint8_t *dest, int dstW,
956 int yalpha, int uvalpha, int y,
957 enum PixelFormat target, int hasAlpha)
959 const int16_t *buf0 = buf[0], *buf1 = buf[1],
960 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
961 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
962 *abuf0 = hasAlpha ? abuf[0] : NULL,
963 *abuf1 = hasAlpha ? abuf[1] : NULL;
964 int yalpha1 = 4095 - yalpha;
965 int uvalpha1 = 4095 - uvalpha;
968 for (i = 0; i < (dstW >> 1); i++) {
969 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
970 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
971 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
972 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
974 const void *r = c->table_rV[V],
975 *g = (c->table_gU[U] + c->table_gV[V]),
979 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
980 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
983 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
984 r, g, b, y, target, hasAlpha);
988 static av_always_inline void
989 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
990 const int16_t *ubuf[2], const int16_t *vbuf[2],
991 const int16_t *abuf0, uint8_t *dest, int dstW,
992 int uvalpha, int y, enum PixelFormat target,
995 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
998 if (uvalpha < 2048) {
999 for (i = 0; i < (dstW >> 1); i++) {
1000 int Y1 = buf0[i * 2] >> 7;
1001 int Y2 = buf0[i * 2 + 1] >> 7;
1002 int U = ubuf0[i] >> 7;
1003 int V = vbuf0[i] >> 7;
1005 const void *r = c->table_rV[V],
1006 *g = (c->table_gU[U] + c->table_gV[V]),
1007 *b = c->table_bU[U];
1010 A1 = abuf0[i * 2 ] >> 7;
1011 A2 = abuf0[i * 2 + 1] >> 7;
1014 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1015 r, g, b, y, target, hasAlpha);
1018 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1019 for (i = 0; i < (dstW >> 1); i++) {
1020 int Y1 = buf0[i * 2] >> 7;
1021 int Y2 = buf0[i * 2 + 1] >> 7;
1022 int U = (ubuf0[i] + ubuf1[i]) >> 8;
1023 int V = (vbuf0[i] + vbuf1[i]) >> 8;
1025 const void *r = c->table_rV[V],
1026 *g = (c->table_gU[U] + c->table_gV[V]),
1027 *b = c->table_bU[U];
1030 A1 = abuf0[i * 2 ] >> 7;
1031 A2 = abuf0[i * 2 + 1] >> 7;
1034 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1035 r, g, b, y, target, hasAlpha);
1040 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1041 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1042 const int16_t **lumSrc, int lumFilterSize, \
1043 const int16_t *chrFilter, const int16_t **chrUSrc, \
1044 const int16_t **chrVSrc, int chrFilterSize, \
1045 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1048 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1049 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1050 alpSrc, dest, dstW, y, fmt, hasAlpha); \
1052 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1053 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1054 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1055 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1056 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1057 int yalpha, int uvalpha, int y) \
1059 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1060 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1063 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1064 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1065 const int16_t *abuf0, uint8_t *dest, int dstW, \
1066 int uvalpha, int y) \
1068 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1069 dstW, uvalpha, y, fmt, hasAlpha); \
1073 YUV2RGBWRAPPER(yuv2rgb,, 32_1, PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1074 YUV2RGBWRAPPER(yuv2rgb,, 32, PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1076 #if CONFIG_SWSCALE_ALPHA
1077 YUV2RGBWRAPPER(yuv2rgb,, a32_1, PIX_FMT_RGB32_1, 1)
1078 YUV2RGBWRAPPER(yuv2rgb,, a32, PIX_FMT_RGB32, 1)
1080 YUV2RGBWRAPPER(yuv2rgb,, x32_1, PIX_FMT_RGB32_1, 0)
1081 YUV2RGBWRAPPER(yuv2rgb,, x32, PIX_FMT_RGB32, 0)
1083 YUV2RGBWRAPPER(yuv2, rgb, rgb24, PIX_FMT_RGB24, 0)
1084 YUV2RGBWRAPPER(yuv2, rgb, bgr24, PIX_FMT_BGR24, 0)
1085 YUV2RGBWRAPPER(yuv2rgb,, 16, PIX_FMT_RGB565, 0)
1086 YUV2RGBWRAPPER(yuv2rgb,, 15, PIX_FMT_RGB555, 0)
1087 YUV2RGBWRAPPER(yuv2rgb,, 12, PIX_FMT_RGB444, 0)
1088 YUV2RGBWRAPPER(yuv2rgb,, 8, PIX_FMT_RGB8, 0)
1089 YUV2RGBWRAPPER(yuv2rgb,, 4, PIX_FMT_RGB4, 0)
1090 YUV2RGBWRAPPER(yuv2rgb,, 4b, PIX_FMT_RGB4_BYTE, 0)
1092 static av_always_inline void
1093 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1094 const int16_t **lumSrc, int lumFilterSize,
1095 const int16_t *chrFilter, const int16_t **chrUSrc,
1096 const int16_t **chrVSrc, int chrFilterSize,
1097 const int16_t **alpSrc, uint8_t *dest,
1098 int dstW, int y, enum PixelFormat target, int hasAlpha)
1101 int step = (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) ? 3 : 4;
1103 for (i = 0; i < dstW; i++) {
1110 for (j = 0; j < lumFilterSize; j++) {
1111 Y += lumSrc[j][i] * lumFilter[j];
1113 for (j = 0; j < chrFilterSize; j++) {
1114 U += chrUSrc[j][i] * chrFilter[j];
1115 V += chrVSrc[j][i] * chrFilter[j];
1122 for (j = 0; j < lumFilterSize; j++) {
1123 A += alpSrc[j][i] * lumFilter[j];
1127 A = av_clip_uint8(A);
1129 Y -= c->yuv2rgb_y_offset;
1130 Y *= c->yuv2rgb_y_coeff;
1132 R = Y + V*c->yuv2rgb_v2r_coeff;
1133 G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1134 B = Y + U*c->yuv2rgb_u2b_coeff;
1135 if ((R | G | B) & 0xC0000000) {
1136 R = av_clip_uintp2(R, 30);
1137 G = av_clip_uintp2(G, 30);
1138 B = av_clip_uintp2(B, 30);
1143 dest[0] = hasAlpha ? A : 255;
1157 dest[3] = hasAlpha ? A : 255;
1160 dest[0] = hasAlpha ? A : 255;
1175 dest[3] = hasAlpha ? A : 255;
1183 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1184 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1185 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1186 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1188 #if CONFIG_SWSCALE_ALPHA
1189 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, 1)
1190 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, 1)
1191 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, 1)
1192 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, 1)
1194 YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, PIX_FMT_BGRA, 0)
1195 YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, PIX_FMT_ABGR, 0)
1196 YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, PIX_FMT_RGBA, 0)
1197 YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, PIX_FMT_ARGB, 0)
1199 YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, PIX_FMT_BGR24, 0)
1200 YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, PIX_FMT_RGB24, 0)
1202 void ff_sws_init_output_funcs(SwsContext *c,
1203 yuv2planar1_fn *yuv2plane1,
1204 yuv2planarX_fn *yuv2planeX,
1205 yuv2interleavedX_fn *yuv2nv12cX,
1206 yuv2packed1_fn *yuv2packed1,
1207 yuv2packed2_fn *yuv2packed2,
1208 yuv2packedX_fn *yuv2packedX)
1210 enum PixelFormat dstFormat = c->dstFormat;
1212 if (is16BPS(dstFormat)) {
1213 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
1214 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
1215 } else if (is9_OR_10BPS(dstFormat)) {
1216 if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
1217 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
1218 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
1220 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
1221 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
1224 *yuv2plane1 = yuv2plane1_8_c;
1225 *yuv2planeX = yuv2planeX_8_c;
1226 if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)
1227 *yuv2nv12cX = yuv2nv12cX_c;
1230 if(c->flags & SWS_FULL_CHR_H_INT) {
1231 switch (dstFormat) {
1234 *yuv2packedX = yuv2rgba32_full_X_c;
1236 #if CONFIG_SWSCALE_ALPHA
1238 *yuv2packedX = yuv2rgba32_full_X_c;
1240 #endif /* CONFIG_SWSCALE_ALPHA */
1242 *yuv2packedX = yuv2rgbx32_full_X_c;
1244 #endif /* !CONFIG_SMALL */
1248 *yuv2packedX = yuv2argb32_full_X_c;
1250 #if CONFIG_SWSCALE_ALPHA
1252 *yuv2packedX = yuv2argb32_full_X_c;
1254 #endif /* CONFIG_SWSCALE_ALPHA */
1256 *yuv2packedX = yuv2xrgb32_full_X_c;
1258 #endif /* !CONFIG_SMALL */
1262 *yuv2packedX = yuv2bgra32_full_X_c;
1264 #if CONFIG_SWSCALE_ALPHA
1266 *yuv2packedX = yuv2bgra32_full_X_c;
1268 #endif /* CONFIG_SWSCALE_ALPHA */
1270 *yuv2packedX = yuv2bgrx32_full_X_c;
1272 #endif /* !CONFIG_SMALL */
1276 *yuv2packedX = yuv2abgr32_full_X_c;
1278 #if CONFIG_SWSCALE_ALPHA
1280 *yuv2packedX = yuv2abgr32_full_X_c;
1282 #endif /* CONFIG_SWSCALE_ALPHA */
1284 *yuv2packedX = yuv2xbgr32_full_X_c;
1286 #endif /* !CONFIG_SMALL */
1289 *yuv2packedX = yuv2rgb24_full_X_c;
1292 *yuv2packedX = yuv2bgr24_full_X_c;
1296 switch (dstFormat) {
1297 case PIX_FMT_RGB48LE:
1298 *yuv2packed1 = yuv2rgb48le_1_c;
1299 *yuv2packed2 = yuv2rgb48le_2_c;
1300 *yuv2packedX = yuv2rgb48le_X_c;
1302 case PIX_FMT_RGB48BE:
1303 *yuv2packed1 = yuv2rgb48be_1_c;
1304 *yuv2packed2 = yuv2rgb48be_2_c;
1305 *yuv2packedX = yuv2rgb48be_X_c;
1307 case PIX_FMT_BGR48LE:
1308 *yuv2packed1 = yuv2bgr48le_1_c;
1309 *yuv2packed2 = yuv2bgr48le_2_c;
1310 *yuv2packedX = yuv2bgr48le_X_c;
1312 case PIX_FMT_BGR48BE:
1313 *yuv2packed1 = yuv2bgr48be_1_c;
1314 *yuv2packed2 = yuv2bgr48be_2_c;
1315 *yuv2packedX = yuv2bgr48be_X_c;
1320 *yuv2packed1 = yuv2rgb32_1_c;
1321 *yuv2packed2 = yuv2rgb32_2_c;
1322 *yuv2packedX = yuv2rgb32_X_c;
1324 #if CONFIG_SWSCALE_ALPHA
1326 *yuv2packed1 = yuv2rgba32_1_c;
1327 *yuv2packed2 = yuv2rgba32_2_c;
1328 *yuv2packedX = yuv2rgba32_X_c;
1330 #endif /* CONFIG_SWSCALE_ALPHA */
1332 *yuv2packed1 = yuv2rgbx32_1_c;
1333 *yuv2packed2 = yuv2rgbx32_2_c;
1334 *yuv2packedX = yuv2rgbx32_X_c;
1336 #endif /* !CONFIG_SMALL */
1338 case PIX_FMT_RGB32_1:
1339 case PIX_FMT_BGR32_1:
1341 *yuv2packed1 = yuv2rgb32_1_1_c;
1342 *yuv2packed2 = yuv2rgb32_1_2_c;
1343 *yuv2packedX = yuv2rgb32_1_X_c;
1345 #if CONFIG_SWSCALE_ALPHA
1347 *yuv2packed1 = yuv2rgba32_1_1_c;
1348 *yuv2packed2 = yuv2rgba32_1_2_c;
1349 *yuv2packedX = yuv2rgba32_1_X_c;
1351 #endif /* CONFIG_SWSCALE_ALPHA */
1353 *yuv2packed1 = yuv2rgbx32_1_1_c;
1354 *yuv2packed2 = yuv2rgbx32_1_2_c;
1355 *yuv2packedX = yuv2rgbx32_1_X_c;
1357 #endif /* !CONFIG_SMALL */
1360 *yuv2packed1 = yuv2rgb24_1_c;
1361 *yuv2packed2 = yuv2rgb24_2_c;
1362 *yuv2packedX = yuv2rgb24_X_c;
1365 *yuv2packed1 = yuv2bgr24_1_c;
1366 *yuv2packed2 = yuv2bgr24_2_c;
1367 *yuv2packedX = yuv2bgr24_X_c;
1369 case PIX_FMT_RGB565LE:
1370 case PIX_FMT_RGB565BE:
1371 case PIX_FMT_BGR565LE:
1372 case PIX_FMT_BGR565BE:
1373 *yuv2packed1 = yuv2rgb16_1_c;
1374 *yuv2packed2 = yuv2rgb16_2_c;
1375 *yuv2packedX = yuv2rgb16_X_c;
1377 case PIX_FMT_RGB555LE:
1378 case PIX_FMT_RGB555BE:
1379 case PIX_FMT_BGR555LE:
1380 case PIX_FMT_BGR555BE:
1381 *yuv2packed1 = yuv2rgb15_1_c;
1382 *yuv2packed2 = yuv2rgb15_2_c;
1383 *yuv2packedX = yuv2rgb15_X_c;
1385 case PIX_FMT_RGB444LE:
1386 case PIX_FMT_RGB444BE:
1387 case PIX_FMT_BGR444LE:
1388 case PIX_FMT_BGR444BE:
1389 *yuv2packed1 = yuv2rgb12_1_c;
1390 *yuv2packed2 = yuv2rgb12_2_c;
1391 *yuv2packedX = yuv2rgb12_X_c;
1395 *yuv2packed1 = yuv2rgb8_1_c;
1396 *yuv2packed2 = yuv2rgb8_2_c;
1397 *yuv2packedX = yuv2rgb8_X_c;
1401 *yuv2packed1 = yuv2rgb4_1_c;
1402 *yuv2packed2 = yuv2rgb4_2_c;
1403 *yuv2packedX = yuv2rgb4_X_c;
1405 case PIX_FMT_RGB4_BYTE:
1406 case PIX_FMT_BGR4_BYTE:
1407 *yuv2packed1 = yuv2rgb4b_1_c;
1408 *yuv2packed2 = yuv2rgb4b_2_c;
1409 *yuv2packedX = yuv2rgb4b_X_c;
1413 switch (dstFormat) {
1414 case PIX_FMT_MONOWHITE:
1415 *yuv2packed1 = yuv2monowhite_1_c;
1416 *yuv2packed2 = yuv2monowhite_2_c;
1417 *yuv2packedX = yuv2monowhite_X_c;
1419 case PIX_FMT_MONOBLACK:
1420 *yuv2packed1 = yuv2monoblack_1_c;
1421 *yuv2packed2 = yuv2monoblack_2_c;
1422 *yuv2packedX = yuv2monoblack_X_c;
1424 case PIX_FMT_YUYV422:
1425 *yuv2packed1 = yuv2yuyv422_1_c;
1426 *yuv2packed2 = yuv2yuyv422_2_c;
1427 *yuv2packedX = yuv2yuyv422_X_c;
1429 case PIX_FMT_UYVY422:
1430 *yuv2packed1 = yuv2uyvy422_1_c;
1431 *yuv2packed2 = yuv2uyvy422_2_c;
1432 *yuv2packedX = yuv2uyvy422_X_c;