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