2021-11-26 15:23:16 +02:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
|
2023-11-06 01:36:17 +02:00
|
|
|
#include "libavutil/ffmath.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2021-11-26 15:23:16 +02:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "audio.h"
|
2024-08-12 15:29:00 +02:00
|
|
|
#include "filters.h"
|
2021-11-26 15:23:16 +02:00
|
|
|
#include "formats.h"
|
|
|
|
|
2023-11-06 01:36:17 +02:00
|
|
|
enum DetectionModes {
|
|
|
|
DET_UNSET = 0,
|
|
|
|
DET_DISABLED,
|
|
|
|
DET_OFF,
|
|
|
|
DET_ON,
|
2023-11-08 15:31:50 +02:00
|
|
|
DET_ADAPTIVE,
|
2023-11-06 01:36:17 +02:00
|
|
|
NB_DMODES,
|
|
|
|
};
|
|
|
|
|
2023-11-04 16:23:59 +02:00
|
|
|
enum FilterModes {
|
|
|
|
LISTEN = -1,
|
|
|
|
CUT_BELOW,
|
|
|
|
CUT_ABOVE,
|
|
|
|
BOOST_BELOW,
|
|
|
|
BOOST_ABOVE,
|
2023-11-06 01:36:17 +02:00
|
|
|
NB_FMODES,
|
2023-11-04 16:23:59 +02:00
|
|
|
};
|
|
|
|
|
2023-11-04 15:34:18 +02:00
|
|
|
typedef struct ChannelContext {
|
|
|
|
double fa_double[3], fm_double[3];
|
|
|
|
double dstate_double[2];
|
|
|
|
double fstate_double[2];
|
2023-11-05 16:52:46 +02:00
|
|
|
double tstate_double[2];
|
2023-11-06 01:36:17 +02:00
|
|
|
double lin_gain_double;
|
|
|
|
double detect_double;
|
|
|
|
double threshold_log_double;
|
|
|
|
double new_threshold_log_double;
|
2023-11-08 15:31:50 +02:00
|
|
|
double log_sum_double;
|
|
|
|
double sum_double;
|
2023-11-04 15:34:18 +02:00
|
|
|
float fa_float[3], fm_float[3];
|
|
|
|
float dstate_float[2];
|
|
|
|
float fstate_float[2];
|
2023-11-05 16:52:46 +02:00
|
|
|
float tstate_float[2];
|
2023-11-06 01:36:17 +02:00
|
|
|
float lin_gain_float;
|
|
|
|
float detect_float;
|
|
|
|
float threshold_log_float;
|
|
|
|
float new_threshold_log_float;
|
2023-11-08 15:31:50 +02:00
|
|
|
float log_sum_float;
|
|
|
|
float sum_float;
|
|
|
|
void *dqueue;
|
|
|
|
void *queue;
|
|
|
|
int position;
|
|
|
|
int size;
|
|
|
|
int front;
|
|
|
|
int back;
|
2023-11-06 01:36:17 +02:00
|
|
|
int detection;
|
2023-11-05 12:47:38 +02:00
|
|
|
int init;
|
2023-11-04 15:34:18 +02:00
|
|
|
} ChannelContext;
|
|
|
|
|
2021-11-26 15:23:16 +02:00
|
|
|
typedef struct AudioDynamicEqualizerContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
double threshold;
|
2023-11-06 01:36:17 +02:00
|
|
|
double threshold_log;
|
2021-11-26 15:23:16 +02:00
|
|
|
double dfrequency;
|
|
|
|
double dqfactor;
|
|
|
|
double tfrequency;
|
|
|
|
double tqfactor;
|
|
|
|
double ratio;
|
|
|
|
double range;
|
|
|
|
double makeup;
|
2023-11-06 01:36:17 +02:00
|
|
|
double dattack;
|
|
|
|
double drelease;
|
|
|
|
double dattack_coef;
|
|
|
|
double drelease_coef;
|
|
|
|
double gattack_coef;
|
|
|
|
double grelease_coef;
|
2021-11-26 15:23:16 +02:00
|
|
|
int mode;
|
2023-01-20 14:59:16 +02:00
|
|
|
int detection;
|
2023-04-28 16:35:22 +02:00
|
|
|
int tftype;
|
|
|
|
int dftype;
|
2023-04-29 10:40:18 +02:00
|
|
|
int precision;
|
|
|
|
int format;
|
2023-11-08 15:31:50 +02:00
|
|
|
int nb_channels;
|
2023-04-29 10:40:18 +02:00
|
|
|
|
|
|
|
int (*filter_prepare)(AVFilterContext *ctx);
|
|
|
|
int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
|
|
|
|
|
|
|
|
double da_double[3], dm_double[3];
|
|
|
|
float da_float[3], dm_float[3];
|
2021-11-26 15:23:16 +02:00
|
|
|
|
2023-11-04 15:34:18 +02:00
|
|
|
ChannelContext *cc;
|
2021-11-26 15:23:16 +02:00
|
|
|
} AudioDynamicEqualizerContext;
|
|
|
|
|
2024-08-29 09:10:01 +02:00
|
|
|
static int query_formats(const AVFilterContext *ctx,
|
|
|
|
AVFilterFormatsConfig **cfg_in,
|
|
|
|
AVFilterFormatsConfig **cfg_out)
|
2021-11-26 15:23:16 +02:00
|
|
|
{
|
2024-08-29 09:10:01 +02:00
|
|
|
const AudioDynamicEqualizerContext *s = ctx->priv;
|
2023-04-29 10:40:18 +02:00
|
|
|
static const enum AVSampleFormat sample_fmts[3][3] = {
|
|
|
|
{ AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
|
|
|
|
{ AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
|
|
|
|
{ AV_SAMPLE_FMT_DBLP, AV_SAMPLE_FMT_NONE },
|
|
|
|
};
|
|
|
|
int ret;
|
2021-11-26 15:23:16 +02:00
|
|
|
|
2024-08-29 09:10:01 +02:00
|
|
|
if ((ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out,
|
|
|
|
sample_fmts[s->precision])) < 0)
|
2023-04-29 10:40:18 +02:00
|
|
|
return ret;
|
2022-10-07 23:57:46 +02:00
|
|
|
|
2024-08-29 09:10:01 +02:00
|
|
|
return 0;
|
2021-11-26 15:23:16 +02:00
|
|
|
}
|
|
|
|
|
2023-04-29 10:40:18 +02:00
|
|
|
static double get_coef(double x, double sr)
|
2021-11-26 15:23:16 +02:00
|
|
|
{
|
2023-11-06 01:36:17 +02:00
|
|
|
return 1.0 - exp(-1.0 / (0.001 * x * sr));
|
2021-11-26 15:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct ThreadData {
|
|
|
|
AVFrame *in, *out;
|
|
|
|
} ThreadData;
|
|
|
|
|
2023-04-29 10:40:18 +02:00
|
|
|
#define DEPTH 32
|
|
|
|
#include "adynamicequalizer_template.c"
|
2023-04-28 22:42:30 +02:00
|
|
|
|
2023-04-29 10:40:18 +02:00
|
|
|
#undef DEPTH
|
|
|
|
#define DEPTH 64
|
|
|
|
#include "adynamicequalizer_template.c"
|
|
|
|
|
|
|
|
static int config_input(AVFilterLink *inlink)
|
2021-11-26 15:23:16 +02:00
|
|
|
{
|
2023-04-29 10:40:18 +02:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2021-11-26 15:23:16 +02:00
|
|
|
AudioDynamicEqualizerContext *s = ctx->priv;
|
2023-04-28 16:35:22 +02:00
|
|
|
|
2023-04-29 10:40:18 +02:00
|
|
|
s->format = inlink->format;
|
2023-11-04 15:34:18 +02:00
|
|
|
s->cc = av_calloc(inlink->ch_layout.nb_channels, sizeof(*s->cc));
|
|
|
|
if (!s->cc)
|
2023-04-29 10:40:18 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2023-11-08 15:31:50 +02:00
|
|
|
s->nb_channels = inlink->ch_layout.nb_channels;
|
2023-04-28 16:35:22 +02:00
|
|
|
|
2023-04-29 10:40:18 +02:00
|
|
|
switch (s->format) {
|
|
|
|
case AV_SAMPLE_FMT_DBLP:
|
|
|
|
s->filter_prepare = filter_prepare_double;
|
|
|
|
s->filter_channels = filter_channels_double;
|
2023-04-28 16:35:22 +02:00
|
|
|
break;
|
2023-04-29 10:40:18 +02:00
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
|
|
|
s->filter_prepare = filter_prepare_float;
|
|
|
|
s->filter_channels = filter_channels_float;
|
|
|
|
break;
|
2021-11-26 15:23:16 +02:00
|
|
|
}
|
|
|
|
|
2023-11-08 15:31:50 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
ChannelContext *cc = &s->cc[ch];
|
|
|
|
cc->queue = av_calloc(inlink->sample_rate, sizeof(double));
|
|
|
|
cc->dqueue = av_calloc(inlink->sample_rate, sizeof(double));
|
|
|
|
if (!cc->queue || !cc->dqueue)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2021-11-26 15:23:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2023-04-29 10:40:18 +02:00
|
|
|
AudioDynamicEqualizerContext *s = ctx->priv;
|
2021-11-26 15:23:16 +02:00
|
|
|
ThreadData td;
|
|
|
|
AVFrame *out;
|
|
|
|
|
|
|
|
if (av_frame_is_writable(in)) {
|
|
|
|
out = in;
|
|
|
|
} else {
|
|
|
|
out = ff_get_audio_buffer(outlink, in->nb_samples);
|
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
av_frame_copy_props(out, in);
|
|
|
|
}
|
|
|
|
|
|
|
|
td.in = in;
|
|
|
|
td.out = out;
|
2023-04-29 10:40:18 +02:00
|
|
|
s->filter_prepare(ctx);
|
|
|
|
ff_filter_execute(ctx, s->filter_channels, &td, NULL,
|
2021-08-31 16:03:14 +02:00
|
|
|
FFMIN(outlink->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
|
2021-11-26 15:23:16 +02:00
|
|
|
|
|
|
|
if (out != in)
|
|
|
|
av_frame_free(&in);
|
|
|
|
return ff_filter_frame(outlink, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioDynamicEqualizerContext *s = ctx->priv;
|
|
|
|
|
2023-11-08 15:31:50 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
ChannelContext *cc = &s->cc[ch];
|
|
|
|
av_freep(&cc->queue);
|
|
|
|
av_freep(&cc->dqueue);
|
|
|
|
}
|
2023-11-04 15:34:18 +02:00
|
|
|
av_freep(&s->cc);
|
2021-11-26 15:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(AudioDynamicEqualizerContext, x)
|
2023-04-29 10:40:18 +02:00
|
|
|
#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
2021-11-26 15:23:16 +02:00
|
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
|
|
|
|
|
|
|
|
static const AVOption adynamicequalizer_options[] = {
|
|
|
|
{ "threshold", "set detection threshold", OFFSET(threshold), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 100, FLAGS },
|
|
|
|
{ "dfrequency", "set detection frequency", OFFSET(dfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
|
|
|
|
{ "dqfactor", "set detection Q factor", OFFSET(dqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
|
|
|
|
{ "tfrequency", "set target frequency", OFFSET(tfrequency), AV_OPT_TYPE_DOUBLE, {.dbl=1000}, 2, 1000000, FLAGS },
|
|
|
|
{ "tqfactor", "set target Q factor", OFFSET(tqfactor), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.001, 1000, FLAGS },
|
2023-11-06 01:36:17 +02:00
|
|
|
{ "attack", "set detection attack duration", OFFSET(dattack), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 0.01, 2000, FLAGS },
|
|
|
|
{ "release","set detection release duration",OFFSET(drelease), AV_OPT_TYPE_DOUBLE, {.dbl=200}, 0.01, 2000, FLAGS },
|
2022-10-07 23:57:46 +02:00
|
|
|
{ "ratio", "set ratio factor", OFFSET(ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0, 30, FLAGS },
|
2023-11-06 01:36:17 +02:00
|
|
|
{ "makeup", "set makeup gain", OFFSET(makeup), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 1000, FLAGS },
|
|
|
|
{ "range", "set max gain", OFFSET(range), AV_OPT_TYPE_DOUBLE, {.dbl=50}, 1, 2000, FLAGS },
|
2024-02-11 16:41:05 +02:00
|
|
|
{ "mode", "set mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, LISTEN,NB_FMODES-1,FLAGS, .unit = "mode" },
|
|
|
|
{ "listen", 0, 0, AV_OPT_TYPE_CONST, {.i64=LISTEN}, 0, 0, FLAGS, .unit = "mode" },
|
|
|
|
{ "cutbelow", 0, 0, AV_OPT_TYPE_CONST, {.i64=CUT_BELOW},0, 0, FLAGS, .unit = "mode" },
|
|
|
|
{ "cutabove", 0, 0, AV_OPT_TYPE_CONST, {.i64=CUT_ABOVE},0, 0, FLAGS, .unit = "mode" },
|
|
|
|
{ "boostbelow", 0, 0, AV_OPT_TYPE_CONST, {.i64=BOOST_BELOW},0, 0, FLAGS, .unit = "mode" },
|
|
|
|
{ "boostabove", 0, 0, AV_OPT_TYPE_CONST, {.i64=BOOST_ABOVE},0, 0, FLAGS, .unit = "mode" },
|
|
|
|
{ "dftype", "set detection filter type",OFFSET(dftype), AV_OPT_TYPE_INT, {.i64=0}, 0, 3, FLAGS, .unit = "dftype" },
|
|
|
|
{ "bandpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "dftype" },
|
|
|
|
{ "lowpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "dftype" },
|
|
|
|
{ "highpass", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, .unit = "dftype" },
|
|
|
|
{ "peak", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, FLAGS, .unit = "dftype" },
|
|
|
|
{ "tftype", "set target filter type", OFFSET(tftype), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, .unit = "tftype" },
|
|
|
|
{ "bell", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "tftype" },
|
|
|
|
{ "lowshelf", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "tftype" },
|
|
|
|
{ "highshelf",0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, .unit = "tftype" },
|
|
|
|
{ "auto", "set auto threshold", OFFSET(detection), AV_OPT_TYPE_INT, {.i64=DET_OFF},DET_DISABLED,NB_DMODES-1,FLAGS, .unit = "auto" },
|
|
|
|
{ "disabled", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_DISABLED}, 0, 0, FLAGS, .unit = "auto" },
|
|
|
|
{ "off", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_OFF}, 0, 0, FLAGS, .unit = "auto" },
|
|
|
|
{ "on", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_ON}, 0, 0, FLAGS, .unit = "auto" },
|
|
|
|
{ "adaptive", 0, 0, AV_OPT_TYPE_CONST, {.i64=DET_ADAPTIVE}, 0, 0, FLAGS, .unit = "auto" },
|
|
|
|
{ "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, AF, .unit = "precision" },
|
|
|
|
{ "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, AF, .unit = "precision" },
|
|
|
|
{ "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, AF, .unit = "precision" },
|
|
|
|
{ "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, AF, .unit = "precision" },
|
2021-11-26 15:23:16 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(adynamicequalizer);
|
|
|
|
|
|
|
|
static const AVFilterPad inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const AVFilter ff_af_adynamicequalizer = {
|
|
|
|
.name = "adynamicequalizer",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply Dynamic Equalization of input audio."),
|
|
|
|
.priv_size = sizeof(AudioDynamicEqualizerContext),
|
|
|
|
.priv_class = &adynamicequalizer_class,
|
|
|
|
.uninit = uninit,
|
|
|
|
FILTER_INPUTS(inputs),
|
2023-08-03 00:59:02 +02:00
|
|
|
FILTER_OUTPUTS(ff_audio_default_filterpad),
|
2024-08-29 09:10:01 +02:00
|
|
|
FILTER_QUERY_FUNC2(query_formats),
|
2021-11-26 15:23:16 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
|
|
|
|
AVFILTER_FLAG_SLICE_THREADS,
|
|
|
|
.process_command = ff_filter_process_command,
|
|
|
|
};
|