From 2a74826b027cb031acdf1f85519ba61cca932509 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Sun, 14 May 2023 22:33:37 +0200 Subject: [PATCH] avfilter/*xfade: reduce memory consumption There is no always need for new buffers. --- libavfilter/af_afade.c | 13 +++++++++++++ libavfilter/vf_xfade.c | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/libavfilter/af_afade.c b/libavfilter/af_afade.c index 48938384ff..2f45b12904 100644 --- a/libavfilter/af_afade.c +++ b/libavfilter/af_afade.c @@ -672,14 +672,27 @@ static int acrossfade_config_output(AVFilterLink *outlink) return 0; } +static AVFrame *get_audio_buffer(AVFilterLink *inlink, int nb_samples) +{ + AVFilterContext *ctx = inlink->dst; + AudioFadeContext *s = ctx->priv; + + return (s->crossfade_is_over || + (ff_inlink_queued_samples(inlink) > s->nb_samples)) ? + ff_null_get_audio_buffer (inlink, nb_samples) : + ff_default_get_audio_buffer(inlink, nb_samples); +} + static const AVFilterPad avfilter_af_acrossfade_inputs[] = { { .name = "crossfade0", .type = AVMEDIA_TYPE_AUDIO, + .get_buffer.audio = get_audio_buffer, }, { .name = "crossfade1", .type = AVMEDIA_TYPE_AUDIO, + .get_buffer.audio = get_audio_buffer, }, }; diff --git a/libavfilter/vf_xfade.c b/libavfilter/vf_xfade.c index 9f66927365..5745a24173 100644 --- a/libavfilter/vf_xfade.c +++ b/libavfilter/vf_xfade.c @@ -2042,14 +2042,25 @@ static int xfade_activate(AVFilterContext *ctx) return FFERROR_NOT_READY; } +static AVFrame *get_video_buffer(AVFilterLink *inlink, int w, int h) +{ + XFadeContext *s = inlink->dst->priv; + + return s->xfade_is_over || !s->need_second ? + ff_null_get_video_buffer (inlink, w, h) : + ff_default_get_video_buffer(inlink, w, h); +} + static const AVFilterPad xfade_inputs[] = { { .name = "main", .type = AVMEDIA_TYPE_VIDEO, + .get_buffer.video = get_video_buffer, }, { .name = "xfade", .type = AVMEDIA_TYPE_VIDEO, + .get_buffer.video = get_video_buffer, }, };