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