Make ACLs work correctly for streams. Try comparing the addresses in
[libav.git] / postproc / swscale.c
CommitLineData
fe8054c0 1/*
5427e242 2 Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
fe8054c0
MN
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/*
7322a67c 20 supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09
46de8b73 21 supported output formats: YV12, I420/IYUV, YUY2, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
e09d12f4 22 {BGR,RGB}{1,4,8,15,16} support dithering
a861d4d7 23
e09d12f4
MN
24 unscaled special converters (YV12=I420=IYUV, Y800=Y8)
25 YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
26 x -> x
27 YUV9 -> YV12
28 YUV9/YV12 -> Y800
29 Y800 -> YUV9/YV12
b935781b
MN
30 BGR24 -> BGR32 & RGB24 -> RGB32
31 BGR32 -> BGR24 & RGB32 -> RGB24
4bb3fa5e 32 BGR15 -> BGR16
b935781b
MN
33*/
34
35/*
e09d12f4
MN
36tested special converters (most are tested actually but i didnt write it down ...)
37 YV12 -> BGR16
b935781b 38 YV12 -> YV12
4bb3fa5e 39 BGR15 -> BGR16
1e1c4fe9 40 BGR16 -> BGR16
e09d12f4 41 YVU9 -> YV12
b935781b
MN
42
43untested special converters
1e1c4fe9
MN
44 YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
45 YV12/I420 -> YV12/I420
46 YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
b935781b
MN
47 BGR24 -> BGR32 & RGB24 -> RGB32
48 BGR32 -> BGR24 & RGB32 -> RGB24
ec22603f 49 BGR24 -> YV12
28bf81c9
MN
50*/
51
d3f41512 52#include <inttypes.h>
dda87e9f 53#include <string.h>
077ea8a7 54#include <math.h>
c1b0bfb4 55#include <stdio.h>
d3f41512 56#include "../config.h"
9b464428 57#include "../mangle.h"
81b7c056 58#include <assert.h>
c1b0bfb4
MN
59#ifdef HAVE_MALLOC_H
60#include <malloc.h>
b6acbc3c
BS
61#else
62#include <stdlib.h>
c1b0bfb4 63#endif
d604bab9 64#include "swscale.h"
5427e242 65#include "swscale_internal.h"
7630f2e0 66#include "../cpudetect.h"
a861d4d7 67#include "../bswap.h"
28bf81c9 68#include "../libvo/img_format.h"
37079906 69#include "rgb2rgb.h"
b0db4198 70#include "../libvo/fastmemcpy.h"
4a53a912 71#include "../mp_msg.h"
0d9f3d85
A
72
73#define MSG_WARN(args...) mp_msg(MSGT_SWS,MSGL_WARN, ##args )
74#define MSG_FATAL(args...) mp_msg(MSGT_SWS,MSGL_FATAL, ##args )
75#define MSG_ERR(args...) mp_msg(MSGT_SWS,MSGL_ERR, ##args )
76#define MSG_V(args...) mp_msg(MSGT_SWS,MSGL_V, ##args )
77#define MSG_DBG2(args...) mp_msg(MSGT_SWS,MSGL_DBG2, ##args )
78#define MSG_INFO(args...) mp_msg(MSGT_SWS,MSGL_INFO, ##args )
79
541c4eb9 80#undef MOVNTQ
7d7f78b5 81#undef PAVGB
d3f41512 82
783e9cc9 83//#undef HAVE_MMX2
7f56a527 84//#define HAVE_3DNOW
d3f41512 85//#undef HAVE_MMX
783e9cc9 86//#undef ARCH_X86
2ba1bff0 87//#define WORDS_BIGENDIAN
d604bab9 88#define DITHER1XBPP
d3f41512 89
ac6a2e45
MN
90#define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
91
1e621b18 92#define RET 0xC3 //near return opcode for X86
c1b0bfb4 93
28bf81c9 94#ifdef MP_DEBUG
81b7c056 95#define ASSERT(x) assert(x);
28bf81c9 96#else
c1b0bfb4 97#define ASSERT(x) ;
28bf81c9
MN
98#endif
99
100#ifdef M_PI
101#define PI M_PI
102#else
103#define PI 3.14159265358979323846
104#endif
c1b0bfb4 105
6c7506de 106//FIXME replace this with something faster
d80e2fa2
MN
107#define isPlanarYUV(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YVU9 \
108 || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P)
7322a67c 109#define isYUV(x) ((x)==IMGFMT_UYVY || (x)==IMGFMT_YUY2 || isPlanarYUV(x))
44c1035c 110#define isGray(x) ((x)==IMGFMT_Y800)
cf7d1c1a
MN
111#define isRGB(x) (((x)&IMGFMT_RGB_MASK)==IMGFMT_RGB)
112#define isBGR(x) (((x)&IMGFMT_BGR_MASK)==IMGFMT_BGR)
7322a67c 113#define isSupportedIn(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2 || (x)==IMGFMT_UYVY\
b72034dd 114 || (x)==IMGFMT_BGR32|| (x)==IMGFMT_BGR24|| (x)==IMGFMT_BGR16|| (x)==IMGFMT_BGR15\
a861d4d7 115 || (x)==IMGFMT_RGB32|| (x)==IMGFMT_RGB24\
d80e2fa2
MN
116 || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9\
117 || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P)
46de8b73 118#define isSupportedOut(x) ((x)==IMGFMT_YV12 || (x)==IMGFMT_I420 || (x)==IMGFMT_YUY2\
d80e2fa2 119 || (x)==IMGFMT_444P || (x)==IMGFMT_422P || (x)==IMGFMT_411P\
cf7d1c1a 120 || isRGB(x) || isBGR(x)\
e616aa93 121 || (x)==IMGFMT_Y800 || (x)==IMGFMT_YVU9)
7322a67c 122#define isPacked(x) ((x)==IMGFMT_YUY2 || (x)==IMGFMT_UYVY ||isRGB(x) || isBGR(x))
6ff0ad6b
MN
123
124#define RGB2YUV_SHIFT 16
1e621b18
MN
125#define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
126#define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
127#define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
128#define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
129#define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
130#define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
131#define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
132#define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
133#define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
6c7506de 134
e3d2500f 135extern int verbose; // defined in mplayer.c
0481412a
MN
136extern const int32_t Inverse_Table_6_9[8][4];
137
783e9cc9
MN
138/*
139NOTES
d604bab9 140Special versions: fast Y 1:1 scaling (no interpolation in y direction)
31190492 141
783e9cc9 142TODO
d604bab9 143more intelligent missalignment avoidance for the horizontal scaler
c1b0bfb4
MN
144write special vertical cubic upscale version
145Optimize C code (yv12 / minmax)
ff7ba856 146add support for packed pixel yuv input & output
6ff0ad6b
MN
147add support for Y8 output
148optimize bgr24 & bgr32
ff7ba856 149add BGR4 output support
1e621b18 150write special BGR->BGR scaler
783e9cc9 151*/
31190492 152
d604bab9 153#define ABS(a) ((a) > 0 ? (a) : (-(a)))
2ff198c1
MN
154#define MIN(a,b) ((a) > (b) ? (b) : (a))
155#define MAX(a,b) ((a) < (b) ? (b) : (a))
d604bab9 156
7630f2e0
MN
157#ifdef ARCH_X86
158#define CAN_COMPILE_X86_ASM
d604bab9
MN
159#endif
160
7630f2e0 161#ifdef CAN_COMPILE_X86_ASM
f62255fb
MN
162static uint64_t __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
163static uint64_t __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
d604bab9 164static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
077ea8a7 165static uint64_t __attribute__((aligned(8))) w02= 0x0002000200020002LL;
d604bab9
MN
166static uint64_t __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
167static uint64_t __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
168static uint64_t __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
6ff0ad6b 169static uint64_t __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
d604bab9 170
3fe8e8f0
MN
171static volatile uint64_t __attribute__((aligned(8))) b5Dither;
172static volatile uint64_t __attribute__((aligned(8))) g5Dither;
173static volatile uint64_t __attribute__((aligned(8))) g6Dither;
174static volatile uint64_t __attribute__((aligned(8))) r5Dither;
d8fa3c54
MN
175
176static uint64_t __attribute__((aligned(8))) dither4[2]={
177 0x0103010301030103LL,
178 0x0200020002000200LL,};
179
180static uint64_t __attribute__((aligned(8))) dither8[2]={
181 0x0602060206020602LL,
182 0x0004000400040004LL,};
d604bab9
MN
183
184static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
185static uint64_t __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
186static uint64_t __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
187static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
188static uint64_t __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
189static uint64_t __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
190
99d2cb72
MN
191static uint64_t __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
192static uint64_t __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
193static uint64_t __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
194
ac6a2e45
MN
195#ifdef FAST_BGR2YV12
196static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000000210041000DULL;
4342fc14
MN
197static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
198static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
ac6a2e45
MN
199#else
200static const uint64_t bgr2YCoeff __attribute__((aligned(8))) = 0x000020E540830C8BULL;
4342fc14
MN
201static const uint64_t bgr2UCoeff __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
202static const uint64_t bgr2VCoeff __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
ac6a2e45
MN
203#endif
204static const uint64_t bgr2YOffset __attribute__((aligned(8))) = 0x1010101010101010ULL;
4342fc14 205static const uint64_t bgr2UVOffset __attribute__((aligned(8)))= 0x8080808080808080ULL;
ac6a2e45 206static const uint64_t w1111 __attribute__((aligned(8))) = 0x0001000100010001ULL;
d604bab9 207#endif
783e9cc9
MN
208
209// clipping helper table for C implementations:
210static unsigned char clip_table[768];
211
28bf81c9 212//global sws_flags from the command line
1f347f22 213int sws_flags=2;
077ea8a7 214
5cebb24b
MN
215//global srcFilter
216SwsFilter src_filter= {NULL, NULL, NULL, NULL};
217
218float sws_lum_gblur= 0.0;
219float sws_chr_gblur= 0.0;
220int sws_chr_vshift= 0;
221int sws_chr_hshift= 0;
5521b193
MN
222float sws_chr_sharpen= 0.0;
223float sws_lum_sharpen= 0.0;
5cebb24b 224
28bf81c9
MN
225/* cpuCaps combined from cpudetect and whats actually compiled in
226 (if there is no support for something compiled in it wont appear here) */
227static CpuCaps cpuCaps;
d3f41512 228
28bf81c9
MN
229void (*swScale)(SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
230 int srcSliceH, uint8_t* dst[], int dstStride[])=NULL;
2ff198c1 231
5cebb24b 232static SwsVector *getConvVec(SwsVector *a, SwsVector *b);
5859233b 233
cf7d1c1a
MN
234extern const uint8_t dither_2x2_4[2][8];
235extern const uint8_t dither_2x2_8[2][8];
236extern const uint8_t dither_8x8_32[8][8];
237extern const uint8_t dither_8x8_73[8][8];
238extern const uint8_t dither_8x8_220[8][8];
5cebb24b 239
7630f2e0 240#ifdef CAN_COMPILE_X86_ASM
96034638
MN
241void in_asm_used_var_warning_killer()
242{
20380eb8 243 volatile int i= bF8+bFC+w10+
5ac80202 244 bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
6ff0ad6b 245 M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
96034638
MN
246 if(i) i=0;
247}
248#endif
d604bab9 249
c7a810cc 250static int testFormat[]={
e616aa93 251IMGFMT_YVU9,
c7a810cc
MN
252IMGFMT_YV12,
253//IMGFMT_IYUV,
254IMGFMT_I420,
255IMGFMT_BGR15,
256IMGFMT_BGR16,
257IMGFMT_BGR24,
258IMGFMT_BGR32,
81a571a8
MN
259IMGFMT_RGB24,
260IMGFMT_RGB32,
c7a810cc
MN
261//IMGFMT_Y8,
262IMGFMT_Y800,
263//IMGFMT_YUY2,
2640
265};
266
267static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h){
268 int x,y;
269 uint64_t ssd=0;
270
271 for(y=0; y<h; y++){
272 for(x=0; x<w; x++){
273 int d= src1[x + y*stride1] - src2[x + y*stride2];
274 ssd+= d*d;
275 }
276 }
277 return ssd;
278}
279
280// test by ref -> src -> dst -> out & compare out against ref
281// ref & out are YV12
282static void doTest(uint8_t *ref[3], int refStride[3], int w, int h, int srcFormat, int dstFormat,
283 int srcW, int srcH, int dstW, int dstH, int flags){
284 uint8_t *src[3];
285 uint8_t *dst[3];
286 uint8_t *out[3];
287 int srcStride[3], dstStride[3];
288 int i;
289 uint64_t ssdY, ssdU, ssdV;
290 SwsContext *srcContext, *dstContext, *outContext;
291
292 for(i=0; i<3; i++){
e09d12f4
MN
293 // avoid stride % bpp != 0
294 if(srcFormat==IMGFMT_RGB24 || srcFormat==IMGFMT_BGR24)
295 srcStride[i]= srcW*3;
296 else
297 srcStride[i]= srcW*4;
298
299 if(dstFormat==IMGFMT_RGB24 || dstFormat==IMGFMT_BGR24)
300 dstStride[i]= dstW*3;
301 else
302 dstStride[i]= dstW*4;
303
c7a810cc
MN
304 src[i]= malloc(srcStride[i]*srcH);
305 dst[i]= malloc(dstStride[i]*dstH);
306 out[i]= malloc(refStride[i]*h);
307 }
308
309 srcContext= getSwsContext(w, h, IMGFMT_YV12, srcW, srcH, srcFormat, flags, NULL, NULL);
310 dstContext= getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL);
311 outContext= getSwsContext(dstW, dstH, dstFormat, w, h, IMGFMT_YV12, flags, NULL, NULL);
312 if(srcContext==NULL ||dstContext==NULL ||outContext==NULL){
313 printf("Failed allocating swsContext\n");
314 goto end;
315 }
316// printf("test %X %X %X -> %X %X %X\n", (int)ref[0], (int)ref[1], (int)ref[2],
317// (int)src[0], (int)src[1], (int)src[2]);
318
319 srcContext->swScale(srcContext, ref, refStride, 0, h , src, srcStride);
320 dstContext->swScale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
321 outContext->swScale(outContext, dst, dstStride, 0, dstH, out, refStride);
322
323 ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
324 ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
325 ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
326
327 if(isGray(srcFormat) || isGray(dstFormat)) ssdU=ssdV=0; //FIXME check that output is really gray
328
329 ssdY/= w*h;
330 ssdU/= w*h/4;
331 ssdV/= w*h/4;
332
e616aa93 333 if(ssdY>100 || ssdU>50 || ssdV>50){
c7a810cc
MN
334 printf(" %s %dx%d -> %s %4dx%4d flags=%2d SSD=%5lld,%5lld,%5lld\n",
335 vo_format_name(srcFormat), srcW, srcH,
336 vo_format_name(dstFormat), dstW, dstH,
337 flags,
338 ssdY, ssdU, ssdV);
339 }
340
341 end:
342
343 freeSwsContext(srcContext);
344 freeSwsContext(dstContext);
345 freeSwsContext(outContext);
346
347 for(i=0; i<3; i++){
348 free(src[i]);
349 free(dst[i]);
350 free(out[i]);
351 }
352}
353
354static void selfTest(uint8_t *src[3], int stride[3], int w, int h){
355 int srcFormat, dstFormat, srcFormatIndex, dstFormatIndex;
356 int srcW, srcH, dstW, dstH;
357 int flags;
358
359 for(srcFormatIndex=0; ;srcFormatIndex++){
360 srcFormat= testFormat[srcFormatIndex];
361 if(!srcFormat) break;
362 for(dstFormatIndex=0; ;dstFormatIndex++){
363 dstFormat= testFormat[dstFormatIndex];
364 if(!dstFormat) break;
365 if(!isSupportedOut(dstFormat)) continue;
e09d12f4
MN
366printf("%s -> %s\n",
367 vo_format_name(srcFormat),
368 vo_format_name(dstFormat));
c7a810cc
MN
369
370 srcW= w+w/3;
371 srcH= h+h/3;
372 for(dstW=w; dstW<w*2; dstW+= dstW/3){
373 for(dstH=h; dstH<h*2; dstH+= dstH/3){
374 for(flags=1; flags<33; flags*=2)
375 doTest(src, stride, w, h, srcFormat, dstFormat,
376 srcW, srcH, dstW, dstH, flags);
377 }
378 }
379 }
380 }
381}
382
5859233b 383static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
e3d2500f 384 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
5859233b 385 uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
e3d2500f
MN
386{
387 //FIXME Optimize (just quickly writen not opti..)
388 int i;
5859233b 389 for(i=0; i<dstW; i++)
e3d2500f
MN
390 {
391 int val=0;
392 int j;
393 for(j=0; j<lumFilterSize; j++)
394 val += lumSrc[j][i] * lumFilter[j];
395
396 dest[i]= MIN(MAX(val>>19, 0), 255);
397 }
398
399 if(uDest != NULL)
5859233b 400 for(i=0; i<chrDstW; i++)
e3d2500f
MN
401 {
402 int u=0;
403 int v=0;
404 int j;
627690b5 405 for(j=0; j<chrFilterSize; j++)
e3d2500f
MN
406 {
407 u += chrSrc[j][i] * chrFilter[j];
408 v += chrSrc[j][i + 2048] * chrFilter[j];
409 }
410
411 uDest[i]= MIN(MAX(u>>19, 0), 255);
412 vDest[i]= MIN(MAX(v>>19, 0), 255);
413 }
414}
415
46de8b73 416
25593e29 417#define YSCALE_YUV_2_PACKEDX_C(type) \
cf7d1c1a
MN
418 for(i=0; i<(dstW>>1); i++){\
419 int j;\
420 int Y1=0;\
421 int Y2=0;\
422 int U=0;\
423 int V=0;\
424 type *r, *b, *g;\
425 const int i2= 2*i;\
426 \
427 for(j=0; j<lumFilterSize; j++)\
428 {\
429 Y1 += lumSrc[j][i2] * lumFilter[j];\
430 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
431 }\
432 for(j=0; j<chrFilterSize; j++)\
433 {\
434 U += chrSrc[j][i] * chrFilter[j];\
435 V += chrSrc[j][i+2048] * chrFilter[j];\
436 }\
437 Y1>>=19;\
438 Y2>>=19;\
439 U >>=19;\
440 V >>=19;\
441 if((Y1|Y2|U|V)&256)\
442 {\
443 if(Y1>255) Y1=255;\
444 else if(Y1<0)Y1=0;\
445 if(Y2>255) Y2=255;\
446 else if(Y2<0)Y2=0;\
447 if(U>255) U=255;\
448 else if(U<0) U=0;\
449 if(V>255) V=255;\
450 else if(V<0) V=0;\
46de8b73
MN
451 }
452
453#define YSCALE_YUV_2_RGBX_C(type) \
25593e29 454 YSCALE_YUV_2_PACKEDX_C(type)\
cf7d1c1a
MN
455 r = c->table_rV[V];\
456 g = c->table_gU[U] + c->table_gV[V];\
457 b = c->table_bU[U];\
458
25593e29 459#define YSCALE_YUV_2_PACKED2_C \
cf7d1c1a
MN
460 for(i=0; i<(dstW>>1); i++){\
461 const int i2= 2*i;\
462 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19;\
463 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;\
464 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19;\
465 int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;\
46de8b73
MN
466
467#define YSCALE_YUV_2_RGB2_C(type) \
25593e29 468 YSCALE_YUV_2_PACKED2_C\
cf7d1c1a
MN
469 type *r, *b, *g;\
470 r = c->table_rV[V];\
471 g = c->table_gU[U] + c->table_gV[V];\
472 b = c->table_bU[U];\
473
25593e29 474#define YSCALE_YUV_2_PACKED1_C \
cf7d1c1a
MN
475 for(i=0; i<(dstW>>1); i++){\
476 const int i2= 2*i;\
477 int Y1= buf0[i2 ]>>7;\
478 int Y2= buf0[i2+1]>>7;\
479 int U= (uvbuf1[i ])>>7;\
480 int V= (uvbuf1[i+2048])>>7;\
46de8b73
MN
481
482#define YSCALE_YUV_2_RGB1_C(type) \
25593e29 483 YSCALE_YUV_2_PACKED1_C\
cf7d1c1a
MN
484 type *r, *b, *g;\
485 r = c->table_rV[V];\
486 g = c->table_gU[U] + c->table_gV[V];\
487 b = c->table_bU[U];\
488
25593e29 489#define YSCALE_YUV_2_PACKED1B_C \
cf7d1c1a
MN
490 for(i=0; i<(dstW>>1); i++){\
491 const int i2= 2*i;\
492 int Y1= buf0[i2 ]>>7;\
493 int Y2= buf0[i2+1]>>7;\
494 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
495 int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
46de8b73
MN
496
497#define YSCALE_YUV_2_RGB1B_C(type) \
25593e29 498 YSCALE_YUV_2_PACKED1B_C\
cf7d1c1a
MN
499 type *r, *b, *g;\
500 r = c->table_rV[V];\
501 g = c->table_gU[U] + c->table_gV[V];\
502 b = c->table_bU[U];\
503
46de8b73 504#define YSCALE_YUV_2_ANYRGB_C(func, func2)\
cf7d1c1a
MN
505 switch(c->dstFormat)\
506 {\
507 case IMGFMT_BGR32:\
508 case IMGFMT_RGB32:\
509 func(uint32_t)\
510 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
511 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
512 } \
513 break;\
514 case IMGFMT_RGB24:\
515 func(uint8_t)\
516 ((uint8_t*)dest)[0]= r[Y1];\
517 ((uint8_t*)dest)[1]= g[Y1];\
518 ((uint8_t*)dest)[2]= b[Y1];\
519 ((uint8_t*)dest)[3]= r[Y2];\
520 ((uint8_t*)dest)[4]= g[Y2];\
521 ((uint8_t*)dest)[5]= b[Y2];\
522 ((uint8_t*)dest)+=6;\
523 }\
524 break;\
525 case IMGFMT_BGR24:\
526 func(uint8_t)\
527 ((uint8_t*)dest)[0]= b[Y1];\
528 ((uint8_t*)dest)[1]= g[Y1];\
529 ((uint8_t*)dest)[2]= r[Y1];\
530 ((uint8_t*)dest)[3]= b[Y2];\
531 ((uint8_t*)dest)[4]= g[Y2];\
532 ((uint8_t*)dest)[5]= r[Y2];\
533 ((uint8_t*)dest)+=6;\
534 }\
535 break;\
536 case IMGFMT_RGB16:\
537 case IMGFMT_BGR16:\
538 {\
539 const int dr1= dither_2x2_8[y&1 ][0];\
540 const int dg1= dither_2x2_4[y&1 ][0];\
541 const int db1= dither_2x2_8[(y&1)^1][0];\
542 const int dr2= dither_2x2_8[y&1 ][1];\
543 const int dg2= dither_2x2_4[y&1 ][1];\
544 const int db2= dither_2x2_8[(y&1)^1][1];\
545 func(uint16_t)\
546 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
547 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
548 }\
549 }\
550 break;\
551 case IMGFMT_RGB15:\
552 case IMGFMT_BGR15:\
553 {\
554 const int dr1= dither_2x2_8[y&1 ][0];\
555 const int dg1= dither_2x2_8[y&1 ][1];\
556 const int db1= dither_2x2_8[(y&1)^1][0];\
557 const int dr2= dither_2x2_8[y&1 ][1];\
558 const int dg2= dither_2x2_8[y&1 ][0];\
559 const int db2= dither_2x2_8[(y&1)^1][1];\
560 func(uint16_t)\
561 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
562 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
563 }\
564 }\
565 break;\
566 case IMGFMT_RGB8:\
567 case IMGFMT_BGR8:\
568 {\
569 const uint8_t * const d64= dither_8x8_73[y&7];\
570 const uint8_t * const d32= dither_8x8_32[y&7];\
571 func(uint8_t)\
572 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
573 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
574 }\
575 }\
576 break;\
577 case IMGFMT_RGB4:\
578 case IMGFMT_BGR4:\
579 {\
580 const uint8_t * const d64= dither_8x8_73 [y&7];\
581 const uint8_t * const d128=dither_8x8_220[y&7];\
582 func(uint8_t)\
799fd467 583 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
f17457ac
MN
584 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
585 }\
586 }\
587 break;\
588 case IMGFMT_RG4B:\
589 case IMGFMT_BG4B:\
590 {\
591 const uint8_t * const d64= dither_8x8_73 [y&7];\
592 const uint8_t * const d128=dither_8x8_220[y&7];\
593 func(uint8_t)\
cf7d1c1a
MN
594 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
595 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
596 }\
597 }\
598 break;\
599 case IMGFMT_RGB1:\
600 case IMGFMT_BGR1:\
601 {\
602 const uint8_t * const d128=dither_8x8_220[y&7];\
603 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
604 for(i=0; i<dstW-7; i+=8){\
605 int acc;\
606 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
607 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
608 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
609 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
610 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
611 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
612 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
613 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
614 ((uint8_t*)dest)[0]= acc;\
615 ((uint8_t*)dest)++;\
616 }\
617\
618/*\
619((uint8_t*)dest)-= dstW>>4;\
620{\
621 int acc=0;\
622 int left=0;\
623 static int top[1024];\
624 static int last_new[1024][1024];\
625 static int last_in3[1024][1024];\
626 static int drift[1024][1024];\
627 int topLeft=0;\
628 int shift=0;\
629 int count=0;\
630 const uint8_t * const d128=dither_8x8_220[y&7];\
631 int error_new=0;\
632 int error_in3=0;\
633 int f=0;\
634 \
635 for(i=dstW>>1; i<dstW; i++){\
636 int in= ((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19);\
637 int in2 = (76309 * (in - 16) + 32768) >> 16;\
638 int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
639 int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
640 + (last_new[y][i] - in3)*f/256;\
641 int new= old> 128 ? 255 : 0;\
642\
643 error_new+= ABS(last_new[y][i] - new);\
644 error_in3+= ABS(last_in3[y][i] - in3);\
645 f= error_new - error_in3*4;\
646 if(f<0) f=0;\
647 if(f>256) f=256;\
648\
649 topLeft= top[i];\
650 left= top[i]= old - new;\
651 last_new[y][i]= new;\
652 last_in3[y][i]= in3;\
653\
654 acc+= acc + (new&1);\
655 if((i&7)==6){\
656 ((uint8_t*)dest)[0]= acc;\
657 ((uint8_t*)dest)++;\
658 }\
659 }\
660}\
661*/\
662 }\
663 break;\
46de8b73
MN
664 case IMGFMT_YUY2:\
665 func2\
666 ((uint8_t*)dest)[2*i2+0]= Y1;\
667 ((uint8_t*)dest)[2*i2+1]= U;\
668 ((uint8_t*)dest)[2*i2+2]= Y2;\
669 ((uint8_t*)dest)[2*i2+3]= V;\
670 } \
671 break;\
cf7d1c1a
MN
672 }\
673
674
25593e29 675static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
e3d2500f 676 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
cf7d1c1a 677 uint8_t *dest, int dstW, int y)
e3d2500f 678{
cf7d1c1a
MN
679 int i;
680 switch(c->dstFormat)
e3d2500f 681 {
cf7d1c1a
MN
682 case IMGFMT_RGB32:
683 case IMGFMT_BGR32:
684 YSCALE_YUV_2_RGBX_C(uint32_t)
685 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
686 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
e3d2500f 687 }
cf7d1c1a
MN
688 break;
689 case IMGFMT_RGB24:
690 YSCALE_YUV_2_RGBX_C(uint8_t)
691 ((uint8_t*)dest)[0]= r[Y1];
692 ((uint8_t*)dest)[1]= g[Y1];
693 ((uint8_t*)dest)[2]= b[Y1];
694 ((uint8_t*)dest)[3]= r[Y2];
695 ((uint8_t*)dest)[4]= g[Y2];
696 ((uint8_t*)dest)[5]= b[Y2];
697 ((uint8_t*)dest)+=6;
698 }
699 break;
700 case IMGFMT_BGR24:
701 YSCALE_YUV_2_RGBX_C(uint8_t)
702 ((uint8_t*)dest)[0]= b[Y1];
703 ((uint8_t*)dest)[1]= g[Y1];
704 ((uint8_t*)dest)[2]= r[Y1];
705 ((uint8_t*)dest)[3]= b[Y2];
706 ((uint8_t*)dest)[4]= g[Y2];
707 ((uint8_t*)dest)[5]= r[Y2];
708 ((uint8_t*)dest)+=6;
709 }
710 break;
711 case IMGFMT_RGB16:
712 case IMGFMT_BGR16:
713 {
714 const int dr1= dither_2x2_8[y&1 ][0];
715 const int dg1= dither_2x2_4[y&1 ][0];
716 const int db1= dither_2x2_8[(y&1)^1][0];
717 const int dr2= dither_2x2_8[y&1 ][1];
718 const int dg2= dither_2x2_4[y&1 ][1];
719 const int db2= dither_2x2_8[(y&1)^1][1];
720 YSCALE_YUV_2_RGBX_C(uint16_t)
721 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
722 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
e3d2500f 723 }
e3d2500f 724 }
cf7d1c1a
MN
725 break;
726 case IMGFMT_RGB15:
727 case IMGFMT_BGR15:
728 {
729 const int dr1= dither_2x2_8[y&1 ][0];
730 const int dg1= dither_2x2_8[y&1 ][1];
731 const int db1= dither_2x2_8[(y&1)^1][0];
732 const int dr2= dither_2x2_8[y&1 ][1];
733 const int dg2= dither_2x2_8[y&1 ][0];
734 const int db2= dither_2x2_8[(y&1)^1][1];
735 YSCALE_YUV_2_RGBX_C(uint16_t)
736 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
737 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
e3d2500f 738 }
cf7d1c1a
MN
739 }
740 break;
741 case IMGFMT_RGB8:
742 case IMGFMT_BGR8:
743 {
744 const uint8_t * const d64= dither_8x8_73[y&7];
745 const uint8_t * const d32= dither_8x8_32[y&7];
746 YSCALE_YUV_2_RGBX_C(uint8_t)
747 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
748 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
e3d2500f 749 }
e3d2500f 750 }
cf7d1c1a
MN
751 break;
752 case IMGFMT_RGB4:
753 case IMGFMT_BGR4:
754 {
755 const uint8_t * const d64= dither_8x8_73 [y&7];
756 const uint8_t * const d128=dither_8x8_220[y&7];
757 YSCALE_YUV_2_RGBX_C(uint8_t)
799fd467 758 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]
f17457ac
MN
759 +((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);
760 }
761 }
762 break;
763 case IMGFMT_RG4B:
764 case IMGFMT_BG4B:
765 {
766 const uint8_t * const d64= dither_8x8_73 [y&7];
767 const uint8_t * const d128=dither_8x8_220[y&7];
768 YSCALE_YUV_2_RGBX_C(uint8_t)
cf7d1c1a
MN
769 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
770 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
e3d2500f 771 }
cf7d1c1a
MN
772 }
773 break;
774 case IMGFMT_RGB1:
775 case IMGFMT_BGR1:
776 {
777 const uint8_t * const d128=dither_8x8_220[y&7];
778 uint8_t *g= c->table_gU[128] + c->table_gV[128];
779 int acc=0;
780 for(i=0; i<dstW-1; i+=2){
781 int j;
782 int Y1=0;
783 int Y2=0;
784
785 for(j=0; j<lumFilterSize; j++)
786 {
787 Y1 += lumSrc[j][i] * lumFilter[j];
788 Y2 += lumSrc[j][i+1] * lumFilter[j];
789 }
790 Y1>>=19;
791 Y2>>=19;
792 if((Y1|Y2)&256)
793 {
794 if(Y1>255) Y1=255;
795 else if(Y1<0)Y1=0;
796 if(Y2>255) Y2=255;
797 else if(Y2<0)Y2=0;
798 }
799 acc+= acc + g[Y1+d128[(i+0)&7]];
800 acc+= acc + g[Y2+d128[(i+1)&7]];
801 if((i&7)==6){
802 ((uint8_t*)dest)[0]= acc;
803 ((uint8_t*)dest)++;
804 }
e3d2500f 805 }
e3d2500f 806 }
cf7d1c1a 807 break;
46de8b73 808 case IMGFMT_YUY2:
25593e29 809 YSCALE_YUV_2_PACKEDX_C(void)
46de8b73
MN
810 ((uint8_t*)dest)[2*i2+0]= Y1;
811 ((uint8_t*)dest)[2*i2+1]= U;
812 ((uint8_t*)dest)[2*i2+2]= Y2;
813 ((uint8_t*)dest)[2*i2+3]= V;
814 }
815 break;
e3d2500f
MN
816 }
817}
818
819
7630f2e0
MN
820//Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
821//Plain C versions
726a959a
MN
822#if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
823#define COMPILE_C
824#endif
825
826#ifdef CAN_COMPILE_X86_ASM
827
828#if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
829#define COMPILE_MMX
830#endif
831
832#if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
833#define COMPILE_MMX2
834#endif
835
836#if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
837#define COMPILE_3DNOW
838#endif
839#endif //CAN_COMPILE_X86_ASM
840
841#undef HAVE_MMX
842#undef HAVE_MMX2
843#undef HAVE_3DNOW
726a959a
MN
844
845#ifdef COMPILE_C
7630f2e0
MN
846#undef HAVE_MMX
847#undef HAVE_MMX2
848#undef HAVE_3DNOW
7630f2e0
MN
849#define RENAME(a) a ## _C
850#include "swscale_template.c"
726a959a 851#endif
397c035e 852
7630f2e0 853#ifdef CAN_COMPILE_X86_ASM
397c035e 854
7630f2e0
MN
855//X86 versions
856/*
857#undef RENAME
858#undef HAVE_MMX
859#undef HAVE_MMX2
860#undef HAVE_3DNOW
861#define ARCH_X86
862#define RENAME(a) a ## _X86
863#include "swscale_template.c"
1faf0867 864*/
7630f2e0 865//MMX versions
726a959a 866#ifdef COMPILE_MMX
7630f2e0
MN
867#undef RENAME
868#define HAVE_MMX
869#undef HAVE_MMX2
870#undef HAVE_3DNOW
7630f2e0
MN
871#define RENAME(a) a ## _MMX
872#include "swscale_template.c"
726a959a 873#endif
7630f2e0
MN
874
875//MMX2 versions
726a959a 876#ifdef COMPILE_MMX2
7630f2e0
MN
877#undef RENAME
878#define HAVE_MMX
879#define HAVE_MMX2
880#undef HAVE_3DNOW
7630f2e0
MN
881#define RENAME(a) a ## _MMX2
882#include "swscale_template.c"
726a959a 883#endif
7630f2e0
MN
884
885//3DNOW versions
726a959a 886#ifdef COMPILE_3DNOW
7630f2e0
MN
887#undef RENAME
888#define HAVE_MMX
889#undef HAVE_MMX2
890#define HAVE_3DNOW
7630f2e0
MN
891#define RENAME(a) a ## _3DNow
892#include "swscale_template.c"
726a959a 893#endif
7630f2e0
MN
894
895#endif //CAN_COMPILE_X86_ASM
896
897// minor note: the HAVE_xyz is messed up after that line so dont use it
d604bab9 898
d3f41512 899
6c7506de 900// old global scaler, dont use for new code
28bf81c9
MN
901// will use sws_flags from the command line
902void SwScale_YV12slice(unsigned char* src[], int srcStride[], int srcSliceY ,
903 int srcSliceH, uint8_t* dst[], int dstStride, int dstbpp,
d1fac6cf 904 int srcW, int srcH, int dstW, int dstH){
31190492 905
28bf81c9
MN
906 static SwsContext *context=NULL;
907 int dstFormat;
28bf81c9
MN
908 int dstStride3[3]= {dstStride, dstStride>>1, dstStride>>1};
909
6c7506de 910 switch(dstbpp)
28bf81c9 911 {
6c7506de
MN
912 case 8 : dstFormat= IMGFMT_Y8; break;
913 case 12: dstFormat= IMGFMT_YV12; break;
914 case 15: dstFormat= IMGFMT_BGR15; break;
915 case 16: dstFormat= IMGFMT_BGR16; break;
916 case 24: dstFormat= IMGFMT_BGR24; break;
917 case 32: dstFormat= IMGFMT_BGR32; break;
918 default: return;
919 }
920
921 if(!context) context=getSwsContextFromCmdLine(srcW, srcH, IMGFMT_YV12, dstW, dstH, dstFormat);
922
b6654a54 923 context->swScale(context, src, srcStride, srcSliceY, srcSliceH, dst, dstStride3);
6c7506de
MN
924}
925
5859233b 926void swsGetFlagsAndFilterFromCmdLine(int *flags, SwsFilter **srcFilterParam, SwsFilter **dstFilterParam)
6c7506de 927{
6c7506de 928 static int firstTime=1;
5859233b 929 *flags=0;
6c7506de 930
5521b193 931#ifdef ARCH_X86
6c7506de
MN
932 if(gCpuCaps.hasMMX)
933 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
5521b193 934#endif
6c7506de
MN
935 if(firstTime)
936 {
28bf81c9 937 firstTime=0;
5859233b 938 *flags= SWS_PRINT_INFO;
6c7506de 939 }
5859233b 940 else if(verbose>1) *flags= SWS_PRINT_INFO;
6c7506de
MN
941
942 if(src_filter.lumH) freeVec(src_filter.lumH);
943 if(src_filter.lumV) freeVec(src_filter.lumV);
944 if(src_filter.chrH) freeVec(src_filter.chrH);
945 if(src_filter.chrV) freeVec(src_filter.chrV);
946
947 if(sws_lum_gblur!=0.0){
948 src_filter.lumH= getGaussianVec(sws_lum_gblur, 3.0);
949 src_filter.lumV= getGaussianVec(sws_lum_gblur, 3.0);
950 }else{
951 src_filter.lumH= getIdentityVec();
952 src_filter.lumV= getIdentityVec();
953 }
c7f822d9 954
6c7506de
MN
955 if(sws_chr_gblur!=0.0){
956 src_filter.chrH= getGaussianVec(sws_chr_gblur, 3.0);
957 src_filter.chrV= getGaussianVec(sws_chr_gblur, 3.0);
958 }else{
959 src_filter.chrH= getIdentityVec();
960 src_filter.chrV= getIdentityVec();
961 }
5521b193 962
6c7506de
MN
963 if(sws_chr_sharpen!=0.0){
964 SwsVector *g= getConstVec(-1.0, 3);
965 SwsVector *id= getConstVec(10.0/sws_chr_sharpen, 1);
966 g->coeff[1]=2.0;
967 addVec(id, g);
968 convVec(src_filter.chrH, id);
969 convVec(src_filter.chrV, id);
970 freeVec(g);
971 freeVec(id);
972 }
5521b193 973
6c7506de
MN
974 if(sws_lum_sharpen!=0.0){
975 SwsVector *g= getConstVec(-1.0, 3);
976 SwsVector *id= getConstVec(10.0/sws_lum_sharpen, 1);
977 g->coeff[1]=2.0;
978 addVec(id, g);
979 convVec(src_filter.lumH, id);
980 convVec(src_filter.lumV, id);
981 freeVec(g);
982 freeVec(id);
983 }
c7f822d9 984
6c7506de
MN
985 if(sws_chr_hshift)
986 shiftVec(src_filter.chrH, sws_chr_hshift);
c7f822d9 987
6c7506de
MN
988 if(sws_chr_vshift)
989 shiftVec(src_filter.chrV, sws_chr_vshift);
5521b193 990
6c7506de
MN
991 normalizeVec(src_filter.chrH, 1.0);
992 normalizeVec(src_filter.chrV, 1.0);
993 normalizeVec(src_filter.lumH, 1.0);
994 normalizeVec(src_filter.lumV, 1.0);
28bf81c9 995
6c7506de
MN
996 if(verbose > 1) printVec(src_filter.chrH);
997 if(verbose > 1) printVec(src_filter.lumH);
28bf81c9
MN
998
999 switch(sws_flags)
1000 {
5859233b
MN
1001 case 0: *flags|= SWS_FAST_BILINEAR; break;
1002 case 1: *flags|= SWS_BILINEAR; break;
1003 case 2: *flags|= SWS_BICUBIC; break;
1004 case 3: *flags|= SWS_X; break;
1005 case 4: *flags|= SWS_POINT; break;
1006 case 5: *flags|= SWS_AREA; break;
81a571a8 1007 case 6: *flags|= SWS_BICUBLIN; break;
a86c461c
MN
1008 case 7: *flags|= SWS_GAUSS; break;
1009 case 8: *flags|= SWS_SINC; break;
1010 case 9: *flags|= SWS_LANCZOS; break;
1011 case 10:*flags|= SWS_SPLINE; break;
5859233b 1012 default:*flags|= SWS_BILINEAR; break;
28bf81c9 1013 }
5859233b
MN
1014
1015 *srcFilterParam= &src_filter;
1016 *dstFilterParam= NULL;
1017}
1018
1019// will use sws_flags & src_filter (from cmd line)
1020SwsContext *getSwsContextFromCmdLine(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat)
1021{
1022 int flags;
1023 SwsFilter *dstFilterParam, *srcFilterParam;
1024 swsGetFlagsAndFilterFromCmdLine(&flags, &srcFilterParam, &dstFilterParam);
28bf81c9 1025
5859233b 1026 return getSwsContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, srcFilterParam, dstFilterParam);
28bf81c9
MN
1027}
1028
a86c461c
MN
1029static double getSplineCoeff(double a, double b, double c, double d, double dist)
1030{
1031// printf("%f %f %f %f %f\n", a,b,c,d,dist);
1032 if(dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
1033 else return getSplineCoeff( 0.0,
1034 b+ 2.0*c + 3.0*d,
1035 c + 3.0*d,
1036 -b- 3.0*c - 6.0*d,
1037 dist-1.0);
1038}
6c7506de 1039
c7f822d9
MN
1040static inline void initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
1041 int srcW, int dstW, int filterAlign, int one, int flags,
1042 SwsVector *srcFilter, SwsVector *dstFilter)
28bf81c9
MN
1043{
1044 int i;
c7f822d9
MN
1045 int filterSize;
1046 int filter2Size;
1047 int minFilterSize;
1048 double *filter=NULL;
1049 double *filter2=NULL;
28bf81c9
MN
1050#ifdef ARCH_X86
1051 if(gCpuCaps.hasMMX)
1052 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
726a959a 1053#endif
31190492 1054
adeaecb9 1055 // Note the +1 is for the MMXscaler which reads over the end
6c7506de 1056 *filterPos = (int16_t*)memalign(8, (dstW+1)*sizeof(int16_t));
6c7506de 1057
28bf81c9
MN
1058 if(ABS(xInc - 0x10000) <10) // unscaled
1059 {
1060 int i;
c7f822d9
MN
1061 filterSize= 1;
1062 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
1063 for(i=0; i<dstW*filterSize; i++) filter[i]=0;
28bf81c9
MN
1064
1065 for(i=0; i<dstW; i++)
1066 {
c7f822d9
MN
1067 filter[i*filterSize]=1;
1068 (*filterPos)[i]=i;
28bf81c9
MN
1069 }
1070
1071 }
ff7ba856
MN
1072 else if(flags&SWS_POINT) // lame looking point sampling mode
1073 {
1074 int i;
1075 int xDstInSrc;
1076 filterSize= 1;
1077 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
1078
1079 xDstInSrc= xInc/2 - 0x8000;
1080 for(i=0; i<dstW; i++)
1081 {
8a01d20c 1082 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
ff7ba856
MN
1083
1084 (*filterPos)[i]= xx;
1085 filter[i]= 1.0;
1086 xDstInSrc+= xInc;
1087 }
1088 }
a86c461c 1089 else if((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
28bf81c9
MN
1090 {
1091 int i;
1092 int xDstInSrc;
c7f822d9
MN
1093 if (flags&SWS_BICUBIC) filterSize= 4;
1094 else if(flags&SWS_X ) filterSize= 4;
d8863d37 1095 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
c7f822d9 1096 filter= (double*)memalign(8, dstW*sizeof(double)*filterSize);
28bf81c9
MN
1097
1098 xDstInSrc= xInc/2 - 0x8000;
1099 for(i=0; i<dstW; i++)
1100 {
8a01d20c 1101 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
28bf81c9
MN
1102 int j;
1103
c7f822d9 1104 (*filterPos)[i]= xx;
d8863d37 1105 //Bilinear upscale / linear interpolate / Area averaging
c7f822d9 1106 for(j=0; j<filterSize; j++)
28bf81c9
MN
1107 {
1108 double d= ABS((xx<<16) - xDstInSrc)/(double)(1<<16);
1109 double coeff= 1.0 - d;
1110 if(coeff<0) coeff=0;
c7f822d9 1111 filter[i*filterSize + j]= coeff;
28bf81c9
MN
1112 xx++;
1113 }
28bf81c9
MN
1114 xDstInSrc+= xInc;
1115 }
1116 }
a86c461c 1117 else
28bf81c9 1118 {
a86c461c
MN
1119 double xDstInSrc;
1120 double sizeFactor, filterSizeInSrc;
1121 const double xInc1= (double)xInc / (double)(1<<16);
1122 int param= (flags&SWS_PARAM_MASK)>>SWS_PARAM_SHIFT;
1123
1124 if (flags&SWS_BICUBIC) sizeFactor= 4.0;
1125 else if(flags&SWS_X) sizeFactor= 8.0;
1126 else if(flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
1127 else if(flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
1128 else if(flags&SWS_LANCZOS) sizeFactor= param ? 2.0*param : 6.0;
93768378 1129 else if(flags&SWS_SINC) sizeFactor= 20.0; // infinite ;)
a86c461c
MN
1130 else if(flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
1131 else if(flags&SWS_BILINEAR) sizeFactor= 2.0;
93768378
MN
1132 else {
1133 sizeFactor= 0.0; //GCC warning killer
1134 ASSERT(0)
1135 }
a86c461c
MN
1136
1137 if(xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
1138 else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
81b7c056 1139
a86c461c
MN
1140 filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
1141 if(filterSize > srcW-2) filterSize=srcW-2;
28bf81c9 1142
a86c461c
MN
1143 filter= (double*)memalign(16, dstW*sizeof(double)*filterSize);
1144
1145 xDstInSrc= xInc1 / 2.0 - 0.5;
28bf81c9
MN
1146 for(i=0; i<dstW; i++)
1147 {
a86c461c 1148 int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
28bf81c9 1149 int j;
c7f822d9
MN
1150 (*filterPos)[i]= xx;
1151 for(j=0; j<filterSize; j++)
28bf81c9 1152 {
a86c461c 1153 double d= ABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
28bf81c9 1154 double coeff;
a86c461c 1155 if(flags & SWS_BICUBIC)
28bf81c9 1156 {
a86c461c
MN
1157 double A= param ? -param*0.01 : -0.60;
1158
28bf81c9
MN
1159 // Equation is from VirtualDub
1160 if(d<1.0)
1161 coeff = (1.0 - (A+3.0)*d*d + (A+2.0)*d*d*d);
1162 else if(d<2.0)
1163 coeff = (-4.0*A + 8.0*A*d - 5.0*A*d*d + A*d*d*d);
1164 else
1165 coeff=0.0;
1166 }
a86c461c
MN
1167/* else if(flags & SWS_X)
1168 {
1169 double p= param ? param*0.01 : 0.3;
1170 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1171 coeff*= pow(2.0, - p*d*d);
1172 }*/
1173 else if(flags & SWS_X)
1174 {
1175 double A= param ? param*0.1 : 1.0;
1176
1177 if(d<1.0)
1178 coeff = cos(d*PI);
1179 else
1180 coeff=-1.0;
1181 if(coeff<0.0) coeff= -pow(-coeff, A);
1182 else coeff= pow( coeff, A);
1183 coeff= coeff*0.5 + 0.5;
1184 }
d8863d37 1185 else if(flags & SWS_AREA)
28bf81c9 1186 {
a86c461c 1187 double srcPixelSize= 1.0/xInc1;
d8863d37
MN
1188 if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
1189 else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
1190 else coeff=0.0;
1191 }
a86c461c
MN
1192 else if(flags & SWS_GAUSS)
1193 {
1194 double p= param ? param*0.1 : 3.0;
1195 coeff = pow(2.0, - p*d*d);
1196 }
1197 else if(flags & SWS_SINC)
1198 {
1199 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1200 }
1201 else if(flags & SWS_LANCZOS)
1202 {
1203 double p= param ? param : 3.0;
1204 coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
1205 if(d>p) coeff=0;
1206 }
1207 else if(flags & SWS_BILINEAR)
28bf81c9
MN
1208 {
1209 coeff= 1.0 - d;
1210 if(coeff<0) coeff=0;
1211 }
a86c461c
MN
1212 else if(flags & SWS_SPLINE)
1213 {
1214 double p=-2.196152422706632;
1215 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
1216 }
93768378
MN
1217 else {
1218 coeff= 0.0; //GCC warning killer
1219 ASSERT(0)
1220 }
a86c461c 1221
c7f822d9 1222 filter[i*filterSize + j]= coeff;
28bf81c9
MN
1223 xx++;
1224 }
a86c461c 1225 xDstInSrc+= xInc1;
28bf81c9
MN
1226 }
1227 }
1228
c7f822d9
MN
1229 /* apply src & dst Filter to filter -> filter2
1230 free(filter);
1231 */
81b7c056 1232 ASSERT(filterSize>0)
c7f822d9
MN
1233 filter2Size= filterSize;
1234 if(srcFilter) filter2Size+= srcFilter->length - 1;
1235 if(dstFilter) filter2Size+= dstFilter->length - 1;
81b7c056 1236 ASSERT(filter2Size>0)
c7f822d9
MN
1237 filter2= (double*)memalign(8, filter2Size*dstW*sizeof(double));
1238
1239 for(i=0; i<dstW; i++)
1240 {
1241 int j;
1242 SwsVector scaleFilter;
1243 SwsVector *outVec;
1244
1245 scaleFilter.coeff= filter + i*filterSize;
1246 scaleFilter.length= filterSize;
1247
5cebb24b 1248 if(srcFilter) outVec= getConvVec(srcFilter, &scaleFilter);
c7f822d9
MN
1249 else outVec= &scaleFilter;
1250
1251 ASSERT(outVec->length == filter2Size)
1252 //FIXME dstFilter
1253
1254 for(j=0; j<outVec->length; j++)
1255 {
1256 filter2[i*filter2Size + j]= outVec->coeff[j];
1257 }
1258
1259 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1260
1261 if(outVec != &scaleFilter) freeVec(outVec);
1262 }
1263 free(filter); filter=NULL;
1264
1265 /* try to reduce the filter-size (step1 find size and shift left) */
1266 // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
1267 minFilterSize= 0;
1268 for(i=dstW-1; i>=0; i--)
1269 {
1270 int min= filter2Size;
1271 int j;
1272 double cutOff=0.0;
1273
1274 /* get rid off near zero elements on the left by shifting left */
1275 for(j=0; j<filter2Size; j++)
1276 {
1277 int k;
1278 cutOff += ABS(filter2[i*filter2Size]);
1279
1280 if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1281
1282 /* preserve Monotonicity because the core cant handle the filter otherwise */
1283 if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1284
1285 // Move filter coeffs left
1286 for(k=1; k<filter2Size; k++)
1287 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1288 filter2[i*filter2Size + k - 1]= 0.0;
1289 (*filterPos)[i]++;
1290 }
1291
1292 cutOff=0.0;
1293 /* count near zeros on the right */
1294 for(j=filter2Size-1; j>0; j--)
1295 {
1296 cutOff += ABS(filter2[i*filter2Size + j]);
1297
1298 if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1299 min--;
1300 }
1301
1302 if(min>minFilterSize) minFilterSize= min;
1303 }
1304
81b7c056 1305 ASSERT(minFilterSize > 0)
6c7506de 1306 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
81b7c056 1307 ASSERT(filterSize > 0)
6c7506de
MN
1308 filter= (double*)memalign(8, filterSize*dstW*sizeof(double));
1309 *outFilterSize= filterSize;
1310
4a53a912 1311 if(flags&SWS_PRINT_INFO)
0d9f3d85 1312 MSG_INFO("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
c7f822d9
MN
1313 /* try to reduce the filter-size (step2 reduce it) */
1314 for(i=0; i<dstW; i++)
1315 {
1316 int j;
1317
6c7506de
MN
1318 for(j=0; j<filterSize; j++)
1319 {
1320 if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
1321 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1322 }
c7f822d9 1323 }
6c7506de
MN
1324 free(filter2); filter2=NULL;
1325
c7f822d9
MN
1326
1327 //FIXME try to align filterpos if possible
1328
28bf81c9
MN
1329 //fix borders
1330 for(i=0; i<dstW; i++)
1331 {
1332 int j;
c7f822d9 1333 if((*filterPos)[i] < 0)
28bf81c9
MN
1334 {
1335 // Move filter coeffs left to compensate for filterPos
6c7506de 1336 for(j=1; j<filterSize; j++)
28bf81c9 1337 {
c7f822d9 1338 int left= MAX(j + (*filterPos)[i], 0);
6c7506de
MN
1339 filter[i*filterSize + left] += filter[i*filterSize + j];
1340 filter[i*filterSize + j]=0;
28bf81c9 1341 }
c7f822d9 1342 (*filterPos)[i]= 0;
28bf81c9
MN
1343 }
1344
6c7506de 1345 if((*filterPos)[i] + filterSize > srcW)
28bf81c9 1346 {
6c7506de 1347 int shift= (*filterPos)[i] + filterSize - srcW;
28bf81c9 1348 // Move filter coeffs right to compensate for filterPos
6c7506de 1349 for(j=filterSize-2; j>=0; j--)
28bf81c9 1350 {
6c7506de
MN
1351 int right= MIN(j + shift, filterSize-1);
1352 filter[i*filterSize +right] += filter[i*filterSize +j];
1353 filter[i*filterSize +j]=0;
28bf81c9 1354 }
6c7506de 1355 (*filterPos)[i]= srcW - filterSize;
28bf81c9
MN
1356 }
1357 }
1358
6c7506de
MN
1359 // Note the +1 is for the MMXscaler which reads over the end
1360 *outFilter= (int16_t*)memalign(8, *outFilterSize*(dstW+1)*sizeof(int16_t));
1361 memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
c7f822d9
MN
1362
1363 /* Normalize & Store in outFilter */
28bf81c9
MN
1364 for(i=0; i<dstW; i++)
1365 {
1366 int j;
1367 double sum=0;
1368 double scale= one;
6c7506de 1369 for(j=0; j<filterSize; j++)
28bf81c9 1370 {
6c7506de 1371 sum+= filter[i*filterSize + j];
28bf81c9
MN
1372 }
1373 scale/= sum;
93768378 1374 for(j=0; j<*outFilterSize; j++)
28bf81c9 1375 {
6c7506de 1376 (*outFilter)[i*(*outFilterSize) + j]= (int)(filter[i*filterSize + j]*scale);
28bf81c9
MN
1377 }
1378 }
adeaecb9
MN
1379
1380 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1381 for(i=0; i<*outFilterSize; i++)
1382 {
1383 int j= dstW*(*outFilterSize);
1384 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1385 }
c7f822d9 1386
6c7506de 1387 free(filter);
7630f2e0 1388}
31190492 1389
28bf81c9 1390#ifdef ARCH_X86
b7dc6f66 1391static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
28bf81c9 1392{
b7dc6f66
MN
1393 uint8_t *fragmentA;
1394 int imm8OfPShufW1A;
1395 int imm8OfPShufW2A;
1396 int fragmentLengthA;
1397 uint8_t *fragmentB;
1398 int imm8OfPShufW1B;
1399 int imm8OfPShufW2B;
1400 int fragmentLengthB;
1401 int fragmentPos;
28bf81c9
MN
1402
1403 int xpos, i;
1404
1405 // create an optimized horizontal scaling routine
1406
1407 //code fragment
1408
1409 asm volatile(
1410 "jmp 9f \n\t"
1411 // Begin
1412 "0: \n\t"
b7dc6f66
MN
1413 "movq (%%edx, %%eax), %%mm3 \n\t"
1414 "movd (%%ecx, %%esi), %%mm0 \n\t"
1415 "movd 1(%%ecx, %%esi), %%mm1 \n\t"
1416 "punpcklbw %%mm7, %%mm1 \n\t"
1417 "punpcklbw %%mm7, %%mm0 \n\t"
28bf81c9
MN
1418 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1419 "1: \n\t"
28bf81c9
MN
1420 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1421 "2: \n\t"
28bf81c9 1422 "psubw %%mm1, %%mm0 \n\t"
b7dc6f66 1423 "movl 8(%%ebx, %%eax), %%esi \n\t"
28bf81c9 1424 "pmullw %%mm3, %%mm0 \n\t"
28bf81c9
MN
1425 "psllw $7, %%mm1 \n\t"
1426 "paddw %%mm1, %%mm0 \n\t"
1427
1428 "movq %%mm0, (%%edi, %%eax) \n\t"
1429
1430 "addl $8, %%eax \n\t"
1431 // End
1432 "9: \n\t"
1433// "int $3\n\t"
1434 "leal 0b, %0 \n\t"
1435 "leal 1b, %1 \n\t"
1436 "leal 2b, %2 \n\t"
1437 "decl %1 \n\t"
1438 "decl %2 \n\t"
1439 "subl %0, %1 \n\t"
1440 "subl %0, %2 \n\t"
1441 "leal 9b, %3 \n\t"
1442 "subl %0, %3 \n\t"
b7dc6f66
MN
1443
1444
1445 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1446 "=r" (fragmentLengthA)
28bf81c9
MN
1447 );
1448
b7dc6f66
MN
1449 asm volatile(
1450 "jmp 9f \n\t"
1451 // Begin
1452 "0: \n\t"
1453 "movq (%%edx, %%eax), %%mm3 \n\t"
1454 "movd (%%ecx, %%esi), %%mm0 \n\t"
1455 "punpcklbw %%mm7, %%mm0 \n\t"
1456 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1457 "1: \n\t"
1458 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1459 "2: \n\t"
1460 "psubw %%mm1, %%mm0 \n\t"
1461 "movl 8(%%ebx, %%eax), %%esi \n\t"
1462 "pmullw %%mm3, %%mm0 \n\t"
1463 "psllw $7, %%mm1 \n\t"
1464 "paddw %%mm1, %%mm0 \n\t"
1465
1466 "movq %%mm0, (%%edi, %%eax) \n\t"
28bf81c9 1467
b7dc6f66
MN
1468 "addl $8, %%eax \n\t"
1469 // End
1470 "9: \n\t"
1471// "int $3\n\t"
1472 "leal 0b, %0 \n\t"
1473 "leal 1b, %1 \n\t"
1474 "leal 2b, %2 \n\t"
1475 "decl %1 \n\t"
1476 "decl %2 \n\t"
1477 "subl %0, %1 \n\t"
1478 "subl %0, %2 \n\t"
1479 "leal 9b, %3 \n\t"
1480 "subl %0, %3 \n\t"
1481
1482
1483 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1484 "=r" (fragmentLengthB)
1485 );
1486
1487 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1488 fragmentPos=0;
1489
1490 for(i=0; i<dstW/numSplits; i++)
28bf81c9
MN
1491 {
1492 int xx=xpos>>16;
1493
1494 if((i&3) == 0)
1495 {
1496 int a=0;
1497 int b=((xpos+xInc)>>16) - xx;
1498 int c=((xpos+xInc*2)>>16) - xx;
1499 int d=((xpos+xInc*3)>>16) - xx;
1500
b7dc6f66
MN
1501 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1502 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1503 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1504 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1505 filterPos[i/2]= xx;
1506
1507 if(d+1<4)
1508 {
1509 int maxShift= 3-(d+1);
1510 int shift=0;
1511
1512 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1513
1514 funnyCode[fragmentPos + imm8OfPShufW1B]=
1515 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1516 funnyCode[fragmentPos + imm8OfPShufW2B]=
1517 a | (b<<2) | (c<<4) | (d<<6);
1518
1519 if(i+3>=dstW) shift=maxShift; //avoid overread
1520 else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1521
1522 if(shift && i>=shift)
1523 {
1524 funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1525 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1526 filterPos[i/2]-=shift;
1527 }
1528
1529 fragmentPos+= fragmentLengthB;
1530 }
1531 else
1532 {
1533 int maxShift= 3-d;
1534 int shift=0;
1535
1536 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
28bf81c9 1537
b7dc6f66
MN
1538 funnyCode[fragmentPos + imm8OfPShufW1A]=
1539 funnyCode[fragmentPos + imm8OfPShufW2A]=
1540 a | (b<<2) | (c<<4) | (d<<6);
28bf81c9 1541
b7dc6f66
MN
1542 if(i+4>=dstW) shift=maxShift; //avoid overread
1543 else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
28bf81c9 1544
b7dc6f66
MN
1545 if(shift && i>=shift)
1546 {
1547 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1548 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1549 filterPos[i/2]-=shift;
1550 }
1551
1552 fragmentPos+= fragmentLengthA;
1553 }
1554
1555 funnyCode[fragmentPos]= RET;
28bf81c9
MN
1556 }
1557 xpos+=xInc;
1558 }
b7dc6f66 1559 filterPos[i/2]= xpos>>16; // needed to jump to the next part
28bf81c9
MN
1560}
1561#endif // ARCH_X86
1562
1563//FIXME remove
31190492 1564void SwScale_Init(){
28bf81c9
MN
1565}
1566
1567static void globalInit(){
31190492
A
1568 // generating tables:
1569 int i;
c1b0bfb4
MN
1570 for(i=0; i<768; i++){
1571 int c= MIN(MAX(i-256, 0), 255);
1572 clip_table[i]=c;
b18ea156 1573 }
c1b0bfb4 1574
28bf81c9
MN
1575cpuCaps= gCpuCaps;
1576
1577#ifdef RUNTIME_CPUDETECT
1578#ifdef CAN_COMPILE_X86_ASM
1579 // ordered per speed fasterst first
1580 if(gCpuCaps.hasMMX2)
1581 swScale= swScale_MMX2;
1582 else if(gCpuCaps.has3DNow)
7f56a527 1583 swScale= swScale_3DNow;
28bf81c9
MN
1584 else if(gCpuCaps.hasMMX)
1585 swScale= swScale_MMX;
1586 else
1587 swScale= swScale_C;
1588
1589#else
1590 swScale= swScale_C;
1591 cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1592#endif
1593#else //RUNTIME_CPUDETECT
1594#ifdef HAVE_MMX2
1595 swScale= swScale_MMX2;
1596 cpuCaps.has3DNow = 0;
1597#elif defined (HAVE_3DNOW)
7f56a527 1598 swScale= swScale_3DNow;
28bf81c9
MN
1599 cpuCaps.hasMMX2 = 0;
1600#elif defined (HAVE_MMX)
1601 swScale= swScale_MMX;
1602 cpuCaps.hasMMX2 = cpuCaps.has3DNow = 0;
1603#else
1604 swScale= swScale_C;
1605 cpuCaps.hasMMX2 = cpuCaps.hasMMX = cpuCaps.has3DNow = 0;
1606#endif
1607#endif //!RUNTIME_CPUDETECT
31190492 1608}
7630f2e0 1609
0d9f3d85
A
1610static void PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1611 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1612 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1613 /* Copy Y plane */
1614 if(dstStride[0]==srcStride[0])
1615 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1616 else
1617 {
1618 int i;
1619 uint8_t *srcPtr= src[0];
1620 uint8_t *dstPtr= dst;
1621 for(i=0; i<srcSliceH; i++)
1622 {
1623 memcpy(dstPtr, srcPtr, srcStride[0]);
1624 srcPtr+= srcStride[0];
1625 dstPtr+= dstStride[0];
1626 }
1627 }
1628 dst = dstParam[1] + dstStride[1]*srcSliceY;
1629 if(c->srcFormat==IMGFMT_YV12)
1630 interleaveBytes( src[1],src[2],dst,c->srcW,srcSliceH,srcStride[1],srcStride[2],dstStride[0] );
1631 else /* I420 & IYUV */
1632 interleaveBytes( src[2],src[1],dst,c->srcW,srcSliceH,srcStride[2],srcStride[1],dstStride[0] );
1633}
1634
44c1035c 1635static void PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
0d9f3d85
A
1636 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1637 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1638
1639 if(c->srcFormat==IMGFMT_YV12)
1640 yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1641 else /* I420 & IYUV */
1642 yv12toyuy2( src[0],src[2],src[1],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1643}
1644
e09d12f4
MN
1645/* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
1646static void rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1647 int srcSliceH, uint8_t* dst[], int dstStride[]){
1648 const int srcFormat= c->srcFormat;
1649 const int dstFormat= c->dstFormat;
1650 const int srcBpp= ((srcFormat&0xFF) + 7)>>3;
1651 const int dstBpp= ((dstFormat&0xFF) + 7)>>3;
1652 const int srcId= (srcFormat&0xFF)>>2; // 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8
1653 const int dstId= (dstFormat&0xFF)>>2;
1654 void (*conv)(const uint8_t *src, uint8_t *dst, unsigned src_size)=NULL;
1655
1656 /* BGR -> BGR */
20380eb8
MN
1657 if( (isBGR(srcFormat) && isBGR(dstFormat))
1658 || (isRGB(srcFormat) && isRGB(dstFormat))){
e09d12f4
MN
1659 switch(srcId | (dstId<<4)){
1660 case 0x34: conv= rgb16to15; break;
1661 case 0x36: conv= rgb24to15; break;
1662 case 0x38: conv= rgb32to15; break;
1663 case 0x43: conv= rgb15to16; break;
1664 case 0x46: conv= rgb24to16; break;
1665 case 0x48: conv= rgb32to16; break;
1666 case 0x63: conv= rgb15to24; break;
1667 case 0x64: conv= rgb16to24; break;
1668 case 0x68: conv= rgb32to24; break;
1669 case 0x83: conv= rgb15to32; break;
1670 case 0x84: conv= rgb16to32; break;
1671 case 0x86: conv= rgb24to32; break;
1672 default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
1673 vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
b935781b 1674 }
20380eb8
MN
1675 }else if( (isBGR(srcFormat) && isRGB(dstFormat))
1676 || (isRGB(srcFormat) && isBGR(dstFormat))){
e09d12f4
MN
1677 switch(srcId | (dstId<<4)){
1678 case 0x33: conv= rgb15tobgr15; break;
1679 case 0x34: conv= rgb16tobgr15; break;
1680 case 0x36: conv= rgb24tobgr15; break;
1681 case 0x38: conv= rgb32tobgr15; break;
1682 case 0x43: conv= rgb15tobgr16; break;
1683 case 0x44: conv= rgb16tobgr16; break;
1684 case 0x46: conv= rgb24tobgr16; break;
1685 case 0x48: conv= rgb32tobgr16; break;
1686 case 0x63: conv= rgb15tobgr24; break;
1687 case 0x64: conv= rgb16tobgr24; break;
1688 case 0x66: conv= rgb24tobgr24; break;
1689 case 0x68: conv= rgb32tobgr24; break;
1690 case 0x83: conv= rgb15tobgr32; break;
1691 case 0x84: conv= rgb16tobgr32; break;
1692 case 0x86: conv= rgb24tobgr32; break;
1693 case 0x88: conv= rgb32tobgr32; break;
1694 default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
1695 vo_format_name(srcFormat), vo_format_name(dstFormat)); break;
0d9f3d85 1696 }
20380eb8
MN
1697 }else{
1698 MSG_ERR("swScaler: internal error %s -> %s converter\n",
1699 vo_format_name(srcFormat), vo_format_name(dstFormat));
e09d12f4 1700 }
20380eb8 1701
e09d12f4
MN
1702 if(dstStride[0]*srcBpp == srcStride[0]*dstBpp)
1703 conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
0d9f3d85
A
1704 else
1705 {
1706 int i;
1707 uint8_t *srcPtr= src[0];
1708 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1709
1710 for(i=0; i<srcSliceH; i++)
1711 {
e09d12f4 1712 conv(srcPtr, dstPtr, c->srcW*srcBpp);
0d9f3d85
A
1713 srcPtr+= srcStride[0];
1714 dstPtr+= dstStride[0];
1715 }
1716 }
1717}
1718
ec22603f
MN
1719static void bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1720 int srcSliceH, uint8_t* dst[], int dstStride[]){
1721
1722 rgb24toyv12(
1723 src[0],
1724 dst[0]+ srcSliceY *dstStride[0],
1725 dst[1]+(srcSliceY>>1)*dstStride[1],
1726 dst[2]+(srcSliceY>>1)*dstStride[2],
1727 c->srcW, srcSliceH,
1728 dstStride[0], dstStride[1], srcStride[0]);
1729}
1730
b241cbf2
MN
1731static void yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1732 int srcSliceH, uint8_t* dst[], int dstStride[]){
1733 int i;
1734
1735 /* copy Y */
1736 if(srcStride[0]==dstStride[0])
1737 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1738 else{
1739 uint8_t *srcPtr= src[0];
1740 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1741
1742 for(i=0; i<srcSliceH; i++)
1743 {
1744 memcpy(dstPtr, srcPtr, c->srcW);
1745 srcPtr+= srcStride[0];
1746 dstPtr+= dstStride[0];
1747 }
1748 }
1749
1750 if(c->dstFormat==IMGFMT_YV12){
1751 planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1752 planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1753 }else{
1754 planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1755 planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1756 }
1757}
1758
44c1035c
MN
1759/**
1760 * bring pointers in YUV order instead of YVU
1761 */
5427e242 1762inline void sws_orderYUV(int format, uint8_t * sortedP[], int sortedStride[], uint8_t * p[], int stride[]){
d80e2fa2
MN
1763 if(format == IMGFMT_YV12 || format == IMGFMT_YVU9
1764 || format == IMGFMT_444P || format == IMGFMT_422P || format == IMGFMT_411P){
44c1035c
MN
1765 sortedP[0]= p[0];
1766 sortedP[1]= p[1];
1767 sortedP[2]= p[2];
c7a810cc
MN
1768 sortedStride[0]= stride[0];
1769 sortedStride[1]= stride[1];
1770 sortedStride[2]= stride[2];
44c1035c 1771 }
c7a810cc 1772 else if(isPacked(format) || isGray(format))
44c1035c
MN
1773 {
1774 sortedP[0]= p[0];
1775 sortedP[1]=
1776 sortedP[2]= NULL;
c7a810cc 1777 sortedStride[0]= stride[0];
44c1035c
MN
1778 sortedStride[1]=
1779 sortedStride[2]= 0;
1780 }
e09d12f4 1781 else if(format == IMGFMT_I420)
44c1035c
MN
1782 {
1783 sortedP[0]= p[0];
1784 sortedP[1]= p[2];
1785 sortedP[2]= p[1];
c7a810cc
MN
1786 sortedStride[0]= stride[0];
1787 sortedStride[1]= stride[2];
1788 sortedStride[2]= stride[1];
e09d12f4
MN
1789 }else{
1790 MSG_ERR("internal error in orderYUV\n");
44c1035c
MN
1791 }
1792}
b935781b 1793
b6654a54
MN
1794/* unscaled copy like stuff (assumes nearly identical formats) */
1795static void simpleCopy(SwsContext *c, uint8_t* srcParam[], int srcStrideParam[], int srcSliceY,
44c1035c 1796 int srcSliceH, uint8_t* dstParam[], int dstStrideParam[]){
b6654a54
MN
1797
1798 int srcStride[3];
44c1035c 1799 int dstStride[3];
b6654a54
MN
1800 uint8_t *src[3];
1801 uint8_t *dst[3];
1802
5427e242
MN
1803 sws_orderYUV(c->srcFormat, src, srcStride, srcParam, srcStrideParam);
1804 sws_orderYUV(c->dstFormat, dst, dstStride, dstParam, dstStrideParam);
b6654a54
MN
1805
1806 if(isPacked(c->srcFormat))
1807 {
1808 if(dstStride[0]==srcStride[0])
1809 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1810 else
1811 {
1812 int i;
1813 uint8_t *srcPtr= src[0];
1814 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
a861d4d7
MN
1815 int length=0;
1816
1817 /* universal length finder */
9bd8bd1a
MN
1818 while(length+c->srcW <= ABS(dstStride[0])
1819 && length+c->srcW <= ABS(srcStride[0])) length+= c->srcW;
a861d4d7 1820 ASSERT(length!=0);
b6654a54
MN
1821
1822 for(i=0; i<srcSliceH; i++)
1823 {
1824 memcpy(dstPtr, srcPtr, length);
1825 srcPtr+= srcStride[0];
1826 dstPtr+= dstStride[0];
1827 }
1828 }
1829 }
1830 else
44c1035c 1831 { /* Planar YUV or gray */
b6654a54
MN
1832 int plane;
1833 for(plane=0; plane<3; plane++)
1834 {
e616aa93
MN
1835 int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1836 int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1837 int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
44c1035c
MN
1838
1839 if((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
0d9f3d85 1840 {
44c1035c 1841 if(!isGray(c->dstFormat))
e616aa93 1842 memset(dst[plane], 128, dstStride[plane]*height);
0d9f3d85 1843 }
b6654a54
MN
1844 else
1845 {
44c1035c
MN
1846 if(dstStride[plane]==srcStride[plane])
1847 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1848 else
b6654a54 1849 {
44c1035c
MN
1850 int i;
1851 uint8_t *srcPtr= src[plane];
1852 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1853 for(i=0; i<height; i++)
1854 {
1855 memcpy(dstPtr, srcPtr, length);
1856 srcPtr+= srcStride[plane];
1857 dstPtr+= dstStride[plane];
1858 }
b6654a54
MN
1859 }
1860 }
1861 }
1862 }
37079906 1863}
28bf81c9 1864
44c1035c 1865static int remove_dup_fourcc(int fourcc)
0d9f3d85
A
1866{
1867 switch(fourcc)
1868 {
1869 case IMGFMT_IYUV: return IMGFMT_I420;
1870 case IMGFMT_Y8 : return IMGFMT_Y800;
0c51ef97 1871 case IMGFMT_IF09: return IMGFMT_YVU9;
0d9f3d85
A
1872 default: return fourcc;
1873 }
1874}
1875
c7a810cc
MN
1876static void getSubSampleFactors(int *h, int *v, int format){
1877 switch(format){
7322a67c 1878 case IMGFMT_UYVY:
c7a810cc
MN
1879 case IMGFMT_YUY2:
1880 *h=1;
1881 *v=0;
1882 break;
1883 case IMGFMT_YV12:
1884 case IMGFMT_I420:
e616aa93 1885 case IMGFMT_Y800: //FIXME remove after different subsamplings are fully implemented
c7a810cc
MN
1886 *h=1;
1887 *v=1;
1888 break;
1889 case IMGFMT_YVU9:
1890 *h=2;
1891 *v=2;
1892 break;
d80e2fa2
MN
1893 case IMGFMT_444P:
1894 *h=0;
1895 *v=0;
1896 break;
1897 case IMGFMT_422P:
1898 *h=1;
1899 *v=0;
1900 break;
1901 case IMGFMT_411P:
1902 *h=2;
1903 *v=0;
1904 break;
c7a810cc
MN
1905 default:
1906 *h=0;
1907 *v=0;
1908 break;
1909 }
1910}
1911
5427e242
MN
1912static uint16_t roundToInt16(int64_t f){
1913 int r= (f + (1<<15))>>16;
1914 if(r<-0x7FFF) return 0x8000;
1915 else if(r> 0x7FFF) return 0x7FFF;
1916 else return r;
0481412a
MN
1917}
1918
1919/**
5427e242 1920 * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
0481412a 1921 * @param fullRange if 1 then the luma range is 0..255 if 0 its 16..235
5427e242 1922 * @return -1 if not supported
0481412a 1923 */
5427e242
MN
1924int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
1925 int64_t crv = inv_table[0];
1926 int64_t cbu = inv_table[1];
1927 int64_t cgu = -inv_table[2];
1928 int64_t cgv = -inv_table[3];
1929 int64_t cy = 1<<16;
1930 int64_t oy = 0;
1931
1932 if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1933 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
1934 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
1935
1936 c->brightness= brightness;
1937 c->contrast = contrast;
1938 c->saturation= saturation;
1939 c->srcRange = srcRange;
1940 c->dstRange = dstRange;
0481412a
MN
1941
1942 c->uOffset= 0x0400040004000400LL;
1943 c->vOffset= 0x0400040004000400LL;
1944
5427e242
MN
1945 if(!srcRange){
1946 cy= (cy*255) / 219;
1947 oy= 16<<16;
0481412a
MN
1948 }
1949
5427e242
MN
1950 cy = (cy *contrast )>>16;
1951 crv= (crv*contrast * saturation)>>32;
1952 cbu= (cbu*contrast * saturation)>>32;
1953 cgu= (cgu*contrast * saturation)>>32;
1954 cgv= (cgv*contrast * saturation)>>32;
0481412a 1955
5427e242 1956 oy -= 256*brightness;
0481412a
MN
1957
1958 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
1959 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
1960 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
1961 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
1962 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
1963 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
5427e242
MN
1964
1965 yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
1966 //FIXME factorize
1967
1968 return 0;
1969}
1970
1971/**
1972 * @return -1 if not supported
1973 */
1974int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
1975 if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1976
1977 *inv_table = c->srcColorspaceTable;
1978 *table = c->dstColorspaceTable;
1979 *srcRange = c->srcRange;
1980 *dstRange = c->dstRange;
1981 *brightness= c->brightness;
1982 *contrast = c->contrast;
1983 *saturation= c->saturation;
1984
1985 return 0;
0481412a
MN
1986}
1987
28bf81c9
MN
1988SwsContext *getSwsContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
1989 SwsFilter *srcFilter, SwsFilter *dstFilter){
1990
28bf81c9
MN
1991 SwsContext *c;
1992 int i;
37079906 1993 int usesFilter;
e09d12f4 1994 int unscaled, needsDither;
c7f822d9 1995 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
5cebb24b
MN
1996#ifdef ARCH_X86
1997 if(gCpuCaps.hasMMX)
1998 asm volatile("emms\n\t"::: "memory");
1999#endif
28bf81c9 2000 if(swScale==NULL) globalInit();
e616aa93 2001//srcFormat= IMGFMT_Y800;
e09d12f4 2002//dstFormat= IMGFMT_Y800;
6ff0ad6b 2003 /* avoid dupplicate Formats, so we dont need to check to much */
0d9f3d85
A
2004 srcFormat = remove_dup_fourcc(srcFormat);
2005 dstFormat = remove_dup_fourcc(dstFormat);
44c1035c
MN
2006
2007 unscaled = (srcW == dstW && srcH == dstH);
e09d12f4
MN
2008 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2009 && (dstFormat&0xFF)<24
2010 && ((dstFormat&0xFF)<(srcFormat&0xFF) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
44c1035c
MN
2011
2012 if(!isSupportedIn(srcFormat))
b81cf274 2013 {
44c1035c
MN
2014 MSG_ERR("swScaler: %s is not supported as input format\n", vo_format_name(srcFormat));
2015 return NULL;
2016 }
2017 if(!isSupportedOut(dstFormat))
2018 {
2019 MSG_ERR("swScaler: %s is not supported as output format\n", vo_format_name(dstFormat));
2020 return NULL;
b81cf274 2021 }
44c1035c 2022
28bf81c9 2023 /* sanity check */
b81cf274
MN
2024 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
2025 {
0d9f3d85 2026 MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
b81cf274
MN
2027 srcW, srcH, dstW, dstH);
2028 return NULL;
2029 }
28bf81c9 2030
c7f822d9
MN
2031 if(!dstFilter) dstFilter= &dummyFilter;
2032 if(!srcFilter) srcFilter= &dummyFilter;
2033
28bf81c9 2034 c= memalign(64, sizeof(SwsContext));
c7f822d9 2035 memset(c, 0, sizeof(SwsContext));
28bf81c9
MN
2036
2037 c->srcW= srcW;
2038 c->srcH= srcH;
2039 c->dstW= dstW;
2040 c->dstH= dstH;
5521b193
MN
2041 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2042 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
28bf81c9
MN
2043 c->flags= flags;
2044 c->dstFormat= dstFormat;
2045 c->srcFormat= srcFormat;
2046
37079906
MN
2047 usesFilter=0;
2048 if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesFilter=1;
2049 if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesFilter=1;
2050 if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesFilter=1;
2051 if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesFilter=1;
2052 if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesFilter=1;
2053 if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesFilter=1;
2054 if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesFilter=1;
2055 if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesFilter=1;
e616aa93
MN
2056
2057 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2058 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2059
2060 // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
2061 if((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2062
5859233b
MN
2063 // drop some chroma lines if the user wants it
2064 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2065 c->chrSrcVSubSample+= c->vChrDrop;
e616aa93 2066
5859233b 2067 // drop every 2. pixel for chroma calculation unless user wants full chroma
e616aa93
MN
2068 if((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP))
2069 c->chrSrcHSubSample=1;
2070
2071 c->chrIntHSubSample= c->chrDstHSubSample;
2072 c->chrIntVSubSample= c->chrSrcVSubSample;
5427e242 2073
e616aa93
MN
2074 // note the -((-x)>>y) is so that we allways round toward +inf
2075 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2076 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2077 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2078 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
5427e242
MN
2079
2080 sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], 0, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, 0, 0, 1<<16, 1<<16);
cf7d1c1a 2081
b935781b 2082 /* unscaled special Cases */
44c1035c 2083 if(unscaled && !usesFilter)
37079906 2084 {
0d9f3d85
A
2085 /* yv12_to_nv12 */
2086 if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_NV12)
2087 {
2088 c->swScale= PlanarToNV12Wrapper;
0d9f3d85 2089 }
37079906 2090 /* yuv2bgr */
5427e242 2091 if((srcFormat==IMGFMT_YV12 || srcFormat==IMGFMT_I420 || srcFormat==IMGFMT_422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
37079906 2092 {
5427e242 2093 c->swScale= yuv2rgb_get_func_ptr(c);
37079906 2094 }
b241cbf2
MN
2095
2096 if( srcFormat==IMGFMT_YVU9 && (dstFormat==IMGFMT_YV12 || dstFormat==IMGFMT_I420) )
2097 {
2098 c->swScale= yvu9toyv12Wrapper;
b241cbf2
MN
2099 }
2100
ec22603f
MN
2101 /* bgr24toYV12 */
2102 if(srcFormat==IMGFMT_BGR24 && dstFormat==IMGFMT_YV12)
ec22603f 2103 c->swScale= bgr24toyv12Wrapper;
e09d12f4
MN
2104
2105 /* rgb/bgr -> rgb/bgr (no dither needed forms) */
2106 if( (isBGR(srcFormat) || isRGB(srcFormat))
2107 && (isBGR(dstFormat) || isRGB(dstFormat))
2108 && !needsDither)
2109 c->swScale= rgb2rgbWrapper;
2110
2111 /* LQ converters if -sws 0 or -sws 4*/
2112 if(c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2113 /* rgb/bgr -> rgb/bgr (dither needed forms) */
2114 if( (isBGR(srcFormat) || isRGB(srcFormat))
2115 && (isBGR(dstFormat) || isRGB(dstFormat))
2116 && needsDither)
2117 c->swScale= rgb2rgbWrapper;
2ce486d8
MN
2118
2119 /* yv12_to_yuy2 */
2120 if((srcFormat == IMGFMT_YV12||srcFormat==IMGFMT_I420)&&dstFormat == IMGFMT_YUY2)
2121 {
2122 c->swScale= PlanarToYuy2Wrapper;
2ce486d8 2123 }
e09d12f4 2124 }
ec22603f 2125
20380eb8
MN
2126 /* simple copy */
2127 if( srcFormat == dstFormat
2128 || (srcFormat==IMGFMT_YV12 && dstFormat==IMGFMT_I420)
2129 || (srcFormat==IMGFMT_I420 && dstFormat==IMGFMT_YV12)
2130 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2131 || (isPlanarYUV(dstFormat) && isGray(srcFormat))
2132 )
2133 {
2134 c->swScale= simpleCopy;
2135 }
2136
e09d12f4 2137 if(c->swScale){
ec22603f 2138 if(flags&SWS_PRINT_INFO)
0d9f3d85 2139 MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
ec22603f
MN
2140 vo_format_name(srcFormat), vo_format_name(dstFormat));
2141 return c;
2142 }
37079906
MN
2143 }
2144
28bf81c9
MN
2145 if(cpuCaps.hasMMX2)
2146 {
2147 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2148 if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2149 {
2150 if(flags&SWS_PRINT_INFO)
0d9f3d85 2151 MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
28bf81c9
MN
2152 }
2153 }
2154 else
2155 c->canMMX2BeUsed=0;
2156
1e621b18
MN
2157 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2158 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2159
28bf81c9
MN
2160 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2161 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2162 // n-2 is the last chrominance sample available
2163 // this is not perfect, but noone shuld notice the difference, the more correct variant
2164 // would be like the vertical one, but that would require some special code for the
2165 // first and last pixel
2166 if(flags&SWS_FAST_BILINEAR)
2167 {
1e621b18
MN
2168 if(c->canMMX2BeUsed)
2169 {
2170 c->lumXInc+= 20;
2171 c->chrXInc+= 20;
2172 }
28bf81c9 2173 //we dont use the x86asm scaler if mmx is available
1e621b18
MN
2174 else if(cpuCaps.hasMMX)
2175 {
2176 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2177 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2178 }
28bf81c9
MN
2179 }
2180
28bf81c9
MN
2181 /* precalculate horizontal scaler filter coefficients */
2182 {
2183 const int filterAlign= cpuCaps.hasMMX ? 4 : 1;
2184
c7f822d9 2185 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
81a571a8
MN
2186 srcW , dstW, filterAlign, 1<<14,
2187 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
c7f822d9
MN
2188 srcFilter->lumH, dstFilter->lumH);
2189 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
81a571a8
MN
2190 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2191 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
c7f822d9 2192 srcFilter->chrH, dstFilter->chrH);
28bf81c9
MN
2193
2194#ifdef ARCH_X86
2195// cant downscale !!!
2196 if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2197 {
b7dc6f66
MN
2198 c->lumMmx2Filter = (int16_t*)memalign(8, (dstW /8+8)*sizeof(int16_t));
2199 c->chrMmx2Filter = (int16_t*)memalign(8, (c->chrDstW /4+8)*sizeof(int16_t));
2200 c->lumMmx2FilterPos= (int32_t*)memalign(8, (dstW /2/8+8)*sizeof(int32_t));
2201 c->chrMmx2FilterPos= (int32_t*)memalign(8, (c->chrDstW/2/4+8)*sizeof(int32_t));
2202
2203 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2204 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
28bf81c9
MN
2205 }
2206#endif
2207 } // Init Horizontal stuff
2208
2209
2210
2211 /* precalculate vertical scaler filter coefficients */
c7f822d9 2212 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
81a571a8
MN
2213 srcH , dstH, 1, (1<<12)-4,
2214 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
c7f822d9
MN
2215 srcFilter->lumV, dstFilter->lumV);
2216 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
81a571a8
MN
2217 c->chrSrcH, c->chrDstH, 1, (1<<12)-4,
2218 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2219 srcFilter->chrV, dstFilter->chrV);
28bf81c9
MN
2220
2221 // Calculate Buffer Sizes so that they wont run out while handling these damn slices
2222 c->vLumBufSize= c->vLumFilterSize;
2223 c->vChrBufSize= c->vChrFilterSize;
2224 for(i=0; i<dstH; i++)
2225 {
2226 int chrI= i*c->chrDstH / dstH;
2227 int nextSlice= MAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
e616aa93
MN
2228 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2229 nextSlice&= ~3; // Slices start at boundaries which are divisable through 4
28bf81c9
MN
2230 if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2231 c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
e616aa93
MN
2232 if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2233 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
28bf81c9
MN
2234 }
2235
2236 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
c7f822d9
MN
2237 c->lumPixBuf= (int16_t**)memalign(4, c->vLumBufSize*2*sizeof(int16_t*));
2238 c->chrPixBuf= (int16_t**)memalign(4, c->vChrBufSize*2*sizeof(int16_t*));
6c7506de 2239 //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
2240 for(i=0; i<c->vLumBufSize; i++)
2241 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= (uint16_t*)memalign(8, 4000);
2242 for(i=0; i<c->vChrBufSize; i++)
2243 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= (uint16_t*)memalign(8, 8000);
2244
2245 //try to avoid drawing green stuff between the right end and the stride end
2246 for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
2247 for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
2248
2249 ASSERT(c->chrDstH <= dstH)
28bf81c9 2250
28bf81c9
MN
2251 if(flags&SWS_PRINT_INFO)
2252 {
2253#ifdef DITHER1XBPP
5521b193
MN
2254 char *dither= " dithered";
2255#else
2256 char *dither= "";
28bf81c9
MN
2257#endif
2258 if(flags&SWS_FAST_BILINEAR)
0d9f3d85 2259 MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
28bf81c9 2260 else if(flags&SWS_BILINEAR)
0d9f3d85 2261 MSG_INFO("\nSwScaler: BILINEAR scaler, ");
28bf81c9 2262 else if(flags&SWS_BICUBIC)
0d9f3d85 2263 MSG_INFO("\nSwScaler: BICUBIC scaler, ");
1e621b18 2264 else if(flags&SWS_X)
0d9f3d85 2265 MSG_INFO("\nSwScaler: Experimental scaler, ");
ff7ba856 2266 else if(flags&SWS_POINT)
0d9f3d85 2267 MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
d8863d37 2268 else if(flags&SWS_AREA)
0d9f3d85 2269 MSG_INFO("\nSwScaler: Area Averageing scaler, ");
81a571a8 2270 else if(flags&SWS_BICUBLIN)
a86c461c
MN
2271 MSG_INFO("\nSwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
2272 else if(flags&SWS_GAUSS)
2273 MSG_INFO("\nSwScaler: Gaussian scaler, ");
2274 else if(flags&SWS_SINC)
2275 MSG_INFO("\nSwScaler: Sinc scaler, ");
2276 else if(flags&SWS_LANCZOS)
2277 MSG_INFO("\nSwScaler: Lanczos scaler, ");
2278 else if(flags&SWS_SPLINE)
2279 MSG_INFO("\nSwScaler: Bicubic spline scaler, ");
28bf81c9 2280 else
0d9f3d85 2281 MSG_INFO("\nSwScaler: ehh flags invalid?! ");
28bf81c9 2282
0d9f3d85
A
2283 if(dstFormat==IMGFMT_BGR15 || dstFormat==IMGFMT_BGR16)
2284 MSG_INFO("from %s to%s %s ",
2285 vo_format_name(srcFormat), dither, vo_format_name(dstFormat));
2286 else
2287 MSG_INFO("from %s to %s ",
2288 vo_format_name(srcFormat), vo_format_name(dstFormat));
28bf81c9
MN
2289
2290 if(cpuCaps.hasMMX2)
0d9f3d85 2291 MSG_INFO("using MMX2\n");
28bf81c9 2292 else if(cpuCaps.has3DNow)
0d9f3d85 2293 MSG_INFO("using 3DNOW\n");
28bf81c9 2294 else if(cpuCaps.hasMMX)
0d9f3d85 2295 MSG_INFO("using MMX\n");
28bf81c9 2296 else
0d9f3d85 2297 MSG_INFO("using C\n");
28bf81c9
MN
2298 }
2299
a749913f 2300 if((flags & SWS_PRINT_INFO) && verbose>0)
28bf81c9
MN
2301 {
2302 if(cpuCaps.hasMMX)
2303 {
2304 if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
0d9f3d85 2305 MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
28bf81c9
MN
2306 else
2307 {
2308 if(c->hLumFilterSize==4)
0d9f3d85 2309 MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
28bf81c9 2310 else if(c->hLumFilterSize==8)
0d9f3d85 2311 MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
28bf81c9 2312 else
0d9f3d85 2313 MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
28bf81c9
MN
2314
2315 if(c->hChrFilterSize==4)
0d9f3d85 2316 MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
28bf81c9 2317 else if(c->hChrFilterSize==8)
0d9f3d85 2318 MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
28bf81c9 2319 else
0d9f3d85 2320 MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
28bf81c9
MN
2321 }
2322 }
2323 else
2324 {
2325#ifdef ARCH_X86
0d9f3d85 2326 MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
28bf81c9
MN
2327#else
2328 if(flags & SWS_FAST_BILINEAR)
0d9f3d85 2329 MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
28bf81c9 2330 else
0d9f3d85 2331 MSG_V("SwScaler: using C scaler for horizontal scaling\n");
28bf81c9
MN
2332#endif
2333 }
6c7506de 2334 if(isPlanarYUV(dstFormat))
28bf81c9
MN
2335 {
2336 if(c->vLumFilterSize==1)
0d9f3d85 2337 MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
28bf81c9 2338 else
0d9f3d85 2339 MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", cpuCaps.hasMMX ? "MMX" : "C");
28bf81c9
MN
2340 }
2341 else
2342 {
2343 if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
0d9f3d85 2344 MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
28bf81c9
MN
2345 "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",cpuCaps.hasMMX ? "MMX" : "C");
2346 else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
0d9f3d85 2347 MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
28bf81c9 2348 else
0d9f3d85 2349 MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", cpuCaps.hasMMX ? "MMX" : "C");
28bf81c9
MN
2350 }
2351
2352 if(dstFormat==IMGFMT_BGR24)
0d9f3d85 2353 MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
28bf81c9 2354 cpuCaps.hasMMX2 ? "MMX2" : (cpuCaps.hasMMX ? "MMX" : "C"));
fd284805 2355 else if(dstFormat==IMGFMT_BGR32)
0d9f3d85 2356 MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
fd284805 2357 else if(dstFormat==IMGFMT_BGR16)
0d9f3d85 2358 MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
fd284805 2359 else if(dstFormat==IMGFMT_BGR15)
0d9f3d85 2360 MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", cpuCaps.hasMMX ? "MMX" : "C");
28bf81c9 2361
0d9f3d85 2362 MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
28bf81c9 2363 }
1e621b18
MN
2364 if((flags & SWS_PRINT_INFO) && verbose>1)
2365 {
0d9f3d85 2366 MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1e621b18 2367 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
0d9f3d85 2368 MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1e621b18
MN
2369 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2370 }
37079906
MN
2371
2372 c->swScale= swScale;
28bf81c9
MN
2373 return c;
2374}
2375
2376/**
2377 * returns a normalized gaussian curve used to filter stuff
2378 * quality=3 is high quality, lowwer is lowwer quality
2379 */
c7f822d9
MN
2380
2381SwsVector *getGaussianVec(double variance, double quality){
28bf81c9
MN
2382 const int length= (int)(variance*quality + 0.5) | 1;
2383 int i;
2384 double *coeff= memalign(sizeof(double), length*sizeof(double));
2385 double middle= (length-1)*0.5;
c7f822d9
MN
2386 SwsVector *vec= malloc(sizeof(SwsVector));
2387
2388 vec->coeff= coeff;
2389 vec->length= length;
28bf81c9
MN
2390
2391 for(i=0; i<length; i++)
2392 {
2393 double dist= i-middle;
2394 coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
2395 }
2396
c7f822d9
MN
2397 normalizeVec(vec, 1.0);
2398
2399 return vec;
28bf81c9
MN
2400}
2401
5521b193
MN
2402SwsVector *getConstVec(double c, int length){
2403 int i;
2404 double *coeff= memalign(sizeof(double), length*sizeof(double));
2405 SwsVector *vec= malloc(sizeof(SwsVector));
2406
2407 vec->coeff= coeff;
2408 vec->length= length;
2409
2410 for(i=0; i<length; i++)
2411 coeff[i]= c;
2412
2413 return vec;
2414}
2415
2416
c7f822d9
MN
2417SwsVector *getIdentityVec(void){
2418 double *coeff= memalign(sizeof(double), sizeof(double));
2419 SwsVector *vec= malloc(sizeof(SwsVector));
2420 coeff[0]= 1.0;
2421
2422 vec->coeff= coeff;
2423 vec->length= 1;
2424
2425 return vec;
2426}
2427
2428void normalizeVec(SwsVector *a, double height){
28bf81c9
MN
2429 int i;
2430 double sum=0;
2431 double inv;
2432
c7f822d9
MN
2433 for(i=0; i<a->length; i++)
2434 sum+= a->coeff[i];
28bf81c9
MN
2435
2436 inv= height/sum;
2437
c7f822d9 2438 for(i=0; i<a->length; i++)
8664c807 2439 a->coeff[i]*= inv;
28bf81c9
MN
2440}
2441
c7f822d9
MN
2442void scaleVec(SwsVector *a, double scalar){
2443 int i;
2444
2445 for(i=0; i<a->length; i++)
2446 a->coeff[i]*= scalar;
2447}
2448
5cebb24b 2449static SwsVector *getConvVec(SwsVector *a, SwsVector *b){
c7f822d9 2450 int length= a->length + b->length - 1;
28bf81c9
MN
2451 double *coeff= memalign(sizeof(double), length*sizeof(double));
2452 int i, j;
c7f822d9
MN
2453 SwsVector *vec= malloc(sizeof(SwsVector));
2454
2455 vec->coeff= coeff;
2456 vec->length= length;
28bf81c9
MN
2457
2458 for(i=0; i<length; i++) coeff[i]= 0.0;
2459
c7f822d9 2460 for(i=0; i<a->length; i++)
28bf81c9 2461 {
c7f822d9 2462 for(j=0; j<b->length; j++)
28bf81c9 2463 {
c7f822d9 2464 coeff[i+j]+= a->coeff[i]*b->coeff[j];
28bf81c9
MN
2465 }
2466 }
2467
c7f822d9 2468 return vec;
28bf81c9
MN
2469}
2470
5cebb24b 2471static SwsVector *sumVec(SwsVector *a, SwsVector *b){
c7f822d9 2472 int length= MAX(a->length, b->length);
28bf81c9
MN
2473 double *coeff= memalign(sizeof(double), length*sizeof(double));
2474 int i;
c7f822d9
MN
2475 SwsVector *vec= malloc(sizeof(SwsVector));
2476
2477 vec->coeff= coeff;
2478 vec->length= length;
28bf81c9
MN
2479
2480 for(i=0; i<length; i++) coeff[i]= 0.0;
2481
c7f822d9
MN
2482 for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2483 for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
2484
2485 return vec;
28bf81c9 2486}
c7f822d9 2487
5cebb24b 2488static SwsVector *diffVec(SwsVector *a, SwsVector *b){
c7f822d9
MN
2489 int length= MAX(a->length, b->length);
2490 double *coeff= memalign(sizeof(double), length*sizeof(double));
2491 int i;
2492 SwsVector *vec= malloc(sizeof(SwsVector));
2493
2494 vec->coeff= coeff;
2495 vec->length= length;
2496
2497 for(i=0; i<length; i++) coeff[i]= 0.0;
2498
2499 for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2500 for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
2501
2502 return vec;
2503}
2504
2505/* shift left / or right if "shift" is negative */
5cebb24b 2506static SwsVector *getShiftedVec(SwsVector *a, int shift){
c7f822d9
MN
2507 int length= a->length + ABS(shift)*2;
2508 double *coeff= memalign(sizeof(double), length*sizeof(double));
ff7ba856 2509 int i;
c7f822d9
MN
2510 SwsVector *vec= malloc(sizeof(SwsVector));
2511
2512 vec->coeff= coeff;
2513 vec->length= length;
2514
2515 for(i=0; i<length; i++) coeff[i]= 0.0;
2516
2517 for(i=0; i<a->length; i++)
2518 {
2519 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2520 }
2521
2522 return vec;
2523}
2524
5cebb24b
MN
2525void shiftVec(SwsVector *a, int shift){
2526 SwsVector *shifted= getShiftedVec(a, shift);
2527 free(a->coeff);
2528 a->coeff= shifted->coeff;
2529 a->length= shifted->length;
2530 free(shifted);
2531}
2532
2533void addVec(SwsVector *a, SwsVector *b){
2534 SwsVector *sum= sumVec(a, b);
2535 free(a->coeff);
2536 a->coeff= sum->coeff;
2537 a->length= sum->length;
2538 free(sum);
2539}
2540
2541void subVec(SwsVector *a, SwsVector *b){
2542 SwsVector *diff= diffVec(a, b);
2543 free(a->coeff);
2544 a->coeff= diff->coeff;
2545 a->length= diff->length;
2546 free(diff);
2547}
2548
2549void convVec(SwsVector *a, SwsVector *b){
2550 SwsVector *conv= getConvVec(a, b);
2551 free(a->coeff);
2552 a->coeff= conv->coeff;
2553 a->length= conv->length;
2554 free(conv);
2555}
2556
2557SwsVector *cloneVec(SwsVector *a){
2558 double *coeff= memalign(sizeof(double), a->length*sizeof(double));
2559 int i;
2560 SwsVector *vec= malloc(sizeof(SwsVector));
2561
2562 vec->coeff= coeff;
2563 vec->length= a->length;
2564
2565 for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
2566
2567 return vec;
2568}
2569
c7f822d9
MN
2570void printVec(SwsVector *a){
2571 int i;
2572 double max=0;
2573 double min=0;
2574 double range;
2575
2576 for(i=0; i<a->length; i++)
2577 if(a->coeff[i]>max) max= a->coeff[i];
2578
2579 for(i=0; i<a->length; i++)
2580 if(a->coeff[i]<min) min= a->coeff[i];
2581
2582 range= max - min;
2583
2584 for(i=0; i<a->length; i++)
2585 {
2586 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
0d9f3d85
A
2587 MSG_DBG2("%1.3f ", a->coeff[i]);
2588 for(;x>0; x--) MSG_DBG2(" ");
2589 MSG_DBG2("|\n");
c7f822d9
MN
2590 }
2591}
2592
2593void freeVec(SwsVector *a){
2594 if(!a) return;
2595 if(a->coeff) free(a->coeff);
2596 a->coeff=NULL;
2597 a->length=0;
2598 free(a);
2599}
2600
2601void freeSwsContext(SwsContext *c){
2602 int i;
c7f822d9
MN
2603 if(!c) return;
2604
2605 if(c->lumPixBuf)
2606 {
6c7506de 2607 for(i=0; i<c->vLumBufSize; i++)
c7f822d9
MN
2608 {
2609 if(c->lumPixBuf[i]) free(c->lumPixBuf[i]);
2610 c->lumPixBuf[i]=NULL;
2611 }
2612 free(c->lumPixBuf);
2613 c->lumPixBuf=NULL;
2614 }
2615
2616 if(c->chrPixBuf)
2617 {
6c7506de 2618 for(i=0; i<c->vChrBufSize; i++)
c7f822d9
MN
2619 {
2620 if(c->chrPixBuf[i]) free(c->chrPixBuf[i]);
2621 c->chrPixBuf[i]=NULL;
2622 }
2623 free(c->chrPixBuf);
2624 c->chrPixBuf=NULL;
2625 }
2626
2627 if(c->vLumFilter) free(c->vLumFilter);
2628 c->vLumFilter = NULL;
2629 if(c->vChrFilter) free(c->vChrFilter);
2630 c->vChrFilter = NULL;
2631 if(c->hLumFilter) free(c->hLumFilter);
2632 c->hLumFilter = NULL;
2633 if(c->hChrFilter) free(c->hChrFilter);
2634 c->hChrFilter = NULL;
2635
2636 if(c->vLumFilterPos) free(c->vLumFilterPos);
2637 c->vLumFilterPos = NULL;
2638 if(c->vChrFilterPos) free(c->vChrFilterPos);
2639 c->vChrFilterPos = NULL;
2640 if(c->hLumFilterPos) free(c->hLumFilterPos);
2641 c->hLumFilterPos = NULL;
2642 if(c->hChrFilterPos) free(c->hChrFilterPos);
2643 c->hChrFilterPos = NULL;
2644
b7dc6f66
MN
2645 if(c->lumMmx2Filter) free(c->lumMmx2Filter);
2646 c->lumMmx2Filter=NULL;
2647 if(c->chrMmx2Filter) free(c->chrMmx2Filter);
2648 c->chrMmx2Filter=NULL;
2649 if(c->lumMmx2FilterPos) free(c->lumMmx2FilterPos);
2650 c->lumMmx2FilterPos=NULL;
2651 if(c->chrMmx2FilterPos) free(c->chrMmx2FilterPos);
2652 c->chrMmx2FilterPos=NULL;
cf7d1c1a
MN
2653 if(c->yuvTable) free(c->yuvTable);
2654 c->yuvTable=NULL;
b7dc6f66 2655
c7f822d9
MN
2656 free(c);
2657}
2658