From patchwork Fri Jun 10 10:09:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 36140 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:6914:b0:82:6b11:2509 with SMTP id q20csp852933pzj; Fri, 10 Jun 2022 03:06:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/8PpRLBydl3h6wsYncLjKU0RPDJ0BGhWBRRSMW4EbLclXEgI1tKlF31OT2wO/BRazBpE7 X-Received: by 2002:aa7:c492:0:b0:42d:ed6a:26e5 with SMTP id m18-20020aa7c492000000b0042ded6a26e5mr49539840edq.64.1654855618558; Fri, 10 Jun 2022 03:06:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654855618; cv=none; d=google.com; s=arc-20160816; b=gvDz4bO90ZfrrdAW3J+2l/OVmcvAg5E2Hk7cUGVkn2I8sDl+ZxJ/00SCfvGFt813u6 9hzWFxgXi7sz/eRQYJBe2y+IJAF+8CUtbKHHBlK9Ft4KlDcymH9EyjmXdrpXgMUM6mLf tznAei99pkC9T6fhXVO1DsT9U1XYOvnjG6qImro12SYef+oDR685xIEHQ9uc1blNWlOk NBPFhqIqtQadyVBt7fGRDL2K6no6FXXlNJ+HtZDmX/sjeYt++bOXOSyj4M1e/psp8D3M 7DNBx77lceM44hZiDHF/Jht0LWI9BwkHCAYMB9uNHUU01o1h86fO22Gj/Xa8kTuD/8KO kfSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :message-id:date:from:mime-version:dkim-signature:delivered-to; bh=FKdOyrlBT2Ir6wxQ78TD0MGAiget21Yg8NOADzLfoOo=; b=Gx5mvQfEXZcKLYe5lIPOtBHcvZz6OyLxbs2QNg0fs0A6WZDhLevaotYyIoTV96SkcY 0fYolAthEzAigdEXp3BpDLbmzOyzdRCozVg5AVwpYDZVe1vbGCkiA4lBpNCExbjZzjZF hYJv24n0QhW9K09h1R9vR4MIdkZs6Gt9ZrVZY8WmRnMu96xf29+jzGzmJjgAJuK5lkK+ Ij7Q4yX132HmxFX+CSRcMkFs19tQZppYR3ejcVS1DbyW3Z4hGHIIWqJyEb0nCyolj9Zb mDM/uRvGbeaOkeUwZ5BHMMp+jEjGmYhwaqsVKAEeHujLrhQdghMrys/8XxxrDsUhAQs2 c6Cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=RF3eAPd4; 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 i16-20020a1709064fd000b006fe8b1baec0si20081465ejw.709.2022.06.10.03.06.57; Fri, 10 Jun 2022 03:06:58 -0700 (PDT) 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=RF3eAPd4; 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 0730B68B4DF; Fri, 10 Jun 2022 13:06:54 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id ED5C768B6A4 for ; Fri, 10 Jun 2022 13:06:46 +0300 (EEST) Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-30fa61b1a83so261792427b3.0 for ; Fri, 10 Jun 2022 03:06:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=SKs+39T9rQ0QEUUa43Gn3y+/Fyf+VuI/ElfJgbjxe9Y=; b=RF3eAPd45LpED4otf+tLWVJi3ARpqyTAq4ELow4ONFhZiM1V1dStO6i7HmdQgvWgf8 1FHj6wF7O4eKSQ4qq0YxAiXA7eoGKQ59rkhml+FxF8jE/mv2A7ezYwwovhmOL2tAj4C5 RgntMd7yn8h3aaIIrOjxdFNVA0xTxIFLdFwAthy6GK9Ql4Uef5ogo3mHZomeaD/uEjN6 bD3TQcTfxm+SV1cShsW1Zb5kuWRN0C0h35krwd0f6X/idCWh9Bno6KwoObQ8SWXDC2T8 AcevZc9iqi5otHutx2nhGMkXf1nqdRLR4fb3zjE24QQh1GG6DoMDVEBrET26q1FnePcQ 5Qvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=SKs+39T9rQ0QEUUa43Gn3y+/Fyf+VuI/ElfJgbjxe9Y=; b=4E9olmEaVjUo35dSVmSHT4GkUN4GxmYMBKw7jT3uQwwm7+RxLSB/QJzhmykfMwBCCq +jIsGeLmGf5ZKNSFYTr1X75XPku8LS6aeDfcyw13KDcBGNP30+CKyHke3GyfqtyMtESr uB8Wf7bO9oZKO15AgwwyfPsnGIkZjsiPfxywP96gyLu4pFBiN+xfyfGgqN9wK+6chP+Z eNn2wz9KkuvO3TIRz9hsK1t/Gsi3sJbq32rd/XvyjQznKJMQ6gdhvHokCEYA2zm61Uft W8ucChgQR/p2xGbQ6cG+cGD2xqhkozi/32aA4XVFZXv4XeeHQ8Y/p3+4sVii2WQnVWV+ f94g== X-Gm-Message-State: AOAM531VMfLS8tatt/Tjoo1wsnDfwu8qbvGBUrLELhHXSmPvcXn1HTm1 /50fIWvHTEJGpMAMHfVweAzIw2dE8TjagKgCdl69cQ2u5OE= X-Received: by 2002:a81:598a:0:b0:30c:cdd5:f2fd with SMTP id n132-20020a81598a000000b0030ccdd5f2fdmr44539643ywb.177.1654855605486; Fri, 10 Jun 2022 03:06:45 -0700 (PDT) MIME-Version: 1.0 From: Paul B Mahol Date: Fri, 10 Jun 2022 12:09:38 +0200 Message-ID: To: FFmpeg development discussions and patches X-Content-Filtered-By: Mailman/MimeDel 2.1.29 Subject: [FFmpeg-devel] [PATCH] avfilter: add virtualbass 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: ynuQsty60enE Hi, Patch attached. From 68b1edf19c441a59fd53c0286eee841244cf5f11 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Mon, 30 May 2022 11:36:22 +0200 Subject: [PATCH] avfilter: add virtualbass filter Signed-off-by: Paul B Mahol --- doc/filters.texi | 20 ++++ libavfilter/Makefile | 1 + libavfilter/af_virtualbass.c | 183 +++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 4 files changed, 205 insertions(+) create mode 100644 libavfilter/af_virtualbass.c diff --git a/doc/filters.texi b/doc/filters.texi index d65e83d4d0..5abab5de18 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -6927,6 +6927,26 @@ Depth of modulation as a percentage. Range is 0.0 - 1.0. Default value is 0.5. @end table +@section virtualbass + +Apply audio Virtual Bass filter. + +This filter accepts stereo input and produce stereo with LFE (2.1) channels output. +The newly produced LFE channel have enhanced virtual bass originally obtained from both stereo channels. +This filter outputs front left and front right channels unchanged as available in stereo input. + +The filter accepts the following options: + +@table @option +@item cutoff +Set the virtual bass cutoff frequency. Default value is 250 Hz. +Allowed range is from 100 to 500 Hz. + +@item strength +Set the virtual bass strength. Allowed range is from 0.5 to 3. +Default value is 3. +@end table + @section volume Adjust the input audio volume. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index e0e4d0de2c..7ba1c8a861 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -164,6 +164,7 @@ OBJS-$(CONFIG_SURROUND_FILTER) += af_surround.o OBJS-$(CONFIG_TREBLE_FILTER) += af_biquads.o OBJS-$(CONFIG_TREMOLO_FILTER) += af_tremolo.o OBJS-$(CONFIG_VIBRATO_FILTER) += af_vibrato.o generate_wave_table.o +OBJS-$(CONFIG_VIRTUALBASS_FILTER) += af_virtualbass.o OBJS-$(CONFIG_VOLUME_FILTER) += af_volume.o OBJS-$(CONFIG_VOLUMEDETECT_FILTER) += af_volumedetect.o diff --git a/libavfilter/af_virtualbass.c b/libavfilter/af_virtualbass.c new file mode 100644 index 0000000000..950159096b --- /dev/null +++ b/libavfilter/af_virtualbass.c @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2022 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/channel_layout.h" +#include "libavutil/opt.h" +#include "audio.h" +#include "avfilter.h" +#include "filters.h" +#include "internal.h" + +#include + +typedef struct AudioVirtualBassContext { + const AVClass *class; + + double cutoff; + double strength; + + double a[3], m[3], cf[2]; +} AudioVirtualBassContext; + +#define OFFSET(x) offsetof(AudioVirtualBassContext, x) +#define TFLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM +#define FLAGS AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption virtualbass_options[] = { + { "cutoff", "set virtual bass cutoff", OFFSET(cutoff), AV_OPT_TYPE_DOUBLE, {.dbl=250},100,500, FLAGS }, + { "strength", "set virtual bass strength", OFFSET(strength), AV_OPT_TYPE_DOUBLE, {.dbl=3}, 0.5, 3, TFLAGS }, + {NULL} +}; + +AVFILTER_DEFINE_CLASS(virtualbass); + +static int query_formats(AVFilterContext *ctx) +{ + AVFilterChannelLayouts *in_layout = NULL, *out_layout = NULL; + AVFilterFormats *formats = NULL; + int ret; + + if ((ret = ff_add_format (&formats, AV_SAMPLE_FMT_DBLP )) < 0 || + (ret = ff_set_common_formats (ctx, formats )) < 0 || + (ret = ff_add_channel_layout (&in_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) < 0 || + (ret = ff_channel_layouts_ref(in_layout, &ctx->inputs[0]->outcfg.channel_layouts)) < 0 || + (ret = ff_add_channel_layout (&out_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2POINT1)) < 0 || + (ret = ff_channel_layouts_ref(out_layout, &ctx->outputs[0]->incfg.channel_layouts)) < 0) + return ret; + + return ff_set_common_all_samplerates(ctx); +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + AudioVirtualBassContext *s = ctx->priv; + const double Q = 0.707; + double g, k; + + g = tan(M_PI * s->cutoff / inlink->sample_rate); + k = 1. / Q; + s->a[0] = 1. / (1. + g * (g + k)); + s->a[1] = g * s->a[0]; + s->a[2] = g * s->a[1]; + s->m[0] = 0.; + s->m[1] = 0.; + s->m[2] = 1.; + + return 0; +} + +#define SQR(x) ((x) * (x)) + +static double vb_fun(double x) +{ + double y = 2.5 * atan(0.9 * x) + 2.5 * sqrt(1. - SQR(0.9 * x)) - 2.5; + + return y < 0. ? sin(y) : y; +} + +static void vb_stereo(AVFilterContext *ctx, AVFrame *out, AVFrame *in) +{ + AudioVirtualBassContext *s = ctx->priv; + const double *lsrc = (const double *)in->extended_data[0]; + const double *rsrc = (const double *)in->extended_data[1]; + double *ldst = (double *)out->extended_data[0]; + double *rdst = (double *)out->extended_data[1]; + double *lfe = (double *)out->extended_data[2]; + const double st = M_PI / s->strength; + const double a0 = s->a[0]; + const double a1 = s->a[1]; + const double a2 = s->a[2]; + const double m0 = s->m[0]; + const double m1 = s->m[1]; + const double m2 = s->m[2]; + double b0 = s->cf[0]; + double b1 = s->cf[1]; + + memcpy(ldst, lsrc, in->nb_samples * sizeof(double)); + memcpy(rdst, rsrc, in->nb_samples * sizeof(double)); + + for (int n = 0; n < in->nb_samples; n++) { + const double center = (lsrc[n] + rsrc[n]) * 0.5; + const double v0 = center; + const double v3 = v0 - b1; + const double v1 = a0 * b0 + a1 * v3; + const double v2 = b1 + a1 * b0 + a2 * v3; + double b, vb; + + b0 = 2. * v1 - b0; + b1 = 2. * v2 - b1; + + b = m0 * v0 + m1 * v1 + m2 * v2; + vb = sin(vb_fun(b) * st); + + lfe[n] = vb; + } + + s->cf[0] = b0; + s->cf[1] = b1; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out; + + out = ff_get_audio_buffer(outlink, in->nb_samples); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + + vb_stereo(ctx, out, in); + + out->pts = in->pts; + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +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_virtualbass = { + .name = "virtualbass", + .description = NULL_IF_CONFIG_SMALL("Audio Virtual Bass."), + .priv_size = sizeof(AudioVirtualBassContext), + .priv_class = &virtualbass_class, + FILTER_INPUTS(inputs), + FILTER_OUTPUTS(outputs), + FILTER_QUERY_FUNC(query_formats), + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, + .process_command = ff_filter_process_command, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 2f72477523..ec70feef11 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -153,6 +153,7 @@ extern const AVFilter ff_af_tiltshelf; extern const AVFilter ff_af_treble; extern const AVFilter ff_af_tremolo; extern const AVFilter ff_af_vibrato; +extern const AVFilter ff_af_virtualbass; extern const AVFilter ff_af_volume; extern const AVFilter ff_af_volumedetect; -- 2.36.1