From 58b049f2fa4f192b00baadb5f1f32ca366f936ea Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Wed, 16 May 2012 09:19:46 +0200 Subject: [PATCH] lavfi: support automatically inserting the fifo filter when needed. This breaks libavfilter ABI. --- doc/APIchanges | 2 +- libavfilter/avfilter.h | 8 ++++ libavfilter/avfiltergraph.c | 40 ++++++++++++++++++ libavfilter/buffersink.c | 81 +++++++++---------------------------- libavfilter/internal.h | 8 ++++ libavfilter/version.h | 4 +- 6 files changed, 79 insertions(+), 64 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index b84c7ae85c..990e36e5c0 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -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 diff --git a/libavfilter/avfilter.h b/libavfilter/avfilter.h index aff662f8f6..c92f7e14d4 100644 --- a/libavfilter/avfilter.h +++ b/libavfilter/avfilter.h @@ -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 diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c index 3d463a84d5..95e0a219af 100644 --- a/libavfilter/avfiltergraph.c +++ b/libavfilter/avfiltergraph.c @@ -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))) diff --git a/libavfilter/buffersink.c b/libavfilter/buffersink.c index 13fb2558c1..50d949b2ac 100644 --- a/libavfilter/buffersink.c +++ b/libavfilter/buffersink.c @@ -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) +static void start_frame(AVFilterLink *link, AVFilterBufferRef *buf) { - BufferSinkContext *sink = ctx->priv; + BufferSinkContext *s = link->dst->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) -{ - 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; - } - - 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 }}, }; diff --git a/libavfilter/internal.h b/libavfilter/internal.h index 01b8f664f8..954610e3ca 100644 --- a/libavfilter/internal.h +++ b/libavfilter/internal.h @@ -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 diff --git a/libavfilter/version.h b/libavfilter/version.h index 562147b657..b8f2749e12 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -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, \