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.

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