libyuv/source/rotate_argb.cc
Frank Barchard f249eaa72f [libyuv] Fix top 20 bugs and performance issues
1. Fix multi-bit TestCpuFlag(A | B) checks in AYUVToNV12, AYUVToNV21,
   RAWToRGBA, NV12ToRGB24Matrix, NV21ToRGB24Matrix, MergeRGBPlane, and
   MergeXR30Plane to compare == (A | B) so AVX-512 VBMI/BW kernels are
   not dispatched when AVX512VBMI or AVX512VL is absent.
2. Fix 2-byte SVE2 heap buffer overflow on odd widths in
   I422ToARGB1555Row_SVE_SC and I422ToARGB4444Row_SVE_SC using
   trn1/trn2 + st2b under .b predicates.
3. Fix ARGBPolynomialRow_SSE2 to use packssdw before packuswb, and add
   missing vzeroupper to SplitRGBRow_AVX2.
4. Fix CalcFrameSsim to return 0.0 instead of 0.0 / 0 (NaN) when
   width <= 8 || height <= 8 so I420Ssim stays finite on small frames.
5. Fix negative-height vertical inversion in ConvertFromI420,
   SplitARGBPlane (when dst_a is NULL), I422Rotate, I010Rotate, and
   I210Rotate.
6. Fix horizontal flip (negative x/dx) in ScaleFilterCols_NEON (signed
   sbfx #16, #16 offset extraction) and ScaleAddCols1_16_C (shift
   x >>= 16 before indexing src_ptr).
7. Fix ConvertToARGB odd crop_x chroma alignment in NV12/NV21, src_u
   offset in I420/YV12/J420/H420/U420, and FOURCC_U422 calling
   U422ToARGB instead of H422ToARGB.
8. Remove dead 14-instruction block in ScaleRowUp2_Bilinear_16_SSE2.
9. Fix UVScale_16 for src_width == 1 (P010ToP410/P210ToP410), enable
   and fix column order in TransposeWx4_32_SVE2, fix ByteToFloatRow
   NEON Any BPP (3 -> 4), fix AB64ToARGBRow_Any_*, ScaleColsUp2_SSE2,
   ScaleUVRowDown2Box_SME, and UYVYToARGBRow_SME macro guards, fix SVE2
   loop tail branches (b.lt/b.ge), and remove duplicate HAS_* macros.
10. Fix ConvertFP16ToFP32Column_NEON loop condition and unsigned b.lo
    underflow on widths < 8 or not multiples of 8.
11. Fix UYVYToNV12 IS_ALIGNED(awidth, ...) checks and WASM SIMD
    IS_ALIGNED(width, 16) checks in I420ToRGB24Matrix /
    I422ToRGB24Matrix.
12. Wire SplitUVRow_SVE2, SplitUVRow_LSX, and SplitUVRow_LASX in
    NV24ToARGB.
13. Fix MJpegDecoder heap buffer overflows in LoadFrame (reallocate
    databuf_[i] when scanlines_size changes), DecodeToBuffers, and
    DecodeToCallback (clamp scanlines_to_copy to lines_left and align
    skip to max_v_samp_factor).
14. Replace width & (vl - 1) tail calculations with post-loop remainder
    across 18 SVE2 functions in row_sve.h for non-power-of-2 SVE VL,
    and remove duplicate label 2: in UYVYToARGBRow_SVE_SC.
15. Handle width & 15 tail pixels in DetileToYUY2_C and validate
    IS_POWEROFTWO(tile_height) in DetileSplitUVPlane and DetileToYUY2.
16. Avoid signed 32-bit overflow in DivideRow_16_C and C16TO8, fix
    ClampFloatToZero_SSE2 pointer increment (lea 0x4) and loop counter
    (sub $0x1), and initialize xmm3 in ARGBCopyYToAlphaRow_SSE2.
17. Check max leaf 7 subleaf and max extended CPUID leaf in GetCpuFlags
    before querying subleaf 7:1 and extended leaf 0x80000021.
18. Free plane_u on error in I422ToNV21, and fix 4x/2x scratch row
    over-allocation in ScaleARGBBilinearDown and ScaleUVBilinearDown.
19. Add ARGBSetRow_RVV and ARGBShuffleRow_RVV in row_rvv.cc.
20. Fix NEON mask (15 -> 7) in ScaleUVRowUp2_Linear_Any_NEON /
    _16_Any_NEON, use SIMD InterpolateRow + SplitUVRow in NV16ToI420,
    wire SSSE3/SSE2 in ARGBToI400Matrix / ARGBToYUY2Matrix /
    ARGBToUYVYMatrix and RGB24Mirror, and split HalfFloatRow multiply
    into (src * scale) * 2^-112 to avoid 50x Intel denormal stalls.

