2010-08-24 11:38:00 +03:00
|
|
|
/*
|
2010-11-28 12:22:58 +02:00
|
|
|
* Copyright (c) 2007 Bobby Bingham
|
2010-08-24 11:38:00 +03: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 09:21:47 +03:00
|
|
|
* FIFO buffering filter
|
2010-08-24 11:38:00 +03:00
|
|
|
*/
|
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/common.h"
|
2012-05-27 15:18:49 +03:00
|
|
|
#include "libavutil/mathematics.h"
|
|
|
|
|
2012-05-16 09:21:47 +03:00
|
|
|
#include "audio.h"
|
2010-08-24 11:38:00 +03:00
|
|
|
#include "avfilter.h"
|
2012-05-30 12:20:32 +03:00
|
|
|
#include "internal.h"
|
2010-08-24 11:38:00 +03:00
|
|
|
|
2012-05-16 09:21:47 +03:00
|
|
|
typedef struct Buf {
|
2012-11-28 10:41:07 +03:00
|
|
|
AVFrame *frame;
|
2019-09-28 21:57:52 +02:00
|
|
|
struct Buf *next;
|
2012-05-16 09:21:47 +03:00
|
|
|
} Buf;
|
2010-08-24 11:38:00 +03:00
|
|
|
|
2014-04-11 12:54:15 +03:00
|
|
|
typedef struct FifoContext {
|
2012-05-16 09:21:47 +03:00
|
|
|
Buf root;
|
|
|
|
Buf *last; ///< last buffered frame
|
2012-05-27 15:18:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When a specific number of output samples is requested, the partial
|
|
|
|
* buffer is stored here
|
|
|
|
*/
|
2012-11-28 10:41:07 +03:00
|
|
|
AVFrame *out;
|
|
|
|
int allocated_samples; ///< number of samples out was allocated for
|
2010-08-24 11:38:00 +03:00
|
|
|
} FifoContext;
|
|
|
|
|
2013-03-13 10:26:39 +03:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2010-08-24 11:38:00 +03:00
|
|
|
{
|
2019-09-28 22:06:50 +02:00
|
|
|
FifoContext *s = ctx->priv;
|
|
|
|
s->last = &s->root;
|
2010-08-24 11:38:00 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
2019-09-28 22:06:50 +02:00
|
|
|
FifoContext *s = ctx->priv;
|
2012-05-16 09:21:47 +03:00
|
|
|
Buf *buf, *tmp;
|
2010-08-24 11:38:00 +03:00
|
|
|
|
2019-09-28 22:06:50 +02:00
|
|
|
for (buf = s->root.next; buf; buf = tmp) {
|
2012-05-16 09:21:47 +03:00
|
|
|
tmp = buf->next;
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&buf->frame);
|
2012-05-16 09:21:47 +03:00
|
|
|
av_free(buf);
|
2010-08-24 11:38:00 +03:00
|
|
|
}
|
2012-05-27 15:18:49 +03:00
|
|
|
|
2019-09-28 22:06:50 +02:00
|
|
|
av_frame_free(&s->out);
|
2010-08-24 11:38:00 +03:00
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static int add_to_queue(AVFilterLink *inlink, AVFrame *frame)
|
2010-08-24 11:38:00 +03:00
|
|
|
{
|
2019-09-28 22:06:50 +02:00
|
|
|
FifoContext *s = inlink->dst->priv;
|
2010-08-24 11:38:00 +03:00
|
|
|
|
2019-09-28 22:06:50 +02:00
|
|
|
s->last->next = av_mallocz(sizeof(Buf));
|
|
|
|
if (!s->last->next) {
|
2012-11-28 10:41:07 +03:00
|
|
|
av_frame_free(&frame);
|
2012-07-02 21:13:40 +03:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2019-09-28 22:06:50 +02:00
|
|
|
s->last = s->last->next;
|
|
|
|
s->last->frame = frame;
|
2012-07-02 21:13:40 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-27 15:18:49 +03: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 11:38:00 +03:00
|
|
|
static int request_frame(AVFilterLink *outlink)
|
|
|
|
{
|
2019-09-28 22:06:50 +02:00
|
|
|
FifoContext *s = outlink->src->priv;
|
2012-07-02 21:13:40 +03:00
|
|
|
int ret = 0;
|
2010-08-24 11:38:00 +03:00
|
|
|
|
2019-09-28 22:06:50 +02:00
|
|
|
if (!s->root.next) {
|
2020-08-12 19:17:29 +02:00
|
|
|
if ((ret = ff_request_frame(outlink->src->inputs[0])) < 0)
|
2010-08-24 11:38:00 +03:00
|
|
|
return ret;
|
2019-09-28 22:06:50 +02:00
|
|
|
if (!s->root.next)
|
2015-10-22 09:57:34 +02:00
|
|
|
return 0;
|
2010-08-24 11:38:00 +03:00
|
|
|
}
|
2020-08-12 19:17:29 +02:00
|
|
|
ret = ff_filter_frame(outlink, s->root.next->frame);
|
|
|
|
queue_pop(s);
|
2012-07-02 21:13:40 +03:00
|
|
|
return ret;
|
2010-08-24 11:38:00 +03:00
|
|
|
}
|
|
|
|
|
2012-07-24 16:14:01 +03:00
|
|
|
static const AVFilterPad avfilter_vf_fifo_inputs[] = {
|
|
|
|
{
|
2019-09-28 21:57:52 +02:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.filter_frame = add_to_queue,
|
2012-07-24 16:14:01 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_vf_fifo_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_fifo = {
|
2019-09-28 21:57:52 +02:00
|
|
|
.name = "fifo",
|
2010-08-24 11:38:00 +03:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer input images and send them when they are requested."),
|
2019-09-28 21:57:52 +02:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(FifoContext),
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(avfilter_vf_fifo_inputs),
|
|
|
|
FILTER_OUTPUTS(avfilter_vf_fifo_outputs),
|
2012-07-24 16:14:01 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_afifo_inputs[] = {
|
|
|
|
{
|
2019-09-28 21:57:52 +02:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.filter_frame = add_to_queue,
|
2012-07-24 16:14:01 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad avfilter_af_afifo_outputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
},
|
2010-08-24 11:38:00 +03:00
|
|
|
};
|
2012-05-16 09:21:47 +03:00
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_afifo = {
|
2012-05-16 09:21:47 +03:00
|
|
|
.name = "afifo",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Buffer input frames and send them when they are requested."),
|
2019-09-28 21:57:52 +02:00
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.priv_size = sizeof(FifoContext),
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(avfilter_af_afifo_inputs),
|
|
|
|
FILTER_OUTPUTS(avfilter_af_afifo_outputs),
|
2012-05-16 09:21:47 +03:00
|
|
|
};
|