FFmpeg coverage


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