2 * Copyright (C) 2001-2012 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/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, static const uint8_t, dither_2x2_4)[2][8]={
40 { 1, 3, 1, 3, 1, 3, 1, 3, },
41 { 2, 0, 2, 0, 2, 0, 2, 0, },
44 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
45 { 6, 2, 6, 2, 6, 2, 6, 2, },
46 { 0, 4, 0, 4, 0, 4, 0, 4, },
49 DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
50 { 8, 4, 11, 7, 8, 4, 11, 7, },
51 { 2, 14, 1, 13, 2, 14, 1, 13, },
52 { 10, 6, 9, 5, 10, 6, 9, 5, },
53 { 0, 12, 3, 15, 0, 12, 3, 15, },
56 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
57 { 17, 9, 23, 15, 16, 8, 22, 14, },
58 { 5, 29, 3, 27, 4, 28, 2, 26, },
59 { 21, 13, 19, 11, 20, 12, 18, 10, },
60 { 0, 24, 6, 30, 1, 25, 7, 31, },
61 { 16, 8, 22, 14, 17, 9, 23, 15, },
62 { 4, 28, 2, 26, 5, 29, 3, 27, },
63 { 20, 12, 18, 10, 21, 13, 19, 11, },
64 { 1, 25, 7, 31, 0, 24, 6, 30, },
67 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
68 { 0, 55, 14, 68, 3, 58, 17, 72, },
69 { 37, 18, 50, 32, 40, 22, 54, 35, },
70 { 9, 64, 5, 59, 13, 67, 8, 63, },
71 { 46, 27, 41, 23, 49, 31, 44, 26, },
72 { 2, 57, 16, 71, 1, 56, 15, 70, },
73 { 39, 21, 52, 34, 38, 19, 51, 33, },
74 { 11, 66, 7, 62, 10, 65, 6, 60, },
75 { 48, 30, 43, 25, 47, 29, 42, 24, },
79 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
80 {117, 62, 158, 103, 113, 58, 155, 100, },
81 { 34, 199, 21, 186, 31, 196, 17, 182, },
82 {144, 89, 131, 76, 141, 86, 127, 72, },
83 { 0, 165, 41, 206, 10, 175, 52, 217, },
84 {110, 55, 151, 96, 120, 65, 162, 107, },
85 { 28, 193, 14, 179, 38, 203, 24, 189, },
86 {138, 83, 124, 69, 148, 93, 134, 79, },
87 { 7, 172, 48, 213, 3, 168, 45, 210, },
90 // tries to correct a gamma of 1.5
91 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
92 { 0, 143, 18, 200, 2, 156, 25, 215, },
93 { 78, 28, 125, 64, 89, 36, 138, 74, },
94 { 10, 180, 3, 161, 16, 195, 8, 175, },
95 {109, 51, 93, 38, 121, 60, 105, 47, },
96 { 1, 152, 23, 210, 0, 147, 20, 205, },
97 { 85, 33, 134, 71, 81, 30, 130, 67, },
98 { 14, 190, 6, 171, 12, 185, 5, 166, },
99 {117, 57, 101, 44, 113, 54, 97, 41, },
102 // tries to correct a gamma of 2.0
103 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
104 { 0, 124, 8, 193, 0, 140, 12, 213, },
105 { 55, 14, 104, 42, 66, 19, 119, 52, },
106 { 3, 168, 1, 145, 6, 187, 3, 162, },
107 { 86, 31, 70, 21, 99, 39, 82, 28, },
108 { 0, 134, 11, 206, 0, 129, 9, 200, },
109 { 62, 17, 114, 48, 58, 16, 109, 45, },
110 { 5, 181, 2, 157, 4, 175, 1, 151, },
111 { 95, 36, 78, 26, 90, 34, 74, 24, },
114 // tries to correct a gamma of 2.5
115 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
116 { 0, 107, 3, 187, 0, 125, 6, 212, },
117 { 39, 7, 86, 28, 49, 11, 102, 36, },
118 { 1, 158, 0, 131, 3, 180, 1, 151, },
119 { 68, 19, 52, 12, 81, 25, 64, 17, },
120 { 0, 119, 5, 203, 0, 113, 4, 195, },
121 { 45, 9, 96, 33, 42, 8, 91, 30, },
122 { 2, 172, 1, 144, 2, 165, 0, 137, },
123 { 77, 23, 60, 15, 72, 21, 56, 14, },
127 #define output_pixel(pos, val, bias, signedness) \
129 AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
131 AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
134 static av_always_inline void
135 yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
136 int big_endian, int output_bits)
140 av_assert0(output_bits == 16);
142 for (i = 0; i < dstW; i++) {
143 int val = src[i] + (1 << (shift - 1));
144 output_pixel(&dest[i], val, 0, uint);
148 static av_always_inline void
149 yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
150 const int32_t **src, uint16_t *dest, int dstW,
151 int big_endian, int output_bits)
155 av_assert0(output_bits == 16);
157 for (i = 0; i < dstW; i++) {
158 int val = 1 << (shift - 1);
161 /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
162 * filters (or anything with negative coeffs, the range can be slightly
163 * wider in both directions. To account for this overflow, we subtract
164 * a constant so it always fits in the signed range (assuming a
165 * reasonable filterSize), and re-add that at the end. */
167 for (j = 0; j < filterSize; j++)
168 val += src[j][i] * filter[j];
170 output_pixel(&dest[i], val, 0x8000, int);
176 #define output_pixel(pos, val) \
178 AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
180 AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
183 static av_always_inline void
184 yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
185 int big_endian, int output_bits)
188 int shift = 15 - output_bits;
190 for (i = 0; i < dstW; i++) {
191 int val = src[i] + (1 << (shift - 1));
192 output_pixel(&dest[i], val);
196 static av_always_inline void
197 yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
198 const int16_t **src, uint16_t *dest, int dstW,
199 int big_endian, int output_bits)
202 int shift = 11 + 16 - output_bits;
204 for (i = 0; i < dstW; i++) {
205 int val = 1 << (shift - 1);
208 for (j = 0; j < filterSize; j++)
209 val += src[j][i] * filter[j];
211 output_pixel(&dest[i], val);
217 #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
218 static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
219 uint8_t *dest, int dstW, \
220 const uint8_t *dither, int offset)\
222 yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
223 (uint16_t *) dest, dstW, is_be, bits); \
225 static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
226 const int16_t **src, uint8_t *dest, int dstW, \
227 const uint8_t *dither, int offset)\
229 yuv2planeX_## template_size ## _c_template(filter, \
230 filterSize, (const typeX_t **) src, \
231 (uint16_t *) dest, dstW, is_be, bits); \
233 yuv2NBPS( 9, BE, 1, 10, int16_t)
234 yuv2NBPS( 9, LE, 0, 10, int16_t)
235 yuv2NBPS(10, BE, 1, 10, int16_t)
236 yuv2NBPS(10, LE, 0, 10, int16_t)
237 yuv2NBPS(16, BE, 1, 16, int32_t)
238 yuv2NBPS(16, LE, 0, 16, int32_t)
240 static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
241 const int16_t **src, uint8_t *dest, int dstW,
242 const uint8_t *dither, int offset)
245 for (i=0; i<dstW; i++) {
246 int val = dither[(i + offset) & 7] << 12;
248 for (j=0; j<filterSize; j++)
249 val += src[j][i] * filter[j];
251 dest[i]= av_clip_uint8(val>>19);
255 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
256 const uint8_t *dither, int offset)
259 for (i=0; i<dstW; i++) {
260 int val = (src[i] + dither[(i + offset) & 7]) >> 7;
261 dest[i]= av_clip_uint8(val);
265 static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
266 const int16_t **chrUSrc, const int16_t **chrVSrc,
267 uint8_t *dest, int chrDstW)
269 enum PixelFormat dstFormat = c->dstFormat;
270 const uint8_t *chrDither = c->chrDither8;
273 if (dstFormat == PIX_FMT_NV12)
274 for (i=0; i<chrDstW; i++) {
275 int u = chrDither[i & 7] << 12;
276 int v = chrDither[(i + 3) & 7] << 12;
278 for (j=0; j<chrFilterSize; j++) {
279 u += chrUSrc[j][i] * chrFilter[j];
280 v += chrVSrc[j][i] * chrFilter[j];
283 dest[2*i]= av_clip_uint8(u>>19);
284 dest[2*i+1]= av_clip_uint8(v>>19);
287 for (i=0; i<chrDstW; i++) {
288 int u = chrDither[i & 7] << 12;
289 int v = chrDither[(i + 3) & 7] << 12;
291 for (j=0; j<chrFilterSize; j++) {
292 u += chrUSrc[j][i] * chrFilter[j];
293 v += chrVSrc[j][i] * chrFilter[j];
296 dest[2*i]= av_clip_uint8(v>>19);
297 dest[2*i+1]= av_clip_uint8(u>>19);
301 #define output_pixel(pos, acc) \
302 if (target == PIX_FMT_MONOBLACK) { \
308 static av_always_inline void
309 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
310 const int16_t **lumSrc, int lumFilterSize,
311 const int16_t *chrFilter, const int16_t **chrUSrc,
312 const int16_t **chrVSrc, int chrFilterSize,
313 const int16_t **alpSrc, uint8_t *dest, int dstW,
314 int y, enum PixelFormat target)
316 const uint8_t * const d128=dither_8x8_220[y&7];
317 uint8_t *g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
321 for (i = 0; i < dstW - 1; i += 2) {
326 for (j = 0; j < lumFilterSize; j++) {
327 Y1 += lumSrc[j][i] * lumFilter[j];
328 Y2 += lumSrc[j][i+1] * lumFilter[j];
332 if ((Y1 | Y2) & 0x100) {
333 Y1 = av_clip_uint8(Y1);
334 Y2 = av_clip_uint8(Y2);
336 acc += acc + g[Y1 + d128[(i + 0) & 7]];
337 acc += acc + g[Y2 + d128[(i + 1) & 7]];
339 output_pixel(*dest++, acc);
344 static av_always_inline void
345 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
346 const int16_t *ubuf[2], const int16_t *vbuf[2],
347 const int16_t *abuf[2], uint8_t *dest, int dstW,
348 int yalpha, int uvalpha, int y,
349 enum PixelFormat target)
351 const int16_t *buf0 = buf[0], *buf1 = buf[1];
352 const uint8_t * const d128 = dither_8x8_220[y & 7];
353 uint8_t *g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
354 int yalpha1 = 4095 - yalpha;
357 for (i = 0; i < dstW - 7; i += 8) {
358 int acc = g[((buf0[i ] * yalpha1 + buf1[i ] * yalpha) >> 19) + d128[0]];
359 acc += acc + g[((buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19) + d128[1]];
360 acc += acc + g[((buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19) + d128[2]];
361 acc += acc + g[((buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19) + d128[3]];
362 acc += acc + g[((buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19) + d128[4]];
363 acc += acc + g[((buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19) + d128[5]];
364 acc += acc + g[((buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19) + d128[6]];
365 acc += acc + g[((buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19) + d128[7]];
366 output_pixel(*dest++, acc);
370 static av_always_inline void
371 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
372 const int16_t *ubuf[2], const int16_t *vbuf[2],
373 const int16_t *abuf0, uint8_t *dest, int dstW,
374 int uvalpha, int y, enum PixelFormat target)
376 const uint8_t * const d128 = dither_8x8_220[y & 7];
377 uint8_t *g = c->table_gU[128 + YUVRGB_TABLE_HEADROOM] + c->table_gV[128 + YUVRGB_TABLE_HEADROOM];
380 for (i = 0; i < dstW - 7; i += 8) {
381 int acc = g[(buf0[i ] >> 7) + d128[0]];
382 acc += acc + g[(buf0[i + 1] >> 7) + d128[1]];
383 acc += acc + g[(buf0[i + 2] >> 7) + d128[2]];
384 acc += acc + g[(buf0[i + 3] >> 7) + d128[3]];
385 acc += acc + g[(buf0[i + 4] >> 7) + d128[4]];
386 acc += acc + g[(buf0[i + 5] >> 7) + d128[5]];
387 acc += acc + g[(buf0[i + 6] >> 7) + d128[6]];
388 acc += acc + g[(buf0[i + 7] >> 7) + d128[7]];
389 output_pixel(*dest++, acc);
395 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
396 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
397 const int16_t **lumSrc, int lumFilterSize, \
398 const int16_t *chrFilter, const int16_t **chrUSrc, \
399 const int16_t **chrVSrc, int chrFilterSize, \
400 const int16_t **alpSrc, uint8_t *dest, int dstW, \
403 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
404 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
405 alpSrc, dest, dstW, y, fmt); \
408 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
409 const int16_t *ubuf[2], const int16_t *vbuf[2], \
410 const int16_t *abuf[2], uint8_t *dest, int dstW, \
411 int yalpha, int uvalpha, int y) \
413 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
414 dest, dstW, yalpha, uvalpha, y, fmt); \
417 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
418 const int16_t *ubuf[2], const int16_t *vbuf[2], \
419 const int16_t *abuf0, uint8_t *dest, int dstW, \
420 int uvalpha, int y) \
422 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
423 abuf0, dest, dstW, uvalpha, \
427 YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE)
428 YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK)
430 #define output_pixels(pos, Y1, U, Y2, V) \
431 if (target == PIX_FMT_YUYV422) { \
432 dest[pos + 0] = Y1; \
434 dest[pos + 2] = Y2; \
438 dest[pos + 1] = Y1; \
440 dest[pos + 3] = Y2; \
443 static av_always_inline void
444 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
445 const int16_t **lumSrc, int lumFilterSize,
446 const int16_t *chrFilter, const int16_t **chrUSrc,
447 const int16_t **chrVSrc, int chrFilterSize,
448 const int16_t **alpSrc, uint8_t *dest, int dstW,
449 int y, enum PixelFormat target)
453 for (i = 0; i < (dstW >> 1); i++) {
460 for (j = 0; j < lumFilterSize; j++) {
461 Y1 += lumSrc[j][i * 2] * lumFilter[j];
462 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
464 for (j = 0; j < chrFilterSize; j++) {
465 U += chrUSrc[j][i] * chrFilter[j];
466 V += chrVSrc[j][i] * chrFilter[j];
472 if ((Y1 | Y2 | U | V) & 0x100) {
473 Y1 = av_clip_uint8(Y1);
474 Y2 = av_clip_uint8(Y2);
475 U = av_clip_uint8(U);
476 V = av_clip_uint8(V);
478 output_pixels(4*i, Y1, U, Y2, V);
482 static av_always_inline void
483 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
484 const int16_t *ubuf[2], const int16_t *vbuf[2],
485 const int16_t *abuf[2], uint8_t *dest, int dstW,
486 int yalpha, int uvalpha, int y,
487 enum PixelFormat target)
489 const int16_t *buf0 = buf[0], *buf1 = buf[1],
490 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
491 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
492 int yalpha1 = 4095 - yalpha;
493 int uvalpha1 = 4095 - uvalpha;
496 for (i = 0; i < (dstW >> 1); i++) {
497 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
498 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
499 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
500 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
502 output_pixels(i * 4, Y1, U, Y2, V);
506 static av_always_inline void
507 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
508 const int16_t *ubuf[2], const int16_t *vbuf[2],
509 const int16_t *abuf0, uint8_t *dest, int dstW,
510 int uvalpha, int y, enum PixelFormat target)
512 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
515 if (uvalpha < 2048) {
516 for (i = 0; i < (dstW >> 1); i++) {
517 int Y1 = buf0[i * 2] >> 7;
518 int Y2 = buf0[i * 2 + 1] >> 7;
519 int U = ubuf0[i] >> 7;
520 int V = vbuf0[i] >> 7;
522 output_pixels(i * 4, Y1, U, Y2, V);
525 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
526 for (i = 0; i < (dstW >> 1); i++) {
527 int Y1 = buf0[i * 2] >> 7;
528 int Y2 = buf0[i * 2 + 1] >> 7;
529 int U = (ubuf0[i] + ubuf1[i]) >> 8;
530 int V = (vbuf0[i] + vbuf1[i]) >> 8;
532 output_pixels(i * 4, Y1, U, Y2, V);
539 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422)
540 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422)
542 #define R_B ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? R : B)
543 #define B_R ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? B : R)
544 #define output_pixel(pos, val) \
545 if (isBE(target)) { \
551 static av_always_inline void
552 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
553 const int32_t **lumSrc, int lumFilterSize,
554 const int16_t *chrFilter, const int32_t **chrUSrc,
555 const int32_t **chrVSrc, int chrFilterSize,
556 const int32_t **alpSrc, uint16_t *dest, int dstW,
557 int y, enum PixelFormat target)
561 for (i = 0; i < (dstW >> 1); i++) {
563 int Y1 = -0x40000000;
564 int Y2 = -0x40000000;
565 int U = -128 << 23; // 19
569 for (j = 0; j < lumFilterSize; j++) {
570 Y1 += lumSrc[j][i * 2] * lumFilter[j];
571 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
573 for (j = 0; j < chrFilterSize; j++) {
574 U += chrUSrc[j][i] * chrFilter[j];
575 V += chrVSrc[j][i] * chrFilter[j];
578 // 8bit: 12+15=27; 16-bit: 12+19=31
586 // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
587 Y1 -= c->yuv2rgb_y_offset;
588 Y2 -= c->yuv2rgb_y_offset;
589 Y1 *= c->yuv2rgb_y_coeff;
590 Y2 *= c->yuv2rgb_y_coeff;
593 // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
595 R = V * c->yuv2rgb_v2r_coeff;
596 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
597 B = U * c->yuv2rgb_u2b_coeff;
599 // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
600 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
601 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
602 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
603 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
604 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
605 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
610 static av_always_inline void
611 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
612 const int32_t *ubuf[2], const int32_t *vbuf[2],
613 const int32_t *abuf[2], uint16_t *dest, int dstW,
614 int yalpha, int uvalpha, int y,
615 enum PixelFormat target)
617 const int32_t *buf0 = buf[0], *buf1 = buf[1],
618 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
619 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
620 int yalpha1 = 4095 - yalpha;
621 int uvalpha1 = 4095 - uvalpha;
624 for (i = 0; i < (dstW >> 1); i++) {
625 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
626 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
627 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
628 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
631 Y1 -= c->yuv2rgb_y_offset;
632 Y2 -= c->yuv2rgb_y_offset;
633 Y1 *= c->yuv2rgb_y_coeff;
634 Y2 *= c->yuv2rgb_y_coeff;
638 R = V * c->yuv2rgb_v2r_coeff;
639 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
640 B = U * c->yuv2rgb_u2b_coeff;
642 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
643 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
644 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
645 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
646 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
647 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
652 static av_always_inline void
653 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
654 const int32_t *ubuf[2], const int32_t *vbuf[2],
655 const int32_t *abuf0, uint16_t *dest, int dstW,
656 int uvalpha, int y, enum PixelFormat target)
658 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
661 if (uvalpha < 2048) {
662 for (i = 0; i < (dstW >> 1); i++) {
663 int Y1 = (buf0[i * 2] ) >> 2;
664 int Y2 = (buf0[i * 2 + 1]) >> 2;
665 int U = (ubuf0[i] + (-128 << 11)) >> 2;
666 int V = (vbuf0[i] + (-128 << 11)) >> 2;
669 Y1 -= c->yuv2rgb_y_offset;
670 Y2 -= c->yuv2rgb_y_offset;
671 Y1 *= c->yuv2rgb_y_coeff;
672 Y2 *= c->yuv2rgb_y_coeff;
676 R = V * c->yuv2rgb_v2r_coeff;
677 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
678 B = U * c->yuv2rgb_u2b_coeff;
680 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
681 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
682 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
683 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
684 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
685 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
689 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
690 for (i = 0; i < (dstW >> 1); i++) {
691 int Y1 = (buf0[i * 2] ) >> 2;
692 int Y2 = (buf0[i * 2 + 1]) >> 2;
693 int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
694 int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
697 Y1 -= c->yuv2rgb_y_offset;
698 Y2 -= c->yuv2rgb_y_offset;
699 Y1 *= c->yuv2rgb_y_coeff;
700 Y2 *= c->yuv2rgb_y_coeff;
704 R = V * c->yuv2rgb_v2r_coeff;
705 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
706 B = U * c->yuv2rgb_u2b_coeff;
708 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
709 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
710 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
711 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
712 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
713 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
723 #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
724 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
725 const int16_t **_lumSrc, int lumFilterSize, \
726 const int16_t *chrFilter, const int16_t **_chrUSrc, \
727 const int16_t **_chrVSrc, int chrFilterSize, \
728 const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
731 const int32_t **lumSrc = (const int32_t **) _lumSrc, \
732 **chrUSrc = (const int32_t **) _chrUSrc, \
733 **chrVSrc = (const int32_t **) _chrVSrc, \
734 **alpSrc = (const int32_t **) _alpSrc; \
735 uint16_t *dest = (uint16_t *) _dest; \
736 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
737 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
738 alpSrc, dest, dstW, y, fmt); \
741 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
742 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
743 const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
744 int yalpha, int uvalpha, int y) \
746 const int32_t **buf = (const int32_t **) _buf, \
747 **ubuf = (const int32_t **) _ubuf, \
748 **vbuf = (const int32_t **) _vbuf, \
749 **abuf = (const int32_t **) _abuf; \
750 uint16_t *dest = (uint16_t *) _dest; \
751 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
752 dest, dstW, yalpha, uvalpha, y, fmt); \
755 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
756 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
757 const int16_t *_abuf0, uint8_t *_dest, int dstW, \
758 int uvalpha, int y) \
760 const int32_t *buf0 = (const int32_t *) _buf0, \
761 **ubuf = (const int32_t **) _ubuf, \
762 **vbuf = (const int32_t **) _vbuf, \
763 *abuf0 = (const int32_t *) _abuf0; \
764 uint16_t *dest = (uint16_t *) _dest; \
765 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
766 dstW, uvalpha, y, fmt); \
769 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, PIX_FMT_RGB48BE)
770 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, PIX_FMT_RGB48LE)
771 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, PIX_FMT_BGR48BE)
772 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, PIX_FMT_BGR48LE)
775 * Write out 2 RGB pixels in the target pixel format. This function takes a
776 * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
777 * things like endianness conversion and shifting. The caller takes care of
778 * setting the correct offset in these tables from the chroma (U/V) values.
779 * This function then uses the luminance (Y1/Y2) values to write out the
780 * correct RGB values into the destination buffer.
782 static av_always_inline void
783 yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
784 unsigned A1, unsigned A2,
785 const void *_r, const void *_g, const void *_b, int y,
786 enum PixelFormat target, int hasAlpha)
788 if (target == PIX_FMT_ARGB || target == PIX_FMT_RGBA ||
789 target == PIX_FMT_ABGR || target == PIX_FMT_BGRA) {
790 uint32_t *dest = (uint32_t *) _dest;
791 const uint32_t *r = (const uint32_t *) _r;
792 const uint32_t *g = (const uint32_t *) _g;
793 const uint32_t *b = (const uint32_t *) _b;
796 int sh = hasAlpha ? ((target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24) : 0;
798 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
799 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
802 int sh = (target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24;
804 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
805 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
807 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
808 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
811 } else if (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) {
812 uint8_t *dest = (uint8_t *) _dest;
813 const uint8_t *r = (const uint8_t *) _r;
814 const uint8_t *g = (const uint8_t *) _g;
815 const uint8_t *b = (const uint8_t *) _b;
817 #define r_b ((target == PIX_FMT_RGB24) ? r : b)
818 #define b_r ((target == PIX_FMT_RGB24) ? b : r)
820 dest[i * 6 + 0] = r_b[Y1];
821 dest[i * 6 + 1] = g[Y1];
822 dest[i * 6 + 2] = b_r[Y1];
823 dest[i * 6 + 3] = r_b[Y2];
824 dest[i * 6 + 4] = g[Y2];
825 dest[i * 6 + 5] = b_r[Y2];
828 } else if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565 ||
829 target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555 ||
830 target == PIX_FMT_RGB444 || target == PIX_FMT_BGR444) {
831 uint16_t *dest = (uint16_t *) _dest;
832 const uint16_t *r = (const uint16_t *) _r;
833 const uint16_t *g = (const uint16_t *) _g;
834 const uint16_t *b = (const uint16_t *) _b;
835 int dr1, dg1, db1, dr2, dg2, db2;
837 if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565) {
838 dr1 = dither_2x2_8[ y & 1 ][0];
839 dg1 = dither_2x2_4[ y & 1 ][0];
840 db1 = dither_2x2_8[(y & 1) ^ 1][0];
841 dr2 = dither_2x2_8[ y & 1 ][1];
842 dg2 = dither_2x2_4[ y & 1 ][1];
843 db2 = dither_2x2_8[(y & 1) ^ 1][1];
844 } else if (target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555) {
845 dr1 = dither_2x2_8[ y & 1 ][0];
846 dg1 = dither_2x2_8[ y & 1 ][1];
847 db1 = dither_2x2_8[(y & 1) ^ 1][0];
848 dr2 = dither_2x2_8[ y & 1 ][1];
849 dg2 = dither_2x2_8[ y & 1 ][0];
850 db2 = dither_2x2_8[(y & 1) ^ 1][1];
852 dr1 = dither_4x4_16[ y & 3 ][0];
853 dg1 = dither_4x4_16[ y & 3 ][1];
854 db1 = dither_4x4_16[(y & 3) ^ 3][0];
855 dr2 = dither_4x4_16[ y & 3 ][1];
856 dg2 = dither_4x4_16[ y & 3 ][0];
857 db2 = dither_4x4_16[(y & 3) ^ 3][1];
860 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
861 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
862 } else /* 8/4-bit */ {
863 uint8_t *dest = (uint8_t *) _dest;
864 const uint8_t *r = (const uint8_t *) _r;
865 const uint8_t *g = (const uint8_t *) _g;
866 const uint8_t *b = (const uint8_t *) _b;
867 int dr1, dg1, db1, dr2, dg2, db2;
869 if (target == PIX_FMT_RGB8 || target == PIX_FMT_BGR8) {
870 const uint8_t * const d64 = dither_8x8_73[y & 7];
871 const uint8_t * const d32 = dither_8x8_32[y & 7];
872 dr1 = dg1 = d32[(i * 2 + 0) & 7];
873 db1 = d64[(i * 2 + 0) & 7];
874 dr2 = dg2 = d32[(i * 2 + 1) & 7];
875 db2 = d64[(i * 2 + 1) & 7];
877 const uint8_t * const d64 = dither_8x8_73 [y & 7];
878 const uint8_t * const d128 = dither_8x8_220[y & 7];
879 dr1 = db1 = d128[(i * 2 + 0) & 7];
880 dg1 = d64[(i * 2 + 0) & 7];
881 dr2 = db2 = d128[(i * 2 + 1) & 7];
882 dg2 = d64[(i * 2 + 1) & 7];
885 if (target == PIX_FMT_RGB4 || target == PIX_FMT_BGR4) {
886 dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
887 ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
889 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
890 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
895 static av_always_inline void
896 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
897 const int16_t **lumSrc, int lumFilterSize,
898 const int16_t *chrFilter, const int16_t **chrUSrc,
899 const int16_t **chrVSrc, int chrFilterSize,
900 const int16_t **alpSrc, uint8_t *dest, int dstW,
901 int y, enum PixelFormat target, int hasAlpha)
905 for (i = 0; i < (dstW >> 1); i++) {
911 const void *r, *g, *b;
913 for (j = 0; j < lumFilterSize; j++) {
914 Y1 += lumSrc[j][i * 2] * lumFilter[j];
915 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
917 for (j = 0; j < chrFilterSize; j++) {
918 U += chrUSrc[j][i] * chrFilter[j];
919 V += chrVSrc[j][i] * chrFilter[j];
928 for (j = 0; j < lumFilterSize; j++) {
929 A1 += alpSrc[j][i * 2 ] * lumFilter[j];
930 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
934 if ((A1 | A2) & 0x100) {
935 A1 = av_clip_uint8(A1);
936 A2 = av_clip_uint8(A2);
940 r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
941 g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
942 b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
944 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
945 r, g, b, y, target, hasAlpha);
949 static av_always_inline void
950 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
951 const int16_t *ubuf[2], const int16_t *vbuf[2],
952 const int16_t *abuf[2], uint8_t *dest, int dstW,
953 int yalpha, int uvalpha, int y,
954 enum PixelFormat target, int hasAlpha)
956 const int16_t *buf0 = buf[0], *buf1 = buf[1],
957 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
958 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
959 *abuf0 = hasAlpha ? abuf[0] : NULL,
960 *abuf1 = hasAlpha ? abuf[1] : NULL;
961 int yalpha1 = 4095 - yalpha;
962 int uvalpha1 = 4095 - uvalpha;
965 for (i = 0; i < (dstW >> 1); i++) {
966 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
967 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
968 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
969 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
971 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
972 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
973 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
976 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
977 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
980 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
981 r, g, b, y, target, hasAlpha);
985 static av_always_inline void
986 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
987 const int16_t *ubuf[2], const int16_t *vbuf[2],
988 const int16_t *abuf0, uint8_t *dest, int dstW,
989 int uvalpha, int y, enum PixelFormat target,
992 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
995 if (uvalpha < 2048) {
996 for (i = 0; i < (dstW >> 1); i++) {
997 int Y1 = buf0[i * 2] >> 7;
998 int Y2 = buf0[i * 2 + 1] >> 7;
999 int U = ubuf0[i] >> 7;
1000 int V = vbuf0[i] >> 7;
1002 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1003 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1004 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1007 A1 = abuf0[i * 2 ] >> 7;
1008 A2 = abuf0[i * 2 + 1] >> 7;
1011 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1012 r, g, b, y, target, hasAlpha);
1015 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1016 for (i = 0; i < (dstW >> 1); i++) {
1017 int Y1 = buf0[i * 2] >> 7;
1018 int Y2 = buf0[i * 2 + 1] >> 7;
1019 int U = (ubuf0[i] + ubuf1[i]) >> 8;
1020 int V = (vbuf0[i] + vbuf1[i]) >> 8;
1022 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1023 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1024 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1027 A1 = abuf0[i * 2 ] >> 7;
1028 A2 = abuf0[i * 2 + 1] >> 7;
1031 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1032 r, g, b, y, target, hasAlpha);
1037 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1038 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1039 const int16_t **lumSrc, int lumFilterSize, \
1040 const int16_t *chrFilter, const int16_t **chrUSrc, \
1041 const int16_t **chrVSrc, int chrFilterSize, \
1042 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1045 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1046 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1047 alpSrc, dest, dstW, y, fmt, hasAlpha); \
1049 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1050 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1051 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1052 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1053 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1054 int yalpha, int uvalpha, int y) \
1056 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1057 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1060 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1061 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1062 const int16_t *abuf0, uint8_t *dest, int dstW, \
1063 int uvalpha, int y) \
1065 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1066 dstW, uvalpha, y, fmt, hasAlpha); \
1070 YUV2RGBWRAPPER(yuv2rgb,, 32_1, PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1071 YUV2RGBWRAPPER(yuv2rgb,, 32, PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1073 #if CONFIG_SWSCALE_ALPHA
1074 YUV2RGBWRAPPER(yuv2rgb,, a32_1, PIX_FMT_RGB32_1, 1)
1075 YUV2RGBWRAPPER(yuv2rgb,, a32, PIX_FMT_RGB32, 1)
1077 YUV2RGBWRAPPER(yuv2rgb,, x32_1, PIX_FMT_RGB32_1, 0)
1078 YUV2RGBWRAPPER(yuv2rgb,, x32, PIX_FMT_RGB32, 0)
1080 YUV2RGBWRAPPER(yuv2, rgb, rgb24, PIX_FMT_RGB24, 0)
1081 YUV2RGBWRAPPER(yuv2, rgb, bgr24, PIX_FMT_BGR24, 0)
1082 YUV2RGBWRAPPER(yuv2rgb,, 16, PIX_FMT_RGB565, 0)
1083 YUV2RGBWRAPPER(yuv2rgb,, 15, PIX_FMT_RGB555, 0)
1084 YUV2RGBWRAPPER(yuv2rgb,, 12, PIX_FMT_RGB444, 0)
1085 YUV2RGBWRAPPER(yuv2rgb,, 8, PIX_FMT_RGB8, 0)
1086 YUV2RGBWRAPPER(yuv2rgb,, 4, PIX_FMT_RGB4, 0)
1087 YUV2RGBWRAPPER(yuv2rgb,, 4b, PIX_FMT_RGB4_BYTE, 0)
1089 static av_always_inline void
1090 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1091 const int16_t **lumSrc, int lumFilterSize,
1092 const int16_t *chrFilter, const int16_t **chrUSrc,
1093 const int16_t **chrVSrc, int chrFilterSize,
1094 const int16_t **alpSrc, uint8_t *dest,
1095 int dstW, int y, enum PixelFormat target, int hasAlpha)
1098 int step = (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) ? 3 : 4;
1100 for (i = 0; i < dstW; i++) {
1103 int U = (1<<9)-(128 << 19);
1104 int V = (1<<9)-(128 << 19);
1107 for (j = 0; j < lumFilterSize; j++) {
1108 Y += lumSrc[j][i] * lumFilter[j];
1110 for (j = 0; j < chrFilterSize; j++) {
1111 U += chrUSrc[j][i] * chrFilter[j];
1112 V += chrVSrc[j][i] * chrFilter[j];
1119 for (j = 0; j < lumFilterSize; j++) {
1120 A += alpSrc[j][i] * lumFilter[j];
1124 A = av_clip_uint8(A);
1126 Y -= c->yuv2rgb_y_offset;
1127 Y *= c->yuv2rgb_y_coeff;
1129 R = Y + V*c->yuv2rgb_v2r_coeff;
1130 G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1131 B = Y + U*c->yuv2rgb_u2b_coeff;
1132 if ((R | G | B) & 0xC0000000) {
1133 R = av_clip_uintp2(R, 30);
1134 G = av_clip_uintp2(G, 30);
1135 B = av_clip_uintp2(B, 30);
1140 dest[0] = hasAlpha ? A : 255;
1154 dest[3] = hasAlpha ? A : 255;
1157 dest[0] = hasAlpha ? A : 255;
1171 dest[3] = hasAlpha ? A : 255;
1179 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1180 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1181 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1182 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1184 #if CONFIG_SWSCALE_ALPHA
1185 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, 1)
1186 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, 1)
1187 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, 1)
1188 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, 1)
1190 YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, PIX_FMT_BGRA, 0)
1191 YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, PIX_FMT_ABGR, 0)
1192 YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, PIX_FMT_RGBA, 0)
1193 YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, PIX_FMT_ARGB, 0)
1195 YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, PIX_FMT_BGR24, 0)
1196 YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, PIX_FMT_RGB24, 0)
1198 void ff_sws_init_output_funcs(SwsContext *c,
1199 yuv2planar1_fn *yuv2plane1,
1200 yuv2planarX_fn *yuv2planeX,
1201 yuv2interleavedX_fn *yuv2nv12cX,
1202 yuv2packed1_fn *yuv2packed1,
1203 yuv2packed2_fn *yuv2packed2,
1204 yuv2packedX_fn *yuv2packedX)
1206 enum PixelFormat dstFormat = c->dstFormat;
1208 if (is16BPS(dstFormat)) {
1209 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
1210 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
1211 } else if (is9_OR_10BPS(dstFormat)) {
1212 if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
1213 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
1214 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
1216 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
1217 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
1220 *yuv2plane1 = yuv2plane1_8_c;
1221 *yuv2planeX = yuv2planeX_8_c;
1222 if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)
1223 *yuv2nv12cX = yuv2nv12cX_c;
1226 if(c->flags & SWS_FULL_CHR_H_INT) {
1227 switch (dstFormat) {
1230 *yuv2packedX = yuv2rgba32_full_X_c;
1232 #if CONFIG_SWSCALE_ALPHA
1234 *yuv2packedX = yuv2rgba32_full_X_c;
1236 #endif /* CONFIG_SWSCALE_ALPHA */
1238 *yuv2packedX = yuv2rgbx32_full_X_c;
1240 #endif /* !CONFIG_SMALL */
1244 *yuv2packedX = yuv2argb32_full_X_c;
1246 #if CONFIG_SWSCALE_ALPHA
1248 *yuv2packedX = yuv2argb32_full_X_c;
1250 #endif /* CONFIG_SWSCALE_ALPHA */
1252 *yuv2packedX = yuv2xrgb32_full_X_c;
1254 #endif /* !CONFIG_SMALL */
1258 *yuv2packedX = yuv2bgra32_full_X_c;
1260 #if CONFIG_SWSCALE_ALPHA
1262 *yuv2packedX = yuv2bgra32_full_X_c;
1264 #endif /* CONFIG_SWSCALE_ALPHA */
1266 *yuv2packedX = yuv2bgrx32_full_X_c;
1268 #endif /* !CONFIG_SMALL */
1272 *yuv2packedX = yuv2abgr32_full_X_c;
1274 #if CONFIG_SWSCALE_ALPHA
1276 *yuv2packedX = yuv2abgr32_full_X_c;
1278 #endif /* CONFIG_SWSCALE_ALPHA */
1280 *yuv2packedX = yuv2xbgr32_full_X_c;
1282 #endif /* !CONFIG_SMALL */
1285 *yuv2packedX = yuv2rgb24_full_X_c;
1288 *yuv2packedX = yuv2bgr24_full_X_c;
1295 switch (dstFormat) {
1296 case PIX_FMT_RGB48LE:
1297 *yuv2packed1 = yuv2rgb48le_1_c;
1298 *yuv2packed2 = yuv2rgb48le_2_c;
1299 *yuv2packedX = yuv2rgb48le_X_c;
1301 case PIX_FMT_RGB48BE:
1302 *yuv2packed1 = yuv2rgb48be_1_c;
1303 *yuv2packed2 = yuv2rgb48be_2_c;
1304 *yuv2packedX = yuv2rgb48be_X_c;
1306 case PIX_FMT_BGR48LE:
1307 *yuv2packed1 = yuv2bgr48le_1_c;
1308 *yuv2packed2 = yuv2bgr48le_2_c;
1309 *yuv2packedX = yuv2bgr48le_X_c;
1311 case PIX_FMT_BGR48BE:
1312 *yuv2packed1 = yuv2bgr48be_1_c;
1313 *yuv2packed2 = yuv2bgr48be_2_c;
1314 *yuv2packedX = yuv2bgr48be_X_c;
1319 *yuv2packed1 = yuv2rgb32_1_c;
1320 *yuv2packed2 = yuv2rgb32_2_c;
1321 *yuv2packedX = yuv2rgb32_X_c;
1323 #if CONFIG_SWSCALE_ALPHA
1325 *yuv2packed1 = yuv2rgba32_1_c;
1326 *yuv2packed2 = yuv2rgba32_2_c;
1327 *yuv2packedX = yuv2rgba32_X_c;
1329 #endif /* CONFIG_SWSCALE_ALPHA */
1331 *yuv2packed1 = yuv2rgbx32_1_c;
1332 *yuv2packed2 = yuv2rgbx32_2_c;
1333 *yuv2packedX = yuv2rgbx32_X_c;
1335 #endif /* !CONFIG_SMALL */
1337 case PIX_FMT_RGB32_1:
1338 case PIX_FMT_BGR32_1:
1340 *yuv2packed1 = yuv2rgb32_1_1_c;
1341 *yuv2packed2 = yuv2rgb32_1_2_c;
1342 *yuv2packedX = yuv2rgb32_1_X_c;
1344 #if CONFIG_SWSCALE_ALPHA
1346 *yuv2packed1 = yuv2rgba32_1_1_c;
1347 *yuv2packed2 = yuv2rgba32_1_2_c;
1348 *yuv2packedX = yuv2rgba32_1_X_c;
1350 #endif /* CONFIG_SWSCALE_ALPHA */
1352 *yuv2packed1 = yuv2rgbx32_1_1_c;
1353 *yuv2packed2 = yuv2rgbx32_1_2_c;
1354 *yuv2packedX = yuv2rgbx32_1_X_c;
1356 #endif /* !CONFIG_SMALL */
1359 *yuv2packed1 = yuv2rgb24_1_c;
1360 *yuv2packed2 = yuv2rgb24_2_c;
1361 *yuv2packedX = yuv2rgb24_X_c;
1364 *yuv2packed1 = yuv2bgr24_1_c;
1365 *yuv2packed2 = yuv2bgr24_2_c;
1366 *yuv2packedX = yuv2bgr24_X_c;
1368 case PIX_FMT_RGB565LE:
1369 case PIX_FMT_RGB565BE:
1370 case PIX_FMT_BGR565LE:
1371 case PIX_FMT_BGR565BE:
1372 *yuv2packed1 = yuv2rgb16_1_c;
1373 *yuv2packed2 = yuv2rgb16_2_c;
1374 *yuv2packedX = yuv2rgb16_X_c;
1376 case PIX_FMT_RGB555LE:
1377 case PIX_FMT_RGB555BE:
1378 case PIX_FMT_BGR555LE:
1379 case PIX_FMT_BGR555BE:
1380 *yuv2packed1 = yuv2rgb15_1_c;
1381 *yuv2packed2 = yuv2rgb15_2_c;
1382 *yuv2packedX = yuv2rgb15_X_c;
1384 case PIX_FMT_RGB444LE:
1385 case PIX_FMT_RGB444BE:
1386 case PIX_FMT_BGR444LE:
1387 case PIX_FMT_BGR444BE:
1388 *yuv2packed1 = yuv2rgb12_1_c;
1389 *yuv2packed2 = yuv2rgb12_2_c;
1390 *yuv2packedX = yuv2rgb12_X_c;
1394 *yuv2packed1 = yuv2rgb8_1_c;
1395 *yuv2packed2 = yuv2rgb8_2_c;
1396 *yuv2packedX = yuv2rgb8_X_c;
1400 *yuv2packed1 = yuv2rgb4_1_c;
1401 *yuv2packed2 = yuv2rgb4_2_c;
1402 *yuv2packedX = yuv2rgb4_X_c;
1404 case PIX_FMT_RGB4_BYTE:
1405 case PIX_FMT_BGR4_BYTE:
1406 *yuv2packed1 = yuv2rgb4b_1_c;
1407 *yuv2packed2 = yuv2rgb4b_2_c;
1408 *yuv2packedX = yuv2rgb4b_X_c;
1412 switch (dstFormat) {
1413 case PIX_FMT_MONOWHITE:
1414 *yuv2packed1 = yuv2monowhite_1_c;
1415 *yuv2packed2 = yuv2monowhite_2_c;
1416 *yuv2packedX = yuv2monowhite_X_c;
1418 case PIX_FMT_MONOBLACK:
1419 *yuv2packed1 = yuv2monoblack_1_c;
1420 *yuv2packed2 = yuv2monoblack_2_c;
1421 *yuv2packedX = yuv2monoblack_X_c;
1423 case PIX_FMT_YUYV422:
1424 *yuv2packed1 = yuv2yuyv422_1_c;
1425 *yuv2packed2 = yuv2yuyv422_2_c;
1426 *yuv2packedX = yuv2yuyv422_X_c;
1428 case PIX_FMT_UYVY422:
1429 *yuv2packed1 = yuv2uyvy422_1_c;
1430 *yuv2packed2 = yuv2uyvy422_2_c;
1431 *yuv2packedX = yuv2uyvy422_X_c;