PR #26362 opened by Rainbaby URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26362 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26362.patch
Some AVS+ broadcasts set weighting_quant_flag on every picture, and the decoder refuses those pictures. This adds the weighted quantization of GY/T 257.1-2012 with its Cb and Cr QP deltas. The output matches a closed source decoder on the broadcast captures I have. Streams without weighting decode at the same speed. #26315 has an AWQ commit as well. This series was ready before it came in. The last commit takes index 3 and model 3 from it. GY/T 257.1 reserves both, but GDM decodes them according to its author, and the model 3 layout is in US 9762917. With that the two give the same output. ```fate-samples cavs/avsplus-awq.ts ``` >From 392bc3125270655e5b9f1fc4037f4fb6a4c6d3b9 Mon Sep 17 00:00:00 2001 From: Rainbaby <[email protected]> Date: Mon, 9 Nov 2026 16:22:03 +0900 Subject: [PATCH 1/4] avcodec/cavs: look up the chroma QP per plane AVS+ (GY/T 257.1-2012) can adjust the Cb and Cr quantization parameters separately by chroma_quant_param_delta_cb and chroma_quant_param_delta_cr in the picture header (9.6.1), and the loop filter then uses each plane's own QP for chroma edges (9.11). Keep both deltas in the context and look the chroma QP up per plane. The deltas are always zero for now, so the output does not change. Signed-off-by: Rainbaby <[email protected]> --- libavcodec/cavs.c | 22 ++++++++++++++++++++-- libavcodec/cavs.h | 10 ++++++++++ libavcodec/cavsdec.c | 7 ++++--- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/libavcodec/cavs.c b/libavcodec/cavs.c index 72a3ce70edb..457899f0f38 100644 --- a/libavcodec/cavs.c +++ b/libavcodec/cavs.c @@ -105,6 +105,16 @@ static inline int get_bs(cavs_vector *mvP, cavs_vector *mvQ, int b) beta = beta_tab[av_clip_uintp2(qp_avg + h->beta_offset, 6)]; \ tc = tc_tab[av_clip_uintp2(qp_avg + h->alpha_offset, 6)]; +/** + * Average of the chroma QPs of the current block and a neighbour for the + * chroma edge between them (9.11). + */ +static inline int chroma_qp_avg(const AVSContext *h, int qp_nb, int plane) +{ + return (ff_cavs_chroma_qp_of(h, h->qp, plane) + + ff_cavs_chroma_qp_of(h, qp_nb, plane) + 1) >> 1; +} + /** * in-loop deblocking filter for a single macroblock * @@ -159,9 +169,13 @@ void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type) qp_avg = (h->qp + h->left_qp + 1) >> 1; SET_PARAMS; h->cdsp.cavs_filter_lv(h->cy, h->l_stride, alpha, beta, tc, bs[0], bs[1]); - qp_avg = (ff_cavs_chroma_qp[h->qp] + ff_cavs_chroma_qp[h->left_qp] + 1) >> 1; + qp_avg = chroma_qp_avg(h, h->left_qp, 0); SET_PARAMS; h->cdsp.cavs_filter_cv(h->cu, h->c_stride, alpha, beta, tc, bs[0], bs[1]); + if (h->chroma_qp_delta[0] != h->chroma_qp_delta[1]) { + qp_avg = chroma_qp_avg(h, h->left_qp, 1); + SET_PARAMS; + } h->cdsp.cavs_filter_cv(h->cv, h->c_stride, alpha, beta, tc, bs[0], bs[1]); } qp_avg = h->qp; @@ -173,9 +187,13 @@ void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type) qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1; SET_PARAMS; h->cdsp.cavs_filter_lh(h->cy, h->l_stride, alpha, beta, tc, bs[4], bs[5]); - qp_avg = (ff_cavs_chroma_qp[h->qp] + ff_cavs_chroma_qp[h->top_qp[h->mbx]] + 1) >> 1; + qp_avg = chroma_qp_avg(h, h->top_qp[h->mbx], 0); SET_PARAMS; h->cdsp.cavs_filter_ch(h->cu, h->c_stride, alpha, beta, tc, bs[4], bs[5]); + if (h->chroma_qp_delta[0] != h->chroma_qp_delta[1]) { + qp_avg = chroma_qp_avg(h, h->top_qp[h->mbx], 1); + SET_PARAMS; + } h->cdsp.cavs_filter_ch(h->cv, h->c_stride, alpha, beta, tc, bs[4], bs[5]); } } diff --git a/libavcodec/cavs.h b/libavcodec/cavs.h index acc33b07934..80f17397dd7 100644 --- a/libavcodec/cavs.h +++ b/libavcodec/cavs.h @@ -220,6 +220,7 @@ typedef struct AVSContext { uint8_t *top_c_pred_mode; ///< IntraChromaPredMode above, per column int loop_filter_disable; int alpha_offset, beta_offset; + int chroma_qp_delta[2]; ///< chroma_quant_param_delta of Cb and Cr int ref_flag; int mbx, mby, mbidx; ///< macroblock coordinates int flags; ///< availability flags of neighbouring macroblocks @@ -308,6 +309,15 @@ typedef struct AVSContext { extern const uint8_t ff_cavs_chroma_qp[64]; 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). + */ +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]]; +} + /** mark block as using intra prediction */ #define CAVS_DIR_MV (cavs_vector){ 0, 0, 1, REF_DIR, 0 } /** mark block as "no prediction from this direction" diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c index ab168ba8ca5..9ff2e50de17 100644 --- a/libavcodec/cavsdec.c +++ b/libavcodec/cavsdec.c @@ -1147,16 +1147,17 @@ static int decode_residual(AVSContext *h, const struct dec_2dvlc *r, static inline int decode_residual_chroma(AVSContext *h) { - const int qp = ff_cavs_chroma_qp[h->qp]; + const int qp_cb = ff_cavs_chroma_qp_of(h, h->qp, 0); + const int qp_cr = ff_cavs_chroma_qp_of(h, h->qp, 1); int ret; if (h->cbp & (1 << 4)) { - ret = decode_residual(h, chroma_dec, 0, 1, qp, h->cu, h->c_stride); + ret = decode_residual(h, chroma_dec, 0, 1, qp_cb, h->cu, h->c_stride); if (ret < 0) return ret; } if (h->cbp & (1 << 5)) { - ret = decode_residual(h, chroma_dec, 0, 1, qp, h->cv, h->c_stride); + ret = decode_residual(h, chroma_dec, 0, 1, qp_cr, h->cv, h->c_stride); if (ret < 0) return ret; } -- 2.54.1 >From a7abbc2161cc9ae9f9959505d3632582e23e6609 Mon Sep 17 00:00:00 2001 From: Rainbaby <[email protected]> Date: Mon, 9 Nov 2026 17:40:44 +0900 Subject: [PATCH 2/4] 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 <[email protected]> --- libavcodec/cavs.h | 8 ++- libavcodec/cavsdec.c | 137 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 137 insertions(+), 8 deletions(-) diff --git a/libavcodec/cavs.h b/libavcodec/cavs.h index 80f17397dd7..44eea49161f 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(qp + h->chroma_qp_delta[plane], 0, 63)]; } /** mark block as using intra prediction */ diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c index 9ff2e50de17..91685efa680 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); } -- 2.54.1 >From f68a94b0298d42769d1505d41ac4dba3727234bd Mon Sep 17 00:00:00 2001 From: Rainbaby <[email protected]> Date: Mon, 9 Nov 2026 20:27:27 +0900 Subject: [PATCH 3/4] tests/fate/video: add an AVS+ test for weighted quantization The sample is 8 frames of a 1080i broadcast that sets weighting_quant_flag on every picture. It ends at a scene cut where CurrentQP plus the chroma delta reaches 64, so it covers the clipped chroma QP as well. The output matches a closed source decoder. Signed-off-by: Rainbaby <[email protected]> --- tests/fate/video.mak | 3 +++ tests/ref/fate/cavs-avsplus-awq | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/ref/fate/cavs-avsplus-awq diff --git a/tests/fate/video.mak b/tests/fate/video.mak index e663b807a84..abfcdf04eb3 100644 --- a/tests/fate/video.mak +++ b/tests/fate/video.mak @@ -82,6 +82,9 @@ fate-cavs-avsplus: CMD = framecrc -i $(TARGET_SAMPLES)/cavs/avsplus-1080i-testca FATE_VIDEO-$(call FRAMECRC, MPEGTS, CAVS, CAVSVIDEO_PARSER) += fate-cavs-avsplus-user-data fate-cavs-avsplus-user-data: CMD = framecrc -i $(TARGET_SAMPLES)/cavs/avsplus-user-data.ts -an +FATE_VIDEO-$(call FRAMECRC, MPEGTS, CAVS, CAVSVIDEO_PARSER) += fate-cavs-avsplus-awq +fate-cavs-avsplus-awq: CMD = framecrc -i $(TARGET_SAMPLES)/cavs/avsplus-awq.ts -an + FATE_VIDEO-$(call FRAMECRC, CDG, CDGRAPHICS, SCALE_FILTER) += fate-cdgraphics fate-cdgraphics: CMD = framecrc -i $(TARGET_SAMPLES)/cdgraphics/BrotherJohn.cdg -pix_fmt rgba -t 1 -vf scale diff --git a/tests/ref/fate/cavs-avsplus-awq b/tests/ref/fate/cavs-avsplus-awq new file mode 100644 index 00000000000..696dcae916c --- /dev/null +++ b/tests/ref/fate/cavs-avsplus-awq @@ -0,0 +1,13 @@ +#tb 0: 1/25 +#media_type 0: video +#codec_id 0: rawvideo +#dimensions 0: 1920x1080 +#sar 0: 0/1 +0, 0, 0, 1, 3110400, 0xd6b7dee5 +0, 1, 1, 1, 3110400, 0xf6f55fd8 +0, 2, 2, 1, 3110400, 0x7875c6a3 +0, 3, 3, 1, 3110400, 0xc81dcc8c +0, 4, 4, 1, 3110400, 0xecdd6c4b +0, 5, 5, 1, 3110400, 0x42423a4e +0, 6, 6, 1, 3110400, 0x82b621b9 +0, 7, 7, 1, 3110400, 0x7d68dae2 -- 2.54.1 >From 84fa24db09dea951d9ec56d6ffad7635d79364f8 Mon Sep 17 00:00:00 2001 From: Rainbaby <[email protected]> Date: Thu, 26 Nov 2026 08:31:50 +0900 Subject: [PATCH 4/4] avcodec/cavsdec: decode weighted quantization index 3 and model 3 GY/T 257.1 reserves both. They are decoded as in PR #26315 by jianfeng.zheng, which follows the GDM reference decoder. Signed-off-by: Rainbaby <[email protected]> --- libavcodec/cavsdec.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c index 91685efa680..fb0884e19b5 100644 --- a/libavcodec/cavsdec.c +++ b/libavcodec/cavsdec.c @@ -1900,18 +1900,21 @@ static int set_ref_list(AVSContext *h) static int decode_weighting_quant(AVSContext *h) { /* WeightingQuantParamDefault, WeightingQuantParamBase1 and - * WeightingQuantParamBase2 (9.2): the bases of wqP[0], wqP[1] and wqP[2] + * WeightingQuantParamBase2 (9.2): the bases of wqP[0], wqP[1] and wqP[2]. + * The fourth set is for the reserved index 3, see below. */ - static const uint8_t wq_param[3][6] = { + static const uint8_t wq_param[4][6] = { { 128, 98, 106, 116, 116, 128 }, { 135, 143, 143, 160, 160, 213 }, { 128, 98, 106, 116, 116, 128 }, + { 128, 128, 128, 128, 128, 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. + * The fourth layout is for the reserved model 3, see below. */ - static const uint8_t wq_model[3][64] = { + static const uint8_t wq_model[4][64] = { { 0, 0, 0, 4, 4, 4, 5, 5, 0, 0, 3, 3, 3, 3, 5, 5, @@ -1939,6 +1942,15 @@ static int decode_weighting_quant(AVSContext *h) 3, 2, 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, 3, 2, 1, 5, 5, + 0, 0, 4, 3, 2, 1, 5, 5, + 0, 4, 4, 3, 2, 1, 5, 5, + 3, 3, 3, 3, 2, 5, 5, 5, + 2, 2, 2, 2, 5, 5, 5, 5, + 1, 1, 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; @@ -1960,15 +1972,13 @@ static int decode_weighting_quant(AVSContext *h) } 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; - } + /* GY/T 257.1 reserves index 3 and model 3 (7.2.3.1). They are decoded + * as the GDM reference decoder is reported to: index 3 is a flat set + * without deltas, model 3 a fourth layout. + */ /* wqP[index]; only that set is used (9.4.9) */ memcpy(param, wq_param[index], sizeof(param)); - if (index) { + if (index == 1 || index == 2) { for (int i = 0; i < 6; i++) { int delta = get_se_golomb(gb); if (delta < -param[i] || delta > 255 - param[i]) { -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
