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/opt.h"
30 #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 {
46 float pdiff_lut_scale;
47 float weight_lut[WEIGHT_LUT_SIZE];
63 AudioNLMDNDSPContext dsp;
64 } AudioNLMeansContext;
73 #define OFFSET(x) offsetof(AudioNLMeansContext, x)
74 #define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
76 static const AVOption anlmdn_options[] = {
77 { "s", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=0.00001},0.00001, 10, AF },
78 { "p", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AF },
79 { "r", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AF },
80 { "o", "set output mode", OFFSET(om), AV_OPT_TYPE_INT, {.i64=OUT_MODE}, 0, NB_MODES-1, AF, "mode" },
81 { "i", "input", 0, AV_OPT_TYPE_CONST, {.i64=IN_MODE}, 0, 0, AF, "mode" },
82 { "o", "output", 0, AV_OPT_TYPE_CONST, {.i64=OUT_MODE}, 0, 0, AF, "mode" },
83 { "n", "noise", 0, AV_OPT_TYPE_CONST, {.i64=NOISE_MODE},0, 0, AF, "mode" },
87 AVFILTER_DEFINE_CLASS(anlmdn);
89 static int query_formats(AVFilterContext *ctx)
91 AVFilterFormats *formats = NULL;
92 AVFilterChannelLayouts *layouts = NULL;
93 static const enum AVSampleFormat sample_fmts[] = {
99 formats = ff_make_format_list(sample_fmts);
101 return AVERROR(ENOMEM);
102 ret = ff_set_common_formats(ctx, formats);
106 layouts = ff_all_channel_counts();
108 return AVERROR(ENOMEM);
110 ret = ff_set_common_channel_layouts(ctx, layouts);
114 formats = ff_all_samplerates();
115 return ff_set_common_samplerates(ctx, formats);
118 static float compute_distance_ssd_c(const float *f1, const float *f2, ptrdiff_t K)
122 for (int k = -K; k <= K; k++)
123 distance += SQR(f1[k] - f2[k]);
128 static void compute_cache_c(float *cache, const float *f,
129 ptrdiff_t S, ptrdiff_t K,
130 ptrdiff_t i, ptrdiff_t jj)
134 for (int j = jj; j < jj + S; j++, v++)
135 cache[v] += -SQR(f[i - K - 1] - f[j - K - 1]) + SQR(f[i + K] - f[j + K]);
138 void ff_anlmdn_init(AudioNLMDNDSPContext *dsp)
140 dsp->compute_distance_ssd = compute_distance_ssd_c;
141 dsp->compute_cache = compute_cache_c;
144 ff_anlmdn_init_x86(dsp);
147 static int config_output(AVFilterLink *outlink)
149 AVFilterContext *ctx = outlink->src;
150 AudioNLMeansContext *s = ctx->priv;
153 s->K = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
154 s->S = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE);
157 s->pts = AV_NOPTS_VALUE;
159 s->N = s->H + (s->K + s->S) * 2;
161 av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", s->K, s->S, s->H, s->N);
163 av_frame_free(&s->in);
164 av_frame_free(&s->cache);
165 s->in = ff_get_audio_buffer(outlink, s->N);
167 return AVERROR(ENOMEM);
169 s->cache = ff_get_audio_buffer(outlink, s->S * 2);
171 return AVERROR(ENOMEM);
173 s->fifo = av_audio_fifo_alloc(outlink->format, outlink->channels, s->N);
175 return AVERROR(ENOMEM);
177 ret = av_audio_fifo_write(s->fifo, (void **)s->in->extended_data, s->K + s->S);
181 s->pdiff_lut_scale = 1.f / MAX_DIFF * WEIGHT_LUT_SIZE;
182 for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
183 float w = -i / s->pdiff_lut_scale;
185 s->weight_lut[i] = expf(w);
188 ff_anlmdn_init(&s->dsp);
193 static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
195 AudioNLMeansContext *s = ctx->priv;
199 const int om = s->om;
200 const float *f = (const float *)(s->in->extended_data[ch]) + K;
201 float *cache = (float *)s->cache->extended_data[ch];
202 const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
203 float *dst = (float *)out->extended_data[ch] + s->offset;
205 for (int i = S; i < s->H + S; i++) {
206 float P = 0.f, Q = 0.f;
210 for (int j = i - S; j <= i + S; j++) {
213 cache[v++] = s->dsp.compute_distance_ssd(f + i, f + j, K);
216 s->dsp.compute_cache(cache, f, S, K, i, i - S);
217 s->dsp.compute_cache(cache + S, f, S, K, i, i + 1);
220 for (int j = 0; j < 2 * S && !ctx->is_disabled; j++) {
221 const float distance = cache[j];
222 unsigned weight_lut_idx;
225 av_assert2(distance >= 0.f);
229 weight_lut_idx = w * s->pdiff_lut_scale;
230 av_assert2(weight_lut_idx < WEIGHT_LUT_SIZE);
231 w = s->weight_lut[weight_lut_idx];
232 P += w * f[i - S + j + (j >= S)];
240 case IN_MODE: dst[i - S] = f[i]; break;
241 case OUT_MODE: dst[i - S] = P / Q; break;
242 case NOISE_MODE: dst[i - S] = f[i] - (P / Q); break;
249 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
251 AVFilterContext *ctx = inlink->dst;
252 AVFilterLink *outlink = ctx->outputs[0];
253 AudioNLMeansContext *s = ctx->priv;
255 int available, wanted, ret;
257 if (s->pts == AV_NOPTS_VALUE)
260 ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
265 available = av_audio_fifo_size(s->fifo);
266 wanted = (available / s->H) * s->H;
268 if (wanted >= s->H && available >= s->N) {
269 out = ff_get_audio_buffer(outlink, wanted);
271 return AVERROR(ENOMEM);
274 while (available >= s->N) {
275 ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data, s->N);
279 ctx->internal->execute(ctx, filter_channel, out, NULL, inlink->channels);
281 av_audio_fifo_drain(s->fifo, s->H);
289 out->nb_samples = s->offset;
290 if (s->eof_left >= 0) {
291 out->nb_samples = FFMIN(s->eof_left, s->offset);
292 s->eof_left -= out->nb_samples;
296 return ff_filter_frame(outlink, out);
302 static int request_frame(AVFilterLink *outlink)
304 AVFilterContext *ctx = outlink->src;
305 AudioNLMeansContext *s = ctx->priv;
308 ret = ff_request_frame(ctx->inputs[0]);
310 if (ret == AVERROR_EOF && s->eof_left != 0) {
314 s->eof_left = av_audio_fifo_size(s->fifo) - (s->S + s->K);
317 in = ff_get_audio_buffer(outlink, s->H);
319 return AVERROR(ENOMEM);
321 return filter_frame(ctx->inputs[0], in);
327 static av_cold void uninit(AVFilterContext *ctx)
329 AudioNLMeansContext *s = ctx->priv;
331 av_audio_fifo_free(s->fifo);
332 av_frame_free(&s->in);
333 av_frame_free(&s->cache);
336 static const AVFilterPad inputs[] = {
339 .type = AVMEDIA_TYPE_AUDIO,
340 .filter_frame = filter_frame,
345 static const AVFilterPad outputs[] = {
348 .type = AVMEDIA_TYPE_AUDIO,
349 .config_props = config_output,
350 .request_frame = request_frame,
355 AVFilter ff_af_anlmdn = {
357 .description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."),
358 .query_formats = query_formats,
359 .priv_size = sizeof(AudioNLMeansContext),
360 .priv_class = &anlmdn_class,
364 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL |
365 AVFILTER_FLAG_SLICE_THREADS,