From patchwork Tue Feb 6 10:52:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Gaullier X-Patchwork-Id: 46069 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:7b08:b0:19e:8a94:b663 with SMTP id s8csp1318437pzh; Tue, 6 Feb 2024 02:52:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IH1sMJcEjUFIbEKy64YAm6BNvrxJNUNBjIuH35nkpwsovmt6lIPR9JaMOZH+0kG2gwNCUDO X-Received: by 2002:a17:906:1e93:b0:a30:4618:80a0 with SMTP id e19-20020a1709061e9300b00a30461880a0mr1236198ejj.33.1707216745655; Tue, 06 Feb 2024 02:52:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1707216745; cv=none; d=google.com; s=arc-20160816; b=EjyLieu6eJK7w8MkNJA4beDbZB75TsrPryBrjoXAiDBD8NKJezDWmrTFsTSRRA00cA fw+sW7R9XjZcCnLmvUCTMrCWlgKxiN34ujJ+p04XB3wZfA/1af6AE3NZ2bjecZBcTsRt i7Zjyo+RrBVRXcAe2GWGqxGga+1HPpQ3NCqrsNE/imzH1TN+1a73NhvdW+9iaWreps8C 7T7VMlZDI2RiRNtOiWZwjwL0pzOrlsW9CTfjuD6OciSDkcPRay0KRvmoq1emn31KmrvQ WPPUPdf+x2SbTK5puXZXPa5gjVHgK8ZEJwbyGnEoUaqkml7TPVvKLJMbTzdVmlSOkOid qQow== 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=ACXfOd1sIEZ/bmF2QkiJmPzEujtwBOmO/cBjHjyP5/U=; fh=Ae3vdMdGI3NVOUsSiOK7kpmj5btiA3nYMF7sdMWIgdo=; b=me8xsMZbXnZhgTiiPIINGQfCFaYAgpVuZJy73QWAIh1NEZ/SjC6aHqNPM1QshPbUYJ qUnQ67Z/RsCa29rucjkAVBEfeMn9gtlxp3CblthHpBQHVcW3Ol0m0t6EKRRxJ0f99p1c INfYgzYxuRblXg2p8VLtq+Lg1mqm7Zec8qMBQomyuJ4kpQixaapf0CuyFv3xyR1U0+Zl kSWlMTSrWAiLEW3fcQno9sZUaL+fw4LcQ1xA6WJt0UQAdQ3OyTHeEwR5BqjXM3ErmZoM av0B1UiJNpRit0DjDd9detc+VPmsyVW+DwppOFCMXKpg0zMhSYDOtgj+6jucNXFDePkJ z6fA==; 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 X-Forwarded-Encrypted: i=0; AJvYcCUfxuXQwip8I9rDgmmhMTHAcRI7FOZGfJLTGpFhOi83vp1yDjkqKFClh+n5Fj1VmmnZqJliUvYmqey/n/h0WwhILA6SZaAPqMp2WTKLa+psusz+rmuA4pujL4fqBXmQkX5YotwZs5pWiHTSJ3E/Z3zAzwTBOXSW26wPSHbx2FE4YWKJ7Tz1RGOg7bT+Th3547b/x235KROzMkuBw8+UAPw4Xw3r1VJmgSqKLsXJT/mVCaLFZnlCcHgGw2KmKq7TQXQgiUV/K6/OPorfnm0KPsBcD0O9WGkQXr0ab+M6uPIrgif5rg9UMu1HS8nAttn6sDnullM6Rzs3I/jfdLL9gskFxK/mVGrdsSsNH5doW4JLbKGiOmS1xWEg+tBIe0y/w9WJzfWk0aTcH1bbULIDvH9jJytTd8l3wpzCeKyw/pCqRpZN6RkC7kKiwTKHAHmVG/hhFetuUJal0xXvjaP+NQJq49jSQW8N0lWWsxUN4fYX4EROjS1/bboKTalVGtbc8KRE27SxuytVqV+k48zYTNgcZy7miWM/NGla3YlEjBR76xvmJa/obRvajA5IeHaQyvBn5r//07vC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com with ESMTP id z10-20020a17090655ca00b00a37ad9a5be8si918082ejp.398.2024.02.06.02.52.25; Tue, 06 Feb 2024 02:52:25 -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 10B1368D14A; Tue, 6 Feb 2024 12:52:22 +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 B437E68A9C7 for ; Tue, 6 Feb 2024 12:52:14 +0200 (EET) Received: from cji.paris (unknown [172.16.3.159]) by srv-infra-2.infra.inf.glb.tvvideoms.com (Postfix) with ESMTP id E992040539; Tue, 6 Feb 2024 10:52:13 +0000 (UTC) From: Nicolas Gaullier To: ffmpeg-devel@ffmpeg.org Date: Tue, 6 Feb 2024 11:52:09 +0100 Message-Id: <20240206105209.315221-2-nicolas.gaullier@cji.paris> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20240206105209.315221-1-nicolas.gaullier@cji.paris> References: <20240206105209.315221-1-nicolas.gaullier@cji.paris> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 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: 7J3Vjcthvx+5 Yet another probesize used to get the last pts (and thus the duration) of mpegts/ps files. 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 exemple, especially if streamcopying above it. The current code does not behave well with high bitrates and high video buffering (physical gap between the last video packet and the last audio packet). 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. Here, even if audio duration is found but not the video duration, there will be a retry, so at the end the full user-overriden probesize will be used. Signed-off-by: Nicolas Gaullier --- libavformat/avformat.h | 8 ++++++++ libavformat/demux.c | 13 ++++++++----- libavformat/options_table.h | 1 + 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libavformat/avformat.h b/libavformat/avformat.h index 5d0fe82250..533f5a963d 100644 --- a/libavformat/avformat.h +++ b/libavformat/avformat.h @@ -1823,6 +1823,14 @@ typedef struct AVFormatContext { * Freed by libavformat in avformat_free_context(). */ AVStreamGroup **stream_groups; + + /** + * Maximum number of bytes read at the end of input in order to determine the + * stream durations. Used by avformat_find_stream_info() for MPEG-TS/PS. + * + * Demuxing only, set by the caller before avformat_open_input(). + */ + int64_t duration_probesize; } AVFormatContext; /** diff --git a/libavformat/demux.c b/libavformat/demux.c index 6f640b92b1..798b44c979 100644 --- a/libavformat/demux.c +++ b/libavformat/demux.c @@ -1740,8 +1740,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) @@ -1749,6 +1750,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; @@ -1784,7 +1787,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; @@ -1793,7 +1796,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 { @@ -1847,7 +1850,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 91708de453..c2bdb484a7 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 stream durations", OFFSET(duration_probesize), AV_OPT_TYPE_INT64, {.i64 = 0 }, 0, INT64_MAX, D}, {NULL}, };