2 * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
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.
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.
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
20 * the C code (not assembly, mmx, ...) of this file can be used
21 * under the LGPL license too
25 supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09, PAL8
26 supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
27 {BGR,RGB}{1,4,8,15,16} support dithering
29 unscaled special converters (YV12=I420=IYUV, Y800=Y8)
30 YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
35 BGR24 -> BGR32 & RGB24 -> RGB32
36 BGR32 -> BGR24 & RGB32 -> RGB24
41 tested special converters (most are tested actually, but I did not write it down ...)
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
64 #ifdef HAVE_SYS_MMAN_H
66 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
67 #define MAP_ANONYMOUS MAP_ANON
71 #include "swscale_internal.h"
73 #include "libavutil/x86_cpu.h"
74 #include "libavutil/bswap.h"
83 //#define WORDS_BIGENDIAN
86 #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
88 #define RET 0xC3 //near return opcode for X86
93 #define PI 3.14159265358979323846
96 #define isSupportedIn(x) ( \
97 (x)==PIX_FMT_YUV420P \
98 || (x)==PIX_FMT_YUVA420P \
99 || (x)==PIX_FMT_YUYV422 \
100 || (x)==PIX_FMT_UYVY422 \
101 || (x)==PIX_FMT_RGB32 \
102 || (x)==PIX_FMT_BGR24 \
103 || (x)==PIX_FMT_BGR565 \
104 || (x)==PIX_FMT_BGR555 \
105 || (x)==PIX_FMT_BGR32 \
106 || (x)==PIX_FMT_RGB24 \
107 || (x)==PIX_FMT_RGB565 \
108 || (x)==PIX_FMT_RGB555 \
109 || (x)==PIX_FMT_GRAY8 \
110 || (x)==PIX_FMT_YUV410P \
111 || (x)==PIX_FMT_GRAY16BE \
112 || (x)==PIX_FMT_GRAY16LE \
113 || (x)==PIX_FMT_YUV444P \
114 || (x)==PIX_FMT_YUV422P \
115 || (x)==PIX_FMT_YUV411P \
116 || (x)==PIX_FMT_PAL8 \
117 || (x)==PIX_FMT_BGR8 \
118 || (x)==PIX_FMT_RGB8 \
119 || (x)==PIX_FMT_BGR4_BYTE \
120 || (x)==PIX_FMT_RGB4_BYTE \
121 || (x)==PIX_FMT_YUV440P \
123 #define isSupportedOut(x) ( \
124 (x)==PIX_FMT_YUV420P \
125 || (x)==PIX_FMT_YUYV422 \
126 || (x)==PIX_FMT_UYVY422 \
127 || (x)==PIX_FMT_YUV444P \
128 || (x)==PIX_FMT_YUV422P \
129 || (x)==PIX_FMT_YUV411P \
132 || (x)==PIX_FMT_NV12 \
133 || (x)==PIX_FMT_NV21 \
134 || (x)==PIX_FMT_GRAY16BE \
135 || (x)==PIX_FMT_GRAY16LE \
136 || (x)==PIX_FMT_GRAY8 \
137 || (x)==PIX_FMT_YUV410P \
139 #define isPacked(x) ( \
141 || (x)==PIX_FMT_YUYV422 \
142 || (x)==PIX_FMT_UYVY422 \
147 #define RGB2YUV_SHIFT 16
148 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
149 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
150 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
151 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
152 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
153 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
154 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
155 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
156 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
158 extern const int32_t Inverse_Table_6_9
[8][4];
162 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
165 more intelligent misalignment avoidance for the horizontal scaler
166 write special vertical cubic upscale version
167 Optimize C code (yv12 / minmax)
168 add support for packed pixel yuv input & output
169 add support for Y8 output
170 optimize bgr24 & bgr32
171 add BGR4 output support
172 write special BGR->BGR scaler
175 #if defined(ARCH_X86) && defined (CONFIG_GPL)
176 DECLARE_ASM_CONST(8, uint64_t, bF8
)= 0xF8F8F8F8F8F8F8F8LL
;
177 DECLARE_ASM_CONST(8, uint64_t, bFC
)= 0xFCFCFCFCFCFCFCFCLL
;
178 DECLARE_ASM_CONST(8, uint64_t, w10
)= 0x0010001000100010LL
;
179 DECLARE_ASM_CONST(8, uint64_t, w02
)= 0x0002000200020002LL
;
180 DECLARE_ASM_CONST(8, uint64_t, bm00001111
)=0x00000000FFFFFFFFLL
;
181 DECLARE_ASM_CONST(8, uint64_t, bm00000111
)=0x0000000000FFFFFFLL
;
182 DECLARE_ASM_CONST(8, uint64_t, bm11111000
)=0xFFFFFFFFFF000000LL
;
183 DECLARE_ASM_CONST(8, uint64_t, bm01010101
)=0x00FF00FF00FF00FFLL
;
185 static volatile uint64_t attribute_used
__attribute__((aligned(8))) b5Dither
;
186 static volatile uint64_t attribute_used
__attribute__((aligned(8))) g5Dither
;
187 static volatile uint64_t attribute_used
__attribute__((aligned(8))) g6Dither
;
188 static volatile uint64_t attribute_used
__attribute__((aligned(8))) r5Dither
;
190 const DECLARE_ALIGNED(8, uint64_t, ff_dither4
[2]) = {
191 0x0103010301030103LL
,
192 0x0200020002000200LL
,};
194 const DECLARE_ALIGNED(8, uint64_t, ff_dither8
[2]) = {
195 0x0602060206020602LL
,
196 0x0004000400040004LL
,};
198 DECLARE_ASM_CONST(8, uint64_t, b16Mask
)= 0x001F001F001F001FLL
;
199 DECLARE_ASM_CONST(8, uint64_t, g16Mask
)= 0x07E007E007E007E0LL
;
200 DECLARE_ASM_CONST(8, uint64_t, r16Mask
)= 0xF800F800F800F800LL
;
201 DECLARE_ASM_CONST(8, uint64_t, b15Mask
)= 0x001F001F001F001FLL
;
202 DECLARE_ASM_CONST(8, uint64_t, g15Mask
)= 0x03E003E003E003E0LL
;
203 DECLARE_ASM_CONST(8, uint64_t, r15Mask
)= 0x7C007C007C007C00LL
;
205 DECLARE_ALIGNED(8, const uint64_t, ff_M24A
) = 0x00FF0000FF0000FFLL
;
206 DECLARE_ALIGNED(8, const uint64_t, ff_M24B
) = 0xFF0000FF0000FF00LL
;
207 DECLARE_ALIGNED(8, const uint64_t, ff_M24C
) = 0x0000FF0000FF0000LL
;
210 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff
) = 0x000000210041000DULL
;
211 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff
) = 0x0000FFEEFFDC0038ULL
;
212 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff
) = 0x00000038FFD2FFF8ULL
;
214 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff
) = 0x000020E540830C8BULL
;
215 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff
) = 0x0000ED0FDAC23831ULL
;
216 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff
) = 0x00003831D0E6F6EAULL
;
217 #endif /* FAST_BGR2YV12 */
218 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset
) = 0x1010101010101010ULL
;
219 DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset
) = 0x8080808080808080ULL
;
220 DECLARE_ALIGNED(8, const uint64_t, ff_w1111
) = 0x0001000100010001ULL
;
221 #endif /* defined(ARCH_X86) */
223 // clipping helper table for C implementations:
224 static unsigned char clip_table
[768];
226 static SwsVector
*sws_getConvVec(SwsVector
*a
, SwsVector
*b
);
228 extern const uint8_t dither_2x2_4
[2][8];
229 extern const uint8_t dither_2x2_8
[2][8];
230 extern const uint8_t dither_8x8_32
[8][8];
231 extern const uint8_t dither_8x8_73
[8][8];
232 extern const uint8_t dither_8x8_220
[8][8];
234 const char *sws_format_name(enum PixelFormat format
)
237 case PIX_FMT_YUV420P
:
239 case PIX_FMT_YUVA420P
:
241 case PIX_FMT_YUYV422
:
247 case PIX_FMT_YUV422P
:
249 case PIX_FMT_YUV444P
:
253 case PIX_FMT_YUV410P
:
255 case PIX_FMT_YUV411P
:
261 case PIX_FMT_GRAY16BE
:
263 case PIX_FMT_GRAY16LE
:
267 case PIX_FMT_MONOWHITE
:
269 case PIX_FMT_MONOBLACK
:
273 case PIX_FMT_YUVJ420P
:
275 case PIX_FMT_YUVJ422P
:
277 case PIX_FMT_YUVJ444P
:
279 case PIX_FMT_XVMC_MPEG2_MC
:
280 return "xvmc_mpeg2_mc";
281 case PIX_FMT_XVMC_MPEG2_IDCT
:
282 return "xvmc_mpeg2_idct";
283 case PIX_FMT_UYVY422
:
285 case PIX_FMT_UYYVYY411
:
287 case PIX_FMT_RGB32_1
:
289 case PIX_FMT_BGR32_1
:
301 case PIX_FMT_BGR4_BYTE
:
307 case PIX_FMT_RGB4_BYTE
:
313 case PIX_FMT_YUV440P
:
316 return "Unknown format";
320 static inline void yuv2yuvXinC(int16_t *lumFilter
, int16_t **lumSrc
, int lumFilterSize
,
321 int16_t *chrFilter
, int16_t **chrSrc
, int chrFilterSize
,
322 uint8_t *dest
, uint8_t *uDest
, uint8_t *vDest
, int dstW
, int chrDstW
)
324 //FIXME Optimize (just quickly writen not opti..)
326 for (i
=0; i
<dstW
; i
++)
330 for (j
=0; j
<lumFilterSize
; j
++)
331 val
+= lumSrc
[j
][i
] * lumFilter
[j
];
333 dest
[i
]= av_clip_uint8(val
>>19);
337 for (i
=0; i
<chrDstW
; i
++)
342 for (j
=0; j
<chrFilterSize
; j
++)
344 u
+= chrSrc
[j
][i
] * chrFilter
[j
];
345 v
+= chrSrc
[j
][i
+ VOFW
] * chrFilter
[j
];
348 uDest
[i
]= av_clip_uint8(u
>>19);
349 vDest
[i
]= av_clip_uint8(v
>>19);
353 static inline void yuv2nv12XinC(int16_t *lumFilter
, int16_t **lumSrc
, int lumFilterSize
,
354 int16_t *chrFilter
, int16_t **chrSrc
, int chrFilterSize
,
355 uint8_t *dest
, uint8_t *uDest
, int dstW
, int chrDstW
, int dstFormat
)
357 //FIXME Optimize (just quickly writen not opti..)
359 for (i
=0; i
<dstW
; i
++)
363 for (j
=0; j
<lumFilterSize
; j
++)
364 val
+= lumSrc
[j
][i
] * lumFilter
[j
];
366 dest
[i
]= av_clip_uint8(val
>>19);
372 if (dstFormat
== PIX_FMT_NV12
)
373 for (i
=0; i
<chrDstW
; i
++)
378 for (j
=0; j
<chrFilterSize
; j
++)
380 u
+= chrSrc
[j
][i
] * chrFilter
[j
];
381 v
+= chrSrc
[j
][i
+ VOFW
] * chrFilter
[j
];
384 uDest
[2*i
]= av_clip_uint8(u
>>19);
385 uDest
[2*i
+1]= av_clip_uint8(v
>>19);
388 for (i
=0; i
<chrDstW
; i
++)
393 for (j
=0; j
<chrFilterSize
; j
++)
395 u
+= chrSrc
[j
][i
] * chrFilter
[j
];
396 v
+= chrSrc
[j
][i
+ VOFW
] * chrFilter
[j
];
399 uDest
[2*i
]= av_clip_uint8(v
>>19);
400 uDest
[2*i
+1]= av_clip_uint8(u
>>19);
404 #define YSCALE_YUV_2_PACKEDX_C(type) \
405 for (i=0; i<(dstW>>1); i++){\
411 type av_unused *r, *b, *g;\
414 for (j=0; j<lumFilterSize; j++)\
416 Y1 += lumSrc[j][i2] * lumFilter[j];\
417 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
419 for (j=0; j<chrFilterSize; j++)\
421 U += chrSrc[j][i] * chrFilter[j];\
422 V += chrSrc[j][i+VOFW] * chrFilter[j];\
428 if ((Y1|Y2|U|V)&256)\
430 if (Y1>255) Y1=255; \
431 else if (Y1<0)Y1=0; \
432 if (Y2>255) Y2=255; \
433 else if (Y2<0)Y2=0; \
440 #define YSCALE_YUV_2_RGBX_C(type) \
441 YSCALE_YUV_2_PACKEDX_C(type) \
442 r = (type *)c->table_rV[V]; \
443 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
444 b = (type *)c->table_bU[U]; \
446 #define YSCALE_YUV_2_PACKED2_C \
447 for (i=0; i<(dstW>>1); i++){ \
449 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
450 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
451 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
452 int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
454 #define YSCALE_YUV_2_RGB2_C(type) \
455 YSCALE_YUV_2_PACKED2_C\
457 r = (type *)c->table_rV[V];\
458 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
459 b = (type *)c->table_bU[U];\
461 #define YSCALE_YUV_2_PACKED1_C \
462 for (i=0; i<(dstW>>1); i++){\
464 int Y1= buf0[i2 ]>>7;\
465 int Y2= buf0[i2+1]>>7;\
466 int U= (uvbuf1[i ])>>7;\
467 int V= (uvbuf1[i+VOFW])>>7;\
469 #define YSCALE_YUV_2_RGB1_C(type) \
470 YSCALE_YUV_2_PACKED1_C\
472 r = (type *)c->table_rV[V];\
473 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
474 b = (type *)c->table_bU[U];\
476 #define YSCALE_YUV_2_PACKED1B_C \
477 for (i=0; i<(dstW>>1); i++){\
479 int Y1= buf0[i2 ]>>7;\
480 int Y2= buf0[i2+1]>>7;\
481 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
482 int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
484 #define YSCALE_YUV_2_RGB1B_C(type) \
485 YSCALE_YUV_2_PACKED1B_C\
487 r = (type *)c->table_rV[V];\
488 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
489 b = (type *)c->table_bU[U];\
491 #define YSCALE_YUV_2_ANYRGB_C(func, func2)\
492 switch(c->dstFormat)\
497 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
498 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
503 ((uint8_t*)dest)[0]= r[Y1];\
504 ((uint8_t*)dest)[1]= g[Y1];\
505 ((uint8_t*)dest)[2]= b[Y1];\
506 ((uint8_t*)dest)[3]= r[Y2];\
507 ((uint8_t*)dest)[4]= g[Y2];\
508 ((uint8_t*)dest)[5]= b[Y2];\
514 ((uint8_t*)dest)[0]= b[Y1];\
515 ((uint8_t*)dest)[1]= g[Y1];\
516 ((uint8_t*)dest)[2]= r[Y1];\
517 ((uint8_t*)dest)[3]= b[Y2];\
518 ((uint8_t*)dest)[4]= g[Y2];\
519 ((uint8_t*)dest)[5]= r[Y2];\
523 case PIX_FMT_RGB565:\
524 case PIX_FMT_BGR565:\
526 const int dr1= dither_2x2_8[y&1 ][0];\
527 const int dg1= dither_2x2_4[y&1 ][0];\
528 const int db1= dither_2x2_8[(y&1)^1][0];\
529 const int dr2= dither_2x2_8[y&1 ][1];\
530 const int dg2= dither_2x2_4[y&1 ][1];\
531 const int db2= dither_2x2_8[(y&1)^1][1];\
533 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
534 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
538 case PIX_FMT_RGB555:\
539 case PIX_FMT_BGR555:\
541 const int dr1= dither_2x2_8[y&1 ][0];\
542 const int dg1= dither_2x2_8[y&1 ][1];\
543 const int db1= dither_2x2_8[(y&1)^1][0];\
544 const int dr2= dither_2x2_8[y&1 ][1];\
545 const int dg2= dither_2x2_8[y&1 ][0];\
546 const int db2= dither_2x2_8[(y&1)^1][1];\
548 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
549 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
556 const uint8_t * const d64= dither_8x8_73[y&7];\
557 const uint8_t * const d32= dither_8x8_32[y&7];\
559 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
560 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
567 const uint8_t * const d64= dither_8x8_73 [y&7];\
568 const uint8_t * const d128=dither_8x8_220[y&7];\
570 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
571 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
575 case PIX_FMT_RGB4_BYTE:\
576 case PIX_FMT_BGR4_BYTE:\
578 const uint8_t * const d64= dither_8x8_73 [y&7];\
579 const uint8_t * const d128=dither_8x8_220[y&7];\
581 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
582 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
586 case PIX_FMT_MONOBLACK:\
588 const uint8_t * const d128=dither_8x8_220[y&7];\
589 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
590 for (i=0; i<dstW-7; i+=8){\
592 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
593 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
594 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
595 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
596 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
597 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
598 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
599 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
600 ((uint8_t*)dest)[0]= acc;\
605 ((uint8_t*)dest)-= dstW>>4;\
609 static int top[1024];\
610 static int last_new[1024][1024];\
611 static int last_in3[1024][1024];\
612 static int drift[1024][1024];\
616 const uint8_t * const d128=dither_8x8_220[y&7];\
621 for (i=dstW>>1; i<dstW; i++){\
622 int in= ((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19);\
623 int in2 = (76309 * (in - 16) + 32768) >> 16;\
624 int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
625 int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
626 + (last_new[y][i] - in3)*f/256;\
627 int new= old> 128 ? 255 : 0;\
629 error_new+= FFABS(last_new[y][i] - new);\
630 error_in3+= FFABS(last_in3[y][i] - in3);\
631 f= error_new - error_in3*4;\
636 left= top[i]= old - new;\
637 last_new[y][i]= new;\
638 last_in3[y][i]= in3;\
640 acc+= acc + (new&1);\
642 ((uint8_t*)dest)[0]= acc;\
650 case PIX_FMT_YUYV422:\
652 ((uint8_t*)dest)[2*i2+0]= Y1;\
653 ((uint8_t*)dest)[2*i2+1]= U;\
654 ((uint8_t*)dest)[2*i2+2]= Y2;\
655 ((uint8_t*)dest)[2*i2+3]= V;\
658 case PIX_FMT_UYVY422:\
660 ((uint8_t*)dest)[2*i2+0]= U;\
661 ((uint8_t*)dest)[2*i2+1]= Y1;\
662 ((uint8_t*)dest)[2*i2+2]= V;\
663 ((uint8_t*)dest)[2*i2+3]= Y2;\
669 static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
670 int16_t *chrFilter
, int16_t **chrSrc
, int chrFilterSize
,
671 uint8_t *dest
, int dstW
, int y
)
678 YSCALE_YUV_2_RGBX_C(uint32_t)
679 ((uint32_t*)dest
)[i2
+0]= r
[Y1
] + g
[Y1
] + b
[Y1
];
680 ((uint32_t*)dest
)[i2
+1]= r
[Y2
] + g
[Y2
] + b
[Y2
];
684 YSCALE_YUV_2_RGBX_C(uint8_t)
685 ((uint8_t*)dest
)[0]= r
[Y1
];
686 ((uint8_t*)dest
)[1]= g
[Y1
];
687 ((uint8_t*)dest
)[2]= b
[Y1
];
688 ((uint8_t*)dest
)[3]= r
[Y2
];
689 ((uint8_t*)dest
)[4]= g
[Y2
];
690 ((uint8_t*)dest
)[5]= b
[Y2
];
695 YSCALE_YUV_2_RGBX_C(uint8_t)
696 ((uint8_t*)dest
)[0]= b
[Y1
];
697 ((uint8_t*)dest
)[1]= g
[Y1
];
698 ((uint8_t*)dest
)[2]= r
[Y1
];
699 ((uint8_t*)dest
)[3]= b
[Y2
];
700 ((uint8_t*)dest
)[4]= g
[Y2
];
701 ((uint8_t*)dest
)[5]= r
[Y2
];
708 const int dr1
= dither_2x2_8
[y
&1 ][0];
709 const int dg1
= dither_2x2_4
[y
&1 ][0];
710 const int db1
= dither_2x2_8
[(y
&1)^1][0];
711 const int dr2
= dither_2x2_8
[y
&1 ][1];
712 const int dg2
= dither_2x2_4
[y
&1 ][1];
713 const int db2
= dither_2x2_8
[(y
&1)^1][1];
714 YSCALE_YUV_2_RGBX_C(uint16_t)
715 ((uint16_t*)dest
)[i2
+0]= r
[Y1
+dr1
] + g
[Y1
+dg1
] + b
[Y1
+db1
];
716 ((uint16_t*)dest
)[i2
+1]= r
[Y2
+dr2
] + g
[Y2
+dg2
] + b
[Y2
+db2
];
723 const int dr1
= dither_2x2_8
[y
&1 ][0];
724 const int dg1
= dither_2x2_8
[y
&1 ][1];
725 const int db1
= dither_2x2_8
[(y
&1)^1][0];
726 const int dr2
= dither_2x2_8
[y
&1 ][1];
727 const int dg2
= dither_2x2_8
[y
&1 ][0];
728 const int db2
= dither_2x2_8
[(y
&1)^1][1];
729 YSCALE_YUV_2_RGBX_C(uint16_t)
730 ((uint16_t*)dest
)[i2
+0]= r
[Y1
+dr1
] + g
[Y1
+dg1
] + b
[Y1
+db1
];
731 ((uint16_t*)dest
)[i2
+1]= r
[Y2
+dr2
] + g
[Y2
+dg2
] + b
[Y2
+db2
];
738 const uint8_t * const d64
= dither_8x8_73
[y
&7];
739 const uint8_t * const d32
= dither_8x8_32
[y
&7];
740 YSCALE_YUV_2_RGBX_C(uint8_t)
741 ((uint8_t*)dest
)[i2
+0]= r
[Y1
+d32
[(i2
+0)&7]] + g
[Y1
+d32
[(i2
+0)&7]] + b
[Y1
+d64
[(i2
+0)&7]];
742 ((uint8_t*)dest
)[i2
+1]= r
[Y2
+d32
[(i2
+1)&7]] + g
[Y2
+d32
[(i2
+1)&7]] + b
[Y2
+d64
[(i2
+1)&7]];
749 const uint8_t * const d64
= dither_8x8_73
[y
&7];
750 const uint8_t * const d128
=dither_8x8_220
[y
&7];
751 YSCALE_YUV_2_RGBX_C(uint8_t)
752 ((uint8_t*)dest
)[i
]= r
[Y1
+d128
[(i2
+0)&7]] + g
[Y1
+d64
[(i2
+0)&7]] + b
[Y1
+d128
[(i2
+0)&7]]
753 +((r
[Y2
+d128
[(i2
+1)&7]] + g
[Y2
+d64
[(i2
+1)&7]] + b
[Y2
+d128
[(i2
+1)&7]])<<4);
757 case PIX_FMT_RGB4_BYTE
:
758 case PIX_FMT_BGR4_BYTE
:
760 const uint8_t * const d64
= dither_8x8_73
[y
&7];
761 const uint8_t * const d128
=dither_8x8_220
[y
&7];
762 YSCALE_YUV_2_RGBX_C(uint8_t)
763 ((uint8_t*)dest
)[i2
+0]= r
[Y1
+d128
[(i2
+0)&7]] + g
[Y1
+d64
[(i2
+0)&7]] + b
[Y1
+d128
[(i2
+0)&7]];
764 ((uint8_t*)dest
)[i2
+1]= r
[Y2
+d128
[(i2
+1)&7]] + g
[Y2
+d64
[(i2
+1)&7]] + b
[Y2
+d128
[(i2
+1)&7]];
768 case PIX_FMT_MONOBLACK
:
770 const uint8_t * const d128
=dither_8x8_220
[y
&7];
771 uint8_t *g
= c
->table_gU
[128] + c
->table_gV
[128];
773 for (i
=0; i
<dstW
-1; i
+=2){
778 for (j
=0; j
<lumFilterSize
; j
++)
780 Y1
+= lumSrc
[j
][i
] * lumFilter
[j
];
781 Y2
+= lumSrc
[j
][i
+1] * lumFilter
[j
];
792 acc
+= acc
+ g
[Y1
+d128
[(i
+0)&7]];
793 acc
+= acc
+ g
[Y2
+d128
[(i
+1)&7]];
795 ((uint8_t*)dest
)[0]= acc
;
801 case PIX_FMT_YUYV422
:
802 YSCALE_YUV_2_PACKEDX_C(void)
803 ((uint8_t*)dest
)[2*i2
+0]= Y1
;
804 ((uint8_t*)dest
)[2*i2
+1]= U
;
805 ((uint8_t*)dest
)[2*i2
+2]= Y2
;
806 ((uint8_t*)dest
)[2*i2
+3]= V
;
809 case PIX_FMT_UYVY422
:
810 YSCALE_YUV_2_PACKEDX_C(void)
811 ((uint8_t*)dest
)[2*i2
+0]= U
;
812 ((uint8_t*)dest
)[2*i2
+1]= Y1
;
813 ((uint8_t*)dest
)[2*i2
+2]= V
;
814 ((uint8_t*)dest
)[2*i2
+3]= Y2
;
821 //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
823 #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT) || !defined(CONFIG_GPL)
828 #if (defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
829 #define COMPILE_ALTIVEC
830 #endif //HAVE_ALTIVEC
831 #endif //ARCH_POWERPC
833 #if defined(ARCH_X86)
835 #if ((defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
839 #if (defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
843 #if ((defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
844 #define COMPILE_3DNOW
846 #endif //ARCH_X86 || ARCH_X86_64
857 #define RENAME(a) a ## _C
858 #include "swscale_template.c"
861 #ifdef COMPILE_ALTIVEC
864 #define RENAME(a) a ## _altivec
865 #include "swscale_template.c"
868 #if defined(ARCH_X86)
877 #define RENAME(a) a ## _X86
878 #include "swscale_template.c"
886 #define RENAME(a) a ## _MMX
887 #include "swscale_template.c"
896 #define RENAME(a) a ## _MMX2
897 #include "swscale_template.c"
906 #define RENAME(a) a ## _3DNow
907 #include "swscale_template.c"
910 #endif //ARCH_X86 || ARCH_X86_64
912 // minor note: the HAVE_xyz is messed up after that line so don't use it
914 static double getSplineCoeff(double a
, double b
, double c
, double d
, double dist
)
916 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
917 if (dist
<=1.0) return ((d
*dist
+ c
)*dist
+ b
)*dist
+a
;
918 else return getSplineCoeff( 0.0,
925 static inline int initFilter(int16_t **outFilter
, int16_t **filterPos
, int *outFilterSize
, int xInc
,
926 int srcW
, int dstW
, int filterAlign
, int one
, int flags
,
927 SwsVector
*srcFilter
, SwsVector
*dstFilter
, double param
[2])
934 double *filter2
=NULL
;
936 #if defined(ARCH_X86)
937 if (flags
& SWS_CPU_CAPS_MMX
)
938 asm volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
941 // Note the +1 is for the MMXscaler which reads over the end
942 *filterPos
= av_malloc((dstW
+1)*sizeof(int16_t));
944 if (FFABS(xInc
- 0x10000) <10) // unscaled
948 filter
= av_malloc(dstW
*sizeof(double)*filterSize
);
949 for (i
=0; i
<dstW
*filterSize
; i
++) filter
[i
]=0;
951 for (i
=0; i
<dstW
; i
++)
953 filter
[i
*filterSize
]=1;
958 else if (flags
&SWS_POINT
) // lame looking point sampling mode
963 filter
= av_malloc(dstW
*sizeof(double)*filterSize
);
965 xDstInSrc
= xInc
/2 - 0x8000;
966 for (i
=0; i
<dstW
; i
++)
968 int xx
= (xDstInSrc
- ((filterSize
-1)<<15) + (1<<15))>>16;
975 else if ((xInc
<= (1<<16) && (flags
&SWS_AREA
)) || (flags
&SWS_FAST_BILINEAR
)) // bilinear upscale
979 if (flags
&SWS_BICUBIC
) filterSize
= 4;
980 else if (flags
&SWS_X
) filterSize
= 4;
981 else filterSize
= 2; // SWS_BILINEAR / SWS_AREA
982 filter
= av_malloc(dstW
*sizeof(double)*filterSize
);
984 xDstInSrc
= xInc
/2 - 0x8000;
985 for (i
=0; i
<dstW
; i
++)
987 int xx
= (xDstInSrc
- ((filterSize
-1)<<15) + (1<<15))>>16;
991 //Bilinear upscale / linear interpolate / Area averaging
992 for (j
=0; j
<filterSize
; j
++)
994 double d
= FFABS((xx
<<16) - xDstInSrc
)/(double)(1<<16);
995 double coeff
= 1.0 - d
;
996 if (coeff
<0) coeff
=0;
997 filter
[i
*filterSize
+ j
]= coeff
;
1006 double sizeFactor
, filterSizeInSrc
;
1007 const double xInc1
= (double)xInc
/ (double)(1<<16);
1009 if (flags
&SWS_BICUBIC
) sizeFactor
= 4.0;
1010 else if (flags
&SWS_X
) sizeFactor
= 8.0;
1011 else if (flags
&SWS_AREA
) sizeFactor
= 1.0; //downscale only, for upscale it is bilinear
1012 else if (flags
&SWS_GAUSS
) sizeFactor
= 8.0; // infinite ;)
1013 else if (flags
&SWS_LANCZOS
) sizeFactor
= param
[0] != SWS_PARAM_DEFAULT
? 2.0*param
[0] : 6.0;
1014 else if (flags
&SWS_SINC
) sizeFactor
= 20.0; // infinite ;)
1015 else if (flags
&SWS_SPLINE
) sizeFactor
= 20.0; // infinite ;)
1016 else if (flags
&SWS_BILINEAR
) sizeFactor
= 2.0;
1018 sizeFactor
= 0.0; //GCC warning killer
1022 if (xInc1
<= 1.0) filterSizeInSrc
= sizeFactor
; // upscale
1023 else filterSizeInSrc
= sizeFactor
*srcW
/ (double)dstW
;
1025 filterSize
= (int)ceil(1 + filterSizeInSrc
); // will be reduced later if possible
1026 if (filterSize
> srcW
-2) filterSize
=srcW
-2;
1028 filter
= av_malloc(dstW
*sizeof(double)*filterSize
);
1030 xDstInSrc
= xInc1
/ 2.0 - 0.5;
1031 for (i
=0; i
<dstW
; i
++)
1033 int xx
= (int)(xDstInSrc
- (filterSize
-1)*0.5 + 0.5);
1035 (*filterPos
)[i
]= xx
;
1036 for (j
=0; j
<filterSize
; j
++)
1038 double d
= FFABS(xx
- xDstInSrc
)/filterSizeInSrc
*sizeFactor
;
1040 if (flags
& SWS_BICUBIC
)
1042 double B
= param
[0] != SWS_PARAM_DEFAULT
? param
[0] : 0.0;
1043 double C
= param
[1] != SWS_PARAM_DEFAULT
? param
[1] : 0.6;
1046 coeff
= (12-9*B
-6*C
)*d
*d
*d
+ (-18+12*B
+6*C
)*d
*d
+ 6-2*B
;
1048 coeff
= (-B
-6*C
)*d
*d
*d
+ (6*B
+30*C
)*d
*d
+ (-12*B
-48*C
)*d
+8*B
+24*C
;
1052 /* else if (flags & SWS_X)
1054 double p= param ? param*0.01 : 0.3;
1055 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1056 coeff*= pow(2.0, - p*d*d);
1058 else if (flags
& SWS_X
)
1060 double A
= param
[0] != SWS_PARAM_DEFAULT
? param
[0] : 1.0;
1066 if (coeff
<0.0) coeff
= -pow(-coeff
, A
);
1067 else coeff
= pow( coeff
, A
);
1068 coeff
= coeff
*0.5 + 0.5;
1070 else if (flags
& SWS_AREA
)
1072 double srcPixelSize
= 1.0/xInc1
;
1073 if (d
+ srcPixelSize
/2 < 0.5) coeff
= 1.0;
1074 else if (d
- srcPixelSize
/2 < 0.5) coeff
= (0.5-d
)/srcPixelSize
+ 0.5;
1077 else if (flags
& SWS_GAUSS
)
1079 double p
= param
[0] != SWS_PARAM_DEFAULT
? param
[0] : 3.0;
1080 coeff
= pow(2.0, - p
*d
*d
);
1082 else if (flags
& SWS_SINC
)
1084 coeff
= d
? sin(d
*PI
)/(d
*PI
) : 1.0;
1086 else if (flags
& SWS_LANCZOS
)
1088 double p
= param
[0] != SWS_PARAM_DEFAULT
? param
[0] : 3.0;
1089 coeff
= d
? sin(d
*PI
)*sin(d
*PI
/p
)/(d
*d
*PI
*PI
/p
) : 1.0;
1092 else if (flags
& SWS_BILINEAR
)
1095 if (coeff
<0) coeff
=0;
1097 else if (flags
& SWS_SPLINE
)
1099 double p
=-2.196152422706632;
1100 coeff
= getSplineCoeff(1.0, 0.0, p
, -p
-1.0, d
);
1103 coeff
= 0.0; //GCC warning killer
1107 filter
[i
*filterSize
+ j
]= coeff
;
1114 /* apply src & dst Filter to filter -> filter2
1117 assert(filterSize
>0);
1118 filter2Size
= filterSize
;
1119 if (srcFilter
) filter2Size
+= srcFilter
->length
- 1;
1120 if (dstFilter
) filter2Size
+= dstFilter
->length
- 1;
1121 assert(filter2Size
>0);
1122 filter2
= av_malloc(filter2Size
*dstW
*sizeof(double));
1124 for (i
=0; i
<dstW
; i
++)
1127 SwsVector scaleFilter
;
1130 scaleFilter
.coeff
= filter
+ i
*filterSize
;
1131 scaleFilter
.length
= filterSize
;
1133 if (srcFilter
) outVec
= sws_getConvVec(srcFilter
, &scaleFilter
);
1134 else outVec
= &scaleFilter
;
1136 assert(outVec
->length
== filter2Size
);
1139 for (j
=0; j
<outVec
->length
; j
++)
1141 filter2
[i
*filter2Size
+ j
]= outVec
->coeff
[j
];
1144 (*filterPos
)[i
]+= (filterSize
-1)/2 - (filter2Size
-1)/2;
1146 if (outVec
!= &scaleFilter
) sws_freeVec(outVec
);
1150 /* try to reduce the filter-size (step1 find size and shift left) */
1151 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
1153 for (i
=dstW
-1; i
>=0; i
--)
1155 int min
= filter2Size
;
1159 /* get rid off near zero elements on the left by shifting left */
1160 for (j
=0; j
<filter2Size
; j
++)
1163 cutOff
+= FFABS(filter2
[i
*filter2Size
]);
1165 if (cutOff
> SWS_MAX_REDUCE_CUTOFF
) break;
1167 /* preserve monotonicity because the core can't handle the filter otherwise */
1168 if (i
<dstW
-1 && (*filterPos
)[i
] >= (*filterPos
)[i
+1]) break;
1170 // Move filter coeffs left
1171 for (k
=1; k
<filter2Size
; k
++)
1172 filter2
[i
*filter2Size
+ k
- 1]= filter2
[i
*filter2Size
+ k
];
1173 filter2
[i
*filter2Size
+ k
- 1]= 0.0;
1178 /* count near zeros on the right */
1179 for (j
=filter2Size
-1; j
>0; j
--)
1181 cutOff
+= FFABS(filter2
[i
*filter2Size
+ j
]);
1183 if (cutOff
> SWS_MAX_REDUCE_CUTOFF
) break;
1187 if (min
>minFilterSize
) minFilterSize
= min
;
1190 if (flags
& SWS_CPU_CAPS_ALTIVEC
) {
1191 // we can handle the special case 4,
1192 // so we don't want to go to the full 8
1193 if (minFilterSize
< 5)
1196 // we really don't want to waste our time
1197 // doing useless computation, so fall-back on
1198 // the scalar C code for very small filter.
1199 // vectorizing is worth it only if you have
1200 // decent-sized vector.
1201 if (minFilterSize
< 3)
1205 if (flags
& SWS_CPU_CAPS_MMX
) {
1206 // special case for unscaled vertical filtering
1207 if (minFilterSize
== 1 && filterAlign
== 2)
1211 assert(minFilterSize
> 0);
1212 filterSize
= (minFilterSize
+(filterAlign
-1)) & (~(filterAlign
-1));
1213 assert(filterSize
> 0);
1214 filter
= av_malloc(filterSize
*dstW
*sizeof(double));
1215 if (filterSize
>= MAX_FILTER_SIZE
|| !filter
)
1217 *outFilterSize
= filterSize
;
1219 if (flags
&SWS_PRINT_INFO
)
1220 av_log(NULL
, AV_LOG_VERBOSE
, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size
, filterSize
);
1221 /* try to reduce the filter-size (step2 reduce it) */
1222 for (i
=0; i
<dstW
; i
++)
1226 for (j
=0; j
<filterSize
; j
++)
1228 if (j
>=filter2Size
) filter
[i
*filterSize
+ j
]= 0.0;
1229 else filter
[i
*filterSize
+ j
]= filter2
[i
*filter2Size
+ j
];
1234 //FIXME try to align filterpos if possible
1237 for (i
=0; i
<dstW
; i
++)
1240 if ((*filterPos
)[i
] < 0)
1242 // Move filter coeffs left to compensate for filterPos
1243 for (j
=1; j
<filterSize
; j
++)
1245 int left
= FFMAX(j
+ (*filterPos
)[i
], 0);
1246 filter
[i
*filterSize
+ left
] += filter
[i
*filterSize
+ j
];
1247 filter
[i
*filterSize
+ j
]=0;
1252 if ((*filterPos
)[i
] + filterSize
> srcW
)
1254 int shift
= (*filterPos
)[i
] + filterSize
- srcW
;
1255 // Move filter coeffs right to compensate for filterPos
1256 for (j
=filterSize
-2; j
>=0; j
--)
1258 int right
= FFMIN(j
+ shift
, filterSize
-1);
1259 filter
[i
*filterSize
+right
] += filter
[i
*filterSize
+j
];
1260 filter
[i
*filterSize
+j
]=0;
1262 (*filterPos
)[i
]= srcW
- filterSize
;
1266 // Note the +1 is for the MMXscaler which reads over the end
1267 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1268 *outFilter
= av_mallocz(*outFilterSize
*(dstW
+1)*sizeof(int16_t));
1270 /* Normalize & Store in outFilter */
1271 for (i
=0; i
<dstW
; i
++)
1278 for (j
=0; j
<filterSize
; j
++)
1280 sum
+= filter
[i
*filterSize
+ j
];
1283 for (j
=0; j
<*outFilterSize
; j
++)
1285 double v
= filter
[i
*filterSize
+ j
]*scale
+ error
;
1286 int intV
= floor(v
+ 0.5);
1287 (*outFilter
)[i
*(*outFilterSize
) + j
]= intV
;
1292 (*filterPos
)[dstW
]= (*filterPos
)[dstW
-1]; // the MMX scaler will read over the end
1293 for (i
=0; i
<*outFilterSize
; i
++)
1295 int j
= dstW
*(*outFilterSize
);
1296 (*outFilter
)[j
+ i
]= (*outFilter
)[j
+ i
- (*outFilterSize
)];
1307 static void initMMX2HScaler(int dstW
, int xInc
, uint8_t *funnyCode
, int16_t *filter
, int32_t *filterPos
, int numSplits
)
1310 long imm8OfPShufW1A
;
1311 long imm8OfPShufW2A
;
1312 long fragmentLengthA
;
1314 long imm8OfPShufW1B
;
1315 long imm8OfPShufW2B
;
1316 long fragmentLengthB
;
1321 // create an optimized horizontal scaling routine
1329 "movq (%%"REG_d
", %%"REG_a
"), %%mm3 \n\t"
1330 "movd (%%"REG_c
", %%"REG_S
"), %%mm0 \n\t"
1331 "movd 1(%%"REG_c
", %%"REG_S
"), %%mm1 \n\t"
1332 "punpcklbw %%mm7, %%mm1 \n\t"
1333 "punpcklbw %%mm7, %%mm0 \n\t"
1334 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1336 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1338 "psubw %%mm1, %%mm0 \n\t"
1339 "movl 8(%%"REG_b
", %%"REG_a
"), %%esi \n\t"
1340 "pmullw %%mm3, %%mm0 \n\t"
1341 "psllw $7, %%mm1 \n\t"
1342 "paddw %%mm1, %%mm0 \n\t"
1344 "movq %%mm0, (%%"REG_D
", %%"REG_a
") \n\t"
1346 "add $8, %%"REG_a
" \n\t"
1350 "lea " LOCAL_MANGLE(0b
) ", %0 \n\t"
1351 "lea " LOCAL_MANGLE(1b
) ", %1 \n\t"
1352 "lea " LOCAL_MANGLE(2b
) ", %2 \n\t"
1357 "lea " LOCAL_MANGLE(9b
) ", %3 \n\t"
1361 :"=r" (fragmentA
), "=r" (imm8OfPShufW1A
), "=r" (imm8OfPShufW2A
),
1362 "=r" (fragmentLengthA
)
1369 "movq (%%"REG_d
", %%"REG_a
"), %%mm3 \n\t"
1370 "movd (%%"REG_c
", %%"REG_S
"), %%mm0 \n\t"
1371 "punpcklbw %%mm7, %%mm0 \n\t"
1372 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1374 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1376 "psubw %%mm1, %%mm0 \n\t"
1377 "movl 8(%%"REG_b
", %%"REG_a
"), %%esi \n\t"
1378 "pmullw %%mm3, %%mm0 \n\t"
1379 "psllw $7, %%mm1 \n\t"
1380 "paddw %%mm1, %%mm0 \n\t"
1382 "movq %%mm0, (%%"REG_D
", %%"REG_a
") \n\t"
1384 "add $8, %%"REG_a
" \n\t"
1388 "lea " LOCAL_MANGLE(0b
) ", %0 \n\t"
1389 "lea " LOCAL_MANGLE(1b
) ", %1 \n\t"
1390 "lea " LOCAL_MANGLE(2b
) ", %2 \n\t"
1395 "lea " LOCAL_MANGLE(9b
) ", %3 \n\t"
1399 :"=r" (fragmentB
), "=r" (imm8OfPShufW1B
), "=r" (imm8OfPShufW2B
),
1400 "=r" (fragmentLengthB
)
1403 xpos
= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1406 for (i
=0; i
<dstW
/numSplits
; i
++)
1413 int b
=((xpos
+xInc
)>>16) - xx
;
1414 int c
=((xpos
+xInc
*2)>>16) - xx
;
1415 int d
=((xpos
+xInc
*3)>>16) - xx
;
1417 filter
[i
] = (( xpos
& 0xFFFF) ^ 0xFFFF)>>9;
1418 filter
[i
+1] = (((xpos
+xInc
) & 0xFFFF) ^ 0xFFFF)>>9;
1419 filter
[i
+2] = (((xpos
+xInc
*2) & 0xFFFF) ^ 0xFFFF)>>9;
1420 filter
[i
+3] = (((xpos
+xInc
*3) & 0xFFFF) ^ 0xFFFF)>>9;
1425 int maxShift
= 3-(d
+1);
1428 memcpy(funnyCode
+ fragmentPos
, fragmentB
, fragmentLengthB
);
1430 funnyCode
[fragmentPos
+ imm8OfPShufW1B
]=
1431 (a
+1) | ((b
+1)<<2) | ((c
+1)<<4) | ((d
+1)<<6);
1432 funnyCode
[fragmentPos
+ imm8OfPShufW2B
]=
1433 a
| (b
<<2) | (c
<<4) | (d
<<6);
1435 if (i
+3>=dstW
) shift
=maxShift
; //avoid overread
1436 else if ((filterPos
[i
/2]&3) <= maxShift
) shift
=filterPos
[i
/2]&3; //Align
1438 if (shift
&& i
>=shift
)
1440 funnyCode
[fragmentPos
+ imm8OfPShufW1B
]+= 0x55*shift
;
1441 funnyCode
[fragmentPos
+ imm8OfPShufW2B
]+= 0x55*shift
;
1442 filterPos
[i
/2]-=shift
;
1445 fragmentPos
+= fragmentLengthB
;
1452 memcpy(funnyCode
+ fragmentPos
, fragmentA
, fragmentLengthA
);
1454 funnyCode
[fragmentPos
+ imm8OfPShufW1A
]=
1455 funnyCode
[fragmentPos
+ imm8OfPShufW2A
]=
1456 a
| (b
<<2) | (c
<<4) | (d
<<6);
1458 if (i
+4>=dstW
) shift
=maxShift
; //avoid overread
1459 else if ((filterPos
[i
/2]&3) <= maxShift
) shift
=filterPos
[i
/2]&3; //partial align
1461 if (shift
&& i
>=shift
)
1463 funnyCode
[fragmentPos
+ imm8OfPShufW1A
]+= 0x55*shift
;
1464 funnyCode
[fragmentPos
+ imm8OfPShufW2A
]+= 0x55*shift
;
1465 filterPos
[i
/2]-=shift
;
1468 fragmentPos
+= fragmentLengthA
;
1471 funnyCode
[fragmentPos
]= RET
;
1475 filterPos
[i
/2]= xpos
>>16; // needed to jump to the next part
1477 #endif /* COMPILE_MMX2 */
1479 static void globalInit(void){
1480 // generating tables:
1482 for (i
=0; i
<768; i
++){
1483 int c
= av_clip_uint8(i
-256);
1488 static SwsFunc
getSwsFunc(int flags
){
1490 #if defined(RUNTIME_CPUDETECT) && defined (CONFIG_GPL)
1491 #if defined(ARCH_X86)
1492 // ordered per speed fastest first
1493 if (flags
& SWS_CPU_CAPS_MMX2
)
1494 return swScale_MMX2
;
1495 else if (flags
& SWS_CPU_CAPS_3DNOW
)
1496 return swScale_3DNow
;
1497 else if (flags
& SWS_CPU_CAPS_MMX
)
1504 if (flags
& SWS_CPU_CAPS_ALTIVEC
)
1505 return swScale_altivec
;
1510 #endif /* defined(ARCH_X86) */
1511 #else //RUNTIME_CPUDETECT
1513 return swScale_MMX2
;
1514 #elif defined (HAVE_3DNOW)
1515 return swScale_3DNow
;
1516 #elif defined (HAVE_MMX)
1518 #elif defined (HAVE_ALTIVEC)
1519 return swScale_altivec
;
1523 #endif //!RUNTIME_CPUDETECT
1526 static int PlanarToNV12Wrapper(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1527 int srcSliceH
, uint8_t* dstParam
[], int dstStride
[]){
1528 uint8_t *dst
=dstParam
[0] + dstStride
[0]*srcSliceY
;
1530 if (dstStride
[0]==srcStride
[0] && srcStride
[0] > 0)
1531 memcpy(dst
, src
[0], srcSliceH
*dstStride
[0]);
1535 uint8_t *srcPtr
= src
[0];
1536 uint8_t *dstPtr
= dst
;
1537 for (i
=0; i
<srcSliceH
; i
++)
1539 memcpy(dstPtr
, srcPtr
, c
->srcW
);
1540 srcPtr
+= srcStride
[0];
1541 dstPtr
+= dstStride
[0];
1544 dst
= dstParam
[1] + dstStride
[1]*srcSliceY
/2;
1545 if (c
->dstFormat
== PIX_FMT_NV12
)
1546 interleaveBytes(src
[1], src
[2], dst
, c
->srcW
/2, srcSliceH
/2, srcStride
[1], srcStride
[2], dstStride
[0]);
1548 interleaveBytes(src
[2], src
[1], dst
, c
->srcW
/2, srcSliceH
/2, srcStride
[2], srcStride
[1], dstStride
[0]);
1553 static int PlanarToYuy2Wrapper(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1554 int srcSliceH
, uint8_t* dstParam
[], int dstStride
[]){
1555 uint8_t *dst
=dstParam
[0] + dstStride
[0]*srcSliceY
;
1557 yv12toyuy2(src
[0], src
[1], src
[2], dst
, c
->srcW
, srcSliceH
, srcStride
[0], srcStride
[1], dstStride
[0]);
1562 static int PlanarToUyvyWrapper(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1563 int srcSliceH
, uint8_t* dstParam
[], int dstStride
[]){
1564 uint8_t *dst
=dstParam
[0] + dstStride
[0]*srcSliceY
;
1566 yv12touyvy(src
[0], src
[1], src
[2], dst
, c
->srcW
, srcSliceH
, srcStride
[0], srcStride
[1], dstStride
[0]);
1571 /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
1572 static int rgb2rgbWrapper(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1573 int srcSliceH
, uint8_t* dst
[], int dstStride
[]){
1574 const int srcFormat
= c
->srcFormat
;
1575 const int dstFormat
= c
->dstFormat
;
1576 const int srcBpp
= (fmt_depth(srcFormat
) + 7) >> 3;
1577 const int dstBpp
= (fmt_depth(dstFormat
) + 7) >> 3;
1578 const int srcId
= fmt_depth(srcFormat
) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1579 const int dstId
= fmt_depth(dstFormat
) >> 2;
1580 void (*conv
)(const uint8_t *src
, uint8_t *dst
, long src_size
)=NULL
;
1583 if ( (isBGR(srcFormat
) && isBGR(dstFormat
))
1584 || (isRGB(srcFormat
) && isRGB(dstFormat
))){
1585 switch(srcId
| (dstId
<<4)){
1586 case 0x34: conv
= rgb16to15
; break;
1587 case 0x36: conv
= rgb24to15
; break;
1588 case 0x38: conv
= rgb32to15
; break;
1589 case 0x43: conv
= rgb15to16
; break;
1590 case 0x46: conv
= rgb24to16
; break;
1591 case 0x48: conv
= rgb32to16
; break;
1592 case 0x63: conv
= rgb15to24
; break;
1593 case 0x64: conv
= rgb16to24
; break;
1594 case 0x68: conv
= rgb32to24
; break;
1595 case 0x83: conv
= rgb15to32
; break;
1596 case 0x84: conv
= rgb16to32
; break;
1597 case 0x86: conv
= rgb24to32
; break;
1598 default: av_log(c
, AV_LOG_ERROR
, "internal error %s -> %s converter\n",
1599 sws_format_name(srcFormat
), sws_format_name(dstFormat
)); break;
1601 }else if ( (isBGR(srcFormat
) && isRGB(dstFormat
))
1602 || (isRGB(srcFormat
) && isBGR(dstFormat
))){
1603 switch(srcId
| (dstId
<<4)){
1604 case 0x33: conv
= rgb15tobgr15
; break;
1605 case 0x34: conv
= rgb16tobgr15
; break;
1606 case 0x36: conv
= rgb24tobgr15
; break;
1607 case 0x38: conv
= rgb32tobgr15
; break;
1608 case 0x43: conv
= rgb15tobgr16
; break;
1609 case 0x44: conv
= rgb16tobgr16
; break;
1610 case 0x46: conv
= rgb24tobgr16
; break;
1611 case 0x48: conv
= rgb32tobgr16
; break;
1612 case 0x63: conv
= rgb15tobgr24
; break;
1613 case 0x64: conv
= rgb16tobgr24
; break;
1614 case 0x66: conv
= rgb24tobgr24
; break;
1615 case 0x68: conv
= rgb32tobgr24
; break;
1616 case 0x83: conv
= rgb15tobgr32
; break;
1617 case 0x84: conv
= rgb16tobgr32
; break;
1618 case 0x86: conv
= rgb24tobgr32
; break;
1619 case 0x88: conv
= rgb32tobgr32
; break;
1620 default: av_log(c
, AV_LOG_ERROR
, "internal error %s -> %s converter\n",
1621 sws_format_name(srcFormat
), sws_format_name(dstFormat
)); break;
1624 av_log(c
, AV_LOG_ERROR
, "internal error %s -> %s converter\n",
1625 sws_format_name(srcFormat
), sws_format_name(dstFormat
));
1630 if (dstStride
[0]*srcBpp
== srcStride
[0]*dstBpp
&& srcStride
[0] > 0)
1631 conv(src
[0], dst
[0] + dstStride
[0]*srcSliceY
, srcSliceH
*srcStride
[0]);
1635 uint8_t *srcPtr
= src
[0];
1636 uint8_t *dstPtr
= dst
[0] + dstStride
[0]*srcSliceY
;
1638 for (i
=0; i
<srcSliceH
; i
++)
1640 conv(srcPtr
, dstPtr
, c
->srcW
*srcBpp
);
1641 srcPtr
+= srcStride
[0];
1642 dstPtr
+= dstStride
[0];
1649 static int bgr24toyv12Wrapper(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1650 int srcSliceH
, uint8_t* dst
[], int dstStride
[]){
1654 dst
[0]+ srcSliceY
*dstStride
[0],
1655 dst
[1]+(srcSliceY
>>1)*dstStride
[1],
1656 dst
[2]+(srcSliceY
>>1)*dstStride
[2],
1658 dstStride
[0], dstStride
[1], srcStride
[0]);
1662 static int yvu9toyv12Wrapper(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1663 int srcSliceH
, uint8_t* dst
[], int dstStride
[]){
1667 if (srcStride
[0]==dstStride
[0] && srcStride
[0] > 0)
1668 memcpy(dst
[0]+ srcSliceY
*dstStride
[0], src
[0], srcStride
[0]*srcSliceH
);
1670 uint8_t *srcPtr
= src
[0];
1671 uint8_t *dstPtr
= dst
[0] + dstStride
[0]*srcSliceY
;
1673 for (i
=0; i
<srcSliceH
; i
++)
1675 memcpy(dstPtr
, srcPtr
, c
->srcW
);
1676 srcPtr
+= srcStride
[0];
1677 dstPtr
+= dstStride
[0];
1681 if (c
->dstFormat
==PIX_FMT_YUV420P
){
1682 planar2x(src
[1], dst
[1], c
->chrSrcW
, c
->chrSrcH
, srcStride
[1], dstStride
[1]);
1683 planar2x(src
[2], dst
[2], c
->chrSrcW
, c
->chrSrcH
, srcStride
[2], dstStride
[2]);
1685 planar2x(src
[1], dst
[2], c
->chrSrcW
, c
->chrSrcH
, srcStride
[1], dstStride
[2]);
1686 planar2x(src
[2], dst
[1], c
->chrSrcW
, c
->chrSrcH
, srcStride
[2], dstStride
[1]);
1691 /* unscaled copy like stuff (assumes nearly identical formats) */
1692 static int packedCopy(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1693 int srcSliceH
, uint8_t* dst
[], int dstStride
[])
1695 if (dstStride
[0]==srcStride
[0] && srcStride
[0] > 0)
1696 memcpy(dst
[0] + dstStride
[0]*srcSliceY
, src
[0], srcSliceH
*dstStride
[0]);
1700 uint8_t *srcPtr
= src
[0];
1701 uint8_t *dstPtr
= dst
[0] + dstStride
[0]*srcSliceY
;
1704 /* universal length finder */
1705 while(length
+c
->srcW
<= FFABS(dstStride
[0])
1706 && length
+c
->srcW
<= FFABS(srcStride
[0])) length
+= c
->srcW
;
1709 for (i
=0; i
<srcSliceH
; i
++)
1711 memcpy(dstPtr
, srcPtr
, length
);
1712 srcPtr
+= srcStride
[0];
1713 dstPtr
+= dstStride
[0];
1719 static int planarCopy(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1720 int srcSliceH
, uint8_t* dst
[], int dstStride
[])
1723 for (plane
=0; plane
<3; plane
++)
1725 int length
= plane
==0 ? c
->srcW
: -((-c
->srcW
)>>c
->chrDstHSubSample
);
1726 int y
= plane
==0 ? srcSliceY
: -((-srcSliceY
)>>c
->chrDstVSubSample
);
1727 int height
= plane
==0 ? srcSliceH
: -((-srcSliceH
)>>c
->chrDstVSubSample
);
1729 if ((isGray(c
->srcFormat
) || isGray(c
->dstFormat
)) && plane
>0)
1731 if (!isGray(c
->dstFormat
))
1732 memset(dst
[plane
], 128, dstStride
[plane
]*height
);
1736 if (dstStride
[plane
]==srcStride
[plane
] && srcStride
[plane
] > 0)
1737 memcpy(dst
[plane
] + dstStride
[plane
]*y
, src
[plane
], height
*dstStride
[plane
]);
1741 uint8_t *srcPtr
= src
[plane
];
1742 uint8_t *dstPtr
= dst
[plane
] + dstStride
[plane
]*y
;
1743 for (i
=0; i
<height
; i
++)
1745 memcpy(dstPtr
, srcPtr
, length
);
1746 srcPtr
+= srcStride
[plane
];
1747 dstPtr
+= dstStride
[plane
];
1755 static int gray16togray(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1756 int srcSliceH
, uint8_t* dst
[], int dstStride
[]){
1758 int length
= c
->srcW
;
1760 int height
= srcSliceH
;
1762 uint8_t *srcPtr
= src
[0];
1763 uint8_t *dstPtr
= dst
[0] + dstStride
[0]*y
;
1765 if (!isGray(c
->dstFormat
)){
1766 int height
= -((-srcSliceH
)>>c
->chrDstVSubSample
);
1767 memset(dst
[1], 128, dstStride
[1]*height
);
1768 memset(dst
[2], 128, dstStride
[2]*height
);
1770 if (c
->srcFormat
== PIX_FMT_GRAY16LE
) srcPtr
++;
1771 for (i
=0; i
<height
; i
++)
1773 for (j
=0; j
<length
; j
++) dstPtr
[j
] = srcPtr
[j
<<1];
1774 srcPtr
+= srcStride
[0];
1775 dstPtr
+= dstStride
[0];
1780 static int graytogray16(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1781 int srcSliceH
, uint8_t* dst
[], int dstStride
[]){
1783 int length
= c
->srcW
;
1785 int height
= srcSliceH
;
1787 uint8_t *srcPtr
= src
[0];
1788 uint8_t *dstPtr
= dst
[0] + dstStride
[0]*y
;
1789 for (i
=0; i
<height
; i
++)
1791 for (j
=0; j
<length
; j
++)
1793 dstPtr
[j
<<1] = srcPtr
[j
];
1794 dstPtr
[(j
<<1)+1] = srcPtr
[j
];
1796 srcPtr
+= srcStride
[0];
1797 dstPtr
+= dstStride
[0];
1802 static int gray16swap(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
1803 int srcSliceH
, uint8_t* dst
[], int dstStride
[]){
1805 int length
= c
->srcW
;
1807 int height
= srcSliceH
;
1809 uint16_t *srcPtr
= (uint16_t*)src
[0];
1810 uint16_t *dstPtr
= (uint16_t*)(dst
[0] + dstStride
[0]*y
/2);
1811 for (i
=0; i
<height
; i
++)
1813 for (j
=0; j
<length
; j
++) dstPtr
[j
] = bswap_16(srcPtr
[j
]);
1814 srcPtr
+= srcStride
[0]/2;
1815 dstPtr
+= dstStride
[0]/2;
1821 static void getSubSampleFactors(int *h
, int *v
, int format
){
1823 case PIX_FMT_UYVY422
:
1824 case PIX_FMT_YUYV422
:
1828 case PIX_FMT_YUV420P
:
1829 case PIX_FMT_YUVA420P
:
1830 case PIX_FMT_GRAY16BE
:
1831 case PIX_FMT_GRAY16LE
:
1832 case PIX_FMT_GRAY8
: //FIXME remove after different subsamplings are fully implemented
1838 case PIX_FMT_YUV440P
:
1842 case PIX_FMT_YUV410P
:
1846 case PIX_FMT_YUV444P
:
1850 case PIX_FMT_YUV422P
:
1854 case PIX_FMT_YUV411P
:
1865 static uint16_t roundToInt16(int64_t f
){
1866 int r
= (f
+ (1<<15))>>16;
1867 if (r
<-0x7FFF) return 0x8000;
1868 else if (r
> 0x7FFF) return 0x7FFF;
1873 * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
1874 * @param fullRange if 1 then the luma range is 0..255 if 0 it is 16..235
1875 * @return -1 if not supported
1877 int sws_setColorspaceDetails(SwsContext
*c
, const int inv_table
[4], int srcRange
, const int table
[4], int dstRange
, int brightness
, int contrast
, int saturation
){
1878 int64_t crv
= inv_table
[0];
1879 int64_t cbu
= inv_table
[1];
1880 int64_t cgu
= -inv_table
[2];
1881 int64_t cgv
= -inv_table
[3];
1885 if (isYUV(c
->dstFormat
) || isGray(c
->dstFormat
)) return -1;
1886 memcpy(c
->srcColorspaceTable
, inv_table
, sizeof(int)*4);
1887 memcpy(c
->dstColorspaceTable
, table
, sizeof(int)*4);
1889 c
->brightness
= brightness
;
1890 c
->contrast
= contrast
;
1891 c
->saturation
= saturation
;
1892 c
->srcRange
= srcRange
;
1893 c
->dstRange
= dstRange
;
1895 c
->uOffset
= 0x0400040004000400LL
;
1896 c
->vOffset
= 0x0400040004000400LL
;
1902 crv
= (crv
*224) / 255;
1903 cbu
= (cbu
*224) / 255;
1904 cgu
= (cgu
*224) / 255;
1905 cgv
= (cgv
*224) / 255;
1908 cy
= (cy
*contrast
)>>16;
1909 crv
= (crv
*contrast
* saturation
)>>32;
1910 cbu
= (cbu
*contrast
* saturation
)>>32;
1911 cgu
= (cgu
*contrast
* saturation
)>>32;
1912 cgv
= (cgv
*contrast
* saturation
)>>32;
1914 oy
-= 256*brightness
;
1916 c
->yCoeff
= roundToInt16(cy
*8192) * 0x0001000100010001ULL
;
1917 c
->vrCoeff
= roundToInt16(crv
*8192) * 0x0001000100010001ULL
;
1918 c
->ubCoeff
= roundToInt16(cbu
*8192) * 0x0001000100010001ULL
;
1919 c
->vgCoeff
= roundToInt16(cgv
*8192) * 0x0001000100010001ULL
;
1920 c
->ugCoeff
= roundToInt16(cgu
*8192) * 0x0001000100010001ULL
;
1921 c
->yOffset
= roundToInt16(oy
* 8) * 0x0001000100010001ULL
;
1923 yuv2rgb_c_init_tables(c
, inv_table
, srcRange
, brightness
, contrast
, saturation
);
1926 #ifdef COMPILE_ALTIVEC
1927 if (c
->flags
& SWS_CPU_CAPS_ALTIVEC
)
1928 yuv2rgb_altivec_init_tables (c
, inv_table
, brightness
, contrast
, saturation
);
1934 * @return -1 if not supported
1936 int sws_getColorspaceDetails(SwsContext
*c
, int **inv_table
, int *srcRange
, int **table
, int *dstRange
, int *brightness
, int *contrast
, int *saturation
){
1937 if (isYUV(c
->dstFormat
) || isGray(c
->dstFormat
)) return -1;
1939 *inv_table
= c
->srcColorspaceTable
;
1940 *table
= c
->dstColorspaceTable
;
1941 *srcRange
= c
->srcRange
;
1942 *dstRange
= c
->dstRange
;
1943 *brightness
= c
->brightness
;
1944 *contrast
= c
->contrast
;
1945 *saturation
= c
->saturation
;
1950 static int handle_jpeg(int *format
)
1953 case PIX_FMT_YUVJ420P
:
1954 *format
= PIX_FMT_YUV420P
;
1956 case PIX_FMT_YUVJ422P
:
1957 *format
= PIX_FMT_YUV422P
;
1959 case PIX_FMT_YUVJ444P
:
1960 *format
= PIX_FMT_YUV444P
;
1962 case PIX_FMT_YUVJ440P
:
1963 *format
= PIX_FMT_YUV440P
;
1970 SwsContext
*sws_getContext(int srcW
, int srcH
, int srcFormat
, int dstW
, int dstH
, int dstFormat
, int flags
,
1971 SwsFilter
*srcFilter
, SwsFilter
*dstFilter
, double *param
){
1975 int usesVFilter
, usesHFilter
;
1976 int unscaled
, needsDither
;
1977 int srcRange
, dstRange
;
1978 SwsFilter dummyFilter
= {NULL
, NULL
, NULL
, NULL
};
1979 #if defined(ARCH_X86)
1980 if (flags
& SWS_CPU_CAPS_MMX
)
1981 asm volatile("emms\n\t"::: "memory");
1984 #if !defined(RUNTIME_CPUDETECT) || !defined (CONFIG_GPL) //ensure that the flags match the compiled variant if cpudetect is off
1985 flags
&= ~(SWS_CPU_CAPS_MMX
|SWS_CPU_CAPS_MMX2
|SWS_CPU_CAPS_3DNOW
|SWS_CPU_CAPS_ALTIVEC
|SWS_CPU_CAPS_BFIN
);
1987 flags
|= SWS_CPU_CAPS_MMX
|SWS_CPU_CAPS_MMX2
;
1988 #elif defined (HAVE_3DNOW)
1989 flags
|= SWS_CPU_CAPS_MMX
|SWS_CPU_CAPS_3DNOW
;
1990 #elif defined (HAVE_MMX)
1991 flags
|= SWS_CPU_CAPS_MMX
;
1992 #elif defined (HAVE_ALTIVEC)
1993 flags
|= SWS_CPU_CAPS_ALTIVEC
;
1994 #elif defined (ARCH_BFIN)
1995 flags
|= SWS_CPU_CAPS_BFIN
;
1997 #endif /* RUNTIME_CPUDETECT */
1998 if (clip_table
[512] != 255) globalInit();
1999 if (!rgb15to16
) sws_rgb2rgb_init(flags
);
2001 unscaled
= (srcW
== dstW
&& srcH
== dstH
);
2002 needsDither
= (isBGR(dstFormat
) || isRGB(dstFormat
))
2003 && (fmt_depth(dstFormat
))<24
2004 && ((fmt_depth(dstFormat
))<(fmt_depth(srcFormat
)) || (!(isRGB(srcFormat
) || isBGR(srcFormat
))));
2006 srcRange
= handle_jpeg(&srcFormat
);
2007 dstRange
= handle_jpeg(&dstFormat
);
2009 if (!isSupportedIn(srcFormat
))
2011 av_log(NULL
, AV_LOG_ERROR
, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat
));
2014 if (!isSupportedOut(dstFormat
))
2016 av_log(NULL
, AV_LOG_ERROR
, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat
));
2020 i
= flags
& ( SWS_POINT
2031 if(!i
|| (i
& (i
-1)))
2033 av_log(NULL
, AV_LOG_ERROR
, "swScaler: Exactly one scaler algorithm must be choosen\n");
2039 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
2041 av_log(NULL
, AV_LOG_ERROR
, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2042 srcW
, srcH
, dstW
, dstH
);
2045 if(srcW
> VOFW
|| dstW
> VOFW
){
2046 av_log(NULL
, AV_LOG_ERROR
, "swScaler: Compile time max width is "AV_STRINGIFY(VOFW
)" change VOF/VOFW and recompile\n");
2050 if (!dstFilter
) dstFilter
= &dummyFilter
;
2051 if (!srcFilter
) srcFilter
= &dummyFilter
;
2053 c
= av_mallocz(sizeof(SwsContext
));
2055 c
->av_class
= &sws_context_class
;
2060 c
->lumXInc
= ((srcW
<<16) + (dstW
>>1))/dstW
;
2061 c
->lumYInc
= ((srcH
<<16) + (dstH
>>1))/dstH
;
2063 c
->dstFormat
= dstFormat
;
2064 c
->srcFormat
= srcFormat
;
2065 c
->vRounder
= 4* 0x0001000100010001ULL
;
2067 usesHFilter
= usesVFilter
= 0;
2068 if (dstFilter
->lumV
&& dstFilter
->lumV
->length
>1) usesVFilter
=1;
2069 if (dstFilter
->lumH
&& dstFilter
->lumH
->length
>1) usesHFilter
=1;
2070 if (dstFilter
->chrV
&& dstFilter
->chrV
->length
>1) usesVFilter
=1;
2071 if (dstFilter
->chrH
&& dstFilter
->chrH
->length
>1) usesHFilter
=1;
2072 if (srcFilter
->lumV
&& srcFilter
->lumV
->length
>1) usesVFilter
=1;
2073 if (srcFilter
->lumH
&& srcFilter
->lumH
->length
>1) usesHFilter
=1;
2074 if (srcFilter
->chrV
&& srcFilter
->chrV
->length
>1) usesVFilter
=1;
2075 if (srcFilter
->chrH
&& srcFilter
->chrH
->length
>1) usesHFilter
=1;
2077 getSubSampleFactors(&c
->chrSrcHSubSample
, &c
->chrSrcVSubSample
, srcFormat
);
2078 getSubSampleFactors(&c
->chrDstHSubSample
, &c
->chrDstVSubSample
, dstFormat
);
2080 // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
2081 if ((isBGR(dstFormat
) || isRGB(dstFormat
)) && !(flags
&SWS_FULL_CHR_H_INT
)) c
->chrDstHSubSample
=1;
2083 // drop some chroma lines if the user wants it
2084 c
->vChrDrop
= (flags
&SWS_SRC_V_CHR_DROP_MASK
)>>SWS_SRC_V_CHR_DROP_SHIFT
;
2085 c
->chrSrcVSubSample
+= c
->vChrDrop
;
2087 // drop every 2. pixel for chroma calculation unless user wants full chroma
2088 if ((isBGR(srcFormat
) || isRGB(srcFormat
)) && !(flags
&SWS_FULL_CHR_H_INP
)
2089 && srcFormat
!=PIX_FMT_RGB8
&& srcFormat
!=PIX_FMT_BGR8
2090 && srcFormat
!=PIX_FMT_RGB4
&& srcFormat
!=PIX_FMT_BGR4
2091 && srcFormat
!=PIX_FMT_RGB4_BYTE
&& srcFormat
!=PIX_FMT_BGR4_BYTE
)
2092 c
->chrSrcHSubSample
=1;
2095 c
->param
[0] = param
[0];
2096 c
->param
[1] = param
[1];
2099 c
->param
[1] = SWS_PARAM_DEFAULT
;
2102 c
->chrIntHSubSample
= c
->chrDstHSubSample
;
2103 c
->chrIntVSubSample
= c
->chrSrcVSubSample
;
2105 // Note the -((-x)>>y) is so that we always round toward +inf.
2106 c
->chrSrcW
= -((-srcW
) >> c
->chrSrcHSubSample
);
2107 c
->chrSrcH
= -((-srcH
) >> c
->chrSrcVSubSample
);
2108 c
->chrDstW
= -((-dstW
) >> c
->chrDstHSubSample
);
2109 c
->chrDstH
= -((-dstH
) >> c
->chrDstVSubSample
);
2111 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);
2113 /* unscaled special Cases */
2114 if (unscaled
&& !usesHFilter
&& !usesVFilter
)
2117 if (srcFormat
== PIX_FMT_YUV420P
&& (dstFormat
== PIX_FMT_NV12
|| dstFormat
== PIX_FMT_NV21
))
2119 c
->swScale
= PlanarToNV12Wrapper
;
2123 if ((srcFormat
==PIX_FMT_YUV420P
|| srcFormat
==PIX_FMT_YUV422P
) && (isBGR(dstFormat
) || isRGB(dstFormat
)))
2125 c
->swScale
= yuv2rgb_get_func_ptr(c
);
2129 if (srcFormat
==PIX_FMT_YUV410P
&& dstFormat
==PIX_FMT_YUV420P
)
2131 c
->swScale
= yvu9toyv12Wrapper
;
2135 if (srcFormat
==PIX_FMT_BGR24
&& dstFormat
==PIX_FMT_YUV420P
)
2136 c
->swScale
= bgr24toyv12Wrapper
;
2138 /* rgb/bgr -> rgb/bgr (no dither needed forms) */
2139 if ( (isBGR(srcFormat
) || isRGB(srcFormat
))
2140 && (isBGR(dstFormat
) || isRGB(dstFormat
))
2141 && srcFormat
!= PIX_FMT_BGR8
&& dstFormat
!= PIX_FMT_BGR8
2142 && srcFormat
!= PIX_FMT_RGB8
&& dstFormat
!= PIX_FMT_RGB8
2143 && srcFormat
!= PIX_FMT_BGR4
&& dstFormat
!= PIX_FMT_BGR4
2144 && srcFormat
!= PIX_FMT_RGB4
&& dstFormat
!= PIX_FMT_RGB4
2145 && srcFormat
!= PIX_FMT_BGR4_BYTE
&& dstFormat
!= PIX_FMT_BGR4_BYTE
2146 && srcFormat
!= PIX_FMT_RGB4_BYTE
&& dstFormat
!= PIX_FMT_RGB4_BYTE
2147 && srcFormat
!= PIX_FMT_MONOBLACK
&& dstFormat
!= PIX_FMT_MONOBLACK
2149 c
->swScale
= rgb2rgbWrapper
;
2151 /* LQ converters if -sws 0 or -sws 4*/
2152 if (c
->flags
&(SWS_FAST_BILINEAR
|SWS_POINT
)){
2153 /* rgb/bgr -> rgb/bgr (dither needed forms) */
2154 if ( (isBGR(srcFormat
) || isRGB(srcFormat
))
2155 && (isBGR(dstFormat
) || isRGB(dstFormat
))
2157 c
->swScale
= rgb2rgbWrapper
;
2160 if (srcFormat
== PIX_FMT_YUV420P
&&
2161 (dstFormat
== PIX_FMT_YUYV422
|| dstFormat
== PIX_FMT_UYVY422
))
2163 if (dstFormat
== PIX_FMT_YUYV422
)
2164 c
->swScale
= PlanarToYuy2Wrapper
;
2166 c
->swScale
= PlanarToUyvyWrapper
;
2170 #ifdef COMPILE_ALTIVEC
2171 if ((c
->flags
& SWS_CPU_CAPS_ALTIVEC
) &&
2172 ((srcFormat
== PIX_FMT_YUV420P
&&
2173 (dstFormat
== PIX_FMT_YUYV422
|| dstFormat
== PIX_FMT_UYVY422
)))) {
2174 // unscaled YV12 -> packed YUV, we want speed
2175 if (dstFormat
== PIX_FMT_YUYV422
)
2176 c
->swScale
= yv12toyuy2_unscaled_altivec
;
2178 c
->swScale
= yv12touyvy_unscaled_altivec
;
2183 if ( srcFormat
== dstFormat
2184 || (isPlanarYUV(srcFormat
) && isGray(dstFormat
))
2185 || (isPlanarYUV(dstFormat
) && isGray(srcFormat
)))
2187 if (isPacked(c
->srcFormat
))
2188 c
->swScale
= packedCopy
;
2189 else /* Planar YUV or gray */
2190 c
->swScale
= planarCopy
;
2193 /* gray16{le,be} conversions */
2194 if (isGray16(srcFormat
) && (isPlanarYUV(dstFormat
) || (dstFormat
== PIX_FMT_GRAY8
)))
2196 c
->swScale
= gray16togray
;
2198 if ((isPlanarYUV(srcFormat
) || (srcFormat
== PIX_FMT_GRAY8
)) && isGray16(dstFormat
))
2200 c
->swScale
= graytogray16
;
2202 if (srcFormat
!= dstFormat
&& isGray16(srcFormat
) && isGray16(dstFormat
))
2204 c
->swScale
= gray16swap
;
2208 if (flags
& SWS_CPU_CAPS_BFIN
)
2209 ff_bfin_get_unscaled_swscale (c
);
2213 if (flags
&SWS_PRINT_INFO
)
2214 av_log(c
, AV_LOG_INFO
, "using unscaled %s -> %s special converter\n",
2215 sws_format_name(srcFormat
), sws_format_name(dstFormat
));
2220 if (flags
& SWS_CPU_CAPS_MMX2
)
2222 c
->canMMX2BeUsed
= (dstW
>=srcW
&& (dstW
&31)==0 && (srcW
&15)==0) ? 1 : 0;
2223 if (!c
->canMMX2BeUsed
&& dstW
>=srcW
&& (srcW
&15)==0 && (flags
&SWS_FAST_BILINEAR
))
2225 if (flags
&SWS_PRINT_INFO
)
2226 av_log(c
, AV_LOG_INFO
, "output Width is not a multiple of 32 -> no MMX2 scaler\n");
2228 if (usesHFilter
) c
->canMMX2BeUsed
=0;
2233 c
->chrXInc
= ((c
->chrSrcW
<<16) + (c
->chrDstW
>>1))/c
->chrDstW
;
2234 c
->chrYInc
= ((c
->chrSrcH
<<16) + (c
->chrDstH
>>1))/c
->chrDstH
;
2236 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2237 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2238 // n-2 is the last chrominance sample available
2239 // this is not perfect, but no one should notice the difference, the more correct variant
2240 // would be like the vertical one, but that would require some special code for the
2241 // first and last pixel
2242 if (flags
&SWS_FAST_BILINEAR
)
2244 if (c
->canMMX2BeUsed
)
2249 //we don't use the x86asm scaler if mmx is available
2250 else if (flags
& SWS_CPU_CAPS_MMX
)
2252 c
->lumXInc
= ((srcW
-2)<<16)/(dstW
-2) - 20;
2253 c
->chrXInc
= ((c
->chrSrcW
-2)<<16)/(c
->chrDstW
-2) - 20;
2257 /* precalculate horizontal scaler filter coefficients */
2259 const int filterAlign
=
2260 (flags
& SWS_CPU_CAPS_MMX
) ? 4 :
2261 (flags
& SWS_CPU_CAPS_ALTIVEC
) ? 8 :
2264 initFilter(&c
->hLumFilter
, &c
->hLumFilterPos
, &c
->hLumFilterSize
, c
->lumXInc
,
2265 srcW
, dstW
, filterAlign
, 1<<14,
2266 (flags
&SWS_BICUBLIN
) ? (flags
|SWS_BICUBIC
) : flags
,
2267 srcFilter
->lumH
, dstFilter
->lumH
, c
->param
);
2268 initFilter(&c
->hChrFilter
, &c
->hChrFilterPos
, &c
->hChrFilterSize
, c
->chrXInc
,
2269 c
->chrSrcW
, c
->chrDstW
, filterAlign
, 1<<14,
2270 (flags
&SWS_BICUBLIN
) ? (flags
|SWS_BILINEAR
) : flags
,
2271 srcFilter
->chrH
, dstFilter
->chrH
, c
->param
);
2273 #define MAX_FUNNY_CODE_SIZE 10000
2274 #if defined(COMPILE_MMX2)
2275 // can't downscale !!!
2276 if (c
->canMMX2BeUsed
&& (flags
& SWS_FAST_BILINEAR
))
2278 #ifdef MAP_ANONYMOUS
2279 c
->funnyYCode
= (uint8_t*)mmap(NULL
, MAX_FUNNY_CODE_SIZE
, PROT_EXEC
| PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
, 0, 0);
2280 c
->funnyUVCode
= (uint8_t*)mmap(NULL
, MAX_FUNNY_CODE_SIZE
, PROT_EXEC
| PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
, 0, 0);
2282 c
->funnyYCode
= av_malloc(MAX_FUNNY_CODE_SIZE
);
2283 c
->funnyUVCode
= av_malloc(MAX_FUNNY_CODE_SIZE
);
2286 c
->lumMmx2Filter
= av_malloc((dstW
/8+8)*sizeof(int16_t));
2287 c
->chrMmx2Filter
= av_malloc((c
->chrDstW
/4+8)*sizeof(int16_t));
2288 c
->lumMmx2FilterPos
= av_malloc((dstW
/2/8+8)*sizeof(int32_t));
2289 c
->chrMmx2FilterPos
= av_malloc((c
->chrDstW
/2/4+8)*sizeof(int32_t));
2291 initMMX2HScaler( dstW
, c
->lumXInc
, c
->funnyYCode
, c
->lumMmx2Filter
, c
->lumMmx2FilterPos
, 8);
2292 initMMX2HScaler(c
->chrDstW
, c
->chrXInc
, c
->funnyUVCode
, c
->chrMmx2Filter
, c
->chrMmx2FilterPos
, 4);
2294 #endif /* defined(COMPILE_MMX2) */
2295 } // Init Horizontal stuff
2299 /* precalculate vertical scaler filter coefficients */
2301 const int filterAlign
=
2302 (flags
& SWS_CPU_CAPS_MMX
) && (flags
& SWS_ACCURATE_RND
) ? 2 :
2303 (flags
& SWS_CPU_CAPS_ALTIVEC
) ? 8 :
2306 initFilter(&c
->vLumFilter
, &c
->vLumFilterPos
, &c
->vLumFilterSize
, c
->lumYInc
,
2307 srcH
, dstH
, filterAlign
, (1<<12)-4,
2308 (flags
&SWS_BICUBLIN
) ? (flags
|SWS_BICUBIC
) : flags
,
2309 srcFilter
->lumV
, dstFilter
->lumV
, c
->param
);
2310 initFilter(&c
->vChrFilter
, &c
->vChrFilterPos
, &c
->vChrFilterSize
, c
->chrYInc
,
2311 c
->chrSrcH
, c
->chrDstH
, filterAlign
, (1<<12)-4,
2312 (flags
&SWS_BICUBLIN
) ? (flags
|SWS_BILINEAR
) : flags
,
2313 srcFilter
->chrV
, dstFilter
->chrV
, c
->param
);
2316 c
->vYCoeffsBank
= av_malloc(sizeof (vector
signed short)*c
->vLumFilterSize
*c
->dstH
);
2317 c
->vCCoeffsBank
= av_malloc(sizeof (vector
signed short)*c
->vChrFilterSize
*c
->chrDstH
);
2319 for (i
=0;i
<c
->vLumFilterSize
*c
->dstH
;i
++) {
2321 short *p
= (short *)&c
->vYCoeffsBank
[i
];
2323 p
[j
] = c
->vLumFilter
[i
];
2326 for (i
=0;i
<c
->vChrFilterSize
*c
->chrDstH
;i
++) {
2328 short *p
= (short *)&c
->vCCoeffsBank
[i
];
2330 p
[j
] = c
->vChrFilter
[i
];
2335 // Calculate Buffer Sizes so that they won't run out while handling these damn slices
2336 c
->vLumBufSize
= c
->vLumFilterSize
;
2337 c
->vChrBufSize
= c
->vChrFilterSize
;
2338 for (i
=0; i
<dstH
; i
++)
2340 int chrI
= i
*c
->chrDstH
/ dstH
;
2341 int nextSlice
= FFMAX(c
->vLumFilterPos
[i
] + c
->vLumFilterSize
- 1,
2342 ((c
->vChrFilterPos
[chrI
] + c
->vChrFilterSize
- 1)<<c
->chrSrcVSubSample
));
2344 nextSlice
>>= c
->chrSrcVSubSample
;
2345 nextSlice
<<= c
->chrSrcVSubSample
;
2346 if (c
->vLumFilterPos
[i
] + c
->vLumBufSize
< nextSlice
)
2347 c
->vLumBufSize
= nextSlice
- c
->vLumFilterPos
[i
];
2348 if (c
->vChrFilterPos
[chrI
] + c
->vChrBufSize
< (nextSlice
>>c
->chrSrcVSubSample
))
2349 c
->vChrBufSize
= (nextSlice
>>c
->chrSrcVSubSample
) - c
->vChrFilterPos
[chrI
];
2352 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2353 c
->lumPixBuf
= av_malloc(c
->vLumBufSize
*2*sizeof(int16_t*));
2354 c
->chrPixBuf
= av_malloc(c
->vChrBufSize
*2*sizeof(int16_t*));
2355 //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)
2356 /* align at 16 bytes for AltiVec */
2357 for (i
=0; i
<c
->vLumBufSize
; i
++)
2358 c
->lumPixBuf
[i
]= c
->lumPixBuf
[i
+c
->vLumBufSize
]= av_mallocz(VOF
+1);
2359 for (i
=0; i
<c
->vChrBufSize
; i
++)
2360 c
->chrPixBuf
[i
]= c
->chrPixBuf
[i
+c
->vChrBufSize
]= av_malloc((VOF
+1)*2);
2362 //try to avoid drawing green stuff between the right end and the stride end
2363 for (i
=0; i
<c
->vChrBufSize
; i
++) memset(c
->chrPixBuf
[i
], 64, (VOF
+1)*2);
2365 assert(2*VOFW
== VOF
);
2367 assert(c
->chrDstH
<= dstH
);
2369 if (flags
&SWS_PRINT_INFO
)
2372 const char *dither
= " dithered";
2374 const char *dither
= "";
2376 if (flags
&SWS_FAST_BILINEAR
)
2377 av_log(c
, AV_LOG_INFO
, "FAST_BILINEAR scaler, ");
2378 else if (flags
&SWS_BILINEAR
)
2379 av_log(c
, AV_LOG_INFO
, "BILINEAR scaler, ");
2380 else if (flags
&SWS_BICUBIC
)
2381 av_log(c
, AV_LOG_INFO
, "BICUBIC scaler, ");
2382 else if (flags
&SWS_X
)
2383 av_log(c
, AV_LOG_INFO
, "Experimental scaler, ");
2384 else if (flags
&SWS_POINT
)
2385 av_log(c
, AV_LOG_INFO
, "Nearest Neighbor / POINT scaler, ");
2386 else if (flags
&SWS_AREA
)
2387 av_log(c
, AV_LOG_INFO
, "Area Averageing scaler, ");
2388 else if (flags
&SWS_BICUBLIN
)
2389 av_log(c
, AV_LOG_INFO
, "luma BICUBIC / chroma BILINEAR scaler, ");
2390 else if (flags
&SWS_GAUSS
)
2391 av_log(c
, AV_LOG_INFO
, "Gaussian scaler, ");
2392 else if (flags
&SWS_SINC
)
2393 av_log(c
, AV_LOG_INFO
, "Sinc scaler, ");
2394 else if (flags
&SWS_LANCZOS
)
2395 av_log(c
, AV_LOG_INFO
, "Lanczos scaler, ");
2396 else if (flags
&SWS_SPLINE
)
2397 av_log(c
, AV_LOG_INFO
, "Bicubic spline scaler, ");
2399 av_log(c
, AV_LOG_INFO
, "ehh flags invalid?! ");
2401 if (dstFormat
==PIX_FMT_BGR555
|| dstFormat
==PIX_FMT_BGR565
)
2402 av_log(c
, AV_LOG_INFO
, "from %s to%s %s ",
2403 sws_format_name(srcFormat
), dither
, sws_format_name(dstFormat
));
2405 av_log(c
, AV_LOG_INFO
, "from %s to %s ",
2406 sws_format_name(srcFormat
), sws_format_name(dstFormat
));
2408 if (flags
& SWS_CPU_CAPS_MMX2
)
2409 av_log(c
, AV_LOG_INFO
, "using MMX2\n");
2410 else if (flags
& SWS_CPU_CAPS_3DNOW
)
2411 av_log(c
, AV_LOG_INFO
, "using 3DNOW\n");
2412 else if (flags
& SWS_CPU_CAPS_MMX
)
2413 av_log(c
, AV_LOG_INFO
, "using MMX\n");
2414 else if (flags
& SWS_CPU_CAPS_ALTIVEC
)
2415 av_log(c
, AV_LOG_INFO
, "using AltiVec\n");
2417 av_log(c
, AV_LOG_INFO
, "using C\n");
2420 if (flags
& SWS_PRINT_INFO
)
2422 if (flags
& SWS_CPU_CAPS_MMX
)
2424 if (c
->canMMX2BeUsed
&& (flags
&SWS_FAST_BILINEAR
))
2425 av_log(c
, AV_LOG_VERBOSE
, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2428 if (c
->hLumFilterSize
==4)
2429 av_log(c
, AV_LOG_VERBOSE
, "using 4-tap MMX scaler for horizontal luminance scaling\n");
2430 else if (c
->hLumFilterSize
==8)
2431 av_log(c
, AV_LOG_VERBOSE
, "using 8-tap MMX scaler for horizontal luminance scaling\n");
2433 av_log(c
, AV_LOG_VERBOSE
, "using n-tap MMX scaler for horizontal luminance scaling\n");
2435 if (c
->hChrFilterSize
==4)
2436 av_log(c
, AV_LOG_VERBOSE
, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
2437 else if (c
->hChrFilterSize
==8)
2438 av_log(c
, AV_LOG_VERBOSE
, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
2440 av_log(c
, AV_LOG_VERBOSE
, "using n-tap MMX scaler for horizontal chrominance scaling\n");
2445 #if defined(ARCH_X86)
2446 av_log(c
, AV_LOG_VERBOSE
, "using X86-Asm scaler for horizontal scaling\n");
2448 if (flags
& SWS_FAST_BILINEAR
)
2449 av_log(c
, AV_LOG_VERBOSE
, "using FAST_BILINEAR C scaler for horizontal scaling\n");
2451 av_log(c
, AV_LOG_VERBOSE
, "using C scaler for horizontal scaling\n");
2454 if (isPlanarYUV(dstFormat
))
2456 if (c
->vLumFilterSize
==1)
2457 av_log(c
, AV_LOG_VERBOSE
, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags
& SWS_CPU_CAPS_MMX
) ? "MMX" : "C");
2459 av_log(c
, AV_LOG_VERBOSE
, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags
& SWS_CPU_CAPS_MMX
) ? "MMX" : "C");
2463 if (c
->vLumFilterSize
==1 && c
->vChrFilterSize
==2)
2464 av_log(c
, AV_LOG_VERBOSE
, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2465 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags
& SWS_CPU_CAPS_MMX
) ? "MMX" : "C");
2466 else if (c
->vLumFilterSize
==2 && c
->vChrFilterSize
==2)
2467 av_log(c
, AV_LOG_VERBOSE
, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags
& SWS_CPU_CAPS_MMX
) ? "MMX" : "C");
2469 av_log(c
, AV_LOG_VERBOSE
, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags
& SWS_CPU_CAPS_MMX
) ? "MMX" : "C");
2472 if (dstFormat
==PIX_FMT_BGR24
)
2473 av_log(c
, AV_LOG_VERBOSE
, "using %s YV12->BGR24 Converter\n",
2474 (flags
& SWS_CPU_CAPS_MMX2
) ? "MMX2" : ((flags
& SWS_CPU_CAPS_MMX
) ? "MMX" : "C"));
2475 else if (dstFormat
==PIX_FMT_RGB32
)
2476 av_log(c
, AV_LOG_VERBOSE
, "using %s YV12->BGR32 Converter\n", (flags
& SWS_CPU_CAPS_MMX
) ? "MMX" : "C");
2477 else if (dstFormat
==PIX_FMT_BGR565
)
2478 av_log(c
, AV_LOG_VERBOSE
, "using %s YV12->BGR16 Converter\n", (flags
& SWS_CPU_CAPS_MMX
) ? "MMX" : "C");
2479 else if (dstFormat
==PIX_FMT_BGR555
)
2480 av_log(c
, AV_LOG_VERBOSE
, "using %s YV12->BGR15 Converter\n", (flags
& SWS_CPU_CAPS_MMX
) ? "MMX" : "C");
2482 av_log(c
, AV_LOG_VERBOSE
, "%dx%d -> %dx%d\n", srcW
, srcH
, dstW
, dstH
);
2484 if (flags
& SWS_PRINT_INFO
)
2486 av_log(c
, AV_LOG_DEBUG
, "Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2487 c
->srcW
, c
->srcH
, c
->dstW
, c
->dstH
, c
->lumXInc
, c
->lumYInc
);
2488 av_log(c
, AV_LOG_DEBUG
, "Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2489 c
->chrSrcW
, c
->chrSrcH
, c
->chrDstW
, c
->chrDstH
, c
->chrXInc
, c
->chrYInc
);
2492 c
->swScale
= getSwsFunc(flags
);
2497 * swscale wrapper, so we don't need to export the SwsContext.
2498 * assumes planar YUV to be in YUV order instead of YVU
2500 int sws_scale(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
2501 int srcSliceH
, uint8_t* dst
[], int dstStride
[]){
2503 uint8_t* src2
[4]= {src
[0], src
[1], src
[2]};
2505 if (c
->sliceDir
== 0 && srcSliceY
!= 0 && srcSliceY
+ srcSliceH
!= c
->srcH
) {
2506 av_log(c
, AV_LOG_ERROR
, "Slices start in the middle!\n");
2509 if (c
->sliceDir
== 0) {
2510 if (srcSliceY
== 0) c
->sliceDir
= 1; else c
->sliceDir
= -1;
2513 if (c
->srcFormat
== PIX_FMT_PAL8
){
2514 for (i
=0; i
<256; i
++){
2515 int p
= ((uint32_t*)(src
[1]))[i
];
2516 int r
= (p
>>16)&0xFF;
2517 int g
= (p
>> 8)&0xFF;
2519 int y
= av_clip_uint8(((RY
*r
+ GY
*g
+ BY
*b
)>>RGB2YUV_SHIFT
) + 16 );
2520 int u
= av_clip_uint8(((RU
*r
+ GU
*g
+ BU
*b
)>>RGB2YUV_SHIFT
) + 128);
2521 int v
= av_clip_uint8(((RV
*r
+ GV
*g
+ BV
*b
)>>RGB2YUV_SHIFT
) + 128);
2522 pal
[i
]= y
+ (u
<<8) + (v
<<16);
2524 src2
[1]= (uint8_t*)pal
;
2527 // copy strides, so they can safely be modified
2528 if (c
->sliceDir
== 1) {
2529 // slices go from top to bottom
2530 int srcStride2
[4]= {srcStride
[0], srcStride
[1], srcStride
[2]};
2531 int dstStride2
[4]= {dstStride
[0], dstStride
[1], dstStride
[2]};
2532 return c
->swScale(c
, src2
, srcStride2
, srcSliceY
, srcSliceH
, dst
, dstStride2
);
2534 // slices go from bottom to top => we flip the image internally
2535 uint8_t* dst2
[4]= {dst
[0] + (c
->dstH
-1)*dstStride
[0],
2536 dst
[1] + ((c
->dstH
>>c
->chrDstVSubSample
)-1)*dstStride
[1],
2537 dst
[2] + ((c
->dstH
>>c
->chrDstVSubSample
)-1)*dstStride
[2]};
2538 int srcStride2
[4]= {-srcStride
[0], -srcStride
[1], -srcStride
[2]};
2539 int dstStride2
[4]= {-dstStride
[0], -dstStride
[1], -dstStride
[2]};
2541 src2
[0] += (srcSliceH
-1)*srcStride
[0];
2542 if (c
->srcFormat
!= PIX_FMT_PAL8
)
2543 src2
[1] += ((srcSliceH
>>c
->chrSrcVSubSample
)-1)*srcStride
[1];
2544 src2
[2] += ((srcSliceH
>>c
->chrSrcVSubSample
)-1)*srcStride
[2];
2546 return c
->swScale(c
, src2
, srcStride2
, c
->srcH
-srcSliceY
-srcSliceH
, srcSliceH
, dst2
, dstStride2
);
2551 * swscale wrapper, so we don't need to export the SwsContext
2553 int sws_scale_ordered(SwsContext
*c
, uint8_t* src
[], int srcStride
[], int srcSliceY
,
2554 int srcSliceH
, uint8_t* dst
[], int dstStride
[]){
2555 return sws_scale(c
, src
, srcStride
, srcSliceY
, srcSliceH
, dst
, dstStride
);
2558 SwsFilter
*sws_getDefaultFilter(float lumaGBlur
, float chromaGBlur
,
2559 float lumaSharpen
, float chromaSharpen
,
2560 float chromaHShift
, float chromaVShift
,
2563 SwsFilter
*filter
= av_malloc(sizeof(SwsFilter
));
2565 if (lumaGBlur
!=0.0){
2566 filter
->lumH
= sws_getGaussianVec(lumaGBlur
, 3.0);
2567 filter
->lumV
= sws_getGaussianVec(lumaGBlur
, 3.0);
2569 filter
->lumH
= sws_getIdentityVec();
2570 filter
->lumV
= sws_getIdentityVec();
2573 if (chromaGBlur
!=0.0){
2574 filter
->chrH
= sws_getGaussianVec(chromaGBlur
, 3.0);
2575 filter
->chrV
= sws_getGaussianVec(chromaGBlur
, 3.0);
2577 filter
->chrH
= sws_getIdentityVec();
2578 filter
->chrV
= sws_getIdentityVec();
2581 if (chromaSharpen
!=0.0){
2582 SwsVector
*id
= sws_getIdentityVec();
2583 sws_scaleVec(filter
->chrH
, -chromaSharpen
);
2584 sws_scaleVec(filter
->chrV
, -chromaSharpen
);
2585 sws_addVec(filter
->chrH
, id
);
2586 sws_addVec(filter
->chrV
, id
);
2590 if (lumaSharpen
!=0.0){
2591 SwsVector
*id
= sws_getIdentityVec();
2592 sws_scaleVec(filter
->lumH
, -lumaSharpen
);
2593 sws_scaleVec(filter
->lumV
, -lumaSharpen
);
2594 sws_addVec(filter
->lumH
, id
);
2595 sws_addVec(filter
->lumV
, id
);
2599 if (chromaHShift
!= 0.0)
2600 sws_shiftVec(filter
->chrH
, (int)(chromaHShift
+0.5));
2602 if (chromaVShift
!= 0.0)
2603 sws_shiftVec(filter
->chrV
, (int)(chromaVShift
+0.5));
2605 sws_normalizeVec(filter
->chrH
, 1.0);
2606 sws_normalizeVec(filter
->chrV
, 1.0);
2607 sws_normalizeVec(filter
->lumH
, 1.0);
2608 sws_normalizeVec(filter
->lumV
, 1.0);
2610 if (verbose
) sws_printVec(filter
->chrH
);
2611 if (verbose
) sws_printVec(filter
->lumH
);
2617 * returns a normalized gaussian curve used to filter stuff
2618 * quality=3 is high quality, lowwer is lowwer quality
2620 SwsVector
*sws_getGaussianVec(double variance
, double quality
){
2621 const int length
= (int)(variance
*quality
+ 0.5) | 1;
2623 double *coeff
= av_malloc(length
*sizeof(double));
2624 double middle
= (length
-1)*0.5;
2625 SwsVector
*vec
= av_malloc(sizeof(SwsVector
));
2628 vec
->length
= length
;
2630 for (i
=0; i
<length
; i
++)
2632 double dist
= i
-middle
;
2633 coeff
[i
]= exp(-dist
*dist
/(2*variance
*variance
)) / sqrt(2*variance
*PI
);
2636 sws_normalizeVec(vec
, 1.0);
2641 SwsVector
*sws_getConstVec(double c
, int length
){
2643 double *coeff
= av_malloc(length
*sizeof(double));
2644 SwsVector
*vec
= av_malloc(sizeof(SwsVector
));
2647 vec
->length
= length
;
2649 for (i
=0; i
<length
; i
++)
2656 SwsVector
*sws_getIdentityVec(void){
2657 return sws_getConstVec(1.0, 1);
2660 double sws_dcVec(SwsVector
*a
){
2664 for (i
=0; i
<a
->length
; i
++)
2670 void sws_scaleVec(SwsVector
*a
, double scalar
){
2673 for (i
=0; i
<a
->length
; i
++)
2674 a
->coeff
[i
]*= scalar
;
2677 void sws_normalizeVec(SwsVector
*a
, double height
){
2678 sws_scaleVec(a
, height
/sws_dcVec(a
));
2681 static SwsVector
*sws_getConvVec(SwsVector
*a
, SwsVector
*b
){
2682 int length
= a
->length
+ b
->length
- 1;
2683 double *coeff
= av_malloc(length
*sizeof(double));
2685 SwsVector
*vec
= av_malloc(sizeof(SwsVector
));
2688 vec
->length
= length
;
2690 for (i
=0; i
<length
; i
++) coeff
[i
]= 0.0;
2692 for (i
=0; i
<a
->length
; i
++)
2694 for (j
=0; j
<b
->length
; j
++)
2696 coeff
[i
+j
]+= a
->coeff
[i
]*b
->coeff
[j
];
2703 static SwsVector
*sws_sumVec(SwsVector
*a
, SwsVector
*b
){
2704 int length
= FFMAX(a
->length
, b
->length
);
2705 double *coeff
= av_malloc(length
*sizeof(double));
2707 SwsVector
*vec
= av_malloc(sizeof(SwsVector
));
2710 vec
->length
= length
;
2712 for (i
=0; i
<length
; i
++) coeff
[i
]= 0.0;
2714 for (i
=0; i
<a
->length
; i
++) coeff
[i
+ (length
-1)/2 - (a
->length
-1)/2]+= a
->coeff
[i
];
2715 for (i
=0; i
<b
->length
; i
++) coeff
[i
+ (length
-1)/2 - (b
->length
-1)/2]+= b
->coeff
[i
];
2720 static SwsVector
*sws_diffVec(SwsVector
*a
, SwsVector
*b
){
2721 int length
= FFMAX(a
->length
, b
->length
);
2722 double *coeff
= av_malloc(length
*sizeof(double));
2724 SwsVector
*vec
= av_malloc(sizeof(SwsVector
));
2727 vec
->length
= length
;
2729 for (i
=0; i
<length
; i
++) coeff
[i
]= 0.0;
2731 for (i
=0; i
<a
->length
; i
++) coeff
[i
+ (length
-1)/2 - (a
->length
-1)/2]+= a
->coeff
[i
];
2732 for (i
=0; i
<b
->length
; i
++) coeff
[i
+ (length
-1)/2 - (b
->length
-1)/2]-= b
->coeff
[i
];
2737 /* shift left / or right if "shift" is negative */
2738 static SwsVector
*sws_getShiftedVec(SwsVector
*a
, int shift
){
2739 int length
= a
->length
+ FFABS(shift
)*2;
2740 double *coeff
= av_malloc(length
*sizeof(double));
2742 SwsVector
*vec
= av_malloc(sizeof(SwsVector
));
2745 vec
->length
= length
;
2747 for (i
=0; i
<length
; i
++) coeff
[i
]= 0.0;
2749 for (i
=0; i
<a
->length
; i
++)
2751 coeff
[i
+ (length
-1)/2 - (a
->length
-1)/2 - shift
]= a
->coeff
[i
];
2757 void sws_shiftVec(SwsVector
*a
, int shift
){
2758 SwsVector
*shifted
= sws_getShiftedVec(a
, shift
);
2760 a
->coeff
= shifted
->coeff
;
2761 a
->length
= shifted
->length
;
2765 void sws_addVec(SwsVector
*a
, SwsVector
*b
){
2766 SwsVector
*sum
= sws_sumVec(a
, b
);
2768 a
->coeff
= sum
->coeff
;
2769 a
->length
= sum
->length
;
2773 void sws_subVec(SwsVector
*a
, SwsVector
*b
){
2774 SwsVector
*diff
= sws_diffVec(a
, b
);
2776 a
->coeff
= diff
->coeff
;
2777 a
->length
= diff
->length
;
2781 void sws_convVec(SwsVector
*a
, SwsVector
*b
){
2782 SwsVector
*conv
= sws_getConvVec(a
, b
);
2784 a
->coeff
= conv
->coeff
;
2785 a
->length
= conv
->length
;
2789 SwsVector
*sws_cloneVec(SwsVector
*a
){
2790 double *coeff
= av_malloc(a
->length
*sizeof(double));
2792 SwsVector
*vec
= av_malloc(sizeof(SwsVector
));
2795 vec
->length
= a
->length
;
2797 for (i
=0; i
<a
->length
; i
++) coeff
[i
]= a
->coeff
[i
];
2802 void sws_printVec(SwsVector
*a
){
2808 for (i
=0; i
<a
->length
; i
++)
2809 if (a
->coeff
[i
]>max
) max
= a
->coeff
[i
];
2811 for (i
=0; i
<a
->length
; i
++)
2812 if (a
->coeff
[i
]<min
) min
= a
->coeff
[i
];
2816 for (i
=0; i
<a
->length
; i
++)
2818 int x
= (int)((a
->coeff
[i
]-min
)*60.0/range
+0.5);
2819 av_log(NULL
, AV_LOG_DEBUG
, "%1.3f ", a
->coeff
[i
]);
2820 for (;x
>0; x
--) av_log(NULL
, AV_LOG_DEBUG
, " ");
2821 av_log(NULL
, AV_LOG_DEBUG
, "|\n");
2825 void sws_freeVec(SwsVector
*a
){
2827 av_freep(&a
->coeff
);
2832 void sws_freeFilter(SwsFilter
*filter
){
2833 if (!filter
) return;
2835 if (filter
->lumH
) sws_freeVec(filter
->lumH
);
2836 if (filter
->lumV
) sws_freeVec(filter
->lumV
);
2837 if (filter
->chrH
) sws_freeVec(filter
->chrH
);
2838 if (filter
->chrV
) sws_freeVec(filter
->chrV
);
2843 void sws_freeContext(SwsContext
*c
){
2849 for (i
=0; i
<c
->vLumBufSize
; i
++)
2850 av_freep(&c
->lumPixBuf
[i
]);
2851 av_freep(&c
->lumPixBuf
);
2856 for (i
=0; i
<c
->vChrBufSize
; i
++)
2857 av_freep(&c
->chrPixBuf
[i
]);
2858 av_freep(&c
->chrPixBuf
);
2861 av_freep(&c
->vLumFilter
);
2862 av_freep(&c
->vChrFilter
);
2863 av_freep(&c
->hLumFilter
);
2864 av_freep(&c
->hChrFilter
);
2866 av_freep(&c
->vYCoeffsBank
);
2867 av_freep(&c
->vCCoeffsBank
);
2870 av_freep(&c
->vLumFilterPos
);
2871 av_freep(&c
->vChrFilterPos
);
2872 av_freep(&c
->hLumFilterPos
);
2873 av_freep(&c
->hChrFilterPos
);
2875 #if defined(ARCH_X86) && defined(CONFIG_GPL)
2876 #ifdef MAP_ANONYMOUS
2877 if (c
->funnyYCode
) munmap(c
->funnyYCode
, MAX_FUNNY_CODE_SIZE
);
2878 if (c
->funnyUVCode
) munmap(c
->funnyUVCode
, MAX_FUNNY_CODE_SIZE
);
2880 av_free(c
->funnyYCode
);
2881 av_free(c
->funnyUVCode
);
2884 c
->funnyUVCode
=NULL
;
2885 #endif /* defined(ARCH_X86) */
2887 av_freep(&c
->lumMmx2Filter
);
2888 av_freep(&c
->chrMmx2Filter
);
2889 av_freep(&c
->lumMmx2FilterPos
);
2890 av_freep(&c
->chrMmx2FilterPos
);
2891 av_freep(&c
->yuvTable
);
2897 * Checks if context is valid or reallocs a new one instead.
2898 * If context is NULL, just calls sws_getContext() to get a new one.
2899 * Otherwise, checks if the parameters are the same already saved in context.
2900 * If that is the case, returns the current context.
2901 * Otherwise, frees context and gets a new one.
2903 * Be warned that srcFilter, dstFilter are not checked, they are
2904 * asumed to remain valid.
2906 struct SwsContext
*sws_getCachedContext(struct SwsContext
*context
,
2907 int srcW
, int srcH
, int srcFormat
,
2908 int dstW
, int dstH
, int dstFormat
, int flags
,
2909 SwsFilter
*srcFilter
, SwsFilter
*dstFilter
, double *param
)
2911 static const double default_param
[2] = {SWS_PARAM_DEFAULT
, SWS_PARAM_DEFAULT
};
2914 param
= default_param
;
2917 if (context
->srcW
!= srcW
|| context
->srcH
!= srcH
||
2918 context
->srcFormat
!= srcFormat
||
2919 context
->dstW
!= dstW
|| context
->dstH
!= dstH
||
2920 context
->dstFormat
!= dstFormat
|| context
->flags
!= flags
||
2921 context
->param
[0] != param
[0] || context
->param
[1] != param
[1])
2923 sws_freeContext(context
);
2928 return sws_getContext(srcW
, srcH
, srcFormat
,
2929 dstW
, dstH
, dstFormat
, flags
,
2930 srcFilter
, dstFilter
, param
);