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