FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avidec.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 856 1181 72.5%
Branches: 546 889 61.4%

Line Branch Exec Source
1 /*
2 * AVI demuxer
3 * Copyright (c) 2001 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config_components.h"
23
24 #include <inttypes.h>
25
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mathematics.h"
33 #include "avformat.h"
34 #include "avi.h"
35 #include "demux.h"
36 #include "dv.h"
37 #include "internal.h"
38 #include "isom.h"
39 #include "riff.h"
40 #include "libavcodec/bytestream.h"
41 #include "libavcodec/exif.h"
42 #include "libavcodec/startcode.h"
43
44 typedef struct AVIStream {
45 int64_t frame_offset; /* current frame (video) or byte (audio) counter
46 * (used to compute the pts) */
47 int remaining;
48 int packet_size;
49
50 uint32_t handler;
51 uint32_t scale;
52 uint32_t rate;
53 int sample_size; /* size of one sample (or packet)
54 * (in the rate/scale sense) in bytes */
55
56 int64_t cum_len; /* temporary storage (used during seek) */
57 int prefix; /* normally 'd'<<8 + 'c' or 'w'<<8 + 'b' */
58 int prefix_count;
59 uint32_t pal[256];
60 int has_pal;
61 int dshow_block_align; /* block align variable used to emulate bugs in
62 * the MS dshow demuxer */
63
64 AVFormatContext *sub_ctx;
65 AVPacket *sub_pkt;
66 AVBufferRef *sub_buffer;
67
68 int64_t seek_pos;
69 } AVIStream;
70
71 typedef struct AVIContext {
72 const AVClass *class;
73 int64_t riff_end;
74 int64_t movi_end;
75 int64_t fsize;
76 int64_t io_fsize;
77 int64_t movi_list;
78 int64_t last_pkt_pos;
79 int index_loaded;
80 int is_odml;
81 int non_interleaved;
82 int stream_index;
83 DVDemuxContext *dv_demux;
84 int odml_depth;
85 int64_t odml_read;
86 int64_t odml_max_pos;
87 int use_odml;
88 #define MAX_ODML_DEPTH 1000
89 int64_t dts_max;
90 } AVIContext;
91
92
93 static const AVOption options[] = {
94 { "use_odml", "use odml index", offsetof(AVIContext, use_odml), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM},
95 { NULL },
96 };
97
98 static const AVClass demuxer_class = {
99 .class_name = "avi",
100 .item_name = av_default_item_name,
101 .option = options,
102 .version = LIBAVUTIL_VERSION_INT,
103 .category = AV_CLASS_CATEGORY_DEMUXER,
104 };
105
106
107 static const char avi_headers[][8] = {
108 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' },
109 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' },
110 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19 },
111 { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' },
112 { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' },
113 { 0 }
114 };
115
116 static const AVMetadataConv avi_metadata_conv[] = {
117 { "strn", "title" },
118 { "isbj", "subject" },
119 { "inam", "title" },
120 { "iart", "artist" },
121 { "icop", "copyright" },
122 { "icmt", "comment" },
123 { "ignr", "genre" },
124 { "iprd", "product" },
125 { "isft", "software" },
126
127 { 0 },
128 };
129
130 static int avi_load_index(AVFormatContext *s);
131 static int guess_ni_flag(AVFormatContext *s);
132
133 #define print_tag(s, str, tag, size) \
134 av_log(s, AV_LOG_TRACE, "pos:%"PRIX64" %s: tag=%s size=0x%x\n", \
135 avio_tell(pb), str, av_fourcc2str(tag), size) \
136
137 59763 static inline int get_duration(AVIStream *ast, int len)
138 {
139
2/2
✓ Branch 0 taken 9235 times.
✓ Branch 1 taken 50528 times.
59763 if (ast->sample_size)
140 9235 return len;
141
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 50316 times.
50528 else if (ast->dshow_block_align)
142 212 return (len + (int64_t)ast->dshow_block_align - 1) / ast->dshow_block_align;
143 else
144 50316 return 1;
145 }
146
147 446 static int get_riff(AVFormatContext *s, AVIOContext *pb)
148 {
149 446 AVIContext *avi = s->priv_data;
150 446 char header[8] = {0};
151 int i;
152
153 /* check RIFF header */
154 446 avio_read(pb, header, 4);
155 446 avi->riff_end = avio_rl32(pb); /* RIFF chunk size */
156 446 avi->riff_end += avio_tell(pb); /* RIFF chunk end */
157 446 avio_read(pb, header + 4, 4);
158
159
1/2
✓ Branch 0 taken 454 times.
✗ Branch 1 not taken.
454 for (i = 0; avi_headers[i][0]; i++)
160
2/2
✓ Branch 0 taken 446 times.
✓ Branch 1 taken 8 times.
454 if (!memcmp(header, avi_headers[i], 8))
161 446 break;
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 446 times.
446 if (!avi_headers[i][0])
163 return AVERROR_INVALIDDATA;
164
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 446 times.
446 if (header[7] == 0x19)
166 av_log(s, AV_LOG_INFO,
167 "This file has been generated by a totally broken muxer.\n");
168
169 446 return 0;
170 }
171
172 40 static int read_odml_index(AVFormatContext *s, int64_t frame_num)
173 {
174 40 AVIContext *avi = s->priv_data;
175 40 AVIOContext *pb = s->pb;
176 40 int longs_per_entry = avio_rl16(pb);
177 40 int index_sub_type = avio_r8(pb);
178 40 int index_type = avio_r8(pb);
179 40 int entries_in_use = avio_rl32(pb);
180 40 int chunk_id = avio_rl32(pb);
181 40 int64_t base = avio_rl64(pb);
182 40 int stream_id = ((chunk_id & 0xFF) - '0') * 10 +
183 40 ((chunk_id >> 8 & 0xFF) - '0');
184 AVStream *st;
185 AVIStream *ast;
186 int i;
187 40 int64_t last_pos = -1;
188 40 int64_t filesize = avi->fsize;
189
190 40 av_log(s, AV_LOG_TRACE,
191 "longs_per_entry:%d index_type:%d entries_in_use:%d "
192 "chunk_id:%X base:%16"PRIX64" frame_num:%"PRId64"\n",
193 longs_per_entry,
194 index_type,
195 entries_in_use,
196 chunk_id,
197 base,
198 frame_num);
199
200
3/4
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 29 times.
40 if (stream_id >= s->nb_streams || stream_id < 0)
201 11 return AVERROR_INVALIDDATA;
202 29 st = s->streams[stream_id];
203 29 ast = st->priv_data;
204
205
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 29 times.
29 if (index_sub_type || entries_in_use < 0)
206 return AVERROR_INVALIDDATA;
207
208 29 avio_rl32(pb);
209
210
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
29 if (index_type && longs_per_entry != 2)
211 return AVERROR_INVALIDDATA;
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (index_type > 1)
213 return AVERROR_INVALIDDATA;
214
215
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 29 times.
29 if (filesize > 0 && base >= filesize) {
216 av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
217 if (base >> 32 == (base & 0xFFFFFFFF) &&
218 (base & 0xFFFFFFFF) < filesize &&
219 filesize <= 0xFFFFFFFF)
220 base &= 0xFFFFFFFF;
221 else
222 return AVERROR_INVALIDDATA;
223 }
224
225
2/2
✓ Branch 0 taken 5695 times.
✓ Branch 1 taken 18 times.
5713 for (i = 0; i < entries_in_use; i++) {
226
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5692 times.
5695 avi->odml_max_pos = FFMAX(avi->odml_max_pos, avio_tell(pb));
227
228 // If we read more than there are bytes then we must have been reading something twice
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5695 times.
5695 if (avi->odml_read > avi->odml_max_pos)
230 return AVERROR_INVALIDDATA;
231
232
2/2
✓ Branch 0 taken 5675 times.
✓ Branch 1 taken 20 times.
5695 if (index_type) {
233 5675 int64_t pos = avio_rl32(pb) + base - 8;
234 5675 int len = avio_rl32(pb);
235 5675 int key = len >= 0;
236 5675 len &= 0x7FFFFFFF;
237 5675 avi->odml_read += 8;
238
239 5675 av_log(s, AV_LOG_TRACE, "pos:%"PRId64", len:%X\n", pos, len);
240
241
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5675 times.
5675 if (avio_feof(pb))
242 return AVERROR_INVALIDDATA;
243
244
3/4
✓ Branch 0 taken 5675 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5673 times.
5675 if (last_pos == pos || pos == base - 8)
245 2 avi->non_interleaved = 1;
246
3/4
✓ Branch 0 taken 5675 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5457 times.
✓ Branch 3 taken 218 times.
5675 if (last_pos != pos && len)
247 5457 av_add_index_entry(st, pos, ast->cum_len, len, 0,
248 key ? AVINDEX_KEYFRAME : 0);
249
250 5675 ast->cum_len += get_duration(ast, len);
251 5675 last_pos = pos;
252 } else {
253 int64_t offset, pos;
254 int duration;
255 int ret;
256 20 avi->odml_read += 16;
257
258 20 offset = avio_rl64(pb);
259 20 avio_rl32(pb); /* size */
260 20 duration = avio_rl32(pb);
261
262
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
20 if (avio_feof(pb) || offset > INT64_MAX - 8)
263 return AVERROR_INVALIDDATA;
264
265 20 pos = avio_tell(pb);
266
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (avi->odml_depth > MAX_ODML_DEPTH) {
268 av_log(s, AV_LOG_ERROR, "Too deeply nested ODML indexes\n");
269 return AVERROR_INVALIDDATA;
270 }
271
272
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if (avio_seek(pb, offset + 8, SEEK_SET) < 0)
273 return -1;
274 20 avi->odml_depth++;
275 20 ret = read_odml_index(s, frame_num);
276 20 avi->odml_depth--;
277 20 frame_num += duration;
278
279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if (avio_seek(pb, pos, SEEK_SET) < 0) {
280 av_log(s, AV_LOG_ERROR, "Failed to restore position after reading index\n");
281 return -1;
282 }
283
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9 times.
20 if (ret < 0)
284 11 return ret;
285 }
286 }
287 18 avi->index_loaded = 2;
288 18 return 0;
289 }
290
291 7 static void clean_index(AVFormatContext *s)
292 {
293 int i;
294 int64_t j;
295
296
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 7 times.
19 for (i = 0; i < s->nb_streams; i++) {
297 12 AVStream *st = s->streams[i];
298 12 FFStream *const sti = ffstream(st);
299 12 AVIStream *ast = st->priv_data;
300 12 int n = sti->nb_index_entries;
301 12 int max = ast->sample_size;
302 int64_t pos, size, ts;
303
304
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
12 if (n != 1 || ast->sample_size == 0)
305 10 continue;
306
307
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 while (max < 1024)
308 18 max += max;
309
310 2 pos = sti->index_entries[0].pos;
311 2 size = sti->index_entries[0].size;
312 2 ts = sti->index_entries[0].timestamp;
313
314
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 2 times.
15 for (j = 0; j < size; j += max)
315 13 av_add_index_entry(st, pos + j, ts + j, FFMIN(max, size - j), 0,
316 AVINDEX_KEYFRAME);
317 }
318 7 }
319
320 19 static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag,
321 uint32_t size)
322 {
323 19 AVIOContext *pb = s->pb;
324 19 char key[5] = { 0 };
325 char *value;
326
327 19 size += (size & 1);
328
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (size == UINT_MAX)
330 return AVERROR(EINVAL);
331 19 value = av_malloc(size + 1);
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!value)
333 return AVERROR(ENOMEM);
334
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if (avio_read(pb, value, size) != size) {
335 av_freep(&value);
336 return AVERROR_INVALIDDATA;
337 }
338 19 value[size] = 0;
339
340 19 AV_WL32(key, tag);
341
342
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 return av_dict_set(st ? &st->metadata : &s->metadata, key, value,
343 AV_DICT_DONT_STRDUP_VAL);
344 }
345
346 static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
347 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
348
349 1 static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
350 {
351 char month[4], time[9], buffer[64];
352 int i, day, year;
353 /* parse standard AVI date format (ie. "Mon Mar 10 15:04:43 2003") */
354
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
355 month, &day, time, &year) == 4) {
356
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
13 for (i = 0; i < 12; i++)
357
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 11 times.
12 if (!av_strcasecmp(month, months[i])) {
358 1 snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s",
359 year, i + 1, day, time);
360 1 av_dict_set(metadata, "creation_time", buffer, 0);
361 }
362 } else if (date[4] == '/' && date[7] == '/') {
363 date[4] = date[7] = '-';
364 av_dict_set(metadata, "creation_time", date, 0);
365 }
366 1 }
367
368 static void avi_read_nikon(AVFormatContext *s, uint64_t end)
369 {
370 while (avio_tell(s->pb) < end && !avio_feof(s->pb)) {
371 uint32_t tag = avio_rl32(s->pb);
372 uint32_t size = avio_rl32(s->pb);
373 switch (tag) {
374 case MKTAG('n', 'c', 't', 'g'): /* Nikon Tags */
375 {
376 uint64_t tag_end = avio_tell(s->pb) + size;
377 while (avio_tell(s->pb) < tag_end && !avio_feof(s->pb)) {
378 uint16_t tag = avio_rl16(s->pb);
379 uint16_t size = avio_rl16(s->pb);
380 const char *name = NULL;
381 char buffer[64] = { 0 };
382 uint64_t remaining = tag_end - avio_tell(s->pb);
383 size = FFMIN(size, remaining);
384 size -= avio_read(s->pb, buffer,
385 FFMIN(size, sizeof(buffer) - 1));
386 switch (tag) {
387 case 0x03:
388 name = "maker";
389 break;
390 case 0x04:
391 name = "model";
392 break;
393 case 0x13:
394 name = "creation_time";
395 if (buffer[4] == ':' && buffer[7] == ':')
396 buffer[4] = buffer[7] = '-';
397 break;
398 }
399 if (name)
400 av_dict_set(&s->metadata, name, buffer, 0);
401 avio_skip(s->pb, size);
402 }
403 break;
404 }
405 default:
406 avio_skip(s->pb, size);
407 break;
408 }
409 }
410 }
411
412 6 static int avi_extract_stream_metadata(AVFormatContext *s, AVStream *st)
413 {
414 GetByteContext gb;
415 6 uint8_t *data = st->codecpar->extradata;
416 6 int data_size = st->codecpar->extradata_size;
417 int tag, offset;
418
419
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!data || data_size < 8) {
420 return AVERROR_INVALIDDATA;
421 }
422
423 6 bytestream2_init(&gb, data, data_size);
424
425 6 tag = bytestream2_get_le32(&gb);
426
427
1/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 switch (tag) {
428 case MKTAG('A', 'V', 'I', 'F'):
429 // skip 4 byte padding
430 bytestream2_skip(&gb, 4);
431 offset = bytestream2_tell(&gb);
432
433 // decode EXIF tags from IFD, AVI is always little-endian
434 return avpriv_exif_decode_ifd(s, data + offset, data_size - offset,
435 1, 0, &st->metadata);
436 break;
437 case MKTAG('C', 'A', 'S', 'I'):
438 avpriv_request_sample(s, "RIFF stream data tag type CASI (%u)", tag);
439 break;
440 case MKTAG('Z', 'o', 'r', 'a'):
441 avpriv_request_sample(s, "RIFF stream data tag type Zora (%u)", tag);
442 break;
443 6 default:
444 6 break;
445 }
446
447 6 return 0;
448 }
449
450 446 static int calculate_bitrate(AVFormatContext *s)
451 {
452 446 AVIContext *avi = s->priv_data;
453 int i, j;
454 446 int64_t lensum = 0;
455 446 int64_t maxpos = 0;
456
457
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 446 times.
935 for (i = 0; i<s->nb_streams; i++) {
458 489 int64_t len = 0;
459 489 FFStream *const sti = ffstream(s->streams[i]);
460
461
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 431 times.
489 if (!sti->nb_index_entries)
462 58 continue;
463
464
2/2
✓ Branch 0 taken 29525 times.
✓ Branch 1 taken 431 times.
29956 for (j = 0; j < sti->nb_index_entries; j++)
465 29525 len += sti->index_entries[j].size;
466 431 maxpos = FFMAX(maxpos, sti->index_entries[j-1].pos);
467 431 lensum += len;
468 }
469
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 345 times.
446 if (maxpos < av_rescale(avi->io_fsize, 9, 10)) // index does not cover the whole file
470 101 return 0;
471
3/4
✓ Branch 0 taken 345 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 318 times.
345 if (lensum*9/10 > maxpos || lensum < maxpos*9/10) // frame sum and filesize mismatch
472 27 return 0;
473
474
2/2
✓ Branch 0 taken 338 times.
✓ Branch 1 taken 318 times.
656 for (i = 0; i<s->nb_streams; i++) {
475 338 int64_t len = 0;
476 338 AVStream *st = s->streams[i];
477 338 FFStream *const sti = ffstream(st);
478 int64_t duration;
479 int64_t bitrate;
480
481
2/2
✓ Branch 0 taken 26340 times.
✓ Branch 1 taken 338 times.
26678 for (j = 0; j < sti->nb_index_entries; j++)
482 26340 len += sti->index_entries[j].size;
483
484
4/4
✓ Branch 0 taken 336 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 21 times.
✓ Branch 3 taken 315 times.
338 if (sti->nb_index_entries < 2 || st->codecpar->bit_rate > 0)
485 23 continue;
486 315 duration = sti->index_entries[j-1].timestamp - sti->index_entries[0].timestamp;
487 315 bitrate = av_rescale(8*len, st->time_base.den, duration * st->time_base.num);
488
1/2
✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
315 if (bitrate > 0) {
489 315 st->codecpar->bit_rate = bitrate;
490 }
491 }
492 318 return 1;
493 }
494
495 446 static int avi_read_header(AVFormatContext *s)
496 {
497 446 AVIContext *avi = s->priv_data;
498 446 AVIOContext *pb = s->pb;
499 unsigned int tag, tag1, handler;
500 int codec_type, stream_index, frame_period;
501 unsigned int size;
502 int i;
503 AVStream *st;
504 446 AVIStream *ast = NULL;
505 446 int avih_width = 0, avih_height = 0;
506 446 int amv_file_format = 0;
507 446 uint64_t list_end = 0;
508 int64_t pos;
509 int ret;
510 AVDictionaryEntry *dict_entry;
511
512 446 avi->stream_index = -1;
513
514 446 ret = get_riff(s, pb);
515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 446 times.
446 if (ret < 0)
516 return ret;
517
518 446 av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
519
520 446 avi->io_fsize = avi->fsize = avio_size(pb);
521
3/4
✓ Branch 0 taken 446 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✓ Branch 3 taken 409 times.
446 if (avi->fsize <= 0 || avi->fsize < avi->riff_end)
522
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 avi->fsize = avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
523
524 /* first list tag */
525 446 stream_index = -1;
526 446 codec_type = -1;
527 446 frame_period = 0;
528 3956 for (;;) {
529
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4402 times.
4402 if (avio_feof(pb))
530 return AVERROR_INVALIDDATA;
531 4402 tag = avio_rl32(pb);
532 4402 size = avio_rl32(pb);
533
534 4402 print_tag(s, "tag", tag, size);
535
536
12/13
✓ Branch 0 taken 1498 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 444 times.
✓ Branch 5 taken 490 times.
✓ Branch 6 taken 489 times.
✓ Branch 7 taken 11 times.
✓ Branch 8 taken 20 times.
✓ Branch 9 taken 10 times.
✓ Branch 10 taken 19 times.
✓ Branch 11 taken 1379 times.
✗ Branch 12 not taken.
4402 switch (tag) {
537 1498 case MKTAG('L', 'I', 'S', 'T'):
538 1498 list_end = avio_tell(pb) + size;
539 /* Ignored, except at start of video packets. */
540 1498 tag1 = avio_rl32(pb);
541
542 1498 print_tag(s, "list", tag1, 0);
543
544
2/2
✓ Branch 0 taken 446 times.
✓ Branch 1 taken 1052 times.
1498 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
545 446 avi->movi_list = avio_tell(pb) - 4;
546
2/2
✓ Branch 0 taken 444 times.
✓ Branch 1 taken 2 times.
446 if (size)
547 444 avi->movi_end = avi->movi_list + size + (size & 1);
548 else
549 2 avi->movi_end = avi->fsize;
550 446 av_log(s, AV_LOG_TRACE, "movi end=%"PRIx64"\n", avi->movi_end);
551 446 goto end_of_header;
552
2/2
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 991 times.
1052 } else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
553 61 ff_read_riff_info(s, size - 4);
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 991 times.
991 else if (tag1 == MKTAG('n', 'c', 'd', 't'))
555 avi_read_nikon(s, list_end);
556
557 3956 break;
558 1 case MKTAG('I', 'D', 'I', 'T'):
559 {
560 1 unsigned char date[64] = { 0 };
561 1 size += (size & 1);
562 1 size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
563 1 avio_skip(pb, size);
564 1 avi_metadata_creation_time(&s->metadata, date);
565 1 break;
566 }
567 39 case MKTAG('d', 'm', 'l', 'h'):
568 39 avi->is_odml = 1;
569 39 avio_skip(pb, size + (size & 1));
570 39 break;
571 2 case MKTAG('a', 'm', 'v', 'h'):
572 2 amv_file_format = 1;
573 446 case MKTAG('a', 'v', 'i', 'h'):
574 /* AVI header */
575 /* using frame_period is bad idea */
576 446 frame_period = avio_rl32(pb);
577 446 avio_rl32(pb); /* max. bytes per second */
578 446 avio_rl32(pb);
579 446 avi->non_interleaved |= avio_rl32(pb) & AVIF_MUSTUSEINDEX;
580
581 446 avio_skip(pb, 2 * 4);
582 446 avio_rl32(pb);
583 446 avio_rl32(pb);
584 446 avih_width = avio_rl32(pb);
585 446 avih_height = avio_rl32(pb);
586
587 446 avio_skip(pb, size - 10 * 4);
588 446 break;
589 490 case MKTAG('s', 't', 'r', 'h'):
590 /* stream header */
591
592 490 tag1 = avio_rl32(pb);
593 490 handler = avio_rl32(pb); /* codec tag */
594
595
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 489 times.
490 if (tag1 == MKTAG('p', 'a', 'd', 's')) {
596 1 avio_skip(pb, size - 8);
597 1 break;
598 } else {
599 489 stream_index++;
600 489 st = avformat_new_stream(s, NULL);
601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 489 times.
489 if (!st)
602 return AVERROR(ENOMEM);
603
604 489 st->id = stream_index;
605 489 ast = av_mallocz(sizeof(AVIStream));
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 489 times.
489 if (!ast)
607 return AVERROR(ENOMEM);
608 489 st->priv_data = ast;
609 }
610
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 485 times.
489 if (amv_file_format)
611 4 tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
612
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 : MKTAG('v', 'i', 'd', 's');
613
614 489 print_tag(s, "strh", tag1, -1);
615
616
2/4
✓ Branch 0 taken 489 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 489 times.
489 if (tag1 == MKTAG('i', 'a', 'v', 's') ||
617 tag1 == MKTAG('i', 'v', 'a', 's')) {
618 int64_t dv_dur;
619
620 /* After some consideration -- I don't think we
621 * have to support anything but DV in type1 AVIs. */
622 if (s->nb_streams != 1)
623 return AVERROR_INVALIDDATA;
624
625 if (handler != MKTAG('d', 'v', 's', 'd') &&
626 handler != MKTAG('d', 'v', 'h', 'd') &&
627 handler != MKTAG('d', 'v', 's', 'l'))
628 return AVERROR_INVALIDDATA;
629
630 if (!CONFIG_DV_DEMUXER)
631 return AVERROR_DEMUXER_NOT_FOUND;
632
633 ast = s->streams[0]->priv_data;
634 st->priv_data = NULL;
635 ff_remove_stream(s, st);
636
637 avi->dv_demux = avpriv_dv_init_demux(s);
638 if (!avi->dv_demux) {
639 av_free(ast);
640 return AVERROR(ENOMEM);
641 }
642
643 s->streams[0]->priv_data = ast;
644 avio_skip(pb, 3 * 4);
645 ast->scale = avio_rl32(pb);
646 ast->rate = avio_rl32(pb);
647 avio_skip(pb, 4); /* start time */
648
649 dv_dur = avio_rl32(pb);
650 if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
651 dv_dur *= AV_TIME_BASE;
652 s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
653 }
654 /* else, leave duration alone; timing estimation in utils.c
655 * will make a guess based on bitrate. */
656
657 stream_index = s->nb_streams - 1;
658 avio_skip(pb, size - 9 * 4);
659 break;
660 }
661
662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 489 times.
489 av_assert0(stream_index < s->nb_streams);
663 489 ast->handler = handler;
664
665 489 avio_rl32(pb); /* flags */
666 489 avio_rl16(pb); /* priority */
667 489 avio_rl16(pb); /* language */
668 489 avio_rl32(pb); /* initial frame */
669 489 ast->scale = avio_rl32(pb);
670 489 ast->rate = avio_rl32(pb);
671
3/4
✓ Branch 0 taken 485 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 485 times.
489 if (!(ast->scale && ast->rate)) {
672 4 av_log(s, AV_LOG_WARNING,
673 "scale/rate is %"PRIu32"/%"PRIu32" which is invalid. "
674 "(This file has been generated by broken software.)\n",
675 ast->scale,
676 ast->rate);
677
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (frame_period) {
678 4 ast->rate = 1000000;
679 4 ast->scale = frame_period;
680 } else {
681 ast->rate = 25;
682 ast->scale = 1;
683 }
684 }
685 489 avpriv_set_pts_info(st, 64, ast->scale, ast->rate);
686
687 489 ast->cum_len = avio_rl32(pb); /* start */
688 489 st->nb_frames = avio_rl32(pb);
689
690 489 st->start_time = 0;
691 489 avio_rl32(pb); /* buffer size */
692 489 avio_rl32(pb); /* quality */
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 489 times.
489 if (ast->cum_len > 3600LL * ast->rate / ast->scale) {
694 av_log(s, AV_LOG_ERROR, "crazy start time, iam scared, giving up\n");
695 ast->cum_len = 0;
696 }
697 489 ast->sample_size = avio_rl32(pb);
698 489 ast->cum_len *= FFMAX(1, ast->sample_size);
699 489 av_log(s, AV_LOG_TRACE, "%"PRIu32" %"PRIu32" %d\n",
700 ast->rate, ast->scale, ast->sample_size);
701
702 switch (tag1) {
703 443 case MKTAG('v', 'i', 'd', 's'):
704 443 codec_type = AVMEDIA_TYPE_VIDEO;
705
706 443 ast->sample_size = 0;
707 443 st->avg_frame_rate = av_inv_q(st->time_base);
708 443 break;
709 46 case MKTAG('a', 'u', 'd', 's'):
710 46 codec_type = AVMEDIA_TYPE_AUDIO;
711 46 break;
712 case MKTAG('t', 'x', 't', 's'):
713 codec_type = AVMEDIA_TYPE_SUBTITLE;
714 break;
715 case MKTAG('d', 'a', 't', 's'):
716 codec_type = AVMEDIA_TYPE_DATA;
717 break;
718 default:
719 av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
720 }
721
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 489 times.
489 if (ast->sample_size < 0) {
723 if (s->error_recognition & AV_EF_EXPLODE) {
724 av_log(s, AV_LOG_ERROR,
725 "Invalid sample_size %d at stream %d\n",
726 ast->sample_size,
727 stream_index);
728 return AVERROR_INVALIDDATA;
729 }
730 av_log(s, AV_LOG_WARNING,
731 "Invalid sample_size %d at stream %d "
732 "setting it to 0\n",
733 ast->sample_size,
734 stream_index);
735 ast->sample_size = 0;
736 }
737
738
2/2
✓ Branch 0 taken 453 times.
✓ Branch 1 taken 36 times.
489 if (ast->sample_size == 0) {
739 453 st->duration = st->nb_frames;
740
5/6
✓ Branch 0 taken 448 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 448 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✓ Branch 5 taken 410 times.
453 if (st->duration > 0 && avi->io_fsize > 0 && avi->riff_end > avi->io_fsize) {
741 38 av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
742 38 st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
743 }
744 }
745 489 ast->frame_offset = ast->cum_len;
746 489 avio_skip(pb, size - 12 * 4);
747 489 break;
748 489 case MKTAG('s', 't', 'r', 'f'):
749 /* stream header */
750
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 489 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
489 if (!size && (codec_type == AVMEDIA_TYPE_AUDIO ||
751 codec_type == AVMEDIA_TYPE_VIDEO))
752 break;
753
2/4
✓ Branch 0 taken 489 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 489 times.
489 if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
754 avio_skip(pb, size);
755 } else {
756 489 uint64_t cur_pos = avio_tell(pb);
757 FFStream *sti;
758 unsigned esize;
759
2/2
✓ Branch 0 taken 485 times.
✓ Branch 1 taken 4 times.
489 if (cur_pos < list_end)
760 485 size = FFMIN(size, list_end - cur_pos);
761 489 st = s->streams[stream_index];
762 489 sti = ffstream(st);
763
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 489 times.
489 if (st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN) {
764 avio_skip(pb, size);
765 break;
766 }
767 switch (codec_type) {
768 443 case AVMEDIA_TYPE_VIDEO:
769
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 441 times.
443 if (amv_file_format) {
770 2 st->codecpar->width = avih_width;
771 2 st->codecpar->height = avih_height;
772 2 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
773 2 st->codecpar->codec_id = AV_CODEC_ID_AMV;
774 2 avio_skip(pb, size);
775 489 break;
776 }
777 441 tag1 = ff_get_bmp_header(pb, st, &esize);
778
779
2/4
✓ Branch 0 taken 441 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 441 times.
441 if (tag1 == MKTAG('D', 'X', 'S', 'B') ||
780 tag1 == MKTAG('D', 'X', 'S', 'A')) {
781 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
782 st->codecpar->codec_tag = tag1;
783 st->codecpar->codec_id = AV_CODEC_ID_XSUB;
784 break;
785 }
786
787
4/6
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 291 times.
✓ Branch 2 taken 150 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 150 times.
✗ Branch 5 not taken.
441 if (size > 10 * 4 && size < (1 << 30) && size < avi->fsize) {
788
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
150 if (esize == size-1 && (esize&1)) {
789 12 st->codecpar->extradata_size = esize - 10 * 4;
790 } else
791 138 st->codecpar->extradata_size = size - 10 * 4;
792
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (st->codecpar->extradata) {
793 av_log(s, AV_LOG_WARNING, "New extradata in strf chunk, freeing previous one.\n");
794 }
795 150 ret = ff_get_extradata(s, st->codecpar, pb,
796 150 st->codecpar->extradata_size);
797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (ret < 0)
798 return ret;
799 }
800
801 // FIXME: check if the encoder really did this correctly
802
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 429 times.
441 if (st->codecpar->extradata_size & 1)
803 12 avio_r8(pb);
804
805 /* Extract palette from extradata if bpp <= 8.
806 * This code assumes that extradata contains only palette.
807 * This is true for all paletted codecs implemented in
808 * FFmpeg. */
809
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 291 times.
441 if (st->codecpar->extradata_size &&
810
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 139 times.
150 (st->codecpar->bits_per_coded_sample <= 8)) {
811 11 int pal_size = (1 << st->codecpar->bits_per_coded_sample) << 2;
812 const uint8_t *pal_src;
813
814 11 pal_size = FFMIN(pal_size, st->codecpar->extradata_size);
815 11 pal_src = st->codecpar->extradata +
816 11 st->codecpar->extradata_size - pal_size;
817 /* Exclude the "BottomUp" field from the palette */
818
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (pal_src - st->codecpar->extradata >= 9 &&
819 !memcmp(st->codecpar->extradata + st->codecpar->extradata_size - 9, "BottomUp", 9))
820 pal_src -= 9;
821
2/2
✓ Branch 0 taken 1306 times.
✓ Branch 1 taken 11 times.
1317 for (i = 0; i < pal_size / 4; i++)
822 1306 ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src + 4 * i);
823 11 ast->has_pal = 1;
824 }
825
826 441 print_tag(s, "video", tag1, 0);
827
828 441 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
829 441 st->codecpar->codec_tag = tag1;
830 441 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags,
831 tag1);
832 /* If codec is not found yet, try with the mov tags. */
833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 441 times.
441 if (!st->codecpar->codec_id) {
834 st->codecpar->codec_id =
835 ff_codec_get_id(ff_codec_movvideo_tags, tag1);
836 if (st->codecpar->codec_id)
837 av_log(s, AV_LOG_WARNING,
838 "mov tag found in avi (fourcc %s)\n",
839 av_fourcc2str(tag1));
840 }
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 441 times.
441 if (!st->codecpar->codec_id)
842 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags_unofficial, tag1);
843
844 /* This is needed to get the pict type which is necessary
845 * for generating correct pts. */
846 441 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
847
848
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 384 times.
441 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4 &&
849
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 55 times.
57 ast->handler == MKTAG('X', 'V', 'I', 'D'))
850 2 st->codecpar->codec_tag = MKTAG('X', 'V', 'I', 'D');
851
852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 441 times.
441 if (st->codecpar->codec_tag == MKTAG('V', 'S', 'S', 'H'))
853 sti->need_parsing = AVSTREAM_PARSE_FULL;
854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 441 times.
441 if (st->codecpar->codec_id == AV_CODEC_ID_RV40)
855 sti->need_parsing = AVSTREAM_PARSE_NONE;
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 441 times.
441 if (st->codecpar->codec_id == AV_CODEC_ID_HEVC &&
857 st->codecpar->codec_tag == MKTAG('H', '2', '6', '5'))
858 sti->need_parsing = AVSTREAM_PARSE_FULL;
859
860
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 440 times.
441 if (st->codecpar->codec_id == AV_CODEC_ID_AVRN &&
861
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 st->codecpar->codec_tag == MKTAG('A', 'V', 'R', 'n') &&
862
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (st->codecpar->extradata_size < 31 ||
863
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 memcmp(&st->codecpar->extradata[28], "1:1", 3)))
864 st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
865
866
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 428 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
441 if (st->codecpar->codec_tag == 0 && st->codecpar->height > 0 &&
867 st->codecpar->extradata_size < 1U << 30) {
868 st->codecpar->extradata_size += 9;
869 if ((ret = av_reallocp(&st->codecpar->extradata,
870 st->codecpar->extradata_size +
871 AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
872 st->codecpar->extradata_size = 0;
873 return ret;
874 } else
875 memcpy(st->codecpar->extradata + st->codecpar->extradata_size - 9,
876 "BottomUp", 9);
877 }
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 441 times.
441 if (st->codecpar->height == INT_MIN)
879 return AVERROR_INVALIDDATA;
880 441 st->codecpar->height = FFABS(st->codecpar->height);
881
882 // avio_skip(pb, size - 5 * 4);
883 441 break;
884 46 case AVMEDIA_TYPE_AUDIO:
885 46 ret = ff_get_wav_header(s, pb, st->codecpar, size, 0);
886
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (ret < 0)
887 return ret;
888 46 ast->dshow_block_align = st->codecpar->block_align;
889
3/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 36 times.
✗ Branch 3 not taken.
46 if (ast->sample_size && st->codecpar->block_align &&
890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 ast->sample_size != st->codecpar->block_align) {
891 av_log(s,
892 AV_LOG_WARNING,
893 "sample size (%d) != block align (%d)\n",
894 ast->sample_size,
895 st->codecpar->block_align);
896 ast->sample_size = st->codecpar->block_align;
897 }
898 /* 2-aligned
899 * (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
900
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (size & 1)
901 avio_skip(pb, 1);
902 /* Force parsing as several audio frames can be in
903 * one packet and timestamps refer to packet start. */
904 46 sti->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
905 /* ADTS header is in extradata, AAC without header must be
906 * stored as exact frames. Parser not needed and it will
907 * fail. */
908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
909 st->codecpar->extradata_size)
910 sti->need_parsing = AVSTREAM_PARSE_NONE;
911 // The flac parser does not work with AVSTREAM_PARSE_TIMESTAMPS
912
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
46 if (st->codecpar->codec_id == AV_CODEC_ID_FLAC)
913 sti->need_parsing = AVSTREAM_PARSE_NONE;
914 /* AVI files with Xan DPCM audio (wrongly) declare PCM
915 * audio in the header but have Axan as stream_code_tag. */
916
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 44 times.
46 if (ast->handler == AV_RL32("Axan")) {
917 2 st->codecpar->codec_id = AV_CODEC_ID_XAN_DPCM;
918 2 st->codecpar->codec_tag = 0;
919 2 ast->dshow_block_align = 0;
920 }
921
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 44 times.
46 if (amv_file_format) {
922 2 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_AMV;
923 2 ast->dshow_block_align = 0;
924 }
925
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if ((st->codecpar->codec_id == AV_CODEC_ID_AAC ||
926
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 st->codecpar->codec_id == AV_CODEC_ID_FTR ||
927
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
928
3/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
46 st->codecpar->codec_id == AV_CODEC_ID_MP2 ) && ast->dshow_block_align <= 4 && ast->dshow_block_align) {
929 av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
930 ast->dshow_block_align = 0;
931 }
932
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
46 if (st->codecpar->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
933
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 46 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
46 st->codecpar->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
934
3/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 44 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
46 st->codecpar->codec_id == AV_CODEC_ID_MP3 && ast->dshow_block_align == 1152 && ast->sample_size == 1152) {
935 av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
936 ast->sample_size = 0;
937 }
938 46 break;
939 case AVMEDIA_TYPE_SUBTITLE:
940 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
941 sti->request_probe = 1;
942 avio_skip(pb, size);
943 break;
944 default:
945 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
946 st->codecpar->codec_id = AV_CODEC_ID_NONE;
947 st->codecpar->codec_tag = 0;
948 avio_skip(pb, size);
949 break;
950 }
951 }
952 489 break;
953 11 case MKTAG('s', 't', 'r', 'd'):
954
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (stream_index >= (unsigned)s->nb_streams
955
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
11 || s->streams[stream_index]->codecpar->extradata_size
956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 || s->streams[stream_index]->codecpar->codec_tag == MKTAG('H','2','6','4')) {
957 5 avio_skip(pb, size);
958 } else {
959 6 uint64_t cur_pos = avio_tell(pb);
960
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (cur_pos < list_end)
961 6 size = FFMIN(size, list_end - cur_pos);
962 6 st = s->streams[stream_index];
963
964
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (size<(1<<30)) {
965
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (st->codecpar->extradata) {
966 av_log(s, AV_LOG_WARNING, "New extradata in strd chunk, freeing previous one.\n");
967 }
968
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
969 return ret;
970 }
971
972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (st->codecpar->extradata_size & 1) //FIXME check if the encoder really did this correctly
973 avio_r8(pb);
974
975 6 ret = avi_extract_stream_metadata(s, st);
976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
977 av_log(s, AV_LOG_WARNING, "could not decoding EXIF data in stream header.\n");
978 }
979 }
980 11 break;
981 20 case MKTAG('i', 'n', 'd', 'x'):
982 20 pos = avio_tell(pb);
983
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !(s->flags & AVFMT_FLAG_IGNIDX) &&
984
3/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 9 times.
40 avi->use_odml &&
985 20 read_odml_index(s, 0) < 0 &&
986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 (s->error_recognition & AV_EF_EXPLODE))
987 return AVERROR_INVALIDDATA;
988 20 avio_seek(pb, pos + size, SEEK_SET);
989 20 break;
990 10 case MKTAG('v', 'p', 'r', 'p'):
991
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 if (stream_index < (unsigned)s->nb_streams && size > 9 * 4) {
992 AVRational active, active_aspect;
993
994 10 st = s->streams[stream_index];
995 10 avio_rl32(pb);
996 10 avio_rl32(pb);
997 10 avio_rl32(pb);
998 10 avio_rl32(pb);
999 10 avio_rl32(pb);
1000
1001 10 active_aspect.den = avio_rl16(pb);
1002 10 active_aspect.num = avio_rl16(pb);
1003 10 active.num = avio_rl32(pb);
1004 10 active.den = avio_rl32(pb);
1005 10 avio_rl32(pb); // nbFieldsPerFrame
1006
1007
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 if (active_aspect.num && active_aspect.den &&
1008
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 active.num && active.den) {
1009 10 st->sample_aspect_ratio = av_div_q(active_aspect, active);
1010 10 av_log(s, AV_LOG_TRACE, "vprp %d/%d %d/%d\n",
1011 active_aspect.num, active_aspect.den,
1012 active.num, active.den);
1013 }
1014 10 size -= 9 * 4;
1015 }
1016 10 avio_skip(pb, size);
1017 10 break;
1018 19 case MKTAG('s', 't', 'r', 'n'):
1019 case MKTAG('i', 's', 'b', 'j'):
1020 case MKTAG('i', 'n', 'a', 'm'):
1021 case MKTAG('i', 'a', 'r', 't'):
1022 case MKTAG('i', 'c', 'o', 'p'):
1023 case MKTAG('i', 'c', 'm', 't'):
1024 case MKTAG('i', 'g', 'n', 'r'):
1025 case MKTAG('i', 'p', 'o', 'd'):
1026 case MKTAG('i', 's', 'o', 'f'):
1027
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (s->nb_streams) {
1028 19 ret = avi_read_tag(s, s->streams[s->nb_streams - 1], tag, size);
1029
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
1030 return ret;
1031 19 break;
1032 }
1033 default:
1034
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1379 times.
1379 if (size > 1000000) {
1035 av_log(s, AV_LOG_ERROR,
1036 "Something went wrong during header parsing, "
1037 "tag %s has size %u, "
1038 "I will ignore it and try to continue anyway.\n",
1039 av_fourcc2str(tag), size);
1040 if (s->error_recognition & AV_EF_EXPLODE)
1041 return AVERROR_INVALIDDATA;
1042 avi->movi_list = avio_tell(pb) - 4;
1043 avi->movi_end = avi->fsize;
1044 goto end_of_header;
1045 }
1046 /* Do not fail for very large idx1 tags */
1047 case MKTAG('i', 'd', 'x', '1'):
1048 /* skip tag */
1049 1379 size += (size & 1);
1050 1379 avio_skip(pb, size);
1051 1379 break;
1052 }
1053 }
1054
1055 446 end_of_header:
1056 /* check stream number */
1057
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 446 times.
446 if (stream_index != s->nb_streams - 1)
1058 return AVERROR_INVALIDDATA;
1059
1060
3/4
✓ Branch 0 taken 440 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 440 times.
✗ Branch 3 not taken.
446 if (!avi->index_loaded && (pb->seekable & AVIO_SEEKABLE_NORMAL))
1061 440 avi_load_index(s);
1062 446 calculate_bitrate(s);
1063 446 avi->index_loaded |= 1;
1064
1065
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 446 times.
446 if ((ret = guess_ni_flag(s)) < 0)
1066 return ret;
1067
1068 446 avi->non_interleaved |= ret | (s->flags & AVFMT_FLAG_SORT_DTS);
1069
1070 446 dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
1071
3/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 386 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
446 if (dict_entry && !strcmp(dict_entry->value, "PotEncoder"))
1072 for (i = 0; i < s->nb_streams; i++) {
1073 AVStream *st = s->streams[i];
1074 if ( st->codecpar->codec_id == AV_CODEC_ID_MPEG1VIDEO
1075 || st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1076 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
1077 }
1078
1079
2/2
✓ Branch 0 taken 465 times.
✓ Branch 1 taken 37 times.
502 for (i = 0; i < s->nb_streams; i++) {
1080 465 AVStream *st = s->streams[i];
1081
2/2
✓ Branch 1 taken 409 times.
✓ Branch 2 taken 56 times.
465 if (ffstream(st)->nb_index_entries)
1082 409 break;
1083 }
1084 // DV-in-AVI cannot be non-interleaved, if set this must be
1085 // a mis-detection.
1086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 446 times.
446 if (avi->dv_demux)
1087 avi->non_interleaved = 0;
1088
4/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 409 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 36 times.
446 if (i == s->nb_streams && avi->non_interleaved) {
1089 1 av_log(s, AV_LOG_WARNING,
1090 "Non-interleaved AVI without index, switching to interleaved\n");
1091 1 avi->non_interleaved = 0;
1092 }
1093
1094
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 439 times.
446 if (avi->non_interleaved) {
1095 7 av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
1096 7 clean_index(s);
1097 }
1098
1099 446 ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
1100 446 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
1101
1102 446 return 0;
1103 }
1104
1105 static int read_gab2_sub(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1106 {
1107 if (pkt->size >= 7 &&
1108 pkt->size < INT_MAX - AVPROBE_PADDING_SIZE &&
1109 !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
1110 uint8_t desc[256];
1111 int score = AVPROBE_SCORE_EXTENSION, ret;
1112 AVIStream *ast = st->priv_data;
1113 const AVInputFormat *sub_demuxer;
1114 AVRational time_base;
1115 int size;
1116 AVProbeData pd;
1117 unsigned int desc_len;
1118 AVIOContext *pb = avio_alloc_context(pkt->data + 7,
1119 pkt->size - 7,
1120 0, NULL, NULL, NULL, NULL);
1121 if (!pb)
1122 goto error;
1123
1124 desc_len = avio_rl32(pb);
1125
1126 if (desc_len > pb->buf_end - pb->buf_ptr)
1127 goto error;
1128
1129 ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
1130 avio_skip(pb, desc_len - ret);
1131 if (*desc)
1132 av_dict_set(&st->metadata, "title", desc, 0);
1133
1134 avio_rl16(pb); /* flags? */
1135 avio_rl32(pb); /* data size */
1136
1137 size = pb->buf_end - pb->buf_ptr;
1138 pd = (AVProbeData) { .buf = av_mallocz(size + AVPROBE_PADDING_SIZE),
1139 .buf_size = size };
1140 if (!pd.buf)
1141 goto error;
1142 memcpy(pd.buf, pb->buf_ptr, size);
1143 sub_demuxer = av_probe_input_format2(&pd, 1, &score);
1144 av_freep(&pd.buf);
1145 if (!sub_demuxer)
1146 goto error;
1147
1148 if (strcmp(sub_demuxer->name, "srt") && strcmp(sub_demuxer->name, "ass"))
1149 goto error;
1150
1151 if (!(ast->sub_pkt = av_packet_alloc()))
1152 goto error;
1153
1154 if (!(ast->sub_ctx = avformat_alloc_context()))
1155 goto error;
1156
1157 ast->sub_ctx->pb = pb;
1158
1159 if (ff_copy_whiteblacklists(ast->sub_ctx, s) < 0)
1160 goto error;
1161
1162 if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
1163 if (ast->sub_ctx->nb_streams != 1)
1164 goto error;
1165 ff_read_packet(ast->sub_ctx, ast->sub_pkt);
1166 avcodec_parameters_copy(st->codecpar, ast->sub_ctx->streams[0]->codecpar);
1167 time_base = ast->sub_ctx->streams[0]->time_base;
1168 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
1169 }
1170 ast->sub_buffer = pkt->buf;
1171 pkt->buf = NULL;
1172 av_packet_unref(pkt);
1173 return 1;
1174
1175 error:
1176 av_packet_free(&ast->sub_pkt);
1177 av_freep(&ast->sub_ctx);
1178 avio_context_free(&pb);
1179 }
1180 return 0;
1181 }
1182
1183 25898 static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
1184 AVPacket *pkt)
1185 {
1186 25898 AVIStream *ast, *next_ast = next_st->priv_data;
1187 25898 int64_t ts, next_ts, ts_min = INT64_MAX;
1188 25898 AVStream *st, *sub_st = NULL;
1189 int i;
1190
1191 25898 next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
1192 25898 AV_TIME_BASE_Q);
1193
1194
2/2
✓ Branch 0 taken 32747 times.
✓ Branch 1 taken 25898 times.
58645 for (i = 0; i < s->nb_streams; i++) {
1195 32747 st = s->streams[i];
1196 32747 ast = st->priv_data;
1197
4/8
✓ Branch 0 taken 28436 times.
✓ Branch 1 taken 4311 times.
✓ Branch 2 taken 28436 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 28436 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
32747 if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt && ast->sub_pkt->data) {
1198 ts = av_rescale_q(ast->sub_pkt->dts, st->time_base, AV_TIME_BASE_Q);
1199 if (ts <= next_ts && ts < ts_min) {
1200 ts_min = ts;
1201 sub_st = st;
1202 }
1203 }
1204 }
1205
1206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25898 times.
25898 if (sub_st) {
1207 ast = sub_st->priv_data;
1208 av_packet_move_ref(pkt, ast->sub_pkt);
1209 pkt->stream_index = sub_st->index;
1210
1211 if (ff_read_packet(ast->sub_ctx, ast->sub_pkt) < 0)
1212 ast->sub_pkt->data = NULL;
1213 }
1214 25898 return sub_st;
1215 }
1216
1217 495268 static int get_stream_idx(const unsigned *d)
1218 {
1219
4/4
✓ Branch 0 taken 274427 times.
✓ Branch 1 taken 220841 times.
✓ Branch 2 taken 87672 times.
✓ Branch 3 taken 186755 times.
495268 if (d[0] >= '0' && d[0] <= '9' &&
1220
4/4
✓ Branch 0 taken 87312 times.
✓ Branch 1 taken 360 times.
✓ Branch 2 taken 58846 times.
✓ Branch 3 taken 28466 times.
87672 d[1] >= '0' && d[1] <= '9') {
1221 58846 return (d[0] - '0') * 10 + (d[1] - '0');
1222 } else {
1223 436422 return 100; // invalid stream ID
1224 }
1225 }
1226
1227 /**
1228 *
1229 * @param exit_early set to 1 to just gather packet position without making the changes needed to actually read & return the packet
1230 */
1231 25927 static int avi_sync(AVFormatContext *s, int exit_early)
1232 {
1233 25927 AVIContext *avi = s->priv_data;
1234 25927 AVIOContext *pb = s->pb;
1235 int n;
1236 unsigned int d[8];
1237 unsigned int size;
1238 int64_t i, sync;
1239
1240 29946 start_sync:
1241 29946 memset(d, -1, sizeof(d));
1242
2/2
✓ Branch 2 taken 344814 times.
✓ Branch 3 taken 457 times.
345271 for (i = sync = avio_tell(pb); !avio_feof(pb); i++) {
1243 int j;
1244
1245
2/2
✓ Branch 0 taken 2413698 times.
✓ Branch 1 taken 344814 times.
2758512 for (j = 0; j < 7; j++)
1246 2413698 d[j] = d[j + 1];
1247 344814 d[7] = avio_r8(pb);
1248
1249 344814 size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
1250
1251 344814 n = get_stream_idx(d + 2);
1252 ff_tlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
1253 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
1254
4/4
✓ Branch 0 taken 122551 times.
✓ Branch 1 taken 222263 times.
✓ Branch 2 taken 12530 times.
✓ Branch 3 taken 110021 times.
344814 if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
1255 234793 continue;
1256
1257 // parse ix##
1258
5/6
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 109571 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 449 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
110021 if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
1259 // parse JUNK
1260
6/8
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 109552 times.
✓ Branch 2 taken 467 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 467 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 467 times.
110020 (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
1261
6/8
✓ Branch 0 taken 449 times.
✓ Branch 1 taken 109104 times.
✓ Branch 2 taken 448 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 448 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 448 times.
109553 (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1') ||
1262
4/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 109104 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
109105 (d[0] == 'i' && d[1] == 'n' && d[2] == 'd' && d[3] == 'x')) {
1263 916 avio_skip(pb, size);
1264 916 goto start_sync;
1265 }
1266
1267 // parse stray LIST
1268
6/8
✓ Branch 0 taken 647 times.
✓ Branch 1 taken 108458 times.
✓ Branch 2 taken 646 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 646 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 646 times.
✗ Branch 7 not taken.
109105 if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
1269 646 avio_skip(pb, 4);
1270 646 goto start_sync;
1271 }
1272
1273 108459 n = get_stream_idx(d);
1274
1275
2/2
✓ Branch 0 taken 41207 times.
✓ Branch 1 taken 67252 times.
108459 if (!((i - avi->last_pkt_pos) & 1) &&
1276
2/2
✓ Branch 1 taken 916 times.
✓ Branch 2 taken 40291 times.
41207 get_stream_idx(d + 1) < s->nb_streams)
1277 916 continue;
1278
1279 // detect ##ix chunk and skip
1280
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 107543 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
107543 if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
1281 avio_skip(pb, size);
1282 goto start_sync;
1283 }
1284
1285
3/6
✓ Branch 0 taken 6035 times.
✓ Branch 1 taken 101508 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6035 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
107543 if (d[2] == 'w' && d[3] == 'c' && n < s->nb_streams) {
1286 avio_skip(pb, 16 * 3 + 8);
1287 goto start_sync;
1288 }
1289
1290
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 107543 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
107543 if (avi->dv_demux && n != 0)
1291 continue;
1292
1293 // parse ##dc/##wb
1294
2/2
✓ Branch 0 taken 27927 times.
✓ Branch 1 taken 79616 times.
107543 if (n < s->nb_streams) {
1295 AVStream *st;
1296 AVIStream *ast;
1297 27927 st = s->streams[n];
1298 27927 ast = st->priv_data;
1299
1300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27927 times.
27927 if (!ast) {
1301 av_log(s, AV_LOG_WARNING, "Skipping foreign stream %d packet\n", n);
1302 continue;
1303 }
1304
1305
2/2
✓ Branch 0 taken 8757 times.
✓ Branch 1 taken 19170 times.
27927 if (s->nb_streams >= 2) {
1306 8757 AVStream *st1 = s->streams[1];
1307 8757 AVIStream *ast1 = st1->priv_data;
1308 // workaround for broken small-file-bug402.avi
1309
4/6
✓ Branch 0 taken 8757 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3430 times.
✓ Branch 3 taken 5327 times.
✓ Branch 4 taken 3430 times.
✗ Branch 5 not taken.
8757 if (ast1 && d[2] == 'w' && d[3] == 'b'
1310
2/2
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 3116 times.
3430 && n == 0
1311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 314 times.
314 && st ->codecpar->codec_type == AVMEDIA_TYPE_VIDEO
1312 && st1->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
1313 && ast->prefix == 'd'*256+'c'
1314 && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
1315 ) {
1316 n = 1;
1317 st = st1;
1318 ast = ast1;
1319 av_log(s, AV_LOG_WARNING,
1320 "Invalid stream + prefix combination, assuming audio.\n");
1321 }
1322 }
1323
1324
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 27923 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
27927 if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
1325 4 int k = avio_r8(pb);
1326 4 int last = (k + avio_r8(pb) - 1) & 0xFF;
1327
1328 4 avio_rl16(pb); // flags
1329
1330 // b + (g << 8) + (r << 16);
1331
2/2
✓ Branch 0 taken 759 times.
✓ Branch 1 taken 4 times.
763 for (; k <= last; k++)
1332 759 ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
1333
1334 4 ast->has_pal = 1;
1335 4 goto start_sync;
1336
4/4
✓ Branch 0 taken 24674 times.
✓ Branch 1 taken 3249 times.
✓ Branch 2 taken 24660 times.
✓ Branch 3 taken 14 times.
27923 } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
1337
2/4
✓ Branch 0 taken 27909 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 27909 times.
27909 d[2] < 128 && d[3] < 128) ||
1338
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 d[2] * 256 + d[3] == ast->prefix /* ||
1339 (d[2] == 'd' && d[3] == 'c') ||
1340 (d[2] == 'w' && d[3] == 'b') */) {
1341
2/2
✓ Branch 0 taken 403 times.
✓ Branch 1 taken 27520 times.
27923 if (exit_early)
1342 403 return 0;
1343
2/2
✓ Branch 0 taken 26962 times.
✓ Branch 1 taken 558 times.
27520 if (d[2] * 256 + d[3] == ast->prefix)
1344 26962 ast->prefix_count++;
1345 else {
1346 558 ast->prefix = d[2] * 256 + d[3];
1347 558 ast->prefix_count = 0;
1348 }
1349
1350
1/2
✓ Branch 0 taken 27520 times.
✗ Branch 1 not taken.
27520 if (!avi->dv_demux &&
1351
4/4
✓ Branch 0 taken 4596 times.
✓ Branch 1 taken 22924 times.
✓ Branch 2 taken 4317 times.
✓ Branch 3 taken 279 times.
27520 ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
1352 // FIXME: needs a little reordering
1353 (st->discard >= AVDISCARD_NONKEY &&
1354 !(pkt->flags & AV_PKT_FLAG_KEY)) */
1355
2/2
✓ Branch 0 taken 2174 times.
✓ Branch 1 taken 25067 times.
27241 || st->discard >= AVDISCARD_ALL)) {
1356
1357 2453 ast->frame_offset += get_duration(ast, size);
1358 2453 avio_skip(pb, size);
1359 2453 goto start_sync;
1360 }
1361
1362 25067 avi->stream_index = n;
1363 25067 ast->packet_size = size + 8;
1364 25067 ast->remaining = size;
1365
1366
2/2
✓ Branch 0 taken 23620 times.
✓ Branch 1 taken 1447 times.
25067 if (size) {
1367 23620 FFStream *const sti = ffstream(st);
1368 23620 uint64_t pos = avio_tell(pb) - 8;
1369
3/4
✓ Branch 0 taken 23565 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 23565 times.
✗ Branch 3 not taken.
23620 if (!sti->index_entries || !sti->nb_index_entries ||
1370
2/2
✓ Branch 0 taken 3253 times.
✓ Branch 1 taken 20312 times.
23565 sti->index_entries[sti->nb_index_entries - 1].pos < pos) {
1371 3308 av_add_index_entry(st, pos, ast->frame_offset, size,
1372 0, AVINDEX_KEYFRAME);
1373 }
1374 }
1375 25067 return 0;
1376 }
1377 }
1378 }
1379
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 457 times.
457 if (pb->error)
1381 return pb->error;
1382 457 return AVERROR_EOF;
1383 }
1384
1385 766 static int ni_prepare_read(AVFormatContext *s)
1386 {
1387 766 AVIContext *avi = s->priv_data;
1388 766 int best_stream_index = 0;
1389 766 AVStream *best_st = NULL;
1390 FFStream *best_sti;
1391 AVIStream *best_ast;
1392 766 int64_t best_ts = INT64_MAX;
1393 int i;
1394
1395
2/2
✓ Branch 0 taken 1288 times.
✓ Branch 1 taken 766 times.
2054 for (i = 0; i < s->nb_streams; i++) {
1396 1288 AVStream *st = s->streams[i];
1397 1288 FFStream *const sti = ffstream(st);
1398 1288 AVIStream *ast = st->priv_data;
1399 1288 int64_t ts = ast->frame_offset;
1400 int64_t last_ts;
1401
1402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1288 times.
1288 if (!sti->nb_index_entries)
1403 16 continue;
1404
1405 1288 last_ts = sti->index_entries[sti->nb_index_entries - 1].timestamp;
1406
4/4
✓ Branch 0 taken 858 times.
✓ Branch 1 taken 430 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 842 times.
1288 if (!ast->remaining && ts > last_ts)
1407 16 continue;
1408
1409 1272 ts = av_rescale_q(ts, st->time_base,
1410 1272 (AVRational) { FFMAX(1, ast->sample_size),
1411 AV_TIME_BASE });
1412
1413 1272 av_log(s, AV_LOG_TRACE, "%"PRId64" %d/%d %"PRId64"\n", ts,
1414 st->time_base.num, st->time_base.den, ast->frame_offset);
1415
2/2
✓ Branch 0 taken 975 times.
✓ Branch 1 taken 297 times.
1272 if (ts < best_ts) {
1416 975 best_ts = ts;
1417 975 best_st = st;
1418 975 best_stream_index = i;
1419 }
1420 }
1421
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 761 times.
766 if (!best_st)
1422 5 return AVERROR_EOF;
1423
1424 761 best_sti = ffstream(best_st);
1425 761 best_ast = best_st->priv_data;
1426 761 best_ts = best_ast->frame_offset;
1427
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 580 times.
761 if (best_ast->remaining) {
1428 181 i = av_index_search_timestamp(best_st,
1429 best_ts,
1430 AVSEEK_FLAG_ANY |
1431 AVSEEK_FLAG_BACKWARD);
1432 } else {
1433 580 i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
1434
1/2
✓ Branch 0 taken 580 times.
✗ Branch 1 not taken.
580 if (i >= 0)
1435 580 best_ast->frame_offset = best_sti->index_entries[i].timestamp;
1436 }
1437
1438
1/2
✓ Branch 0 taken 761 times.
✗ Branch 1 not taken.
761 if (i >= 0) {
1439 761 int64_t pos = best_sti->index_entries[i].pos;
1440 761 pos += best_ast->packet_size - best_ast->remaining;
1441
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 761 times.
761 if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
1442 return AVERROR_EOF;
1443
1444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 761 times.
761 av_assert0(best_ast->remaining <= best_ast->packet_size);
1445
1446 761 avi->stream_index = best_stream_index;
1447
2/2
✓ Branch 0 taken 580 times.
✓ Branch 1 taken 181 times.
761 if (!best_ast->remaining)
1448 580 best_ast->packet_size =
1449 580 best_ast->remaining = best_sti->index_entries[i].size;
1450 }
1451 else
1452 return AVERROR_EOF;
1453
1454 761 return 0;
1455 }
1456
1457 26359 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
1458 {
1459 26359 AVIContext *avi = s->priv_data;
1460 26359 AVIOContext *pb = s->pb;
1461 int err;
1462
1463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26359 times.
26359 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1464 int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
1465 if (size >= 0)
1466 return size;
1467 else
1468 goto resync;
1469 }
1470
1471
2/2
✓ Branch 0 taken 766 times.
✓ Branch 1 taken 25593 times.
26359 if (avi->non_interleaved) {
1472 766 err = ni_prepare_read(s);
1473
2/2
✓ Branch 0 taken 761 times.
✓ Branch 1 taken 5 times.
766 if (err < 0)
1474 5 return err;
1475 }
1476
1477 26354 resync:
1478
2/2
✓ Branch 0 taken 25898 times.
✓ Branch 1 taken 25524 times.
51422 if (avi->stream_index >= 0) {
1479 25898 AVStream *st = s->streams[avi->stream_index];
1480 25898 FFStream *const sti = ffstream(st);
1481 25898 AVIStream *ast = st->priv_data;
1482 25898 int dv_demux = CONFIG_DV_DEMUXER && avi->dv_demux;
1483 int size, err;
1484
1485
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25898 times.
25898 if (get_subtitle_pkt(s, st, pkt))
1486 return 0;
1487
1488 // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
1489
2/2
✓ Branch 0 taken 22266 times.
✓ Branch 1 taken 3632 times.
25898 if (ast->sample_size <= 1)
1490 22266 size = INT_MAX;
1491
2/2
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 3394 times.
3632 else if (ast->sample_size < 32)
1492 // arbitrary multiplier to avoid tiny packets for raw PCM data
1493 238 size = 1024 * ast->sample_size;
1494 else
1495 3394 size = ast->sample_size;
1496
1497
2/2
✓ Branch 0 taken 22277 times.
✓ Branch 1 taken 3621 times.
25898 if (size > ast->remaining)
1498 22277 size = ast->remaining;
1499 25898 avi->last_pkt_pos = avio_tell(pb);
1500 25898 err = av_get_packet(pb, pkt, size);
1501
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 25861 times.
25898 if (err < 0)
1502 37 return err;
1503 25861 size = err;
1504
1505
4/6
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 25846 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
25861 if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2 && !dv_demux) {
1506 uint8_t *pal;
1507 15 pal = av_packet_new_side_data(pkt,
1508 AV_PKT_DATA_PALETTE,
1509 AVPALETTE_SIZE);
1510
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (!pal) {
1511 av_log(s, AV_LOG_ERROR,
1512 "Failed to allocate data for palette\n");
1513 } else {
1514 15 memcpy(pal, ast->pal, AVPALETTE_SIZE);
1515 15 ast->has_pal = 0;
1516 }
1517 }
1518
1519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25861 times.
25861 if (CONFIG_DV_DEMUXER && dv_demux) {
1520 size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
1521 pkt->data, pkt->size, pkt->pos);
1522 pkt->flags |= AV_PKT_FLAG_KEY;
1523 if (size < 0)
1524 av_packet_unref(pkt);
1525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25861 times.
25861 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1526 !st->codecpar->codec_tag && read_gab2_sub(s, st, pkt)) {
1527 ast->frame_offset++;
1528 avi->stream_index = -1;
1529 ast->remaining = 0;
1530 goto resync;
1531 } else {
1532 /* XXX: How to handle B-frames in AVI? */
1533 25861 pkt->dts = ast->frame_offset;
1534 // pkt->dts += ast->start;
1535
2/2
✓ Branch 0 taken 3999 times.
✓ Branch 1 taken 21862 times.
25861 if (ast->sample_size)
1536 3999 pkt->dts /= ast->sample_size;
1537 25861 pkt->stream_index = avi->stream_index;
1538
1539
3/4
✓ Branch 0 taken 21461 times.
✓ Branch 1 taken 4400 times.
✓ Branch 2 taken 21461 times.
✗ Branch 3 not taken.
47322 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->index_entries) {
1540 AVIndexEntry *e;
1541 int index;
1542
1543 21461 index = av_index_search_timestamp(st, ast->frame_offset, AVSEEK_FLAG_ANY);
1544 21461 e = &sti->index_entries[index];
1545
1546
4/4
✓ Branch 0 taken 21348 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 20013 times.
✓ Branch 3 taken 1335 times.
21461 if (index >= 0 && e->timestamp == ast->frame_offset) {
1547
2/2
✓ Branch 0 taken 2485 times.
✓ Branch 1 taken 17528 times.
20013 if (index == sti->nb_index_entries-1) {
1548 2485 int key=1;
1549 2485 uint32_t state=-1;
1550
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 2433 times.
2485 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
1551 52 const uint8_t *ptr = pkt->data, *end = ptr + FFMIN(size, 256);
1552
1/2
✓ Branch 0 taken 109 times.
✗ Branch 1 not taken.
109 while (ptr < end) {
1553 109 ptr = avpriv_find_start_code(ptr, end, &state);
1554
3/4
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
109 if (state == 0x1B6 && ptr < end) {
1555 52 key = !(*ptr & 0xC0);
1556 52 break;
1557 }
1558 }
1559 }
1560
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 2446 times.
2485 if (!key)
1561 39 e->flags &= ~AVINDEX_KEYFRAME;
1562 }
1563
2/2
✓ Branch 0 taken 11873 times.
✓ Branch 1 taken 8140 times.
20013 if (e->flags & AVINDEX_KEYFRAME)
1564 11873 pkt->flags |= AV_PKT_FLAG_KEY;
1565 }
1566 } else {
1567 4400 pkt->flags |= AV_PKT_FLAG_KEY;
1568 }
1569 25861 ast->frame_offset += get_duration(ast, pkt->size);
1570 }
1571 25861 ast->remaining -= err;
1572
2/2
✓ Branch 0 taken 25613 times.
✓ Branch 1 taken 248 times.
25861 if (!ast->remaining) {
1573 25613 avi->stream_index = -1;
1574 25613 ast->packet_size = 0;
1575 }
1576
1577
6/6
✓ Branch 0 taken 25103 times.
✓ Branch 1 taken 758 times.
✓ Branch 2 taken 23656 times.
✓ Branch 3 taken 1447 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 23655 times.
25861 if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
1578 1 av_packet_unref(pkt);
1579 1 goto resync;
1580 }
1581 25860 ast->seek_pos= 0;
1582
1583
6/6
✓ Branch 0 taken 25102 times.
✓ Branch 1 taken 758 times.
✓ Branch 2 taken 25006 times.
✓ Branch 3 taken 96 times.
✓ Branch 4 taken 21859 times.
✓ Branch 5 taken 3147 times.
25860 if (!avi->non_interleaved && sti->nb_index_entries > 1 && avi->index_loaded > 1) {
1584 21859 int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
1585
1586
2/2
✓ Branch 0 taken 21194 times.
✓ Branch 1 taken 665 times.
21859 if (avi->dts_max < dts) {
1587 21194 avi->dts_max = dts;
1588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 665 times.
665 } else if (avi->dts_max - (uint64_t)dts > 2*AV_TIME_BASE) {
1589 avi->non_interleaved= 1;
1590 av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
1591 }
1592 }
1593
1594 25860 return 0;
1595 }
1596
1597
2/2
✓ Branch 1 taken 457 times.
✓ Branch 2 taken 25067 times.
25524 if ((err = avi_sync(s, 0)) < 0)
1598 457 return err;
1599 25067 goto resync;
1600 }
1601
1602 /* XXX: We make the implicit supposition that the positions are sorted
1603 * for each stream. */
1604 403 static int avi_read_idx1(AVFormatContext *s, int size)
1605 {
1606 403 AVIContext *avi = s->priv_data;
1607 403 AVIOContext *pb = s->pb;
1608 int nb_index_entries, i;
1609 AVStream *st;
1610 AVIStream *ast;
1611 int64_t pos;
1612 403 unsigned int index, tag, flags, len, first_packet = 1;
1613 403 int64_t last_pos = -1;
1614 403 unsigned last_idx = -1;
1615 403 int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1616 403 int anykey = 0;
1617
1618 403 nb_index_entries = size / 16;
1619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 403 times.
403 if (nb_index_entries <= 0)
1620 return AVERROR_INVALIDDATA;
1621
1622 403 idx1_pos = avio_tell(pb);
1623 403 avio_seek(pb, avi->movi_list + 4, SEEK_SET);
1624
1/2
✓ Branch 1 taken 403 times.
✗ Branch 2 not taken.
403 if (avi_sync(s, 1) == 0)
1625 403 first_packet_pos = avio_tell(pb) - 8;
1626 403 avi->stream_index = -1;
1627 403 avio_seek(pb, idx1_pos, SEEK_SET);
1628
1629
3/4
✓ Branch 0 taken 382 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 382 times.
403 if (s->nb_streams == 1 && s->streams[0]->codecpar->codec_tag == AV_RL32("MMES")) {
1630 first_packet_pos = 0;
1631 data_offset = avi->movi_list;
1632 }
1633
1634 /* Read the entries and sort them in each stream component. */
1635
2/2
✓ Branch 0 taken 26066 times.
✓ Branch 1 taken 403 times.
26469 for (i = 0; i < nb_index_entries; i++) {
1636
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26066 times.
26066 if (avio_feof(pb))
1637 return -1;
1638
1639 26066 tag = avio_rl32(pb);
1640 26066 flags = avio_rl32(pb);
1641 26066 pos = avio_rl32(pb);
1642 26066 len = avio_rl32(pb);
1643 26066 av_log(s, AV_LOG_TRACE, "%d: tag=0x%x flags=0x%x pos=0x%"PRIx64" len=%d/",
1644 i, tag, flags, pos, len);
1645
1646 26066 index = ((tag & 0xff) - '0') * 10;
1647 26066 index += (tag >> 8 & 0xff) - '0';
1648
2/2
✓ Branch 0 taken 290 times.
✓ Branch 1 taken 25776 times.
26066 if (index >= s->nb_streams)
1649 290 continue;
1650 25776 st = s->streams[index];
1651 25776 ast = st->priv_data;
1652
1653 /* Skip 'xxpc' palette change entries in the index until a logic
1654 * to process these is properly implemented. */
1655
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25774 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
25776 if ((tag >> 16 & 0xff) == 'p' && (tag >> 24 & 0xff) == 'c')
1656 2 continue;
1657
1658
3/4
✓ Branch 0 taken 403 times.
✓ Branch 1 taken 25371 times.
✓ Branch 2 taken 403 times.
✗ Branch 3 not taken.
25774 if (first_packet && first_packet_pos) {
1659
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 402 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
403 if (avi->movi_list + 4 != pos || pos + 500 > first_packet_pos)
1660 403 data_offset = first_packet_pos - pos;
1661 403 first_packet = 0;
1662 }
1663 25774 pos += data_offset;
1664
1665 25774 av_log(s, AV_LOG_TRACE, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
1666
1667 // even if we have only a single stream, we should
1668 // switch to non-interleaved to get correct timestamps
1669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25774 times.
25774 if (last_pos == pos)
1670 avi->non_interleaved = 1;
1671
3/4
✓ Branch 0 taken 25774 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24068 times.
✓ Branch 3 taken 1706 times.
25774 if (last_idx != pos && len) {
1672 24068 av_add_index_entry(st, pos, ast->cum_len, len, 0,
1673 24068 (flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
1674 24068 last_idx= pos;
1675 }
1676 25774 ast->cum_len += get_duration(ast, len);
1677 25774 last_pos = pos;
1678 25774 anykey |= flags&AVIIF_INDEX;
1679 }
1680
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 402 times.
403 if (!anykey) {
1681
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (index = 0; index < s->nb_streams; index++) {
1682 1 FFStream *const sti = ffstream(s->streams[index]);
1683
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (sti->nb_index_entries)
1684 1 sti->index_entries[0].flags |= AVINDEX_KEYFRAME;
1685 }
1686 }
1687 403 return 0;
1688 }
1689
1690 /* Scan the index and consider any file with streams more than
1691 * 2 seconds or 64MB apart non-interleaved. */
1692 441 static int check_stream_max_drift(AVFormatContext *s)
1693 {
1694 int64_t min_pos, pos;
1695 int i;
1696 441 int *idx = av_calloc(s->nb_streams, sizeof(*idx));
1697
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 441 times.
441 if (!idx)
1698 return AVERROR(ENOMEM);
1699
2/2
✓ Branch 0 taken 29579 times.
✓ Branch 1 taken 441 times.
30020 for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) {
1700 29579 int64_t max_dts = INT64_MIN / 2;
1701 29579 int64_t min_dts = INT64_MAX / 2;
1702 29579 int64_t max_buffer = 0;
1703
1704 29579 min_pos = INT64_MAX;
1705
1706
2/2
✓ Branch 0 taken 38056 times.
✓ Branch 1 taken 29579 times.
67635 for (i = 0; i < s->nb_streams; i++) {
1707 38056 AVStream *st = s->streams[i];
1708 38056 AVIStream *ast = st->priv_data;
1709 38056 FFStream *const sti = ffstream(st);
1710 38056 int n = sti->nb_index_entries;
1711
4/4
✓ Branch 0 taken 66471 times.
✓ Branch 1 taken 723 times.
✓ Branch 2 taken 29138 times.
✓ Branch 3 taken 37333 times.
67194 while (idx[i] < n && sti->index_entries[idx[i]].pos < pos)
1712 29138 idx[i]++;
1713
2/2
✓ Branch 0 taken 37333 times.
✓ Branch 1 taken 723 times.
38056 if (idx[i] < n) {
1714 int64_t dts;
1715 37333 dts = av_rescale_q(sti->index_entries[idx[i]].timestamp /
1716 37333 FFMAX(ast->sample_size, 1),
1717 37333 st->time_base, AV_TIME_BASE_Q);
1718 37333 min_dts = FFMIN(min_dts, dts);
1719 37333 min_pos = FFMIN(min_pos, sti->index_entries[idx[i]].pos);
1720 }
1721 }
1722
2/2
✓ Branch 0 taken 38056 times.
✓ Branch 1 taken 29579 times.
67635 for (i = 0; i < s->nb_streams; i++) {
1723 38056 AVStream *st = s->streams[i];
1724 38056 FFStream *const sti = ffstream(st);
1725 38056 AVIStream *ast = st->priv_data;
1726
1727
4/4
✓ Branch 0 taken 37376 times.
✓ Branch 1 taken 680 times.
✓ Branch 2 taken 36955 times.
✓ Branch 3 taken 421 times.
38056 if (idx[i] && min_dts != INT64_MAX / 2) {
1728 int64_t dts, delta_dts;
1729 36955 dts = av_rescale_q(sti->index_entries[idx[i] - 1].timestamp /
1730 36955 FFMAX(ast->sample_size, 1),
1731 36955 st->time_base, AV_TIME_BASE_Q);
1732 36955 delta_dts = av_sat_sub64(dts, min_dts);
1733 36955 max_dts = FFMAX(max_dts, dts);
1734 36955 max_buffer = FFMAX(max_buffer,
1735 av_rescale(delta_dts,
1736 st->codecpar->bit_rate,
1737 AV_TIME_BASE));
1738 }
1739 }
1740
2/4
✓ Branch 1 taken 29579 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 29579 times.
29579 if (av_sat_sub64(max_dts, min_dts) > 2 * AV_TIME_BASE ||
1741 max_buffer > 1024 * 1024 * 8 * 8) {
1742 av_free(idx);
1743 return 1;
1744 }
1745 }
1746 441 av_free(idx);
1747 441 return 0;
1748 }
1749
1750 446 static int guess_ni_flag(AVFormatContext *s)
1751 {
1752 int i;
1753 446 int64_t last_start = 0;
1754 446 int64_t first_end = INT64_MAX;
1755 446 int64_t oldpos = avio_tell(s->pb);
1756
1757
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 446 times.
935 for (i = 0; i < s->nb_streams; i++) {
1758 489 AVStream *st = s->streams[i];
1759 489 FFStream *const sti = ffstream(st);
1760 489 int n = sti->nb_index_entries;
1761 unsigned int size;
1762
1763
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 431 times.
489 if (n <= 0)
1764 58 continue;
1765
1766
2/2
✓ Branch 0 taken 394 times.
✓ Branch 1 taken 37 times.
431 if (n >= 2) {
1767 394 int64_t pos = sti->index_entries[0].pos;
1768 unsigned tag[2];
1769 394 avio_seek(s->pb, pos, SEEK_SET);
1770 394 tag[0] = avio_r8(s->pb);
1771 394 tag[1] = avio_r8(s->pb);
1772 394 avio_rl16(s->pb);
1773 394 size = avio_rl32(s->pb);
1774
3/4
✓ Branch 1 taken 393 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 393 times.
394 if (get_stream_idx(tag) == i && pos + size > sti->index_entries[1].pos)
1775 last_start = INT64_MAX;
1776
3/4
✓ Branch 1 taken 393 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 393 times.
394 if (get_stream_idx(tag) == i && size == sti->index_entries[0].size + 8)
1777 last_start = INT64_MAX;
1778 }
1779
1780
2/2
✓ Branch 0 taken 421 times.
✓ Branch 1 taken 10 times.
431 if (sti->index_entries[0].pos > last_start)
1781 421 last_start = sti->index_entries[0].pos;
1782
2/2
✓ Branch 0 taken 418 times.
✓ Branch 1 taken 13 times.
431 if (sti->index_entries[n - 1].pos < first_end)
1783 418 first_end = sti->index_entries[n - 1].pos;
1784 }
1785 446 avio_seek(s->pb, oldpos, SEEK_SET);
1786
1787
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 441 times.
446 if (last_start > first_end)
1788 5 return 1;
1789
1790 441 return check_stream_max_drift(s);
1791 }
1792
1793 440 static int avi_load_index(AVFormatContext *s)
1794 {
1795 440 AVIContext *avi = s->priv_data;
1796 440 AVIOContext *pb = s->pb;
1797 uint32_t tag, size;
1798 440 int64_t pos = avio_tell(pb);
1799 int64_t next;
1800 440 int ret = -1;
1801
1802
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 440 times.
440 if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
1803 goto the_end; // maybe truncated file
1804 440 av_log(s, AV_LOG_TRACE, "movi_end=0x%"PRIx64"\n", avi->movi_end);
1805 for (;;) {
1806 852 tag = avio_rl32(pb);
1807 852 size = avio_rl32(pb);
1808
2/2
✓ Branch 1 taken 418 times.
✓ Branch 2 taken 434 times.
852 if (avio_feof(pb))
1809 418 break;
1810 434 next = avio_tell(pb);
1811
2/4
✓ Branch 0 taken 434 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 434 times.
434 if (next < 0 || next > INT64_MAX - size - (size & 1))
1812 break;
1813 434 next += size + (size & 1LL);
1814
1815
3/4
✓ Branch 0 taken 403 times.
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 403 times.
✗ Branch 3 not taken.
837 if (tag == MKTAG('i', 'd', 'x', '1') &&
1816 403 avi_read_idx1(s, size) >= 0) {
1817 403 avi->index_loaded=2;
1818 403 ret = 0;
1819
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 22 times.
31 }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
1820 9 uint32_t tag1 = avio_rl32(pb);
1821
1822
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 if (tag1 == MKTAG('I', 'N', 'F', 'O'))
1823 5 ff_read_riff_info(s, size - 4);
1824
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 }else if (!ret)
1825 22 break;
1826
1827
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 412 times.
412 if (avio_seek(pb, next, SEEK_SET) < 0)
1828 break; // something is wrong here
1829 }
1830
1831 the_end:
1832 440 avio_seek(pb, pos, SEEK_SET);
1833 440 return ret;
1834 }
1835
1836 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
1837 {
1838 AVIStream *ast2 = st2->priv_data;
1839 int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
1840 av_packet_unref(ast2->sub_pkt);
1841 if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
1842 avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
1843 ff_read_packet(ast2->sub_ctx, ast2->sub_pkt);
1844 }
1845
1846 728 static int avi_read_seek(AVFormatContext *s, int stream_index,
1847 int64_t timestamp, int flags)
1848 {
1849 728 AVIContext *avi = s->priv_data;
1850 AVStream *st;
1851 FFStream *sti;
1852 int i, index;
1853 int64_t pos, pos_min;
1854 AVIStream *ast;
1855
1856 /* Does not matter which stream is requested dv in avi has the
1857 * stream information in the first video stream.
1858 */
1859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
728 if (avi->dv_demux)
1860 stream_index = 0;
1861
1862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
728 if (!avi->index_loaded) {
1863 /* we only load the index on demand */
1864 avi_load_index(s);
1865 avi->index_loaded |= 1;
1866 }
1867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
728 av_assert0(stream_index >= 0);
1868
1869 728 st = s->streams[stream_index];
1870 728 sti = ffstream(st);
1871 728 ast = st->priv_data;
1872 728 index = av_index_search_timestamp(st,
1873 728 timestamp * FFMAX(ast->sample_size, 1),
1874 flags);
1875
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 530 times.
728 if (index < 0) {
1876
1/2
✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
198 if (sti->nb_index_entries > 0)
1877 198 av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
1878 198 timestamp * FFMAX(ast->sample_size, 1),
1879 198 sti->index_entries[0].timestamp,
1880 198 sti->index_entries[sti->nb_index_entries - 1].timestamp);
1881 198 return AVERROR_INVALIDDATA;
1882 }
1883
1884 /* find the position */
1885 530 pos = sti->index_entries[index].pos;
1886 530 timestamp = sti->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
1887
1888 530 av_log(s, AV_LOG_TRACE, "XX %"PRId64" %d %"PRId64"\n",
1889 530 timestamp, index, sti->index_entries[index].timestamp);
1890
1891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 530 times.
530 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1892 /* One and only one real stream for DV in AVI, and it has video */
1893 /* offsets. Calling with other stream indexes should have failed */
1894 /* the av_index_search_timestamp call above. */
1895
1896 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1897 return -1;
1898
1899 /* Feed the DV video stream version of the timestamp to the */
1900 /* DV demux so it can synthesize correct timestamps. */
1901 ff_dv_offset_reset(avi->dv_demux, timestamp);
1902
1903 avi->stream_index = -1;
1904 return 0;
1905 }
1906
1907 530 pos_min = pos;
1908
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 530 times.
1077 for (i = 0; i < s->nb_streams; i++) {
1909 547 AVStream *st2 = s->streams[i];
1910 547 FFStream *const sti2 = ffstream(st2);
1911 547 AVIStream *ast2 = st2->priv_data;
1912
1913 547 ast2->packet_size =
1914 547 ast2->remaining = 0;
1915
1916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (ast2->sub_ctx) {
1917 seek_subtitle(st, st2, timestamp);
1918 continue;
1919 }
1920
1921
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (sti2->nb_index_entries <= 0)
1922 continue;
1923
1924 // av_assert1(st2->codecpar->block_align);
1925 547 index = av_index_search_timestamp(st2,
1926 547 av_rescale_q(timestamp,
1927 st->time_base,
1928 st2->time_base) *
1929 547 FFMAX(ast2->sample_size, 1),
1930 547 flags |
1931 AVSEEK_FLAG_BACKWARD |
1932
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 530 times.
547 (st2->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
1933
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (index < 0)
1934 index = 0;
1935 547 ast2->seek_pos = sti2->index_entries[index].pos;
1936 547 pos_min = FFMIN(pos_min,ast2->seek_pos);
1937 }
1938
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 530 times.
1077 for (i = 0; i < s->nb_streams; i++) {
1939 547 AVStream *st2 = s->streams[i];
1940 547 FFStream *const sti2 = ffstream(st2);
1941 547 AVIStream *ast2 = st2->priv_data;
1942
1943
2/4
✓ Branch 0 taken 547 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 547 times.
547 if (ast2->sub_ctx || sti2->nb_index_entries <= 0)
1944 continue;
1945
1946 547 index = av_index_search_timestamp(
1947 st2,
1948 547 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
1949
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 530 times.
547 flags | AVSEEK_FLAG_BACKWARD | (st2->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
1950
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (index < 0)
1951 index = 0;
1952
5/6
✓ Branch 0 taken 558 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 421 times.
✓ Branch 3 taken 137 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 410 times.
558 while (!avi->non_interleaved && index > 0 && sti2->index_entries[index-1].pos >= pos_min)
1953 11 index--;
1954 547 ast2->frame_offset = sti2->index_entries[index].timestamp;
1955 }
1956
1957 /* do the seek */
1958
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 530 times.
530 if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
1959 av_log(s, AV_LOG_ERROR, "Seek failed\n");
1960 return -1;
1961 }
1962 530 avi->stream_index = -1;
1963 530 avi->dts_max = INT_MIN;
1964 530 return 0;
1965 }
1966
1967 446 static int avi_read_close(AVFormatContext *s)
1968 {
1969 int i;
1970 446 AVIContext *avi = s->priv_data;
1971
1972
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 446 times.
935 for (i = 0; i < s->nb_streams; i++) {
1973 489 AVStream *st = s->streams[i];
1974 489 AVIStream *ast = st->priv_data;
1975
1/2
✓ Branch 0 taken 489 times.
✗ Branch 1 not taken.
489 if (ast) {
1976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 489 times.
489 if (ast->sub_ctx) {
1977 av_freep(&ast->sub_ctx->pb);
1978 avformat_close_input(&ast->sub_ctx);
1979 }
1980 489 av_buffer_unref(&ast->sub_buffer);
1981 489 av_packet_free(&ast->sub_pkt);
1982 }
1983 }
1984
1985 446 av_freep(&avi->dv_demux);
1986
1987 446 return 0;
1988 }
1989
1990 6780 static int avi_probe(const AVProbeData *p)
1991 {
1992 int i;
1993
1994 /* check file header */
1995
2/2
✓ Branch 0 taken 32136 times.
✓ Branch 1 taken 6337 times.
38473 for (i = 0; avi_headers[i][0]; i++)
1996
2/2
✓ Branch 0 taken 2417 times.
✓ Branch 1 taken 29719 times.
32136 if (AV_RL32(p->buf ) == AV_RL32(avi_headers[i] ) &&
1997
2/2
✓ Branch 0 taken 443 times.
✓ Branch 1 taken 1974 times.
2417 AV_RL32(p->buf + 8) == AV_RL32(avi_headers[i] + 4))
1998 443 return AVPROBE_SCORE_MAX;
1999
2000 6337 return 0;
2001 }
2002
2003 const AVInputFormat ff_avi_demuxer = {
2004 .name = "avi",
2005 .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
2006 .priv_data_size = sizeof(AVIContext),
2007 .flags_internal = FF_FMT_INIT_CLEANUP,
2008 .extensions = "avi",
2009 .read_probe = avi_probe,
2010 .read_header = avi_read_header,
2011 .read_packet = avi_read_packet,
2012 .read_close = avi_read_close,
2013 .read_seek = avi_read_seek,
2014 .priv_class = &demuxer_class,
2015 };
2016