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