1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-08-04 22:03:09 +02:00

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 <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt
2025-03-01 00:04:43 +01:00
parent 6844e96116
commit 89a8033fc9
4 changed files with 17 additions and 12 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -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(mb_array_size, 2 * sizeof(rcc->cplx_tab));
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);

View File

@@ -89,6 +89,8 @@ typedef struct RateControlContext{
char *rc_eq;
struct AVExpr *rc_eq_eval;
float *cplx_tab, *bits_tab;
}RateControlContext;
struct MpegEncContext;