History

avformat/dhav: fix backward scanning for get_duration and optimize seeking

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 to avoid additional seek operations.

Justin Ruggles committed 1 year ago

Commit 36ec9211 parent c8b09fb1 file · +39 −15Browse files

Unified · Split

libavformat/dhav.c+39−15
@@ -22,6 +22,7 @@ · 21 unmodified lines
22 22
23#include <time.h>23#include <time.h>
24 24
25#include "libavutil/intreadwrite.h"
25#include "libavutil/mem.h"26#include "libavutil/mem.h"
26#include "libavutil/parseutils.h"27#include "libavutil/parseutils.h"
27#include "avio_internal.h"28#include "avio_internal.h"
@@ -232,37 +233,60 @@ static void get_timeinfo(unsigned date, struct tm *timeinfo) · 204 unmodified lines
232 timeinfo->tm_sec = sec;233 timeinfo->tm_sec = sec;
233}234}
234 235
236#define MAX_DURATION_BUFFER_SIZE (1024*1024)
237
235static int64_t get_duration(AVFormatContext *s)238static int64_t get_duration(AVFormatContext *s)
236{239{
237 DHAVContext *dhav = s->priv_data;
238 int64_t start_pos = avio_tell(s->pb);240 int64_t start_pos = avio_tell(s->pb);
241 int64_t end_pos = -1;
239 int64_t start = 0, end = 0;242 int64_t start = 0, end = 0;
240 struct tm timeinfo;243 struct tm timeinfo;
241 int max_interations = 100000;244 uint8_t *end_buffer;
245 int64_t end_buffer_size;
246 int64_t end_buffer_pos;
247 int64_t offset;
248 unsigned date;
242 249
243 if (!s->pb->seekable)250 if (!s->pb->seekable)
244 return 0;251 return 0;
245 252
246 avio_seek(s->pb, avio_size(s->pb) - 8, SEEK_SET);253 if (start_pos + 16 > avio_size(s->pb))
247 while (avio_tell(s->pb) > 12 && max_interations--) {254 return 0;
248 if (avio_rl32(s->pb) == MKTAG('d','h','a','v')) {
249 int64_t seek_back = avio_rl32(s->pb);
250 255
251 avio_seek(s->pb, -seek_back, SEEK_CUR);256 avio_skip(s->pb, 16);
252 read_chunk(s);257 date = avio_rl32(s->pb);
253 get_timeinfo(dhav->date, &timeinfo);258 get_timeinfo(date, &timeinfo);
254 end = av_timegm(&timeinfo) * 1000LL;259 start = av_timegm(&timeinfo) * 1000LL;
260
261 end_buffer_size = FFMIN(MAX_DURATION_BUFFER_SIZE, avio_size(s->pb));
262 end_buffer = av_malloc(end_buffer_size);
263 if (!end_buffer) {
264 avio_seek(s->pb, start_pos, SEEK_SET);
265 return 0;
266 }
267 end_buffer_pos = avio_size(s->pb) - end_buffer_size;
268 avio_seek(s->pb, end_buffer_pos, SEEK_SET);
269 avio_read(s->pb, end_buffer, end_buffer_size);
270
271 offset = end_buffer_size - 8;
272 while (offset > 0) {
273 if (AV_RL32(end_buffer + offset) == MKTAG('d','h','a','v')) {
274 int64_t seek_back = AV_RL32(end_buffer + offset + 4);
275 end_pos = end_buffer_pos + offset - seek_back + 8;
255 break;276 break;
256 } else {277 } else {
257 avio_seek(s->pb, -12, SEEK_CUR);278 offset -= 9;
258 }279 }
259 }280 }
260 281
261 avio_seek(s->pb, start_pos, SEEK_SET);282 if (end_pos < 0 || end_pos + 16 > end_buffer_pos + end_buffer_size) {
283 avio_seek(s->pb, start_pos, SEEK_SET);
284 return 0;
285 }
262 286
263 read_chunk(s);287 date = AV_RL32(end_buffer + (end_pos - end_buffer_pos) + 16);
264 get_timeinfo(dhav->date, &timeinfo);288 get_timeinfo(date, &timeinfo);
265 start = av_timegm(&timeinfo) * 1000LL;289 end = av_timegm(&timeinfo) * 1000LL;
266 290
267 avio_seek(s->pb, start_pos, SEEK_SET);291 avio_seek(s->pb, start_pos, SEEK_SET);
268 292

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t