From patchwork Mon Apr 27 18:05:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 19296 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 87760448F42 for ; Mon, 27 Apr 2020 21:05:48 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 624B968BCBE; Mon, 27 Apr 2020 21:05:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F2BF368BC5E for ; Mon, 27 Apr 2020 21:05:41 +0300 (EEST) Received: by mail-wr1-f47.google.com with SMTP id d15so20024420wrx.3 for ; Mon, 27 Apr 2020 11:05:41 -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=0mAn5RqGC0jnEOmhD8KqRitDNUCsM+60/C51uovCgIo=; b=E7jY6YjVu0t6GHXBec3sp9FV5CVZADgMygam/6YAfoukJJhf18WXYOdkCJ8qRK7Lh+ Vyk1U4j0mNjlN25skg1krC0M3y4lTsPNjbJgOw/3gCjrhIzEglS8SkGtfYhe7j/ZO0k+ I7OkjoAcx4Fy2HSN4W3Vxvw4TmTjPVYYckVU9KZNLpYNi2aGsRiGP55plsJBdsilCi5T oxO8HejgCSbVMOtpsNzhqOAR7efVsViakxGAA2eMJfJsWStFvsUo3VWpSvUyMaHbIyS0 64mElr0P5Z8NvS+tgFv3GuA0WChMU7sHOw5zpvD3hlJ8zkZXaayv9Y+PmVDxsQWNCQmc v9gg== 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=0mAn5RqGC0jnEOmhD8KqRitDNUCsM+60/C51uovCgIo=; b=YM43PG7R0JdVPKNcGDyHz3gs1gUTMzFpZMSu7pskqFxP+PeNgt4k1r9XDvKfDuJZ+N 4/Z251ngrMK9ZeWPOWm7jL3uhmO//Mm10Qt7oBHX8mhpA850dgC7Vm/dFhWVWElFfBt+ o61PFOW18ZC/Gea+gpYqSDbHtDvbc2RsWqsVva8+seVyP9Z/Pe6KJ3MllgbIVyqBFLzu yMlygYfi7BVb9uHGy/ZJlDVe/IvyqSnn29UFCKMRMDPyUXIMr6fYZ/Ao9McBG7MHgRlz kOFOvmPmLfSzJG5DK/aKt64zCky3eH9VvZMgoSB6DYCXbW9ssTwJG+8XPcGf0/7aakK4 7aUQ== X-Gm-Message-State: AGi0PuYi9NaRQXA9HzHU8HH0qg4MmttkB6kYIUiGVaNkh18GAyeTCgNh svXF0JR9u3VKjgtKCYf65dipT8Xk X-Google-Smtp-Source: APiQypLDZBo92IuH+gShtLFLxyAOuoHmpbqBAfm/VN1MowBgXSjk+zkqW0oPZZJiMUVEAR4CSjGqqw== X-Received: by 2002:adf:e8c2:: with SMTP id k2mr28406397wrn.396.1588010740768; Mon, 27 Apr 2020 11:05:40 -0700 (PDT) Received: from localhost.localdomain ([77.237.109.227]) by smtp.gmail.com with ESMTPSA id t16sm22600121wrb.8.2020.04.27.11.05.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2020 11:05:40 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Apr 2020 20:05:32 +0200 Message-Id: <20200427180532.2044-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add asubboost 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 | 31 ++++++ libavfilter/Makefile | 1 + libavfilter/af_asubboost.c | 210 +++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 4 files changed, 243 insertions(+) create mode 100644 libavfilter/af_asubboost.c diff --git a/doc/filters.texi b/doc/filters.texi index 71a6787289..18c8528da7 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -2446,6 +2446,37 @@ Number of points where the waveform crosses the zero level axis. Rate of Zero crossings and number of audio samples. @end table +@section asubboost +Boost subwoofer frequencies. + +The filter accepts the following options: + +@table @option +@item dry +Set dry gain, how much of original signal is kept. Allowed range is from 0 to 1. +Default value is 0.5. + +@item wet +Set wet gain, how much of filtered signal is kept. Allowed range is from 0 to 1. +Default value is 0.8. + +@item decay +Set delay line decay gain value. Allowed range is from 0 to 1. +Default value is 0.7. + +@item feedback +Set delay line feedback gain value. Allowed range is from 0 to 1. +Default value is 0.5. + +@item cutoff +Set cutoff frequenciy in herz. Allowed range is 50 to 250. +Default value is 100. + +@item delay +Set delay. Allowed range is from 1 to 100. +Default value is 20. +@end table + @section atempo Adjust audio tempo. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index e1205eb063..f982afe15f 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -86,6 +86,7 @@ OBJS-$(CONFIG_ASPLIT_FILTER) += split.o OBJS-$(CONFIG_ASR_FILTER) += af_asr.o OBJS-$(CONFIG_ASTATS_FILTER) += af_astats.o OBJS-$(CONFIG_ASTREAMSELECT_FILTER) += f_streamselect.o framesync.o +OBJS-$(CONFIG_ASUBBOOST_FILTER) += af_asubboost.o OBJS-$(CONFIG_ATEMPO_FILTER) += af_atempo.o OBJS-$(CONFIG_ATRIM_FILTER) += trim.o OBJS-$(CONFIG_AXCORRELATE_FILTER) += af_axcorrelate.o diff --git a/libavfilter/af_asubboost.c b/libavfilter/af_asubboost.c new file mode 100644 index 0000000000..37ed853687 --- /dev/null +++ b/libavfilter/af_asubboost.c @@ -0,0 +1,210 @@ +/* + * 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/channel_layout.h" +#include "libavutil/ffmath.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "audio.h" +#include "formats.h" + +typedef struct ASubBoostContext { + const AVClass *class; + + double dry_gain; + double wet_gain; + double feedback; + double decay; + double delay; + double cutoff; + + double a0, a1, a2; + double b0, b1, b2; + + int write_pos; + int buffer_samples; + + AVFrame *i, *o; + AVFrame *buffer; +} ASubBoostContext; + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterFormats *formats = NULL; + AVFilterChannelLayouts *layouts = NULL; + static const enum AVSampleFormat sample_fmts[] = { + AV_SAMPLE_FMT_DBLP, + 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 int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + ASubBoostContext *s = ctx->priv; + + double w0 = 2 * M_PI * s->cutoff / inlink->sample_rate; + double alpha = sin(w0) / 2 * sqrt(2 * (1 / 0.5 - 1) + 2); + + s->a0 = 1 + alpha; + s->a1 = -2 * cos(w0); + s->a2 = 1 - alpha; + s->b0 = (1 - cos(w0)) / 2; + s->b1 = 1 - cos(w0); + s->b2 = (1 - cos(w0)) / 2; + + s->a1 /= s->a0; + s->a2 /= s->a0; + s->b0 /= s->a0; + s->b1 /= s->a0; + s->b2 /= s->a0; + + s->buffer_samples = inlink->sample_rate * s->delay / 1000; + + s->buffer = ff_get_audio_buffer(inlink, s->buffer_samples); + s->i = ff_get_audio_buffer(inlink, 2); + s->o = ff_get_audio_buffer(inlink, 2); + if (!s->buffer || !s->i || !s->o) + return AVERROR(ENOMEM); + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + ASubBoostContext *s = ctx->priv; + const float wet = s->wet_gain, dry = s->dry_gain, feedback = s->feedback, decay = s->decay; + int write_pos; + 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 < in->channels; ch++) { + const double *src = (const double *)in->extended_data[ch]; + double *dst = (double *)out->extended_data[ch]; + double *buffer = (double *)s->buffer->extended_data[ch]; + double *ix = (double *)s->i->extended_data[ch]; + double *ox = (double *)s->o->extended_data[ch]; + + write_pos = s->write_pos; + for (int n = 0; n < in->nb_samples; n++) { + double out_sample; + + out_sample = src[n] * s->b0 + ix[0] * s->b1 + ix[1] * s->b2 - ox[0] * s->a1 - ox[1] * s->a2; + ix[1] = ix[0]; + ix[0] = src[n]; + ox[1] = ox[0]; + ox[0] = out_sample; + + buffer[write_pos] = buffer[write_pos] * decay + out_sample * feedback; + dst[n] = src[n] * dry + buffer[write_pos] * wet; + + if (++write_pos >= s->buffer_samples) + write_pos = 0; + } + } + + s->write_pos = write_pos; + + if (out != in) + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + ASubBoostContext *s = ctx->priv; + + av_frame_free(&s->buffer); + av_frame_free(&s->i); + av_frame_free(&s->o); +} + +#define OFFSET(x) offsetof(ASubBoostContext, x) +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption asubboost_options[] = { + { "dry", "set dry gain", OFFSET(dry_gain), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, FLAGS }, + { "wet", "set wet gain", OFFSET(wet_gain), AV_OPT_TYPE_DOUBLE, {.dbl=0.8}, 0, 1, FLAGS }, + { "decay", "set decay", OFFSET(decay), AV_OPT_TYPE_DOUBLE, {.dbl=0.7}, 0, 1, FLAGS }, + { "feedback", "set feedback", OFFSET(feedback), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, FLAGS }, + { "cutoff", "set cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=100}, 50, 250, FLAGS }, + { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=20}, 1, 100, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(asubboost); + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + }, + { NULL } +}; + +AVFilter ff_af_asubboost = { + .name = "asubboost", + .description = NULL_IF_CONFIG_SMALL("Boost subwoofer frequencies."), + .query_formats = query_formats, + .priv_size = sizeof(ASubBoostContext), + .priv_class = &asubboost_class, + .uninit = uninit, + .inputs = inputs, + .outputs = outputs, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 6354375e91..1b94501da0 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -79,6 +79,7 @@ extern AVFilter ff_af_asplit; extern AVFilter ff_af_asr; extern AVFilter ff_af_astats; extern AVFilter ff_af_astreamselect; +extern AVFilter ff_af_asubboost; extern AVFilter ff_af_atempo; extern AVFilter ff_af_atrim; extern AVFilter ff_af_axcorrelate;