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

# 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 eeb54cd2f4fcee5603967bbb85ae109170a7fa31 Mon Sep 17 00:00:00 2001
From: Kieran Kunhya <[email protected]>
Date: Wed, 11 Nov 2026 02:50:56 +0000
Subject: [PATCH 1/6] 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


>From 8f4fbe6d4f4c84449d84be1071a0a0bdcd00f736 Mon Sep 17 00:00:00 2001
From: Kieran Kunhya <[email protected]>
Date: Wed, 11 Nov 2026 02:38:58 +0000
Subject: [PATCH 2/6] avcodec/x86/h264_idct: Fix
 ff_h264_luma_dc_dequant_idct_sse2 for qmul > 32767

For qmul > 32767 the function multiplied by qmul >> 7 and shifted by one
less. That is only exact when the low 7 bits of qmul are zero, which
holds for flat and default scaling matrices but not for custom DC
scaling factors at low and mid QP (e.g. scale 85 at QP 25 gives
qmul 59840). From qmul >= 1 << 22 the shifted value also no longer fits
the signed words pmaddwd multiplies. In both cases the output differs
from the C code.

Split qmul into its low 15 bits and the rest instead and compute both
products with pmaddwd; their sum equals x * qmul modulo 2^32, like the
C code.

Extend the checkasm test to cover qmul > 32767; it previously only used
qmul < 4096.
---
 libavcodec/x86/h264_idct.asm | 50 ++++++++++++++++++------------------
 tests/checkasm/h264dsp.c     | 22 ++++++++--------
 2 files changed, 37 insertions(+), 35 deletions(-)

diff --git a/libavcodec/x86/h264_idct.asm b/libavcodec/x86/h264_idct.asm
index 2014129e9fa..ab1c516b836 100644
--- a/libavcodec/x86/h264_idct.asm
+++ b/libavcodec/x86/h264_idct.asm
@@ -572,22 +572,24 @@ RET
     SWAP %1, %4, %3
 %endmacro
 
-; requires m5 to contain pw_1
-%macro DEQUANT 1
+; requires m5 to contain pw_1; with %2 set, m6 holds the high part of qmul
+%macro DEQUANT 1-2 0
     movd        m4, t3d
     pshufd      m4, m4, 0
-    punpcklwd   m0, m5
-    punpcklwd   m1, m5
-    punpcklwd   m2, m5
-    punpcklwd   m3, m5
-    pmaddwd     m0, m4
-    pmaddwd     m1, m4
-    pmaddwd     m2, m4
-    pmaddwd     m3, m4
-    psrad       m0, %1
-    psrad       m1, %1
-    psrad       m2, %1
-    psrad       m3, %1
+%assign %%i 0
+%rep 4
+    punpcklwd   m %+ %%i, m5
+%if %2
+    pmaddwd     m7, m %+ %%i, m6
+    pslld       m7, 15
+%endif
+    pmaddwd     m %+ %%i, m4
+%if %2
+    paddd       m %+ %%i, m7
+%endif
+    psrad       m %+ %%i, %1
+%assign %%i %%i+1
+%endrep
 %endmacro
 
 %macro STORE_WORDS 10
@@ -636,8 +638,8 @@ RET
 %endif
 %endmacro
 
-%macro DEQUANT_STORE 1
-    DEQUANT     %1
+%macro DEQUANT_STORE 1-2 0
+    DEQUANT     %1, %2
     STORE_WORDS m0, m1,  0,  1,  4,  5,  2,  3,  6,  7
     STORE_WORDS m2, m3,  8,  9, 12, 13, 10, 11, 14, 15
 %endmacro
@@ -678,17 +680,15 @@ cglobal h264_luma_dc_dequant_idct, 3, 4, 6
     DEQUANT_STORE 8
     RET
 .big_qmul:
-    bsr        t0d, t3d
-    WIN64_PUSH_XMM 7
+    ; x * qmul = x * (qmul & 0x7fff) + (x * (qmul >> 15) << 15), modulo 2^32 
like the C code
+    WIN64_PUSH_XMM 8
+    mov        t1d, t3d
+    shr        t1d, 15
+    and        t3d, 0x7fff
     add        t3d, 128 << 16
-    mov        t1d, 7
-    cmp        t0d, t1d
-    cmovg      t0d, t1d
-    inc        t1d
-    shr        t3d, t0b
-    sub        t1d, t0d
     movd        m6, t1d
