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.

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