You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1873 lines
63KB

  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. #include <inttypes.h>
  22. #include "libavutil/avassert.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/bswap.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/internal.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/mathematics.h"
  30. #include "avformat.h"
  31. #include "avi.h"
  32. #include "dv.h"
  33. #include "internal.h"
  34. #include "riff.h"
  35. #include "libavcodec/bytestream.h"
  36. #include "libavcodec/exif.h"
  37. typedef struct AVIStream {
  38. int64_t frame_offset; /* current frame (video) or byte (audio) counter
  39. * (used to compute the pts) */
  40. int remaining;
  41. int packet_size;
  42. uint32_t scale;
  43. uint32_t rate;
  44. int sample_size; /* size of one sample (or packet)
  45. * (in the rate/scale sense) in bytes */
  46. int64_t cum_len; /* temporary storage (used during seek) */
  47. int prefix; /* normally 'd'<<8 + 'c' or 'w'<<8 + 'b' */
  48. int prefix_count;
  49. uint32_t pal[256];
  50. int has_pal;
  51. int dshow_block_align; /* block align variable used to emulate bugs in
  52. * the MS dshow demuxer */
  53. AVFormatContext *sub_ctx;
  54. AVPacket sub_pkt;
  55. uint8_t *sub_buffer;
  56. int64_t seek_pos;
  57. } AVIStream;
  58. typedef struct {
  59. const AVClass *class;
  60. int64_t riff_end;
  61. int64_t movi_end;
  62. int64_t fsize;
  63. int64_t io_fsize;
  64. int64_t movi_list;
  65. int64_t last_pkt_pos;
  66. int index_loaded;
  67. int is_odml;
  68. int non_interleaved;
  69. int stream_index;
  70. DVDemuxContext *dv_demux;
  71. int odml_depth;
  72. int use_odml;
  73. #define MAX_ODML_DEPTH 1000
  74. int64_t dts_max;
  75. } AVIContext;
  76. static const AVOption options[] = {
  77. { "use_odml", "use odml index", offsetof(AVIContext, use_odml), AV_OPT_TYPE_INT, {.i64 = 1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM},
  78. { NULL },
  79. };
  80. static const AVClass demuxer_class = {
  81. .class_name = "avi",
  82. .item_name = av_default_item_name,
  83. .option = options,
  84. .version = LIBAVUTIL_VERSION_INT,
  85. .category = AV_CLASS_CATEGORY_DEMUXER,
  86. };
  87. static const char avi_headers[][8] = {
  88. { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' },
  89. { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' },
  90. { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19 },
  91. { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' },
  92. { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' },
  93. { 0 }
  94. };
  95. static const AVMetadataConv avi_metadata_conv[] = {
  96. { "strn", "title" },
  97. { 0 },
  98. };
  99. static int avi_load_index(AVFormatContext *s);
  100. static int guess_ni_flag(AVFormatContext *s);
  101. #define print_tag(str, tag, size) \
  102. av_dlog(NULL, "pos:%"PRIX64" %s: tag=%c%c%c%c size=0x%x\n", \
  103. avio_tell(pb), str, tag & 0xff, \
  104. (tag >> 8) & 0xff, \
  105. (tag >> 16) & 0xff, \
  106. (tag >> 24) & 0xff, \
  107. size)
  108. static inline int get_duration(AVIStream *ast, int len)
  109. {
  110. if (ast->sample_size)
  111. return len;
  112. else if (ast->dshow_block_align)
  113. return (len + ast->dshow_block_align - 1) / ast->dshow_block_align;
  114. else
  115. return 1;
  116. }
  117. static int get_riff(AVFormatContext *s, AVIOContext *pb)
  118. {
  119. AVIContext *avi = s->priv_data;
  120. char header[8];
  121. int i;
  122. /* check RIFF header */
  123. avio_read(pb, header, 4);
  124. avi->riff_end = avio_rl32(pb); /* RIFF chunk size */
  125. avi->riff_end += avio_tell(pb); /* RIFF chunk end */
  126. avio_read(pb, header + 4, 4);
  127. for (i = 0; avi_headers[i][0]; i++)
  128. if (!memcmp(header, avi_headers[i], 8))
  129. break;
  130. if (!avi_headers[i][0])
  131. return AVERROR_INVALIDDATA;
  132. if (header[7] == 0x19)
  133. av_log(s, AV_LOG_INFO,
  134. "This file has been generated by a totally broken muxer.\n");
  135. return 0;
  136. }
  137. static int read_braindead_odml_indx(AVFormatContext *s, int frame_num)
  138. {
  139. AVIContext *avi = s->priv_data;
  140. AVIOContext *pb = s->pb;
  141. int longs_pre_entry = avio_rl16(pb);
  142. int index_sub_type = avio_r8(pb);
  143. int index_type = avio_r8(pb);
  144. int entries_in_use = avio_rl32(pb);
  145. int chunk_id = avio_rl32(pb);
  146. int64_t base = avio_rl64(pb);
  147. int stream_id = ((chunk_id & 0xFF) - '0') * 10 +
  148. ((chunk_id >> 8 & 0xFF) - '0');
  149. AVStream *st;
  150. AVIStream *ast;
  151. int i;
  152. int64_t last_pos = -1;
  153. int64_t filesize = avi->fsize;
  154. av_dlog(s,
  155. "longs_pre_entry:%d index_type:%d entries_in_use:%d "
  156. "chunk_id:%X base:%16"PRIX64"\n",
  157. longs_pre_entry,
  158. index_type,
  159. entries_in_use,
  160. chunk_id,
  161. base);
  162. if (stream_id >= s->nb_streams || stream_id < 0)
  163. return AVERROR_INVALIDDATA;
  164. st = s->streams[stream_id];
  165. ast = st->priv_data;
  166. if (index_sub_type)
  167. return AVERROR_INVALIDDATA;
  168. avio_rl32(pb);
  169. if (index_type && longs_pre_entry != 2)
  170. return AVERROR_INVALIDDATA;
  171. if (index_type > 1)
  172. return AVERROR_INVALIDDATA;
  173. if (filesize > 0 && base >= filesize) {
  174. av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
  175. if (base >> 32 == (base & 0xFFFFFFFF) &&
  176. (base & 0xFFFFFFFF) < filesize &&
  177. filesize <= 0xFFFFFFFF)
  178. base &= 0xFFFFFFFF;
  179. else
  180. return AVERROR_INVALIDDATA;
  181. }
  182. for (i = 0; i < entries_in_use; i++) {
  183. if (index_type) {
  184. int64_t pos = avio_rl32(pb) + base - 8;
  185. int len = avio_rl32(pb);
  186. int key = len >= 0;
  187. len &= 0x7FFFFFFF;
  188. #ifdef DEBUG_SEEK
  189. av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
  190. #endif
  191. if (url_feof(pb))
  192. return AVERROR_INVALIDDATA;
  193. if (last_pos == pos || pos == base - 8)
  194. avi->non_interleaved = 1;
  195. if (last_pos != pos && len)
  196. av_add_index_entry(st, pos, ast->cum_len, len, 0,
  197. key ? AVINDEX_KEYFRAME : 0);
  198. ast->cum_len += get_duration(ast, len);
  199. last_pos = pos;
  200. } else {
  201. int64_t offset, pos;
  202. int duration;
  203. offset = avio_rl64(pb);
  204. avio_rl32(pb); /* size */
  205. duration = avio_rl32(pb);
  206. if (url_feof(pb))
  207. return AVERROR_INVALIDDATA;
  208. pos = avio_tell(pb);
  209. if (avi->odml_depth > MAX_ODML_DEPTH) {
  210. av_log(s, AV_LOG_ERROR, "Too deeply nested ODML indexes\n");
  211. return AVERROR_INVALIDDATA;
  212. }
  213. if (avio_seek(pb, offset + 8, SEEK_SET) < 0)
  214. return -1;
  215. avi->odml_depth++;
  216. read_braindead_odml_indx(s, frame_num);
  217. avi->odml_depth--;
  218. frame_num += duration;
  219. if (avio_seek(pb, pos, SEEK_SET) < 0) {
  220. av_log(s, AV_LOG_ERROR, "Failed to restore position after reading index\n");
  221. return -1;
  222. }
  223. }
  224. }
  225. avi->index_loaded = 2;
  226. return 0;
  227. }
  228. static void clean_index(AVFormatContext *s)
  229. {
  230. int i;
  231. int64_t j;
  232. for (i = 0; i < s->nb_streams; i++) {
  233. AVStream *st = s->streams[i];
  234. AVIStream *ast = st->priv_data;
  235. int n = st->nb_index_entries;
  236. int max = ast->sample_size;
  237. int64_t pos, size, ts;
  238. if (n != 1 || ast->sample_size == 0)
  239. continue;
  240. while (max < 1024)
  241. max += max;
  242. pos = st->index_entries[0].pos;
  243. size = st->index_entries[0].size;
  244. ts = st->index_entries[0].timestamp;
  245. for (j = 0; j < size; j += max)
  246. av_add_index_entry(st, pos + j, ts + j, FFMIN(max, size - j), 0,
  247. AVINDEX_KEYFRAME);
  248. }
  249. }
  250. static int avi_read_tag(AVFormatContext *s, AVStream *st, uint32_t tag,
  251. uint32_t size)
  252. {
  253. AVIOContext *pb = s->pb;
  254. char key[5] = { 0 };
  255. char *value;
  256. size += (size & 1);
  257. if (size == UINT_MAX)
  258. return AVERROR(EINVAL);
  259. value = av_malloc(size + 1);
  260. if (!value)
  261. return AVERROR(ENOMEM);
  262. avio_read(pb, value, size);
  263. value[size] = 0;
  264. AV_WL32(key, tag);
  265. return av_dict_set(st ? &st->metadata : &s->metadata, key, value,
  266. AV_DICT_DONT_STRDUP_VAL);
  267. }
  268. static const char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  269. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  270. static void avi_metadata_creation_time(AVDictionary **metadata, char *date)
  271. {
  272. char month[4], time[9], buffer[64];
  273. int i, day, year;
  274. /* parse standard AVI date format (ie. "Mon Mar 10 15:04:43 2003") */
  275. if (sscanf(date, "%*3s%*[ ]%3s%*[ ]%2d%*[ ]%8s%*[ ]%4d",
  276. month, &day, time, &year) == 4) {
  277. for (i = 0; i < 12; i++)
  278. if (!av_strcasecmp(month, months[i])) {
  279. snprintf(buffer, sizeof(buffer), "%.4d-%.2d-%.2d %s",
  280. year, i + 1, day, time);
  281. av_dict_set(metadata, "creation_time", buffer, 0);
  282. }
  283. } else if (date[4] == '/' && date[7] == '/') {
  284. date[4] = date[7] = '-';
  285. av_dict_set(metadata, "creation_time", date, 0);
  286. }
  287. }
  288. static void avi_read_nikon(AVFormatContext *s, uint64_t end)
  289. {
  290. while (avio_tell(s->pb) < end) {
  291. uint32_t tag = avio_rl32(s->pb);
  292. uint32_t size = avio_rl32(s->pb);
  293. switch (tag) {
  294. case MKTAG('n', 'c', 't', 'g'): /* Nikon Tags */
  295. {
  296. uint64_t tag_end = avio_tell(s->pb) + size;
  297. while (avio_tell(s->pb) < tag_end) {
  298. uint16_t tag = avio_rl16(s->pb);
  299. uint16_t size = avio_rl16(s->pb);
  300. const char *name = NULL;
  301. char buffer[64] = { 0 };
  302. size = FFMIN(size, tag_end - avio_tell(s->pb));
  303. size -= avio_read(s->pb, buffer,
  304. FFMIN(size, sizeof(buffer) - 1));
  305. switch (tag) {
  306. case 0x03:
  307. name = "maker";
  308. break;
  309. case 0x04:
  310. name = "model";
  311. break;
  312. case 0x13:
  313. name = "creation_time";
  314. if (buffer[4] == ':' && buffer[7] == ':')
  315. buffer[4] = buffer[7] = '-';
  316. break;
  317. }
  318. if (name)
  319. av_dict_set(&s->metadata, name, buffer, 0);
  320. avio_skip(s->pb, size);
  321. }
  322. break;
  323. }
  324. default:
  325. avio_skip(s->pb, size);
  326. break;
  327. }
  328. }
  329. }
  330. static int avi_extract_stream_metadata(AVStream *st)
  331. {
  332. GetByteContext gb;
  333. uint8_t *data = st->codec->extradata;
  334. int data_size = st->codec->extradata_size;
  335. int tag, offset;
  336. if (!data || data_size < 8) {
  337. return AVERROR_INVALIDDATA;
  338. }
  339. bytestream2_init(&gb, data, data_size);
  340. tag = bytestream2_get_le32(&gb);
  341. switch (tag) {
  342. case MKTAG('A', 'V', 'I', 'F'):
  343. // skip 4 byte padding
  344. bytestream2_skip(&gb, 4);
  345. offset = bytestream2_tell(&gb);
  346. bytestream2_init(&gb, data + offset, data_size - offset);
  347. // decode EXIF tags from IFD, AVI is always little-endian
  348. return avpriv_exif_decode_ifd(st->codec, &gb, 1, 0, &st->metadata);
  349. break;
  350. case MKTAG('C', 'A', 'S', 'I'):
  351. avpriv_request_sample(st->codec, "RIFF stream data tag type CASI (%u)", tag);
  352. break;
  353. case MKTAG('Z', 'o', 'r', 'a'):
  354. avpriv_request_sample(st->codec, "RIFF stream data tag type Zora (%u)", tag);
  355. break;
  356. default:
  357. break;
  358. }
  359. return 0;
  360. }
  361. static int calculate_bitrate(AVFormatContext *s)
  362. {
  363. AVIContext *avi = s->priv_data;
  364. int i, j;
  365. int64_t lensum = 0;
  366. int64_t maxpos = 0;
  367. for (i = 0; i<s->nb_streams; i++) {
  368. int64_t len = 0;
  369. AVStream *st = s->streams[i];
  370. if (!st->nb_index_entries)
  371. continue;
  372. for (j = 0; j < st->nb_index_entries; j++)
  373. len += st->index_entries[j].size;
  374. maxpos = FFMAX(maxpos, st->index_entries[j-1].pos);
  375. lensum += len;
  376. }
  377. if (maxpos < avi->io_fsize*9/10) // index does not cover the whole file
  378. return 0;
  379. if (lensum*9/10 > maxpos || lensum < maxpos*9/10) // frame sum and filesize mismatch
  380. return 0;
  381. for (i = 0; i<s->nb_streams; i++) {
  382. int64_t len = 0;
  383. AVStream *st = s->streams[i];
  384. int64_t duration;
  385. for (j = 0; j < st->nb_index_entries; j++)
  386. len += st->index_entries[j].size;
  387. if (st->nb_index_entries < 2 || st->codec->bit_rate > 0)
  388. continue;
  389. duration = st->index_entries[j-1].timestamp - st->index_entries[0].timestamp;
  390. st->codec->bit_rate = av_rescale(8*len, st->time_base.den, duration * st->time_base.num);
  391. }
  392. return 1;
  393. }
  394. static int avi_read_header(AVFormatContext *s)
  395. {
  396. AVIContext *avi = s->priv_data;
  397. AVIOContext *pb = s->pb;
  398. unsigned int tag, tag1, handler;
  399. int codec_type, stream_index, frame_period;
  400. unsigned int size;
  401. int i;
  402. AVStream *st;
  403. AVIStream *ast = NULL;
  404. int avih_width = 0, avih_height = 0;
  405. int amv_file_format = 0;
  406. uint64_t list_end = 0;
  407. int ret;
  408. AVDictionaryEntry *dict_entry;
  409. avi->stream_index = -1;
  410. ret = get_riff(s, pb);
  411. if (ret < 0)
  412. return ret;
  413. av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
  414. avi->io_fsize = avi->fsize = avio_size(pb);
  415. if (avi->fsize <= 0 || avi->fsize < avi->riff_end)
  416. avi->fsize = avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
  417. /* first list tag */
  418. stream_index = -1;
  419. codec_type = -1;
  420. frame_period = 0;
  421. for (;;) {
  422. if (url_feof(pb))
  423. goto fail;
  424. tag = avio_rl32(pb);
  425. size = avio_rl32(pb);
  426. print_tag("tag", tag, size);
  427. switch (tag) {
  428. case MKTAG('L', 'I', 'S', 'T'):
  429. list_end = avio_tell(pb) + size;
  430. /* Ignored, except at start of video packets. */
  431. tag1 = avio_rl32(pb);
  432. print_tag("list", tag1, 0);
  433. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  434. avi->movi_list = avio_tell(pb) - 4;
  435. if (size)
  436. avi->movi_end = avi->movi_list + size + (size & 1);
  437. else
  438. avi->movi_end = avi->fsize;
  439. av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end);
  440. goto end_of_header;
  441. } else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
  442. ff_read_riff_info(s, size - 4);
  443. else if (tag1 == MKTAG('n', 'c', 'd', 't'))
  444. avi_read_nikon(s, list_end);
  445. break;
  446. case MKTAG('I', 'D', 'I', 'T'):
  447. {
  448. unsigned char date[64] = { 0 };
  449. size += (size & 1);
  450. size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
  451. avio_skip(pb, size);
  452. avi_metadata_creation_time(&s->metadata, date);
  453. break;
  454. }
  455. case MKTAG('d', 'm', 'l', 'h'):
  456. avi->is_odml = 1;
  457. avio_skip(pb, size + (size & 1));
  458. break;
  459. case MKTAG('a', 'm', 'v', 'h'):
  460. amv_file_format = 1;
  461. case MKTAG('a', 'v', 'i', 'h'):
  462. /* AVI header */
  463. /* using frame_period is bad idea */
  464. frame_period = avio_rl32(pb);
  465. avio_rl32(pb); /* max. bytes per second */
  466. avio_rl32(pb);
  467. avi->non_interleaved |= avio_rl32(pb) & AVIF_MUSTUSEINDEX;
  468. avio_skip(pb, 2 * 4);
  469. avio_rl32(pb);
  470. avio_rl32(pb);
  471. avih_width = avio_rl32(pb);
  472. avih_height = avio_rl32(pb);
  473. avio_skip(pb, size - 10 * 4);
  474. break;
  475. case MKTAG('s', 't', 'r', 'h'):
  476. /* stream header */
  477. tag1 = avio_rl32(pb);
  478. handler = avio_rl32(pb); /* codec tag */
  479. if (tag1 == MKTAG('p', 'a', 'd', 's')) {
  480. avio_skip(pb, size - 8);
  481. break;
  482. } else {
  483. stream_index++;
  484. st = avformat_new_stream(s, NULL);
  485. if (!st)
  486. goto fail;
  487. st->id = stream_index;
  488. ast = av_mallocz(sizeof(AVIStream));
  489. if (!ast)
  490. goto fail;
  491. st->priv_data = ast;
  492. }
  493. if (amv_file_format)
  494. tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
  495. : MKTAG('v', 'i', 'd', 's');
  496. print_tag("strh", tag1, -1);
  497. if (tag1 == MKTAG('i', 'a', 'v', 's') ||
  498. tag1 == MKTAG('i', 'v', 'a', 's')) {
  499. int64_t dv_dur;
  500. /* After some consideration -- I don't think we
  501. * have to support anything but DV in type1 AVIs. */
  502. if (s->nb_streams != 1)
  503. goto fail;
  504. if (handler != MKTAG('d', 'v', 's', 'd') &&
  505. handler != MKTAG('d', 'v', 'h', 'd') &&
  506. handler != MKTAG('d', 'v', 's', 'l'))
  507. goto fail;
  508. ast = s->streams[0]->priv_data;
  509. av_freep(&s->streams[0]->codec->extradata);
  510. av_freep(&s->streams[0]->codec);
  511. if (s->streams[0]->info)
  512. av_freep(&s->streams[0]->info->duration_error);
  513. av_freep(&s->streams[0]->info);
  514. av_freep(&s->streams[0]);
  515. s->nb_streams = 0;
  516. if (CONFIG_DV_DEMUXER) {
  517. avi->dv_demux = avpriv_dv_init_demux(s);
  518. if (!avi->dv_demux)
  519. goto fail;
  520. } else
  521. goto fail;
  522. s->streams[0]->priv_data = ast;
  523. avio_skip(pb, 3 * 4);
  524. ast->scale = avio_rl32(pb);
  525. ast->rate = avio_rl32(pb);
  526. avio_skip(pb, 4); /* start time */
  527. dv_dur = avio_rl32(pb);
  528. if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
  529. dv_dur *= AV_TIME_BASE;
  530. s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
  531. }
  532. /* else, leave duration alone; timing estimation in utils.c
  533. * will make a guess based on bitrate. */
  534. stream_index = s->nb_streams - 1;
  535. avio_skip(pb, size - 9 * 4);
  536. break;
  537. }
  538. av_assert0(stream_index < s->nb_streams);
  539. st->codec->stream_codec_tag = handler;
  540. avio_rl32(pb); /* flags */
  541. avio_rl16(pb); /* priority */
  542. avio_rl16(pb); /* language */
  543. avio_rl32(pb); /* initial frame */
  544. ast->scale = avio_rl32(pb);
  545. ast->rate = avio_rl32(pb);
  546. if (!(ast->scale && ast->rate)) {
  547. av_log(s, AV_LOG_WARNING,
  548. "scale/rate is %"PRIu32"/%"PRIu32" which is invalid. "
  549. "(This file has been generated by broken software.)\n",
  550. ast->scale,
  551. ast->rate);
  552. if (frame_period) {
  553. ast->rate = 1000000;
  554. ast->scale = frame_period;
  555. } else {
  556. ast->rate = 25;
  557. ast->scale = 1;
  558. }
  559. }
  560. avpriv_set_pts_info(st, 64, ast->scale, ast->rate);
  561. ast->cum_len = avio_rl32(pb); /* start */
  562. st->nb_frames = avio_rl32(pb);
  563. st->start_time = 0;
  564. avio_rl32(pb); /* buffer size */
  565. avio_rl32(pb); /* quality */
  566. if (ast->cum_len*ast->scale/ast->rate > 3600) {
  567. av_log(s, AV_LOG_ERROR, "crazy start time, iam scared, giving up\n");
  568. return AVERROR_INVALIDDATA;
  569. }
  570. ast->sample_size = avio_rl32(pb); /* sample ssize */
  571. ast->cum_len *= FFMAX(1, ast->sample_size);
  572. av_dlog(s, "%"PRIu32" %"PRIu32" %d\n",
  573. ast->rate, ast->scale, ast->sample_size);
  574. switch (tag1) {
  575. case MKTAG('v', 'i', 'd', 's'):
  576. codec_type = AVMEDIA_TYPE_VIDEO;
  577. ast->sample_size = 0;
  578. st->avg_frame_rate = av_inv_q(st->time_base);
  579. break;
  580. case MKTAG('a', 'u', 'd', 's'):
  581. codec_type = AVMEDIA_TYPE_AUDIO;
  582. break;
  583. case MKTAG('t', 'x', 't', 's'):
  584. codec_type = AVMEDIA_TYPE_SUBTITLE;
  585. break;
  586. case MKTAG('d', 'a', 't', 's'):
  587. codec_type = AVMEDIA_TYPE_DATA;
  588. break;
  589. default:
  590. av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
  591. }
  592. if (ast->sample_size == 0) {
  593. st->duration = st->nb_frames;
  594. if (st->duration > 0 && avi->io_fsize > 0 && avi->riff_end > avi->io_fsize) {
  595. av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
  596. st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
  597. }
  598. }
  599. ast->frame_offset = ast->cum_len;
  600. avio_skip(pb, size - 12 * 4);
  601. break;
  602. case MKTAG('s', 't', 'r', 'f'):
  603. /* stream header */
  604. if (!size)
  605. break;
  606. if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
  607. avio_skip(pb, size);
  608. } else {
  609. uint64_t cur_pos = avio_tell(pb);
  610. unsigned esize;
  611. if (cur_pos < list_end)
  612. size = FFMIN(size, list_end - cur_pos);
  613. st = s->streams[stream_index];
  614. if (st->codec->codec_type != AVMEDIA_TYPE_UNKNOWN) {
  615. avio_skip(pb, size);
  616. break;
  617. }
  618. switch (codec_type) {
  619. case AVMEDIA_TYPE_VIDEO:
  620. if (amv_file_format) {
  621. st->codec->width = avih_width;
  622. st->codec->height = avih_height;
  623. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  624. st->codec->codec_id = AV_CODEC_ID_AMV;
  625. avio_skip(pb, size);
  626. break;
  627. }
  628. tag1 = ff_get_bmp_header(pb, st, &esize);
  629. if (tag1 == MKTAG('D', 'X', 'S', 'B') ||
  630. tag1 == MKTAG('D', 'X', 'S', 'A')) {
  631. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  632. st->codec->codec_tag = tag1;
  633. st->codec->codec_id = AV_CODEC_ID_XSUB;
  634. break;
  635. }
  636. if (size > 10 * 4 && size < (1 << 30) && size < avi->fsize) {
  637. if (esize == size-1 && (esize&1)) {
  638. st->codec->extradata_size = esize - 10 * 4;
  639. } else
  640. st->codec->extradata_size = size - 10 * 4;
  641. if (ff_get_extradata(st->codec, pb, st->codec->extradata_size) < 0)
  642. return AVERROR(ENOMEM);
  643. }
  644. // FIXME: check if the encoder really did this correctly
  645. if (st->codec->extradata_size & 1)
  646. avio_r8(pb);
  647. /* Extract palette from extradata if bpp <= 8.
  648. * This code assumes that extradata contains only palette.
  649. * This is true for all paletted codecs implemented in
  650. * FFmpeg. */
  651. if (st->codec->extradata_size &&
  652. (st->codec->bits_per_coded_sample <= 8)) {
  653. int pal_size = (1 << st->codec->bits_per_coded_sample) << 2;
  654. const uint8_t *pal_src;
  655. pal_size = FFMIN(pal_size, st->codec->extradata_size);
  656. pal_src = st->codec->extradata +
  657. st->codec->extradata_size - pal_size;
  658. for (i = 0; i < pal_size / 4; i++)
  659. ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
  660. ast->has_pal = 1;
  661. }
  662. print_tag("video", tag1, 0);
  663. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  664. st->codec->codec_tag = tag1;
  665. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags,
  666. tag1);
  667. /* This is needed to get the pict type which is necessary
  668. * for generating correct pts. */
  669. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  670. if (st->codec->codec_tag == MKTAG('V', 'S', 'S', 'H'))
  671. st->need_parsing = AVSTREAM_PARSE_FULL;
  672. if (st->codec->codec_tag == 0 && st->codec->height > 0 &&
  673. st->codec->extradata_size < 1U << 30) {
  674. st->codec->extradata_size += 9;
  675. if ((ret = av_reallocp(&st->codec->extradata,
  676. st->codec->extradata_size +
  677. FF_INPUT_BUFFER_PADDING_SIZE)) < 0) {
  678. st->codec->extradata_size = 0;
  679. return ret;
  680. } else
  681. memcpy(st->codec->extradata + st->codec->extradata_size - 9,
  682. "BottomUp", 9);
  683. }
  684. st->codec->height = FFABS(st->codec->height);
  685. // avio_skip(pb, size - 5 * 4);
  686. break;
  687. case AVMEDIA_TYPE_AUDIO:
  688. ret = ff_get_wav_header(pb, st->codec, size);
  689. if (ret < 0)
  690. return ret;
  691. ast->dshow_block_align = st->codec->block_align;
  692. if (ast->sample_size && st->codec->block_align &&
  693. ast->sample_size != st->codec->block_align) {
  694. av_log(s,
  695. AV_LOG_WARNING,
  696. "sample size (%d) != block align (%d)\n",
  697. ast->sample_size,
  698. st->codec->block_align);
  699. ast->sample_size = st->codec->block_align;
  700. }
  701. /* 2-aligned
  702. * (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  703. if (size & 1)
  704. avio_skip(pb, 1);
  705. /* Force parsing as several audio frames can be in
  706. * one packet and timestamps refer to packet start. */
  707. st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
  708. /* ADTS header is in extradata, AAC without header must be
  709. * stored as exact frames. Parser not needed and it will
  710. * fail. */
  711. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  712. st->codec->extradata_size)
  713. st->need_parsing = AVSTREAM_PARSE_NONE;
  714. /* AVI files with Xan DPCM audio (wrongly) declare PCM
  715. * audio in the header but have Axan as stream_code_tag. */
  716. if (st->codec->stream_codec_tag == AV_RL32("Axan")) {
  717. st->codec->codec_id = AV_CODEC_ID_XAN_DPCM;
  718. st->codec->codec_tag = 0;
  719. ast->dshow_block_align = 0;
  720. }
  721. if (amv_file_format) {
  722. st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_AMV;
  723. ast->dshow_block_align = 0;
  724. }
  725. if (st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align <= 4 && ast->dshow_block_align) {
  726. av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
  727. ast->dshow_block_align = 0;
  728. }
  729. if (st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
  730. st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
  731. st->codec->codec_id == AV_CODEC_ID_MP3 && ast->dshow_block_align == 1152 && ast->sample_size == 1152) {
  732. av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
  733. ast->sample_size = 0;
  734. }
  735. break;
  736. case AVMEDIA_TYPE_SUBTITLE:
  737. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  738. st->request_probe= 1;
  739. avio_skip(pb, size);
  740. break;
  741. default:
  742. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  743. st->codec->codec_id = AV_CODEC_ID_NONE;
  744. st->codec->codec_tag = 0;
  745. avio_skip(pb, size);
  746. break;
  747. }
  748. }
  749. break;
  750. case MKTAG('s', 't', 'r', 'd'):
  751. if (stream_index >= (unsigned)s->nb_streams
  752. || s->streams[stream_index]->codec->extradata_size
  753. || s->streams[stream_index]->codec->codec_tag == MKTAG('H','2','6','4')) {
  754. avio_skip(pb, size);
  755. } else {
  756. uint64_t cur_pos = avio_tell(pb);
  757. if (cur_pos < list_end)
  758. size = FFMIN(size, list_end - cur_pos);
  759. st = s->streams[stream_index];
  760. if (size<(1<<30)) {
  761. if (ff_get_extradata(st->codec, pb, size) < 0)
  762. return AVERROR(ENOMEM);
  763. }
  764. if (st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
  765. avio_r8(pb);
  766. ret = avi_extract_stream_metadata(st);
  767. if (ret < 0) {
  768. av_log(s, AV_LOG_WARNING, "could not decoding EXIF data in stream header.\n");
  769. }
  770. }
  771. break;
  772. case MKTAG('i', 'n', 'd', 'x'):
  773. i = avio_tell(pb);
  774. if (pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
  775. avi->use_odml &&
  776. read_braindead_odml_indx(s, 0) < 0 &&
  777. (s->error_recognition & AV_EF_EXPLODE))
  778. goto fail;
  779. avio_seek(pb, i + size, SEEK_SET);
  780. break;
  781. case MKTAG('v', 'p', 'r', 'p'):
  782. if (stream_index < (unsigned)s->nb_streams && size > 9 * 4) {
  783. AVRational active, active_aspect;
  784. st = s->streams[stream_index];
  785. avio_rl32(pb);
  786. avio_rl32(pb);
  787. avio_rl32(pb);
  788. avio_rl32(pb);
  789. avio_rl32(pb);
  790. active_aspect.den = avio_rl16(pb);
  791. active_aspect.num = avio_rl16(pb);
  792. active.num = avio_rl32(pb);
  793. active.den = avio_rl32(pb);
  794. avio_rl32(pb); // nbFieldsPerFrame
  795. if (active_aspect.num && active_aspect.den &&
  796. active.num && active.den) {
  797. st->sample_aspect_ratio = av_div_q(active_aspect, active);
  798. av_dlog(s, "vprp %d/%d %d/%d\n",
  799. active_aspect.num, active_aspect.den,
  800. active.num, active.den);
  801. }
  802. size -= 9 * 4;
  803. }
  804. avio_skip(pb, size);
  805. break;
  806. case MKTAG('s', 't', 'r', 'n'):
  807. if (s->nb_streams) {
  808. ret = avi_read_tag(s, s->streams[s->nb_streams - 1], tag, size);
  809. if (ret < 0)
  810. return ret;
  811. break;
  812. }
  813. default:
  814. if (size > 1000000) {
  815. av_log(s, AV_LOG_ERROR,
  816. "Something went wrong during header parsing, "
  817. "I will ignore it and try to continue anyway.\n");
  818. if (s->error_recognition & AV_EF_EXPLODE)
  819. goto fail;
  820. avi->movi_list = avio_tell(pb) - 4;
  821. avi->movi_end = avi->fsize;
  822. goto end_of_header;
  823. }
  824. /* skip tag */
  825. size += (size & 1);
  826. avio_skip(pb, size);
  827. break;
  828. }
  829. }
  830. end_of_header:
  831. /* check stream number */
  832. if (stream_index != s->nb_streams - 1) {
  833. fail:
  834. return AVERROR_INVALIDDATA;
  835. }
  836. if (!avi->index_loaded && pb->seekable)
  837. avi_load_index(s);
  838. calculate_bitrate(s);
  839. avi->index_loaded |= 1;
  840. if ((ret = guess_ni_flag(s)) < 0)
  841. return ret;
  842. avi->non_interleaved |= ret | (s->flags & AVFMT_FLAG_SORT_DTS);
  843. dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
  844. if (dict_entry && !strcmp(dict_entry->value, "PotEncoder"))
  845. for (i = 0; i < s->nb_streams; i++) {
  846. AVStream *st = s->streams[i];
  847. if ( st->codec->codec_id == AV_CODEC_ID_MPEG1VIDEO
  848. || st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
  849. st->need_parsing = AVSTREAM_PARSE_FULL;
  850. }
  851. for (i = 0; i < s->nb_streams; i++) {
  852. AVStream *st = s->streams[i];
  853. if (st->nb_index_entries)
  854. break;
  855. }
  856. // DV-in-AVI cannot be non-interleaved, if set this must be
  857. // a mis-detection.
  858. if (avi->dv_demux)
  859. avi->non_interleaved = 0;
  860. if (i == s->nb_streams && avi->non_interleaved) {
  861. av_log(s, AV_LOG_WARNING,
  862. "Non-interleaved AVI without index, switching to interleaved\n");
  863. avi->non_interleaved = 0;
  864. }
  865. if (avi->non_interleaved) {
  866. av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
  867. clean_index(s);
  868. }
  869. ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
  870. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  871. return 0;
  872. }
  873. static int read_gab2_sub(AVStream *st, AVPacket *pkt)
  874. {
  875. if (pkt->size >= 7 &&
  876. pkt->size < INT_MAX - AVPROBE_PADDING_SIZE &&
  877. !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
  878. uint8_t desc[256];
  879. int score = AVPROBE_SCORE_EXTENSION, ret;
  880. AVIStream *ast = st->priv_data;
  881. AVInputFormat *sub_demuxer;
  882. AVRational time_base;
  883. int size;
  884. AVIOContext *pb = avio_alloc_context(pkt->data + 7,
  885. pkt->size - 7,
  886. 0, NULL, NULL, NULL, NULL);
  887. AVProbeData pd;
  888. unsigned int desc_len = avio_rl32(pb);
  889. if (desc_len > pb->buf_end - pb->buf_ptr)
  890. goto error;
  891. ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
  892. avio_skip(pb, desc_len - ret);
  893. if (*desc)
  894. av_dict_set(&st->metadata, "title", desc, 0);
  895. avio_rl16(pb); /* flags? */
  896. avio_rl32(pb); /* data size */
  897. size = pb->buf_end - pb->buf_ptr;
  898. pd = (AVProbeData) { .buf = av_mallocz(size + AVPROBE_PADDING_SIZE),
  899. .buf_size = size };
  900. if (!pd.buf)
  901. goto error;
  902. memcpy(pd.buf, pb->buf_ptr, size);
  903. sub_demuxer = av_probe_input_format2(&pd, 1, &score);
  904. av_freep(&pd.buf);
  905. if (!sub_demuxer)
  906. goto error;
  907. if (!(ast->sub_ctx = avformat_alloc_context()))
  908. goto error;
  909. ast->sub_ctx->pb = pb;
  910. if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
  911. ff_read_packet(ast->sub_ctx, &ast->sub_pkt);
  912. *st->codec = *ast->sub_ctx->streams[0]->codec;
  913. ast->sub_ctx->streams[0]->codec->extradata = NULL;
  914. time_base = ast->sub_ctx->streams[0]->time_base;
  915. avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
  916. }
  917. ast->sub_buffer = pkt->data;
  918. memset(pkt, 0, sizeof(*pkt));
  919. return 1;
  920. error:
  921. av_freep(&pb);
  922. }
  923. return 0;
  924. }
  925. static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
  926. AVPacket *pkt)
  927. {
  928. AVIStream *ast, *next_ast = next_st->priv_data;
  929. int64_t ts, next_ts, ts_min = INT64_MAX;
  930. AVStream *st, *sub_st = NULL;
  931. int i;
  932. next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
  933. AV_TIME_BASE_Q);
  934. for (i = 0; i < s->nb_streams; i++) {
  935. st = s->streams[i];
  936. ast = st->priv_data;
  937. if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
  938. ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
  939. if (ts <= next_ts && ts < ts_min) {
  940. ts_min = ts;
  941. sub_st = st;
  942. }
  943. }
  944. }
  945. if (sub_st) {
  946. ast = sub_st->priv_data;
  947. *pkt = ast->sub_pkt;
  948. pkt->stream_index = sub_st->index;
  949. if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
  950. ast->sub_pkt.data = NULL;
  951. }
  952. return sub_st;
  953. }
  954. static int get_stream_idx(unsigned *d)
  955. {
  956. if (d[0] >= '0' && d[0] <= '9' &&
  957. d[1] >= '0' && d[1] <= '9') {
  958. return (d[0] - '0') * 10 + (d[1] - '0');
  959. } else {
  960. return 100; // invalid stream ID
  961. }
  962. }
  963. /**
  964. *
  965. * @param exit_early set to 1 to just gather packet position without making the changes needed to actually read & return the packet
  966. */
  967. static int avi_sync(AVFormatContext *s, int exit_early)
  968. {
  969. AVIContext *avi = s->priv_data;
  970. AVIOContext *pb = s->pb;
  971. int n;
  972. unsigned int d[8];
  973. unsigned int size;
  974. int64_t i, sync;
  975. start_sync:
  976. memset(d, -1, sizeof(d));
  977. for (i = sync = avio_tell(pb); !url_feof(pb); i++) {
  978. int j;
  979. for (j = 0; j < 7; j++)
  980. d[j] = d[j + 1];
  981. d[7] = avio_r8(pb);
  982. size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
  983. n = get_stream_idx(d + 2);
  984. av_dlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
  985. d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
  986. if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
  987. continue;
  988. // parse ix##
  989. if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
  990. // parse JUNK
  991. (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
  992. (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')) {
  993. avio_skip(pb, size);
  994. goto start_sync;
  995. }
  996. // parse stray LIST
  997. if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
  998. avio_skip(pb, 4);
  999. goto start_sync;
  1000. }
  1001. n = avi->dv_demux ? 0 : get_stream_idx(d);
  1002. if (!((i - avi->last_pkt_pos) & 1) &&
  1003. get_stream_idx(d + 1) < s->nb_streams)
  1004. continue;
  1005. // detect ##ix chunk and skip
  1006. if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
  1007. avio_skip(pb, size);
  1008. goto start_sync;
  1009. }
  1010. // parse ##dc/##wb
  1011. if (n < s->nb_streams) {
  1012. AVStream *st;
  1013. AVIStream *ast;
  1014. st = s->streams[n];
  1015. ast = st->priv_data;
  1016. if (!ast) {
  1017. av_log(s, AV_LOG_WARNING, "Skipping foreign stream %d packet\n", n);
  1018. continue;
  1019. }
  1020. if (s->nb_streams >= 2) {
  1021. AVStream *st1 = s->streams[1];
  1022. AVIStream *ast1 = st1->priv_data;
  1023. // workaround for broken small-file-bug402.avi
  1024. if ( d[2] == 'w' && d[3] == 'b'
  1025. && n == 0
  1026. && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO
  1027. && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO
  1028. && ast->prefix == 'd'*256+'c'
  1029. && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
  1030. ) {
  1031. n = 1;
  1032. st = st1;
  1033. ast = ast1;
  1034. av_log(s, AV_LOG_WARNING,
  1035. "Invalid stream + prefix combination, assuming audio.\n");
  1036. }
  1037. }
  1038. if (!avi->dv_demux &&
  1039. ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
  1040. // FIXME: needs a little reordering
  1041. (st->discard >= AVDISCARD_NONKEY &&
  1042. !(pkt->flags & AV_PKT_FLAG_KEY)) */
  1043. || st->discard >= AVDISCARD_ALL)) {
  1044. if (!exit_early) {
  1045. ast->frame_offset += get_duration(ast, size);
  1046. avio_skip(pb, size);
  1047. goto start_sync;
  1048. }
  1049. }
  1050. if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
  1051. int k = avio_r8(pb);
  1052. int last = (k + avio_r8(pb) - 1) & 0xFF;
  1053. avio_rl16(pb); // flags
  1054. // b + (g << 8) + (r << 16);
  1055. for (; k <= last; k++)
  1056. ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
  1057. ast->has_pal = 1;
  1058. goto start_sync;
  1059. } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
  1060. d[2] < 128 && d[3] < 128) ||
  1061. d[2] * 256 + d[3] == ast->prefix /* ||
  1062. (d[2] == 'd' && d[3] == 'c') ||
  1063. (d[2] == 'w' && d[3] == 'b') */) {
  1064. if (exit_early)
  1065. return 0;
  1066. if (d[2] * 256 + d[3] == ast->prefix)
  1067. ast->prefix_count++;
  1068. else {
  1069. ast->prefix = d[2] * 256 + d[3];
  1070. ast->prefix_count = 0;
  1071. }
  1072. avi->stream_index = n;
  1073. ast->packet_size = size + 8;
  1074. ast->remaining = size;
  1075. if (size) {
  1076. uint64_t pos = avio_tell(pb) - 8;
  1077. if (!st->index_entries || !st->nb_index_entries ||
  1078. st->index_entries[st->nb_index_entries - 1].pos < pos) {
  1079. av_add_index_entry(st, pos, ast->frame_offset, size,
  1080. 0, AVINDEX_KEYFRAME);
  1081. }
  1082. }
  1083. return 0;
  1084. }
  1085. }
  1086. }
  1087. if (pb->error)
  1088. return pb->error;
  1089. return AVERROR_EOF;
  1090. }
  1091. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  1092. {
  1093. AVIContext *avi = s->priv_data;
  1094. AVIOContext *pb = s->pb;
  1095. int err;
  1096. #if FF_API_DESTRUCT_PACKET
  1097. void *dstr;
  1098. #endif
  1099. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  1100. int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
  1101. if (size >= 0)
  1102. return size;
  1103. else
  1104. goto resync;
  1105. }
  1106. if (avi->non_interleaved) {
  1107. int best_stream_index = 0;
  1108. AVStream *best_st = NULL;
  1109. AVIStream *best_ast;
  1110. int64_t best_ts = INT64_MAX;
  1111. int i;
  1112. for (i = 0; i < s->nb_streams; i++) {
  1113. AVStream *st = s->streams[i];
  1114. AVIStream *ast = st->priv_data;
  1115. int64_t ts = ast->frame_offset;
  1116. int64_t last_ts;
  1117. if (!st->nb_index_entries)
  1118. continue;
  1119. last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
  1120. if (!ast->remaining && ts > last_ts)
  1121. continue;
  1122. ts = av_rescale_q(ts, st->time_base,
  1123. (AVRational) { FFMAX(1, ast->sample_size),
  1124. AV_TIME_BASE });
  1125. av_dlog(s, "%"PRId64" %d/%d %"PRId64"\n", ts,
  1126. st->time_base.num, st->time_base.den, ast->frame_offset);
  1127. if (ts < best_ts) {
  1128. best_ts = ts;
  1129. best_st = st;
  1130. best_stream_index = i;
  1131. }
  1132. }
  1133. if (!best_st)
  1134. return AVERROR_EOF;
  1135. best_ast = best_st->priv_data;
  1136. best_ts = best_ast->frame_offset;
  1137. if (best_ast->remaining) {
  1138. i = av_index_search_timestamp(best_st,
  1139. best_ts,
  1140. AVSEEK_FLAG_ANY |
  1141. AVSEEK_FLAG_BACKWARD);
  1142. } else {
  1143. i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  1144. if (i >= 0)
  1145. best_ast->frame_offset = best_st->index_entries[i].timestamp;
  1146. }
  1147. if (i >= 0) {
  1148. int64_t pos = best_st->index_entries[i].pos;
  1149. pos += best_ast->packet_size - best_ast->remaining;
  1150. if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
  1151. return AVERROR_EOF;
  1152. av_assert0(best_ast->remaining <= best_ast->packet_size);
  1153. avi->stream_index = best_stream_index;
  1154. if (!best_ast->remaining)
  1155. best_ast->packet_size =
  1156. best_ast->remaining = best_st->index_entries[i].size;
  1157. }
  1158. else
  1159. return AVERROR_EOF;
  1160. }
  1161. resync:
  1162. if (avi->stream_index >= 0) {
  1163. AVStream *st = s->streams[avi->stream_index];
  1164. AVIStream *ast = st->priv_data;
  1165. int size, err;
  1166. if (get_subtitle_pkt(s, st, pkt))
  1167. return 0;
  1168. // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
  1169. if (ast->sample_size <= 1)
  1170. size = INT_MAX;
  1171. else if (ast->sample_size < 32)
  1172. // arbitrary multiplier to avoid tiny packets for raw PCM data
  1173. size = 1024 * ast->sample_size;
  1174. else
  1175. size = ast->sample_size;
  1176. if (size > ast->remaining)
  1177. size = ast->remaining;
  1178. avi->last_pkt_pos = avio_tell(pb);
  1179. err = av_get_packet(pb, pkt, size);
  1180. if (err < 0)
  1181. return err;
  1182. size = err;
  1183. if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2) {
  1184. uint8_t *pal;
  1185. pal = av_packet_new_side_data(pkt,
  1186. AV_PKT_DATA_PALETTE,
  1187. AVPALETTE_SIZE);
  1188. if (!pal) {
  1189. av_log(s, AV_LOG_ERROR,
  1190. "Failed to allocate data for palette\n");
  1191. } else {
  1192. memcpy(pal, ast->pal, AVPALETTE_SIZE);
  1193. ast->has_pal = 0;
  1194. }
  1195. }
  1196. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  1197. AVBufferRef *avbuf = pkt->buf;
  1198. #if FF_API_DESTRUCT_PACKET
  1199. FF_DISABLE_DEPRECATION_WARNINGS
  1200. dstr = pkt->destruct;
  1201. FF_ENABLE_DEPRECATION_WARNINGS
  1202. #endif
  1203. size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
  1204. pkt->data, pkt->size, pkt->pos);
  1205. #if FF_API_DESTRUCT_PACKET
  1206. FF_DISABLE_DEPRECATION_WARNINGS
  1207. pkt->destruct = dstr;
  1208. FF_ENABLE_DEPRECATION_WARNINGS
  1209. #endif
  1210. pkt->buf = avbuf;
  1211. pkt->flags |= AV_PKT_FLAG_KEY;
  1212. if (size < 0)
  1213. av_free_packet(pkt);
  1214. } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  1215. !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
  1216. ast->frame_offset++;
  1217. avi->stream_index = -1;
  1218. ast->remaining = 0;
  1219. goto resync;
  1220. } else {
  1221. /* XXX: How to handle B-frames in AVI? */
  1222. pkt->dts = ast->frame_offset;
  1223. // pkt->dts += ast->start;
  1224. if (ast->sample_size)
  1225. pkt->dts /= ast->sample_size;
  1226. av_dlog(s,
  1227. "dts:%"PRId64" offset:%"PRId64" %d/%d smpl_siz:%d "
  1228. "base:%d st:%d size:%d\n",
  1229. pkt->dts,
  1230. ast->frame_offset,
  1231. ast->scale,
  1232. ast->rate,
  1233. ast->sample_size,
  1234. AV_TIME_BASE,
  1235. avi->stream_index,
  1236. size);
  1237. pkt->stream_index = avi->stream_index;
  1238. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->index_entries) {
  1239. AVIndexEntry *e;
  1240. int index;
  1241. index = av_index_search_timestamp(st, ast->frame_offset, AVSEEK_FLAG_ANY);
  1242. e = &st->index_entries[index];
  1243. if (index >= 0 && e->timestamp == ast->frame_offset) {
  1244. if (index == st->nb_index_entries-1) {
  1245. int key=1;
  1246. int i;
  1247. uint32_t state=-1;
  1248. for (i=0; i<FFMIN(size,256); i++) {
  1249. if (st->codec->codec_id == AV_CODEC_ID_MPEG4) {
  1250. if (state == 0x1B6) {
  1251. key= !(pkt->data[i]&0xC0);
  1252. break;
  1253. }
  1254. }else
  1255. break;
  1256. state= (state<<8) + pkt->data[i];
  1257. }
  1258. if (!key)
  1259. e->flags &= ~AVINDEX_KEYFRAME;
  1260. }
  1261. if (e->flags & AVINDEX_KEYFRAME)
  1262. pkt->flags |= AV_PKT_FLAG_KEY;
  1263. }
  1264. } else {
  1265. pkt->flags |= AV_PKT_FLAG_KEY;
  1266. }
  1267. ast->frame_offset += get_duration(ast, pkt->size);
  1268. }
  1269. ast->remaining -= err;
  1270. if (!ast->remaining) {
  1271. avi->stream_index = -1;
  1272. ast->packet_size = 0;
  1273. }
  1274. if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
  1275. av_free_packet(pkt);
  1276. goto resync;
  1277. }
  1278. ast->seek_pos= 0;
  1279. if (!avi->non_interleaved && st->nb_index_entries>1 && avi->index_loaded>1) {
  1280. int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
  1281. if (avi->dts_max - dts > 2*AV_TIME_BASE) {
  1282. avi->non_interleaved= 1;
  1283. av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
  1284. }else if (avi->dts_max < dts)
  1285. avi->dts_max = dts;
  1286. }
  1287. return 0;
  1288. }
  1289. if ((err = avi_sync(s, 0)) < 0)
  1290. return err;
  1291. goto resync;
  1292. }
  1293. /* XXX: We make the implicit supposition that the positions are sorted
  1294. * for each stream. */
  1295. static int avi_read_idx1(AVFormatContext *s, int size)
  1296. {
  1297. AVIContext *avi = s->priv_data;
  1298. AVIOContext *pb = s->pb;
  1299. int nb_index_entries, i;
  1300. AVStream *st;
  1301. AVIStream *ast;
  1302. unsigned int index, tag, flags, pos, len, first_packet = 1;
  1303. unsigned last_pos = -1;
  1304. unsigned last_idx = -1;
  1305. int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
  1306. int anykey = 0;
  1307. nb_index_entries = size / 16;
  1308. if (nb_index_entries <= 0)
  1309. return AVERROR_INVALIDDATA;
  1310. idx1_pos = avio_tell(pb);
  1311. avio_seek(pb, avi->movi_list + 4, SEEK_SET);
  1312. if (avi_sync(s, 1) == 0)
  1313. first_packet_pos = avio_tell(pb) - 8;
  1314. avi->stream_index = -1;
  1315. avio_seek(pb, idx1_pos, SEEK_SET);
  1316. if (s->nb_streams == 1 && s->streams[0]->codec->codec_tag == AV_RL32("MMES")) {
  1317. first_packet_pos = 0;
  1318. data_offset = avi->movi_list;
  1319. }
  1320. /* Read the entries and sort them in each stream component. */
  1321. for (i = 0; i < nb_index_entries; i++) {
  1322. if (url_feof(pb))
  1323. return -1;
  1324. tag = avio_rl32(pb);
  1325. flags = avio_rl32(pb);
  1326. pos = avio_rl32(pb);
  1327. len = avio_rl32(pb);
  1328. av_dlog(s, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  1329. i, tag, flags, pos, len);
  1330. index = ((tag & 0xff) - '0') * 10;
  1331. index += (tag >> 8 & 0xff) - '0';
  1332. if (index >= s->nb_streams)
  1333. continue;
  1334. st = s->streams[index];
  1335. ast = st->priv_data;
  1336. if (first_packet && first_packet_pos) {
  1337. data_offset = first_packet_pos - pos;
  1338. first_packet = 0;
  1339. }
  1340. pos += data_offset;
  1341. av_dlog(s, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
  1342. // even if we have only a single stream, we should
  1343. // switch to non-interleaved to get correct timestamps
  1344. if (last_pos == pos)
  1345. avi->non_interleaved = 1;
  1346. if (last_idx != pos && len) {
  1347. av_add_index_entry(st, pos, ast->cum_len, len, 0,
  1348. (flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  1349. last_idx= pos;
  1350. }
  1351. ast->cum_len += get_duration(ast, len);
  1352. last_pos = pos;
  1353. anykey |= flags&AVIIF_INDEX;
  1354. }
  1355. if (!anykey) {
  1356. for (index = 0; index < s->nb_streams; index++) {
  1357. st = s->streams[index];
  1358. if (st->nb_index_entries)
  1359. st->index_entries[0].flags |= AVINDEX_KEYFRAME;
  1360. }
  1361. }
  1362. return 0;
  1363. }
  1364. /* Scan the index and consider any file with streams more than
  1365. * 2 seconds or 64MB apart non-interleaved. */
  1366. static int check_stream_max_drift(AVFormatContext *s)
  1367. {
  1368. int64_t min_pos, pos;
  1369. int i;
  1370. int *idx = av_mallocz_array(s->nb_streams, sizeof(*idx));
  1371. if (!idx)
  1372. return AVERROR(ENOMEM);
  1373. for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) {
  1374. int64_t max_dts = INT64_MIN / 2;
  1375. int64_t min_dts = INT64_MAX / 2;
  1376. int64_t max_buffer = 0;
  1377. min_pos = INT64_MAX;
  1378. for (i = 0; i < s->nb_streams; i++) {
  1379. AVStream *st = s->streams[i];
  1380. AVIStream *ast = st->priv_data;
  1381. int n = st->nb_index_entries;
  1382. while (idx[i] < n && st->index_entries[idx[i]].pos < pos)
  1383. idx[i]++;
  1384. if (idx[i] < n) {
  1385. int64_t dts;
  1386. dts = av_rescale_q(st->index_entries[idx[i]].timestamp /
  1387. FFMAX(ast->sample_size, 1),
  1388. st->time_base, AV_TIME_BASE_Q);
  1389. min_dts = FFMIN(min_dts, dts);
  1390. min_pos = FFMIN(min_pos, st->index_entries[idx[i]].pos);
  1391. }
  1392. }
  1393. for (i = 0; i < s->nb_streams; i++) {
  1394. AVStream *st = s->streams[i];
  1395. AVIStream *ast = st->priv_data;
  1396. if (idx[i] && min_dts != INT64_MAX / 2) {
  1397. int64_t dts;
  1398. dts = av_rescale_q(st->index_entries[idx[i] - 1].timestamp /
  1399. FFMAX(ast->sample_size, 1),
  1400. st->time_base, AV_TIME_BASE_Q);
  1401. max_dts = FFMAX(max_dts, dts);
  1402. max_buffer = FFMAX(max_buffer,
  1403. av_rescale(dts - min_dts,
  1404. st->codec->bit_rate,
  1405. AV_TIME_BASE));
  1406. }
  1407. }
  1408. if (max_dts - min_dts > 2 * AV_TIME_BASE ||
  1409. max_buffer > 1024 * 1024 * 8 * 8) {
  1410. av_free(idx);
  1411. return 1;
  1412. }
  1413. }
  1414. av_free(idx);
  1415. return 0;
  1416. }
  1417. static int guess_ni_flag(AVFormatContext *s)
  1418. {
  1419. int i;
  1420. int64_t last_start = 0;
  1421. int64_t first_end = INT64_MAX;
  1422. int64_t oldpos = avio_tell(s->pb);
  1423. for (i = 0; i < s->nb_streams; i++) {
  1424. AVStream *st = s->streams[i];
  1425. int n = st->nb_index_entries;
  1426. unsigned int size;
  1427. if (n <= 0)
  1428. continue;
  1429. if (n >= 2) {
  1430. int64_t pos = st->index_entries[0].pos;
  1431. avio_seek(s->pb, pos + 4, SEEK_SET);
  1432. size = avio_rl32(s->pb);
  1433. if (pos + size > st->index_entries[1].pos)
  1434. last_start = INT64_MAX;
  1435. }
  1436. if (st->index_entries[0].pos > last_start)
  1437. last_start = st->index_entries[0].pos;
  1438. if (st->index_entries[n - 1].pos < first_end)
  1439. first_end = st->index_entries[n - 1].pos;
  1440. }
  1441. avio_seek(s->pb, oldpos, SEEK_SET);
  1442. if (last_start > first_end)
  1443. return 1;
  1444. return check_stream_max_drift(s);
  1445. }
  1446. static int avi_load_index(AVFormatContext *s)
  1447. {
  1448. AVIContext *avi = s->priv_data;
  1449. AVIOContext *pb = s->pb;
  1450. uint32_t tag, size;
  1451. int64_t pos = avio_tell(pb);
  1452. int64_t next;
  1453. int ret = -1;
  1454. if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
  1455. goto the_end; // maybe truncated file
  1456. av_dlog(s, "movi_end=0x%"PRIx64"\n", avi->movi_end);
  1457. for (;;) {
  1458. tag = avio_rl32(pb);
  1459. size = avio_rl32(pb);
  1460. if (url_feof(pb))
  1461. break;
  1462. next = avio_tell(pb) + size + (size & 1);
  1463. av_dlog(s, "tag=%c%c%c%c size=0x%x\n",
  1464. tag & 0xff,
  1465. (tag >> 8) & 0xff,
  1466. (tag >> 16) & 0xff,
  1467. (tag >> 24) & 0xff,
  1468. size);
  1469. if (tag == MKTAG('i', 'd', 'x', '1') &&
  1470. avi_read_idx1(s, size) >= 0) {
  1471. avi->index_loaded=2;
  1472. ret = 0;
  1473. }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
  1474. uint32_t tag1 = avio_rl32(pb);
  1475. if (tag1 == MKTAG('I', 'N', 'F', 'O'))
  1476. ff_read_riff_info(s, size - 4);
  1477. }else if (!ret)
  1478. break;
  1479. if (avio_seek(pb, next, SEEK_SET) < 0)
  1480. break; // something is wrong here
  1481. }
  1482. the_end:
  1483. avio_seek(pb, pos, SEEK_SET);
  1484. return ret;
  1485. }
  1486. static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
  1487. {
  1488. AVIStream *ast2 = st2->priv_data;
  1489. int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
  1490. av_free_packet(&ast2->sub_pkt);
  1491. if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
  1492. avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
  1493. ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
  1494. }
  1495. static int avi_read_seek(AVFormatContext *s, int stream_index,
  1496. int64_t timestamp, int flags)
  1497. {
  1498. AVIContext *avi = s->priv_data;
  1499. AVStream *st;
  1500. int i, index;
  1501. int64_t pos, pos_min;
  1502. AVIStream *ast;
  1503. /* Does not matter which stream is requested dv in avi has the
  1504. * stream information in the first video stream.
  1505. */
  1506. if (avi->dv_demux)
  1507. stream_index = 0;
  1508. if (!avi->index_loaded) {
  1509. /* we only load the index on demand */
  1510. avi_load_index(s);
  1511. avi->index_loaded |= 1;
  1512. }
  1513. av_assert0(stream_index >= 0);
  1514. st = s->streams[stream_index];
  1515. ast = st->priv_data;
  1516. index = av_index_search_timestamp(st,
  1517. timestamp * FFMAX(ast->sample_size, 1),
  1518. flags);
  1519. if (index < 0) {
  1520. if (st->nb_index_entries > 0)
  1521. av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
  1522. timestamp * FFMAX(ast->sample_size, 1),
  1523. st->index_entries[0].timestamp,
  1524. st->index_entries[st->nb_index_entries - 1].timestamp);
  1525. return AVERROR_INVALIDDATA;
  1526. }
  1527. /* find the position */
  1528. pos = st->index_entries[index].pos;
  1529. timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
  1530. av_dlog(s, "XX %"PRId64" %d %"PRId64"\n",
  1531. timestamp, index, st->index_entries[index].timestamp);
  1532. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  1533. /* One and only one real stream for DV in AVI, and it has video */
  1534. /* offsets. Calling with other stream indexes should have failed */
  1535. /* the av_index_search_timestamp call above. */
  1536. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  1537. return -1;
  1538. /* Feed the DV video stream version of the timestamp to the */
  1539. /* DV demux so it can synthesize correct timestamps. */
  1540. ff_dv_offset_reset(avi->dv_demux, timestamp);
  1541. avi->stream_index = -1;
  1542. return 0;
  1543. }
  1544. pos_min = pos;
  1545. for (i = 0; i < s->nb_streams; i++) {
  1546. AVStream *st2 = s->streams[i];
  1547. AVIStream *ast2 = st2->priv_data;
  1548. ast2->packet_size =
  1549. ast2->remaining = 0;
  1550. if (ast2->sub_ctx) {
  1551. seek_subtitle(st, st2, timestamp);
  1552. continue;
  1553. }
  1554. if (st2->nb_index_entries <= 0)
  1555. continue;
  1556. // av_assert1(st2->codec->block_align);
  1557. av_assert0(fabs(av_q2d(st2->time_base) - ast2->scale / (double)ast2->rate) < av_q2d(st2->time_base) * 0.00000001);
  1558. index = av_index_search_timestamp(st2,
  1559. av_rescale_q(timestamp,
  1560. st->time_base,
  1561. st2->time_base) *
  1562. FFMAX(ast2->sample_size, 1),
  1563. flags |
  1564. AVSEEK_FLAG_BACKWARD |
  1565. (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
  1566. if (index < 0)
  1567. index = 0;
  1568. ast2->seek_pos = st2->index_entries[index].pos;
  1569. pos_min = FFMIN(pos_min,ast2->seek_pos);
  1570. }
  1571. for (i = 0; i < s->nb_streams; i++) {
  1572. AVStream *st2 = s->streams[i];
  1573. AVIStream *ast2 = st2->priv_data;
  1574. if (ast2->sub_ctx || st2->nb_index_entries <= 0)
  1575. continue;
  1576. index = av_index_search_timestamp(
  1577. st2,
  1578. av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
  1579. flags | AVSEEK_FLAG_BACKWARD | (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
  1580. if (index < 0)
  1581. index = 0;
  1582. while (!avi->non_interleaved && index>0 && st2->index_entries[index-1].pos >= pos_min)
  1583. index--;
  1584. ast2->frame_offset = st2->index_entries[index].timestamp;
  1585. }
  1586. /* do the seek */
  1587. if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
  1588. av_log(s, AV_LOG_ERROR, "Seek failed\n");
  1589. return -1;
  1590. }
  1591. avi->stream_index = -1;
  1592. avi->dts_max = INT_MIN;
  1593. return 0;
  1594. }
  1595. static int avi_read_close(AVFormatContext *s)
  1596. {
  1597. int i;
  1598. AVIContext *avi = s->priv_data;
  1599. for (i = 0; i < s->nb_streams; i++) {
  1600. AVStream *st = s->streams[i];
  1601. AVIStream *ast = st->priv_data;
  1602. if (ast) {
  1603. if (ast->sub_ctx) {
  1604. av_freep(&ast->sub_ctx->pb);
  1605. avformat_close_input(&ast->sub_ctx);
  1606. }
  1607. av_free(ast->sub_buffer);
  1608. av_free_packet(&ast->sub_pkt);
  1609. }
  1610. }
  1611. av_free(avi->dv_demux);
  1612. return 0;
  1613. }
  1614. static int avi_probe(AVProbeData *p)
  1615. {
  1616. int i;
  1617. /* check file header */
  1618. for (i = 0; avi_headers[i][0]; i++)
  1619. if (!memcmp(p->buf, avi_headers[i], 4) &&
  1620. !memcmp(p->buf + 8, avi_headers[i] + 4, 4))
  1621. return AVPROBE_SCORE_MAX;
  1622. return 0;
  1623. }
  1624. AVInputFormat ff_avi_demuxer = {
  1625. .name = "avi",
  1626. .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
  1627. .priv_data_size = sizeof(AVIContext),
  1628. .extensions = "avi",
  1629. .read_probe = avi_probe,
  1630. .read_header = avi_read_header,
  1631. .read_packet = avi_read_packet,
  1632. .read_close = avi_read_close,
  1633. .read_seek = avi_read_seek,
  1634. .priv_class = &demuxer_class,
  1635. };