From patchwork Sat Jan 7 09:47:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 39911 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp1740633pzb; Sat, 7 Jan 2023 01:47:46 -0800 (PST) X-Google-Smtp-Source: AMrXdXuA0YLEdfWzQapOgPq6h81LWqgZHTzKNPYK4fus+1IX7Iqh6FrEhwuQsZF9ufP7hDH+Q5SU X-Received: by 2002:a17:907:a4c3:b0:84d:150d:5006 with SMTP id vq3-20020a170907a4c300b0084d150d5006mr6697658ejc.49.1673084866013; Sat, 07 Jan 2023 01:47:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673084865; cv=none; d=google.com; s=arc-20160816; b=LI+UW7Ej+xcqFfZkyVfNNkNqP91EIqrvBELVfCAq8KweqxMRvBs2lHCbIWG0CbBncR drAP7Eb/SSSZjccf2L4ijynm6gLInLWOSGxNgi8ygQVYa9qVZR3ZEnR6wjxnSnFI6gpn UBg7mB84esydMDelUzQO1FC8KyI2q/oaa6YfgkJKUlB51EzVyqcGmg7Xm/CR+LljM3i+ 2qE73iCwXJbOy2SXlR5NQQ3UQj8qd72h240yF7eguycAsQasp2JXc2edbYsNfBZZ2Lce vsRLw6OE09pg8NBeRhB318zDoP2b5lJDtn3oHyUdyfW9WhzaVwWa/HH3E3sYNRv5TX9D NqGQ== 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:message-id:date:to:from :delivered-to; bh=8k6Pope/kuqiso4EerpEdg2QQrLGGGIdKnJMtsn9Wv8=; b=ECA2CfuxXzWDvbHQFWyn1yJ5W3hXB+N+6hwDDpKddOK3HGEPkQNelvQ2dA0I3x9gXl apaeRfPWld3taC4DtTimVxSylPIyQOWWvFSPfkr+ZJg/k+jVWanzs9ihGOghXXRyr9Xw D3OqEnou7+lwjI6q5o3VMvgAGwhYUVKai68ySIcihxParb59TJAr5DlCI6t/pTWiwg7x iGFWaOXtC4R44sQ15VN4wGxD3y3GW0DVyMlOl2/06BgWGDw30lZG9IQwfiEf9pbSyMaA T/yS2VhFSC4252uyYb+5sh/Y3qaUY+h+rCuL7LK3xOjrxxGWpFXQTUR1/HeKu9DyhDdK RymA== 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 dd19-20020a1709069b9300b0080a15fe49bfsi3737466ejc.896.2023.01.07.01.47.45; Sat, 07 Jan 2023 01:47:45 -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 3084E68BBDE; Sat, 7 Jan 2023 11:47:41 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0A53768BA72 for ; Sat, 7 Jan 2023 11:47:33 +0200 (EET) Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4NpwNf2x4Dz9sSk for ; Sat, 7 Jan 2023 10:47:30 +0100 (CET) From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Sat, 7 Jan 2023 15:17:01 +0530 Message-Id: <20230107094701.1116-1-ffmpeg@gyani.pro> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4NpwNf2x4Dz9sSk Subject: [FFmpeg-devel] [PATCH] avformat/segment: calculate segment durations correctly. 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: uelhI0jnd+gy segment_time and segment_times are defined as duration specifications, not timestamps, so calculation of segment duration must account for initial timestamp. Fixed. --- libavformat/segment.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/libavformat/segment.c b/libavformat/segment.c index 0e6421ea5d..80e4bf851c 100644 --- a/libavformat/segment.c +++ b/libavformat/segment.c @@ -116,6 +116,7 @@ typedef struct SegmentContext { int64_t initial_offset; ///< initial timestamps offset, expressed in microseconds char *reference_stream_specifier; ///< reference stream specifier int reference_stream_index; + int64_t reference_stream_first_pts; ///< initial timestamp, expressed in microseconds int break_non_keyframes; int write_empty; @@ -746,6 +747,8 @@ static int seg_init(AVFormatContext *s) seg->reference_stream_index, av_get_media_type_string(s->streams[seg->reference_stream_index]->codecpar->codec_type)); + seg->reference_stream_first_pts = AV_NOPTS_VALUE; + seg->oformat = av_guess_format(seg->format, s->url, NULL); if (!seg->oformat) @@ -898,6 +901,18 @@ calc_times: pkt->flags & AV_PKT_FLAG_KEY, pkt->stream_index == seg->reference_stream_index ? seg->frame_count : -1); + if (seg->reference_stream_first_pts == AV_NOPTS_VALUE && + pkt->stream_index == seg->reference_stream_index && + pkt->pts != AV_NOPTS_VALUE) { + seg->reference_stream_first_pts = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q); + } + + if (seg->reference_stream_first_pts != AV_NOPTS_VALUE) { + end_pts += (INT64_MAX - end_pts >= seg->reference_stream_first_pts) ? + seg->reference_stream_first_pts : + INT64_MAX - end_pts; + } + if (pkt->pts != AV_NOPTS_VALUE) pkt_pts_avtb = av_rescale_q(pkt->pts, st->time_base, AV_TIME_BASE_Q);