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