From patchwork Tue Mar 5 12:07:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Gaullier X-Patchwork-Id: 46811 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:3944:b0:19e:cdac:8cce with SMTP id ac4csp349004pzc; Tue, 5 Mar 2024 04:08:07 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXdhfIhTxkt0KnB2EY9lBLCdjVVDRzQzZoiA5/fV4HV48LL+aLUMprr4g6+2AxIpHVS5o6Hf1hE2JpweU1a1yPaC7fLtdsDFctnVQ== X-Google-Smtp-Source: AGHT+IHfwZf8qA8rkuDsZsbxyH26ScneRWff+UCtDTsGiK1cED7K7c1QoVkkNcfgn48coj7DuWwa X-Received: by 2002:a17:907:8e99:b0:a44:d4d7:fba0 with SMTP id tx25-20020a1709078e9900b00a44d4d7fba0mr218466ejc.0.1709640487323; Tue, 05 Mar 2024 04:08:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1709640487; cv=none; d=google.com; s=arc-20160816; b=Z113d6fn2lSAiR44IWxzF0iLnAQCb50+guvSRl+14IYjsaMyebeoonlGEw0KteZSj2 wKFQzVI+j5Ye1L2uFe1nGEYTLye3ftBtCzjzqzwu/byJtZ9Vn/YoK5sybGITtVS5Bqqk wTHbAI+F2rqbwPP0/1z57OyxU6RaEvmdwY1vLkNPRKddfgI9SYIYMdD81axPfDw3PpnN H6240utdaX3QOZ5kza6Sq0B7X0x9lVPUyhDMvwE9qgdIenEu5s2v61yLPOpPWiq0q3p7 xXsrtZatVWjmzeHK0y9rq4BCF6ZvOshBomrbcjDNB6A6B5GzSrpLcoHZ1u0aGaTOfapU f88Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc: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=cx84WRRggKXEnBandvh8vLEEG1s2lPlr/gm1723+oZU=; fh=zgMS+X4RExv2k7fU8QdWClnqVJdqO/9UbUuBS5xxJDs=; b=CYy6Ohx+Q6h2q0WoU7fEENJuCJGwzvGWjSUKG4Uors3ko+yX/rzxRRfGFOpwvRX5nE MLGJblk5BkoIWBG0W5V9quqNbHKVJKSekXNmi0DA78RApA55BMiOKpiRK7d4B1gebNAO vXcr4BqTOCH0ZUcTOzNluLns57PH2g3X4XLF+Xjoe0Q5v5e1xsXf37PyWXYDOvHo5bNP nD4QIg6oHCjNFL58TRrz4WsMi6AVh6Fn0D6BrKelF4WC/9FLezF+/i5Qhb+daBkvdVLi u40o6MzAgK9fHAyP0gByeSailodd4IzHh50hDYSXN9FA6JdqQ3Y1TY35oixl5ArVG55X EtCw==; dara=google.com 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 f17-20020a170906049100b00a43fd9b00a2si5127177eja.31.2024.03.05.04.08.06; Tue, 05 Mar 2024 04:08:07 -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 04AEF68CE09; Tue, 5 Mar 2024 14:08:03 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from srv-infra-2.infra.inf.glb.tvvideoms.com (www.inf.tvvideoms.com [213.205.126.156]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 1202168CDBE for ; Tue, 5 Mar 2024 14:07:55 +0200 (EET) Received: from cji.paris (unknown [172.16.3.159]) by srv-infra-2.infra.inf.glb.tvvideoms.com (Postfix) with ESMTP id 3A9F440773; Tue, 5 Mar 2024 12:07:55 +0000 (UTC) From: Nicolas Gaullier To: ffmpeg-devel@ffmpeg.org Date: Tue, 5 Mar 2024 13:07:51 +0100 Message-Id: <20240305120751.2025216-2-nicolas.gaullier@cji.paris> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240305120751.2025216-1-nicolas.gaullier@cji.paris> References: <20240305120751.2025216-1-nicolas.gaullier@cji.paris> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/1] avformat/demux: Add durationprobesize AVOption 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 Cc: Nicolas Gaullier Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: fL2Lwzx3iLVP Yet another probesize used to get the durations when estimate_timings_from_pts is required. It is aimed at users interested in better durations probing for itself, or because using avformat_find_stream_info indirectly and requiring exact values: for concatdec for example, especially if streamcopying above it. The current code is a performance trade-off that can fail to get video stream durations in a scenario with high bitrates and buffering for files ending cleanly (as opposed to live captures): the physical gap between the last video packet and the last audio packet is very high in such a case. Default behaviour is unchanged: 250k up to 250k << 6 (step by step) Setting this new option has two effects: - override the maximum probesize (currently 250k << 6) - reduce the number of steps to 1 instead of 6, this is to avoid detecting the audio "too early" and failing to reach a video packet. Even if a single audio stream duration is found but not the other audio/video stream durations, there will be a retry, so at the end the full user-overriden probesize will be used as expected by the user. Signed-off-by: Nicolas Gaullier --- doc/APIchanges | 3 +++ doc/formats.texi | 13 +++++++++++++ libavformat/avformat.h | 9 +++++++++ libavformat/demux.c | 13 ++++++++----- libavformat/options_table.h | 1 + 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index 7d46ebb006..548c91effe 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -2,6 +2,9 @@ The last version increases of all libraries were on 2023-02-09 API changes, most recent first: +2024-03-05 - xxxxxxxxxx - lavf 60.23.100 - options_table.h + Add AVOption durationprobesize + 2024-02-28 - xxxxxxxxxx - swr 4.14.100 - swresample.h swr_convert() now accepts arrays of const pointers (to input and output). diff --git a/doc/formats.texi b/doc/formats.texi index 69fc1457a4..b9feef5d15 100644 --- a/doc/formats.texi +++ b/doc/formats.texi @@ -228,6 +228,19 @@ would require too many resources due to a large number of streams. Skip estimation of input duration when calculated using PTS. At present, applicable for MPEG-PS and MPEG-TS. +@item durationprobesize @var{integer} (@emph{input}) +Set probing size in bytes for estimating durations when calculated using PTS. +At present, applicable for MPEG-PS and MPEG-TS. +It is aimed at users interested in better durations probing for itself, or indirectly +for specific use cases like using the concat demuxer. +Files with high bitrates and ending cleanly (as opposed to live captures), can lead +to a large physical gap between the last video packet and the last audio packet, +so many bytes have to be read in order to get a video stream duration. +Setting this value has a performance impact even for small files because the probing size is fixed. +Default behaviour is a trade-off, largely adaptive: the probing size may range from +250K up to 16M, but it is not extended to get streams durations at all costs. +Must be an integer not lesser than 1, or 0 for default behaviour. + @item strict, f_strict @var{integer} (@emph{input/output}) Specify how strictly to follow the standards. @code{f_strict} is deprecated and should be used only via the @command{ffmpeg} tool. diff --git a/libavformat/avformat.h b/libavformat/avformat.h index f4506f4cf1..7b714f3c65 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -1994,6 +1994,15 @@ typedef struct AVFormatContext { * Freed by libavformat in avformat_free_context(). */ AVStreamGroup **stream_groups; + + /** + * Maximum number of bytes read from input in order to determine stream durations + * when using estimate_timings_from_pts in avformat_find_stream_info(). + * + * - encoding: unused + * - decoding: set by user + */ + int64_t duration_probesize; } AVFormatContext; /** diff --git a/libavformat/demux.c b/libavformat/demux.c index 2e1d0ed66d..4a570ca2ce 100644 --- a/libavformat/demux.c +++ b/libavformat/demux.c @@ -1836,8 +1836,9 @@ static void estimate_timings_from_bit_rate(AVFormatContext *ic) "Estimating duration from bitrate, this may be inaccurate\n"); } -#define DURATION_MAX_READ_SIZE 250000LL -#define DURATION_MAX_RETRY 6 +#define DURATION_MAX_READ_SIZE_DEFAULT 250000LL +#define DURATION_MAX_RETRY_DEFAULT 6 +#define DURATION_MAX_RETRY_USER 1 /* only usable for MPEG-PS streams */ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset) @@ -1845,6 +1846,8 @@ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset) FFFormatContext *const si = ffformatcontext(ic); AVPacket *const pkt = si->pkt; int num, den, read_size, ret; + int64_t duration_max_read_size = ic->duration_probesize ? ic->duration_probesize >> DURATION_MAX_RETRY_USER : DURATION_MAX_READ_SIZE_DEFAULT; + int duration_max_retry = ic->duration_probesize ? DURATION_MAX_RETRY_USER : DURATION_MAX_RETRY_DEFAULT; int found_duration = 0; int is_end; int64_t filesize, offset, duration; @@ -1880,7 +1883,7 @@ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset) filesize = ic->pb ? avio_size(ic->pb) : 0; do { is_end = found_duration; - offset = filesize - (DURATION_MAX_READ_SIZE << retry); + offset = filesize - (duration_max_read_size << retry); if (offset < 0) offset = 0; @@ -1889,7 +1892,7 @@ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset) for (;;) { AVStream *st; FFStream *sti; - if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0))) + if (read_size >= duration_max_read_size << (FFMAX(retry - 1, 0))) break; do { @@ -1943,7 +1946,7 @@ static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset) } } while (!is_end && offset && - ++retry <= DURATION_MAX_RETRY); + ++retry <= duration_max_retry); av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN); diff --git a/libavformat/options_table.h b/libavformat/options_table.h index b9dca147f9..0cc921a28d 100644 --- a/libavformat/options_table.h +++ b/libavformat/options_table.h @@ -108,6 +108,7 @@ static const AVOption avformat_options[] = { {"max_streams", "maximum number of streams", OFFSET(max_streams), AV_OPT_TYPE_INT, { .i64 = 1000 }, 0, INT_MAX, D }, {"skip_estimate_duration_from_pts", "skip duration calculation in estimate_timings_from_pts", OFFSET(skip_estimate_duration_from_pts), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, D}, {"max_probe_packets", "Maximum number of packets to probe a codec", OFFSET(max_probe_packets), AV_OPT_TYPE_INT, { .i64 = 2500 }, 0, INT_MAX, D }, +{"durationprobesize", "maximum number of bytes to probe the durations of the streams in estimate_timings_from_pts", OFFSET(duration_probesize), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, D}, {NULL}, };