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

Francois Cartegnie fcartegnie at gmail.com
Thu Mar 7 15:31:27 CET 2019


Le 07/03/2019 à 15:25, Thomas Guillem a écrit :
> 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;

non exact and slower than integer/remain * timescale
What's the need ?

-- 
Francois Cartegnie
VideoLAN - VLC Developer


More information about the vlc-devel mailing list