From 133e5b3a51945a2096f568e99aea03ac00d7edbc Mon Sep 17 00:00:00 2001 From: John Cox Date: Thu, 14 Mar 2013 13:27:34 +0000 Subject: [PATCH] Fix tsplay when PCR wraps tsplay used to think that wrap was a discontinuity. This was safe enough but glitched the output. This fixes that. --- tswrite.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tswrite.c b/tswrite.c index 878c412..95eb20d 100644 --- a/tswrite.c +++ b/tswrite.c @@ -523,6 +523,15 @@ static int free_buffered_TS_output(buffered_TS_output_p *writer) *writer = NULL; return 0; } + + +// Get a useful unsigned diff between two PCRs allowing for wrap through zero +#define PCR_WRAP (0x200000000LL * 300LL) + +static inline uint64_t pcr_delta_u(const uint64_t a, const uint64_t b) +{ + return a < b ? a + PCR_WRAP - b : a - b; +} // ============================================================ // Timing @@ -625,7 +634,9 @@ static int set_buffer_item_time_pcr(buffered_TS_output_p writer) if (found_pcr) { - if (writer->packet[ii].pcr < last_pcr) + uint64_t delta_pcr = pcr_delta_u(writer->packet[ii].pcr, last_pcr); + + if (delta_pcr > 2 * 90000LL) { // We've suffered a discontinuity (quite likely because we've looped // back to the start of the file). We plainly don't want to continue @@ -649,7 +660,6 @@ static int set_buffer_item_time_pcr(buffered_TS_output_p writer) else { // This is our second or later PCR - we can calculate interesting things - uint64_t delta_pcr = writer->packet[ii].pcr - last_pcr; int delta_bytes = (writer->packet[ii].index-last_pcr_index)*TS_PACKET_SIZE; int extra_bytes; double extra_time;