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
23 #include "libavutil/avassert.h"
24 #include "libavutil/audio_fifo.h"
25 #include "libavutil/avstring.h"
26 #include "libavutil/opt.h"
31 #include "af_anlmdndsp.h"
33 #define WEIGHT_LUT_NBITS 20
34 #define WEIGHT_LUT_SIZE (1<<WEIGHT_LUT_NBITS)
36 #define SQR(x) ((x) * (x))
38 typedef struct AudioNLMeansContext {
47 float pdiff_lut_scale;
48 float weight_lut[WEIGHT_LUT_SIZE];
64 AudioNLMDNDSPContext dsp;
65 } AudioNLMeansContext;
74 #define OFFSET(x) offsetof(AudioNLMeansContext, x)
75 #define AFT AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
77 static const AVOption anlmdn_options[] = {
78 { "s", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10, AFT },
79 { "p", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AFT },
80 { "r", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AFT },
81 { "o", "set output mode", OFFSET(om), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, AFT, "mode" },
82 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AFT, "mode" },
83 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AFT, "mode" },
84 { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE},0, 0, AFT, "mode" },
85 { "m", "set smooth factor", OFFSET(m), AV_OPT_TYPE_FLOAT, {.dbl=11.}, 1, 15, AFT },
89 AVFILTER_DEFINE_CLASS(anlmdn);
91 static int query_formats(AVFilterContext *ctx)
93 AVFilterFormats *formats = NULL;
94 AVFilterChannelLayouts *layouts = NULL;
95 static const enum AVSampleFormat sample_fmts[] = {
101 formats = ff_make_format_list(sample_fmts);
103 return AVERROR(ENOMEM);
104 ret = ff_set_common_formats(ctx, formats);
108 layouts = ff_all_channel_counts();
110 return AVERROR(ENOMEM);
112 ret = ff_set_common_channel_layouts(ctx, layouts);
116 formats = ff_all_samplerates();
117 return ff_set_common_samplerates(ctx, formats);
120 static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
124 for (int k = -K; k <= K; k++)
125 distance += SQR(f1[k] - f2[k]);
130 static void compute_cache_c(float *cache, const float *f,
131 ptrdiff_t S, ptrdiff_t K,
132 ptrdiff_t i, ptrdiff_t jj)
136 for (int j = jj; j < jj + S; j++, v++)
137 cache[v] += -SQR(f[i - K - 1] - f[j - K - 1]) + SQR(f[i + K] - f[j + K]);
140 void ff_anlmdn_init(AudioNLMDNDSPContext *dsp)
142 dsp->compute_distance_ssd = compute_distance_ssd_c;
143 dsp->compute_cache = compute_cache_c;
146 ff_anlmdn_init_x86(dsp);
149 static int config_filter(AVFilterContext *ctx)
151 AudioNLMeansContext *s = ctx->priv;
152 AVFilterLink *outlink = ctx->outputs[0];
153 int newK, newS, newH, newN;
154 AVFrame *new_in, *new_cache;
156 newK = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
157 newS = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE);
160 newN = newH + (newK + newS) * 2;
162 av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", newK, newS, newH, newN);
164 if (!s->cache || s->cache->nb_samples < newS * 2) {
165 new_cache = ff_get_audio_buffer(outlink, newS * 2);
167 av_frame_free(&s->cache);
168 s->cache = new_cache;
170 return AVERROR(ENOMEM);
174 return AVERROR(ENOMEM);
176 s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE;
177 for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
178 float w = -i / s->pdiff_lut_scale;
180 s->weight_lut[i] = expf(w);
183 if (!s->in || s->in->nb_samples < newN) {
184 new_in = ff_get_audio_buffer(outlink, newN);
186 av_frame_free(&s->in);
189 return AVERROR(ENOMEM);
193 return AVERROR(ENOMEM);
203 static int config_output(AVFilterLink *outlink)
205 AVFilterContext *ctx = outlink->src;
206 AudioNLMeansContext *s = ctx->priv;
210 s->pts = AV_NOPTS_VALUE;
212 ret = config_filter(ctx);
216 s->fifo = av_audio_fifo_alloc(outlink->format, outlink->channels, s->N);
218 return AVERROR(ENOMEM);
220 ret = av_audio_fifo_write(s->fifo, (void **)s->in->extended_data, s->K + s->S);
224 ff_anlmdn_init(&s->dsp);
229 static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
231 AudioNLMeansContext *s = ctx->priv;
235 const int om = s->om;
236 const float *f = (const float *)(s->in->extended_data[ch]) + K;
237 float *cache = (float *)s->cache->extended_data[ch];
238 const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
239 float *dst = (float *)out->extended_data[ch] + s->offset;
240 const float smooth = s->m;
242 for (int i = S; i < s->H + S; i++) {
243 float P = 0.f, Q = 0.f;
247 for (int j = i - S; j <= i + S; j++) {
250 cache[v++] = s->dsp.compute_distance_ssd(f + i, f + j, K);
253 s->dsp.compute_cache(cache, f, S, K, i, i - S);
254 s->dsp.compute_cache(cache + S, f, S, K, i, i + 1);
257 for (int j = 0; j < 2 * S && !ctx->is_disabled; j++) {
258 const float distance = cache[j];
259 unsigned weight_lut_idx;
262 if (distance < 0.f) {
269 weight_lut_idx = w * s->pdiff_lut_scale;
270 av_assert2(weight_lut_idx < WEIGHT_LUT_SIZE);
271 w = s->weight_lut[weight_lut_idx];
272 P += w * f[i - S + j + (j >= S)];
280 case IN_MODE: dst[i - S] = f[i]; break;
281 case OUT_MODE: dst[i - S] = P / Q; break;
282 case NOISE_MODE: dst[i - S] = f[i] - (P / Q); break;
289 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
291 AVFilterContext *ctx = inlink->dst;
292 AVFilterLink *outlink = ctx->outputs[0];
293 AudioNLMeansContext *s = ctx->priv;
295 int available, wanted, ret;
297 if (s->pts == AV_NOPTS_VALUE)
300 ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
305 available = av_audio_fifo_size(s->fifo);
306 wanted = (available / s->H) * s->H;
308 if (wanted >= s->H && available >= s->N) {
309 out = ff_get_audio_buffer(outlink, wanted);
311 return AVERROR(ENOMEM);
314 while (available >= s->N) {
315 ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data, s->N);
319 ctx->internal->execute(ctx, filter_channel, out, NULL, inlink->channels);
321 av_audio_fifo_drain(s->fifo, s->H);
329 out->nb_samples = s->offset;
330 if (s->eof_left >= 0) {
331 out->nb_samples = FFMIN(s->eof_left, s->offset);
332 s->eof_left -= out->nb_samples;
334 s->pts += av_rescale_q(s->offset, (AVRational){1, outlink->sample_rate}, outlink->time_base);
336 return ff_filter_frame(outlink, out);
342 static int request_frame(AVFilterLink *outlink)
344 AVFilterContext *ctx = outlink->src;
345 AudioNLMeansContext *s = ctx->priv;
348 ret = ff_request_frame(ctx->inputs[0]);
350 if (ret == AVERROR_EOF && s->eof_left != 0) {
354 s->eof_left = av_audio_fifo_size(s->fifo) - (s->S + s->K);
355 if (s->eof_left <= 0)
357 in = ff_get_audio_buffer(outlink, s->H);
359 return AVERROR(ENOMEM);
361 return filter_frame(ctx->inputs[0], in);
367 static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
368 char *res, int res_len, int flags)
372 ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
376 ret = config_filter(ctx);
383 static av_cold void uninit(AVFilterContext *ctx)
385 AudioNLMeansContext *s = ctx->priv;
387 av_audio_fifo_free(s->fifo);
388 av_frame_free(&s->in);
389 av_frame_free(&s->cache);
392 static const AVFilterPad inputs[] = {
395 .type = AVMEDIA_TYPE_AUDIO,
396 .filter_frame = filter_frame,
401 static const AVFilterPad outputs[] = {
404 .type = AVMEDIA_TYPE_AUDIO,
405 .config_props = config_output,
406 .request_frame = request_frame,
411 const AVFilter ff_af_anlmdn = {
413 .description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."),
414 .query_formats = query_formats,
415 .priv_size = sizeof(AudioNLMeansContext),
416 .priv_class = &anlmdn_class,
420 .process_command = process_command,
421 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
422 AVFILTER_FLAG_SLICE_THREADS,