diff mbox series

[FFmpeg-devel] avfilter: add fractional delay IR source filter

Message ID CAPYw7P6sY4Wsep6JzePqO_d=7vvZM592pw6A124c+dCVV1-gjw@mail.gmail.com
State New
Headers show
Series [FFmpeg-devel] avfilter: add fractional delay IR source filter | expand

Checks

Context Check Description
yinshiyou/configure_loongarch64 warning Failed to apply patch
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Paul B Mahol Jan. 8, 2023, 2:03 p.m. UTC
Patch attached.

Comments

Paul B Mahol Jan. 15, 2023, 9:45 a.m. UTC | #1
On 1/8/23, Paul B Mahol <onemda@gmail.com> wrote:
> Patch attached.
>

will apply soon
diff mbox series

Patch

From 7492dad5bc3a7826e5abd67f26503c74bd5745d2 Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@gmail.com>
Date: Sun, 8 Jan 2023 13:53:39 +0100
Subject: [PATCH] avfilter: add fractional delay IR source filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 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