You've already forked FFmpeg
mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-06-30 22:24:04 +02:00
avfilter/af_anlmdn: stop using fifo and rewriting pts
This commit is contained in:
@ -21,12 +21,12 @@
|
|||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
|
||||||
#include "libavutil/avassert.h"
|
#include "libavutil/avassert.h"
|
||||||
#include "libavutil/audio_fifo.h"
|
|
||||||
#include "libavutil/avstring.h"
|
#include "libavutil/avstring.h"
|
||||||
#include "libavutil/opt.h"
|
#include "libavutil/opt.h"
|
||||||
#include "avfilter.h"
|
#include "avfilter.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "formats.h"
|
#include "formats.h"
|
||||||
|
#include "filters.h"
|
||||||
|
|
||||||
#include "af_anlmdndsp.h"
|
#include "af_anlmdndsp.h"
|
||||||
|
|
||||||
@ -50,14 +50,8 @@ typedef struct AudioNLMeansContext {
|
|||||||
int N;
|
int N;
|
||||||
int H;
|
int H;
|
||||||
|
|
||||||
int offset;
|
|
||||||
AVFrame *in;
|
|
||||||
AVFrame *cache;
|
AVFrame *cache;
|
||||||
|
AVFrame *window;
|
||||||
int64_t pts;
|
|
||||||
|
|
||||||
AVAudioFifo *fifo;
|
|
||||||
int eof_left;
|
|
||||||
|
|
||||||
AudioNLMDNDSPContext dsp;
|
AudioNLMDNDSPContext dsp;
|
||||||
} AudioNLMeansContext;
|
} AudioNLMeansContext;
|
||||||
@ -132,7 +126,6 @@ static int config_filter(AVFilterContext *ctx)
|
|||||||
AudioNLMeansContext *s = ctx->priv;
|
AudioNLMeansContext *s = ctx->priv;
|
||||||
AVFilterLink *outlink = ctx->outputs[0];
|
AVFilterLink *outlink = ctx->outputs[0];
|
||||||
int newK, newS, newH, newN;
|
int newK, newS, newH, newN;
|
||||||
AVFrame *new_in, *new_cache;
|
|
||||||
|
|
||||||
newK = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
|
newK = av_rescale(s->pd, outlink->sample_rate, AV_TIME_BASE);
|
||||||
newS = av_rescale(s->rd, 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);
|
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) {
|
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 (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);
|
av_frame_free(&s->cache);
|
||||||
s->cache = new_cache;
|
s->cache = new_cache;
|
||||||
} else {
|
} else {
|
||||||
@ -154,6 +150,21 @@ static int config_filter(AVFilterContext *ctx)
|
|||||||
if (!s->cache)
|
if (!s->cache)
|
||||||
return AVERROR(ENOMEM);
|
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;
|
s->pdiff_lut_scale = 1.f / s->m * WEIGHT_LUT_SIZE;
|
||||||
for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
|
for (int i = 0; i < WEIGHT_LUT_SIZE; i++) {
|
||||||
float w = -i / s->pdiff_lut_scale;
|
float w = -i / s->pdiff_lut_scale;
|
||||||
@ -161,18 +172,6 @@ static int config_filter(AVFilterContext *ctx)
|
|||||||
s->weight_lut[i] = expf(w);
|
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->K = newK;
|
||||||
s->S = newS;
|
s->S = newS;
|
||||||
s->H = newH;
|
s->H = newH;
|
||||||
@ -187,21 +186,10 @@ static int config_output(AVFilterLink *outlink)
|
|||||||
AudioNLMeansContext *s = ctx->priv;
|
AudioNLMeansContext *s = ctx->priv;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
s->eof_left = -1;
|
|
||||||
s->pts = AV_NOPTS_VALUE;
|
|
||||||
|
|
||||||
ret = config_filter(ctx);
|
ret = config_filter(ctx);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return ret;
|
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);
|
ff_anlmdn_init(&s->dsp);
|
||||||
|
|
||||||
return 0;
|
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 S = s->S;
|
||||||
const int K = s->K;
|
const int K = s->K;
|
||||||
const int om = s->om;
|
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];
|
float *cache = (float *)s->cache->extended_data[ch];
|
||||||
const float sw = (65536.f / (4 * K + 2)) / sqrtf(s->a);
|
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 *const weight_lut = s->weight_lut;
|
||||||
const float pdiff_lut_scale = s->pdiff_lut_scale;
|
const float pdiff_lut_scale = s->pdiff_lut_scale;
|
||||||
const float smooth = fminf(s->m, WEIGHT_LUT_SIZE / 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;
|
AVFilterContext *ctx = inlink->dst;
|
||||||
AVFilterLink *outlink = ctx->outputs[0];
|
AVFilterLink *outlink = ctx->outputs[0];
|
||||||
AudioNLMeansContext *s = ctx->priv;
|
AudioNLMeansContext *s = ctx->priv;
|
||||||
AVFrame *out = NULL;
|
const int offset = s->N - s->H;
|
||||||
int available, wanted, ret;
|
AVFrame *out;
|
||||||
|
|
||||||
if (s->pts == AV_NOPTS_VALUE)
|
out = ff_get_audio_buffer(outlink, in->nb_samples);
|
||||||
s->pts = in->pts;
|
|
||||||
|
|
||||||
ret = av_audio_fifo_write(s->fifo, (void **)in->extended_data,
|
|
||||||
in->nb_samples);
|
|
||||||
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)
|
if (!out)
|
||||||
return AVERROR(ENOMEM);
|
return AVERROR(ENOMEM);
|
||||||
}
|
|
||||||
|
|
||||||
while (available >= s->N) {
|
for (int ch = 0; ch < in->channels; ch++) {
|
||||||
ret = av_audio_fifo_peek(s->fifo, (void **)s->in->extended_data, s->N);
|
float *src = (float *)s->window->extended_data[ch];
|
||||||
if (ret < 0)
|
|
||||||
break;
|
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);
|
ff_filter_execute(ctx, filter_channel, out, NULL, inlink->channels);
|
||||||
|
|
||||||
av_audio_fifo_drain(s->fifo, s->H);
|
out->pts = in->pts;
|
||||||
|
av_frame_free(&in);
|
||||||
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 ff_filter_frame(outlink, out);
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
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;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
ret = ff_inlink_consume_samples(inlink, s->H, s->H, &in);
|
||||||
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int process_command(AVFilterContext *ctx, const char *cmd, const char *args,
|
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)
|
if (ret < 0)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
ret = config_filter(ctx);
|
return config_filter(ctx);
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static av_cold void uninit(AVFilterContext *ctx)
|
static av_cold void uninit(AVFilterContext *ctx)
|
||||||
{
|
{
|
||||||
AudioNLMeansContext *s = ctx->priv;
|
AudioNLMeansContext *s = ctx->priv;
|
||||||
|
|
||||||
av_audio_fifo_free(s->fifo);
|
|
||||||
av_frame_free(&s->in);
|
|
||||||
av_frame_free(&s->cache);
|
av_frame_free(&s->cache);
|
||||||
|
av_frame_free(&s->window);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const AVFilterPad inputs[] = {
|
static const AVFilterPad inputs[] = {
|
||||||
{
|
{
|
||||||
.name = "default",
|
.name = "default",
|
||||||
.type = AVMEDIA_TYPE_AUDIO,
|
.type = AVMEDIA_TYPE_AUDIO,
|
||||||
.filter_frame = filter_frame,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -383,7 +344,6 @@ static const AVFilterPad outputs[] = {
|
|||||||
.name = "default",
|
.name = "default",
|
||||||
.type = AVMEDIA_TYPE_AUDIO,
|
.type = AVMEDIA_TYPE_AUDIO,
|
||||||
.config_props = config_output,
|
.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."),
|
.description = NULL_IF_CONFIG_SMALL("Reduce broadband noise from stream using Non-Local Means."),
|
||||||
.priv_size = sizeof(AudioNLMeansContext),
|
.priv_size = sizeof(AudioNLMeansContext),
|
||||||
.priv_class = &anlmdn_class,
|
.priv_class = &anlmdn_class,
|
||||||
|
.activate = activate,
|
||||||
.uninit = uninit,
|
.uninit = uninit,
|
||||||
FILTER_INPUTS(inputs),
|
FILTER_INPUTS(inputs),
|
||||||
FILTER_OUTPUTS(outputs),
|
FILTER_OUTPUTS(outputs),
|
||||||
|
Reference in New Issue
Block a user