From patchwork Wed Jan 4 16:42:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 39865 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp8941936pzb; Wed, 4 Jan 2023 08:45:40 -0800 (PST) X-Google-Smtp-Source: AMrXdXvLf5eYdBcDINdVk3RijarRNPIdIwVTIN6+rtVyabCPorws0GdmE+Gjd4MOegpLYIqyrMs3 X-Received: by 2002:aa7:c30b:0:b0:47b:a6e:6ba5 with SMTP id l11-20020aa7c30b000000b0047b0a6e6ba5mr40558238edq.12.1672850740605; Wed, 04 Jan 2023 08:45:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672850740; cv=none; d=google.com; s=arc-20160816; b=LuRN49mZ3zKgav4y7x69O074a9Yw2NjhaQV1BWIBKlrOwDhRZRC1Tr4Q6pvaN2PEdp 0d+ewakIInDt8LsTYymQAtIsEfpjeF5CBHN8lXmnd0KGQivvZD//5WxpkYBl1NLXIaNE jAcF5fcN65grmWGVbRFq7CHh33DzkEklAaz475ov171niaAshujWU9qFE6MxiC4H0c+l EqKaPFvKMsXuYyTlhxtyaNRGgywEaz4ODhJ/Z7FonBllajLL1RIjJTYbUrV7v8AH40Zi WEQItKYF0UWrwiaXhpWFDePANW82QvmQ/51D9IlyW/PPXwQET1DO3E4A/hKEKuTaHBtZ 1L2Q== 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=aas9haCkYENhktI0M3PeXoHgmLkXlcd5YQH7KRjQKlE=; b=VV3pu/TdJ44PmJ8mtAVg8hfyyfJu4r30YaP1KMdMZZhvstupo3p91GFOfpzlYeNsDl 3EuLq6s6sguDK5wjPiIp6vjquyVqRYsdVmnaPSVOIiDg/K+dsHjcY6ewO4tQX3IFcBOn 5fCDwxdPibk4Q+/niNOzh98xPsUI9IhcJADdfdyU0WCZO5fpo3lO/11KFYfVc3lYjkG+ sa/nvwVB6kXSNNiHl1jHIMUssNJ02E24O8i3LF7dkxhNFN7TwO1iARzv/QhIYuKhZdUh FZEATKAGiXU079TX/PQhF3htzmGqr+CarTp4CkzO2HkB6SoCo8efJZeejka0PIjVPP3k 5dyg== 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 z8-20020a05640235c800b0048ef14a74e4si3395189edc.141.2023.01.04.08.45.39; Wed, 04 Jan 2023 08:45:40 -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 7669B68B109; Wed, 4 Jan 2023 18:45:27 +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 22A9968B109 for ; Wed, 4 Jan 2023 18:45:20 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id DC9B02400F5 for ; Wed, 4 Jan 2023 17:45:19 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id cBtVWJ8krGzy for ; Wed, 4 Jan 2023 17:45:19 +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 9034E2404F7 for ; Wed, 4 Jan 2023 17:45:18 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id A9FC53A0354 for ; Wed, 4 Jan 2023 17:45:10 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 4 Jan 2023 17:42:43 +0100 Message-Id: <20230104164246.6133-5-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20230104164246.6133-1-anton@khirnov.net> References: <20230104164246.6133-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/8] fftools/ffmpeg: move video frame dup/drop logic into its own function 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: MPF0U7QfygGb --- fftools/ffmpeg.c | 137 ++++++++++++++++++++++++++--------------------- 1 file changed, 75 insertions(+), 62 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 66a24a1a9c..04de57e41e 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1030,6 +1030,79 @@ static void do_subtitle_out(OutputFile *of, } } +/* Convert frame timestamps to the encoder timebase and decide how many times + * should this (and possibly previous) frame be repeated in order to conform to + * desired target framerate (if any). + */ +static void video_sync_process(OutputFile *of, OutputStream *ost, + AVFrame *next_picture, double duration, + int64_t *nb_frames, int64_t *nb_frames_prev) +{ + double delta0, delta; + + double sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture); + /* delta0 is the "drift" between the input frame (next_picture) and + * where it would fall in the output. */ + delta0 = sync_ipts - ost->next_pts; + delta = delta0 + duration; + + // tracks the number of times the PREVIOUS frame should be duplicated, + // mostly for variable framerate (VFR) + *nb_frames_prev = 0; + /* by default, we output a single frame */ + *nb_frames = 1; + + if (delta0 < 0 && + delta > 0 && + ost->vsync_method != VSYNC_PASSTHROUGH && + ost->vsync_method != VSYNC_DROP) { + if (delta0 < -0.6) { + av_log(NULL, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0); + } else + av_log(NULL, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0); + sync_ipts = ost->next_pts; + duration += delta0; + delta0 = 0; + } + + switch (ost->vsync_method) { + case VSYNC_VSCFR: + if (ost->vsync_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; + ost->next_pts = llrint(sync_ipts); + } + 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->vsync_frame_number) { + *nb_frames = 0; + } else if (delta < -1.1) + *nb_frames = 0; + else if (delta > 1.1) { + *nb_frames = llrintf(delta); + if (delta0 > 1.1) + *nb_frames_prev = llrintf(delta0 - 0.6); + } + next_picture->duration = 1; + break; + case VSYNC_VFR: + if (delta <= -0.6) + *nb_frames = 0; + else if (delta > 0.6) + ost->next_pts = llrint(sync_ipts); + next_picture->duration = duration; + break; + case VSYNC_DROP: + case VSYNC_PASSTHROUGH: + next_picture->duration = duration; + ost->next_pts = llrint(sync_ipts); + break; + default: + av_assert0(0); + } +} + static enum AVPictureType forced_kf_apply(KeyframeForceCtx *kf, AVRational tb, const AVFrame *in_picture, int dup_idx) { @@ -1089,7 +1162,6 @@ static void do_video_out(OutputFile *of, AVCodecContext *enc = ost->enc_ctx; AVRational frame_rate; int64_t nb_frames, nb_frames_prev, i; - double delta, delta0; double duration = 0; InputStream *ist = ost->ist; AVFilterContext *filter = ost->filter->filter; @@ -1118,67 +1190,8 @@ static void do_video_out(OutputFile *of, ost->last_nb0_frames[1], ost->last_nb0_frames[2]); } else { - double sync_ipts = adjust_frame_pts_to_encoder_tb(of, ost, next_picture); - /* delta0 is the "drift" between the input frame (next_picture) and - * where it would fall in the output. */ - delta0 = sync_ipts - ost->next_pts; - delta = delta0 + duration; - - // tracks the number of times the PREVIOUS frame should be duplicated, - // mostly for variable framerate (VFR) - nb_frames_prev = 0; - /* by default, we output a single frame */ - nb_frames = 1; - - if (delta0 < 0 && - delta > 0 && - ost->vsync_method != VSYNC_PASSTHROUGH && - ost->vsync_method != VSYNC_DROP) { - if (delta0 < -0.6) { - av_log(NULL, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0); - } else - av_log(NULL, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0); - sync_ipts = ost->next_pts; - duration += delta0; - delta0 = 0; - } - - switch (ost->vsync_method) { - case VSYNC_VSCFR: - if (ost->vsync_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; - ost->next_pts = llrint(sync_ipts); - } - 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->vsync_frame_number) { - nb_frames = 0; - } else if (delta < -1.1) - nb_frames = 0; - else if (delta > 1.1) { - nb_frames = llrintf(delta); - if (delta0 > 1.1) - nb_frames_prev = llrintf(delta0 - 0.6); - } - next_picture->duration = 1; - break; - case VSYNC_VFR: - if (delta <= -0.6) - nb_frames = 0; - else if (delta > 0.6) - ost->next_pts = llrint(sync_ipts); - next_picture->duration = duration; - break; - case VSYNC_DROP: - case VSYNC_PASSTHROUGH: - next_picture->duration = duration; - ost->next_pts = llrint(sync_ipts); - break; - default: - av_assert0(0); - } + video_sync_process(of, ost, next_picture, duration, + &nb_frames, &nb_frames_prev); } memmove(ost->last_nb0_frames + 1,