From patchwork Sun Jan 6 13:16:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 11663 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id D563444C2A9 for ; Sun, 6 Jan 2019 15:24:26 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8F2DB68A54D; Sun, 6 Jan 2019 15:24:23 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DBD6A680626 for ; Sun, 6 Jan 2019 15:24:16 +0200 (EET) Received: by mail-wr1-f65.google.com with SMTP id t27so40628134wra.6 for ; Sun, 06 Jan 2019 05:24:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=t0FCMtPyjpNUuGVlmOloHgBMcl/3QU3a6xqEftFUVM4=; b=K27VuqvfbAP6k8s2BU7UFui3htDs+tctqOeBkPHJP4H5O/vCD7sv17YPp2yAoXvnLs hkr8TKdBGfpZGE7QrlZYUPeWO1oa6dsImFImWxtXhTXiYY7un2ssK/AduwACqEY+AIaY A5wgcqrQ6R6L8CzVGn2zO0D8V63dz1pmeuiiOvmWCED9jb/HxJ6KjDF3hBb/h6JMYD26 3/PnZFwH1PpP8UyuQcrtAT+C6Ycor1wK/h2RcrfkVfXzUCMTo4TZEfvTXFRhTaTzLIrg cAj1ZtI3PhfiiuzGT1hGvnQaSRchjh/MAfaly2/XjtzhLx4HHYyqniIPgfJ7XTljTfiF clIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=t0FCMtPyjpNUuGVlmOloHgBMcl/3QU3a6xqEftFUVM4=; b=ADB/AWkEMlB7mzCiRN+hTtzl62bZQ4Z/v/9IdHANtpAnjolpc4CcIPs3eec+UytxjZ 6coc/umKtsSg5aAjAHhhkHrewNd40eSpX/e0qzwn6ny5nSLs/k7NPFAJtg5Hoa8xstxx xCwMnPRi4oKs/202NmKRVDtva6vsxs7lJihoME6VbTGD2frQfz1rRzqmc8FOXurIWlIU ENaqGG2PUK40BbFycmWHLQ5rqI8DP5+P7FD+/AsdU6j7Rl9eZCp+UrSmi/uOnoyQa0qq IdhUw186Gkzj24us3c0nKu03SmFMhcYU+w41RlRnI7yddqNaIDO/T3YRsArx2oTcjYho T2Ug== X-Gm-Message-State: AJcUukeXiSbTgfAOd85Dx66oGBpJs+3V9q9e+9JbVPwy6df/y1Xs4z4V zu1dUWjmD0veg6d1BEqoPzDeyXja X-Google-Smtp-Source: ALg8bN4MvgAHj0sd5yNv6qDltcfsS1kN7e/kr91g7hi4x//wZxBQoHL3OFHb8iQGLynlyue6ZRHRUQ== X-Received: by 2002:adf:ee46:: with SMTP id w6mr50844624wro.261.1546780634045; Sun, 06 Jan 2019 05:17:14 -0800 (PST) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id y8sm5061985wmg.13.2019.01.06.05.17.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 06 Jan 2019 05:17:13 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 6 Jan 2019 14:16:59 +0100 Message-Id: <20190106131659.21716-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add anlmdn audio filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- doc/filters.texi | 23 ++++ libavfilter/Makefile | 1 + libavfilter/af_anlmdn.c | 249 +++++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 4 files changed, 274 insertions(+) create mode 100644 libavfilter/af_anlmdn.c diff --git a/doc/filters.texi b/doc/filters.texi index 98858c5356..4031d1ef9a 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -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 in number of samples. Allowed range is from 0 to 4096. +Default value is 32. + +@item r +Set research radius in number of samples. Allowed range is from 1 to 4096. +Default value is 200. +@end table + @section anull Pass the audio source unchanged to the output. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 6e2658186d..d31fa59e0d 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -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 diff --git a/libavfilter/af_anlmdn.c b/libavfilter/af_anlmdn.c new file mode 100644 index 0000000000..3eaea68dfa --- /dev/null +++ b/libavfilter/af_anlmdn.c @@ -0,0 +1,249 @@ +/* + * 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 + +#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; + int K; + int S; + + int N; + int hop_size; + + 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 radius", OFFSET(K), AV_OPT_TYPE_INT, {.i64=32}, 0, 4096, AF }, + { "r", "set research radius", OFFSET(S), AV_OPT_TYPE_INT, {.i64=200}, 1, 4096, 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->pts = AV_NOPTS_VALUE; + s->N = s->K * 2 + 1 + (s->K + s->S) * 2; + s->hop_size = s->K * 2 + 1; + + 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 + 1); + 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]; + + for (int i = S; i < s->hop_size + S; i++) { + float P = 0.f, Q = 0.f; + int v = 0; + + for (int j = i - S; j <= i + S; j++, v++) { + float w, distance; + + if (i == j) { + distance = 0.f; + } else if (i == S) { + cache[v] = distance = s->compute_distance(f + i, f + j, K); + } else { + distance = cache[v] = cache[v] - SQR(f[i - K - 1] - f[j - K - 1]) + SQR(f[i + K] - f[j + K]); + } + + av_assert0(distance >= 0.f); + w = expf(-distance * sw); + P += w * f[j]; + Q += w; + } + + 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 ret = 0; + + 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); + + while (av_audio_fifo_size(s->fifo) >= s->N) { + out = ff_get_audio_buffer(outlink, s->hop_size); + if (!out) + return AVERROR(ENOMEM); + + 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->hop_size); + + out->pts = s->pts; + s->pts += s->hop_size; + + ret = ff_filter_frame(outlink, out); + if (ret < 0) + break; + } + + if (ret < 0) + av_frame_free(&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, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index a600069500..c8cff6e435 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -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;