82 #define MAX_ODML_DEPTH 1000
102 {
'R',
'I',
'F',
'F',
'A',
'V',
'I',
' ' },
103 {
'R',
'I',
'F',
'F',
'A',
'V',
'I',
'X' },
104 {
'R',
'I',
'F',
'F',
'A',
'V',
'I', 0x19 },
105 {
'O',
'N',
'2',
' ',
'O',
'N',
'2',
'f' },
106 {
'R',
'I',
'F',
'F',
'A',
'M',
'V',
' ' },
118 #define print_tag(str, tag, size) \
119 av_dlog(NULL, "pos:%"PRIX64" %s: tag=%c%c%c%c size=0x%x\n", \
120 avio_tell(pb), str, tag & 0xff, \
122 (tag >> 16) & 0xff, \
123 (tag >> 24) & 0xff, \
154 if (header[7] == 0x19)
156 "This file has been generated by a totally broken muxer.\n");
166 int index_sub_type =
avio_r8(pb);
171 int stream_id = ((chunk_id & 0xFF) -
'0') * 10 +
172 ((chunk_id >> 8 & 0xFF) -
'0');
176 int64_t last_pos = -1;
177 int64_t filesize = avi->
fsize;
180 "longs_pre_entry:%d index_type:%d entries_in_use:%d "
181 "chunk_id:%X base:%16"PRIX64
"\n",
188 if (stream_id >= s->
nb_streams || stream_id < 0)
198 if (index_type && longs_pre_entry != 2)
203 if (filesize > 0 && base >= filesize) {
205 if (base >> 32 == (base & 0xFFFFFFFF) &&
206 (base & 0xFFFFFFFF) < filesize &&
207 filesize <= 0xFFFFFFFF)
213 for (i = 0; i < entries_in_use; i++) {
226 if (last_pos == pos || pos == base - 8)
228 if (last_pos != pos && len)
251 if (
avio_seek(pb, offset + 8, SEEK_SET) < 0)
279 int64_t pos,
size, ts;
291 for (j = 0; j <
size; j += max)
306 if (size == UINT_MAX)
321 static const char months[12][4] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
322 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
326 char month[4], time[9],
buffer[64];
329 if (sscanf(date,
"%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
330 month, &day, time, &year) == 4) {
331 for (i = 0; i < 12; i++)
333 snprintf(buffer,
sizeof(buffer),
"%.4d-%.2d-%.2d %s",
334 year, i + 1, day, time);
337 }
else if (date[4] ==
'/' && date[7] ==
'/') {
338 date[4] = date[7] =
'-';
349 case MKTAG(
'n',
'c',
't',
'g'):
359 FFMIN(size,
sizeof(buffer) - 1));
368 name =
"creation_time";
369 if (buffer[4] ==
':' && buffer[7] ==
':')
370 buffer[4] = buffer[7] =
'-';
393 if (!data || data_size < 8) {
399 tag = bytestream2_get_le32(&gb);
402 case MKTAG(
'A',
'V',
'I',
'F'):
411 case MKTAG(
'C',
'A',
'S',
'I'):
414 case MKTAG(
'Z',
'o',
'r',
'a'):
443 if (maxpos < avi->io_fsize*9/10)
445 if (lensum*9/10 > maxpos || lensum < maxpos*9/10)
474 int avih_width = 0, avih_height = 0;
475 int amv_file_format = 0;
476 uint64_t list_end = 0;
505 case MKTAG(
'L',
'I',
'S',
'T'):
512 if (tag1 ==
MKTAG(
'm',
'o',
'v',
'i')) {
520 }
else if (tag1 ==
MKTAG(
'I',
'N',
'F',
'O'))
522 else if (tag1 ==
MKTAG(
'n',
'c',
'd',
't'))
526 case MKTAG(
'I',
'D',
'I',
'T'):
528 unsigned char date[64] = { 0 };
535 case MKTAG(
'd',
'm',
'l',
'h'):
539 case MKTAG(
'a',
'm',
'v',
'h'):
541 case MKTAG(
'a',
'v',
'i',
'h'):
557 case MKTAG(
's',
't',
'r',
'h'):
563 if (tag1 ==
MKTAG(
'p',
'a',
'd',
's')) {
572 st->
id = stream_index;
579 tag1 = stream_index ?
MKTAG(
'a',
'u',
'd',
's')
580 :
MKTAG(
'v',
'i',
'd',
's');
584 if (tag1 ==
MKTAG(
'i',
'a',
'v',
's') ||
585 tag1 ==
MKTAG(
'i',
'v',
'a',
's')) {
593 if (handler !=
MKTAG(
'd',
'v',
's',
'd') &&
594 handler !=
MKTAG(
'd',
'v',
'h',
'd') &&
595 handler !=
MKTAG(
'd',
'v',
's',
'l'))
606 if (CONFIG_DV_DEMUXER) {
619 if (ast->
scale > 0 && ast->
rate > 0 && dv_dur > 0) {
642 "scale/rate is %"PRIu32
"/%"PRIu32
" which is invalid. "
643 "(This file has been generated by broken software.)\n",
648 ast->
scale = frame_period;
668 av_dlog(s,
"%"PRIu32
" %"PRIu32
" %d\n",
672 case MKTAG(
'v',
'i',
'd',
's'):
678 case MKTAG(
'a',
'u',
'd',
's'):
681 case MKTAG(
't',
'x',
't',
's'):
684 case MKTAG(
'd',
'a',
't',
's'):
700 case MKTAG(
's',
't',
'r',
'f'):
709 if (cur_pos < list_end)
710 size =
FFMIN(size, list_end - cur_pos);
716 switch (codec_type) {
718 if (amv_file_format) {
728 if (tag1 ==
MKTAG(
'D',
'X',
'S',
'B') ||
729 tag1 ==
MKTAG(
'D',
'X',
'S',
'A')) {
736 if (size > 10 * 4 && size < (1 << 30) &&
size < avi->fsize) {
737 if (esize == size-1 && (esize&1)) {
765 for (i = 0; i < pal_size / 4; i++)
766 ast->
pal[i] = 0xFFU<<24 |
AV_RL32(pal_src+4*i);
781 ast->
handler == MKTAG(
'X',
'V',
'I',
'D'))
812 "sample size (%d) != block align (%d)\n",
837 if (amv_file_format) {
866 case MKTAG(
's',
't',
'r',
'd'):
873 if (cur_pos < list_end)
874 size =
FFMIN(size, list_end - cur_pos);
891 case MKTAG(
'i',
'n',
'd',
'x'):
900 case MKTAG(
'v',
'p',
'r',
'p'):
901 if (stream_index < (
unsigned)s->
nb_streams && size > 9 * 4) {
917 if (active_aspect.
num && active_aspect.
den &&
918 active.
num && active.
den) {
920 av_dlog(s,
"vprp %d/%d %d/%d\n",
921 active_aspect.
num, active_aspect.
den,
928 case MKTAG(
's',
't',
'r',
'n'):
936 if (size > 1000000) {
938 "Something went wrong during header parsing, "
939 "I will ignore it and try to continue anyway.\n");
972 if (dict_entry && !strcmp(dict_entry->
value,
"PotEncoder"))
991 "Non-interleaved AVI without index, switching to interleaved\n");
1008 if (pkt->
size >= 7 &&
1061 memset(pkt, 0,
sizeof(*pkt));
1075 int64_t ts, next_ts, ts_min = INT64_MAX;
1087 if (ts <= next_ts && ts < ts_min) {
1107 if (d[0] >=
'0' && d[0] <=
'9' &&
1108 d[1] >=
'0' && d[1] <=
'9') {
1109 return (d[0] -
'0') * 10 + (d[1] -
'0');
1129 memset(d, -1,
sizeof(d));
1133 for (j = 0; j < 7; j++)
1137 size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
1140 av_dlog(s,
"%X %X %X %X %X %X %X %X %"PRId64
" %u %d\n",
1141 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
1142 if (i*(avi->
io_fsize>0) + (uint64_t)size > avi->
fsize || d[0] > 127)
1146 if ((d[0] ==
'i' && d[1] ==
'x' && n < s->
nb_streams) ||
1148 (d[0] ==
'J' && d[1] ==
'U' && d[2] ==
'N' && d[3] ==
'K') ||
1149 (d[0] ==
'i' && d[1] ==
'd' && d[2] ==
'x' && d[3] ==
'1')) {
1155 if (d[0] ==
'L' && d[1] ==
'I' && d[2] ==
'S' && d[3] ==
'T') {
1167 if (d[2] ==
'i' && d[3] ==
'x' && n < s->nb_streams) {
1176 if (n < s->nb_streams) {
1191 if ( d[2] ==
'w' && d[3] ==
'b'
1195 && ast->
prefix ==
'd'*256+
'c'
1202 "Invalid stream + prefix combination, assuming audio.\n");
1219 if (d[2] ==
'p' && d[3] ==
'c' && size <= 4 * 256 + 4) {
1221 int last = (k +
avio_r8(pb) - 1) & 0xFF;
1226 for (; k <= last; k++)
1232 d[2] < 128 && d[3] < 128) ||
1233 d[2] * 256 + d[3] == ast->
prefix
1238 if (d[2] * 256 + d[3] == ast->
prefix)
1241 ast->
prefix = d[2] * 256 + d[3];
1272 #if FF_API_DESTRUCT_PACKET
1276 if (CONFIG_DV_DEMUXER && avi->
dv_demux) {
1285 int best_stream_index = 0;
1288 int64_t best_ts = INT64_MAX;
1308 av_dlog(s,
"%"PRId64
" %d/%d %"PRId64
"\n", ts,
1309 st->time_base.num, st->time_base.den, ast->frame_offset);
1313 best_stream_index = i;
1319 best_ast = best_st->priv_data;
1320 best_ts = best_ast->frame_offset;
1321 if (best_ast->remaining) {
1329 best_ast->frame_offset = best_st->index_entries[i].timestamp;
1333 int64_t pos = best_st->index_entries[i].pos;
1334 pos += best_ast->packet_size - best_ast->remaining;
1338 av_assert0(best_ast->remaining <= best_ast->packet_size);
1341 if (!best_ast->remaining)
1342 best_ast->packet_size =
1343 best_ast->remaining = best_st->index_entries[i].size;
1350 if (avi->stream_index >= 0) {
1351 AVStream *st =
s->streams[avi->stream_index];
1382 "Failed to allocate data for palette\n");
1389 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1391 #if FF_API_DESTRUCT_PACKET
1398 #if FF_API_DESTRUCT_PACKET
1410 avi->stream_index = -1;
1420 "dts:%"PRId64
" offset:%"PRId64
" %d/%d smpl_siz:%d "
1421 "base:%d st:%d size:%d\n",
1444 for (i=0; i<
FFMIN(size,256); i++) {
1446 if (state == 0x1B6) {
1452 state= (state<<8) +
pkt->
data[i];
1467 avi->stream_index = -1;
1477 if (!avi->non_interleaved && st->
nb_index_entries>1 && avi->index_loaded>1) {
1481 avi->non_interleaved= 1;
1483 }
else if (avi->dts_max < dts)
1501 int nb_index_entries, i;
1505 unsigned last_pos = -1;
1506 unsigned last_idx = -1;
1507 int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
1510 nb_index_entries = size / 16;
1511 if (nb_index_entries <= 0)
1522 first_packet_pos = 0;
1527 for (i = 0; i < nb_index_entries; i++) {
1535 av_dlog(s,
"%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
1536 i, tag, flags, pos, len);
1538 index = ((tag & 0xff) -
'0') * 10;
1539 index += (tag >> 8 & 0xff) -
'0';
1545 if (first_packet && first_packet_pos) {
1546 data_offset = first_packet_pos - pos;
1555 if (last_pos == pos)
1557 if (last_idx != pos && len) {
1567 for (index = 0; index < s->
nb_streams; index++) {
1580 int64_t min_pos, pos;
1585 for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) {
1586 int64_t max_dts = INT64_MIN / 2;
1587 int64_t min_dts = INT64_MAX / 2;
1588 int64_t max_buffer = 0;
1590 min_pos = INT64_MAX;
1603 min_dts =
FFMIN(min_dts, dts);
1611 if (idx[i] && min_dts != INT64_MAX / 2) {
1616 max_dts =
FFMAX(max_dts, dts);
1617 max_buffer =
FFMAX(max_buffer,
1624 max_buffer > 1024 * 1024 * 8 * 8) {
1636 int64_t last_start = 0;
1637 int64_t first_end = INT64_MAX;
1653 last_start = INT64_MAX;
1663 if (last_start > first_end)
1686 next =
avio_tell(pb) + size + (size & 1);
1688 av_dlog(s,
"tag=%c%c%c%c size=0x%x\n",
1695 if (tag ==
MKTAG(
'i',
'd',
'x',
'1') &&
1699 }
else if (tag ==
MKTAG(
'L',
'I',
'S',
'T')) {
1702 if (tag1 ==
MKTAG(
'I',
'N',
'F',
'O'))
1727 int64_t timestamp,
int flags)
1732 int64_t pos, pos_min;
1748 st = s->
streams[stream_index];
1755 av_log(s,
AV_LOG_DEBUG,
"Failed to find timestamp %"PRId64
" in index %"PRId64
" .. %"PRId64
"\n",
1766 av_dlog(s,
"XX %"PRId64
" %d %"PRId64
"\n",
1769 if (CONFIG_DV_DEMUXER && avi->
dv_demux) {
1884 .extensions =
"avi",
1890 .priv_class = &demuxer_class,