FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avidec.c
Date: 2026-04-19 20:43:40
Exec Total Coverage
Lines: 863 1201 71.9%
Functions: 21 24 87.5%
Branches: 552 906 60.9%

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