1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-21 10:55:51 +02:00

avfilter/af_adynamicequalizer: add new structure to hold filtering state

This commit is contained in:
Paul B Mahol 2023-11-04 14:34:18 +01:00
parent 3f890fbfd9
commit 43226efc21
2 changed files with 34 additions and 22 deletions

View File

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

View File

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