3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "config_components.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/mathematics.h"
40 #include "libavcodec/bytestream.h"
41 #include "libavcodec/exif.h"
42 #include "libavcodec/startcode.h"
44 typedef struct AVIStream {
45 int64_t frame_offset; /* current frame (video) or byte (audio) counter
46 * (used to compute the pts) */
53 int sample_size; /* size of one sample (or packet)
54 * (in the rate/scale sense) in bytes */
56 int64_t cum_len; /* temporary storage (used during seek) */
57 int prefix; /* normally 'd'<<8 + 'c' or 'w'<<8 + 'b' */
61 int dshow_block_align; /* block align variable used to emulate bugs in
62 * the MS dshow demuxer */
64 AVFormatContext *sub_ctx;
66 AVBufferRef *sub_buffer;
71 typedef struct AVIContext {
83 DVDemuxContext *dv_demux;
88 #define MAX_ODML_DEPTH 1000
93 static const AVOption options[] = {
94 { "use_odml", "use odml index", offsetof(AVIContext, use_odml), AV_OPT_TYPE_BOOL, {.i64 = 1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM},
98 static const AVClass demuxer_class = {
100 .item_name = av_default_item_name,
102 .version = LIBAVUTIL_VERSION_INT,
103 .category = AV_CLASS_CATEGORY_DEMUXER,
107 static const char avi_headers[][8] = {
108 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' },
109 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' },
110 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19 },
111 { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' },
112 { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' },
116 static const AVMetadataConv avi_metadata_conv[] = {
118 { "isbj", "subject" },
120 { "iart", "artist" },
121 { "icop", "copyright" },
122 { "icmt", "comment" },
124 { "iprd", "product" },
125 { "isft", "software" },
130 static int avi_load_index(AVFormatContext *s);
131 static int guess_ni_flag(AVFormatContext *s);
133 #define print_tag(s, str, tag, size) \
134 av_log(s, AV_LOG_TRACE, "pos:%"PRIX64" %s: tag=%s size=0x%x\n", \
135 avio_tell(pb), str, av_fourcc2str(tag), size) \
137 static inline int get_duration(AVIStream *ast, int len)
139 if (ast->sample_size)
141 else if (ast->dshow_block_align)
142 return (len + (int64_t)ast->dshow_block_align - 1) / ast->dshow_block_align;
147 static int get_riff(AVFormatContext *s, AVIOContext *pb)
149 AVIContext *avi = s->priv_data;
150 char header[8] = {0};
153 /* check RIFF header */
154 avio_read(pb, header, 4);
155 avi->riff_end = avio_rl32(pb); /* RIFF chunk size */
156 avi->riff_end += avio_tell(pb); /* RIFF chunk end */
157 avio_read(pb, header + 4, 4);
159 for (i = 0; avi_headers[i][0]; i++)
160 if (!memcmp(header, avi_headers[i], 8))
162 if (!avi_headers[i][0])
163 return AVERROR_INVALIDDATA;
165 if (header[7] == 0x19)
166 av_log(s, AV_LOG_INFO,
167 "This file has been generated by a totally broken muxer.\n");
172 static int read_odml_index(AVFormatContext *s, int64_t frame_num)
174 AVIContext *avi = s->priv_data;
175 AVIOContext *pb = s->pb;
176 int longs_per_entry = avio_rl16(pb);
177 int index_sub_type = avio_r8(pb);
178 int index_type = avio_r8(pb);
179 int entries_in_use = avio_rl32(pb);
180 int chunk_id = avio_rl32(pb);
181 int64_t base = avio_rl64(pb);
182 int stream_id = ((chunk_id & 0xFF) - '0') * 10 +
183 ((chunk_id >> 8 & 0xFF) - '0');
187 int64_t last_pos = -1;
188 int64_t filesize = avi->fsize;
190 av_log(s, AV_LOG_TRACE,
191 "longs_per_entry:%d index_type:%d entries_in_use:%d "
192 "chunk_id:%X base:%16"PRIX64" frame_num:%"PRId64"\n",
200 if (stream_id >= s->nb_streams || stream_id < 0)
201 return AVERROR_INVALIDDATA;
202 st = s->streams[stream_id];
205 if (index_sub_type || entries_in_use < 0)
206 return AVERROR_INVALIDDATA;
210 if (index_type && longs_per_entry != 2)
211 return AVERROR_INVALIDDATA;
213 return AVERROR_INVALIDDATA;
215 if (filesize > 0 && base >= filesize) {
216 av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
217 if (base >> 32 == (base & 0xFFFFFFFF) &&
218 (base & 0xFFFFFFFF) < filesize &&
219 filesize <= 0xFFFFFFFF)
222 return AVERROR_INVALIDDATA;
225 for (i = 0; i < entries_in_use; i++) {
226 avi->odml_max_pos = FFMAX(avi->odml_max_pos, avio_tell(pb));
228 // If we read more than there are bytes then we must have been reading something twice
229 if (avi->odml_read > avi->odml_max_pos)
230 return AVERROR_INVALIDDATA;
233 int64_t pos = avio_rl32(pb) + base - 8;
234 int len = avio_rl32(pb);
239 av_log(s, AV_LOG_TRACE, "pos:%"PRId64", len:%X\n", pos, len);
242 return AVERROR_INVALIDDATA;
244 if (last_pos == pos || pos == base - 8)
245 avi->non_interleaved = 1;
246 if (last_pos != pos && len)
247 av_add_index_entry(st, pos, ast->cum_len, len, 0,
248 key ? AVINDEX_KEYFRAME : 0);
250 ast->cum_len += get_duration(ast, len);
256 avi->odml_read += 16;
258 offset = avio_rl64(pb);
259 avio_rl32(pb); /* size */
260 duration = avio_rl32(pb);
262 if (avio_feof(pb) || offset > INT64_MAX - 8)
263 return AVERROR_INVALIDDATA;
267 if (avi->odml_depth > MAX_ODML_DEPTH) {
268 av_log(s, AV_LOG_ERROR, "Too deeply nested ODML indexes\n");
269 return AVERROR_INVALIDDATA;
272 if (avio_seek(pb, offset + 8, SEEK_SET) < 0)
275 ret = read_odml_index(s, frame_num);
277 frame_num += duration;
279 if (avio_seek(pb, pos, SEEK_SET) < 0) {
280 av_log(s, AV_LOG_ERROR, "Failed to restore position after reading index\n");
287 avi->index_loaded = 2;
291 static void clean_index(AVFormatContext *s)
296 for (i = 0; i < s->nb_streams; i++) {
297 AVStream *st = s->streams[i];
298 FFStream *const sti = ffstream(st);
299 AVIStream *ast = st->priv_data;
300 int n = sti->nb_index_entries;
301 int max = ast->sample_size;
302 int64_t pos, size, ts;
304 if (n != 1 || ast->sample_size == 0)
310 pos = sti->index_entries[0].pos;
311 size = sti->index_entries[0].size;
312 ts = sti->index_entries[0].timestamp;
314 for (j = 0; j < size; j += max)
315 av_add_index_entry(st, pos + j, ts + j, FFMIN(max, size - j), 0,
320 static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag,
323 AVIOContext *pb = s->pb;
329 if (size == UINT_MAX)
330 return AVERROR(EINVAL);
331 value = av_malloc(size + 1);
333 return AVERROR(ENOMEM);
334 if (avio_read(pb, value, size) != size) {
336 return AVERROR_INVALIDDATA;
342 return av_dict_set(st ? &st->metadata : &s->metadata, key, value,
343 AV_DICT_DONT_STRDUP_VAL);
346 static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
347 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
349 static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
351 char month[4], time[9], buffer[64];
353 /* parse standard AVI date format (ie. "Mon Mar 10 15:04:43 2003") */
354 if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
355 month, &day, time, &year) == 4) {
356 for (i = 0; i < 12; i++)
357 if (!av_strcasecmp(month, months[i])) {
358 snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s",
359 year, i + 1, day, time);
360 av_dict_set(metadata, "creation_time", buffer, 0);
362 } else if (date[4] == '/' && date[7] == '/') {
363 date[4] = date[7] = '-';
364 av_dict_set(metadata, "creation_time", date, 0);
368 static void avi_read_nikon(AVFormatContext *s, uint64_t end)
370 while (avio_tell(s->pb) < end && !avio_feof(s->pb)) {
371 uint32_t tag = avio_rl32(s->pb);
372 uint32_t size = avio_rl32(s->pb);
374 case MKTAG('n', 'c', 't', 'g'): /* Nikon Tags */
376 uint64_t tag_end = avio_tell(s->pb) + size;
377 while (avio_tell(s->pb) < tag_end && !avio_feof(s->pb)) {
378 uint16_t tag = avio_rl16(s->pb);
379 uint16_t size = avio_rl16(s->pb);
380 const char *name = NULL;
381 char buffer[64] = { 0 };
382 uint64_t remaining = tag_end - avio_tell(s->pb);
383 size = FFMIN(size, remaining);
384 size -= avio_read(s->pb, buffer,
385 FFMIN(size, sizeof(buffer) - 1));
394 name = "creation_time";
395 if (buffer[4] == ':' && buffer[7] == ':')
396 buffer[4] = buffer[7] = '-';
400 av_dict_set(&s->metadata, name, buffer, 0);
401 avio_skip(s->pb, size);
406 avio_skip(s->pb, size);
412 static int avi_extract_stream_metadata(AVFormatContext *s, AVStream *st)
415 uint8_t *data = st->codecpar->extradata;
416 int data_size = st->codecpar->extradata_size;
419 if (!data || data_size < 8) {
420 return AVERROR_INVALIDDATA;
423 bytestream2_init(&gb, data, data_size);
425 tag = bytestream2_get_le32(&gb);
428 case MKTAG('A', 'V', 'I', 'F'):
429 // skip 4 byte padding
430 bytestream2_skip(&gb, 4);
431 offset = bytestream2_tell(&gb);
433 // decode EXIF tags from IFD, AVI is always little-endian
434 return avpriv_exif_decode_ifd(s, data + offset, data_size - offset,
435 1, 0, &st->metadata);
437 case MKTAG('C', 'A', 'S', 'I'):
438 avpriv_request_sample(s, "RIFF stream data tag type CASI (%u)", tag);
440 case MKTAG('Z', 'o', 'r', 'a'):
441 avpriv_request_sample(s, "RIFF stream data tag type Zora (%u)", tag);
450 static int calculate_bitrate(AVFormatContext *s)
452 AVIContext *avi = s->priv_data;
457 for (i = 0; i<s->nb_streams; i++) {
459 FFStream *const sti = ffstream(s->streams[i]);
461 if (!sti->nb_index_entries)
464 for (j = 0; j < sti->nb_index_entries; j++)
465 len += sti->index_entries[j].size;
466 maxpos = FFMAX(maxpos, sti->index_entries[j-1].pos);
469 if (maxpos < av_rescale(avi->io_fsize, 9, 10)) // index does not cover the whole file
471 if (lensum*9/10 > maxpos || lensum < maxpos*9/10) // frame sum and filesize mismatch
474 for (i = 0; i<s->nb_streams; i++) {
476 AVStream *st = s->streams[i];
477 FFStream *const sti = ffstream(st);
481 for (j = 0; j < sti->nb_index_entries; j++)
482 len += sti->index_entries[j].size;
484 if (sti->nb_index_entries < 2 || st->codecpar->bit_rate > 0)
486 duration = sti->index_entries[j-1].timestamp - sti->index_entries[0].timestamp;
487 bitrate = av_rescale(8*len, st->time_base.den, duration * st->time_base.num);
489 st->codecpar->bit_rate = bitrate;
495 static int avi_read_header(AVFormatContext *s)
497 AVIContext *avi = s->priv_data;
498 AVIOContext *pb = s->pb;
499 unsigned int tag, tag1, handler;
500 int codec_type, stream_index, frame_period;
504 AVIStream *ast = NULL;
505 int avih_width = 0, avih_height = 0;
506 int amv_file_format = 0;
507 uint64_t list_end = 0;
510 AVDictionaryEntry *dict_entry;
512 avi->stream_index = -1;
514 ret = get_riff(s, pb);
518 av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
520 avi->io_fsize = avi->fsize = avio_size(pb);
521 if (avi->fsize <= 0 || avi->fsize < avi->riff_end)
522 avi->fsize = avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
530 return AVERROR_INVALIDDATA;
532 size = avio_rl32(pb);
534 print_tag(s, "tag", tag, size);
537 case MKTAG('L', 'I', 'S', 'T'):
538 list_end = avio_tell(pb) + size;
539 /* Ignored, except at start of video packets. */
540 tag1 = avio_rl32(pb);
542 print_tag(s, "list", tag1, 0);
544 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
545 avi->movi_list = avio_tell(pb) - 4;
547 avi->movi_end = avi->movi_list + size + (size & 1);
549 avi->movi_end = avi->fsize;
550 av_log(s, AV_LOG_TRACE, "movi end=%"PRIx64"\n", avi->movi_end);
552 } else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
553 ff_read_riff_info(s, size - 4);
554 else if (tag1 == MKTAG('n', 'c', 'd', 't'))
555 avi_read_nikon(s, list_end);
558 case MKTAG('I', 'D', 'I', 'T'):
560 unsigned char date[64] = { 0 };
562 size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
564 avi_metadata_creation_time(&s->metadata, date);
567 case MKTAG('d', 'm', 'l', 'h'):
569 avio_skip(pb, size + (size & 1));
571 case MKTAG('a', 'm', 'v', 'h'):
573 case MKTAG('a', 'v', 'i', 'h'):
575 /* using frame_period is bad idea */
576 frame_period = avio_rl32(pb);
577 avio_rl32(pb); /* max. bytes per second */
579 avi->non_interleaved |= avio_rl32(pb) & AVIF_MUSTUSEINDEX;
581 avio_skip(pb, 2 * 4);
584 avih_width = avio_rl32(pb);
585 avih_height = avio_rl32(pb);
587 avio_skip(pb, size - 10 * 4);
589 case MKTAG('s', 't', 'r', 'h'):
592 tag1 = avio_rl32(pb);
593 handler = avio_rl32(pb); /* codec tag */
595 if (tag1 == MKTAG('p', 'a', 'd', 's')) {
596 avio_skip(pb, size - 8);
600 st = avformat_new_stream(s, NULL);
602 return AVERROR(ENOMEM);
604 st->id = stream_index;
605 ast = av_mallocz(sizeof(AVIStream));
607 return AVERROR(ENOMEM);
611 tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
612 : MKTAG('v', 'i', 'd', 's');
614 print_tag(s, "strh", tag1, -1);
616 if (tag1 == MKTAG('i', 'a', 'v', 's') ||
617 tag1 == MKTAG('i', 'v', 'a', 's')) {
620 /* After some consideration -- I don't think we
621 * have to support anything but DV in type1 AVIs. */
622 if (s->nb_streams != 1)
623 return AVERROR_INVALIDDATA;
625 if (handler != MKTAG('d', 'v', 's', 'd') &&
626 handler != MKTAG('d', 'v', 'h', 'd') &&
627 handler != MKTAG('d', 'v', 's', 'l'))
628 return AVERROR_INVALIDDATA;
630 if (!CONFIG_DV_DEMUXER)
631 return AVERROR_DEMUXER_NOT_FOUND;
633 ast = s->streams[0]->priv_data;
634 st->priv_data = NULL;
635 ff_remove_stream(s, st);
637 avi->dv_demux = avpriv_dv_init_demux(s);
638 if (!avi->dv_demux) {
640 return AVERROR(ENOMEM);
643 s->streams[0]->priv_data = ast;
644 avio_skip(pb, 3 * 4);
645 ast->scale = avio_rl32(pb);
646 ast->rate = avio_rl32(pb);
647 avio_skip(pb, 4); /* start time */
649 dv_dur = avio_rl32(pb);
650 if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
651 dv_dur *= AV_TIME_BASE;
652 s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
654 /* else, leave duration alone; timing estimation in utils.c
655 * will make a guess based on bitrate. */
657 stream_index = s->nb_streams - 1;
658 avio_skip(pb, size - 9 * 4);
662 av_assert0(stream_index < s->nb_streams);
663 ast->handler = handler;
665 avio_rl32(pb); /* flags */
666 avio_rl16(pb); /* priority */
667 avio_rl16(pb); /* language */
668 avio_rl32(pb); /* initial frame */
669 ast->scale = avio_rl32(pb);
670 ast->rate = avio_rl32(pb);
671 if (!(ast->scale && ast->rate)) {
672 av_log(s, AV_LOG_WARNING,
673 "scale/rate is %"PRIu32"/%"PRIu32" which is invalid. "
674 "(This file has been generated by broken software.)\n",
679 ast->scale = frame_period;
685 avpriv_set_pts_info(st, 64, ast->scale, ast->rate);
687 ast->cum_len = avio_rl32(pb); /* start */
688 st->nb_frames = avio_rl32(pb);
691 avio_rl32(pb); /* buffer size */
692 avio_rl32(pb); /* quality */
693 if (ast->cum_len > 3600LL * ast->rate / ast->scale) {
694 av_log(s, AV_LOG_ERROR, "crazy start time, iam scared, giving up\n");
697 ast->sample_size = avio_rl32(pb);
698 ast->cum_len *= FFMAX(1, ast->sample_size);
699 av_log(s, AV_LOG_TRACE, "%"PRIu32" %"PRIu32" %d\n",
700 ast->rate, ast->scale, ast->sample_size);
703 case MKTAG('v', 'i', 'd', 's'):
704 codec_type = AVMEDIA_TYPE_VIDEO;
706 ast->sample_size = 0;
707 st->avg_frame_rate = av_inv_q(st->time_base);
709 case MKTAG('a', 'u', 'd', 's'):
710 codec_type = AVMEDIA_TYPE_AUDIO;
712 case MKTAG('t', 'x', 't', 's'):
713 codec_type = AVMEDIA_TYPE_SUBTITLE;
715 case MKTAG('d', 'a', 't', 's'):
716 codec_type = AVMEDIA_TYPE_DATA;
719 av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
722 if (ast->sample_size < 0) {
723 if (s->error_recognition & AV_EF_EXPLODE) {
724 av_log(s, AV_LOG_ERROR,
725 "Invalid sample_size %d at stream %d\n",
728 return AVERROR_INVALIDDATA;
730 av_log(s, AV_LOG_WARNING,
731 "Invalid sample_size %d at stream %d "
735 ast->sample_size = 0;
738 if (ast->sample_size == 0) {
739 st->duration = st->nb_frames;
740 if (st->duration > 0 && avi->io_fsize > 0 && avi->riff_end > avi->io_fsize) {
741 av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
742 st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
745 ast->frame_offset = ast->cum_len;
746 avio_skip(pb, size - 12 * 4);
748 case MKTAG('s', 't', 'r', 'f'):
750 if (!size && (codec_type == AVMEDIA_TYPE_AUDIO ||
751 codec_type == AVMEDIA_TYPE_VIDEO))
753 if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
756 uint64_t cur_pos = avio_tell(pb);
759 if (cur_pos < list_end)
760 size = FFMIN(size, list_end - cur_pos);
761 st = s->streams[stream_index];
763 if (st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN) {
767 switch (codec_type) {
768 case AVMEDIA_TYPE_VIDEO:
769 if (amv_file_format) {
770 st->codecpar->width = avih_width;
771 st->codecpar->height = avih_height;
772 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
773 st->codecpar->codec_id = AV_CODEC_ID_AMV;
777 tag1 = ff_get_bmp_header(pb, st, &esize);
779 if (tag1 == MKTAG('D', 'X', 'S', 'B') ||
780 tag1 == MKTAG('D', 'X', 'S', 'A')) {
781 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
782 st->codecpar->codec_tag = tag1;
783 st->codecpar->codec_id = AV_CODEC_ID_XSUB;
787 if (size > 10 * 4 && size < (1 << 30) && size < avi->fsize) {
788 if (esize == size-1 && (esize&1)) {
789 st->codecpar->extradata_size = esize - 10 * 4;
791 st->codecpar->extradata_size = size - 10 * 4;
792 if (st->codecpar->extradata) {
793 av_log(s, AV_LOG_WARNING, "New extradata in strf chunk, freeing previous one.\n");
795 ret = ff_get_extradata(s, st->codecpar, pb,
796 st->codecpar->extradata_size);
801 // FIXME: check if the encoder really did this correctly
802 if (st->codecpar->extradata_size & 1)
805 /* Extract palette from extradata if bpp <= 8.
806 * This code assumes that extradata contains only palette.
807 * This is true for all paletted codecs implemented in
809 if (st->codecpar->extradata_size &&
810 (st->codecpar->bits_per_coded_sample <= 8)) {
811 int pal_size = (1 << st->codecpar->bits_per_coded_sample) << 2;
812 const uint8_t *pal_src;
814 pal_size = FFMIN(pal_size, st->codecpar->extradata_size);
815 pal_src = st->codecpar->extradata +
816 st->codecpar->extradata_size - pal_size;
817 /* Exclude the "BottomUp" field from the palette */
818 if (pal_src - st->codecpar->extradata >= 9 &&
819 !memcmp(st->codecpar->extradata + st->codecpar->extradata_size - 9, "BottomUp", 9))
821 for (i = 0; i < pal_size / 4; i++)
822 ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src + 4 * i);
826 print_tag(s, "video", tag1, 0);
828 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
829 st->codecpar->codec_tag = tag1;
830 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags,
832 /* If codec is not found yet, try with the mov tags. */
833 if (!st->codecpar->codec_id) {
834 st->codecpar->codec_id =
835 ff_codec_get_id(ff_codec_movvideo_tags, tag1);
836 if (st->codecpar->codec_id)
837 av_log(s, AV_LOG_WARNING,
838 "mov tag found in avi (fourcc %s)\n",
839 av_fourcc2str(tag1));
841 if (!st->codecpar->codec_id)
842 st->codecpar->codec_id = ff_codec_get_id(ff_codec_bmp_tags_unofficial, tag1);
844 /* This is needed to get the pict type which is necessary
845 * for generating correct pts. */
846 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
848 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4 &&
849 ast->handler == MKTAG('X', 'V', 'I', 'D'))
850 st->codecpar->codec_tag = MKTAG('X', 'V', 'I', 'D');
852 if (st->codecpar->codec_tag == MKTAG('V', 'S', 'S', 'H'))
853 sti->need_parsing = AVSTREAM_PARSE_FULL;
854 if (st->codecpar->codec_id == AV_CODEC_ID_RV40)
855 sti->need_parsing = AVSTREAM_PARSE_NONE;
856 if (st->codecpar->codec_id == AV_CODEC_ID_HEVC &&
857 st->codecpar->codec_tag == MKTAG('H', '2', '6', '5'))
858 sti->need_parsing = AVSTREAM_PARSE_FULL;
860 if (st->codecpar->codec_id == AV_CODEC_ID_AVRN &&
861 st->codecpar->codec_tag == MKTAG('A', 'V', 'R', 'n') &&
862 (st->codecpar->extradata_size < 31 ||
863 memcmp(&st->codecpar->extradata[28], "1:1", 3)))
864 st->codecpar->codec_id = AV_CODEC_ID_MJPEG;
866 if (st->codecpar->codec_tag == 0 && st->codecpar->height > 0 &&
867 st->codecpar->extradata_size < 1U << 30) {
868 st->codecpar->extradata_size += 9;
869 if ((ret = av_reallocp(&st->codecpar->extradata,
870 st->codecpar->extradata_size +
871 AV_INPUT_BUFFER_PADDING_SIZE)) < 0) {
872 st->codecpar->extradata_size = 0;
875 memcpy(st->codecpar->extradata + st->codecpar->extradata_size - 9,
878 if (st->codecpar->height == INT_MIN)
879 return AVERROR_INVALIDDATA;
880 st->codecpar->height = FFABS(st->codecpar->height);
882 // avio_skip(pb, size - 5 * 4);
884 case AVMEDIA_TYPE_AUDIO:
885 ret = ff_get_wav_header(s, pb, st->codecpar, size, 0);
888 ast->dshow_block_align = st->codecpar->block_align;
889 if (ast->sample_size && st->codecpar->block_align &&
890 ast->sample_size != st->codecpar->block_align) {
893 "sample size (%d) != block align (%d)\n",
895 st->codecpar->block_align);
896 ast->sample_size = st->codecpar->block_align;
899 * (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
902 /* Force parsing as several audio frames can be in
903 * one packet and timestamps refer to packet start. */
904 sti->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
905 /* ADTS header is in extradata, AAC without header must be
906 * stored as exact frames. Parser not needed and it will
908 if (st->codecpar->codec_id == AV_CODEC_ID_AAC &&
909 st->codecpar->extradata_size)
910 sti->need_parsing = AVSTREAM_PARSE_NONE;
911 // The flac parser does not work with AVSTREAM_PARSE_TIMESTAMPS
912 if (st->codecpar->codec_id == AV_CODEC_ID_FLAC)
913 sti->need_parsing = AVSTREAM_PARSE_NONE;
914 /* AVI files with Xan DPCM audio (wrongly) declare PCM
915 * audio in the header but have Axan as stream_code_tag. */
916 if (ast->handler == AV_RL32("Axan")) {
917 st->codecpar->codec_id = AV_CODEC_ID_XAN_DPCM;
918 st->codecpar->codec_tag = 0;
919 ast->dshow_block_align = 0;
921 if (amv_file_format) {
922 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_AMV;
923 ast->dshow_block_align = 0;
925 if ((st->codecpar->codec_id == AV_CODEC_ID_AAC ||
926 st->codecpar->codec_id == AV_CODEC_ID_FTR ||
927 st->codecpar->codec_id == AV_CODEC_ID_FLAC ||
928 st->codecpar->codec_id == AV_CODEC_ID_MP2 ) && ast->dshow_block_align <= 4 && ast->dshow_block_align) {
929 av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
930 ast->dshow_block_align = 0;
932 if (st->codecpar->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
933 st->codecpar->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
934 st->codecpar->codec_id == AV_CODEC_ID_MP3 && ast->dshow_block_align == 1152 && ast->sample_size == 1152) {
935 av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
936 ast->sample_size = 0;
939 case AVMEDIA_TYPE_SUBTITLE:
940 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
941 sti->request_probe = 1;
945 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
946 st->codecpar->codec_id = AV_CODEC_ID_NONE;
947 st->codecpar->codec_tag = 0;
953 case MKTAG('s', 't', 'r', 'd'):
954 if (stream_index >= (unsigned)s->nb_streams
955 || s->streams[stream_index]->codecpar->extradata_size
956 || s->streams[stream_index]->codecpar->codec_tag == MKTAG('H','2','6','4')) {
959 uint64_t cur_pos = avio_tell(pb);
960 if (cur_pos < list_end)
961 size = FFMIN(size, list_end - cur_pos);
962 st = s->streams[stream_index];
965 if (st->codecpar->extradata) {
966 av_log(s, AV_LOG_WARNING, "New extradata in strd chunk, freeing previous one.\n");
968 if ((ret = ff_get_extradata(s, st->codecpar, pb, size)) < 0)
972 if (st->codecpar->extradata_size & 1) //FIXME check if the encoder really did this correctly
975 ret = avi_extract_stream_metadata(s, st);
977 av_log(s, AV_LOG_WARNING, "could not decoding EXIF data in stream header.\n");
981 case MKTAG('i', 'n', 'd', 'x'):
983 if ((pb->seekable & AVIO_SEEKABLE_NORMAL) && !(s->flags & AVFMT_FLAG_IGNIDX) &&
985 read_odml_index(s, 0) < 0 &&
986 (s->error_recognition & AV_EF_EXPLODE))
987 return AVERROR_INVALIDDATA;
988 avio_seek(pb, pos + size, SEEK_SET);
990 case MKTAG('v', 'p', 'r', 'p'):
991 if (stream_index < (unsigned)s->nb_streams && size > 9 * 4) {
992 AVRational active, active_aspect;
994 st = s->streams[stream_index];
1001 active_aspect.den = avio_rl16(pb);
1002 active_aspect.num = avio_rl16(pb);
1003 active.num = avio_rl32(pb);
1004 active.den = avio_rl32(pb);
1005 avio_rl32(pb); // nbFieldsPerFrame
1007 if (active_aspect.num && active_aspect.den &&
1008 active.num && active.den) {
1009 st->sample_aspect_ratio = av_div_q(active_aspect, active);
1010 av_log(s, AV_LOG_TRACE, "vprp %d/%d %d/%d\n",
1011 active_aspect.num, active_aspect.den,
1012 active.num, active.den);
1016 avio_skip(pb, size);
1018 case MKTAG('s', 't', 'r', 'n'):
1019 case MKTAG('i', 's', 'b', 'j'):
1020 case MKTAG('i', 'n', 'a', 'm'):
1021 case MKTAG('i', 'a', 'r', 't'):
1022 case MKTAG('i', 'c', 'o', 'p'):
1023 case MKTAG('i', 'c', 'm', 't'):
1024 case MKTAG('i', 'g', 'n', 'r'):
1025 case MKTAG('i', 'p', 'o', 'd'):
1026 case MKTAG('i', 's', 'o', 'f'):
1027 if (s->nb_streams) {
1028 ret = avi_read_tag(s, s->streams[s->nb_streams - 1], tag, size);
1034 if (size > 1000000) {
1035 av_log(s, AV_LOG_ERROR,
1036 "Something went wrong during header parsing, "
1037 "tag %s has size %u, "
1038 "I will ignore it and try to continue anyway.\n",
1039 av_fourcc2str(tag), size);
1040 if (s->error_recognition & AV_EF_EXPLODE)
1041 return AVERROR_INVALIDDATA;
1042 avi->movi_list = avio_tell(pb) - 4;
1043 avi->movi_end = avi->fsize;
1046 /* Do not fail for very large idx1 tags */
1047 case MKTAG('i', 'd', 'x', '1'):
1050 avio_skip(pb, size);
1056 /* check stream number */
1057 if (stream_index != s->nb_streams - 1)
1058 return AVERROR_INVALIDDATA;
1060 if (!avi->index_loaded && (pb->seekable & AVIO_SEEKABLE_NORMAL))
1062 calculate_bitrate(s);
1063 avi->index_loaded |= 1;
1065 if ((ret = guess_ni_flag(s)) < 0)
1068 avi->non_interleaved |= ret | (s->flags & AVFMT_FLAG_SORT_DTS);
1070 dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
1071 if (dict_entry && !strcmp(dict_entry->value, "PotEncoder"))
1072 for (i = 0; i < s->nb_streams; i++) {
1073 AVStream *st = s->streams[i];
1074 if ( st->codecpar->codec_id == AV_CODEC_ID_MPEG1VIDEO
1075 || st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO)
1076 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
1079 for (i = 0; i < s->nb_streams; i++) {
1080 AVStream *st = s->streams[i];
1081 if (ffstream(st)->nb_index_entries)
1084 // DV-in-AVI cannot be non-interleaved, if set this must be
1087 avi->non_interleaved = 0;
1088 if (i == s->nb_streams && avi->non_interleaved) {
1089 av_log(s, AV_LOG_WARNING,
1090 "Non-interleaved AVI without index, switching to interleaved\n");
1091 avi->non_interleaved = 0;
1094 if (avi->non_interleaved) {
1095 av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
1099 ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
1100 ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
1105 static int read_gab2_sub(AVFormatContext *s, AVStream *st, AVPacket *pkt)
1107 if (pkt->size >= 7 &&
1108 pkt->size < INT_MAX - AVPROBE_PADDING_SIZE &&
1109 !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
1111 int score = AVPROBE_SCORE_EXTENSION, ret;
1112 AVIStream *ast = st->priv_data;
1113 const AVInputFormat *sub_demuxer;
1114 AVRational time_base;
1117 unsigned int desc_len;
1118 AVIOContext *pb = avio_alloc_context(pkt->data + 7,
1120 0, NULL, NULL, NULL, NULL);
1124 desc_len = avio_rl32(pb);
1126 if (desc_len > pb->buf_end - pb->buf_ptr)
1129 ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
1130 avio_skip(pb, desc_len - ret);
1132 av_dict_set(&st->metadata, "title", desc, 0);
1134 avio_rl16(pb); /* flags? */
1135 avio_rl32(pb); /* data size */
1137 size = pb->buf_end - pb->buf_ptr;
1138 pd = (AVProbeData) { .buf = av_mallocz(size + AVPROBE_PADDING_SIZE),
1142 memcpy(pd.buf, pb->buf_ptr, size);
1143 sub_demuxer = av_probe_input_format2(&pd, 1, &score);
1148 if (strcmp(sub_demuxer->name, "srt") && strcmp(sub_demuxer->name, "ass"))
1151 if (!(ast->sub_pkt = av_packet_alloc()))
1154 if (!(ast->sub_ctx = avformat_alloc_context()))
1157 ast->sub_ctx->pb = pb;
1159 if (ff_copy_whiteblacklists(ast->sub_ctx, s) < 0)
1162 if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
1163 if (ast->sub_ctx->nb_streams != 1)
1165 ff_read_packet(ast->sub_ctx, ast->sub_pkt);
1166 avcodec_parameters_copy(st->codecpar, ast->sub_ctx->streams[0]->codecpar);
1167 time_base = ast->sub_ctx->streams[0]->time_base;
1168 avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
1170 ast->sub_buffer = pkt->buf;
1172 av_packet_unref(pkt);
1176 av_packet_free(&ast->sub_pkt);
1177 av_freep(&ast->sub_ctx);
1178 avio_context_free(&pb);
1183 static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
1186 AVIStream *ast, *next_ast = next_st->priv_data;
1187 int64_t ts, next_ts, ts_min = INT64_MAX;
1188 AVStream *st, *sub_st = NULL;
1191 next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
1194 for (i = 0; i < s->nb_streams; i++) {
1196 ast = st->priv_data;
1197 if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt && ast->sub_pkt->data) {
1198 ts = av_rescale_q(ast->sub_pkt->dts, st->time_base, AV_TIME_BASE_Q);
1199 if (ts <= next_ts && ts < ts_min) {
1207 ast = sub_st->priv_data;
1208 av_packet_move_ref(pkt, ast->sub_pkt);
1209 pkt->stream_index = sub_st->index;
1211 if (ff_read_packet(ast->sub_ctx, ast->sub_pkt) < 0)
1212 ast->sub_pkt->data = NULL;
1217 static int get_stream_idx(const unsigned *d)
1219 if (d[0] >= '0' && d[0] <= '9' &&
1220 d[1] >= '0' && d[1] <= '9') {
1221 return (d[0] - '0') * 10 + (d[1] - '0');
1223 return 100; // invalid stream ID
1229 * @param exit_early set to 1 to just gather packet position without making the changes needed to actually read & return the packet
1231 static int avi_sync(AVFormatContext *s, int exit_early)
1233 AVIContext *avi = s->priv_data;
1234 AVIOContext *pb = s->pb;
1241 memset(d, -1, sizeof(d));
1242 for (i = sync = avio_tell(pb); !avio_feof(pb); i++) {
1245 for (j = 0; j < 7; j++)
1249 size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
1251 n = get_stream_idx(d + 2);
1252 ff_tlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
1253 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
1254 if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
1258 if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
1260 (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
1261 (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1') ||
1262 (d[0] == 'i' && d[1] == 'n' && d[2] == 'd' && d[3] == 'x')) {
1263 avio_skip(pb, size);
1268 if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
1273 n = get_stream_idx(d);
1275 if (!((i - avi->last_pkt_pos) & 1) &&
1276 get_stream_idx(d + 1) < s->nb_streams)
1279 // detect ##ix chunk and skip
1280 if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
1281 avio_skip(pb, size);
1285 if (d[2] == 'w' && d[3] == 'c' && n < s->nb_streams) {
1286 avio_skip(pb, 16 * 3 + 8);
1290 if (avi->dv_demux && n != 0)
1294 if (n < s->nb_streams) {
1298 ast = st->priv_data;
1301 av_log(s, AV_LOG_WARNING, "Skipping foreign stream %d packet\n", n);
1305 if (s->nb_streams >= 2) {
1306 AVStream *st1 = s->streams[1];
1307 AVIStream *ast1 = st1->priv_data;
1308 // workaround for broken small-file-bug402.avi
1309 if (ast1 && d[2] == 'w' && d[3] == 'b'
1311 && st ->codecpar->codec_type == AVMEDIA_TYPE_VIDEO
1312 && st1->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
1313 && ast->prefix == 'd'*256+'c'
1314 && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
1319 av_log(s, AV_LOG_WARNING,
1320 "Invalid stream + prefix combination, assuming audio.\n");
1324 if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
1325 int k = avio_r8(pb);
1326 int last = (k + avio_r8(pb) - 1) & 0xFF;
1328 avio_rl16(pb); // flags
1330 // b + (g << 8) + (r << 16);
1331 for (; k <= last; k++)
1332 ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
1336 } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
1337 d[2] < 128 && d[3] < 128) ||
1338 d[2] * 256 + d[3] == ast->prefix /* ||
1339 (d[2] == 'd' && d[3] == 'c') ||
1340 (d[2] == 'w' && d[3] == 'b') */) {
1343 if (d[2] * 256 + d[3] == ast->prefix)
1344 ast->prefix_count++;
1346 ast->prefix = d[2] * 256 + d[3];
1347 ast->prefix_count = 0;
1350 if (!avi->dv_demux &&
1351 ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
1352 // FIXME: needs a little reordering
1353 (st->discard >= AVDISCARD_NONKEY &&
1354 !(pkt->flags & AV_PKT_FLAG_KEY)) */
1355 || st->discard >= AVDISCARD_ALL)) {
1357 ast->frame_offset += get_duration(ast, size);
1358 avio_skip(pb, size);
1362 avi->stream_index = n;
1363 ast->packet_size = size + 8;
1364 ast->remaining = size;
1367 FFStream *const sti = ffstream(st);
1368 uint64_t pos = avio_tell(pb) - 8;
1369 if (!sti->index_entries || !sti->nb_index_entries ||
1370 sti->index_entries[sti->nb_index_entries - 1].pos < pos) {
1371 av_add_index_entry(st, pos, ast->frame_offset, size,
1372 0, AVINDEX_KEYFRAME);
1385 static int ni_prepare_read(AVFormatContext *s)
1387 AVIContext *avi = s->priv_data;
1388 int best_stream_index = 0;
1389 AVStream *best_st = NULL;
1391 AVIStream *best_ast;
1392 int64_t best_ts = INT64_MAX;
1395 for (i = 0; i < s->nb_streams; i++) {
1396 AVStream *st = s->streams[i];
1397 FFStream *const sti = ffstream(st);
1398 AVIStream *ast = st->priv_data;
1399 int64_t ts = ast->frame_offset;
1402 if (!sti->nb_index_entries)
1405 last_ts = sti->index_entries[sti->nb_index_entries - 1].timestamp;
1406 if (!ast->remaining && ts > last_ts)
1409 ts = av_rescale_q(ts, st->time_base,
1410 (AVRational) { FFMAX(1, ast->sample_size),
1413 av_log(s, AV_LOG_TRACE, "%"PRId64" %d/%d %"PRId64"\n", ts,
1414 st->time_base.num, st->time_base.den, ast->frame_offset);
1418 best_stream_index = i;
1424 best_sti = ffstream(best_st);
1425 best_ast = best_st->priv_data;
1426 best_ts = best_ast->frame_offset;
1427 if (best_ast->remaining) {
1428 i = av_index_search_timestamp(best_st,
1431 AVSEEK_FLAG_BACKWARD);
1433 i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
1435 best_ast->frame_offset = best_sti->index_entries[i].timestamp;
1439 int64_t pos = best_sti->index_entries[i].pos;
1440 pos += best_ast->packet_size - best_ast->remaining;
1441 if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
1444 av_assert0(best_ast->remaining <= best_ast->packet_size);
1446 avi->stream_index = best_stream_index;
1447 if (!best_ast->remaining)
1448 best_ast->packet_size =
1449 best_ast->remaining = best_sti->index_entries[i].size;
1457 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
1459 AVIContext *avi = s->priv_data;
1460 AVIOContext *pb = s->pb;
1463 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1464 int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
1471 if (avi->non_interleaved) {
1472 err = ni_prepare_read(s);
1478 if (avi->stream_index >= 0) {
1479 AVStream *st = s->streams[avi->stream_index];
1480 FFStream *const sti = ffstream(st);
1481 AVIStream *ast = st->priv_data;
1482 int dv_demux = CONFIG_DV_DEMUXER && avi->dv_demux;
1485 if (get_subtitle_pkt(s, st, pkt))
1488 // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
1489 if (ast->sample_size <= 1)
1491 else if (ast->sample_size < 32)
1492 // arbitrary multiplier to avoid tiny packets for raw PCM data
1493 size = 1024 * ast->sample_size;
1495 size = ast->sample_size;
1497 if (size > ast->remaining)
1498 size = ast->remaining;
1499 avi->last_pkt_pos = avio_tell(pb);
1500 err = av_get_packet(pb, pkt, size);
1505 if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2 && !dv_demux) {
1507 pal = av_packet_new_side_data(pkt,
1508 AV_PKT_DATA_PALETTE,
1511 av_log(s, AV_LOG_ERROR,
1512 "Failed to allocate data for palette\n");
1514 memcpy(pal, ast->pal, AVPALETTE_SIZE);
1519 if (CONFIG_DV_DEMUXER && dv_demux) {
1520 size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
1521 pkt->data, pkt->size, pkt->pos);
1522 pkt->flags |= AV_PKT_FLAG_KEY;
1524 av_packet_unref(pkt);
1525 } else if (st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE &&
1526 !st->codecpar->codec_tag && read_gab2_sub(s, st, pkt)) {
1527 ast->frame_offset++;
1528 avi->stream_index = -1;
1532 /* XXX: How to handle B-frames in AVI? */
1533 pkt->dts = ast->frame_offset;
1534 // pkt->dts += ast->start;
1535 if (ast->sample_size)
1536 pkt->dts /= ast->sample_size;
1537 pkt->stream_index = avi->stream_index;
1539 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->index_entries) {
1543 index = av_index_search_timestamp(st, ast->frame_offset, AVSEEK_FLAG_ANY);
1544 e = &sti->index_entries[index];
1546 if (index >= 0 && e->timestamp == ast->frame_offset) {
1547 if (index == sti->nb_index_entries-1) {
1550 if (st->codecpar->codec_id == AV_CODEC_ID_MPEG4) {
1551 const uint8_t *ptr = pkt->data, *end = ptr + FFMIN(size, 256);
1553 ptr = avpriv_find_start_code(ptr, end, &state);
1554 if (state == 0x1B6 && ptr < end) {
1555 key = !(*ptr & 0xC0);
1561 e->flags &= ~AVINDEX_KEYFRAME;
1563 if (e->flags & AVINDEX_KEYFRAME)
1564 pkt->flags |= AV_PKT_FLAG_KEY;
1567 pkt->flags |= AV_PKT_FLAG_KEY;
1569 ast->frame_offset += get_duration(ast, pkt->size);
1571 ast->remaining -= err;
1572 if (!ast->remaining) {
1573 avi->stream_index = -1;
1574 ast->packet_size = 0;
1577 if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
1578 av_packet_unref(pkt);
1583 if (!avi->non_interleaved && sti->nb_index_entries > 1 && avi->index_loaded > 1) {
1584 int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
1586 if (avi->dts_max < dts) {
1588 } else if (avi->dts_max - (uint64_t)dts > 2*AV_TIME_BASE) {
1589 avi->non_interleaved= 1;
1590 av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
1597 if ((err = avi_sync(s, 0)) < 0)
1602 /* XXX: We make the implicit supposition that the positions are sorted
1603 * for each stream. */
1604 static int avi_read_idx1(AVFormatContext *s, int size)
1606 AVIContext *avi = s->priv_data;
1607 AVIOContext *pb = s->pb;
1608 int nb_index_entries, i;
1612 unsigned int index, tag, flags, len, first_packet = 1;
1613 int64_t last_pos = -1;
1614 unsigned last_idx = -1;
1615 int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1618 nb_index_entries = size / 16;
1619 if (nb_index_entries <= 0)
1620 return AVERROR_INVALIDDATA;
1622 idx1_pos = avio_tell(pb);
1623 avio_seek(pb, avi->movi_list + 4, SEEK_SET);
1624 if (avi_sync(s, 1) == 0)
1625 first_packet_pos = avio_tell(pb) - 8;
1626 avi->stream_index = -1;
1627 avio_seek(pb, idx1_pos, SEEK_SET);
1629 if (s->nb_streams == 1 && s->streams[0]->codecpar->codec_tag == AV_RL32("MMES")) {
1630 first_packet_pos = 0;
1631 data_offset = avi->movi_list;
1634 /* Read the entries and sort them in each stream component. */
1635 for (i = 0; i < nb_index_entries; i++) {
1639 tag = avio_rl32(pb);
1640 flags = avio_rl32(pb);
1641 pos = avio_rl32(pb);
1642 len = avio_rl32(pb);
1643 av_log(s, AV_LOG_TRACE, "%d: tag=0x%x flags=0x%x pos=0x%"PRIx64" len=%d/",
1644 i, tag, flags, pos, len);
1646 index = ((tag & 0xff) - '0') * 10;
1647 index += (tag >> 8 & 0xff) - '0';
1648 if (index >= s->nb_streams)
1650 st = s->streams[index];
1651 ast = st->priv_data;
1653 /* Skip 'xxpc' palette change entries in the index until a logic
1654 * to process these is properly implemented. */
1655 if ((tag >> 16 & 0xff) == 'p' && (tag >> 24 & 0xff) == 'c')
1658 if (first_packet && first_packet_pos) {
1659 if (avi->movi_list + 4 != pos || pos + 500 > first_packet_pos)
1660 data_offset = first_packet_pos - pos;
1665 av_log(s, AV_LOG_TRACE, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
1667 // even if we have only a single stream, we should
1668 // switch to non-interleaved to get correct timestamps
1669 if (last_pos == pos)
1670 avi->non_interleaved = 1;
1671 if (last_idx != pos && len) {
1672 av_add_index_entry(st, pos, ast->cum_len, len, 0,
1673 (flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
1676 ast->cum_len += get_duration(ast, len);
1678 anykey |= flags&AVIIF_INDEX;
1681 for (index = 0; index < s->nb_streams; index++) {
1682 FFStream *const sti = ffstream(s->streams[index]);
1683 if (sti->nb_index_entries)
1684 sti->index_entries[0].flags |= AVINDEX_KEYFRAME;
1690 /* Scan the index and consider any file with streams more than
1691 * 2 seconds or 64MB apart non-interleaved. */
1692 static int check_stream_max_drift(AVFormatContext *s)
1694 int64_t min_pos, pos;
1696 int *idx = av_calloc(s->nb_streams, sizeof(*idx));
1698 return AVERROR(ENOMEM);
1699 for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) {
1700 int64_t max_dts = INT64_MIN / 2;
1701 int64_t min_dts = INT64_MAX / 2;
1702 int64_t max_buffer = 0;
1704 min_pos = INT64_MAX;
1706 for (i = 0; i < s->nb_streams; i++) {
1707 AVStream *st = s->streams[i];
1708 AVIStream *ast = st->priv_data;
1709 FFStream *const sti = ffstream(st);
1710 int n = sti->nb_index_entries;
1711 while (idx[i] < n && sti->index_entries[idx[i]].pos < pos)
1715 dts = av_rescale_q(sti->index_entries[idx[i]].timestamp /
1716 FFMAX(ast->sample_size, 1),
1717 st->time_base, AV_TIME_BASE_Q);
1718 min_dts = FFMIN(min_dts, dts);
1719 min_pos = FFMIN(min_pos, sti->index_entries[idx[i]].pos);
1722 for (i = 0; i < s->nb_streams; i++) {
1723 AVStream *st = s->streams[i];
1724 FFStream *const sti = ffstream(st);
1725 AVIStream *ast = st->priv_data;
1727 if (idx[i] && min_dts != INT64_MAX / 2) {
1728 int64_t dts, delta_dts;
1729 dts = av_rescale_q(sti->index_entries[idx[i] - 1].timestamp /
1730 FFMAX(ast->sample_size, 1),
1731 st->time_base, AV_TIME_BASE_Q);
1732 delta_dts = av_sat_sub64(dts, min_dts);
1733 max_dts = FFMAX(max_dts, dts);
1734 max_buffer = FFMAX(max_buffer,
1735 av_rescale(delta_dts,
1736 st->codecpar->bit_rate,
1740 if (av_sat_sub64(max_dts, min_dts) > 2 * AV_TIME_BASE ||
1741 max_buffer > 1024 * 1024 * 8 * 8) {
1750 static int guess_ni_flag(AVFormatContext *s)
1753 int64_t last_start = 0;
1754 int64_t first_end = INT64_MAX;
1755 int64_t oldpos = avio_tell(s->pb);
1757 for (i = 0; i < s->nb_streams; i++) {
1758 AVStream *st = s->streams[i];
1759 FFStream *const sti = ffstream(st);
1760 int n = sti->nb_index_entries;
1767 int64_t pos = sti->index_entries[0].pos;
1769 avio_seek(s->pb, pos, SEEK_SET);
1770 tag[0] = avio_r8(s->pb);
1771 tag[1] = avio_r8(s->pb);
1773 size = avio_rl32(s->pb);
1774 if (get_stream_idx(tag) == i && pos + size > sti->index_entries[1].pos)
1775 last_start = INT64_MAX;
1776 if (get_stream_idx(tag) == i && size == sti->index_entries[0].size + 8)
1777 last_start = INT64_MAX;
1780 if (sti->index_entries[0].pos > last_start)
1781 last_start = sti->index_entries[0].pos;
1782 if (sti->index_entries[n - 1].pos < first_end)
1783 first_end = sti->index_entries[n - 1].pos;
1785 avio_seek(s->pb, oldpos, SEEK_SET);
1787 if (last_start > first_end)
1790 return check_stream_max_drift(s);
1793 static int avi_load_index(AVFormatContext *s)
1795 AVIContext *avi = s->priv_data;
1796 AVIOContext *pb = s->pb;
1798 int64_t pos = avio_tell(pb);
1802 if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
1803 goto the_end; // maybe truncated file
1804 av_log(s, AV_LOG_TRACE, "movi_end=0x%"PRIx64"\n", avi->movi_end);
1806 tag = avio_rl32(pb);
1807 size = avio_rl32(pb);
1810 next = avio_tell(pb);
1811 if (next < 0 || next > INT64_MAX - size - (size & 1))
1813 next += size + (size & 1LL);
1815 if (tag == MKTAG('i', 'd', 'x', '1') &&
1816 avi_read_idx1(s, size) >= 0) {
1817 avi->index_loaded=2;
1819 }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
1820 uint32_t tag1 = avio_rl32(pb);
1822 if (tag1 == MKTAG('I', 'N', 'F', 'O'))
1823 ff_read_riff_info(s, size - 4);
1827 if (avio_seek(pb, next, SEEK_SET) < 0)
1828 break; // something is wrong here
1832 avio_seek(pb, pos, SEEK_SET);
1836 static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
1838 AVIStream *ast2 = st2->priv_data;
1839 int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
1840 av_packet_unref(ast2->sub_pkt);
1841 if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
1842 avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
1843 ff_read_packet(ast2->sub_ctx, ast2->sub_pkt);
1846 static int avi_read_seek(AVFormatContext *s, int stream_index,
1847 int64_t timestamp, int flags)
1849 AVIContext *avi = s->priv_data;
1853 int64_t pos, pos_min;
1856 /* Does not matter which stream is requested dv in avi has the
1857 * stream information in the first video stream.
1862 if (!avi->index_loaded) {
1863 /* we only load the index on demand */
1865 avi->index_loaded |= 1;
1867 av_assert0(stream_index >= 0);
1869 st = s->streams[stream_index];
1871 ast = st->priv_data;
1873 if (avi->dv_demux) {
1874 // index entries are in the AVI scale/rate timebase, which does
1875 // not match DV demuxer's stream timebase
1876 timestamp = av_rescale_q(timestamp, st->time_base,
1877 (AVRational){ ast->scale, ast->rate });
1879 timestamp *= FFMAX(ast->sample_size, 1);
1881 index = av_index_search_timestamp(st, timestamp, flags);
1883 if (sti->nb_index_entries > 0)
1884 av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
1886 sti->index_entries[0].timestamp,
1887 sti->index_entries[sti->nb_index_entries - 1].timestamp);
1888 return AVERROR_INVALIDDATA;
1891 /* find the position */
1892 pos = sti->index_entries[index].pos;
1893 timestamp = sti->index_entries[index].timestamp;
1895 av_log(s, AV_LOG_TRACE, "XX %"PRId64" %d %"PRId64"\n",
1896 timestamp, index, sti->index_entries[index].timestamp);
1898 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1899 /* One and only one real stream for DV in AVI, and it has video */
1900 /* offsets. Calling with other stream indexes should have failed */
1901 /* the av_index_search_timestamp call above. */
1903 if (avio_seek(s->pb, pos, SEEK_SET) < 0)
1906 /* Feed the DV video stream version of the timestamp to the */
1907 /* DV demux so it can synthesize correct timestamps. */
1908 ff_dv_ts_reset(avi->dv_demux,
1909 av_rescale_q(timestamp, (AVRational){ ast->scale, ast->rate },
1912 avi->stream_index = -1;
1915 timestamp /= FFMAX(ast->sample_size, 1);
1918 for (i = 0; i < s->nb_streams; i++) {
1919 AVStream *st2 = s->streams[i];
1920 FFStream *const sti2 = ffstream(st2);
1921 AVIStream *ast2 = st2->priv_data;
1924 ast2->remaining = 0;
1926 if (ast2->sub_ctx) {
1927 seek_subtitle(st, st2, timestamp);
1931 if (sti2->nb_index_entries <= 0)
1934 // av_assert1(st2->codecpar->block_align);
1935 index = av_index_search_timestamp(st2,
1936 av_rescale_q(timestamp,
1939 FFMAX(ast2->sample_size, 1),
1941 AVSEEK_FLAG_BACKWARD |
1942 (st2->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
1945 ast2->seek_pos = sti2->index_entries[index].pos;
1946 pos_min = FFMIN(pos_min,ast2->seek_pos);
1948 for (i = 0; i < s->nb_streams; i++) {
1949 AVStream *st2 = s->streams[i];
1950 FFStream *const sti2 = ffstream(st2);
1951 AVIStream *ast2 = st2->priv_data;
1953 if (ast2->sub_ctx || sti2->nb_index_entries <= 0)
1956 index = av_index_search_timestamp(
1958 av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
1959 flags | AVSEEK_FLAG_BACKWARD | (st2->codecpar->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
1962 while (!avi->non_interleaved && index > 0 && sti2->index_entries[index-1].pos >= pos_min)
1964 ast2->frame_offset = sti2->index_entries[index].timestamp;
1968 if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
1969 av_log(s, AV_LOG_ERROR, "Seek failed\n");
1972 avi->stream_index = -1;
1973 avi->dts_max = INT_MIN;
1977 static int avi_read_close(AVFormatContext *s)
1980 AVIContext *avi = s->priv_data;
1982 for (i = 0; i < s->nb_streams; i++) {
1983 AVStream *st = s->streams[i];
1984 AVIStream *ast = st->priv_data;
1987 av_freep(&ast->sub_ctx->pb);
1988 avformat_close_input(&ast->sub_ctx);
1990 av_buffer_unref(&ast->sub_buffer);
1991 av_packet_free(&ast->sub_pkt);
1995 av_freep(&avi->dv_demux);
2000 static int avi_probe(const AVProbeData *p)
2004 /* check file header */
2005 for (i = 0; avi_headers[i][0]; i++)
2006 if (AV_RL32(p->buf ) == AV_RL32(avi_headers[i] ) &&
2007 AV_RL32(p->buf + 8) == AV_RL32(avi_headers[i] + 4))
2008 return AVPROBE_SCORE_MAX;
2013 const AVInputFormat ff_avi_demuxer = {
2015 .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
2016 .priv_data_size = sizeof(AVIContext),
2017 .flags_internal = FF_FMT_INIT_CLEANUP,
2018 .extensions = "avi",
2019 .read_probe = avi_probe,
2020 .read_header = avi_read_header,
2021 .read_packet = avi_read_packet,
2022 .read_close = avi_read_close,
2023 .read_seek = avi_read_seek,
2024 .priv_class = &demuxer_class,