Commit 6e894a0390 for ffmpeg
commit 6e894a03904021aaa2d527805a4dd10fbbbc5534
Author: Rainbaby <r@bgr.jp>
Date: Mon Nov 9 17:40:44 2026 +0900
avcodec/cavsdec: support adaptive weighted quantization
Some AVS+ broadcasts set weighting_quant_flag on every picture, which
the decoder refused, so nothing of such a stream decoded. Read the
weighted quantization parameters that end the picture header
(GY/T 257.1-2012 tables 21 and 22), build WeightingQuantMatrix8x8 from
the chosen parameter set and model (9.2), scale the coefficients by it
(9.6.2), and apply the chroma QP deltas.
These streams can push CurrentQP plus the chroma delta past 63, which
the standard does not allow. The table index is clipped, which matches
the output of a closed source decoder on these streams.
Signed-off-by: Rainbaby <r@bgr.jp>
diff --git a/libavcodec/cavs.h b/libavcodec/cavs.h
index 80f17397dd..7b7acf35d2 100644
--- a/libavcodec/cavs.h
+++ b/libavcodec/cavs.h
@@ -27,6 +27,7 @@
#include <string.h>
#include "libavutil/attributes.h"
+#include "libavutil/common.h"
#include "libavutil/frame.h"
#include "libavutil/mem_internal.h"
@@ -220,7 +221,9 @@ typedef struct AVSContext {
uint8_t *top_c_pred_mode; ///< IntraChromaPredMode above, per column
int loop_filter_disable;
int alpha_offset, beta_offset;
+ int weighting_quant; ///< weighting_quant_flag of the picture
int chroma_qp_delta[2]; ///< chroma_quant_param_delta of Cb and Cr
+ uint8_t wq_matrix[64]; ///< WeightingQuantMatrix8x8 (9.4.9), IDCT order
int ref_flag;
int mbx, mby, mbidx; ///< macroblock coordinates
int flags; ///< availability flags of neighbouring macroblocks
@@ -311,11 +314,12 @@ extern const uint8_t ff_cavs_partition_flags[30];
/**
* Chroma quantization parameter (9.6.1): table 61 indexed by CurrentQP plus
- * the picture's chroma delta for @p plane (0 Cb, 1 Cr).
+ * the picture's chroma delta for @p plane (0 Cb, 1 Cr). The standard keeps
+ * the index in 0..63, but some streams exceed it, so clip.
*/
static inline int ff_cavs_chroma_qp_of(const AVSContext *h, int qp, int plane)
{
- return ff_cavs_chroma_qp[qp + h->chroma_qp_delta[plane]];
+ return ff_cavs_chroma_qp[av_clip_uintp2(qp + h->chroma_qp_delta[plane], 6)];
}
/** mark block as using intra prediction */
diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index 9ff2e50de1..91685efa68 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -34,6 +34,7 @@
#include "avcodec.h"
#include "get_bits.h"
#include "golomb.h"
+#include "idctdsp.h"
#include "cavs.h"
#include "codec_internal.h"
#include "profiles.h"
@@ -584,8 +585,15 @@ static inline int get_ue_code(GetBitContext *gb, int order)
return ret;
}
-static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
- int16_t *dst, int mul, int shift, int coeff_num)
+/**
+ * Inverse scan and dequantize the level/run pairs of one block into dst.
+ * @param weighted scale by WeightingQuantMatrix8x8 as well; a constant in
+ * both callers, so each gets its own loop
+ */
+static av_always_inline int dequant_internal(AVSContext *h, int16_t *level_buf,
+ uint8_t *run_buf, int16_t *dst,
+ int mul, int shift, int coeff_num,
+ int weighted)
{
int round = 1 << (shift - 1);
int pos = -1;
@@ -600,11 +608,29 @@ static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
h->cur.poc, h->mbx, h->mby);
return AVERROR_INVALIDDATA;
}
- dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
+ if (weighted) {
+ /* scale by WeightingQuantMatrix8x8 first (9.6.2); c * mul
+ * exceeds 32 bits even for conformant coefficients
+ */
+ int64_t c = level_buf[coeff_num] * h->wq_matrix[scantab[pos]] >> 3;
+ dst[scantab[pos]] = ((c * mul >> 4) + round) >> shift;
+ } else {
+ dst[scantab[pos]] = (level_buf[coeff_num] * mul + round) >> shift;
+ }
}
return 0;
}
+static inline int dequant(AVSContext *h, int16_t *level_buf, uint8_t *run_buf,
+ int16_t *dst, int mul, int shift, int coeff_num)
+{
+ if (h->weighting_quant)
+ return dequant_internal(h, level_buf, run_buf, dst, mul, shift,
+ coeff_num, 1);
+ return dequant_internal(h, level_buf, run_buf, dst, mul, shift,
+ coeff_num, 0);
+}
+
/**
* decode coefficients from one 8x8 block, dequantize, inverse transform
* and add them to sample block
@@ -1867,6 +1893,101 @@ static int set_ref_list(AVSContext *h)
return 0;
}
+/**
+ * Read the weighted quantization parameters of a picture header (tables 21
+ * and 22) and build WeightingQuantMatrix8x8 (9.2, 9.4.9).
+ */
+static int decode_weighting_quant(AVSContext *h)
+{
+ /* WeightingQuantParamDefault, WeightingQuantParamBase1 and
+ * WeightingQuantParamBase2 (9.2): the bases of wqP[0], wqP[1] and wqP[2]
+ */
+ static const uint8_t wq_param[3][6] = {
+ { 128, 98, 106, 116, 116, 128 },
+ { 135, 143, 143, 160, 160, 213 },
+ { 128, 98, 106, 116, 116, 128 },
+ };
+ /* wqM8x8 of the three models (9.2 c)), as indices into wqP. The figures
+ * of 9.5 print the column number i across and the row number j down,
+ * so these are read the same way: raster order of the block.
+ */
+ static const uint8_t wq_model[3][64] = {
+ {
+ 0, 0, 0, 4, 4, 4, 5, 5,
+ 0, 0, 3, 3, 3, 3, 5, 5,
+ 0, 3, 2, 2, 1, 1, 5, 5,
+ 4, 3, 2, 2, 1, 5, 5, 5,
+ 4, 3, 1, 1, 5, 5, 5, 5,
+ 4, 3, 1, 5, 5, 5, 5, 5,
+ 5, 5, 5, 5, 5, 5, 5, 5,
+ 5, 5, 5, 5, 5, 5, 5, 5,
+ }, {
+ 0, 0, 0, 4, 4, 4, 5, 5,
+ 0, 0, 4, 4, 4, 4, 5, 5,
+ 0, 3, 2, 2, 2, 1, 5, 5,
+ 3, 3, 2, 2, 1, 5, 5, 5,
+ 3, 3, 2, 1, 5, 5, 5, 5,
+ 3, 3, 1, 5, 5, 5, 5, 5,
+ 5, 5, 5, 5, 5, 5, 5, 5,
+ 5, 5, 5, 5, 5, 5, 5, 5,
+ }, {
+ 0, 0, 0, 4, 4, 3, 5, 5,
+ 0, 0, 4, 4, 3, 2, 5, 5,
+ 0, 4, 4, 3, 2, 1, 5, 5,
+ 4, 4, 3, 2, 1, 5, 5, 5,
+ 4, 3, 2, 1, 5, 5, 5, 5,
+ 3, 2, 1, 5, 5, 5, 5, 5,
+ 5, 5, 5, 5, 5, 5, 5, 5,
+ 5, 5, 5, 5, 5, 5, 5, 5,
+ },
+ };
+ GetBitContext *gb = &h->gb;
+ uint8_t permutation[64];
+ uint8_t param[6];
+ int index, model;
+
+ skip_bits1(gb); //reserved_bits
+ if (!get_bits1(gb)) { //chroma_quant_param_disable
+ for (int i = 0; i < 2; i++) {
+ int delta = get_se_golomb(gb);
+ if (delta < -16 || delta > 16) {
+ av_log(h->avctx, AV_LOG_ERROR,
+ "chroma_quant_param_delta %d out of range\n", delta);
+ return AVERROR_INVALIDDATA;
+ }
+ h->chroma_qp_delta[i] = delta;
+ }
+ }
+ index = get_bits(gb, 2); //weighting_quant_param_index
+ model = get_bits(gb, 2); //weighting_quant_model
+ if (index == 3 || model == 3) {
+ av_log(h->avctx, AV_LOG_ERROR,
+ "weighting_quant_param_index %d or weighting_quant_model %d "
+ "is reserved\n", index, model);
+ return AVERROR_INVALIDDATA;
+ }
+ /* wqP[index]; only that set is used (9.4.9) */
+ memcpy(param, wq_param[index], sizeof(param));
+ if (index) {
+ for (int i = 0; i < 6; i++) {
+ int delta = get_se_golomb(gb);
+ if (delta < -param[i] || delta > 255 - param[i]) {
+ av_log(h->avctx, AV_LOG_ERROR,
+ "weighting_quant_param_delta %d out of range\n", delta);
+ return AVERROR_INVALIDDATA;
+ }
+ param[i] += delta;
+ }
+ }
+ if (get_bits_left(gb) < 0)
+ return AVERROR_INVALIDDATA;
+
+ ff_init_scantable_permutation(permutation, h->cdsp.idct_perm);
+ for (int i = 0; i < 64; i++)
+ h->wq_matrix[permutation[i]] = param[wq_model[model][i]];
+ return 0;
+}
+
static int decode_pic(AVSContext *h)
{
int ret;
@@ -1988,10 +2109,14 @@ static int decode_pic(AVSContext *h)
/* AWQ parameters and aec_enable end the header (tables 21, 22) */
h->aec_enable = 0;
+ h->weighting_quant = 0;
+ h->chroma_qp_delta[0] = h->chroma_qp_delta[1] = 0;
if (h->profile == AV_PROFILE_CAVS_BROADCASTING) {
- if (get_bits1(&h->gb)) { //weighting_quant_flag
- avpriv_request_sample(h->avctx, "Adaptive weighted quantization");
- return AVERROR_PATCHWELCOME;
+ h->weighting_quant = get_bits1(&h->gb);
+ if (h->weighting_quant) {
+ ret = decode_weighting_quant(h);
+ if (ret < 0)
+ return ret;
}
h->aec_enable = get_bits1(&h->gb);
}