2 * Copyright (c) 2016 Paul B Mahol
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; either version 2.1 of the License,
9 * 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/audio_fifo.h"
22 #include "libavutil/avstring.h"
23 #include "libavfilter/internal.h"
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
26 #include "libavcodec/avfft.h"
27 #include "libavutil/eval.h"
30 #include "window_func.h"
32 typedef struct AFFTFiltContext {
39 FFTContext *fft, *ifft;
40 FFTComplex **fft_data;
41 FFTComplex **fft_temp;
54 float *window_func_lut;
57 static const char *const var_names[] = { "sr", "b", "nb", "ch", "chs", "pts", "re", "im", NULL };
58 enum { VAR_SAMPLE_RATE, VAR_BIN, VAR_NBBINS, VAR_CHANNEL, VAR_CHANNELS, VAR_PTS, VAR_REAL, VAR_IMAG, VAR_VARS_NB };
60 #define OFFSET(x) offsetof(AFFTFiltContext, x)
61 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
63 static const AVOption afftfilt_options[] = {
64 { "real", "set channels real expressions", OFFSET(real_str), AV_OPT_TYPE_STRING, {.str = "re" }, 0, 0, A },
65 { "imag", "set channels imaginary expressions", OFFSET(img_str), AV_OPT_TYPE_STRING, {.str = "im" }, 0, 0, A },
66 { "win_size", "set window size", OFFSET(fft_size), AV_OPT_TYPE_INT, {.i64=4096}, 16, 131072, A },
67 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, A, "win_func" },
68 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, A, "win_func" },
69 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, A, "win_func" },
70 { "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, A, "win_func" },
71 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, A, "win_func" },
72 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, A, "win_func" },
73 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, A, "win_func" },
74 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, A, "win_func" },
75 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, A, "win_func" },
76 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, A, "win_func" },
77 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, A, "win_func" },
78 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, A, "win_func" },
79 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, A, "win_func" },
80 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, A, "win_func" },
81 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, A, "win_func" },
82 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, A, "win_func" },
83 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, A, "win_func" },
84 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, A, "win_func" },
85 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, A, "win_func" },
86 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, A, "win_func" },
87 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, A, "win_func" },
88 { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, A, "win_func" },
89 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, A },
93 AVFILTER_DEFINE_CLASS(afftfilt);
95 static inline double getreal(void *priv, double x, double ch)
97 AFFTFiltContext *s = priv;
100 ich = av_clip(ch, 0, s->nb_exprs - 1);
101 ix = av_clip(x, 0, s->window_size / 2);
103 return s->fft_data[ich][ix].re;
106 static inline double getimag(void *priv, double x, double ch)
108 AFFTFiltContext *s = priv;
111 ich = av_clip(ch, 0, s->nb_exprs - 1);
112 ix = av_clip(x, 0, s->window_size / 2);
114 return s->fft_data[ich][ix].im;
117 static double realf(void *priv, double x, double ch) { return getreal(priv, x, ch); }
118 static double imagf(void *priv, double x, double ch) { return getimag(priv, x, ch); }
120 static const char *const func2_names[] = { "real", "imag", NULL };
121 static double (*const func2[])(void *, double, double) = { realf, imagf, NULL };
123 static int config_input(AVFilterLink *inlink)
125 AVFilterContext *ctx = inlink->dst;
126 AFFTFiltContext *s = ctx->priv;
127 char *saveptr = NULL;
131 const char *last_expr = "1";
133 s->channels = inlink->channels;
134 s->pts = AV_NOPTS_VALUE;
135 s->fft_bits = av_log2(s->fft_size);
136 s->fft = av_fft_init(s->fft_bits, 0);
137 s->ifft = av_fft_init(s->fft_bits, 1);
138 if (!s->fft || !s->ifft)
139 return AVERROR(ENOMEM);
141 s->window_size = 1 << s->fft_bits;
143 s->fft_data = av_calloc(inlink->channels, sizeof(*s->fft_data));
145 return AVERROR(ENOMEM);
147 s->fft_temp = av_calloc(inlink->channels, sizeof(*s->fft_temp));
149 return AVERROR(ENOMEM);
151 for (ch = 0; ch < inlink->channels; ch++) {
152 s->fft_data[ch] = av_calloc(s->window_size, sizeof(**s->fft_data));
153 if (!s->fft_data[ch])
154 return AVERROR(ENOMEM);
157 for (ch = 0; ch < inlink->channels; ch++) {
158 s->fft_temp[ch] = av_calloc(s->window_size, sizeof(**s->fft_temp));
159 if (!s->fft_temp[ch])
160 return AVERROR(ENOMEM);
163 s->real = av_calloc(inlink->channels, sizeof(*s->real));
165 return AVERROR(ENOMEM);
167 s->imag = av_calloc(inlink->channels, sizeof(*s->imag));
169 return AVERROR(ENOMEM);
171 args = av_strdup(s->real_str);
173 return AVERROR(ENOMEM);
175 for (ch = 0; ch < inlink->channels; ch++) {
176 char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
178 ret = av_expr_parse(&s->real[ch], arg ? arg : last_expr, var_names,
179 NULL, NULL, func2_names, func2, 0, ctx);
189 args = av_strdup(s->img_str ? s->img_str : s->real_str);
191 return AVERROR(ENOMEM);
195 for (ch = 0; ch < inlink->channels; ch++) {
196 char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
198 ret = av_expr_parse(&s->imag[ch], arg ? arg : last_expr, var_names,
199 NULL, NULL, func2_names, func2, 0, ctx);
208 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size);
210 return AVERROR(ENOMEM);
212 s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_size,
213 sizeof(*s->window_func_lut));
214 if (!s->window_func_lut)
215 return AVERROR(ENOMEM);
216 generate_window_func(s->window_func_lut, s->window_size, s->win_func, &overlap);
218 s->overlap = overlap;
220 s->hop_size = s->window_size * (1 - s->overlap);
221 if (s->hop_size <= 0)
222 return AVERROR(EINVAL);
224 s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
226 return AVERROR(ENOMEM);
234 static int filter_frame(AVFilterLink *inlink)
236 AVFilterContext *ctx = inlink->dst;
237 AVFilterLink *outlink = ctx->outputs[0];
238 AFFTFiltContext *s = ctx->priv;
239 const int window_size = s->window_size;
240 const float f = 1. / (s->window_size / 2);
241 double values[VAR_VARS_NB];
242 AVFrame *out, *in = NULL;
246 in = ff_get_audio_buffer(outlink, window_size);
248 return AVERROR(ENOMEM);
251 ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
255 for (ch = 0; ch < inlink->channels; ch++) {
256 const float *src = (float *)in->extended_data[ch];
257 FFTComplex *fft_data = s->fft_data[ch];
259 for (n = 0; n < in->nb_samples; n++) {
260 fft_data[n].re = src[n] * s->window_func_lut[n];
264 for (; n < window_size; n++) {
270 values[VAR_PTS] = s->pts;
271 values[VAR_SAMPLE_RATE] = inlink->sample_rate;
272 values[VAR_NBBINS] = window_size / 2;
273 values[VAR_CHANNELS] = inlink->channels;
275 for (ch = 0; ch < inlink->channels; ch++) {
276 FFTComplex *fft_data = s->fft_data[ch];
278 av_fft_permute(s->fft, fft_data);
279 av_fft_calc(s->fft, fft_data);
282 for (ch = 0; ch < inlink->channels; ch++) {
283 FFTComplex *fft_data = s->fft_data[ch];
284 FFTComplex *fft_temp = s->fft_temp[ch];
285 float *buf = (float *)s->buffer->extended_data[ch];
287 values[VAR_CHANNEL] = ch;
289 for (n = 0; n <= window_size / 2; n++) {
293 values[VAR_REAL] = fft_data[n].re;
294 values[VAR_IMAG] = fft_data[n].im;
296 fr = av_expr_eval(s->real[ch], values, s);
297 fi = av_expr_eval(s->imag[ch], values, s);
303 for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
304 fft_temp[n].re = fft_temp[x].re;
305 fft_temp[n].im = -fft_temp[x].im;
308 av_fft_permute(s->ifft, fft_temp);
309 av_fft_calc(s->ifft, fft_temp);
311 for (i = 0; i < window_size; i++) {
312 buf[i] += s->fft_temp[ch][i].re * f;
316 out = ff_get_audio_buffer(outlink, s->hop_size);
318 ret = AVERROR(ENOMEM);
323 s->pts += av_rescale_q(s->hop_size, (AVRational){1, outlink->sample_rate}, outlink->time_base);
325 for (ch = 0; ch < inlink->channels; ch++) {
326 float *dst = (float *)out->extended_data[ch];
327 float *buf = (float *)s->buffer->extended_data[ch];
329 for (n = 0; n < s->hop_size; n++)
330 dst[n] = buf[n] * (1.f - s->overlap);
331 memmove(buf, buf + s->hop_size, window_size * 4);
334 ret = ff_filter_frame(outlink, out);
338 av_audio_fifo_drain(s->fifo, s->hop_size);
342 return ret < 0 ? ret : 0;
345 static int activate(AVFilterContext *ctx)
347 AVFilterLink *inlink = ctx->inputs[0];
348 AVFilterLink *outlink = ctx->outputs[0];
349 AFFTFiltContext *s = ctx->priv;
354 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
356 if (!s->eof && av_audio_fifo_size(s->fifo) < s->window_size) {
357 ret = ff_inlink_consume_frame(inlink, &in);
362 ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
364 if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
373 if ((av_audio_fifo_size(s->fifo) >= s->window_size) ||
374 (av_audio_fifo_size(s->fifo) > 0 && s->eof)) {
375 ret = filter_frame(inlink);
376 if (av_audio_fifo_size(s->fifo) >= s->window_size)
377 ff_filter_set_ready(ctx, 100);
381 if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
382 if (status == AVERROR_EOF) {
384 if (av_audio_fifo_size(s->fifo) >= 0) {
385 ff_filter_set_ready(ctx, 100);
391 if (s->eof && av_audio_fifo_size(s->fifo) <= 0) {
392 ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
397 FF_FILTER_FORWARD_WANTED(outlink, inlink);
399 return FFERROR_NOT_READY;
402 static int query_formats(AVFilterContext *ctx)
404 AVFilterFormats *formats;
405 AVFilterChannelLayouts *layouts;
406 static const enum AVSampleFormat sample_fmts[] = {
412 layouts = ff_all_channel_counts();
414 return AVERROR(ENOMEM);
415 ret = ff_set_common_channel_layouts(ctx, layouts);
419 formats = ff_make_format_list(sample_fmts);
421 return AVERROR(ENOMEM);
422 ret = ff_set_common_formats(ctx, formats);
426 formats = ff_all_samplerates();
428 return AVERROR(ENOMEM);
429 return ff_set_common_samplerates(ctx, formats);
432 static av_cold void uninit(AVFilterContext *ctx)
434 AFFTFiltContext *s = ctx->priv;
440 for (i = 0; i < s->channels; i++) {
442 av_freep(&s->fft_data[i]);
444 av_freep(&s->fft_temp[i]);
446 av_freep(&s->fft_data);
447 av_freep(&s->fft_temp);
449 for (i = 0; i < s->nb_exprs; i++) {
450 av_expr_free(s->real[i]);
451 av_expr_free(s->imag[i]);
456 av_frame_free(&s->buffer);
457 av_freep(&s->window_func_lut);
459 av_audio_fifo_free(s->fifo);
462 static const AVFilterPad inputs[] = {
465 .type = AVMEDIA_TYPE_AUDIO,
466 .config_props = config_input,
471 static const AVFilterPad outputs[] = {
474 .type = AVMEDIA_TYPE_AUDIO,
479 AVFilter ff_af_afftfilt = {
481 .description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to samples in frequency domain."),
482 .priv_size = sizeof(AFFTFiltContext),
483 .priv_class = &afftfilt_class,
486 .activate = activate,
487 .query_formats = query_formats,