3 * Copyright (c) 2001 Fabrice Bellard.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 typedef struct AVIStream {
31 int64_t frame_offset; /* current frame (video) or byte (audio) counter
32 (used to compute the pts) */
38 int sample_size; /* size of one sample (or packet) (in the rate/scale sense) in bytes */
40 int64_t cum_len; /* temporary storage (used during seek) */
42 int prefix; ///< normally 'd'<<8 + 'c' or 'w'<<8 + 'b'
54 DVDemuxContext* dv_demux;
57 static int avi_load_index(AVFormatContext *s);
58 static int guess_ni_flag(AVFormatContext *s);
61 static void print_tag(const char *str, unsigned int tag, int size)
63 printf("%s: tag=%c%c%c%c size=0x%x\n",
72 static int get_riff(AVIContext *avi, ByteIOContext *pb)
75 /* check RIFF header */
78 if (tag != MKTAG('R', 'I', 'F', 'F'))
80 avi->riff_end = get_le32(pb); /* RIFF chunk size */
81 avi->riff_end += url_ftell(pb); /* RIFF chunk end */
83 if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
89 static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
90 AVIContext *avi = s->priv_data;
91 ByteIOContext *pb = &s->pb;
92 int longs_pre_entry= get_le16(pb);
93 int index_sub_type = get_byte(pb);
94 int index_type = get_byte(pb);
95 int entries_in_use = get_le32(pb);
96 int chunk_id = get_le32(pb);
97 int64_t base = get_le64(pb);
98 int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0');
102 int64_t last_pos= -1;
104 // av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%Ld\n",
105 // longs_pre_entry,index_type, entries_in_use, chunk_id, base);
107 if(stream_id > s->nb_streams || stream_id < 0)
109 st= s->streams[stream_id];
117 if(index_type && longs_pre_entry != 2)
122 for(i=0; i<entries_in_use; i++){
124 int64_t pos= get_le32(pb) + base - 8;
125 int len = get_le32(pb);
129 //av_log(s, AV_LOG_ERROR, "pos:%Ld, len:%X\n", pos, len);
130 if(last_pos == pos || pos == base - 8)
131 avi->non_interleaved= 1;
133 av_add_index_entry(st, pos, ast->cum_len, len, 0, key ? AVINDEX_KEYFRAME : 0);
136 ast->cum_len += len / ast->sample_size;
143 offset = get_le64(pb);
144 get_le32(pb); /* size */
145 duration = get_le32(pb);
148 url_fseek(pb, offset+8, SEEK_SET);
149 read_braindead_odml_indx(s, frame_num);
150 frame_num += duration;
152 url_fseek(pb, pos, SEEK_SET);
158 static void clean_index(AVFormatContext *s){
161 for(i=0; i<s->nb_streams; i++){
162 AVStream *st = s->streams[i];
163 AVIStream *ast = st->priv_data;
164 int n= st->nb_index_entries;
165 int max= ast->sample_size;
166 int64_t pos, size, ts;
168 if(n != 1 || ast->sample_size==0)
171 while(max < 1024) max+=max;
173 pos= st->index_entries[0].pos;
174 size= st->index_entries[0].size;
175 ts= st->index_entries[0].timestamp;
177 for(j=0; j<size; j+=max){
178 av_add_index_entry(st, pos+j, ts + j/ast->sample_size, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME);
183 static int avi_read_tag(ByteIOContext *pb, char *buf, int maxlen, unsigned int size)
185 offset_t i = url_ftell(pb);
187 get_strz(pb, buf, maxlen);
188 url_fseek(pb, i+size, SEEK_SET);
192 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
194 AVIContext *avi = s->priv_data;
195 ByteIOContext *pb = &s->pb;
196 uint32_t tag, tag1, handler;
197 int codec_type, stream_index, frame_period, bit_rate;
198 unsigned int size, nb_frames;
201 AVIStream *ast = NULL;
202 int xan_video = 0; /* hack to support Xan A/V */
204 avi->stream_index= -1;
206 if (get_riff(avi, pb) < 0)
219 print_tag("tag", tag, size);
223 case MKTAG('L', 'I', 'S', 'T'):
224 /* ignored, except when start of video packets */
227 print_tag("list", tag1, 0);
229 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
230 avi->movi_list = url_ftell(pb) - 4;
231 if(size) avi->movi_end = avi->movi_list + size;
232 else avi->movi_end = url_fsize(pb);
234 printf("movi end=%Lx\n", avi->movi_end);
239 case MKTAG('d', 'm', 'l', 'h'):
241 url_fskip(pb, size + (size & 1));
243 case MKTAG('a', 'v', 'i', 'h'):
245 /* using frame_period is bad idea */
246 frame_period = get_le32(pb);
247 bit_rate = get_le32(pb) * 8;
249 avi->non_interleaved |= get_le32(pb) & AVIF_MUSTUSEINDEX;
251 url_fskip(pb, 2 * 4);
255 st = av_new_stream(s, i);
259 ast = av_mallocz(sizeof(AVIStream));
264 url_fskip(pb, size - 7 * 4);
266 case MKTAG('s', 't', 'r', 'h'):
270 handler = get_le32(pb); /* codec tag */
272 print_tag("strh", tag1, -1);
274 if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){
276 * After some consideration -- I don't think we
277 * have to support anything but DV in a type1 AVIs.
279 if (s->nb_streams != 1)
282 if (handler != MKTAG('d', 'v', 's', 'd') &&
283 handler != MKTAG('d', 'v', 'h', 'd') &&
284 handler != MKTAG('d', 'v', 's', 'l'))
287 ast = s->streams[0]->priv_data;
288 av_freep(&s->streams[0]->codec->extradata);
289 av_freep(&s->streams[0]);
291 avi->dv_demux = dv_init_demux(s);
294 s->streams[0]->priv_data = ast;
295 url_fskip(pb, 3 * 4);
296 ast->scale = get_le32(pb);
297 ast->rate = get_le32(pb);
298 stream_index = s->nb_streams - 1;
299 url_fskip(pb, size - 7*4);
303 if (stream_index >= s->nb_streams) {
304 url_fskip(pb, size - 8);
305 /* ignore padding stream */
306 if (tag1 == MKTAG('p', 'a', 'd', 's'))
310 st = s->streams[stream_index];
312 st->codec->stream_codec_tag= handler;
314 get_le32(pb); /* flags */
315 get_le16(pb); /* priority */
316 get_le16(pb); /* language */
317 get_le32(pb); /* initial frame */
318 ast->scale = get_le32(pb);
319 ast->rate = get_le32(pb);
320 if(ast->scale && ast->rate){
321 }else if(frame_period){
323 ast->scale = frame_period;
328 av_set_pts_info(st, 64, ast->scale, ast->rate);
330 ast->cum_len=get_le32(pb); /* start */
331 nb_frames = get_le32(pb);
334 st->duration = nb_frames;
335 get_le32(pb); /* buffer size */
336 get_le32(pb); /* quality */
337 ast->sample_size = get_le32(pb); /* sample ssize */
338 // av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->rate, ast->scale, ast->start, ast->sample_size);
341 case MKTAG('v', 'i', 'd', 's'):
342 codec_type = CODEC_TYPE_VIDEO;
344 ast->sample_size = 0;
346 case MKTAG('a', 'u', 'd', 's'):
347 codec_type = CODEC_TYPE_AUDIO;
349 case MKTAG('t', 'x', 't', 's'):
351 codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME
353 case MKTAG('p', 'a', 'd', 's'):
354 codec_type = CODEC_TYPE_UNKNOWN;
358 av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
361 ast->frame_offset= ast->cum_len * FFMAX(ast->sample_size, 1);
362 url_fskip(pb, size - 12 * 4);
364 case MKTAG('s', 't', 'r', 'f'):
366 if (stream_index >= s->nb_streams || avi->dv_demux) {
369 st = s->streams[stream_index];
371 case CODEC_TYPE_VIDEO:
372 get_le32(pb); /* size */
373 st->codec->width = get_le32(pb);
374 st->codec->height = get_le32(pb);
375 get_le16(pb); /* panes */
376 st->codec->bits_per_sample= get_le16(pb); /* depth */
378 get_le32(pb); /* ImageSize */
379 get_le32(pb); /* XPelsPerMeter */
380 get_le32(pb); /* YPelsPerMeter */
381 get_le32(pb); /* ClrUsed */
382 get_le32(pb); /* ClrImportant */
384 if(size > 10*4 && size<(1<<30)){
385 st->codec->extradata_size= size - 10*4;
386 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
387 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
390 if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
393 /* Extract palette from extradata if bpp <= 8 */
394 /* This code assumes that extradata contains only palette */
395 /* This is true for all paletted codecs implemented in ffmpeg */
396 if (st->codec->extradata_size && (st->codec->bits_per_sample <= 8)) {
397 st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
398 #ifdef WORDS_BIGENDIAN
399 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
400 st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
402 memcpy(st->codec->palctrl->palette, st->codec->extradata,
403 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
405 st->codec->palctrl->palette_changed = 1;
409 print_tag("video", tag1, 0);
411 st->codec->codec_type = CODEC_TYPE_VIDEO;
412 st->codec->codec_tag = tag1;
413 st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
414 if (st->codec->codec_id == CODEC_ID_XAN_WC4)
416 st->need_parsing = 2; //only parse headers dont do slower repacketization, this is needed to get the pict type which is needed for generating correct pts
417 // url_fskip(pb, size - 5 * 4);
419 case CODEC_TYPE_AUDIO:
420 get_wav_header(pb, st->codec, size);
421 if(ast->sample_size && st->codec->block_align && ast->sample_size % st->codec->block_align)
422 av_log(s, AV_LOG_DEBUG, "invalid sample size or block align detected\n");
423 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
425 /* special case time: To support Xan DPCM, hardcode
426 * the format if Xxan is the video codec */
427 st->need_parsing = 1;
428 /* force parsing as several audio frames can be in
431 st->codec->codec_id = CODEC_ID_XAN_DPCM;
434 st->codec->codec_type = CODEC_TYPE_DATA;
435 st->codec->codec_id= CODEC_ID_NONE;
436 st->codec->codec_tag= 0;
442 case MKTAG('i', 'n', 'd', 'x'):
444 if(!url_is_streamed(pb)){
445 read_braindead_odml_indx(s, 0);
448 url_fseek(pb, i+size, SEEK_SET);
450 case MKTAG('I', 'N', 'A', 'M'):
451 avi_read_tag(pb, s->title, sizeof(s->title), size);
453 case MKTAG('I', 'A', 'R', 'T'):
454 avi_read_tag(pb, s->author, sizeof(s->author), size);
456 case MKTAG('I', 'C', 'O', 'P'):
457 avi_read_tag(pb, s->copyright, sizeof(s->copyright), size);
459 case MKTAG('I', 'C', 'M', 'T'):
460 avi_read_tag(pb, s->comment, sizeof(s->comment), size);
462 case MKTAG('I', 'G', 'N', 'R'):
463 avi_read_tag(pb, s->genre, sizeof(s->genre), size);
473 /* check stream number */
474 if (stream_index != s->nb_streams - 1) {
476 for(i=0;i<s->nb_streams;i++) {
477 av_freep(&s->streams[i]->codec->extradata);
478 av_freep(&s->streams[i]);
483 if(!avi->index_loaded && !url_is_streamed(pb))
485 avi->index_loaded = 1;
486 avi->non_interleaved |= guess_ni_flag(s);
487 if(avi->non_interleaved)
493 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
495 AVIContext *avi = s->priv_data;
496 ByteIOContext *pb = &s->pb;
502 size = dv_get_packet(avi->dv_demux, pkt);
507 if(avi->non_interleaved){
508 int best_stream_index = 0;
509 AVStream *best_st= NULL;
511 int64_t best_ts= INT64_MAX;
514 for(i=0; i<s->nb_streams; i++){
515 AVStream *st = s->streams[i];
516 AVIStream *ast = st->priv_data;
517 int64_t ts= ast->frame_offset;
520 ts /= ast->sample_size;
521 ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
523 // av_log(NULL, AV_LOG_DEBUG, "%Ld %d/%d %Ld\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
527 best_stream_index= i;
530 best_ast = best_st->priv_data;
531 best_ts= av_rescale(best_ts, best_st->time_base.den, AV_TIME_BASE * (int64_t)best_st->time_base.num); //FIXME a little ugly
532 if(best_ast->remaining)
533 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
535 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
537 // av_log(NULL, AV_LOG_DEBUG, "%d\n", i);
539 int64_t pos= best_st->index_entries[i].pos;
540 pos += best_ast->packet_size - best_ast->remaining;
541 url_fseek(&s->pb, pos + 8, SEEK_SET);
542 // av_log(NULL, AV_LOG_DEBUG, "pos=%Ld\n", pos);
544 assert(best_ast->remaining <= best_ast->packet_size);
546 avi->stream_index= best_stream_index;
547 if(!best_ast->remaining)
548 best_ast->packet_size=
549 best_ast->remaining= best_st->index_entries[i].size;
554 if(avi->stream_index >= 0){
555 AVStream *st= s->streams[ avi->stream_index ];
556 AVIStream *ast= st->priv_data;
559 if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
561 else if(ast->sample_size < 32)
562 size= 64*ast->sample_size;
564 size= ast->sample_size;
566 if(size > ast->remaining)
567 size= ast->remaining;
568 av_get_packet(pb, pkt, size);
571 dstr = pkt->destruct;
572 size = dv_produce_packet(avi->dv_demux, pkt,
573 pkt->data, pkt->size);
574 pkt->destruct = dstr;
575 pkt->flags |= PKT_FLAG_KEY;
577 /* XXX: how to handle B frames in avi ? */
578 pkt->dts = ast->frame_offset;
579 // pkt->dts += ast->start;
581 pkt->dts /= ast->sample_size;
582 //av_log(NULL, AV_LOG_DEBUG, "dts:%Ld offset:%Ld %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, avi->stream_index, size);
583 pkt->stream_index = avi->stream_index;
585 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
586 if(st->index_entries){
590 index= av_index_search_timestamp(st, pkt->dts, 0);
591 e= &st->index_entries[index];
593 if(index >= 0 && e->timestamp == ast->frame_offset){
594 if (e->flags & AVINDEX_KEYFRAME)
595 pkt->flags |= PKT_FLAG_KEY;
598 /* if no index, better to say that all frames
600 pkt->flags |= PKT_FLAG_KEY;
603 pkt->flags |= PKT_FLAG_KEY;
606 ast->frame_offset += pkt->size;
610 ast->remaining -= size;
612 avi->stream_index= -1;
623 memset(d, -1, sizeof(int)*8);
624 for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
627 if (i >= avi->movi_end) {
629 url_fskip(pb, avi->riff_end - i);
630 avi->riff_end = avi->movi_end = url_fsize(pb);
639 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
641 if( d[2] >= '0' && d[2] <= '9'
642 && d[3] >= '0' && d[3] <= '9'){
643 n= (d[2] - '0') * 10 + (d[3] - '0');
645 n= 100; //invalid stream id
647 //av_log(NULL, AV_LOG_DEBUG, "%X %X %X %X %X %X %X %X %lld %d %d\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
648 if(i + size > avi->movi_end || d[0]<0)
652 if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
654 ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){
656 //av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
660 if( d[0] >= '0' && d[0] <= '9'
661 && d[1] >= '0' && d[1] <= '9'){
662 n= (d[0] - '0') * 10 + (d[1] - '0');
664 n= 100; //invalid stream id
668 if(n < s->nb_streams){
674 if( (st->discard >= AVDISCARD_DEFAULT && size==0)
675 /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering
676 || st->discard >= AVDISCARD_ALL){
677 if(ast->sample_size) ast->frame_offset += pkt->size;
678 else ast->frame_offset++;
683 if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
684 d[2]*256+d[3] == ast->prefix /*||
685 (d[2] == 'd' && d[3] == 'c') ||
686 (d[2] == 'w' && d[3] == 'b')*/) {
688 //av_log(NULL, AV_LOG_DEBUG, "OK\n");
689 if(d[2]*256+d[3] == ast->prefix)
692 ast->prefix= d[2]*256+d[3];
693 ast->prefix_count= 0;
696 avi->stream_index= n;
697 ast->packet_size= size + 8;
698 ast->remaining= size;
702 /* palette changed chunk */
703 if ( d[0] >= '0' && d[0] <= '9'
704 && d[1] >= '0' && d[1] <= '9'
705 && ((d[2] == 'p' && d[3] == 'c'))
706 && n < s->nb_streams && i + size <= avi->movi_end) {
709 int first, clr, flags, k, p;
713 first = get_byte(pb);
715 if(!clr) /* all 256 colors used */
717 flags = get_le16(pb);
719 for (k = first; k < clr + first; k++) {
725 st->codec->palctrl->palette[k] = b + (g << 8) + (r << 16);
727 st->codec->palctrl->palette_changed = 1;
736 /* XXX: we make the implicit supposition that the position are sorted
738 static int avi_read_idx1(AVFormatContext *s, int size)
740 AVIContext *avi = s->priv_data;
741 ByteIOContext *pb = &s->pb;
742 int nb_index_entries, i;
745 unsigned int index, tag, flags, pos, len;
746 unsigned last_pos= -1;
748 nb_index_entries = size / 16;
749 if (nb_index_entries <= 0)
752 /* read the entries and sort them in each stream component */
753 for(i = 0; i < nb_index_entries; i++) {
755 flags = get_le32(pb);
758 #if defined(DEBUG_SEEK)
759 av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
760 i, tag, flags, pos, len);
762 if(i==0 && pos > avi->movi_list)
763 avi->movi_list= 0; //FIXME better check
764 pos += avi->movi_list;
766 index = ((tag & 0xff) - '0') * 10;
767 index += ((tag >> 8) & 0xff) - '0';
768 if (index >= s->nb_streams)
770 st = s->streams[index];
773 #if defined(DEBUG_SEEK)
774 av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%d\n", len, ast->cum_len);
777 avi->non_interleaved= 1;
779 av_add_index_entry(st, pos, ast->cum_len, len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
781 ast->cum_len += len / ast->sample_size;
789 static int guess_ni_flag(AVFormatContext *s){
791 int64_t last_start=0;
792 int64_t first_end= INT64_MAX;
794 for(i=0; i<s->nb_streams; i++){
795 AVStream *st = s->streams[i];
796 int n= st->nb_index_entries;
801 if(st->index_entries[0].pos > last_start)
802 last_start= st->index_entries[0].pos;
803 if(st->index_entries[n-1].pos < first_end)
804 first_end= st->index_entries[n-1].pos;
806 return last_start > first_end;
809 static int avi_load_index(AVFormatContext *s)
811 AVIContext *avi = s->priv_data;
812 ByteIOContext *pb = &s->pb;
814 offset_t pos= url_ftell(pb);
816 url_fseek(pb, avi->movi_end, SEEK_SET);
818 printf("movi_end=0x%llx\n", avi->movi_end);
826 printf("tag=%c%c%c%c size=0x%x\n",
834 case MKTAG('i', 'd', 'x', '1'):
835 if (avi_read_idx1(s, size) < 0)
848 url_fseek(pb, pos, SEEK_SET);
852 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
854 AVIContext *avi = s->priv_data;
859 if (!avi->index_loaded) {
860 /* we only load the index on demand */
862 avi->index_loaded = 1;
864 assert(stream_index>= 0);
866 st = s->streams[stream_index];
867 index= av_index_search_timestamp(st, timestamp, flags);
871 /* find the position */
872 pos = st->index_entries[index].pos;
873 timestamp = st->index_entries[index].timestamp;
875 // av_log(NULL, AV_LOG_DEBUG, "XX %Ld %d %Ld\n", timestamp, index, st->index_entries[index].timestamp);
877 for(i = 0; i < s->nb_streams; i++) {
878 AVStream *st2 = s->streams[i];
879 AVIStream *ast2 = st2->priv_data;
884 if (st2->nb_index_entries <= 0)
887 // assert(st2->codec->block_align);
888 assert(st2->time_base.den == ast2->rate);
889 assert(st2->time_base.num == ast2->scale);
890 index = av_index_search_timestamp(
892 av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
893 flags | AVSEEK_FLAG_BACKWARD);
897 if(!avi->non_interleaved){
898 while(index>0 && st2->index_entries[index].pos > pos)
900 while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
904 // av_log(NULL, AV_LOG_DEBUG, "%Ld %d %Ld\n", timestamp, index, st2->index_entries[index].timestamp);
905 /* extract the current frame number */
906 ast2->frame_offset = st2->index_entries[index].timestamp;
907 if(ast2->sample_size)
908 ast2->frame_offset *=ast2->sample_size;
912 dv_flush_audio_packets(avi->dv_demux);
914 url_fseek(&s->pb, pos, SEEK_SET);
915 avi->stream_index= -1;
919 static int avi_read_close(AVFormatContext *s)
922 AVIContext *avi = s->priv_data;
924 for(i=0;i<s->nb_streams;i++) {
925 AVStream *st = s->streams[i];
926 AVIStream *ast = st->priv_data;
928 av_free(st->codec->palctrl);
932 av_free(avi->dv_demux);
937 static int avi_probe(AVProbeData *p)
939 /* check file header */
940 if (p->buf_size <= 32)
942 if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
943 p->buf[2] == 'F' && p->buf[3] == 'F' &&
944 p->buf[8] == 'A' && p->buf[9] == 'V' &&
945 p->buf[10] == 'I' && p->buf[11] == ' ')
946 return AVPROBE_SCORE_MAX;
951 AVInputFormat avi_demuxer = {