Rearrange AV_[RW][BL]*() macros
[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
2017 /* sanity check */
2018 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
2019 {
2020 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2021 srcW, srcH, dstW, dstH);
2022 return NULL;
2023 }
8b2fce0d
MN
2024 if(srcW > VOFW || dstW > VOFW){
2025 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile time max width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2026 return NULL;
2027 }
221b804f
DB
2028
2029 if (!dstFilter) dstFilter= &dummyFilter;
2030 if (!srcFilter) srcFilter= &dummyFilter;
2031
2032 c= av_mallocz(sizeof(SwsContext));
2033
2034 c->av_class = &sws_context_class;
2035 c->srcW= srcW;
2036 c->srcH= srcH;
2037 c->dstW= dstW;
2038 c->dstH= dstH;
2039 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2040 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2041 c->flags= flags;
2042 c->dstFormat= dstFormat;
2043 c->srcFormat= srcFormat;
2044 c->vRounder= 4* 0x0001000100010001ULL;
2045
2046 usesHFilter= usesVFilter= 0;
1b0a4572
BF
2047 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2048 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2049 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2050 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2051 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2052 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2053 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2054 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
221b804f
DB
2055
2056 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2057 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2058
2059 // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
2060 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2061
2062 // drop some chroma lines if the user wants it
2063 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2064 c->chrSrcVSubSample+= c->vChrDrop;
2065
2066 // drop every 2. pixel for chroma calculation unless user wants full chroma
2067 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2068 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2069 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2070 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE)
2071 c->chrSrcHSubSample=1;
2072
2073 if (param){
2074 c->param[0] = param[0];
2075 c->param[1] = param[1];
2076 }else{
2077 c->param[0] =
2078 c->param[1] = SWS_PARAM_DEFAULT;
2079 }
2080
2081 c->chrIntHSubSample= c->chrDstHSubSample;
2082 c->chrIntVSubSample= c->chrSrcVSubSample;
2083
2084 // Note the -((-x)>>y) is so that we always round toward +inf.
2085 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2086 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2087 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2088 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2089
2090 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);
2091
2092 /* unscaled special Cases */
2093 if (unscaled && !usesHFilter && !usesVFilter)
2094 {
2095 /* yv12_to_nv12 */
2096 if (srcFormat == PIX_FMT_YUV420P && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2097 {
2098 c->swScale= PlanarToNV12Wrapper;
2099 }
9bde778e 2100#ifdef CONFIG_GPL
221b804f
DB
2101 /* yuv2bgr */
2102 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
2103 {
2104 c->swScale= yuv2rgb_get_func_ptr(c);
2105 }
9bde778e 2106#endif
6a4970ab 2107
e5091488 2108 if (srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P)
221b804f
DB
2109 {
2110 c->swScale= yvu9toyv12Wrapper;
2111 }
2112
2113 /* bgr24toYV12 */
2114 if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P)
2115 c->swScale= bgr24toyv12Wrapper;
2116
2117 /* rgb/bgr -> rgb/bgr (no dither needed forms) */
2118 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2119 && (isBGR(dstFormat) || isRGB(dstFormat))
2120 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2121 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2122 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2123 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2124 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2125 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2126 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2127 && !needsDither)
2128 c->swScale= rgb2rgbWrapper;
2129
2130 /* LQ converters if -sws 0 or -sws 4*/
2131 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2132 /* rgb/bgr -> rgb/bgr (dither needed forms) */
2133 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2134 && (isBGR(dstFormat) || isRGB(dstFormat))
2135 && needsDither)
2136 c->swScale= rgb2rgbWrapper;
2137
2138 /* yv12_to_yuy2 */
2139 if (srcFormat == PIX_FMT_YUV420P &&
2140 (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422))
2141 {
2142 if (dstFormat == PIX_FMT_YUYV422)
2143 c->swScale= PlanarToYuy2Wrapper;
2144 else
2145 c->swScale= PlanarToUyvyWrapper;
2146 }
2147 }
ec22603f 2148
6634d0ef 2149#ifdef COMPILE_ALTIVEC
221b804f
DB
2150 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2151 ((srcFormat == PIX_FMT_YUV420P &&
2152 (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422)))) {
2153 // unscaled YV12 -> packed YUV, we want speed
2154 if (dstFormat == PIX_FMT_YUYV422)
2155 c->swScale= yv12toyuy2_unscaled_altivec;
2156 else
2157 c->swScale= yv12touyvy_unscaled_altivec;
2158 }
b71cf33c
RD
2159#endif
2160
221b804f
DB
2161 /* simple copy */
2162 if ( srcFormat == dstFormat
2163 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
e5091488 2164 || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
221b804f 2165 {
2d35ae56
LB
2166 if (isPacked(c->srcFormat))
2167 c->swScale= packedCopy;
2168 else /* Planar YUV or gray */
2169 c->swScale= planarCopy;
221b804f
DB
2170 }
2171
2172 /* gray16{le,be} conversions */
2173 if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2174 {
2175 c->swScale= gray16togray;
2176 }
2177 if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2178 {
2179 c->swScale= graytogray16;
2180 }
2181 if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2182 {
2183 c->swScale= gray16swap;
2184 }
2185
1ebbfe15
MH
2186#ifdef ARCH_BFIN
2187 if (flags & SWS_CPU_CAPS_BFIN)
2188 ff_bfin_get_unscaled_swscale (c);
2189#endif
2190
221b804f
DB
2191 if (c->swScale){
2192 if (flags&SWS_PRINT_INFO)
4b0c30b7 2193 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
221b804f
DB
2194 sws_format_name(srcFormat), sws_format_name(dstFormat));
2195 return c;
2196 }
2197 }
2198
2199 if (flags & SWS_CPU_CAPS_MMX2)
2200 {
2201 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2202 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2203 {
2204 if (flags&SWS_PRINT_INFO)
4b0c30b7 2205 av_log(c, AV_LOG_INFO, "output Width is not a multiple of 32 -> no MMX2 scaler\n");
221b804f
DB
2206 }
2207 if (usesHFilter) c->canMMX2BeUsed=0;
2208 }
2209 else
2210 c->canMMX2BeUsed=0;
2211
2212 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2213 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2214
2215 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2216 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2217 // n-2 is the last chrominance sample available
86bdf3fd 2218 // this is not perfect, but no one should notice the difference, the more correct variant
221b804f
DB
2219 // would be like the vertical one, but that would require some special code for the
2220 // first and last pixel
2221 if (flags&SWS_FAST_BILINEAR)
2222 {
2223 if (c->canMMX2BeUsed)
2224 {
2225 c->lumXInc+= 20;
2226 c->chrXInc+= 20;
2227 }
2228 //we don't use the x86asm scaler if mmx is available
2229 else if (flags & SWS_CPU_CAPS_MMX)
2230 {
2231 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2232 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2233 }
2234 }
2235
2236 /* precalculate horizontal scaler filter coefficients */
2237 {
2238 const int filterAlign=
2239 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2240 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2241 1;
2242
2243 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2244 srcW , dstW, filterAlign, 1<<14,
2245 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2246 srcFilter->lumH, dstFilter->lumH, c->param);
2247 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2248 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2249 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2250 srcFilter->chrH, dstFilter->chrH, c->param);
28bf81c9 2251
dbdae6ec 2252#define MAX_FUNNY_CODE_SIZE 10000
17c613ef 2253#if defined(COMPILE_MMX2)
77a416e8 2254// can't downscale !!!
221b804f
DB
2255 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2256 {
113ef149 2257#ifdef MAP_ANONYMOUS
221b804f
DB
2258 c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2259 c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
38d5c282 2260#else
221b804f
DB
2261 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2262 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
38d5c282
AJ
2263#endif
2264
221b804f
DB
2265 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2266 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2267 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2268 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
b7dc6f66 2269
221b804f
DB
2270 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2271 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2272 }
17c613ef 2273#endif /* defined(COMPILE_MMX2) */
221b804f 2274 } // Init Horizontal stuff
28bf81c9
MN
2275
2276
2277
221b804f
DB
2278 /* precalculate vertical scaler filter coefficients */
2279 {
2280 const int filterAlign=
2281 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2282 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2283 1;
8c266f0c 2284
221b804f
DB
2285 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2286 srcH , dstH, filterAlign, (1<<12)-4,
2287 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2288 srcFilter->lumV, dstFilter->lumV, c->param);
2289 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2290 c->chrSrcH, c->chrDstH, filterAlign, (1<<12)-4,
2291 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2292 srcFilter->chrV, dstFilter->chrV, c->param);
d33d485e
AC
2293
2294#ifdef HAVE_ALTIVEC
221b804f
DB
2295 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2296 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2297
2298 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2299 int j;
2300 short *p = (short *)&c->vYCoeffsBank[i];
2301 for (j=0;j<8;j++)
2302 p[j] = c->vLumFilter[i];
2303 }
2304
2305 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2306 int j;
2307 short *p = (short *)&c->vCCoeffsBank[i];
2308 for (j=0;j<8;j++)
2309 p[j] = c->vChrFilter[i];
2310 }
d33d485e 2311#endif
221b804f
DB
2312 }
2313
2314 // Calculate Buffer Sizes so that they won't run out while handling these damn slices
2315 c->vLumBufSize= c->vLumFilterSize;
2316 c->vChrBufSize= c->vChrFilterSize;
2317 for (i=0; i<dstH; i++)
2318 {
2319 int chrI= i*c->chrDstH / dstH;
2320 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2321 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2322
2323 nextSlice>>= c->chrSrcVSubSample;
2324 nextSlice<<= c->chrSrcVSubSample;
2325 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
e5091488 2326 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
221b804f
DB
2327 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2328 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2329 }
2330
2331 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2332 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2333 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2334 //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)
2335 /* align at 16 bytes for AltiVec */
2336 for (i=0; i<c->vLumBufSize; i++)
8b2fce0d 2337 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
221b804f 2338 for (i=0; i<c->vChrBufSize; i++)
8b2fce0d 2339 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
221b804f
DB
2340
2341 //try to avoid drawing green stuff between the right end and the stride end
8b2fce0d
MN
2342 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2343
5a352b14 2344 assert(2*VOFW == VOF);
221b804f 2345
fcc402b1 2346 assert(c->chrDstH <= dstH);
221b804f
DB
2347
2348 if (flags&SWS_PRINT_INFO)
2349 {
28bf81c9 2350#ifdef DITHER1XBPP
3e62b7e3 2351 const char *dither= " dithered";
5521b193 2352#else
3e62b7e3 2353 const char *dither= "";
28bf81c9 2354#endif
221b804f 2355 if (flags&SWS_FAST_BILINEAR)
4b0c30b7 2356 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
221b804f 2357 else if (flags&SWS_BILINEAR)
4b0c30b7 2358 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
221b804f 2359 else if (flags&SWS_BICUBIC)
4b0c30b7 2360 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
221b804f 2361 else if (flags&SWS_X)
4b0c30b7 2362 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
221b804f 2363 else if (flags&SWS_POINT)
4b0c30b7 2364 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
221b804f 2365 else if (flags&SWS_AREA)
4b0c30b7 2366 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
221b804f 2367 else if (flags&SWS_BICUBLIN)
4b0c30b7 2368 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
221b804f 2369 else if (flags&SWS_GAUSS)
4b0c30b7 2370 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
221b804f 2371 else if (flags&SWS_SINC)
4b0c30b7 2372 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
221b804f 2373 else if (flags&SWS_LANCZOS)
4b0c30b7 2374 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
221b804f 2375 else if (flags&SWS_SPLINE)
4b0c30b7 2376 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
221b804f 2377 else
4b0c30b7 2378 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
221b804f
DB
2379
2380 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2381 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2382 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2383 else
2384 av_log(c, AV_LOG_INFO, "from %s to %s ",
2385 sws_format_name(srcFormat), sws_format_name(dstFormat));
2386
2387 if (flags & SWS_CPU_CAPS_MMX2)
2388 av_log(c, AV_LOG_INFO, "using MMX2\n");
2389 else if (flags & SWS_CPU_CAPS_3DNOW)
2390 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2391 else if (flags & SWS_CPU_CAPS_MMX)
2392 av_log(c, AV_LOG_INFO, "using MMX\n");
2393 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2394 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2395 else
2396 av_log(c, AV_LOG_INFO, "using C\n");
2397 }
2398
2399 if (flags & SWS_PRINT_INFO)
2400 {
2401 if (flags & SWS_CPU_CAPS_MMX)
2402 {
2403 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
4b0c30b7 2404 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
221b804f
DB
2405 else
2406 {
2407 if (c->hLumFilterSize==4)
4b0c30b7 2408 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
221b804f 2409 else if (c->hLumFilterSize==8)
4b0c30b7 2410 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
221b804f 2411 else
4b0c30b7 2412 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
221b804f
DB
2413
2414 if (c->hChrFilterSize==4)
4b0c30b7 2415 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
221b804f 2416 else if (c->hChrFilterSize==8)
4b0c30b7 2417 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
221b804f 2418 else
4b0c30b7 2419 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
221b804f
DB
2420 }
2421 }
2422 else
2423 {
3d6a30d9 2424#if defined(ARCH_X86)
4b0c30b7 2425 av_log(c, AV_LOG_VERBOSE, "using X86-Asm scaler for horizontal scaling\n");
28bf81c9 2426#else
221b804f 2427 if (flags & SWS_FAST_BILINEAR)
4b0c30b7 2428 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
221b804f 2429 else
4b0c30b7 2430 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
28bf81c9 2431#endif
221b804f
DB
2432 }
2433 if (isPlanarYUV(dstFormat))
2434 {
2435 if (c->vLumFilterSize==1)
4b0c30b7 2436 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 2437 else
4b0c30b7 2438 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
2439 }
2440 else
2441 {
2442 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
4b0c30b7
BC
2443 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2444 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2445 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
4b0c30b7 2446 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 2447 else
4b0c30b7 2448 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
2449 }
2450
2451 if (dstFormat==PIX_FMT_BGR24)
4b0c30b7 2452 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 Converter\n",
221b804f
DB
2453 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2454 else if (dstFormat==PIX_FMT_RGB32)
4b0c30b7 2455 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2456 else if (dstFormat==PIX_FMT_BGR565)
4b0c30b7 2457 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2458 else if (dstFormat==PIX_FMT_BGR555)
4b0c30b7 2459 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2460
4b0c30b7 2461 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
221b804f
DB
2462 }
2463 if (flags & SWS_PRINT_INFO)
2464 {
4b0c30b7 2465 av_log(c, AV_LOG_DEBUG, "Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
221b804f 2466 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
4b0c30b7 2467 av_log(c, AV_LOG_DEBUG, "Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
221b804f
DB
2468 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2469 }
2470
2471 c->swScale= getSwsFunc(flags);
2472 return c;
28bf81c9
MN
2473}
2474
2475/**
56b69633 2476 * swscale wrapper, so we don't need to export the SwsContext.
fccb9b2b
MN
2477 * assumes planar YUV to be in YUV order instead of YVU
2478 */
703b56fb 2479int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2480 int srcSliceH, uint8_t* dst[], int dstStride[]){
2481 int i;
2482 uint8_t* src2[4]= {src[0], src[1], src[2]};
2483 uint32_t pal[256];
2484 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3f0bc115 2485 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
221b804f
DB
2486 return 0;
2487 }
2488 if (c->sliceDir == 0) {
2489 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2490 }
2491
2492 if (c->srcFormat == PIX_FMT_PAL8){
2493 for (i=0; i<256; i++){
2494 int p= ((uint32_t*)(src[1]))[i];
2495 int r= (p>>16)&0xFF;
2496 int g= (p>> 8)&0xFF;
2497 int b= p &0xFF;
2498 int y= av_clip_uint8(((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16 );
2499 int u= av_clip_uint8(((RU*r + GU*g + BU*b)>>RGB2YUV_SHIFT) + 128);
2500 int v= av_clip_uint8(((RV*r + GV*g + BV*b)>>RGB2YUV_SHIFT) + 128);
2501 pal[i]= y + (u<<8) + (v<<16);
21c08a3f 2502 }
74498eb4 2503 src2[1]= (uint8_t*)pal;
221b804f 2504 }
21c08a3f 2505
221b804f
DB
2506 // copy strides, so they can safely be modified
2507 if (c->sliceDir == 1) {
2508 // slices go from top to bottom
2509 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2]};
2510 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2]};
2511 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2512 } else {
2513 // slices go from bottom to top => we flip the image internally
2514 uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
2515 dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
2516 dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
2517 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2]};
2518 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2]};
2519
2520 src2[0] += (srcSliceH-1)*srcStride[0];
2521 if (c->srcFormat != PIX_FMT_PAL8)
2522 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2523 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2524
2525 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2526 }
fccb9b2b
MN
2527}
2528
2529/**
56b69633 2530 * swscale wrapper, so we don't need to export the SwsContext
d4e24275 2531 */
703b56fb 2532int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2533 int srcSliceH, uint8_t* dst[], int dstStride[]){
2534 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
d4e24275 2535}
d4e24275 2536
6a4970ab 2537SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
221b804f
DB
2538 float lumaSharpen, float chromaSharpen,
2539 float chromaHShift, float chromaVShift,
2540 int verbose)
e21206a8 2541{
221b804f
DB
2542 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2543
2544 if (lumaGBlur!=0.0){
2545 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2546 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2547 }else{
2548 filter->lumH= sws_getIdentityVec();
2549 filter->lumV= sws_getIdentityVec();
2550 }
2551
2552 if (chromaGBlur!=0.0){
2553 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2554 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2555 }else{
2556 filter->chrH= sws_getIdentityVec();
2557 filter->chrV= sws_getIdentityVec();
2558 }
2559
2560 if (chromaSharpen!=0.0){
2561 SwsVector *id= sws_getIdentityVec();
2562 sws_scaleVec(filter->chrH, -chromaSharpen);
2563 sws_scaleVec(filter->chrV, -chromaSharpen);
2564 sws_addVec(filter->chrH, id);
2565 sws_addVec(filter->chrV, id);
2566 sws_freeVec(id);
2567 }
2568
2569 if (lumaSharpen!=0.0){
2570 SwsVector *id= sws_getIdentityVec();
2571 sws_scaleVec(filter->lumH, -lumaSharpen);
2572 sws_scaleVec(filter->lumV, -lumaSharpen);
2573 sws_addVec(filter->lumH, id);
2574 sws_addVec(filter->lumV, id);
2575 sws_freeVec(id);
2576 }
2577
2578 if (chromaHShift != 0.0)
2579 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2580
2581 if (chromaVShift != 0.0)
2582 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2583
2584 sws_normalizeVec(filter->chrH, 1.0);
2585 sws_normalizeVec(filter->chrV, 1.0);
2586 sws_normalizeVec(filter->lumH, 1.0);
2587 sws_normalizeVec(filter->lumV, 1.0);
2588
2589 if (verbose) sws_printVec(filter->chrH);
2590 if (verbose) sws_printVec(filter->lumH);
2591
2592 return filter;
e21206a8
MN
2593}
2594
d4e24275 2595/**
28bf81c9
MN
2596 * returns a normalized gaussian curve used to filter stuff
2597 * quality=3 is high quality, lowwer is lowwer quality
2598 */
d4e24275 2599SwsVector *sws_getGaussianVec(double variance, double quality){
221b804f
DB
2600 const int length= (int)(variance*quality + 0.5) | 1;
2601 int i;
2602 double *coeff= av_malloc(length*sizeof(double));
2603 double middle= (length-1)*0.5;
2604 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2605
221b804f
DB
2606 vec->coeff= coeff;
2607 vec->length= length;
28bf81c9 2608
221b804f
DB
2609 for (i=0; i<length; i++)
2610 {
2611 double dist= i-middle;
e5091488 2612 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
221b804f 2613 }
28bf81c9 2614
221b804f 2615 sws_normalizeVec(vec, 1.0);
c7f822d9 2616
221b804f 2617 return vec;
28bf81c9
MN
2618}
2619
d4e24275 2620SwsVector *sws_getConstVec(double c, int length){
221b804f
DB
2621 int i;
2622 double *coeff= av_malloc(length*sizeof(double));
2623 SwsVector *vec= av_malloc(sizeof(SwsVector));
5521b193 2624
221b804f
DB
2625 vec->coeff= coeff;
2626 vec->length= length;
5521b193 2627
221b804f
DB
2628 for (i=0; i<length; i++)
2629 coeff[i]= c;
5521b193 2630
221b804f 2631 return vec;
5521b193
MN
2632}
2633
2634
d4e24275 2635SwsVector *sws_getIdentityVec(void){
221b804f 2636 return sws_getConstVec(1.0, 1);
c7f822d9
MN
2637}
2638
2e728364 2639double sws_dcVec(SwsVector *a){
221b804f
DB
2640 int i;
2641 double sum=0;
28bf81c9 2642
221b804f
DB
2643 for (i=0; i<a->length; i++)
2644 sum+= a->coeff[i];
28bf81c9 2645
221b804f 2646 return sum;
28bf81c9
MN
2647}
2648
d4e24275 2649void sws_scaleVec(SwsVector *a, double scalar){
221b804f 2650 int i;
c7f822d9 2651
221b804f
DB
2652 for (i=0; i<a->length; i++)
2653 a->coeff[i]*= scalar;
c7f822d9
MN
2654}
2655
2e728364 2656void sws_normalizeVec(SwsVector *a, double height){
221b804f 2657 sws_scaleVec(a, height/sws_dcVec(a));
2e728364
MN
2658}
2659
d4e24275 2660static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
221b804f
DB
2661 int length= a->length + b->length - 1;
2662 double *coeff= av_malloc(length*sizeof(double));
2663 int i, j;
2664 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2665
221b804f
DB
2666 vec->coeff= coeff;
2667 vec->length= length;
28bf81c9 2668
221b804f 2669 for (i=0; i<length; i++) coeff[i]= 0.0;
28bf81c9 2670
221b804f
DB
2671 for (i=0; i<a->length; i++)
2672 {
2673 for (j=0; j<b->length; j++)
2674 {
2675 coeff[i+j]+= a->coeff[i]*b->coeff[j];
2676 }
2677 }
28bf81c9 2678
221b804f 2679 return vec;
28bf81c9
MN
2680}
2681
d4e24275 2682static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
221b804f
DB
2683 int length= FFMAX(a->length, b->length);
2684 double *coeff= av_malloc(length*sizeof(double));
2685 int i;
2686 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2687
221b804f
DB
2688 vec->coeff= coeff;
2689 vec->length= length;
28bf81c9 2690
221b804f 2691 for (i=0; i<length; i++) coeff[i]= 0.0;
28bf81c9 2692
221b804f
DB
2693 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2694 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
c7f822d9 2695
221b804f 2696 return vec;
28bf81c9 2697}
c7f822d9 2698
d4e24275 2699static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
221b804f
DB
2700 int length= FFMAX(a->length, b->length);
2701 double *coeff= av_malloc(length*sizeof(double));
2702 int i;
2703 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2704
221b804f
DB
2705 vec->coeff= coeff;
2706 vec->length= length;
c7f822d9 2707
221b804f 2708 for (i=0; i<length; i++) coeff[i]= 0.0;
c7f822d9 2709
221b804f
DB
2710 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2711 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
c7f822d9 2712
221b804f 2713 return vec;
c7f822d9
MN
2714}
2715
2716/* shift left / or right if "shift" is negative */
d4e24275 2717static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
221b804f
DB
2718 int length= a->length + FFABS(shift)*2;
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++)
2729 {
2730 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2731 }
c7f822d9 2732
221b804f 2733 return vec;
c7f822d9
MN
2734}
2735
d4e24275 2736void sws_shiftVec(SwsVector *a, int shift){
221b804f
DB
2737 SwsVector *shifted= sws_getShiftedVec(a, shift);
2738 av_free(a->coeff);
2739 a->coeff= shifted->coeff;
2740 a->length= shifted->length;
2741 av_free(shifted);
5cebb24b
MN
2742}
2743
d4e24275 2744void sws_addVec(SwsVector *a, SwsVector *b){
221b804f
DB
2745 SwsVector *sum= sws_sumVec(a, b);
2746 av_free(a->coeff);
2747 a->coeff= sum->coeff;
2748 a->length= sum->length;
2749 av_free(sum);
5cebb24b
MN
2750}
2751
d4e24275 2752void sws_subVec(SwsVector *a, SwsVector *b){
221b804f
DB
2753 SwsVector *diff= sws_diffVec(a, b);
2754 av_free(a->coeff);
2755 a->coeff= diff->coeff;
2756 a->length= diff->length;
2757 av_free(diff);
5cebb24b
MN
2758}
2759
d4e24275 2760void sws_convVec(SwsVector *a, SwsVector *b){
221b804f
DB
2761 SwsVector *conv= sws_getConvVec(a, b);
2762 av_free(a->coeff);
2763 a->coeff= conv->coeff;
2764 a->length= conv->length;
2765 av_free(conv);
5cebb24b
MN
2766}
2767
d4e24275 2768SwsVector *sws_cloneVec(SwsVector *a){
221b804f
DB
2769 double *coeff= av_malloc(a->length*sizeof(double));
2770 int i;
2771 SwsVector *vec= av_malloc(sizeof(SwsVector));
5cebb24b 2772
221b804f
DB
2773 vec->coeff= coeff;
2774 vec->length= a->length;
5cebb24b 2775
221b804f 2776 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
5cebb24b 2777
221b804f 2778 return vec;
5cebb24b
MN
2779}
2780
d4e24275 2781void sws_printVec(SwsVector *a){
221b804f
DB
2782 int i;
2783 double max=0;
2784 double min=0;
2785 double range;
2786
2787 for (i=0; i<a->length; i++)
2788 if (a->coeff[i]>max) max= a->coeff[i];
2789
2790 for (i=0; i<a->length; i++)
2791 if (a->coeff[i]<min) min= a->coeff[i];
2792
2793 range= max - min;
2794
2795 for (i=0; i<a->length; i++)
2796 {
2797 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
2798 av_log(NULL, AV_LOG_DEBUG, "%1.3f ", a->coeff[i]);
2799 for (;x>0; x--) av_log(NULL, AV_LOG_DEBUG, " ");
2800 av_log(NULL, AV_LOG_DEBUG, "|\n");
2801 }
c7f822d9
MN
2802}
2803
d4e24275 2804void sws_freeVec(SwsVector *a){
221b804f
DB
2805 if (!a) return;
2806 av_free(a->coeff);
2807 a->coeff=NULL;
2808 a->length=0;
2809 av_free(a);
c7f822d9
MN
2810}
2811
e21206a8 2812void sws_freeFilter(SwsFilter *filter){
221b804f 2813 if (!filter) return;
e21206a8 2814
221b804f
DB
2815 if (filter->lumH) sws_freeVec(filter->lumH);
2816 if (filter->lumV) sws_freeVec(filter->lumV);
2817 if (filter->chrH) sws_freeVec(filter->chrH);
2818 if (filter->chrV) sws_freeVec(filter->chrV);
2819 av_free(filter);
e21206a8
MN
2820}
2821
2822
d4e24275 2823void sws_freeContext(SwsContext *c){
221b804f
DB
2824 int i;
2825 if (!c) return;
2826
2827 if (c->lumPixBuf)
2828 {
2829 for (i=0; i<c->vLumBufSize; i++)
2830 {
2831 av_free(c->lumPixBuf[i]);
2832 c->lumPixBuf[i]=NULL;
2833 }
2834 av_free(c->lumPixBuf);
2835 c->lumPixBuf=NULL;
2836 }
2837
2838 if (c->chrPixBuf)
2839 {
2840 for (i=0; i<c->vChrBufSize; i++)
2841 {
2842 av_free(c->chrPixBuf[i]);
2843 c->chrPixBuf[i]=NULL;
2844 }
2845 av_free(c->chrPixBuf);
2846 c->chrPixBuf=NULL;
2847 }
2848
2849 av_free(c->vLumFilter);
2850 c->vLumFilter = NULL;
2851 av_free(c->vChrFilter);
2852 c->vChrFilter = NULL;
2853 av_free(c->hLumFilter);
2854 c->hLumFilter = NULL;
2855 av_free(c->hChrFilter);
2856 c->hChrFilter = NULL;
d33d485e 2857#ifdef HAVE_ALTIVEC
221b804f
DB
2858 av_free(c->vYCoeffsBank);
2859 c->vYCoeffsBank = NULL;
2860 av_free(c->vCCoeffsBank);
2861 c->vCCoeffsBank = NULL;
d33d485e 2862#endif
c7f822d9 2863
221b804f
DB
2864 av_free(c->vLumFilterPos);
2865 c->vLumFilterPos = NULL;
2866 av_free(c->vChrFilterPos);
2867 c->vChrFilterPos = NULL;
2868 av_free(c->hLumFilterPos);
2869 c->hLumFilterPos = NULL;
2870 av_free(c->hChrFilterPos);
2871 c->hChrFilterPos = NULL;
c7f822d9 2872
9bde778e 2873#if defined(ARCH_X86) && defined(CONFIG_GPL)
0ce5275d 2874#ifdef MAP_ANONYMOUS
221b804f
DB
2875 if (c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
2876 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
38d5c282 2877#else
221b804f
DB
2878 av_free(c->funnyYCode);
2879 av_free(c->funnyUVCode);
38d5c282 2880#endif
221b804f
DB
2881 c->funnyYCode=NULL;
2882 c->funnyUVCode=NULL;
3d6a30d9 2883#endif /* defined(ARCH_X86) */
38d5c282 2884
221b804f
DB
2885 av_free(c->lumMmx2Filter);
2886 c->lumMmx2Filter=NULL;
2887 av_free(c->chrMmx2Filter);
2888 c->chrMmx2Filter=NULL;
2889 av_free(c->lumMmx2FilterPos);
2890 c->lumMmx2FilterPos=NULL;
2891 av_free(c->chrMmx2FilterPos);
2892 c->chrMmx2FilterPos=NULL;
2893 av_free(c->yuvTable);
2894 c->yuvTable=NULL;
2895
2896 av_free(c);
c7f822d9
MN
2897}
2898
22e46959
VP
2899/**
2900 * Checks if context is valid or reallocs a new one instead.
2901 * If context is NULL, just calls sws_getContext() to get a new one.
2902 * Otherwise, checks if the parameters are the same already saved in context.
2903 * If that is the case, returns the current context.
2904 * Otherwise, frees context and gets a new one.
2905 *
2906 * Be warned that srcFilter, dstFilter are not checked, they are
2907 * asumed to remain valid.
2908 */
2909struct SwsContext *sws_getCachedContext(struct SwsContext *context,
221b804f
DB
2910 int srcW, int srcH, int srcFormat,
2911 int dstW, int dstH, int dstFormat, int flags,
2912 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
22e46959 2913{
337a2a45
KMH
2914 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
2915
2916 if (!param)
2917 param = default_param;
2918
1b0a4572 2919 if (context) {
b8db4e22
BF
2920 if (context->srcW != srcW || context->srcH != srcH ||
2921 context->srcFormat != srcFormat ||
2922 context->dstW != dstW || context->dstH != dstH ||
2923 context->dstFormat != dstFormat || context->flags != flags ||
2924 context->param[0] != param[0] || context->param[1] != param[1])
22e46959
VP
2925 {
2926 sws_freeContext(context);
2927 context = NULL;
2928 }
2929 }
1b0a4572 2930 if (!context) {
22e46959 2931 return sws_getContext(srcW, srcH, srcFormat,
221b804f
DB
2932 dstW, dstH, dstFormat, flags,
2933 srcFilter, dstFilter, param);
22e46959
VP
2934 }
2935 return context;
2936}
2937