]> git.sesse.net Git - ffmpeg/blob - libswscale/output.c
Merge commit '1f77e634bb838f71ff21923b5e9fe3104c831c52'
[ffmpeg] / libswscale / output.c
1 /*
2  * Copyright (C) 2001-2012 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <math.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/avutil.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/bswap.h"
30 #include "libavutil/cpu.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mathematics.h"
33 #include "libavutil/pixdesc.h"
34 #include "config.h"
35 #include "rgb2rgb.h"
36 #include "swscale.h"
37 #include "swscale_internal.h"
38
39 DECLARE_ALIGNED(8, const uint8_t, ff_dither_2x2_4)[][8] = {
40 {  1,   3,   1,   3,   1,   3,   1,   3, },
41 {  2,   0,   2,   0,   2,   0,   2,   0, },
42 {  1,   3,   1,   3,   1,   3,   1,   3, },
43 };
44
45 DECLARE_ALIGNED(8, const uint8_t, ff_dither_2x2_8)[][8] = {
46 {  6,   2,   6,   2,   6,   2,   6,   2, },
47 {  0,   4,   0,   4,   0,   4,   0,   4, },
48 {  6,   2,   6,   2,   6,   2,   6,   2, },
49 };
50
51 DECLARE_ALIGNED(8, const uint8_t, ff_dither_4x4_16)[][8] = {
52 {  8,   4,  11,   7,   8,   4,  11,   7, },
53 {  2,  14,   1,  13,   2,  14,   1,  13, },
54 { 10,   6,   9,   5,  10,   6,   9,   5, },
55 {  0,  12,   3,  15,   0,  12,   3,  15, },
56 {  8,   4,  11,   7,   8,   4,  11,   7, },
57 };
58
59 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_32)[][8] = {
60 { 17,   9,  23,  15,  16,   8,  22,  14, },
61 {  5,  29,   3,  27,   4,  28,   2,  26, },
62 { 21,  13,  19,  11,  20,  12,  18,  10, },
63 {  0,  24,   6,  30,   1,  25,   7,  31, },
64 { 16,   8,  22,  14,  17,   9,  23,  15, },
65 {  4,  28,   2,  26,   5,  29,   3,  27, },
66 { 20,  12,  18,  10,  21,  13,  19,  11, },
67 {  1,  25,   7,  31,   0,  24,   6,  30, },
68 { 17,   9,  23,  15,  16,   8,  22,  14, },
69 };
70
71 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_73)[][8] = {
72 {  0,  55,  14,  68,   3,  58,  17,  72, },
73 { 37,  18,  50,  32,  40,  22,  54,  35, },
74 {  9,  64,   5,  59,  13,  67,   8,  63, },
75 { 46,  27,  41,  23,  49,  31,  44,  26, },
76 {  2,  57,  16,  71,   1,  56,  15,  70, },
77 { 39,  21,  52,  34,  38,  19,  51,  33, },
78 { 11,  66,   7,  62,  10,  65,   6,  60, },
79 { 48,  30,  43,  25,  47,  29,  42,  24, },
80 {  0,  55,  14,  68,   3,  58,  17,  72, },
81 };
82
83 #if 1
84 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
85 {117,  62, 158, 103, 113,  58, 155, 100, },
86 { 34, 199,  21, 186,  31, 196,  17, 182, },
87 {144,  89, 131,  76, 141,  86, 127,  72, },
88 {  0, 165,  41, 206,  10, 175,  52, 217, },
89 {110,  55, 151,  96, 120,  65, 162, 107, },
90 { 28, 193,  14, 179,  38, 203,  24, 189, },
91 {138,  83, 124,  69, 148,  93, 134,  79, },
92 {  7, 172,  48, 213,   3, 168,  45, 210, },
93 {117,  62, 158, 103, 113,  58, 155, 100, },
94 };
95 #elif 1
96 // tries to correct a gamma of 1.5
97 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
98 {  0, 143,  18, 200,   2, 156,  25, 215, },
99 { 78,  28, 125,  64,  89,  36, 138,  74, },
100 { 10, 180,   3, 161,  16, 195,   8, 175, },
101 {109,  51,  93,  38, 121,  60, 105,  47, },
102 {  1, 152,  23, 210,   0, 147,  20, 205, },
103 { 85,  33, 134,  71,  81,  30, 130,  67, },
104 { 14, 190,   6, 171,  12, 185,   5, 166, },
105 {117,  57, 101,  44, 113,  54,  97,  41, },
106 {  0, 143,  18, 200,   2, 156,  25, 215, },
107 };
108 #elif 1
109 // tries to correct a gamma of 2.0
110 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
111 {  0, 124,   8, 193,   0, 140,  12, 213, },
112 { 55,  14, 104,  42,  66,  19, 119,  52, },
113 {  3, 168,   1, 145,   6, 187,   3, 162, },
114 { 86,  31,  70,  21,  99,  39,  82,  28, },
115 {  0, 134,  11, 206,   0, 129,   9, 200, },
116 { 62,  17, 114,  48,  58,  16, 109,  45, },
117 {  5, 181,   2, 157,   4, 175,   1, 151, },
118 { 95,  36,  78,  26,  90,  34,  74,  24, },
119 {  0, 124,   8, 193,   0, 140,  12, 213, },
120 };
121 #else
122 // tries to correct a gamma of 2.5
123 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[][8] = {
124 {  0, 107,   3, 187,   0, 125,   6, 212, },
125 { 39,   7,  86,  28,  49,  11, 102,  36, },
126 {  1, 158,   0, 131,   3, 180,   1, 151, },
127 { 68,  19,  52,  12,  81,  25,  64,  17, },
128 {  0, 119,   5, 203,   0, 113,   4, 195, },
129 { 45,   9,  96,  33,  42,   8,  91,  30, },
130 {  2, 172,   1, 144,   2, 165,   0, 137, },
131 { 77,  23,  60,  15,  72,  21,  56,  14, },
132 {  0, 107,   3, 187,   0, 125,   6, 212, },
133 };
134 #endif
135
136 #define output_pixel(pos, val, bias, signedness) \
137     if (big_endian) { \
138         AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
139     } else { \
140         AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
141     }
142
143 static av_always_inline void
144 yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
145                          int big_endian, int output_bits)
146 {
147     int i;
148     int shift = 3;
149     av_assert0(output_bits == 16);
150
151     for (i = 0; i < dstW; i++) {
152         int val = src[i] + (1 << (shift - 1));
153         output_pixel(&dest[i], val, 0, uint);
154     }
155 }
156
157 static av_always_inline void
158 yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
159                          const int32_t **src, uint16_t *dest, int dstW,
160                          int big_endian, int output_bits)
161 {
162     int i;
163     int shift = 15;
164     av_assert0(output_bits == 16);
165
166     for (i = 0; i < dstW; i++) {
167         int val = 1 << (shift - 1);
168         int j;
169
170         /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
171          * filters (or anything with negative coeffs, the range can be slightly
172          * wider in both directions. To account for this overflow, we subtract
173          * a constant so it always fits in the signed range (assuming a
174          * reasonable filterSize), and re-add that at the end. */
175         val -= 0x40000000;
176         for (j = 0; j < filterSize; j++)
177             val += src[j][i] * (unsigned)filter[j];
178
179         output_pixel(&dest[i], val, 0x8000, int);
180     }
181 }
182
183 #undef output_pixel
184
185 #define output_pixel(pos, val) \
186     if (big_endian) { \
187         AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
188     } else { \
189         AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
190     }
191
192 static av_always_inline void
193 yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
194                          int big_endian, int output_bits)
195 {
196     int i;
197     int shift = 15 - output_bits;
198
199     for (i = 0; i < dstW; i++) {
200         int val = src[i] + (1 << (shift - 1));
201         output_pixel(&dest[i], val);
202     }
203 }
204
205 static av_always_inline void
206 yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
207                          const int16_t **src, uint16_t *dest, int dstW,
208                          int big_endian, int output_bits)
209 {
210     int i;
211     int shift = 11 + 16 - output_bits;
212
213     for (i = 0; i < dstW; i++) {
214         int val = 1 << (shift - 1);
215         int j;
216
217         for (j = 0; j < filterSize; j++)
218             val += src[j][i] * filter[j];
219
220         output_pixel(&dest[i], val);
221     }
222 }
223
224 #undef output_pixel
225
226 #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
227 static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
228                               uint8_t *dest, int dstW, \
229                               const uint8_t *dither, int offset)\
230 { \
231     yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
232                          (uint16_t *) dest, dstW, is_be, bits); \
233 }\
234 static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
235                               const int16_t **src, uint8_t *dest, int dstW, \
236                               const uint8_t *dither, int offset)\
237 { \
238     yuv2planeX_## template_size ## _c_template(filter, \
239                          filterSize, (const typeX_t **) src, \
240                          (uint16_t *) dest, dstW, is_be, bits); \
241 }
242 yuv2NBPS( 9, BE, 1, 10, int16_t)
243 yuv2NBPS( 9, LE, 0, 10, int16_t)
244 yuv2NBPS(10, BE, 1, 10, int16_t)
245 yuv2NBPS(10, LE, 0, 10, int16_t)
246 yuv2NBPS(12, BE, 1, 10, int16_t)
247 yuv2NBPS(12, LE, 0, 10, int16_t)
248 yuv2NBPS(14, BE, 1, 10, int16_t)
249 yuv2NBPS(14, LE, 0, 10, int16_t)
250 yuv2NBPS(16, BE, 1, 16, int32_t)
251 yuv2NBPS(16, LE, 0, 16, int32_t)
252
253 static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
254                            const int16_t **src, uint8_t *dest, int dstW,
255                            const uint8_t *dither, int offset)
256 {
257     int i;
258     for (i=0; i<dstW; i++) {
259         int val = dither[(i + offset) & 7] << 12;
260         int j;
261         for (j=0; j<filterSize; j++)
262             val += src[j][i] * filter[j];
263
264         dest[i]= av_clip_uint8(val>>19);
265     }
266 }
267
268 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
269                            const uint8_t *dither, int offset)
270 {
271     int i;
272     for (i=0; i<dstW; i++) {
273         int val = (src[i] + dither[(i + offset) & 7]) >> 7;
274         dest[i]= av_clip_uint8(val);
275     }
276 }
277
278 static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
279                         const int16_t **chrUSrc, const int16_t **chrVSrc,
280                         uint8_t *dest, int chrDstW)
281 {
282     enum AVPixelFormat dstFormat = c->dstFormat;
283     const uint8_t *chrDither = c->chrDither8;
284     int i;
285
286     if (dstFormat == AV_PIX_FMT_NV12)
287         for (i=0; i<chrDstW; i++) {
288             int u = chrDither[i & 7] << 12;
289             int v = chrDither[(i + 3) & 7] << 12;
290             int j;
291             for (j=0; j<chrFilterSize; j++) {
292                 u += chrUSrc[j][i] * chrFilter[j];
293                 v += chrVSrc[j][i] * chrFilter[j];
294             }
295
296             dest[2*i]= av_clip_uint8(u>>19);
297             dest[2*i+1]= av_clip_uint8(v>>19);
298         }
299     else
300         for (i=0; i<chrDstW; i++) {
301             int u = chrDither[i & 7] << 12;
302             int v = chrDither[(i + 3) & 7] << 12;
303             int j;
304             for (j=0; j<chrFilterSize; j++) {
305                 u += chrUSrc[j][i] * chrFilter[j];
306                 v += chrVSrc[j][i] * chrFilter[j];
307             }
308
309             dest[2*i]= av_clip_uint8(v>>19);
310             dest[2*i+1]= av_clip_uint8(u>>19);
311         }
312 }
313
314 #define accumulate_bit(acc, val) \
315     acc <<= 1; \
316     acc |= (val) >= 234
317 #define output_pixel(pos, acc) \
318     if (target == AV_PIX_FMT_MONOBLACK) { \
319         pos = acc; \
320     } else { \
321         pos = ~acc; \
322     }
323
324 static av_always_inline void
325 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
326                       const int16_t **lumSrc, int lumFilterSize,
327                       const int16_t *chrFilter, const int16_t **chrUSrc,
328                       const int16_t **chrVSrc, int chrFilterSize,
329                       const int16_t **alpSrc, uint8_t *dest, int dstW,
330                       int y, enum AVPixelFormat target)
331 {
332     const uint8_t * const d128 = ff_dither_8x8_220[y&7];
333     int i;
334     unsigned acc = 0;
335     int err = 0;
336
337     for (i = 0; i < dstW; i += 2) {
338         int j;
339         int Y1 = 1 << 18;
340         int Y2 = 1 << 18;
341
342         for (j = 0; j < lumFilterSize; j++) {
343             Y1 += lumSrc[j][i]   * lumFilter[j];
344             Y2 += lumSrc[j][i+1] * lumFilter[j];
345         }
346         Y1 >>= 19;
347         Y2 >>= 19;
348         if ((Y1 | Y2) & 0x100) {
349             Y1 = av_clip_uint8(Y1);
350             Y2 = av_clip_uint8(Y2);
351         }
352         if (c->dither == SWS_DITHER_ED) {
353             Y1 += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
354             c->dither_error[0][i] = err;
355             acc = 2*acc + (Y1 >= 128);
356             Y1 -= 220*(acc&1);
357
358             err = Y2 + ((7*Y1 + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4);
359             c->dither_error[0][i+1] = Y1;
360             acc = 2*acc + (err >= 128);
361             err -= 220*(acc&1);
362         } else {
363             accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
364             accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
365         }
366         if ((i & 7) == 6) {
367             output_pixel(*dest++, acc);
368         }
369     }
370     c->dither_error[0][i] = err;
371
372     if (i & 6) {
373         output_pixel(*dest, acc);
374     }
375 }
376
377 static av_always_inline void
378 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
379                       const int16_t *ubuf[2], const int16_t *vbuf[2],
380                       const int16_t *abuf[2], uint8_t *dest, int dstW,
381                       int yalpha, int uvalpha, int y,
382                       enum AVPixelFormat target)
383 {
384     const int16_t *buf0  = buf[0],  *buf1  = buf[1];
385     const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
386     int  yalpha1 = 4096 - yalpha;
387     int i;
388     av_assert2(yalpha  <= 4096U);
389
390     if (c->dither == SWS_DITHER_ED) {
391         int err = 0;
392         int acc = 0;
393         for (i = 0; i < dstW; i +=2) {
394             int Y;
395
396             Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
397             Y += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
398             c->dither_error[0][i] = err;
399             acc = 2*acc + (Y >= 128);
400             Y -= 220*(acc&1);
401
402             err = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
403             err += (7*Y + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4;
404             c->dither_error[0][i+1] = Y;
405             acc = 2*acc + (err >= 128);
406             err -= 220*(acc&1);
407
408             if ((i & 7) == 6)
409                 output_pixel(*dest++, acc);
410         }
411         c->dither_error[0][i] = err;
412     } else {
413     for (i = 0; i < dstW; i += 8) {
414         int Y, acc = 0;
415
416         Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
417         accumulate_bit(acc, Y + d128[0]);
418         Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
419         accumulate_bit(acc, Y + d128[1]);
420         Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
421         accumulate_bit(acc, Y + d128[2]);
422         Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
423         accumulate_bit(acc, Y + d128[3]);
424         Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
425         accumulate_bit(acc, Y + d128[4]);
426         Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
427         accumulate_bit(acc, Y + d128[5]);
428         Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
429         accumulate_bit(acc, Y + d128[6]);
430         Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
431         accumulate_bit(acc, Y + d128[7]);
432
433         output_pixel(*dest++, acc);
434     }
435     }
436 }
437
438 static av_always_inline void
439 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
440                       const int16_t *ubuf[2], const int16_t *vbuf[2],
441                       const int16_t *abuf0, uint8_t *dest, int dstW,
442                       int uvalpha, int y, enum AVPixelFormat target)
443 {
444     const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
445     int i;
446
447     if (c->dither == SWS_DITHER_ED) {
448         int err = 0;
449         int acc = 0;
450         for (i = 0; i < dstW; i +=2) {
451             int Y;
452
453             Y = ((buf0[i + 0] + 64) >> 7);
454             Y += (7*err + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2] + 8 - 256)>>4;
455             c->dither_error[0][i] = err;
456             acc = 2*acc + (Y >= 128);
457             Y -= 220*(acc&1);
458
459             err = ((buf0[i + 1] + 64) >> 7);
460             err += (7*Y + 1*c->dither_error[0][i+1] + 5*c->dither_error[0][i+2] + 3*c->dither_error[0][i+3] + 8 - 256)>>4;
461             c->dither_error[0][i+1] = Y;
462             acc = 2*acc + (err >= 128);
463             err -= 220*(acc&1);
464
465             if ((i & 7) == 6)
466                 output_pixel(*dest++, acc);
467         }
468         c->dither_error[0][i] = err;
469     } else {
470     for (i = 0; i < dstW; i += 8) {
471         int acc = 0;
472         accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
473         accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
474         accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
475         accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
476         accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
477         accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
478         accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
479         accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
480
481         output_pixel(*dest++, acc);
482     }
483     }
484 }
485
486 #undef output_pixel
487 #undef accumulate_bit
488
489 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
490 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
491                                 const int16_t **lumSrc, int lumFilterSize, \
492                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
493                                 const int16_t **chrVSrc, int chrFilterSize, \
494                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
495                                 int y) \
496 { \
497     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
498                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
499                                   alpSrc, dest, dstW, y, fmt); \
500 } \
501  \
502 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
503                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
504                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
505                                 int yalpha, int uvalpha, int y) \
506 { \
507     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
508                                   dest, dstW, yalpha, uvalpha, y, fmt); \
509 } \
510  \
511 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
512                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
513                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
514                                 int uvalpha, int y) \
515 { \
516     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
517                                   abuf0, dest, dstW, uvalpha, \
518                                   y, fmt); \
519 }
520
521 YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
522 YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
523
524 #define output_pixels(pos, Y1, U, Y2, V) \
525     if (target == AV_PIX_FMT_YUYV422) { \
526         dest[pos + 0] = Y1; \
527         dest[pos + 1] = U;  \
528         dest[pos + 2] = Y2; \
529         dest[pos + 3] = V;  \
530     } else if (target == AV_PIX_FMT_YVYU422) { \
531         dest[pos + 0] = Y1; \
532         dest[pos + 1] = V;  \
533         dest[pos + 2] = Y2; \
534         dest[pos + 3] = U;  \
535     } else { /* AV_PIX_FMT_UYVY422 */ \
536         dest[pos + 0] = U;  \
537         dest[pos + 1] = Y1; \
538         dest[pos + 2] = V;  \
539         dest[pos + 3] = Y2; \
540     }
541
542 static av_always_inline void
543 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
544                      const int16_t **lumSrc, int lumFilterSize,
545                      const int16_t *chrFilter, const int16_t **chrUSrc,
546                      const int16_t **chrVSrc, int chrFilterSize,
547                      const int16_t **alpSrc, uint8_t *dest, int dstW,
548                      int y, enum AVPixelFormat target)
549 {
550     int i;
551
552     for (i = 0; i < ((dstW + 1) >> 1); i++) {
553         int j;
554         int Y1 = 1 << 18;
555         int Y2 = 1 << 18;
556         int U  = 1 << 18;
557         int V  = 1 << 18;
558
559         for (j = 0; j < lumFilterSize; j++) {
560             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
561             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
562         }
563         for (j = 0; j < chrFilterSize; j++) {
564             U += chrUSrc[j][i] * chrFilter[j];
565             V += chrVSrc[j][i] * chrFilter[j];
566         }
567         Y1 >>= 19;
568         Y2 >>= 19;
569         U  >>= 19;
570         V  >>= 19;
571         if ((Y1 | Y2 | U | V) & 0x100) {
572             Y1 = av_clip_uint8(Y1);
573             Y2 = av_clip_uint8(Y2);
574             U  = av_clip_uint8(U);
575             V  = av_clip_uint8(V);
576         }
577         output_pixels(4*i, Y1, U, Y2, V);
578     }
579 }
580
581 static av_always_inline void
582 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
583                      const int16_t *ubuf[2], const int16_t *vbuf[2],
584                      const int16_t *abuf[2], uint8_t *dest, int dstW,
585                      int yalpha, int uvalpha, int y,
586                      enum AVPixelFormat target)
587 {
588     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
589                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
590                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
591     int  yalpha1 = 4096 - yalpha;
592     int uvalpha1 = 4096 - uvalpha;
593     int i;
594     av_assert2(yalpha  <= 4096U);
595     av_assert2(uvalpha <= 4096U);
596
597     for (i = 0; i < ((dstW + 1) >> 1); i++) {
598         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
599         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
600         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
601         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
602
603         if ((Y1 | Y2 | U | V) & 0x100) {
604             Y1 = av_clip_uint8(Y1);
605             Y2 = av_clip_uint8(Y2);
606             U  = av_clip_uint8(U);
607             V  = av_clip_uint8(V);
608         }
609
610         output_pixels(i * 4, Y1, U, Y2, V);
611     }
612 }
613
614 static av_always_inline void
615 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
616                      const int16_t *ubuf[2], const int16_t *vbuf[2],
617                      const int16_t *abuf0, uint8_t *dest, int dstW,
618                      int uvalpha, int y, enum AVPixelFormat target)
619 {
620     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
621     int i;
622
623     if (uvalpha < 2048) {
624         for (i = 0; i < ((dstW + 1) >> 1); i++) {
625             int Y1 = (buf0[i * 2    ]+64) >> 7;
626             int Y2 = (buf0[i * 2 + 1]+64) >> 7;
627             int U  = (ubuf0[i]       +64) >> 7;
628             int V  = (vbuf0[i]       +64) >> 7;
629
630             if ((Y1 | Y2 | U | V) & 0x100) {
631                 Y1 = av_clip_uint8(Y1);
632                 Y2 = av_clip_uint8(Y2);
633                 U  = av_clip_uint8(U);
634                 V  = av_clip_uint8(V);
635             }
636
637             Y1 = av_clip_uint8(Y1);
638             Y2 = av_clip_uint8(Y2);
639             U  = av_clip_uint8(U);
640             V  = av_clip_uint8(V);
641
642             output_pixels(i * 4, Y1, U, Y2, V);
643         }
644     } else {
645         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
646         for (i = 0; i < ((dstW + 1) >> 1); i++) {
647             int Y1 = (buf0[i * 2    ]    + 64) >> 7;
648             int Y2 = (buf0[i * 2 + 1]    + 64) >> 7;
649             int U  = (ubuf0[i] + ubuf1[i]+128) >> 8;
650             int V  = (vbuf0[i] + vbuf1[i]+128) >> 8;
651
652             if ((Y1 | Y2 | U | V) & 0x100) {
653                 Y1 = av_clip_uint8(Y1);
654                 Y2 = av_clip_uint8(Y2);
655                 U  = av_clip_uint8(U);
656                 V  = av_clip_uint8(V);
657             }
658
659             Y1 = av_clip_uint8(Y1);
660             Y2 = av_clip_uint8(Y2);
661             U  = av_clip_uint8(U);
662             V  = av_clip_uint8(V);
663
664             output_pixels(i * 4, Y1, U, Y2, V);
665         }
666     }
667 }
668
669 #undef output_pixels
670
671 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
672 YUV2PACKEDWRAPPER(yuv2, 422, yvyu422, AV_PIX_FMT_YVYU422)
673 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
674
675 #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE || target == AV_PIX_FMT_RGBA64LE || target == AV_PIX_FMT_RGBA64BE) ? R : B)
676 #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE || target == AV_PIX_FMT_RGBA64LE || target == AV_PIX_FMT_RGBA64BE) ? B : R)
677 #define output_pixel(pos, val) \
678     if (isBE(target)) { \
679         AV_WB16(pos, val); \
680     } else { \
681         AV_WL16(pos, val); \
682     }
683
684 static av_always_inline void
685 yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter,
686                        const int32_t **lumSrc, int lumFilterSize,
687                        const int16_t *chrFilter, const int32_t **chrUSrc,
688                        const int32_t **chrVSrc, int chrFilterSize,
689                        const int32_t **alpSrc, uint16_t *dest, int dstW,
690                        int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
691 {
692     int i;
693     int A1 = 0xffff<<14, A2 = 0xffff<<14;
694
695     for (i = 0; i < ((dstW + 1) >> 1); i++) {
696         int j;
697         int Y1 = -0x40000000;
698         int Y2 = -0x40000000;
699         int U  = -(128 << 23); // 19
700         int V  = -(128 << 23);
701         int R, G, B;
702
703         for (j = 0; j < lumFilterSize; j++) {
704             Y1 += lumSrc[j][i * 2]     * (unsigned)lumFilter[j];
705             Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
706         }
707         for (j = 0; j < chrFilterSize; j++) {;
708             U += chrUSrc[j][i] * (unsigned)chrFilter[j];
709             V += chrVSrc[j][i] * (unsigned)chrFilter[j];
710         }
711
712         if (hasAlpha) {
713             A1 = -0x40000000;
714             A2 = -0x40000000;
715             for (j = 0; j < lumFilterSize; j++) {
716                 A1 += alpSrc[j][i * 2]     * (unsigned)lumFilter[j];
717                 A2 += alpSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
718             }
719             A1 >>= 1;
720             A1 += 0x20002000;
721             A2 >>= 1;
722             A2 += 0x20002000;
723         }
724
725         // 8 bits: 12+15=27; 16 bits: 12+19=31
726         Y1 >>= 14; // 10
727         Y1 += 0x10000;
728         Y2 >>= 14;
729         Y2 += 0x10000;
730         U  >>= 14;
731         V  >>= 14;
732
733         // 8 bits: 27 -> 17 bits, 16 bits: 31 - 14 = 17 bits
734         Y1 -= c->yuv2rgb_y_offset;
735         Y2 -= c->yuv2rgb_y_offset;
736         Y1 *= c->yuv2rgb_y_coeff;
737         Y2 *= c->yuv2rgb_y_coeff;
738         Y1 += 1 << 13; // 21
739         Y2 += 1 << 13;
740         // 8 bits: 17 + 13 bits = 30 bits, 16 bits: 17 + 13 bits = 30 bits
741
742         R = V * c->yuv2rgb_v2r_coeff;
743         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
744         B =                            U * c->yuv2rgb_u2b_coeff;
745
746         // 8 bits: 30 - 22 = 8 bits, 16 bits: 30 bits - 14 = 16 bits
747         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
748         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
749         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
750         if (eightbytes) {
751             output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
752             output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
753             output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
754             output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
755             output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
756             dest += 8;
757         } else {
758             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
759             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
760             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
761             dest += 6;
762         }
763     }
764 }
765
766 static av_always_inline void
767 yuv2rgba64_2_c_template(SwsContext *c, const int32_t *buf[2],
768                        const int32_t *ubuf[2], const int32_t *vbuf[2],
769                        const int32_t *abuf[2], uint16_t *dest, int dstW,
770                        int yalpha, int uvalpha, int y,
771                        enum AVPixelFormat target, int hasAlpha, int eightbytes)
772 {
773     const int32_t *buf0  = buf[0],  *buf1  = buf[1],
774                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
775                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
776                   *abuf0 = hasAlpha ? abuf[0] : NULL,
777                   *abuf1 = hasAlpha ? abuf[1] : NULL;
778     int  yalpha1 = 4096 - yalpha;
779     int uvalpha1 = 4096 - uvalpha;
780     int i;
781     int A1 = 0xffff<<14, A2 = 0xffff<<14;
782
783     av_assert2(yalpha  <= 4096U);
784     av_assert2(uvalpha <= 4096U);
785
786     for (i = 0; i < ((dstW + 1) >> 1); i++) {
787         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha) >> 14;
788         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha) >> 14;
789         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha - (128 << 23)) >> 14;
790         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha - (128 << 23)) >> 14;
791         int R, G, B;
792
793         Y1 -= c->yuv2rgb_y_offset;
794         Y2 -= c->yuv2rgb_y_offset;
795         Y1 *= c->yuv2rgb_y_coeff;
796         Y2 *= c->yuv2rgb_y_coeff;
797         Y1 += 1 << 13;
798         Y2 += 1 << 13;
799
800         R = V * c->yuv2rgb_v2r_coeff;
801         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
802         B =                            U * c->yuv2rgb_u2b_coeff;
803
804         if (hasAlpha) {
805             A1 = (abuf0[i * 2    ] * yalpha1 + abuf1[i * 2    ] * yalpha) >> 1;
806             A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 1;
807
808             A1 += 1 << 13;
809             A2 += 1 << 13;
810         }
811
812         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
813         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
814         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
815         if (eightbytes) {
816             output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
817             output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
818             output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
819             output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
820             output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
821             dest += 8;
822         } else {
823             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
824             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
825             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
826             dest += 6;
827         }
828     }
829 }
830
831 static av_always_inline void
832 yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0,
833                        const int32_t *ubuf[2], const int32_t *vbuf[2],
834                        const int32_t *abuf0, uint16_t *dest, int dstW,
835                        int uvalpha, int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
836 {
837     const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
838     int i;
839     int A1 = 0xffff<<14, A2= 0xffff<<14;
840
841     if (uvalpha < 2048) {
842         for (i = 0; i < ((dstW + 1) >> 1); i++) {
843             int Y1 = (buf0[i * 2]    ) >> 2;
844             int Y2 = (buf0[i * 2 + 1]) >> 2;
845             int U  = (ubuf0[i] - (128 << 11)) >> 2;
846             int V  = (vbuf0[i] - (128 << 11)) >> 2;
847             int R, G, B;
848
849             Y1 -= c->yuv2rgb_y_offset;
850             Y2 -= c->yuv2rgb_y_offset;
851             Y1 *= c->yuv2rgb_y_coeff;
852             Y2 *= c->yuv2rgb_y_coeff;
853             Y1 += 1 << 13;
854             Y2 += 1 << 13;
855
856             if (hasAlpha) {
857                 A1 = abuf0[i * 2    ] << 11;
858                 A2 = abuf0[i * 2 + 1] << 11;
859
860                 A1 += 1 << 13;
861                 A2 += 1 << 13;
862             }
863
864             R = V * c->yuv2rgb_v2r_coeff;
865             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
866             B =                            U * c->yuv2rgb_u2b_coeff;
867
868             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
869             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
870             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
871             if (eightbytes) {
872                 output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
873                 output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
874                 output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
875                 output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
876                 output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
877                 dest += 8;
878             } else {
879                 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
880                 output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
881                 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
882                 dest += 6;
883             }
884         }
885     } else {
886         const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
887         int A1 = 0xffff<<14, A2 = 0xffff<<14;
888         for (i = 0; i < ((dstW + 1) >> 1); i++) {
889             int Y1 = (buf0[i * 2]    ) >> 2;
890             int Y2 = (buf0[i * 2 + 1]) >> 2;
891             int U  = (ubuf0[i] + ubuf1[i] - (128 << 12)) >> 3;
892             int V  = (vbuf0[i] + vbuf1[i] - (128 << 12)) >> 3;
893             int R, G, B;
894
895             Y1 -= c->yuv2rgb_y_offset;
896             Y2 -= c->yuv2rgb_y_offset;
897             Y1 *= c->yuv2rgb_y_coeff;
898             Y2 *= c->yuv2rgb_y_coeff;
899             Y1 += 1 << 13;
900             Y2 += 1 << 13;
901
902             if (hasAlpha) {
903                 A1 = abuf0[i * 2    ] << 11;
904                 A2 = abuf0[i * 2 + 1] << 11;
905
906                 A1 += 1 << 13;
907                 A2 += 1 << 13;
908             }
909
910             R = V * c->yuv2rgb_v2r_coeff;
911             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
912             B =                            U * c->yuv2rgb_u2b_coeff;
913
914             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
915             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
916             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
917             if (eightbytes) {
918                 output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
919                 output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
920                 output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
921                 output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
922                 output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
923                 dest += 8;
924             } else {
925                 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
926                 output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
927                 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
928                 dest += 6;
929             }
930         }
931     }
932 }
933
934 static av_always_inline void
935 yuv2rgba64_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
936                        const int32_t **lumSrc, int lumFilterSize,
937                        const int16_t *chrFilter, const int32_t **chrUSrc,
938                        const int32_t **chrVSrc, int chrFilterSize,
939                        const int32_t **alpSrc, uint16_t *dest, int dstW,
940                        int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
941 {
942     int i;
943     int A = 0xffff<<14;
944
945     for (i = 0; i < dstW; i++) {
946         int j;
947         int Y  = -0x40000000;
948         int U  = -(128 << 23); // 19
949         int V  = -(128 << 23);
950         int R, G, B;
951
952         for (j = 0; j < lumFilterSize; j++) {
953             Y += lumSrc[j][i]  * (unsigned)lumFilter[j];
954         }
955         for (j = 0; j < chrFilterSize; j++) {;
956             U += chrUSrc[j][i] * (unsigned)chrFilter[j];
957             V += chrVSrc[j][i] * (unsigned)chrFilter[j];
958         }
959
960         if (hasAlpha) {
961             A = -0x40000000;
962             for (j = 0; j < lumFilterSize; j++) {
963                 A += alpSrc[j][i] * (unsigned)lumFilter[j];
964             }
965             A >>= 1;
966             A += 0x20002000;
967         }
968
969         // 8bit: 12+15=27; 16-bit: 12+19=31
970         Y  >>= 14; // 10
971         Y += 0x10000;
972         U  >>= 14;
973         V  >>= 14;
974
975         // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
976         Y -= c->yuv2rgb_y_offset;
977         Y *= c->yuv2rgb_y_coeff;
978         Y += 1 << 13; // 21
979         // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
980
981         R = V * c->yuv2rgb_v2r_coeff;
982         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
983         B =                            U * c->yuv2rgb_u2b_coeff;
984
985         // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
986         output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
987         output_pixel(&dest[1], av_clip_uintp2(  G + Y, 30) >> 14);
988         output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
989         if (eightbytes) {
990             output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
991             dest += 4;
992         } else {
993             dest += 3;
994         }
995     }
996 }
997
998 static av_always_inline void
999 yuv2rgba64_full_2_c_template(SwsContext *c, const int32_t *buf[2],
1000                        const int32_t *ubuf[2], const int32_t *vbuf[2],
1001                        const int32_t *abuf[2], uint16_t *dest, int dstW,
1002                        int yalpha, int uvalpha, int y,
1003                        enum AVPixelFormat target, int hasAlpha, int eightbytes)
1004 {
1005     const int32_t *buf0  = buf[0],  *buf1  = buf[1],
1006                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1007                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1008                   *abuf0 = hasAlpha ? abuf[0] : NULL,
1009                   *abuf1 = hasAlpha ? abuf[1] : NULL;
1010     int  yalpha1 = 4096 - yalpha;
1011     int uvalpha1 = 4096 - uvalpha;
1012     int i;
1013     int A = 0xffff<<14;
1014
1015     av_assert2(yalpha  <= 4096U);
1016     av_assert2(uvalpha <= 4096U);
1017
1018     for (i = 0; i < dstW; i++) {
1019         int Y  = (buf0[i]     * yalpha1  + buf1[i]     * yalpha) >> 14;
1020         int U  = (ubuf0[i]   * uvalpha1 + ubuf1[i]     * uvalpha - (128 << 23)) >> 14;
1021         int V  = (vbuf0[i]   * uvalpha1 + vbuf1[i]     * uvalpha - (128 << 23)) >> 14;
1022         int R, G, B;
1023
1024         Y -= c->yuv2rgb_y_offset;
1025         Y *= c->yuv2rgb_y_coeff;
1026         Y += 1 << 13;
1027
1028         R = V * c->yuv2rgb_v2r_coeff;
1029         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1030         B =                            U * c->yuv2rgb_u2b_coeff;
1031
1032         if (hasAlpha) {
1033             A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 1;
1034
1035             A += 1 << 13;
1036         }
1037
1038         output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1039         output_pixel(&dest[1], av_clip_uintp2(  G + Y, 30) >> 14);
1040         output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1041         if (eightbytes) {
1042             output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1043             dest += 4;
1044         } else {
1045             dest += 3;
1046         }
1047     }
1048 }
1049
1050 static av_always_inline void
1051 yuv2rgba64_full_1_c_template(SwsContext *c, const int32_t *buf0,
1052                        const int32_t *ubuf[2], const int32_t *vbuf[2],
1053                        const int32_t *abuf0, uint16_t *dest, int dstW,
1054                        int uvalpha, int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
1055 {
1056     const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1057     int i;
1058     int A = 0xffff<<14;
1059
1060     if (uvalpha < 2048) {
1061         for (i = 0; i < dstW; i++) {
1062             int Y  = (buf0[i]) >> 2;
1063             int U  = (ubuf0[i] - (128 << 11)) >> 2;
1064             int V  = (vbuf0[i] - (128 << 11)) >> 2;
1065             int R, G, B;
1066
1067             Y -= c->yuv2rgb_y_offset;
1068             Y *= c->yuv2rgb_y_coeff;
1069             Y += 1 << 13;
1070
1071             if (hasAlpha) {
1072                 A = abuf0[i] << 11;
1073
1074                 A += 1 << 13;
1075             }
1076
1077             R = V * c->yuv2rgb_v2r_coeff;
1078             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1079             B =                            U * c->yuv2rgb_u2b_coeff;
1080
1081             output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1082             output_pixel(&dest[1], av_clip_uintp2(  G + Y, 30) >> 14);
1083             output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1084             if (eightbytes) {
1085                 output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1086                 dest += 4;
1087             } else {
1088                 dest += 3;
1089             }
1090         }
1091     } else {
1092         const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1093         int A = 0xffff<<14;
1094         for (i = 0; i < dstW; i++) {
1095             int Y  = (buf0[i]    ) >> 2;
1096             int U  = (ubuf0[i] + ubuf1[i] - (128 << 12)) >> 3;
1097             int V  = (vbuf0[i] + vbuf1[i] - (128 << 12)) >> 3;
1098             int R, G, B;
1099
1100             Y -= c->yuv2rgb_y_offset;
1101             Y *= c->yuv2rgb_y_coeff;
1102             Y += 1 << 13;
1103
1104             if (hasAlpha) {
1105                 A = abuf0[i] << 11;
1106
1107                 A += 1 << 13;
1108             }
1109
1110             R = V * c->yuv2rgb_v2r_coeff;
1111             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1112             B =                            U * c->yuv2rgb_u2b_coeff;
1113
1114             output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1115             output_pixel(&dest[1], av_clip_uintp2(  G + Y, 30) >> 14);
1116             output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1117             if (eightbytes) {
1118                 output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1119                 dest += 4;
1120             } else {
1121                 dest += 3;
1122             }
1123         }
1124     }
1125 }
1126
1127 #undef output_pixel
1128 #undef r_b
1129 #undef b_r
1130
1131 #define YUV2PACKED16WRAPPER(name, base, ext, fmt, hasAlpha, eightbytes) \
1132 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1133                         const int16_t **_lumSrc, int lumFilterSize, \
1134                         const int16_t *chrFilter, const int16_t **_chrUSrc, \
1135                         const int16_t **_chrVSrc, int chrFilterSize, \
1136                         const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
1137                         int y) \
1138 { \
1139     const int32_t **lumSrc  = (const int32_t **) _lumSrc, \
1140                   **chrUSrc = (const int32_t **) _chrUSrc, \
1141                   **chrVSrc = (const int32_t **) _chrVSrc, \
1142                   **alpSrc  = (const int32_t **) _alpSrc; \
1143     uint16_t *dest = (uint16_t *) _dest; \
1144     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1145                           chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1146                           alpSrc, dest, dstW, y, fmt, hasAlpha, eightbytes); \
1147 } \
1148  \
1149 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
1150                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
1151                         const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
1152                         int yalpha, int uvalpha, int y) \
1153 { \
1154     const int32_t **buf  = (const int32_t **) _buf, \
1155                   **ubuf = (const int32_t **) _ubuf, \
1156                   **vbuf = (const int32_t **) _vbuf, \
1157                   **abuf = (const int32_t **) _abuf; \
1158     uint16_t *dest = (uint16_t *) _dest; \
1159     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1160                           dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha, eightbytes); \
1161 } \
1162  \
1163 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
1164                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
1165                         const int16_t *_abuf0, uint8_t *_dest, int dstW, \
1166                         int uvalpha, int y) \
1167 { \
1168     const int32_t *buf0  = (const int32_t *)  _buf0, \
1169                  **ubuf  = (const int32_t **) _ubuf, \
1170                  **vbuf  = (const int32_t **) _vbuf, \
1171                   *abuf0 = (const int32_t *)  _abuf0; \
1172     uint16_t *dest = (uint16_t *) _dest; \
1173     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1174                                   dstW, uvalpha, y, fmt, hasAlpha, eightbytes); \
1175 }
1176
1177 YUV2PACKED16WRAPPER(yuv2, rgba64, rgb48be, AV_PIX_FMT_RGB48BE, 0, 0)
1178 YUV2PACKED16WRAPPER(yuv2, rgba64, rgb48le, AV_PIX_FMT_RGB48LE, 0, 0)
1179 YUV2PACKED16WRAPPER(yuv2, rgba64, bgr48be, AV_PIX_FMT_BGR48BE, 0, 0)
1180 YUV2PACKED16WRAPPER(yuv2, rgba64, bgr48le, AV_PIX_FMT_BGR48LE, 0, 0)
1181 YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64be, AV_PIX_FMT_RGBA64BE, 1, 1)
1182 YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64le, AV_PIX_FMT_RGBA64LE, 1, 1)
1183 YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64be, AV_PIX_FMT_RGBA64BE, 0, 1)
1184 YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64le, AV_PIX_FMT_RGBA64LE, 0, 1)
1185 YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64be, AV_PIX_FMT_BGRA64BE, 1, 1)
1186 YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64le, AV_PIX_FMT_BGRA64LE, 1, 1)
1187 YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64be, AV_PIX_FMT_BGRA64BE, 0, 1)
1188 YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64le, AV_PIX_FMT_BGRA64LE, 0, 1)
1189
1190 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgb48be_full, AV_PIX_FMT_RGB48BE, 0, 0)
1191 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgb48le_full, AV_PIX_FMT_RGB48LE, 0, 0)
1192 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgr48be_full, AV_PIX_FMT_BGR48BE, 0, 0)
1193 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgr48le_full, AV_PIX_FMT_BGR48LE, 0, 0)
1194 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgba64be_full, AV_PIX_FMT_RGBA64BE, 1, 1)
1195 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgba64le_full, AV_PIX_FMT_RGBA64LE, 1, 1)
1196 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbx64be_full, AV_PIX_FMT_RGBA64BE, 0, 1)
1197 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbx64le_full, AV_PIX_FMT_RGBA64LE, 0, 1)
1198 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgra64be_full, AV_PIX_FMT_BGRA64BE, 1, 1)
1199 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgra64le_full, AV_PIX_FMT_BGRA64LE, 1, 1)
1200 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgrx64be_full, AV_PIX_FMT_BGRA64BE, 0, 1)
1201 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgrx64le_full, AV_PIX_FMT_BGRA64LE, 0, 1)
1202
1203 /*
1204  * Write out 2 RGB pixels in the target pixel format. This function takes a
1205  * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
1206  * things like endianness conversion and shifting. The caller takes care of
1207  * setting the correct offset in these tables from the chroma (U/V) values.
1208  * This function then uses the luminance (Y1/Y2) values to write out the
1209  * correct RGB values into the destination buffer.
1210  */
1211 static av_always_inline void
1212 yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
1213               unsigned A1, unsigned A2,
1214               const void *_r, const void *_g, const void *_b, int y,
1215               enum AVPixelFormat target, int hasAlpha)
1216 {
1217     if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
1218         target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
1219         uint32_t *dest = (uint32_t *) _dest;
1220         const uint32_t *r = (const uint32_t *) _r;
1221         const uint32_t *g = (const uint32_t *) _g;
1222         const uint32_t *b = (const uint32_t *) _b;
1223
1224 #if CONFIG_SMALL
1225         int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
1226
1227         dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
1228         dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
1229 #else
1230         if (hasAlpha) {
1231             int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
1232
1233             av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0);
1234             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
1235             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
1236         } else {
1237 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
1238             int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
1239
1240             av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0xFF);
1241 #endif
1242             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
1243             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
1244         }
1245 #endif
1246     } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
1247         uint8_t *dest = (uint8_t *) _dest;
1248         const uint8_t *r = (const uint8_t *) _r;
1249         const uint8_t *g = (const uint8_t *) _g;
1250         const uint8_t *b = (const uint8_t *) _b;
1251
1252 #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
1253 #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
1254
1255         dest[i * 6 + 0] = r_b[Y1];
1256         dest[i * 6 + 1] =   g[Y1];
1257         dest[i * 6 + 2] = b_r[Y1];
1258         dest[i * 6 + 3] = r_b[Y2];
1259         dest[i * 6 + 4] =   g[Y2];
1260         dest[i * 6 + 5] = b_r[Y2];
1261 #undef r_b
1262 #undef b_r
1263     } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
1264                target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
1265                target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
1266         uint16_t *dest = (uint16_t *) _dest;
1267         const uint16_t *r = (const uint16_t *) _r;
1268         const uint16_t *g = (const uint16_t *) _g;
1269         const uint16_t *b = (const uint16_t *) _b;
1270         int dr1, dg1, db1, dr2, dg2, db2;
1271
1272         if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
1273             dr1 = ff_dither_2x2_8[ y & 1     ][0];
1274             dg1 = ff_dither_2x2_4[ y & 1     ][0];
1275             db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
1276             dr2 = ff_dither_2x2_8[ y & 1     ][1];
1277             dg2 = ff_dither_2x2_4[ y & 1     ][1];
1278             db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
1279         } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
1280             dr1 = ff_dither_2x2_8[ y & 1     ][0];
1281             dg1 = ff_dither_2x2_8[ y & 1     ][1];
1282             db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
1283             dr2 = ff_dither_2x2_8[ y & 1     ][1];
1284             dg2 = ff_dither_2x2_8[ y & 1     ][0];
1285             db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
1286         } else {
1287             dr1 = ff_dither_4x4_16[ y & 3     ][0];
1288             dg1 = ff_dither_4x4_16[ y & 3     ][1];
1289             db1 = ff_dither_4x4_16[(y & 3) ^ 3][0];
1290             dr2 = ff_dither_4x4_16[ y & 3     ][1];
1291             dg2 = ff_dither_4x4_16[ y & 3     ][0];
1292             db2 = ff_dither_4x4_16[(y & 3) ^ 3][1];
1293         }
1294
1295         dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
1296         dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
1297     } else /* 8/4 bits */ {
1298         uint8_t *dest = (uint8_t *) _dest;
1299         const uint8_t *r = (const uint8_t *) _r;
1300         const uint8_t *g = (const uint8_t *) _g;
1301         const uint8_t *b = (const uint8_t *) _b;
1302         int dr1, dg1, db1, dr2, dg2, db2;
1303
1304         if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
1305             const uint8_t * const d64 = ff_dither_8x8_73[y & 7];
1306             const uint8_t * const d32 = ff_dither_8x8_32[y & 7];
1307             dr1 = dg1 = d32[(i * 2 + 0) & 7];
1308             db1 =       d64[(i * 2 + 0) & 7];
1309             dr2 = dg2 = d32[(i * 2 + 1) & 7];
1310             db2 =       d64[(i * 2 + 1) & 7];
1311         } else {
1312             const uint8_t * const d64  = ff_dither_8x8_73 [y & 7];
1313             const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
1314             dr1 = db1 = d128[(i * 2 + 0) & 7];
1315             dg1 =        d64[(i * 2 + 0) & 7];
1316             dr2 = db2 = d128[(i * 2 + 1) & 7];
1317             dg2 =        d64[(i * 2 + 1) & 7];
1318         }
1319
1320         if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
1321             dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
1322                     ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
1323         } else {
1324             dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
1325             dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
1326         }
1327     }
1328 }
1329
1330 static av_always_inline void
1331 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
1332                      const int16_t **lumSrc, int lumFilterSize,
1333                      const int16_t *chrFilter, const int16_t **chrUSrc,
1334                      const int16_t **chrVSrc, int chrFilterSize,
1335                      const int16_t **alpSrc, uint8_t *dest, int dstW,
1336                      int y, enum AVPixelFormat target, int hasAlpha)
1337 {
1338     int i;
1339
1340     for (i = 0; i < ((dstW + 1) >> 1); i++) {
1341         int j, A1, A2;
1342         int Y1 = 1 << 18;
1343         int Y2 = 1 << 18;
1344         int U  = 1 << 18;
1345         int V  = 1 << 18;
1346         const void *r, *g, *b;
1347
1348         for (j = 0; j < lumFilterSize; j++) {
1349             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
1350             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
1351         }
1352         for (j = 0; j < chrFilterSize; j++) {
1353             U += chrUSrc[j][i] * chrFilter[j];
1354             V += chrVSrc[j][i] * chrFilter[j];
1355         }
1356         Y1 >>= 19;
1357         Y2 >>= 19;
1358         U  >>= 19;
1359         V  >>= 19;
1360         if (hasAlpha) {
1361             A1 = 1 << 18;
1362             A2 = 1 << 18;
1363             for (j = 0; j < lumFilterSize; j++) {
1364                 A1 += alpSrc[j][i * 2    ] * lumFilter[j];
1365                 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
1366             }
1367             A1 >>= 19;
1368             A2 >>= 19;
1369             if ((A1 | A2) & 0x100) {
1370                 A1 = av_clip_uint8(A1);
1371                 A2 = av_clip_uint8(A2);
1372             }
1373         }
1374
1375         r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM];
1376         g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
1377         b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1378
1379         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1380                       r, g, b, y, target, hasAlpha);
1381     }
1382 }
1383
1384 static av_always_inline void
1385 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
1386                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1387                      const int16_t *abuf[2], uint8_t *dest, int dstW,
1388                      int yalpha, int uvalpha, int y,
1389                      enum AVPixelFormat target, int hasAlpha)
1390 {
1391     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
1392                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1393                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1394                   *abuf0 = hasAlpha ? abuf[0] : NULL,
1395                   *abuf1 = hasAlpha ? abuf[1] : NULL;
1396     int  yalpha1 = 4096 - yalpha;
1397     int uvalpha1 = 4096 - uvalpha;
1398     int i;
1399     av_assert2(yalpha  <= 4096U);
1400     av_assert2(uvalpha <= 4096U);
1401
1402     for (i = 0; i < ((dstW + 1) >> 1); i++) {
1403         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
1404         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
1405         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
1406         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
1407         int A1, A2;
1408         const void *r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1409                    *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1410                    *b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1411
1412         if (hasAlpha) {
1413             A1 = (abuf0[i * 2    ] * yalpha1 + abuf1[i * 2    ] * yalpha) >> 19;
1414             A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1415             A1 = av_clip_uint8(A1);
1416             A2 = av_clip_uint8(A2);
1417         }
1418
1419         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1420                       r, g, b, y, target, hasAlpha);
1421     }
1422 }
1423
1424 static av_always_inline void
1425 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1426                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1427                      const int16_t *abuf0, uint8_t *dest, int dstW,
1428                      int uvalpha, int y, enum AVPixelFormat target,
1429                      int hasAlpha)
1430 {
1431     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1432     int i;
1433
1434     if (uvalpha < 2048) {
1435         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1436             int Y1 = (buf0[i * 2    ] + 64) >> 7;
1437             int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1438             int U  = (ubuf0[i]        + 64) >> 7;
1439             int V  = (vbuf0[i]        + 64) >> 7;
1440             int A1, A2;
1441             const void *r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1442                        *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1443                        *b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1444
1445             if (hasAlpha) {
1446                 A1 = abuf0[i * 2    ] * 255 + 16384 >> 15;
1447                 A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
1448                 A1 = av_clip_uint8(A1);
1449                 A2 = av_clip_uint8(A2);
1450             }
1451
1452             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1453                           r, g, b, y, target, hasAlpha);
1454         }
1455     } else {
1456         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1457         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1458             int Y1 = (buf0[i * 2    ]     +  64) >> 7;
1459             int Y2 = (buf0[i * 2 + 1]     +  64) >> 7;
1460             int U  = (ubuf0[i] + ubuf1[i] + 128) >> 8;
1461             int V  = (vbuf0[i] + vbuf1[i] + 128) >> 8;
1462             int A1, A2;
1463             const void *r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1464                        *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1465                        *b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1466
1467             if (hasAlpha) {
1468                 A1 = (abuf0[i * 2    ] + 64) >> 7;
1469                 A2 = (abuf0[i * 2 + 1] + 64) >> 7;
1470                 A1 = av_clip_uint8(A1);
1471                 A2 = av_clip_uint8(A2);
1472             }
1473
1474             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1475                           r, g, b, y, target, hasAlpha);
1476         }
1477     }
1478 }
1479
1480 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1481 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1482                                 const int16_t **lumSrc, int lumFilterSize, \
1483                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
1484                                 const int16_t **chrVSrc, int chrFilterSize, \
1485                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1486                                 int y) \
1487 { \
1488     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1489                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1490                                   alpSrc, dest, dstW, y, fmt, hasAlpha); \
1491 }
1492
1493 #define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
1494 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1495 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1496                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1497                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1498                                 int yalpha, int uvalpha, int y) \
1499 { \
1500     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1501                                   dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1502 }
1503
1504 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1505 YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
1506 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1507                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1508                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
1509                                 int uvalpha, int y) \
1510 { \
1511     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1512                                   dstW, uvalpha, y, fmt, hasAlpha); \
1513 }
1514
1515 #if CONFIG_SMALL
1516 YUV2RGBWRAPPER(yuv2rgb,,  32_1,  AV_PIX_FMT_RGB32_1,   CONFIG_SWSCALE_ALPHA && c->needAlpha)
1517 YUV2RGBWRAPPER(yuv2rgb,,  32,    AV_PIX_FMT_RGB32,     CONFIG_SWSCALE_ALPHA && c->needAlpha)
1518 #else
1519 #if CONFIG_SWSCALE_ALPHA
1520 YUV2RGBWRAPPER(yuv2rgb,, a32_1,  AV_PIX_FMT_RGB32_1,   1)
1521 YUV2RGBWRAPPER(yuv2rgb,, a32,    AV_PIX_FMT_RGB32,     1)
1522 #endif
1523 YUV2RGBWRAPPER(yuv2rgb,, x32_1,  AV_PIX_FMT_RGB32_1,   0)
1524 YUV2RGBWRAPPER(yuv2rgb,, x32,    AV_PIX_FMT_RGB32,     0)
1525 #endif
1526 YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24,   0)
1527 YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24,   0)
1528 YUV2RGBWRAPPER(yuv2rgb,,  16,    AV_PIX_FMT_RGB565,    0)
1529 YUV2RGBWRAPPER(yuv2rgb,,  15,    AV_PIX_FMT_RGB555,    0)
1530 YUV2RGBWRAPPER(yuv2rgb,,  12,    AV_PIX_FMT_RGB444,    0)
1531 YUV2RGBWRAPPER(yuv2rgb,,   8,    AV_PIX_FMT_RGB8,      0)
1532 YUV2RGBWRAPPER(yuv2rgb,,   4,    AV_PIX_FMT_RGB4,      0)
1533 YUV2RGBWRAPPER(yuv2rgb,,   4b,   AV_PIX_FMT_RGB4_BYTE, 0)
1534
1535 static av_always_inline void yuv2rgb_write_full(SwsContext *c,
1536     uint8_t *dest, int i, int Y, int A, int U, int V,
1537     int y, enum AVPixelFormat target, int hasAlpha, int err[4])
1538 {
1539     int R, G, B;
1540     int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
1541
1542     Y -= c->yuv2rgb_y_offset;
1543     Y *= c->yuv2rgb_y_coeff;
1544     Y += 1 << 21;
1545     R = Y + V*c->yuv2rgb_v2r_coeff;
1546     G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1547     B = Y +                          U*c->yuv2rgb_u2b_coeff;
1548     if ((R | G | B) & 0xC0000000) {
1549         R = av_clip_uintp2(R, 30);
1550         G = av_clip_uintp2(G, 30);
1551         B = av_clip_uintp2(B, 30);
1552     }
1553
1554     switch(target) {
1555     case AV_PIX_FMT_ARGB:
1556         dest[0] = hasAlpha ? A : 255;
1557         dest[1] = R >> 22;
1558         dest[2] = G >> 22;
1559         dest[3] = B >> 22;
1560         break;
1561     case AV_PIX_FMT_RGB24:
1562         dest[0] = R >> 22;
1563         dest[1] = G >> 22;
1564         dest[2] = B >> 22;
1565         break;
1566     case AV_PIX_FMT_RGBA:
1567         dest[0] = R >> 22;
1568         dest[1] = G >> 22;
1569         dest[2] = B >> 22;
1570         dest[3] = hasAlpha ? A : 255;
1571         break;
1572     case AV_PIX_FMT_ABGR:
1573         dest[0] = hasAlpha ? A : 255;
1574         dest[1] = B >> 22;
1575         dest[2] = G >> 22;
1576         dest[3] = R >> 22;
1577         break;
1578     case AV_PIX_FMT_BGR24:
1579         dest[0] = B >> 22;
1580         dest[1] = G >> 22;
1581         dest[2] = R >> 22;
1582         break;
1583     case AV_PIX_FMT_BGRA:
1584         dest[0] = B >> 22;
1585         dest[1] = G >> 22;
1586         dest[2] = R >> 22;
1587         dest[3] = hasAlpha ? A : 255;
1588         break;
1589     case AV_PIX_FMT_BGR4_BYTE:
1590     case AV_PIX_FMT_RGB4_BYTE:
1591     case AV_PIX_FMT_BGR8:
1592     case AV_PIX_FMT_RGB8:
1593     {
1594         int r,g,b;
1595
1596         switch (c->dither) {
1597         default:
1598         case SWS_DITHER_AUTO:
1599         case SWS_DITHER_ED:
1600             R >>= 22;
1601             G >>= 22;
1602             B >>= 22;
1603             R += (7*err[0] + 1*c->dither_error[0][i] + 5*c->dither_error[0][i+1] + 3*c->dither_error[0][i+2])>>4;
1604             G += (7*err[1] + 1*c->dither_error[1][i] + 5*c->dither_error[1][i+1] + 3*c->dither_error[1][i+2])>>4;
1605             B += (7*err[2] + 1*c->dither_error[2][i] + 5*c->dither_error[2][i+1] + 3*c->dither_error[2][i+2])>>4;
1606             c->dither_error[0][i] = err[0];
1607             c->dither_error[1][i] = err[1];
1608             c->dither_error[2][i] = err[2];
1609             r = R >> (isrgb8 ? 5 : 7);
1610             g = G >> (isrgb8 ? 5 : 6);
1611             b = B >> (isrgb8 ? 6 : 7);
1612             r = av_clip(r, 0, isrgb8 ? 7 : 1);
1613             g = av_clip(g, 0, isrgb8 ? 7 : 3);
1614             b = av_clip(b, 0, isrgb8 ? 3 : 1);
1615             err[0] = R - r*(isrgb8 ? 36 : 255);
1616             err[1] = G - g*(isrgb8 ? 36 : 85);
1617             err[2] = B - b*(isrgb8 ? 85 : 255);
1618             break;
1619         case SWS_DITHER_A_DITHER:
1620             if (isrgb8) {
1621   /* see http://pippin.gimp.org/a_dither/ for details/origin */
1622 #define A_DITHER(u,v)   (((((u)+((v)*236))*119)&0xff))
1623                 r = (((R >> 19) + A_DITHER(i,y)  -96)>>8);
1624                 g = (((G >> 19) + A_DITHER(i + 17,y) - 96)>>8);
1625                 b = (((B >> 20) + A_DITHER(i + 17*2,y) -96)>>8);
1626                 r = av_clip_uintp2(r, 3);
1627                 g = av_clip_uintp2(g, 3);
1628                 b = av_clip_uintp2(b, 2);
1629             } else {
1630                 r = (((R >> 21) + A_DITHER(i,y)-256)>>8);
1631                 g = (((G >> 19) + A_DITHER(i + 17,y)-256)>>8);
1632                 b = (((B >> 21) + A_DITHER(i + 17*2,y)-256)>>8);
1633                 r = av_clip_uintp2(r, 1);
1634                 g = av_clip_uintp2(g, 2);
1635                 b = av_clip_uintp2(b, 1);
1636             }
1637             break;
1638         case SWS_DITHER_X_DITHER:
1639             if (isrgb8) {
1640   /* see http://pippin.gimp.org/a_dither/ for details/origin */
1641 #define X_DITHER(u,v)   (((((u)^((v)*237))*181)&0x1ff)/2)
1642                 r = (((R >> 19) + X_DITHER(i,y) - 96)>>8);
1643                 g = (((G >> 19) + X_DITHER(i + 17,y) - 96)>>8);
1644                 b = (((B >> 20) + X_DITHER(i + 17*2,y) - 96)>>8);
1645                 r = av_clip_uintp2(r, 3);
1646                 g = av_clip_uintp2(g, 3);
1647                 b = av_clip_uintp2(b, 2);
1648             } else {
1649                 r = (((R >> 21) + X_DITHER(i,y)-256)>>8);
1650                 g = (((G >> 19) + X_DITHER(i + 17,y)-256)>>8);
1651                 b = (((B >> 21) + X_DITHER(i + 17*2,y)-256)>>8);
1652                 r = av_clip_uintp2(r, 1);
1653                 g = av_clip_uintp2(g, 2);
1654                 b = av_clip_uintp2(b, 1);
1655             }
1656
1657             break;
1658         }
1659
1660         if(target == AV_PIX_FMT_BGR4_BYTE) {
1661             dest[0] = r + 2*g + 8*b;
1662         } else if(target == AV_PIX_FMT_RGB4_BYTE) {
1663             dest[0] = b + 2*g + 8*r;
1664         } else if(target == AV_PIX_FMT_BGR8) {
1665             dest[0] = r + 8*g + 64*b;
1666         } else if(target == AV_PIX_FMT_RGB8) {
1667             dest[0] = b + 4*g + 32*r;
1668         } else
1669             av_assert2(0);
1670         break;}
1671     }
1672 }
1673
1674 static av_always_inline void
1675 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1676                           const int16_t **lumSrc, int lumFilterSize,
1677                           const int16_t *chrFilter, const int16_t **chrUSrc,
1678                           const int16_t **chrVSrc, int chrFilterSize,
1679                           const int16_t **alpSrc, uint8_t *dest,
1680                           int dstW, int y, enum AVPixelFormat target, int hasAlpha)
1681 {
1682     int i;
1683     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1684     int err[4] = {0};
1685     int A = 0; //init to silence warning
1686
1687     if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1688        || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
1689         step = 1;
1690
1691     for (i = 0; i < dstW; i++) {
1692         int j;
1693         int Y = 1<<9;
1694         int U = (1<<9)-(128 << 19);
1695         int V = (1<<9)-(128 << 19);
1696
1697         for (j = 0; j < lumFilterSize; j++) {
1698             Y += lumSrc[j][i] * lumFilter[j];
1699         }
1700         for (j = 0; j < chrFilterSize; j++) {
1701             U += chrUSrc[j][i] * chrFilter[j];
1702             V += chrVSrc[j][i] * chrFilter[j];
1703         }
1704         Y >>= 10;
1705         U >>= 10;
1706         V >>= 10;
1707         if (hasAlpha) {
1708             A = 1 << 18;
1709             for (j = 0; j < lumFilterSize; j++) {
1710                 A += alpSrc[j][i] * lumFilter[j];
1711             }
1712             A >>= 19;
1713             if (A & 0x100)
1714                 A = av_clip_uint8(A);
1715         }
1716         yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1717         dest += step;
1718     }
1719     c->dither_error[0][i] = err[0];
1720     c->dither_error[1][i] = err[1];
1721     c->dither_error[2][i] = err[2];
1722 }
1723
1724 static av_always_inline void
1725 yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
1726                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1727                      const int16_t *abuf[2], uint8_t *dest, int dstW,
1728                      int yalpha, int uvalpha, int y,
1729                      enum AVPixelFormat target, int hasAlpha)
1730 {
1731     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
1732                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1733                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1734                   *abuf0 = hasAlpha ? abuf[0] : NULL,
1735                   *abuf1 = hasAlpha ? abuf[1] : NULL;
1736     int  yalpha1 = 4096 - yalpha;
1737     int uvalpha1 = 4096 - uvalpha;
1738     int i;
1739     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1740     int err[4] = {0};
1741     int A = 0; // init to silcene warning
1742
1743     av_assert2(yalpha  <= 4096U);
1744     av_assert2(uvalpha <= 4096U);
1745
1746     if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1747        || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
1748         step = 1;
1749
1750     for (i = 0; i < dstW; i++) {
1751         int Y = ( buf0[i] * yalpha1  +  buf1[i] * yalpha             ) >> 10; //FIXME rounding
1752         int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
1753         int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
1754
1755         if (hasAlpha) {
1756             A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
1757             if (A & 0x100)
1758                 A = av_clip_uint8(A);
1759         }
1760
1761         yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1762         dest += step;
1763     }
1764     c->dither_error[0][i] = err[0];
1765     c->dither_error[1][i] = err[1];
1766     c->dither_error[2][i] = err[2];
1767 }
1768
1769 static av_always_inline void
1770 yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
1771                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1772                      const int16_t *abuf0, uint8_t *dest, int dstW,
1773                      int uvalpha, int y, enum AVPixelFormat target,
1774                      int hasAlpha)
1775 {
1776     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1777     int i;
1778     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1779     int err[4] = {0};
1780
1781     if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1782        || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
1783         step = 1;
1784
1785     if (uvalpha < 2048) {
1786         int A = 0; //init to silence warning
1787         for (i = 0; i < dstW; i++) {
1788             int Y = buf0[i] << 2;
1789             int U = (ubuf0[i] - (128<<7)) * 4;
1790             int V = (vbuf0[i] - (128<<7)) * 4;
1791
1792             if (hasAlpha) {
1793                 A = (abuf0[i] + 64) >> 7;
1794                 if (A & 0x100)
1795                     A = av_clip_uint8(A);
1796             }
1797
1798             yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1799             dest += step;
1800         }
1801     } else {
1802         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1803         int A = 0; //init to silence warning
1804         for (i = 0; i < dstW; i++) {
1805             int Y = buf0[i] << 2;
1806             int U = (ubuf0[i] + ubuf1[i] - (128<<8)) << 1;
1807             int V = (vbuf0[i] + vbuf1[i] - (128<<8)) << 1;
1808
1809             if (hasAlpha) {
1810                 A = (abuf0[i] + 64) >> 7;
1811                 if (A & 0x100)
1812                     A = av_clip_uint8(A);
1813             }
1814
1815             yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1816             dest += step;
1817         }
1818     }
1819
1820     c->dither_error[0][i] = err[0];
1821     c->dither_error[1][i] = err[1];
1822     c->dither_error[2][i] = err[2];
1823 }
1824
1825 #if CONFIG_SMALL
1826 YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  CONFIG_SWSCALE_ALPHA && c->needAlpha)
1827 YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  CONFIG_SWSCALE_ALPHA && c->needAlpha)
1828 YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  CONFIG_SWSCALE_ALPHA && c->needAlpha)
1829 YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  CONFIG_SWSCALE_ALPHA && c->needAlpha)
1830 #else
1831 #if CONFIG_SWSCALE_ALPHA
1832 YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  1)
1833 YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  1)
1834 YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  1)
1835 YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  1)
1836 #endif
1837 YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA,  0)
1838 YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR,  0)
1839 YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA,  0)
1840 YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB,  0)
1841 #endif
1842 YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full,  AV_PIX_FMT_BGR24, 0)
1843 YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full,  AV_PIX_FMT_RGB24, 0)
1844
1845 YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full,  AV_PIX_FMT_BGR4_BYTE, 0)
1846 YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full,  AV_PIX_FMT_RGB4_BYTE, 0)
1847 YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full,   AV_PIX_FMT_BGR8,  0)
1848 YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full,   AV_PIX_FMT_RGB8,  0)
1849
1850 static void
1851 yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
1852                   const int16_t **lumSrc, int lumFilterSize,
1853                   const int16_t *chrFilter, const int16_t **chrUSrc,
1854                   const int16_t **chrVSrc, int chrFilterSize,
1855                   const int16_t **alpSrc, uint8_t **dest,
1856                   int dstW, int y)
1857 {
1858     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
1859     int i;
1860     int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
1861     uint16_t **dest16 = (uint16_t**)dest;
1862     int SH = 22 + 8 - desc->comp[0].depth;
1863     int A = 0; // init to silence warning
1864
1865     for (i = 0; i < dstW; i++) {
1866         int j;
1867         int Y = 1 << 9;
1868         int U = (1 << 9) - (128 << 19);
1869         int V = (1 << 9) - (128 << 19);
1870         int R, G, B;
1871
1872         for (j = 0; j < lumFilterSize; j++)
1873             Y += lumSrc[j][i] * lumFilter[j];
1874
1875         for (j = 0; j < chrFilterSize; j++) {
1876             U += chrUSrc[j][i] * chrFilter[j];
1877             V += chrVSrc[j][i] * chrFilter[j];
1878         }
1879
1880         Y >>= 10;
1881         U >>= 10;
1882         V >>= 10;
1883
1884         if (hasAlpha) {
1885             A = 1 << 18;
1886
1887             for (j = 0; j < lumFilterSize; j++)
1888                 A += alpSrc[j][i] * lumFilter[j];
1889
1890             A >>= 19;
1891
1892             if (A & 0x100)
1893                 A = av_clip_uint8(A);
1894         }
1895
1896         Y -= c->yuv2rgb_y_offset;
1897         Y *= c->yuv2rgb_y_coeff;
1898         Y += 1 << 21;
1899         R = Y + V * c->yuv2rgb_v2r_coeff;
1900         G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1901         B = Y +                            U * c->yuv2rgb_u2b_coeff;
1902
1903         if ((R | G | B) & 0xC0000000) {
1904             R = av_clip_uintp2(R, 30);
1905             G = av_clip_uintp2(G, 30);
1906             B = av_clip_uintp2(B, 30);
1907         }
1908
1909         if (SH != 22) {
1910             dest16[0][i] = G >> SH;
1911             dest16[1][i] = B >> SH;
1912             dest16[2][i] = R >> SH;
1913             if (hasAlpha)
1914                 dest16[3][i] = A;
1915         } else {
1916             dest[0][i] = G >> 22;
1917             dest[1][i] = B >> 22;
1918             dest[2][i] = R >> 22;
1919             if (hasAlpha)
1920                 dest[3][i] = A;
1921         }
1922     }
1923     if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
1924         for (i = 0; i < dstW; i++) {
1925             dest16[0][i] = av_bswap16(dest16[0][i]);
1926             dest16[1][i] = av_bswap16(dest16[1][i]);
1927             dest16[2][i] = av_bswap16(dest16[2][i]);
1928             if (hasAlpha)
1929                 dest16[3][i] = av_bswap16(dest16[3][i]);
1930         }
1931     }
1932 }
1933
1934 static void
1935 yuv2ya8_1_c(SwsContext *c, const int16_t *buf0,
1936             const int16_t *ubuf[2], const int16_t *vbuf[2],
1937             const int16_t *abuf0, uint8_t *dest, int dstW,
1938             int uvalpha, int y)
1939 {
1940     int hasAlpha = !!abuf0;
1941     int i;
1942
1943     for (i = 0; i < dstW; i++) {
1944         int Y = (buf0[i] + 64) >> 7;
1945         int A;
1946
1947         Y = av_clip_uint8(Y);
1948
1949         if (hasAlpha) {
1950             A = (abuf0[i] + 64) >> 7;
1951             if (A & 0x100)
1952                 A = av_clip_uint8(A);
1953         }
1954
1955         dest[i * 2    ] = Y;
1956         dest[i * 2 + 1] = hasAlpha ? A : 255;
1957     }
1958 }
1959
1960 static void
1961 yuv2ya8_2_c(SwsContext *c, const int16_t *buf[2],
1962             const int16_t *ubuf[2], const int16_t *vbuf[2],
1963             const int16_t *abuf[2], uint8_t *dest, int dstW,
1964             int yalpha, int uvalpha, int y)
1965 {
1966     int hasAlpha = abuf && abuf[0] && abuf[1];
1967     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
1968                   *abuf0 = hasAlpha ? abuf[0] : NULL,
1969                   *abuf1 = hasAlpha ? abuf[1] : NULL;
1970     int  yalpha1 = 4096 - yalpha;
1971     int i;
1972
1973     av_assert2(yalpha  <= 4096U);
1974
1975     for (i = 0; i < dstW; i++) {
1976         int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 19;
1977         int A;
1978
1979         Y = av_clip_uint8(Y);
1980
1981         if (hasAlpha) {
1982             A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 19;
1983             A = av_clip_uint8(A);
1984         }
1985
1986         dest[i * 2    ] = Y;
1987         dest[i * 2 + 1] = hasAlpha ? A : 255;
1988     }
1989 }
1990
1991 static void
1992 yuv2ya8_X_c(SwsContext *c, const int16_t *lumFilter,
1993             const int16_t **lumSrc, int lumFilterSize,
1994             const int16_t *chrFilter, const int16_t **chrUSrc,
1995             const int16_t **chrVSrc, int chrFilterSize,
1996             const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1997 {
1998     int hasAlpha = !!alpSrc;
1999     int i;
2000
2001     for (i = 0; i < dstW; i++) {
2002         int j;
2003         int Y = 1 << 18, A = 1 << 18;
2004
2005         for (j = 0; j < lumFilterSize; j++)
2006             Y += lumSrc[j][i] * lumFilter[j];
2007
2008         Y >>= 19;
2009         if (Y  & 0x100)
2010             Y = av_clip_uint8(Y);
2011
2012         if (hasAlpha) {
2013             for (j = 0; j < lumFilterSize; j++)
2014                 A += alpSrc[j][i] * lumFilter[j];
2015
2016             A >>= 19;
2017
2018             if (A & 0x100)
2019                 A = av_clip_uint8(A);
2020         }
2021
2022         dest[2 * i    ] = Y;
2023         dest[2 * i + 1] = hasAlpha ? A : 255;
2024     }
2025 }
2026
2027 static void
2028 yuv2ayuv64le_X_c(SwsContext *c, const int16_t *lumFilter,
2029                  const int16_t **_lumSrc, int lumFilterSize,
2030                  const int16_t *chrFilter, const int16_t **_chrUSrc,
2031                  const int16_t **_chrVSrc, int chrFilterSize,
2032                  const int16_t **_alpSrc, uint8_t *dest, int dstW, int y)
2033 {
2034     const int32_t **lumSrc  = (const int32_t **) _lumSrc,
2035                   **chrUSrc = (const int32_t **) _chrUSrc,
2036                   **chrVSrc = (const int32_t **) _chrVSrc,
2037                   **alpSrc  = (const int32_t **) _alpSrc;
2038     int hasAlpha = !!alpSrc;
2039     int i;
2040
2041     for (i = 0; i < dstW; i++) {
2042         int Y = 1 << 14, U = 1 << 14;
2043         int V = 1 << 14, A = 1 << 14;
2044         int j;
2045
2046         Y -= 0x40000000;
2047         U -= 0x40000000;
2048         V -= 0x40000000;
2049         A -= 0x40000000;
2050
2051         for (j = 0; j < lumFilterSize; j++)
2052             Y += lumSrc[j][i] * (unsigned)lumFilter[j];
2053
2054         for (j = 0; j < chrFilterSize; j++)
2055             U += chrUSrc[j][i] * (unsigned)chrFilter[j];
2056
2057         for (j = 0; j < chrFilterSize; j++)
2058             V += chrVSrc[j][i] * (unsigned)chrFilter[j];
2059
2060         if (hasAlpha)
2061             for (j = 0; j < lumFilterSize; j++)
2062                 A += alpSrc[j][i] * (unsigned)lumFilter[j];
2063
2064         Y = 0x8000 + av_clip_int16(Y >> 15);
2065         U = 0x8000 + av_clip_int16(U >> 15);
2066         V = 0x8000 + av_clip_int16(V >> 15);
2067         A = 0x8000 + av_clip_int16(A >> 15);
2068
2069         AV_WL16(dest + 8 * i, hasAlpha ? A : 65535);
2070         AV_WL16(dest + 8 * i + 2, Y);
2071         AV_WL16(dest + 8 * i + 4, U);
2072         AV_WL16(dest + 8 * i + 6, V);
2073     }
2074 }
2075
2076 av_cold void ff_sws_init_output_funcs(SwsContext *c,
2077                                       yuv2planar1_fn *yuv2plane1,
2078                                       yuv2planarX_fn *yuv2planeX,
2079                                       yuv2interleavedX_fn *yuv2nv12cX,
2080                                       yuv2packed1_fn *yuv2packed1,
2081                                       yuv2packed2_fn *yuv2packed2,
2082                                       yuv2packedX_fn *yuv2packedX,
2083                                       yuv2anyX_fn *yuv2anyX)
2084 {
2085     enum AVPixelFormat dstFormat = c->dstFormat;
2086     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
2087
2088     if (is16BPS(dstFormat)) {
2089         *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c  : yuv2planeX_16LE_c;
2090         *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c  : yuv2plane1_16LE_c;
2091     } else if (is9_OR_10BPS(dstFormat)) {
2092         if (desc->comp[0].depth == 9) {
2093             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c  : yuv2planeX_9LE_c;
2094             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c  : yuv2plane1_9LE_c;
2095         } else if (desc->comp[0].depth == 10) {
2096             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c  : yuv2planeX_10LE_c;
2097             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c  : yuv2plane1_10LE_c;
2098         } else if (desc->comp[0].depth == 12) {
2099             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c  : yuv2planeX_12LE_c;
2100             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c  : yuv2plane1_12LE_c;
2101         } else if (desc->comp[0].depth == 14) {
2102             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c  : yuv2planeX_14LE_c;
2103             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c  : yuv2plane1_14LE_c;
2104         } else
2105             av_assert0(0);
2106     } else {
2107         *yuv2plane1 = yuv2plane1_8_c;
2108         *yuv2planeX = yuv2planeX_8_c;
2109         if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
2110             *yuv2nv12cX = yuv2nv12cX_c;
2111     }
2112
2113     if(c->flags & SWS_FULL_CHR_H_INT) {
2114         switch (dstFormat) {
2115             case AV_PIX_FMT_RGBA:
2116 #if CONFIG_SMALL
2117                 *yuv2packedX = yuv2rgba32_full_X_c;
2118                 *yuv2packed2 = yuv2rgba32_full_2_c;
2119                 *yuv2packed1 = yuv2rgba32_full_1_c;
2120 #else
2121 #if CONFIG_SWSCALE_ALPHA
2122                 if (c->needAlpha) {
2123                     *yuv2packedX = yuv2rgba32_full_X_c;
2124                     *yuv2packed2 = yuv2rgba32_full_2_c;
2125                     *yuv2packed1 = yuv2rgba32_full_1_c;
2126                 } else
2127 #endif /* CONFIG_SWSCALE_ALPHA */
2128                 {
2129                     *yuv2packedX = yuv2rgbx32_full_X_c;
2130                     *yuv2packed2 = yuv2rgbx32_full_2_c;
2131                     *yuv2packed1 = yuv2rgbx32_full_1_c;
2132                 }
2133 #endif /* !CONFIG_SMALL */
2134                 break;
2135             case AV_PIX_FMT_ARGB:
2136 #if CONFIG_SMALL
2137                 *yuv2packedX = yuv2argb32_full_X_c;
2138                 *yuv2packed2 = yuv2argb32_full_2_c;
2139                 *yuv2packed1 = yuv2argb32_full_1_c;
2140 #else
2141 #if CONFIG_SWSCALE_ALPHA
2142                 if (c->needAlpha) {
2143                     *yuv2packedX = yuv2argb32_full_X_c;
2144                     *yuv2packed2 = yuv2argb32_full_2_c;
2145                     *yuv2packed1 = yuv2argb32_full_1_c;
2146                 } else
2147 #endif /* CONFIG_SWSCALE_ALPHA */
2148                 {
2149                     *yuv2packedX = yuv2xrgb32_full_X_c;
2150                     *yuv2packed2 = yuv2xrgb32_full_2_c;
2151                     *yuv2packed1 = yuv2xrgb32_full_1_c;
2152                 }
2153 #endif /* !CONFIG_SMALL */
2154                 break;
2155             case AV_PIX_FMT_BGRA:
2156 #if CONFIG_SMALL
2157                 *yuv2packedX = yuv2bgra32_full_X_c;
2158                 *yuv2packed2 = yuv2bgra32_full_2_c;
2159                 *yuv2packed1 = yuv2bgra32_full_1_c;
2160 #else
2161 #if CONFIG_SWSCALE_ALPHA
2162                 if (c->needAlpha) {
2163                     *yuv2packedX = yuv2bgra32_full_X_c;
2164                     *yuv2packed2 = yuv2bgra32_full_2_c;
2165                     *yuv2packed1 = yuv2bgra32_full_1_c;
2166                 } else
2167 #endif /* CONFIG_SWSCALE_ALPHA */
2168                 {
2169                     *yuv2packedX = yuv2bgrx32_full_X_c;
2170                     *yuv2packed2 = yuv2bgrx32_full_2_c;
2171                     *yuv2packed1 = yuv2bgrx32_full_1_c;
2172                 }
2173 #endif /* !CONFIG_SMALL */
2174                 break;
2175             case AV_PIX_FMT_ABGR:
2176 #if CONFIG_SMALL
2177                 *yuv2packedX = yuv2abgr32_full_X_c;
2178                 *yuv2packed2 = yuv2abgr32_full_2_c;
2179                 *yuv2packed1 = yuv2abgr32_full_1_c;
2180 #else
2181 #if CONFIG_SWSCALE_ALPHA
2182                 if (c->needAlpha) {
2183                     *yuv2packedX = yuv2abgr32_full_X_c;
2184                     *yuv2packed2 = yuv2abgr32_full_2_c;
2185                     *yuv2packed1 = yuv2abgr32_full_1_c;
2186                 } else
2187 #endif /* CONFIG_SWSCALE_ALPHA */
2188                 {
2189                     *yuv2packedX = yuv2xbgr32_full_X_c;
2190                     *yuv2packed2 = yuv2xbgr32_full_2_c;
2191                     *yuv2packed1 = yuv2xbgr32_full_1_c;
2192                 }
2193 #endif /* !CONFIG_SMALL */
2194                 break;
2195         case AV_PIX_FMT_RGBA64LE:
2196 #if CONFIG_SWSCALE_ALPHA
2197             if (c->needAlpha) {
2198                 *yuv2packedX = yuv2rgba64le_full_X_c;
2199                 *yuv2packed2 = yuv2rgba64le_full_2_c;
2200                 *yuv2packed1 = yuv2rgba64le_full_1_c;
2201             } else
2202 #endif /* CONFIG_SWSCALE_ALPHA */
2203             {
2204                 *yuv2packedX = yuv2rgbx64le_full_X_c;
2205                 *yuv2packed2 = yuv2rgbx64le_full_2_c;
2206                 *yuv2packed1 = yuv2rgbx64le_full_1_c;
2207             }
2208             break;
2209         case AV_PIX_FMT_RGBA64BE:
2210 #if CONFIG_SWSCALE_ALPHA
2211             if (c->needAlpha) {
2212                 *yuv2packedX = yuv2rgba64be_full_X_c;
2213                 *yuv2packed2 = yuv2rgba64be_full_2_c;
2214                 *yuv2packed1 = yuv2rgba64be_full_1_c;
2215             } else
2216 #endif /* CONFIG_SWSCALE_ALPHA */
2217             {
2218                 *yuv2packedX = yuv2rgbx64be_full_X_c;
2219                 *yuv2packed2 = yuv2rgbx64be_full_2_c;
2220                 *yuv2packed1 = yuv2rgbx64be_full_1_c;
2221             }
2222             break;
2223         case AV_PIX_FMT_BGRA64LE:
2224 #if CONFIG_SWSCALE_ALPHA
2225             if (c->needAlpha) {
2226                 *yuv2packedX = yuv2bgra64le_full_X_c;
2227                 *yuv2packed2 = yuv2bgra64le_full_2_c;
2228                 *yuv2packed1 = yuv2bgra64le_full_1_c;
2229             } else
2230 #endif /* CONFIG_SWSCALE_ALPHA */
2231             {
2232                 *yuv2packedX = yuv2bgrx64le_full_X_c;
2233                 *yuv2packed2 = yuv2bgrx64le_full_2_c;
2234                 *yuv2packed1 = yuv2bgrx64le_full_1_c;
2235             }
2236             break;
2237         case AV_PIX_FMT_BGRA64BE:
2238 #if CONFIG_SWSCALE_ALPHA
2239             if (c->needAlpha) {
2240                 *yuv2packedX = yuv2bgra64be_full_X_c;
2241                 *yuv2packed2 = yuv2bgra64be_full_2_c;
2242                 *yuv2packed1 = yuv2bgra64be_full_1_c;
2243             } else
2244 #endif /* CONFIG_SWSCALE_ALPHA */
2245             {
2246                 *yuv2packedX = yuv2bgrx64be_full_X_c;
2247                 *yuv2packed2 = yuv2bgrx64be_full_2_c;
2248                 *yuv2packed1 = yuv2bgrx64be_full_1_c;
2249             }
2250             break;
2251
2252         case AV_PIX_FMT_RGB24:
2253             *yuv2packedX = yuv2rgb24_full_X_c;
2254             *yuv2packed2 = yuv2rgb24_full_2_c;
2255             *yuv2packed1 = yuv2rgb24_full_1_c;
2256             break;
2257         case AV_PIX_FMT_BGR24:
2258             *yuv2packedX = yuv2bgr24_full_X_c;
2259             *yuv2packed2 = yuv2bgr24_full_2_c;
2260             *yuv2packed1 = yuv2bgr24_full_1_c;
2261             break;
2262         case AV_PIX_FMT_RGB48LE:
2263             *yuv2packedX = yuv2rgb48le_full_X_c;
2264             *yuv2packed2 = yuv2rgb48le_full_2_c;
2265             *yuv2packed1 = yuv2rgb48le_full_1_c;
2266             break;
2267         case AV_PIX_FMT_BGR48LE:
2268             *yuv2packedX = yuv2bgr48le_full_X_c;
2269             *yuv2packed2 = yuv2bgr48le_full_2_c;
2270             *yuv2packed1 = yuv2bgr48le_full_1_c;
2271             break;
2272         case AV_PIX_FMT_RGB48BE:
2273             *yuv2packedX = yuv2rgb48be_full_X_c;
2274             *yuv2packed2 = yuv2rgb48be_full_2_c;
2275             *yuv2packed1 = yuv2rgb48be_full_1_c;
2276             break;
2277         case AV_PIX_FMT_BGR48BE:
2278             *yuv2packedX = yuv2bgr48be_full_X_c;
2279             *yuv2packed2 = yuv2bgr48be_full_2_c;
2280             *yuv2packed1 = yuv2bgr48be_full_1_c;
2281             break;
2282         case AV_PIX_FMT_BGR4_BYTE:
2283             *yuv2packedX = yuv2bgr4_byte_full_X_c;
2284             *yuv2packed2 = yuv2bgr4_byte_full_2_c;
2285             *yuv2packed1 = yuv2bgr4_byte_full_1_c;
2286             break;
2287         case AV_PIX_FMT_RGB4_BYTE:
2288             *yuv2packedX = yuv2rgb4_byte_full_X_c;
2289             *yuv2packed2 = yuv2rgb4_byte_full_2_c;
2290             *yuv2packed1 = yuv2rgb4_byte_full_1_c;
2291             break;
2292         case AV_PIX_FMT_BGR8:
2293             *yuv2packedX = yuv2bgr8_full_X_c;
2294             *yuv2packed2 = yuv2bgr8_full_2_c;
2295             *yuv2packed1 = yuv2bgr8_full_1_c;
2296             break;
2297         case AV_PIX_FMT_RGB8:
2298             *yuv2packedX = yuv2rgb8_full_X_c;
2299             *yuv2packed2 = yuv2rgb8_full_2_c;
2300             *yuv2packed1 = yuv2rgb8_full_1_c;
2301             break;
2302         case AV_PIX_FMT_GBRP:
2303         case AV_PIX_FMT_GBRP9BE:
2304         case AV_PIX_FMT_GBRP9LE:
2305         case AV_PIX_FMT_GBRP10BE:
2306         case AV_PIX_FMT_GBRP10LE:
2307         case AV_PIX_FMT_GBRP12BE:
2308         case AV_PIX_FMT_GBRP12LE:
2309         case AV_PIX_FMT_GBRP14BE:
2310         case AV_PIX_FMT_GBRP14LE:
2311         case AV_PIX_FMT_GBRP16BE:
2312         case AV_PIX_FMT_GBRP16LE:
2313         case AV_PIX_FMT_GBRAP:
2314             *yuv2anyX = yuv2gbrp_full_X_c;
2315             break;
2316         }
2317         if (!*yuv2packedX && !*yuv2anyX)
2318             goto YUV_PACKED;
2319     } else {
2320         YUV_PACKED:
2321         switch (dstFormat) {
2322         case AV_PIX_FMT_RGBA64LE:
2323 #if CONFIG_SWSCALE_ALPHA
2324             if (c->needAlpha) {
2325                 *yuv2packed1 = yuv2rgba64le_1_c;
2326                 *yuv2packed2 = yuv2rgba64le_2_c;
2327                 *yuv2packedX = yuv2rgba64le_X_c;
2328             } else
2329 #endif /* CONFIG_SWSCALE_ALPHA */
2330             {
2331                 *yuv2packed1 = yuv2rgbx64le_1_c;
2332                 *yuv2packed2 = yuv2rgbx64le_2_c;
2333                 *yuv2packedX = yuv2rgbx64le_X_c;
2334             }
2335             break;
2336         case AV_PIX_FMT_RGBA64BE:
2337 #if CONFIG_SWSCALE_ALPHA
2338             if (c->needAlpha) {
2339                 *yuv2packed1 = yuv2rgba64be_1_c;
2340                 *yuv2packed2 = yuv2rgba64be_2_c;
2341                 *yuv2packedX = yuv2rgba64be_X_c;
2342             } else
2343 #endif /* CONFIG_SWSCALE_ALPHA */
2344             {
2345                 *yuv2packed1 = yuv2rgbx64be_1_c;
2346                 *yuv2packed2 = yuv2rgbx64be_2_c;
2347                 *yuv2packedX = yuv2rgbx64be_X_c;
2348             }
2349             break;
2350         case AV_PIX_FMT_BGRA64LE:
2351 #if CONFIG_SWSCALE_ALPHA
2352             if (c->needAlpha) {
2353                 *yuv2packed1 = yuv2bgra64le_1_c;
2354                 *yuv2packed2 = yuv2bgra64le_2_c;
2355                 *yuv2packedX = yuv2bgra64le_X_c;
2356             } else
2357 #endif /* CONFIG_SWSCALE_ALPHA */
2358             {
2359                 *yuv2packed1 = yuv2bgrx64le_1_c;
2360                 *yuv2packed2 = yuv2bgrx64le_2_c;
2361                 *yuv2packedX = yuv2bgrx64le_X_c;
2362             }
2363             break;
2364         case AV_PIX_FMT_BGRA64BE:
2365 #if CONFIG_SWSCALE_ALPHA
2366             if (c->needAlpha) {
2367                 *yuv2packed1 = yuv2bgra64be_1_c;
2368                 *yuv2packed2 = yuv2bgra64be_2_c;
2369                 *yuv2packedX = yuv2bgra64be_X_c;
2370             } else
2371 #endif /* CONFIG_SWSCALE_ALPHA */
2372             {
2373                 *yuv2packed1 = yuv2bgrx64be_1_c;
2374                 *yuv2packed2 = yuv2bgrx64be_2_c;
2375                 *yuv2packedX = yuv2bgrx64be_X_c;
2376             }
2377             break;
2378         case AV_PIX_FMT_RGB48LE:
2379             *yuv2packed1 = yuv2rgb48le_1_c;
2380             *yuv2packed2 = yuv2rgb48le_2_c;
2381             *yuv2packedX = yuv2rgb48le_X_c;
2382             break;
2383         case AV_PIX_FMT_RGB48BE:
2384             *yuv2packed1 = yuv2rgb48be_1_c;
2385             *yuv2packed2 = yuv2rgb48be_2_c;
2386             *yuv2packedX = yuv2rgb48be_X_c;
2387             break;
2388         case AV_PIX_FMT_BGR48LE:
2389             *yuv2packed1 = yuv2bgr48le_1_c;
2390             *yuv2packed2 = yuv2bgr48le_2_c;
2391             *yuv2packedX = yuv2bgr48le_X_c;
2392             break;
2393         case AV_PIX_FMT_BGR48BE:
2394             *yuv2packed1 = yuv2bgr48be_1_c;
2395             *yuv2packed2 = yuv2bgr48be_2_c;
2396             *yuv2packedX = yuv2bgr48be_X_c;
2397             break;
2398         case AV_PIX_FMT_RGB32:
2399         case AV_PIX_FMT_BGR32:
2400 #if CONFIG_SMALL
2401             *yuv2packed1 = yuv2rgb32_1_c;
2402             *yuv2packed2 = yuv2rgb32_2_c;
2403             *yuv2packedX = yuv2rgb32_X_c;
2404 #else
2405 #if CONFIG_SWSCALE_ALPHA
2406                 if (c->needAlpha) {
2407                     *yuv2packed1 = yuv2rgba32_1_c;
2408                     *yuv2packed2 = yuv2rgba32_2_c;
2409                     *yuv2packedX = yuv2rgba32_X_c;
2410                 } else
2411 #endif /* CONFIG_SWSCALE_ALPHA */
2412                 {
2413                     *yuv2packed1 = yuv2rgbx32_1_c;
2414                     *yuv2packed2 = yuv2rgbx32_2_c;
2415                     *yuv2packedX = yuv2rgbx32_X_c;
2416                 }
2417 #endif /* !CONFIG_SMALL */
2418             break;
2419         case AV_PIX_FMT_RGB32_1:
2420         case AV_PIX_FMT_BGR32_1:
2421 #if CONFIG_SMALL
2422                 *yuv2packed1 = yuv2rgb32_1_1_c;
2423                 *yuv2packed2 = yuv2rgb32_1_2_c;
2424                 *yuv2packedX = yuv2rgb32_1_X_c;
2425 #else
2426 #if CONFIG_SWSCALE_ALPHA
2427                 if (c->needAlpha) {
2428                     *yuv2packed1 = yuv2rgba32_1_1_c;
2429                     *yuv2packed2 = yuv2rgba32_1_2_c;
2430                     *yuv2packedX = yuv2rgba32_1_X_c;
2431                 } else
2432 #endif /* CONFIG_SWSCALE_ALPHA */
2433                 {
2434                     *yuv2packed1 = yuv2rgbx32_1_1_c;
2435                     *yuv2packed2 = yuv2rgbx32_1_2_c;
2436                     *yuv2packedX = yuv2rgbx32_1_X_c;
2437                 }
2438 #endif /* !CONFIG_SMALL */
2439                 break;
2440         case AV_PIX_FMT_RGB24:
2441             *yuv2packed1 = yuv2rgb24_1_c;
2442             *yuv2packed2 = yuv2rgb24_2_c;
2443             *yuv2packedX = yuv2rgb24_X_c;
2444             break;
2445         case AV_PIX_FMT_BGR24:
2446             *yuv2packed1 = yuv2bgr24_1_c;
2447             *yuv2packed2 = yuv2bgr24_2_c;
2448             *yuv2packedX = yuv2bgr24_X_c;
2449             break;
2450         case AV_PIX_FMT_RGB565LE:
2451         case AV_PIX_FMT_RGB565BE:
2452         case AV_PIX_FMT_BGR565LE:
2453         case AV_PIX_FMT_BGR565BE:
2454             *yuv2packed1 = yuv2rgb16_1_c;
2455             *yuv2packed2 = yuv2rgb16_2_c;
2456             *yuv2packedX = yuv2rgb16_X_c;
2457             break;
2458         case AV_PIX_FMT_RGB555LE:
2459         case AV_PIX_FMT_RGB555BE:
2460         case AV_PIX_FMT_BGR555LE:
2461         case AV_PIX_FMT_BGR555BE:
2462             *yuv2packed1 = yuv2rgb15_1_c;
2463             *yuv2packed2 = yuv2rgb15_2_c;
2464             *yuv2packedX = yuv2rgb15_X_c;
2465             break;
2466         case AV_PIX_FMT_RGB444LE:
2467         case AV_PIX_FMT_RGB444BE:
2468         case AV_PIX_FMT_BGR444LE:
2469         case AV_PIX_FMT_BGR444BE:
2470             *yuv2packed1 = yuv2rgb12_1_c;
2471             *yuv2packed2 = yuv2rgb12_2_c;
2472             *yuv2packedX = yuv2rgb12_X_c;
2473             break;
2474         case AV_PIX_FMT_RGB8:
2475         case AV_PIX_FMT_BGR8:
2476             *yuv2packed1 = yuv2rgb8_1_c;
2477             *yuv2packed2 = yuv2rgb8_2_c;
2478             *yuv2packedX = yuv2rgb8_X_c;
2479             break;
2480         case AV_PIX_FMT_RGB4:
2481         case AV_PIX_FMT_BGR4:
2482             *yuv2packed1 = yuv2rgb4_1_c;
2483             *yuv2packed2 = yuv2rgb4_2_c;
2484             *yuv2packedX = yuv2rgb4_X_c;
2485             break;
2486         case AV_PIX_FMT_RGB4_BYTE:
2487         case AV_PIX_FMT_BGR4_BYTE:
2488             *yuv2packed1 = yuv2rgb4b_1_c;
2489             *yuv2packed2 = yuv2rgb4b_2_c;
2490             *yuv2packedX = yuv2rgb4b_X_c;
2491             break;
2492         }
2493     }
2494     switch (dstFormat) {
2495     case AV_PIX_FMT_MONOWHITE:
2496         *yuv2packed1 = yuv2monowhite_1_c;
2497         *yuv2packed2 = yuv2monowhite_2_c;
2498         *yuv2packedX = yuv2monowhite_X_c;
2499         break;
2500     case AV_PIX_FMT_MONOBLACK:
2501         *yuv2packed1 = yuv2monoblack_1_c;
2502         *yuv2packed2 = yuv2monoblack_2_c;
2503         *yuv2packedX = yuv2monoblack_X_c;
2504         break;
2505     case AV_PIX_FMT_YUYV422:
2506         *yuv2packed1 = yuv2yuyv422_1_c;
2507         *yuv2packed2 = yuv2yuyv422_2_c;
2508         *yuv2packedX = yuv2yuyv422_X_c;
2509         break;
2510     case AV_PIX_FMT_YVYU422:
2511         *yuv2packed1 = yuv2yvyu422_1_c;
2512         *yuv2packed2 = yuv2yvyu422_2_c;
2513         *yuv2packedX = yuv2yvyu422_X_c;
2514         break;
2515     case AV_PIX_FMT_UYVY422:
2516         *yuv2packed1 = yuv2uyvy422_1_c;
2517         *yuv2packed2 = yuv2uyvy422_2_c;
2518         *yuv2packedX = yuv2uyvy422_X_c;
2519         break;
2520     case AV_PIX_FMT_YA8:
2521         *yuv2packed1 = yuv2ya8_1_c;
2522         *yuv2packed2 = yuv2ya8_2_c;
2523         *yuv2packedX = yuv2ya8_X_c;
2524         break;
2525     case AV_PIX_FMT_AYUV64LE:
2526         *yuv2packedX = yuv2ayuv64le_X_c;
2527         break;
2528     }
2529 }