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