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