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