avfilter/avf_showfreqs: switch to activate

This commit is contained in:
Paul B Mahol 2019-05-26 17:11:17 +02:00
parent a9fa6b8e02
commit fc0de1c04b
1 changed files with 50 additions and 28 deletions

View File

@ -30,6 +30,7 @@
#include "libavutil/opt.h"
#include "libavutil/parseutils.h"
#include "audio.h"
#include "filters.h"
#include "video.h"
#include "avfilter.h"
#include "internal.h"
@ -439,27 +440,21 @@ static int plot_freqs(AVFilterLink *inlink, AVFrame *in)
return ff_filter_frame(outlink, out);
}
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
static int filter_frame(AVFilterLink *inlink)
{
AVFilterContext *ctx = inlink->dst;
ShowFreqsContext *s = ctx->priv;
AVFrame *fin = NULL;
int consumed = 0;
int ret = 0;
if (s->pts == AV_NOPTS_VALUE)
s->pts = in->pts - av_audio_fifo_size(s->fifo);
av_audio_fifo_write(s->fifo, (void **)in->extended_data, in->nb_samples);
while (av_audio_fifo_size(s->fifo) >= s->win_size) {
fin = ff_get_audio_buffer(inlink, s->win_size);
if (!fin) {
ret = AVERROR(ENOMEM);
goto fail;
}
fin->pts = s->pts + consumed;
consumed += s->hop_size;
fin->pts = s->pts;
s->pts += s->hop_size;
ret = av_audio_fifo_peek(s->fifo, (void **)fin->extended_data, s->win_size);
if (ret < 0)
goto fail;
@ -467,17 +462,44 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
ret = plot_freqs(inlink, fin);
av_frame_free(&fin);
av_audio_fifo_drain(s->fifo, s->hop_size);
if (ret < 0)
goto fail;
}
fail:
s->pts = AV_NOPTS_VALUE;
av_frame_free(&fin);
av_frame_free(&in);
return ret;
}
static int activate(AVFilterContext *ctx)
{
AVFilterLink *inlink = ctx->inputs[0];
AVFilterLink *outlink = ctx->outputs[0];
ShowFreqsContext *s = ctx->priv;
AVFrame *in = NULL;
int ret = 0;
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
if (av_audio_fifo_size(s->fifo) < s->win_size)
ret = ff_inlink_consume_samples(inlink, s->win_size, s->win_size, &in);
if (ret < 0)
return ret;
if (ret > 0) {
av_audio_fifo_write(s->fifo, (void **)in->extended_data, in->nb_samples);
if (s->pts == AV_NOPTS_VALUE)
s->pts = in->pts;
}
if (av_audio_fifo_size(s->fifo) >= s->win_size) {
ret = filter_frame(inlink);
if (ret <= 0)
return ret;
}
FF_FILTER_FORWARD_STATUS(inlink, outlink);
FF_FILTER_FORWARD_WANTED(outlink, inlink);
return FFERROR_NOT_READY;
}
static av_cold void uninit(AVFilterContext *ctx)
{
ShowFreqsContext *s = ctx->priv;
@ -500,7 +522,6 @@ static const AVFilterPad showfreqs_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.filter_frame = filter_frame,
},
{ NULL }
};
@ -521,6 +542,7 @@ AVFilter ff_avf_showfreqs = {
.uninit = uninit,
.query_formats = query_formats,
.priv_size = sizeof(ShowFreqsContext),
.activate = activate,
.inputs = showfreqs_inputs,
.outputs = showfreqs_outputs,
.priv_class = &showfreqs_class,