Commit 433c8742ce for aom
commit 433c8742cead33f3a4a63de40da85843a8f1bb87
Author: Frank Galligan <fgalligan@google.com>
Date: Tue Dec 1 10:37:58 2026 -0800
av1,neon: Saturate idct4 stage 3 before shift
Saturate idct4_s16x8_neon stage-3 outputs to signed 16 bits via
vqmovn_s32 before applying the out_rshift == 1 rounding right shift
on TX_4X16 row transforms. The C reference (av1_idct4) clamps stage 3
to [-32768, 32767] prior to av1_round_shift_array(..., 1), whereas
vqrshrn_n_s32 shifted first and saturated afterward, diverging when
row sums exceed int16_t range (e.g., TX_4X16 ADST_DCT).
Keep vqrshrn_n_s32 for iadst4_s16x8_neon (where av1_iadst4 skips
stage-3 clamping under DO_RANGE_CHECK_CLAMP == 0) and for
out_rshift == 4 column transforms (which feed 8-bit [0, 255] pixel
reconstruction clipping where any overflow beyond +-32767 saturates
identically). Add AV1LbdInvTxfm2d.Stage3Clamp covering this path.
TAG=agy
CONV=490488d4-7005-402a-8cc4-c245227f80fe
Change-Id: I2a82d0a810b4f284779d628b370ed5d9f8b8f6a2
diff --git a/av1/common/arm/av1_inv_txfm_neon.c b/av1/common/arm/av1_inv_txfm_neon.c
index 2ad8c6ec62..9b8c06fb29 100644
--- a/av1/common/arm/av1_inv_txfm_neon.c
+++ b/av1/common/arm/av1_inv_txfm_neon.c
@@ -3801,11 +3801,21 @@ static inline void lowbd_inv_txfm_dc_only_4xn_neon(uint8_t *output, int stride,
static AOM_FORCE_INLINE int16x8_t narrow_inv_txfm4_out_neon(int32x4_t lo,
int32x4_t hi,
- int out_rshift) {
+ int out_rshift,
+ int clamp_stage3) {
if (out_rshift == 4) {
+ // out_rshift == 4 is only used on column transforms feeding 8-bit [0, 255]
+ // pixel reconstruction clipping, where any overflow past int16_t saturates
+ // past +-2048 to 0 or 255 either way.
return vcombine_s16(vqrshrn_n_s32(lo, 4), vqrshrn_n_s32(hi, 4));
}
if (out_rshift == 1) {
+ if (clamp_stage3) {
+ // av1_idct4() clamps stage 3 to [-32768, 32767] before shifting right
+ // by out_rshift.
+ const int16x8_t sat = vcombine_s16(vqmovn_s32(lo), vqmovn_s32(hi));
+ return vrshrq_n_s16(sat, 1);
+ }
return vcombine_s16(vqrshrn_n_s32(lo, 1), vqrshrn_n_s32(hi, 1));
}
assert(out_rshift == 0);
@@ -3851,13 +3861,13 @@ static AOM_FORCE_INLINE void idct4_s16x8_neon(const int16x8_t *in,
INV_COS_BIT);
out[0] = narrow_inv_txfm4_out_neon(vaddq_s32(s0_lo, s3_lo),
- vaddq_s32(s0_hi, s3_hi), out_rshift);
+ vaddq_s32(s0_hi, s3_hi), out_rshift, 1);
out[1] = narrow_inv_txfm4_out_neon(vaddq_s32(s1_lo, s2_lo),
- vaddq_s32(s1_hi, s2_hi), out_rshift);
+ vaddq_s32(s1_hi, s2_hi), out_rshift, 1);
out[2] = narrow_inv_txfm4_out_neon(vsubq_s32(s1_lo, s2_lo),
- vsubq_s32(s1_hi, s2_hi), out_rshift);
+ vsubq_s32(s1_hi, s2_hi), out_rshift, 1);
out[3] = narrow_inv_txfm4_out_neon(vsubq_s32(s0_lo, s3_lo),
- vsubq_s32(s0_hi, s3_hi), out_rshift);
+ vsubq_s32(s0_hi, s3_hi), out_rshift, 1);
}
static AOM_FORCE_INLINE void iadst4_s16x8_neon(const int16x8_t *in,
@@ -3909,10 +3919,10 @@ static AOM_FORCE_INLINE void iadst4_s16x8_neon(const int16x8_t *in,
const int32x4_t y3_hi =
vrshrq_n_s32(vsubq_s32(vaddq_s32(s0_hi, s1_hi), s3_hi), INV_COS_BIT);
- out[0] = narrow_inv_txfm4_out_neon(y0_lo, y0_hi, out_rshift);
- out[1] = narrow_inv_txfm4_out_neon(y1_lo, y1_hi, out_rshift);
- out[2] = narrow_inv_txfm4_out_neon(y2_lo, y2_hi, out_rshift);
- out[3] = narrow_inv_txfm4_out_neon(y3_lo, y3_hi, out_rshift);
+ out[0] = narrow_inv_txfm4_out_neon(y0_lo, y0_hi, out_rshift, 0);
+ out[1] = narrow_inv_txfm4_out_neon(y1_lo, y1_hi, out_rshift, 0);
+ out[2] = narrow_inv_txfm4_out_neon(y2_lo, y2_hi, out_rshift, 0);
+ out[3] = narrow_inv_txfm4_out_neon(y3_lo, y3_hi, out_rshift, 0);
}
static AOM_FORCE_INLINE void apply_txfm4_s16x8_neon(int16x8_t *a,
diff --git a/test/av1_inv_txfm2d_test.cc b/test/av1_inv_txfm2d_test.cc
index c5a213c4c7..5eabaa06ec 100644
--- a/test/av1_inv_txfm2d_test.cc
+++ b/test/av1_inv_txfm2d_test.cc
@@ -363,6 +363,76 @@ TEST_P(AV1LbdInvTxfm2d, gt_int16) {
}
}
+TEST_P(AV1LbdInvTxfm2d, Stage3Clamp) {
+ const LbdInvTxfm2dFunc target_func = GET_PARAM(0);
+ if (target_func == nullptr) return;
+ const int bd = 8;
+ const int kStride = 64;
+ const int kBlkSize = kStride * kStride;
+ DECLARE_ALIGNED(32, int32_t, inv_input[kBlkSize]);
+ DECLARE_ALIGNED(16, uint8_t, output[kBlkSize]);
+ DECLARE_ALIGNED(16, uint16_t, ref_output[kBlkSize]);
+
+ auto reset_input = [&]() {
+ for (int i = 0; i < kBlkSize; ++i) {
+ inv_input[i] = 0;
+ }
+ };
+
+ auto run_case = [&](TX_TYPE tx_type, TX_SIZE tx_size, int eob,
+ uint8_t init_pred, const char *label) {
+ InvTxfm2dFunc ref_func = libaom_test::inv_txfm_func_ls[tx_size];
+ if (ref_func == nullptr) return;
+ const int rows = tx_size_high[tx_size];
+ const int cols = tx_size_wide[tx_size];
+ for (int i = 0; i < kBlkSize; ++i) {
+ output[i] = init_pred;
+ ref_output[i] = init_pred;
+ }
+ ref_func(inv_input, ref_output, kStride, tx_type, bd);
+ target_func(inv_input, output, kStride, tx_type, tx_size, eob);
+ for (int r = 0; r < rows; ++r) {
+ for (int c = 0; c < cols; ++c) {
+ const uint8_t ref_value =
+ static_cast<uint8_t>(ref_output[r * kStride + c]);
+ ASSERT_EQ(ref_value, output[r * kStride + c])
+ << label << " [" << r << "," << c << "] tx_size=" << cols << "x"
+ << rows << " tx_type=" << tx_type_name[tx_type] << " eob=" << eob;
+ }
+ }
+ };
+
+ // Regression vector from Argon profile0_core/test10072_2027.obu (TX_4X16):
+ // For x = [0, 32767, 3021, 2014] in row idct4 with out_rshift=1:
+ // s0 = round_shift(3021 * 2896, 12) = 2136,
+ // s3 = round_shift(32767 * 3784 + 2014 * 1567, 12) = 31042,
+ // s0 + s3 = 33178 > INT16_MAX, which C clamps to 32767 before >> 1 -> 16384.
+ for (int sign : { 1, -1 }) {
+ reset_input();
+ inv_input[24] = sign * 32767;
+ inv_input[40] = sign * 3021;
+ inv_input[56] = sign * 2014;
+ ASSERT_NO_FATAL_FAILURE(
+ run_case(ADST_DCT, TX_4X16, 41, 128, "test10072_row8"));
+ }
+
+ // Stage-3 int16 saturation before out_rshift=1 across TX_4X16 IDCT row
+ // types, plus IADST row types verifying C stage-3 parity on conformant
+ // extremes.
+ static const TX_TYPE kTx4x16Types[] = { DCT_DCT, ADST_DCT, FLIPADST_DCT,
+ DCT_ADST, ADST_ADST };
+ for (TX_TYPE tx_type : kTx4x16Types) {
+ for (int sign : { 1, -1 }) {
+ reset_input();
+ inv_input[16] = sign * 32767;
+ inv_input[32] = sign * 3021;
+ inv_input[48] = sign * 2014;
+ ASSERT_NO_FATAL_FAILURE(
+ run_case(tx_type, TX_4X16, 64, 128, "stage3_4x16_row0"));
+ }
+ }
+}
+
TEST_P(AV1LbdInvTxfm2d, DISABLED_Speed) {
for (int j = 1; j < (int)(TX_SIZES_ALL); ++j) {
for (int i = 0; i < (int)TX_TYPES; ++i) {