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