From patchwork Sun Jan 8 14:03:44 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: 39925 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp2457574pzb; Sun, 8 Jan 2023 06:04:00 -0800 (PST) X-Google-Smtp-Source: AMrXdXsKMYidg7SCnWE6K8P6bwt1999R+QwsKnqMI99tMqdaMwtITSKorineQgBsre3RtBo/VRaw X-Received: by 2002:a17:906:27d4:b0:7c1:337e:575b with SMTP id k20-20020a17090627d400b007c1337e575bmr52554017ejc.66.1673186640236; Sun, 08 Jan 2023 06:04:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673186640; cv=none; d=google.com; s=arc-20160816; b=PHVZG/FWTtwDYdihjGwvnTmJmYW+PaF5mHZPzQbud+ncPonbYSMeZ90J8ulSSi5+5Z DlA6L/ftf0c62tje4UENJRDBwir3bZZWJBBvwnwGn0UqARSa5Q8SDi5u8o5xP72zsNKp ykGMlpMH6rFSxNP/X+SPehAGiQtxkXrlg800PnDLdPLOLMn7ALBTObx/YGFFKHAIWlmX A2bOZyKHCeqQnKiR5/Kyi2IKMLCy+Uo181dXmHTeTsKXNA9pOOapOskKRaHUr2YF7xOq qkKaAH5/IHX27uM8tXt+ZuLG8VI8Aau4/v6aVioV8qq+LuJhe4TiGcsuJaL5ToDAH0JN lvpg== 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=SoEx16i/HTIafY5JD/P6BK+klBOIz76LHORyyoYc2AU=; b=hFNWEqbVb45efCEASdhUSHc6YEDpHiiu+aWnIyMAQpd1kISDJU3csZ0TxB0wfvcCdZ uiywPvd7Iaaj+aAv/TC9216gj7YH+bObDxRqCPi40GtOPVtXTiv6CGdY7il9L4+HWyuH 4I9WzgBbkgiHcmO3KMJU7sIYHTD7Jpxuomd0Qqhl0kXY2T1waWMS23eQBmNdcPlY/+1q yBX5CT7t2kVDNWN7Y5MiBfWj9dUT+zksdB+zMUugdSatAU1153y3W0UXjPcORjclAa2R WH1qwmsKGYEBG9nYdlKVPWcTaFoZCwTw63aAyiSw/9uj9O3xS6vi0+XX3lGXV0c8Ujc2 0fmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=Ax4HOBlu; 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 oz9-20020a1709077d8900b0084c748f2679si7708469ejc.550.2023.01.08.06.03.57; Sun, 08 Jan 2023 06:04:00 -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=Ax4HOBlu; 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 0484268BB61; Sun, 8 Jan 2023 16:03:54 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ua1-f43.google.com (mail-ua1-f43.google.com [209.85.222.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4FD57680C4A for ; Sun, 8 Jan 2023 16:03:47 +0200 (EET) Received: by mail-ua1-f43.google.com with SMTP id u3so1428494uae.0 for ; Sun, 08 Jan 2023 06:03:47 -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=zNIihEP4h/HIQBYh4oWw1fo4AIYI6g7yEXNKbCxNptw=; b=Ax4HOBluUWxS7SHOjO6gciq+pVrYFpPzzCtyJ6nKYKX0cmaK5oTTq2ku/VtfZqp5hQ uuwo+K4LmbSRLHiZ9drQAwzQvcHJQNudeQZjjgN33PHTkTRgH6f98CmCemylRhWWIEMO qdQQkpi8Hjm29T03HhFJlJpS0Et321dlVGPKMpipHRiSJjuP9B0l3RkLfUa3JZOiIfd8 JYixcGgDZCUekwOjOokl+5/ufBZZYrU7zxzVRT3D4ZFNv6EDRyxqXqOaT9wyK+e+XHtq og9w7xw4Gt7W8U20W1PIOlH9Q/ZYJOy/JCDbDwyiyc9ObiscJYinXHY+nh3ugVGduCkJ bl2A== 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=zNIihEP4h/HIQBYh4oWw1fo4AIYI6g7yEXNKbCxNptw=; b=JcC0fxKCzh8zLl1YlBGfXgUftrLp0qnm6i7Q58AS3AHhOYLljeCb3KzoQfB9IneuMe lwgtDW/TDFh3EQzYk711LwfdFzcSc3mXcolUDgB9Q8T019qlo2S6sq9S4v3qfTzDduIV OhBDVRTsqmFIbQDnA0/qHCeIlPorLcz4alNFTaTVPV3vQiNebyNfrfn4uaiZZDyI/JYO P8rQ+cvXlGYBuh84tx4aOWuFlN+69QiHMA177hxxGE2P6IKmmNRhOFCMrf9ZSQFD0iww p7928ezHNsybiBWaA3uZlbdRkChYj5RT9bQ9QKWD3gJMkoGmgKPi77LR0bmYu7NG/YOp ljug== X-Gm-Message-State: AFqh2krBLTkflozlOtaI7CXmades+CbalAm5JmX2cGYnw2SinQKvD5i0 jhywvt75IWrc/m0Lprwe4rL6ERBo98NMYrFWD8527IHs X-Received: by 2002:ab0:7142:0:b0:537:3057:6967 with SMTP id k2-20020ab07142000000b0053730576967mr3703592uao.57.1673186625502; Sun, 08 Jan 2023 06:03:45 -0800 (PST) MIME-Version: 1.0 Received: by 2002:a05:612c:2428:b0:32a:5eaa:92e4 with HTTP; Sun, 8 Jan 2023 06:03:44 -0800 (PST) From: Paul B Mahol Date: Sun, 8 Jan 2023 15:03:44 +0100 Message-ID: To: FFmpeg development discussions and patches Subject: [FFmpeg-devel] [PATCH] avfilter: add fractional delay IR 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: wTVjZ8T6HRj/ Patch attached. From 7492dad5bc3a7826e5abd67f26503c74bd5745d2 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Sun, 8 Jan 2023 13:53:39 +0100 Subject: [PATCH] avfilter: add fractional delay IR source filter Signed-off-by: Paul B Mahol --- doc/filters.texi | 23 ++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/asrc_afdelaysrc.c | 145 ++++++++++++++++++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 libavfilter/asrc_afdelaysrc.c diff --git a/doc/filters.texi b/doc/filters.texi index 9c32339141..e8febc2bfd 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -7519,6 +7519,29 @@ aevalsrc="0.1*sin(2*PI*(360-2.5/2)*t) | 0.1*sin(2*PI*(360+2.5/2)*t)" @end itemize +@section afdelaysrc + +Generate a fractional delay FIR 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 delay, d +Set the fractional delay. Default is 0. + +@item sample_rate, r +Set the sample rate, default is 44100. + +@item nb_samples, n +Set the number of samples per each frame. Default is 1024. + +@item taps, t +Set the number of filter coefficents in output audio stream. +Default value is 0. +@end table + @section afirsrc Generate a FIR coefficients using frequency sampling method. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index f235ac047e..65cf3e7d7e 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -172,6 +172,7 @@ OBJS-$(CONFIG_VOLUME_FILTER) += af_volume.o OBJS-$(CONFIG_VOLUMEDETECT_FILTER) += af_volumedetect.o OBJS-$(CONFIG_AEVALSRC_FILTER) += aeval.o +OBJS-$(CONFIG_AFDELAYSRC_FILTER) += asrc_afdelaysrc.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 2ece5c15c8..c01cae3e84 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -160,6 +160,7 @@ extern const AVFilter ff_af_volume; extern const AVFilter ff_af_volumedetect; extern const AVFilter ff_asrc_aevalsrc; +extern const AVFilter ff_asrc_afdelaysrc; extern const AVFilter ff_asrc_afirsrc; extern const AVFilter ff_asrc_anoisesrc; extern const AVFilter ff_asrc_anullsrc; diff --git a/libavfilter/asrc_afdelaysrc.c b/libavfilter/asrc_afdelaysrc.c new file mode 100644 index 0000000000..784771f84b --- /dev/null +++ b/libavfilter/asrc_afdelaysrc.c @@ -0,0 +1,145 @@ +/* + * Copyright (c) 2023 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/opt.h" + +#include "audio.h" +#include "avfilter.h" +#include "filters.h" +#include "internal.h" + +typedef struct AFDelaySrcContext { + const AVClass *class; + + double delay; + int sample_rate; + int nb_samples; + int nb_taps; + + int64_t pts; +} AFDelaySrcContext; + +static float sincf(float x) +{ + if (x == 0.f) + return 1.f; + return sinf(M_PI * x) / (M_PI * x); +} + +static int activate(AVFilterContext *ctx) +{ + AVFilterLink *outlink = ctx->outputs[0]; + AFDelaySrcContext *s = ctx->priv; + AVFrame *frame = NULL; + int nb_samples; + float *dst; + + if (!ff_outlink_frame_wanted(outlink)) + return FFERROR_NOT_READY; + + nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts); + if (nb_samples <= 0) { + ff_outlink_set_status(outlink, AVERROR_EOF, s->pts); + return 0; + } + + if (!(frame = ff_get_audio_buffer(outlink, nb_samples))) + return AVERROR(ENOMEM); + + dst = (float *)frame->extended_data[0]; + for (int n = 0; n < nb_samples; n++) { + float x = s->pts + n; + dst[n] = sincf(x - s->delay) * cosf(M_PI * (x - s->delay) / s->nb_taps) / sincf((x - s->delay) / s->nb_taps); + } + + frame->pts = s->pts; + s->pts += nb_samples; + + return ff_filter_frame(outlink, frame); +} + +static int query_formats(AVFilterContext *ctx) +{ + AFDelaySrcContext *s = ctx->priv; + static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } }; + int sample_rates[] = { s->sample_rate, -1 }; + static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, + AV_SAMPLE_FMT_NONE }; + int ret = ff_set_common_formats_from_list(ctx, sample_fmts); + if (ret < 0) + return ret; + + ret = ff_set_common_channel_layouts_from_list(ctx, chlayouts); + if (ret < 0) + return ret; + + return ff_set_common_samplerates_from_list(ctx, sample_rates); +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + AFDelaySrcContext *s = ctx->priv; + + outlink->sample_rate = s->sample_rate; + s->pts = 0; + if (s->nb_taps <= 0) + s->nb_taps = s->delay * 8 + 1; + + return 0; +} + +static const AVFilterPad afdelaysrc_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_output, + }, +}; + +#define AF AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM +#define OFFSET(x) offsetof(AFDelaySrcContext, x) + +static const AVOption afdelaysrc_options[] = { + { "delay", "set fractional delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE,{.dbl=0}, 0, INT16_MAX, AF }, + { "d", "set fractional delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE,{.dbl=0}, 0, INT16_MAX, AF }, + { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, AF }, + { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, AF }, + { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, AF }, + { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, AF }, + { "taps", "set number of taps for delay filter", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=0}, 0, 32768, AF }, + { "t", "set number of taps for delay filter", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=0}, 0, 32768, AF }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(afdelaysrc); + +const AVFilter ff_asrc_afdelaysrc = { + .name = "afdelaysrc", + .description = NULL_IF_CONFIG_SMALL("Generate a Fractional delay FIR coefficients."), + .priv_size = sizeof(AFDelaySrcContext), + .priv_class = &afdelaysrc_class, + .activate = activate, + .inputs = NULL, + FILTER_OUTPUTS(afdelaysrc_outputs), + FILTER_QUERY_FUNC(query_formats), +}; -- 2.37.2