#include "avi.h"
#include "dv.h"
+#undef NDEBUG
+#include <assert.h>
+
//#define DEBUG
//#define DEBUG_SEEK
-typedef struct AVIIndexEntry {
- unsigned int flags;
- unsigned int pos;
- unsigned int cum_len; /* sum of all lengths before this packet */
-} AVIIndexEntry;
-
typedef struct AVIStream {
- AVIIndexEntry *index_entries;
- int nb_index_entries;
- int index_entries_allocated_size;
- int frame_offset; /* current frame (video) or byte (audio) counter
+ int64_t frame_offset; /* current frame (video) or byte (audio) counter
(used to compute the pts) */
+ int remaining;
+ int packet_size;
+
int scale;
int rate;
int sample_size; /* audio only data */
int start;
- int new_frame_offset; /* temporary storage (used during seek) */
int cum_len; /* temporary storage (used during seek) */
int prefix; ///< normally 'd'<<8 + 'c' or 'w'<<8 + 'b'
offset_t movi_list;
int index_loaded;
int is_odml;
+ int non_interleaved;
+ int stream_index;
DVDemuxContext* dv_demux;
} AVIContext;
AVIContext *avi = s->priv_data;
ByteIOContext *pb = &s->pb;
uint32_t tag, tag1, handler;
- int codec_type, stream_index, frame_period, bit_rate, scale, rate;
+ int codec_type, stream_index, frame_period, bit_rate;
unsigned int size, nb_frames;
int i, n;
AVStream *st;
AVIStream *ast;
int xan_video = 0; /* hack to support Xan A/V */
+ avi->stream_index= -1;
+
if (get_riff(avi, pb) < 0)
return -1;
if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
avi->movi_list = url_ftell(pb) - 4;
if(size) avi->movi_end = avi->movi_list + size;
- else avi->movi_end = url_filesize(url_fileno(pb));
+ else avi->movi_end = url_fsize(pb);
#ifdef DEBUG
printf("movi end=%Lx\n", avi->movi_end);
#endif
#ifdef DEBUG
print_tag("strh", tag1, -1);
#endif
- switch(tag1) {
- case MKTAG('i', 'a', 'v', 's'):
- case MKTAG('i', 'v', 'a', 's'):
+ if(tag1 == MKTAG('i', 'a', 'v', 's') || tag1 == MKTAG('i', 'v', 'a', 's')){
/*
* After some consideration -- I don't think we
* have to support anything but DV in a type1 AVIs.
handler != MKTAG('d', 'v', 's', 'l'))
goto fail;
- av_freep(&s->streams[0]->codec.extradata);
+ ast = s->streams[0]->priv_data;
+ av_freep(&s->streams[0]->codec->extradata);
av_freep(&s->streams[0]);
s->nb_streams = 0;
avi->dv_demux = dv_init_demux(s);
if (!avi->dv_demux)
goto fail;
+ s->streams[0]->priv_data = ast;
+ url_fskip(pb, 3 * 4);
+ ast->scale = get_le32(pb);
+ ast->rate = get_le32(pb);
stream_index = s->nb_streams - 1;
- url_fskip(pb, size - 8);
- break;
+ url_fskip(pb, size - 7*4);
+ break;
+ }
+
+ if (stream_index >= s->nb_streams) {
+ url_fskip(pb, size - 8);
+ break;
+ }
+ st = s->streams[stream_index];
+ ast = st->priv_data;
+ st->codec->stream_codec_tag= handler;
+
+ get_le32(pb); /* flags */
+ get_le16(pb); /* priority */
+ get_le16(pb); /* language */
+ get_le32(pb); /* initial frame */
+ ast->scale = get_le32(pb);
+ ast->rate = get_le32(pb);
+ if(ast->scale && ast->rate){
+ }else if(frame_period){
+ ast->rate = 1000000;
+ ast->scale = frame_period;
+ }else{
+ ast->rate = 25;
+ ast->scale = 1;
+ }
+ av_set_pts_info(st, 64, ast->scale, ast->rate);
+
+ ast->start= get_le32(pb); /* start */
+ nb_frames = get_le32(pb);
+
+ st->start_time = 0;
+ st->duration = nb_frames;
+ get_le32(pb); /* buffer size */
+ get_le32(pb); /* quality */
+ ast->sample_size = get_le32(pb); /* sample ssize */
+// av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->rate, ast->scale, ast->start, ast->sample_size);
+
+ switch(tag1) {
case MKTAG('v', 'i', 'd', 's'):
codec_type = CODEC_TYPE_VIDEO;
- if (stream_index >= s->nb_streams) {
- url_fskip(pb, size - 8);
- break;
- }
-
- st = s->streams[stream_index];
- ast = st->priv_data;
- st->codec.stream_codec_tag= handler;
-
- get_le32(pb); /* flags */
- get_le16(pb); /* priority */
- get_le16(pb); /* language */
- get_le32(pb); /* XXX: initial frame ? */
- scale = get_le32(pb); /* scale */
- rate = get_le32(pb); /* rate */
-
- if(scale && rate){
- }else if(frame_period){
- rate = 1000000;
- scale = frame_period;
- }else{
- rate = 25;
- scale = 1;
- }
- ast->rate = rate;
- ast->scale = scale;
- av_set_pts_info(st, 64, scale, rate);
- st->codec.frame_rate = rate;
- st->codec.frame_rate_base = scale;
- get_le32(pb); /* start */
- nb_frames = get_le32(pb);
- st->start_time = 0;
- st->duration = av_rescale(nb_frames,
- st->codec.frame_rate_base * AV_TIME_BASE,
- st->codec.frame_rate);
- url_fskip(pb, size - 9 * 4);
+ ast->sample_size = 0;
break;
case MKTAG('a', 'u', 'd', 's'):
- {
- unsigned int length;
-
- codec_type = CODEC_TYPE_AUDIO;
-
- if (stream_index >= s->nb_streams) {
- url_fskip(pb, size - 8);
- break;
- }
- st = s->streams[stream_index];
- ast = st->priv_data;
-
- get_le32(pb); /* flags */
- get_le16(pb); /* priority */
- get_le16(pb); /* language */
- get_le32(pb); /* initial frame */
- ast->scale = get_le32(pb); /* scale */
- ast->rate = get_le32(pb);
- av_set_pts_info(st, 64, ast->scale, ast->rate);
- ast->start= get_le32(pb); /* start */
- length = get_le32(pb); /* length, in samples or bytes */
- get_le32(pb); /* buffer size */
- get_le32(pb); /* quality */
- ast->sample_size = get_le32(pb); /* sample ssize */
-//av_log(NULL, AV_LOG_DEBUG, "%d %d %d %d\n", ast->scale, ast->rate, ast->sample_size, ast->start);
- st->start_time = 0;
- if (ast->rate != 0)
- st->duration = (int64_t)length * AV_TIME_BASE / ast->rate;
- url_fskip(pb, size - 12 * 4);
- }
+ codec_type = CODEC_TYPE_AUDIO;
break;
case MKTAG('t', 'x', 't', 's'):
//FIXME
codec_type = CODEC_TYPE_DATA; //CODEC_TYPE_SUB ? FIXME
- url_fskip(pb, size - 8);
+ break;
+ case MKTAG('p', 'a', 'd', 's'):
+ codec_type = CODEC_TYPE_UNKNOWN;
+ stream_index--;
break;
default:
+ av_log(s, AV_LOG_ERROR, "unknown stream type %X\n", tag1);
goto fail;
}
+ url_fskip(pb, size - 12 * 4);
break;
case MKTAG('s', 't', 'r', 'f'):
/* stream header */
switch(codec_type) {
case CODEC_TYPE_VIDEO:
get_le32(pb); /* size */
- st->codec.width = get_le32(pb);
- st->codec.height = get_le32(pb);
+ st->codec->width = get_le32(pb);
+ st->codec->height = get_le32(pb);
get_le16(pb); /* panes */
- st->codec.bits_per_sample= get_le16(pb); /* depth */
+ st->codec->bits_per_sample= get_le16(pb); /* depth */
tag1 = get_le32(pb);
get_le32(pb); /* ImageSize */
get_le32(pb); /* XPelsPerMeter */
get_le32(pb); /* ClrUsed */
get_le32(pb); /* ClrImportant */
- st->codec.extradata_size= size - 10*4;
- st->codec.extradata= av_malloc(st->codec.extradata_size);
- get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
+ if(size > 10*4 && size<(1<<30)){
+ st->codec->extradata_size= size - 10*4;
+ st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
+ get_buffer(pb, st->codec->extradata, st->codec->extradata_size);
+ }
- if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
+ if(st->codec->extradata_size & 1) //FIXME check if the encoder really did this correctly
get_byte(pb);
/* Extract palette from extradata if bpp <= 8 */
/* This code assumes that extradata contains only palette */
/* This is true for all paletted codecs implemented in ffmpeg */
- if (st->codec.extradata_size && (st->codec.bits_per_sample <= 8)) {
- st->codec.palctrl = av_mallocz(sizeof(AVPaletteControl));
+ if (st->codec->extradata_size && (st->codec->bits_per_sample <= 8)) {
+ st->codec->palctrl = av_mallocz(sizeof(AVPaletteControl));
#ifdef WORDS_BIGENDIAN
- for (i = 0; i < FFMIN(st->codec.extradata_size, AVPALETTE_SIZE)/4; i++)
- st->codec.palctrl->palette[i] = bswap_32(((uint32_t*)st->codec.extradata)[i]);
+ for (i = 0; i < FFMIN(st->codec->extradata_size, AVPALETTE_SIZE)/4; i++)
+ st->codec->palctrl->palette[i] = bswap_32(((uint32_t*)st->codec->extradata)[i]);
#else
- memcpy(st->codec.palctrl->palette, st->codec.extradata,
- FFMIN(st->codec.extradata_size, AVPALETTE_SIZE));
+ memcpy(st->codec->palctrl->palette, st->codec->extradata,
+ FFMIN(st->codec->extradata_size, AVPALETTE_SIZE));
#endif
- st->codec.palctrl->palette_changed = 1;
+ st->codec->palctrl->palette_changed = 1;
}
#ifdef DEBUG
print_tag("video", tag1, 0);
#endif
- st->codec.codec_type = CODEC_TYPE_VIDEO;
- st->codec.codec_tag = tag1;
- st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
- if (st->codec.codec_id == CODEC_ID_XAN_WC4)
+ st->codec->codec_type = CODEC_TYPE_VIDEO;
+ st->codec->codec_tag = tag1;
+ st->codec->codec_id = codec_get_id(codec_bmp_tags, tag1);
+ if (st->codec->codec_id == CODEC_ID_XAN_WC4)
xan_video = 1;
// url_fskip(pb, size - 5 * 4);
break;
case CODEC_TYPE_AUDIO:
- get_wav_header(pb, &st->codec, size);
+ get_wav_header(pb, st->codec, size);
if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
url_fskip(pb, 1);
/* special case time: To support Xan DPCM, hardcode
/* force parsing as several audio frames can be in
one packet */
if (xan_video)
- st->codec.codec_id = CODEC_ID_XAN_DPCM;
+ st->codec->codec_id = CODEC_ID_XAN_DPCM;
break;
default:
- st->codec.codec_type = CODEC_TYPE_DATA;
- st->codec.codec_id= CODEC_ID_NONE;
- st->codec.codec_tag= 0;
+ st->codec->codec_type = CODEC_TYPE_DATA;
+ st->codec->codec_id= CODEC_ID_NONE;
+ st->codec->codec_tag= 0;
url_fskip(pb, size);
break;
}
if (stream_index != s->nb_streams - 1) {
fail:
for(i=0;i<s->nb_streams;i++) {
- av_freep(&s->streams[i]->codec.extradata);
+ av_freep(&s->streams[i]->codec->extradata);
av_freep(&s->streams[i]);
}
return -1;
AVIContext *avi = s->priv_data;
ByteIOContext *pb = &s->pb;
int n, d[8], size;
- offset_t i;
+ offset_t i, sync;
void* dstr;
-
- memset(d, -1, sizeof(int)*8);
if (avi->dv_demux) {
size = dv_get_packet(avi->dv_demux, pkt);
if (size >= 0)
return size;
}
+
+ if(avi->non_interleaved){
+ int best_stream_index;
+ AVStream *best_st= NULL;
+ AVIStream *best_ast;
+ int64_t best_ts= INT64_MAX;
+ int i;
+
+ for(i=0; i<s->nb_streams; i++){
+ AVStream *st = s->streams[i];
+ AVIStream *ast = st->priv_data;
+ int64_t ts= ast->frame_offset;
+
+ if(ast->sample_size)
+ ts /= ast->sample_size;
+ ts= av_rescale(ts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den);
+
+// av_log(NULL, AV_LOG_DEBUG, "%Ld %d/%d %Ld\n", ts, st->time_base.num, st->time_base.den, ast->frame_offset);
+ if(ts < best_ts){
+ best_ts= ts;
+ best_st= st;
+ best_stream_index= i;
+ }
+ }
+ best_ast = best_st->priv_data;
+ 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
+ if(best_ast->remaining)
+ i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY | AVSEEK_FLAG_BACKWARD);
+ else
+ i= av_index_search_timestamp(best_st, best_ts, AVSEEK_FLAG_ANY);
+
+// av_log(NULL, AV_LOG_DEBUG, "%d\n", i);
+ if(i>=0){
+ int64_t pos= best_st->index_entries[i].pos;
+ pos += avi->movi_list + best_ast->packet_size - best_ast->remaining;
+ url_fseek(&s->pb, pos, SEEK_SET);
+// av_log(NULL, AV_LOG_DEBUG, "pos=%Ld\n", pos);
+
+ if(best_ast->remaining)
+ avi->stream_index= best_stream_index;
+ else
+ avi->stream_index= -1;
+ }
+ }
+
+resync:
+ if(avi->stream_index >= 0){
+ AVStream *st= s->streams[ avi->stream_index ];
+ AVIStream *ast= st->priv_data;
+ int size;
+
+ if(ast->sample_size == 0)
+ size= INT_MAX;
+ else if(ast->sample_size < 32)
+ size= 64*ast->sample_size;
+ else
+ size= ast->sample_size;
+
+ if(size > ast->remaining)
+ size= ast->remaining;
+ av_get_packet(pb, pkt, size);
- for(i=url_ftell(pb); !url_feof(pb); i++) {
+ if (avi->dv_demux) {
+ dstr = pkt->destruct;
+ size = dv_produce_packet(avi->dv_demux, pkt,
+ pkt->data, pkt->size);
+ pkt->destruct = dstr;
+ pkt->flags |= PKT_FLAG_KEY;
+ } else {
+ /* XXX: how to handle B frames in avi ? */
+ pkt->dts = ast->frame_offset;
+// pkt->dts += ast->start;
+ if(ast->sample_size)
+ pkt->dts /= ast->sample_size;
+//av_log(NULL, AV_LOG_DEBUG, "dts:%Ld offset:%d %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, n, size);
+ pkt->stream_index = avi->stream_index;
+
+ if (st->codec->codec_type == CODEC_TYPE_VIDEO) {
+ if(st->index_entries){
+ AVIndexEntry *e;
+ int index;
+
+ index= av_index_search_timestamp(st, pkt->dts, 0);
+ e= &st->index_entries[index];
+
+ if(index >= 0 && e->timestamp == ast->frame_offset){
+ if (e->flags & AVINDEX_KEYFRAME)
+ pkt->flags |= PKT_FLAG_KEY;
+ }
+ } else {
+ /* if no index, better to say that all frames
+ are key frames */
+ pkt->flags |= PKT_FLAG_KEY;
+ }
+ } else {
+ pkt->flags |= PKT_FLAG_KEY;
+ }
+ if(ast->sample_size)
+ ast->frame_offset += pkt->size;
+ else
+ ast->frame_offset++;
+ }
+ ast->remaining -= size;
+ if(!ast->remaining){
+ avi->stream_index= -1;
+ ast->packet_size= 0;
+ if (size & 1) {
+ get_byte(pb);
+ size++;
+ }
+ }
+
+ return size;
+ }
+
+ memset(d, -1, sizeof(int)*8);
+ for(i=sync=url_ftell(pb); !url_feof(pb); i++) {
int j;
if (i >= avi->movi_end) {
if (avi->is_odml) {
url_fskip(pb, avi->riff_end - i);
- avi->riff_end = avi->movi_end = url_filesize(url_fileno(pb));
+ avi->riff_end = avi->movi_end = url_fsize(pb);
} else
break;
}
//parse JUNK
||(d[0] == 'J' && d[1] == 'U' && d[2] == 'N' && d[3] == 'K')){
url_fskip(pb, size);
- i+= size;
- memset(d, -1, sizeof(int)*8);
//av_log(NULL, AV_LOG_DEBUG, "SKIP\n");
- continue;
+ goto resync;
}
if( d[0] >= '0' && d[0] <= '9'
AVIStream *ast;
st = s->streams[n];
ast = st->priv_data;
+
+ if( (st->discard >= AVDISCARD_DEFAULT && size==0)
+ /*|| (st->discard >= AVDISCARD_NONKEY && !(pkt->flags & PKT_FLAG_KEY))*/ //FIXME needs a little reordering
+ || st->discard >= AVDISCARD_ALL){
+ if(ast->sample_size) ast->frame_offset += pkt->size;
+ else ast->frame_offset++;
+ url_fskip(pb, size);
+ goto resync;
+ }
- if( (ast->prefix_count<5 && d[2]<128 && d[3]<128) ||
+ if( ((ast->prefix_count<5 || sync+9 > i) && d[2]<128 && d[3]<128) ||
d[2]*256+d[3] == ast->prefix /*||
(d[2] == 'd' && d[3] == 'c') ||
(d[2] == 'w' && d[3] == 'b')*/) {
ast->prefix_count= 0;
}
- av_new_packet(pkt, size);
- get_buffer(pb, pkt->data, size);
- if (size & 1) {
- get_byte(pb);
- size++;
- }
-
- if (avi->dv_demux) {
- dstr = pkt->destruct;
- size = dv_produce_packet(avi->dv_demux, pkt,
- pkt->data, pkt->size);
- pkt->destruct = dstr;
- pkt->flags |= PKT_FLAG_KEY;
- } else {
- /* XXX: how to handle B frames in avi ? */
- pkt->dts = ast->frame_offset;
-// pkt->dts += ast->start;
- if(ast->sample_size)
- pkt->dts /= ast->sample_size;
-//av_log(NULL, AV_LOG_DEBUG, "dts:%Ld offset:%d %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, n, size);
- pkt->stream_index = n;
- /* FIXME: We really should read index for that */
- if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
- if (ast->frame_offset < ast->nb_index_entries) {
- if (ast->index_entries[ast->frame_offset].flags & AVIIF_INDEX)
- pkt->flags |= PKT_FLAG_KEY;
- } else {
- /* if no index, better to say that all frames
- are key frames */
- pkt->flags |= PKT_FLAG_KEY;
- }
- } else {
- pkt->flags |= PKT_FLAG_KEY;
- }
- if(ast->sample_size)
- ast->frame_offset += pkt->size;
- else
- ast->frame_offset++;
- }
- return size;
+ avi->stream_index= n;
+ ast->packet_size= size + 8;
+ ast->remaining= size;
+ goto resync;
}
}
+ /* palette changed chunk */
+ if ( d[0] >= '0' && d[0] <= '9'
+ && d[1] >= '0' && d[1] <= '9'
+ && ((d[2] == 'p' && d[3] == 'c'))
+ && n < s->nb_streams && i + size <= avi->movi_end) {
+
+ AVStream *st;
+ int first, clr, flags, k, p;
+
+ st = s->streams[n];
+
+ first = get_byte(pb);
+ clr = get_byte(pb);
+ if(!clr) /* all 256 colors used */
+ clr = 256;
+ flags = get_le16(pb);
+ p = 4;
+ for (k = first; k < clr + first; k++) {
+ int r, g, b;
+ r = get_byte(pb);
+ g = get_byte(pb);
+ b = get_byte(pb);
+ get_byte(pb);
+ st->codec->palctrl->palette[k] = b + (g << 8) + (r << 16);
+ }
+ st->codec->palctrl->palette_changed = 1;
+ goto resync;
+ }
+
}
return -1;
for each stream */
static int avi_read_idx1(AVFormatContext *s, int size)
{
+ AVIContext *avi = s->priv_data;
ByteIOContext *pb = &s->pb;
int nb_index_entries, i;
AVStream *st;
AVIStream *ast;
- AVIIndexEntry *ie, *entries;
unsigned int index, tag, flags, pos, len;
+ unsigned last_pos= -1;
nb_index_entries = size / 16;
if (nb_index_entries <= 0)
flags = get_le32(pb);
pos = get_le32(pb);
len = get_le32(pb);
-#if defined(DEBUG_SEEK) && 0
- printf("%d: tag=0x%x flags=0x%x pos=0x%x len=%d\n",
+#if defined(DEBUG_SEEK)
+ av_log(NULL, AV_LOG_DEBUG, "%d: tag=0x%x flags=0x%x pos=0x%x len=%d/",
i, tag, flags, pos, len);
#endif
+ if(i==0 && pos > avi->movi_list)
+ avi->movi_list= 0; //FIXME better check
+
index = ((tag & 0xff) - '0') * 10;
index += ((tag >> 8) & 0xff) - '0';
if (index >= s->nb_streams)
st = s->streams[index];
ast = st->priv_data;
- entries = av_fast_realloc(ast->index_entries,
- &ast->index_entries_allocated_size,
- (ast->nb_index_entries + 1) *
- sizeof(AVIIndexEntry));
- if (entries) {
- ast->index_entries = entries;
- ie = &entries[ast->nb_index_entries++];
- ie->flags = flags;
- ie->pos = pos;
- ie->cum_len = ast->cum_len;
- ast->cum_len += len;
- }
+#if defined(DEBUG_SEEK)
+ av_log(NULL, AV_LOG_DEBUG, "%d cum_len=%d\n", len, ast->cum_len);
+#endif
+ if(last_pos == pos)
+ avi->non_interleaved= 1;
+ else
+ av_add_index_entry(st, pos, ast->cum_len, 0, (flags&AVIIF_INDEX) ? AVINDEX_KEYFRAME : 0);
+ if(ast->sample_size)
+ ast->cum_len += len / ast->sample_size;
+ else
+ ast->cum_len ++;
+ last_pos= pos;
}
return 0;
}
+static int guess_ni_flag(AVFormatContext *s){
+ AVIContext *avi = s->priv_data;
+ int i;
+ int64_t last_start=0;
+ int64_t first_end= INT64_MAX;
+
+ for(i=0; i<s->nb_streams; i++){
+ AVStream *st = s->streams[i];
+ AVIStream *ast = st->priv_data;
+ int n= st->nb_index_entries;
+
+ if(n <= 0)
+ continue;
+
+ if(st->index_entries[0].pos > last_start)
+ last_start= st->index_entries[0].pos;
+ if(st->index_entries[n-1].pos < first_end)
+ first_end= st->index_entries[n-1].pos;
+ }
+ return last_start > first_end;
+}
+
static int avi_load_index(AVFormatContext *s)
{
AVIContext *avi = s->priv_data;
}
}
the_end:
+ avi->non_interleaved |= guess_ni_flag(s);
url_fseek(pb, pos, SEEK_SET);
return 0;
}
-/* return the index entry whose position is immediately >= 'wanted_pos' */
-static int locate_frame_in_index(AVIIndexEntry *entries,
- int nb_entries, int wanted_pos)
-{
- int a, b, m, pos;
-
- a = 0;
- b = nb_entries - 1;
- while (a <= b) {
- m = (a + b) >> 1;
- pos = entries[m].pos;
- if (pos == wanted_pos)
- goto found;
- else if (pos > wanted_pos) {
- b = m - 1;
- } else {
- a = m + 1;
- }
- }
- m = a;
- if (m > 0)
- m--;
- found:
- return m;
-}
-
-static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp)
+static int avi_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
{
AVIContext *avi = s->priv_data;
AVStream *st;
- AVIStream *ast;
- int frame_number, i;
+ int i, index;
int64_t pos;
if (!avi->index_loaded) {
avi_load_index(s);
avi->index_loaded = 1;
}
- if (stream_index < 0) {
- for(i = 0; i < s->nb_streams; i++) {
- st = s->streams[i];
- if (st->codec.codec_type == CODEC_TYPE_VIDEO)
- goto found;
- }
- return -1;
- found:
- stream_index = i;
- }
+ assert(stream_index>= 0);
st = s->streams[stream_index];
- if (st->codec.codec_type != CODEC_TYPE_VIDEO)
- return -1;
- ast = st->priv_data;
- /* compute the frame number */
- frame_number = timestamp;
-#ifdef DEBUG_SEEK
- printf("timestamp=%0.3f nb_indexes=%d frame_number=%d\n",
- (double)timestamp / AV_TIME_BASE,
- ast->nb_index_entries, frame_number);
-#endif
- /* find a closest key frame before */
- if (frame_number >= ast->nb_index_entries)
- return -1;
- while (frame_number >= 0 &&
- !(ast->index_entries[frame_number].flags & AVIIF_INDEX))
- frame_number--;
- if (frame_number < 0)
+ index= av_index_search_timestamp(st, timestamp, flags);
+ if(index<0)
return -1;
- ast->new_frame_offset = frame_number;
-
+
/* find the position */
- pos = ast->index_entries[frame_number].pos;
+ pos = st->index_entries[index].pos;
+ timestamp = st->index_entries[index].timestamp;
+
+// av_log(NULL, AV_LOG_DEBUG, "XX %Ld %d %Ld\n", timestamp, index, st->index_entries[index].timestamp);
-#ifdef DEBUG_SEEK
- printf("key_frame_number=%d pos=0x%llx\n",
- frame_number, pos);
-#endif
-
- /* update the frame counters for all the other stream by looking
- at the positions just after the one found */
for(i = 0; i < s->nb_streams; i++) {
- int j;
- if (i != stream_index) {
- st = s->streams[i];
- ast = st->priv_data;
- if (ast->nb_index_entries <= 0)
- return -1;
- j = locate_frame_in_index(ast->index_entries,
- ast->nb_index_entries,
- pos);
- /* get next frame */
- if ((j + 1) < ast->nb_index_entries)
- j++;
- /* extract the current frame number */
- if (ast->sample_size==0)
- ast->new_frame_offset = j;
- else
- ast->new_frame_offset = ast->index_entries[j].cum_len;
+ AVStream *st2 = s->streams[i];
+ AVIStream *ast2 = st2->priv_data;
+
+ ast2->packet_size=
+ ast2->remaining= 0;
+
+ if (st2->nb_index_entries <= 0)
+ continue;
+
+// assert(st2->codec.block_align);
+ assert(st2->time_base.den == ast2->rate);
+ assert(st2->time_base.num == ast2->scale);
+ index = av_index_search_timestamp(
+ st2,
+ av_rescale(timestamp, st2->time_base.den*(int64_t)st->time_base.num, st->time_base.den * (int64_t)st2->time_base.num),
+ flags | AVSEEK_FLAG_BACKWARD);
+ if(index<0)
+ index=0;
+
+ if(!avi->non_interleaved){
+ while(index>0 && st2->index_entries[index].pos > pos)
+ index--;
+ while(index+1 < st2->nb_index_entries && st2->index_entries[index].pos < pos)
+ index++;
}
+
+// av_log(NULL, AV_LOG_DEBUG, "%Ld %d %Ld\n", timestamp, index, st2->index_entries[index].timestamp);
+ /* extract the current frame number */
+ ast2->frame_offset = st2->index_entries[index].timestamp;
+ if(ast2->sample_size)
+ ast2->frame_offset *=ast2->sample_size;
}
-
- /* everything is OK now. We can update the frame offsets */
- for(i = 0; i < s->nb_streams; i++) {
- st = s->streams[i];
- ast = st->priv_data;
- ast->frame_offset = ast->new_frame_offset;
-#ifdef DEBUG_SEEK
- printf("%d: frame_offset=%d\n", i,
- ast->frame_offset);
-#endif
- }
+
+ if (avi->dv_demux)
+ dv_flush_audio_packets(avi->dv_demux);
/* do the seek */
pos += avi->movi_list;
url_fseek(&s->pb, pos, SEEK_SET);
+ avi->stream_index= -1;
return 0;
}
for(i=0;i<s->nb_streams;i++) {
AVStream *st = s->streams[i];
AVIStream *ast = st->priv_data;
- if(ast){
- av_free(ast->index_entries);
- av_free(ast);
- }
- av_free(st->codec.extradata);
- av_free(st->codec.palctrl);
+ av_free(ast);
+ av_free(st->codec->extradata);
+ av_free(st->codec->palctrl);
}
if (avi->dv_demux)