From patchwork Mon Dec 13 15:20:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32442 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp5577459iog; Mon, 13 Dec 2021 07:26:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJxgpQT71BO3o5lwXkv6rh3b8mIWikpZtMnnCVWwERLcpNUoWMA9zX7OPR1XUbLCP273a2CG X-Received: by 2002:a17:906:dfe4:: with SMTP id lc4mr46225988ejc.125.1639409190087; Mon, 13 Dec 2021 07:26:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1639409190; cv=none; d=google.com; s=arc-20160816; b=M7+yk7G5XGj9wnpLTbyCWSBmjdAUvBJpux6e69hzK7SkAabM7RApZOjV/vJXxvhx8a YiHpsMRwScC+0S8DNM+QSG6rcpTrlm9P3WuoXqwoxKrjZiAQ9nm56+KAKBTij0aoz2Dz nyMMZ2jnI6SYMTPv/8/HI/iDeLN7d6YJsOyQCy9koUKs+8ZPvO7kjL8wpvfDKwD0uw3v MDB6Mo3br8phoEO7sJCjTsoRMuLUAgtywn0DzeoEZiivGMLL6eN8ffipiq5VaTCHa5jL raojhK5lo/2MRS0JAYSwYjTOLqn56oSYxhi8Itwog4/JvnBEEkU98dRTZ7ThmHuYCmSu ozGw== 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:message-id :date:to:from:delivered-to; bh=e13l7emmgtxEOfdv1O3mepues7Q7EZS5UF5gtgwGCTU=; b=xHCpox8e8YF8eobu0rMzpAPhbRZpM/Q3BhWZGkohdyLTDCS+suuZF+zo1Prdp4em8l ER9CTN7r/hbgHhckyxZUwUEtvllJ0rwBVKfIlcYqJqqKtLB2qSqUNmEUphQW4JORXaX+ Wqr/NRnGxGZzDn3i9m+KlLTy+GkFbj6It9Yne7vDZqhPLeU12mThwKVb0/QIyOS2YSWy MorEiYS5GovOIGDDKsZcN2Qv+cqvO2GC+Ppa4w+/y6Yfs3mPHKoMSnRi05lsx93gXEPy MwlBwA1wGAHEFswPi4H8MwIgRgzJM5WqKZuIfPwosFskH5V3vIbmxY8lp1w280hBf+EX qX/A== ARC-Authentication-Results: i=1; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z1si23716834edc.600.2021.12.13.07.26.29; Mon, 13 Dec 2021 07:26:30 -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 9A7DB68B04A; Mon, 13 Dec 2021 17:22:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id AAE4768AFC2 for ; Mon, 13 Dec 2021 17:22:19 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 6C62524062A for ; Mon, 13 Dec 2021 16:22:17 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 72DuX7DqOJw9 for ; Mon, 13 Dec 2021 16:22:16 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 44F0E24068D for ; Mon, 13 Dec 2021 16:22:08 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 68FCB3A0C0A; Mon, 13 Dec 2021 16:22:07 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 13 Dec 2021 16:20:41 +0100 Message-Id: <20211213152042.5900-23-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211213152042.5900-1-anton@khirnov.net> References: <20211213152042.5900-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 23/24] ffmpeg: simplify the use of OutputStream.frame_number X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: SBDTYCHlqF7w It features in limiting the number of output frames (-frames option) and currently can be incremented from two places: - for video encoding (not streamcopy), in do_video_out() after each successful avcodec_send_frame() call - for all other cases, in of_submit_packet() Not only is this inconsistent and confusing, but also the special treatment for video encoding is redundant, since AVCodecContext.frame_count stores the exact same value (number of successful avcodec_send_frame()) calls. Replace the use of OutputStream.frame_count in do_video_out() with AVCodecContext.frame_count. Modify OutputStream.frame_count in the same way for all streams. --- fftools/ffmpeg.c | 9 ++++----- fftools/ffmpeg_mux.c | 19 +++++++------------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 538601e440..aff3cc350e 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1031,7 +1031,7 @@ static void do_video_out(OutputFile *of, switch (ost->vsync_method) { case VSYNC_VSCFR: - if (ost->frame_number == 0 && delta0 >= 0.5) { + if (enc->frame_number == 0 && delta0 >= 0.5) { av_log(NULL, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0)); delta = duration; delta0 = 0; @@ -1039,7 +1039,7 @@ static void do_video_out(OutputFile *of, } case VSYNC_CFR: // FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c - if (frame_drop_threshold && delta < frame_drop_threshold && ost->frame_number) { + if (frame_drop_threshold && delta < frame_drop_threshold && enc->frame_number) { nb_frames = 0; } else if (delta < -1.1) nb_frames = 0; @@ -1069,7 +1069,7 @@ static void do_video_out(OutputFile *of, * But there may be reordering, so we can't throw away frames on encoder * flush, we need to limit them here, before they go into encoder. */ - nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number); + nb_frames = FFMIN(nb_frames, ost->max_frames - enc->frame_number); nb0_frames = FFMIN(nb0_frames, nb_frames); memmove(ost->last_nb0_frames + 1, @@ -1081,7 +1081,7 @@ static void do_video_out(OutputFile *of, nb_frames_drop++; av_log(NULL, AV_LOG_VERBOSE, "*** dropping frame %d from stream %d at ts %"PRId64"\n", - ost->frame_number, ost->st->index, ost->last_frame->pts); + enc->frame_number, ost->st->index, ost->last_frame->pts); } if (nb_frames > (nb0_frames && ost->last_dropped) + (nb_frames > nb0_frames)) { if (nb_frames > dts_error_threshold * 30) { @@ -1222,7 +1222,6 @@ static void do_video_out(OutputFile *of, } } ost->sync_opts++; - ost->frame_number++; if (vstats_filename && frame_size) do_video_stats(ost, frame_size); diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c index e97ec8ab93..c3666fbba1 100644 --- a/fftools/ffmpeg_mux.c +++ b/fftools/ffmpeg_mux.c @@ -200,23 +200,18 @@ static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt) void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost) { - AVStream *st = ost->st; int ret; /* - * Audio encoders may split the packets -- #frames in != #packets out. - * But there is no reordering, so we can limit the number of output packets - * by simply dropping them here. - * Counting encoded video frames needs to be done separately because of - * reordering, see do_video_out(). + * Limit the number of output frames by dropping any excess packets. + * This should not trigger for video encoding, which due to possible + * reordering needs to be treated specially - see do_video_out(). */ - if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)) { - if (ost->frame_number >= ost->max_frames) { - av_packet_unref(pkt); - return; - } - ost->frame_number++; + if (ost->frame_number >= ost->max_frames) { + av_packet_unref(pkt); + return; } + ost->frame_number++; if (of->mux->header_written) { write_packet(of, ost, pkt);