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