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
63 return FFMPEG_CONFIGURATION;
68 #define LICENSE_PREFIX "libswscale license: "
72 #define RET 0xC3 // near return opcode for x86
228 #if FF_API_SWS_FORMAT_NAME
235 return "Unknown format";
243 return ((d * dist + c) * dist + b) * dist +
a;
246 b + 2.0 * c + 3.0 * d,
248 -b - 3.0 * c - 6.0 * d,
253 int *outFilterSize,
int xInc,
int srcW,
254 int dstW,
int filterAlign,
int one,
264 int64_t *filter2 =
NULL;
265 const int64_t fone = 1LL << (54 -
FFMIN(
av_log2(srcW/dstW), 8));
273 if (
FFABS(xInc - 0x10000) < 10) {
277 dstW *
sizeof(*filter) * filterSize, fail);
279 for (i = 0; i < dstW; i++) {
280 filter[i * filterSize] = fone;
288 dstW *
sizeof(*filter) * filterSize, fail);
290 xDstInSrc = xInc / 2 - 0x8000;
291 for (i = 0; i < dstW; i++) {
292 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
294 (*filterPos)[i] = xx;
298 }
else if ((xInc <= (1 << 16) && (flags &
SWS_AREA)) ||
304 dstW *
sizeof(*filter) * filterSize, fail);
306 xDstInSrc = xInc / 2 - 0x8000;
307 for (i = 0; i < dstW; i++) {
308 int xx = (xDstInSrc - ((filterSize - 1) << 15) + (1 << 15)) >> 16;
311 (*filterPos)[i] = xx;
313 for (j = 0; j < filterSize; j++) {
314 int64_t
coeff= fone -
FFABS(((int64_t)xx<<16) - xDstInSrc)*(fone>>16);
317 filter[i * filterSize + j] =
coeff;
328 else if (flags &
SWS_X)
347 filterSize = 1 + sizeFactor;
349 filterSize = 1 + (sizeFactor * srcW + dstW - 1) / dstW;
351 filterSize =
FFMIN(filterSize, srcW - 2);
352 filterSize =
FFMAX(filterSize, 1);
355 dstW *
sizeof(*filter) * filterSize, fail);
357 xDstInSrc = xInc - 0x10000;
358 for (i = 0; i < dstW; i++) {
359 int xx = (xDstInSrc - ((filterSize - 2) << 16)) / (1 << 17);
361 (*filterPos)[i] = xx;
362 for (j = 0; j < filterSize; j++) {
363 int64_t d = (
FFABS(((int64_t)xx << 17) - xDstInSrc)) << 13;
369 floatd = d * (1.0 / (1 << 30));
371 if (flags & SWS_BICUBIC) {
375 if (d >= 1LL << 31) {
378 int64_t dd = (d * d) >> 30;
379 int64_t ddd = (dd * d) >> 30;
382 coeff = (12 * (1 << 24) - 9 * B - 6 * C) * ddd +
383 (-18 * (1 << 24) + 12 * B + 6 * C) * dd +
384 (6 * (1 << 24) - 2 * B) * (1 << 30);
386 coeff = (-B - 6 * C) * ddd +
387 (6 * B + 30 * C) * dd +
388 (-12 * B - 48 * C) * d +
389 (8 * B + 24 * C) * (1 << 30);
391 coeff /= (1LL<<54)/fone;
394 else if (flags & SWS_X) {
395 double p = param ? param * 0.01 : 0.3;
396 coeff = d ? sin(d *
M_PI) / (d *
M_PI) : 1.0;
397 coeff *= pow(2.0, -p * d * d);
400 else if (flags & SWS_X) {
405 c = cos(floatd *
M_PI);
412 coeff = (c * 0.5 + 0.5) * fone;
413 }
else if (flags & SWS_AREA) {
414 int64_t d2 = d - (1 << 29);
415 if (d2 * xInc < -(1LL << (29 + 16)))
416 coeff = 1.0 * (1LL << (30 + 16));
417 else if (d2 * xInc < (1LL << (29 + 16)))
418 coeff = -d2 * xInc + (1LL << (29 + 16));
421 coeff *= fone >> (30 + 16);
422 }
else if (flags & SWS_GAUSS) {
424 coeff = (pow(2.0, -p * floatd * floatd)) * fone;
425 }
else if (flags & SWS_SINC) {
426 coeff = (d ? sin(floatd *
M_PI) / (floatd *
M_PI) : 1.0) * fone;
427 }
else if (flags & SWS_LANCZOS) {
429 coeff = (d ? sin(floatd *
M_PI) * sin(floatd * M_PI / p) /
430 (floatd * floatd * M_PI * M_PI / p) : 1.0) * fone;
433 }
else if (flags & SWS_BILINEAR) {
434 coeff = (1 << 30) - d;
438 }
else if (flags & SWS_SPLINE) {
439 double p = -2.196152422706632;
445 filter[i * filterSize + j] =
coeff;
448 xDstInSrc += 2 * xInc;
456 filter2Size = filterSize;
458 filter2Size += srcFilter->
length - 1;
460 filter2Size += dstFilter->
length - 1;
464 for (i = 0; i < dstW; i++) {
468 for (k = 0; k < srcFilter->
length; k++) {
469 for (j = 0; j < filterSize; j++)
470 filter2[i * filter2Size + k + j] +=
471 srcFilter->
coeff[k] * filter[i * filterSize + j];
474 for (j = 0; j < filterSize; j++)
475 filter2[i * filter2Size + j] = filter[i * filterSize + j];
479 (*filterPos)[i] += (filterSize - 1) / 2 - (filter2Size - 1) / 2;
486 for (i = dstW - 1; i >= 0; i--) {
487 int min = filter2Size;
489 int64_t cutOff = 0.0;
492 for (j = 0; j < filter2Size; j++) {
494 cutOff +=
FFABS(filter2[i * filter2Size]);
501 if (i < dstW - 1 && (*filterPos)[i] >= (*filterPos)[i + 1])
505 for (k = 1; k < filter2Size; k++)
506 filter2[i * filter2Size + k - 1] = filter2[i * filter2Size + k];
507 filter2[i * filter2Size + k - 1] = 0;
513 for (j = filter2Size - 1; j > 0; j--) {
514 cutOff +=
FFABS(filter2[i * filter2Size + j]);
521 if (min > minFilterSize)
527 if (minFilterSize < 5)
533 if (minFilterSize < 3)
539 if (minFilterSize == 1 && filterAlign == 2)
544 filterSize = (minFilterSize + (filterAlign - 1)) & (~(filterAlign - 1));
546 filter =
av_malloc(filterSize * dstW *
sizeof(*filter));
549 av_log(
NULL,
AV_LOG_ERROR,
"sws: filterSize %d is too large, try less extreem scaling or increase MAX_FILTER_SIZE and recompile\n", filterSize);
552 *outFilterSize = filterSize;
556 "SwScaler: reducing / aligning filtersize %d -> %d\n",
557 filter2Size, filterSize);
559 for (i = 0; i < dstW; i++) {
562 for (j = 0; j < filterSize; j++) {
563 if (j >= filter2Size)
564 filter[i * filterSize + j] = 0;
566 filter[i * filterSize + j] = filter2[i * filter2Size + j];
568 filter[i * filterSize + j] = 0;
575 for (i = 0; i < dstW; i++) {
577 if ((*filterPos)[i] < 0) {
579 for (j = 1; j < filterSize; j++) {
580 int left =
FFMAX(j + (*filterPos)[i], 0);
581 filter[i * filterSize + left] += filter[i * filterSize + j];
582 filter[i * filterSize + j] = 0;
587 if ((*filterPos)[i] + filterSize > srcW) {
588 int shift = (*filterPos)[i] + filterSize - srcW;
590 for (j = filterSize - 2; j >= 0; j--) {
591 int right =
FFMIN(j + shift, filterSize - 1);
592 filter[i * filterSize + right] += filter[i * filterSize + j];
593 filter[i * filterSize + j] = 0;
595 (*filterPos)[i]= srcW - filterSize;
602 *outFilterSize * (dstW + 3) *
sizeof(int16_t), fail);
605 for (i = 0; i < dstW; i++) {
610 for (j = 0; j < filterSize; j++) {
611 sum += filter[i * filterSize + j];
613 sum = (sum + one / 2) / one;
614 for (j = 0; j < *outFilterSize; j++) {
615 int64_t
v = filter[i * filterSize + j] + error;
617 (*outFilter)[i * (*outFilterSize) + j] = intV;
618 error = v - intV * sum;
622 (*filterPos)[dstW + 0] =
623 (*filterPos)[dstW + 1] =
624 (*filterPos)[dstW + 2] = (*filterPos)[dstW - 1];
626 for (i = 0; i < *outFilterSize; i++) {
627 int k = (dstW - 1) * (*outFilterSize) + i;
628 (*outFilter)[k + 1 * (*outFilterSize)] =
629 (*outFilter)[k + 2 * (*outFilterSize)] =
630 (*outFilter)[k + 3 * (*outFilterSize)] = (*outFilter)[k];
643 #if HAVE_MMXEXT_INLINE
644 static av_cold int init_hscaler_mmxext(
int dstW,
int xInc,
uint8_t *filterCode,
673 "movq (%%"REG_d
", %%"REG_a
"), %%mm3 \n\t"
674 "movd (%%"REG_c
", %%"REG_S
"), %%mm0 \n\t"
675 "movd 1(%%"REG_c
", %%"REG_S
"), %%mm1 \n\t"
676 "punpcklbw %%mm7, %%mm1 \n\t"
677 "punpcklbw %%mm7, %%mm0 \n\t"
678 "pshufw $0xFF, %%mm1, %%mm1 \n\t"
680 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
682 "psubw %%mm1, %%mm0 \n\t"
683 "movl 8(%%"REG_b
", %%"REG_a
"), %%esi \n\t"
684 "pmullw %%mm3, %%mm0 \n\t"
685 "psllw $7, %%mm1 \n\t"
686 "paddw %%mm1, %%mm0 \n\t"
688 "movq %%mm0, (%%"REG_D
", %%"REG_a
") \n\t"
690 "add $8, %%"REG_a
" \n\t"
705 :
"=r" (fragmentA),
"=r" (imm8OfPShufW1A),
"=r" (imm8OfPShufW2A),
706 "=r" (fragmentLengthA)
713 "movq (%%"REG_d
", %%"REG_a
"), %%mm3 \n\t"
714 "movd (%%"REG_c
", %%"REG_S
"), %%mm0 \n\t"
715 "punpcklbw %%mm7, %%mm0 \n\t"
716 "pshufw $0xFF, %%mm0, %%mm1 \n\t"
718 "pshufw $0xFF, %%mm0, %%mm0 \n\t"
720 "psubw %%mm1, %%mm0 \n\t"
721 "movl 8(%%"REG_b
", %%"REG_a
"), %%esi \n\t"
722 "pmullw %%mm3, %%mm0 \n\t"
723 "psllw $7, %%mm1 \n\t"
724 "paddw %%mm1, %%mm0 \n\t"
726 "movq %%mm0, (%%"REG_D
", %%"REG_a
") \n\t"
728 "add $8, %%"REG_a
" \n\t"
743 :
"=r" (fragmentB),
"=r" (imm8OfPShufW1B),
"=r" (imm8OfPShufW2B),
744 "=r" (fragmentLengthB)
750 for (i = 0; i < dstW / numSplits; i++) {
755 int b = ((xpos + xInc) >> 16) - xx;
756 int c = ((xpos + xInc * 2) >> 16) - xx;
757 int d = ((xpos + xInc * 3) >> 16) - xx;
758 int inc = (d + 1 < 4);
759 uint8_t *fragment = (d + 1 < 4) ? fragmentB : fragmentA;
760 x86_reg imm8OfPShufW1 = (d + 1 < 4) ? imm8OfPShufW1B : imm8OfPShufW1A;
761 x86_reg imm8OfPShufW2 = (d + 1 < 4) ? imm8OfPShufW2B : imm8OfPShufW2A;
762 x86_reg fragmentLength = (d + 1 < 4) ? fragmentLengthB : fragmentLengthA;
763 int maxShift = 3 - (d + inc);
767 filter[i] = ((xpos & 0xFFFF) ^ 0xFFFF) >> 9;
768 filter[i + 1] = (((xpos + xInc) & 0xFFFF) ^ 0xFFFF) >> 9;
769 filter[i + 2] = (((xpos + xInc * 2) & 0xFFFF) ^ 0xFFFF) >> 9;
770 filter[i + 3] = (((xpos + xInc * 3) & 0xFFFF) ^ 0xFFFF) >> 9;
771 filterPos[i / 2] = xx;
773 memcpy(filterCode + fragmentPos, fragment, fragmentLength);
775 filterCode[fragmentPos + imm8OfPShufW1] = (a + inc) |
779 filterCode[fragmentPos + imm8OfPShufW2] = a | (b << 2) |
783 if (i + 4 - inc >= dstW)
785 else if ((filterPos[i / 2] & 3) <= maxShift)
786 shift = filterPos[i / 2] & 3;
788 if (shift && i >= shift) {
789 filterCode[fragmentPos + imm8OfPShufW1] += 0x55 *
shift;
790 filterCode[fragmentPos + imm8OfPShufW2] += 0x55 *
shift;
791 filterPos[i / 2] -=
shift;
795 fragmentPos += fragmentLength;
798 filterCode[fragmentPos] =
RET;
803 filterPos[((i / 2) + 1) & (~1)] = xpos >> 16;
805 return fragmentPos + 1;
818 int64_t
W,
V, Z, Cy, Cu, Cv;
819 int64_t vr = table[0];
820 int64_t ub = table[1];
821 int64_t ug = -table[2];
822 int64_t vg = -table[3];
827 static const int8_t map[] = {
852 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
853 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
854 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
855 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
856 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
857 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
858 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
859 -1 , -1 , -1 , -1 , -1 , -1 , -1 , -1 ,
896 c->input_rgb2yuv_table[
BY_IDX] = ((int)(0.114 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
897 c->input_rgb2yuv_table[
BV_IDX] = (-(int)(0.081 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
898 c->input_rgb2yuv_table[
BU_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
899 c->input_rgb2yuv_table[
GY_IDX] = ((int)(0.587 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
900 c->input_rgb2yuv_table[
GV_IDX] = (-(int)(0.419 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
901 c->input_rgb2yuv_table[
GU_IDX] = (-(int)(0.331 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
902 c->input_rgb2yuv_table[
RY_IDX] = ((int)(0.299 * 219 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
903 c->input_rgb2yuv_table[
RV_IDX] = ((int)(0.500 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
904 c->input_rgb2yuv_table[
RU_IDX] = (-(int)(0.169 * 224 / 255 * (1 << RGB2YUV_SHIFT) + 0.5));
907 AV_WL16(p + 16*4 + 2*i, map[i] >= 0 ? c->input_rgb2yuv_table[map[i]] : 0);
915 static const int16_t xyz2rgb_matrix[3][4] = {
916 {13270, -6295, -2041},
918 { 228, -835, 4329} };
919 static int16_t xyzgamma_tab[4096], rgbgamma_tab[4096];
925 if (rgbgamma_tab[4095])
929 for (i = 0; i < 4096; i++) {
930 xyzgamma_tab[i] =
lrint(pow(i / 4095.0, xyzgamma) * 4095.0);
931 rgbgamma_tab[i] =
lrint(pow(i / 4095.0, rgbgamma) * 4095.0);
936 int srcRange,
const int table[4],
int dstRange,
937 int brightness,
int contrast,
int saturation)
965 contrast, saturation);
970 contrast, saturation);
979 int *srcRange,
int **
table,
int *dstRange,
980 int *brightness,
int *contrast,
int *saturation)
1052 int usesVFilter, usesHFilter;
1059 int dst_stride =
FFALIGN(dstW *
sizeof(int16_t) + 66, 16);
1072 unscaled = (srcW == dstW && srcH == dstH);
1077 av_log(c,
AV_LOG_WARNING,
"deprecated pixel format used, make sure you did set range correctly\n");
1113 if (!i || (i & (i - 1))) {
1118 if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
1122 srcW, srcH, dstW, dstH);
1127 dstFilter = &dummyFilter;
1129 srcFilter = &dummyFilter;
1131 c->
lumXInc = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
1132 c->
lumYInc = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
1135 c->
vRounder = 4 * 0x0001000100010001ULL;
1137 usesVFilter = (srcFilter->
lumV && srcFilter->
lumV->
length > 1) ||
1141 usesHFilter = (srcFilter->
lumH && srcFilter->
lumH->
length > 1) ||
1163 "Error diffusion dither is only supported in full chroma interpolation for destination format '%s'\n",
1170 "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
1177 if (!(flags & SWS_FULL_CHR_H_INT)) {
1179 "%s output is not supported with half chroma resolution, switching to full\n",
1188 if (flags & SWS_FULL_CHR_H_INT &&
1203 "full chroma interpolation for destination format '%s' not yet implemented\n",
1205 flags &= ~SWS_FULL_CHR_H_INT;
1208 if (
isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
1240 if (unscaled && !usesHFilter && !usesVFilter &&
1247 "using unscaled %s -> %s special converter\n",
1266 (srcW & 15) == 0) ? 1 : 0;
1272 "output width is not a multiple of 32 -> no MMXEXT scaler\n");
1289 if (flags & SWS_FAST_BILINEAR) {
1296 c->
lumXInc = ((int64_t)(srcW - 2) << 16) / (dstW - 2) - 20;
1301 #define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
1305 #if HAVE_MMXEXT_INLINE
1315 PROT_READ | PROT_WRITE,
1316 MAP_PRIVATE | MAP_ANONYMOUS,
1319 PROT_READ | PROT_WRITE,
1320 MAP_PRIVATE | MAP_ANONYMOUS,
1322 #elif HAVE_VIRTUALALLOC
1326 PAGE_EXECUTE_READWRITE);
1330 PAGE_EXECUTE_READWRITE);
1336 #ifdef MAP_ANONYMOUS
1363 const int filterAlign =
1370 srcW, dstW, filterAlign, 1 << 14,
1372 cpu_flags, srcFilter->
lumH, dstFilter->
lumH,
1378 (flags & SWS_BICUBLIN) ? (flags |
SWS_BILINEAR) : flags,
1379 cpu_flags, srcFilter->
chrH, dstFilter->
chrH,
1387 const int filterAlign =
1393 c->
lumYInc, srcH, dstH, filterAlign, (1 << 12),
1400 filterAlign, (1 << 12),
1412 short *p = (
short *)&c->vYCoeffsBank[i];
1413 for (j = 0; j < 8; j++)
1419 short *p = (
short *)&c->vCCoeffsBank[i];
1420 for (j = 0; j < 8; j++)
1429 for (i = 0; i < dstH; i++) {
1430 int chrI = (int64_t)i * c->
chrDstH / dstH;
1445 for (i = 0; i < 4; i++)
1460 dst_stride + 16, fail);
1468 dst_stride * 2 + 32, fail);
1473 if (CONFIG_SWSCALE_ALPHA && c->
alpPixBuf)
1476 dst_stride + 16, fail);
1484 for(j=0; j<dst_stride/2+1; j++)
1487 for(j=0; j<dst_stride+1; j++)
1493 if (flags & SWS_FAST_BILINEAR)
1499 else if (flags &
SWS_X)
1543 "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1546 "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
1557 #if FF_API_SWS_GETCONTEXT
1561 SwsFilter *dstFilter,
const double *param)
1583 c->
param[0] = param[0];
1584 c->
param[1] = param[1];
1600 float lumaSharpen,
float chromaSharpen,
1601 float chromaHShift,
float chromaVShift,
1608 if (lumaGBlur != 0.0) {
1616 if (chromaGBlur != 0.0) {
1624 if (chromaSharpen != 0.0) {
1633 if (lumaSharpen != 0.0) {
1642 if (chromaHShift != 0.0)
1645 if (chromaVShift != 0.0)
1665 if(length <= 0 || length > INT_MAX/
sizeof(
double))
1680 const int length = (int)(variance * quality + 0.5) | 1;
1682 double middle = (length - 1) * 0.5;
1685 if(variance < 0 || quality < 0)
1693 for (i = 0; i <
length; i++) {
1694 double dist = i - middle;
1695 vec->
coeff[i] = exp(-dist * dist / (2 * variance * variance)) /
1696 sqrt(2 * variance *
M_PI);
1712 for (i = 0; i <
length; i++)
1728 for (i = 0; i < a->
length; i++)
1738 for (i = 0; i < a->
length; i++)
1739 a->
coeff[i] *= scalar;
1756 for (i = 0; i < a->
length; i++) {
1757 for (j = 0; j < b->
length; j++) {
1774 for (i = 0; i < a->
length; i++)
1791 for (i = 0; i < a->
length; i++)
1809 for (i = 0; i < a->
length; i++) {
1810 vec->
coeff[i + (length - 1) / 2 -
1861 for (i = 0; i < a->
length; i++)
1874 for (i = 0; i < a->
length; i++)
1875 if (a->
coeff[i] > max)
1878 for (i = 0; i < a->
length; i++)
1879 if (a->
coeff[i] < min)
1884 for (i = 0; i < a->
length; i++) {
1885 int x = (int)((a->
coeff[i] - min) * 60.0 / range + 0.5);
1886 av_log(log_ctx, log_level,
"%1.3f ", a->
coeff[i]);
1888 av_log(log_ctx, log_level,
" ");
1889 av_log(log_ctx, log_level,
"|\n");
1937 if (CONFIG_SWSCALE_ALPHA && c->
alpPixBuf) {
1943 for (i = 0; i < 4; i++)
1966 #elif HAVE_VIRTUALALLOC
1991 const double *
param)
1997 param = default_param;
2000 (context->
srcW != srcW ||
2001 context->
srcH != srcH ||
2003 context->
dstW != dstW ||
2004 context->
dstH != dstH ||
2006 context->
flags != flags ||
2007 context->
param[0] != param[0] ||
2008 context->
param[1] != param[1])) {
2029 context->
param[0] = param[0];
2030 context->
param[1] = param[1];
2034 context->
dstRange, 0, 1 << 16, 1 << 16);