FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avidec.c
Date: 2026-07-16 17:05:34
Exec Total Coverage
Lines: 865 1206 71.7%
Functions: 21 24 87.5%
Branches: 554 910 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 64905 static inline int get_duration(AVIStream *ast, int len)
141 {
142
2/2
✓ Branch 0 taken 9492 times.
✓ Branch 1 taken 55413 times.
64905 if (ast->sample_size)
143 9492 return len;
144
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 55201 times.
55413 else if (ast->dshow_block_align)
145 212 return (len + (int64_t)ast->dshow_block_align - 1) / ast->dshow_block_align;
146 else
147 55201 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
65 if (size < 4)
567 return AVERROR_INVALIDDATA;
568 65 ff_read_riff_info(s, size - 4);
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1055 times.
1055 } else if (tag1 == MKTAG('n', 'c', 'd', 't'))
570 avi_read_nikon(s, list_end);
571
572 4207 break;
573 1 case MKTAG('I', 'D', 'I', 'T'):
574 {
575 1 unsigned char date[64] = { 0 };
576 1 size += (size & 1);
577 1 size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
578 1 avio_skip(pb, size);
579 1 avi_metadata_creation_time(&s->metadata, date);
580 1 break;
581 }
582 42 case MKTAG('d', 'm', 'l', 'h'):
583 42 avi->is_odml = 1;
584 42 avio_skip(pb, size + (size & 1));
585 42 break;
586 2 case MKTAG('a', 'm', 'v', 'h'):
587 2 amv_file_format = 1;
588 av_fallthrough;
589 476 case MKTAG('a', 'v', 'i', 'h'):
590 /* AVI header */
591 /* using frame_period is bad idea */
592 476 frame_period = avio_rl32(pb);
593 476 avio_rl32(pb); /* max. bytes per second */
594 476 avio_rl32(pb);
595 476 avi->non_interleaved |= avio_rl32(pb) & AVIF_MUSTUSEINDEX;
596
597 476 avio_skip(pb, 2 * 4);
598 476 avio_rl32(pb);
599 476 avio_rl32(pb);
600 476 avih_width = avio_rl32(pb);
601 476 avih_height = avio_rl32(pb);
602
603 476 avio_skip(pb, size - 10 * 4);
604 476 break;
605 521 case MKTAG('s', 't', 'r', 'h'): {
606 /* stream header */
607
608 521 tag1 = avio_rl32(pb);
609 521 handler = avio_rl32(pb); /* codec tag */
610
611
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 520 times.
521 if (tag1 == MKTAG('p', 'a', 'd', 's')) {
612 1 avio_skip(pb, size - 8);
613 521 break;
614 }
615 520 stream_index++;
616 520 AVStream *const st = avformat_new_stream(s, NULL);
617
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 if (!st)
618 return AVERROR(ENOMEM);
619
620 520 st->id = stream_index;
621 520 ast = av_mallocz(sizeof(AVIStream));
622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 if (!ast)
623 return AVERROR(ENOMEM);
624 520 st->priv_data = ast;
625
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 516 times.
520 if (amv_file_format)
626 4 tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
627
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 : MKTAG('v', 'i', 'd', 's');
628
629 520 print_tag(s, "strh", tag1, -1);
630
631
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') ||
632 tag1 == MKTAG('i', 'v', 'a', 's')) {
633 int64_t dv_dur;
634
635 /* After some consideration -- I don't think we
636 * have to support anything but DV in type1 AVIs. */
637 if (s->nb_streams != 1)
638 return AVERROR_INVALIDDATA;
639
640 if (handler != MKTAG('d', 'v', 's', 'd') &&
641 handler != MKTAG('d', 'v', 'h', 'd') &&
642 handler != MKTAG('d', 'v', 's', 'l'))
643 return AVERROR_INVALIDDATA;
644
645 if (!CONFIG_DV_DEMUXER)
646 return AVERROR_DEMUXER_NOT_FOUND;
647
648 ast = s->streams[0]->priv_data;
649 st->priv_data = NULL;
650 ff_remove_stream(s, st);
651
652 avi->dv_demux = avpriv_dv_init_demux(s);
653 if (!avi->dv_demux) {
654 av_free(ast);
655 return AVERROR(ENOMEM);
656 }
657
658 s->streams[0]->priv_data = ast;
659 avio_skip(pb, 3 * 4);
660 ast->scale = avio_rl32(pb);
661 ast->rate = avio_rl32(pb);
662 avio_skip(pb, 4); /* start time */
663
664 dv_dur = avio_rl32(pb);
665 if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
666 dv_dur *= AV_TIME_BASE;
667 s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
668 }
669 /* else, leave duration alone; timing estimation in utils.c
670 * will make a guess based on bitrate. */
671
672 stream_index = s->nb_streams - 1;
673 avio_skip(pb, size - 9 * 4);
674 break;
675 }
676
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 av_assert0(stream_index < s->nb_streams);
678 520 ast->handler = handler;
679
680 520 avio_rl32(pb); /* flags */
681 520 avio_rl16(pb); /* priority */
682 520 avio_rl16(pb); /* language */
683 520 avio_rl32(pb); /* initial frame */
684 520 ast->scale = avio_rl32(pb);
685 520 ast->rate = avio_rl32(pb);
686
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)) {
687 4 av_log(s, AV_LOG_WARNING,
688 "scale/rate is %"PRIu32"/%"PRIu32" which is invalid. "
689 "(This file has been generated by broken software.)\n",
690 ast->scale,
691 ast->rate);
692
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (frame_period) {
693 4 ast->rate = 1000000;
694 4 ast->scale = frame_period;
695 } else {
696 ast->rate = 25;
697 ast->scale = 1;
698 }
699 }
700 520 avpriv_set_pts_info(st, 64, ast->scale, ast->rate);
701
702 520 ast->cum_len = avio_rl32(pb); /* start */
703 520 st->nb_frames = avio_rl32(pb);
704
705 520 st->start_time = 0;
706 520 avio_rl32(pb); /* buffer size */
707 520 avio_rl32(pb); /* quality */
708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 if (ast->cum_len > 3600LL * ast->rate / ast->scale) {
709 av_log(s, AV_LOG_ERROR, "crazy start time, iam scared, giving up\n");
710 ast->cum_len = 0;
711 }
712 520 ast->sample_size = avio_rl32(pb);
713 520 ast->cum_len *= FFMAX(1, ast->sample_size);
714 520 av_log(s, AV_LOG_TRACE, "%"PRIu32" %"PRIu32" %d\n",
715 ast->rate, ast->scale, ast->sample_size);
716
717
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) {
718 473 case MKTAG('v', 'i', 'd', 's'):
719 473 codec_type = AVMEDIA_TYPE_VIDEO;
720
721 473 ast->sample_size = 0;
722 473 st->avg_frame_rate = av_inv_q(st->time_base);
723 473 break;
724 47 case MKTAG('a', 'u', 'd', 's'):
725 47 codec_type = AVMEDIA_TYPE_AUDIO;
726 47 break;
727 case MKTAG('t', 'x', 't', 's'):
728 codec_type = AVMEDIA_TYPE_SUBTITLE;
729 break;
730 case MKTAG('d', 'a', 't', 's'):
731 codec_type = AVMEDIA_TYPE_DATA;
732 break;
733 default:
734 av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
735 }
736
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 if (ast->sample_size < 0) {
738 if (s->error_recognition & AV_EF_EXPLODE) {
739 av_log(s, AV_LOG_ERROR,
740 "Invalid sample_size %d at stream %d\n",
741 ast->sample_size,
742 stream_index);
743 return AVERROR_INVALIDDATA;
744 }
745 av_log(s, AV_LOG_WARNING,
746 "Invalid sample_size %d at stream %d "
747 "setting it to 0\n",
748 ast->sample_size,
749 stream_index);
750 ast->sample_size = 0;
751 }
752
753
2/2
✓ Branch 0 taken 483 times.
✓ Branch 1 taken 37 times.
520 if (ast->sample_size == 0) {
754 483 st->duration = st->nb_frames;
755
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) {
756 39 av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
757 39 st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
758 }
759 }
760 520 ast->frame_offset = ast->cum_len;
761 520 avio_skip(pb, size - 12 * 4);
762 520 break;
763 }
764 520 case MKTAG('s', 't', 'r', 'f'):
765 /* stream header */
766
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 ||
767 codec_type == AVMEDIA_TYPE_VIDEO))
768 break;
769
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) {
770 avio_skip(pb, size);
771 } else {
772 520 uint64_t cur_pos = avio_tell(pb);
773 unsigned esize;
774
2/2
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 4 times.
520 if (cur_pos < list_end)
775 516 size = FFMIN(size, list_end - cur_pos);
776 520 AVStream *const st = s->streams[stream_index];
777 520 FFStream *const sti = ffstream(st);
778
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 if (st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN) {
779 avio_skip(pb, size);
780 break;
781 }
782
2/4
✓ Branch 0 taken 473 times.
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
520 switch (codec_type) {
783 473 case AVMEDIA_TYPE_VIDEO:
784
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 471 times.
473 if (amv_file_format) {
785 2 st->codecpar->width = avih_width;
786 2 st->codecpar->height = avih_height;
787 2 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
788 2 st->codecpar->codec_id = AV_CODEC_ID_AMV;
789 2 avio_skip(pb, size);
790 520 break;
791 }
792 471 tag1 = ff_get_bmp_header(pb, st, &esize);
793
794
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') ||
795 tag1 == MKTAG('D', 'X', 'S', 'A')) {
796 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
797 st->codecpar->codec_tag = tag1;
798 st->codecpar->codec_id = AV_CODEC_ID_XSUB;
799 break;
800 }
801
802
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) {
803
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)) {
804 18 st->codecpar->extradata_size = esize - 10 * 4;
805 } else
806 152 st->codecpar->extradata_size = size - 10 * 4;
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
170 if (st->codecpar->extradata) {
808 av_log(s, AV_LOG_WARNING, "New extradata in strf chunk, freeing previous one.\n");
809 }
810 170 ret = ff_get_extradata(s, st->codecpar, pb,
811 170 st->codecpar->extradata_size);
812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 170 times.
170 if (ret < 0)
813 return ret;
814 }
815
816 // FIXME: check if the encoder really did this correctly
817
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 453 times.
471 if (st->codecpar->extradata_size & 1)
818 18 avio_r8(pb);
819
820 /* Extract palette from extradata if bpp <= 8.
821 * This code assumes that extradata contains only palette.
822 * This is true for all paletted codecs implemented in
823 * FFmpeg. */
824
2/2
✓ Branch 0 taken 170 times.
✓ Branch 1 taken 301 times.
471 if (st->codecpar->extradata_size &&
825
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 155 times.
170 (st->codecpar->bits_per_coded_sample <= 8)) {
826 15 int pal_size = (1 << st->codecpar->bits_per_coded_sample) << 2;
827 const uint8_t *pal_src;
828
829 15 pal_size = FFMIN(pal_size, st->codecpar->extradata_size);
830 15 pal_src = st->codecpar->extradata +
831 15 st->codecpar->extradata_size - pal_size;
832 /* Exclude the "BottomUp" field from the palette */
833
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (pal_src - st->codecpar->extradata >= 9 &&
834 !memcmp(st->codecpar->extradata + st->codecpar->extradata_size - 9, "BottomUp", 9))
835 pal_src -= 9;
836
2/2
✓ Branch 0 taken 2330 times.
✓ Branch 1 taken 15 times.
2345 for (i = 0; i < pal_size / 4; i++)
837 2330 ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src + 4 * i);
838 15 ast->has_pal = 1;
839 }
840
841 471 print_tag(s, "video", tag1, 0);
842
843 471 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
844 471 st->codecpar->codec_tag = tag1;
845 471 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags,
846 tag1);
847 /* If codec is not found yet, try with the mov tags. */
848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471 times.
471 if (!st->codecpar->codec_id) {
849 st->codecpar->codec_id =
850 ff_codec_get_id(ff_codec_movvideo_tags, tag1);
851 if (st->codecpar->codec_id)
852 av_log(s, AV_LOG_WARNING,
853 "mov tag found in avi (fourcc %s)\n",
854 av_fourcc2str(tag1));
855 }
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471 times.
471 if (!st->codecpar->codec_id)
857 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags_unofficial, tag1);
858
859 /* This is needed to get the pict type which is necessary
860 * for generating correct pts. */
861 471 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
862
863
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 415 times.
471 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4 &&
864
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 54 times.
56 ast->handler == MKTAG('X', 'V', 'I', 'D'))
865 2 st->codecpar->codec_tag = MKTAG('X', 'V', 'I', 'D');
866
867
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471 times.
471 if (st->codecpar->codec_tag == MKTAG('V', 'S', 'S', 'H'))
868 sti->need_parsing = AVSTREAM_PARSE_FULL;
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471 times.
471 if (st->codecpar->codec_id == AV_CODEC_ID_RV40)
870 sti->need_parsing = AVSTREAM_PARSE_NONE;
871
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471 times.
471 if (st->codecpar->codec_id == AV_CODEC_ID_HEVC &&
872 st->codecpar->codec_tag == MKTAG('H', '2', '6', '5'))
873 sti->need_parsing = AVSTREAM_PARSE_FULL;
874
875
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 470 times.
471 if (st->codecpar->codec_id == AV_CODEC_ID_AVRN &&
876
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 st->codecpar->codec_tag == MKTAG('A', 'V', 'R', 'n') &&
877
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (st->codecpar->extradata_size < 31 ||
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 memcmp(&st->codecpar->extradata[28], "1:1", 3)))
879 st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
880
881
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 &&
882 st->codecpar->extradata_size < 1U << 30) {
883 st->codecpar->extradata_size += 9;
884 if ((ret = av_reallocp(&st->codecpar->extradata,
885 st->codecpar->extradata_size +
886 AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
887 st->codecpar->extradata_size = 0;
888 return ret;
889 } else
890 memcpy(st->codecpar->extradata + st->codecpar->extradata_size - 9,
891 "BottomUp", 9);
892 }
893
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471 times.
471 if (st->codecpar->height == INT_MIN)
894 return AVERROR_INVALIDDATA;
895 471 st->codecpar->height = FFABS(st->codecpar->height);
896
897 // avio_skip(pb, size - 5 * 4);
898 471 break;
899 47 case AVMEDIA_TYPE_AUDIO:
900 47 ret = ff_get_wav_header(s, pb, st->codecpar, size, 0);
901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (ret < 0)
902 return ret;
903 47 ast->dshow_block_align = st->codecpar->block_align;
904
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 &&
905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 ast->sample_size != st->codecpar->block_align) {
906 av_log(s,
907 AV_LOG_WARNING,
908 "sample size (%d) != block align (%d)\n",
909 ast->sample_size,
910 st->codecpar->block_align);
911 ast->sample_size = st->codecpar->block_align;
912 }
913 /* 2-aligned
914 * (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
915
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (size & 1)
916 avio_skip(pb, 1);
917 /* Force parsing as several audio frames can be in
918 * one packet and timestamps refer to packet start. */
919 47 sti->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
920 /* ADTS header is in extradata, AAC without header must be
921 * stored as exact frames. Parser not needed and it will
922 * fail. */
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
924 st->codecpar->extradata_size)
925 sti->need_parsing = AVSTREAM_PARSE_NONE;
926 // The flac parser does not work with AVSTREAM_PARSE_TIMESTAMPS
927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47 times.
47 if (st->codecpar->codec_id == AV_CODEC_ID_FLAC)
928 sti->need_parsing = AVSTREAM_PARSE_NONE;
929 /* AVI files with Xan DPCM audio (wrongly) declare PCM
930 * audio in the header but have Axan as stream_code_tag. */
931
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 45 times.
47 if (ast->handler == AV_RL32("Axan")) {
932 2 st->codecpar->codec_id = AV_CODEC_ID_XAN_DPCM;
933 2 st->codecpar->codec_tag = 0;
934 2 ast->dshow_block_align = 0;
935 }
936
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 45 times.
47 if (amv_file_format) {
937 2 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_AMV;
938 2 ast->dshow_block_align = 0;
939 }
940
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 if ((st->codecpar->codec_id == AV_CODEC_ID_AAC ||
941
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 st->codecpar->codec_id == AV_CODEC_ID_FTR ||
942
1/2
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
47 st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
943
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) {
944 av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
945 ast->dshow_block_align = 0;
946 }
947
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 ||
948
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 ||
949
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) {
950 av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
951 ast->sample_size = 0;
952 }
953 47 break;
954 case AVMEDIA_TYPE_SUBTITLE:
955 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
956 sti->request_probe = 1;
957 avio_skip(pb, size);
958 break;
959 default:
960 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
961 st->codecpar->codec_id = AV_CODEC_ID_NONE;
962 st->codecpar->codec_tag = 0;
963 avio_skip(pb, size);
964 break;
965 }
966 }
967 520 break;
968 11 case MKTAG('s', 't', 'r', 'd'):
969
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (stream_index >= (unsigned)s->nb_streams
970
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
11 || s->streams[stream_index]->codecpar->extradata_size
971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 || s->streams[stream_index]->codecpar->codec_tag == MKTAG('H','2','6','4')) {
972 5 avio_skip(pb, size);
973 } else {
974 6 uint64_t cur_pos = avio_tell(pb);
975
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (cur_pos < list_end)
976 6 size = FFMIN(size, list_end - cur_pos);
977 6 AVStream *const st = s->streams[stream_index];
978
979
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (size<(1<<30)) {
980
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (st->codecpar->extradata) {
981 av_log(s, AV_LOG_WARNING, "New extradata in strd chunk, freeing previous one.\n");
982 }
983
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
984 return ret;
985 }
986
987
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
988 avio_r8(pb);
989
990 6 ret = avi_extract_stream_metadata(s, st);
991
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
992 av_log(s, AV_LOG_WARNING, "could not decoding EXIF data in stream header.\n");
993 }
994 }
995 11 break;
996 22 case MKTAG('i', 'n', 'd', 'x'):
997 22 pos = avio_tell(pb);
998
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) &&
999
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 &&
1000 22 read_odml_index(s, 0) < 0 &&
1001
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 (s->error_recognition & AV_EF_EXPLODE))
1002 return AVERROR_INVALIDDATA;
1003 22 avio_seek(pb, pos + size, SEEK_SET);
1004 22 break;
1005 10 case MKTAG('v', 'p', 'r', 'p'):
1006
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) {
1007 10 AVStream *const st = s->streams[stream_index];
1008 AVRational active, active_aspect;
1009
1010 10 avio_rl32(pb);
1011 10 avio_rl32(pb);
1012 10 avio_rl32(pb);
1013 10 avio_rl32(pb);
1014 10 avio_rl32(pb);
1015
1016 10 active_aspect.den = avio_rl16(pb);
1017 10 active_aspect.num = avio_rl16(pb);
1018 10 active.num = avio_rl32(pb);
1019 10 active.den = avio_rl32(pb);
1020 10 avio_rl32(pb); // nbFieldsPerFrame
1021
1022
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 &&
1023
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) {
1024 10 st->sample_aspect_ratio = av_div_q(active_aspect, active);
1025 10 av_log(s, AV_LOG_TRACE, "vprp %d/%d %d/%d\n",
1026 active_aspect.num, active_aspect.den,
1027 active.num, active.den);
1028 }
1029 10 size -= 9 * 4;
1030 }
1031 10 avio_skip(pb, size);
1032 10 break;
1033 19 case MKTAG('s', 't', 'r', 'n'):
1034 case MKTAG('i', 's', 'b', 'j'):
1035 case MKTAG('i', 'n', 'a', 'm'):
1036 case MKTAG('i', 'a', 'r', 't'):
1037 case MKTAG('i', 'c', 'o', 'p'):
1038 case MKTAG('i', 'c', 'm', 't'):
1039 case MKTAG('i', 'g', 'n', 'r'):
1040 case MKTAG('i', 'p', 'o', 'd'):
1041 case MKTAG('i', 's', 'o', 'f'):
1042
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (s->nb_streams) {
1043 19 ret = avi_read_tag(s, s->streams[s->nb_streams - 1], tag, size);
1044
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
1045 return ret;
1046 19 break;
1047 }
1048 av_fallthrough;
1049 default:
1050
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1465 times.
1465 if (size > 1000000) {
1051 av_log(s, AV_LOG_ERROR,
1052 "Something went wrong during header parsing, "
1053 "tag %s has size %u, "
1054 "I will ignore it and try to continue anyway.\n",
1055 av_fourcc2str(tag), size);
1056 if (s->error_recognition & AV_EF_EXPLODE)
1057 return AVERROR_INVALIDDATA;
1058 avi->movi_list = avio_tell(pb) - 4;
1059 avi->movi_end = avi->fsize;
1060 goto end_of_header;
1061 }
1062 av_fallthrough;
1063 /* Do not fail for very large idx1 tags */
1064 case MKTAG('i', 'd', 'x', '1'):
1065 /* skip tag */
1066 1465 size += (size & 1);
1067 1465 avio_skip(pb, size);
1068 1465 break;
1069 }
1070 }
1071
1072 476 end_of_header:
1073 /* check stream number */
1074
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 476 times.
476 if (stream_index != s->nb_streams - 1)
1075 return AVERROR_INVALIDDATA;
1076
1077
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))
1078 470 avi_load_index(s);
1079 476 calculate_bitrate(s);
1080 476 avi->index_loaded |= 1;
1081
1082
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 476 times.
476 if ((ret = guess_ni_flag(s)) < 0)
1083 return ret;
1084
1085 476 avi->non_interleaved |= ret | (s->flags & AVFMT_FLAG_SORT_DTS);
1086
1087 476 dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
1088
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"))
1089 for (i = 0; i < s->nb_streams; i++) {
1090 AVStream *st = s->streams[i];
1091 if ( st->codecpar->codec_id == AV_CODEC_ID_MPEG1VIDEO
1092 || st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1093 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
1094 }
1095
1096
2/2
✓ Branch 0 taken 496 times.
✓ Branch 1 taken 38 times.
534 for (i = 0; i < s->nb_streams; i++) {
1097 496 AVStream *st = s->streams[i];
1098
2/2
✓ Branch 1 taken 438 times.
✓ Branch 2 taken 58 times.
496 if (ffstream(st)->nb_index_entries)
1099 438 break;
1100 }
1101 // DV-in-AVI cannot be non-interleaved, if set this must be
1102 // a mis-detection.
1103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 476 times.
476 if (avi->dv_demux)
1104 avi->non_interleaved = 0;
1105
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) {
1106 2 av_log(s, AV_LOG_WARNING,
1107 "Non-interleaved AVI without index, switching to interleaved\n");
1108 2 avi->non_interleaved = 0;
1109 }
1110
1111
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 469 times.
476 if (avi->non_interleaved) {
1112 7 av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
1113 7 clean_index(s);
1114 }
1115
1116 476 ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
1117 476 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
1118
1119 476 return 0;
1120 }
1121
1122 static int read_gab2_sub(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1123 {
1124 if (pkt->size >= 7 &&
1125 pkt->size < INT_MAX - AVPROBE_PADDING_SIZE &&
1126 !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
1127 uint8_t desc[256];
1128 int score = AVPROBE_SCORE_EXTENSION, ret;
1129 AVIStream *ast = st->priv_data;
1130 const AVInputFormat *sub_demuxer;
1131 AVRational time_base;
1132 int size;
1133 AVProbeData pd;
1134 unsigned int desc_len;
1135
1136 if (ast->sub_ctx)
1137 return 0;
1138
1139 AVIOContext *pb = avio_alloc_context(pkt->data + 7,
1140 pkt->size - 7,
1141 0, NULL, NULL, NULL, NULL);
1142 if (!pb)
1143 goto error;
1144
1145 desc_len = avio_rl32(pb);
1146
1147 if (desc_len > pb->buf_end - pb->buf_ptr)
1148 goto error;
1149
1150 ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
1151 avio_skip(pb, desc_len - ret);
1152 if (*desc)
1153 av_dict_set(&st->metadata, "title", desc, 0);
1154
1155 avio_rl16(pb); /* flags? */
1156 avio_rl32(pb); /* data size */
1157
1158 size = pb->buf_end - pb->buf_ptr;
1159 pd = (AVProbeData) { .buf = av_mallocz(size + AVPROBE_PADDING_SIZE),
1160 .buf_size = size };
1161 if (!pd.buf)
1162 goto error;
1163 memcpy(pd.buf, pb->buf_ptr, size);
1164 sub_demuxer = av_probe_input_format2(&pd, 1, &score);
1165 av_freep(&pd.buf);
1166 if (!sub_demuxer)
1167 goto error;
1168
1169 if (strcmp(sub_demuxer->name, "srt") && strcmp(sub_demuxer->name, "ass"))
1170 goto error;
1171
1172 if (!(ast->sub_pkt = av_packet_alloc()))
1173 goto error;
1174
1175 if (!(ast->sub_ctx = avformat_alloc_context()))
1176 goto error;
1177
1178 ast->sub_ctx->pb = pb;
1179
1180 if (ff_copy_whiteblacklists(ast->sub_ctx, s) < 0)
1181 goto error;
1182
1183 if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
1184 if (ast->sub_ctx->nb_streams != 1)
1185 goto error;
1186 ff_read_packet(ast->sub_ctx, ast->sub_pkt);
1187 avcodec_parameters_copy(st->codecpar, ast->sub_ctx->streams[0]->codecpar);
1188 time_base = ast->sub_ctx->streams[0]->time_base;
1189 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
1190 }
1191 ast->sub_buffer = pkt->buf;
1192 pkt->buf = NULL;
1193 av_packet_unref(pkt);
1194 return 1;
1195
1196 error:
1197 av_packet_free(&ast->sub_pkt);
1198 av_freep(&ast->sub_ctx);
1199 avio_context_free(&pb);
1200 }
1201 return 0;
1202 }
1203
1204 28037 static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
1205 AVPacket *pkt)
1206 {
1207 28037 AVIStream *ast, *next_ast = next_st->priv_data;
1208 28037 int64_t ts, next_ts, ts_min = INT64_MAX;
1209 28037 AVStream *st, *sub_st = NULL;
1210 int i;
1211
1212 28037 next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
1213 28037 AV_TIME_BASE_Q);
1214
1215
2/2
✓ Branch 0 taken 35457 times.
✓ Branch 1 taken 28037 times.
63494 for (i = 0; i < s->nb_streams; i++) {
1216 35457 st = s->streams[i];
1217 35457 ast = st->priv_data;
1218
4/8
✓ Branch 0 taken 30861 times.
✓ Branch 1 taken 4596 times.
✓ Branch 2 taken 30861 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 30861 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
35457 if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt && ast->sub_pkt->data) {
1219 ts = av_rescale_q(ast->sub_pkt->dts, st->time_base, AV_TIME_BASE_Q);
1220 if (ts <= next_ts && ts < ts_min) {
1221 ts_min = ts;
1222 sub_st = st;
1223 }
1224 }
1225 }
1226
1227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28037 times.
28037 if (sub_st) {
1228 ast = sub_st->priv_data;
1229 av_packet_move_ref(pkt, ast->sub_pkt);
1230 pkt->stream_index = sub_st->index;
1231
1232 if (ff_read_packet(ast->sub_ctx, ast->sub_pkt) < 0)
1233 ast->sub_pkt->data = NULL;
1234 }
1235 28037 return sub_st;
1236 }
1237
1238 528311 static int get_stream_idx(const unsigned *d)
1239 {
1240
4/4
✓ Branch 0 taken 304218 times.
✓ Branch 1 taken 224093 times.
✓ Branch 2 taken 97563 times.
✓ Branch 3 taken 206655 times.
528311 if (d[0] >= '0' && d[0] <= '9' &&
1241
4/4
✓ Branch 0 taken 97202 times.
✓ Branch 1 taken 361 times.
✓ Branch 2 taken 65489 times.
✓ Branch 3 taken 31713 times.
97563 d[1] >= '0' && d[1] <= '9') {
1242 65489 return (d[0] - '0') * 10 + (d[1] - '0');
1243 } else {
1244 462822 return 100; // invalid stream ID
1245 }
1246 }
1247
1248 /**
1249 *
1250 * @param exit_early set to 1 to just gather packet position without making the changes needed to actually read & return the packet
1251 */
1252 28125 static int avi_sync(AVFormatContext *s, int exit_early)
1253 {
1254 28125 AVIContext *avi = s->priv_data;
1255 28125 AVIOContext *pb = s->pb;
1256 int n;
1257 unsigned int d[8];
1258 unsigned int size;
1259 int64_t i, sync;
1260
1261 5084 start_sync:
1262 33209 memset(d, -1, sizeof(d));
1263
2/2
✓ Branch 2 taken 372896 times.
✓ Branch 3 taken 488 times.
373384 for (i = sync = avio_tell(pb); !avio_feof(pb); i++) {
1264 int j;
1265
1266
2/2
✓ Branch 0 taken 2610272 times.
✓ Branch 1 taken 372896 times.
2983168 for (j = 0; j < 7; j++)
1267 2610272 d[j] = d[j + 1];
1268 372896 d[7] = avio_r8(pb);
1269
1270 372896 size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
1271
1272 372896 n = get_stream_idx(d + 2);
1273 ff_tlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
1274 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
1275
4/4
✓ Branch 0 taken 129012 times.
✓ Branch 1 taken 243884 times.
✓ Branch 2 taken 14688 times.
✓ Branch 3 taken 114324 times.
372896 if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
1276 258572 continue;
1277
1278 // parse ix##
1279
5/6
✓ Branch 0 taken 481 times.
✓ Branch 1 taken 113843 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 480 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
114324 if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
1280 // parse JUNK
1281
6/8
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 113855 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.
114323 (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
1282
6/8
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 113376 times.
✓ Branch 2 taken 479 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 479 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 479 times.
113856 (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1') ||
1283
4/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 113376 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.
113377 (d[0] == 'i' && d[1] == 'n' && d[2] == 'd' && d[3] == 'x')) {
1284 947 avio_skip(pb, size);
1285 947 goto start_sync;
1286 }
1287
1288 // parse stray LIST
1289
6/8
✓ Branch 0 taken 656 times.
✓ Branch 1 taken 112721 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.
113377 if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
1290 655 avio_skip(pb, 4);
1291 655 goto start_sync;
1292 }
1293
1294 112722 n = get_stream_idx(d);
1295
1296
2/2
✓ Branch 0 taken 41855 times.
✓ Branch 1 taken 70867 times.
112722 if (!((i - avi->last_pkt_pos) & 1) &&
1297
2/2
✓ Branch 1 taken 1105 times.
✓ Branch 2 taken 40750 times.
41855 get_stream_idx(d + 1) < s->nb_streams)
1298 1105 continue;
1299
1300 // detect ##ix chunk and skip
1301
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 111617 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
111617 if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
1302 avio_skip(pb, size);
1303 goto start_sync;
1304 }
1305
1306
3/6
✓ Branch 0 taken 6331 times.
✓ Branch 1 taken 105286 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6331 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
111617 if (d[2] == 'w' && d[3] == 'c' && n < s->nb_streams) {
1307 avio_skip(pb, 16 * 3 + 8);
1308 goto start_sync;
1309 }
1310
1311
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 111617 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
111617 if (avi->dv_demux && n != 0)
1312 continue;
1313
1314 // parse ##dc/##wb
1315
2/2
✓ Branch 0 taken 31119 times.
✓ Branch 1 taken 80498 times.
111617 if (n < s->nb_streams) {
1316 AVStream *st;
1317 AVIStream *ast;
1318 31119 st = s->streams[n];
1319 31119 ast = st->priv_data;
1320
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31119 times.
31119 if (!ast) {
1322 av_log(s, AV_LOG_WARNING, "Skipping foreign stream %d packet\n", n);
1323 continue;
1324 }
1325
1326
2/2
✓ Branch 0 taken 10352 times.
✓ Branch 1 taken 20767 times.
31119 if (s->nb_streams >= 2) {
1327 10352 AVStream *st1 = s->streams[1];
1328 10352 AVIStream *ast1 = st1->priv_data;
1329 // workaround for broken small-file-bug402.avi
1330
4/6
✓ Branch 0 taken 10352 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3726 times.
✓ Branch 3 taken 6626 times.
✓ Branch 4 taken 3726 times.
✗ Branch 5 not taken.
10352 if (ast1 && d[2] == 'w' && d[3] == 'b'
1331
2/2
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 3412 times.
3726 && n == 0
1332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 314 times.
314 && st ->codecpar->codec_type == AVMEDIA_TYPE_VIDEO
1333 && st1->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
1334 && ast->prefix == 'd'*256+'c'
1335 && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
1336 ) {
1337 n = 1;
1338 st = st1;
1339 ast = ast1;
1340 av_log(s, AV_LOG_WARNING,
1341 "Invalid stream + prefix combination, assuming audio.\n");
1342 }
1343 }
1344
1345
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 31115 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
31119 if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
1346 4 int k = avio_r8(pb);
1347 4 int last = (k + avio_r8(pb) - 1) & 0xFF;
1348
1349 4 avio_rl16(pb); // flags
1350
1351 // b + (g << 8) + (r << 16);
1352
2/2
✓ Branch 0 taken 759 times.
✓ Branch 1 taken 4 times.
763 for (; k <= last; k++)
1353 759 ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
1354
1355 4 ast->has_pal = 1;
1356 4 goto start_sync;
1357
4/4
✓ Branch 0 taken 27623 times.
✓ Branch 1 taken 3492 times.
✓ Branch 2 taken 27609 times.
✓ Branch 3 taken 14 times.
31115 } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
1358
2/4
✓ Branch 0 taken 31101 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 31101 times.
31101 d[2] < 128 && d[3] < 128) ||
1359
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 d[2] * 256 + d[3] == ast->prefix /* ||
1360 (d[2] == 'd' && d[3] == 'c') ||
1361 (d[2] == 'w' && d[3] == 'b') */) {
1362
2/2
✓ Branch 0 taken 432 times.
✓ Branch 1 taken 30683 times.
31115 if (exit_early)
1363 432 return 0;
1364
2/2
✓ Branch 0 taken 30078 times.
✓ Branch 1 taken 605 times.
30683 if (d[2] * 256 + d[3] == ast->prefix)
1365 30078 ast->prefix_count++;
1366 else {
1367 605 ast->prefix = d[2] * 256 + d[3];
1368 605 ast->prefix_count = 0;
1369 }
1370
1371
1/2
✓ Branch 0 taken 30683 times.
✗ Branch 1 not taken.
30683 if (!avi->dv_demux &&
1372
4/4
✓ Branch 0 taken 4913 times.
✓ Branch 1 taken 25770 times.
✓ Branch 2 taken 4111 times.
✓ Branch 3 taken 802 times.
30683 ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
1373 // FIXME: needs a little reordering
1374 (st->discard >= AVDISCARD_NONKEY &&
1375 !(pkt->flags & AV_PKT_FLAG_KEY)) */
1376
2/2
✓ Branch 0 taken 2676 times.
✓ Branch 1 taken 27205 times.
29881 || st->discard >= AVDISCARD_ALL)) {
1377
1378 3478 ast->frame_offset += get_duration(ast, size);
1379 3478 avio_skip(pb, size);
1380 3478 goto start_sync;
1381 }
1382
1383 27205 avi->stream_index = n;
1384 27205 ast->packet_size = size + 8;
1385 27205 ast->remaining = size;
1386
1387
2/2
✓ Branch 0 taken 25758 times.
✓ Branch 1 taken 1447 times.
27205 if (size) {
1388 25758 FFStream *const sti = ffstream(st);
1389 25758 uint64_t pos = avio_tell(pb) - 8;
1390
3/4
✓ Branch 0 taken 25701 times.
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 25701 times.
✗ Branch 3 not taken.
25758 if (!sti->index_entries || !sti->nb_index_entries ||
1391
2/2
✓ Branch 0 taken 3149 times.
✓ Branch 1 taken 22552 times.
25701 sti->index_entries[sti->nb_index_entries - 1].pos < pos) {
1392 3206 av_add_index_entry(st, pos, ast->frame_offset, size,
1393 0, AVINDEX_KEYFRAME);
1394 }
1395 }
1396 27205 return 0;
1397 }
1398 }
1399 }
1400
1401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 488 times.
488 if (pb->error)
1402 return pb->error;
1403 488 return AVERROR_EOF;
1404 }
1405
1406 766 static int ni_prepare_read(AVFormatContext *s)
1407 {
1408 766 AVIContext *avi = s->priv_data;
1409 766 int best_stream_index = 0;
1410 766 AVStream *best_st = NULL;
1411 FFStream *best_sti;
1412 AVIStream *best_ast;
1413 766 int64_t best_ts = INT64_MAX;
1414 int i;
1415
1416
2/2
✓ Branch 0 taken 1288 times.
✓ Branch 1 taken 766 times.
2054 for (i = 0; i < s->nb_streams; i++) {
1417 1288 AVStream *st = s->streams[i];
1418 1288 FFStream *const sti = ffstream(st);
1419 1288 AVIStream *ast = st->priv_data;
1420 1288 int64_t ts = ast->frame_offset;
1421 int64_t last_ts;
1422
1423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1288 times.
1288 if (!sti->nb_index_entries)
1424 16 continue;
1425
1426 1288 last_ts = sti->index_entries[sti->nb_index_entries - 1].timestamp;
1427
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)
1428 16 continue;
1429
1430 1272 ts = av_rescale_q(ts, st->time_base,
1431 1272 (AVRational) { FFMAX(1, ast->sample_size),
1432 AV_TIME_BASE });
1433
1434 1272 av_log(s, AV_LOG_TRACE, "%"PRId64" %d/%d %"PRId64"\n", ts,
1435 st->time_base.num, st->time_base.den, ast->frame_offset);
1436
2/2
✓ Branch 0 taken 975 times.
✓ Branch 1 taken 297 times.
1272 if (ts < best_ts) {
1437 975 best_ts = ts;
1438 975 best_st = st;
1439 975 best_stream_index = i;
1440 }
1441 }
1442
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 761 times.
766 if (!best_st)
1443 5 return AVERROR_EOF;
1444
1445 761 best_sti = ffstream(best_st);
1446 761 best_ast = best_st->priv_data;
1447 761 best_ts = best_ast->frame_offset;
1448
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 580 times.
761 if (best_ast->remaining) {
1449 181 i = av_index_search_timestamp(best_st,
1450 best_ts,
1451 AVSEEK_FLAG_ANY |
1452 AVSEEK_FLAG_BACKWARD);
1453 } else {
1454 580 i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
1455
1/2
✓ Branch 0 taken 580 times.
✗ Branch 1 not taken.
580 if (i >= 0)
1456 580 best_ast->frame_offset = best_sti->index_entries[i].timestamp;
1457 }
1458
1459
1/2
✓ Branch 0 taken 761 times.
✗ Branch 1 not taken.
761 if (i >= 0) {
1460 761 int64_t pos = best_sti->index_entries[i].pos;
1461 761 pos += best_ast->packet_size - best_ast->remaining;
1462
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 761 times.
761 if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
1463 return AVERROR_EOF;
1464
1465
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 761 times.
761 av_assert0(best_ast->remaining <= best_ast->packet_size);
1466
1467 761 avi->stream_index = best_stream_index;
1468
2/2
✓ Branch 0 taken 580 times.
✓ Branch 1 taken 181 times.
761 if (!best_ast->remaining)
1469 580 best_ast->packet_size =
1470 580 best_ast->remaining = best_sti->index_entries[i].size;
1471 }
1472 else
1473 return AVERROR_EOF;
1474
1475 761 return 0;
1476 }
1477
1478 28529 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
1479 {
1480 28529 AVIContext *avi = s->priv_data;
1481 28529 AVIOContext *pb = s->pb;
1482 int err;
1483
1484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28529 times.
28529 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1485 int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
1486 if (size >= 0)
1487 return size;
1488 else
1489 goto resync;
1490 }
1491
1492
2/2
✓ Branch 0 taken 27763 times.
✓ Branch 1 taken 766 times.
28529 if (avi->non_interleaved) {
1493 766 err = ni_prepare_read(s);
1494
2/2
✓ Branch 0 taken 761 times.
✓ Branch 1 taken 5 times.
766 if (err < 0)
1495 5 return err;
1496 }
1497
1498 55730 resync:
1499
2/2
✓ Branch 0 taken 28037 times.
✓ Branch 1 taken 27693 times.
55730 if (avi->stream_index >= 0) {
1500 28037 AVStream *st = s->streams[avi->stream_index];
1501 28037 FFStream *const sti = ffstream(st);
1502 28037 AVIStream *ast = st->priv_data;
1503 28037 int dv_demux = CONFIG_DV_DEMUXER && avi->dv_demux;
1504 int size;
1505
1506
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28037 times.
28037 if (get_subtitle_pkt(s, st, pkt))
1507 return 0;
1508
1509 // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
1510
2/2
✓ Branch 0 taken 24654 times.
✓ Branch 1 taken 3383 times.
28037 if (ast->sample_size <= 1)
1511 24654 size = INT_MAX;
1512
2/2
✓ Branch 0 taken 239 times.
✓ Branch 1 taken 3144 times.
3383 else if (ast->sample_size < 32)
1513 // arbitrary multiplier to avoid tiny packets for raw PCM data
1514 239 size = 1024 * ast->sample_size;
1515 else
1516 3144 size = ast->sample_size;
1517
1518
2/2
✓ Branch 0 taken 24666 times.
✓ Branch 1 taken 3371 times.
28037 if (size > ast->remaining)
1519 24666 size = ast->remaining;
1520 28037 avi->last_pkt_pos = avio_tell(pb);
1521 28037 err = av_get_packet(pb, pkt, size);
1522
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 27999 times.
28037 if (err < 0)
1523 38 return err;
1524 27999 size = err;
1525
1526
4/6
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 27980 times.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
27999 if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2 && !dv_demux) {
1527 uint8_t *pal;
1528 19 pal = av_packet_new_side_data(pkt,
1529 AV_PKT_DATA_PALETTE,
1530 AVPALETTE_SIZE);
1531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!pal) {
1532 av_log(s, AV_LOG_ERROR,
1533 "Failed to allocate data for palette\n");
1534 } else {
1535 19 memcpy(pal, ast->pal, AVPALETTE_SIZE);
1536 19 ast->has_pal = 0;
1537 }
1538 }
1539
1540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27999 times.
27999 if (CONFIG_DV_DEMUXER && dv_demux) {
1541 size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
1542 pkt->data, pkt->size, pkt->pos);
1543 pkt->flags |= AV_PKT_FLAG_KEY;
1544 if (size < 0)
1545 av_packet_unref(pkt);
1546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27999 times.
27999 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1547 !st->codecpar->codec_tag && read_gab2_sub(s, st, pkt)) {
1548 ast->frame_offset++;
1549 avi->stream_index = -1;
1550 ast->remaining = 0;
1551 goto resync;
1552 } else {
1553 /* XXX: How to handle B-frames in AVI? */
1554 27999 pkt->dts = ast->frame_offset;
1555 // pkt->dts += ast->start;
1556
2/2
✓ Branch 0 taken 3791 times.
✓ Branch 1 taken 24208 times.
27999 if (ast->sample_size)
1557 3791 pkt->dts /= ast->sample_size;
1558 27999 pkt->stream_index = avi->stream_index;
1559
1560
3/4
✓ Branch 0 taken 23789 times.
✓ Branch 1 taken 4210 times.
✓ Branch 2 taken 23789 times.
✗ Branch 3 not taken.
51788 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->index_entries) {
1561 AVIndexEntry *e;
1562 int index;
1563
1564 23789 index = av_index_search_timestamp(st, ast->frame_offset, AVSEEK_FLAG_ANY);
1565 23789 e = &sti->index_entries[index];
1566
1567
4/4
✓ Branch 0 taken 23676 times.
✓ Branch 1 taken 113 times.
✓ Branch 2 taken 22341 times.
✓ Branch 3 taken 1335 times.
23789 if (index >= 0 && e->timestamp == ast->frame_offset) {
1568
2/2
✓ Branch 0 taken 2655 times.
✓ Branch 1 taken 19686 times.
22341 if (index == sti->nb_index_entries-1) {
1569 2655 int key=1;
1570 2655 uint32_t state=-1;
1571
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 2603 times.
2655 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
1572 52 const uint8_t *ptr = pkt->data, *end = ptr + FFMIN(size, 256);
1573
1/2
✓ Branch 0 taken 109 times.
✗ Branch 1 not taken.
109 while (ptr < end) {
1574 109 ptr = avpriv_find_start_code(ptr, end, &state);
1575
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) {
1576 52 key = !(*ptr & 0xC0);
1577 52 break;
1578 }
1579 }
1580 }
1581
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 2616 times.
2655 if (!key)
1582 39 e->flags &= ~AVINDEX_KEYFRAME;
1583 }
1584
2/2
✓ Branch 0 taken 12725 times.
✓ Branch 1 taken 9616 times.
22341 if (e->flags & AVINDEX_KEYFRAME)
1585 12725 pkt->flags |= AV_PKT_FLAG_KEY;
1586 }
1587 } else {
1588 4210 pkt->flags |= AV_PKT_FLAG_KEY;
1589 }
1590 27999 ast->frame_offset += get_duration(ast, pkt->size);
1591 }
1592 27999 ast->remaining -= err;
1593
2/2
✓ Branch 0 taken 27750 times.
✓ Branch 1 taken 249 times.
27999 if (!ast->remaining) {
1594 27750 avi->stream_index = -1;
1595 27750 ast->packet_size = 0;
1596 }
1597
1598
6/6
✓ Branch 0 taken 27241 times.
✓ Branch 1 taken 758 times.
✓ Branch 2 taken 25794 times.
✓ Branch 3 taken 1447 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 25793 times.
27999 if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
1599 1 av_packet_unref(pkt);
1600 1 goto resync;
1601 }
1602 27998 ast->seek_pos= 0;
1603
1604
6/6
✓ Branch 0 taken 27240 times.
✓ Branch 1 taken 758 times.
✓ Branch 2 taken 27138 times.
✓ Branch 3 taken 102 times.
✓ Branch 4 taken 24095 times.
✓ Branch 5 taken 3043 times.
27998 if (!avi->non_interleaved && sti->nb_index_entries > 1 && avi->index_loaded > 1) {
1605 24095 int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
1606
1607
2/2
✓ Branch 0 taken 23363 times.
✓ Branch 1 taken 732 times.
24095 if (avi->dts_max < dts) {
1608 23363 avi->dts_max = dts;
1609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 732 times.
732 } else if (avi->dts_max - (uint64_t)dts > 2*AV_TIME_BASE) {
1610 avi->non_interleaved= 1;
1611 av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
1612 }
1613 }
1614
1615 27998 return 0;
1616 }
1617
1618
2/2
✓ Branch 1 taken 488 times.
✓ Branch 2 taken 27205 times.
27693 if ((err = avi_sync(s, 0)) < 0)
1619 488 return err;
1620 27205 goto resync;
1621 }
1622
1623 /* XXX: We make the implicit supposition that the positions are sorted
1624 * for each stream. */
1625 432 static int avi_read_idx1(AVFormatContext *s, int size)
1626 {
1627 432 AVIContext *avi = s->priv_data;
1628 432 AVIOContext *pb = s->pb;
1629 int nb_index_entries, i;
1630 AVStream *st;
1631 AVIStream *ast;
1632 int64_t pos;
1633 432 unsigned int index, tag, flags, len, first_packet = 1;
1634 432 int64_t last_pos = -1;
1635 432 unsigned last_idx = -1;
1636 432 int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1637 432 int anykey = 0;
1638
1639 432 nb_index_entries = size / 16;
1640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 432 times.
432 if (nb_index_entries <= 0)
1641 return AVERROR_INVALIDDATA;
1642
1643 432 idx1_pos = avio_tell(pb);
1644 432 avio_seek(pb, avi->movi_list + 4, SEEK_SET);
1645
1/2
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
432 if (avi_sync(s, 1) == 0)
1646 432 first_packet_pos = avio_tell(pb) - 8;
1647 432 avi->stream_index = -1;
1648 432 avio_seek(pb, idx1_pos, SEEK_SET);
1649
1650
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")) {
1651 first_packet_pos = 0;
1652 data_offset = avi->movi_list;
1653 }
1654
1655 /* Read the entries and sort them in each stream component. */
1656
2/2
✓ Branch 0 taken 28045 times.
✓ Branch 1 taken 432 times.
28477 for (i = 0; i < nb_index_entries; i++) {
1657
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 28045 times.
28045 if (avio_feof(pb))
1658 return -1;
1659
1660 28045 tag = avio_rl32(pb);
1661 28045 flags = avio_rl32(pb);
1662 28045 pos = avio_rl32(pb);
1663 28045 len = avio_rl32(pb);
1664 28045 av_log(s, AV_LOG_TRACE, "%d: tag=0x%x flags=0x%x pos=0x%"PRIx64" len=%d/",
1665 i, tag, flags, pos, len);
1666
1667 28045 index = ((tag & 0xff) - '0') * 10;
1668 28045 index += (tag >> 8 & 0xff) - '0';
1669
2/2
✓ Branch 0 taken 290 times.
✓ Branch 1 taken 27755 times.
28045 if (index >= s->nb_streams)
1670 290 continue;
1671 27755 st = s->streams[index];
1672 27755 ast = st->priv_data;
1673
1674 /* Skip 'xxpc' palette change entries in the index until a logic
1675 * to process these is properly implemented. */
1676
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')
1677 2 continue;
1678
1679
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) {
1680
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)
1681 432 data_offset = first_packet_pos - pos;
1682 432 first_packet = 0;
1683 }
1684 27753 pos += data_offset;
1685
1686 27753 av_log(s, AV_LOG_TRACE, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
1687
1688 // even if we have only a single stream, we should
1689 // switch to non-interleaved to get correct timestamps
1690
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27753 times.
27753 if (last_pos == pos)
1691 avi->non_interleaved = 1;
1692
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) {
1693 25524 av_add_index_entry(st, pos, ast->cum_len, len, 0,
1694 25524 (flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
1695 25524 last_idx= pos;
1696 }
1697 27753 ast->cum_len += get_duration(ast, len);
1698 27753 last_pos = pos;
1699 27753 anykey |= flags&AVIIF_INDEX;
1700 }
1701
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 431 times.
432 if (!anykey) {
1702
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (index = 0; index < s->nb_streams; index++) {
1703 1 FFStream *const sti = ffstream(s->streams[index]);
1704
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (sti->nb_index_entries)
1705 1 sti->index_entries[0].flags |= AVINDEX_KEYFRAME;
1706 }
1707 }
1708 432 return 0;
1709 }
1710
1711 /* Scan the index and consider any file with streams more than
1712 * 2 seconds or 64MB apart non-interleaved. */
1713 471 static int check_stream_max_drift(AVFormatContext *s)
1714 {
1715 int64_t min_pos, pos;
1716 int i;
1717 471 int *idx = av_calloc(s->nb_streams, sizeof(*idx));
1718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 471 times.
471 if (!idx)
1719 return AVERROR(ENOMEM);
1720
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) {
1721 31065 int64_t max_dts = INT64_MIN / 2;
1722 31065 int64_t min_dts = INT64_MAX / 2;
1723 31065 int64_t max_buffer = 0;
1724
1725 31065 min_pos = INT64_MAX;
1726
1727
2/2
✓ Branch 0 taken 39543 times.
✓ Branch 1 taken 31065 times.
70608 for (i = 0; i < s->nb_streams; i++) {
1728 39543 AVStream *st = s->streams[i];
1729 39543 AVIStream *ast = st->priv_data;
1730 39543 FFStream *const sti = ffstream(st);
1731 39543 int n = sti->nb_index_entries;
1732
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)
1733 30594 idx[i]++;
1734
2/2
✓ Branch 0 taken 38789 times.
✓ Branch 1 taken 754 times.
39543 if (idx[i] < n) {
1735 int64_t dts;
1736 38789 dts = av_rescale_q(sti->index_entries[idx[i]].timestamp /
1737 38789 FFMAX(ast->sample_size, 1),
1738 38789 st->time_base, AV_TIME_BASE_Q);
1739 38789 min_dts = FFMIN(min_dts, dts);
1740 38789 min_pos = FFMIN(min_pos, sti->index_entries[idx[i]].pos);
1741 }
1742 }
1743
2/2
✓ Branch 0 taken 39543 times.
✓ Branch 1 taken 31065 times.
70608 for (i = 0; i < s->nb_streams; i++) {
1744 39543 AVStream *st = s->streams[i];
1745 39543 FFStream *const sti = ffstream(st);
1746 39543 AVIStream *ast = st->priv_data;
1747
1748
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) {
1749 int64_t dts, delta_dts;
1750 38382 dts = av_rescale_q(sti->index_entries[idx[i] - 1].timestamp /
1751 38382 FFMAX(ast->sample_size, 1),
1752 38382 st->time_base, AV_TIME_BASE_Q);
1753 38382 delta_dts = av_sat_sub64(dts, min_dts);
1754 38382 max_dts = FFMAX(max_dts, dts);
1755 38382 max_buffer = FFMAX(max_buffer,
1756 av_rescale(delta_dts,
1757 st->codecpar->bit_rate,
1758 AV_TIME_BASE));
1759 }
1760 }
1761
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 ||
1762 max_buffer > 1024 * 1024 * 8 * 8) {
1763 av_free(idx);
1764 return 1;
1765 }
1766 }
1767 471 av_free(idx);
1768 471 return 0;
1769 }
1770
1771 476 static int guess_ni_flag(AVFormatContext *s)
1772 {
1773 int i;
1774 476 int64_t last_start = 0;
1775 476 int64_t first_end = INT64_MAX;
1776 476 int64_t oldpos = avio_tell(s->pb);
1777
1778
2/2
✓ Branch 0 taken 520 times.
✓ Branch 1 taken 476 times.
996 for (i = 0; i < s->nb_streams; i++) {
1779 520 AVStream *st = s->streams[i];
1780 520 FFStream *const sti = ffstream(st);
1781 520 int n = sti->nb_index_entries;
1782 unsigned int size;
1783
1784
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 460 times.
520 if (n <= 0)
1785 60 continue;
1786
1787
2/2
✓ Branch 0 taken 419 times.
✓ Branch 1 taken 41 times.
460 if (n >= 2) {
1788 419 int64_t pos = sti->index_entries[0].pos;
1789 unsigned tag[2];
1790 419 avio_seek(s->pb, pos, SEEK_SET);
1791 419 tag[0] = avio_r8(s->pb);
1792 419 tag[1] = avio_r8(s->pb);
1793 419 avio_rl16(s->pb);
1794 419 size = avio_rl32(s->pb);
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 && pos + size > sti->index_entries[1].pos)
1796 last_start = INT64_MAX;
1797
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)
1798 last_start = INT64_MAX;
1799 }
1800
1801
2/2
✓ Branch 0 taken 450 times.
✓ Branch 1 taken 10 times.
460 if (sti->index_entries[0].pos > last_start)
1802 450 last_start = sti->index_entries[0].pos;
1803
2/2
✓ Branch 0 taken 447 times.
✓ Branch 1 taken 13 times.
460 if (sti->index_entries[n - 1].pos < first_end)
1804 447 first_end = sti->index_entries[n - 1].pos;
1805 }
1806 476 avio_seek(s->pb, oldpos, SEEK_SET);
1807
1808
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 471 times.
476 if (last_start > first_end)
1809 5 return 1;
1810
1811 471 return check_stream_max_drift(s);
1812 }
1813
1814 470 static int avi_load_index(AVFormatContext *s)
1815 {
1816 470 AVIContext *avi = s->priv_data;
1817 470 AVIOContext *pb = s->pb;
1818 uint32_t tag, size;
1819 470 int64_t pos = avio_tell(pb);
1820 int64_t next;
1821 470 int ret = -1;
1822
1823
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 470 times.
470 if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
1824 goto the_end; // maybe truncated file
1825 470 av_log(s, AV_LOG_TRACE, "movi_end=0x%"PRIx64"\n", avi->movi_end);
1826 for (;;) {
1827 911 tag = avio_rl32(pb);
1828 911 size = avio_rl32(pb);
1829
2/2
✓ Branch 1 taken 448 times.
✓ Branch 2 taken 463 times.
911 if (avio_feof(pb))
1830 448 break;
1831 463 next = avio_tell(pb);
1832
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))
1833 break;
1834 463 next += size + (size & 1LL);
1835
1836
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') &&
1837 432 avi_read_idx1(s, size) >= 0) {
1838 432 avi->index_loaded=2;
1839 432 ret = 0;
1840
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 22 times.
31 }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
1841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (size < 4) {
1842 av_log(s, AV_LOG_WARNING, "Invalid size (%u) LIST in index\n", size);
1843 break;
1844 }
1845 9 uint32_t tag1 = avio_rl32(pb);
1846
1847
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 4 times.
9 if (tag1 == MKTAG('I', 'N', 'F', 'O'))
1848 5 ff_read_riff_info(s, size - 4);
1849
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 }else if (!ret)
1850 22 break;
1851
1852
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 441 times.
441 if (avio_seek(pb, next, SEEK_SET) < 0)
1853 break; // something is wrong here
1854 }
1855
1856 the_end:
1857 470 avio_seek(pb, pos, SEEK_SET);
1858 470 return ret;
1859 }
1860
1861 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
1862 {
1863 AVIStream *ast2 = st2->priv_data;
1864 int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
1865 av_packet_unref(ast2->sub_pkt);
1866 if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
1867 avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
1868 ff_read_packet(ast2->sub_ctx, ast2->sub_pkt);
1869 }
1870
1871 728 static int avi_read_seek(AVFormatContext *s, int stream_index,
1872 int64_t timestamp, int flags)
1873 {
1874 728 AVIContext *avi = s->priv_data;
1875 AVStream *st;
1876 FFStream *sti;
1877 int i, index;
1878 int64_t pos, pos_min;
1879 AVIStream *ast;
1880
1881 /* Does not matter which stream is requested dv in avi has the
1882 * stream information in the first video stream.
1883 */
1884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
728 if (avi->dv_demux)
1885 stream_index = 0;
1886
1887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
728 if (!avi->index_loaded) {
1888 /* we only load the index on demand */
1889 avi_load_index(s);
1890 avi->index_loaded |= 1;
1891 }
1892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
728 av_assert0(stream_index >= 0);
1893
1894 728 st = s->streams[stream_index];
1895 728 sti = ffstream(st);
1896 728 ast = st->priv_data;
1897
1898
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
728 if (avi->dv_demux) {
1899 // index entries are in the AVI scale/rate timebase, which does
1900 // not match DV demuxer's stream timebase
1901 timestamp = av_rescale_q(timestamp, st->time_base,
1902 (AVRational){ ast->scale, ast->rate });
1903 } else
1904 728 timestamp *= FFMAX(ast->sample_size, 1);
1905
1906 728 index = av_index_search_timestamp(st, timestamp, flags);
1907
2/2
✓ Branch 0 taken 198 times.
✓ Branch 1 taken 530 times.
728 if (index < 0) {
1908
1/2
✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
198 if (sti->nb_index_entries > 0)
1909 198 av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
1910 timestamp,
1911 198 sti->index_entries[0].timestamp,
1912 198 sti->index_entries[sti->nb_index_entries - 1].timestamp);
1913 198 return AVERROR_INVALIDDATA;
1914 }
1915
1916 /* find the position */
1917 530 pos = sti->index_entries[index].pos;
1918 530 timestamp = sti->index_entries[index].timestamp;
1919
1920 530 av_log(s, AV_LOG_TRACE, "XX %"PRId64" %d %"PRId64"\n",
1921 530 timestamp, index, sti->index_entries[index].timestamp);
1922
1923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 530 times.
530 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1924 /* One and only one real stream for DV in AVI, and it has video */
1925 /* offsets. Calling with other stream indexes should have failed */
1926 /* the av_index_search_timestamp call above. */
1927
1928 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1929 return -1;
1930
1931 /* Feed the DV video stream version of the timestamp to the */
1932 /* DV demux so it can synthesize correct timestamps. */
1933 ff_dv_ts_reset(avi->dv_demux,
1934 av_rescale_q(timestamp, (AVRational){ ast->scale, ast->rate },
1935 st->time_base));
1936
1937 avi->stream_index = -1;
1938 return 0;
1939 }
1940 530 timestamp /= FFMAX(ast->sample_size, 1);
1941
1942 530 pos_min = pos;
1943
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 530 times.
1077 for (i = 0; i < s->nb_streams; i++) {
1944 547 AVStream *st2 = s->streams[i];
1945 547 FFStream *const sti2 = ffstream(st2);
1946 547 AVIStream *ast2 = st2->priv_data;
1947
1948 547 ast2->packet_size =
1949 547 ast2->remaining = 0;
1950
1951
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (ast2->sub_ctx) {
1952 seek_subtitle(st, st2, timestamp);
1953 continue;
1954 }
1955
1956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (sti2->nb_index_entries <= 0)
1957 continue;
1958
1959 // av_assert1(st2->codecpar->block_align);
1960 547 index = av_index_search_timestamp(st2,
1961 547 av_rescale_q(timestamp,
1962 st->time_base,
1963 st2->time_base) *
1964 547 FFMAX(ast2->sample_size, 1),
1965 547 flags |
1966 AVSEEK_FLAG_BACKWARD |
1967
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 530 times.
547 (st2->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
1968
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (index < 0)
1969 index = 0;
1970 547 ast2->seek_pos = sti2->index_entries[index].pos;
1971 547 pos_min = FFMIN(pos_min,ast2->seek_pos);
1972 }
1973
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 530 times.
1077 for (i = 0; i < s->nb_streams; i++) {
1974 547 AVStream *st2 = s->streams[i];
1975 547 FFStream *const sti2 = ffstream(st2);
1976 547 AVIStream *ast2 = st2->priv_data;
1977
1978
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)
1979 continue;
1980
1981 547 index = av_index_search_timestamp(
1982 st2,
1983 547 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
1984
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));
1985
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 547 times.
547 if (index < 0)
1986 index = 0;
1987
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)
1988 11 index--;
1989 547 ast2->frame_offset = sti2->index_entries[index].timestamp;
1990 }
1991
1992 /* do the seek */
1993
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 530 times.
530 if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
1994 av_log(s, AV_LOG_ERROR, "Seek failed\n");
1995 return -1;
1996 }
1997 530 avi->stream_index = -1;
1998 530 avi->dts_max = INT_MIN;
1999 530 return 0;
2000 }
2001
2002 476 static int avi_read_close(AVFormatContext *s)
2003 {
2004 int i;
2005 476 AVIContext *avi = s->priv_data;
2006
2007
2/2
✓ Branch 0 taken 520 times.
✓ Branch 1 taken 476 times.
996 for (i = 0; i < s->nb_streams; i++) {
2008 520 AVStream *st = s->streams[i];
2009 520 AVIStream *ast = st->priv_data;
2010
1/2
✓ Branch 0 taken 520 times.
✗ Branch 1 not taken.
520 if (ast) {
2011
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520 times.
520 if (ast->sub_ctx) {
2012 av_freep(&ast->sub_ctx->pb);
2013 avformat_close_input(&ast->sub_ctx);
2014 }
2015 520 av_buffer_unref(&ast->sub_buffer);
2016 520 av_packet_free(&ast->sub_pkt);
2017 }
2018 }
2019
2020 476 av_freep(&avi->dv_demux);
2021
2022 476 return 0;
2023 }
2024
2025 7768 static int avi_probe(const AVProbeData *p)
2026 {
2027 int i;
2028
2029 /* check file header */
2030
2/2
✓ Branch 0 taken 36956 times.
✓ Branch 1 taken 7295 times.
44251 for (i = 0; avi_headers[i][0]; i++)
2031
2/2
✓ Branch 0 taken 2679 times.
✓ Branch 1 taken 34277 times.
36956 if (AV_RL32(p->buf ) == AV_RL32(avi_headers[i] ) &&
2032
2/2
✓ Branch 0 taken 473 times.
✓ Branch 1 taken 2206 times.
2679 AV_RL32(p->buf + 8) == AV_RL32(avi_headers[i] + 4))
2033 473 return AVPROBE_SCORE_MAX;
2034
2035 7295 return 0;
2036 }
2037
2038 const FFInputFormat ff_avi_demuxer = {
2039 .p.name = "avi",
2040 .p.long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
2041 .p.extensions = "avi",
2042 .p.priv_class = &demuxer_class,
2043 .priv_data_size = sizeof(AVIContext),
2044 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
2045 .read_probe = avi_probe,
2046 .read_header = avi_read_header,
2047 .read_packet = avi_read_packet,
2048 .read_close = avi_read_close,
2049 .read_seek = avi_read_seek,
2050 };
2051