From patchwork Thu Nov 25 19:31:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 31706 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:d206:0:0:0:0:0 with SMTP id q6csp1239397iob; Thu, 25 Nov 2021 11:31:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJws6PgAqOrvc1hW9NyoBQM4h9m//xvuIMvprxpyab8KmPnuN971iuhu4+iKH7fQCia84y+N X-Received: by 2002:a17:907:9156:: with SMTP id l22mr33188227ejs.220.1637868693105; Thu, 25 Nov 2021 11:31:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1637868693; cv=none; d=google.com; s=arc-20160816; b=T44ptfPgGqnZuWlDP7cKl9wiOhbVFXaemz/shTVGFQPecdno8K5MP8dQ9TnATGFAkP vawmgaBq6neKv6d+tfc5Qi8u0i0uwdBtCg1SlJNpQMSMzWme+Itj1AJ/HmL54oBDmirX dRCveJEuIZs+Oe2gTkPgVFs9T3TEFdMlX2XwL1Gre+xKKg3/VQHGrX4jS5ZPhumgbYq8 kkAVt/o4BCEXmalGomE0NmCsd1Zq/Apxl0cbVcTs+3hmmNID4WMwRjXC+0/OW5weJXoI 9TmaILGByaHUnvIPjLRNpadb0mCWpG/lS2sHuiXEETbL/QcnfjNPpYiThvWW+IVy+KZs 0m/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=6YNQMAnztZLEVeIj6FY5vGjjNL6hOqAJRJdc1rVMYbo=; b=WYCUVjpTctWQkxAUYw5Q2nnJv5CUx/noFWKjCzzMHWsP6FeFO3VUDZdHeDZDlGG6Tp OqMrl7i+12cVwTtpPEMs66hSYQ50NPYpB2Wq4+bZVJ0UC4G8FIyO5ReZfCrXB0JfZZDt hi6BYiLKvus7dXyBgbaNTgWbjlsSuByUfXs9u+iojE2CDgDJEutic1WuTKqmZs1y5WvR ZydDZNrlJfHbNwVAYG1Oq///QsHKzpCmHz4A4ZRw9z1ArlrHzpgWL0Gxa6AN2kQlojrN 550wySF5osgbTf9BJ+NOjzCMpoOlMmpn8TbhyV7XAuJaVkIZgO7Dg8dAdNx8XWcUQKRz 1+yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Kirv4q8J; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id j10si12629322edw.318.2021.11.25.11.31.31; Thu, 25 Nov 2021 11:31:33 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Kirv4q8J; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CFF4668AD86; Thu, 25 Nov 2021 21:31:29 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E4498680226 for ; Thu, 25 Nov 2021 21:31:22 +0200 (EET) Received: by mail-ed1-f43.google.com with SMTP id z5so29720350edd.3 for ; Thu, 25 Nov 2021 11:31:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=FOSJvy+tX04fXwEgolPEp/wAYoMr/BARYfNJO+rwjDU=; b=Kirv4q8JPiGlDpCeGznuzTDhSsEXVV0SlxiPhVIb1YncL21GhzaHoMEisj9W0eCtft ETdFJpb3ClytCPWmxbzdcgUcjb7UyPaiPxw2A5mxwtvrKVsUdh0YmpiTTesaI7KaW68Q EMpwcHy7PnmHURJASnFbm/3hSoYF+3OQ1F84UmopCVfXucoTA1Lk+VUg+Olx2OqSlE8H SvMoft4j/pWgjKaFGnxU9lVafOf3wMKGBsiDvYmkC5zfIJ0ElTtpXCMF0xQtM1D+vNYh G/5q8OgcPIJc/tYlEuqnidjkZ2htZqj4LrJ1GXYDMa7OQ4CpF+Lj3xcn4Es9vYNG62mk S0Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=FOSJvy+tX04fXwEgolPEp/wAYoMr/BARYfNJO+rwjDU=; b=1z4HA287lqvGpwlKQNDDjU0n2rgyywMN8veFg+9ryQSRGmbNuVlvezGsQB3L43WZal X0ZntDHoZawkQdgTn6nEyprXCbL1tmvRLINAivzqNVsyS9IZuYUSjEo8goAu5sPNgCj1 2uRfezWZIeNfTr94PFLVb/iNzDbJFAzSubJqsIHBwA4ViptaCT/QzhIo6QX4Bl4V8zdR 6rMuUjLAgu4BkN4+8+D7szyb7CG8YXyhJQCNoxYFRYAUcnxO4oUXrXtlrSFXfk7Qaf46 ZXxmUg4uvlVZCeHw222P6hz9jZKyGsnWAKj8RvdCWkK575fMvU7T4zVAtSPqCFzOY2Ww 5cUQ== X-Gm-Message-State: AOAM5323Ato4/0f3hHiaaWpno2AAyKmG3jJ1x37lZ/CBWdjHCeo4rprf exXpObUGWnB6xgt2ad7iQVFik6fV4Mo= X-Received: by 2002:a17:906:a08d:: with SMTP id q13mr33949547ejy.465.1637868682190; Thu, 25 Nov 2021 11:31:22 -0800 (PST) Received: from localhost.localdomain ([95.168.120.41]) by smtp.gmail.com with ESMTPSA id l18sm1854545ejo.114.2021.11.25.11.31.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Nov 2021 11:31:21 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Nov 2021 20:31:25 +0100 Message-Id: <20211125193125.601497-1-onemda@gmail.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avfilter: add audio dynamic smooth filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: sum9cnYGNyMN Signed-off-by: Paul B Mahol --- doc/filters.texi | 20 +++++ libavfilter/Makefile | 1 + libavfilter/af_adynamicsmooth.c | 142 ++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 4 files changed, 164 insertions(+) create mode 100644 libavfilter/af_adynamicsmooth.c diff --git a/doc/filters.texi b/doc/filters.texi index 04cbf4231d..0347e66d0c 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -843,6 +843,26 @@ Compute derivative/integral of audio stream. Applying both filters one after another produces original audio. +@section adynamicsmooth + +Apply dynamic smoothing to input audio stream. + +A description of the accepted options follows. + +@table @option +@item sensitivity +Set an amount of sensitivity to frequency fluctations. Default is 2. +Allowed range is from 0 to 1e+06. + +@item basefreq +Set a base frequency for smoothing. Default value is 22050. +Allowed range is from 2 to 1e+06. +@end table + +@subsection Commands + +This filter supports the all above options as @ref{commands}. + @section aecho Apply echoing to the input audio. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 551d13aadc..c8082c4a2f 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -44,6 +44,7 @@ OBJS-$(CONFIG_ADECORRELATE_FILTER) += af_adecorrelate.o OBJS-$(CONFIG_ADELAY_FILTER) += af_adelay.o OBJS-$(CONFIG_ADENORM_FILTER) += af_adenorm.o OBJS-$(CONFIG_ADERIVATIVE_FILTER) += af_aderivative.o +OBJS-$(CONFIG_ADYNAMICSMOOTH_FILTER) += af_adynamicsmooth.o OBJS-$(CONFIG_AECHO_FILTER) += af_aecho.o OBJS-$(CONFIG_AEMPHASIS_FILTER) += af_aemphasis.o OBJS-$(CONFIG_AEVAL_FILTER) += aeval.o diff --git a/libavfilter/af_adynamicsmooth.c b/libavfilter/af_adynamicsmooth.c new file mode 100644 index 0000000000..4e00fecc6a --- /dev/null +++ b/libavfilter/af_adynamicsmooth.c @@ -0,0 +1,142 @@ +/* + * 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/ffmath.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "audio.h" +#include "formats.h" + +typedef struct AudioDynamicSmoothContext { + const AVClass *class; + + double sensitivity; + double basefreq; + + AVFrame *coeffs; +} AudioDynamicSmoothContext; + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + AudioDynamicSmoothContext *s = ctx->priv; + + s->coeffs = ff_get_audio_buffer(inlink, 3); + if (!s->coeffs) + return AVERROR(ENOMEM); + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + AudioDynamicSmoothContext *s = ctx->priv; + const double sensitivity = s->sensitivity; + const double wc = s->basefreq / in->sample_rate; + AVFrame *out; + + if (av_frame_is_writable(in)) { + out = in; + } else { + out = ff_get_audio_buffer(outlink, in->nb_samples); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, in); + } + + for (int ch = 0; ch < out->channels; ch++) { + const double *src = (const double *)in->extended_data[ch]; + double *dst = (double *)out->extended_data[ch]; + double *coeffs = (double *)s->coeffs->extended_data[ch]; + double low1 = coeffs[0]; + double low2 = coeffs[1]; + double inz = coeffs[2]; + + for (int n = 0; n < out->nb_samples; n++) { + double low1z = low1; + double low2z = low2; + double bandz = low2z - low1z; + double wd = wc + sensitivity * fabs(bandz); + double g = fmin(1., wd * (5.9948827 + wd * (-11.969296 + wd * 15.959062))); + + low1 = low1z + g * (0.5 * (src[n] + inz) - low1z); + low2 = low2z + g * (0.5 * (low1 + low1z) - low2z); + inz = src[n]; + dst[n] = ctx->is_disabled ? src[n] : low2; + } + + coeffs[0] = low1; + coeffs[1] = low2; + coeffs[2] = inz; + } + + if (out != in) + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + AudioDynamicSmoothContext *s = ctx->priv; + + av_frame_free(&s->coeffs); +} + +#define OFFSET(x) offsetof(AudioDynamicSmoothContext, x) +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM + +static const AVOption adynamicsmooth_options[] = { + { "sensitivity", "set smooth sensitivity", OFFSET(sensitivity), AV_OPT_TYPE_DOUBLE, {.dbl=2}, 0, 1000000, FLAGS }, + { "basefreq", "set base frequency", OFFSET(basefreq), AV_OPT_TYPE_DOUBLE, {.dbl=22050}, 2, 1000000, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(adynamicsmooth); + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_frame = filter_frame, + .config_props = config_input, + }, +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + }, +}; + +const AVFilter ff_af_adynamicsmooth = { + .name = "adynamicsmooth", + .description = NULL_IF_CONFIG_SMALL("Apply Dynamic Smoothing of input audio."), + .priv_size = sizeof(AudioDynamicSmoothContext), + .priv_class = &adynamicsmooth_class, + .uninit = uninit, + FILTER_INPUTS(inputs), + FILTER_OUTPUTS(outputs), + FILTER_SINGLE_SAMPLEFMT(AV_SAMPLE_FMT_DBLP), + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, + .process_command = ff_filter_process_command, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 00c36c3f63..c5c0e9b28b 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -37,6 +37,7 @@ extern const AVFilter ff_af_adecorrelate; extern const AVFilter ff_af_adelay; extern const AVFilter ff_af_adenorm; extern const AVFilter ff_af_aderivative; +extern const AVFilter ff_af_adynamicsmooth; extern const AVFilter ff_af_aecho; extern const AVFilter ff_af_aemphasis; extern const AVFilter ff_af_aeval;