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
21 #include "libavutil/channel_layout.h"
22 #include "libavutil/opt.h"
23 #include "audio.h"
24 #include "avfilter.h"
25 #include "internal.h"
26 #include "filters.h"
27 #include "window_func.h"
28
29 typedef struct HilbertContext {
30 const AVClass *class;
31
32 int sample_rate;
33 int nb_taps;
34 int nb_samples;
35 int win_func;
36
37 float *taps;
38 int64_t pts;
39 } HilbertContext;
40
41 #define OFFSET(x) offsetof(HilbertContext, x)
42 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
43
44 static const AVOption hilbert_options[] = {
45 { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
46 { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
47 { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
48 { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=22051}, 11, UINT16_MAX, FLAGS },
49 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
50 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
51 WIN_FUNC_OPTION("win_func", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
52 WIN_FUNC_OPTION("w", OFFSET(win_func), FLAGS, WFUNC_BLACKMAN),
53 {NULL}
54 };
55
56 AVFILTER_DEFINE_CLASS(hilbert);
57
init(AVFilterContext * ctx)58 static av_cold int init(AVFilterContext *ctx)
59 {
60 HilbertContext *s = ctx->priv;
61
62 if (!(s->nb_taps & 1)) {
63 av_log(s, AV_LOG_ERROR, "Number of taps %d must be odd length.\n", s->nb_taps);
64 return AVERROR(EINVAL);
65 }
66
67 return 0;
68 }
69
uninit(AVFilterContext * ctx)70 static av_cold void uninit(AVFilterContext *ctx)
71 {
72 HilbertContext *s = ctx->priv;
73
74 av_freep(&s->taps);
75 }
76
query_formats(AVFilterContext * ctx)77 static av_cold int query_formats(AVFilterContext *ctx)
78 {
79 HilbertContext *s = ctx->priv;
80 static const AVChannelLayout chlayouts[] = { AV_CHANNEL_LAYOUT_MONO, { 0 } };
81 int sample_rates[] = { s->sample_rate, -1 };
82 static const enum AVSampleFormat sample_fmts[] = {
83 AV_SAMPLE_FMT_FLT,
84 AV_SAMPLE_FMT_NONE
85 };
86 int ret = ff_set_common_formats_from_list(ctx, sample_fmts);
87 if (ret < 0)
88 return ret;
89
90 ret = ff_set_common_channel_layouts_from_list(ctx, chlayouts);
91 if (ret < 0)
92 return ret;
93
94 return ff_set_common_samplerates_from_list(ctx, sample_rates);
95 }
96
config_props(AVFilterLink * outlink)97 static av_cold int config_props(AVFilterLink *outlink)
98 {
99 AVFilterContext *ctx = outlink->src;
100 HilbertContext *s = ctx->priv;
101 float overlap;
102 int i;
103
104 s->taps = av_malloc_array(s->nb_taps, sizeof(*s->taps));
105 if (!s->taps)
106 return AVERROR(ENOMEM);
107
108 generate_window_func(s->taps, s->nb_taps, s->win_func, &overlap);
109
110 for (i = 0; i < s->nb_taps; i++) {
111 int k = -(s->nb_taps / 2) + i;
112
113 if (k & 1) {
114 float pk = M_PI * k;
115
116 s->taps[i] *= (1.f - cosf(pk)) / pk;
117 } else {
118 s->taps[i] = 0.f;
119 }
120 }
121
122 s->pts = 0;
123
124 return 0;
125 }
126
activate(AVFilterContext * ctx)127 static int activate(AVFilterContext *ctx)
128 {
129 AVFilterLink *outlink = ctx->outputs[0];
130 HilbertContext *s = ctx->priv;
131 AVFrame *frame;
132 int nb_samples;
133
134 if (!ff_outlink_frame_wanted(outlink))
135 return FFERROR_NOT_READY;
136
137 nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
138 if (nb_samples <= 0) {
139 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
140 return 0;
141 }
142
143 if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
144 return AVERROR(ENOMEM);
145
146 memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
147
148 frame->pts = s->pts;
149 s->pts += nb_samples;
150 return ff_filter_frame(outlink, frame);
151 }
152
153 static const AVFilterPad hilbert_outputs[] = {
154 {
155 .name = "default",
156 .type = AVMEDIA_TYPE_AUDIO,
157 .config_props = config_props,
158 },
159 };
160
161 const AVFilter ff_asrc_hilbert = {
162 .name = "hilbert",
163 .description = NULL_IF_CONFIG_SMALL("Generate a Hilbert transform FIR coefficients."),
164 .init = init,
165 .uninit = uninit,
166 .activate = activate,
167 .priv_size = sizeof(HilbertContext),
168 .inputs = NULL,
169 FILTER_OUTPUTS(hilbert_outputs),
170 FILTER_QUERY_FUNC(query_formats),
171 .priv_class = &hilbert_class,
172 };
173