FFmpeg
Loading...
Searching...
No Matches
asrc_hilbert.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Paul B Mahol
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
22#include "libavutil/mem.h"
23#include "libavutil/opt.h"
24#include "audio.h"
25#include "avfilter.h"
26#include "formats.h"
27#include "filters.h"
28#include "window_func.h"
29
30typedef struct HilbertContext {
31 const AVClass *class;
32
37
38 float *taps;
41
42#define OFFSET(x) offsetof(HilbertContext, x)
43#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
44
45static const AVOption hilbert_options[] = {
46 { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
47 { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
48 { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
49 { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
50 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
51 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
52 WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
54 {NULL}
55};
56
58
60{
61 HilbertContext *s = ctx->priv;
62
63 if (!(s->nb_taps & 1)) {
64 av_log(ctx, AV_LOG_ERROR, "Number of taps %d must be odd length.\n", s->nb_taps);
65 return AVERROR(EINVAL);
66 }
67
68 return 0;
69}
70
72{
73 HilbertContext *s = ctx->priv;
74
75 av_freep(&s->taps);
76}
77
79 AVFilterFormatsConfig **cfg_in,
80 AVFilterFormatsConfig **cfg_out)
81{
82 HilbertContext *s = ctx->priv;
83 static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } };
84 int sample_rates[] = { s->sample_rate, -1 };
85 static const enum AVSampleFormat sample_fmts[] = {
88 };
89 int ret = ff_set_sample_formats_from_list2(ctx, cfg_in, cfg_out, sample_fmts);
90 if (ret < 0)
91 return ret;
92
93 ret = ff_set_common_channel_layouts_from_list2(ctx, cfg_in, cfg_out, chlayouts);
94 if (ret < 0)
95 return ret;
96
98}
99
101{
102 AVFilterContext *ctx = outlink->src;
103 HilbertContext *s = ctx->priv;
104 float overlap;
105 int i;
106
107 s->taps = av_malloc_array(s->nb_taps, sizeof(*s->taps));
108 if (!s->taps)
109 return AVERROR(ENOMEM);
110
111 generate_window_func(s->taps, s->nb_taps, s->win_func, &overlap);
112
113 for (i = 0; i < s->nb_taps; i++) {
114 int k = -(s->nb_taps / 2) + i;
115
116 if (k & 1) {
117 float pk = M_PI * k;
118
119 s->taps[i] *= (1.f - cosf(pk)) / pk;
120 } else {
121 s->taps[i] = 0.f;
122 }
123 }
124
125 s->pts = 0;
126
127 return 0;
128}
129
131{
132 AVFilterLink *outlink = ctx->outputs[0];
133 HilbertContext *s = ctx->priv;
134 AVFrame *frame;
135 int nb_samples;
136
137 if (!ff_outlink_frame_wanted(outlink))
138 return FFERROR_NOT_READY;
139
140 nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
141 if (nb_samples <= 0) {
142 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
143 return 0;
144 }
145
146 if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
147 return AVERROR(ENOMEM);
148
149 memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
150
151 frame->pts = s->pts;
152 s->pts += nb_samples;
153 return ff_filter_frame(outlink, frame);
154}
155
156static const AVFilterPad hilbert_outputs[] = {
157 {
158 .name = "default",
159 .type = AVMEDIA_TYPE_AUDIO,
160 .config_props = config_props,
161 },
162};
163
165 .p.name = "hilbert",
166 .p.description = NULL_IF_CONFIG_SMALL("Generate a Hilbert transform FIR coefficients."),
167 .p.priv_class = &hilbert_class,
168 .init = init,
169 .uninit = uninit,
170 .activate = activate,
171 .priv_size = sizeof(HilbertContext),
174};
static enum AVSampleFormat sample_fmts[]
Definition adpcmenc.c:933
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
@ WFUNC_BLACKMAN
const FFFilter ff_asrc_hilbert
static const AVOption hilbert_options[]
static const AVFilterPad hilbert_outputs[]
static av_cold int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)
static av_cold int config_props(AVFilterLink *outlink)
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_outlink_frame_wanted(AVFilterLink *link)
Test if a frame is wanted on an output link.
Definition avfilter.c:1690
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static const int sample_rates[]
Definition dcaenc.h:34
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_set_common_samplerates_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const int *samplerates)
Definition formats.c:1050
int ff_set_common_channel_layouts_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const AVChannelLayout *fmts)
Definition formats.c:1026
int ff_set_sample_formats_from_list2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, const enum AVSampleFormat *fmts)
Definition formats.c:1154
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AV_CHANNEL_LAYOUT_MONO
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
AVSampleFormat
Audio sample formats.
Definition samplefmt.h:55
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_NONE
Definition samplefmt.h:56
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition filters.h:629
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#define cosf(x)
Definition libm.h:80
#define FFMIN(a, b)
Definition macros.h:49
#define M_PI
Definition mathematics.h:67
Memory handling functions.
AVOptions.
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
An AVChannelLayout holds information about the channel layout of audio data.
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
#define WIN_FUNC_OPTION(win_func_opt_name, win_func_offset, flag, default_window_func)
Definition window_func.h:37
static void generate_window_func(float *lut, int N, int win_func, float *overlap)
Definition window_func.h:63