Parse the slice weighting parameters (luma/chroma scale and shift per
reference, mb_weight_pred_flag) instead of rejecting the stream, read
the per-MB weighting_prediction flag (via the AEC decision at context
index 322 for AEC slices) and apply luma/chroma scale+shift to inter
prediction blocks. For bi-predicted blocks each direction is weighted
separately into a temporary buffer before averaging, matching the
RM52K/GDM4.2 reference decoders.

The weight parameter index of an MV follows the reference index order
of the reference list: the coded index for P slices, forward/backward
slots alternating per reference field for field B slices.

This also fixes bitstream desync on streams using weighted prediction
(previously decoding aborted with 'Weighted prediction is not
implemented').

Co-Authored-By: kimi-k3 with kimi-code
Signed-off-by: jianfeng.zheng <[email protected]>
---
 libavcodec/cavs.c    | 118 ++++++++++++++++++++++++++++++++++++++++++-
 libavcodec/cavs.h    |  13 +++++
 libavcodec/cavsdec.c |  63 +++++++++++++++++++++--
 3 files changed, 188 insertions(+), 6 deletions(-)

diff --git a/libavcodec/cavs.c b/libavcodec/cavs.c
index 399b25d6b4..8ce8168422 100644
--- a/libavcodec/cavs.c
+++ b/libavcodec/cavs.c
@@ -548,6 +548,68 @@ static inline void mc_dir_part(AVSContext *h, AVFrame 
*pic, int parity,
     chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx & 7, my_c & 7);
 }
 
