general convolution filtering of the source picture
[libav.git] / postproc / swscale.c
CommitLineData
31190492
A
1
2// Software scaling and colorspace conversion routines for MPlayer
3
afa569af 4// Orginal C implementation by A'rpi/ESP-team <arpi@thot.banki.hu>
783e9cc9 5// current version mostly by Michael Niedermayer (michaelni@gmx.at)
d604bab9 6// the parts written by michael are under GNU GPL
783e9cc9 7
28bf81c9
MN
8/*
9 supported Input formats: YV12 (grayscale soon too)
10 supported output formats: YV12, BGR15, BGR16, BGR24, BGR32 (grayscale soon too)
11*/
12
d3f41512 13#include <inttypes.h>
dda87e9f 14#include <string.h>
077ea8a7 15#include <math.h>
c1b0bfb4 16#include <stdio.h>
d3f41512 17#include "../config.h"
9b464428 18#include "../mangle.h"
c1b0bfb4
MN
19#ifdef HAVE_MALLOC_H
20#include <malloc.h>
21#endif
d604bab9 22#include "swscale.h"
7630f2e0 23#include "../cpudetect.h"
28bf81c9 24#include "../libvo/img_format.h"
541c4eb9 25#undef MOVNTQ
7d7f78b5 26#undef PAVGB
d3f41512 27
783e9cc9 28//#undef HAVE_MMX2
7f56a527 29//#define HAVE_3DNOW
d3f41512 30//#undef HAVE_MMX
783e9cc9 31//#undef ARCH_X86
d604bab9 32#define DITHER1XBPP
d3f41512
MN
33
34#define RET 0xC3 //near return opcode
c1b0bfb4 35
28bf81c9
MN
36#ifdef MP_DEBUG
37#define ASSERT(x) if(!(x)) { printf("ASSERT " #x " failed\n"); *((int*)0)=0; }
38#else
c1b0bfb4 39#define ASSERT(x) ;
28bf81c9
MN
40#endif
41
42#ifdef M_PI
43#define PI M_PI
44#else
45#define PI 3.14159265358979323846
46#endif
c1b0bfb4 47
e3d2500f 48extern int verbose; // defined in mplayer.c
783e9cc9
MN
49/*
50NOTES
d3f41512 51
d604bab9 52known BUGS with known cause (no bugreports please!, but patches are welcome :) )
e3d2500f 53horizontal fast_bilinear MMX2 scaler reads 1-7 samples too much (might cause a sig11)
d604bab9 54
e3d2500f 55Supported output formats BGR15 BGR16 BGR24 BGR32 YV12
d604bab9
MN
56BGR15 & BGR16 MMX verions support dithering
57Special versions: fast Y 1:1 scaling (no interpolation in y direction)
31190492 58
783e9cc9 59TODO
d604bab9 60more intelligent missalignment avoidance for the horizontal scaler
02a0a992
MN
61dither in C
62change the distance of the u & v buffer
c1b0bfb4
MN
63Move static / global vars into a struct so multiple scalers can be used
64write special vertical cubic upscale version
65Optimize C code (yv12 / minmax)
783e9cc9 66*/
31190492 67
d604bab9 68#define ABS(a) ((a) > 0 ? (a) : (-(a)))
2ff198c1
MN
69#define MIN(a,b) ((a) > (b) ? (b) : (a))
70#define MAX(a,b) ((a) < (b) ? (b) : (a))
d604bab9 71
7630f2e0
MN
72#ifdef ARCH_X86
73#define CAN_COMPILE_X86_ASM
d604bab9
MN
74#endif
75
7630f2e0 76#ifdef CAN_COMPILE_X86_ASM
d604bab9 77static uint64_t __attribute__((aligned(8))) yCoeff= 0x2568256825682568LL;
390b20a6
MN
78static uint64_t __attribute__((aligned(8))) vrCoeff= 0x3343334333433343LL;
79static uint64_t __attribute__((aligned(8))) ubCoeff= 0x40cf40cf40cf40cfLL;
80static uint64_t __attribute__((aligned(8))) vgCoeff= 0xE5E2E5E2E5E2E5E2LL;
81static uint64_t __attribute__((aligned(8))) ugCoeff= 0xF36EF36EF36EF36ELL;
f62255fb
MN
82static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
83static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
d604bab9
MN
84static uint64_t __attribute__((aligned(8))) w400= 0x0400040004000400LL;
85static uint64_t __attribute__((aligned(8))) w80= 0x0080008000800080LL;
86static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
077ea8a7 87static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
d604bab9
MN
88static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
89static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
90static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
91
3fe8e8f0
MN
92static volatile uint64_t __attribute__((aligned(8))) b5Dither;
93static volatile uint64_t __attribute__((aligned(8))) g5Dither;
94static volatile uint64_t __attribute__((aligned(8))) g6Dither;
95static volatile uint64_t __attribute__((aligned(8))) r5Dither;
d8fa3c54
MN
96
97static uint64_t __attribute__((aligned(8))) dither4[2]={
98 0x0103010301030103LL,
99 0x0200020002000200LL,};
100
101static uint64_t __attribute__((aligned(8))) dither8[2]={
102 0x0602060206020602LL,
103 0x0004000400040004LL,};
d604bab9
MN
104
105static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
106static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
107static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
108static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
109static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
110static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
111
99d2cb72
MN
112static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
113static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
114static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
115
28bf81c9 116// FIXME remove
d604bab9
MN
117static uint64_t __attribute__((aligned(8))) asm_yalpha1;
118static uint64_t __attribute__((aligned(8))) asm_uvalpha1;
d604bab9 119#endif
783e9cc9
MN
120
121// clipping helper table for C implementations:
122static unsigned char clip_table[768];
123
b18ea156
MN
124static unsigned short clip_table16b[768];
125static unsigned short clip_table16g[768];
126static unsigned short clip_table16r[768];
127static unsigned short clip_table15b[768];
128static unsigned short clip_table15g[768];
129static unsigned short clip_table15r[768];
130
783e9cc9
MN
131// yuv->rgb conversion tables:
132static int yuvtab_2568[256];
133static int yuvtab_3343[256];
134static int yuvtab_0c92[256];
135static int yuvtab_1a1e[256];
136static int yuvtab_40cf[256];
c1b0bfb4
MN
137// Needed for cubic scaler to catch overflows
138static int clip_yuvtab_2568[768];
139static int clip_yuvtab_3343[768];
140static int clip_yuvtab_0c92[768];
141static int clip_yuvtab_1a1e[768];
142static int clip_yuvtab_40cf[768];
143
28bf81c9 144//global sws_flags from the command line
077ea8a7
MN
145int sws_flags=0;
146
28bf81c9
MN
147/* cpuCaps combined from cpudetect and whats actually compiled in
148 (if there is no support for something compiled in it wont appear here) */
149static CpuCaps cpuCaps;
d3f41512 150
28bf81c9
MN
151void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
152 int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
2ff198c1 153
7630f2e0 154#ifdef CAN_COMPILE_X86_ASM
96034638
MN
155void in_asm_used_var_warning_killer()
156{
077ea8a7 157 volatile int i= yCoeff+vrCoeff+ubCoeff+vgCoeff+ugCoeff+bF8+bFC+w400+w80+w10+
28bf81c9
MN
158 bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+asm_yalpha1+ asm_uvalpha1+
159 M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0];
96034638
MN
160 if(i) i=0;
161}
162#endif
d604bab9 163
e3d2500f
MN
164static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
165 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
166 uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW)
167{
168 //FIXME Optimize (just quickly writen not opti..)
169 int i;
170 for(i=0; i<dstW; i++)
171 {
172 int val=0;
173 int j;
174 for(j=0; j<lumFilterSize; j++)
175 val += lumSrc[j][i] * lumFilter[j];
176
177 dest[i]= MIN(MAX(val>>19, 0), 255);
178 }
179
180 if(uDest != NULL)
181 for(i=0; i<(dstW>>1); i++)
182 {
183 int u=0;
184 int v=0;
185 int j;
627690b5 186 for(j=0; j<chrFilterSize; j++)
e3d2500f
MN
187 {
188 u += chrSrc[j][i] * chrFilter[j];
189 v += chrSrc[j][i + 2048] * chrFilter[j];
190 }
191
192 uDest[i]= MIN(MAX(u>>19, 0), 255);
193 vDest[i]= MIN(MAX(v>>19, 0), 255);
194 }
195}
196
197static inline void yuv2rgbXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
198 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
28bf81c9 199 uint8_t *dest, int dstW, int dstFormat)
e3d2500f 200{
28bf81c9 201 if(dstFormat==IMGFMT_BGR32)
e3d2500f
MN
202 {
203 int i;
204 for(i=0; i<(dstW>>1); i++){
205 int j;
206 int Y1=0;
207 int Y2=0;
208 int U=0;
209 int V=0;
210 int Cb, Cr, Cg;
211 for(j=0; j<lumFilterSize; j++)
212 {
213 Y1 += lumSrc[j][2*i] * lumFilter[j];
214 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
215 }
216 for(j=0; j<chrFilterSize; j++)
217 {
218 U += chrSrc[j][i] * chrFilter[j];
219 V += chrSrc[j][i+2048] * chrFilter[j];
220 }
221 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
222 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
223 U >>= 19;
224 V >>= 19;
225
226 Cb= clip_yuvtab_40cf[U+ 256];
227 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
228 Cr= clip_yuvtab_3343[V+ 256];
229
230 dest[8*i+0]=clip_table[((Y1 + Cb) >>13)];
231 dest[8*i+1]=clip_table[((Y1 + Cg) >>13)];
232 dest[8*i+2]=clip_table[((Y1 + Cr) >>13)];
233
234 dest[8*i+4]=clip_table[((Y2 + Cb) >>13)];
235 dest[8*i+5]=clip_table[((Y2 + Cg) >>13)];
236 dest[8*i+6]=clip_table[((Y2 + Cr) >>13)];
237 }
238 }
28bf81c9 239 else if(dstFormat==IMGFMT_BGR24)
e3d2500f
MN
240 {
241 int i;
242 for(i=0; i<(dstW>>1); i++){
243 int j;
244 int Y1=0;
245 int Y2=0;
246 int U=0;
247 int V=0;
248 int Cb, Cr, Cg;
249 for(j=0; j<lumFilterSize; j++)
250 {
251 Y1 += lumSrc[j][2*i] * lumFilter[j];
252 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
253 }
254 for(j=0; j<chrFilterSize; j++)
255 {
256 U += chrSrc[j][i] * chrFilter[j];
257 V += chrSrc[j][i+2048] * chrFilter[j];
258 }
259 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
260 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
261 U >>= 19;
262 V >>= 19;
263
264 Cb= clip_yuvtab_40cf[U+ 256];
265 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
266 Cr= clip_yuvtab_3343[V+ 256];
267
268 dest[0]=clip_table[((Y1 + Cb) >>13)];
269 dest[1]=clip_table[((Y1 + Cg) >>13)];
270 dest[2]=clip_table[((Y1 + Cr) >>13)];
271
272 dest[3]=clip_table[((Y2 + Cb) >>13)];
273 dest[4]=clip_table[((Y2 + Cg) >>13)];
274 dest[5]=clip_table[((Y2 + Cr) >>13)];
275 dest+=6;
276 }
277 }
28bf81c9 278 else if(dstFormat==IMGFMT_BGR16)
e3d2500f
MN
279 {
280 int i;
281 for(i=0; i<(dstW>>1); i++){
282 int j;
283 int Y1=0;
284 int Y2=0;
285 int U=0;
286 int V=0;
287 int Cb, Cr, Cg;
288 for(j=0; j<lumFilterSize; j++)
289 {
290 Y1 += lumSrc[j][2*i] * lumFilter[j];
291 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
292 }
293 for(j=0; j<chrFilterSize; j++)
294 {
295 U += chrSrc[j][i] * chrFilter[j];
296 V += chrSrc[j][i+2048] * chrFilter[j];
297 }
298 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
299 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
300 U >>= 19;
301 V >>= 19;
302
303 Cb= clip_yuvtab_40cf[U+ 256];
304 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
305 Cr= clip_yuvtab_3343[V+ 256];
306
307 ((uint16_t*)dest)[2*i] =
308 clip_table16b[(Y1 + Cb) >>13] |
309 clip_table16g[(Y1 + Cg) >>13] |
310 clip_table16r[(Y1 + Cr) >>13];
311
312 ((uint16_t*)dest)[2*i+1] =
313 clip_table16b[(Y2 + Cb) >>13] |
314 clip_table16g[(Y2 + Cg) >>13] |
315 clip_table16r[(Y2 + Cr) >>13];
316 }
317 }
28bf81c9 318 else if(dstFormat==IMGFMT_BGR15)
e3d2500f
MN
319 {
320 int i;
321 for(i=0; i<(dstW>>1); i++){
322 int j;
323 int Y1=0;
324 int Y2=0;
325 int U=0;
326 int V=0;
327 int Cb, Cr, Cg;
328 for(j=0; j<lumFilterSize; j++)
329 {
330 Y1 += lumSrc[j][2*i] * lumFilter[j];
331 Y2 += lumSrc[j][2*i+1] * lumFilter[j];
332 }
333 for(j=0; j<chrFilterSize; j++)
334 {
335 U += chrSrc[j][i] * chrFilter[j];
336 V += chrSrc[j][i+2048] * chrFilter[j];
337 }
338 Y1= clip_yuvtab_2568[ (Y1>>19) + 256 ];
339 Y2= clip_yuvtab_2568[ (Y2>>19) + 256 ];
340 U >>= 19;
341 V >>= 19;
342
343 Cb= clip_yuvtab_40cf[U+ 256];
344 Cg= clip_yuvtab_1a1e[V+ 256] + yuvtab_0c92[U+ 256];
345 Cr= clip_yuvtab_3343[V+ 256];
346
347 ((uint16_t*)dest)[2*i] =
348 clip_table15b[(Y1 + Cb) >>13] |
349 clip_table15g[(Y1 + Cg) >>13] |
350 clip_table15r[(Y1 + Cr) >>13];
351
352 ((uint16_t*)dest)[2*i+1] =
353 clip_table15b[(Y2 + Cb) >>13] |
354 clip_table15g[(Y2 + Cg) >>13] |
355 clip_table15r[(Y2 + Cr) >>13];
356 }
357 }
358}
359
360
7630f2e0
MN
361//Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
362//Plain C versions
726a959a
MN
363#if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
364#define COMPILE_C
365#endif
366
367#ifdef CAN_COMPILE_X86_ASM
368
369#if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
370#define COMPILE_MMX
371#endif
372
373#if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
374#define COMPILE_MMX2
375#endif
376
377#if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
378#define COMPILE_3DNOW
379#endif
380#endif //CAN_COMPILE_X86_ASM
381
382#undef HAVE_MMX
383#undef HAVE_MMX2
384#undef HAVE_3DNOW
385#undef ARCH_X86
386
387#ifdef COMPILE_C
7630f2e0
MN
388#undef HAVE_MMX
389#undef HAVE_MMX2
390#undef HAVE_3DNOW
391#undef ARCH_X86
392#define RENAME(a) a ## _C
393#include "swscale_template.c"
726a959a 394#endif
397c035e 395
7630f2e0 396#ifdef CAN_COMPILE_X86_ASM
397c035e 397
7630f2e0
MN
398//X86 versions
399/*
400#undef RENAME
401#undef HAVE_MMX
402#undef HAVE_MMX2
403#undef HAVE_3DNOW
404#define ARCH_X86
405#define RENAME(a) a ## _X86
406#include "swscale_template.c"
1faf0867 407*/
7630f2e0 408//MMX versions
726a959a 409#ifdef COMPILE_MMX
7630f2e0
MN
410#undef RENAME
411#define HAVE_MMX
412#undef HAVE_MMX2
413#undef HAVE_3DNOW
414#define ARCH_X86
415#define RENAME(a) a ## _MMX
416#include "swscale_template.c"
726a959a 417#endif
7630f2e0
MN
418
419//MMX2 versions
726a959a 420#ifdef COMPILE_MMX2
7630f2e0
MN
421#undef RENAME
422#define HAVE_MMX
423#define HAVE_MMX2
424#undef HAVE_3DNOW
425#define ARCH_X86
426#define RENAME(a) a ## _MMX2
427#include "swscale_template.c"
726a959a 428#endif
7630f2e0
MN
429
430//3DNOW versions
726a959a 431#ifdef COMPILE_3DNOW
7630f2e0
MN
432#undef RENAME
433#define HAVE_MMX
434#undef HAVE_MMX2
435#define HAVE_3DNOW
436#define ARCH_X86
437#define RENAME(a) a ## _3DNow
438#include "swscale_template.c"
726a959a 439#endif
7630f2e0
MN
440
441#endif //CAN_COMPILE_X86_ASM
442
443// minor note: the HAVE_xyz is messed up after that line so dont use it
d604bab9 444
d3f41512 445
c7f822d9 446// old global scaler, dont use for new code, unless it uses only the stuff from the command line
28bf81c9
MN
447// will use sws_flags from the command line
448void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
449 int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
d1fac6cf 450 int srcW, int srcH, int dstW, int dstH){
31190492 451
28bf81c9
MN
452 static SwsContext *context=NULL;
453 int dstFormat;
454 int flags=0;
455 static int firstTime=1;
456 int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
c7f822d9 457 static SwsFilter srcFilter={NULL, NULL, NULL, NULL};
28bf81c9
MN
458
459 if(firstTime)
460 {
461 flags= SWS_PRINT_INFO;
462 firstTime=0;
c7f822d9
MN
463{/*
464 SwsVector *g= getGaussianVec(1.7, 2);
465 SwsVector *id= getIdentityVec();
466 scaleVec(g, 0.2);
467
468
469// srcFilter.chrH= diffVec(id, g);
470// srcFilter.chrH= shiftVec(id, 20);
471 srcFilter.chrH= g;
472// freeVec(g);
473 freeVec(id);
474
475 normalizeVec(srcFilter.chrH, 1.0);
476 printVec(srcFilter.chrH);
477
478 srcFilter.lumV= srcFilter.lumH= srcFilter.chrV= srcFilter.chrH;
479 srcFilter.lumH = srcFilter.lumV = NULL;
480// srcFilter.chrH = srcFilter.chrV = NULL;
481*/}
28bf81c9
MN
482 }
483
484 switch(dstbpp)
485 {
486 case 8 : dstFormat= IMGFMT_Y8; break;
487 case 12: dstFormat= IMGFMT_YV12; break;
488 case 15: dstFormat= IMGFMT_BGR15; break;
489 case 16: dstFormat= IMGFMT_BGR16; break;
490 case 24: dstFormat= IMGFMT_BGR24; break;
491 case 32: dstFormat= IMGFMT_BGR32; break;
492 default: return;
493 }
494
495 switch(sws_flags)
496 {
497 case 0: flags|= SWS_FAST_BILINEAR; break;
498 case 1: flags|= SWS_BILINEAR; break;
499 case 2: flags|= SWS_BICUBIC; break;
500 case 3: flags|= SWS_X; break;
501 default:flags|= SWS_BILINEAR; break;
502 }
503
c7f822d9 504 if(!context) context=getSwsContext(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat, flags, &srcFilter, NULL);
28bf81c9
MN
505
506
507 swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
508}
509
c7f822d9
MN
510static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
511 int srcW, int dstW, int filterAlign, int one, int flags,
512 SwsVector *srcFilter, SwsVector *dstFilter)
28bf81c9
MN
513{
514 int i;
c7f822d9
MN
515 int filterSize;
516 int filter2Size;
517 int minFilterSize;
518 double *filter=NULL;
519 double *filter2=NULL;
28bf81c9
MN
520#ifdef ARCH_X86
521 if(gCpuCaps.hasMMX)
522 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
726a959a 523#endif
31190492 524
c7f822d9
MN
525 *filterPos = (int16_t*)memalign(8, dstW*sizeof(int16_t));
526
28bf81c9
MN
527 if(ABS(xInc - 0x10000) <10) // unscaled
528 {
529 int i;
c7f822d9
MN
530 filterSize= 1;
531 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
532 for(i=0; i<dstW*filterSize; i++) filter[i]=0;
28bf81c9
MN
533
534 for(i=0; i<dstW; i++)
535 {
c7f822d9
MN
536 filter[i*filterSize]=1;
537 (*filterPos)[i]=i;
28bf81c9
MN
538 }
539
540 }
541 else if(xInc <= (1<<16) || (flags&SWS_FAST_BILINEAR)) // upscale
542 {
543 int i;
544 int xDstInSrc;
c7f822d9
MN
545 if (flags&SWS_BICUBIC) filterSize= 4;
546 else if(flags&SWS_X ) filterSize= 4;
547 else filterSize= 2;
28bf81c9 548// printf("%d %d %d\n", filterSize, srcW, dstW);
c7f822d9 549 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
28bf81c9
MN
550
551 xDstInSrc= xInc/2 - 0x8000;
552 for(i=0; i<dstW; i++)
553 {
c7f822d9 554 int xx= (xDstInSrc>>16) - (filterSize>>1) + 1;
28bf81c9
MN
555 int j;
556
c7f822d9 557 (*filterPos)[i]= xx;
28bf81c9
MN
558 if((flags & SWS_BICUBIC) || (flags & SWS_X))
559 {
560 double d= ABS(((xx+1)<<16) - xDstInSrc)/(double)(1<<16);
561 double y1,y2,y3,y4;
562 double A= -0.6;
563 if(flags & SWS_BICUBIC){
564 // Equation is from VirtualDub
565 y1 = ( + A*d - 2.0*A*d*d + A*d*d*d);
566 y2 = (+ 1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
567 y3 = ( - A*d + (2.0*A+3.0)*d*d - (A+2.0)*d*d*d);
568 y4 = ( + A*d*d - A*d*d*d);
569 }else{
570 // cubic interpolation (derived it myself)
571 y1 = ( -2.0*d + 3.0*d*d - 1.0*d*d*d)/6.0;
572 y2 = (6.0 -3.0*d - 6.0*d*d + 3.0*d*d*d)/6.0;
573 y3 = ( +6.0*d + 3.0*d*d - 3.0*d*d*d)/6.0;
574 y4 = ( -1.0*d + 1.0*d*d*d)/6.0;
575 }
576
577// printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
c7f822d9
MN
578 filter[i*filterSize + 0]= y1;
579 filter[i*filterSize + 1]= y2;
580 filter[i*filterSize + 2]= y3;
581 filter[i*filterSize + 3]= y4;
28bf81c9
MN
582// printf("%1.3f %1.3f %1.3f %1.3f %1.3f\n",d , y1, y2, y3, y4);
583 }
584 else
585 {
c7f822d9 586 for(j=0; j<filterSize; j++)
28bf81c9
MN
587 {
588 double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
589 double coeff= 1.0 - d;
590 if(coeff<0) coeff=0;
591 // printf("%d %d %d \n", coeff, (int)d, xDstInSrc);
c7f822d9 592 filter[i*filterSize + j]= coeff;
28bf81c9
MN
593 xx++;
594 }
595 }
596 xDstInSrc+= xInc;
597 }
598 }
599 else // downscale
600 {
601 int xDstInSrc;
c7f822d9
MN
602 if(flags&SWS_BICUBIC) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
603 else if(flags&SWS_X) filterSize= (int)ceil(1 + 4.0*srcW / (double)dstW);
604 else filterSize= (int)ceil(1 + 2.0*srcW / (double)dstW);
28bf81c9 605// printf("%d %d %d\n", *filterSize, srcW, dstW);
c7f822d9 606 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
28bf81c9
MN
607
608 xDstInSrc= xInc/2 - 0x8000;
609 for(i=0; i<dstW; i++)
610 {
c7f822d9 611 int xx= (int)((double)xDstInSrc/(double)(1<<16) - (filterSize-1)*0.5 + 0.5);
28bf81c9 612 int j;
c7f822d9
MN
613 (*filterPos)[i]= xx;
614 for(j=0; j<filterSize; j++)
28bf81c9
MN
615 {
616 double d= ABS((xx<<16) - xDstInSrc)/(double)xInc;
617 double coeff;
618 if((flags & SWS_BICUBIC) || (flags & SWS_X))
619 {
620 double A= -0.75;
621// d*=2;
622 // Equation is from VirtualDub
623 if(d<1.0)
624 coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
625 else if(d<2.0)
626 coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
627 else
628 coeff=0.0;
629 }
630/* else if(flags & SWS_X)
631 {
632 }*/
633 else
634 {
635 coeff= 1.0 - d;
636 if(coeff<0) coeff=0;
637 }
638// printf("%1.3f %d %d \n", coeff, (int)d, xDstInSrc);
c7f822d9 639 filter[i*filterSize + j]= coeff;
28bf81c9
MN
640 xx++;
641 }
642 xDstInSrc+= xInc;
643 }
644 }
645
c7f822d9
MN
646 /* apply src & dst Filter to filter -> filter2
647 free(filter);
648 */
649 filter2Size= filterSize;
650 if(srcFilter) filter2Size+= srcFilter->length - 1;
651 if(dstFilter) filter2Size+= dstFilter->length - 1;
652 filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
653
654 for(i=0; i<dstW; i++)
655 {
656 int j;
657 SwsVector scaleFilter;
658 SwsVector *outVec;
659
660 scaleFilter.coeff= filter + i*filterSize;
661 scaleFilter.length= filterSize;
662
663 if(srcFilter) outVec= convVec(srcFilter, &scaleFilter);
664 else outVec= &scaleFilter;
665
666 ASSERT(outVec->length == filter2Size)
667 //FIXME dstFilter
668
669 for(j=0; j<outVec->length; j++)
670 {
671 filter2[i*filter2Size + j]= outVec->coeff[j];
672 }
673
674 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
675
676 if(outVec != &scaleFilter) freeVec(outVec);
677 }
678 free(filter); filter=NULL;
679
680 /* try to reduce the filter-size (step1 find size and shift left) */
681 // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
682 minFilterSize= 0;
683 for(i=dstW-1; i>=0; i--)
684 {
685 int min= filter2Size;
686 int j;
687 double cutOff=0.0;
688
689 /* get rid off near zero elements on the left by shifting left */
690 for(j=0; j<filter2Size; j++)
691 {
692 int k;
693 cutOff += ABS(filter2[i*filter2Size]);
694
695 if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
696
697 /* preserve Monotonicity because the core cant handle the filter otherwise */
698 if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
699
700 // Move filter coeffs left
701 for(k=1; k<filter2Size; k++)
702 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
703 filter2[i*filter2Size + k - 1]= 0.0;
704 (*filterPos)[i]++;
705 }
706
707 cutOff=0.0;
708 /* count near zeros on the right */
709 for(j=filter2Size-1; j>0; j--)
710 {
711 cutOff += ABS(filter2[i*filter2Size + j]);
712
713 if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
714 min--;
715 }
716
717 if(min>minFilterSize) minFilterSize= min;
718 }
719
720 /* try to reduce the filter-size (step2 reduce it) */
721 for(i=0; i<dstW; i++)
722 {
723 int j;
724
725 for(j=0; j<minFilterSize; j++)
726 filter2[i*minFilterSize + j]= filter2[i*filter2Size + j];
727 }
728 if((flags&SWS_PRINT_INFO) && verbose)
729 printf("SwScaler: reducing filtersize %d -> %d\n", filter2Size, minFilterSize);
730 filter2Size= minFilterSize;
731 ASSERT(filter2Size > 0)
732
733 //FIXME try to align filterpos if possible
734
28bf81c9
MN
735 //fix borders
736 for(i=0; i<dstW; i++)
737 {
738 int j;
c7f822d9 739 if((*filterPos)[i] < 0)
28bf81c9
MN
740 {
741 // Move filter coeffs left to compensate for filterPos
c7f822d9 742 for(j=1; j<filter2Size; j++)
28bf81c9 743 {
c7f822d9
MN
744 int left= MAX(j + (*filterPos)[i], 0);
745 filter2[i*filter2Size + left] += filter2[i*filter2Size + j];
746 filter2[i*filter2Size + j]=0;
28bf81c9 747 }
c7f822d9 748 (*filterPos)[i]= 0;
28bf81c9
MN
749 }
750
c7f822d9 751 if((*filterPos)[i] + filter2Size > srcW)
28bf81c9 752 {
c7f822d9 753 int shift= (*filterPos)[i] + filter2Size - srcW;
28bf81c9 754 // Move filter coeffs right to compensate for filterPos
c7f822d9 755 for(j=filter2Size-2; j>=0; j--)
28bf81c9 756 {
c7f822d9
MN
757 int right= MIN(j + shift, filter2Size-1);
758 filter2[i*filter2Size +right] += filter2[i*filter2Size +j];
759 filter2[i*filter2Size +j]=0;
28bf81c9 760 }
c7f822d9 761 (*filterPos)[i]= srcW - filter2Size;
28bf81c9
MN
762 }
763 }
764
28bf81c9 765
c7f822d9
MN
766 *outFilterSize= (filter2Size +(filterAlign-1)) & (~(filterAlign-1));
767 *outFilter= (int16_t*)memalign(8, *outFilterSize*dstW*sizeof(int16_t));
768 memset(*outFilter, 0, *outFilterSize*dstW*sizeof(int16_t));
769
770 /* Normalize & Store in outFilter */
28bf81c9
MN
771 for(i=0; i<dstW; i++)
772 {
773 int j;
774 double sum=0;
775 double scale= one;
c7f822d9 776 for(j=0; j<filter2Size; j++)
28bf81c9 777 {
c7f822d9 778 sum+= filter2[i*filter2Size + j];
28bf81c9
MN
779 }
780 scale/= sum;
c7f822d9 781 for(j=0; j<filter2Size; j++)
28bf81c9 782 {
c7f822d9 783 (*outFilter)[i*(*outFilterSize) + j]= (int)(filter2[i*filter2Size + j]*scale);
28bf81c9
MN
784 }
785 }
c7f822d9
MN
786
787 free(filter2);
7630f2e0 788}
31190492 789
28bf81c9
MN
790#ifdef ARCH_X86
791static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode)
792{
793 uint8_t *fragment;
794 int imm8OfPShufW1;
795 int imm8OfPShufW2;
796 int fragmentLength;
797
798 int xpos, i;
799
800 // create an optimized horizontal scaling routine
801
802 //code fragment
803
804 asm volatile(
805 "jmp 9f \n\t"
806 // Begin
807 "0: \n\t"
808 "movq (%%esi), %%mm0 \n\t" //FIXME Alignment
809 "movq %%mm0, %%mm1 \n\t"
810 "psrlq $8, %%mm0 \n\t"
811 "punpcklbw %%mm7, %%mm1 \n\t"
812 "movq %%mm2, %%mm3 \n\t"
813 "punpcklbw %%mm7, %%mm0 \n\t"
814 "addw %%bx, %%cx \n\t" //2*xalpha += (4*lumXInc)&0xFFFF
815 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
816 "1: \n\t"
817 "adcl %%edx, %%esi \n\t" //xx+= (4*lumXInc)>>16 + carry
818 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
819 "2: \n\t"
820 "psrlw $9, %%mm3 \n\t"
821 "psubw %%mm1, %%mm0 \n\t"
822 "pmullw %%mm3, %%mm0 \n\t"
823 "paddw %%mm6, %%mm2 \n\t" // 2*alpha += xpos&0xFFFF
824 "psllw $7, %%mm1 \n\t"
825 "paddw %%mm1, %%mm0 \n\t"
826
827 "movq %%mm0, (%%edi, %%eax) \n\t"
828
829 "addl $8, %%eax \n\t"
830 // End
831 "9: \n\t"
832// "int $3\n\t"
833 "leal 0b, %0 \n\t"
834 "leal 1b, %1 \n\t"
835 "leal 2b, %2 \n\t"
836 "decl %1 \n\t"
837 "decl %2 \n\t"
838 "subl %0, %1 \n\t"
839 "subl %0, %2 \n\t"
840 "leal 9b, %3 \n\t"
841 "subl %0, %3 \n\t"
842 :"=r" (fragment), "=r" (imm8OfPShufW1), "=r" (imm8OfPShufW2),
843 "=r" (fragmentLength)
844 );
845
846 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
847
848 for(i=0; i<dstW/8; i++)
849 {
850 int xx=xpos>>16;
851
852 if((i&3) == 0)
853 {
854 int a=0;
855 int b=((xpos+xInc)>>16) - xx;
856 int c=((xpos+xInc*2)>>16) - xx;
857 int d=((xpos+xInc*3)>>16) - xx;
858
859 memcpy(funnyCode + fragmentLength*i/4, fragment, fragmentLength);
860
861 funnyCode[fragmentLength*i/4 + imm8OfPShufW1]=
862 funnyCode[fragmentLength*i/4 + imm8OfPShufW2]=
863 a | (b<<2) | (c<<4) | (d<<6);
864
865 // if we dont need to read 8 bytes than dont :), reduces the chance of
866 // crossing a cache line
867 if(d<3) funnyCode[fragmentLength*i/4 + 1]= 0x6E;
868
869 funnyCode[fragmentLength*(i+4)/4]= RET;
870 }
871 xpos+=xInc;
872 }
873}
874#endif // ARCH_X86
875
876//FIXME remove
31190492 877void SwScale_Init(){
28bf81c9
MN
878}
879
880static void globalInit(){
31190492
A
881 // generating tables:
882 int i;
c1b0bfb4
MN
883 for(i=0; i<768; i++){
884 int c= MIN(MAX(i-256, 0), 255);
885 clip_table[i]=c;
886 yuvtab_2568[c]= clip_yuvtab_2568[i]=(0x2568*(c-16))+(256<<13);
887 yuvtab_3343[c]= clip_yuvtab_3343[i]=0x3343*(c-128);
888 yuvtab_0c92[c]= clip_yuvtab_0c92[i]=-0x0c92*(c-128);
889 yuvtab_1a1e[c]= clip_yuvtab_1a1e[i]=-0x1a1e*(c-128);
890 yuvtab_40cf[c]= clip_yuvtab_40cf[i]=0x40cf*(c-128);
31190492
A
891 }
892
b18ea156
MN
893 for(i=0; i<768; i++)
894 {
28bf81c9 895 int v= clip_table[i];
b18ea156
MN
896 clip_table16b[i]= v>>3;
897 clip_table16g[i]= (v<<3)&0x07E0;
898 clip_table16r[i]= (v<<8)&0xF800;
899 clip_table15b[i]= v>>3;
900 clip_table15g[i]= (v<<2)&0x03E0;
901 clip_table15r[i]= (v<<7)&0x7C00;
902 }
c1b0bfb4 903
28bf81c9
MN
904cpuCaps= gCpuCaps;
905
906#ifdef RUNTIME_CPUDETECT
907#ifdef CAN_COMPILE_X86_ASM
908 // ordered per speed fasterst first
909 if(gCpuCaps.hasMMX2)
910 swScale= swScale_MMX2;
911 else if(gCpuCaps.has3DNow)
7f56a527 912 swScale= swScale_3DNow;
28bf81c9
MN
913 else if(gCpuCaps.hasMMX)
914 swScale= swScale_MMX;
915 else
916 swScale= swScale_C;
917
918#else
919 swScale= swScale_C;
920 cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
921#endif
922#else //RUNTIME_CPUDETECT
923#ifdef HAVE_MMX2
924 swScale= swScale_MMX2;
925 cpuCaps.has3DNow = 0;
926#elif defined (HAVE_3DNOW)
7f56a527 927 swScale= swScale_3DNow;
28bf81c9
MN
928 cpuCaps.hasMMX2 = 0;
929#elif defined (HAVE_MMX)
930 swScale= swScale_MMX;
931 cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
932#else
933 swScale= swScale_C;
934 cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
935#endif
936#endif //!RUNTIME_CPUDETECT
31190492 937}
7630f2e0 938
28bf81c9
MN
939
940SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
941 SwsFilter *srcFilter, SwsFilter *dstFilter){
942
943 const int widthAlign= dstFormat==IMGFMT_YV12 ? 16 : 8;
944 SwsContext *c;
945 int i;
c7f822d9
MN
946 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
947
28bf81c9
MN
948 if(swScale==NULL) globalInit();
949
950 /* sanity check */
951 if(srcW<1 || srcH<1 || dstW<1 || dstH<1) return NULL;
28bf81c9
MN
952
953/* FIXME
954 if(dstStride[0]%widthAlign !=0 )
955 {
956 if(flags & SWS_PRINT_INFO)
957 fprintf(stderr, "SwScaler: Warning: dstStride is not a multiple of %d!\n"
958 "SwScaler: ->cannot do aligned memory acesses anymore\n",
959 widthAlign);
960 }
961*/
c7f822d9
MN
962 if(!dstFilter) dstFilter= &dummyFilter;
963 if(!srcFilter) srcFilter= &dummyFilter;
964
28bf81c9 965 c= memalign(64, sizeof(SwsContext));
c7f822d9 966 memset(c, 0, sizeof(SwsContext));
28bf81c9
MN
967
968 c->srcW= srcW;
969 c->srcH= srcH;
970 c->dstW= dstW;
971 c->dstH= dstH;
972 c->lumXInc= ((srcW<<16) + (1<<15))/dstW;
973 c->lumYInc= ((srcH<<16) + (1<<15))/dstH;
974 c->flags= flags;
975 c->dstFormat= dstFormat;
976 c->srcFormat= srcFormat;
977
978 if(cpuCaps.hasMMX2)
979 {
980 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
981 if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
982 {
983 if(flags&SWS_PRINT_INFO)
984 fprintf(stderr, "SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
985 }
986 }
987 else
988 c->canMMX2BeUsed=0;
989
990 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
991 // but only for the FAST_BILINEAR mode otherwise do correct scaling
992 // n-2 is the last chrominance sample available
993 // this is not perfect, but noone shuld notice the difference, the more correct variant
994 // would be like the vertical one, but that would require some special code for the
995 // first and last pixel
996 if(flags&SWS_FAST_BILINEAR)
997 {
998 if(c->canMMX2BeUsed) c->lumXInc+= 20;
999 //we dont use the x86asm scaler if mmx is available
1000 else if(cpuCaps.hasMMX) c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
1001 }
1002
1003 /* set chrXInc & chrDstW */
1004 if((flags&SWS_FULL_UV_IPOL) && dstFormat!=IMGFMT_YV12)
1005 c->chrXInc= c->lumXInc>>1, c->chrDstW= dstW;
1006 else
1007 c->chrXInc= c->lumXInc, c->chrDstW= (dstW+1)>>1;
1008
1009 /* set chrYInc & chrDstH */
1010 if(dstFormat==IMGFMT_YV12) c->chrYInc= c->lumYInc, c->chrDstH= (dstH+1)>>1;
1011 else c->chrYInc= c->lumYInc>>1, c->chrDstH= dstH;
1012
1013 /* precalculate horizontal scaler filter coefficients */
1014 {
1015 const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
1016
c7f822d9
MN
1017 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
1018 srcW , dstW, filterAlign, 1<<14, flags,
1019 srcFilter->lumH, dstFilter->lumH);
1020 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
1021 (srcW+1)>>1, c->chrDstW, filterAlign, 1<<14, flags,
1022 srcFilter->chrH, dstFilter->chrH);
28bf81c9
MN
1023
1024#ifdef ARCH_X86
1025// cant downscale !!!
1026 if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
1027 {
1028 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode);
1029 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode);
1030 }
1031#endif
1032 } // Init Horizontal stuff
1033
1034
1035
1036 /* precalculate vertical scaler filter coefficients */
c7f822d9
MN
1037 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
1038 srcH , dstH, 1, (1<<12)-4, flags,
1039 srcFilter->lumV, dstFilter->lumV);
1040 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
1041 (srcH+1)>>1, c->chrDstH, 1, (1<<12)-4, flags,
1042 srcFilter->chrV, dstFilter->chrV);
28bf81c9
MN
1043
1044 // Calculate Buffer Sizes so that they wont run out while handling these damn slices
1045 c->vLumBufSize= c->vLumFilterSize;
1046 c->vChrBufSize= c->vChrFilterSize;
1047 for(i=0; i<dstH; i++)
1048 {
1049 int chrI= i*c->chrDstH / dstH;
1050 int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
1051 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<1));
1052 nextSlice&= ~1; // Slices start at even boundaries
1053 if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
1054 c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
1055 if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>1))
1056 c->vChrBufSize= (nextSlice>>1) - c->vChrFilterPos[chrI];
1057 }
1058
1059 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
1060 // allocate several megabytes to handle all possible cases)
c7f822d9
MN
1061 c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
1062 c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
28bf81c9
MN
1063 for(i=0; i<c->vLumBufSize; i++)
1064 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
1065 for(i=0; i<c->vChrBufSize; i++)
1066 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
1067
1068 //try to avoid drawing green stuff between the right end and the stride end
1069 for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
1070 for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
1071
1072 ASSERT(c->chrDstH <= dstH)
28bf81c9
MN
1073
1074 // pack filter data for mmx code
1075 if(cpuCaps.hasMMX)
1076 {
c7f822d9
MN
1077 c->lumMmxFilter= (int16_t*)memalign(8, c->vLumFilterSize* dstH*4*sizeof(int16_t));
1078 c->chrMmxFilter= (int16_t*)memalign(8, c->vChrFilterSize*c->chrDstH*4*sizeof(int16_t));
28bf81c9
MN
1079 for(i=0; i<c->vLumFilterSize*dstH; i++)
1080 c->lumMmxFilter[4*i]=c->lumMmxFilter[4*i+1]=c->lumMmxFilter[4*i+2]=c->lumMmxFilter[4*i+3]=
1081 c->vLumFilter[i];
1082 for(i=0; i<c->vChrFilterSize*c->chrDstH; i++)
1083 c->chrMmxFilter[4*i]=c->chrMmxFilter[4*i+1]=c->chrMmxFilter[4*i+2]=c->chrMmxFilter[4*i+3]=
1084 c->vChrFilter[i];
1085 }
1086
1087 if(flags&SWS_PRINT_INFO)
1088 {
1089#ifdef DITHER1XBPP
1090 char *dither= cpuCaps.hasMMX ? " dithered" : "";
1091#endif
1092 if(flags&SWS_FAST_BILINEAR)
1093 fprintf(stderr, "\nSwScaler: FAST_BILINEAR scaler ");
1094 else if(flags&SWS_BILINEAR)
1095 fprintf(stderr, "\nSwScaler: BILINEAR scaler ");
1096 else if(flags&SWS_BICUBIC)
1097 fprintf(stderr, "\nSwScaler: BICUBIC scaler ");
1098 else
1099 fprintf(stderr, "\nSwScaler: ehh flags invalid?! ");
1100
1101 if(dstFormat==IMGFMT_BGR15)
1102 fprintf(stderr, "with%s BGR15 output ", dither);
1103 else if(dstFormat==IMGFMT_BGR16)
1104 fprintf(stderr, "with%s BGR16 output ", dither);
1105 else if(dstFormat==IMGFMT_BGR24)
1106 fprintf(stderr, "with BGR24 output ");
1107 else if(dstFormat==IMGFMT_BGR32)
1108 fprintf(stderr, "with BGR32 output ");
1109 else if(dstFormat==IMGFMT_YV12)
1110 fprintf(stderr, "with YV12 output ");
1111 else
1112 fprintf(stderr, "without output ");
1113
1114 if(cpuCaps.hasMMX2)
1115 fprintf(stderr, "using MMX2\n");
1116 else if(cpuCaps.has3DNow)
1117 fprintf(stderr, "using 3DNOW\n");
1118 else if(cpuCaps.hasMMX)
1119 fprintf(stderr, "using MMX\n");
1120 else
1121 fprintf(stderr, "using C\n");
1122 }
1123
1124 if((flags & SWS_PRINT_INFO) && verbose)
1125 {
1126 if(cpuCaps.hasMMX)
1127 {
1128 if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
1129 printf("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
1130 else
1131 {
1132 if(c->hLumFilterSize==4)
1133 printf("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
1134 else if(c->hLumFilterSize==8)
1135 printf("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
1136 else
1137 printf("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
1138
1139 if(c->hChrFilterSize==4)
1140 printf("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
1141 else if(c->hChrFilterSize==8)
1142 printf("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
1143 else
1144 printf("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
1145 }
1146 }
1147 else
1148 {
1149#ifdef ARCH_X86
1150 printf("SwScaler: using X86-Asm scaler for horizontal scaling\n");
1151#else
1152 if(flags & SWS_FAST_BILINEAR)
1153 printf("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
1154 else
1155 printf("SwScaler: using C scaler for horizontal scaling\n");
1156#endif
1157 }
1158 if(dstFormat==IMGFMT_YV12)
1159 {
1160 if(c->vLumFilterSize==1)
1161 printf("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12)\n", cpuCaps.hasMMX ? "MMX" : "C");
1162 else
1163 printf("SwScaler: using n-tap %s scaler for vertical scaling (YV12)\n", cpuCaps.hasMMX ? "MMX" : "C");
1164 }
1165 else
1166 {
1167 if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
1168 printf("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
1169 "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
1170 else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
1171 printf("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
1172 else
1173 printf("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
1174 }
1175
1176 if(dstFormat==IMGFMT_BGR24)
1177 printf("SwScaler: using %s YV12->BGR24 Converter\n",
1178 cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
1179 else
1180 printf("SwScaler: using %s YV12->BGR Converter\n", cpuCaps.hasMMX ? "MMX" : "C");//FIXME print format
1181
1182 printf("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1183 }
1184
1185 return c;
1186}
1187
1188/**
1189 * returns a normalized gaussian curve used to filter stuff
1190 * quality=3 is high quality, lowwer is lowwer quality
1191 */
c7f822d9
MN
1192
1193SwsVector *getGaussianVec(double variance, double quality){
28bf81c9
MN
1194 const int length= (int)(variance*quality + 0.5) | 1;
1195 int i;
1196 double *coeff= memalign(sizeof(double), length*sizeof(double));
1197 double middle= (length-1)*0.5;
c7f822d9
MN
1198 SwsVector *vec= malloc(sizeof(SwsVector));
1199
1200 vec->coeff= coeff;
1201 vec->length= length;
28bf81c9
MN
1202
1203 for(i=0; i<length; i++)
1204 {
1205 double dist= i-middle;
1206 coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
1207 }
1208
c7f822d9
MN
1209 normalizeVec(vec, 1.0);
1210
1211 return vec;
28bf81c9
MN
1212}
1213
c7f822d9
MN
1214SwsVector *getIdentityVec(void){
1215 double *coeff= memalign(sizeof(double), sizeof(double));
1216 SwsVector *vec= malloc(sizeof(SwsVector));
1217 coeff[0]= 1.0;
1218
1219 vec->coeff= coeff;
1220 vec->length= 1;
1221
1222 return vec;
1223}
1224
1225void normalizeVec(SwsVector *a, double height){
28bf81c9
MN
1226 int i;
1227 double sum=0;
1228 double inv;
1229
c7f822d9
MN
1230 for(i=0; i<a->length; i++)
1231 sum+= a->coeff[i];
28bf81c9
MN
1232
1233 inv= height/sum;
1234
c7f822d9
MN
1235 for(i=0; i<a->length; i++)
1236 a->coeff[i]*= height;
28bf81c9
MN
1237}
1238
c7f822d9
MN
1239void scaleVec(SwsVector *a, double scalar){
1240 int i;
1241
1242 for(i=0; i<a->length; i++)
1243 a->coeff[i]*= scalar;
1244}
1245
1246SwsVector *convVec(SwsVector *a, SwsVector *b){
1247 int length= a->length + b->length - 1;
28bf81c9
MN
1248 double *coeff= memalign(sizeof(double), length*sizeof(double));
1249 int i, j;
c7f822d9
MN
1250 SwsVector *vec= malloc(sizeof(SwsVector));
1251
1252 vec->coeff= coeff;
1253 vec->length= length;
28bf81c9
MN
1254
1255 for(i=0; i<length; i++) coeff[i]= 0.0;
1256
c7f822d9 1257 for(i=0; i<a->length; i++)
28bf81c9 1258 {
c7f822d9 1259 for(j=0; j<b->length; j++)
28bf81c9 1260 {
c7f822d9 1261 coeff[i+j]+= a->coeff[i]*b->coeff[j];
28bf81c9
MN
1262 }
1263 }
1264
c7f822d9 1265 return vec;
28bf81c9
MN
1266}
1267
c7f822d9
MN
1268SwsVector *sumVec(SwsVector *a, SwsVector *b){
1269 int length= MAX(a->length, b->length);
28bf81c9
MN
1270 double *coeff= memalign(sizeof(double), length*sizeof(double));
1271 int i;
c7f822d9
MN
1272 SwsVector *vec= malloc(sizeof(SwsVector));
1273
1274 vec->coeff= coeff;
1275 vec->length= length;
28bf81c9
MN
1276
1277 for(i=0; i<length; i++) coeff[i]= 0.0;
1278
c7f822d9
MN
1279 for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1280 for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
1281
1282 return vec;
28bf81c9 1283}
c7f822d9
MN
1284
1285SwsVector *diffVec(SwsVector *a, SwsVector *b){
1286 int length= MAX(a->length, b->length);
1287 double *coeff= memalign(sizeof(double), length*sizeof(double));
1288 int i;
1289 SwsVector *vec= malloc(sizeof(SwsVector));
1290
1291 vec->coeff= coeff;
1292 vec->length= length;
1293
1294 for(i=0; i<length; i++) coeff[i]= 0.0;
1295
1296 for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1297 for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
1298
1299 return vec;
1300}
1301
1302/* shift left / or right if "shift" is negative */
1303SwsVector *shiftVec(SwsVector *a, int shift){
1304 int length= a->length + ABS(shift)*2;
1305 double *coeff= memalign(sizeof(double), length*sizeof(double));
1306 int i, j;
1307 SwsVector *vec= malloc(sizeof(SwsVector));
1308
1309 vec->coeff= coeff;
1310 vec->length= length;
1311
1312 for(i=0; i<length; i++) coeff[i]= 0.0;
1313
1314 for(i=0; i<a->length; i++)
1315 {
1316 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
1317 }
1318
1319 return vec;
1320}
1321
1322void printVec(SwsVector *a){
1323 int i;
1324 double max=0;
1325 double min=0;
1326 double range;
1327
1328 for(i=0; i<a->length; i++)
1329 if(a->coeff[i]>max) max= a->coeff[i];
1330
1331 for(i=0; i<a->length; i++)
1332 if(a->coeff[i]<min) min= a->coeff[i];
1333
1334 range= max - min;
1335
1336 for(i=0; i<a->length; i++)
1337 {
1338 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
1339 printf("%1.3f ", a->coeff[i]);
1340 for(;x>0; x--) printf(" ");
1341 printf("|\n");
1342 }
1343}
1344
1345void freeVec(SwsVector *a){
1346 if(!a) return;
1347 if(a->coeff) free(a->coeff);
1348 a->coeff=NULL;
1349 a->length=0;
1350 free(a);
1351}
1352
1353void freeSwsContext(SwsContext *c){
1354 int i;
1355
1356 if(!c) return;
1357
1358 if(c->lumPixBuf)
1359 {
1360 for(i=0; i<c->vLumBufSize*2; i++)
1361 {
1362 if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
1363 c->lumPixBuf[i]=NULL;
1364 }
1365 free(c->lumPixBuf);
1366 c->lumPixBuf=NULL;
1367 }
1368
1369 if(c->chrPixBuf)
1370 {
1371 for(i=0; i<c->vChrBufSize*2; i++)
1372 {
1373 if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
1374 c->chrPixBuf[i]=NULL;
1375 }
1376 free(c->chrPixBuf);
1377 c->chrPixBuf=NULL;
1378 }
1379
1380 if(c->vLumFilter) free(c->vLumFilter);
1381 c->vLumFilter = NULL;
1382 if(c->vChrFilter) free(c->vChrFilter);
1383 c->vChrFilter = NULL;
1384 if(c->hLumFilter) free(c->hLumFilter);
1385 c->hLumFilter = NULL;
1386 if(c->hChrFilter) free(c->hChrFilter);
1387 c->hChrFilter = NULL;
1388
1389 if(c->vLumFilterPos) free(c->vLumFilterPos);
1390 c->vLumFilterPos = NULL;
1391 if(c->vChrFilterPos) free(c->vChrFilterPos);
1392 c->vChrFilterPos = NULL;
1393 if(c->hLumFilterPos) free(c->hLumFilterPos);
1394 c->hLumFilterPos = NULL;
1395 if(c->hChrFilterPos) free(c->hChrFilterPos);
1396 c->hChrFilterPos = NULL;
1397
1398 if(c->lumMmxFilter) free(c->lumMmxFilter);
1399 c->lumMmxFilter = NULL;
1400 if(c->chrMmxFilter) free(c->chrMmxFilter);
1401 c->chrMmxFilter = NULL;
1402
1403 free(c);
1404}
1405
7f56a527 1406