2015-08-04 12:41:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 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
|
|
|
|
*/
|
|
|
|
|
2016-08-17 19:56:42 +00:00
|
|
|
#include <float.h>
|
2015-08-04 12:41:35 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2021-07-30 07:24:49 +00:00
|
|
|
#include "libavutil/tx.h"
|
2015-08-04 12:41:35 +00:00
|
|
|
#include "libavutil/audio_fifo.h"
|
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/channel_layout.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
|
|
|
#include "audio.h"
|
2019-05-26 15:11:17 +00:00
|
|
|
#include "filters.h"
|
2015-08-04 12:41:35 +00:00
|
|
|
#include "video.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "internal.h"
|
2015-12-28 17:39:31 +00:00
|
|
|
#include "window_func.h"
|
2015-08-04 12:41:35 +00:00
|
|
|
|
2020-11-28 19:43:14 +00:00
|
|
|
enum DataMode { MAGNITUDE, PHASE, DELAY, NB_DATA };
|
2015-08-04 12:41:35 +00:00
|
|
|
enum DisplayMode { LINE, BAR, DOT, NB_MODES };
|
2015-12-20 18:52:51 +00:00
|
|
|
enum ChannelMode { COMBINED, SEPARATE, NB_CMODES };
|
2015-08-04 12:41:35 +00:00
|
|
|
enum FrequencyScale { FS_LINEAR, FS_LOG, FS_RLOG, NB_FSCALES };
|
|
|
|
enum AmplitudeScale { AS_LINEAR, AS_SQRT, AS_CBRT, AS_LOG, NB_ASCALES };
|
|
|
|
|
|
|
|
typedef struct ShowFreqsContext {
|
|
|
|
const AVClass *class;
|
|
|
|
int w, h;
|
|
|
|
int mode;
|
2020-11-26 22:26:04 +00:00
|
|
|
int data_mode;
|
2015-12-20 18:52:51 +00:00
|
|
|
int cmode;
|
2019-07-10 14:03:34 +00:00
|
|
|
int fft_size;
|
2015-08-04 12:41:35 +00:00
|
|
|
int ascale, fscale;
|
|
|
|
int avg;
|
|
|
|
int win_func;
|
2021-07-30 07:24:49 +00:00
|
|
|
AVTXContext *fft;
|
|
|
|
av_tx_fn tx_fn;
|
|
|
|
AVComplexFloat **fft_input;
|
|
|
|
AVComplexFloat **fft_data;
|
2015-08-04 12:41:35 +00:00
|
|
|
float **avg_data;
|
|
|
|
float *window_func_lut;
|
|
|
|
float overlap;
|
2016-08-17 19:56:42 +00:00
|
|
|
float minamp;
|
2016-01-18 18:38:14 +00:00
|
|
|
int hop_size;
|
2015-08-04 12:41:35 +00:00
|
|
|
int nb_channels;
|
|
|
|
int nb_freq;
|
|
|
|
int win_size;
|
|
|
|
float scale;
|
|
|
|
char *colors;
|
|
|
|
AVAudioFifo *fifo;
|
|
|
|
int64_t pts;
|
|
|
|
} ShowFreqsContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(ShowFreqsContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
|
|
|
static const AVOption showfreqs_options[] = {
|
|
|
|
{ "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
|
|
|
|
{ "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
|
|
|
|
{ "mode", "set display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=BAR}, 0, NB_MODES-1, FLAGS, "mode" },
|
|
|
|
{ "line", "show lines", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "mode" },
|
|
|
|
{ "bar", "show bars", 0, AV_OPT_TYPE_CONST, {.i64=BAR}, 0, 0, FLAGS, "mode" },
|
|
|
|
{ "dot", "show dots", 0, AV_OPT_TYPE_CONST, {.i64=DOT}, 0, 0, FLAGS, "mode" },
|
|
|
|
{ "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=AS_LOG}, 0, NB_ASCALES-1, FLAGS, "ascale" },
|
|
|
|
{ "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=AS_LINEAR}, 0, 0, FLAGS, "ascale" },
|
|
|
|
{ "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=AS_SQRT}, 0, 0, FLAGS, "ascale" },
|
|
|
|
{ "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=AS_CBRT}, 0, 0, FLAGS, "ascale" },
|
|
|
|
{ "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=AS_LOG}, 0, 0, FLAGS, "ascale" },
|
|
|
|
{ "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=FS_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
|
|
|
|
{ "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=FS_LINEAR}, 0, 0, FLAGS, "fscale" },
|
|
|
|
{ "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_LOG}, 0, 0, FLAGS, "fscale" },
|
|
|
|
{ "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_RLOG}, 0, 0, FLAGS, "fscale" },
|
2019-07-10 14:03:34 +00:00
|
|
|
{ "win_size", "set window size", OFFSET(fft_size), AV_OPT_TYPE_INT, {.i64=2048}, 16, 65536, FLAGS },
|
2015-08-04 12:41:35 +00:00
|
|
|
{ "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
|
|
|
|
{ "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
|
2015-09-15 09:13:53 +00:00
|
|
|
{ "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
|
2016-01-05 10:37:11 +00:00
|
|
|
{ "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
|
2016-08-16 12:30:43 +00:00
|
|
|
{ "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
|
2016-08-16 16:03:56 +00:00
|
|
|
{ "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
|
|
|
|
{ "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
|
2018-10-27 11:13:31 +00:00
|
|
|
{ "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN} , 0, 0, FLAGS, "win_func" },
|
2015-08-04 12:41:35 +00:00
|
|
|
{ "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=1.}, 0., 1., FLAGS },
|
|
|
|
{ "averaging", "set time averaging", OFFSET(avg), AV_OPT_TYPE_INT, {.i64=1}, 0, INT32_MAX, FLAGS },
|
|
|
|
{ "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
|
2015-12-20 18:52:51 +00:00
|
|
|
{ "cmode", "set channel mode", OFFSET(cmode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_CMODES-1, FLAGS, "cmode" },
|
|
|
|
{ "combined", "show all channels in same window", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "cmode" },
|
|
|
|
{ "separate", "show each channel in own window", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "cmode" },
|
2016-08-17 19:56:42 +00:00
|
|
|
{ "minamp", "set minimum amplitude", OFFSET(minamp), AV_OPT_TYPE_FLOAT, {.dbl=1e-6}, FLT_MIN, 1e-6, FLAGS },
|
2020-11-26 22:26:04 +00:00
|
|
|
{ "data", "set data mode", OFFSET(data_mode), AV_OPT_TYPE_INT, {.i64=MAGNITUDE}, 0, NB_DATA-1, FLAGS, "data" },
|
|
|
|
{ "magnitude", "show magnitude", 0, AV_OPT_TYPE_CONST, {.i64=MAGNITUDE}, 0, 0, FLAGS, "data" },
|
|
|
|
{ "phase", "show phase", 0, AV_OPT_TYPE_CONST, {.i64=PHASE}, 0, 0, FLAGS, "data" },
|
2020-11-28 19:43:14 +00:00
|
|
|
{ "delay", "show group delay",0, AV_OPT_TYPE_CONST, {.i64=DELAY}, 0, 0, FLAGS, "data" },
|
2015-08-04 12:41:35 +00:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(showfreqs);
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats = NULL;
|
|
|
|
AVFilterChannelLayouts *layouts = NULL;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
|
|
|
|
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
|
2015-10-05 03:39:25 +00:00
|
|
|
int ret;
|
2015-08-04 12:41:35 +00:00
|
|
|
|
|
|
|
/* set input audio formats */
|
|
|
|
formats = ff_make_format_list(sample_fmts);
|
2020-08-24 10:16:34 +00:00
|
|
|
if ((ret = ff_formats_ref(formats, &inlink->outcfg.formats)) < 0)
|
2015-10-05 03:39:25 +00:00
|
|
|
return ret;
|
2015-08-04 12:41:35 +00:00
|
|
|
|
|
|
|
layouts = ff_all_channel_layouts();
|
2020-08-24 10:16:34 +00:00
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &inlink->outcfg.channel_layouts)) < 0)
|
2015-10-05 03:39:25 +00:00
|
|
|
return ret;
|
2015-08-04 12:41:35 +00:00
|
|
|
|
|
|
|
formats = ff_all_samplerates();
|
2020-08-24 10:16:34 +00:00
|
|
|
if ((ret = ff_formats_ref(formats, &inlink->outcfg.samplerates)) < 0)
|
2015-10-05 03:39:25 +00:00
|
|
|
return ret;
|
2015-08-04 12:41:35 +00:00
|
|
|
|
|
|
|
/* set output video format */
|
|
|
|
formats = ff_make_format_list(pix_fmts);
|
2020-08-24 10:16:34 +00:00
|
|
|
if ((ret = ff_formats_ref(formats, &outlink->incfg.formats)) < 0)
|
2015-10-05 03:39:25 +00:00
|
|
|
return ret;
|
2015-08-04 12:41:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-31 20:38:49 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
ShowFreqsContext *s = ctx->priv;
|
|
|
|
|
|
|
|
s->pts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-04 12:41:35 +00:00
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
ShowFreqsContext *s = ctx->priv;
|
2021-07-30 07:24:49 +00:00
|
|
|
float overlap, scale;
|
|
|
|
int i, ret;
|
2015-08-04 12:41:35 +00:00
|
|
|
|
2021-07-30 07:24:49 +00:00
|
|
|
s->nb_freq = s->fft_size / 2;
|
|
|
|
s->win_size = s->fft_size;
|
2015-08-04 12:41:35 +00:00
|
|
|
av_audio_fifo_free(s->fifo);
|
2021-07-30 07:24:49 +00:00
|
|
|
av_tx_uninit(&s->fft);
|
|
|
|
ret = av_tx_init(&s->fft, &s->tx_fn, AV_TX_FLOAT_FFT, 0, s->fft_size, &scale, 0);
|
|
|
|
if (ret < 0) {
|
2015-08-04 12:41:35 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
|
|
|
|
"The window size might be too high.\n");
|
2021-07-30 07:24:49 +00:00
|
|
|
return ret;
|
2015-08-04 12:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FFT buffers: x2 for each (display) channel buffer.
|
|
|
|
* Note: we use free and malloc instead of a realloc-like function to
|
|
|
|
* make sure the buffer is aligned in memory for the FFT functions. */
|
|
|
|
for (i = 0; i < s->nb_channels; i++) {
|
2021-07-30 07:24:49 +00:00
|
|
|
av_freep(&s->fft_input[i]);
|
2015-08-04 12:41:35 +00:00
|
|
|
av_freep(&s->fft_data[i]);
|
|
|
|
av_freep(&s->avg_data[i]);
|
|
|
|
}
|
2021-07-30 07:24:49 +00:00
|
|
|
av_freep(&s->fft_input);
|
2015-08-04 12:41:35 +00:00
|
|
|
av_freep(&s->fft_data);
|
|
|
|
av_freep(&s->avg_data);
|
|
|
|
s->nb_channels = inlink->channels;
|
|
|
|
|
2021-07-30 07:24:49 +00:00
|
|
|
s->fft_input = av_calloc(s->nb_channels, sizeof(*s->fft_input));
|
|
|
|
if (!s->fft_input)
|
|
|
|
return AVERROR(ENOMEM);
|
2015-08-04 12:41:35 +00:00
|
|
|
s->fft_data = av_calloc(s->nb_channels, sizeof(*s->fft_data));
|
|
|
|
if (!s->fft_data)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
s->avg_data = av_calloc(s->nb_channels, sizeof(*s->avg_data));
|
2019-09-21 17:07:42 +00:00
|
|
|
if (!s->avg_data)
|
2015-08-04 12:41:35 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (i = 0; i < s->nb_channels; i++) {
|
2021-07-30 07:24:49 +00:00
|
|
|
s->fft_input[i] = av_calloc(FFALIGN(s->win_size, 512), sizeof(**s->fft_input));
|
|
|
|
s->fft_data[i] = av_calloc(FFALIGN(s->win_size, 512), sizeof(**s->fft_data));
|
2015-08-04 12:41:35 +00:00
|
|
|
s->avg_data[i] = av_calloc(s->nb_freq, sizeof(**s->avg_data));
|
2021-07-30 07:24:49 +00:00
|
|
|
if (!s->fft_data[i] || !s->avg_data[i] || !s->fft_input[i])
|
2015-08-04 12:41:35 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pre-calc windowing function */
|
|
|
|
s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_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->win_size, s->win_func, &overlap);
|
2015-08-04 12:41:35 +00:00
|
|
|
if (s->overlap == 1.)
|
|
|
|
s->overlap = overlap;
|
2016-01-18 18:38:14 +00:00
|
|
|
s->hop_size = (1. - s->overlap) * s->win_size;
|
|
|
|
if (s->hop_size < 1) {
|
2015-08-04 12:41:35 +00:00
|
|
|
av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (s->scale = 0, i = 0; i < s->win_size; i++) {
|
|
|
|
s->scale += s->window_func_lut[i] * s->window_func_lut[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
|
|
|
|
outlink->sample_aspect_ratio = (AVRational){1,1};
|
|
|
|
outlink->w = s->w;
|
|
|
|
outlink->h = s->h;
|
|
|
|
|
|
|
|
s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
|
|
|
|
if (!s->fifo)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void draw_dot(AVFrame *out, int x, int y, uint8_t fg[4])
|
|
|
|
{
|
|
|
|
|
|
|
|
uint32_t color = AV_RL32(out->data[0] + y * out->linesize[0] + x * 4);
|
|
|
|
|
|
|
|
if ((color & 0xffffff) != 0)
|
|
|
|
AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg) | color);
|
|
|
|
else
|
|
|
|
AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_sx(ShowFreqsContext *s, int f)
|
|
|
|
{
|
|
|
|
switch (s->fscale) {
|
|
|
|
case FS_LINEAR:
|
|
|
|
return (s->w/(float)s->nb_freq)*f;
|
|
|
|
case FS_LOG:
|
|
|
|
return s->w-pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.));
|
|
|
|
case FS_RLOG:
|
|
|
|
return pow(s->w, f/(s->nb_freq-1.));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float get_bsize(ShowFreqsContext *s, int f)
|
|
|
|
{
|
|
|
|
switch (s->fscale) {
|
|
|
|
case FS_LINEAR:
|
|
|
|
return s->w/(float)s->nb_freq;
|
|
|
|
case FS_LOG:
|
|
|
|
return pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.))-
|
|
|
|
pow(s->w, (s->nb_freq-f-2)/(s->nb_freq-1.));
|
|
|
|
case FS_RLOG:
|
|
|
|
return pow(s->w, (f+1)/(s->nb_freq-1.))-
|
|
|
|
pow(s->w, f /(s->nb_freq-1.));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1.;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void plot_freq(ShowFreqsContext *s, int ch,
|
|
|
|
double a, int f, uint8_t fg[4], int *prev_y,
|
|
|
|
AVFrame *out, AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
const int w = s->w;
|
2016-08-17 19:56:42 +00:00
|
|
|
const float min = s->minamp;
|
2015-08-04 12:41:35 +00:00
|
|
|
const float avg = s->avg_data[ch][f];
|
|
|
|
const float bsize = get_bsize(s, f);
|
|
|
|
const int sx = get_sx(s, f);
|
2015-12-20 18:52:51 +00:00
|
|
|
int end = outlink->h;
|
2015-08-04 12:41:35 +00:00
|
|
|
int x, y, i;
|
|
|
|
|
|
|
|
switch(s->ascale) {
|
|
|
|
case AS_SQRT:
|
|
|
|
a = 1.0 - sqrt(a);
|
|
|
|
break;
|
|
|
|
case AS_CBRT:
|
|
|
|
a = 1.0 - cbrt(a);
|
|
|
|
break;
|
|
|
|
case AS_LOG:
|
2016-08-17 19:56:42 +00:00
|
|
|
a = log(av_clipd(a, min, 1)) / log(min);
|
2015-08-04 12:41:35 +00:00
|
|
|
break;
|
|
|
|
case AS_LINEAR:
|
|
|
|
a = 1.0 - a;
|
|
|
|
break;
|
|
|
|
}
|
2015-12-20 18:52:51 +00:00
|
|
|
|
|
|
|
switch (s->cmode) {
|
|
|
|
case COMBINED:
|
|
|
|
y = a * outlink->h - 1;
|
|
|
|
break;
|
|
|
|
case SEPARATE:
|
|
|
|
end = (outlink->h / s->nb_channels) * (ch + 1);
|
|
|
|
y = (outlink->h / s->nb_channels) * ch + a * (outlink->h / s->nb_channels) - 1;
|
|
|
|
break;
|
2016-02-11 23:37:11 +00:00
|
|
|
default:
|
|
|
|
av_assert0(0);
|
2015-12-20 18:52:51 +00:00
|
|
|
}
|
2015-08-04 12:41:35 +00:00
|
|
|
if (y < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (s->avg) {
|
|
|
|
case 0:
|
2016-08-30 13:28:41 +00:00
|
|
|
y = s->avg_data[ch][f] = !outlink->frame_count_in ? y : FFMIN(avg, y);
|
2015-08-04 12:41:35 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
default:
|
2016-08-30 13:28:41 +00:00
|
|
|
s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outlink->frame_count_in + 1, s->avg) * y);
|
2015-08-04 12:41:35 +00:00
|
|
|
y = s->avg_data[ch][f];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(s->mode) {
|
|
|
|
case LINE:
|
|
|
|
if (*prev_y == -1) {
|
|
|
|
*prev_y = y;
|
|
|
|
}
|
|
|
|
if (y <= *prev_y) {
|
|
|
|
for (x = sx + 1; x < sx + bsize && x < w; x++)
|
|
|
|
draw_dot(out, x, y, fg);
|
|
|
|
for (i = y; i <= *prev_y; i++)
|
|
|
|
draw_dot(out, sx, i, fg);
|
|
|
|
} else {
|
|
|
|
for (i = *prev_y; i <= y; i++)
|
|
|
|
draw_dot(out, sx, i, fg);
|
|
|
|
for (x = sx + 1; x < sx + bsize && x < w; x++)
|
|
|
|
draw_dot(out, x, i - 1, fg);
|
|
|
|
}
|
|
|
|
*prev_y = y;
|
|
|
|
break;
|
|
|
|
case BAR:
|
|
|
|
for (x = sx; x < sx + bsize && x < w; x++)
|
2015-12-20 18:52:51 +00:00
|
|
|
for (i = y; i < end; i++)
|
2015-08-04 12:41:35 +00:00
|
|
|
draw_dot(out, x, i, fg);
|
|
|
|
break;
|
|
|
|
case DOT:
|
|
|
|
for (x = sx; x < sx + bsize && x < w; x++)
|
|
|
|
draw_dot(out, x, y, fg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int plot_freqs(AVFilterLink *inlink, AVFrame *in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
ShowFreqsContext *s = ctx->priv;
|
|
|
|
const int win_size = s->win_size;
|
|
|
|
char *colors, *color, *saveptr = NULL;
|
|
|
|
AVFrame *out;
|
|
|
|
int ch, n;
|
|
|
|
|
|
|
|
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
|
|
|
if (!out)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (n = 0; n < outlink->h; n++)
|
|
|
|
memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4);
|
|
|
|
|
|
|
|
/* fill FFT input with the number of samples available */
|
|
|
|
for (ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
const float *p = (float *)in->extended_data[ch];
|
|
|
|
|
|
|
|
for (n = 0; n < in->nb_samples; n++) {
|
2021-07-30 07:24:49 +00:00
|
|
|
s->fft_input[ch][n].re = p[n] * s->window_func_lut[n];
|
|
|
|
s->fft_input[ch][n].im = 0;
|
2015-08-04 12:41:35 +00:00
|
|
|
}
|
|
|
|
for (; n < win_size; n++) {
|
2021-07-30 07:24:49 +00:00
|
|
|
s->fft_input[ch][n].re = 0;
|
|
|
|
s->fft_input[ch][n].im = 0;
|
2015-08-04 12:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* run FFT on each samples set */
|
|
|
|
for (ch = 0; ch < s->nb_channels; ch++) {
|
2021-07-30 07:24:49 +00:00
|
|
|
s->tx_fn(s->fft, s->fft_data[ch], s->fft_input[ch], sizeof(float));
|
2015-08-04 12:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define RE(x, ch) s->fft_data[ch][x].re
|
|
|
|
#define IM(x, ch) s->fft_data[ch][x].im
|
|
|
|
#define M(a, b) (sqrt((a) * (a) + (b) * (b)))
|
2020-11-26 22:26:04 +00:00
|
|
|
#define P(a, b) (atan2((b), (a)))
|
2015-08-04 12:41:35 +00:00
|
|
|
|
|
|
|
colors = av_strdup(s->colors);
|
2015-09-05 13:35:37 +00:00
|
|
|
if (!colors) {
|
|
|
|
av_frame_free(&out);
|
2015-08-04 12:41:35 +00:00
|
|
|
return AVERROR(ENOMEM);
|
2015-09-05 13:35:37 +00:00
|
|
|
}
|
2015-08-04 12:41:35 +00:00
|
|
|
|
|
|
|
for (ch = 0; ch < s->nb_channels; ch++) {
|
|
|
|
uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
|
|
|
|
int prev_y = -1, f;
|
|
|
|
double a;
|
|
|
|
|
|
|
|
color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
|
|
|
|
if (color)
|
|
|
|
av_parse_color(fg, color, -1, ctx);
|
|
|
|
|
2020-11-26 22:26:04 +00:00
|
|
|
switch (s->data_mode) {
|
|
|
|
case MAGNITUDE:
|
|
|
|
a = av_clipd(M(RE(0, ch), 0) / s->scale, 0, 1);
|
|
|
|
plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink);
|
2015-08-04 12:41:35 +00:00
|
|
|
|
2020-11-26 22:26:04 +00:00
|
|
|
for (f = 1; f < s->nb_freq; f++) {
|
|
|
|
a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1);
|
2015-08-04 12:41:35 +00:00
|
|
|
|
2020-11-26 22:26:04 +00:00
|
|
|
plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PHASE:
|
|
|
|
a = av_clipd((M_PI + P(RE(0, ch), 0)) / (2. * M_PI), 0, 1);
|
|
|
|
plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink);
|
|
|
|
|
|
|
|
for (f = 1; f < s->nb_freq; f++) {
|
|
|
|
a = av_clipd((M_PI + P(RE(f, ch), IM(f, ch))) / (2. * M_PI), 0, 1);
|
|
|
|
|
|
|
|
plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
|
|
|
|
}
|
|
|
|
break;
|
2020-11-28 19:43:14 +00:00
|
|
|
case DELAY:
|
|
|
|
plot_freq(s, ch, 0, 0, fg, &prev_y, out, outlink);
|
|
|
|
|
|
|
|
for (f = 1; f < s->nb_freq; f++) {
|
|
|
|
a = av_clipd((M_PI - P(IM(f, ch) * RE(f-1, ch) - IM(f-1, ch) * RE(f, ch),
|
|
|
|
RE(f, ch) * RE(f-1, ch) + IM(f, ch) * IM(f-1, ch))) / (2. * M_PI), 0, 1);
|
|
|
|
|
|
|
|
plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
|
|
|
|
}
|
|
|
|
break;
|
2015-08-04 12:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_free(colors);
|
|
|
|
out->pts = in->pts;
|
2017-06-13 13:07:36 +00:00
|
|
|
out->sample_aspect_ratio = (AVRational){1,1};
|
2015-08-04 12:41:35 +00:00
|
|
|
return ff_filter_frame(outlink, out);
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:11:17 +00:00
|
|
|
static int filter_frame(AVFilterLink *inlink)
|
2015-08-04 12:41:35 +00:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
ShowFreqsContext *s = ctx->priv;
|
|
|
|
AVFrame *fin = NULL;
|
2015-09-05 13:34:35 +00:00
|
|
|
int ret = 0;
|
2015-08-04 12:41:35 +00:00
|
|
|
|
2019-05-26 15:11:17 +00:00
|
|
|
fin = ff_get_audio_buffer(inlink, s->win_size);
|
|
|
|
if (!fin) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2016-01-31 20:38:49 +00:00
|
|
|
|
2019-05-26 15:11:17 +00:00
|
|
|
fin->pts = s->pts;
|
|
|
|
s->pts += s->hop_size;
|
|
|
|
ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2015-08-04 12:41:35 +00:00
|
|
|
|
2019-05-26 15:11:17 +00:00
|
|
|
ret = plot_freqs(inlink, fin);
|
|
|
|
av_frame_free(&fin);
|
|
|
|
av_audio_fifo_drain(s->fifo, s->hop_size);
|
2015-08-04 12:41:35 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
av_frame_free(&fin);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-26 15:11:17 +00:00
|
|
|
static int activate(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
ShowFreqsContext *s = ctx->priv;
|
|
|
|
AVFrame *in = NULL;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
|
|
|
|
|
|
|
if (av_audio_fifo_size(s->fifo) < s->win_size)
|
|
|
|
ret = ff_inlink_consume_samples(inlink, s->win_size, s->win_size, &in);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (ret > 0) {
|
|
|
|
av_audio_fifo_write(s->fifo, (void **)in->extended_data, in->nb_samples);
|
|
|
|
if (s->pts == AV_NOPTS_VALUE)
|
|
|
|
s->pts = in->pts;
|
2019-10-26 04:21:56 +00:00
|
|
|
av_frame_free(&in);
|
2019-05-26 15:11:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (av_audio_fifo_size(s->fifo) >= s->win_size) {
|
|
|
|
ret = filter_frame(inlink);
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
FF_FILTER_FORWARD_STATUS(inlink, outlink);
|
|
|
|
FF_FILTER_FORWARD_WANTED(outlink, inlink);
|
|
|
|
|
|
|
|
return FFERROR_NOT_READY;
|
|
|
|
}
|
|
|
|
|
2015-08-04 12:41:35 +00:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
ShowFreqsContext *s = ctx->priv;
|
|
|
|
int i;
|
|
|
|
|
2021-07-30 07:24:49 +00:00
|
|
|
av_tx_uninit(&s->fft);
|
2015-08-04 12:41:35 +00:00
|
|
|
for (i = 0; i < s->nb_channels; i++) {
|
2021-07-30 07:24:49 +00:00
|
|
|
if (s->fft_input)
|
|
|
|
av_freep(&s->fft_input[i]);
|
2016-01-08 13:56:15 +00:00
|
|
|
if (s->fft_data)
|
|
|
|
av_freep(&s->fft_data[i]);
|
|
|
|
if (s->avg_data)
|
|
|
|
av_freep(&s->avg_data[i]);
|
2015-08-04 12:41:35 +00:00
|
|
|
}
|
2021-07-30 07:24:49 +00:00
|
|
|
av_freep(&s->fft_input);
|
2015-08-04 12:41:35 +00:00
|
|
|
av_freep(&s->fft_data);
|
|
|
|
av_freep(&s->avg_data);
|
|
|
|
av_freep(&s->window_func_lut);
|
|
|
|
av_audio_fifo_free(s->fifo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad showfreqs_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad showfreqs_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_avf_showfreqs = {
|
2015-08-04 12:41:35 +00:00
|
|
|
.name = "showfreqs",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Convert input audio to a frequencies video output."),
|
2016-01-31 20:38:49 +00:00
|
|
|
.init = init,
|
2015-08-04 12:41:35 +00:00
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.priv_size = sizeof(ShowFreqsContext),
|
2019-05-26 15:11:17 +00:00
|
|
|
.activate = activate,
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(showfreqs_inputs),
|
|
|
|
FILTER_OUTPUTS(showfreqs_outputs),
|
2015-08-04 12:41:35 +00:00
|
|
|
.priv_class = &showfreqs_class,
|
|
|
|
};
|