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.

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