You've already forked FFmpeg
mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-08-10 06:10:52 +02:00
avfilter/af_afftfilt: stop resetting pts
Also stop using audio fifo.
This commit is contained in:
@@ -18,7 +18,6 @@
|
|||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "libavutil/audio_fifo.h"
|
|
||||||
#include "libavutil/avstring.h"
|
#include "libavutil/avstring.h"
|
||||||
#include "libavfilter/internal.h"
|
#include "libavfilter/internal.h"
|
||||||
#include "libavutil/common.h"
|
#include "libavutil/common.h"
|
||||||
@@ -46,12 +45,10 @@ typedef struct AFFTFiltContext {
|
|||||||
int window_size;
|
int window_size;
|
||||||
AVExpr **real;
|
AVExpr **real;
|
||||||
AVExpr **imag;
|
AVExpr **imag;
|
||||||
AVAudioFifo *fifo;
|
|
||||||
int64_t pts;
|
|
||||||
int hop_size;
|
int hop_size;
|
||||||
float overlap;
|
float overlap;
|
||||||
|
AVFrame *window;
|
||||||
AVFrame *buffer;
|
AVFrame *buffer;
|
||||||
int eof;
|
|
||||||
int win_func;
|
int win_func;
|
||||||
float *window_func_lut;
|
float *window_func_lut;
|
||||||
} AFFTFiltContext;
|
} AFFTFiltContext;
|
||||||
@@ -113,7 +110,6 @@ static int config_input(AVFilterLink *inlink)
|
|||||||
int buf_size;
|
int buf_size;
|
||||||
|
|
||||||
s->channels = inlink->channels;
|
s->channels = inlink->channels;
|
||||||
s->pts = AV_NOPTS_VALUE;
|
|
||||||
ret = av_tx_init(&s->fft, &s->tx_fn, AV_TX_FLOAT_FFT, 0, s->fft_size, &scale, 0);
|
ret = av_tx_init(&s->fft, &s->tx_fn, AV_TX_FLOAT_FFT, 0, s->fft_size, &scale, 0);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
@@ -196,15 +192,13 @@ static int config_input(AVFilterLink *inlink)
|
|||||||
|
|
||||||
av_freep(&args);
|
av_freep(&args);
|
||||||
|
|
||||||
s->fifo = av_audio_fifo_alloc(inlink->format, inlink->channels, s->window_size);
|
|
||||||
if (!s->fifo)
|
|
||||||
return AVERROR(ENOMEM);
|
|
||||||
|
|
||||||
s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_size,
|
s->window_func_lut = av_realloc_f(s->window_func_lut, s->window_size,
|
||||||
sizeof(*s->window_func_lut));
|
sizeof(*s->window_func_lut));
|
||||||
if (!s->window_func_lut)
|
if (!s->window_func_lut)
|
||||||
return AVERROR(ENOMEM);
|
return AVERROR(ENOMEM);
|
||||||
generate_window_func(s->window_func_lut, s->window_size, s->win_func, &overlap);
|
generate_window_func(s->window_func_lut, s->window_size, s->win_func, &overlap);
|
||||||
|
for (int i = 0; i < s->window_size; i++)
|
||||||
|
s->window_func_lut[i] = sqrtf(s->window_func_lut[i] / s->window_size);
|
||||||
if (s->overlap == 1)
|
if (s->overlap == 1)
|
||||||
s->overlap = overlap;
|
s->overlap = overlap;
|
||||||
|
|
||||||
@@ -212,6 +206,10 @@ static int config_input(AVFilterLink *inlink)
|
|||||||
if (s->hop_size <= 0)
|
if (s->hop_size <= 0)
|
||||||
return AVERROR(EINVAL);
|
return AVERROR(EINVAL);
|
||||||
|
|
||||||
|
s->window = ff_get_audio_buffer(inlink, s->window_size * 2);
|
||||||
|
if (!s->window)
|
||||||
|
return AVERROR(ENOMEM);
|
||||||
|
|
||||||
s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
|
s->buffer = ff_get_audio_buffer(inlink, s->window_size * 2);
|
||||||
if (!s->buffer)
|
if (!s->buffer)
|
||||||
return AVERROR(ENOMEM);
|
return AVERROR(ENOMEM);
|
||||||
@@ -222,43 +220,34 @@ fail:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
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];
|
||||||
AFFTFiltContext *s = ctx->priv;
|
AFFTFiltContext *s = ctx->priv;
|
||||||
const int window_size = s->window_size;
|
const int window_size = s->window_size;
|
||||||
const float f = 1. / (s->window_size / 2);
|
const float *window_lut = s->window_func_lut;
|
||||||
|
const float f = sqrtf(1.f - s->overlap);
|
||||||
double values[VAR_VARS_NB];
|
double values[VAR_VARS_NB];
|
||||||
AVFrame *out, *in = NULL;
|
|
||||||
int ch, n, ret, i;
|
int ch, n, ret, i;
|
||||||
|
AVFrame *out;
|
||||||
if (!in) {
|
|
||||||
in = ff_get_audio_buffer(outlink, window_size);
|
|
||||||
if (!in)
|
|
||||||
return AVERROR(ENOMEM);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = av_audio_fifo_peek(s->fifo, (void **)in->extended_data, window_size);
|
|
||||||
if (ret < 0)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
for (ch = 0; ch < inlink->channels; ch++) {
|
for (ch = 0; ch < inlink->channels; ch++) {
|
||||||
const float *src = (float *)in->extended_data[ch];
|
const int offset = s->window_size - s->hop_size;
|
||||||
|
float *src = (float *)s->window->extended_data[ch];
|
||||||
AVComplexFloat *fft_in = s->fft_in[ch];
|
AVComplexFloat *fft_in = s->fft_in[ch];
|
||||||
|
|
||||||
for (n = 0; n < in->nb_samples; n++) {
|
memmove(src, &src[s->hop_size], offset * sizeof(float));
|
||||||
fft_in[n].re = src[n] * s->window_func_lut[n];
|
memcpy(&src[offset], in->extended_data[ch], in->nb_samples * sizeof(float));
|
||||||
fft_in[n].im = 0;
|
memset(&src[offset + in->nb_samples], 0, (s->hop_size - in->nb_samples) * sizeof(float));
|
||||||
}
|
|
||||||
|
|
||||||
for (; n < window_size; n++) {
|
for (n = 0; n < window_size; n++) {
|
||||||
fft_in[n].re = 0;
|
fft_in[n].re = src[n] * window_lut[n];
|
||||||
fft_in[n].im = 0;
|
fft_in[n].im = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
values[VAR_PTS] = s->pts;
|
values[VAR_PTS] = in->pts;
|
||||||
values[VAR_SAMPLE_RATE] = inlink->sample_rate;
|
values[VAR_SAMPLE_RATE] = inlink->sample_rate;
|
||||||
values[VAR_NBBINS] = window_size / 2;
|
values[VAR_NBBINS] = window_size / 2;
|
||||||
values[VAR_CHANNELS] = inlink->channels;
|
values[VAR_CHANNELS] = inlink->channels;
|
||||||
@@ -278,7 +267,7 @@ static int filter_frame(AVFilterLink *inlink)
|
|||||||
values[VAR_CHANNEL] = ch;
|
values[VAR_CHANNEL] = ch;
|
||||||
|
|
||||||
if (ctx->is_disabled) {
|
if (ctx->is_disabled) {
|
||||||
for (n = 0; n <= window_size / 2; n++) {
|
for (n = 0; n < window_size; n++) {
|
||||||
fft_temp[n].re = fft_out[n].re;
|
fft_temp[n].re = fft_out[n].re;
|
||||||
fft_temp[n].im = fft_out[n].im;
|
fft_temp[n].im = fft_out[n].im;
|
||||||
}
|
}
|
||||||
@@ -296,17 +285,18 @@ static int filter_frame(AVFilterLink *inlink)
|
|||||||
fft_temp[n].re = fr;
|
fft_temp[n].re = fr;
|
||||||
fft_temp[n].im = fi;
|
fft_temp[n].im = fi;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
|
for (n = window_size / 2 + 1, x = window_size / 2 - 1; n < window_size; n++, x--) {
|
||||||
fft_temp[n].re = fft_temp[x].re;
|
fft_temp[n].re = fft_temp[x].re;
|
||||||
fft_temp[n].im = -fft_temp[x].im;
|
fft_temp[n].im = -fft_temp[x].im;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
s->itx_fn(s->ifft, fft_out, fft_temp, sizeof(float));
|
s->itx_fn(s->ifft, fft_out, fft_temp, sizeof(float));
|
||||||
|
|
||||||
|
memmove(buf, buf + s->hop_size, window_size * sizeof(float));
|
||||||
for (i = 0; i < window_size; i++) {
|
for (i = 0; i < window_size; i++) {
|
||||||
buf[i] += s->fft_out[ch][i].re * f;
|
buf[i] += fft_out[i].re * window_lut[i] * f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,24 +306,20 @@ static int filter_frame(AVFilterLink *inlink)
|
|||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
out->pts = s->pts;
|
out->pts = in->pts;
|
||||||
s->pts += av_rescale_q(s->hop_size, (AVRational){1, outlink->sample_rate}, outlink->time_base);
|
out->nb_samples = in->nb_samples;
|
||||||
|
|
||||||
for (ch = 0; ch < inlink->channels; ch++) {
|
for (ch = 0; ch < inlink->channels; ch++) {
|
||||||
float *dst = (float *)out->extended_data[ch];
|
float *dst = (float *)out->extended_data[ch];
|
||||||
float *buf = (float *)s->buffer->extended_data[ch];
|
float *buf = (float *)s->buffer->extended_data[ch];
|
||||||
|
|
||||||
for (n = 0; n < s->hop_size; n++)
|
memcpy(dst, buf, s->hop_size * sizeof(float));
|
||||||
dst[n] = buf[n] * (1.f - s->overlap);
|
|
||||||
memmove(buf, buf + s->hop_size, window_size * 4);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = ff_filter_frame(outlink, out);
|
ret = ff_filter_frame(outlink, out);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
av_audio_fifo_drain(s->fifo, s->hop_size);
|
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
av_frame_free(&in);
|
av_frame_free(&in);
|
||||||
return ret < 0 ? ret : 0;
|
return ret < 0 ? ret : 0;
|
||||||
@@ -350,47 +336,20 @@ static int activate(AVFilterContext *ctx)
|
|||||||
|
|
||||||
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
||||||
|
|
||||||
if (!s->eof && av_audio_fifo_size(s->fifo) < s->window_size) {
|
ret = ff_inlink_consume_samples(inlink, s->hop_size, s->hop_size, &in);
|
||||||
ret = ff_inlink_consume_frame(inlink, &in);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
if (ret > 0) {
|
if (ret > 0)
|
||||||
ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
|
ret = filter_frame(inlink, in);
|
||||||
in->nb_samples);
|
|
||||||
if (ret >= 0 && s->pts == AV_NOPTS_VALUE)
|
|
||||||
s->pts = in->pts;
|
|
||||||
|
|
||||||
av_frame_free(&in);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((av_audio_fifo_size(s->fifo) >= s->window_size) ||
|
if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
|
||||||
(av_audio_fifo_size(s->fifo) > 0 && s->eof)) {
|
ff_outlink_set_status(outlink, status, pts);
|
||||||
ret = filter_frame(inlink);
|
|
||||||
if (av_audio_fifo_size(s->fifo) >= s->window_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;
|
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;
|
||||||
@@ -424,9 +383,8 @@ static av_cold void uninit(AVFilterContext *ctx)
|
|||||||
av_freep(&s->real);
|
av_freep(&s->real);
|
||||||
av_freep(&s->imag);
|
av_freep(&s->imag);
|
||||||
av_frame_free(&s->buffer);
|
av_frame_free(&s->buffer);
|
||||||
|
av_frame_free(&s->window);
|
||||||
av_freep(&s->window_func_lut);
|
av_freep(&s->window_func_lut);
|
||||||
|
|
||||||
av_audio_fifo_free(s->fifo);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const AVFilterPad inputs[] = {
|
static const AVFilterPad inputs[] = {
|
||||||
|
Reference in New Issue
Block a user