From 8b9df201dfb2fa557b5dfc4e04c927d3f13a0dd9 Mon Sep 17 00:00:00 2001 From: Zohar Kelrich Date: Sun, 24 Jul 2011 11:13:50 +0300 Subject: mpegts: Fix for continuity counter Make continuity counter respect discontinuity flag and null packets. Unpack the adaptation_field_control field. Signed-off-by: Zohar Kelrich Signed-off-by: Luca Barbato --- libavformat/mpegts.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'libavformat/mpegts.c') diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c index 410507db6d..13b31177a5 100644 --- a/libavformat/mpegts.c +++ b/libavformat/mpegts.c @@ -1248,7 +1248,8 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet) { AVFormatContext *s = ts->stream; MpegTSFilter *tss; - int len, pid, cc, expected_cc, cc_ok, afc, is_start; + int len, pid, cc, expected_cc, cc_ok, afc, is_start, is_discontinuity, + has_adaptation, has_payload; const uint8_t *p, *p_end; int64_t pos; @@ -1264,20 +1265,29 @@ static int handle_packet(MpegTSContext *ts, const uint8_t *packet) if (!tss) return 0; + afc = (packet[3] >> 4) & 3; + if (afc == 0) /* reserved value */ + return 0; + has_adaptation = afc & 2; + has_payload = afc & 1; + is_discontinuity = has_adaptation + && packet[4] != 0 /* with length > 0 */ + && (packet[5] & 0x80); /* and discontinuity indicated */ + /* continuity check (currently not used) */ cc = (packet[3] & 0xf); - expected_cc = (packet[3] & 0x10) ? (tss->last_cc + 1) & 0x0f : tss->last_cc; - cc_ok = (tss->last_cc < 0) || (expected_cc == cc); + expected_cc = has_payload ? (tss->last_cc + 1) & 0x0f : tss->last_cc; + cc_ok = pid == 0x1FFF // null packet PID + || is_discontinuity + || tss->last_cc < 0 + || expected_cc == cc; + tss->last_cc = cc; - /* skip adaptation field */ - afc = (packet[3] >> 4) & 3; - p = packet + 4; - if (afc == 0) /* reserved value */ + if (!has_payload) return 0; - if (afc == 2) /* adaptation field only */ - return 0; - if (afc == 3) { + p = packet + 4; + if (has_adaptation) { /* skip adapation field */ p += p[0] + 1; } -- cgit v1.2.3