1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-12-23 12:43:46 +02:00

avfilter/af_anlmdn: stop using fifo and rewriting pts

This commit is contained in:
Paul B Mahol 2022-03-09 21:39:46 +01:00
parent 41cae501b7
commit c71b76e141

View File

@ -21,12 +21,12 @@
#include <float.h>
#include "libavutil/avassert.h"
#include "libavutil/audio_fifo.h"
#include "libavutil/avstring.h"
#include "libavutil/opt.h"
#include "avfilter.h"
#include "audio.h"
#include "formats.h"
#include "filters.h"
#include "af_anlmdndsp.h"
@ -50,14 +50,8 @@ typedef struct AudioNLMeansContext {
int N;
int H;
int offset;
AVFrame *in;
AVFrame *cache;
int64_t pts;
AVAudioFifo *fifo;
int eof_left;
AVFrame *window;
AudioNLMDNDSPContext dsp;
} AudioNLMeansContext;
@ -132,7 +126,6 @@ static int config_filter(AVFilterContext *ctx)
AudioNLMeansContext *s = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
int newK, newS, newH, newN;
AVFrame *new_in, *new_cache;
newK = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
newS = av_rescale(s->rd, outlink->sample_rate, AV_TIME_BASE);
@ -143,8 +136,11 @@ static int config_filter(AVFilterContext *ctx)
av_log(ctx, AV_LOG_DEBUG, "K:%d S:%d H:%d N:%d\n", newK, newS, newH, newN);
if (!s->cache || s->cache->nb_samples < newS * 2) {
new_cache = ff_get_audio_buffer(outlink, newS * 2);
AVFrame *new_cache = ff_get_audio_buffer(outlink, newS * 2);
if (new_cache) {
if (s->cache)
av_samples_copy(new_cache->extended_data, s->cache->extended_data, 0, 0,
s->cache->nb_samples, new_cache->channels, new_cache->format);
av_frame_free(&s->cache);
s->cache = new_cache;
} else {
@ -154,6 +150,21 @@ static int config_filter(AVFilterContext *ctx)
if (!s->cache)
return AVERROR(ENOMEM);
if (!s->window || s->window->nb_samples < newN) {
AVFrame *new_window = ff_get_audio_buffer(outlink, newN);
if (new_window) {
if (s->window)
av_samples_copy(new_window->extended_data, s->window->extended_data, 0, 0,
s->window->nb_samples, new_window->channels, new_window->format);
av_frame_free(&s->window);
s->window = new_window;
} else {
return AVERROR(ENOMEM);
}
}
if (!s->window)
return AVERROR(ENOMEM);
s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE;
for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
float w = -i / s->pdiff_lut_scale;
@ -161,18 +172,6 @@ static int config_filter(AVFilterContext *ctx)
s->weight_lut[i] = expf(w);
}
if (!s->in || s->in->nb_samples < newN) {
new_in = ff_get_audio_buffer(outlink, newN);
if (new_in) {
av_frame_free(&s->in);
s->in = new_in;
} else {
return AVERROR(ENOMEM);
}
}
if (!s->in)
return AVERROR(ENOMEM);
s->K = newK;
s->S = newS;
s->H = newH;
@ -187,21 +186,10 @@ static int config_output(AVFilterLink *outlink)
AudioNLMeansContext *s = ctx->priv;
int ret;
s->eof_left = -1;
s->pts = AV_NOPTS_VALUE;
ret = config_filter(ctx);
if (ret < 0)
return ret;
s->fifo = av_audio_fifo_alloc(outlink->format, outlink->channels, s->N);
if (!s->fifo)
return AVERROR(ENOMEM);
ret = av_audio_fifo_write(s->fifo, (void **)s->in->extended_data, s->K + s->S);
if (ret < 0)
return ret;
ff_anlmdn_init(&s->dsp);
return 0;
@ -214,10 +202,10 @@ static int filter_channel(AVFilterContext *ctx, void *arg, int ch, int nb_jobs)
const int S = s->S;
const int K = s->K;
const int om = s->om;
const float *f = (const float *)(s->in->extended_data[ch]) + K;
const float *f = (const float *)(s->window->extended_data[ch]) + K;
float *cache = (float *)s->cache->extended_data[ch];
const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
float *dst = (float *)out->extended_data[ch] + s->offset;
float *dst = (float *)out->extended_data[ch];
const float *const weight_lut = s->weight_lut;
const float pdiff_lut_scale = s->pdiff_lut_scale;
const float smooth = fminf(s->m, WEIGHT_LUT_SIZE / pdiff_lut_scale);
@ -272,77 +260,56 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
AVFilterContext *ctx = inlink->dst;
AVFilterLink *outlink = ctx->outputs[0];
AudioNLMeansContext *s = ctx->priv;
AVFrame *out = NULL;
int available, wanted, ret;
const int offset = s->N - s->H;
AVFrame *out;
if (s->pts == AV_NOPTS_VALUE)
s->pts = in->pts;
out = ff_get_audio_buffer(outlink, in->nb_samples);
if (!out)
return AVERROR(ENOMEM);
ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
in->nb_samples);
for (int ch = 0; ch < in->channels; ch++) {
float *src = (float *)s->window->extended_data[ch];
memmove(src, &src[s->H], offset * sizeof(float));
memcpy(&src[offset], in->extended_data[ch], in->nb_samples * sizeof(float));
memset(&src[offset + in->nb_samples], 0, (s->H - in->nb_samples) * sizeof(float));
}
ff_filter_execute(ctx, filter_channel, out, NULL, inlink->channels);
out->pts = in->pts;
av_frame_free(&in);
s->offset = 0;
available = av_audio_fifo_size(s->fifo);
wanted = (available / s->H) * s->H;
if (wanted >= s->H && available >= s->N) {
out = ff_get_audio_buffer(outlink, wanted);
if (!out)
return AVERROR(ENOMEM);
}
while (available >= s->N) {
ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data, s->N);
if (ret < 0)
break;
ff_filter_execute(ctx, filter_channel, out, NULL, inlink->channels);
av_audio_fifo_drain(s->fifo, s->H);
s->offset += s->H;
available -= s->H;
}
if (out) {
out->pts = s->pts;
out->nb_samples = s->offset;
if (s->eof_left >= 0) {
out->nb_samples = FFMIN(s->eof_left, s->offset);
s->eof_left -= out->nb_samples;
}
s->pts += av_rescale_q(s->offset, (AVRational){1, outlink->sample_rate}, outlink->time_base);
return ff_filter_frame(outlink, out);
}
return ret;
return ff_filter_frame(outlink, out);
}
static int request_frame(AVFilterLink *outlink)
static int activate(AVFilterContext *ctx)
{
AVFilterContext *ctx = outlink->src;
AVFilterLink *inlink = ctx->inputs[0];
AVFilterLink *outlink = ctx->outputs[0];
AudioNLMeansContext *s = ctx->priv;
int ret;
AVFrame *in = NULL;
int ret = 0, status;
int64_t pts;
ret = ff_request_frame(ctx->inputs[0]);
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
if (ret == AVERROR_EOF && s->eof_left != 0) {
AVFrame *in;
ret = ff_inlink_consume_samples(inlink, s->H, s->H, &in);
if (ret < 0)
return ret;
if (s->eof_left < 0)
s->eof_left = av_audio_fifo_size(s->fifo) - (s->S + s->K);
if (s->eof_left <= 0)
return AVERROR_EOF;
in = ff_get_audio_buffer(outlink, s->H);
if (!in)
return AVERROR(ENOMEM);
return filter_frame(ctx->inputs[0], in);
if (ret > 0) {
return filter_frame(inlink, in);
} else if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
ff_outlink_set_status(outlink, status, pts);
return 0;
} else {
if (ff_inlink_queued_samples(inlink) >= s->H) {
ff_filter_set_ready(ctx, 10);
} else if (ff_outlink_frame_wanted(outlink)) {
ff_inlink_request_frame(inlink);
}
return 0;
}
return ret;
}
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
@ -354,27 +321,21 @@ static int process_command(AVFilterContext *ctx, const char *cmd, const char *ar
if (ret < 0)
return ret;
ret = config_filter(ctx);
if (ret < 0)
return ret;
return 0;
return config_filter(ctx);
}
static av_cold void uninit(AVFilterContext *ctx)
{
AudioNLMeansContext *s = ctx->priv;
av_audio_fifo_free(s->fifo);
av_frame_free(&s->in);
av_frame_free(&s->cache);
av_frame_free(&s->window);
}
static const AVFilterPad inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.filter_frame = filter_frame,
},
};
@ -383,7 +344,6 @@ static const AVFilterPad outputs[] = {
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.config_props = config_output,
.request_frame = request_frame,
},
};
@ -392,6 +352,7 @@ const AVFilter ff_af_anlmdn = {
.description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."),
.priv_size = sizeof(AudioNLMeansContext),
.priv_class = &anlmdn_class,
.activate = activate,
.uninit = uninit,
FILTER_INPUTS(inputs),
FILTER_OUTPUTS(outputs),