[vlc-devel] [PATCH 01/18] clock: Avg: use double

Thomas Guillem thomas at gllm.fr
Thu Mar 7 15:25:23 CET 2019


From: Denis Charmet <typx at videolan.org>

Signed-off-by: Thomas Guillem <thomas at gllm.fr>
---
 src/clock/clock_internal.c | 55 +++++++++++++++++++++-----------------
 src/clock/clock_internal.h | 27 ++++++++++---------
 src/clock/input_clock.c    |  2 +-
 3 files changed, 45 insertions(+), 39 deletions(-)

diff --git a/src/clock/clock_internal.c b/src/clock/clock_internal.c
index 49711cbf01..3352f3f7ec 100644
--- a/src/clock/clock_internal.c
+++ b/src/clock/clock_internal.c
@@ -30,47 +30,52 @@
 /*****************************************************************************
  * Long term average helpers
  *****************************************************************************/
-void AvgInit( average_t *p_avg, int i_divider )
+void AvgInit(average_t *avg, int range)
 {
-    p_avg->i_divider = i_divider;
-    AvgReset( p_avg );
+    avg->range = range;
+    AvgReset(avg);
 }
 
-void AvgClean( average_t *p_avg )
+void AvgClean(average_t * avg)
 {
-    VLC_UNUSED(p_avg);
+    VLC_UNUSED(avg);
 }
 
-void AvgReset( average_t *p_avg )
+void AvgReset(average_t *avg)
 {
-    p_avg->i_value = 0;
-    p_avg->i_residue = 0;
-    p_avg->i_count = 0;
+    avg->value = 0.0f;
+    avg->count = 0;
 }
 
-void AvgUpdate( average_t *p_avg, vlc_tick_t i_value )
+void AvgUpdate(average_t *avg, double value)
 {
-    const int i_f0 = __MIN( p_avg->i_divider - 1, p_avg->i_count );
-    const int i_f1 = p_avg->i_divider - i_f0;
+    const int new_value_weight = 1;
+    int average_weight;
+    int divider;
+    if (avg->count < avg->range)
+    {
+        average_weight = avg->count++;
+        divider = avg->count;
+    }
+    else
+    {
+        average_weight = avg->range - 1;
+        divider = avg->range;
+    }
 
-    const vlc_tick_t i_tmp = i_f0 * p_avg->i_value + i_f1 * i_value + p_avg->i_residue;
-
-    p_avg->i_value   = i_tmp / p_avg->i_divider;
-    p_avg->i_residue = i_tmp % p_avg->i_divider;
-
-    p_avg->i_count++;
+    const double tmp = average_weight * avg->value + new_value_weight * value;
+    avg->value = tmp / divider;
 }
 
-vlc_tick_t AvgGet( average_t *p_avg )
+double AvgGet(average_t *avg)
 {
-    return p_avg->i_value;
+    return avg->value;
 }
 
-void AvgRescale( average_t *p_avg, int i_divider )
+void AvgRescale(average_t *avg, int range)
 {
-    const vlc_tick_t i_tmp = p_avg->i_value * p_avg->i_divider + p_avg->i_residue;
+    const double tmp = avg->value * avg->range;
 
-    p_avg->i_divider = i_divider;
-    p_avg->i_value   = i_tmp / p_avg->i_divider;
-    p_avg->i_residue = i_tmp % p_avg->i_divider;
+    avg->range = range;
+    avg->value = tmp / avg->range;
 }
diff --git a/src/clock/clock_internal.h b/src/clock/clock_internal.h
index 21da59c075..fa0bc2dedc 100644
--- a/src/clock/clock_internal.h
+++ b/src/clock/clock_internal.h
@@ -27,25 +27,26 @@
  * Structures
  *****************************************************************************/
 
- /**
- * This structure holds long term average
+/**
+ * This structure holds long term moving average
  */
 typedef struct
 {
-    vlc_tick_t i_value;
-    int     i_residue;
-
-    int     i_count;
-    int     i_divider;
+    double value; /* The average value */
+    int count; /* The number of sample evaluated */
+    int range; /* The maximum range of sample on which we calculate the average*/
 } average_t;
 
-void    AvgInit( average_t *, int i_divider );
-void    AvgClean( average_t * );
+void AvgInit(average_t *, int range);
+void AvgClean(average_t *);
+
+void AvgReset(average_t *);
+
+/*  calculates (previous_average * (range - 1) + new_value)/range */
+void AvgUpdate(average_t *, double value);
 
-void    AvgReset( average_t * );
-void    AvgUpdate( average_t *, vlc_tick_t i_value );
-vlc_tick_t AvgGet( average_t * );
-void    AvgRescale( average_t *, int i_divider );
+double AvgGet(average_t *);
+void AvgRescale(average_t *, int range);
 
 /* */
 typedef struct
diff --git a/src/clock/input_clock.c b/src/clock/input_clock.c
index 3729c74e03..e76dae1ecd 100644
--- a/src/clock/input_clock.c
+++ b/src/clock/input_clock.c
@@ -532,7 +532,7 @@ void input_clock_SetJitter( input_clock_t *cl,
     if( i_cr_average < 10 )
         i_cr_average = 10;
 
-    if( cl->drift.i_divider != i_cr_average )
+    if( cl->drift.range != i_cr_average )
         AvgRescale( &cl->drift, i_cr_average );
 
     vlc_mutex_unlock( &cl->lock );
-- 
2.20.1



More information about the vlc-devel mailing list