-    DEQUANT_STORE m6
+    pshufd      m6, m6, 0
+    DEQUANT_STORE 8, 1
     RET
 
 %macro IDCT_XMM 1
diff --git a/tests/checkasm/h264dsp.c b/tests/checkasm/h264dsp.c
index 3c071e2658f..4f867949644 100644
--- a/tests/checkasm/h264dsp.c
+++ b/tests/checkasm/h264dsp.c
@@ -414,10 +414,12 @@ static void check_idct_dequant(void)
     LOCAL_ALIGNED_16(int32_t, dst0_32, [16 * 16]);
     LOCAL_ALIGNED_16(int32_t, dst1_32, [16 * 16]);
     H264DSPContext h;
-    int bit_depth, i, qmul;
+    int bit_depth, i, qmul[2];
     declare_func(void, int16_t *output, int16_t *input, int qmul);
 
-    qmul = rnd() % 4096;
+    qmul[0] = rnd() % 4096;
+    /* above 32767, up to the largest 8-bit dequant4_coeff (18 * 255 << 10) */
+    qmul[1] = 32768 + rnd() % ((18 * 255 << 10) - 32767);
 
     for (i = 0; i < FF_ARRAY_ELEMS(depths); i++) {
         bit_depth = depths[i];
@@ -437,15 +439,15 @@ static void check_idct_dequant(void)
             for (int j = 0; j < 16; j++)
                 src32[j] = (rnd() % (1 << (bit_depth + 1))) - (1 << bit_depth);
         }
-        memset(dst_ref, 0, 16 * 16 * SIZEOF_COEF);
-        memset(dst_new, 0, 16 * 16 * SIZEOF_COEF);
-
         if (check_func(h.luma_dc_dequant_idct, "luma_dc_dequant_idct_%d", 
bit_depth)) {
-
-            call_ref(dst_ref, src, qmul);
-            call_new(dst_new, src, qmul);
-            checkasm_check_dctcoef(dst0, 16*SIZEOF_COEF, dst1, 16*SIZEOF_COEF, 
16, 16, "dst");
-            bench_new(dst_new, src, qmul);
+            for (int j = 0; j < 2; j++) {
+                memset(dst_ref, 0, 16 * 16 * SIZEOF_COEF);
+                memset(dst_new, 0, 16 * 16 * SIZEOF_COEF);
+                call_ref(dst_ref, src, qmul[j]);
+                call_new(dst_new, src, qmul[j]);
+                checkasm_check_dctcoef(dst0, 16*SIZEOF_COEF, dst1, 
16*SIZEOF_COEF, 16, 16, "dst");
+            }
+            bench_new(dst_new, src, qmul[0]);
         }
     }
 }
-- 
2.54.0


>From a6fbfabe362028513a9f0feb8a684fb3e78adcc5 Mon Sep 17 00:00:00 2001
From: Kieran Kunhya <[email protected]>
Date: Wed, 11 Nov 2026 09:42:59 +0000
Subject: [PATCH 3/6] checkasm/h264chroma: test every fractional position and
 block height

The test only covered mx and my 0 and 1 and the height equal to the
width, with src aligned to 4. Test all 64 mx/my combinations, at the
block heights H.264 uses per width for 4:2:0 and 4:2:2 (8x4..8x16,
4x2..4x16, 2x2..2x8), with src at any pixel offset and dst filled with
random pixels, so the avg functions average with something other than
their own source.
---
 tests/checkasm/h264chroma.c | 59 ++++++++++++++++++++++---------------
 1 file changed, 36 insertions(+), 23 deletions(-)

diff --git a/tests/checkasm/h264chroma.c b/tests/checkasm/h264chroma.c
index 5697b2b2d84..167ef86fabb 100644
--- a/tests/checkasm/h264chroma.c
+++ b/tests/checkasm/h264chroma.c
@@ -26,51 +26,64 @@
 #include "libavutil/intreadwrite.h"
 
 #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
+/* the same stride for src and dst, as in the decoders */
+#define STRIDE   64
+/* mc8 at height 16 reads 17 rows of 9 pixels, from up to 7 pixels in */
+#define BUF_SIZE (STRIDE * 18)
 
