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