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