Commit 7496569a authored by michael's avatar michael

better generic index building and seeking code


git-svn-id: file:///var/local/repositories/ffmpeg/trunk@7841 9553f0bf-9b14-0410-a0b8-cfaf0461ba5b
parent 8d534670
...@@ -138,6 +138,7 @@ typedef struct AVFormatParameters { ...@@ -138,6 +138,7 @@ typedef struct AVFormatParameters {
raw picture data */ raw picture data */
#define AVFMT_GLOBALHEADER 0x0040 /* format wants global header */ #define AVFMT_GLOBALHEADER 0x0040 /* format wants global header */
#define AVFMT_NOTIMESTAMPS 0x0080 /* format doesnt need / has any timestamps */ #define AVFMT_NOTIMESTAMPS 0x0080 /* format doesnt need / has any timestamps */
#define AVFMT_GENERIC_INDEX 0x0100 /* use generic index building code */
typedef struct AVOutputFormat { typedef struct AVOutputFormat {
const char *name; const char *name;
......
...@@ -393,6 +393,7 @@ AVInputFormat mp3_demuxer = { ...@@ -393,6 +393,7 @@ AVInputFormat mp3_demuxer = {
mp3_read_header, mp3_read_header,
mp3_read_packet, mp3_read_packet,
mp3_read_close, mp3_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "mp2,mp3,m2a", /* XXX: use probe */ .extensions = "mp2,mp3,m2a", /* XXX: use probe */
}; };
#endif #endif
......
...@@ -414,6 +414,7 @@ AVInputFormat shorten_demuxer = { ...@@ -414,6 +414,7 @@ AVInputFormat shorten_demuxer = {
shorten_read_header, shorten_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "shn", .extensions = "shn",
}; };
...@@ -425,6 +426,7 @@ AVInputFormat flac_demuxer = { ...@@ -425,6 +426,7 @@ AVInputFormat flac_demuxer = {
flac_read_header, flac_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "flac", .extensions = "flac",
}; };
...@@ -452,6 +454,7 @@ AVInputFormat ac3_demuxer = { ...@@ -452,6 +454,7 @@ AVInputFormat ac3_demuxer = {
ac3_read_header, ac3_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "ac3", .extensions = "ac3",
}; };
...@@ -479,6 +482,7 @@ AVInputFormat dts_demuxer = { ...@@ -479,6 +482,7 @@ AVInputFormat dts_demuxer = {
dts_read_header, dts_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "dts", .extensions = "dts",
}; };
...@@ -490,6 +494,7 @@ AVInputFormat aac_demuxer = { ...@@ -490,6 +494,7 @@ AVInputFormat aac_demuxer = {
aac_read_header, aac_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "aac", .extensions = "aac",
}; };
...@@ -501,6 +506,7 @@ AVInputFormat h261_demuxer = { ...@@ -501,6 +506,7 @@ AVInputFormat h261_demuxer = {
video_read_header, video_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "h261", .extensions = "h261",
.value = CODEC_ID_H261, .value = CODEC_ID_H261,
}; };
...@@ -529,6 +535,7 @@ AVInputFormat h263_demuxer = { ...@@ -529,6 +535,7 @@ AVInputFormat h263_demuxer = {
video_read_header, video_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
// .extensions = "h263", //FIXME remove after writing mpeg4_probe // .extensions = "h263", //FIXME remove after writing mpeg4_probe
.value = CODEC_ID_H263, .value = CODEC_ID_H263,
}; };
...@@ -557,6 +564,7 @@ AVInputFormat m4v_demuxer = { ...@@ -557,6 +564,7 @@ AVInputFormat m4v_demuxer = {
video_read_header, video_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "m4v", //FIXME remove after writing mpeg4_probe .extensions = "m4v", //FIXME remove after writing mpeg4_probe
.value = CODEC_ID_MPEG4, .value = CODEC_ID_MPEG4,
}; };
...@@ -585,6 +593,7 @@ AVInputFormat h264_demuxer = { ...@@ -585,6 +593,7 @@ AVInputFormat h264_demuxer = {
video_read_header, video_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe .extensions = "h26l,h264,264", //FIXME remove after writing mpeg4_probe
.value = CODEC_ID_H264, .value = CODEC_ID_H264,
}; };
...@@ -613,6 +622,7 @@ AVInputFormat mpegvideo_demuxer = { ...@@ -613,6 +622,7 @@ AVInputFormat mpegvideo_demuxer = {
video_read_header, video_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.value = CODEC_ID_MPEG1VIDEO, .value = CODEC_ID_MPEG1VIDEO,
}; };
...@@ -656,6 +666,7 @@ AVInputFormat mjpeg_demuxer = { ...@@ -656,6 +666,7 @@ AVInputFormat mjpeg_demuxer = {
video_read_header, video_read_header,
raw_read_partial_packet, raw_read_partial_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "mjpg,mjpeg", .extensions = "mjpg,mjpeg",
.value = CODEC_ID_MJPEG, .value = CODEC_ID_MJPEG,
}; };
...@@ -668,6 +679,7 @@ AVInputFormat ingenient_demuxer = { ...@@ -668,6 +679,7 @@ AVInputFormat ingenient_demuxer = {
video_read_header, video_read_header,
ingenient_read_packet, ingenient_read_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "cgi", // FIXME .extensions = "cgi", // FIXME
.value = CODEC_ID_MJPEG, .value = CODEC_ID_MJPEG,
}; };
...@@ -700,6 +712,7 @@ AVInputFormat pcm_ ## name ## _demuxer = {\ ...@@ -700,6 +712,7 @@ AVInputFormat pcm_ ## name ## _demuxer = {\
raw_read_packet,\ raw_read_packet,\
raw_read_close,\ raw_read_close,\
pcm_read_seek,\ pcm_read_seek,\
.flags= AVFMT_GENERIC_INDEX,\
.extensions = ext,\ .extensions = ext,\
.value = codec,\ .value = codec,\
}; };
...@@ -797,6 +810,7 @@ AVInputFormat rawvideo_demuxer = { ...@@ -797,6 +810,7 @@ AVInputFormat rawvideo_demuxer = {
raw_read_header, raw_read_header,
rawvideo_read_packet, rawvideo_read_packet,
raw_read_close, raw_read_close,
.flags= AVFMT_GENERIC_INDEX,
.extensions = "yuv,cif,qcif", .extensions = "yuv,cif,qcif",
.value = CODEC_ID_RAWVIDEO, .value = CODEC_ID_RAWVIDEO,
}; };
......
...@@ -788,6 +788,12 @@ static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt) ...@@ -788,6 +788,12 @@ static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt)
pkt->dts = st->parser->dts; pkt->dts = st->parser->dts;
pkt->destruct = av_destruct_packet_nofree; pkt->destruct = av_destruct_packet_nofree;
compute_pkt_fields(s, st, st->parser, pkt); compute_pkt_fields(s, st, st->parser, pkt);
if((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & PKT_FLAG_KEY){
av_add_index_entry(st, st->parser->frame_offset, pkt->dts,
0, 0, AVINDEX_KEYFRAME);
}
break; break;
} }
} else { } else {
...@@ -836,6 +842,10 @@ static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt) ...@@ -836,6 +842,10 @@ static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt)
}else if(st->need_parsing == 2){ }else if(st->need_parsing == 2){
st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES; st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
} }
if(st->parser && (s->iformat->flags & AVFMT_GENERIC_INDEX)){
st->parser->last_frame_offset=
st->parser->cur_offset= s->cur_pkt.pos;
}
} }
} }
} }
...@@ -1370,23 +1380,42 @@ static int av_seek_frame_generic(AVFormatContext *s, ...@@ -1370,23 +1380,42 @@ static int av_seek_frame_generic(AVFormatContext *s,
AVStream *st; AVStream *st;
AVIndexEntry *ie; AVIndexEntry *ie;
if (!s->index_built) {
if (is_raw_stream(s)) {
av_build_index_raw(s);
} else {
return -1;
}
s->index_built = 1;
}
st = s->streams[stream_index]; st = s->streams[stream_index];
index = av_index_search_timestamp(st, timestamp, flags); index = av_index_search_timestamp(st, timestamp, flags);
if(index < 0){
int i;
AVPacket pkt;
if(st->index_entries && st->nb_index_entries){
ie= &st->index_entries[st->nb_index_entries-1];
url_fseek(&s->pb, ie->pos, SEEK_SET);
av_update_cur_dts(s, st, ie->timestamp);
}else
url_fseek(&s->pb, 0, SEEK_SET);
for(i=0;; i++) {
int ret = av_read_frame(s, &pkt);
if(ret<0)
break;
av_free_packet(&pkt);
if(stream_index == pkt.stream_index){
if((pkt.flags & PKT_FLAG_KEY) && pkt.dts > timestamp)
break;
}
}
index = av_index_search_timestamp(st, timestamp, flags);
}
if (index < 0) if (index < 0)
return -1; return -1;
/* now we have found the index, we can seek */
ie = &st->index_entries[index];
av_read_frame_flush(s); av_read_frame_flush(s);
if (s->iformat->read_seek){
if(s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
return 0;
}
ie = &st->index_entries[index];
url_fseek(&s->pb, ie->pos, SEEK_SET); url_fseek(&s->pb, ie->pos, SEEK_SET);
av_update_cur_dts(s, st, ie->timestamp); av_update_cur_dts(s, st, ie->timestamp);
......
...@@ -188,13 +188,11 @@ static int wav_read_packet(AVFormatContext *s, ...@@ -188,13 +188,11 @@ static int wav_read_packet(AVFormatContext *s,
size = (size / st->codec->block_align) * st->codec->block_align; size = (size / st->codec->block_align) * st->codec->block_align;
} }
size= FFMIN(size, left); size= FFMIN(size, left);
if (av_new_packet(pkt, size)) ret= av_get_packet(&s->pb, pkt, size);
if (ret <= 0)
return AVERROR_IO; return AVERROR_IO;
pkt->stream_index = 0; pkt->stream_index = 0;
ret = get_buffer(&s->pb, pkt->data, pkt->size);
if (ret < 0)
av_free_packet(pkt);
/* note: we need to modify the packet size here to handle the last /* note: we need to modify the packet size here to handle the last
packet */ packet */
pkt->size = ret; pkt->size = ret;
...@@ -235,6 +233,7 @@ AVInputFormat wav_demuxer = { ...@@ -235,6 +233,7 @@ AVInputFormat wav_demuxer = {
wav_read_packet, wav_read_packet,
wav_read_close, wav_read_close,
wav_read_seek, wav_read_seek,
.flags= AVFMT_GENERIC_INDEX,
.codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0}, .codec_tag= (const AVCodecTag*[]){codec_wav_tags, 0},
}; };
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment