2016-01-16 14:09:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/audio_fifo.h"
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavfilter/internal.h"
|
|
|
|
#include "libavutil/common.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavcodec/avfft.h"
|
|
|
|
#include "libavutil/eval.h"
|
|
|
|
#include "audio.h"
|
2019-05-08 13:03:22 +00:00
|
|
|
#include "filters.h"
|
2016-01-16 14:09:25 +00:00
|
|
|
#include "window_func.h"
|
|
|
|
|
|
|
|
typedef struct AFFTFiltContext {
|
|
|
|
const AVClass *class;
|
|
|
|
char *real_str;
|
|
|
|
char *img_str;
|
|
|
|
int fft_bits;
|
|
|
|
|
|
|
|
FFTContext *fft, *ifft;
|
|
|
|
FFTComplex **fft_data;
|
2018-11-11 20:11:30 +00:00
|
|
|
FFTComplex **fft_temp;
|
2016-01-16 14:09:25 +00:00
|
|
|
int nb_exprs;
|
|
|
|
int window_size;
|
|
|
|
AVExpr **real;
|
|
|
|
AVExpr **imag;
|
|
|
|
AVAudioFifo *fifo;
|
|
|
|
int64_t pts;
|
|
|
|
int hop_size;
|
|
|
|
float overlap;
|
|
|
|
AVFrame *buffer;
|
2019-05-08 13:03:22 +00:00
|
|
|
int eof;
|
2016-01-16 14:09:25 +00:00
|
|
|
int win_func;
|
|
|
|
float *window_func_lut;
|
|
|
|
} AFFTFiltContext;
|
|
|
|
|
2018-11-11 20:11:30 +00:00
|
|
|
static const char *const var_names[] = { "sr", "b", "nb", "ch", "chs", "pts", "re", "im", NULL };
|
|
|
|
enum { VAR_SAMPLE_RATE, VAR_BIN, VAR_NBBINS, VAR_CHANNEL, VAR_CHANNELS, VAR_PTS, VAR_REAL, VAR_IMAG, VAR_VARS_NB };
|
2016-01-16 14:09:25 +00:00
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(AFFTFiltContext, x)
|
|
|
|
#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
|
|
|
|
|
|
|
static const AVOption afftfilt_options[] = {
|
2018-11-11 20:11:30 +00:00
|
|
|
{ "real", "set channels real expressions", OFFSET(real_str), AV_OPT_TYPE_STRING, {.str = "re" }, 0, 0, A },
|
|
|
|
{ "imag", "set channels imaginary expressions", OFFSET(img_str), AV_OPT_TYPE_STRING, {.str = "im" }, 0, 0, A },
|
2016-03-04 12:53:02 +00:00
|
|
|
{ "win_size", "set window size", OFFSET(fft_bits), AV_OPT_TYPE_INT, {.i64=12}, 4, 17, A, "fft" },
|
2016-01-16 14:09:25 +00:00
|
|
|
{ "w16", 0, 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, A, "fft" },
|
|
|
|
{ "w32", 0, 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, A, "fft" },
|
|
|
|
{ "w64", 0, 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, A, "fft" },
|
|
|
|
{ "w128", 0, 0, AV_OPT_TYPE_CONST, {.i64=7}, 0, 0, A, "fft" },
|
|
|
|
{ "w256", 0, 0, AV_OPT_TYPE_CONST, {.i64=8}, 0, 0, A, "fft" },
|
|
|
|
{ "w512", 0, 0, AV_OPT_TYPE_CONST, {.i64=9}, 0, 0, A, "fft" },
|
|
|
|
{ "w1024", 0, 0, AV_OPT_TYPE_CONST, {.i64=10}, 0, 0, A, "fft" },
|
|
|
|
{ "w2048", 0, 0, AV_OPT_TYPE_CONST, {.i64=11}, 0, 0, A, "fft" },
|
|
|
|
{ "w4096", 0, 0, AV_OPT_TYPE_CONST, {.i64=12}, 0, 0, A, "fft" },
|
|
|
|
{ "w8192", 0, 0, AV_OPT_TYPE_CONST, {.i64=13}, 0, 0, A, "fft" },
|
|
|
|
{ "w16384", 0, 0, AV_OPT_TYPE_CONST, {.i64=14}, 0, 0, A, "fft" },
|
|
|
|
{ "w32768", 0, 0, AV_OPT_TYPE_CONST, {.i64=15}, 0, 0, A, "fft" },
|
|
|
|
{ "w65536", 0, 0, AV_OPT_TYPE_CONST, {.i64=16}, 0, 0, A, "fft" },
|
2016-03-04 21:20:12 +00:00
|
|
|
{ "w131072",0, 0, AV_OPT_TYPE_CONST, {.i64=17}, 0, 0, A, "fft" },
|
2016-01-16 14:09:25 +00:00
|
|
|
{ "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = WFUNC_HANNING}, 0, NB_WFUNC-1, A, "win_func" },
|
|
|
|
{ "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, A, "win_func" },
|
|
|
|
{ "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, A, "win_func" },
|
|
|
|
{ "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, A, "win_func" },
|
|
|
|
{ "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, A, "win_func" },
|
|
|
|
{ "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, A, "win_func" },
|
2018-11-11 20:21:14 +00:00
|
|
|
{ "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, A, "win_func" },
|
|
|
|
{ "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, A, "win_func" },
|
|
|
|
{ "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, A, "win_func" },
|
|
|
|
{ "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, A, "win_func" },
|
|
|
|
{ "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, A, "win_func" },
|
|
|
|
{ "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, A, "win_func" },
|
2016-01-16 14:09:25 +00:00
|
|
|
{ "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, A, "win_func" },
|
2018-11-11 20:21:14 +00:00
|
|
|
{ "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, A, "win_func" },
|
|
|
|
{ "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, A, "win_func" },
|
|
|
|
{ "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, A, "win_func" },
|
|
|
|
{ "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, A, "win_func" },
|
|
|
|
{ "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, A, "win_func" },
|
|
|
|
{ "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, A, "win_func" },
|
|
|
|
{ "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, A, "win_func" },
|
|
|
|
{ "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, A, "win_func" },
|
|
|
|
{ "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN}, 0, 0, A, "win_func" },
|
2016-01-16 14:09:25 +00:00
|
|
|
{ "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, A },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(afftfilt);
|
|
|
|
|
2018-11-11 20:11:30 +00:00
|
|
|
static inline double getreal(void *priv, double x, double ch)
|
|
|
|
{
|
|
|
|
AFFTFiltContext *s = priv;
|
|
|
|
int ich, ix;
|
|
|
|
|
|
|
|
ich = av_clip(ch, 0, s->nb_exprs - 1);
|
|
|
|
ix = av_clip(x, 0, s->window_size / 2);
|
|
|
|
|
|
|
|
return s->fft_data[ich][ix].re;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline double getimag(void *priv, double x, double ch)
|
|
|
|
{
|
|
|
|
AFFTFiltContext *s = priv;
|
|
|
|
int ich, ix;
|
|
|
|
|
|
|
|
ich = av_clip(ch, 0, s->nb_exprs - 1);
|
|
|
|
ix = av_clip(x, 0, s->window_size / 2);
|
|
|
|
|
|
|
|
return s->fft_data[ich][ix].im;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double realf(void *priv, double x, double ch) { return getreal(priv, x, ch); }
|
|
|
|
static double imagf(void *priv, double x, double ch) { return getimag(priv, x, ch); }
|
|
|
|
|
|
|
|
static const char *const func2_names[] = { "real", "imag", NULL };
|
|
|
|
double (*func2[])(void *, double, double) = { realf, imagf, NULL };
|
|
|
|
|
2016-01-16 14:09:25 +00:00
|
|
|
static int config_input(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AFFTFiltContext *s = ctx->priv;
|
|
|
|
char *saveptr = NULL;
|
2019-07-06 10:01:38 +00:00
|
|
|
int ret = 0, ch;
|
2016-01-16 14:09:25 +00:00
|
|
|
float overlap;
|
2016-02-04 13:04:43 +00:00
|
|
|
char *args;
|
|
|
|
const char *last_expr = "1";
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2018-11-12 11:30:37 +00:00
|
|
|
s->pts = AV_NOPTS_VALUE;
|
2016-01-16 14:09:25 +00:00
|
|
|
s->fft = av_fft_init(s->fft_bits, 0);
|
|
|
|
s->ifft = av_fft_init(s->fft_bits, 1);
|
|
|
|
if (!s->fft || !s->ifft)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
s->window_size = 1 << s->fft_bits;
|
|
|
|
|
|
|
|
s->fft_data = av_calloc(inlink->channels, sizeof(*s->fft_data));
|
|
|
|
if (!s->fft_data)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2018-11-11 20:11:30 +00:00
|
|
|
s->fft_temp = av_calloc(inlink->channels, sizeof(*s->fft_temp));
|
|
|
|
if (!s->fft_temp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2016-01-16 14:09:25 +00:00
|
|
|
for (ch = 0; ch < inlink->channels; ch++) {
|
|
|
|
s->fft_data[ch] = av_calloc(s->window_size, sizeof(**s->fft_data));
|
|
|
|
if (!s->fft_data[ch])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2018-11-11 20:11:30 +00:00
|
|
|
for (ch = 0; ch < inlink->channels; ch++) {
|
|
|
|
s->fft_temp[ch] = av_calloc(s->window_size, sizeof(**s->fft_temp));
|
|
|
|
if (!s->fft_temp[ch])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2016-01-16 14:09:25 +00:00
|
|
|
s->real = av_calloc(inlink->channels, sizeof(*s->real));
|
|
|
|
if (!s->real)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
s->imag = av_calloc(inlink->channels, sizeof(*s->imag));
|
|
|
|
if (!s->imag)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
args = av_strdup(s->real_str);
|
|
|
|
if (!args)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (ch = 0; ch < inlink->channels; ch++) {
|
|
|
|
char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
|
|
|
|
|
|
|
|
ret = av_expr_parse(&s->real[ch], arg ? arg : last_expr, var_names,
|
2018-11-11 20:11:30 +00:00
|
|
|
NULL, NULL, func2_names, func2, 0, ctx);
|
2016-01-16 14:09:25 +00:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
if (arg)
|
|
|
|
last_expr = arg;
|
|
|
|
s->nb_exprs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_free(args);
|
|
|
|
|
|
|
|
args = av_strdup(s->img_str ? s->img_str : s->real_str);
|
|
|
|
if (!args)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (ch = 0; ch < inlink->channels; ch++) {
|
|
|
|
char *arg = av_strtok(ch == 0 ? args : NULL, "|", &saveptr);
|
|
|
|
|
|
|
|
ret = av_expr_parse(&s->imag[ch], arg ? arg : last_expr, var_names,
|
2018-11-11 20:11:30 +00:00
|
|
|
NULL, NULL, func2_names, func2, 0, ctx);
|
2016-01-16 14:09:25 +00:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
if (arg)
|
|
|
|
last_expr = arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_free(args);
|
|
|
|
|
|
|
|
s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size);
|
|
|
|
if (!s->fifo)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_size,
|
|
|
|
sizeof(*s->window_func_lut));
|
|
|
|
if (!s->window_func_lut)
|
|
|
|
return AVERROR(ENOMEM);
|
2017-09-23 13:21:22 +00:00
|
|
|
generate_window_func(s->window_func_lut, s->window_size, s->win_func, &overlap);
|
2016-01-16 14:09:25 +00:00
|
|
|
if (s->overlap == 1)
|
|
|
|
s->overlap = overlap;
|
|
|
|
|
|
|
|
s->hop_size = s->window_size * (1 - s->overlap);
|
|
|
|
if (s->hop_size <= 0)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
|
|
|
|
if (!s->buffer)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
static int filter_frame(AVFilterLink *inlink)
|
2016-01-16 14:09:25 +00:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
AFFTFiltContext *s = ctx->priv;
|
|
|
|
const int window_size = s->window_size;
|
2019-07-06 10:01:38 +00:00
|
|
|
const float f = 1. / (s->window_size / 2);
|
2016-01-16 14:09:25 +00:00
|
|
|
double values[VAR_VARS_NB];
|
|
|
|
AVFrame *out, *in = NULL;
|
2019-05-08 13:03:22 +00:00
|
|
|
int ch, n, ret, i;
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
if (!in) {
|
|
|
|
in = ff_get_audio_buffer(outlink, window_size);
|
|
|
|
if (!in)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2018-11-12 11:30:37 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
|
2017-11-20 12:42:59 +00:00
|
|
|
if (ret < 0)
|
2019-05-08 13:03:22 +00:00
|
|
|
goto fail;
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
for (ch = 0; ch < inlink->channels; ch++) {
|
|
|
|
const float *src = (float *)in->extended_data[ch];
|
|
|
|
FFTComplex *fft_data = s->fft_data[ch];
|
|
|
|
|
|
|
|
for (n = 0; n < in->nb_samples; n++) {
|
|
|
|
fft_data[n].re = src[n] * s->window_func_lut[n];
|
|
|
|
fft_data[n].im = 0;
|
2016-01-16 14:09:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
for (; n < window_size; n++) {
|
|
|
|
fft_data[n].re = 0;
|
|
|
|
fft_data[n].im = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
values[VAR_PTS] = s->pts;
|
|
|
|
values[VAR_SAMPLE_RATE] = inlink->sample_rate;
|
|
|
|
values[VAR_NBBINS] = window_size / 2;
|
|
|
|
values[VAR_CHANNELS] = inlink->channels;
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
for (ch = 0; ch < inlink->channels; ch++) {
|
|
|
|
FFTComplex *fft_data = s->fft_data[ch];
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
av_fft_permute(s->fft, fft_data);
|
|
|
|
av_fft_calc(s->fft, fft_data);
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
for (ch = 0; ch < inlink->channels; ch++) {
|
|
|
|
FFTComplex *fft_data = s->fft_data[ch];
|
|
|
|
FFTComplex *fft_temp = s->fft_temp[ch];
|
|
|
|
float *buf = (float *)s->buffer->extended_data[ch];
|
|
|
|
int x;
|
|
|
|
values[VAR_CHANNEL] = ch;
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
for (n = 0; n <= window_size / 2; n++) {
|
|
|
|
float fr, fi;
|
2018-11-12 08:27:08 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
values[VAR_BIN] = n;
|
|
|
|
values[VAR_REAL] = fft_data[n].re;
|
|
|
|
values[VAR_IMAG] = fft_data[n].im;
|
2018-11-12 08:27:08 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
fr = av_expr_eval(s->real[ch], values, s);
|
|
|
|
fi = av_expr_eval(s->imag[ch], values, s);
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
fft_temp[n].re = fr;
|
|
|
|
fft_temp[n].im = fi;
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
|
|
|
|
fft_temp[n].re = fft_temp[x].re;
|
|
|
|
fft_temp[n].im = -fft_temp[x].im;
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
av_fft_permute(s->ifft, fft_temp);
|
|
|
|
av_fft_calc(s->ifft, fft_temp);
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
for (i = 0; i < window_size; i++) {
|
|
|
|
buf[i] += s->fft_temp[ch][i].re * f;
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
out = ff_get_audio_buffer(outlink, s->hop_size);
|
|
|
|
if (!out) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
out->pts = s->pts;
|
|
|
|
s->pts += s->hop_size;
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
for (ch = 0; ch < inlink->channels; ch++) {
|
|
|
|
float *dst = (float *)out->extended_data[ch];
|
|
|
|
float *buf = (float *)s->buffer->extended_data[ch];
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
for (n = 0; n < s->hop_size; n++)
|
|
|
|
dst[n] = buf[n] * (1.f - s->overlap);
|
|
|
|
memmove(buf, buf + s->hop_size, window_size * 4);
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
ret = ff_filter_frame(outlink, out);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
av_audio_fifo_drain(s->fifo, s->hop_size);
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
fail:
|
|
|
|
av_frame_free(&in);
|
|
|
|
return ret < 0 ? ret : 0;
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
static int activate(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
AFFTFiltContext *s = ctx->priv;
|
|
|
|
AVFrame *in = NULL;
|
|
|
|
int ret = 0, status;
|
|
|
|
int64_t pts;
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
if (!s->eof && av_audio_fifo_size(s->fifo) < s->window_size) {
|
|
|
|
ret = ff_inlink_consume_frame(inlink, &in);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
if (ret > 0) {
|
|
|
|
ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
|
|
|
|
in->nb_samples);
|
|
|
|
if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
|
|
|
|
s->pts = in->pts;
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
av_frame_free(&in);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
if ((av_audio_fifo_size(s->fifo) >= s->window_size) ||
|
|
|
|
(av_audio_fifo_size(s->fifo) > 0 && s->eof)) {
|
|
|
|
ret = filter_frame(inlink);
|
|
|
|
if (av_audio_fifo_size(s->fifo) >= s->window_size)
|
|
|
|
ff_filter_set_ready(ctx, 100);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
|
|
|
|
if (status == AVERROR_EOF) {
|
|
|
|
s->eof = 1;
|
|
|
|
if (av_audio_fifo_size(s->fifo) >= 0) {
|
|
|
|
ff_filter_set_ready(ctx, 100);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
}
|
2019-05-08 13:03:22 +00:00
|
|
|
}
|
2016-01-16 14:09:25 +00:00
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
if (s->eof && av_audio_fifo_size(s->fifo) <= 0) {
|
|
|
|
ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
|
|
|
|
return 0;
|
2016-01-16 14:09:25 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 13:03:22 +00:00
|
|
|
if (!s->eof)
|
|
|
|
FF_FILTER_FORWARD_WANTED(outlink, inlink);
|
|
|
|
|
|
|
|
return FFERROR_NOT_READY;
|
2016-01-16 14:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats;
|
|
|
|
AVFilterChannelLayouts *layouts;
|
|
|
|
static const enum AVSampleFormat sample_fmts[] = {
|
|
|
|
AV_SAMPLE_FMT_FLTP,
|
|
|
|
AV_SAMPLE_FMT_NONE
|
|
|
|
};
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
layouts = ff_all_channel_counts();
|
|
|
|
if (!layouts)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = ff_set_common_channel_layouts(ctx, layouts);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
formats = ff_make_format_list(sample_fmts);
|
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = ff_set_common_formats(ctx, formats);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
formats = ff_all_samplerates();
|
|
|
|
if (!formats)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
return ff_set_common_samplerates(ctx, formats);
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AFFTFiltContext *s = ctx->priv;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
av_fft_end(s->fft);
|
|
|
|
av_fft_end(s->ifft);
|
|
|
|
|
|
|
|
for (i = 0; i < s->nb_exprs; i++) {
|
|
|
|
if (s->fft_data)
|
|
|
|
av_freep(&s->fft_data[i]);
|
2018-11-11 20:11:30 +00:00
|
|
|
if (s->fft_temp)
|
|
|
|
av_freep(&s->fft_temp[i]);
|
2016-01-16 14:09:25 +00:00
|
|
|
}
|
|
|
|
av_freep(&s->fft_data);
|
2018-11-11 20:11:30 +00:00
|
|
|
av_freep(&s->fft_temp);
|
2016-01-16 14:09:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i < s->nb_exprs; i++) {
|
|
|
|
av_expr_free(s->real[i]);
|
|
|
|
av_expr_free(s->imag[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_freep(&s->real);
|
|
|
|
av_freep(&s->imag);
|
|
|
|
av_frame_free(&s->buffer);
|
2017-06-04 20:51:57 +00:00
|
|
|
av_freep(&s->window_func_lut);
|
|
|
|
|
|
|
|
av_audio_fifo_free(s->fifo);
|
2016-01-16 14:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFilter ff_af_afftfilt = {
|
|
|
|
.name = "afftfilt",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Apply arbitrary expressions to samples in frequency domain."),
|
|
|
|
.priv_size = sizeof(AFFTFiltContext),
|
|
|
|
.priv_class = &afftfilt_class,
|
|
|
|
.inputs = inputs,
|
|
|
|
.outputs = outputs,
|
2019-05-08 13:03:22 +00:00
|
|
|
.activate = activate,
|
2016-01-16 14:09:25 +00:00
|
|
|
.query_formats = query_formats,
|
|
|
|
.uninit = uninit,
|
|
|
|
};
|