FFmpeg
Loading...
Searching...
No Matches
avidec.c
Go to the documentation of this file.
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
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"
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"
44#include "libavcodec/exif.h"
46
47typedef struct AVIStream {
48 int64_t frame_offset; /* current frame (video) or byte (audio) counter
49 * (used to compute the pts) */
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' */
62 uint32_t pal[256];
64 int dshow_block_align; /* block align variable used to emulate bugs in
65 * the MS dshow demuxer */
66
70
72} AVIStream;
73
94
95
96static 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
101static 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
110static 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
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
133static int avi_load_index(AVFormatContext *s);
134static 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
140static inline int get_duration(AVIStream *ast, int len)
141{
142 if (ast->sample_size)
143 return len;
144 else if (ast->dshow_block_align)
145 return (len + (int64_t)ast->dshow_block_align - 1) / ast->dshow_block_align;
146 else
147 return 1;
148}
149
151{
152 AVIContext *avi = s->priv_data;
153 char header[8] = {0};
154 int i;
155
156 /* check RIFF header */
157 avio_read(pb, header, 4);
158 avi->riff_end = avio_rl32(pb); /* RIFF chunk size */
159 avi->riff_end += avio_tell(pb); /* RIFF chunk end */
160 avio_read(pb, header + 4, 4);
161
162 for (i = 0; avi_headers[i][0]; i++)
163 if (!memcmp(header, avi_headers[i], 8))
164 break;
165 if (!avi_headers[i][0])
166 return AVERROR_INVALIDDATA;
167
168 if (header[7] == 0x19)
170 "This file has been generated by a totally broken muxer.\n");
171
172 return 0;
173}
174
176{
177 AVIContext *avi = s->priv_data;
178 AVIOContext *pb = s->pb;
179 int longs_per_entry = avio_rl16(pb);
180 int index_sub_type = avio_r8(pb);
181 int index_type = avio_r8(pb);
182 int entries_in_use = avio_rl32(pb);
183 int chunk_id = avio_rl32(pb);
184 int64_t base = avio_rl64(pb);
185 int stream_id = ((chunk_id & 0xFF) - '0') * 10 +
186 ((chunk_id >> 8 & 0xFF) - '0');
187 AVStream *st;
188 AVIStream *ast;
189 int i;
190 int64_t last_pos = -1;
191 int64_t filesize = avi->fsize;
192
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 if (stream_id >= s->nb_streams || stream_id < 0)
204 return AVERROR_INVALIDDATA;
205 st = s->streams[stream_id];
206 ast = st->priv_data;
207
208 if (index_sub_type || entries_in_use < 0)
209 return AVERROR_INVALIDDATA;
210
211 avio_rl32(pb);
212
213 if (index_type && longs_per_entry != 2)
214 return AVERROR_INVALIDDATA;
215 if (index_type > 1)
216 return AVERROR_INVALIDDATA;
217
218 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 for (i = 0; i < entries_in_use; i++) {
229 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 if (avi->odml_read > avi->odml_max_pos)
233 return AVERROR_INVALIDDATA;
234
235 if (index_type) {
236 int64_t pos = avio_rl32(pb) + base - 8;
237 int len = avio_rl32(pb);
238 int key = len >= 0;
239 len &= 0x7FFFFFFF;
240 avi->odml_read += 8;
241
242 av_log(s, AV_LOG_TRACE, "pos:%"PRId64", len:%X\n", pos, len);
243
244 if (avio_feof(pb))
245 return AVERROR_INVALIDDATA;
246
247 if (last_pos == pos || pos == base - 8)
248 avi->non_interleaved = 1;
249 if (last_pos != pos && len)
250 av_add_index_entry(st, pos, ast->cum_len, len, 0,
251 key ? AVINDEX_KEYFRAME : 0);
252
253 ast->cum_len += get_duration(ast, len);
254 last_pos = pos;
255 } else {
257 int duration;
258 int ret;
259 avi->odml_read += 16;
260
261 offset = avio_rl64(pb);
262 avio_rl32(pb); /* size */
263 duration = avio_rl32(pb);
264
265 if (avio_feof(pb) || offset > INT64_MAX - 8)
266 return AVERROR_INVALIDDATA;
267
268 pos = avio_tell(pb);
269
270 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 if (avio_seek(pb, offset + 8, SEEK_SET) < 0)
276 return -1;
277 avi->odml_depth++;
278 ret = read_odml_index(s, frame_num);
279 avi->odml_depth--;
280 frame_num += duration;
281
282 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 if (ret < 0)
287 return ret;
288 }
289 }
290 avi->index_loaded = 2;
291 return 0;
292}
293
295{
296 int i;
297 int64_t j;
298
299 for (i = 0; i < s->nb_streams; i++) {
300 AVStream *st = s->streams[i];
301 FFStream *const sti = ffstream(st);
302 AVIStream *ast = st->priv_data;
303 int n = sti->nb_index_entries;
304 int max = ast->sample_size;
305 int64_t pos, size, ts;
306
307 if (n != 1 || ast->sample_size == 0)
308 continue;
309
310 while (max < 1024)
311 max += max;
312
313 pos = sti->index_entries[0].pos;
314 size = sti->index_entries[0].size;
315 ts = sti->index_entries[0].timestamp;
316
317 for (j = 0; j < size; j += max)
318 av_add_index_entry(st, pos + j, ts + j, FFMIN(max, size - j), 0,
320 }
321}
322
323static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag,
324 uint32_t size)
325{
326 AVIOContext *pb = s->pb;
327 char key[5] = { 0 };
328 char *value;
329
330 size += (size & 1);
331
332 if (size == UINT_MAX)
333 return AVERROR(EINVAL);
334 value = av_malloc(size + 1);
335 if (!value)
336 return AVERROR(ENOMEM);
337 if (avio_read(pb, value, size) != size) {
338 av_freep(&value);
339 return AVERROR_INVALIDDATA;
340 }
341 value[size] = 0;
342
343 AV_WL32(key, tag);
344
345 return av_dict_set(st ? &st->metadata : &s->metadata, key, value,
347}
348
349static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
350 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
351
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 if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
358 month, &day, time, &year) == 4) {
359 for (i = 0; i < 12; i++)
360 if (!av_strcasecmp(month, months[i])) {
361 snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s",
362 year, i + 1, day, time);
363 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}
370
371static 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
416{
418 uint8_t *data = st->codecpar->extradata;
419 int data_size = st->codecpar->extradata_size;
420 int tag, offset;
421
422 if (!data || data_size < 8) {
423 return AVERROR_INVALIDDATA;
424 }
425
426 bytestream2_init(&gb, data, data_size);
427
428 tag = bytestream2_get_le32(&gb);
429
430 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);
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 default:
453 break;
454 }
455
456 return 0;
457}
458
460{
461 AVIContext *avi = s->priv_data;
462 int i, j;
463 int64_t lensum = 0;
464 int64_t maxpos = 0;
465
466 for (i = 0; i<s->nb_streams; i++) {
467 int64_t len = 0;
468 FFStream *const sti = ffstream(s->streams[i]);
469
470 if (!sti->nb_index_entries)
471 continue;
472
473 for (j = 0; j < sti->nb_index_entries; j++)
474 len += sti->index_entries[j].size;
475 maxpos = FFMAX(maxpos, sti->index_entries[j-1].pos);
476 lensum += len;
477 }
478 if (maxpos < av_rescale(avi->io_fsize, 9, 10)) // index does not cover the whole file
479 return 0;
480 if (lensum*9/10 > maxpos || lensum < maxpos*9/10) // frame sum and filesize mismatch
481 return 0;
482
483 for (i = 0; i<s->nb_streams; i++) {
484 int64_t len = 0;
485 AVStream *st = s->streams[i];
486 FFStream *const sti = ffstream(st);
488 AVInteger bitrate_i, den_i, num_i;
489
490 for (j = 0; j < sti->nb_index_entries; j++)
491 len += sti->index_entries[j].size;
492
493 if (sti->nb_index_entries < 2 || st->codecpar->bit_rate > 0)
494 continue;
497 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 bitrate_i = av_div_i(num_i, den_i);
499 if (av_cmp_i(bitrate_i, av_int2i(INT64_MAX)) <= 0) {
500 int64_t bitrate = av_i2int(bitrate_i);
501 if (bitrate > 0) {
503 }
504 }
505 }
506 return 1;
507}
508
510{
511 AVIContext *avi = s->priv_data;
512 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 AVIStream *ast = NULL;
518 int avih_width = 0, avih_height = 0;
519 int amv_file_format = 0;
520 uint64_t list_end = 0;
521 int64_t pos;
522 int ret;
523 AVDictionaryEntry *dict_entry;
524
525 avi->stream_index = -1;
526
527 ret = get_riff(s, pb);
528 if (ret < 0)
529 return ret;
530
531 av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
532
533 avi->io_fsize = avi->fsize = avio_size(pb);
534 if (avi->fsize <= 0 || avi->fsize < avi->riff_end)
535 avi->fsize = avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
536
537 /* first list tag */
538 stream_index = -1;
539 codec_type = -1;
540 frame_period = 0;
541 for (;;) {
542 if (avio_feof(pb))
543 return AVERROR_INVALIDDATA;
544 tag = avio_rl32(pb);
545 size = avio_rl32(pb);
546
547 print_tag(s, "tag", tag, size);
548
549 switch (tag) {
550 case MKTAG('L', 'I', 'S', 'T'):
551 list_end = avio_tell(pb) + size;
552 /* Ignored, except at start of video packets. */
553 tag1 = avio_rl32(pb);
554
555 print_tag(s, "list", tag1, 0);
556
557 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
558 avi->movi_list = avio_tell(pb) - 4;
559 if (size)
560 avi->movi_end = avi->movi_list + size + (size & 1);
561 else
562 avi->movi_end = avi->fsize;
563 av_log(s, AV_LOG_TRACE, "movi end=%"PRIx64"\n", avi->movi_end);
564 goto end_of_header;
565 } else if (tag1 == MKTAG('I', 'N', 'F', 'O')) {
566 if (size < 4)
567 return AVERROR_INVALIDDATA;
569 } else if (tag1 == MKTAG('n', 'c', 'd', 't'))
570 avi_read_nikon(s, list_end);
571
572 break;
573 case MKTAG('I', 'D', 'I', 'T'):
574 {
575 unsigned char date[64] = { 0 };
576 size += (size & 1);
577 size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
578 avio_skip(pb, size);
579 avi_metadata_creation_time(&s->metadata, date);
580 break;
581 }
582 case MKTAG('d', 'm', 'l', 'h'):
583 avi->is_odml = 1;
584 avio_skip(pb, size + (size & 1));
585 break;
586 case MKTAG('a', 'm', 'v', 'h'):
587 amv_file_format = 1;
589 case MKTAG('a', 'v', 'i', 'h'):
590 /* AVI header */
591 /* using frame_period is bad idea */
592 frame_period = avio_rl32(pb);
593 avio_rl32(pb); /* max. bytes per second */
594 avio_rl32(pb);
596
597 avio_skip(pb, 2 * 4);
598 avio_rl32(pb);
599 avio_rl32(pb);
600 avih_width = avio_rl32(pb);
601 avih_height = avio_rl32(pb);
602
603 avio_skip(pb, size - 10 * 4);
604 break;
605 case MKTAG('s', 't', 'r', 'h'): {
606 /* stream header */
607
608 tag1 = avio_rl32(pb);
609 handler = avio_rl32(pb); /* codec tag */
610
611 if (tag1 == MKTAG('p', 'a', 'd', 's')) {
612 avio_skip(pb, size - 8);
613 break;
614 }
615 stream_index++;
616 AVStream *const st = avformat_new_stream(s, NULL);
617 if (!st)
618 return AVERROR(ENOMEM);
619
620 st->id = stream_index;
621 ast = av_mallocz(sizeof(AVIStream));
622 if (!ast)
623 return AVERROR(ENOMEM);
624 st->priv_data = ast;
625 if (amv_file_format)
626 tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
627 : MKTAG('v', 'i', 'd', 's');
628
629 print_tag(s, "strh", tag1, -1);
630
631 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)
647
648 ast = s->streams[0]->priv_data;
649 st->priv_data = NULL;
650 ff_remove_stream(s, st);
651
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 av_assert0(stream_index < s->nb_streams);
678 ast->handler = handler;
679
680 avio_rl32(pb); /* flags */
681 avio_rl16(pb); /* priority */
682 avio_rl16(pb); /* language */
683 avio_rl32(pb); /* initial frame */
684 ast->scale = avio_rl32(pb);
685 ast->rate = avio_rl32(pb);
686 if (!(ast->scale && ast->rate)) {
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 if (frame_period) {
693 ast->rate = 1000000;
694 ast->scale = frame_period;
695 } else {
696 ast->rate = 25;
697 ast->scale = 1;
698 }
699 }
700 avpriv_set_pts_info(st, 64, ast->scale, ast->rate);
701
702 ast->cum_len = avio_rl32(pb); /* start */
703 st->nb_frames = avio_rl32(pb);
704
705 st->start_time = 0;
706 avio_rl32(pb); /* buffer size */
707 avio_rl32(pb); /* quality */
708 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 ast->sample_size = avio_rl32(pb);
713 ast->cum_len *= FFMAX(1, ast->sample_size);
714 av_log(s, AV_LOG_TRACE, "%"PRIu32" %"PRIu32" %d\n",
715 ast->rate, ast->scale, ast->sample_size);
716
717 switch (tag1) {
718 case MKTAG('v', 'i', 'd', 's'):
720
721 ast->sample_size = 0;
723 break;
724 case MKTAG('a', 'u', 'd', 's'):
726 break;
727 case MKTAG('t', 'x', 't', 's'):
729 break;
730 case MKTAG('d', 'a', 't', 's'):
732 break;
733 default:
734 av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
735 }
736
737 if (ast->sample_size < 0) {
738 if (s->error_recognition & AV_EF_EXPLODE) {
740 "Invalid sample_size %d at stream %d\n",
741 ast->sample_size,
742 stream_index);
743 return AVERROR_INVALIDDATA;
744 }
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 if (ast->sample_size == 0) {
754 st->duration = st->nb_frames;
755 if (st->duration > 0 && avi->io_fsize > 0 && avi->riff_end > avi->io_fsize) {
756 av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
757 st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
758 }
759 }
760 ast->frame_offset = ast->cum_len;
761 avio_skip(pb, size - 12 * 4);
762 break;
763 }
764 case MKTAG('s', 't', 'r', 'f'):
765 /* stream header */
766 if (!size && (codec_type == AVMEDIA_TYPE_AUDIO ||
768 break;
769 if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
770 avio_skip(pb, size);
771 } else {
772 uint64_t cur_pos = avio_tell(pb);
773 unsigned esize;
774 if (cur_pos < list_end)
775 size = FFMIN(size, list_end - cur_pos);
776 AVStream *const st = s->streams[stream_index];
777 FFStream *const sti = ffstream(st);
779 avio_skip(pb, size);
780 break;
781 }
782 switch (codec_type) {
784 if (amv_file_format) {
785 st->codecpar->width = avih_width;
786 st->codecpar->height = avih_height;
789 avio_skip(pb, size);
790 break;
791 }
792 tag1 = ff_get_bmp_header(pb, st, &esize);
793
794 if (tag1 == MKTAG('D', 'X', 'S', 'B') ||
795 tag1 == MKTAG('D', 'X', 'S', 'A')) {
797 st->codecpar->codec_tag = tag1;
799 break;
800 }
801
802 if (size > 10 * 4 && size < (1 << 30) && size < avi->fsize) {
803 if (esize == size-1 && (esize&1)) {
804 st->codecpar->extradata_size = esize - 10 * 4;
805 } else
806 st->codecpar->extradata_size = size - 10 * 4;
807 if (st->codecpar->extradata) {
808 av_log(s, AV_LOG_WARNING, "New extradata in strf chunk, freeing previous one.\n");
809 }
810 ret = ff_get_extradata(s, st->codecpar, pb,
812 if (ret < 0)
813 return ret;
814 }
815
816 // FIXME: check if the encoder really did this correctly
817 if (st->codecpar->extradata_size & 1)
818 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 if (st->codecpar->extradata_size &&
825 (st->codecpar->bits_per_coded_sample <= 8)) {
826 int pal_size = (1 << st->codecpar->bits_per_coded_sample) << 2;
827 const uint8_t *pal_src;
828
829 pal_size = FFMIN(pal_size, st->codecpar->extradata_size);
830 pal_src = st->codecpar->extradata +
831 st->codecpar->extradata_size - pal_size;
832 /* Exclude the "BottomUp" field from the palette */
833 if (pal_src - st->codecpar->extradata >= 9 &&
834 !memcmp(st->codecpar->extradata + st->codecpar->extradata_size - 9, "BottomUp", 9))
835 pal_src -= 9;
836 for (i = 0; i < pal_size / 4; i++)
837 ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src + 4 * i);
838 ast->has_pal = 1;
839 }
840
841 print_tag(s, "video", tag1, 0);
842
844 st->codecpar->codec_tag = tag1;
846 tag1);
847 /* If codec is not found yet, try with the mov tags. */
848 if (!st->codecpar->codec_id) {
849 st->codecpar->codec_id =
851 if (st->codecpar->codec_id)
853 "mov tag found in avi (fourcc %s)\n",
854 av_fourcc2str(tag1));
855 }
856 if (!st->codecpar->codec_id)
858
859 /* This is needed to get the pict type which is necessary
860 * for generating correct pts. */
862
863 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4 &&
864 ast->handler == MKTAG('X', 'V', 'I', 'D'))
865 st->codecpar->codec_tag = MKTAG('X', 'V', 'I', 'D');
866
867 if (st->codecpar->codec_tag == MKTAG('V', 'S', 'S', 'H'))
871 if (st->codecpar->codec_id == AV_CODEC_ID_HEVC &&
872 st->codecpar->codec_tag == MKTAG('H', '2', '6', '5'))
874
875 if (st->codecpar->codec_id == AV_CODEC_ID_AVRN &&
876 st->codecpar->codec_tag == MKTAG('A', 'V', 'R', 'n') &&
877 (st->codecpar->extradata_size < 31 ||
878 memcmp(&st->codecpar->extradata[28], "1:1", 3)))
880
881 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,
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 if (st->codecpar->height == INT_MIN)
894 return AVERROR_INVALIDDATA;
895 st->codecpar->height = FFABS(st->codecpar->height);
896
897// avio_skip(pb, size - 5 * 4);
898 break;
900 ret = ff_get_wav_header(s, pb, st->codecpar, size, 0);
901 if (ret < 0)
902 return ret;
904 if (ast->sample_size && st->codecpar->block_align &&
905 ast->sample_size != st->codecpar->block_align) {
906 av_log(s,
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 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. */
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 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
926 // The flac parser does not work with AVSTREAM_PARSE_TIMESTAMPS
929 /* AVI files with Xan DPCM audio (wrongly) declare PCM
930 * audio in the header but have Axan as stream_code_tag. */
931 if (ast->handler == AV_RL32("Axan")) {
933 st->codecpar->codec_tag = 0;
934 ast->dshow_block_align = 0;
935 }
936 if (amv_file_format) {
938 ast->dshow_block_align = 0;
939 }
940 if ((st->codecpar->codec_id == AV_CODEC_ID_AAC ||
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 if (st->codecpar->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
948 st->codecpar->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
949 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 break;
956 sti->request_probe = 1;
957 avio_skip(pb, size);
958 break;
959 default:
962 st->codecpar->codec_tag = 0;
963 avio_skip(pb, size);
964 break;
965 }
966 }
967 break;
968 case MKTAG('s', 't', 'r', 'd'):
969 if (stream_index >= (unsigned)s->nb_streams
970 || s->streams[stream_index]->codecpar->extradata_size
971 || s->streams[stream_index]->codecpar->codec_tag == MKTAG('H','2','6','4')) {
972 avio_skip(pb, size);
973 } else {
974 uint64_t cur_pos = avio_tell(pb);
975 if (cur_pos < list_end)
976 size = FFMIN(size, list_end - cur_pos);
977 AVStream *const st = s->streams[stream_index];
978
979 if (size<(1<<30)) {
980 if (st->codecpar->extradata) {
981 av_log(s, AV_LOG_WARNING, "New extradata in strd chunk, freeing previous one.\n");
982 }
983 if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
984 return ret;
985 }
986
987 if (st->codecpar->extradata_size & 1) //FIXME check if the encoder really did this correctly
988 avio_r8(pb);
989
991 if (ret < 0) {
992 av_log(s, AV_LOG_WARNING, "could not decoding EXIF data in stream header.\n");
993 }
994 }
995 break;
996 case MKTAG('i', 'n', 'd', 'x'):
997 pos = avio_tell(pb);
998 if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !(s->flags & AVFMT_FLAG_IGNIDX) &&
999 avi->use_odml &&
1000 read_odml_index(s, 0) < 0 &&
1001 (s->error_recognition & AV_EF_EXPLODE))
1002 return AVERROR_INVALIDDATA;
1003 avio_seek(pb, pos + size, SEEK_SET);
1004 break;
1005 case MKTAG('v', 'p', 'r', 'p'):
1006 if (stream_index < (unsigned)s->nb_streams && size > 9 * 4) {
1007 AVStream *const st = s->streams[stream_index];
1008 AVRational active, active_aspect;
1009
1010 avio_rl32(pb);
1011 avio_rl32(pb);
1012 avio_rl32(pb);
1013 avio_rl32(pb);
1014 avio_rl32(pb);
1015
1016 active_aspect.den = avio_rl16(pb);
1017 active_aspect.num = avio_rl16(pb);
1018 active.num = avio_rl32(pb);
1019 active.den = avio_rl32(pb);
1020 avio_rl32(pb); // nbFieldsPerFrame
1021
1022 if (active_aspect.num && active_aspect.den &&
1023 active.num && active.den) {
1024 st->sample_aspect_ratio = av_div_q(active_aspect, active);
1025 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 size -= 9 * 4;
1030 }
1031 avio_skip(pb, size);
1032 break;
1033 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 if (s->nb_streams) {
1043 ret = avi_read_tag(s, s->streams[s->nb_streams - 1], tag, size);
1044 if (ret < 0)
1045 return ret;
1046 break;
1047 }
1049 default:
1050 if (size > 1000000) {
1052 "Something went wrong during header parsing, "
1053 "tag %s has size %u, "
1054 "I will ignore it and try to continue anyway.\n",
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 }
1063 /* Do not fail for very large idx1 tags */
1064 case MKTAG('i', 'd', 'x', '1'):
1065 /* skip tag */
1066 size += (size & 1);
1067 avio_skip(pb, size);
1068 break;
1069 }
1070 }
1071
1072end_of_header:
1073 /* check stream number */
1074 if (stream_index != s->nb_streams - 1)
1075 return AVERROR_INVALIDDATA;
1076
1077 if (!avi->index_loaded && (pb->seekable & AVIO_SEEKABLE_NORMAL))
1080 avi->index_loaded |= 1;
1081
1082 if ((ret = guess_ni_flag(s)) < 0)
1083 return ret;
1084
1085 avi->non_interleaved |= ret | (s->flags & AVFMT_FLAG_SORT_DTS);
1086
1087 dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
1088 if (dict_entry && !strcmp(dict_entry->value, "PotEncoder"))
1089 for (i = 0; i < s->nb_streams; i++) {
1090 AVStream *st = s->streams[i];
1094 }
1095
1096 for (i = 0; i < s->nb_streams; i++) {
1097 AVStream *st = s->streams[i];
1098 if (ffstream(st)->nb_index_entries)
1099 break;
1100 }
1101 // DV-in-AVI cannot be non-interleaved, if set this must be
1102 // a mis-detection.
1103 if (avi->dv_demux)
1104 avi->non_interleaved = 0;
1105 if (i == s->nb_streams && avi->non_interleaved) {
1107 "Non-interleaved AVI without index, switching to interleaved\n");
1108 avi->non_interleaved = 0;
1109 }
1110
1111 if (avi->non_interleaved) {
1112 av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
1113 clean_index(s);
1114 }
1115
1118
1119 return 0;
1120}
1121
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;
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);
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;
1194 return 1;
1195
1196error:
1197 av_packet_free(&ast->sub_pkt);
1198 av_freep(&ast->sub_ctx);
1199 avio_context_free(&pb);
1200 }
1201 return 0;
1202}
1203
1205 AVPacket *pkt)
1206{
1207 AVIStream *ast, *next_ast = next_st->priv_data;
1208 int64_t ts, next_ts, ts_min = INT64_MAX;
1209 AVStream *st, *sub_st = NULL;
1210 int i;
1211
1212 next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
1214
1215 for (i = 0; i < s->nb_streams; i++) {
1216 st = s->streams[i];
1217 ast = st->priv_data;
1218 if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt && ast->sub_pkt->data) {
1220 if (ts <= next_ts && ts < ts_min) {
1221 ts_min = ts;
1222 sub_st = st;
1223 }
1224 }
1225 }
1226
1227 if (sub_st) {
1228 ast = sub_st->priv_data;
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 return sub_st;
1236}
1237
1238static int get_stream_idx(const unsigned *d)
1239{
1240 if (d[0] >= '0' && d[0] <= '9' &&
1241 d[1] >= '0' && d[1] <= '9') {
1242 return (d[0] - '0') * 10 + (d[1] - '0');
1243 } else {
1244 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 */
1252static int avi_sync(AVFormatContext *s, int exit_early)
1253{
1254 AVIContext *avi = s->priv_data;
1255 AVIOContext *pb = s->pb;
1256 int n;
1257 unsigned int d[8];
1258 unsigned int size;
1259 int64_t i, sync;
1260
1261start_sync:
1262 memset(d, -1, sizeof(d));
1263 for (i = sync = avio_tell(pb); !avio_feof(pb); i++) {
1264 int j;
1265
1266 for (j = 0; j < 7; j++)
1267 d[j] = d[j + 1];
1268 d[7] = avio_r8(pb);
1269
1270 size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
1271
1272 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 if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
1276 continue;
1277
1278 // parse ix##
1279 if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
1280 // parse JUNK
1281 (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
1282 (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1') ||
1283 (d[0] == 'i' && d[1] == 'n' && d[2] == 'd' && d[3] == 'x')) {
1284 avio_skip(pb, size);
1285 goto start_sync;
1286 }
1287
1288 // parse stray LIST
1289 if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
1290 avio_skip(pb, 4);
1291 goto start_sync;
1292 }
1293
1294 n = get_stream_idx(d);
1295
1296 if (!((i - avi->last_pkt_pos) & 1) &&
1297 get_stream_idx(d + 1) < s->nb_streams)
1298 continue;
1299
1300 // detect ##ix chunk and skip
1301 if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
1302 avio_skip(pb, size);
1303 goto start_sync;
1304 }
1305
1306 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 if (avi->dv_demux && n != 0)
1312 continue;
1313
1314 // parse ##dc/##wb
1315 if (n < s->nb_streams) {
1316 AVStream *st;
1317 AVIStream *ast;
1318 st = s->streams[n];
1319 ast = st->priv_data;
1320
1321 if (!ast) {
1322 av_log(s, AV_LOG_WARNING, "Skipping foreign stream %d packet\n", n);
1323 continue;
1324 }
1325
1326 if (s->nb_streams >= 2) {
1327 AVStream *st1 = s->streams[1];
1328 AVIStream *ast1 = st1->priv_data;
1329 // workaround for broken small-file-bug402.avi
1330 if (ast1 && d[2] == 'w' && d[3] == 'b'
1331 && n == 0
1332 && st ->codecpar->codec_type == AVMEDIA_TYPE_VIDEO
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;
1341 "Invalid stream + prefix combination, assuming audio.\n");
1342 }
1343 }
1344
1345 if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
1346 int k = avio_r8(pb);
1347 int last = (k + avio_r8(pb) - 1) & 0xFF;
1348
1349 avio_rl16(pb); // flags
1350
1351 // b + (g << 8) + (r << 16);
1352 for (; k <= last; k++)
1353 ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
1354
1355 ast->has_pal = 1;
1356 goto start_sync;
1357 } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
1358 d[2] < 128 && d[3] < 128) ||
1359 d[2] * 256 + d[3] == ast->prefix /* ||
1360 (d[2] == 'd' && d[3] == 'c') ||
1361 (d[2] == 'w' && d[3] == 'b') */) {
1362 if (exit_early)
1363 return 0;
1364 if (d[2] * 256 + d[3] == ast->prefix)
1365 ast->prefix_count++;
1366 else {
1367 ast->prefix = d[2] * 256 + d[3];
1368 ast->prefix_count = 0;
1369 }
1370
1371 if (!avi->dv_demux &&
1372 ((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 || st->discard >= AVDISCARD_ALL)) {
1377
1378 ast->frame_offset += get_duration(ast, size);
1379 avio_skip(pb, size);
1380 goto start_sync;
1381 }
1382
1383 avi->stream_index = n;
1384 ast->packet_size = size + 8;
1385 ast->remaining = size;
1386
1387 if (size) {
1388 FFStream *const sti = ffstream(st);
1389 uint64_t pos = avio_tell(pb) - 8;
1390 if (!sti->index_entries || !sti->nb_index_entries ||
1391 sti->index_entries[sti->nb_index_entries - 1].pos < pos) {
1393 0, AVINDEX_KEYFRAME);
1394 }
1395 }
1396 return 0;
1397 }
1398 }
1399 }
1400
1401 if (pb->error)
1402 return pb->error;
1403 return AVERROR_EOF;
1404}
1405
1407{
1408 AVIContext *avi = s->priv_data;
1409 int best_stream_index = 0;
1410 AVStream *best_st = NULL;
1411 FFStream *best_sti;
1412 AVIStream *best_ast;
1413 int64_t best_ts = INT64_MAX;
1414 int i;
1415
1416 for (i = 0; i < s->nb_streams; i++) {
1417 AVStream *st = s->streams[i];
1418 FFStream *const sti = ffstream(st);
1419 AVIStream *ast = st->priv_data;
1420 int64_t ts = ast->frame_offset;
1421 int64_t last_ts;
1422
1423 if (!sti->nb_index_entries)
1424 continue;
1425
1426 last_ts = sti->index_entries[sti->nb_index_entries - 1].timestamp;
1427 if (!ast->remaining && ts > last_ts)
1428 continue;
1429
1430 ts = av_rescale_q(ts, st->time_base,
1431 (AVRational) { FFMAX(1, ast->sample_size),
1432 AV_TIME_BASE });
1433
1434 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 if (ts < best_ts) {
1437 best_ts = ts;
1438 best_st = st;
1439 best_stream_index = i;
1440 }
1441 }
1442 if (!best_st)
1443 return AVERROR_EOF;
1444
1445 best_sti = ffstream(best_st);
1446 best_ast = best_st->priv_data;
1447 best_ts = best_ast->frame_offset;
1448 if (best_ast->remaining) {
1449 i = av_index_search_timestamp(best_st,
1450 best_ts,
1453 } else {
1454 i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
1455 if (i >= 0)
1456 best_ast->frame_offset = best_sti->index_entries[i].timestamp;
1457 }
1458
1459 if (i >= 0) {
1460 int64_t pos = best_sti->index_entries[i].pos;
1461 pos += best_ast->packet_size - best_ast->remaining;
1462 if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
1463 return AVERROR_EOF;
1464
1465 av_assert0(best_ast->remaining <= best_ast->packet_size);
1466
1467 avi->stream_index = best_stream_index;
1468 if (!best_ast->remaining)
1469 best_ast->packet_size =
1470 best_ast->remaining = best_sti->index_entries[i].size;
1471 }
1472 else
1473 return AVERROR_EOF;
1474
1475 return 0;
1476}
1477
1479{
1480 AVIContext *avi = s->priv_data;
1481 AVIOContext *pb = s->pb;
1482 int err;
1483
1484 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1486 if (size >= 0)
1487 return size;
1488 else
1489 goto resync;
1490 }
1491
1492 if (avi->non_interleaved) {
1493 err = ni_prepare_read(s);
1494 if (err < 0)
1495 return err;
1496 }
1497
1498resync:
1499 if (avi->stream_index >= 0) {
1500 AVStream *st = s->streams[avi->stream_index];
1501 FFStream *const sti = ffstream(st);
1502 AVIStream *ast = st->priv_data;
1503 int dv_demux = CONFIG_DV_DEMUXER && avi->dv_demux;
1504 int size;
1505
1506 if (get_subtitle_pkt(s, st, pkt))
1507 return 0;
1508
1509 // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
1510 if (ast->sample_size <= 1)
1511 size = INT_MAX;
1512 else if (ast->sample_size < 32)
1513 // arbitrary multiplier to avoid tiny packets for raw PCM data
1514 size = 1024 * ast->sample_size;
1515 else
1516 size = ast->sample_size;
1517
1518 if (size > ast->remaining)
1519 size = ast->remaining;
1520 avi->last_pkt_pos = avio_tell(pb);
1521 err = av_get_packet(pb, pkt, size);
1522 if (err < 0)
1523 return err;
1524 size = err;
1525
1526 if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2 && !dv_demux) {
1527 uint8_t *pal;
1531 if (!pal) {
1533 "Failed to allocate data for palette\n");
1534 } else {
1535 memcpy(pal, ast->pal, AVPALETTE_SIZE);
1536 ast->has_pal = 0;
1537 }
1538 }
1539
1540 if (CONFIG_DV_DEMUXER && dv_demux) {
1542 pkt->data, pkt->size, pkt->pos);
1543 pkt->flags |= AV_PKT_FLAG_KEY;
1544 if (size < 0)
1546 } 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 pkt->dts = ast->frame_offset;
1555// pkt->dts += ast->start;
1556 if (ast->sample_size)
1557 pkt->dts /= ast->sample_size;
1558 pkt->stream_index = avi->stream_index;
1559
1561 AVIndexEntry *e;
1562 int index;
1563
1565 e = &sti->index_entries[index];
1566
1567 if (index >= 0 && e->timestamp == ast->frame_offset) {
1568 if (index == sti->nb_index_entries-1) {
1569 int key=1;
1570 uint32_t state=-1;
1571 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
1572 const uint8_t *ptr = pkt->data, *end = ptr + FFMIN(size, 256);
1573 while (ptr < end) {
1574 ptr = avpriv_find_start_code(ptr, end, &state);
1575 if (state == 0x1B6 && ptr < end) {
1576 key = !(*ptr & 0xC0);
1577 break;
1578 }
1579 }
1580 }
1581 if (!key)
1583 }
1584 if (e->flags & AVINDEX_KEYFRAME)
1585 pkt->flags |= AV_PKT_FLAG_KEY;
1586 }
1587 } else {
1588 pkt->flags |= AV_PKT_FLAG_KEY;
1589 }
1590 ast->frame_offset += get_duration(ast, pkt->size);
1591 }
1592 ast->remaining -= err;
1593 if (!ast->remaining) {
1594 avi->stream_index = -1;
1595 ast->packet_size = 0;
1596 }
1597
1598 if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
1600 goto resync;
1601 }
1602 ast->seek_pos= 0;
1603
1604 if (!avi->non_interleaved && sti->nb_index_entries > 1 && avi->index_loaded > 1) {
1606
1607 if (avi->dts_max < dts) {
1608 avi->dts_max = dts;
1609 } 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 return 0;
1616 }
1617
1618 if ((err = avi_sync(s, 0)) < 0)
1619 return err;
1620 goto resync;
1621}
1622
1623/* XXX: We make the implicit supposition that the positions are sorted
1624 * for each stream. */
1626{
1627 AVIContext *avi = s->priv_data;
1628 AVIOContext *pb = s->pb;
1629 int nb_index_entries, i;
1630 AVStream *st;
1631 AVIStream *ast;
1632 int64_t pos;
1633 unsigned int index, tag, flags, len, first_packet = 1;
1634 int64_t last_pos = -1;
1635 unsigned last_idx = -1;
1636 int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1637 int anykey = 0;
1638
1639 nb_index_entries = size / 16;
1640 if (nb_index_entries <= 0)
1641 return AVERROR_INVALIDDATA;
1642
1643 idx1_pos = avio_tell(pb);
1644 avio_seek(pb, avi->movi_list + 4, SEEK_SET);
1645 if (avi_sync(s, 1) == 0)
1646 first_packet_pos = avio_tell(pb) - 8;
1647 avi->stream_index = -1;
1648 avio_seek(pb, idx1_pos, SEEK_SET);
1649
1650 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 for (i = 0; i < nb_index_entries; i++) {
1657 if (avio_feof(pb))
1658 return -1;
1659
1660 tag = avio_rl32(pb);
1661 flags = avio_rl32(pb);
1662 pos = avio_rl32(pb);
1663 len = avio_rl32(pb);
1664 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 index = ((tag & 0xff) - '0') * 10;
1668 index += (tag >> 8 & 0xff) - '0';
1669 if (index >= s->nb_streams)
1670 continue;
1671 st = s->streams[index];
1672 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 if ((tag >> 16 & 0xff) == 'p' && (tag >> 24 & 0xff) == 'c')
1677 continue;
1678
1679 if (first_packet && first_packet_pos) {
1680 if (avi->movi_list + 4 != pos || pos + 500 > first_packet_pos)
1681 data_offset = first_packet_pos - pos;
1682 first_packet = 0;
1683 }
1684 pos += data_offset;
1685
1686 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 if (last_pos == pos)
1691 avi->non_interleaved = 1;
1692 if (last_idx != pos && len) {
1693 av_add_index_entry(st, pos, ast->cum_len, len, 0,
1695 last_idx= pos;
1696 }
1697 ast->cum_len += get_duration(ast, len);
1698 last_pos = pos;
1699 anykey |= flags&AVIIF_INDEX;
1700 }
1701 if (!anykey) {
1702 for (index = 0; index < s->nb_streams; index++) {
1703 FFStream *const sti = ffstream(s->streams[index]);
1704 if (sti->nb_index_entries)
1706 }
1707 }
1708 return 0;
1709}
1710
1711/* Scan the index and consider any file with streams more than
1712 * 2 seconds or 64MB apart non-interleaved. */
1714{
1715 int64_t min_pos, pos;
1716 int i;
1717 int *idx = av_calloc(s->nb_streams, sizeof(*idx));
1718 if (!idx)
1719 return AVERROR(ENOMEM);
1720 for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1ULL) {
1721 int64_t max_dts = INT64_MIN / 2;
1722 int64_t min_dts = INT64_MAX / 2;
1723 int64_t max_buffer = 0;
1724
1725 min_pos = INT64_MAX;
1726
1727 for (i = 0; i < s->nb_streams; i++) {
1728 AVStream *st = s->streams[i];
1729 AVIStream *ast = st->priv_data;
1730 FFStream *const sti = ffstream(st);
1731 int n = sti->nb_index_entries;
1732 while (idx[i] < n && sti->index_entries[idx[i]].pos < pos)
1733 idx[i]++;
1734 if (idx[i] < n) {
1735 int64_t dts;
1736 dts = av_rescale_q(sti->index_entries[idx[i]].timestamp /
1737 FFMAX(ast->sample_size, 1),
1739 min_dts = FFMIN(min_dts, dts);
1740 min_pos = FFMIN(min_pos, sti->index_entries[idx[i]].pos);
1741 }
1742 }
1743 for (i = 0; i < s->nb_streams; i++) {
1744 AVStream *st = s->streams[i];
1745 FFStream *const sti = ffstream(st);
1746 AVIStream *ast = st->priv_data;
1747
1748 if (idx[i] && min_dts != INT64_MAX / 2) {
1749 int64_t dts, delta_dts;
1750 dts = av_rescale_q(sti->index_entries[idx[i] - 1].timestamp /
1751 FFMAX(ast->sample_size, 1),
1753 delta_dts = av_sat_sub64(dts, min_dts);
1754 max_dts = FFMAX(max_dts, dts);
1755 max_buffer = FFMAX(max_buffer,
1756 av_rescale(delta_dts,
1757 st->codecpar->bit_rate,
1758 AV_TIME_BASE));
1759 }
1760 }
1761 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 av_free(idx);
1768 return 0;
1769}
1770
1772{
1773 int i;
1774 int64_t last_start = 0;
1775 int64_t first_end = INT64_MAX;
1776 int64_t oldpos = avio_tell(s->pb);
1777
1778 for (i = 0; i < s->nb_streams; i++) {
1779 AVStream *st = s->streams[i];
1780 FFStream *const sti = ffstream(st);
1781 int n = sti->nb_index_entries;
1782 unsigned int size;
1783
1784 if (n <= 0)
1785 continue;
1786
1787 if (n >= 2) {
1788 int64_t pos = sti->index_entries[0].pos;
1789 unsigned tag[2];
1790 avio_seek(s->pb, pos, SEEK_SET);
1791 tag[0] = avio_r8(s->pb);
1792 tag[1] = avio_r8(s->pb);
1793 avio_rl16(s->pb);
1794 size = avio_rl32(s->pb);
1795 if (get_stream_idx(tag) == i && pos + size > sti->index_entries[1].pos)
1796 last_start = INT64_MAX;
1797 if (get_stream_idx(tag) == i && size == sti->index_entries[0].size + 8)
1798 last_start = INT64_MAX;
1799 }
1800
1801 if (sti->index_entries[0].pos > last_start)
1802 last_start = sti->index_entries[0].pos;
1803 if (sti->index_entries[n - 1].pos < first_end)
1804 first_end = sti->index_entries[n - 1].pos;
1805 }
1806 avio_seek(s->pb, oldpos, SEEK_SET);
1807
1808 if (last_start > first_end)
1809 return 1;
1810
1811 return check_stream_max_drift(s);
1812}
1813
1815{
1816 AVIContext *avi = s->priv_data;
1817 AVIOContext *pb = s->pb;
1818 uint32_t tag, size;
1819 int64_t pos = avio_tell(pb);
1820 int64_t next;
1821 int ret = -1;
1822
1823 if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
1824 goto the_end; // maybe truncated file
1825 av_log(s, AV_LOG_TRACE, "movi_end=0x%"PRIx64"\n", avi->movi_end);
1826 for (;;) {
1827 tag = avio_rl32(pb);
1828 size = avio_rl32(pb);
1829 if (avio_feof(pb))
1830 break;
1831 next = avio_tell(pb);
1832 if (next < 0 || next > INT64_MAX - size - (size & 1))
1833 break;
1834 next += size + (size & 1LL);
1835
1836 if (tag == MKTAG('i', 'd', 'x', '1') &&
1837 avi_read_idx1(s, size) >= 0) {
1838 avi->index_loaded=2;
1839 ret = 0;
1840 }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
1841 if (size < 4) {
1842 av_log(s, AV_LOG_WARNING, "Invalid size (%u) LIST in index\n", size);
1843 break;
1844 }
1845 uint32_t tag1 = avio_rl32(pb);
1846
1847 if (tag1 == MKTAG('I', 'N', 'F', 'O'))
1848 ff_read_riff_info(s, size - 4);
1849 }else if (!ret)
1850 break;
1851
1852 if (avio_seek(pb, next, SEEK_SET) < 0)
1853 break; // something is wrong here
1854 }
1855
1856the_end:
1857 avio_seek(pb, pos, SEEK_SET);
1858 return ret;
1859}
1860
1861static 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
1871static int avi_read_seek(AVFormatContext *s, int stream_index,
1872 int64_t timestamp, int flags)
1873{
1874 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 if (avi->dv_demux)
1885 stream_index = 0;
1886
1887 if (!avi->index_loaded) {
1888 /* we only load the index on demand */
1890 avi->index_loaded |= 1;
1891 }
1892 av_assert0(stream_index >= 0);
1893
1894 st = s->streams[stream_index];
1895 sti = ffstream(st);
1896 ast = st->priv_data;
1897
1898 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 timestamp *= FFMAX(ast->sample_size, 1);
1905
1906 index = av_index_search_timestamp(st, timestamp, flags);
1907 if (index < 0) {
1908 if (sti->nb_index_entries > 0)
1909 av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
1910 timestamp,
1911 sti->index_entries[0].timestamp,
1913 return AVERROR_INVALIDDATA;
1914 }
1915
1916 /* find the position */
1917 pos = sti->index_entries[index].pos;
1918 timestamp = sti->index_entries[index].timestamp;
1919
1920 av_log(s, AV_LOG_TRACE, "XX %"PRId64" %d %"PRId64"\n",
1921 timestamp, index, sti->index_entries[index].timestamp);
1922
1923 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. */
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 timestamp /= FFMAX(ast->sample_size, 1);
1941
1942 pos_min = pos;
1943 for (i = 0; i < s->nb_streams; i++) {
1944 AVStream *st2 = s->streams[i];
1945 FFStream *const sti2 = ffstream(st2);
1946 AVIStream *ast2 = st2->priv_data;
1947
1948 ast2->packet_size =
1949 ast2->remaining = 0;
1950
1951 if (ast2->sub_ctx) {
1952 seek_subtitle(st, st2, timestamp);
1953 continue;
1954 }
1955
1956 if (sti2->nb_index_entries <= 0)
1957 continue;
1958
1959// av_assert1(st2->codecpar->block_align);
1961 av_rescale_q(timestamp,
1962 st->time_base,
1963 st2->time_base) *
1964 FFMAX(ast2->sample_size, 1),
1965 flags |
1968 if (index < 0)
1969 index = 0;
1970 ast2->seek_pos = sti2->index_entries[index].pos;
1971 pos_min = FFMIN(pos_min,ast2->seek_pos);
1972 }
1973 for (i = 0; i < s->nb_streams; i++) {
1974 AVStream *st2 = s->streams[i];
1975 FFStream *const sti2 = ffstream(st2);
1976 AVIStream *ast2 = st2->priv_data;
1977
1978 if (ast2->sub_ctx || sti2->nb_index_entries <= 0)
1979 continue;
1980
1982 st2,
1983 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
1985 if (index < 0)
1986 index = 0;
1987 while (!avi->non_interleaved && index > 0 && sti2->index_entries[index-1].pos >= pos_min)
1988 index--;
1990 }
1991
1992 /* do the seek */
1993 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 avi->stream_index = -1;
1998 avi->dts_max = INT_MIN;
1999 return 0;
2000}
2001
2003{
2004 int i;
2005 AVIContext *avi = s->priv_data;
2006
2007 for (i = 0; i < s->nb_streams; i++) {
2008 AVStream *st = s->streams[i];
2009 AVIStream *ast = st->priv_data;
2010 if (ast) {
2011 if (ast->sub_ctx) {
2012 av_freep(&ast->sub_ctx->pb);
2014 }
2016 av_packet_free(&ast->sub_pkt);
2017 }
2018 }
2019
2020 av_freep(&avi->dv_demux);
2021
2022 return 0;
2023}
2024
2025static int avi_probe(const AVProbeData *p)
2026{
2027 int i;
2028
2029 /* check file header */
2030 for (i = 0; avi_headers[i][0]; i++)
2031 if (AV_RL32(p->buf ) == AV_RL32(avi_headers[i] ) &&
2032 AV_RL32(p->buf + 8) == AV_RL32(avi_headers[i] + 4))
2033 return AVPROBE_SCORE_MAX;
2034
2035 return 0;
2036}
2037
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,
2050};
const FFInputFormat ff_avi_demuxer
Definition avidec.c:2038
static const AVClass demuxer_class
Definition apngdec.c:418
static int64_t fsize(FILE *f)
Definition audiomatch.c:29
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define get_duration(index)
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition avformat.c:879
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
void ff_remove_stream(AVFormatContext *s, AVStream *st)
Remove a stream from its AVFormatContext and free it.
Definition avformat.c:122
Main libavformat public API header.
#define AVFMT_FLAG_SORT_DTS
try to interleave outputted packets by dts (using this flag can slow demuxing down)
Definition avformat.h:1502
#define AVINDEX_KEYFRAME
Definition avformat.h:628
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
#define AVPROBE_PADDING_SIZE
extra allocated bytes at the end of the probe buffer
Definition avformat.h:485
#define AVFMT_FLAG_IGNIDX
Ignore index.
Definition avformat.h:1486
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition avformat.h:2618
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
#define AVSEEK_FLAG_BACKWARD
seek backward
Definition avformat.h:2616
@ AVSTREAM_PARSE_TIMESTAMPS
full parsing and interpolation of timestamps for frames not starting on a packet boundary
Definition avformat.h:613
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition avformat.h:612
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition avformat.h:611
@ AVSTREAM_PARSE_NONE
Definition avformat.h:610
#define AVIF_MUSTUSEINDEX
Definition avi.h:25
#define AVIIF_INDEX
Definition avi.h:38
static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag, uint32_t size)
Definition avidec.c:323
static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition avidec.c:1871
static int avi_sync(AVFormatContext *s, int exit_early)
Definition avidec.c:1252
static int avi_extract_stream_metadata(AVFormatContext *s, AVStream *st)
Definition avidec.c:415
static int avi_load_index(AVFormatContext *s)
Definition avidec.c:1814
#define MAX_ODML_DEPTH
Definition avidec.c:91
static int get_riff(AVFormatContext *s, AVIOContext *pb)
Definition avidec.c:150
static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
Definition avidec.c:352
static int avi_read_idx1(AVFormatContext *s, int size)
Definition avidec.c:1625
static int check_stream_max_drift(AVFormatContext *s)
Definition avidec.c:1713
static const AVMetadataConv avi_metadata_conv[]
Definition avidec.c:119
static void avi_read_nikon(AVFormatContext *s, uint64_t end)
Definition avidec.c:371
static int calculate_bitrate(AVFormatContext *s)
Definition avidec.c:459
static int read_odml_index(AVFormatContext *s, int64_t frame_num)
Definition avidec.c:175
static int avi_probe(const AVProbeData *p)
Definition avidec.c:2025
#define print_tag(s, str, tag, size)
Definition avidec.c:136
static const char avi_headers[][8]
Definition avidec.c:110
static int ni_prepare_read(AVFormatContext *s)
Definition avidec.c:1406
static int avi_read_close(AVFormatContext *s)
Definition avidec.c:2002
static int avi_read_header(AVFormatContext *s)
Definition avidec.c:509
static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
Definition avidec.c:1861
static AVStream * get_subtitle_pkt(AVFormatContext *s, AVStream *next_st, AVPacket *pkt)
Definition avidec.c:1204
static int get_stream_idx(const unsigned *d)
Definition avidec.c:1238
static void clean_index(AVFormatContext *s)
Definition avidec.c:294
static int guess_ni_flag(AVFormatContext *s)
Definition avidec.c:1771
static int read_gab2_sub(AVFormatContext *s, AVStream *st, AVPacket *pkt)
Definition avidec.c:1122
static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition avidec.c:1478
static const char months[12][4]
Definition avidec.c:349
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition avio.h:41
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
int avio_get_str16le(AVIOContext *pb, int maxlen, char *buf, int buflen)
Read a UTF-16 string from pb and convert it to UTF-8.
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
AVIOContext * avio_alloc_context(unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, const uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Allocate and initialize an AVIOContext for buffered I/O.
Definition aviobuf.c:109
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
void avio_context_free(AVIOContext **s)
Free the supplied IO context and everything associated with it.
Definition aviobuf.c:126
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
uint64_t avio_rl64(AVIOContext *s)
Definition aviobuf.c:741
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition bytestream.h:168
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
#define av_sat_sub64
Definition common.h:142
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define max(a, b)
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition demux.c:629
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
int ff_get_extradata(void *logctx, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
static AVPacket * pkt
Public dictionary API.
double value
Definition eval.c:102
int av_exif_parse_buffer(void *logctx, const uint8_t *buf, size_t size, AVExifMetadata *ifd, enum AVExifHeaderMode header_mode)
Decodes the EXIF data provided in the buffer and writes it into the struct *ifd.
Definition exif.c:882
int av_exif_ifd_to_dict(void *logctx, const AVExifMetadata *ifd, AVDictionary **metadata)
Recursively reads all tags from the IFD and stores them in the provided metadata dictionary.
Definition exif.c:1053
void av_exif_free(AVExifMetadata *ifd)
Frees all resources associated with the given EXIF metadata struct.
Definition exif.c:659
EXIF metadata parser.
@ AV_EXIF_ASSUME_LE
skip the TIFF header, assume little endian
Definition exif.h:64
static struct @346255127015250356166251341105367306144006377143 state
static int64_t filesize(AVIOContext *pb)
Definition ffmpeg_mux.c:51
const char * key
static int64_t duration
Definition ffplay.c:330
static unsigned int nb_streams
Definition ffprobe.c:352
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition opt.h:355
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_CODEC_ID_XSUB
Definition codec_id.h:569
@ AV_CODEC_ID_FTR
Definition codec_id.h:553
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_ADPCM_IMA_AMV
Definition codec_id.h:389
@ AV_CODEC_ID_MP2
Definition codec_id.h:453
@ AV_CODEC_ID_RV40
Definition codec_id.h:119
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_AAC
Definition codec_id.h:455
@ AV_CODEC_ID_XAN_DPCM
Definition codec_id.h:444
@ AV_CODEC_ID_FLAC
Definition codec_id.h:465
@ AV_CODEC_ID_MPEG4
Definition codec_id.h:62
@ AV_CODEC_ID_MJPEG
Definition codec_id.h:57
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition codec_id.h:454
@ AV_CODEC_ID_MPEG1VIDEO
Definition codec_id.h:51
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition codec_id.h:52
@ AV_CODEC_ID_AMV
Definition codec_id.h:157
@ AV_CODEC_ID_AVRN
Definition codec_id.h:255
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AVDISCARD_ALL
discard all
Definition defs.h:232
@ AVDISCARD_DEFAULT
discard useless packets like 0 size packets in avi
Definition defs.h:227
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition packet.h:47
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition packet.c:231
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:491
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition options.c:165
int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Seek to timestamp ts.
Definition seek.c:664
int avformat_open_input(AVFormatContext **ps, const char *url, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition demux.c:231
const AVInputFormat * av_probe_input_format2(const AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
Definition format.c:238
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition demux.c:377
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition seek.c:122
int av_index_search_timestamp(AVStream *st, int64_t timestamp, int flags)
Get the index for a specific timestamp.
Definition seek.c:245
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition dict.c:60
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition dict.h:79
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:86
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AVERROR_DEMUXER_NOT_FOUND
Demuxer not found.
Definition error.h:55
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition rational.c:88
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition mem.c:188
#define av_fourcc2str(fourcc)
Definition avutil.h:323
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_SUBTITLE
Definition avutil.h:203
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition avutil.h:199
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition avstring.c:208
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition avutil.h:263
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int index
Definition gxfenc.c:90
AVInteger av_div_i(AVInteger a, AVInteger b)
Return a/b.
Definition integer.c:143
int av_cmp_i(AVInteger a, AVInteger b)
Return 0 if a==b, 1 if a>b and -1 if a<b.
Definition integer.c:87
AVInteger av_int2i(int64_t a)
Convert the given int64_t to an AVInteger.
Definition integer.c:149
AVInteger av_shr_i(AVInteger a, int s)
bitwise shift
Definition integer.c:99
AVInteger av_mul_i(AVInteger a, AVInteger b)
Definition integer.c:66
int64_t av_i2int(AVInteger a)
Convert the given AVInteger to an int64_t.
Definition integer.c:160
AVInteger av_add_i(AVInteger a, AVInteger b)
Definition integer.c:36
arbitrary precision integers
#define AV_WL32(p, v)
#define AV_RL32(p)
#define AV_RL16(p)
const AVCodecTag ff_codec_movvideo_tags[]
Definition isom_tags.c:29
unsigned offset
Definition libaomenc.c:763
int avpriv_dv_produce_packet(DVDemuxContext *c, AVPacket *pkt, uint8_t *buf, int buf_size, int64_t pos)
Definition dv.c:738
int avpriv_dv_get_packet(DVDemuxContext *c, AVPacket *pkt)
Definition dv.c:733
DVDemuxContext * avpriv_dv_init_demux(AVFormatContext *s)
Definition dv.c:728
void ff_dv_ts_reset(DVDemuxContext *c, int64_t ts_video)
struct DVDemuxContext DVDemuxContext
Definition dv.h:33
static int resync(AVFormatContext *s)
Definition flvdec.c:1167
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition utils.c:143
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition libcdio.c:151
const char * desc
Definition libsvtav1.c:83
static void handler(vbi_event *ev, void *user_data)
@ AV_CLASS_CATEGORY_DEMUXER
Definition log.h:33
#define FFMIN(a, b)
Definition macros.h:49
#define MKTAG(a, b, c, d)
Definition macros.h:55
#define FFMAX(a, b)
Definition macros.h:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition metadata.c:59
uint32_t tag
Definition movenc.c:2087
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
#define AVPALETTE_SIZE
Definition pixfmt.h:32
const char * name
Definition qsvenc.c:142
const AVCodecTag ff_codec_bmp_tags_unofficial[]
Definition riff.c:519
const AVCodecTag ff_codec_bmp_tags[]
Definition riff.c:36
const AVMetadataConv ff_riff_info_conv[]
Definition riff.c:632
internal header for RIFF based (de)muxers do NOT include this in end user applications
int ff_get_bmp_header(AVIOContext *pb, AVStream *st, uint32_t *size)
Read BITMAPINFOHEADER structure and set AVStream codec width, height and bits_per_encoded_sample fiel...
Definition riffdec.c:294
int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb, AVCodecParameters *par, int size, int big_endian)
Definition riffdec.c:141
int ff_read_riff_info(AVFormatContext *s, int64_t size)
Definition riffdec.c:313
enum AVMediaType codec_type
Definition rtp.c:37
static const uint8_t header[24]
Definition sdr2.c:68
#define snprintf
Definition snprintf.h:34
unsigned int pos
Definition spdifenc.c:431
Accelerated start code search function for start codes common to MPEG-1/2/4 video,...
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
A reference to a data buffer.
Definition buffer.h:82
Describe the class of an AVClass context structure.
Definition log.h:76
int extradata_size
Size of the extradata content in bytes.
Definition codec_par.h:75
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
int block_align
The number of bytes per coded audio frame, required by some formats.
Definition codec_par.h:221
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
char * value
Definition dict.h:92
Format I/O context.
Definition avformat.h:1333
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition avformat.h:1389
AVIOContext * pb
I/O context.
Definition avformat.h:1375
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
int64_t last_pkt_pos
Definition avidec.c:81
int stream_index
Definition avidec.c:85
int64_t odml_max_pos
Definition avidec.c:89
int64_t movi_list
Definition avidec.c:80
int64_t riff_end
Definition avidec.c:76
DVDemuxContext * dv_demux
Definition avidec.c:86
int64_t dts_max
Definition avidec.c:92
int odml_depth
Definition avidec.c:87
int non_interleaved
Definition avidec.c:84
int index_loaded
Definition avidec.c:82
int64_t io_fsize
Definition avidec.c:79
int64_t fsize
Definition avidec.c:78
int64_t movi_end
Definition avidec.c:77
int use_odml
Definition avidec.c:90
int is_odml
Definition avidec.c:83
int64_t odml_read
Definition avidec.c:88
Bytestream IO Context.
Definition avio.h:160
unsigned char * buf_end
End of the data, may be less than buffer+buffer_size if the read function returned less data than req...
Definition avio.h:228
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition avio.h:261
unsigned char * buf_ptr
Current position in the buffer.
Definition avio.h:227
int error
contains the error code or 0 if no error happened
Definition avio.h:239
int has_pal
Definition avidec.c:63
uint32_t handler
Definition avidec.c:53
int remaining
Definition avidec.c:50
int dshow_block_align
Definition avidec.c:64
int64_t frame_offset
Definition avidec.c:48
int sample_size
Definition avidec.c:56
uint32_t rate
Definition avidec.c:55
uint32_t scale
Definition avidec.c:54
uint32_t pal[256]
Definition avidec.c:62
int packet_size
Definition avidec.c:51
AVPacket * sub_pkt
Definition avidec.c:68
int prefix_count
Definition avidec.c:61
AVBufferRef * sub_buffer
Definition avidec.c:69
int64_t seek_pos
Definition avidec.c:71
AVFormatContext * sub_ctx
Definition avidec.c:67
int64_t cum_len
Definition avidec.c:59
int prefix
Definition avidec.c:60
int64_t pos
Definition avformat.h:621
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition avformat.h:622
const char * name
A comma separated list of short names for the format.
Definition avformat.h:570
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition packet.h:602
uint8_t * data
Definition packet.h:603
This structure contains the data a format has to probe a file.
Definition avformat.h:471
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition avformat.h:473
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition avformat.h:844
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition avformat.h:837
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
AVDictionary * metadata
Definition avformat.h:846
void * priv_data
Definition avformat.h:791
int id
Format-specific stream ID.
Definition avformat.h:778
int index
stream index in AVFormatContext
Definition avformat.h:772
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:855
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avformat.h:805
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
Definition internal.h:205
int nb_index_entries
Definition internal.h:193
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition internal.h:191
enum AVStreamParseType need_parsing
Definition internal.h:321
#define av_free(p)
#define av_mallocz(s)
#define avpriv_request_sample(...)
#define av_freep(p)
#define ff_tlog(a,...)
#define av_log(a,...)
static void error(const char *err)
int64_t bitrate
Definition av1_levels.c:47
static char buffer[20]
Definition seek.c:32
int size
int len
uint8_t base
Definition vp3data.h:128