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.

1877 lines
64KB

  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 (avio_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 (avio_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 (avio_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. /* Exclude the "BottomUp" field from the palette */
  659. if (pal_src - st->codec->extradata >= 9 &&
  660. !memcmp(st->codec->extradata + st->codec->extradata_size - 9, "BottomUp", 9))
  661. pal_src -= 9;
  662. for (i = 0; i < pal_size / 4; i++)
  663. ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
  664. ast->has_pal = 1;
  665. }
  666. print_tag("video", tag1, 0);
  667. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  668. st->codec->codec_tag = tag1;
  669. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags,
  670. tag1);
  671. /* This is needed to get the pict type which is necessary
  672. * for generating correct pts. */
  673. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  674. if (st->codec->codec_tag == MKTAG('V', 'S', 'S', 'H'))
  675. st->need_parsing = AVSTREAM_PARSE_FULL;
  676. if (st->codec->codec_tag == 0 && st->codec->height > 0 &&
  677. st->codec->extradata_size < 1U << 30) {
  678. st->codec->extradata_size += 9;
  679. if ((ret = av_reallocp(&st->codec->extradata,
  680. st->codec->extradata_size +
  681. FF_INPUT_BUFFER_PADDING_SIZE)) < 0) {
  682. st->codec->extradata_size = 0;
  683. return ret;
  684. } else
  685. memcpy(st->codec->extradata + st->codec->extradata_size - 9,
  686. "BottomUp", 9);
  687. }
  688. st->codec->height = FFABS(st->codec->height);
  689. // avio_skip(pb, size - 5 * 4);
  690. break;
  691. case AVMEDIA_TYPE_AUDIO:
  692. ret = ff_get_wav_header(pb, st->codec, size);
  693. if (ret < 0)
  694. return ret;
  695. ast->dshow_block_align = st->codec->block_align;
  696. if (ast->sample_size && st->codec->block_align &&
  697. ast->sample_size != st->codec->block_align) {
  698. av_log(s,
  699. AV_LOG_WARNING,
  700. "sample size (%d) != block align (%d)\n",
  701. ast->sample_size,
  702. st->codec->block_align);
  703. ast->sample_size = st->codec->block_align;
  704. }
  705. /* 2-aligned
  706. * (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  707. if (size & 1)
  708. avio_skip(pb, 1);
  709. /* Force parsing as several audio frames can be in
  710. * one packet and timestamps refer to packet start. */
  711. st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
  712. /* ADTS header is in extradata, AAC without header must be
  713. * stored as exact frames. Parser not needed and it will
  714. * fail. */
  715. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  716. st->codec->extradata_size)
  717. st->need_parsing = AVSTREAM_PARSE_NONE;
  718. /* AVI files with Xan DPCM audio (wrongly) declare PCM
  719. * audio in the header but have Axan as stream_code_tag. */
  720. if (st->codec->stream_codec_tag == AV_RL32("Axan")) {
  721. st->codec->codec_id = AV_CODEC_ID_XAN_DPCM;
  722. st->codec->codec_tag = 0;
  723. ast->dshow_block_align = 0;
  724. }
  725. if (amv_file_format) {
  726. st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_AMV;
  727. ast->dshow_block_align = 0;
  728. }
  729. if (st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align <= 4 && ast->dshow_block_align) {
  730. av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
  731. ast->dshow_block_align = 0;
  732. }
  733. if (st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
  734. st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
  735. st->codec->codec_id == AV_CODEC_ID_MP3 && ast->dshow_block_align == 1152 && ast->sample_size == 1152) {
  736. av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
  737. ast->sample_size = 0;
  738. }
  739. break;
  740. case AVMEDIA_TYPE_SUBTITLE:
  741. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  742. st->request_probe= 1;
  743. avio_skip(pb, size);
  744. break;
  745. default:
  746. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  747. st->codec->codec_id = AV_CODEC_ID_NONE;
  748. st->codec->codec_tag = 0;
  749. avio_skip(pb, size);
  750. break;
  751. }
  752. }
  753. break;
  754. case MKTAG('s', 't', 'r', 'd'):
  755. if (stream_index >= (unsigned)s->nb_streams
  756. || s->streams[stream_index]->codec->extradata_size
  757. || s->streams[stream_index]->codec->codec_tag == MKTAG('H','2','6','4')) {
  758. avio_skip(pb, size);
  759. } else {
  760. uint64_t cur_pos = avio_tell(pb);
  761. if (cur_pos < list_end)
  762. size = FFMIN(size, list_end - cur_pos);
  763. st = s->streams[stream_index];
  764. if (size<(1<<30)) {
  765. if (ff_get_extradata(st->codec, pb, size) < 0)
  766. return AVERROR(ENOMEM);
  767. }
  768. if (st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
  769. avio_r8(pb);
  770. ret = avi_extract_stream_metadata(st);
  771. if (ret < 0) {
  772. av_log(s, AV_LOG_WARNING, "could not decoding EXIF data in stream header.\n");
  773. }
  774. }
  775. break;
  776. case MKTAG('i', 'n', 'd', 'x'):
  777. i = avio_tell(pb);
  778. if (pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
  779. avi->use_odml &&
  780. read_braindead_odml_indx(s, 0) < 0 &&
  781. (s->error_recognition & AV_EF_EXPLODE))
  782. goto fail;
  783. avio_seek(pb, i + size, SEEK_SET);
  784. break;
  785. case MKTAG('v', 'p', 'r', 'p'):
  786. if (stream_index < (unsigned)s->nb_streams && size > 9 * 4) {
  787. AVRational active, active_aspect;
  788. st = s->streams[stream_index];
  789. avio_rl32(pb);
  790. avio_rl32(pb);
  791. avio_rl32(pb);
  792. avio_rl32(pb);
  793. avio_rl32(pb);
  794. active_aspect.den = avio_rl16(pb);
  795. active_aspect.num = avio_rl16(pb);
  796. active.num = avio_rl32(pb);
  797. active.den = avio_rl32(pb);
  798. avio_rl32(pb); // nbFieldsPerFrame
  799. if (active_aspect.num && active_aspect.den &&
  800. active.num && active.den) {
  801. st->sample_aspect_ratio = av_div_q(active_aspect, active);
  802. av_dlog(s, "vprp %d/%d %d/%d\n",
  803. active_aspect.num, active_aspect.den,
  804. active.num, active.den);
  805. }
  806. size -= 9 * 4;
  807. }
  808. avio_skip(pb, size);
  809. break;
  810. case MKTAG('s', 't', 'r', 'n'):
  811. if (s->nb_streams) {
  812. ret = avi_read_tag(s, s->streams[s->nb_streams - 1], tag, size);
  813. if (ret < 0)
  814. return ret;
  815. break;
  816. }
  817. default:
  818. if (size > 1000000) {
  819. av_log(s, AV_LOG_ERROR,
  820. "Something went wrong during header parsing, "
  821. "I will ignore it and try to continue anyway.\n");
  822. if (s->error_recognition & AV_EF_EXPLODE)
  823. goto fail;
  824. avi->movi_list = avio_tell(pb) - 4;
  825. avi->movi_end = avi->fsize;
  826. goto end_of_header;
  827. }
  828. /* skip tag */
  829. size += (size & 1);
  830. avio_skip(pb, size);
  831. break;
  832. }
  833. }
  834. end_of_header:
  835. /* check stream number */
  836. if (stream_index != s->nb_streams - 1) {
  837. fail:
  838. return AVERROR_INVALIDDATA;
  839. }
  840. if (!avi->index_loaded && pb->seekable)
  841. avi_load_index(s);
  842. calculate_bitrate(s);
  843. avi->index_loaded |= 1;
  844. if ((ret = guess_ni_flag(s)) < 0)
  845. return ret;
  846. avi->non_interleaved |= ret | (s->flags & AVFMT_FLAG_SORT_DTS);
  847. dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
  848. if (dict_entry && !strcmp(dict_entry->value, "PotEncoder"))
  849. for (i = 0; i < s->nb_streams; i++) {
  850. AVStream *st = s->streams[i];
  851. if ( st->codec->codec_id == AV_CODEC_ID_MPEG1VIDEO
  852. || st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
  853. st->need_parsing = AVSTREAM_PARSE_FULL;
  854. }
  855. for (i = 0; i < s->nb_streams; i++) {
  856. AVStream *st = s->streams[i];
  857. if (st->nb_index_entries)
  858. break;
  859. }
  860. // DV-in-AVI cannot be non-interleaved, if set this must be
  861. // a mis-detection.
  862. if (avi->dv_demux)
  863. avi->non_interleaved = 0;
  864. if (i == s->nb_streams && avi->non_interleaved) {
  865. av_log(s, AV_LOG_WARNING,
  866. "Non-interleaved AVI without index, switching to interleaved\n");
  867. avi->non_interleaved = 0;
  868. }
  869. if (avi->non_interleaved) {
  870. av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
  871. clean_index(s);
  872. }
  873. ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
  874. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  875. return 0;
  876. }
  877. static int read_gab2_sub(AVStream *st, AVPacket *pkt)
  878. {
  879. if (pkt->size >= 7 &&
  880. pkt->size < INT_MAX - AVPROBE_PADDING_SIZE &&
  881. !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
  882. uint8_t desc[256];
  883. int score = AVPROBE_SCORE_EXTENSION, ret;
  884. AVIStream *ast = st->priv_data;
  885. AVInputFormat *sub_demuxer;
  886. AVRational time_base;
  887. int size;
  888. AVIOContext *pb = avio_alloc_context(pkt->data + 7,
  889. pkt->size - 7,
  890. 0, NULL, NULL, NULL, NULL);
  891. AVProbeData pd;
  892. unsigned int desc_len = avio_rl32(pb);
  893. if (desc_len > pb->buf_end - pb->buf_ptr)
  894. goto error;
  895. ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
  896. avio_skip(pb, desc_len - ret);
  897. if (*desc)
  898. av_dict_set(&st->metadata, "title", desc, 0);
  899. avio_rl16(pb); /* flags? */
  900. avio_rl32(pb); /* data size */
  901. size = pb->buf_end - pb->buf_ptr;
  902. pd = (AVProbeData) { .buf = av_mallocz(size + AVPROBE_PADDING_SIZE),
  903. .buf_size = size };
  904. if (!pd.buf)
  905. goto error;
  906. memcpy(pd.buf, pb->buf_ptr, size);
  907. sub_demuxer = av_probe_input_format2(&pd, 1, &score);
  908. av_freep(&pd.buf);
  909. if (!sub_demuxer)
  910. goto error;
  911. if (!(ast->sub_ctx = avformat_alloc_context()))
  912. goto error;
  913. ast->sub_ctx->pb = pb;
  914. if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
  915. ff_read_packet(ast->sub_ctx, &ast->sub_pkt);
  916. *st->codec = *ast->sub_ctx->streams[0]->codec;
  917. ast->sub_ctx->streams[0]->codec->extradata = NULL;
  918. time_base = ast->sub_ctx->streams[0]->time_base;
  919. avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
  920. }
  921. ast->sub_buffer = pkt->data;
  922. memset(pkt, 0, sizeof(*pkt));
  923. return 1;
  924. error:
  925. av_freep(&pb);
  926. }
  927. return 0;
  928. }
  929. static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
  930. AVPacket *pkt)
  931. {
  932. AVIStream *ast, *next_ast = next_st->priv_data;
  933. int64_t ts, next_ts, ts_min = INT64_MAX;
  934. AVStream *st, *sub_st = NULL;
  935. int i;
  936. next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
  937. AV_TIME_BASE_Q);
  938. for (i = 0; i < s->nb_streams; i++) {
  939. st = s->streams[i];
  940. ast = st->priv_data;
  941. if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
  942. ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
  943. if (ts <= next_ts && ts < ts_min) {
  944. ts_min = ts;
  945. sub_st = st;
  946. }
  947. }
  948. }
  949. if (sub_st) {
  950. ast = sub_st->priv_data;
  951. *pkt = ast->sub_pkt;
  952. pkt->stream_index = sub_st->index;
  953. if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
  954. ast->sub_pkt.data = NULL;
  955. }
  956. return sub_st;
  957. }
  958. static int get_stream_idx(const unsigned *d)
  959. {
  960. if (d[0] >= '0' && d[0] <= '9' &&
  961. d[1] >= '0' && d[1] <= '9') {
  962. return (d[0] - '0') * 10 + (d[1] - '0');
  963. } else {
  964. return 100; // invalid stream ID
  965. }
  966. }
  967. /**
  968. *
  969. * @param exit_early set to 1 to just gather packet position without making the changes needed to actually read & return the packet
  970. */
  971. static int avi_sync(AVFormatContext *s, int exit_early)
  972. {
  973. AVIContext *avi = s->priv_data;
  974. AVIOContext *pb = s->pb;
  975. int n;
  976. unsigned int d[8];
  977. unsigned int size;
  978. int64_t i, sync;
  979. start_sync:
  980. memset(d, -1, sizeof(d));
  981. for (i = sync = avio_tell(pb); !avio_feof(pb); i++) {
  982. int j;
  983. for (j = 0; j < 7; j++)
  984. d[j] = d[j + 1];
  985. d[7] = avio_r8(pb);
  986. size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
  987. n = get_stream_idx(d + 2);
  988. av_dlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
  989. d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
  990. if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
  991. continue;
  992. // parse ix##
  993. if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
  994. // parse JUNK
  995. (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
  996. (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')) {
  997. avio_skip(pb, size);
  998. goto start_sync;
  999. }
  1000. // parse stray LIST
  1001. if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
  1002. avio_skip(pb, 4);
  1003. goto start_sync;
  1004. }
  1005. n = avi->dv_demux ? 0 : get_stream_idx(d);
  1006. if (!((i - avi->last_pkt_pos) & 1) &&
  1007. get_stream_idx(d + 1) < s->nb_streams)
  1008. continue;
  1009. // detect ##ix chunk and skip
  1010. if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
  1011. avio_skip(pb, size);
  1012. goto start_sync;
  1013. }
  1014. // parse ##dc/##wb
  1015. if (n < s->nb_streams) {
  1016. AVStream *st;
  1017. AVIStream *ast;
  1018. st = s->streams[n];
  1019. ast = st->priv_data;
  1020. if (!ast) {
  1021. av_log(s, AV_LOG_WARNING, "Skipping foreign stream %d packet\n", n);
  1022. continue;
  1023. }
  1024. if (s->nb_streams >= 2) {
  1025. AVStream *st1 = s->streams[1];
  1026. AVIStream *ast1 = st1->priv_data;
  1027. // workaround for broken small-file-bug402.avi
  1028. if ( d[2] == 'w' && d[3] == 'b'
  1029. && n == 0
  1030. && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO
  1031. && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO
  1032. && ast->prefix == 'd'*256+'c'
  1033. && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
  1034. ) {
  1035. n = 1;
  1036. st = st1;
  1037. ast = ast1;
  1038. av_log(s, AV_LOG_WARNING,
  1039. "Invalid stream + prefix combination, assuming audio.\n");
  1040. }
  1041. }
  1042. if (!avi->dv_demux &&
  1043. ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
  1044. // FIXME: needs a little reordering
  1045. (st->discard >= AVDISCARD_NONKEY &&
  1046. !(pkt->flags & AV_PKT_FLAG_KEY)) */
  1047. || st->discard >= AVDISCARD_ALL)) {
  1048. if (!exit_early) {
  1049. ast->frame_offset += get_duration(ast, size);
  1050. avio_skip(pb, size);
  1051. goto start_sync;
  1052. }
  1053. }
  1054. if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
  1055. int k = avio_r8(pb);
  1056. int last = (k + avio_r8(pb) - 1) & 0xFF;
  1057. avio_rl16(pb); // flags
  1058. // b + (g << 8) + (r << 16);
  1059. for (; k <= last; k++)
  1060. ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
  1061. ast->has_pal = 1;
  1062. goto start_sync;
  1063. } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
  1064. d[2] < 128 && d[3] < 128) ||
  1065. d[2] * 256 + d[3] == ast->prefix /* ||
  1066. (d[2] == 'd' && d[3] == 'c') ||
  1067. (d[2] == 'w' && d[3] == 'b') */) {
  1068. if (exit_early)
  1069. return 0;
  1070. if (d[2] * 256 + d[3] == ast->prefix)
  1071. ast->prefix_count++;
  1072. else {
  1073. ast->prefix = d[2] * 256 + d[3];
  1074. ast->prefix_count = 0;
  1075. }
  1076. avi->stream_index = n;
  1077. ast->packet_size = size + 8;
  1078. ast->remaining = size;
  1079. if (size) {
  1080. uint64_t pos = avio_tell(pb) - 8;
  1081. if (!st->index_entries || !st->nb_index_entries ||
  1082. st->index_entries[st->nb_index_entries - 1].pos < pos) {
  1083. av_add_index_entry(st, pos, ast->frame_offset, size,
  1084. 0, AVINDEX_KEYFRAME);
  1085. }
  1086. }
  1087. return 0;
  1088. }
  1089. }
  1090. }
  1091. if (pb->error)
  1092. return pb->error;
  1093. return AVERROR_EOF;
  1094. }
  1095. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  1096. {
  1097. AVIContext *avi = s->priv_data;
  1098. AVIOContext *pb = s->pb;
  1099. int err;
  1100. #if FF_API_DESTRUCT_PACKET
  1101. void *dstr;
  1102. #endif
  1103. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  1104. int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
  1105. if (size >= 0)
  1106. return size;
  1107. else
  1108. goto resync;
  1109. }
  1110. if (avi->non_interleaved) {
  1111. int best_stream_index = 0;
  1112. AVStream *best_st = NULL;
  1113. AVIStream *best_ast;
  1114. int64_t best_ts = INT64_MAX;
  1115. int i;
  1116. for (i = 0; i < s->nb_streams; i++) {
  1117. AVStream *st = s->streams[i];
  1118. AVIStream *ast = st->priv_data;
  1119. int64_t ts = ast->frame_offset;
  1120. int64_t last_ts;
  1121. if (!st->nb_index_entries)
  1122. continue;
  1123. last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
  1124. if (!ast->remaining && ts > last_ts)
  1125. continue;
  1126. ts = av_rescale_q(ts, st->time_base,
  1127. (AVRational) { FFMAX(1, ast->sample_size),
  1128. AV_TIME_BASE });
  1129. av_dlog(s, "%"PRId64" %d/%d %"PRId64"\n", ts,
  1130. st->time_base.num, st->time_base.den, ast->frame_offset);
  1131. if (ts < best_ts) {
  1132. best_ts = ts;
  1133. best_st = st;
  1134. best_stream_index = i;
  1135. }
  1136. }
  1137. if (!best_st)
  1138. return AVERROR_EOF;
  1139. best_ast = best_st->priv_data;
  1140. best_ts = best_ast->frame_offset;
  1141. if (best_ast->remaining) {
  1142. i = av_index_search_timestamp(best_st,
  1143. best_ts,
  1144. AVSEEK_FLAG_ANY |
  1145. AVSEEK_FLAG_BACKWARD);
  1146. } else {
  1147. i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  1148. if (i >= 0)
  1149. best_ast->frame_offset = best_st->index_entries[i].timestamp;
  1150. }
  1151. if (i >= 0) {
  1152. int64_t pos = best_st->index_entries[i].pos;
  1153. pos += best_ast->packet_size - best_ast->remaining;
  1154. if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
  1155. return AVERROR_EOF;
  1156. av_assert0(best_ast->remaining <= best_ast->packet_size);
  1157. avi->stream_index = best_stream_index;
  1158. if (!best_ast->remaining)
  1159. best_ast->packet_size =
  1160. best_ast->remaining = best_st->index_entries[i].size;
  1161. }
  1162. else
  1163. return AVERROR_EOF;
  1164. }
  1165. resync:
  1166. if (avi->stream_index >= 0) {
  1167. AVStream *st = s->streams[avi->stream_index];
  1168. AVIStream *ast = st->priv_data;
  1169. int size, err;
  1170. if (get_subtitle_pkt(s, st, pkt))
  1171. return 0;
  1172. // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
  1173. if (ast->sample_size <= 1)
  1174. size = INT_MAX;
  1175. else if (ast->sample_size < 32)
  1176. // arbitrary multiplier to avoid tiny packets for raw PCM data
  1177. size = 1024 * ast->sample_size;
  1178. else
  1179. size = ast->sample_size;
  1180. if (size > ast->remaining)
  1181. size = ast->remaining;
  1182. avi->last_pkt_pos = avio_tell(pb);
  1183. err = av_get_packet(pb, pkt, size);
  1184. if (err < 0)
  1185. return err;
  1186. size = err;
  1187. if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2) {
  1188. uint8_t *pal;
  1189. pal = av_packet_new_side_data(pkt,
  1190. AV_PKT_DATA_PALETTE,
  1191. AVPALETTE_SIZE);
  1192. if (!pal) {
  1193. av_log(s, AV_LOG_ERROR,
  1194. "Failed to allocate data for palette\n");
  1195. } else {
  1196. memcpy(pal, ast->pal, AVPALETTE_SIZE);
  1197. ast->has_pal = 0;
  1198. }
  1199. }
  1200. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  1201. AVBufferRef *avbuf = pkt->buf;
  1202. #if FF_API_DESTRUCT_PACKET
  1203. FF_DISABLE_DEPRECATION_WARNINGS
  1204. dstr = pkt->destruct;
  1205. FF_ENABLE_DEPRECATION_WARNINGS
  1206. #endif
  1207. size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
  1208. pkt->data, pkt->size, pkt->pos);
  1209. #if FF_API_DESTRUCT_PACKET
  1210. FF_DISABLE_DEPRECATION_WARNINGS
  1211. pkt->destruct = dstr;
  1212. FF_ENABLE_DEPRECATION_WARNINGS
  1213. #endif
  1214. pkt->buf = avbuf;
  1215. pkt->flags |= AV_PKT_FLAG_KEY;
  1216. if (size < 0)
  1217. av_free_packet(pkt);
  1218. } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  1219. !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
  1220. ast->frame_offset++;
  1221. avi->stream_index = -1;
  1222. ast->remaining = 0;
  1223. goto resync;
  1224. } else {
  1225. /* XXX: How to handle B-frames in AVI? */
  1226. pkt->dts = ast->frame_offset;
  1227. // pkt->dts += ast->start;
  1228. if (ast->sample_size)
  1229. pkt->dts /= ast->sample_size;
  1230. av_dlog(s,
  1231. "dts:%"PRId64" offset:%"PRId64" %d/%d smpl_siz:%d "
  1232. "base:%d st:%d size:%d\n",
  1233. pkt->dts,
  1234. ast->frame_offset,
  1235. ast->scale,
  1236. ast->rate,
  1237. ast->sample_size,
  1238. AV_TIME_BASE,
  1239. avi->stream_index,
  1240. size);
  1241. pkt->stream_index = avi->stream_index;
  1242. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->index_entries) {
  1243. AVIndexEntry *e;
  1244. int index;
  1245. index = av_index_search_timestamp(st, ast->frame_offset, AVSEEK_FLAG_ANY);
  1246. e = &st->index_entries[index];
  1247. if (index >= 0 && e->timestamp == ast->frame_offset) {
  1248. if (index == st->nb_index_entries-1) {
  1249. int key=1;
  1250. int i;
  1251. uint32_t state=-1;
  1252. for (i=0; i<FFMIN(size,256); i++) {
  1253. if (st->codec->codec_id == AV_CODEC_ID_MPEG4) {
  1254. if (state == 0x1B6) {
  1255. key= !(pkt->data[i]&0xC0);
  1256. break;
  1257. }
  1258. }else
  1259. break;
  1260. state= (state<<8) + pkt->data[i];
  1261. }
  1262. if (!key)
  1263. e->flags &= ~AVINDEX_KEYFRAME;
  1264. }
  1265. if (e->flags & AVINDEX_KEYFRAME)
  1266. pkt->flags |= AV_PKT_FLAG_KEY;
  1267. }
  1268. } else {
  1269. pkt->flags |= AV_PKT_FLAG_KEY;
  1270. }
  1271. ast->frame_offset += get_duration(ast, pkt->size);
  1272. }
  1273. ast->remaining -= err;
  1274. if (!ast->remaining) {
  1275. avi->stream_index = -1;
  1276. ast->packet_size = 0;
  1277. }
  1278. if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
  1279. av_free_packet(pkt);
  1280. goto resync;
  1281. }
  1282. ast->seek_pos= 0;
  1283. if (!avi->non_interleaved && st->nb_index_entries>1 && avi->index_loaded>1) {
  1284. int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
  1285. if (avi->dts_max - dts > 2*AV_TIME_BASE) {
  1286. avi->non_interleaved= 1;
  1287. av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
  1288. }else if (avi->dts_max < dts)
  1289. avi->dts_max = dts;
  1290. }
  1291. return 0;
  1292. }
  1293. if ((err = avi_sync(s, 0)) < 0)
  1294. return err;
  1295. goto resync;
  1296. }
  1297. /* XXX: We make the implicit supposition that the positions are sorted
  1298. * for each stream. */
  1299. static int avi_read_idx1(AVFormatContext *s, int size)
  1300. {
  1301. AVIContext *avi = s->priv_data;
  1302. AVIOContext *pb = s->pb;
  1303. int nb_index_entries, i;
  1304. AVStream *st;
  1305. AVIStream *ast;
  1306. unsigned int index, tag, flags, pos, len, first_packet = 1;
  1307. unsigned last_pos = -1;
  1308. unsigned last_idx = -1;
  1309. int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
  1310. int anykey = 0;
  1311. nb_index_entries = size / 16;
  1312. if (nb_index_entries <= 0)
  1313. return AVERROR_INVALIDDATA;
  1314. idx1_pos = avio_tell(pb);
  1315. avio_seek(pb, avi->movi_list + 4, SEEK_SET);
  1316. if (avi_sync(s, 1) == 0)
  1317. first_packet_pos = avio_tell(pb) - 8;
  1318. avi->stream_index = -1;
  1319. avio_seek(pb, idx1_pos, SEEK_SET);
  1320. if (s->nb_streams == 1 && s->streams[0]->codec->codec_tag == AV_RL32("MMES")) {
  1321. first_packet_pos = 0;
  1322. data_offset = avi->movi_list;
  1323. }
  1324. /* Read the entries and sort them in each stream component. */
  1325. for (i = 0; i < nb_index_entries; i++) {
  1326. if (avio_feof(pb))
  1327. return -1;
  1328. tag = avio_rl32(pb);
  1329. flags = avio_rl32(pb);
  1330. pos = avio_rl32(pb);
  1331. len = avio_rl32(pb);
  1332. av_dlog(s, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  1333. i, tag, flags, pos, len);
  1334. index = ((tag & 0xff) - '0') * 10;
  1335. index += (tag >> 8 & 0xff) - '0';
  1336. if (index >= s->nb_streams)
  1337. continue;
  1338. st = s->streams[index];
  1339. ast = st->priv_data;
  1340. if (first_packet && first_packet_pos) {
  1341. data_offset = first_packet_pos - pos;
  1342. first_packet = 0;
  1343. }
  1344. pos += data_offset;
  1345. av_dlog(s, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
  1346. // even if we have only a single stream, we should
  1347. // switch to non-interleaved to get correct timestamps
  1348. if (last_pos == pos)
  1349. avi->non_interleaved = 1;
  1350. if (last_idx != pos && len) {
  1351. av_add_index_entry(st, pos, ast->cum_len, len, 0,
  1352. (flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  1353. last_idx= pos;
  1354. }
  1355. ast->cum_len += get_duration(ast, len);
  1356. last_pos = pos;
  1357. anykey |= flags&AVIIF_INDEX;
  1358. }
  1359. if (!anykey) {
  1360. for (index = 0; index < s->nb_streams; index++) {
  1361. st = s->streams[index];
  1362. if (st->nb_index_entries)
  1363. st->index_entries[0].flags |= AVINDEX_KEYFRAME;
  1364. }
  1365. }
  1366. return 0;
  1367. }
  1368. /* Scan the index and consider any file with streams more than
  1369. * 2 seconds or 64MB apart non-interleaved. */
  1370. static int check_stream_max_drift(AVFormatContext *s)
  1371. {
  1372. int64_t min_pos, pos;
  1373. int i;
  1374. int *idx = av_mallocz_array(s->nb_streams, sizeof(*idx));
  1375. if (!idx)
  1376. return AVERROR(ENOMEM);
  1377. for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) {
  1378. int64_t max_dts = INT64_MIN / 2;
  1379. int64_t min_dts = INT64_MAX / 2;
  1380. int64_t max_buffer = 0;
  1381. min_pos = INT64_MAX;
  1382. for (i = 0; i < s->nb_streams; i++) {
  1383. AVStream *st = s->streams[i];
  1384. AVIStream *ast = st->priv_data;
  1385. int n = st->nb_index_entries;
  1386. while (idx[i] < n && st->index_entries[idx[i]].pos < pos)
  1387. idx[i]++;
  1388. if (idx[i] < n) {
  1389. int64_t dts;
  1390. dts = av_rescale_q(st->index_entries[idx[i]].timestamp /
  1391. FFMAX(ast->sample_size, 1),
  1392. st->time_base, AV_TIME_BASE_Q);
  1393. min_dts = FFMIN(min_dts, dts);
  1394. min_pos = FFMIN(min_pos, st->index_entries[idx[i]].pos);
  1395. }
  1396. }
  1397. for (i = 0; i < s->nb_streams; i++) {
  1398. AVStream *st = s->streams[i];
  1399. AVIStream *ast = st->priv_data;
  1400. if (idx[i] && min_dts != INT64_MAX / 2) {
  1401. int64_t dts;
  1402. dts = av_rescale_q(st->index_entries[idx[i] - 1].timestamp /
  1403. FFMAX(ast->sample_size, 1),
  1404. st->time_base, AV_TIME_BASE_Q);
  1405. max_dts = FFMAX(max_dts, dts);
  1406. max_buffer = FFMAX(max_buffer,
  1407. av_rescale(dts - min_dts,
  1408. st->codec->bit_rate,
  1409. AV_TIME_BASE));
  1410. }
  1411. }
  1412. if (max_dts - min_dts > 2 * AV_TIME_BASE ||
  1413. max_buffer > 1024 * 1024 * 8 * 8) {
  1414. av_free(idx);
  1415. return 1;
  1416. }
  1417. }
  1418. av_free(idx);
  1419. return 0;
  1420. }
  1421. static int guess_ni_flag(AVFormatContext *s)
  1422. {
  1423. int i;
  1424. int64_t last_start = 0;
  1425. int64_t first_end = INT64_MAX;
  1426. int64_t oldpos = avio_tell(s->pb);
  1427. for (i = 0; i < s->nb_streams; i++) {
  1428. AVStream *st = s->streams[i];
  1429. int n = st->nb_index_entries;
  1430. unsigned int size;
  1431. if (n <= 0)
  1432. continue;
  1433. if (n >= 2) {
  1434. int64_t pos = st->index_entries[0].pos;
  1435. avio_seek(s->pb, pos + 4, SEEK_SET);
  1436. size = avio_rl32(s->pb);
  1437. if (pos + size > st->index_entries[1].pos)
  1438. last_start = INT64_MAX;
  1439. }
  1440. if (st->index_entries[0].pos > last_start)
  1441. last_start = st->index_entries[0].pos;
  1442. if (st->index_entries[n - 1].pos < first_end)
  1443. first_end = st->index_entries[n - 1].pos;
  1444. }
  1445. avio_seek(s->pb, oldpos, SEEK_SET);
  1446. if (last_start > first_end)
  1447. return 1;
  1448. return check_stream_max_drift(s);
  1449. }
  1450. static int avi_load_index(AVFormatContext *s)
  1451. {
  1452. AVIContext *avi = s->priv_data;
  1453. AVIOContext *pb = s->pb;
  1454. uint32_t tag, size;
  1455. int64_t pos = avio_tell(pb);
  1456. int64_t next;
  1457. int ret = -1;
  1458. if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
  1459. goto the_end; // maybe truncated file
  1460. av_dlog(s, "movi_end=0x%"PRIx64"\n", avi->movi_end);
  1461. for (;;) {
  1462. tag = avio_rl32(pb);
  1463. size = avio_rl32(pb);
  1464. if (avio_feof(pb))
  1465. break;
  1466. next = avio_tell(pb) + size + (size & 1);
  1467. av_dlog(s, "tag=%c%c%c%c size=0x%x\n",
  1468. tag & 0xff,
  1469. (tag >> 8) & 0xff,
  1470. (tag >> 16) & 0xff,
  1471. (tag >> 24) & 0xff,
  1472. size);
  1473. if (tag == MKTAG('i', 'd', 'x', '1') &&
  1474. avi_read_idx1(s, size) >= 0) {
  1475. avi->index_loaded=2;
  1476. ret = 0;
  1477. }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
  1478. uint32_t tag1 = avio_rl32(pb);
  1479. if (tag1 == MKTAG('I', 'N', 'F', 'O'))
  1480. ff_read_riff_info(s, size - 4);
  1481. }else if (!ret)
  1482. break;
  1483. if (avio_seek(pb, next, SEEK_SET) < 0)
  1484. break; // something is wrong here
  1485. }
  1486. the_end:
  1487. avio_seek(pb, pos, SEEK_SET);
  1488. return ret;
  1489. }
  1490. static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
  1491. {
  1492. AVIStream *ast2 = st2->priv_data;
  1493. int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
  1494. av_free_packet(&ast2->sub_pkt);
  1495. if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
  1496. avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
  1497. ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
  1498. }
  1499. static int avi_read_seek(AVFormatContext *s, int stream_index,
  1500. int64_t timestamp, int flags)
  1501. {
  1502. AVIContext *avi = s->priv_data;
  1503. AVStream *st;
  1504. int i, index;
  1505. int64_t pos, pos_min;
  1506. AVIStream *ast;
  1507. /* Does not matter which stream is requested dv in avi has the
  1508. * stream information in the first video stream.
  1509. */
  1510. if (avi->dv_demux)
  1511. stream_index = 0;
  1512. if (!avi->index_loaded) {
  1513. /* we only load the index on demand */
  1514. avi_load_index(s);
  1515. avi->index_loaded |= 1;
  1516. }
  1517. av_assert0(stream_index >= 0);
  1518. st = s->streams[stream_index];
  1519. ast = st->priv_data;
  1520. index = av_index_search_timestamp(st,
  1521. timestamp * FFMAX(ast->sample_size, 1),
  1522. flags);
  1523. if (index < 0) {
  1524. if (st->nb_index_entries > 0)
  1525. av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
  1526. timestamp * FFMAX(ast->sample_size, 1),
  1527. st->index_entries[0].timestamp,
  1528. st->index_entries[st->nb_index_entries - 1].timestamp);
  1529. return AVERROR_INVALIDDATA;
  1530. }
  1531. /* find the position */
  1532. pos = st->index_entries[index].pos;
  1533. timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
  1534. av_dlog(s, "XX %"PRId64" %d %"PRId64"\n",
  1535. timestamp, index, st->index_entries[index].timestamp);
  1536. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  1537. /* One and only one real stream for DV in AVI, and it has video */
  1538. /* offsets. Calling with other stream indexes should have failed */
  1539. /* the av_index_search_timestamp call above. */
  1540. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  1541. return -1;
  1542. /* Feed the DV video stream version of the timestamp to the */
  1543. /* DV demux so it can synthesize correct timestamps. */
  1544. ff_dv_offset_reset(avi->dv_demux, timestamp);
  1545. avi->stream_index = -1;
  1546. return 0;
  1547. }
  1548. pos_min = pos;
  1549. for (i = 0; i < s->nb_streams; i++) {
  1550. AVStream *st2 = s->streams[i];
  1551. AVIStream *ast2 = st2->priv_data;
  1552. ast2->packet_size =
  1553. ast2->remaining = 0;
  1554. if (ast2->sub_ctx) {
  1555. seek_subtitle(st, st2, timestamp);
  1556. continue;
  1557. }
  1558. if (st2->nb_index_entries <= 0)
  1559. continue;
  1560. // av_assert1(st2->codec->block_align);
  1561. av_assert0(fabs(av_q2d(st2->time_base) - ast2->scale / (double)ast2->rate) < av_q2d(st2->time_base) * 0.00000001);
  1562. index = av_index_search_timestamp(st2,
  1563. av_rescale_q(timestamp,
  1564. st->time_base,
  1565. st2->time_base) *
  1566. FFMAX(ast2->sample_size, 1),
  1567. flags |
  1568. AVSEEK_FLAG_BACKWARD |
  1569. (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
  1570. if (index < 0)
  1571. index = 0;
  1572. ast2->seek_pos = st2->index_entries[index].pos;
  1573. pos_min = FFMIN(pos_min,ast2->seek_pos);
  1574. }
  1575. for (i = 0; i < s->nb_streams; i++) {
  1576. AVStream *st2 = s->streams[i];
  1577. AVIStream *ast2 = st2->priv_data;
  1578. if (ast2->sub_ctx || st2->nb_index_entries <= 0)
  1579. continue;
  1580. index = av_index_search_timestamp(
  1581. st2,
  1582. av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
  1583. flags | AVSEEK_FLAG_BACKWARD | (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
  1584. if (index < 0)
  1585. index = 0;
  1586. while (!avi->non_interleaved && index>0 && st2->index_entries[index-1].pos >= pos_min)
  1587. index--;
  1588. ast2->frame_offset = st2->index_entries[index].timestamp;
  1589. }
  1590. /* do the seek */
  1591. if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
  1592. av_log(s, AV_LOG_ERROR, "Seek failed\n");
  1593. return -1;
  1594. }
  1595. avi->stream_index = -1;
  1596. avi->dts_max = INT_MIN;
  1597. return 0;
  1598. }
  1599. static int avi_read_close(AVFormatContext *s)
  1600. {
  1601. int i;
  1602. AVIContext *avi = s->priv_data;
  1603. for (i = 0; i < s->nb_streams; i++) {
  1604. AVStream *st = s->streams[i];
  1605. AVIStream *ast = st->priv_data;
  1606. if (ast) {
  1607. if (ast->sub_ctx) {
  1608. av_freep(&ast->sub_ctx->pb);
  1609. avformat_close_input(&ast->sub_ctx);
  1610. }
  1611. av_free(ast->sub_buffer);
  1612. av_free_packet(&ast->sub_pkt);
  1613. }
  1614. }
  1615. av_free(avi->dv_demux);
  1616. return 0;
  1617. }
  1618. static int avi_probe(AVProbeData *p)
  1619. {
  1620. int i;
  1621. /* check file header */
  1622. for (i = 0; avi_headers[i][0]; i++)
  1623. if (!memcmp(p->buf, avi_headers[i], 4) &&
  1624. !memcmp(p->buf + 8, avi_headers[i] + 4, 4))
  1625. return AVPROBE_SCORE_MAX;
  1626. return 0;
  1627. }
  1628. AVInputFormat ff_avi_demuxer = {
  1629. .name = "avi",
  1630. .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
  1631. .priv_data_size = sizeof(AVIContext),
  1632. .extensions = "avi",
  1633. .read_probe = avi_probe,
  1634. .read_header = avi_read_header,
  1635. .read_packet = avi_read_packet,
  1636. .read_close = avi_read_close,
  1637. .read_seek = avi_read_seek,
  1638. .priv_class = &demuxer_class,
  1639. };