Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] avformat/dhav: fix backward scanning for get_duration and optimize seeking
@ 2025-03-24 15:57 Derek Buitenhuis
  0 siblings, 0 replies; only message in thread
From: Derek Buitenhuis @ 2025-03-24 15:57 UTC (permalink / raw)
  To: ffmpeg-devel

From: Justin Ruggles <justinr@vimeo.com>

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.

Signed-off-by: Justin Ruggles <justinr@vimeo.com>
Signed-off-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
---
 libavformat/dhav.c | 38 +++++++++++++++++++++++++++++---------
 1 file changed, 29 insertions(+), 9 deletions(-)

diff --git a/libavformat/dhav.c b/libavformat/dhav.c
index b2ead99609..15a75b5033 100644
--- a/libavformat/dhav.c
+++ b/libavformat/dhav.c
@@ -232,34 +232,54 @@ 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;
 
     if (!s->pb->seekable)
         return 0;
 
+    end_buffer_size = FFMIN(MAX_DURATION_BUFFER_SIZE, avio_size(s->pb));
+    end_buffer = av_malloc(end_buffer_size);
+    if (!end_buffer)
+        return 0;
+    end_buffer_pos = avio_size(s->pb) - end_buffer_size;
+    avio_seek(s->pb, end_buffer_pos, SEEK_SET);
+    ffio_ensure_seekback(s->pb, end_buffer_size);
+    avio_read(s->pb, end_buffer, end_buffer_size);
+    av_freep(&end_buffer);
+
     avio_seek(s->pb, avio_size(s->pb) - 8, SEEK_SET);
-    while (avio_tell(s->pb) > 12 && max_interations--) {
+    while (avio_tell(s->pb) > end_buffer_pos) {
         if (avio_rl32(s->pb) == MKTAG('d','h','a','v')) {
             int64_t seek_back = avio_rl32(s->pb);
-
-            avio_seek(s->pb, -seek_back, SEEK_CUR);
-            read_chunk(s);
-            get_timeinfo(dhav->date, &timeinfo);
-            end = av_timegm(&timeinfo) * 1000LL;
+            end_pos = avio_tell(s->pb) - seek_back;
             break;
         } else {
-            avio_seek(s->pb, -12, SEEK_CUR);
+            avio_seek(s->pb, -5, SEEK_CUR);
         }
     }
 
-    avio_seek(s->pb, start_pos, SEEK_SET);
+    if (end_pos < 0) {
+        avio_seek(s->pb, start_pos, SEEK_SET);
+        return 0;
+    }
+
+    avio_seek(s->pb, end_pos, SEEK_SET);
+    read_chunk(s);
+    get_timeinfo(dhav->date, &timeinfo);
+    end = av_timegm(&timeinfo) * 1000LL;
 
+    avio_seek(s->pb, start_pos, SEEK_SET);
     read_chunk(s);
     get_timeinfo(dhav->date, &timeinfo);
     start = av_timegm(&timeinfo) * 1000LL;
-- 
2.47.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-03-24 15:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-03-24 15:57 [FFmpeg-devel] [PATCH] avformat/dhav: fix backward scanning for get_duration and optimize seeking Derek Buitenhuis

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git