19d93ddacfd5eecfa72ecf1e5e246f36bd758ab8
[libav.git] / 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 modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * 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 * the C code (not assembly, mmx, ...) of this file can be used
21 * under the LGPL license too
22 */
23
24 /*
25 supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
26 supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
27 {BGR,RGB}{1,4,8,15,16} support dithering
28
29 unscaled special converters (YV12=I420=IYUV, Y800=Y8)
30 YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
31 x -> x
32 YUV9 -> YV12
33 YUV9/YV12 -> Y800
34 Y800 -> YUV9/YV12
35 BGR24 -> BGR32 & RGB24 -> RGB32
36 BGR32 -> BGR24 & RGB32 -> RGB24
37 BGR15 -> BGR16
38 */
39
40 /*
41 tested special converters (most are tested actually, but I did not write it down ...)
42 YV12 -> BGR16
43 YV12 -> YV12
44 BGR15 -> BGR16
45 BGR16 -> BGR16
46 YVU9 -> YV12
47
48 untested special converters
49 YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
50 YV12/I420 -> YV12/I420
51 YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
52 BGR24 -> BGR32 & RGB24 -> RGB32
53 BGR32 -> BGR24 & RGB32 -> RGB24
54 BGR24 -> YV12
55 */
56
57 #define _SVID_SOURCE //needed for MAP_ANONYMOUS
58 #include <inttypes.h>
59 #include <string.h>
60 #include <math.h>
61 #include <stdio.h>
62 #include <unistd.h>
63 #include "config.h"
64 #include <assert.h>
65 #if HAVE_SYS_MMAN_H
66 #include <sys/mman.h>
67 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
68 #define MAP_ANONYMOUS MAP_ANON
69 #endif
70 #endif
71 #if HAVE_VIRTUALALLOC
72 #define WIN32_LEAN_AND_MEAN
73 #include <windows.h>
74 #endif
75 #include "swscale.h"
76 #include "swscale_internal.h"
77 #include "rgb2rgb.h"
78 #include "libavutil/x86_cpu.h"
79 #include "libavutil/bswap.h"
80
81 unsigned swscale_version(void)
82 {
83 return LIBSWSCALE_VERSION_INT;
84 }
85
86 #undef MOVNTQ
87 #undef PAVGB
88
89 //#undef HAVE_MMX2
90 //#define HAVE_AMD3DNOW
91 //#undef HAVE_MMX
92 //#undef ARCH_X86
93 //#define WORDS_BIGENDIAN
94 #define DITHER1XBPP
95
96 #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
97
98 #define RET 0xC3 //near return opcode for x86
99
100 #ifdef M_PI
101 #define PI M_PI
102 #else
103 #define PI 3.14159265358979323846
104 #endif
105
106 #define isSupportedIn(x) ( \
107 (x)==PIX_FMT_YUV420P \
108 || (x)==PIX_FMT_YUVA420P \
109 || (x)==PIX_FMT_YUYV422 \
110 || (x)==PIX_FMT_UYVY422 \
111 || (x)==PIX_FMT_RGB32 \
112 || (x)==PIX_FMT_RGB32_1 \
113 || (x)==PIX_FMT_BGR24 \
114 || (x)==PIX_FMT_BGR565 \
115 || (x)==PIX_FMT_BGR555 \
116 || (x)==PIX_FMT_BGR32 \
117 || (x)==PIX_FMT_BGR32_1 \
118 || (x)==PIX_FMT_RGB24 \
119 || (x)==PIX_FMT_RGB565 \
120 || (x)==PIX_FMT_RGB555 \
121 || (x)==PIX_FMT_GRAY8 \
122 || (x)==PIX_FMT_YUV410P \
123 || (x)==PIX_FMT_YUV440P \
124 || (x)==PIX_FMT_GRAY16BE \
125 || (x)==PIX_FMT_GRAY16LE \
126 || (x)==PIX_FMT_YUV444P \
127 || (x)==PIX_FMT_YUV422P \
128 || (x)==PIX_FMT_YUV411P \
129 || (x)==PIX_FMT_PAL8 \
130 || (x)==PIX_FMT_BGR8 \
131 || (x)==PIX_FMT_RGB8 \
132 || (x)==PIX_FMT_BGR4_BYTE \
133 || (x)==PIX_FMT_RGB4_BYTE \
134 || (x)==PIX_FMT_YUV440P \
135 || (x)==PIX_FMT_MONOWHITE \
136 || (x)==PIX_FMT_MONOBLACK \
137 || (x)==PIX_FMT_YUV420PLE \
138 || (x)==PIX_FMT_YUV422PLE \
139 || (x)==PIX_FMT_YUV444PLE \
140 || (x)==PIX_FMT_YUV420PBE \
141 || (x)==PIX_FMT_YUV422PBE \
142 || (x)==PIX_FMT_YUV444PBE \
143 )
144 #define isSupportedOut(x) ( \
145 (x)==PIX_FMT_YUV420P \
146 || (x)==PIX_FMT_YUVA420P \
147 || (x)==PIX_FMT_YUYV422 \
148 || (x)==PIX_FMT_UYVY422 \
149 || (x)==PIX_FMT_YUV444P \
150 || (x)==PIX_FMT_YUV422P \
151 || (x)==PIX_FMT_YUV411P \
152 || isRGB(x) \
153 || isBGR(x) \
154 || (x)==PIX_FMT_NV12 \
155 || (x)==PIX_FMT_NV21 \
156 || (x)==PIX_FMT_GRAY16BE \
157 || (x)==PIX_FMT_GRAY16LE \
158 || (x)==PIX_FMT_GRAY8 \
159 || (x)==PIX_FMT_YUV410P \
160 || (x)==PIX_FMT_YUV440P \
161 || (x)==PIX_FMT_YUV420PLE \
162 || (x)==PIX_FMT_YUV422PLE \
163 || (x)==PIX_FMT_YUV444PLE \
164 || (x)==PIX_FMT_YUV420PBE \
165 || (x)==PIX_FMT_YUV422PBE \
166 || (x)==PIX_FMT_YUV444PBE \
167 )
168 #define isPacked(x) ( \
169 (x)==PIX_FMT_PAL8 \
170 || (x)==PIX_FMT_YUYV422 \
171 || (x)==PIX_FMT_UYVY422 \
172 || isRGB(x) \
173 || isBGR(x) \
174 )
175 #define usePal(x) ( \
176 (x)==PIX_FMT_PAL8 \
177 || (x)==PIX_FMT_BGR4_BYTE \
178 || (x)==PIX_FMT_RGB4_BYTE \
179 || (x)==PIX_FMT_BGR8 \
180 || (x)==PIX_FMT_RGB8 \
181 )
182
183 #define RGB2YUV_SHIFT 15
184 #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
185 #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
186 #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
187 #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
188 #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
189 #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
190 #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
191 #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
192 #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
193
194 extern const int32_t ff_yuv2rgb_coeffs[8][4];
195
196 static const double rgb2yuv_table[8][9]={
197 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
198 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
199 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
200 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
201 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
202 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
203 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
204 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
205 };
206
207 /*
208 NOTES
209 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
210
211 TODO
212 more intelligent misalignment avoidance for the horizontal scaler
213 write special vertical cubic upscale version
214 optimize C code (YV12 / minmax)
215 add support for packed pixel YUV input & output
216 add support for Y8 output
217 optimize BGR24 & BGR32
218 add BGR4 output support
219 write special BGR->BGR scaler
220 */
221
222 #if ARCH_X86 && CONFIG_GPL
223 DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
224 DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
225 DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
226 DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
227 DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
228 DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
229 DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
230 DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
231
232 const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
233 0x0103010301030103LL,
234 0x0200020002000200LL,};
235
236 const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
237 0x0602060206020602LL,
238 0x0004000400040004LL,};
239
240 DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
241 DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
242 DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
243 DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
244 DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
245 DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
246
247 DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
248 DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
249 DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
250
251 #ifdef FAST_BGR2YV12
252 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
253 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
254 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
255 #else
256 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
257 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
258 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
259 #endif /* FAST_BGR2YV12 */
260 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
261 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
262 DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
263
264 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
265 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
266 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
267 DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
268 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
269
270 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV[2][4]) = {
271 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
272 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
273 };
274
275 DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
276
277 #endif /* ARCH_X86 && CONFIG_GPL */
278
279 // clipping helper table for C implementations:
280 static unsigned char clip_table[768];
281
282 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
283
284 static const uint8_t __attribute__((aligned(8))) dither_2x2_4[2][8]={
285 { 1, 3, 1, 3, 1, 3, 1, 3, },
286 { 2, 0, 2, 0, 2, 0, 2, 0, },
287 };
288
289 static const uint8_t __attribute__((aligned(8))) dither_2x2_8[2][8]={
290 { 6, 2, 6, 2, 6, 2, 6, 2, },
291 { 0, 4, 0, 4, 0, 4, 0, 4, },
292 };
293
294 const uint8_t __attribute__((aligned(8))) dither_8x8_32[8][8]={
295 { 17, 9, 23, 15, 16, 8, 22, 14, },
296 { 5, 29, 3, 27, 4, 28, 2, 26, },
297 { 21, 13, 19, 11, 20, 12, 18, 10, },
298 { 0, 24, 6, 30, 1, 25, 7, 31, },
299 { 16, 8, 22, 14, 17, 9, 23, 15, },
300 { 4, 28, 2, 26, 5, 29, 3, 27, },
301 { 20, 12, 18, 10, 21, 13, 19, 11, },
302 { 1, 25, 7, 31, 0, 24, 6, 30, },
303 };
304
305 #if 0
306 const uint8_t __attribute__((aligned(8))) dither_8x8_64[8][8]={
307 { 0, 48, 12, 60, 3, 51, 15, 63, },
308 { 32, 16, 44, 28, 35, 19, 47, 31, },
309 { 8, 56, 4, 52, 11, 59, 7, 55, },
310 { 40, 24, 36, 20, 43, 27, 39, 23, },
311 { 2, 50, 14, 62, 1, 49, 13, 61, },
312 { 34, 18, 46, 30, 33, 17, 45, 29, },
313 { 10, 58, 6, 54, 9, 57, 5, 53, },
314 { 42, 26, 38, 22, 41, 25, 37, 21, },
315 };
316 #endif
317
318 const uint8_t __attribute__((aligned(8))) dither_8x8_73[8][8]={
319 { 0, 55, 14, 68, 3, 58, 17, 72, },
320 { 37, 18, 50, 32, 40, 22, 54, 35, },
321 { 9, 64, 5, 59, 13, 67, 8, 63, },
322 { 46, 27, 41, 23, 49, 31, 44, 26, },
323 { 2, 57, 16, 71, 1, 56, 15, 70, },
324 { 39, 21, 52, 34, 38, 19, 51, 33, },
325 { 11, 66, 7, 62, 10, 65, 6, 60, },
326 { 48, 30, 43, 25, 47, 29, 42, 24, },
327 };
328
329 #if 0
330 const uint8_t __attribute__((aligned(8))) dither_8x8_128[8][8]={
331 { 68, 36, 92, 60, 66, 34, 90, 58, },
332 { 20, 116, 12, 108, 18, 114, 10, 106, },
333 { 84, 52, 76, 44, 82, 50, 74, 42, },
334 { 0, 96, 24, 120, 6, 102, 30, 126, },
335 { 64, 32, 88, 56, 70, 38, 94, 62, },
336 { 16, 112, 8, 104, 22, 118, 14, 110, },
337 { 80, 48, 72, 40, 86, 54, 78, 46, },
338 { 4, 100, 28, 124, 2, 98, 26, 122, },
339 };
340 #endif
341
342 #if 1
343 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
344 {117, 62, 158, 103, 113, 58, 155, 100, },
345 { 34, 199, 21, 186, 31, 196, 17, 182, },
346 {144, 89, 131, 76, 141, 86, 127, 72, },
347 { 0, 165, 41, 206, 10, 175, 52, 217, },
348 {110, 55, 151, 96, 120, 65, 162, 107, },
349 { 28, 193, 14, 179, 38, 203, 24, 189, },
350 {138, 83, 124, 69, 148, 93, 134, 79, },
351 { 7, 172, 48, 213, 3, 168, 45, 210, },
352 };
353 #elif 1
354 // tries to correct a gamma of 1.5
355 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
356 { 0, 143, 18, 200, 2, 156, 25, 215, },
357 { 78, 28, 125, 64, 89, 36, 138, 74, },
358 { 10, 180, 3, 161, 16, 195, 8, 175, },
359 {109, 51, 93, 38, 121, 60, 105, 47, },
360 { 1, 152, 23, 210, 0, 147, 20, 205, },
361 { 85, 33, 134, 71, 81, 30, 130, 67, },
362 { 14, 190, 6, 171, 12, 185, 5, 166, },
363 {117, 57, 101, 44, 113, 54, 97, 41, },
364 };
365 #elif 1
366 // tries to correct a gamma of 2.0
367 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
368 { 0, 124, 8, 193, 0, 140, 12, 213, },
369 { 55, 14, 104, 42, 66, 19, 119, 52, },
370 { 3, 168, 1, 145, 6, 187, 3, 162, },
371 { 86, 31, 70, 21, 99, 39, 82, 28, },
372 { 0, 134, 11, 206, 0, 129, 9, 200, },
373 { 62, 17, 114, 48, 58, 16, 109, 45, },
374 { 5, 181, 2, 157, 4, 175, 1, 151, },
375 { 95, 36, 78, 26, 90, 34, 74, 24, },
376 };
377 #else
378 // tries to correct a gamma of 2.5
379 const uint8_t __attribute__((aligned(8))) dither_8x8_220[8][8]={
380 { 0, 107, 3, 187, 0, 125, 6, 212, },
381 { 39, 7, 86, 28, 49, 11, 102, 36, },
382 { 1, 158, 0, 131, 3, 180, 1, 151, },
383 { 68, 19, 52, 12, 81, 25, 64, 17, },
384 { 0, 119, 5, 203, 0, 113, 4, 195, },
385 { 45, 9, 96, 33, 42, 8, 91, 30, },
386 { 2, 172, 1, 144, 2, 165, 0, 137, },
387 { 77, 23, 60, 15, 72, 21, 56, 14, },
388 };
389 #endif
390
391 const char *sws_format_name(enum PixelFormat format)
392 {
393 switch (format) {
394 case PIX_FMT_YUV420P:
395 return "yuv420p";
396 case PIX_FMT_YUVA420P:
397 return "yuva420p";
398 case PIX_FMT_YUYV422:
399 return "yuyv422";
400 case PIX_FMT_RGB24:
401 return "rgb24";
402 case PIX_FMT_BGR24:
403 return "bgr24";
404 case PIX_FMT_YUV422P:
405 return "yuv422p";
406 case PIX_FMT_YUV444P:
407 return "yuv444p";
408 case PIX_FMT_RGB32:
409 return "rgb32";
410 case PIX_FMT_YUV410P:
411 return "yuv410p";
412 case PIX_FMT_YUV411P:
413 return "yuv411p";
414 case PIX_FMT_RGB565:
415 return "rgb565";
416 case PIX_FMT_RGB555:
417 return "rgb555";
418 case PIX_FMT_GRAY16BE:
419 return "gray16be";
420 case PIX_FMT_GRAY16LE:
421 return "gray16le";
422 case PIX_FMT_GRAY8:
423 return "gray8";
424 case PIX_FMT_MONOWHITE:
425 return "mono white";
426 case PIX_FMT_MONOBLACK:
427 return "mono black";
428 case PIX_FMT_PAL8:
429 return "Palette";
430 case PIX_FMT_YUVJ420P:
431 return "yuvj420p";
432 case PIX_FMT_YUVJ422P:
433 return "yuvj422p";
434 case PIX_FMT_YUVJ444P:
435 return "yuvj444p";
436 case PIX_FMT_XVMC_MPEG2_MC:
437 return "xvmc_mpeg2_mc";
438 case PIX_FMT_XVMC_MPEG2_IDCT:
439 return "xvmc_mpeg2_idct";
440 case PIX_FMT_UYVY422:
441 return "uyvy422";
442 case PIX_FMT_UYYVYY411:
443 return "uyyvyy411";
444 case PIX_FMT_RGB32_1:
445 return "rgb32x";
446 case PIX_FMT_BGR32_1:
447 return "bgr32x";
448 case PIX_FMT_BGR32:
449 return "bgr32";
450 case PIX_FMT_BGR565:
451 return "bgr565";
452 case PIX_FMT_BGR555:
453 return "bgr555";
454 case PIX_FMT_BGR8:
455 return "bgr8";
456 case PIX_FMT_BGR4:
457 return "bgr4";
458 case PIX_FMT_BGR4_BYTE:
459 return "bgr4 byte";
460 case PIX_FMT_RGB8:
461 return "rgb8";
462 case PIX_FMT_RGB4:
463 return "rgb4";
464 case PIX_FMT_RGB4_BYTE:
465 return "rgb4 byte";
466 case PIX_FMT_NV12:
467 return "nv12";
468 case PIX_FMT_NV21:
469 return "nv21";
470 case PIX_FMT_YUV440P:
471 return "yuv440p";
472 case PIX_FMT_VDPAU_H264:
473 return "vdpau_h264";
474 case PIX_FMT_VDPAU_MPEG1:
475 return "vdpau_mpeg1";
476 case PIX_FMT_VDPAU_MPEG2:
477 return "vdpau_mpeg2";
478 case PIX_FMT_VDPAU_WMV3:
479 return "vdpau_wmv3";
480 case PIX_FMT_VDPAU_VC1:
481 return "vdpau_vc1";
482 case PIX_FMT_YUV420PLE:
483 return "yuv420ple";
484 case PIX_FMT_YUV422PLE:
485 return "yuv422ple";
486 case PIX_FMT_YUV444PLE:
487 return "yuv444ple";
488 case PIX_FMT_YUV420PBE:
489 return "yuv420pbe";
490 case PIX_FMT_YUV422PBE:
491 return "yuv422pbe";
492 case PIX_FMT_YUV444PBE:
493 return "yuv444pbe";
494 default:
495 return "Unknown format";
496 }
497 }
498
499 static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
500 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
501 const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
502 {
503 //FIXME Optimize (just quickly written not optimized..)
504 int i;
505 for (i=0; i<dstW; i++)
506 {
507 int val=1<<18;
508 int j;
509 for (j=0; j<lumFilterSize; j++)
510 val += lumSrc[j][i] * lumFilter[j];
511
512 dest[i]= av_clip_uint8(val>>19);
513 }
514
515 if (uDest)
516 for (i=0; i<chrDstW; i++)
517 {
518 int u=1<<18;
519 int v=1<<18;
520 int j;
521 for (j=0; j<chrFilterSize; j++)
522 {
523 u += chrSrc[j][i] * chrFilter[j];
524 v += chrSrc[j][i + VOFW] * chrFilter[j];
525 }
526
527 uDest[i]= av_clip_uint8(u>>19);
528 vDest[i]= av_clip_uint8(v>>19);
529 }
530
531 if (CONFIG_SWSCALE_ALPHA && aDest)
532 for (i=0; i<dstW; i++){
533 int val=1<<18;
534 int j;
535 for (j=0; j<lumFilterSize; j++)
536 val += alpSrc[j][i] * lumFilter[j];
537
538 aDest[i]= av_clip_uint8(val>>19);
539 }
540
541 }
542
543 static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
544 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
545 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
546 {
547 //FIXME Optimize (just quickly written not optimized..)
548 int i;
549 for (i=0; i<dstW; i++)
550 {
551 int val=1<<18;
552 int j;
553 for (j=0; j<lumFilterSize; j++)
554 val += lumSrc[j][i] * lumFilter[j];
555
556 dest[i]= av_clip_uint8(val>>19);
557 }
558
559 if (!uDest)
560 return;
561
562 if (dstFormat == PIX_FMT_NV12)
563 for (i=0; i<chrDstW; i++)
564 {
565 int u=1<<18;
566 int v=1<<18;
567 int j;
568 for (j=0; j<chrFilterSize; j++)
569 {
570 u += chrSrc[j][i] * chrFilter[j];
571 v += chrSrc[j][i + VOFW] * chrFilter[j];
572 }
573
574 uDest[2*i]= av_clip_uint8(u>>19);
575 uDest[2*i+1]= av_clip_uint8(v>>19);
576 }
577 else
578 for (i=0; i<chrDstW; i++)
579 {
580 int u=1<<18;
581 int v=1<<18;
582 int j;
583 for (j=0; j<chrFilterSize; j++)
584 {
585 u += chrSrc[j][i] * chrFilter[j];
586 v += chrSrc[j][i + VOFW] * chrFilter[j];
587 }
588
589 uDest[2*i]= av_clip_uint8(v>>19);
590 uDest[2*i+1]= av_clip_uint8(u>>19);
591 }
592 }
593
594 #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
595 for (i=0; i<(dstW>>1); i++){\
596 int j;\
597 int Y1 = 1<<18;\
598 int Y2 = 1<<18;\
599 int U = 1<<18;\
600 int V = 1<<18;\
601 int av_unused A1, A2;\
602 type av_unused *r, *b, *g;\
603 const int i2= 2*i;\
604 \
605 for (j=0; j<lumFilterSize; j++)\
606 {\
607 Y1 += lumSrc[j][i2] * lumFilter[j];\
608 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
609 }\
610 for (j=0; j<chrFilterSize; j++)\
611 {\
612 U += chrSrc[j][i] * chrFilter[j];\
613 V += chrSrc[j][i+VOFW] * chrFilter[j];\
614 }\
615 Y1>>=19;\
616 Y2>>=19;\
617 U >>=19;\
618 V >>=19;\
619 if (alpha){\
620 A1 = 1<<18;\
621 A2 = 1<<18;\
622 for (j=0; j<lumFilterSize; j++){\
623 A1 += alpSrc[j][i2 ] * lumFilter[j];\
624 A2 += alpSrc[j][i2+1] * lumFilter[j];\
625 }\
626 A1>>=19;\
627 A2>>=19;\
628 }\
629
630 #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
631 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
632 if ((Y1|Y2|U|V)&256)\
633 {\
634 if (Y1>255) Y1=255; \
635 else if (Y1<0)Y1=0; \
636 if (Y2>255) Y2=255; \
637 else if (Y2<0)Y2=0; \
638 if (U>255) U=255; \
639 else if (U<0) U=0; \
640 if (V>255) V=255; \
641 else if (V<0) V=0; \
642 }\
643 if (alpha && ((A1|A2)&256)){\
644 A1=av_clip_uint8(A1);\
645 A2=av_clip_uint8(A2);\
646 }
647
648 #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
649 for (i=0; i<dstW; i++){\
650 int j;\
651 int Y = 0;\
652 int U = -128<<19;\
653 int V = -128<<19;\
654 int av_unused A;\
655 int R,G,B;\
656 \
657 for (j=0; j<lumFilterSize; j++){\
658 Y += lumSrc[j][i ] * lumFilter[j];\
659 }\
660 for (j=0; j<chrFilterSize; j++){\
661 U += chrSrc[j][i ] * chrFilter[j];\
662 V += chrSrc[j][i+VOFW] * chrFilter[j];\
663 }\
664 Y >>=10;\
665 U >>=10;\
666 V >>=10;\
667 if (alpha){\
668 A = rnd;\
669 for (j=0; j<lumFilterSize; j++)\
670 A += alpSrc[j][i ] * lumFilter[j];\
671 A >>=19;\
672 if (A&256)\
673 A = av_clip_uint8(A);\
674 }\
675
676 #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
677 YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
678 Y-= c->yuv2rgb_y_offset;\
679 Y*= c->yuv2rgb_y_coeff;\
680 Y+= rnd;\
681 R= Y + V*c->yuv2rgb_v2r_coeff;\
682 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
683 B= Y + U*c->yuv2rgb_u2b_coeff;\
684 if ((R|G|B)&(0xC0000000)){\
685 if (R>=(256<<22)) R=(256<<22)-1; \
686 else if (R<0)R=0; \
687 if (G>=(256<<22)) G=(256<<22)-1; \
688 else if (G<0)G=0; \
689 if (B>=(256<<22)) B=(256<<22)-1; \
690 else if (B<0)B=0; \
691 }\
692
693
694 #define YSCALE_YUV_2_GRAY16_C \
695 for (i=0; i<(dstW>>1); i++){\
696 int j;\
697 int Y1 = 1<<18;\
698 int Y2 = 1<<18;\
699 int U = 1<<18;\
700 int V = 1<<18;\
701 \
702 const int i2= 2*i;\
703 \
704 for (j=0; j<lumFilterSize; j++)\
705 {\
706 Y1 += lumSrc[j][i2] * lumFilter[j];\
707 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
708 }\
709 Y1>>=11;\
710 Y2>>=11;\
711 if ((Y1|Y2|U|V)&65536)\
712 {\
713 if (Y1>65535) Y1=65535; \
714 else if (Y1<0)Y1=0; \
715 if (Y2>65535) Y2=65535; \
716 else if (Y2<0)Y2=0; \
717 }
718
719 #define YSCALE_YUV_2_RGBX_C(type,alpha) \
720 YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
721 r = (type *)c->table_rV[V]; \
722 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
723 b = (type *)c->table_bU[U]; \
724
725 #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
726 for (i=0; i<(dstW>>1); i++){ \
727 const int i2= 2*i; \
728 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
729 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
730 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
731 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
732 type av_unused *r, *b, *g; \
733 int av_unused A1, A2; \
734 if (alpha){\
735 A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
736 A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
737 }\
738
739 #define YSCALE_YUV_2_GRAY16_2_C \
740 for (i=0; i<(dstW>>1); i++){ \
741 const int i2= 2*i; \
742 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
743 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
744
745 #define YSCALE_YUV_2_RGB2_C(type,alpha) \
746 YSCALE_YUV_2_PACKED2_C(type,alpha)\
747 r = (type *)c->table_rV[V];\
748 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
749 b = (type *)c->table_bU[U];\
750
751 #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
752 for (i=0; i<(dstW>>1); i++){\
753 const int i2= 2*i;\
754 int Y1= buf0[i2 ]>>7;\
755 int Y2= buf0[i2+1]>>7;\
756 int U= (uvbuf1[i ])>>7;\
757 int V= (uvbuf1[i+VOFW])>>7;\
758 type av_unused *r, *b, *g;\
759 int av_unused A1, A2;\
760 if (alpha){\
761 A1= abuf0[i2 ]>>7;\
762 A2= abuf0[i2+1]>>7;\
763 }\
764
765 #define YSCALE_YUV_2_GRAY16_1_C \
766 for (i=0; i<(dstW>>1); i++){\
767 const int i2= 2*i;\
768 int Y1= buf0[i2 ]<<1;\
769 int Y2= buf0[i2+1]<<1;\
770
771 #define YSCALE_YUV_2_RGB1_C(type,alpha) \
772 YSCALE_YUV_2_PACKED1_C(type,alpha)\
773 r = (type *)c->table_rV[V];\
774 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
775 b = (type *)c->table_bU[U];\
776
777 #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
778 for (i=0; i<(dstW>>1); i++){\
779 const int i2= 2*i;\
780 int Y1= buf0[i2 ]>>7;\
781 int Y2= buf0[i2+1]>>7;\
782 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
783 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
784 type av_unused *r, *b, *g;\
785 int av_unused A1, A2;\
786 if (alpha){\
787 A1= abuf0[i2 ]>>7;\
788 A2= abuf0[i2+1]>>7;\
789 }\
790
791 #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
792 YSCALE_YUV_2_PACKED1B_C(type,alpha)\
793 r = (type *)c->table_rV[V];\
794 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
795 b = (type *)c->table_bU[U];\
796
797 #define YSCALE_YUV_2_MONO2_C \
798 const uint8_t * const d128=dither_8x8_220[y&7];\
799 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
800 for (i=0; i<dstW-7; i+=8){\
801 int acc;\
802 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
803 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
804 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
805 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
806 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
807 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
808 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
809 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
810 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
811 dest++;\
812 }\
813
814
815 #define YSCALE_YUV_2_MONOX_C \
816 const uint8_t * const d128=dither_8x8_220[y&7];\
817 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
818 int acc=0;\
819 for (i=0; i<dstW-1; i+=2){\
820 int j;\
821 int Y1=1<<18;\
822 int Y2=1<<18;\
823 \
824 for (j=0; j<lumFilterSize; j++)\
825 {\
826 Y1 += lumSrc[j][i] * lumFilter[j];\
827 Y2 += lumSrc[j][i+1] * lumFilter[j];\
828 }\
829 Y1>>=19;\
830 Y2>>=19;\
831 if ((Y1|Y2)&256)\
832 {\
833 if (Y1>255) Y1=255;\
834 else if (Y1<0)Y1=0;\
835 if (Y2>255) Y2=255;\
836 else if (Y2<0)Y2=0;\
837 }\
838 acc+= acc + g[Y1+d128[(i+0)&7]];\
839 acc+= acc + g[Y2+d128[(i+1)&7]];\
840 if ((i&7)==6){\
841 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
842 dest++;\
843 }\
844 }
845
846
847 #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
848 switch(c->dstFormat)\
849 {\
850 case PIX_FMT_RGBA:\
851 case PIX_FMT_BGRA:\
852 if (CONFIG_SMALL){\
853 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
854 func(uint32_t,needAlpha)\
855 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
856 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
857 }\
858 }else{\
859 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
860 func(uint32_t,1)\
861 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
862 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
863 }\
864 }else{\
865 func(uint32_t,0)\
866 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
867 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
868 }\
869 }\
870 }\
871 break;\
872 case PIX_FMT_ARGB:\
873 case PIX_FMT_ABGR:\
874 if (CONFIG_SMALL){\
875 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
876 func(uint32_t,needAlpha)\
877 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
878 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
879 }\
880 }else{\
881 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){\
882 func(uint32_t,1)\
883 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
884 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
885 }\
886 }else{\
887 func(uint32_t,0)\
888 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
889 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
890 }\
891 }\
892 } \
893 break;\
894 case PIX_FMT_RGB24:\
895 func(uint8_t,0)\
896 ((uint8_t*)dest)[0]= r[Y1];\
897 ((uint8_t*)dest)[1]= g[Y1];\
898 ((uint8_t*)dest)[2]= b[Y1];\
899 ((uint8_t*)dest)[3]= r[Y2];\
900 ((uint8_t*)dest)[4]= g[Y2];\
901 ((uint8_t*)dest)[5]= b[Y2];\
902 dest+=6;\
903 }\
904 break;\
905 case PIX_FMT_BGR24:\
906 func(uint8_t,0)\
907 ((uint8_t*)dest)[0]= b[Y1];\
908 ((uint8_t*)dest)[1]= g[Y1];\
909 ((uint8_t*)dest)[2]= r[Y1];\
910 ((uint8_t*)dest)[3]= b[Y2];\
911 ((uint8_t*)dest)[4]= g[Y2];\
912 ((uint8_t*)dest)[5]= r[Y2];\
913 dest+=6;\
914 }\
915 break;\
916 case PIX_FMT_RGB565:\
917 case PIX_FMT_BGR565:\
918 {\
919 const int dr1= dither_2x2_8[y&1 ][0];\
920 const int dg1= dither_2x2_4[y&1 ][0];\
921 const int db1= dither_2x2_8[(y&1)^1][0];\
922 const int dr2= dither_2x2_8[y&1 ][1];\
923 const int dg2= dither_2x2_4[y&1 ][1];\
924 const int db2= dither_2x2_8[(y&1)^1][1];\
925 func(uint16_t,0)\
926 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
927 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
928 }\
929 }\
930 break;\
931 case PIX_FMT_RGB555:\
932 case PIX_FMT_BGR555:\
933 {\
934 const int dr1= dither_2x2_8[y&1 ][0];\
935 const int dg1= dither_2x2_8[y&1 ][1];\
936 const int db1= dither_2x2_8[(y&1)^1][0];\
937 const int dr2= dither_2x2_8[y&1 ][1];\
938 const int dg2= dither_2x2_8[y&1 ][0];\
939 const int db2= dither_2x2_8[(y&1)^1][1];\
940 func(uint16_t,0)\
941 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
942 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
943 }\
944 }\
945 break;\
946 case PIX_FMT_RGB8:\
947 case PIX_FMT_BGR8:\
948 {\
949 const uint8_t * const d64= dither_8x8_73[y&7];\
950 const uint8_t * const d32= dither_8x8_32[y&7];\
951 func(uint8_t,0)\
952 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
953 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
954 }\
955 }\
956 break;\
957 case PIX_FMT_RGB4:\
958 case PIX_FMT_BGR4:\
959 {\
960 const uint8_t * const d64= dither_8x8_73 [y&7];\
961 const uint8_t * const d128=dither_8x8_220[y&7];\
962 func(uint8_t,0)\
963 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
964 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
965 }\
966 }\
967 break;\
968 case PIX_FMT_RGB4_BYTE:\
969 case PIX_FMT_BGR4_BYTE:\
970 {\
971 const uint8_t * const d64= dither_8x8_73 [y&7];\
972 const uint8_t * const d128=dither_8x8_220[y&7];\
973 func(uint8_t,0)\
974 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
975 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
976 }\
977 }\
978 break;\
979 case PIX_FMT_MONOBLACK:\
980 case PIX_FMT_MONOWHITE:\
981 {\
982 func_monoblack\
983 }\
984 break;\
985 case PIX_FMT_YUYV422:\
986 func2\
987 ((uint8_t*)dest)[2*i2+0]= Y1;\
988 ((uint8_t*)dest)[2*i2+1]= U;\
989 ((uint8_t*)dest)[2*i2+2]= Y2;\
990 ((uint8_t*)dest)[2*i2+3]= V;\
991 } \
992 break;\
993 case PIX_FMT_UYVY422:\
994 func2\
995 ((uint8_t*)dest)[2*i2+0]= U;\
996 ((uint8_t*)dest)[2*i2+1]= Y1;\
997 ((uint8_t*)dest)[2*i2+2]= V;\
998 ((uint8_t*)dest)[2*i2+3]= Y2;\
999 } \
1000 break;\
1001 case PIX_FMT_GRAY16BE:\
1002 func_g16\
1003 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
1004 ((uint8_t*)dest)[2*i2+1]= Y1;\
1005 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
1006 ((uint8_t*)dest)[2*i2+3]= Y2;\
1007 } \
1008 break;\
1009 case PIX_FMT_GRAY16LE:\
1010 func_g16\
1011 ((uint8_t*)dest)[2*i2+0]= Y1;\
1012 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
1013 ((uint8_t*)dest)[2*i2+2]= Y2;\
1014 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
1015 } \
1016 break;\
1017 }\
1018
1019
1020 static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1021 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1022 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1023 {
1024 int i;
1025 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)
1026 }
1027
1028 static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
1029 const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
1030 const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
1031 {
1032 int i;
1033 int step= fmt_depth(c->dstFormat)/8;
1034 int aidx= 3;
1035
1036 switch(c->dstFormat){
1037 case PIX_FMT_ARGB:
1038 dest++;
1039 aidx= 0;
1040 case PIX_FMT_RGB24:
1041 aidx--;
1042 case PIX_FMT_RGBA:
1043 if (CONFIG_SMALL){
1044 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1045 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1046 dest[aidx]= needAlpha ? A : 255;
1047 dest[0]= R>>22;
1048 dest[1]= G>>22;
1049 dest[2]= B>>22;
1050 dest+= step;
1051 }
1052 }else{
1053 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1054 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1055 dest[aidx]= A;
1056 dest[0]= R>>22;
1057 dest[1]= G>>22;
1058 dest[2]= B>>22;
1059 dest+= step;
1060 }
1061 }else{
1062 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1063 dest[aidx]= 255;
1064 dest[0]= R>>22;
1065 dest[1]= G>>22;
1066 dest[2]= B>>22;
1067 dest+= step;
1068 }
1069 }
1070 }
1071 break;
1072 case PIX_FMT_ABGR:
1073 dest++;
1074 aidx= 0;
1075 case PIX_FMT_BGR24:
1076 aidx--;
1077 case PIX_FMT_BGRA:
1078 if (CONFIG_SMALL){
1079 int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
1080 YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
1081 dest[aidx]= needAlpha ? A : 255;
1082 dest[0]= B>>22;
1083 dest[1]= G>>22;
1084 dest[2]= R>>22;
1085 dest+= step;
1086 }
1087 }else{
1088 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
1089 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
1090 dest[aidx]= A;
1091 dest[0]= B>>22;
1092 dest[1]= G>>22;
1093 dest[2]= R>>22;
1094 dest+= step;
1095 }
1096 }else{
1097 YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
1098 dest[aidx]= 255;
1099 dest[0]= B>>22;
1100 dest[1]= G>>22;
1101 dest[2]= R>>22;
1102 dest+= step;
1103 }
1104 }
1105 }
1106 break;
1107 default:
1108 assert(0);
1109 }
1110 }
1111
1112 static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val){
1113 int i;
1114 uint8_t *ptr = plane + stride*y;
1115 for (i=0; i<height; i++){
1116 memset(ptr, val, width);
1117 ptr += stride;
1118 }
1119 }
1120
1121 //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
1122 //Plain C versions
1123 #if !HAVE_MMX || CONFIG_RUNTIME_CPUDETECT || !CONFIG_GPL
1124 #define COMPILE_C
1125 #endif
1126
1127 #if ARCH_PPC
1128 #if (HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1129 #undef COMPILE_C
1130 #define COMPILE_ALTIVEC
1131 #endif
1132 #endif //ARCH_PPC
1133
1134 #if ARCH_X86
1135
1136 #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1137 #define COMPILE_MMX
1138 #endif
1139
1140 #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1141 #define COMPILE_MMX2
1142 #endif
1143
1144 #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
1145 #define COMPILE_3DNOW
1146 #endif
1147 #endif //ARCH_X86
1148
1149 #undef HAVE_MMX
1150 #undef HAVE_MMX2
1151 #undef HAVE_AMD3DNOW
1152 #undef HAVE_ALTIVEC
1153 #define HAVE_MMX 0
1154 #define HAVE_MMX2 0
1155 #define HAVE_AMD3DNOW 0
1156 #define HAVE_ALTIVEC 0
1157
1158 #ifdef COMPILE_C
1159 #define RENAME(a) a ## _C
1160 #include "swscale_template.c"
1161 #endif
1162
1163 #ifdef COMPILE_ALTIVEC
1164 #undef RENAME
1165 #undef HAVE_ALTIVEC
1166 #define HAVE_ALTIVEC 1
1167 #define RENAME(a) a ## _altivec
1168 #include "swscale_template.c"
1169 #endif
1170
1171 #if ARCH_X86
1172
1173 //MMX versions
1174 #ifdef COMPILE_MMX
1175 #undef RENAME
1176 #undef HAVE_MMX
1177 #undef HAVE_MMX2
1178 #undef HAVE_AMD3DNOW
1179 #define HAVE_MMX 1
1180 #define HAVE_MMX2 0
1181 #define HAVE_AMD3DNOW 0
1182 #define RENAME(a) a ## _MMX
1183 #include "swscale_template.c"
1184 #endif
1185
1186 //MMX2 versions
1187 #ifdef COMPILE_MMX2
1188 #undef RENAME
1189 #undef HAVE_MMX
1190 #undef HAVE_MMX2
1191 #undef HAVE_AMD3DNOW
1192 #define HAVE_MMX 1
1193 #define HAVE_MMX2 1
1194 #define HAVE_AMD3DNOW 0
1195 #define RENAME(a) a ## _MMX2
1196 #include "swscale_template.c"
1197 #endif
1198
1199 //3DNOW versions
1200 #ifdef COMPILE_3DNOW
1201 #undef RENAME
1202 #undef HAVE_MMX
1203 #undef HAVE_MMX2
1204 #undef HAVE_AMD3DNOW
1205 #define HAVE_MMX 1
1206 #define HAVE_MMX2 0
1207 #define HAVE_AMD3DNOW 1
1208 #define RENAME(a) a ## _3DNow
1209 #include "swscale_template.c"
1210 #endif
1211
1212 #endif //ARCH_X86
1213
1214 // minor note: the HAVE_xyz are messed up after this line so don't use them
1215
1216 static double getSplineCoeff(double a, double b, double c, double d, double dist)
1217 {
1218 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
1219 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1220 else return getSplineCoeff( 0.0,
1221 b+ 2.0*c + 3.0*d,
1222 c + 3.0*d,
1223 -b- 3.0*c - 6.0*d,
1224 dist-1.0);
1225 }
1226
1227 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1228 int srcW, int dstW, int filterAlign, int one, int flags,
1229 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
1230 {
1231 int i;
1232 int filterSize;
1233 int filter2Size;
1234 int minFilterSize;
1235 int64_t *filter=NULL;
1236 int64_t *filter2=NULL;
1237 const int64_t fone= 1LL<<54;
1238 int ret= -1;
1239 #if ARCH_X86
1240 if (flags & SWS_CPU_CAPS_MMX)
1241 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
1242 #endif
1243
1244 // NOTE: the +1 is for the MMX scaler which reads over the end
1245 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
1246
1247 if (FFABS(xInc - 0x10000) <10) // unscaled
1248 {
1249 int i;
1250 filterSize= 1;
1251 filter= av_mallocz(dstW*sizeof(*filter)*filterSize);
1252
1253 for (i=0; i<dstW; i++)
1254 {
1255 filter[i*filterSize]= fone;
1256 (*filterPos)[i]=i;
1257 }
1258
1259 }
1260 else if (flags&SWS_POINT) // lame looking point sampling mode
1261 {
1262 int i;
1263 int xDstInSrc;
1264 filterSize= 1;
1265 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1266
1267 xDstInSrc= xInc/2 - 0x8000;
1268 for (i=0; i<dstW; i++)
1269 {
1270 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1271
1272 (*filterPos)[i]= xx;
1273 filter[i]= fone;
1274 xDstInSrc+= xInc;
1275 }
1276 }
1277 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
1278 {
1279 int i;
1280 int xDstInSrc;
1281 if (flags&SWS_BICUBIC) filterSize= 4;
1282 else if (flags&SWS_X ) filterSize= 4;
1283 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
1284 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1285
1286 xDstInSrc= xInc/2 - 0x8000;
1287 for (i=0; i<dstW; i++)
1288 {
1289 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1290 int j;
1291
1292 (*filterPos)[i]= xx;
1293 //bilinear upscale / linear interpolate / area averaging
1294 for (j=0; j<filterSize; j++)
1295 {
1296 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
1297 if (coeff<0) coeff=0;
1298 filter[i*filterSize + j]= coeff;
1299 xx++;
1300 }
1301 xDstInSrc+= xInc;
1302 }
1303 }
1304 else
1305 {
1306 int xDstInSrc;
1307 int sizeFactor;
1308
1309 if (flags&SWS_BICUBIC) sizeFactor= 4;
1310 else if (flags&SWS_X) sizeFactor= 8;
1311 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
1312 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
1313 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
1314 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
1315 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
1316 else if (flags&SWS_BILINEAR) sizeFactor= 2;
1317 else {
1318 sizeFactor= 0; //GCC warning killer
1319 assert(0);
1320 }
1321
1322 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
1323 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
1324
1325 if (filterSize > srcW-2) filterSize=srcW-2;
1326
1327 filter= av_malloc(dstW*sizeof(*filter)*filterSize);
1328
1329 xDstInSrc= xInc - 0x10000;
1330 for (i=0; i<dstW; i++)
1331 {
1332 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
1333 int j;
1334 (*filterPos)[i]= xx;
1335 for (j=0; j<filterSize; j++)
1336 {
1337 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
1338 double floatd;
1339 int64_t coeff;
1340
1341 if (xInc > 1<<16)
1342 d= d*dstW/srcW;
1343 floatd= d * (1.0/(1<<30));
1344
1345 if (flags & SWS_BICUBIC)
1346 {
1347 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
1348 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
1349 int64_t dd = ( d*d)>>30;
1350 int64_t ddd= (dd*d)>>30;
1351
1352 if (d < 1LL<<30)
1353 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
1354 else if (d < 1LL<<31)
1355 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
1356 else
1357 coeff=0.0;
1358 coeff *= fone>>(30+24);
1359 }
1360 /* else if (flags & SWS_X)
1361 {
1362 double p= param ? param*0.01 : 0.3;
1363 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1364 coeff*= pow(2.0, - p*d*d);
1365 }*/
1366 else if (flags & SWS_X)
1367 {
1368 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1369 double c;
1370
1371 if (floatd<1.0)
1372 c = cos(floatd*PI);
1373 else
1374 c=-1.0;
1375 if (c<0.0) c= -pow(-c, A);
1376 else c= pow( c, A);
1377 coeff= (c*0.5 + 0.5)*fone;
1378 }
1379 else if (flags & SWS_AREA)
1380 {
1381 int64_t d2= d - (1<<29);
1382 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
1383 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
1384 else coeff=0.0;
1385 coeff *= fone>>(30+16);
1386 }
1387 else if (flags & SWS_GAUSS)
1388 {
1389 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1390 coeff = (pow(2.0, - p*floatd*floatd))*fone;
1391 }
1392 else if (flags & SWS_SINC)
1393 {
1394 coeff = (d ? sin(floatd*PI)/(floatd*PI) : 1.0)*fone;
1395 }
1396 else if (flags & SWS_LANCZOS)
1397 {
1398 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1399 coeff = (d ? sin(floatd*PI)*sin(floatd*PI/p)/(floatd*floatd*PI*PI/p) : 1.0)*fone;
1400 if (floatd>p) coeff=0;
1401 }
1402 else if (flags & SWS_BILINEAR)
1403 {
1404 coeff= (1<<30) - d;
1405 if (coeff<0) coeff=0;
1406 coeff *= fone >> 30;
1407 }
1408 else if (flags & SWS_SPLINE)
1409 {
1410 double p=-2.196152422706632;
1411 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
1412 }
1413 else {
1414 coeff= 0.0; //GCC warning killer
1415 assert(0);
1416 }
1417
1418 filter[i*filterSize + j]= coeff;
1419 xx++;
1420 }
1421 xDstInSrc+= 2*xInc;
1422 }
1423 }
1424
1425 /* apply src & dst Filter to filter -> filter2
1426 av_free(filter);
1427 */
1428 assert(filterSize>0);
1429 filter2Size= filterSize;
1430 if (srcFilter) filter2Size+= srcFilter->length - 1;
1431 if (dstFilter) filter2Size+= dstFilter->length - 1;
1432 assert(filter2Size>0);
1433 filter2= av_mallocz(filter2Size*dstW*sizeof(*filter2));
1434
1435 for (i=0; i<dstW; i++)
1436 {
1437 int j, k;
1438
1439 if(srcFilter){
1440 for (k=0; k<srcFilter->length; k++){
1441 for (j=0; j<filterSize; j++)
1442 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
1443 }
1444 }else{
1445 for (j=0; j<filterSize; j++)
1446 filter2[i*filter2Size + j]= filter[i*filterSize + j];
1447 }
1448 //FIXME dstFilter
1449
1450 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1451 }
1452 av_freep(&filter);
1453
1454 /* try to reduce the filter-size (step1 find size and shift left) */
1455 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1456 minFilterSize= 0;
1457 for (i=dstW-1; i>=0; i--)
1458 {
1459 int min= filter2Size;
1460 int j;
1461 int64_t cutOff=0.0;
1462
1463 /* get rid off near zero elements on the left by shifting left */
1464 for (j=0; j<filter2Size; j++)
1465 {
1466 int k;
1467 cutOff += FFABS(filter2[i*filter2Size]);
1468
1469 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1470
1471 /* preserve monotonicity because the core can't handle the filter otherwise */
1472 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1473
1474 // move filter coefficients left
1475 for (k=1; k<filter2Size; k++)
1476 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1477 filter2[i*filter2Size + k - 1]= 0;
1478 (*filterPos)[i]++;
1479 }
1480
1481 cutOff=0;
1482 /* count near zeros on the right */
1483 for (j=filter2Size-1; j>0; j--)
1484 {
1485 cutOff += FFABS(filter2[i*filter2Size + j]);
1486
1487 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
1488 min--;
1489 }
1490
1491 if (min>minFilterSize) minFilterSize= min;
1492 }
1493
1494 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1495 // we can handle the special case 4,
1496 // so we don't want to go to the full 8
1497 if (minFilterSize < 5)
1498 filterAlign = 4;
1499
1500 // We really don't want to waste our time
1501 // doing useless computation, so fall back on
1502 // the scalar C code for very small filters.
1503 // Vectorizing is worth it only if you have a
1504 // decent-sized vector.
1505 if (minFilterSize < 3)
1506 filterAlign = 1;
1507 }
1508
1509 if (flags & SWS_CPU_CAPS_MMX) {
1510 // special case for unscaled vertical filtering
1511 if (minFilterSize == 1 && filterAlign == 2)
1512 filterAlign= 1;
1513 }
1514
1515 assert(minFilterSize > 0);
1516 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1517 assert(filterSize > 0);
1518 filter= av_malloc(filterSize*dstW*sizeof(*filter));
1519 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1520 goto error;
1521 *outFilterSize= filterSize;
1522
1523 if (flags&SWS_PRINT_INFO)
1524 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1525 /* try to reduce the filter-size (step2 reduce it) */
1526 for (i=0; i<dstW; i++)
1527 {
1528 int j;
1529
1530 for (j=0; j<filterSize; j++)
1531 {
1532 if (j>=filter2Size) filter[i*filterSize + j]= 0;
1533 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1534 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1535 filter[i*filterSize + j]= 0;
1536 }
1537 }
1538
1539
1540 //FIXME try to align filterPos if possible
1541
1542 //fix borders
1543 for (i=0; i<dstW; i++)
1544 {
1545 int j;
1546 if ((*filterPos)[i] < 0)
1547 {
1548 // move filter coefficients left to compensate for filterPos
1549 for (j=1; j<filterSize; j++)
1550 {
1551 int left= FFMAX(j + (*filterPos)[i], 0);
1552 filter[i*filterSize + left] += filter[i*filterSize + j];
1553 filter[i*filterSize + j]=0;
1554 }
1555 (*filterPos)[i]= 0;
1556 }
1557
1558 if ((*filterPos)[i] + filterSize > srcW)
1559 {
1560 int shift= (*filterPos)[i] + filterSize - srcW;
1561 // move filter coefficients right to compensate for filterPos
1562 for (j=filterSize-2; j>=0; j--)
1563 {
1564 int right= FFMIN(j + shift, filterSize-1);
1565 filter[i*filterSize +right] += filter[i*filterSize +j];
1566 filter[i*filterSize +j]=0;
1567 }
1568 (*filterPos)[i]= srcW - filterSize;
1569 }
1570 }
1571
1572 // Note the +1 is for the MMX scaler which reads over the end
1573 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1574 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1575
1576 /* normalize & store in outFilter */
1577 for (i=0; i<dstW; i++)
1578 {
1579 int j;
1580 int64_t error=0;
1581 int64_t sum=0;
1582
1583 for (j=0; j<filterSize; j++)
1584 {
1585 sum+= filter[i*filterSize + j];
1586 }
1587 sum= (sum + one/2)/ one;
1588 for (j=0; j<*outFilterSize; j++)
1589 {
1590 int64_t v= filter[i*filterSize + j] + error;
1591 int intV= ROUNDED_DIV(v, sum);
1592 (*outFilter)[i*(*outFilterSize) + j]= intV;
1593 error= v - intV*sum;
1594 }
1595 }
1596
1597 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1598 for (i=0; i<*outFilterSize; i++)
1599 {
1600 int j= dstW*(*outFilterSize);
1601 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1602 }
1603
1604 ret=0;
1605 error:
1606 av_free(filter);
1607 av_free(filter2);
1608 return ret;
1609 }
1610
1611 #ifdef COMPILE_MMX2
1612 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1613 {
1614 uint8_t *fragmentA;
1615 x86_reg imm8OfPShufW1A;
1616 x86_reg imm8OfPShufW2A;
1617 x86_reg fragmentLengthA;
1618 uint8_t *fragmentB;
1619 x86_reg imm8OfPShufW1B;
1620 x86_reg imm8OfPShufW2B;
1621 x86_reg fragmentLengthB;
1622 int fragmentPos;
1623
1624 int xpos, i;
1625
1626 // create an optimized horizontal scaling routine
1627
1628 //code fragment
1629
1630 __asm__ volatile(
1631 "jmp 9f \n\t"
1632 // Begin
1633 "0: \n\t"
1634 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1635 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1636 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1637 "punpcklbw %%mm7, %%mm1 \n\t"
1638 "punpcklbw %%mm7, %%mm0 \n\t"
1639 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1640 "1: \n\t"
1641 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1642 "2: \n\t"
1643 "psubw %%mm1, %%mm0 \n\t"
1644 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1645 "pmullw %%mm3, %%mm0 \n\t"
1646 "psllw $7, %%mm1 \n\t"
1647 "paddw %%mm1, %%mm0 \n\t"
1648
1649 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1650
1651 "add $8, %%"REG_a" \n\t"
1652 // End
1653 "9: \n\t"
1654 // "int $3 \n\t"
1655 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1656 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1657 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1658 "dec %1 \n\t"
1659 "dec %2 \n\t"
1660 "sub %0, %1 \n\t"
1661 "sub %0, %2 \n\t"
1662 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1663 "sub %0, %3 \n\t"
1664
1665
1666 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1667 "=r" (fragmentLengthA)
1668 );
1669
1670 __asm__ volatile(
1671 "jmp 9f \n\t"
1672 // Begin
1673 "0: \n\t"
1674 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1675 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1676 "punpcklbw %%mm7, %%mm0 \n\t"
1677 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1678 "1: \n\t"
1679 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1680 "2: \n\t"
1681 "psubw %%mm1, %%mm0 \n\t"
1682 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1683 "pmullw %%mm3, %%mm0 \n\t"
1684 "psllw $7, %%mm1 \n\t"
1685 "paddw %%mm1, %%mm0 \n\t"
1686
1687 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1688
1689 "add $8, %%"REG_a" \n\t"
1690 // End
1691 "9: \n\t"
1692 // "int $3 \n\t"
1693 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1694 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1695 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1696 "dec %1 \n\t"
1697 "dec %2 \n\t"
1698 "sub %0, %1 \n\t"
1699 "sub %0, %2 \n\t"
1700 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1701 "sub %0, %3 \n\t"
1702
1703
1704 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1705 "=r" (fragmentLengthB)
1706 );
1707
1708 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1709 fragmentPos=0;
1710
1711 for (i=0; i<dstW/numSplits; i++)
1712 {
1713 int xx=xpos>>16;
1714
1715 if ((i&3) == 0)
1716 {
1717 int a=0;
1718 int b=((xpos+xInc)>>16) - xx;
1719 int c=((xpos+xInc*2)>>16) - xx;
1720 int d=((xpos+xInc*3)>>16) - xx;
1721
1722 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1723 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1724 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1725 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1726 filterPos[i/2]= xx;
1727
1728 if (d+1<4)
1729 {
1730 int maxShift= 3-(d+1);
1731 int shift=0;
1732
1733 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1734
1735 funnyCode[fragmentPos + imm8OfPShufW1B]=
1736 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1737 funnyCode[fragmentPos + imm8OfPShufW2B]=
1738 a | (b<<2) | (c<<4) | (d<<6);
1739
1740 if (i+3>=dstW) shift=maxShift; //avoid overread
1741 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1742
1743 if (shift && i>=shift)
1744 {
1745 funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1746 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1747 filterPos[i/2]-=shift;
1748 }
1749
1750 fragmentPos+= fragmentLengthB;
1751 }
1752 else
1753 {
1754 int maxShift= 3-d;
1755 int shift=0;
1756
1757 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1758
1759 funnyCode[fragmentPos + imm8OfPShufW1A]=
1760 funnyCode[fragmentPos + imm8OfPShufW2A]=
1761 a | (b<<2) | (c<<4) | (d<<6);
1762
1763 if (i+4>=dstW) shift=maxShift; //avoid overread
1764 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1765
1766 if (shift && i>=shift)
1767 {
1768 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1769 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1770 filterPos[i/2]-=shift;
1771 }
1772
1773 fragmentPos+= fragmentLengthA;
1774 }
1775
1776 funnyCode[fragmentPos]= RET;
1777 }
1778 xpos+=xInc;
1779 }
1780 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
1781 }
1782 #endif /* COMPILE_MMX2 */
1783
1784 static void globalInit(void){
1785 // generating tables:
1786 int i;
1787 for (i=0; i<768; i++){
1788 int c= av_clip_uint8(i-256);
1789 clip_table[i]=c;
1790 }
1791 }
1792
1793 static SwsFunc getSwsFunc(SwsContext *c)
1794 {
1795 int flags = c->flags;
1796
1797 #if CONFIG_RUNTIME_CPUDETECT && CONFIG_GPL
1798 #if ARCH_X86
1799 // ordered per speed fastest first
1800 if (flags & SWS_CPU_CAPS_MMX2) {
1801 sws_init_swScale_MMX2(c);
1802 return swScale_MMX2;
1803 } else if (flags & SWS_CPU_CAPS_3DNOW) {
1804 sws_init_swScale_3DNow(c);
1805 return swScale_3DNow;
1806 } else if (flags & SWS_CPU_CAPS_MMX) {
1807 sws_init_swScale_MMX(c);
1808 return swScale_MMX;
1809 } else {
1810 sws_init_swScale_C(c);
1811 return swScale_C;
1812 }
1813
1814 #else
1815 #if ARCH_PPC
1816 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1817 sws_init_swScale_altivec(c);
1818 return swScale_altivec;
1819 } else {
1820 sws_init_swScale_C(c);
1821 return swScale_C;
1822 }
1823 #endif
1824 sws_init_swScale_C(c);
1825 return swScale_C;
1826 #endif /* ARCH_X86 */
1827 #else //CONFIG_RUNTIME_CPUDETECT
1828 #if HAVE_MMX2
1829 sws_init_swScale_MMX2(c);
1830 return swScale_MMX2;
1831 #elif HAVE_AMD3DNOW
1832 sws_init_swScale_3DNow(c);
1833 return swScale_3DNow;
1834 #elif HAVE_MMX
1835 sws_init_swScale_MMX(c);
1836 return swScale_MMX;
1837 #elif HAVE_ALTIVEC
1838 sws_init_swScale_altivec(c);
1839 return swScale_altivec;
1840 #else
1841 sws_init_swScale_C(c);
1842 return swScale_C;
1843 #endif
1844 #endif //!CONFIG_RUNTIME_CPUDETECT
1845 }
1846
1847 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1848 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1849 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1850 /* Copy Y plane */
1851 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1852 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1853 else
1854 {
1855 int i;
1856 const uint8_t *srcPtr= src[0];
1857 uint8_t *dstPtr= dst;
1858 for (i=0; i<srcSliceH; i++)
1859 {
1860 memcpy(dstPtr, srcPtr, c->srcW);
1861 srcPtr+= srcStride[0];
1862 dstPtr+= dstStride[0];
1863 }
1864 }
1865 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1866 if (c->dstFormat == PIX_FMT_NV12)
1867 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1868 else
1869 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1870
1871 return srcSliceH;
1872 }
1873
1874 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1875 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1876 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1877
1878 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1879
1880 return srcSliceH;
1881 }
1882
1883 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1884 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1885 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1886
1887 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1888
1889 return srcSliceH;
1890 }
1891
1892 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1893 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1894 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1895
1896 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1897
1898 return srcSliceH;
1899 }
1900
1901 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1902 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1903 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1904
1905 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1906
1907 return srcSliceH;
1908 }
1909
1910 static int YUYV2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1911 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1912 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1913 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1914 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1915
1916 yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1917
1918 if (dstParam[3])
1919 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1920
1921 return srcSliceH;
1922 }
1923
1924 static int YUYV2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1925 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1926 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1927 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1928 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1929
1930 yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1931
1932 return srcSliceH;
1933 }
1934
1935 static int UYVY2YUV420Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1936 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1937 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1938 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
1939 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
1940
1941 uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1942
1943 if (dstParam[3])
1944 fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
1945
1946 return srcSliceH;
1947 }
1948
1949 static int UYVY2YUV422Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1950 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1951 uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
1952 uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
1953 uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
1954
1955 uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
1956
1957 return srcSliceH;
1958 }
1959
1960 static int pal2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1961 int srcSliceH, uint8_t* dst[], int dstStride[]){
1962 const enum PixelFormat srcFormat= c->srcFormat;
1963 const enum PixelFormat dstFormat= c->dstFormat;
1964 void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
1965 const uint8_t *palette)=NULL;
1966 int i;
1967 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1968 uint8_t *srcPtr= src[0];
1969
1970 if (!usePal(srcFormat))
1971 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1972 sws_format_name(srcFormat), sws_format_name(dstFormat));
1973
1974 switch(dstFormat){
1975 case PIX_FMT_RGB32 : conv = palette8topacked32; break;
1976 case PIX_FMT_BGR32 : conv = palette8topacked32; break;
1977 case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
1978 case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
1979 case PIX_FMT_RGB24 : conv = palette8topacked24; break;
1980 case PIX_FMT_BGR24 : conv = palette8topacked24; break;
1981 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1982 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1983 }
1984
1985
1986 for (i=0; i<srcSliceH; i++) {
1987 conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
1988 srcPtr+= srcStride[0];
1989 dstPtr+= dstStride[0];
1990 }
1991
1992 return srcSliceH;
1993 }
1994
1995 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1996 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1997 int srcSliceH, uint8_t* dst[], int dstStride[]){
1998 const enum PixelFormat srcFormat= c->srcFormat;
1999 const enum PixelFormat dstFormat= c->dstFormat;
2000 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
2001 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
2002 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
2003 const int dstId= fmt_depth(dstFormat) >> 2;
2004 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
2005
2006 /* BGR -> BGR */
2007 if ( (isBGR(srcFormat) && isBGR(dstFormat))
2008 || (isRGB(srcFormat) && isRGB(dstFormat))){
2009 switch(srcId | (dstId<<4)){
2010 case 0x34: conv= rgb16to15; break;
2011 case 0x36: conv= rgb24to15; break;
2012 case 0x38: conv= rgb32to15; break;
2013 case 0x43: conv= rgb15to16; break;
2014 case 0x46: conv= rgb24to16; break;
2015 case 0x48: conv= rgb32to16; break;
2016 case 0x63: conv= rgb15to24; break;
2017 case 0x64: conv= rgb16to24; break;
2018 case 0x68: conv= rgb32to24; break;
2019 case 0x83: conv= rgb15to32; break;
2020 case 0x84: conv= rgb16to32; break;
2021 case 0x86: conv= rgb24to32; break;
2022 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2023 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2024 }
2025 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
2026 || (isRGB(srcFormat) && isBGR(dstFormat))){
2027 switch(srcId | (dstId<<4)){
2028 case 0x33: conv= rgb15tobgr15; break;
2029 case 0x34: conv= rgb16tobgr15; break;
2030 case 0x36: conv= rgb24tobgr15; break;
2031 case 0x38: conv= rgb32tobgr15; break;
2032 case 0x43: conv= rgb15tobgr16; break;
2033 case 0x44: conv= rgb16tobgr16; break;
2034 case 0x46: conv= rgb24tobgr16; break;
2035 case 0x48: conv= rgb32tobgr16; break;
2036 case 0x63: conv= rgb15tobgr24; break;
2037 case 0x64: conv= rgb16tobgr24; break;
2038 case 0x66: conv= rgb24tobgr24; break;
2039 case 0x68: conv= rgb32tobgr24; break;
2040 case 0x83: conv= rgb15tobgr32; break;
2041 case 0x84: conv= rgb16tobgr32; break;
2042 case 0x86: conv= rgb24tobgr32; break;
2043 case 0x88: conv= rgb32tobgr32; break;
2044 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2045 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
2046 }
2047 }else{
2048 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
2049 sws_format_name(srcFormat), sws_format_name(dstFormat));
2050 }
2051
2052 if(conv)
2053 {
2054 uint8_t *srcPtr= src[0];
2055 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
2056 srcPtr += ALT32_CORR;
2057
2058 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
2059 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
2060 else
2061 {
2062 int i;
2063 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2064
2065 for (i=0; i<srcSliceH; i++)
2066 {
2067 conv(srcPtr, dstPtr, c->srcW*srcBpp);
2068 srcPtr+= srcStride[0];
2069 dstPtr+= dstStride[0];
2070 }
2071 }
2072 }
2073 return srcSliceH;
2074 }
2075
2076 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2077 int srcSliceH, uint8_t* dst[], int dstStride[]){
2078
2079 rgb24toyv12(
2080 src[0],
2081 dst[0]+ srcSliceY *dstStride[0],
2082 dst[1]+(srcSliceY>>1)*dstStride[1],
2083 dst[2]+(srcSliceY>>1)*dstStride[2],
2084 c->srcW, srcSliceH,
2085 dstStride[0], dstStride[1], srcStride[0]);
2086 if (dst[3])
2087 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2088 return srcSliceH;
2089 }
2090
2091 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2092 int srcSliceH, uint8_t* dst[], int dstStride[]){
2093 int i;
2094
2095 /* copy Y */
2096 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
2097 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
2098 else{
2099 uint8_t *srcPtr= src[0];
2100 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2101
2102 for (i=0; i<srcSliceH; i++)
2103 {
2104 memcpy(dstPtr, srcPtr, c->srcW);
2105 srcPtr+= srcStride[0];
2106 dstPtr+= dstStride[0];
2107 }
2108 }
2109
2110 if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P){
2111 planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
2112 planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
2113 }else{
2114 planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
2115 planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
2116 }
2117 if (dst[3])
2118 fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
2119 return srcSliceH;
2120 }
2121
2122 /* unscaled copy like stuff (assumes nearly identical formats) */
2123 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2124 int srcSliceH, uint8_t* dst[], int dstStride[])
2125 {
2126 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
2127 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
2128 else
2129 {
2130 int i;
2131 uint8_t *srcPtr= src[0];
2132 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
2133 int length=0;
2134
2135 /* universal length finder */
2136 while(length+c->srcW <= FFABS(dstStride[0])
2137 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
2138 assert(length!=0);
2139
2140 for (i=0; i<srcSliceH; i++)
2141 {
2142 memcpy(dstPtr, srcPtr, length);
2143 srcPtr+= srcStride[0];
2144 dstPtr+= dstStride[0];
2145 }
2146 }
2147 return srcSliceH;
2148 }
2149
2150 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2151 int srcSliceH, uint8_t* dst[], int dstStride[])
2152 {
2153 int plane, i, j;
2154 for (plane=0; plane<4; plane++)
2155 {
2156 int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
2157 int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
2158 int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2159 uint8_t *srcPtr= src[plane];
2160 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
2161
2162 if (!dst[plane]) continue;
2163 // ignore palette for GRAY8
2164 if (plane == 1 && !dst[2]) continue;
2165 if (!src[plane] || (plane == 1 && !src[2])){
2166 if(is16BPS(c->dstFormat))
2167 length*=2;
2168 fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
2169 }else
2170 {
2171 if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)){
2172 if (!isBE(c->srcFormat)) srcPtr++;
2173 for (i=0; i<height; i++){
2174 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
2175 srcPtr+= srcStride[plane];
2176 dstPtr+= dstStride[plane];
2177 }
2178 }else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)){
2179 for (i=0; i<height; i++){
2180 for (j=0; j<length; j++){
2181 dstPtr[ j<<1 ] = srcPtr[j];
2182 dstPtr[(j<<1)+1] = srcPtr[j];
2183 }
2184 srcPtr+= srcStride[plane];
2185 dstPtr+= dstStride[plane];
2186 }
2187 }else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
2188 && isBE(c->srcFormat) != isBE(c->dstFormat)){
2189
2190 for (i=0; i<height; i++){
2191 for (j=0; j<length; j++)
2192 ((uint16_t*)dstPtr)[j] = bswap_16(((uint16_t*)srcPtr)[j]);
2193 srcPtr+= srcStride[plane];
2194 dstPtr+= dstStride[plane];
2195 }
2196 } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
2197 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
2198 else
2199 {
2200 if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
2201 length*=2;
2202 for (i=0; i<height; i++)
2203 {
2204 memcpy(dstPtr, srcPtr, length);
2205 srcPtr+= srcStride[plane];
2206 dstPtr+= dstStride[plane];
2207 }
2208 }
2209 }
2210 }
2211 return srcSliceH;
2212 }
2213
2214
2215 static void getSubSampleFactors(int *h, int *v, int format){
2216 switch(format){
2217 case PIX_FMT_UYVY422:
2218 case PIX_FMT_YUYV422:
2219 *h=1;
2220 *v=0;
2221 break;
2222 case PIX_FMT_YUV420P:
2223 case PIX_FMT_YUV420PLE:
2224 case PIX_FMT_YUV420PBE:
2225 case PIX_FMT_YUVA420P:
2226 case PIX_FMT_GRAY16BE:
2227 case PIX_FMT_GRAY16LE:
2228 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
2229 case PIX_FMT_NV12:
2230 case PIX_FMT_NV21:
2231 *h=1;
2232 *v=1;
2233 break;
2234 case PIX_FMT_YUV440P:
2235 *h=0;
2236 *v=1;
2237 break;
2238 case PIX_FMT_YUV410P:
2239 *h=2;
2240 *v=2;
2241 break;
2242 case PIX_FMT_YUV444P:
2243 case PIX_FMT_YUV444PLE:
2244 case PIX_FMT_YUV444PBE:
2245 *h=0;
2246 *v=0;
2247 break;
2248 case PIX_FMT_YUV422P:
2249 case PIX_FMT_YUV422PLE:
2250 case PIX_FMT_YUV422PBE:
2251 *h=1;
2252 *v=0;
2253 break;
2254 case PIX_FMT_YUV411P:
2255 *h=2;
2256 *v=0;
2257 break;
2258 default:
2259 *h=0;
2260 *v=0;
2261 break;
2262 }
2263 }
2264
2265 static uint16_t roundToInt16(int64_t f){
2266 int r= (f + (1<<15))>>16;
2267 if (r<-0x7FFF) return 0x8000;
2268 else if (r> 0x7FFF) return 0x7FFF;
2269 else return r;
2270 }
2271
2272 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
2273 int64_t crv = inv_table[0];
2274 int64_t cbu = inv_table[1];
2275 int64_t cgu = -inv_table[2];
2276 int64_t cgv = -inv_table[3];
2277 int64_t cy = 1<<16;
2278 int64_t oy = 0;
2279
2280 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
2281 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
2282
2283 c->brightness= brightness;
2284 c->contrast = contrast;
2285 c->saturation= saturation;
2286 c->srcRange = srcRange;
2287 c->dstRange = dstRange;
2288 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2289
2290 c->uOffset= 0x0400040004000400LL;
2291 c->vOffset= 0x0400040004000400LL;
2292
2293 if (!srcRange){
2294 cy= (cy*255) / 219;
2295 oy= 16<<16;
2296 }else{
2297 crv= (crv*224) / 255;
2298 cbu= (cbu*224) / 255;
2299 cgu= (cgu*224) / 255;
2300 cgv= (cgv*224) / 255;
2301 }
2302
2303 cy = (cy *contrast )>>16;
2304 crv= (crv*contrast * saturation)>>32;
2305 cbu= (cbu*contrast * saturation)>>32;
2306 cgu= (cgu*contrast * saturation)>>32;
2307 cgv= (cgv*contrast * saturation)>>32;
2308
2309 oy -= 256*brightness;
2310
2311 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
2312 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
2313 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
2314 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
2315 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
2316 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
2317
2318 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
2319 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
2320 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
2321 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
2322 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
2323 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
2324
2325 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
2326 //FIXME factorize
2327
2328 #ifdef COMPILE_ALTIVEC
2329 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
2330 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
2331 #endif
2332 return 0;
2333 }
2334
2335 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
2336 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
2337
2338 *inv_table = c->srcColorspaceTable;
2339 *table = c->dstColorspaceTable;
2340 *srcRange = c->srcRange;
2341 *dstRange = c->dstRange;
2342 *brightness= c->brightness;
2343 *contrast = c->contrast;
2344 *saturation= c->saturation;
2345
2346 return 0;
2347 }
2348
2349 static int handle_jpeg(enum PixelFormat *format)
2350 {
2351 switch (*format) {
2352 case PIX_FMT_YUVJ420P:
2353 *format = PIX_FMT_YUV420P;
2354 return 1;
2355 case PIX_FMT_YUVJ422P:
2356 *format = PIX_FMT_YUV422P;
2357 return 1;
2358 case PIX_FMT_YUVJ444P:
2359 *format = PIX_FMT_YUV444P;
2360 return 1;
2361 case PIX_FMT_YUVJ440P:
2362 *format = PIX_FMT_YUV440P;
2363 return 1;
2364 default:
2365 return 0;
2366 }
2367 }
2368
2369 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat, int dstW, int dstH, enum PixelFormat dstFormat, int flags,
2370 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
2371
2372 SwsContext *c;
2373 int i;
2374 int usesVFilter, usesHFilter;
2375 int unscaled, needsDither;
2376 int srcRange, dstRange;
2377 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2378 #if ARCH_X86
2379 if (flags & SWS_CPU_CAPS_MMX)
2380 __asm__ volatile("emms\n\t"::: "memory");
2381 #endif
2382
2383 #if !CONFIG_RUNTIME_CPUDETECT || !CONFIG_GPL //ensure that the flags match the compiled variant if cpudetect is off
2384 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2385 #if HAVE_MMX2
2386 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2387 #elif HAVE_AMD3DNOW
2388 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2389 #elif HAVE_MMX
2390 flags |= SWS_CPU_CAPS_MMX;
2391 #elif HAVE_ALTIVEC
2392 flags |= SWS_CPU_CAPS_ALTIVEC;
2393 #elif ARCH_BFIN
2394 flags |= SWS_CPU_CAPS_BFIN;
2395 #endif
2396 #endif /* CONFIG_RUNTIME_CPUDETECT */
2397 if (clip_table[512] != 255) globalInit();
2398 if (!rgb15to16) sws_rgb2rgb_init(flags);
2399
2400 unscaled = (srcW == dstW && srcH == dstH);
2401 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2402 && (fmt_depth(dstFormat))<24
2403 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2404
2405 srcRange = handle_jpeg(&srcFormat);
2406 dstRange = handle_jpeg(&dstFormat);
2407
2408 if (!isSupportedIn(srcFormat))
2409 {
2410 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2411 return NULL;
2412 }
2413 if (!isSupportedOut(dstFormat))
2414 {
2415 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2416 return NULL;
2417 }
2418
2419 i= flags & ( SWS_POINT
2420 |SWS_AREA
2421 |SWS_BILINEAR
2422 |SWS_FAST_BILINEAR
2423 |SWS_BICUBIC
2424 |SWS_X
2425 |SWS_GAUSS
2426 |SWS_LANCZOS
2427 |SWS_SINC
2428 |SWS_SPLINE
2429 |SWS_BICUBLIN);
2430 if(!i || (i & (i-1)))
2431 {
2432 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
2433 return NULL;
2434 }
2435
2436 /* sanity check */
2437 if (srcW<4 || srcH<1 || dstW<8 || dstH<1) //FIXME check if these are enough and try to lowwer them after fixing the relevant parts of the code
2438 {
2439 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2440 srcW, srcH, dstW, dstH);
2441 return NULL;
2442 }
2443 if(srcW > VOFW || dstW > VOFW){
2444 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2445 return NULL;
2446 }
2447
2448 if (!dstFilter) dstFilter= &dummyFilter;
2449 if (!srcFilter) srcFilter= &dummyFilter;
2450
2451 c= av_mallocz(sizeof(SwsContext));
2452
2453 c->av_class = &sws_context_class;
2454 c->srcW= srcW;
2455 c->srcH= srcH;
2456 c->dstW= dstW;
2457 c->dstH= dstH;
2458 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2459 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2460 c->flags= flags;
2461 c->dstFormat= dstFormat;
2462 c->srcFormat= srcFormat;
2463 c->vRounder= 4* 0x0001000100010001ULL;
2464
2465 usesHFilter= usesVFilter= 0;
2466 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2467 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2468 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2469 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2470 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2471 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2472 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2473 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2474
2475 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2476 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2477
2478 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
2479 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2480
2481 // drop some chroma lines if the user wants it
2482 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2483 c->chrSrcVSubSample+= c->vChrDrop;
2484
2485 // drop every other pixel for chroma calculation unless user wants full chroma
2486 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2487 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2488 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2489 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2490 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2491 c->chrSrcHSubSample=1;
2492
2493 if (param){
2494 c->param[0] = param[0];
2495 c->param[1] = param[1];
2496 }else{
2497 c->param[0] =
2498 c->param[1] = SWS_PARAM_DEFAULT;
2499 }
2500
2501 c->chrIntHSubSample= c->chrDstHSubSample;
2502 c->chrIntVSubSample= c->chrSrcVSubSample;
2503
2504 // Note the -((-x)>>y) is so that we always round toward +inf.
2505 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2506 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2507 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2508 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2509
2510 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2511
2512 /* unscaled special cases */
2513 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2514 {
2515 /* yv12_to_nv12 */
2516 if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2517 {
2518 c->swScale= PlanarToNV12Wrapper;
2519 }
2520 /* yuv2bgr */
2521 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
2522 && !(flags & SWS_ACCURATE_RND) && !(dstH&1))
2523 {
2524 c->swScale= ff_yuv2rgb_get_func_ptr(c);
2525 }
2526
2527 if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT))
2528 {
2529 c->swScale= yvu9toyv12Wrapper;
2530 }
2531
2532 /* bgr24toYV12 */
2533 if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
2534 c->swScale= bgr24toyv12Wrapper;
2535
2536 /* RGB/BGR -> RGB/BGR (no dither needed forms) */
2537 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2538 && (isBGR(dstFormat) || isRGB(dstFormat))
2539 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2540 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2541 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2542 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2543 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2544 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2545 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2546 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2547 && dstFormat != PIX_FMT_RGB32_1
2548 && dstFormat != PIX_FMT_BGR32_1
2549 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2550 c->swScale= rgb2rgbWrapper;
2551
2552 if ((usePal(srcFormat) && (
2553 dstFormat == PIX_FMT_RGB32 ||
2554 dstFormat == PIX_FMT_RGB32_1 ||
2555 dstFormat == PIX_FMT_RGB24 ||
2556 dstFormat == PIX_FMT_BGR32 ||
2557 dstFormat == PIX_FMT_BGR32_1 ||
2558 dstFormat == PIX_FMT_BGR24)))
2559 c->swScale= pal2rgbWrapper;
2560
2561 if (srcFormat == PIX_FMT_YUV422P)
2562 {
2563 if (dstFormat == PIX_FMT_YUYV422)
2564 c->swScale= YUV422PToYuy2Wrapper;
2565 else if (dstFormat == PIX_FMT_UYVY422)
2566 c->swScale= YUV422PToUyvyWrapper;
2567 }
2568
2569 /* LQ converters if -sws 0 or -sws 4*/
2570 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2571 /* yv12_to_yuy2 */
2572 if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P)
2573 {
2574 if (dstFormat == PIX_FMT_YUYV422)
2575 c->swScale= PlanarToYuy2Wrapper;
2576 else if (dstFormat == PIX_FMT_UYVY422)
2577 c->swScale= PlanarToUyvyWrapper;
2578 }
2579 }
2580 if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2581 c->swScale= YUYV2YUV420Wrapper;
2582 if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
2583 c->swScale= UYVY2YUV420Wrapper;
2584 if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
2585 c->swScale= YUYV2YUV422Wrapper;
2586 if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
2587 c->swScale= UYVY2YUV422Wrapper;
2588
2589 #ifdef COMPILE_ALTIVEC
2590 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2591 !(c->flags & SWS_BITEXACT) &&
2592 srcFormat == PIX_FMT_YUV420P) {
2593 // unscaled YV12 -> packed YUV, we want speed
2594 if (dstFormat == PIX_FMT_YUYV422)
2595 c->swScale= yv12toyuy2_unscaled_altivec;
2596 else if (dstFormat == PIX_FMT_UYVY422)
2597 c->swScale= yv12touyvy_unscaled_altivec;
2598 }
2599 #endif
2600
2601 /* simple copy */
2602 if ( srcFormat == dstFormat
2603 || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
2604 || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
2605 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2606 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2607 || (isGray(dstFormat) && isGray(srcFormat))
2608 || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
2609 && c->chrDstHSubSample == c->chrSrcHSubSample
2610 && c->chrDstVSubSample == c->chrSrcVSubSample))
2611 {
2612 if (isPacked(c->srcFormat))
2613 c->swScale= packedCopy;
2614 else /* Planar YUV or gray */
2615 c->swScale= planarCopy;
2616 }
2617 #if ARCH_BFIN
2618 if (flags & SWS_CPU_CAPS_BFIN)
2619 ff_bfin_get_unscaled_swscale (c);
2620 #endif
2621
2622 if (c->swScale){
2623 if (flags&SWS_PRINT_INFO)
2624 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2625 sws_format_name(srcFormat), sws_format_name(dstFormat));
2626 return c;
2627 }
2628 }
2629
2630 if (flags & SWS_CPU_CAPS_MMX2)
2631 {
2632 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2633 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2634 {
2635 if (flags&SWS_PRINT_INFO)
2636 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
2637 }
2638 if (usesHFilter) c->canMMX2BeUsed=0;
2639 }
2640 else
2641 c->canMMX2BeUsed=0;
2642
2643 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2644 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2645
2646 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2647 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2648 // n-2 is the last chrominance sample available
2649 // this is not perfect, but no one should notice the difference, the more correct variant
2650 // would be like the vertical one, but that would require some special code for the
2651 // first and last pixel
2652 if (flags&SWS_FAST_BILINEAR)
2653 {
2654 if (c->canMMX2BeUsed)
2655 {
2656 c->lumXInc+= 20;
2657 c->chrXInc+= 20;
2658 }
2659 //we don't use the x86 asm scaler if MMX is available
2660 else if (flags & SWS_CPU_CAPS_MMX)
2661 {
2662 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2663 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2664 }
2665 }
2666
2667 /* precalculate horizontal scaler filter coefficients */
2668 {
2669 const int filterAlign=
2670 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2671 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2672 1;
2673
2674 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2675 srcW , dstW, filterAlign, 1<<14,
2676 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2677 srcFilter->lumH, dstFilter->lumH, c->param);
2678 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2679 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2680 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2681 srcFilter->chrH, dstFilter->chrH, c->param);
2682
2683 #define MAX_FUNNY_CODE_SIZE 10000
2684 #if defined(COMPILE_MMX2)
2685 // can't downscale !!!
2686 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2687 {
2688 #ifdef MAP_ANONYMOUS
2689 c->funnyYCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2690 c->funnyUVCode = mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2691 #elif HAVE_VIRTUALALLOC
2692 c->funnyYCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2693 c->funnyUVCode = VirtualAlloc(NULL, MAX_FUNNY_CODE_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
2694 #else
2695 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2696 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2697 #endif
2698
2699 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2700 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2701 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2702 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2703
2704 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2705 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2706 }
2707 #endif /* defined(COMPILE_MMX2) */
2708 } // initialize horizontal stuff
2709
2710
2711
2712 /* precalculate vertical scaler filter coefficients */
2713 {
2714 const int filterAlign=
2715 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2716 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2717 1;
2718
2719 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2720 srcH , dstH, filterAlign, (1<<12),
2721 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2722 srcFilter->lumV, dstFilter->lumV, c->param);
2723 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2724 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2725 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2726 srcFilter->chrV, dstFilter->chrV, c->param);
2727
2728 #if HAVE_ALTIVEC
2729 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2730 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2731
2732 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2733 int j;
2734 short *p = (short *)&c->vYCoeffsBank[i];
2735 for (j=0;j<8;j++)
2736 p[j] = c->vLumFilter[i];
2737 }
2738
2739 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2740 int j;
2741 short *p = (short *)&c->vCCoeffsBank[i];
2742 for (j=0;j<8;j++)
2743 p[j] = c->vChrFilter[i];
2744 }
2745 #endif
2746 }
2747
2748 // calculate buffer sizes so that they won't run out while handling these damn slices
2749 c->vLumBufSize= c->vLumFilterSize;
2750 c->vChrBufSize= c->vChrFilterSize;
2751 for (i=0; i<dstH; i++)
2752 {
2753 int chrI= i*c->chrDstH / dstH;
2754 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2755 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2756
2757 nextSlice>>= c->chrSrcVSubSample;
2758 nextSlice<<= c->chrSrcVSubSample;
2759 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2760 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2761 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2762 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2763 }
2764
2765 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2766 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2767 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2768 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
2769 c->alpPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2770 //Note we need at least one pixel more at the end because of the MMX code (just in case someone wanna replace the 4000/8000)
2771 /* align at 16 bytes for AltiVec */
2772 for (i=0; i<c->vLumBufSize; i++)
2773 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2774 for (i=0; i<c->vChrBufSize; i++)
2775 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2776 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
2777 for (i=0; i<c->vLumBufSize; i++)
2778 c->alpPixBuf[i]= c->alpPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2779
2780 //try to avoid drawing green stuff between the right end and the stride end
2781 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2782
2783 assert(2*VOFW == VOF);
2784
2785 assert(c->chrDstH <= dstH);
2786
2787 if (flags&SWS_PRINT_INFO)
2788 {
2789 #ifdef DITHER1XBPP
2790 const char *dither= " dithered";
2791 #else
2792 const char *dither= "";
2793 #endif
2794 if (flags&SWS_FAST_BILINEAR)
2795 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2796 else if (flags&SWS_BILINEAR)
2797 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2798 else if (flags&SWS_BICUBIC)
2799 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2800 else if (flags&SWS_X)
2801 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2802 else if (flags&SWS_POINT)
2803 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2804 else if (flags&SWS_AREA)
2805 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2806 else if (flags&SWS_BICUBLIN)
2807 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2808 else if (flags&SWS_GAUSS)
2809 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2810 else if (flags&SWS_SINC)
2811 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2812 else if (flags&SWS_LANCZOS)
2813 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2814 else if (flags&SWS_SPLINE)
2815 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2816 else
2817 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2818
2819 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2820 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2821 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2822 else
2823 av_log(c, AV_LOG_INFO, "from %s to %s ",
2824 sws_format_name(srcFormat), sws_format_name(dstFormat));
2825
2826 if (flags & SWS_CPU_CAPS_MMX2)
2827 av_log(c, AV_LOG_INFO, "using MMX2\n");
2828 else if (flags & SWS_CPU_CAPS_3DNOW)
2829 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2830 else if (flags & SWS_CPU_CAPS_MMX)
2831 av_log(c, AV_LOG_INFO, "using MMX\n");
2832 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2833 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2834 else
2835 av_log(c, AV_LOG_INFO, "using C\n");
2836 }
2837
2838 if (flags & SWS_PRINT_INFO)
2839 {
2840 if (flags & SWS_CPU_CAPS_MMX)
2841 {
2842 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2843 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2844 else
2845 {
2846 if (c->hLumFilterSize==4)
2847 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2848 else if (c->hLumFilterSize==8)
2849 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2850 else
2851 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2852
2853 if (c->hChrFilterSize==4)
2854 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2855 else if (c->hChrFilterSize==8)
2856 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2857 else
2858 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2859 }
2860 }
2861 else
2862 {
2863 #if ARCH_X86
2864 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
2865 #else
2866 if (flags & SWS_FAST_BILINEAR)
2867 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
2868 else
2869 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
2870 #endif
2871 }
2872 if (isPlanarYUV(dstFormat))
2873 {
2874 if (c->vLumFilterSize==1)
2875 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2876 else
2877 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2878 }
2879 else
2880 {
2881 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2882 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2883 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2884 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2885 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2886 else
2887 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2888 }
2889
2890 if (dstFormat==PIX_FMT_BGR24)
2891 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
2892 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2893 else if (dstFormat==PIX_FMT_RGB32)
2894 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2895 else if (dstFormat==PIX_FMT_BGR565)
2896 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2897 else if (dstFormat==PIX_FMT_BGR555)
2898 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2899
2900 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2901 }
2902 if (flags & SWS_PRINT_INFO)
2903 {
2904 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2905 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2906 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2907 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2908 }
2909
2910 c->swScale= getSwsFunc(c);
2911 return c;
2912 }
2913
2914 static void reset_ptr(uint8_t* src[], int format){
2915 if(!isALPHA(format))
2916 src[3]=NULL;
2917 if(!isPlanarYUV(format)){
2918 src[3]=src[2]=NULL;
2919 if( format != PIX_FMT_PAL8
2920 && format != PIX_FMT_RGB8
2921 && format != PIX_FMT_BGR8
2922 && format != PIX_FMT_RGB4_BYTE
2923 && format != PIX_FMT_BGR4_BYTE
2924 )
2925 src[1]= NULL;
2926 }
2927 }
2928
2929 /**
2930 * swscale wrapper, so we don't need to export the SwsContext.
2931 * Assumes planar YUV to be in YUV order instead of YVU.
2932 */
2933 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2934 int srcSliceH, uint8_t* dst[], int dstStride[]){
2935 int i;
2936 uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
2937 uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
2938
2939 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2940 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2941 return 0;
2942 }
2943 if (c->sliceDir == 0) {
2944 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2945 }
2946
2947 if (usePal(c->srcFormat)){
2948 for (i=0; i<256; i++){
2949 int p, r, g, b,y,u,v;
2950 if(c->srcFormat == PIX_FMT_PAL8){
2951 p=((uint32_t*)(src[1]))[i];
2952 r= (p>>16)&0xFF;
2953 g= (p>> 8)&0xFF;
2954 b= p &0xFF;
2955 }else if(c->srcFormat == PIX_FMT_RGB8){
2956 r= (i>>5 )*36;
2957 g= ((i>>2)&7)*36;
2958 b= (i&3 )*85;
2959 }else if(c->srcFormat == PIX_FMT_BGR8){
2960 b= (i>>6 )*85;
2961 g= ((i>>3)&7)*36;
2962 r= (i&7 )*36;
2963 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
2964 r= (i>>3 )*255;
2965 g= ((i>>1)&3)*85;
2966 b= (i&1 )*255;
2967 }else {
2968 assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
2969 b= (i>>3 )*255;
2970 g= ((i>>1)&3)*85;
2971 r= (i&1 )*255;
2972 }
2973 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2974 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2975 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2976 c->pal_yuv[i]= y + (u<<8) + (v<<16);
2977
2978
2979 switch(c->dstFormat) {
2980 case PIX_FMT_BGR32:
2981 #ifndef WORDS_BIGENDIAN
2982 case PIX_FMT_RGB24:
2983 #endif
2984 c->pal_rgb[i]= r + (g<<8) + (b<<16);
2985 break;
2986 case PIX_FMT_BGR32_1:
2987 #ifdef WORDS_BIGENDIAN
2988 case PIX_FMT_BGR24:
2989 #endif
2990 c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
2991 break;
2992 case PIX_FMT_RGB32_1:
2993 #ifdef WORDS_BIGENDIAN
2994 case PIX_FMT_RGB24:
2995 #endif
2996 c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
2997 break;
2998 case PIX_FMT_RGB32:
2999 #ifndef WORDS_BIGENDIAN
3000 case PIX_FMT_BGR24:
3001 #endif
3002 default:
3003 c->pal_rgb[i]= b + (g<<8) + (r<<16);
3004 }
3005 }
3006 }
3007
3008 // copy strides, so they can safely be modified
3009 if (c->sliceDir == 1) {
3010 // slices go from top to bottom
3011 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
3012 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
3013
3014 reset_ptr(src2, c->srcFormat);
3015 reset_ptr(dst2, c->dstFormat);
3016
3017 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
3018 } else {
3019 // slices go from bottom to top => we flip the image internally
3020 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
3021 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
3022
3023 src2[0] += (srcSliceH-1)*srcStride[0];
3024 if (!usePal(c->srcFormat))
3025 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
3026 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
3027 src2[3] += (srcSliceH-1)*srcStride[3];
3028 dst2[0] += ( c->dstH -1)*dstStride[0];
3029 dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
3030 dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
3031 dst2[3] += ( c->dstH -1)*dstStride[3];
3032
3033 reset_ptr(src2, c->srcFormat);
3034 reset_ptr(dst2, c->dstFormat);
3035
3036 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
3037 }
3038 }
3039
3040 #if LIBSWSCALE_VERSION_MAJOR < 1
3041 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
3042 int srcSliceH, uint8_t* dst[], int dstStride[]){
3043 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
3044 }
3045 #endif
3046
3047 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
3048 float lumaSharpen, float chromaSharpen,
3049 float chromaHShift, float chromaVShift,
3050 int verbose)
3051 {
3052 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
3053
3054 if (lumaGBlur!=0.0){
3055 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
3056 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
3057 }else{
3058 filter->lumH= sws_getIdentityVec();
3059 filter->lumV= sws_getIdentityVec();
3060 }
3061
3062 if (chromaGBlur!=0.0){
3063 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
3064 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
3065 }else{
3066 filter->chrH= sws_getIdentityVec();
3067 filter->chrV= sws_getIdentityVec();
3068 }
3069
3070 if (chromaSharpen!=0.0){
3071 SwsVector *id= sws_getIdentityVec();
3072 sws_scaleVec(filter->chrH, -chromaSharpen);
3073 sws_scaleVec(filter->chrV, -chromaSharpen);
3074 sws_addVec(filter->chrH, id);
3075 sws_addVec(filter->chrV, id);
3076 sws_freeVec(id);
3077 }
3078
3079 if (lumaSharpen!=0.0){
3080 SwsVector *id= sws_getIdentityVec();
3081 sws_scaleVec(filter->lumH, -lumaSharpen);
3082 sws_scaleVec(filter->lumV, -lumaSharpen);
3083 sws_addVec(filter->lumH, id);
3084 sws_addVec(filter->lumV, id);
3085 sws_freeVec(id);
3086 }
3087
3088 if (chromaHShift != 0.0)
3089 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
3090
3091 if (chromaVShift != 0.0)
3092 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
3093
3094 sws_normalizeVec(filter->chrH, 1.0);
3095 sws_normalizeVec(filter->chrV, 1.0);
3096 sws_normalizeVec(filter->lumH, 1.0);
3097 sws_normalizeVec(filter->lumV, 1.0);
3098
3099 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
3100 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
3101
3102 return filter;
3103 }
3104
3105 SwsVector *sws_getGaussianVec(double variance, double quality){
3106 const int length= (int)(variance*quality + 0.5) | 1;
3107 int i;
3108 double *coeff= av_malloc(length*sizeof(double));
3109 double middle= (length-1)*0.5;
3110 SwsVector *vec= av_malloc(sizeof(SwsVector));
3111
3112 vec->coeff= coeff;
3113 vec->length= length;
3114
3115 for (i=0; i<length; i++)
3116 {
3117 double dist= i-middle;
3118 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
3119 }
3120
3121 sws_normalizeVec(vec, 1.0);
3122
3123 return vec;
3124 }
3125
3126 SwsVector *sws_getConstVec(double c, int length){
3127 int i;
3128 double *coeff= av_malloc(length*sizeof(double));
3129 SwsVector *vec= av_malloc(sizeof(SwsVector));
3130
3131 vec->coeff= coeff;
3132 vec->length= length;
3133
3134 for (i=0; i<length; i++)
3135 coeff[i]= c;
3136
3137 return vec;
3138 }
3139
3140
3141 SwsVector *sws_getIdentityVec(void){
3142 return sws_getConstVec(1.0, 1);
3143 }
3144
3145 double sws_dcVec(SwsVector *a){
3146 int i;
3147 double sum=0;
3148
3149 for (i=0; i<a->length; i++)
3150 sum+= a->coeff[i];
3151
3152 return sum;
3153 }
3154
3155 void sws_scaleVec(SwsVector *a, double scalar){
3156 int i;
3157
3158 for (i=0; i<a->length; i++)
3159 a->coeff[i]*= scalar;
3160 }
3161
3162 void sws_normalizeVec(SwsVector *a, double height){
3163 sws_scaleVec(a, height/sws_dcVec(a));
3164 }
3165
3166 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
3167 int length= a->length + b->length - 1;
3168 double *coeff= av_malloc(length*sizeof(double));
3169 int i, j;
3170 SwsVector *vec= av_malloc(sizeof(SwsVector));
3171
3172 vec->coeff= coeff;
3173 vec->length= length;
3174
3175 for (i=0; i<length; i++) coeff[i]= 0.0;
3176
3177 for (i=0; i<a->length; i++)
3178 {
3179 for (j=0; j<b->length; j++)
3180 {
3181 coeff[i+j]+= a->coeff[i]*b->coeff[j];
3182 }
3183 }
3184
3185 return vec;
3186 }
3187
3188 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
3189 int length= FFMAX(a->length, b->length);
3190 double *coeff= av_malloc(length*sizeof(double));
3191 int i;
3192 SwsVector *vec= av_malloc(sizeof(SwsVector));
3193
3194 vec->coeff= coeff;
3195 vec->length= length;
3196
3197 for (i=0; i<length; i++) coeff[i]= 0.0;
3198
3199 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3200 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
3201
3202 return vec;
3203 }
3204
3205 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
3206 int length= FFMAX(a->length, b->length);
3207 double *coeff= av_malloc(length*sizeof(double));
3208 int i;
3209 SwsVector *vec= av_malloc(sizeof(SwsVector));
3210
3211 vec->coeff= coeff;
3212 vec->length= length;
3213
3214 for (i=0; i<length; i++) coeff[i]= 0.0;
3215
3216 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
3217 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
3218
3219 return vec;
3220 }
3221
3222 /* shift left / or right if "shift" is negative */
3223 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
3224 int length= a->length + FFABS(shift)*2;
3225 double *coeff= av_malloc(length*sizeof(double));
3226 int i;
3227 SwsVector *vec= av_malloc(sizeof(SwsVector));
3228
3229 vec->coeff= coeff;
3230 vec->length= length;
3231
3232 for (i=0; i<length; i++) coeff[i]= 0.0;
3233
3234 for (i=0; i<a->length; i++)
3235 {
3236 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
3237 }
3238
3239 return vec;
3240 }
3241
3242 void sws_shiftVec(SwsVector *a, int shift){
3243 SwsVector *shifted= sws_getShiftedVec(a, shift);
3244 av_free(a->coeff);
3245 a->coeff= shifted->coeff;
3246 a->length= shifted->length;
3247 av_free(shifted);
3248 }
3249
3250 void sws_addVec(SwsVector *a, SwsVector *b){
3251 SwsVector *sum= sws_sumVec(a, b);
3252 av_free(a->coeff);
3253 a->coeff= sum->coeff;
3254 a->length= sum->length;
3255 av_free(sum);
3256 }
3257
3258 void sws_subVec(SwsVector *a, SwsVector *b){
3259 SwsVector *diff= sws_diffVec(a, b);
3260 av_free(a->coeff);
3261 a->coeff= diff->coeff;
3262 a->length= diff->length;
3263 av_free(diff);
3264 }
3265
3266 void sws_convVec(SwsVector *a, SwsVector *b){
3267 SwsVector *conv= sws_getConvVec(a, b);
3268 av_free(a->coeff);
3269 a->coeff= conv->coeff;
3270 a->length= conv->length;
3271 av_free(conv);
3272 }
3273
3274 SwsVector *sws_cloneVec(SwsVector *a){
3275 double *coeff= av_malloc(a->length*sizeof(double));
3276 int i;
3277 SwsVector *vec= av_malloc(sizeof(SwsVector));
3278
3279 vec->coeff= coeff;
3280 vec->length= a->length;
3281
3282 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
3283
3284 return vec;
3285 }
3286
3287 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level){
3288 int i;
3289 double max=0;
3290 double min=0;
3291 double range;
3292
3293 for (i=0; i<a->length; i++)
3294 if (a->coeff[i]>max) max= a->coeff[i];
3295
3296 for (i=0; i<a->length; i++)
3297 if (a->coeff[i]<min) min= a->coeff[i];
3298
3299 range= max - min;
3300
3301 for (i=0; i<a->length; i++)
3302 {
3303 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
3304 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
3305 for (;x>0; x--) av_log(log_ctx, log_level, " ");
3306 av_log(log_ctx, log_level, "|\n");
3307 }
3308 }
3309
3310 #if LIBSWSCALE_VERSION_MAJOR < 1
3311 void sws_printVec(SwsVector *a){
3312 sws_printVec2(a, NULL, AV_LOG_DEBUG);
3313 }
3314 #endif
3315
3316 void sws_freeVec(SwsVector *a){
3317 if (!a) return;
3318 av_freep(&a->coeff);
3319 a->length=0;
3320 av_free(a);
3321 }
3322
3323 void sws_freeFilter(SwsFilter *filter){
3324 if (!filter) return;
3325
3326 if (filter->lumH) sws_freeVec(filter->lumH);
3327 if (filter->lumV) sws_freeVec(filter->lumV);
3328 if (filter->chrH) sws_freeVec(filter->chrH);
3329 if (filter->chrV) sws_freeVec(filter->chrV);
3330 av_free(filter);
3331 }
3332
3333
3334 void sws_freeContext(SwsContext *c){
3335 int i;
3336 if (!c) return;
3337
3338 if (c->lumPixBuf)
3339 {
3340 for (i=0; i<c->vLumBufSize; i++)
3341 av_freep(&c->lumPixBuf[i]);
3342 av_freep(&c->lumPixBuf);
3343 }
3344
3345 if (c->chrPixBuf)
3346 {
3347 for (i=0; i<c->vChrBufSize; i++)
3348 av_freep(&c->chrPixBuf[i]);
3349 av_freep(&c->chrPixBuf);
3350 }
3351
3352 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf){
3353 for (i=0; i<c->vLumBufSize; i++)
3354 av_freep(&c->alpPixBuf[i]);
3355 av_freep(&c->alpPixBuf);
3356 }
3357
3358 av_freep(&c->vLumFilter);
3359 av_freep(&c->vChrFilter);
3360 av_freep(&c->hLumFilter);
3361 av_freep(&c->hChrFilter);
3362 #if HAVE_ALTIVEC
3363 av_freep(&c->vYCoeffsBank);
3364 av_freep(&c->vCCoeffsBank);
3365 #endif
3366
3367 av_freep(&c->vLumFilterPos);
3368 av_freep(&c->vChrFilterPos);
3369 av_freep(&c->hLumFilterPos);
3370 av_freep(&c->hChrFilterPos);
3371
3372 #if ARCH_X86 && CONFIG_GPL
3373 #ifdef MAP_ANONYMOUS
3374 if (c->funnyYCode ) munmap(c->funnyYCode , MAX_FUNNY_CODE_SIZE);
3375 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
3376 #elif HAVE_VIRTUALALLOC
3377 if (c->funnyYCode ) VirtualFree(c->funnyYCode , MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3378 if (c->funnyUVCode) VirtualFree(c->funnyUVCode, MAX_FUNNY_CODE_SIZE, MEM_RELEASE);
3379 #else
3380 av_free(c->funnyYCode );
3381 av_free(c->funnyUVCode);
3382 #endif
3383 c->funnyYCode=NULL;
3384 c->funnyUVCode=NULL;
3385 #endif /* ARCH_X86 && CONFIG_GPL */
3386
3387 av_freep(&c->lumMmx2Filter);
3388 av_freep(&c->chrMmx2Filter);
3389 av_freep(&c->lumMmx2FilterPos);
3390 av_freep(&c->chrMmx2FilterPos);
3391 av_freep(&c->yuvTable);
3392
3393 av_free(c);
3394 }
3395
3396 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
3397 int srcW, int srcH, enum PixelFormat srcFormat,
3398 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
3399 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
3400<