]> git.sesse.net Git - ffmpeg/blob - libswscale/swscale.c
swscale: remove duplicate code in ppc/ subdirectory.
[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/cpu.h"
66 #include "libavutil/avutil.h"
67 #include "libavutil/mathematics.h"
68 #include "libavutil/bswap.h"
69 #include "libavutil/pixdesc.h"
70
71 #undef MOVNTQ
72 #undef PAVGB
73
74 #define DITHER1XBPP
75
76 #define isPacked(x)         (       \
77            (x)==PIX_FMT_PAL8        \
78         || (x)==PIX_FMT_YUYV422     \
79         || (x)==PIX_FMT_UYVY422     \
80         || (x)==PIX_FMT_Y400A       \
81         || isAnyRGB(x)              \
82     )
83
84 #define RGB2YUV_SHIFT 15
85 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
86 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
87 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
88 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
89 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
90 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
91 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
92 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
93 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
94
95 static const double rgb2yuv_table[8][9]={
96     {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
97     {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5}, //ITU709
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.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //DEFAULT / ITU601 / ITU624 / SMPTE 170M
100     {0.59  , 0.11  , 0.30  , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
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.701 , 0.087 , 0.212 , -0.384, 0.5, -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
104 };
105
106 /*
107 NOTES
108 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
109
110 TODO
111 more intelligent misalignment avoidance for the horizontal scaler
112 write special vertical cubic upscale version
113 optimize C code (YV12 / minmax)
114 add support for packed pixel YUV input & output
115 add support for Y8 output
116 optimize BGR24 & BGR32
117 add BGR4 output support
118 write special BGR->BGR scaler
119 */
120
121 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
122 {  1,   3,   1,   3,   1,   3,   1,   3, },
123 {  2,   0,   2,   0,   2,   0,   2,   0, },
124 };
125
126 DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
127 {  6,   2,   6,   2,   6,   2,   6,   2, },
128 {  0,   4,   0,   4,   0,   4,   0,   4, },
129 };
130
131 DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
132 {  8,   4,  11,   7,   8,   4,  11,   7, },
133 {  2,  14,   1,  13,   2,  14,   1,  13, },
134 { 10,   6,   9,   5,  10,   6,   9,   5, },
135 {  0,  12,   3,  15,   0,  12,   3,  15, },
136 };
137
138 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
139 { 17,   9,  23,  15,  16,   8,  22,  14, },
140 {  5,  29,   3,  27,   4,  28,   2,  26, },
141 { 21,  13,  19,  11,  20,  12,  18,  10, },
142 {  0,  24,   6,  30,   1,  25,   7,  31, },
143 { 16,   8,  22,  14,  17,   9,  23,  15, },
144 {  4,  28,   2,  26,   5,  29,   3,  27, },
145 { 20,  12,  18,  10,  21,  13,  19,  11, },
146 {  1,  25,   7,  31,   0,  24,   6,  30, },
147 };
148
149 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
150 {  0,  55,  14,  68,   3,  58,  17,  72, },
151 { 37,  18,  50,  32,  40,  22,  54,  35, },
152 {  9,  64,   5,  59,  13,  67,   8,  63, },
153 { 46,  27,  41,  23,  49,  31,  44,  26, },
154 {  2,  57,  16,  71,   1,  56,  15,  70, },
155 { 39,  21,  52,  34,  38,  19,  51,  33, },
156 { 11,  66,   7,  62,  10,  65,   6,  60, },
157 { 48,  30,  43,  25,  47,  29,  42,  24, },
158 };
159
160 #if 1
161 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
162 {117,  62, 158, 103, 113,  58, 155, 100, },
163 { 34, 199,  21, 186,  31, 196,  17, 182, },
164 {144,  89, 131,  76, 141,  86, 127,  72, },
165 {  0, 165,  41, 206,  10, 175,  52, 217, },
166 {110,  55, 151,  96, 120,  65, 162, 107, },
167 { 28, 193,  14, 179,  38, 203,  24, 189, },
168 {138,  83, 124,  69, 148,  93, 134,  79, },
169 {  7, 172,  48, 213,   3, 168,  45, 210, },
170 };
171 #elif 1
172 // tries to correct a gamma of 1.5
173 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
174 {  0, 143,  18, 200,   2, 156,  25, 215, },
175 { 78,  28, 125,  64,  89,  36, 138,  74, },
176 { 10, 180,   3, 161,  16, 195,   8, 175, },
177 {109,  51,  93,  38, 121,  60, 105,  47, },
178 {  1, 152,  23, 210,   0, 147,  20, 205, },
179 { 85,  33, 134,  71,  81,  30, 130,  67, },
180 { 14, 190,   6, 171,  12, 185,   5, 166, },
181 {117,  57, 101,  44, 113,  54,  97,  41, },
182 };
183 #elif 1
184 // tries to correct a gamma of 2.0
185 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
186 {  0, 124,   8, 193,   0, 140,  12, 213, },
187 { 55,  14, 104,  42,  66,  19, 119,  52, },
188 {  3, 168,   1, 145,   6, 187,   3, 162, },
189 { 86,  31,  70,  21,  99,  39,  82,  28, },
190 {  0, 134,  11, 206,   0, 129,   9, 200, },
191 { 62,  17, 114,  48,  58,  16, 109,  45, },
192 {  5, 181,   2, 157,   4, 175,   1, 151, },
193 { 95,  36,  78,  26,  90,  34,  74,  24, },
194 };
195 #else
196 // tries to correct a gamma of 2.5
197 DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
198 {  0, 107,   3, 187,   0, 125,   6, 212, },
199 { 39,   7,  86,  28,  49,  11, 102,  36, },
200 {  1, 158,   0, 131,   3, 180,   1, 151, },
201 { 68,  19,  52,  12,  81,  25,  64,  17, },
202 {  0, 119,   5, 203,   0, 113,   4, 195, },
203 { 45,   9,  96,  33,  42,   8,  91,  30, },
204 {  2, 172,   1, 144,   2, 165,   0, 137, },
205 { 77,  23,  60,  15,  72,  21,  56,  14, },
206 };
207 #endif
208
209 static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
210                                                     const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
211                                                     const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
212                                                     int dstW, int chrDstW, int big_endian, int output_bits)
213 {
214     //FIXME Optimize (just quickly written not optimized..)
215     int i;
216     int shift = 11 + 16 - output_bits;
217
218 #define output_pixel(pos, val) \
219     if (big_endian) { \
220         if (output_bits == 16) { \
221             AV_WB16(pos, av_clip_uint16(val >> shift)); \
222         } else { \
223             AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
224         } \
225     } else { \
226         if (output_bits == 16) { \
227             AV_WL16(pos, av_clip_uint16(val >> shift)); \
228         } else { \
229             AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
230         } \
231     }
232     for (i = 0; i < dstW; i++) {
233         int val = 1 << (26-output_bits);
234         int j;
235
236         for (j = 0; j < lumFilterSize; j++)
237             val += lumSrc[j][i] * lumFilter[j];
238
239         output_pixel(&dest[i], val);
240     }
241
242     if (uDest) {
243         for (i = 0; i < chrDstW; i++) {
244             int u = 1 << (26-output_bits);
245             int v = 1 << (26-output_bits);
246             int j;
247
248             for (j = 0; j < chrFilterSize; j++) {
249                 u += chrSrc[j][i       ] * chrFilter[j];
250                 v += chrSrc[j][i + VOFW] * chrFilter[j];
251             }
252
253             output_pixel(&uDest[i], u);
254             output_pixel(&vDest[i], v);
255         }
256     }
257
258     if (CONFIG_SWSCALE_ALPHA && aDest) {
259         for (i = 0; i < dstW; i++) {
260             int val = 1 << (26-output_bits);
261             int j;
262
263             for (j = 0; j < lumFilterSize; j++)
264                 val += alpSrc[j][i] * lumFilter[j];
265
266             output_pixel(&aDest[i], val);
267         }
268     }
269 }
270
271 #define yuv2NBPS(bits, BE_LE, is_be) \
272 static void yuv2yuvX ## bits ## BE_LE ## _c(const int16_t *lumFilter, \
273                               const int16_t **lumSrc, int lumFilterSize, \
274                               const int16_t *chrFilter, const int16_t **chrSrc, \
275                               int chrFilterSize, const int16_t **alpSrc, \
276                               uint16_t *dest, uint16_t *uDest, uint16_t *vDest, \
277                               uint16_t *aDest, int dstW, int chrDstW) \
278 { \
279     yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize, \
280                            chrFilter, chrSrc, chrFilterSize, \
281                            alpSrc, \
282                            dest, uDest, vDest, aDest, \
283                            dstW, chrDstW, is_be, bits); \
284 }
285 yuv2NBPS( 9, BE, 1);
286 yuv2NBPS( 9, LE, 0);
287 yuv2NBPS(10, BE, 1);
288 yuv2NBPS(10, LE, 0);
289 yuv2NBPS(16, BE, 1);
290 yuv2NBPS(16, LE, 0);
291
292 static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
293                                  const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
294                                  const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
295                                  enum PixelFormat dstFormat)
296 {
297 #define conv16(bits) \
298     if (isBE(dstFormat)) { \
299         yuv2yuvX ## bits ## BE_c(lumFilter, lumSrc, lumFilterSize, \
300                                chrFilter, chrSrc, chrFilterSize, \
301                                alpSrc, \
302                                dest, uDest, vDest, aDest, \
303                                dstW, chrDstW); \
304     } else { \
305         yuv2yuvX ## bits ## LE_c(lumFilter, lumSrc, lumFilterSize, \
306                                chrFilter, chrSrc, chrFilterSize, \
307                                alpSrc, \
308                                dest, uDest, vDest, aDest, \
309                                dstW, chrDstW); \
310     }
311     if (is16BPS(dstFormat)) {
312         conv16(16);
313     } else if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
314         conv16(9);
315     } else {
316         conv16(10);
317     }
318 #undef conv16
319 }
320
321 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
322                                const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
323                                const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
324 {
325     //FIXME Optimize (just quickly written not optimized..)
326     int i;
327     for (i=0; i<dstW; i++) {
328         int val=1<<18;
329         int j;
330         for (j=0; j<lumFilterSize; j++)
331             val += lumSrc[j][i] * lumFilter[j];
332
333         dest[i]= av_clip_uint8(val>>19);
334     }
335
336     if (uDest)
337         for (i=0; i<chrDstW; i++) {
338             int u=1<<18;
339             int v=1<<18;
340             int j;
341             for (j=0; j<chrFilterSize; j++) {
342                 u += chrSrc[j][i] * chrFilter[j];
343                 v += chrSrc[j][i + VOFW] * chrFilter[j];
344             }
345
346             uDest[i]= av_clip_uint8(u>>19);
347             vDest[i]= av_clip_uint8(v>>19);
348         }
349
350     if (CONFIG_SWSCALE_ALPHA && aDest)
351         for (i=0; i<dstW; i++) {
352             int val=1<<18;
353             int j;
354             for (j=0; j<lumFilterSize; j++)
355                 val += alpSrc[j][i] * lumFilter[j];
356
357             aDest[i]= av_clip_uint8(val>>19);
358         }
359
360 }
361
362 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
363                                 const int16_t *chrFilter, const int16_t **chrSrc, 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 += chrSrc[j][i] * chrFilter[j];
387                 v += chrSrc[j][i + VOFW] * 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 += chrSrc[j][i] * chrFilter[j];
400                 v += chrSrc[j][i + VOFW] * 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 += chrSrc[j][i] * chrFilter[j];\
425             V += chrSrc[j][i+VOFW] * 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 += chrSrc[j][i     ] * chrFilter[j];\
473             V += chrSrc[j][i+VOFW] * 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= (uvbuf0[i     ]*uvalpha1+uvbuf1[i     ]*uvalpha)>>19;  \
539         int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*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= (uvbuf1[i     ])>>7;\
565         int V= (uvbuf1[i+VOFW])>>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= (uvbuf0[i     ] + uvbuf1[i     ])>>8;\
591         int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>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, const int16_t **lumSrc, int lumFilterSize,
874                                   const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
875                                   const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
876 {
877     int i;
878     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)
879 }
880
881 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
882                                     const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
883                                     const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
884 {
885     int i;
886     int step= c->dstFormatBpp/8;
887     int aidx= 3;
888
889     switch(c->dstFormat) {
890     case PIX_FMT_ARGB:
891         dest++;
892         aidx= 0;
893     case PIX_FMT_RGB24:
894         aidx--;
895     case PIX_FMT_RGBA:
896         if (CONFIG_SMALL) {
897             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
898             YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
899                 dest[aidx]= needAlpha ? A : 255;
900                 dest[0]= R>>22;
901                 dest[1]= G>>22;
902                 dest[2]= B>>22;
903                 dest+= step;
904             }
905         } else {
906             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
907                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
908                     dest[aidx]= A;
909                     dest[0]= R>>22;
910                     dest[1]= G>>22;
911                     dest[2]= B>>22;
912                     dest+= step;
913                 }
914             } else {
915                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
916                     dest[aidx]= 255;
917                     dest[0]= R>>22;
918                     dest[1]= G>>22;
919                     dest[2]= B>>22;
920                     dest+= step;
921                 }
922             }
923         }
924         break;
925     case PIX_FMT_ABGR:
926         dest++;
927         aidx= 0;
928     case PIX_FMT_BGR24:
929         aidx--;
930     case PIX_FMT_BGRA:
931         if (CONFIG_SMALL) {
932             int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
933             YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
934                 dest[aidx]= needAlpha ? A : 255;
935                 dest[0]= B>>22;
936                 dest[1]= G>>22;
937                 dest[2]= R>>22;
938                 dest+= step;
939             }
940         } else {
941             if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
942                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
943                     dest[aidx]= A;
944                     dest[0]= B>>22;
945                     dest[1]= G>>22;
946                     dest[2]= R>>22;
947                     dest+= step;
948                 }
949             } else {
950                 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
951                     dest[aidx]= 255;
952                     dest[0]= B>>22;
953                     dest[1]= G>>22;
954                     dest[2]= R>>22;
955                     dest+= step;
956                 }
957             }
958         }
959         break;
960     default:
961         assert(0);
962     }
963 }
964
965 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
966 {
967     int i;
968     uint8_t *ptr = plane + stride*y;
969     for (i=0; i<height; i++) {
970         memset(ptr, val, width);
971         ptr += stride;
972     }
973 }
974
975 static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, long width,
976                             uint32_t *unused)
977 {
978     int i;
979     for (i = 0; i < width; i++) {
980         int r = src[i*6+0];
981         int g = src[i*6+2];
982         int b = src[i*6+4];
983
984         dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
985     }
986 }
987
988 static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
989                              const uint8_t *src1, const uint8_t *src2,
990                              long width, uint32_t *unused)
991 {
992     int i;
993     assert(src1==src2);
994     for (i = 0; i < width; i++) {
995         int r = src1[6*i + 0];
996         int g = src1[6*i + 2];
997         int b = src1[6*i + 4];
998
999         dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1000         dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1001     }
1002 }
1003
1004 static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1005                                   const uint8_t *src1, const uint8_t *src2,
1006                                   long width, uint32_t *unused)
1007 {
1008     int i;
1009     assert(src1==src2);
1010     for (i = 0; i < width; i++) {
1011         int r= src1[12*i + 0] + src1[12*i + 6];
1012         int g= src1[12*i + 2] + src1[12*i + 8];
1013         int b= src1[12*i + 4] + src1[12*i + 10];
1014
1015         dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1016         dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1017     }
1018 }
1019
1020 static inline void bgr48ToY(uint8_t *dst, const uint8_t *src, long width,
1021                             uint32_t *unused)
1022 {
1023     int i;
1024     for (i = 0; i < width; i++) {
1025         int b = src[i*6+0];
1026         int g = src[i*6+2];
1027         int r = src[i*6+4];
1028
1029         dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1030     }
1031 }
1032
1033 static inline void bgr48ToUV(uint8_t *dstU, uint8_t *dstV,
1034                              const uint8_t *src1, const uint8_t *src2,
1035                              long width, uint32_t *unused)
1036 {
1037     int i;
1038     for (i = 0; i < width; i++) {
1039         int b = src1[6*i + 0];
1040         int g = src1[6*i + 2];
1041         int r = src1[6*i + 4];
1042
1043         dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1044         dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
1045     }
1046 }
1047
1048 static inline void bgr48ToUV_half(uint8_t *dstU, uint8_t *dstV,
1049                                   const uint8_t *src1, const uint8_t *src2,
1050                                   long width, uint32_t *unused)
1051 {
1052     int i;
1053     for (i = 0; i < width; i++) {
1054         int b= src1[12*i + 0] + src1[12*i + 6];
1055         int g= src1[12*i + 2] + src1[12*i + 8];
1056         int r= src1[12*i + 4] + src1[12*i + 10];
1057
1058         dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1059         dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
1060     }
1061 }
1062
1063 #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
1064 static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
1065 {\
1066     int i;\
1067     for (i=0; i<width; i++) {\
1068         int b= (((const type*)src)[i]>>shb)&maskb;\
1069         int g= (((const type*)src)[i]>>shg)&maskg;\
1070         int r= (((const type*)src)[i]>>shr)&maskr;\
1071 \
1072         dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
1073     }\
1074 }
1075
1076 BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1077 BGR2Y(uint32_t,bgr321ToY,16,16, 0, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1078 BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY   , BY<< 8, RGB2YUV_SHIFT+8)
1079 BGR2Y(uint32_t,rgb321ToY, 0,16,16, 0xFF00, 0x00FF, 0xFF00, RY    , GY<<8, BY    , RGB2YUV_SHIFT+8)
1080 BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY    , RGB2YUV_SHIFT+8)
1081 BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY    , RGB2YUV_SHIFT+7)
1082 BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY    , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
1083 BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY    , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
1084
1085 static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1086 {
1087     int i;
1088     for (i=0; i<width; i++) {
1089         dst[i]= src[4*i];
1090     }
1091 }
1092
1093 #define BGR2UV(type, name, shr, shg, shb, shp, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S) \
1094 static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1095 {\
1096     int i;\
1097     for (i=0; i<width; i++) {\
1098         int b= ((((const type*)src)[i]>>shp)&maskb)>>shb;\
1099         int g= ((((const type*)src)[i]>>shp)&maskg)>>shg;\
1100         int r= ((((const type*)src)[i]>>shp)&maskr)>>shr;\
1101 \
1102         dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
1103         dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
1104     }\
1105 }\
1106 static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
1107 {\
1108     int i;\
1109     for (i=0; i<width; i++) {\
1110         int pix0= ((const type*)src)[2*i+0]>>shp;\
1111         int pix1= ((const type*)src)[2*i+1]>>shp;\
1112         int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
1113         int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
1114         int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
1115         g&= maskg|(2*maskg);\
1116 \
1117         g>>=shg;\
1118 \
1119         dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
1120         dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
1121     }\
1122 }
1123
1124 BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1125 BGR2UV(uint32_t,bgr321ToUV,16, 0, 0, 8, 0xFF0000, 0xFF00,   0x00FF, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1126 BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1127 BGR2UV(uint32_t,rgb321ToUV, 0, 0,16, 8,   0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU   , BU<< 8, RV<< 8, GV   , BV<< 8, RGB2YUV_SHIFT+8)
1128 BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0,   0x001F, 0x07E0,   0xF800, RU<<11, GU<<5, BU    , RV<<11, GV<<5, BV    , RGB2YUV_SHIFT+8)
1129 BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0,   0x001F, 0x03E0,   0x7C00, RU<<10, GU<<5, BU    , RV<<10, GV<<5, BV    , RGB2YUV_SHIFT+7)
1130 BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0,   0xF800, 0x07E0,   0x001F, RU    , GU<<5, BU<<11, RV    , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
1131 BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0,   0x7C00, 0x03E0,   0x001F, RU    , GU<<5, BU<<10, RV    , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
1132
1133 static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
1134 {
1135     int i;
1136     for (i=0; i<width; i++) {
1137         int d= src[i];
1138
1139         dst[i]= pal[d] & 0xFF;
1140     }
1141 }
1142
1143 static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
1144                            const uint8_t *src1, const uint8_t *src2,
1145                            long width, uint32_t *pal)
1146 {
1147     int i;
1148     assert(src1 == src2);
1149     for (i=0; i<width; i++) {
1150         int p= pal[src1[i]];
1151
1152         dstU[i]= p>>8;
1153         dstV[i]= p>>16;
1154     }
1155 }
1156
1157 static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1158 {
1159     int i, j;
1160     for (i=0; i<width/8; i++) {
1161         int d= ~src[i];
1162         for(j=0; j<8; j++)
1163             dst[8*i+j]= ((d>>(7-j))&1)*255;
1164     }
1165 }
1166
1167 static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
1168 {
1169     int i, j;
1170     for (i=0; i<width/8; i++) {
1171         int d= src[i];
1172         for(j=0; j<8; j++)
1173             dst[8*i+j]= ((d>>(7-j))&1)*255;
1174     }
1175 }
1176
1177 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1178 //Plain C versions
1179
1180 #define COMPILE_TEMPLATE_MMX2 0
1181 #define COMPILE_TEMPLATE_AMD3DNOW 0
1182 #define COMPILE_TEMPLATE_ALTIVEC 0
1183
1184 #include "swscale_template.c"
1185
1186 #if HAVE_ALTIVEC
1187 #undef RENAME
1188 #undef COMPILE_TEMPLATE_ALTIVEC
1189 #define COMPILE_TEMPLATE_ALTIVEC 1
1190 #define RENAME(a) a ## _altivec
1191 #include "ppc/swscale_template.c"
1192 #endif
1193
1194 //MMX versions
1195 #if HAVE_MMX
1196 #undef RENAME
1197 #undef COMPILE_TEMPLATE_MMX2
1198 #undef COMPILE_TEMPLATE_AMD3DNOW
1199 #define COMPILE_TEMPLATE_MMX2 0
1200 #define COMPILE_TEMPLATE_AMD3DNOW 0
1201 #define RENAME(a) a ## _MMX
1202 #include "x86/swscale_template.c"
1203 #endif
1204
1205 //MMX2 versions
1206 #if HAVE_MMX2
1207 #undef RENAME
1208 #undef COMPILE_TEMPLATE_MMX2
1209 #undef COMPILE_TEMPLATE_AMD3DNOW
1210 #define COMPILE_TEMPLATE_MMX2 1
1211 #define COMPILE_TEMPLATE_AMD3DNOW 0
1212 #define RENAME(a) a ## _MMX2
1213 #include "x86/swscale_template.c"
1214 #endif
1215
1216 //3DNOW versions
1217 #if HAVE_AMD3DNOW
1218 #undef RENAME
1219 #undef COMPILE_TEMPLATE_MMX2
1220 #undef COMPILE_TEMPLATE_AMD3DNOW
1221 #define COMPILE_TEMPLATE_MMX2 0
1222 #define COMPILE_TEMPLATE_AMD3DNOW 1
1223 #define RENAME(a) a ## _3DNow
1224 #include "x86/swscale_template.c"
1225 #endif
1226
1227 SwsFunc ff_getSwsFunc(SwsContext *c)
1228 {
1229     int cpu_flags = av_get_cpu_flags();
1230
1231     sws_init_swScale_c(c);
1232
1233 #if HAVE_MMX2
1234     // ordered per speed fastest first
1235     if (cpu_flags & AV_CPU_FLAG_MMX2) {
1236         sws_init_swScale_MMX2(c);
1237         return swScale_MMX2;
1238     } else
1239 #endif
1240 #if HAVE_AMD3DNOW
1241     if (cpu_flags & AV_CPU_FLAG_3DNOW) {
1242         sws_init_swScale_3DNow(c);
1243         return swScale_3DNow;
1244     } else
1245 #endif
1246 #if HAVE_MMX
1247     if (cpu_flags & AV_CPU_FLAG_MMX) {
1248         sws_init_swScale_MMX(c);
1249         return swScale_MMX;
1250     } else
1251 #endif
1252 #if HAVE_ALTIVEC
1253     if (cpu_flags & AV_CPU_FLAG_ALTIVEC)
1254         sws_init_swScale_altivec(c);
1255 #endif
1256
1257     return swScale_c;
1258 }
1259
1260 static void copyPlane(const uint8_t *src, int srcStride,
1261                       int srcSliceY, int srcSliceH, int width,
1262                       uint8_t *dst, int dstStride)
1263 {
1264     dst += dstStride * srcSliceY;
1265     if (dstStride == srcStride && srcStride > 0) {
1266         memcpy(dst, src, srcSliceH * dstStride);
1267     } else {
1268         int i;
1269         for (i=0; i<srcSliceH; i++) {
1270             memcpy(dst, src, width);
1271             src += srcStride;
1272             dst += dstStride;
1273         }
1274     }
1275 }
1276
1277 static int planarToNv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1278                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1279 {
1280     uint8_t *dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1281
1282     copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1283               dstParam[0], dstStride[0]);
1284
1285     if (c->dstFormat == PIX_FMT_NV12)
1286         interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1287     else
1288         interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1289
1290     return srcSliceH;
1291 }
1292
1293 static int planarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1294                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1295 {
1296     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1297
1298     yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1299
1300     return srcSliceH;
1301 }
1302
1303 static int planarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1304                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1305 {
1306     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1307
1308     yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1309
1310     return srcSliceH;
1311 }
1312
1313 static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1314                                 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1315 {
1316     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1317
1318     yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1319
1320     return srcSliceH;
1321 }
1322
1323 static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1324                                 int srcSliceH, uint8_t* dstParam[], int dstStride[])
1325 {
1326     uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1327
1328     yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1329
1330     return srcSliceH;
1331 }
1332
1333 static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1334                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1335 {
1336     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1337     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1338     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1339
1340     yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1341
1342     if (dstParam[3])
1343         fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1344
1345     return srcSliceH;
1346 }
1347
1348 static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1349                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1350 {
1351     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1352     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1353     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1354
1355     yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1356
1357     return srcSliceH;
1358 }
1359
1360 static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1361                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1362 {
1363     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1364     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1365     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1366
1367     uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1368
1369     if (dstParam[3])
1370         fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1371
1372     return srcSliceH;
1373 }
1374
1375 static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1376                                int srcSliceH, uint8_t* dstParam[], int dstStride[])
1377 {
1378     uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1379     uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1380     uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1381
1382     uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1383
1384     return srcSliceH;
1385 }
1386
1387 static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1388 {
1389     long i;
1390     for (i=0; i<num_pixels; i++)
1391         ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | (src[(i<<1)+1] << 24);
1392 }
1393
1394 static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1395 {
1396     long i;
1397
1398     for (i=0; i<num_pixels; i++)
1399         ((uint32_t *) dst)[i] = ((const uint32_t *)palette)[src[i<<1]] | src[(i<<1)+1];
1400 }
1401
1402 static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
1403 {
1404     long i;
1405
1406     for (i=0; i<num_pixels; i++) {
1407         //FIXME slow?
1408         dst[0]= palette[src[i<<1]*4+0];
1409         dst[1]= palette[src[i<<1]*4+1];
1410         dst[2]= palette[src[i<<1]*4+2];
1411         dst+= 3;
1412     }
1413 }
1414
1415 static int palToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1416                            int srcSliceH, uint8_t* dst[], int dstStride[])
1417 {
1418     const enum PixelFormat srcFormat= c->srcFormat;
1419     const enum PixelFormat dstFormat= c->dstFormat;
1420     void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1421                  const uint8_t *palette)=NULL;
1422     int i;
1423     uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1424     const uint8_t *srcPtr= src[0];
1425
1426     if (srcFormat == PIX_FMT_Y400A) {
1427         switch (dstFormat) {
1428         case PIX_FMT_RGB32  : conv = gray8aToPacked32; break;
1429         case PIX_FMT_BGR32  : conv = gray8aToPacked32; break;
1430         case PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
1431         case PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
1432         case PIX_FMT_RGB24  : conv = gray8aToPacked24; break;
1433         case PIX_FMT_BGR24  : conv = gray8aToPacked24; break;
1434         }
1435     } else if (usePal(srcFormat)) {
1436         switch (dstFormat) {
1437         case PIX_FMT_RGB32  : conv = sws_convertPalette8ToPacked32; break;
1438         case PIX_FMT_BGR32  : conv = sws_convertPalette8ToPacked32; break;
1439         case PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
1440         case PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
1441         case PIX_FMT_RGB24  : conv = sws_convertPalette8ToPacked24; break;
1442         case PIX_FMT_BGR24  : conv = sws_convertPalette8ToPacked24; break;
1443         }
1444     }
1445
1446     if (!conv)
1447         av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1448                sws_format_name(srcFormat), sws_format_name(dstFormat));
1449     else {
1450         for (i=0; i<srcSliceH; i++) {
1451             conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1452             srcPtr+= srcStride[0];
1453             dstPtr+= dstStride[0];
1454         }
1455     }
1456
1457     return srcSliceH;
1458 }
1459
1460 #define isRGBA32(x) (            \
1461            (x) == PIX_FMT_ARGB   \
1462         || (x) == PIX_FMT_RGBA   \
1463         || (x) == PIX_FMT_BGRA   \
1464         || (x) == PIX_FMT_ABGR   \
1465         )
1466
1467 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1468 static int rgbToRgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1469                            int srcSliceH, uint8_t* dst[], int dstStride[])
1470 {
1471     const enum PixelFormat srcFormat= c->srcFormat;
1472     const enum PixelFormat dstFormat= c->dstFormat;
1473     const int srcBpp= (c->srcFormatBpp + 7) >> 3;
1474     const int dstBpp= (c->dstFormatBpp + 7) >> 3;
1475     const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1476     const int dstId= c->dstFormatBpp >> 2;
1477     void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1478
1479 #define CONV_IS(src, dst) (srcFormat == PIX_FMT_##src && dstFormat == PIX_FMT_##dst)
1480
1481     if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
1482         if (     CONV_IS(ABGR, RGBA)
1483               || CONV_IS(ARGB, BGRA)
1484               || CONV_IS(BGRA, ARGB)
1485               || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
1486         else if (CONV_IS(ABGR, ARGB)
1487               || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
1488         else if (CONV_IS(ABGR, BGRA)
1489               || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
1490         else if (CONV_IS(BGRA, RGBA)
1491               || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
1492         else if (CONV_IS(BGRA, ABGR)
1493               || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
1494     } else
1495     /* BGR -> BGR */
1496     if (  (isBGRinInt(srcFormat) && isBGRinInt(dstFormat))
1497        || (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
1498         switch(srcId | (dstId<<4)) {
1499         case 0x34: conv= rgb16to15; break;
1500         case 0x36: conv= rgb24to15; break;
1501         case 0x38: conv= rgb32to15; break;
1502         case 0x43: conv= rgb15to16; break;
1503         case 0x46: conv= rgb24to16; break;
1504         case 0x48: conv= rgb32to16; break;
1505         case 0x63: conv= rgb15to24; break;
1506         case 0x64: conv= rgb16to24; break;
1507         case 0x68: conv= rgb32to24; break;
1508         case 0x83: conv= rgb15to32; break;
1509         case 0x84: conv= rgb16to32; break;
1510         case 0x86: conv= rgb24to32; break;
1511         }
1512     } else if (  (isBGRinInt(srcFormat) && isRGBinInt(dstFormat))
1513              || (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
1514         switch(srcId | (dstId<<4)) {
1515         case 0x33: conv= rgb15tobgr15; break;
1516         case 0x34: conv= rgb16tobgr15; break;
1517         case 0x36: conv= rgb24tobgr15; break;
1518         case 0x38: conv= rgb32tobgr15; break;
1519         case 0x43: conv= rgb15tobgr16; break;
1520         case 0x44: conv= rgb16tobgr16; break;
1521         case 0x46: conv= rgb24tobgr16; break;
1522         case 0x48: conv= rgb32tobgr16; break;
1523         case 0x63: conv= rgb15tobgr24; break;
1524         case 0x64: conv= rgb16tobgr24; break;
1525         case 0x66: conv= rgb24tobgr24; break;
1526         case 0x68: conv= rgb32tobgr24; break;
1527         case 0x83: conv= rgb15tobgr32; break;
1528         case 0x84: conv= rgb16tobgr32; break;
1529         case 0x86: conv= rgb24tobgr32; break;
1530         }
1531     }
1532
1533     if (!conv) {
1534         av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1535                sws_format_name(srcFormat), sws_format_name(dstFormat));
1536     } else {
1537         const uint8_t *srcPtr= src[0];
1538               uint8_t *dstPtr= dst[0];
1539         if ((srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1) && !isRGBA32(dstFormat))
1540             srcPtr += ALT32_CORR;
1541
1542         if ((dstFormat == PIX_FMT_RGB32_1 || dstFormat == PIX_FMT_BGR32_1) && !isRGBA32(srcFormat))
1543             dstPtr += ALT32_CORR;
1544
1545         if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1546             conv(srcPtr, dstPtr + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1547         else {
1548             int i;
1549             dstPtr += dstStride[0]*srcSliceY;
1550
1551             for (i=0; i<srcSliceH; i++) {
1552                 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1553                 srcPtr+= srcStride[0];
1554                 dstPtr+= dstStride[0];
1555             }
1556         }
1557     }
1558     return srcSliceH;
1559 }
1560
1561 static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1562                               int srcSliceH, uint8_t* dst[], int dstStride[])
1563 {
1564     rgb24toyv12(
1565         src[0],
1566         dst[0]+ srcSliceY    *dstStride[0],
1567         dst[1]+(srcSliceY>>1)*dstStride[1],
1568         dst[2]+(srcSliceY>>1)*dstStride[2],
1569         c->srcW, srcSliceH,
1570         dstStride[0], dstStride[1], srcStride[0]);
1571     if (dst[3])
1572         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1573     return srcSliceH;
1574 }
1575
1576 static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1577                              int srcSliceH, uint8_t* dst[], int dstStride[])
1578 {
1579     copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
1580               dst[0], dstStride[0]);
1581
1582     planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
1583              srcSliceH >> 2, srcStride[1], dstStride[1]);
1584     planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
1585              srcSliceH >> 2, srcStride[2], dstStride[2]);
1586     if (dst[3])
1587         fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1588     return srcSliceH;
1589 }
1590
1591 /* unscaled copy like stuff (assumes nearly identical formats) */
1592 static int packedCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1593                              int srcSliceH, uint8_t* dst[], int dstStride[])
1594 {
1595     if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1596         memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1597     else {
1598         int i;
1599         const uint8_t *srcPtr= src[0];
1600         uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1601         int length=0;
1602
1603         /* universal length finder */
1604         while(length+c->srcW <= FFABS(dstStride[0])
1605            && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1606         assert(length!=0);
1607
1608         for (i=0; i<srcSliceH; i++) {
1609             memcpy(dstPtr, srcPtr, length);
1610             srcPtr+= srcStride[0];
1611             dstPtr+= dstStride[0];
1612         }
1613     }
1614     return srcSliceH;
1615 }
1616
1617 static int planarCopyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
1618                              int srcSliceH, uint8_t* dst[], int dstStride[])
1619 {
1620     int plane, i, j;
1621     for (plane=0; plane<4; plane++) {
1622         int length= (plane==0 || plane==3) ? c->srcW  : -((-c->srcW  )>>c->chrDstHSubSample);
1623         int y=      (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1624         int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1625         const uint8_t *srcPtr= src[plane];
1626         uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1627
1628         if (!dst[plane]) continue;
1629         // ignore palette for GRAY8
1630         if (plane == 1 && !dst[2]) continue;
1631         if (!src[plane] || (plane == 1 && !src[2])) {
1632             if(is16BPS(c->dstFormat))
1633                 length*=2;
1634             fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
1635         } else {
1636             if(is9_OR_10BPS(c->srcFormat)) {
1637                 const int src_depth = av_pix_fmt_descriptors[c->srcFormat].comp[plane].depth_minus1+1;
1638                 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
1639                 const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
1640
1641                 if (is16BPS(c->dstFormat)) {
1642                     uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1643 #define COPY9_OR_10TO16(rfunc, wfunc) \
1644                     for (i = 0; i < height; i++) { \
1645                         for (j = 0; j < length; j++) { \
1646                             int srcpx = rfunc(&srcPtr2[j]); \
1647                             wfunc(&dstPtr2[j], (srcpx<<(16-src_depth)) | (srcpx>>(2*src_depth-16))); \
1648                         } \
1649                         dstPtr2 += dstStride[plane]/2; \
1650                         srcPtr2 += srcStride[plane]/2; \
1651                     }
1652                     if (isBE(c->dstFormat)) {
1653                         if (isBE(c->srcFormat)) {
1654                             COPY9_OR_10TO16(AV_RB16, AV_WB16);
1655                         } else {
1656                             COPY9_OR_10TO16(AV_RL16, AV_WB16);
1657                         }
1658                     } else {
1659                         if (isBE(c->srcFormat)) {
1660                             COPY9_OR_10TO16(AV_RB16, AV_WL16);
1661                         } else {
1662                             COPY9_OR_10TO16(AV_RL16, AV_WL16);
1663                         }
1664                     }
1665                 } else if (is9_OR_10BPS(c->dstFormat)) {
1666                     uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1667 #define COPY9_OR_10TO9_OR_10(loop) \
1668                     for (i = 0; i < height; i++) { \
1669                         for (j = 0; j < length; j++) { \
1670                             loop; \
1671                         } \
1672                         dstPtr2 += dstStride[plane]/2; \
1673                         srcPtr2 += srcStride[plane]/2; \
1674                     }
1675 #define COPY9_OR_10TO9_OR_10_2(rfunc, wfunc) \
1676                     if (dst_depth > src_depth) { \
1677                         COPY9_OR_10TO9_OR_10(int srcpx = rfunc(&srcPtr2[j]); \
1678                             wfunc(&dstPtr2[j], (srcpx << 1) | (srcpx >> 9))); \
1679                     } else if (dst_depth < src_depth) { \
1680                         COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]) >> 1)); \
1681                     } else { \
1682                         COPY9_OR_10TO9_OR_10(wfunc(&dstPtr2[j], rfunc(&srcPtr2[j]))); \
1683                     }
1684                     if (isBE(c->dstFormat)) {
1685                         if (isBE(c->srcFormat)) {
1686                             COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WB16);
1687                         } else {
1688                             COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WB16);
1689                         }
1690                     } else {
1691                         if (isBE(c->srcFormat)) {
1692                             COPY9_OR_10TO9_OR_10_2(AV_RB16, AV_WL16);
1693                         } else {
1694                             COPY9_OR_10TO9_OR_10_2(AV_RL16, AV_WL16);
1695                         }
1696                     }
1697                 } else {
1698                     // FIXME Maybe dither instead.
1699 #define COPY9_OR_10TO8(rfunc) \
1700                     for (i = 0; i < height; i++) { \
1701                         for (j = 0; j < length; j++) { \
1702                             dstPtr[j] = rfunc(&srcPtr2[j])>>(src_depth-8); \
1703                         } \
1704                         dstPtr  += dstStride[plane]; \
1705                         srcPtr2 += srcStride[plane]/2; \
1706                     }
1707                     if (isBE(c->srcFormat)) {
1708                         COPY9_OR_10TO8(AV_RB16);
1709                     } else {
1710                         COPY9_OR_10TO8(AV_RL16);
1711                     }
1712                 }
1713             } else if(is9_OR_10BPS(c->dstFormat)) {
1714                 const int dst_depth = av_pix_fmt_descriptors[c->dstFormat].comp[plane].depth_minus1+1;
1715                 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
1716
1717                 if (is16BPS(c->srcFormat)) {
1718                     const uint16_t *srcPtr2 = (const uint16_t*)srcPtr;
1719 #define COPY16TO9_OR_10(rfunc, wfunc) \
1720                     for (i = 0; i < height; i++) { \
1721                         for (j = 0; j < length; j++) { \
1722                             wfunc(&dstPtr2[j], rfunc(&srcPtr2[j])>>(16-dst_depth)); \
1723                         } \
1724                         dstPtr2 += dstStride[plane]/2; \
1725                         srcPtr2 += srcStride[plane]/2; \
1726                     }
1727                     if (isBE(c->dstFormat)) {
1728                         if (isBE(c->srcFormat)) {
1729                             COPY16TO9_OR_10(AV_RB16, AV_WB16);
1730                         } else {
1731                             COPY16TO9_OR_10(AV_RL16, AV_WB16);
1732                         }
1733                     } else {
1734                         if (isBE(c->srcFormat)) {
1735                             COPY16TO9_OR_10(AV_RB16, AV_WL16);
1736                         } else {
1737                             COPY16TO9_OR_10(AV_RL16, AV_WL16);
1738                         }
1739                     }
1740                 } else /* 8bit */ {
1741 #define COPY8TO9_OR_10(wfunc) \
1742                     for (i = 0; i < height; i++) { \
1743                         for (j = 0; j < length; j++) { \
1744                             const int srcpx = srcPtr[j]; \
1745                             wfunc(&dstPtr2[j], (srcpx<<(dst_depth-8)) | (srcpx >> (16-dst_depth))); \
1746                         } \
1747                         dstPtr2 += dstStride[plane]/2; \
1748                         srcPtr  += srcStride[plane]; \
1749                     }
1750                     if (isBE(c->dstFormat)) {
1751                         COPY8TO9_OR_10(AV_WB16);
1752                     } else {
1753                         COPY8TO9_OR_10(AV_WL16);
1754                     }
1755                 }
1756             } else if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
1757                 if (!isBE(c->srcFormat)) srcPtr++;
1758                 for (i=0; i<height; i++) {
1759                     for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1760                     srcPtr+= srcStride[plane];
1761                     dstPtr+= dstStride[plane];
1762                 }
1763             } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
1764                 for (i=0; i<height; i++) {
1765                     for (j=0; j<length; j++) {
1766                         dstPtr[ j<<1   ] = srcPtr[j];
1767                         dstPtr[(j<<1)+1] = srcPtr[j];
1768                     }
1769                     srcPtr+= srcStride[plane];
1770                     dstPtr+= dstStride[plane];
1771                 }
1772             } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
1773                   && isBE(c->srcFormat) != isBE(c->dstFormat)) {
1774
1775                 for (i=0; i<height; i++) {
1776                     for (j=0; j<length; j++)
1777                         ((uint16_t*)dstPtr)[j] = av_bswap16(((const uint16_t*)srcPtr)[j]);
1778                     srcPtr+= srcStride[plane];
1779                     dstPtr+= dstStride[plane];
1780                 }
1781             } else if (dstStride[plane] == srcStride[plane] &&
1782                        srcStride[plane] > 0 && srcStride[plane] == length) {
1783                 memcpy(dst[plane] + dstStride[plane]*y, src[plane],
1784                        height*dstStride[plane]);
1785             } else {
1786                 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
1787                     length*=2;
1788                 for (i=0; i<height; i++) {
1789                     memcpy(dstPtr, srcPtr, length);
1790                     srcPtr+= srcStride[plane];
1791                     dstPtr+= dstStride[plane];
1792                 }
1793             }
1794         }
1795     }
1796     return srcSliceH;
1797 }
1798
1799 void ff_get_unscaled_swscale(SwsContext *c)
1800 {
1801     const enum PixelFormat srcFormat = c->srcFormat;
1802     const enum PixelFormat dstFormat = c->dstFormat;
1803     const int flags = c->flags;
1804     const int dstH = c->dstH;
1805     int needsDither;
1806
1807     needsDither= isAnyRGB(dstFormat)
1808         &&  c->dstFormatBpp < 24
1809         && (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
1810
1811     /* yv12_to_nv12 */
1812     if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
1813         c->swScale= planarToNv12Wrapper;
1814     }
1815     /* yuv2bgr */
1816     if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && isAnyRGB(dstFormat)
1817         && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
1818         c->swScale= ff_yuv2rgb_get_func_ptr(c);
1819     }
1820
1821     if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
1822         c->swScale= yvu9ToYv12Wrapper;
1823     }
1824
1825     /* bgr24toYV12 */
1826     if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
1827         c->swScale= bgr24ToYv12Wrapper;
1828
1829     /* RGB/BGR -> RGB/BGR (no dither needed forms) */
1830     if (   isAnyRGB(srcFormat)
1831         && isAnyRGB(dstFormat)
1832         && srcFormat != PIX_FMT_BGR8      && dstFormat != PIX_FMT_BGR8
1833         && srcFormat != PIX_FMT_RGB8      && dstFormat != PIX_FMT_RGB8
1834         && srcFormat != PIX_FMT_BGR4      && dstFormat != PIX_FMT_BGR4
1835         && srcFormat != PIX_FMT_RGB4      && dstFormat != PIX_FMT_RGB4
1836         && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
1837         && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
1838         && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
1839         && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
1840         && srcFormat != PIX_FMT_RGB48LE   && dstFormat != PIX_FMT_RGB48LE
1841         && srcFormat != PIX_FMT_RGB48BE   && dstFormat != PIX_FMT_RGB48BE
1842         && srcFormat != PIX_FMT_BGR48LE   && dstFormat != PIX_FMT_BGR48LE
1843         && srcFormat != PIX_FMT_BGR48BE   && dstFormat != PIX_FMT_BGR48BE
1844         && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
1845         c->swScale= rgbToRgbWrapper;
1846
1847     if ((usePal(srcFormat) && (
1848         dstFormat == PIX_FMT_RGB32   ||
1849         dstFormat == PIX_FMT_RGB32_1 ||
1850         dstFormat == PIX_FMT_RGB24   ||
1851         dstFormat == PIX_FMT_BGR32   ||
1852         dstFormat == PIX_FMT_BGR32_1 ||
1853         dstFormat == PIX_FMT_BGR24)))
1854         c->swScale= palToRgbWrapper;
1855
1856     if (srcFormat == PIX_FMT_YUV422P) {
1857         if (dstFormat == PIX_FMT_YUYV422)
1858             c->swScale= yuv422pToYuy2Wrapper;
1859         else if (dstFormat == PIX_FMT_UYVY422)
1860             c->swScale= yuv422pToUyvyWrapper;
1861     }
1862
1863     /* LQ converters if -sws 0 or -sws 4*/
1864     if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
1865         /* yv12_to_yuy2 */
1866         if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
1867             if (dstFormat == PIX_FMT_YUYV422)
1868                 c->swScale= planarToYuy2Wrapper;
1869             else if (dstFormat == PIX_FMT_UYVY422)
1870                 c->swScale= planarToUyvyWrapper;
1871         }
1872     }
1873     if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1874         c->swScale= yuyvToYuv420Wrapper;
1875     if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
1876         c->swScale= uyvyToYuv420Wrapper;
1877     if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
1878         c->swScale= yuyvToYuv422Wrapper;
1879     if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
1880         c->swScale= uyvyToYuv422Wrapper;
1881
1882 #if HAVE_ALTIVEC
1883     if ((av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) &&
1884         !(c->flags & SWS_BITEXACT) &&
1885         srcFormat == PIX_FMT_YUV420P) {
1886         // unscaled YV12 -> packed YUV, we want speed
1887         if (dstFormat == PIX_FMT_YUYV422)
1888             c->swScale= yv12toyuy2_unscaled_altivec;
1889         else if (dstFormat == PIX_FMT_UYVY422)
1890             c->swScale= yv12touyvy_unscaled_altivec;
1891     }
1892 #endif
1893
1894     /* simple copy */
1895     if (  srcFormat == dstFormat
1896         || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
1897         || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
1898         || (isPlanarYUV(srcFormat) && isGray(dstFormat))
1899         || (isPlanarYUV(dstFormat) && isGray(srcFormat))
1900         || (isGray(dstFormat) && isGray(srcFormat))
1901         || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
1902             && c->chrDstHSubSample == c->chrSrcHSubSample
1903             && c->chrDstVSubSample == c->chrSrcVSubSample
1904             && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
1905             && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
1906     {
1907         if (isPacked(c->srcFormat))
1908             c->swScale= packedCopyWrapper;
1909         else /* Planar YUV or gray */
1910             c->swScale= planarCopyWrapper;
1911     }
1912 #if ARCH_BFIN
1913     ff_bfin_get_unscaled_swscale (c);
1914 #endif
1915 }
1916
1917 static void reset_ptr(const uint8_t* src[], int format)
1918 {
1919     if(!isALPHA(format))
1920         src[3]=NULL;
1921     if(!isPlanarYUV(format)) {
1922         src[3]=src[2]=NULL;
1923
1924         if (!usePal(format))
1925             src[1]= NULL;
1926     }
1927 }
1928
1929 static int check_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt,
1930                                 const int linesizes[4])
1931 {
1932     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
1933     int i;
1934
1935     for (i = 0; i < 4; i++) {
1936         int plane = desc->comp[i].plane;
1937         if (!data[plane] || !linesizes[plane])
1938             return 0;
1939     }
1940
1941     return 1;
1942 }
1943
1944 /**
1945  * swscale wrapper, so we don't need to export the SwsContext.
1946  * Assumes planar YUV to be in YUV order instead of YVU.
1947  */
1948 int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
1949               int srcSliceH, uint8_t* const dst[], const int dstStride[])
1950 {
1951     int i;
1952     const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
1953     uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
1954
1955     // do not mess up sliceDir if we have a "trailing" 0-size slice
1956     if (srcSliceH == 0)
1957         return 0;
1958
1959     if (!check_image_pointers(src, c->srcFormat, srcStride)) {
1960         av_log(c, AV_LOG_ERROR, "bad src image pointers\n");
1961         return 0;
1962     }
1963     if (!check_image_pointers(dst, c->dstFormat, dstStride)) {
1964         av_log(c, AV_LOG_ERROR, "bad dst image pointers\n");
1965         return 0;
1966     }
1967
1968     if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
1969         av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
1970         return 0;
1971     }
1972     if (c->sliceDir == 0) {
1973         if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
1974     }
1975
1976     if (usePal(c->srcFormat)) {
1977         for (i=0; i<256; i++) {
1978             int p, r, g, b,y,u,v;
1979             if(c->srcFormat == PIX_FMT_PAL8) {
1980                 p=((const uint32_t*)(src[1]))[i];
1981                 r= (p>>16)&0xFF;
1982                 g= (p>> 8)&0xFF;
1983                 b=  p     &0xFF;
1984             } else if(c->srcFormat == PIX_FMT_RGB8) {
1985                 r= (i>>5    )*36;
1986                 g= ((i>>2)&7)*36;
1987                 b= (i&3     )*85;
1988             } else if(c->srcFormat == PIX_FMT_BGR8) {
1989                 b= (i>>6    )*85;
1990                 g= ((i>>3)&7)*36;
1991                 r= (i&7     )*36;
1992             } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
1993                 r= (i>>3    )*255;
1994                 g= ((i>>1)&3)*85;
1995                 b= (i&1     )*255;
1996             } else if(c->srcFormat == PIX_FMT_GRAY8 || c->srcFormat == PIX_FMT_Y400A) {
1997                 r = g = b = i;
1998             } else {
1999                 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2000                 b= (i>>3    )*255;
2001                 g= ((i>>1)&3)*85;
2002                 r= (i&1     )*255;
2003             }
2004             y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2005             u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2006             v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2007             c->pal_yuv[i]= y + (u<<8) + (v<<16);
2008
2009             switch(c->dstFormat) {
2010             case PIX_FMT_BGR32:
2011 #if !HAVE_BIGENDIAN
2012             case PIX_FMT_RGB24:
2013 #endif
2014                 c->pal_rgb[i]=  r + (g<<8) + (b<<16);
2015                 break;
2016             case PIX_FMT_BGR32_1:
2017 #if HAVE_BIGENDIAN
2018             case PIX_FMT_BGR24:
2019 #endif
2020                 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2021                 break;
2022             case PIX_FMT_RGB32_1:
2023 #if HAVE_BIGENDIAN
2024             case PIX_FMT_RGB24:
2025 #endif
2026                 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2027                 break;
2028             case PIX_FMT_RGB32:
2029 #if !HAVE_BIGENDIAN
2030             case PIX_FMT_BGR24:
2031 #endif
2032             default:
2033                 c->pal_rgb[i]=  b + (g<<8) + (r<<16);
2034             }
2035         }
2036     }
2037
2038     // copy strides, so they can safely be modified
2039     if (c->sliceDir == 1) {
2040         // slices go from top to bottom
2041         int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
2042         int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
2043
2044         reset_ptr(src2, c->srcFormat);
2045         reset_ptr((const uint8_t**)dst2, c->dstFormat);
2046
2047         /* reset slice direction at end of frame */
2048         if (srcSliceY + srcSliceH == c->srcH)
2049             c->sliceDir = 0;
2050
2051         return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
2052     } else {
2053         // slices go from bottom to top => we flip the image internally
2054         int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
2055         int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
2056
2057         src2[0] += (srcSliceH-1)*srcStride[0];
2058         if (!usePal(c->srcFormat))
2059             src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2060         src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2061         src2[3] += (srcSliceH-1)*srcStride[3];
2062         dst2[0] += ( c->dstH                      -1)*dstStride[0];
2063         dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
2064         dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
2065         dst2[3] += ( c->dstH                      -1)*dstStride[3];
2066
2067         reset_ptr(src2, c->srcFormat);
2068         reset_ptr((const uint8_t**)dst2, c->dstFormat);
2069
2070         /* reset slice direction at end of frame */
2071         if (!srcSliceY)
2072             c->sliceDir = 0;
2073
2074         return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2075     }
2076 }
2077
2078 /* Convert the palette to the same packed 32-bit format as the palette */
2079 void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2080 {
2081     long i;
2082
2083     for (i=0; i<num_pixels; i++)
2084         ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
2085 }
2086
2087 /* Palette format: ABCD -> dst format: ABC */
2088 void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst, long num_pixels, const uint8_t *palette)
2089 {
2090     long i;
2091
2092     for (i=0; i<num_pixels; i++) {
2093         //FIXME slow?
2094         dst[0]= palette[src[i]*4+0];
2095         dst[1]= palette[src[i]*4+1];
2096         dst[2]= palette[src[i]*4+2];
2097         dst+= 3;
2098     }
2099 }