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.

730 lines
22KB

  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. url_fskip(pb, size);
  301. break;
  302. }
  303. }
  304. break;
  305. default:
  306. /* skip tag */
  307. size += (size & 1);
  308. url_fskip(pb, size);
  309. break;
  310. }
  311. }
  312. end_of_header:
  313. /* check stream number */
  314. if (stream_index != s->nb_streams - 1) {
  315. fail:
  316. for(i=0;i<s->nb_streams;i++) {
  317. av_freep(&s->streams[i]->codec.extradata);
  318. av_freep(&s->streams[i]);
  319. }
  320. return -1;
  321. }
  322. assert(!avi->index_loaded);
  323. avi_load_index(s);
  324. avi->index_loaded = 1;
  325. return 0;
  326. }
  327. static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
  328. {
  329. AVIContext *avi = s->priv_data;
  330. ByteIOContext *pb = &s->pb;
  331. int n, d[8], size, i;
  332. void* dstr;
  333. memset(d, -1, sizeof(int)*8);
  334. if (avi->dv_demux) {
  335. size = dv_get_packet(avi->dv_demux, pkt);
  336. if (size >= 0)
  337. return size;
  338. }
  339. for(i=url_ftell(pb); !url_feof(pb); i++) {
  340. int j;
  341. if (i >= avi->movi_end) { /* Let's see if it's an OpenDML AVI */
  342. uint32_t tag, size, tag2;
  343. url_fskip(pb, avi->riff_end - url_ftell(pb));
  344. if (get_riff(avi, pb) < 0)
  345. return -1;
  346. tag = get_le32(pb);
  347. size = get_le32(pb);
  348. tag2 = get_le32(pb);
  349. if (tag == MKTAG('L','I','S','T') && tag2 == MKTAG('m','o','v','i'))
  350. avi->movi_end = url_ftell(pb) + size - 4;
  351. else
  352. return -1;
  353. }
  354. for(j=0; j<7; j++)
  355. d[j]= d[j+1];
  356. d[7]= get_byte(pb);
  357. size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
  358. //parse ix##
  359. n= (d[2] - '0') * 10 + (d[3] - '0');
  360. if( d[2] >= '0' && d[2] <= '9'
  361. && d[3] >= '0' && d[3] <= '9'
  362. && d[0] == 'i' && d[1] == 'x'
  363. && n < s->nb_streams
  364. && i + size <= avi->movi_end){
  365. url_fskip(pb, size);
  366. }
  367. //parse ##dc/##wb
  368. n= (d[0] - '0') * 10 + (d[1] - '0');
  369. if( d[0] >= '0' && d[0] <= '9'
  370. && d[1] >= '0' && d[1] <= '9'
  371. && ((d[2] == 'd' && d[3] == 'c') ||
  372. (d[2] == 'w' && d[3] == 'b') ||
  373. (d[2] == 'd' && d[3] == 'b') ||
  374. (d[2] == '_' && d[3] == '_'))
  375. && n < s->nb_streams
  376. && i + size <= avi->movi_end) {
  377. av_new_packet(pkt, size);
  378. get_buffer(pb, pkt->data, size);
  379. if (size & 1) {
  380. get_byte(pb);
  381. size++;
  382. }
  383. if (avi->dv_demux) {
  384. dstr = pkt->destruct;
  385. size = dv_produce_packet(avi->dv_demux, pkt,
  386. pkt->data, pkt->size);
  387. pkt->destruct = dstr;
  388. pkt->flags |= PKT_FLAG_KEY;
  389. } else {
  390. AVStream *st;
  391. AVIStream *ast;
  392. st = s->streams[n];
  393. ast = st->priv_data;
  394. /* XXX: how to handle B frames in avi ? */
  395. pkt->dts = ast->frame_offset;
  396. // pkt->dts += ast->start;
  397. if(ast->sample_size)
  398. pkt->dts /= ast->sample_size;
  399. //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);
  400. pkt->stream_index = n;
  401. /* FIXME: We really should read index for that */
  402. if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
  403. if (ast->frame_offset < ast->nb_index_entries) {
  404. if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX)
  405. pkt->flags |= PKT_FLAG_KEY;
  406. } else {
  407. /* if no index, better to say that all frames
  408. are key frames */
  409. pkt->flags |= PKT_FLAG_KEY;
  410. }
  411. } else {
  412. pkt->flags |= PKT_FLAG_KEY;
  413. }
  414. if(ast->sample_size)
  415. ast->frame_offset += pkt->size;
  416. else
  417. ast->frame_offset++;
  418. }
  419. return size;
  420. }
  421. }
  422. return -1;
  423. }
  424. /* XXX: we make the implicit supposition that the position are sorted
  425. for each stream */
  426. static int avi_read_idx1(AVFormatContext *s, int size)
  427. {
  428. ByteIOContext *pb = &s->pb;
  429. int nb_index_entries, i;
  430. AVStream *st;
  431. AVIStream *ast;
  432. AVIIndexEntry *ie, *entries;
  433. unsigned int index, tag, flags, pos, len;
  434. nb_index_entries = size / 16;
  435. if (nb_index_entries <= 0)
  436. return -1;
  437. /* read the entries and sort them in each stream component */
  438. for(i = 0; i < nb_index_entries; i++) {
  439. tag = get_le32(pb);
  440. flags = get_le32(pb);
  441. pos = get_le32(pb);
  442. len = get_le32(pb);
  443. #if defined(DEBUG_SEEK) && 0
  444. printf("%d: tag=0x%x flags=0x%x pos=0x%x len=%d\n",
  445. i, tag, flags, pos, len);
  446. #endif
  447. index = ((tag & 0xff) - '0') * 10;
  448. index += ((tag >> 8) & 0xff) - '0';
  449. if (index >= s->nb_streams)
  450. continue;
  451. st = s->streams[index];
  452. ast = st->priv_data;
  453. entries = av_fast_realloc(ast->index_entries,
  454. &ast->index_entries_allocated_size,
  455. (ast->nb_index_entries + 1) *
  456. sizeof(AVIIndexEntry));
  457. if (entries) {
  458. ast->index_entries = entries;
  459. ie = &entries[ast->nb_index_entries++];
  460. ie->flags = flags;
  461. ie->pos = pos;
  462. ie->cum_len = ast->cum_len;
  463. ast->cum_len += len;
  464. }
  465. }
  466. return 0;
  467. }
  468. static int avi_load_index(AVFormatContext *s)
  469. {
  470. AVIContext *avi = s->priv_data;
  471. ByteIOContext *pb = &s->pb;
  472. uint32_t tag, size;
  473. offset_t pos= url_ftell(pb);
  474. url_fseek(pb, avi->movi_end, SEEK_SET);
  475. #ifdef DEBUG_SEEK
  476. printf("movi_end=0x%llx\n", avi->movi_end);
  477. #endif
  478. for(;;) {
  479. if (url_feof(pb))
  480. break;
  481. tag = get_le32(pb);
  482. size = get_le32(pb);
  483. #ifdef DEBUG_SEEK
  484. printf("tag=%c%c%c%c size=0x%x\n",
  485. tag & 0xff,
  486. (tag >> 8) & 0xff,
  487. (tag >> 16) & 0xff,
  488. (tag >> 24) & 0xff,
  489. size);
  490. #endif
  491. switch(tag) {
  492. case MKTAG('i', 'd', 'x', '1'):
  493. if (avi_read_idx1(s, size) < 0)
  494. goto skip;
  495. else
  496. goto the_end;
  497. break;
  498. default:
  499. skip:
  500. size += (size & 1);
  501. url_fskip(pb, size);
  502. break;
  503. }
  504. }
  505. the_end:
  506. url_fseek(pb, pos, SEEK_SET);
  507. return 0;
  508. }
  509. /* return the index entry whose position is immediately >= 'wanted_pos' */
  510. static int locate_frame_in_index(AVIIndexEntry *entries,
  511. int nb_entries, int wanted_pos)
  512. {
  513. int a, b, m, pos;
  514. a = 0;
  515. b = nb_entries - 1;
  516. while (a <= b) {
  517. m = (a + b) >> 1;
  518. pos = entries[m].pos;
  519. if (pos == wanted_pos)
  520. goto found;
  521. else if (pos > wanted_pos) {
  522. b = m - 1;
  523. } else {
  524. a = m + 1;
  525. }
  526. }
  527. m = a;
  528. if (m > 0)
  529. m--;
  530. found:
  531. return m;
  532. }
  533. static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp)
  534. {
  535. AVIContext *avi = s->priv_data;
  536. AVStream *st;
  537. AVIStream *ast;
  538. int frame_number, i;
  539. int64_t pos;
  540. if (!avi->index_loaded) {
  541. /* we only load the index on demand */
  542. avi_load_index(s);
  543. avi->index_loaded = 1;
  544. }
  545. if (stream_index < 0) {
  546. for(i = 0; i < s->nb_streams; i++) {
  547. st = s->streams[i];
  548. if (st->codec.codec_type == CODEC_TYPE_VIDEO)
  549. goto found;
  550. }
  551. return -1;
  552. found:
  553. stream_index = i;
  554. }
  555. st = s->streams[stream_index];
  556. if (st->codec.codec_type != CODEC_TYPE_VIDEO)
  557. return -1;
  558. ast = st->priv_data;
  559. /* compute the frame number */
  560. frame_number = timestamp;
  561. #ifdef DEBUG_SEEK
  562. printf("timestamp=%0.3f nb_indexes=%d frame_number=%d\n",
  563. (double)timestamp / AV_TIME_BASE,
  564. ast->nb_index_entries, frame_number);
  565. #endif
  566. /* find a closest key frame before */
  567. if (frame_number >= ast->nb_index_entries)
  568. return -1;
  569. while (frame_number >= 0 &&
  570. !(ast->index_entries[frame_number].flags & AVIIF_INDEX))
  571. frame_number--;
  572. if (frame_number < 0)
  573. return -1;
  574. ast->new_frame_offset = frame_number;
  575. /* find the position */
  576. pos = ast->index_entries[frame_number].pos;
  577. #ifdef DEBUG_SEEK
  578. printf("key_frame_number=%d pos=0x%llx\n",
  579. frame_number, pos);
  580. #endif
  581. /* update the frame counters for all the other stream by looking
  582. at the positions just after the one found */
  583. for(i = 0; i < s->nb_streams; i++) {
  584. int j;
  585. if (i != stream_index) {
  586. st = s->streams[i];
  587. ast = st->priv_data;
  588. if (ast->nb_index_entries <= 0)
  589. return -1;
  590. j = locate_frame_in_index(ast->index_entries,
  591. ast->nb_index_entries,
  592. pos);
  593. /* get next frame */
  594. if ((j + 1) < ast->nb_index_entries)
  595. j++;
  596. /* extract the current frame number */
  597. if (ast->sample_size==0)
  598. ast->new_frame_offset = j;
  599. else
  600. ast->new_frame_offset = ast->index_entries[j].cum_len;
  601. }
  602. }
  603. /* everything is OK now. We can update the frame offsets */
  604. for(i = 0; i < s->nb_streams; i++) {
  605. st = s->streams[i];
  606. ast = st->priv_data;
  607. ast->frame_offset = ast->new_frame_offset;
  608. #ifdef DEBUG_SEEK
  609. printf("%d: frame_offset=%d\n", i,
  610. ast->frame_offset);
  611. #endif
  612. }
  613. /* do the seek */
  614. pos += avi->movi_list;
  615. url_fseek(&s->pb, pos, SEEK_SET);
  616. return 0;
  617. }
  618. static int avi_read_close(AVFormatContext *s)
  619. {
  620. int i;
  621. AVIContext *avi = s->priv_data;
  622. for(i=0;i<s->nb_streams;i++) {
  623. AVStream *st = s->streams[i];
  624. AVIStream *ast = st->priv_data;
  625. if(ast){
  626. av_free(ast->index_entries);
  627. av_free(ast);
  628. }
  629. av_free(st->codec.extradata);
  630. av_free(st->codec.palctrl);
  631. }
  632. if (avi->dv_demux)
  633. av_free(avi->dv_demux);
  634. return 0;
  635. }
  636. static int avi_probe(AVProbeData *p)
  637. {
  638. /* check file header */
  639. if (p->buf_size <= 32)
  640. return 0;
  641. if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
  642. p->buf[2] == 'F' && p->buf[3] == 'F' &&
  643. p->buf[8] == 'A' && p->buf[9] == 'V' &&
  644. p->buf[10] == 'I' && p->buf[11] == ' ')
  645. return AVPROBE_SCORE_MAX;
  646. else
  647. return 0;
  648. }
  649. static AVInputFormat avi_iformat = {
  650. "avi",
  651. "avi format",
  652. sizeof(AVIContext),
  653. avi_probe,
  654. avi_read_header,
  655. avi_read_packet,
  656. avi_read_close,
  657. avi_read_seek,
  658. };
  659. int avidec_init(void)
  660. {
  661. av_register_input_format(&avi_iformat);
  662. return 0;
  663. }