/* * Copyright 2020 The LibYuv Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree.
*/
#include"libyuv/scale.h"
#include <assert.h> #include <string.h>
#include"libyuv/cpu_id.h" #include"libyuv/planar_functions.h"// For CopyUV #include"libyuv/row.h" #include"libyuv/scale_row.h"
static __inlineint Abs(int v) { return v >= 0 ? v : -v;
}
// ScaleUV, 1/2 // This is an optimized version for scaling down a UV to 1/2 of // its original size. #if HAS_SCALEUVDOWN2 staticvoid ScaleUVDown2(int src_width, int src_height, int dst_width, int dst_height, int src_stride, int dst_stride, const uint8_t* src_uv,
uint8_t* dst_uv, int x, int dx, int y, int dy, enum FilterMode filtering) { int j; int row_stride = src_stride * (dy >> 16); void (*ScaleUVRowDown2)(const uint8_t* src_uv, ptrdiff_t src_stride,
uint8_t* dst_uv, int dst_width) =
filtering == kFilterNone
? ScaleUVRowDown2_C
: (filtering == kFilterLinear ? ScaleUVRowDown2Linear_C
: ScaleUVRowDown2Box_C);
(void)src_width;
(void)src_height;
(void)dx;
assert(dx == 65536 * 2); // Test scale factor of 2.
assert((dy & 0x1ffff) == 0); // Test vertical scale is multiple of 2. // Advance to odd row, even column. if (filtering == kFilterBilinear) {
src_uv += (y >> 16) * src_stride + (x >> 16) * 2;
} else {
src_uv += (y >> 16) * src_stride + ((x >> 16) - 1) * 2;
}
#ifdefined(HAS_SCALEUVROWDOWN2BOX_SSSE3) if (TestCpuFlag(kCpuHasSSSE3) && filtering) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_SSSE3; if (IS_ALIGNED(dst_width, 4)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_SSSE3;
}
} #endif #ifdefined(HAS_SCALEUVROWDOWN2BOX_AVX2) if (TestCpuFlag(kCpuHasAVX2) && filtering) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_AVX2; if (IS_ALIGNED(dst_width, 8)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_AVX2;
}
} #endif #ifdefined(HAS_SCALEUVROWDOWN2BOX_NEON) if (TestCpuFlag(kCpuHasNEON) && filtering) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_NEON; if (IS_ALIGNED(dst_width, 8)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_NEON;
}
} #endif
// This code is not enabled. Only box filter is available at this time. #ifdefined(HAS_SCALEUVROWDOWN2_SSSE3) if (TestCpuFlag(kCpuHasSSSE3)) {
ScaleUVRowDown2 =
filtering == kFilterNone
? ScaleUVRowDown2_Any_SSSE3
: (filtering == kFilterLinear ? ScaleUVRowDown2Linear_Any_SSSE3
: ScaleUVRowDown2Box_Any_SSSE3); if (IS_ALIGNED(dst_width, 2)) {
ScaleUVRowDown2 =
filtering == kFilterNone
? ScaleUVRowDown2_SSSE3
: (filtering == kFilterLinear ? ScaleUVRowDown2Linear_SSSE3
: ScaleUVRowDown2Box_SSSE3);
}
} #endif // This code is not enabled. Only box filter is available at this time. #ifdefined(HAS_SCALEUVROWDOWN2_NEON) if (TestCpuFlag(kCpuHasNEON)) {
ScaleUVRowDown2 =
filtering == kFilterNone
? ScaleUVRowDown2_Any_NEON
: (filtering == kFilterLinear ? ScaleUVRowDown2Linear_Any_NEON
: ScaleUVRowDown2Box_Any_NEON); if (IS_ALIGNED(dst_width, 8)) {
ScaleUVRowDown2 =
filtering == kFilterNone
? ScaleUVRowDown2_NEON
: (filtering == kFilterLinear ? ScaleUVRowDown2Linear_NEON
: ScaleUVRowDown2Box_NEON);
}
} #endif #ifdefined(HAS_SCALEUVROWDOWN2_MMI) if (TestCpuFlag(kCpuHasMMI)) {
ScaleUVRowDown2 =
filtering == kFilterNone
? ScaleUVRowDown2_Any_MMI
: (filtering == kFilterLinear ? ScaleUVRowDown2Linear_Any_MMI
: ScaleUVRowDown2Box_Any_MMI); if (IS_ALIGNED(dst_width, 2)) {
ScaleUVRowDown2 =
filtering == kFilterNone
? ScaleUVRowDown2_MMI
: (filtering == kFilterLinear ? ScaleUVRowDown2Linear_MMI
: ScaleUVRowDown2Box_MMI);
}
} #endif #ifdefined(HAS_SCALEUVROWDOWN2_MSA) if (TestCpuFlag(kCpuHasMSA)) {
ScaleUVRowDown2 =
filtering == kFilterNone
? ScaleUVRowDown2_Any_MSA
: (filtering == kFilterLinear ? ScaleUVRowDown2Linear_Any_MSA
: ScaleUVRowDown2Box_Any_MSA); if (IS_ALIGNED(dst_width, 2)) {
ScaleUVRowDown2 =
filtering == kFilterNone
? ScaleUVRowDown2_MSA
: (filtering == kFilterLinear ? ScaleUVRowDown2Linear_MSA
: ScaleUVRowDown2Box_MSA);
}
} #endif
// ScaleUV, 1/4 // This is an optimized version for scaling down a UV to 1/4 of // its original size. #if HAS_SCALEUVDOWN4BOX staticvoid ScaleUVDown4Box(int src_width, int src_height, int dst_width, int dst_height, int src_stride, int dst_stride, const uint8_t* src_uv,
uint8_t* dst_uv, int x, int dx, int y, int dy) { int j; // Allocate 2 rows of UV. constint kRowSize = (dst_width * 2 * 2 + 15) & ~15;
align_buffer_64(row, kRowSize * 2); int row_stride = src_stride * (dy >> 16); void (*ScaleUVRowDown2)(const uint8_t* src_uv, ptrdiff_t src_stride,
uint8_t* dst_uv, int dst_width) =
ScaleUVRowDown2Box_C; // Advance to odd row, even column.
src_uv += (y >> 16) * src_stride + (x >> 16) * 2;
(void)src_width;
(void)src_height;
(void)dx;
assert(dx == 65536 * 4); // Test scale factor of 4.
assert((dy & 0x3ffff) == 0); // Test vertical scale is multiple of 4.
#ifdefined(HAS_SCALEUVROWDOWN2BOX_SSSE3) if (TestCpuFlag(kCpuHasSSSE3)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_SSSE3; if (IS_ALIGNED(dst_width, 4)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_SSSE3;
}
} #endif #ifdefined(HAS_SCALEUVROWDOWN2BOX_AVX2) if (TestCpuFlag(kCpuHasAVX2)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_AVX2; if (IS_ALIGNED(dst_width, 8)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_AVX2;
}
} #endif #ifdefined(HAS_SCALEUVROWDOWN2BOX_NEON) if (TestCpuFlag(kCpuHasNEON)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_Any_NEON; if (IS_ALIGNED(dst_width, 8)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_NEON;
}
} #endif
for (j = 0; j < dst_height; ++j) {
yi = y >> 16; if (yi != lasty) { if (y > max_y) {
y = max_y;
yi = y >> 16;
src = src_uv + yi * src_stride;
} if (yi != lasty) {
ScaleUVFilterCols(rowptr, src, dst_width, x, dx);
rowptr += rowstride;
rowstride = -rowstride;
lasty = yi;
src += src_stride;
}
} if (filtering == kFilterLinear) {
InterpolateRow(dst_uv, rowptr, 0, dst_width * 2, 0);
} else { int yf = (y >> 8) & 255;
InterpolateRow(dst_uv, rowptr, rowstride, dst_width * 2, yf);
}
dst_uv += dst_stride;
y += dy;
}
free_aligned_buffer_64(row);
}
} #endif// HAS_SCALEUVBILINEARUP
// Scale UV to/from any dimensions, without interpolation. // Fixed point math is used for performance: The upper 16 bits // of x and dx is the integer part of the source position and // the lower 16 bits are the fixed decimal part.
staticvoid ScaleUVSimple(int src_width, int src_height, int dst_width, int dst_height, int src_stride, int dst_stride, const uint8_t* src_uv,
uint8_t* dst_uv, int x, int dx, int y, int dy) { int j; void (*ScaleUVCols)(uint8_t * dst_uv, const uint8_t* src_uv, int dst_width, int x, int dx) =
(src_width >= 32768) ? ScaleUVCols64_C : ScaleUVCols_C;
(void)src_height; #ifdefined(HAS_SCALEUVCOLS_SSSE3) if (TestCpuFlag(kCpuHasSSSE3) && src_width < 32768) {
ScaleUVCols = ScaleUVCols_SSSE3;
} #endif #ifdefined(HAS_SCALEUVCOLS_NEON) if (TestCpuFlag(kCpuHasNEON)) {
ScaleUVCols = ScaleUVCols_Any_NEON; if (IS_ALIGNED(dst_width, 8)) {
ScaleUVCols = ScaleUVCols_NEON;
}
} #endif #ifdefined(HAS_SCALEUVCOLS_MMI) if (TestCpuFlag(kCpuHasMMI)) {
ScaleUVCols = ScaleUVCols_Any_MMI; if (IS_ALIGNED(dst_width, 1)) {
ScaleUVCols = ScaleUVCols_MMI;
}
} #endif #ifdefined(HAS_SCALEUVCOLS_MSA) if (TestCpuFlag(kCpuHasMSA)) {
ScaleUVCols = ScaleUVCols_Any_MSA; if (IS_ALIGNED(dst_width, 4)) {
ScaleUVCols = ScaleUVCols_MSA;
}
} #endif if (src_width * 2 == dst_width && x < 0x8000) {
ScaleUVCols = ScaleUVColsUp2_C; #ifdefined(HAS_SCALEUVCOLSUP2_SSSE3) if (TestCpuFlag(kCpuHasSSSE3) && IS_ALIGNED(dst_width, 8)) {
ScaleUVCols = ScaleUVColsUp2_SSSE3;
} #endif #ifdefined(HAS_SCALEUVCOLSUP2_MMI) if (TestCpuFlag(kCpuHasMMI) && IS_ALIGNED(dst_width, 4)) {
ScaleUVCols = ScaleUVColsUp2_MMI;
} #endif
}
// Scale a UV plane (from NV12) // This function in turn calls a scaling function // suitable for handling the desired resolutions. staticvoid ScaleUV(const uint8_t* src, int src_stride, int src_width, int src_height,
uint8_t* dst, int dst_stride, int dst_width, int dst_height, int clip_x, int clip_y, int clip_width, int clip_height, enum FilterMode filtering) { // Initial source x/y coordinate and step values as 16.16 fixed point. int x = 0; int y = 0; int dx = 0; int dy = 0; // UV does not support box filter yet, but allow the user to pass it. // Simplify filtering when possible.
filtering = ScaleFilterReduce(src_width, src_height, dst_width, dst_height,
filtering);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.