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