/* * 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_uv.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) * (intptr_t)src_stride + (x >> 16) * 2;
} else {
src_uv += (y >> 16) * (intptr_t)src_stride + ((x >> 16) - 1) * 2;
}
// ScaleUV, 1/4 // This is an optimized version for scaling down a UV to 1/4 of // its original size. #if HAS_SCALEUVDOWN4BOX staticint 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 row_size = (dst_width * 2 * 2 + 15) & ~15;
align_buffer_64(row, row_size * 2); if (!row) return 1; 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) * (intptr_t)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 #ifdefined(HAS_SCALEUVROWDOWN2BOX_SME) if (TestCpuFlag(kCpuHasSME)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_SME;
} #endif #ifdefined(HAS_SCALEUVROWDOWN2BOX_RVV) if (TestCpuFlag(kCpuHasRVV)) {
ScaleUVRowDown2 = ScaleUVRowDown2Box_RVV;
} #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 * (intptr_t)src_stride;
} if (yi != lasty) {
ScaleUVFilterCols(rowptr, src, dst_width, x, dx);
rowptr += rowstride;
rowstride = -rowstride;
lasty = yi; if ((y + 65536) < max_y) {
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);
} return 0;
} #endif// HAS_SCALEUVBILINEARUP
// Scale UV, horizontally up by 2 times. // Uses linear filter horizontally, nearest vertically. // This is an optimized version for scaling up a plane to 2 times of // its original width, using linear interpolation. // This is used to scale U and V planes of NV16 to NV24. staticvoid ScaleUVLinearUp2(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) { void (*ScaleRowUp)(const uint8_t* src_uv, uint8_t* dst_uv, int dst_width) =
ScaleUVRowUp2_Linear_Any_C; int i; int y; int dy;
// This function can only scale up by 2 times horizontally.
(void)src_width;
assert(src_width == ((dst_width + 1) / 2));
#ifdef HAS_SCALEUVROWUP2_LINEAR_SSSE3 if (TestCpuFlag(kCpuHasSSSE3)) {
ScaleRowUp = ScaleUVRowUp2_Linear_Any_SSSE3;
} #endif
#ifdef HAS_SCALEUVROWUP2_LINEAR_AVX2 if (TestCpuFlag(kCpuHasAVX2)) {
ScaleRowUp = ScaleUVRowUp2_Linear_Any_AVX2;
} #endif
#ifdef HAS_SCALEUVROWUP2_LINEAR_NEON if (TestCpuFlag(kCpuHasNEON)) {
ScaleRowUp = ScaleUVRowUp2_Linear_Any_NEON;
} #endif
#ifdef HAS_SCALEUVROWUP2_LINEAR_RVV if (TestCpuFlag(kCpuHasRVV)) {
ScaleRowUp = ScaleUVRowUp2_Linear_RVV;
} #endif
if (dst_height == 1) {
ScaleRowUp(src_uv + ((src_height - 1) / 2) * (intptr_t)src_stride, dst_uv,
dst_width);
} else {
dy = FixedDiv(src_height - 1, dst_height - 1);
y = (1 << 15) - 1; for (i = 0; i < dst_height; ++i) {
ScaleRowUp(src_uv + (y >> 16) * (intptr_t)src_stride, dst_uv, dst_width);
dst_uv += dst_stride;
y += dy;
}
}
}
// Scale plane, up by 2 times. // This is an optimized version for scaling up a plane to 2 times of // its original size, using bilinear interpolation. // This is used to scale U and V planes of NV12 to NV24. staticvoid ScaleUVBilinearUp2(int src_width, int src_height, int dst_width, int dst_height, int src_stride, int dst_stride, const uint8_t* src_ptr,
uint8_t* dst_ptr) { void (*Scale2RowUp)(const uint8_t* src_ptr, ptrdiff_t src_stride,
uint8_t* dst_ptr, ptrdiff_t dst_stride, int dst_width) =
ScaleUVRowUp2_Bilinear_Any_C; int x;
// This function can only scale up by 2 times.
(void)src_width;
assert(src_width == ((dst_width + 1) / 2));
assert(src_height == ((dst_height + 1) / 2));
#ifdef HAS_SCALEUVROWUP2_BILINEAR_SSSE3 if (TestCpuFlag(kCpuHasSSSE3)) {
Scale2RowUp = ScaleUVRowUp2_Bilinear_Any_SSSE3;
} #endif
#ifdef HAS_SCALEUVROWUP2_BILINEAR_AVX2 if (TestCpuFlag(kCpuHasAVX2)) {
Scale2RowUp = ScaleUVRowUp2_Bilinear_Any_AVX2;
} #endif
#ifdef HAS_SCALEUVROWUP2_BILINEAR_NEON if (TestCpuFlag(kCpuHasNEON)) {
Scale2RowUp = ScaleUVRowUp2_Bilinear_Any_NEON;
} #endif
#ifdef HAS_SCALEUVROWUP2_BILINEAR_RVV if (TestCpuFlag(kCpuHasRVV)) {
Scale2RowUp = ScaleUVRowUp2_Bilinear_RVV;
} #endif
Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width);
dst_ptr += dst_stride; for (x = 0; x < src_height - 1; ++x) {
Scale2RowUp(src_ptr, src_stride, dst_ptr, dst_stride, dst_width);
src_ptr += src_stride; // TODO(fbarchard): Test performance of writing one row of destination at a // time.
dst_ptr += 2 * dst_stride;
} if (!(dst_height & 1)) {
Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width);
}
}
// Scale 16 bit UV, horizontally up by 2 times. // Uses linear filter horizontally, nearest vertically. // This is an optimized version for scaling up a plane to 2 times of // its original width, using linear interpolation. // This is used to scale U and V planes of P210 to P410. staticvoid ScaleUVLinearUp2_16(int src_width, int src_height, int dst_width, int dst_height, int src_stride, int dst_stride, const uint16_t* src_uv,
uint16_t* dst_uv) { void (*ScaleRowUp)(const uint16_t* src_uv, uint16_t* dst_uv, int dst_width) =
ScaleUVRowUp2_Linear_16_Any_C; int i; int y; int dy;
// This function can only scale up by 2 times horizontally.
(void)src_width;
assert(src_width == ((dst_width + 1) / 2));
#ifdef HAS_SCALEUVROWUP2_LINEAR_16_SSE41 if (TestCpuFlag(kCpuHasSSE41)) {
ScaleRowUp = ScaleUVRowUp2_Linear_16_Any_SSE41;
} #endif
#ifdef HAS_SCALEUVROWUP2_LINEAR_16_AVX2 if (TestCpuFlag(kCpuHasAVX2)) {
ScaleRowUp = ScaleUVRowUp2_Linear_16_Any_AVX2;
} #endif
#ifdef HAS_SCALEUVROWUP2_LINEAR_16_NEON if (TestCpuFlag(kCpuHasNEON)) {
ScaleRowUp = ScaleUVRowUp2_Linear_16_Any_NEON;
} #endif
if (dst_height == 1) {
ScaleRowUp(src_uv + ((src_height - 1) / 2) * (intptr_t)src_stride, dst_uv,
dst_width);
} else {
dy = FixedDiv(src_height - 1, dst_height - 1);
y = (1 << 15) - 1; for (i = 0; i < dst_height; ++i) {
ScaleRowUp(src_uv + (y >> 16) * (intptr_t)src_stride, dst_uv, dst_width);
dst_uv += dst_stride;
y += dy;
}
}
}
// Scale 16 bit UV, up by 2 times. // This is an optimized version for scaling up a plane to 2 times of // its original size, using bilinear interpolation. // This is used to scale U and V planes of P010 to P410. staticvoid ScaleUVBilinearUp2_16(int src_width, int src_height, int dst_width, int dst_height, int src_stride, int dst_stride, const uint16_t* src_ptr,
uint16_t* dst_ptr) { void (*Scale2RowUp)(const uint16_t* src_ptr, ptrdiff_t src_stride,
uint16_t* dst_ptr, ptrdiff_t dst_stride, int dst_width) =
ScaleUVRowUp2_Bilinear_16_Any_C; int x;
// This function can only scale up by 2 times.
(void)src_width;
assert(src_width == ((dst_width + 1) / 2));
assert(src_height == ((dst_height + 1) / 2));
#ifdef HAS_SCALEUVROWUP2_BILINEAR_16_SSE41 if (TestCpuFlag(kCpuHasSSE41)) {
Scale2RowUp = ScaleUVRowUp2_Bilinear_16_Any_SSE41;
} #endif
#ifdef HAS_SCALEUVROWUP2_BILINEAR_16_AVX2 if (TestCpuFlag(kCpuHasAVX2)) {
Scale2RowUp = ScaleUVRowUp2_Bilinear_16_Any_AVX2;
} #endif
#ifdef HAS_SCALEUVROWUP2_BILINEAR_16_NEON if (TestCpuFlag(kCpuHasNEON)) {
Scale2RowUp = ScaleUVRowUp2_Bilinear_16_Any_NEON;
} #endif
Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width);
dst_ptr += dst_stride; for (x = 0; x < src_height - 1; ++x) {
Scale2RowUp(src_ptr, src_stride, dst_ptr, dst_stride, dst_width);
src_ptr += src_stride; // TODO(fbarchard): Test performance of writing one row of destination at a // time.
dst_ptr += 2 * dst_stride;
} if (!(dst_height & 1)) {
Scale2RowUp(src_ptr, 0, dst_ptr, 0, dst_width);
}
}
// 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_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
}
// Scale a UV plane (from NV12) // This function in turn calls a scaling function // suitable for handling the desired resolutions. staticint 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);
// Scale an UV image.
LIBYUV_API int UVScale(const uint8_t* src_uv, int src_stride_uv, int src_width, int src_height,
uint8_t* dst_uv, int dst_stride_uv, int dst_width, int dst_height, enum FilterMode filtering) { if (!src_uv || src_width <= 0 || src_height == 0 || src_width > 32768 ||
src_height > 32768 || !dst_uv || dst_width <= 0 || dst_height <= 0) { return -1;
} return ScaleUV(src_uv, src_stride_uv, src_width, src_height, dst_uv,
dst_stride_uv, dst_width, dst_height, 0, 0, dst_width,
dst_height, filtering);
}
// Scale a 16 bit UV image. // This function is currently incomplete, it can't handle all cases.
LIBYUV_API int UVScale_16(const uint16_t* src_uv, int src_stride_uv, int src_width, int src_height,
uint16_t* dst_uv, int dst_stride_uv, int dst_width, int dst_height, enum FilterMode filtering) { 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.