[FFmpeg-devel] avfilter: add hilbert source FIR filter

Submitted by Paul B Mahol on Jan. 1, 2018, 4:02 p.m.

Details

Message ID 20180101160213.29411-1-onemda@gmail.com
State New
Headers show

Commit Message

Paul B Mahol Jan. 1, 2018, 4:02 p.m.
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 doc/filters.texi           |  28 +++++++
 libavfilter/Makefile       |   1 +
 libavfilter/allfilters.c   |   1 +
 libavfilter/asrc_hilbert.c | 201 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 231 insertions(+)
 create mode 100644 libavfilter/asrc_hilbert.c

Comments

Carl Eugen Hoyos Jan. 1, 2018, 4:09 p.m.
2018-01-01 17:02 GMT+01:00 Paul B Mahol <onemda@gmail.com>:

> +    float *dst;

Write-only variable.

Carl Eugen
Paul B Mahol Jan. 1, 2018, 4:16 p.m.
On 1/1/18, Carl Eugen Hoyos <ceffmpeg@gmail.com> wrote:
> 2018-01-01 17:02 GMT+01:00 Paul B Mahol <onemda@gmail.com>:
>
>> +    float *dst;
>
> Write-only variable.
>

Removed locally.
Moritz Barsnick Jan. 1, 2018, 6:10 p.m.
On Mon, Jan 01, 2018 at 17:02:13 +0100, Paul B Mahol wrote:
> +Generate an odd-tap Hilbert transform FIR coefficients.

"an" is singular, "coefficients" is plural.

> +The resulted stream can be used with @ref{afir} filter for phase-shifting
       ^ resulting

> +@item win_func, w
> +Set window function to be used when generating FIR coefficients.
> +@end table

Usually it would be good listing them, but considering how many there
are... I'm short of opinions here.

Moritz

Patch hide | download patch | download mbox

diff --git a/doc/filters.texi b/doc/filters.texi
index f651f1234d..f42f4c9190 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -926,6 +926,7 @@  afftfilt="1-clip((b/nb)*b,0,1)"
 @end example
 @end itemize
 
+@anchor{afir}
 @section afir
 
 Apply an arbitrary Frequency Impulse Response filter.
@@ -4873,6 +4874,33 @@  anoisesrc=d=60:c=pink:r=44100:a=0.5
 @end example
 @end itemize
 
+@section hilbert
+
+Generate an odd-tap Hilbert transform FIR coefficients.
+
+The resulted stream can be used with @ref{afir} filter for phase-shifting
+the signal by 90 degrees.
+
+This is used in many matrix coding schemes and for analytic signal generation.
+The process is often written as a multiplication by i (or j), the imaginary unit.
+
+The filter accepts the following options:
+
+@table @option
+
+@item sample_rate, s
+Set sample rate, default is 44100.
+
+@item taps, t
+Set length of FIR filter, default is 22051.
+
+@item nb_samples, n
+Set number of samples per each frame.
+
+@item win_func, w
+Set window function to be used when generating FIR coefficients.
+@end table
+
 @section sine
 
 Generate an audio signal made of a sine wave with amplitude 1/8.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 8bde542163..5eb331c961 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -126,6 +126,7 @@  OBJS-$(CONFIG_AEVALSRC_FILTER)               += aeval.o
 OBJS-$(CONFIG_ANOISESRC_FILTER)              += asrc_anoisesrc.o
 OBJS-$(CONFIG_ANULLSRC_FILTER)               += asrc_anullsrc.o
 OBJS-$(CONFIG_FLITE_FILTER)                  += asrc_flite.o
+OBJS-$(CONFIG_HILBERT_FILTER)                += asrc_hilbert.o
 OBJS-$(CONFIG_SINE_FILTER)                   += asrc_sine.o
 
 OBJS-$(CONFIG_ANULLSINK_FILTER)              += asink_anullsink.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 67c073091f..7d5129d929 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -137,6 +137,7 @@  static void register_all(void)
     REGISTER_FILTER(ANOISESRC,      anoisesrc,      asrc);
     REGISTER_FILTER(ANULLSRC,       anullsrc,       asrc);
     REGISTER_FILTER(FLITE,          flite,          asrc);
+    REGISTER_FILTER(HILBERT,        hilbert,        asrc);
     REGISTER_FILTER(SINE,           sine,           asrc);
 
     REGISTER_FILTER(ANULLSINK,      anullsink,      asink);
