]> git.sesse.net Git - ffmpeg/blob - libswscale/output.c
swscale: make yuv2interleavedX more asm-friendly
[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 static void yuv2p016cX_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither, const int16_t *chrFilter, int chrFilterSize,
184                          const int16_t **chrUSrc, const int16_t **chrVSrc,
185                          uint8_t *dest8, int chrDstW)
186 {
187     uint16_t *dest = (uint16_t*)dest8;
188     const int32_t **uSrc = (const int32_t **)chrUSrc;
189     const int32_t **vSrc = (const int32_t **)chrVSrc;
190     int shift = 15;
191     int big_endian = dstFormat == AV_PIX_FMT_P016BE;
192     int i, j;
193
194     for (i = 0; i < chrDstW; i++) {
195         int u = 1 << (shift - 1);
196         int v = 1 << (shift - 1);
197
198         /* See yuv2planeX_16_c_template for details. */
199         u -= 0x40000000;
200         v -= 0x40000000;
201         for (j = 0; j < chrFilterSize; j++) {
202             u += uSrc[j][i] * (unsigned)chrFilter[j];
203             v += vSrc[j][i] * (unsigned)chrFilter[j];
204         }
205
206         output_pixel(&dest[2*i]  , u, 0x8000, int);
207         output_pixel(&dest[2*i+1], v, 0x8000, int);
208     }
209 }
210
211 static av_always_inline void
212 yuv2plane1_float_c_template(const int32_t *src, float *dest, int dstW)
213 {
214     static const int big_endian = HAVE_BIGENDIAN;
215     static const int shift = 3;
216     static const float float_mult = 1.0f / 65535.0f;
217     int i, val;
218     uint16_t val_uint;
219
220     for (i = 0; i < dstW; ++i){
221         val = src[i] + (1 << (shift - 1));
222         output_pixel(&val_uint, val, 0, uint);
223         dest[i] = float_mult * (float)val_uint;
224     }
225 }
226
227 static av_always_inline void
228 yuv2plane1_float_bswap_c_template(const int32_t *src, uint32_t *dest, int dstW)
229 {
230     static const int big_endian = HAVE_BIGENDIAN;
231     static const int shift = 3;
232     static const float float_mult = 1.0f / 65535.0f;
233     int i, val;
234     uint16_t val_uint;
235
236     for (i = 0; i < dstW; ++i){
237         val = src[i] + (1 << (shift - 1));
238         output_pixel(&val_uint, val, 0, uint);
239         dest[i] = av_bswap32(av_float2int(float_mult * (float)val_uint));
240     }
241 }
242
243 static av_always_inline void
244 yuv2planeX_float_c_template(const int16_t *filter, int filterSize, const int32_t **src,
245                             float *dest, int dstW)
246 {
247     static const int big_endian = HAVE_BIGENDIAN;
248     static const int shift = 15;
249     static const float float_mult = 1.0f / 65535.0f;
250     int i, j, val;
251     uint16_t val_uint;
252
253     for (i = 0; i < dstW; ++i){
254         val = (1 << (shift - 1)) - 0x40000000;
255         for (j = 0; j < filterSize; ++j){
256             val += src[j][i] * (unsigned)filter[j];
257         }
258         output_pixel(&val_uint, val, 0x8000, int);
259         dest[i] = float_mult * (float)val_uint;
260     }
261 }
262
263 static av_always_inline void
264 yuv2planeX_float_bswap_c_template(const int16_t *filter, int filterSize, const int32_t **src,
265                             uint32_t *dest, int dstW)
266 {
267     static const int big_endian = HAVE_BIGENDIAN;
268     static const int shift = 15;
269     static const float float_mult = 1.0f / 65535.0f;
270     int i, j, val;
271     uint16_t val_uint;
272
273     for (i = 0; i < dstW; ++i){
274         val = (1 << (shift - 1)) - 0x40000000;
275         for (j = 0; j < filterSize; ++j){
276             val += src[j][i] * (unsigned)filter[j];
277         }
278         output_pixel(&val_uint, val, 0x8000, int);
279         dest[i] = av_bswap32(av_float2int(float_mult * (float)val_uint));
280     }
281 }
282
283 #define yuv2plane1_float(template, dest_type, BE_LE) \
284 static void yuv2plane1_float ## BE_LE ## _c(const int16_t *src, uint8_t *dest, int dstW, \
285                                             const uint8_t *dither, int offset) \
286 { \
287     template((const int32_t *)src, (dest_type *)dest, dstW); \
288 }
289
290 #define yuv2planeX_float(template, dest_type, BE_LE) \
291 static void yuv2planeX_float ## BE_LE ## _c(const int16_t *filter, int filterSize, \
292                                             const int16_t **src, uint8_t *dest, int dstW, \
293                                             const uint8_t *dither, int offset) \
294 { \
295     template(filter, filterSize, (const int32_t **)src, (dest_type *)dest, dstW); \
296 }
297
298 #if HAVE_BIGENDIAN
299 yuv2plane1_float(yuv2plane1_float_c_template,       float,    BE)
300 yuv2plane1_float(yuv2plane1_float_bswap_c_template, uint32_t, LE)
301 yuv2planeX_float(yuv2planeX_float_c_template,       float,    BE)
302 yuv2planeX_float(yuv2planeX_float_bswap_c_template, uint32_t, LE)
303 #else
304 yuv2plane1_float(yuv2plane1_float_c_template,       float,    LE)
305 yuv2plane1_float(yuv2plane1_float_bswap_c_template, uint32_t, BE)
306 yuv2planeX_float(yuv2planeX_float_c_template,       float,    LE)
307 yuv2planeX_float(yuv2planeX_float_bswap_c_template, uint32_t, BE)
308 #endif
309
310 #undef output_pixel
311
312 #define output_pixel(pos, val) \
313     if (big_endian) { \
314         AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
315     } else { \
316         AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
317     }
318
319 static av_always_inline void
320 yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
321                          int big_endian, int output_bits)
322 {
323     int i;
324     int shift = 15 - output_bits;
325
326     for (i = 0; i < dstW; i++) {
327         int val = src[i] + (1 << (shift - 1));
328         output_pixel(&dest[i], val);
329     }
330 }
331
332 static av_always_inline void
333 yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
334                          const int16_t **src, uint16_t *dest, int dstW,
335                          int big_endian, int output_bits)
336 {
337     int i;
338     int shift = 11 + 16 - output_bits;
339
340     for (i = 0; i < dstW; i++) {
341         int val = 1 << (shift - 1);
342         int j;
343
344         for (j = 0; j < filterSize; j++)
345             val += src[j][i] * filter[j];
346
347         output_pixel(&dest[i], val);
348     }
349 }
350
351 #undef output_pixel
352
353 #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
354 static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
355                               uint8_t *dest, int dstW, \
356                               const uint8_t *dither, int offset)\
357 { \
358     yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
359                          (uint16_t *) dest, dstW, is_be, bits); \
360 }\
361 static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
362                               const int16_t **src, uint8_t *dest, int dstW, \
363                               const uint8_t *dither, int offset)\
364 { \
365     yuv2planeX_## template_size ## _c_template(filter, \
366                          filterSize, (const typeX_t **) src, \
367                          (uint16_t *) dest, dstW, is_be, bits); \
368 }
369 yuv2NBPS( 9, BE, 1, 10, int16_t)
370 yuv2NBPS( 9, LE, 0, 10, int16_t)
371 yuv2NBPS(10, BE, 1, 10, int16_t)
372 yuv2NBPS(10, LE, 0, 10, int16_t)
373 yuv2NBPS(12, BE, 1, 10, int16_t)
374 yuv2NBPS(12, LE, 0, 10, int16_t)
375 yuv2NBPS(14, BE, 1, 10, int16_t)
376 yuv2NBPS(14, LE, 0, 10, int16_t)
377 yuv2NBPS(16, BE, 1, 16, int32_t)
378 yuv2NBPS(16, LE, 0, 16, int32_t)
379
380 static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
381                            const int16_t **src, uint8_t *dest, int dstW,
382                            const uint8_t *dither, int offset)
383 {
384     int i;
385     for (i=0; i<dstW; i++) {
386         int val = dither[(i + offset) & 7] << 12;
387         int j;
388         for (j=0; j<filterSize; j++)
389             val += src[j][i] * filter[j];
390
391         dest[i]= av_clip_uint8(val>>19);
392     }
393 }
394
395 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
396                            const uint8_t *dither, int offset)
397 {
398     int i;
399     for (i=0; i<dstW; i++) {
400         int val = (src[i] + dither[(i + offset) & 7]) >> 7;
401         dest[i]= av_clip_uint8(val);
402     }
403 }
404
405 static void yuv2nv12cX_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither, const int16_t *chrFilter, int chrFilterSize,
406                         const int16_t **chrUSrc, const int16_t **chrVSrc,
407                         uint8_t *dest, int chrDstW)
408 {
409     int i;
410
411     if (dstFormat == AV_PIX_FMT_NV12 ||
412         dstFormat == AV_PIX_FMT_NV24)
413         for (i=0; i<chrDstW; i++) {
414             int u = chrDither[i & 7] << 12;
415             int v = chrDither[(i + 3) & 7] << 12;
416             int j;
417             for (j=0; j<chrFilterSize; j++) {
418                 u += chrUSrc[j][i] * chrFilter[j];
419                 v += chrVSrc[j][i] * chrFilter[j];
420             }
421
422             dest[2*i]= av_clip_uint8(u>>19);
423             dest[2*i+1]= av_clip_uint8(v>>19);
424         }
425     else
426         for (i=0; i<chrDstW; i++) {
427             int u = chrDither[i & 7] << 12;
428             int v = chrDither[(i + 3) & 7] << 12;
429             int j;
430             for (j=0; j<chrFilterSize; j++) {
431                 u += chrUSrc[j][i] * chrFilter[j];
432                 v += chrVSrc[j][i] * chrFilter[j];
433             }
434
435             dest[2*i]= av_clip_uint8(v>>19);
436             dest[2*i+1]= av_clip_uint8(u>>19);
437         }
438 }
439
440
441 #define output_pixel(pos, val) \
442     if (big_endian) { \
443         AV_WB16(pos, av_clip_uintp2(val >> shift, 10) << 6); \
444     } else { \
445         AV_WL16(pos, av_clip_uintp2(val >> shift, 10) << 6); \
446     }
447
448 static void yuv2p010l1_c(const int16_t *src,
449                          uint16_t *dest, int dstW,
450                          int big_endian)
451 {
452     int i;
453     int shift = 5;
454
455     for (i = 0; i < dstW; i++) {
456         int val = src[i] + (1 << (shift - 1));
457         output_pixel(&dest[i], val);
458     }
459 }
460
461 static void yuv2p010lX_c(const int16_t *filter, int filterSize,
462                          const int16_t **src, uint16_t *dest, int dstW,
463                          int big_endian)
464 {
465     int i, j;
466     int shift = 17;
467
468     for (i = 0; i < dstW; i++) {
469         int val = 1 << (shift - 1);
470
471         for (j = 0; j < filterSize; j++)
472             val += src[j][i] * filter[j];
473
474         output_pixel(&dest[i], val);
475     }
476 }
477
478 static void yuv2p010cX_c(enum AVPixelFormat dstFormat, const uint8_t *chrDither, const int16_t *chrFilter, int chrFilterSize,
479                          const int16_t **chrUSrc, const int16_t **chrVSrc,
480                          uint8_t *dest8, int chrDstW)
481 {
482     uint16_t *dest = (uint16_t*)dest8;
483     int shift = 17;
484     int big_endian = dstFormat == AV_PIX_FMT_P010BE;
485     int i, j;
486
487     for (i = 0; i < chrDstW; i++) {
488         int u = 1 << (shift - 1);
489         int v = 1 << (shift - 1);
490
491         for (j = 0; j < chrFilterSize; j++) {
492             u += chrUSrc[j][i] * chrFilter[j];
493             v += chrVSrc[j][i] * chrFilter[j];
494         }
495
496         output_pixel(&dest[2*i]  , u);
497         output_pixel(&dest[2*i+1], v);
498     }
499 }
500
501 static void yuv2p010l1_LE_c(const int16_t *src,
502                             uint8_t *dest, int dstW,
503                             const uint8_t *dither, int offset)
504 {
505     yuv2p010l1_c(src, (uint16_t*)dest, dstW, 0);
506 }
507
508 static void yuv2p010l1_BE_c(const int16_t *src,
509                             uint8_t *dest, int dstW,
510                             const uint8_t *dither, int offset)
511 {
512     yuv2p010l1_c(src, (uint16_t*)dest, dstW, 1);
513 }
514
515 static void yuv2p010lX_LE_c(const int16_t *filter, int filterSize,
516                             const int16_t **src, uint8_t *dest, int dstW,
517                             const uint8_t *dither, int offset)
518 {
519     yuv2p010lX_c(filter, filterSize, src, (uint16_t*)dest, dstW, 0);
520 }
521
522 static void yuv2p010lX_BE_c(const int16_t *filter, int filterSize,
523                             const int16_t **src, uint8_t *dest, int dstW,
524                             const uint8_t *dither, int offset)
525 {
526     yuv2p010lX_c(filter, filterSize, src, (uint16_t*)dest, dstW, 1);
527 }
528
529 #undef output_pixel
530
531
532 #define accumulate_bit(acc, val) \
533     acc <<= 1; \
534     acc |= (val) >= 234
535 #define output_pixel(pos, acc) \
536     if (target == AV_PIX_FMT_MONOBLACK) { \
537         pos = acc; \
538     } else { \
539         pos = ~acc; \
540     }
541
542 static av_always_inline void
543 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
544                       const int16_t **lumSrc, int lumFilterSize,
545                       const int16_t *chrFilter, const int16_t **chrUSrc,
546                       const int16_t **chrVSrc, int chrFilterSize,
547                       const int16_t **alpSrc, uint8_t *dest, int dstW,
548                       int y, enum AVPixelFormat target)
549 {
550     const uint8_t * const d128 = ff_dither_8x8_220[y&7];
551     int i;
552     unsigned acc = 0;
553     int err = 0;
554
555     for (i = 0; i < dstW; i += 2) {
556         int j;
557         int Y1 = 1 << 18;
558         int Y2 = 1 << 18;
559
560         for (j = 0; j < lumFilterSize; j++) {
561             Y1 += lumSrc[j][i]   * lumFilter[j];
562             Y2 += lumSrc[j][i+1] * lumFilter[j];
563         }
564         Y1 >>= 19;
565         Y2 >>= 19;
566         if ((Y1 | Y2) & 0x100) {
567             Y1 = av_clip_uint8(Y1);
568             Y2 = av_clip_uint8(Y2);
569         }
570         if (c->dither == SWS_DITHER_ED) {
571             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;
572             c->dither_error[0][i] = err;
573             acc = 2*acc + (Y1 >= 128);
574             Y1 -= 220*(acc&1);
575
576             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);
577             c->dither_error[0][i+1] = Y1;
578             acc = 2*acc + (err >= 128);
579             err -= 220*(acc&1);
580         } else {
581             accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
582             accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
583         }
584         if ((i & 7) == 6) {
585             output_pixel(*dest++, acc);
586         }
587     }
588     c->dither_error[0][i] = err;
589
590     if (i & 6) {
591         output_pixel(*dest, acc);
592     }
593 }
594
595 static av_always_inline void
596 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
597                       const int16_t *ubuf[2], const int16_t *vbuf[2],
598                       const int16_t *abuf[2], uint8_t *dest, int dstW,
599                       int yalpha, int uvalpha, int y,
600                       enum AVPixelFormat target)
601 {
602     const int16_t *buf0  = buf[0],  *buf1  = buf[1];
603     const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
604     int  yalpha1 = 4096 - yalpha;
605     int i;
606     av_assert2(yalpha  <= 4096U);
607
608     if (c->dither == SWS_DITHER_ED) {
609         int err = 0;
610         int acc = 0;
611         for (i = 0; i < dstW; i +=2) {
612             int Y;
613
614             Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
615             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;
616             c->dither_error[0][i] = err;
617             acc = 2*acc + (Y >= 128);
618             Y -= 220*(acc&1);
619
620             err = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
621             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;
622             c->dither_error[0][i+1] = Y;
623             acc = 2*acc + (err >= 128);
624             err -= 220*(acc&1);
625
626             if ((i & 7) == 6)
627                 output_pixel(*dest++, acc);
628         }
629         c->dither_error[0][i] = err;
630     } else {
631         for (i = 0; i < dstW; i += 8) {
632             int Y, acc = 0;
633
634             Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
635             accumulate_bit(acc, Y + d128[0]);
636             Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
637             accumulate_bit(acc, Y + d128[1]);
638             Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
639             accumulate_bit(acc, Y + d128[2]);
640             Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
641             accumulate_bit(acc, Y + d128[3]);
642             Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
643             accumulate_bit(acc, Y + d128[4]);
644             Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
645             accumulate_bit(acc, Y + d128[5]);
646             Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
647             accumulate_bit(acc, Y + d128[6]);
648             Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
649             accumulate_bit(acc, Y + d128[7]);
650
651             output_pixel(*dest++, acc);
652         }
653     }
654 }
655
656 static av_always_inline void
657 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
658                       const int16_t *ubuf[2], const int16_t *vbuf[2],
659                       const int16_t *abuf0, uint8_t *dest, int dstW,
660                       int uvalpha, int y, enum AVPixelFormat target)
661 {
662     const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
663     int i;
664
665     if (c->dither == SWS_DITHER_ED) {
666         int err = 0;
667         int acc = 0;
668         for (i = 0; i < dstW; i +=2) {
669             int Y;
670
671             Y = ((buf0[i + 0] + 64) >> 7);
672             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;
673             c->dither_error[0][i] = err;
674             acc = 2*acc + (Y >= 128);
675             Y -= 220*(acc&1);
676
677             err = ((buf0[i + 1] + 64) >> 7);
678             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;
679             c->dither_error[0][i+1] = Y;
680             acc = 2*acc + (err >= 128);
681             err -= 220*(acc&1);
682
683             if ((i & 7) == 6)
684                 output_pixel(*dest++, acc);
685         }
686         c->dither_error[0][i] = err;
687     } else {
688         for (i = 0; i < dstW; i += 8) {
689             int acc = 0;
690             accumulate_bit(acc, ((buf0[i + 0] + 64) >> 7) + d128[0]);
691             accumulate_bit(acc, ((buf0[i + 1] + 64) >> 7) + d128[1]);
692             accumulate_bit(acc, ((buf0[i + 2] + 64) >> 7) + d128[2]);
693             accumulate_bit(acc, ((buf0[i + 3] + 64) >> 7) + d128[3]);
694             accumulate_bit(acc, ((buf0[i + 4] + 64) >> 7) + d128[4]);
695             accumulate_bit(acc, ((buf0[i + 5] + 64) >> 7) + d128[5]);
696             accumulate_bit(acc, ((buf0[i + 6] + 64) >> 7) + d128[6]);
697             accumulate_bit(acc, ((buf0[i + 7] + 64) >> 7) + d128[7]);
698
699             output_pixel(*dest++, acc);
700         }
701     }
702 }
703
704 #undef output_pixel
705 #undef accumulate_bit
706
707 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
708 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
709                                 const int16_t **lumSrc, int lumFilterSize, \
710                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
711                                 const int16_t **chrVSrc, int chrFilterSize, \
712                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
713                                 int y) \
714 { \
715     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
716                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
717                                   alpSrc, dest, dstW, y, fmt); \
718 } \
719  \
720 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
721                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
722                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
723                                 int yalpha, int uvalpha, int y) \
724 { \
725     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
726                                   dest, dstW, yalpha, uvalpha, y, fmt); \
727 } \
728  \
729 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
730                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
731                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
732                                 int uvalpha, int y) \
733 { \
734     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
735                                   abuf0, dest, dstW, uvalpha, \
736                                   y, fmt); \
737 }
738
739 YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
740 YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
741
742 #define output_pixels(pos, Y1, U, Y2, V) \
743     if (target == AV_PIX_FMT_YUYV422) { \
744         dest[pos + 0] = Y1; \
745         dest[pos + 1] = U;  \
746         dest[pos + 2] = Y2; \
747         dest[pos + 3] = V;  \
748     } else if (target == AV_PIX_FMT_YVYU422) { \
749         dest[pos + 0] = Y1; \
750         dest[pos + 1] = V;  \
751         dest[pos + 2] = Y2; \
752         dest[pos + 3] = U;  \
753     } else { /* AV_PIX_FMT_UYVY422 */ \
754         dest[pos + 0] = U;  \
755         dest[pos + 1] = Y1; \
756         dest[pos + 2] = V;  \
757         dest[pos + 3] = Y2; \
758     }
759
760 static av_always_inline void
761 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
762                      const int16_t **lumSrc, int lumFilterSize,
763                      const int16_t *chrFilter, const int16_t **chrUSrc,
764                      const int16_t **chrVSrc, int chrFilterSize,
765                      const int16_t **alpSrc, uint8_t *dest, int dstW,
766                      int y, enum AVPixelFormat target)
767 {
768     int i;
769
770     for (i = 0; i < ((dstW + 1) >> 1); i++) {
771         int j;
772         int Y1 = 1 << 18;
773         int Y2 = 1 << 18;
774         int U  = 1 << 18;
775         int V  = 1 << 18;
776
777         for (j = 0; j < lumFilterSize; j++) {
778             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
779             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
780         }
781         for (j = 0; j < chrFilterSize; j++) {
782             U += chrUSrc[j][i] * chrFilter[j];
783             V += chrVSrc[j][i] * chrFilter[j];
784         }
785         Y1 >>= 19;
786         Y2 >>= 19;
787         U  >>= 19;
788         V  >>= 19;
789         if ((Y1 | Y2 | U | V) & 0x100) {
790             Y1 = av_clip_uint8(Y1);
791             Y2 = av_clip_uint8(Y2);
792             U  = av_clip_uint8(U);
793             V  = av_clip_uint8(V);
794         }
795         output_pixels(4*i, Y1, U, Y2, V);
796     }
797 }
798
799 static av_always_inline void
800 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
801                      const int16_t *ubuf[2], const int16_t *vbuf[2],
802                      const int16_t *abuf[2], uint8_t *dest, int dstW,
803                      int yalpha, int uvalpha, int y,
804                      enum AVPixelFormat target)
805 {
806     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
807                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
808                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
809     int  yalpha1 = 4096 - yalpha;
810     int uvalpha1 = 4096 - uvalpha;
811     int i;
812     av_assert2(yalpha  <= 4096U);
813     av_assert2(uvalpha <= 4096U);
814
815     for (i = 0; i < ((dstW + 1) >> 1); i++) {
816         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
817         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
818         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
819         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
820
821         if ((Y1 | Y2 | U | V) & 0x100) {
822             Y1 = av_clip_uint8(Y1);
823             Y2 = av_clip_uint8(Y2);
824             U  = av_clip_uint8(U);
825             V  = av_clip_uint8(V);
826         }
827
828         output_pixels(i * 4, Y1, U, Y2, V);
829     }
830 }
831
832 static av_always_inline void
833 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
834                      const int16_t *ubuf[2], const int16_t *vbuf[2],
835                      const int16_t *abuf0, uint8_t *dest, int dstW,
836                      int uvalpha, int y, enum AVPixelFormat target)
837 {
838     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
839     int i;
840
841     if (uvalpha < 2048) {
842         for (i = 0; i < ((dstW + 1) >> 1); i++) {
843             int Y1 = (buf0[i * 2    ]+64) >> 7;
844             int Y2 = (buf0[i * 2 + 1]+64) >> 7;
845             int U  = (ubuf0[i]       +64) >> 7;
846             int V  = (vbuf0[i]       +64) >> 7;
847
848             if ((Y1 | Y2 | U | V) & 0x100) {
849                 Y1 = av_clip_uint8(Y1);
850                 Y2 = av_clip_uint8(Y2);
851                 U  = av_clip_uint8(U);
852                 V  = av_clip_uint8(V);
853             }
854
855             output_pixels(i * 4, Y1, U, Y2, V);
856         }
857     } else {
858         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
859         for (i = 0; i < ((dstW + 1) >> 1); i++) {
860             int Y1 = (buf0[i * 2    ]    + 64) >> 7;
861             int Y2 = (buf0[i * 2 + 1]    + 64) >> 7;
862             int U  = (ubuf0[i] + ubuf1[i]+128) >> 8;
863             int V  = (vbuf0[i] + vbuf1[i]+128) >> 8;
864
865             if ((Y1 | Y2 | U | V) & 0x100) {
866                 Y1 = av_clip_uint8(Y1);
867                 Y2 = av_clip_uint8(Y2);
868                 U  = av_clip_uint8(U);
869                 V  = av_clip_uint8(V);
870             }
871
872             output_pixels(i * 4, Y1, U, Y2, V);
873         }
874     }
875 }
876
877 #undef output_pixels
878
879 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
880 YUV2PACKEDWRAPPER(yuv2, 422, yvyu422, AV_PIX_FMT_YVYU422)
881 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
882
883 #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)
884 #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)
885 #define output_pixel(pos, val) \
886     if (isBE(target)) { \
887         AV_WB16(pos, val); \
888     } else { \
889         AV_WL16(pos, val); \
890     }
891
892 static av_always_inline void
893 yuv2ya16_X_c_template(SwsContext *c, const int16_t *lumFilter,
894                         const int32_t **lumSrc, int lumFilterSize,
895                         const int16_t *chrFilter, const int32_t **unused_chrUSrc,
896                         const int32_t **unused_chrVSrc, int unused_chrFilterSize,
897                         const int32_t **alpSrc, uint16_t *dest, int dstW,
898                         int y, enum AVPixelFormat target, int unused_hasAlpha, int unused_eightbytes)
899 {
900     int hasAlpha = !!alpSrc;
901     int i;
902
903     for (i = 0; i < dstW; i++) {
904         int j;
905         int Y = -0x40000000;
906         int A = 0xffff;
907
908         for (j = 0; j < lumFilterSize; j++)
909             Y += lumSrc[j][i] * lumFilter[j];
910
911         Y >>= 15;
912         Y += (1<<3) + 0x8000;
913         Y = av_clip_uint16(Y);
914
915         if (hasAlpha) {
916             A = -0x40000000 + (1<<14);
917             for (j = 0; j < lumFilterSize; j++)
918                 A += alpSrc[j][i] * lumFilter[j];
919
920             A >>= 15;
921             A += 0x8000;
922             A = av_clip_uint16(A);
923         }
924
925         output_pixel(&dest[2 * i    ], Y);
926         output_pixel(&dest[2 * i + 1], A);
927     }
928 }
929
930 static av_always_inline void
931 yuv2ya16_2_c_template(SwsContext *c, const int32_t *buf[2],
932                         const int32_t *unused_ubuf[2], const int32_t *unused_vbuf[2],
933                         const int32_t *abuf[2], uint16_t *dest, int dstW,
934                         int yalpha, int unused_uvalpha, int y,
935                         enum AVPixelFormat target, int unused_hasAlpha, int unused_eightbytes)
936 {
937     int hasAlpha = abuf && abuf[0] && abuf[1];
938     const int32_t *buf0  = buf[0],  *buf1  = buf[1],
939     *abuf0 = hasAlpha ? abuf[0] : NULL,
940     *abuf1 = hasAlpha ? abuf[1] : NULL;
941     int  yalpha1 = 4096 - yalpha;
942     int i;
943
944     av_assert2(yalpha  <= 4096U);
945
946     for (i = 0; i < dstW; i++) {
947         int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 15;
948         int A;
949
950         Y = av_clip_uint16(Y);
951
952         if (hasAlpha) {
953             A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 15;
954             A = av_clip_uint16(A);
955         }
956
957         output_pixel(&dest[2 * i    ], Y);
958         output_pixel(&dest[2 * i + 1], hasAlpha ? A : 65535);
959     }
960 }
961
962 static av_always_inline void
963 yuv2ya16_1_c_template(SwsContext *c, const int32_t *buf0,
964                         const int32_t *unused_ubuf[2], const int32_t *unused_vbuf[2],
965                         const int32_t *abuf0, uint16_t *dest, int dstW,
966                         int unused_uvalpha, int y, enum AVPixelFormat target, int unused_hasAlpha, int unused_eightbytes)
967 {
968     int hasAlpha = !!abuf0;
969     int i;
970
971     for (i = 0; i < dstW; i++) {
972         int Y = buf0[i] >> 3;/* 19 - 16 */
973         int A;
974
975         Y = av_clip_uint16(Y);
976
977         if (hasAlpha) {
978             A = abuf0[i] >> 3;
979             if (A & 0x100)
980                 A = av_clip_uint16(A);
981         }
982
983         output_pixel(&dest[2 * i    ], Y);
984         output_pixel(&dest[2 * i + 1], hasAlpha ? A : 65535);
985     }
986 }
987
988 static av_always_inline void
989 yuv2rgba64_X_c_template(SwsContext *c, const int16_t *lumFilter,
990                        const int32_t **lumSrc, int lumFilterSize,
991                        const int16_t *chrFilter, const int32_t **chrUSrc,
992                        const int32_t **chrVSrc, int chrFilterSize,
993                        const int32_t **alpSrc, uint16_t *dest, int dstW,
994                        int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
995 {
996     int i;
997     int A1 = 0xffff<<14, A2 = 0xffff<<14;
998
999     for (i = 0; i < ((dstW + 1) >> 1); i++) {
1000         int j;
1001         int Y1 = -0x40000000;
1002         int Y2 = -0x40000000;
1003         int U  = -(128 << 23); // 19
1004         int V  = -(128 << 23);
1005         int R, G, B;
1006
1007         for (j = 0; j < lumFilterSize; j++) {
1008             Y1 += lumSrc[j][i * 2]     * (unsigned)lumFilter[j];
1009             Y2 += lumSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
1010         }
1011         for (j = 0; j < chrFilterSize; j++) {;
1012             U += chrUSrc[j][i] * (unsigned)chrFilter[j];
1013             V += chrVSrc[j][i] * (unsigned)chrFilter[j];
1014         }
1015
1016         if (hasAlpha) {
1017             A1 = -0x40000000;
1018             A2 = -0x40000000;
1019             for (j = 0; j < lumFilterSize; j++) {
1020                 A1 += alpSrc[j][i * 2]     * (unsigned)lumFilter[j];
1021                 A2 += alpSrc[j][i * 2 + 1] * (unsigned)lumFilter[j];
1022             }
1023             A1 >>= 1;
1024             A1 += 0x20002000;
1025             A2 >>= 1;
1026             A2 += 0x20002000;
1027         }
1028
1029         // 8 bits: 12+15=27; 16 bits: 12+19=31
1030         Y1 >>= 14; // 10
1031         Y1 += 0x10000;
1032         Y2 >>= 14;
1033         Y2 += 0x10000;
1034         U  >>= 14;
1035         V  >>= 14;
1036
1037         // 8 bits: 27 -> 17 bits, 16 bits: 31 - 14 = 17 bits
1038         Y1 -= c->yuv2rgb_y_offset;
1039         Y2 -= c->yuv2rgb_y_offset;
1040         Y1 *= c->yuv2rgb_y_coeff;
1041         Y2 *= c->yuv2rgb_y_coeff;
1042         Y1 += 1 << 13; // 21
1043         Y2 += 1 << 13;
1044         // 8 bits: 17 + 13 bits = 30 bits, 16 bits: 17 + 13 bits = 30 bits
1045
1046         R = V * c->yuv2rgb_v2r_coeff;
1047         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1048         B =                            U * c->yuv2rgb_u2b_coeff;
1049
1050         // 8 bits: 30 - 22 = 8 bits, 16 bits: 30 bits - 14 = 16 bits
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         if (eightbytes) {
1055             output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
1056             output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
1057             output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
1058             output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
1059             output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
1060             dest += 8;
1061         } else {
1062             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
1063             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
1064             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
1065             dest += 6;
1066         }
1067     }
1068 }
1069
1070 static av_always_inline void
1071 yuv2rgba64_2_c_template(SwsContext *c, const int32_t *buf[2],
1072                        const int32_t *ubuf[2], const int32_t *vbuf[2],
1073                        const int32_t *abuf[2], uint16_t *dest, int dstW,
1074                        int yalpha, int uvalpha, int y,
1075                        enum AVPixelFormat target, int hasAlpha, int eightbytes)
1076 {
1077     const int32_t *buf0  = buf[0],  *buf1  = buf[1],
1078                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1079                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1080                   *abuf0 = hasAlpha ? abuf[0] : NULL,
1081                   *abuf1 = hasAlpha ? abuf[1] : NULL;
1082     int  yalpha1 = 4096 - yalpha;
1083     int uvalpha1 = 4096 - uvalpha;
1084     int i;
1085     int A1 = 0xffff<<14, A2 = 0xffff<<14;
1086
1087     av_assert2(yalpha  <= 4096U);
1088     av_assert2(uvalpha <= 4096U);
1089
1090     for (i = 0; i < ((dstW + 1) >> 1); i++) {
1091         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha) >> 14;
1092         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha) >> 14;
1093         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha - (128 << 23)) >> 14;
1094         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha - (128 << 23)) >> 14;
1095         int R, G, B;
1096
1097         Y1 -= c->yuv2rgb_y_offset;
1098         Y2 -= c->yuv2rgb_y_offset;
1099         Y1 *= c->yuv2rgb_y_coeff;
1100         Y2 *= c->yuv2rgb_y_coeff;
1101         Y1 += 1 << 13;
1102         Y2 += 1 << 13;
1103
1104         R = V * c->yuv2rgb_v2r_coeff;
1105         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1106         B =                            U * c->yuv2rgb_u2b_coeff;
1107
1108         if (hasAlpha) {
1109             A1 = (abuf0[i * 2    ] * yalpha1 + abuf1[i * 2    ] * yalpha) >> 1;
1110             A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 1;
1111
1112             A1 += 1 << 13;
1113             A2 += 1 << 13;
1114         }
1115
1116         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
1117         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
1118         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
1119         if (eightbytes) {
1120             output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
1121             output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
1122             output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
1123             output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
1124             output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
1125             dest += 8;
1126         } else {
1127             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
1128             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
1129             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
1130             dest += 6;
1131         }
1132     }
1133 }
1134
1135 static av_always_inline void
1136 yuv2rgba64_1_c_template(SwsContext *c, const int32_t *buf0,
1137                        const int32_t *ubuf[2], const int32_t *vbuf[2],
1138                        const int32_t *abuf0, uint16_t *dest, int dstW,
1139                        int uvalpha, int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
1140 {
1141     const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1142     int i;
1143     int A1 = 0xffff<<14, A2= 0xffff<<14;
1144
1145     if (uvalpha < 2048) {
1146         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1147             int Y1 = (buf0[i * 2]    ) >> 2;
1148             int Y2 = (buf0[i * 2 + 1]) >> 2;
1149             int U  = (ubuf0[i] - (128 << 11)) >> 2;
1150             int V  = (vbuf0[i] - (128 << 11)) >> 2;
1151             int R, G, B;
1152
1153             Y1 -= c->yuv2rgb_y_offset;
1154             Y2 -= c->yuv2rgb_y_offset;
1155             Y1 *= c->yuv2rgb_y_coeff;
1156             Y2 *= c->yuv2rgb_y_coeff;
1157             Y1 += 1 << 13;
1158             Y2 += 1 << 13;
1159
1160             if (hasAlpha) {
1161                 A1 = abuf0[i * 2    ] << 11;
1162                 A2 = abuf0[i * 2 + 1] << 11;
1163
1164                 A1 += 1 << 13;
1165                 A2 += 1 << 13;
1166             }
1167
1168             R = V * c->yuv2rgb_v2r_coeff;
1169             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1170             B =                            U * c->yuv2rgb_u2b_coeff;
1171
1172             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
1173             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
1174             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
1175             if (eightbytes) {
1176                 output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
1177                 output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
1178                 output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
1179                 output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
1180                 output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
1181                 dest += 8;
1182             } else {
1183                 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
1184                 output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
1185                 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
1186                 dest += 6;
1187             }
1188         }
1189     } else {
1190         const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1191         int A1 = 0xffff<<14, A2 = 0xffff<<14;
1192         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1193             int Y1 = (buf0[i * 2]    ) >> 2;
1194             int Y2 = (buf0[i * 2 + 1]) >> 2;
1195             int U  = (ubuf0[i] + ubuf1[i] - (128 << 12)) >> 3;
1196             int V  = (vbuf0[i] + vbuf1[i] - (128 << 12)) >> 3;
1197             int R, G, B;
1198
1199             Y1 -= c->yuv2rgb_y_offset;
1200             Y2 -= c->yuv2rgb_y_offset;
1201             Y1 *= c->yuv2rgb_y_coeff;
1202             Y2 *= c->yuv2rgb_y_coeff;
1203             Y1 += 1 << 13;
1204             Y2 += 1 << 13;
1205
1206             if (hasAlpha) {
1207                 A1 = abuf0[i * 2    ] << 11;
1208                 A2 = abuf0[i * 2 + 1] << 11;
1209
1210                 A1 += 1 << 13;
1211                 A2 += 1 << 13;
1212             }
1213
1214             R = V * c->yuv2rgb_v2r_coeff;
1215             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1216             B =                            U * c->yuv2rgb_u2b_coeff;
1217
1218             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
1219             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
1220             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
1221             if (eightbytes) {
1222                 output_pixel(&dest[3], av_clip_uintp2(A1      , 30) >> 14);
1223                 output_pixel(&dest[4], av_clip_uintp2(R_B + Y2, 30) >> 14);
1224                 output_pixel(&dest[5], av_clip_uintp2(  G + Y2, 30) >> 14);
1225                 output_pixel(&dest[6], av_clip_uintp2(B_R + Y2, 30) >> 14);
1226                 output_pixel(&dest[7], av_clip_uintp2(A2      , 30) >> 14);
1227                 dest += 8;
1228             } else {
1229                 output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
1230                 output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
1231                 output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
1232                 dest += 6;
1233             }
1234         }
1235     }
1236 }
1237
1238 static av_always_inline void
1239 yuv2rgba64_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1240                        const int32_t **lumSrc, int lumFilterSize,
1241                        const int16_t *chrFilter, const int32_t **chrUSrc,
1242                        const int32_t **chrVSrc, int chrFilterSize,
1243                        const int32_t **alpSrc, uint16_t *dest, int dstW,
1244                        int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
1245 {
1246     int i;
1247     int A = 0xffff<<14;
1248
1249     for (i = 0; i < dstW; i++) {
1250         int j;
1251         int Y  = -0x40000000;
1252         int U  = -(128 << 23); // 19
1253         int V  = -(128 << 23);
1254         int R, G, B;
1255
1256         for (j = 0; j < lumFilterSize; j++) {
1257             Y += lumSrc[j][i]  * (unsigned)lumFilter[j];
1258         }
1259         for (j = 0; j < chrFilterSize; j++) {;
1260             U += chrUSrc[j][i] * (unsigned)chrFilter[j];
1261             V += chrVSrc[j][i] * (unsigned)chrFilter[j];
1262         }
1263
1264         if (hasAlpha) {
1265             A = -0x40000000;
1266             for (j = 0; j < lumFilterSize; j++) {
1267                 A += alpSrc[j][i] * (unsigned)lumFilter[j];
1268             }
1269             A >>= 1;
1270             A += 0x20002000;
1271         }
1272
1273         // 8bit: 12+15=27; 16-bit: 12+19=31
1274         Y  >>= 14; // 10
1275         Y += 0x10000;
1276         U  >>= 14;
1277         V  >>= 14;
1278
1279         // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
1280         Y -= c->yuv2rgb_y_offset;
1281         Y *= c->yuv2rgb_y_coeff;
1282         Y += 1 << 13; // 21
1283         // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
1284
1285         R = V * c->yuv2rgb_v2r_coeff;
1286         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1287         B =                            U * c->yuv2rgb_u2b_coeff;
1288
1289         // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
1290         output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1291         output_pixel(&dest[1], av_clip_uintp2(  G + Y, 30) >> 14);
1292         output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1293         if (eightbytes) {
1294             output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1295             dest += 4;
1296         } else {
1297             dest += 3;
1298         }
1299     }
1300 }
1301
1302 static av_always_inline void
1303 yuv2rgba64_full_2_c_template(SwsContext *c, const int32_t *buf[2],
1304                        const int32_t *ubuf[2], const int32_t *vbuf[2],
1305                        const int32_t *abuf[2], uint16_t *dest, int dstW,
1306                        int yalpha, int uvalpha, int y,
1307                        enum AVPixelFormat target, int hasAlpha, int eightbytes)
1308 {
1309     const int32_t *buf0  = buf[0],  *buf1  = buf[1],
1310                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1311                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1312                   *abuf0 = hasAlpha ? abuf[0] : NULL,
1313                   *abuf1 = hasAlpha ? abuf[1] : NULL;
1314     int  yalpha1 = 4096 - yalpha;
1315     int uvalpha1 = 4096 - uvalpha;
1316     int i;
1317     int A = 0xffff<<14;
1318
1319     av_assert2(yalpha  <= 4096U);
1320     av_assert2(uvalpha <= 4096U);
1321
1322     for (i = 0; i < dstW; i++) {
1323         int Y  = (buf0[i]     * yalpha1  + buf1[i]     * yalpha) >> 14;
1324         int U  = (ubuf0[i]   * uvalpha1 + ubuf1[i]     * uvalpha - (128 << 23)) >> 14;
1325         int V  = (vbuf0[i]   * uvalpha1 + vbuf1[i]     * uvalpha - (128 << 23)) >> 14;
1326         int R, G, B;
1327
1328         Y -= c->yuv2rgb_y_offset;
1329         Y *= c->yuv2rgb_y_coeff;
1330         Y += 1 << 13;
1331
1332         R = V * c->yuv2rgb_v2r_coeff;
1333         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1334         B =                            U * c->yuv2rgb_u2b_coeff;
1335
1336         if (hasAlpha) {
1337             A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 1;
1338
1339             A += 1 << 13;
1340         }
1341
1342         output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1343         output_pixel(&dest[1], av_clip_uintp2(  G + Y, 30) >> 14);
1344         output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1345         if (eightbytes) {
1346             output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1347             dest += 4;
1348         } else {
1349             dest += 3;
1350         }
1351     }
1352 }
1353
1354 static av_always_inline void
1355 yuv2rgba64_full_1_c_template(SwsContext *c, const int32_t *buf0,
1356                        const int32_t *ubuf[2], const int32_t *vbuf[2],
1357                        const int32_t *abuf0, uint16_t *dest, int dstW,
1358                        int uvalpha, int y, enum AVPixelFormat target, int hasAlpha, int eightbytes)
1359 {
1360     const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1361     int i;
1362     int A = 0xffff<<14;
1363
1364     if (uvalpha < 2048) {
1365         for (i = 0; i < dstW; i++) {
1366             int Y  = (buf0[i]) >> 2;
1367             int U  = (ubuf0[i] - (128 << 11)) >> 2;
1368             int V  = (vbuf0[i] - (128 << 11)) >> 2;
1369             int R, G, B;
1370
1371             Y -= c->yuv2rgb_y_offset;
1372             Y *= c->yuv2rgb_y_coeff;
1373             Y += 1 << 13;
1374
1375             if (hasAlpha) {
1376                 A = abuf0[i] << 11;
1377
1378                 A += 1 << 13;
1379             }
1380
1381             R = V * c->yuv2rgb_v2r_coeff;
1382             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1383             B =                            U * c->yuv2rgb_u2b_coeff;
1384
1385             output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1386             output_pixel(&dest[1], av_clip_uintp2(  G + Y, 30) >> 14);
1387             output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1388             if (eightbytes) {
1389                 output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1390                 dest += 4;
1391             } else {
1392                 dest += 3;
1393             }
1394         }
1395     } else {
1396         const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1397         int A = 0xffff<<14;
1398         for (i = 0; i < dstW; i++) {
1399             int Y  = (buf0[i]    ) >> 2;
1400             int U  = (ubuf0[i] + ubuf1[i] - (128 << 12)) >> 3;
1401             int V  = (vbuf0[i] + vbuf1[i] - (128 << 12)) >> 3;
1402             int R, G, B;
1403
1404             Y -= c->yuv2rgb_y_offset;
1405             Y *= c->yuv2rgb_y_coeff;
1406             Y += 1 << 13;
1407
1408             if (hasAlpha) {
1409                 A = abuf0[i] << 11;
1410
1411                 A += 1 << 13;
1412             }
1413
1414             R = V * c->yuv2rgb_v2r_coeff;
1415             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1416             B =                            U * c->yuv2rgb_u2b_coeff;
1417
1418             output_pixel(&dest[0], av_clip_uintp2(R_B + Y, 30) >> 14);
1419             output_pixel(&dest[1], av_clip_uintp2(  G + Y, 30) >> 14);
1420             output_pixel(&dest[2], av_clip_uintp2(B_R + Y, 30) >> 14);
1421             if (eightbytes) {
1422                 output_pixel(&dest[3], av_clip_uintp2(A, 30) >> 14);
1423                 dest += 4;
1424             } else {
1425                 dest += 3;
1426             }
1427         }
1428     }
1429 }
1430
1431 #undef output_pixel
1432 #undef r_b
1433 #undef b_r
1434
1435 #define YUV2PACKED16WRAPPER(name, base, ext, fmt, hasAlpha, eightbytes) \
1436 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1437                         const int16_t **_lumSrc, int lumFilterSize, \
1438                         const int16_t *chrFilter, const int16_t **_chrUSrc, \
1439                         const int16_t **_chrVSrc, int chrFilterSize, \
1440                         const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
1441                         int y) \
1442 { \
1443     const int32_t **lumSrc  = (const int32_t **) _lumSrc, \
1444                   **chrUSrc = (const int32_t **) _chrUSrc, \
1445                   **chrVSrc = (const int32_t **) _chrVSrc, \
1446                   **alpSrc  = (const int32_t **) _alpSrc; \
1447     uint16_t *dest = (uint16_t *) _dest; \
1448     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1449                           chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1450                           alpSrc, dest, dstW, y, fmt, hasAlpha, eightbytes); \
1451 } \
1452  \
1453 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
1454                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
1455                         const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
1456                         int yalpha, int uvalpha, int y) \
1457 { \
1458     const int32_t **buf  = (const int32_t **) _buf, \
1459                   **ubuf = (const int32_t **) _ubuf, \
1460                   **vbuf = (const int32_t **) _vbuf, \
1461                   **abuf = (const int32_t **) _abuf; \
1462     uint16_t *dest = (uint16_t *) _dest; \
1463     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1464                           dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha, eightbytes); \
1465 } \
1466  \
1467 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
1468                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
1469                         const int16_t *_abuf0, uint8_t *_dest, int dstW, \
1470                         int uvalpha, int y) \
1471 { \
1472     const int32_t *buf0  = (const int32_t *)  _buf0, \
1473                  **ubuf  = (const int32_t **) _ubuf, \
1474                  **vbuf  = (const int32_t **) _vbuf, \
1475                   *abuf0 = (const int32_t *)  _abuf0; \
1476     uint16_t *dest = (uint16_t *) _dest; \
1477     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1478                                   dstW, uvalpha, y, fmt, hasAlpha, eightbytes); \
1479 }
1480
1481 YUV2PACKED16WRAPPER(yuv2, rgba64, rgb48be, AV_PIX_FMT_RGB48BE, 0, 0)
1482 YUV2PACKED16WRAPPER(yuv2, rgba64, rgb48le, AV_PIX_FMT_RGB48LE, 0, 0)
1483 YUV2PACKED16WRAPPER(yuv2, rgba64, bgr48be, AV_PIX_FMT_BGR48BE, 0, 0)
1484 YUV2PACKED16WRAPPER(yuv2, rgba64, bgr48le, AV_PIX_FMT_BGR48LE, 0, 0)
1485 YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64be, AV_PIX_FMT_RGBA64BE, 1, 1)
1486 YUV2PACKED16WRAPPER(yuv2, rgba64, rgba64le, AV_PIX_FMT_RGBA64LE, 1, 1)
1487 YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64be, AV_PIX_FMT_RGBA64BE, 0, 1)
1488 YUV2PACKED16WRAPPER(yuv2, rgba64, rgbx64le, AV_PIX_FMT_RGBA64LE, 0, 1)
1489 YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64be, AV_PIX_FMT_BGRA64BE, 1, 1)
1490 YUV2PACKED16WRAPPER(yuv2, rgba64, bgra64le, AV_PIX_FMT_BGRA64LE, 1, 1)
1491 YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64be, AV_PIX_FMT_BGRA64BE, 0, 1)
1492 YUV2PACKED16WRAPPER(yuv2, rgba64, bgrx64le, AV_PIX_FMT_BGRA64LE, 0, 1)
1493 YUV2PACKED16WRAPPER(yuv2, ya16, ya16be, AV_PIX_FMT_YA16BE, 1, 0)
1494 YUV2PACKED16WRAPPER(yuv2, ya16, ya16le, AV_PIX_FMT_YA16LE, 1, 0)
1495
1496 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgb48be_full, AV_PIX_FMT_RGB48BE, 0, 0)
1497 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgb48le_full, AV_PIX_FMT_RGB48LE, 0, 0)
1498 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgr48be_full, AV_PIX_FMT_BGR48BE, 0, 0)
1499 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgr48le_full, AV_PIX_FMT_BGR48LE, 0, 0)
1500 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgba64be_full, AV_PIX_FMT_RGBA64BE, 1, 1)
1501 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgba64le_full, AV_PIX_FMT_RGBA64LE, 1, 1)
1502 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbx64be_full, AV_PIX_FMT_RGBA64BE, 0, 1)
1503 YUV2PACKED16WRAPPER(yuv2, rgba64_full, rgbx64le_full, AV_PIX_FMT_RGBA64LE, 0, 1)
1504 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgra64be_full, AV_PIX_FMT_BGRA64BE, 1, 1)
1505 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgra64le_full, AV_PIX_FMT_BGRA64LE, 1, 1)
1506 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgrx64be_full, AV_PIX_FMT_BGRA64BE, 0, 1)
1507 YUV2PACKED16WRAPPER(yuv2, rgba64_full, bgrx64le_full, AV_PIX_FMT_BGRA64LE, 0, 1)
1508
1509 /*
1510  * Write out 2 RGB pixels in the target pixel format. This function takes a
1511  * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
1512  * things like endianness conversion and shifting. The caller takes care of
1513  * setting the correct offset in these tables from the chroma (U/V) values.
1514  * This function then uses the luminance (Y1/Y2) values to write out the
1515  * correct RGB values into the destination buffer.
1516  */
1517 static av_always_inline void
1518 yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
1519               unsigned A1, unsigned A2,
1520               const void *_r, const void *_g, const void *_b, int y,
1521               enum AVPixelFormat target, int hasAlpha)
1522 {
1523     if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
1524         target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
1525         uint32_t *dest = (uint32_t *) _dest;
1526         const uint32_t *r = (const uint32_t *) _r;
1527         const uint32_t *g = (const uint32_t *) _g;
1528         const uint32_t *b = (const uint32_t *) _b;
1529
1530 #if CONFIG_SMALL
1531         int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
1532
1533         dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
1534         dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
1535 #else
1536         if (hasAlpha) {
1537             int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
1538
1539             av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0);
1540             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
1541             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
1542         } else {
1543 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL > 1
1544             int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
1545
1546             av_assert2((((r[Y1] + g[Y1] + b[Y1]) >> sh) & 0xFF) == 0xFF);
1547 #endif
1548             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
1549             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
1550         }
1551 #endif
1552     } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
1553         uint8_t *dest = (uint8_t *) _dest;
1554         const uint8_t *r = (const uint8_t *) _r;
1555         const uint8_t *g = (const uint8_t *) _g;
1556         const uint8_t *b = (const uint8_t *) _b;
1557
1558 #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
1559 #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
1560
1561         dest[i * 6 + 0] = r_b[Y1];
1562         dest[i * 6 + 1] =   g[Y1];
1563         dest[i * 6 + 2] = b_r[Y1];
1564         dest[i * 6 + 3] = r_b[Y2];
1565         dest[i * 6 + 4] =   g[Y2];
1566         dest[i * 6 + 5] = b_r[Y2];
1567 #undef r_b
1568 #undef b_r
1569     } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
1570                target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
1571                target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
1572         uint16_t *dest = (uint16_t *) _dest;
1573         const uint16_t *r = (const uint16_t *) _r;
1574         const uint16_t *g = (const uint16_t *) _g;
1575         const uint16_t *b = (const uint16_t *) _b;
1576         int dr1, dg1, db1, dr2, dg2, db2;
1577
1578         if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
1579             dr1 = ff_dither_2x2_8[ y & 1     ][0];
1580             dg1 = ff_dither_2x2_4[ y & 1     ][0];
1581             db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
1582             dr2 = ff_dither_2x2_8[ y & 1     ][1];
1583             dg2 = ff_dither_2x2_4[ y & 1     ][1];
1584             db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
1585         } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
1586             dr1 = ff_dither_2x2_8[ y & 1     ][0];
1587             dg1 = ff_dither_2x2_8[ y & 1     ][1];
1588             db1 = ff_dither_2x2_8[(y & 1) ^ 1][0];
1589             dr2 = ff_dither_2x2_8[ y & 1     ][1];
1590             dg2 = ff_dither_2x2_8[ y & 1     ][0];
1591             db2 = ff_dither_2x2_8[(y & 1) ^ 1][1];
1592         } else {
1593             dr1 = ff_dither_4x4_16[ y & 3     ][0];
1594             dg1 = ff_dither_4x4_16[ y & 3     ][1];
1595             db1 = ff_dither_4x4_16[(y & 3) ^ 3][0];
1596             dr2 = ff_dither_4x4_16[ y & 3     ][1];
1597             dg2 = ff_dither_4x4_16[ y & 3     ][0];
1598             db2 = ff_dither_4x4_16[(y & 3) ^ 3][1];
1599         }
1600
1601         dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
1602         dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
1603     } else if (target == AV_PIX_FMT_X2RGB10) {
1604         uint32_t *dest = (uint32_t *) _dest;
1605         const uint32_t *r = (const uint32_t *) _r;
1606         const uint32_t *g = (const uint32_t *) _g;
1607         const uint32_t *b = (const uint32_t *) _b;
1608         dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
1609         dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
1610     } else /* 8/4 bits */ {
1611         uint8_t *dest = (uint8_t *) _dest;
1612         const uint8_t *r = (const uint8_t *) _r;
1613         const uint8_t *g = (const uint8_t *) _g;
1614         const uint8_t *b = (const uint8_t *) _b;
1615         int dr1, dg1, db1, dr2, dg2, db2;
1616
1617         if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
1618             const uint8_t * const d64 = ff_dither_8x8_73[y & 7];
1619             const uint8_t * const d32 = ff_dither_8x8_32[y & 7];
1620             dr1 = dg1 = d32[(i * 2 + 0) & 7];
1621             db1 =       d64[(i * 2 + 0) & 7];
1622             dr2 = dg2 = d32[(i * 2 + 1) & 7];
1623             db2 =       d64[(i * 2 + 1) & 7];
1624         } else {
1625             const uint8_t * const d64  = ff_dither_8x8_73 [y & 7];
1626             const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
1627             dr1 = db1 = d128[(i * 2 + 0) & 7];
1628             dg1 =        d64[(i * 2 + 0) & 7];
1629             dr2 = db2 = d128[(i * 2 + 1) & 7];
1630             dg2 =        d64[(i * 2 + 1) & 7];
1631         }
1632
1633         if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
1634             dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
1635                     ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
1636         } else {
1637             dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
1638             dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
1639         }
1640     }
1641 }
1642
1643 static av_always_inline void
1644 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
1645                      const int16_t **lumSrc, int lumFilterSize,
1646                      const int16_t *chrFilter, const int16_t **chrUSrc,
1647                      const int16_t **chrVSrc, int chrFilterSize,
1648                      const int16_t **alpSrc, uint8_t *dest, int dstW,
1649                      int y, enum AVPixelFormat target, int hasAlpha)
1650 {
1651     int i;
1652
1653     for (i = 0; i < ((dstW + 1) >> 1); i++) {
1654         int j, A1, A2;
1655         int Y1 = 1 << 18;
1656         int Y2 = 1 << 18;
1657         int U  = 1 << 18;
1658         int V  = 1 << 18;
1659         const void *r, *g, *b;
1660
1661         for (j = 0; j < lumFilterSize; j++) {
1662             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
1663             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
1664         }
1665         for (j = 0; j < chrFilterSize; j++) {
1666             U += chrUSrc[j][i] * chrFilter[j];
1667             V += chrVSrc[j][i] * chrFilter[j];
1668         }
1669         Y1 >>= 19;
1670         Y2 >>= 19;
1671         U  >>= 19;
1672         V  >>= 19;
1673         if (hasAlpha) {
1674             A1 = 1 << 18;
1675             A2 = 1 << 18;
1676             for (j = 0; j < lumFilterSize; j++) {
1677                 A1 += alpSrc[j][i * 2    ] * lumFilter[j];
1678                 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
1679             }
1680             A1 >>= 19;
1681             A2 >>= 19;
1682             if ((A1 | A2) & 0x100) {
1683                 A1 = av_clip_uint8(A1);
1684                 A2 = av_clip_uint8(A2);
1685             }
1686         }
1687
1688         r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM];
1689         g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]);
1690         b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1691
1692         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1693                       r, g, b, y, target, hasAlpha);
1694     }
1695 }
1696
1697 static av_always_inline void
1698 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
1699                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1700                      const int16_t *abuf[2], uint8_t *dest, int dstW,
1701                      int yalpha, int uvalpha, int y,
1702                      enum AVPixelFormat target, int hasAlpha)
1703 {
1704     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
1705                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1706                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1707                   *abuf0 = hasAlpha ? abuf[0] : NULL,
1708                   *abuf1 = hasAlpha ? abuf[1] : NULL;
1709     int  yalpha1 = 4096 - yalpha;
1710     int uvalpha1 = 4096 - uvalpha;
1711     int i;
1712     av_assert2(yalpha  <= 4096U);
1713     av_assert2(uvalpha <= 4096U);
1714
1715     for (i = 0; i < ((dstW + 1) >> 1); i++) {
1716         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
1717         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
1718         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
1719         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
1720         int A1, A2;
1721         const void *r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1722                    *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1723                    *b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1724
1725         if (hasAlpha) {
1726             A1 = (abuf0[i * 2    ] * yalpha1 + abuf1[i * 2    ] * yalpha) >> 19;
1727             A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1728             A1 = av_clip_uint8(A1);
1729             A2 = av_clip_uint8(A2);
1730         }
1731
1732         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1733                       r, g, b, y, target, hasAlpha);
1734     }
1735 }
1736
1737 static av_always_inline void
1738 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1739                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1740                      const int16_t *abuf0, uint8_t *dest, int dstW,
1741                      int uvalpha, int y, enum AVPixelFormat target,
1742                      int hasAlpha)
1743 {
1744     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1745     int i;
1746
1747     if (uvalpha < 2048) {
1748         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1749             int Y1 = (buf0[i * 2    ] + 64) >> 7;
1750             int Y2 = (buf0[i * 2 + 1] + 64) >> 7;
1751             int U  = (ubuf0[i]        + 64) >> 7;
1752             int V  = (vbuf0[i]        + 64) >> 7;
1753             int A1, A2;
1754             const void *r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1755                        *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1756                        *b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1757
1758             if (hasAlpha) {
1759                 A1 = abuf0[i * 2    ] * 255 + 16384 >> 15;
1760                 A2 = abuf0[i * 2 + 1] * 255 + 16384 >> 15;
1761                 A1 = av_clip_uint8(A1);
1762                 A2 = av_clip_uint8(A2);
1763             }
1764
1765             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1766                           r, g, b, y, target, hasAlpha);
1767         }
1768     } else {
1769         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1770         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1771             int Y1 = (buf0[i * 2    ]     +  64) >> 7;
1772             int Y2 = (buf0[i * 2 + 1]     +  64) >> 7;
1773             int U  = (ubuf0[i] + ubuf1[i] + 128) >> 8;
1774             int V  = (vbuf0[i] + vbuf1[i] + 128) >> 8;
1775             int A1, A2;
1776             const void *r =  c->table_rV[V + YUVRGB_TABLE_HEADROOM],
1777                        *g = (c->table_gU[U + YUVRGB_TABLE_HEADROOM] + c->table_gV[V + YUVRGB_TABLE_HEADROOM]),
1778                        *b =  c->table_bU[U + YUVRGB_TABLE_HEADROOM];
1779
1780             if (hasAlpha) {
1781                 A1 = (abuf0[i * 2    ] + 64) >> 7;
1782                 A2 = (abuf0[i * 2 + 1] + 64) >> 7;
1783                 A1 = av_clip_uint8(A1);
1784                 A2 = av_clip_uint8(A2);
1785             }
1786
1787             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1788                           r, g, b, y, target, hasAlpha);
1789         }
1790     }
1791 }
1792
1793 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1794 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1795                                 const int16_t **lumSrc, int lumFilterSize, \
1796                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
1797                                 const int16_t **chrVSrc, int chrFilterSize, \
1798                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1799                                 int y) \
1800 { \
1801     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1802                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1803                                   alpSrc, dest, dstW, y, fmt, hasAlpha); \
1804 }
1805
1806 #define YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
1807 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1808 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1809                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1810                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1811                                 int yalpha, int uvalpha, int y) \
1812 { \
1813     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1814                                   dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1815 }
1816
1817 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1818 YUV2RGBWRAPPERX2(name, base, ext, fmt, hasAlpha) \
1819 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1820                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1821                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
1822                                 int uvalpha, int y) \
1823 { \
1824     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1825                                   dstW, uvalpha, y, fmt, hasAlpha); \
1826 }
1827
1828 #if CONFIG_SMALL
1829 YUV2RGBWRAPPER(yuv2rgb,,  32_1,  AV_PIX_FMT_RGB32_1,   CONFIG_SWSCALE_ALPHA && c->needAlpha)
1830 YUV2RGBWRAPPER(yuv2rgb,,  32,    AV_PIX_FMT_RGB32,     CONFIG_SWSCALE_ALPHA && c->needAlpha)
1831 #else
1832 #if CONFIG_SWSCALE_ALPHA
1833 YUV2RGBWRAPPER(yuv2rgb,, a32_1,  AV_PIX_FMT_RGB32_1,   1)
1834 YUV2RGBWRAPPER(yuv2rgb,, a32,    AV_PIX_FMT_RGB32,     1)
1835 #endif
1836 YUV2RGBWRAPPER(yuv2rgb,, x32_1,  AV_PIX_FMT_RGB32_1,   0)
1837 YUV2RGBWRAPPER(yuv2rgb,, x32,    AV_PIX_FMT_RGB32,     0)
1838 #endif
1839 YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24,   0)
1840 YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24,   0)
1841 YUV2RGBWRAPPER(yuv2rgb,,  16,    AV_PIX_FMT_RGB565,    0)
1842 YUV2RGBWRAPPER(yuv2rgb,,  15,    AV_PIX_FMT_RGB555,    0)
1843 YUV2RGBWRAPPER(yuv2rgb,,  12,    AV_PIX_FMT_RGB444,    0)
1844 YUV2RGBWRAPPER(yuv2rgb,,   8,    AV_PIX_FMT_RGB8,      0)
1845 YUV2RGBWRAPPER(yuv2rgb,,   4,    AV_PIX_FMT_RGB4,      0)
1846 YUV2RGBWRAPPER(yuv2rgb,,   4b,   AV_PIX_FMT_RGB4_BYTE, 0)
1847 YUV2RGBWRAPPER(yuv2, rgb, x2rgb10, AV_PIX_FMT_X2RGB10, 0)
1848
1849 static av_always_inline void yuv2rgb_write_full(SwsContext *c,
1850     uint8_t *dest, int i, int Y, int A, int U, int V,
1851     int y, enum AVPixelFormat target, int hasAlpha, int err[4])
1852 {
1853     int R, G, B;
1854     int isrgb8 = target == AV_PIX_FMT_BGR8 || target == AV_PIX_FMT_RGB8;
1855
1856     Y -= c->yuv2rgb_y_offset;
1857     Y *= c->yuv2rgb_y_coeff;
1858     Y += 1 << 21;
1859     R = (unsigned)Y + V*c->yuv2rgb_v2r_coeff;
1860     G = (unsigned)Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1861     B = (unsigned)Y +                          U*c->yuv2rgb_u2b_coeff;
1862     if ((R | G | B) & 0xC0000000) {
1863         R = av_clip_uintp2(R, 30);
1864         G = av_clip_uintp2(G, 30);
1865         B = av_clip_uintp2(B, 30);
1866     }
1867
1868     switch(target) {
1869     case AV_PIX_FMT_ARGB:
1870         dest[0] = hasAlpha ? A : 255;
1871         dest[1] = R >> 22;
1872         dest[2] = G >> 22;
1873         dest[3] = B >> 22;
1874         break;
1875     case AV_PIX_FMT_RGB24:
1876         dest[0] = R >> 22;
1877         dest[1] = G >> 22;
1878         dest[2] = B >> 22;
1879         break;
1880     case AV_PIX_FMT_RGBA:
1881         dest[0] = R >> 22;
1882         dest[1] = G >> 22;
1883         dest[2] = B >> 22;
1884         dest[3] = hasAlpha ? A : 255;
1885         break;
1886     case AV_PIX_FMT_ABGR:
1887         dest[0] = hasAlpha ? A : 255;
1888         dest[1] = B >> 22;
1889         dest[2] = G >> 22;
1890         dest[3] = R >> 22;
1891         break;
1892     case AV_PIX_FMT_BGR24:
1893         dest[0] = B >> 22;
1894         dest[1] = G >> 22;
1895         dest[2] = R >> 22;
1896         break;
1897     case AV_PIX_FMT_BGRA:
1898         dest[0] = B >> 22;
1899         dest[1] = G >> 22;
1900         dest[2] = R >> 22;
1901         dest[3] = hasAlpha ? A : 255;
1902         break;
1903     case AV_PIX_FMT_BGR4_BYTE:
1904     case AV_PIX_FMT_RGB4_BYTE:
1905     case AV_PIX_FMT_BGR8:
1906     case AV_PIX_FMT_RGB8:
1907     {
1908         int r,g,b;
1909
1910         switch (c->dither) {
1911         default:
1912         case SWS_DITHER_AUTO:
1913         case SWS_DITHER_ED:
1914             R >>= 22;
1915             G >>= 22;
1916             B >>= 22;
1917             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;
1918             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;
1919             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;
1920             c->dither_error[0][i] = err[0];
1921             c->dither_error[1][i] = err[1];
1922             c->dither_error[2][i] = err[2];
1923             r = R >> (isrgb8 ? 5 : 7);
1924             g = G >> (isrgb8 ? 5 : 6);
1925             b = B >> (isrgb8 ? 6 : 7);
1926             r = av_clip(r, 0, isrgb8 ? 7 : 1);
1927             g = av_clip(g, 0, isrgb8 ? 7 : 3);
1928             b = av_clip(b, 0, isrgb8 ? 3 : 1);
1929             err[0] = R - r*(isrgb8 ? 36 : 255);
1930             err[1] = G - g*(isrgb8 ? 36 : 85);
1931             err[2] = B - b*(isrgb8 ? 85 : 255);
1932             break;
1933         case SWS_DITHER_A_DITHER:
1934             if (isrgb8) {
1935   /* see http://pippin.gimp.org/a_dither/ for details/origin */
1936 #define A_DITHER(u,v)   (((((u)+((v)*236))*119)&0xff))
1937                 r = (((R >> 19) + A_DITHER(i,y)  -96)>>8);
1938                 g = (((G >> 19) + A_DITHER(i + 17,y) - 96)>>8);
1939                 b = (((B >> 20) + A_DITHER(i + 17*2,y) -96)>>8);
1940                 r = av_clip_uintp2(r, 3);
1941                 g = av_clip_uintp2(g, 3);
1942                 b = av_clip_uintp2(b, 2);
1943             } else {
1944                 r = (((R >> 21) + A_DITHER(i,y)-256)>>8);
1945                 g = (((G >> 19) + A_DITHER(i + 17,y)-256)>>8);
1946                 b = (((B >> 21) + A_DITHER(i + 17*2,y)-256)>>8);
1947                 r = av_clip_uintp2(r, 1);
1948                 g = av_clip_uintp2(g, 2);
1949                 b = av_clip_uintp2(b, 1);
1950             }
1951             break;
1952         case SWS_DITHER_X_DITHER:
1953             if (isrgb8) {
1954   /* see http://pippin.gimp.org/a_dither/ for details/origin */
1955 #define X_DITHER(u,v)   (((((u)^((v)*237))*181)&0x1ff)/2)
1956                 r = (((R >> 19) + X_DITHER(i,y) - 96)>>8);
1957                 g = (((G >> 19) + X_DITHER(i + 17,y) - 96)>>8);
1958                 b = (((B >> 20) + X_DITHER(i + 17*2,y) - 96)>>8);
1959                 r = av_clip_uintp2(r, 3);
1960                 g = av_clip_uintp2(g, 3);
1961                 b = av_clip_uintp2(b, 2);
1962             } else {
1963                 r = (((R >> 21) + X_DITHER(i,y)-256)>>8);
1964                 g = (((G >> 19) + X_DITHER(i + 17,y)-256)>>8);
1965                 b = (((B >> 21) + X_DITHER(i + 17*2,y)-256)>>8);
1966                 r = av_clip_uintp2(r, 1);
1967                 g = av_clip_uintp2(g, 2);
1968                 b = av_clip_uintp2(b, 1);
1969             }
1970
1971             break;
1972         }
1973
1974         if(target == AV_PIX_FMT_BGR4_BYTE) {
1975             dest[0] = r + 2*g + 8*b;
1976         } else if(target == AV_PIX_FMT_RGB4_BYTE) {
1977             dest[0] = b + 2*g + 8*r;
1978         } else if(target == AV_PIX_FMT_BGR8) {
1979             dest[0] = r + 8*g + 64*b;
1980         } else if(target == AV_PIX_FMT_RGB8) {
1981             dest[0] = b + 4*g + 32*r;
1982         } else
1983             av_assert2(0);
1984         break;}
1985     }
1986 }
1987
1988 static av_always_inline void
1989 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1990                           const int16_t **lumSrc, int lumFilterSize,
1991                           const int16_t *chrFilter, const int16_t **chrUSrc,
1992                           const int16_t **chrVSrc, int chrFilterSize,
1993                           const int16_t **alpSrc, uint8_t *dest,
1994                           int dstW, int y, enum AVPixelFormat target, int hasAlpha)
1995 {
1996     int i;
1997     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1998     int err[4] = {0};
1999     int A = 0; //init to silence warning
2000
2001     if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
2002        || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
2003         step = 1;
2004
2005     for (i = 0; i < dstW; i++) {
2006         int j;
2007         int Y = 1<<9;
2008         int U = (1<<9)-(128 << 19);
2009         int V = (1<<9)-(128 << 19);
2010
2011         for (j = 0; j < lumFilterSize; j++) {
2012             Y += lumSrc[j][i] * lumFilter[j];
2013         }
2014         for (j = 0; j < chrFilterSize; j++) {
2015             U += chrUSrc[j][i] * chrFilter[j];
2016             V += chrVSrc[j][i] * chrFilter[j];
2017         }
2018         Y >>= 10;
2019         U >>= 10;
2020         V >>= 10;
2021         if (hasAlpha) {
2022             A = 1 << 18;
2023             for (j = 0; j < lumFilterSize; j++) {
2024                 A += alpSrc[j][i] * lumFilter[j];
2025             }
2026             A >>= 19;
2027             if (A & 0x100)
2028                 A = av_clip_uint8(A);
2029         }
2030         yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
2031         dest += step;
2032     }
2033     c->dither_error[0][i] = err[0];
2034     c->dither_error[1][i] = err[1];
2035     c->dither_error[2][i] = err[2];
2036 }
2037
2038 static av_always_inline void
2039 yuv2rgb_full_2_c_template(SwsContext *c, const int16_t *buf[2],
2040                      const int16_t *ubuf[2], const int16_t *vbuf[2],
2041                      const int16_t *abuf[2], uint8_t *dest, int dstW,
2042                      int yalpha, int uvalpha, int y,
2043                      enum AVPixelFormat target, int hasAlpha)
2044 {
2045     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
2046                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
2047                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
2048                   *abuf0 = hasAlpha ? abuf[0] : NULL,
2049                   *abuf1 = hasAlpha ? abuf[1] : NULL;
2050     int  yalpha1 = 4096 - yalpha;
2051     int uvalpha1 = 4096 - uvalpha;
2052     int i;
2053     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
2054     int err[4] = {0};
2055     int A = 0; // init to silcene warning
2056
2057     av_assert2(yalpha  <= 4096U);
2058     av_assert2(uvalpha <= 4096U);
2059
2060     if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
2061        || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
2062         step = 1;
2063
2064     for (i = 0; i < dstW; i++) {
2065         int Y = ( buf0[i] * yalpha1  +  buf1[i] * yalpha             ) >> 10; //FIXME rounding
2066         int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha-(128 << 19)) >> 10;
2067         int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha-(128 << 19)) >> 10;
2068
2069         if (hasAlpha) {
2070             A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha + (1<<18)) >> 19;
2071             if (A & 0x100)
2072                 A = av_clip_uint8(A);
2073         }
2074
2075         yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
2076         dest += step;
2077     }
2078     c->dither_error[0][i] = err[0];
2079     c->dither_error[1][i] = err[1];
2080     c->dither_error[2][i] = err[2];
2081 }
2082
2083 static av_always_inline void
2084 yuv2rgb_full_1_c_template(SwsContext *c, const int16_t *buf0,
2085                      const int16_t *ubuf[2], const int16_t *vbuf[2],
2086                      const int16_t *abuf0, uint8_t *dest, int dstW,
2087                      int uvalpha, int y, enum AVPixelFormat target,
2088                      int hasAlpha)
2089 {
2090     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
2091     int i;
2092     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
2093     int err[4] = {0};
2094
2095     if(   target == AV_PIX_FMT_BGR4_BYTE || target == AV_PIX_FMT_RGB4_BYTE
2096        || target == AV_PIX_FMT_BGR8      || target == AV_PIX_FMT_RGB8)
2097         step = 1;
2098
2099     if (uvalpha < 2048) {
2100         int A = 0; //init to silence warning
2101         for (i = 0; i < dstW; i++) {
2102             int Y = buf0[i] * 4;
2103             int U = (ubuf0[i] - (128<<7)) * 4;
2104             int V = (vbuf0[i] - (128<<7)) * 4;
2105
2106             if (hasAlpha) {
2107                 A = (abuf0[i] + 64) >> 7;
2108                 if (A & 0x100)
2109                     A = av_clip_uint8(A);
2110             }
2111
2112             yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
2113             dest += step;
2114         }
2115     } else {
2116         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
2117         int A = 0; //init to silence warning
2118         for (i = 0; i < dstW; i++) {
2119             int Y = buf0[i] * 4;
2120             int U = (ubuf0[i] + ubuf1[i] - (128<<8)) * 2;
2121             int V = (vbuf0[i] + vbuf1[i] - (128<<8)) * 2;
2122
2123             if (hasAlpha) {
2124                 A = (abuf0[i] + 64) >> 7;
2125                 if (A & 0x100)
2126                     A = av_clip_uint8(A);
2127             }
2128
2129             yuv2rgb_write_full(c, dest, i, Y, A, U, V, y, target, hasAlpha, err);
2130             dest += step;
2131         }
2132     }
2133
2134     c->dither_error[0][i] = err[0];
2135     c->dither_error[1][i] = err[1];
2136     c->dither_error[2][i] = err[2];
2137 }
2138
2139 #if CONFIG_SMALL
2140 YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  CONFIG_SWSCALE_ALPHA && c->needAlpha)
2141 YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  CONFIG_SWSCALE_ALPHA && c->needAlpha)
2142 YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  CONFIG_SWSCALE_ALPHA && c->needAlpha)
2143 YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  CONFIG_SWSCALE_ALPHA && c->needAlpha)
2144 #else
2145 #if CONFIG_SWSCALE_ALPHA
2146 YUV2RGBWRAPPER(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  1)
2147 YUV2RGBWRAPPER(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  1)
2148 YUV2RGBWRAPPER(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  1)
2149 YUV2RGBWRAPPER(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  1)
2150 #endif
2151 YUV2RGBWRAPPER(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA,  0)
2152 YUV2RGBWRAPPER(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR,  0)
2153 YUV2RGBWRAPPER(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA,  0)
2154 YUV2RGBWRAPPER(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB,  0)
2155 #endif
2156 YUV2RGBWRAPPER(yuv2, rgb_full, bgr24_full,  AV_PIX_FMT_BGR24, 0)
2157 YUV2RGBWRAPPER(yuv2, rgb_full, rgb24_full,  AV_PIX_FMT_RGB24, 0)
2158
2159 YUV2RGBWRAPPER(yuv2, rgb_full, bgr4_byte_full,  AV_PIX_FMT_BGR4_BYTE, 0)
2160 YUV2RGBWRAPPER(yuv2, rgb_full, rgb4_byte_full,  AV_PIX_FMT_RGB4_BYTE, 0)
2161 YUV2RGBWRAPPER(yuv2, rgb_full, bgr8_full,   AV_PIX_FMT_BGR8,  0)
2162 YUV2RGBWRAPPER(yuv2, rgb_full, rgb8_full,   AV_PIX_FMT_RGB8,  0)
2163
2164 static void
2165 yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
2166                   const int16_t **lumSrc, int lumFilterSize,
2167                   const int16_t *chrFilter, const int16_t **chrUSrc,
2168                   const int16_t **chrVSrc, int chrFilterSize,
2169                   const int16_t **alpSrc, uint8_t **dest,
2170                   int dstW, int y)
2171 {
2172     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
2173     int i;
2174     int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
2175     uint16_t **dest16 = (uint16_t**)dest;
2176     int SH = 22 + 8 - desc->comp[0].depth;
2177     int A = 0; // init to silence warning
2178
2179     for (i = 0; i < dstW; i++) {
2180         int j;
2181         int Y = 1 << 9;
2182         int U = (1 << 9) - (128 << 19);
2183         int V = (1 << 9) - (128 << 19);
2184         int R, G, B;
2185
2186         for (j = 0; j < lumFilterSize; j++)
2187             Y += lumSrc[j][i] * lumFilter[j];
2188
2189         for (j = 0; j < chrFilterSize; j++) {
2190             U += chrUSrc[j][i] * chrFilter[j];
2191             V += chrVSrc[j][i] * chrFilter[j];
2192         }
2193
2194         Y >>= 10;
2195         U >>= 10;
2196         V >>= 10;
2197
2198         if (hasAlpha) {
2199             A = 1 << 18;
2200
2201             for (j = 0; j < lumFilterSize; j++)
2202                 A += alpSrc[j][i] * lumFilter[j];
2203
2204             if (A & 0xF8000000)
2205                 A =  av_clip_uintp2(A, 27);
2206         }
2207
2208         Y -= c->yuv2rgb_y_offset;
2209         Y *= c->yuv2rgb_y_coeff;
2210         Y += 1 << (SH-1);
2211         R = Y + V * c->yuv2rgb_v2r_coeff;
2212         G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
2213         B = Y +                            U * c->yuv2rgb_u2b_coeff;
2214
2215         if ((R | G | B) & 0xC0000000) {
2216             R = av_clip_uintp2(R, 30);
2217             G = av_clip_uintp2(G, 30);
2218             B = av_clip_uintp2(B, 30);
2219         }
2220
2221         if (SH != 22) {
2222             dest16[0][i] = G >> SH;
2223             dest16[1][i] = B >> SH;
2224             dest16[2][i] = R >> SH;
2225             if (hasAlpha)
2226                 dest16[3][i] = A >> (SH - 3);
2227         } else {
2228             dest[0][i] = G >> 22;
2229             dest[1][i] = B >> 22;
2230             dest[2][i] = R >> 22;
2231             if (hasAlpha)
2232                 dest[3][i] = A >> 19;
2233         }
2234     }
2235     if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
2236         for (i = 0; i < dstW; i++) {
2237             dest16[0][i] = av_bswap16(dest16[0][i]);
2238             dest16[1][i] = av_bswap16(dest16[1][i]);
2239             dest16[2][i] = av_bswap16(dest16[2][i]);
2240             if (hasAlpha)
2241                 dest16[3][i] = av_bswap16(dest16[3][i]);
2242         }
2243     }
2244 }
2245
2246 static void
2247 yuv2gbrp16_full_X_c(SwsContext *c, const int16_t *lumFilter,
2248                     const int16_t **lumSrcx, int lumFilterSize,
2249                     const int16_t *chrFilter, const int16_t **chrUSrcx,
2250                     const int16_t **chrVSrcx, int chrFilterSize,
2251                     const int16_t **alpSrcx, uint8_t **dest,
2252                     int dstW, int y)
2253 {
2254     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
2255     int i;
2256     int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrcx;
2257     uint16_t **dest16 = (uint16_t**)dest;
2258     const int32_t **lumSrc  = (const int32_t**)lumSrcx;
2259     const int32_t **chrUSrc = (const int32_t**)chrUSrcx;
2260     const int32_t **chrVSrc = (const int32_t**)chrVSrcx;
2261     const int32_t **alpSrc  = (const int32_t**)alpSrcx;
2262
2263     for (i = 0; i < dstW; i++) {
2264         int j;
2265         int Y = -0x40000000;
2266         int U = -(128 << 23);
2267         int V = -(128 << 23);
2268         int R, G, B, A;
2269
2270         for (j = 0; j < lumFilterSize; j++)
2271             Y += lumSrc[j][i] * (unsigned)lumFilter[j];
2272
2273         for (j = 0; j < chrFilterSize; j++) {
2274             U += chrUSrc[j][i] * (unsigned)chrFilter[j];
2275             V += chrVSrc[j][i] * (unsigned)chrFilter[j];
2276         }
2277
2278         Y >>= 14;
2279         Y += 0x10000;
2280         U >>= 14;
2281         V >>= 14;
2282
2283         if (hasAlpha) {
2284             A = -0x40000000;
2285
2286             for (j = 0; j < lumFilterSize; j++)
2287                 A += alpSrc[j][i] * (unsigned)lumFilter[j];
2288
2289             A >>= 1;
2290             A += 0x20002000;
2291         }
2292
2293         Y -= c->yuv2rgb_y_offset;
2294         Y *= c->yuv2rgb_y_coeff;
2295         Y += 1 << 13;
2296         R = V * c->yuv2rgb_v2r_coeff;
2297         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
2298         B =                            U * c->yuv2rgb_u2b_coeff;
2299
2300         R = av_clip_uintp2(Y + R, 30);
2301         G = av_clip_uintp2(Y + G, 30);
2302         B = av_clip_uintp2(Y + B, 30);
2303
2304         dest16[0][i] = G >> 14;
2305         dest16[1][i] = B >> 14;
2306         dest16[2][i] = R >> 14;
2307         if (hasAlpha)
2308             dest16[3][i] = av_clip_uintp2(A, 30) >> 14;
2309     }
2310     if ((!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
2311         for (i = 0; i < dstW; i++) {
2312             dest16[0][i] = av_bswap16(dest16[0][i]);
2313             dest16[1][i] = av_bswap16(dest16[1][i]);
2314             dest16[2][i] = av_bswap16(dest16[2][i]);
2315             if (hasAlpha)
2316                 dest16[3][i] = av_bswap16(dest16[3][i]);
2317         }
2318     }
2319 }
2320
2321 static void
2322 yuv2gbrpf32_full_X_c(SwsContext *c, const int16_t *lumFilter,
2323                     const int16_t **lumSrcx, int lumFilterSize,
2324                     const int16_t *chrFilter, const int16_t **chrUSrcx,
2325                     const int16_t **chrVSrcx, int chrFilterSize,
2326                     const int16_t **alpSrcx, uint8_t **dest,
2327                     int dstW, int y)
2328 {
2329     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
2330     int i;
2331     int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrcx;
2332     uint32_t **dest32 = (uint32_t**)dest;
2333     const int32_t **lumSrc  = (const int32_t**)lumSrcx;
2334     const int32_t **chrUSrc = (const int32_t**)chrUSrcx;
2335     const int32_t **chrVSrc = (const int32_t**)chrVSrcx;
2336     const int32_t **alpSrc  = (const int32_t**)alpSrcx;
2337     static const float float_mult = 1.0f / 65535.0f;
2338
2339     for (i = 0; i < dstW; i++) {
2340         int j;
2341         int Y = -0x40000000;
2342         int U = -(128 << 23);
2343         int V = -(128 << 23);
2344         int R, G, B, A;
2345
2346         for (j = 0; j < lumFilterSize; j++)
2347             Y += lumSrc[j][i] * (unsigned)lumFilter[j];
2348
2349         for (j = 0; j < chrFilterSize; j++) {
2350             U += chrUSrc[j][i] * (unsigned)chrFilter[j];
2351             V += chrVSrc[j][i] * (unsigned)chrFilter[j];
2352         }
2353
2354         Y >>= 14;
2355         Y += 0x10000;
2356         U >>= 14;
2357         V >>= 14;
2358
2359         if (hasAlpha) {
2360             A = -0x40000000;
2361
2362             for (j = 0; j < lumFilterSize; j++)
2363                 A += alpSrc[j][i] * (unsigned)lumFilter[j];
2364
2365             A >>= 1;
2366             A += 0x20002000;
2367         }
2368
2369         Y -= c->yuv2rgb_y_offset;
2370         Y *= c->yuv2rgb_y_coeff;
2371         Y += 1 << 13;
2372         R = V * c->yuv2rgb_v2r_coeff;
2373         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
2374         B =                            U * c->yuv2rgb_u2b_coeff;
2375
2376         R = av_clip_uintp2(Y + R, 30);
2377         G = av_clip_uintp2(Y + G, 30);
2378         B = av_clip_uintp2(Y + B, 30);
2379
2380         dest32[0][i] = av_float2int(float_mult * (float)(G >> 14));
2381         dest32[1][i] = av_float2int(float_mult * (float)(B >> 14));
2382         dest32[2][i] = av_float2int(float_mult * (float)(R >> 14));
2383         if (hasAlpha)
2384             dest32[3][i] = av_float2int(float_mult * (float)(av_clip_uintp2(A, 30) >> 14));
2385     }
2386     if ((!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
2387         for (i = 0; i < dstW; i++) {
2388             dest32[0][i] = av_bswap32(dest32[0][i]);
2389             dest32[1][i] = av_bswap32(dest32[1][i]);
2390             dest32[2][i] = av_bswap32(dest32[2][i]);
2391             if (hasAlpha)
2392                 dest32[3][i] = av_bswap32(dest32[3][i]);
2393         }
2394     }
2395 }
2396
2397 static void
2398 yuv2ya8_1_c(SwsContext *c, const int16_t *buf0,
2399             const int16_t *ubuf[2], const int16_t *vbuf[2],
2400             const int16_t *abuf0, uint8_t *dest, int dstW,
2401             int uvalpha, int y)
2402 {
2403     int hasAlpha = !!abuf0;
2404     int i;
2405
2406     for (i = 0; i < dstW; i++) {
2407         int Y = (buf0[i] + 64) >> 7;
2408         int A;
2409
2410         Y = av_clip_uint8(Y);
2411
2412         if (hasAlpha) {
2413             A = (abuf0[i] + 64) >> 7;
2414             if (A & 0x100)
2415                 A = av_clip_uint8(A);
2416         }
2417
2418         dest[i * 2    ] = Y;
2419         dest[i * 2 + 1] = hasAlpha ? A : 255;
2420     }
2421 }
2422
2423 static void
2424 yuv2ya8_2_c(SwsContext *c, const int16_t *buf[2],
2425             const int16_t *ubuf[2], const int16_t *vbuf[2],
2426             const int16_t *abuf[2], uint8_t *dest, int dstW,
2427             int yalpha, int uvalpha, int y)
2428 {
2429     int hasAlpha = abuf && abuf[0] && abuf[1];
2430     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
2431                   *abuf0 = hasAlpha ? abuf[0] : NULL,
2432                   *abuf1 = hasAlpha ? abuf[1] : NULL;
2433     int  yalpha1 = 4096 - yalpha;
2434     int i;
2435
2436     av_assert2(yalpha  <= 4096U);
2437
2438     for (i = 0; i < dstW; i++) {
2439         int Y = (buf0[i] * yalpha1 + buf1[i] * yalpha) >> 19;
2440         int A;
2441
2442         Y = av_clip_uint8(Y);
2443
2444         if (hasAlpha) {
2445             A = (abuf0[i] * yalpha1 + abuf1[i] * yalpha) >> 19;
2446             A = av_clip_uint8(A);
2447         }
2448
2449         dest[i * 2    ] = Y;
2450         dest[i * 2 + 1] = hasAlpha ? A : 255;
2451     }
2452 }
2453
2454 static void
2455 yuv2ya8_X_c(SwsContext *c, const int16_t *lumFilter,
2456             const int16_t **lumSrc, int lumFilterSize,
2457             const int16_t *chrFilter, const int16_t **chrUSrc,
2458             const int16_t **chrVSrc, int chrFilterSize,
2459             const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
2460 {
2461     int hasAlpha = !!alpSrc;
2462     int i;
2463
2464     for (i = 0; i < dstW; i++) {
2465         int j;
2466         int Y = 1 << 18, A = 1 << 18;
2467
2468         for (j = 0; j < lumFilterSize; j++)
2469             Y += lumSrc[j][i] * lumFilter[j];
2470
2471         Y >>= 19;
2472         if (Y  & 0x100)
2473             Y = av_clip_uint8(Y);
2474
2475         if (hasAlpha) {
2476             for (j = 0; j < lumFilterSize; j++)
2477                 A += alpSrc[j][i] * lumFilter[j];
2478
2479             A >>= 19;
2480
2481             if (A & 0x100)
2482                 A = av_clip_uint8(A);
2483         }
2484
2485         dest[2 * i    ] = Y;
2486         dest[2 * i + 1] = hasAlpha ? A : 255;
2487     }
2488 }
2489
2490 static void
2491 yuv2ayuv64le_X_c(SwsContext *c, const int16_t *lumFilter,
2492                  const int16_t **_lumSrc, int lumFilterSize,
2493                  const int16_t *chrFilter, const int16_t **_chrUSrc,
2494                  const int16_t **_chrVSrc, int chrFilterSize,
2495                  const int16_t **_alpSrc, uint8_t *dest, int dstW, int y)
2496 {
2497     const int32_t **lumSrc  = (const int32_t **) _lumSrc,
2498                   **chrUSrc = (const int32_t **) _chrUSrc,
2499                   **chrVSrc = (const int32_t **) _chrVSrc,
2500                   **alpSrc  = (const int32_t **) _alpSrc;
2501     int hasAlpha = !!alpSrc;
2502     int i;
2503
2504     for (i = 0; i < dstW; i++) {
2505         int Y = 1 << 14, U = 1 << 14;
2506         int V = 1 << 14, A = 1 << 14;
2507         int j;
2508
2509         Y -= 0x40000000;
2510         U -= 0x40000000;
2511         V -= 0x40000000;
2512         A -= 0x40000000;
2513
2514         for (j = 0; j < lumFilterSize; j++)
2515             Y += lumSrc[j][i] * (unsigned)lumFilter[j];
2516
2517         for (j = 0; j < chrFilterSize; j++)
2518             U += chrUSrc[j][i] * (unsigned)chrFilter[j];
2519
2520         for (j = 0; j < chrFilterSize; j++)
2521             V += chrVSrc[j][i] * (unsigned)chrFilter[j];
2522
2523         if (hasAlpha)
2524             for (j = 0; j < lumFilterSize; j++)
2525                 A += alpSrc[j][i] * (unsigned)lumFilter[j];
2526
2527         Y = 0x8000 + av_clip_int16(Y >> 15);
2528         U = 0x8000 + av_clip_int16(U >> 15);
2529         V = 0x8000 + av_clip_int16(V >> 15);
2530         A = 0x8000 + av_clip_int16(A >> 15);
2531
2532         AV_WL16(dest + 8 * i, hasAlpha ? A : 65535);
2533         AV_WL16(dest + 8 * i + 2, Y);
2534         AV_WL16(dest + 8 * i + 4, U);
2535         AV_WL16(dest + 8 * i + 6, V);
2536     }
2537 }
2538
2539 av_cold void ff_sws_init_output_funcs(SwsContext *c,
2540                                       yuv2planar1_fn *yuv2plane1,
2541                                       yuv2planarX_fn *yuv2planeX,
2542                                       yuv2interleavedX_fn *yuv2nv12cX,
2543                                       yuv2packed1_fn *yuv2packed1,
2544                                       yuv2packed2_fn *yuv2packed2,
2545                                       yuv2packedX_fn *yuv2packedX,
2546                                       yuv2anyX_fn *yuv2anyX)
2547 {
2548     enum AVPixelFormat dstFormat = c->dstFormat;
2549     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
2550
2551     if (dstFormat == AV_PIX_FMT_P010LE || dstFormat == AV_PIX_FMT_P010BE) {
2552         *yuv2plane1 = isBE(dstFormat) ? yuv2p010l1_BE_c : yuv2p010l1_LE_c;
2553         *yuv2planeX = isBE(dstFormat) ? yuv2p010lX_BE_c : yuv2p010lX_LE_c;
2554         *yuv2nv12cX = yuv2p010cX_c;
2555     } else if (is16BPS(dstFormat)) {
2556         *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c  : yuv2planeX_16LE_c;
2557         *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c  : yuv2plane1_16LE_c;
2558         if (dstFormat == AV_PIX_FMT_P016LE || dstFormat == AV_PIX_FMT_P016BE) {
2559           *yuv2nv12cX = yuv2p016cX_c;
2560         }
2561     } else if (isNBPS(dstFormat)) {
2562         if (desc->comp[0].depth == 9) {
2563             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c  : yuv2planeX_9LE_c;
2564             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c  : yuv2plane1_9LE_c;
2565         } else if (desc->comp[0].depth == 10) {
2566             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c  : yuv2planeX_10LE_c;
2567             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c  : yuv2plane1_10LE_c;
2568         } else if (desc->comp[0].depth == 12) {
2569             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c  : yuv2planeX_12LE_c;
2570             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c  : yuv2plane1_12LE_c;
2571         } else if (desc->comp[0].depth == 14) {
2572             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c  : yuv2planeX_14LE_c;
2573             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c  : yuv2plane1_14LE_c;
2574         } else
2575             av_assert0(0);
2576     } else if (dstFormat == AV_PIX_FMT_GRAYF32BE) {
2577         *yuv2planeX = yuv2planeX_floatBE_c;
2578         *yuv2plane1 = yuv2plane1_floatBE_c;
2579     } else if (dstFormat == AV_PIX_FMT_GRAYF32LE) {
2580         *yuv2planeX = yuv2planeX_floatLE_c;
2581         *yuv2plane1 = yuv2plane1_floatLE_c;
2582     } else {
2583         *yuv2plane1 = yuv2plane1_8_c;
2584         *yuv2planeX = yuv2planeX_8_c;
2585         if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21 ||
2586             dstFormat == AV_PIX_FMT_NV24 || dstFormat == AV_PIX_FMT_NV42)
2587             *yuv2nv12cX = yuv2nv12cX_c;
2588     }
2589
2590     if(c->flags & SWS_FULL_CHR_H_INT) {
2591         switch (dstFormat) {
2592             case AV_PIX_FMT_RGBA:
2593 #if CONFIG_SMALL
2594                 *yuv2packedX = yuv2rgba32_full_X_c;
2595                 *yuv2packed2 = yuv2rgba32_full_2_c;
2596                 *yuv2packed1 = yuv2rgba32_full_1_c;
2597 #else
2598 #if CONFIG_SWSCALE_ALPHA
2599                 if (c->needAlpha) {
2600                     *yuv2packedX = yuv2rgba32_full_X_c;
2601                     *yuv2packed2 = yuv2rgba32_full_2_c;
2602                     *yuv2packed1 = yuv2rgba32_full_1_c;
2603                 } else
2604 #endif /* CONFIG_SWSCALE_ALPHA */
2605                 {
2606                     *yuv2packedX = yuv2rgbx32_full_X_c;
2607                     *yuv2packed2 = yuv2rgbx32_full_2_c;
2608                     *yuv2packed1 = yuv2rgbx32_full_1_c;
2609                 }
2610 #endif /* !CONFIG_SMALL */
2611                 break;
2612             case AV_PIX_FMT_ARGB:
2613 #if CONFIG_SMALL
2614                 *yuv2packedX = yuv2argb32_full_X_c;
2615                 *yuv2packed2 = yuv2argb32_full_2_c;
2616                 *yuv2packed1 = yuv2argb32_full_1_c;
2617 #else
2618 #if CONFIG_SWSCALE_ALPHA
2619                 if (c->needAlpha) {
2620                     *yuv2packedX = yuv2argb32_full_X_c;
2621                     *yuv2packed2 = yuv2argb32_full_2_c;
2622                     *yuv2packed1 = yuv2argb32_full_1_c;
2623                 } else
2624 #endif /* CONFIG_SWSCALE_ALPHA */
2625                 {
2626                     *yuv2packedX = yuv2xrgb32_full_X_c;
2627                     *yuv2packed2 = yuv2xrgb32_full_2_c;
2628                     *yuv2packed1 = yuv2xrgb32_full_1_c;
2629                 }
2630 #endif /* !CONFIG_SMALL */
2631                 break;
2632             case AV_PIX_FMT_BGRA:
2633 #if CONFIG_SMALL
2634                 *yuv2packedX = yuv2bgra32_full_X_c;
2635                 *yuv2packed2 = yuv2bgra32_full_2_c;
2636                 *yuv2packed1 = yuv2bgra32_full_1_c;
2637 #else
2638 #if CONFIG_SWSCALE_ALPHA
2639                 if (c->needAlpha) {
2640                     *yuv2packedX = yuv2bgra32_full_X_c;
2641                     *yuv2packed2 = yuv2bgra32_full_2_c;
2642                     *yuv2packed1 = yuv2bgra32_full_1_c;
2643                 } else
2644 #endif /* CONFIG_SWSCALE_ALPHA */
2645                 {
2646                     *yuv2packedX = yuv2bgrx32_full_X_c;
2647                     *yuv2packed2 = yuv2bgrx32_full_2_c;
2648                     *yuv2packed1 = yuv2bgrx32_full_1_c;
2649                 }
2650 #endif /* !CONFIG_SMALL */
2651                 break;
2652             case AV_PIX_FMT_ABGR:
2653 #if CONFIG_SMALL
2654                 *yuv2packedX = yuv2abgr32_full_X_c;
2655                 *yuv2packed2 = yuv2abgr32_full_2_c;
2656                 *yuv2packed1 = yuv2abgr32_full_1_c;
2657 #else
2658 #if CONFIG_SWSCALE_ALPHA
2659                 if (c->needAlpha) {
2660                     *yuv2packedX = yuv2abgr32_full_X_c;
2661                     *yuv2packed2 = yuv2abgr32_full_2_c;
2662                     *yuv2packed1 = yuv2abgr32_full_1_c;
2663                 } else
2664 #endif /* CONFIG_SWSCALE_ALPHA */
2665                 {
2666                     *yuv2packedX = yuv2xbgr32_full_X_c;
2667                     *yuv2packed2 = yuv2xbgr32_full_2_c;
2668                     *yuv2packed1 = yuv2xbgr32_full_1_c;
2669                 }
2670 #endif /* !CONFIG_SMALL */
2671                 break;
2672         case AV_PIX_FMT_RGBA64LE:
2673 #if CONFIG_SWSCALE_ALPHA
2674             if (c->needAlpha) {
2675                 *yuv2packedX = yuv2rgba64le_full_X_c;
2676                 *yuv2packed2 = yuv2rgba64le_full_2_c;
2677                 *yuv2packed1 = yuv2rgba64le_full_1_c;
2678             } else
2679 #endif /* CONFIG_SWSCALE_ALPHA */
2680             {
2681                 *yuv2packedX = yuv2rgbx64le_full_X_c;
2682                 *yuv2packed2 = yuv2rgbx64le_full_2_c;
2683                 *yuv2packed1 = yuv2rgbx64le_full_1_c;
2684             }
2685             break;
2686         case AV_PIX_FMT_RGBA64BE:
2687 #if CONFIG_SWSCALE_ALPHA
2688             if (c->needAlpha) {
2689                 *yuv2packedX = yuv2rgba64be_full_X_c;
2690                 *yuv2packed2 = yuv2rgba64be_full_2_c;
2691                 *yuv2packed1 = yuv2rgba64be_full_1_c;
2692             } else
2693 #endif /* CONFIG_SWSCALE_ALPHA */
2694             {
2695                 *yuv2packedX = yuv2rgbx64be_full_X_c;
2696                 *yuv2packed2 = yuv2rgbx64be_full_2_c;
2697                 *yuv2packed1 = yuv2rgbx64be_full_1_c;
2698             }
2699             break;
2700         case AV_PIX_FMT_BGRA64LE:
2701 #if CONFIG_SWSCALE_ALPHA
2702             if (c->needAlpha) {
2703                 *yuv2packedX = yuv2bgra64le_full_X_c;
2704                 *yuv2packed2 = yuv2bgra64le_full_2_c;
2705                 *yuv2packed1 = yuv2bgra64le_full_1_c;
2706             } else
2707 #endif /* CONFIG_SWSCALE_ALPHA */
2708             {
2709                 *yuv2packedX = yuv2bgrx64le_full_X_c;
2710                 *yuv2packed2 = yuv2bgrx64le_full_2_c;
2711                 *yuv2packed1 = yuv2bgrx64le_full_1_c;
2712             }
2713             break;
2714         case AV_PIX_FMT_BGRA64BE:
2715 #if CONFIG_SWSCALE_ALPHA
2716             if (c->needAlpha) {
2717                 *yuv2packedX = yuv2bgra64be_full_X_c;
2718                 *yuv2packed2 = yuv2bgra64be_full_2_c;
2719                 *yuv2packed1 = yuv2bgra64be_full_1_c;
2720             } else
2721 #endif /* CONFIG_SWSCALE_ALPHA */
2722             {
2723                 *yuv2packedX = yuv2bgrx64be_full_X_c;
2724                 *yuv2packed2 = yuv2bgrx64be_full_2_c;
2725                 *yuv2packed1 = yuv2bgrx64be_full_1_c;
2726             }
2727             break;
2728
2729         case AV_PIX_FMT_RGB24:
2730             *yuv2packedX = yuv2rgb24_full_X_c;
2731             *yuv2packed2 = yuv2rgb24_full_2_c;
2732             *yuv2packed1 = yuv2rgb24_full_1_c;
2733             break;
2734         case AV_PIX_FMT_BGR24:
2735             *yuv2packedX = yuv2bgr24_full_X_c;
2736             *yuv2packed2 = yuv2bgr24_full_2_c;
2737             *yuv2packed1 = yuv2bgr24_full_1_c;
2738             break;
2739         case AV_PIX_FMT_RGB48LE:
2740             *yuv2packedX = yuv2rgb48le_full_X_c;
2741             *yuv2packed2 = yuv2rgb48le_full_2_c;
2742             *yuv2packed1 = yuv2rgb48le_full_1_c;
2743             break;
2744         case AV_PIX_FMT_BGR48LE:
2745             *yuv2packedX = yuv2bgr48le_full_X_c;
2746             *yuv2packed2 = yuv2bgr48le_full_2_c;
2747             *yuv2packed1 = yuv2bgr48le_full_1_c;
2748             break;
2749         case AV_PIX_FMT_RGB48BE:
2750             *yuv2packedX = yuv2rgb48be_full_X_c;
2751             *yuv2packed2 = yuv2rgb48be_full_2_c;
2752             *yuv2packed1 = yuv2rgb48be_full_1_c;
2753             break;
2754         case AV_PIX_FMT_BGR48BE:
2755             *yuv2packedX = yuv2bgr48be_full_X_c;
2756             *yuv2packed2 = yuv2bgr48be_full_2_c;
2757             *yuv2packed1 = yuv2bgr48be_full_1_c;
2758             break;
2759         case AV_PIX_FMT_BGR4_BYTE:
2760             *yuv2packedX = yuv2bgr4_byte_full_X_c;
2761             *yuv2packed2 = yuv2bgr4_byte_full_2_c;
2762             *yuv2packed1 = yuv2bgr4_byte_full_1_c;
2763             break;
2764         case AV_PIX_FMT_RGB4_BYTE:
2765             *yuv2packedX = yuv2rgb4_byte_full_X_c;
2766             *yuv2packed2 = yuv2rgb4_byte_full_2_c;
2767             *yuv2packed1 = yuv2rgb4_byte_full_1_c;
2768             break;
2769         case AV_PIX_FMT_BGR8:
2770             *yuv2packedX = yuv2bgr8_full_X_c;
2771             *yuv2packed2 = yuv2bgr8_full_2_c;
2772             *yuv2packed1 = yuv2bgr8_full_1_c;
2773             break;
2774         case AV_PIX_FMT_RGB8:
2775             *yuv2packedX = yuv2rgb8_full_X_c;
2776             *yuv2packed2 = yuv2rgb8_full_2_c;
2777             *yuv2packed1 = yuv2rgb8_full_1_c;
2778             break;
2779         case AV_PIX_FMT_GBRP:
2780         case AV_PIX_FMT_GBRP9BE:
2781         case AV_PIX_FMT_GBRP9LE:
2782         case AV_PIX_FMT_GBRP10BE:
2783         case AV_PIX_FMT_GBRP10LE:
2784         case AV_PIX_FMT_GBRP12BE:
2785         case AV_PIX_FMT_GBRP12LE:
2786         case AV_PIX_FMT_GBRP14BE:
2787         case AV_PIX_FMT_GBRP14LE:
2788         case AV_PIX_FMT_GBRAP:
2789         case AV_PIX_FMT_GBRAP10BE:
2790         case AV_PIX_FMT_GBRAP10LE:
2791         case AV_PIX_FMT_GBRAP12BE:
2792         case AV_PIX_FMT_GBRAP12LE:
2793             *yuv2anyX = yuv2gbrp_full_X_c;
2794             break;
2795         case AV_PIX_FMT_GBRP16BE:
2796         case AV_PIX_FMT_GBRP16LE:
2797         case AV_PIX_FMT_GBRAP16BE:
2798         case AV_PIX_FMT_GBRAP16LE:
2799             *yuv2anyX = yuv2gbrp16_full_X_c;
2800             break;
2801         case AV_PIX_FMT_GBRPF32BE:
2802         case AV_PIX_FMT_GBRPF32LE:
2803         case AV_PIX_FMT_GBRAPF32BE:
2804         case AV_PIX_FMT_GBRAPF32LE:
2805             *yuv2anyX = yuv2gbrpf32_full_X_c;
2806             break;
2807         }
2808         if (!*yuv2packedX && !*yuv2anyX)
2809             goto YUV_PACKED;
2810     } else {
2811         YUV_PACKED:
2812         switch (dstFormat) {
2813         case AV_PIX_FMT_RGBA64LE:
2814 #if CONFIG_SWSCALE_ALPHA
2815             if (c->needAlpha) {
2816                 *yuv2packed1 = yuv2rgba64le_1_c;
2817                 *yuv2packed2 = yuv2rgba64le_2_c;
2818                 *yuv2packedX = yuv2rgba64le_X_c;
2819             } else
2820 #endif /* CONFIG_SWSCALE_ALPHA */
2821             {
2822                 *yuv2packed1 = yuv2rgbx64le_1_c;
2823                 *yuv2packed2 = yuv2rgbx64le_2_c;
2824                 *yuv2packedX = yuv2rgbx64le_X_c;
2825             }
2826             break;
2827         case AV_PIX_FMT_RGBA64BE:
2828 #if CONFIG_SWSCALE_ALPHA
2829             if (c->needAlpha) {
2830                 *yuv2packed1 = yuv2rgba64be_1_c;
2831                 *yuv2packed2 = yuv2rgba64be_2_c;
2832                 *yuv2packedX = yuv2rgba64be_X_c;
2833             } else
2834 #endif /* CONFIG_SWSCALE_ALPHA */
2835             {
2836                 *yuv2packed1 = yuv2rgbx64be_1_c;
2837                 *yuv2packed2 = yuv2rgbx64be_2_c;
2838                 *yuv2packedX = yuv2rgbx64be_X_c;
2839             }
2840             break;
2841         case AV_PIX_FMT_BGRA64LE:
2842 #if CONFIG_SWSCALE_ALPHA
2843             if (c->needAlpha) {
2844                 *yuv2packed1 = yuv2bgra64le_1_c;
2845                 *yuv2packed2 = yuv2bgra64le_2_c;
2846                 *yuv2packedX = yuv2bgra64le_X_c;
2847             } else
2848 #endif /* CONFIG_SWSCALE_ALPHA */
2849             {
2850                 *yuv2packed1 = yuv2bgrx64le_1_c;
2851                 *yuv2packed2 = yuv2bgrx64le_2_c;
2852                 *yuv2packedX = yuv2bgrx64le_X_c;
2853             }
2854             break;
2855         case AV_PIX_FMT_BGRA64BE:
2856 #if CONFIG_SWSCALE_ALPHA
2857             if (c->needAlpha) {
2858                 *yuv2packed1 = yuv2bgra64be_1_c;
2859                 *yuv2packed2 = yuv2bgra64be_2_c;
2860                 *yuv2packedX = yuv2bgra64be_X_c;
2861             } else
2862 #endif /* CONFIG_SWSCALE_ALPHA */
2863             {
2864                 *yuv2packed1 = yuv2bgrx64be_1_c;
2865                 *yuv2packed2 = yuv2bgrx64be_2_c;
2866                 *yuv2packedX = yuv2bgrx64be_X_c;
2867             }
2868             break;
2869         case AV_PIX_FMT_RGB48LE:
2870             *yuv2packed1 = yuv2rgb48le_1_c;
2871             *yuv2packed2 = yuv2rgb48le_2_c;
2872             *yuv2packedX = yuv2rgb48le_X_c;
2873             break;
2874         case AV_PIX_FMT_RGB48BE:
2875             *yuv2packed1 = yuv2rgb48be_1_c;
2876             *yuv2packed2 = yuv2rgb48be_2_c;
2877             *yuv2packedX = yuv2rgb48be_X_c;
2878             break;
2879         case AV_PIX_FMT_BGR48LE:
2880             *yuv2packed1 = yuv2bgr48le_1_c;
2881             *yuv2packed2 = yuv2bgr48le_2_c;
2882             *yuv2packedX = yuv2bgr48le_X_c;
2883             break;
2884         case AV_PIX_FMT_BGR48BE:
2885             *yuv2packed1 = yuv2bgr48be_1_c;
2886             *yuv2packed2 = yuv2bgr48be_2_c;
2887             *yuv2packedX = yuv2bgr48be_X_c;
2888             break;
2889         case AV_PIX_FMT_RGB32:
2890         case AV_PIX_FMT_BGR32:
2891 #if CONFIG_SMALL
2892             *yuv2packed1 = yuv2rgb32_1_c;
2893             *yuv2packed2 = yuv2rgb32_2_c;
2894             *yuv2packedX = yuv2rgb32_X_c;
2895 #else
2896 #if CONFIG_SWSCALE_ALPHA
2897                 if (c->needAlpha) {
2898                     *yuv2packed1 = yuv2rgba32_1_c;
2899                     *yuv2packed2 = yuv2rgba32_2_c;
2900                     *yuv2packedX = yuv2rgba32_X_c;
2901                 } else
2902 #endif /* CONFIG_SWSCALE_ALPHA */
2903                 {
2904                     *yuv2packed1 = yuv2rgbx32_1_c;
2905                     *yuv2packed2 = yuv2rgbx32_2_c;
2906                     *yuv2packedX = yuv2rgbx32_X_c;
2907                 }
2908 #endif /* !CONFIG_SMALL */
2909             break;
2910         case AV_PIX_FMT_RGB32_1:
2911         case AV_PIX_FMT_BGR32_1:
2912 #if CONFIG_SMALL
2913                 *yuv2packed1 = yuv2rgb32_1_1_c;
2914                 *yuv2packed2 = yuv2rgb32_1_2_c;
2915                 *yuv2packedX = yuv2rgb32_1_X_c;
2916 #else
2917 #if CONFIG_SWSCALE_ALPHA
2918                 if (c->needAlpha) {
2919                     *yuv2packed1 = yuv2rgba32_1_1_c;
2920                     *yuv2packed2 = yuv2rgba32_1_2_c;
2921                     *yuv2packedX = yuv2rgba32_1_X_c;
2922                 } else
2923 #endif /* CONFIG_SWSCALE_ALPHA */
2924                 {
2925                     *yuv2packed1 = yuv2rgbx32_1_1_c;
2926                     *yuv2packed2 = yuv2rgbx32_1_2_c;
2927                     *yuv2packedX = yuv2rgbx32_1_X_c;
2928                 }
2929 #endif /* !CONFIG_SMALL */
2930                 break;
2931         case AV_PIX_FMT_RGB24:
2932             *yuv2packed1 = yuv2rgb24_1_c;
2933             *yuv2packed2 = yuv2rgb24_2_c;
2934             *yuv2packedX = yuv2rgb24_X_c;
2935             break;
2936         case AV_PIX_FMT_BGR24:
2937             *yuv2packed1 = yuv2bgr24_1_c;
2938             *yuv2packed2 = yuv2bgr24_2_c;
2939             *yuv2packedX = yuv2bgr24_X_c;
2940             break;
2941         case AV_PIX_FMT_RGB565LE:
2942         case AV_PIX_FMT_RGB565BE:
2943         case AV_PIX_FMT_BGR565LE:
2944         case AV_PIX_FMT_BGR565BE:
2945             *yuv2packed1 = yuv2rgb16_1_c;
2946             *yuv2packed2 = yuv2rgb16_2_c;
2947             *yuv2packedX = yuv2rgb16_X_c;
2948             break;
2949         case AV_PIX_FMT_RGB555LE:
2950         case AV_PIX_FMT_RGB555BE:
2951         case AV_PIX_FMT_BGR555LE:
2952         case AV_PIX_FMT_BGR555BE:
2953             *yuv2packed1 = yuv2rgb15_1_c;
2954             *yuv2packed2 = yuv2rgb15_2_c;
2955             *yuv2packedX = yuv2rgb15_X_c;
2956             break;
2957         case AV_PIX_FMT_RGB444LE:
2958         case AV_PIX_FMT_RGB444BE:
2959         case AV_PIX_FMT_BGR444LE:
2960         case AV_PIX_FMT_BGR444BE:
2961             *yuv2packed1 = yuv2rgb12_1_c;
2962             *yuv2packed2 = yuv2rgb12_2_c;
2963             *yuv2packedX = yuv2rgb12_X_c;
2964             break;
2965         case AV_PIX_FMT_RGB8:
2966         case AV_PIX_FMT_BGR8:
2967             *yuv2packed1 = yuv2rgb8_1_c;
2968             *yuv2packed2 = yuv2rgb8_2_c;
2969             *yuv2packedX = yuv2rgb8_X_c;
2970             break;
2971         case AV_PIX_FMT_RGB4:
2972         case AV_PIX_FMT_BGR4:
2973             *yuv2packed1 = yuv2rgb4_1_c;
2974             *yuv2packed2 = yuv2rgb4_2_c;
2975             *yuv2packedX = yuv2rgb4_X_c;
2976             break;
2977         case AV_PIX_FMT_RGB4_BYTE:
2978         case AV_PIX_FMT_BGR4_BYTE:
2979             *yuv2packed1 = yuv2rgb4b_1_c;
2980             *yuv2packed2 = yuv2rgb4b_2_c;
2981             *yuv2packedX = yuv2rgb4b_X_c;
2982             break;
2983         case AV_PIX_FMT_X2RGB10LE:
2984         case AV_PIX_FMT_X2RGB10BE:
2985             *yuv2packed1 = yuv2x2rgb10_1_c;
2986             *yuv2packed2 = yuv2x2rgb10_2_c;
2987             *yuv2packedX = yuv2x2rgb10_X_c;
2988             break;
2989         }
2990     }
2991     switch (dstFormat) {
2992     case AV_PIX_FMT_MONOWHITE:
2993         *yuv2packed1 = yuv2monowhite_1_c;
2994         *yuv2packed2 = yuv2monowhite_2_c;
2995         *yuv2packedX = yuv2monowhite_X_c;
2996         break;
2997     case AV_PIX_FMT_MONOBLACK:
2998         *yuv2packed1 = yuv2monoblack_1_c;
2999         *yuv2packed2 = yuv2monoblack_2_c;
3000         *yuv2packedX = yuv2monoblack_X_c;
3001         break;
3002     case AV_PIX_FMT_YUYV422:
3003         *yuv2packed1 = yuv2yuyv422_1_c;
3004         *yuv2packed2 = yuv2yuyv422_2_c;
3005         *yuv2packedX = yuv2yuyv422_X_c;
3006         break;
3007     case AV_PIX_FMT_YVYU422:
3008         *yuv2packed1 = yuv2yvyu422_1_c;
3009         *yuv2packed2 = yuv2yvyu422_2_c;
3010         *yuv2packedX = yuv2yvyu422_X_c;
3011         break;
3012     case AV_PIX_FMT_UYVY422:
3013         *yuv2packed1 = yuv2uyvy422_1_c;
3014         *yuv2packed2 = yuv2uyvy422_2_c;
3015         *yuv2packedX = yuv2uyvy422_X_c;
3016         break;
3017     case AV_PIX_FMT_YA8:
3018         *yuv2packed1 = yuv2ya8_1_c;
3019         *yuv2packed2 = yuv2ya8_2_c;
3020         *yuv2packedX = yuv2ya8_X_c;
3021         break;
3022     case AV_PIX_FMT_YA16LE:
3023         *yuv2packed1 = yuv2ya16le_1_c;
3024         *yuv2packed2 = yuv2ya16le_2_c;
3025         *yuv2packedX = yuv2ya16le_X_c;
3026         break;
3027     case AV_PIX_FMT_YA16BE:
3028         *yuv2packed1 = yuv2ya16be_1_c;
3029         *yuv2packed2 = yuv2ya16be_2_c;
3030         *yuv2packedX = yuv2ya16be_X_c;
3031         break;
3032     case AV_PIX_FMT_AYUV64LE:
3033         *yuv2packedX = yuv2ayuv64le_X_c;
3034         break;
3035     }
3036 }