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