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" },
79 AVFILTER_DEFINE_CLASS_EXT(anlms, "anlm(f|s)", anlms_options);
81 static int query_formats(AVFilterContext *ctx)
83 static const enum AVSampleFormat sample_fmts[] = {
87 int ret = ff_set_common_all_channel_counts(ctx);
91 ret = ff_set_common_formats_from_list(ctx, sample_fmts);
95 return ff_set_common_all_samplerates(ctx);
98 static float fir_sample(AudioNLMSContext *s, float sample, float *delay,
99 float *coeffs, float *tmp, int *offset)
101 const int order = s->order;
104 delay[*offset] = sample;
106 memcpy(tmp, coeffs + order - *offset, order * sizeof(float));
108 output = s->fdsp->scalarproduct_float(delay, tmp, s->kernel_size);
116 static float process_sample(AudioNLMSContext *s, float input, float desired,
117 float *delay, float *coeffs, float *tmp, int *offsetp)
119 const int order = s->order;
120 const float leakage = s->leakage;
121 const float mu = s->mu;
122 const float a = 1.f - leakage * mu;
123 float sum, output, e, norm, b;
124 int offset = *offsetp;
126 delay[offset + order] = input;
128 output = fir_sample(s, input, delay, coeffs, tmp, offsetp);
129 e = desired - output;
131 sum = s->fdsp->scalarproduct_float(delay, delay, s->kernel_size);
138 memcpy(tmp, delay + offset, order * sizeof(float));
140 s->fdsp->vector_fmul_scalar(coeffs, coeffs, a, s->kernel_size);
142 s->fdsp->vector_fmac_scalar(coeffs, tmp, b, s->kernel_size);
144 memcpy(coeffs + order, coeffs, order * sizeof(float));
146 switch (s->output_mode) {
147 case IN_MODE: output = input; break;
148 case DESIRED_MODE: output = desired; break;
149 case OUT_MODE: /*output = output;*/ break;
150 case NOISE_MODE: output = desired - output; break;
155 static int process_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
157 AudioNLMSContext *s = ctx->priv;
159 const int start = (out->ch_layout.nb_channels * jobnr) / nb_jobs;
160 const int end = (out->ch_layout.nb_channels * (jobnr+1)) / nb_jobs;
162 for (int c = start; c < end; c++) {
163 const float *input = (const float *)s->frame[0]->extended_data[c];
164 const float *desired = (const float *)s->frame[1]->extended_data[c];
165 float *delay = (float *)s->delay->extended_data[c];
166 float *coeffs = (float *)s->coeffs->extended_data[c];
167 float *tmp = (float *)s->tmp->extended_data[c];
168 int *offset = (int *)s->offset->extended_data[c];
169 float *output = (float *)out->extended_data[c];
171 for (int n = 0; n < out->nb_samples; n++) {
172 output[n] = process_sample(s, input[n], desired[n], delay, coeffs, tmp, offset);
173 if (ctx->is_disabled)
174 output[n] = input[n];
181 static int activate(AVFilterContext *ctx)
183 AudioNLMSContext *s = ctx->priv;
188 FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx);
190 nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]),
191 ff_inlink_queued_samples(ctx->inputs[1]));
192 for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) {
196 if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) {
197 ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]);
203 if (s->frame[0] && s->frame[1]) {
206 out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples);
208 av_frame_free(&s->frame[0]);
209 av_frame_free(&s->frame[1]);
210 return AVERROR(ENOMEM);
213 ff_filter_execute(ctx, process_channels, out, NULL,
214 FFMIN(ctx->outputs[0]->ch_layout.nb_channels, ff_filter_get_nb_threads(ctx)));
216 out->pts = s->frame[0]->pts;
218 av_frame_free(&s->frame[0]);
219 av_frame_free(&s->frame[1]);
221 ret = ff_filter_frame(ctx->outputs[0], out);
227 for (i = 0; i < 2; i++) {
228 if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) {
229 ff_outlink_set_status(ctx->outputs[0], status, pts);
235 if (ff_outlink_frame_wanted(ctx->outputs[0])) {
236 for (i = 0; i < 2; i++) {
237 if (ff_inlink_queued_samples(ctx->inputs[i]) > 0)
239 ff_inlink_request_frame(ctx->inputs[i]);
246 static int config_output(AVFilterLink *outlink)
248 AVFilterContext *ctx = outlink->src;
249 AudioNLMSContext *s = ctx->priv;
251 s->anlmf = !strcmp(ctx->filter->name, "anlmf");
252 s->kernel_size = FFALIGN(s->order, 16);
255 s->offset = ff_get_audio_buffer(outlink, 1);
257 s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
259 s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size);
261 s->tmp = ff_get_audio_buffer(outlink, s->kernel_size);
262 if (!s->delay || !s->coeffs || !s->offset || !s->tmp)
263 return AVERROR(ENOMEM);
268 static av_cold int init(AVFilterContext *ctx)
270 AudioNLMSContext *s = ctx->priv;
272 s->fdsp = avpriv_float_dsp_alloc(0);
274 return AVERROR(ENOMEM);
279 static av_cold void uninit(AVFilterContext *ctx)
281 AudioNLMSContext *s = ctx->priv;
284 av_frame_free(&s->delay);
285 av_frame_free(&s->coeffs);
286 av_frame_free(&s->offset);
287 av_frame_free(&s->tmp);
290 static const AVFilterPad inputs[] = {
293 .type = AVMEDIA_TYPE_AUDIO,
297 .type = AVMEDIA_TYPE_AUDIO,
301 static const AVFilterPad outputs[] = {
304 .type = AVMEDIA_TYPE_AUDIO,
305 .config_props = config_output,
309 const AVFilter ff_af_anlms = {
311 .description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Squares algorithm to first audio stream."),
312 .priv_size = sizeof(AudioNLMSContext),
313 .priv_class = &anlms_class,
316 .activate = activate,
317 FILTER_INPUTS(inputs),
318 FILTER_OUTPUTS(outputs),
319 FILTER_QUERY_FUNC(query_formats),
320 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
321 AVFILTER_FLAG_SLICE_THREADS,
322 .process_command = ff_filter_process_command,
325 const AVFilter ff_af_anlmf = {
327 .description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Fourth algorithm to first audio stream."),
328 .priv_size = sizeof(AudioNLMSContext),
329 .priv_class = &anlms_class,
332 .activate = activate,
333 FILTER_INPUTS(inputs),
334 FILTER_OUTPUTS(outputs),
335 FILTER_QUERY_FUNC(query_formats),
336 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
337 AVFILTER_FLAG_SLICE_THREADS,
338 .process_command = ff_filter_process_command,