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