From patchwork Fri Feb 16 20:02:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Calvin Walton X-Patchwork-Id: 7613 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.150.1 with SMTP id c1csp1110416jai; Fri, 16 Feb 2018 12:02:29 -0800 (PST) X-Google-Smtp-Source: AH8x225Aa6HOk8Y75O7SNCWby3oIICbbK4wPy8W5PxXvfyQpnzD9BZZfk4rgzOB1zTutMyL09Ojn X-Received: by 10.223.128.14 with SMTP id 14mr6989799wrk.94.1518811349872; Fri, 16 Feb 2018 12:02:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518811349; cv=none; d=google.com; s=arc-20160816; b=TdPTstxRnDVpgAVAwjvtnT/8ExUjU/vPPWAzVdJhPUt291Iu79RxUewWTPErJg7akO T5pG7lWPo94vt1KfHsZc36egrZMNab/8P7rAecg7i00IOJ4LVh8hH8EKFlybk7z8N7mQ Zk5UJ/Huozd35hv+l1++OgkLT7Kl0zIZU2OzhWO/OTA8CJOaWxe50yef18yb+SUeHQav N8apUtpMpgLIEDNRIwW5fqQuEHGAGmY4zuVliYOm0xBFklpgzqLFaa3eAVdfrBWCLzPi CuaHTo+OYQu5+CwU/7YCH7waiI3EcRdF+c8vshfkGEpbsKSijXNktihBtrEZoNBwgGYZ 2b5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:date:to:from :message-id:dkim-signature:delivered-to:arc-authentication-results; bh=pc0NdInOdB3LJyiqwfl9kOHBwLjU8zi/a6s9/xdq6to=; b=V8DwErC+VREilGhQ2jVCpcWUj7lHXxuHVrBVp+PyrkTZ0s9eTlkXTYTwXgnbWpN2l8 1ldFj70MTNwMgtB0Vv7FuPOfSiDe64Z+Mq3fKF9m+/ezdLFgXmK236m2F8rOjfkhQx9w r4wRHz4y1kPsPH1Q/jIkTntyfEkk8942LtKqgMzhsyJaTTC6PNsEWXTZFRqYwLT9FTDS zHr/49/AjNydF2ViBbk7Rr+MKZIsnKI4gP/0jc62d9RzHdMJTiaHZijyMxa1hPuygyuY BgquYifC2Jx9tnfYh/Vz6h9zLcJOW1Fy35GscMPW78b1ZTN8Er5VYexY/KWZrOt5oCG2 L2+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@kepstin.ca header.s=google header.b=o10gTQoG; 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; dmarc=fail (p=NONE sp=REJECT dis=NONE) header.from=kepstin.ca Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id k3si10090218wmi.211.2018.02.16.12.02.29; Fri, 16 Feb 2018 12:02:29 -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; dkim=neutral (body hash did not verify) header.i=@kepstin.ca header.s=google header.b=o10gTQoG; 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; dmarc=fail (p=NONE sp=REJECT dis=NONE) header.from=kepstin.ca Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2EBDB68A205; Fri, 16 Feb 2018 22:02:15 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io0-f179.google.com (mail-io0-f179.google.com [209.85.223.179]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 21A9C689E46 for ; Fri, 16 Feb 2018 22:02:08 +0200 (EET) Received: by mail-io0-f179.google.com with SMTP id z6so5297062iob.11 for ; Fri, 16 Feb 2018 12:02:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kepstin.ca; s=google; h=message-id:subject:from:to:date:in-reply-to:references:mime-version :content-transfer-encoding; bh=qbmJwBBxBwPEW6PKtc9eIu7AZrzP9d9o+PGFeFKJV2s=; b=o10gTQoG6L8y0UwZNWtRD7Ek3K7cwjX+Pgj02rTRv9XiyPYL0/a9bA0DL+HgzpXFmi rkJ/GEMsc9Gz1IA8YmGKNg+BOgRWuBN3vNyi2h84aTIeK4oqP0js8rx3mRkyV+wf9BYO C1E3aVxV/2/CgGni0Qtq2slQ8PgRbo13FcGW0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:mime-version:content-transfer-encoding; bh=qbmJwBBxBwPEW6PKtc9eIu7AZrzP9d9o+PGFeFKJV2s=; b=alxbFUg0iawp2FhoDJMNpAEmoeBhEn8anh62Qte5H9JUAoS3sLAlv+NrgDYev9caaa F+uJMoeNKdXsLABBENVEQBBYfNq/TOjdrpHsl3YJopYUDVs5SB1J3lp2RPQ1KgFwFwe6 iUdIHOkCcImz9/FWnPKdY+EqDxiuJ8iwz354Ft2YhJvqXP6urUbAU+i7M0LDg9geP6X6 IXUUEte2I8phMfvYBTUSLflVOIvLS6boT02tgmTokD9cC33AzJILG//9wvQfJmWUqtZw z1LIicMy7yVZSSf2X6lvO85fS2qs16zi17QB04AFcelI5v35d6vNJprUaNsvF1vuUnYS SbfA== X-Gm-Message-State: APf1xPAOH0aV2Nv2CfbCLO3RNxGB87+9Iw+xnmVU9M6Vwjc4CdUPd2Xj eq5kQxEuC1kqRtFZX1RSuzTs39ptQuM= X-Received: by 10.107.18.70 with SMTP id a67mr9305572ioj.190.1518811339069; Fri, 16 Feb 2018 12:02:19 -0800 (PST) Received: from ?IPv6:2600:16f6:ca87:c180:8398:d8f:5779:6303? ([2600:16f6:ca87:c180:8398:d8f:5779:6303]) by smtp.gmail.com with ESMTPSA id 72sm13102291ioe.60.2018.02.16.12.02.18 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 16 Feb 2018 12:02:18 -0800 (PST) Message-ID: <1518811337.23857.6.camel@kepstin.ca> From: Calvin Walton To: FFmpeg development discussions and patches Date: Fri, 16 Feb 2018 15:02:17 -0500 In-Reply-To: References: X-Mailer: Evolution 3.26.5 Mime-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] libavfilter/vf_fps: Rewrite using activate callback 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" The old version of the filter had a problem where it would queue up all of the duplicate frames required to fill a timestamp gap in a single call to filter_frame. In problematic files - I've hit this in webcam streams with large gaps due to network issues - this will queue up a potentially huge number of frames. (I've seen it trigger the Linux OOM-killer on particularly large pts gaps.) This revised version of the filter using the activate callback will generate at most 1 frame each time it is called, and respects output links having the frame_wanted_out set to a negative number to indicate that they don't want frames. TODO: This is still a work in progress. It may have different behaviour in some cases from the old fps filter. I have not yet implemented the "eof_action" option, since I haven't figured out what it's supposed to do. --- libavfilter/vf_fps.c | 398 +++++++++++++++++++++++++++++---------------------- 1 file changed, 230 insertions(+), 168 deletions(-) diff --git a/libavfilter/vf_fps.c b/libavfilter/vf_fps.c index dbafd2c35a..16e432db0b 100644 --- a/libavfilter/vf_fps.c +++ b/libavfilter/vf_fps.c @@ -2,6 +2,7 @@ * Copyright 2007 Bobby Bingham * Copyright 2012 Robert Nagy * Copyright 2012 Anton Khirnov + * Copyright 2018 Calvin Walton * * This file is part of FFmpeg. * @@ -28,17 +29,12 @@ #include #include -#include "libavutil/common.h" -#include "libavutil/fifo.h" +#include "libavutil/avassert.h" #include "libavutil/mathematics.h" #include "libavutil/opt.h" -#include "libavutil/parseutils.h" - -#define FF_INTERNAL_FIELDS 1 -#include "framequeue.h" #include "avfilter.h" +#include "filters.h" #include "internal.h" -#include "video.h" enum EOFAction { EOF_ACTION_ROUND, @@ -49,17 +45,24 @@ enum EOFAction { typedef struct FPSContext { const AVClass *class; - AVFifoBuffer *fifo; ///< store frames until we get two successive timestamps - - /* timestamps in input timebase */ - int64_t first_pts; ///< pts of the first frame that arrived on this filter - + /* Options */ double start_time; ///< pts, in seconds, of the expected first frame - AVRational framerate; ///< target framerate int rounding; ///< AVRounding method for timestamps int eof_action; ///< action performed for last frame in FIFO + /* Set during outlink configuration */ + int64_t start_pts; ///< pts of the expected first frame + + /* Runtime state */ + int status; ///< buffered input status + int64_t status_pts; ///< buffered input status timestamp + + AVFrame *frames[2]; ///< buffered frames + int frames_count; ///< number of buffered frames + + int64_t next_pts; ///< pts of the next frame to output + /* statistics */ int frames_in; ///< number of frames on input int frames_out; ///< number of frames on output @@ -91,31 +94,46 @@ static av_cold int init(AVFilterContext *ctx) { FPSContext *s = ctx->priv; - if (!(s->fifo = av_fifo_alloc_array(2, sizeof(AVFrame*)))) - return AVERROR(ENOMEM); + /* Pass INT64_MIN/MAX through unchanged to avoid special cases for AV_NOPTS_VALUE */ + s->rounding = s->rounding | AV_ROUND_PASS_MINMAX; - s->first_pts = AV_NOPTS_VALUE; + s->start_pts = AV_NOPTS_VALUE; + s->status_pts = AV_NOPTS_VALUE; + s->next_pts = AV_NOPTS_VALUE; av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den); return 0; } -static void flush_fifo(AVFifoBuffer *fifo) +/* Remove the first frame from the buffer, returning it */ +static AVFrame *shift_frame(FPSContext *s) { - while (av_fifo_size(fifo)) { - AVFrame *tmp; - av_fifo_generic_read(fifo, &tmp, sizeof(tmp), NULL); - av_frame_free(&tmp); - } + AVFrame *frame; + + /* Must only be called when there are frames in the buffer */ + av_assert1(s->frames_count > 0); + + frame = s->frames[0]; + s->frames[0] = s->frames[1]; + s->frames[1] = NULL; + s->frames_count--; + + return frame; } static av_cold void uninit(AVFilterContext *ctx) { FPSContext *s = ctx->priv; - if (s->fifo) { - s->drop += av_fifo_size(s->fifo) / sizeof(AVFrame*); - flush_fifo(s->fifo); - av_fifo_freep(&s->fifo); + + AVFrame *frame; + + /* Free any remaining buffered frames. This only happens if a downstream + * filter has asked us to stop, so don't count them as dropped. */ + av_log(ctx, AV_LOG_DEBUG, "Discarding %d buffered frame(s) at exit.\n", + s->frames_count); + while (s->frames_count > 0) { + frame = shift_frame(s); + av_frame_free(&frame); } av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, " @@ -128,194 +146,238 @@ static int config_props(AVFilterLink* link) link->time_base = av_inv_q(s->framerate); link->frame_rate= s->framerate; - link->w = link->src->inputs[0]->w; - link->h = link->src->inputs[0]->h; + + /* Convert the start time option to output timebase and save it */ + if (s->start_time != DBL_MAX && s->start_time != AV_NOPTS_VALUE) { + double first_pts = s->start_time * AV_TIME_BASE; + first_pts = FFMIN(FFMAX(first_pts, INT64_MIN), INT64_MAX); + s->start_pts = av_rescale_q_rnd(first_pts, AV_TIME_BASE_Q, + link->time_base, s->rounding); + } return 0; } -static int request_frame(AVFilterLink *outlink) +/* Read a frame from the input and save it in the buffer */ +static void read_frame(AVFilterContext *ctx, FPSContext *s, AVFilterLink *inlink, AVFilterLink *outlink) { - AVFilterContext *ctx = outlink->src; - FPSContext *s = ctx->priv; + AVFrame *frame; int ret; + int64_t in_pts; - ret = ff_request_frame(ctx->inputs[0]); - - /* flush the fifo */ - if (ret == AVERROR_EOF && av_fifo_size(s->fifo)) { - int i; - for (i = 0; av_fifo_size(s->fifo); i++) { - AVFrame *buf; - - av_fifo_generic_read(s->fifo, &buf, sizeof(buf), NULL); - if (av_fifo_size(s->fifo)) { - buf->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base, - outlink->time_base) + s->frames_out; - - if ((ret = ff_filter_frame(outlink, buf)) < 0) - return ret; - - s->frames_out++; - } else { - /* This is the last frame, we may have to duplicate it to match - * the last frame duration */ - int j; - int eof_rounding = (s->eof_action == EOF_ACTION_PASS) ? AV_ROUND_UP : s->rounding; - int delta = av_rescale_q_rnd(ctx->inputs[0]->current_pts - s->first_pts, - ctx->inputs[0]->time_base, - outlink->time_base, eof_rounding) - s->frames_out; - av_log(ctx, AV_LOG_DEBUG, "EOF frames_out:%d delta:%d\n", s->frames_out, delta); - /* if the delta is equal to 1, it means we just need to output - * the last frame. Greater than 1 means we will need duplicate - * delta-1 frames */ - if (delta > 0 ) { - for (j = 0; j < delta; j++) { - AVFrame *dup = av_frame_clone(buf); - - av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n"); - dup->pts = av_rescale_q(s->first_pts, ctx->inputs[0]->time_base, - outlink->time_base) + s->frames_out; - - if ((ret = ff_filter_frame(outlink, dup)) < 0) - return ret; - - s->frames_out++; - if (j > 0) s->dup++; - } - av_frame_free(&buf); - } else { - /* for delta less or equal to 0, we should drop the frame, - * otherwise, we will have one or more extra frames */ - av_frame_free(&buf); - s->drop++; - } - } - } - return 0; - } + /* Must only be called when we have buffer room available */ + av_assert1(s->frames_count < 2); - return ret; -} + ret = ff_inlink_consume_frame(inlink, &frame); + /* Caller must have run ff_inlink_check_available_frame first */ + av_assert1(ret); -static int write_to_fifo(AVFifoBuffer *fifo, AVFrame *buf) -{ - int ret; + /* Convert frame pts to output timebase */ + in_pts = frame->pts; + frame->pts = av_rescale_q_rnd(in_pts, inlink->time_base, + outlink->time_base, s->rounding); - if (!av_fifo_space(fifo) && - (ret = av_fifo_realloc2(fifo, 2*av_fifo_size(fifo)))) { - av_frame_free(&buf); - return ret; - } + av_log(ctx, AV_LOG_DEBUG, "Read frame with in pts %"PRId64", out pts %"PRId64"\n", + in_pts, frame->pts); - av_fifo_generic_write(fifo, &buf, sizeof(buf), NULL); - return 0; + s->frames[s->frames_count++] = frame; + s->frames_in++; } -static int filter_frame(AVFilterLink *inlink, AVFrame *buf) +/* Write a frame to the output */ +static int write_frame(AVFilterContext *ctx, FPSContext *s, AVFilterLink *outlink) { - AVFilterContext *ctx = inlink->dst; - FPSContext *s = ctx->priv; - AVFilterLink *outlink = ctx->outputs[0]; - int64_t delta; - int i, ret; + AVFrame *frame; + int ret; + int dup = 0; - s->frames_in++; - /* discard frames until we get the first timestamp */ - if (s->first_pts == AV_NOPTS_VALUE) { - if (buf->pts != AV_NOPTS_VALUE) { - ret = write_to_fifo(s->fifo, buf); - if (ret < 0) - return ret; - - if (s->start_time != DBL_MAX && s->start_time != AV_NOPTS_VALUE) { - double first_pts = s->start_time * AV_TIME_BASE; - first_pts = FFMIN(FFMAX(first_pts, INT64_MIN), INT64_MAX); - s->first_pts = av_rescale_q(first_pts, AV_TIME_BASE_Q, - inlink->time_base); - av_log(ctx, AV_LOG_VERBOSE, "Set first pts to (in:%"PRId64" out:%"PRId64")\n", - s->first_pts, av_rescale_q(first_pts, AV_TIME_BASE_Q, - outlink->time_base)); - } else { - s->first_pts = buf->pts; - } + av_assert1(s->frames_count == 2); + + /* We haven't yet determined the pts of the first frame */ + if (s->next_pts == AV_NOPTS_VALUE) { + if (s->frames[0]->pts != AV_NOPTS_VALUE) { + s->next_pts = s->frames[0]->pts; + av_log(ctx, AV_LOG_VERBOSE, "Set first pts to %"PRId64"\n", + s->next_pts); } else { av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no " - "timestamp.\n"); - av_frame_free(&buf); + "timestamp.\n"); + frame = shift_frame(s); + av_frame_free(&frame); s->drop++; + return AVERROR(EAGAIN); } - return 0; } - /* now wait for the next timestamp */ - if (buf->pts == AV_NOPTS_VALUE || av_fifo_size(s->fifo) <= 0) { - return write_to_fifo(s->fifo, buf); + /* If the second buffered frame is acceptable for the next frame, drop the first. */ + if (s->frames[1]->pts <= s->next_pts) { + frame = shift_frame(s); + if (frame->pts == s->next_pts) { + av_log(ctx, AV_LOG_DEBUG, "Dropping frame with pts %"PRId64"\n", frame->pts); + s->drop++; + } + av_frame_free(&frame); + return AVERROR(EAGAIN); + + /* The second buffered frame is in the future, output the first */ + } else { + frame = av_frame_clone(s->frames[0]); + frame->pts = s->next_pts++; + + /* If we're using a different pts than the frame's original, it's a dup */ + if (s->frames[0]->pts != frame->pts) { + av_log(ctx, AV_LOG_DEBUG, "Duplicating frame with pts %"PRId64" to pts %"PRId64"\n", + s->frames[0]->pts, frame->pts); + dup = 1; + } else { + av_log(ctx, AV_LOG_DEBUG, "Writing frame with pts %"PRId64"\n", frame->pts); + } + + ret = ff_filter_frame(outlink, frame); + if (ret >= 0) { + s->frames_out++; + s->dup += dup; + } + + return ret; } +} - /* number of output frames */ - delta = av_rescale_q_rnd(buf->pts - s->first_pts, inlink->time_base, - outlink->time_base, s->rounding) - s->frames_out ; +/* Write the last frame to the output */ +static int write_frame_eof(AVFilterContext *ctx, FPSContext *s, AVFilterLink *outlink) +{ + AVFrame *frame; + int ret; + int dup = 0; - if (delta < 1) { - /* drop everything buffered except the last */ - int drop = av_fifo_size(s->fifo)/sizeof(AVFrame*); + av_assert1(s->frames_count == 1); - av_log(ctx, AV_LOG_DEBUG, "Dropping %d frame(s).\n", drop); - s->drop += drop; + /* Either we've already hit the status timestamp or it's in the past, + * e.g. AV_NOPTS_VALUE, so drop the last frame. */ + if (s->status_pts <= s->next_pts) { + frame = shift_frame(s); + if (frame->pts == s->next_pts) { + av_log(ctx, AV_LOG_DEBUG, "Dropping frame with pts %"PRId64"\n", frame->pts); + s->drop++; + } + av_frame_free(&frame); + return AVERROR(EAGAIN); + + /* We haven't yet reached the timestamp of the status (EOF) */ + } else { + frame = av_frame_clone(s->frames[0]); + frame->pts = s->next_pts++; + + /* If we're using a different pts than the frame's original, it's a dup */ + if (s->frames[0]->pts != frame->pts) { + av_log(ctx, AV_LOG_DEBUG, "Duplicating frame with pts %"PRId64" to pts %"PRId64"\n", + s->frames[0]->pts, frame->pts); + dup = 1; + } else { + av_log(ctx, AV_LOG_DEBUG, "Writing frame with pts %"PRId64"\n", frame->pts); + } - flush_fifo(s->fifo); - ret = write_to_fifo(s->fifo, buf); + ret = ff_filter_frame(outlink, frame); + if (ret >= 0) { + s->frames_out++; + s->dup += dup; + } return ret; } +} - /* can output >= 1 frames */ - for (i = 0; i < delta; i++) { - AVFrame *buf_out; - av_fifo_generic_read(s->fifo, &buf_out, sizeof(buf_out), NULL); +static void update_eof_pts(AVFilterContext *ctx, FPSContext *s, AVFilterLink *inlink, AVFilterLink *outlink) +{ + /* Convert status_pts to outlink timebase */ + int eof_rounding = (s->eof_action == EOF_ACTION_PASS) ? AV_ROUND_UP : s->rounding; + s->status_pts = av_rescale_q_rnd(s->status_pts, inlink->time_base, + outlink->time_base, eof_rounding); - /* duplicate the frame if needed */ - if (!av_fifo_size(s->fifo) && i < delta - 1) { - AVFrame *dup = av_frame_clone(buf_out); + av_log(ctx, AV_LOG_DEBUG, "EOF is at pts %"PRId64"\n", s->status_pts); +} - av_log(ctx, AV_LOG_DEBUG, "Duplicating frame.\n"); - if (dup) - ret = write_to_fifo(s->fifo, dup); - else - ret = AVERROR(ENOMEM); +static int activate(AVFilterContext *ctx) +{ + FPSContext *s = ctx->priv; + AVFilterLink *inlink = ctx->inputs[0]; + AVFilterLink *outlink = ctx->outputs[0]; - if (ret < 0) { - av_frame_free(&buf_out); - av_frame_free(&buf); - return ret; - } + int ret; - s->dup++; + FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); + + /* No buffered status: normal operation */ + if (!s->status) { + + /* Read available input frames if we have room */ + while (s->frames_count < 2 && + ff_inlink_check_available_frame(inlink)) { + read_frame(ctx, s, inlink, outlink); } - buf_out->pts = av_rescale_q(s->first_pts, inlink->time_base, - outlink->time_base) + s->frames_out; + /* We do not yet have enough frames to produce output */ + if (s->frames_count < 2) { - if ((ret = ff_filter_frame(outlink, buf_out)) < 0) { - av_frame_free(&buf); - return ret; + ret = ff_inlink_acknowledge_status(inlink, &s->status, &s->status_pts); + if (!ret) { + /* If someone wants us to output, we'd better ask for more input */ + if (ff_outlink_frame_wanted(outlink) > 0) + ff_inlink_request_frame(inlink); + + return 0; + } + if (ret > 0) { + /* Need to calculate the end timestamp in the output timebase */ + update_eof_pts(ctx, s, inlink, outlink); + } } - s->frames_out++; + /* Buffered frames are available, so generate an output frame */ + if (s->frames_count == 2 && ff_outlink_frame_wanted(outlink) >= 0) { + ret = write_frame(ctx, s, outlink); + /* Couldn't generate a frame: e.g. a frame was dropped */ + if (ret == AVERROR(EAGAIN)) { + /* Schedule us to perform another step */ + ff_filter_set_ready(ctx, 100); + return 0; + } + return ret; + } } - flush_fifo(s->fifo); - ret = write_to_fifo(s->fifo, buf); + /* There is a buffered status, flush frames and forward it */ + if (s->status) { + + /* There are still buffered frames, so flush a frame */ + if (s->frames_count > 0) { + if (s->frames_count == 2) + ret = write_frame(ctx, s, outlink); + else + ret = write_frame_eof(ctx, s, outlink); + + /* Couldn't generate a frame: e.g. a frame was dropped */ + if (ret == AVERROR(EAGAIN)) { + /* Schedule us to perform another step */ + ff_filter_set_ready(ctx, 100); + return 0; + } + } + + /* No frames left, so forward the status */ + if (s->frames_count == 0) { + ff_outlink_set_status(outlink, s->status, s->next_pts); + return 0; + } + } - return ret; + return FFERROR_NOT_READY; } static const AVFilterPad avfilter_vf_fps_inputs[] = { { .name = "default", .type = AVMEDIA_TYPE_VIDEO, - .filter_frame = filter_frame, }, { NULL } }; @@ -324,8 +386,7 @@ static const AVFilterPad avfilter_vf_fps_outputs[] = { { .name = "default", .type = AVMEDIA_TYPE_VIDEO, - .request_frame = request_frame, - .config_props = config_props + .config_props = config_props, }, { NULL } }; @@ -337,6 +398,7 @@ AVFilter ff_vf_fps = { .uninit = uninit, .priv_size = sizeof(FPSContext), .priv_class = &fps_class, + .activate = activate, .inputs = avfilter_vf_fps_inputs, .outputs = avfilter_vf_fps_outputs, };