81 #define MAX_ODML_DEPTH 1000
101 {
'R',
'I',
'F',
'F',
'A',
'V',
'I',
' ' },
102 {
'R',
'I',
'F',
'F',
'A',
'V',
'I',
'X' },
103 {
'R',
'I',
'F',
'F',
'A',
'V',
'I', 0x19 },
104 {
'O',
'N',
'2',
' ',
'O',
'N',
'2',
'f' },
105 {
'R',
'I',
'F',
'F',
'A',
'M',
'V',
' ' },
117 #define print_tag(str, tag, size) \
118 av_dlog(NULL, "pos:%"PRIX64" %s: tag=%c%c%c%c size=0x%x\n", \
119 avio_tell(pb), str, tag & 0xff, \
121 (tag >> 16) & 0xff, \
122 (tag >> 24) & 0xff, \
153 if (header[7] == 0x19)
155 "This file has been generated by a totally broken muxer.\n");
165 int index_sub_type =
avio_r8(pb);
170 int stream_id = ((chunk_id & 0xFF) -
'0') * 10 +
171 ((chunk_id >> 8 & 0xFF) -
'0');
175 int64_t last_pos = -1;
176 int64_t filesize = avi->
fsize;
179 "longs_pre_entry:%d index_type:%d entries_in_use:%d "
180 "chunk_id:%X base:%16"PRIX64
"\n",
187 if (stream_id >= s->
nb_streams || stream_id < 0)
197 if (index_type && longs_pre_entry != 2)
202 if (filesize > 0 && base >= filesize) {
204 if (base >> 32 == (base & 0xFFFFFFFF) &&
205 (base & 0xFFFFFFFF) < filesize &&
206 filesize <= 0xFFFFFFFF)
212 for (i = 0; i < entries_in_use; i++) {
225 if (last_pos == pos || pos == base - 8)
227 if (last_pos != pos && len)
250 if (
avio_seek(pb, offset + 8, SEEK_SET) < 0)
278 int64_t pos,
size, ts;
290 for (j = 0; j <
size; j += max)
305 if (size == UINT_MAX)
319 static const char months[12][4] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
320 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
324 char month[4], time[9],
buffer[64];
327 if (sscanf(date,
"%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
328 month, &day, time, &year) == 4) {
329 for (i = 0; i < 12; i++)
331 snprintf(buffer,
sizeof(buffer),
"%.4d-%.2d-%.2d %s",
332 year, i + 1, day, time);
335 }
else if (date[4] ==
'/' && date[7] ==
'/') {
336 date[4] = date[7] =
'-';
347 case MKTAG(
'n',
'c',
't',
'g'):
353 const char *
name = NULL;
357 FFMIN(size,
sizeof(buffer) - 1));
366 name =
"creation_time";
367 if (buffer[4] ==
':' && buffer[7] ==
':')
368 buffer[4] = buffer[7] =
'-';
391 if (!data || data_size < 8) {
397 tag = bytestream2_get_le32(&gb);
400 case MKTAG(
'A',
'V',
'I',
'F'):
409 case MKTAG(
'C',
'A',
'S',
'I'):
412 case MKTAG(
'Z',
'o',
'r',
'a'):
441 if (maxpos < avi->io_fsize*9/10)
443 if (lensum*9/10 > maxpos || lensum < maxpos*9/10)
472 int avih_width = 0, avih_height = 0;
473 int amv_file_format = 0;
474 uint64_t list_end = 0;
503 case MKTAG(
'L',
'I',
'S',
'T'):
510 if (tag1 ==
MKTAG(
'm',
'o',
'v',
'i')) {
518 }
else if (tag1 ==
MKTAG(
'I',
'N',
'F',
'O'))
520 else if (tag1 ==
MKTAG(
'n',
'c',
'd',
't'))
524 case MKTAG(
'I',
'D',
'I',
'T'):
526 unsigned char date[64] = { 0 };
533 case MKTAG(
'd',
'm',
'l',
'h'):
537 case MKTAG(
'a',
'm',
'v',
'h'):
539 case MKTAG(
'a',
'v',
'i',
'h'):
555 case MKTAG(
's',
't',
'r',
'h'):
561 if (tag1 ==
MKTAG(
'p',
'a',
'd',
's')) {
570 st->
id = stream_index;
577 tag1 = stream_index ?
MKTAG(
'a',
'u',
'd',
's')
578 :
MKTAG(
'v',
'i',
'd',
's');
582 if (tag1 ==
MKTAG(
'i',
'a',
'v',
's') ||
583 tag1 ==
MKTAG(
'i',
'v',
'a',
's')) {
591 if (handler !=
MKTAG(
'd',
'v',
's',
'd') &&
592 handler !=
MKTAG(
'd',
'v',
'h',
'd') &&
593 handler !=
MKTAG(
'd',
'v',
's',
'l'))
604 if (CONFIG_DV_DEMUXER) {
617 if (ast->
scale > 0 && ast->
rate > 0 && dv_dur > 0) {
640 "scale/rate is %"PRIu32
"/%"PRIu32
" which is invalid. "
641 "(This file has been generated by broken software.)\n",
646 ast->
scale = frame_period;
666 av_dlog(s,
"%"PRIu32
" %"PRIu32
" %d\n",
670 case MKTAG(
'v',
'i',
'd',
's'):
676 case MKTAG(
'a',
'u',
'd',
's'):
679 case MKTAG(
't',
'x',
't',
's'):
682 case MKTAG(
'd',
'a',
't',
's'):
698 case MKTAG(
's',
't',
'r',
'f'):
707 if (cur_pos < list_end)
708 size =
FFMIN(size, list_end - cur_pos);
714 switch (codec_type) {
716 if (amv_file_format) {
726 if (tag1 ==
MKTAG(
'D',
'X',
'S',
'B') ||
727 tag1 ==
MKTAG(
'D',
'X',
'S',
'A')) {
734 if (size > 10 * 4 && size < (1 << 30) &&
size < avi->fsize) {
735 if (esize == size-1 && (esize&1)) {
759 for (i = 0; i < pal_size / 4; i++)
760 ast->
pal[i] = 0xFFU<<24 |
AV_RL32(pal_src+4*i);
801 "sample size (%d) != block align (%d)\n",
826 if (amv_file_format) {
855 case MKTAG(
's',
't',
'r',
'd'):
862 if (cur_pos < list_end)
863 size =
FFMIN(size, list_end - cur_pos);
880 case MKTAG(
'i',
'n',
'd',
'x'):
889 case MKTAG(
'v',
'p',
'r',
'p'):
890 if (stream_index < (
unsigned)s->
nb_streams && size > 9 * 4) {
906 if (active_aspect.
num && active_aspect.
den &&
907 active.
num && active.
den) {
909 av_dlog(s,
"vprp %d/%d %d/%d\n",
910 active_aspect.
num, active_aspect.
den,
917 case MKTAG(
's',
't',
'r',
'n'):
925 if (size > 1000000) {
927 "Something went wrong during header parsing, "
928 "I will ignore it and try to continue anyway.\n");
961 if (dict_entry && !strcmp(dict_entry->
value,
"PotEncoder"))
980 "Non-interleaved AVI without index, switching to interleaved\n");
997 if (pkt->
size >= 7 &&
1008 0, NULL, NULL, NULL, NULL);
1046 memset(pkt, 0,
sizeof(*pkt));
1059 int64_t ts, next_ts, ts_min = INT64_MAX;
1071 if (ts <= next_ts && ts < ts_min) {
1091 if (d[0] >=
'0' && d[0] <=
'9' &&
1092 d[1] >=
'0' && d[1] <=
'9') {
1093 return (d[0] -
'0') * 10 + (d[1] -
'0');
1113 memset(d, -1,
sizeof(d));
1117 for (j = 0; j < 7; j++)
1121 size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
1124 av_dlog(s,
"%X %X %X %X %X %X %X %X %"PRId64
" %u %d\n",
1125 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
1126 if (i*(avi->
io_fsize>0) + (uint64_t)size > avi->
fsize || d[0] > 127)
1130 if ((d[0] ==
'i' && d[1] ==
'x' && n < s->
nb_streams) ||
1132 (d[0] ==
'J' && d[1] ==
'U' && d[2] ==
'N' && d[3] ==
'K') ||
1133 (d[0] ==
'i' && d[1] ==
'd' && d[2] ==
'x' && d[3] ==
'1')) {
1139 if (d[0] ==
'L' && d[1] ==
'I' && d[2] ==
'S' && d[3] ==
'T') {
1151 if (d[2] ==
'i' && d[3] ==
'x' && n < s->nb_streams) {
1157 if (n < s->nb_streams) {
1172 if ( d[2] ==
'w' && d[3] ==
'b'
1176 && ast->
prefix ==
'd'*256+
'c'
1183 "Invalid stream + prefix combination, assuming audio.\n");
1200 if (d[2] ==
'p' && d[3] ==
'c' && size <= 4 * 256 + 4) {
1202 int last = (k +
avio_r8(pb) - 1) & 0xFF;
1207 for (; k <= last; k++)
1213 d[2] < 128 && d[3] < 128) ||
1214 d[2] * 256 + d[3] == ast->
prefix
1219 if (d[2] * 256 + d[3] == ast->
prefix)
1222 ast->
prefix = d[2] * 256 + d[3];
1253 #if FF_API_DESTRUCT_PACKET
1257 if (CONFIG_DV_DEMUXER && avi->
dv_demux) {
1266 int best_stream_index = 0;
1269 int64_t best_ts = INT64_MAX;
1289 av_dlog(s,
"%"PRId64
" %d/%d %"PRId64
"\n", ts,
1290 st->time_base.num, st->time_base.den, ast->frame_offset);
1294 best_stream_index = i;
1300 best_ast = best_st->priv_data;
1301 best_ts = best_ast->frame_offset;
1302 if (best_ast->remaining) {
1310 best_ast->frame_offset = best_st->index_entries[i].timestamp;
1314 int64_t pos = best_st->index_entries[i].pos;
1315 pos += best_ast->packet_size - best_ast->remaining;
1319 av_assert0(best_ast->remaining <= best_ast->packet_size);
1322 if (!best_ast->remaining)
1323 best_ast->packet_size =
1324 best_ast->remaining = best_st->index_entries[i].size;
1331 if (avi->stream_index >= 0) {
1332 AVStream *st =
s->streams[avi->stream_index];
1363 "Failed to allocate data for palette\n");
1370 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1372 #if FF_API_DESTRUCT_PACKET
1379 #if FF_API_DESTRUCT_PACKET
1391 avi->stream_index = -1;
1401 "dts:%"PRId64
" offset:%"PRId64
" %d/%d smpl_siz:%d "
1402 "base:%d st:%d size:%d\n",
1425 for (i=0; i<
FFMIN(size,256); i++) {
1427 if (state == 0x1B6) {
1433 state= (state<<8) +
pkt->
data[i];
1448 avi->stream_index = -1;
1458 if (!avi->non_interleaved && st->
nb_index_entries>1 && avi->index_loaded>1) {
1462 avi->non_interleaved= 1;
1464 }
else if (avi->dts_max < dts)
1482 int nb_index_entries, i;
1486 unsigned last_pos = -1;
1487 unsigned last_idx = -1;
1488 int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1491 nb_index_entries = size / 16;
1492 if (nb_index_entries <= 0)
1503 first_packet_pos = 0;
1508 for (i = 0; i < nb_index_entries; i++) {
1516 av_dlog(s,
"%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
1517 i, tag, flags, pos, len);
1519 index = ((tag & 0xff) -
'0') * 10;
1520 index += (tag >> 8 & 0xff) -
'0';
1526 if (first_packet && first_packet_pos) {
1527 data_offset = first_packet_pos - pos;
1536 if (last_pos == pos)
1538 if (last_idx != pos && len) {
1548 for (index = 0; index < s->
nb_streams; index++) {
1561 int64_t min_pos, pos;
1566 for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) {
1567 int64_t max_dts = INT64_MIN / 2;
1568 int64_t min_dts = INT64_MAX / 2;
1569 int64_t max_buffer = 0;
1571 min_pos = INT64_MAX;
1584 min_dts =
FFMIN(min_dts, dts);
1592 if (idx[i] && min_dts != INT64_MAX / 2) {
1597 max_dts =
FFMAX(max_dts, dts);
1598 max_buffer =
FFMAX(max_buffer,
1605 max_buffer > 1024 * 1024 * 8 * 8) {
1617 int64_t last_start = 0;
1618 int64_t first_end = INT64_MAX;
1634 last_start = INT64_MAX;
1644 if (last_start > first_end)
1667 next =
avio_tell(pb) + size + (size & 1);
1669 av_dlog(s,
"tag=%c%c%c%c size=0x%x\n",
1676 if (tag ==
MKTAG(
'i',
'd',
'x',
'1') &&
1680 }
else if (tag ==
MKTAG(
'L',
'I',
'S',
'T')) {
1683 if (tag1 ==
MKTAG(
'I',
'N',
'F',
'O'))
1708 int64_t timestamp,
int flags)
1713 int64_t pos, pos_min;
1729 st = s->
streams[stream_index];
1736 av_log(s,
AV_LOG_DEBUG,
"Failed to find timestamp %"PRId64
" in index %"PRId64
" .. %"PRId64
"\n",
1747 av_dlog(s,
"XX %"PRId64
" %d %"PRId64
"\n",
1750 if (CONFIG_DV_DEMUXER && avi->
dv_demux) {
1865 .extensions =
"avi",
1871 .priv_class = &demuxer_class,