The GDM reference decoder keeps dequantized coefficients in 32-bit
ints (rm52k truncates them to int16), so AEC streams with large levels
at low QPs must not be truncated to int16 before the inverse
transform.

Add dequant32(), which writes to an int32 block in natural scan order,
and cavs_idct8_add_int32(), a variant of cavs_idct8_add_c() matching
GDM's inv_transform_B8(): same butterflies and the same clipping of
the row and column results to int16, but without truncating the input
coefficients first.

Co-Authored-By: kimi-k3 with kimi-code
Signed-off-by: jianfeng.zheng <[email protected]>
---
 libavcodec/cavs.c    |   4 +-
 libavcodec/cavs.h    |   2 +
 libavcodec/cavsdec.c | 116 +++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 115 insertions(+), 7 deletions(-)

diff --git a/libavcodec/cavs.c b/libavcodec/cavs.c
index 8ce8168422..7c575345ef 100644
--- a/libavcodec/cavs.c
+++ b/libavcodec/cavs.c
@@ -63,7 +63,7 @@ static const cavs_vector un_mv = { 0, 0, 1, NOT_AVAIL, 0 };
  * Inverse block scan method 2 (clause 9.5.3 c), figure 23), for field
  * coded pictures, as the inverse ff_permute_scantable() expects.
  */
-static const uint8_t cavs_scan_field[64] = {
+const uint8_t ff_cavs_field_scan[64] = {
      0,  8, 16,  1, 24, 32,  9, 17, 40, 48, 25,  2, 10, 56, 33, 18,
      3, 41, 49, 26, 11, 19,  4, 57, 34, 12, 42, 27, 20, 50, 35, 28,
      5, 13, 58, 43, 36, 21,  6, 29, 51, 44, 14, 22, 37, 59, 52, 30,
@@ -1073,7 +1073,7 @@ av_cold int ff_cavs_init(AVCodecContext *avctx)
     ff_cavsdsp_init(&h->cdsp);
     ff_init_scantable_permutation(permutation, h->cdsp.idct_perm);
     ff_permute_scantable(h->permutated_scantable, ff_zigzag_direct, 
permutation);
-    ff_permute_scantable(h->permutated_scantable_field, cavs_scan_field,
+    ff_permute_scantable(h->permutated_scantable_field, ff_cavs_field_scan,
                          permutation);
     h->scantable = h->permutated_scantable;
 
diff --git a/libavcodec/cavs.h b/libavcodec/cavs.h
index f2f959f132..cae362a791 100644
--- a/libavcodec/cavs.h
+++ b/libavcodec/cavs.h
@@ -276,6 +276,7 @@ typedef struct AVSContext {
     int cbp422;        ///< coded block pattern of the second chroma 8x8
                        ///< blocks (4:2:2): bit0 = U, bit1 = V
     DECLARE_ALIGNED(32, int16_t, block)[64];
+    DECLARE_ALIGNED(32, int, block32)[64]; ///< AEC path: 32-bit coefficients
     uint8_t permutated_scantable[64];
     /** inverse block scan method 2 of clause 9.5.3 c), for field pictures */
     uint8_t permutated_scantable_field[64];
@@ -322,6 +323,7 @@ typedef struct AVSContext {
 } AVSContext;
 
 extern const uint8_t     ff_cavs_chroma_qp[64];
+extern const uint8_t     ff_cavs_field_scan[64];
 extern const uint8_t     ff_cavs_partition_flags[30];
 
 /** mark block as using intra prediction */
diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index 405f32493f..bc0c21917d 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -605,6 +605,111 @@ static inline int dequant(AVSContext *h, int16_t 
*level_buf, uint8_t *run_buf,
     return 0;
 }
 
+/**
+ * AEC path variant of dequant(): the GDM reference keeps the dequantized
+ * coefficients in 32-bit ints (rm52k truncates them to int16), so large
+ * levels at low QPs must not be truncated here
+ */
+static inline int dequant32(AVSContext *h, int16_t *level_buf, uint8_t 
*run_buf,
+                            int *dst, int mul, int shift, int coeff_num)
+{
+    int round = 1 << (shift - 1);
+    int pos = -1;
+    /* natural scan order: the AEC path uses its own 32-bit IDCT
+     * (cavs_idct8_add_int32) which expects unpermuted coefficients */
+    const uint8_t *scantab = h->pic_structure ? ff_zigzag_direct
+                                              : ff_cavs_field_scan;
+
+    /* inverse scan and dequantization */
+    while (--coeff_num >= 0) {
+        pos += run_buf[coeff_num];
+        if (pos > 63) {
+            av_log(h->avctx, AV_LOG_ERROR,
+                   "position out of block bounds at pic %d MB(%d,%d)\n",
+                   h->cur.poc, h->mbx, h->mby);
+            return AVERROR_INVALIDDATA;
+        }
+        dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
+    }
+    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
  *  and add them to sample block
@@ -963,7 +1068,6 @@ static int decode_residual_block_aec(AVSContext *h, int 
chroma, int qp,
 {
     int16_t level_buf[65];
     uint8_t run_buf[65];
-    int16_t *block = h->block;
     int lmax = 0, pos = 0, i, base, ret;
 
     if (h->pic_structure)
@@ -1046,11 +1150,13 @@ static int decode_residual_block_aec(AVSContext *h, int 
chroma, int qp,
         pos  = FFMIN(pos + run_val + 1, 63);
     }
 
-    ret = dequant(h, level_buf, run_buf, block, dequant_mul[qp],
-                  dequant_shift[qp], i);
+    int *block32 = h->block32;
+
+    ret = dequant32(h, level_buf, run_buf, block32, dequant_mul[qp],
+                    dequant_shift[qp], i);
     if (ret >= 0)
-        h->cdsp.cavs_idct8_add(dst, block, stride);
-    h->bdsp.clear_block(block);
+        cavs_idct8_add_int32(dst, block32, stride);
+    memset(block32, 0, 64 * sizeof(*block32));
     return ret;
 }
 
-- 
2.34.1

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

Reply via email to