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