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