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