2 * Copyright (c) 2019 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/channel_layout.h"
22 #include "libavutil/common.h"
23 #include "libavutil/float_dsp.h"
24 #include "libavutil/opt.h"
40 typedef struct AudioNLMSContext {
59 AVFloatDSPContext *fdsp;
62 #define OFFSET(x) offsetof(AudioNLMSContext, x)
63 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
64 #define AT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
66 static const AVOption anlms_options[] = {
67 { "order", "set the filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=256}, 1, INT16_MAX, A },
68 { "mu", "set the filter mu", OFFSET(mu), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 2, AT },
69 { "eps", "set the filter eps", OFFSET(eps), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, AT },
70 { "leakage", "set the filter leakage", OFFSET(leakage), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, AT },
71 { "out_mode", "set output mode", OFFSET(output_mode), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_OMODES-1, AT, "mode" },
72 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AT, "mode" },
73 { "d", "desired", 0, AV_OPT_TYPE_CONST, {.i64=DESIRED_MODE}, 0, 0, AT, "mode" },
74 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AT, "mode" },
75 { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE}, 0, 0, AT, "mode" },
76 { "e", "error", 0, AV_OPT_TYPE_CONST, {.i64=ERROR_MODE}, 0, 0, AT, "mode" },
80 AVFILTER_DEFINE_CLASS_EXT(anlms, "anlm(f|s)", anlms_options);
82 static float fir_sample(AudioNLMSContext *s, float sample, float *delay,
83 float *coeffs, float *tmp, int *offset)
85 const int order = s->order;
88 delay[*offset] = sample;
90 memcpy(tmp, coeffs + order - *offset, order * sizeof(float));
92 output = s->fdsp->scalarproduct_float(delay, tmp, s->kernel_size);
100 static float process_sample(AudioNLMSContext *s, float input, float desired,
101 float *delay, float *coeffs, float *tmp, int *offsetp)
103 const int order = s->order;
104 const float leakage = s->leakage;
105 const float mu = s->mu;
106 const float a = 1.f - leakage;
107 float sum, output, e, norm, b;
108 int offset = *offsetp;
110 delay[offset + order] = input;
112 output = fir_sample(s, input, delay, coeffs, tmp, offsetp);
113 e = desired - output;
115 sum = s->fdsp->scalarproduct_float(delay, delay, s->kernel_size);
122 memcpy(tmp, delay + offset, order * sizeof(float));
124 s->fdsp->vector_fmul_scalar(coeffs, coeffs, a, s->kernel_size);
126 s->fdsp->vector_fmac_scalar(coeffs, tmp, b, s->kernel_size);
128 memcpy(coeffs + order, coeffs, order * sizeof(float));
130 switch (s->output_mode) {
131 case IN_MODE: output = input; break;
132 case DESIRED_MODE: output = desired; break;
133 case OUT_MODE: output = desired - output; break;
134 case NOISE_MODE: output = input - output; break;
135 case ERROR_MODE: break;
140 static int process_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
142 AudioNLMSContext *s = ctx->priv;
144 const int start = (out->ch_layout.nb_channels * jobnr) / nb_jobs;
145 const int end = (out->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
147 for (int c = start; c < end; c++) {
148 const float *input = (const float *)s->frame[0]->extended_data[c];
149 const float *desired = (const float *)s->frame[1]->extended_data[c];
150 float *delay = (float *)s->delay->extended_data[c];
151 float *coeffs = (float *)s->coeffs->extended_data[c];
152 float *tmp = (float *)s->tmp->extended_data[c];
153 int *offset = (int *)s->offset->extended_data[c];
154 float *output = (float *)out->extended_data[c];
156 for (int n = 0; n < out->nb_samples; n++) {
157 output[n] = process_sample(s, input[n], desired[n], delay, coeffs, tmp, offset);
158 if (ctx->is_disabled)
159 output[n] = input[n];
166 static int activate(AVFilterContext *ctx)
168 AudioNLMSContext *s = ctx->priv;
173 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
175 nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
176 ff_inlink_queued_samples(ctx->inputs[1]));
177 for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
181 if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
182 ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]);
188 if (s->frame[0] && s->frame[1]) {
191 out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples);
193 av_frame_free(&s->frame[0]);
194 av_frame_free(&s->frame[1]);
195 return AVERROR(ENOMEM);
198 ff_filter_execute(ctx, process_channels, out, NULL,
199 FFMIN(ctx->outputs[0]->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
201 out->pts = s->frame[0]->pts;
203 av_frame_free(&s->frame[0]);
204 av_frame_free(&s->frame[1]);
206 ret = ff_filter_frame(ctx->outputs[0], out);
212 for (i = 0; i < 2; i++) {
213 if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
214 ff_outlink_set_status(ctx->outputs[0], status, pts);
220 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
221 for (i = 0; i < 2; i++) {
222 if (ff_inlink_queued_samples(ctx->inputs[i]) > 0)
224 ff_inlink_request_frame(ctx->inputs[i]);
231 static int config_output(AVFilterLink *outlink)
233 AVFilterContext *ctx = outlink->src;
234 AudioNLMSContext *s = ctx->priv;
236 s->anlmf = !strcmp(ctx->filter->name, "anlmf");
237 s->kernel_size = FFALIGN(s->order, 16);
240 s->offset = ff_get_audio_buffer(outlink, 1);
242 s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
244 s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
246 s->tmp = ff_get_audio_buffer(outlink, s->kernel_size);
247 if (!s->delay || !s->coeffs || !s->offset || !s->tmp)
248 return AVERROR(ENOMEM);
253 static av_cold int init(AVFilterContext *ctx)
255 AudioNLMSContext *s = ctx->priv;
257 s->fdsp = avpriv_float_dsp_alloc(0);
259 return AVERROR(ENOMEM);
264 static av_cold void uninit(AVFilterContext *ctx)
266 AudioNLMSContext *s = ctx->priv;
269 av_frame_free(&s->delay);
270 av_frame_free(&s->coeffs);
271 av_frame_free(&s->offset);
272 av_frame_free(&s->tmp);
275 static const AVFilterPad inputs[] = {
278 .type = AVMEDIA_TYPE_AUDIO,
282 .type = AVMEDIA_TYPE_AUDIO,
286 static const AVFilterPad outputs[] = {
289 .type = AVMEDIA_TYPE_AUDIO,
290 .config_props = config_output,
294 const AVFilter ff_af_anlms = {
296 .description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Squares algorithm to first audio stream."),
297 .priv_size = sizeof(AudioNLMSContext),
298 .priv_class = &anlms_class,
301 .activate = activate,
302 FILTER_INPUTS(inputs),
303 FILTER_OUTPUTS(outputs),
304 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
305 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
306 AVFILTER_FLAG_SLICE_THREADS,
307 .process_command = ff_filter_process_command,
310 const AVFilter ff_af_anlmf = {
312 .description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Fourth algorithm to first audio stream."),
313 .priv_size = sizeof(AudioNLMSContext),
314 .priv_class = &anlms_class,
317 .activate = activate,
318 FILTER_INPUTS(inputs),
319 FILTER_OUTPUTS(outputs),
320 FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_FLTP),
321 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
322 AVFILTER_FLAG_SLICE_THREADS,
323 .process_command = ff_filter_process_command,