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