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.

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