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.

1899 lines
65KB

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