Move cavs_idct8_add_int32() from cavsdec.c to cavsdsp.c as
ff_cavs_idct8_add_int32() and cover it in libavcodec/tests/cavsdsp:
against a matrix-product reference with 64-bit sums, including
coefficients beyond the int16 range that only this variant accepts,
and byte-for-byte against the 16-bit C version for in-range inputs
(220000 blocks total).

Co-Authored-By: kimi-k3 with kimi-code
Signed-off-by: jianfeng.zheng <[email protected]>
---
 libavcodec/cavsdec.c       | 79 +-------------------------------------
 libavcodec/cavsdsp.c       | 76 ++++++++++++++++++++++++++++++++++++
 libavcodec/cavsdsp.h       |  2 +
 libavcodec/tests/cavsdsp.c | 70 +++++++++++++++++++++++++++++++++
 4 files changed, 150 insertions(+), 77 deletions(-)

diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index c1c8eca67b..baa44035cb 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -692,81 +692,6 @@ static inline int dequant32(AVSContext *h, int16_t 
*level_buf, uint8_t *run_buf,
     return 0;
 }
 
-/**
- * AEC path variant of cavs_idct8_add() taking 32-bit coefficients, matching
- * inv_transform_B8() of the GDM reference: same butterflies as
- * cavs_idct8_add_c(), same clipping of the row and column results to int16,
- * but the (possibly out-of-int16-range) input is not truncated first
- */
-static void cavs_idct8_add_int32(uint8_t *dst, int *block, ptrdiff_t stride)
-{
-    int i;
-    int (*src)[8] = (int(*)[8])block;
-
-    for (i = 0; i < 8; i++) {
-        const int a0 = 3 * src[i][1] - 2 * src[i][7];
-        const int a1 = 3 * src[i][3] + 2 * src[i][5];
-        const int a2 = 2 * src[i][3] - 3 * src[i][5];
-        const int a3 = 2 * src[i][1] + 3 * src[i][7];
-
-        const int b4 = 2 * (a0 + a1 + a3) + a1;
-        const int b5 = 2 * (a0 - a1 + a2) + a0;
-        const int b6 = 2 * (a3 - a2 - a1) + a3;
-        const int b7 = 2 * (a0 - a2 - a3) - a2;
-
-        const int a7 = 4 * src[i][2] - 10 * src[i][6];
-        const int a6 = 4 * src[i][6] + 10 * src[i][2];
-        const int a5 = 8 * (src[i][0] - src[i][4]) + 4;
-        const int a4 = 8 * (src[i][0] + src[i][4]) + 4;
-
-        const int b0 = a4 + a6;
-        const int b1 = a5 + a7;
-        const int b2 = a5 - a7;
-        const int b3 = a4 - a6;
-
-        /* the GDM reference clips the row results to int16 before shifting */
-        src[i][0] = av_clip_int16(b0 + b4) >> 3;
-        src[i][1] = av_clip_int16(b1 + b5) >> 3;
-        src[i][2] = av_clip_int16(b2 + b6) >> 3;
-        src[i][3] = av_clip_int16(b3 + b7) >> 3;
-        src[i][4] = av_clip_int16(b3 - b7) >> 3;
-        src[i][5] = av_clip_int16(b2 - b6) >> 3;
-        src[i][6] = av_clip_int16(b1 - b5) >> 3;
-        src[i][7] = av_clip_int16(b0 - b4) >> 3;
-    }
-    for (i = 0; i < 8; i++) {
-        const int a0 = 3 * src[1][i] - 2 * src[7][i];
-        const int a1 = 3 * src[3][i] + 2 * src[5][i];
-        const int a2 = 2 * src[3][i] - 3 * src[5][i];
-        const int a3 = 2 * src[1][i] + 3 * src[7][i];
-
-        const int b4 = 2 * (a0 + a1 + a3) + a1;
-        const int b5 = 2 * (a0 - a1 + a2) + a0;
-        const int b6 = 2 * (a3 - a2 - a1) + a3;
-        const int b7 = 2 * (a0 - a2 - a3) - a2;
-
-        const int a7 = 4 * src[2][i] - 10 * src[6][i];
-        const int a6 = 4 * src[6][i] + 10 * src[2][i];
-        const int a5 = 8 * (src[0][i] - src[4][i]);
-        const int a4 = 8 * (src[0][i] + src[4][i]);
-
-        const int b0 = a4 + a6;
-        const int b1 = a5 + a7;
-        const int b2 = a5 - a7;
-        const int b3 = a4 - a6;
-
-        /* GDM clips the column results (including the +64 rounding) to
-         * int16 before shifting */
-        dst[i + 0*stride] = av_clip_uint8( dst[i + 0*stride] + 
(av_clip_int16(b0 + b4 + 64) >> 7));
-        dst[i + 1*stride] = av_clip_uint8( dst[i + 1*stride] + 
(av_clip_int16(b1 + b5 + 64) >> 7));
-        dst[i + 2*stride] = av_clip_uint8( dst[i + 2*stride] + 
(av_clip_int16(b2 + b6 + 64) >> 7));
-        dst[i + 3*stride] = av_clip_uint8( dst[i + 3*stride] + 
(av_clip_int16(b3 + b7 + 64) >> 7));
-        dst[i + 4*stride] = av_clip_uint8( dst[i + 4*stride] + 
(av_clip_int16(b3 - b7 + 64) >> 7));
-        dst[i + 5*stride] = av_clip_uint8( dst[i + 5*stride] + 
(av_clip_int16(b2 - b6 + 64) >> 7));
-        dst[i + 6*stride] = av_clip_uint8( dst[i + 6*stride] + 
(av_clip_int16(b1 - b5 + 64) >> 7));
-        dst[i + 7*stride] = av_clip_uint8( dst[i + 7*stride] + 
(av_clip_int16(b0 - b4 + 64) >> 7));
-    }
-}
 
 /**
  * decode coefficients from one 8x8 block, dequantize, inverse transform
@@ -823,7 +748,7 @@ static int decode_residual_block(AVSContext *h, 
GetBitContext *gb,
 
         if ((ret = dequant32(h, level_buf, run_buf, block32, dequant_mul[qp],
                              dequant_shift[qp], i)) >= 0)
-            cavs_idct8_add_int32(dst, block32, stride);
+            ff_cavs_idct8_add_int32(dst, block32, stride);
         /* dequant32() can write before it fails, so always clear the block */
         memset(block32, 0, 64 * sizeof(*block32));
         return ret;