+/*****************************************************************************
+ *
+ * weighted prediction
+ *
+ ****************************************************************************/
+
+static inline void cavs_wp_block(uint8_t *dst, ptrdiff_t stride, int width,
+                                 int height, int scale, int shift)
+{
+    int x, y;
+
+    for (y = 0; y < height; y++) {
+        for (x = 0; x < width; x++)
+            dst[x] = av_clip_uint8(((dst[x] * scale + 16) >> 5) + shift);
+        dst += stride;
+    }
+}
+
+static inline void cavs_avg_block(uint8_t *dst, const uint8_t *src,
+                                  ptrdiff_t stride, int width, int height)
+{
+    int x, y;
+
+    for (y = 0; y < height; y++) {
+        for (x = 0; x < width; x++)
+            dst[x] = (dst[x] + src[x] + 1) >> 1;
+        dst += stride;
+        src += stride;
+    }
+}
+
+/**
+ * apply weighted prediction to one prediction block
+ * @param chroma_width  chroma block width (height is 2x for 4:2:2)
+ * @param idx index into the weight parameter arrays
+ */
+static void cavs_wp_part(AVSContext *h, uint8_t *dest_y, uint8_t *dest_cb,
+                         uint8_t *dest_cr, int chroma_width, int chroma_height,
+                         int idx)
+{
+    int luma_size = chroma_width * 2;
+
+    cavs_wp_block(dest_y,  h->l_stride, luma_size, luma_size,
+                  h->luma_scale[idx], h->luma_shift[idx]);
+    cavs_wp_block(dest_cb, h->c_stride, chroma_width, chroma_height,
+                  h->chroma_scale[idx], h->chroma_shift[idx]);
+    cavs_wp_block(dest_cr, h->c_stride, chroma_width, chroma_height,
+                  h->chroma_scale[idx], h->chroma_shift[idx]);
+}
+
+static inline void cavs_avg_part(AVSContext *h, uint8_t *dest_y, uint8_t 
*dest_cb,
+                                 uint8_t *dest_cr, const uint8_t *tmp_y,
+                                 const uint8_t *tmp_cb, const uint8_t *tmp_cr,
+                                 int chroma_width, int chroma_height)
+{
+    int luma_size = chroma_width * 2;
+
+    cavs_avg_block(dest_y,  tmp_y,  h->l_stride, luma_size, luma_size);
+    cavs_avg_block(dest_cb, tmp_cb, h->c_stride, chroma_width, chroma_height);
+    cavs_avg_block(dest_cr, tmp_cr, h->c_stride, chroma_width, chroma_height);
+}
+
 static inline void mc_part_std(AVSContext *h, int chroma_height,
                                uint8_t *dest_y,
                                uint8_t *dest_cb,
@@ -561,7 +623,12 @@ static inline void mc_part_std(AVSContext *h, int 
chroma_height,
 {
     qpel_mc_func *qpix_op =  qpix_put;
     h264_chroma_mc_func chroma_op = chroma_put;
-    /* for 4:2:2 the chroma block keeps the luma block's height */
+    int has_fwd, has_bwd;
+    int fw_idx = 0, bw_idx = 0; ///< weight parameter indexes
+    /* chroma_width keeps the pre-doubled value; for 4:2:2 the chroma
+     * block keeps the luma block's height */
+    const int chroma_width = chroma_height;
+
     chroma_height <<= (h->chroma_format == 2);
 
     dest_y   += x_offset * 2 + y_offset * h->l_stride * 2;
@@ -571,7 +638,44 @@ static inline void mc_part_std(AVSContext *h, int 
chroma_height,
     /* the row is counted inside the current field (clause 3.24) */
     y_offset += 8 * h->field_mby;
 
-    if (mv->ref >= 0) {
+    has_fwd = mv->ref >= 0;
+    has_bwd = (mv + MV_BWD_OFFS)->ref >= 0;
+
+    if (h->cur.f->pict_type == AV_PICTURE_TYPE_B) {
+        if (!h->pic_structure) {
+            /* field B: the weight sets alternate forward/backward fields */
+            fw_idx = 2 * (mv->ref - h->ref_base[0]);
+            bw_idx = 2 * ((mv + MV_BWD_OFFS)->ref - h->ref_base[1]) + 1;
+        } else {
+            fw_idx = 0;
+            bw_idx = 1;
+        }
+    } else {
+        fw_idx = mv->ref - h->ref_base[0];
+    }
+
+    if (h->mb_weight_pred && has_fwd && has_bwd) {
+        /* weighted bi-prediction: weight each direction separately */
+        mc_dir_part(h, h->ref[mv->ref].f, h->ref[mv->ref].parity,
+                    chroma_height, 0,
+                    h->wp_y, h->wp_u, h->wp_v, x_offset, y_offset,
+                    qpix_put, chroma_put, mv);
+        cavs_wp_part(h, h->wp_y, h->wp_u, h->wp_v,
+                     chroma_width, chroma_height, fw_idx);
+        mc_dir_part(h, h->ref[(mv + MV_BWD_OFFS)->ref].f,
+                    h->ref[(mv + MV_BWD_OFFS)->ref].parity,
+                    chroma_height, 1,
+                    dest_y, dest_cb, dest_cr, x_offset, y_offset,
+                    qpix_put, chroma_put, mv + MV_BWD_OFFS);
+        cavs_wp_part(h, dest_y, dest_cb, dest_cr,
+                     chroma_width, chroma_height, bw_idx);
+        cavs_avg_part(h, dest_y, dest_cb, dest_cr,
+                      h->wp_y, h->wp_u, h->wp_v,
+                      chroma_width, chroma_height);
+        return;
+    }
+
+    if (has_fwd) {
         mc_dir_part(h, h->ref[mv->ref].f, h->ref[mv->ref].parity,
                     chroma_height, 0,
                     dest_y, dest_cb, dest_cr, x_offset, y_offset,
@@ -588,6 +692,15 @@ static inline void mc_part_std(AVSContext *h, int 
chroma_height,
                     dest_y, dest_cb, dest_cr, x_offset, y_offset,
                     qpix_op, chroma_op, mv);
     }
+
+    if (h->mb_weight_pred) {
+        if (has_fwd)
+            cavs_wp_part(h, dest_y, dest_cb, dest_cr,
+                         chroma_width, chroma_height, fw_idx);
+        else if (has_bwd)
+            cavs_wp_part(h, dest_y, dest_cb, dest_cr,
+                         chroma_width, chroma_height, bw_idx);
+    }
 }
 
 void ff_cavs_inter(AVSContext *h, enum cavs_mb mb_type)
@@ -1018,6 +1131,7 @@ av_cold int ff_cavs_end(AVCodecContext *avctx)
     av_freep(&h->col_mv);
     av_freep(&h->col_type_base);
     av_freep(&h->edge_emu_buffer);
+    av_freep(&h->wp_y);
     av_freep(&h->deemulated_buf);
     h->deemulated_buf_size = 0;
     av_freep(&h->stc_offset);
diff --git a/libavcodec/cavs.h b/libavcodec/cavs.h
index d40c8fe3e3..f2f959f132 100644
--- a/libavcodec/cavs.h
+++ b/libavcodec/cavs.h
@@ -222,6 +222,19 @@ typedef struct AVSContext {
     int loop_filter_disable;
     int alpha_offset, beta_offset;
     int ref_flag;
+
+    /** \defgroup slice weighting
+     * @{
+     */
+    uint32_t slice_weight_pred_flag : 1;
+    uint32_t mb_weight_pred_flag    : 1;
+    uint8_t luma_scale[4];
+    int8_t luma_shift[4];
+    uint8_t chroma_scale[4];
+    int8_t chroma_shift[4];
+    /**@}*/
+    int mb_weight_pred;  ///< apply weighted prediction to current MB
+    uint8_t *wp_y, *wp_u, *wp_v; ///< temp buffers for weighted bi-prediction
     int mbx, mby, mbidx; ///< macroblock coordinates
     int flags;         ///< availability flags of neighbouring macroblocks
     int stc;           ///< last start code
diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index 706b1efb65..405f32493f 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -684,6 +684,7 @@ static int decode_residual_block(AVSContext *h, 
GetBitContext *gb,
 #define AEC_COEFF_FRAME_CHROMA      124
 #define AEC_COEFF_FIELD_LUMA        190
 #define AEC_COEFF_FIELD_CHROMA      256
+#define AEC_MB_WEIGHT_PRED          322
 
 /* a conforming block's quantized coefficients span -2^(n+3)..2^(n+3)-1
  * with n the sample precision, 8 for both profiles (clause 9.6.2) */
@@ -1527,6 +1528,21 @@ static int decode_mb_p(AVSContext *h, enum cavs_mb 
mb_type)
                mb_type);
         return AVERROR_INVALIDDATA;
     }
+    if (h->slice_weight_pred_flag) {
+        /* skip MBs are never weighted when per-MB weights are signalled */
+        if (h->mb_weight_pred_flag) {
+            if (mb_type != P_SKIP)
+                h->mb_weight_pred = h->aec_enable
+                    ? ff_cavs_aec_decode_decision(&h->aec, AEC_MB_WEIGHT_PRED)
+                    : get_bits1(&h->gb);
+            else
+                h->mb_weight_pred = 0;
+        } else {
+            h->mb_weight_pred = 1;
+        }
+    } else {
+        h->mb_weight_pred = 0;
+    }
     ff_cavs_inter(h, mb_type);
     set_intra_mode_default(h);
     store_mvs(h);
@@ -1707,6 +1723,21 @@ static int decode_mb_b(AVSContext *h, enum cavs_mb 
mb_type)
                 MV(MV_BWD_X1, MV_BWD_C2, MV_PRED_TOPRIGHT, BLK_8X16, 
ref[1][1]);
         }
     }
+    if (h->slice_weight_pred_flag) {
+        /* skip MBs are never weighted when per-MB weights are signalled */
+        if (h->mb_weight_pred_flag) {
+            if (mb_type != B_SKIP)
+                h->mb_weight_pred = h->aec_enable
+                    ? ff_cavs_aec_decode_decision(&h->aec, AEC_MB_WEIGHT_PRED)
+                    : get_bits1(&h->gb);
+            else
+                h->mb_weight_pred = 0;
+        } else {
+            h->mb_weight_pred = 1;
+        }
+    } else {
+        h->mb_weight_pred = 0;
+    }
     ff_cavs_inter(h, mb_type);
     set_intra_mode_default(h);
     if (mb_type != B_SKIP) {
@@ -1804,11 +1835,24 @@ static inline int decode_slice_header(AVSContext *h, 
GetBitContext *gb)
     }
     /* inter frame or second slice can have weighting params */
     if ((h->cur.f->pict_type != AV_PICTURE_TYPE_I) ||
-        (!h->pic_structure && h->mbidx >= h->mb_width * h->mb_height / 2))
-        if (get_bits1(gb)) { //slice_weighting_flag
-            avpriv_request_sample(h->avctx, "Weighted prediction");
-            return AVERROR_PATCHWELCOME;
+        (!h->pic_structure && h->mbidx >= h->mb_width * h->mb_height / 2)) {
+        h->slice_weight_pred_flag = get_bits1(gb);
+        if (h->slice_weight_pred_flag) {
+            /* a second I field codes one weight set, a field picture
+             * codes four (one per reference field), a frame two */
+            int nref = h->cur.f->pict_type == AV_PICTURE_TYPE_I ? 1
+                     : !h->pic_structure ? 4 : 2;
+            for (int i = 0; i < nref; i++) {
+                h->luma_scale[i]   = get_bits(gb, 8);
+                h->luma_shift[i]   = get_sbits(gb, 8);
+                skip_bits1(gb);
+                h->chroma_scale[i] = get_bits(gb, 8);
+                h->chroma_shift[i] = get_sbits(gb, 8);
+                skip_bits1(gb);
+            }
+            h->mb_weight_pred_flag = get_bits1(gb);
         }
+    }
     return 0;
 }
 
@@ -2018,6 +2062,17 @@ static int decode_pic(AVSContext *h)
         if (!h->edge_emu_buffer)
             return AVERROR(ENOMEM);
     }
+    if (!h->wp_y) {
+        int alloc_size = FFALIGN(FFABS(h->cur.f->linesize[0]) + 32, 32);
+        /* 16 luma rows + 2 x 16 chroma rows (4:2:2) for weighted
+         * bi-prediction; rows may be up to twice the frame stride for
+         * field pictures */
+        h->wp_y = av_mallocz(alloc_size * 2 * 48);
+        if (!h->wp_y)
+            return AVERROR(ENOMEM);
+        h->wp_u = h->wp_y + alloc_size * 2 * 16;
+        h->wp_v = h->wp_u + alloc_size * 2 * 16;
+    }
 
     /* picture_distance; DistanceIndex is twice it (clause 9.4.6.1) */
     h->cur.poc = get_bits(&h->gb, 8) * 2;
-- 
2.34.1

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

Reply via email to