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