[vlc-devel] [PATCH] clock: Cast floating point scaled timestamps back to vlc_tick_t before adding offsets

Martin Storsjö martin at martin.st
Wed Mar 27 12:56:24 CET 2019


Since the absolute values of vlc_tick_t times (especially for
system times) can be rather large, handling them in floating point
form can lose precision.

This is a concrete issue with input_clock_t, where rate is float,
which only stores 24 bits of precision, while a system vlc_tick_t
measured since system boot easily exceeds 40 bits.

clock.c does similar rescalings, but the rate variables are double
there, which only would be a problem if handling absolute timestamps
over 53 bits.
---
 src/clock/input_clock.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/clock/input_clock.c b/src/clock/input_clock.c
index 3e9bfc6a60..c17a28bf72 100644
--- a/src/clock/input_clock.c
+++ b/src/clock/input_clock.c
@@ -316,7 +316,7 @@ void input_clock_ChangeRate( input_clock_t *cl, float rate )
     {
         /* Move the reference point (as if we were playing at the new rate
          * from the start */
-        cl->ref.system = cl->last.system - (cl->last.system - cl->ref.system) / rate * cl->rate;
+        cl->ref.system = cl->last.system - (vlc_tick_t) ((cl->last.system - cl->ref.system) / rate * cl->rate);
     }
     cl->rate = rate;
 
@@ -568,7 +568,7 @@ static vlc_tick_t ClockStreamToSystem( input_clock_t *cl, vlc_tick_t i_stream )
     if( !cl->b_has_reference )
         return VLC_TICK_INVALID;
 
-    return ( i_stream - cl->ref.stream ) / cl->rate + cl->ref.system;
+    return (vlc_tick_t) (( i_stream - cl->ref.stream ) / cl->rate) + cl->ref.system;
 }
 
 /*****************************************************************************
@@ -579,7 +579,7 @@ static vlc_tick_t ClockStreamToSystem( input_clock_t *cl, vlc_tick_t i_stream )
 static vlc_tick_t ClockSystemToStream( input_clock_t *cl, vlc_tick_t i_system )
 {
     assert( cl->b_has_reference );
-    return ( i_system - cl->ref.system ) * cl->rate + cl->ref.stream;
+    return (vlc_tick_t) (( i_system - cl->ref.system ) * cl->rate) + cl->ref.stream;
 }
 
 /**
-- 
2.17.1



More information about the vlc-devel mailing list