avfilter/af_aspectralstats: stop using fifo and rewritting pts
This commit is contained in:
parent
644b6ed3ff
commit
6562d28b9b
@ -21,7 +21,6 @@
|
|||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
#include "libavutil/audio_fifo.h"
|
|
||||||
#include "libavutil/opt.h"
|
#include "libavutil/opt.h"
|
||||||
#include "libavutil/tx.h"
|
#include "libavutil/tx.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
@ -54,16 +53,14 @@ typedef struct AudioSpectralStatsContext {
|
|||||||
int nb_channels;
|
int nb_channels;
|
||||||
int hop_size;
|
int hop_size;
|
||||||
ChannelSpectralStats *stats;
|
ChannelSpectralStats *stats;
|
||||||
AVAudioFifo *fifo;
|
|
||||||
float *window_func_lut;
|
float *window_func_lut;
|
||||||
int64_t pts;
|
|
||||||
int eof;
|
|
||||||
av_tx_fn tx_fn;
|
av_tx_fn tx_fn;
|
||||||
AVTXContext **fft;
|
AVTXContext **fft;
|
||||||
AVComplexFloat **fft_in;
|
AVComplexFloat **fft_in;
|
||||||
AVComplexFloat **fft_out;
|
AVComplexFloat **fft_out;
|
||||||
float **prev_magnitude;
|
float **prev_magnitude;
|
||||||
float **magnitude;
|
float **magnitude;
|
||||||
|
AVFrame *window;
|
||||||
} AudioSpectralStatsContext;
|
} AudioSpectralStatsContext;
|
||||||
|
|
||||||
#define OFFSET(x) offsetof(AudioSpectralStatsContext, x)
|
#define OFFSET(x) offsetof(AudioSpectralStatsContext, x)
|
||||||
@ -85,10 +82,6 @@ static int config_output(AVFilterLink *outlink)
|
|||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
s->nb_channels = outlink->channels;
|
s->nb_channels = outlink->channels;
|
||||||
s->fifo = av_audio_fifo_alloc(outlink->format, s->nb_channels, s->win_size);
|
|
||||||
if (!s->fifo)
|
|
||||||
return AVERROR(ENOMEM);
|
|
||||||
|
|
||||||
s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size,
|
s->window_func_lut = av_realloc_f(s->window_func_lut, s->win_size,
|
||||||
sizeof(*s->window_func_lut));
|
sizeof(*s->window_func_lut));
|
||||||
if (!s->window_func_lut)
|
if (!s->window_func_lut)
|
||||||
@ -147,6 +140,10 @@ static int config_output(AVFilterLink *outlink)
|
|||||||
return AVERROR(ENOMEM);
|
return AVERROR(ENOMEM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s->window = ff_get_audio_buffer(outlink, s->win_size);
|
||||||
|
if (!s->window)
|
||||||
|
return AVERROR(ENOMEM);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -392,14 +389,15 @@ static float spectral_rolloff(const float *const spectral, int size, int max_fre
|
|||||||
static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
|
||||||
{
|
{
|
||||||
AudioSpectralStatsContext *s = ctx->priv;
|
AudioSpectralStatsContext *s = ctx->priv;
|
||||||
|
const float *window_func_lut = s->window_func_lut;
|
||||||
AVFrame *in = arg;
|
AVFrame *in = arg;
|
||||||
const int channels = s->nb_channels;
|
const int channels = s->nb_channels;
|
||||||
const int samples = in->nb_samples;
|
|
||||||
const int start = (channels * jobnr) / nb_jobs;
|
const int start = (channels * jobnr) / nb_jobs;
|
||||||
const int end = (channels * (jobnr+1)) / nb_jobs;
|
const int end = (channels * (jobnr+1)) / nb_jobs;
|
||||||
|
const int offset = s->win_size - s->hop_size;
|
||||||
|
|
||||||
for (int ch = start; ch < end; ch++) {
|
for (int ch = start; ch < end; ch++) {
|
||||||
const float *const src = (const float *const)in->extended_data[ch];
|
float *window = (float *)s->window->extended_data[ch];
|
||||||
ChannelSpectralStats *stats = &s->stats[ch];
|
ChannelSpectralStats *stats = &s->stats[ch];
|
||||||
AVComplexFloat *fft_out = s->fft_out[ch];
|
AVComplexFloat *fft_out = s->fft_out[ch];
|
||||||
AVComplexFloat *fft_in = s->fft_in[ch];
|
AVComplexFloat *fft_in = s->fft_in[ch];
|
||||||
@ -407,13 +405,12 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_job
|
|||||||
float *prev_magnitude = s->prev_magnitude[ch];
|
float *prev_magnitude = s->prev_magnitude[ch];
|
||||||
const float scale = 1.f / s->win_size;
|
const float scale = 1.f / s->win_size;
|
||||||
|
|
||||||
for (int n = 0; n < samples; n++) {
|
memmove(window, &window[s->hop_size], offset * sizeof(float));
|
||||||
fft_in[n].re = src[n] * s->window_func_lut[n];
|
memcpy(&window[offset], in->extended_data[ch], in->nb_samples * sizeof(float));
|
||||||
fft_in[n].im = 0;
|
memset(&window[offset + in->nb_samples], 0, (s->hop_size - in->nb_samples) * sizeof(float));
|
||||||
}
|
|
||||||
|
|
||||||
for (int n = in->nb_samples; n < s->win_size; n++) {
|
for (int n = 0; n < s->win_size; n++) {
|
||||||
fft_in[n].re = 0;
|
fft_in[n].re = window[n] * window_func_lut[n];
|
||||||
fft_in[n].im = 0;
|
fft_in[n].im = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -447,30 +444,30 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int jobnr, int nb_job
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int filter_frame(AVFilterLink *inlink)
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
||||||
{
|
{
|
||||||
AVFilterContext *ctx = inlink->dst;
|
AVFilterContext *ctx = inlink->dst;
|
||||||
AVFilterLink *outlink = ctx->outputs[0];
|
AVFilterLink *outlink = ctx->outputs[0];
|
||||||
AudioSpectralStatsContext *s = ctx->priv;
|
AudioSpectralStatsContext *s = ctx->priv;
|
||||||
AVDictionary **metadata;
|
AVDictionary **metadata;
|
||||||
AVFrame *out, *in = NULL;
|
AVFrame *out;
|
||||||
int ret = 0;
|
int ret;
|
||||||
|
|
||||||
out = ff_get_audio_buffer(outlink, s->hop_size);
|
if (av_frame_is_writable(in)) {
|
||||||
|
out = in;
|
||||||
|
} else {
|
||||||
|
out = ff_get_audio_buffer(outlink, in->nb_samples);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
ret = AVERROR(ENOMEM);
|
av_frame_free(&in);
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!in) {
|
|
||||||
in = ff_get_audio_buffer(outlink, s->win_size);
|
|
||||||
if (!in)
|
|
||||||
return AVERROR(ENOMEM);
|
return AVERROR(ENOMEM);
|
||||||
}
|
}
|
||||||
|
ret = av_frame_copy_props(out, in);
|
||||||
ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, s->win_size);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
ret = av_frame_copy(out, in);
|
||||||
|
if (ret < 0)
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
metadata = &out->metadata;
|
metadata = &out->metadata;
|
||||||
ff_filter_execute(ctx, filter_channel, in, NULL,
|
ff_filter_execute(ctx, filter_channel, in, NULL,
|
||||||
@ -478,70 +475,34 @@ static int filter_frame(AVFilterLink *inlink)
|
|||||||
|
|
||||||
set_metadata(s, metadata);
|
set_metadata(s, metadata);
|
||||||
|
|
||||||
out->pts = s->pts;
|
if (out != in)
|
||||||
s->pts += av_rescale_q(s->hop_size, (AVRational){1, outlink->sample_rate}, outlink->time_base);
|
|
||||||
|
|
||||||
av_audio_fifo_read(s->fifo, (void **)out->extended_data, s->hop_size);
|
|
||||||
|
|
||||||
av_frame_free(&in);
|
av_frame_free(&in);
|
||||||
return ff_filter_frame(outlink, out);
|
return ff_filter_frame(outlink, out);
|
||||||
fail:
|
fail:
|
||||||
av_frame_free(&in);
|
av_frame_free(&in);
|
||||||
return ret < 0 ? ret : 0;
|
av_frame_free(&out);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int activate(AVFilterContext *ctx)
|
static int activate(AVFilterContext *ctx)
|
||||||
{
|
{
|
||||||
AVFilterLink *inlink = ctx->inputs[0];
|
|
||||||
AVFilterLink *outlink = ctx->outputs[0];
|
|
||||||
AudioSpectralStatsContext *s = ctx->priv;
|
AudioSpectralStatsContext *s = ctx->priv;
|
||||||
AVFrame *in = NULL;
|
AVFilterLink *outlink = ctx->outputs[0];
|
||||||
int ret = 0, status;
|
AVFilterLink *inlink = ctx->inputs[0];
|
||||||
int64_t pts;
|
AVFrame *in;
|
||||||
|
int ret;
|
||||||
|
|
||||||
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
||||||
|
|
||||||
if (!s->eof && av_audio_fifo_size(s->fifo) < s->win_size) {
|
ret = ff_inlink_consume_samples(inlink, s->hop_size, s->hop_size, &in);
|
||||||
ret = ff_inlink_consume_frame(inlink, &in);
|
if (ret < 0)
|
||||||
|
return ret;
|
||||||
|
if (ret > 0)
|
||||||
|
ret = filter_frame(inlink, in);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
if (ret > 0) {
|
FF_FILTER_FORWARD_STATUS(inlink, outlink);
|
||||||
ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
|
|
||||||
in->nb_samples);
|
|
||||||
if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
|
|
||||||
s->pts = in->pts;
|
|
||||||
|
|
||||||
av_frame_free(&in);
|
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((av_audio_fifo_size(s->fifo) >= s->win_size) ||
|
|
||||||
(av_audio_fifo_size(s->fifo) > 0 && s->eof)) {
|
|
||||||
ret = filter_frame(inlink);
|
|
||||||
if (av_audio_fifo_size(s->fifo) >= s->win_size)
|
|
||||||
ff_filter_set_ready(ctx, 100);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) {
|
|
||||||
if (status == AVERROR_EOF) {
|
|
||||||
s->eof = 1;
|
|
||||||
if (av_audio_fifo_size(s->fifo) >= 0) {
|
|
||||||
ff_filter_set_ready(ctx, 100);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s->eof && av_audio_fifo_size(s->fifo) <= 0) {
|
|
||||||
ff_outlink_set_status(outlink, AVERROR_EOF, s->pts);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!s->eof)
|
|
||||||
FF_FILTER_FORWARD_WANTED(outlink, inlink);
|
FF_FILTER_FORWARD_WANTED(outlink, inlink);
|
||||||
|
|
||||||
return FFERROR_NOT_READY;
|
return FFERROR_NOT_READY;
|
||||||
@ -572,8 +533,7 @@ static av_cold void uninit(AVFilterContext *ctx)
|
|||||||
av_freep(&s->stats);
|
av_freep(&s->stats);
|
||||||
|
|
||||||
av_freep(&s->window_func_lut);
|
av_freep(&s->window_func_lut);
|
||||||
av_audio_fifo_free(s->fifo);
|
av_frame_free(&s->window);
|
||||||
s->fifo = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const AVFilterPad aspectralstats_inputs[] = {
|
static const AVFilterPad aspectralstats_inputs[] = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user