[FFmpeg-devel] [PATCH] lavfi: add showspectrum filter.
Stefano Sabatini
stefasab at gmail.com
Thu Aug 16 17:34:07 CEST 2012
On date Wednesday 2012-08-15 17:56:37 +0200, Clément Bœsch encoded:
[...]
> From 6e17f2d21b40a4c65fd6961034d8a98f8ee3c22e Mon Sep 17 00:00:00 2001
> From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
> Date: Thu, 29 Dec 2011 23:45:45 +0100
> Subject: [PATCH] lavfi: add showspectrum filter.
>
> TODO: bump
> TODO: changelog
> ---
> doc/filters.texi | 13 ++
> libavfilter/Makefile | 1 +
> libavfilter/allfilters.c | 1 +
> libavfilter/avf_showspectrum.c | 316 +++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 331 insertions(+)
> create mode 100644 libavfilter/avf_showspectrum.c
>
> diff --git a/doc/filters.texi b/doc/filters.texi
> index 763085c..c5eafc9 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -4139,6 +4139,19 @@ do not have exactly the same duration in the first file.
>
> @end itemize
>
> + at section showspectrum
> +
> +Convert input audio to a video output, representing the audio spectrum.
Uhm maybe explains a bit of the math, e.g. what the video plane
represent.
> +
> +The filter accepts the following named parameters:
> + at table @option
> + at item size, s
> +Specify the video size for the output. Default value is @code{640x480}.
> + at end table
> +
> +The usage is very similar to the showwaves filter; see the examples in that
> +section.
> +
> @section showwaves
>
> Convert input audio to a video output, representing the samples waves.
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index b6aeb76..59fc920 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -201,6 +201,7 @@ OBJS-$(CONFIG_MP_FILTER) += libmpcodecs/pullup.o
>
> # multimedia filters
> OBJS-$(CONFIG_CONCAT_FILTER) += avf_concat.o
> +OBJS-$(CONFIG_SHOWSPECTRUM_FILTER) += avf_showspectrum.o
> OBJS-$(CONFIG_SHOWWAVES_FILTER) += avf_showwaves.o
>
> # multimedia sources
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index 2949a4c..5c3f161 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -139,6 +139,7 @@ void avfilter_register_all(void)
>
> /* multimedia filters */
> REGISTER_FILTER (CONCAT, concat, avf);
> + REGISTER_FILTER (SHOWSPECTRUM,showspectrum,avf);
> REGISTER_FILTER (SHOWWAVES, showwaves, avf);
>
> /* multimedia sources */
> diff --git a/libavfilter/avf_showspectrum.c b/libavfilter/avf_showspectrum.c
> new file mode 100644
> index 0000000..f281d1a
> --- /dev/null
> +++ b/libavfilter/avf_showspectrum.c
> @@ -0,0 +1,316 @@
> +/*
> + * Copyright (c) 2012 Clément Bœsch
> + *
> + * 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
> + */
> +
> +/**
> + * @file
> + * audio to spectrum (video) transmedia filter, based on ffplay rdft showmode
> + * and lavfi/avf_showwaves
> + */
> +
> +#include <math.h>
> +
> +#include "libavcodec/avfft.h"
> +#include "libavutil/audioconvert.h"
> +#include "libavutil/opt.h"
> +#include "libavutil/parseutils.h"
> +#include "avfilter.h"
> +#include "formats.h"
> +#include "audio.h"
> +#include "video.h"
> +#include "internal.h"
> +
> +typedef struct {
> + const AVClass *class;
> + int w, h;
> + AVFilterBufferRef *outpicref;
> + int req_fullfilled;
> + int xpos; ///< x position (current column)
> + RDFTContext *rdft; ///< Real Discrete Fourier Transform context
> + int rdft_bits; ///< number of bits (RDFT window size = 1<<rdft_bits)
> + FFTSample *rdft_data; ///< bins holder for each (displayed) channels
> + int filled; ///< number of samples (per channel) filled in current rdft_buffer
> + int consumed; ///< number of samples (per channel) consumed from the input frame
> + float *windowing; ///< Window function LUT
uhm... more meaningful name?
> +} ShowSpectrumContext;
> +
> +#define OFFSET(x) offsetof(ShowSpectrumContext, x)
> +
> +static const AVOption showspectrum_options[] = {
> + { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0 },
> + { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x480"}, 0, 0 },
> + { NULL },
> +};
> +
> +AVFILTER_DEFINE_CLASS(showspectrum);
> +
> +static av_cold int init(AVFilterContext *ctx, const char *args)
> +{
> + ShowSpectrumContext *showspectrum = ctx->priv;
> + int err;
> +
> + showspectrum->class = &showspectrum_class;
> + av_opt_set_defaults(showspectrum);
> +
> + if ((err = av_set_options_string(showspectrum, args, "=", ":")) < 0)
> + return err;
> +
> + return 0;
> +}
> +
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> + ShowSpectrumContext *showspectrum = ctx->priv;
> +
> + av_rdft_end(showspectrum->rdft);
> + av_freep(&showspectrum->rdft_data);
> + av_freep(&showspectrum->windowing);
> + avfilter_unref_bufferp(&showspectrum->outpicref);
> +}
> +
> +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_S16P, -1 };
> + static const enum PixelFormat pix_fmts[] = { PIX_FMT_RGB24, -1 };
> +
> + /* set input audio formats */
> + formats = ff_make_format_list(sample_fmts);
> + if (!formats)
> + return AVERROR(ENOMEM);
> + ff_formats_ref(formats, &inlink->out_formats);
> +
> + layouts = ff_all_channel_layouts();
> + if (!layouts)
> + return AVERROR(ENOMEM);
> + ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts);
> +
> + formats = ff_all_samplerates();
> + if (!formats)
> + return AVERROR(ENOMEM);
> + ff_formats_ref(formats, &inlink->out_samplerates);
> +
> + /* set output video format */
> + formats = ff_make_format_list(pix_fmts);
> + if (!formats)
> + return AVERROR(ENOMEM);
> + ff_formats_ref(formats, &outlink->in_formats);
> +
> + return 0;
> +}
> +
> +static int config_output(AVFilterLink *outlink)
> +{
> + AVFilterContext *ctx = outlink->src;
> + ShowSpectrumContext *showspectrum = ctx->priv;
> + int i, rdft_bits, win_size;
> +
> + outlink->w = showspectrum->w;
> + outlink->h = showspectrum->h;
> +
> + /* RDFT window size (precision) according to the requested output frame height */
> + for (rdft_bits = 1; 1<<rdft_bits < 2*outlink->h; rdft_bits++);
> + win_size = 1 << rdft_bits;
> +
> + /* (re-)configuration if the video output changed (or first init) */
> + if (rdft_bits != showspectrum->rdft_bits) {
> + AVFilterBufferRef *outpicref;
> +
> + av_rdft_end(showspectrum->rdft);
> + showspectrum->rdft = av_rdft_init(rdft_bits, DFT_R2C);
> + showspectrum->rdft_bits = rdft_bits;
> +
> + /* RDFT buffers: x2 for each (display) channel buffer */
> + av_free(showspectrum->rdft_data);
> + showspectrum->rdft_data = av_malloc(2 * win_size * sizeof(*showspectrum->rdft_data));
> + if (!showspectrum->rdft_data)
> + return AVERROR(ENOMEM);
av_realloc_f?
> + showspectrum->filled = 0;
> +
> + /* pre-calc windowing function (hann here) */
> + av_free(showspectrum->windowing);
> + showspectrum->windowing = av_malloc(win_size * sizeof(*showspectrum->windowing));
> + if (!showspectrum->windowing)
> + return AVERROR(ENOMEM);
As above
> + for (i = 0; i < win_size; i++)
> + showspectrum->windowing[i] = .5f * (1 - cos(2*M_PI*i / (win_size-1)));
> +
> + /* prepare the initial picref buffer (black frame) */
> + avfilter_unref_bufferp(&showspectrum->outpicref);
> + showspectrum->outpicref = outpicref =
> + ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE2,
> + outlink->w, outlink->h);
> + if (!outpicref)
> + return AVERROR(ENOMEM);
> + outlink->sample_aspect_ratio = (AVRational){1,1};
> + memset(outpicref->data[0], 0, outlink->h * outpicref->linesize[0]);
> + }
> +
> + if (showspectrum->xpos >= outlink->w)
> + showspectrum->xpos = 0;
> +
> + av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d RDFT window size:%d\n",
> + showspectrum->w, showspectrum->h, win_size);
> + return 0;
> +}
> +
> +inline static void push_frame(AVFilterLink *outlink)
> +{
> + ShowSpectrumContext *showspectrum = outlink->src->priv;
> +
> + showspectrum->xpos++;
> + if (showspectrum->xpos >= outlink->w)
> + showspectrum->xpos = 0;
> + showspectrum->filled = 0;
> + showspectrum->req_fullfilled = 1;
> +
> + ff_start_frame(outlink, avfilter_ref_buffer(showspectrum->outpicref, ~AV_PERM_READ));
~AV_PERM_WRITE?
[...]
Looks good to me otherwise.
--
FFmpeg = Fundamental and Fundamental Magnificient Peaceful Extended Glue
More information about the ffmpeg-devel
mailing list