]> git.sesse.net Git - ffmpeg/blob - libswscale/output.c
Merge commit 'eac77fcd56fc2a3391f0d86faf54302afb368ff7'
[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) >= (128 + 110)
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
389     if (c->dither == SWS_DITHER_ED) {
390         int err = 0;
391         int acc = 0;
392         for (i = 0; i < dstW; i +=2) {
393             int Y;
394
395             Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
396             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;
397             c->dither_error[0][i] = err;
398             acc = 2*acc + (Y >= 128);
399             Y -= 220*(acc&1);
400
401             err = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
402             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;
403             c->dither_error[0][i+1] = Y;
404             acc = 2*acc + (err >= 128);
405             err -= 220*(acc&1);
406
407             if ((i & 7) == 6)
408                 output_pixel(*dest++, acc);
409         }
410         c->dither_error[0][i] = err;
411     } else {
412     for (i = 0; i < dstW; i += 8) {
413         int Y, acc = 0;
414
415         Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
416         accumulate_bit(acc, Y + d128[0]);
417         Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
418         accumulate_bit(acc, Y + d128[1]);
419         Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
420         accumulate_bit(acc, Y + d128[2]);
421         Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
422         accumulate_bit(acc, Y + d128[3]);
423         Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
424         accumulate_bit(acc, Y + d128[4]);
425         Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
426         accumulate_bit(acc, Y + d128[5]);
427         Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
428         accumulate_bit(acc, Y + d128[6]);
429         Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
430         accumulate_bit(acc, Y + d128[7]);
431
432         output_pixel(*dest++, acc);
433     }
434     }
435 }
436
437 static av_always_inline void
438 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
439                       const int16_t *ubuf[2], const int16_t *vbuf[2],
440                       const int16_t *abuf0, uint8_t *dest, int dstW,
441                       int uvalpha, int y, enum AVPixelFormat target)
442 {
443     const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
444     int i;
445
446     if (c->dither == SWS_DITHER_ED) {
447         int err = 0;
448         int acc = 0;
449         for (i = 0; i < dstW; i +=2) {
450             int Y;
451
452             Y = ((buf0[i + 0] + 64) >> 7);
453             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;
454             c->dither_error[0][i] = err;
455             acc = 2*acc + (Y >= 128);
456             Y -= 220*(acc&1);
457
458             err = ((buf0[i + 1] + 64) >> 7);
459             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;
460             c->dither_error[0][i+1] = Y;
461             acc = 2*acc + (err >= 128);
462             err -= 220*(acc&1);
463
464             if ((i & 7) == 6)
465                 output_pixel(*dest++, acc);
466         }
467         c->dither_error[0][i] = err;
468     } else {
469     for (i = 0; i < dstW; i += 8) {
470         int acc = 0;
471         accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
472         accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
473         accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
474         accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
475         accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
476         accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
477         accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
478         accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
479
480         output_pixel(*dest++, acc);
481     }
482     }
483 }
484
485 #undef output_pixel
486 #undef accumulate_bit
487
488 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
489 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
490                                 const int16_t **lumSrc, int lumFilterSize, \
491                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
492                                 const int16_t **chrVSrc, int chrFilterSize, \
493                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
494                                 int y) \
495 { \
496     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
497                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
498                                   alpSrc, dest, dstW, y, fmt); \
499 } \
500  \
501 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
502                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
503                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
504                                 int yalpha, int uvalpha, int y) \
505 { \
506     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
507                                   dest, dstW, yalpha, uvalpha, y, fmt); \
508 } \
509  \
510 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
511                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
512                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
513                                 int uvalpha, int y) \
514 { \
515     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
516                                   abuf0, dest, dstW, uvalpha, \
517                                   y, fmt); \
518 }
519
520 YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
521 YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
522
523 #define output_pixels(pos, Y1, U, Y2, V) \
524     if (target == AV_PIX_FMT_YUYV422) { \
525         dest[pos + 0] = Y1; \
526         dest[pos + 1] = U;  \
527         dest[pos + 2] = Y2; \
528         dest[pos + 3] = V;  \
529     } else if (target == AV_PIX_FMT_YVYU422) { \
530         dest[pos + 0] = Y1; \
531         dest[pos + 1] = V;  \
532         dest[pos + 2] = Y2; \
533         dest[pos + 3] = U;  \
534     } else { /* AV_PIX_FMT_UYVY422 */ \
535         dest[pos + 0] = U;  \
536         dest[pos + 1] = Y1; \
537         dest[pos + 2] = V;  \
538         dest[pos + 3] = Y2; \
539     }
540
541 static av_always_inline void
542 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
543                      const int16_t **lumSrc, int lumFilterSize,
544                      const int16_t *chrFilter, const int16_t **chrUSrc,
545                      const int16_t **chrVSrc, int chrFilterSize,
546                      const int16_t **alpSrc, uint8_t *dest, int dstW,
547                      int y, enum AVPixelFormat target)
548 {
549     int i;
550
551     for (i = 0; i < ((dstW + 1) >> 1); i++) {
552         int j;
553         int Y1 = 1 << 18;
554         int Y2 = 1 << 18;
555         int U  = 1 << 18;
556         int V  = 1 << 18;
557
558         for (j = 0; j < lumFilterSize; j++) {
559             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
560             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
561         }
562         for (j = 0; j < chrFilterSize; j++) {
563             U += chrUSrc[j][i] * chrFilter[j];
564             V += chrVSrc[j][i] * chrFilter[j];
565         }
566         Y1 >>= 19;
567         Y2 >>= 19;
568         U  >>= 19;
569         V  >>= 19;
570         if ((Y1 | Y2 | U | V) & 0x100) {
571             Y1 = av_clip_uint8(Y1);
572             Y2 = av_clip_uint8(Y2);
573             U  = av_clip_uint8(U);
574             V  = av_clip_uint8(V);
575         }
576         output_pixels(4*i, Y1, U, Y2, V);
577     }
578 }
579
580 static av_always_inline void
581 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
582                      const int16_t *ubuf[2], const int16_t *vbuf[2],
583                      const int16_t *abuf[2], uint8_t *dest, int dstW,
584                      int yalpha, int uvalpha, int y,
585                      enum AVPixelFormat target)
586 {
587     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
588                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
589                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
590     int  yalpha1 = 4096 - yalpha;
591     int uvalpha1 = 4096 - uvalpha;
592     int i;
593
594     for (i = 0; i < ((dstW + 1) >> 1); i++) {
595         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
596         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
597         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
598         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
599
600         if ((Y1 | Y2 | U | V) & 0x100) {
601             Y1 = av_clip_uint8(Y1);
602             Y2 = av_clip_uint8(Y2);
603             U  = av_clip_uint8(U);
604             V  = av_clip_uint8(V);
605         }
606
607         output_pixels(i * 4, Y1, U, Y2, V);
608     }
609 }
610
611 static av_always_inline void
612 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
613                      const int16_t *ubuf[2], const int16_t *vbuf[2],
614                      const int16_t *abuf0, uint8_t *dest, int dstW,
615                      int uvalpha, int y, enum AVPixelFormat target)
616 {
617     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
618     int i;
619
620     if (uvalpha < 2048) {
621         for (i = 0; i < ((dstW + 1) >> 1); i++) {
622             int Y1 = (buf0[i * 2    ]+64) >> 7;
623             int Y2 = (buf0[i * 2 + 1]+64) >> 7;
624             int U  = (ubuf0[i]       +64) >> 7;
625             int V  = (vbuf0[i]       +64) >> 7;
626
627             if ((Y1 | Y2 | U | V) & 0x100) {
628                 Y1 = av_clip_uint8(Y1);
629                 Y2 = av_clip_uint8(Y2);
630                 U  = av_clip_uint8(U);
631                 V  = av_clip_uint8(V);
632             }
633
634             Y1 = av_clip_uint8(Y1);
635             Y2 = av_clip_uint8(Y2);
636             U  = av_clip_uint8(U);
637             V  = av_clip_uint8(V);
638
639             output_pixels(i * 4, Y1, U, Y2, V);
640         }
641     } else {
642         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
643         for (i = 0; i < ((dstW + 1) >> 1); i++) {
644             int Y1 = (buf0[i * 2    ]    + 64) >> 7;
645             int Y2 = (buf0[i * 2 + 1]    + 64) >> 7;
646             int U  = (ubuf0[i] + ubuf1[i]+128) >> 8;
647             int V  = (vbuf0[i] + vbuf1[i]+128) >> 8;
648
649             if ((Y1 | Y2 | U | V) & 0x100) {
650                 Y1 = av_clip_uint8(Y1);
651                 Y2 = av_clip_uint8(Y2);
652                 U  = av_clip_uint8(U);
653                 V  = av_clip_uint8(V);
654             }
655
656             Y1 = av_clip_uint8(Y1);
657             Y2 = av_clip_uint8(Y2);
658             U  = av_clip_uint8(U);
659             V  = av_clip_uint8(V);
660
661             output_pixels(i * 4, Y1, U, Y2, V);
662         }
663     }
664 }
665
666 #undef output_pixels
667
668 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
669 YUV2PACKEDWRAPPER(yuv2, 422, yvyu422, AV_PIX_FMT_YVYU422)
670 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
671
672 #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)
673 #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)
674 #define output_pixel(pos, val) \
675     if (isBE(target)) { \
676         AV_WB16(pos, val); \
677     } else { \
678         AV_WL16(pos, val); \
679     }
680
681 static av_always_inline void
682 yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter,
683                        const int32_t **lumSrc, int lumFilterSize,
684                        const int16_t *chrFilter, const int32_t **chrUSrc,
685                        const int32_t **chrVSrc, int chrFilterSize,
686                        const int32_t **alpSrc, uint16_t *dest, int dstW,
687                        int y, enum AVPixelFormat target, int hasAlpha)
688 {
689     int i;
690     int A1 = 0xffff<<14, A2 = 0xffff<<14;
691
692     for (i = 0; i < ((dstW + 1) >> 1); i++) {
693         int j;
694         int Y1 = -0x40000000;
695         int Y2 = -0x40000000;
696         int U  = -128 << 23; // 19
697         int V  = -128 << 23;
698         int R, G, B;
699
700         for (j = 0; j < lumFilterSize; j++) {
701             Y1 += lumSrc[j][i * 2]     * (unsigned)lumFilter[j];
702             Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
703         }
704         for (j = 0; j < chrFilterSize; j++) {;
705             U += chrUSrc[j][i] * (unsigned)chrFilter[j];
706             V += chrVSrc[j][i] * (unsigned)chrFilter[j];
707         }
708
709         if (hasAlpha) {
710             A1 = -0x40000000;
711             A2 = -0x40000000;
712             for (j = 0; j < lumFilterSize; j++) {
713                 A1 += alpSrc[j][i * 2]     * (unsigned)lumFilter[j];
714                 A2 += alpSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
715             }
716             A1 >>= 1;
717             A1 += 0x20002000;
718             A2 >>= 1;
719             A2 += 0x20002000;
720         }
721
722         // 8bit: 12+15=27; 16-bit: 12+19=31
723         Y1 >>= 14; // 10
724         Y1 += 0x10000;
725         Y2 >>= 14;
726         Y2 += 0x10000;
727         U  >>= 14;
728         V  >>= 14;
729
730         // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
731         Y1 -= c->yuv2rgb_y_offset;
732         Y2 -= c->yuv2rgb_y_offset;
733         Y1 *= c->yuv2rgb_y_coeff;
734         Y2 *= c->yuv2rgb_y_coeff;
735         Y1 += 1 << 13; // 21
736         Y2 += 1 << 13;
737         // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
738
739         R = V * c->yuv2rgb_v2r_coeff;
740         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
741         B =                            U * c->yuv2rgb_u2b_coeff;
742
743         // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
744         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
745         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
746         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
747         output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
748         output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
749         output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
750         output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
751         output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
752         dest += 8;
753     }
754 }
755
756 static av_always_inline void
757 yuv2rgba64_2_c_template(SwsContext *c, const int32_t *buf[2],
758                        const int32_t *ubuf[2], const int32_t *vbuf[2],
759                        const int32_t *abuf[2], uint16_t *dest, int dstW,
760                        int yalpha, int uvalpha, int y,
761                        enum AVPixelFormat target, int hasAlpha)
762 {
763     const int32_t *buf0  = buf[0],  *buf1  = buf[1],
764                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
765                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
766                   *abuf0 = hasAlpha ? abuf[0] : NULL,
767                   *abuf1 = hasAlpha ? abuf[1] : NULL;
768     int  yalpha1 = 4096 - yalpha;
769     int uvalpha1 = 4096 - uvalpha;
770     int i;
771     int A1 = 0xffff<<14, A2 = 0xffff<<14;
772
773     for (i = 0; i < ((dstW + 1) >> 1); i++) {
774         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha) >> 14;
775         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha) >> 14;
776         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha + (-128 << 23)) >> 14;
777         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha + (-128 << 23)) >> 14;
778         int R, G, B;
779
780         Y1 -= c->yuv2rgb_y_offset;
781         Y2 -= c->yuv2rgb_y_offset;
782         Y1 *= c->yuv2rgb_y_coeff;
783         Y2 *= c->yuv2rgb_y_coeff;
784         Y1 += 1 << 13;
785         Y2 += 1 << 13;
786
787         R = V * c->yuv2rgb_v2r_coeff;
788         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
789         B =                            U * c->yuv2rgb_u2b_coeff;
790
791         if (hasAlpha) {
792             A1 = (abuf0[i * 2    ] * yalpha1 + abuf1[i * 2    ] * yalpha) >> 1;
793             A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 1;
794
795             A1 += 1 << 13;
796             A2 += 1 << 13;
797         }
798
799         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
800         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
801         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
802         output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
803         output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
804         output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
805         output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
806         output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
807         dest += 8;
808     }
809 }
810
811 static av_always_inline void
812 yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0,
813                        const int32_t *ubuf[2], const int32_t *vbuf[2],
814                        const int32_t *abuf0, uint16_t *dest, int dstW,
815                        int uvalpha, int y, enum AVPixelFormat target, int hasAlpha)
816 {
817     const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
818     int i;
819     int A1 = 0xffff<<14, A2= 0xffff<<14;
820
821     if (uvalpha < 2048) {
822         for (i = 0; i < ((dstW + 1) >> 1); i++) {
823             int Y1 = (buf0[i * 2]    ) >> 2;
824             int Y2 = (buf0[i * 2 + 1]) >> 2;
825             int U  = (ubuf0[i] + (-128 << 11)) >> 2;
826             int V  = (vbuf0[i] + (-128 << 11)) >> 2;
827             int R, G, B;
828
829             Y1 -= c->yuv2rgb_y_offset;
830             Y2 -= c->yuv2rgb_y_offset;
831             Y1 *= c->yuv2rgb_y_coeff;
832             Y2 *= c->yuv2rgb_y_coeff;
833             Y1 += 1 << 13;
834             Y2 += 1 << 13;
835
836             if (hasAlpha) {
837                 A1 = abuf0[i * 2    ] << 11;
838                 A2 = abuf0[i * 2 + 1] << 11;
839
840                 A1 += 1 << 13;
841                 A2 += 1 << 13;
842             }
843
844             R = V * c->yuv2rgb_v2r_coeff;
845             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
846             B =                            U * c->yuv2rgb_u2b_coeff;
847
848             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
849             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
850             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
851             output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
852             output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
853             output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
854             output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
855             output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
856             dest += 8;
857         }
858     } else {
859         const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
860         int A1 = 0xffff<<14, A2 = 0xffff<<14;
861         for (i = 0; i < ((dstW + 1) >> 1); i++) {
862             int Y1 = (buf0[i * 2]    ) >> 2;
863             int Y2 = (buf0[i * 2 + 1]) >> 2;
864             int U  = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
865             int V  = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
866             int R, G, B;
867
868             Y1 -= c->yuv2rgb_y_offset;
869             Y2 -= c->yuv2rgb_y_offset;
870             Y1 *= c->yuv2rgb_y_coeff;
871             Y2 *= c->yuv2rgb_y_coeff;
872             Y1 += 1 << 13;
873             Y2 += 1 << 13;
874
875             if (hasAlpha) {
876                 A1 = abuf0[i * 2    ] << 11;
877                 A2 = abuf0[i * 2 + 1] << 11;
878
879                 A1 += 1 << 13;
880                 A2 += 1 << 13;
881             }
882
883             R = V * c->yuv2rgb_v2r_coeff;
884             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
885             B =                            U * c->yuv2rgb_u2b_coeff;
886
887             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
888             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
889             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
890             output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
891             output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
892             output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
893             output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
894             output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
895             dest += 8;
896         }
897     }
898 }
899
900 static av_always_inline void
901 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
902                        const int32_t **lumSrc, int lumFilterSize,
903                        const int16_t *chrFilter, const int32_t **chrUSrc,
904                        const int32_t **chrVSrc, int chrFilterSize,
905                        const int32_t **alpSrc, uint16_t *dest, int dstW,
906                        int y, enum AVPixelFormat target, int hasAlpha)
907 {
908     int i;
909
910     for (i = 0; i < ((dstW + 1) >> 1); i++) {
911         int j;
912         int Y1 = -0x40000000;
913         int Y2 = -0x40000000;
914         int U  = -128 << 23; // 19
915         int V  = -128 << 23;
916         int R, G, B;
917
918         for (j = 0; j < lumFilterSize; j++) {
919             Y1 += lumSrc[j][i * 2]     * (unsigned)lumFilter[j];
920             Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
921         }
922         for (j = 0; j < chrFilterSize; j++) {;
923             U += chrUSrc[j][i] * (unsigned)chrFilter[j];
924             V += chrVSrc[j][i] * (unsigned)chrFilter[j];
925         }
926
927         // 8bit: 12+15=27; 16-bit: 12+19=31
928         Y1 >>= 14; // 10
929         Y1 += 0x10000;
930         Y2 >>= 14;
931         Y2 += 0x10000;
932         U  >>= 14;
933         V  >>= 14;
934
935         // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
936         Y1 -= c->yuv2rgb_y_offset;
937         Y2 -= c->yuv2rgb_y_offset;
938         Y1 *= c->yuv2rgb_y_coeff;
939         Y2 *= c->yuv2rgb_y_coeff;
940         Y1 += 1 << 13; // 21
941         Y2 += 1 << 13;
942         // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
943
944         R = V * c->yuv2rgb_v2r_coeff;
945         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
946         B =                            U * c->yuv2rgb_u2b_coeff;
947
948         // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
949         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
950         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
951         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
952         output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
953         output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
954         output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
955         dest += 6;
956     }
957 }
958
959 static av_always_inline void
960 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
961                        const int32_t *ubuf[2], const int32_t *vbuf[2],
962                        const int32_t *abuf[2], uint16_t *dest, int dstW,
963                        int yalpha, int uvalpha, int y,
964                        enum AVPixelFormat target, int hasAlpha)
965 {
966     const int32_t *buf0  = buf[0],  *buf1  = buf[1],
967                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
968                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
969     int  yalpha1 = 4096 - yalpha;
970     int uvalpha1 = 4096 - uvalpha;
971     int i;
972
973     for (i = 0; i < ((dstW + 1) >> 1); i++) {
974         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha) >> 14;
975         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha) >> 14;
976         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha + (-128 << 23)) >> 14;
977         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha + (-128 << 23)) >> 14;
978         int R, G, B;
979
980         Y1 -= c->yuv2rgb_y_offset;
981         Y2 -= c->yuv2rgb_y_offset;
982         Y1 *= c->yuv2rgb_y_coeff;
983         Y2 *= c->yuv2rgb_y_coeff;
984         Y1 += 1 << 13;
985         Y2 += 1 << 13;
986
987         R = V * c->yuv2rgb_v2r_coeff;
988         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
989         B =                            U * c->yuv2rgb_u2b_coeff;
990
991         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
992         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
993         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
994         output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
995         output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
996         output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
997         dest += 6;
998     }
999 }
1000
1001 static av_always_inline void
1002 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
1003                        const int32_t *ubuf[2], const int32_t *vbuf[2],
1004                        const int32_t *abuf0, uint16_t *dest, int dstW,
1005                        int uvalpha, int y, enum AVPixelFormat target, int hasAlpha)
1006 {
1007     const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1008     int i;
1009
1010     if (uvalpha < 2048) {
1011         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1012             int Y1 = (buf0[i * 2]    ) >> 2;
1013             int Y2 = (buf0[i * 2 + 1]) >> 2;
1014             int U  = (ubuf0[i] + (-128 << 11)) >> 2;
1015             int V  = (vbuf0[i] + (-128 << 11)) >> 2;
1016             int R, G, B;
1017
1018             Y1 -= c->yuv2rgb_y_offset;
1019             Y2 -= c->yuv2rgb_y_offset;
1020             Y1 *= c->yuv2rgb_y_coeff;
1021             Y2 *= c->yuv2rgb_y_coeff;
1022             Y1 += 1 << 13;
1023             Y2 += 1 << 13;
1024
1025             R = V * c->yuv2rgb_v2r_coeff;
1026             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1027             B =                            U * c->yuv2rgb_u2b_coeff;
1028
1029             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
1030             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
1031             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
1032             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
1033             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
1034             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
1035             dest += 6;
1036         }
1037     } else {
1038         const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1039         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1040             int Y1 = (buf0[i * 2]    ) >> 2;
1041             int Y2 = (buf0[i * 2 + 1]) >> 2;
1042             int U  = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
1043             int V  = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
1044             int R, G, B;
1045
1046             Y1 -= c->yuv2rgb_y_offset;
1047             Y2 -= c->yuv2rgb_y_offset;
1048             Y1 *= c->yuv2rgb_y_coeff;
1049             Y2 *= c->yuv2rgb_y_coeff;
1050             Y1 += 1 << 13;
1051             Y2 += 1 << 13;
1052
1053             R = V * c->yuv2rgb_v2r_coeff;
1054             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1055             B =                            U * c->yuv2rgb_u2b_coeff;
1056
1057             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
1058             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
1059             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
1060             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
1061             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
1062             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
1063             dest += 6;
1064         }
1065     }
1066 }
1067
1068 #undef output_pixel
1069 #undef r_b
1070 #undef b_r
1071
1072 #define YUV2PACKED16WRAPPER(name, base, ext, fmt, hasAlpha) \
1073 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1074                         const int16_t **_lumSrc, int lumFilterSize, \
1075                         const int16_t *chrFilter, const int16_t **_chrUSrc, \
1076                         const int16_t **_chrVSrc, int chrFilterSize, \
1077                         const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
1078                         int y) \
1079 { \
1080     const int32_t **lumSrc  = (const int32_t **) _lumSrc, \
1081                   **chrUSrc = (const int32_t **) _chrUSrc, \
1082                   **chrVSrc = (const int32_t **) _chrVSrc, \
1083                   **alpSrc  = (const int32_t **) _alpSrc; \
1084     uint16_t *dest = (uint16_t *) _dest; \
1085     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1086                           chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1087                           alpSrc, dest, dstW, y, fmt, hasAlpha); \
1088 } \
1089  \
1090 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
1091                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
1092                         const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
1093                         int yalpha, int uvalpha, int y) \
1094 { \
1095     const int32_t **buf  = (const int32_t **) _buf, \
1096                   **ubuf = (const int32_t **) _ubuf, \
1097                   **vbuf = (const int32_t **) _vbuf, \
1098                   **abuf = (const int32_t **) _abuf; \
1099     uint16_t *dest = (uint16_t *) _dest; \
1100     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1101                           dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1102 } \
1103  \
1104 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
1105                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
1106                         const int16_t *_abuf0, uint8_t *_dest, int dstW, \
1107                         int uvalpha, int y) \
1108 { \
1109     const int32_t *buf0  = (const int32_t *)  _buf0, \
1110                  **ubuf  = (const int32_t **) _ubuf, \
1111                  **vbuf  = (const int32_t **) _vbuf, \
1112                   *abuf0 = (const int32_t *)  _abuf0; \
1113     uint16_t *dest = (uint16_t *) _dest; \
1114     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1115                                   dstW, uvalpha, y, fmt, hasAlpha); \
1116 }
1117
1118 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, AV_PIX_FMT_RGB48BE, 0)
1119 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, AV_PIX_FMT_RGB48LE, 0)
1120 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, AV_PIX_FMT_BGR48BE, 0)
1121 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, AV_PIX_FMT_BGR48LE, 0)
1122 YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64be, AV_PIX_FMT_RGBA64BE, 1)
1123 YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64le, AV_PIX_FMT_RGBA64LE, 1)
1124 YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64be, AV_PIX_FMT_RGBA64BE, 0)
1125 YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64le, AV_PIX_FMT_RGBA64LE, 0)
1126 YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64be, AV_PIX_FMT_BGRA64BE, 1)
1127 YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64le, AV_PIX_FMT_BGRA64LE, 1)
1128 YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64be, AV_PIX_FMT_BGRA64BE, 0)
1129 YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64le, AV_PIX_FMT_BGRA64LE, 0)
1130
1131 /*
1132  * Write out 2 RGB pixels in the target pixel format. This function takes a
1133  * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
1134  * things like endianness conversion and shifting. The caller takes care of
1135  * setting the correct offset in these tables from the chroma (U/V) values.
1136  * This function then uses the luminance (Y1/Y2) values to write out the
1137  * correct RGB values into the destination buffer.
1138  */
1139 static av_always_inline void
1140 yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
1141               unsigned A1, unsigned A2,
1142               const void *_r, const void *_g, const void *_b, int y,
1143               enum AVPixelFormat target, int hasAlpha)
1144 {
1145     if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
1146         target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
1147         uint32_t *dest = (uint32_t *) _dest;
1148         const uint32_t *r = (const uint32_t *) _r;
1149         const uint32_t *g = (const uint32_t *) _g;
1150         const uint32_t *b = (const uint32_t *) _b;
1151
1152 #if CONFIG_SMALL
1153         int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
1154
1155         dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
1156         dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
1157 #else
1158         if (hasAlpha) {
1159             int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
1160
1161             av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0);
1162             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
1163             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
1164         } else {
1165 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
1166             int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
1167
1168             av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0xFF);
1169 #endif
1170             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
1171             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
1172         }
1173 #endif
1174     } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
1175         uint8_t *dest = (uint8_t *) _dest;
1176         const uint8_t *r = (const uint8_t *) _r;
1177         const uint8_t *g = (const uint8_t *) _g;
1178         const uint8_t *b = (const uint8_t *) _b;
1179
1180 #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
1181 #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
1182
1183         dest[i * 6 + 0] = r_b[Y1];
1184         dest[i * 6 + 1] =   g[Y1];
1185         dest[i * 6 + 2] = b_r[Y1];
1186         dest[i * 6 + 3] = r_b[Y2];
1187         dest[i * 6 + 4] =   g[Y2];
1188         dest[i * 6 + 5] = b_r[Y2];
1189 #undef r_b
1190 #undef b_r
1191     } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
1192                target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
1193                target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
1194         uint16_t *dest = (uint16_t *) _dest;
1195         const uint16_t *r = (const uint16_t *) _r;
1196         const uint16_t *g = (const uint16_t *) _g;
1197         const uint16_t *b = (const uint16_t *) _b;
1198         int dr1, dg1, db1, dr2, dg2, db2;
1199
1200         if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
1201             dr1 = ff_dither_2x2_8[ y & 1     ][0];
1202             dg1 = ff_dither_2x2_4[ y & 1     ][0];
1203             db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
1204             dr2 = ff_dither_2x2_8[ y & 1     ][1];
1205             dg2 = ff_dither_2x2_4[ y & 1     ][1];
1206             db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
1207         } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
1208             dr1 = ff_dither_2x2_8[ y & 1     ][0];
1209             dg1 = ff_dither_2x2_8[ y & 1     ][1];
1210             db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
1211             dr2 = ff_dither_2x2_8[ y & 1     ][1];
1212             dg2 = ff_dither_2x2_8[ y & 1     ][0];
1213             db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
1214         } else {
1215             dr1 = ff_dither_4x4_16[ y & 3     ][0];
1216             dg1 = ff_dither_4x4_16[ y & 3     ][1];
1217             db1 = ff_dither_4x4_16[(y & 3) ^ 3][0];
1218             dr2 = ff_dither_4x4_16[ y & 3     ][1];
1219             dg2 = ff_dither_4x4_16[ y & 3     ][0];
1220             db2 = ff_dither_4x4_16[(y & 3) ^ 3][1];
1221         }
1222
1223         dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
1224         dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
1225     } else /* 8/4-bit */ {
1226         uint8_t *dest = (uint8_t *) _dest;
1227         const uint8_t *r = (const uint8_t *) _r;
1228         const uint8_t *g = (const uint8_t *) _g;
1229         const uint8_t *b = (const uint8_t *) _b;
1230         int dr1, dg1, db1, dr2, dg2, db2;
1231
1232         if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
1233             const uint8_t * const d64 = ff_dither_8x8_73[y & 7];
1234             const uint8_t * const d32 = ff_dither_8x8_32[y & 7];
1235             dr1 = dg1 = d32[(i * 2 + 0) & 7];
1236             db1 =       d64[(i * 2 + 0) & 7];
1237             dr2 = dg2 = d32[(i * 2 + 1) & 7];
1238             db2 =       d64[(i * 2 + 1) & 7];
1239         } else {
1240             const uint8_t * const d64  = ff_dither_8x8_73 [y & 7];
1241             const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
1242             dr1 = db1 = d128[(i * 2 + 0) & 7];
1243             dg1 =        d64[(i * 2 + 0) & 7];
1244             dr2 = db2 = d128[(i * 2 + 1) & 7];
1245             dg2 =        d64[(i * 2 + 1) & 7];
1246         }
1247
1248         if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
1249             dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
1250                     ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
1251         } else {
1252             dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
1253             dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
1254         }
1255     }
1256 }
1257
1258 static av_always_inline void
1259 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
1260                      const int16_t **lumSrc, int lumFilterSize,
1261                      const int16_t *chrFilter, const int16_t **chrUSrc,
1262                      const int16_t **chrVSrc, int chrFilterSize,
1263                      const int16_t **alpSrc, uint8_t *dest, int dstW,
1264                      int y, enum AVPixelFormat target, int hasAlpha)
1265 {
1266     int i;
1267
1268     for (i = 0; i < ((dstW + 1) >> 1); i++) {
1269         int j, A1, A2;
1270         int Y1 = 1 << 18;
1271         int Y2 = 1 << 18;
1272         int U  = 1 << 18;
1273         int V  = 1 << 18;
1274         const void *r, *g, *b;
1275
1276         for (j = 0; j < lumFilterSize; j++) {
1277             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
1278             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
1279         }
1280         for (j = 0; j < chrFilterSize; j++) {
1281             U += chrUSrc[j][i] * chrFilter[j];
1282             V += chrVSrc[j][i] * chrFilter[j];
1283         }
1284         Y1 >>= 19;
1285         Y2 >>= 19;
1286         U  >>= 19;
1287         V  >>= 19;
1288         if (hasAlpha) {
1289             A1 = 1 << 18;
1290             A2 = 1 << 18;
1291             for (j = 0; j < lumFilterSize; j++) {
1292                 A1 += alpSrc[j][i * 2    ] * lumFilter[j];
1293                 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
1294             }
1295             A1 >>= 19;
1296             A2 >>= 19;
1297             if ((A1 | A2) & 0x100) {
1298                 A1 = av_clip_uint8(A1);
1299                 A2 = av_clip_uint8(A2);
1300             }
1301         }
1302
1303         r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM];
1304         g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
1305         b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1306
1307         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1308                       r, g, b, y, target, hasAlpha);
1309     }
1310 }
1311
1312 static av_always_inline void
1313 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
1314                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1315                      const int16_t *abuf[2], uint8_t *dest, int dstW,
1316                      int yalpha, int uvalpha, int y,
1317                      enum AVPixelFormat target, int hasAlpha)
1318 {
1319     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
1320                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1321                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1322                   *abuf0 = hasAlpha ? abuf[0] : NULL,
1323                   *abuf1 = hasAlpha ? abuf[1] : NULL;
1324     int  yalpha1 = 4096 - yalpha;
1325     int uvalpha1 = 4096 - uvalpha;
1326     int i;
1327
1328     for (i = 0; i < ((dstW + 1) >> 1); i++) {
1329         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
1330         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
1331         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
1332         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
1333         int A1, A2;
1334         const void *r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1335                    *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1336                    *b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1337
1338         if (hasAlpha) {
1339             A1 = (abuf0[i * 2    ] * yalpha1 + abuf1[i * 2    ] * yalpha) >> 19;
1340             A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1341             A1 = av_clip_uint8(A1);
1342             A2 = av_clip_uint8(A2);
1343         }
1344
1345         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1346                       r, g, b, y, target, hasAlpha);
1347     }
1348 }
1349
1350 static av_always_inline void
1351 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1352                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1353                      const int16_t *abuf0, uint8_t *dest, int dstW,
1354                      int uvalpha, int y, enum AVPixelFormat target,
1355                      int hasAlpha)
1356 {
1357     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1358     int i;
1359
1360     if (uvalpha < 2048) {
1361         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1362             int Y1 = (buf0[i * 2    ] + 64) >> 7;
1363             int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1364             int U  = (ubuf0[i]        + 64) >> 7;
1365             int V  = (vbuf0[i]        + 64) >> 7;
1366             int A1, A2;
1367             const void *r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1368                        *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1369                        *b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1370
1371             if (hasAlpha) {
1372                 A1 = abuf0[i * 2    ] * 255 + 16384 >> 15;
1373                 A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
1374                 A1 = av_clip_uint8(A1);
1375                 A2 = av_clip_uint8(A2);
1376             }
1377
1378             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1379                           r, g, b, y, target, hasAlpha);
1380         }
1381     } else {
1382         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1383         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1384             int Y1 = (buf0[i * 2    ]     +  64) >> 7;
1385             int Y2 = (buf0[i * 2 + 1]     +  64) >> 7;
1386             int U  = (ubuf0[i] + ubuf1[i] + 128) >> 8;
1387             int V  = (vbuf0[i] + vbuf1[i] + 128) >> 8;
1388             int A1, A2;
1389             const void *r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1390                        *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1391                        *b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1392
1393             if (hasAlpha) {
1394                 A1 = (abuf0[i * 2    ] + 64) >> 7;
1395                 A2 = (abuf0[i * 2 + 1] + 64) >> 7;
1396                 A1 = av_clip_uint8(A1);
1397                 A2 = av_clip_uint8(A2);
1398             }
1399
1400             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1401                           r, g, b, y, target, hasAlpha);
1402         }
1403     }
1404 }
1405
1406 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1407 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1408                                 const int16_t **lumSrc, int lumFilterSize, \
1409                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
1410                                 const int16_t **chrVSrc, int chrFilterSize, \
1411                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1412                                 int y) \
1413 { \
1414     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1415                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1416                                   alpSrc, dest, dstW, y, fmt, hasAlpha); \
1417 }
1418
1419 #define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
1420 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1421 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1422                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1423                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1424                                 int yalpha, int uvalpha, int y) \
1425 { \
1426     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1427                                   dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1428 }
1429
1430 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1431 YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
1432 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1433                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1434                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
1435                                 int uvalpha, int y) \
1436 { \
1437     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1438                                   dstW, uvalpha, y, fmt, hasAlpha); \
1439 }
1440
1441 #if CONFIG_SMALL
1442 YUV2RGBWRAPPER(yuv2rgb,,  32_1,  AV_PIX_FMT_RGB32_1,   CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1443 YUV2RGBWRAPPER(yuv2rgb,,  32,    AV_PIX_FMT_RGB32,     CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1444 #else
1445 #if CONFIG_SWSCALE_ALPHA
1446 YUV2RGBWRAPPER(yuv2rgb,, a32_1,  AV_PIX_FMT_RGB32_1,   1)
1447 YUV2RGBWRAPPER(yuv2rgb,, a32,    AV_PIX_FMT_RGB32,     1)
1448 #endif
1449 YUV2RGBWRAPPER(yuv2rgb,, x32_1,  AV_PIX_FMT_RGB32_1,   0)
1450 YUV2RGBWRAPPER(yuv2rgb,, x32,    AV_PIX_FMT_RGB32,     0)
1451 #endif
1452 YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24,   0)
1453 YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24,   0)
1454 YUV2RGBWRAPPER(yuv2rgb,,  16,    AV_PIX_FMT_RGB565,    0)
1455 YUV2RGBWRAPPER(yuv2rgb,,  15,    AV_PIX_FMT_RGB555,    0)
1456 YUV2RGBWRAPPER(yuv2rgb,,  12,    AV_PIX_FMT_RGB444,    0)
1457 YUV2RGBWRAPPER(yuv2rgb,,   8,    AV_PIX_FMT_RGB8,      0)
1458 YUV2RGBWRAPPER(yuv2rgb,,   4,    AV_PIX_FMT_RGB4,      0)
1459 YUV2RGBWRAPPER(yuv2rgb,,   4b,   AV_PIX_FMT_RGB4_BYTE, 0)
1460
1461 static av_always_inline void yuv2rgb_write_full(SwsContext *c,
1462     uint8_t *dest, int i, int Y, int A, int U, int V,
1463     int y, enum AVPixelFormat target, int hasAlpha, int err[4])
1464 {
1465     int R, G, B;
1466     int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
1467
1468     Y -= c->yuv2rgb_y_offset;
1469     Y *= c->yuv2rgb_y_coeff;
1470     Y += 1 << 21;
1471     R = Y + V*c->yuv2rgb_v2r_coeff;
1472     G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1473     B = Y +                          U*c->yuv2rgb_u2b_coeff;
1474     if ((R | G | B) & 0xC0000000) {
1475         R = av_clip_uintp2(R, 30);
1476         G = av_clip_uintp2(G, 30);
1477         B = av_clip_uintp2(B, 30);
1478     }
1479
1480     switch(target) {
1481     case AV_PIX_FMT_ARGB:
1482         dest[0] = hasAlpha ? A : 255;
1483         dest[1] = R >> 22;
1484         dest[2] = G >> 22;
1485         dest[3] = B >> 22;
1486         break;
1487     case AV_PIX_FMT_RGB24:
1488         dest[0] = R >> 22;
1489         dest[1] = G >> 22;
1490         dest[2] = B >> 22;
1491         break;
1492     case AV_PIX_FMT_RGBA:
1493         dest[0] = R >> 22;
1494         dest[1] = G >> 22;
1495         dest[2] = B >> 22;
1496         dest[3] = hasAlpha ? A : 255;
1497         break;
1498     case AV_PIX_FMT_ABGR:
1499         dest[0] = hasAlpha ? A : 255;
1500         dest[1] = B >> 22;
1501         dest[2] = G >> 22;
1502         dest[3] = R >> 22;
1503         break;
1504     case AV_PIX_FMT_BGR24:
1505         dest[0] = B >> 22;
1506         dest[1] = G >> 22;
1507         dest[2] = R >> 22;
1508         break;
1509     case AV_PIX_FMT_BGRA:
1510         dest[0] = B >> 22;
1511         dest[1] = G >> 22;
1512         dest[2] = R >> 22;
1513         dest[3] = hasAlpha ? A : 255;
1514         break;
1515     case AV_PIX_FMT_BGR4_BYTE:
1516     case AV_PIX_FMT_RGB4_BYTE:
1517     case AV_PIX_FMT_BGR8:
1518     case AV_PIX_FMT_RGB8:
1519     {
1520         int r,g,b;
1521
1522         switch (c->dither) {
1523         default:
1524         case SWS_DITHER_AUTO:
1525         case SWS_DITHER_ED:
1526             R >>= 22;
1527             G >>= 22;
1528             B >>= 22;
1529             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;
1530             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;
1531             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;
1532             c->dither_error[0][i] = err[0];
1533             c->dither_error[1][i] = err[1];
1534             c->dither_error[2][i] = err[2];
1535             r = R >> (isrgb8 ? 5 : 7);
1536             g = G >> (isrgb8 ? 5 : 6);
1537             b = B >> (isrgb8 ? 6 : 7);
1538             r = av_clip(r, 0, isrgb8 ? 7 : 1);
1539             g = av_clip(g, 0, isrgb8 ? 7 : 3);
1540             b = av_clip(b, 0, isrgb8 ? 3 : 1);
1541             err[0] = R - r*(isrgb8 ? 36 : 255);
1542             err[1] = G - g*(isrgb8 ? 36 : 85);
1543             err[2] = B - b*(isrgb8 ? 85 : 255);
1544             break;
1545         case SWS_DITHER_A_DITHER:
1546             if (isrgb8) {
1547   /* see http://pippin.gimp.org/a_dither/ for details/origin */
1548 #define A_DITHER(u,v)   (((((u)+((v)*236))*119)&0xff))
1549                 r = (((R >> 19) + A_DITHER(i,y)  -96)>>8);
1550                 g = (((G >> 19) + A_DITHER(i + 17,y) - 96)>>8);
1551                 b = (((B >> 20) + A_DITHER(i + 17*2,y) -96)>>8);
1552                 r = av_clip(r, 0, 7);
1553                 g = av_clip(g, 0, 7);
1554                 b = av_clip(b, 0, 3);
1555             } else {
1556                 r = (((R >> 21) + A_DITHER(i,y)-256)>>8);
1557                 g = (((G >> 19) + A_DITHER(i + 17,y)-256)>>8);
1558                 b = (((B >> 21) + A_DITHER(i + 17*2,y)-256)>>8);
1559                 r = av_clip(r, 0, 1);
1560                 g = av_clip(g, 0, 3);
1561                 b = av_clip(b, 0, 1);
1562             }
1563             break;
1564         case SWS_DITHER_X_DITHER:
1565             if (isrgb8) {
1566   /* see http://pippin.gimp.org/a_dither/ for details/origin */
1567 #define X_DITHER(u,v)   (((((u)^((v)*237))*181)&0x1ff)/2)
1568                 r = (((R >> 19) + X_DITHER(i,y) - 96)>>8);
1569                 g = (((G >> 19) + X_DITHER(i + 17,y) - 96)>>8);
1570                 b = (((B >> 20) + X_DITHER(i + 17*2,y) - 96)>>8);
1571                 r = av_clip(r, 0, 7);
1572                 g = av_clip(g, 0, 7);
1573                 b = av_clip(b, 0, 3);
1574             } else {
1575                 r = (((R >> 21) + X_DITHER(i,y)-256)>>8);
1576                 g = (((G >> 19) + X_DITHER(i + 17,y)-256)>>8);
1577                 b = (((B >> 21) + X_DITHER(i + 17*2,y)-256)>>8);
1578                 r = av_clip(r, 0, 1);
1579                 g = av_clip(g, 0, 3);
1580                 b = av_clip(b, 0, 1);
1581             }
1582
1583             break;
1584         }
1585
1586         if(target == AV_PIX_FMT_BGR4_BYTE) {
1587             dest[0] = r + 2*g + 8*b;
1588         } else if(target == AV_PIX_FMT_RGB4_BYTE) {
1589             dest[0] = b + 2*g + 8*r;
1590         } else if(target == AV_PIX_FMT_BGR8) {
1591             dest[0] = r + 8*g + 64*b;
1592         } else if(target == AV_PIX_FMT_RGB8) {
1593             dest[0] = b + 4*g + 32*r;
1594         } else
1595             av_assert2(0);
1596         break;}
1597     }
1598 }
1599
1600 static av_always_inline void
1601 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1602                           const int16_t **lumSrc, int lumFilterSize,
1603                           const int16_t *chrFilter, const int16_t **chrUSrc,
1604                           const int16_t **chrVSrc, int chrFilterSize,
1605                           const int16_t **alpSrc, uint8_t *dest,
1606                           int dstW, int y, enum AVPixelFormat target, int hasAlpha)
1607 {
1608     int i;
1609     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1610     int err[4] = {0};
1611     int A = 0; //init to silence warning
1612
1613     if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1614        || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
1615         step = 1;
1616
1617     for (i = 0; i < dstW; i++) {
1618         int j;
1619         int Y = 1<<9;
1620         int U = (1<<9)-(128 << 19);
1621         int V = (1<<9)-(128 << 19);
1622
1623         for (j = 0; j < lumFilterSize; j++) {
1624             Y += lumSrc[j][i] * lumFilter[j];
1625         }
1626         for (j = 0; j < chrFilterSize; j++) {
1627             U += chrUSrc[j][i] * chrFilter[j];
1628             V += chrVSrc[j][i] * chrFilter[j];
1629         }
1630         Y >>= 10;
1631         U >>= 10;
1632         V >>= 10;
1633         if (hasAlpha) {
1634             A = 1 << 18;
1635             for (j = 0; j < lumFilterSize; j++) {
1636                 A += alpSrc[j][i] * lumFilter[j];
1637             }
1638             A >>= 19;
1639             if (A & 0x100)
1640                 A = av_clip_uint8(A);
1641         }
1642         yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1643         dest += step;
1644     }
1645     c->dither_error[0][i] = err[0];
1646     c->dither_error[1][i] = err[1];
1647     c->dither_error[2][i] = err[2];
1648 }
1649
1650 static av_always_inline void
1651 yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
1652                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1653                      const int16_t *abuf[2], uint8_t *dest, int dstW,
1654                      int yalpha, int uvalpha, int y,
1655                      enum AVPixelFormat target, int hasAlpha)
1656 {
1657     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
1658                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1659                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1660                   *abuf0 = hasAlpha ? abuf[0] : NULL,
1661                   *abuf1 = hasAlpha ? abuf[1] : NULL;
1662     int  yalpha1 = 4096 - yalpha;
1663     int uvalpha1 = 4096 - uvalpha;
1664     int i;
1665     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1666     int err[4] = {0};
1667     int A = 0; // init to silcene warning
1668
1669     if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1670        || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
1671         step = 1;
1672
1673     for (i = 0; i < dstW; i++) {
1674         int Y = ( buf0[i] * yalpha1  +  buf1[i] * yalpha             ) >> 10; //FIXME rounding
1675         int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
1676         int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
1677
1678         if (hasAlpha) {
1679             A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
1680             if (A & 0x100)
1681                 A = av_clip_uint8(A);
1682         }
1683
1684         yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1685         dest += step;
1686     }
1687     c->dither_error[0][i] = err[0];
1688     c->dither_error[1][i] = err[1];
1689     c->dither_error[2][i] = err[2];
1690 }
1691
1692 static av_always_inline void
1693 yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
1694                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1695                      const int16_t *abuf0, uint8_t *dest, int dstW,
1696                      int uvalpha, int y, enum AVPixelFormat target,
1697                      int hasAlpha)
1698 {
1699     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1700     int i;
1701     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1702     int err[4] = {0};
1703
1704     if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
1705        || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
1706         step = 1;
1707
1708     if (uvalpha < 2048) {
1709         int A = 0; //init to silence warning
1710         for (i = 0; i < dstW; i++) {
1711             int Y = buf0[i] << 2;
1712             int U = (ubuf0[i] - (128<<7)) << 2;
1713             int V = (vbuf0[i] - (128<<7)) << 2;
1714
1715             if (hasAlpha) {
1716                 A = (abuf0[i] + 64) >> 7;
1717                 if (A & 0x100)
1718                     A = av_clip_uint8(A);
1719             }
1720
1721             yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1722             dest += step;
1723         }
1724     } else {
1725         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1726         int A = 0; //init to silence warning
1727         for (i = 0; i < dstW; i++) {
1728             int Y = buf0[i] << 2;
1729             int U = (ubuf0[i] + ubuf1[i] - (128<<8)) << 1;
1730             int V = (vbuf0[i] + vbuf1[i] - (128<<8)) << 1;
1731
1732             if (hasAlpha) {
1733                 A = (abuf0[i] + 64) >> 7;
1734                 if (A & 0x100)
1735                     A = av_clip_uint8(A);
1736             }
1737
1738             yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
1739             dest += step;
1740         }
1741     }
1742
1743     c->dither_error[0][i] = err[0];
1744     c->dither_error[1][i] = err[1];
1745     c->dither_error[2][i] = err[2];
1746 }
1747
1748 #if CONFIG_SMALL
1749 YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1750 YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1751 YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1752 YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1753 #else
1754 #if CONFIG_SWSCALE_ALPHA
1755 YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  1)
1756 YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  1)
1757 YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  1)
1758 YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  1)
1759 #endif
1760 YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA,  0)
1761 YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR,  0)
1762 YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA,  0)
1763 YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB,  0)
1764 #endif
1765 YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full,  AV_PIX_FMT_BGR24, 0)
1766 YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full,  AV_PIX_FMT_RGB24, 0)
1767
1768 YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full,  AV_PIX_FMT_BGR4_BYTE, 0)
1769 YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full,  AV_PIX_FMT_RGB4_BYTE, 0)
1770 YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full,   AV_PIX_FMT_BGR8,  0)
1771 YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full,   AV_PIX_FMT_RGB8,  0)
1772
1773 static void
1774 yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
1775                   const int16_t **lumSrc, int lumFilterSize,
1776                   const int16_t *chrFilter, const int16_t **chrUSrc,
1777                   const int16_t **chrVSrc, int chrFilterSize,
1778                   const int16_t **alpSrc, uint8_t **dest,
1779                   int dstW, int y)
1780 {
1781     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
1782     int i;
1783     int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
1784     uint16_t **dest16 = (uint16_t**)dest;
1785     int SH = 22 + 7 - desc->comp[0].depth_minus1;
1786     int A = 0; // init to silence warning
1787
1788     for (i = 0; i < dstW; i++) {
1789         int j;
1790         int Y = 1 << 9;
1791         int U = (1 << 9) - (128 << 19);
1792         int V = (1 << 9) - (128 << 19);
1793         int R, G, B;
1794
1795         for (j = 0; j < lumFilterSize; j++)
1796             Y += lumSrc[j][i] * lumFilter[j];
1797
1798         for (j = 0; j < chrFilterSize; j++) {
1799             U += chrUSrc[j][i] * chrFilter[j];
1800             V += chrVSrc[j][i] * chrFilter[j];
1801         }
1802
1803         Y >>= 10;
1804         U >>= 10;
1805         V >>= 10;
1806
1807         if (hasAlpha) {
1808             A = 1 << 18;
1809
1810             for (j = 0; j < lumFilterSize; j++)
1811                 A += alpSrc[j][i] * lumFilter[j];
1812
1813             A >>= 19;
1814
1815             if (A & 0x100)
1816                 A = av_clip_uint8(A);
1817         }
1818
1819         Y -= c->yuv2rgb_y_offset;
1820         Y *= c->yuv2rgb_y_coeff;
1821         Y += 1 << 21;
1822         R = Y + V * c->yuv2rgb_v2r_coeff;
1823         G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1824         B = Y +                            U * c->yuv2rgb_u2b_coeff;
1825
1826         if ((R | G | B) & 0xC0000000) {
1827             R = av_clip_uintp2(R, 30);
1828             G = av_clip_uintp2(G, 30);
1829             B = av_clip_uintp2(B, 30);
1830         }
1831
1832         if (SH != 22) {
1833             dest16[0][i] = G >> SH;
1834             dest16[1][i] = B >> SH;
1835             dest16[2][i] = R >> SH;
1836             if (hasAlpha)
1837                 dest16[3][i] = A;
1838         } else {
1839             dest[0][i] = G >> 22;
1840             dest[1][i] = B >> 22;
1841             dest[2][i] = R >> 22;
1842             if (hasAlpha)
1843                 dest[3][i] = A;
1844         }
1845     }
1846     if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
1847         for (i = 0; i < dstW; i++) {
1848             dest16[0][i] = av_bswap16(dest16[0][i]);
1849             dest16[1][i] = av_bswap16(dest16[1][i]);
1850             dest16[2][i] = av_bswap16(dest16[2][i]);
1851             if (hasAlpha)
1852                 dest16[3][i] = av_bswap16(dest16[3][i]);
1853         }
1854     }
1855 }
1856
1857 av_cold void ff_sws_init_output_funcs(SwsContext *c,
1858                                       yuv2planar1_fn *yuv2plane1,
1859                                       yuv2planarX_fn *yuv2planeX,
1860                                       yuv2interleavedX_fn *yuv2nv12cX,
1861                                       yuv2packed1_fn *yuv2packed1,
1862                                       yuv2packed2_fn *yuv2packed2,
1863                                       yuv2packedX_fn *yuv2packedX,
1864                                       yuv2anyX_fn *yuv2anyX)
1865 {
1866     enum AVPixelFormat dstFormat = c->dstFormat;
1867     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
1868
1869     if (is16BPS(dstFormat)) {
1870         *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c  : yuv2planeX_16LE_c;
1871         *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c  : yuv2plane1_16LE_c;
1872     } else if (is9_OR_10BPS(dstFormat)) {
1873         if (desc->comp[0].depth_minus1 == 8) {
1874             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c  : yuv2planeX_9LE_c;
1875             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c  : yuv2plane1_9LE_c;
1876         } else if (desc->comp[0].depth_minus1 == 9) {
1877             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c  : yuv2planeX_10LE_c;
1878             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c  : yuv2plane1_10LE_c;
1879         } else if (desc->comp[0].depth_minus1 == 11) {
1880             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c  : yuv2planeX_12LE_c;
1881             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c  : yuv2plane1_12LE_c;
1882         } else if (desc->comp[0].depth_minus1 == 13) {
1883             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c  : yuv2planeX_14LE_c;
1884             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c  : yuv2plane1_14LE_c;
1885         } else
1886             av_assert0(0);
1887     } else {
1888         *yuv2plane1 = yuv2plane1_8_c;
1889         *yuv2planeX = yuv2planeX_8_c;
1890         if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
1891             *yuv2nv12cX = yuv2nv12cX_c;
1892     }
1893
1894     if(c->flags & SWS_FULL_CHR_H_INT) {
1895         switch (dstFormat) {
1896             case AV_PIX_FMT_RGBA:
1897 #if CONFIG_SMALL
1898                 *yuv2packedX = yuv2rgba32_full_X_c;
1899                 *yuv2packed2 = yuv2rgba32_full_2_c;
1900                 *yuv2packed1 = yuv2rgba32_full_1_c;
1901 #else
1902 #if CONFIG_SWSCALE_ALPHA
1903                 if (c->alpPixBuf) {
1904                     *yuv2packedX = yuv2rgba32_full_X_c;
1905                     *yuv2packed2 = yuv2rgba32_full_2_c;
1906                     *yuv2packed1 = yuv2rgba32_full_1_c;
1907                 } else
1908 #endif /* CONFIG_SWSCALE_ALPHA */
1909                 {
1910                     *yuv2packedX = yuv2rgbx32_full_X_c;
1911                     *yuv2packed2 = yuv2rgbx32_full_2_c;
1912                     *yuv2packed1 = yuv2rgbx32_full_1_c;
1913                 }
1914 #endif /* !CONFIG_SMALL */
1915                 break;
1916             case AV_PIX_FMT_ARGB:
1917 #if CONFIG_SMALL
1918                 *yuv2packedX = yuv2argb32_full_X_c;
1919                 *yuv2packed2 = yuv2argb32_full_2_c;
1920                 *yuv2packed1 = yuv2argb32_full_1_c;
1921 #else
1922 #if CONFIG_SWSCALE_ALPHA
1923                 if (c->alpPixBuf) {
1924                     *yuv2packedX = yuv2argb32_full_X_c;
1925                     *yuv2packed2 = yuv2argb32_full_2_c;
1926                     *yuv2packed1 = yuv2argb32_full_1_c;
1927                 } else
1928 #endif /* CONFIG_SWSCALE_ALPHA */
1929                 {
1930                     *yuv2packedX = yuv2xrgb32_full_X_c;
1931                     *yuv2packed2 = yuv2xrgb32_full_2_c;
1932                     *yuv2packed1 = yuv2xrgb32_full_1_c;
1933                 }
1934 #endif /* !CONFIG_SMALL */
1935                 break;
1936             case AV_PIX_FMT_BGRA:
1937 #if CONFIG_SMALL
1938                 *yuv2packedX = yuv2bgra32_full_X_c;
1939                 *yuv2packed2 = yuv2bgra32_full_2_c;
1940                 *yuv2packed1 = yuv2bgra32_full_1_c;
1941 #else
1942 #if CONFIG_SWSCALE_ALPHA
1943                 if (c->alpPixBuf) {
1944                     *yuv2packedX = yuv2bgra32_full_X_c;
1945                     *yuv2packed2 = yuv2bgra32_full_2_c;
1946                     *yuv2packed1 = yuv2bgra32_full_1_c;
1947                 } else
1948 #endif /* CONFIG_SWSCALE_ALPHA */
1949                 {
1950                     *yuv2packedX = yuv2bgrx32_full_X_c;
1951                     *yuv2packed2 = yuv2bgrx32_full_2_c;
1952                     *yuv2packed1 = yuv2bgrx32_full_1_c;
1953                 }
1954 #endif /* !CONFIG_SMALL */
1955                 break;
1956             case AV_PIX_FMT_ABGR:
1957 #if CONFIG_SMALL
1958                 *yuv2packedX = yuv2abgr32_full_X_c;
1959                 *yuv2packed2 = yuv2abgr32_full_2_c;
1960                 *yuv2packed1 = yuv2abgr32_full_1_c;
1961 #else
1962 #if CONFIG_SWSCALE_ALPHA
1963                 if (c->alpPixBuf) {
1964                     *yuv2packedX = yuv2abgr32_full_X_c;
1965                     *yuv2packed2 = yuv2abgr32_full_2_c;
1966                     *yuv2packed1 = yuv2abgr32_full_1_c;
1967                 } else
1968 #endif /* CONFIG_SWSCALE_ALPHA */
1969                 {
1970                     *yuv2packedX = yuv2xbgr32_full_X_c;
1971                     *yuv2packed2 = yuv2xbgr32_full_2_c;
1972                     *yuv2packed1 = yuv2xbgr32_full_1_c;
1973                 }
1974 #endif /* !CONFIG_SMALL */
1975                 break;
1976             case AV_PIX_FMT_RGB24:
1977             *yuv2packedX = yuv2rgb24_full_X_c;
1978             *yuv2packed2 = yuv2rgb24_full_2_c;
1979             *yuv2packed1 = yuv2rgb24_full_1_c;
1980             break;
1981         case AV_PIX_FMT_BGR24:
1982             *yuv2packedX = yuv2bgr24_full_X_c;
1983             *yuv2packed2 = yuv2bgr24_full_2_c;
1984             *yuv2packed1 = yuv2bgr24_full_1_c;
1985             break;
1986         case AV_PIX_FMT_BGR4_BYTE:
1987             *yuv2packedX = yuv2bgr4_byte_full_X_c;
1988             *yuv2packed2 = yuv2bgr4_byte_full_2_c;
1989             *yuv2packed1 = yuv2bgr4_byte_full_1_c;
1990             break;
1991         case AV_PIX_FMT_RGB4_BYTE:
1992             *yuv2packedX = yuv2rgb4_byte_full_X_c;
1993             *yuv2packed2 = yuv2rgb4_byte_full_2_c;
1994             *yuv2packed1 = yuv2rgb4_byte_full_1_c;
1995             break;
1996         case AV_PIX_FMT_BGR8:
1997             *yuv2packedX = yuv2bgr8_full_X_c;
1998             *yuv2packed2 = yuv2bgr8_full_2_c;
1999             *yuv2packed1 = yuv2bgr8_full_1_c;
2000             break;
2001         case AV_PIX_FMT_RGB8:
2002             *yuv2packedX = yuv2rgb8_full_X_c;
2003             *yuv2packed2 = yuv2rgb8_full_2_c;
2004             *yuv2packed1 = yuv2rgb8_full_1_c;
2005             break;
2006         case AV_PIX_FMT_GBRP:
2007         case AV_PIX_FMT_GBRP9BE:
2008         case AV_PIX_FMT_GBRP9LE:
2009         case AV_PIX_FMT_GBRP10BE:
2010         case AV_PIX_FMT_GBRP10LE:
2011         case AV_PIX_FMT_GBRP12BE:
2012         case AV_PIX_FMT_GBRP12LE:
2013         case AV_PIX_FMT_GBRP14BE:
2014         case AV_PIX_FMT_GBRP14LE:
2015         case AV_PIX_FMT_GBRP16BE:
2016         case AV_PIX_FMT_GBRP16LE:
2017         case AV_PIX_FMT_GBRAP:
2018             *yuv2anyX = yuv2gbrp_full_X_c;
2019             break;
2020         }
2021         if (!*yuv2packedX && !*yuv2anyX)
2022             goto YUV_PACKED;
2023     } else {
2024         YUV_PACKED:
2025         switch (dstFormat) {
2026         case AV_PIX_FMT_RGBA64LE:
2027 #if CONFIG_SWSCALE_ALPHA
2028             if (c->alpPixBuf) {
2029                 *yuv2packed1 = yuv2rgba64le_1_c;
2030                 *yuv2packed2 = yuv2rgba64le_2_c;
2031                 *yuv2packedX = yuv2rgba64le_X_c;
2032             } else
2033 #endif /* CONFIG_SWSCALE_ALPHA */
2034             {
2035                 *yuv2packed1 = yuv2rgbx64le_1_c;
2036                 *yuv2packed2 = yuv2rgbx64le_2_c;
2037                 *yuv2packedX = yuv2rgbx64le_X_c;
2038             }
2039             break;
2040         case AV_PIX_FMT_RGBA64BE:
2041 #if CONFIG_SWSCALE_ALPHA
2042             if (c->alpPixBuf) {
2043                 *yuv2packed1 = yuv2rgba64be_1_c;
2044                 *yuv2packed2 = yuv2rgba64be_2_c;
2045                 *yuv2packedX = yuv2rgba64be_X_c;
2046             } else
2047 #endif /* CONFIG_SWSCALE_ALPHA */
2048             {
2049                 *yuv2packed1 = yuv2rgbx64be_1_c;
2050                 *yuv2packed2 = yuv2rgbx64be_2_c;
2051                 *yuv2packedX = yuv2rgbx64be_X_c;
2052             }
2053             break;
2054         case AV_PIX_FMT_BGRA64LE:
2055 #if CONFIG_SWSCALE_ALPHA
2056             if (c->alpPixBuf) {
2057                 *yuv2packed1 = yuv2bgra64le_1_c;
2058                 *yuv2packed2 = yuv2bgra64le_2_c;
2059                 *yuv2packedX = yuv2bgra64le_X_c;
2060             } else
2061 #endif /* CONFIG_SWSCALE_ALPHA */
2062             {
2063                 *yuv2packed1 = yuv2bgrx64le_1_c;
2064                 *yuv2packed2 = yuv2bgrx64le_2_c;
2065                 *yuv2packedX = yuv2bgrx64le_X_c;
2066             }
2067             break;
2068         case AV_PIX_FMT_BGRA64BE:
2069 #if CONFIG_SWSCALE_ALPHA
2070             if (c->alpPixBuf) {
2071                 *yuv2packed1 = yuv2bgra64be_1_c;
2072                 *yuv2packed2 = yuv2bgra64be_2_c;
2073                 *yuv2packedX = yuv2bgra64be_X_c;
2074             } else
2075 #endif /* CONFIG_SWSCALE_ALPHA */
2076             {
2077                 *yuv2packed1 = yuv2bgrx64be_1_c;
2078                 *yuv2packed2 = yuv2bgrx64be_2_c;
2079                 *yuv2packedX = yuv2bgrx64be_X_c;
2080             }
2081             break;
2082         case AV_PIX_FMT_RGB48LE:
2083             *yuv2packed1 = yuv2rgb48le_1_c;
2084             *yuv2packed2 = yuv2rgb48le_2_c;
2085             *yuv2packedX = yuv2rgb48le_X_c;
2086             break;
2087         case AV_PIX_FMT_RGB48BE:
2088             *yuv2packed1 = yuv2rgb48be_1_c;
2089             *yuv2packed2 = yuv2rgb48be_2_c;
2090             *yuv2packedX = yuv2rgb48be_X_c;
2091             break;
2092         case AV_PIX_FMT_BGR48LE:
2093             *yuv2packed1 = yuv2bgr48le_1_c;
2094             *yuv2packed2 = yuv2bgr48le_2_c;
2095             *yuv2packedX = yuv2bgr48le_X_c;
2096             break;
2097         case AV_PIX_FMT_BGR48BE:
2098             *yuv2packed1 = yuv2bgr48be_1_c;
2099             *yuv2packed2 = yuv2bgr48be_2_c;
2100             *yuv2packedX = yuv2bgr48be_X_c;
2101             break;
2102         case AV_PIX_FMT_RGB32:
2103         case AV_PIX_FMT_BGR32:
2104 #if CONFIG_SMALL
2105             *yuv2packed1 = yuv2rgb32_1_c;
2106             *yuv2packed2 = yuv2rgb32_2_c;
2107             *yuv2packedX = yuv2rgb32_X_c;
2108 #else
2109 #if CONFIG_SWSCALE_ALPHA
2110                 if (c->alpPixBuf) {
2111                     *yuv2packed1 = yuv2rgba32_1_c;
2112                     *yuv2packed2 = yuv2rgba32_2_c;
2113                     *yuv2packedX = yuv2rgba32_X_c;
2114                 } else
2115 #endif /* CONFIG_SWSCALE_ALPHA */
2116                 {
2117                     *yuv2packed1 = yuv2rgbx32_1_c;
2118                     *yuv2packed2 = yuv2rgbx32_2_c;
2119                     *yuv2packedX = yuv2rgbx32_X_c;
2120                 }
2121 #endif /* !CONFIG_SMALL */
2122             break;
2123         case AV_PIX_FMT_RGB32_1:
2124         case AV_PIX_FMT_BGR32_1:
2125 #if CONFIG_SMALL
2126                 *yuv2packed1 = yuv2rgb32_1_1_c;
2127                 *yuv2packed2 = yuv2rgb32_1_2_c;
2128                 *yuv2packedX = yuv2rgb32_1_X_c;
2129 #else
2130 #if CONFIG_SWSCALE_ALPHA
2131                 if (c->alpPixBuf) {
2132                     *yuv2packed1 = yuv2rgba32_1_1_c;
2133                     *yuv2packed2 = yuv2rgba32_1_2_c;
2134                     *yuv2packedX = yuv2rgba32_1_X_c;
2135                 } else
2136 #endif /* CONFIG_SWSCALE_ALPHA */
2137                 {
2138                     *yuv2packed1 = yuv2rgbx32_1_1_c;
2139                     *yuv2packed2 = yuv2rgbx32_1_2_c;
2140                     *yuv2packedX = yuv2rgbx32_1_X_c;
2141                 }
2142 #endif /* !CONFIG_SMALL */
2143                 break;
2144         case AV_PIX_FMT_RGB24:
2145             *yuv2packed1 = yuv2rgb24_1_c;
2146             *yuv2packed2 = yuv2rgb24_2_c;
2147             *yuv2packedX = yuv2rgb24_X_c;
2148             break;
2149         case AV_PIX_FMT_BGR24:
2150             *yuv2packed1 = yuv2bgr24_1_c;
2151             *yuv2packed2 = yuv2bgr24_2_c;
2152             *yuv2packedX = yuv2bgr24_X_c;
2153             break;
2154         case AV_PIX_FMT_RGB565LE:
2155         case AV_PIX_FMT_RGB565BE:
2156         case AV_PIX_FMT_BGR565LE:
2157         case AV_PIX_FMT_BGR565BE:
2158             *yuv2packed1 = yuv2rgb16_1_c;
2159             *yuv2packed2 = yuv2rgb16_2_c;
2160             *yuv2packedX = yuv2rgb16_X_c;
2161             break;
2162         case AV_PIX_FMT_RGB555LE:
2163         case AV_PIX_FMT_RGB555BE:
2164         case AV_PIX_FMT_BGR555LE:
2165         case AV_PIX_FMT_BGR555BE:
2166             *yuv2packed1 = yuv2rgb15_1_c;
2167             *yuv2packed2 = yuv2rgb15_2_c;
2168             *yuv2packedX = yuv2rgb15_X_c;
2169             break;
2170         case AV_PIX_FMT_RGB444LE:
2171         case AV_PIX_FMT_RGB444BE:
2172         case AV_PIX_FMT_BGR444LE:
2173         case AV_PIX_FMT_BGR444BE:
2174             *yuv2packed1 = yuv2rgb12_1_c;
2175             *yuv2packed2 = yuv2rgb12_2_c;
2176             *yuv2packedX = yuv2rgb12_X_c;
2177             break;
2178         case AV_PIX_FMT_RGB8:
2179         case AV_PIX_FMT_BGR8:
2180             *yuv2packed1 = yuv2rgb8_1_c;
2181             *yuv2packed2 = yuv2rgb8_2_c;
2182             *yuv2packedX = yuv2rgb8_X_c;
2183             break;
2184         case AV_PIX_FMT_RGB4:
2185         case AV_PIX_FMT_BGR4:
2186             *yuv2packed1 = yuv2rgb4_1_c;
2187             *yuv2packed2 = yuv2rgb4_2_c;
2188             *yuv2packedX = yuv2rgb4_X_c;
2189             break;
2190         case AV_PIX_FMT_RGB4_BYTE:
2191         case AV_PIX_FMT_BGR4_BYTE:
2192             *yuv2packed1 = yuv2rgb4b_1_c;
2193             *yuv2packed2 = yuv2rgb4b_2_c;
2194             *yuv2packedX = yuv2rgb4b_X_c;
2195             break;
2196         }
2197     }
2198     switch (dstFormat) {
2199     case AV_PIX_FMT_MONOWHITE:
2200         *yuv2packed1 = yuv2monowhite_1_c;
2201         *yuv2packed2 = yuv2monowhite_2_c;
2202         *yuv2packedX = yuv2monowhite_X_c;
2203         break;
2204     case AV_PIX_FMT_MONOBLACK:
2205         *yuv2packed1 = yuv2monoblack_1_c;
2206         *yuv2packed2 = yuv2monoblack_2_c;
2207         *yuv2packedX = yuv2monoblack_X_c;
2208         break;
2209     case AV_PIX_FMT_YUYV422:
2210         *yuv2packed1 = yuv2yuyv422_1_c;
2211         *yuv2packed2 = yuv2yuyv422_2_c;
2212         *yuv2packedX = yuv2yuyv422_X_c;
2213         break;
2214     case AV_PIX_FMT_YVYU422:
2215         *yuv2packed1 = yuv2yvyu422_1_c;
2216         *yuv2packed2 = yuv2yvyu422_2_c;
2217         *yuv2packedX = yuv2yvyu422_X_c;
2218         break;
2219     case AV_PIX_FMT_UYVY422:
2220         *yuv2packed1 = yuv2uyvy422_1_c;
2221         *yuv2packed2 = yuv2uyvy422_2_c;
2222         *yuv2packedX = yuv2uyvy422_X_c;
2223         break;
2224     }
2225 }