From 71d5c05ea2e4560be99a5dbf9d36a65ea8443e2f Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Tue, 20 May 2025 23:58:34 +0200 Subject: [PATCH 14/19] avcodec/mpegvideo_dec: Simplify check for unquantizing inter blocks Just ensure that dct_unquantize_inter is set iff it is used and check for the function pointer instead. Signed-off-by: Andreas Rheinhardt --- libavcodec/h263dec.c | 3 ++- libavcodec/mpeg4videodec.c | 6 ++++-- libavcodec/mpeg4videodec.h | 2 ++ libavcodec/mpegvideo_dec.c | 8 +++----- libavcodec/msmpeg4dec.c | 3 +++ 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/libavcodec/h263dec.c b/libavcodec/h263dec.c index 501b8b44ff..c9515480f0 100644 --- a/libavcodec/h263dec.c +++ b/libavcodec/h263dec.c @@ -110,7 +110,8 @@ av_cold int ff_h263_decode_init(AVCodecContext *avctx) ff_mpv_unquantize_init(&unquant_dsp_ctx, avctx->flags & AV_CODEC_FLAG_BITEXACT, 0); // dct_unquantize defaults for H.263; - // they might change on a per-frame basis for MPEG-4. + // they might change on a per-frame basis for MPEG-4; + // dct_unquantize_inter will be unset for MSMPEG4 codecs later. s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_h263_intra; s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_h263_inter; diff --git a/libavcodec/mpeg4videodec.c b/libavcodec/mpeg4videodec.c index c4eed9136a..936711d72e 100644 --- a/libavcodec/mpeg4videodec.c +++ b/libavcodec/mpeg4videodec.c @@ -3417,6 +3417,8 @@ static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb, s->dct_unquantize_intra = s->mpeg_quant ? ctx->dct_unquantize_mpeg2_intra : ctx->dct_unquantize_h263_intra; + // The following tells ff_mpv_reconstruct_mb() to unquantize iff mpeg_quant + s->dct_unquantize_inter = s->mpeg_quant ? ctx->dct_unquantize_mpeg2_inter : NULL; end: /* detect buggy encoders which don't set the low_delay flag @@ -3961,8 +3963,8 @@ static av_cold int decode_init(AVCodecContext *avctx) ctx->dct_unquantize_h263_intra = unquant_dsp_ctx.dct_unquantize_h263_intra; ctx->dct_unquantize_mpeg2_intra = unquant_dsp_ctx.dct_unquantize_mpeg2_intra; // dct_unquantize_inter is only used with MPEG-2 quantizers, - // so we can already set dct_unquantize_inter here once and for all. - s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_mpeg2_inter; + // so that is all we keep. + ctx->dct_unquantize_mpeg2_inter = unquant_dsp_ctx.dct_unquantize_mpeg2_inter; s->y_dc_scale_table = ff_mpeg4_y_dc_scale_table; s->c_dc_scale_table = ff_mpeg4_c_dc_scale_table; diff --git a/libavcodec/mpeg4videodec.h b/libavcodec/mpeg4videodec.h index 57a2f81816..111d60ba10 100644 --- a/libavcodec/mpeg4videodec.h +++ b/libavcodec/mpeg4videodec.h @@ -91,6 +91,8 @@ typedef struct Mpeg4DecContext { Mpeg4VideoDSPContext mdsp; + void (*dct_unquantize_mpeg2_inter)(MpegEncContext *s, + int16_t *block, int n, int qscale); void (*dct_unquantize_mpeg2_intra)(MpegEncContext *s, int16_t *block, int n, int qscale); void (*dct_unquantize_h263_intra)(MpegEncContext *s, diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c index c85c7db722..f8551b93c8 100644 --- a/libavcodec/mpegvideo_dec.c +++ b/libavcodec/mpegvideo_dec.c @@ -960,11 +960,8 @@ void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64], } /* add dct residue */ - if (!(IS_MPEG12_H261(s) || s->msmpeg4_version != MSMP4_UNUSED || - (s->codec_id == AV_CODEC_ID_MPEG4 && !s->mpeg_quant))) { + if (is_mpeg12 != DEFINITELY_MPEG12_H261 && s->dct_unquantize_inter) { // H.263, H.263+, H.263I, FLV, RV10, RV20 and MPEG-4 with MPEG-2 quantization - // Also RV30, RV40 when performing error resilience, but - // all blocks are skipped in this case. add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale); add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale); add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale); @@ -978,7 +975,8 @@ void mpv_reconstruct_mb_internal(MpegEncContext *s, int16_t block[12][64], } else if (is_mpeg12 == DEFINITELY_MPEG12_H261 || lowres_flag || (s->codec_id != AV_CODEC_ID_WMV2)) { // H.261, MPEG-1, MPEG-2, MPEG-4 with H.263 quantization, // MSMP4V1-3 and WMV1. - // Also the VC-1 family when performing error resilience + // Also RV30, RV40 and the VC-1 family when performing error resilience, + // but all blocks are skipped in this case. add_dct(s, block[0], 0, dest_y , dct_linesize); add_dct(s, block[1], 1, dest_y + block_size, dct_linesize); add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize); diff --git a/libavcodec/msmpeg4dec.c b/libavcodec/msmpeg4dec.c index df67d43542..bfd2ef0b02 100644 --- a/libavcodec/msmpeg4dec.c +++ b/libavcodec/msmpeg4dec.c @@ -366,6 +366,9 @@ av_cold int ff_msmpeg4_decode_init(AVCodecContext *avctx) if (ff_h263_decode_init(avctx) < 0) return -1; + // We unquantize inter blocks as we parse them. + s->dct_unquantize_inter = NULL; + ff_msmpeg4_common_init(s); switch (s->msmpeg4_version) { -- 2.45.2