]> git.sesse.net Git - ffmpeg/blob - libswscale/output.c
swscale: clip before assigning tables in RGB output functions.
[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/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, 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, 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, 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, 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, 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, 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, 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(16, BE, 1, 16, int32_t)
235 yuv2NBPS(16, LE, 0, 16, int32_t)
236
237 static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
238                            const int16_t **src, uint8_t *dest, int dstW,
239                            const uint8_t *dither, int offset)
240 {
241     int i;
242     for (i=0; i<dstW; i++) {
243         int val = dither[(i + offset) & 7] << 12;
244         int j;
245         for (j=0; j<filterSize; j++)
246             val += src[j][i] * filter[j];
247
248         dest[i]= av_clip_uint8(val>>19);
249     }
250 }
251
252 static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
253                            const uint8_t *dither, int offset)
254 {
255     int i;
256     for (i=0; i<dstW; i++) {
257         int val = (src[i] + dither[(i + offset) & 7]) >> 7;
258         dest[i]= av_clip_uint8(val);
259     }
260 }
261
262 static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
263                         const int16_t **chrUSrc, const int16_t **chrVSrc,
264                         uint8_t *dest, int chrDstW)
265 {
266     enum PixelFormat dstFormat = c->dstFormat;
267     const uint8_t *chrDither = c->chrDither8;
268     int i;
269
270     if (dstFormat == PIX_FMT_NV12)
271         for (i=0; i<chrDstW; i++) {
272             int u = chrDither[i & 7] << 12;
273             int v = chrDither[(i + 3) & 7] << 12;
274             int j;
275             for (j=0; j<chrFilterSize; j++) {
276                 u += chrUSrc[j][i] * chrFilter[j];
277                 v += chrVSrc[j][i] * chrFilter[j];
278             }
279
280             dest[2*i]= av_clip_uint8(u>>19);
281             dest[2*i+1]= av_clip_uint8(v>>19);
282         }
283     else
284         for (i=0; i<chrDstW; i++) {
285             int u = chrDither[i & 7] << 12;
286             int v = chrDither[(i + 3) & 7] << 12;
287             int j;
288             for (j=0; j<chrFilterSize; j++) {
289                 u += chrUSrc[j][i] * chrFilter[j];
290                 v += chrVSrc[j][i] * chrFilter[j];
291             }
292
293             dest[2*i]= av_clip_uint8(v>>19);
294             dest[2*i+1]= av_clip_uint8(u>>19);
295         }
296 }
297
298 #define accumulate_bit(acc, val) \
299     acc <<= 1; \
300     acc |= (val) >= (128 + 110)
301 #define output_pixel(pos, acc) \
302     if (target == PIX_FMT_MONOBLACK) { \
303         pos = acc; \
304     } else { \
305         pos = ~acc; \
306     }
307
308 static av_always_inline void
309 yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
310                       const int16_t **lumSrc, int lumFilterSize,
311                       const int16_t *chrFilter, const int16_t **chrUSrc,
312                       const int16_t **chrVSrc, int chrFilterSize,
313                       const int16_t **alpSrc, uint8_t *dest, int dstW,
314                       int y, enum PixelFormat target)
315 {
316     const uint8_t * const d128=dither_8x8_220[y&7];
317     int i;
318     unsigned acc = 0;
319
320     for (i = 0; i < dstW; i += 2) {
321         int j;
322         int Y1 = 1 << 18;
323         int Y2 = 1 << 18;
324
325         for (j = 0; j < lumFilterSize; j++) {
326             Y1 += lumSrc[j][i]   * lumFilter[j];
327             Y2 += lumSrc[j][i+1] * lumFilter[j];
328         }
329         Y1 >>= 19;
330         Y2 >>= 19;
331         if ((Y1 | Y2) & 0x100) {
332             Y1 = av_clip_uint8(Y1);
333             Y2 = av_clip_uint8(Y2);
334         }
335         accumulate_bit(acc, Y1 + d128[(i + 0) & 7]);
336         accumulate_bit(acc, Y2 + d128[(i + 1) & 7]);
337         if ((i & 7) == 6) {
338             output_pixel(*dest++, acc);
339         }
340     }
341
342     if (i & 6) {
343         output_pixel(*dest, acc);
344     }
345 }
346
347 static av_always_inline void
348 yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
349                       const int16_t *ubuf[2], const int16_t *vbuf[2],
350                       const int16_t *abuf[2], uint8_t *dest, int dstW,
351                       int yalpha, int uvalpha, int y,
352                       enum PixelFormat target)
353 {
354     const int16_t *buf0  = buf[0],  *buf1  = buf[1];
355     const uint8_t * const d128 = dither_8x8_220[y & 7];
356     int  yalpha1 = 4096 - yalpha;
357     int i;
358
359     for (i = 0; i < dstW; i += 8) {
360         int Y, acc = 0;
361
362         Y = (buf0[i + 0] * yalpha1 + buf1[i + 0] * yalpha) >> 19;
363         accumulate_bit(acc, Y + d128[0]);
364         Y = (buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19;
365         accumulate_bit(acc, Y + d128[1]);
366         Y = (buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19;
367         accumulate_bit(acc, Y + d128[2]);
368         Y = (buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19;
369         accumulate_bit(acc, Y + d128[3]);
370         Y = (buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19;
371         accumulate_bit(acc, Y + d128[4]);
372         Y = (buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19;
373         accumulate_bit(acc, Y + d128[5]);
374         Y = (buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19;
375         accumulate_bit(acc, Y + d128[6]);
376         Y = (buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19;
377         accumulate_bit(acc, Y + d128[7]);
378
379         output_pixel(*dest++, acc);
380     }
381 }
382
383 static av_always_inline void
384 yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
385                       const int16_t *ubuf[2], const int16_t *vbuf[2],
386                       const int16_t *abuf0, uint8_t *dest, int dstW,
387                       int uvalpha, int y, enum PixelFormat target)
388 {
389     const uint8_t * const d128 = dither_8x8_220[y & 7];
390     int i;
391
392     for (i = 0; i < dstW; i += 8) {
393         int acc = 0;
394
395         accumulate_bit(acc, (buf0[i + 0] >> 7) + d128[0]);
396         accumulate_bit(acc, (buf0[i + 1] >> 7) + d128[1]);
397         accumulate_bit(acc, (buf0[i + 2] >> 7) + d128[2]);
398         accumulate_bit(acc, (buf0[i + 3] >> 7) + d128[3]);
399         accumulate_bit(acc, (buf0[i + 4] >> 7) + d128[4]);
400         accumulate_bit(acc, (buf0[i + 5] >> 7) + d128[5]);
401         accumulate_bit(acc, (buf0[i + 6] >> 7) + d128[6]);
402         accumulate_bit(acc, (buf0[i + 7] >> 7) + d128[7]);
403
404         output_pixel(*dest++, acc);
405     }
406 }
407
408 #undef output_pixel
409 #undef accumulate_bit
410
411 #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
412 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
413                                 const int16_t **lumSrc, int lumFilterSize, \
414                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
415                                 const int16_t **chrVSrc, int chrFilterSize, \
416                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
417                                 int y) \
418 { \
419     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
420                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
421                                   alpSrc, dest, dstW, y, fmt); \
422 } \
423  \
424 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
425                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
426                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
427                                 int yalpha, int uvalpha, int y) \
428 { \
429     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
430                                   dest, dstW, yalpha, uvalpha, y, fmt); \
431 } \
432  \
433 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
434                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
435                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
436                                 int uvalpha, int y) \
437 { \
438     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
439                                   abuf0, dest, dstW, uvalpha, \
440                                   y, fmt); \
441 }
442
443 YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE)
444 YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK)
445
446 #define output_pixels(pos, Y1, U, Y2, V) \
447     if (target == PIX_FMT_YUYV422) { \
448         dest[pos + 0] = Y1; \
449         dest[pos + 1] = U;  \
450         dest[pos + 2] = Y2; \
451         dest[pos + 3] = V;  \
452     } else { \
453         dest[pos + 0] = U;  \
454         dest[pos + 1] = Y1; \
455         dest[pos + 2] = V;  \
456         dest[pos + 3] = Y2; \
457     }
458
459 static av_always_inline void
460 yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
461                      const int16_t **lumSrc, int lumFilterSize,
462                      const int16_t *chrFilter, const int16_t **chrUSrc,
463                      const int16_t **chrVSrc, int chrFilterSize,
464                      const int16_t **alpSrc, uint8_t *dest, int dstW,
465                      int y, enum PixelFormat target)
466 {
467     int i;
468
469     for (i = 0; i < ((dstW + 1) >> 1); i++) {
470         int j;
471         int Y1 = 1 << 18;
472         int Y2 = 1 << 18;
473         int U  = 1 << 18;
474         int V  = 1 << 18;
475
476         for (j = 0; j < lumFilterSize; j++) {
477             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
478             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
479         }
480         for (j = 0; j < chrFilterSize; j++) {
481             U += chrUSrc[j][i] * chrFilter[j];
482             V += chrVSrc[j][i] * chrFilter[j];
483         }
484         Y1 >>= 19;
485         Y2 >>= 19;
486         U  >>= 19;
487         V  >>= 19;
488         if ((Y1 | Y2 | U | V) & 0x100) {
489             Y1 = av_clip_uint8(Y1);
490             Y2 = av_clip_uint8(Y2);
491             U  = av_clip_uint8(U);
492             V  = av_clip_uint8(V);
493         }
494         output_pixels(4*i, Y1, U, Y2, V);
495     }
496 }
497
498 static av_always_inline void
499 yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
500                      const int16_t *ubuf[2], const int16_t *vbuf[2],
501                      const int16_t *abuf[2], uint8_t *dest, int dstW,
502                      int yalpha, int uvalpha, int y,
503                      enum PixelFormat target)
504 {
505     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
506                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
507                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
508     int  yalpha1 = 4096 - yalpha;
509     int uvalpha1 = 4096 - uvalpha;
510     int i;
511
512     for (i = 0; i < ((dstW + 1) >> 1); i++) {
513         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
514         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
515         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
516         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
517
518         Y1 = av_clip_uint8(Y1);
519         Y2 = av_clip_uint8(Y2);
520         U  = av_clip_uint8(U);
521         V  = av_clip_uint8(V);
522
523         output_pixels(i * 4, Y1, U, Y2, V);
524     }
525 }
526
527 static av_always_inline void
528 yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
529                      const int16_t *ubuf[2], const int16_t *vbuf[2],
530                      const int16_t *abuf0, uint8_t *dest, int dstW,
531                      int uvalpha, int y, enum PixelFormat target)
532 {
533     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
534     int i;
535
536     if (uvalpha < 2048) {
537         for (i = 0; i < ((dstW + 1) >> 1); i++) {
538             int Y1 = buf0[i * 2]     >> 7;
539             int Y2 = buf0[i * 2 + 1] >> 7;
540             int U  = ubuf0[i]        >> 7;
541             int V  = vbuf0[i]        >> 7;
542
543             Y1 = av_clip_uint8(Y1);
544             Y2 = av_clip_uint8(Y2);
545             U  = av_clip_uint8(U);
546             V  = av_clip_uint8(V);
547
548             output_pixels(i * 4, Y1, U, Y2, V);
549         }
550     } else {
551         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
552         for (i = 0; i < ((dstW + 1) >> 1); i++) {
553             int Y1 =  buf0[i * 2]          >> 7;
554             int Y2 =  buf0[i * 2 + 1]      >> 7;
555             int U  = (ubuf0[i] + ubuf1[i]) >> 8;
556             int V  = (vbuf0[i] + vbuf1[i]) >> 8;
557
558             Y1 = av_clip_uint8(Y1);
559             Y2 = av_clip_uint8(Y2);
560             U  = av_clip_uint8(U);
561             V  = av_clip_uint8(V);
562
563             output_pixels(i * 4, Y1, U, Y2, V);
564         }
565     }
566 }
567
568 #undef output_pixels
569
570 YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422)
571 YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422)
572
573 #define R_B ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? R : B)
574 #define B_R ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? B : R)
575 #define output_pixel(pos, val) \
576     if (isBE(target)) { \
577         AV_WB16(pos, val); \
578     } else { \
579         AV_WL16(pos, val); \
580     }
581
582 static av_always_inline void
583 yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
584                        const int32_t **lumSrc, int lumFilterSize,
585                        const int16_t *chrFilter, const int32_t **chrUSrc,
586                        const int32_t **chrVSrc, int chrFilterSize,
587                        const int32_t **alpSrc, uint16_t *dest, int dstW,
588                        int y, enum PixelFormat target)
589 {
590     int i;
591
592     for (i = 0; i < ((dstW + 1) >> 1); i++) {
593         int j;
594         int Y1 = -0x40000000;
595         int Y2 = -0x40000000;
596         int U  = -128 << 23; // 19
597         int V  = -128 << 23;
598         int R, G, B;
599
600         for (j = 0; j < lumFilterSize; j++) {
601             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
602             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
603         }
604         for (j = 0; j < chrFilterSize; j++) {
605             U += chrUSrc[j][i] * chrFilter[j];
606             V += chrVSrc[j][i] * chrFilter[j];
607         }
608
609         // 8bit: 12+15=27; 16-bit: 12+19=31
610         Y1 >>= 14; // 10
611         Y1 += 0x10000;
612         Y2 >>= 14;
613         Y2 += 0x10000;
614         U  >>= 14;
615         V  >>= 14;
616
617         // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
618         Y1 -= c->yuv2rgb_y_offset;
619         Y2 -= c->yuv2rgb_y_offset;
620         Y1 *= c->yuv2rgb_y_coeff;
621         Y2 *= c->yuv2rgb_y_coeff;
622         Y1 += 1 << 13; // 21
623         Y2 += 1 << 13;
624         // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
625
626         R = V * c->yuv2rgb_v2r_coeff;
627         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
628         B =                            U * c->yuv2rgb_u2b_coeff;
629
630         // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
631         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
632         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
633         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
634         output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
635         output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
636         output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
637         dest += 6;
638     }
639 }
640
641 static av_always_inline void
642 yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
643                        const int32_t *ubuf[2], const int32_t *vbuf[2],
644                        const int32_t *abuf[2], uint16_t *dest, int dstW,
645                        int yalpha, int uvalpha, int y,
646                        enum PixelFormat target)
647 {
648     const int32_t *buf0  = buf[0],  *buf1  = buf[1],
649                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
650                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
651     int  yalpha1 = 4096 - yalpha;
652     int uvalpha1 = 4096 - uvalpha;
653     int i;
654
655     for (i = 0; i < ((dstW + 1) >> 1); i++) {
656         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha) >> 14;
657         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha) >> 14;
658         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha + (-128 << 23)) >> 14;
659         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha + (-128 << 23)) >> 14;
660         int R, G, B;
661
662         Y1 -= c->yuv2rgb_y_offset;
663         Y2 -= c->yuv2rgb_y_offset;
664         Y1 *= c->yuv2rgb_y_coeff;
665         Y2 *= c->yuv2rgb_y_coeff;
666         Y1 += 1 << 13;
667         Y2 += 1 << 13;
668
669         R = V * c->yuv2rgb_v2r_coeff;
670         G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
671         B =                            U * c->yuv2rgb_u2b_coeff;
672
673         output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
674         output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
675         output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
676         output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
677         output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
678         output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
679         dest += 6;
680     }
681 }
682
683 static av_always_inline void
684 yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
685                        const int32_t *ubuf[2], const int32_t *vbuf[2],
686                        const int32_t *abuf0, uint16_t *dest, int dstW,
687                        int uvalpha, int y, enum PixelFormat target)
688 {
689     const int32_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
690     int i;
691
692     if (uvalpha < 2048) {
693         for (i = 0; i < ((dstW + 1) >> 1); i++) {
694             int Y1 = (buf0[i * 2]    ) >> 2;
695             int Y2 = (buf0[i * 2 + 1]) >> 2;
696             int U  = (ubuf0[i] + (-128 << 11)) >> 2;
697             int V  = (vbuf0[i] + (-128 << 11)) >> 2;
698             int R, G, B;
699
700             Y1 -= c->yuv2rgb_y_offset;
701             Y2 -= c->yuv2rgb_y_offset;
702             Y1 *= c->yuv2rgb_y_coeff;
703             Y2 *= c->yuv2rgb_y_coeff;
704             Y1 += 1 << 13;
705             Y2 += 1 << 13;
706
707             R = V * c->yuv2rgb_v2r_coeff;
708             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
709             B =                            U * c->yuv2rgb_u2b_coeff;
710
711             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
712             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
713             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
714             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
715             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
716             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
717             dest += 6;
718         }
719     } else {
720         const int32_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
721         for (i = 0; i < ((dstW + 1) >> 1); i++) {
722             int Y1 = (buf0[i * 2]    ) >> 2;
723             int Y2 = (buf0[i * 2 + 1]) >> 2;
724             int U  = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
725             int V  = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
726             int R, G, B;
727
728             Y1 -= c->yuv2rgb_y_offset;
729             Y2 -= c->yuv2rgb_y_offset;
730             Y1 *= c->yuv2rgb_y_coeff;
731             Y2 *= c->yuv2rgb_y_coeff;
732             Y1 += 1 << 13;
733             Y2 += 1 << 13;
734
735             R = V * c->yuv2rgb_v2r_coeff;
736             G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
737             B =                            U * c->yuv2rgb_u2b_coeff;
738
739             output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
740             output_pixel(&dest[1], av_clip_uintp2(  G + Y1, 30) >> 14);
741             output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
742             output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
743             output_pixel(&dest[4], av_clip_uintp2(  G + Y2, 30) >> 14);
744             output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
745             dest += 6;
746         }
747     }
748 }
749
750 #undef output_pixel
751 #undef r_b
752 #undef b_r
753
754 #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
755 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
756                         const int16_t **_lumSrc, int lumFilterSize, \
757                         const int16_t *chrFilter, const int16_t **_chrUSrc, \
758                         const int16_t **_chrVSrc, int chrFilterSize, \
759                         const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
760                         int y) \
761 { \
762     const int32_t **lumSrc  = (const int32_t **) _lumSrc, \
763                   **chrUSrc = (const int32_t **) _chrUSrc, \
764                   **chrVSrc = (const int32_t **) _chrVSrc, \
765                   **alpSrc  = (const int32_t **) _alpSrc; \
766     uint16_t *dest = (uint16_t *) _dest; \
767     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
768                           chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
769                           alpSrc, dest, dstW, y, fmt); \
770 } \
771  \
772 static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
773                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
774                         const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
775                         int yalpha, int uvalpha, int y) \
776 { \
777     const int32_t **buf  = (const int32_t **) _buf, \
778                   **ubuf = (const int32_t **) _ubuf, \
779                   **vbuf = (const int32_t **) _vbuf, \
780                   **abuf = (const int32_t **) _abuf; \
781     uint16_t *dest = (uint16_t *) _dest; \
782     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
783                           dest, dstW, yalpha, uvalpha, y, fmt); \
784 } \
785  \
786 static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
787                         const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
788                         const int16_t *_abuf0, uint8_t *_dest, int dstW, \
789                         int uvalpha, int y) \
790 { \
791     const int32_t *buf0  = (const int32_t *)  _buf0, \
792                  **ubuf  = (const int32_t **) _ubuf, \
793                  **vbuf  = (const int32_t **) _vbuf, \
794                   *abuf0 = (const int32_t *)  _abuf0; \
795     uint16_t *dest = (uint16_t *) _dest; \
796     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
797                                   dstW, uvalpha, y, fmt); \
798 }
799
800 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, PIX_FMT_RGB48BE)
801 YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, PIX_FMT_RGB48LE)
802 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, PIX_FMT_BGR48BE)
803 YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, PIX_FMT_BGR48LE)
804
805 /*
806  * Write out 2 RGB pixels in the target pixel format. This function takes a
807  * R/G/B LUT as generated by ff_yuv2rgb_c_init_tables(), which takes care of
808  * things like endianness conversion and shifting. The caller takes care of
809  * setting the correct offset in these tables from the chroma (U/V) values.
810  * This function then uses the luminance (Y1/Y2) values to write out the
811  * correct RGB values into the destination buffer.
812  */
813 static av_always_inline void
814 yuv2rgb_write(uint8_t *_dest, int i, unsigned Y1, unsigned Y2,
815               unsigned A1, unsigned A2,
816               const void *_r, const void *_g, const void *_b, int y,
817               enum PixelFormat target, int hasAlpha)
818 {
819     if (target == PIX_FMT_ARGB || target == PIX_FMT_RGBA ||
820         target == PIX_FMT_ABGR || target == PIX_FMT_BGRA) {
821         uint32_t *dest = (uint32_t *) _dest;
822         const uint32_t *r = (const uint32_t *) _r;
823         const uint32_t *g = (const uint32_t *) _g;
824         const uint32_t *b = (const uint32_t *) _b;
825
826 #if CONFIG_SMALL
827         int sh = hasAlpha ? ((target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24) : 0;
828
829         dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
830         dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
831 #else
832         if (hasAlpha) {
833             int sh = (target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24;
834
835             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
836             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
837         } else {
838             dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
839             dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
840         }
841 #endif
842     } else if (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) {
843         uint8_t *dest = (uint8_t *) _dest;
844         const uint8_t *r = (const uint8_t *) _r;
845         const uint8_t *g = (const uint8_t *) _g;
846         const uint8_t *b = (const uint8_t *) _b;
847
848 #define r_b ((target == PIX_FMT_RGB24) ? r : b)
849 #define b_r ((target == PIX_FMT_RGB24) ? b : r)
850         dest[i * 6 + 0] = r_b[Y1];
851         dest[i * 6 + 1] =   g[Y1];
852         dest[i * 6 + 2] = b_r[Y1];
853         dest[i * 6 + 3] = r_b[Y2];
854         dest[i * 6 + 4] =   g[Y2];
855         dest[i * 6 + 5] = b_r[Y2];
856 #undef r_b
857 #undef b_r
858     } else if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565 ||
859                target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555 ||
860                target == PIX_FMT_RGB444 || target == PIX_FMT_BGR444) {
861         uint16_t *dest = (uint16_t *) _dest;
862         const uint16_t *r = (const uint16_t *) _r;
863         const uint16_t *g = (const uint16_t *) _g;
864         const uint16_t *b = (const uint16_t *) _b;
865         int dr1, dg1, db1, dr2, dg2, db2;
866
867         if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565) {
868             dr1 = dither_2x2_8[ y & 1     ][0];
869             dg1 = dither_2x2_4[ y & 1     ][0];
870             db1 = dither_2x2_8[(y & 1) ^ 1][0];
871             dr2 = dither_2x2_8[ y & 1     ][1];
872             dg2 = dither_2x2_4[ y & 1     ][1];
873             db2 = dither_2x2_8[(y & 1) ^ 1][1];
874         } else if (target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555) {
875             dr1 = dither_2x2_8[ y & 1     ][0];
876             dg1 = dither_2x2_8[ y & 1     ][1];
877             db1 = dither_2x2_8[(y & 1) ^ 1][0];
878             dr2 = dither_2x2_8[ y & 1     ][1];
879             dg2 = dither_2x2_8[ y & 1     ][0];
880             db2 = dither_2x2_8[(y & 1) ^ 1][1];
881         } else {
882             dr1 = dither_4x4_16[ y & 3     ][0];
883             dg1 = dither_4x4_16[ y & 3     ][1];
884             db1 = dither_4x4_16[(y & 3) ^ 3][0];
885             dr2 = dither_4x4_16[ y & 3     ][1];
886             dg2 = dither_4x4_16[ y & 3     ][0];
887             db2 = dither_4x4_16[(y & 3) ^ 3][1];
888         }
889
890         dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
891         dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
892     } else /* 8/4-bit */ {
893         uint8_t *dest = (uint8_t *) _dest;
894         const uint8_t *r = (const uint8_t *) _r;
895         const uint8_t *g = (const uint8_t *) _g;
896         const uint8_t *b = (const uint8_t *) _b;
897         int dr1, dg1, db1, dr2, dg2, db2;
898
899         if (target == PIX_FMT_RGB8 || target == PIX_FMT_BGR8) {
900             const uint8_t * const d64 = dither_8x8_73[y & 7];
901             const uint8_t * const d32 = dither_8x8_32[y & 7];
902             dr1 = dg1 = d32[(i * 2 + 0) & 7];
903             db1 =       d64[(i * 2 + 0) & 7];
904             dr2 = dg2 = d32[(i * 2 + 1) & 7];
905             db2 =       d64[(i * 2 + 1) & 7];
906         } else {
907             const uint8_t * const d64  = dither_8x8_73 [y & 7];
908             const uint8_t * const d128 = dither_8x8_220[y & 7];
909             dr1 = db1 = d128[(i * 2 + 0) & 7];
910             dg1 =        d64[(i * 2 + 0) & 7];
911             dr2 = db2 = d128[(i * 2 + 1) & 7];
912             dg2 =        d64[(i * 2 + 1) & 7];
913         }
914
915         if (target == PIX_FMT_RGB4 || target == PIX_FMT_BGR4) {
916             dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
917                     ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
918         } else {
919             dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
920             dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
921         }
922     }
923 }
924
925 static av_always_inline void
926 yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
927                      const int16_t **lumSrc, int lumFilterSize,
928                      const int16_t *chrFilter, const int16_t **chrUSrc,
929                      const int16_t **chrVSrc, int chrFilterSize,
930                      const int16_t **alpSrc, uint8_t *dest, int dstW,
931                      int y, enum PixelFormat target, int hasAlpha)
932 {
933     int i;
934
935     for (i = 0; i < ((dstW + 1) >> 1); i++) {
936         int j, A1, A2;
937         int Y1 = 1 << 18;
938         int Y2 = 1 << 18;
939         int U  = 1 << 18;
940         int V  = 1 << 18;
941         const void *r, *g, *b;
942
943         for (j = 0; j < lumFilterSize; j++) {
944             Y1 += lumSrc[j][i * 2]     * lumFilter[j];
945             Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
946         }
947         for (j = 0; j < chrFilterSize; j++) {
948             U += chrUSrc[j][i] * chrFilter[j];
949             V += chrVSrc[j][i] * chrFilter[j];
950         }
951         Y1 >>= 19;
952         Y2 >>= 19;
953         U  >>= 19;
954         V  >>= 19;
955         if ((Y1 | Y2 | U | V) & 0x100) {
956             Y1 = av_clip_uint8(Y1);
957             Y2 = av_clip_uint8(Y2);
958             U  = av_clip_uint8(U);
959             V  = av_clip_uint8(V);
960         }
961         if (hasAlpha) {
962             A1 = 1 << 18;
963             A2 = 1 << 18;
964             for (j = 0; j < lumFilterSize; j++) {
965                 A1 += alpSrc[j][i * 2    ] * lumFilter[j];
966                 A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
967             }
968             A1 >>= 19;
969             A2 >>= 19;
970             if ((A1 | A2) & 0x100) {
971                 A1 = av_clip_uint8(A1);
972                 A2 = av_clip_uint8(A2);
973             }
974         }
975
976         /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
977         r =  c->table_rV[V];
978         g = (c->table_gU[U] + c->table_gV[V]);
979         b =  c->table_bU[U];
980
981         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
982                       r, g, b, y, target, hasAlpha);
983     }
984 }
985
986 static av_always_inline void
987 yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
988                      const int16_t *ubuf[2], const int16_t *vbuf[2],
989                      const int16_t *abuf[2], uint8_t *dest, int dstW,
990                      int yalpha, int uvalpha, int y,
991                      enum PixelFormat target, int hasAlpha)
992 {
993     const int16_t *buf0  = buf[0],  *buf1  = buf[1],
994                   *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
995                   *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
996                   *abuf0 = hasAlpha ? abuf[0] : NULL,
997                   *abuf1 = hasAlpha ? abuf[1] : NULL;
998     int  yalpha1 = 4096 - yalpha;
999     int uvalpha1 = 4096 - uvalpha;
1000     int i;
1001
1002     for (i = 0; i < ((dstW + 1) >> 1); i++) {
1003         int Y1 = (buf0[i * 2]     * yalpha1  + buf1[i * 2]     * yalpha)  >> 19;
1004         int Y2 = (buf0[i * 2 + 1] * yalpha1  + buf1[i * 2 + 1] * yalpha)  >> 19;
1005         int U  = (ubuf0[i]        * uvalpha1 + ubuf1[i]        * uvalpha) >> 19;
1006         int V  = (vbuf0[i]        * uvalpha1 + vbuf1[i]        * uvalpha) >> 19;
1007         int A1, A2;
1008         const void *r, *g, *b;
1009
1010         Y1 = av_clip_uint8(Y1);
1011         Y2 = av_clip_uint8(Y2);
1012         U  = av_clip_uint8(U);
1013         V  = av_clip_uint8(V);
1014
1015         r =  c->table_rV[V];
1016         g = (c->table_gU[U] + c->table_gV[V]);
1017         b =  c->table_bU[U];
1018
1019         if (hasAlpha) {
1020             A1 = (abuf0[i * 2    ] * yalpha1 + abuf1[i * 2    ] * yalpha) >> 19;
1021             A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
1022             A1 = av_clip_uint8(A1);
1023             A2 = av_clip_uint8(A2);
1024         }
1025
1026         yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1027                       r, g, b, y, target, hasAlpha);
1028     }
1029 }
1030
1031 static av_always_inline void
1032 yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
1033                      const int16_t *ubuf[2], const int16_t *vbuf[2],
1034                      const int16_t *abuf0, uint8_t *dest, int dstW,
1035                      int uvalpha, int y, enum PixelFormat target,
1036                      int hasAlpha)
1037 {
1038     const int16_t *ubuf0 = ubuf[0], *vbuf0 = vbuf[0];
1039     int i;
1040
1041     if (uvalpha < 2048) {
1042         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1043             int Y1 = buf0[i * 2]     >> 7;
1044             int Y2 = buf0[i * 2 + 1] >> 7;
1045             int U  = ubuf0[i]        >> 7;
1046             int V  = vbuf0[i]        >> 7;
1047             int A1, A2;
1048             const void *r, *g, *b;
1049
1050             Y1 = av_clip_uint8(Y1);
1051             Y2 = av_clip_uint8(Y2);
1052             U  = av_clip_uint8(U);
1053             V  = av_clip_uint8(V);
1054
1055             r =  c->table_rV[V];
1056             g = (c->table_gU[U] + c->table_gV[V]);
1057             b =  c->table_bU[U];
1058
1059             if (hasAlpha) {
1060                 A1 = abuf0[i * 2    ] >> 7;
1061                 A2 = abuf0[i * 2 + 1] >> 7;
1062                 A1 = av_clip_uint8(A1);
1063                 A2 = av_clip_uint8(A2);
1064             }
1065
1066             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1067                           r, g, b, y, target, hasAlpha);
1068         }
1069     } else {
1070         const int16_t *ubuf1 = ubuf[1], *vbuf1 = vbuf[1];
1071         for (i = 0; i < ((dstW + 1) >> 1); i++) {
1072             int Y1 =  buf0[i * 2]          >> 7;
1073             int Y2 =  buf0[i * 2 + 1]      >> 7;
1074             int U  = (ubuf0[i] + ubuf1[i]) >> 8;
1075             int V  = (vbuf0[i] + vbuf1[i]) >> 8;
1076             int A1, A2;
1077             const void *r, *g, *b;
1078
1079             Y1 = av_clip_uint8(Y1);
1080             Y2 = av_clip_uint8(Y2);
1081             U  = av_clip_uint8(U);
1082             V  = av_clip_uint8(V);
1083
1084             r =  c->table_rV[V];
1085             g = (c->table_gU[U] + c->table_gV[V]);
1086             b =  c->table_bU[U];
1087
1088             if (hasAlpha) {
1089                 A1 = abuf0[i * 2    ] >> 7;
1090                 A2 = abuf0[i * 2 + 1] >> 7;
1091                 A1 = av_clip_uint8(A1);
1092                 A2 = av_clip_uint8(A2);
1093             }
1094
1095             yuv2rgb_write(dest, i, Y1, Y2, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
1096                           r, g, b, y, target, hasAlpha);
1097         }
1098     }
1099 }
1100
1101 #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1102 static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
1103                                 const int16_t **lumSrc, int lumFilterSize, \
1104                                 const int16_t *chrFilter, const int16_t **chrUSrc, \
1105                                 const int16_t **chrVSrc, int chrFilterSize, \
1106                                 const int16_t **alpSrc, uint8_t *dest, int dstW, \
1107                                 int y) \
1108 { \
1109     name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
1110                                   chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
1111                                   alpSrc, dest, dstW, y, fmt, hasAlpha); \
1112 }
1113 #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
1114 YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
1115 static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
1116                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1117                                 const int16_t *abuf[2], uint8_t *dest, int dstW, \
1118                                 int yalpha, int uvalpha, int y) \
1119 { \
1120     name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
1121                                   dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
1122 } \
1123  \
1124 static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
1125                                 const int16_t *ubuf[2], const int16_t *vbuf[2], \
1126                                 const int16_t *abuf0, uint8_t *dest, int dstW, \
1127                                 int uvalpha, int y) \
1128 { \
1129     name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
1130                                   dstW, uvalpha, y, fmt, hasAlpha); \
1131 }
1132
1133 #if CONFIG_SMALL
1134 YUV2RGBWRAPPER(yuv2rgb,,  32_1,  PIX_FMT_RGB32_1,   CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1135 YUV2RGBWRAPPER(yuv2rgb,,  32,    PIX_FMT_RGB32,     CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1136 #else
1137 #if CONFIG_SWSCALE_ALPHA
1138 YUV2RGBWRAPPER(yuv2rgb,, a32_1,  PIX_FMT_RGB32_1,   1)
1139 YUV2RGBWRAPPER(yuv2rgb,, a32,    PIX_FMT_RGB32,     1)
1140 #endif
1141 YUV2RGBWRAPPER(yuv2rgb,, x32_1,  PIX_FMT_RGB32_1,   0)
1142 YUV2RGBWRAPPER(yuv2rgb,, x32,    PIX_FMT_RGB32,     0)
1143 #endif
1144 YUV2RGBWRAPPER(yuv2, rgb, rgb24, PIX_FMT_RGB24,   0)
1145 YUV2RGBWRAPPER(yuv2, rgb, bgr24, PIX_FMT_BGR24,   0)
1146 YUV2RGBWRAPPER(yuv2rgb,,  16,    PIX_FMT_RGB565,    0)
1147 YUV2RGBWRAPPER(yuv2rgb,,  15,    PIX_FMT_RGB555,    0)
1148 YUV2RGBWRAPPER(yuv2rgb,,  12,    PIX_FMT_RGB444,    0)
1149 YUV2RGBWRAPPER(yuv2rgb,,   8,    PIX_FMT_RGB8,      0)
1150 YUV2RGBWRAPPER(yuv2rgb,,   4,    PIX_FMT_RGB4,      0)
1151 YUV2RGBWRAPPER(yuv2rgb,,   4b,   PIX_FMT_RGB4_BYTE, 0)
1152
1153 static av_always_inline void
1154 yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
1155                           const int16_t **lumSrc, int lumFilterSize,
1156                           const int16_t *chrFilter, const int16_t **chrUSrc,
1157                           const int16_t **chrVSrc, int chrFilterSize,
1158                           const int16_t **alpSrc, uint8_t *dest,
1159                           int dstW, int y, enum PixelFormat target, int hasAlpha)
1160 {
1161     int i;
1162     int step = (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) ? 3 : 4;
1163
1164     for (i = 0; i < dstW; i++) {
1165         int j;
1166         int Y = 0;
1167         int U = -128 << 19;
1168         int V = -128 << 19;
1169         int R, G, B, A;
1170
1171         for (j = 0; j < lumFilterSize; j++) {
1172             Y += lumSrc[j][i] * lumFilter[j];
1173         }
1174         for (j = 0; j < chrFilterSize; j++) {
1175             U += chrUSrc[j][i] * chrFilter[j];
1176             V += chrVSrc[j][i] * chrFilter[j];
1177         }
1178         Y >>= 10;
1179         U >>= 10;
1180         V >>= 10;
1181         if (hasAlpha) {
1182             A = 1 << 21;
1183             for (j = 0; j < lumFilterSize; j++) {
1184                 A += alpSrc[j][i] * lumFilter[j];
1185             }
1186             A >>= 19;
1187             if (A & 0x100)
1188                 A = av_clip_uint8(A);
1189         }
1190         Y -= c->yuv2rgb_y_offset;
1191         Y *= c->yuv2rgb_y_coeff;
1192         Y += 1 << 21;
1193         R = Y + V*c->yuv2rgb_v2r_coeff;
1194         G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
1195         B = Y +                          U*c->yuv2rgb_u2b_coeff;
1196         if ((R | G | B) & 0xC0000000) {
1197             R = av_clip_uintp2(R, 30);
1198             G = av_clip_uintp2(G, 30);
1199             B = av_clip_uintp2(B, 30);
1200         }
1201
1202         switch(target) {
1203         case PIX_FMT_ARGB:
1204             dest[0] = hasAlpha ? A : 255;
1205             dest[1] = R >> 22;
1206             dest[2] = G >> 22;
1207             dest[3] = B >> 22;
1208             break;
1209         case PIX_FMT_RGB24:
1210             dest[0] = R >> 22;
1211             dest[1] = G >> 22;
1212             dest[2] = B >> 22;
1213             break;
1214         case PIX_FMT_RGBA:
1215             dest[0] = R >> 22;
1216             dest[1] = G >> 22;
1217             dest[2] = B >> 22;
1218             dest[3] = hasAlpha ? A : 255;
1219             break;
1220         case PIX_FMT_ABGR:
1221             dest[0] = hasAlpha ? A : 255;
1222             dest[1] = B >> 22;
1223             dest[2] = G >> 22;
1224             dest[3] = R >> 22;
1225             dest += 4;
1226             break;
1227         case PIX_FMT_BGR24:
1228             dest[0] = B >> 22;
1229             dest[1] = G >> 22;
1230             dest[2] = R >> 22;
1231             break;
1232         case PIX_FMT_BGRA:
1233             dest[0] = B >> 22;
1234             dest[1] = G >> 22;
1235             dest[2] = R >> 22;
1236             dest[3] = hasAlpha ? A : 255;
1237             break;
1238         }
1239         dest += step;
1240     }
1241 }
1242
1243 #if CONFIG_SMALL
1244 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1245 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1246 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1247 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB,  CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1248 #else
1249 #if CONFIG_SWSCALE_ALPHA
1250 YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA,  1)
1251 YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR,  1)
1252 YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA,  1)
1253 YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB,  1)
1254 #endif
1255 YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, PIX_FMT_BGRA,  0)
1256 YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, PIX_FMT_ABGR,  0)
1257 YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, PIX_FMT_RGBA,  0)
1258 YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, PIX_FMT_ARGB,  0)
1259 #endif
1260 YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full,  PIX_FMT_BGR24, 0)
1261 YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full,  PIX_FMT_RGB24, 0)
1262
1263 void ff_sws_init_output_funcs(SwsContext *c,
1264                               yuv2planar1_fn *yuv2plane1,
1265                               yuv2planarX_fn *yuv2planeX,
1266                               yuv2interleavedX_fn *yuv2nv12cX,
1267                               yuv2packed1_fn *yuv2packed1,
1268                               yuv2packed2_fn *yuv2packed2,
1269                               yuv2packedX_fn *yuv2packedX)
1270 {
1271     enum PixelFormat dstFormat = c->dstFormat;
1272
1273     if (is16BPS(dstFormat)) {
1274         *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c  : yuv2planeX_16LE_c;
1275         *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c  : yuv2plane1_16LE_c;
1276     } else if (is9_OR_10BPS(dstFormat)) {
1277         if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
1278             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c  : yuv2planeX_9LE_c;
1279             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c  : yuv2plane1_9LE_c;
1280         } else {
1281             *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c  : yuv2planeX_10LE_c;
1282             *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c  : yuv2plane1_10LE_c;
1283         }
1284     } else {
1285         *yuv2plane1 = yuv2plane1_8_c;
1286         *yuv2planeX = yuv2planeX_8_c;
1287         if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)
1288             *yuv2nv12cX = yuv2nv12cX_c;
1289     }
1290
1291     if(c->flags & SWS_FULL_CHR_H_INT) {
1292         switch (dstFormat) {
1293             case PIX_FMT_RGBA:
1294 #if CONFIG_SMALL
1295                 *yuv2packedX = yuv2rgba32_full_X_c;
1296 #else
1297 #if CONFIG_SWSCALE_ALPHA
1298                 if (c->alpPixBuf) {
1299                     *yuv2packedX = yuv2rgba32_full_X_c;
1300                 } else
1301 #endif /* CONFIG_SWSCALE_ALPHA */
1302                 {
1303                     *yuv2packedX = yuv2rgbx32_full_X_c;
1304                 }
1305 #endif /* !CONFIG_SMALL */
1306                 break;
1307             case PIX_FMT_ARGB:
1308 #if CONFIG_SMALL
1309                 *yuv2packedX = yuv2argb32_full_X_c;
1310 #else
1311 #if CONFIG_SWSCALE_ALPHA
1312                 if (c->alpPixBuf) {
1313                     *yuv2packedX = yuv2argb32_full_X_c;
1314                 } else
1315 #endif /* CONFIG_SWSCALE_ALPHA */
1316                 {
1317                     *yuv2packedX = yuv2xrgb32_full_X_c;
1318                 }
1319 #endif /* !CONFIG_SMALL */
1320                 break;
1321             case PIX_FMT_BGRA:
1322 #if CONFIG_SMALL
1323                 *yuv2packedX = yuv2bgra32_full_X_c;
1324 #else
1325 #if CONFIG_SWSCALE_ALPHA
1326                 if (c->alpPixBuf) {
1327                     *yuv2packedX = yuv2bgra32_full_X_c;
1328                 } else
1329 #endif /* CONFIG_SWSCALE_ALPHA */
1330                 {
1331                     *yuv2packedX = yuv2bgrx32_full_X_c;
1332                 }
1333 #endif /* !CONFIG_SMALL */
1334                 break;
1335             case PIX_FMT_ABGR:
1336 #if CONFIG_SMALL
1337                 *yuv2packedX = yuv2abgr32_full_X_c;
1338 #else
1339 #if CONFIG_SWSCALE_ALPHA
1340                 if (c->alpPixBuf) {
1341                     *yuv2packedX = yuv2abgr32_full_X_c;
1342                 } else
1343 #endif /* CONFIG_SWSCALE_ALPHA */
1344                 {
1345                     *yuv2packedX = yuv2xbgr32_full_X_c;
1346                 }
1347 #endif /* !CONFIG_SMALL */
1348                 break;
1349             case PIX_FMT_RGB24:
1350             *yuv2packedX = yuv2rgb24_full_X_c;
1351             break;
1352         case PIX_FMT_BGR24:
1353             *yuv2packedX = yuv2bgr24_full_X_c;
1354             break;
1355         }
1356     } else {
1357         switch (dstFormat) {
1358         case PIX_FMT_RGB48LE:
1359             *yuv2packed1 = yuv2rgb48le_1_c;
1360             *yuv2packed2 = yuv2rgb48le_2_c;
1361             *yuv2packedX = yuv2rgb48le_X_c;
1362             break;
1363         case PIX_FMT_RGB48BE:
1364             *yuv2packed1 = yuv2rgb48be_1_c;
1365             *yuv2packed2 = yuv2rgb48be_2_c;
1366             *yuv2packedX = yuv2rgb48be_X_c;
1367             break;
1368         case PIX_FMT_BGR48LE:
1369             *yuv2packed1 = yuv2bgr48le_1_c;
1370             *yuv2packed2 = yuv2bgr48le_2_c;
1371             *yuv2packedX = yuv2bgr48le_X_c;
1372             break;
1373         case PIX_FMT_BGR48BE:
1374             *yuv2packed1 = yuv2bgr48be_1_c;
1375             *yuv2packed2 = yuv2bgr48be_2_c;
1376             *yuv2packedX = yuv2bgr48be_X_c;
1377             break;
1378         case PIX_FMT_RGB32:
1379         case PIX_FMT_BGR32:
1380 #if CONFIG_SMALL
1381             *yuv2packed1 = yuv2rgb32_1_c;
1382             *yuv2packed2 = yuv2rgb32_2_c;
1383             *yuv2packedX = yuv2rgb32_X_c;
1384 #else
1385 #if CONFIG_SWSCALE_ALPHA
1386                 if (c->alpPixBuf) {
1387                     *yuv2packed1 = yuv2rgba32_1_c;
1388                     *yuv2packed2 = yuv2rgba32_2_c;
1389                     *yuv2packedX = yuv2rgba32_X_c;
1390                 } else
1391 #endif /* CONFIG_SWSCALE_ALPHA */
1392                 {
1393                     *yuv2packed1 = yuv2rgbx32_1_c;
1394                     *yuv2packed2 = yuv2rgbx32_2_c;
1395                     *yuv2packedX = yuv2rgbx32_X_c;
1396                 }
1397 #endif /* !CONFIG_SMALL */
1398             break;
1399         case PIX_FMT_RGB32_1:
1400         case PIX_FMT_BGR32_1:
1401 #if CONFIG_SMALL
1402                 *yuv2packed1 = yuv2rgb32_1_1_c;
1403                 *yuv2packed2 = yuv2rgb32_1_2_c;
1404                 *yuv2packedX = yuv2rgb32_1_X_c;
1405 #else
1406 #if CONFIG_SWSCALE_ALPHA
1407                 if (c->alpPixBuf) {
1408                     *yuv2packed1 = yuv2rgba32_1_1_c;
1409                     *yuv2packed2 = yuv2rgba32_1_2_c;
1410                     *yuv2packedX = yuv2rgba32_1_X_c;
1411                 } else
1412 #endif /* CONFIG_SWSCALE_ALPHA */
1413                 {
1414                     *yuv2packed1 = yuv2rgbx32_1_1_c;
1415                     *yuv2packed2 = yuv2rgbx32_1_2_c;
1416                     *yuv2packedX = yuv2rgbx32_1_X_c;
1417                 }
1418 #endif /* !CONFIG_SMALL */
1419                 break;
1420         case PIX_FMT_RGB24:
1421             *yuv2packed1 = yuv2rgb24_1_c;
1422             *yuv2packed2 = yuv2rgb24_2_c;
1423             *yuv2packedX = yuv2rgb24_X_c;
1424             break;
1425         case PIX_FMT_BGR24:
1426             *yuv2packed1 = yuv2bgr24_1_c;
1427             *yuv2packed2 = yuv2bgr24_2_c;
1428             *yuv2packedX = yuv2bgr24_X_c;
1429             break;
1430         case PIX_FMT_RGB565LE:
1431         case PIX_FMT_RGB565BE:
1432         case PIX_FMT_BGR565LE:
1433         case PIX_FMT_BGR565BE:
1434             *yuv2packed1 = yuv2rgb16_1_c;
1435             *yuv2packed2 = yuv2rgb16_2_c;
1436             *yuv2packedX = yuv2rgb16_X_c;
1437             break;
1438         case PIX_FMT_RGB555LE:
1439         case PIX_FMT_RGB555BE:
1440         case PIX_FMT_BGR555LE:
1441         case PIX_FMT_BGR555BE:
1442             *yuv2packed1 = yuv2rgb15_1_c;
1443             *yuv2packed2 = yuv2rgb15_2_c;
1444             *yuv2packedX = yuv2rgb15_X_c;
1445             break;
1446         case PIX_FMT_RGB444LE:
1447         case PIX_FMT_RGB444BE:
1448         case PIX_FMT_BGR444LE:
1449         case PIX_FMT_BGR444BE:
1450             *yuv2packed1 = yuv2rgb12_1_c;
1451             *yuv2packed2 = yuv2rgb12_2_c;
1452             *yuv2packedX = yuv2rgb12_X_c;
1453             break;
1454         case PIX_FMT_RGB8:
1455         case PIX_FMT_BGR8:
1456             *yuv2packed1 = yuv2rgb8_1_c;
1457             *yuv2packed2 = yuv2rgb8_2_c;
1458             *yuv2packedX = yuv2rgb8_X_c;
1459             break;
1460         case PIX_FMT_RGB4:
1461         case PIX_FMT_BGR4:
1462             *yuv2packed1 = yuv2rgb4_1_c;
1463             *yuv2packed2 = yuv2rgb4_2_c;
1464             *yuv2packedX = yuv2rgb4_X_c;
1465             break;
1466         case PIX_FMT_RGB4_BYTE:
1467         case PIX_FMT_BGR4_BYTE:
1468             *yuv2packed1 = yuv2rgb4b_1_c;
1469             *yuv2packed2 = yuv2rgb4b_2_c;
1470             *yuv2packedX = yuv2rgb4b_X_c;
1471             break;
1472         }
1473     }
1474     switch (dstFormat) {
1475     case PIX_FMT_MONOWHITE:
1476         *yuv2packed1 = yuv2monowhite_1_c;
1477         *yuv2packed2 = yuv2monowhite_2_c;
1478         *yuv2packedX = yuv2monowhite_X_c;
1479         break;
1480     case PIX_FMT_MONOBLACK:
1481         *yuv2packed1 = yuv2monoblack_1_c;
1482         *yuv2packed2 = yuv2monoblack_2_c;
1483         *yuv2packedX = yuv2monoblack_X_c;
1484         break;
1485     case PIX_FMT_YUYV422:
1486         *yuv2packed1 = yuv2yuyv422_1_c;
1487         *yuv2packed2 = yuv2yuyv422_2_c;
1488         *yuv2packedX = yuv2yuyv422_X_c;
1489         break;
1490     case PIX_FMT_UYVY422:
1491         *yuv2packed1 = yuv2uyvy422_1_c;
1492         *yuv2packed2 = yuv2uyvy422_2_c;
1493         *yuv2packedX = yuv2uyvy422_X_c;
1494         break;
1495     }
1496 }