]> git.sesse.net Git - ffmpeg/blob - libswscale/swscale.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libswscale / swscale.c
1 /*
2  * Copyright (C) 2001-2003 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 /*
22   supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
23   supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
24   {BGR,RGB}{1,4,8,15,16} support dithering
25
26   unscaled special converters (YV12=I420=IYUV, Y800=Y8)
27   YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
28   x -> x
29   YUV9 -> YV12
30   YUV9/YV12 -> Y800
31   Y800 -> YUV9/YV12
32   BGR24 -> BGR32 & RGB24 -> RGB32
33   BGR32 -> BGR24 & RGB32 -> RGB24
34   BGR15 -> BGR16
35 */
36
37 /*
38 tested special converters (most are tested actually, but I did not write it down ...)
39  YV12 -> BGR12/BGR16
40  YV12 -> YV12
41  BGR15 -> BGR16
42  BGR16 -> BGR16
43  YVU9 -> YV12
44
45 untested special converters
46   YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
47   YV12/I420 -> YV12/I420
48   YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
49   BGR24 -> BGR32 & RGB24 -> RGB32
50   BGR32 -> BGR24 & RGB32 -> RGB24
51   BGR24 -> YV12
52 */
53
54 #include <inttypes.h>
55 #include <string.h>
56 #include <math.h>
57 #include <stdio.h>
58 #include "config.h"
59 #include <assert.h>
60 #include "swscale.h"
61 #include "swscale_internal.h"
62 #include "rgb2rgb.h"
63 #include "libavutil/avassert.h"
64 #include "libavutil/intreadwrite.h"
65 #include "libavutil/x86_cpu.h"
66 #include "libavutil/cpu.h"
67 #include "libavutil/avutil.h"
68 #include "libavutil/mathematics.h"
69 #include "libavutil/bswap.h"
70 #include "libavutil/pixdesc.h"
71
72 #undef MOVNTQ
73 #undef PAVGB
74
75 #define DITHER1XBPP
76
77 #define isPacked(x)         (       \
78            (x)==PIX_FMT_PAL8        \
79         || (x)==PIX_FMT_YUYV422     \
80         || (x)==PIX_FMT_UYVY422     \
81         || (x)==PIX_FMT_GRAY8A       \
82         || isAnyRGB(x)              \
83     )
84
85 #define RGB2YUV_SHIFT 15
86 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
87 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
88 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
89 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
90 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
91 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
92 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
93 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
94 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
95
96 static const double rgb2yuv_table[8][9]={
97     {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
98     {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
99     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
100     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
101     {0.59  , 0.11  , 0.30  , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
102     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
103     {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
104     {0.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
105 };
106
107 /*
108 NOTES
109 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
110
111 TODO
112 more intelligent misalignment avoidance for the horizontal scaler
113 write special vertical cubic upscale version
114 optimize C code (YV12 / minmax)
115 add support for packed pixel YUV input & output
116 add support for Y8 output
117 optimize BGR24 & BGR32
118 add BGR4 output support
119 write special BGR->BGR scaler
120 */
121
122 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
123 {  1,   3,   1,   3,   1,   3,   1,   3, },
124 {  2,   0,   2,   0,   2,   0,   2,   0, },
125 };
126
127 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
128 {  6,   2,   6,   2,   6,   2,   6,   2, },
129 {  0,   4,   0,   4,   0,   4,   0,   4, },
130 };
131
132 DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
133 {  8,   4,  11,   7,   8,   4,  11,   7, },
134 {  2,  14,   1,  13,   2,  14,   1,  13, },
135 { 10,   6,   9,   5,  10,   6,   9,   5, },
136 {  0,  12,   3,  15,   0,  12,   3,  15, },
137 };
138
139 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
140 { 17,   9,  23,  15,  16,   8,  22,  14, },
141 {  5,  29,   3,  27,   4,  28,   2,  26, },
142 { 21,  13,  19,  11,  20,  12,  18,  10, },
143 {  0,  24,   6,  30,   1,  25,   7,  31, },
144 { 16,   8,  22,  14,  17,   9,  23,  15, },
145 {  4,  28,   2,  26,   5,  29,   3,  27, },
146 { 20,  12,  18,  10,  21,  13,  19,  11, },
147 {  1,  25,   7,  31,   0,  24,   6,  30, },
148 };
149
150 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
151 {  0,  55,  14,  68,   3,  58,  17,  72, },
152 { 37,  18,  50,  32,  40,  22,  54,  35, },
153 {  9,  64,   5,  59,  13,  67,   8,  63, },
154 { 46,  27,  41,  23,  49,  31,  44,  26, },
155 {  2,  57,  16,  71,   1,  56,  15,  70, },
156 { 39,  21,  52,  34,  38,  19,  51,  33, },
157 { 11,  66,   7,  62,  10,  65,   6,  60, },
158 { 48,  30,  43,  25,  47,  29,  42,  24, },
159 };
160
161 #if 1
162 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
163 {117,  62, 158, 103, 113,  58, 155, 100, },
164 { 34, 199,  21, 186,  31, 196,  17, 182, },
165 {144,  89, 131,  76, 141,  86, 127,  72, },
166 {  0, 165,  41, 206,  10, 175,  52, 217, },
167 {110,  55, 151,  96, 120,  65, 162, 107, },
168 { 28, 193,  14, 179,  38, 203,  24, 189, },
169 {138,  83, 124,  69, 148,  93, 134,  79, },
170 {  7, 172,  48, 213,   3, 168,  45, 210, },
171 };
172 #elif 1
173 // tries to correct a gamma of 1.5
174 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
175 {  0, 143,  18, 200,   2, 156,  25, 215, },
176 { 78,  28, 125,  64,  89,  36, 138,  74, },
177 { 10, 180,   3, 161,  16, 195,   8, 175, },
178 {109,  51,  93,  38, 121,  60, 105,  47, },
179 {  1, 152,  23, 210,   0, 147,  20, 205, },
180 { 85,  33, 134,  71,  81,  30, 130,  67, },
181 { 14, 190,   6, 171,  12, 185,   5, 166, },
182 {117,  57, 101,  44, 113,  54,  97,  41, },
183 };
184 #elif 1
185 // tries to correct a gamma of 2.0
186 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
187 {  0, 124,   8, 193,   0, 140,  12, 213, },
188 { 55,  14, 104,  42,  66,  19, 119,  52, },
189 {  3, 168,   1, 145,   6, 187,   3, 162, },
190 { 86,  31,  70,  21,  99,  39,  82,  28, },
191 {  0, 134,  11, 206,   0, 129,   9, 200, },
192 { 62,  17, 114,  48,  58,  16, 109,  45, },
193 {  5, 181,   2, 157,   4, 175,   1, 151, },
194 { 95,  36,  78,  26,  90,  34,  74,  24, },
195 };
196 #else
197 // tries to correct a gamma of 2.5
198 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
199 {  0, 107,   3, 187,   0, 125,   6, 212, },
200 { 39,   7,  86,  28,  49,  11, 102,  36, },
201 {  1, 158,   0, 131,   3, 180,   1, 151, },
202 { 68,  19,  52,  12,  81,  25,  64,  17, },
203 {  0, 119,   5, 203,   0, 113,   4, 195, },
204 { 45,   9,  96,  33,  42,   8,  91,  30, },
205 {  2, 172,   1, 144,   2, 165,   0, 137, },
206 { 77,  23,  60,  15,  72,  21,  56,  14, },
207 };
208 #endif
209
210 DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
211 {
212   {   0,  1,  0,  1,  0,  1,  0,  1,},
213   {   1,  0,  1,  0,  1,  0,  1,  0,},
214   {   0,  1,  0,  1,  0,  1,  0,  1,},
215   {   1,  0,  1,  0,  1,  0,  1,  0,},
216   {   0,  1,  0,  1,  0,  1,  0,  1,},
217   {   1,  0,  1,  0,  1,  0,  1,  0,},
218   {   0,  1,  0,  1,  0,  1,  0,  1,},
219   {   1,  0,  1,  0,  1,  0,  1,  0,},
220 },{
221   {   1,  2,  1,  2,  1,  2,  1,  2,},
222   {   3,  0,  3,  0,  3,  0,  3,  0,},
223   {   1,  2,  1,  2,  1,  2,  1,  2,},
224   {   3,  0,  3,  0,  3,  0,  3,  0,},
225   {   1,  2,  1,  2,  1,  2,  1,  2,},
226   {   3,  0,  3,  0,  3,  0,  3,  0,},
227   {   1,  2,  1,  2,  1,  2,  1,  2,},
228   {   3,  0,  3,  0,  3,  0,  3,  0,},
229 },{
230   {   2,  4,  3,  5,  2,  4,  3,  5,},
231   {   6,  0,  7,  1,  6,  0,  7,  1,},
232   {   3,  5,  2,  4,  3,  5,  2,  4,},
233   {   7,  1,  6,  0,  7,  1,  6,  0,},
234   {   2,  4,  3,  5,  2,  4,  3,  5,},
235   {   6,  0,  7,  1,  6,  0,  7,  1,},
236   {   3,  5,  2,  4,  3,  5,  2,  4,},
237   {   7,  1,  6,  0,  7,  1,  6,  0,},
238 },{
239   {   4,  8,  7, 11,  4,  8,  7, 11,},
240   {  12,  0, 15,  3, 12,  0, 15,  3,},
241   {   6, 10,  5,  9,  6, 10,  5,  9,},
242   {  14,  2, 13,  1, 14,  2, 13,  1,},
243   {   4,  8,  7, 11,  4,  8,  7, 11,},
244   {  12,  0, 15,  3, 12,  0, 15,  3,},
245   {   6, 10,  5,  9,  6, 10,  5,  9,},
246   {  14,  2, 13,  1, 14,  2, 13,  1,},
247 },{
248   {   9, 17, 15, 23,  8, 16, 14, 22,},
249   {  25,  1, 31,  7, 24,  0, 30,  6,},
250   {  13, 21, 11, 19, 12, 20, 10, 18,},
251   {  29,  5, 27,  3, 28,  4, 26,  2,},
252   {   8, 16, 14, 22,  9, 17, 15, 23,},
253   {  24,  0, 30,  6, 25,  1, 31,  7,},
254   {  12, 20, 10, 18, 13, 21, 11, 19,},
255   {  28,  4, 26,  2, 29,  5, 27,  3,},
256 },{
257   {  18, 34, 30, 46, 17, 33, 29, 45,},
258   {  50,  2, 62, 14, 49,  1, 61, 13,},
259   {  26, 42, 22, 38, 25, 41, 21, 37,},
260   {  58, 10, 54,  6, 57,  9, 53,  5,},
261   {  16, 32, 28, 44, 19, 35, 31, 47,},
262   {  48,  0, 60, 12, 51,  3, 63, 15,},
263   {  24, 40, 20, 36, 27, 43, 23, 39,},
264   {  56,  8, 52,  4, 59, 11, 55,  7,},
265 },{
266   {  18, 34, 30, 46, 17, 33, 29, 45,},
267   {  50,  2, 62, 14, 49,  1, 61, 13,},
268   {  26, 42, 22, 38, 25, 41, 21, 37,},
269   {  58, 10, 54,  6, 57,  9, 53,  5,},
270   {  16, 32, 28, 44, 19, 35, 31, 47,},
271   {  48,  0, 60, 12, 51,  3, 63, 15,},
272   {  24, 40, 20, 36, 27, 43, 23, 39,},
273   {  56,  8, 52,  4, 59, 11, 55,  7,},
274 },{
275   {  36, 68, 60, 92, 34, 66, 58, 90,},
276   { 100,  4,124, 28, 98,  2,122, 26,},
277   {  52, 84, 44, 76, 50, 82, 42, 74,},
278   { 116, 20,108, 12,114, 18,106, 10,},
279   {  32, 64, 56, 88, 38, 70, 62, 94,},
280   {  96,  0,120, 24,102,  6,126, 30,},
281   {  48, 80, 40, 72, 54, 86, 46, 78,},
282   { 112, 16,104,  8,118, 22,110, 14,},
283 }};
284
285 uint16_t dither_scale[15][16]={
286 {    2,    3,    3,    5,    5,    5,    5,    5,    5,    5,    5,    5,    5,    5,    5,    5,},
287 {    2,    3,    7,    7,   13,   13,   25,   25,   25,   25,   25,   25,   25,   25,   25,   25,},
288 {    3,    3,    4,   15,   15,   29,   57,   57,   57,  113,  113,  113,  113,  113,  113,  113,},
289 {    3,    4,    4,    5,   31,   31,   61,  121,  241,  241,  241,  241,  481,  481,  481,  481,},
290 {    3,    4,    5,    5,    6,   63,   63,  125,  249,  497,  993,  993,  993,  993,  993, 1985,},
291 {    3,    5,    6,    6,    6,    7,  127,  127,  253,  505, 1009, 2017, 4033, 4033, 4033, 4033,},
292 {    3,    5,    6,    7,    7,    7,    8,  255,  255,  509, 1017, 2033, 4065, 8129,16257,16257,},
293 {    3,    5,    6,    8,    8,    8,    8,    9,  511,  511, 1021, 2041, 4081, 8161,16321,32641,},
294 {    3,    5,    7,    8,    9,    9,    9,    9,   10, 1023, 1023, 2045, 4089, 8177,16353,32705,},
295 {    3,    5,    7,    8,   10,   10,   10,   10,   10,   11, 2047, 2047, 4093, 8185,16369,32737,},
296 {    3,    5,    7,    8,   10,   11,   11,   11,   11,   11,   12, 4095, 4095, 8189,16377,32753,},
297 {    3,    5,    7,    9,   10,   12,   12,   12,   12,   12,   12,   13, 8191, 8191,16381,32761,},
298 {    3,    5,    7,    9,   10,   12,   13,   13,   13,   13,   13,   13,   14,16383,16383,32765,},
299 {    3,    5,    7,    9,   10,   12,   14,   14,   14,   14,   14,   14,   14,   15,32767,32767,},
300 {    3,    5,    7,    9,   11,   12,   14,   15,   15,   15,   15,   15,   15,   15,   16,65535,},
301 };
302
303 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
304                                                     const int16_t *chrFilter, const int16_t **chrUSrc,
305                                                     const int16_t **chrVSrc, int chrFilterSize,
306                                                     const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
307                                                     int dstW, int chrDstW, int big_endian, int output_bits)
308 {
309     //FIXME Optimize (just quickly written not optimized..)
310     int i;
311     int shift = 11 + 16 - output_bits;
312
313 #define output_pixel(pos, val) \
314     if (big_endian) { \
315         if (output_bits == 16) { \
316             AV_WB16(pos, av_clip_uint16(val >> shift)); \
317         } else { \
318             AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
319         } \
320     } else { \
321         if (output_bits == 16) { \
322             AV_WL16(pos, av_clip_uint16(val >> shift)); \
323         } else { \
324             AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
325         } \
326     }
327     for (i = 0; i < dstW; i++) {
328         int val = 1 << (26-output_bits);
329         int j;
330
331         for (j = 0; j < lumFilterSize; j++)
332             val += lumSrc[j][i] * lumFilter[j];
333
334         output_pixel(&dest[i], val);
335     }
336
337     if (uDest) {
338         for (i = 0; i < chrDstW; i++) {
339             int u = 1 << (26-output_bits);
340             int v = 1 << (26-output_bits);
341             int j;
342
343             for (j = 0; j < chrFilterSize; j++) {
344                 u += chrUSrc[j][i] * chrFilter[j];
345                 v += chrVSrc[j][i] * chrFilter[j];
346             }
347
348             output_pixel(&uDest[i], u);
349             output_pixel(&vDest[i], v);
350         }
351     }
352
353     if (CONFIG_SWSCALE_ALPHA && aDest) {
354         for (i = 0; i < dstW; i++) {
355             int val = 1 << (26-output_bits);
356             int j;
357
358             for (j = 0; j < lumFilterSize; j++)
359                 val += alpSrc[j][i] * lumFilter[j];
360
361             output_pixel(&aDest[i], val);
362         }
363     }
364 }
365
366 #define yuv2NBPS(bits, BE_LE, is_be) \
367 static void yuv2yuvX ## bits ## BE_LE ## _c(const int16_t *lumFilter, \
368                               const int16_t **lumSrc, int lumFilterSize, \
369                               const int16_t *chrFilter, const int16_t **chrUSrc, \
370                               const int16_t **chrVSrc, \
371                               int chrFilterSize, const int16_t **alpSrc, \
372                               uint16_t *dest, uint16_t *uDest, uint16_t *vDest, \
373                               uint16_t *aDest, int dstW, int chrDstW) \
374 { \
375     yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize, \
376                            chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
377                            alpSrc, \
378                            dest, uDest, vDest, aDest, \
379                            dstW, chrDstW, is_be, bits); \
380 }
381 yuv2NBPS( 9, BE, 1);
382 yuv2NBPS( 9, LE, 0);
383 yuv2NBPS(10, BE, 1);
384 yuv2NBPS(10, LE, 0);
385 yuv2NBPS(16, BE, 1);
386 yuv2NBPS(16, LE, 0);
387
388 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
389                                  const int16_t *chrFilter, const int16_t **chrUSrc, const int16_t **chrVSrc, int chrFilterSize,
390                                  const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
391                                  enum PixelFormat dstFormat)
392 {
393     if (isNBPS(dstFormat)) {
394         const int depth = av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1+1;
395         yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
396                               chrFilter, chrUSrc, chrVSrc, chrFilterSize,
397                               alpSrc,
398                               dest, uDest, vDest, aDest,
399                               dstW, chrDstW, isBE(dstFormat), depth);
400     } else {
401         if (isBE(dstFormat)) {
402             yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
403                                    chrFilter, chrUSrc, chrVSrc, chrFilterSize,
404                                    alpSrc,
405                                    dest, uDest, vDest, aDest,
406                                    dstW, chrDstW, 1, 16);
407         } else {
408             yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
409                                    chrFilter, chrUSrc, chrVSrc, chrFilterSize,
410                                    alpSrc,
411                                    dest, uDest, vDest, aDest,
412                                    dstW, chrDstW, 0, 16);
413         }
414     }
415 }
416
417 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
418                                const int16_t *chrFilter, const int16_t **chrUSrc,
419                                const int16_t **chrVSrc, int chrFilterSize,
420                                const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
421 {
422     //FIXME Optimize (just quickly written not optimized..)
423     int i;
424     for (i=0; i<dstW; i++) {
425         int val=1<<18;
426         int j;
427         for (j=0; j<lumFilterSize; j++)
428             val += lumSrc[j][i] * lumFilter[j];
429
430         dest[i]= av_clip_uint8(val>>19);
431     }
432
433     if (uDest)
434         for (i=0; i<chrDstW; i++) {
435             int u=1<<18;
436             int v=1<<18;
437             int j;
438             for (j=0; j<chrFilterSize; j++) {
439                 u += chrUSrc[j][i] * chrFilter[j];
440                 v += chrVSrc[j][i] * chrFilter[j];
441             }
442
443             uDest[i]= av_clip_uint8(u>>19);
444             vDest[i]= av_clip_uint8(v>>19);
445         }
446
447     if (CONFIG_SWSCALE_ALPHA && aDest)
448         for (i=0; i<dstW; i++) {
449             int val=1<<18;
450             int j;
451             for (j=0; j<lumFilterSize; j++)
452                 val += alpSrc[j][i] * lumFilter[j];
453
454             aDest[i]= av_clip_uint8(val>>19);
455         }
456
457 }
458
459 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
460                                 const int16_t *chrFilter, const int16_t **chrUSrc,
461                                 const int16_t **chrVSrc, int chrFilterSize,
462                                 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
463 {
464     //FIXME Optimize (just quickly written not optimized..)
465     int i;
466     for (i=0; i<dstW; i++) {
467         int val=1<<18;
468         int j;
469         for (j=0; j<lumFilterSize; j++)
470             val += lumSrc[j][i] * lumFilter[j];
471
472         dest[i]= av_clip_uint8(val>>19);
473     }
474
475     if (!uDest)
476         return;
477
478     if (dstFormat == PIX_FMT_NV12)
479         for (i=0; i<chrDstW; i++) {
480             int u=1<<18;
481             int v=1<<18;
482             int j;
483             for (j=0; j<chrFilterSize; j++) {
484                 u += chrUSrc[j][i] * chrFilter[j];
485                 v += chrVSrc[j][i] * chrFilter[j];
486             }
487
488             uDest[2*i]= av_clip_uint8(u>>19);
489             uDest[2*i+1]= av_clip_uint8(v>>19);
490         }
491     else
492         for (i=0; i<chrDstW; i++) {
493             int u=1<<18;
494             int v=1<<18;
495             int j;
496             for (j=0; j<chrFilterSize; j++) {
497                 u += chrUSrc[j][i] * chrFilter[j];
498                 v += chrVSrc[j][i] * chrFilter[j];
499             }
500
501             uDest[2*i]= av_clip_uint8(v>>19);
502             uDest[2*i+1]= av_clip_uint8(u>>19);
503         }
504 }
505
506 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
507     for (i=0; i<(dstW>>1); i++) {\
508         int j;\
509         int Y1 = 1<<18;\
510         int Y2 = 1<<18;\
511         int U  = 1<<18;\
512         int V  = 1<<18;\
513         int av_unused A1, A2;\
514         type av_unused *r, *b, *g;\
515         const int i2= 2*i;\
516         \
517         for (j=0; j<lumFilterSize; j++) {\
518             Y1 += lumSrc[j][i2] * lumFilter[j];\
519             Y2 += lumSrc[j][i2+1] * lumFilter[j];\
520         }\
521         for (j=0; j<chrFilterSize; j++) {\
522             U += chrUSrc[j][i] * chrFilter[j];\
523             V += chrVSrc[j][i] * chrFilter[j];\
524         }\
525         Y1>>=19;\
526         Y2>>=19;\
527         U >>=19;\
528         V >>=19;\
529         if (alpha) {\
530             A1 = 1<<18;\
531             A2 = 1<<18;\
532             for (j=0; j<lumFilterSize; j++) {\
533                 A1 += alpSrc[j][i2  ] * lumFilter[j];\
534                 A2 += alpSrc[j][i2+1] * lumFilter[j];\
535             }\
536             A1>>=19;\
537             A2>>=19;\
538         }
539
540 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
541         YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
542         if ((Y1|Y2|U|V)&256) {\
543             if (Y1>255)   Y1=255; \
544             else if (Y1<0)Y1=0;   \
545             if (Y2>255)   Y2=255; \
546             else if (Y2<0)Y2=0;   \
547             if (U>255)    U=255;  \
548             else if (U<0) U=0;    \
549             if (V>255)    V=255;  \
550             else if (V<0) V=0;    \
551         }\
552         if (alpha && ((A1|A2)&256)) {\
553             A1=av_clip_uint8(A1);\
554             A2=av_clip_uint8(A2);\
555         }
556
557 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
558     for (i=0; i<dstW; i++) {\
559         int j;\
560         int Y = 0;\
561         int U = -128<<19;\
562         int V = -128<<19;\
563         int av_unused A;\
564         int R,G,B;\
565         \
566         for (j=0; j<lumFilterSize; j++) {\
567             Y += lumSrc[j][i     ] * lumFilter[j];\
568         }\
569         for (j=0; j<chrFilterSize; j++) {\
570             U += chrUSrc[j][i] * chrFilter[j];\
571             V += chrVSrc[j][i] * chrFilter[j];\
572         }\
573         Y >>=10;\
574         U >>=10;\
575         V >>=10;\
576         if (alpha) {\
577             A = rnd;\
578             for (j=0; j<lumFilterSize; j++)\
579                 A += alpSrc[j][i     ] * lumFilter[j];\
580             A >>=19;\
581             if (A&256)\
582                 A = av_clip_uint8(A);\
583         }
584
585 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
586     YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
587         Y-= c->yuv2rgb_y_offset;\
588         Y*= c->yuv2rgb_y_coeff;\
589         Y+= rnd;\
590         R= Y + V*c->yuv2rgb_v2r_coeff;\
591         G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
592         B= Y +                          U*c->yuv2rgb_u2b_coeff;\
593         if ((R|G|B)&(0xC0000000)) {\
594             if (R>=(256<<22))   R=(256<<22)-1; \
595             else if (R<0)R=0;   \
596             if (G>=(256<<22))   G=(256<<22)-1; \
597             else if (G<0)G=0;   \
598             if (B>=(256<<22))   B=(256<<22)-1; \
599             else if (B<0)B=0;   \
600         }
601
602 #define YSCALE_YUV_2_GRAY16_C \
603     for (i=0; i<(dstW>>1); i++) {\
604         int j;\
605         int Y1 = 1<<18;\
606         int Y2 = 1<<18;\
607         int U  = 1<<18;\
608         int V  = 1<<18;\
609         \
610         const int i2= 2*i;\
611         \
612         for (j=0; j<lumFilterSize; j++) {\
613             Y1 += lumSrc[j][i2] * lumFilter[j];\
614             Y2 += lumSrc[j][i2+1] * lumFilter[j];\
615         }\
616         Y1>>=11;\
617         Y2>>=11;\
618         if ((Y1|Y2|U|V)&65536) {\
619             if (Y1>65535)   Y1=65535; \
620             else if (Y1<0)Y1=0;   \
621             if (Y2>65535)   Y2=65535; \
622             else if (Y2<0)Y2=0;   \
623         }
624
625 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
626     YSCALE_YUV_2_PACKEDX_C(type,alpha)  /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
627     r = (type *)c->table_rV[V];   \
628     g = (type *)(c->table_gU[U] + c->table_gV[V]); \
629     b = (type *)c->table_bU[U];
630
631 #define YSCALE_YUV_2_PACKED2_C(type,alpha)   \
632     for (i=0; i<(dstW>>1); i++) { \
633         const int i2= 2*i;       \
634         int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>19;           \
635         int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;           \
636         int U= (ubuf0[i]*uvalpha1+ubuf1[i]*uvalpha)>>19;              \
637         int V= (vbuf0[i]*uvalpha1+vbuf1[i]*uvalpha)>>19;              \
638         type av_unused *r, *b, *g;                                    \
639         int av_unused A1, A2;                                         \
640         if (alpha) {\
641             A1= (abuf0[i2  ]*yalpha1+abuf1[i2  ]*yalpha)>>19;         \
642             A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19;         \
643         }
644
645 #define YSCALE_YUV_2_GRAY16_2_C   \
646     for (i=0; i<(dstW>>1); i++) { \
647         const int i2= 2*i;       \
648         int Y1= (buf0[i2  ]*yalpha1+buf1[i2  ]*yalpha)>>11;           \
649         int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
650
651 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
652     YSCALE_YUV_2_PACKED2_C(type,alpha)\
653     r = (type *)c->table_rV[V];\
654     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
655     b = (type *)c->table_bU[U];
656
657 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
658     for (i=0; i<(dstW>>1); i++) {\
659         const int i2= 2*i;\
660         int Y1= buf0[i2  ]>>7;\
661         int Y2= buf0[i2+1]>>7;\
662         int U= (ubuf1[i])>>7;\
663         int V= (vbuf1[i])>>7;\
664         type av_unused *r, *b, *g;\
665         int av_unused A1, A2;\
666         if (alpha) {\
667             A1= abuf0[i2  ]>>7;\
668             A2= abuf0[i2+1]>>7;\
669         }
670
671 #define YSCALE_YUV_2_GRAY16_1_C \
672     for (i=0; i<(dstW>>1); i++) {\
673         const int i2= 2*i;\
674         int Y1= buf0[i2  ]<<1;\
675         int Y2= buf0[i2+1]<<1;
676
677 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
678     YSCALE_YUV_2_PACKED1_C(type,alpha)\
679     r = (type *)c->table_rV[V];\
680     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
681     b = (type *)c->table_bU[U];
682
683 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
684     for (i=0; i<(dstW>>1); i++) {\
685         const int i2= 2*i;\
686         int Y1= buf0[i2  ]>>7;\
687         int Y2= buf0[i2+1]>>7;\
688         int U= (ubuf0[i] + ubuf1[i])>>8;\
689         int V= (vbuf0[i] + vbuf1[i])>>8;\
690         type av_unused *r, *b, *g;\
691         int av_unused A1, A2;\
692         if (alpha) {\
693             A1= abuf0[i2  ]>>7;\
694             A2= abuf0[i2+1]>>7;\
695         }
696
697 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
698     YSCALE_YUV_2_PACKED1B_C(type,alpha)\
699     r = (type *)c->table_rV[V];\
700     g = (type *)(c->table_gU[U] + c->table_gV[V]);\
701     b = (type *)c->table_bU[U];
702
703 #define YSCALE_YUV_2_MONO2_C \
704     const uint8_t * const d128=dither_8x8_220[y&7];\
705     uint8_t *g= c->table_gU[128] + c->table_gV[128];\
706     for (i=0; i<dstW-7; i+=8) {\
707         int acc;\
708         acc =       g[((buf0[i  ]*yalpha1+buf1[i  ]*yalpha)>>19) + d128[0]];\
709         acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
710         acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
711         acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
712         acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
713         acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
714         acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
715         acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
716         ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
717         dest++;\
718     }
719
720 #define YSCALE_YUV_2_MONOX_C \
721     const uint8_t * const d128=dither_8x8_220[y&7];\
722     uint8_t *g= c->table_gU[128] + c->table_gV[128];\
723     int acc=0;\
724     for (i=0; i<dstW-1; i+=2) {\
725         int j;\
726         int Y1=1<<18;\
727         int Y2=1<<18;\
728 \
729         for (j=0; j<lumFilterSize; j++) {\
730             Y1 += lumSrc[j][i] * lumFilter[j];\
731             Y2 += lumSrc[j][i+1] * lumFilter[j];\
732         }\
733         Y1>>=19;\
734         Y2>>=19;\
735         if ((Y1|Y2)&256) {\
736             if (Y1>255)   Y1=255;\
737             else if (Y1<0)Y1=0;\
738             if (Y2>255)   Y2=255;\
739             else if (Y2<0)Y2=0;\
740         }\
741         acc+= acc + g[Y1+d128[(i+0)&7]];\
742         acc+= acc + g[Y2+d128[(i+1)&7]];\
743         if ((i&7)==6) {\
744             ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
745             dest++;\
746         }\
747     }
748
749 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
750     switch(c->dstFormat) {\
751     case PIX_FMT_RGB48BE:\
752     case PIX_FMT_RGB48LE:\
753         func(uint8_t,0)\
754             ((uint8_t*)dest)[ 0]= r[Y1];\
755             ((uint8_t*)dest)[ 1]= r[Y1];\
756             ((uint8_t*)dest)[ 2]= g[Y1];\
757             ((uint8_t*)dest)[ 3]= g[Y1];\
758             ((uint8_t*)dest)[ 4]= b[Y1];\
759             ((uint8_t*)dest)[ 5]= b[Y1];\
760             ((uint8_t*)dest)[ 6]= r[Y2];\
761             ((uint8_t*)dest)[ 7]= r[Y2];\
762             ((uint8_t*)dest)[ 8]= g[Y2];\
763             ((uint8_t*)dest)[ 9]= g[Y2];\
764             ((uint8_t*)dest)[10]= b[Y2];\
765             ((uint8_t*)dest)[11]= b[Y2];\
766             dest+=12;\
767         }\
768         break;\
769     case PIX_FMT_BGR48BE:\
770     case PIX_FMT_BGR48LE:\
771         func(uint8_t,0)\
772             ((uint8_t*)dest)[ 0] = ((uint8_t*)dest)[ 1] = b[Y1];\
773             ((uint8_t*)dest)[ 2] = ((uint8_t*)dest)[ 3] = g[Y1];\
774             ((uint8_t*)dest)[ 4] = ((uint8_t*)dest)[ 5] = r[Y1];\
775             ((uint8_t*)dest)[ 6] = ((uint8_t*)dest)[ 7] = b[Y2];\
776             ((uint8_t*)dest)[ 8] = ((uint8_t*)dest)[ 9] = g[Y2];\
777             ((uint8_t*)dest)[10] = ((uint8_t*)dest)[11] = r[Y2];\
778             dest+=12;\
779         }\
780         break;\
781     case PIX_FMT_RGBA:\
782     case PIX_FMT_BGRA:\
783         if (CONFIG_SMALL) {\
784             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
785             func(uint32_t,needAlpha)\
786                 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
787                 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
788             }\
789         } else {\
790             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
791                 func(uint32_t,1)\
792                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
793                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
794                 }\
795             } else {\
796                 func(uint32_t,0)\
797                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
798                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
799                 }\
800             }\
801         }\
802         break;\
803     case PIX_FMT_ARGB:\
804     case PIX_FMT_ABGR:\
805         if (CONFIG_SMALL) {\
806             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
807             func(uint32_t,needAlpha)\
808                 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
809                 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
810             }\
811         } else {\
812             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
813                 func(uint32_t,1)\
814                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
815                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
816                 }\
817             } else {\
818                 func(uint32_t,0)\
819                     ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
820                     ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
821                 }\
822             }\
823         }                \
824         break;\
825     case PIX_FMT_RGB24:\
826         func(uint8_t,0)\
827             ((uint8_t*)dest)[0]= r[Y1];\
828             ((uint8_t*)dest)[1]= g[Y1];\
829             ((uint8_t*)dest)[2]= b[Y1];\
830             ((uint8_t*)dest)[3]= r[Y2];\
831             ((uint8_t*)dest)[4]= g[Y2];\
832             ((uint8_t*)dest)[5]= b[Y2];\
833             dest+=6;\
834         }\
835         break;\
836     case PIX_FMT_BGR24:\
837         func(uint8_t,0)\
838             ((uint8_t*)dest)[0]= b[Y1];\
839             ((uint8_t*)dest)[1]= g[Y1];\
840             ((uint8_t*)dest)[2]= r[Y1];\
841             ((uint8_t*)dest)[3]= b[Y2];\
842             ((uint8_t*)dest)[4]= g[Y2];\
843             ((uint8_t*)dest)[5]= r[Y2];\
844             dest+=6;\
845         }\
846         break;\
847     case PIX_FMT_RGB565BE:\
848     case PIX_FMT_RGB565LE:\
849     case PIX_FMT_BGR565BE:\
850     case PIX_FMT_BGR565LE:\
851         {\
852             const int dr1= dither_2x2_8[y&1    ][0];\
853             const int dg1= dither_2x2_4[y&1    ][0];\
854             const int db1= dither_2x2_8[(y&1)^1][0];\
855             const int dr2= dither_2x2_8[y&1    ][1];\
856             const int dg2= dither_2x2_4[y&1    ][1];\
857             const int db2= dither_2x2_8[(y&1)^1][1];\
858             func(uint16_t,0)\
859                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
860                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
861             }\
862         }\
863         break;\
864     case PIX_FMT_RGB555BE:\
865     case PIX_FMT_RGB555LE:\
866     case PIX_FMT_BGR555BE:\
867     case PIX_FMT_BGR555LE:\
868         {\
869             const int dr1= dither_2x2_8[y&1    ][0];\
870             const int dg1= dither_2x2_8[y&1    ][1];\
871             const int db1= dither_2x2_8[(y&1)^1][0];\
872             const int dr2= dither_2x2_8[y&1    ][1];\
873             const int dg2= dither_2x2_8[y&1    ][0];\
874             const int db2= dither_2x2_8[(y&1)^1][1];\
875             func(uint16_t,0)\
876                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
877                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
878             }\
879         }\
880         break;\
881     case PIX_FMT_RGB444BE:\
882     case PIX_FMT_RGB444LE:\
883     case PIX_FMT_BGR444BE:\
884     case PIX_FMT_BGR444LE:\
885         {\
886             const int dr1= dither_4x4_16[y&3    ][0];\
887             const int dg1= dither_4x4_16[y&3    ][1];\
888             const int db1= dither_4x4_16[(y&3)^3][0];\
889             const int dr2= dither_4x4_16[y&3    ][1];\
890             const int dg2= dither_4x4_16[y&3    ][0];\
891             const int db2= dither_4x4_16[(y&3)^3][1];\
892             func(uint16_t,0)\
893                 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
894                 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
895             }\
896         }\
897         break;\
898     case PIX_FMT_RGB8:\
899     case PIX_FMT_BGR8:\
900         {\
901             const uint8_t * const d64= dither_8x8_73[y&7];\
902             const uint8_t * const d32= dither_8x8_32[y&7];\
903             func(uint8_t,0)\
904                 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
905                 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
906             }\
907         }\
908         break;\
909     case PIX_FMT_RGB4:\
910     case PIX_FMT_BGR4:\
911         {\
912             const uint8_t * const d64= dither_8x8_73 [y&7];\
913             const uint8_t * const d128=dither_8x8_220[y&7];\
914             func(uint8_t,0)\
915                 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
916                                  + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
917             }\
918         }\
919         break;\
920     case PIX_FMT_RGB4_BYTE:\
921     case PIX_FMT_BGR4_BYTE:\
922         {\
923             const uint8_t * const d64= dither_8x8_73 [y&7];\
924             const uint8_t * const d128=dither_8x8_220[y&7];\
925             func(uint8_t,0)\
926                 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
927                 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
928             }\
929         }\
930         break;\
931     case PIX_FMT_MONOBLACK:\
932     case PIX_FMT_MONOWHITE:\
933         {\
934             func_monoblack\
935         }\
936         break;\
937     case PIX_FMT_YUYV422:\
938         func2\
939             ((uint8_t*)dest)[2*i2+0]= Y1;\
940             ((uint8_t*)dest)[2*i2+1]= U;\
941             ((uint8_t*)dest)[2*i2+2]= Y2;\
942             ((uint8_t*)dest)[2*i2+3]= V;\
943         }                \
944         break;\
945     case PIX_FMT_UYVY422:\
946         func2\
947             ((uint8_t*)dest)[2*i2+0]= U;\
948             ((uint8_t*)dest)[2*i2+1]= Y1;\
949             ((uint8_t*)dest)[2*i2+2]= V;\
950             ((uint8_t*)dest)[2*i2+3]= Y2;\
951         }                \
952         break;\
953     case PIX_FMT_GRAY16BE:\
954         func_g16\
955             ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
956             ((uint8_t*)dest)[2*i2+1]= Y1;\
957             ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
958             ((uint8_t*)dest)[2*i2+3]= Y2;\
959         }                \
960         break;\
961     case PIX_FMT_GRAY16LE:\
962         func_g16\
963             ((uint8_t*)dest)[2*i2+0]= Y1;\
964             ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
965             ((uint8_t*)dest)[2*i2+2]= Y2;\
966             ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
967         }                \
968         break;\
969     }
970
971 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter,
972                                   const int16_t **lumSrc, int lumFilterSize,
973                                   const int16_t *chrFilter, const int16_t **chrUSrc,
974                                   const int16_t **chrVSrc, int chrFilterSize,
975                                   const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
976 {
977     int i;
978     YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
979 }
980
981 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter,
982                                     const int16_t **lumSrc, int lumFilterSize,
983                                     const int16_t *chrFilter, const int16_t **chrUSrc,
984                                     const int16_t **chrVSrc, int chrFilterSize,
985                                     const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
986 {
987     int i;
988     int step= c->dstFormatBpp/8;
989     int aidx= 3;
990
991     switch(c->dstFormat) {
992     case PIX_FMT_ARGB:
993         dest++;
994         aidx= 0;
995     case PIX_FMT_RGB24:
996         aidx--;
997     case PIX_FMT_RGBA:
998         if (CONFIG_SMALL) {
999             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1000             YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1001                 dest[aidx]= needAlpha ? A : 255;
1002                 dest[0]= R>>22;
1003                 dest[1]= G>>22;
1004                 dest[2]= B>>22;
1005                 dest+= step;
1006             }
1007         } else {
1008             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1009                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1010                     dest[aidx]= A;
1011                     dest[0]= R>>22;
1012                     dest[1]= G>>22;
1013                     dest[2]= B>>22;
1014                     dest+= step;
1015                 }
1016             } else {
1017                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1018                     dest[aidx]= 255;
1019                     dest[0]= R>>22;
1020                     dest[1]= G>>22;
1021                     dest[2]= B>>22;
1022                     dest+= step;
1023                 }
1024             }
1025         }
1026         break;
1027     case PIX_FMT_ABGR:
1028         dest++;
1029         aidx= 0;
1030     case PIX_FMT_BGR24:
1031         aidx--;
1032     case PIX_FMT_BGRA:
1033         if (CONFIG_SMALL) {
1034             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1035             YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1036                 dest[aidx]= needAlpha ? A : 255;
1037                 dest[0]= B>>22;
1038                 dest[1]= G>>22;
1039                 dest[2]= R>>22;
1040                 dest+= step;
1041             }
1042         } else {
1043             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1044                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1045                     dest[aidx]= A;
1046                     dest[0]= B>>22;
1047                     dest[1]= G>>22;
1048                     dest[2]= R>>22;
1049                     dest+= step;
1050                 }
1051             } else {
1052                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1053                     dest[aidx]= 255;
1054                     dest[0]= B>>22;
1055                     dest[1]= G>>22;
1056                     dest[2]= R>>22;
1057                     dest+= step;
1058                 }
1059             }
1060         }
1061         break;
1062     default:
1063         assert(0);
1064     }
1065 }
1066
1067 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
1068 {
1069     int i;
1070     uint8_t *ptr = plane + stride*y;
1071     for (i=0; i<height; i++) {
1072         memset(ptr, val, width);
1073         ptr += stride;
1074     }
1075 }
1076
1077 #define RGB48(name, R, B, READ)\
1078 static inline void name ## ToY(int16_t *dst, const uint16_t *src, int width, uint32_t *unused)\
1079 {\
1080     int i;\
1081     for (i = 0; i < width; i++) {\
1082         int r = READ(&src[i*3+R]);\
1083         int g = READ(&src[i*3+1]);\
1084         int b = READ(&src[i*3+B]);\
1085 \
1086         dst[i] = (RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1+8)) + (1<<(RGB2YUV_SHIFT-7+8))) >> (RGB2YUV_SHIFT-6+8);\
1087     }\
1088 }\
1089 \
1090 static inline void name ## ToUV(int16_t *dstU, int16_t *dstV,\
1091                              const uint16_t *src1, const uint16_t *src2,\
1092                              int width, uint32_t *unused)\
1093 {\
1094     int i;\
1095     assert(src1==src2);\
1096     for (i = 0; i < width; i++) {\
1097         int r = READ(&src1[3*i + R]);\
1098         int g = READ(&src1[3*i + 1]);\
1099         int b = READ(&src1[3*i + B]);\
1100 \
1101         dstU[i] = (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1+8)) + (1<<(RGB2YUV_SHIFT-7+8))) >> (RGB2YUV_SHIFT-6+8);\
1102         dstV[i] = (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1+8)) + (1<<(RGB2YUV_SHIFT-7+8))) >> (RGB2YUV_SHIFT-6+8);\
1103     }\
1104 }\
1105 \
1106 static inline void name ## ToUV_half(int16_t *dstU, int16_t *dstV,\
1107                                   const uint16_t *src1, const uint16_t *src2,\
1108                                   int width, uint32_t *unused)\
1109 {\
1110     int i;\
1111     assert(src1==src2);\
1112     for (i = 0; i < width; i++) {\
1113         int r= READ(&src1[6*i + R]) + READ(&src1[6*i + 3+R]);\
1114         int g= READ(&src1[6*i + 1]) + READ(&src1[6*i + 4]);\
1115         int b= READ(&src1[6*i + B]) + READ(&src1[6*i + 3+B]);\
1116 \
1117         dstU[i]= (RU*r + GU*g + BU*b + (256U<<(RGB2YUV_SHIFT+8)) + (1<<(RGB2YUV_SHIFT-6+8))) >> (RGB2YUV_SHIFT-5+8);\
1118         dstV[i]= (RV*r + GV*g + BV*b + (256U<<(RGB2YUV_SHIFT+8)) + (1<<(RGB2YUV_SHIFT-6+8))) >> (RGB2YUV_SHIFT-5+8);\
1119     }\
1120 }
1121
1122 RGB48(rgb48LE, 0, 2, AV_RL16)
1123 RGB48(rgb48BE, 0, 2, AV_RB16)
1124 RGB48(bgr48LE, 2, 0, AV_RL16)
1125 RGB48(bgr48BE, 2, 0, AV_RB16)
1126
1127 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1128 static inline void name(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)\
1129 {\
1130     int i;\
1131     for (i=0; i<width; i++) {\
1132         int b= (((const type*)src)[i]>>shb)&maskb;\
1133         int g= (((const type*)src)[i]>>shg)&maskg;\
1134         int r= (((const type*)src)[i]>>shr)&maskr;\
1135 \
1136         dst[i]= (((RY)*r + (GY)*g + (BY)*b + (32<<((S)-1)) + (1<<(S-7)))>>((S)-6));\
1137     }\
1138 }
1139
1140 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1141 BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1142 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1143 BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1144 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
1145 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
1146 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1147 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1148
1149 static inline void abgrToA(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
1150 {
1151     int i;
1152     for (i=0; i<width; i++) {
1153         dst[i]= src[4*i]<<6;
1154     }
1155 }
1156
1157 #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1158 static inline void name(int16_t *dstU, int16_t *dstV, const uint8_t *src, const uint8_t *dummy, int width, uint32_t *unused)\
1159 {\
1160     int i;\
1161     for (i=0; i<width; i++) {\
1162         int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1163         int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1164         int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1165 \
1166         dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (256<<((S)-1)) + (1<<(S-7)))>>((S)-6);\
1167         dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (256<<((S)-1)) + (1<<(S-7)))>>((S)-6);\
1168     }\
1169 }\
1170 static inline void name ## _half(int16_t *dstU, int16_t *dstV, const uint8_t *src, const uint8_t *dummy, int width, uint32_t *unused)\
1171 {\
1172     int i;\
1173     for (i=0; i<width; i++) {\
1174         int pix0= ((const type*)src)[2*i+0]>>shp;\
1175         int pix1= ((const type*)src)[2*i+1]>>shp;\
1176         int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1177         int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1178         int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1179         g&= maskg|(2*maskg);\
1180 \
1181         g>>=shg;\
1182 \
1183         dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (256U<<(S)) + (1<<(S-6)))>>((S)-6+1);\
1184         dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (256U<<(S)) + (1<<(S-6)))>>((S)-6+1);\
1185     }\
1186 }
1187
1188 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1189 BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1190 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1191 BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1192 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1193 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1194 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1195 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1196
1197 static inline void palToA(int16_t *dst, const uint8_t *src, int width, uint32_t *pal)
1198 {
1199     int i;
1200     for (i=0; i<width; i++) {
1201         int d= src[i];
1202
1203         dst[i]= (pal[d] >> 24)<<6;
1204     }
1205 }
1206
1207 static inline void palToY(int16_t *dst, const uint8_t *src, long width, uint32_t *pal)
1208 {
1209     int i;
1210     for (i=0; i<width; i++) {
1211         int d= src[i];
1212
1213         dst[i]= (pal[d] & 0xFF)<<6;
1214     }
1215 }
1216
1217 static inline void palToUV(uint16_t *dstU, int16_t *dstV,
1218                            const uint8_t *src1, const uint8_t *src2,
1219                            int width, uint32_t *pal)
1220 {
1221     int i;
1222     assert(src1 == src2);
1223     for (i=0; i<width; i++) {
1224         int p= pal[src1[i]];
1225
1226         dstU[i]= (uint8_t)(p>> 8)<<6;
1227         dstV[i]= (uint8_t)(p>>16)<<6;
1228     }
1229 }
1230
1231 static inline void monowhite2Y(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
1232 {
1233     int i, j;
1234     for (i=0; i<width/8; i++) {
1235         int d= ~src[i];
1236         for(j=0; j<8; j++)
1237             dst[8*i+j]= ((d>>(7-j))&1)*16383;
1238     }
1239 }
1240
1241 static inline void monoblack2Y(int16_t *dst, const uint8_t *src, int width, uint32_t *unused)
1242 {
1243     int i, j;
1244     for (i=0; i<width/8; i++) {
1245         int d= src[i];
1246         for(j=0; j<8; j++)
1247             dst[8*i+j]= ((d>>(7-j))&1)*16383;
1248     }
1249 }
1250
1251 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1252 //Plain C versions
1253
1254 #define COMPILE_TEMPLATE_MMX2 0
1255 #define COMPILE_TEMPLATE_ALTIVEC 0
1256
1257 #include "swscale_template.c"
1258
1259 #if HAVE_ALTIVEC
1260 #undef RENAME
1261 #undef COMPILE_TEMPLATE_ALTIVEC
1262 #define COMPILE_TEMPLATE_ALTIVEC 1
1263 #define RENAME(a) a ## _altivec
1264 #include "ppc/swscale_template.c"
1265 #endif
1266
1267 //MMX versions
1268 #if HAVE_MMX
1269 #undef RENAME
1270 #undef COMPILE_TEMPLATE_MMX2
1271 #define COMPILE_TEMPLATE_MMX2 0
1272 #define RENAME(a) a ## _MMX
1273 #include "x86/swscale_template.c"
1274 #endif
1275
1276 //MMX2 versions
1277 #if HAVE_MMX2
1278 #undef RENAME
1279 #undef COMPILE_TEMPLATE_MMX2
1280 #define COMPILE_TEMPLATE_MMX2 1
1281 #define RENAME(a) a ## _MMX2
1282 #include "x86/swscale_template.c"
1283 #endif
1284
1285 SwsFunc ff_getSwsFunc(SwsContext *c)
1286 {
1287     int cpu_flags = av_get_cpu_flags();
1288
1289     sws_init_swScale_c(c);
1290
1291 #if HAVE_MMX
1292     if (cpu_flags & AV_CPU_FLAG_MMX)
1293         sws_init_swScale_MMX(c);
1294 #endif
1295 #if HAVE_MMX2
1296     if (cpu_flags & AV_CPU_FLAG_MMX2)
1297         sws_init_swScale_MMX2(c);
1298 #endif
1299 #if HAVE_ALTIVEC
1300     if (cpu_flags & AV_CPU_FLAG_ALTIVEC)
1301         sws_init_swScale_altivec(c);
1302 #endif
1303
1304     return swScale_c;
1305 }
1306
1307 static void copyPlane(const uint8_t *src, int srcStride,
1308                       int srcSliceY, int srcSliceH, int width,
1309                       uint8_t *dst, int dstStride)
1310 {
1311     dst += dstStride * srcSliceY;
1312     if (dstStride == srcStride && srcStride > 0) {
1313         memcpy(dst, src, srcSliceH * dstStride);
1314     } else {
1315         int i;
1316         for (i=0; i<srcSliceH; i++) {
1317             memcpy(dst, src, width);
1318             src += srcStride;
1319             dst += dstStride;
1320         }
1321     }
1322 }
1323
1324 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1325                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1326 {
1327     uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1328
1329     copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1330               dstParam[0], dstStride[0]);
1331
1332     if (c->dstFormat == PIX_FMT_NV12)
1333         interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1334     else
1335         interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1336
1337     return srcSliceH;
1338 }
1339
1340 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1341                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1342 {
1343     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1344
1345     yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1346
1347     return srcSliceH;
1348 }
1349
1350 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1351                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1352 {
1353     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1354
1355     yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1356
1357     return srcSliceH;
1358 }
1359
1360 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1361                                 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1362 {
1363     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1364
1365     yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1366
1367     return srcSliceH;
1368 }
1369
1370 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1371                                 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1372 {
1373     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1374
1375     yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1376
1377     return srcSliceH;
1378 }
1379
1380 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1381                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1382 {
1383     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1384     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1385     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1386
1387     yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1388
1389     if (dstParam[3])
1390         fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1391
1392     return srcSliceH;
1393 }
1394
1395 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1396                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1397 {
1398     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1399     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1400     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1401
1402     yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1403
1404     return srcSliceH;
1405 }
1406
1407 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1408                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1409 {
1410     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1411     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1412     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1413
1414     uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1415
1416     if (dstParam[3])
1417         fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1418
1419     return srcSliceH;
1420 }
1421
1422 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1423                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1424 {
1425     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1426     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1427     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1428
1429     uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1430
1431     return srcSliceH;
1432 }
1433
1434 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
1435 {
1436     int i;
1437     for (i=0; i<num_pixels; i++)
1438         ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1439 }
1440
1441 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
1442 {
1443     int i;
1444
1445     for (i=0; i<num_pixels; i++)
1446         ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1447 }
1448
1449 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
1450 {
1451     int i;
1452
1453     for (i=0; i<num_pixels; i++) {
1454         //FIXME slow?
1455         dst[0]= palette[src[i<<1]*4+0];
1456         dst[1]= palette[src[i<<1]*4+1];
1457         dst[2]= palette[src[i<<1]*4+2];
1458         dst+= 3;
1459     }
1460 }
1461
1462 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1463                            int srcSliceH, uint8_t* dst[], int dstStride[])
1464 {
1465     const enum PixelFormat srcFormat= c->srcFormat;
1466     const enum PixelFormat dstFormat= c->dstFormat;
1467     void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
1468                  const uint8_t *palette)=NULL;
1469     int i;
1470     uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1471     const uint8_t *srcPtr= src[0];
1472
1473     if (srcFormat == PIX_FMT_GRAY8A) {
1474         switch (dstFormat) {
1475         case PIX_FMT_RGB32  : conv = gray8aToPacked32; break;
1476         case PIX_FMT_BGR32  : conv = gray8aToPacked32; break;
1477         case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1478         case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1479         case PIX_FMT_RGB24  : conv = gray8aToPacked24; break;
1480         case PIX_FMT_BGR24  : conv = gray8aToPacked24; break;
1481         }
1482     } else if (usePal(srcFormat)) {
1483         switch (dstFormat) {
1484         case PIX_FMT_RGB32  : conv = sws_convertPalette8ToPacked32; break;
1485         case PIX_FMT_BGR32  : conv = sws_convertPalette8ToPacked32; break;
1486         case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1487         case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1488         case PIX_FMT_RGB24  : conv = sws_convertPalette8ToPacked24; break;
1489         case PIX_FMT_BGR24  : conv = sws_convertPalette8ToPacked24; break;
1490         }
1491     }
1492
1493     if (!conv)
1494         av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1495                av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1496     else {
1497         for (i=0; i<srcSliceH; i++) {
1498             conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1499             srcPtr+= srcStride[0];
1500             dstPtr+= dstStride[0];
1501         }
1502     }
1503
1504     return srcSliceH;
1505 }
1506
1507 #define isRGBA32(x) (            \
1508            (x) == PIX_FMT_ARGB   \
1509         || (x) == PIX_FMT_RGBA   \
1510         || (x) == PIX_FMT_BGRA   \
1511         || (x) == PIX_FMT_ABGR   \
1512         )
1513
1514 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1515 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1516                            int srcSliceH, uint8_t* dst[], int dstStride[])
1517 {
1518     const enum PixelFormat srcFormat= c->srcFormat;
1519     const enum PixelFormat dstFormat= c->dstFormat;
1520     const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1521     const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1522     const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1523     const int dstId= c->dstFormatBpp >> 2;
1524     void (*conv)(const uint8_t *src, uint8_t *dst, int src_size)=NULL;
1525
1526 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1527
1528     if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1529         if (     CONV_IS(ABGR, RGBA)
1530               || CONV_IS(ARGB, BGRA)
1531               || CONV_IS(BGRA, ARGB)
1532               || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1533         else if (CONV_IS(ABGR, ARGB)
1534               || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1535         else if (CONV_IS(ABGR, BGRA)
1536               || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1537         else if (CONV_IS(BGRA, RGBA)
1538               || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1539         else if (CONV_IS(BGRA, ABGR)
1540               || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1541     } else
1542     /* BGR -> BGR */
1543     if (  (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1544        || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1545         switch(srcId | (dstId<<4)) {
1546         case 0x34: conv= rgb16to15; break;
1547         case 0x36: conv= rgb24to15; break;
1548         case 0x38: conv= rgb32to15; break;
1549         case 0x43: conv= rgb15to16; break;
1550         case 0x46: conv= rgb24to16; break;
1551         case 0x48: conv= rgb32to16; break;
1552         case 0x63: conv= rgb15to24; break;
1553         case 0x64: conv= rgb16to24; break;
1554         case 0x68: conv= rgb32to24; break;
1555         case 0x83: conv= rgb15to32; break;
1556         case 0x84: conv= rgb16to32; break;
1557         case 0x86: conv= rgb24to32; break;
1558         }
1559     } else if (  (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1560              || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1561         switch(srcId | (dstId<<4)) {
1562         case 0x33: conv= rgb15tobgr15; break;
1563         case 0x34: conv= rgb16tobgr15; break;
1564         case 0x36: conv= rgb24tobgr15; break;
1565         case 0x38: conv= rgb32tobgr15; break;
1566         case 0x43: conv= rgb15tobgr16; break;
1567         case 0x44: conv= rgb16tobgr16; break;
1568         case 0x46: conv= rgb24tobgr16; break;
1569         case 0x48: conv= rgb32tobgr16; break;
1570         case 0x63: conv= rgb15tobgr24; break;
1571         case 0x64: conv= rgb16tobgr24; break;
1572         case 0x66: conv= rgb24tobgr24; break;
1573         case 0x68: conv= rgb32tobgr24; break;
1574         case 0x83: conv= rgb15tobgr32; break;
1575         case 0x84: conv= rgb16tobgr32; break;
1576         case 0x86: conv= rgb24tobgr32; break;
1577         }
1578     }
1579
1580     if (!conv) {
1581         av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1582                av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
1583     } else {
1584         const uint8_t *srcPtr= src[0];
1585               uint8_t *dstPtr= dst[0];
1586         if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1587             srcPtr += ALT32_CORR;
1588
1589         if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1590             dstPtr += ALT32_CORR;
1591
1592         if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0 && !(srcStride[0]%srcBpp))
1593             conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1594         else {
1595             int i;
1596             dstPtr += dstStride[0]*srcSliceY;
1597
1598             for (i=0; i<srcSliceH; i++) {
1599                 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1600                 srcPtr+= srcStride[0];
1601                 dstPtr+= dstStride[0];
1602             }
1603         }
1604     }
1605     return srcSliceH;
1606 }
1607
1608 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1609                               int srcSliceH, uint8_t* dst[], int dstStride[])
1610 {
1611     rgb24toyv12(
1612         src[0],
1613         dst[0]+ srcSliceY    *dstStride[0],
1614         dst[1]+(srcSliceY>>1)*dstStride[1],
1615         dst[2]+(srcSliceY>>1)*dstStride[2],
1616         c->srcW, srcSliceH,
1617         dstStride[0], dstStride[1], srcStride[0]);
1618     if (dst[3])
1619         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1620     return srcSliceH;
1621 }
1622
1623 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1624                              int srcSliceH, uint8_t* dst[], int dstStride[])
1625 {
1626     copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1627               dst[0], dstStride[0]);
1628
1629     planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1630              srcSliceH >> 2, srcStride[1], dstStride[1]);
1631     planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1632              srcSliceH >> 2, srcStride[2], dstStride[2]);
1633     if (dst[3])
1634         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1635     return srcSliceH;
1636 }
1637
1638 /* unscaled copy like stuff (assumes nearly identical formats) */
1639 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1640                              int srcSliceH, uint8_t* dst[], int dstStride[])
1641 {
1642     if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1643         memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1644     else {
1645         int i;
1646         const uint8_t *srcPtr= src[0];
1647         uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1648         int length=0;
1649
1650         /* universal length finder */
1651         while(length+c->srcW <= FFABS(dstStride[0])
1652            && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1653         assert(length!=0);
1654
1655         for (i=0; i<srcSliceH; i++) {
1656             memcpy(dstPtr, srcPtr, length);
1657             srcPtr+= srcStride[0];
1658             dstPtr+= dstStride[0];
1659         }
1660     }
1661     return srcSliceH;
1662 }
1663
1664 #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
1665     uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\
1666     int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\
1667     for (i = 0; i < height; i++) {\
1668         const uint8_t *dither= dithers[src_depth-9][i&7];\
1669         for (j = 0; j < length-7; j+=8){\
1670             dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\
1671             dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\
1672             dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\
1673             dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\
1674             dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\
1675             dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\
1676             dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\
1677             dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\
1678         }\
1679         for (; j < length; j++)\
1680             dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\
1681         dst += dstStride;\
1682         src += srcStride;\
1683     }
1684
1685
1686 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1687                              int srcSliceH, uint8_t* dst[], int dstStride[])
1688 {
1689     int plane, i, j;
1690     for (plane=0; plane<4; plane++) {
1691         int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1692         int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1693         int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1694         const uint8_t *srcPtr= src[plane];
1695         uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1696
1697         if (!dst[plane]) continue;
1698         // ignore palette for GRAY8
1699         if (plane == 1 && !dst[2]) continue;
1700         if (!src[plane] || (plane == 1 && !src[2])) {
1701             if(is16BPS(c->dstFormat))
1702                 length*=2;
1703             fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1704         } else {
1705             if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)
1706                || (is16BPS(c->srcFormat) != is16BPS(c->dstFormat))
1707             ) {
1708                 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
1709                 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
1710                 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
1711                 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1712
1713                 if (dst_depth == 8) {
1714                     if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
1715                         DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
1716                     } else {
1717                         DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, av_bswap16, )
1718                     }
1719                 } else if (src_depth == 8) {
1720                     for (i = 0; i < height; i++) {
1721                         if(isBE(c->dstFormat)){
1722                             for (j = 0; j < length; j++)
1723                                 AV_WB16(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |
1724                                                      (srcPtr[j]>>(2*8-dst_depth)));
1725                         } else {
1726                             for (j = 0; j < length; j++)
1727                                 AV_WL16(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |
1728                                                      (srcPtr[j]>>(2*8-dst_depth)));
1729                         }
1730                         dstPtr2 += dstStride[plane]/2;
1731                         srcPtr  += srcStride[plane];
1732                     }
1733                 } else if (src_depth <= dst_depth) {
1734                     for (i = 0; i < height; i++) {
1735 #define COPY_UP(r,w) \
1736     for (j = 0; j < length; j++){ \
1737         unsigned int v= r(&srcPtr2[j]);\
1738         w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
1739                        (v>>(2*src_depth-dst_depth)));\
1740     }
1741                         if(isBE(c->srcFormat)){
1742                             if(isBE(c->dstFormat)){
1743                                 COPY_UP(AV_RB16, AV_WB16)
1744                             } else {
1745                                 COPY_UP(AV_RB16, AV_WL16)
1746                             }
1747                         } else {
1748                             if(isBE(c->dstFormat)){
1749                                 COPY_UP(AV_RL16, AV_WB16)
1750                             } else {
1751                                 COPY_UP(AV_RL16, AV_WL16)
1752                             }
1753                         }
1754                         dstPtr2 += dstStride[plane]/2;
1755                         srcPtr2 += srcStride[plane]/2;
1756                     }
1757                 } else {
1758                     if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
1759                         if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
1760                             DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
1761                         } else {
1762                             DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , av_bswap16)
1763                         }
1764                     }else{
1765                         if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
1766                             DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, )
1767                         } else {
1768                             DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, av_bswap16)
1769                         }
1770                     }
1771                 }
1772             } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1773                   && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1774
1775                 for (i=0; i<height; i++) {
1776                     for (j=0; j<length; j++)
1777                         ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1778                     srcPtr+= srcStride[plane];
1779                     dstPtr+= dstStride[plane];
1780                 }
1781             } else if (dstStride[plane] == srcStride[plane] &&
1782                        srcStride[plane] > 0 && srcStride[plane] == length) {
1783                 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1784                        height*dstStride[plane]);
1785             } else {
1786                 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1787                     length*=2;
1788                 for (i=0; i<height; i++) {
1789                     memcpy(dstPtr, srcPtr, length);
1790                     srcPtr+= srcStride[plane];
1791                     dstPtr+= dstStride[plane];
1792                 }
1793             }
1794         }
1795     }
1796     return srcSliceH;
1797 }
1798
1799 void ff_get_unscaled_swscale(SwsContext *c)
1800 {
1801     const enum PixelFormat srcFormat = c->srcFormat;
1802     const enum PixelFormat dstFormat = c->dstFormat;
1803     const int flags = c->flags;
1804     const int dstH = c->dstH;
1805     int needsDither;
1806
1807     needsDither= isAnyRGB(dstFormat)
1808         &&  c->dstFormatBpp < 24
1809         && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1810
1811     /* yv12_to_nv12 */
1812     if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1813         c->swScale= planarToNv12Wrapper;
1814     }
1815     /* yuv2bgr */
1816     if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1817         && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1818         c->swScale= ff_yuv2rgb_get_func_ptr(c);
1819     }
1820
1821     if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1822         c->swScale= yvu9ToYv12Wrapper;
1823     }
1824
1825     /* bgr24toYV12 */
1826     if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1827         c->swScale= bgr24ToYv12Wrapper;
1828
1829     /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1830     if (   isAnyRGB(srcFormat)
1831         && isAnyRGB(dstFormat)
1832         && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
1833         && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
1834         && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
1835         && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
1836         && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1837         && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1838         && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1839         && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1840         && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
1841         && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
1842         && srcFormat != PIX_FMT_BGR48LE   && dstFormat != PIX_FMT_BGR48LE
1843         && srcFormat != PIX_FMT_BGR48BE   && dstFormat != PIX_FMT_BGR48BE
1844         && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1845         c->swScale= rgbToRgbWrapper;
1846
1847     if ((usePal(srcFormat) && (
1848         dstFormat == PIX_FMT_RGB32   ||
1849         dstFormat == PIX_FMT_RGB32_1 ||
1850         dstFormat == PIX_FMT_RGB24   ||
1851         dstFormat == PIX_FMT_BGR32   ||
1852         dstFormat == PIX_FMT_BGR32_1 ||
1853         dstFormat == PIX_FMT_BGR24)))
1854         c->swScale= palToRgbWrapper;
1855
1856     if (srcFormat == PIX_FMT_YUV422P) {
1857         if (dstFormat == PIX_FMT_YUYV422)
1858             c->swScale= yuv422pToYuy2Wrapper;
1859         else if (dstFormat == PIX_FMT_UYVY422)
1860             c->swScale= yuv422pToUyvyWrapper;
1861     }
1862
1863     /* LQ converters if -sws 0 or -sws 4*/
1864     if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1865         /* yv12_to_yuy2 */
1866         if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1867             if (dstFormat == PIX_FMT_YUYV422)
1868                 c->swScale= planarToYuy2Wrapper;
1869             else if (dstFormat == PIX_FMT_UYVY422)
1870                 c->swScale= planarToUyvyWrapper;
1871         }
1872     }
1873     if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1874         c->swScale= yuyvToYuv420Wrapper;
1875     if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1876         c->swScale= uyvyToYuv420Wrapper;
1877     if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1878         c->swScale= yuyvToYuv422Wrapper;
1879     if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1880         c->swScale= uyvyToYuv422Wrapper;
1881
1882 #if HAVE_ALTIVEC
1883     if ((av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) &&
1884         !(c->flags & SWS_BITEXACT) &&
1885         srcFormat == PIX_FMT_YUV420P) {
1886         // unscaled YV12 -> packed YUV, we want speed
1887         if (dstFormat == PIX_FMT_YUYV422)
1888             c->swScale= yv12toyuy2_unscaled_altivec;
1889         else if (dstFormat == PIX_FMT_UYVY422)
1890             c->swScale= yv12touyvy_unscaled_altivec;
1891     }
1892 #endif
1893
1894     /* simple copy */
1895     if (  srcFormat == dstFormat
1896         || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1897         || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1898         || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1899         || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1900         || (isGray(dstFormat) && isGray(srcFormat))
1901         || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1902             && c->chrDstHSubSample == c->chrSrcHSubSample
1903             && c->chrDstVSubSample == c->chrSrcVSubSample
1904             && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1905             && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1906     {
1907         if (isPacked(c->srcFormat))
1908             c->swScale= packedCopyWrapper;
1909         else /* Planar YUV or gray */
1910             c->swScale= planarCopyWrapper;
1911     }
1912 #if ARCH_BFIN
1913     ff_bfin_get_unscaled_swscale (c);
1914 #endif
1915 }
1916
1917 static void reset_ptr(const uint8_t* src[], int format)
1918 {
1919     if(!isALPHA(format))
1920         src[3]=NULL;
1921     if(!isPlanarYUV(format)) {
1922         src[3]=src[2]=NULL;
1923
1924         if (!usePal(format))
1925             src[1]= NULL;
1926     }
1927 }
1928
1929 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1930                                 const int linesizes[4])
1931 {
1932     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1933     int i;
1934
1935     for (i = 0; i < 4; i++) {
1936         int plane = desc->comp[i].plane;
1937         if (!data[plane] || !linesizes[plane])
1938             return 0;
1939     }
1940
1941     return 1;
1942 }
1943
1944 /**
1945  * swscale wrapper, so we don't need to export the SwsContext.
1946  * Assumes planar YUV to be in YUV order instead of YVU.
1947  */
1948 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1949               int srcSliceH, uint8_t* const dst[], const int dstStride[])
1950 {
1951     int i;
1952     const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1953     uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1954
1955     // do not mess up sliceDir if we have a "trailing" 0-size slice
1956     if (srcSliceH == 0)
1957         return 0;
1958
1959     if (!check_image_pointers(src, c->srcFormat, srcStride)) {
1960         av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1961         return 0;
1962     }
1963     if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1964         av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1965         return 0;
1966     }
1967
1968     if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1969         av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1970         return 0;
1971     }
1972     if (c->sliceDir == 0) {
1973         if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1974     }
1975
1976     if (usePal(c->srcFormat)) {
1977         for (i=0; i<256; i++) {
1978             int p, r, g, b, y, u, v, a = 0xff;
1979             if(c->srcFormat == PIX_FMT_PAL8) {
1980                 p=((const uint32_t*)(src[1]))[i];
1981                 a= (p>>24)&0xFF;
1982                 r= (p>>16)&0xFF;
1983                 g= (p>> 8)&0xFF;
1984                 b=  p     &0xFF;
1985             } else if(c->srcFormat == PIX_FMT_RGB8) {
1986                 r= (i>>5    )*36;
1987                 g= ((i>>2)&7)*36;
1988                 b= (i&3     )*85;
1989             } else if(c->srcFormat == PIX_FMT_BGR8) {
1990                 b= (i>>6    )*85;
1991                 g= ((i>>3)&7)*36;
1992                 r= (i&7     )*36;
1993             } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1994                 r= (i>>3    )*255;
1995                 g= ((i>>1)&3)*85;
1996                 b= (i&1     )*255;
1997             } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_GRAY8A) {
1998                 r = g = b = i;
1999             } else {
2000                 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2001                 b= (i>>3    )*255;
2002                 g= ((i>>1)&3)*85;
2003                 r= (i&1     )*255;
2004             }
2005             y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2006             u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2007             v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2008             c->pal_yuv[i]= y + (u<<8) + (v<<16) + (a<<24);
2009
2010             switch(c->dstFormat) {
2011             case PIX_FMT_BGR32:
2012 #if !HAVE_BIGENDIAN
2013             case PIX_FMT_RGB24:
2014 #endif
2015                 c->pal_rgb[i]=  r + (g<<8) + (b<<16) + (a<<24);
2016                 break;
2017             case PIX_FMT_BGR32_1:
2018 #if HAVE_BIGENDIAN
2019             case PIX_FMT_BGR24:
2020 #endif
2021                 c->pal_rgb[i]= a + (r<<8) + (g<<16) + (b<<24);
2022                 break;
2023             case PIX_FMT_RGB32_1:
2024 #if HAVE_BIGENDIAN
2025             case PIX_FMT_RGB24:
2026 #endif
2027                 c->pal_rgb[i]= a + (b<<8) + (g<<16) + (r<<24);
2028                 break;
2029             case PIX_FMT_RGB32:
2030 #if !HAVE_BIGENDIAN
2031             case PIX_FMT_BGR24:
2032 #endif
2033             default:
2034                 c->pal_rgb[i]=  b + (g<<8) + (r<<16) + (a<<24);
2035             }
2036         }
2037     }
2038
2039     // copy strides, so they can safely be modified
2040     if (c->sliceDir == 1) {
2041         // slices go from top to bottom
2042         int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2043         int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
2044
2045         reset_ptr(src2, c->srcFormat);
2046         reset_ptr((const uint8_t**)dst2, c->dstFormat);
2047
2048         /* reset slice direction at end of frame */
2049         if (srcSliceY + srcSliceH == c->srcH)
2050             c->sliceDir = 0;
2051
2052         return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
2053     } else {
2054         // slices go from bottom to top => we flip the image internally
2055         int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2056         int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2057
2058         src2[0] += (srcSliceH-1)*srcStride[0];
2059         if (!usePal(c->srcFormat))
2060             src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2061         src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2062         src2[3] += (srcSliceH-1)*srcStride[3];
2063         dst2[0] += ( c->dstH                      -1)*dstStride[0];
2064         dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2065         dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2066         dst2[3] += ( c->dstH                      -1)*dstStride[3];
2067
2068         reset_ptr(src2, c->srcFormat);
2069         reset_ptr((const uint8_t**)dst2, c->dstFormat);
2070
2071         /* reset slice direction at end of frame */
2072         if (!srcSliceY)
2073             c->sliceDir = 0;
2074
2075         return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2076     }
2077 }
2078
2079 #if LIBSWSCALE_VERSION_MAJOR < 1
2080 int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
2081                       int srcSliceH, uint8_t* dst[], int dstStride[])
2082 {
2083     return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2084 }
2085 #endif
2086
2087 /* Convert the palette to the same packed 32-bit format as the palette */
2088 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
2089 {
2090     int i;
2091
2092     for (i=0; i<num_pixels; i++)
2093         ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2094 }
2095
2096 /* Palette format: ABCD -> dst format: ABC */
2097 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels, const uint8_t *palette)
2098 {
2099     int i;
2100
2101     for (i=0; i<num_pixels; i++) {
2102         //FIXME slow?
2103         dst[0]= palette[src[i]*4+0];
2104         dst[1]= palette[src[i]*4+1];
2105         dst[2]= palette[src[i]*4+2];
2106         dst+= 3;
2107     }
2108 }