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 accumulate_bit(acc, val) \
303 acc |= (val) >= (128 + 110)
304 #define output_pixel(pos, acc) \
305 if (target == PIX_FMT_MONOBLACK) { \
311 static av_always_inline void
312 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
313 const int16_t **lumSrc, int lumFilterSize,
314 const int16_t *chrFilter, const int16_t **chrUSrc,
315 const int16_t **chrVSrc, int chrFilterSize,
316 const int16_t **alpSrc, uint8_t *dest, int dstW,
317 int y, enum PixelFormat target)
319 const uint8_t * const d128=dither_8x8_220[y&7];
323 for (i = 0; i < dstW - 1; i += 2) {
328 for (j = 0; j < lumFilterSize; j++) {
329 Y1 += lumSrc[j][i] * lumFilter[j];
330 Y2 += lumSrc[j][i+1] * lumFilter[j];
334 if ((Y1 | Y2) & 0x100) {
335 Y1 = av_clip_uint8(Y1);
336 Y2 = av_clip_uint8(Y2);
338 accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
339 accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
341 output_pixel(*dest++, acc);
346 static av_always_inline void
347 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
348 const int16_t *ubuf[2], const int16_t *vbuf[2],
349 const int16_t *abuf[2], uint8_t *dest, int dstW,
350 int yalpha, int uvalpha, int y,
351 enum PixelFormat target)
353 const int16_t *buf0 = buf[0], *buf1 = buf[1];
354 const uint8_t * const d128 = dither_8x8_220[y & 7];
355 int yalpha1 = 4095 - yalpha;
358 for (i = 0; i < dstW - 7; i += 8) {
361 Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
362 accumulate_bit(acc, Y + d128[0]);
363 Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
364 accumulate_bit(acc, Y + d128[1]);
365 Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
366 accumulate_bit(acc, Y + d128[2]);
367 Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
368 accumulate_bit(acc, Y + d128[3]);
369 Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
370 accumulate_bit(acc, Y + d128[4]);
371 Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
372 accumulate_bit(acc, Y + d128[5]);
373 Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
374 accumulate_bit(acc, Y + d128[6]);
375 Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
376 accumulate_bit(acc, Y + d128[7]);
378 output_pixel(*dest++, acc);
382 static av_always_inline void
383 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
384 const int16_t *ubuf[2], const int16_t *vbuf[2],
385 const int16_t *abuf0, uint8_t *dest, int dstW,
386 int uvalpha, int y, enum PixelFormat target)
388 const uint8_t * const d128 = dither_8x8_220[y & 7];
391 for (i = 0; i < dstW - 7; i += 8) {
394 accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
395 accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
396 accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
397 accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
398 accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
399 accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
400 accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
401 accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
403 output_pixel(*dest++, acc);
408 #undef accumulate_bit
410 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
411 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
412 const int16_t **lumSrc, int lumFilterSize, \
413 const int16_t *chrFilter, const int16_t **chrUSrc, \
414 const int16_t **chrVSrc, int chrFilterSize, \
415 const int16_t **alpSrc, uint8_t *dest, int dstW, \
418 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
419 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
420 alpSrc, dest, dstW, y, fmt); \
423 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
424 const int16_t *ubuf[2], const int16_t *vbuf[2], \
425 const int16_t *abuf[2], uint8_t *dest, int dstW, \
426 int yalpha, int uvalpha, int y) \
428 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
429 dest, dstW, yalpha, uvalpha, y, fmt); \
432 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
433 const int16_t *ubuf[2], const int16_t *vbuf[2], \
434 const int16_t *abuf0, uint8_t *dest, int dstW, \
435 int uvalpha, int y) \
437 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
438 abuf0, dest, dstW, uvalpha, \
442 YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE)
443 YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK)
445 #define output_pixels(pos, Y1, U, Y2, V) \
446 if (target == PIX_FMT_YUYV422) { \
447 dest[pos + 0] = Y1; \
449 dest[pos + 2] = Y2; \
453 dest[pos + 1] = Y1; \
455 dest[pos + 3] = Y2; \
458 static av_always_inline void
459 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
460 const int16_t **lumSrc, int lumFilterSize,
461 const int16_t *chrFilter, const int16_t **chrUSrc,
462 const int16_t **chrVSrc, int chrFilterSize,
463 const int16_t **alpSrc, uint8_t *dest, int dstW,
464 int y, enum PixelFormat target)
468 for (i = 0; i < (dstW >> 1); i++) {
475 for (j = 0; j < lumFilterSize; j++) {
476 Y1 += lumSrc[j][i * 2] * lumFilter[j];
477 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
479 for (j = 0; j < chrFilterSize; j++) {
480 U += chrUSrc[j][i] * chrFilter[j];
481 V += chrVSrc[j][i] * chrFilter[j];
487 if ((Y1 | Y2 | U | V) & 0x100) {
488 Y1 = av_clip_uint8(Y1);
489 Y2 = av_clip_uint8(Y2);
490 U = av_clip_uint8(U);
491 V = av_clip_uint8(V);
493 output_pixels(4*i, Y1, U, Y2, V);
497 static av_always_inline void
498 yuv2422_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 PixelFormat target)
504 const int16_t *buf0 = buf[0], *buf1 = buf[1],
505 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
506 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
507 int yalpha1 = 4095 - yalpha;
508 int uvalpha1 = 4095 - uvalpha;
511 for (i = 0; i < (dstW >> 1); i++) {
512 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
513 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
514 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
515 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
517 if ((Y1 | Y2 | U | V) & 0x100) {
518 Y1 = av_clip_uint8(Y1);
519 Y2 = av_clip_uint8(Y2);
520 U = av_clip_uint8(U);
521 V = av_clip_uint8(V);
524 output_pixels(i * 4, Y1, U, Y2, V);
528 static av_always_inline void
529 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
530 const int16_t *ubuf[2], const int16_t *vbuf[2],
531 const int16_t *abuf0, uint8_t *dest, int dstW,
532 int uvalpha, int y, enum PixelFormat target)
534 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
537 if (uvalpha < 2048) {
538 for (i = 0; i < (dstW >> 1); i++) {
539 int Y1 = (buf0[i * 2 ]+64) >> 7;
540 int Y2 = (buf0[i * 2 + 1]+64) >> 7;
541 int U = (ubuf0[i] +64) >> 7;
542 int V = (vbuf0[i] +64) >> 7;
544 if ((Y1 | Y2 | U | V) & 0x100) {
545 Y1 = av_clip_uint8(Y1);
546 Y2 = av_clip_uint8(Y2);
547 U = av_clip_uint8(U);
548 V = av_clip_uint8(V);
551 output_pixels(i * 4, Y1, U, Y2, V);
554 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
555 for (i = 0; i < (dstW >> 1); i++) {
556 int Y1 = (buf0[i * 2 ] + 64) >> 7;
557 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
558 int U = (ubuf0[i] + ubuf1[i]+128) >> 8;
559 int V = (vbuf0[i] + vbuf1[i]+128) >> 8;
561 if ((Y1 | Y2 | U | V) & 0x100) {
562 Y1 = av_clip_uint8(Y1);
563 Y2 = av_clip_uint8(Y2);
564 U = av_clip_uint8(U);
565 V = av_clip_uint8(V);
568 output_pixels(i * 4, Y1, U, Y2, V);
575 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422)
576 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422)
578 #define R_B ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? R : B)
579 #define B_R ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? B : R)
580 #define output_pixel(pos, val) \
581 if (isBE(target)) { \
587 static av_always_inline void
588 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
589 const int32_t **lumSrc, int lumFilterSize,
590 const int16_t *chrFilter, const int32_t **chrUSrc,
591 const int32_t **chrVSrc, int chrFilterSize,
592 const int32_t **alpSrc, uint16_t *dest, int dstW,
593 int y, enum PixelFormat target)
597 for (i = 0; i < (dstW >> 1); i++) {
599 int Y1 = -0x40000000;
600 int Y2 = -0x40000000;
601 int U = -128 << 23; // 19
605 for (j = 0; j < lumFilterSize; j++) {
606 Y1 += lumSrc[j][i * 2] * lumFilter[j];
607 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
609 for (j = 0; j < chrFilterSize; j++) {
610 U += chrUSrc[j][i] * chrFilter[j];
611 V += chrVSrc[j][i] * chrFilter[j];
614 // 8bit: 12+15=27; 16-bit: 12+19=31
622 // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
623 Y1 -= c->yuv2rgb_y_offset;
624 Y2 -= c->yuv2rgb_y_offset;
625 Y1 *= c->yuv2rgb_y_coeff;
626 Y2 *= c->yuv2rgb_y_coeff;
629 // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
631 R = V * c->yuv2rgb_v2r_coeff;
632 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
633 B = U * c->yuv2rgb_u2b_coeff;
635 // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
636 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
637 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
638 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
639 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
640 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
641 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
646 static av_always_inline void
647 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
648 const int32_t *ubuf[2], const int32_t *vbuf[2],
649 const int32_t *abuf[2], uint16_t *dest, int dstW,
650 int yalpha, int uvalpha, int y,
651 enum PixelFormat target)
653 const int32_t *buf0 = buf[0], *buf1 = buf[1],
654 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
655 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
656 int yalpha1 = 4095 - yalpha;
657 int uvalpha1 = 4095 - uvalpha;
660 for (i = 0; i < (dstW >> 1); i++) {
661 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
662 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
663 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
664 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
667 Y1 -= c->yuv2rgb_y_offset;
668 Y2 -= c->yuv2rgb_y_offset;
669 Y1 *= c->yuv2rgb_y_coeff;
670 Y2 *= c->yuv2rgb_y_coeff;
674 R = V * c->yuv2rgb_v2r_coeff;
675 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
676 B = U * c->yuv2rgb_u2b_coeff;
678 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
679 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
680 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
681 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
682 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
683 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
688 static av_always_inline void
689 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
690 const int32_t *ubuf[2], const int32_t *vbuf[2],
691 const int32_t *abuf0, uint16_t *dest, int dstW,
692 int uvalpha, int y, enum PixelFormat target)
694 const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
697 if (uvalpha < 2048) {
698 for (i = 0; i < (dstW >> 1); i++) {
699 int Y1 = (buf0[i * 2] ) >> 2;
700 int Y2 = (buf0[i * 2 + 1]) >> 2;
701 int U = (ubuf0[i] + (-128 << 11)) >> 2;
702 int V = (vbuf0[i] + (-128 << 11)) >> 2;
705 Y1 -= c->yuv2rgb_y_offset;
706 Y2 -= c->yuv2rgb_y_offset;
707 Y1 *= c->yuv2rgb_y_coeff;
708 Y2 *= c->yuv2rgb_y_coeff;
712 R = V * c->yuv2rgb_v2r_coeff;
713 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
714 B = U * c->yuv2rgb_u2b_coeff;
716 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
717 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
718 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
719 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
720 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
721 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
725 const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
726 for (i = 0; i < (dstW >> 1); i++) {
727 int Y1 = (buf0[i * 2] ) >> 2;
728 int Y2 = (buf0[i * 2 + 1]) >> 2;
729 int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
730 int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
733 Y1 -= c->yuv2rgb_y_offset;
734 Y2 -= c->yuv2rgb_y_offset;
735 Y1 *= c->yuv2rgb_y_coeff;
736 Y2 *= c->yuv2rgb_y_coeff;
740 R = V * c->yuv2rgb_v2r_coeff;
741 G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
742 B = U * c->yuv2rgb_u2b_coeff;
744 output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
745 output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
746 output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
747 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
748 output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
749 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
759 #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
760 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
761 const int16_t **_lumSrc, int lumFilterSize, \
762 const int16_t *chrFilter, const int16_t **_chrUSrc, \
763 const int16_t **_chrVSrc, int chrFilterSize, \
764 const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
767 const int32_t **lumSrc = (const int32_t **) _lumSrc, \
768 **chrUSrc = (const int32_t **) _chrUSrc, \
769 **chrVSrc = (const int32_t **) _chrVSrc, \
770 **alpSrc = (const int32_t **) _alpSrc; \
771 uint16_t *dest = (uint16_t *) _dest; \
772 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
773 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
774 alpSrc, dest, dstW, y, fmt); \
777 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
778 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
779 const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
780 int yalpha, int uvalpha, int y) \
782 const int32_t **buf = (const int32_t **) _buf, \
783 **ubuf = (const int32_t **) _ubuf, \
784 **vbuf = (const int32_t **) _vbuf, \
785 **abuf = (const int32_t **) _abuf; \
786 uint16_t *dest = (uint16_t *) _dest; \
787 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
788 dest, dstW, yalpha, uvalpha, y, fmt); \
791 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
792 const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
793 const int16_t *_abuf0, uint8_t *_dest, int dstW, \
794 int uvalpha, int y) \
796 const int32_t *buf0 = (const int32_t *) _buf0, \
797 **ubuf = (const int32_t **) _ubuf, \
798 **vbuf = (const int32_t **) _vbuf, \
799 *abuf0 = (const int32_t *) _abuf0; \
800 uint16_t *dest = (uint16_t *) _dest; \
801 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
802 dstW, uvalpha, y, fmt); \
805 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, PIX_FMT_RGB48BE)
806 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, PIX_FMT_RGB48LE)
807 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, PIX_FMT_BGR48BE)
808 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, PIX_FMT_BGR48LE)
811 * Write out 2 RGB pixels in the target pixel format. This function takes a
812 * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
813 * things like endianness conversion and shifting. The caller takes care of
814 * setting the correct offset in these tables from the chroma (U/V) values.
815 * This function then uses the luminance (Y1/Y2) values to write out the
816 * correct RGB values into the destination buffer.
818 static av_always_inline void
819 yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
820 unsigned A1, unsigned A2,
821 const void *_r, const void *_g, const void *_b, int y,
822 enum PixelFormat target, int hasAlpha)
824 if (target == PIX_FMT_ARGB || target == PIX_FMT_RGBA ||
825 target == PIX_FMT_ABGR || target == PIX_FMT_BGRA) {
826 uint32_t *dest = (uint32_t *) _dest;
827 const uint32_t *r = (const uint32_t *) _r;
828 const uint32_t *g = (const uint32_t *) _g;
829 const uint32_t *b = (const uint32_t *) _b;
832 int sh = hasAlpha ? ((target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24) : 0;
834 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
835 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
838 int sh = (target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24;
840 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
841 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
843 dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
844 dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
847 } else if (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) {
848 uint8_t *dest = (uint8_t *) _dest;
849 const uint8_t *r = (const uint8_t *) _r;
850 const uint8_t *g = (const uint8_t *) _g;
851 const uint8_t *b = (const uint8_t *) _b;
853 #define r_b ((target == PIX_FMT_RGB24) ? r : b)
854 #define b_r ((target == PIX_FMT_RGB24) ? b : r)
856 dest[i * 6 + 0] = r_b[Y1];
857 dest[i * 6 + 1] = g[Y1];
858 dest[i * 6 + 2] = b_r[Y1];
859 dest[i * 6 + 3] = r_b[Y2];
860 dest[i * 6 + 4] = g[Y2];
861 dest[i * 6 + 5] = b_r[Y2];
864 } else if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565 ||
865 target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555 ||
866 target == PIX_FMT_RGB444 || target == PIX_FMT_BGR444) {
867 uint16_t *dest = (uint16_t *) _dest;
868 const uint16_t *r = (const uint16_t *) _r;
869 const uint16_t *g = (const uint16_t *) _g;
870 const uint16_t *b = (const uint16_t *) _b;
871 int dr1, dg1, db1, dr2, dg2, db2;
873 if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565) {
874 dr1 = dither_2x2_8[ y & 1 ][0];
875 dg1 = dither_2x2_4[ y & 1 ][0];
876 db1 = dither_2x2_8[(y & 1) ^ 1][0];
877 dr2 = dither_2x2_8[ y & 1 ][1];
878 dg2 = dither_2x2_4[ y & 1 ][1];
879 db2 = dither_2x2_8[(y & 1) ^ 1][1];
880 } else if (target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555) {
881 dr1 = dither_2x2_8[ y & 1 ][0];
882 dg1 = dither_2x2_8[ y & 1 ][1];
883 db1 = dither_2x2_8[(y & 1) ^ 1][0];
884 dr2 = dither_2x2_8[ y & 1 ][1];
885 dg2 = dither_2x2_8[ y & 1 ][0];
886 db2 = dither_2x2_8[(y & 1) ^ 1][1];
888 dr1 = dither_4x4_16[ y & 3 ][0];
889 dg1 = dither_4x4_16[ y & 3 ][1];
890 db1 = dither_4x4_16[(y & 3) ^ 3][0];
891 dr2 = dither_4x4_16[ y & 3 ][1];
892 dg2 = dither_4x4_16[ y & 3 ][0];
893 db2 = dither_4x4_16[(y & 3) ^ 3][1];
896 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
897 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
898 } else /* 8/4-bit */ {
899 uint8_t *dest = (uint8_t *) _dest;
900 const uint8_t *r = (const uint8_t *) _r;
901 const uint8_t *g = (const uint8_t *) _g;
902 const uint8_t *b = (const uint8_t *) _b;
903 int dr1, dg1, db1, dr2, dg2, db2;
905 if (target == PIX_FMT_RGB8 || target == PIX_FMT_BGR8) {
906 const uint8_t * const d64 = dither_8x8_73[y & 7];
907 const uint8_t * const d32 = dither_8x8_32[y & 7];
908 dr1 = dg1 = d32[(i * 2 + 0) & 7];
909 db1 = d64[(i * 2 + 0) & 7];
910 dr2 = dg2 = d32[(i * 2 + 1) & 7];
911 db2 = d64[(i * 2 + 1) & 7];
913 const uint8_t * const d64 = dither_8x8_73 [y & 7];
914 const uint8_t * const d128 = dither_8x8_220[y & 7];
915 dr1 = db1 = d128[(i * 2 + 0) & 7];
916 dg1 = d64[(i * 2 + 0) & 7];
917 dr2 = db2 = d128[(i * 2 + 1) & 7];
918 dg2 = d64[(i * 2 + 1) & 7];
921 if (target == PIX_FMT_RGB4 || target == PIX_FMT_BGR4) {
922 dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
923 ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
925 dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
926 dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
931 static av_always_inline void
932 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
933 const int16_t **lumSrc, int lumFilterSize,
934 const int16_t *chrFilter, const int16_t **chrUSrc,
935 const int16_t **chrVSrc, int chrFilterSize,
936 const int16_t **alpSrc, uint8_t *dest, int dstW,
937 int y, enum PixelFormat target, int hasAlpha)
941 for (i = 0; i < (dstW >> 1); i++) {
947 const void *r, *g, *b;
949 for (j = 0; j < lumFilterSize; j++) {
950 Y1 += lumSrc[j][i * 2] * lumFilter[j];
951 Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
953 for (j = 0; j < chrFilterSize; j++) {
954 U += chrUSrc[j][i] * chrFilter[j];
955 V += chrVSrc[j][i] * chrFilter[j];
964 for (j = 0; j < lumFilterSize; j++) {
965 A1 += alpSrc[j][i * 2 ] * lumFilter[j];
966 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
970 if ((A1 | A2) & 0x100) {
971 A1 = av_clip_uint8(A1);
972 A2 = av_clip_uint8(A2);
976 r = c->table_rV[V + YUVRGB_TABLE_HEADROOM];
977 g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
978 b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
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_2_c_template(SwsContext *c, const int16_t *buf[2],
987 const int16_t *ubuf[2], const int16_t *vbuf[2],
988 const int16_t *abuf[2], uint8_t *dest, int dstW,
989 int yalpha, int uvalpha, int y,
990 enum PixelFormat target, int hasAlpha)
992 const int16_t *buf0 = buf[0], *buf1 = buf[1],
993 *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
994 *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
995 *abuf0 = hasAlpha ? abuf[0] : NULL,
996 *abuf1 = hasAlpha ? abuf[1] : NULL;
997 int yalpha1 = 4095 - yalpha;
998 int uvalpha1 = 4095 - uvalpha;
1001 for (i = 0; i < (dstW >> 1); i++) {
1002 int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
1003 int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
1004 int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
1005 int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
1007 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1008 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1009 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1012 A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
1013 A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1016 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1017 r, g, b, y, target, hasAlpha);
1021 static av_always_inline void
1022 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1023 const int16_t *ubuf[2], const int16_t *vbuf[2],
1024 const int16_t *abuf0, uint8_t *dest, int dstW,
1025 int uvalpha, int y, enum PixelFormat target,
1028 const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1031 if (uvalpha < 2048) {
1032 for (i = 0; i < (dstW >> 1); i++) {
1033 int Y1 = (buf0[i * 2 ] + 64) >> 7;
1034 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1035 int U = (ubuf0[i] + 64) >> 7;
1036 int V = (vbuf0[i] + 64) >> 7;
1038 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1039 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1040 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1043 A1 = (abuf0[i * 2 ] + 64) >> 7;
1044 A2 = (abuf0[i * 2 + 1] + 64) >> 7;
1047 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1048 r, g, b, y, target, hasAlpha);
1051 const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1052 for (i = 0; i < (dstW >> 1); i++) {
1053 int Y1 = (buf0[i * 2 ] + 64) >> 7;
1054 int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1055 int U = (ubuf0[i] + ubuf1[i] + 128) >> 8;
1056 int V = (vbuf0[i] + vbuf1[i] + 128) >> 8;
1058 const void *r = c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1059 *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1060 *b = c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1063 A1 = (abuf0[i * 2 ] + 64) >> 7;
1064 A2 = (abuf0[i * 2 + 1] + 64) >> 7;
1067 yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1068 r, g, b, y, target, hasAlpha);
1073 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1074 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1075 const int16_t **lumSrc, int lumFilterSize, \
1076 const int16_t *chrFilter, const int16_t **chrUSrc, \
1077 const int16_t **chrVSrc, int chrFilterSize, \
1078 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1081 name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1082 chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1083 alpSrc, dest, dstW, y, fmt, hasAlpha); \
1085 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1086 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1087 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1088 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1089 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1090 int yalpha, int uvalpha, int y) \
1092 name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1093 dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1096 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1097 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1098 const int16_t *abuf0, uint8_t *dest, int dstW, \
1099 int uvalpha, int y) \
1101 name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1102 dstW, uvalpha, y, fmt, hasAlpha); \
1106 YUV2RGBWRAPPER(yuv2rgb,, 32_1, PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1107 YUV2RGBWRAPPER(yuv2rgb,, 32, PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1109 #if CONFIG_SWSCALE_ALPHA
1110 YUV2RGBWRAPPER(yuv2rgb,, a32_1, PIX_FMT_RGB32_1, 1)
1111 YUV2RGBWRAPPER(yuv2rgb,, a32, PIX_FMT_RGB32, 1)
1113 YUV2RGBWRAPPER(yuv2rgb,, x32_1, PIX_FMT_RGB32_1, 0)
1114 YUV2RGBWRAPPER(yuv2rgb,, x32, PIX_FMT_RGB32, 0)
1116 YUV2RGBWRAPPER(yuv2, rgb, rgb24, PIX_FMT_RGB24, 0)
1117 YUV2RGBWRAPPER(yuv2, rgb, bgr24, PIX_FMT_BGR24, 0)
1118 YUV2RGBWRAPPER(yuv2rgb,, 16, PIX_FMT_RGB565, 0)
1119 YUV2RGBWRAPPER(yuv2rgb,, 15, PIX_FMT_RGB555, 0)
1120 YUV2RGBWRAPPER(yuv2rgb,, 12, PIX_FMT_RGB444, 0)
1121 YUV2RGBWRAPPER(yuv2rgb,, 8, PIX_FMT_RGB8, 0)
1122 YUV2RGBWRAPPER(yuv2rgb,, 4, PIX_FMT_RGB4, 0)
1123 YUV2RGBWRAPPER(yuv2rgb,, 4b, PIX_FMT_RGB4_BYTE, 0)
1125 static av_always_inline void
1126 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1127 const int16_t **lumSrc, int lumFilterSize,
1128 const int16_t *chrFilter, const int16_t **chrUSrc,
1129 const int16_t **chrVSrc, int chrFilterSize,
1130 const int16_t **alpSrc, uint8_t *dest,
1131 int dstW, int y, enum PixelFormat target, int hasAlpha)
1134 int step = (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) ? 3 : 4;
1136 for (i = 0; i < dstW; i++) {
1139 int U = (1<<9)-(128 << 19);
1140 int V = (1<<9)-(128 << 19);
1143 for (j = 0; j < lumFilterSize; j++) {
1144 Y += lumSrc[j][i] * lumFilter[j];
1146 for (j = 0; j < chrFilterSize; j++) {
1147 U += chrUSrc[j][i] * chrFilter[j];
1148 V += chrVSrc[j][i] * chrFilter[j];
1155 for (j = 0; j < lumFilterSize; j++) {
1156 A += alpSrc[j][i] * lumFilter[j];
1160 A = av_clip_uint8(A);
1162 Y -= c->yuv2rgb_y_offset;
1163 Y *= c->yuv2rgb_y_coeff;
1165 R = Y + V*c->yuv2rgb_v2r_coeff;
1166 G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1167 B = Y + U*c->yuv2rgb_u2b_coeff;
1168 if ((R | G | B) & 0xC0000000) {
1169 R = av_clip_uintp2(R, 30);
1170 G = av_clip_uintp2(G, 30);
1171 B = av_clip_uintp2(B, 30);
1176 dest[0] = hasAlpha ? A : 255;
1190 dest[3] = hasAlpha ? A : 255;
1193 dest[0] = hasAlpha ? A : 255;
1207 dest[3] = hasAlpha ? A : 255;
1215 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1216 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1217 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1218 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1220 #if CONFIG_SWSCALE_ALPHA
1221 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, 1)
1222 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, 1)
1223 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, 1)
1224 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, 1)
1226 YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, PIX_FMT_BGRA, 0)
1227 YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, PIX_FMT_ABGR, 0)
1228 YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, PIX_FMT_RGBA, 0)
1229 YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, PIX_FMT_ARGB, 0)
1231 YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, PIX_FMT_BGR24, 0)
1232 YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, PIX_FMT_RGB24, 0)
1234 void ff_sws_init_output_funcs(SwsContext *c,
1235 yuv2planar1_fn *yuv2plane1,
1236 yuv2planarX_fn *yuv2planeX,
1237 yuv2interleavedX_fn *yuv2nv12cX,
1238 yuv2packed1_fn *yuv2packed1,
1239 yuv2packed2_fn *yuv2packed2,
1240 yuv2packedX_fn *yuv2packedX)
1242 enum PixelFormat dstFormat = c->dstFormat;
1244 if (is16BPS(dstFormat)) {
1245 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
1246 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
1247 } else if (is9_OR_10BPS(dstFormat)) {
1248 if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
1249 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
1250 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
1252 *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
1253 *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
1256 *yuv2plane1 = yuv2plane1_8_c;
1257 *yuv2planeX = yuv2planeX_8_c;
1258 if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)
1259 *yuv2nv12cX = yuv2nv12cX_c;
1262 if(c->flags & SWS_FULL_CHR_H_INT) {
1263 switch (dstFormat) {
1266 *yuv2packedX = yuv2rgba32_full_X_c;
1268 #if CONFIG_SWSCALE_ALPHA
1270 *yuv2packedX = yuv2rgba32_full_X_c;
1272 #endif /* CONFIG_SWSCALE_ALPHA */
1274 *yuv2packedX = yuv2rgbx32_full_X_c;
1276 #endif /* !CONFIG_SMALL */
1280 *yuv2packedX = yuv2argb32_full_X_c;
1282 #if CONFIG_SWSCALE_ALPHA
1284 *yuv2packedX = yuv2argb32_full_X_c;
1286 #endif /* CONFIG_SWSCALE_ALPHA */
1288 *yuv2packedX = yuv2xrgb32_full_X_c;
1290 #endif /* !CONFIG_SMALL */
1294 *yuv2packedX = yuv2bgra32_full_X_c;
1296 #if CONFIG_SWSCALE_ALPHA
1298 *yuv2packedX = yuv2bgra32_full_X_c;
1300 #endif /* CONFIG_SWSCALE_ALPHA */
1302 *yuv2packedX = yuv2bgrx32_full_X_c;
1304 #endif /* !CONFIG_SMALL */
1308 *yuv2packedX = yuv2abgr32_full_X_c;
1310 #if CONFIG_SWSCALE_ALPHA
1312 *yuv2packedX = yuv2abgr32_full_X_c;
1314 #endif /* CONFIG_SWSCALE_ALPHA */
1316 *yuv2packedX = yuv2xbgr32_full_X_c;
1318 #endif /* !CONFIG_SMALL */
1321 *yuv2packedX = yuv2rgb24_full_X_c;
1324 *yuv2packedX = yuv2bgr24_full_X_c;
1331 switch (dstFormat) {
1332 case PIX_FMT_RGB48LE:
1333 *yuv2packed1 = yuv2rgb48le_1_c;
1334 *yuv2packed2 = yuv2rgb48le_2_c;
1335 *yuv2packedX = yuv2rgb48le_X_c;
1337 case PIX_FMT_RGB48BE:
1338 *yuv2packed1 = yuv2rgb48be_1_c;
1339 *yuv2packed2 = yuv2rgb48be_2_c;
1340 *yuv2packedX = yuv2rgb48be_X_c;
1342 case PIX_FMT_BGR48LE:
1343 *yuv2packed1 = yuv2bgr48le_1_c;
1344 *yuv2packed2 = yuv2bgr48le_2_c;
1345 *yuv2packedX = yuv2bgr48le_X_c;
1347 case PIX_FMT_BGR48BE:
1348 *yuv2packed1 = yuv2bgr48be_1_c;
1349 *yuv2packed2 = yuv2bgr48be_2_c;
1350 *yuv2packedX = yuv2bgr48be_X_c;
1355 *yuv2packed1 = yuv2rgb32_1_c;
1356 *yuv2packed2 = yuv2rgb32_2_c;
1357 *yuv2packedX = yuv2rgb32_X_c;
1359 #if CONFIG_SWSCALE_ALPHA
1361 *yuv2packed1 = yuv2rgba32_1_c;
1362 *yuv2packed2 = yuv2rgba32_2_c;
1363 *yuv2packedX = yuv2rgba32_X_c;
1365 #endif /* CONFIG_SWSCALE_ALPHA */
1367 *yuv2packed1 = yuv2rgbx32_1_c;
1368 *yuv2packed2 = yuv2rgbx32_2_c;
1369 *yuv2packedX = yuv2rgbx32_X_c;
1371 #endif /* !CONFIG_SMALL */
1373 case PIX_FMT_RGB32_1:
1374 case PIX_FMT_BGR32_1:
1376 *yuv2packed1 = yuv2rgb32_1_1_c;
1377 *yuv2packed2 = yuv2rgb32_1_2_c;
1378 *yuv2packedX = yuv2rgb32_1_X_c;
1380 #if CONFIG_SWSCALE_ALPHA
1382 *yuv2packed1 = yuv2rgba32_1_1_c;
1383 *yuv2packed2 = yuv2rgba32_1_2_c;
1384 *yuv2packedX = yuv2rgba32_1_X_c;
1386 #endif /* CONFIG_SWSCALE_ALPHA */
1388 *yuv2packed1 = yuv2rgbx32_1_1_c;
1389 *yuv2packed2 = yuv2rgbx32_1_2_c;
1390 *yuv2packedX = yuv2rgbx32_1_X_c;
1392 #endif /* !CONFIG_SMALL */
1395 *yuv2packed1 = yuv2rgb24_1_c;
1396 *yuv2packed2 = yuv2rgb24_2_c;
1397 *yuv2packedX = yuv2rgb24_X_c;
1400 *yuv2packed1 = yuv2bgr24_1_c;
1401 *yuv2packed2 = yuv2bgr24_2_c;
1402 *yuv2packedX = yuv2bgr24_X_c;
1404 case PIX_FMT_RGB565LE:
1405 case PIX_FMT_RGB565BE:
1406 case PIX_FMT_BGR565LE:
1407 case PIX_FMT_BGR565BE:
1408 *yuv2packed1 = yuv2rgb16_1_c;
1409 *yuv2packed2 = yuv2rgb16_2_c;
1410 *yuv2packedX = yuv2rgb16_X_c;
1412 case PIX_FMT_RGB555LE:
1413 case PIX_FMT_RGB555BE:
1414 case PIX_FMT_BGR555LE:
1415 case PIX_FMT_BGR555BE:
1416 *yuv2packed1 = yuv2rgb15_1_c;
1417 *yuv2packed2 = yuv2rgb15_2_c;
1418 *yuv2packedX = yuv2rgb15_X_c;
1420 case PIX_FMT_RGB444LE:
1421 case PIX_FMT_RGB444BE:
1422 case PIX_FMT_BGR444LE:
1423 case PIX_FMT_BGR444BE:
1424 *yuv2packed1 = yuv2rgb12_1_c;
1425 *yuv2packed2 = yuv2rgb12_2_c;
1426 *yuv2packedX = yuv2rgb12_X_c;
1430 *yuv2packed1 = yuv2rgb8_1_c;
1431 *yuv2packed2 = yuv2rgb8_2_c;
1432 *yuv2packedX = yuv2rgb8_X_c;
1436 *yuv2packed1 = yuv2rgb4_1_c;
1437 *yuv2packed2 = yuv2rgb4_2_c;
1438 *yuv2packedX = yuv2rgb4_X_c;
1440 case PIX_FMT_RGB4_BYTE:
1441 case PIX_FMT_BGR4_BYTE:
1442 *yuv2packed1 = yuv2rgb4b_1_c;
1443 *yuv2packed2 = yuv2rgb4b_2_c;
1444 *yuv2packedX = yuv2rgb4b_X_c;
1448 switch (dstFormat) {
1449 case PIX_FMT_MONOWHITE:
1450 *yuv2packed1 = yuv2monowhite_1_c;
1451 *yuv2packed2 = yuv2monowhite_2_c;
1452 *yuv2packedX = yuv2monowhite_X_c;
1454 case PIX_FMT_MONOBLACK:
1455 *yuv2packed1 = yuv2monoblack_1_c;
1456 *yuv2packed2 = yuv2monoblack_2_c;
1457 *yuv2packedX = yuv2monoblack_X_c;
1459 case PIX_FMT_YUYV422:
1460 *yuv2packed1 = yuv2yuyv422_1_c;
1461 *yuv2packed2 = yuv2yuyv422_2_c;
1462 *yuv2packedX = yuv2yuyv422_X_c;
1464 case PIX_FMT_UYVY422:
1465 *yuv2packed1 = yuv2uyvy422_1_c;
1466 *yuv2packed2 = yuv2uyvy422_2_c;
1467 *yuv2packedX = yuv2uyvy422_X_c;