From patchwork Sat Jan 7 12:07:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 39913 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp1803733pzb; Sat, 7 Jan 2023 04:07:53 -0800 (PST) X-Google-Smtp-Source: AMrXdXsn6xFLkQhtmeC5PkJ9ZUz64ak87R+GfhI9O3Yd9MdCzPu28EabfCHa3YH7l4XoqbuaVB4u X-Received: by 2002:a17:906:fd4d:b0:84d:221c:82cf with SMTP id wi13-20020a170906fd4d00b0084d221c82cfmr3589670ejb.65.1673093273158; Sat, 07 Jan 2023 04:07:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673093273; cv=none; d=google.com; s=arc-20160816; b=qJ+cyzQ3mWejemrlayTOmQTbSR1noMH2LiveX4KFBLbYy7zW/VA7CfU3LQMrhEPUnx Ffs9omyk34npZQ0HUx1RCZs6bzZha2+yTibDZYZi44Rs2mgVoP6rtfz5MtsSwUOEwucV r/BF36iyVEDwSb6a4DK9qQTNJhnz8F/iHyGwLcFVMJRI1Z0TtFpnt5fpV41rAPH4vDH0 O+Ccd9QvzjHNT3rPb9S0SfgPKK9K7vlOKCeiDPZcJD8Ikaa4yX19BEwfo6l30sYoQ6q3 YdfmHIs4CMeo/2Kvqxe3BGzeLLz2XrcQC+ZawxzWkj+8+Z378zrqOOIfX1ctNSyoSexN roJw== 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=Oq/SQ+YITCXuW7WM5EU9B2d0oUHLBfrBlVufwrMBYR8=; b=EQGbk7mEuR4D8dpQ/wjlNbG2/KG1m4mZ8Em3SpKKMfRmeAZKTP+aRgE4O7bBcPafI2 dzVlAmA9pOgAumDifuKWC3cskuKUXcvgkbqTKjmW+HHCEMhsi2YH8ms5LenlPh3kwkC/ ldG0oWd+v0WlzzQ+gAUyjfonNTVD/HGSk7JVNOSKyhi1+PUTLW4DC2cesMgG7jnQTBJH 1JkRVyJpgmtCxjC1s3sY5boXglWNzWr+cXXxaZABzc9YW6+q9DTd9U3PbaaaLbA5D3Jo 0UvrcQa35y3g1neJCC3621H1lt9dZZOle1WuNRGWbsClPU8nyeZNyeIX4Z9PcWNIHJdS YRPA== 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 o6-20020a170906974600b008357d517d6bsi4961350ejy.394.2023.01.07.04.07.51; Sat, 07 Jan 2023 04:07:53 -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 3440868B9FF; Sat, 7 Jan 2023 14:07:48 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-103.mailbox.org (mout-p-103.mailbox.org [80.241.56.161]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3D94D68B9FF for ; Sat, 7 Jan 2023 14:07:40 +0200 (EET) Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (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-103.mailbox.org (Postfix) with ESMTPS id 4NpzVK6sXTz9sQn for ; Sat, 7 Jan 2023 13:07:37 +0100 (CET) From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Sat, 7 Jan 2023 17:37:08 +0530 Message-Id: <20230107120708.8758-1-ffmpeg@gyani.pro> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4NpzVK6sXTz9sQn Subject: [FFmpeg-devel] [PATCH v2] 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: dWSU7Sgn+Clg segment_time and segment_times are defined as duration specifications, not timestamps, so calculation of segment duration must account for initial timestamp. Fixed. FATE ref for segment-mp4-to-ts changed on account of avoiding premature segment cut at the end of the first segment. --- libavformat/segment.c | 15 +++++++++++++++ tests/ref/fate/segment-mp4-to-ts | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) 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); diff --git a/tests/ref/fate/segment-mp4-to-ts b/tests/ref/fate/segment-mp4-to-ts index 54fcadb846..2994416270 100644 --- a/tests/ref/fate/segment-mp4-to-ts +++ b/tests/ref/fate/segment-mp4-to-ts @@ -23,7 +23,7 @@ 0, 50400, 54000, 3600, 607, 0xc53c2339, F=0x0, S=1, 1 0, 54000, 72000, 3600, 4755, 0x2f642b58, F=0x0, S=1, 1 0, 57600, 64800, 3600, 1182, 0xbe1a4847, F=0x0, S=1, 1 -0, 61200, 61200, 3600, 809, 0x8d948a4e, F=0x2, S=1, 1 +0, 61200, 61200, 3600, 809, 0x8d948a4e, F=0x0, S=1, 1 0, 64800, 68400, 3600, 656, 0x4fa03c2b, F=0x0, S=1, 1 0, 68400, 86400, 3600, 26555, 0x5629b584, S=1, 1 0, 72000, 79200, 3600, 1141, 0x761b31e8, F=0x0, S=1, 1 @@ -71,7 +71,7 @@ 0, 223200, 226800, 3600, 809, 0x84e37fee, F=0x0, S=1, 1 0, 226800, 244800, 3600, 4541, 0xd4e5c0de, F=0x0, S=1, 1 0, 230400, 237600, 3600, 1545, 0x0099fc98, F=0x0, S=1, 1 -0, 234000, 234000, 3600, 929, 0xfd72d049, F=0x0, S=1, 1 +0, 234000, 234000, 3600, 929, 0xfd72d049, F=0x2, S=1, 1 0, 237600, 241200, 3600, 829, 0xcfda9e96, F=0x0, S=1, 1 0, 241200, 259200, 3600, 24220, 0x5ca21d71, S=1, 1 0, 244800, 252000, 3600, 1422, 0xcde6cc34, F=0x0, S=1, 1