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.

1892 lines
64KB

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