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