3 * Copyright (c) 2001 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/bswap.h"
35 typedef struct AVIStream {
36 int64_t frame_offset; /* current frame (video) or byte (audio) counter
37 (used to compute the pts) */
43 int sample_size; /* size of one sample (or packet) (in the rate/scale sense) in bytes */
45 int64_t cum_len; /* temporary storage (used during seek) */
47 int prefix; ///< normally 'd'<<8 + 'c' or 'w'<<8 + 'b'
63 DVDemuxContext* dv_demux;
66 static const char avi_headers[][8] = {
67 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', ' ' },
68 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 'X' },
69 { 'R', 'I', 'F', 'F', 'A', 'V', 'I', 0x19},
70 { 'O', 'N', '2', ' ', 'O', 'N', '2', 'f' },
71 { 'R', 'I', 'F', 'F', 'A', 'M', 'V', ' ' },
75 static int avi_load_index(AVFormatContext *s);
76 static int guess_ni_flag(AVFormatContext *s);
79 static void print_tag(const char *str, unsigned int tag, int size)
81 printf("%s: tag=%c%c%c%c size=0x%x\n",
90 static int get_riff(AVIContext *avi, ByteIOContext *pb)
95 /* check RIFF header */
96 get_buffer(pb, header, 4);
97 avi->riff_end = get_le32(pb); /* RIFF chunk size */
98 avi->riff_end += url_ftell(pb); /* RIFF chunk end */
99 get_buffer(pb, header+4, 4);
101 for(i=0; avi_headers[i][0]; i++)
102 if(!memcmp(header, avi_headers[i], 8))
104 if(!avi_headers[i][0])
107 if(header[7] == 0x19)
108 av_log(NULL, AV_LOG_INFO, "This file has been generated by a totally broken muxer.\n");
113 static int read_braindead_odml_indx(AVFormatContext *s, int frame_num){
114 AVIContext *avi = s->priv_data;
115 ByteIOContext *pb = s->pb;
116 int longs_pre_entry= get_le16(pb);
117 int index_sub_type = get_byte(pb);
118 int index_type = get_byte(pb);
119 int entries_in_use = get_le32(pb);
120 int chunk_id = get_le32(pb);
121 int64_t base = get_le64(pb);
122 int stream_id= 10*((chunk_id&0xFF) - '0') + (((chunk_id>>8)&0xFF) - '0');
126 int64_t last_pos= -1;
127 int64_t filesize= url_fsize(s->pb);
130 av_log(s, AV_LOG_ERROR, "longs_pre_entry:%d index_type:%d entries_in_use:%d chunk_id:%X base:%16"PRIX64"\n",
131 longs_pre_entry,index_type, entries_in_use, chunk_id, base);
134 if(stream_id > s->nb_streams || stream_id < 0)
136 st= s->streams[stream_id];
144 if(index_type && longs_pre_entry != 2)
149 if(filesize > 0 && base >= filesize){
150 av_log(s, AV_LOG_ERROR, "ODML index invalid\n");
151 if(base>>32 == (base & 0xFFFFFFFF) && (base & 0xFFFFFFFF) < filesize && filesize <= 0xFFFFFFFF)
157 for(i=0; i<entries_in_use; i++){
159 int64_t pos= get_le32(pb) + base - 8;
160 int len = get_le32(pb);
165 av_log(s, AV_LOG_ERROR, "pos:%"PRId64", len:%X\n", pos, len);
167 if(last_pos == pos || pos == base - 8)
168 avi->non_interleaved= 1;
170 av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, key ? AVINDEX_KEYFRAME : 0);
180 offset = get_le64(pb);
181 get_le32(pb); /* size */
182 duration = get_le32(pb);
185 url_fseek(pb, offset+8, SEEK_SET);
186 read_braindead_odml_indx(s, frame_num);
187 frame_num += duration;
189 url_fseek(pb, pos, SEEK_SET);
196 static void clean_index(AVFormatContext *s){
200 for(i=0; i<s->nb_streams; i++){
201 AVStream *st = s->streams[i];
202 AVIStream *ast = st->priv_data;
203 int n= st->nb_index_entries;
204 int max= ast->sample_size;
205 int64_t pos, size, ts;
207 if(n != 1 || ast->sample_size==0)
210 while(max < 1024) max+=max;
212 pos= st->index_entries[0].pos;
213 size= st->index_entries[0].size;
214 ts= st->index_entries[0].timestamp;
216 for(j=0; j<size; j+=max){
217 av_add_index_entry(st, pos+j, ts + j/ast->sample_size, FFMIN(max, size-j), 0, AVINDEX_KEYFRAME);
222 static int avi_read_tag(AVFormatContext *s, const char *key, unsigned int size)
224 ByteIOContext *pb = s->pb;
227 int64_t i = url_ftell(pb);
229 get_strz(pb, value, sizeof(value));
230 url_fseek(pb, i+size, SEEK_SET);
232 return av_metadata_set(&s->metadata, key, value);
235 static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
237 AVIContext *avi = s->priv_data;
238 ByteIOContext *pb = s->pb;
239 unsigned int tag, tag1, handler;
240 int codec_type, stream_index, frame_period, bit_rate;
241 unsigned int size, nb_frames;
244 AVIStream *ast = NULL;
245 int avih_width=0, avih_height=0;
246 int amv_file_format=0;
248 avi->stream_index= -1;
250 if (get_riff(avi, pb) < 0)
253 avi->fsize = url_fsize(pb);
255 avi->fsize= avi->riff_end;
267 print_tag("tag", tag, size);
271 case MKTAG('L', 'I', 'S', 'T'):
272 /* Ignored, except at start of video packets. */
275 print_tag("list", tag1, 0);
277 if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
278 avi->movi_list = url_ftell(pb) - 4;
279 if(size) avi->movi_end = avi->movi_list + size + (size & 1);
280 else avi->movi_end = url_fsize(pb);
282 printf("movi end=%"PRIx64"\n", avi->movi_end);
287 case MKTAG('d', 'm', 'l', 'h'):
289 url_fskip(pb, size + (size & 1));
291 case MKTAG('a', 'm', 'v', 'h'):
293 case MKTAG('a', 'v', 'i', 'h'):
295 /* using frame_period is bad idea */
296 frame_period = get_le32(pb);
297 bit_rate = get_le32(pb) * 8;
299 avi->non_interleaved |= get_le32(pb) & AVIF_MUSTUSEINDEX;
301 url_fskip(pb, 2 * 4);
304 avih_width=get_le32(pb);
305 avih_height=get_le32(pb);
307 url_fskip(pb, size - 10 * 4);
309 case MKTAG('s', 't', 'r', 'h'):
313 handler = get_le32(pb); /* codec tag */
315 if(tag1 == MKTAG('p', 'a', 'd', 's')){
316 url_fskip(pb, size - 8);
320 st = av_new_stream(s, stream_index);
324 ast = av_mallocz(sizeof(AVIStream));
330 tag1 = stream_index ? MKTAG('a','u','d','s') : MKTAG('v','i','d','s');
333 print_tag("strh", tag1, -1);
335 if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){
339 * After some consideration -- I don't think we
340 * have to support anything but DV in type1 AVIs.
342 if (s->nb_streams != 1)
345 if (handler != MKTAG('d', 'v', 's', 'd') &&
346 handler != MKTAG('d', 'v', 'h', 'd') &&
347 handler != MKTAG('d', 'v', 's', 'l'))
350 ast = s->streams[0]->priv_data;
351 av_freep(&s->streams[0]->codec->extradata);
352 av_freep(&s->streams[0]);
354 if (CONFIG_DV_DEMUXER) {
355 avi->dv_demux = dv_init_demux(s);
359 s->streams[0]->priv_data = ast;
360 url_fskip(pb, 3 * 4);
361 ast->scale = get_le32(pb);
362 ast->rate = get_le32(pb);
363 url_fskip(pb, 4); /* start time */
365 dv_dur = get_le32(pb);
366 if (ast->scale > 0 && ast->rate > 0 && dv_dur > 0) {
367 dv_dur *= AV_TIME_BASE;
368 s->duration = av_rescale(dv_dur, ast->scale, ast->rate);
371 * else, leave duration alone; timing estimation in utils.c
372 * will make a guess based on bitrate.
375 stream_index = s->nb_streams - 1;
376 url_fskip(pb, size - 9*4);
380 assert(stream_index < s->nb_streams);
381 st->codec->stream_codec_tag= handler;
383 get_le32(pb); /* flags */
384 get_le16(pb); /* priority */
385 get_le16(pb); /* language */
386 get_le32(pb); /* initial frame */
387 ast->scale = get_le32(pb);
388 ast->rate = get_le32(pb);
389 if(!(ast->scale && ast->rate)){
390 av_log(s, AV_LOG_WARNING, "scale/rate is %u/%u which is invalid. (This file has been generated by broken software.)\n", ast->scale, ast->rate);
393 ast->scale = frame_period;
399 av_set_pts_info(st, 64, ast->scale, ast->rate);
401 ast->cum_len=get_le32(pb); /* start */
402 nb_frames = get_le32(pb);
405 st->duration = nb_frames;
406 get_le32(pb); /* buffer size */
407 get_le32(pb); /* quality */
408 ast->sample_size = get_le32(pb); /* sample ssize */
409 ast->cum_len *= FFMAX(1, ast->sample_size);
410 // av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->rate, ast->scale, ast->start, ast->sample_size);
413 case MKTAG('v', 'i', 'd', 's'):
414 codec_type = CODEC_TYPE_VIDEO;
416 ast->sample_size = 0;
418 case MKTAG('a', 'u', 'd', 's'):
419 codec_type = CODEC_TYPE_AUDIO;
421 case MKTAG('t', 'x', 't', 's'):
423 codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME
425 case MKTAG('d', 'a', 't', 's'):
426 codec_type = CODEC_TYPE_DATA;
429 av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
432 ast->frame_offset= ast->cum_len;
433 url_fskip(pb, size - 12 * 4);
435 case MKTAG('s', 't', 'r', 'f'):
437 if (stream_index >= (unsigned)s->nb_streams || avi->dv_demux) {
440 st = s->streams[stream_index];
442 case CODEC_TYPE_VIDEO:
444 st->codec->width=avih_width;
445 st->codec->height=avih_height;
446 st->codec->codec_type = CODEC_TYPE_VIDEO;
447 st->codec->codec_id = CODEC_ID_AMV;
451 get_le32(pb); /* size */
452 st->codec->width = get_le32(pb);
453 st->codec->height = get_le32(pb);
454 get_le16(pb); /* panes */
455 st->codec->bits_per_coded_sample= get_le16(pb); /* depth */
457 get_le32(pb); /* ImageSize */
458 get_le32(pb); /* XPelsPerMeter */
459 get_le32(pb); /* YPelsPerMeter */
460 get_le32(pb); /* ClrUsed */
461 get_le32(pb); /* ClrImportant */
463 if (tag1 == MKTAG('D', 'X', 'S', 'B')) {
464 st->codec->codec_type = CODEC_TYPE_SUBTITLE;
465 st->codec->codec_tag = tag1;
466 st->codec->codec_id = CODEC_ID_XSUB;
470 if(size > 10*4 && size<(1<<30)){
471 st->codec->extradata_size= size - 10*4;
472 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
473 get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
476 if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
479 /* Extract palette from extradata if bpp <= 8. */
480 /* This code assumes that extradata contains only palette. */
481 /* This is true for all paletted codecs implemented in FFmpeg. */
482 if (st->codec->extradata_size && (st->codec->bits_per_coded_sample <= 8)) {
483 st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
484 #ifdef WORDS_BIGENDIAN
485 for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
486 st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
488 memcpy(st->codec->palctrl->palette, st->codec->extradata,
489 FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
491 st->codec->palctrl->palette_changed = 1;
495 print_tag("video", tag1, 0);
497 st->codec->codec_type = CODEC_TYPE_VIDEO;
498 st->codec->codec_tag = tag1;
499 st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
500 st->need_parsing = AVSTREAM_PARSE_HEADERS; // This is needed to get the pict type which is necessary for generating correct pts.
501 // url_fskip(pb, size - 5 * 4);
503 case CODEC_TYPE_AUDIO:
504 get_wav_header(pb, st->codec, size);
505 if(ast->sample_size && st->codec->block_align && ast->sample_size != st->codec->block_align){
506 av_log(s, AV_LOG_WARNING, "sample size (%d) != block align (%d)\n", ast->sample_size, st->codec->block_align);
507 ast->sample_size= st->codec->block_align;
509 if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
511 /* Force parsing as several audio frames can be in
512 * one packet and timestamps refer to packet start. */
513 st->need_parsing = AVSTREAM_PARSE_TIMESTAMPS;
514 /* ADTS header is in extradata, AAC without header must be
515 * stored as exact frames. Parser not needed and it will
517 if (st->codec->codec_id == CODEC_ID_AAC && st->codec->extradata_size)
518 st->need_parsing = AVSTREAM_PARSE_NONE;
519 /* AVI files with Xan DPCM audio (wrongly) declare PCM
520 * audio in the header but have Axan as stream_code_tag. */
521 if (st->codec->stream_codec_tag == AV_RL32("Axan")){
522 st->codec->codec_id = CODEC_ID_XAN_DPCM;
523 st->codec->codec_tag = 0;
526 st->codec->codec_id = CODEC_ID_ADPCM_IMA_AMV;
529 st->codec->codec_type = CODEC_TYPE_DATA;
530 st->codec->codec_id= CODEC_ID_NONE;
531 st->codec->codec_tag= 0;
537 case MKTAG('i', 'n', 'd', 'x'):
539 if(!url_is_streamed(pb) && !(s->flags & AVFMT_FLAG_IGNIDX)){
540 read_braindead_odml_indx(s, 0);
542 url_fseek(pb, i+size, SEEK_SET);
544 case MKTAG('v', 'p', 'r', 'p'):
545 if(stream_index < (unsigned)s->nb_streams && size > 9*4){
546 AVRational active, active_aspect;
548 st = s->streams[stream_index];
555 active_aspect.den= get_le16(pb);
556 active_aspect.num= get_le16(pb);
557 active.num = get_le32(pb);
558 active.den = get_le32(pb);
559 get_le32(pb); //nbFieldsPerFrame
561 if(active_aspect.num && active_aspect.den && active.num && active.den){
562 st->sample_aspect_ratio= av_div_q(active_aspect, active);
563 //av_log(s, AV_LOG_ERROR, "vprp %d/%d %d/%d\n", active_aspect.num, active_aspect.den, active.num, active.den);
567 url_fseek(pb, size, SEEK_CUR);
569 case MKTAG('I', 'N', 'A', 'M'):
570 avi_read_tag(s, "Title", size);
572 case MKTAG('I', 'A', 'R', 'T'):
573 avi_read_tag(s, "Artist", size);
575 case MKTAG('I', 'C', 'O', 'P'):
576 avi_read_tag(s, "Copyright", size);
578 case MKTAG('I', 'C', 'M', 'T'):
579 avi_read_tag(s, "Comment", size);
581 case MKTAG('I', 'G', 'N', 'R'):
582 avi_read_tag(s, "Genre", size);
584 case MKTAG('I', 'P', 'R', 'D'):
585 avi_read_tag(s, "Album", size);
587 case MKTAG('I', 'P', 'R', 'T'):
588 avi_read_tag(s, "Track", size);
592 av_log(s, AV_LOG_ERROR, "Something went wrong during header parsing, "
593 "I will ignore it and try to continue anyway.\n");
594 avi->movi_list = url_ftell(pb) - 4;
595 avi->movi_end = url_fsize(pb);
605 /* check stream number */
606 if (stream_index != s->nb_streams - 1) {
611 if(!avi->index_loaded && !url_is_streamed(pb))
613 avi->index_loaded = 1;
614 avi->non_interleaved |= guess_ni_flag(s);
615 if(avi->non_interleaved) {
616 av_log(s, AV_LOG_INFO, "non-interleaved AVI\n");
623 static int get_stream_idx(int *d){
624 if( d[0] >= '0' && d[0] <= '9'
625 && d[1] >= '0' && d[1] <= '9'){
626 return (d[0] - '0') * 10 + (d[1] - '0');
628 return 100; //invalid stream ID
632 static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
634 AVIContext *avi = s->priv_data;
635 ByteIOContext *pb = s->pb;
640 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
641 size = dv_get_packet(avi->dv_demux, pkt);
646 if(avi->non_interleaved){
647 int best_stream_index = 0;
648 AVStream *best_st= NULL;
650 int64_t best_ts= INT64_MAX;
653 for(i=0; i<s->nb_streams; i++){
654 AVStream *st = s->streams[i];
655 AVIStream *ast = st->priv_data;
656 int64_t ts= ast->frame_offset;
659 ts /= ast->sample_size;
660 ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
662 // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d/%d %"PRId64"\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
663 if(ts < best_ts && st->nb_index_entries){
666 best_stream_index= i;
672 best_ast = best_st->priv_data;
673 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
674 if(best_ast->remaining)
675 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
677 i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
679 best_ast->frame_offset= best_st->index_entries[i].timestamp
680 * FFMAX(1, best_ast->sample_size);
683 // av_log(NULL, AV_LOG_DEBUG, "%d\n", i);
685 int64_t pos= best_st->index_entries[i].pos;
686 pos += best_ast->packet_size - best_ast->remaining;
687 url_fseek(s->pb, pos + 8, SEEK_SET);
688 // av_log(NULL, AV_LOG_DEBUG, "pos=%"PRId64"\n", pos);
690 assert(best_ast->remaining <= best_ast->packet_size);
692 avi->stream_index= best_stream_index;
693 if(!best_ast->remaining)
694 best_ast->packet_size=
695 best_ast->remaining= best_st->index_entries[i].size;
700 if(avi->stream_index >= 0){
701 AVStream *st= s->streams[ avi->stream_index ];
702 AVIStream *ast= st->priv_data;
705 if(ast->sample_size <= 1) // minorityreport.AVI block_align=1024 sample_size=1 IMA-ADPCM
707 else if(ast->sample_size < 32)
708 size= 64*ast->sample_size;
710 size= ast->sample_size;
712 if(size > ast->remaining)
713 size= ast->remaining;
714 avi->last_pkt_pos= url_ftell(pb);
715 av_get_packet(pb, pkt, size);
717 if(ast->has_pal && pkt->data && pkt->size<(unsigned)INT_MAX/2){
720 pkt->data = av_realloc(pkt->data, pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
722 memcpy(pkt->data + pkt->size - 4*256, ast->pal, 4*256);
725 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
726 dstr = pkt->destruct;
727 size = dv_produce_packet(avi->dv_demux, pkt,
728 pkt->data, pkt->size);
729 pkt->destruct = dstr;
730 pkt->flags |= PKT_FLAG_KEY;
732 /* XXX: How to handle B-frames in AVI? */
733 pkt->dts = ast->frame_offset;
734 // pkt->dts += ast->start;
736 pkt->dts /= ast->sample_size;
737 //av_log(NULL, AV_LOG_DEBUG, "dts:%"PRId64" offset:%"PRId64" %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);
738 pkt->stream_index = avi->stream_index;
740 if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
743 assert(st->index_entries);
745 index= av_index_search_timestamp(st, pkt->dts, 0);
746 e= &st->index_entries[index];
748 if(index >= 0 && e->timestamp == ast->frame_offset){
749 if (e->flags & AVINDEX_KEYFRAME)
750 pkt->flags |= PKT_FLAG_KEY;
753 pkt->flags |= PKT_FLAG_KEY;
756 ast->frame_offset += pkt->size;
760 ast->remaining -= size;
762 avi->stream_index= -1;
769 memset(d, -1, sizeof(int)*8);
770 for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
777 size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
779 n= get_stream_idx(d+2);
780 //av_log(NULL, AV_LOG_DEBUG, "%X %X %X %X %X %X %X %X %"PRId64" %d %d\n", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], i, size, n);
781 if(i + size > avi->fsize || d[0]<0)
785 if( (d[0] == 'i' && d[1] == 'x' && n < s->nb_streams)
787 ||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')
788 ||(d[0] == 'i' && d[1] == 'd' && d[2] == 'x' && d[3] == '1')){
790 //av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
794 n= get_stream_idx(d);
796 if(!((i-avi->last_pkt_pos)&1) && get_stream_idx(d+1) < s->nb_streams)
800 if(n < s->nb_streams){
806 if(s->nb_streams>=2){
807 AVStream *st1 = s->streams[1];
808 AVIStream *ast1= st1->priv_data;
809 //workaround for broken small-file-bug402.avi
810 if( d[2] == 'w' && d[3] == 'b'
812 && st ->codec->codec_type == CODEC_TYPE_VIDEO
813 && st1->codec->codec_type == CODEC_TYPE_AUDIO
814 && ast->prefix == 'd'*256+'c'
815 && (d[2]*256+d[3] == ast1->prefix || !ast1->prefix_count)
820 av_log(s, AV_LOG_WARNING, "Invalid stream + prefix combination, assuming audio.\n");
825 if( (st->discard >= AVDISCARD_DEFAULT && size==0)
826 /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering
827 || st->discard >= AVDISCARD_ALL){
828 if(ast->sample_size) ast->frame_offset += pkt->size;
829 else ast->frame_offset++;
834 if (d[2] == 'p' && d[3] == 'c' && size<=4*256+4) {
835 int k = get_byte(pb);
836 int last = (k + get_byte(pb) - 1) & 0xFF;
838 get_le16(pb); //flags
840 for (; k <= last; k++)
841 ast->pal[k] = get_be32(pb)>>8;// b + (g << 8) + (r << 16);
844 } else if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
845 d[2]*256+d[3] == ast->prefix /*||
846 (d[2] == 'd' && d[3] == 'c') ||
847 (d[2] == 'w' && d[3] == 'b')*/) {
849 //av_log(NULL, AV_LOG_DEBUG, "OK\n");
850 if(d[2]*256+d[3] == ast->prefix)
853 ast->prefix= d[2]*256+d[3];
854 ast->prefix_count= 0;
857 avi->stream_index= n;
858 ast->packet_size= size + 8;
859 ast->remaining= size;
862 uint64_t pos= url_ftell(pb) - 8;
863 if(!st->index_entries || !st->nb_index_entries || st->index_entries[st->nb_index_entries - 1].pos < pos){
864 av_add_index_entry(st, pos, ast->frame_offset / FFMAX(1, ast->sample_size), size, 0, AVINDEX_KEYFRAME);
875 /* XXX: We make the implicit supposition that the positions are sorted
877 static int avi_read_idx1(AVFormatContext *s, int size)
879 AVIContext *avi = s->priv_data;
880 ByteIOContext *pb = s->pb;
881 int nb_index_entries, i;
884 unsigned int index, tag, flags, pos, len;
885 unsigned last_pos= -1;
887 nb_index_entries = size / 16;
888 if (nb_index_entries <= 0)
891 /* Read the entries and sort them in each stream component. */
892 for(i = 0; i < nb_index_entries; i++) {
894 flags = get_le32(pb);
897 #if defined(DEBUG_SEEK)
898 av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
899 i, tag, flags, pos, len);
901 if(i==0 && pos > avi->movi_list)
902 avi->movi_list= 0; //FIXME better check
903 pos += avi->movi_list;
905 index = ((tag & 0xff) - '0') * 10;
906 index += ((tag >> 8) & 0xff) - '0';
907 if (index >= s->nb_streams)
909 st = s->streams[index];
912 #if defined(DEBUG_SEEK)
913 av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%"PRId64"\n", len, ast->cum_len);
916 avi->non_interleaved= 1;
918 av_add_index_entry(st, pos, ast->cum_len / FFMAX(1, ast->sample_size), len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
928 static int guess_ni_flag(AVFormatContext *s){
930 int64_t last_start=0;
931 int64_t first_end= INT64_MAX;
933 for(i=0; i<s->nb_streams; i++){
934 AVStream *st = s->streams[i];
935 int n= st->nb_index_entries;
940 if(st->index_entries[0].pos > last_start)
941 last_start= st->index_entries[0].pos;
942 if(st->index_entries[n-1].pos < first_end)
943 first_end= st->index_entries[n-1].pos;
945 return last_start > first_end;
948 static int avi_load_index(AVFormatContext *s)
950 AVIContext *avi = s->priv_data;
951 ByteIOContext *pb = s->pb;
953 int64_t pos= url_ftell(pb);
955 url_fseek(pb, avi->movi_end, SEEK_SET);
957 printf("movi_end=0x%"PRIx64"\n", avi->movi_end);
965 printf("tag=%c%c%c%c size=0x%x\n",
973 case MKTAG('i', 'd', 'x', '1'):
974 if (avi_read_idx1(s, size) < 0)
987 url_fseek(pb, pos, SEEK_SET);
991 static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
993 AVIContext *avi = s->priv_data;
998 if (!avi->index_loaded) {
999 /* we only load the index on demand */
1001 avi->index_loaded = 1;
1003 assert(stream_index>= 0);
1005 st = s->streams[stream_index];
1006 index= av_index_search_timestamp(st, timestamp, flags);
1010 /* find the position */
1011 pos = st->index_entries[index].pos;
1012 timestamp = st->index_entries[index].timestamp;
1014 // av_log(NULL, AV_LOG_DEBUG, "XX %"PRId64" %d %"PRId64"\n", timestamp, index, st->index_entries[index].timestamp);
1016 if (CONFIG_DV_DEMUXER && avi->dv_demux) {
1017 /* One and only one real stream for DV in AVI, and it has video */
1018 /* offsets. Calling with other stream indexes should have failed */
1019 /* the av_index_search_timestamp call above. */
1020 assert(stream_index == 0);
1022 /* Feed the DV video stream version of the timestamp to the */
1023 /* DV demux so it can synthesize correct timestamps. */
1024 dv_offset_reset(avi->dv_demux, timestamp);
1026 url_fseek(s->pb, pos, SEEK_SET);
1027 avi->stream_index= -1;
1031 for(i = 0; i < s->nb_streams; i++) {
1032 AVStream *st2 = s->streams[i];
1033 AVIStream *ast2 = st2->priv_data;
1038 if (st2->nb_index_entries <= 0)
1041 // assert(st2->codec->block_align);
1042 assert((int64_t)st2->time_base.num*ast2->rate == (int64_t)st2->time_base.den*ast2->scale);
1043 index = av_index_search_timestamp(
1045 av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
1046 flags | AVSEEK_FLAG_BACKWARD);
1050 if(!avi->non_interleaved){
1051 while(index>0 && st2->index_entries[index].pos > pos)
1053 while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
1057 // av_log(NULL, AV_LOG_DEBUG, "%"PRId64" %d %"PRId64"\n", timestamp, index, st2->index_entries[index].timestamp);
1058 /* extract the current frame number */
1059 ast2->frame_offset = st2->index_entries[index].timestamp;
1060 if(ast2->sample_size)
1061 ast2->frame_offset *=ast2->sample_size;
1065 url_fseek(s->pb, pos, SEEK_SET);
1066 avi->stream_index= -1;
1070 static int avi_read_close(AVFormatContext *s)
1073 AVIContext *avi = s->priv_data;
1075 for(i=0;i<s->nb_streams;i++) {
1076 AVStream *st = s->streams[i];
1077 av_free(st->codec->palctrl);
1081 av_free(avi->dv_demux);
1086 static int avi_probe(AVProbeData *p)
1090 /* check file header */
1091 for(i=0; avi_headers[i][0]; i++)
1092 if(!memcmp(p->buf , avi_headers[i] , 4) &&
1093 !memcmp(p->buf+8, avi_headers[i]+4, 4))
1094 return AVPROBE_SCORE_MAX;
1099 AVInputFormat avi_demuxer = {
1101 NULL_IF_CONFIG_SMALL("AVI format"),