2010-08-24 08:38:00 +00:00
|
|
|
/*
|
2010-11-28 10:22:58 +00:00
|
|
|
* Copyright (c) 2007 Bobby Bingham
|
2010-08-24 08:38:00 +00:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2012-05-16 06:21:47 +00:00
|
|
|
* FIFO buffering filter
|
2010-08-24 08:38:00 +00:00
|
|
|
*/
|
|
|
|
|
2012-08-06 13:49:32 +00:00
|
|
|
#include "libavutil/common.h"
|
2012-05-27 12:18:49 +00:00
|
|
|
#include "libavutil/mathematics.h"
|
|
|
|
|
2012-05-16 06:21:47 +00:00
|
|
|
#include "audio.h"
|
2010-08-24 08:38:00 +00:00
|
|
|
#include "avfilter.h"
|
2012-05-30 09:20:32 +00:00
|
|
|
#include "internal.h"
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2012-05-16 06:21:47 +00:00
|
|
|
typedef struct Buf {
|
2012-11-28 07:41:07 +00:00
|
|
|
AVFrame *frame;
|
2019-09-28 19:57:52 +00:00
|
|
|
struct Buf *next;
|
2012-05-16 06:21:47 +00:00
|
|
|
} Buf;
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2014-04-11 09:54:15 +00:00
|
|
|
typedef struct FifoContext {
|
2012-05-16 06:21:47 +00:00
|
|
|
Buf root;
|
|
|
|
Buf *last; ///< last buffered frame
|
2012-05-27 12:18:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When a specific number of output samples is requested, the partial
|
|
|
|
* buffer is stored here
|
|
|
|
*/
|
2012-11-28 07:41:07 +00:00
|
|
|
AVFrame *out;
|
|
|
|
int allocated_samples; ///< number of samples out was allocated for
|
2010-08-24 08:38:00 +00:00
|
|
|
} FifoContext;
|
|
|
|
|
2013-03-13 07:26:39 +00:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2010-08-24 08:38:00 +00:00
|
|
|
{
|
2019-09-28 20:06:50 +00:00
|
|
|
FifoContext *s = ctx->priv;
|
|
|
|
s->last = &s->root;
|
2010-08-24 08:38:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
2019-09-28 20:06:50 +00:00
|
|
|
FifoContext *s = ctx->priv;
|
2012-05-16 06:21:47 +00:00
|
|
|
Buf *buf, *tmp;
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2019-09-28 20:06:50 +00:00
|
|
|
for (buf = s->root.next; buf; buf = tmp) {
|
2012-05-16 06:21:47 +00:00
|
|
|
tmp = buf->next;
|
2012-11-28 07:41:07 +00:00
|
|
|
av_frame_free(&buf->frame);
|
2012-05-16 06:21:47 +00:00
|
|
|
av_free(buf);
|
2010-08-24 08:38:00 +00:00
|
|
|
}
|
2012-05-27 12:18:49 +00:00
|
|
|
|
2019-09-28 20:06:50 +00:00
|
|
|
av_frame_free(&s->out);
|
2010-08-24 08:38:00 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 07:41:07 +00:00
|
|
|
static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
|
2010-08-24 08:38:00 +00:00
|
|
|
{
|
2019-09-28 20:06:50 +00:00
|
|
|
FifoContext *s = inlink->dst->priv;
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2019-09-28 20:06:50 +00:00
|
|
|
s->last->next = av_mallocz(sizeof(Buf));
|
|
|
|
if (!s->last->next) {
|
2012-11-28 07:41:07 +00:00
|
|
|
av_frame_free(&frame);
|
2012-07-02 18:13:40 +00:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2019-09-28 20:06:50 +00:00
|
|
|
s->last = s->last->next;
|
|
|
|
s->last->frame = frame;
|
2012-07-02 18:13:40 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-27 12:18:49 +00:00
|
|
|
static void queue_pop(FifoContext *s)
|
|
|
|
{
|
|
|
|
Buf *tmp = s->root.next->next;
|
|
|
|
if (s->last == s->root.next)
|
|
|
|
s->last = &s->root;
|
|
|
|
av_freep(&s->root.next);
|
|
|
|
s->root.next = tmp;
|
|
|
|
}
|
|
|
|
|
2010-08-24 08:38:00 +00:00
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
2019-09-28 20:06:50 +00:00
|
|
|
FifoContext *s = outlink->src->priv;
|
2012-07-02 18:13:40 +00:00
|
|
|
int ret = 0;
|
2010-08-24 08:38:00 +00:00
|
|
|
|
2019-09-28 20:06:50 +00:00
|
|
|
if (!s->root.next) {
|
2020-08-12 17:17:29 +00:00
|
|
|
if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
|
2010-08-24 08:38:00 +00:00
|
|
|
return ret;
|
2019-09-28 20:06:50 +00:00
|
|
|
if (!s->root.next)
|
2015-10-22 07:57:34 +00:00
|
|
|
return 0;
|
2010-08-24 08:38:00 +00:00
|
|
|
}
|
2020-08-12 17:17:29 +00:00
|
|
|
ret = ff_filter_frame(outlink, s->root.next->frame);
|
|
|
|
queue_pop(s);
|
2012-07-02 18:13:40 +00:00
|
|
|
return ret;
|
2010-08-24 08:38:00 +00:00
|
|
|
}
|
|
|
|
|
2012-07-24 13:14:01 +00:00
|
|
|
static const AVFilterPad avfilter_vf_fifo_inputs[] = {
|
|
|
|
{
|
2019-09-28 19:57:52 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.filter_frame = add_to_queue,
|
2012-07-24 13:14:01 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_fifo_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_vf_fifo = {
|
2019-09-28 19:57:52 +00:00
|
|
|
.name = "fifo",
|
2010-08-24 08:38:00 +00:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
|
2019-09-28 19:57:52 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(FifoContext),
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(avfilter_vf_fifo_inputs),
|
|
|
|
FILTER_OUTPUTS(avfilter_vf_fifo_outputs),
|
2012-07-24 13:14:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_afifo_inputs[] = {
|
|
|
|
{
|
2019-09-28 19:57:52 +00:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = add_to_queue,
|
2012-07-24 13:14:01 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_afifo_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
2010-08-24 08:38:00 +00:00
|
|
|
};
|
2012-05-16 06:21:47 +00:00
|
|
|
|
2021-04-19 16:33:56 +00:00
|
|
|
const AVFilter ff_af_afifo = {
|
2012-05-16 06:21:47 +00:00
|
|
|
.name = "afifo",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
|
2019-09-28 19:57:52 +00:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(FifoContext),
|
2021-08-12 11:05:31 +00:00
|
|
|
FILTER_INPUTS(avfilter_af_afifo_inputs),
|
|
|
|
FILTER_OUTPUTS(avfilter_af_afifo_outputs),
|
2012-05-16 06:21:47 +00:00
|
|
|
};
|