@@ -1225,7 +1150,7 @@ static int decode_residual_block_aec(AVSContext *h, int 
chroma, int qp,
     ret = dequant32(h, level_buf, run_buf, block32, dequant_mul[qp],
                     dequant_shift[qp], i);
     if (ret >= 0)
-        cavs_idct8_add_int32(dst, block32, stride);
+        ff_cavs_idct8_add_int32(dst, block32, stride);
     memset(block32, 0, 64 * sizeof(*block32));
     return ret;
 }
diff --git a/libavcodec/cavsdsp.c b/libavcodec/cavsdsp.c
index 684ce290ea..9cd9ac77e8 100644
--- a/libavcodec/cavsdsp.c
+++ b/libavcodec/cavsdsp.c
@@ -548,6 +548,82 @@ CAVS_MC(avg_, 16)
 #define put_cavs_qpel16_mc00_c ff_put_pixels16x16_c
 #define avg_cavs_qpel16_mc00_c ff_avg_pixels16x16_c
 
+/**
+ * AEC path variant of cavs_idct8_add() taking 32-bit coefficients, matching
+ * inv_transform_B8() of the GDM reference: same butterflies as
+ * cavs_idct8_add_c(), same clipping of the row and column results to int16,
+ * but the (possibly out-of-int16-range) input is not truncated first
+ */
+void ff_cavs_idct8_add_int32(uint8_t *dst, int *block, ptrdiff_t stride)
+{
+    int i;
+    int (*src)[8] = (int(*)[8])block;
+
+    for (i = 0; i < 8; i++) {
+        const int a0 = 3 * src[i][1] - 2 * src[i][7];
+        const int a1 = 3 * src[i][3] + 2 * src[i][5];
+        const int a2 = 2 * src[i][3] - 3 * src[i][5];
+        const int a3 = 2 * src[i][1] + 3 * src[i][7];
+
+        const int b4 = 2 * (a0 + a1 + a3) + a1;
+        const int b5 = 2 * (a0 - a1 + a2) + a0;
+        const int b6 = 2 * (a3 - a2 - a1) + a3;
+        const int b7 = 2 * (a0 - a2 - a3) - a2;
+
+        const int a7 = 4 * src[i][2] - 10 * src[i][6];
+        const int a6 = 4 * src[i][6] + 10 * src[i][2];
+        const int a5 = 8 * (src[i][0] - src[i][4]) + 4;
+        const int a4 = 8 * (src[i][0] + src[i][4]) + 4;
+
+        const int b0 = a4 + a6;
+        const int b1 = a5 + a7;
+        const int b2 = a5 - a7;
+        const int b3 = a4 - a6;
+
+        /* the GDM reference clips the row results to int16 before shifting */
+        src[i][0] = av_clip_int16(b0 + b4) >> 3;
+        src[i][1] = av_clip_int16(b1 + b5) >> 3;
+        src[i][2] = av_clip_int16(b2 + b6) >> 3;
+        src[i][3] = av_clip_int16(b3 + b7) >> 3;
+        src[i][4] = av_clip_int16(b3 - b7) >> 3;
+        src[i][5] = av_clip_int16(b2 - b6) >> 3;
+        src[i][6] = av_clip_int16(b1 - b5) >> 3;
+        src[i][7] = av_clip_int16(b0 - b4) >> 3;
+    }
+    for (i = 0; i < 8; i++) {
+        const int a0 = 3 * src[1][i] - 2 * src[7][i];
+        const int a1 = 3 * src[3][i] + 2 * src[5][i];
+        const int a2 = 2 * src[3][i] - 3 * src[5][i];
+        const int a3 = 2 * src[1][i] + 3 * src[7][i];
+
+        const int b4 = 2 * (a0 + a1 + a3) + a1;
+        const int b5 = 2 * (a0 - a1 + a2) + a0;
+        const int b6 = 2 * (a3 - a2 - a1) + a3;
+        const int b7 = 2 * (a0 - a2 - a3) - a2;
+
+        const int a7 = 4 * src[2][i] - 10 * src[6][i];
+        const int a6 = 4 * src[6][i] + 10 * src[2][i];
+        const int a5 = 8 * (src[0][i] - src[4][i]);
+        const int a4 = 8 * (src[0][i] + src[4][i]);
+
+        const int b0 = a4 + a6;
+        const int b1 = a5 + a7;
+        const int b2 = a5 - a7;
+        const int b3 = a4 - a6;
+
+        /* GDM clips the column results (including the +64 rounding) to
+         * int16 before shifting */
+        dst[i + 0*stride] = av_clip_uint8( dst[i + 0*stride] + 
(av_clip_int16(b0 + b4 + 64) >> 7));
+        dst[i + 1*stride] = av_clip_uint8( dst[i + 1*stride] + 
(av_clip_int16(b1 + b5 + 64) >> 7));
+        dst[i + 2*stride] = av_clip_uint8( dst[i + 2*stride] + 
(av_clip_int16(b2 + b6 + 64) >> 7));
+        dst[i + 3*stride] = av_clip_uint8( dst[i + 3*stride] + 
(av_clip_int16(b3 + b7 + 64) >> 7));
+        dst[i + 4*stride] = av_clip_uint8( dst[i + 4*stride] + 
(av_clip_int16(b3 - b7 + 64) >> 7));
+        dst[i + 5*stride] = av_clip_uint8( dst[i + 5*stride] + 
(av_clip_int16(b2 - b6 + 64) >> 7));
+        dst[i + 6*stride] = av_clip_uint8( dst[i + 6*stride] + 
(av_clip_int16(b1 - b5 + 64) >> 7));
+        dst[i + 7*stride] = av_clip_uint8( dst[i + 7*stride] + 
(av_clip_int16(b0 - b4 + 64) >> 7));
+    }
+}
+
 av_cold void ff_cavsdsp_init(CAVSDSPContext* c)
 {
 #define dspfunc(PFX, IDX, NUM) \
diff --git a/libavcodec/cavsdsp.h b/libavcodec/cavsdsp.h
index 57cee4939f..0826a3a171 100644
--- a/libavcodec/cavsdsp.h
+++ b/libavcodec/cavsdsp.h
@@ -38,6 +38,8 @@ typedef struct CAVSDSPContext {
     int idct_perm;
 } CAVSDSPContext;
 
+void ff_cavs_idct8_add_int32(uint8_t *dst, int *block, ptrdiff_t stride);
+
 void ff_cavsdsp_init(CAVSDSPContext* c);
 void ff_cavsdsp_init_x86(CAVSDSPContext* c);
 
diff --git a/libavcodec/tests/cavsdsp.c b/libavcodec/tests/cavsdsp.c
index d91d7ea256..37af2118e3 100644
--- a/libavcodec/tests/cavsdsp.c
+++ b/libavcodec/tests/cavsdsp.c
@@ -70,6 +70,55 @@ static void idct8_add_ref(uint8_t *dst, const int16_t 
coeffs[64])
     }
 }
 
