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
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #define _SVID_SOURCE //needed for MAP_ANONYMOUS
22 #define _DARWIN_C_SOURCE // needed for MAP_ANON
31 #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
32 #define MAP_ANONYMOUS MAP_ANON
36 #define WIN32_LEAN_AND_MEAN
40 #include "swscale_internal.h"
42 #include "libavutil/intreadwrite.h"
43 #include "libavutil/x86_cpu.h"
44 #include "libavutil/avutil.h"
45 #include "libavutil/bswap.h"
46 #include "libavutil/opt.h"
47 #include "libavutil/pixdesc.h"
49 unsigned swscale_version(void)
51 return LIBSWSCALE_VERSION_INT;
54 const char *swscale_configuration(void)
56 return FFMPEG_CONFIGURATION;
59 const char *swscale_license(void)
61 #define LICENSE_PREFIX "libswscale license: "
62 return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
65 #define RET 0xC3 //near return opcode for x86
67 #define isSupportedIn(x) ( \
68 (x)==PIX_FMT_YUV420P \
69 || (x)==PIX_FMT_YUVA420P \
70 || (x)==PIX_FMT_YUYV422 \
71 || (x)==PIX_FMT_UYVY422 \
72 || (x)==PIX_FMT_RGB48BE \
73 || (x)==PIX_FMT_RGB48LE \
74 || (x)==PIX_FMT_RGB32 \
75 || (x)==PIX_FMT_RGB32_1 \
76 || (x)==PIX_FMT_BGR48BE \
77 || (x)==PIX_FMT_BGR48LE \
78 || (x)==PIX_FMT_BGR24 \
79 || (x)==PIX_FMT_BGR565 \
80 || (x)==PIX_FMT_BGR555 \
81 || (x)==PIX_FMT_BGR32 \
82 || (x)==PIX_FMT_BGR32_1 \
83 || (x)==PIX_FMT_RGB24 \
84 || (x)==PIX_FMT_RGB565 \
85 || (x)==PIX_FMT_RGB555 \
86 || (x)==PIX_FMT_GRAY8 \
87 || (x)==PIX_FMT_Y400A \
88 || (x)==PIX_FMT_YUV410P \
89 || (x)==PIX_FMT_YUV440P \
90 || (x)==PIX_FMT_NV12 \
91 || (x)==PIX_FMT_NV21 \
92 || (x)==PIX_FMT_GRAY16BE \
93 || (x)==PIX_FMT_GRAY16LE \
94 || (x)==PIX_FMT_YUV444P \
95 || (x)==PIX_FMT_YUV422P \
96 || (x)==PIX_FMT_YUV411P \
97 || (x)==PIX_FMT_YUVJ420P \
98 || (x)==PIX_FMT_YUVJ422P \
99 || (x)==PIX_FMT_YUVJ440P \
100 || (x)==PIX_FMT_YUVJ444P \
101 || (x)==PIX_FMT_PAL8 \
102 || (x)==PIX_FMT_BGR8 \
103 || (x)==PIX_FMT_RGB8 \
104 || (x)==PIX_FMT_BGR4_BYTE \
105 || (x)==PIX_FMT_RGB4_BYTE \
106 || (x)==PIX_FMT_YUV440P \
107 || (x)==PIX_FMT_MONOWHITE \
108 || (x)==PIX_FMT_MONOBLACK \
109 || (x)==PIX_FMT_YUV420P16LE \
110 || (x)==PIX_FMT_YUV422P16LE \
111 || (x)==PIX_FMT_YUV444P16LE \
112 || (x)==PIX_FMT_YUV420P16BE \
113 || (x)==PIX_FMT_YUV422P16BE \
114 || (x)==PIX_FMT_YUV444P16BE \
115 || (x)==PIX_FMT_YUV420P9 \
116 || (x)==PIX_FMT_YUV420P10 \
119 int sws_isSupportedInput(enum PixelFormat pix_fmt)
121 return isSupportedIn(pix_fmt);
124 #define isSupportedOut(x) ( \
125 (x)==PIX_FMT_YUV420P \
126 || (x)==PIX_FMT_YUVA420P \
127 || (x)==PIX_FMT_YUYV422 \
128 || (x)==PIX_FMT_UYVY422 \
129 || (x)==PIX_FMT_YUV444P \
130 || (x)==PIX_FMT_YUV422P \
131 || (x)==PIX_FMT_YUV411P \
132 || (x)==PIX_FMT_YUVJ420P \
133 || (x)==PIX_FMT_YUVJ422P \
134 || (x)==PIX_FMT_YUVJ440P \
135 || (x)==PIX_FMT_YUVJ444P \
137 || (x)==PIX_FMT_NV12 \
138 || (x)==PIX_FMT_NV21 \
139 || (x)==PIX_FMT_GRAY16BE \
140 || (x)==PIX_FMT_GRAY16LE \
141 || (x)==PIX_FMT_GRAY8 \
142 || (x)==PIX_FMT_YUV410P \
143 || (x)==PIX_FMT_YUV440P \
144 || (x)==PIX_FMT_YUV420P16LE \
145 || (x)==PIX_FMT_YUV422P16LE \
146 || (x)==PIX_FMT_YUV444P16LE \
147 || (x)==PIX_FMT_YUV420P16BE \
148 || (x)==PIX_FMT_YUV422P16BE \
149 || (x)==PIX_FMT_YUV444P16BE \
152 int sws_isSupportedOutput(enum PixelFormat pix_fmt)
154 return isSupportedOut(pix_fmt);
157 extern const int32_t ff_yuv2rgb_coeffs[8][4];
159 const char *sws_format_name(enum PixelFormat format)
161 if ((unsigned)format < PIX_FMT_NB && av_pix_fmt_descriptors[format].name)
162 return av_pix_fmt_descriptors[format].name;
164 return "Unknown format";
167 static double getSplineCoeff(double a, double b, double c, double d, double dist)
169 // printf("%f %f %f %f %f\n", a,b,c,d,dist);
170 if (dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
171 else return getSplineCoeff( 0.0,
178 static int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
179 int srcW, int dstW, int filterAlign, int one, int flags,
180 SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
186 int64_t *filter=NULL;
187 int64_t *filter2=NULL;
188 const int64_t fone= 1LL<<54;
191 if (flags & SWS_CPU_CAPS_MMX)
192 __asm__ volatile("emms\n\t"::: "memory"); //FIXME this should not be required but it IS (even for non-MMX versions)
195 // NOTE: the +1 is for the MMX scaler which reads over the end
196 FF_ALLOC_OR_GOTO(NULL, *filterPos, (dstW+1)*sizeof(int16_t), fail);
198 if (FFABS(xInc - 0x10000) <10) { // unscaled
201 FF_ALLOCZ_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
203 for (i=0; i<dstW; i++) {
204 filter[i*filterSize]= fone;
208 } else if (flags&SWS_POINT) { // lame looking point sampling mode
212 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
214 xDstInSrc= xInc/2 - 0x8000;
215 for (i=0; i<dstW; i++) {
216 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
222 } else if ((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) { // bilinear upscale
226 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
228 xDstInSrc= xInc/2 - 0x8000;
229 for (i=0; i<dstW; i++) {
230 int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
234 //bilinear upscale / linear interpolate / area averaging
235 for (j=0; j<filterSize; j++) {
236 int64_t coeff= fone - FFABS((xx<<16) - xDstInSrc)*(fone>>16);
237 if (coeff<0) coeff=0;
238 filter[i*filterSize + j]= coeff;
247 if (flags&SWS_BICUBIC) sizeFactor= 4;
248 else if (flags&SWS_X) sizeFactor= 8;
249 else if (flags&SWS_AREA) sizeFactor= 1; //downscale only, for upscale it is bilinear
250 else if (flags&SWS_GAUSS) sizeFactor= 8; // infinite ;)
251 else if (flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? ceil(2*param[0]) : 6;
252 else if (flags&SWS_SINC) sizeFactor= 20; // infinite ;)
253 else if (flags&SWS_SPLINE) sizeFactor= 20; // infinite ;)
254 else if (flags&SWS_BILINEAR) sizeFactor= 2;
256 sizeFactor= 0; //GCC warning killer
260 if (xInc <= 1<<16) filterSize= 1 + sizeFactor; // upscale
261 else filterSize= 1 + (sizeFactor*srcW + dstW - 1)/ dstW;
263 if (filterSize > srcW-2) filterSize=srcW-2;
265 FF_ALLOC_OR_GOTO(NULL, filter, dstW*sizeof(*filter)*filterSize, fail);
267 xDstInSrc= xInc - 0x10000;
268 for (i=0; i<dstW; i++) {
269 int xx= (xDstInSrc - ((filterSize-2)<<16)) / (1<<17);
272 for (j=0; j<filterSize; j++) {
273 int64_t d= ((int64_t)FFABS((xx<<17) - xDstInSrc))<<13;
279 floatd= d * (1.0/(1<<30));
281 if (flags & SWS_BICUBIC) {
282 int64_t B= (param[0] != SWS_PARAM_DEFAULT ? param[0] : 0) * (1<<24);
283 int64_t C= (param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6) * (1<<24);
284 int64_t dd = ( d*d)>>30;
285 int64_t ddd= (dd*d)>>30;
288 coeff = (12*(1<<24)-9*B-6*C)*ddd + (-18*(1<<24)+12*B+6*C)*dd + (6*(1<<24)-2*B)*(1<<30);
289 else if (d < 1LL<<31)
290 coeff = (-B-6*C)*ddd + (6*B+30*C)*dd + (-12*B-48*C)*d + (8*B+24*C)*(1<<30);
293 coeff *= fone>>(30+24);
295 /* else if (flags & SWS_X) {
296 double p= param ? param*0.01 : 0.3;
297 coeff = d ? sin(d*M_PI)/(d*M_PI) : 1.0;
298 coeff*= pow(2.0, - p*d*d);
300 else if (flags & SWS_X) {
301 double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
305 c = cos(floatd*M_PI);
308 if (c<0.0) c= -pow(-c, A);
310 coeff= (c*0.5 + 0.5)*fone;
311 } else if (flags & SWS_AREA) {
312 int64_t d2= d - (1<<29);
313 if (d2*xInc < -(1LL<<(29+16))) coeff= 1.0 * (1LL<<(30+16));
314 else if (d2*xInc < (1LL<<(29+16))) coeff= -d2*xInc + (1LL<<(29+16));
316 coeff *= fone>>(30+16);
317 } else if (flags & SWS_GAUSS) {
318 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
319 coeff = (pow(2.0, - p*floatd*floatd))*fone;
320 } else if (flags & SWS_SINC) {
321 coeff = (d ? sin(floatd*M_PI)/(floatd*M_PI) : 1.0)*fone;
322 } else if (flags & SWS_LANCZOS) {
323 double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
324 coeff = (d ? sin(floatd*M_PI)*sin(floatd*M_PI/p)/(floatd*floatd*M_PI*M_PI/p) : 1.0)*fone;
325 if (floatd>p) coeff=0;
326 } else if (flags & SWS_BILINEAR) {
328 if (coeff<0) coeff=0;
330 } else if (flags & SWS_SPLINE) {
331 double p=-2.196152422706632;
332 coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, floatd) * fone;
334 coeff= 0.0; //GCC warning killer
338 filter[i*filterSize + j]= coeff;
345 /* apply src & dst Filter to filter -> filter2
348 assert(filterSize>0);
349 filter2Size= filterSize;
350 if (srcFilter) filter2Size+= srcFilter->length - 1;
351 if (dstFilter) filter2Size+= dstFilter->length - 1;
352 assert(filter2Size>0);
353 FF_ALLOCZ_OR_GOTO(NULL, filter2, filter2Size*dstW*sizeof(*filter2), fail);
355 for (i=0; i<dstW; i++) {
359 for (k=0; k<srcFilter->length; k++) {
360 for (j=0; j<filterSize; j++)
361 filter2[i*filter2Size + k + j] += srcFilter->coeff[k]*filter[i*filterSize + j];
364 for (j=0; j<filterSize; j++)
365 filter2[i*filter2Size + j]= filter[i*filterSize + j];
369 (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
373 /* try to reduce the filter-size (step1 find size and shift left) */
374 // Assume it is near normalized (*0.5 or *2.0 is OK but * 0.001 is not).
376 for (i=dstW-1; i>=0; i--) {
377 int min= filter2Size;
381 /* get rid of near zero elements on the left by shifting left */
382 for (j=0; j<filter2Size; j++) {
384 cutOff += FFABS(filter2[i*filter2Size]);
386 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
388 /* preserve monotonicity because the core can't handle the filter otherwise */
389 if (i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
391 // move filter coefficients left
392 for (k=1; k<filter2Size; k++)
393 filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
394 filter2[i*filter2Size + k - 1]= 0;
399 /* count near zeros on the right */
400 for (j=filter2Size-1; j>0; j--) {
401 cutOff += FFABS(filter2[i*filter2Size + j]);
403 if (cutOff > SWS_MAX_REDUCE_CUTOFF*fone) break;
407 if (min>minFilterSize) minFilterSize= min;
410 if (flags & SWS_CPU_CAPS_ALTIVEC) {
411 // we can handle the special case 4,
412 // so we don't want to go to the full 8
413 if (minFilterSize < 5)
416 // We really don't want to waste our time
417 // doing useless computation, so fall back on
418 // the scalar C code for very small filters.
419 // Vectorizing is worth it only if you have a
420 // decent-sized vector.
421 if (minFilterSize < 3)
425 if (flags & SWS_CPU_CAPS_MMX) {
426 // special case for unscaled vertical filtering
427 if (minFilterSize == 1 && filterAlign == 2)
431 assert(minFilterSize > 0);
432 filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
433 assert(filterSize > 0);
434 filter= av_malloc(filterSize*dstW*sizeof(*filter));
435 if (filterSize >= MAX_FILTER_SIZE*16/((flags&SWS_ACCURATE_RND) ? APCK_SIZE : 16) || !filter)
437 *outFilterSize= filterSize;
439 if (flags&SWS_PRINT_INFO)
440 av_log(NULL, AV_LOG_VERBOSE, "SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
441 /* try to reduce the filter-size (step2 reduce it) */
442 for (i=0; i<dstW; i++) {
445 for (j=0; j<filterSize; j++) {
446 if (j>=filter2Size) filter[i*filterSize + j]= 0;
447 else filter[i*filterSize + j]= filter2[i*filter2Size + j];
448 if((flags & SWS_BITEXACT) && j>=minFilterSize)
449 filter[i*filterSize + j]= 0;
453 //FIXME try to align filterPos if possible
456 for (i=0; i<dstW; i++) {
458 if ((*filterPos)[i] < 0) {
459 // move filter coefficients left to compensate for filterPos
460 for (j=1; j<filterSize; j++) {
461 int left= FFMAX(j + (*filterPos)[i], 0);
462 filter[i*filterSize + left] += filter[i*filterSize + j];
463 filter[i*filterSize + j]=0;
468 if ((*filterPos)[i] + filterSize > srcW) {
469 int shift= (*filterPos)[i] + filterSize - srcW;
470 // move filter coefficients right to compensate for filterPos
471 for (j=filterSize-2; j>=0; j--) {
472 int right= FFMIN(j + shift, filterSize-1);
473 filter[i*filterSize +right] += filter[i*filterSize +j];
474 filter[i*filterSize +j]=0;
476 (*filterPos)[i]= srcW - filterSize;
480 // Note the +1 is for the MMX scaler which reads over the end
481 /* align at 16 for AltiVec (needed by hScale_altivec_real) */
482 FF_ALLOCZ_OR_GOTO(NULL, *outFilter, *outFilterSize*(dstW+1)*sizeof(int16_t), fail);
484 /* normalize & store in outFilter */
485 for (i=0; i<dstW; i++) {
490 for (j=0; j<filterSize; j++) {
491 sum+= filter[i*filterSize + j];
493 sum= (sum + one/2)/ one;
494 for (j=0; j<*outFilterSize; j++) {
495 int64_t v= filter[i*filterSize + j] + error;
496 int intV= ROUNDED_DIV(v, sum);
497 (*outFilter)[i*(*outFilterSize) + j]= intV;
502 (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
503 for (i=0; i<*outFilterSize; i++) {
504 int j= dstW*(*outFilterSize);
505 (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
515 #if ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT)
516 static int initMMX2HScaler(int dstW, int xInc, uint8_t *filterCode, int16_t *filter, int32_t *filterPos, int numSplits)
519 x86_reg imm8OfPShufW1A;
520 x86_reg imm8OfPShufW2A;
521 x86_reg fragmentLengthA;
523 x86_reg imm8OfPShufW1B;
524 x86_reg imm8OfPShufW2B;
525 x86_reg fragmentLengthB;
530 // create an optimized horizontal scaling routine
531 /* This scaler is made of runtime-generated MMX2 code using specially
532 * tuned pshufw instructions. For every four output pixels, if four
533 * input pixels are enough for the fast bilinear scaling, then a chunk
534 * of fragmentB is used. If five input pixels are needed, then a chunk
535 * of fragmentA is used.
544 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
545 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
546 "movd 1(%%"REG_c", %%"REG_S"), %%mm1 \n\t"
547 "punpcklbw %%mm7, %%mm1 \n\t"
548 "punpcklbw %%mm7, %%mm0 \n\t"
549 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
551 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
553 "psubw %%mm1, %%mm0 \n\t"
554 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
555 "pmullw %%mm3, %%mm0 \n\t"
556 "psllw $7, %%mm1 \n\t"
557 "paddw %%mm1, %%mm0 \n\t"
559 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
561 "add $8, %%"REG_a" \n\t"
565 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
566 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
567 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
572 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
576 :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
577 "=r" (fragmentLengthA)
584 "movq (%%"REG_d", %%"REG_a"), %%mm3 \n\t"
585 "movd (%%"REG_c", %%"REG_S"), %%mm0 \n\t"
586 "punpcklbw %%mm7, %%mm0 \n\t"
587 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
589 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
591 "psubw %%mm1, %%mm0 \n\t"
592 "movl 8(%%"REG_b", %%"REG_a"), %%esi \n\t"
593 "pmullw %%mm3, %%mm0 \n\t"
594 "psllw $7, %%mm1 \n\t"
595 "paddw %%mm1, %%mm0 \n\t"
597 "movq %%mm0, (%%"REG_D", %%"REG_a") \n\t"
599 "add $8, %%"REG_a" \n\t"
603 "lea " LOCAL_MANGLE(0b) ", %0 \n\t"
604 "lea " LOCAL_MANGLE(1b) ", %1 \n\t"
605 "lea " LOCAL_MANGLE(2b) ", %2 \n\t"
610 "lea " LOCAL_MANGLE(9b) ", %3 \n\t"
614 :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
615 "=r" (fragmentLengthB)
618 xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
621 for (i=0; i<dstW/numSplits; i++) {
626 int b=((xpos+xInc)>>16) - xx;
627 int c=((xpos+xInc*2)>>16) - xx;
628 int d=((xpos+xInc*3)>>16) - xx;
630 uint8_t *fragment = (d+1<4) ? fragmentB : fragmentA;
631 x86_reg imm8OfPShufW1 = (d+1<4) ? imm8OfPShufW1B : imm8OfPShufW1A;
632 x86_reg imm8OfPShufW2 = (d+1<4) ? imm8OfPShufW2B : imm8OfPShufW2A;
633 x86_reg fragmentLength = (d+1<4) ? fragmentLengthB : fragmentLengthA;
634 int maxShift= 3-(d+inc);
638 filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
639 filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
640 filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
641 filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
644 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
646 filterCode[fragmentPos + imm8OfPShufW1]=
647 (a+inc) | ((b+inc)<<2) | ((c+inc)<<4) | ((d+inc)<<6);
648 filterCode[fragmentPos + imm8OfPShufW2]=
649 a | (b<<2) | (c<<4) | (d<<6);
651 if (i+4-inc>=dstW) shift=maxShift; //avoid overread
652 else if ((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
654 if (shift && i>=shift) {
655 filterCode[fragmentPos + imm8OfPShufW1]+= 0x55*shift;
656 filterCode[fragmentPos + imm8OfPShufW2]+= 0x55*shift;
657 filterPos[i/2]-=shift;
661 fragmentPos+= fragmentLength;
664 filterCode[fragmentPos]= RET;
669 filterPos[((i/2)+1)&(~1)]= xpos>>16; // needed to jump to the next part
671 return fragmentPos + 1;
673 #endif /* ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) */
675 static void getSubSampleFactors(int *h, int *v, enum PixelFormat format)
677 *h = av_pix_fmt_descriptors[format].log2_chroma_w;
678 *v = av_pix_fmt_descriptors[format].log2_chroma_h;
681 static int update_flags_cpu(int flags);
683 int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation)
685 memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
686 memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
688 c->brightness= brightness;
689 c->contrast = contrast;
690 c->saturation= saturation;
691 c->srcRange = srcRange;
692 c->dstRange = dstRange;
693 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
695 c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->dstFormat]);
696 c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[c->srcFormat]);
697 c->flags = update_flags_cpu(c->flags);
699 ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
703 if (c->flags & SWS_CPU_CAPS_ALTIVEC)
704 ff_yuv2rgb_init_tables_altivec(c, inv_table, brightness, contrast, saturation);
709 int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation)
711 if (isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
713 *inv_table = c->srcColorspaceTable;
714 *table = c->dstColorspaceTable;
715 *srcRange = c->srcRange;
716 *dstRange = c->dstRange;
717 *brightness= c->brightness;
718 *contrast = c->contrast;
719 *saturation= c->saturation;
724 static int handle_jpeg(enum PixelFormat *format)
727 case PIX_FMT_YUVJ420P: *format = PIX_FMT_YUV420P; return 1;
728 case PIX_FMT_YUVJ422P: *format = PIX_FMT_YUV422P; return 1;
729 case PIX_FMT_YUVJ444P: *format = PIX_FMT_YUV444P; return 1;
730 case PIX_FMT_YUVJ440P: *format = PIX_FMT_YUV440P; return 1;
735 static int update_flags_cpu(int flags)
737 #if !CONFIG_RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
738 flags &= ~( SWS_CPU_CAPS_MMX
742 |SWS_CPU_CAPS_ALTIVEC
744 flags |= ff_hardcodedcpuflags();
745 #endif /* CONFIG_RUNTIME_CPUDETECT */
749 SwsContext *sws_alloc_context(void)
751 SwsContext *c= av_mallocz(sizeof(SwsContext));
753 c->av_class = &sws_context_class;
754 av_opt_set_defaults(c);
759 int sws_init_context(SwsContext *c, SwsFilter *srcFilter, SwsFilter *dstFilter)
762 int usesVFilter, usesHFilter;
764 SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
770 enum PixelFormat srcFormat= c->srcFormat;
771 enum PixelFormat dstFormat= c->dstFormat;
773 flags= c->flags = update_flags_cpu(c->flags);
775 if (flags & SWS_CPU_CAPS_MMX)
776 __asm__ volatile("emms\n\t"::: "memory");
778 if (!rgb15to16) sws_rgb2rgb_init(flags);
780 unscaled = (srcW == dstW && srcH == dstH);
782 if (!isSupportedIn(srcFormat)) {
783 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as input pixel format\n", sws_format_name(srcFormat));
784 return AVERROR(EINVAL);
786 if (!isSupportedOut(dstFormat)) {
787 av_log(NULL, AV_LOG_ERROR, "swScaler: %s is not supported as output pixel format\n", sws_format_name(dstFormat));
788 return AVERROR(EINVAL);
791 i= flags & ( SWS_POINT
802 if(!i || (i & (i-1))) {
803 av_log(NULL, AV_LOG_ERROR, "swScaler: Exactly one scaler algorithm must be chosen\n");
804 return AVERROR(EINVAL);
807 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
808 av_log(NULL, AV_LOG_ERROR, "swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
809 srcW, srcH, dstW, dstH);
810 return AVERROR(EINVAL);
812 if(srcW > VOFW || dstW > VOFW) {
813 av_log(NULL, AV_LOG_ERROR, "swScaler: Compile-time maximum width is "AV_STRINGIFY(VOFW)" change VOF/VOFW and recompile\n");
814 return AVERROR(EINVAL);
817 if (!dstFilter) dstFilter= &dummyFilter;
818 if (!srcFilter) srcFilter= &dummyFilter;
820 c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
821 c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
822 c->dstFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[dstFormat]);
823 c->srcFormatBpp = av_get_bits_per_pixel(&av_pix_fmt_descriptors[srcFormat]);
824 c->vRounder= 4* 0x0001000100010001ULL;
826 usesVFilter = (srcFilter->lumV && srcFilter->lumV->length>1) ||
827 (srcFilter->chrV && srcFilter->chrV->length>1) ||
828 (dstFilter->lumV && dstFilter->lumV->length>1) ||
829 (dstFilter->chrV && dstFilter->chrV->length>1);
830 usesHFilter = (srcFilter->lumH && srcFilter->lumH->length>1) ||
831 (srcFilter->chrH && srcFilter->chrH->length>1) ||
832 (dstFilter->lumH && dstFilter->lumH->length>1) ||
833 (dstFilter->chrH && dstFilter->chrH->length>1);
835 getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
836 getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
838 // reuse chroma for 2 pixels RGB/BGR unless user wants full chroma interpolation
839 if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
841 // drop some chroma lines if the user wants it
842 c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
843 c->chrSrcVSubSample+= c->vChrDrop;
845 // drop every other pixel for chroma calculation unless user wants full chroma
846 if (isAnyRGB(srcFormat) && !(flags&SWS_FULL_CHR_H_INP)
847 && srcFormat!=PIX_FMT_RGB8 && srcFormat!=PIX_FMT_BGR8
848 && srcFormat!=PIX_FMT_RGB4 && srcFormat!=PIX_FMT_BGR4
849 && srcFormat!=PIX_FMT_RGB4_BYTE && srcFormat!=PIX_FMT_BGR4_BYTE
850 && ((dstW>>c->chrDstHSubSample) <= (srcW>>1) || (flags&SWS_FAST_BILINEAR)))
851 c->chrSrcHSubSample=1;
853 // Note the -((-x)>>y) is so that we always round toward +inf.
854 c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
855 c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
856 c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
857 c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
859 /* unscaled special cases */
860 if (unscaled && !usesHFilter && !usesVFilter && (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
861 ff_get_unscaled_swscale(c);
864 if (flags&SWS_PRINT_INFO)
865 av_log(c, AV_LOG_INFO, "using unscaled %s -> %s special converter\n",
866 sws_format_name(srcFormat), sws_format_name(dstFormat));
871 if (flags & SWS_CPU_CAPS_MMX2) {
872 c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
873 if (!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR)) {
874 if (flags&SWS_PRINT_INFO)
875 av_log(c, AV_LOG_INFO, "output width is not a multiple of 32 -> no MMX2 scaler\n");
877 if (usesHFilter) c->canMMX2BeUsed=0;
882 c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
883 c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
885 // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
886 // but only for the FAST_BILINEAR mode otherwise do correct scaling
887 // n-2 is the last chrominance sample available
888 // this is not perfect, but no one should notice the difference, the more correct variant
889 // would be like the vertical one, but that would require some special code for the
890 // first and last pixel
891 if (flags&SWS_FAST_BILINEAR) {
892 if (c->canMMX2BeUsed) {
896 //we don't use the x86 asm scaler if MMX is available
897 else if (flags & SWS_CPU_CAPS_MMX) {
898 c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
899 c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
903 /* precalculate horizontal scaler filter coefficients */
905 #if ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT)
906 // can't downscale !!!
907 if (c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR)) {
908 c->lumMmx2FilterCodeSize = initMMX2HScaler( dstW, c->lumXInc, NULL, NULL, NULL, 8);
909 c->chrMmx2FilterCodeSize = initMMX2HScaler(c->chrDstW, c->chrXInc, NULL, NULL, NULL, 4);
912 c->lumMmx2FilterCode = mmap(NULL, c->lumMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
913 c->chrMmx2FilterCode = mmap(NULL, c->chrMmx2FilterCodeSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
914 #elif HAVE_VIRTUALALLOC
915 c->lumMmx2FilterCode = VirtualAlloc(NULL, c->lumMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
916 c->chrMmx2FilterCode = VirtualAlloc(NULL, c->chrMmx2FilterCodeSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
918 c->lumMmx2FilterCode = av_malloc(c->lumMmx2FilterCodeSize);
919 c->chrMmx2FilterCode = av_malloc(c->chrMmx2FilterCodeSize);
922 if (!c->lumMmx2FilterCode || !c->chrMmx2FilterCode)
923 return AVERROR(ENOMEM);
924 FF_ALLOCZ_OR_GOTO(c, c->hLumFilter , (dstW /8+8)*sizeof(int16_t), fail);
925 FF_ALLOCZ_OR_GOTO(c, c->hChrFilter , (c->chrDstW /4+8)*sizeof(int16_t), fail);
926 FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW /2/8+8)*sizeof(int32_t), fail);
927 FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW/2/4+8)*sizeof(int32_t), fail);
929 initMMX2HScaler( dstW, c->lumXInc, c->lumMmx2FilterCode, c->hLumFilter, c->hLumFilterPos, 8);
930 initMMX2HScaler(c->chrDstW, c->chrXInc, c->chrMmx2FilterCode, c->hChrFilter, c->hChrFilterPos, 4);
933 mprotect(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
934 mprotect(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize, PROT_EXEC | PROT_READ);
937 #endif /* ARCH_X86 && (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) */
939 const int filterAlign=
940 (flags & SWS_CPU_CAPS_MMX) ? 4 :
941 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
944 if (initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
945 srcW , dstW, filterAlign, 1<<14,
946 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
947 srcFilter->lumH, dstFilter->lumH, c->param) < 0)
949 if (initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
950 c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
951 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
952 srcFilter->chrH, dstFilter->chrH, c->param) < 0)
955 } // initialize horizontal stuff
957 /* precalculate vertical scaler filter coefficients */
959 const int filterAlign=
960 (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
961 (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
964 if (initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
965 srcH , dstH, filterAlign, (1<<12),
966 (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
967 srcFilter->lumV, dstFilter->lumV, c->param) < 0)
969 if (initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
970 c->chrSrcH, c->chrDstH, filterAlign, (1<<12),
971 (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
972 srcFilter->chrV, dstFilter->chrV, c->param) < 0)
976 FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof (vector signed short)*c->vLumFilterSize*c->dstH, fail);
977 FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH, fail);
979 for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
981 short *p = (short *)&c->vYCoeffsBank[i];
983 p[j] = c->vLumFilter[i];
986 for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
988 short *p = (short *)&c->vCCoeffsBank[i];
990 p[j] = c->vChrFilter[i];
995 // calculate buffer sizes so that they won't run out while handling these damn slices
996 c->vLumBufSize= c->vLumFilterSize;
997 c->vChrBufSize= c->vChrFilterSize;
998 for (i=0; i<dstH; i++) {
999 int chrI= i*c->chrDstH / dstH;
1000 int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
1001 ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
1003 nextSlice>>= c->chrSrcVSubSample;
1004 nextSlice<<= c->chrSrcVSubSample;
1005 if (c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
1006 c->vLumBufSize= nextSlice - c->vLumFilterPos[i];
1007 if (c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
1008 c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
1011 // allocate pixbufs (we use dynamic allocation because otherwise we would need to
1012 // allocate several megabytes to handle all possible cases)
1013 FF_ALLOC_OR_GOTO(c, c->lumPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
1014 FF_ALLOC_OR_GOTO(c, c->chrPixBuf, c->vChrBufSize*2*sizeof(int16_t*), fail);
1015 if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
1016 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize*2*sizeof(int16_t*), fail);
1017 //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)
1018 /* align at 16 bytes for AltiVec */
1019 for (i=0; i<c->vLumBufSize; i++) {
1020 FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i+c->vLumBufSize], VOF+1, fail);
1021 c->lumPixBuf[i] = c->lumPixBuf[i+c->vLumBufSize];
1023 for (i=0; i<c->vChrBufSize; i++) {
1024 FF_ALLOC_OR_GOTO(c, c->chrPixBuf[i+c->vChrBufSize], (VOF+1)*2, fail);
1025 c->chrPixBuf[i] = c->chrPixBuf[i+c->vChrBufSize];
1027 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
1028 for (i=0; i<c->vLumBufSize; i++) {
1029 FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i+c->vLumBufSize], VOF+1, fail);
1030 c->alpPixBuf[i] = c->alpPixBuf[i+c->vLumBufSize];
1033 //try to avoid drawing green stuff between the right end and the stride end
1034 for (i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, (VOF+1)*2);
1036 assert(2*VOFW == VOF);
1038 assert(c->chrDstH <= dstH);
1040 if (flags&SWS_PRINT_INFO) {
1041 if (flags&SWS_FAST_BILINEAR) av_log(c, AV_LOG_INFO, "FAST_BILINEAR scaler, ");
1042 else if (flags&SWS_BILINEAR) av_log(c, AV_LOG_INFO, "BILINEAR scaler, ");
1043 else if (flags&SWS_BICUBIC) av_log(c, AV_LOG_INFO, "BICUBIC scaler, ");
1044 else if (flags&SWS_X) av_log(c, AV_LOG_INFO, "Experimental scaler, ");
1045 else if (flags&SWS_POINT) av_log(c, AV_LOG_INFO, "Nearest Neighbor / POINT scaler, ");
1046 else if (flags&SWS_AREA) av_log(c, AV_LOG_INFO, "Area Averaging scaler, ");
1047 else if (flags&SWS_BICUBLIN) av_log(c, AV_LOG_INFO, "luma BICUBIC / chroma BILINEAR scaler, ");
1048 else if (flags&SWS_GAUSS) av_log(c, AV_LOG_INFO, "Gaussian scaler, ");
1049 else if (flags&SWS_SINC) av_log(c, AV_LOG_INFO, "Sinc scaler, ");
1050 else if (flags&SWS_LANCZOS) av_log(c, AV_LOG_INFO, "Lanczos scaler, ");
1051 else if (flags&SWS_SPLINE) av_log(c, AV_LOG_INFO, "Bicubic spline scaler, ");
1052 else av_log(c, AV_LOG_INFO, "ehh flags invalid?! ");
1054 av_log(c, AV_LOG_INFO, "from %s to %s%s ",
1055 sws_format_name(srcFormat),
1057 dstFormat == PIX_FMT_BGR555 || dstFormat == PIX_FMT_BGR565 ||
1058 dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
1059 dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE ? "dithered " : "",
1063 sws_format_name(dstFormat));
1065 if (flags & SWS_CPU_CAPS_MMX2) av_log(c, AV_LOG_INFO, "using MMX2\n");
1066 else if (flags & SWS_CPU_CAPS_3DNOW) av_log(c, AV_LOG_INFO, "using 3DNOW\n");
1067 else if (flags & SWS_CPU_CAPS_MMX) av_log(c, AV_LOG_INFO, "using MMX\n");
1068 else if (flags & SWS_CPU_CAPS_ALTIVEC) av_log(c, AV_LOG_INFO, "using AltiVec\n");
1069 else av_log(c, AV_LOG_INFO, "using C\n");
1071 if (flags & SWS_CPU_CAPS_MMX) {
1072 if (c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
1073 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
1075 if (c->hLumFilterSize==4)
1076 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal luminance scaling\n");
1077 else if (c->hLumFilterSize==8)
1078 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal luminance scaling\n");
1080 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal luminance scaling\n");
1082 if (c->hChrFilterSize==4)
1083 av_log(c, AV_LOG_VERBOSE, "using 4-tap MMX scaler for horizontal chrominance scaling\n");
1084 else if (c->hChrFilterSize==8)
1085 av_log(c, AV_LOG_VERBOSE, "using 8-tap MMX scaler for horizontal chrominance scaling\n");
1087 av_log(c, AV_LOG_VERBOSE, "using n-tap MMX scaler for horizontal chrominance scaling\n");
1091 av_log(c, AV_LOG_VERBOSE, "using x86 asm scaler for horizontal scaling\n");
1093 if (flags & SWS_FAST_BILINEAR)
1094 av_log(c, AV_LOG_VERBOSE, "using FAST_BILINEAR C scaler for horizontal scaling\n");
1096 av_log(c, AV_LOG_VERBOSE, "using C scaler for horizontal scaling\n");
1099 if (isPlanarYUV(dstFormat)) {
1100 if (c->vLumFilterSize==1)
1101 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1103 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1105 if (c->vLumFilterSize==1 && c->vChrFilterSize==2)
1106 av_log(c, AV_LOG_VERBOSE, "using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
1107 " 2-tap scaler for vertical chrominance scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1108 else if (c->vLumFilterSize==2 && c->vChrFilterSize==2)
1109 av_log(c, AV_LOG_VERBOSE, "using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1111 av_log(c, AV_LOG_VERBOSE, "using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1114 if (dstFormat==PIX_FMT_BGR24)
1115 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR24 converter\n",
1116 (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
1117 else if (dstFormat==PIX_FMT_RGB32)
1118 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR32 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1119 else if (dstFormat==PIX_FMT_BGR565)
1120 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR16 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1121 else if (dstFormat==PIX_FMT_BGR555)
1122 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR15 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1123 else if (dstFormat == PIX_FMT_RGB444BE || dstFormat == PIX_FMT_RGB444LE ||
1124 dstFormat == PIX_FMT_BGR444BE || dstFormat == PIX_FMT_BGR444LE)
1125 av_log(c, AV_LOG_VERBOSE, "using %s YV12->BGR12 converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
1127 av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
1128 av_log(c, AV_LOG_DEBUG, "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1129 c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
1130 av_log(c, AV_LOG_DEBUG, "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1131 c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
1134 c->swScale= ff_getSwsFunc(c);
1136 fail: //FIXME replace things by appropriate error codes
1140 #if FF_API_SWS_GETCONTEXT
1141 SwsContext *sws_getContext(int srcW, int srcH, enum PixelFormat srcFormat,
1142 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
1143 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
1147 if(!(c=sws_alloc_context()))
1155 c->srcRange = handle_jpeg(&srcFormat);
1156 c->dstRange = handle_jpeg(&dstFormat);
1157 c->srcFormat= srcFormat;
1158 c->dstFormat= dstFormat;
1161 c->param[0] = param[0];
1162 c->param[1] = param[1];
1164 sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, c->dstRange, 0, 1<<16, 1<<16);
1166 if(sws_init_context(c, srcFilter, dstFilter) < 0){
1175 SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
1176 float lumaSharpen, float chromaSharpen,
1177 float chromaHShift, float chromaVShift,
1180 SwsFilter *filter= av_malloc(sizeof(SwsFilter));
1184 if (lumaGBlur!=0.0) {
1185 filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
1186 filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
1188 filter->lumH= sws_getIdentityVec();
1189 filter->lumV= sws_getIdentityVec();
1192 if (chromaGBlur!=0.0) {
1193 filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
1194 filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
1196 filter->chrH= sws_getIdentityVec();
1197 filter->chrV= sws_getIdentityVec();
1200 if (chromaSharpen!=0.0) {
1201 SwsVector *id= sws_getIdentityVec();
1202 sws_scaleVec(filter->chrH, -chromaSharpen);
1203 sws_scaleVec(filter->chrV, -chromaSharpen);
1204 sws_addVec(filter->chrH, id);
1205 sws_addVec(filter->chrV, id);
1209 if (lumaSharpen!=0.0) {
1210 SwsVector *id= sws_getIdentityVec();
1211 sws_scaleVec(filter->lumH, -lumaSharpen);
1212 sws_scaleVec(filter->lumV, -lumaSharpen);
1213 sws_addVec(filter->lumH, id);
1214 sws_addVec(filter->lumV, id);
1218 if (chromaHShift != 0.0)
1219 sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
1221 if (chromaVShift != 0.0)
1222 sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
1224 sws_normalizeVec(filter->chrH, 1.0);
1225 sws_normalizeVec(filter->chrV, 1.0);
1226 sws_normalizeVec(filter->lumH, 1.0);
1227 sws_normalizeVec(filter->lumV, 1.0);
1229 if (verbose) sws_printVec2(filter->chrH, NULL, AV_LOG_DEBUG);
1230 if (verbose) sws_printVec2(filter->lumH, NULL, AV_LOG_DEBUG);
1235 SwsVector *sws_allocVec(int length)
1237 SwsVector *vec = av_malloc(sizeof(SwsVector));
1240 vec->length = length;
1241 vec->coeff = av_malloc(sizeof(double) * length);
1247 SwsVector *sws_getGaussianVec(double variance, double quality)
1249 const int length= (int)(variance*quality + 0.5) | 1;
1251 double middle= (length-1)*0.5;
1252 SwsVector *vec= sws_allocVec(length);
1257 for (i=0; i<length; i++) {
1258 double dist= i-middle;
1259 vec->coeff[i]= exp(-dist*dist/(2*variance*variance)) / sqrt(2*variance*M_PI);
1262 sws_normalizeVec(vec, 1.0);
1267 SwsVector *sws_getConstVec(double c, int length)
1270 SwsVector *vec= sws_allocVec(length);
1275 for (i=0; i<length; i++)
1281 SwsVector *sws_getIdentityVec(void)
1283 return sws_getConstVec(1.0, 1);
1286 static double sws_dcVec(SwsVector *a)
1291 for (i=0; i<a->length; i++)
1297 void sws_scaleVec(SwsVector *a, double scalar)
1301 for (i=0; i<a->length; i++)
1302 a->coeff[i]*= scalar;
1305 void sws_normalizeVec(SwsVector *a, double height)
1307 sws_scaleVec(a, height/sws_dcVec(a));
1310 static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b)
1312 int length= a->length + b->length - 1;
1314 SwsVector *vec= sws_getConstVec(0.0, length);
1319 for (i=0; i<a->length; i++) {
1320 for (j=0; j<b->length; j++) {
1321 vec->coeff[i+j]+= a->coeff[i]*b->coeff[j];
1328 static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b)
1330 int length= FFMAX(a->length, b->length);
1332 SwsVector *vec= sws_getConstVec(0.0, length);
1337 for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1338 for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
1343 static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b)
1345 int length= FFMAX(a->length, b->length);
1347 SwsVector *vec= sws_getConstVec(0.0, length);
1352 for (i=0; i<a->length; i++) vec->coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
1353 for (i=0; i<b->length; i++) vec->coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
1358 /* shift left / or right if "shift" is negative */
1359 static SwsVector *sws_getShiftedVec(SwsVector *a, int shift)
1361 int length= a->length + FFABS(shift)*2;
1363 SwsVector *vec= sws_getConstVec(0.0, length);
1368 for (i=0; i<a->length; i++) {
1369 vec->coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
1375 void sws_shiftVec(SwsVector *a, int shift)
1377 SwsVector *shifted= sws_getShiftedVec(a, shift);
1379 a->coeff= shifted->coeff;
1380 a->length= shifted->length;
1384 void sws_addVec(SwsVector *a, SwsVector *b)
1386 SwsVector *sum= sws_sumVec(a, b);
1388 a->coeff= sum->coeff;
1389 a->length= sum->length;
1393 void sws_subVec(SwsVector *a, SwsVector *b)
1395 SwsVector *diff= sws_diffVec(a, b);
1397 a->coeff= diff->coeff;
1398 a->length= diff->length;
1402 void sws_convVec(SwsVector *a, SwsVector *b)
1404 SwsVector *conv= sws_getConvVec(a, b);
1406 a->coeff= conv->coeff;
1407 a->length= conv->length;
1411 SwsVector *sws_cloneVec(SwsVector *a)
1414 SwsVector *vec= sws_allocVec(a->length);
1419 for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
1424 void sws_printVec2(SwsVector *a, AVClass *log_ctx, int log_level)
1431 for (i=0; i<a->length; i++)
1432 if (a->coeff[i]>max) max= a->coeff[i];
1434 for (i=0; i<a->length; i++)
1435 if (a->coeff[i]<min) min= a->coeff[i];
1439 for (i=0; i<a->length; i++) {
1440 int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
1441 av_log(log_ctx, log_level, "%1.3f ", a->coeff[i]);
1442 for (;x>0; x--) av_log(log_ctx, log_level, " ");
1443 av_log(log_ctx, log_level, "|\n");
1447 #if LIBSWSCALE_VERSION_MAJOR < 1
1448 void sws_printVec(SwsVector *a)
1450 sws_printVec2(a, NULL, AV_LOG_DEBUG);
1454 void sws_freeVec(SwsVector *a)
1457 av_freep(&a->coeff);
1462 void sws_freeFilter(SwsFilter *filter)
1464 if (!filter) return;
1466 if (filter->lumH) sws_freeVec(filter->lumH);
1467 if (filter->lumV) sws_freeVec(filter->lumV);
1468 if (filter->chrH) sws_freeVec(filter->chrH);
1469 if (filter->chrV) sws_freeVec(filter->chrV);
1473 void sws_freeContext(SwsContext *c)
1479 for (i=0; i<c->vLumBufSize; i++)
1480 av_freep(&c->lumPixBuf[i]);
1481 av_freep(&c->lumPixBuf);
1485 for (i=0; i<c->vChrBufSize; i++)
1486 av_freep(&c->chrPixBuf[i]);
1487 av_freep(&c->chrPixBuf);
1490 if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
1491 for (i=0; i<c->vLumBufSize; i++)
1492 av_freep(&c->alpPixBuf[i]);
1493 av_freep(&c->alpPixBuf);
1496 av_freep(&c->vLumFilter);
1497 av_freep(&c->vChrFilter);
1498 av_freep(&c->hLumFilter);
1499 av_freep(&c->hChrFilter);
1501 av_freep(&c->vYCoeffsBank);
1502 av_freep(&c->vCCoeffsBank);
1505 av_freep(&c->vLumFilterPos);
1506 av_freep(&c->vChrFilterPos);
1507 av_freep(&c->hLumFilterPos);
1508 av_freep(&c->hChrFilterPos);
1511 #ifdef MAP_ANONYMOUS
1512 if (c->lumMmx2FilterCode) munmap(c->lumMmx2FilterCode, c->lumMmx2FilterCodeSize);
1513 if (c->chrMmx2FilterCode) munmap(c->chrMmx2FilterCode, c->chrMmx2FilterCodeSize);
1514 #elif HAVE_VIRTUALALLOC
1515 if (c->lumMmx2FilterCode) VirtualFree(c->lumMmx2FilterCode, 0, MEM_RELEASE);
1516 if (c->chrMmx2FilterCode) VirtualFree(c->chrMmx2FilterCode, 0, MEM_RELEASE);
1518 av_free(c->lumMmx2FilterCode);
1519 av_free(c->chrMmx2FilterCode);
1521 c->lumMmx2FilterCode=NULL;
1522 c->chrMmx2FilterCode=NULL;
1523 #endif /* ARCH_X86 */
1525 av_freep(&c->yuvTable);
1530 struct SwsContext *sws_getCachedContext(struct SwsContext *context,
1531 int srcW, int srcH, enum PixelFormat srcFormat,
1532 int dstW, int dstH, enum PixelFormat dstFormat, int flags,
1533 SwsFilter *srcFilter, SwsFilter *dstFilter, const double *param)
1535 static const double default_param[2] = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT};
1538 param = default_param;
1540 flags = update_flags_cpu(flags);
1543 (context->srcW != srcW ||
1544 context->srcH != srcH ||
1545 context->srcFormat != srcFormat ||
1546 context->dstW != dstW ||
1547 context->dstH != dstH ||
1548 context->dstFormat != dstFormat ||
1549 context->flags != flags ||
1550 context->param[0] != param[0] ||
1551 context->param[1] != param[1])) {
1552 sws_freeContext(context);
1557 if (!(context = sws_alloc_context()))
1559 context->srcW = srcW;
1560 context->srcH = srcH;
1561 context->srcRange = handle_jpeg(&srcFormat);
1562 context->srcFormat = srcFormat;
1563 context->dstW = dstW;
1564 context->dstH = dstH;
1565 context->dstRange = handle_jpeg(&dstFormat);
1566 context->dstFormat = dstFormat;
1567 context->flags = flags;
1568 context->param[0] = param[0];
1569 context->param[1] = param[1];
1570 sws_setColorspaceDetails(context, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], context->srcRange, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT] /* FIXME*/, context->dstRange, 0, 1<<16, 1<<16);
1571 if (sws_init_context(context, srcFilter, dstFilter) < 0) {
1572 sws_freeContext(context);