FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/avidec.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 863 1195 72.2%
Functions: 21 24 87.5%
Branches: 552 904 61.1%

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