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