Add , to clarify sentence.
[libav.git] / libswscale / swscale.c
CommitLineData
fe8054c0 1/*
d026b45e
DB
2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FFmpeg; if not, write to the Free Software
b19bcbaa 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d026b45e 19 *
807e0c66
LA
20 * the C code (not assembly, mmx, ...) of this file can be used
21 * under the LGPL license too
d026b45e 22 */
783e9cc9 23
28bf81c9 24/*
9990e426 25 supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
caeaabe7 26 supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
e09d12f4 27 {BGR,RGB}{1,4,8,15,16} support dithering
6a4970ab 28
e09d12f4
MN
29 unscaled special converters (YV12=I420=IYUV, Y800=Y8)
30 YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
31 x -> x
32 YUV9 -> YV12
33 YUV9/YV12 -> Y800
34 Y800 -> YUV9/YV12
b935781b
MN
35 BGR24 -> BGR32 & RGB24 -> RGB32
36 BGR32 -> BGR24 & RGB32 -> RGB24
4bb3fa5e 37 BGR15 -> BGR16
b935781b
MN
38*/
39
6a4970ab 40/*
a6f6b237 41tested special converters (most are tested actually, but I did not write it down ...)
e09d12f4 42 YV12 -> BGR16
b935781b 43 YV12 -> YV12
4bb3fa5e 44 BGR15 -> BGR16
1e1c4fe9 45 BGR16 -> BGR16
e09d12f4 46 YVU9 -> YV12
b935781b
MN
47
48untested special converters
86bdf3fd 49 YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be ok)
1e1c4fe9
MN
50 YV12/I420 -> YV12/I420
51 YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
b935781b
MN
52 BGR24 -> BGR32 & RGB24 -> RGB32
53 BGR32 -> BGR24 & RGB32 -> RGB24
ec22603f 54 BGR24 -> YV12
28bf81c9
MN
55*/
56
d63a2cb1 57#define _SVID_SOURCE //needed for MAP_ANONYMOUS
d3f41512 58#include <inttypes.h>
dda87e9f 59#include <string.h>
077ea8a7 60#include <math.h>
c1b0bfb4 61#include <stdio.h>
171d7d78 62#include <unistd.h>
b2d374c9 63#include "config.h"
81b7c056 64#include <assert.h>
38d5c282
AJ
65#ifdef HAVE_SYS_MMAN_H
66#include <sys/mman.h>
113ef149
RD
67#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
68#define MAP_ANONYMOUS MAP_ANON
69#endif
38d5c282 70#endif
d604bab9 71#include "swscale.h"
5427e242 72#include "swscale_internal.h"
37079906 73#include "rgb2rgb.h"
83da2c6f
DB
74#include "libavutil/x86_cpu.h"
75#include "libavutil/bswap.h"
0d9f3d85 76
b3e03fa7
SS
77unsigned swscale_version(void)
78{
79 return LIBSWSCALE_VERSION_INT;
80}
81
541c4eb9 82#undef MOVNTQ
7d7f78b5 83#undef PAVGB
d3f41512 84
783e9cc9 85//#undef HAVE_MMX2
7f56a527 86//#define HAVE_3DNOW
d3f41512 87//#undef HAVE_MMX
783e9cc9 88//#undef ARCH_X86
2ba1bff0 89//#define WORDS_BIGENDIAN
d604bab9 90#define DITHER1XBPP
d3f41512 91
ac6a2e45
MN
92#define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
93
1e621b18 94#define RET 0xC3 //near return opcode for X86
c1b0bfb4 95
28bf81c9
MN
96#ifdef M_PI
97#define PI M_PI
98#else
99#define PI 3.14159265358979323846
100#endif
c1b0bfb4 101
9d9de37d
IP
102#define isSupportedIn(x) ( \
103 (x)==PIX_FMT_YUV420P \
79973335 104 || (x)==PIX_FMT_YUVA420P \
9d9de37d
IP
105 || (x)==PIX_FMT_YUYV422 \
106 || (x)==PIX_FMT_UYVY422 \
107 || (x)==PIX_FMT_RGB32 \
9990e426 108 || (x)==PIX_FMT_RGB32_1 \
9d9de37d
IP
109 || (x)==PIX_FMT_BGR24 \
110 || (x)==PIX_FMT_BGR565 \
111 || (x)==PIX_FMT_BGR555 \
112 || (x)==PIX_FMT_BGR32 \
9990e426 113 || (x)==PIX_FMT_BGR32_1 \
9d9de37d
IP
114 || (x)==PIX_FMT_RGB24 \
115 || (x)==PIX_FMT_RGB565 \
116 || (x)==PIX_FMT_RGB555 \
117 || (x)==PIX_FMT_GRAY8 \
118 || (x)==PIX_FMT_YUV410P \
6c80eb16 119 || (x)==PIX_FMT_YUV440P \
9d9de37d
IP
120 || (x)==PIX_FMT_GRAY16BE \
121 || (x)==PIX_FMT_GRAY16LE \
122 || (x)==PIX_FMT_YUV444P \
123 || (x)==PIX_FMT_YUV422P \
124 || (x)==PIX_FMT_YUV411P \
125 || (x)==PIX_FMT_PAL8 \
126 || (x)==PIX_FMT_BGR8 \
127 || (x)==PIX_FMT_RGB8 \
128 || (x)==PIX_FMT_BGR4_BYTE \
129 || (x)==PIX_FMT_RGB4_BYTE \
9ba7fe6d 130 || (x)==PIX_FMT_YUV440P \
3d05e078
MN
131 || (x)==PIX_FMT_MONOWHITE \
132 || (x)==PIX_FMT_MONOBLACK \
9d9de37d
IP
133 )
134#define isSupportedOut(x) ( \
135 (x)==PIX_FMT_YUV420P \
136 || (x)==PIX_FMT_YUYV422 \
137 || (x)==PIX_FMT_UYVY422 \
138 || (x)==PIX_FMT_YUV444P \
139 || (x)==PIX_FMT_YUV422P \
140 || (x)==PIX_FMT_YUV411P \
141 || isRGB(x) \
142 || isBGR(x) \
143 || (x)==PIX_FMT_NV12 \
144 || (x)==PIX_FMT_NV21 \
145 || (x)==PIX_FMT_GRAY16BE \
146 || (x)==PIX_FMT_GRAY16LE \
147 || (x)==PIX_FMT_GRAY8 \
148 || (x)==PIX_FMT_YUV410P \
6c80eb16 149 || (x)==PIX_FMT_YUV440P \
9d9de37d
IP
150 )
151#define isPacked(x) ( \
152 (x)==PIX_FMT_PAL8 \
153 || (x)==PIX_FMT_YUYV422 \
154 || (x)==PIX_FMT_UYVY422 \
155 || isRGB(x) \
156 || isBGR(x) \
157 )
6ff0ad6b 158
6b79dbce 159#define RGB2YUV_SHIFT 15
7b5d7b9e
MN
160#define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
161#define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
162#define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
163#define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
164#define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
165#define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
166#define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
167#define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
168#define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
6c7506de 169
0481412a
MN
170extern const int32_t Inverse_Table_6_9[8][4];
171
0f5d4aa8
MN
172static const double rgb2yuv_table[8][9]={
173 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
174 {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
175 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
176 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
177 {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
178 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
179 {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
180 {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
181};
182
783e9cc9
MN
183/*
184NOTES
d604bab9 185Special versions: fast Y 1:1 scaling (no interpolation in y direction)
31190492 186
783e9cc9 187TODO
bd7c6fd5 188more intelligent misalignment avoidance for the horizontal scaler
c1b0bfb4
MN
189write special vertical cubic upscale version
190Optimize C code (yv12 / minmax)
ff7ba856 191add support for packed pixel yuv input & output
6ff0ad6b
MN
192add support for Y8 output
193optimize bgr24 & bgr32
ff7ba856 194add BGR4 output support
1e621b18 195write special BGR->BGR scaler
783e9cc9 196*/
31190492 197
9bde778e 198#if defined(ARCH_X86) && defined (CONFIG_GPL)
d334c7c2
RD
199DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
200DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
201DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
202DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
203DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
204DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
205DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
206DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
d604bab9 207
db7a2e0d
MC
208static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
209static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
210static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
211static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
d8fa3c54 212
0cb25594 213const DECLARE_ALIGNED(8, uint64_t, ff_dither4[2]) = {
221b804f
DB
214 0x0103010301030103LL,
215 0x0200020002000200LL,};
d8fa3c54 216
0cb25594 217const DECLARE_ALIGNED(8, uint64_t, ff_dither8[2]) = {
221b804f
DB
218 0x0602060206020602LL,
219 0x0004000400040004LL,};
d604bab9 220
d334c7c2
RD
221DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
222DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
223DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
224DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
225DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
226DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
d604bab9 227
5802683a
RD
228DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
229DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
230DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
99d2cb72 231
ac6a2e45 232#ifdef FAST_BGR2YV12
5802683a
RD
233DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
234DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
235DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
ac6a2e45 236#else
5802683a
RD
237DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
238DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
239DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
69796008 240#endif /* FAST_BGR2YV12 */
5802683a
RD
241DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
242DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
243DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
dfb09bd1
MN
244
245DECLARE_ALIGNED(8, const uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
246DECLARE_ALIGNED(8, const uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
247DECLARE_ALIGNED(8, const uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
248DECLARE_ALIGNED(8, const uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
249DECLARE_ALIGNED(8, const uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
250
251DECLARE_ALIGNED(8, const uint64_t, ff_bgr24toUV[2][4]) = {
252 {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
253 {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
254};
255
256DECLARE_ALIGNED(8, const uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
257
3d6a30d9 258#endif /* defined(ARCH_X86) */
783e9cc9
MN
259
260// clipping helper table for C implementations:
261static unsigned char clip_table[768];
262
d4e24275 263static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
6a4970ab 264
cf7d1c1a
MN
265extern const uint8_t dither_2x2_4[2][8];
266extern const uint8_t dither_2x2_8[2][8];
267extern const uint8_t dither_8x8_32[8][8];
268extern const uint8_t dither_8x8_73[8][8];
269extern const uint8_t dither_8x8_220[8][8];
5cebb24b 270
8055ede6 271const char *sws_format_name(enum PixelFormat format)
94c4def2 272{
e9e12f0e
LA
273 switch (format) {
274 case PIX_FMT_YUV420P:
275 return "yuv420p";
79973335
AJ
276 case PIX_FMT_YUVA420P:
277 return "yuva420p";
e9e12f0e
LA
278 case PIX_FMT_YUYV422:
279 return "yuyv422";
280 case PIX_FMT_RGB24:
281 return "rgb24";
282 case PIX_FMT_BGR24:
283 return "bgr24";
284 case PIX_FMT_YUV422P:
285 return "yuv422p";
286 case PIX_FMT_YUV444P:
287 return "yuv444p";
288 case PIX_FMT_RGB32:
289 return "rgb32";
290 case PIX_FMT_YUV410P:
291 return "yuv410p";
292 case PIX_FMT_YUV411P:
293 return "yuv411p";
294 case PIX_FMT_RGB565:
295 return "rgb565";
296 case PIX_FMT_RGB555:
297 return "rgb555";
4884b9e5
KS
298 case PIX_FMT_GRAY16BE:
299 return "gray16be";
300 case PIX_FMT_GRAY16LE:
301 return "gray16le";
e9e12f0e
LA
302 case PIX_FMT_GRAY8:
303 return "gray8";
304 case PIX_FMT_MONOWHITE:
305 return "mono white";
306 case PIX_FMT_MONOBLACK:
307 return "mono black";
308 case PIX_FMT_PAL8:
309 return "Palette";
310 case PIX_FMT_YUVJ420P:
311 return "yuvj420p";
312 case PIX_FMT_YUVJ422P:
313 return "yuvj422p";
314 case PIX_FMT_YUVJ444P:
315 return "yuvj444p";
316 case PIX_FMT_XVMC_MPEG2_MC:
317 return "xvmc_mpeg2_mc";
318 case PIX_FMT_XVMC_MPEG2_IDCT:
319 return "xvmc_mpeg2_idct";
320 case PIX_FMT_UYVY422:
321 return "uyvy422";
322 case PIX_FMT_UYYVYY411:
323 return "uyyvyy411";
324 case PIX_FMT_RGB32_1:
325 return "rgb32x";
326 case PIX_FMT_BGR32_1:
327 return "bgr32x";
328 case PIX_FMT_BGR32:
329 return "bgr32";
330 case PIX_FMT_BGR565:
331 return "bgr565";
332 case PIX_FMT_BGR555:
333 return "bgr555";
334 case PIX_FMT_BGR8:
335 return "bgr8";
336 case PIX_FMT_BGR4:
337 return "bgr4";
338 case PIX_FMT_BGR4_BYTE:
339 return "bgr4 byte";
340 case PIX_FMT_RGB8:
341 return "rgb8";
342 case PIX_FMT_RGB4:
343 return "rgb4";
344 case PIX_FMT_RGB4_BYTE:
345 return "rgb4 byte";
346 case PIX_FMT_NV12:
347 return "nv12";
348 case PIX_FMT_NV21:
349 return "nv21";
9ba7fe6d
350 case PIX_FMT_YUV440P:
351 return "yuv440p";
e9e12f0e
LA
352 default:
353 return "Unknown format";
354 }
94c4def2
LA
355}
356
5859233b 357static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
221b804f
DB
358 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
359 uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
e3d2500f 360{
221b804f
DB
361 //FIXME Optimize (just quickly writen not opti..)
362 int i;
363 for (i=0; i<dstW; i++)
364 {
365 int val=1<<18;
366 int j;
367 for (j=0; j<lumFilterSize; j++)
368 val += lumSrc[j][i] * lumFilter[j];
369
370 dest[i]= av_clip_uint8(val>>19);
371 }
372
1b0a4572 373 if (uDest)
221b804f
DB
374 for (i=0; i<chrDstW; i++)
375 {
376 int u=1<<18;
377 int v=1<<18;
378 int j;
379 for (j=0; j<chrFilterSize; j++)
380 {
381 u += chrSrc[j][i] * chrFilter[j];
8b2fce0d 382 v += chrSrc[j][i + VOFW] * chrFilter[j];
221b804f
DB
383 }
384
385 uDest[i]= av_clip_uint8(u>>19);
386 vDest[i]= av_clip_uint8(v>>19);
387 }
e3d2500f
MN
388}
389
6118e52e 390static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
221b804f
DB
391 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
392 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
6118e52e 393{
221b804f
DB
394 //FIXME Optimize (just quickly writen not opti..)
395 int i;
396 for (i=0; i<dstW; i++)
397 {
398 int val=1<<18;
399 int j;
400 for (j=0; j<lumFilterSize; j++)
401 val += lumSrc[j][i] * lumFilter[j];
402
403 dest[i]= av_clip_uint8(val>>19);
404 }
405
1b0a4572 406 if (!uDest)
221b804f
DB
407 return;
408
409 if (dstFormat == PIX_FMT_NV12)
410 for (i=0; i<chrDstW; i++)
411 {
412 int u=1<<18;
413 int v=1<<18;
414 int j;
415 for (j=0; j<chrFilterSize; j++)
416 {
417 u += chrSrc[j][i] * chrFilter[j];
8b2fce0d 418 v += chrSrc[j][i + VOFW] * chrFilter[j];
221b804f
DB
419 }
420
421 uDest[2*i]= av_clip_uint8(u>>19);
422 uDest[2*i+1]= av_clip_uint8(v>>19);
423 }
424 else
425 for (i=0; i<chrDstW; i++)
426 {
427 int u=1<<18;
428 int v=1<<18;
429 int j;
430 for (j=0; j<chrFilterSize; j++)
431 {
432 u += chrSrc[j][i] * chrFilter[j];
8b2fce0d 433 v += chrSrc[j][i + VOFW] * chrFilter[j];
221b804f
DB
434 }
435
436 uDest[2*i]= av_clip_uint8(v>>19);
437 uDest[2*i+1]= av_clip_uint8(u>>19);
438 }
6118e52e 439}
46de8b73 440
bdf397ba 441#define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type) \
221b804f
DB
442 for (i=0; i<(dstW>>1); i++){\
443 int j;\
444 int Y1 = 1<<18;\
445 int Y2 = 1<<18;\
446 int U = 1<<18;\
447 int V = 1<<18;\
2db27aad 448 type av_unused *r, *b, *g;\
221b804f
DB
449 const int i2= 2*i;\
450 \
451 for (j=0; j<lumFilterSize; j++)\
452 {\
453 Y1 += lumSrc[j][i2] * lumFilter[j];\
454 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
455 }\
456 for (j=0; j<chrFilterSize; j++)\
457 {\
458 U += chrSrc[j][i] * chrFilter[j];\
8b2fce0d 459 V += chrSrc[j][i+VOFW] * chrFilter[j];\
221b804f
DB
460 }\
461 Y1>>=19;\
462 Y2>>=19;\
463 U >>=19;\
464 V >>=19;\
bdf397ba
MN
465
466#define YSCALE_YUV_2_PACKEDX_C(type) \
467 YSCALE_YUV_2_PACKEDX_NOCLIP_C(type)\
221b804f
DB
468 if ((Y1|Y2|U|V)&256)\
469 {\
470 if (Y1>255) Y1=255; \
471 else if (Y1<0)Y1=0; \
472 if (Y2>255) Y2=255; \
473 else if (Y2<0)Y2=0; \
474 if (U>255) U=255; \
475 else if (U<0) U=0; \
476 if (V>255) V=255; \
477 else if (V<0) V=0; \
478 }
6a4970ab 479
f0faee4c
MN
480#define YSCALE_YUV_2_PACKEDX_FULL_C \
481 for (i=0; i<dstW; i++){\
482 int j;\
483 int Y = 0;\
484 int U = -128<<19;\
485 int V = -128<<19;\
486 int R,G,B;\
487 \
488 for (j=0; j<lumFilterSize; j++){\
489 Y += lumSrc[j][i ] * lumFilter[j];\
490 }\
491 for (j=0; j<chrFilterSize; j++){\
492 U += chrSrc[j][i ] * chrFilter[j];\
493 V += chrSrc[j][i+VOFW] * chrFilter[j];\
494 }\
495 Y >>=10;\
496 U >>=10;\
497 V >>=10;\
498
499#define YSCALE_YUV_2_RGBX_FULL_C(rnd) \
500 YSCALE_YUV_2_PACKEDX_FULL_C\
43c16478
MN
501 Y-= c->yuv2rgb_y_offset;\
502 Y*= c->yuv2rgb_y_coeff;\
f0faee4c 503 Y+= rnd;\
43c16478
MN
504 R= Y + V*c->yuv2rgb_v2r_coeff;\
505 G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
506 B= Y + U*c->yuv2rgb_u2b_coeff;\
f0faee4c
MN
507 if ((R|G|B)&(0xC0000000)){\
508 if (R>=(256<<22)) R=(256<<22)-1; \
509 else if (R<0)R=0; \
510 if (G>=(256<<22)) G=(256<<22)-1; \
511 else if (G<0)G=0; \
512 if (B>=(256<<22)) B=(256<<22)-1; \
513 else if (B<0)B=0; \
514 }\
515
516
e69bd294 517#define YSCALE_YUV_2_GRAY16_C \
b0880d5d
MN
518 for (i=0; i<(dstW>>1); i++){\
519 int j;\
520 int Y1 = 1<<18;\
521 int Y2 = 1<<18;\
522 int U = 1<<18;\
523 int V = 1<<18;\
e69bd294 524 \
b0880d5d
MN
525 const int i2= 2*i;\
526 \
527 for (j=0; j<lumFilterSize; j++)\
528 {\
529 Y1 += lumSrc[j][i2] * lumFilter[j];\
530 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
531 }\
532 Y1>>=11;\
533 Y2>>=11;\
534 if ((Y1|Y2|U|V)&65536)\
535 {\
536 if (Y1>65535) Y1=65535; \
537 else if (Y1<0)Y1=0; \
538 if (Y2>65535) Y2=65535; \
539 else if (Y2<0)Y2=0; \
540 }
541
46de8b73 542#define YSCALE_YUV_2_RGBX_C(type) \
b82ecd62 543 YSCALE_YUV_2_PACKEDX_C(type) /* FIXME fix tables so that cliping is not needed and then use _NOCLIP*/\
221b804f
DB
544 r = (type *)c->table_rV[V]; \
545 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
546 b = (type *)c->table_bU[U]; \
547
548#define YSCALE_YUV_2_PACKED2_C \
549 for (i=0; i<(dstW>>1); i++){ \
550 const int i2= 2*i; \
551 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
552 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
553 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
8b2fce0d 554 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
46de8b73 555
b0880d5d
MN
556#define YSCALE_YUV_2_GRAY16_2_C \
557 for (i=0; i<(dstW>>1); i++){ \
558 const int i2= 2*i; \
559 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
560 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11; \
561
46de8b73 562#define YSCALE_YUV_2_RGB2_C(type) \
221b804f
DB
563 YSCALE_YUV_2_PACKED2_C\
564 type *r, *b, *g;\
565 r = (type *)c->table_rV[V];\
566 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
567 b = (type *)c->table_bU[U];\
cf7d1c1a 568
25593e29 569#define YSCALE_YUV_2_PACKED1_C \
221b804f
DB
570 for (i=0; i<(dstW>>1); i++){\
571 const int i2= 2*i;\
572 int Y1= buf0[i2 ]>>7;\
573 int Y2= buf0[i2+1]>>7;\
574 int U= (uvbuf1[i ])>>7;\
8b2fce0d 575 int V= (uvbuf1[i+VOFW])>>7;\
46de8b73 576
b0880d5d
MN
577#define YSCALE_YUV_2_GRAY16_1_C \
578 for (i=0; i<(dstW>>1); i++){\
579 const int i2= 2*i;\
580 int Y1= buf0[i2 ]<<1;\
581 int Y2= buf0[i2+1]<<1;\
582
46de8b73 583#define YSCALE_YUV_2_RGB1_C(type) \
221b804f
DB
584 YSCALE_YUV_2_PACKED1_C\
585 type *r, *b, *g;\
586 r = (type *)c->table_rV[V];\
587 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
588 b = (type *)c->table_bU[U];\
cf7d1c1a 589
25593e29 590#define YSCALE_YUV_2_PACKED1B_C \
221b804f
DB
591 for (i=0; i<(dstW>>1); i++){\
592 const int i2= 2*i;\
593 int Y1= buf0[i2 ]>>7;\
594 int Y2= buf0[i2+1]>>7;\
595 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
8b2fce0d 596 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
46de8b73
MN
597
598#define YSCALE_YUV_2_RGB1B_C(type) \
221b804f
DB
599 YSCALE_YUV_2_PACKED1B_C\
600 type *r, *b, *g;\
601 r = (type *)c->table_rV[V];\
602 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
603 b = (type *)c->table_bU[U];\
cf7d1c1a 604
ec1bca2a 605#define YSCALE_YUV_2_MONO2_C \
e69bd294
MN
606 const uint8_t * const d128=dither_8x8_220[y&7];\
607 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
608 for (i=0; i<dstW-7; i+=8){\
609 int acc;\
610 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
611 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
612 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
613 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
614 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
615 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
616 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
617 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
ec1bca2a 618 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
e69bd294
MN
619 dest++;\
620 }\
621
622
ec1bca2a 623#define YSCALE_YUV_2_MONOX_C \
e69bd294
MN
624 const uint8_t * const d128=dither_8x8_220[y&7];\
625 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
626 int acc=0;\
627 for (i=0; i<dstW-1; i+=2){\
628 int j;\
629 int Y1=1<<18;\
630 int Y2=1<<18;\
631\
632 for (j=0; j<lumFilterSize; j++)\
633 {\
634 Y1 += lumSrc[j][i] * lumFilter[j];\
635 Y2 += lumSrc[j][i+1] * lumFilter[j];\
636 }\
637 Y1>>=19;\
638 Y2>>=19;\
639 if ((Y1|Y2)&256)\
640 {\
641 if (Y1>255) Y1=255;\
642 else if (Y1<0)Y1=0;\
643 if (Y2>255) Y2=255;\
644 else if (Y2<0)Y2=0;\
645 }\
646 acc+= acc + g[Y1+d128[(i+0)&7]];\
647 acc+= acc + g[Y2+d128[(i+1)&7]];\
648 if ((i&7)==6){\
ec1bca2a 649 ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
e69bd294
MN
650 dest++;\
651 }\
652 }
653
654
655#define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
221b804f
DB
656 switch(c->dstFormat)\
657 {\
658 case PIX_FMT_RGB32:\
659 case PIX_FMT_BGR32:\
9990e426
MN
660 case PIX_FMT_RGB32_1:\
661 case PIX_FMT_BGR32_1:\
221b804f
DB
662 func(uint32_t)\
663 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
664 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
665 } \
666 break;\
667 case PIX_FMT_RGB24:\
668 func(uint8_t)\
669 ((uint8_t*)dest)[0]= r[Y1];\
670 ((uint8_t*)dest)[1]= g[Y1];\
671 ((uint8_t*)dest)[2]= b[Y1];\
672 ((uint8_t*)dest)[3]= r[Y2];\
673 ((uint8_t*)dest)[4]= g[Y2];\
674 ((uint8_t*)dest)[5]= b[Y2];\
675 dest+=6;\
676 }\
677 break;\
678 case PIX_FMT_BGR24:\
679 func(uint8_t)\
680 ((uint8_t*)dest)[0]= b[Y1];\
681 ((uint8_t*)dest)[1]= g[Y1];\
682 ((uint8_t*)dest)[2]= r[Y1];\
683 ((uint8_t*)dest)[3]= b[Y2];\
684 ((uint8_t*)dest)[4]= g[Y2];\
685 ((uint8_t*)dest)[5]= r[Y2];\
686 dest+=6;\
687 }\
688 break;\
689 case PIX_FMT_RGB565:\
690 case PIX_FMT_BGR565:\
691 {\
692 const int dr1= dither_2x2_8[y&1 ][0];\
693 const int dg1= dither_2x2_4[y&1 ][0];\
694 const int db1= dither_2x2_8[(y&1)^1][0];\
695 const int dr2= dither_2x2_8[y&1 ][1];\
696 const int dg2= dither_2x2_4[y&1 ][1];\
697 const int db2= dither_2x2_8[(y&1)^1][1];\
698 func(uint16_t)\
699 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
700 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
701 }\
702 }\
703 break;\
704 case PIX_FMT_RGB555:\
705 case PIX_FMT_BGR555:\
706 {\
707 const int dr1= dither_2x2_8[y&1 ][0];\
708 const int dg1= dither_2x2_8[y&1 ][1];\
709 const int db1= dither_2x2_8[(y&1)^1][0];\
710 const int dr2= dither_2x2_8[y&1 ][1];\
711 const int dg2= dither_2x2_8[y&1 ][0];\
712 const int db2= dither_2x2_8[(y&1)^1][1];\
713 func(uint16_t)\
714 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
715 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
716 }\
717 }\
718 break;\
719 case PIX_FMT_RGB8:\
720 case PIX_FMT_BGR8:\
721 {\
722 const uint8_t * const d64= dither_8x8_73[y&7];\
723 const uint8_t * const d32= dither_8x8_32[y&7];\
724 func(uint8_t)\
725 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
726 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
727 }\
728 }\
729 break;\
730 case PIX_FMT_RGB4:\
731 case PIX_FMT_BGR4:\
732 {\
733 const uint8_t * const d64= dither_8x8_73 [y&7];\
734 const uint8_t * const d128=dither_8x8_220[y&7];\
735 func(uint8_t)\
736 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
737 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
738 }\
739 }\
740 break;\
741 case PIX_FMT_RGB4_BYTE:\
742 case PIX_FMT_BGR4_BYTE:\
743 {\
744 const uint8_t * const d64= dither_8x8_73 [y&7];\
745 const uint8_t * const d128=dither_8x8_220[y&7];\
746 func(uint8_t)\
747 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
748 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
749 }\
750 }\
751 break;\
752 case PIX_FMT_MONOBLACK:\
ec1bca2a 753 case PIX_FMT_MONOWHITE:\
221b804f 754 {\
e69bd294 755 func_monoblack\
221b804f
DB
756 }\
757 break;\
758 case PIX_FMT_YUYV422:\
759 func2\
760 ((uint8_t*)dest)[2*i2+0]= Y1;\
761 ((uint8_t*)dest)[2*i2+1]= U;\
762 ((uint8_t*)dest)[2*i2+2]= Y2;\
763 ((uint8_t*)dest)[2*i2+3]= V;\
764 } \
765 break;\
766 case PIX_FMT_UYVY422:\
767 func2\
768 ((uint8_t*)dest)[2*i2+0]= U;\
769 ((uint8_t*)dest)[2*i2+1]= Y1;\
770 ((uint8_t*)dest)[2*i2+2]= V;\
771 ((uint8_t*)dest)[2*i2+3]= Y2;\
772 } \
773 break;\
b0880d5d
MN
774 case PIX_FMT_GRAY16BE:\
775 func_g16\
776 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
777 ((uint8_t*)dest)[2*i2+1]= Y1;\
778 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
779 ((uint8_t*)dest)[2*i2+3]= Y2;\
780 } \
781 break;\
782 case PIX_FMT_GRAY16LE:\
783 func_g16\
784 ((uint8_t*)dest)[2*i2+0]= Y1;\
785 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
786 ((uint8_t*)dest)[2*i2+2]= Y2;\
787 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
788 } \
789 break;\
221b804f 790 }\
cf7d1c1a
MN
791
792
25593e29 793static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
221b804f
DB
794 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
795 uint8_t *dest, int dstW, int y)
e3d2500f 796{
221b804f 797 int i;
ec1bca2a 798 YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
e3d2500f
MN
799}
800
f0faee4c
MN
801static inline void yuv2rgbXinC_full(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
802 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
803 uint8_t *dest, int dstW, int y)
804{
805 int i;
806 int step= fmt_depth(c->dstFormat)/8;
807
808 switch(c->dstFormat){
809 case PIX_FMT_ARGB:
810 dest++;
811 case PIX_FMT_RGB24:
812 case PIX_FMT_RGBA:
813 YSCALE_YUV_2_RGBX_FULL_C(1<<21)
814 dest[0]= R>>22;
815 dest[1]= G>>22;
816 dest[2]= B>>22;
817 dest[3]= 0;
818 dest+= step;
819 }
820 break;
821 case PIX_FMT_ABGR:
822 dest++;
823 case PIX_FMT_BGR24:
824 case PIX_FMT_BGRA:
825 YSCALE_YUV_2_RGBX_FULL_C(1<<21)
826 dest[0]= B>>22;
827 dest[1]= G>>22;
828 dest[2]= R>>22;
829 dest[3]= 0;
830 dest+= step;
831 }
832 break;
833 default:
834 assert(0);
835 }
836}
e3d2500f 837
7630f2e0
MN
838//Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
839//Plain C versions
9bde778e 840#if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT) || !defined(CONFIG_GPL)
726a959a
MN
841#define COMPILE_C
842#endif
843
a2faa401 844#ifdef ARCH_POWERPC
9bde778e 845#if (defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
a2faa401
RD
846#define COMPILE_ALTIVEC
847#endif //HAVE_ALTIVEC
848#endif //ARCH_POWERPC
849
3d6a30d9 850#if defined(ARCH_X86)
726a959a 851
9bde778e 852#if ((defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
726a959a
MN
853#define COMPILE_MMX
854#endif
855
9bde778e 856#if (defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
726a959a
MN
857#define COMPILE_MMX2
858#endif
859
9bde778e 860#if ((defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
726a959a
MN
861#define COMPILE_3DNOW
862#endif
6e1c66bc 863#endif //ARCH_X86 || ARCH_X86_64
726a959a
MN
864
865#undef HAVE_MMX
866#undef HAVE_MMX2
867#undef HAVE_3DNOW
726a959a
MN
868
869#ifdef COMPILE_C
7630f2e0
MN
870#undef HAVE_MMX
871#undef HAVE_MMX2
872#undef HAVE_3DNOW
a2faa401 873#undef HAVE_ALTIVEC
7630f2e0
MN
874#define RENAME(a) a ## _C
875#include "swscale_template.c"
726a959a 876#endif
397c035e 877
a2faa401
RD
878#ifdef COMPILE_ALTIVEC
879#undef RENAME
880#define HAVE_ALTIVEC
881#define RENAME(a) a ## _altivec
882#include "swscale_template.c"
883#endif
a2faa401 884
3d6a30d9 885#if defined(ARCH_X86)
397c035e 886
7630f2e0
MN
887//X86 versions
888/*
889#undef RENAME
890#undef HAVE_MMX
891#undef HAVE_MMX2
892#undef HAVE_3DNOW
893#define ARCH_X86
894#define RENAME(a) a ## _X86
895#include "swscale_template.c"
1faf0867 896*/
7630f2e0 897//MMX versions
726a959a 898#ifdef COMPILE_MMX
7630f2e0
MN
899#undef RENAME
900#define HAVE_MMX
901#undef HAVE_MMX2
902#undef HAVE_3DNOW
7630f2e0
MN
903#define RENAME(a) a ## _MMX
904#include "swscale_template.c"
726a959a 905#endif
7630f2e0
MN
906
907//MMX2 versions
726a959a 908#ifdef COMPILE_MMX2
7630f2e0
MN
909#undef RENAME
910#define HAVE_MMX
911#define HAVE_MMX2
912#undef HAVE_3DNOW
7630f2e0
MN
913#define RENAME(a) a ## _MMX2
914#include "swscale_template.c"
726a959a 915#endif
7630f2e0
MN
916
917//3DNOW versions
726a959a 918#ifdef COMPILE_3DNOW
7630f2e0
MN
919#undef RENAME
920#define HAVE_MMX
921#undef HAVE_MMX2
922#define HAVE_3DNOW
7630f2e0
MN
923#define RENAME(a) a ## _3DNow
924#include "swscale_template.c"
726a959a 925#endif
7630f2e0 926
6e1c66bc 927#endif //ARCH_X86 || ARCH_X86_64
7630f2e0 928
77a416e8 929// minor note: the HAVE_xyz is messed up after that line so don't use it
d604bab9 930
a86c461c
MN
931static double getSplineCoeff(double a, double b, double c, double d, double dist)
932{
221b804f
DB
933// printf("%f %f %f %f %f\n", a,b,c,d,dist);
934 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
935 else return getSplineCoeff( 0.0,
936 b+ 2.0*c + 3.0*d,
937 c + 3.0*d,
938 -b- 3.0*c - 6.0*d,
939 dist-1.0);
a86c461c 940}
6c7506de 941
bca11e75 942static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
221b804f
DB
943 int srcW, int dstW, int filterAlign, int one, int flags,
944 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
28bf81c9 945{
221b804f
DB
946 int i;
947 int filterSize;
948 int filter2Size;
949 int minFilterSize;
950 double *filter=NULL;
951 double *filter2=NULL;
091d3bdc 952 int ret= -1;
3d6a30d9 953#if defined(ARCH_X86)
221b804f 954 if (flags & SWS_CPU_CAPS_MMX)
86bdf3fd 955 asm volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
726a959a 956#endif
31190492 957
221b804f
DB
958 // Note the +1 is for the MMXscaler which reads over the end
959 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
960
961 if (FFABS(xInc - 0x10000) <10) // unscaled
962 {
963 int i;
964 filterSize= 1;
965 filter= av_malloc(dstW*sizeof(double)*filterSize);
966 for (i=0; i<dstW*filterSize; i++) filter[i]=0;
967
968 for (i=0; i<dstW; i++)
969 {
970 filter[i*filterSize]=1;
971 (*filterPos)[i]=i;
972 }
973
974 }
975 else if (flags&SWS_POINT) // lame looking point sampling mode
976 {
977 int i;
978 int xDstInSrc;
979 filterSize= 1;
980 filter= av_malloc(dstW*sizeof(double)*filterSize);
981
982 xDstInSrc= xInc/2 - 0x8000;
983 for (i=0; i<dstW; i++)
984 {
985 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
986
987 (*filterPos)[i]= xx;
988 filter[i]= 1.0;
989 xDstInSrc+= xInc;
990 }
991 }
992 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
993 {
994 int i;
995 int xDstInSrc;
996 if (flags&SWS_BICUBIC) filterSize= 4;
997 else if (flags&SWS_X ) filterSize= 4;
998 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
999 filter= av_malloc(dstW*sizeof(double)*filterSize);
1000
1001 xDstInSrc= xInc/2 - 0x8000;
1002 for (i=0; i<dstW; i++)
1003 {
1004 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1005 int j;
1006
1007 (*filterPos)[i]= xx;
1008 //Bilinear upscale / linear interpolate / Area averaging
1009 for (j=0; j<filterSize; j++)
1010 {
1011 double d= FFABS((xx<<16) - xDstInSrc)/(double)(1<<16);
1012 double coeff= 1.0 - d;
1013 if (coeff<0) coeff=0;
1014 filter[i*filterSize + j]= coeff;
1015 xx++;
1016 }
1017 xDstInSrc+= xInc;
1018 }
1019 }
1020 else
1021 {
1022 double xDstInSrc;
1023 double sizeFactor, filterSizeInSrc;
1024 const double xInc1= (double)xInc / (double)(1<<16);
1025
1026 if (flags&SWS_BICUBIC) sizeFactor= 4.0;
1027 else if (flags&SWS_X) sizeFactor= 8.0;
1028 else if (flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
1029 else if (flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
1030 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
1031 else if (flags&SWS_SINC) sizeFactor= 20.0; // infinite ;)
1032 else if (flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
1033 else if (flags&SWS_BILINEAR) sizeFactor= 2.0;
1034 else {
1035 sizeFactor= 0.0; //GCC warning killer
fcc402b1 1036 assert(0);
221b804f
DB
1037 }
1038
1039 if (xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
1040 else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
1041
1042 filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
1043 if (filterSize > srcW-2) filterSize=srcW-2;
1044
1045 filter= av_malloc(dstW*sizeof(double)*filterSize);
1046
1047 xDstInSrc= xInc1 / 2.0 - 0.5;
1048 for (i=0; i<dstW; i++)
1049 {
1050 int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
1051 int j;
1052 (*filterPos)[i]= xx;
1053 for (j=0; j<filterSize; j++)
1054 {
1055 double d= FFABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
1056 double coeff;
1057 if (flags & SWS_BICUBIC)
1058 {
1059 double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
1060 double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
1061
1062 if (d<1.0)
1063 coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
1064 else if (d<2.0)
1065 coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
1066 else
1067 coeff=0.0;
1068 }
1069/* else if (flags & SWS_X)
1070 {
1071 double p= param ? param*0.01 : 0.3;
1072 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1073 coeff*= pow(2.0, - p*d*d);
1074 }*/
1075 else if (flags & SWS_X)
1076 {
1077 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1078
1079 if (d<1.0)
1080 coeff = cos(d*PI);
1081 else
1082 coeff=-1.0;
1083 if (coeff<0.0) coeff= -pow(-coeff, A);
1084 else coeff= pow( coeff, A);
1085 coeff= coeff*0.5 + 0.5;
1086 }
1087 else if (flags & SWS_AREA)
1088 {
1089 double srcPixelSize= 1.0/xInc1;
1090 if (d + srcPixelSize/2 < 0.5) coeff= 1.0;
1091 else if (d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
1092 else coeff=0.0;
1093 }
1094 else if (flags & SWS_GAUSS)
1095 {
1096 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1097 coeff = pow(2.0, - p*d*d);
1098 }
1099 else if (flags & SWS_SINC)
1100 {
1101 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1102 }
1103 else if (flags & SWS_LANCZOS)
1104 {
1105 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1106 coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
1107 if (d>p) coeff=0;
1108 }
1109 else if (flags & SWS_BILINEAR)
1110 {
1111 coeff= 1.0 - d;
1112 if (coeff<0) coeff=0;
1113 }
1114 else if (flags & SWS_SPLINE)
1115 {
1116 double p=-2.196152422706632;
1117 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
1118 }
1119 else {
1120 coeff= 0.0; //GCC warning killer
fcc402b1 1121 assert(0);
221b804f
DB
1122 }
1123
1124 filter[i*filterSize + j]= coeff;
1125 xx++;
1126 }
1127 xDstInSrc+= xInc1;
1128 }
1129 }
1130
1131 /* apply src & dst Filter to filter -> filter2
1132 av_free(filter);
1133 */
fcc402b1 1134 assert(filterSize>0);
221b804f
DB
1135 filter2Size= filterSize;
1136 if (srcFilter) filter2Size+= srcFilter->length - 1;
1137 if (dstFilter) filter2Size+= dstFilter->length - 1;
fcc402b1 1138 assert(filter2Size>0);
221b804f
DB
1139 filter2= av_malloc(filter2Size*dstW*sizeof(double));
1140
1141 for (i=0; i<dstW; i++)
1142 {
1143 int j;
1144 SwsVector scaleFilter;
1145 SwsVector *outVec;
1146
1147 scaleFilter.coeff= filter + i*filterSize;
1148 scaleFilter.length= filterSize;
1149
1150 if (srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
1151 else outVec= &scaleFilter;
1152
fcc402b1 1153 assert(outVec->length == filter2Size);
221b804f
DB
1154 //FIXME dstFilter
1155
1156 for (j=0; j<outVec->length; j++)
1157 {
1158 filter2[i*filter2Size + j]= outVec->coeff[j];
1159 }
1160
1161 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1162
1163 if (outVec != &scaleFilter) sws_freeVec(outVec);
1164 }
47b7382d 1165 av_freep(&filter);
221b804f
DB
1166
1167 /* try to reduce the filter-size (step1 find size and shift left) */
86bdf3fd 1168 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
221b804f
DB
1169 minFilterSize= 0;
1170 for (i=dstW-1; i>=0; i--)
1171 {
1172 int min= filter2Size;
1173 int j;
1174 double cutOff=0.0;
1175
1176 /* get rid off near zero elements on the left by shifting left */
1177 for (j=0; j<filter2Size; j++)
1178 {
1179 int k;
1180 cutOff += FFABS(filter2[i*filter2Size]);
1181
1182 if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1183
86bdf3fd 1184 /* preserve monotonicity because the core can't handle the filter otherwise */
221b804f
DB
1185 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1186
1187 // Move filter coeffs left
1188 for (k=1; k<filter2Size; k++)
1189 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1190 filter2[i*filter2Size + k - 1]= 0.0;
1191 (*filterPos)[i]++;
1192 }
1193
1194 cutOff=0.0;
1195 /* count near zeros on the right */
1196 for (j=filter2Size-1; j>0; j--)
1197 {
1198 cutOff += FFABS(filter2[i*filter2Size + j]);
1199
1200 if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1201 min--;
1202 }
1203
1204 if (min>minFilterSize) minFilterSize= min;
1205 }
1206
1207 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1208 // we can handle the special case 4,
1209 // so we don't want to go to the full 8
1210 if (minFilterSize < 5)
8c266f0c
RD
1211 filterAlign = 4;
1212
221b804f
DB
1213 // we really don't want to waste our time
1214 // doing useless computation, so fall-back on
1215 // the scalar C code for very small filter.
1216 // vectorizing is worth it only if you have
1217 // decent-sized vector.
1218 if (minFilterSize < 3)
8c266f0c 1219 filterAlign = 1;
221b804f
DB
1220 }
1221
1222 if (flags & SWS_CPU_CAPS_MMX) {
1223 // special case for unscaled vertical filtering
1224 if (minFilterSize == 1 && filterAlign == 2)
1225 filterAlign= 1;
1226 }
1227
fcc402b1 1228 assert(minFilterSize > 0);
221b804f 1229 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
fcc402b1 1230 assert(filterSize > 0);
221b804f 1231 filter= av_malloc(filterSize*dstW*sizeof(double));
1625216e 1232 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
091d3bdc 1233 goto error;
221b804f
DB
1234 *outFilterSize= filterSize;
1235
1236 if (flags&SWS_PRINT_INFO)
1237 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1238 /* try to reduce the filter-size (step2 reduce it) */
1239 for (i=0; i<dstW; i++)
1240 {
1241 int j;
1242
1243 for (j=0; j<filterSize; j++)
1244 {
1245 if (j>=filter2Size) filter[i*filterSize + j]= 0.0;
1246 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
88bc5a64
MN
1247 if((flags & SWS_BITEXACT) && j>=minFilterSize)
1248 filter[i*filterSize + j]= 0.0;
8c266f0c 1249 }
221b804f 1250 }
221b804f
DB
1251
1252
1253 //FIXME try to align filterpos if possible
8c266f0c 1254
221b804f
DB
1255 //fix borders
1256 for (i=0; i<dstW; i++)
1257 {
1258 int j;
1259 if ((*filterPos)[i] < 0)
1260 {
1261 // Move filter coeffs left to compensate for filterPos
1262 for (j=1; j<filterSize; j++)
1263 {
1264 int left= FFMAX(j + (*filterPos)[i], 0);
1265 filter[i*filterSize + left] += filter[i*filterSize + j];
1266 filter[i*filterSize + j]=0;
1267 }
1268 (*filterPos)[i]= 0;
bca11e75
MN
1269 }
1270
221b804f
DB
1271 if ((*filterPos)[i] + filterSize > srcW)
1272 {
1273 int shift= (*filterPos)[i] + filterSize - srcW;
1274 // Move filter coeffs right to compensate for filterPos
1275 for (j=filterSize-2; j>=0; j--)
1276 {
1277 int right= FFMIN(j + shift, filterSize-1);
1278 filter[i*filterSize +right] += filter[i*filterSize +j];
1279 filter[i*filterSize +j]=0;
1280 }
1281 (*filterPos)[i]= srcW - filterSize;
1282 }
1283 }
1284
1285 // Note the +1 is for the MMXscaler which reads over the end
1286 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1287 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1288
1289 /* Normalize & Store in outFilter */
1290 for (i=0; i<dstW; i++)
1291 {
1292 int j;
1293 double error=0;
1294 double sum=0;
1295 double scale= one;
1296
1297 for (j=0; j<filterSize; j++)
1298 {
1299 sum+= filter[i*filterSize + j];
1300 }
1301 scale/= sum;
1302 for (j=0; j<*outFilterSize; j++)
1303 {
1304 double v= filter[i*filterSize + j]*scale + error;
1305 int intV= floor(v + 0.5);
1306 (*outFilter)[i*(*outFilterSize) + j]= intV;
1307 error = v - intV;
1308 }
1309 }
1310
1311 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1312 for (i=0; i<*outFilterSize; i++)
1313 {
1314 int j= dstW*(*outFilterSize);
1315 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1316 }
1317
091d3bdc
MN
1318 ret=0;
1319error:
221b804f 1320 av_free(filter);
091d3bdc
MN
1321 av_free(filter2);
1322 return ret;
7630f2e0 1323}
31190492 1324
17c613ef 1325#ifdef COMPILE_MMX2
b7dc6f66 1326static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
28bf81c9 1327{
221b804f
DB
1328 uint8_t *fragmentA;
1329 long imm8OfPShufW1A;
1330 long imm8OfPShufW2A;
1331 long fragmentLengthA;
1332 uint8_t *fragmentB;
1333 long imm8OfPShufW1B;
1334 long imm8OfPShufW2B;
1335 long fragmentLengthB;
1336 int fragmentPos;
1337
1338 int xpos, i;
1339
1340 // create an optimized horizontal scaling routine
1341
1342 //code fragment
1343
1344 asm volatile(
1345 "jmp 9f \n\t"
1346 // Begin
1347 "0: \n\t"
1348 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1349 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1350 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1351 "punpcklbw %%mm7, %%mm1 \n\t"
1352 "punpcklbw %%mm7, %%mm0 \n\t"
1353 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1354 "1: \n\t"
1355 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1356 "2: \n\t"
1357 "psubw %%mm1, %%mm0 \n\t"
1358 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1359 "pmullw %%mm3, %%mm0 \n\t"
1360 "psllw $7, %%mm1 \n\t"
1361 "paddw %%mm1, %%mm0 \n\t"
1362
1363 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1364
1365 "add $8, %%"REG_a" \n\t"
1366 // End
1367 "9: \n\t"
1368// "int $3 \n\t"
86593486
AS
1369 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1370 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1371 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
221b804f
DB
1372 "dec %1 \n\t"
1373 "dec %2 \n\t"
1374 "sub %0, %1 \n\t"
1375 "sub %0, %2 \n\t"
86593486 1376 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
221b804f
DB
1377 "sub %0, %3 \n\t"
1378
1379
1380 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1381 "=r" (fragmentLengthA)
1382 );
1383
1384 asm volatile(
1385 "jmp 9f \n\t"
1386 // Begin
1387 "0: \n\t"
1388 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1389 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1390 "punpcklbw %%mm7, %%mm0 \n\t"
1391 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1392 "1: \n\t"
1393 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1394 "2: \n\t"
1395 "psubw %%mm1, %%mm0 \n\t"
1396 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1397 "pmullw %%mm3, %%mm0 \n\t"
1398 "psllw $7, %%mm1 \n\t"
1399 "paddw %%mm1, %%mm0 \n\t"
1400
1401 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1402
1403 "add $8, %%"REG_a" \n\t"
1404 // End
1405 "9: \n\t"
1406// "int $3 \n\t"
86593486
AS
1407 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1408 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1409 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
221b804f
DB
1410 "dec %1 \n\t"
1411 "dec %2 \n\t"
1412 "sub %0, %1 \n\t"
1413 "sub %0, %2 \n\t"
86593486 1414 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
221b804f
DB
1415 "sub %0, %3 \n\t"
1416
1417
1418 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1419 "=r" (fragmentLengthB)
1420 );
1421
1422 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1423 fragmentPos=0;
1424
1425 for (i=0; i<dstW/numSplits; i++)
1426 {
1427 int xx=xpos>>16;
1428
1429 if ((i&3) == 0)
1430 {
1431 int a=0;
1432 int b=((xpos+xInc)>>16) - xx;
1433 int c=((xpos+xInc*2)>>16) - xx;
1434 int d=((xpos+xInc*3)>>16) - xx;
1435
1436 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1437 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1438 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1439 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1440 filterPos[i/2]= xx;
1441
1442 if (d+1<4)
1443 {
1444 int maxShift= 3-(d+1);
1445 int shift=0;
1446
1447 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1448
1449 funnyCode[fragmentPos + imm8OfPShufW1B]=
1450 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1451 funnyCode[fragmentPos + imm8OfPShufW2B]=
1452 a | (b<<2) | (c<<4) | (d<<6);
1453
1454 if (i+3>=dstW) shift=maxShift; //avoid overread
1455 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1456
1457 if (shift && i>=shift)
1458 {
1459 funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1460 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1461 filterPos[i/2]-=shift;
1462 }
1463
1464 fragmentPos+= fragmentLengthB;
1465 }
1466 else
1467 {
1468 int maxShift= 3-d;
1469 int shift=0;
1470
1471 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1472
1473 funnyCode[fragmentPos + imm8OfPShufW1A]=
1474 funnyCode[fragmentPos + imm8OfPShufW2A]=
1475 a | (b<<2) | (c<<4) | (d<<6);
1476
1477 if (i+4>=dstW) shift=maxShift; //avoid overread
1478 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1479
1480 if (shift && i>=shift)
1481 {
1482 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1483 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1484 filterPos[i/2]-=shift;
1485 }
1486
1487 fragmentPos+= fragmentLengthA;
1488 }
1489
1490 funnyCode[fragmentPos]= RET;
1491 }
1492 xpos+=xInc;
1493 }
1494 filterPos[i/2]= xpos>>16; // needed to jump to the next part
28bf81c9 1495}
17c613ef 1496#endif /* COMPILE_MMX2 */
28bf81c9 1497
9b2283cc 1498static void globalInit(void){
31190492
A
1499 // generating tables:
1500 int i;
221b804f
DB
1501 for (i=0; i<768; i++){
1502 int c= av_clip_uint8(i-256);
1503 clip_table[i]=c;
b18ea156 1504 }
516b1f82 1505}
c1b0bfb4 1506
516b1f82 1507static SwsFunc getSwsFunc(int flags){
6a4970ab 1508
9bde778e 1509#if defined(RUNTIME_CPUDETECT) && defined (CONFIG_GPL)
3d6a30d9 1510#if defined(ARCH_X86)
c14731d8 1511 // ordered per speed fastest first
221b804f
DB
1512 if (flags & SWS_CPU_CAPS_MMX2)
1513 return swScale_MMX2;
1514 else if (flags & SWS_CPU_CAPS_3DNOW)
1515 return swScale_3DNow;
1516 else if (flags & SWS_CPU_CAPS_MMX)
1517 return swScale_MMX;
1518 else
1519 return swScale_C;
28bf81c9
MN
1520
1521#else
a2faa401 1522#ifdef ARCH_POWERPC
221b804f
DB
1523 if (flags & SWS_CPU_CAPS_ALTIVEC)
1524 return swScale_altivec;
1525 else
1526 return swScale_C;
a2faa401 1527#endif
221b804f 1528 return swScale_C;
3d6a30d9 1529#endif /* defined(ARCH_X86) */
28bf81c9
MN
1530#else //RUNTIME_CPUDETECT
1531#ifdef HAVE_MMX2
221b804f 1532 return swScale_MMX2;
28bf81c9 1533#elif defined (HAVE_3DNOW)
221b804f 1534 return swScale_3DNow;
28bf81c9 1535#elif defined (HAVE_MMX)
221b804f 1536 return swScale_MMX;
a2faa401 1537#elif defined (HAVE_ALTIVEC)
221b804f 1538 return swScale_altivec;
28bf81c9 1539#else
221b804f 1540 return swScale_C;
28bf81c9
MN
1541#endif
1542#endif //!RUNTIME_CPUDETECT
31190492 1543}
7630f2e0 1544
d4e24275 1545static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1546 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1547 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1548 /* Copy Y plane */
1549 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1550 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1551 else
1552 {
1553 int i;
1554 uint8_t *srcPtr= src[0];
1555 uint8_t *dstPtr= dst;
1556 for (i=0; i<srcSliceH; i++)
1557 {
1558 memcpy(dstPtr, srcPtr, c->srcW);
1559 srcPtr+= srcStride[0];
1560 dstPtr+= dstStride[0];
1561 }
1562 }
1563 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1564 if (c->dstFormat == PIX_FMT_NV12)
30c48a0a 1565 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
221b804f 1566 else
30c48a0a 1567 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
221b804f
DB
1568
1569 return srcSliceH;
0d9f3d85
A
1570}
1571
d4e24275 1572static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1573 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1574 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
0d9f3d85 1575
30c48a0a 1576 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
fccb9b2b 1577
221b804f 1578 return srcSliceH;
0d9f3d85
A
1579}
1580
caeaabe7 1581static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1582 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1583 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
caeaabe7 1584
30c48a0a 1585 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
caeaabe7 1586
221b804f 1587 return srcSliceH;
caeaabe7
AB
1588}
1589
a6100f39
BC
1590static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1591 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1592 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1593
1594 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1595
1596 return srcSliceH;
1597}
1598
1599static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1600 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1601 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1602
1603 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1604
1605 return srcSliceH;
1606}
1607
9990e426 1608/* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
d4e24275 1609static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1610 int srcSliceH, uint8_t* dst[], int dstStride[]){
1611 const int srcFormat= c->srcFormat;
1612 const int dstFormat= c->dstFormat;
1613 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
1614 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
1615 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1616 const int dstId= fmt_depth(dstFormat) >> 2;
1617 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1618
1619 /* BGR -> BGR */
1620 if ( (isBGR(srcFormat) && isBGR(dstFormat))
1621 || (isRGB(srcFormat) && isRGB(dstFormat))){
1622 switch(srcId | (dstId<<4)){
1623 case 0x34: conv= rgb16to15; break;
1624 case 0x36: conv= rgb24to15; break;
1625 case 0x38: conv= rgb32to15; break;
1626 case 0x43: conv= rgb15to16; break;
1627 case 0x46: conv= rgb24to16; break;
1628 case 0x48: conv= rgb32to16; break;
1629 case 0x63: conv= rgb15to24; break;
1630 case 0x64: conv= rgb16to24; break;
1631 case 0x68: conv= rgb32to24; break;
1632 case 0x83: conv= rgb15to32; break;
1633 case 0x84: conv= rgb16to32; break;
1634 case 0x86: conv= rgb24to32; break;
3f0bc115 1635 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
221b804f
DB
1636 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1637 }
1638 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
1639 || (isRGB(srcFormat) && isBGR(dstFormat))){
1640 switch(srcId | (dstId<<4)){
1641 case 0x33: conv= rgb15tobgr15; break;
1642 case 0x34: conv= rgb16tobgr15; break;
1643 case 0x36: conv= rgb24tobgr15; break;
1644 case 0x38: conv= rgb32tobgr15; break;
1645 case 0x43: conv= rgb15tobgr16; break;
1646 case 0x44: conv= rgb16tobgr16; break;
1647 case 0x46: conv= rgb24tobgr16; break;
1648 case 0x48: conv= rgb32tobgr16; break;
1649 case 0x63: conv= rgb15tobgr24; break;
1650 case 0x64: conv= rgb16tobgr24; break;
1651 case 0x66: conv= rgb24tobgr24; break;
1652 case 0x68: conv= rgb32tobgr24; break;
1653 case 0x83: conv= rgb15tobgr32; break;
1654 case 0x84: conv= rgb16tobgr32; break;
1655 case 0x86: conv= rgb24tobgr32; break;
1656 case 0x88: conv= rgb32tobgr32; break;
3f0bc115 1657 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
221b804f
DB
1658 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1659 }
1660 }else{
3f0bc115 1661 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
221b804f
DB
1662 sws_format_name(srcFormat), sws_format_name(dstFormat));
1663 }
1664
068b0f4f
BF
1665 if(conv)
1666 {
9990e426
MN
1667 uint8_t *srcPtr= src[0];
1668 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
1669 srcPtr += ALT32_CORR;
1670
5efaf000 1671 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
9990e426 1672 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
c4ca31d0 1673 else
221b804f 1674 {
c4ca31d0 1675 int i;
c4ca31d0
BF
1676 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1677
1678 for (i=0; i<srcSliceH; i++)
1679 {
1680 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1681 srcPtr+= srcStride[0];
1682 dstPtr+= dstStride[0];
1683 }
221b804f
DB
1684 }
1685 }
1686 return srcSliceH;
0d9f3d85
A
1687}
1688
d4e24275 1689static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1690 int srcSliceH, uint8_t* dst[], int dstStride[]){
1691
1692 rgb24toyv12(
1693 src[0],
1694 dst[0]+ srcSliceY *dstStride[0],
1695 dst[1]+(srcSliceY>>1)*dstStride[1],
1696 dst[2]+(srcSliceY>>1)*dstStride[2],
1697 c->srcW, srcSliceH,
1698 dstStride[0], dstStride[1], srcStride[0]);
1699 return srcSliceH;
ec22603f
MN
1700}
1701
d4e24275 1702static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1703 int srcSliceH, uint8_t* dst[], int dstStride[]){
1704 int i;
1705
1706 /* copy Y */
1707 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
1708 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1709 else{
1710 uint8_t *srcPtr= src[0];
1711 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1712
1713 for (i=0; i<srcSliceH; i++)
1714 {
1715 memcpy(dstPtr, srcPtr, c->srcW);
1716 srcPtr+= srcStride[0];
1717 dstPtr+= dstStride[0];
1718 }
1719 }
1720
1721 if (c->dstFormat==PIX_FMT_YUV420P){
1722 planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1723 planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1724 }else{
1725 planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1726 planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1727 }
1728 return srcSliceH;
b241cbf2
MN
1729}
1730
b6654a54 1731/* unscaled copy like stuff (assumes nearly identical formats) */
2d35ae56 1732static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
bc5a0444
LB
1733 int srcSliceH, uint8_t* dst[], int dstStride[])
1734{
1735 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1736 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1737 else
1738 {
1739 int i;
1740 uint8_t *srcPtr= src[0];
1741 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1742 int length=0;
221b804f 1743
bc5a0444
LB
1744 /* universal length finder */
1745 while(length+c->srcW <= FFABS(dstStride[0])
1746 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
fcc402b1 1747 assert(length!=0);
2d35ae56 1748
bc5a0444 1749 for (i=0; i<srcSliceH; i++)
221b804f 1750 {
bc5a0444
LB
1751 memcpy(dstPtr, srcPtr, length);
1752 srcPtr+= srcStride[0];
1753 dstPtr+= dstStride[0];
221b804f 1754 }
bc5a0444 1755 }
2d35ae56
LB
1756 return srcSliceH;
1757}
bc5a0444 1758
2d35ae56
LB
1759static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1760 int srcSliceH, uint8_t* dst[], int dstStride[])
1761{
bc5a0444
LB
1762 int plane;
1763 for (plane=0; plane<3; plane++)
1764 {
1765 int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1766 int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1767 int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
2d35ae56 1768
bc5a0444 1769 if ((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
221b804f 1770 {
bc5a0444
LB
1771 if (!isGray(c->dstFormat))
1772 memset(dst[plane], 128, dstStride[plane]*height);
1773 }
1774 else
1775 {
1776 if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1777 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
221b804f
DB
1778 else
1779 {
bc5a0444
LB
1780 int i;
1781 uint8_t *srcPtr= src[plane];
1782 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1783 for (i=0; i<height; i++)
221b804f 1784 {
bc5a0444
LB
1785 memcpy(dstPtr, srcPtr, length);
1786 srcPtr+= srcStride[plane];
1787 dstPtr+= dstStride[plane];
221b804f
DB
1788 }
1789 }
1790 }
bc5a0444 1791 }
221b804f 1792 return srcSliceH;
37079906 1793}
28bf81c9 1794
4884b9e5 1795static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1796 int srcSliceH, uint8_t* dst[], int dstStride[]){
1797
1798 int length= c->srcW;
1799 int y= srcSliceY;
1800 int height= srcSliceH;
1801 int i, j;
1802 uint8_t *srcPtr= src[0];
1803 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1804
1805 if (!isGray(c->dstFormat)){
1806 int height= -((-srcSliceH)>>c->chrDstVSubSample);
1807 memset(dst[1], 128, dstStride[1]*height);
1808 memset(dst[2], 128, dstStride[2]*height);
1809 }
1810 if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
1811 for (i=0; i<height; i++)
1812 {
1813 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1814 srcPtr+= srcStride[0];
1815 dstPtr+= dstStride[0];
1816 }
1817 return srcSliceH;
4884b9e5
KS
1818}
1819
1820static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1821 int srcSliceH, uint8_t* dst[], int dstStride[]){
1822
1823 int length= c->srcW;
1824 int y= srcSliceY;
1825 int height= srcSliceH;
1826 int i, j;
1827 uint8_t *srcPtr= src[0];
1828 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1829 for (i=0; i<height; i++)
1830 {
1831 for (j=0; j<length; j++)
1832 {
1833 dstPtr[j<<1] = srcPtr[j];
1834 dstPtr[(j<<1)+1] = srcPtr[j];
1835 }
1836 srcPtr+= srcStride[0];
1837 dstPtr+= dstStride[0];
1838 }
1839 return srcSliceH;
4884b9e5
KS
1840}
1841
1842static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
1843 int srcSliceH, uint8_t* dst[], int dstStride[]){
1844
1845 int length= c->srcW;
1846 int y= srcSliceY;
1847 int height= srcSliceH;
1848 int i, j;
73d046e2 1849 uint16_t *srcPtr= (uint16_t*)src[0];
b8b015f4 1850 uint16_t *dstPtr= (uint16_t*)(dst[0] + dstStride[0]*y/2);
221b804f
DB
1851 for (i=0; i<height; i++)
1852 {
1853 for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
1854 srcPtr+= srcStride[0]/2;
1855 dstPtr+= dstStride[0]/2;
1856 }
1857 return srcSliceH;
4884b9e5
KS
1858}
1859
1860
c7a810cc 1861static void getSubSampleFactors(int *h, int *v, int format){
221b804f
DB
1862 switch(format){
1863 case PIX_FMT_UYVY422:
1864 case PIX_FMT_YUYV422:
1865 *h=1;
1866 *v=0;
1867 break;
1868 case PIX_FMT_YUV420P:
79973335 1869 case PIX_FMT_YUVA420P:
221b804f
DB
1870 case PIX_FMT_GRAY16BE:
1871 case PIX_FMT_GRAY16LE:
1872 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
1873 case PIX_FMT_NV12:
1874 case PIX_FMT_NV21:
1875 *h=1;
1876 *v=1;
1877 break;
9ba7fe6d
1878 case PIX_FMT_YUV440P:
1879 *h=0;
1880 *v=1;
1881 break;
221b804f
DB
1882 case PIX_FMT_YUV410P:
1883 *h=2;
1884 *v=2;
1885 break;
1886 case PIX_FMT_YUV444P:
1887 *h=0;
1888 *v=0;
1889 break;
1890 case PIX_FMT_YUV422P:
1891 *h=1;
1892 *v=0;
1893 break;
1894 case PIX_FMT_YUV411P:
1895 *h=2;
1896 *v=0;
1897 break;
1898 default:
1899 *h=0;
1900 *v=0;
1901 break;
1902 }
c7a810cc
MN
1903}
1904
5427e242 1905static uint16_t roundToInt16(int64_t f){
221b804f
DB
1906 int r= (f + (1<<15))>>16;
1907 if (r<-0x7FFF) return 0x8000;
1908 else if (r> 0x7FFF) return 0x7FFF;
1909 else return r;
0481412a
MN
1910}
1911
1912/**
5427e242 1913 * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
86bdf3fd 1914 * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
5427e242 1915 * @return -1 if not supported
0481412a 1916 */
5427e242 1917int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
221b804f
DB
1918 int64_t crv = inv_table[0];
1919 int64_t cbu = inv_table[1];
1920 int64_t cgu = -inv_table[2];
1921 int64_t cgv = -inv_table[3];
1922 int64_t cy = 1<<16;
1923 int64_t oy = 0;
1924
221b804f
DB
1925 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
1926 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
1927
1928 c->brightness= brightness;
1929 c->contrast = contrast;
1930 c->saturation= saturation;
1931 c->srcRange = srcRange;
1932 c->dstRange = dstRange;
6bc0c792 1933 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return 0;
221b804f
DB
1934
1935 c->uOffset= 0x0400040004000400LL;
1936 c->vOffset= 0x0400040004000400LL;
1937
1938 if (!srcRange){
1939 cy= (cy*255) / 219;
1940 oy= 16<<16;
1941 }else{
1942 crv= (crv*224) / 255;
1943 cbu= (cbu*224) / 255;
1944 cgu= (cgu*224) / 255;
1945 cgv= (cgv*224) / 255;
1946 }
0481412a 1947
221b804f
DB
1948 cy = (cy *contrast )>>16;
1949 crv= (crv*contrast * saturation)>>32;
1950 cbu= (cbu*contrast * saturation)>>32;
1951 cgu= (cgu*contrast * saturation)>>32;
1952 cgv= (cgv*contrast * saturation)>>32;
0481412a 1953
221b804f 1954 oy -= 256*brightness;
0481412a 1955
221b804f
DB
1956 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
1957 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
1958 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
1959 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
1960 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
1961 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
5427e242 1962
43c16478
MN
1963 c->yuv2rgb_y_coeff = (int16_t)roundToInt16(cy <<13);
1964 c->yuv2rgb_y_offset = (int16_t)roundToInt16(oy << 9);
1965 c->yuv2rgb_v2r_coeff= (int16_t)roundToInt16(crv<<13);
1966 c->yuv2rgb_v2g_coeff= (int16_t)roundToInt16(cgv<<13);
1967 c->yuv2rgb_u2g_coeff= (int16_t)roundToInt16(cgu<<13);
1968 c->yuv2rgb_u2b_coeff= (int16_t)roundToInt16(cbu<<13);
f0faee4c 1969
221b804f
DB
1970 yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
1971 //FIXME factorize
a31de956 1972
6634d0ef 1973#ifdef COMPILE_ALTIVEC
221b804f
DB
1974 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
1975 yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
6a4970ab 1976#endif
221b804f 1977 return 0;
5427e242
MN
1978}
1979
1980/**
1981 * @return -1 if not supported
1982 */
1983int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
221b804f 1984 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
5427e242 1985
221b804f
DB
1986 *inv_table = c->srcColorspaceTable;
1987 *table = c->dstColorspaceTable;
1988 *srcRange = c->srcRange;
1989 *dstRange = c->dstRange;
1990 *brightness= c->brightness;
1991 *contrast = c->contrast;
1992 *saturation= c->saturation;
6a4970ab 1993
221b804f 1994 return 0;
0481412a
MN
1995}
1996
44cdb423
LA
1997static int handle_jpeg(int *format)
1998{
221b804f
DB
1999 switch (*format) {
2000 case PIX_FMT_YUVJ420P:
2001 *format = PIX_FMT_YUV420P;
2002 return 1;
2003 case PIX_FMT_YUVJ422P:
2004 *format = PIX_FMT_YUV422P;
2005 return 1;
2006 case PIX_FMT_YUVJ444P:
2007 *format = PIX_FMT_YUV444P;
2008 return 1;
9ba7fe6d
2009 case PIX_FMT_YUVJ440P:
2010 *format = PIX_FMT_YUV440P;
2011 return 1;
221b804f
DB
2012 default:
2013 return 0;
2014 }
44cdb423
LA
2015}
2016
e9e12f0e 2017SwsContext *sws_getContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
221b804f
DB
2018 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
2019
2020 SwsContext *c;
2021 int i;
2022 int usesVFilter, usesHFilter;
2023 int unscaled, needsDither;
2024 int srcRange, dstRange;
2025 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
3d6a30d9 2026#if defined(ARCH_X86)
221b804f
DB
2027 if (flags & SWS_CPU_CAPS_MMX)
2028 asm volatile("emms\n\t"::: "memory");
5cebb24b 2029#endif
516b1f82 2030
9bde778e 2031#if !defined(RUNTIME_CPUDETECT) || !defined (CONFIG_GPL) //ensure that the flags match the compiled variant if cpudetect is off
d3f3eea9 2032 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
516b1f82 2033#ifdef HAVE_MMX2
221b804f 2034 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
516b1f82 2035#elif defined (HAVE_3DNOW)
221b804f 2036 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
516b1f82 2037#elif defined (HAVE_MMX)
221b804f 2038 flags |= SWS_CPU_CAPS_MMX;
a2faa401 2039#elif defined (HAVE_ALTIVEC)
221b804f 2040 flags |= SWS_CPU_CAPS_ALTIVEC;
721d5e3b 2041#elif defined (ARCH_BFIN)
d3f3eea9 2042 flags |= SWS_CPU_CAPS_BFIN;
516b1f82 2043#endif
69796008 2044#endif /* RUNTIME_CPUDETECT */
221b804f 2045 if (clip_table[512] != 255) globalInit();
1b0a4572 2046 if (!rgb15to16) sws_rgb2rgb_init(flags);
221b804f
DB
2047
2048 unscaled = (srcW == dstW && srcH == dstH);
2049 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2050 && (fmt_depth(dstFormat))<24
2051 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2052
2053 srcRange = handle_jpeg(&srcFormat);
2054 dstRange = handle_jpeg(&dstFormat);
2055
2056 if (!isSupportedIn(srcFormat))
2057 {
0d6fd5ec 2058 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
221b804f
DB
2059 return NULL;
2060 }
2061 if (!isSupportedOut(dstFormat))
2062 {
0d6fd5ec 2063 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
221b804f
DB
2064 return NULL;
2065 }
2066
010c00bc
MN
2067 i= flags & ( SWS_POINT
2068 |SWS_AREA
6afc7c19 2069 |SWS_BILINEAR
010c00bc
MN
2070 |SWS_FAST_BILINEAR
2071 |SWS_BICUBIC
2072 |SWS_X
2073 |SWS_GAUSS
2074 |SWS_LANCZOS
2075 |SWS_SINC
2076 |SWS_SPLINE
2077 |SWS_BICUBLIN);
2078 if(!i || (i & (i-1)))
2079 {
2080 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be choosen\n");
2081 return NULL;
2082 }
010c00bc 2083
221b804f
DB
2084 /* sanity check */
2085 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
2086 {
2087 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2088 srcW, srcH, dstW, dstH);
2089 return NULL;
2090 }
8b2fce0d
MN
2091 if(srcW > VOFW || dstW > VOFW){
2092 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile time max width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2093 return NULL;
2094 }
221b804f
DB
2095
2096 if (!dstFilter) dstFilter= &dummyFilter;
2097 if (!srcFilter) srcFilter= &dummyFilter;
2098
2099 c= av_mallocz(sizeof(SwsContext));
2100
2101 c->av_class = &sws_context_class;
2102 c->srcW= srcW;
2103 c->srcH= srcH;
2104 c->dstW= dstW;
2105 c->dstH= dstH;
2106 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2107 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2108 c->flags= flags;
2109 c->dstFormat= dstFormat;
2110 c->srcFormat= srcFormat;
2111 c->vRounder= 4* 0x0001000100010001ULL;
2112
2113 usesHFilter= usesVFilter= 0;
1b0a4572
BF
2114 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2115 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2116 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2117 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2118 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2119 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2120 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2121 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
221b804f
DB
2122
2123 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2124 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2125
2126 // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
2127 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2128
2129 // drop some chroma lines if the user wants it
2130 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2131 c->chrSrcVSubSample+= c->vChrDrop;
2132
2133 // drop every 2. pixel for chroma calculation unless user wants full chroma
2134 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2135 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2136 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
dfb09bd1 2137 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2f60f629 2138 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
221b804f
DB
2139 c->chrSrcHSubSample=1;
2140
2141 if (param){
2142 c->param[0] = param[0];
2143 c->param[1] = param[1];
2144 }else{
2145 c->param[0] =
2146 c->param[1] = SWS_PARAM_DEFAULT;
2147 }
2148
2149 c->chrIntHSubSample= c->chrDstHSubSample;
2150 c->chrIntVSubSample= c->chrSrcVSubSample;
2151
2152 // Note the -((-x)>>y) is so that we always round toward +inf.
2153 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2154 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2155 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2156 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2157
2158 sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], srcRange, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
2159
2160 /* unscaled special Cases */
6bc0c792 2161 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
221b804f
DB
2162 {
2163 /* yv12_to_nv12 */
2164 if (srcFormat == PIX_FMT_YUV420P && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2165 {
2166 c->swScale= PlanarToNV12Wrapper;
2167 }
9bde778e 2168#ifdef CONFIG_GPL
221b804f 2169 /* yuv2bgr */
4155ece5
MN
2170 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P) && (isBGR(dstFormat) || isRGB(dstFormat))
2171 && !(flags & SWS_ACCURATE_RND))
221b804f
DB
2172 {
2173 c->swScale= yuv2rgb_get_func_ptr(c);
2174 }
9bde778e 2175#endif
6a4970ab 2176
84c12535 2177 if (srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_BITEXACT))
221b804f
DB
2178 {
2179 c->swScale= yvu9toyv12Wrapper;
2180 }
2181
2182 /* bgr24toYV12 */
08857704 2183 if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_ACCURATE_RND))
221b804f
DB
2184 c->swScale= bgr24toyv12Wrapper;
2185
2186 /* rgb/bgr -> rgb/bgr (no dither needed forms) */
2187 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2188 && (isBGR(dstFormat) || isRGB(dstFormat))
2189 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2190 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2191 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2192 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2193 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2194 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2195 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
ec1bca2a 2196 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
9990e426
MN
2197 && dstFormat != PIX_FMT_RGB32_1
2198 && dstFormat != PIX_FMT_BGR32_1
736143c8 2199 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
221b804f
DB
2200 c->swScale= rgb2rgbWrapper;
2201
a6100f39
BC
2202 if (srcFormat == PIX_FMT_YUV422P)
2203 {
2204 if (dstFormat == PIX_FMT_YUYV422)
2205 c->swScale= YUV422PToYuy2Wrapper;
2206 else if (dstFormat == PIX_FMT_UYVY422)
2207 c->swScale= YUV422PToUyvyWrapper;
2208 }
2209
221b804f
DB
2210 /* LQ converters if -sws 0 or -sws 4*/
2211 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
221b804f 2212 /* yv12_to_yuy2 */
3b38f1c6 2213 if (srcFormat == PIX_FMT_YUV420P)
221b804f
DB
2214 {
2215 if (dstFormat == PIX_FMT_YUYV422)
2216 c->swScale= PlanarToYuy2Wrapper;
3b38f1c6 2217 else if (dstFormat == PIX_FMT_UYVY422)
221b804f
DB
2218 c->swScale= PlanarToUyvyWrapper;
2219 }
2220 }
ec22603f 2221
6634d0ef 2222#ifdef COMPILE_ALTIVEC
221b804f 2223 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
3b38f1c6 2224 srcFormat == PIX_FMT_YUV420P) {
221b804f
DB
2225 // unscaled YV12 -> packed YUV, we want speed
2226 if (dstFormat == PIX_FMT_YUYV422)
2227 c->swScale= yv12toyuy2_unscaled_altivec;
3b38f1c6 2228 else if (dstFormat == PIX_FMT_UYVY422)
221b804f
DB
2229 c->swScale= yv12touyvy_unscaled_altivec;
2230 }
b71cf33c
RD
2231#endif
2232
221b804f
DB
2233 /* simple copy */
2234 if ( srcFormat == dstFormat
2235 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
e5091488 2236 || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
221b804f 2237 {
2d35ae56
LB
2238 if (isPacked(c->srcFormat))
2239 c->swScale= packedCopy;
2240 else /* Planar YUV or gray */
2241 c->swScale= planarCopy;
221b804f
DB
2242 }
2243
2244 /* gray16{le,be} conversions */
2245 if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2246 {
2247 c->swScale= gray16togray;
2248 }
2249 if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2250 {
2251 c->swScale= graytogray16;
2252 }
2253 if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2254 {
2255 c->swScale= gray16swap;
2256 }
2257
1ebbfe15
MH
2258#ifdef ARCH_BFIN
2259 if (flags & SWS_CPU_CAPS_BFIN)
2260 ff_bfin_get_unscaled_swscale (c);
2261#endif
2262
221b804f
DB
2263 if (c->swScale){
2264 if (flags&SWS_PRINT_INFO)
4b0c30b7 2265 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
221b804f
DB
2266 sws_format_name(srcFormat), sws_format_name(dstFormat));
2267 return c;
2268 }
2269 }
2270
2271 if (flags & SWS_CPU_CAPS_MMX2)
2272 {
2273 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2274 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2275 {
2276 if (flags&SWS_PRINT_INFO)
4b0c30b7 2277 av_log(c, AV_LOG_INFO, "output Width is not a multiple of 32 -> no MMX2 scaler\n");
221b804f
DB
2278 }
2279 if (usesHFilter) c->canMMX2BeUsed=0;
2280 }
2281 else
2282 c->canMMX2BeUsed=0;
2283
2284 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2285 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2286
2287 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2288 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2289 // n-2 is the last chrominance sample available
86bdf3fd 2290 // this is not perfect, but no one should notice the difference, the more correct variant
221b804f
DB
2291 // would be like the vertical one, but that would require some special code for the
2292 // first and last pixel
2293 if (flags&SWS_FAST_BILINEAR)
2294 {
2295 if (c->canMMX2BeUsed)
2296 {
2297 c->lumXInc+= 20;
2298 c->chrXInc+= 20;
2299 }
2300 //we don't use the x86asm scaler if mmx is available
2301 else if (flags & SWS_CPU_CAPS_MMX)
2302 {
2303 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2304 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2305 }
2306 }
2307
2308 /* precalculate horizontal scaler filter coefficients */
2309 {
2310 const int filterAlign=
2311 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2312 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2313 1;
2314
2315 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2316 srcW , dstW, filterAlign, 1<<14,
2317 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2318 srcFilter->lumH, dstFilter->lumH, c->param);
2319 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2320 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2321 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2322 srcFilter->chrH, dstFilter->chrH, c->param);
28bf81c9 2323
dbdae6ec 2324#define MAX_FUNNY_CODE_SIZE 10000
17c613ef 2325#if defined(COMPILE_MMX2)
77a416e8 2326// can't downscale !!!
221b804f
DB
2327 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2328 {
113ef149 2329#ifdef MAP_ANONYMOUS
221b804f
DB
2330 c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2331 c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
38d5c282 2332#else
221b804f
DB
2333 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2334 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
38d5c282
AJ
2335#endif
2336
221b804f
DB
2337 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2338 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2339 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2340 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
b7dc6f66 2341
221b804f
DB
2342 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2343 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2344 }
17c613ef 2345#endif /* defined(COMPILE_MMX2) */
221b804f 2346 } // Init Horizontal stuff
28bf81c9
MN
2347
2348
2349
221b804f
DB
2350 /* precalculate vertical scaler filter coefficients */
2351 {
2352 const int filterAlign=
2353 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2354 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2355 1;
8c266f0c 2356
221b804f 2357 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
5fe4aad7 2358 srcH , dstH, filterAlign, (1<<12),
221b804f
DB
2359 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2360 srcFilter->lumV, dstFilter->lumV, c->param);
2361 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
5fe4aad7 2362 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
221b804f
DB
2363 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2364 srcFilter->chrV, dstFilter->chrV, c->param);
d33d485e
AC
2365
2366#ifdef HAVE_ALTIVEC
221b804f
DB
2367 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2368 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2369
2370 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2371 int j;
2372 short *p = (short *)&c->vYCoeffsBank[i];
2373 for (j=0;j<8;j++)
2374 p[j] = c->vLumFilter[i];
2375 }
2376
2377 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2378 int j;
2379 short *p = (short *)&c->vCCoeffsBank[i];
2380 for (j=0;j<8;j++)
2381 p[j] = c->vChrFilter[i];
2382 }
d33d485e 2383#endif
221b804f
DB
2384 }
2385
2386 // Calculate Buffer Sizes so that they won't run out while handling these damn slices
2387 c->vLumBufSize= c->vLumFilterSize;
2388 c->vChrBufSize= c->vChrFilterSize;
2389 for (i=0; i<dstH; i++)
2390 {
2391 int chrI= i*c->chrDstH / dstH;
2392 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2393 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2394
2395 nextSlice>>= c->chrSrcVSubSample;
2396 nextSlice<<= c->chrSrcVSubSample;
2397 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
e5091488 2398 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
221b804f
DB
2399 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2400 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2401 }
2402
2403 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2404 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2405 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2406 //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)
2407 /* align at 16 bytes for AltiVec */
2408 for (i=0; i<c->vLumBufSize; i++)
8b2fce0d 2409 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
221b804f 2410 for (i=0; i<c->vChrBufSize; i++)
8b2fce0d 2411 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
221b804f
DB
2412
2413 //try to avoid drawing green stuff between the right end and the stride end
8b2fce0d
MN
2414 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2415
5a352b14 2416 assert(2*VOFW == VOF);
221b804f 2417
fcc402b1 2418 assert(c->chrDstH <= dstH);
221b804f
DB
2419
2420 if (flags&SWS_PRINT_INFO)
2421 {
28bf81c9 2422#ifdef DITHER1XBPP
3e62b7e3 2423 const char *dither= " dithered";
5521b193 2424#else
3e62b7e3 2425 const char *dither= "";
28bf81c9 2426#endif
221b804f 2427 if (flags&SWS_FAST_BILINEAR)
4b0c30b7 2428 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
221b804f 2429 else if (flags&SWS_BILINEAR)
4b0c30b7 2430 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
221b804f 2431 else if (flags&SWS_BICUBIC)
4b0c30b7 2432 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
221b804f 2433 else if (flags&SWS_X)
4b0c30b7 2434 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
221b804f 2435 else if (flags&SWS_POINT)
4b0c30b7 2436 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
221b804f 2437 else if (flags&SWS_AREA)
4b0c30b7 2438 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
221b804f 2439 else if (flags&SWS_BICUBLIN)
4b0c30b7 2440 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
221b804f 2441 else if (flags&SWS_GAUSS)
4b0c30b7 2442 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
221b804f 2443 else if (flags&SWS_SINC)
4b0c30b7 2444 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
221b804f 2445 else if (flags&SWS_LANCZOS)
4b0c30b7 2446 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
221b804f 2447 else if (flags&SWS_SPLINE)
4b0c30b7 2448 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
221b804f 2449 else
4b0c30b7 2450 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
221b804f
DB
2451
2452 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2453 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2454 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2455 else
2456 av_log(c, AV_LOG_INFO, "from %s to %s ",
2457 sws_format_name(srcFormat), sws_format_name(dstFormat));
2458
2459 if (flags & SWS_CPU_CAPS_MMX2)
2460 av_log(c, AV_LOG_INFO, "using MMX2\n");
2461 else if (flags & SWS_CPU_CAPS_3DNOW)
2462 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2463 else if (flags & SWS_CPU_CAPS_MMX)
2464 av_log(c, AV_LOG_INFO, "using MMX\n");
2465 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2466 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2467 else
2468 av_log(c, AV_LOG_INFO, "using C\n");
2469 }
2470
2471 if (flags & SWS_PRINT_INFO)
2472 {
2473 if (flags & SWS_CPU_CAPS_MMX)
2474 {
2475 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
4b0c30b7 2476 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
221b804f
DB
2477 else
2478 {
2479 if (c->hLumFilterSize==4)
4b0c30b7 2480 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
221b804f 2481 else if (c->hLumFilterSize==8)
4b0c30b7 2482 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
221b804f 2483 else
4b0c30b7 2484 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
221b804f
DB
2485
2486 if (c->hChrFilterSize==4)
4b0c30b7 2487 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
221b804f 2488 else if (c->hChrFilterSize==8)
4b0c30b7 2489 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
221b804f 2490 else
4b0c30b7 2491 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
221b804f
DB
2492 }
2493 }
2494 else
2495 {
3d6a30d9 2496#if defined(ARCH_X86)
4b0c30b7 2497 av_log(c, AV_LOG_VERBOSE, "using X86-Asm scaler for horizontal scaling\n");
28bf81c9 2498#else
221b804f 2499 if (flags & SWS_FAST_BILINEAR)
4b0c30b7 2500 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
221b804f 2501 else
4b0c30b7 2502 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
28bf81c9 2503#endif
221b804f
DB
2504 }
2505 if (isPlanarYUV(dstFormat))
2506 {
2507 if (c->vLumFilterSize==1)
4b0c30b7 2508 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2509 else
4b0c30b7 2510 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f
DB
2511 }
2512 else
2513 {
2514 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
4b0c30b7
BC
2515 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2516 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2517 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
4b0c30b7 2518 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2519 else
4b0c30b7 2520 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f
DB
2521 }
2522
2523 if (dstFormat==PIX_FMT_BGR24)
4b0c30b7 2524 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 Converter\n",
221b804f
DB
2525 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2526 else if (dstFormat==PIX_FMT_RGB32)
4b0c30b7 2527 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2528 else if (dstFormat==PIX_FMT_BGR565)
4b0c30b7 2529 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2530 else if (dstFormat==PIX_FMT_BGR555)
4b0c30b7 2531 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
221b804f 2532
4b0c30b7 2533 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
221b804f
DB
2534 }
2535 if (flags & SWS_PRINT_INFO)
2536 {
4b0c30b7 2537 av_log(c, AV_LOG_DEBUG, "Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
221b804f 2538 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
4b0c30b7 2539 av_log(c, AV_LOG_DEBUG, "Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
221b804f
DB
2540 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2541 }
2542
2543 c->swScale= getSwsFunc(flags);
2544 return c;
28bf81c9
MN
2545}
2546
2547/**
56b69633 2548 * swscale wrapper, so we don't need to export the SwsContext.
fccb9b2b
MN
2549 * assumes planar YUV to be in YUV order instead of YVU
2550 */
703b56fb 2551int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2552 int srcSliceH, uint8_t* dst[], int dstStride[]){
2553 int i;
2554 uint8_t* src2[4]= {src[0], src[1], src[2]};
2555 uint32_t pal[256];
14623020
MN
2556 int use_pal= c->srcFormat == PIX_FMT_PAL8
2557 || c->srcFormat == PIX_FMT_BGR4_BYTE
2558 || c->srcFormat == PIX_FMT_RGB4_BYTE
2559 || c->srcFormat == PIX_FMT_BGR8
2560 || c->srcFormat == PIX_FMT_RGB8;
2561
221b804f 2562 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
3f0bc115 2563 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
221b804f
DB
2564 return 0;
2565 }
2566 if (c->sliceDir == 0) {
2567 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2568 }
2569
14623020 2570 if (use_pal){
221b804f 2571 for (i=0; i<256; i++){
14623020
MN
2572 int p, r, g, b,y,u,v;
2573 if(c->srcFormat == PIX_FMT_PAL8){
2574 p=((uint32_t*)(src[1]))[i];
2575 r= (p>>16)&0xFF;
2576 g= (p>> 8)&0xFF;
2577 b= p &0xFF;
2578 }else if(c->srcFormat == PIX_FMT_RGB8){
2579 r= (i>>5 )*36;
2580 g= ((i>>2)&7)*36;
2581 b= (i&3 )*85;
2582 }else if(c->srcFormat == PIX_FMT_BGR8){
2583 b= (i>>6 )*85;
2584 g= ((i>>3)&7)*36;
2585 r= (i&7 )*36;
2586 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
2587 r= (i>>3 )*255;
2588 g= ((i>>1)&3)*85;
2589 b= (i&1 )*255;
2590 }else if(c->srcFormat == PIX_FMT_BGR4_BYTE){
2591 b= (i>>3 )*255;
2592 g= ((i>>1)&3)*85;
2593 r= (i&1 )*255;
2594 }
cbcb408f
MN
2595 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2596 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2597 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
221b804f 2598 pal[i]= y + (u<<8) + (v<<16);
21c08a3f 2599 }
74498eb4 2600 src2[1]= (uint8_t*)pal;
221b804f 2601 }
21c08a3f 2602
221b804f
DB
2603 // copy strides, so they can safely be modified
2604 if (c->sliceDir == 1) {
2605 // slices go from top to bottom
2606 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2]};
2607 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2]};
2608 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2609 } else {
2610 // slices go from bottom to top => we flip the image internally
2611 uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
2612 dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
2613 dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
2614 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2]};
2615 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2]};
2616
2617 src2[0] += (srcSliceH-1)*srcStride[0];
14623020 2618 if (!use_pal)
221b804f
DB
2619 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2620 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2621
2622 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2623 }
fccb9b2b
MN
2624}
2625
2626/**
56b69633 2627 * swscale wrapper, so we don't need to export the SwsContext
d4e24275 2628 */
703b56fb 2629int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
221b804f
DB
2630 int srcSliceH, uint8_t* dst[], int dstStride[]){
2631 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
d4e24275 2632}
d4e24275 2633
6a4970ab 2634SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
221b804f
DB
2635 float lumaSharpen, float chromaSharpen,
2636 float chromaHShift, float chromaVShift,
2637 int verbose)
e21206a8 2638{
221b804f
DB
2639 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2640
2641 if (lumaGBlur!=0.0){
2642 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2643 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2644 }else{
2645 filter->lumH= sws_getIdentityVec();
2646 filter->lumV= sws_getIdentityVec();
2647 }
2648
2649 if (chromaGBlur!=0.0){
2650 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2651 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2652 }else{
2653 filter->chrH= sws_getIdentityVec();
2654 filter->chrV= sws_getIdentityVec();
2655 }
2656
2657 if (chromaSharpen!=0.0){
2658 SwsVector *id= sws_getIdentityVec();
2659 sws_scaleVec(filter->chrH, -chromaSharpen);
2660 sws_scaleVec(filter->chrV, -chromaSharpen);
2661 sws_addVec(filter->chrH, id);
2662 sws_addVec(filter->chrV, id);
2663 sws_freeVec(id);
2664 }
2665
2666 if (lumaSharpen!=0.0){
2667 SwsVector *id= sws_getIdentityVec();
2668 sws_scaleVec(filter->lumH, -lumaSharpen);
2669 sws_scaleVec(filter->lumV, -lumaSharpen);
2670 sws_addVec(filter->lumH, id);
2671 sws_addVec(filter->lumV, id);
2672 sws_freeVec(id);
2673 }
2674
2675 if (chromaHShift != 0.0)
2676 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2677
2678 if (chromaVShift != 0.0)
2679 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2680
2681 sws_normalizeVec(filter->chrH, 1.0);
2682 sws_normalizeVec(filter->chrV, 1.0);
2683 sws_normalizeVec(filter->lumH, 1.0);
2684 sws_normalizeVec(filter->lumV, 1.0);
2685
2686 if (verbose) sws_printVec(filter->chrH);
2687 if (verbose) sws_printVec(filter->lumH);
2688
2689 return filter;
e21206a8
MN
2690}
2691
d4e24275 2692/**
28bf81c9
MN
2693 * returns a normalized gaussian curve used to filter stuff
2694 * quality=3 is high quality, lowwer is lowwer quality
2695 */
d4e24275 2696SwsVector *sws_getGaussianVec(double variance, double quality){
221b804f
DB
2697 const int length= (int)(variance*quality + 0.5) | 1;
2698 int i;
2699 double *coeff= av_malloc(length*sizeof(double));
2700 double middle= (length-1)*0.5;
2701 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2702
221b804f
DB
2703 vec->coeff= coeff;
2704 vec->length= length;
28bf81c9 2705
221b804f
DB
2706 for (i=0; i<length; i++)
2707 {
2708 double dist= i-middle;
e5091488 2709 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
221b804f 2710 }
28bf81c9 2711
221b804f 2712 sws_normalizeVec(vec, 1.0);
c7f822d9 2713
221b804f 2714 return vec;
28bf81c9
MN
2715}
2716
d4e24275 2717SwsVector *sws_getConstVec(double c, int length){
221b804f
DB
2718 int i;
2719 double *coeff= av_malloc(length*sizeof(double));
2720 SwsVector *vec= av_malloc(sizeof(SwsVector));
5521b193 2721
221b804f
DB
2722 vec->coeff= coeff;
2723 vec->length= length;
5521b193 2724
221b804f
DB
2725 for (i=0; i<length; i++)
2726 coeff[i]= c;
5521b193 2727
221b804f 2728 return vec;
5521b193
MN
2729}
2730
2731
d4e24275 2732SwsVector *sws_getIdentityVec(void){
221b804f 2733 return sws_getConstVec(1.0, 1);
c7f822d9
MN
2734}
2735
2e728364 2736double sws_dcVec(SwsVector *a){
221b804f
DB
2737 int i;
2738 double sum=0;
28bf81c9 2739
221b804f
DB
2740 for (i=0; i<a->length; i++)
2741 sum+= a->coeff[i];
28bf81c9 2742
221b804f 2743 return sum;
28bf81c9
MN
2744}
2745
d4e24275 2746void sws_scaleVec(SwsVector *a, double scalar){
221b804f 2747 int i;
c7f822d9 2748
221b804f
DB
2749 for (i=0; i<a->length; i++)
2750 a->coeff[i]*= scalar;
c7f822d9
MN
2751}
2752
2e728364 2753void sws_normalizeVec(SwsVector *a, double height){
221b804f 2754 sws_scaleVec(a, height/sws_dcVec(a));
2e728364
MN
2755}
2756
d4e24275 2757static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
221b804f
DB
2758 int length= a->length + b->length - 1;
2759 double *coeff= av_malloc(length*sizeof(double));
2760 int i, j;
2761 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2762
221b804f
DB
2763 vec->coeff= coeff;
2764 vec->length= length;
28bf81c9 2765
221b804f 2766 for (i=0; i<length; i++) coeff[i]= 0.0;
28bf81c9 2767
221b804f
DB
2768 for (i=0; i<a->length; i++)
2769 {
2770 for (j=0; j<b->length; j++)
2771 {
2772 coeff[i+j]+= a->coeff[i]*b->coeff[j];
2773 }
2774 }
28bf81c9 2775
221b804f 2776 return vec;
28bf81c9
MN
2777}
2778
d4e24275 2779static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
221b804f
DB
2780 int length= FFMAX(a->length, b->length);
2781 double *coeff= av_malloc(length*sizeof(double));
2782 int i;
2783 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2784
221b804f
DB
2785 vec->coeff= coeff;
2786 vec->length= length;
28bf81c9 2787
221b804f 2788 for (i=0; i<length; i++) coeff[i]= 0.0;
28bf81c9 2789
221b804f
DB
2790 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2791 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
c7f822d9 2792
221b804f 2793 return vec;
28bf81c9 2794}
c7f822d9 2795
d4e24275 2796static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
221b804f
DB
2797 int length= FFMAX(a->length, b->length);
2798 double *coeff= av_malloc(length*sizeof(double));
2799 int i;
2800 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2801
221b804f
DB
2802 vec->coeff= coeff;
2803 vec->length= length;
c7f822d9 2804
221b804f 2805 for (i=0; i<length; i++) coeff[i]= 0.0;
c7f822d9 2806
221b804f
DB
2807 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2808 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
c7f822d9 2809
221b804f 2810 return vec;
c7f822d9
MN
2811}
2812
2813/* shift left / or right if "shift" is negative */
d4e24275 2814static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
221b804f
DB
2815 int length= a->length + FFABS(shift)*2;
2816 double *coeff= av_malloc(length*sizeof(double));
2817 int i;
2818 SwsVector *vec= av_malloc(sizeof(SwsVector));
c7f822d9 2819
221b804f
DB
2820 vec->coeff= coeff;
2821 vec->length= length;
c7f822d9 2822
221b804f 2823 for (i=0; i<length; i++) coeff[i]= 0.0;
c7f822d9 2824
221b804f
DB
2825 for (i=0; i<a->length; i++)
2826 {
2827 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2828 }
c7f822d9 2829
221b804f 2830 return vec;
c7f822d9
MN
2831}
2832
d4e24275 2833void sws_shiftVec(SwsVector *a, int shift){
221b804f
DB
2834 SwsVector *shifted= sws_getShiftedVec(a, shift);
2835 av_free(a->coeff);
2836 a->coeff= shifted->coeff;
2837 a->length= shifted->length;
2838 av_free(shifted);
5cebb24b
MN
2839}
2840
d4e24275 2841void sws_addVec(SwsVector *a, SwsVector *b){
221b804f
DB
2842 SwsVector *sum= sws_sumVec(a, b);
2843 av_free(a->coeff);
2844 a->coeff= sum->coeff;
2845 a->length= sum->length;
2846 av_free(sum);
5cebb24b
MN
2847}
2848
d4e24275 2849void sws_subVec(SwsVector *a, SwsVector *b){
221b804f
DB
2850 SwsVector *diff= sws_diffVec(a, b);
2851 av_free(a->coeff);
2852 a->coeff= diff->coeff;
2853 a->length= diff->length;
2854 av_free(diff);
5cebb24b
MN
2855}
2856
d4e24275 2857void sws_convVec(SwsVector *a, SwsVector *b){
221b804f
DB
2858 SwsVector *conv= sws_getConvVec(a, b);
2859 av_free(a->coeff);
2860 a->coeff= conv->coeff;
2861 a->length= conv->length;
2862 av_free(conv);
5cebb24b
MN
2863}
2864
d4e24275 2865SwsVector *sws_cloneVec(SwsVector *a){
221b804f
DB
2866 double *coeff= av_malloc(a->length*sizeof(double));
2867 int i;
2868 SwsVector *vec= av_malloc(sizeof(SwsVector));
5cebb24b 2869
221b804f
DB
2870 vec->coeff= coeff;
2871 vec->length= a->length;
5cebb24b 2872
221b804f 2873 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
5cebb24b 2874
221b804f 2875 return vec;
5cebb24b
MN
2876}
2877
d4e24275 2878void sws_printVec(SwsVector *a){
221b804f
DB
2879 int i;
2880 double max=0;
2881 double min=0;
2882 double range;
2883
2884 for (i=0; i<a->length; i++)
2885 if (a->coeff[i]>max) max= a->coeff[i];
2886
2887 for (i=0; i<a->length; i++)
2888 if (a->coeff[i]<min) min= a->coeff[i];
2889
2890 range= max - min;
2891
2892 for (i=0; i<a->length; i++)
2893 {
2894 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
2895 av_log(NULL, AV_LOG_DEBUG, "%1.3f ", a->coeff[i]);
2896 for (;x>0; x--) av_log(NULL, AV_LOG_DEBUG, " ");
2897 av_log(NULL, AV_LOG_DEBUG, "|\n");
2898 }
c7f822d9
MN
2899}
2900
d4e24275 2901void sws_freeVec(SwsVector *a){
221b804f 2902 if (!a) return;
47b7382d 2903 av_freep(&a->coeff);
221b804f
DB
2904 a->length=0;
2905 av_free(a);
c7f822d9
MN
2906}
2907
e21206a8 2908void sws_freeFilter(SwsFilter *filter){
221b804f 2909 if (!filter) return;
e21206a8 2910
221b804f
DB
2911 if (filter->lumH) sws_freeVec(filter->lumH);
2912 if (filter->lumV) sws_freeVec(filter->lumV);
2913 if (filter->chrH) sws_freeVec(filter->chrH);
2914 if (filter->chrV) sws_freeVec(filter->chrV);
2915 av_free(filter);
e21206a8
MN
2916}
2917
2918
d4e24275 2919void sws_freeContext(SwsContext *c){
221b804f
DB
2920 int i;
2921 if (!c) return;
2922
2923 if (c->lumPixBuf)
2924 {
2925 for (i=0; i<c->vLumBufSize; i++)
47b7382d
MN
2926 av_freep(&c->lumPixBuf[i]);
2927 av_freep(&c->lumPixBuf);
221b804f
DB
2928 }
2929
2930 if (c->chrPixBuf)
2931 {
2932 for (i=0; i<c->vChrBufSize; i++)
47b7382d
MN
2933 av_freep(&c->chrPixBuf[i]);
2934 av_freep(&c->chrPixBuf);
221b804f
DB
2935 }
2936
47b7382d
MN
2937 av_freep(&c->vLumFilter);
2938 av_freep(&c->vChrFilter);
2939 av_freep(&c->hLumFilter);
2940 av_freep(&c->hChrFilter);
d33d485e 2941#ifdef HAVE_ALTIVEC
47b7382d
MN
2942 av_freep(&c->vYCoeffsBank);
2943 av_freep(&c->vCCoeffsBank);
d33d485e 2944#endif
c7f822d9 2945
47b7382d
MN
2946 av_freep(&c->vLumFilterPos);
2947 av_freep(&c->vChrFilterPos);
2948 av_freep(&c->hLumFilterPos);
2949 av_freep(&c->hChrFilterPos);
c7f822d9 2950
9bde778e 2951#if defined(ARCH_X86) && defined(CONFIG_GPL)
0ce5275d 2952#ifdef MAP_ANONYMOUS
221b804f
DB
2953 if (c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
2954 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
38d5c282 2955#else
221b804f
DB
2956 av_free(c->funnyYCode);
2957 av_free(c->funnyUVCode);
38d5c282 2958#endif
221b804f
DB
2959 c->funnyYCode=NULL;
2960 c->funnyUVCode=NULL;
3d6a30d9 2961#endif /* defined(ARCH_X86) */
38d5c282 2962
47b7382d
MN
2963 av_freep(&c->lumMmx2Filter);
2964 av_freep(&c->chrMmx2Filter);
2965 av_freep(&c->lumMmx2FilterPos);
2966 av_freep(&c->chrMmx2FilterPos);
2967 av_freep(&c->yuvTable);
221b804f
DB
2968
2969 av_free(c);
c7f822d9
MN
2970}
2971
22e46959
VP
2972/**
2973 * Checks if context is valid or reallocs a new one instead.
2974 * If context is NULL, just calls sws_getContext() to get a new one.
2975 * Otherwise, checks if the parameters are the same already saved in context.
2976 * If that is the case, returns the current context.
2977 * Otherwise, frees context and gets a new one.
2978 *
2979 * Be warned that srcFilter, dstFilter are not checked, they are
2980 * asumed to remain valid.
2981 */
2982struct SwsContext *sws_getCachedContext(struct SwsContext *context,
221b804f
DB
2983 int srcW, int srcH, int srcFormat,
2984 int dstW, int dstH, int dstFormat, int flags,
2985 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
22e46959 2986{
337a2a45
KMH
2987 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
2988
2989 if (!param)
2990 param = default_param;
2991
1b0a4572 2992 if (context) {
b8db4e22
BF
2993 if (context->srcW != srcW || context->srcH != srcH ||
2994 context->srcFormat != srcFormat ||
2995 context->dstW != dstW || context->dstH != dstH ||
2996 context->dstFormat != dstFormat || context->flags != flags ||
2997 context->param[0] != param[0] || context->param[1] != param[1])
22e46959
VP
2998 {
2999 sws_freeContext(context);
3000 context = NULL;
3001 }
3002 }
1b0a4572 3003 if (!context) {
22e46959 3004 return sws_getContext(srcW, srcH, srcFormat,
221b804f
DB
3005 dstW, dstH, dstFormat, flags,
3006 srcFilter, dstFilter, param);
22e46959
VP
3007 }
3008 return context;
3009}
3010