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.

1885 lines
64KB

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