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.

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