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