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