diff --git a/libavfilter/asrc_hilbert.c b/libavfilter/asrc_hilbert.c
new file mode 100644
index 0000000000..1270a6b9f1
--- /dev/null
+++ b/libavfilter/asrc_hilbert.c
@@ -0,0 +1,201 @@ 
+/*
+ * Copyright (c) 2018 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/opt.h"
+#include "audio.h"
+#include "avfilter.h"
+#include "internal.h"
+#include "window_func.h"
+
+typedef struct HilbertContext {
+    const AVClass *class;
+
+    int sample_rate;
+    int nb_taps;
+    int nb_samples;
+    int win_func;
+
+    float *taps;
+    int64_t pts;
+} HilbertContext;
+
+#define OFFSET(x) offsetof(HilbertContext, x)
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption hilbert_options[] = {
+    { "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 },
+    { "taps",        "set number of taps", OFFSET(nb_taps),     AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
+    { "t",           "set number of taps", OFFSET(nb_taps),     AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_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 },
+    { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_BLACKMAN}, 0, NB_WFUNC-1, FLAGS, "win_func" },
+    { "w",        "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_BLACKMAN}, 0, NB_WFUNC-1, FLAGS, "win_func" },
+        { "rect",     "Rectangular",      0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT},     0, 0, FLAGS, "win_func" },
+        { "bartlett", "Bartlett",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
+        { "hanning",  "Hanning",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING},  0, 0, FLAGS, "win_func" },
+        { "hamming",  "Hamming",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING},  0, 0, FLAGS, "win_func" },
+        { "blackman", "Blackman",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
+        { "welch",    "Welch",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH},    0, 0, FLAGS, "win_func" },
+        { "flattop",  "Flat-top",         0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP},  0, 0, FLAGS, "win_func" },
+        { "bharris",  "Blackman-Harris",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS},  0, 0, FLAGS, "win_func" },
+        { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
+        { "bhann",    "Bartlett-Hann",    0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN},    0, 0, FLAGS, "win_func" },
+        { "sine",     "Sine",             0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE},     0, 0, FLAGS, "win_func" },
+        { "nuttall",  "Nuttall",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL},  0, 0, FLAGS, "win_func" },
+        { "lanczos",  "Lanczos",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS},  0, 0, FLAGS, "win_func" },
+        { "gauss",    "Gauss",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS},    0, 0, FLAGS, "win_func" },
+        { "tukey",    "Tukey",            0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY},    0, 0, FLAGS, "win_func" },
+        { "dolph",    "Dolph-Chebyshev",  0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH},    0, 0, FLAGS, "win_func" },
+        { "cauchy",   "Cauchy",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY},   0, 0, FLAGS, "win_func" },
+        { "parzen",   "Parzen",           0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN},   0, 0, FLAGS, "win_func" },
+        { "poisson",  "Poisson",          0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON},  0, 0, FLAGS, "win_func" },
+    {NULL}
+};
+
+AVFILTER_DEFINE_CLASS(hilbert);
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    HilbertContext *s = ctx->priv;
+
+    if (!(s->nb_taps & 1)) {
+        av_log(s, AV_LOG_ERROR, "Number of taps %d must be odd length.\n", s->nb_taps);
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    HilbertContext *s = ctx->priv;
+
+    av_freep(&s->taps);
+}
+
+static av_cold int query_formats(AVFilterContext *ctx)
+{
+    HilbertContext *s = ctx->priv;
+    static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
+    int sample_rates[] = { s->sample_rate, -1 };
+    static const enum AVSampleFormat sample_fmts[] = {
+        AV_SAMPLE_FMT_FLT,
+        AV_SAMPLE_FMT_NONE
+    };
+
+    AVFilterFormats *formats;
+    AVFilterChannelLayouts *layouts;
+    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 = avfilter_make_format64_list(chlayouts);
+    if (!layouts)
+        return AVERROR(ENOMEM);
+    ret = ff_set_common_channel_layouts(ctx, layouts);
+    if (ret < 0)
+        return ret;
+
+    formats = ff_make_format_list(sample_rates);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    return ff_set_common_samplerates(ctx, formats);
+}
+
+static av_cold int config_props(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    HilbertContext *s = ctx->priv;
+    float overlap;
+    int i;
+
+    s->taps = av_malloc_array(s->nb_taps, sizeof(*s->taps));
+    if (!s->taps)
+        return AVERROR(ENOMEM);
+
+    generate_window_func(s->taps, s->nb_taps, s->win_func, &overlap);
+
+    for (i = 0; i < s->nb_taps; i++) {
+        int k = -(s->nb_taps / 2) + i;
+
+        if (k & 1) {
+            float pk = M_PI * k;
+
+            s->taps[i] *= (1.f - cosf(pk)) / pk;
+        } else {
+            s->taps[i] = 0.f;
+        }
+    }
+
+    s->pts = 0;
+
+    return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    HilbertContext *s = ctx->priv;
+    AVFrame *frame;
+    int nb_samples;
+    float *dst;
+
+    nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
+    if (!nb_samples)
+        return AVERROR_EOF;
+
+    if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
+        return AVERROR(ENOMEM);
+
+    dst = (float *)frame->data[0];
+    memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
+
+    frame->pts = s->pts;
+    s->pts    += nb_samples;
+    return ff_filter_frame(outlink, frame);
+}
+
+static const AVFilterPad hilbert_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_AUDIO,
+        .request_frame = request_frame,
+        .config_props  = config_props,
+    },
+    { NULL }
+};
+
+AVFilter ff_asrc_hilbert = {
+    .name          = "hilbert",
+    .description   = NULL_IF_CONFIG_SMALL("Generate a Hilbert transform FIR coefficients."),
+    .query_formats = query_formats,
+    .init          = init,
+    .uninit        = uninit,
+    .priv_size     = sizeof(HilbertContext),
+    .inputs        = NULL,
+    .outputs       = hilbert_outputs,
+    .priv_class    = &hilbert_class,
+};