PR #25941 opened by kierank
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25941
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25941.patch

The biweight kernels add the rounding offset ((offset + 1) | 1) << log2_denom
to the 16-bit weighted sum with paddsw and then shift by log2_denom + 1.
At log2_denom 7 the offset alone can reach 32640, the addition saturates
at 32767 and the output is capped at 127 instead of the C result. For
example log2_denom 7, weightd 83, weights -31, offset 254 gives 174 in C
and 127 in asm.

Shift by log2_denom first, then add the offset and shift by one:
((sum >> d) + ((o + 1) | 1)) >> 1 is the same value as
(sum + (((o + 1) | 1) << d)) >> (d + 1), and the saturating add can now
only saturate for values that clip to 0 or 255 anyway. The offset is no
longer halved in the weight == 128 path, since it is no longer shifted.

This makes the asm bit-exact for every log2_denom the bitstream allows,
so the checkasm test no longer limits 8-bit log2_denom to 6.

# Summary of changes

Briefly describe what this PR does and why.

<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).

Attached filenames must match the sample's filename:

```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->



>From aaef98bd65450c8ad5bba367872456d2d1c86110 Mon Sep 17 00:00:00 2001
From: Kieran Kunhya <[email protected]>
Date: Wed, 11 Nov 2026 02:50:56 +0000
Subject: [PATCH] avcodec/x86/h264_weight: fix 8-bit biweight rounding overflow
 at log2_denom 7

The biweight kernels add the rounding offset ((offset + 1) | 1) << log2_denom
to the 16-bit weighted sum with paddsw and then shift by log2_denom + 1.
At log2_denom 7 the offset alone can reach 32640, the addition saturates
at 32767 and the output is capped at 127 instead of the C result. For
example log2_denom 7, weightd 83, weights -31, offset 254 gives 174 in C
and 127 in asm.

Shift by log2_denom first, then add the offset and shift by one:
((sum >> d) + ((o + 1) | 1)) >> 1 is the same value as
(sum + (((o + 1) | 1) << d)) >> (d + 1), and the saturating add can now
only saturate for values that clip to 0 or 255 anyway. The offset is no
longer halved in the weight == 128 path, since it is no longer shifted.

This makes the asm bit-exact for every log2_denom the bitstream allows,
so the checkasm test no longer limits 8-bit log2_denom to 6.
---
 libavcodec/x86/h264_weight.asm | 19 ++++++++++---------
 tests/checkasm/h264dsp.c       |  9 +--------
 2 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/libavcodec/x86/h264_weight.asm b/libavcodec/x86/h264_weight.asm
index 751c910b710..0da2a434175 100644
--- a/libavcodec/x86/h264_weight.asm
+++ b/libavcodec/x86/h264_weight.asm
@@ -120,7 +120,6 @@ WEIGHT_FUNC_HALF_MM 8
     mov  off_regd, r7m
     add  off_regd, 1
     or   off_regd, 1
-    add       r4d, 1
     cmp       r6d, 128
     je .nonnormal
     cmp       r5d, 128
@@ -128,7 +127,6 @@ WEIGHT_FUNC_HALF_MM 8
 .nonnormal:
     sar       r5d, 1
     sar       r6d, 1
-    sar  off_regd, 1
     sub       r4d, 1
 .normal:
 %if cpuflag(ssse3)
@@ -140,8 +138,6 @@ WEIGHT_FUNC_HALF_MM 8
 %endif
     movd       m5, off_regd
     movd       m3, r4d
-    pslld      m5, m3
-    psrld      m5, 1
 %if cpuflag(ssse3)
     punpcklbw  m4, m0
     pshuflw    m4, m4, 0
@@ -171,10 +167,12 @@ WEIGHT_FUNC_HALF_MM 8
 %endmacro
 
 %macro BIWEIGHT_STEPB 0
-    paddsw     m0, m5
-    paddsw     m1, m5
     psraw      m0, m3
     psraw      m1, m3
+    paddsw     m0, m5
+    paddsw     m1, m5
+    psraw      m0, 1
+    psraw      m1, 1
     packuswb   m0, m1
 %endmacro
 
@@ -216,8 +214,9 @@ cglobal h264_biweight_%1, 7, 8, %2
     pmullw     m1, m4
     paddsw     m0, m1
 %endif
-    paddsw     m0, m5
     psraw      m0, m3
+    paddsw     m0, m5
+    psraw      m0, 1
     packuswb   m0, m0
     movd       [r0], m0
     add        r0, r2
@@ -249,10 +248,12 @@ BIWEIGHT_FUNC_HALF_MM 8, 8
 %macro BIWEIGHT_SSSE3_OP 0
     pmaddubsw  m0, m4
     pmaddubsw  m2, m4
-    paddsw     m0, m5
-    paddsw     m2, m5
     psraw      m0, m3
     psraw      m2, m3
+    paddsw     m0, m5
+    paddsw     m2, m5
+    psraw      m0, 1
+    psraw      m2, 1
     packuswb   m0, m2
 %endmacro
 
diff --git a/tests/checkasm/h264dsp.c b/tests/checkasm/h264dsp.c
index 6e9792a0f6e..3c071e2658f 100644
--- a/tests/checkasm/h264dsp.c
+++ b/tests/checkasm/h264dsp.c
@@ -648,14 +648,7 @@ static void check_biweight(void)
                      hgt >= FFMAX(2, w / 2); hgt >>= 1) {
                     for (int i = 0; i < 32; i++) {
                         int stride = 32 * SIZEOF_PIXEL;
-                        // Spec allows for 0 <= log2_denom <= 7 regardless
-                        // of bit depth, but x86 asm impl. is bit accurate
-                        // only up to 6 for 8bpp.
-                        //
-                        // In practice, log2_denom > 3 is rarely used.
-                        int max_log2_denom_8b = 6;
-                        int log2_denom = rnd() %
-                            (bit_depth == 8 ? (max_log2_denom_8b + 1) : 8);
+                        int log2_denom = rnd() % 8;
 
                         int weightd, weights;
                         do {
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to