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