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