aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/mpegts.c
diff options
context:
space:
mode:
authorFabrice Bellard <fabrice@bellard.org>2003-11-10 18:47:52 +0000
committerFabrice Bellard <fabrice@bellard.org>2003-11-10 18:47:52 +0000
commit27f388aa2488aeceb736fa45ea3733e1b5cd1967 (patch)
tree2b6215800c149a16e9d18278a088fd8da6cfcb51 /libavformat/mpegts.c
parentf9b5459e7925d2bf04aabb1bae93d94b01662e44 (diff)
downloadffmpeg-27f388aa2488aeceb736fa45ea3733e1b5cd1967.tar.gz
seek support
Originally committed as revision 2501 to svn://svn.ffmpeg.org/ffmpeg/trunk
Diffstat (limited to 'libavformat/mpegts.c')
-rw-r--r--libavformat/mpegts.c158
1 files changed, 157 insertions, 1 deletions
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 6c4f1a16a0..e7ec00fe4d 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -21,6 +21,7 @@
#include "mpegts.h"
//#define DEBUG_SI
+//#define DEBUG_SEEK
/* 1.0 second at 24Mbit/s */
#define MAX_SCAN_PACKETS 32000
@@ -87,7 +88,8 @@ struct MpegTSContext {
/* used to estimate the exact PCR */
int64_t cur_pcr;
int pcr_incr;
-
+ int pcr_pid;
+
/* data needed to handle file based ts */
int stop_parse; /* stop parsing loop */
AVPacket *pkt; /* packet containing av data */
@@ -367,6 +369,7 @@ static void pmt_cb(void *opaque, const uint8_t *section, int section_len)
pcr_pid = get16(&p, p_end) & 0x1fff;
if (pcr_pid < 0)
return;
+ ts->pcr_pid = pcr_pid;
#ifdef DEBUG_SI
printf("pcr_pid=0x%x\n", pcr_pid);
#endif
@@ -710,6 +713,7 @@ static void mpegts_push_data(void *opaque,
st->priv_data = pes;
st->codec.codec_type = codec_type;
st->codec.codec_id = codec_id;
+ st->need_parsing = 1;
pes->st = st;
}
}
@@ -1117,6 +1121,7 @@ static int mpegts_read_header(AVFormatContext *s,
}
nb_packets++;
}
+ ts->pcr_pid = pcr_pid;
/* NOTE1: the bitrate is computed without the FEC */
/* NOTE2: it is only the bitrate of the start of the stream */
@@ -1204,6 +1209,156 @@ static int mpegts_read_close(AVFormatContext *s)
return 0;
}
+static int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
+ int64_t *ppos, int find_next)
+{
+ MpegTSContext *ts = s->priv_data;
+ int64_t pos, timestamp;
+ uint8_t buf[TS_PACKET_SIZE];
+ int pcr_l, pid;
+
+ pos = *ppos;
+ if (find_next) {
+ for(;;) {
+ url_fseek(&s->pb, pos, SEEK_SET);
+ if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
+ return AV_NOPTS_VALUE;
+ pid = ((buf[1] & 0x1f) << 8) | buf[2];
+ if (pid == ts->pcr_pid &&
+ parse_pcr(&timestamp, &pcr_l, buf) == 0) {
+ break;
+ }
+ pos += ts->raw_packet_size;
+ }
+ } else {
+ for(;;) {
+ pos -= ts->raw_packet_size;
+ if (pos < 0)
+ return AV_NOPTS_VALUE;
+ url_fseek(&s->pb, pos, SEEK_SET);
+ if (get_buffer(&s->pb, buf, TS_PACKET_SIZE) != TS_PACKET_SIZE)
+ return AV_NOPTS_VALUE;
+ pid = ((buf[1] & 0x1f) << 8) | buf[2];
+ if (pid == ts->pcr_pid &&
+ parse_pcr(&timestamp, &pcr_l, buf) == 0) {
+ break;
+ }
+ }
+ }
+ *ppos = pos;
+ return timestamp;
+}
+
+typedef int64_t ReadTimestampFunc(AVFormatContext *s, int stream_index,
+ int64_t *ppos, int find_next);
+
+static int64_t do_block_align(int64_t val, int block_align)
+{
+ return (val / block_align) * block_align;
+}
+
+/* XXX: use it in other formats */
+static int timestamp_read_seek(AVFormatContext *s,
+ int stream_index, int64_t timestamp,
+ ReadTimestampFunc *read_timestamp,
+ int block_align)
+{
+ int64_t pos_min, pos_max, pos;
+ int64_t dts_min, dts_max, dts;
+
+#ifdef DEBUG_SEEK
+ printf("read_seek: %d %0.3f\n", stream_index, timestamp / 90000.0);
+#endif
+
+ pos_min = 0;
+ dts_min = read_timestamp(s, stream_index, &pos_min, 1);
+ if (dts_min == AV_NOPTS_VALUE) {
+ /* we can reach this case only if no PTS are present in
+ the whole stream */
+ return -1;
+ }
+ pos_max = do_block_align(url_filesize(url_fileno(&s->pb)), block_align) -
+ block_align;
+ dts_max = read_timestamp(s, stream_index, &pos_max, 0);
+
+ while (pos_min <= pos_max) {
+#ifdef DEBUG_SEEK
+ printf("pos_min=0x%llx pos_max=0x%llx dts_min=%0.3f dts_max=%0.3f\n",
+ pos_min, pos_max,
+ dts_min / 90000.0, dts_max / 90000.0);
+#endif
+ if (timestamp <= dts_min) {
+ pos = pos_min;
+ goto found;
+ } else if (timestamp >= dts_max) {
+ pos = pos_max;
+ goto found;
+ } else {
+ /* interpolate position (better than dichotomy) */
+ pos = (int64_t)((double)(pos_max - pos_min) *
+ (double)(timestamp - dts_min) /
+ (double)(dts_max - dts_min)) + pos_min;
+ pos = do_block_align(pos, block_align);
+ }
+#ifdef DEBUG_SEEK
+ printf("pos=0x%llx\n", pos);
+#endif
+ /* read the next timestamp */
+ dts = read_timestamp(s, stream_index, &pos, 1);
+ /* check if we are lucky */
+ if (dts == AV_NOPTS_VALUE) {
+ /* should never happen */
+ pos = pos_min;
+ goto found;
+ } else if (timestamp == dts) {
+ goto found;
+ } else if (timestamp < dts) {
+ pos_max = pos;
+ dts_max = read_timestamp(s, stream_index, &pos_max, 0);
+ if (dts_max == AV_NOPTS_VALUE) {
+ /* should never happen */
+ break;
+ } else if (timestamp >= dts_max) {
+ pos = pos_max;
+ goto found;
+ }
+ } else {
+ pos_min = pos + block_align;
+ dts_min = read_timestamp(s, stream_index, &pos_min, 1);
+ if (dts_min == AV_NOPTS_VALUE) {
+ /* should never happen */
+ goto found;
+ } else if (timestamp <= dts_min) {
+ goto found;
+ }
+ }
+ }
+ pos = pos_min;
+ found:
+#ifdef DEBUG_SEEK
+ pos_min = pos;
+ dts_min = read_timestamp(s, stream_index, &pos_min, 1);
+ pos_min += block_align;
+ dts_max = read_timestamp(s, stream_index, &pos_min, 1);
+ printf("pos=0x%llx %0.3f<=%0.3f<=%0.3f\n",
+ pos, dts_min / 90000.0, timestamp / 90000.0, dts_max / 90000.0);
+#endif
+ /* do the seek */
+ url_fseek(&s->pb, pos, SEEK_SET);
+ return 0;
+}
+
+static int mpegts_read_seek(AVFormatContext *s,
+ int stream_index, int64_t timestamp)
+{
+ MpegTSContext *ts = s->priv_data;
+
+ timestamp = (timestamp * 90000) / AV_TIME_BASE;
+
+ return timestamp_read_seek(s, stream_index, timestamp,
+ mpegts_get_pcr, ts->raw_packet_size);
+}
+
/**************************************************************/
/* parsing functions - called from other demuxers such as RTP */
@@ -1265,6 +1420,7 @@ AVInputFormat mpegts_demux = {
mpegts_read_header,
mpegts_read_packet,
mpegts_read_close,
+ mpegts_read_seek,
.flags = AVFMT_SHOW_IDS,
};