Guessing from bitrate will provide adequate results in most cases. --- We have this ugly thing in utils.c cargoculted from 2003 that tries to seek before the end for mpeg and mpegts and guesses the duration from the packets it finds there.
I think this kind of format-specific hacks isn't justified, so we should get rid of it. Alternatively we could try to make this more generic or move the code to mpeg/mpegts demuxers. Opinions? --- libavformat/utils.c | 96 +--------------------------------------------- tests/ref/seek/lavf_mpg | 2 +- 2 files changed, 4 insertions(+), 94 deletions(-) diff --git a/libavformat/utils.c b/libavformat/utils.c index 6393b62..93f8be8 100644 --- a/libavformat/utils.c +++ b/libavformat/utils.c @@ -1929,91 +1929,7 @@ static void estimate_timings_from_bit_rate(AVFormatContext *ic) } } -#define DURATION_MAX_READ_SIZE 250000 -#define DURATION_MAX_RETRY 3 - -/* only usable for MPEG-PS streams */ -static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset) -{ - AVPacket pkt1, *pkt = &pkt1; - AVStream *st; - int read_size, i, ret; - int64_t end_time; - int64_t filesize, offset, duration; - int retry=0; - - ic->cur_st = NULL; - - /* flush packet queue */ - flush_packet_queue(ic); - - for (i=0; i<ic->nb_streams; i++) { - st = ic->streams[i]; - if (st->start_time == AV_NOPTS_VALUE && st->first_dts == AV_NOPTS_VALUE) - av_log(st->codec, AV_LOG_WARNING, "start time is not set in estimate_timings_from_pts\n"); - - if (st->parser) { - av_parser_close(st->parser); - st->parser= NULL; - av_free_packet(&st->cur_pkt); - } - } - - /* estimate the end time (duration) */ - /* XXX: may need to support wrapping */ - filesize = ic->pb ? avio_size(ic->pb) : 0; - end_time = AV_NOPTS_VALUE; - do{ - offset = filesize - (DURATION_MAX_READ_SIZE<<retry); - if (offset < 0) - offset = 0; - - avio_seek(ic->pb, offset, SEEK_SET); - read_size = 0; - for(;;) { - if (read_size >= DURATION_MAX_READ_SIZE<<(FFMAX(retry-1,0))) - break; - - do { - ret = av_read_packet(ic, pkt); - } while(ret == AVERROR(EAGAIN)); - if (ret != 0) - break; - read_size += pkt->size; - st = ic->streams[pkt->stream_index]; - if (pkt->pts != AV_NOPTS_VALUE && - (st->start_time != AV_NOPTS_VALUE || - st->first_dts != AV_NOPTS_VALUE)) { - duration = end_time = pkt->pts; - if (st->start_time != AV_NOPTS_VALUE) - duration -= st->start_time; - else - duration -= st->first_dts; - if (duration < 0) - duration += 1LL<<st->pts_wrap_bits; - if (duration > 0) { - if (st->duration == AV_NOPTS_VALUE || st->duration < duration) - st->duration = duration; - } - } - av_free_packet(pkt); - } - }while( end_time==AV_NOPTS_VALUE - && filesize > (DURATION_MAX_READ_SIZE<<retry) - && ++retry <= DURATION_MAX_RETRY); - - fill_all_stream_timings(ic); - - avio_seek(ic->pb, old_offset, SEEK_SET); - for (i=0; i<ic->nb_streams; i++) { - st= ic->streams[i]; - st->cur_dts= st->first_dts; - st->last_IP_pts = AV_NOPTS_VALUE; - st->reference_dts = AV_NOPTS_VALUE; - } -} - -static void estimate_timings(AVFormatContext *ic, int64_t old_offset) +static void estimate_timings(AVFormatContext *ic) { int64_t file_size; @@ -2026,12 +1942,7 @@ static void estimate_timings(AVFormatContext *ic, int64_t old_offset) file_size = 0; } - if ((!strcmp(ic->iformat->name, "mpeg") || - !strcmp(ic->iformat->name, "mpegts")) && - file_size && ic->pb->seekable) { - /* get accurate estimate from the PTSes */ - estimate_timings_from_pts(ic, old_offset); - } else if (has_duration(ic)) { + if (has_duration(ic)) { /* at least one component has timings - we use them for all the components */ fill_all_stream_timings(ic); @@ -2233,7 +2144,6 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) int i, count, ret, read_size, j; AVStream *st; AVPacket pkt1, *pkt; - int64_t old_offset = avio_tell(ic->pb); int orig_nb_streams = ic->nb_streams; // new streams might appear, no options for those for(i=0;i<ic->nb_streams;i++) { @@ -2479,7 +2389,7 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options) } } - estimate_timings(ic, old_offset); + estimate_timings(ic); compute_chapters_end(ic); diff --git a/tests/ref/seek/lavf_mpg b/tests/ref/seek/lavf_mpg index dc51195..d037d9e 100644 --- a/tests/ref/seek/lavf_mpg +++ b/tests/ref/seek/lavf_mpg @@ -1,6 +1,6 @@ ret: 0 st: 1 flags:1 dts: 1.000000 pts: 1.000000 pos: 2048 size: 208 ret: 0 st:-1 flags:0 ts:-1.000000 -ret: 0 st: 1 flags:1 dts: 1.000000 pts: 1.000000 pos: 2048 size: 208 +ret: 0 st: 0 flags:0 dts: 1.000000 pts: 1.040000 pos: 26624 size: 16303 ret: 0 st:-1 flags:1 ts: 1.894167 ret: 0 st: 0 flags:0 dts: 1.880000 pts: 1.920000 pos: 327680 size: 12894 ret: 0 st: 0 flags:0 ts: 0.788333 -- 1.7.6.3 _______________________________________________ libav-devel mailing list libav-devel@libav.org https://lists.libav.org/mailman/listinfo/libav-devel