+/**
+ * Reference for the 32-bit input variant: same butterflies, but the
+ * (possibly out-of-int16-range) input is not truncated first; sums
+ * are computed with 64-bit intermediates.
+ */
+static void idct8_add_int32_ref(uint8_t *dst, const int coeffs[64])
+{
+    int tmp[8][8];
+
+    for (int i = 0; i < 8; i++) {
+        for (int j = 0; j < 8; j++) {
+            int64_t sum = 4;
+            for (int k = 0; k < 8; k++)
+                sum += (int64_t)coeffs[8 * i + k] * t8[j][k];
+            tmp[i][j] = av_clip_int16(sum) >> 3;
+        }
+    }
+    for (int i = 0; i < 8; i++) {
+        for (int j = 0; j < 8; j++) {
+            int sum = 64;
+            for (int k = 0; k < 8; k++)
+                sum += t8[i][k] * tmp[k][j];
+            dst[j] = av_clip_uint8(dst[j] + (av_clip_int16(sum) >> 7));
+        }
+        dst += STRIDE;
+    }
+}
+
+static int check_block32(const int coeffs[64], AVLFG *lfg, int n)
+{
+    DECLARE_ALIGNED(16, int, block)[64];
+    DECLARE_ALIGNED(16, uint8_t, ref)[8 * STRIDE];
+    DECLARE_ALIGNED(16, uint8_t, out)[8 * STRIDE];
+
+    /* the 32-bit variant expects unpermuted coefficients */
+    for (int i = 0; i < 64; i++)
+        block[i] = coeffs[i];
+    for (int i = 0; i < 8 * STRIDE; i++)
+        ref[i] = out[i] = av_lfg_get(lfg);
+
+    idct8_add_int32_ref(ref, coeffs);
+    ff_cavs_idct8_add_int32(out, block, STRIDE);
+    if (memcmp(ref, out, sizeof(ref))) {
+        fprintf(stderr, "cavs_idct8_add_int32: mismatch in block %d\n", n);
+        return 1;
+    }
+    return 0;
+}
+
 static int check_block(CAVSDSPContext *c, const uint8_t *perm,
                        const int16_t coeffs[64], AVLFG *lfg, int n)
 {
@@ -116,6 +165,7 @@ int main(void)
     /* magnitudes from the whole int16_t range down to single digits */
     for (int iter = 0; iter < 100000; iter++) {
         int count = 1 + av_lfg_get(&lfg) % 64;
+        int coeffs32[64] = { 0 };
 
         memset(coeffs, 0, sizeof(coeffs));
         for (int k = 0; k < count; k++) {
@@ -123,6 +173,26 @@ int main(void)
             coeffs[r & 63] = (int16_t)av_lfg_get(&lfg) >> (r >> 6 & 15);
         }
         ret |= check_block(&c, perm, coeffs, &lfg, n++);
+        /* the int32 variant must agree with the 16-bit C version for
+         * int16-range inputs */
+        for (int i = 0; i < 64; i++)
+            coeffs32[i] = coeffs[i];
+        ret |= check_block32(coeffs32, &lfg, n++);
+    }
+
+    /* and out-of-int16-range coefficients, which only it accepts; bound
+     * them to the range the decoder can produce (dequant32 output is
+     * well within 1<<24, which keeps the butterfly sums in int range) */
+    for (int iter = 0; iter < 10000; iter++) {
+        int coeffs32[64] = { 0 };
+        int count = 1 + av_lfg_get(&lfg) % 64;
+
+        for (int k = 0; k < count; k++) {
+            unsigned r = av_lfg_get(&lfg);
+            coeffs32[r & 63] = av_clip((int)av_lfg_get(&lfg) >> (r >> 6 & 15),
+                                       -(1 << 24), (1 << 24) - 1);
+        }
+        ret |= check_block32(coeffs32, &lfg, n++);
     }
 
     return ret;
-- 
2.34.1

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

Reply via email to