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