Commit 54cec21601 for ffmpeg
commit 54cec21601db5ba69386278d0d9ce3e9eeef9f2e
Author: Wesley Maa <wesley.maa@gmail.com>
Date: Sun Nov 8 11:15:57 2026 -0500
aarch64/vc1dsp: fix 16-bit overflow in the 4-point stage of inv_trans_4x4/8x4
The vertical (second, 4-point) pass of ff_vc1_inv_trans_4x4_neon and
ff_vc1_inv_trans_8x4_neon formed
t1 = 17 * src[0] + 17 * src[16]
t2 = 17 * src[0] - 17 * src[16]
in 16-bit lanes (mul, mul, add, sub) and then folded them into
t3/2 and t4/2 with "ssra #1". Additions and multiplications wrap
harmlessly as long as the final result fits, but the arithmetic shift
does not: once |src[0] +- src[16]| exceeds 1927, 17 * (src[0] +- src[16])
no longer fits in int16, the shift brings in the wrong sign bit and the
output flips sign. For example a column of first-pass values
src[0] = 1823, src[16] = -110 gives t2 = 32861; the C code computes
(t2 + t4 + 64) >> 7 = 256 and clips it to 255, while the NEON code
wraps t2 to -32675 and the residual saturates to 0.
Such values are well within the ranges the VC-1 spec allows for the
intermediate (E, up to +-4096) and final (R, up to +-512) values, and
they do occur in real streams: decoding a real-world WMV3 Main profile
file, bright 4x4 and 8x4 inter blocks (residual around +256 over a
dark prediction) come out as isolated black pixels, and the error then
propagates through the following P frames until the next key frame.
Compute a = src[0] +- src[16] first, which cannot overflow, and
derive t >> 1 = (17 * a) >> 1 = 8 * a + (a >> 1) with shl #3 +
ssra #1. This is exact because 16 * a is even. Everything after that
is modular add/sub, and the value that is finally shifted,
(t1 +- t3) >> 1, is within 32 of 64 * R and fits in 16 bits for
every R in [-511, 511], so the stage now matches the C code for the
whole spec range except an output of exactly -512. The instruction
count is unchanged (the two neg are no longer needed) and checkasm
--bench shows no difference.
The 4-point first passes (rows of 4x4 and 4x8) have the same shape, but
there an overflowing t1 implies max(t1 + t3, t1 - t3) >= 32768, i.e. an
intermediate value E >= 4096 outside the spec range, so they are left
untouched.
Signed-off-by: Wesley Maa <wesley.maa@gmail.com>
diff --git a/libavcodec/aarch64/vc1dsp_neon.S b/libavcodec/aarch64/vc1dsp_neon.S
index d8e7788026..a93df5dc66 100644
--- a/libavcodec/aarch64/vc1dsp_neon.S
+++ b/libavcodec/aarch64/vc1dsp_neon.S
@@ -278,18 +278,18 @@ function ff_vc1_inv_trans_8x4_neon, export=1
mul v3.8h, v4.8h, v0.h[5] // 22/2 * src[24]
trn1 v2.4s, v6.4s, v2.4s
mul v4.8h, v4.8h, v0.h[4] // 10/2 * src[24]
- mul v6.8h, v7.8h, v0.h[6] // 17 * src[0]
- mul v1.8h, v1.8h, v0.h[6] // 17 * src[16]
+ add v6.8h, v7.8h, v1.8h // src[0] + src[16]
+ sub v1.8h, v7.8h, v1.8h // src[0] - src[16]
mls v3.8h, v2.8h, v0.h[4] // t4/2 = - 10/2 * src[8] + 22/2 * src[24]
mla v4.8h, v2.8h, v0.h[5] // t3/2 = 22/2 * src[8] + 10/2 * src[24]
- add v0.8h, v6.8h, v1.8h // t1 = 17 * src[0] + 17 * src[16]
- sub v1.8h, v6.8h, v1.8h // t2 = 17 * src[0] - 17 * src[16]
- neg v2.8h, v3.8h // -t4/2
- neg v6.8h, v4.8h // -t3/2
- ssra v4.8h, v0.8h, #1 // (t1 + t3) >> 1
- ssra v2.8h, v1.8h, #1 // (t2 - t4) >> 1
- ssra v3.8h, v1.8h, #1 // (t2 + t4) >> 1
- ssra v6.8h, v0.8h, #1 // (t1 - t3) >> 1
+ shl v0.8h, v6.8h, #3 // 8 * (src[0] + src[16])
+ shl v7.8h, v1.8h, #3 // 8 * (src[0] - src[16])
+ ssra v0.8h, v6.8h, #1 // t1 >> 1
+ ssra v7.8h, v1.8h, #1 // t2 >> 1
+ sub v6.8h, v0.8h, v4.8h // (t1 - t3) >> 1
+ add v4.8h, v4.8h, v0.8h // (t1 + t3) >> 1
+ sub v2.8h, v7.8h, v3.8h // (t2 - t4) >> 1
+ add v3.8h, v3.8h, v7.8h // (t2 + t4) >> 1
srshr v0.8h, v4.8h, #6 // (t1 + t3 + 64) >> 7
srshr v1.8h, v2.8h, #6 // (t2 - t4 + 64) >> 7
srshr v2.8h, v3.8h, #6 // (t2 + t4 + 64) >> 7
@@ -505,18 +505,18 @@ function ff_vc1_inv_trans_4x4_neon, export=1
trn2 v1.2s, v1.2s, v2.2s // 20 21 22 23
mul v2.4h, v4.4h, v0.h[1] // 22/2 * src[24]
mul v4.4h, v4.4h, v0.h[0] // 10/2 * src[24]
- mul v7.4h, v16.4h, v0.h[2] // 17 * src[0]
- mul v1.4h, v1.4h, v0.h[2] // 17 * src[16]
+ add v7.4h, v16.4h, v1.4h // src[0] + src[16]
+ sub v1.4h, v16.4h, v1.4h // src[0] - src[16]
mls v2.4h, v3.4h, v0.h[0] // t4/2 = - 10/2 * src[8] + 22/2 * src[24]
mla v4.4h, v3.4h, v0.h[1] // t3/2 = 22/2 * src[8] + 10/2 * src[24]
- add v0.4h, v7.4h, v1.4h // t1 = 17 * src[0] + 17 * src[16]
- sub v1.4h, v7.4h, v1.4h // t2 = 17 * src[0] - 17 * src[16]
- neg v3.4h, v2.4h // -t4/2
- neg v7.4h, v4.4h // -t3/2
- ssra v4.4h, v0.4h, #1 // (t1 + t3) >> 1
- ssra v3.4h, v1.4h, #1 // (t2 - t4) >> 1
- ssra v2.4h, v1.4h, #1 // (t2 + t4) >> 1
- ssra v7.4h, v0.4h, #1 // (t1 - t3) >> 1
+ shl v0.4h, v7.4h, #3 // 8 * (src[0] + src[16])
+ shl v16.4h, v1.4h, #3 // 8 * (src[0] - src[16])
+ ssra v0.4h, v7.4h, #1 // t1 >> 1
+ ssra v16.4h, v1.4h, #1 // t2 >> 1
+ sub v7.4h, v0.4h, v4.4h // (t1 - t3) >> 1
+ add v4.4h, v4.4h, v0.4h // (t1 + t3) >> 1
+ sub v3.4h, v16.4h, v2.4h // (t2 - t4) >> 1
+ add v2.4h, v2.4h, v16.4h // (t2 + t4) >> 1
trn1 v0.2d, v4.2d, v3.2d
trn1 v1.2d, v2.2d, v7.2d
srshr v0.8h, v0.8h, #6 // (t1 + t3 + 64) >> 7, (t2 - t4 + 64) >> 7