2 * Copyright (c) 2020 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavutil/eval.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/tx.h"
27 #include "window_func.h"
29 typedef struct AudioFIRSourceContext {
32 char *freq_points_str;
40 AVComplexFloat *complexf;
57 } AudioFIRSourceContext;
59 #define OFFSET(x) offsetof(AudioFIRSourceContext, x)
60 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
62 static const AVOption afirsrc_options[] = {
63 { "taps", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=1025}, 9, UINT16_MAX, FLAGS },
64 { "t", "set number of taps", OFFSET(nb_taps), AV_OPT_TYPE_INT, {.i64=1025}, 9, UINT16_MAX, FLAGS },
65 { "frequency", "set frequency points", OFFSET(freq_points_str), AV_OPT_TYPE_STRING, {.str="0 1"}, 0, 0, FLAGS },
66 { "f", "set frequency points", OFFSET(freq_points_str), AV_OPT_TYPE_STRING, {.str="0 1"}, 0, 0, FLAGS },
67 { "magnitude", "set magnitude values", OFFSET(magnitude_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, FLAGS },
68 { "m", "set magnitude values", OFFSET(magnitude_str), AV_OPT_TYPE_STRING, {.str="1 1"}, 0, 0, FLAGS },
69 { "phase", "set phase values", OFFSET(phase_str), AV_OPT_TYPE_STRING, {.str="0 0"}, 0, 0, FLAGS },
70 { "p", "set phase values", OFFSET(phase_str), AV_OPT_TYPE_STRING, {.str="0 0"}, 0, 0, FLAGS },
71 { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
72 { "r", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64=44100}, 1, INT_MAX, FLAGS },
73 { "nb_samples", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
74 { "n", "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 1, INT_MAX, FLAGS },
75 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_BLACKMAN}, 0, NB_WFUNC-1, FLAGS, "win_func" },
76 { "w", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_BLACKMAN}, 0, NB_WFUNC-1, FLAGS, "win_func" },
77 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
78 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
79 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
80 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
81 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
82 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
83 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
84 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
85 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
86 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
87 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
88 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
89 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
90 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
91 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
92 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
93 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
94 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
95 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
96 { "bohman" , "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, FLAGS, "win_func" },
100 AVFILTER_DEFINE_CLASS(afirsrc);
102 static av_cold int init(AVFilterContext *ctx)
104 AudioFIRSourceContext *s = ctx->priv;
106 if (!(s->nb_taps & 1)) {
107 av_log(s, AV_LOG_WARNING, "Number of taps %d must be odd length.\n", s->nb_taps);
114 static av_cold void uninit(AVFilterContext *ctx)
116 AudioFIRSourceContext *s = ctx->priv;
121 av_freep(&s->magnitude);
123 av_freep(&s->complexf);
124 av_tx_uninit(&s->tx_ctx);
127 static av_cold int query_formats(AVFilterContext *ctx)
129 AudioFIRSourceContext *s = ctx->priv;
130 static const int64_t chlayouts[] = { AV_CH_LAYOUT_MONO, -1 };
131 int sample_rates[] = { s->sample_rate, -1 };
132 static const enum AVSampleFormat sample_fmts[] = {
137 AVFilterFormats *formats;
138 AVFilterChannelLayouts *layouts;
141 formats = ff_make_format_list(sample_fmts);
143 return AVERROR(ENOMEM);
144 ret = ff_set_common_formats (ctx, formats);
148 layouts = ff_make_format64_list(chlayouts);
150 return AVERROR(ENOMEM);
151 ret = ff_set_common_channel_layouts(ctx, layouts);
155 formats = ff_make_format_list(sample_rates);
157 return AVERROR(ENOMEM);
158 return ff_set_common_samplerates(ctx, formats);
161 static int parse_string(char *str, float **items, int *nb_items, int *items_size)
166 new_items = av_fast_realloc(NULL, items_size, 1 * sizeof(float));
168 return AVERROR(ENOMEM);
173 return AVERROR(EINVAL);
176 (*items)[(*nb_items)++] = av_strtod(tail, &tail);
177 new_items = av_fast_realloc(*items, items_size, (*nb_items + 1) * sizeof(float));
179 return AVERROR(ENOMEM);
183 } while (tail && *tail);
188 static void lininterp(AVComplexFloat *complexf,
190 const float *magnitude,
194 for (int i = 0; i < minterp; i++) {
195 for (int j = 1; j < m; j++) {
196 const float x = i / (float)minterp;
199 const float mg = (x - freq[j-1]) / (freq[j] - freq[j-1]) * (magnitude[j] - magnitude[j-1]) + magnitude[j-1];
200 const float ph = (x - freq[j-1]) / (freq[j] - freq[j-1]) * (phase[j] - phase[j-1]) + phase[j-1];
202 complexf[i].re = mg * cosf(ph);
203 complexf[i].im = mg * sinf(ph);
210 static av_cold int config_output(AVFilterLink *outlink)
212 AVFilterContext *ctx = outlink->src;
213 AudioFIRSourceContext *s = ctx->priv;
214 float overlap, scale = 1.f, compensation;
215 int fft_size, middle, ret;
217 s->nb_freq = s->nb_magnitude = s->nb_phase = 0;
219 ret = parse_string(s->freq_points_str, &s->freq, &s->nb_freq, &s->freq_size);
223 ret = parse_string(s->magnitude_str, &s->magnitude, &s->nb_magnitude, &s->magnitude_size);
227 ret = parse_string(s->phase_str, &s->phase, &s->nb_phase, &s->phase_size);
231 if (s->nb_freq != s->nb_magnitude && s->nb_freq != s->nb_phase && s->nb_freq >= 2) {
232 av_log(ctx, AV_LOG_ERROR, "Number of frequencies, magnitudes and phases must be same and >= 2.\n");
233 return AVERROR(EINVAL);
236 for (int i = 0; i < s->nb_freq; i++) {
237 if (i == 0 && s->freq[i] != 0.f) {
238 av_log(ctx, AV_LOG_ERROR, "First frequency must be 0.\n");
239 return AVERROR(EINVAL);
242 if (i == s->nb_freq - 1 && s->freq[i] != 1.f) {
243 av_log(ctx, AV_LOG_ERROR, "Last frequency must be 1.\n");
244 return AVERROR(EINVAL);
247 if (i && s->freq[i] < s->freq[i-1]) {
248 av_log(ctx, AV_LOG_ERROR, "Frequencies must be in increasing order.\n");
249 return AVERROR(EINVAL);
253 fft_size = 1 << (av_log2(s->nb_taps) + 1);
254 s->complexf = av_calloc(fft_size * 2, sizeof(*s->complexf));
256 return AVERROR(ENOMEM);
258 ret = av_tx_init(&s->tx_ctx, &s->tx_fn, AV_TX_FLOAT_FFT, 1, fft_size, &scale, 0);
262 s->taps = av_calloc(s->nb_taps, sizeof(*s->taps));
264 return AVERROR(ENOMEM);
266 s->win = av_calloc(s->nb_taps, sizeof(*s->win));
268 return AVERROR(ENOMEM);
270 generate_window_func(s->win, s->nb_taps, s->win_func, &overlap);
272 lininterp(s->complexf, s->freq, s->magnitude, s->phase, s->nb_freq, fft_size / 2);
274 s->tx_fn(s->tx_ctx, s->complexf + fft_size, s->complexf, sizeof(float));
276 compensation = 2.f / fft_size;
277 middle = s->nb_taps / 2;
279 for (int i = 0; i <= middle; i++) {
280 s->taps[ i] = s->complexf[fft_size + middle - i].re * compensation * s->win[i];
281 s->taps[middle + i] = s->complexf[fft_size + i].re * compensation * s->win[middle + i];
289 static int request_frame(AVFilterLink *outlink)
291 AVFilterContext *ctx = outlink->src;
292 AudioFIRSourceContext *s = ctx->priv;
296 nb_samples = FFMIN(s->nb_samples, s->nb_taps - s->pts);
300 if (!(frame = ff_get_audio_buffer(outlink, nb_samples)))
301 return AVERROR(ENOMEM);
303 memcpy(frame->data[0], s->taps + s->pts, nb_samples * sizeof(float));
306 s->pts += nb_samples;
307 return ff_filter_frame(outlink, frame);
310 static const AVFilterPad afirsrc_outputs[] = {
313 .type = AVMEDIA_TYPE_AUDIO,
314 .request_frame = request_frame,
315 .config_props = config_output,
320 const AVFilter ff_asrc_afirsrc = {
322 .description = NULL_IF_CONFIG_SMALL("Generate a FIR coefficients audio stream."),
323 .query_formats = query_formats,
326 .priv_size = sizeof(AudioFIRSourceContext),
328 .outputs = afirsrc_outputs,
329 .priv_class = &afirsrc_class,