-#define randomize_buffers(bit_depth)             \
-    do {                                         \
-        if (bit_depth == 8) {                    \
-            for (int i = 0; i < 16*18; i++)      \
-                src[i] = rnd();                  \
-        } else {                                 \
-            unsigned mask = (1 << bit_depth) - 1;\
-            for (int i = 0; i < 16*18*2; i += 2) \
-                AV_WN16A(&src[i], rnd() & mask); \
-        }                                        \
+#define randomize_buffers(bit_depth)                                     \
+    do {                                                                 \
+        unsigned mask = bit_depth == 8 ? 0xffff : (1 << bit_depth) - 1;  \
+        for (int i = 0; i < BUF_SIZE; i += 2) {                          \
+            unsigned d = rnd() & mask;                                   \
+            AV_WN16A(&src[i], rnd() & mask);                             \
+            AV_WN16A(&dst0[i], d);                                       \
+            AV_WN16A(&dst1[i], d);                                       \
+        }                                                                \
     } while (0)
 
+/* the block heights per width in H.264: 4:2:0 and 4:2:2 partitions */
+static const int heights[3][5] = {
+    { 4, 8, 16 },    /* mc8 */
+    { 2, 4, 8, 16 }, /* mc4 */
+    { 2, 4, 8 },     /* mc2 */
+};
+
 static void check_chroma_mc(void)
 {
     H264ChromaContext h;
-    DECLARE_ALIGNED_4(uint8_t, src) [16 * 18 * 2];
-    DECLARE_ALIGNED_16(uint8_t, dst0)[16 * 18 * 2];
-    DECLARE_ALIGNED_16(uint8_t, dst1)[16 * 18 * 2];
+    LOCAL_ALIGNED_16(uint8_t, src,  [BUF_SIZE]);
+    LOCAL_ALIGNED_16(uint8_t, dst0, [BUF_SIZE]);
+    LOCAL_ALIGNED_16(uint8_t, dst1, [BUF_SIZE]);
 
     declare_func(void, uint8_t *dst, const uint8_t *src,
                  ptrdiff_t stride, int h, int x, int y);
 
     for (int bit_depth = 8; bit_depth <= 10; bit_depth++) {
         ff_h264chroma_init(&h, bit_depth);
-        randomize_buffers(bit_depth);
         for (int size = 0; size < 3; size++) {
             int block_size = 8 >> size;
 
 #define CHECK_CHROMA_MC(name)                                                  
                           \
             do {                                                               
                           \
                 if (check_func(h.name## _pixels_tab[size], #name "_mc%d_%d", 1 
<< (3-size), bit_depth)) { \
-                    for (int x = 0; x < 2; x++) {                              
                           \
-                        for (int y = 0; y < 2; y++) {                          
                           \
-                            memcpy(dst0, src, 16 * 18 * SIZEOF_PIXEL);         
                           \
-                            memcpy(dst1, src, 16 * 18 * SIZEOF_PIXEL);         
                           \
-                            call_ref(dst0, src, 16 * SIZEOF_PIXEL, block_size, 
x, y);                     \
-                            call_new(dst1, src, 16 * SIZEOF_PIXEL, block_size, 
x, y);                     \
-                            if (memcmp(dst0, dst1, 16 * 16 * SIZEOF_PIXEL)) {  
                           \
-                                fprintf(stderr, #name "_%d: x:%i, y:%i\n", 
bit_depth, x, y);              \
+                    for (int i = 0; heights[size][i]; i++) {                   
                           \
+                        int height = heights[size][i];                         
                           \
+                        for (int xy = 0; xy < 64; xy++) {                      
                           \
+                            int x = xy & 7, y = xy >> 3;                       
                           \
+                            /* src has no alignment requirement */             
                           \
+                            int off = (rnd() & 7) * SIZEOF_PIXEL;              
                           \
+                            randomize_buffers(bit_depth);                      
                           \
+                            call_ref(dst0, src + off, STRIDE, height, x, y);   
                           \
+                            call_new(dst1, src + off, STRIDE, height, x, y);   
                           \
+                            if (memcmp(dst0, dst1, BUF_SIZE)) {                
                           \
+                                fprintf(stderr, #name "_%d: h:%i, x:%i, y:%i, 
src offset:%i\n",           \
+                                        bit_depth, height, x, y, off);         
                           \
                                 fail();                                        
                           \
                             }                                                  
                           \
-                            bench_new(dst1, src, 16 * SIZEOF_PIXEL, 
block_size, x, y);                    \
                         }                                                      
                           \
                     }                                                          
                           \
+                    bench_new(dst1, src + SIZEOF_PIXEL, STRIDE, block_size, 3, 
5);                        \
                 }                                                              
                           \
             } while (0)
 
-- 
2.54.0


>From c9c5167ccc1ecc63c644f491ebe19ae89c2d2e81 Mon Sep 17 00:00:00 2001
From: Kieran Kunhya <[email protected]>
Date: Wed, 11 Nov 2026 09:43:39 +0000
Subject: [PATCH 4/6] checkasm/h264dsp: test weight and biweight at all H.264
 block heights

Add the 4x16 and 2x8 blocks, which 4:2:2 chroma uses, and implicit
bi-prediction weights: log2_denom 5 with weightd from -64 to 128 and
weightd + weights = 64. Explicit weights never reach 128, so this is
the only way the weight == 128 path of the x86 biweight functions is
tested.
---
 tests/checkasm/h264dsp.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/tests/checkasm/h264dsp.c b/tests/checkasm/h264dsp.c
index 4f867949644..b340b4c7818 100644
--- a/tests/checkasm/h264dsp.c
+++ b/tests/checkasm/h264dsp.c
@@ -598,7 +598,7 @@ static void check_weight(void)
 
             if (check_func(h.weight_pixels_tab[idx], "weight_%dx%d_%d",
                            w, 16, bit_depth)) {
-                for (int hgt = FFMIN(16, 2 * w);
+                for (int hgt = FFMIN(16, 4 * w);
                      hgt >= FFMAX(2, w / 2); hgt >>= 1) {
                     for (int i = 0; i < 32; i++) {
                         int stride = 32 * SIZEOF_PIXEL;
@@ -646,19 +646,28 @@ static void check_biweight(void)
 
             if (check_func(h.biweight_pixels_tab[idx], "biweight_%dx%d_%d",
                            w, 16, bit_depth)) {
-                for (int hgt = FFMIN(16, 2 * w);
+                for (int hgt = FFMIN(16, 4 * w);
                      hgt >= FFMAX(2, w / 2); hgt >>= 1) {
                     for (int i = 0; i < 32; i++) {
                         int stride = 32 * SIZEOF_PIXEL;
-                        int log2_denom = rnd() % 8;
+                        int log2_denom, weightd, weights, offset;
 
-                        int weightd, weights;
-                        do {
-                            weightd = rnd() % 256 - 128;
-                            weights = rnd() % 256 - 128;
-                        } while (weightd + weights < -128 ||
-                                 weightd + weights > (log2_denom == 7 ? 127 : 
128));
-                        int offset = (rnd() % 256 - 128) + (rnd() % 256 - 128);
+                        if (i & 3) {
+                            log2_denom = rnd() % 8;
+                            do {
+                                weightd = rnd() % 256 - 128;
+                                weights = rnd() % 256 - 128;
+                            } while (weightd + weights < -128 ||
+                                     weightd + weights > (log2_denom == 7 ? 
127 : 128));
+                            offset = (rnd() % 256 - 128) + (rnd() % 256 - 128);
+                        } else {
+                            // Implicit weights: log2_denom 5,
+                            // -64 <= weightd <= 128, weightd + weights = 64
+                            log2_denom = 5;
+                            weightd    = rnd() % 193 - 64;
+                            weights    = 64 - weightd;
+                            offset     = 0;
+                        }
 
                         memset(dst, 0, 32 * 32 * 2);
                         memset(src, 0, 32 * 32 * 2);
-- 
2.54.0


>From 80c1d1583156b88c4cddad255084b673d8a4d881 Mon Sep 17 00:00:00 2001
From: Kieran Kunhya <[email protected]>
Date: Wed, 11 Nov 2026 09:43:58 +0000
Subject: [PATCH 5/6] checkasm/h264dsp: test the DC-only blocks of idct_add8

idct_add8 adds a block with idct_dc_add when its nnz is 0 but its DC
coefficient is not, which is the usual case for chroma, where the DC
comes from the chroma DC transform. The test only had blocks with
nnz 0 and no coefficients, so that path was never taken.
---
 tests/checkasm/h264dsp.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/tests/checkasm/h264dsp.c b/tests/checkasm/h264dsp.c
index b340b4c7818..eb0b6e4bf8f 100644
--- a/tests/checkasm/h264dsp.c
+++ b/tests/checkasm/h264dsp.c
@@ -364,15 +364,19 @@ static void check_idct_add8(void)
                     int col = (k % 4) * 4;
                     int offset = (row * 16 + col) * SIZEOF_PIXEL +
                                  (j - 1) * 16 * 16 * SIZEOF_PIXEL;
-                    int nnz = rnd() % 3;
+                    // 0: no coefficients, 1: DC, 2: all, 3: DC only from
+                    // the chroma DC transform, which leaves nnz at 0
+                    int nnz = rnd() % 4;
                     int16_t *cblk = &coef_full[i * 16 * SIZEOF_COEF / 2];
 
                     randomize_buffers(bit_depth - 8);
                     dct4x4(coef, bit_depth);
 
-                    if (nnz > 1)
+                    if (nnz == 2)
                         nnz = 16;
-                    memcpy(cblk, coef, nnz * SIZEOF_COEF);
+                    memcpy(cblk, coef, (nnz == 3 ? 1 : nnz) * SIZEOF_COEF);
+                    if (nnz == 3)
+                        nnz = 0;
 
                     for (int y = 0; y < 4; y++)
                         memcpy(&dst_full[offset + y * 16 * SIZEOF_PIXEL],
-- 
2.54.0


>From 5c8a925b8132d2d655842fadcc49489bb25be64a Mon Sep 17 00:00:00 2001
From: Kieran Kunhya <[email protected]>
Date: Wed, 11 Nov 2026 09:45:25 +0000
Subject: [PATCH 6/6] checkasm/h264dsp: test loop_filter_strength

The C version is inlined in h264_loopfilter.c and the function pointer is
SIMD only, so the reference is a local model of the decoder's bS rule
(check_mv() plus nnz). The inputs follow fill_filter_caches(): P (L0) and
B (L0, L1 or bi) partitions of 16x16, 16x8, 8x16 and 8x8 for the current
MB, intra/unavailable or partitioned left and top neighbours, unused lists
as LIST_NOT_USED with zero mvs, frame and field mvy limits, and the
decoder's edges, step, mask_mv0 and mask_mv1 arguments. Only the edges
the decoder reads are compared.
---
 tests/checkasm/h264dsp.c | 203 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 203 insertions(+)

diff --git a/tests/checkasm/h264dsp.c b/tests/checkasm/h264dsp.c
index eb0b6e4bf8f..05f5721b0cd 100644
--- a/tests/checkasm/h264dsp.c
+++ b/tests/checkasm/h264dsp.c
@@ -585,6 +585,206 @@ static void check_loop_filter_intra(void)
     }
 }
 
+/* The decoder's mv-based bS rule (check_mv() in h264_loopfilter.c): 1 if
+ * the refs differ or an mv component differs by >= 4 (y: mvy_limit). */
+static int strength_check_mv(int8_t ref[2][40], int16_t mv[2][40][2],
+                             int b, int bn, int bidir, int mvy_limit)
+{
+#define MV_DIFF(l0, l1) (mv[l0][b][0] - mv[l1][bn][0] + 3 >= 7U || \
+                         FFABS(mv[l0][b][1] - mv[l1][bn][1]) >= mvy_limit)
+    int v = ref[0][b] != ref[0][bn];
+    if (!v && ref[0][b] != -1)
+        v = MV_DIFF(0, 0);
+    if (bidir) {
+        if (!v)
+            v = ref[1][b] != ref[1][bn] || MV_DIFF(1, 1);
+        if (v) {
+            /* the same two predictions with the lists swapped */
+            if (ref[0][b] != ref[1][bn] || ref[1][b] != ref[0][bn])
+                return 1;
+            return MV_DIFF(0, 1) || MV_DIFF(1, 0);
+        }
+    }
+    return v;
+#undef MV_DIFF
+}
+
+/* bS for the internal and left/top edges of one non-intra MB: 2 if either
+ * side has coefficients, else the mv rule. Only the edges the decoder reads
+ * (0..edges-1 in steps of step, both directions) are defined; the others
+ * are scratch the function may leave or overwrite. */
+static void strength_ref(int16_t bS[2][4][4], const uint8_t *nnz,
+                         int8_t ref[2][40], int16_t mv[2][40][2], int bidir,
+                         int edges, int step, int mvy_limit)
+{
+    for (int edge = 0; edge < edges; edge += step) {
+        for (int i = 0; i < 4; i++) {
+            int b = 12 + edge + 8 * i, bn = b - 1;
+            bS[0][edge][i] = (nnz[b] | nnz[bn]) ? 2 :
+                             strength_check_mv(ref, mv, b, bn, bidir, 
mvy_limit);
+            b  = 12 + 8 * edge + i;
+            bn = b - 8;
+            bS[1][edge][i] = (nnz[b] | nnz[bn]) ? 2 :
+                             strength_check_mv(ref, mv, b, bn, bidir, 
mvy_limit);
+        }
+    }
+}
+
+static int strength_differs(int16_t bs0[2][4][4], int16_t bs1[2][4][4],
+                            int edges, int step)
+{
+    for (int dir = 0; dir < 2; dir++)
+        for (int edge = 0; edge < edges; edge += step)
+            if (memcmp(bs0[dir][edge], bs1[dir][edge], 4 * sizeof(int16_t)))
+                return 1;
+    return 0;
+}
+
+/* One partition's motion. P: L0; B: L0, L1 or bi. Unused lists are
+ * LIST_NOT_USED with zero mvs, as fill_filter_caches() leaves them. The mvs
+ * sit near base, so that the +-4 / +-2 thresholds are crossed both ways. */
+static void strength_rnd_motion(int8_t r[2], int16_t m[2][2], int bidir,
+                                const int base[2])
+{
+    int use = bidir ? 1 + rnd() % 3 : 1;
+    for (int l = 0; l < 2; l++) {
+        if (use & (1 << l)) {
+            r[l]    = rnd() % 3;
+            m[l][0] = base[0] + (int)(rnd() % 11) - 5;
+            m[l][1] = base[1] + (int)(rnd() % 11) - 5;
+        } else {
+            r[l]    = -1;
+            m[l][0] = m[l][1] = 0;
+        }
+    }
+}
+
+static void strength_set(int8_t ref[2][40], int16_t mv[2][40][2], int idx,
+                         const int8_t r[2], int16_t m[2][2])
+{
+    for (int l = 0; l < 2; l++) {
+        ref[l][idx]   = r[l];
+        mv[l][idx][0] = m[l][0];
+        mv[l][idx][1] = m[l][1];
+    }
+}
+
+static void check_loop_filter_strength(void)
+{
+    LOCAL_ALIGNED_16(int16_t, bs0, [2 * 4 * 4]);
+    LOCAL_ALIGNED_16(int16_t, bs1, [2 * 4 * 4]);
+    LOCAL_ALIGNED_16(uint8_t, nnz, [40]);
+    /* the bidir SSE2 version loads 8 ref bytes at list 1 + 36 */
+    LOCAL_ALIGNED_16(int8_t,  refbuf, [2 * 40 + 16]);
+    LOCAL_ALIGNED_16(int16_t, mvbuf,  [2 * 40 * 2]);
+    int8_t  (*ref)[40]   = (int8_t (*)[40])refbuf;
+    int16_t (*mv)[40][2] = (int16_t (*)[40][2])mvbuf;
+    /* the decoder's mask_edge1 for 16x16, 16x8, 8x16 and 8x8 */
+    static const int mask_edge1[4] = { 3, 1, 3, 4 };
+    H264DSPContext h;
+    int bidir = 0, edges = 4, step = 1, mask_mv0 = 0, mask_mv1 = 0, field = 0;
+    declare_func(void, int16_t bS[2][4][4], uint8_t nnz[40],
+                 int8_t ref[2][40], int16_t mv[2][40][2], int bidir,
+                 int edges, int step, int mask_mv0, int mask_mv1, int field);
+
+    ff_h264dsp_init(&h, 8, 1);
+    /* SIMD only: the C version is inlined in h264_loopfilter.c, so the
+     * reference is strength_ref() */
+    if (!check_func(h.loop_filter_strength, "loop_filter_strength"))
+        return;
+
+    for (int run = 0; run < 4096; run++) {
+        int8_t  pr[16][2], r[2];
+        int16_t pm[16][2][2], m[2][2];
+        int base[2], ptype, left, top, mvy_limit;
+
+        bidir     = rnd() & 1;
+        field     = rnd() & 1;
+        mvy_limit = field ? 2 : 4;
+        /* mvs well inside the level limits (x +-8192, y +-2048 qpel) */
+        base[0] = (int)(rnd() % 16001) - 8000;
+        base[1] = (int)(rnd() % 4001) - 2000;
+
+        for (int i = 0; i < 40; i++)
+            nnz[i] = (rnd() & 1) ? 0 : 1 + rnd() % 16;
+        for (int i = 0; i < 2 * 40 + 16; i++)
+            refbuf[i] = rnd();
+        for (int i = 0; i < 2 * 40 * 2; i++)
+            mvbuf[i] = rnd();
+
+        /* current MB: 16x16, 16x8, 8x16 or 8x8 (at 4x4 granularity) */
+        ptype = rnd() & 3;
+        for (int i = 0; i < 16; i++) {
+            int far[2];
+            far[0] = (int)(rnd() % 16001) - 8000;
+            far[1] = (int)(rnd() % 4001) - 2000;
+            strength_rnd_motion(pr[i], pm[i], bidir, (rnd() & 7) ? base : far);
+        }
+        for (int y = 0; y < 4; y++) {
+            for (int x = 0; x < 4; x++) {
+                int part = ptype == 0 ? 0 : ptype == 1 ? y >> 1 :
+                           ptype == 2 ? x >> 1 : x + 4 * y;
+                strength_set(ref, mv, 12 + x + 8 * y, pr[part], pm[part]);
+            }
+        }
+
+        r[0] = r[1] = -1;
+        memset(m, 0, sizeof(m));
+
+        /* left MB column: intra or unavailable, 16x16/8x16 (one motion),
+         * 16x8 (two) or 8x8 (four) */
+        left = rnd() & 3;
+        for (int y = 0; y < 4; y++) {
+            if (!left || (left == 1 && y) || (left == 2 && (y & 1))) {
+                if (!left) {
+                    r[0] = r[1] = -1;
+                    memset(m, 0, sizeof(m));
+                }
+            } else {
+                strength_rnd_motion(r, m, bidir, base);
+            }
+            strength_set(ref, mv, 11 + 8 * y, r, m);
+        }
+
+        /* top MB row: intra or unavailable, or inter */
+        top = rnd() & 1;
+        for (int x = 0; x < 4; x++) {
+            if (!top) {
+                r[0] = r[1] = -1;
+                memset(m, 0, sizeof(m));
+            } else {
+                strength_rnd_motion(r, m, bidir, base);
+            }
+            strength_set(ref, mv, 4 + x, r, m);
+        }
+
+        /* The decoder's arguments: the mv checks are skipped along the
+         * edges that lie inside one partition. edges 1: 16x16 without luma
+         * coefficients, so without the 8x8 transform (step 1). */
+        edges    = ptype == 0 && (rnd() & 1) ? 1 : 4;
+        step     = edges == 4 ? 1 + (rnd() & 1) : 1;
+        mask_mv1 = mask_edge1[ptype];
+        mask_mv0 = (mask_mv1 & 2) && left == 1 ? 3 : 0;
+
+        for (int i = 0; i < 2 * 4 * 4; i++)
+            bs0[i] = bs1[i] = rnd();
+        strength_ref((int16_t (*)[4][4])bs0, nnz, ref, mv, bidir, edges, step,
+                     mvy_limit);
+        call_new((int16_t (*)[4][4])bs1, nnz, ref, mv, bidir, edges, step,
+                 mask_mv0, mask_mv1, field);
+        if (strength_differs((int16_t (*)[4][4])bs0, (int16_t (*)[4][4])bs1,
+                             edges, step)) {
+            fprintf(stderr, "loop_filter_strength: bidir %d edges %d step %d "
+                    "mask_mv0 %d mask_mv1 %d field %d ptype %d left %d top 
%d\n",
+                    bidir, edges, step, mask_mv0, mask_mv1, field, ptype, 
left, top);
+            fail();
+            break;
+        }
+    }
+    bench_new((int16_t (*)[4][4])bs1, nnz, ref, mv, bidir, edges, step,
+              mask_mv0, mask_mv1, field);
+}
+
 static void check_weight(void)
 {
     LOCAL_ALIGNED_16(uint8_t, dst, [32 * 32 * 2]);
@@ -714,6 +914,9 @@ void checkasm_check_h264dsp(void)
     check_loop_filter_intra();
     report("loop_filter_intra");
 
+    check_loop_filter_strength();
+    report("loop_filter_strength");
+
     check_weight();
     report("weight");
 
-- 
2.54.0

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

Reply via email to