From patchwork Tue Mar 28 11:30:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_Storsj=C3=B6?= X-Patchwork-Id: 40887 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:7a30:b0:df:834d:2c1a with SMTP id t48csp2096224pzh; Tue, 28 Mar 2023 04:31:14 -0700 (PDT) X-Google-Smtp-Source: AKy350bJscOiXM99lpaS+ncG0IjjPGZw10XiQmRXBrsLm3aKLW3/1/WhxQQQ/+fMpUKhYMuOHEyH X-Received: by 2002:a05:6402:15a:b0:4f9:deb4:b984 with SMTP id s26-20020a056402015a00b004f9deb4b984mr15744854edu.9.1680003074092; Tue, 28 Mar 2023 04:31:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680003074; cv=none; d=google.com; s=arc-20160816; b=UxCwjfjGLFufdLf6bnIS8cKkbwhZOxCzTLl6336t5B+KcS+eea5dZs4Gh59sOyaeql Elo3+iUiQHgh91k840bTdDU8FdWFhfaBGJeAlzx5fT9oEOVeXlRAEiez+1cV+8vWIHJy WTzIUdZ3NRvN+kr7bn/keiESjYBF0NGnP/tKY1gSJUpQ68qT5lEnSxYhctvf0meeQYI4 CSmnv9eyKNavOKJlmTAB2lIuataDQQWnvCZg7UeGRL/yRTfmruRE9TkrNWhkcERxlksU jO7rKfL3Jo1fmXGhFhyJh32T2NWyods0cnkvBSSOZowBFJgjTYOl6GfGWbakvIh+cqfv YP9Q== 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:dkim-signature:delivered-to; bh=0Y27oem8c4sI+r1PMx/TJDx2d7OT/dsz5heHNiIn1do=; b=WY7YD/+yX4dl9zyY1tUr1Csj/n9LNaQNMEDZlUROE6FnNWX47uuO/jNCLsWGeuDX6d dxQJ2+DFNsqmi+OCoqUUtJuoJDZBIeUhlred5+LWQ3PjQEzwyPVssgPhDoSXofZ/K+yh X/jAq1VMqJbqrt+YNyN+dRReX59p3zK4JuxG94CGlt/m4d6s+7gcA9gKUQ1Q4vrE7TxU Pm3tC0WqlJy9LmKrrkucwjqdiCl6IjzoOhidpPI+vQKgA0UGHEzQfLoVUNqvrKtKO4FA JxPjgzGbSJN+bAfEHFt6Q56E7s2ZxRDSPa9c/NadL9ifLUbxwp4XDZk130M9UPsAIliK /U9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@martin-st.20210112.gappssmtp.com header.s=20210112 header.b=sneRvUm5; 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 r21-20020aa7da15000000b004fd2b07230esi29143026eds.64.2023.03.28.04.31.13; Tue, 28 Mar 2023 04:31:14 -0700 (PDT) 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; dkim=neutral (body hash did not verify) header.i=@martin-st.20210112.gappssmtp.com header.s=20210112 header.b=sneRvUm5; 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 15B4A68CB31; Tue, 28 Mar 2023 14:31:01 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F1B7168C312 for ; Tue, 28 Mar 2023 14:30:53 +0300 (EEST) Received: by mail-lj1-f182.google.com with SMTP id e21so12158635ljn.7 for ; Tue, 28 Mar 2023 04:30:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=martin-st.20210112.gappssmtp.com; s=20210112; t=1680003053; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LVoblLr9aNFk1CTG60kuB3OlrjyCsXz20Xy7fBAGDyw=; b=sneRvUm5/zcJiBdqpSff9qn5v2MLtVETjdosACbK69ErZNmtV+vyI4UhcL7roHLjiR HPrMH9EZosJPppN2PvBAL2nOu7BbZ/9K+6Tmr2xzGzptwu2AjVu5/0oNUrhGJedIGzR0 K1X7tSVcXeeMDgDI3xMA4Qz85CRP66rUSoQFdqYn559Vx5o0m9tv62SbjiRS4Nrc9byD GObQ+pgUuX+QsRhgIJuy9g/Y3hyWyoojDvA6K7OigIKWo6u69cA5DHP8uQ0hktYJ4aNh 1pVGg/CdFnkLnrY7ru4CqzEY8W4FKnVA+UNTFLEr+HCAqkVYBqySieIRAgDYACpXNfah /eYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680003053; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LVoblLr9aNFk1CTG60kuB3OlrjyCsXz20Xy7fBAGDyw=; b=BNQP9u+jbSEatGRsJ5fOWzmGjssJh9QB26Z9pDZF7Mx3sJB/cDZ6GY0CllUApnnqOa /8syIwzicGoTpbhPOHwwEejttCt6/vGUmYWpQyWGh3hEWIxXhTNkiJmkVsBC35KODFda rFq+5eyaPCJ4y2FRyF+FIZ5Lo40PSvmjGBZIu8cx6sQJDb267xeEQ+ppGKHyjcjFdYmj zFBu57m0/D3RoJiBCVMQPwG0IjLFDSPbs/nEK8+xtw8fki3qB3kTDToUS+ohnROVdMAv dV6GN6QgAnTnlmgUgHye8TWiuwK8yxX8gjjJJqbxpUHetbJdI1wi7UjUqSJdJN7Ljotl NdWQ== X-Gm-Message-State: AAQBX9dxL3DvYzD9mQorMODkVqGrt5Ym5qDKExGLmvXN1sT+ZuUj6qma FdSoFgI5oN8GUW/u9Ns0P0leVWxxb7lzGzBR9ksJeg== X-Received: by 2002:a2e:7812:0:b0:29c:9739:bc24 with SMTP id t18-20020a2e7812000000b0029c9739bc24mr4933153ljc.39.1680003053191; Tue, 28 Mar 2023 04:30:53 -0700 (PDT) Received: from localhost (dsl-tkubng21-58c01c-243.dhcp.inet.fi. [88.192.28.243]) by smtp.gmail.com with ESMTPSA id f13-20020a2e918d000000b0029c13f4d526sm4116937ljg.79.2023.03.28.04.30.52 (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 28 Mar 2023 04:30:52 -0700 (PDT) From: =?utf-8?q?Martin_Storsj=C3=B6?= To: ffmpeg-devel@ffmpeg.org Date: Tue, 28 Mar 2023 14:30:51 +0300 Message-Id: <20230328113051.3951-2-martin@martin.st> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230328113051.3951-1-martin@martin.st> References: <20230328113051.3951-1-martin@martin.st> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCHv2 2/2] libavformat: Improve ff_configure_buffers_for_index for excessive deltas 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: Michael Niedermayer , Marton Balint Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: QyTUM6oEPN4f Previously, the ff_configure_buffers_for_index function had upper sanity limits of 16 MB (1<<24) for buffer_size and 8 MB (1<<23) for short_seek_threshold. However, if the index contained entries with a much larger delta, setting pos_delta to a value larger than the sanity limit, we would end up not increasing the buffer size at all. Instead, ignore the individual deltas that are excessive, but increase the buffer size based on the deltas that are below the sanity limit. Only count deltas that are below 1<<23, 8 MB; pos_delta gets doubled before setting the buffer size - this matches the previous maximum buffer size of 1<<24, 16 MB. This can happen e.g. with a mov file with some tracks containing some samples that belong in the start of the file, at the end of the mdat, while the rest of the file is mostly reasonably interleaved; previously those samples caused the maximum pos_delta to skyrocket, skipping any buffer size enlargement. Signed-off-by: Martin Storsjö --- v2: Ignore entries that are out of range instead of clipping to the maximum allowed. --- libavformat/seek.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/libavformat/seek.c b/libavformat/seek.c index faa47f961f..386312cd3a 100644 --- a/libavformat/seek.c +++ b/libavformat/seek.c @@ -204,7 +204,9 @@ void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance) const AVIndexEntry *const e1 = &sti1->index_entries[i1]; int64_t e1_pts = av_rescale_q(e1->timestamp, st1->time_base, AV_TIME_BASE_Q); - skip = FFMAX(skip, e1->size); + if (e1->size < (1 << 23)) + skip = FFMAX(skip, e1->size); + for (; i2 < sti2->nb_index_entries; i2++) { const AVIndexEntry *const e2 = &sti2->index_entries[i2]; int64_t e2_pts = av_rescale_q(e2->timestamp, st2->time_base, AV_TIME_BASE_Q); @@ -212,7 +214,8 @@ void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance) if (e2_pts < e1_pts || e2_pts - (uint64_t)e1_pts < time_tolerance) continue; cur_delta = FFABS(e1->pos - e2->pos); - pos_delta = FFMAX(pos_delta, cur_delta); + if (cur_delta < (1 << 23)) + pos_delta = FFMAX(pos_delta, cur_delta); break; } } @@ -222,7 +225,7 @@ void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance) pos_delta *= 2; ctx = ffiocontext(s->pb); /* XXX This could be adjusted depending on protocol*/ - if (s->pb->buffer_size < pos_delta && pos_delta < (1<<24)) { + if (s->pb->buffer_size < pos_delta) { av_log(s, AV_LOG_VERBOSE, "Reconfiguring buffers to size %"PRId64"\n", pos_delta); /* realloc the buffer and the original data will be retained */ @@ -234,9 +237,7 @@ void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance) ctx->short_seek_threshold = FFMAX(ctx->short_seek_threshold, pos_delta/2); } - if (skip < (1<<23)) { - ctx->short_seek_threshold = FFMAX(ctx->short_seek_threshold, skip); - } + ctx->short_seek_threshold = FFMAX(ctx->short_seek_threshold, skip); } int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)