2 * Copyright (c) 2015 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
8 * License 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 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
24 #include "libavcodec/avfft.h"
25 #include "libavutil/audio_fifo.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/parseutils.h"
37 #include "window_func.h"
39 enum DisplayMode { LINE, BAR, DOT, NB_MODES };
40 enum ChannelMode { COMBINED, SEPARATE, NB_CMODES };
41 enum FrequencyScale { FS_LINEAR, FS_LOG, FS_RLOG, NB_FSCALES };
42 enum AmplitudeScale { AS_LINEAR, AS_SQRT, AS_CBRT, AS_LOG, NB_ASCALES };
44 typedef struct ShowFreqsContext {
55 FFTComplex **fft_data;
57 float *window_func_lut;
70 #define OFFSET(x) offsetof(ShowFreqsContext, x)
71 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
73 static const AVOption showfreqs_options[] = {
74 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
75 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "1024x512"}, 0, 0, FLAGS },
76 { "mode", "set display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=BAR}, 0, NB_MODES-1, FLAGS, "mode" },
77 { "line", "show lines", 0, AV_OPT_TYPE_CONST, {.i64=LINE}, 0, 0, FLAGS, "mode" },
78 { "bar", "show bars", 0, AV_OPT_TYPE_CONST, {.i64=BAR}, 0, 0, FLAGS, "mode" },
79 { "dot", "show dots", 0, AV_OPT_TYPE_CONST, {.i64=DOT}, 0, 0, FLAGS, "mode" },
80 { "ascale", "set amplitude scale", OFFSET(ascale), AV_OPT_TYPE_INT, {.i64=AS_LOG}, 0, NB_ASCALES-1, FLAGS, "ascale" },
81 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=AS_LINEAR}, 0, 0, FLAGS, "ascale" },
82 { "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=AS_SQRT}, 0, 0, FLAGS, "ascale" },
83 { "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=AS_CBRT}, 0, 0, FLAGS, "ascale" },
84 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=AS_LOG}, 0, 0, FLAGS, "ascale" },
85 { "fscale", "set frequency scale", OFFSET(fscale), AV_OPT_TYPE_INT, {.i64=FS_LINEAR}, 0, NB_FSCALES-1, FLAGS, "fscale" },
86 { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=FS_LINEAR}, 0, 0, FLAGS, "fscale" },
87 { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_LOG}, 0, 0, FLAGS, "fscale" },
88 { "rlog", "reverse logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=FS_RLOG}, 0, 0, FLAGS, "fscale" },
89 { "win_size", "set window size", OFFSET(fft_size), AV_OPT_TYPE_INT, {.i64=2048}, 16, 65536, FLAGS },
90 { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64=WFUNC_HANNING}, 0, NB_WFUNC-1, FLAGS, "win_func" },
91 { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, FLAGS, "win_func" },
92 { "bartlett", "Bartlett", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BARTLETT}, 0, 0, FLAGS, "win_func" },
93 { "hanning", "Hanning", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 0, 0, FLAGS, "win_func" },
94 { "hamming", "Hamming", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HAMMING}, 0, 0, FLAGS, "win_func" },
95 { "blackman", "Blackman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BLACKMAN}, 0, 0, FLAGS, "win_func" },
96 { "welch", "Welch", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_WELCH}, 0, 0, FLAGS, "win_func" },
97 { "flattop", "Flat-top", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_FLATTOP}, 0, 0, FLAGS, "win_func" },
98 { "bharris", "Blackman-Harris", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHARRIS}, 0, 0, FLAGS, "win_func" },
99 { "bnuttall", "Blackman-Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BNUTTALL}, 0, 0, FLAGS, "win_func" },
100 { "bhann", "Bartlett-Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BHANN}, 0, 0, FLAGS, "win_func" },
101 { "sine", "Sine", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_SINE}, 0, 0, FLAGS, "win_func" },
102 { "nuttall", "Nuttall", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_NUTTALL}, 0, 0, FLAGS, "win_func" },
103 { "lanczos", "Lanczos", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_LANCZOS}, 0, 0, FLAGS, "win_func" },
104 { "gauss", "Gauss", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_GAUSS}, 0, 0, FLAGS, "win_func" },
105 { "tukey", "Tukey", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_TUKEY}, 0, 0, FLAGS, "win_func" },
106 { "dolph", "Dolph-Chebyshev", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_DOLPH}, 0, 0, FLAGS, "win_func" },
107 { "cauchy", "Cauchy", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_CAUCHY}, 0, 0, FLAGS, "win_func" },
108 { "parzen", "Parzen", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_PARZEN}, 0, 0, FLAGS, "win_func" },
109 { "poisson", "Poisson", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_POISSON}, 0, 0, FLAGS, "win_func" },
110 { "bohman", "Bohman", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_BOHMAN} , 0, 0, FLAGS, "win_func" },
111 { "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl=1.}, 0., 1., FLAGS },
112 { "averaging", "set time averaging", OFFSET(avg), AV_OPT_TYPE_INT, {.i64=1}, 0, INT32_MAX, FLAGS },
113 { "colors", "set channels colors", OFFSET(colors), AV_OPT_TYPE_STRING, {.str = "red|green|blue|yellow|orange|lime|pink|magenta|brown" }, 0, 0, FLAGS },
114 { "cmode", "set channel mode", OFFSET(cmode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_CMODES-1, FLAGS, "cmode" },
115 { "combined", "show all channels in same window", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "cmode" },
116 { "separate", "show each channel in own window", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "cmode" },
117 { "minamp", "set minimum amplitude", OFFSET(minamp), AV_OPT_TYPE_FLOAT, {.dbl=1e-6}, FLT_MIN, 1e-6, FLAGS },
121 AVFILTER_DEFINE_CLASS(showfreqs);
123 static int query_formats(AVFilterContext *ctx)
125 AVFilterFormats *formats = NULL;
126 AVFilterChannelLayouts *layouts = NULL;
127 AVFilterLink *inlink = ctx->inputs[0];
128 AVFilterLink *outlink = ctx->outputs[0];
129 static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
130 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
133 /* set input audio formats */
134 formats = ff_make_format_list(sample_fmts);
135 if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
138 layouts = ff_all_channel_layouts();
139 if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
142 formats = ff_all_samplerates();
143 if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
146 /* set output video format */
147 formats = ff_make_format_list(pix_fmts);
148 if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
154 static av_cold int init(AVFilterContext *ctx)
156 ShowFreqsContext *s = ctx->priv;
158 s->pts = AV_NOPTS_VALUE;
163 static int config_output(AVFilterLink *outlink)
165 AVFilterContext *ctx = outlink->src;
166 AVFilterLink *inlink = ctx->inputs[0];
167 ShowFreqsContext *s = ctx->priv;
171 s->fft_bits = av_log2(s->fft_size);
172 s->nb_freq = 1 << (s->fft_bits - 1);
173 s->win_size = s->nb_freq << 1;
174 av_audio_fifo_free(s->fifo);
176 s->fft = av_fft_init(s->fft_bits, 0);
178 av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
179 "The window size might be too high.\n");
180 return AVERROR(ENOMEM);
183 /* FFT buffers: x2 for each (display) channel buffer.
184 * Note: we use free and malloc instead of a realloc-like function to
185 * make sure the buffer is aligned in memory for the FFT functions. */
186 for (i = 0; i < s->nb_channels; i++) {
187 av_freep(&s->fft_data[i]);
188 av_freep(&s->avg_data[i]);
190 av_freep(&s->fft_data);
191 av_freep(&s->avg_data);
192 s->nb_channels = inlink->channels;
194 s->fft_data = av_calloc(s->nb_channels, sizeof(*s->fft_data));
196 return AVERROR(ENOMEM);
197 s->avg_data = av_calloc(s->nb_channels, sizeof(*s->avg_data));
199 return AVERROR(ENOMEM);
200 for (i = 0; i < s->nb_channels; i++) {
201 s->fft_data[i] = av_calloc(s->win_size, sizeof(**s->fft_data));
202 s->avg_data[i] = av_calloc(s->nb_freq, sizeof(**s->avg_data));
203 if (!s->fft_data[i] || !s->avg_data[i])
204 return AVERROR(ENOMEM);
207 /* pre-calc windowing function */
208 s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size,
209 sizeof(*s->window_func_lut));
210 if (!s->window_func_lut)
211 return AVERROR(ENOMEM);
212 generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
213 if (s->overlap == 1.)
214 s->overlap = overlap;
215 s->hop_size = (1. - s->overlap) * s->win_size;
216 if (s->hop_size < 1) {
217 av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
218 return AVERROR(EINVAL);
221 for (s->scale = 0, i = 0; i < s->win_size; i++) {
222 s->scale += s->window_func_lut[i] * s->window_func_lut[i];
225 outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
226 outlink->sample_aspect_ratio = (AVRational){1,1};
230 s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
232 return AVERROR(ENOMEM);
236 static inline void draw_dot(AVFrame *out, int x, int y, uint8_t fg[4])
239 uint32_t color = AV_RL32(out->data[0] + y * out->linesize[0] + x * 4);
241 if ((color & 0xffffff) != 0)
242 AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg) | color);
244 AV_WL32(out->data[0] + y * out->linesize[0] + x * 4, AV_RL32(fg));
247 static int get_sx(ShowFreqsContext *s, int f)
251 return (s->w/(float)s->nb_freq)*f;
253 return s->w-pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.));
255 return pow(s->w, f/(s->nb_freq-1.));
261 static float get_bsize(ShowFreqsContext *s, int f)
265 return s->w/(float)s->nb_freq;
267 return pow(s->w, (s->nb_freq-f-1)/(s->nb_freq-1.))-
268 pow(s->w, (s->nb_freq-f-2)/(s->nb_freq-1.));
270 return pow(s->w, (f+1)/(s->nb_freq-1.))-
271 pow(s->w, f /(s->nb_freq-1.));
277 static inline void plot_freq(ShowFreqsContext *s, int ch,
278 double a, int f, uint8_t fg[4], int *prev_y,
279 AVFrame *out, AVFilterLink *outlink)
282 const float min = s->minamp;
283 const float avg = s->avg_data[ch][f];
284 const float bsize = get_bsize(s, f);
285 const int sx = get_sx(s, f);
286 int end = outlink->h;
297 a = log(av_clipd(a, min, 1)) / log(min);
306 y = a * outlink->h - 1;
309 end = (outlink->h / s->nb_channels) * (ch + 1);
310 y = (outlink->h / s->nb_channels) * ch + a * (outlink->h / s->nb_channels) - 1;
320 y = s->avg_data[ch][f] = !outlink->frame_count_in ? y : FFMIN(avg, y);
325 s->avg_data[ch][f] = avg + y * (y - avg) / (FFMIN(outlink->frame_count_in + 1, s->avg) * y);
326 y = s->avg_data[ch][f];
336 for (x = sx + 1; x < sx + bsize && x < w; x++)
337 draw_dot(out, x, y, fg);
338 for (i = y; i <= *prev_y; i++)
339 draw_dot(out, sx, i, fg);
341 for (i = *prev_y; i <= y; i++)
342 draw_dot(out, sx, i, fg);
343 for (x = sx + 1; x < sx + bsize && x < w; x++)
344 draw_dot(out, x, i - 1, fg);
349 for (x = sx; x < sx + bsize && x < w; x++)
350 for (i = y; i < end; i++)
351 draw_dot(out, x, i, fg);
354 for (x = sx; x < sx + bsize && x < w; x++)
355 draw_dot(out, x, y, fg);
360 static int plot_freqs(AVFilterLink *inlink, AVFrame *in)
362 AVFilterContext *ctx = inlink->dst;
363 AVFilterLink *outlink = ctx->outputs[0];
364 ShowFreqsContext *s = ctx->priv;
365 const int win_size = s->win_size;
366 char *colors, *color, *saveptr = NULL;
370 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
372 return AVERROR(ENOMEM);
374 for (n = 0; n < outlink->h; n++)
375 memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4);
377 /* fill FFT input with the number of samples available */
378 for (ch = 0; ch < s->nb_channels; ch++) {
379 const float *p = (float *)in->extended_data[ch];
381 for (n = 0; n < in->nb_samples; n++) {
382 s->fft_data[ch][n].re = p[n] * s->window_func_lut[n];
383 s->fft_data[ch][n].im = 0;
385 for (; n < win_size; n++) {
386 s->fft_data[ch][n].re = 0;
387 s->fft_data[ch][n].im = 0;
391 /* run FFT on each samples set */
392 for (ch = 0; ch < s->nb_channels; ch++) {
393 av_fft_permute(s->fft, s->fft_data[ch]);
394 av_fft_calc(s->fft, s->fft_data[ch]);
397 #define RE(x, ch) s->fft_data[ch][x].re
398 #define IM(x, ch) s->fft_data[ch][x].im
399 #define M(a, b) (sqrt((a) * (a) + (b) * (b)))
401 colors = av_strdup(s->colors);
404 return AVERROR(ENOMEM);
407 for (ch = 0; ch < s->nb_channels; ch++) {
408 uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff };
412 color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr);
414 av_parse_color(fg, color, -1, ctx);
416 a = av_clipd(M(RE(0, ch), 0) / s->scale, 0, 1);
417 plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink);
419 for (f = 1; f < s->nb_freq; f++) {
420 a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1);
422 plot_freq(s, ch, a, f, fg, &prev_y, out, outlink);
428 out->sample_aspect_ratio = (AVRational){1,1};
429 return ff_filter_frame(outlink, out);
432 static int filter_frame(AVFilterLink *inlink)
434 AVFilterContext *ctx = inlink->dst;
435 ShowFreqsContext *s = ctx->priv;
439 fin = ff_get_audio_buffer(inlink, s->win_size);
441 ret = AVERROR(ENOMEM);
446 s->pts += s->hop_size;
447 ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
451 ret = plot_freqs(inlink, fin);
453 av_audio_fifo_drain(s->fifo, s->hop_size);
460 static int activate(AVFilterContext *ctx)
462 AVFilterLink *inlink = ctx->inputs[0];
463 AVFilterLink *outlink = ctx->outputs[0];
464 ShowFreqsContext *s = ctx->priv;
468 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
470 if (av_audio_fifo_size(s->fifo) < s->win_size)
471 ret = ff_inlink_consume_samples(inlink, s->win_size, s->win_size, &in);
475 av_audio_fifo_write(s->fifo, (void **)in->extended_data, in->nb_samples);
476 if (s->pts == AV_NOPTS_VALUE)
480 if (av_audio_fifo_size(s->fifo) >= s->win_size) {
481 ret = filter_frame(inlink);
486 FF_FILTER_FORWARD_STATUS(inlink, outlink);
487 FF_FILTER_FORWARD_WANTED(outlink, inlink);
489 return FFERROR_NOT_READY;
492 static av_cold void uninit(AVFilterContext *ctx)
494 ShowFreqsContext *s = ctx->priv;
498 for (i = 0; i < s->nb_channels; i++) {
500 av_freep(&s->fft_data[i]);
502 av_freep(&s->avg_data[i]);
504 av_freep(&s->fft_data);
505 av_freep(&s->avg_data);
506 av_freep(&s->window_func_lut);
507 av_audio_fifo_free(s->fifo);
510 static const AVFilterPad showfreqs_inputs[] = {
513 .type = AVMEDIA_TYPE_AUDIO,
518 static const AVFilterPad showfreqs_outputs[] = {
521 .type = AVMEDIA_TYPE_VIDEO,
522 .config_props = config_output,
527 AVFilter ff_avf_showfreqs = {
529 .description = NULL_IF_CONFIG_SMALL("Convert input audio to a frequencies video output."),
532 .query_formats = query_formats,
533 .priv_size = sizeof(ShowFreqsContext),
534 .activate = activate,
535 .inputs = showfreqs_inputs,
536 .outputs = showfreqs_outputs,
537 .priv_class = &showfreqs_class,