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.

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