Add support for PIX_FMT_MONOWHITE as output format.
[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_MONO2_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]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
615 dest++;\
616 }\
617
618
619 #define YSCALE_YUV_2_MONOX_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]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~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 case PIX_FMT_MONOWHITE:\
750 {\
751 func_monoblack\
752 }\
753 break;\
754 case PIX_FMT_YUYV422:\
755 func2\
756 ((uint8_t*)dest)[2*i2+0]= Y1;\
757 ((uint8_t*)dest)[2*i2+1]= U;\
758 ((uint8_t*)dest)[2*i2+2]= Y2;\
759 ((uint8_t*)dest)[2*i2+3]= V;\
760 } \
761 break;\
762 case PIX_FMT_UYVY422:\
763 func2\
764 ((uint8_t*)dest)[2*i2+0]= U;\
765 ((uint8_t*)dest)[2*i2+1]= Y1;\
766 ((uint8_t*)dest)[2*i2+2]= V;\
767 ((uint8_t*)dest)[2*i2+3]= Y2;\
768 } \
769 break;\
770 case PIX_FMT_GRAY16BE:\
771 func_g16\
772 ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
773 ((uint8_t*)dest)[2*i2+1]= Y1;\
774 ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
775 ((uint8_t*)dest)[2*i2+3]= Y2;\
776 } \
777 break;\
778 case PIX_FMT_GRAY16LE:\
779 func_g16\
780 ((uint8_t*)dest)[2*i2+0]= Y1;\
781 ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
782 ((uint8_t*)dest)[2*i2+2]= Y2;\
783 ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
784 } \
785 break;\
786 }\
787
788
789 static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
790 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
791 uint8_t *dest, int dstW, int y)
792 {
793 int i;
794 YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
795 }
796
797 static inline void yuv2rgbXinC_full(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
798 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
799 uint8_t *dest, int dstW, int y)
800 {
801 int i;
802 int step= fmt_depth(c->dstFormat)/8;
803
804 switch(c->dstFormat){
805 case PIX_FMT_ARGB:
806 dest++;
807 case PIX_FMT_RGB24:
808 case PIX_FMT_RGBA:
809 YSCALE_YUV_2_RGBX_FULL_C(1<<21)
810 dest[0]= R>>22;
811 dest[1]= G>>22;
812 dest[2]= B>>22;
813 dest[3]= 0;
814 dest+= step;
815 }
816 break;
817 case PIX_FMT_ABGR:
818 dest++;
819 case PIX_FMT_BGR24:
820 case PIX_FMT_BGRA:
821 YSCALE_YUV_2_RGBX_FULL_C(1<<21)
822 dest[0]= B>>22;
823 dest[1]= G>>22;
824 dest[2]= R>>22;
825 dest[3]= 0;
826 dest+= step;
827 }
828 break;
829 default:
830 assert(0);
831 }
832 }
833
834 //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
835 //Plain C versions
836 #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT) || !defined(CONFIG_GPL)
837 #define COMPILE_C
838 #endif
839
840 #ifdef ARCH_POWERPC
841 #if (defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
842 #define COMPILE_ALTIVEC
843 #endif //HAVE_ALTIVEC
844 #endif //ARCH_POWERPC
845
846 #if defined(ARCH_X86)
847
848 #if ((defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
849 #define COMPILE_MMX
850 #endif
851
852 #if (defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
853 #define COMPILE_MMX2
854 #endif
855
856 #if ((defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
857 #define COMPILE_3DNOW
858 #endif
859 #endif //ARCH_X86 || ARCH_X86_64
860
861 #undef HAVE_MMX
862 #undef HAVE_MMX2
863 #undef HAVE_3DNOW
864
865 #ifdef COMPILE_C
866 #undef HAVE_MMX
867 #undef HAVE_MMX2
868 #undef HAVE_3DNOW
869 #undef HAVE_ALTIVEC
870 #define RENAME(a) a ## _C
871 #include "swscale_template.c"
872 #endif
873
874 #ifdef COMPILE_ALTIVEC
875 #undef RENAME
876 #define HAVE_ALTIVEC
877 #define RENAME(a) a ## _altivec
878 #include "swscale_template.c"
879 #endif
880
881 #if defined(ARCH_X86)
882
883 //X86 versions
884 /*
885 #undef RENAME
886 #undef HAVE_MMX
887 #undef HAVE_MMX2
888 #undef HAVE_3DNOW
889 #define ARCH_X86
890 #define RENAME(a) a ## _X86
891 #include "swscale_template.c"
892 */
893 //MMX versions
894 #ifdef COMPILE_MMX
895 #undef RENAME
896 #define HAVE_MMX
897 #undef HAVE_MMX2
898 #undef HAVE_3DNOW
899 #define RENAME(a) a ## _MMX
900 #include "swscale_template.c"
901 #endif
902
903 //MMX2 versions
904 #ifdef COMPILE_MMX2
905 #undef RENAME
906 #define HAVE_MMX
907 #define HAVE_MMX2
908 #undef HAVE_3DNOW
909 #define RENAME(a) a ## _MMX2
910 #include "swscale_template.c"
911 #endif
912
913 //3DNOW versions
914 #ifdef COMPILE_3DNOW
915 #undef RENAME
916 #define HAVE_MMX
917 #undef HAVE_MMX2
918 #define HAVE_3DNOW
919 #define RENAME(a) a ## _3DNow
920 #include "swscale_template.c"
921 #endif
922
923 #endif //ARCH_X86 || ARCH_X86_64
924
925 // minor note: the HAVE_xyz is messed up after that line so don't use it
926
927 static double getSplineCoeff(double a, double b, double c, double d, double dist)
928 {
929 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
930 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
931 else return getSplineCoeff( 0.0,
932 b+ 2.0*c + 3.0*d,
933 c + 3.0*d,
934 -b- 3.0*c - 6.0*d,
935 dist-1.0);
936 }
937
938 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
939 int srcW, int dstW, int filterAlign, int one, int flags,
940 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
941 {
942 int i;
943 int filterSize;
944 int filter2Size;
945 int minFilterSize;
946 double *filter=NULL;
947 double *filter2=NULL;
948 int ret= -1;
949 #if defined(ARCH_X86)
950 if (flags & SWS_CPU_CAPS_MMX)
951 asm volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
952 #endif
953
954 // Note the +1 is for the MMXscaler which reads over the end
955 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
956
957 if (FFABS(xInc - 0x10000) <10) // unscaled
958 {
959 int i;
960 filterSize= 1;
961 filter= av_malloc(dstW*sizeof(double)*filterSize);
962 for (i=0; i<dstW*filterSize; i++) filter[i]=0;
963
964 for (i=0; i<dstW; i++)
965 {
966 filter[i*filterSize]=1;
967 (*filterPos)[i]=i;
968 }
969
970 }
971 else if (flags&SWS_POINT) // lame looking point sampling mode
972 {
973 int i;
974 int xDstInSrc;
975 filterSize= 1;
976 filter= av_malloc(dstW*sizeof(double)*filterSize);
977
978 xDstInSrc= xInc/2 - 0x8000;
979 for (i=0; i<dstW; i++)
980 {
981 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
982
983 (*filterPos)[i]= xx;
984 filter[i]= 1.0;
985 xDstInSrc+= xInc;
986 }
987 }
988 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
989 {
990 int i;
991 int xDstInSrc;
992 if (flags&SWS_BICUBIC) filterSize= 4;
993 else if (flags&SWS_X ) filterSize= 4;
994 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
995 filter= av_malloc(dstW*sizeof(double)*filterSize);
996
997 xDstInSrc= xInc/2 - 0x8000;
998 for (i=0; i<dstW; i++)
999 {
1000 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1001 int j;
1002
1003 (*filterPos)[i]= xx;
1004 //Bilinear upscale / linear interpolate / Area averaging
1005 for (j=0; j<filterSize; j++)
1006 {
1007 double d= FFABS((xx<<16) - xDstInSrc)/(double)(1<<16);
1008 double coeff= 1.0 - d;
1009 if (coeff<0) coeff=0;
1010 filter[i*filterSize + j]= coeff;
1011 xx++;
1012 }
1013 xDstInSrc+= xInc;
1014 }
1015 }
1016 else
1017 {
1018 double xDstInSrc;
1019 double sizeFactor, filterSizeInSrc;
1020 const double xInc1= (double)xInc / (double)(1<<16);
1021
1022 if (flags&SWS_BICUBIC) sizeFactor= 4.0;
1023 else if (flags&SWS_X) sizeFactor= 8.0;
1024 else if (flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
1025 else if (flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
1026 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
1027 else if (flags&SWS_SINC) sizeFactor= 20.0; // infinite ;)
1028 else if (flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
1029 else if (flags&SWS_BILINEAR) sizeFactor= 2.0;
1030 else {
1031 sizeFactor= 0.0; //GCC warning killer
1032 assert(0);
1033 }
1034
1035 if (xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
1036 else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
1037
1038 filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
1039 if (filterSize > srcW-2) filterSize=srcW-2;
1040
1041 filter= av_malloc(dstW*sizeof(double)*filterSize);
1042
1043 xDstInSrc= xInc1 / 2.0 - 0.5;
1044 for (i=0; i<dstW; i++)
1045 {
1046 int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
1047 int j;
1048 (*filterPos)[i]= xx;
1049 for (j=0; j<filterSize; j++)
1050 {
1051 double d= FFABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
1052 double coeff;
1053 if (flags & SWS_BICUBIC)
1054 {
1055 double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
1056 double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
1057
1058 if (d<1.0)
1059 coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
1060 else if (d<2.0)
1061 coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
1062 else
1063 coeff=0.0;
1064 }
1065 /* else if (flags & SWS_X)
1066 {
1067 double p= param ? param*0.01 : 0.3;
1068 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1069 coeff*= pow(2.0, - p*d*d);
1070 }*/
1071 else if (flags & SWS_X)
1072 {
1073 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1074
1075 if (d<1.0)
1076 coeff = cos(d*PI);
1077 else
1078 coeff=-1.0;
1079 if (coeff<0.0) coeff= -pow(-coeff, A);
1080 else coeff= pow( coeff, A);
1081 coeff= coeff*0.5 + 0.5;
1082 }
1083 else if (flags & SWS_AREA)
1084 {
1085 double srcPixelSize= 1.0/xInc1;
1086 if (d + srcPixelSize/2 < 0.5) coeff= 1.0;
1087 else if (d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
1088 else coeff=0.0;
1089 }
1090 else if (flags & SWS_GAUSS)
1091 {
1092 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1093 coeff = pow(2.0, - p*d*d);
1094 }
1095 else if (flags & SWS_SINC)
1096 {
1097 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1098 }
1099 else if (flags & SWS_LANCZOS)
1100 {
1101 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1102 coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
1103 if (d>p) coeff=0;
1104 }
1105 else if (flags & SWS_BILINEAR)
1106 {
1107 coeff= 1.0 - d;
1108 if (coeff<0) coeff=0;
1109 }
1110 else if (flags & SWS_SPLINE)
1111 {
1112 double p=-2.196152422706632;
1113 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
1114 }
1115 else {
1116 coeff= 0.0; //GCC warning killer
1117 assert(0);
1118 }
1119
1120 filter[i*filterSize + j]= coeff;
1121 xx++;
1122 }
1123 xDstInSrc+= xInc1;
1124 }
1125 }
1126
1127 /* apply src & dst Filter to filter -> filter2
1128 av_free(filter);
1129 */
1130 assert(filterSize>0);
1131 filter2Size= filterSize;
1132 if (srcFilter) filter2Size+= srcFilter->length - 1;
1133 if (dstFilter) filter2Size+= dstFilter->length - 1;
1134 assert(filter2Size>0);
1135 filter2= av_malloc(filter2Size*dstW*sizeof(double));
1136
1137 for (i=0; i<dstW; i++)
1138 {
1139 int j;
1140 SwsVector scaleFilter;
1141 SwsVector *outVec;
1142
1143 scaleFilter.coeff= filter + i*filterSize;
1144 scaleFilter.length= filterSize;
1145
1146 if (srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
1147 else outVec= &scaleFilter;
1148
1149 assert(outVec->length == filter2Size);
1150 //FIXME dstFilter
1151
1152 for (j=0; j<outVec->length; j++)
1153 {
1154 filter2[i*filter2Size + j]= outVec->coeff[j];
1155 }
1156
1157 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1158
1159 if (outVec != &scaleFilter) sws_freeVec(outVec);
1160 }
1161 av_freep(&filter);
1162
1163 /* try to reduce the filter-size (step1 find size and shift left) */
1164 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1165 minFilterSize= 0;
1166 for (i=dstW-1; i>=0; i--)
1167 {
1168 int min= filter2Size;
1169 int j;
1170 double cutOff=0.0;
1171
1172 /* get rid off near zero elements on the left by shifting left */
1173 for (j=0; j<filter2Size; j++)
1174 {
1175 int k;
1176 cutOff += FFABS(filter2[i*filter2Size]);
1177
1178 if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1179
1180 /* preserve monotonicity because the core can't handle the filter otherwise */
1181 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1182
1183 // Move filter coeffs left
1184 for (k=1; k<filter2Size; k++)
1185 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1186 filter2[i*filter2Size + k - 1]= 0.0;
1187 (*filterPos)[i]++;
1188 }
1189
1190 cutOff=0.0;
1191 /* count near zeros on the right */
1192 for (j=filter2Size-1; j>0; j--)
1193 {
1194 cutOff += FFABS(filter2[i*filter2Size + j]);
1195
1196 if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1197 min--;
1198 }
1199
1200 if (min>minFilterSize) minFilterSize= min;
1201 }
1202
1203 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1204 // we can handle the special case 4,
1205 // so we don't want to go to the full 8
1206 if (minFilterSize < 5)
1207 filterAlign = 4;
1208
1209 // we really don't want to waste our time
1210 // doing useless computation, so fall-back on
1211 // the scalar C code for very small filter.
1212 // vectorizing is worth it only if you have
1213 // decent-sized vector.
1214 if (minFilterSize < 3)
1215 filterAlign = 1;
1216 }
1217
1218 if (flags & SWS_CPU_CAPS_MMX) {
1219 // special case for unscaled vertical filtering
1220 if (minFilterSize == 1 && filterAlign == 2)
1221 filterAlign= 1;
1222 }
1223
1224 assert(minFilterSize > 0);
1225 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1226 assert(filterSize > 0);
1227 filter= av_malloc(filterSize*dstW*sizeof(double));
1228 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
1229 goto error;
1230 *outFilterSize= filterSize;
1231
1232 if (flags&SWS_PRINT_INFO)
1233 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1234 /* try to reduce the filter-size (step2 reduce it) */
1235 for (i=0; i<dstW; i++)
1236 {
1237 int j;
1238
1239 for (j=0; j<filterSize; j++)
1240 {
1241 if (j>=filter2Size) filter[i*filterSize + j]= 0.0;
1242 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1243 }
1244 }
1245
1246
1247 //FIXME try to align filterpos if possible
1248
1249 //fix borders
1250 for (i=0; i<dstW; i++)
1251 {
1252 int j;
1253 if ((*filterPos)[i] < 0)
1254 {
1255 // Move filter coeffs left to compensate for filterPos
1256 for (j=1; j<filterSize; j++)
1257 {
1258 int left= FFMAX(j + (*filterPos)[i], 0);
1259 filter[i*filterSize + left] += filter[i*filterSize + j];
1260 filter[i*filterSize + j]=0;
1261 }
1262 (*filterPos)[i]= 0;
1263 }
1264
1265 if ((*filterPos)[i] + filterSize > srcW)
1266 {
1267 int shift= (*filterPos)[i] + filterSize - srcW;
1268 // Move filter coeffs right to compensate for filterPos
1269 for (j=filterSize-2; j>=0; j--)
1270 {
1271 int right= FFMIN(j + shift, filterSize-1);
1272 filter[i*filterSize +right] += filter[i*filterSize +j];
1273 filter[i*filterSize +j]=0;
1274 }
1275 (*filterPos)[i]= srcW - filterSize;
1276 }
1277 }
1278
1279 // Note the +1 is for the MMXscaler which reads over the end
1280 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1281 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1282
1283 /* Normalize & Store in outFilter */
1284 for (i=0; i<dstW; i++)
1285 {
1286 int j;
1287 double error=0;
1288 double sum=0;
1289 double scale= one;
1290
1291 for (j=0; j<filterSize; j++)
1292 {
1293 sum+= filter[i*filterSize + j];
1294 }
1295 scale/= sum;
1296 for (j=0; j<*outFilterSize; j++)
1297 {
1298 double v= filter[i*filterSize + j]*scale + error;
1299 int intV= floor(v + 0.5);
1300 (*outFilter)[i*(*outFilterSize) + j]= intV;
1301 error = v - intV;
1302 }
1303 }
1304
1305 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1306 for (i=0; i<*outFilterSize; i++)
1307 {
1308 int j= dstW*(*outFilterSize);
1309 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1310 }
1311
1312 ret=0;
1313 error:
1314 av_free(filter);
1315 av_free(filter2);
1316 return ret;
1317 }
1318
1319 #ifdef COMPILE_MMX2
1320 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1321 {
1322 uint8_t *fragmentA;
1323 long imm8OfPShufW1A;
1324 long imm8OfPShufW2A;
1325 long fragmentLengthA;
1326 uint8_t *fragmentB;
1327 long imm8OfPShufW1B;
1328 long imm8OfPShufW2B;
1329 long fragmentLengthB;
1330 int fragmentPos;
1331
1332 int xpos, i;
1333
1334 // create an optimized horizontal scaling routine
1335
1336 //code fragment
1337
1338 asm volatile(
1339 "jmp 9f \n\t"
1340 // Begin
1341 "0: \n\t"
1342 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1343 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1344 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1345 "punpcklbw %%mm7, %%mm1 \n\t"
1346 "punpcklbw %%mm7, %%mm0 \n\t"
1347 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1348 "1: \n\t"
1349 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1350 "2: \n\t"
1351 "psubw %%mm1, %%mm0 \n\t"
1352 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1353 "pmullw %%mm3, %%mm0 \n\t"
1354 "psllw $7, %%mm1 \n\t"
1355 "paddw %%mm1, %%mm0 \n\t"
1356
1357 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1358
1359 "add $8, %%"REG_a" \n\t"
1360 // End
1361 "9: \n\t"
1362 // "int $3 \n\t"
1363 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1364 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1365 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1366 "dec %1 \n\t"
1367 "dec %2 \n\t"
1368 "sub %0, %1 \n\t"
1369 "sub %0, %2 \n\t"
1370 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1371 "sub %0, %3 \n\t"
1372
1373
1374 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1375 "=r" (fragmentLengthA)
1376 );
1377
1378 asm volatile(
1379 "jmp 9f \n\t"
1380 // Begin
1381 "0: \n\t"
1382 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1383 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1384 "punpcklbw %%mm7, %%mm0 \n\t"
1385 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1386 "1: \n\t"
1387 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1388 "2: \n\t"
1389 "psubw %%mm1, %%mm0 \n\t"
1390 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1391 "pmullw %%mm3, %%mm0 \n\t"
1392 "psllw $7, %%mm1 \n\t"
1393 "paddw %%mm1, %%mm0 \n\t"
1394
1395 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1396
1397 "add $8, %%"REG_a" \n\t"
1398 // End
1399 "9: \n\t"
1400 // "int $3 \n\t"
1401 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
1402 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
1403 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
1404 "dec %1 \n\t"
1405 "dec %2 \n\t"
1406 "sub %0, %1 \n\t"
1407 "sub %0, %2 \n\t"
1408 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
1409 "sub %0, %3 \n\t"
1410
1411
1412 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1413 "=r" (fragmentLengthB)
1414 );
1415
1416 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1417 fragmentPos=0;
1418
1419 for (i=0; i<dstW/numSplits; i++)
1420 {
1421 int xx=xpos>>16;
1422
1423 if ((i&3) == 0)
1424 {
1425 int a=0;
1426 int b=((xpos+xInc)>>16) - xx;
1427 int c=((xpos+xInc*2)>>16) - xx;
1428 int d=((xpos+xInc*3)>>16) - xx;
1429
1430 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1431 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1432 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1433 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1434 filterPos[i/2]= xx;
1435
1436 if (d+1<4)
1437 {
1438 int maxShift= 3-(d+1);
1439 int shift=0;
1440
1441 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1442
1443 funnyCode[fragmentPos + imm8OfPShufW1B]=
1444 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1445 funnyCode[fragmentPos + imm8OfPShufW2B]=
1446 a | (b<<2) | (c<<4) | (d<<6);
1447
1448 if (i+3>=dstW) shift=maxShift; //avoid overread
1449 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1450
1451 if (shift && i>=shift)
1452 {
1453 funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1454 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1455 filterPos[i/2]-=shift;
1456 }
1457
1458 fragmentPos+= fragmentLengthB;
1459 }
1460 else
1461 {
1462 int maxShift= 3-d;
1463 int shift=0;
1464
1465 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1466
1467 funnyCode[fragmentPos + imm8OfPShufW1A]=
1468 funnyCode[fragmentPos + imm8OfPShufW2A]=
1469 a | (b<<2) | (c<<4) | (d<<6);
1470
1471 if (i+4>=dstW) shift=maxShift; //avoid overread
1472 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1473
1474 if (shift && i>=shift)
1475 {
1476 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1477 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1478 filterPos[i/2]-=shift;
1479 }
1480
1481 fragmentPos+= fragmentLengthA;
1482 }
1483
1484 funnyCode[fragmentPos]= RET;
1485 }
1486 xpos+=xInc;
1487 }
1488 filterPos[i/2]= xpos>>16; // needed to jump to the next part
1489 }
1490 #endif /* COMPILE_MMX2 */
1491
1492 static void globalInit(void){
1493 // generating tables:
1494 int i;
1495 for (i=0; i<768; i++){
1496 int c= av_clip_uint8(i-256);
1497 clip_table[i]=c;
1498 }
1499 }
1500
1501 static SwsFunc getSwsFunc(int flags){
1502
1503 #if defined(RUNTIME_CPUDETECT) && defined (CONFIG_GPL)
1504 #if defined(ARCH_X86)
1505 // ordered per speed fastest first
1506 if (flags & SWS_CPU_CAPS_MMX2)
1507 return swScale_MMX2;
1508 else if (flags & SWS_CPU_CAPS_3DNOW)
1509 return swScale_3DNow;
1510 else if (flags & SWS_CPU_CAPS_MMX)
1511 return swScale_MMX;
1512 else
1513 return swScale_C;
1514
1515 #else
1516 #ifdef ARCH_POWERPC
1517 if (flags & SWS_CPU_CAPS_ALTIVEC)
1518 return swScale_altivec;
1519 else
1520 return swScale_C;
1521 #endif
1522 return swScale_C;
1523 #endif /* defined(ARCH_X86) */
1524 #else //RUNTIME_CPUDETECT
1525 #ifdef HAVE_MMX2
1526 return swScale_MMX2;
1527 #elif defined (HAVE_3DNOW)
1528 return swScale_3DNow;
1529 #elif defined (HAVE_MMX)
1530 return swScale_MMX;
1531 #elif defined (HAVE_ALTIVEC)
1532 return swScale_altivec;
1533 #else
1534 return swScale_C;
1535 #endif
1536 #endif //!RUNTIME_CPUDETECT
1537 }
1538
1539 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1540 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1541 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1542 /* Copy Y plane */
1543 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1544 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1545 else
1546 {
1547 int i;
1548 uint8_t *srcPtr= src[0];
1549 uint8_t *dstPtr= dst;
1550 for (i=0; i<srcSliceH; i++)
1551 {
1552 memcpy(dstPtr, srcPtr, c->srcW);
1553 srcPtr+= srcStride[0];
1554 dstPtr+= dstStride[0];
1555 }
1556 }
1557 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1558 if (c->dstFormat == PIX_FMT_NV12)
1559 interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
1560 else
1561 interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
1562
1563 return srcSliceH;
1564 }
1565
1566 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1567 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1568 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1569
1570 yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1571
1572 return srcSliceH;
1573 }
1574
1575 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1576 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1577 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1578
1579 yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
1580
1581 return srcSliceH;
1582 }
1583
1584 static int YUV422PToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1585 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1586 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1587
1588 yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1589
1590 return srcSliceH;
1591 }
1592
1593 static int YUV422PToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1594 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1595 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1596
1597 yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
1598
1599 return srcSliceH;
1600 }
1601
1602 /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
1603 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1604 int srcSliceH, uint8_t* dst[], int dstStride[]){
1605 const int srcFormat= c->srcFormat;
1606 const int dstFormat= c->dstFormat;
1607 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
1608 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
1609 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1610 const int dstId= fmt_depth(dstFormat) >> 2;
1611 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1612
1613 /* BGR -> BGR */
1614 if ( (isBGR(srcFormat) && isBGR(dstFormat))
1615 || (isRGB(srcFormat) && isRGB(dstFormat))){
1616 switch(srcId | (dstId<<4)){
1617 case 0x34: conv= rgb16to15; break;
1618 case 0x36: conv= rgb24to15; break;
1619 case 0x38: conv= rgb32to15; break;
1620 case 0x43: conv= rgb15to16; break;
1621 case 0x46: conv= rgb24to16; break;
1622 case 0x48: conv= rgb32to16; break;
1623 case 0x63: conv= rgb15to24; break;
1624 case 0x64: conv= rgb16to24; break;
1625 case 0x68: conv= rgb32to24; break;
1626 case 0x83: conv= rgb15to32; break;
1627 case 0x84: conv= rgb16to32; break;
1628 case 0x86: conv= rgb24to32; break;
1629 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1630 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1631 }
1632 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
1633 || (isRGB(srcFormat) && isBGR(dstFormat))){
1634 switch(srcId | (dstId<<4)){
1635 case 0x33: conv= rgb15tobgr15; break;
1636 case 0x34: conv= rgb16tobgr15; break;
1637 case 0x36: conv= rgb24tobgr15; break;
1638 case 0x38: conv= rgb32tobgr15; break;
1639 case 0x43: conv= rgb15tobgr16; break;
1640 case 0x44: conv= rgb16tobgr16; break;
1641 case 0x46: conv= rgb24tobgr16; break;
1642 case 0x48: conv= rgb32tobgr16; break;
1643 case 0x63: conv= rgb15tobgr24; break;
1644 case 0x64: conv= rgb16tobgr24; break;
1645 case 0x66: conv= rgb24tobgr24; break;
1646 case 0x68: conv= rgb32tobgr24; break;
1647 case 0x83: conv= rgb15tobgr32; break;
1648 case 0x84: conv= rgb16tobgr32; break;
1649 case 0x86: conv= rgb24tobgr32; break;
1650 case 0x88: conv= rgb32tobgr32; break;
1651 default: av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1652 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1653 }
1654 }else{
1655 av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
1656 sws_format_name(srcFormat), sws_format_name(dstFormat));
1657 }
1658
1659 if(conv)
1660 {
1661 uint8_t *srcPtr= src[0];
1662 if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
1663 srcPtr += ALT32_CORR;
1664
1665 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
1666 conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1667 else
1668 {
1669 int i;
1670 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1671
1672 for (i=0; i<srcSliceH; i++)
1673 {
1674 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1675 srcPtr+= srcStride[0];
1676 dstPtr+= dstStride[0];
1677 }
1678 }
1679 }
1680 return srcSliceH;
1681 }
1682
1683 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1684 int srcSliceH, uint8_t* dst[], int dstStride[]){
1685
1686 rgb24toyv12(
1687 src[0],
1688 dst[0]+ srcSliceY *dstStride[0],
1689 dst[1]+(srcSliceY>>1)*dstStride[1],
1690 dst[2]+(srcSliceY>>1)*dstStride[2],
1691 c->srcW, srcSliceH,
1692 dstStride[0], dstStride[1], srcStride[0]);
1693 return srcSliceH;
1694 }
1695
1696 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1697 int srcSliceH, uint8_t* dst[], int dstStride[]){
1698 int i;
1699
1700 /* copy Y */
1701 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
1702 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1703 else{
1704 uint8_t *srcPtr= src[0];
1705 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1706
1707 for (i=0; i<srcSliceH; i++)
1708 {
1709 memcpy(dstPtr, srcPtr, c->srcW);
1710 srcPtr+= srcStride[0];
1711 dstPtr+= dstStride[0];
1712 }
1713 }
1714
1715 if (c->dstFormat==PIX_FMT_YUV420P){
1716 planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1717 planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1718 }else{
1719 planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1720 planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1721 }
1722 return srcSliceH;
1723 }
1724
1725 /* unscaled copy like stuff (assumes nearly identical formats) */
1726 static int packedCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1727 int srcSliceH, uint8_t* dst[], int dstStride[])
1728 {
1729 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1730 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1731 else
1732 {
1733 int i;
1734 uint8_t *srcPtr= src[0];
1735 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1736 int length=0;
1737
1738 /* universal length finder */
1739 while(length+c->srcW <= FFABS(dstStride[0])
1740 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1741 assert(length!=0);
1742
1743 for (i=0; i<srcSliceH; i++)
1744 {
1745 memcpy(dstPtr, srcPtr, length);
1746 srcPtr+= srcStride[0];
1747 dstPtr+= dstStride[0];
1748 }
1749 }
1750 return srcSliceH;
1751 }
1752
1753 static int planarCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1754 int srcSliceH, uint8_t* dst[], int dstStride[])
1755 {
1756 int plane;
1757 for (plane=0; plane<3; plane++)
1758 {
1759 int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1760 int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1761 int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1762
1763 if ((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
1764 {
1765 if (!isGray(c->dstFormat))
1766 memset(dst[plane], 128, dstStride[plane]*height);
1767 }
1768 else
1769 {
1770 if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1771 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1772 else
1773 {
1774 int i;
1775 uint8_t *srcPtr= src[plane];
1776 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1777 for (i=0; i<height; i++)
1778 {
1779 memcpy(dstPtr, srcPtr, length);
1780 srcPtr+= srcStride[plane];
1781 dstPtr+= dstStride[plane];
1782 }
1783 }
1784 }
1785 }
1786 return srcSliceH;
1787 }
1788
1789 static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1790 int srcSliceH, uint8_t* dst[], int dstStride[]){
1791
1792 int length= c->srcW;
1793 int y= srcSliceY;
1794 int height= srcSliceH;
1795 int i, j;
1796 uint8_t *srcPtr= src[0];
1797 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1798
1799 if (!isGray(c->dstFormat)){
1800 int height= -((-srcSliceH)>>c->chrDstVSubSample);
1801 memset(dst[1], 128, dstStride[1]*height);
1802 memset(dst[2], 128, dstStride[2]*height);
1803 }
1804 if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
1805 for (i=0; i<height; i++)
1806 {
1807 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1808 srcPtr+= srcStride[0];
1809 dstPtr+= dstStride[0];
1810 }
1811 return srcSliceH;
1812 }
1813
1814 static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1815 int srcSliceH, uint8_t* dst[], int dstStride[]){
1816
1817 int length= c->srcW;
1818 int y= srcSliceY;
1819 int height= srcSliceH;
1820 int i, j;
1821 uint8_t *srcPtr= src[0];
1822 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1823 for (i=0; i<height; i++)
1824 {
1825 for (j=0; j<length; j++)
1826 {
1827 dstPtr[j<<1] = srcPtr[j];
1828 dstPtr[(j<<1)+1] = srcPtr[j];
1829 }
1830 srcPtr+= srcStride[0];
1831 dstPtr+= dstStride[0];
1832 }
1833 return srcSliceH;
1834 }
1835
1836 static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1837 int srcSliceH, uint8_t* dst[], int dstStride[]){
1838
1839 int length= c->srcW;
1840 int y= srcSliceY;
1841 int height= srcSliceH;
1842 int i, j;
1843 uint16_t *srcPtr= (uint16_t*)src[0];
1844 uint16_t *dstPtr= (uint16_t*)(dst[0] + dstStride[0]*y/2);
1845 for (i=0; i<height; i++)
1846 {
1847 for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
1848 srcPtr+= srcStride[0]/2;
1849 dstPtr+= dstStride[0]/2;
1850 }
1851 return srcSliceH;
1852 }
1853
1854
1855 static void getSubSampleFactors(int *h, int *v, int format){
1856 switch(format){
1857 case PIX_FMT_UYVY422:
1858 case PIX_FMT_YUYV422:
1859 *h=1;
1860 *v=0;
1861 break;
1862 case PIX_FMT_YUV420P:
1863 case PIX_FMT_YUVA420P:
1864 case PIX_FMT_GRAY16BE:
1865 case PIX_FMT_GRAY16LE:
1866 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
1867 case PIX_FMT_NV12:
1868 case PIX_FMT_NV21:
1869 *h=1;
1870 *v=1;
1871 break;
1872 case PIX_FMT_YUV440P:
1873 *h=0;
1874 *v=1;
1875 break;
1876 case PIX_FMT_YUV410P:
1877 *h=2;
1878 *v=2;
1879 break;
1880 case PIX_FMT_YUV444P:
1881 *h=0;
1882 *v=0;
1883 break;
1884 case PIX_FMT_YUV422P:
1885 *h=1;
1886 *v=0;
1887 break;
1888 case PIX_FMT_YUV411P:
1889 *h=2;
1890 *v=0;
1891 break;
1892 default:
1893 *h=0;
1894 *v=0;
1895 break;
1896 }
1897 }
1898
1899 static uint16_t roundToInt16(int64_t f){
1900 int r= (f + (1<<15))>>16;
1901 if (r<-0x7FFF) return 0x8000;
1902 else if (r> 0x7FFF) return 0x7FFF;
1903 else return r;
1904 }
1905
1906 /**
1907 * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
1908 * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
1909 * @return -1 if not supported
1910 */
1911 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
1912 int64_t crv = inv_table[0];
1913 int64_t cbu = inv_table[1];
1914 int64_t cgu = -inv_table[2];
1915 int64_t cgv = -inv_table[3];
1916 int64_t cy = 1<<16;
1917 int64_t oy = 0;
1918
1919 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
1920 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
1921
1922 c->brightness= brightness;
1923 c->contrast = contrast;
1924 c->saturation= saturation;
1925 c->srcRange = srcRange;
1926 c->dstRange = dstRange;
1927 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return 0;
1928
1929 c->uOffset= 0x0400040004000400LL;
1930 c->vOffset= 0x0400040004000400LL;
1931
1932 if (!srcRange){
1933 cy= (cy*255) / 219;
1934 oy= 16<<16;
1935 }else{
1936 crv= (crv*224) / 255;
1937 cbu= (cbu*224) / 255;
1938 cgu= (cgu*224) / 255;
1939 cgv= (cgv*224) / 255;
1940 }
1941
1942 cy = (cy *contrast )>>16;
1943 crv= (crv*contrast * saturation)>>32;
1944 cbu= (cbu*contrast * saturation)>>32;
1945 cgu= (cgu*contrast * saturation)>>32;
1946 cgv= (cgv*contrast * saturation)>>32;
1947
1948 oy -= 256*brightness;
1949
1950 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
1951 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
1952 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
1953 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
1954 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
1955 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
1956
1957 c->cy = (int16_t)roundToInt16(cy <<13);
1958 c->oy = (int16_t)roundToInt16(oy <<9);
1959 c->cvr= (int16_t)roundToInt16(crv<<13);
1960 c->cvg= (int16_t)roundToInt16(cgv<<13);
1961 c->cug= (int16_t)roundToInt16(cgu<<13);
1962 c->cub= (int16_t)roundToInt16(cbu<<13);
1963
1964 yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
1965 //FIXME factorize
1966
1967 #ifdef COMPILE_ALTIVEC
1968 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
1969 yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
1970 #endif
1971 return 0;
1972 }
1973
1974 /**
1975 * @return -1 if not supported
1976 */
1977 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
1978 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1979
1980 *inv_table = c->srcColorspaceTable;
1981 *table = c->dstColorspaceTable;
1982 *srcRange = c->srcRange;
1983 *dstRange = c->dstRange;
1984 *brightness= c->brightness;
1985 *contrast = c->contrast;
1986 *saturation= c->saturation;
1987
1988 return 0;
1989 }
1990
1991 static int handle_jpeg(int *format)
1992 {
1993 switch (*format) {
1994 case PIX_FMT_YUVJ420P:
1995 *format = PIX_FMT_YUV420P;
1996 return 1;
1997 case PIX_FMT_YUVJ422P:
1998 *format = PIX_FMT_YUV422P;
1999 return 1;
2000 case PIX_FMT_YUVJ444P:
2001 *format = PIX_FMT_YUV444P;
2002 return 1;
2003 case PIX_FMT_YUVJ440P:
2004 *format = PIX_FMT_YUV440P;
2005 return 1;
2006 default:
2007 return 0;
2008 }
2009 }
2010
2011 SwsContext *sws_getContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
2012 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
2013
2014 SwsContext *c;
2015 int i;
2016 int usesVFilter, usesHFilter;
2017 int unscaled, needsDither;
2018 int srcRange, dstRange;
2019 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
2020 #if defined(ARCH_X86)
2021 if (flags & SWS_CPU_CAPS_MMX)
2022 asm volatile("emms\n\t"::: "memory");
2023 #endif
2024
2025 #if !defined(RUNTIME_CPUDETECT) || !defined (CONFIG_GPL) //ensure that the flags match the compiled variant if cpudetect is off
2026 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
2027 #ifdef HAVE_MMX2
2028 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
2029 #elif defined (HAVE_3DNOW)
2030 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2031 #elif defined (HAVE_MMX)
2032 flags |= SWS_CPU_CAPS_MMX;
2033 #elif defined (HAVE_ALTIVEC)
2034 flags |= SWS_CPU_CAPS_ALTIVEC;
2035 #elif defined (ARCH_BFIN)
2036 flags |= SWS_CPU_CAPS_BFIN;
2037 #endif
2038 #endif /* RUNTIME_CPUDETECT */
2039 if (clip_table[512] != 255) globalInit();
2040 if (!rgb15to16) sws_rgb2rgb_init(flags);
2041
2042 unscaled = (srcW == dstW && srcH == dstH);
2043 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2044 && (fmt_depth(dstFormat))<24
2045 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2046
2047 srcRange = handle_jpeg(&srcFormat);
2048 dstRange = handle_jpeg(&dstFormat);
2049
2050 if (!isSupportedIn(srcFormat))
2051 {
2052 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
2053 return NULL;
2054 }
2055 if (!isSupportedOut(dstFormat))
2056 {
2057 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
2058 return NULL;
2059 }
2060
2061 i= flags & ( SWS_POINT
2062 |SWS_AREA
2063 |SWS_BILINEAR
2064 |SWS_FAST_BILINEAR
2065 |SWS_BICUBIC
2066 |SWS_X
2067 |SWS_GAUSS
2068 |SWS_LANCZOS
2069 |SWS_SINC
2070 |SWS_SPLINE
2071 |SWS_BICUBLIN);
2072 if(!i || (i & (i-1)))
2073 {
2074 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be choosen\n");
2075 return NULL;
2076 }
2077
2078 /* sanity check */
2079 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
2080 {
2081 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2082 srcW, srcH, dstW, dstH);
2083 return NULL;
2084 }
2085 if(srcW > VOFW || dstW > VOFW){
2086 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile time max width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
2087 return NULL;
2088 }
2089
2090 if (!dstFilter) dstFilter= &dummyFilter;
2091 if (!srcFilter) srcFilter= &dummyFilter;
2092
2093 c= av_mallocz(sizeof(SwsContext));
2094
2095 c->av_class = &sws_context_class;
2096 c->srcW= srcW;
2097 c->srcH= srcH;
2098 c->dstW= dstW;
2099 c->dstH= dstH;
2100 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2101 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2102 c->flags= flags;
2103 c->dstFormat= dstFormat;
2104 c->srcFormat= srcFormat;
2105 c->vRounder= 4* 0x0001000100010001ULL;
2106
2107 usesHFilter= usesVFilter= 0;
2108 if (dstFilter->lumV && dstFilter->lumV->length>1) usesVFilter=1;
2109 if (dstFilter->lumH && dstFilter->lumH->length>1) usesHFilter=1;
2110 if (dstFilter->chrV && dstFilter->chrV->length>1) usesVFilter=1;
2111 if (dstFilter->chrH && dstFilter->chrH->length>1) usesHFilter=1;
2112 if (srcFilter->lumV && srcFilter->lumV->length>1) usesVFilter=1;
2113 if (srcFilter->lumH && srcFilter->lumH->length>1) usesHFilter=1;
2114 if (srcFilter->chrV && srcFilter->chrV->length>1) usesVFilter=1;
2115 if (srcFilter->chrH && srcFilter->chrH->length>1) usesHFilter=1;
2116
2117 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2118 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2119
2120 // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
2121 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2122
2123 // drop some chroma lines if the user wants it
2124 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2125 c->chrSrcVSubSample+= c->vChrDrop;
2126
2127 // drop every 2. pixel for chroma calculation unless user wants full chroma
2128 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2129 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2130 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2131 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
2132 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2133 c->chrSrcHSubSample=1;
2134
2135 if (param){
2136 c->param[0] = param[0];
2137 c->param[1] = param[1];
2138 }else{
2139 c->param[0] =
2140 c->param[1] = SWS_PARAM_DEFAULT;
2141 }
2142
2143 c->chrIntHSubSample= c->chrDstHSubSample;
2144 c->chrIntVSubSample= c->chrSrcVSubSample;
2145
2146 // Note the -((-x)>>y) is so that we always round toward +inf.
2147 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2148 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2149 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2150 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2151
2152 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);
2153
2154 /* unscaled special Cases */
2155 if (unscaled && !usesHFilter && !usesVFilter && (srcRange == dstRange || isBGR(dstFormat) || isRGB(dstFormat)))
2156 {
2157 /* yv12_to_nv12 */
2158 if (srcFormat == PIX_FMT_YUV420P && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2159 {
2160 c->swScale= PlanarToNV12Wrapper;
2161 }
2162 #ifdef CONFIG_GPL
2163 /* yuv2bgr */
2164 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P) && (isBGR(dstFormat) || isRGB(dstFormat))
2165 && !(flags & SWS_ACCURATE_RND))
2166 {
2167 c->swScale= yuv2rgb_get_func_ptr(c);
2168 }
2169 #endif
2170
2171 if (srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P)
2172 {
2173 c->swScale= yvu9toyv12Wrapper;
2174 }
2175
2176 /* bgr24toYV12 */
2177 if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P && !(flags & SWS_ACCURATE_RND))
2178 c->swScale= bgr24toyv12Wrapper;
2179
2180 /* rgb/bgr -> rgb/bgr (no dither needed forms) */
2181 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2182 && (isBGR(dstFormat) || isRGB(dstFormat))
2183 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2184 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2185 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2186 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2187 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2188 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2189 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2190 && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
2191 && dstFormat != PIX_FMT_RGB32_1
2192 && dstFormat != PIX_FMT_BGR32_1
2193 && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
2194 c->swScale= rgb2rgbWrapper;
2195
2196 if (srcFormat == PIX_FMT_YUV422P)
2197 {
2198 if (dstFormat == PIX_FMT_YUYV422)
2199 c->swScale= YUV422PToYuy2Wrapper;
2200 else if (dstFormat == PIX_FMT_UYVY422)
2201 c->swScale= YUV422PToUyvyWrapper;
2202 }
2203
2204 /* LQ converters if -sws 0 or -sws 4*/
2205 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2206 /* yv12_to_yuy2 */
2207 if (srcFormat == PIX_FMT_YUV420P)
2208 {
2209 if (dstFormat == PIX_FMT_YUYV422)
2210 c->swScale= PlanarToYuy2Wrapper;
2211 else if (dstFormat == PIX_FMT_UYVY422)
2212 c->swScale= PlanarToUyvyWrapper;
2213 }
2214 }
2215
2216 #ifdef COMPILE_ALTIVEC
2217 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2218 srcFormat == PIX_FMT_YUV420P) {
2219 // unscaled YV12 -> packed YUV, we want speed
2220 if (dstFormat == PIX_FMT_YUYV422)
2221 c->swScale= yv12toyuy2_unscaled_altivec;
2222 else if (dstFormat == PIX_FMT_UYVY422)
2223 c->swScale= yv12touyvy_unscaled_altivec;
2224 }
2225 #endif
2226
2227 /* simple copy */
2228 if ( srcFormat == dstFormat
2229 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2230 || (isPlanarYUV(dstFormat) && isGray(srcFormat)))
2231 {
2232 if (isPacked(c->srcFormat))
2233 c->swScale= packedCopy;
2234 else /* Planar YUV or gray */
2235 c->swScale= planarCopy;
2236 }
2237
2238 /* gray16{le,be} conversions */
2239 if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2240 {
2241 c->swScale= gray16togray;
2242 }
2243 if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2244 {
2245 c->swScale= graytogray16;
2246 }
2247 if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2248 {
2249 c->swScale= gray16swap;
2250 }
2251
2252 #ifdef ARCH_BFIN
2253 if (flags & SWS_CPU_CAPS_BFIN)
2254 ff_bfin_get_unscaled_swscale (c);
2255 #endif
2256
2257 if (c->swScale){
2258 if (flags&SWS_PRINT_INFO)
2259 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
2260 sws_format_name(srcFormat), sws_format_name(dstFormat));
2261 return c;
2262 }
2263 }
2264
2265 if (flags & SWS_CPU_CAPS_MMX2)
2266 {
2267 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2268 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2269 {
2270 if (flags&SWS_PRINT_INFO)
2271 av_log(c, AV_LOG_INFO, "output Width is not a multiple of 32 -> no MMX2 scaler\n");
2272 }
2273 if (usesHFilter) c->canMMX2BeUsed=0;
2274 }
2275 else
2276 c->canMMX2BeUsed=0;
2277
2278 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2279 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2280
2281 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2282 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2283 // n-2 is the last chrominance sample available
2284 // this is not perfect, but no one should notice the difference, the more correct variant
2285 // would be like the vertical one, but that would require some special code for the
2286 // first and last pixel
2287 if (flags&SWS_FAST_BILINEAR)
2288 {
2289 if (c->canMMX2BeUsed)
2290 {
2291 c->lumXInc+= 20;
2292 c->chrXInc+= 20;
2293 }
2294 //we don't use the x86asm scaler if mmx is available
2295 else if (flags & SWS_CPU_CAPS_MMX)
2296 {
2297 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2298 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2299 }
2300 }
2301
2302 /* precalculate horizontal scaler filter coefficients */
2303 {
2304 const int filterAlign=
2305 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2306 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2307 1;
2308
2309 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2310 srcW , dstW, filterAlign, 1<<14,
2311 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2312 srcFilter->lumH, dstFilter->lumH, c->param);
2313 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2314 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2315 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2316 srcFilter->chrH, dstFilter->chrH, c->param);
2317
2318 #define MAX_FUNNY_CODE_SIZE 10000
2319 #if defined(COMPILE_MMX2)
2320 // can't downscale !!!
2321 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2322 {
2323 #ifdef MAP_ANONYMOUS
2324 c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2325 c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2326 #else
2327 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2328 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2329 #endif
2330
2331 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2332 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2333 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2334 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2335
2336 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2337 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2338 }
2339 #endif /* defined(COMPILE_MMX2) */
2340 } // Init Horizontal stuff
2341
2342
2343
2344 /* precalculate vertical scaler filter coefficients */
2345 {
2346 const int filterAlign=
2347 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2348 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2349 1;
2350
2351 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2352 srcH , dstH, filterAlign, (1<<12),
2353 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2354 srcFilter->lumV, dstFilter->lumV, c->param);
2355 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2356 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
2357 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2358 srcFilter->chrV, dstFilter->chrV, c->param);
2359
2360 #ifdef HAVE_ALTIVEC
2361 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2362 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2363
2364 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2365 int j;
2366 short *p = (short *)&c->vYCoeffsBank[i];
2367 for (j=0;j<8;j++)
2368 p[j] = c->vLumFilter[i];
2369 }
2370
2371 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2372 int j;
2373 short *p = (short *)&c->vCCoeffsBank[i];
2374 for (j=0;j<8;j++)
2375 p[j] = c->vChrFilter[i];
2376 }
2377 #endif
2378 }
2379
2380 // Calculate Buffer Sizes so that they won't run out while handling these damn slices
2381 c->vLumBufSize= c->vLumFilterSize;
2382 c->vChrBufSize= c->vChrFilterSize;
2383 for (i=0; i<dstH; i++)
2384 {
2385 int chrI= i*c->chrDstH / dstH;
2386 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2387 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2388
2389 nextSlice>>= c->chrSrcVSubSample;
2390 nextSlice<<= c->chrSrcVSubSample;
2391 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2392 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
2393 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2394 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2395 }
2396
2397 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2398 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2399 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2400 //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)
2401 /* align at 16 bytes for AltiVec */
2402 for (i=0; i<c->vLumBufSize; i++)
2403 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(VOF+1);
2404 for (i=0; i<c->vChrBufSize; i++)
2405 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc((VOF+1)*2);
2406
2407 //try to avoid drawing green stuff between the right end and the stride end
2408 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
2409
2410 assert(2*VOFW == VOF);
2411
2412 assert(c->chrDstH <= dstH);
2413
2414 if (flags&SWS_PRINT_INFO)
2415 {
2416 #ifdef DITHER1XBPP
2417 const char *dither= " dithered";
2418 #else
2419 const char *dither= "";
2420 #endif
2421 if (flags&SWS_FAST_BILINEAR)
2422 av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
2423 else if (flags&SWS_BILINEAR)
2424 av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
2425 else if (flags&SWS_BICUBIC)
2426 av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
2427 else if (flags&SWS_X)
2428 av_log(c, AV_LOG_INFO, "Experimental scaler, ");
2429 else if (flags&SWS_POINT)
2430 av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
2431 else if (flags&SWS_AREA)
2432 av_log(c, AV_LOG_INFO, "Area Averageing scaler, ");
2433 else if (flags&SWS_BICUBLIN)
2434 av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
2435 else if (flags&SWS_GAUSS)
2436 av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
2437 else if (flags&SWS_SINC)
2438 av_log(c, AV_LOG_INFO, "Sinc scaler, ");
2439 else if (flags&SWS_LANCZOS)
2440 av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
2441 else if (flags&SWS_SPLINE)
2442 av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
2443 else
2444 av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
2445
2446 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2447 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2448 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2449 else
2450 av_log(c, AV_LOG_INFO, "from %s to %s ",
2451 sws_format_name(srcFormat), sws_format_name(dstFormat));
2452
2453 if (flags & SWS_CPU_CAPS_MMX2)
2454 av_log(c, AV_LOG_INFO, "using MMX2\n");
2455 else if (flags & SWS_CPU_CAPS_3DNOW)
2456 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2457 else if (flags & SWS_CPU_CAPS_MMX)
2458 av_log(c, AV_LOG_INFO, "using MMX\n");
2459 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2460 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2461 else
2462 av_log(c, AV_LOG_INFO, "using C\n");
2463 }
2464
2465 if (flags & SWS_PRINT_INFO)
2466 {
2467 if (flags & SWS_CPU_CAPS_MMX)
2468 {
2469 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2470 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2471 else
2472 {
2473 if (c->hLumFilterSize==4)
2474 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2475 else if (c->hLumFilterSize==8)
2476 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2477 else
2478 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
2479
2480 if (c->hChrFilterSize==4)
2481 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2482 else if (c->hChrFilterSize==8)
2483 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2484 else
2485 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2486 }
2487 }
2488 else
2489 {
2490 #if defined(ARCH_X86)
2491 av_log(c, AV_LOG_VERBOSE, "using X86-Asm scaler for horizontal scaling\n");
2492 #else
2493 if (flags & SWS_FAST_BILINEAR)
2494 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
2495 else
2496 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
2497 #endif
2498 }
2499 if (isPlanarYUV(dstFormat))
2500 {
2501 if (c->vLumFilterSize==1)
2502 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2503 else
2504 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2505 }
2506 else
2507 {
2508 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2509 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2510 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2511 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2512 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2513 else
2514 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2515 }
2516
2517 if (dstFormat==PIX_FMT_BGR24)
2518 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 Converter\n",
2519 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2520 else if (dstFormat==PIX_FMT_RGB32)
2521 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2522 else if (dstFormat==PIX_FMT_BGR565)
2523 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2524 else if (dstFormat==PIX_FMT_BGR555)
2525 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2526
2527 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2528 }
2529 if (flags & SWS_PRINT_INFO)
2530 {
2531 av_log(c, AV_LOG_DEBUG, "Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2532 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2533 av_log(c, AV_LOG_DEBUG, "Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2534 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2535 }
2536
2537 c->swScale= getSwsFunc(flags);
2538 return c;
2539 }
2540
2541 /**
2542 * swscale wrapper, so we don't need to export the SwsContext.
2543 * assumes planar YUV to be in YUV order instead of YVU
2544 */
2545 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2546 int srcSliceH, uint8_t* dst[], int dstStride[]){
2547 int i;
2548 uint8_t* src2[4]= {src[0], src[1], src[2]};
2549 uint32_t pal[256];
2550 int use_pal= c->srcFormat == PIX_FMT_PAL8
2551 || c->srcFormat == PIX_FMT_BGR4_BYTE
2552 || c->srcFormat == PIX_FMT_RGB4_BYTE
2553 || c->srcFormat == PIX_FMT_BGR8
2554 || c->srcFormat == PIX_FMT_RGB8;
2555
2556 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2557 av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
2558 return 0;
2559 }
2560 if (c->sliceDir == 0) {
2561 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2562 }
2563
2564 if (use_pal){
2565 for (i=0; i<256; i++){
2566 int p, r, g, b,y,u,v;
2567 if(c->srcFormat == PIX_FMT_PAL8){
2568 p=((uint32_t*)(src[1]))[i];
2569 r= (p>>16)&0xFF;
2570 g= (p>> 8)&0xFF;
2571 b= p &0xFF;
2572 }else if(c->srcFormat == PIX_FMT_RGB8){
2573 r= (i>>5 )*36;
2574 g= ((i>>2)&7)*36;
2575 b= (i&3 )*85;
2576 }else if(c->srcFormat == PIX_FMT_BGR8){
2577 b= (i>>6 )*85;
2578 g= ((i>>3)&7)*36;
2579 r= (i&7 )*36;
2580 }else if(c->srcFormat == PIX_FMT_RGB4_BYTE){
2581 r= (i>>3 )*255;
2582 g= ((i>>1)&3)*85;
2583 b= (i&1 )*255;
2584 }else if(c->srcFormat == PIX_FMT_BGR4_BYTE){
2585 b= (i>>3 )*255;
2586 g= ((i>>1)&3)*85;
2587 r= (i&1 )*255;
2588 }
2589 y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2590 u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2591 v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
2592 pal[i]= y + (u<<8) + (v<<16);
2593 }
2594 src2[1]= (uint8_t*)pal;
2595 }
2596
2597 // copy strides, so they can safely be modified
2598 if (c->sliceDir == 1) {
2599 // slices go from top to bottom
2600 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2]};
2601 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2]};
2602 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2603 } else {
2604 // slices go from bottom to top => we flip the image internally
2605 uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
2606 dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
2607 dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
2608 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2]};
2609 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2]};
2610
2611 src2[0] += (srcSliceH-1)*srcStride[0];
2612 if (!use_pal)
2613 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2614 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2615
2616 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2617 }
2618 }
2619
2620 /**
2621 * swscale wrapper, so we don't need to export the SwsContext
2622 */
2623 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2624 int srcSliceH, uint8_t* dst[], int dstStride[]){
2625 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2626 }
2627
2628 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
2629 float lumaSharpen, float chromaSharpen,
2630 float chromaHShift, float chromaVShift,
2631 int verbose)
2632 {
2633 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2634
2635 if (lumaGBlur!=0.0){
2636 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2637 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2638 }else{
2639 filter->lumH= sws_getIdentityVec();
2640 filter->lumV= sws_getIdentityVec();
2641 }
2642
2643 if (chromaGBlur!=0.0){
2644 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2645 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2646 }else{
2647 filter->chrH= sws_getIdentityVec();
2648 filter->chrV= sws_getIdentityVec();
2649 }
2650
2651 if (chromaSharpen!=0.0){
2652 SwsVector *id= sws_getIdentityVec();
2653 sws_scaleVec(filter->chrH, -chromaSharpen);
2654 sws_scaleVec(filter->chrV, -chromaSharpen);
2655 sws_addVec(filter->chrH, id);
2656 sws_addVec(filter->chrV, id);
2657 sws_freeVec(id);
2658 }
2659
2660 if (lumaSharpen!=0.0){
2661 SwsVector *id= sws_getIdentityVec();
2662 sws_scaleVec(filter->lumH, -lumaSharpen);
2663 sws_scaleVec(filter->lumV, -lumaSharpen);
2664 sws_addVec(filter->lumH, id);
2665 sws_addVec(filter->lumV, id);
2666 sws_freeVec(id);
2667 }
2668
2669 if (chromaHShift != 0.0)
2670 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2671
2672 if (chromaVShift != 0.0)
2673 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2674
2675 sws_normalizeVec(filter->chrH, 1.0);
2676 sws_normalizeVec(filter->chrV, 1.0);
2677 sws_normalizeVec(filter->lumH, 1.0);
2678 sws_normalizeVec(filter->lumV, 1.0);
2679
2680 if (verbose) sws_printVec(filter->chrH);
2681 if (verbose) sws_printVec(filter->lumH);
2682
2683 return filter;
2684 }
2685
2686 /**
2687 * returns a normalized gaussian curve used to filter stuff
2688 * quality=3 is high quality, lowwer is lowwer quality
2689 */
2690 SwsVector *sws_getGaussianVec(double variance, double quality){
2691 const int length= (int)(variance*quality + 0.5) | 1;
2692 int i;
2693 double *coeff= av_malloc(length*sizeof(double));
2694 double middle= (length-1)*0.5;
2695 SwsVector *vec= av_malloc(sizeof(SwsVector));
2696
2697 vec->coeff= coeff;
2698 vec->length= length;
2699
2700 for (i=0; i<length; i++)
2701 {
2702 double dist= i-middle;
2703 coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*PI);
2704 }
2705
2706 sws_normalizeVec(vec, 1.0);
2707
2708 return vec;
2709 }
2710
2711 SwsVector *sws_getConstVec(double c, int length){
2712 int i;
2713 double *coeff= av_malloc(length*sizeof(double));
2714 SwsVector *vec= av_malloc(sizeof(SwsVector));
2715
2716 vec->coeff= coeff;
2717 vec->length= length;
2718
2719 for (i=0; i<length; i++)
2720 coeff[i]= c;
2721
2722 return vec;
2723 }
2724
2725
2726 SwsVector *sws_getIdentityVec(void){
2727 return sws_getConstVec(1.0, 1);
2728 }
2729
2730 double sws_dcVec(SwsVector *a){
2731 int i;
2732 double sum=0;
2733
2734 for (i=0; i<a->length; i++)
2735 sum+= a->coeff[i];
2736
2737 return sum;
2738 }
2739
2740 void sws_scaleVec(SwsVector *a, double scalar){
2741 int i;
2742
2743 for (i=0; i<a->length; i++)
2744 a->coeff[i]*= scalar;
2745 }
2746
2747 void sws_normalizeVec(SwsVector *a, double height){
2748 sws_scaleVec(a, height/sws_dcVec(a));
2749 }
2750
2751 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
2752 int length= a->length + b->length - 1;
2753 double *coeff= av_malloc(length*sizeof(double));
2754 int i, j;
2755 SwsVector *vec= av_malloc(sizeof(SwsVector));
2756
2757 vec->coeff= coeff;
2758 vec->length= length;
2759
2760 for (i=0; i<length; i++) coeff[i]= 0.0;
2761
2762 for (i=0; i<a->length; i++)
2763 {
2764 for (j=0; j<b->length; j++)
2765 {
2766 coeff[i+j]+= a->coeff[i]*b->coeff[j];
2767 }
2768 }
2769
2770 return vec;
2771 }
2772
2773 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
2774 int length= FFMAX(a->length, b->length);
2775 double *coeff= av_malloc(length*sizeof(double));
2776 int i;
2777 SwsVector *vec= av_malloc(sizeof(SwsVector));
2778
2779 vec->coeff= coeff;
2780 vec->length= length;
2781
2782 for (i=0; i<length; i++) coeff[i]= 0.0;
2783
2784 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2785 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
2786
2787 return vec;
2788 }
2789
2790 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
2791 int length= FFMAX(a->length, b->length);
2792 double *coeff= av_malloc(length*sizeof(double));
2793 int i;
2794 SwsVector *vec= av_malloc(sizeof(SwsVector));
2795
2796 vec->coeff= coeff;
2797 vec->length= length;
2798
2799 for (i=0; i<length; i++) coeff[i]= 0.0;
2800
2801 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2802 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
2803
2804 return vec;
2805 }
2806
2807 /* shift left / or right if "shift" is negative */
2808 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
2809 int length= a->length + FFABS(shift)*2;
2810 double *coeff= av_malloc(length*sizeof(double));
2811 int i;
2812 SwsVector *vec= av_malloc(sizeof(SwsVector));
2813
2814 vec->coeff= coeff;
2815 vec->length= length;
2816
2817 for (i=0; i<length; i++) coeff[i]= 0.0;
2818
2819 for (i=0; i<a->length; i++)
2820 {
2821 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2822 }
2823
2824 return vec;
2825 }
2826
2827 void sws_shiftVec(SwsVector *a, int shift){
2828 SwsVector *shifted= sws_getShiftedVec(a, shift);
2829 av_free(a->coeff);
2830 a->coeff= shifted->coeff;
2831 a->length= shifted->length;
2832 av_free(shifted);
2833 }
2834
2835 void sws_addVec(SwsVector *a, SwsVector *b){
2836 SwsVector *sum= sws_sumVec(a, b);
2837 av_free(a->coeff);
2838 a->coeff= sum->coeff;
2839 a->length= sum->length;
2840 av_free(sum);
2841 }
2842
2843 void sws_subVec(SwsVector *a, SwsVector *b){
2844 SwsVector *diff= sws_diffVec(a, b);
2845 av_free(a->coeff);
2846 a->coeff= diff->coeff;
2847 a->length= diff->length;
2848 av_free(diff);
2849 }
2850
2851 void sws_convVec(SwsVector *a, SwsVector *b){
2852 SwsVector *conv= sws_getConvVec(a, b);
2853 av_free(a->coeff);
2854 a->coeff= conv->coeff;
2855 a->length= conv->length;
2856 av_free(conv);
2857 }
2858
2859 SwsVector *sws_cloneVec(SwsVector *a){
2860 double *coeff= av_malloc(a->length*sizeof(double));
2861 int i;
2862 SwsVector *vec= av_malloc(sizeof(SwsVector));
2863
2864 vec->coeff= coeff;
2865 vec->length= a->length;
2866
2867 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
2868
2869 return vec;
2870 }
2871
2872 void sws_printVec(SwsVector *a){
2873 int i;
2874 double max=0;
2875 double min=0;
2876 double range;
2877
2878 for (i=0; i<a->length; i++)
2879 if (a->coeff[i]>max) max= a->coeff[i];
2880
2881 for (i=0; i<a->length; i++)
2882 if (a->coeff[i]<min) min= a->coeff[i];
2883
2884 range= max - min;
2885
2886 for (i=0; i<a->length; i++)
2887 {
2888 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
2889 av_log(NULL, AV_LOG_DEBUG, "%1.3f ", a->coeff[i]);
2890 for (;x>0; x--) av_log(NULL, AV_LOG_DEBUG, " ");
2891 av_log(NULL, AV_LOG_DEBUG, "|\n");
2892 }
2893 }
2894
2895 void sws_freeVec(SwsVector *a){
2896 if (!a) return;
2897 av_freep(&a->coeff);
2898 a->length=0;
2899 av_free(a);
2900 }
2901
2902 void sws_freeFilter(SwsFilter *filter){
2903 if (!filter) return;
2904
2905 if (filter->lumH) sws_freeVec(filter->lumH);
2906 if (filter->lumV) sws_freeVec(filter->lumV);
2907 if (filter->chrH) sws_freeVec(filter->chrH);
2908 if (filter->chrV) sws_freeVec(filter->chrV);
2909 av_free(filter);
2910 }
2911
2912
2913 void sws_freeContext(SwsContext *c){
2914 int i;
2915 if (!c) return;
2916
2917 if (c->lumPixBuf)
2918 {
2919 for (i=0; i<c->vLumBufSize; i++)
2920 av_freep(&c->lumPixBuf[i]);
2921 av_freep(&c->lumPixBuf);
2922 }
2923
2924 if (c->chrPixBuf)
2925 {
2926 for (i=0; i<c->vChrBufSize; i++)
2927 av_freep(&c->chrPixBuf[i]);
2928 av_freep(&c->chrPixBuf);
2929 }
2930
2931 av_freep(&c->vLumFilter);
2932 av_freep(&c->vChrFilter);
2933 av_freep(&c->hLumFilter);
2934 av_freep(&c->hChrFilter);
2935 #ifdef HAVE_ALTIVEC
2936 av_freep(&c->vYCoeffsBank);
2937 av_freep(&c->vCCoeffsBank);
2938 #endif
2939
2940 av_freep(&c->vLumFilterPos);
2941 av_freep(&c->vChrFilterPos);
2942 av_freep(&c->hLumFilterPos);
2943 av_freep(&c->hChrFilterPos);
2944
2945 #if defined(ARCH_X86) && defined(CONFIG_GPL)
2946 #ifdef MAP_ANONYMOUS
2947 if (c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
2948 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
2949 #else
2950 av_free(c->funnyYCode);
2951 av_free(c->funnyUVCode);
2952 #endif
2953 c->funnyYCode=NULL;
2954 c->funnyUVCode=NULL;
2955 #endif /* defined(ARCH_X86) */
2956
2957 av_freep(&c->lumMmx2Filter);
2958 av_freep(&c->chrMmx2Filter);
2959 av_freep(&c->lumMmx2FilterPos);
2960 av_freep(&c->chrMmx2FilterPos);
2961 av_freep(&c->yuvTable);
2962
2963 av_free(c);
2964 }
2965
2966 /**
2967 * Checks if context is valid or reallocs a new one instead.
2968 * If context is NULL, just calls sws_getContext() to get a new one.
2969 * Otherwise, checks if the parameters are the same already saved in context.
2970 * If that is the case, returns the current context.
2971 * Otherwise, frees context and gets a new one.
2972 *
2973 * Be warned that srcFilter, dstFilter are not checked, they are
2974 * asumed to remain valid.
2975 */
2976 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
2977 int srcW, int srcH, int srcFormat,
2978 int dstW, int dstH, int dstFormat, int flags,
2979 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
2980 {
2981 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
2982
2983 if (!param)
2984 param = default_param;
2985
2986 if (context) {
2987 if (context->srcW != srcW || context->srcH != srcH ||
2988 context->srcFormat != srcFormat ||
2989 context->dstW != dstW || context->dstH != dstH ||
2990 context->dstFormat != dstFormat || context->flags != flags ||
2991 context->param[0] != param[0] || context->param[1] != param[1])
2992 {
2993 sws_freeContext(context);
2994 context = NULL;
2995 }
2996 }
2997 if (!context) {
2998 return sws_getContext(srcW, srcH, srcFormat,
2999 dstW, dstH, dstFormat, flags,
3000 srcFilter, dstFilter, param);
3001 }
3002 return context;
3003 }
3004