Commit 58b049f2 authored by Anton Khirnov's avatar Anton Khirnov

lavfi: support automatically inserting the fifo filter when needed.

This breaks libavfilter ABI.
parent fa066239
......@@ -4,7 +4,7 @@ since the last major version increase.
The last version increases were:
libavcodec: 2012-01-27
libavdevice: 2011-04-18
libavfilter: 2011-04-18
libavfilter: 2012-06-22
libavformat: 2012-01-27
libavresample: 2012-04-24
libswscale: 2011-06-20
......
......@@ -363,6 +363,14 @@ struct AVFilterPad {
* and another value on error.
*/
int (*config_props)(AVFilterLink *link);
/**
* The filter expects a fifo to be inserted on its input link,
* typically because it has a delay.
*
* input pads only.
*/
int needs_fifo;
};
#endif
......
......@@ -591,12 +591,52 @@ static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
return 0;
}
static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
{
AVFilterContext *f;
int i, j, ret;
int fifo_count = 0;
for (i = 0; i < graph->filter_count; i++) {
f = graph->filters[i];
for (j = 0; j < f->nb_inputs; j++) {
AVFilterLink *link = f->inputs[j];
AVFilterContext *fifo_ctx;
AVFilter *fifo;
char name[32];
if (!link->dstpad->needs_fifo)
continue;
fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
avfilter_get_by_name("fifo") :
avfilter_get_by_name("afifo");
snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
NULL, graph);
if (ret < 0)
return ret;
ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
if (ret < 0)
return ret;
}
}
return 0;
}
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
{
int ret;
if ((ret = graph_check_validity(graphctx, log_ctx)))
return ret;
if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
return ret;
if ((ret = graph_config_formats(graphctx, log_ctx)))
return ret;
if ((ret = graph_config_links(graphctx, log_ctx)))
......
......@@ -25,7 +25,7 @@
#include "libavutil/audio_fifo.h"
#include "libavutil/audioconvert.h"
#include "libavutil/fifo.h"
#include "libavutil/avassert.h"
#include "libavutil/mathematics.h"
#include "audio.h"
......@@ -34,86 +34,45 @@
#include "internal.h"
typedef struct {
AVFifoBuffer *fifo; ///< FIFO buffer of frame references
AVFilterBufferRef *cur_buf; ///< last buffer delivered on the sink
AVAudioFifo *audio_fifo; ///< FIFO for audio samples
int64_t next_pts; ///< interpolating audio pts
} BufferSinkContext;
#define FIFO_INIT_SIZE 8
static av_cold void uninit(AVFilterContext *ctx)
{
BufferSinkContext *sink = ctx->priv;
while (sink->fifo && av_fifo_size(sink->fifo)) {
AVFilterBufferRef *buf;
av_fifo_generic_read(sink->fifo, &buf, sizeof(buf), NULL);
avfilter_unref_buffer(buf);
}
av_fifo_free(sink->fifo);
if (sink->audio_fifo)
av_audio_fifo_free(sink->audio_fifo);
}
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
{
BufferSinkContext *sink = ctx->priv;
if (!(sink->fifo = av_fifo_alloc(FIFO_INIT_SIZE*sizeof(AVFilterBufferRef*)))) {
av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n");
return AVERROR(ENOMEM);
}
return 0;
}
static void write_buf(AVFilterContext *ctx, AVFilterBufferRef *buf)
static void start_frame(AVFilterLink *link, AVFilterBufferRef *buf)
{
BufferSinkContext *sink = ctx->priv;
if (av_fifo_space(sink->fifo) < sizeof(AVFilterBufferRef *) &&
(av_fifo_realloc2(sink->fifo, av_fifo_size(sink->fifo) * 2) < 0)) {
av_log(ctx, AV_LOG_ERROR, "Error reallocating the FIFO.\n");
return;
}
BufferSinkContext *s = link->dst->priv;
av_fifo_generic_write(sink->fifo, &buf, sizeof(buf), NULL);
}
static void end_frame(AVFilterLink *link)
{
write_buf(link->dst, link->cur_buf);
av_assert0(!s->cur_buf);
s->cur_buf = buf;
link->cur_buf = NULL;
}
static void filter_samples(AVFilterLink *link, AVFilterBufferRef *buf)
{
write_buf(link->dst, buf);
}
};
int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf)
{
BufferSinkContext *sink = ctx->priv;
BufferSinkContext *s = ctx->priv;
AVFilterLink *link = ctx->inputs[0];
int ret;
if (!buf) {
if (av_fifo_size(sink->fifo))
return av_fifo_size(sink->fifo)/sizeof(*buf);
else
return ff_poll_frame(ctx->inputs[0]);
}
if (!buf)
return ff_poll_frame(ctx->inputs[0]);
if (!av_fifo_size(sink->fifo) &&
(ret = ff_request_frame(link)) < 0)
if ((ret = ff_request_frame(link)) < 0)
return ret;
if (!av_fifo_size(sink->fifo))
if (!s->cur_buf)
return AVERROR(EINVAL);
av_fifo_generic_read(sink->fifo, buf, sizeof(*buf), NULL);
*buf = s->cur_buf;
s->cur_buf = NULL;
return 0;
}
......@@ -182,13 +141,13 @@ AVFilter avfilter_vsink_buffer = {
.name = "buffersink",
.description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."),
.priv_size = sizeof(BufferSinkContext),
.init = init,
.uninit = uninit,
.inputs = (AVFilterPad[]) {{ .name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.end_frame = end_frame,
.min_perms = AV_PERM_READ, },
.start_frame = start_frame,
.min_perms = AV_PERM_READ,
.needs_fifo = 1 },
{ .name = NULL }},
.outputs = (AVFilterPad[]) {{ .name = NULL }},
};
......@@ -197,13 +156,13 @@ AVFilter avfilter_asink_abuffer = {
.name = "abuffersink",
.description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."),
.priv_size = sizeof(BufferSinkContext),
.init = init,
.uninit = uninit,
.inputs = (AVFilterPad[]) {{ .name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.filter_samples = filter_samples,
.min_perms = AV_PERM_READ, },
.filter_samples = start_frame,
.min_perms = AV_PERM_READ,
.needs_fifo = 1 },
{ .name = NULL }},
.outputs = (AVFilterPad[]) {{ .name = NULL }},
};
......@@ -149,6 +149,14 @@ struct AVFilterPad {
* and another value on error.
*/
int (*config_props)(AVFilterLink *link);
/**
* The filter expects a fifo to be inserted on its input link,
* typically because it has a delay.
*
* input pads only.
*/
int needs_fifo;
};
#endif
......
......@@ -28,8 +28,8 @@
#include "libavutil/avutil.h"
#define LIBAVFILTER_VERSION_MAJOR 2
#define LIBAVFILTER_VERSION_MINOR 23
#define LIBAVFILTER_VERSION_MAJOR 3
#define LIBAVFILTER_VERSION_MINOR 0
#define LIBAVFILTER_VERSION_MICRO 0
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment