From patchwork Sat Dec 24 17:41:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas George X-Patchwork-Id: 1922 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp384001vsb; Sat, 24 Dec 2016 09:44:18 -0800 (PST) X-Received: by 10.194.140.234 with SMTP id rj10mr11437414wjb.39.1482601458165; Sat, 24 Dec 2016 09:44:18 -0800 (PST) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w9si40011683wjq.15.2016.12.24.09.44.17; Sat, 24 Dec 2016 09:44:18 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 21F1B689D06; Sat, 24 Dec 2016 19:42:19 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from nef2.ens.fr (nef2.ens.fr [129.199.96.40]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9E970689C15 for ; Sat, 24 Dec 2016 19:42:11 +0200 (EET) Received: from phare.normalesup.org (archicubes.ens.fr [129.199.129.80]) by nef2.ens.fr (8.13.6/1.01.28121999) with ESMTP id uBOHgCIX095840 for ; Sat, 24 Dec 2016 18:42:12 +0100 (CET) Received: by phare.normalesup.org (Postfix, from userid 1001) id 75673E0087; Sat, 24 Dec 2016 18:42:12 +0100 (CET) From: Nicolas George To: ffmpeg-devel@ffmpeg.org Date: Sat, 24 Dec 2016 18:41:49 +0100 Message-Id: <20161224174149.8995-18-george@nsup.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20161224174149.8995-1-george@nsup.org> References: <20161224174149.8995-1-george@nsup.org> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (nef2.ens.fr [129.199.96.32]); Sat, 24 Dec 2016 18:42:12 +0100 (CET) Subject: [FFmpeg-devel] [PATCH 17/17] lavfi/buffersink: move to the new design. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Nicolas George --- libavfilter/buffersink.c | 211 ++++++++++++----------------------------------- 1 file changed, 54 insertions(+), 157 deletions(-) Hum, it seems diff is not making it easy to see what functions are changed, deleted or added. return_or_keep_frame() is a small helper, entirely new. av_buffersink_get_frame() does not change at all, it's just diff matching empty lines and braces around it to another part of the code. get_frame_internal() is a complete rewrite of av_buffersink_get_frame_flags(); better read it by itself instead of as a diff. read_from_fifo() is gone. The TODO comment is about a possible future enhancement of the public API. diff --git a/libavfilter/buffersink.c b/libavfilter/buffersink.c index 030ca80315..fe653addae 100644 --- a/libavfilter/buffersink.c +++ b/libavfilter/buffersink.c @@ -23,12 +23,10 @@ * buffer sink */ -#include "libavutil/audio_fifo.h" #include "libavutil/avassert.h" #include "libavutil/channel_layout.h" #include "libavutil/common.h" #include "libavutil/internal.h" -#include "libavutil/mathematics.h" #include "libavutil/opt.h" #define FF_INTERNAL_FIELDS 1 @@ -41,7 +39,6 @@ typedef struct BufferSinkContext { const AVClass *class; - AVFifoBuffer *fifo; ///< FIFO buffer of video frame references unsigned warning_limit; /* only used for video */ @@ -59,182 +56,73 @@ typedef struct BufferSinkContext { int *sample_rates; ///< list of accepted sample rates, terminated by -1 int sample_rates_size; - /* only used for compat API */ - AVAudioFifo *audio_fifo; ///< FIFO for audio samples - int64_t next_pts; ///< interpolating audio pts + AVFrame *peeked_frame; } BufferSinkContext; #define NB_ITEMS(list) (list ## _size / sizeof(*list)) #define FIFO_INIT_SIZE 8 #define FIFO_INIT_ELEMENT_SIZE sizeof(void *) -static av_cold void uninit(AVFilterContext *ctx) -{ - BufferSinkContext *sink = ctx->priv; - AVFrame *frame; - - if (sink->audio_fifo) - av_audio_fifo_free(sink->audio_fifo); - - if (sink->fifo) { - while (av_fifo_size(sink->fifo) >= FIFO_INIT_ELEMENT_SIZE) { - av_fifo_generic_read(sink->fifo, &frame, sizeof(frame), NULL); - av_frame_free(&frame); - } - av_fifo_freep(&sink->fifo); - } -} - -static int add_buffer_ref(AVFilterContext *ctx, AVFrame *ref) +int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame) { - BufferSinkContext *buf = ctx->priv; - - if (av_fifo_space(buf->fifo) < FIFO_INIT_ELEMENT_SIZE) { - /* realloc fifo size */ - if (av_fifo_realloc2(buf->fifo, av_fifo_size(buf->fifo) * 2) < 0) { - av_log(ctx, AV_LOG_ERROR, - "Cannot buffer more frames. Consume some available frames " - "before adding new ones.\n"); - return AVERROR(ENOMEM); - } - } - - /* cache frame */ - av_fifo_generic_write(buf->fifo, &ref, FIFO_INIT_ELEMENT_SIZE, NULL); - return 0; + return av_buffersink_get_frame_flags(ctx, frame, 0); } -static int filter_frame(AVFilterLink *link, AVFrame *frame) +static int return_or_keep_frame(BufferSinkContext *buf, AVFrame *out, AVFrame *in, int flags) { - AVFilterContext *ctx = link->dst; - BufferSinkContext *buf = link->dst->priv; - int ret; - - if ((ret = add_buffer_ref(ctx, frame)) < 0) - return ret; - if (buf->warning_limit && - av_fifo_size(buf->fifo) / FIFO_INIT_ELEMENT_SIZE >= buf->warning_limit) { - av_log(ctx, AV_LOG_WARNING, - "%d buffers queued in %s, something may be wrong.\n", - buf->warning_limit, - (char *)av_x_if_null(ctx->name, ctx->filter->name)); - buf->warning_limit *= 10; + if ((flags & AV_BUFFERSINK_FLAG_PEEK)) { + buf->peeked_frame = in; + return out ? av_frame_ref(out, in) : 0; + } else { + av_assert1(out); + buf->peeked_frame = NULL; + av_frame_move_ref(out, in); + av_frame_free(&in); + return 0; } - return 0; -} - -int attribute_align_arg av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame) -{ - return av_buffersink_get_frame_flags(ctx, frame, 0); } -int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags) +static int get_frame_internal(AVFilterContext *ctx, AVFrame *frame, int flags, int samples) { BufferSinkContext *buf = ctx->priv; AVFilterLink *inlink = ctx->inputs[0]; - int peek_in_framequeue = 0, ret; - int64_t frame_count; + int ret; AVFrame *cur_frame; - /* no picref available, fetch it from the filterchain */ - while (!av_fifo_size(buf->fifo)) { - /* if peek_in_framequeue is true later, then ff_request_frame() and - the ff_filter_graph_run_once() loop will take a frame from it and - move it to the internal fifo, ending the global loop */ - av_assert0(!peek_in_framequeue); - if (inlink->status_out) + if (buf->peeked_frame) + return return_or_keep_frame(buf, frame, buf->peeked_frame, flags); + + while (1) { + ret = samples ? ff_link_consume_samples(inlink, samples, samples, &cur_frame) : + ff_link_consume_frame(inlink, &cur_frame); + if (ret < 0) { + return ret; + } else if (ret) { + /* TODO return the frame instead of copying it */ + return return_or_keep_frame(buf, frame, cur_frame, flags); + } else if (inlink->status_out || ff_link_acknowledge_status(inlink)) { return inlink->status_out; - peek_in_framequeue = ff_framequeue_queued_frames(&inlink->fifo) && - ff_framequeue_queued_samples(&inlink->fifo) >= inlink->min_samples; - if ((flags & AV_BUFFERSINK_FLAG_NO_REQUEST) && !peek_in_framequeue) + } else if ((flags & AV_BUFFERSINK_FLAG_NO_REQUEST)) { return AVERROR(EAGAIN); - if ((ret = ff_request_frame(inlink)) < 0) - return ret; - frame_count = inlink->frame_count_out; - while (frame_count == inlink->frame_count_out) { + } else if (inlink->frame_wanted_out) { ret = ff_filter_graph_run_once(ctx->graph); if (ret < 0) return ret; + } else { + ff_link_set_frame_wanted(inlink); } } - - if (flags & AV_BUFFERSINK_FLAG_PEEK) { - cur_frame = *((AVFrame **)av_fifo_peek2(buf->fifo, 0)); - if ((ret = av_frame_ref(frame, cur_frame)) < 0) - return ret; - } else { - av_fifo_generic_read(buf->fifo, &cur_frame, sizeof(cur_frame), NULL); - av_frame_move_ref(frame, cur_frame); - av_frame_free(&cur_frame); - } - - return 0; } -static int read_from_fifo(AVFilterContext *ctx, AVFrame *frame, - int nb_samples) +int attribute_align_arg av_buffersink_get_frame_flags(AVFilterContext *ctx, AVFrame *frame, int flags) { - BufferSinkContext *s = ctx->priv; - AVFilterLink *link = ctx->inputs[0]; - AVFrame *tmp; - - if (!(tmp = ff_get_audio_buffer(link, nb_samples))) - return AVERROR(ENOMEM); - av_audio_fifo_read(s->audio_fifo, (void**)tmp->extended_data, nb_samples); - - tmp->pts = s->next_pts; - if (s->next_pts != AV_NOPTS_VALUE) - s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate}, - link->time_base); - - av_frame_move_ref(frame, tmp); - av_frame_free(&tmp); - - return 0; + return get_frame_internal(ctx, frame, flags, ctx->inputs[0]->min_samples); } int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx, AVFrame *frame, int nb_samples) { - BufferSinkContext *s = ctx->priv; - AVFilterLink *link = ctx->inputs[0]; - AVFrame *cur_frame; - int ret = 0; - - if (!s->audio_fifo) { - int nb_channels = link->channels; - if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples))) - return AVERROR(ENOMEM); - } - - while (ret >= 0) { - if (av_audio_fifo_size(s->audio_fifo) >= nb_samples) - return read_from_fifo(ctx, frame, nb_samples); - - if (!(cur_frame = av_frame_alloc())) - return AVERROR(ENOMEM); - ret = av_buffersink_get_frame_flags(ctx, cur_frame, 0); - if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo)) { - av_frame_free(&cur_frame); - return read_from_fifo(ctx, frame, av_audio_fifo_size(s->audio_fifo)); - } else if (ret < 0) { - av_frame_free(&cur_frame); - return ret; - } - - if (cur_frame->pts != AV_NOPTS_VALUE) { - s->next_pts = cur_frame->pts - - av_rescale_q(av_audio_fifo_size(s->audio_fifo), - (AVRational){ 1, link->sample_rate }, - link->time_base); - } - - ret = av_audio_fifo_write(s->audio_fifo, (void**)cur_frame->extended_data, - cur_frame->nb_samples); - av_frame_free(&cur_frame); - } - - return ret; + return get_frame_internal(ctx, frame, 0, nb_samples); } AVBufferSinkParams *av_buffersink_params_alloc(void) @@ -261,13 +149,24 @@ static av_cold int common_init(AVFilterContext *ctx) { BufferSinkContext *buf = ctx->priv; - buf->fifo = av_fifo_alloc_array(FIFO_INIT_SIZE, FIFO_INIT_ELEMENT_SIZE); - if (!buf->fifo) { - av_log(ctx, AV_LOG_ERROR, "Failed to allocate fifo\n"); - return AVERROR(ENOMEM); - } buf->warning_limit = 100; - buf->next_pts = AV_NOPTS_VALUE; + return 0; +} + +static int activate(AVFilterContext *ctx) +{ + BufferSinkContext *buf = ctx->priv; + + if (buf->warning_limit && + ff_framequeue_queued_frames(&ctx->inputs[0]->fifo) >= buf->warning_limit) { + av_log(ctx, AV_LOG_WARNING, + "%d buffers queued in %s, something may be wrong.\n", + buf->warning_limit, + (char *)av_x_if_null(ctx->name, ctx->filter->name)); + buf->warning_limit *= 10; + } + + /* The frame is queued, the rest is up to get_frame_internal */ return 0; } @@ -281,7 +180,7 @@ void av_buffersink_set_frame_size(AVFilterContext *ctx, unsigned frame_size) #define MAKE_AVFILTERLINK_ACCESSOR(type, field) \ type av_buffersink_get_##field(const AVFilterContext *ctx) { \ - av_assert0(ctx->filter->uninit == uninit); \ + av_assert0(ctx->filter->activate == activate); \ return ctx->inputs[0]->field; \ } @@ -437,7 +336,6 @@ static const AVFilterPad avfilter_vsink_buffer_inputs[] = { { .name = "default", .type = AVMEDIA_TYPE_VIDEO, - .filter_frame = filter_frame, }, { NULL } }; @@ -448,9 +346,9 @@ AVFilter ff_vsink_buffer = { .priv_size = sizeof(BufferSinkContext), .priv_class = &buffersink_class, .init_opaque = vsink_init, - .uninit = uninit, .query_formats = vsink_query_formats, + .activate = activate, .inputs = avfilter_vsink_buffer_inputs, .outputs = NULL, }; @@ -459,7 +357,6 @@ static const AVFilterPad avfilter_asink_abuffer_inputs[] = { { .name = "default", .type = AVMEDIA_TYPE_AUDIO, - .filter_frame = filter_frame, }, { NULL } }; @@ -470,9 +367,9 @@ AVFilter ff_asink_abuffer = { .priv_class = &abuffersink_class, .priv_size = sizeof(BufferSinkContext), .init_opaque = asink_init, - .uninit = uninit, .query_formats = asink_query_formats, + .activate = activate, .inputs = avfilter_asink_abuffer_inputs, .outputs = NULL, };