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.

1898 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 && !avio_feof(s->pb)) {
  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 && !avio_feof(s->pb)) {
  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. int64_t bitrate;
  386. for (j = 0; j < st->nb_index_entries; j++)
  387. len += st->index_entries[j].size;
  388. if (st->nb_index_entries < 2 || st->codec->bit_rate > 0)
  389. continue;
  390. duration = st->index_entries[j-1].timestamp - st->index_entries[0].timestamp;
  391. bitrate = av_rescale(8*len, st->time_base.den, duration * st->time_base.num);
  392. if (bitrate <= INT_MAX && bitrate > 0) {
  393. st->codec->bit_rate = bitrate;
  394. }
  395. }
  396. return 1;
  397. }
  398. static int avi_read_header(AVFormatContext *s)
  399. {
  400. AVIContext *avi = s->priv_data;
  401. AVIOContext *pb = s->pb;
  402. unsigned int tag, tag1, handler;
  403. int codec_type, stream_index, frame_period;
  404. unsigned int size;
  405. int i;
  406. AVStream *st;
  407. AVIStream *ast = NULL;
  408. int avih_width = 0, avih_height = 0;
  409. int amv_file_format = 0;
  410. uint64_t list_end = 0;
  411. int ret;
  412. AVDictionaryEntry *dict_entry;
  413. avi->stream_index = -1;
  414. ret = get_riff(s, pb);
  415. if (ret < 0)
  416. return ret;
  417. av_log(avi, AV_LOG_DEBUG, "use odml:%d\n", avi->use_odml);
  418. avi->io_fsize = avi->fsize = avio_size(pb);
  419. if (avi->fsize <= 0 || avi->fsize < avi->riff_end)
  420. avi->fsize = avi->riff_end == 8 ? INT64_MAX : avi->riff_end;
  421. /* first list tag */
  422. stream_index = -1;
  423. codec_type = -1;
  424. frame_period = 0;
  425. for (;;) {
  426. if (avio_feof(pb))
  427. goto fail;
  428. tag = avio_rl32(pb);
  429. size = avio_rl32(pb);
  430. print_tag("tag", tag, size);
  431. switch (tag) {
  432. case MKTAG('L', 'I', 'S', 'T'):
  433. list_end = avio_tell(pb) + size;
  434. /* Ignored, except at start of video packets. */
  435. tag1 = avio_rl32(pb);
  436. print_tag("list", tag1, 0);
  437. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  438. avi->movi_list = avio_tell(pb) - 4;
  439. if (size)
  440. avi->movi_end = avi->movi_list + size + (size & 1);
  441. else
  442. avi->movi_end = avi->fsize;
  443. av_dlog(NULL, "movi end=%"PRIx64"\n", avi->movi_end);
  444. goto end_of_header;
  445. } else if (tag1 == MKTAG('I', 'N', 'F', 'O'))
  446. ff_read_riff_info(s, size - 4);
  447. else if (tag1 == MKTAG('n', 'c', 'd', 't'))
  448. avi_read_nikon(s, list_end);
  449. break;
  450. case MKTAG('I', 'D', 'I', 'T'):
  451. {
  452. unsigned char date[64] = { 0 };
  453. size += (size & 1);
  454. size -= avio_read(pb, date, FFMIN(size, sizeof(date) - 1));
  455. avio_skip(pb, size);
  456. avi_metadata_creation_time(&s->metadata, date);
  457. break;
  458. }
  459. case MKTAG('d', 'm', 'l', 'h'):
  460. avi->is_odml = 1;
  461. avio_skip(pb, size + (size & 1));
  462. break;
  463. case MKTAG('a', 'm', 'v', 'h'):
  464. amv_file_format = 1;
  465. case MKTAG('a', 'v', 'i', 'h'):
  466. /* AVI header */
  467. /* using frame_period is bad idea */
  468. frame_period = avio_rl32(pb);
  469. avio_rl32(pb); /* max. bytes per second */
  470. avio_rl32(pb);
  471. avi->non_interleaved |= avio_rl32(pb) & AVIF_MUSTUSEINDEX;
  472. avio_skip(pb, 2 * 4);
  473. avio_rl32(pb);
  474. avio_rl32(pb);
  475. avih_width = avio_rl32(pb);
  476. avih_height = avio_rl32(pb);
  477. avio_skip(pb, size - 10 * 4);
  478. break;
  479. case MKTAG('s', 't', 'r', 'h'):
  480. /* stream header */
  481. tag1 = avio_rl32(pb);
  482. handler = avio_rl32(pb); /* codec tag */
  483. if (tag1 == MKTAG('p', 'a', 'd', 's')) {
  484. avio_skip(pb, size - 8);
  485. break;
  486. } else {
  487. stream_index++;
  488. st = avformat_new_stream(s, NULL);
  489. if (!st)
  490. goto fail;
  491. st->id = stream_index;
  492. ast = av_mallocz(sizeof(AVIStream));
  493. if (!ast)
  494. goto fail;
  495. st->priv_data = ast;
  496. }
  497. if (amv_file_format)
  498. tag1 = stream_index ? MKTAG('a', 'u', 'd', 's')
  499. : MKTAG('v', 'i', 'd', 's');
  500. print_tag("strh", tag1, -1);
  501. if (tag1 == MKTAG('i', 'a', 'v', 's') ||
  502. tag1 == MKTAG('i', 'v', 'a', 's')) {
  503. int64_t dv_dur;
  504. /* After some consideration -- I don't think we
  505. * have to support anything but DV in type1 AVIs. */
  506. if (s->nb_streams != 1)
  507. goto fail;
  508. if (handler != MKTAG('d', 'v', 's', 'd') &&
  509. handler != MKTAG('d', 'v', 'h', 'd') &&
  510. handler != MKTAG('d', 'v', 's', 'l'))
  511. goto fail;
  512. ast = s->streams[0]->priv_data;
  513. av_freep(&s->streams[0]->codec->extradata);
  514. av_freep(&s->streams[0]->codec);
  515. if (s->streams[0]->info)
  516. av_freep(&s->streams[0]->info->duration_error);
  517. av_freep(&s->streams[0]->info);
  518. av_freep(&s->streams[0]);
  519. s->nb_streams = 0;
  520. if (CONFIG_DV_DEMUXER) {
  521. avi->dv_demux = avpriv_dv_init_demux(s);
  522. if (!avi->dv_demux)
  523. goto fail;
  524. } else
  525. goto fail;
  526. s->streams[0]->priv_data = ast;
  527. avio_skip(pb, 3 * 4);
  528. ast->scale = avio_rl32(pb);
  529. ast->rate = avio_rl32(pb);
  530. avio_skip(pb, 4); /* start time */
  531. dv_dur = avio_rl32(pb);
  532. if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
  533. dv_dur *= AV_TIME_BASE;
  534. s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
  535. }
  536. /* else, leave duration alone; timing estimation in utils.c
  537. * will make a guess based on bitrate. */
  538. stream_index = s->nb_streams - 1;
  539. avio_skip(pb, size - 9 * 4);
  540. break;
  541. }
  542. av_assert0(stream_index < s->nb_streams);
  543. st->codec->stream_codec_tag = handler;
  544. avio_rl32(pb); /* flags */
  545. avio_rl16(pb); /* priority */
  546. avio_rl16(pb); /* language */
  547. avio_rl32(pb); /* initial frame */
  548. ast->scale = avio_rl32(pb);
  549. ast->rate = avio_rl32(pb);
  550. if (!(ast->scale && ast->rate)) {
  551. av_log(s, AV_LOG_WARNING,
  552. "scale/rate is %"PRIu32"/%"PRIu32" which is invalid. "
  553. "(This file has been generated by broken software.)\n",
  554. ast->scale,
  555. ast->rate);
  556. if (frame_period) {
  557. ast->rate = 1000000;
  558. ast->scale = frame_period;
  559. } else {
  560. ast->rate = 25;
  561. ast->scale = 1;
  562. }
  563. }
  564. avpriv_set_pts_info(st, 64, ast->scale, ast->rate);
  565. ast->cum_len = avio_rl32(pb); /* start */
  566. st->nb_frames = avio_rl32(pb);
  567. st->start_time = 0;
  568. avio_rl32(pb); /* buffer size */
  569. avio_rl32(pb); /* quality */
  570. if (ast->cum_len*ast->scale/ast->rate > 3600) {
  571. av_log(s, AV_LOG_ERROR, "crazy start time, iam scared, giving up\n");
  572. return AVERROR_INVALIDDATA;
  573. }
  574. ast->sample_size = avio_rl32(pb); /* sample ssize */
  575. ast->cum_len *= FFMAX(1, ast->sample_size);
  576. av_dlog(s, "%"PRIu32" %"PRIu32" %d\n",
  577. ast->rate, ast->scale, ast->sample_size);
  578. switch (tag1) {
  579. case MKTAG('v', 'i', 'd', 's'):
  580. codec_type = AVMEDIA_TYPE_VIDEO;
  581. ast->sample_size = 0;
  582. st->avg_frame_rate = av_inv_q(st->time_base);
  583. break;
  584. case MKTAG('a', 'u', 'd', 's'):
  585. codec_type = AVMEDIA_TYPE_AUDIO;
  586. break;
  587. case MKTAG('t', 'x', 't', 's'):
  588. codec_type = AVMEDIA_TYPE_SUBTITLE;
  589. break;
  590. case MKTAG('d', 'a', 't', 's'):
  591. codec_type = AVMEDIA_TYPE_DATA;
  592. break;
  593. default:
  594. av_log(s, AV_LOG_INFO, "unknown stream type %X\n", tag1);
  595. }
  596. if (ast->sample_size < 0) {
  597. if (s->error_recognition & AV_EF_EXPLODE) {
  598. av_log(s, AV_LOG_ERROR,
  599. "Invalid sample_size %d at stream %d\n",
  600. ast->sample_size,
  601. stream_index);
  602. goto fail;
  603. }
  604. av_log(s, AV_LOG_WARNING,
  605. "Invalid sample_size %d at stream %d "
  606. "setting it to 0\n",
  607. ast->sample_size,
  608. stream_index);
  609. ast->sample_size = 0;
  610. }
  611. if (ast->sample_size == 0) {
  612. st->duration = st->nb_frames;
  613. if (st->duration > 0 && avi->io_fsize > 0 && avi->riff_end > avi->io_fsize) {
  614. av_log(s, AV_LOG_DEBUG, "File is truncated adjusting duration\n");
  615. st->duration = av_rescale(st->duration, avi->io_fsize, avi->riff_end);
  616. }
  617. }
  618. ast->frame_offset = ast->cum_len;
  619. avio_skip(pb, size - 12 * 4);
  620. break;
  621. case MKTAG('s', 't', 'r', 'f'):
  622. /* stream header */
  623. if (!size)
  624. break;
  625. if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
  626. avio_skip(pb, size);
  627. } else {
  628. uint64_t cur_pos = avio_tell(pb);
  629. unsigned esize;
  630. if (cur_pos < list_end)
  631. size = FFMIN(size, list_end - cur_pos);
  632. st = s->streams[stream_index];
  633. if (st->codec->codec_type != AVMEDIA_TYPE_UNKNOWN) {
  634. avio_skip(pb, size);
  635. break;
  636. }
  637. switch (codec_type) {
  638. case AVMEDIA_TYPE_VIDEO:
  639. if (amv_file_format) {
  640. st->codec->width = avih_width;
  641. st->codec->height = avih_height;
  642. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  643. st->codec->codec_id = AV_CODEC_ID_AMV;
  644. avio_skip(pb, size);
  645. break;
  646. }
  647. tag1 = ff_get_bmp_header(pb, st, &esize);
  648. if (tag1 == MKTAG('D', 'X', 'S', 'B') ||
  649. tag1 == MKTAG('D', 'X', 'S', 'A')) {
  650. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  651. st->codec->codec_tag = tag1;
  652. st->codec->codec_id = AV_CODEC_ID_XSUB;
  653. break;
  654. }
  655. if (size > 10 * 4 && size < (1 << 30) && size < avi->fsize) {
  656. if (esize == size-1 && (esize&1)) {
  657. st->codec->extradata_size = esize - 10 * 4;
  658. } else
  659. st->codec->extradata_size = size - 10 * 4;
  660. if (ff_get_extradata(st->codec, pb, st->codec->extradata_size) < 0)
  661. return AVERROR(ENOMEM);
  662. }
  663. // FIXME: check if the encoder really did this correctly
  664. if (st->codec->extradata_size & 1)
  665. avio_r8(pb);
  666. /* Extract palette from extradata if bpp <= 8.
  667. * This code assumes that extradata contains only palette.
  668. * This is true for all paletted codecs implemented in
  669. * FFmpeg. */
  670. if (st->codec->extradata_size &&
  671. (st->codec->bits_per_coded_sample <= 8)) {
  672. int pal_size = (1 << st->codec->bits_per_coded_sample) << 2;
  673. const uint8_t *pal_src;
  674. pal_size = FFMIN(pal_size, st->codec->extradata_size);
  675. pal_src = st->codec->extradata +
  676. st->codec->extradata_size - pal_size;
  677. for (i = 0; i < pal_size / 4; i++)
  678. ast->pal[i] = 0xFFU<<24 | AV_RL32(pal_src+4*i);
  679. ast->has_pal = 1;
  680. }
  681. print_tag("video", tag1, 0);
  682. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  683. st->codec->codec_tag = tag1;
  684. st->codec->codec_id = ff_codec_get_id(ff_codec_bmp_tags,
  685. tag1);
  686. /* This is needed to get the pict type which is necessary
  687. * for generating correct pts. */
  688. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  689. if (st->codec->codec_tag == MKTAG('V', 'S', 'S', 'H'))
  690. st->need_parsing = AVSTREAM_PARSE_FULL;
  691. if (st->codec->codec_tag == 0 && st->codec->height > 0 &&
  692. st->codec->extradata_size < 1U << 30) {
  693. st->codec->extradata_size += 9;
  694. if ((ret = av_reallocp(&st->codec->extradata,
  695. st->codec->extradata_size +
  696. FF_INPUT_BUFFER_PADDING_SIZE)) < 0) {
  697. st->codec->extradata_size = 0;
  698. return ret;
  699. } else
  700. memcpy(st->codec->extradata + st->codec->extradata_size - 9,
  701. "BottomUp", 9);
  702. }
  703. st->codec->height = FFABS(st->codec->height);
  704. // avio_skip(pb, size - 5 * 4);
  705. break;
  706. case AVMEDIA_TYPE_AUDIO:
  707. ret = ff_get_wav_header(s, pb, st->codec, size);
  708. if (ret < 0)
  709. return ret;
  710. ast->dshow_block_align = st->codec->block_align;
  711. if (ast->sample_size && st->codec->block_align &&
  712. ast->sample_size != st->codec->block_align) {
  713. av_log(s,
  714. AV_LOG_WARNING,
  715. "sample size (%d) != block align (%d)\n",
  716. ast->sample_size,
  717. st->codec->block_align);
  718. ast->sample_size = st->codec->block_align;
  719. }
  720. /* 2-aligned
  721. * (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  722. if (size & 1)
  723. avio_skip(pb, 1);
  724. /* Force parsing as several audio frames can be in
  725. * one packet and timestamps refer to packet start. */
  726. st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
  727. /* ADTS header is in extradata, AAC without header must be
  728. * stored as exact frames. Parser not needed and it will
  729. * fail. */
  730. if (st->codec->codec_id == AV_CODEC_ID_AAC &&
  731. st->codec->extradata_size)
  732. st->need_parsing = AVSTREAM_PARSE_NONE;
  733. /* AVI files with Xan DPCM audio (wrongly) declare PCM
  734. * audio in the header but have Axan as stream_code_tag. */
  735. if (st->codec->stream_codec_tag == AV_RL32("Axan")) {
  736. st->codec->codec_id = AV_CODEC_ID_XAN_DPCM;
  737. st->codec->codec_tag = 0;
  738. ast->dshow_block_align = 0;
  739. }
  740. if (amv_file_format) {
  741. st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_AMV;
  742. ast->dshow_block_align = 0;
  743. }
  744. if (st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align <= 4 && ast->dshow_block_align) {
  745. av_log(s, AV_LOG_DEBUG, "overriding invalid dshow_block_align of %d\n", ast->dshow_block_align);
  746. ast->dshow_block_align = 0;
  747. }
  748. if (st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 1024 && ast->sample_size == 1024 ||
  749. st->codec->codec_id == AV_CODEC_ID_AAC && ast->dshow_block_align == 4096 && ast->sample_size == 4096 ||
  750. st->codec->codec_id == AV_CODEC_ID_MP3 && ast->dshow_block_align == 1152 && ast->sample_size == 1152) {
  751. av_log(s, AV_LOG_DEBUG, "overriding sample_size\n");
  752. ast->sample_size = 0;
  753. }
  754. break;
  755. case AVMEDIA_TYPE_SUBTITLE:
  756. st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  757. st->request_probe= 1;
  758. avio_skip(pb, size);
  759. break;
  760. default:
  761. st->codec->codec_type = AVMEDIA_TYPE_DATA;
  762. st->codec->codec_id = AV_CODEC_ID_NONE;
  763. st->codec->codec_tag = 0;
  764. avio_skip(pb, size);
  765. break;
  766. }
  767. }
  768. break;
  769. case MKTAG('s', 't', 'r', 'd'):
  770. if (stream_index >= (unsigned)s->nb_streams
  771. || s->streams[stream_index]->codec->extradata_size
  772. || s->streams[stream_index]->codec->codec_tag == MKTAG('H','2','6','4')) {
  773. avio_skip(pb, size);
  774. } else {
  775. uint64_t cur_pos = avio_tell(pb);
  776. if (cur_pos < list_end)
  777. size = FFMIN(size, list_end - cur_pos);
  778. st = s->streams[stream_index];
  779. if (size<(1<<30)) {
  780. if (ff_get_extradata(st->codec, pb, size) < 0)
  781. return AVERROR(ENOMEM);
  782. }
  783. if (st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
  784. avio_r8(pb);
  785. ret = avi_extract_stream_metadata(st);
  786. if (ret < 0) {
  787. av_log(s, AV_LOG_WARNING, "could not decoding EXIF data in stream header.\n");
  788. }
  789. }
  790. break;
  791. case MKTAG('i', 'n', 'd', 'x'):
  792. i = avio_tell(pb);
  793. if (pb->seekable && !(s->flags & AVFMT_FLAG_IGNIDX) &&
  794. avi->use_odml &&
  795. read_braindead_odml_indx(s, 0) < 0 &&
  796. (s->error_recognition & AV_EF_EXPLODE))
  797. goto fail;
  798. avio_seek(pb, i + size, SEEK_SET);
  799. break;
  800. case MKTAG('v', 'p', 'r', 'p'):
  801. if (stream_index < (unsigned)s->nb_streams && size > 9 * 4) {
  802. AVRational active, active_aspect;
  803. st = s->streams[stream_index];
  804. avio_rl32(pb);
  805. avio_rl32(pb);
  806. avio_rl32(pb);
  807. avio_rl32(pb);
  808. avio_rl32(pb);
  809. active_aspect.den = avio_rl16(pb);
  810. active_aspect.num = avio_rl16(pb);
  811. active.num = avio_rl32(pb);
  812. active.den = avio_rl32(pb);
  813. avio_rl32(pb); // nbFieldsPerFrame
  814. if (active_aspect.num && active_aspect.den &&
  815. active.num && active.den) {
  816. st->sample_aspect_ratio = av_div_q(active_aspect, active);
  817. av_dlog(s, "vprp %d/%d %d/%d\n",
  818. active_aspect.num, active_aspect.den,
  819. active.num, active.den);
  820. }
  821. size -= 9 * 4;
  822. }
  823. avio_skip(pb, size);
  824. break;
  825. case MKTAG('s', 't', 'r', 'n'):
  826. if (s->nb_streams) {
  827. ret = avi_read_tag(s, s->streams[s->nb_streams - 1], tag, size);
  828. if (ret < 0)
  829. return ret;
  830. break;
  831. }
  832. default:
  833. if (size > 1000000) {
  834. av_log(s, AV_LOG_ERROR,
  835. "Something went wrong during header parsing, "
  836. "I will ignore it and try to continue anyway.\n");
  837. if (s->error_recognition & AV_EF_EXPLODE)
  838. goto fail;
  839. avi->movi_list = avio_tell(pb) - 4;
  840. avi->movi_end = avi->fsize;
  841. goto end_of_header;
  842. }
  843. /* skip tag */
  844. size += (size & 1);
  845. avio_skip(pb, size);
  846. break;
  847. }
  848. }
  849. end_of_header:
  850. /* check stream number */
  851. if (stream_index != s->nb_streams - 1) {
  852. fail:
  853. return AVERROR_INVALIDDATA;
  854. }
  855. if (!avi->index_loaded && pb->seekable)
  856. avi_load_index(s);
  857. calculate_bitrate(s);
  858. avi->index_loaded |= 1;
  859. if ((ret = guess_ni_flag(s)) < 0)
  860. return ret;
  861. avi->non_interleaved |= ret | (s->flags & AVFMT_FLAG_SORT_DTS);
  862. dict_entry = av_dict_get(s->metadata, "ISFT", NULL, 0);
  863. if (dict_entry && !strcmp(dict_entry->value, "PotEncoder"))
  864. for (i = 0; i < s->nb_streams; i++) {
  865. AVStream *st = s->streams[i];
  866. if ( st->codec->codec_id == AV_CODEC_ID_MPEG1VIDEO
  867. || st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
  868. st->need_parsing = AVSTREAM_PARSE_FULL;
  869. }
  870. for (i = 0; i < s->nb_streams; i++) {
  871. AVStream *st = s->streams[i];
  872. if (st->nb_index_entries)
  873. break;
  874. }
  875. // DV-in-AVI cannot be non-interleaved, if set this must be
  876. // a mis-detection.
  877. if (avi->dv_demux)
  878. avi->non_interleaved = 0;
  879. if (i == s->nb_streams && avi->non_interleaved) {
  880. av_log(s, AV_LOG_WARNING,
  881. "Non-interleaved AVI without index, switching to interleaved\n");
  882. avi->non_interleaved = 0;
  883. }
  884. if (avi->non_interleaved) {
  885. av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
  886. clean_index(s);
  887. }
  888. ff_metadata_conv_ctx(s, NULL, avi_metadata_conv);
  889. ff_metadata_conv_ctx(s, NULL, ff_riff_info_conv);
  890. return 0;
  891. }
  892. static int read_gab2_sub(AVStream *st, AVPacket *pkt)
  893. {
  894. if (pkt->size >= 7 &&
  895. pkt->size < INT_MAX - AVPROBE_PADDING_SIZE &&
  896. !strcmp(pkt->data, "GAB2") && AV_RL16(pkt->data + 5) == 2) {
  897. uint8_t desc[256];
  898. int score = AVPROBE_SCORE_EXTENSION, ret;
  899. AVIStream *ast = st->priv_data;
  900. AVInputFormat *sub_demuxer;
  901. AVRational time_base;
  902. int size;
  903. AVIOContext *pb = avio_alloc_context(pkt->data + 7,
  904. pkt->size - 7,
  905. 0, NULL, NULL, NULL, NULL);
  906. AVProbeData pd;
  907. unsigned int desc_len = avio_rl32(pb);
  908. if (desc_len > pb->buf_end - pb->buf_ptr)
  909. goto error;
  910. ret = avio_get_str16le(pb, desc_len, desc, sizeof(desc));
  911. avio_skip(pb, desc_len - ret);
  912. if (*desc)
  913. av_dict_set(&st->metadata, "title", desc, 0);
  914. avio_rl16(pb); /* flags? */
  915. avio_rl32(pb); /* data size */
  916. size = pb->buf_end - pb->buf_ptr;
  917. pd = (AVProbeData) { .buf = av_mallocz(size + AVPROBE_PADDING_SIZE),
  918. .buf_size = size };
  919. if (!pd.buf)
  920. goto error;
  921. memcpy(pd.buf, pb->buf_ptr, size);
  922. sub_demuxer = av_probe_input_format2(&pd, 1, &score);
  923. av_freep(&pd.buf);
  924. if (!sub_demuxer)
  925. goto error;
  926. if (!(ast->sub_ctx = avformat_alloc_context()))
  927. goto error;
  928. ast->sub_ctx->pb = pb;
  929. if (!avformat_open_input(&ast->sub_ctx, "", sub_demuxer, NULL)) {
  930. ff_read_packet(ast->sub_ctx, &ast->sub_pkt);
  931. *st->codec = *ast->sub_ctx->streams[0]->codec;
  932. ast->sub_ctx->streams[0]->codec->extradata = NULL;
  933. time_base = ast->sub_ctx->streams[0]->time_base;
  934. avpriv_set_pts_info(st, 64, time_base.num, time_base.den);
  935. }
  936. ast->sub_buffer = pkt->data;
  937. memset(pkt, 0, sizeof(*pkt));
  938. return 1;
  939. error:
  940. av_freep(&pb);
  941. }
  942. return 0;
  943. }
  944. static AVStream *get_subtitle_pkt(AVFormatContext *s, AVStream *next_st,
  945. AVPacket *pkt)
  946. {
  947. AVIStream *ast, *next_ast = next_st->priv_data;
  948. int64_t ts, next_ts, ts_min = INT64_MAX;
  949. AVStream *st, *sub_st = NULL;
  950. int i;
  951. next_ts = av_rescale_q(next_ast->frame_offset, next_st->time_base,
  952. AV_TIME_BASE_Q);
  953. for (i = 0; i < s->nb_streams; i++) {
  954. st = s->streams[i];
  955. ast = st->priv_data;
  956. if (st->discard < AVDISCARD_ALL && ast && ast->sub_pkt.data) {
  957. ts = av_rescale_q(ast->sub_pkt.dts, st->time_base, AV_TIME_BASE_Q);
  958. if (ts <= next_ts && ts < ts_min) {
  959. ts_min = ts;
  960. sub_st = st;
  961. }
  962. }
  963. }
  964. if (sub_st) {
  965. ast = sub_st->priv_data;
  966. *pkt = ast->sub_pkt;
  967. pkt->stream_index = sub_st->index;
  968. if (ff_read_packet(ast->sub_ctx, &ast->sub_pkt) < 0)
  969. ast->sub_pkt.data = NULL;
  970. }
  971. return sub_st;
  972. }
  973. static int get_stream_idx(unsigned *d)
  974. {
  975. if (d[0] >= '0' && d[0] <= '9' &&
  976. d[1] >= '0' && d[1] <= '9') {
  977. return (d[0] - '0') * 10 + (d[1] - '0');
  978. } else {
  979. return 100; // invalid stream ID
  980. }
  981. }
  982. /**
  983. *
  984. * @param exit_early set to 1 to just gather packet position without making the changes needed to actually read & return the packet
  985. */
  986. static int avi_sync(AVFormatContext *s, int exit_early)
  987. {
  988. AVIContext *avi = s->priv_data;
  989. AVIOContext *pb = s->pb;
  990. int n;
  991. unsigned int d[8];
  992. unsigned int size;
  993. int64_t i, sync;
  994. start_sync:
  995. memset(d, -1, sizeof(d));
  996. for (i = sync = avio_tell(pb); !avio_feof(pb); i++) {
  997. int j;
  998. for (j = 0; j < 7; j++)
  999. d[j] = d[j + 1];
  1000. d[7] = avio_r8(pb);
  1001. size = d[4] + (d[5] << 8) + (d[6] << 16) + (d[7] << 24);
  1002. n = get_stream_idx(d + 2);
  1003. av_dlog(s, "%X %X %X %X %X %X %X %X %"PRId64" %u %d\n",
  1004. d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
  1005. if (i*(avi->io_fsize>0) + (uint64_t)size > avi->fsize || d[0] > 127)
  1006. continue;
  1007. // parse ix##
  1008. if ((d[0] == 'i' && d[1] == 'x' && n < s->nb_streams) ||
  1009. // parse JUNK
  1010. (d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K') ||
  1011. (d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')) {
  1012. avio_skip(pb, size);
  1013. goto start_sync;
  1014. }
  1015. // parse stray LIST
  1016. if (d[0] == 'L' && d[1] == 'I' && d[2] == 'S' && d[3] == 'T') {
  1017. avio_skip(pb, 4);
  1018. goto start_sync;
  1019. }
  1020. n = get_stream_idx(d);
  1021. if (!((i - avi->last_pkt_pos) & 1) &&
  1022. get_stream_idx(d + 1) < s->nb_streams)
  1023. continue;
  1024. // detect ##ix chunk and skip
  1025. if (d[2] == 'i' && d[3] == 'x' && n < s->nb_streams) {
  1026. avio_skip(pb, size);
  1027. goto start_sync;
  1028. }
  1029. if (avi->dv_demux && n != 0)
  1030. continue;
  1031. // parse ##dc/##wb
  1032. if (n < s->nb_streams) {
  1033. AVStream *st;
  1034. AVIStream *ast;
  1035. st = s->streams[n];
  1036. ast = st->priv_data;
  1037. if (!ast) {
  1038. av_log(s, AV_LOG_WARNING, "Skipping foreign stream %d packet\n", n);
  1039. continue;
  1040. }
  1041. if (s->nb_streams >= 2) {
  1042. AVStream *st1 = s->streams[1];
  1043. AVIStream *ast1 = st1->priv_data;
  1044. // workaround for broken small-file-bug402.avi
  1045. if ( d[2] == 'w' && d[3] == 'b'
  1046. && n == 0
  1047. && st ->codec->codec_type == AVMEDIA_TYPE_VIDEO
  1048. && st1->codec->codec_type == AVMEDIA_TYPE_AUDIO
  1049. && ast->prefix == 'd'*256+'c'
  1050. && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
  1051. ) {
  1052. n = 1;
  1053. st = st1;
  1054. ast = ast1;
  1055. av_log(s, AV_LOG_WARNING,
  1056. "Invalid stream + prefix combination, assuming audio.\n");
  1057. }
  1058. }
  1059. if (!avi->dv_demux &&
  1060. ((st->discard >= AVDISCARD_DEFAULT && size == 0) /* ||
  1061. // FIXME: needs a little reordering
  1062. (st->discard >= AVDISCARD_NONKEY &&
  1063. !(pkt->flags & AV_PKT_FLAG_KEY)) */
  1064. || st->discard >= AVDISCARD_ALL)) {
  1065. if (!exit_early) {
  1066. ast->frame_offset += get_duration(ast, size);
  1067. avio_skip(pb, size);
  1068. goto start_sync;
  1069. }
  1070. }
  1071. if (d[2] == 'p' && d[3] == 'c' && size <= 4 * 256 + 4) {
  1072. int k = avio_r8(pb);
  1073. int last = (k + avio_r8(pb) - 1) & 0xFF;
  1074. avio_rl16(pb); // flags
  1075. // b + (g << 8) + (r << 16);
  1076. for (; k <= last; k++)
  1077. ast->pal[k] = 0xFFU<<24 | avio_rb32(pb)>>8;
  1078. ast->has_pal = 1;
  1079. goto start_sync;
  1080. } else if (((ast->prefix_count < 5 || sync + 9 > i) &&
  1081. d[2] < 128 && d[3] < 128) ||
  1082. d[2] * 256 + d[3] == ast->prefix /* ||
  1083. (d[2] == 'd' && d[3] == 'c') ||
  1084. (d[2] == 'w' && d[3] == 'b') */) {
  1085. if (exit_early)
  1086. return 0;
  1087. if (d[2] * 256 + d[3] == ast->prefix)
  1088. ast->prefix_count++;
  1089. else {
  1090. ast->prefix = d[2] * 256 + d[3];
  1091. ast->prefix_count = 0;
  1092. }
  1093. avi->stream_index = n;
  1094. ast->packet_size = size + 8;
  1095. ast->remaining = size;
  1096. if (size) {
  1097. uint64_t pos = avio_tell(pb) - 8;
  1098. if (!st->index_entries || !st->nb_index_entries ||
  1099. st->index_entries[st->nb_index_entries - 1].pos < pos) {
  1100. av_add_index_entry(st, pos, ast->frame_offset, size,
  1101. 0, AVINDEX_KEYFRAME);
  1102. }
  1103. }
  1104. return 0;
  1105. }
  1106. }
  1107. }
  1108. if (pb->error)
  1109. return pb->error;
  1110. return AVERROR_EOF;
  1111. }
  1112. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  1113. {
  1114. AVIContext *avi = s->priv_data;
  1115. AVIOContext *pb = s->pb;
  1116. int err;
  1117. #if FF_API_DESTRUCT_PACKET
  1118. void *dstr;
  1119. #endif
  1120. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  1121. int size = avpriv_dv_get_packet(avi->dv_demux, pkt);
  1122. if (size >= 0)
  1123. return size;
  1124. else
  1125. goto resync;
  1126. }
  1127. if (avi->non_interleaved) {
  1128. int best_stream_index = 0;
  1129. AVStream *best_st = NULL;
  1130. AVIStream *best_ast;
  1131. int64_t best_ts = INT64_MAX;
  1132. int i;
  1133. for (i = 0; i < s->nb_streams; i++) {
  1134. AVStream *st = s->streams[i];
  1135. AVIStream *ast = st->priv_data;
  1136. int64_t ts = ast->frame_offset;
  1137. int64_t last_ts;
  1138. if (!st->nb_index_entries)
  1139. continue;
  1140. last_ts = st->index_entries[st->nb_index_entries - 1].timestamp;
  1141. if (!ast->remaining && ts > last_ts)
  1142. continue;
  1143. ts = av_rescale_q(ts, st->time_base,
  1144. (AVRational) { FFMAX(1, ast->sample_size),
  1145. AV_TIME_BASE });
  1146. av_dlog(s, "%"PRId64" %d/%d %"PRId64"\n", ts,
  1147. st->time_base.num, st->time_base.den, ast->frame_offset);
  1148. if (ts < best_ts) {
  1149. best_ts = ts;
  1150. best_st = st;
  1151. best_stream_index = i;
  1152. }
  1153. }
  1154. if (!best_st)
  1155. return AVERROR_EOF;
  1156. best_ast = best_st->priv_data;
  1157. best_ts = best_ast->frame_offset;
  1158. if (best_ast->remaining) {
  1159. i = av_index_search_timestamp(best_st,
  1160. best_ts,
  1161. AVSEEK_FLAG_ANY |
  1162. AVSEEK_FLAG_BACKWARD);
  1163. } else {
  1164. i = av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
  1165. if (i >= 0)
  1166. best_ast->frame_offset = best_st->index_entries[i].timestamp;
  1167. }
  1168. if (i >= 0) {
  1169. int64_t pos = best_st->index_entries[i].pos;
  1170. pos += best_ast->packet_size - best_ast->remaining;
  1171. if (avio_seek(s->pb, pos + 8, SEEK_SET) < 0)
  1172. return AVERROR_EOF;
  1173. av_assert0(best_ast->remaining <= best_ast->packet_size);
  1174. avi->stream_index = best_stream_index;
  1175. if (!best_ast->remaining)
  1176. best_ast->packet_size =
  1177. best_ast->remaining = best_st->index_entries[i].size;
  1178. }
  1179. else
  1180. return AVERROR_EOF;
  1181. }
  1182. resync:
  1183. if (avi->stream_index >= 0) {
  1184. AVStream *st = s->streams[avi->stream_index];
  1185. AVIStream *ast = st->priv_data;
  1186. int size, err;
  1187. if (get_subtitle_pkt(s, st, pkt))
  1188. return 0;
  1189. // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
  1190. if (ast->sample_size <= 1)
  1191. size = INT_MAX;
  1192. else if (ast->sample_size < 32)
  1193. // arbitrary multiplier to avoid tiny packets for raw PCM data
  1194. size = 1024 * ast->sample_size;
  1195. else
  1196. size = ast->sample_size;
  1197. if (size > ast->remaining)
  1198. size = ast->remaining;
  1199. avi->last_pkt_pos = avio_tell(pb);
  1200. err = av_get_packet(pb, pkt, size);
  1201. if (err < 0)
  1202. return err;
  1203. size = err;
  1204. if (ast->has_pal && pkt->size < (unsigned)INT_MAX / 2) {
  1205. uint8_t *pal;
  1206. pal = av_packet_new_side_data(pkt,
  1207. AV_PKT_DATA_PALETTE,
  1208. AVPALETTE_SIZE);
  1209. if (!pal) {
  1210. av_log(s, AV_LOG_ERROR,
  1211. "Failed to allocate data for palette\n");
  1212. } else {
  1213. memcpy(pal, ast->pal, AVPALETTE_SIZE);
  1214. ast->has_pal = 0;
  1215. }
  1216. }
  1217. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  1218. AVBufferRef *avbuf = pkt->buf;
  1219. #if FF_API_DESTRUCT_PACKET
  1220. FF_DISABLE_DEPRECATION_WARNINGS
  1221. dstr = pkt->destruct;
  1222. FF_ENABLE_DEPRECATION_WARNINGS
  1223. #endif
  1224. size = avpriv_dv_produce_packet(avi->dv_demux, pkt,
  1225. pkt->data, pkt->size, pkt->pos);
  1226. #if FF_API_DESTRUCT_PACKET
  1227. FF_DISABLE_DEPRECATION_WARNINGS
  1228. pkt->destruct = dstr;
  1229. FF_ENABLE_DEPRECATION_WARNINGS
  1230. #endif
  1231. pkt->buf = avbuf;
  1232. pkt->flags |= AV_PKT_FLAG_KEY;
  1233. if (size < 0)
  1234. av_free_packet(pkt);
  1235. } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  1236. !st->codec->codec_tag && read_gab2_sub(st, pkt)) {
  1237. ast->frame_offset++;
  1238. avi->stream_index = -1;
  1239. ast->remaining = 0;
  1240. goto resync;
  1241. } else {
  1242. /* XXX: How to handle B-frames in AVI? */
  1243. pkt->dts = ast->frame_offset;
  1244. // pkt->dts += ast->start;
  1245. if (ast->sample_size)
  1246. pkt->dts /= ast->sample_size;
  1247. av_dlog(s,
  1248. "dts:%"PRId64" offset:%"PRId64" %d/%d smpl_siz:%d "
  1249. "base:%d st:%d size:%d\n",
  1250. pkt->dts,
  1251. ast->frame_offset,
  1252. ast->scale,
  1253. ast->rate,
  1254. ast->sample_size,
  1255. AV_TIME_BASE,
  1256. avi->stream_index,
  1257. size);
  1258. pkt->stream_index = avi->stream_index;
  1259. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->index_entries) {
  1260. AVIndexEntry *e;
  1261. int index;
  1262. index = av_index_search_timestamp(st, ast->frame_offset, AVSEEK_FLAG_ANY);
  1263. e = &st->index_entries[index];
  1264. if (index >= 0 && e->timestamp == ast->frame_offset) {
  1265. if (index == st->nb_index_entries-1) {
  1266. int key=1;
  1267. int i;
  1268. uint32_t state=-1;
  1269. for (i=0; i<FFMIN(size,256); i++) {
  1270. if (st->codec->codec_id == AV_CODEC_ID_MPEG4) {
  1271. if (state == 0x1B6) {
  1272. key= !(pkt->data[i]&0xC0);
  1273. break;
  1274. }
  1275. }else
  1276. break;
  1277. state= (state<<8) + pkt->data[i];
  1278. }
  1279. if (!key)
  1280. e->flags &= ~AVINDEX_KEYFRAME;
  1281. }
  1282. if (e->flags & AVINDEX_KEYFRAME)
  1283. pkt->flags |= AV_PKT_FLAG_KEY;
  1284. }
  1285. } else {
  1286. pkt->flags |= AV_PKT_FLAG_KEY;
  1287. }
  1288. ast->frame_offset += get_duration(ast, pkt->size);
  1289. }
  1290. ast->remaining -= err;
  1291. if (!ast->remaining) {
  1292. avi->stream_index = -1;
  1293. ast->packet_size = 0;
  1294. }
  1295. if (!avi->non_interleaved && pkt->pos >= 0 && ast->seek_pos > pkt->pos) {
  1296. av_free_packet(pkt);
  1297. goto resync;
  1298. }
  1299. ast->seek_pos= 0;
  1300. if (!avi->non_interleaved && st->nb_index_entries>1 && avi->index_loaded>1) {
  1301. int64_t dts= av_rescale_q(pkt->dts, st->time_base, AV_TIME_BASE_Q);
  1302. if (avi->dts_max - dts > 2*AV_TIME_BASE) {
  1303. avi->non_interleaved= 1;
  1304. av_log(s, AV_LOG_INFO, "Switching to NI mode, due to poor interleaving\n");
  1305. }else if (avi->dts_max < dts)
  1306. avi->dts_max = dts;
  1307. }
  1308. return 0;
  1309. }
  1310. if ((err = avi_sync(s, 0)) < 0)
  1311. return err;
  1312. goto resync;
  1313. }
  1314. /* XXX: We make the implicit supposition that the positions are sorted
  1315. * for each stream. */
  1316. static int avi_read_idx1(AVFormatContext *s, int size)
  1317. {
  1318. AVIContext *avi = s->priv_data;
  1319. AVIOContext *pb = s->pb;
  1320. int nb_index_entries, i;
  1321. AVStream *st;
  1322. AVIStream *ast;
  1323. unsigned int index, tag, flags, pos, len, first_packet = 1;
  1324. unsigned last_pos = -1;
  1325. unsigned last_idx = -1;
  1326. int64_t idx1_pos, first_packet_pos = 0, data_offset = 0;
  1327. int anykey = 0;
  1328. nb_index_entries = size / 16;
  1329. if (nb_index_entries <= 0)
  1330. return AVERROR_INVALIDDATA;
  1331. idx1_pos = avio_tell(pb);
  1332. avio_seek(pb, avi->movi_list + 4, SEEK_SET);
  1333. if (avi_sync(s, 1) == 0)
  1334. first_packet_pos = avio_tell(pb) - 8;
  1335. avi->stream_index = -1;
  1336. avio_seek(pb, idx1_pos, SEEK_SET);
  1337. if (s->nb_streams == 1 && s->streams[0]->codec->codec_tag == AV_RL32("MMES")) {
  1338. first_packet_pos = 0;
  1339. data_offset = avi->movi_list;
  1340. }
  1341. /* Read the entries and sort them in each stream component. */
  1342. for (i = 0; i < nb_index_entries; i++) {
  1343. if (avio_feof(pb))
  1344. return -1;
  1345. tag = avio_rl32(pb);
  1346. flags = avio_rl32(pb);
  1347. pos = avio_rl32(pb);
  1348. len = avio_rl32(pb);
  1349. av_dlog(s, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
  1350. i, tag, flags, pos, len);
  1351. index = ((tag & 0xff) - '0') * 10;
  1352. index += (tag >> 8 & 0xff) - '0';
  1353. if (index >= s->nb_streams)
  1354. continue;
  1355. st = s->streams[index];
  1356. ast = st->priv_data;
  1357. if (first_packet && first_packet_pos) {
  1358. if (avi->movi_list + 4 != pos || pos + 500 > first_packet_pos)
  1359. data_offset = first_packet_pos - pos;
  1360. first_packet = 0;
  1361. }
  1362. pos += data_offset;
  1363. av_dlog(s, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
  1364. // even if we have only a single stream, we should
  1365. // switch to non-interleaved to get correct timestamps
  1366. if (last_pos == pos)
  1367. avi->non_interleaved = 1;
  1368. if (last_idx != pos && len) {
  1369. av_add_index_entry(st, pos, ast->cum_len, len, 0,
  1370. (flags & AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
  1371. last_idx= pos;
  1372. }
  1373. ast->cum_len += get_duration(ast, len);
  1374. last_pos = pos;
  1375. anykey |= flags&AVIIF_INDEX;
  1376. }
  1377. if (!anykey) {
  1378. for (index = 0; index < s->nb_streams; index++) {
  1379. st = s->streams[index];
  1380. if (st->nb_index_entries)
  1381. st->index_entries[0].flags |= AVINDEX_KEYFRAME;
  1382. }
  1383. }
  1384. return 0;
  1385. }
  1386. /* Scan the index and consider any file with streams more than
  1387. * 2 seconds or 64MB apart non-interleaved. */
  1388. static int check_stream_max_drift(AVFormatContext *s)
  1389. {
  1390. int64_t min_pos, pos;
  1391. int i;
  1392. int *idx = av_mallocz_array(s->nb_streams, sizeof(*idx));
  1393. if (!idx)
  1394. return AVERROR(ENOMEM);
  1395. for (min_pos = pos = 0; min_pos != INT64_MAX; pos = min_pos + 1LU) {
  1396. int64_t max_dts = INT64_MIN / 2;
  1397. int64_t min_dts = INT64_MAX / 2;
  1398. int64_t max_buffer = 0;
  1399. min_pos = INT64_MAX;
  1400. for (i = 0; i < s->nb_streams; i++) {
  1401. AVStream *st = s->streams[i];
  1402. AVIStream *ast = st->priv_data;
  1403. int n = st->nb_index_entries;
  1404. while (idx[i] < n && st->index_entries[idx[i]].pos < pos)
  1405. idx[i]++;
  1406. if (idx[i] < n) {
  1407. int64_t dts;
  1408. dts = av_rescale_q(st->index_entries[idx[i]].timestamp /
  1409. FFMAX(ast->sample_size, 1),
  1410. st->time_base, AV_TIME_BASE_Q);
  1411. min_dts = FFMIN(min_dts, dts);
  1412. min_pos = FFMIN(min_pos, st->index_entries[idx[i]].pos);
  1413. }
  1414. }
  1415. for (i = 0; i < s->nb_streams; i++) {
  1416. AVStream *st = s->streams[i];
  1417. AVIStream *ast = st->priv_data;
  1418. if (idx[i] && min_dts != INT64_MAX / 2) {
  1419. int64_t dts;
  1420. dts = av_rescale_q(st->index_entries[idx[i] - 1].timestamp /
  1421. FFMAX(ast->sample_size, 1),
  1422. st->time_base, AV_TIME_BASE_Q);
  1423. max_dts = FFMAX(max_dts, dts);
  1424. max_buffer = FFMAX(max_buffer,
  1425. av_rescale(dts - min_dts,
  1426. st->codec->bit_rate,
  1427. AV_TIME_BASE));
  1428. }
  1429. }
  1430. if (max_dts - min_dts > 2 * AV_TIME_BASE ||
  1431. max_buffer > 1024 * 1024 * 8 * 8) {
  1432. av_free(idx);
  1433. return 1;
  1434. }
  1435. }
  1436. av_free(idx);
  1437. return 0;
  1438. }
  1439. static int guess_ni_flag(AVFormatContext *s)
  1440. {
  1441. int i;
  1442. int64_t last_start = 0;
  1443. int64_t first_end = INT64_MAX;
  1444. int64_t oldpos = avio_tell(s->pb);
  1445. for (i = 0; i < s->nb_streams; i++) {
  1446. AVStream *st = s->streams[i];
  1447. int n = st->nb_index_entries;
  1448. unsigned int size;
  1449. if (n <= 0)
  1450. continue;
  1451. if (n >= 2) {
  1452. int64_t pos = st->index_entries[0].pos;
  1453. avio_seek(s->pb, pos + 4, SEEK_SET);
  1454. size = avio_rl32(s->pb);
  1455. if (pos + size > st->index_entries[1].pos)
  1456. last_start = INT64_MAX;
  1457. }
  1458. if (st->index_entries[0].pos > last_start)
  1459. last_start = st->index_entries[0].pos;
  1460. if (st->index_entries[n - 1].pos < first_end)
  1461. first_end = st->index_entries[n - 1].pos;
  1462. }
  1463. avio_seek(s->pb, oldpos, SEEK_SET);
  1464. if (last_start > first_end)
  1465. return 1;
  1466. return check_stream_max_drift(s);
  1467. }
  1468. static int avi_load_index(AVFormatContext *s)
  1469. {
  1470. AVIContext *avi = s->priv_data;
  1471. AVIOContext *pb = s->pb;
  1472. uint32_t tag, size;
  1473. int64_t pos = avio_tell(pb);
  1474. int64_t next;
  1475. int ret = -1;
  1476. if (avio_seek(pb, avi->movi_end, SEEK_SET) < 0)
  1477. goto the_end; // maybe truncated file
  1478. av_dlog(s, "movi_end=0x%"PRIx64"\n", avi->movi_end);
  1479. for (;;) {
  1480. tag = avio_rl32(pb);
  1481. size = avio_rl32(pb);
  1482. if (avio_feof(pb))
  1483. break;
  1484. next = avio_tell(pb) + size + (size & 1);
  1485. av_dlog(s, "tag=%c%c%c%c size=0x%x\n",
  1486. tag & 0xff,
  1487. (tag >> 8) & 0xff,
  1488. (tag >> 16) & 0xff,
  1489. (tag >> 24) & 0xff,
  1490. size);
  1491. if (tag == MKTAG('i', 'd', 'x', '1') &&
  1492. avi_read_idx1(s, size) >= 0) {
  1493. avi->index_loaded=2;
  1494. ret = 0;
  1495. }else if (tag == MKTAG('L', 'I', 'S', 'T')) {
  1496. uint32_t tag1 = avio_rl32(pb);
  1497. if (tag1 == MKTAG('I', 'N', 'F', 'O'))
  1498. ff_read_riff_info(s, size - 4);
  1499. }else if (!ret)
  1500. break;
  1501. if (avio_seek(pb, next, SEEK_SET) < 0)
  1502. break; // something is wrong here
  1503. }
  1504. the_end:
  1505. avio_seek(pb, pos, SEEK_SET);
  1506. return ret;
  1507. }
  1508. static void seek_subtitle(AVStream *st, AVStream *st2, int64_t timestamp)
  1509. {
  1510. AVIStream *ast2 = st2->priv_data;
  1511. int64_t ts2 = av_rescale_q(timestamp, st->time_base, st2->time_base);
  1512. av_free_packet(&ast2->sub_pkt);
  1513. if (avformat_seek_file(ast2->sub_ctx, 0, INT64_MIN, ts2, ts2, 0) >= 0 ||
  1514. avformat_seek_file(ast2->sub_ctx, 0, ts2, ts2, INT64_MAX, 0) >= 0)
  1515. ff_read_packet(ast2->sub_ctx, &ast2->sub_pkt);
  1516. }
  1517. static int avi_read_seek(AVFormatContext *s, int stream_index,
  1518. int64_t timestamp, int flags)
  1519. {
  1520. AVIContext *avi = s->priv_data;
  1521. AVStream *st;
  1522. int i, index;
  1523. int64_t pos, pos_min;
  1524. AVIStream *ast;
  1525. /* Does not matter which stream is requested dv in avi has the
  1526. * stream information in the first video stream.
  1527. */
  1528. if (avi->dv_demux)
  1529. stream_index = 0;
  1530. if (!avi->index_loaded) {
  1531. /* we only load the index on demand */
  1532. avi_load_index(s);
  1533. avi->index_loaded |= 1;
  1534. }
  1535. av_assert0(stream_index >= 0);
  1536. st = s->streams[stream_index];
  1537. ast = st->priv_data;
  1538. index = av_index_search_timestamp(st,
  1539. timestamp * FFMAX(ast->sample_size, 1),
  1540. flags);
  1541. if (index < 0) {
  1542. if (st->nb_index_entries > 0)
  1543. av_log(s, AV_LOG_DEBUG, "Failed to find timestamp %"PRId64 " in index %"PRId64 " .. %"PRId64 "\n",
  1544. timestamp * FFMAX(ast->sample_size, 1),
  1545. st->index_entries[0].timestamp,
  1546. st->index_entries[st->nb_index_entries - 1].timestamp);
  1547. return AVERROR_INVALIDDATA;
  1548. }
  1549. /* find the position */
  1550. pos = st->index_entries[index].pos;
  1551. timestamp = st->index_entries[index].timestamp / FFMAX(ast->sample_size, 1);
  1552. av_dlog(s, "XX %"PRId64" %d %"PRId64"\n",
  1553. timestamp, index, st->index_entries[index].timestamp);
  1554. if (CONFIG_DV_DEMUXER && avi->dv_demux) {
  1555. /* One and only one real stream for DV in AVI, and it has video */
  1556. /* offsets. Calling with other stream indexes should have failed */
  1557. /* the av_index_search_timestamp call above. */
  1558. if (avio_seek(s->pb, pos, SEEK_SET) < 0)
  1559. return -1;
  1560. /* Feed the DV video stream version of the timestamp to the */
  1561. /* DV demux so it can synthesize correct timestamps. */
  1562. ff_dv_offset_reset(avi->dv_demux, timestamp);
  1563. avi->stream_index = -1;
  1564. return 0;
  1565. }
  1566. pos_min = pos;
  1567. for (i = 0; i < s->nb_streams; i++) {
  1568. AVStream *st2 = s->streams[i];
  1569. AVIStream *ast2 = st2->priv_data;
  1570. ast2->packet_size =
  1571. ast2->remaining = 0;
  1572. if (ast2->sub_ctx) {
  1573. seek_subtitle(st, st2, timestamp);
  1574. continue;
  1575. }
  1576. if (st2->nb_index_entries <= 0)
  1577. continue;
  1578. // av_assert1(st2->codec->block_align);
  1579. av_assert0(fabs(av_q2d(st2->time_base) - ast2->scale / (double)ast2->rate) < av_q2d(st2->time_base) * 0.00000001);
  1580. index = av_index_search_timestamp(st2,
  1581. av_rescale_q(timestamp,
  1582. st->time_base,
  1583. st2->time_base) *
  1584. FFMAX(ast2->sample_size, 1),
  1585. flags |
  1586. AVSEEK_FLAG_BACKWARD |
  1587. (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
  1588. if (index < 0)
  1589. index = 0;
  1590. ast2->seek_pos = st2->index_entries[index].pos;
  1591. pos_min = FFMIN(pos_min,ast2->seek_pos);
  1592. }
  1593. for (i = 0; i < s->nb_streams; i++) {
  1594. AVStream *st2 = s->streams[i];
  1595. AVIStream *ast2 = st2->priv_data;
  1596. if (ast2->sub_ctx || st2->nb_index_entries <= 0)
  1597. continue;
  1598. index = av_index_search_timestamp(
  1599. st2,
  1600. av_rescale_q(timestamp, st->time_base, st2->time_base) * FFMAX(ast2->sample_size, 1),
  1601. flags | AVSEEK_FLAG_BACKWARD | (st2->codec->codec_type != AVMEDIA_TYPE_VIDEO ? AVSEEK_FLAG_ANY : 0));
  1602. if (index < 0)
  1603. index = 0;
  1604. while (!avi->non_interleaved && index>0 && st2->index_entries[index-1].pos >= pos_min)
  1605. index--;
  1606. ast2->frame_offset = st2->index_entries[index].timestamp;
  1607. }
  1608. /* do the seek */
  1609. if (avio_seek(s->pb, pos_min, SEEK_SET) < 0) {
  1610. av_log(s, AV_LOG_ERROR, "Seek failed\n");
  1611. return -1;
  1612. }
  1613. avi->stream_index = -1;
  1614. avi->dts_max = INT_MIN;
  1615. return 0;
  1616. }
  1617. static int avi_read_close(AVFormatContext *s)
  1618. {
  1619. int i;
  1620. AVIContext *avi = s->priv_data;
  1621. for (i = 0; i < s->nb_streams; i++) {
  1622. AVStream *st = s->streams[i];
  1623. AVIStream *ast = st->priv_data;
  1624. if (ast) {
  1625. if (ast->sub_ctx) {
  1626. av_freep(&ast->sub_ctx->pb);
  1627. avformat_close_input(&ast->sub_ctx);
  1628. }
  1629. av_free(ast->sub_buffer);
  1630. av_free_packet(&ast->sub_pkt);
  1631. }
  1632. }
  1633. av_free(avi->dv_demux);
  1634. return 0;
  1635. }
  1636. static int avi_probe(AVProbeData *p)
  1637. {
  1638. int i;
  1639. /* check file header */
  1640. for (i = 0; avi_headers[i][0]; i++)
  1641. if (!memcmp(p->buf, avi_headers[i], 4) &&
  1642. !memcmp(p->buf + 8, avi_headers[i] + 4, 4))
  1643. return AVPROBE_SCORE_MAX;
  1644. return 0;
  1645. }
  1646. AVInputFormat ff_avi_demuxer = {
  1647. .name = "avi",
  1648. .long_name = NULL_IF_CONFIG_SMALL("AVI (Audio Video Interleaved)"),
  1649. .priv_data_size = sizeof(AVIContext),
  1650. .extensions = "avi",
  1651. .read_probe = avi_probe,
  1652. .read_header = avi_read_header,
  1653. .read_packet = avi_read_packet,
  1654. .read_close = avi_read_close,
  1655. .read_seek = avi_read_seek,
  1656. .priv_class = &demuxer_class,
  1657. };