2022-11-19 20:01:23 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022 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 <float.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2022-11-19 20:01:23 +02:00
|
|
|
#include "libavutil/tx.h"
|
|
|
|
#include "libavutil/channel_layout.h"
|
2023-07-10 12:08:13 +02:00
|
|
|
#include "libavutil/float_dsp.h"
|
2022-11-19 20:01:23 +02:00
|
|
|
#include "libavutil/cpu.h"
|
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
|
|
|
#include "audio.h"
|
2023-08-03 23:44:07 +02:00
|
|
|
#include "formats.h"
|
2022-11-19 20:01:23 +02:00
|
|
|
#include "video.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "filters.h"
|
|
|
|
|
|
|
|
enum FrequencyScale {
|
|
|
|
FSCALE_LINEAR,
|
2023-07-30 17:40:07 +02:00
|
|
|
FSCALE_LOG,
|
2022-11-19 20:01:23 +02:00
|
|
|
FSCALE_BARK,
|
|
|
|
FSCALE_MEL,
|
|
|
|
FSCALE_ERBS,
|
2023-07-09 19:56:10 +02:00
|
|
|
FSCALE_SQRT,
|
2023-07-09 20:06:46 +02:00
|
|
|
FSCALE_CBRT,
|
2023-07-09 20:50:04 +02:00
|
|
|
FSCALE_QDRT,
|
2023-11-11 01:44:36 +02:00
|
|
|
FSCALE_FM,
|
2022-11-19 20:01:23 +02:00
|
|
|
NB_FSCALE
|
|
|
|
};
|
|
|
|
|
2023-07-30 17:36:50 +02:00
|
|
|
enum IntensityScale {
|
|
|
|
ISCALE_LOG,
|
|
|
|
ISCALE_LINEAR,
|
|
|
|
ISCALE_SQRT,
|
|
|
|
ISCALE_CBRT,
|
|
|
|
ISCALE_QDRT,
|
|
|
|
NB_ISCALE
|
|
|
|
};
|
|
|
|
|
2022-11-19 20:01:23 +02:00
|
|
|
enum DirectionMode {
|
|
|
|
DIRECTION_LR,
|
|
|
|
DIRECTION_RL,
|
|
|
|
DIRECTION_UD,
|
|
|
|
DIRECTION_DU,
|
|
|
|
NB_DIRECTION
|
|
|
|
};
|
|
|
|
|
|
|
|
enum SlideMode {
|
|
|
|
SLIDE_REPLACE,
|
|
|
|
SLIDE_SCROLL,
|
2022-11-30 19:03:00 +02:00
|
|
|
SLIDE_FRAME,
|
2022-11-19 20:01:23 +02:00
|
|
|
NB_SLIDE
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct ShowCWTContext {
|
|
|
|
const AVClass *class;
|
|
|
|
int w, h;
|
|
|
|
int mode;
|
|
|
|
char *rate_str;
|
|
|
|
AVRational auto_frame_rate;
|
|
|
|
AVRational frame_rate;
|
2023-07-21 23:11:11 +02:00
|
|
|
AVTXContext **fft, **ifft;
|
|
|
|
av_tx_fn tx_fn, itx_fn;
|
2023-08-01 11:12:27 +02:00
|
|
|
int fft_size, ifft_size;
|
2022-11-19 20:01:23 +02:00
|
|
|
int pos;
|
|
|
|
int64_t in_pts;
|
|
|
|
int64_t old_pts;
|
2022-11-29 15:03:34 +02:00
|
|
|
int64_t eof_pts;
|
2022-11-19 20:01:23 +02:00
|
|
|
float *frequency_band;
|
2023-07-22 21:45:48 +02:00
|
|
|
AVComplexFloat **kernel;
|
2022-11-19 20:01:23 +02:00
|
|
|
unsigned *index;
|
2023-07-21 23:11:11 +02:00
|
|
|
int *kernel_start, *kernel_stop;
|
2023-07-10 11:00:24 +02:00
|
|
|
AVFrame *cache;
|
2022-11-19 20:01:23 +02:00
|
|
|
AVFrame *outpicref;
|
|
|
|
AVFrame *fft_in;
|
|
|
|
AVFrame *fft_out;
|
2023-07-10 12:08:13 +02:00
|
|
|
AVFrame *dst_x;
|
|
|
|
AVFrame *src_x;
|
2022-11-19 20:01:23 +02:00
|
|
|
AVFrame *ifft_in;
|
|
|
|
AVFrame *ifft_out;
|
|
|
|
AVFrame *ch_out;
|
2023-07-27 14:36:13 +02:00
|
|
|
AVFrame *over;
|
2023-07-10 22:52:04 +02:00
|
|
|
AVFrame *bh_out;
|
2022-11-19 20:01:23 +02:00
|
|
|
int nb_threads;
|
|
|
|
int nb_channels;
|
|
|
|
int nb_consumed_samples;
|
|
|
|
int pps;
|
2022-11-29 15:03:34 +02:00
|
|
|
int eof;
|
2022-11-19 20:01:23 +02:00
|
|
|
int slide;
|
2022-11-30 19:03:00 +02:00
|
|
|
int new_frame;
|
2022-11-19 20:01:23 +02:00
|
|
|
int direction;
|
2023-07-21 23:11:11 +02:00
|
|
|
int hop_size, ihop_size;
|
|
|
|
int hop_index, ihop_index;
|
|
|
|
int input_padding_size, output_padding_size;
|
|
|
|
int input_sample_count, output_sample_count;
|
2022-11-19 20:01:23 +02:00
|
|
|
int frequency_band_count;
|
|
|
|
float logarithmic_basis;
|
2023-07-30 17:36:50 +02:00
|
|
|
int intensity_scale;
|
2022-11-19 20:01:23 +02:00
|
|
|
int frequency_scale;
|
2023-07-21 23:11:11 +02:00
|
|
|
float minimum_frequency, maximum_frequency;
|
2023-08-01 10:46:14 +02:00
|
|
|
float minimum_intensity, maximum_intensity;
|
2022-11-19 20:01:23 +02:00
|
|
|
float deviation;
|
2023-07-10 22:52:04 +02:00
|
|
|
float bar_ratio;
|
|
|
|
int bar_size;
|
2023-11-11 23:24:34 +02:00
|
|
|
int sono_size;
|
2023-07-12 22:49:56 +02:00
|
|
|
float rotation;
|
2023-07-10 12:08:13 +02:00
|
|
|
|
|
|
|
AVFloatDSPContext *fdsp;
|
2022-11-19 20:01:23 +02:00
|
|
|
} ShowCWTContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(ShowCWTContext, x)
|
|
|
|
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
|
|
|
|
|
|
|
|
static const AVOption showcwt_options[] = {
|
|
|
|
{ "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 },
|
|
|
|
{ "rate", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
|
|
|
|
{ "r", "set video rate", OFFSET(rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
|
2024-02-11 16:41:05 +02:00
|
|
|
{ "scale", "set frequency scale", OFFSET(frequency_scale), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_FSCALE-1, FLAGS, .unit = "scale" },
|
|
|
|
{ "linear", "linear", 0, AV_OPT_TYPE_CONST,{.i64=FSCALE_LINEAR}, 0, 0, FLAGS, .unit = "scale" },
|
|
|
|
{ "log", "logarithmic", 0, AV_OPT_TYPE_CONST,{.i64=FSCALE_LOG}, 0, 0, FLAGS, .unit = "scale" },
|
|
|
|
{ "bark", "bark", 0, AV_OPT_TYPE_CONST,{.i64=FSCALE_BARK}, 0, 0, FLAGS, .unit = "scale" },
|
|
|
|
{ "mel", "mel", 0, AV_OPT_TYPE_CONST,{.i64=FSCALE_MEL}, 0, 0, FLAGS, .unit = "scale" },
|
|
|
|
{ "erbs", "erbs", 0, AV_OPT_TYPE_CONST,{.i64=FSCALE_ERBS}, 0, 0, FLAGS, .unit = "scale" },
|
|
|
|
{ "sqrt", "sqrt", 0, AV_OPT_TYPE_CONST,{.i64=FSCALE_SQRT}, 0, 0, FLAGS, .unit = "scale" },
|
|
|
|
{ "cbrt", "cbrt", 0, AV_OPT_TYPE_CONST,{.i64=FSCALE_CBRT}, 0, 0, FLAGS, .unit = "scale" },
|
|
|
|
{ "qdrt", "qdrt", 0, AV_OPT_TYPE_CONST,{.i64=FSCALE_QDRT}, 0, 0, FLAGS, .unit = "scale" },
|
|
|
|
{ "fm", "fm", 0, AV_OPT_TYPE_CONST,{.i64=FSCALE_FM}, 0, 0, FLAGS, .unit = "scale" },
|
|
|
|
{ "iscale", "set intensity scale", OFFSET(intensity_scale),AV_OPT_TYPE_INT, {.i64=0}, 0, NB_ISCALE-1, FLAGS, .unit = "iscale" },
|
|
|
|
{ "linear", "linear", 0, AV_OPT_TYPE_CONST,{.i64=ISCALE_LINEAR}, 0, 0, FLAGS, .unit = "iscale" },
|
|
|
|
{ "log", "logarithmic", 0, AV_OPT_TYPE_CONST,{.i64=ISCALE_LOG}, 0, 0, FLAGS, .unit = "iscale" },
|
|
|
|
{ "sqrt", "sqrt", 0, AV_OPT_TYPE_CONST,{.i64=ISCALE_SQRT}, 0, 0, FLAGS, .unit = "iscale" },
|
|
|
|
{ "cbrt", "cbrt", 0, AV_OPT_TYPE_CONST,{.i64=ISCALE_CBRT}, 0, 0, FLAGS, .unit = "iscale" },
|
|
|
|
{ "qdrt", "qdrt", 0, AV_OPT_TYPE_CONST,{.i64=ISCALE_QDRT}, 0, 0, FLAGS, .unit = "iscale" },
|
2023-07-22 17:25:06 +02:00
|
|
|
{ "min", "set minimum frequency", OFFSET(minimum_frequency), AV_OPT_TYPE_FLOAT, {.dbl = 20.}, 1, 192000, FLAGS },
|
|
|
|
{ "max", "set maximum frequency", OFFSET(maximum_frequency), AV_OPT_TYPE_FLOAT, {.dbl = 20000.}, 1, 192000, FLAGS },
|
2023-08-01 10:46:14 +02:00
|
|
|
{ "imin", "set minimum intensity", OFFSET(minimum_intensity), AV_OPT_TYPE_FLOAT, {.dbl = 0.}, 0, 1, FLAGS },
|
|
|
|
{ "imax", "set maximum intensity", OFFSET(maximum_intensity), AV_OPT_TYPE_FLOAT, {.dbl = 1.}, 0, 1, FLAGS },
|
2022-11-19 20:01:23 +02:00
|
|
|
{ "logb", "set logarithmic basis", OFFSET(logarithmic_basis), AV_OPT_TYPE_FLOAT, {.dbl = 0.0001}, 0, 1, FLAGS },
|
2023-07-30 19:57:50 +02:00
|
|
|
{ "deviation", "set frequency deviation", OFFSET(deviation), AV_OPT_TYPE_FLOAT, {.dbl = 1.}, 0, 100, FLAGS },
|
2022-11-19 20:01:23 +02:00
|
|
|
{ "pps", "set pixels per second", OFFSET(pps), AV_OPT_TYPE_INT, {.i64 = 64}, 1, 1024, FLAGS },
|
2024-02-11 16:41:05 +02:00
|
|
|
{ "mode", "set output mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 4, FLAGS, .unit = "mode" },
|
|
|
|
{ "magnitude", "magnitude", 0, AV_OPT_TYPE_CONST,{.i64=0}, 0, 0, FLAGS, .unit = "mode" },
|
|
|
|
{ "phase", "phase", 0, AV_OPT_TYPE_CONST,{.i64=1}, 0, 0, FLAGS, .unit = "mode" },
|
|
|
|
{ "magphase", "magnitude+phase", 0, AV_OPT_TYPE_CONST,{.i64=2}, 0, 0, FLAGS, .unit = "mode" },
|
|
|
|
{ "channel", "color per channel", 0, AV_OPT_TYPE_CONST,{.i64=3}, 0, 0, FLAGS, .unit = "mode" },
|
|
|
|
{ "stereo", "stereo difference", 0, AV_OPT_TYPE_CONST,{.i64=4}, 0, 0, FLAGS, .unit = "mode" },
|
|
|
|
{ "slide", "set slide mode", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_SLIDE-1, FLAGS, .unit = "slide" },
|
|
|
|
{ "replace", "replace", 0, AV_OPT_TYPE_CONST,{.i64=SLIDE_REPLACE},0, 0, FLAGS, .unit = "slide" },
|
|
|
|
{ "scroll", "scroll", 0, AV_OPT_TYPE_CONST,{.i64=SLIDE_SCROLL}, 0, 0, FLAGS, .unit = "slide" },
|
|
|
|
{ "frame", "frame", 0, AV_OPT_TYPE_CONST,{.i64=SLIDE_FRAME}, 0, 0, FLAGS, .unit = "slide" },
|
|
|
|
{ "direction", "set direction mode", OFFSET(direction), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_DIRECTION-1, FLAGS, .unit = "direction" },
|
|
|
|
{ "lr", "left to right", 0, AV_OPT_TYPE_CONST,{.i64=DIRECTION_LR}, 0, 0, FLAGS, .unit = "direction" },
|
|
|
|
{ "rl", "right to left", 0, AV_OPT_TYPE_CONST,{.i64=DIRECTION_RL}, 0, 0, FLAGS, .unit = "direction" },
|
|
|
|
{ "ud", "up to down", 0, AV_OPT_TYPE_CONST,{.i64=DIRECTION_UD}, 0, 0, FLAGS, .unit = "direction" },
|
|
|
|
{ "du", "down to up", 0, AV_OPT_TYPE_CONST,{.i64=DIRECTION_DU}, 0, 0, FLAGS, .unit = "direction" },
|
2023-11-11 23:24:34 +02:00
|
|
|
{ "bar", "set bargraph ratio", OFFSET(bar_ratio), AV_OPT_TYPE_FLOAT, {.dbl = 0.}, 0, 1, FLAGS },
|
2023-07-12 22:49:56 +02:00
|
|
|
{ "rotation", "set color rotation", OFFSET(rotation), AV_OPT_TYPE_FLOAT, {.dbl = 0}, -1, 1, FLAGS },
|
2022-11-19 20:01:23 +02:00
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(showcwt);
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
ShowCWTContext *s = ctx->priv;
|
|
|
|
|
|
|
|
av_freep(&s->frequency_band);
|
|
|
|
av_freep(&s->kernel_start);
|
|
|
|
av_freep(&s->kernel_stop);
|
|
|
|
av_freep(&s->index);
|
|
|
|
|
2023-07-10 11:00:24 +02:00
|
|
|
av_frame_free(&s->cache);
|
2022-11-19 20:01:23 +02:00
|
|
|
av_frame_free(&s->outpicref);
|
|
|
|
av_frame_free(&s->fft_in);
|
|
|
|
av_frame_free(&s->fft_out);
|
2023-07-10 12:08:13 +02:00
|
|
|
av_frame_free(&s->dst_x);
|
|
|
|
av_frame_free(&s->src_x);
|
2022-11-19 20:01:23 +02:00
|
|
|
av_frame_free(&s->ifft_in);
|
|
|
|
av_frame_free(&s->ifft_out);
|
|
|
|
av_frame_free(&s->ch_out);
|
2023-07-27 14:36:13 +02:00
|
|
|
av_frame_free(&s->over);
|
2023-07-10 22:52:04 +02:00
|
|
|
av_frame_free(&s->bh_out);
|
2022-11-29 15:39:18 +02:00
|
|
|
|
|
|
|
if (s->fft) {
|
|
|
|
for (int n = 0; n < s->nb_threads; n++)
|
|
|
|
av_tx_uninit(&s->fft[n]);
|
|
|
|
av_freep(&s->fft);
|
|
|
|
}
|
2022-11-19 20:01:23 +02:00
|
|
|
|
|
|
|
if (s->ifft) {
|
|
|
|
for (int n = 0; n < s->nb_threads; n++)
|
|
|
|
av_tx_uninit(&s->ifft[n]);
|
2022-11-29 15:39:18 +02:00
|
|
|
av_freep(&s->ifft);
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
2023-07-10 12:08:13 +02:00
|
|
|
|
2023-07-22 21:45:48 +02:00
|
|
|
if (s->kernel) {
|
|
|
|
for (int n = 0; n < s->frequency_band_count; n++)
|
|
|
|
av_freep(&s->kernel[n]);
|
|
|
|
}
|
|
|
|
av_freep(&s->kernel);
|
|
|
|
|
2023-07-10 12:08:13 +02:00
|
|
|
av_freep(&s->fdsp);
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2024-09-25 10:33:38 +02:00
|
|
|
static int query_formats(const AVFilterContext *ctx,
|
|
|
|
AVFilterFormatsConfig **cfg_in,
|
|
|
|
AVFilterFormatsConfig **cfg_out)
|
2022-11-19 20:01:23 +02:00
|
|
|
{
|
|
|
|
AVFilterFormats *formats = NULL;
|
|
|
|
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_YUVA444P, AV_PIX_FMT_NONE };
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
formats = ff_make_format_list(sample_fmts);
|
2024-09-25 10:33:38 +02:00
|
|
|
if ((ret = ff_formats_ref(formats, &cfg_in[0]->formats)) < 0)
|
2022-11-19 20:01:23 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
formats = ff_make_format_list(pix_fmts);
|
2024-09-25 10:33:38 +02:00
|
|
|
if ((ret = ff_formats_ref(formats, &cfg_out[0]->formats)) < 0)
|
2022-11-19 20:01:23 +02:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-03 21:41:53 +02:00
|
|
|
static float frequency_band(float *frequency_band,
|
|
|
|
int frequency_band_count,
|
|
|
|
float frequency_range,
|
|
|
|
float frequency_offset,
|
|
|
|
int frequency_scale, float deviation)
|
2022-11-19 20:01:23 +02:00
|
|
|
{
|
2023-08-03 21:41:53 +02:00
|
|
|
float ret = 0.f;
|
|
|
|
|
|
|
|
deviation = sqrtf(deviation / (4.f * M_PI)); // Heisenberg Gabor Limit
|
2022-11-19 20:01:23 +02:00
|
|
|
for (int y = 0; y < frequency_band_count; y++) {
|
|
|
|
float frequency = frequency_range * (1.f - (float)y / frequency_band_count) + frequency_offset;
|
|
|
|
float frequency_derivative = frequency_range / frequency_band_count;
|
|
|
|
|
|
|
|
switch (frequency_scale) {
|
2023-07-30 17:40:07 +02:00
|
|
|
case FSCALE_LOG:
|
2022-11-19 20:01:23 +02:00
|
|
|
frequency = powf(2.f, frequency);
|
|
|
|
frequency_derivative *= logf(2.f) * frequency;
|
|
|
|
break;
|
|
|
|
case FSCALE_BARK:
|
|
|
|
frequency = 600.f * sinhf(frequency / 6.f);
|
|
|
|
frequency_derivative *= sqrtf(frequency * frequency + 360000.f) / 6.f;
|
|
|
|
break;
|
|
|
|
case FSCALE_MEL:
|
|
|
|
frequency = 700.f * (powf(10.f, frequency / 2595.f) - 1.f);
|
|
|
|
frequency_derivative *= (frequency + 700.f) * logf(10.f) / 2595.f;
|
|
|
|
break;
|
|
|
|
case FSCALE_ERBS:
|
|
|
|
frequency = 676170.4f / (47.06538f - expf(frequency * 0.08950404f)) - 14678.49f;
|
2023-08-01 11:12:27 +02:00
|
|
|
frequency_derivative *= (frequency * frequency + 14990.4f * frequency + 4577850.f) / 160514.f;
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
2023-07-09 19:56:10 +02:00
|
|
|
case FSCALE_SQRT:
|
|
|
|
frequency = frequency * frequency;
|
|
|
|
frequency_derivative *= 2.f * sqrtf(frequency);
|
|
|
|
break;
|
2023-07-09 20:06:46 +02:00
|
|
|
case FSCALE_CBRT:
|
|
|
|
frequency = frequency * frequency * frequency;
|
|
|
|
frequency_derivative *= 3.f * powf(frequency, 2.f / 3.f);
|
|
|
|
break;
|
2023-07-09 20:50:04 +02:00
|
|
|
case FSCALE_QDRT:
|
|
|
|
frequency = frequency * frequency * frequency * frequency;
|
|
|
|
frequency_derivative *= 4.f * powf(frequency, 3.f / 4.f);
|
|
|
|
break;
|
2023-11-11 01:44:36 +02:00
|
|
|
case FSCALE_FM:
|
|
|
|
frequency = 2.f * powf(frequency, 3.f / 2.f) / 3.f;
|
|
|
|
frequency_derivative *= sqrtf(frequency);
|
|
|
|
break;
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-08-03 21:41:53 +02:00
|
|
|
frequency_band[y*2 ] = frequency;
|
|
|
|
frequency_band[y*2+1] = frequency_derivative * deviation;
|
|
|
|
|
|
|
|
ret = 1.f / (frequency_derivative * deviation);
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
2023-08-03 21:41:53 +02:00
|
|
|
|
|
|
|
return ret;
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-08-01 10:46:14 +02:00
|
|
|
static float remap_log(ShowCWTContext *s, float value, int iscale, float log_factor)
|
2022-11-19 20:01:23 +02:00
|
|
|
{
|
2023-08-01 10:46:14 +02:00
|
|
|
const float max = s->maximum_intensity;
|
|
|
|
const float min = s->minimum_intensity;
|
2023-07-30 17:36:50 +02:00
|
|
|
float ret;
|
|
|
|
|
2023-08-01 10:46:14 +02:00
|
|
|
value += min;
|
|
|
|
|
2023-07-30 17:36:50 +02:00
|
|
|
switch (iscale) {
|
|
|
|
case ISCALE_LINEAR:
|
2023-08-01 10:46:14 +02:00
|
|
|
ret = max - expf(value / log_factor);
|
2023-07-30 17:36:50 +02:00
|
|
|
break;
|
|
|
|
case ISCALE_LOG:
|
|
|
|
value = logf(value) * log_factor;
|
2023-08-01 10:46:14 +02:00
|
|
|
ret = max - av_clipf(value, 0.f, 1.f);
|
2023-07-30 17:36:50 +02:00
|
|
|
break;
|
|
|
|
case ISCALE_SQRT:
|
2023-08-01 10:46:14 +02:00
|
|
|
value = max - expf(value / log_factor);
|
|
|
|
ret = sqrtf(value);
|
2023-07-30 17:36:50 +02:00
|
|
|
break;
|
|
|
|
case ISCALE_CBRT:
|
2023-08-01 10:46:14 +02:00
|
|
|
value = max - expf(value / log_factor);
|
|
|
|
ret = cbrtf(value);
|
2023-07-30 17:36:50 +02:00
|
|
|
break;
|
|
|
|
case ISCALE_QDRT:
|
2023-08-01 10:46:14 +02:00
|
|
|
value = max - expf(value / log_factor);
|
|
|
|
ret = powf(value, 0.25f);
|
2023-07-30 17:36:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2023-08-01 10:46:14 +02:00
|
|
|
return av_clipf(ret, 0.f, 1.f);
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2022-11-29 15:39:18 +02:00
|
|
|
static int run_channel_cwt_prepare(AVFilterContext *ctx, void *arg, int jobnr, int ch)
|
2022-11-19 20:01:23 +02:00
|
|
|
{
|
|
|
|
ShowCWTContext *s = ctx->priv;
|
2022-11-30 17:36:30 +02:00
|
|
|
const int hop_size = s->hop_size;
|
2022-11-19 20:01:23 +02:00
|
|
|
AVFrame *fin = arg;
|
2023-07-27 14:36:13 +02:00
|
|
|
float *cache = (float *)s->cache->extended_data[ch];
|
2022-11-19 20:01:23 +02:00
|
|
|
AVComplexFloat *src = (AVComplexFloat *)s->fft_in->extended_data[ch];
|
|
|
|
AVComplexFloat *dst = (AVComplexFloat *)s->fft_out->extended_data[ch];
|
2023-07-27 14:36:13 +02:00
|
|
|
const int offset = (s->input_padding_size - hop_size) >> 1;
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2022-11-29 15:03:34 +02:00
|
|
|
if (fin) {
|
|
|
|
const float *input = (const float *)fin->extended_data[ch];
|
2023-07-27 14:36:13 +02:00
|
|
|
const int offset = s->hop_size - fin->nb_samples;
|
2022-11-29 01:27:14 +02:00
|
|
|
|
2023-07-27 14:36:13 +02:00
|
|
|
memmove(cache, &cache[fin->nb_samples], offset * sizeof(float));
|
|
|
|
memcpy(&cache[offset], input, fin->nb_samples * sizeof(float));
|
2022-11-29 15:03:34 +02:00
|
|
|
}
|
|
|
|
|
2023-07-10 11:00:24 +02:00
|
|
|
if (fin && s->hop_index + fin->nb_samples < hop_size)
|
2022-11-29 01:27:14 +02:00
|
|
|
return 0;
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2023-08-01 11:12:27 +02:00
|
|
|
memset(src, 0, sizeof(float) * s->fft_size);
|
|
|
|
for (int n = 0; n < hop_size; n++)
|
2023-07-27 14:36:13 +02:00
|
|
|
src[n+offset].re = cache[n];
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2022-11-29 15:39:18 +02:00
|
|
|
s->tx_fn(s->fft[jobnr], dst, src, sizeof(*src));
|
2022-11-19 20:01:23 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-10 22:52:04 +02:00
|
|
|
#define DRAW_BAR_COLOR(x) \
|
|
|
|
do { \
|
|
|
|
if (Y <= ht) { \
|
|
|
|
dstY[x] = 0; \
|
|
|
|
dstU[x] = 128; \
|
|
|
|
dstV[x] = 128; \
|
|
|
|
} else { \
|
|
|
|
float mul = (Y - ht) * bh[0]; \
|
|
|
|
dstY[x] = av_clip_uint8(lrintf(Y * mul * 255.f)); \
|
|
|
|
dstU[x] = av_clip_uint8(lrintf((U-0.5f) * 128.f + 128)); \
|
|
|
|
dstV[x] = av_clip_uint8(lrintf((V-0.5f) * 128.f + 128)); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static void draw_bar(ShowCWTContext *s, int y,
|
|
|
|
float Y, float U, float V)
|
|
|
|
{
|
|
|
|
float *bh = ((float *)s->bh_out->extended_data[0]) + y;
|
|
|
|
const ptrdiff_t ylinesize = s->outpicref->linesize[0];
|
|
|
|
const ptrdiff_t ulinesize = s->outpicref->linesize[1];
|
|
|
|
const ptrdiff_t vlinesize = s->outpicref->linesize[2];
|
|
|
|
const int direction = s->direction;
|
2023-11-11 23:24:34 +02:00
|
|
|
const int sono_size = s->sono_size;
|
2023-07-10 22:52:04 +02:00
|
|
|
const int bar_size = s->bar_size;
|
|
|
|
const float rcp_bar_h = 1.f / bar_size;
|
|
|
|
uint8_t *dstY, *dstU, *dstV;
|
2023-11-11 23:24:34 +02:00
|
|
|
const int w = s->w;
|
2023-07-10 22:52:04 +02:00
|
|
|
|
|
|
|
bh[0] = 1.f / (Y + 0.0001f);
|
|
|
|
switch (direction) {
|
2023-11-11 23:24:34 +02:00
|
|
|
case DIRECTION_LR:
|
|
|
|
dstY = s->outpicref->data[0] + y * ylinesize;
|
|
|
|
dstU = s->outpicref->data[1] + y * ulinesize;
|
|
|
|
dstV = s->outpicref->data[2] + y * vlinesize;
|
|
|
|
for (int x = 0; x < bar_size; x++) {
|
|
|
|
float ht = (bar_size - x) * rcp_bar_h;
|
|
|
|
DRAW_BAR_COLOR(x);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIRECTION_RL:
|
|
|
|
dstY = s->outpicref->data[0] + y * ylinesize;
|
|
|
|
dstU = s->outpicref->data[1] + y * ulinesize;
|
|
|
|
dstV = s->outpicref->data[2] + y * vlinesize;
|
|
|
|
for (int x = 0; x < bar_size; x++) {
|
|
|
|
float ht = x * rcp_bar_h;
|
|
|
|
DRAW_BAR_COLOR(w - bar_size + x);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIRECTION_UD:
|
|
|
|
dstY = s->outpicref->data[0] + w - 1 - y;
|
|
|
|
dstU = s->outpicref->data[1] + w - 1 - y;
|
|
|
|
dstV = s->outpicref->data[2] + w - 1 - y;
|
|
|
|
for (int x = 0; x < bar_size; x++) {
|
|
|
|
float ht = (bar_size - x) * rcp_bar_h;
|
|
|
|
DRAW_BAR_COLOR(0);
|
|
|
|
dstY += ylinesize;
|
|
|
|
dstU += ulinesize;
|
|
|
|
dstV += vlinesize;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIRECTION_DU:
|
|
|
|
dstY = s->outpicref->data[0] + w - 1 - y + ylinesize * sono_size;
|
|
|
|
dstU = s->outpicref->data[1] + w - 1 - y + ulinesize * sono_size;
|
|
|
|
dstV = s->outpicref->data[2] + w - 1 - y + vlinesize * sono_size;
|
|
|
|
for (int x = 0; x < bar_size; x++) {
|
|
|
|
float ht = x * rcp_bar_h;
|
|
|
|
DRAW_BAR_COLOR(0);
|
|
|
|
dstY += ylinesize;
|
|
|
|
dstU += ulinesize;
|
|
|
|
dstV += vlinesize;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-10 22:52:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 20:01:23 +02:00
|
|
|
static int draw(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
ShowCWTContext *s = ctx->priv;
|
|
|
|
const ptrdiff_t ylinesize = s->outpicref->linesize[0];
|
|
|
|
const ptrdiff_t ulinesize = s->outpicref->linesize[1];
|
|
|
|
const ptrdiff_t vlinesize = s->outpicref->linesize[2];
|
2022-12-04 12:15:21 +02:00
|
|
|
const ptrdiff_t alinesize = s->outpicref->linesize[3];
|
2022-11-19 20:01:23 +02:00
|
|
|
const float log_factor = 1.f/logf(s->logarithmic_basis);
|
|
|
|
const int count = s->frequency_band_count;
|
|
|
|
const int start = (count * jobnr) / nb_jobs;
|
|
|
|
const int end = (count * (jobnr+1)) / nb_jobs;
|
2023-07-30 15:11:53 +02:00
|
|
|
const int nb_channels = s->nb_channels;
|
2023-07-30 17:36:50 +02:00
|
|
|
const int iscale = s->intensity_scale;
|
2022-11-19 20:01:23 +02:00
|
|
|
const int ihop_index = s->ihop_index;
|
|
|
|
const int ihop_size = s->ihop_size;
|
2023-07-12 22:49:56 +02:00
|
|
|
const float rotation = s->rotation;
|
2022-11-19 20:01:23 +02:00
|
|
|
const int direction = s->direction;
|
2022-12-04 12:15:21 +02:00
|
|
|
uint8_t *dstY, *dstU, *dstV, *dstA;
|
2023-11-11 23:24:34 +02:00
|
|
|
const int sono_size = s->sono_size;
|
2023-07-10 22:52:04 +02:00
|
|
|
const int bar_size = s->bar_size;
|
2022-11-19 20:01:23 +02:00
|
|
|
const int mode = s->mode;
|
|
|
|
const int w_1 = s->w - 1;
|
|
|
|
const int x = s->pos;
|
|
|
|
float Y, U, V;
|
|
|
|
|
|
|
|
for (int y = start; y < end; y++) {
|
2023-07-30 15:11:53 +02:00
|
|
|
const AVComplexFloat *src = ((const AVComplexFloat *)s->ch_out->extended_data[y]) +
|
|
|
|
0 * ihop_size + ihop_index;
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2023-11-11 23:24:34 +02:00
|
|
|
if (sono_size <= 0)
|
|
|
|
goto skip;
|
|
|
|
|
2022-11-19 20:01:23 +02:00
|
|
|
switch (direction) {
|
|
|
|
case DIRECTION_LR:
|
|
|
|
case DIRECTION_RL:
|
|
|
|
dstY = s->outpicref->data[0] + y * ylinesize;
|
|
|
|
dstU = s->outpicref->data[1] + y * ulinesize;
|
|
|
|
dstV = s->outpicref->data[2] + y * vlinesize;
|
2022-12-04 12:15:21 +02:00
|
|
|
dstA = s->outpicref->data[3] ? s->outpicref->data[3] + y * alinesize : NULL;
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
|
|
|
case DIRECTION_UD:
|
|
|
|
case DIRECTION_DU:
|
|
|
|
dstY = s->outpicref->data[0] + x * ylinesize + w_1 - y;
|
|
|
|
dstU = s->outpicref->data[1] + x * ulinesize + w_1 - y;
|
|
|
|
dstV = s->outpicref->data[2] + x * vlinesize + w_1 - y;
|
2022-12-04 12:15:21 +02:00
|
|
|
dstA = s->outpicref->data[3] ? s->outpicref->data[3] + x * alinesize + w_1 - y : NULL;
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (s->slide) {
|
|
|
|
case SLIDE_REPLACE:
|
2022-11-30 19:03:00 +02:00
|
|
|
case SLIDE_FRAME:
|
2022-11-19 20:01:23 +02:00
|
|
|
/* nothing to do here */
|
|
|
|
break;
|
|
|
|
case SLIDE_SCROLL:
|
|
|
|
switch (s->direction) {
|
|
|
|
case DIRECTION_RL:
|
|
|
|
memmove(dstY, dstY + 1, w_1);
|
|
|
|
memmove(dstU, dstU + 1, w_1);
|
|
|
|
memmove(dstV, dstV + 1, w_1);
|
2022-12-04 12:15:21 +02:00
|
|
|
if (dstA != NULL)
|
|
|
|
memmove(dstA, dstA + 1, w_1);
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
|
|
|
case DIRECTION_LR:
|
|
|
|
memmove(dstY + 1, dstY, w_1);
|
|
|
|
memmove(dstU + 1, dstU, w_1);
|
|
|
|
memmove(dstV + 1, dstV, w_1);
|
2022-12-04 12:15:21 +02:00
|
|
|
if (dstA != NULL)
|
|
|
|
memmove(dstA + 1, dstA, w_1);
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction == DIRECTION_RL ||
|
|
|
|
direction == DIRECTION_LR) {
|
|
|
|
dstY += x;
|
|
|
|
dstU += x;
|
|
|
|
dstV += x;
|
2022-12-04 12:15:21 +02:00
|
|
|
if (dstA != NULL)
|
|
|
|
dstA += x;
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
2023-11-11 23:24:34 +02:00
|
|
|
skip:
|
2022-11-19 20:01:23 +02:00
|
|
|
|
|
|
|
switch (mode) {
|
2022-11-30 20:55:17 +02:00
|
|
|
case 4:
|
|
|
|
{
|
2023-07-30 15:11:53 +02:00
|
|
|
const AVComplexFloat *src2 = (nb_channels > 1) ? src + ihop_size: src;
|
2022-11-30 20:55:17 +02:00
|
|
|
float z, u, v;
|
|
|
|
|
|
|
|
z = hypotf(src[0].re + src2[0].re, src[0].im + src2[0].im);
|
|
|
|
u = hypotf(src[0].re, src[0].im);
|
|
|
|
v = hypotf(src2[0].re, src2[0].im);
|
|
|
|
|
2023-08-01 10:46:14 +02:00
|
|
|
z = remap_log(s, z, iscale, log_factor);
|
|
|
|
u = remap_log(s, u, iscale, log_factor);
|
|
|
|
v = remap_log(s, v, iscale, log_factor);
|
2022-11-30 20:55:17 +02:00
|
|
|
|
|
|
|
Y = z;
|
2023-07-12 22:49:56 +02:00
|
|
|
U = sinf((v - u) * M_PI_2);
|
|
|
|
V = sinf((u - v) * M_PI_2);
|
|
|
|
|
|
|
|
u = U * cosf(rotation * M_PI) - V * sinf(rotation * M_PI);
|
|
|
|
v = U * sinf(rotation * M_PI) + V * cosf(rotation * M_PI);
|
|
|
|
|
|
|
|
U = 0.5f + 0.5f * z * u;
|
|
|
|
V = 0.5f + 0.5f * z * v;
|
2022-11-30 20:55:17 +02:00
|
|
|
|
2023-11-11 23:24:34 +02:00
|
|
|
if (sono_size > 0) {
|
|
|
|
dstY[0] = av_clip_uint8(lrintf(Y * 255.f));
|
|
|
|
dstU[0] = av_clip_uint8(lrintf(U * 255.f));
|
|
|
|
dstV[0] = av_clip_uint8(lrintf(V * 255.f));
|
|
|
|
if (dstA)
|
|
|
|
dstA[0] = dstY[0];
|
|
|
|
}
|
2023-07-10 22:52:04 +02:00
|
|
|
|
|
|
|
if (bar_size > 0)
|
|
|
|
draw_bar(s, y, Y, U, V);
|
2022-11-30 20:55:17 +02:00
|
|
|
}
|
|
|
|
break;
|
2022-11-19 20:01:23 +02:00
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
const int nb_channels = s->nb_channels;
|
|
|
|
const float yf = 1.f / nb_channels;
|
|
|
|
|
|
|
|
Y = 0.f;
|
|
|
|
U = V = 0.5f;
|
|
|
|
for (int ch = 0; ch < nb_channels; ch++) {
|
2023-07-30 15:11:53 +02:00
|
|
|
const AVComplexFloat *srcn = src + ihop_size * ch;
|
2022-11-19 20:01:23 +02:00
|
|
|
float z;
|
|
|
|
|
2023-07-30 15:11:53 +02:00
|
|
|
z = hypotf(srcn[0].re, srcn[0].im);
|
2023-08-01 10:46:14 +02:00
|
|
|
z = remap_log(s, z, iscale, log_factor);
|
2022-11-19 20:01:23 +02:00
|
|
|
|
|
|
|
Y += z * yf;
|
2023-07-12 22:49:56 +02:00
|
|
|
U += z * yf * sinf(2.f * M_PI * (ch * yf + rotation));
|
|
|
|
V += z * yf * cosf(2.f * M_PI * (ch * yf + rotation));
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-11-11 23:24:34 +02:00
|
|
|
if (sono_size > 0) {
|
|
|
|
dstY[0] = av_clip_uint8(lrintf(Y * 255.f));
|
|
|
|
dstU[0] = av_clip_uint8(lrintf(U * 255.f));
|
|
|
|
dstV[0] = av_clip_uint8(lrintf(V * 255.f));
|
|
|
|
if (dstA)
|
|
|
|
dstA[0] = dstY[0];
|
|
|
|
}
|
2023-07-10 22:52:04 +02:00
|
|
|
|
|
|
|
if (bar_size > 0)
|
|
|
|
draw_bar(s, y, Y, U, V);
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
Y = hypotf(src[0].re, src[0].im);
|
2023-08-01 10:46:14 +02:00
|
|
|
Y = remap_log(s, Y, iscale, log_factor);
|
2022-11-19 20:01:23 +02:00
|
|
|
U = atan2f(src[0].im, src[0].re);
|
|
|
|
U = 0.5f + 0.5f * U * Y / M_PI;
|
|
|
|
V = 1.f - U;
|
|
|
|
|
2023-11-11 23:24:34 +02:00
|
|
|
if (sono_size > 0) {
|
|
|
|
dstY[0] = av_clip_uint8(lrintf(Y * 255.f));
|
|
|
|
dstU[0] = av_clip_uint8(lrintf(U * 255.f));
|
|
|
|
dstV[0] = av_clip_uint8(lrintf(V * 255.f));
|
|
|
|
if (dstA)
|
|
|
|
dstA[0] = dstY[0];
|
|
|
|
}
|
|
|
|
|
2023-07-10 22:52:04 +02:00
|
|
|
if (bar_size > 0)
|
|
|
|
draw_bar(s, y, Y, U, V);
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
Y = atan2f(src[0].im, src[0].re);
|
|
|
|
Y = 0.5f + 0.5f * Y / M_PI;
|
|
|
|
|
2023-11-11 23:24:34 +02:00
|
|
|
if (sono_size > 0) {
|
|
|
|
dstY[0] = av_clip_uint8(lrintf(Y * 255.f));
|
|
|
|
if (dstA)
|
|
|
|
dstA[0] = dstY[0];
|
|
|
|
}
|
|
|
|
|
2023-07-10 22:52:04 +02:00
|
|
|
if (bar_size > 0)
|
|
|
|
draw_bar(s, y, Y, 0.5f, 0.5f);
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
Y = hypotf(src[0].re, src[0].im);
|
2023-08-01 10:46:14 +02:00
|
|
|
Y = remap_log(s, Y, iscale, log_factor);
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2023-11-11 23:24:34 +02:00
|
|
|
if (sono_size > 0) {
|
|
|
|
dstY[0] = av_clip_uint8(lrintf(Y * 255.f));
|
|
|
|
if (dstA)
|
|
|
|
dstA[0] = dstY[0];
|
|
|
|
}
|
2023-07-10 22:52:04 +02:00
|
|
|
|
|
|
|
if (bar_size > 0)
|
|
|
|
draw_bar(s, y, Y, 0.5f, 0.5f);
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int run_channel_cwt(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
ShowCWTContext *s = ctx->priv;
|
|
|
|
const int ch = *(int *)arg;
|
2023-07-23 09:04:46 +02:00
|
|
|
const AVComplexFloat *fft_out = (const AVComplexFloat *)s->fft_out->extended_data[ch];
|
2023-07-27 13:03:39 +02:00
|
|
|
AVComplexFloat *isrc = (AVComplexFloat *)s->ifft_in->extended_data[jobnr];
|
|
|
|
AVComplexFloat *idst = (AVComplexFloat *)s->ifft_out->extended_data[jobnr];
|
2023-07-21 23:11:11 +02:00
|
|
|
const int output_padding_size = s->output_padding_size;
|
2023-07-29 17:11:28 +02:00
|
|
|
const int input_padding_size = s->input_padding_size;
|
|
|
|
const float scale = 1.f / input_padding_size;
|
2022-11-19 20:01:23 +02:00
|
|
|
const int ihop_size = s->ihop_size;
|
|
|
|
const int count = s->frequency_band_count;
|
|
|
|
const int start = (count * jobnr) / nb_jobs;
|
|
|
|
const int end = (count * (jobnr+1)) / nb_jobs;
|
|
|
|
|
|
|
|
for (int y = start; y < end; y++) {
|
2023-07-30 15:11:53 +02:00
|
|
|
AVComplexFloat *chout = ((AVComplexFloat *)s->ch_out->extended_data[y]) + ch * ihop_size;
|
2023-07-27 14:36:13 +02:00
|
|
|
AVComplexFloat *over = ((AVComplexFloat *)s->over->extended_data[ch]) + y * ihop_size;
|
2023-07-10 12:08:13 +02:00
|
|
|
AVComplexFloat *dstx = (AVComplexFloat *)s->dst_x->extended_data[jobnr];
|
|
|
|
AVComplexFloat *srcx = (AVComplexFloat *)s->src_x->extended_data[jobnr];
|
2023-07-22 21:45:48 +02:00
|
|
|
const AVComplexFloat *kernel = s->kernel[y];
|
2022-11-19 20:01:23 +02:00
|
|
|
const unsigned *index = (const unsigned *)s->index;
|
|
|
|
const int kernel_start = s->kernel_start[y];
|
|
|
|
const int kernel_stop = s->kernel_stop[y];
|
2023-07-27 14:36:13 +02:00
|
|
|
const int kernel_range = kernel_stop - kernel_start + 1;
|
2023-07-29 17:11:28 +02:00
|
|
|
int offset;
|
|
|
|
|
|
|
|
if (kernel_start >= 0) {
|
|
|
|
offset = 0;
|
|
|
|
memcpy(srcx, fft_out + kernel_start, sizeof(*fft_out) * kernel_range);
|
|
|
|
} else {
|
|
|
|
offset = -kernel_start;
|
|
|
|
memcpy(srcx+offset, fft_out, sizeof(*fft_out) * (kernel_range-offset));
|
|
|
|
memcpy(srcx, fft_out+input_padding_size-offset, sizeof(*fft_out)*offset);
|
|
|
|
}
|
2023-07-10 12:08:13 +02:00
|
|
|
|
2023-07-28 21:13:05 +02:00
|
|
|
s->fdsp->vector_fmul_scalar((float *)srcx, (const float *)srcx, scale, FFALIGN(kernel_range * 2, 4));
|
2023-07-10 12:08:13 +02:00
|
|
|
s->fdsp->vector_fmul((float *)dstx, (const float *)srcx,
|
2023-07-22 21:45:48 +02:00
|
|
|
(const float *)kernel, FFALIGN(kernel_range * 2, 16));
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2023-07-21 23:11:11 +02:00
|
|
|
memset(isrc, 0, sizeof(*isrc) * output_padding_size);
|
2023-07-29 17:11:28 +02:00
|
|
|
if (offset == 0) {
|
2023-11-11 18:23:55 +02:00
|
|
|
const unsigned *kindex = index + kernel_start;
|
2023-07-29 17:11:28 +02:00
|
|
|
for (int i = 0; i < kernel_range; i++) {
|
2023-11-11 18:23:55 +02:00
|
|
|
const unsigned n = kindex[i];
|
2023-07-29 17:11:28 +02:00
|
|
|
|
|
|
|
isrc[n].re += dstx[i].re;
|
|
|
|
isrc[n].im += dstx[i].im;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < kernel_range; i++) {
|
|
|
|
const unsigned n = (i-kernel_start) & (output_padding_size-1);
|
2022-11-30 17:36:30 +02:00
|
|
|
|
2023-07-29 17:11:28 +02:00
|
|
|
isrc[n].re += dstx[i].re;
|
|
|
|
isrc[n].im += dstx[i].im;
|
|
|
|
}
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s->itx_fn(s->ifft[jobnr], idst, isrc, sizeof(*isrc));
|
|
|
|
|
2023-07-27 14:36:13 +02:00
|
|
|
memcpy(chout, idst, sizeof(*chout) * ihop_size);
|
|
|
|
for (int n = 0; n < ihop_size; n++) {
|
|
|
|
chout[n].re += over[n].re;
|
|
|
|
chout[n].im += over[n].im;
|
|
|
|
}
|
|
|
|
memcpy(over, idst + ihop_size, sizeof(*over) * ihop_size);
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-21 23:11:11 +02:00
|
|
|
static int compute_kernel(AVFilterContext *ctx)
|
2022-11-19 20:01:23 +02:00
|
|
|
{
|
|
|
|
ShowCWTContext *s = ctx->priv;
|
2023-07-28 22:25:22 +02:00
|
|
|
const int size = s->input_padding_size;
|
2022-11-19 20:01:23 +02:00
|
|
|
const int output_sample_count = s->output_sample_count;
|
|
|
|
const int fsize = s->frequency_band_count;
|
2023-07-20 22:30:42 +02:00
|
|
|
int *kernel_start = s->kernel_start;
|
|
|
|
int *kernel_stop = s->kernel_stop;
|
2022-11-19 20:01:23 +02:00
|
|
|
unsigned *index = s->index;
|
2023-07-30 13:16:46 +02:00
|
|
|
int range_min = INT_MAX;
|
|
|
|
int range_max = 0, ret = 0;
|
2023-07-22 21:45:48 +02:00
|
|
|
float *tkernel;
|
|
|
|
|
|
|
|
tkernel = av_malloc_array(size, sizeof(*tkernel));
|
|
|
|
if (!tkernel)
|
|
|
|
return AVERROR(ENOMEM);
|
2022-11-19 20:01:23 +02:00
|
|
|
|
|
|
|
for (int y = 0; y < fsize; y++) {
|
2023-07-22 21:45:48 +02:00
|
|
|
AVComplexFloat *kernel = s->kernel[y];
|
2023-07-30 19:57:50 +02:00
|
|
|
int start = INT_MIN, stop = INT_MAX;
|
2023-08-01 11:12:27 +02:00
|
|
|
const float frequency = s->frequency_band[y*2];
|
2023-07-29 17:11:28 +02:00
|
|
|
const float deviation = 1.f / (s->frequency_band[y*2+1] *
|
2023-08-01 11:12:27 +02:00
|
|
|
output_sample_count);
|
2023-07-30 19:57:50 +02:00
|
|
|
const int a = FFMAX(frequency-12.f*sqrtf(1.f/deviation)-0.5f, -size);
|
|
|
|
const int b = FFMIN(frequency+12.f*sqrtf(1.f/deviation)-0.5f, size+a);
|
|
|
|
const int range = -a;
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2023-07-22 21:45:48 +02:00
|
|
|
memset(tkernel, 0, size * sizeof(*tkernel));
|
2023-07-30 13:16:46 +02:00
|
|
|
for (int n = a; n < b; n++) {
|
2023-07-29 11:21:28 +02:00
|
|
|
float ff, f = n+0.5f-frequency;
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2023-07-28 21:13:05 +02:00
|
|
|
ff = expf(-f*f*deviation);
|
2023-07-29 17:11:28 +02:00
|
|
|
tkernel[n+range] = ff;
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-07-30 13:16:46 +02:00
|
|
|
for (int n = a; n < b; n++) {
|
|
|
|
if (tkernel[n+range] != 0.f) {
|
|
|
|
if (tkernel[n+range] > FLT_MIN)
|
2023-07-30 19:57:50 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "out of range kernel %g\n", tkernel[n+range]);
|
2023-07-22 21:45:48 +02:00
|
|
|
start = n;
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-30 13:16:46 +02:00
|
|
|
for (int n = b; n >= a; n--) {
|
|
|
|
if (tkernel[n+range] != 0.f) {
|
|
|
|
if (tkernel[n+range] > FLT_MIN)
|
2023-07-30 19:57:50 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "out of range kernel %g\n", tkernel[n+range]);
|
2023-07-30 13:16:46 +02:00
|
|
|
stop = n;
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-07-22 21:45:48 +02:00
|
|
|
|
2023-07-30 19:57:50 +02:00
|
|
|
if (start == INT_MIN || stop == INT_MAX) {
|
|
|
|
ret = AVERROR(EINVAL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-30 13:16:46 +02:00
|
|
|
kernel_start[y] = start;
|
|
|
|
kernel_stop[y] = stop;
|
2023-07-22 21:45:48 +02:00
|
|
|
|
2023-07-30 19:57:50 +02:00
|
|
|
kernel = av_calloc(FFALIGN(stop-start+1, 16), sizeof(*kernel));
|
2023-07-22 21:45:48 +02:00
|
|
|
if (!kernel) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-30 13:16:46 +02:00
|
|
|
for (int n = 0; n <= stop - start; n++) {
|
|
|
|
kernel[n].re = tkernel[n+range+start];
|
|
|
|
kernel[n].im = tkernel[n+range+start];
|
2023-07-22 21:45:48 +02:00
|
|
|
}
|
|
|
|
|
2023-07-30 13:16:46 +02:00
|
|
|
range_min = FFMIN(range_min, stop+1-start);
|
|
|
|
range_max = FFMAX(range_max, stop+1-start);
|
|
|
|
|
2023-07-22 21:45:48 +02:00
|
|
|
s->kernel[y] = kernel;
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int n = 0; n < size; n++)
|
2023-07-30 15:11:53 +02:00
|
|
|
index[n] = n & (s->output_padding_size - 1);
|
2023-07-21 23:11:11 +02:00
|
|
|
|
2023-07-30 13:16:46 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "range_min: %d\n", range_min);
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "range_max: %d\n", range_max);
|
2023-07-21 23:11:11 +02:00
|
|
|
|
2023-07-22 21:45:48 +02:00
|
|
|
av_freep(&tkernel);
|
|
|
|
|
|
|
|
return ret;
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config_output(AVFilterLink *outlink)
|
|
|
|
{
|
2024-08-06 08:50:21 +02:00
|
|
|
FilterLink *l = ff_filter_link(outlink);
|
2022-11-19 20:01:23 +02:00
|
|
|
AVFilterContext *ctx = outlink->src;
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
ShowCWTContext *s = ctx->priv;
|
2023-11-11 01:44:36 +02:00
|
|
|
const float limit_frequency = inlink->sample_rate * 0.5f;
|
|
|
|
float maximum_frequency = fminf(s->maximum_frequency, limit_frequency);
|
2022-11-19 20:01:23 +02:00
|
|
|
float minimum_frequency = s->minimum_frequency;
|
|
|
|
float scale = 1.f, factor;
|
|
|
|
int ret;
|
|
|
|
|
2023-07-22 17:25:06 +02:00
|
|
|
if (minimum_frequency >= maximum_frequency) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "min frequency (%f) >= (%f) max frequency\n",
|
|
|
|
minimum_frequency, maximum_frequency);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2022-11-19 20:01:23 +02:00
|
|
|
uninit(ctx);
|
|
|
|
|
2023-07-10 12:08:13 +02:00
|
|
|
s->fdsp = avpriv_float_dsp_alloc(0);
|
|
|
|
if (!s->fdsp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2022-11-19 20:01:23 +02:00
|
|
|
switch (s->direction) {
|
|
|
|
case DIRECTION_LR:
|
|
|
|
case DIRECTION_RL:
|
2023-07-10 22:52:04 +02:00
|
|
|
s->bar_size = s->w * s->bar_ratio;
|
2023-11-11 23:24:34 +02:00
|
|
|
s->sono_size = s->w - s->bar_size;
|
2022-11-19 20:01:23 +02:00
|
|
|
s->frequency_band_count = s->h;
|
|
|
|
break;
|
|
|
|
case DIRECTION_UD:
|
|
|
|
case DIRECTION_DU:
|
2023-07-10 22:52:04 +02:00
|
|
|
s->bar_size = s->h * s->bar_ratio;
|
2023-11-11 23:24:34 +02:00
|
|
|
s->sono_size = s->h - s->bar_size;
|
2022-11-19 20:01:23 +02:00
|
|
|
s->frequency_band_count = s->w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-08-03 21:41:53 +02:00
|
|
|
switch (s->frequency_scale) {
|
|
|
|
case FSCALE_LOG:
|
|
|
|
minimum_frequency = logf(minimum_frequency) / logf(2.f);
|
|
|
|
maximum_frequency = logf(maximum_frequency) / logf(2.f);
|
|
|
|
break;
|
|
|
|
case FSCALE_BARK:
|
|
|
|
minimum_frequency = 6.f * asinhf(minimum_frequency / 600.f);
|
|
|
|
maximum_frequency = 6.f * asinhf(maximum_frequency / 600.f);
|
|
|
|
break;
|
|
|
|
case FSCALE_MEL:
|
|
|
|
minimum_frequency = 2595.f * log10f(1.f + minimum_frequency / 700.f);
|
|
|
|
maximum_frequency = 2595.f * log10f(1.f + maximum_frequency / 700.f);
|
|
|
|
break;
|
|
|
|
case FSCALE_ERBS:
|
|
|
|
minimum_frequency = 11.17268f * logf(1.f + (46.06538f * minimum_frequency) / (minimum_frequency + 14678.49f));
|
|
|
|
maximum_frequency = 11.17268f * logf(1.f + (46.06538f * maximum_frequency) / (maximum_frequency + 14678.49f));
|
|
|
|
break;
|
|
|
|
case FSCALE_SQRT:
|
|
|
|
minimum_frequency = sqrtf(minimum_frequency);
|
|
|
|
maximum_frequency = sqrtf(maximum_frequency);
|
|
|
|
break;
|
|
|
|
case FSCALE_CBRT:
|
|
|
|
minimum_frequency = cbrtf(minimum_frequency);
|
|
|
|
maximum_frequency = cbrtf(maximum_frequency);
|
|
|
|
break;
|
|
|
|
case FSCALE_QDRT:
|
|
|
|
minimum_frequency = powf(minimum_frequency, 0.25f);
|
|
|
|
maximum_frequency = powf(maximum_frequency, 0.25f);
|
|
|
|
break;
|
2023-11-11 01:44:36 +02:00
|
|
|
case FSCALE_FM:
|
|
|
|
minimum_frequency = powf(9.f * (minimum_frequency * minimum_frequency) / 4.f, 1.f / 3.f);
|
|
|
|
maximum_frequency = powf(9.f * (maximum_frequency * maximum_frequency) / 4.f, 1.f / 3.f);
|
|
|
|
break;
|
2023-08-03 21:41:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s->frequency_band = av_calloc(s->frequency_band_count,
|
|
|
|
sizeof(*s->frequency_band) * 2);
|
|
|
|
if (!s->frequency_band)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
s->nb_consumed_samples = inlink->sample_rate *
|
|
|
|
frequency_band(s->frequency_band,
|
|
|
|
s->frequency_band_count, maximum_frequency - minimum_frequency,
|
|
|
|
minimum_frequency, s->frequency_scale, s->deviation);
|
|
|
|
s->nb_consumed_samples = FFMIN(s->nb_consumed_samples, 65536);
|
|
|
|
|
2022-11-19 20:01:23 +02:00
|
|
|
s->nb_threads = FFMIN(s->frequency_band_count, ff_filter_get_nb_threads(ctx));
|
|
|
|
s->nb_channels = inlink->ch_layout.nb_channels;
|
|
|
|
s->old_pts = AV_NOPTS_VALUE;
|
2022-11-29 15:03:34 +02:00
|
|
|
s->eof_pts = AV_NOPTS_VALUE;
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2023-07-27 14:36:13 +02:00
|
|
|
s->input_sample_count = 1 << (32 - ff_clz(s->nb_consumed_samples));
|
2023-07-21 23:11:11 +02:00
|
|
|
s->input_padding_size = 1 << (32 - ff_clz(s->input_sample_count));
|
|
|
|
s->output_sample_count = FFMAX(1, av_rescale(s->input_sample_count, s->pps, inlink->sample_rate));
|
|
|
|
s->output_padding_size = 1 << (32 - ff_clz(s->output_sample_count));
|
|
|
|
|
2023-07-27 14:36:13 +02:00
|
|
|
s->hop_size = s->input_sample_count;
|
2023-07-21 23:11:11 +02:00
|
|
|
s->ihop_size = s->output_padding_size >> 1;
|
2022-11-19 20:01:23 +02:00
|
|
|
|
|
|
|
outlink->w = s->w;
|
|
|
|
outlink->h = s->h;
|
|
|
|
outlink->sample_aspect_ratio = (AVRational){1,1};
|
|
|
|
|
2023-08-01 11:12:27 +02:00
|
|
|
s->fft_size = FFALIGN(s->input_padding_size, av_cpu_max_align());
|
|
|
|
s->ifft_size = FFALIGN(s->output_padding_size, av_cpu_max_align());
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2022-11-29 15:39:18 +02:00
|
|
|
s->fft = av_calloc(s->nb_threads, sizeof(*s->fft));
|
|
|
|
if (!s->fft)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (int n = 0; n < s->nb_threads; n++) {
|
|
|
|
ret = av_tx_init(&s->fft[n], &s->tx_fn, AV_TX_FLOAT_FFT, 0, s->input_padding_size, &scale, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2022-11-19 20:01:23 +02:00
|
|
|
|
|
|
|
s->ifft = av_calloc(s->nb_threads, sizeof(*s->ifft));
|
|
|
|
if (!s->ifft)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
for (int n = 0; n < s->nb_threads; n++) {
|
|
|
|
ret = av_tx_init(&s->ifft[n], &s->itx_fn, AV_TX_FLOAT_FFT, 1, s->output_padding_size, &scale, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->outpicref = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
2023-08-01 11:12:27 +02:00
|
|
|
s->fft_in = ff_get_audio_buffer(inlink, s->fft_size * 2);
|
|
|
|
s->fft_out = ff_get_audio_buffer(inlink, s->fft_size * 2);
|
2023-07-10 12:08:13 +02:00
|
|
|
s->dst_x = av_frame_alloc();
|
|
|
|
s->src_x = av_frame_alloc();
|
2023-07-22 21:45:48 +02:00
|
|
|
s->kernel = av_calloc(s->frequency_band_count, sizeof(*s->kernel));
|
2023-07-27 14:36:13 +02:00
|
|
|
s->cache = ff_get_audio_buffer(inlink, s->hop_size);
|
|
|
|
s->over = ff_get_audio_buffer(inlink, s->frequency_band_count * 2 * s->ihop_size);
|
2023-07-10 22:52:04 +02:00
|
|
|
s->bh_out = ff_get_audio_buffer(inlink, s->frequency_band_count);
|
2022-11-19 20:01:23 +02:00
|
|
|
s->ifft_in = av_frame_alloc();
|
|
|
|
s->ifft_out = av_frame_alloc();
|
2023-07-30 15:11:53 +02:00
|
|
|
s->ch_out = av_frame_alloc();
|
2022-11-19 20:01:23 +02:00
|
|
|
s->index = av_calloc(s->input_padding_size, sizeof(*s->index));
|
|
|
|
s->kernel_start = av_calloc(s->frequency_band_count, sizeof(*s->kernel_start));
|
|
|
|
s->kernel_stop = av_calloc(s->frequency_band_count, sizeof(*s->kernel_stop));
|
2023-07-27 14:36:13 +02:00
|
|
|
if (!s->outpicref || !s->fft_in || !s->fft_out || !s->src_x || !s->dst_x || !s->over ||
|
2023-07-10 22:52:04 +02:00
|
|
|
!s->ifft_in || !s->ifft_out || !s->kernel_start || !s->kernel_stop || !s->ch_out ||
|
2023-08-03 21:41:53 +02:00
|
|
|
!s->cache || !s->index || !s->bh_out || !s->kernel)
|
2022-11-19 20:01:23 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2023-07-30 15:11:53 +02:00
|
|
|
s->ch_out->format = inlink->format;
|
|
|
|
s->ch_out->nb_samples = 2 * s->ihop_size * inlink->ch_layout.nb_channels;
|
|
|
|
s->ch_out->ch_layout.nb_channels = s->frequency_band_count;
|
|
|
|
ret = av_frame_get_buffer(s->ch_out, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-11-19 20:01:23 +02:00
|
|
|
s->ifft_in->format = inlink->format;
|
2023-08-01 11:12:27 +02:00
|
|
|
s->ifft_in->nb_samples = s->ifft_size * 2;
|
2023-07-27 13:03:39 +02:00
|
|
|
s->ifft_in->ch_layout.nb_channels = s->nb_threads;
|
2022-11-19 20:01:23 +02:00
|
|
|
ret = av_frame_get_buffer(s->ifft_in, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
s->ifft_out->format = inlink->format;
|
2023-08-01 11:12:27 +02:00
|
|
|
s->ifft_out->nb_samples = s->ifft_size * 2;
|
2023-07-27 13:03:39 +02:00
|
|
|
s->ifft_out->ch_layout.nb_channels = s->nb_threads;
|
2022-11-19 20:01:23 +02:00
|
|
|
ret = av_frame_get_buffer(s->ifft_out, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2023-07-10 12:08:13 +02:00
|
|
|
s->src_x->format = inlink->format;
|
2023-08-01 11:12:27 +02:00
|
|
|
s->src_x->nb_samples = s->fft_size * 2;
|
2023-07-10 12:08:13 +02:00
|
|
|
s->src_x->ch_layout.nb_channels = s->nb_threads;
|
|
|
|
ret = av_frame_get_buffer(s->src_x, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
s->dst_x->format = inlink->format;
|
2023-08-01 11:12:27 +02:00
|
|
|
s->dst_x->nb_samples = s->fft_size * 2;
|
2023-07-10 12:08:13 +02:00
|
|
|
s->dst_x->ch_layout.nb_channels = s->nb_threads;
|
|
|
|
ret = av_frame_get_buffer(s->dst_x, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2022-11-19 20:01:23 +02:00
|
|
|
s->outpicref->sample_aspect_ratio = (AVRational){1,1};
|
|
|
|
|
|
|
|
for (int y = 0; y < outlink->h; y++) {
|
|
|
|
memset(s->outpicref->data[0] + y * s->outpicref->linesize[0], 0, outlink->w);
|
|
|
|
memset(s->outpicref->data[1] + y * s->outpicref->linesize[1], 128, outlink->w);
|
|
|
|
memset(s->outpicref->data[2] + y * s->outpicref->linesize[2], 128, outlink->w);
|
|
|
|
if (s->outpicref->data[3])
|
|
|
|
memset(s->outpicref->data[3] + y * s->outpicref->linesize[3], 0, outlink->w);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->outpicref->color_range = AVCOL_RANGE_JPEG;
|
|
|
|
|
2023-08-01 11:12:27 +02:00
|
|
|
factor = s->input_padding_size / (float)inlink->sample_rate;
|
2023-08-03 21:41:53 +02:00
|
|
|
for (int n = 0; n < s->frequency_band_count; n++) {
|
|
|
|
s->frequency_band[2*n ] *= factor;
|
|
|
|
s->frequency_band[2*n+1] *= factor;
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-07-27 14:36:13 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "factor: %f\n", factor);
|
2023-08-03 21:41:53 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "nb_consumed_samples: %d\n", s->nb_consumed_samples);
|
2023-07-27 14:36:13 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "hop_size: %d\n", s->hop_size);
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "ihop_size: %d\n", s->ihop_size);
|
2022-11-19 20:01:23 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "input_sample_count: %d\n", s->input_sample_count);
|
2023-07-21 23:11:11 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "input_padding_size: %d\n", s->input_padding_size);
|
2022-11-19 20:01:23 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "output_sample_count: %d\n", s->output_sample_count);
|
2023-07-21 23:11:11 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "output_padding_size: %d\n", s->output_padding_size);
|
2022-11-19 20:01:23 +02:00
|
|
|
|
|
|
|
switch (s->direction) {
|
|
|
|
case DIRECTION_LR:
|
|
|
|
case DIRECTION_UD:
|
2023-07-10 22:52:04 +02:00
|
|
|
s->pos = s->bar_size;
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
2023-11-11 23:24:34 +02:00
|
|
|
case DIRECTION_RL:
|
2022-11-19 20:01:23 +02:00
|
|
|
case DIRECTION_DU:
|
2023-11-11 23:24:34 +02:00
|
|
|
s->pos = s->sono_size;
|
2022-11-19 20:01:23 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->auto_frame_rate = av_make_q(inlink->sample_rate, s->hop_size);
|
|
|
|
if (strcmp(s->rate_str, "auto")) {
|
|
|
|
ret = av_parse_video_rate(&s->frame_rate, s->rate_str);
|
2024-06-11 22:44:46 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2022-11-19 20:01:23 +02:00
|
|
|
} else {
|
|
|
|
s->frame_rate = s->auto_frame_rate;
|
|
|
|
}
|
2024-08-06 08:50:21 +02:00
|
|
|
l->frame_rate = s->frame_rate;
|
|
|
|
outlink->time_base = av_inv_q(l->frame_rate);
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2023-07-22 21:45:48 +02:00
|
|
|
ret = compute_kernel(ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2022-11-19 20:01:23 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:04:08 +02:00
|
|
|
static int output_frame(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
ShowCWTContext *s = ctx->priv;
|
2022-12-04 12:15:21 +02:00
|
|
|
const int nb_planes = 3 + (s->outpicref->data[3] != NULL);
|
2022-11-29 12:04:08 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
switch (s->slide) {
|
|
|
|
case SLIDE_SCROLL:
|
|
|
|
switch (s->direction) {
|
|
|
|
case DIRECTION_UD:
|
2022-12-04 12:15:21 +02:00
|
|
|
for (int p = 0; p < nb_planes; p++) {
|
2022-11-29 12:04:08 +02:00
|
|
|
ptrdiff_t linesize = s->outpicref->linesize[p];
|
|
|
|
|
2023-07-10 22:52:04 +02:00
|
|
|
for (int y = s->h - 1; y > s->bar_size; y--) {
|
2022-11-29 12:04:08 +02:00
|
|
|
uint8_t *dst = s->outpicref->data[p] + y * linesize;
|
|
|
|
|
|
|
|
memmove(dst, dst - linesize, s->w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIRECTION_DU:
|
2022-12-04 12:15:21 +02:00
|
|
|
for (int p = 0; p < nb_planes; p++) {
|
2022-11-29 12:04:08 +02:00
|
|
|
ptrdiff_t linesize = s->outpicref->linesize[p];
|
|
|
|
|
2023-11-11 23:24:34 +02:00
|
|
|
for (int y = 0; y < s->sono_size; y++) {
|
2022-11-29 12:04:08 +02:00
|
|
|
uint8_t *dst = s->outpicref->data[p] + y * linesize;
|
|
|
|
|
|
|
|
memmove(dst, dst + linesize, s->w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ff_filter_execute(ctx, draw, NULL, NULL, s->nb_threads);
|
|
|
|
|
|
|
|
switch (s->slide) {
|
|
|
|
case SLIDE_REPLACE:
|
2022-11-30 19:03:00 +02:00
|
|
|
case SLIDE_FRAME:
|
2022-11-29 12:04:08 +02:00
|
|
|
switch (s->direction) {
|
|
|
|
case DIRECTION_LR:
|
|
|
|
s->pos++;
|
2022-11-30 19:03:00 +02:00
|
|
|
if (s->pos >= s->w) {
|
2023-07-10 22:52:04 +02:00
|
|
|
s->pos = s->bar_size;
|
2022-11-30 19:03:00 +02:00
|
|
|
s->new_frame = 1;
|
|
|
|
}
|
2022-11-29 12:04:08 +02:00
|
|
|
break;
|
|
|
|
case DIRECTION_RL:
|
|
|
|
s->pos--;
|
2022-11-30 19:03:00 +02:00
|
|
|
if (s->pos < 0) {
|
2023-11-11 23:24:34 +02:00
|
|
|
s->pos = s->sono_size;
|
2022-11-30 19:03:00 +02:00
|
|
|
s->new_frame = 1;
|
|
|
|
}
|
2022-11-29 12:04:08 +02:00
|
|
|
break;
|
|
|
|
case DIRECTION_UD:
|
|
|
|
s->pos++;
|
2022-11-30 19:03:00 +02:00
|
|
|
if (s->pos >= s->h) {
|
2023-07-10 22:52:04 +02:00
|
|
|
s->pos = s->bar_size;
|
2022-11-30 19:03:00 +02:00
|
|
|
s->new_frame = 1;
|
|
|
|
}
|
2022-11-29 12:04:08 +02:00
|
|
|
break;
|
|
|
|
case DIRECTION_DU:
|
|
|
|
s->pos--;
|
2022-11-30 19:03:00 +02:00
|
|
|
if (s->pos < 0) {
|
2023-11-11 23:24:34 +02:00
|
|
|
s->pos = s->sono_size;
|
2022-11-30 19:03:00 +02:00
|
|
|
s->new_frame = 1;
|
|
|
|
}
|
2022-11-29 12:04:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SLIDE_SCROLL:
|
|
|
|
switch (s->direction) {
|
|
|
|
case DIRECTION_UD:
|
|
|
|
case DIRECTION_LR:
|
2023-07-10 22:52:04 +02:00
|
|
|
s->pos = s->bar_size;
|
2022-11-29 12:04:08 +02:00
|
|
|
break;
|
|
|
|
case DIRECTION_RL:
|
|
|
|
case DIRECTION_DU:
|
2023-11-11 23:24:34 +02:00
|
|
|
s->pos = s->sono_size;
|
2022-11-29 12:04:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-30 19:03:00 +02:00
|
|
|
if (s->slide == SLIDE_FRAME && s->eof) {
|
|
|
|
switch (s->direction) {
|
|
|
|
case DIRECTION_LR:
|
2022-12-04 12:15:21 +02:00
|
|
|
for (int p = 0; p < nb_planes; p++) {
|
2022-11-30 19:03:00 +02:00
|
|
|
ptrdiff_t linesize = s->outpicref->linesize[p];
|
|
|
|
const int size = s->w - s->pos;
|
2022-12-04 12:15:21 +02:00
|
|
|
const int fill = p > 0 && p < 3 ? 128 : 0;
|
2022-11-30 19:03:00 +02:00
|
|
|
const int x = s->pos;
|
|
|
|
|
|
|
|
for (int y = 0; y < s->h; y++) {
|
|
|
|
uint8_t *dst = s->outpicref->data[p] + y * linesize + x;
|
|
|
|
|
|
|
|
memset(dst, fill, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIRECTION_RL:
|
2022-12-04 12:15:21 +02:00
|
|
|
for (int p = 0; p < nb_planes; p++) {
|
2022-11-30 19:03:00 +02:00
|
|
|
ptrdiff_t linesize = s->outpicref->linesize[p];
|
|
|
|
const int size = s->w - s->pos;
|
2022-12-04 12:15:21 +02:00
|
|
|
const int fill = p > 0 && p < 3 ? 128 : 0;
|
2022-11-30 19:03:00 +02:00
|
|
|
|
|
|
|
for (int y = 0; y < s->h; y++) {
|
|
|
|
uint8_t *dst = s->outpicref->data[p] + y * linesize;
|
|
|
|
|
|
|
|
memset(dst, fill, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIRECTION_UD:
|
2022-12-04 12:15:21 +02:00
|
|
|
for (int p = 0; p < nb_planes; p++) {
|
2022-11-30 19:03:00 +02:00
|
|
|
ptrdiff_t linesize = s->outpicref->linesize[p];
|
2022-12-04 12:15:21 +02:00
|
|
|
const int fill = p > 0 && p < 3 ? 128 : 0;
|
2022-11-30 19:03:00 +02:00
|
|
|
|
|
|
|
for (int y = s->pos; y < s->h; y++) {
|
|
|
|
uint8_t *dst = s->outpicref->data[p] + y * linesize;
|
|
|
|
|
|
|
|
memset(dst, fill, s->w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIRECTION_DU:
|
2022-12-04 12:15:21 +02:00
|
|
|
for (int p = 0; p < nb_planes; p++) {
|
2022-11-30 19:03:00 +02:00
|
|
|
ptrdiff_t linesize = s->outpicref->linesize[p];
|
2022-12-04 12:15:21 +02:00
|
|
|
const int fill = p > 0 && p < 3 ? 128 : 0;
|
2022-11-30 19:03:00 +02:00
|
|
|
|
|
|
|
for (int y = s->h - s->pos; y >= 0; y--) {
|
|
|
|
uint8_t *dst = s->outpicref->data[p] + y * linesize;
|
|
|
|
|
|
|
|
memset(dst, fill, s->w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s->new_frame = s->slide == SLIDE_FRAME && (s->new_frame || s->eof);
|
|
|
|
|
|
|
|
if (s->slide != SLIDE_FRAME || s->new_frame == 1) {
|
|
|
|
int64_t pts_offset = s->new_frame ? 0LL : av_rescale(s->ihop_index, s->hop_size, s->ihop_size);
|
2023-07-27 14:36:13 +02:00
|
|
|
const int offset = (s->input_padding_size - s->hop_size) >> 1;
|
2022-11-30 19:03:00 +02:00
|
|
|
|
2023-07-27 14:36:13 +02:00
|
|
|
pts_offset = av_rescale_q(pts_offset - offset, av_make_q(1, inlink->sample_rate), inlink->time_base);
|
2022-11-30 19:03:00 +02:00
|
|
|
s->outpicref->pts = av_rescale_q(s->in_pts + pts_offset, inlink->time_base, outlink->time_base);
|
2023-01-17 15:34:47 +02:00
|
|
|
s->outpicref->duration = 1;
|
2022-11-30 19:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s->ihop_index++;
|
|
|
|
if (s->ihop_index >= s->ihop_size)
|
2023-07-27 14:36:13 +02:00
|
|
|
s->ihop_index = s->hop_index = 0;
|
2022-11-30 19:03:00 +02:00
|
|
|
|
|
|
|
if (s->slide == SLIDE_FRAME && s->new_frame == 0)
|
|
|
|
return 1;
|
|
|
|
|
2022-11-29 12:04:08 +02:00
|
|
|
if (s->old_pts < s->outpicref->pts) {
|
|
|
|
AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
|
|
|
if (!out)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
ret = av_frame_copy_props(out, s->outpicref);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
ret = av_frame_copy(out, s->outpicref);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
s->old_pts = s->outpicref->pts;
|
2022-11-30 19:03:00 +02:00
|
|
|
s->new_frame = 0;
|
2022-11-29 12:04:08 +02:00
|
|
|
ret = ff_filter_frame(outlink, out);
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
|
|
|
fail:
|
|
|
|
av_frame_free(&out);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-29 15:39:18 +02:00
|
|
|
static int run_channels_cwt_prepare(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
|
|
|
{
|
|
|
|
ShowCWTContext *s = ctx->priv;
|
|
|
|
const int count = s->nb_channels;
|
|
|
|
const int start = (count * jobnr) / nb_jobs;
|
|
|
|
const int end = (count * (jobnr+1)) / nb_jobs;
|
|
|
|
|
|
|
|
for (int ch = start; ch < end; ch++)
|
|
|
|
run_channel_cwt_prepare(ctx, arg, jobnr, ch);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-19 20:01:23 +02:00
|
|
|
static int activate(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
ShowCWTContext *s = ctx->priv;
|
|
|
|
int ret = 0, status;
|
|
|
|
int64_t pts;
|
|
|
|
|
|
|
|
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
|
|
|
|
|
|
|
if (s->outpicref) {
|
2022-11-29 15:03:34 +02:00
|
|
|
AVFrame *fin = NULL;
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2023-07-27 14:36:13 +02:00
|
|
|
if (s->hop_index < s->hop_size) {
|
2022-11-29 15:03:34 +02:00
|
|
|
if (!s->eof) {
|
|
|
|
ret = ff_inlink_consume_samples(inlink, 1, s->hop_size - s->hop_index, &fin);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2022-11-19 20:01:23 +02:00
|
|
|
|
2022-11-29 15:03:34 +02:00
|
|
|
if (ret > 0 || s->eof) {
|
2022-11-29 15:39:18 +02:00
|
|
|
ff_filter_execute(ctx, run_channels_cwt_prepare, fin, NULL,
|
|
|
|
FFMIN(s->nb_threads, s->nb_channels));
|
2022-11-29 15:03:34 +02:00
|
|
|
if (fin) {
|
2023-11-11 18:23:55 +02:00
|
|
|
if (s->hop_index == 0) {
|
2022-11-29 15:03:34 +02:00
|
|
|
s->in_pts = fin->pts;
|
2023-11-11 18:23:55 +02:00
|
|
|
if (s->old_pts == AV_NOPTS_VALUE)
|
|
|
|
s->old_pts = av_rescale_q(s->in_pts, inlink->time_base, outlink->time_base) - 1;
|
|
|
|
}
|
2022-11-29 15:03:34 +02:00
|
|
|
s->hop_index += fin->nb_samples;
|
|
|
|
av_frame_free(&fin);
|
|
|
|
} else {
|
|
|
|
s->hop_index = s->hop_size;
|
|
|
|
}
|
2022-11-19 20:01:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 01:27:14 +02:00
|
|
|
if (s->hop_index >= s->hop_size || s->ihop_index > 0) {
|
2022-11-19 20:01:23 +02:00
|
|
|
for (int ch = 0; ch < s->nb_channels && s->ihop_index == 0; ch++) {
|
|
|
|
ff_filter_execute(ctx, run_channel_cwt, (void *)&ch, NULL,
|
|
|
|
s->nb_threads);
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:04:08 +02:00
|
|
|
ret = output_frame(ctx);
|
|
|
|
if (ret != 1)
|
2022-11-19 20:01:23 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 15:33:29 +02:00
|
|
|
if (s->eof) {
|
2022-11-30 19:03:00 +02:00
|
|
|
if (s->slide == SLIDE_FRAME)
|
|
|
|
ret = output_frame(ctx);
|
2022-11-29 15:03:34 +02:00
|
|
|
ff_outlink_set_status(outlink, AVERROR_EOF, s->eof_pts);
|
2022-11-30 19:03:00 +02:00
|
|
|
return ret;
|
2022-11-29 15:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
|
2022-11-19 20:01:23 +02:00
|
|
|
if (status == AVERROR_EOF) {
|
2022-11-29 15:03:34 +02:00
|
|
|
s->eof = 1;
|
|
|
|
ff_filter_set_ready(ctx, 10);
|
|
|
|
s->eof_pts = av_rescale_q(pts, inlink->time_base, outlink->time_base);
|
2022-11-19 20:01:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:04:08 +02:00
|
|
|
if (ff_inlink_queued_samples(inlink) > 0 || s->ihop_index ||
|
2022-11-29 15:03:34 +02:00
|
|
|
s->hop_index >= s->hop_size || s->eof) {
|
2022-11-19 20:01:23 +02:00
|
|
|
ff_filter_set_ready(ctx, 10);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ff_outlink_frame_wanted(outlink)) {
|
|
|
|
ff_inlink_request_frame(inlink);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FFERROR_NOT_READY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVFilterPad showcwt_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.config_props = config_output,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const AVFilter ff_avf_showcwt = {
|
|
|
|
.name = "showcwt",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Convert input audio to a CWT (Continuous Wavelet Transform) spectrum video output."),
|
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(ShowCWTContext),
|
2023-08-03 00:59:02 +02:00
|
|
|
FILTER_INPUTS(ff_audio_default_filterpad),
|
2022-11-19 20:01:23 +02:00
|
|
|
FILTER_OUTPUTS(showcwt_outputs),
|
2024-09-25 10:33:38 +02:00
|
|
|
FILTER_QUERY_FUNC2(query_formats),
|
2022-11-19 20:01:23 +02:00
|
|
|
.activate = activate,
|
|
|
|
.priv_class = &showcwt_class,
|
|
|
|
.flags = AVFILTER_FLAG_SLICE_THREADS,
|
|
|
|
};
|