From 43226efc2188ec2cd16d3b9c4e189bcde1a2a0e5 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Sat, 4 Nov 2023 14:34:18 +0100 Subject: [PATCH] avfilter/af_adynamicequalizer: add new structure to hold filtering state --- libavfilter/adynamicequalizer_template.c | 35 ++++++++++++------------ libavfilter/af_adynamicequalizer.c | 21 +++++++++++--- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/libavfilter/adynamicequalizer_template.c b/libavfilter/adynamicequalizer_template.c index 26fcebf8bc..5667b50b1d 100644 --- a/libavfilter/adynamicequalizer_template.c +++ b/libavfilter/adynamicequalizer_template.c @@ -166,25 +166,25 @@ static int fn(filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int n for (int ch = start; ch < end; ch++) { const ftype *src = (const ftype *)in->extended_data[ch]; ftype *dst = (ftype *)out->extended_data[ch]; - ftype *state = (ftype *)s->state->extended_data[ch]; - const ftype threshold = detection == 0 ? state[5] : s->threshold; - ftype fa[3], fm[3]; + ChannelContext *cc = &s->cc[ch]; + const ftype threshold = detection == 0 ? fn(cc->threshold) : s->threshold; + ftype *fa = fn(cc->fa), *fm = fn(cc->fm); + ftype *fstate = fn(cc->fstate); + ftype *dstate = fn(cc->dstate); + ftype gain = fn(cc->gain); if (detection < 0) - state[5] = threshold; - - memcpy(fa, state + 8, sizeof(fa)); - memcpy(fm, state + 11, sizeof(fm)); + fn(cc->threshold) = threshold; for (int n = 0; n < out->nb_samples; n++) { - ftype detect, gain, v, listen; + ftype detect, v, listen; ftype k, g; - detect = listen = fn(get_svf)(src[n], dm, da, state); + detect = listen = fn(get_svf)(src[n], dm, da, dstate); detect = FABS(detect); if (detection > 0) - state[5] = FMAX(state[5], detect); + fn(cc->threshold) = FMAX(fn(cc->threshold), detect); if (mode >= 0) { if (direction == 0 && detect < threshold) { @@ -200,17 +200,17 @@ static int fn(filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int n } { - ftype delta = detect - state[4]; + ftype delta = detect - gain; if (delta > EPSILON) - detect = state[4] + attack * delta; + detect = gain + attack * delta; else if (delta < -EPSILON) - detect = state[4] + release * delta; + detect = gain + release * delta; } } - if (state[4] != detect) { - state[4] = gain = detect; + if (gain != detect) { + gain = detect; switch (tftype) { case 0: @@ -251,13 +251,12 @@ static int fn(filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int n } } - v = fn(get_svf)(src[n], fm, fa, &state[2]); + v = fn(get_svf)(src[n], fm, fa, fstate); v = mode == -1 ? listen : v; dst[n] = ctx->is_disabled ? src[n] : v; } - memcpy(state + 8, fa, sizeof(fa)); - memcpy(state + 11, fm, sizeof(fm)); + fn(cc->gain) = gain; } return 0; diff --git a/libavfilter/af_adynamicequalizer.c b/libavfilter/af_adynamicequalizer.c index 2c674cbd7c..a84e8e5f57 100644 --- a/libavfilter/af_adynamicequalizer.c +++ b/libavfilter/af_adynamicequalizer.c @@ -23,6 +23,19 @@ #include "audio.h" #include "formats.h" +typedef struct ChannelContext { + double fa_double[3], fm_double[3]; + double dstate_double[2]; + double fstate_double[2]; + double gain_double; + double threshold_double; + float fa_float[3], fm_float[3]; + float dstate_float[2]; + float fstate_float[2]; + float gain_float; + float threshold_float; +} ChannelContext; + typedef struct AudioDynamicEqualizerContext { const AVClass *class; @@ -52,7 +65,7 @@ typedef struct AudioDynamicEqualizerContext { double da_double[3], dm_double[3]; float da_float[3], dm_float[3]; - AVFrame *state; + ChannelContext *cc; } AudioDynamicEqualizerContext; static int query_formats(AVFilterContext *ctx) @@ -96,8 +109,8 @@ static int config_input(AVFilterLink *inlink) AudioDynamicEqualizerContext *s = ctx->priv; s->format = inlink->format; - s->state = ff_get_audio_buffer(inlink, 16); - if (!s->state) + s->cc = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->cc)); + if (!s->cc) return AVERROR(ENOMEM); switch (s->format) { @@ -148,7 +161,7 @@ static av_cold void uninit(AVFilterContext *ctx) { AudioDynamicEqualizerContext *s = ctx->priv; - av_frame_free(&s->state); + av_freep(&s->cc); } #define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x)