2018-01-01 15:34:02 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-06-12 01:10:58 +02:00
|
|
|
#include "libavutil/channel_layout.h"
|
2018-01-01 15:34:02 +01:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "internal.h"
|
2021-08-21 16:31:04 +02:00
|
|
|
#include "filters.h"
|
2018-01-01 15:34:02 +01:00
|
|
|
#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 },
|
2021-10-15 10:41:01 +02:00
|
|
|
WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
|
|
|
|
WIN_FUNC_OPTION("w", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
|
2018-01-01 15:34:02 +01:00
|
|
|
{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;
|
2021-08-31 16:03:14 +02:00
|
|
|
static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } };
|
2018-01-01 15:34:02 +01:00
|
|
|
int sample_rates[] = { s->sample_rate, -1 };
|
|
|
|
static const enum AVSampleFormat sample_fmts[] = {
|
|
|
|
AV_SAMPLE_FMT_FLT,
|
|
|
|
AV_SAMPLE_FMT_NONE
|
|
|
|
};
|
2021-08-10 01:25:31 +02:00
|
|
|
int ret = ff_set_common_formats_from_list(ctx, sample_fmts);
|
2018-01-01 15:34:02 +01:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2021-08-10 01:25:31 +02:00
|
|
|
ret = ff_set_common_channel_layouts_from_list(ctx, chlayouts);
|
2018-01-01 15:34:02 +01:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2021-08-10 01:25:31 +02:00
|
|
|
return ff_set_common_samplerates_from_list(ctx, sample_rates);
|
2018-01-01 15:34:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:31:04 +02:00
|
|
|
static int activate(AVFilterContext *ctx)
|
2018-01-01 15:34:02 +01:00
|
|
|
{
|
2021-08-21 16:31:04 +02:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2018-01-01 15:34:02 +01:00
|
|
|
HilbertContext *s = ctx->priv;
|
|
|
|
AVFrame *frame;
|
|
|
|
int nb_samples;
|
|
|
|
|
2021-08-21 16:31:04 +02:00
|
|
|
if (!ff_outlink_frame_wanted(outlink))
|
|
|
|
return FFERROR_NOT_READY;
|
|
|
|
|
2018-01-01 15:34:02 +01:00
|
|
|
nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
|
2021-08-21 16:31:04 +02:00
|
|
|
if (nb_samples <= 0) {
|
|
|
|
ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-01 15:34:02 +01:00
|
|
|
|
|
|
|
if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
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,
|
|
|
|
.config_props = config_props,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_asrc_hilbert = {
|
2018-01-01 15:34:02 +01:00
|
|
|
.name = "hilbert",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Generate a Hilbert transform FIR coefficients."),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
2021-08-21 16:31:04 +02:00
|
|
|
.activate = activate,
|
2018-01-01 15:34:02 +01:00
|
|
|
.priv_size = sizeof(HilbertContext),
|
|
|
|
.inputs = NULL,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_OUTPUTS(hilbert_outputs),
|
2021-09-27 12:07:35 +02:00
|
|
|
FILTER_QUERY_FUNC(query_formats),
|
2018-01-01 15:34:02 +01:00
|
|
|
.priv_class = &hilbert_class,
|
|
|
|
};
|