Bug: b/574714081
Test: libyuv_unittest
Change-Id: Iec3ae5c58d52bee3ef13cf9b765ac84b733aa576
Reviewed-on: https://chromium-review.googlesource.com/c/libyuv/libyuv/+/8601931
Reviewed-by: richard winterton <rrwinterton@gmail.com>
Commit-Queue: Frank Barchard <fbarchard@google.com>
2026-10-19 16:46:40 -07:00

259 lines
8.1 KiB
C++

/*
* Copyright 2012 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/rotate_argb.h"
#include <limits.h>
#include "libyuv/convert.h"
#include "libyuv/cpu_id.h"
#include "libyuv/planar_functions.h"
#include "libyuv/rotate.h"
#include "libyuv/rotate_row.h"
#include "libyuv/row.h"
#ifdef __cplusplus
namespace libyuv {
extern "C" {
#endif
static int ARGBTranspose(const uint8_t* src_argb,
int src_stride_argb,
uint8_t* dst_argb,
int dst_stride_argb,
int width,
int height) {
#if defined(HAS_TRANSPOSEWXH_32_NEON) || defined(HAS_TRANSPOSEWXH_32_SME) || \
defined(HAS_TRANSPOSEWXH_32_RVV) || defined(HAS_TRANSPOSEWXH_32_HVX)
void (*TransposeWxH_32)(const uint8_t* src, int src_stride, uint8_t* dst,
int dst_stride, int width, int height) = NULL;
#endif
#if defined(HAS_TRANSPOSEWX16_32_AVX512BW)
void (*TransposeWx16_32)(const uint8_t* src, int src_stride, uint8_t* dst,
int dst_stride, int width) = NULL;
#endif
#if defined(HAS_TRANSPOSEWX8_32_AVX2)
void (*TransposeWx8_32)(const uint8_t* src, int src_stride, uint8_t* dst,
int dst_stride, int width) = NULL;
#endif
void (*TransposeWx4_32)(const uint8_t* src, int src_stride, uint8_t* dst,
int dst_stride, int width) = NULL;
// Check stride is a multiple of 4.
if (src_stride_argb & 3) {
return -1;
}
#if defined(HAS_TRANSPOSEWX4_32_SSE2)
if (TestCpuFlag(kCpuHasSSE2)) {
TransposeWx4_32 = TransposeWx4_32_Any_SSE2;
if (IS_ALIGNED(height, 4)) { // Width of dest.
TransposeWx4_32 = TransposeWx4_32_SSE2;
}
}
#endif
#if defined(HAS_TRANSPOSEWX8_32_AVX2)
if (TestCpuFlag(kCpuHasAVX2)) {
TransposeWx8_32 = TransposeWx8_32_Any_AVX2;
if (IS_ALIGNED(width, 4)) {
TransposeWx8_32 = TransposeWx8_32_AVX2;
}
}
#endif
#if defined(HAS_TRANSPOSEWX16_32_AVX512BW)
if (TestCpuFlag(kCpuHasAVX512BW)) {
TransposeWx16_32 = TransposeWx16_32_AVX512BW;
}
#endif
#if defined(HAS_TRANSPOSEWXH_32_NEON)
if (TestCpuFlag(kCpuHasNEON)) {
TransposeWxH_32 = TransposeWxH_32_NEON;
}
#endif
#if defined(HAS_TRANSPOSEWX4_32_SVE2)
if (TestCpuFlag(kCpuHasSVE2)) {
TransposeWx4_32 = TransposeWx4_32_SVE2;
#if defined(HAS_TRANSPOSEWXH_32_NEON)
TransposeWxH_32 = NULL;
#endif
}
#endif
#if defined(HAS_TRANSPOSEWXH_32_SME)
if (TestCpuFlag(kCpuHasSME)) {
TransposeWxH_32 = TransposeWxH_32_SME;
}
#endif
#if defined(HAS_TRANSPOSEWXH_32_RVV)
if (TestCpuFlag(kCpuHasRVV)) {
TransposeWxH_32 = TransposeWxH_32_RVV;
}
#endif
#if defined(HAS_TRANSPOSEWXH_32_HVX)
if (TestCpuFlag(kCpuHasHVX)) {
TransposeWxH_32 = TransposeWxH_32_HVX;
}
#endif
#if defined(HAS_TRANSPOSEWX4_32_LSX)
if (TestCpuFlag(kCpuHasLSX)) {
TransposeWx4_32 = TransposeWx4_32_Any_LSX;
if (IS_ALIGNED(height, 4)) { // Width of dest.
TransposeWx4_32 = TransposeWx4_32_LSX;
}
}
#endif
#if defined(HAS_TRANSPOSEWX4_32_LASX)
if (TestCpuFlag(kCpuHasLASX)) {
TransposeWx4_32 = TransposeWx4_32_Any_LASX;
if (IS_ALIGNED(height, 8)) { // Width of dest.
TransposeWx4_32 = TransposeWx4_32_LASX;
}
}
#endif
#if defined(HAS_TRANSPOSEWX4_32_WASMSIMD)
if (TestCpuFlag(kCpuHasWASMSIMD)) {
TransposeWx4_32 = TransposeWx4_32_Any_WASMSIMD;
if (IS_ALIGNED(height, 4)) { // Width of dest.
TransposeWx4_32 = TransposeWx4_32_WASMSIMD;
}
}
#endif
#if defined(HAS_TRANSPOSEWXH_32_NEON) || defined(HAS_TRANSPOSEWXH_32_SME) || \
defined(HAS_TRANSPOSEWXH_32_RVV) || defined(HAS_TRANSPOSEWXH_32_HVX)
if (TransposeWxH_32) {
TransposeWxH_32(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
height);
return 0;
}
#endif
#if defined(HAS_TRANSPOSEWX16_32_AVX512BW)
if (TransposeWx16_32) {
while (height >= 16) {
TransposeWx16_32(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
width);
src_argb += 16 * (ptrdiff_t)src_stride_argb;
dst_argb += 16 * 4;
height -= 16;
}
}
#endif
#if defined(HAS_TRANSPOSEWX8_32_AVX2)
if (TransposeWx8_32) {
while (height >= 8) {
TransposeWx8_32(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
width);
src_argb += 8 * (ptrdiff_t)src_stride_argb;
dst_argb += 8 * 4;
height -= 8;
}
}
#endif
if (height > 0) {
if (TransposeWx4_32) {
while (width >= 4) {
TransposeWx4_32(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
height);
src_argb += 4 * 4;
dst_argb += 4 * (ptrdiff_t)dst_stride_argb;
width -= 4;
}
}
if (width > 0) {
TransposeWxH_32_C(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
width, height);
}
}
return 0;
}
static int ARGBRotate90(const uint8_t* src_argb,
int src_stride_argb,
uint8_t* dst_argb,
int dst_stride_argb,
int width,
int height) {
// Rotate by 90 is a ARGBTranspose with the source read
// from bottom to top. So set the source pointer to the end
// of the buffer and flip the sign of the source stride.
src_argb += (ptrdiff_t)src_stride_argb * (height - 1);
src_stride_argb = -src_stride_argb;
return ARGBTranspose(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
width, height);
}
static int ARGBRotate270(const uint8_t* src_argb,
int src_stride_argb,
uint8_t* dst_argb,
int dst_stride_argb,
int width,
int height) {
// Rotate by 270 is a ARGBTranspose with the destination written
// from bottom to top. So set the destination pointer to the end
// of the buffer and flip the sign of the destination stride.
dst_argb += (ptrdiff_t)dst_stride_argb * (width - 1);
dst_stride_argb = -dst_stride_argb;
return ARGBTranspose(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
width, height);
}
static int ARGBRotate180(const uint8_t* src_argb,
int src_stride_argb,
uint8_t* dst_argb,
int dst_stride_argb,
int width,
int height) {
// Rotate by 180 is a vertical flip (negative height) and horizontal mirror.
return ARGBMirror(src_argb, src_stride_argb, dst_argb, dst_stride_argb, width,
-height);
}
LIBYUV_API
int ARGBRotate(const uint8_t* src_argb,
int src_stride_argb,
uint8_t* dst_argb,
int dst_stride_argb,
int width,
int height,
enum RotationMode mode) {
if (!src_argb || width <= 0 || height == 0 || height == INT_MIN ||
!dst_argb) {
return -1;
}
// Negative height means invert the image.
if (height < 0) {
height = -height;
src_argb = src_argb + (ptrdiff_t)(height - 1) * src_stride_argb;
src_stride_argb = -src_stride_argb;
}
switch (mode) {
case kRotate0:
// copy frame
return ARGBCopy(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
width, height);
case kRotate90:
return ARGBRotate90(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
width, height);
case kRotate270:
return ARGBRotate270(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
width, height);
case kRotate180:
return ARGBRotate180(src_argb, src_stride_argb, dst_argb, dst_stride_argb,
width, height);
default:
break;
}
return -1;
}
#ifdef __cplusplus
} // extern "C"
} // namespace libyuv
#endif