mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2024-12-23 12:43:46 +02:00
avfilter: add anlmdn audio filter
Signed-off-by: Paul B Mahol <onemda@gmail.com>
This commit is contained in:
parent
10931a0661
commit
8a1fc95840
@ -13,6 +13,7 @@ version <next>:
|
||||
- GIF parser
|
||||
- vividas demuxer
|
||||
- hymt decoder
|
||||
- anlmdn filter
|
||||
|
||||
|
||||
version 4.1:
|
||||
|
@ -1750,6 +1750,29 @@ Full filter invocation with asendcmd may look like this:
|
||||
asendcmd=c='4.0 anequalizer change 0|f=200|w=50|g=1',anequalizer=...
|
||||
@end table
|
||||
|
||||
@section anlmdn
|
||||
|
||||
Reduce broadband noise in audio samples using Non-Local Means algorithm.
|
||||
|
||||
Each sample is adjusted by looking for other samples with similar contexts. This
|
||||
context similarity is defined by comparing their surrounding patches of size
|
||||
@option{p}. Patches are searched in an area of @option{r} around the sample.
|
||||
|
||||
The filter accepts the following options.
|
||||
|
||||
@table @option
|
||||
@item s
|
||||
Set denoising strength. Allowed range is from 1 to 9999. Default value is 1.
|
||||
|
||||
@item p
|
||||
Set patch radius duration. Allowed range is from 1 to 100 milliseconds.
|
||||
Default value is 2 milliseconds.
|
||||
|
||||
@item r
|
||||
Set research radius duration. Allowed range is from 2 to 300 milliseconds.
|
||||
Default value is 6 milliseconds.
|
||||
@end table
|
||||
|
||||
@section anull
|
||||
|
||||
Pass the audio source unchanged to the output.
|
||||
|
@ -63,6 +63,7 @@ OBJS-$(CONFIG_AMETADATA_FILTER) += f_metadata.o
|
||||
OBJS-$(CONFIG_AMIX_FILTER) += af_amix.o
|
||||
OBJS-$(CONFIG_AMULTIPLY_FILTER) += af_amultiply.o
|
||||
OBJS-$(CONFIG_ANEQUALIZER_FILTER) += af_anequalizer.o
|
||||
OBJS-$(CONFIG_ANLMDN_FILTER) += af_anlmdn.o
|
||||
OBJS-$(CONFIG_ANULL_FILTER) += af_anull.o
|
||||
OBJS-$(CONFIG_APAD_FILTER) += af_apad.o
|
||||
OBJS-$(CONFIG_APERMS_FILTER) += f_perms.o
|
||||
|
271
libavfilter/af_anlmdn.c
Normal file
271
libavfilter/af_anlmdn.c
Normal file
@ -0,0 +1,271 @@
|
||||
/*
|
||||
* 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 <float.h>
|
||||
|
||||
#include "libavutil/avassert.h"
|
||||
#include "libavutil/audio_fifo.h"
|
||||
#include "libavutil/opt.h"
|
||||
#include "avfilter.h"
|
||||
#include "audio.h"
|
||||
#include "formats.h"
|
||||
|
||||
#define SQR(x) ((x) * (x))
|
||||
|
||||
typedef struct AudioNLMeansContext {
|
||||
const AVClass *class;
|
||||
|
||||
float a;
|
||||
int64_t pd;
|
||||
int64_t rd;
|
||||
|
||||
int K;
|
||||
int S;
|
||||
int N;
|
||||
int H;
|
||||
|
||||
int offset;
|
||||
AVFrame *in;
|
||||
AVFrame *cache;
|
||||
|
||||
int64_t pts;
|
||||
|
||||
AVAudioFifo *fifo;
|
||||
|
||||
float (*compute_distance)(const float *f1, const float *f2, int K);
|
||||
} AudioNLMeansContext;
|
||||
|
||||
#define OFFSET(x) offsetof(AudioNLMeansContext, x)
|
||||
#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
||||
|
||||
static const AVOption anlmdn_options[] = {
|
||||
{ "s", "set denoising strength", OFFSET(a), AV_OPT_TYPE_FLOAT, {.dbl=1}, 1, 9999, AF },
|
||||
{ "p", "set patch duration", OFFSET(pd), AV_OPT_TYPE_DURATION, {.i64=2000}, 1000, 100000, AF },
|
||||
{ "r", "set research duration", OFFSET(rd), AV_OPT_TYPE_DURATION, {.i64=6000}, 2000, 300000, AF },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
AVFILTER_DEFINE_CLASS(anlmdn);
|
||||
|
||||
static int query_formats(AVFilterContext *ctx)
|
||||
{
|
||||
AVFilterFormats *formats = NULL;
|
||||
AVFilterChannelLayouts *layouts = NULL;
|
||||
static const enum AVSampleFormat sample_fmts[] = {
|
||||
AV_SAMPLE_FMT_FLTP,
|
||||
AV_SAMPLE_FMT_NONE
|
||||
};
|
||||
int ret;
|
||||
|
||||
formats = ff_make_format_list(sample_fmts);
|
||||
if (!formats)
|
||||
return AVERROR(ENOMEM);
|
||||
ret = ff_set_common_formats(ctx, formats);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
layouts = ff_all_channel_counts();
|
||||
if (!layouts)
|
||||
return AVERROR(ENOMEM);
|
||||
|
||||
ret = ff_set_common_channel_layouts(ctx, layouts);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
formats = ff_all_samplerates();
|
||||
return ff_set_common_samplerates(ctx, formats);
|
||||
}
|
||||
|
||||
static float compute_distance_ssd(const float *f1, const float *f2, int K)
|
||||
{
|
||||
float distance = 0.;
|
||||
|
||||
for (int k = -K; k <= K; k++)
|
||||
distance += SQR(f1[k] - f2[k]);
|
||||
|
||||
return distance;
|
||||
}
|
||||
|
||||
static int config_output(AVFilterLink *outlink)
|
||||
{
|
||||
AVFilterContext *ctx = outlink->src;
|
||||
AudioNLMeansContext *s = ctx->priv;
|
||||
|
||||
s->K = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
|
||||
s->S = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE);
|
||||
|
||||
s->pts = AV_NOPTS_VALUE;
|
||||
s->H = s->K * 2 + 1;
|
||||
s->N = s->H + (s->K + s->S) * 2;
|
||||
|
||||
av_frame_free(&s->in);
|
||||
av_frame_free(&s->cache);
|
||||
s->in = ff_get_audio_buffer(outlink, s->N);
|
||||
if (!s->in)
|
||||
return AVERROR(ENOMEM);
|
||||
|
||||
s->cache = ff_get_audio_buffer(outlink, s->S * 2);
|
||||
if (!s->cache)
|
||||
return AVERROR(ENOMEM);
|
||||
|
||||
s->fifo = av_audio_fifo_alloc(outlink->format, outlink->channels, s->N);
|
||||
if (!s->fifo)
|
||||
return AVERROR(ENOMEM);
|
||||
|
||||
s->compute_distance = compute_distance_ssd;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
|
||||
{
|
||||
AudioNLMeansContext *s = ctx->priv;
|
||||
AVFrame *out = arg;
|
||||
const int S = s->S;
|
||||
const int K = s->K;
|
||||
const float *f = (const float *)(s->in->extended_data[ch]) + K;
|
||||
float *cache = (float *)s->cache->extended_data[ch];
|
||||
const float sw = 32768.f / s->a;
|
||||
float *dst = (float *)out->extended_data[ch] + s->offset;
|
||||
|
||||
for (int i = S; i < s->H + S; i++) {
|
||||
float P = 0.f, Q = 0.f;
|
||||
int v = 0;
|
||||
|
||||
if (i == S) {
|
||||
for (int j = i - S; j <= i + S; j++) {
|
||||
if (i == j)
|
||||
continue;
|
||||
cache[v++] = s->compute_distance(f + i, f + j, K);
|
||||
}
|
||||
} else {
|
||||
for (int j = i - S; j < i; j++, v++)
|
||||
cache[v] = cache[v] - SQR(f[i - K - 1] - f[j - K - 1]) + SQR(f[i + K] - f[j + K]);
|
||||
|
||||
for (int j = i + 1; j <= i + S; j++, v++)
|
||||
cache[v] = cache[v] - SQR(f[i - K - 1] - f[j - K - 1]) + SQR(f[i + K] - f[j + K]);
|
||||
}
|
||||
|
||||
for (int j = 0; j < v; j++) {
|
||||
const float distance = cache[j];
|
||||
float w;
|
||||
|
||||
av_assert0(distance >= 0.f);
|
||||
w = expf(-distance * sw);
|
||||
P += w * f[i - S + j + (j >= S)];
|
||||
Q += w;
|
||||
}
|
||||
|
||||
P += f[i];
|
||||
Q += 1;
|
||||
|
||||
dst[i - S] = P / Q;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
||||
{
|
||||
AVFilterContext *ctx = inlink->dst;
|
||||
AVFilterLink *outlink = ctx->outputs[0];
|
||||
AudioNLMeansContext *s = ctx->priv;
|
||||
AVFrame *out = NULL;
|
||||
int available, wanted, ret;
|
||||
|
||||
if (s->pts == AV_NOPTS_VALUE)
|
||||
s->pts = in->pts;
|
||||
|
||||
ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
|
||||
in->nb_samples);
|
||||
av_frame_free(&in);
|
||||
|
||||
s->offset = 0;
|
||||
available = av_audio_fifo_size(s->fifo);
|
||||
wanted = (available / s->H) * s->H;
|
||||
|
||||
if (wanted >= s->H && available >= s->N) {
|
||||
out = ff_get_audio_buffer(outlink, wanted);
|
||||
if (!out)
|
||||
return AVERROR(ENOMEM);
|
||||
}
|
||||
|
||||
while (available >= s->N) {
|
||||
ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data, s->N);
|
||||
if (ret < 0)
|
||||
break;
|
||||
|
||||
ctx->internal->execute(ctx, filter_channel, out, NULL, inlink->channels);
|
||||
|
||||
av_audio_fifo_drain(s->fifo, s->H);
|
||||
|
||||
s->offset += s->H;
|
||||
available -= s->H;
|
||||
}
|
||||
|
||||
if (out) {
|
||||
out->pts = s->pts;
|
||||
out->nb_samples = s->offset;
|
||||
s->pts += s->offset;
|
||||
|
||||
return ff_filter_frame(outlink, out);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static av_cold void uninit(AVFilterContext *ctx)
|
||||
{
|
||||
AudioNLMeansContext *s = ctx->priv;
|
||||
|
||||
av_audio_fifo_free(s->fifo);
|
||||
av_frame_free(&s->in);
|
||||
av_frame_free(&s->cache);
|
||||
}
|
||||
|
||||
static const AVFilterPad inputs[] = {
|
||||
{
|
||||
.name = "default",
|
||||
.type = AVMEDIA_TYPE_AUDIO,
|
||||
.filter_frame = filter_frame,
|
||||
},
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
static const AVFilterPad outputs[] = {
|
||||
{
|
||||
.name = "default",
|
||||
.type = AVMEDIA_TYPE_AUDIO,
|
||||
.config_props = config_output,
|
||||
},
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
AVFilter ff_af_anlmdn = {
|
||||
.name = "anlmdn",
|
||||
.description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."),
|
||||
.query_formats = query_formats,
|
||||
.priv_size = sizeof(AudioNLMeansContext),
|
||||
.priv_class = &anlmdn_class,
|
||||
.uninit = uninit,
|
||||
.inputs = inputs,
|
||||
.outputs = outputs,
|
||||
.flags = AVFILTER_FLAG_SLICE_THREADS,
|
||||
};
|
@ -55,6 +55,7 @@ extern AVFilter ff_af_ametadata;
|
||||
extern AVFilter ff_af_amix;
|
||||
extern AVFilter ff_af_amultiply;
|
||||
extern AVFilter ff_af_anequalizer;
|
||||
extern AVFilter ff_af_anlmdn;
|
||||
extern AVFilter ff_af_anull;
|
||||
extern AVFilter ff_af_apad;
|
||||
extern AVFilter ff_af_aperms;
|
||||
|
@ -30,8 +30,8 @@
|
||||
#include "libavutil/version.h"
|
||||
|
||||
#define LIBAVFILTER_VERSION_MAJOR 7
|
||||
#define LIBAVFILTER_VERSION_MINOR 46
|
||||
#define LIBAVFILTER_VERSION_MICRO 101
|
||||
#define LIBAVFILTER_VERSION_MINOR 47
|
||||
#define LIBAVFILTER_VERSION_MICRO 100
|
||||
|
||||
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
|
||||
LIBAVFILTER_VERSION_MINOR, \
|
||||
|
Loading…
Reference in New Issue
Block a user