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 St, 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 didnt write it down ...)
48 untested special converters
49 YV12/I420 -> BGR15/BGR24/BGR32 (its 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"
76 #include "libvo/fastmemcpy.h"
86 //#define WORDS_BIGENDIAN
89 #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
91 #define RET 0xC3 //near return opcode for X86
94 #define ASSERT(x) assert(x);
102 #define PI 3.14159265358979323846
105 #define isSupportedIn(x) ( \
106 (x)==PIX_FMT_YUV420P \
107 || (x)==PIX_FMT_YUYV422 \
108 || (x)==PIX_FMT_UYVY422 \
109 || (x)==PIX_FMT_RGB32 \
110 || (x)==PIX_FMT_BGR24 \
111 || (x)==PIX_FMT_BGR565 \
112 || (x)==PIX_FMT_BGR555 \
113 || (x)==PIX_FMT_BGR32 \
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 \
130 #define isSupportedOut(x) ( \
131 (x)==PIX_FMT_YUV420P \
132 || (x)==PIX_FMT_YUYV422 \
133 || (x)==PIX_FMT_UYVY422 \
134 || (x)==PIX_FMT_YUV444P \
135 || (x)==PIX_FMT_YUV422P \
136 || (x)==PIX_FMT_YUV411P \
139 || (x)==PIX_FMT_NV12 \
140 || (x)==PIX_FMT_NV21 \
141 || (x)==PIX_FMT_GRAY16BE \
142 || (x)==PIX_FMT_GRAY16LE \
143 || (x)==PIX_FMT_GRAY8 \
144 || (x)==PIX_FMT_YUV410P \
146 #define isPacked(x) ( \
148 || (x)==PIX_FMT_YUYV422 \
149 || (x)==PIX_FMT_UYVY422 \
154 #define RGB2YUV_SHIFT 16
155 #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
156 #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
157 #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
158 #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
159 #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
160 #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
161 #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
162 #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
163 #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
165 extern const int32_t Inverse_Table_6_9[8][4];
169 Special versions: fast Y 1:1 scaling (no interpolation in y direction)
172 more intelligent missalignment avoidance for the horizontal scaler
173 write special vertical cubic upscale version
174 Optimize C code (yv12 / minmax)
175 add support for packed pixel yuv input & output
176 add support for Y8 output
177 optimize bgr24 & bgr32
178 add BGR4 output support
179 write special BGR->BGR scaler
182 #if defined(ARCH_X86) && defined (CONFIG_GPL)
183 static uint64_t attribute_used __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
184 static uint64_t attribute_used __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
185 static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
186 static uint64_t attribute_used __attribute__((aligned(8))) w02= 0x0002000200020002LL;
187 static uint64_t attribute_used __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
188 static uint64_t attribute_used __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
189 static uint64_t attribute_used __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
190 static uint64_t attribute_used __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
192 static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
193 static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
194 static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
195 static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
197 static uint64_t __attribute__((aligned(8))) dither4[2]={
198 0x0103010301030103LL,
199 0x0200020002000200LL,};
201 static uint64_t __attribute__((aligned(8))) dither8[2]={
202 0x0602060206020602LL,
203 0x0004000400040004LL,};
205 static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
206 static uint64_t attribute_used __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
207 static uint64_t attribute_used __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
208 static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
209 static uint64_t attribute_used __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
210 static uint64_t attribute_used __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
212 static uint64_t attribute_used __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
213 static uint64_t attribute_used __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
214 static uint64_t attribute_used __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
217 static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
218 static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
219 static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
221 static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
222 static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
223 static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
224 #endif /* FAST_BGR2YV12 */
225 static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
226 static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8))) = 0x8080808080808080ULL;
227 static const uint64_t w1111 attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
228 #endif /* defined(ARCH_X86) */
230 // clipping helper table for C implementations:
231 static unsigned char clip_table[768];
233 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
235 extern const uint8_t dither_2x2_4[2][8];
236 extern const uint8_t dither_2x2_8[2][8];
237 extern const uint8_t dither_8x8_32[8][8];
238 extern const uint8_t dither_8x8_73[8][8];
239 extern const uint8_t dither_8x8_220[8][8];
241 static const char * sws_context_to_name(void * ptr) {
245 static AVClass sws_context_class = { "SWScaler", sws_context_to_name, NULL };
247 char *sws_format_name(enum PixelFormat format)
250 case PIX_FMT_YUV420P:
252 case PIX_FMT_YUYV422:
258 case PIX_FMT_YUV422P:
260 case PIX_FMT_YUV444P:
264 case PIX_FMT_YUV410P:
266 case PIX_FMT_YUV411P:
272 case PIX_FMT_GRAY16BE:
274 case PIX_FMT_GRAY16LE:
278 case PIX_FMT_MONOWHITE:
280 case PIX_FMT_MONOBLACK:
284 case PIX_FMT_YUVJ420P:
286 case PIX_FMT_YUVJ422P:
288 case PIX_FMT_YUVJ444P:
290 case PIX_FMT_XVMC_MPEG2_MC:
291 return "xvmc_mpeg2_mc";
292 case PIX_FMT_XVMC_MPEG2_IDCT:
293 return "xvmc_mpeg2_idct";
294 case PIX_FMT_UYVY422:
296 case PIX_FMT_UYYVYY411:
298 case PIX_FMT_RGB32_1:
300 case PIX_FMT_BGR32_1:
312 case PIX_FMT_BGR4_BYTE:
318 case PIX_FMT_RGB4_BYTE:
325 return "Unknown format";
329 #if defined(ARCH_X86) && defined (CONFIG_GPL)
330 void in_asm_used_var_warning_killer()
332 volatile int i= bF8+bFC+w10+
333 bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
334 M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
339 static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
340 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
341 uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
343 //FIXME Optimize (just quickly writen not opti..)
345 for (i=0; i<dstW; i++)
349 for (j=0; j<lumFilterSize; j++)
350 val += lumSrc[j][i] * lumFilter[j];
352 dest[i]= av_clip_uint8(val>>19);
356 for (i=0; i<chrDstW; i++)
361 for (j=0; j<chrFilterSize; j++)
363 u += chrSrc[j][i] * chrFilter[j];
364 v += chrSrc[j][i + 2048] * chrFilter[j];
367 uDest[i]= av_clip_uint8(u>>19);
368 vDest[i]= av_clip_uint8(v>>19);
372 static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
373 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
374 uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
376 //FIXME Optimize (just quickly writen not opti..)
378 for (i=0; i<dstW; i++)
382 for (j=0; j<lumFilterSize; j++)
383 val += lumSrc[j][i] * lumFilter[j];
385 dest[i]= av_clip_uint8(val>>19);
391 if (dstFormat == PIX_FMT_NV12)
392 for (i=0; i<chrDstW; i++)
397 for (j=0; j<chrFilterSize; j++)
399 u += chrSrc[j][i] * chrFilter[j];
400 v += chrSrc[j][i + 2048] * chrFilter[j];
403 uDest[2*i]= av_clip_uint8(u>>19);
404 uDest[2*i+1]= av_clip_uint8(v>>19);
407 for (i=0; i<chrDstW; i++)
412 for (j=0; j<chrFilterSize; j++)
414 u += chrSrc[j][i] * chrFilter[j];
415 v += chrSrc[j][i + 2048] * chrFilter[j];
418 uDest[2*i]= av_clip_uint8(v>>19);
419 uDest[2*i+1]= av_clip_uint8(u>>19);
423 #define YSCALE_YUV_2_PACKEDX_C(type) \
424 for (i=0; i<(dstW>>1); i++){\
430 type attribute_unused *r, *b, *g;\
433 for (j=0; j<lumFilterSize; j++)\
435 Y1 += lumSrc[j][i2] * lumFilter[j];\
436 Y2 += lumSrc[j][i2+1] * lumFilter[j];\
438 for (j=0; j<chrFilterSize; j++)\
440 U += chrSrc[j][i] * chrFilter[j];\
441 V += chrSrc[j][i+2048] * chrFilter[j];\
447 if ((Y1|Y2|U|V)&256)\
449 if (Y1>255) Y1=255; \
450 else if (Y1<0)Y1=0; \
451 if (Y2>255) Y2=255; \
452 else if (Y2<0)Y2=0; \
459 #define YSCALE_YUV_2_RGBX_C(type) \
460 YSCALE_YUV_2_PACKEDX_C(type) \
461 r = (type *)c->table_rV[V]; \
462 g = (type *)(c->table_gU[U] + c->table_gV[V]); \
463 b = (type *)c->table_bU[U]; \
465 #define YSCALE_YUV_2_PACKED2_C \
466 for (i=0; i<(dstW>>1); i++){ \
468 int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
469 int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
470 int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
471 int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19; \
473 #define YSCALE_YUV_2_RGB2_C(type) \
474 YSCALE_YUV_2_PACKED2_C\
476 r = (type *)c->table_rV[V];\
477 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
478 b = (type *)c->table_bU[U];\
480 #define YSCALE_YUV_2_PACKED1_C \
481 for (i=0; i<(dstW>>1); i++){\
483 int Y1= buf0[i2 ]>>7;\
484 int Y2= buf0[i2+1]>>7;\
485 int U= (uvbuf1[i ])>>7;\
486 int V= (uvbuf1[i+2048])>>7;\
488 #define YSCALE_YUV_2_RGB1_C(type) \
489 YSCALE_YUV_2_PACKED1_C\
491 r = (type *)c->table_rV[V];\
492 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
493 b = (type *)c->table_bU[U];\
495 #define YSCALE_YUV_2_PACKED1B_C \
496 for (i=0; i<(dstW>>1); i++){\
498 int Y1= buf0[i2 ]>>7;\
499 int Y2= buf0[i2+1]>>7;\
500 int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
501 int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
503 #define YSCALE_YUV_2_RGB1B_C(type) \
504 YSCALE_YUV_2_PACKED1B_C\
506 r = (type *)c->table_rV[V];\
507 g = (type *)(c->table_gU[U] + c->table_gV[V]);\
508 b = (type *)c->table_bU[U];\
510 #define YSCALE_YUV_2_ANYRGB_C(func, func2)\
511 switch(c->dstFormat)\
516 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
517 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
522 ((uint8_t*)dest)[0]= r[Y1];\
523 ((uint8_t*)dest)[1]= g[Y1];\
524 ((uint8_t*)dest)[2]= b[Y1];\
525 ((uint8_t*)dest)[3]= r[Y2];\
526 ((uint8_t*)dest)[4]= g[Y2];\
527 ((uint8_t*)dest)[5]= b[Y2];\
533 ((uint8_t*)dest)[0]= b[Y1];\
534 ((uint8_t*)dest)[1]= g[Y1];\
535 ((uint8_t*)dest)[2]= r[Y1];\
536 ((uint8_t*)dest)[3]= b[Y2];\
537 ((uint8_t*)dest)[4]= g[Y2];\
538 ((uint8_t*)dest)[5]= r[Y2];\
542 case PIX_FMT_RGB565:\
543 case PIX_FMT_BGR565:\
545 const int dr1= dither_2x2_8[y&1 ][0];\
546 const int dg1= dither_2x2_4[y&1 ][0];\
547 const int db1= dither_2x2_8[(y&1)^1][0];\
548 const int dr2= dither_2x2_8[y&1 ][1];\
549 const int dg2= dither_2x2_4[y&1 ][1];\
550 const int db2= dither_2x2_8[(y&1)^1][1];\
552 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
553 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
557 case PIX_FMT_RGB555:\
558 case PIX_FMT_BGR555:\
560 const int dr1= dither_2x2_8[y&1 ][0];\
561 const int dg1= dither_2x2_8[y&1 ][1];\
562 const int db1= dither_2x2_8[(y&1)^1][0];\
563 const int dr2= dither_2x2_8[y&1 ][1];\
564 const int dg2= dither_2x2_8[y&1 ][0];\
565 const int db2= dither_2x2_8[(y&1)^1][1];\
567 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
568 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
575 const uint8_t * const d64= dither_8x8_73[y&7];\
576 const uint8_t * const d32= dither_8x8_32[y&7];\
578 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
579 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
586 const uint8_t * const d64= dither_8x8_73 [y&7];\
587 const uint8_t * const d128=dither_8x8_220[y&7];\
589 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
590 + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
594 case PIX_FMT_RGB4_BYTE:\
595 case PIX_FMT_BGR4_BYTE:\
597 const uint8_t * const d64= dither_8x8_73 [y&7];\
598 const uint8_t * const d128=dither_8x8_220[y&7];\
600 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
601 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
605 case PIX_FMT_MONOBLACK:\
607 const uint8_t * const d128=dither_8x8_220[y&7];\
608 uint8_t *g= c->table_gU[128] + c->table_gV[128];\
609 for (i=0; i<dstW-7; i+=8){\
611 acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
612 acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
613 acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
614 acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
615 acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
616 acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
617 acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
618 acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
619 ((uint8_t*)dest)[0]= acc;\
624 ((uint8_t*)dest)-= dstW>>4;\
628 static int top[1024];\
629 static int last_new[1024][1024];\
630 static int last_in3[1024][1024];\
631 static int drift[1024][1024];\
635 const uint8_t * const d128=dither_8x8_220[y&7];\
640 for (i=dstW>>1; i<dstW; i++){\
641 int in= ((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19);\
642 int in2 = (76309 * (in - 16) + 32768) >> 16;\
643 int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
644 int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
645 + (last_new[y][i] - in3)*f/256;\
646 int new= old> 128 ? 255 : 0;\
648 error_new+= FFABS(last_new[y][i] - new);\
649 error_in3+= FFABS(last_in3[y][i] - in3);\
650 f= error_new - error_in3*4;\
655 left= top[i]= old - new;\
656 last_new[y][i]= new;\
657 last_in3[y][i]= in3;\
659 acc+= acc + (new&1);\
661 ((uint8_t*)dest)[0]= acc;\
669 case PIX_FMT_YUYV422:\
671 ((uint8_t*)dest)[2*i2+0]= Y1;\
672 ((uint8_t*)dest)[2*i2+1]= U;\
673 ((uint8_t*)dest)[2*i2+2]= Y2;\
674 ((uint8_t*)dest)[2*i2+3]= V;\
677 case PIX_FMT_UYVY422:\
679 ((uint8_t*)dest)[2*i2+0]= U;\
680 ((uint8_t*)dest)[2*i2+1]= Y1;\
681 ((uint8_t*)dest)[2*i2+2]= V;\
682 ((uint8_t*)dest)[2*i2+3]= Y2;\
688 static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
689 int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
690 uint8_t *dest, int dstW, int y)
697 YSCALE_YUV_2_RGBX_C(uint32_t)
698 ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
699 ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
703 YSCALE_YUV_2_RGBX_C(uint8_t)
704 ((uint8_t*)dest)[0]= r[Y1];
705 ((uint8_t*)dest)[1]= g[Y1];
706 ((uint8_t*)dest)[2]= b[Y1];
707 ((uint8_t*)dest)[3]= r[Y2];
708 ((uint8_t*)dest)[4]= g[Y2];
709 ((uint8_t*)dest)[5]= b[Y2];
714 YSCALE_YUV_2_RGBX_C(uint8_t)
715 ((uint8_t*)dest)[0]= b[Y1];
716 ((uint8_t*)dest)[1]= g[Y1];
717 ((uint8_t*)dest)[2]= r[Y1];
718 ((uint8_t*)dest)[3]= b[Y2];
719 ((uint8_t*)dest)[4]= g[Y2];
720 ((uint8_t*)dest)[5]= r[Y2];
727 const int dr1= dither_2x2_8[y&1 ][0];
728 const int dg1= dither_2x2_4[y&1 ][0];
729 const int db1= dither_2x2_8[(y&1)^1][0];
730 const int dr2= dither_2x2_8[y&1 ][1];
731 const int dg2= dither_2x2_4[y&1 ][1];
732 const int db2= dither_2x2_8[(y&1)^1][1];
733 YSCALE_YUV_2_RGBX_C(uint16_t)
734 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
735 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
742 const int dr1= dither_2x2_8[y&1 ][0];
743 const int dg1= dither_2x2_8[y&1 ][1];
744 const int db1= dither_2x2_8[(y&1)^1][0];
745 const int dr2= dither_2x2_8[y&1 ][1];
746 const int dg2= dither_2x2_8[y&1 ][0];
747 const int db2= dither_2x2_8[(y&1)^1][1];
748 YSCALE_YUV_2_RGBX_C(uint16_t)
749 ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
750 ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
757 const uint8_t * const d64= dither_8x8_73[y&7];
758 const uint8_t * const d32= dither_8x8_32[y&7];
759 YSCALE_YUV_2_RGBX_C(uint8_t)
760 ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
761 ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
768 const uint8_t * const d64= dither_8x8_73 [y&7];
769 const uint8_t * const d128=dither_8x8_220[y&7];
770 YSCALE_YUV_2_RGBX_C(uint8_t)
771 ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]
772 +((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);
776 case PIX_FMT_RGB4_BYTE:
777 case PIX_FMT_BGR4_BYTE:
779 const uint8_t * const d64= dither_8x8_73 [y&7];
780 const uint8_t * const d128=dither_8x8_220[y&7];
781 YSCALE_YUV_2_RGBX_C(uint8_t)
782 ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
783 ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
787 case PIX_FMT_MONOBLACK:
789 const uint8_t * const d128=dither_8x8_220[y&7];
790 uint8_t *g= c->table_gU[128] + c->table_gV[128];
792 for (i=0; i<dstW-1; i+=2){
797 for (j=0; j<lumFilterSize; j++)
799 Y1 += lumSrc[j][i] * lumFilter[j];
800 Y2 += lumSrc[j][i+1] * lumFilter[j];
811 acc+= acc + g[Y1+d128[(i+0)&7]];
812 acc+= acc + g[Y2+d128[(i+1)&7]];
814 ((uint8_t*)dest)[0]= acc;
820 case PIX_FMT_YUYV422:
821 YSCALE_YUV_2_PACKEDX_C(void)
822 ((uint8_t*)dest)[2*i2+0]= Y1;
823 ((uint8_t*)dest)[2*i2+1]= U;
824 ((uint8_t*)dest)[2*i2+2]= Y2;
825 ((uint8_t*)dest)[2*i2+3]= V;
828 case PIX_FMT_UYVY422:
829 YSCALE_YUV_2_PACKEDX_C(void)
830 ((uint8_t*)dest)[2*i2+0]= U;
831 ((uint8_t*)dest)[2*i2+1]= Y1;
832 ((uint8_t*)dest)[2*i2+2]= V;
833 ((uint8_t*)dest)[2*i2+3]= Y2;
840 //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
842 #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT) || !defined(CONFIG_GPL)
847 #if (defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
848 #define COMPILE_ALTIVEC
849 #endif //HAVE_ALTIVEC
850 #endif //ARCH_POWERPC
852 #if defined(ARCH_X86)
854 #if ((defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
858 #if (defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
862 #if ((defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)) && defined (CONFIG_GPL)
863 #define COMPILE_3DNOW
865 #endif //ARCH_X86 || ARCH_X86_64
876 #define RENAME(a) a ## _C
877 #include "swscale_template.c"
881 #ifdef COMPILE_ALTIVEC
884 #define RENAME(a) a ## _altivec
885 #include "swscale_template.c"
887 #endif //ARCH_POWERPC
889 #if defined(ARCH_X86)
898 #define RENAME(a) a ## _X86
899 #include "swscale_template.c"
907 #define RENAME(a) a ## _MMX
908 #include "swscale_template.c"
917 #define RENAME(a) a ## _MMX2
918 #include "swscale_template.c"
927 #define RENAME(a) a ## _3DNow
928 #include "swscale_template.c"
931 #endif //ARCH_X86 || ARCH_X86_64
933 // minor note: the HAVE_xyz is messed up after that line so don't use it
935 static double getSplineCoeff(double a, double b, double c, double d, double dist)
937 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
938 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
939 else return getSplineCoeff( 0.0,
946 static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
947 int srcW, int dstW, int filterAlign, int one, int flags,
948 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
955 double *filter2=NULL;
956 #if defined(ARCH_X86)
957 if (flags & SWS_CPU_CAPS_MMX)
958 asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
961 // Note the +1 is for the MMXscaler which reads over the end
962 *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
964 if (FFABS(xInc - 0x10000) <10) // unscaled
968 filter= av_malloc(dstW*sizeof(double)*filterSize);
969 for (i=0; i<dstW*filterSize; i++) filter[i]=0;
971 for (i=0; i<dstW; i++)
973 filter[i*filterSize]=1;
978 else if (flags&SWS_POINT) // lame looking point sampling mode
983 filter= av_malloc(dstW*sizeof(double)*filterSize);
985 xDstInSrc= xInc/2 - 0x8000;
986 for (i=0; i<dstW; i++)
988 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
995 else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
999 if (flags&SWS_BICUBIC) filterSize= 4;
1000 else if (flags&SWS_X ) filterSize= 4;
1001 else filterSize= 2; // SWS_BILINEAR / SWS_AREA
1002 filter= av_malloc(dstW*sizeof(double)*filterSize);
1004 xDstInSrc= xInc/2 - 0x8000;
1005 for (i=0; i<dstW; i++)
1007 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
1010 (*filterPos)[i]= xx;
1011 //Bilinear upscale / linear interpolate / Area averaging
1012 for (j=0; j<filterSize; j++)
1014 double d= FFABS((xx<<16) - xDstInSrc)/(double)(1<<16);
1015 double coeff= 1.0 - d;
1016 if (coeff<0) coeff=0;
1017 filter[i*filterSize + j]= coeff;
1026 double sizeFactor, filterSizeInSrc;
1027 const double xInc1= (double)xInc / (double)(1<<16);
1029 if (flags&SWS_BICUBIC) sizeFactor= 4.0;
1030 else if (flags&SWS_X) sizeFactor= 8.0;
1031 else if (flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
1032 else if (flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
1033 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
1034 else if (flags&SWS_SINC) sizeFactor= 20.0; // infinite ;)
1035 else if (flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
1036 else if (flags&SWS_BILINEAR) sizeFactor= 2.0;
1038 sizeFactor= 0.0; //GCC warning killer
1042 if (xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
1043 else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
1045 filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
1046 if (filterSize > srcW-2) filterSize=srcW-2;
1048 filter= av_malloc(dstW*sizeof(double)*filterSize);
1050 xDstInSrc= xInc1 / 2.0 - 0.5;
1051 for (i=0; i<dstW; i++)
1053 int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
1055 (*filterPos)[i]= xx;
1056 for (j=0; j<filterSize; j++)
1058 double d= FFABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
1060 if (flags & SWS_BICUBIC)
1062 double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
1063 double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
1066 coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
1068 coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
1072 /* else if (flags & SWS_X)
1074 double p= param ? param*0.01 : 0.3;
1075 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1076 coeff*= pow(2.0, - p*d*d);
1078 else if (flags & SWS_X)
1080 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
1086 if (coeff<0.0) coeff= -pow(-coeff, A);
1087 else coeff= pow( coeff, A);
1088 coeff= coeff*0.5 + 0.5;
1090 else if (flags & SWS_AREA)
1092 double srcPixelSize= 1.0/xInc1;
1093 if (d + srcPixelSize/2 < 0.5) coeff= 1.0;
1094 else if (d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
1097 else if (flags & SWS_GAUSS)
1099 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1100 coeff = pow(2.0, - p*d*d);
1102 else if (flags & SWS_SINC)
1104 coeff = d ? sin(d*PI)/(d*PI) : 1.0;
1106 else if (flags & SWS_LANCZOS)
1108 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
1109 coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
1112 else if (flags & SWS_BILINEAR)
1115 if (coeff<0) coeff=0;
1117 else if (flags & SWS_SPLINE)
1119 double p=-2.196152422706632;
1120 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
1123 coeff= 0.0; //GCC warning killer
1127 filter[i*filterSize + j]= coeff;
1134 /* apply src & dst Filter to filter -> filter2
1137 ASSERT(filterSize>0)
1138 filter2Size= filterSize;
1139 if (srcFilter) filter2Size+= srcFilter->length - 1;
1140 if (dstFilter) filter2Size+= dstFilter->length - 1;
1141 ASSERT(filter2Size>0)
1142 filter2= av_malloc(filter2Size*dstW*sizeof(double));
1144 for (i=0; i<dstW; i++)
1147 SwsVector scaleFilter;
1150 scaleFilter.coeff= filter + i*filterSize;
1151 scaleFilter.length= filterSize;
1153 if (srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
1154 else outVec= &scaleFilter;
1156 ASSERT(outVec->length == filter2Size)
1159 for (j=0; j<outVec->length; j++)
1161 filter2[i*filter2Size + j]= outVec->coeff[j];
1164 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
1166 if (outVec != &scaleFilter) sws_freeVec(outVec);
1168 av_free(filter); filter=NULL;
1170 /* try to reduce the filter-size (step1 find size and shift left) */
1171 // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
1173 for (i=dstW-1; i>=0; i--)
1175 int min= filter2Size;
1179 /* get rid off near zero elements on the left by shifting left */
1180 for (j=0; j<filter2Size; j++)
1183 cutOff += FFABS(filter2[i*filter2Size]);
1185 if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1187 /* preserve Monotonicity because the core can't handle the filter otherwise */
1188 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
1190 // Move filter coeffs left
1191 for (k=1; k<filter2Size; k++)
1192 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
1193 filter2[i*filter2Size + k - 1]= 0.0;
1198 /* count near zeros on the right */
1199 for (j=filter2Size-1; j>0; j--)
1201 cutOff += FFABS(filter2[i*filter2Size + j]);
1203 if (cutOff > SWS_MAX_REDUCE_CUTOFF) break;
1207 if (min>minFilterSize) minFilterSize= min;
1210 if (flags & SWS_CPU_CAPS_ALTIVEC) {
1211 // we can handle the special case 4,
1212 // so we don't want to go to the full 8
1213 if (minFilterSize < 5)
1216 // we really don't want to waste our time
1217 // doing useless computation, so fall-back on
1218 // the scalar C code for very small filter.
1219 // vectorizing is worth it only if you have
1220 // decent-sized vector.
1221 if (minFilterSize < 3)
1225 if (flags & SWS_CPU_CAPS_MMX) {
1226 // special case for unscaled vertical filtering
1227 if (minFilterSize == 1 && filterAlign == 2)
1231 ASSERT(minFilterSize > 0)
1232 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
1233 ASSERT(filterSize > 0)
1234 filter= av_malloc(filterSize*dstW*sizeof(double));
1235 if (filterSize >= MAX_FILTER_SIZE)
1237 *outFilterSize= filterSize;
1239 if (flags&SWS_PRINT_INFO)
1240 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
1241 /* try to reduce the filter-size (step2 reduce it) */
1242 for (i=0; i<dstW; i++)
1246 for (j=0; j<filterSize; j++)
1248 if (j>=filter2Size) filter[i*filterSize + j]= 0.0;
1249 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
1252 av_free(filter2); filter2=NULL;
1255 //FIXME try to align filterpos if possible
1258 for (i=0; i<dstW; i++)
1261 if ((*filterPos)[i] < 0)
1263 // Move filter coeffs left to compensate for filterPos
1264 for (j=1; j<filterSize; j++)
1266 int left= FFMAX(j + (*filterPos)[i], 0);
1267 filter[i*filterSize + left] += filter[i*filterSize + j];
1268 filter[i*filterSize + j]=0;
1273 if ((*filterPos)[i] + filterSize > srcW)
1275 int shift= (*filterPos)[i] + filterSize - srcW;
1276 // Move filter coeffs right to compensate for filterPos
1277 for (j=filterSize-2; j>=0; j--)
1279 int right= FFMIN(j + shift, filterSize-1);
1280 filter[i*filterSize +right] += filter[i*filterSize +j];
1281 filter[i*filterSize +j]=0;
1283 (*filterPos)[i]= srcW - filterSize;
1287 // Note the +1 is for the MMXscaler which reads over the end
1288 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
1289 *outFilter= av_mallocz(*outFilterSize*(dstW+1)*sizeof(int16_t));
1291 /* Normalize & Store in outFilter */
1292 for (i=0; i<dstW; i++)
1299 for (j=0; j<filterSize; j++)
1301 sum+= filter[i*filterSize + j];
1304 for (j=0; j<*outFilterSize; j++)
1306 double v= filter[i*filterSize + j]*scale + error;
1307 int intV= floor(v + 0.5);
1308 (*outFilter)[i*(*outFilterSize) + j]= intV;
1313 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
1314 for (i=0; i<*outFilterSize; i++)
1316 int j= dstW*(*outFilterSize);
1317 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
1325 static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
1328 long imm8OfPShufW1A;
1329 long imm8OfPShufW2A;
1330 long fragmentLengthA;
1332 long imm8OfPShufW1B;
1333 long imm8OfPShufW2B;
1334 long fragmentLengthB;
1339 // create an optimized horizontal scaling routine
1347 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1348 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1349 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
1350 "punpcklbw %%mm7, %%mm1 \n\t"
1351 "punpcklbw %%mm7, %%mm0 \n\t"
1352 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
1354 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1356 "psubw %%mm1, %%mm0 \n\t"
1357 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1358 "pmullw %%mm3, %%mm0 \n\t"
1359 "psllw $7, %%mm1 \n\t"
1360 "paddw %%mm1, %%mm0 \n\t"
1362 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1364 "add $8, %%"REG_a" \n\t"
1379 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
1380 "=r" (fragmentLengthA)
1387 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
1388 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
1389 "punpcklbw %%mm7, %%mm0 \n\t"
1390 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
1392 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
1394 "psubw %%mm1, %%mm0 \n\t"
1395 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
1396 "pmullw %%mm3, %%mm0 \n\t"
1397 "psllw $7, %%mm1 \n\t"
1398 "paddw %%mm1, %%mm0 \n\t"
1400 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
1402 "add $8, %%"REG_a" \n\t"
1417 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
1418 "=r" (fragmentLengthB)
1421 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
1424 for (i=0; i<dstW/numSplits; i++)
1431 int b=((xpos+xInc)>>16) - xx;
1432 int c=((xpos+xInc*2)>>16) - xx;
1433 int d=((xpos+xInc*3)>>16) - xx;
1435 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
1436 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
1437 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
1438 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
1443 int maxShift= 3-(d+1);
1446 memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
1448 funnyCode[fragmentPos + imm8OfPShufW1B]=
1449 (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
1450 funnyCode[fragmentPos + imm8OfPShufW2B]=
1451 a | (b<<2) | (c<<4) | (d<<6);
1453 if (i+3>=dstW) shift=maxShift; //avoid overread
1454 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
1456 if (shift && i>=shift)
1458 funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
1459 funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
1460 filterPos[i/2]-=shift;
1463 fragmentPos+= fragmentLengthB;
1470 memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
1472 funnyCode[fragmentPos + imm8OfPShufW1A]=
1473 funnyCode[fragmentPos + imm8OfPShufW2A]=
1474 a | (b<<2) | (c<<4) | (d<<6);
1476 if (i+4>=dstW) shift=maxShift; //avoid overread
1477 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
1479 if (shift && i>=shift)
1481 funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
1482 funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
1483 filterPos[i/2]-=shift;
1486 fragmentPos+= fragmentLengthA;
1489 funnyCode[fragmentPos]= RET;
1493 filterPos[i/2]= xpos>>16; // needed to jump to the next part
1495 #endif /* COMPILE_MMX2 */
1497 static void globalInit(void){
1498 // generating tables:
1500 for (i=0; i<768; i++){
1501 int c= av_clip_uint8(i-256);
1506 static SwsFunc getSwsFunc(int flags){
1508 #if defined(RUNTIME_CPUDETECT) && defined (CONFIG_GPL)
1509 #if defined(ARCH_X86)
1510 // ordered per speed fasterst first
1511 if (flags & SWS_CPU_CAPS_MMX2)
1512 return swScale_MMX2;
1513 else if (flags & SWS_CPU_CAPS_3DNOW)
1514 return swScale_3DNow;
1515 else if (flags & SWS_CPU_CAPS_MMX)
1522 if (flags & SWS_CPU_CAPS_ALTIVEC)
1523 return swScale_altivec;
1528 #endif /* defined(ARCH_X86) */
1529 #else //RUNTIME_CPUDETECT
1531 return swScale_MMX2;
1532 #elif defined (HAVE_3DNOW)
1533 return swScale_3DNow;
1534 #elif defined (HAVE_MMX)
1536 #elif defined (HAVE_ALTIVEC)
1537 return swScale_altivec;
1541 #endif //!RUNTIME_CPUDETECT
1544 static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1545 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1546 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1548 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1549 memcpy(dst, src[0], srcSliceH*dstStride[0]);
1553 uint8_t *srcPtr= src[0];
1554 uint8_t *dstPtr= dst;
1555 for (i=0; i<srcSliceH; i++)
1557 memcpy(dstPtr, srcPtr, c->srcW);
1558 srcPtr+= srcStride[0];
1559 dstPtr+= dstStride[0];
1562 dst = dstParam[1] + dstStride[1]*srcSliceY/2;
1563 if (c->dstFormat == PIX_FMT_NV12)
1564 interleaveBytes( src[1],src[2],dst,c->srcW/2,srcSliceH/2,srcStride[1],srcStride[2],dstStride[0] );
1566 interleaveBytes( src[2],src[1],dst,c->srcW/2,srcSliceH/2,srcStride[2],srcStride[1],dstStride[0] );
1571 static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1572 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1573 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1575 yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1580 static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1581 int srcSliceH, uint8_t* dstParam[], int dstStride[]){
1582 uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
1584 yv12touyvy( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
1589 /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
1590 static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1591 int srcSliceH, uint8_t* dst[], int dstStride[]){
1592 const int srcFormat= c->srcFormat;
1593 const int dstFormat= c->dstFormat;
1594 const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
1595 const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
1596 const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
1597 const int dstId= fmt_depth(dstFormat) >> 2;
1598 void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
1601 if ( (isBGR(srcFormat) && isBGR(dstFormat))
1602 || (isRGB(srcFormat) && isRGB(dstFormat))){
1603 switch(srcId | (dstId<<4)){
1604 case 0x34: conv= rgb16to15; break;
1605 case 0x36: conv= rgb24to15; break;
1606 case 0x38: conv= rgb32to15; break;
1607 case 0x43: conv= rgb15to16; break;
1608 case 0x46: conv= rgb24to16; break;
1609 case 0x48: conv= rgb32to16; break;
1610 case 0x63: conv= rgb15to24; break;
1611 case 0x64: conv= rgb16to24; break;
1612 case 0x68: conv= rgb32to24; break;
1613 case 0x83: conv= rgb15to32; break;
1614 case 0x84: conv= rgb16to32; break;
1615 case 0x86: conv= rgb24to32; break;
1616 default: av_log(c, AV_LOG_ERROR, "swScaler: internal error %s -> %s converter\n",
1617 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1619 }else if ( (isBGR(srcFormat) && isRGB(dstFormat))
1620 || (isRGB(srcFormat) && isBGR(dstFormat))){
1621 switch(srcId | (dstId<<4)){
1622 case 0x33: conv= rgb15tobgr15; break;
1623 case 0x34: conv= rgb16tobgr15; break;
1624 case 0x36: conv= rgb24tobgr15; break;
1625 case 0x38: conv= rgb32tobgr15; break;
1626 case 0x43: conv= rgb15tobgr16; break;
1627 case 0x44: conv= rgb16tobgr16; break;
1628 case 0x46: conv= rgb24tobgr16; break;
1629 case 0x48: conv= rgb32tobgr16; break;
1630 case 0x63: conv= rgb15tobgr24; break;
1631 case 0x64: conv= rgb16tobgr24; break;
1632 case 0x66: conv= rgb24tobgr24; break;
1633 case 0x68: conv= rgb32tobgr24; break;
1634 case 0x83: conv= rgb15tobgr32; break;
1635 case 0x84: conv= rgb16tobgr32; break;
1636 case 0x86: conv= rgb24tobgr32; break;
1637 case 0x88: conv= rgb32tobgr32; break;
1638 default: av_log(c, AV_LOG_ERROR, "swScaler: internal error %s -> %s converter\n",
1639 sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
1642 av_log(c, AV_LOG_ERROR, "swScaler: internal error %s -> %s converter\n",
1643 sws_format_name(srcFormat), sws_format_name(dstFormat));
1648 if (dstStride[0]*srcBpp == srcStride[0]*dstBpp)
1649 conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
1653 uint8_t *srcPtr= src[0];
1654 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1656 for (i=0; i<srcSliceH; i++)
1658 conv(srcPtr, dstPtr, c->srcW*srcBpp);
1659 srcPtr+= srcStride[0];
1660 dstPtr+= dstStride[0];
1667 static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1668 int srcSliceH, uint8_t* dst[], int dstStride[]){
1672 dst[0]+ srcSliceY *dstStride[0],
1673 dst[1]+(srcSliceY>>1)*dstStride[1],
1674 dst[2]+(srcSliceY>>1)*dstStride[2],
1676 dstStride[0], dstStride[1], srcStride[0]);
1680 static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1681 int srcSliceH, uint8_t* dst[], int dstStride[]){
1685 if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
1686 memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
1688 uint8_t *srcPtr= src[0];
1689 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1691 for (i=0; i<srcSliceH; i++)
1693 memcpy(dstPtr, srcPtr, c->srcW);
1694 srcPtr+= srcStride[0];
1695 dstPtr+= dstStride[0];
1699 if (c->dstFormat==PIX_FMT_YUV420P){
1700 planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
1701 planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
1703 planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
1704 planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
1709 /* unscaled copy like stuff (assumes nearly identical formats) */
1710 static int simpleCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1711 int srcSliceH, uint8_t* dst[], int dstStride[]){
1713 if (isPacked(c->srcFormat))
1715 if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
1716 memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
1720 uint8_t *srcPtr= src[0];
1721 uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
1724 /* universal length finder */
1725 while(length+c->srcW <= FFABS(dstStride[0])
1726 && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
1729 for (i=0; i<srcSliceH; i++)
1731 memcpy(dstPtr, srcPtr, length);
1732 srcPtr+= srcStride[0];
1733 dstPtr+= dstStride[0];
1738 { /* Planar YUV or gray */
1740 for (plane=0; plane<3; plane++)
1742 int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
1743 int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
1744 int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
1746 if ((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
1748 if (!isGray(c->dstFormat))
1749 memset(dst[plane], 128, dstStride[plane]*height);
1753 if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
1754 memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
1758 uint8_t *srcPtr= src[plane];
1759 uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
1760 for (i=0; i<height; i++)
1762 memcpy(dstPtr, srcPtr, length);
1763 srcPtr+= srcStride[plane];
1764 dstPtr+= dstStride[plane];
1773 static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1774 int srcSliceH, uint8_t* dst[], int dstStride[]){
1776 int length= c->srcW;
1778 int height= srcSliceH;
1780 uint8_t *srcPtr= src[0];
1781 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1783 if (!isGray(c->dstFormat)){
1784 int height= -((-srcSliceH)>>c->chrDstVSubSample);
1785 memset(dst[1], 128, dstStride[1]*height);
1786 memset(dst[2], 128, dstStride[2]*height);
1788 if (c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
1789 for (i=0; i<height; i++)
1791 for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
1792 srcPtr+= srcStride[0];
1793 dstPtr+= dstStride[0];
1798 static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1799 int srcSliceH, uint8_t* dst[], int dstStride[]){
1801 int length= c->srcW;
1803 int height= srcSliceH;
1805 uint8_t *srcPtr= src[0];
1806 uint8_t *dstPtr= dst[0] + dstStride[0]*y;
1807 for (i=0; i<height; i++)
1809 for (j=0; j<length; j++)
1811 dstPtr[j<<1] = srcPtr[j];
1812 dstPtr[(j<<1)+1] = srcPtr[j];
1814 srcPtr+= srcStride[0];
1815 dstPtr+= dstStride[0];
1820 static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
1821 int srcSliceH, uint8_t* dst[], int dstStride[]){
1823 int length= c->srcW;
1825 int height= srcSliceH;
1827 uint16_t *srcPtr= src[0];
1828 uint16_t *dstPtr= dst[0] + dstStride[0]*y/2;
1829 for (i=0; i<height; i++)
1831 for (j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
1832 srcPtr+= srcStride[0]/2;
1833 dstPtr+= dstStride[0]/2;
1839 static void getSubSampleFactors(int *h, int *v, int format){
1841 case PIX_FMT_UYVY422:
1842 case PIX_FMT_YUYV422:
1846 case PIX_FMT_YUV420P:
1847 case PIX_FMT_GRAY16BE:
1848 case PIX_FMT_GRAY16LE:
1849 case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
1855 case PIX_FMT_YUV410P:
1859 case PIX_FMT_YUV444P:
1863 case PIX_FMT_YUV422P:
1867 case PIX_FMT_YUV411P:
1878 static uint16_t roundToInt16(int64_t f){
1879 int r= (f + (1<<15))>>16;
1880 if (r<-0x7FFF) return 0x8000;
1881 else if (r> 0x7FFF) return 0x7FFF;
1886 * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
1887 * @param fullRange if 1 then the luma range is 0..255 if 0 its 16..235
1888 * @return -1 if not supported
1890 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
1891 int64_t crv = inv_table[0];
1892 int64_t cbu = inv_table[1];
1893 int64_t cgu = -inv_table[2];
1894 int64_t cgv = -inv_table[3];
1898 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1899 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
1900 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
1902 c->brightness= brightness;
1903 c->contrast = contrast;
1904 c->saturation= saturation;
1905 c->srcRange = srcRange;
1906 c->dstRange = dstRange;
1908 c->uOffset= 0x0400040004000400LL;
1909 c->vOffset= 0x0400040004000400LL;
1915 crv= (crv*224) / 255;
1916 cbu= (cbu*224) / 255;
1917 cgu= (cgu*224) / 255;
1918 cgv= (cgv*224) / 255;
1921 cy = (cy *contrast )>>16;
1922 crv= (crv*contrast * saturation)>>32;
1923 cbu= (cbu*contrast * saturation)>>32;
1924 cgu= (cgu*contrast * saturation)>>32;
1925 cgv= (cgv*contrast * saturation)>>32;
1927 oy -= 256*brightness;
1929 c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
1930 c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
1931 c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
1932 c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
1933 c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
1934 c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
1936 yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
1939 #ifdef COMPILE_ALTIVEC
1940 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
1941 yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
1947 * @return -1 if not supported
1949 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
1950 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
1952 *inv_table = c->srcColorspaceTable;
1953 *table = c->dstColorspaceTable;
1954 *srcRange = c->srcRange;
1955 *dstRange = c->dstRange;
1956 *brightness= c->brightness;
1957 *contrast = c->contrast;
1958 *saturation= c->saturation;
1963 static int handle_jpeg(int *format)
1966 case PIX_FMT_YUVJ420P:
1967 *format = PIX_FMT_YUV420P;
1969 case PIX_FMT_YUVJ422P:
1970 *format = PIX_FMT_YUV422P;
1972 case PIX_FMT_YUVJ444P:
1973 *format = PIX_FMT_YUV444P;
1980 SwsContext *sws_getContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
1981 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
1985 int usesVFilter, usesHFilter;
1986 int unscaled, needsDither;
1987 int srcRange, dstRange;
1988 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
1989 #if defined(ARCH_X86)
1990 if (flags & SWS_CPU_CAPS_MMX)
1991 asm volatile("emms\n\t"::: "memory");
1994 #if !defined(RUNTIME_CPUDETECT) || !defined (CONFIG_GPL) //ensure that the flags match the compiled variant if cpudetect is off
1995 flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC|SWS_CPU_CAPS_BFIN);
1997 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
1998 #elif defined (HAVE_3DNOW)
1999 flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
2000 #elif defined (HAVE_MMX)
2001 flags |= SWS_CPU_CAPS_MMX;
2002 #elif defined (HAVE_ALTIVEC)
2003 flags |= SWS_CPU_CAPS_ALTIVEC;
2004 #elif defined (ARCH_BFIN)
2005 flags |= SWS_CPU_CAPS_BFIN;
2007 #endif /* RUNTIME_CPUDETECT */
2008 if (clip_table[512] != 255) globalInit();
2009 if (rgb15to16 == NULL) sws_rgb2rgb_init(flags);
2011 unscaled = (srcW == dstW && srcH == dstH);
2012 needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
2013 && (fmt_depth(dstFormat))<24
2014 && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
2016 srcRange = handle_jpeg(&srcFormat);
2017 dstRange = handle_jpeg(&dstFormat);
2019 if (!isSupportedIn(srcFormat))
2021 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input format\n", sws_format_name(srcFormat));
2024 if (!isSupportedOut(dstFormat))
2026 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output format\n", sws_format_name(dstFormat));
2031 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
2033 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
2034 srcW, srcH, dstW, dstH);
2038 if (!dstFilter) dstFilter= &dummyFilter;
2039 if (!srcFilter) srcFilter= &dummyFilter;
2041 c= av_mallocz(sizeof(SwsContext));
2043 c->av_class = &sws_context_class;
2048 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
2049 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
2051 c->dstFormat= dstFormat;
2052 c->srcFormat= srcFormat;
2053 c->vRounder= 4* 0x0001000100010001ULL;
2055 usesHFilter= usesVFilter= 0;
2056 if (dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesVFilter=1;
2057 if (dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesHFilter=1;
2058 if (dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesVFilter=1;
2059 if (dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesHFilter=1;
2060 if (srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesVFilter=1;
2061 if (srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesHFilter=1;
2062 if (srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesVFilter=1;
2063 if (srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesHFilter=1;
2065 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
2066 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
2068 // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
2069 if ((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
2071 // drop some chroma lines if the user wants it
2072 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
2073 c->chrSrcVSubSample+= c->vChrDrop;
2075 // drop every 2. pixel for chroma calculation unless user wants full chroma
2076 if ((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP)
2077 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
2078 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
2079 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE)
2080 c->chrSrcHSubSample=1;
2083 c->param[0] = param[0];
2084 c->param[1] = param[1];
2087 c->param[1] = SWS_PARAM_DEFAULT;
2090 c->chrIntHSubSample= c->chrDstHSubSample;
2091 c->chrIntVSubSample= c->chrSrcVSubSample;
2093 // Note the -((-x)>>y) is so that we always round toward +inf.
2094 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
2095 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
2096 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
2097 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
2099 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);
2101 /* unscaled special Cases */
2102 if (unscaled && !usesHFilter && !usesVFilter)
2105 if (srcFormat == PIX_FMT_YUV420P && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
2107 c->swScale= PlanarToNV12Wrapper;
2111 if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
2113 c->swScale= yuv2rgb_get_func_ptr(c);
2117 if ( srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P )
2119 c->swScale= yvu9toyv12Wrapper;
2123 if (srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P)
2124 c->swScale= bgr24toyv12Wrapper;
2126 /* rgb/bgr -> rgb/bgr (no dither needed forms) */
2127 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2128 && (isBGR(dstFormat) || isRGB(dstFormat))
2129 && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
2130 && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
2131 && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
2132 && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
2133 && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
2134 && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
2135 && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
2137 c->swScale= rgb2rgbWrapper;
2139 /* LQ converters if -sws 0 or -sws 4*/
2140 if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
2141 /* rgb/bgr -> rgb/bgr (dither needed forms) */
2142 if ( (isBGR(srcFormat) || isRGB(srcFormat))
2143 && (isBGR(dstFormat) || isRGB(dstFormat))
2145 c->swScale= rgb2rgbWrapper;
2148 if (srcFormat == PIX_FMT_YUV420P &&
2149 (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422))
2151 if (dstFormat == PIX_FMT_YUYV422)
2152 c->swScale= PlanarToYuy2Wrapper;
2154 c->swScale= PlanarToUyvyWrapper;
2158 #ifdef COMPILE_ALTIVEC
2159 if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
2160 ((srcFormat == PIX_FMT_YUV420P &&
2161 (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422)))) {
2162 // unscaled YV12 -> packed YUV, we want speed
2163 if (dstFormat == PIX_FMT_YUYV422)
2164 c->swScale= yv12toyuy2_unscaled_altivec;
2166 c->swScale= yv12touyvy_unscaled_altivec;
2171 if ( srcFormat == dstFormat
2172 || (isPlanarYUV(srcFormat) && isGray(dstFormat))
2173 || (isPlanarYUV(dstFormat) && isGray(srcFormat)) )
2175 c->swScale= simpleCopy;
2178 /* gray16{le,be} conversions */
2179 if (isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
2181 c->swScale= gray16togray;
2183 if ((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
2185 c->swScale= graytogray16;
2187 if (srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
2189 c->swScale= gray16swap;
2193 if (flags&SWS_PRINT_INFO)
2194 av_log(c, AV_LOG_INFO, "SwScaler: using unscaled %s -> %s special converter\n",
2195 sws_format_name(srcFormat), sws_format_name(dstFormat));
2200 if (flags & SWS_CPU_CAPS_MMX2)
2202 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
2203 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
2205 if (flags&SWS_PRINT_INFO)
2206 av_log(c, AV_LOG_INFO, "SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
2208 if (usesHFilter) c->canMMX2BeUsed=0;
2213 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
2214 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
2216 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
2217 // but only for the FAST_BILINEAR mode otherwise do correct scaling
2218 // n-2 is the last chrominance sample available
2219 // this is not perfect, but noone shuld notice the difference, the more correct variant
2220 // would be like the vertical one, but that would require some special code for the
2221 // first and last pixel
2222 if (flags&SWS_FAST_BILINEAR)
2224 if (c->canMMX2BeUsed)
2229 //we don't use the x86asm scaler if mmx is available
2230 else if (flags & SWS_CPU_CAPS_MMX)
2232 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
2233 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
2237 /* precalculate horizontal scaler filter coefficients */
2239 const int filterAlign=
2240 (flags & SWS_CPU_CAPS_MMX) ? 4 :
2241 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2244 initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
2245 srcW , dstW, filterAlign, 1<<14,
2246 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2247 srcFilter->lumH, dstFilter->lumH, c->param);
2248 initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
2249 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
2250 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2251 srcFilter->chrH, dstFilter->chrH, c->param);
2253 #define MAX_FUNNY_CODE_SIZE 10000
2254 #if defined(COMPILE_MMX2)
2255 // can't downscale !!!
2256 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
2258 #ifdef MAP_ANONYMOUS
2259 c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2260 c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
2262 c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2263 c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
2266 c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
2267 c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
2268 c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
2269 c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
2271 initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
2272 initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
2274 #endif /* defined(COMPILE_MMX2) */
2275 } // Init Horizontal stuff
2279 /* precalculate vertical scaler filter coefficients */
2281 const int filterAlign=
2282 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
2283 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
2286 initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
2287 srcH , dstH, filterAlign, (1<<12)-4,
2288 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
2289 srcFilter->lumV, dstFilter->lumV, c->param);
2290 initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
2291 c->chrSrcH, c->chrDstH, filterAlign, (1<<12)-4,
2292 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
2293 srcFilter->chrV, dstFilter->chrV, c->param);
2296 c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
2297 c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
2299 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
2301 short *p = (short *)&c->vYCoeffsBank[i];
2303 p[j] = c->vLumFilter[i];
2306 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
2308 short *p = (short *)&c->vCCoeffsBank[i];
2310 p[j] = c->vChrFilter[i];
2315 // Calculate Buffer Sizes so that they won't run out while handling these damn slices
2316 c->vLumBufSize= c->vLumFilterSize;
2317 c->vChrBufSize= c->vChrFilterSize;
2318 for (i=0; i<dstH; i++)
2320 int chrI= i*c->chrDstH / dstH;
2321 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
2322 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
2324 nextSlice>>= c->chrSrcVSubSample;
2325 nextSlice<<= c->chrSrcVSubSample;
2326 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
2327 c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
2328 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
2329 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
2332 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
2333 c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
2334 c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
2335 //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)
2336 /* align at 16 bytes for AltiVec */
2337 for (i=0; i<c->vLumBufSize; i++)
2338 c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_mallocz(4000);
2339 for (i=0; i<c->vChrBufSize; i++)
2340 c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc(8000);
2342 //try to avoid drawing green stuff between the right end and the stride end
2343 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
2345 ASSERT(c->chrDstH <= dstH)
2347 if (flags&SWS_PRINT_INFO)
2350 char *dither= " dithered";
2354 if (flags&SWS_FAST_BILINEAR)
2355 av_log(c, AV_LOG_INFO, "SwScaler: FAST_BILINEAR scaler, ");
2356 else if (flags&SWS_BILINEAR)
2357 av_log(c, AV_LOG_INFO, "SwScaler: BILINEAR scaler, ");
2358 else if (flags&SWS_BICUBIC)
2359 av_log(c, AV_LOG_INFO, "SwScaler: BICUBIC scaler, ");
2360 else if (flags&SWS_X)
2361 av_log(c, AV_LOG_INFO, "SwScaler: Experimental scaler, ");
2362 else if (flags&SWS_POINT)
2363 av_log(c, AV_LOG_INFO, "SwScaler: Nearest Neighbor / POINT scaler, ");
2364 else if (flags&SWS_AREA)
2365 av_log(c, AV_LOG_INFO, "SwScaler: Area Averageing scaler, ");
2366 else if (flags&SWS_BICUBLIN)
2367 av_log(c, AV_LOG_INFO, "SwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
2368 else if (flags&SWS_GAUSS)
2369 av_log(c, AV_LOG_INFO, "SwScaler: Gaussian scaler, ");
2370 else if (flags&SWS_SINC)
2371 av_log(c, AV_LOG_INFO, "SwScaler: Sinc scaler, ");
2372 else if (flags&SWS_LANCZOS)
2373 av_log(c, AV_LOG_INFO, "SwScaler: Lanczos scaler, ");
2374 else if (flags&SWS_SPLINE)
2375 av_log(c, AV_LOG_INFO, "SwScaler: Bicubic spline scaler, ");
2377 av_log(c, AV_LOG_INFO, "SwScaler: ehh flags invalid?! ");
2379 if (dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
2380 av_log(c, AV_LOG_INFO, "from %s to%s %s ",
2381 sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
2383 av_log(c, AV_LOG_INFO, "from %s to %s ",
2384 sws_format_name(srcFormat), sws_format_name(dstFormat));
2386 if (flags & SWS_CPU_CAPS_MMX2)
2387 av_log(c, AV_LOG_INFO, "using MMX2\n");
2388 else if (flags & SWS_CPU_CAPS_3DNOW)
2389 av_log(c, AV_LOG_INFO, "using 3DNOW\n");
2390 else if (flags & SWS_CPU_CAPS_MMX)
2391 av_log(c, AV_LOG_INFO, "using MMX\n");
2392 else if (flags & SWS_CPU_CAPS_ALTIVEC)
2393 av_log(c, AV_LOG_INFO, "using AltiVec\n");
2395 av_log(c, AV_LOG_INFO, "using C\n");
2398 if (flags & SWS_PRINT_INFO)
2400 if (flags & SWS_CPU_CAPS_MMX)
2402 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
2403 av_log(c, AV_LOG_VERBOSE, "SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
2406 if (c->hLumFilterSize==4)
2407 av_log(c, AV_LOG_VERBOSE, "SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
2408 else if (c->hLumFilterSize==8)
2409 av_log(c, AV_LOG_VERBOSE, "SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
2411 av_log(c, AV_LOG_VERBOSE, "SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
2413 if (c->hChrFilterSize==4)
2414 av_log(c, AV_LOG_VERBOSE, "SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
2415 else if (c->hChrFilterSize==8)
2416 av_log(c, AV_LOG_VERBOSE, "SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
2418 av_log(c, AV_LOG_VERBOSE, "SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
2423 #if defined(ARCH_X86)
2424 av_log(c, AV_LOG_VERBOSE, "SwScaler: using X86-Asm scaler for horizontal scaling\n");
2426 if (flags & SWS_FAST_BILINEAR)
2427 av_log(c, AV_LOG_VERBOSE, "SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
2429 av_log(c, AV_LOG_VERBOSE, "SwScaler: using C scaler for horizontal scaling\n");
2432 if (isPlanarYUV(dstFormat))
2434 if (c->vLumFilterSize==1)
2435 av_log(c, AV_LOG_VERBOSE, "SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2437 av_log(c, AV_LOG_VERBOSE, "SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2441 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
2442 av_log(c, AV_LOG_VERBOSE, "SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
2443 "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",(flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2444 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
2445 av_log(c, AV_LOG_VERBOSE, "SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2447 av_log(c, AV_LOG_VERBOSE, "SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2450 if (dstFormat==PIX_FMT_BGR24)
2451 av_log(c, AV_LOG_VERBOSE, "SwScaler: using %s YV12->BGR24 Converter\n",
2452 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
2453 else if (dstFormat==PIX_FMT_RGB32)
2454 av_log(c, AV_LOG_VERBOSE, "SwScaler: using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2455 else if (dstFormat==PIX_FMT_BGR565)
2456 av_log(c, AV_LOG_VERBOSE, "SwScaler: using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2457 else if (dstFormat==PIX_FMT_BGR555)
2458 av_log(c, AV_LOG_VERBOSE, "SwScaler: using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
2460 av_log(c, AV_LOG_VERBOSE, "SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
2462 if (flags & SWS_PRINT_INFO)
2464 av_log(c, AV_LOG_DEBUG, "SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2465 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
2466 av_log(c, AV_LOG_DEBUG, "SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
2467 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
2470 c->swScale= getSwsFunc(flags);
2475 * swscale warper, so we don't need to export the SwsContext.
2476 * assumes planar YUV to be in YUV order instead of YVU
2478 int sws_scale(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2479 int srcSliceH, uint8_t* dst[], int dstStride[]){
2481 uint8_t* src2[4]= {src[0], src[1], src[2]};
2483 if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
2484 av_log(c, AV_LOG_ERROR, "swScaler: slices start in the middle!\n");
2487 if (c->sliceDir == 0) {
2488 if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
2491 if (c->srcFormat == PIX_FMT_PAL8){
2492 for (i=0; i<256; i++){
2493 int p= ((uint32_t*)(src[1]))[i];
2494 int r= (p>>16)&0xFF;
2495 int g= (p>> 8)&0xFF;
2497 int y= av_clip_uint8(((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16 );
2498 int u= av_clip_uint8(((RU*r + GU*g + BU*b)>>RGB2YUV_SHIFT) + 128);
2499 int v= av_clip_uint8(((RV*r + GV*g + BV*b)>>RGB2YUV_SHIFT) + 128);
2500 pal[i]= y + (u<<8) + (v<<16);
2505 // copy strides, so they can safely be modified
2506 if (c->sliceDir == 1) {
2507 // slices go from top to bottom
2508 int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2]};
2509 int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2]};
2510 return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
2512 // slices go from bottom to top => we flip the image internally
2513 uint8_t* dst2[4]= {dst[0] + (c->dstH-1)*dstStride[0],
2514 dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
2515 dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
2516 int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2]};
2517 int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2]};
2519 src2[0] += (srcSliceH-1)*srcStride[0];
2520 if (c->srcFormat != PIX_FMT_PAL8)
2521 src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
2522 src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
2524 return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
2529 * swscale warper, so we don't need to export the SwsContext
2531 int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
2532 int srcSliceH, uint8_t* dst[], int dstStride[]){
2533 return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
2536 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
2537 float lumaSharpen, float chromaSharpen,
2538 float chromaHShift, float chromaVShift,
2541 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
2543 if (lumaGBlur!=0.0){
2544 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
2545 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
2547 filter->lumH= sws_getIdentityVec();
2548 filter->lumV= sws_getIdentityVec();
2551 if (chromaGBlur!=0.0){
2552 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
2553 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
2555 filter->chrH= sws_getIdentityVec();
2556 filter->chrV= sws_getIdentityVec();
2559 if (chromaSharpen!=0.0){
2560 SwsVector *id= sws_getIdentityVec();
2561 sws_scaleVec(filter->chrH, -chromaSharpen);
2562 sws_scaleVec(filter->chrV, -chromaSharpen);
2563 sws_addVec(filter->chrH, id);
2564 sws_addVec(filter->chrV, id);
2568 if (lumaSharpen!=0.0){
2569 SwsVector *id= sws_getIdentityVec();
2570 sws_scaleVec(filter->lumH, -lumaSharpen);
2571 sws_scaleVec(filter->lumV, -lumaSharpen);
2572 sws_addVec(filter->lumH, id);
2573 sws_addVec(filter->lumV, id);
2577 if (chromaHShift != 0.0)
2578 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
2580 if (chromaVShift != 0.0)
2581 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
2583 sws_normalizeVec(filter->chrH, 1.0);
2584 sws_normalizeVec(filter->chrV, 1.0);
2585 sws_normalizeVec(filter->lumH, 1.0);
2586 sws_normalizeVec(filter->lumV, 1.0);
2588 if (verbose) sws_printVec(filter->chrH);
2589 if (verbose) sws_printVec(filter->lumH);
2595 * returns a normalized gaussian curve used to filter stuff
2596 * quality=3 is high quality, lowwer is lowwer quality
2598 SwsVector *sws_getGaussianVec(double variance, double quality){
2599 const int length= (int)(variance*quality + 0.5) | 1;
2601 double *coeff= av_malloc(length*sizeof(double));
2602 double middle= (length-1)*0.5;
2603 SwsVector *vec= av_malloc(sizeof(SwsVector));
2606 vec->length= length;
2608 for (i=0; i<length; i++)
2610 double dist= i-middle;
2611 coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
2614 sws_normalizeVec(vec, 1.0);
2619 SwsVector *sws_getConstVec(double c, int length){
2621 double *coeff= av_malloc(length*sizeof(double));
2622 SwsVector *vec= av_malloc(sizeof(SwsVector));
2625 vec->length= length;
2627 for (i=0; i<length; i++)
2634 SwsVector *sws_getIdentityVec(void){
2635 return sws_getConstVec(1.0, 1);
2638 double sws_dcVec(SwsVector *a){
2642 for (i=0; i<a->length; i++)
2648 void sws_scaleVec(SwsVector *a, double scalar){
2651 for (i=0; i<a->length; i++)
2652 a->coeff[i]*= scalar;
2655 void sws_normalizeVec(SwsVector *a, double height){
2656 sws_scaleVec(a, height/sws_dcVec(a));
2659 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
2660 int length= a->length + b->length - 1;
2661 double *coeff= av_malloc(length*sizeof(double));
2663 SwsVector *vec= av_malloc(sizeof(SwsVector));
2666 vec->length= length;
2668 for (i=0; i<length; i++) coeff[i]= 0.0;
2670 for (i=0; i<a->length; i++)
2672 for (j=0; j<b->length; j++)
2674 coeff[i+j]+= a->coeff[i]*b->coeff[j];
2681 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
2682 int length= FFMAX(a->length, b->length);
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++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2693 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
2698 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
2699 int length= FFMAX(a->length, b->length);
2700 double *coeff= av_malloc(length*sizeof(double));
2702 SwsVector *vec= av_malloc(sizeof(SwsVector));
2705 vec->length= length;
2707 for (i=0; i<length; i++) coeff[i]= 0.0;
2709 for (i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
2710 for (i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
2715 /* shift left / or right if "shift" is negative */
2716 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
2717 int length= a->length + FFABS(shift)*2;
2718 double *coeff= av_malloc(length*sizeof(double));
2720 SwsVector *vec= av_malloc(sizeof(SwsVector));
2723 vec->length= length;
2725 for (i=0; i<length; i++) coeff[i]= 0.0;
2727 for (i=0; i<a->length; i++)
2729 coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
2735 void sws_shiftVec(SwsVector *a, int shift){
2736 SwsVector *shifted= sws_getShiftedVec(a, shift);
2738 a->coeff= shifted->coeff;
2739 a->length= shifted->length;
2743 void sws_addVec(SwsVector *a, SwsVector *b){
2744 SwsVector *sum= sws_sumVec(a, b);
2746 a->coeff= sum->coeff;
2747 a->length= sum->length;
2751 void sws_subVec(SwsVector *a, SwsVector *b){
2752 SwsVector *diff= sws_diffVec(a, b);
2754 a->coeff= diff->coeff;
2755 a->length= diff->length;
2759 void sws_convVec(SwsVector *a, SwsVector *b){
2760 SwsVector *conv= sws_getConvVec(a, b);
2762 a->coeff= conv->coeff;
2763 a->length= conv->length;
2767 SwsVector *sws_cloneVec(SwsVector *a){
2768 double *coeff= av_malloc(a->length*sizeof(double));
2770 SwsVector *vec= av_malloc(sizeof(SwsVector));
2773 vec->length= a->length;
2775 for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
2780 void sws_printVec(SwsVector *a){
2786 for (i=0; i<a->length; i++)
2787 if (a->coeff[i]>max) max= a->coeff[i];
2789 for (i=0; i<a->length; i++)
2790 if (a->coeff[i]<min) min= a->coeff[i];
2794 for (i=0; i<a->length; i++)
2796 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
2797 av_log(NULL, AV_LOG_DEBUG, "%1.3f ", a->coeff[i]);
2798 for (;x>0; x--) av_log(NULL, AV_LOG_DEBUG, " ");
2799 av_log(NULL, AV_LOG_DEBUG, "|\n");
2803 void sws_freeVec(SwsVector *a){
2811 void sws_freeFilter(SwsFilter *filter){
2812 if (!filter) return;
2814 if (filter->lumH) sws_freeVec(filter->lumH);
2815 if (filter->lumV) sws_freeVec(filter->lumV);
2816 if (filter->chrH) sws_freeVec(filter->chrH);
2817 if (filter->chrV) sws_freeVec(filter->chrV);
2822 void sws_freeContext(SwsContext *c){
2828 for (i=0; i<c->vLumBufSize; i++)
2830 av_free(c->lumPixBuf[i]);
2831 c->lumPixBuf[i]=NULL;
2833 av_free(c->lumPixBuf);
2839 for (i=0; i<c->vChrBufSize; i++)
2841 av_free(c->chrPixBuf[i]);
2842 c->chrPixBuf[i]=NULL;
2844 av_free(c->chrPixBuf);
2848 av_free(c->vLumFilter);
2849 c->vLumFilter = NULL;
2850 av_free(c->vChrFilter);
2851 c->vChrFilter = NULL;
2852 av_free(c->hLumFilter);
2853 c->hLumFilter = NULL;
2854 av_free(c->hChrFilter);
2855 c->hChrFilter = NULL;
2857 av_free(c->vYCoeffsBank);
2858 c->vYCoeffsBank = NULL;
2859 av_free(c->vCCoeffsBank);
2860 c->vCCoeffsBank = NULL;
2863 av_free(c->vLumFilterPos);
2864 c->vLumFilterPos = NULL;
2865 av_free(c->vChrFilterPos);
2866 c->vChrFilterPos = NULL;
2867 av_free(c->hLumFilterPos);
2868 c->hLumFilterPos = NULL;
2869 av_free(c->hChrFilterPos);
2870 c->hChrFilterPos = NULL;
2872 #if defined(ARCH_X86) && defined(CONFIG_GPL)
2873 #ifdef MAP_ANONYMOUS
2874 if (c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
2875 if (c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
2877 av_free(c->funnyYCode);
2878 av_free(c->funnyUVCode);
2881 c->funnyUVCode=NULL;
2882 #endif /* defined(ARCH_X86) */
2884 av_free(c->lumMmx2Filter);
2885 c->lumMmx2Filter=NULL;
2886 av_free(c->chrMmx2Filter);
2887 c->chrMmx2Filter=NULL;
2888 av_free(c->lumMmx2FilterPos);
2889 c->lumMmx2FilterPos=NULL;
2890 av_free(c->chrMmx2FilterPos);
2891 c->chrMmx2FilterPos=NULL;
2892 av_free(c->yuvTable);
2899 * Checks if context is valid or reallocs a new one instead.
2900 * If context is NULL, just calls sws_getContext() to get a new one.
2901 * Otherwise, checks if the parameters are the same already saved in context.
2902 * If that is the case, returns the current context.
2903 * Otherwise, frees context and gets a new one.
2905 * Be warned that srcFilter, dstFilter are not checked, they are
2906 * asumed to remain valid.
2908 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
2909 int srcW, int srcH, int srcFormat,
2910 int dstW, int dstH, int dstFormat, int flags,
2911 SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
2913 if (context != NULL) {
2914 if ((context->srcW != srcW) || (context->srcH != srcH) ||
2915 (context->srcFormat != srcFormat) ||
2916 (context->dstW != dstW) || (context->dstH != dstH) ||
2917 (context->dstFormat != dstFormat) || (context->flags != flags) ||
2918 (context->param != param))
2920 sws_freeContext(context);
2924 if (context == NULL) {
2925 return sws_getContext(srcW, srcH, srcFormat,
2926 dstW, dstH, dstFormat, flags,
2927 srcFilter, dstFilter, param);