Additional fixes by Nigel Touati-Evans <nigel.touatievans@gmail.com>.
Check the index for streams with a time drift of 2s or a buffer drift
of 64MB.
Bug-Id: 666
CC: libav-stable@libav.org
Sample-Id: yet-another-broken-interleaved-avi.avi
Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
Signed-off-by: Luca Barbato <lu_zero@gentoo.org>
Signed-off-by: Diego Biurrun <diego@biurrun.de>
(cherry picked from commit 9d599e3f6e61438772d8cddd6c9b7c495251f51e)
Signed-off-by: Reinhard Tartler <siretart@tauware.de>
... | ... |
@@ -2,6 +2,7 @@ Entries are sorted chronologically from oldest to youngest within each release, |
2 | 2 |
releases are sorted from youngest to oldest. |
3 | 3 |
|
4 | 4 |
version 10.1: |
5 |
+- avi: Improve non-interleaved detection (bug/666) |
|
5 | 6 |
- arm: hpeldsp: fix put_pixels8_y2_{,no_rnd_}armv6 |
6 | 7 |
- arm: hpeldsp: prevent overreads in armv6 asm (bug/646) |
7 | 8 |
- avfilter: Add missing emms_c when needed |
... | ... |
@@ -795,7 +795,11 @@ fail: |
795 | 795 |
if (!avi->index_loaded && pb->seekable) |
796 | 796 |
avi_load_index(s); |
797 | 797 |
avi->index_loaded = 1; |
798 |
- avi->non_interleaved |= guess_ni_flag(s); |
|
798 |
+ |
|
799 |
+ if ((ret = guess_ni_flag(s)) < 0) |
|
800 |
+ return ret; |
|
801 |
+ |
|
802 |
+ avi->non_interleaved |= ret; |
|
799 | 803 |
for (i = 0; i < s->nb_streams; i++) { |
800 | 804 |
AVStream *st = s->streams[i]; |
801 | 805 |
if (st->nb_index_entries) |
... | ... |
@@ -1307,6 +1311,64 @@ static int avi_read_idx1(AVFormatContext *s, int size) |
1307 | 1307 |
return 0; |
1308 | 1308 |
} |
1309 | 1309 |
|
1310 |
+/* Scan the index and consider any file with streams more than |
|
1311 |
+ * 2 seconds or 64MB apart non-interleaved. */ |
|
1312 |
+static int check_stream_max_drift(AVFormatContext *s) |
|
1313 |
+{ |
|
1314 |
+ int64_t min_pos, pos; |
|
1315 |
+ int i; |
|
1316 |
+ int *idx = av_mallocz_array(s->nb_streams, sizeof(*idx)); |
|
1317 |
+ if (!idx) |
|
1318 |
+ return AVERROR(ENOMEM); |
|
1319 |
+ |
|
1320 |
+ for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) { |
|
1321 |
+ int64_t max_dts = INT64_MIN / 2; |
|
1322 |
+ int64_t min_dts = INT64_MAX / 2; |
|
1323 |
+ int64_t max_buffer = 0; |
|
1324 |
+ |
|
1325 |
+ min_pos = INT64_MAX; |
|
1326 |
+ |
|
1327 |
+ for (i = 0; i < s->nb_streams; i++) { |
|
1328 |
+ AVStream *st = s->streams[i]; |
|
1329 |
+ AVIStream *ast = st->priv_data; |
|
1330 |
+ int n = st->nb_index_entries; |
|
1331 |
+ while (idx[i] < n && st->index_entries[idx[i]].pos < pos) |
|
1332 |
+ idx[i]++; |
|
1333 |
+ if (idx[i] < n) { |
|
1334 |
+ int64_t dts; |
|
1335 |
+ dts = av_rescale_q(st->index_entries[idx[i]].timestamp / |
|
1336 |
+ FFMAX(ast->sample_size, 1), |
|
1337 |
+ st->time_base, AV_TIME_BASE_Q); |
|
1338 |
+ min_dts = FFMIN(min_dts, dts); |
|
1339 |
+ min_pos = FFMIN(min_pos, st->index_entries[idx[i]].pos); |
|
1340 |
+ } |
|
1341 |
+ } |
|
1342 |
+ for (i = 0; i < s->nb_streams; i++) { |
|
1343 |
+ AVStream *st = s->streams[i]; |
|
1344 |
+ AVIStream *ast = st->priv_data; |
|
1345 |
+ |
|
1346 |
+ if (idx[i] && min_dts != INT64_MAX / 2) { |
|
1347 |
+ int64_t dts; |
|
1348 |
+ dts = av_rescale_q(st->index_entries[idx[i] - 1].timestamp / |
|
1349 |
+ FFMAX(ast->sample_size, 1), |
|
1350 |
+ st->time_base, AV_TIME_BASE_Q); |
|
1351 |
+ max_dts = FFMAX(max_dts, dts); |
|
1352 |
+ max_buffer = FFMAX(max_buffer, |
|
1353 |
+ av_rescale(dts - min_dts, |
|
1354 |
+ st->codec->bit_rate, |
|
1355 |
+ AV_TIME_BASE)); |
|
1356 |
+ } |
|
1357 |
+ } |
|
1358 |
+ if (max_dts - min_dts > 2 * AV_TIME_BASE || |
|
1359 |
+ max_buffer > 1024 * 1024 * 8 * 8) { |
|
1360 |
+ av_free(idx); |
|
1361 |
+ return 1; |
|
1362 |
+ } |
|
1363 |
+ } |
|
1364 |
+ av_free(idx); |
|
1365 |
+ return 0; |
|
1366 |
+} |
|
1367 |
+ |
|
1310 | 1368 |
static int guess_ni_flag(AVFormatContext *s) |
1311 | 1369 |
{ |
1312 | 1370 |
int i; |
... | ... |
@@ -1336,7 +1398,11 @@ static int guess_ni_flag(AVFormatContext *s) |
1336 | 1336 |
first_end = st->index_entries[n - 1].pos; |
1337 | 1337 |
} |
1338 | 1338 |
avio_seek(s->pb, oldpos, SEEK_SET); |
1339 |
- return last_start > first_end; |
|
1339 |
+ |
|
1340 |
+ if (last_start > first_end) |
|
1341 |
+ return 1; |
|
1342 |
+ |
|
1343 |
+ return check_stream_max_drift(s); |
|
1340 | 1344 |
} |
1341 | 1345 |
|
1342 | 1346 |
static int avi_load_index(AVFormatContext *s) |