From a8dd8dc6e9a419758006dc212fb590991e927030 Mon Sep 17 00:00:00 2001 From: Ivan Schreter Date: Sat, 22 Aug 2009 16:07:50 +0000 Subject: [PATCH] Use generic multi-stream key frame finding routine to implement read_seek2 and map read_seek to read_seek2. Originally committed as revision 19681 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavformat/mpegts.c | 81 ++++++++++++++++++++++++++++++++------- tests/seek.regression.ref | 58 +++++++++++++--------------- 2 files changed, 94 insertions(+), 45 deletions(-) diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 6c0c314f42..e3bdc6b658 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -28,6 +28,7 @@ #include "avformat.h" #include "mpegts.h" #include "internal.h" +#include "seek.h" /* 1.0 second at 24Mbit/s */ #define MAX_SCAN_PACKETS 32000 @@ -1521,29 +1522,81 @@ static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index, return timestamp; } -static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){ - MpegTSContext *ts = s->priv_data; - uint8_t buf[TS_PACKET_SIZE]; +static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags); + +static int read_seek2(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t target_ts, int64_t max_ts, int flags) +{ int64_t pos; - if(av_seek_frame_binary(s, stream_index, target_ts, flags) < 0) - return -1; + int64_t ts_ret, ts_adj; + int stream_index_gen_search; + AVStream *st; + AVParserState *backup; - pos= url_ftell(s->pb); + backup = ff_store_parser_state(s); - for(;;) { - url_fseek(s->pb, pos, SEEK_SET); - if (get_buffer(s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE) + // detect direction of seeking for search purposes + flags |= (target_ts - min_ts > (uint64_t)(max_ts - target_ts)) ? AVSEEK_FLAG_BACKWARD : 0; + + if (flags & AVSEEK_FLAG_BYTE) { + /* use position directly, we will search starting from it */ + pos = target_ts; + } else { + /* search for some position with good timestamp match */ + if(stream_index < 0){ + stream_index_gen_search = av_find_default_stream_index(s); + if (stream_index_gen_search < 0) { + ff_restore_parser_state(s, backup); + return -1; + } + + st = s->streams[stream_index_gen_search]; + /* timestamp for default must be expressed in AV_TIME_BASE units */ + ts_adj = av_rescale(target_ts, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num); + } else { + ts_adj = target_ts; + stream_index_gen_search = stream_index; + } + pos = av_gen_search(s, stream_index_gen_search, ts_adj, + 0, INT64_MAX, -1, + AV_NOPTS_VALUE, + AV_NOPTS_VALUE, + flags, &ts_ret, mpegts_get_pcr); + if (pos < 0) { + ff_restore_parser_state(s, backup); return -1; -// pid = AV_RB16(buf + 1) & 0x1fff; - if(buf[1] & 0x40) break; - pos += ts->raw_packet_size; + } } - url_fseek(s->pb, pos, SEEK_SET); + /* search for actual matching keyframe/starting position for all streams */ + if (ff_gen_syncpoint_search(s, stream_index, pos, min_ts, target_ts, max_ts, flags) < 0) { + ff_restore_parser_state(s, backup); + return -1; + } + + ff_free_parser_state(s, backup); return 0; } +static int read_seek(AVFormatContext *s, int stream_index, int64_t target_ts, int flags) +{ + int ret; + if (flags & AVSEEK_FLAG_BACKWARD) { + ret = read_seek2(s, stream_index, INT64_MIN, target_ts, target_ts, flags & ~AVSEEK_FLAG_BACKWARD); + if (ret < 0) { + // for compatibility reasons, seek to best-fitting timestamp + ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags & ~AVSEEK_FLAG_BACKWARD); + } + } else { + ret = read_seek2(s, stream_index, target_ts, target_ts, INT64_MAX, flags); + if (ret < 0) { + // for compatibility reasons, seek to best-fitting timestamp + ret = read_seek2(s, stream_index, INT64_MIN, target_ts, INT64_MAX, flags); + } + } + return ret; +} + /**************************************************************/ /* parsing functions - called from other demuxers such as RTP */ @@ -1608,6 +1661,7 @@ AVInputFormat mpegts_demuxer = { read_seek, mpegts_get_pcr, .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT, + .read_seek2 = read_seek2, }; AVInputFormat mpegtsraw_demuxer = { @@ -1621,4 +1675,5 @@ AVInputFormat mpegtsraw_demuxer = { read_seek, mpegts_get_pcr, .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT, + .read_seek2 = read_seek2, }; diff --git a/tests/seek.regression.ref b/tests/seek.regression.ref index 5faf77b069..dc09fa6d4c 100644 --- a/tests/seek.regression.ref +++ b/tests/seek.regression.ref @@ -3599,57 +3599,51 @@ ret:-1 st:-1 ts:-0.645825 flags:1 tests/data/b-lavf.ts ret: 0 st: 0 dts:0.660000 pts:0.700000 pos:564 size:24801 flags:1 ret: 0 st:-1 ts:-1.000000 flags:0 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 +ret: 0 st: 0 dts:0.660000 pts:0.700000 pos:564 size:24801 flags:1 ret: 0 st:-1 ts:1.894167 flags:1 -ret: 0 st: 0 dts:0.780000 pts:0.820000 pos:65612 size:12623 flags:0 +ret: 0 st: 1 dts:1.457556 pts:1.457556 pos:435596 size:409 flags:1 ret: 0 st: 0 ts:0.788333 flags:0 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 -ret: 0 st: 0 ts:-0.317500 flags:1 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 +ret: 0 st: 0 dts:1.100000 pts:1.140000 pos:189504 size:12236 flags:0 +ret:-1 st: 0 ts:-0.317500 flags:1 ret: 0 st: 1 ts:2.576667 flags:0 -ret: 0 st: 0 dts:0.860000 pts:0.900000 pos:96068 size:13086 flags:0 +ret:-5 ret: 0 st: 1 ts:1.470833 flags:1 -ret: 0 st: 0 dts:0.780000 pts:0.820000 pos:65612 size:12623 flags:0 +ret: 0 st: 0 dts:1.140000 pts:1.180000 pos:203980 size:24787 flags:1 ret: 0 st:-1 ts:0.365002 flags:0 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 -ret: 0 st:-1 ts:-0.740831 flags:1 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 +ret: 0 st: 0 dts:0.660000 pts:0.700000 pos:564 size:24801 flags:1 +ret:-1 st:-1 ts:-0.740831 flags:1 ret: 0 st: 0 ts:2.153333 flags:0 -ret: 0 st: 0 dts:0.820000 pts:0.860000 pos:80464 size:13388 flags:0 +ret:-5 ret: 0 st: 0 ts:1.047500 flags:1 -ret: 0 st: 0 dts:0.740000 pts:0.780000 pos:48316 size:14502 flags:0 +ret: 0 st: 0 dts:0.660000 pts:0.700000 pos:564 size:24801 flags:1 ret: 0 st: 1 ts:-0.058333 flags:0 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 +ret: 0 st: 0 dts:0.660000 pts:0.700000 pos:564 size:24801 flags:1 ret: 0 st: 1 ts:2.835833 flags:1 -ret: 0 st: 0 dts:0.900000 pts:0.940000 pos:111296 size:12781 flags:0 +ret: 0 st: 1 dts:1.457556 pts:1.457556 pos:435596 size:409 flags:1 ret: 0 st:-1 ts:1.730004 flags:0 -ret: 0 st: 0 dts:0.780000 pts:0.820000 pos:65612 size:12623 flags:0 -ret: 0 st:-1 ts:0.624171 flags:1 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 +ret:-5 +ret:-1 st:-1 ts:0.624171 flags:1 ret: 0 st: 0 ts:-0.481667 flags:0 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 +ret: 0 st: 0 dts:0.660000 pts:0.700000 pos:564 size:24801 flags:1 ret: 0 st: 0 ts:2.412500 flags:1 -ret: 0 st: 0 dts:0.860000 pts:0.900000 pos:96068 size:13086 flags:0 +ret: 0 st: 1 dts:1.457556 pts:1.457556 pos:435596 size:409 flags:1 ret: 0 st: 1 ts:1.306667 flags:0 -ret: 0 st: 0 dts:0.740000 pts:0.780000 pos:48316 size:14502 flags:0 -ret: 0 st: 1 ts:0.200844 flags:1 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 +ret: 0 st: 0 dts:1.460000 pts:1.500000 pos:351184 size:12679 flags:0 +ret:-1 st: 1 ts:0.200844 flags:1 ret: 0 st:-1 ts:-0.904994 flags:0 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 +ret: 0 st: 0 dts:0.660000 pts:0.700000 pos:564 size:24801 flags:1 ret: 0 st:-1 ts:1.989173 flags:1 -ret: 0 st: 0 dts:0.820000 pts:0.860000 pos:80464 size:13388 flags:0 +ret: 0 st: 1 dts:1.457556 pts:1.457556 pos:435596 size:409 flags:1 ret: 0 st: 0 ts:0.883344 flags:0 -ret: 0 st: 0 dts:0.740000 pts:0.780000 pos:48316 size:14502 flags:0 -ret: 0 st: 0 ts:-0.222489 flags:1 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 +ret: 0 st: 0 dts:1.100000 pts:1.140000 pos:189504 size:12236 flags:0 +ret:-1 st: 0 ts:-0.222489 flags:1 ret: 0 st: 1 ts:2.671678 flags:0 -ret: 0 st: 0 dts:0.860000 pts:0.900000 pos:96068 size:13086 flags:0 +ret:-5 ret: 0 st: 1 ts:1.565844 flags:1 -ret: 0 st: 0 dts:0.780000 pts:0.820000 pos:65612 size:12623 flags:0 +ret: 0 st: 0 dts:1.140000 pts:1.180000 pos:203980 size:24787 flags:1 ret: 0 st:-1 ts:0.460008 flags:0 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 -ret: 0 st:-1 ts:-0.645825 flags:1 -ret: 0 st: 0 dts:0.700000 pts:0.740000 pos:29140 size:16433 flags:0 +ret: 0 st: 0 dts:0.660000 pts:0.700000 pos:564 size:24801 flags:1 +ret:-1 st:-1 ts:-0.645825 flags:1 ---------------- tests/data/b-lavf.ul ret: 0 st: 0 dts:0.000000 pts:0.000000 pos:0 size:1024 flags:1