2011-12-29 23:45:45 +01:00
|
|
|
/*
|
2013-11-20 16:20:11 +01:00
|
|
|
* Copyright (c) 2012-2013 Clément Bœsch
|
2013-02-18 13:34:37 +01:00
|
|
|
* Copyright (c) 2013 Rudolf Polzer <divverent@xonotic.org>
|
2015-12-29 16:52:16 +01:00
|
|
|
* Copyright (c) 2015 Paul B Mahol
|
2011-12-29 23:45:45 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* (by Michael Niedermayer) and lavfi/avf_showwaves (by Stefano Sabatini).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "libavcodec/avfft.h"
|
2015-12-29 16:52:16 +01:00
|
|
|
#include "libavutil/audio_fifo.h"
|
2013-02-01 16:33:17 +01:00
|
|
|
#include "libavutil/avassert.h"
|
2012-11-13 14:16:48 +01:00
|
|
|
#include "libavutil/channel_layout.h"
|
2011-12-29 23:45:45 +01:00
|
|
|
#include "libavutil/opt.h"
|
2015-12-29 16:52:16 +01:00
|
|
|
#include "audio.h"
|
|
|
|
#include "video.h"
|
2011-12-29 23:45:45 +01:00
|
|
|
#include "avfilter.h"
|
|
|
|
#include "internal.h"
|
2015-12-28 18:51:56 +01:00
|
|
|
#include "window_func.h"
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2013-02-01 16:33:17 +01:00
|
|
|
enum DisplayMode { COMBINED, SEPARATE, NB_MODES };
|
2016-01-02 18:10:03 +01:00
|
|
|
enum DisplayScale { LINEAR, SQRT, CBRT, LOG, FOURTHRT, FIFTHRT, NB_SCALES };
|
2016-01-01 23:22:00 +01:00
|
|
|
enum ColorMode { CHANNEL, INTENSITY, RAINBOW, MORELAND, NEBULAE, FIRE, FIERY, NB_CLMODES };
|
2015-12-28 18:14:46 +01:00
|
|
|
enum SlideMode { REPLACE, SCROLL, FULLFRAME, RSCROLL, NB_SLIDES };
|
2015-12-29 09:45:44 +01:00
|
|
|
enum Orientation { VERTICAL, HORIZONTAL, NB_ORIENTATIONS };
|
2013-02-01 16:33:17 +01:00
|
|
|
|
2011-12-29 23:45:45 +01:00
|
|
|
typedef struct {
|
|
|
|
const AVClass *class;
|
|
|
|
int w, h;
|
2013-03-10 01:30:30 +01:00
|
|
|
AVFrame *outpicref;
|
2013-02-01 16:33:17 +01:00
|
|
|
int nb_display_channels;
|
2015-12-29 09:45:44 +01:00
|
|
|
int orientation;
|
|
|
|
int channel_width;
|
2013-02-01 16:33:17 +01:00
|
|
|
int channel_height;
|
2012-10-21 21:01:18 +02:00
|
|
|
int sliding; ///< 1 if sliding mode, 0 otherwise
|
2015-02-02 23:07:54 +01:00
|
|
|
int mode; ///< channel display mode
|
|
|
|
int color_mode; ///< display color scheme
|
|
|
|
int scale;
|
2013-02-01 16:33:17 +01:00
|
|
|
float saturation; ///< color saturation multiplier
|
2011-12-29 23:45:45 +01:00
|
|
|
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)
|
2013-02-01 16:33:17 +01:00
|
|
|
FFTSample **rdft_data; ///< bins holder for each (displayed) channels
|
2011-12-29 23:45:45 +01:00
|
|
|
float *window_func_lut; ///< Window function LUT
|
2015-12-31 20:45:37 +01:00
|
|
|
float **magnitudes;
|
2015-02-02 23:07:54 +01:00
|
|
|
int win_func;
|
2015-12-31 20:31:32 +01:00
|
|
|
int win_size;
|
2015-12-28 23:45:34 +01:00
|
|
|
double win_scale;
|
2015-12-28 18:51:56 +01:00
|
|
|
float overlap;
|
2015-12-29 16:52:16 +01:00
|
|
|
int skip_samples;
|
2013-02-01 16:33:17 +01:00
|
|
|
float *combine_buffer; ///< color combining buffer (3 * h items)
|
2015-12-29 16:52:16 +01:00
|
|
|
AVAudioFifo *fifo;
|
|
|
|
int64_t pts;
|
2015-12-31 20:45:37 +01:00
|
|
|
int single_pic;
|
2011-12-29 23:45:45 +01:00
|
|
|
} ShowSpectrumContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(ShowSpectrumContext, x)
|
2012-08-21 01:03:36 +02:00
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
2011-12-29 23:45:45 +01:00
|
|
|
|
|
|
|
static const AVOption showspectrum_options[] = {
|
2013-02-03 18:03:39 +01:00
|
|
|
{ "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
|
|
|
|
{ "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "640x512"}, 0, 0, FLAGS },
|
2015-12-28 18:14:46 +01:00
|
|
|
{ "slide", "set sliding mode", OFFSET(sliding), AV_OPT_TYPE_INT, {.i64 = 0}, 0, NB_SLIDES-1, FLAGS, "slide" },
|
2014-08-20 18:49:01 +02:00
|
|
|
{ "replace", "replace old columns with new", 0, AV_OPT_TYPE_CONST, {.i64=REPLACE}, 0, 0, FLAGS, "slide" },
|
2014-08-03 19:30:04 +02:00
|
|
|
{ "scroll", "scroll from right to left", 0, AV_OPT_TYPE_CONST, {.i64=SCROLL}, 0, 0, FLAGS, "slide" },
|
2015-12-28 18:14:46 +01:00
|
|
|
{ "rscroll", "scroll from left to right", 0, AV_OPT_TYPE_CONST, {.i64=RSCROLL}, 0, 0, FLAGS, "slide" },
|
2014-08-03 19:30:04 +02:00
|
|
|
{ "fullframe", "return full frames", 0, AV_OPT_TYPE_CONST, {.i64=FULLFRAME}, 0, 0, FLAGS, "slide" },
|
2013-02-01 16:33:17 +01:00
|
|
|
{ "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, COMBINED, NB_MODES-1, FLAGS, "mode" },
|
2013-11-20 16:37:34 +01:00
|
|
|
{ "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
|
|
|
|
{ "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
|
2013-02-01 16:33:17 +01:00
|
|
|
{ "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=CHANNEL}, CHANNEL, NB_CLMODES-1, FLAGS, "color" },
|
2013-11-20 16:37:34 +01:00
|
|
|
{ "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
|
|
|
|
{ "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
|
2015-12-30 18:35:17 +01:00
|
|
|
{ "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
|
|
|
|
{ "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
|
2015-12-30 21:20:16 +01:00
|
|
|
{ "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
|
2015-12-31 11:15:10 +01:00
|
|
|
{ "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
|
2016-01-01 23:22:00 +01:00
|
|
|
{ "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
|
2013-02-01 16:33:17 +01:00
|
|
|
{ "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=SQRT}, LINEAR, NB_SCALES-1, FLAGS, "scale" },
|
2013-11-20 16:37:34 +01:00
|
|
|
{ "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
|
|
|
|
{ "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
|
2016-01-02 18:10:03 +01:00
|
|
|
{ "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
|
|
|
|
{ "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
|
2013-11-20 16:37:34 +01:00
|
|
|
{ "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
|
|
|
|
{ "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
|
2013-02-01 16:33:17 +01:00
|
|
|
{ "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
|
2015-12-28 18:51:56 +01: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" },
|
|
|
|
{ "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 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" },
|
|
|
|
{ "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" },
|
2015-12-29 09:45:44 +01:00
|
|
|
{ "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
|
|
|
|
{ "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
|
|
|
|
{ "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
|
2015-12-29 16:52:16 +01:00
|
|
|
{ "overlap", "set window overlap", OFFSET(overlap), AV_OPT_TYPE_FLOAT, {.dbl = 0}, 0, 1, FLAGS },
|
2013-09-07 14:13:50 +02:00
|
|
|
{ NULL }
|
2011-12-29 23:45:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(showspectrum);
|
|
|
|
|
2015-12-29 19:47:32 +01:00
|
|
|
static const struct ColorTable {
|
2013-02-01 16:33:17 +01:00
|
|
|
float a, y, u, v;
|
2015-12-29 19:47:32 +01:00
|
|
|
} color_table[][8] = {
|
|
|
|
[INTENSITY] = {
|
2013-02-14 23:04:15 +01:00
|
|
|
{ 0, 0, 0, 0 },
|
|
|
|
{ 0.13, .03587126228984074, .1573300977624594, -.02548747583751842 },
|
|
|
|
{ 0.30, .18572281794568020, .1772436246393981, .17475554840414750 },
|
|
|
|
{ 0.60, .28184980583656130, -.1593064119945782, .47132074554608920 },
|
|
|
|
{ 0.73, .65830621175547810, -.3716070802232764, .24352759331252930 },
|
|
|
|
{ 0.78, .76318535758242900, -.4307467689263783, .16866496622310430 },
|
|
|
|
{ 0.91, .95336363636363640, -.2045454545454546, .03313636363636363 },
|
2015-12-29 19:47:32 +01:00
|
|
|
{ 1, 1, 0, 0 }},
|
|
|
|
[RAINBOW] = {
|
|
|
|
{ 0, 0, 0, 0 },
|
|
|
|
{ 0.13, 44/256., (189-128)/256., (138-128)/256. },
|
|
|
|
{ 0.25, 29/256., (186-128)/256., (119-128)/256. },
|
|
|
|
{ 0.38, 119/256., (194-128)/256., (53-128)/256. },
|
|
|
|
{ 0.60, 111/256., (73-128)/256., (59-128)/256. },
|
|
|
|
{ 0.73, 205/256., (19-128)/256., (149-128)/256. },
|
|
|
|
{ 0.86, 135/256., (83-128)/256., (200-128)/256. },
|
|
|
|
{ 1, 73/256., (95-128)/256., (225-128)/256. }},
|
2015-12-30 18:35:17 +01:00
|
|
|
[MORELAND] = {
|
|
|
|
{ 0, 44/256., (181-128)/256., (112-128)/256. },
|
|
|
|
{ 0.13, 126/256., (177-128)/256., (106-128)/256. },
|
|
|
|
{ 0.25, 164/256., (163-128)/256., (109-128)/256. },
|
|
|
|
{ 0.38, 200/256., (140-128)/256., (120-128)/256. },
|
|
|
|
{ 0.60, 201/256., (117-128)/256., (141-128)/256. },
|
|
|
|
{ 0.73, 177/256., (103-128)/256., (165-128)/256. },
|
|
|
|
{ 0.86, 136/256., (100-128)/256., (183-128)/256. },
|
|
|
|
{ 1, 68/256., (117-128)/256., (203-128)/256. }},
|
2015-12-30 21:20:16 +01:00
|
|
|
[NEBULAE] = {
|
|
|
|
{ 0, 10/256., (134-128)/256., (132-128)/256. },
|
|
|
|
{ 0.23, 21/256., (137-128)/256., (130-128)/256. },
|
|
|
|
{ 0.45, 35/256., (134-128)/256., (134-128)/256. },
|
|
|
|
{ 0.57, 51/256., (130-128)/256., (139-128)/256. },
|
|
|
|
{ 0.67, 104/256., (116-128)/256., (162-128)/256. },
|
|
|
|
{ 0.77, 120/256., (105-128)/256., (188-128)/256. },
|
|
|
|
{ 0.87, 140/256., (105-128)/256., (188-128)/256. },
|
|
|
|
{ 1, 1, 0, 0 }},
|
2015-12-31 11:15:10 +01:00
|
|
|
[FIRE] = {
|
|
|
|
{ 0, 0, 0, 0 },
|
|
|
|
{ 0.23, 44/256., (132-128)/256., (127-128)/256. },
|
|
|
|
{ 0.45, 62/256., (116-128)/256., (140-128)/256. },
|
|
|
|
{ 0.57, 75/256., (105-128)/256., (152-128)/256. },
|
|
|
|
{ 0.67, 95/256., (91-128)/256., (166-128)/256. },
|
|
|
|
{ 0.77, 126/256., (74-128)/256., (172-128)/256. },
|
|
|
|
{ 0.87, 164/256., (73-128)/256., (162-128)/256. },
|
|
|
|
{ 1, 1, 0, 0 }},
|
2016-01-01 23:22:00 +01:00
|
|
|
[FIERY] = {
|
|
|
|
{ 0, 0, 0, 0 },
|
|
|
|
{ 0.23, 36/256., (116-128)/256., (163-128)/256. },
|
|
|
|
{ 0.45, 52/256., (102-128)/256., (200-128)/256. },
|
|
|
|
{ 0.57, 116/256., (84-128)/256., (196-128)/256. },
|
|
|
|
{ 0.67, 157/256., (67-128)/256., (181-128)/256. },
|
|
|
|
{ 0.77, 193/256., (40-128)/256., (155-128)/256. },
|
|
|
|
{ 0.87, 221/256., (101-128)/256., (134-128)/256. },
|
|
|
|
{ 1, 1, 0, 0 }},
|
2013-02-01 16:33:17 +01:00
|
|
|
};
|
|
|
|
|
2011-12-29 23:45:45 +01:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
2013-05-16 18:28:25 +02:00
|
|
|
ShowSpectrumContext *s = ctx->priv;
|
2013-02-01 16:33:17 +01:00
|
|
|
int i;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2013-05-16 18:28:25 +02:00
|
|
|
av_freep(&s->combine_buffer);
|
|
|
|
av_rdft_end(s->rdft);
|
2016-01-02 11:49:15 +01:00
|
|
|
if (s->rdft_data) {
|
|
|
|
for (i = 0; i < s->nb_display_channels; i++)
|
|
|
|
av_freep(&s->rdft_data[i]);
|
|
|
|
}
|
2013-05-16 18:28:25 +02:00
|
|
|
av_freep(&s->rdft_data);
|
|
|
|
av_freep(&s->window_func_lut);
|
2016-01-02 11:49:15 +01:00
|
|
|
if (s->magnitudes) {
|
|
|
|
for (i = 0; i < s->nb_display_channels; i++)
|
|
|
|
av_freep(&s->magnitudes[i]);
|
|
|
|
}
|
2015-12-31 20:45:37 +01:00
|
|
|
av_freep(&s->magnitudes);
|
2013-05-16 18:28:25 +02:00
|
|
|
av_frame_free(&s->outpicref);
|
2015-12-29 16:52:16 +01:00
|
|
|
av_audio_fifo_free(s->fifo);
|
2011-12-29 23:45:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterFormats *formats = NULL;
|
|
|
|
AVFilterChannelLayouts *layouts = NULL;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2012-11-05 14:34:08 +01:00
|
|
|
static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_NONE };
|
2013-02-01 16:33:17 +01:00
|
|
|
static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_NONE };
|
2015-10-05 05:39:25 +02:00
|
|
|
int ret;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
|
|
|
/* set input audio formats */
|
|
|
|
formats = ff_make_format_list(sample_fmts);
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_formats_ref(formats, &inlink->out_formats)) < 0)
|
|
|
|
return ret;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
|
|
|
layouts = ff_all_channel_layouts();
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_channel_layouts_ref(layouts, &inlink->out_channel_layouts)) < 0)
|
|
|
|
return ret;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
|
|
|
formats = ff_all_samplerates();
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_formats_ref(formats, &inlink->out_samplerates)) < 0)
|
|
|
|
return ret;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
|
|
|
/* set output video format */
|
|
|
|
formats = ff_make_format_list(pix_fmts);
|
2015-10-05 05:39:25 +02:00
|
|
|
if ((ret = ff_formats_ref(formats, &outlink->in_formats)) < 0)
|
|
|
|
return ret;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = outlink->src;
|
2013-02-01 16:33:17 +01:00
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
2013-05-16 18:28:25 +02:00
|
|
|
ShowSpectrumContext *s = ctx->priv;
|
2015-12-31 20:31:32 +01:00
|
|
|
int i, rdft_bits, h, w;
|
2015-12-29 16:52:16 +01:00
|
|
|
float overlap;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2015-12-31 20:45:37 +01:00
|
|
|
if (!strcmp(ctx->filter->name, "showspectrumpic"))
|
|
|
|
s->single_pic = 1;
|
|
|
|
|
2013-05-16 18:28:25 +02:00
|
|
|
outlink->w = s->w;
|
|
|
|
outlink->h = s->h;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2015-12-29 09:45:44 +01:00
|
|
|
h = (s->mode == COMBINED || s->orientation == HORIZONTAL) ? outlink->h : outlink->h / inlink->channels;
|
|
|
|
w = (s->mode == COMBINED || s->orientation == VERTICAL) ? outlink->w : outlink->w / inlink->channels;
|
2013-05-16 18:28:25 +02:00
|
|
|
s->channel_height = h;
|
2015-12-29 09:45:44 +01:00
|
|
|
s->channel_width = w;
|
|
|
|
|
|
|
|
if (s->orientation == VERTICAL) {
|
|
|
|
/* RDFT window size (precision) according to the requested output frame height */
|
|
|
|
for (rdft_bits = 1; 1 << rdft_bits < 2 * h; rdft_bits++);
|
|
|
|
} else {
|
|
|
|
/* RDFT window size (precision) according to the requested output frame width */
|
|
|
|
for (rdft_bits = 1; 1 << rdft_bits < 2 * w; rdft_bits++);
|
|
|
|
}
|
2015-12-31 20:31:32 +01:00
|
|
|
s->win_size = 1 << rdft_bits;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
|
|
|
/* (re-)configuration if the video output changed (or first init) */
|
2013-05-16 18:28:25 +02:00
|
|
|
if (rdft_bits != s->rdft_bits) {
|
2013-03-10 01:30:30 +01:00
|
|
|
AVFrame *outpicref;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2013-05-16 18:28:25 +02:00
|
|
|
av_rdft_end(s->rdft);
|
|
|
|
s->rdft = av_rdft_init(rdft_bits, DFT_R2C);
|
2014-08-03 20:39:59 +02:00
|
|
|
if (!s->rdft) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Unable to create RDFT context. "
|
|
|
|
"The window size might be too high.\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2013-05-16 18:28:25 +02:00
|
|
|
s->rdft_bits = rdft_bits;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2012-10-26 18:42:54 +02:00
|
|
|
/* RDFT 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. */
|
2013-05-16 18:28:25 +02:00
|
|
|
for (i = 0; i < s->nb_display_channels; i++)
|
|
|
|
av_freep(&s->rdft_data[i]);
|
|
|
|
av_freep(&s->rdft_data);
|
|
|
|
s->nb_display_channels = inlink->channels;
|
2013-02-01 16:33:17 +01:00
|
|
|
|
2015-12-31 20:45:37 +01:00
|
|
|
s->magnitudes = av_calloc(s->nb_display_channels, sizeof(*s->magnitudes));
|
|
|
|
if (!s->magnitudes)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
for (i = 0; i < s->nb_display_channels; i++) {
|
|
|
|
s->magnitudes[i] = av_calloc(s->orientation == VERTICAL ? s->h : s->w, sizeof(**s->magnitudes));
|
|
|
|
if (!s->magnitudes[i])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2015-08-06 14:58:13 +02:00
|
|
|
s->rdft_data = av_calloc(s->nb_display_channels, sizeof(*s->rdft_data));
|
2013-05-16 18:28:25 +02:00
|
|
|
if (!s->rdft_data)
|
2011-12-29 23:45:45 +01:00
|
|
|
return AVERROR(ENOMEM);
|
2013-05-16 18:28:25 +02:00
|
|
|
for (i = 0; i < s->nb_display_channels; i++) {
|
2015-12-31 20:31:32 +01:00
|
|
|
s->rdft_data[i] = av_calloc(s->win_size, sizeof(**s->rdft_data));
|
2013-05-16 18:28:25 +02:00
|
|
|
if (!s->rdft_data[i])
|
2013-02-01 16:33:17 +01:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2013-11-20 16:20:11 +01:00
|
|
|
/* pre-calc windowing function */
|
2013-05-16 18:28:25 +02:00
|
|
|
s->window_func_lut =
|
2015-12-31 20:31:32 +01:00
|
|
|
av_realloc_f(s->window_func_lut, s->win_size,
|
2013-05-16 18:28:25 +02:00
|
|
|
sizeof(*s->window_func_lut));
|
|
|
|
if (!s->window_func_lut)
|
2011-12-29 23:45:45 +01:00
|
|
|
return AVERROR(ENOMEM);
|
2015-12-31 20:31:32 +01:00
|
|
|
ff_generate_window_func(s->window_func_lut, s->win_size, s->win_func, &overlap);
|
2015-12-29 16:52:16 +01:00
|
|
|
if (s->overlap == 1)
|
|
|
|
s->overlap = overlap;
|
2015-12-31 20:31:32 +01:00
|
|
|
s->skip_samples = (1. - s->overlap) * s->win_size;
|
2015-12-29 16:52:16 +01:00
|
|
|
if (s->skip_samples < 1) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "overlap %f too big\n", s->overlap);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2015-12-31 20:31:32 +01:00
|
|
|
for (s->win_scale = 0, i = 0; i < s->win_size; i++) {
|
2015-12-28 23:45:34 +01:00
|
|
|
s->win_scale += s->window_func_lut[i] * s->window_func_lut[i];
|
|
|
|
}
|
|
|
|
s->win_scale = 1. / (sqrt(s->win_scale) * 32768.);
|
|
|
|
|
2011-12-29 23:45:45 +01:00
|
|
|
/* prepare the initial picref buffer (black frame) */
|
2013-05-16 18:28:25 +02:00
|
|
|
av_frame_free(&s->outpicref);
|
|
|
|
s->outpicref = outpicref =
|
2013-03-10 01:30:30 +01:00
|
|
|
ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
2011-12-29 23:45:45 +01:00
|
|
|
if (!outpicref)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
outlink->sample_aspect_ratio = (AVRational){1,1};
|
2013-04-29 15:31:04 +02:00
|
|
|
for (i = 0; i < outlink->h; i++) {
|
|
|
|
memset(outpicref->data[0] + i * outpicref->linesize[0], 0, outlink->w);
|
|
|
|
memset(outpicref->data[1] + i * outpicref->linesize[1], 128, outlink->w);
|
|
|
|
memset(outpicref->data[2] + i * outpicref->linesize[2], 128, outlink->w);
|
|
|
|
}
|
2011-12-29 23:45:45 +01:00
|
|
|
}
|
|
|
|
|
2015-12-29 09:45:44 +01:00
|
|
|
if ((s->orientation == VERTICAL && s->xpos >= outlink->w) ||
|
|
|
|
(s->orientation == HORIZONTAL && s->xpos >= outlink->h))
|
2013-05-16 18:28:25 +02:00
|
|
|
s->xpos = 0;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2015-12-31 20:31:32 +01:00
|
|
|
outlink->frame_rate = av_make_q(inlink->sample_rate, s->win_size * (1.-s->overlap));
|
2015-12-29 09:45:44 +01:00
|
|
|
if (s->orientation == VERTICAL && s->sliding == FULLFRAME)
|
2014-08-03 19:30:04 +02:00
|
|
|
outlink->frame_rate.den *= outlink->w;
|
2015-12-29 09:45:44 +01:00
|
|
|
if (s->orientation == HORIZONTAL && s->sliding == FULLFRAME)
|
|
|
|
outlink->frame_rate.den *= outlink->h;
|
2014-08-03 16:23:18 +02:00
|
|
|
|
2015-12-29 09:45:44 +01:00
|
|
|
if (s->orientation == VERTICAL) {
|
|
|
|
s->combine_buffer =
|
|
|
|
av_realloc_f(s->combine_buffer, outlink->h * 3,
|
|
|
|
sizeof(*s->combine_buffer));
|
|
|
|
} else {
|
|
|
|
s->combine_buffer =
|
|
|
|
av_realloc_f(s->combine_buffer, outlink->w * 3,
|
|
|
|
sizeof(*s->combine_buffer));
|
|
|
|
}
|
2013-02-01 16:33:17 +01:00
|
|
|
|
2011-12-29 23:45:45 +01:00
|
|
|
av_log(ctx, AV_LOG_VERBOSE, "s:%dx%d RDFT window size:%d\n",
|
2015-12-31 20:31:32 +01:00
|
|
|
s->w, s->h, s->win_size);
|
2015-12-29 16:52:16 +01:00
|
|
|
|
|
|
|
av_audio_fifo_free(s->fifo);
|
2015-12-31 20:31:32 +01:00
|
|
|
s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->win_size);
|
2015-12-29 16:52:16 +01:00
|
|
|
if (!s->fifo)
|
|
|
|
return AVERROR(ENOMEM);
|
2011-12-29 23:45:45 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-31 20:45:37 +01:00
|
|
|
static void run_rdft(ShowSpectrumContext *s, AVFrame *fin)
|
2011-12-29 23:45:45 +01:00
|
|
|
{
|
2015-12-31 20:45:37 +01:00
|
|
|
int ch, n;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2015-12-31 20:45:37 +01:00
|
|
|
/* fill RDFT input with the number of samples available */
|
|
|
|
for (ch = 0; ch < s->nb_display_channels; ch++) {
|
|
|
|
const int16_t *p = (int16_t *)fin->extended_data[ch];
|
|
|
|
|
|
|
|
for (n = 0; n < s->win_size; n++)
|
|
|
|
s->rdft_data[ch][n] = p[n] * s->window_func_lut[n];
|
2015-10-02 16:03:16 +02:00
|
|
|
}
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2015-12-31 20:45:37 +01:00
|
|
|
/* run RDFT on each samples set */
|
|
|
|
for (ch = 0; ch < s->nb_display_channels; ch++)
|
|
|
|
av_rdft_calc(s->rdft, s->rdft_data[ch]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RE(y, ch) s->rdft_data[ch][2 * (y) + 0]
|
|
|
|
#define IM(y, ch) s->rdft_data[ch][2 * (y) + 1]
|
|
|
|
#define MAGNITUDE(y, ch) hypot(RE(y, ch), IM(y, ch))
|
|
|
|
|
|
|
|
static void calc_magnitudes(ShowSpectrumContext *s)
|
|
|
|
{
|
|
|
|
int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
|
|
|
|
|
|
|
|
for (ch = 0; ch < s->nb_display_channels; ch++) {
|
|
|
|
float *magnitudes = s->magnitudes[ch];
|
|
|
|
|
|
|
|
for (y = 0; y < h; y++)
|
|
|
|
magnitudes[y] = MAGNITUDE(y, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void acalc_magnitudes(ShowSpectrumContext *s)
|
|
|
|
{
|
|
|
|
int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
|
|
|
|
|
|
|
|
for (ch = 0; ch < s->nb_display_channels; ch++) {
|
|
|
|
float *magnitudes = s->magnitudes[ch];
|
|
|
|
|
|
|
|
for (y = 0; y < h; y++)
|
|
|
|
magnitudes[y] += MAGNITUDE(y, ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scale_magnitudes(ShowSpectrumContext *s, float scale)
|
|
|
|
{
|
|
|
|
int ch, y, h = s->orientation == VERTICAL ? s->h : s->w;
|
|
|
|
|
|
|
|
for (ch = 0; ch < s->nb_display_channels; ch++) {
|
|
|
|
float *magnitudes = s->magnitudes[ch];
|
|
|
|
|
|
|
|
for (y = 0; y < h; y++)
|
|
|
|
magnitudes[y] *= scale;
|
|
|
|
}
|
2011-12-29 23:45:45 +01:00
|
|
|
}
|
|
|
|
|
2016-01-02 15:25:58 +01:00
|
|
|
static void pick_color(ShowSpectrumContext *s,
|
|
|
|
float yf, float uf, float vf,
|
|
|
|
float a, float *out)
|
|
|
|
{
|
|
|
|
if (s->color_mode > CHANNEL) {
|
|
|
|
const int cm = s->color_mode;
|
|
|
|
float y, u, v;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < FF_ARRAY_ELEMS(color_table[cm]) - 1; i++)
|
|
|
|
if (color_table[cm][i].a >= a)
|
|
|
|
break;
|
|
|
|
// i now is the first item >= the color
|
|
|
|
// now we know to interpolate between item i - 1 and i
|
|
|
|
if (a <= color_table[cm][i - 1].a) {
|
|
|
|
y = color_table[cm][i - 1].y;
|
|
|
|
u = color_table[cm][i - 1].u;
|
|
|
|
v = color_table[cm][i - 1].v;
|
|
|
|
} else if (a >= color_table[cm][i].a) {
|
|
|
|
y = color_table[cm][i].y;
|
|
|
|
u = color_table[cm][i].u;
|
|
|
|
v = color_table[cm][i].v;
|
|
|
|
} else {
|
|
|
|
float start = color_table[cm][i - 1].a;
|
|
|
|
float end = color_table[cm][i].a;
|
|
|
|
float lerpfrac = (a - start) / (end - start);
|
|
|
|
y = color_table[cm][i - 1].y * (1.0f - lerpfrac)
|
|
|
|
+ color_table[cm][i].y * lerpfrac;
|
|
|
|
u = color_table[cm][i - 1].u * (1.0f - lerpfrac)
|
|
|
|
+ color_table[cm][i].u * lerpfrac;
|
|
|
|
v = color_table[cm][i - 1].v * (1.0f - lerpfrac)
|
|
|
|
+ color_table[cm][i].v * lerpfrac;
|
|
|
|
}
|
|
|
|
|
|
|
|
out[0] += y * yf;
|
|
|
|
out[1] += u * uf;
|
|
|
|
out[2] += v * vf;
|
|
|
|
} else {
|
|
|
|
out[0] += a * yf;
|
|
|
|
out[1] += a * uf;
|
|
|
|
out[2] += a * vf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clear_combine_buffer(ShowSpectrumContext *s, int size)
|
|
|
|
{
|
|
|
|
int y;
|
|
|
|
|
|
|
|
for (y = 0; y < size; y++) {
|
|
|
|
s->combine_buffer[3 * y ] = 0;
|
|
|
|
s->combine_buffer[3 * y + 1] = 127.5;
|
|
|
|
s->combine_buffer[3 * y + 2] = 127.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 16:58:27 +02:00
|
|
|
static int plot_spectrum_column(AVFilterLink *inlink, AVFrame *insamples)
|
2011-12-29 23:45:45 +01:00
|
|
|
{
|
2013-03-13 09:56:35 +01:00
|
|
|
int ret;
|
2011-12-29 23:45:45 +01:00
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2013-05-16 18:28:25 +02:00
|
|
|
ShowSpectrumContext *s = ctx->priv;
|
|
|
|
AVFrame *outpicref = s->outpicref;
|
2015-12-28 23:45:34 +01:00
|
|
|
const double w = s->win_scale;
|
2015-12-29 09:45:44 +01:00
|
|
|
int h = s->orientation == VERTICAL ? s->channel_height : s->channel_width;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2015-12-31 20:45:37 +01:00
|
|
|
int ch, plane, x, y;
|
2013-02-01 16:33:17 +01:00
|
|
|
|
2015-08-04 15:18:51 +02:00
|
|
|
/* fill a new spectrum column */
|
|
|
|
/* initialize buffer for combining to black */
|
2016-01-02 15:25:58 +01:00
|
|
|
clear_combine_buffer(s, s->orientation == VERTICAL ? outlink->h : outlink->w);
|
2013-02-01 16:33:17 +01:00
|
|
|
|
2015-08-04 15:18:51 +02:00
|
|
|
for (ch = 0; ch < s->nb_display_channels; ch++) {
|
2015-12-31 20:45:37 +01:00
|
|
|
float *magnitudes = s->magnitudes[ch];
|
2015-08-04 15:18:51 +02:00
|
|
|
float yf, uf, vf;
|
|
|
|
|
|
|
|
/* decide color range */
|
|
|
|
switch (s->mode) {
|
|
|
|
case COMBINED:
|
|
|
|
// reduce range by channel count
|
|
|
|
yf = 256.0f / s->nb_display_channels;
|
|
|
|
switch (s->color_mode) {
|
2015-12-29 19:47:32 +01:00
|
|
|
case RAINBOW:
|
2015-12-30 18:35:17 +01:00
|
|
|
case MORELAND:
|
2015-12-30 21:20:16 +01:00
|
|
|
case NEBULAE:
|
2015-12-31 11:15:10 +01:00
|
|
|
case FIRE:
|
2016-01-01 23:22:00 +01:00
|
|
|
case FIERY:
|
2015-08-04 15:18:51 +02:00
|
|
|
case INTENSITY:
|
|
|
|
uf = yf;
|
|
|
|
vf = yf;
|
2013-02-01 16:33:17 +01:00
|
|
|
break;
|
2015-08-04 15:18:51 +02:00
|
|
|
case CHANNEL:
|
|
|
|
/* adjust saturation for mixed UV coloring */
|
|
|
|
/* this factor is correct for infinite channels, an approximation otherwise */
|
|
|
|
uf = yf * M_PI;
|
|
|
|
vf = yf * M_PI;
|
2013-02-01 16:33:17 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_assert0(0);
|
2012-10-21 21:01:18 +02:00
|
|
|
}
|
2015-08-04 15:18:51 +02:00
|
|
|
break;
|
|
|
|
case SEPARATE:
|
|
|
|
// full range
|
|
|
|
yf = 256.0f;
|
|
|
|
uf = 256.0f;
|
|
|
|
vf = 256.0f;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_assert0(0);
|
|
|
|
}
|
2012-10-21 21:01:18 +02:00
|
|
|
|
2015-08-04 15:18:51 +02:00
|
|
|
if (s->color_mode == CHANNEL) {
|
|
|
|
if (s->nb_display_channels > 1) {
|
|
|
|
uf *= 0.5 * sin((2 * M_PI * ch) / s->nb_display_channels);
|
|
|
|
vf *= 0.5 * cos((2 * M_PI * ch) / s->nb_display_channels);
|
|
|
|
} else {
|
|
|
|
uf = 0.0f;
|
|
|
|
vf = 0.0f;
|
2013-02-01 16:33:17 +01:00
|
|
|
}
|
2015-08-04 15:18:51 +02:00
|
|
|
}
|
|
|
|
uf *= s->saturation;
|
|
|
|
vf *= s->saturation;
|
2013-02-01 16:33:17 +01:00
|
|
|
|
2015-08-04 15:18:51 +02:00
|
|
|
/* draw the channel */
|
|
|
|
for (y = 0; y < h; y++) {
|
|
|
|
int row = (s->mode == COMBINED) ? y : ch * h + y;
|
|
|
|
float *out = &s->combine_buffer[3 * row];
|
|
|
|
|
|
|
|
/* get magnitude */
|
2015-12-31 20:45:37 +01:00
|
|
|
float a = w * magnitudes[y];
|
2015-08-04 15:18:51 +02:00
|
|
|
|
|
|
|
/* apply scale */
|
|
|
|
switch (s->scale) {
|
|
|
|
case LINEAR:
|
|
|
|
break;
|
|
|
|
case SQRT:
|
|
|
|
a = sqrt(a);
|
|
|
|
break;
|
|
|
|
case CBRT:
|
|
|
|
a = cbrt(a);
|
|
|
|
break;
|
2016-01-02 18:10:03 +01:00
|
|
|
case FOURTHRT:
|
|
|
|
a = pow(a, 0.25);
|
|
|
|
break;
|
|
|
|
case FIFTHRT:
|
|
|
|
a = pow(a, 0.20);
|
|
|
|
break;
|
2015-08-04 15:18:51 +02:00
|
|
|
case LOG:
|
2015-12-28 20:20:32 +01:00
|
|
|
a = 1 + log10(FFMAX(FFMIN(1, a), 1e-6)) / 5; // zero = -120dBFS
|
2015-08-04 15:18:51 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_assert0(0);
|
2013-02-01 16:33:17 +01:00
|
|
|
}
|
|
|
|
|
2016-01-02 15:25:58 +01:00
|
|
|
pick_color(s, yf, uf, vf, a, out);
|
2013-02-01 16:33:17 +01:00
|
|
|
}
|
2015-08-04 15:18:51 +02:00
|
|
|
}
|
|
|
|
|
2015-12-29 16:52:16 +01:00
|
|
|
av_frame_make_writable(s->outpicref);
|
2015-08-04 15:18:51 +02:00
|
|
|
/* copy to output */
|
2015-12-29 09:45:44 +01:00
|
|
|
if (s->orientation == VERTICAL) {
|
|
|
|
if (s->sliding == SCROLL) {
|
|
|
|
for (plane = 0; plane < 3; plane++) {
|
|
|
|
for (y = 0; y < outlink->h; y++) {
|
|
|
|
uint8_t *p = outpicref->data[plane] +
|
|
|
|
y * outpicref->linesize[plane];
|
|
|
|
memmove(p, p + 1, outlink->w - 1);
|
|
|
|
}
|
2013-02-01 16:33:17 +01:00
|
|
|
}
|
2015-12-29 09:45:44 +01:00
|
|
|
s->xpos = outlink->w - 1;
|
|
|
|
} else if (s->sliding == RSCROLL) {
|
|
|
|
for (plane = 0; plane < 3; plane++) {
|
|
|
|
for (y = 0; y < outlink->h; y++) {
|
|
|
|
uint8_t *p = outpicref->data[plane] +
|
|
|
|
y * outpicref->linesize[plane];
|
|
|
|
memmove(p + 1, p, outlink->w - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->xpos = 0;
|
2013-02-01 16:33:17 +01:00
|
|
|
}
|
2015-12-28 18:14:46 +01:00
|
|
|
for (plane = 0; plane < 3; plane++) {
|
2015-12-29 09:45:44 +01:00
|
|
|
uint8_t *p = outpicref->data[plane] +
|
|
|
|
(outlink->h - 1) * outpicref->linesize[plane] +
|
|
|
|
s->xpos;
|
2015-12-28 18:14:46 +01:00
|
|
|
for (y = 0; y < outlink->h; y++) {
|
2015-12-29 09:45:44 +01:00
|
|
|
*p = lrint(FFMAX(0, FFMIN(s->combine_buffer[3 * y + plane], 255)));
|
|
|
|
p -= outpicref->linesize[plane];
|
2015-12-28 18:14:46 +01:00
|
|
|
}
|
|
|
|
}
|
2015-12-29 09:45:44 +01:00
|
|
|
} else {
|
|
|
|
if (s->sliding == SCROLL) {
|
|
|
|
for (plane = 0; plane < 3; plane++) {
|
|
|
|
for (y = 1; y < outlink->h; y++) {
|
|
|
|
memmove(outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
|
|
|
|
outpicref->data[plane] + (y ) * outpicref->linesize[plane],
|
|
|
|
outlink->w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->xpos = outlink->h - 1;
|
|
|
|
} else if (s->sliding == RSCROLL) {
|
|
|
|
for (plane = 0; plane < 3; plane++) {
|
|
|
|
for (y = outlink->h - 1; y >= 1; y--) {
|
|
|
|
memmove(outpicref->data[plane] + (y ) * outpicref->linesize[plane],
|
|
|
|
outpicref->data[plane] + (y-1) * outpicref->linesize[plane],
|
|
|
|
outlink->w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s->xpos = 0;
|
|
|
|
}
|
|
|
|
for (plane = 0; plane < 3; plane++) {
|
|
|
|
uint8_t *p = outpicref->data[plane] +
|
|
|
|
s->xpos * outpicref->linesize[plane];
|
|
|
|
for (x = 0; x < outlink->w; x++) {
|
|
|
|
*p = lrint(FFMAX(0, FFMIN(s->combine_buffer[3 * x + plane], 255)));
|
|
|
|
p++;
|
|
|
|
}
|
2015-08-04 15:18:51 +02:00
|
|
|
}
|
|
|
|
}
|
2013-02-01 16:33:17 +01:00
|
|
|
|
2015-08-04 15:18:51 +02:00
|
|
|
if (s->sliding != FULLFRAME || s->xpos == 0)
|
|
|
|
outpicref->pts = insamples->pts;
|
2014-08-03 19:30:04 +02:00
|
|
|
|
2015-08-04 15:18:51 +02:00
|
|
|
s->xpos++;
|
2015-12-29 09:45:44 +01:00
|
|
|
if (s->orientation == VERTICAL && s->xpos >= outlink->w)
|
|
|
|
s->xpos = 0;
|
|
|
|
if (s->orientation == HORIZONTAL && s->xpos >= outlink->h)
|
2015-08-04 15:18:51 +02:00
|
|
|
s->xpos = 0;
|
2015-12-31 20:45:37 +01:00
|
|
|
if (!s->single_pic && (s->sliding != FULLFRAME || s->xpos == 0)) {
|
2015-08-04 15:18:51 +02:00
|
|
|
ret = ff_filter_frame(outlink, av_frame_clone(s->outpicref));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2015-12-31 20:31:32 +01:00
|
|
|
return s->win_size;
|
2011-12-29 23:45:45 +01:00
|
|
|
}
|
|
|
|
|
2015-12-31 20:45:37 +01:00
|
|
|
#if CONFIG_SHOWSPECTRUM_FILTER
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
ShowSpectrumContext *s = outlink->src->priv;
|
|
|
|
AVFilterLink *inlink = outlink->src->inputs[0];
|
|
|
|
unsigned i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_request_frame(inlink);
|
|
|
|
if (ret == AVERROR_EOF && s->sliding == FULLFRAME && s->xpos > 0 &&
|
|
|
|
s->outpicref) {
|
|
|
|
if (s->orientation == VERTICAL) {
|
|
|
|
for (i = 0; i < outlink->h; i++) {
|
|
|
|
memset(s->outpicref->data[0] + i * s->outpicref->linesize[0] + s->xpos, 0, outlink->w - s->xpos);
|
|
|
|
memset(s->outpicref->data[1] + i * s->outpicref->linesize[1] + s->xpos, 128, outlink->w - s->xpos);
|
|
|
|
memset(s->outpicref->data[2] + i * s->outpicref->linesize[2] + s->xpos, 128, outlink->w - s->xpos);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = s->xpos; i < outlink->h; i++) {
|
|
|
|
memset(s->outpicref->data[0] + i * s->outpicref->linesize[0], 0, outlink->w);
|
|
|
|
memset(s->outpicref->data[1] + i * s->outpicref->linesize[1], 128, outlink->w);
|
|
|
|
memset(s->outpicref->data[2] + i * s->outpicref->linesize[2], 128, outlink->w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = ff_filter_frame(outlink, s->outpicref);
|
|
|
|
s->outpicref = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-10 01:30:30 +01:00
|
|
|
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
|
2011-12-29 23:45:45 +01:00
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
2013-05-16 18:28:25 +02:00
|
|
|
ShowSpectrumContext *s = ctx->priv;
|
2015-12-29 16:52:16 +01:00
|
|
|
AVFrame *fin = NULL;
|
2014-08-03 16:58:27 +02:00
|
|
|
int ret = 0;
|
2011-12-29 23:45:45 +01:00
|
|
|
|
2015-12-29 16:52:16 +01:00
|
|
|
av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
|
2013-03-10 01:30:30 +01:00
|
|
|
av_frame_free(&insamples);
|
2015-12-31 20:31:32 +01:00
|
|
|
while (av_audio_fifo_size(s->fifo) >= s->win_size) {
|
|
|
|
fin = ff_get_audio_buffer(inlink, s->win_size);
|
2015-12-29 16:52:16 +01:00
|
|
|
if (!fin) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fin->pts = s->pts;
|
|
|
|
s->pts += s->skip_samples;
|
2015-12-31 20:31:32 +01:00
|
|
|
ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
|
2015-12-29 16:52:16 +01:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2015-12-31 20:45:37 +01:00
|
|
|
av_assert0(fin->nb_samples == s->win_size);
|
|
|
|
|
|
|
|
run_rdft(s, fin);
|
|
|
|
calc_magnitudes(s);
|
|
|
|
|
2015-12-29 16:52:16 +01:00
|
|
|
ret = plot_spectrum_column(inlink, fin);
|
|
|
|
av_frame_free(&fin);
|
|
|
|
av_audio_fifo_drain(s->fifo, s->skip_samples);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
av_frame_free(&fin);
|
2013-03-13 09:56:35 +01:00
|
|
|
return ret;
|
2011-12-29 23:45:45 +01:00
|
|
|
}
|
|
|
|
|
2012-11-28 20:01:59 +01:00
|
|
|
static const AVFilterPad showspectrum_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad showspectrum_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_output,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
2013-10-29 11:50:56 +01:00
|
|
|
AVFilter ff_avf_showspectrum = {
|
2013-09-07 14:13:50 +02:00
|
|
|
.name = "showspectrum",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output."),
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.priv_size = sizeof(ShowSpectrumContext),
|
|
|
|
.inputs = showspectrum_inputs,
|
|
|
|
.outputs = showspectrum_outputs,
|
|
|
|
.priv_class = &showspectrum_class,
|
2011-12-29 23:45:45 +01:00
|
|
|
};
|
2015-12-31 20:45:37 +01:00
|
|
|
#endif // CONFIG_SHOWSPECTRUM_FILTER
|
|
|
|
|
|
|
|
#if CONFIG_SHOWSPECTRUMPIC_FILTER
|
|
|
|
|
|
|
|
static const AVOption showspectrumpic_options[] = {
|
|
|
|
{ "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
|
|
|
|
{ "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "4096x2048"}, 0, 0, FLAGS },
|
|
|
|
{ "mode", "set channel display mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=COMBINED}, 0, NB_MODES-1, FLAGS, "mode" },
|
|
|
|
{ "combined", "combined mode", 0, AV_OPT_TYPE_CONST, {.i64=COMBINED}, 0, 0, FLAGS, "mode" },
|
|
|
|
{ "separate", "separate mode", 0, AV_OPT_TYPE_CONST, {.i64=SEPARATE}, 0, 0, FLAGS, "mode" },
|
|
|
|
{ "color", "set channel coloring", OFFSET(color_mode), AV_OPT_TYPE_INT, {.i64=INTENSITY}, 0, NB_CLMODES-1, FLAGS, "color" },
|
|
|
|
{ "channel", "separate color for each channel", 0, AV_OPT_TYPE_CONST, {.i64=CHANNEL}, 0, 0, FLAGS, "color" },
|
|
|
|
{ "intensity", "intensity based coloring", 0, AV_OPT_TYPE_CONST, {.i64=INTENSITY}, 0, 0, FLAGS, "color" },
|
|
|
|
{ "rainbow", "rainbow based coloring", 0, AV_OPT_TYPE_CONST, {.i64=RAINBOW}, 0, 0, FLAGS, "color" },
|
|
|
|
{ "moreland", "moreland based coloring", 0, AV_OPT_TYPE_CONST, {.i64=MORELAND}, 0, 0, FLAGS, "color" },
|
|
|
|
{ "nebulae", "nebulae based coloring", 0, AV_OPT_TYPE_CONST, {.i64=NEBULAE}, 0, 0, FLAGS, "color" },
|
|
|
|
{ "fire", "fire based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIRE}, 0, 0, FLAGS, "color" },
|
2016-01-01 23:22:00 +01:00
|
|
|
{ "fiery", "fiery based coloring", 0, AV_OPT_TYPE_CONST, {.i64=FIERY}, 0, 0, FLAGS, "color" },
|
2015-12-31 20:45:37 +01:00
|
|
|
{ "scale", "set display scale", OFFSET(scale), AV_OPT_TYPE_INT, {.i64=LOG}, 0, NB_SCALES-1, FLAGS, "scale" },
|
|
|
|
{ "sqrt", "square root", 0, AV_OPT_TYPE_CONST, {.i64=SQRT}, 0, 0, FLAGS, "scale" },
|
|
|
|
{ "cbrt", "cubic root", 0, AV_OPT_TYPE_CONST, {.i64=CBRT}, 0, 0, FLAGS, "scale" },
|
2016-01-02 18:10:03 +01:00
|
|
|
{ "4thrt","4th root", 0, AV_OPT_TYPE_CONST, {.i64=FOURTHRT}, 0, 0, FLAGS, "scale" },
|
|
|
|
{ "5thrt","5th root", 0, AV_OPT_TYPE_CONST, {.i64=FIFTHRT}, 0, 0, FLAGS, "scale" },
|
2015-12-31 20:45:37 +01:00
|
|
|
{ "log", "logarithmic", 0, AV_OPT_TYPE_CONST, {.i64=LOG}, 0, 0, FLAGS, "scale" },
|
|
|
|
{ "lin", "linear", 0, AV_OPT_TYPE_CONST, {.i64=LINEAR}, 0, 0, FLAGS, "scale" },
|
|
|
|
{ "saturation", "color saturation multiplier", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl = 1}, -10, 10, FLAGS },
|
|
|
|
{ "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" },
|
|
|
|
{ "hann", "Hann", 0, AV_OPT_TYPE_CONST, {.i64=WFUNC_HANNING}, 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" },
|
|
|
|
{ "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" },
|
|
|
|
{ "orientation", "set orientation", OFFSET(orientation), AV_OPT_TYPE_INT, {.i64=VERTICAL}, 0, NB_ORIENTATIONS-1, FLAGS, "orientation" },
|
|
|
|
{ "vertical", NULL, 0, AV_OPT_TYPE_CONST, {.i64=VERTICAL}, 0, 0, FLAGS, "orientation" },
|
|
|
|
{ "horizontal", NULL, 0, AV_OPT_TYPE_CONST, {.i64=HORIZONTAL}, 0, 0, FLAGS, "orientation" },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(showspectrumpic);
|
|
|
|
|
|
|
|
static int showspectrumpic_request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
|
|
|
ShowSpectrumContext *s = outlink->src->priv;
|
|
|
|
AVFilterLink *inlink = outlink->src->inputs[0];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ff_request_frame(inlink);
|
|
|
|
if (ret == AVERROR_EOF && s->outpicref) {
|
|
|
|
int samples = av_audio_fifo_size(s->fifo);
|
|
|
|
int consumed = 0;
|
|
|
|
int x = 0, sz = s->orientation == VERTICAL ? s->w : s->h;
|
|
|
|
int ch, spf, spb;
|
|
|
|
AVFrame *fin;
|
|
|
|
|
|
|
|
spf = s->win_size * (samples / ((s->win_size * sz) * ceil(samples / (float)(s->win_size * sz))));
|
|
|
|
spb = (samples / (spf * sz)) * spf;
|
|
|
|
|
|
|
|
fin = ff_get_audio_buffer(inlink, s->win_size);
|
|
|
|
if (!fin)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
while (x < sz) {
|
|
|
|
ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_free(&fin);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_audio_fifo_drain(s->fifo, spf);
|
|
|
|
|
|
|
|
if (ret < s->win_size) {
|
|
|
|
for (ch = 0; ch < s->nb_display_channels; ch++) {
|
|
|
|
memset(fin->extended_data[ch] + ret * sizeof(int16_t), 0,
|
|
|
|
(s->win_size - ret) * sizeof(int16_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run_rdft(s, fin);
|
|
|
|
acalc_magnitudes(s);
|
|
|
|
|
|
|
|
consumed += spf;
|
|
|
|
if (consumed >= spb) {
|
|
|
|
int h = s->orientation == VERTICAL ? s->h : s->w;
|
|
|
|
|
|
|
|
scale_magnitudes(s, 1. / (consumed / spf));
|
|
|
|
plot_spectrum_column(inlink, fin);
|
|
|
|
consumed = 0;
|
|
|
|
x++;
|
|
|
|
for (ch = 0; ch < s->nb_display_channels; ch++)
|
|
|
|
memset(s->magnitudes[ch], 0, h * sizeof(float));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_free(&fin);
|
|
|
|
s->outpicref->pts = 0;
|
|
|
|
ret = ff_filter_frame(outlink, s->outpicref);
|
|
|
|
s->outpicref = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int showspectrumpic_filter_frame(AVFilterLink *inlink, AVFrame *insamples)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->dst;
|
|
|
|
ShowSpectrumContext *s = ctx->priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = av_audio_fifo_write(s->fifo, (void **)insamples->extended_data, insamples->nb_samples);
|
|
|
|
av_frame_free(&insamples);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad showspectrumpic_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = showspectrumpic_filter_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad showspectrumpic_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_output,
|
|
|
|
.request_frame = showspectrumpic_request_frame,
|
|
|
|
},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFilter ff_avf_showspectrumpic = {
|
|
|
|
.name = "showspectrumpic",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Convert input audio to a spectrum video output single picture."),
|
|
|
|
.uninit = uninit,
|
|
|
|
.query_formats = query_formats,
|
|
|
|
.priv_size = sizeof(ShowSpectrumContext),
|
|
|
|
.inputs = showspectrumpic_inputs,
|
|
|
|
.outputs = showspectrumpic_outputs,
|
|
|
|
.priv_class = &showspectrumpic_class,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // CONFIG_SHOWSPECTRUMPIC_FILTER
|