[vlc-devel] [PATCH] Fix date_Increment when i_divider_den > 1

David Flynn davidf+nntp at woaf.net
Thu Aug 7 21:39:02 CEST 2008


From: David Flynn <davidf at rd.bbc.co.uk>

Previously, calling date_Increment(d,1) with divider_den > 1 would cause:
 - Uncorrectable rounding error due to ordering of / followed by *
 - Incorrect calculation of i_remainder
 - If i_divider_num / i_divider_den is not integral, a remainder is
   accumulated, but not divided by i_divider_num when added to date.
 => First&last cases are evident with num=30000, den=1001.

This fixes above issues.

Signed-off-by: David Flynn <davidf at rd.bbc.co.uk>
---
 src/misc/mtime.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/misc/mtime.c b/src/misc/mtime.c
index d9af9fd..2203034 100644
--- a/src/misc/mtime.c
+++ b/src/misc/mtime.c
@@ -425,6 +425,8 @@ void date_Init( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
 
 void date_Change( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
 {
+    /* change time scale of remainder */
+    p_date->i_remainder = p_date->i_remainder * i_divider_n / p_date->i_divider_num;
     p_date->i_divider_num = i_divider_n;
     p_date->i_divider_den = i_divider_d;
 }
@@ -473,14 +475,15 @@ void date_Move( date_t *p_date, mtime_t i_difference )
  */
 mtime_t date_Increment( date_t *p_date, uint32_t i_nb_samples )
 {
-    mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
-    p_date->date += i_dividend / p_date->i_divider_num * p_date->i_divider_den;
+    mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000 * p_date->i_divider_den;
+    p_date->date += i_dividend / p_date->i_divider_num;
     p_date->i_remainder += (int)(i_dividend % p_date->i_divider_num);
 
     if( p_date->i_remainder >= p_date->i_divider_num )
     {
         /* This is Bresenham algorithm. */
-        p_date->date += p_date->i_divider_den;
+        /* It is guaranteed that: assert(i_remainder < 2*i_divider_num) */
+        p_date->date += 1;
         p_date->i_remainder -= p_date->i_divider_num;
     }
 
-- 
1.5.6





More information about the vlc-devel mailing list