From patchwork Fri Oct 4 08:04:46 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: 15498 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 1645044A562 for ; Fri, 4 Oct 2019 11:11:58 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DBCEE68813A; Fri, 4 Oct 2019 11:11:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 62F56680A1D for ; Fri, 4 Oct 2019 11:11:51 +0300 (EEST) Received: by mail-wm1-f43.google.com with SMTP id p7so4786517wmp.4 for ; Fri, 04 Oct 2019 01:11:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=jDWRrJCNySUpH343u63QlpmPHPSb4cDFQM/oOOXJ+OM=; b=ngZwBZtT5OnnQhlvxbmWjj3zMKruhHlTWbIrRvdTh+TCZfvdmQV3R1eGme3gqvelqZ brxCmAJn1LDJZeZoVQc6ri+2lLFXPqkV3dFeb3u1dqgZGXXMvxqol7TQqt/1Pl/pU7dY iPF73G0aGBY11fmxMizAL+iORXnFQ0UZciCPhbxyLOcrGEh8xycpqffrpH+wEVGiPQzU eCsO3nXuH5UabWDqm8E5bo/6/9nEshs27Z4CdYunS+G7YV4U71DS2yx+mqEiAETKrDKm 87jEpdgqBULLcdgz9F45ZQ4XMMV3r1W3pQmxKNL5woMjTUHTGPjBPKTeJZ/grn+c5NbH BtSA== 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=jDWRrJCNySUpH343u63QlpmPHPSb4cDFQM/oOOXJ+OM=; b=rYWRkw8Vs3q7sMyfi0H1yk0CWSYX8HWPocMSvZuu8K6JXHUYoQQ+ryyPCSS+GFuTsq iIwh6Eqr6U3gRMbjm3j1Hq8+rPT01xfMPrUmT9TgxG1hHyI0Ovw1jIEYPmo8vBE/IxdQ OL0DPm6rondbkh4vBQtHn1jLwwESNvSOK/YpQoT9ZwS+nOSFSsvKn27DzhDbLGaDOStz lXeEHVpDmFuUREW43vvs+rnyiJWmHOERj9sSwQYPo7Jg15fWnyrWfdUnzxES09in0ye2 dTZcaw+EOamtjjwVgIvHQGeY6GteKMsL/xYJdTsUkWTyV8tvNdDu4XM9yycwrIPIRboe R+hw== X-Gm-Message-State: APjAAAUVSVheE6CtGXKS5BRaZRjhy/api0Zio0bLai22mh/iahL9BLrV zILvpX8lslNkwmv+jAjTv/cZ8INq X-Google-Smtp-Source: APXvYqyDQBdR6twt0yhyVVK/Urt4hYpnaN2H6lITfcovU9ilC0DhD39cMsZXtfNfscvMzyI64JMO7Q== X-Received: by 2002:a1c:1d84:: with SMTP id d126mr9542313wmd.58.1570176294320; Fri, 04 Oct 2019 01:04:54 -0700 (PDT) Received: from localhost.localdomain ([109.227.36.151]) by smtp.gmail.com with ESMTPSA id t13sm14572404wra.70.2019.10.04.01.04.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Oct 2019 01:04:53 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Fri, 4 Oct 2019 10:04:46 +0200 Message-Id: <20191004080446.32102-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add anlms 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_anlms.c | 308 +++++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 4 files changed, 333 insertions(+) create mode 100644 libavfilter/af_anlms.c diff --git a/doc/filters.texi b/doc/filters.texi index fbc3a404dd..49884fe16e 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -1814,6 +1814,29 @@ Change output mode. Syntax for the command is : "i", "o" or "n" string. @end table +@section anlms +Apply Normalized Least-Mean-Squares algorithm to first audio stream using second audio stream. + +This is adaptive filter used to mimic a desired filter by finding the filter coefficients that +relate to producing the least mean square of the error signal (difference between the desired, +2nd input audio stream and the actual signal, 1st input audio stream). + +A description of the accepted options follows. + +@table @option +@item order +Set filter order. + +@item mu +Set filter mu. + +@item eps +Set the filter eps. + +@item leakage +Set the filter leakage. +@end table + @section anull Pass the audio source unchanged to the output. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 182fe9df4b..16bb8cd965 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -63,6 +63,7 @@ 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_ANLMS_FILTER) += af_anlms.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_anlms.c b/libavfilter/af_anlms.c new file mode 100644 index 0000000000..571c0313e1 --- /dev/null +++ b/libavfilter/af_anlms.c @@ -0,0 +1,308 @@ +/* + * 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 "libavutil/avassert.h" +#include "libavutil/channel_layout.h" +#include "libavutil/common.h" +#include "libavutil/float_dsp.h" +#include "libavutil/opt.h" + +#include "audio.h" +#include "avfilter.h" +#include "formats.h" +#include "filters.h" +#include "internal.h" + +typedef struct AudioNLMSContext { + const AVClass *class; + + int order; + float mu; + float eps; + float leakage; + + int kernel_size; + AVFrame *offset; + AVFrame *delay; + AVFrame *coeffs; + AVFrame *tmp; + + AVFrame *frame[2]; + + AVFloatDSPContext *fdsp; +} AudioNLMSContext; + +#define OFFSET(x) offsetof(AudioNLMSContext, x) +#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption anlms_options[] = { + { "order", "set the filter order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=256}, 1, INT16_MAX, A }, + { "mu", "set the filter mu", OFFSET(mu), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, A }, + { "eps", "set the filter eps", OFFSET(eps), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, 1, A }, + { "leakage", "set the filter leakage", OFFSET(leakage), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, A }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(anlms); + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats; + AVFilterChannelLayouts *layouts; + static const enum AVSampleFormat sample_fmts[] = { + AV_SAMPLE_FMT_FLTP, + AV_SAMPLE_FMT_NONE + }; + int 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_make_format_list(sample_fmts); + if (!formats) + return AVERROR(ENOMEM); + ret = ff_set_common_formats(ctx, formats); + if (ret < 0) + return ret; + + formats = ff_all_samplerates(); + if (!formats) + return AVERROR(ENOMEM); + return ff_set_common_samplerates(ctx, formats); +} + +static float fir_sample(AudioNLMSContext *s, float sample, float *delay, + float *coeffs, float *tmp, int *offset) +{ + const int order = s->order; + float output; + + delay[*offset] = sample; + + memcpy(tmp, coeffs + order - *offset, order * sizeof(float)); + + output = s->fdsp->scalarproduct_float(delay, tmp, s->kernel_size); + + if (--(*offset) < 0) + *offset = order - 1; + + return output; +} + +static float process_sample(AudioNLMSContext *s, float input, float desired, + float *delay, float *coeffs, float *tmp, int *offsetp) +{ + const int order = s->order; + const float leakage = s->leakage; + const float mu = s->mu; + const float a = 1.f - leakage * mu; + float sum, y, e, norm, b; + int offset = *offsetp; + + delay[offset + order] = input; + + y = fir_sample(s, input, delay, coeffs, tmp, offsetp); + e = desired - y; + + sum = s->fdsp->scalarproduct_float(delay, delay, s->kernel_size); + + norm = s->eps + sum; + b = mu * e / norm; + + memcpy(tmp, delay + offset, order * sizeof(float)); + + s->fdsp->vector_fmul_scalar(coeffs, coeffs, a, s->kernel_size); + + s->fdsp->vector_fmac_scalar(coeffs, tmp, b, s->kernel_size); + + memcpy(coeffs + order, coeffs, order * sizeof(float)); + + return y; +} + +static int process_channels(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + AudioNLMSContext *s = ctx->priv; + AVFrame *out = arg; + const int start = (out->channels * jobnr) / nb_jobs; + const int end = (out->channels * (jobnr+1)) / nb_jobs; + + for (int c = start; c < end; c++) { + const float *input = (const float *)s->frame[0]->extended_data[c]; + const float *desired = (const float *)s->frame[1]->extended_data[c]; + float *delay = (float *)s->delay->extended_data[c]; + float *coeffs = (float *)s->coeffs->extended_data[c]; + float *tmp = (float *)s->tmp->extended_data[c]; + int *offset = (int *)s->offset->extended_data[c]; + float *output = (float *)out->extended_data[c]; + + for (int n = 0; n < out->nb_samples; n++) + output[n] = process_sample(s, input[n], desired[n], delay, coeffs, tmp, offset); + } + + return 0; +} + +static int activate(AVFilterContext *ctx) +{ + AudioNLMSContext *s = ctx->priv; + int i, ret, status; + int nb_samples; + int64_t pts; + + FF_FILTER_FORWARD_STATUS_BACK_ALL(ctx->outputs[0], ctx); + + nb_samples = FFMIN(ff_inlink_queued_samples(ctx->inputs[0]), + ff_inlink_queued_samples(ctx->inputs[1])); + for (i = 0; i < ctx->nb_inputs && nb_samples > 0; i++) { + if (s->frame[i]) + continue; + + if (ff_inlink_check_available_samples(ctx->inputs[i], nb_samples) > 0) { + ret = ff_inlink_consume_samples(ctx->inputs[i], nb_samples, nb_samples, &s->frame[i]); + if (ret < 0) + return ret; + } + } + + if (s->frame[0] && s->frame[1]) { + AVFrame *out; + + out = ff_get_audio_buffer(ctx->outputs[0], s->frame[0]->nb_samples); + if (!out) { + av_frame_free(&s->frame[0]); + av_frame_free(&s->frame[1]); + return AVERROR(ENOMEM); + } + + ctx->internal->execute(ctx, process_channels, out, NULL, FFMIN(ctx->outputs[0]->channels, + ff_filter_get_nb_threads(ctx))); + + out->pts = s->frame[0]->pts; + + av_frame_free(&s->frame[0]); + av_frame_free(&s->frame[1]); + + ret = ff_filter_frame(ctx->outputs[0], out); + if (ret < 0) + return ret; + } + + if (!nb_samples) { + for (i = 0; i < 2; i++) { + if (ff_inlink_acknowledge_status(ctx->inputs[i], &status, &pts)) { + ff_outlink_set_status(ctx->outputs[0], status, pts); + return 0; + } + } + } + + if (ff_outlink_frame_wanted(ctx->outputs[0])) { + for (i = 0; i < 2; i++) { + if (ff_inlink_queued_samples(ctx->inputs[i]) > 0) + continue; + ff_inlink_request_frame(ctx->inputs[i]); + return 0; + } + } + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + AudioNLMSContext *s = ctx->priv; + + s->kernel_size = FFALIGN(s->order, 16); + + if (!s->offset) + s->offset = ff_get_audio_buffer(outlink, 1); + if (!s->delay) + s->delay = ff_get_audio_buffer(outlink, 2 * s->kernel_size); + if (!s->coeffs) + s->coeffs = ff_get_audio_buffer(outlink, 2 * s->kernel_size); + if (!s->tmp) + s->tmp = ff_get_audio_buffer(outlink, s->kernel_size); + if (!s->delay || !s->coeffs || !s->offset || !s->tmp) + return AVERROR(ENOMEM); + + return 0; +} + +static av_cold int init(AVFilterContext *ctx) +{ + AudioNLMSContext *s = ctx->priv; + + s->fdsp = avpriv_float_dsp_alloc(0); + if (!s->fdsp) + return AVERROR(ENOMEM); + + return 0; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + AudioNLMSContext *s = ctx->priv; + + av_freep(&s->fdsp); + av_frame_free(&s->delay); + av_frame_free(&s->coeffs); + av_frame_free(&s->offset); + av_frame_free(&s->tmp); +} + +static const AVFilterPad inputs[] = { + { + .name = "input", + .type = AVMEDIA_TYPE_AUDIO, + }, + { + .name = "desired", + .type = AVMEDIA_TYPE_AUDIO, + }, + { NULL } +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_output, + }, + { NULL } +}; + +AVFilter ff_af_anlms = { + .name = "anlms", + .description = NULL_IF_CONFIG_SMALL("Apply Normalized Least-Mean-Squares algorithm to first audio stream."), + .priv_size = sizeof(AudioNLMSContext), + .priv_class = &anlms_class, + .init = init, + .uninit = uninit, + .activate = activate, + .query_formats = query_formats, + .inputs = inputs, + .outputs = outputs, + .flags = AVFILTER_FLAG_SLICE_THREADS, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 1a26129069..4f8b3039ed 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -56,6 +56,7 @@ extern AVFilter ff_af_amix; extern AVFilter ff_af_amultiply; extern AVFilter ff_af_anequalizer; extern AVFilter ff_af_anlmdn; +extern AVFilter ff_af_anlms; extern AVFilter ff_af_anull; extern AVFilter ff_af_apad; extern AVFilter ff_af_aperms;