1 From fdd8caea6535434a877587f5325e914ba50ed17f Mon Sep 17 00:00:00 2001
2 From: Cory Fields <theuni-nospam-@xbmc.org>
3 Date: Fri, 9 Jul 2010 16:43:31 -0400
4 Subject: [PATCH 07/13] Read PID timestamps as well as PCR timestamps to find
5 location in mpegts stream
7 Patch part of the XBMC patch set for ffmpeg, downloaded from
8 https://github.com/xbmc/FFmpeg/.
10 Signed-off-by: Bernd Kuhls <bernd.kuhls@t-online.de>
11 Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
13 libavformat/mpegts.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++--
14 1 file changed, 46 insertions(+), 2 deletions(-)
16 diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
17 index 25007a6..d5a8a45 100644
18 --- a/libavformat/mpegts.c
19 +++ b/libavformat/mpegts.c
20 @@ -2459,6 +2459,44 @@ static void seek_back(AVFormatContext *s, AVIOContext *pb, int64_t pos) {
21 av_log(s, pb->seekable ? AV_LOG_ERROR : AV_LOG_INFO, "Unable to seek back to the start\n");
24 +static int parse_timestamp(int64_t *ts, const uint8_t *buf)
29 + if(!(buf[1] & 0x40)) /* must be a start packet */
32 + afc = (buf[3] >> 4) & 3;
34 + if (afc == 0 || afc == 2) /* invalid or only adaption field */
38 + if (p >= buf + TS_PACKET_SIZE)
41 + if (p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01) /* packet_start_code_prefix */
44 + flags = p[3] | 0x100; /* stream type */
45 + if (!((flags >= 0x1c0 && flags <= 0x1df) ||
46 + (flags >= 0x1e0 && flags <= 0x1ef) ||
47 + (flags == 0x1bd) || (flags == 0x1fd)))
51 + if ((flags & 0xc0) == 0x80) {
52 + *ts = ff_parse_pes_pts(p+9);
54 + } else if ((flags & 0xc0) == 0xc0) {
55 + *ts = ff_parse_pes_pts(p+9+5);
62 static int mpegts_read_header(AVFormatContext *s)
64 MpegTSContext *ts = s->priv_data;
65 @@ -2658,6 +2696,7 @@ static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
66 uint8_t buf[TS_PACKET_SIZE];
68 ((PESContext *)s->streams[stream_index]->priv_data)->pcr_pid;
69 + int pid = ((PESContext*)s->streams[stream_index]->priv_data)->pid;
70 int pos47 = ts->pos47_full % ts->raw_packet_size;
72 ((*ppos + ts->raw_packet_size - 1 - pos47) / ts->raw_packet_size) *
73 @@ -2679,6 +2718,11 @@ static av_unused int64_t mpegts_get_pcr(AVFormatContext *s, int stream_index,
77 + if ((pid < 0 || (AV_RB16(buf + 1) & 0x1fff) == pid) &&
78 + parse_timestamp(×tamp, buf) == 0) {
82 pos += ts->raw_packet_size;
85 @@ -2778,7 +2822,7 @@ AVInputFormat ff_mpegts_demuxer = {
86 .read_header = mpegts_read_header,
87 .read_packet = mpegts_read_packet,
88 .read_close = mpegts_read_close,
89 - .read_timestamp = mpegts_get_dts,
90 + .read_timestamp = mpegts_get_pcr,
91 .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
92 .priv_class = &mpegts_class,
94 @@ -2790,7 +2834,7 @@ AVInputFormat ff_mpegtsraw_demuxer = {
95 .read_header = mpegts_read_header,
96 .read_packet = mpegts_raw_read_packet,
97 .read_close = mpegts_read_close,
98 - .read_timestamp = mpegts_get_dts,
99 + .read_timestamp = mpegts_get_pcr,
100 .flags = AVFMT_SHOW_IDS | AVFMT_TS_DISCONT,
101 .priv_class = &mpegtsraw_class,