From 8622dcb39b6554dfcb54c2c95c8dcca96b46d60b Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Wed, 5 Jul 2023 01:11:22 +0200 Subject: [PATCH] avfilter/adynamicequalizer_template: refactor and improve output --- libavfilter/adynamicequalizer_template.c | 46 ++++++++++-------------- libavfilter/af_adynamicequalizer.c | 12 +------ 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/libavfilter/adynamicequalizer_template.c b/libavfilter/adynamicequalizer_template.c index a6b35aa93e..4f7d58c939 100644 --- a/libavfilter/adynamicequalizer_template.c +++ b/libavfilter/adynamicequalizer_template.c @@ -167,13 +167,16 @@ static int fn(filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int n 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]; if (detection < 0) state[5] = threshold; + memcpy(fa, state + 8, sizeof(fa)); + memcpy(fm, state + 11, sizeof(fm)); + for (int n = 0; n < out->nb_samples; n++) { ftype detect, gain, v, listen; - ftype fa[3], fm[3]; ftype k, g; detect = listen = fn(get_svf)(src[n], dm, da, state); @@ -182,46 +185,32 @@ static int fn(filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int n if (detection > 0) state[5] = FMAX(state[5], detect); - if (direction == 0) { - if (detect < threshold) { - if (mode == 0) - detect = ONE / CLIP(ONE + makeup + (threshold - detect) * ratio, ONE, range); - else - detect = CLIP(ONE + makeup + (threshold - detect) * ratio, ONE, range); + if (mode >= 0) { + if (direction == 0 && detect < threshold) { + detect = CLIP(ONE + makeup + (threshold - detect) * ratio, ONE, range); + if (!mode) + detect = ONE / detect; + } else if (direction == 1 && detect > threshold) { + detect = CLIP(ONE + makeup + (detect - threshold) * ratio, ONE, range); + if (!mode) + detect = ONE / detect; } else { detect = ONE; } - } else { - if (detect > threshold) { - if (mode == 0) - detect = ONE / CLIP(ONE + makeup + (detect - threshold) * ratio, ONE, range); - else - detect = CLIP(ONE + makeup + (detect - threshold) * ratio, ONE, range); - } else { - detect = ONE; - } - } - if (direction == 0) { - if (detect > state[4]) { - detect = iattack * detect + attack * state[4]; - } else { - detect = irelease * detect + release * state[4]; - } - } else { - if (detect < state[4]) { + if ((direction == 0 && detect > state[4]) || (direction == 1 && detect < state[4])) { detect = iattack * detect + attack * state[4]; } else { detect = irelease * detect + release * state[4]; } } - if (state[4] != detect || n == 0) { + if (state[4] != detect) { state[4] = gain = detect; switch (tftype) { case 0: - k = ONE / (tqfactor * gain); + k = itqfactor / gain; fa[0] = ONE / (ONE + fg * (fg + k)); fa[1] = fg * fa[0]; @@ -262,6 +251,9 @@ static int fn(filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int n 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)); } return 0; diff --git a/libavfilter/af_adynamicequalizer.c b/libavfilter/af_adynamicequalizer.c index a3aeee91c5..5a2c8efb2a 100644 --- a/libavfilter/af_adynamicequalizer.c +++ b/libavfilter/af_adynamicequalizer.c @@ -96,26 +96,16 @@ static int config_input(AVFilterLink *inlink) AudioDynamicEqualizerContext *s = ctx->priv; s->format = inlink->format; - s->state = ff_get_audio_buffer(inlink, 8); + s->state = ff_get_audio_buffer(inlink, 16); if (!s->state) return AVERROR(ENOMEM); switch (s->format) { case AV_SAMPLE_FMT_DBLP: - for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { - double *state = (double *)s->state->extended_data[ch]; - - state[4] = 1.; - } s->filter_prepare = filter_prepare_double; s->filter_channels = filter_channels_double; break; case AV_SAMPLE_FMT_FLTP: - for (int ch = 0; ch < inlink->ch_layout.nb_channels; ch++) { - float *state = (float *)s->state->extended_data[ch]; - - state[4] = 1.; - } s->filter_prepare = filter_prepare_float; s->filter_channels = filter_channels_float; break;