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