[FFmpeg-devel] [PATCH 3/3] avfilter: add spectrumsynth filter
Paul B Mahol
onemda at gmail.com
Mon Jan 11 00:21:53 CET 2016
Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
Only gray input supported, chroma is ignored. (float pixel format would come handy)
Only fullframe slide mode implemented.
No overlaping and only rectangular window for now.
Only power of 2 height supported and tested.
---
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vaf_spectrumsynth.c | 362 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 364 insertions(+)
create mode 100644 libavfilter/vaf_spectrumsynth.c
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 689da73..0704c4e 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -290,6 +290,7 @@ OBJS-$(CONFIG_SHOWSPECTRUMPIC_FILTER) += avf_showspectrum.o window_func.o
OBJS-$(CONFIG_SHOWVOLUME_FILTER) += avf_showvolume.o
OBJS-$(CONFIG_SHOWWAVES_FILTER) += avf_showwaves.o
OBJS-$(CONFIG_SHOWWAVESPIC_FILTER) += avf_showwaves.o
+OBJS-$(CONFIG_SPECTRUMSYNTH_FILTER) += vaf_spectrumsynth.o
# multimedia sources
OBJS-$(CONFIG_AMOVIE_FILTER) += src_movie.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 2267e88..d4815d6 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -310,6 +310,7 @@ void avfilter_register_all(void)
REGISTER_FILTER(SHOWVOLUME, showvolume, avf);
REGISTER_FILTER(SHOWWAVES, showwaves, avf);
REGISTER_FILTER(SHOWWAVESPIC, showwavespic, avf);
+ REGISTER_FILTER(SPECTRUMSYNTH, spectrumsynth, vaf);
/* multimedia sources */
REGISTER_FILTER(AMOVIE, amovie, avsrc);
diff --git a/libavfilter/vaf_spectrumsynth.c b/libavfilter/vaf_spectrumsynth.c
new file mode 100644
index 0000000..aaafce2
--- /dev/null
+++ b/libavfilter/vaf_spectrumsynth.c
@@ -0,0 +1,362 @@
+/*
+ * 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 "libavcodec/avfft.h"
+#include "libavutil/avassert.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/opt.h"
+#include "libavutil/parseutils.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "audio.h"
+#include "video.h"
+#include "internal.h"
+#include "window_func.h"
+
+enum MagnitudeScale { LINEAR, LOG, NB_SCALES };
+
+typedef struct SpectrumSynthContext {
+ const AVClass *class;
+ int sample_rate;
+ int channels;
+ int scale;
+ int win_func;
+
+ AVFrame *magnitude, *phase;
+ FFTContext *fft; ///< Fast Fourier Transform context
+ int fft_bits; ///< number of bits (FFT window size = 1<<fft_bits)
+ FFTComplex **fft_data; ///< bins holder for each (displayed) channels
+ int nb_samples;
+ int64_t pts;
+} SpectrumSynthContext;
+
+#define OFFSET(x) offsetof(SpectrumSynthContext, x)
+#define A AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_AUDIO_PARAM
+
+static const AVOption spectrumsynth_options[] = {
+ { "sample_rate", "set sample rate", OFFSET(sample_rate), AV_OPT_TYPE_INT, {.i64 = 44100}, 15, INT_MAX, A },
+ { "channels", "set channels", OFFSET(channels), AV_OPT_TYPE_INT, {.i64 = 1}, 1, 8, A },
+ { "scale", "set amplitude scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64 = LOG}, 0, NB_SCALES-1, A, "scale" },
+ { "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, A, "scale" },
+ { "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, A, "scale" },
+ { "win_func", "set window function", OFFSET(win_func), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 0, A, "win_func" },
+ { "rect", "Rectangular", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_RECT}, 0, 0, A, "win_func" },
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(spectrumsynth);
+
+static int query_formats(AVFilterContext *ctx)
+{
+ SpectrumSynthContext *s = ctx->priv;
+ AVFilterFormats *formats = NULL;
+ AVFilterChannelLayouts *layout = NULL;
+ AVFilterLink *magnitude = ctx->inputs[0];
+ AVFilterLink *phase = ctx->inputs[1];
+ 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_YUV444P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUV444P16, AV_PIX_FMT_NONE };
+ int ret, sample_rates[] = { 48000, -1 };
+
+ formats = ff_make_format_list(sample_fmts);
+ if ((ret = ff_formats_ref (formats, &outlink->in_formats )) < 0 ||
+ (ret = ff_add_channel_layout (&layout, FF_COUNT2LAYOUT(s->channels))) < 0 ||
+ (ret = ff_channel_layouts_ref (layout , &outlink->in_channel_layouts)) < 0)
+ return ret;
+
+ sample_rates[0] = s->sample_rate;
+ formats = ff_make_format_list(sample_rates);
+ if (!formats)
+ return AVERROR(ENOMEM);
+ if ((ret = ff_formats_ref(formats, &outlink->in_samplerates)) < 0)
+ return ret;
+
+ formats = ff_make_format_list(pix_fmts);
+ if (!formats)
+ return AVERROR(ENOMEM);
+ if ((ret = ff_formats_ref(formats, &magnitude->out_formats)) < 0)
+ return ret;
+
+ formats = ff_make_format_list(pix_fmts);
+ if (!formats)
+ return AVERROR(ENOMEM);
+ if ((ret = ff_formats_ref(formats, &phase->out_formats)) < 0)
+ return ret;
+
+ return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+ AVFilterContext *ctx = outlink->src;
+ SpectrumSynthContext *s = ctx->priv;
+ int width = ctx->inputs[0]->w;
+ int height = ctx->inputs[0]->h;
+ AVRational time_base = ctx->inputs[0]->time_base;
+ AVRational frame_rate = ctx->inputs[0]->frame_rate;
+ int ch, fft_bits;
+
+ outlink->sample_rate = s->sample_rate;
+ outlink->time_base = (AVRational){1, s->sample_rate};
+
+ if (width != ctx->inputs[1]->w ||
+ height != ctx->inputs[1]->h) {
+ av_log(ctx, AV_LOG_ERROR,
+ "Magnitude and Phase sizes differ (%dx%d vs %dx%d).\n",
+ width, height,
+ ctx->inputs[1]->w, ctx->inputs[1]->h);
+ return AVERROR_INVALIDDATA;
+ } else if (av_cmp_q(time_base, ctx->inputs[1]->time_base) != 0) {
+ av_log(ctx, AV_LOG_ERROR,
+ "Magnitude and Phase time bases differ (%d/%d vs %d/%d).\n",
+ time_base.num, time_base.den,
+ ctx->inputs[1]->time_base.num,
+ ctx->inputs[1]->time_base.den);
+ return AVERROR_INVALIDDATA;
+ } else if (av_cmp_q(frame_rate, ctx->inputs[1]->frame_rate) != 0) {
+ av_log(ctx, AV_LOG_ERROR,
+ "Magnitude and Phase framerates differ (%d/%d vs %d/%d).\n",
+ frame_rate.num, frame_rate.den,
+ ctx->inputs[1]->frame_rate.num,
+ ctx->inputs[1]->frame_rate.den);
+ return AVERROR_INVALIDDATA;
+ }
+
+ for (fft_bits = 1; 1 << fft_bits < 2 * height / s->channels; fft_bits++);
+ s->nb_samples = 1 << fft_bits;
+
+ s->fft = av_fft_init(fft_bits, 1);
+ if (!s->fft) {
+ av_log(ctx, AV_LOG_ERROR, "Unable to create FFT context. "
+ "The window size might be too high.\n");
+ return AVERROR(EINVAL);
+ }
+ s->fft_data = av_calloc(s->channels, sizeof(*s->fft_data));
+ if (!s->fft_data)
+ return AVERROR(ENOMEM);
+ for (ch = 0; ch < s->channels; ch++) {
+ s->fft_data[ch] = av_calloc(s->nb_samples, sizeof(**s->fft_data));
+ if (!s->fft_data[ch])
+ return AVERROR(ENOMEM);
+ }
+
+ return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+ AVFilterContext *ctx = outlink->src;
+ SpectrumSynthContext *s = ctx->priv;
+ int ret;
+
+ if (!s->magnitude) {
+ ret = ff_request_frame(ctx->inputs[0]);
+ if (ret < 0)
+ return ret;
+ }
+ if (!s->phase) {
+ ret = ff_request_frame(ctx->inputs[1]);
+ if (ret < 0)
+ return ret;
+ }
+ return 0;
+}
+
+static int try_push_frame(AVFilterContext *ctx, int x)
+{
+ SpectrumSynthContext *s = ctx->priv;
+ AVFilterLink *outlink = ctx->outputs[0];
+ AVFilterLink *inlink = ctx->inputs[0];
+ int m_linesize, p_linesize;
+ int nb = s->nb_samples;
+ int h = inlink->h / s->channels;
+ int y, f, ch = 0, n;
+ float magnitude, phase;
+ AVFrame *out;
+
+ m_linesize = s->magnitude->linesize[0];
+ p_linesize = s->phase->linesize[0];
+
+ out = ff_get_audio_buffer(outlink, s->nb_samples);
+ if (!out) {
+ av_frame_free(&s->magnitude);
+ av_frame_free(&s->phase);
+ return AVERROR(ENOMEM);
+ }
+
+ for (ch = 0; ch < s->channels; ch++) {
+ int start = h * (ch + 1) - 1;
+ int end = h * ch;
+
+ switch (inlink->format) {
+ case AV_PIX_FMT_YUV444P16:
+ for (y = start, f = 0; y >= end; y--, f++) {
+ const uint16_t *m = (uint16_t *)(s->magnitude->data[0] + y * m_linesize);
+ const uint16_t *p = (uint16_t *)(s->phase->data[0] + y * p_linesize);
+
+ switch (s->scale) {
+ case LINEAR:
+ magnitude = m[x] / (double)UINT16_MAX;
+ break;
+ case LOG:
+ magnitude = ff_exp10(((m[x] / (double)UINT16_MAX) - 1.) * 6.);
+ break;
+ }
+ phase = ((p[x] / (double)UINT16_MAX) * 2. - 1.) * M_PI;
+
+ s->fft_data[ch][f].re = magnitude * cos(phase);
+ s->fft_data[ch][f].im = magnitude * sin(phase);
+ }
+ break;
+ case AV_PIX_FMT_YUVJ444P:
+ case AV_PIX_FMT_YUV444P:
+ for (y = start, f = 0; y >= end; y--, f++) {
+ const uint8_t *m = (uint8_t *)(s->magnitude->data[0] + y * m_linesize);
+ const uint8_t *p = (uint8_t *)(s->phase->data[0] + y * p_linesize);
+
+ switch (s->scale) {
+ case LINEAR:
+ magnitude = m[x] / (double)UINT8_MAX;
+ break;
+ case LOG:
+ magnitude = ff_exp10(((m[x] / (double)UINT8_MAX) - 1.) * 6.);
+ break;
+ }
+ phase = ((p[x] / (double)UINT8_MAX) * 2. - 1.) * M_PI;
+
+ s->fft_data[ch][f].re = magnitude * cos(phase);
+ s->fft_data[ch][f].im = magnitude * sin(phase);
+ }
+ break;
+ }
+
+ s->fft_data[ch][h].re = 0;
+ s->fft_data[ch][h].im = 0;
+ for (y = h + 1, f = h - 1; y < nb; y++, f--) {
+ s->fft_data[ch][y].re = s->fft_data[ch][f].re;
+ s->fft_data[ch][y].im = -s->fft_data[ch][f].im;
+ }
+ }
+
+ for (ch = 0; ch < s->channels; ch++) {
+ float *dst = (float *)out->extended_data[ch];
+
+ av_fft_permute(s->fft, s->fft_data[ch]);
+ av_fft_calc(s->fft, s->fft_data[ch]);
+
+ for (n = 0; n < s->nb_samples; n++) {
+ dst[n] = s->fft_data[ch][n].re;
+ }
+ }
+
+ out->pts = s->pts;
+ s->pts += s->nb_samples;
+ return ff_filter_frame(outlink, out);
+}
+
+static int try_push_frames(AVFilterContext *ctx)
+{
+ SpectrumSynthContext *s = ctx->priv;
+ AVFilterLink *inlink = ctx->inputs[0];
+ int ret, x;
+
+ if (!(s->magnitude && s->phase))
+ return 0;
+
+ for (x = 0; x < inlink->w; x++) {
+ ret = try_push_frame(ctx, x);
+ if (ret < 0)
+ break;
+ }
+
+ av_frame_free(&s->magnitude);
+ av_frame_free(&s->phase);
+ return ret;
+}
+
+static int filter_frame_magnitude(AVFilterLink *inlink, AVFrame *magnitude)
+{
+ AVFilterContext *ctx = inlink->dst;
+ SpectrumSynthContext *s = ctx->priv;
+
+ s->magnitude = magnitude;
+ return try_push_frames(ctx);
+}
+
+static int filter_frame_phase(AVFilterLink *inlink, AVFrame *phase)
+{
+ AVFilterContext *ctx = inlink->dst;
+ SpectrumSynthContext *s = ctx->priv;
+
+ s->phase = phase;
+ return try_push_frames(ctx);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ SpectrumSynthContext *s = ctx->priv;
+ int i;
+
+ av_frame_free(&s->magnitude);
+ av_frame_free(&s->phase);
+ av_fft_end(s->fft);
+ if (s->fft_data) {
+ for (i = 0; i < s->channels; i++)
+ av_freep(&s->fft_data[i]);
+ }
+ av_freep(&s->fft_data);
+}
+
+static const AVFilterPad spectrumsynth_inputs[] = {
+ {
+ .name = "magnitude",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = filter_frame_magnitude,
+ .needs_fifo = 1,
+ },
+ {
+ .name = "phase",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = filter_frame_phase,
+ .needs_fifo = 1,
+ },
+ { NULL }
+};
+
+static const AVFilterPad spectrumsynth_outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .config_props = config_output,
+ .request_frame = request_frame,
+ },
+ { NULL }
+};
+
+AVFilter ff_vaf_spectrumsynth = {
+ .name = "spectrumsynth",
+ .description = NULL_IF_CONFIG_SMALL("Convert input spectrum videos to audio output."),
+ .uninit = uninit,
+ .query_formats = query_formats,
+ .priv_size = sizeof(SpectrumSynthContext),
+ .inputs = spectrumsynth_inputs,
+ .outputs = spectrumsynth_outputs,
+ .priv_class = &spectrumsynth_class,
+};
--
1.9.1
More information about the ffmpeg-devel
mailing list