2019-10-03 18:09:59 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Paul B Mahol
|
|
|
|
*
|
|
|
|
* 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 "libavutil/channel_layout.h"
|
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/float_dsp.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2019-10-03 18:09:59 +02:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
|
|
|
#include "audio.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "filters.h"
|
2023-11-27 20:53:38 +02:00
|
|
|
#include "formats.h"
|
2019-10-03 18:09:59 +02:00
|
|
|
|
|
|
|
enum OutModes {
|
|
|
|
IN_MODE,
|
|
|
|
DESIRED_MODE,
|
|
|
|
OUT_MODE,
|
|
|
|
NOISE_MODE,
|
2023-05-01 20:01:50 +02:00
|
|
|
ERROR_MODE,
|
2019-10-03 18:09:59 +02:00
|
|
|
NB_OMODES
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct AudioNLMSContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
int order;
|
|
|
|
float mu;
|
|
|
|
float eps;
|
|
|
|
float leakage;
|
|
|
|
int output_mode;
|
2023-11-27 20:53:38 +02:00
|
|
|
int precision;
|
2019-10-03 18:09:59 +02:00
|
|
|
|
|
|
|
int kernel_size;
|
|
|
|
AVFrame *offset;
|
|
|
|
AVFrame *delay;
|
|
|
|
AVFrame *coeffs;
|
|
|
|
AVFrame *tmp;
|
|
|
|
|
|
|
|
AVFrame *frame[2];
|
|
|
|
|
2021-12-18 17:04:40 +02:00
|
|
|
int anlmf;
|
|
|
|
|
2023-11-27 20:53:38 +02:00
|
|
|
int (*filter_channels)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
|
|
|
|
|
2019-10-03 18:09:59 +02:00
|
|
|
AVFloatDSPContext *fdsp;
|
|
|
|
} AudioNLMSContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(AudioNLMSContext, x)
|
|
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
2019-10-08 12:49:28 +02:00
|
|
|
#define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
|
2019-10-03 18:09:59 +02:00
|
|
|
|
|
|
|
static const AVOption anlms_options[] = {
|
|
|
|
{ "order", "set the filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=256}, 1, INT16_MAX, A },
|
2019-10-08 12:49:28 +02:00
|
|
|
{ "mu", "set the filter mu", OFFSET(mu), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 2, AT },
|
|
|
|
{ "eps", "set the filter eps", OFFSET(eps), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AT },
|
|
|
|
{ "leakage", "set the filter leakage", OFFSET(leakage), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, AT },
|
2024-02-11 16:41:05 +02:00
|
|
|
{ "out_mode", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_OMODES-1, AT, .unit = "mode" },
|
|
|
|
{ "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AT, .unit = "mode" },
|
|
|
|
{ "d", "desired", 0, AV_OPT_TYPE_CONST, {.i64=DESIRED_MODE}, 0, 0, AT, .unit = "mode" },
|
|
|
|
{ "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AT, .unit = "mode" },
|
|
|
|
{ "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE}, 0, 0, AT, .unit = "mode" },
|
|
|
|
{ "e", "error", 0, AV_OPT_TYPE_CONST, {.i64=ERROR_MODE}, 0, 0, AT, .unit = "mode" },
|
|
|
|
{ "precision", "set processing precision", OFFSET(precision), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, A, .unit = "precision" },
|
|
|
|
{ "auto", "set auto processing precision", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, .unit = "precision" },
|
|
|
|
{ "float", "set single-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, .unit = "precision" },
|
|
|
|
{ "double","set double-floating point processing precision", 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, .unit = "precision" },
|
2019-10-03 18:09:59 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2021-12-18 17:04:40 +02:00
|
|
|
AVFILTER_DEFINE_CLASS_EXT(anlms, "anlm(f|s)", anlms_options);
|
2019-10-03 18:09:59 +02:00
|
|
|
|
2024-08-30 21:56:02 +02:00
|
|
|
static int query_formats(const AVFilterContext *ctx,
|
|
|
|
AVFilterFormatsConfig **cfg_in,
|
|
|
|
AVFilterFormatsConfig **cfg_out)
|
2019-10-03 18:09:59 +02:00
|
|
|
{
|
2024-08-30 21:56:02 +02:00
|
|
|
const AudioNLMSContext *s = ctx->priv;
|
2023-11-27 20:53:38 +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;
|
2019-10-03 18:09:59 +02:00
|
|
|
|
2024-08-30 21:56:02 +02:00
|
|
|
if ((ret = ff_set_common_formats_from_list2(ctx, cfg_in, cfg_out,
|
|
|
|
sample_fmts[s->precision])) < 0)
|
2023-11-27 20:53:38 +02:00
|
|
|
return ret;
|
2019-10-03 18:09:59 +02:00
|
|
|
|
2024-08-30 21:56:02 +02:00
|
|
|
return 0;
|
2019-10-03 18:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int activate(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioNLMSContext *s = ctx->priv;
|
|
|
|
int i, ret, status;
|
|
|
|
int nb_samples;
|
|
|
|
int64_t pts;
|
|
|
|
|
|
|
|
FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
|
|
|
|
|
|
|
|
nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
|
|
|
|
ff_inlink_queued_samples(ctx->inputs[1]));
|
|
|
|
for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
|
|
|
|
if (s->frame[i])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
|
|
|
|
ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->frame[0] && s->frame[1]) {
|
|
|
|
AVFrame *out;
|
|
|
|
|
|
|
|
out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples);
|
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&s->frame[0]);
|
|
|
|
av_frame_free(&s->frame[1]);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2023-11-27 20:53:38 +02:00
|
|
|
ff_filter_execute(ctx, s->filter_channels, out, NULL,
|
2021-08-31 16:03:14 +02:00
|
|
|
FFMIN(ctx->outputs[0]->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
|
2019-10-03 18:09:59 +02:00
|
|
|
|
|
|
|
out->pts = s->frame[0]->pts;
|
2023-11-28 14:08:12 +02:00
|
|
|
out->duration = s->frame[0]->duration;
|
2019-10-03 18:09:59 +02:00
|
|
|
|
|
|
|
av_frame_free(&s->frame[0]);
|
|
|
|
av_frame_free(&s->frame[1]);
|
|
|
|
|
|
|
|
ret = ff_filter_frame(ctx->outputs[0], out);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nb_samples) {
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
|
|
|
|
ff_outlink_set_status(ctx->outputs[0], status, pts);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ff_outlink_frame_wanted(ctx->outputs[0])) {
|
|
|
|
for (i = 0; i < 2; i++) {
|
2023-11-28 13:19:08 +02:00
|
|
|
if (s->frame[i] || ff_inlink_queued_samples(ctx->inputs[i]) > 0)
|
2019-10-03 18:09:59 +02:00
|
|
|
continue;
|
|
|
|
ff_inlink_request_frame(ctx->inputs[i]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-27 20:53:38 +02:00
|
|
|
#define DEPTH 32
|
|
|
|
#include "anlms_template.c"
|
|
|
|
|
|
|
|
#undef DEPTH
|
|
|
|
#define DEPTH 64
|
|
|
|
#include "anlms_template.c"
|
|
|
|
|
2019-10-03 18:09:59 +02:00
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AudioNLMSContext *s = ctx->priv;
|
|
|
|
|
2021-12-18 17:04:40 +02:00
|
|
|
s->anlmf = !strcmp(ctx->filter->name, "anlmf");
|
2019-10-03 18:09:59 +02:00
|
|
|
s->kernel_size = FFALIGN(s->order, 16);
|
|
|
|
|
|
|
|
if (!s->offset)
|
|
|
|
s->offset = ff_get_audio_buffer(outlink, 1);
|
|
|
|
if (!s->delay)
|
|
|
|
s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
|
|
|
|
if (!s->coeffs)
|
|
|
|
s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
|
|
|
|
if (!s->tmp)
|
|
|
|
s->tmp = ff_get_audio_buffer(outlink, s->kernel_size);
|
|
|
|
if (!s->delay || !s->coeffs || !s->offset || !s->tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2023-11-27 20:53:38 +02:00
|
|
|
switch (outlink->format) {
|
|
|
|
case AV_SAMPLE_FMT_DBLP:
|
|
|
|
s->filter_channels = filter_channels_double;
|
|
|
|
break;
|
|
|
|
case AV_SAMPLE_FMT_FLTP:
|
|
|
|
s->filter_channels = filter_channels_float;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:09:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioNLMSContext *s = ctx->priv;
|
|
|
|
|
|
|
|
s->fdsp = avpriv_float_dsp_alloc(0);
|
|
|
|
if (!s->fdsp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AudioNLMSContext *s = ctx->priv;
|
|
|
|
|
|
|
|
av_freep(&s->fdsp);
|
|
|
|
av_frame_free(&s->delay);
|
|
|
|
av_frame_free(&s->coeffs);
|
|
|
|
av_frame_free(&s->offset);
|
|
|
|
av_frame_free(&s->tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "input",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "desired",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_anlms = {
|
2019-10-03 18:09:59 +02:00
|
|
|
.name = "anlms",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Squares algorithm to first audio stream."),
|
|
|
|
.priv_size = sizeof(AudioNLMSContext),
|
|
|
|
.priv_class = &anlms_class,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.activate = activate,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(inputs),
|
|
|
|
FILTER_OUTPUTS(outputs),
|
2023-04-22 15:31:34 +02:00
|
|
|
FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
|
2022-04-27 13:30:49 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
|
|
|
|
AVFILTER_FLAG_SLICE_THREADS,
|
2019-10-08 12:49:28 +02:00
|
|
|
.process_command = ff_filter_process_command,
|
2019-10-03 18:09:59 +02:00
|
|
|
};
|
2021-12-18 17:04:40 +02:00
|
|
|
|
|
|
|
const AVFilter ff_af_anlmf = {
|
|
|
|
.name = "anlmf",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Fourth algorithm to first audio stream."),
|
|
|
|
.priv_size = sizeof(AudioNLMSContext),
|
|
|
|
.priv_class = &anlms_class,
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.activate = activate,
|
|
|
|
FILTER_INPUTS(inputs),
|
|
|
|
FILTER_OUTPUTS(outputs),
|
2024-08-30 21:56:02 +02:00
|
|
|
FILTER_QUERY_FUNC2(query_formats),
|
2022-04-27 13:30:49 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
|
|
|
|
AVFILTER_FLAG_SLICE_THREADS,
|
2021-12-18 17:04:40 +02:00
|
|
|
.process_command = ff_filter_process_command,
|
|
|
|
};
|