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