From patchwork Tue Oct 3 15:35:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 44125 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1204:b0:15d:8365:d4b8 with SMTP id v4csp2141112pzf; Tue, 3 Oct 2023 08:37:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGlc5H0ubm0eqrp5iJ8GyCKrZ8+fCxJxnv1Xhz9BBnYIHBRKjh0B5SjO3gYMOQzz0cEi0/g X-Received: by 2002:a17:906:32db:b0:9ad:99a8:7c53 with SMTP id k27-20020a17090632db00b009ad99a87c53mr13435970ejk.60.1696347439649; Tue, 03 Oct 2023 08:37:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696347439; cv=none; d=google.com; s=arc-20160816; b=XZLVE4x4OEYTNrWM/ENeqXT/8S84oXCo3TPGYaJYqjZI4un8O3ywPpn4PZlSlZok2m l6ppEHpRGgyASxxOId7vxdWw5gTn+ftvNSn37W/zQLj2ShRscnM72ozKXqy1XwZhoyGe Ttg9z217jtwIaA1PSkHYUOP4i6rplKL/EGG8HCS/DqJUN7/CUAUSX0p3qCxWilwLcP2N eNnkPH8Xo2cXfY5vETH7Bu/rh7TZP2uczyehdj02lPas29R2AHTY3eAKEtb/iKtoka1o oraYLMtkz/T7tS4Js5siO0t2GAHxjZXfReG9f4iO19QyjJZTnk8wnGfXmX1yw9vbsS6i HQbA== 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=Y8XEjt/mvE404Mkhm+9xd4BsB56vptdPMj97HxgkaSM=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=hdh256/jY6xV9U3+7KbSWgNL7r6QF7k4s6uVsnhc/llNekBd80tw2PdPyLR8GgzVB4 yLSYz5+OpFvJEuT9AHqu5xfxzy+zFqMdEc5nUs5TCyOaNSFM9scdg8pgiw4JufNZOSXj b1ks1lpI4kOViDUN3VUb7lvAxsWZLSZyMLAuJANvdPIlk8zTJfR/eOJ2+Ty0RWySjuPQ J+0TCF69HDkrySXvqYD6qJLETyiQiHVgRO8RwHITGUbKNEGTCu+M184c88ALjUmvfi7P Tq+6skxwM0BgPK7R3O1+d3/3AUOfJ/OAxQqRpqwUkZm/ph43vCxHRg+x8PPsFQhN2W/+ zYtA== 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 ay13-20020a170906d28d00b0099d5e9aedd7si768922ejb.482.2023.10.03.08.37.19; Tue, 03 Oct 2023 08:37:19 -0700 (PDT) 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 93B4468CD62; Tue, 3 Oct 2023 18:35:58 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DA92B68CCFA for ; Tue, 3 Oct 2023 18:35:46 +0300 (EEST) Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id 6C5F25286 for ; Tue, 3 Oct 2023 17:35:42 +0200 (CEST) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id rkInHnX91OiL for ; Tue, 3 Oct 2023 17:35:42 +0200 (CEST) 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 mail1.khirnov.net (Postfix) with ESMTPS id 366A7527E for ; Tue, 3 Oct 2023 17:35:40 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 54E203A0785 for ; Tue, 3 Oct 2023 17:35:33 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 3 Oct 2023 17:35:23 +0200 Message-Id: <20231003153526.19228-9-anton@khirnov.net> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231003153526.19228-1-anton@khirnov.net> References: <20231003153526.19228-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 09/12] fftools/ffmpeg_enc: move framerate conversion state into a separate struct 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: dnfZtNsVV3UC Makes it more clear what state is specific to framerate conversion, which will be useful in the following commit. --- fftools/ffmpeg_enc.c | 65 ++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index 29d0581cc7..f7ac054e25 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -41,18 +41,24 @@ #include "libavformat/avformat.h" -struct Encoder { - /* predicted pts of the next frame to be encoded */ - int64_t next_pts; - +typedef struct FPSConvContext { AVFrame *last_frame; /* number of frames emitted by the video-encoding sync code */ - int64_t vsync_frame_number; + int64_t frame_number; /* history of nb_frames_prev, i.e. the number of times the * previous frame was duplicated by vsync code in recent * do_video_out() calls */ int64_t frames_prev_hist[3]; + uint64_t dup_warning; +} FPSConvContext; + +struct Encoder { + /* predicted pts of the next frame to be encoded */ + int64_t next_pts; + + FPSConvContext fps; + AVFrame *sq_frame; // packet for receiving encoded output @@ -64,8 +70,6 @@ struct Encoder { // number of packets received from the encoder uint64_t packets_encoded; - uint64_t dup_warning; - int opened; }; @@ -76,7 +80,7 @@ void enc_free(Encoder **penc) if (!enc) return; - av_frame_free(&enc->last_frame); + av_frame_free(&enc->fps.last_frame); av_frame_free(&enc->sq_frame); av_packet_free(&enc->pkt); @@ -95,17 +99,17 @@ int enc_alloc(Encoder **penc, const AVCodec *codec) return AVERROR(ENOMEM); if (codec->type == AVMEDIA_TYPE_VIDEO) { - enc->last_frame = av_frame_alloc(); - if (!enc->last_frame) + enc->fps.last_frame = av_frame_alloc(); + if (!enc->fps.last_frame) goto fail; + + enc->fps.dup_warning = 1000; } enc->pkt = av_packet_alloc(); if (!enc->pkt) goto fail; - enc->dup_warning = 1000; - *penc = enc; return 0; @@ -966,13 +970,14 @@ static void video_sync_process(OutputFile *of, OutputStream *ost, AVFrame *frame int64_t *nb_frames, int64_t *nb_frames_prev) { Encoder *e = ost->enc; + FPSConvContext *fps = &e->fps; AVCodecContext *enc = ost->enc_ctx; double delta0, delta, sync_ipts, duration; if (!frame) { - *nb_frames_prev = *nb_frames = mid_pred(e->frames_prev_hist[0], - e->frames_prev_hist[1], - e->frames_prev_hist[2]); + *nb_frames_prev = *nb_frames = mid_pred(fps->frames_prev_hist[0], + fps->frames_prev_hist[1], + fps->frames_prev_hist[2]); goto finish; } @@ -1006,7 +1011,7 @@ static void video_sync_process(OutputFile *of, OutputStream *ost, AVFrame *frame switch (ost->vsync_method) { case VSYNC_VSCFR: - if (e->vsync_frame_number == 0 && delta0 >= 0.5) { + if (fps->frame_number == 0 && delta0 >= 0.5) { av_log(ost, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0)); delta = duration; delta0 = 0; @@ -1014,7 +1019,7 @@ static void video_sync_process(OutputFile *of, OutputStream *ost, AVFrame *frame } 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 && e->vsync_frame_number) { + if (frame_drop_threshold && delta < frame_drop_threshold && fps->frame_number) { *nb_frames = 0; } else if (delta < -1.1) *nb_frames = 0; @@ -1042,16 +1047,16 @@ static void video_sync_process(OutputFile *of, OutputStream *ost, AVFrame *frame } finish: - memmove(e->frames_prev_hist + 1, - e->frames_prev_hist, - sizeof(e->frames_prev_hist[0]) * (FF_ARRAY_ELEMS(e->frames_prev_hist) - 1)); - e->frames_prev_hist[0] = *nb_frames_prev; + memmove(fps->frames_prev_hist + 1, + fps->frames_prev_hist, + sizeof(fps->frames_prev_hist[0]) * (FF_ARRAY_ELEMS(fps->frames_prev_hist) - 1)); + fps->frames_prev_hist[0] = *nb_frames_prev; if (*nb_frames_prev == 0 && ost->last_dropped) { ost->nb_frames_drop++; av_log(ost, AV_LOG_VERBOSE, "*** dropping frame %"PRId64" at ts %"PRId64"\n", - e->vsync_frame_number, e->last_frame->pts); + fps->frame_number, fps->last_frame->pts); } if (*nb_frames > (*nb_frames_prev && ost->last_dropped) + (*nb_frames > *nb_frames_prev)) { if (*nb_frames > dts_error_threshold * 30) { @@ -1062,9 +1067,9 @@ finish: } ost->nb_frames_dup += *nb_frames - (*nb_frames_prev && ost->last_dropped) - (*nb_frames > *nb_frames_prev); av_log(ost, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", *nb_frames - 1); - if (ost->nb_frames_dup > e->dup_warning) { - av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", e->dup_warning); - e->dup_warning *= 10; + if (ost->nb_frames_dup > fps->dup_warning) { + av_log(ost, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", fps->dup_warning); + fps->dup_warning *= 10; } } @@ -1137,8 +1142,8 @@ static int do_video_out(OutputFile *of, OutputStream *ost, AVFrame *frame) for (i = 0; i < nb_frames; i++) { AVFrame *in_picture; - if (i < nb_frames_prev && e->last_frame->buf[0]) { - in_picture = e->last_frame; + if (i < nb_frames_prev && e->fps.last_frame->buf[0]) { + in_picture = e->fps.last_frame; } else in_picture = frame; @@ -1167,12 +1172,12 @@ static int do_video_out(OutputFile *of, OutputStream *ost, AVFrame *frame) return ret; e->next_pts++; - e->vsync_frame_number++; + e->fps.frame_number++; } - av_frame_unref(e->last_frame); + av_frame_unref(e->fps.last_frame); if (frame) - av_frame_move_ref(e->last_frame, frame); + av_frame_move_ref(e->fps.last_frame, frame); return 0; }