Fix date_Increment when i_divider_den > 1
authorDavid Flynn <davidf@rd.bbc.co.uk>
Thu, 7 Aug 2008 19:39:02 +0000 (19:39 +0000)
committerDerk-Jan Hartman <hartman@videolan.org>
Sun, 10 Aug 2008 21:37:19 +0000 (23:37 +0200)
Previously, calling date_Increment(d,1) with divider_den > 1 would cause:
 - Uncorrectable rounding error due to ordering of / followed by *
 - 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.
 => Both cases are evident with num=30000, den=1001.

This fixes both issues.

Signed-off-by: David Flynn <davidf@rd.bbc.co.uk>
src/misc/mtime.c

index 5ab08c5..d5a809f 100644 (file)
@@ -427,6 +427,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;
 }
@@ -475,14 +477,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;
     }