]> git.sesse.net Git - ffmpeg/blob - libswscale/output.c
ratecontrol: Use correct function pointer casts instead of void*
[ffmpeg] / libswscale / output.c
1 /*
2  * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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/bswap.h"
29 #include "libavutil/cpu.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/pixdesc.h"
33 #include "config.h"
34 #include "rgb2rgb.h"
35 #include "swscale.h"
36 #include "swscale_internal.h"
37
38 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
39 {  1,   3,   1,   3,   1,   3,   1,   3, },
40 {  2,   0,   2,   0,   2,   0,   2,   0, },
41 };
42
43 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
44 {  6,   2,   6,   2,   6,   2,   6,   2, },
45 {  0,   4,   0,   4,   0,   4,   0,   4, },
46 };
47
48 DECLARE_ALIGNED(8, const uint8_t, ff_dither_4x4_16)[4][8] = {
49 {  8,   4,  11,   7,   8,   4,  11,   7, },
50 {  2,  14,   1,  13,   2,  14,   1,  13, },
51 { 10,   6,   9,   5,  10,   6,   9,   5, },
52 {  0,  12,   3,  15,   0,  12,   3,  15, },
53 };
54
55 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_32)[8][8] = {
56 { 17,   9,  23,  15,  16,   8,  22,  14, },
57 {  5,  29,   3,  27,   4,  28,   2,  26, },
58 { 21,  13,  19,  11,  20,  12,  18,  10, },
59 {  0,  24,   6,  30,   1,  25,   7,  31, },
60 { 16,   8,  22,  14,  17,   9,  23,  15, },
61 {  4,  28,   2,  26,   5,  29,   3,  27, },
62 { 20,  12,  18,  10,  21,  13,  19,  11, },
63 {  1,  25,   7,  31,   0,  24,   6,  30, },
64 };
65
66 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_73)[8][8] = {
67 {  0,  55,  14,  68,   3,  58,  17,  72, },
68 { 37,  18,  50,  32,  40,  22,  54,  35, },
69 {  9,  64,   5,  59,  13,  67,   8,  63, },
70 { 46,  27,  41,  23,  49,  31,  44,  26, },
71 {  2,  57,  16,  71,   1,  56,  15,  70, },
72 { 39,  21,  52,  34,  38,  19,  51,  33, },
73 { 11,  66,   7,  62,  10,  65,   6,  60, },
74 { 48,  30,  43,  25,  47,  29,  42,  24, },
75 };
76
77 #if 1
78 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
79 {117,  62, 158, 103, 113,  58, 155, 100, },
80 { 34, 199,  21, 186,  31, 196,  17, 182, },
81 {144,  89, 131,  76, 141,  86, 127,  72, },
82 {  0, 165,  41, 206,  10, 175,  52, 217, },
83 {110,  55, 151,  96, 120,  65, 162, 107, },
84 { 28, 193,  14, 179,  38, 203,  24, 189, },
85 {138,  83, 124,  69, 148,  93, 134,  79, },
86 {  7, 172,  48, 213,   3, 168,  45, 210, },
87 };
88 #elif 1
89 // tries to correct a gamma of 1.5
90 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
91 {  0, 143,  18, 200,   2, 156,  25, 215, },
92 { 78,  28, 125,  64,  89,  36, 138,  74, },
93 { 10, 180,   3, 161,  16, 195,   8, 175, },
94 {109,  51,  93,  38, 121,  60, 105,  47, },
95 {  1, 152,  23, 210,   0, 147,  20, 205, },
96 { 85,  33, 134,  71,  81,  30, 130,  67, },
97 { 14, 190,   6, 171,  12, 185,   5, 166, },
98 {117,  57, 101,  44, 113,  54,  97,  41, },
99 };
100 #elif 1
101 // tries to correct a gamma of 2.0
102 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
103 {  0, 124,   8, 193,   0, 140,  12, 213, },
104 { 55,  14, 104,  42,  66,  19, 119,  52, },
105 {  3, 168,   1, 145,   6, 187,   3, 162, },
106 { 86,  31,  70,  21,  99,  39,  82,  28, },
107 {  0, 134,  11, 206,   0, 129,   9, 200, },
108 { 62,  17, 114,  48,  58,  16, 109,  45, },
109 {  5, 181,   2, 157,   4, 175,   1, 151, },
110 { 95,  36,  78,  26,  90,  34,  74,  24, },
111 };
112 #else
113 // tries to correct a gamma of 2.5
114 DECLARE_ALIGNED(8, const uint8_t, ff_dither_8x8_220)[8][8] = {
115 {  0, 107,   3, 187,   0, 125,   6, 212, },
116 { 39,   7,  86,  28,  49,  11, 102,  36, },
117 {  1, 158,   0, 131,   3, 180,   1, 151, },
118 { 68,  19,  52,  12,  81,  25,  64,  17, },
119 {  0, 119,   5, 203,   0, 113,   4, 195, },
120 { 45,   9,  96,  33,  42,   8,  91,  30, },
121 {  2, 172,   1, 144,   2, 165,   0, 137, },
122 { 77,  23,  60,  15,  72,  21,  56,  14, },
123 };
124 #endif
125
126 #define output_pixel(pos, val, bias, signedness) \
127     if (big_endian) { \
128         AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
129     } else { \
130         AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
131     }
132
133 static av_always_inline void
134 yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
135                          int big_endian, int output_bits)
136 {
137     int i;
138     int shift = 19 - output_bits;
139
140     for (i = 0; i < dstW; i++) {
141         int val = src[i] + (1 << (shift - 1));
142         output_pixel(&dest[i], val, 0, uint);
143     }
144 }
145
146 static av_always_inline void
147 yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
148                          const int32_t **src, uint16_t *dest, int dstW,
149                          int big_endian, int output_bits)
150 {
151     int i;
152     int shift = 15 + 16 - output_bits;
153
154     for (i = 0; i < dstW; i++) {
155         int val = 1 << (30-output_bits);
156         int j;
157
158         /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
159          * filters (or anything with negative coeffs, the range can be slightly
160          * wider in both directions. To account for this overflow, we subtract
161          * a constant so it always fits in the signed range (assuming a
162          * reasonable filterSize), and re-add that at the end. */
163         val -= 0x40000000;
164         for (j = 0; j < filterSize; j++)
165             val += src[j][i] * filter[j];
166
167         output_pixel(&dest[i], val, 0x8000, int);
168     }
169 }
170
171 #undef output_pixel
172
173 #define output_pixel(pos, val) \
174     if (big_endian) { \
175         AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
176     } else { \
177         AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
178     }
179
180 static av_always_inline void
181 yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
182                          int big_endian, int output_bits)
183 {
184     int i;
185     int shift = 15 - output_bits;
186
187     for (i = 0; i < dstW; i++) {
188         int val = src[i] + (1 << (shift - 1));
189         output_pixel(&dest[i], val);
190     }
191 }
192
193 static av_always_inline void
194 yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
195                          const int16_t **src, uint16_t *dest, int dstW,
196                          int big_endian, int output_bits)
197 {
198     int i;
199     int shift = 11 + 16 - output_bits;
200
201     for (i = 0; i < dstW; i++) {
202         int val = 1 << (26-output_bits);
203         int j;
204
205         for (j = 0; j < filterSize; j++)
206             val += src[j][i] * filter[j];
207
208         output_pixel(&dest[i], val);
209     }
210 }
211
212 #undef output_pixel
213
214 #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
215 static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
216                               uint8_t *dest, int dstW, \
217                               const uint8_t *dither, int offset)\
218 { \
219     yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
220                          (uint16_t *) dest, dstW, is_be, bits); \
221 }\
222 static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
223                               const int16_t **src, uint8_t *dest, int dstW, \
224                               const uint8_t *dither, int offset)\
225 { \
226     yuv2planeX_## template_size ## _c_template(filter, \
227                          filterSize, (const typeX_t **) src, \
228                          (uint16_t *) dest, dstW, is_be, bits); \
229 }
230 yuv2NBPS( 9, BE, 1, 10, int16_t)
231 yuv2NBPS( 9, LE, 0, 10, int16_t)
232 yuv2NBPS(10, BE, 1, 10, int16_t)
233 yuv2NBPS(10, LE, 0, 10, int16_t)
234 yuv2NBPS(12, BE, 1, 10, int16_t)
235 yuv2NBPS(12, LE, 0, 10, int16_t)
236 yuv2NBPS(16, BE, 1, 16, int32_t)
237 yuv2NBPS(16, LE, 0, 16, int32_t)
238
239 static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
240                            const int16_t **src, uint8_t *dest, int dstW,
241                            const uint8_t *dither, int offset)
242 {
243     int i;
244     for (i=0; i<dstW; i++) {
245         int val = dither[(i + offset) & 7] << 12;
246         int j;
247         for (j=0; j<filterSize; j++)
248             val += src[j][i] * filter[j];
249
250         dest[i]= av_clip_uint8(val>>19);
251     }
252 }
253
254 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
255                            const uint8_t *dither, int offset)
256 {
257     int i;
258     for (i=0; i<dstW; i++) {
259         int val = (src[i] + dither[(i + offset) & 7]) >> 7;
260         dest[i]= av_clip_uint8(val);
261     }
262 }
263
264 static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
265                         const int16_t **chrUSrc, const int16_t **chrVSrc,
266                         uint8_t *dest, int chrDstW)
267 {
268     enum AVPixelFormat dstFormat = c->dstFormat;
269     const uint8_t *chrDither = c->chrDither8;
270     int i;
271
272     if (dstFormat == AV_PIX_FMT_NV12)
273         for (i=0; i<chrDstW; i++) {
274             int u = chrDither[i & 7] << 12;
275             int v = chrDither[(i + 3) & 7] << 12;
276             int j;
277             for (j=0; j<chrFilterSize; j++) {
278                 u += chrUSrc[j][i] * chrFilter[j];
279                 v += chrVSrc[j][i] * chrFilter[j];
280             }
281
282             dest[2*i]= av_clip_uint8(u>>19);
283             dest[2*i+1]= av_clip_uint8(v>>19);
284         }
285     else
286         for (i=0; i<chrDstW; i++) {
287             int u = chrDither[i & 7] << 12;
288             int v = chrDither[(i + 3) & 7] << 12;
289             int j;
290             for (j=0; j<chrFilterSize; j++) {
291                 u += chrUSrc[j][i] * chrFilter[j];
292                 v += chrVSrc[j][i] * chrFilter[j];
293             }
294
295             dest[2*i]= av_clip_uint8(v>>19);
296             dest[2*i+1]= av_clip_uint8(u>>19);
297         }
298 }
299
300 #define accumulate_bit(acc, val) \
301     acc <<= 1; \
302     acc |= (val) >= (128 + 110)
303 #define output_pixel(pos, acc) \
304     if (target == AV_PIX_FMT_MONOBLACK) { \
305         pos = acc; \
306     } else { \
307         pos = ~acc; \
308     }
309
310 static av_always_inline void
311 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
312                       const int16_t **lumSrc, int lumFilterSize,
313                       const int16_t *chrFilter, const int16_t **chrUSrc,
314                       const int16_t **chrVSrc, int chrFilterSize,
315                       const int16_t **alpSrc, uint8_t *dest, int dstW,
316                       int y, enum AVPixelFormat target)
317 {
318     const uint8_t * const d128 = ff_dither_8x8_220[y&7];
319     int i;
320     unsigned acc = 0;
321
322     for (i = 0; i < dstW; i += 2) {
323         int j;
324         int Y1 = 1 << 18;
325         int Y2 = 1 << 18;
326
327         for (j = 0; j < lumFilterSize; j++) {
328             Y1 += lumSrc[j][i]   * lumFilter[j];
329             Y2 += lumSrc[j][i+1] * lumFilter[j];
330         }
331         Y1 >>= 19;
332         Y2 >>= 19;
333         if ((Y1 | Y2) & 0x100) {
334             Y1 = av_clip_uint8(Y1);
335             Y2 = av_clip_uint8(Y2);
336         }
337         accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
338         accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
339         if ((i & 7) == 6) {
340             output_pixel(*dest++, acc);
341         }
342     }
343
344     if (i & 6) {
345         output_pixel(*dest, acc);
346     }
347 }
348
349 static av_always_inline void
350 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
351                       const int16_t *ubuf[2], const int16_t *vbuf[2],
352                       const int16_t *abuf[2], uint8_t *dest, int dstW,
353                       int yalpha, int uvalpha, int y,
354                       enum AVPixelFormat target)
355 {
356     const int16_t *buf0  = buf[0],  *buf1  = buf[1];
357     const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
358     int  yalpha1 = 4096 - yalpha;
359     int i;
360
361     for (i = 0; i < dstW; i += 8) {
362         int Y, acc = 0;
363
364         Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
365         accumulate_bit(acc, Y + d128[0]);
366         Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
367         accumulate_bit(acc, Y + d128[1]);
368         Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
369         accumulate_bit(acc, Y + d128[2]);
370         Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
371         accumulate_bit(acc, Y + d128[3]);
372         Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
373         accumulate_bit(acc, Y + d128[4]);
374         Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
375         accumulate_bit(acc, Y + d128[5]);
376         Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
377         accumulate_bit(acc, Y + d128[6]);
378         Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
379         accumulate_bit(acc, Y + d128[7]);
380
381         output_pixel(*dest++, acc);
382     }
383 }
384
385 static av_always_inline void
386 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
387                       const int16_t *ubuf[2], const int16_t *vbuf[2],
388                       const int16_t *abuf0, uint8_t *dest, int dstW,
389                       int uvalpha, int y, enum AVPixelFormat target)
390 {
391     const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
392     int i;
393
394     for (i = 0; i < dstW; i += 8) {
395         int acc = 0;
396
397         accumulate_bit(acc, (buf0[i + 0] >> 7) + d128[0]);
398         accumulate_bit(acc, (buf0[i + 1] >> 7) + d128[1]);
399         accumulate_bit(acc, (buf0[i + 2] >> 7) + d128[2]);
400         accumulate_bit(acc, (buf0[i + 3] >> 7) + d128[3]);
401         accumulate_bit(acc, (buf0[i + 4] >> 7) + d128[4]);
402         accumulate_bit(acc, (buf0[i + 5] >> 7) + d128[5]);
403         accumulate_bit(acc, (buf0[i + 6] >> 7) + d128[6]);
404         accumulate_bit(acc, (buf0[i + 7] >> 7) + d128[7]);
405
406         output_pixel(*dest++, acc);
407     }
408 }
409
410 #undef output_pixel
411 #undef accumulate_bit
412
413 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
414 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
415                                 const int16_t **lumSrc, int lumFilterSize, \
416                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
417                                 const int16_t **chrVSrc, int chrFilterSize, \
418                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
419                                 int y) \
420 { \
421     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
422                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
423                                   alpSrc, dest, dstW, y, fmt); \
424 } \
425  \
426 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
427                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
428                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
429                                 int yalpha, int uvalpha, int y) \
430 { \
431     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
432                                   dest, dstW, yalpha, uvalpha, y, fmt); \
433 } \
434  \
435 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
436                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
437                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
438                                 int uvalpha, int y) \
439 { \
440     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
441                                   abuf0, dest, dstW, uvalpha, \
442                                   y, fmt); \
443 }
444
445 YUV2PACKEDWRAPPER(yuv2mono,, white, AV_PIX_FMT_MONOWHITE)
446 YUV2PACKEDWRAPPER(yuv2mono,, black, AV_PIX_FMT_MONOBLACK)
447
448 #define output_pixels(pos, Y1, U, Y2, V) \
449     if (target == AV_PIX_FMT_YUYV422) { \
450         dest[pos + 0] = Y1; \
451         dest[pos + 1] = U;  \
452         dest[pos + 2] = Y2; \
453         dest[pos + 3] = V;  \
454     } else if (target == AV_PIX_FMT_YVYU422) { \
455         dest[pos + 0] = Y1; \
456         dest[pos + 1] = V;  \
457         dest[pos + 2] = Y2; \
458         dest[pos + 3] = U;  \
459     } else { /* AV_PIX_FMT_UYVY422 */ \
460         dest[pos + 0] = U;  \
461         dest[pos + 1] = Y1; \
462         dest[pos + 2] = V;  \
463         dest[pos + 3] = Y2; \
464     }
465
466 static av_always_inline void
467 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
468                      const int16_t **lumSrc, int lumFilterSize,
469                      const int16_t *chrFilter, const int16_t **chrUSrc,
470                      const int16_t **chrVSrc, int chrFilterSize,
471                      const int16_t **alpSrc, uint8_t *dest, int dstW,
472                      int y, enum AVPixelFormat target)
473 {
474     int i;
475
476     for (i = 0; i < ((dstW + 1) >> 1); i++) {
477         int j;
478         int Y1 = 1 << 18;
479         int Y2 = 1 << 18;
480         int U  = 1 << 18;
481         int V  = 1 << 18;
482
483         for (j = 0; j < lumFilterSize; j++) {
484             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
485             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
486         }
487         for (j = 0; j < chrFilterSize; j++) {
488             U += chrUSrc[j][i] * chrFilter[j];
489             V += chrVSrc[j][i] * chrFilter[j];
490         }
491         Y1 >>= 19;
492         Y2 >>= 19;
493         U  >>= 19;
494         V  >>= 19;
495         if ((Y1 | Y2 | U | V) & 0x100) {
496             Y1 = av_clip_uint8(Y1);
497             Y2 = av_clip_uint8(Y2);
498             U  = av_clip_uint8(U);
499             V  = av_clip_uint8(V);
500         }
501         output_pixels(4*i, Y1, U, Y2, V);
502     }
503 }
504
505 static av_always_inline void
506 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
507                      const int16_t *ubuf[2], const int16_t *vbuf[2],
508                      const int16_t *abuf[2], uint8_t *dest, int dstW,
509                      int yalpha, int uvalpha, int y,
510                      enum AVPixelFormat target)
511 {
512     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
513                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
514                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
515     int  yalpha1 = 4096 - yalpha;
516     int uvalpha1 = 4096 - uvalpha;
517     int i;
518
519     for (i = 0; i < ((dstW + 1) >> 1); i++) {
520         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
521         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
522         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
523         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
524
525         Y1 = av_clip_uint8(Y1);
526         Y2 = av_clip_uint8(Y2);
527         U  = av_clip_uint8(U);
528         V  = av_clip_uint8(V);
529
530         output_pixels(i * 4, Y1, U, Y2, V);
531     }
532 }
533
534 static av_always_inline void
535 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
536                      const int16_t *ubuf[2], const int16_t *vbuf[2],
537                      const int16_t *abuf0, uint8_t *dest, int dstW,
538                      int uvalpha, int y, enum AVPixelFormat target)
539 {
540     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
541     int i;
542
543     if (uvalpha < 2048) {
544         for (i = 0; i < ((dstW + 1) >> 1); i++) {
545             int Y1 = buf0[i * 2]     >> 7;
546             int Y2 = buf0[i * 2 + 1] >> 7;
547             int U  = ubuf0[i]        >> 7;
548             int V  = vbuf0[i]        >> 7;
549
550             Y1 = av_clip_uint8(Y1);
551             Y2 = av_clip_uint8(Y2);
552             U  = av_clip_uint8(U);
553             V  = av_clip_uint8(V);
554
555             output_pixels(i * 4, Y1, U, Y2, V);
556         }
557     } else {
558         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
559         for (i = 0; i < ((dstW + 1) >> 1); i++) {
560             int Y1 =  buf0[i * 2]          >> 7;
561             int Y2 =  buf0[i * 2 + 1]      >> 7;
562             int U  = (ubuf0[i] + ubuf1[i]) >> 8;
563             int V  = (vbuf0[i] + vbuf1[i]) >> 8;
564
565             Y1 = av_clip_uint8(Y1);
566             Y2 = av_clip_uint8(Y2);
567             U  = av_clip_uint8(U);
568             V  = av_clip_uint8(V);
569
570             output_pixels(i * 4, Y1, U, Y2, V);
571         }
572     }
573 }
574
575 #undef output_pixels
576
577 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, AV_PIX_FMT_YUYV422)
578 YUV2PACKEDWRAPPER(yuv2, 422, yvyu422, AV_PIX_FMT_YVYU422)
579 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, AV_PIX_FMT_UYVY422)
580
581 #define R_B ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? R : B)
582 #define B_R ((target == AV_PIX_FMT_RGB48LE || target == AV_PIX_FMT_RGB48BE) ? B : R)
583 #define output_pixel(pos, val) \
584     if (isBE(target)) { \
585         AV_WB16(pos, val); \
586     } else { \
587         AV_WL16(pos, val); \
588     }
589
590 static av_always_inline void
591 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
592                        const int32_t **lumSrc, int lumFilterSize,
593                        const int16_t *chrFilter, const int32_t **chrUSrc,
594                        const int32_t **chrVSrc, int chrFilterSize,
595                        const int32_t **alpSrc, uint16_t *dest, int dstW,
596                        int y, enum AVPixelFormat target)
597 {
598     int i;
599
600     for (i = 0; i < ((dstW + 1) >> 1); i++) {
601         int j;
602         int Y1 = -0x40000000;
603         int Y2 = -0x40000000;
604         int U  = -128 << 23; // 19
605         int V  = -128 << 23;
606         int R, G, B;
607
608         for (j = 0; j < lumFilterSize; j++) {
609             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
610             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
611         }
612         for (j = 0; j < chrFilterSize; j++) {
613             U += chrUSrc[j][i] * chrFilter[j];
614             V += chrVSrc[j][i] * chrFilter[j];
615         }
616
617         // 8 bits: 12+15=27; 16 bits: 12+19=31
618         Y1 >>= 14; // 10
619         Y1 += 0x10000;
620         Y2 >>= 14;
621         Y2 += 0x10000;
622         U  >>= 14;
623         V  >>= 14;
624
625         // 8 bits: 27 -> 17 bits, 16 bits: 31 - 14 = 17 bits
626         Y1 -= c->yuv2rgb_y_offset;
627         Y2 -= c->yuv2rgb_y_offset;
628         Y1 *= c->yuv2rgb_y_coeff;
629         Y2 *= c->yuv2rgb_y_coeff;
630         Y1 += 1 << 13; // 21
631         Y2 += 1 << 13;
632         // 8 bits: 17 + 13 bits = 30 bits, 16 bits: 17 + 13 bits = 30 bits
633
634         R = V * c->yuv2rgb_v2r_coeff;
635         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
636         B =                            U * c->yuv2rgb_u2b_coeff;
637
638         // 8 bits: 30 - 22 = 8 bits, 16 bits: 30 bits - 14 = 16 bits
639         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
640         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
641         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
642         output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
643         output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
644         output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
645         dest += 6;
646     }
647 }
648
649 static av_always_inline void
650 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
651                        const int32_t *ubuf[2], const int32_t *vbuf[2],
652                        const int32_t *abuf[2], uint16_t *dest, int dstW,
653                        int yalpha, int uvalpha, int y,
654                        enum AVPixelFormat target)
655 {
656     const int32_t *buf0  = buf[0],  *buf1  = buf[1],
657                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
658                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
659     int  yalpha1 = 4096 - yalpha;
660     int uvalpha1 = 4096 - uvalpha;
661     int i;
662
663     for (i = 0; i < ((dstW + 1) >> 1); i++) {
664         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha) >> 14;
665         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha) >> 14;
666         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha + (-128 << 23)) >> 14;
667         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha + (-128 << 23)) >> 14;
668         int R, G, B;
669
670         Y1 -= c->yuv2rgb_y_offset;
671         Y2 -= c->yuv2rgb_y_offset;
672         Y1 *= c->yuv2rgb_y_coeff;
673         Y2 *= c->yuv2rgb_y_coeff;
674         Y1 += 1 << 13;
675         Y2 += 1 << 13;
676
677         R = V * c->yuv2rgb_v2r_coeff;
678         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
679         B =                            U * c->yuv2rgb_u2b_coeff;
680
681         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
682         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
683         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
684         output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
685         output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
686         output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
687         dest += 6;
688     }
689 }
690
691 static av_always_inline void
692 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
693                        const int32_t *ubuf[2], const int32_t *vbuf[2],
694                        const int32_t *abuf0, uint16_t *dest, int dstW,
695                        int uvalpha, int y, enum AVPixelFormat target)
696 {
697     const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
698     int i;
699
700     if (uvalpha < 2048) {
701         for (i = 0; i < ((dstW + 1) >> 1); i++) {
702             int Y1 = (buf0[i * 2]    ) >> 2;
703             int Y2 = (buf0[i * 2 + 1]) >> 2;
704             int U  = (ubuf0[i] + (-128 << 11)) >> 2;
705             int V  = (vbuf0[i] + (-128 << 11)) >> 2;
706             int R, G, B;
707
708             Y1 -= c->yuv2rgb_y_offset;
709             Y2 -= c->yuv2rgb_y_offset;
710             Y1 *= c->yuv2rgb_y_coeff;
711             Y2 *= c->yuv2rgb_y_coeff;
712             Y1 += 1 << 13;
713             Y2 += 1 << 13;
714
715             R = V * c->yuv2rgb_v2r_coeff;
716             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
717             B =                            U * c->yuv2rgb_u2b_coeff;
718
719             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
720             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
721             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
722             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
723             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
724             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
725             dest += 6;
726         }
727     } else {
728         const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
729         for (i = 0; i < ((dstW + 1) >> 1); i++) {
730             int Y1 = (buf0[i * 2]    ) >> 2;
731             int Y2 = (buf0[i * 2 + 1]) >> 2;
732             int U  = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
733             int V  = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
734             int R, G, B;
735
736             Y1 -= c->yuv2rgb_y_offset;
737             Y2 -= c->yuv2rgb_y_offset;
738             Y1 *= c->yuv2rgb_y_coeff;
739             Y2 *= c->yuv2rgb_y_coeff;
740             Y1 += 1 << 13;
741             Y2 += 1 << 13;
742
743             R = V * c->yuv2rgb_v2r_coeff;
744             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
745             B =                            U * c->yuv2rgb_u2b_coeff;
746
747             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
748             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
749             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
750             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
751             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
752             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
753             dest += 6;
754         }
755     }
756 }
757
758 #undef output_pixel
759 #undef r_b
760 #undef b_r
761
762 #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
763 static void name ## ext ## _X_c(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) \
769 { \
770     const int32_t **lumSrc  = (const int32_t **) _lumSrc, \
771                   **chrUSrc = (const int32_t **) _chrUSrc, \
772                   **chrVSrc = (const int32_t **) _chrVSrc, \
773                   **alpSrc  = (const int32_t **) _alpSrc; \
774     uint16_t *dest = (uint16_t *) _dest; \
775     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
776                           chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
777                           alpSrc, dest, dstW, y, fmt); \
778 } \
779  \
780 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
781                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
782                         const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
783                         int yalpha, int uvalpha, int y) \
784 { \
785     const int32_t **buf  = (const int32_t **) _buf, \
786                   **ubuf = (const int32_t **) _ubuf, \
787                   **vbuf = (const int32_t **) _vbuf, \
788                   **abuf = (const int32_t **) _abuf; \
789     uint16_t *dest = (uint16_t *) _dest; \
790     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
791                           dest, dstW, yalpha, uvalpha, y, fmt); \
792 } \
793  \
794 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
795                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
796                         const int16_t *_abuf0, uint8_t *_dest, int dstW, \
797                         int uvalpha, int y) \
798 { \
799     const int32_t *buf0  = (const int32_t *)  _buf0, \
800                  **ubuf  = (const int32_t **) _ubuf, \
801                  **vbuf  = (const int32_t **) _vbuf, \
802                   *abuf0 = (const int32_t *)  _abuf0; \
803     uint16_t *dest = (uint16_t *) _dest; \
804     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
805                                   dstW, uvalpha, y, fmt); \
806 }
807
808 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, AV_PIX_FMT_RGB48BE)
809 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, AV_PIX_FMT_RGB48LE)
810 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, AV_PIX_FMT_BGR48BE)
811 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, AV_PIX_FMT_BGR48LE)
812
813 /*
814  * Write out 2 RGB pixels in the target pixel format. This function takes a
815  * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
816  * things like endianness conversion and shifting. The caller takes care of
817  * setting the correct offset in these tables from the chroma (U/V) values.
818  * This function then uses the luminance (Y1/Y2) values to write out the
819  * correct RGB values into the destination buffer.
820  */
821 static av_always_inline void
822 yuv2rgb_write(uint8_t *_dest, int i, unsigned Y1, unsigned Y2,
823               unsigned A1, unsigned A2,
824               const void *_r, const void *_g, const void *_b, int y,
825               enum AVPixelFormat target, int hasAlpha)
826 {
827     if (target == AV_PIX_FMT_ARGB || target == AV_PIX_FMT_RGBA ||
828         target == AV_PIX_FMT_ABGR || target == AV_PIX_FMT_BGRA) {
829         uint32_t *dest = (uint32_t *) _dest;
830         const uint32_t *r = (const uint32_t *) _r;
831         const uint32_t *g = (const uint32_t *) _g;
832         const uint32_t *b = (const uint32_t *) _b;
833
834 #if CONFIG_SMALL
835         int sh = hasAlpha ? ((target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24) : 0;
836
837         dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
838         dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
839 #else
840         if (hasAlpha) {
841             int sh = (target == AV_PIX_FMT_RGB32_1 || target == AV_PIX_FMT_BGR32_1) ? 0 : 24;
842
843             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
844             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
845         } else {
846             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
847             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
848         }
849 #endif
850     } else if (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) {
851         uint8_t *dest = (uint8_t *) _dest;
852         const uint8_t *r = (const uint8_t *) _r;
853         const uint8_t *g = (const uint8_t *) _g;
854         const uint8_t *b = (const uint8_t *) _b;
855
856 #define r_b ((target == AV_PIX_FMT_RGB24) ? r : b)
857 #define b_r ((target == AV_PIX_FMT_RGB24) ? b : r)
858         dest[i * 6 + 0] = r_b[Y1];
859         dest[i * 6 + 1] =   g[Y1];
860         dest[i * 6 + 2] = b_r[Y1];
861         dest[i * 6 + 3] = r_b[Y2];
862         dest[i * 6 + 4] =   g[Y2];
863         dest[i * 6 + 5] = b_r[Y2];
864 #undef r_b
865 #undef b_r
866     } else if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565 ||
867                target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555 ||
868                target == AV_PIX_FMT_RGB444 || target == AV_PIX_FMT_BGR444) {
869         uint16_t *dest = (uint16_t *) _dest;
870         const uint16_t *r = (const uint16_t *) _r;
871         const uint16_t *g = (const uint16_t *) _g;
872         const uint16_t *b = (const uint16_t *) _b;
873         int dr1, dg1, db1, dr2, dg2, db2;
874
875         if (target == AV_PIX_FMT_RGB565 || target == AV_PIX_FMT_BGR565) {
876             dr1 = dither_2x2_8[ y & 1     ][0];
877             dg1 = dither_2x2_4[ y & 1     ][0];
878             db1 = dither_2x2_8[(y & 1) ^ 1][0];
879             dr2 = dither_2x2_8[ y & 1     ][1];
880             dg2 = dither_2x2_4[ y & 1     ][1];
881             db2 = dither_2x2_8[(y & 1) ^ 1][1];
882         } else if (target == AV_PIX_FMT_RGB555 || target == AV_PIX_FMT_BGR555) {
883             dr1 = dither_2x2_8[ y & 1     ][0];
884             dg1 = dither_2x2_8[ y & 1     ][1];
885             db1 = dither_2x2_8[(y & 1) ^ 1][0];
886             dr2 = dither_2x2_8[ y & 1     ][1];
887             dg2 = dither_2x2_8[ y & 1     ][0];
888             db2 = dither_2x2_8[(y & 1) ^ 1][1];
889         } else {
890             dr1 = ff_dither_4x4_16[ y & 3     ][0];
891             dg1 = ff_dither_4x4_16[ y & 3     ][1];
892             db1 = ff_dither_4x4_16[(y & 3) ^ 3][0];
893             dr2 = ff_dither_4x4_16[ y & 3     ][1];
894             dg2 = ff_dither_4x4_16[ y & 3     ][0];
895             db2 = ff_dither_4x4_16[(y & 3) ^ 3][1];
896         }
897
898         dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
899         dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
900     } else /* 8/4 bits */ {
901         uint8_t *dest = (uint8_t *) _dest;
902         const uint8_t *r = (const uint8_t *) _r;
903         const uint8_t *g = (const uint8_t *) _g;
904         const uint8_t *b = (const uint8_t *) _b;
905         int dr1, dg1, db1, dr2, dg2, db2;
906
907         if (target == AV_PIX_FMT_RGB8 || target == AV_PIX_FMT_BGR8) {
908             const uint8_t * const d64 = ff_dither_8x8_73[y & 7];
909             const uint8_t * const d32 = ff_dither_8x8_32[y & 7];
910             dr1 = dg1 = d32[(i * 2 + 0) & 7];
911             db1 =       d64[(i * 2 + 0) & 7];
912             dr2 = dg2 = d32[(i * 2 + 1) & 7];
913             db2 =       d64[(i * 2 + 1) & 7];
914         } else {
915             const uint8_t * const d64  = ff_dither_8x8_73 [y & 7];
916             const uint8_t * const d128 = ff_dither_8x8_220[y & 7];
917             dr1 = db1 = d128[(i * 2 + 0) & 7];
918             dg1 =        d64[(i * 2 + 0) & 7];
919             dr2 = db2 = d128[(i * 2 + 1) & 7];
920             dg2 =        d64[(i * 2 + 1) & 7];
921         }
922
923         if (target == AV_PIX_FMT_RGB4 || target == AV_PIX_FMT_BGR4) {
924             dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
925                     ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
926         } else {
927             dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
928             dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
929         }
930     }
931 }
932
933 static av_always_inline void
934 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
935                      const int16_t **lumSrc, int lumFilterSize,
936                      const int16_t *chrFilter, const int16_t **chrUSrc,
937                      const int16_t **chrVSrc, int chrFilterSize,
938                      const int16_t **alpSrc, uint8_t *dest, int dstW,
939                      int y, enum AVPixelFormat target, int hasAlpha)
940 {
941     int i;
942
943     for (i = 0; i < ((dstW + 1) >> 1); i++) {
944         int j, A1, A2;
945         int Y1 = 1 << 18;
946         int Y2 = 1 << 18;
947         int U  = 1 << 18;
948         int V  = 1 << 18;
949         const void *r, *g, *b;
950
951         for (j = 0; j < lumFilterSize; j++) {
952             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
953             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
954         }
955         for (j = 0; j < chrFilterSize; j++) {
956             U += chrUSrc[j][i] * chrFilter[j];
957             V += chrVSrc[j][i] * chrFilter[j];
958         }
959         Y1 >>= 19;
960         Y2 >>= 19;
961         U  >>= 19;
962         V  >>= 19;
963         if ((Y1 | Y2 | U | V) & 0x100) {
964             Y1 = av_clip_uint8(Y1);
965             Y2 = av_clip_uint8(Y2);
966             U  = av_clip_uint8(U);
967             V  = av_clip_uint8(V);
968         }
969         if (hasAlpha) {
970             A1 = 1 << 18;
971             A2 = 1 << 18;
972             for (j = 0; j < lumFilterSize; j++) {
973                 A1 += alpSrc[j][i * 2    ] * lumFilter[j];
974                 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
975             }
976             A1 >>= 19;
977             A2 >>= 19;
978             if ((A1 | A2) & 0x100) {
979                 A1 = av_clip_uint8(A1);
980                 A2 = av_clip_uint8(A2);
981             }
982         }
983
984         /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
985         r =  c->table_rV[V];
986         g = (c->table_gU[U] + c->table_gV[V]);
987         b =  c->table_bU[U];
988
989         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
990                       r, g, b, y, target, hasAlpha);
991     }
992 }
993
994 static av_always_inline void
995 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
996                      const int16_t *ubuf[2], const int16_t *vbuf[2],
997                      const int16_t *abuf[2], uint8_t *dest, int dstW,
998                      int yalpha, int uvalpha, int y,
999                      enum AVPixelFormat target, int hasAlpha)
1000 {
1001     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
1002                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
1003                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
1004                   *abuf0 = hasAlpha ? abuf[0] : NULL,
1005                   *abuf1 = hasAlpha ? abuf[1] : NULL;
1006     int  yalpha1 = 4096 - yalpha;
1007     int uvalpha1 = 4096 - uvalpha;
1008     int i;
1009
1010     for (i = 0; i < ((dstW + 1) >> 1); i++) {
1011         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
1012         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
1013         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
1014         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
1015         int A1, A2;
1016         const void *r, *g, *b;
1017
1018         Y1 = av_clip_uint8(Y1);
1019         Y2 = av_clip_uint8(Y2);
1020         U  = av_clip_uint8(U);
1021         V  = av_clip_uint8(V);
1022
1023         r =  c->table_rV[V];
1024         g = (c->table_gU[U] + c->table_gV[V]);
1025         b =  c->table_bU[U];
1026
1027         if (hasAlpha) {
1028             A1 = (abuf0[i * 2    ] * yalpha1 + abuf1[i * 2    ] * yalpha) >> 19;
1029             A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1030             A1 = av_clip_uint8(A1);
1031             A2 = av_clip_uint8(A2);
1032         }
1033
1034         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1035                       r, g, b, y, target, hasAlpha);
1036     }
1037 }
1038
1039 static av_always_inline void
1040 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1041                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1042                      const int16_t *abuf0, uint8_t *dest, int dstW,
1043                      int uvalpha, int y, enum AVPixelFormat target,
1044                      int hasAlpha)
1045 {
1046     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1047     int i;
1048
1049     if (uvalpha < 2048) {
1050         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1051             int Y1 = buf0[i * 2]     >> 7;
1052             int Y2 = buf0[i * 2 + 1] >> 7;
1053             int U  = ubuf0[i]        >> 7;
1054             int V  = vbuf0[i]        >> 7;
1055             int A1, A2;
1056             const void *r, *g, *b;
1057
1058             Y1 = av_clip_uint8(Y1);
1059             Y2 = av_clip_uint8(Y2);
1060             U  = av_clip_uint8(U);
1061             V  = av_clip_uint8(V);
1062
1063             r =  c->table_rV[V];
1064             g = (c->table_gU[U] + c->table_gV[V]);
1065             b =  c->table_bU[U];
1066
1067             if (hasAlpha) {
1068                 A1 = abuf0[i * 2    ] >> 7;
1069                 A2 = abuf0[i * 2 + 1] >> 7;
1070                 A1 = av_clip_uint8(A1);
1071                 A2 = av_clip_uint8(A2);
1072             }
1073
1074             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1075                           r, g, b, y, target, hasAlpha);
1076         }
1077     } else {
1078         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1079         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1080             int Y1 =  buf0[i * 2]          >> 7;
1081             int Y2 =  buf0[i * 2 + 1]      >> 7;
1082             int U  = (ubuf0[i] + ubuf1[i]) >> 8;
1083             int V  = (vbuf0[i] + vbuf1[i]) >> 8;
1084             int A1, A2;
1085             const void *r, *g, *b;
1086
1087             Y1 = av_clip_uint8(Y1);
1088             Y2 = av_clip_uint8(Y2);
1089             U  = av_clip_uint8(U);
1090             V  = av_clip_uint8(V);
1091
1092             r =  c->table_rV[V];
1093             g = (c->table_gU[U] + c->table_gV[V]);
1094             b =  c->table_bU[U];
1095
1096             if (hasAlpha) {
1097                 A1 = abuf0[i * 2    ] >> 7;
1098                 A2 = abuf0[i * 2 + 1] >> 7;
1099                 A1 = av_clip_uint8(A1);
1100                 A2 = av_clip_uint8(A2);
1101             }
1102
1103             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1104                           r, g, b, y, target, hasAlpha);
1105         }
1106     }
1107 }
1108
1109 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1110 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1111                                 const int16_t **lumSrc, int lumFilterSize, \
1112                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
1113                                 const int16_t **chrVSrc, int chrFilterSize, \
1114                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1115                                 int y) \
1116 { \
1117     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1118                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1119                                   alpSrc, dest, dstW, y, fmt, hasAlpha); \
1120 }
1121 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1122 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1123 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1124                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1125                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1126                                 int yalpha, int uvalpha, int y) \
1127 { \
1128     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1129                                   dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1130 } \
1131  \
1132 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1133                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1134                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
1135                                 int uvalpha, int y) \
1136 { \
1137     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1138                                   dstW, uvalpha, y, fmt, hasAlpha); \
1139 }
1140
1141 #if CONFIG_SMALL
1142 YUV2RGBWRAPPER(yuv2rgb,,  32_1,  AV_PIX_FMT_RGB32_1,   CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1143 YUV2RGBWRAPPER(yuv2rgb,,  32,    AV_PIX_FMT_RGB32,     CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1144 #else
1145 #if CONFIG_SWSCALE_ALPHA
1146 YUV2RGBWRAPPER(yuv2rgb,, a32_1,  AV_PIX_FMT_RGB32_1,   1)
1147 YUV2RGBWRAPPER(yuv2rgb,, a32,    AV_PIX_FMT_RGB32,     1)
1148 #endif
1149 YUV2RGBWRAPPER(yuv2rgb,, x32_1,  AV_PIX_FMT_RGB32_1,   0)
1150 YUV2RGBWRAPPER(yuv2rgb,, x32,    AV_PIX_FMT_RGB32,     0)
1151 #endif
1152 YUV2RGBWRAPPER(yuv2, rgb, rgb24, AV_PIX_FMT_RGB24,   0)
1153 YUV2RGBWRAPPER(yuv2, rgb, bgr24, AV_PIX_FMT_BGR24,   0)
1154 YUV2RGBWRAPPER(yuv2rgb,,  16,    AV_PIX_FMT_RGB565,    0)
1155 YUV2RGBWRAPPER(yuv2rgb,,  15,    AV_PIX_FMT_RGB555,    0)
1156 YUV2RGBWRAPPER(yuv2rgb,,  12,    AV_PIX_FMT_RGB444,    0)
1157 YUV2RGBWRAPPER(yuv2rgb,,   8,    AV_PIX_FMT_RGB8,      0)
1158 YUV2RGBWRAPPER(yuv2rgb,,   4,    AV_PIX_FMT_RGB4,      0)
1159 YUV2RGBWRAPPER(yuv2rgb,,   4b,   AV_PIX_FMT_RGB4_BYTE, 0)
1160
1161 static av_always_inline void
1162 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1163                           const int16_t **lumSrc, int lumFilterSize,
1164                           const int16_t *chrFilter, const int16_t **chrUSrc,
1165                           const int16_t **chrVSrc, int chrFilterSize,
1166                           const int16_t **alpSrc, uint8_t *dest,
1167                           int dstW, int y, enum AVPixelFormat target, int hasAlpha)
1168 {
1169     int i;
1170     int step = (target == AV_PIX_FMT_RGB24 || target == AV_PIX_FMT_BGR24) ? 3 : 4;
1171
1172     for (i = 0; i < dstW; i++) {
1173         int j;
1174         int Y = 0;
1175         int U = -128 << 19;
1176         int V = -128 << 19;
1177         int R, G, B, A;
1178
1179         for (j = 0; j < lumFilterSize; j++) {
1180             Y += lumSrc[j][i] * lumFilter[j];
1181         }
1182         for (j = 0; j < chrFilterSize; j++) {
1183             U += chrUSrc[j][i] * chrFilter[j];
1184             V += chrVSrc[j][i] * chrFilter[j];
1185         }
1186         Y >>= 10;
1187         U >>= 10;
1188         V >>= 10;
1189         if (hasAlpha) {
1190             A = 1 << 21;
1191             for (j = 0; j < lumFilterSize; j++) {
1192                 A += alpSrc[j][i] * lumFilter[j];
1193             }
1194             A >>= 19;
1195             if (A & 0x100)
1196                 A = av_clip_uint8(A);
1197         }
1198         Y -= c->yuv2rgb_y_offset;
1199         Y *= c->yuv2rgb_y_coeff;
1200         Y += 1 << 21;
1201         R = Y + V*c->yuv2rgb_v2r_coeff;
1202         G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1203         B = Y +                          U*c->yuv2rgb_u2b_coeff;
1204         if ((R | G | B) & 0xC0000000) {
1205             R = av_clip_uintp2(R, 30);
1206             G = av_clip_uintp2(G, 30);
1207             B = av_clip_uintp2(B, 30);
1208         }
1209
1210         switch(target) {
1211         case AV_PIX_FMT_ARGB:
1212             dest[0] = hasAlpha ? A : 255;
1213             dest[1] = R >> 22;
1214             dest[2] = G >> 22;
1215             dest[3] = B >> 22;
1216             break;
1217         case AV_PIX_FMT_RGB24:
1218             dest[0] = R >> 22;
1219             dest[1] = G >> 22;
1220             dest[2] = B >> 22;
1221             break;
1222         case AV_PIX_FMT_RGBA:
1223             dest[0] = R >> 22;
1224             dest[1] = G >> 22;
1225             dest[2] = B >> 22;
1226             dest[3] = hasAlpha ? A : 255;
1227             break;
1228         case AV_PIX_FMT_ABGR:
1229             dest[0] = hasAlpha ? A : 255;
1230             dest[1] = B >> 22;
1231             dest[2] = G >> 22;
1232             dest[3] = R >> 22;
1233             dest += 4;
1234             break;
1235         case AV_PIX_FMT_BGR24:
1236             dest[0] = B >> 22;
1237             dest[1] = G >> 22;
1238             dest[2] = R >> 22;
1239             break;
1240         case AV_PIX_FMT_BGRA:
1241             dest[0] = B >> 22;
1242             dest[1] = G >> 22;
1243             dest[2] = R >> 22;
1244             dest[3] = hasAlpha ? A : 255;
1245             break;
1246         }
1247         dest += step;
1248     }
1249 }
1250
1251 #if CONFIG_SMALL
1252 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1253 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1254 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1255 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1256 #else
1257 #if CONFIG_SWSCALE_ALPHA
1258 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, AV_PIX_FMT_BGRA,  1)
1259 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, AV_PIX_FMT_ABGR,  1)
1260 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, AV_PIX_FMT_RGBA,  1)
1261 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, AV_PIX_FMT_ARGB,  1)
1262 #endif
1263 YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, AV_PIX_FMT_BGRA,  0)
1264 YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, AV_PIX_FMT_ABGR,  0)
1265 YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, AV_PIX_FMT_RGBA,  0)
1266 YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, AV_PIX_FMT_ARGB,  0)
1267 #endif
1268 YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full,  AV_PIX_FMT_BGR24, 0)
1269 YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full,  AV_PIX_FMT_RGB24, 0)
1270
1271 static void
1272 yuv2gbrp_full_X_c(SwsContext *c, const int16_t *lumFilter,
1273                   const int16_t **lumSrc, int lumFilterSize,
1274                   const int16_t *chrFilter, const int16_t **chrUSrc,
1275                   const int16_t **chrVSrc, int chrFilterSize,
1276                   const int16_t **alpSrc, uint8_t **dest,
1277                   int dstW, int y)
1278 {
1279     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(c->dstFormat);
1280     int i;
1281     int hasAlpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA) && alpSrc;
1282     uint16_t **dest16 = (uint16_t**)dest;
1283     int SH = 22 + 8 - desc->comp[0].depth;
1284
1285     for (i = 0; i < dstW; i++) {
1286         int j;
1287         int Y = 1 << 9;
1288         int U = (1 << 9) - (128 << 19);
1289         int V = (1 << 9) - (128 << 19);
1290         int R, G, B, A;
1291
1292         for (j = 0; j < lumFilterSize; j++)
1293             Y += lumSrc[j][i] * lumFilter[j];
1294
1295         for (j = 0; j < chrFilterSize; j++) {
1296             U += chrUSrc[j][i] * chrFilter[j];
1297             V += chrVSrc[j][i] * chrFilter[j];
1298         }
1299
1300         Y >>= 10;
1301         U >>= 10;
1302         V >>= 10;
1303
1304         if (hasAlpha) {
1305             A = 1 << 18;
1306
1307             for (j = 0; j < lumFilterSize; j++)
1308                 A += alpSrc[j][i] * lumFilter[j];
1309
1310             A >>= 19;
1311
1312             if (A & 0x100)
1313                 A = av_clip_uint8(A);
1314         }
1315
1316         Y -= c->yuv2rgb_y_offset;
1317         Y *= c->yuv2rgb_y_coeff;
1318         Y += 1 << 21;
1319         R = Y + V * c->yuv2rgb_v2r_coeff;
1320         G = Y + V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
1321         B = Y +                            U * c->yuv2rgb_u2b_coeff;
1322
1323         if ((R | G | B) & 0xC0000000) {
1324             R = av_clip_uintp2(R, 30);
1325             G = av_clip_uintp2(G, 30);
1326             B = av_clip_uintp2(B, 30);
1327         }
1328
1329         if (SH != 22) {
1330             dest16[0][i] = G >> SH;
1331             dest16[1][i] = B >> SH;
1332             dest16[2][i] = R >> SH;
1333             if (hasAlpha)
1334                 dest16[3][i] = A;
1335         } else {
1336             dest[0][i] = G >> 22;
1337             dest[1][i] = B >> 22;
1338             dest[2][i] = R >> 22;
1339             if (hasAlpha)
1340                 dest[3][i] = A;
1341         }
1342     }
1343     if (SH != 22 && (!isBE(c->dstFormat)) != (!HAVE_BIGENDIAN)) {
1344         for (i = 0; i < dstW; i++) {
1345             dest16[0][i] = av_bswap16(dest16[0][i]);
1346             dest16[1][i] = av_bswap16(dest16[1][i]);
1347             dest16[2][i] = av_bswap16(dest16[2][i]);
1348             if (hasAlpha)
1349                 dest16[3][i] = av_bswap16(dest16[3][i]);
1350         }
1351     }
1352 }
1353
1354 av_cold void ff_sws_init_output_funcs(SwsContext *c,
1355                                       yuv2planar1_fn *yuv2plane1,
1356                                       yuv2planarX_fn *yuv2planeX,
1357                                       yuv2interleavedX_fn *yuv2nv12cX,
1358                                       yuv2packed1_fn *yuv2packed1,
1359                                       yuv2packed2_fn *yuv2packed2,
1360                                       yuv2packedX_fn *yuv2packedX,
1361                                       yuv2anyX_fn *yuv2anyX)
1362 {
1363     enum AVPixelFormat dstFormat = c->dstFormat;
1364     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
1365
1366     if (is16BPS(dstFormat)) {
1367         *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c  : yuv2planeX_16LE_c;
1368         *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c  : yuv2plane1_16LE_c;
1369     } else if (is9_15BPS(dstFormat)) {
1370         if (desc->comp[0].depth == 9) {
1371             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c  : yuv2planeX_9LE_c;
1372             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c  : yuv2plane1_9LE_c;
1373         } else if (desc->comp[0].depth == 10) {
1374             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c  : yuv2planeX_10LE_c;
1375             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c  : yuv2plane1_10LE_c;
1376         } else if (desc->comp[0].depth == 12) {
1377             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c  : yuv2planeX_12LE_c;
1378             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c  : yuv2plane1_12LE_c;
1379         } else {
1380             assert(0);
1381         }
1382     } else {
1383         *yuv2plane1 = yuv2plane1_8_c;
1384         *yuv2planeX = yuv2planeX_8_c;
1385         if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
1386             *yuv2nv12cX = yuv2nv12cX_c;
1387     }
1388
1389     if(c->flags & SWS_FULL_CHR_H_INT) {
1390         switch (dstFormat) {
1391             case AV_PIX_FMT_RGBA:
1392 #if CONFIG_SMALL
1393                 *yuv2packedX = yuv2rgba32_full_X_c;
1394 #else
1395 #if CONFIG_SWSCALE_ALPHA
1396                 if (c->alpPixBuf) {
1397                     *yuv2packedX = yuv2rgba32_full_X_c;
1398                 } else
1399 #endif /* CONFIG_SWSCALE_ALPHA */
1400                 {
1401                     *yuv2packedX = yuv2rgbx32_full_X_c;
1402                 }
1403 #endif /* !CONFIG_SMALL */
1404                 break;
1405             case AV_PIX_FMT_ARGB:
1406 #if CONFIG_SMALL
1407                 *yuv2packedX = yuv2argb32_full_X_c;
1408 #else
1409 #if CONFIG_SWSCALE_ALPHA
1410                 if (c->alpPixBuf) {
1411                     *yuv2packedX = yuv2argb32_full_X_c;
1412                 } else
1413 #endif /* CONFIG_SWSCALE_ALPHA */
1414                 {
1415                     *yuv2packedX = yuv2xrgb32_full_X_c;
1416                 }
1417 #endif /* !CONFIG_SMALL */
1418                 break;
1419             case AV_PIX_FMT_BGRA:
1420 #if CONFIG_SMALL
1421                 *yuv2packedX = yuv2bgra32_full_X_c;
1422 #else
1423 #if CONFIG_SWSCALE_ALPHA
1424                 if (c->alpPixBuf) {
1425                     *yuv2packedX = yuv2bgra32_full_X_c;
1426                 } else
1427 #endif /* CONFIG_SWSCALE_ALPHA */
1428                 {
1429                     *yuv2packedX = yuv2bgrx32_full_X_c;
1430                 }
1431 #endif /* !CONFIG_SMALL */
1432                 break;
1433             case AV_PIX_FMT_ABGR:
1434 #if CONFIG_SMALL
1435                 *yuv2packedX = yuv2abgr32_full_X_c;
1436 #else
1437 #if CONFIG_SWSCALE_ALPHA
1438                 if (c->alpPixBuf) {
1439                     *yuv2packedX = yuv2abgr32_full_X_c;
1440                 } else
1441 #endif /* CONFIG_SWSCALE_ALPHA */
1442                 {
1443                     *yuv2packedX = yuv2xbgr32_full_X_c;
1444                 }
1445 #endif /* !CONFIG_SMALL */
1446                 break;
1447             case AV_PIX_FMT_RGB24:
1448             *yuv2packedX = yuv2rgb24_full_X_c;
1449             break;
1450         case AV_PIX_FMT_BGR24:
1451             *yuv2packedX = yuv2bgr24_full_X_c;
1452             break;
1453         case AV_PIX_FMT_GBRP:
1454         case AV_PIX_FMT_GBRP9BE:
1455         case AV_PIX_FMT_GBRP9LE:
1456         case AV_PIX_FMT_GBRP10BE:
1457         case AV_PIX_FMT_GBRP10LE:
1458         case AV_PIX_FMT_GBRP12BE:
1459         case AV_PIX_FMT_GBRP12LE:
1460         case AV_PIX_FMT_GBRAP12BE:
1461         case AV_PIX_FMT_GBRAP12LE:
1462         case AV_PIX_FMT_GBRP16BE:
1463         case AV_PIX_FMT_GBRP16LE:
1464         case AV_PIX_FMT_GBRAP:
1465             *yuv2anyX = yuv2gbrp_full_X_c;
1466             break;
1467         }
1468     } else {
1469         switch (dstFormat) {
1470         case AV_PIX_FMT_RGB48LE:
1471             *yuv2packed1 = yuv2rgb48le_1_c;
1472             *yuv2packed2 = yuv2rgb48le_2_c;
1473             *yuv2packedX = yuv2rgb48le_X_c;
1474             break;
1475         case AV_PIX_FMT_RGB48BE:
1476             *yuv2packed1 = yuv2rgb48be_1_c;
1477             *yuv2packed2 = yuv2rgb48be_2_c;
1478             *yuv2packedX = yuv2rgb48be_X_c;
1479             break;
1480         case AV_PIX_FMT_BGR48LE:
1481             *yuv2packed1 = yuv2bgr48le_1_c;
1482             *yuv2packed2 = yuv2bgr48le_2_c;
1483             *yuv2packedX = yuv2bgr48le_X_c;
1484             break;
1485         case AV_PIX_FMT_BGR48BE:
1486             *yuv2packed1 = yuv2bgr48be_1_c;
1487             *yuv2packed2 = yuv2bgr48be_2_c;
1488             *yuv2packedX = yuv2bgr48be_X_c;
1489             break;
1490         case AV_PIX_FMT_RGB32:
1491         case AV_PIX_FMT_BGR32:
1492 #if CONFIG_SMALL
1493             *yuv2packed1 = yuv2rgb32_1_c;
1494             *yuv2packed2 = yuv2rgb32_2_c;
1495             *yuv2packedX = yuv2rgb32_X_c;
1496 #else
1497 #if CONFIG_SWSCALE_ALPHA
1498                 if (c->alpPixBuf) {
1499                     *yuv2packed1 = yuv2rgba32_1_c;
1500                     *yuv2packed2 = yuv2rgba32_2_c;
1501                     *yuv2packedX = yuv2rgba32_X_c;
1502                 } else
1503 #endif /* CONFIG_SWSCALE_ALPHA */
1504                 {
1505                     *yuv2packed1 = yuv2rgbx32_1_c;
1506                     *yuv2packed2 = yuv2rgbx32_2_c;
1507                     *yuv2packedX = yuv2rgbx32_X_c;
1508                 }
1509 #endif /* !CONFIG_SMALL */
1510             break;
1511         case AV_PIX_FMT_RGB32_1:
1512         case AV_PIX_FMT_BGR32_1:
1513 #if CONFIG_SMALL
1514                 *yuv2packed1 = yuv2rgb32_1_1_c;
1515                 *yuv2packed2 = yuv2rgb32_1_2_c;
1516                 *yuv2packedX = yuv2rgb32_1_X_c;
1517 #else
1518 #if CONFIG_SWSCALE_ALPHA
1519                 if (c->alpPixBuf) {
1520                     *yuv2packed1 = yuv2rgba32_1_1_c;
1521                     *yuv2packed2 = yuv2rgba32_1_2_c;
1522                     *yuv2packedX = yuv2rgba32_1_X_c;
1523                 } else
1524 #endif /* CONFIG_SWSCALE_ALPHA */
1525                 {
1526                     *yuv2packed1 = yuv2rgbx32_1_1_c;
1527                     *yuv2packed2 = yuv2rgbx32_1_2_c;
1528                     *yuv2packedX = yuv2rgbx32_1_X_c;
1529                 }
1530 #endif /* !CONFIG_SMALL */
1531                 break;
1532         case AV_PIX_FMT_RGB24:
1533             *yuv2packed1 = yuv2rgb24_1_c;
1534             *yuv2packed2 = yuv2rgb24_2_c;
1535             *yuv2packedX = yuv2rgb24_X_c;
1536             break;
1537         case AV_PIX_FMT_BGR24:
1538             *yuv2packed1 = yuv2bgr24_1_c;
1539             *yuv2packed2 = yuv2bgr24_2_c;
1540             *yuv2packedX = yuv2bgr24_X_c;
1541             break;
1542         case AV_PIX_FMT_RGB565LE:
1543         case AV_PIX_FMT_RGB565BE:
1544         case AV_PIX_FMT_BGR565LE:
1545         case AV_PIX_FMT_BGR565BE:
1546             *yuv2packed1 = yuv2rgb16_1_c;
1547             *yuv2packed2 = yuv2rgb16_2_c;
1548             *yuv2packedX = yuv2rgb16_X_c;
1549             break;
1550         case AV_PIX_FMT_RGB555LE:
1551         case AV_PIX_FMT_RGB555BE:
1552         case AV_PIX_FMT_BGR555LE:
1553         case AV_PIX_FMT_BGR555BE:
1554             *yuv2packed1 = yuv2rgb15_1_c;
1555             *yuv2packed2 = yuv2rgb15_2_c;
1556             *yuv2packedX = yuv2rgb15_X_c;
1557             break;
1558         case AV_PIX_FMT_RGB444LE:
1559         case AV_PIX_FMT_RGB444BE:
1560         case AV_PIX_FMT_BGR444LE:
1561         case AV_PIX_FMT_BGR444BE:
1562             *yuv2packed1 = yuv2rgb12_1_c;
1563             *yuv2packed2 = yuv2rgb12_2_c;
1564             *yuv2packedX = yuv2rgb12_X_c;
1565             break;
1566         case AV_PIX_FMT_RGB8:
1567         case AV_PIX_FMT_BGR8:
1568             *yuv2packed1 = yuv2rgb8_1_c;
1569             *yuv2packed2 = yuv2rgb8_2_c;
1570             *yuv2packedX = yuv2rgb8_X_c;
1571             break;
1572         case AV_PIX_FMT_RGB4:
1573         case AV_PIX_FMT_BGR4:
1574             *yuv2packed1 = yuv2rgb4_1_c;
1575             *yuv2packed2 = yuv2rgb4_2_c;
1576             *yuv2packedX = yuv2rgb4_X_c;
1577             break;
1578         case AV_PIX_FMT_RGB4_BYTE:
1579         case AV_PIX_FMT_BGR4_BYTE:
1580             *yuv2packed1 = yuv2rgb4b_1_c;
1581             *yuv2packed2 = yuv2rgb4b_2_c;
1582             *yuv2packedX = yuv2rgb4b_X_c;
1583             break;
1584         }
1585     }
1586     switch (dstFormat) {
1587     case AV_PIX_FMT_MONOWHITE:
1588         *yuv2packed1 = yuv2monowhite_1_c;
1589         *yuv2packed2 = yuv2monowhite_2_c;
1590         *yuv2packedX = yuv2monowhite_X_c;
1591         break;
1592     case AV_PIX_FMT_MONOBLACK:
1593         *yuv2packed1 = yuv2monoblack_1_c;
1594         *yuv2packed2 = yuv2monoblack_2_c;
1595         *yuv2packedX = yuv2monoblack_X_c;
1596         break;
1597     case AV_PIX_FMT_YUYV422:
1598         *yuv2packed1 = yuv2yuyv422_1_c;
1599         *yuv2packed2 = yuv2yuyv422_2_c;
1600         *yuv2packedX = yuv2yuyv422_X_c;
1601         break;
1602     case AV_PIX_FMT_YVYU422:
1603         *yuv2packed1 = yuv2yvyu422_1_c;
1604         *yuv2packed2 = yuv2yvyu422_2_c;
1605         *yuv2packedX = yuv2yvyu422_X_c;
1606         break;
1607     case AV_PIX_FMT_UYVY422:
1608         *yuv2packed1 = yuv2uyvy422_1_c;
1609         *yuv2packed2 = yuv2uyvy422_2_c;
1610         *yuv2packedX = yuv2uyvy422_X_c;
1611         break;
1612     }
1613 }