From ce3604d8244abf3278e2ba74876cb4ff2e9fe11d Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Thu, 27 Feb 2025 19:01:03 +0100 Subject: [PATCH 22/40] avcodec/mpegvideo_enc: Only allocate chroma intra matrices when needed Also start factoring the matrix-init code out into a function of its own to declutter ff_mpv_encode_init(). Signed-off-by: Andreas Rheinhardt --- libavcodec/mpegvideo_enc.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index 1fe69f89a6..79d6bfc056 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -348,6 +348,20 @@ static av_cold int me_cmp_init(MpegEncContext *s, AVCodecContext *avctx) return 0; } +static av_cold int init_matrices(MpegEncContext *s, AVCodecContext *avctx) +{ + if (s->out_format == FMT_MJPEG) { + if (!FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix, 32) || + !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix16, 32)) + return AVERROR(ENOMEM); + } else { + s->q_chroma_intra_matrix = s->q_intra_matrix; + s->q_chroma_intra_matrix16 = s->q_intra_matrix16; + } + + return 0; +} + /* init video encoder */ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) { @@ -866,10 +880,8 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) if (!(avctx->stats_out = av_mallocz(256)) || !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix, 32) || - !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix, 32) || !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix, 32) || !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix16, 32) || - !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix16, 32) || !FF_ALLOCZ_TYPED_ARRAY(s->q_inter_matrix16, 32) || !FF_ALLOCZ_TYPED_ARRAY(s->input_picture, MAX_B_FRAMES + 1) || !FF_ALLOCZ_TYPED_ARRAY(s->reordered_input_picture, MAX_B_FRAMES + 1) || @@ -877,6 +889,10 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) !(s->picture_pool = ff_mpv_alloc_pic_pool(0))) return AVERROR(ENOMEM); + ret = init_matrices(s, avctx); + if (ret < 0) + return ret; + /* Allocate MV tables; the MV and MB tables will be copied * to slice contexts by ff_update_duplicate_context(). */ mv_table_size = (s->mb_height + 2) * s->mb_stride + 1; @@ -3700,13 +3716,6 @@ static int encode_picture(MpegEncContext *s, const AVPacket *pkt) update_qscale(s); } - if (s->out_format != FMT_MJPEG) { - if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix); - if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16); - s->q_chroma_intra_matrix = s->q_intra_matrix; - s->q_chroma_intra_matrix16 = s->q_intra_matrix16; - } - ff_me_init_pic(s); s->mb_intra=0; //for the rate distortion & bit compare functions -- 2.45.2