From 0138d80319982b29201c8e31dfd3db76a5680d6c Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Wed, 30 Apr 2025 02:47:42 +0200 Subject: [PATCH 32/44] avcodec/mpegvideo: Defer init of slice contexts in ff_mpv_common_init This will allow the callers to do something between ff_mpv_common_init() and ff_mpv_init_duplicate_contexts(). Signed-off-by: Andreas Rheinhardt --- libavcodec/mpegvideo.c | 11 +---------- libavcodec/mpegvideo_dec.c | 11 ++++++++++- libavcodec/mpegvideo_enc.c | 9 ++++++--- libavcodec/svq1enc.c | 4 ++++ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index 9c96e25e03..b51d9ef00d 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -403,7 +403,7 @@ av_cold int ff_mpv_common_init(MpegEncContext *s) ret = mpv_init_context_frame(s); if (ret < 0) - goto fail; + return ret; if (nb_slices > FFMIN(MAX_THREADS, s->mb_height)) { av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d)," @@ -416,16 +416,7 @@ av_cold int ff_mpv_common_init(MpegEncContext *s) s->thread_context[0] = s; s->slice_context_count = nb_slices; -// if (s->width && s->height) { - ret = ff_mpv_init_duplicate_contexts(s); - if (ret < 0) - goto fail; -// } - return 0; - fail: - ff_mpv_common_end(s); - return ret; } av_cold void ff_mpv_common_end(MpegEncContext *s) diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c index fe5d5bcadd..0d545e5b86 100644 --- a/libavcodec/mpegvideo_dec.c +++ b/libavcodec/mpegvideo_dec.c @@ -146,7 +146,16 @@ av_cold int ff_mpv_decode_reinit(MpegEncContext *const s) ret = ff_set_dimensions(s->avctx, s->width, s->height); if (ret < 0) return ret; - return ff_mpv_common_init(s); + ret = ff_mpv_common_init(s); + if (ret < 0) + goto fail; + ret = ff_mpv_init_duplicate_contexts(s); + if (ret < 0) + goto fail; + return 0; +fail: + ff_mpv_common_end(s); + return ret; } av_cold int ff_mpv_decode_close(AVCodecContext *avctx) diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index ce9315c1b8..031647fa53 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -1021,9 +1021,9 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) m->lmin = m->lmax; } - /* ff_mpv_common_init() will copy (memdup) the contents of the main slice - * to the slice contexts, so we initialize various fields of it - * before calling ff_mpv_common_init(). */ + /* ff_mpv_init_duplicate_contexts() will copy (memdup) the contents of the + * main slice to the slice contexts, so we initialize various fields of it + * before calling ff_mpv_init_duplicate_contexts(). */ ff_mpv_idct_init(&s->c); init_unquantize(&s->c, avctx); ff_fdctdsp_init(&s->fdsp, avctx); @@ -1054,6 +1054,9 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) s->c.slice_ctx_size = sizeof(*s); ret = ff_mpv_common_init(&s->c); + if (ret < 0) + return ret; + ret = ff_mpv_init_duplicate_contexts(&s->c); if (ret < 0) return ret; diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c index c1662da5ec..ad6cae6237 100644 --- a/libavcodec/svq1enc.c +++ b/libavcodec/svq1enc.c @@ -608,6 +608,10 @@ static av_cold int svq1_encode_init(AVCodecContext *avctx) s->m.c.avctx = avctx; ret = ff_mpv_common_init(&s->m.c); + if (ret < 0) + return ret; + // Not really needed + ret = ff_mpv_init_duplicate_contexts(&s->m.c); if (ret < 0) return ret; -- 2.45.2