From patchwork Sun Jan 15 09:48:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 40005 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1d43:b0:b8:6755:f97e with SMTP id cs3csp251188pzb; Sun, 15 Jan 2023 01:48:20 -0800 (PST) X-Google-Smtp-Source: AMrXdXulwYi8DnQRgwB3Me40xkpegohoBUGPX+Mg1JTvyhYPuLtvOGy1FcNwdX1s055JerEP9ur7 X-Received: by 2002:a17:907:76f2:b0:869:236c:ac43 with SMTP id kg18-20020a17090776f200b00869236cac43mr10337427ejc.32.1673776100019; Sun, 15 Jan 2023 01:48:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673776100; cv=none; d=google.com; s=arc-20160816; b=YKnEV+srQDGdMCMtTftL6/hIH98loC+/XblWL+UTyTUBZzS3qjd1aSZDHR9u2LXbpS +Kvg4SNZZq6pOA3oHQTlxQG8qNZ8NtY3pmrdeD2K1wVGnD9EN8RQrB4Z/kiBNV+Dl5H7 4XdSGHFRzfUXgWUSNZ7YsJJb9eIaHPiczFTyboAdxL/kQ6PN7CWDtb+dX0VZ4EqL4C0M OMBjACLN7ek0peLA6TnpC8G/8HH2uGE3Nfo23XKagDYSjTc93wkn2P3oByz+pzvw0uU0 AZJkn1OkDe7tUboE0aQg2BfcU0kd07dttarnJX7n6+MWvGivUUy6XJ2eheGlsxC0eMCs pb/A== 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=MLQ1eTininkOLL7yHIG1hwPBjq+rBzzoTNyznNdQlNE=; b=paTszRV7CIrfV+onKK4eDXrrXB8yUOpAuMzyfoHjdq38Ww/6JDY69jMAbaaavyUGzL DRG2+zf/xll50m/IQCK8f6EJNYBb+IIFngB1eLeeX3cTRJAxYPHI5Q5HaLxi6d5mvYQ1 LogPix7vdxQjDPynin6vhfAHYK4k13URZTVyZ0dkc7W9hy9h6evH21ySStCjWPvIgnG8 6x4Zmo1hyqpk5I6GlU0B2SdrBXoDMBtIqNLemlo90pqsyLPm9N9ziAYmi+1V1SLZSlNx KT+k5EArL1hDVjSjGk+Tk+eoSqvJpxz3NqwoFWmNW2YCI6Q2DDmogr7MxpEKchRPbv6F y5Zw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=kge04kAl; 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 i15-20020a1709064fcf00b007ac60b82ea5si3885583ejw.96.2023.01.15.01.48.19; Sun, 15 Jan 2023 01:48:19 -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=kge04kAl; 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 ACF6568BBF4; Sun, 15 Jan 2023 11:48:16 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-vk1-f177.google.com (mail-vk1-f177.google.com [209.85.221.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3C38A68B35D for ; Sun, 15 Jan 2023 11:48:10 +0200 (EET) Received: by mail-vk1-f177.google.com with SMTP id c21so3949867vkn.10 for ; Sun, 15 Jan 2023 01:48:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=b3o5Y+/RFkN4HTOL/uhBTVRyViJLHjfAbUnmbyyOfuk=; b=kge04kAlF7VchPhnQ8C4cwKH/jmyYVjEdm29rmJduSbwtpxETLYPkE5vumka/9oDAJ MNWhKqsO7CmLrthWnmYpBFC8qVsQXfk0NHR3DOe2E2Mw5AA10NBez7pM6cWkTWHQWdRK 7Zz3ZfqPXuLU2l7ZdO77p4zIDNyO9IzA38LF8jpi58h7AcmtpUnx5TQgKeVSZuf5YRgi 1zPtv9hCu9+AgBCsuy2f7PQnAS/Z7W51mwpn+WZGBc7EbW9V+6yl4NQvmEed7MlkwBtZ Lm71+vYQgHvQkDBGpLImsasRx44sVAr/V9frYYa26ruhVllZG5eMS4LljExNp7kCx7RZ HnoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=b3o5Y+/RFkN4HTOL/uhBTVRyViJLHjfAbUnmbyyOfuk=; b=YI/oD5vQ2YTaDXlsWrvf5DIcO4eGzTY5uQLBcrpUpRyasclBNDzihCkcGcG0O5Sfi0 aFLTQWWRV1WQpkn7+dG6c8pG9zr/kAPBpw0Ay9fNq8jh55nr+RpXxPrczw7iKBqTl+Qc Gf4SygLlo8jXP4fzyL+IEGDOZfwFuJ/DmpfKKIhduXEpwZHG36EcAnvIXoJn5o0uQWFj QViROHfgy1MHyfmph4E7IokJO0rkEk8NT9ABFNiw5lAroN8ixWOWesLgEKk4zTUCr8mI fpE/3inRSA7uq0wG78/woqQsl2mD9entZWf7+hoYQpjTtJMr503f3DosIDOI51J1MgT6 MZQA== X-Gm-Message-State: AFqh2kqjylAmybPzV+mo0W6bnYsHdl38K7Xs1rwYN7kD66kBwRrdTNXI GCSqpYlp3SDTWp9rTY5jdgXOEKjXZ04V9OxMqHDuiMYP X-Received: by 2002:a1f:414e:0:b0:3d9:98d3:3902 with SMTP id o75-20020a1f414e000000b003d998d33902mr4645522vka.39.1673776088236; Sun, 15 Jan 2023 01:48:08 -0800 (PST) MIME-Version: 1.0 Received: by 2002:a05:612c:2428:b0:32a:5eaa:92e4 with HTTP; Sun, 15 Jan 2023 01:48:07 -0800 (PST) From: Paul B Mahol Date: Sun, 15 Jan 2023 10:48:07 +0100 Message-ID: To: FFmpeg development discussions and patches Subject: [FFmpeg-devel] [PATCH] avfilter: add FIR equalizer coefficients source 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: F5VqrLiZodhh Patch attached From 5e22b9bc08f7015853cc93a07e165155601b87fe Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Fri, 13 Jan 2023 13:32:26 +0100 Subject: [PATCH] avfilter: add FIR equalizer coefficients source filter Signed-off-by: Paul B Mahol --- doc/filters.texi | 61 +++++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/asrc_afirsrc.c | 247 ++++++++++++++++++++++++++++++++++++- 4 files changed, 308 insertions(+), 2 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index be70a2396b..c86f44d294 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -7546,6 +7546,67 @@ Specifies the channel layout, and can be a string representing a channel layout. The default value of @var{channel_layout} is "stereo". @end table +@section afireqsrc + +Generate a FIR equalizer coefficients. + +The resulting stream can be used with @ref{afir} filter for filtering the audio signal. + +The filter accepts the following options: + +@table @option +@item preset, p +Set equalizer preset. +Default preset is @code{flat}. + +Available presets are: +@table @samp +@item custom +@item flat +@item acoustic +@item bass +@item beats +@item classic +@item clear +@item deep bass +@item dubstep +@item electronic +@item hard-style +@item hip-hop +@item jazz +@item metal +@item movie +@item pop +@item r&b +@item rock +@item vocal booster +@end table + +@item gains, g +Set custom gains for each band. Only used if the preset option is set to @code{custom}. +Gains are separated by white spaces and each gain is set in dBFS. +Default is @code{0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0}. + +@item bands, b +Set the custom bands from where custon equalizer gains are set. +This must be in strictly increasing order. Only used if the preset option is set to @code{custom}. +Bands are separated by white spaces and each band represent frequency in Hz. +Default is @code{25 40 63 100 160 250 400 630 1000 1600 2500 4000 6300 10000 16000 24000}. + +@item taps, t +Set number of filter coefficents in output audio stream. +Default value is @code{4096}. + +@item sample_rate, r +Set sample rate of output audio stream, default is @code{44100}. + +@item nb_samples, n +Set number of samples per each frame in output audio stream. Default is @code{1024}. + +@item interp, i +Set interpolation method for FIR equalizer coefficients. Can be @code{linear} or @code{cubic}. +@end table + @section afirsrc Generate a FIR coefficients using frequency sampling method. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index a9c835474e..285ef74068 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -174,6 +174,7 @@ OBJS-$(CONFIG_VOLUMEDETECT_FILTER) += af_volumedetect.o OBJS-$(CONFIG_AEVALSRC_FILTER) += aeval.o OBJS-$(CONFIG_AFDELAYSRC_FILTER) += asrc_afdelaysrc.o +OBJS-$(CONFIG_AFIREQSRC_FILTER) += asrc_afirsrc.o OBJS-$(CONFIG_AFIRSRC_FILTER) += asrc_afirsrc.o OBJS-$(CONFIG_ANOISESRC_FILTER) += asrc_anoisesrc.o OBJS-$(CONFIG_ANULLSRC_FILTER) += asrc_anullsrc.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 1c729090e3..c1faafca4a 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -162,6 +162,7 @@ extern const AVFilter ff_af_volumedetect; extern const AVFilter ff_asrc_aevalsrc; extern const AVFilter ff_asrc_afdelaysrc; +extern const AVFilter ff_asrc_afireqsrc; extern const AVFilter ff_asrc_afirsrc; extern const AVFilter ff_asrc_anoisesrc; extern const AVFilter ff_asrc_anullsrc; diff --git a/libavfilter/asrc_afirsrc.c b/libavfilter/asrc_afirsrc.c index d2ea92c41c..1bc6451565 100644 --- a/libavfilter/asrc_afirsrc.c +++ b/libavfilter/asrc_afirsrc.c @@ -18,7 +18,9 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include "libavutil/cpu.h" #include "libavutil/channel_layout.h" +#include "libavutil/ffmath.h" #include "libavutil/eval.h" #include "libavutil/opt.h" #include "libavutil/tx.h" @@ -38,6 +40,8 @@ typedef struct AudioFIRSourceContext { int sample_rate; int nb_samples; int win_func; + int preset; + int interp; AVComplexFloat *complexf; float *freq; @@ -131,7 +135,7 @@ static int parse_string(char *str, float **items, int *nb_items, int *items_size float *new_items; char *tail; - new_items = av_fast_realloc(NULL, items_size, 1 * sizeof(float)); + new_items = av_fast_realloc(NULL, items_size, sizeof(float)); if (!new_items) return AVERROR(ENOMEM); *items = new_items; @@ -142,7 +146,7 @@ static int parse_string(char *str, float **items, int *nb_items, int *items_size do { (*items)[(*nb_items)++] = av_strtod(tail, &tail); - new_items = av_fast_realloc(*items, items_size, (*nb_items + 1) * sizeof(float)); + new_items = av_fast_realloc(*items, items_size, (*nb_items + 2) * sizeof(float)); if (!new_items) return AVERROR(ENOMEM); *items = new_items; @@ -300,3 +304,242 @@ const AVFilter ff_asrc_afirsrc = { FILTER_QUERY_FUNC(query_formats), .priv_class = &afirsrc_class, }; + +#define DEFAULT_BANDS "25 40 63 100 160 250 400 630 1000 1600 2500 4000 6300 10000 16000 24000" + +typedef struct EqPreset { + char name[16]; + float gains[16]; +} EqPreset; + +static const EqPreset eq_presets[] = { + { "flat", { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, + { "acoustic", { 5.0, 4.5, 4.0, 3.5, 1.5, 1.0, 1.5, 1.5, 2.0, 3.0, 3.5, 4.0, 3.7, 3.0, 3.0 } }, + { "bass", { 10.0, 8.8, 8.5, 6.5, 2.5, 1.5, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, + { "beats", { -5.5, -5.0, -4.5, -4.2, -3.5, -3.0, -1.9, 0, 0, 0, 0, 0, 0, 0, 0 } }, + { "classic", { -0.3, 0.3, -3.5, -9.0, -1.0, 0.0, 1.8, 2.1, 0.0, 0.0, 0.0, 4.4, 9.0, 9.0, 9.0 } }, + { "clear", { 3.5, 5.5, 6.5, 9.5, 8.0, 6.5, 3.5, 2.5, 1.3, 5.0, 7.0, 9.0, 10.0, 11.0, 9.0 } }, + { "deep bass", { 12.0, 8.0, 0.0, -6.7, -12.0, -9.0, -3.5, -3.5, -6.1, 0.0, -3.0, -5.0, 0.0, 1.2, 3.0 } }, + { "dubstep", { 12.0, 10.0, 0.5, -1.0, -3.0, -5.0, -5.0, -4.8, -4.5, -2.5, -1.0, 0.0, -2.5, -2.5, 0.0 } }, + { "electronic", { 4.0, 4.0, 3.5, 1.0, 0.0, -0.5, -2.0, 0.0, 2.0, 0.0, 0.0, 1.0, 3.0, 4.0, 4.5 } }, + { "hardstyle", { 6.1, 7.0, 12.0, 6.1, -5.0, -12.0, -2.5, 3.0, 6.5, 0.0, -2.2, -4.5, -6.1, -9.2, -10.0 } }, + { "hip-hop", { 4.5, 4.3, 4.0, 2.5, 1.5, 3.0, -1.0, -1.5, -1.5, 1.5, 0.0, -1.0, 0.0, 1.5, 3.0 } }, + { "jazz", { 0.0, 0.0, 0.0, 2.0, 4.0, 5.9, -5.9, -4.5, -2.5, 2.5, 1.0, -0.8, -0.8, -0.8, -0.8 } }, + { "metal", { 10.5, 10.5, 7.5, 0.0, 2.0, 5.5, 0.0, 0.0, 0.0, 6.1, 0.0, 0.0, 6.1, 10.0, 12.0 } }, + { "movie", { 3.0, 3.0, 6.1, 8.5, 9.0, 7.0, 6.1, 6.1, 5.0, 8.0, 3.5, 3.5, 8.0, 10.0, 8.0 } }, + { "pop", { 0.0, 0.0, 0.0, 0.0, 0.0, 1.3, 2.0, 2.5, 5.0, -1.5, -2.0, -3.0, -3.0, -3.0, -3.0 } }, + { "r&b", { 3.0, 3.0, 7.0, 6.1, 4.5, 1.5, -1.5, -2.0, -1.5, 2.0, 2.5, 3.0, 3.5, 3.8, 4.0 } }, + { "rock", { 0.0, 0.0, 0.0, 3.0, 3.0, -10.0, -4.0, -1.0, 0.8, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0 } }, + { "vocal booster", { -1.5, -2.0, -3.0, -3.0, -0.5, 1.5, 3.5, 3.5, 3.5, 3.0, 2.0, 1.5, 0.0, 0.0, -1.5 } }, +}; + +static const AVOption afireqsrc_options[] = { + { "preset","set equalizer preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=0}, -1, FF_ARRAY_ELEMS(eq_presets)-1, FLAGS, "preset" }, + { "p", "set equalizer preset", OFFSET(preset), AV_OPT_TYPE_INT, {.i64=0}, -1, FF_ARRAY_ELEMS(eq_presets)-1, FLAGS, "preset" }, + { "custom", NULL, 0, AV_OPT_TYPE_CONST, {.i64=-1}, 0, 0, FLAGS, "preset" }, + { eq_presets[ 0].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 0}, 0, 0, FLAGS, "preset" }, + { eq_presets[ 1].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 1}, 0, 0, FLAGS, "preset" }, + { eq_presets[ 2].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 2}, 0, 0, FLAGS, "preset" }, + { eq_presets[ 3].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 3}, 0, 0, FLAGS, "preset" }, + { eq_presets[ 4].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 4}, 0, 0, FLAGS, "preset" }, + { eq_presets[ 5].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 5}, 0, 0, FLAGS, "preset" }, + { eq_presets[ 6].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 6}, 0, 0, FLAGS, "preset" }, + { eq_presets[ 7].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 7}, 0, 0, FLAGS, "preset" }, + { eq_presets[ 8].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 8}, 0, 0, FLAGS, "preset" }, + { eq_presets[ 9].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64= 9}, 0, 0, FLAGS, "preset" }, + { eq_presets[10].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, FLAGS, "preset" }, + { eq_presets[11].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, FLAGS, "preset" }, + { eq_presets[12].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, FLAGS, "preset" }, + { eq_presets[13].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=13}, 0, 0, FLAGS, "preset" }, + { eq_presets[14].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=14}, 0, 0, FLAGS, "preset" }, + { eq_presets[15].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=15}, 0, 0, FLAGS, "preset" }, + { eq_presets[16].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=16}, 0, 0, FLAGS, "preset" }, + { eq_presets[17].name, NULL, 0, AV_OPT_TYPE_CONST, {.i64=17}, 0, 0, FLAGS, "preset" }, + { "gains", "set gain values per band", OFFSET(magnitude_str), AV_OPT_TYPE_STRING, {.str="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"}, 0, 0, FLAGS }, + { "g", "set gain values per band", OFFSET(magnitude_str), AV_OPT_TYPE_STRING, {.str="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"}, 0, 0, FLAGS }, + { "bands", "set central frequency values per band", OFFSET(freq_points_str), AV_OPT_TYPE_STRING, {.str=DEFAULT_BANDS}, 0, 0, FLAGS }, + { "b", "set central frequency values per band", OFFSET(freq_points_str), AV_OPT_TYPE_STRING, {.str=DEFAULT_BANDS}, 0, 0, FLAGS }, + { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=4096}, 16, UINT16_MAX, FLAGS }, + { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=4096}, 16, UINT16_MAX, FLAGS }, + { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS }, + { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS }, + { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS }, + { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS }, + { "interp","set the interpolation", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "interp" }, + { "i", "set the interpolation", OFFSET(interp), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "interp" }, + { "linear", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "interp" }, + { "cubic", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "interp" }, + {NULL} +}; + +AVFILTER_DEFINE_CLASS(afireqsrc); + +static void eq_interp(AVComplexFloat *complexf, + const float *freq, + const float *magnitude, + int m, int interp, int minterp, + const float factor) +{ + complexf[0].re = magnitude[0]; + complexf[0].im = 0; + + for (int i = 1; i < minterp; i++) { + for (int j = 0; j < m; j++) { + const float x = factor * i; + + if (x <= freq[j+1]) { + float g; + + if (interp == 0) { + const float d = freq[j+1] - freq[j]; + const float d0 = x - freq[j]; + const float d1 = freq[j+1] - x; + const float g0 = magnitude[j]; + const float g1 = magnitude[j+1]; + + if (d0 && d1) { + g = (d0 * g1 + d1 * g0) / d; + } else if (d0) { + g = g1; + } else { + g = g0; + } + } else { + if (x <= freq[j]) { + g = magnitude[j]; + } else { + float x1, x2, x3; + float a, b, c, d; + float m0, m1, m2, msum; + const float unit = freq[j+1] - freq[j]; + + m0 = j != 0 ? unit * (magnitude[j] - magnitude[j-1]) / (freq[j] - freq[j-1]) : 0; + m1 = magnitude[j+1] - magnitude[j]; + m2 = j != minterp - 1 ? unit * (magnitude[j+2] - magnitude[j+1]) / (freq[j+2] - freq[j+1]) : 0; + + msum = fabsf(m0) + fabsf(m1); + m0 = msum > 0.f ? (fabsf(m0) * m1 + fabsf(m1) * m0) / msum : 0.f; + msum = fabsf(m1) + fabsf(m2); + m1 = msum > 0.f ? (fabsf(m1) * m2 + fabsf(m2) * m1) / msum : 0.f; + + d = magnitude[j]; + c = m0; + b = 3.f * magnitude[j+1] - m1 - 2.f * c - 3.f * d; + a = magnitude[j+1] - b - c - d; + + x1 = (x - freq[j]) / unit; + x2 = x1 * x1; + x3 = x2 * x1; + + g = a * x3 + b * x2 + c * x1 + d; + } + } + + complexf[i].re = g; + complexf[i].im = 0; + complexf[minterp * 2 - i].re = g; + complexf[minterp * 2 - i].im = 0; + + break; + } + } + } +} + +static av_cold int config_eq_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + AudioFIRSourceContext *s = ctx->priv; + int fft_size, middle, asize, ret; + float scale, factor; + + s->nb_freq = s->nb_magnitude = 0; + if (s->preset < 0) { + ret = parse_string(s->freq_points_str, &s->freq, &s->nb_freq, &s->freq_size); + if (ret < 0) + return ret; + + ret = parse_string(s->magnitude_str, &s->magnitude, &s->nb_magnitude, &s->magnitude_size); + if (ret < 0) + return ret; + } else { + char *freq_str; + + s->nb_magnitude = FF_ARRAY_ELEMS(eq_presets[s->preset].gains); + + freq_str = av_strdup(DEFAULT_BANDS); + if (!freq_str) + return AVERROR(ENOMEM); + + ret = parse_string(freq_str, &s->freq, &s->nb_freq, &s->freq_size); + av_free(freq_str); + if (ret < 0) + return ret; + + s->magnitude = av_calloc(s->nb_magnitude, sizeof(*s->magnitude)); + if (!s->magnitude) + return AVERROR(ENOMEM); + memcpy(s->magnitude, eq_presets[s->preset].gains, sizeof(*s->magnitude) * s->nb_magnitude); + } + + if (s->nb_freq != s->nb_magnitude || s->nb_freq < 2) { + av_log(ctx, AV_LOG_ERROR, "Number of bands and gains must be same and >= 2.\n"); + return AVERROR(EINVAL); + } + + s->freq[s->nb_freq] = outlink->sample_rate * 0.5f; + s->magnitude[s->nb_freq] = s->magnitude[s->nb_freq-1]; + + fft_size = s->nb_taps * 2; + factor = FFMIN(outlink->sample_rate * 0.5f, s->freq[s->nb_freq - 1]) / (float)fft_size; + asize = FFALIGN(fft_size, av_cpu_max_align()); + s->complexf = av_calloc(asize * 2, sizeof(*s->complexf)); + if (!s->complexf) + return AVERROR(ENOMEM); + + scale = 1.f; + ret = av_tx_init(&s->tx_ctx, &s->tx_fn, AV_TX_FLOAT_FFT, 0, fft_size, &scale, 0); + if (ret < 0) + return ret; + + s->taps = av_calloc(s->nb_taps, sizeof(*s->taps)); + if (!s->taps) + return AVERROR(ENOMEM); + + eq_interp(s->complexf, s->freq, s->magnitude, s->nb_freq, s->interp, s->nb_taps, factor); + + for (int i = 0; i < asize * 2; i++) + s->complexf[i].re = ff_exp10f(s->complexf[i].re / 20.f); + + s->tx_fn(s->tx_ctx, s->complexf + asize, s->complexf, sizeof(float)); + + middle = s->nb_taps / 2; + for (int i = 0; i < middle; i++) { + s->taps[middle - i] = (s->complexf[i + asize].re - s->complexf[i + asize].im) / fft_size; + s->taps[middle + i] = (s->complexf[i + asize].re - s->complexf[i + asize].im) / fft_size; + } + + s->pts = 0; + + return 0; +} + +static const AVFilterPad afireqsrc_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_eq_output, + }, +}; + +const AVFilter ff_asrc_afireqsrc = { + .name = "afireqsrc", + .description = NULL_IF_CONFIG_SMALL("Generate a FIR equalizer coefficients audio stream."), + .uninit = uninit, + .activate = activate, + .priv_size = sizeof(AudioFIRSourceContext), + .inputs = NULL, + FILTER_OUTPUTS(afireqsrc_outputs), + FILTER_QUERY_FUNC(query_formats), + .priv_class = &afireqsrc_class, +}; -- 2.37.2