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