From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 85B6F4E045 for ; Fri, 6 Jun 2025 08:51:12 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id F1DE868C3DD; Fri, 6 Jun 2025 11:51:07 +0300 (EEST) Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTPS id E153B68BE8A for ; Fri, 6 Jun 2025 11:51:00 +0300 (EEST) Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-6077e0f2697so665765a12.0 for ; Fri, 06 Jun 2025 01:51:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749199859; x=1749804659; darn=ffmpeg.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=GbBoJJfQqT1emnIEsG+U1IC1z+4QYoY44jllHAZ0Z2U=; b=eUHKh0N2tdLwSsFNoQvYoiDUit5i7pARetVb40/Bofj8+opa0QdMgUMGDx7HW/Bbeq aOV4JBY96XIROCP9g26SK+yMtmvJFXjYCh69ExRR8aO6PBBMdhBC9W8Epx/r2FsjUaTK OSmIlkG/fsyBoEXZzXZF6ZVrb08S0+9tIxpswL2hhl1pNT84MBOwNeMw51Oo48zgyeAn ynFEKqDHA7CIHjvovtAS6iDfQvJaBUpEHZ8FMW2Aje2DO0KrFWL2Kf3naScvpJowYusf cim1QRzw7imHJaTX97uWUSTqQdfFSOWPxDGEoKBcqGAsOrly0ycz5SrL76WMxRp40rBl +CjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749199859; x=1749804659; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GbBoJJfQqT1emnIEsG+U1IC1z+4QYoY44jllHAZ0Z2U=; b=FhjbLyJkD6oorzAss7OpjqLHkbAWiCXLoSb7r/7ro78GjQj+mGN/M2hjiMgg8hF3LH 16zufXLWqov0Mp4gOCd9rIDi388KQLuW0PrRmOpgqig6clRNuoWKuOgCTJF39ABVfapO QLMO6hTVESalXstf4Ft5v0zYxoO58kMGDdxRB2dt1Mxjrxt1T+9S73rN+sfT64Ct/EJ1 /En0a9CoNq9pC8ksN6E+Uxw2BBI14HB8+kGPLt3KWpN2AJrcWApGFPuYEgS8FuSdPVLC iGFXRxdcEU9k9Jf2oVgdhi17ZOb41EcVObU4E2ONSS7ASVJRcvPx28BEV3ajgd+/jfdf SX4w== X-Gm-Message-State: AOJu0YzP/dW1Xf66gFK5JlwUUk0ngb3VbH5kg4KgDIhi6SYu6nP40tYt 5rO8l9UD+yn9c/TNBOqKdkPKJKZhSogU+JB1Bsig5KpohJajHpn4Hn9+oITHzOCqsaDVbQNIMLO IX/oVdv1yBF2d73V1Oq4fxxYOfUxgpI6Tdg== X-Gm-Gg: ASbGncu3CVbs1RswnUR5NK3IuwqDxnB+tDLPydlGwv9UK6QTad8mNIggk4fHeHGhFqe nnRd/KvQz+BMJFD24A9AP30XwkxjbpOShAPpe9NPKQQO5kd/VO1vAM1Rdi1QJYqErsNqHSYvhcQ Oa03WpdImSsmVa7D7U5jExBarwvcub2A== X-Google-Smtp-Source: AGHT+IGA7gbGIjkc9zZUASQISe7JGQrsyM2GVwH1TwOgjLk/BsCzZWG319EXuexm0skVBarT373zMFISlxXJApWegZE= X-Received: by 2002:a05:6402:1ed4:b0:606:f7bf:86f3 with SMTP id 4fb4d7f45d1cf-607735120e8mr2039817a12.6.1749199859220; Fri, 06 Jun 2025 01:50:59 -0700 (PDT) MIME-Version: 1.0 References: <20250521132300.32387-1-derek.buitenhuis@gmail.com> In-Reply-To: <20250521132300.32387-1-derek.buitenhuis@gmail.com> From: Kacper Michajlow Date: Fri, 6 Jun 2025 10:50:47 +0200 X-Gm-Features: AX0GCFvX8PUDWv4OXLkVYoLoszD7SnA6uZ8cFPdVqoV6HnTL08ydy8lPQvN4n1s Message-ID: To: FFmpeg development discussions and patches Subject: Re: [FFmpeg-devel] [PATCH v3] avformat/dhav: fix backward scanning for get_duration and optimize seeking 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On Wed, 21 May 2025 at 15:23, Derek Buitenhuis wrote: > > From: Justin Ruggles > > The backwards scanning done for incomplete final packets should not > assume a specific alignment at the end of the file. Truncated files > result in hundreds of thousands of seeks if the final packet does not > fall on a specific byte boundary, which can be extremely slow. > For example, with HTTP, each backwards seek results in a separate > HTTP request. > > This changes the scanning to check for the end tag 1 byte at a time > and buffers the last 1 MiB using ffio_ensure_seekback to avoid additional > seek operations. > > Co-authored-by: Derek Buitenhuis > Signed-off-by: Justin Ruggles > Signed-off-by: Derek Buitenhuis > --- > libavformat/dhav.c | 54 +++++++++++++++++++++++++++++++++------------- > 1 file changed, 39 insertions(+), 15 deletions(-) > > diff --git a/libavformat/dhav.c b/libavformat/dhav.c > index b2ead99609..d9db775802 100644 > --- a/libavformat/dhav.c > +++ b/libavformat/dhav.c > @@ -22,6 +22,7 @@ > > #include > > +#include "libavutil/intreadwrite.h" > #include "libavutil/mem.h" > #include "libavutil/parseutils.h" > #include "avio_internal.h" > @@ -232,37 +233,60 @@ static void get_timeinfo(unsigned date, struct tm *timeinfo) > timeinfo->tm_sec = sec; > } > > +#define MAX_DURATION_BUFFER_SIZE (1024*1024) > + > static int64_t get_duration(AVFormatContext *s) > { > - DHAVContext *dhav = s->priv_data; > int64_t start_pos = avio_tell(s->pb); > + int64_t end_pos = -1; > int64_t start = 0, end = 0; > struct tm timeinfo; > - int max_interations = 100000; > + uint8_t *end_buffer; > + int64_t end_buffer_size; > + int64_t end_buffer_pos; > + int64_t offset; > + unsigned date; > > if (!s->pb->seekable) > return 0; > > - avio_seek(s->pb, avio_size(s->pb) - 8, SEEK_SET); > - while (avio_tell(s->pb) > 12 && max_interations--) { > - if (avio_rl32(s->pb) == MKTAG('d','h','a','v')) { > - int64_t seek_back = avio_rl32(s->pb); > + if (start_pos + 16 > avio_size(s->pb)) > + return 0; > > - avio_seek(s->pb, -seek_back, SEEK_CUR); > - read_chunk(s); > - get_timeinfo(dhav->date, &timeinfo); > - end = av_timegm(&timeinfo) * 1000LL; > + avio_skip(s->pb, 16); > + date = avio_rl32(s->pb); > + get_timeinfo(date, &timeinfo); > + start = av_timegm(&timeinfo) * 1000LL; > + > + end_buffer_size = FFMIN(MAX_DURATION_BUFFER_SIZE, avio_size(s->pb)); > + end_buffer = av_malloc(end_buffer_size); This allocation is never freed. - Kacper _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".