From patchwork Thu Dec 29 14:34:03 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas George X-Patchwork-Id: 1980 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.89.21 with SMTP id n21csp2440136vsb; Thu, 29 Dec 2016 06:37:01 -0800 (PST) X-Received: by 10.194.175.69 with SMTP id by5mr34767001wjc.201.1483022220983; Thu, 29 Dec 2016 06:37:00 -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 gy5si58111293wjc.285.2016.12.29.06.37.00; Thu, 29 Dec 2016 06:37:00 -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 76428689D5C; Thu, 29 Dec 2016 16:34:18 +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 B6E39689AD9 for ; Thu, 29 Dec 2016 16:34:12 +0200 (EET) Received: from phare.normalesup.org (phare.normalesup.org [129.199.129.80]) by nef2.ens.fr (8.13.6/1.01.28121999) with ESMTP id uBTEYFdL080469 for ; Thu, 29 Dec 2016 15:34:15 +0100 (CET) Received: by phare.normalesup.org (Postfix, from userid 1001) id AE899E00F9; Thu, 29 Dec 2016 15:34:15 +0100 (CET) From: Nicolas George To: ffmpeg-devel@ffmpeg.org Date: Thu, 29 Dec 2016 15:34:03 +0100 Message-Id: <20161229143403.2851-17-george@nsup.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20161229143403.2851-1-george@nsup.org> References: <20161229143403.2851-1-george@nsup.org> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (nef2.ens.fr [129.199.96.32]); Thu, 29 Dec 2016 15:34:16 +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 | 214 +++++++++++++---------------------------------- 1 file changed, 56 insertions(+), 158 deletions(-) Minor changes to accomodate the previous changes. Since diff is not very smart about it, better read the code than the patch. diff --git a/libavfilter/buffersink.c b/libavfilter/buffersink.c index f2d8872622..6b35b54458 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 @@ -36,11 +34,11 @@ #include "avfilter.h" #include "avfilterlink.h" #include "buffersink.h" +#include "filters.h" #include "internal.h" typedef struct BufferSinkContext { const AVClass *class; - AVFifoBuffer *fifo; ///< FIFO buffer of video frame references unsigned warning_limit; /* only used for video */ @@ -58,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 status, 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) - 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) - return AVERROR(EAGAIN); - if ((ret = ff_request_frame(inlink)) < 0) + if (buf->peeked_frame) + return return_or_keep_frame(buf, frame, buf->peeked_frame, flags); + + while (1) { + ret = samples ? ff_inlink_consume_samples(inlink, samples, samples, &cur_frame) : + ff_inlink_consume_frame(inlink, &cur_frame); + if (ret < 0) { return ret; - frame_count = inlink->frame_count_out; - while (frame_count == inlink->frame_count_out) { + } else if (ret) { + /* TODO return the frame instead of copying it */ + return return_or_keep_frame(buf, frame, cur_frame, flags); + } else if (ff_inlink_acknowledge_status(inlink, &status)) { + return status; + } else if ((flags & AV_BUFFERSINK_FLAG_NO_REQUEST)) { + return AVERROR(EAGAIN); + } else if (inlink->frame_wanted_out) { ret = ff_filter_graph_run_once(ctx->graph); if (ret < 0) return ret; + } else { + ff_inlink_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) @@ -260,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; } @@ -280,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; \ } @@ -436,7 +336,6 @@ static const AVFilterPad avfilter_vsink_buffer_inputs[] = { { .name = "default", .type = AVMEDIA_TYPE_VIDEO, - .filter_frame = filter_frame, }, { NULL } }; @@ -447,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, }; @@ -458,7 +357,6 @@ static const AVFilterPad avfilter_asink_abuffer_inputs[] = { { .name = "default", .type = AVMEDIA_TYPE_AUDIO, - .filter_frame = filter_frame, }, { NULL } }; @@ -469,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, };