From 8f25859b09385db6f924db8caf5ab4eddd70160a Mon Sep 17 00:00:00 2001 From: Andreas Rheinhardt Date: Sat, 1 Mar 2025 00:04:43 +0100 Subject: [PATCH 32/40] avcodec/mpegvideo: Move temp ratecontrol bufs to RateControlContext Also only allocate them when they are needed (namely iff adaptive quant is true) and allocate them jointly. Signed-off-by: Andreas Rheinhardt --- libavcodec/mpegvideo.h | 3 --- libavcodec/mpegvideo_enc.c | 5 ----- libavcodec/ratecontrol.c | 19 +++++++++++++++---- libavcodec/ratecontrol.h | 2 ++ 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h index 5fef008cc5..43d4bbfbcb 100644 --- a/libavcodec/mpegvideo.h +++ b/libavcodec/mpegvideo.h @@ -511,9 +511,6 @@ typedef struct MpegEncContext { int lmin, lmax; int vbv_ignore_qmax; - /* temp buffers for rate control */ - float *cplx_tab, *bits_tab; - /* flag to indicate a reinitialization is required, e.g. after * a frame size change */ int context_reinit; diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index 656d312d28..4e4b4c1ba9 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -945,8 +945,6 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) mb_array_size = s->mb_stride * s->mb_height; if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_type, mb_array_size) || !FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size) || - !FF_ALLOC_TYPED_ARRAY (s->cplx_tab, mb_array_size) || - !FF_ALLOC_TYPED_ARRAY (s->bits_tab, mb_array_size) || !FF_ALLOCZ_TYPED_ARRAY(s->mc_mb_var, mb_array_size) || !FF_ALLOCZ_TYPED_ARRAY(s->mb_var, mb_array_size) || !(s->mb_mean = av_mallocz(mb_array_size))) @@ -1075,9 +1073,6 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx) av_freep(&s->mb_type); av_freep(&s->lambda_table); - av_freep(&s->cplx_tab); - av_freep(&s->bits_tab); - av_freep(&s->q_intra_matrix); av_freep(&s->q_intra_matrix16); av_freep(&s->input_picture); diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c index f383c433c4..86fdacd7b1 100644 --- a/libavcodec/ratecontrol.c +++ b/libavcodec/ratecontrol.c @@ -694,6 +694,15 @@ av_cold int ff_rate_control_init(MpegEncContext *s) } } + if (s->adaptive_quant) { + unsigned mb_array_size = s->mb_stride * s->mb_height; + + rcc->cplx_tab = av_malloc_array(2 * sizeof(rcc->cplx_tab), mb_array_size); + if (!rcc->cplx_tab) + return AVERROR(ENOMEM); + rcc->bits_tab = rcc->cplx_tab + mb_array_size; + } + return 0; } @@ -705,6 +714,7 @@ av_cold void ff_rate_control_uninit(RateControlContext *rcc) av_expr_free(rcc->rc_eq_eval); rcc->rc_eq_eval = NULL; av_freep(&rcc->entry); + av_freep(&rcc->cplx_tab); } int ff_vbv_update(MpegEncContext *s, int frame_size) @@ -766,7 +776,8 @@ static void update_predictor(Predictor *p, double q, double var, double size) p->coeff += new_coeff; } -static void adaptive_quantization(MpegEncContext *s, double q) +static void adaptive_quantization(RateControlContext *const rcc, + MpegEncContext *const s, double q) { int i; const float lumi_masking = s->avctx->lumi_masking / (128.0 * 128.0); @@ -777,8 +788,8 @@ static void adaptive_quantization(MpegEncContext *s, double q) const float border_masking = s->border_masking; float bits_sum = 0.0; float cplx_sum = 0.0; - float *cplx_tab = s->cplx_tab; - float *bits_tab = s->bits_tab; + float *cplx_tab = rcc->cplx_tab; + float *bits_tab = rcc->bits_tab; const int qmin = s->avctx->mb_lmin; const int qmax = s->avctx->mb_lmax; const int mb_width = s->mb_width; @@ -1048,7 +1059,7 @@ float ff_rate_estimate_qscale(MpegEncContext *s, int dry_run) q = qmax; if (s->adaptive_quant) - adaptive_quantization(s, q); + adaptive_quantization(rcc, s, q); else q = (int)(q + 0.5); diff --git a/libavcodec/ratecontrol.h b/libavcodec/ratecontrol.h index edeccbc6cd..b889491335 100644 --- a/libavcodec/ratecontrol.h +++ b/libavcodec/ratecontrol.h @@ -89,6 +89,8 @@ typedef struct RateControlContext{ char *rc_eq; struct AVExpr *rc_eq_eval; + + float *cplx_tab, *bits_tab; }RateControlContext; struct MpegEncContext; -- 2.45.2