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