>From 8e730153d48c7e1feeaab599c5c55d4944a58d00 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobias=20G=C3=BCntner?= <fatbull@web.de>
Date: Tue, 16 Aug 2011 21:23:48 +0000
Subject: [PATCH 3/3] Handle timestamp overflow

---
 modules/demux/ps.c |   11 +++++++++++
 modules/demux/ps.h |    3 +++
 2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/modules/demux/ps.c b/modules/demux/ps.c
index fd1621b..9b1e0e8 100644
--- a/modules/demux/ps.c
+++ b/modules/demux/ps.c
@@ -268,6 +268,17 @@ static void FindLength( demux_t *p_demux )
         {
             int64_t i_length = (int64_t)tk->i_last_pts - tk->i_first_pts;
 
+            /*
+             * Workaround if timestamps overflow somewhere in a stream.
+             * Note that this only works for one overflow. To find more
+             * overflows, we would have to scan the whole stream. But
+             * since overflows occur (roughly) every 26 hours and most
+             * videos are considerably shorter or do not contain an
+             * overflow at all, this ought to be good enough.
+             */
+            if( i_length < 0 )
+                i_length += PS_PTS_OVERFLOW;
+
             assert( p_sys->i_length >= 0 );
             if( i_length > p_sys->i_length )
             {
diff --git a/modules/demux/ps.h b/modules/demux/ps.h
index 755e3db..b763eb4 100644
--- a/modules/demux/ps.h
+++ b/modules/demux/ps.h
@@ -45,6 +45,9 @@ static inline int ps_id_to_tk( unsigned i_id )
 #define PS_ID_TO_TK( id ) ps_id_to_tk( id )
 #endif
 
+/* Timestamp overflow period (converted to VLC clock) */
+#define PS_PTS_OVERFLOW (((int64_t)1 << 33) * 100 / 9)
+
 typedef struct ps_psm_t ps_psm_t;
 static inline int ps_id_to_type( const ps_psm_t *, int );
 static inline const uint8_t *ps_id_to_lang( const ps_psm_t *, int );
-- 
1.7.0.4

