]> git.sesse.net Git - vlc/commitdiff
avformat : fix overflow in timestamp computation
authorSébastien Escudier <sebastien-devel@celeos.eu>
Fri, 24 Feb 2012 14:28:28 +0000 (15:28 +0100)
committerSébastien Escudier <sebastien-devel@celeos.eu>
Mon, 27 Feb 2012 08:12:32 +0000 (09:12 +0100)
modules/demux/avformat/demux.c

index 7356bfe2c6ed952f49fa3b2eebae5e2f8c402d50..3264b115d4dd8fda55c7daeb433c276b59d59471 100644 (file)
@@ -609,17 +609,37 @@ static int Demux( demux_t *p_demux )
     if( pkt.flags & AV_PKT_FLAG_KEY )
         p_frame->i_flags |= BLOCK_FLAG_TYPE_I;
 
-    i_start_time = ( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE ) ?
-        ( p_sys->ic->start_time * 1000000 / AV_TIME_BASE )  : 0;
-
-    p_frame->i_dts = ( pkt.dts == (int64_t)AV_NOPTS_VALUE ) ?
-        VLC_TS_INVALID : (pkt.dts) * 1000000 *
-        p_stream->time_base.num /
-        p_stream->time_base.den - i_start_time + VLC_TS_0;
-    p_frame->i_pts = ( pkt.pts == (int64_t)AV_NOPTS_VALUE ) ?
-        VLC_TS_INVALID : (pkt.pts) * 1000000 *
-        p_stream->time_base.num /
-        p_stream->time_base.den - i_start_time + VLC_TS_0;
+    /* Used to avoid timestamps overlow */
+    lldiv_t q;
+    if( p_sys->ic->start_time != (int64_t)AV_NOPTS_VALUE )
+    {
+        q = lldiv( p_sys->ic->start_time, AV_TIME_BASE);
+        i_start_time = q.quot * (int64_t)1000000 + q.rem * (int64_t)1000000 / AV_TIME_BASE;
+    }
+    else
+        i_start_time = 0;
+
+    if( pkt.dts == (int64_t)AV_NOPTS_VALUE )
+        p_frame->i_dts = VLC_TS_INVALID;
+    else
+    {
+        q = lldiv( pkt.dts, p_stream->time_base.den );
+        p_frame->i_dts = q.quot * (int64_t)1000000 *
+            p_stream->time_base.num + q.rem * (int64_t)1000000 *
+            p_stream->time_base.num /
+            p_stream->time_base.den - i_start_time + VLC_TS_0;
+    }
+
+    if( pkt.pts == (int64_t)AV_NOPTS_VALUE )
+        p_frame->i_pts = VLC_TS_INVALID;
+    else
+    {
+        q = lldiv( pkt.pts, p_stream->time_base.den );
+        p_frame->i_pts = q.quot * (int64_t)1000000 *
+            p_stream->time_base.num + q.rem * (int64_t)1000000 *
+            p_stream->time_base.num /
+            p_stream->time_base.den - i_start_time + VLC_TS_0;
+    }
     if( pkt.duration > 0 && p_frame->i_length <= 0 )
         p_frame->i_length = pkt.duration * 1000000 *
             p_stream->time_base.num /