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.

733 lines
23KB

  1. /*
  2. * AVI decoder.
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include "avi.h"
  21. #include "dv.h"
  22. //#define DEBUG
  23. //#define DEBUG_SEEK
  24. typedef struct AVIIndexEntry {
  25. unsigned int flags;
  26. unsigned int pos;
  27. unsigned int cum_len; /* sum of all lengths before this packet */
  28. } AVIIndexEntry;
  29. typedef struct AVIStream {
  30. AVIIndexEntry *index_entries;
  31. int nb_index_entries;
  32. int index_entries_allocated_size;
  33. int frame_offset; /* current frame (video) or byte (audio) counter
  34. (used to compute the pts) */
  35. int scale;
  36. int rate;
  37. int sample_size; /* audio only data */
  38. int start;
  39. int new_frame_offset; /* temporary storage (used during seek) */
  40. int cum_len; /* temporary storage (used during seek) */
  41. } AVIStream;
  42. typedef struct {
  43. int64_t riff_end;
  44. int64_t movi_end;
  45. offset_t movi_list;
  46. int index_loaded;
  47. DVDemuxContext* dv_demux;
  48. } AVIContext;
  49. static int avi_load_index(AVFormatContext *s);
  50. #ifdef DEBUG
  51. static void print_tag(const char *str, unsigned int tag, int size)
  52. {
  53. printf("%s: tag=%c%c%c%c size=0x%x\n",
  54. str, tag & 0xff,
  55. (tag >> 8) & 0xff,
  56. (tag >> 16) & 0xff,
  57. (tag >> 24) & 0xff,
  58. size);
  59. }
  60. #endif
  61. static int get_riff(AVIContext *avi, ByteIOContext *pb)
  62. {
  63. uint32_t tag;
  64. /* check RIFF header */
  65. tag = get_le32(pb);
  66. if (tag != MKTAG('R', 'I', 'F', 'F'))
  67. return -1;
  68. avi->riff_end = get_le32(pb); /* RIFF chunk size */
  69. avi->riff_end += url_ftell(pb); /* RIFF chunk end */
  70. tag = get_le32(pb);
  71. if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
  72. return -1;
  73. return 0;
  74. }
  75. static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
  76. {
  77. AVIContext *avi = s->priv_data;
  78. ByteIOContext *pb = &s->pb;
  79. uint32_t tag, tag1, handler;
  80. int codec_type, stream_index, frame_period, bit_rate, scale, rate;
  81. unsigned int size, nb_frames;
  82. int i, n;
  83. AVStream *st;
  84. AVIStream *ast;
  85. int xan_video = 0; /* hack to support Xan A/V */
  86. if (get_riff(avi, pb) < 0)
  87. return -1;
  88. /* first list tag */
  89. stream_index = -1;
  90. codec_type = -1;
  91. frame_period = 0;
  92. for(;;) {
  93. if (url_feof(pb))
  94. goto fail;
  95. tag = get_le32(pb);
  96. size = get_le32(pb);
  97. #ifdef DEBUG
  98. print_tag("tag", tag, size);
  99. #endif
  100. switch(tag) {
  101. case MKTAG('L', 'I', 'S', 'T'):
  102. /* ignored, except when start of video packets */
  103. tag1 = get_le32(pb);
  104. #ifdef DEBUG
  105. print_tag("list", tag1, 0);
  106. #endif
  107. if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
  108. avi->movi_list = url_ftell(pb) - 4;
  109. avi->movi_end = avi->movi_list + size;
  110. #ifdef DEBUG
  111. printf("movi end=%Lx\n", avi->movi_end);
  112. #endif
  113. goto end_of_header;
  114. }
  115. break;
  116. case MKTAG('a', 'v', 'i', 'h'):
  117. /* avi header */
  118. /* using frame_period is bad idea */
  119. frame_period = get_le32(pb);
  120. bit_rate = get_le32(pb) * 8;
  121. url_fskip(pb, 4 * 4);
  122. n = get_le32(pb);
  123. for(i=0;i<n;i++) {
  124. AVIStream *ast;
  125. st = av_new_stream(s, i);
  126. if (!st)
  127. goto fail;
  128. ast = av_mallocz(sizeof(AVIStream));
  129. if (!ast)
  130. goto fail;
  131. st->priv_data = ast;
  132. }
  133. url_fskip(pb, size - 7 * 4);
  134. break;
  135. case MKTAG('s', 't', 'r', 'h'):
  136. /* stream header */
  137. stream_index++;
  138. tag1 = get_le32(pb);
  139. handler = get_le32(pb); /* codec tag */
  140. #ifdef DEBUG
  141. print_tag("strh", tag1, -1);
  142. #endif
  143. switch(tag1) {
  144. case MKTAG('i', 'a', 'v', 's'):
  145. case MKTAG('i', 'v', 'a', 's'):
  146. /*
  147. * After some consideration -- I don't think we
  148. * have to support anything but DV in a type1 AVIs.
  149. */
  150. if (s->nb_streams != 1)
  151. goto fail;
  152. if (handler != MKTAG('d', 'v', 's', 'd') &&
  153. handler != MKTAG('d', 'v', 'h', 'd') &&
  154. handler != MKTAG('d', 'v', 's', 'l'))
  155. goto fail;
  156. av_freep(&s->streams[0]->codec.extradata);
  157. av_freep(&s->streams[0]);
  158. s->nb_streams = 0;
  159. avi->dv_demux = dv_init_demux(s);
  160. if (!avi->dv_demux)
  161. goto fail;
  162. stream_index = s->nb_streams - 1;
  163. url_fskip(pb, size - 8);
  164. break;
  165. case MKTAG('v', 'i', 'd', 's'):
  166. codec_type = CODEC_TYPE_VIDEO;
  167. if (stream_index >= s->nb_streams) {
  168. url_fskip(pb, size - 8);
  169. break;
  170. }
  171. st = s->streams[stream_index];
  172. ast = st->priv_data;
  173. st->codec.stream_codec_tag= handler;
  174. get_le32(pb); /* flags */
  175. get_le16(pb); /* priority */
  176. get_le16(pb); /* language */
  177. get_le32(pb); /* XXX: initial frame ? */
  178. scale = get_le32(pb); /* scale */
  179. rate = get_le32(pb); /* rate */
  180. if(scale && rate){
  181. }else if(frame_period){
  182. rate = 1000000;
  183. scale = frame_period;
  184. }else{
  185. rate = 25;
  186. scale = 1;
  187. }
  188. ast->rate = rate;
  189. ast->scale = scale;
  190. av_set_pts_info(st, 64, scale, rate);
  191. st->codec.frame_rate = rate;
  192. st->codec.frame_rate_base = scale;
  193. get_le32(pb); /* start */
  194. nb_frames = get_le32(pb);
  195. st->start_time = 0;
  196. st->duration = av_rescale(nb_frames,
  197. st->codec.frame_rate_base * AV_TIME_BASE,
  198. st->codec.frame_rate);
  199. url_fskip(pb, size - 9 * 4);
  200. break;
  201. case MKTAG('a', 'u', 'd', 's'):
  202. {
  203. unsigned int length;
  204. codec_type = CODEC_TYPE_AUDIO;
  205. if (stream_index >= s->nb_streams) {
  206. url_fskip(pb, size - 8);
  207. break;
  208. }
  209. st = s->streams[stream_index];
  210. ast = st->priv_data;
  211. get_le32(pb); /* flags */
  212. get_le16(pb); /* priority */
  213. get_le16(pb); /* language */
  214. get_le32(pb); /* initial frame */
  215. ast->scale = get_le32(pb); /* scale */
  216. ast->rate = get_le32(pb);
  217. av_set_pts_info(st, 64, ast->scale, ast->rate);
  218. ast->start= get_le32(pb); /* start */
  219. length = get_le32(pb); /* length, in samples or bytes */
  220. get_le32(pb); /* buffer size */
  221. get_le32(pb); /* quality */
  222. ast->sample_size = get_le32(pb); /* sample ssize */
  223. //av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->scale, ast->rate, ast->sample_size, ast->start);
  224. st->start_time = 0;
  225. if (ast->rate != 0)
  226. st->duration = (int64_t)length * AV_TIME_BASE / ast->rate;
  227. url_fskip(pb, size - 12 * 4);
  228. }
  229. break;
  230. case MKTAG('t', 'x', 't', 's'):
  231. //FIXME
  232. codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME
  233. url_fskip(pb, size - 8);
  234. break;
  235. default:
  236. goto fail;
  237. }
  238. break;
  239. case MKTAG('s', 't', 'r', 'f'):
  240. /* stream header */
  241. if (stream_index >= s->nb_streams || avi->dv_demux) {
  242. url_fskip(pb, size);
  243. } else {
  244. st = s->streams[stream_index];
  245. switch(codec_type) {
  246. case CODEC_TYPE_VIDEO:
  247. get_le32(pb); /* size */
  248. st->codec.width = get_le32(pb);
  249. st->codec.height = get_le32(pb);
  250. get_le16(pb); /* panes */
  251. st->codec.bits_per_sample= get_le16(pb); /* depth */
  252. tag1 = get_le32(pb);
  253. get_le32(pb); /* ImageSize */
  254. get_le32(pb); /* XPelsPerMeter */
  255. get_le32(pb); /* YPelsPerMeter */
  256. get_le32(pb); /* ClrUsed */
  257. get_le32(pb); /* ClrImportant */
  258. st->codec.extradata_size= size - 10*4;
  259. st->codec.extradata= av_malloc(st->codec.extradata_size);
  260. get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
  261. if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
  262. get_byte(pb);
  263. /* Extract palette from extradata if bpp <= 8 */
  264. /* This code assumes that extradata contains only palette */
  265. /* This is true for all paletted codecs implemented in ffmpeg */
  266. if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) {
  267. st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl));
  268. #ifdef WORDS_BIGENDIAN
  269. for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++)
  270. st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]);
  271. #else
  272. memcpy(st->codec.palctrl->palette, st->codec.extradata,
  273. FFMIN(st->codec.extradata_size, AVPALETTE_SIZE));
  274. #endif
  275. st->codec.palctrl->palette_changed = 1;
  276. }
  277. #ifdef DEBUG
  278. print_tag("video", tag1, 0);
  279. #endif
  280. st->codec.codec_type = CODEC_TYPE_VIDEO;
  281. st->codec.codec_tag = tag1;
  282. st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
  283. if (st->codec.codec_id == CODEC_ID_XAN_WC4)
  284. xan_video = 1;
  285. // url_fskip(pb, size - 5 * 4);
  286. break;
  287. case CODEC_TYPE_AUDIO:
  288. get_wav_header(pb, &st->codec, size);
  289. if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
  290. url_fskip(pb, 1);
  291. /* special case time: To support Xan DPCM, hardcode
  292. * the format if Xxan is the video codec */
  293. st->need_parsing = 1;
  294. /* force parsing as several audio frames can be in
  295. one packet */
  296. if (xan_video)
  297. st->codec.codec_id = CODEC_ID_XAN_DPCM;
  298. break;
  299. default:
  300. st->codec.codec_type = CODEC_TYPE_DATA;
  301. st->codec.codec_id= CODEC_ID_NONE;
  302. st->codec.codec_tag= 0;
  303. url_fskip(pb, size);
  304. break;
  305. }
  306. }
  307. break;
  308. default:
  309. /* skip tag */
  310. size += (size & 1);
  311. url_fskip(pb, size);
  312. break;
  313. }
  314. }
  315. end_of_header:
  316. /* check stream number */
  317. if (stream_index != s->nb_streams - 1) {
  318. fail:
  319. for(i=0;i<s->nb_streams;i++) {
  320. av_freep(&s->streams[i]->codec.extradata);
  321. av_freep(&s->streams[i]);
  322. }
  323. return -1;
  324. }
  325. assert(!avi->index_loaded);
  326. avi_load_index(s);
  327. avi->index_loaded = 1;
  328. return 0;
  329. }
  330. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  331. {
  332. AVIContext *avi = s->priv_data;
  333. ByteIOContext *pb = &s->pb;
  334. int n, d[8], size, i;
  335. void* dstr;
  336. memset(d, -1, sizeof(int)*8);
  337. if (avi->dv_demux) {
  338. size = dv_get_packet(avi->dv_demux, pkt);
  339. if (size >= 0)
  340. return size;
  341. }
  342. for(i=url_ftell(pb); !url_feof(pb); i++) {
  343. int j;
  344. if (i >= avi->movi_end) { /* Let's see if it's an OpenDML AVI */
  345. uint32_t tag, size, tag2;
  346. url_fskip(pb, avi->riff_end - url_ftell(pb));
  347. if (get_riff(avi, pb) < 0)
  348. return -1;
  349. tag = get_le32(pb);
  350. size = get_le32(pb);
  351. tag2 = get_le32(pb);
  352. if (tag == MKTAG('L','I','S','T') && tag2 == MKTAG('m','o','v','i'))
  353. avi->movi_end = url_ftell(pb) + size - 4;
  354. else
  355. return -1;
  356. }
  357. for(j=0; j<7; j++)
  358. d[j]= d[j+1];
  359. d[7]= get_byte(pb);
  360. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  361. //parse ix##
  362. n= (d[2] - '0') * 10 + (d[3] - '0');
  363. if( d[2] >= '0' && d[2] <= '9'
  364. && d[3] >= '0' && d[3] <= '9'
  365. && d[0] == 'i' && d[1] == 'x'
  366. && n < s->nb_streams
  367. && i + size <= avi->movi_end){
  368. url_fskip(pb, size);
  369. }
  370. //parse ##dc/##wb
  371. n= (d[0] - '0') * 10 + (d[1] - '0');
  372. if( d[0] >= '0' && d[0] <= '9'
  373. && d[1] >= '0' && d[1] <= '9'
  374. && ((d[2] == 'd' && d[3] == 'c') ||
  375. (d[2] == 'w' && d[3] == 'b') ||
  376. (d[2] == 'd' && d[3] == 'b') ||
  377. (d[2] == '_' && d[3] == '_'))
  378. && n < s->nb_streams
  379. && i + size <= avi->movi_end) {
  380. av_new_packet(pkt, size);
  381. get_buffer(pb, pkt->data, size);
  382. if (size & 1) {
  383. get_byte(pb);
  384. size++;
  385. }
  386. if (avi->dv_demux) {
  387. dstr = pkt->destruct;
  388. size = dv_produce_packet(avi->dv_demux, pkt,
  389. pkt->data, pkt->size);
  390. pkt->destruct = dstr;
  391. pkt->flags |= PKT_FLAG_KEY;
  392. } else {
  393. AVStream *st;
  394. AVIStream *ast;
  395. st = s->streams[n];
  396. ast = st->priv_data;
  397. /* XXX: how to handle B frames in avi ? */
  398. pkt->dts = ast->frame_offset;
  399. // pkt->dts += ast->start;
  400. if(ast->sample_size)
  401. pkt->dts /= ast->sample_size;
  402. //av_log(NULL, AV_LOG_DEBUG, "dts:%Ld offset:%d %d/%d %d st:%d size:%d\n", pkt->dts, ast->frame_offset, ast->scale, ast->rate, AV_TIME_BASE, n, size);
  403. pkt->stream_index = n;
  404. /* FIXME: We really should read index for that */
  405. if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
  406. if (ast->frame_offset < ast->nb_index_entries) {
  407. if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX)
  408. pkt->flags |= PKT_FLAG_KEY;
  409. } else {
  410. /* if no index, better to say that all frames
  411. are key frames */
  412. pkt->flags |= PKT_FLAG_KEY;
  413. }
  414. } else {
  415. pkt->flags |= PKT_FLAG_KEY;
  416. }
  417. if(ast->sample_size)
  418. ast->frame_offset += pkt->size;
  419. else
  420. ast->frame_offset++;
  421. }
  422. return size;
  423. }
  424. }
  425. return -1;
  426. }
  427. /* XXX: we make the implicit supposition that the position are sorted
  428. for each stream */
  429. static int avi_read_idx1(AVFormatContext *s, int size)
  430. {
  431. ByteIOContext *pb = &s->pb;
  432. int nb_index_entries, i;
  433. AVStream *st;
  434. AVIStream *ast;
  435. AVIIndexEntry *ie, *entries;
  436. unsigned int index, tag, flags, pos, len;
  437. nb_index_entries = size / 16;
  438. if (nb_index_entries <= 0)
  439. return -1;
  440. /* read the entries and sort them in each stream component */
  441. for(i = 0; i < nb_index_entries; i++) {
  442. tag = get_le32(pb);
  443. flags = get_le32(pb);
  444. pos = get_le32(pb);
  445. len = get_le32(pb);
  446. #if defined(DEBUG_SEEK) && 0
  447. printf("%d: tag=0x%x flags=0x%x pos=0x%x len=%d\n",
  448. i, tag, flags, pos, len);
  449. #endif
  450. index = ((tag & 0xff) - '0') * 10;
  451. index += ((tag >> 8) & 0xff) - '0';
  452. if (index >= s->nb_streams)
  453. continue;
  454. st = s->streams[index];
  455. ast = st->priv_data;
  456. entries = av_fast_realloc(ast->index_entries,
  457. &ast->index_entries_allocated_size,
  458. (ast->nb_index_entries + 1) *
  459. sizeof(AVIIndexEntry));
  460. if (entries) {
  461. ast->index_entries = entries;
  462. ie = &entries[ast->nb_index_entries++];
  463. ie->flags = flags;
  464. ie->pos = pos;
  465. ie->cum_len = ast->cum_len;
  466. ast->cum_len += len;
  467. }
  468. }
  469. return 0;
  470. }
  471. static int avi_load_index(AVFormatContext *s)
  472. {
  473. AVIContext *avi = s->priv_data;
  474. ByteIOContext *pb = &s->pb;
  475. uint32_t tag, size;
  476. offset_t pos= url_ftell(pb);
  477. url_fseek(pb, avi->movi_end, SEEK_SET);
  478. #ifdef DEBUG_SEEK
  479. printf("movi_end=0x%llx\n", avi->movi_end);
  480. #endif
  481. for(;;) {
  482. if (url_feof(pb))
  483. break;
  484. tag = get_le32(pb);
  485. size = get_le32(pb);
  486. #ifdef DEBUG_SEEK
  487. printf("tag=%c%c%c%c size=0x%x\n",
  488. tag & 0xff,
  489. (tag >> 8) & 0xff,
  490. (tag >> 16) & 0xff,
  491. (tag >> 24) & 0xff,
  492. size);
  493. #endif
  494. switch(tag) {
  495. case MKTAG('i', 'd', 'x', '1'):
  496. if (avi_read_idx1(s, size) < 0)
  497. goto skip;
  498. else
  499. goto the_end;
  500. break;
  501. default:
  502. skip:
  503. size += (size & 1);
  504. url_fskip(pb, size);
  505. break;
  506. }
  507. }
  508. the_end:
  509. url_fseek(pb, pos, SEEK_SET);
  510. return 0;
  511. }
  512. /* return the index entry whose position is immediately >= 'wanted_pos' */
  513. static int locate_frame_in_index(AVIIndexEntry *entries,
  514. int nb_entries, int wanted_pos)
  515. {
  516. int a, b, m, pos;
  517. a = 0;
  518. b = nb_entries - 1;
  519. while (a <= b) {
  520. m = (a + b) >> 1;
  521. pos = entries[m].pos;
  522. if (pos == wanted_pos)
  523. goto found;
  524. else if (pos > wanted_pos) {
  525. b = m - 1;
  526. } else {
  527. a = m + 1;
  528. }
  529. }
  530. m = a;
  531. if (m > 0)
  532. m--;
  533. found:
  534. return m;
  535. }
  536. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp)
  537. {
  538. AVIContext *avi = s->priv_data;
  539. AVStream *st;
  540. AVIStream *ast;
  541. int frame_number, i;
  542. int64_t pos;
  543. if (!avi->index_loaded) {
  544. /* we only load the index on demand */
  545. avi_load_index(s);
  546. avi->index_loaded = 1;
  547. }
  548. if (stream_index < 0) {
  549. for(i = 0; i < s->nb_streams; i++) {
  550. st = s->streams[i];
  551. if (st->codec.codec_type == CODEC_TYPE_VIDEO)
  552. goto found;
  553. }
  554. return -1;
  555. found:
  556. stream_index = i;
  557. }
  558. st = s->streams[stream_index];
  559. if (st->codec.codec_type != CODEC_TYPE_VIDEO)
  560. return -1;
  561. ast = st->priv_data;
  562. /* compute the frame number */
  563. frame_number = timestamp;
  564. #ifdef DEBUG_SEEK
  565. printf("timestamp=%0.3f nb_indexes=%d frame_number=%d\n",
  566. (double)timestamp / AV_TIME_BASE,
  567. ast->nb_index_entries, frame_number);
  568. #endif
  569. /* find a closest key frame before */
  570. if (frame_number >= ast->nb_index_entries)
  571. return -1;
  572. while (frame_number >= 0 &&
  573. !(ast->index_entries[frame_number].flags & AVIIF_INDEX))
  574. frame_number--;
  575. if (frame_number < 0)
  576. return -1;
  577. ast->new_frame_offset = frame_number;
  578. /* find the position */
  579. pos = ast->index_entries[frame_number].pos;
  580. #ifdef DEBUG_SEEK
  581. printf("key_frame_number=%d pos=0x%llx\n",
  582. frame_number, pos);
  583. #endif
  584. /* update the frame counters for all the other stream by looking
  585. at the positions just after the one found */
  586. for(i = 0; i < s->nb_streams; i++) {
  587. int j;
  588. if (i != stream_index) {
  589. st = s->streams[i];
  590. ast = st->priv_data;
  591. if (ast->nb_index_entries <= 0)
  592. return -1;
  593. j = locate_frame_in_index(ast->index_entries,
  594. ast->nb_index_entries,
  595. pos);
  596. /* get next frame */
  597. if ((j + 1) < ast->nb_index_entries)
  598. j++;
  599. /* extract the current frame number */
  600. if (ast->sample_size==0)
  601. ast->new_frame_offset = j;
  602. else
  603. ast->new_frame_offset = ast->index_entries[j].cum_len;
  604. }
  605. }
  606. /* everything is OK now. We can update the frame offsets */
  607. for(i = 0; i < s->nb_streams; i++) {
  608. st = s->streams[i];
  609. ast = st->priv_data;
  610. ast->frame_offset = ast->new_frame_offset;
  611. #ifdef DEBUG_SEEK
  612. printf("%d: frame_offset=%d\n", i,
  613. ast->frame_offset);
  614. #endif
  615. }
  616. /* do the seek */
  617. pos += avi->movi_list;
  618. url_fseek(&s->pb, pos, SEEK_SET);
  619. return 0;
  620. }
  621. static int avi_read_close(AVFormatContext *s)
  622. {
  623. int i;
  624. AVIContext *avi = s->priv_data;
  625. for(i=0;i<s->nb_streams;i++) {
  626. AVStream *st = s->streams[i];
  627. AVIStream *ast = st->priv_data;
  628. if(ast){
  629. av_free(ast->index_entries);
  630. av_free(ast);
  631. }
  632. av_free(st->codec.extradata);
  633. av_free(st->codec.palctrl);
  634. }
  635. if (avi->dv_demux)
  636. av_free(avi->dv_demux);
  637. return 0;
  638. }
  639. static int avi_probe(AVProbeData *p)
  640. {
  641. /* check file header */
  642. if (p->buf_size <= 32)
  643. return 0;
  644. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  645. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  646. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  647. p->buf[10] == 'I' && p->buf[11] == ' ')
  648. return AVPROBE_SCORE_MAX;
  649. else
  650. return 0;
  651. }
  652. static AVInputFormat avi_iformat = {
  653. "avi",
  654. "avi format",
  655. sizeof(AVIContext),
  656. avi_probe,
  657. avi_read_header,
  658. avi_read_packet,
  659. avi_read_close,
  660. avi_read_seek,
  661. };
  662. int avidec_init(void)
  663. {
  664. av_register_input_format(&avi_iformat);
  665. return 0;
  666. }