23 #define _SVID_SOURCE // needed for MAP_ANONYMOUS
24 #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
66 return FFMPEG_CONFIGURATION;
71 #define LICENSE_PREFIX "libswscale license: "
243 #if FF_API_SWS_FORMAT_NAME
250 return "Unknown format";
258 return ((d * dist + c) * dist + b) * dist +
a;
261 b + 2.0 * c + 3.0 * d,
263 -b - 3.0 * c - 6.0 * d,
270 pos = (128 << chr_subsample) - 128;
273 return pos >> chr_subsample;
290 {
SWS_POINT,
"nearest neighbor / point", -1 },
293 {
SWS_X,
"experimental", 8 },
297 int *outFilterSize,
int xInc,
int srcW,
298 int dstW,
int filterAlign,
int one,
299 int flags,
int cpu_flags,
301 double param[2],
int srcPos,
int dstPos)
308 int64_t *filter2 = NULL;
309 const int64_t fone = 1LL << (54 -
FFMIN(
av_log2(srcW/dstW), 8));
317 if (
FFABS(xInc - 0x10000) < 10 && srcPos == dstPos) {
321 dstW,
sizeof(*filter) * filterSize, fail);
323 for (i = 0; i < dstW; i++) {
324 filter[i * filterSize] = fone;
332 dstW,
sizeof(*filter) * filterSize, fail);
334 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
335 for (i = 0; i < dstW; i++) {
336 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
338 (*filterPos)[i] = xx;
342 }
else if ((xInc <= (1 << 16) && (flags &
SWS_AREA)) ||
348 dstW,
sizeof(*filter) * filterSize, fail);
350 xDstInSrc = ((dstPos*(int64_t)xInc)>>8) - ((srcPos*0x8000LL)>>7);
351 for (i = 0; i < dstW; i++) {
352 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
355 (*filterPos)[i] = xx;
357 for (j = 0; j < filterSize; j++) {
358 int64_t
coeff= fone -
FFABS(((int64_t)xx<<16) - xDstInSrc)*(fone>>16);
361 filter[i * filterSize + j] =
coeff;
371 if (flags & scale_algorithms[i].flag && scale_algorithms[i].size_factor > 0) {
381 filterSize = 1 + sizeFactor;
383 filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
385 filterSize =
FFMIN(filterSize, srcW - 2);
386 filterSize =
FFMAX(filterSize, 1);
389 dstW,
sizeof(*filter) * filterSize, fail);
391 xDstInSrc = ((dstPos*(int64_t)xInc)>>7) - ((srcPos*0x10000LL)>>7);
392 for (i = 0; i < dstW; i++) {
393 int xx = (xDstInSrc - ((int64_t)(filterSize - 2) << 16)) / (1 << 17);
395 (*filterPos)[i] = xx;
396 for (j = 0; j < filterSize; j++) {
397 int64_t d = (
FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
403 floatd = d * (1.0 / (1 << 30));
409 if (d >= 1LL << 31) {
412 int64_t dd = (d * d) >> 30;
413 int64_t ddd = (dd * d) >> 30;
416 coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
417 (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
418 (6 * (1 << 24) - 2 * B) * (1 << 30);
420 coeff = (-B - 6 * C) * ddd +
421 (6 * B + 30 * C) * dd +
422 (-12 * B - 48 * C) * d +
423 (8 * B + 24 * C) * (1 << 30);
425 coeff /= (1LL<<54)/fone;
428 else if (flags &
SWS_X) {
429 double p = param ? param * 0.01 : 0.3;
430 coeff = d ? sin(d *
M_PI) / (d *
M_PI) : 1.0;
431 coeff *= pow(2.0, -p * d * d);
434 else if (flags & SWS_X) {
439 c = cos(floatd *
M_PI);
446 coeff = (c * 0.5 + 0.5) * fone;
448 int64_t d2 = d - (1 << 29);
449 if (d2 * xInc < -(1LL << (29 + 16)))
450 coeff = 1.0 * (1LL << (30 + 16));
451 else if (d2 * xInc < (1LL << (29 + 16)))
452 coeff = -d2 * xInc + (1LL << (29 + 16));
455 coeff *= fone >> (30 + 16);
458 coeff = (pow(2.0, -p * floatd * floatd)) * fone;
460 coeff = (d ? sin(floatd *
M_PI) / (floatd *
M_PI) : 1.0) * fone;
461 }
else if (flags & SWS_LANCZOS) {
463 coeff = (d ? sin(floatd *
M_PI) * sin(floatd * M_PI / p) /
464 (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
468 coeff = (1 << 30) - d;
473 double p = -2.196152422706632;
479 filter[i * filterSize + j] =
coeff;
482 xDstInSrc += 2 * xInc;
490 filter2Size = filterSize;
492 filter2Size += srcFilter->
length - 1;
494 filter2Size += dstFilter->
length - 1;
498 for (i = 0; i < dstW; i++) {
502 for (k = 0; k < srcFilter->
length; k++) {
503 for (j = 0; j < filterSize; j++)
504 filter2[i * filter2Size + k + j] +=
505 srcFilter->
coeff[k] * filter[i * filterSize + j];
508 for (j = 0; j < filterSize; j++)
509 filter2[i * filter2Size + j] = filter[i * filterSize + j];
513 (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
520 for (i = dstW - 1; i >= 0; i--) {
521 int min = filter2Size;
523 int64_t cutOff = 0.0;
526 for (j = 0; j < filter2Size; j++) {
528 cutOff +=
FFABS(filter2[i * filter2Size]);
535 if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
539 for (k = 1; k < filter2Size; k++)
540 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
541 filter2[i * filter2Size + k - 1] = 0;
547 for (j = filter2Size - 1; j > 0; j--) {
548 cutOff +=
FFABS(filter2[i * filter2Size + j]);
555 if (min > minFilterSize)
561 if (minFilterSize < 5)
567 if (minFilterSize < 3)
573 if (minFilterSize == 1 && filterAlign == 2)
578 filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
585 av_log(NULL,
AV_LOG_ERROR,
"sws: filterSize %d is too large, try less extreme scaling or set --sws-max-filter-size and recompile\n",
586 FF_CEIL_RSHIFT((filterSize+1) * ((flags & SWS_ACCURATE_RND) ? APCK_SIZE : 16), 4));
589 *outFilterSize = filterSize;
593 "SwScaler: reducing / aligning filtersize %d -> %d\n",
594 filter2Size, filterSize);
596 for (i = 0; i < dstW; i++) {
599 for (j = 0; j < filterSize; j++) {
600 if (j >= filter2Size)
601 filter[i * filterSize + j] = 0;
603 filter[i * filterSize + j] = filter2[i * filter2Size + j];
605 filter[i * filterSize + j] = 0;
612 for (i = 0; i < dstW; i++) {
614 if ((*filterPos)[i] < 0) {
616 for (j = 1; j < filterSize; j++) {
617 int left =
FFMAX(j + (*filterPos)[i], 0);
618 filter[i * filterSize + left] += filter[i * filterSize + j];
619 filter[i * filterSize + j] = 0;
624 if ((*filterPos)[i] + filterSize > srcW) {
625 int shift = (*filterPos)[i] + filterSize - srcW;
627 for (j = filterSize - 2; j >= 0; j--) {
628 int right =
FFMIN(j + shift, filterSize - 1);
629 filter[i * filterSize + right] += filter[i * filterSize + j];
630 filter[i * filterSize + j] = 0;
632 (*filterPos)[i]= srcW - filterSize;
639 (dstW + 3), *outFilterSize *
sizeof(int16_t), fail);
642 for (i = 0; i < dstW; i++) {
647 for (j = 0; j < filterSize; j++) {
648 sum += filter[i * filterSize + j];
650 sum = (sum + one / 2) / one;
655 for (j = 0; j < *outFilterSize; j++) {
656 int64_t
v = filter[i * filterSize + j] + error;
658 (*outFilter)[i * (*outFilterSize) + j] = intV;
659 error = v - intV * sum;
663 (*filterPos)[dstW + 0] =
664 (*filterPos)[dstW + 1] =
665 (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1];
667 for (i = 0; i < *outFilterSize; i++) {
668 int k = (dstW - 1) * (*outFilterSize) + i;
669 (*outFilter)[k + 1 * (*outFilterSize)] =
670 (*outFilter)[k + 2 * (*outFilterSize)] =
671 (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
686 int64_t
W,
V, Z, Cy, Cu, Cv;
687 int64_t vr = table[0];
688 int64_t ub = table[1];
689 int64_t ug = -table[2];
690 int64_t vg = -table[3];
695 static const int8_t map[] = {
720 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
721 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
722 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
723 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
724 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
725 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
726 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
727 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
764 c->input_rgb2yuv_table[
BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
765 c->input_rgb2yuv_table[
BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
766 c->input_rgb2yuv_table[
BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
767 c->input_rgb2yuv_table[
GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
768 c->input_rgb2yuv_table[
GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
769 c->input_rgb2yuv_table[
GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
770 c->input_rgb2yuv_table[
RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
771 c->input_rgb2yuv_table[
RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
772 c->input_rgb2yuv_table[
RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
775 AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ?
c->input_rgb2yuv_table[map[i]] : 0);
785 static const int16_t xyz2rgb_matrix[3][4] = {
786 {13270, -6295, -2041},
788 { 228, -835, 4329} };
789 static const int16_t rgb2xyz_matrix[3][4] = {
793 static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096], xyzgammainv_tab[4096], rgbgammainv_tab[4096];
802 if (rgbgamma_tab[4095])
806 for (i = 0; i < 4096; i++) {
807 xyzgamma_tab[i] =
lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
808 rgbgamma_tab[i] =
lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
809 xyzgammainv_tab[i] =
lrint(pow(i / 4095.0, xyzgammainv) * 4095.0);
810 rgbgammainv_tab[i] =
lrint(pow(i / 4095.0, rgbgammainv) * 4095.0);
815 int srcRange,
const int table[4],
int dstRange,
816 int brightness,
int contrast,
int saturation)
854 contrast, saturation);
859 contrast, saturation);
868 int *srcRange,
int **
table,
int *dstRange,
869 int *brightness,
int *contrast,
int *saturation)
960 int usesVFilter, usesHFilter;
962 SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
967 int dst_stride =
FFALIGN(dstW *
sizeof(int16_t) + 66, 16);
968 int flags, cpu_flags;
980 unscaled = (srcW == dstW && srcH == dstH);
986 av_log(c,
AV_LOG_WARNING,
"deprecated pixel format used, make sure you did set range correctly\n");
1027 if (dstW < srcW && dstH < srcH)
1029 else if (dstW > srcW && dstH > srcH)
1034 }
else if (i & (i - 1)) {
1036 "Exactly one scaler algorithm must be chosen, got %X\n", i);
1040 if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
1044 srcW, srcH, dstW, dstH);
1049 dstFilter = &dummyFilter;
1051 srcFilter = &dummyFilter;
1053 c->
lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
1054 c->
lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
1057 c->
vRounder = 4 * 0x0001000100010001ULL;
1059 usesVFilter = (srcFilter->
lumV && srcFilter->
lumV->
length > 1) ||
1063 usesHFilter = (srcFilter->
lumH && srcFilter->
lumH->
length > 1) ||
1083 av_log(c,
AV_LOG_DEBUG,
"Forcing full internal H chroma due to input having non subsampled chroma\n");
1100 if (!(flags & SWS_FULL_CHR_H_INT)) {
1103 "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
1109 if (flags & SWS_FULL_CHR_H_INT) {
1112 "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
1119 if (!(flags & SWS_FULL_CHR_H_INT)) {
1121 "%s output is not supported with half chroma resolution, switching to full\n",
1130 if (flags & SWS_FULL_CHR_H_INT &&
1145 "full chroma interpolation for destination format '%s' not yet implemented\n",
1147 flags &= ~SWS_FULL_CHR_H_INT;
1150 if (
isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1201 "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1218 if (flags & SWS_FAST_BILINEAR) {
1225 c->
lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1230 #define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
1234 #if HAVE_MMXEXT_INLINE
1240 NULL, NULL, NULL, 4);
1244 PROT_READ | PROT_WRITE,
1245 MAP_PRIVATE | MAP_ANONYMOUS,
1248 PROT_READ | PROT_WRITE,
1249 MAP_PRIVATE | MAP_ANONYMOUS,
1251 #elif HAVE_VIRTUALALLOC
1255 PAGE_EXECUTE_READWRITE);
1259 PAGE_EXECUTE_READWRITE);
1265 #ifdef MAP_ANONYMOUS
1295 const int filterAlign =
X86_MMX(cpu_flags) ? 4 :
1300 srcW, dstW, filterAlign, 1 << 14,
1302 cpu_flags, srcFilter->
lumH, dstFilter->
lumH,
1310 (flags & SWS_BICUBLIN) ? (flags |
SWS_BILINEAR) : flags,
1311 cpu_flags, srcFilter->
chrH, dstFilter->
chrH,
1321 const int filterAlign =
X86_MMX(cpu_flags) ? 2 :
1325 c->
lumYInc, srcH, dstH, filterAlign, (1 << 12),
1327 cpu_flags, srcFilter->
lumV, dstFilter->
lumV,
1334 filterAlign, (1 << 12),
1335 (flags & SWS_BICUBLIN) ? (flags |
SWS_BILINEAR) : flags,
1336 cpu_flags, srcFilter->
chrV, dstFilter->
chrV,
1349 short *p = (
short *)&c->vYCoeffsBank[i];
1350 for (j = 0; j < 8; j++)
1356 short *p = (
short *)&c->vCCoeffsBank[i];
1357 for (j = 0; j < 8; j++)
1366 for (i = 0; i < dstH; i++) {
1367 int chrI = (int64_t)i * c->
chrDstH / dstH;
1382 for (i = 0; i < 4; i++)
1397 dst_stride + 16, fail);
1405 dst_stride * 2 + 32, fail);
1410 if (CONFIG_SWSCALE_ALPHA && c->
alpPixBuf)
1413 dst_stride + 16, fail);
1421 for(j=0; j<dst_stride/2+1; j++)
1424 for(j=0; j<dst_stride+1; j++)
1430 const char *scaler = NULL, *cpucaps;
1433 if (flags & scale_algorithms[i].flag) {
1439 scaler =
"ehh flags invalid?!";
1460 cpucaps =
"AltiVec";
1468 "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1471 "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1477 if (unscaled && !usesHFilter && !usesVFilter &&
1482 if (flags & SWS_PRINT_INFO)
1484 "using unscaled %s -> %s special converter\n",
1499 SwsFilter *dstFilter,
const double *param)
1515 c->
param[0] = param[0];
1516 c->
param[1] = param[1];
1528 float lumaSharpen,
float chromaSharpen,
1529 float chromaHShift,
float chromaVShift,
1536 if (lumaGBlur != 0.0) {
1544 if (chromaGBlur != 0.0) {
1552 if (chromaSharpen != 0.0) {
1561 if (lumaSharpen != 0.0) {
1570 if (chromaHShift != 0.0)
1573 if (chromaVShift != 0.0)
1608 const int length = (int)(variance * quality + 0.5) | 1;
1610 double middle = (length - 1) * 0.5;
1613 if(variance < 0 || quality < 0)
1621 for (i = 0; i <
length; i++) {
1622 double dist = i - middle;
1623 vec->
coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
1624 sqrt(2 * variance *
M_PI);
1640 for (i = 0; i <
length; i++)
1656 for (i = 0; i < a->
length; i++)
1666 for (i = 0; i < a->
length; i++)
1667 a->
coeff[i] *= scalar;
1684 for (i = 0; i < a->
length; i++) {
1685 for (j = 0; j < b->
length; j++) {
1702 for (i = 0; i < a->
length; i++)
1719 for (i = 0; i < a->
length; i++)
1737 for (i = 0; i < a->
length; i++) {
1738 vec->
coeff[i + (length - 1) / 2 -
1800 for (i = 0; i < a->
length; i++)
1801 if (a->
coeff[i] > max)
1804 for (i = 0; i < a->
length; i++)
1805 if (a->
coeff[i] < min)
1810 for (i = 0; i < a->
length; i++) {
1811 int x = (int)((a->
coeff[i] - min) * 60.0 / range + 0.5);
1812 av_log(log_ctx, log_level,
"%1.3f ", a->
coeff[i]);
1814 av_log(log_ctx, log_level,
" ");
1815 av_log(log_ctx, log_level,
"|\n");
1859 if (CONFIG_SWSCALE_ALPHA && c->
alpPixBuf) {
1865 for (i = 0; i < 4; i++)
1888 #elif HAVE_VIRTUALALLOC
1913 const double *
param)
1919 param = default_param;
1922 (context->
srcW != srcW ||
1923 context->
srcH != srcH ||
1925 context->
dstW != dstW ||
1926 context->
dstH != dstH ||
1928 context->
flags != flags ||
1929 context->
param[0] != param[0] ||
1930 context->
param[1] != param[1])) {
1945 context->
param[0] = param[0];
1946 context->
param[1] = param[1];