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