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