]> git.sesse.net Git - ffmpeg/blob - libswscale/output.c
swscale: handle gray16 as a "planar" YUV format (Y-only, of course).
[ffmpeg] / libswscale / output.c
1 /*
2  * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <assert.h>
22 #include <math.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <string.h>
26
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"
33 #include "config.h"
34 #include "rgb2rgb.h"
35 #include "swscale.h"
36 #include "swscale_internal.h"
37
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, },
41 };
42
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, },
46 };
47
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, },
53 };
54
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, },
64 };
65
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, },
75 };
76
77 #if 1
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, },
87 };
88 #elif 1
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, },
99 };
100 #elif 1
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, },
111 };
112 #else
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, },
123 };
124 #endif
125
126 #define output_pixel(pos, val, bias, signedness) \
127     if (big_endian) { \
128         AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
129     } else { \
130         AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
131     }
132
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)
136 {
137     int i;
138     int shift = 19 - output_bits;
139
140     for (i = 0; i < dstW; i++) {
141         int val = src[i] + (1 << (shift - 1));
142         output_pixel(&dest[i], val, 0, uint);
143     }
144 }
145
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)
150 {
151     int i;
152     int shift = 15 + 16 - output_bits;
153
154     for (i = 0; i < dstW; i++) {
155         int val = 1 << (30-output_bits);
156         int j;
157
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. */
163         val -= 0x40000000;
164         for (j = 0; j < filterSize; j++)
165             val += src[j][i] * filter[j];
166
167         output_pixel(&dest[i], val, 0x8000, int);
168     }
169 }
170
171 #undef output_pixel
172
173 #define output_pixel(pos, val) \
174     if (big_endian) { \
175         AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
176     } else { \
177         AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
178     }
179
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)
183 {
184     int i;
185     int shift = 15 - output_bits;
186
187     for (i = 0; i < dstW; i++) {
188         int val = src[i] + (1 << (shift - 1));
189         output_pixel(&dest[i], val);
190     }
191 }
192
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)
197 {
198     int i;
199     int shift = 11 + 16 - output_bits;
200
201     for (i = 0; i < dstW; i++) {
202         int val = 1 << (26-output_bits);
203         int j;
204
205         for (j = 0; j < filterSize; j++)
206             val += src[j][i] * filter[j];
207
208         output_pixel(&dest[i], val);
209     }
210 }
211
212 #undef output_pixel
213
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)\
218 { \
219     yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
220                          (uint16_t *) dest, dstW, is_be, bits); \
221 }\
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)\
225 { \
226     yuv2planeX_## template_size ## _c_template(filter, \
227                          filterSize, (const typeX_t **) src, \
228                          (uint16_t *) dest, dstW, is_be, bits); \
229 }
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)
236
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)
240 {
241     int i;
242     for (i=0; i<dstW; i++) {
243         int val = dither[(i + offset) & 7] << 12;
244         int j;
245         for (j=0; j<filterSize; j++)
246             val += src[j][i] * filter[j];
247
248         dest[i]= av_clip_uint8(val>>19);
249     }
250 }
251
252 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
253                            const uint8_t *dither, int offset)
254 {
255     int i;
256     for (i=0; i<dstW; i++) {
257         int val = (src[i] + dither[(i + offset) & 7]) >> 7;
258         dest[i]= av_clip_uint8(val);
259     }
260 }
261
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)
265 {
266     enum PixelFormat dstFormat = c->dstFormat;
267     const uint8_t *chrDither = c->chrDither8;
268     int i;
269
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;
274             int j;
275             for (j=0; j<chrFilterSize; j++) {
276                 u += chrUSrc[j][i] * chrFilter[j];
277                 v += chrVSrc[j][i] * chrFilter[j];
278             }
279
280             dest[2*i]= av_clip_uint8(u>>19);
281             dest[2*i+1]= av_clip_uint8(v>>19);
282         }
283     else
284         for (i=0; i<chrDstW; i++) {
285             int u = chrDither[i & 7] << 12;
286             int v = chrDither[(i + 3) & 7] << 12;
287             int j;
288             for (j=0; j<chrFilterSize; j++) {
289                 u += chrUSrc[j][i] * chrFilter[j];
290                 v += chrVSrc[j][i] * chrFilter[j];
291             }
292
293             dest[2*i]= av_clip_uint8(v>>19);
294             dest[2*i+1]= av_clip_uint8(u>>19);
295         }
296 }
297
298 #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
299 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
300                         const int16_t **_lumSrc, int lumFilterSize, \
301                         const int16_t *chrFilter, const int16_t **_chrUSrc, \
302                         const int16_t **_chrVSrc, int chrFilterSize, \
303                         const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
304                         int y) \
305 { \
306     const int32_t **lumSrc  = (const int32_t **) _lumSrc, \
307                   **chrUSrc = (const int32_t **) _chrUSrc, \
308                   **chrVSrc = (const int32_t **) _chrVSrc, \
309                   **alpSrc  = (const int32_t **) _alpSrc; \
310     uint16_t *dest = (uint16_t *) _dest; \
311     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
312                           chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
313                           alpSrc, dest, dstW, y, fmt); \
314 } \
315  \
316 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
317                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
318                         const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
319                         int yalpha, int uvalpha, int y) \
320 { \
321     const int32_t **buf  = (const int32_t **) _buf, \
322                   **ubuf = (const int32_t **) _ubuf, \
323                   **vbuf = (const int32_t **) _vbuf, \
324                   **abuf = (const int32_t **) _abuf; \
325     uint16_t *dest = (uint16_t *) _dest; \
326     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
327                           dest, dstW, yalpha, uvalpha, y, fmt); \
328 } \
329  \
330 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
331                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
332                         const int16_t *_abuf0, uint8_t *_dest, int dstW, \
333                         int uvalpha, int y) \
334 { \
335     const int32_t *buf0  = (const int32_t *)  _buf0, \
336                  **ubuf  = (const int32_t **) _ubuf, \
337                  **vbuf  = (const int32_t **) _vbuf, \
338                   *abuf0 = (const int32_t *)  _abuf0; \
339     uint16_t *dest = (uint16_t *) _dest; \
340     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
341                                   dstW, uvalpha, y, fmt); \
342 }
343
344 #define output_pixel(pos, acc) \
345     if (target == PIX_FMT_MONOBLACK) { \
346         pos = acc; \
347     } else { \
348         pos = ~acc; \
349     }
350
351 static av_always_inline void
352 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
353                       const int16_t **lumSrc, int lumFilterSize,
354                       const int16_t *chrFilter, const int16_t **chrUSrc,
355                       const int16_t **chrVSrc, int chrFilterSize,
356                       const int16_t **alpSrc, uint8_t *dest, int dstW,
357                       int y, enum PixelFormat target)
358 {
359     const uint8_t * const d128=dither_8x8_220[y&7];
360     uint8_t *g = c->table_gU[128] + c->table_gV[128];
361     int i;
362     unsigned acc = 0;
363
364     for (i = 0; i < dstW - 1; i += 2) {
365         int j;
366         int Y1 = 1 << 18;
367         int Y2 = 1 << 18;
368
369         for (j = 0; j < lumFilterSize; j++) {
370             Y1 += lumSrc[j][i]   * lumFilter[j];
371             Y2 += lumSrc[j][i+1] * lumFilter[j];
372         }
373         Y1 >>= 19;
374         Y2 >>= 19;
375         if ((Y1 | Y2) & 0x100) {
376             Y1 = av_clip_uint8(Y1);
377             Y2 = av_clip_uint8(Y2);
378         }
379         acc += acc + g[Y1 + d128[(i + 0) & 7]];
380         acc += acc + g[Y2 + d128[(i + 1) & 7]];
381         if ((i & 7) == 6) {
382             output_pixel(*dest++, acc);
383         }
384     }
385 }
386
387 static av_always_inline void
388 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
389                       const int16_t *ubuf[2], const int16_t *vbuf[2],
390                       const int16_t *abuf[2], uint8_t *dest, int dstW,
391                       int yalpha, int uvalpha, int y,
392                       enum PixelFormat target)
393 {
394     const int16_t *buf0  = buf[0],  *buf1  = buf[1];
395     const uint8_t * const d128 = dither_8x8_220[y & 7];
396     uint8_t *g = c->table_gU[128] + c->table_gV[128];
397     int  yalpha1 = 4095 - yalpha;
398     int i;
399
400     for (i = 0; i < dstW - 7; i += 8) {
401         int acc =    g[((buf0[i    ] * yalpha1 + buf1[i    ] * yalpha) >> 19) + d128[0]];
402         acc += acc + g[((buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19) + d128[1]];
403         acc += acc + g[((buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19) + d128[2]];
404         acc += acc + g[((buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19) + d128[3]];
405         acc += acc + g[((buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19) + d128[4]];
406         acc += acc + g[((buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19) + d128[5]];
407         acc += acc + g[((buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19) + d128[6]];
408         acc += acc + g[((buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19) + d128[7]];
409         output_pixel(*dest++, acc);
410     }
411 }
412
413 static av_always_inline void
414 yuv2mono_1_c_template(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, enum PixelFormat target)
418 {
419     const uint8_t * const d128 = dither_8x8_220[y & 7];
420     uint8_t *g = c->table_gU[128] + c->table_gV[128];
421     int i;
422
423     for (i = 0; i < dstW - 7; i += 8) {
424         int acc =    g[(buf0[i    ] >> 7) + d128[0]];
425         acc += acc + g[(buf0[i + 1] >> 7) + d128[1]];
426         acc += acc + g[(buf0[i + 2] >> 7) + d128[2]];
427         acc += acc + g[(buf0[i + 3] >> 7) + d128[3]];
428         acc += acc + g[(buf0[i + 4] >> 7) + d128[4]];
429         acc += acc + g[(buf0[i + 5] >> 7) + d128[5]];
430         acc += acc + g[(buf0[i + 6] >> 7) + d128[6]];
431         acc += acc + g[(buf0[i + 7] >> 7) + d128[7]];
432         output_pixel(*dest++, acc);
433     }
434 }
435
436 #undef output_pixel
437
438 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
439 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
440                                 const int16_t **lumSrc, int lumFilterSize, \
441                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
442                                 const int16_t **chrVSrc, int chrFilterSize, \
443                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
444                                 int y) \
445 { \
446     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
447                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
448                                   alpSrc, dest, dstW, y, fmt); \
449 } \
450  \
451 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
452                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
453                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
454                                 int yalpha, int uvalpha, int y) \
455 { \
456     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
457                                   dest, dstW, yalpha, uvalpha, y, fmt); \
458 } \
459  \
460 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
461                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
462                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
463                                 int uvalpha, int y) \
464 { \
465     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
466                                   abuf0, dest, dstW, uvalpha, \
467                                   y, fmt); \
468 }
469
470 YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE)
471 YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK)
472
473 #define output_pixels(pos, Y1, U, Y2, V) \
474     if (target == PIX_FMT_YUYV422) { \
475         dest[pos + 0] = Y1; \
476         dest[pos + 1] = U;  \
477         dest[pos + 2] = Y2; \
478         dest[pos + 3] = V;  \
479     } else { \
480         dest[pos + 0] = U;  \
481         dest[pos + 1] = Y1; \
482         dest[pos + 2] = V;  \
483         dest[pos + 3] = Y2; \
484     }
485
486 static av_always_inline void
487 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
488                      const int16_t **lumSrc, int lumFilterSize,
489                      const int16_t *chrFilter, const int16_t **chrUSrc,
490                      const int16_t **chrVSrc, int chrFilterSize,
491                      const int16_t **alpSrc, uint8_t *dest, int dstW,
492                      int y, enum PixelFormat target)
493 {
494     int i;
495
496     for (i = 0; i < (dstW >> 1); i++) {
497         int j;
498         int Y1 = 1 << 18;
499         int Y2 = 1 << 18;
500         int U  = 1 << 18;
501         int V  = 1 << 18;
502
503         for (j = 0; j < lumFilterSize; j++) {
504             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
505             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
506         }
507         for (j = 0; j < chrFilterSize; j++) {
508             U += chrUSrc[j][i] * chrFilter[j];
509             V += chrVSrc[j][i] * chrFilter[j];
510         }
511         Y1 >>= 19;
512         Y2 >>= 19;
513         U  >>= 19;
514         V  >>= 19;
515         if ((Y1 | Y2 | U | V) & 0x100) {
516             Y1 = av_clip_uint8(Y1);
517             Y2 = av_clip_uint8(Y2);
518             U  = av_clip_uint8(U);
519             V  = av_clip_uint8(V);
520         }
521         output_pixels(4*i, Y1, U, Y2, V);
522     }
523 }
524
525 static av_always_inline void
526 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
527                      const int16_t *ubuf[2], const int16_t *vbuf[2],
528                      const int16_t *abuf[2], uint8_t *dest, int dstW,
529                      int yalpha, int uvalpha, int y,
530                      enum PixelFormat target)
531 {
532     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
533                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
534                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
535     int  yalpha1 = 4095 - yalpha;
536     int uvalpha1 = 4095 - uvalpha;
537     int i;
538
539     for (i = 0; i < (dstW >> 1); i++) {
540         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
541         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
542         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
543         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
544
545         output_pixels(i * 4, Y1, U, Y2, V);
546     }
547 }
548
549 static av_always_inline void
550 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
551                      const int16_t *ubuf[2], const int16_t *vbuf[2],
552                      const int16_t *abuf0, uint8_t *dest, int dstW,
553                      int uvalpha, int y, enum PixelFormat target)
554 {
555     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
556     int i;
557
558     if (uvalpha < 2048) {
559         for (i = 0; i < (dstW >> 1); i++) {
560             int Y1 = buf0[i * 2]     >> 7;
561             int Y2 = buf0[i * 2 + 1] >> 7;
562             int U  = ubuf0[i]        >> 7;
563             int V  = vbuf0[i]        >> 7;
564
565             output_pixels(i * 4, Y1, U, Y2, V);
566         }
567     } else {
568         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
569         for (i = 0; i < (dstW >> 1); i++) {
570             int Y1 =  buf0[i * 2]          >> 7;
571             int Y2 =  buf0[i * 2 + 1]      >> 7;
572             int U  = (ubuf0[i] + ubuf1[i]) >> 8;
573             int V  = (vbuf0[i] + vbuf1[i]) >> 8;
574
575             output_pixels(i * 4, Y1, U, Y2, V);
576         }
577     }
578 }
579
580 #undef output_pixels
581
582 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422)
583 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422)
584
585 #define R_B ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? R : B)
586 #define B_R ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? B : R)
587 #define output_pixel(pos, val) \
588     if (isBE(target)) { \
589         AV_WB16(pos, val); \
590     } else { \
591         AV_WL16(pos, val); \
592     }
593
594 static av_always_inline void
595 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
596                        const int32_t **lumSrc, int lumFilterSize,
597                        const int16_t *chrFilter, const int32_t **chrUSrc,
598                        const int32_t **chrVSrc, int chrFilterSize,
599                        const int32_t **alpSrc, uint16_t *dest, int dstW,
600                        int y, enum PixelFormat target)
601 {
602     int i;
603
604     for (i = 0; i < (dstW >> 1); i++) {
605         int j;
606         int Y1 = -0x40000000;
607         int Y2 = -0x40000000;
608         int U  = -128 << 23; // 19
609         int V  = -128 << 23;
610         int R, G, B;
611
612         for (j = 0; j < lumFilterSize; j++) {
613             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
614             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
615         }
616         for (j = 0; j < chrFilterSize; j++) {
617             U += chrUSrc[j][i] * chrFilter[j];
618             V += chrVSrc[j][i] * chrFilter[j];
619         }
620
621         // 8bit: 12+15=27; 16-bit: 12+19=31
622         Y1 >>= 14; // 10
623         Y1 += 0x10000;
624         Y2 >>= 14;
625         Y2 += 0x10000;
626         U  >>= 14;
627         V  >>= 14;
628
629         // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
630         Y1 -= c->yuv2rgb_y_offset;
631         Y2 -= c->yuv2rgb_y_offset;
632         Y1 *= c->yuv2rgb_y_coeff;
633         Y2 *= c->yuv2rgb_y_coeff;
634         Y1 += 1 << 13; // 21
635         Y2 += 1 << 13;
636         // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
637
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;
641
642         // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
643         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
644         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
645         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
646         output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
647         output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
648         output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
649         dest += 6;
650     }
651 }
652
653 static av_always_inline void
654 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
655                        const int32_t *ubuf[2], const int32_t *vbuf[2],
656                        const int32_t *abuf[2], uint16_t *dest, int dstW,
657                        int yalpha, int uvalpha, int y,
658                        enum PixelFormat target)
659 {
660     const int32_t *buf0  = buf[0],  *buf1  = buf[1],
661                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
662                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
663     int  yalpha1 = 4095 - yalpha;
664     int uvalpha1 = 4095 - uvalpha;
665     int i;
666
667     for (i = 0; i < (dstW >> 1); i++) {
668         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha) >> 14;
669         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha) >> 14;
670         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha + (-128 << 23)) >> 14;
671         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha + (-128 << 23)) >> 14;
672         int R, G, B;
673
674         Y1 -= c->yuv2rgb_y_offset;
675         Y2 -= c->yuv2rgb_y_offset;
676         Y1 *= c->yuv2rgb_y_coeff;
677         Y2 *= c->yuv2rgb_y_coeff;
678         Y1 += 1 << 13;
679         Y2 += 1 << 13;
680
681         R = V * c->yuv2rgb_v2r_coeff;
682         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
683         B =                            U * c->yuv2rgb_u2b_coeff;
684
685         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
686         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
687         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
688         output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
689         output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
690         output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
691         dest += 6;
692     }
693 }
694
695 static av_always_inline void
696 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
697                        const int32_t *ubuf[2], const int32_t *vbuf[2],
698                        const int32_t *abuf0, uint16_t *dest, int dstW,
699                        int uvalpha, int y, enum PixelFormat target)
700 {
701     const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
702     int i;
703
704     if (uvalpha < 2048) {
705         for (i = 0; i < (dstW >> 1); i++) {
706             int Y1 = (buf0[i * 2]    ) >> 2;
707             int Y2 = (buf0[i * 2 + 1]) >> 2;
708             int U  = (ubuf0[i] + (-128 << 11)) >> 2;
709             int V  = (vbuf0[i] + (-128 << 11)) >> 2;
710             int R, G, B;
711
712             Y1 -= c->yuv2rgb_y_offset;
713             Y2 -= c->yuv2rgb_y_offset;
714             Y1 *= c->yuv2rgb_y_coeff;
715             Y2 *= c->yuv2rgb_y_coeff;
716             Y1 += 1 << 13;
717             Y2 += 1 << 13;
718
719             R = V * c->yuv2rgb_v2r_coeff;
720             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
721             B =                            U * c->yuv2rgb_u2b_coeff;
722
723             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
724             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
725             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
726             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
727             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
728             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
729             dest += 6;
730         }
731     } else {
732         const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
733         for (i = 0; i < (dstW >> 1); i++) {
734             int Y1 = (buf0[i * 2]    ) >> 2;
735             int Y2 = (buf0[i * 2 + 1]) >> 2;
736             int U  = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
737             int V  = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
738             int R, G, B;
739
740             Y1 -= c->yuv2rgb_y_offset;
741             Y2 -= c->yuv2rgb_y_offset;
742             Y1 *= c->yuv2rgb_y_coeff;
743             Y2 *= c->yuv2rgb_y_coeff;
744             Y1 += 1 << 13;
745             Y2 += 1 << 13;
746
747             R = V * c->yuv2rgb_v2r_coeff;
748             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
749             B =                            U * c->yuv2rgb_u2b_coeff;
750
751             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
752             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
753             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
754             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
755             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
756             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
757             dest += 6;
758         }
759     }
760 }
761
762 #undef output_pixel
763 #undef r_b
764 #undef b_r
765
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)
770
771 /*
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.
778  */
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)
784 {
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;
791
792 #if CONFIG_SMALL
793         int sh = hasAlpha ? ((target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24) : 0;
794
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);
797 #else
798         if (hasAlpha) {
799             int sh = (target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24;
800
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);
803         } else {
804             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
805             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
806         }
807 #endif
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;
813
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];
822 #undef r_b
823 #undef b_r
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;
832
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];
847         } else {
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];
854         }
855
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;
864
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];
872         } else {
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];
879         }
880
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);
884         } else {
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];
887         }
888     }
889 }
890
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)
898 {
899     int i;
900
901     for (i = 0; i < (dstW >> 1); i++) {
902         int j;
903         int Y1 = 1 << 18;
904         int Y2 = 1 << 18;
905         int U  = 1 << 18;
906         int V  = 1 << 18;
907         int av_unused A1, A2;
908         const void *r, *g, *b;
909
910         for (j = 0; j < lumFilterSize; j++) {
911             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
912             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
913         }
914         for (j = 0; j < chrFilterSize; j++) {
915             U += chrUSrc[j][i] * chrFilter[j];
916             V += chrVSrc[j][i] * chrFilter[j];
917         }
918         Y1 >>= 19;
919         Y2 >>= 19;
920         U  >>= 19;
921         V  >>= 19;
922         if ((Y1 | Y2 | U | V) & 0x100) {
923             Y1 = av_clip_uint8(Y1);
924             Y2 = av_clip_uint8(Y2);
925             U  = av_clip_uint8(U);
926             V  = av_clip_uint8(V);
927         }
928         if (hasAlpha) {
929             A1 = 1 << 18;
930             A2 = 1 << 18;
931             for (j = 0; j < lumFilterSize; j++) {
932                 A1 += alpSrc[j][i * 2    ] * lumFilter[j];
933                 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
934             }
935             A1 >>= 19;
936             A2 >>= 19;
937             if ((A1 | A2) & 0x100) {
938                 A1 = av_clip_uint8(A1);
939                 A2 = av_clip_uint8(A2);
940             }
941         }
942
943         /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
944         r =  c->table_rV[V];
945         g = (c->table_gU[U] + c->table_gV[V]);
946         b =  c->table_bU[U];
947
948         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
949                       r, g, b, y, target, hasAlpha);
950     }
951 }
952
953 static av_always_inline void
954 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
955                      const int16_t *ubuf[2], const int16_t *vbuf[2],
956                      const int16_t *abuf[2], uint8_t *dest, int dstW,
957                      int yalpha, int uvalpha, int y,
958                      enum PixelFormat target, int hasAlpha)
959 {
960     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
961                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
962                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
963                   *abuf0 = hasAlpha ? abuf[0] : NULL,
964                   *abuf1 = hasAlpha ? abuf[1] : NULL;
965     int  yalpha1 = 4095 - yalpha;
966     int uvalpha1 = 4095 - uvalpha;
967     int i;
968
969     for (i = 0; i < (dstW >> 1); i++) {
970         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
971         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
972         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
973         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
974         int A1, A2;
975         const void *r =  c->table_rV[V],
976                    *g = (c->table_gU[U] + c->table_gV[V]),
977                    *b =  c->table_bU[U];
978
979         if (hasAlpha) {
980             A1 = (abuf0[i * 2    ] * yalpha1 + abuf1[i * 2    ] * yalpha) >> 19;
981             A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
982         }
983
984         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
985                       r, g, b, y, target, hasAlpha);
986     }
987 }
988
989 static av_always_inline void
990 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
991                      const int16_t *ubuf[2], const int16_t *vbuf[2],
992                      const int16_t *abuf0, uint8_t *dest, int dstW,
993                      int uvalpha, int y, enum PixelFormat target,
994                      int hasAlpha)
995 {
996     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
997     int i;
998
999     if (uvalpha < 2048) {
1000         for (i = 0; i < (dstW >> 1); i++) {
1001             int Y1 = buf0[i * 2]     >> 7;
1002             int Y2 = buf0[i * 2 + 1] >> 7;
1003             int U  = ubuf0[i]        >> 7;
1004             int V  = vbuf0[i]        >> 7;
1005             int A1, A2;
1006             const void *r =  c->table_rV[V],
1007                        *g = (c->table_gU[U] + c->table_gV[V]),
1008                        *b =  c->table_bU[U];
1009
1010             if (hasAlpha) {
1011                 A1 = abuf0[i * 2    ] >> 7;
1012                 A2 = abuf0[i * 2 + 1] >> 7;
1013             }
1014
1015             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1016                           r, g, b, y, target, hasAlpha);
1017         }
1018     } else {
1019         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1020         for (i = 0; i < (dstW >> 1); i++) {
1021             int Y1 =  buf0[i * 2]          >> 7;
1022             int Y2 =  buf0[i * 2 + 1]      >> 7;
1023             int U  = (ubuf0[i] + ubuf1[i]) >> 8;
1024             int V  = (vbuf0[i] + vbuf1[i]) >> 8;
1025             int A1, A2;
1026             const void *r =  c->table_rV[V],
1027                        *g = (c->table_gU[U] + c->table_gV[V]),
1028                        *b =  c->table_bU[U];
1029
1030             if (hasAlpha) {
1031                 A1 = abuf0[i * 2    ] >> 7;
1032                 A2 = abuf0[i * 2 + 1] >> 7;
1033             }
1034
1035             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1036                           r, g, b, y, target, hasAlpha);
1037         }
1038     }
1039 }
1040
1041 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1042 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1043                                 const int16_t **lumSrc, int lumFilterSize, \
1044                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
1045                                 const int16_t **chrVSrc, int chrFilterSize, \
1046                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1047                                 int y) \
1048 { \
1049     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1050                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1051                                   alpSrc, dest, dstW, y, fmt, hasAlpha); \
1052 }
1053 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1054 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1055 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1056                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1057                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1058                                 int yalpha, int uvalpha, int y) \
1059 { \
1060     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1061                                   dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1062 } \
1063  \
1064 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1065                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1066                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
1067                                 int uvalpha, int y) \
1068 { \
1069     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1070                                   dstW, uvalpha, y, fmt, hasAlpha); \
1071 }
1072
1073 #if CONFIG_SMALL
1074 YUV2RGBWRAPPER(yuv2rgb,,  32_1,  PIX_FMT_RGB32_1,   CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1075 YUV2RGBWRAPPER(yuv2rgb,,  32,    PIX_FMT_RGB32,     CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1076 #else
1077 #if CONFIG_SWSCALE_ALPHA
1078 YUV2RGBWRAPPER(yuv2rgb,, a32_1,  PIX_FMT_RGB32_1,   1)
1079 YUV2RGBWRAPPER(yuv2rgb,, a32,    PIX_FMT_RGB32,     1)
1080 #endif
1081 YUV2RGBWRAPPER(yuv2rgb,, x32_1,  PIX_FMT_RGB32_1,   0)
1082 YUV2RGBWRAPPER(yuv2rgb,, x32,    PIX_FMT_RGB32,     0)
1083 #endif
1084 YUV2RGBWRAPPER(yuv2, rgb, rgb24, PIX_FMT_RGB24,   0)
1085 YUV2RGBWRAPPER(yuv2, rgb, bgr24, PIX_FMT_BGR24,   0)
1086 YUV2RGBWRAPPER(yuv2rgb,,  16,    PIX_FMT_RGB565,    0)
1087 YUV2RGBWRAPPER(yuv2rgb,,  15,    PIX_FMT_RGB555,    0)
1088 YUV2RGBWRAPPER(yuv2rgb,,  12,    PIX_FMT_RGB444,    0)
1089 YUV2RGBWRAPPER(yuv2rgb,,   8,    PIX_FMT_RGB8,      0)
1090 YUV2RGBWRAPPER(yuv2rgb,,   4,    PIX_FMT_RGB4,      0)
1091 YUV2RGBWRAPPER(yuv2rgb,,   4b,   PIX_FMT_RGB4_BYTE, 0)
1092
1093 static av_always_inline void
1094 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1095                           const int16_t **lumSrc, int lumFilterSize,
1096                           const int16_t *chrFilter, const int16_t **chrUSrc,
1097                           const int16_t **chrVSrc, int chrFilterSize,
1098                           const int16_t **alpSrc, uint8_t *dest,
1099                           int dstW, int y, enum PixelFormat target, int hasAlpha)
1100 {
1101     int i;
1102     int step = (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) ? 3 : 4;
1103
1104     for (i = 0; i < dstW; i++) {
1105         int j;
1106         int Y = 0;
1107         int U = -128 << 19;
1108         int V = -128 << 19;
1109         int av_unused A;
1110         int R, G, B;
1111
1112         for (j = 0; j < lumFilterSize; j++) {
1113             Y += lumSrc[j][i] * lumFilter[j];
1114         }
1115         for (j = 0; j < chrFilterSize; j++) {
1116             U += chrUSrc[j][i] * chrFilter[j];
1117             V += chrVSrc[j][i] * chrFilter[j];
1118         }
1119         Y >>= 10;
1120         U >>= 10;
1121         V >>= 10;
1122         if (hasAlpha) {
1123             A = 1 << 21;
1124             for (j = 0; j < lumFilterSize; j++) {
1125                 A += alpSrc[j][i] * lumFilter[j];
1126             }
1127             A >>= 19;
1128             if (A & 0x100)
1129                 A = av_clip_uint8(A);
1130         }
1131         Y -= c->yuv2rgb_y_offset;
1132         Y *= c->yuv2rgb_y_coeff;
1133         Y += 1 << 21;
1134         R = Y + V*c->yuv2rgb_v2r_coeff;
1135         G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1136         B = Y +                          U*c->yuv2rgb_u2b_coeff;
1137         if ((R | G | B) & 0xC0000000) {
1138             R = av_clip_uintp2(R, 30);
1139             G = av_clip_uintp2(G, 30);
1140             B = av_clip_uintp2(B, 30);
1141         }
1142
1143         switch(target) {
1144         case PIX_FMT_ARGB:
1145             dest[0] = hasAlpha ? A : 255;
1146             dest[1] = R >> 22;
1147             dest[2] = G >> 22;
1148             dest[3] = B >> 22;
1149             break;
1150         case PIX_FMT_RGB24:
1151             dest[0] = R >> 22;
1152             dest[1] = G >> 22;
1153             dest[2] = B >> 22;
1154             break;
1155         case PIX_FMT_RGBA:
1156             dest[0] = R >> 22;
1157             dest[1] = G >> 22;
1158             dest[2] = B >> 22;
1159             dest[3] = hasAlpha ? A : 255;
1160             break;
1161         case PIX_FMT_ABGR:
1162             dest[0] = hasAlpha ? A : 255;
1163             dest[1] = B >> 22;
1164             dest[2] = G >> 22;
1165             dest[3] = R >> 22;
1166             dest += 4;
1167             break;
1168         case PIX_FMT_BGR24:
1169             dest[0] = B >> 22;
1170             dest[1] = G >> 22;
1171             dest[2] = R >> 22;
1172             break;
1173         case PIX_FMT_BGRA:
1174             dest[0] = B >> 22;
1175             dest[1] = G >> 22;
1176             dest[2] = R >> 22;
1177             dest[3] = hasAlpha ? A : 255;
1178             break;
1179         }
1180         dest += step;
1181     }
1182 }
1183
1184 #if CONFIG_SMALL
1185 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1186 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1187 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1188 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1189 #else
1190 #if CONFIG_SWSCALE_ALPHA
1191 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA,  1)
1192 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR,  1)
1193 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA,  1)
1194 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB,  1)
1195 #endif
1196 YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, PIX_FMT_BGRA,  0)
1197 YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, PIX_FMT_ABGR,  0)
1198 YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, PIX_FMT_RGBA,  0)
1199 YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, PIX_FMT_ARGB,  0)
1200 #endif
1201 YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full,  PIX_FMT_BGR24, 0)
1202 YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full,  PIX_FMT_RGB24, 0)
1203
1204 void ff_sws_init_output_funcs(SwsContext *c,
1205                               yuv2planar1_fn *yuv2plane1,
1206                               yuv2planarX_fn *yuv2planeX,
1207                               yuv2interleavedX_fn *yuv2nv12cX,
1208                               yuv2packed1_fn *yuv2packed1,
1209                               yuv2packed2_fn *yuv2packed2,
1210                               yuv2packedX_fn *yuv2packedX)
1211 {
1212     enum PixelFormat dstFormat = c->dstFormat;
1213
1214     if (is16BPS(dstFormat)) {
1215         *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c  : yuv2planeX_16LE_c;
1216         *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c  : yuv2plane1_16LE_c;
1217     } else if (is9_OR_10BPS(dstFormat)) {
1218         if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
1219             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c  : yuv2planeX_9LE_c;
1220             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c  : yuv2plane1_9LE_c;
1221         } else {
1222             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c  : yuv2planeX_10LE_c;
1223             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c  : yuv2plane1_10LE_c;
1224         }
1225     } else {
1226         *yuv2plane1 = yuv2plane1_8_c;
1227         *yuv2planeX = yuv2planeX_8_c;
1228         if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)
1229             *yuv2nv12cX = yuv2nv12cX_c;
1230     }
1231
1232     if(c->flags & SWS_FULL_CHR_H_INT) {
1233         switch (dstFormat) {
1234             case PIX_FMT_RGBA:
1235 #if CONFIG_SMALL
1236                 *yuv2packedX = yuv2rgba32_full_X_c;
1237 #else
1238 #if CONFIG_SWSCALE_ALPHA
1239                 if (c->alpPixBuf) {
1240                     *yuv2packedX = yuv2rgba32_full_X_c;
1241                 } else
1242 #endif /* CONFIG_SWSCALE_ALPHA */
1243                 {
1244                     *yuv2packedX = yuv2rgbx32_full_X_c;
1245                 }
1246 #endif /* !CONFIG_SMALL */
1247                 break;
1248             case PIX_FMT_ARGB:
1249 #if CONFIG_SMALL
1250                 *yuv2packedX = yuv2argb32_full_X_c;
1251 #else
1252 #if CONFIG_SWSCALE_ALPHA
1253                 if (c->alpPixBuf) {
1254                     *yuv2packedX = yuv2argb32_full_X_c;
1255                 } else
1256 #endif /* CONFIG_SWSCALE_ALPHA */
1257                 {
1258                     *yuv2packedX = yuv2xrgb32_full_X_c;
1259                 }
1260 #endif /* !CONFIG_SMALL */
1261                 break;
1262             case PIX_FMT_BGRA:
1263 #if CONFIG_SMALL
1264                 *yuv2packedX = yuv2bgra32_full_X_c;
1265 #else
1266 #if CONFIG_SWSCALE_ALPHA
1267                 if (c->alpPixBuf) {
1268                     *yuv2packedX = yuv2bgra32_full_X_c;
1269                 } else
1270 #endif /* CONFIG_SWSCALE_ALPHA */
1271                 {
1272                     *yuv2packedX = yuv2bgrx32_full_X_c;
1273                 }
1274 #endif /* !CONFIG_SMALL */
1275                 break;
1276             case PIX_FMT_ABGR:
1277 #if CONFIG_SMALL
1278                 *yuv2packedX = yuv2abgr32_full_X_c;
1279 #else
1280 #if CONFIG_SWSCALE_ALPHA
1281                 if (c->alpPixBuf) {
1282                     *yuv2packedX = yuv2abgr32_full_X_c;
1283                 } else
1284 #endif /* CONFIG_SWSCALE_ALPHA */
1285                 {
1286                     *yuv2packedX = yuv2xbgr32_full_X_c;
1287                 }
1288 #endif /* !CONFIG_SMALL */
1289                 break;
1290             case PIX_FMT_RGB24:
1291             *yuv2packedX = yuv2rgb24_full_X_c;
1292             break;
1293         case PIX_FMT_BGR24:
1294             *yuv2packedX = yuv2bgr24_full_X_c;
1295             break;
1296         }
1297     } else {
1298         switch (dstFormat) {
1299         case PIX_FMT_RGB48LE:
1300             *yuv2packed1 = yuv2rgb48le_1_c;
1301             *yuv2packed2 = yuv2rgb48le_2_c;
1302             *yuv2packedX = yuv2rgb48le_X_c;
1303             break;
1304         case PIX_FMT_RGB48BE:
1305             *yuv2packed1 = yuv2rgb48be_1_c;
1306             *yuv2packed2 = yuv2rgb48be_2_c;
1307             *yuv2packedX = yuv2rgb48be_X_c;
1308             break;
1309         case PIX_FMT_BGR48LE:
1310             *yuv2packed1 = yuv2bgr48le_1_c;
1311             *yuv2packed2 = yuv2bgr48le_2_c;
1312             *yuv2packedX = yuv2bgr48le_X_c;
1313             break;
1314         case PIX_FMT_BGR48BE:
1315             *yuv2packed1 = yuv2bgr48be_1_c;
1316             *yuv2packed2 = yuv2bgr48be_2_c;
1317             *yuv2packedX = yuv2bgr48be_X_c;
1318             break;
1319         case PIX_FMT_RGB32:
1320         case PIX_FMT_BGR32:
1321 #if CONFIG_SMALL
1322             *yuv2packed1 = yuv2rgb32_1_c;
1323             *yuv2packed2 = yuv2rgb32_2_c;
1324             *yuv2packedX = yuv2rgb32_X_c;
1325 #else
1326 #if CONFIG_SWSCALE_ALPHA
1327                 if (c->alpPixBuf) {
1328                     *yuv2packed1 = yuv2rgba32_1_c;
1329                     *yuv2packed2 = yuv2rgba32_2_c;
1330                     *yuv2packedX = yuv2rgba32_X_c;
1331                 } else
1332 #endif /* CONFIG_SWSCALE_ALPHA */
1333                 {
1334                     *yuv2packed1 = yuv2rgbx32_1_c;
1335                     *yuv2packed2 = yuv2rgbx32_2_c;
1336                     *yuv2packedX = yuv2rgbx32_X_c;
1337                 }
1338 #endif /* !CONFIG_SMALL */
1339             break;
1340         case PIX_FMT_RGB32_1:
1341         case PIX_FMT_BGR32_1:
1342 #if CONFIG_SMALL
1343                 *yuv2packed1 = yuv2rgb32_1_1_c;
1344                 *yuv2packed2 = yuv2rgb32_1_2_c;
1345                 *yuv2packedX = yuv2rgb32_1_X_c;
1346 #else
1347 #if CONFIG_SWSCALE_ALPHA
1348                 if (c->alpPixBuf) {
1349                     *yuv2packed1 = yuv2rgba32_1_1_c;
1350                     *yuv2packed2 = yuv2rgba32_1_2_c;
1351                     *yuv2packedX = yuv2rgba32_1_X_c;
1352                 } else
1353 #endif /* CONFIG_SWSCALE_ALPHA */
1354                 {
1355                     *yuv2packed1 = yuv2rgbx32_1_1_c;
1356                     *yuv2packed2 = yuv2rgbx32_1_2_c;
1357                     *yuv2packedX = yuv2rgbx32_1_X_c;
1358                 }
1359 #endif /* !CONFIG_SMALL */
1360                 break;
1361         case PIX_FMT_RGB24:
1362             *yuv2packed1 = yuv2rgb24_1_c;
1363             *yuv2packed2 = yuv2rgb24_2_c;
1364             *yuv2packedX = yuv2rgb24_X_c;
1365             break;
1366         case PIX_FMT_BGR24:
1367             *yuv2packed1 = yuv2bgr24_1_c;
1368             *yuv2packed2 = yuv2bgr24_2_c;
1369             *yuv2packedX = yuv2bgr24_X_c;
1370             break;
1371         case PIX_FMT_RGB565LE:
1372         case PIX_FMT_RGB565BE:
1373         case PIX_FMT_BGR565LE:
1374         case PIX_FMT_BGR565BE:
1375             *yuv2packed1 = yuv2rgb16_1_c;
1376             *yuv2packed2 = yuv2rgb16_2_c;
1377             *yuv2packedX = yuv2rgb16_X_c;
1378             break;
1379         case PIX_FMT_RGB555LE:
1380         case PIX_FMT_RGB555BE:
1381         case PIX_FMT_BGR555LE:
1382         case PIX_FMT_BGR555BE:
1383             *yuv2packed1 = yuv2rgb15_1_c;
1384             *yuv2packed2 = yuv2rgb15_2_c;
1385             *yuv2packedX = yuv2rgb15_X_c;
1386             break;
1387         case PIX_FMT_RGB444LE:
1388         case PIX_FMT_RGB444BE:
1389         case PIX_FMT_BGR444LE:
1390         case PIX_FMT_BGR444BE:
1391             *yuv2packed1 = yuv2rgb12_1_c;
1392             *yuv2packed2 = yuv2rgb12_2_c;
1393             *yuv2packedX = yuv2rgb12_X_c;
1394             break;
1395         case PIX_FMT_RGB8:
1396         case PIX_FMT_BGR8:
1397             *yuv2packed1 = yuv2rgb8_1_c;
1398             *yuv2packed2 = yuv2rgb8_2_c;
1399             *yuv2packedX = yuv2rgb8_X_c;
1400             break;
1401         case PIX_FMT_RGB4:
1402         case PIX_FMT_BGR4:
1403             *yuv2packed1 = yuv2rgb4_1_c;
1404             *yuv2packed2 = yuv2rgb4_2_c;
1405             *yuv2packedX = yuv2rgb4_X_c;
1406             break;
1407         case PIX_FMT_RGB4_BYTE:
1408         case PIX_FMT_BGR4_BYTE:
1409             *yuv2packed1 = yuv2rgb4b_1_c;
1410             *yuv2packed2 = yuv2rgb4b_2_c;
1411             *yuv2packedX = yuv2rgb4b_X_c;
1412             break;
1413         }
1414     }
1415     switch (dstFormat) {
1416     case PIX_FMT_MONOWHITE:
1417         *yuv2packed1 = yuv2monowhite_1_c;
1418         *yuv2packed2 = yuv2monowhite_2_c;
1419         *yuv2packedX = yuv2monowhite_X_c;
1420         break;
1421     case PIX_FMT_MONOBLACK:
1422         *yuv2packed1 = yuv2monoblack_1_c;
1423         *yuv2packed2 = yuv2monoblack_2_c;
1424         *yuv2packedX = yuv2monoblack_X_c;
1425         break;
1426     case PIX_FMT_YUYV422:
1427         *yuv2packed1 = yuv2yuyv422_1_c;
1428         *yuv2packed2 = yuv2yuyv422_2_c;
1429         *yuv2packedX = yuv2yuyv422_X_c;
1430         break;
1431     case PIX_FMT_UYVY422:
1432         *yuv2packed1 = yuv2uyvy422_1_c;
1433         *yuv2packed2 = yuv2uyvy422_2_c;
1434         *yuv2packedX = yuv2uyvy422_X_c;
1435         break;
1436     }
1437 }