VLC  4.0.0-dev
chrono.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * chrono.h: vout chrono
3  *****************************************************************************
4  * Copyright (C) 2009-2010 Laurent Aimar
5  *
6  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22 
23 #ifndef LIBVLC_VOUT_CHRONO_H
24 #define LIBVLC_VOUT_CHRONO_H
25 
26 #include <assert.h>
27 
28 typedef struct {
29  int shift;
31  unsigned avg_count;
32 
33  int shift_mad;
34  vlc_tick_t mad; /* mean absolute deviation */
35  unsigned mad_count;
36 
39 
40 static inline void vout_chrono_Init(vout_chrono_t *chrono, int shift, vlc_tick_t avg_initial)
41 {
42  chrono->avg_count = 0;
43  chrono->mad_count = 0;
44 
45  chrono->shift = shift;
46  chrono->avg = avg_initial;
47 
48  chrono->shift_mad = shift+1;
49  chrono->mad = 0;
50 
51  chrono->start = VLC_TICK_INVALID;
52 }
53 
54 static inline void vout_chrono_Start(vout_chrono_t *chrono)
55 {
56  chrono->start = vlc_tick_now();
57 }
58 
60 {
61  return chrono->avg + 2 * chrono->mad;
62 }
63 
65 {
66  return __MAX(chrono->avg - 2 * chrono->mad, 0);
67 }
68 
69 static inline void vout_chrono_Stop(vout_chrono_t *chrono)
70 {
71  assert(chrono->start != VLC_TICK_INVALID);
72 
73  const vlc_tick_t duration = vlc_tick_now() - chrono->start;
74 
75  if (chrono->avg_count == 0)
76  {
77  /* Overwrite the arbitrary initial values with the real first sample */
78  chrono->avg = duration;
79  chrono->avg_count = 1;
80  }
81  else
82  {
83  /* Update average only if the current point is 'valid' */
84  if( duration < vout_chrono_GetHigh( chrono ) )
85  {
86  if (chrono->avg_count < (1u << chrono->shift))
87  ++chrono->avg_count;
88  chrono->avg = ((chrono->avg_count - 1) * chrono->avg + duration) / chrono->avg_count;
89  }
90 
91  const vlc_tick_t abs_diff = llabs( duration - chrono->avg );
92 
93  /* Always update the mean absolute deviation */
94  if (chrono->mad_count < (1u << chrono->shift_mad))
95  ++chrono->mad_count;
96  chrono->mad = ((chrono->mad_count - 1) * chrono->mad + abs_diff) / chrono->mad_count;
97  }
98 
99  /* For assert */
100  chrono->start = VLC_TICK_INVALID;
101 }
102 
103 #endif
static vlc_tick_t vout_chrono_GetHigh(vout_chrono_t *chrono)
Definition: chrono.h:59
static void vout_chrono_Start(vout_chrono_t *chrono)
Definition: chrono.h:54
static void vout_chrono_Init(vout_chrono_t *chrono, int shift, vlc_tick_t avg_initial)
Definition: chrono.h:40
static vlc_tick_t vout_chrono_GetLow(vout_chrono_t *chrono)
Definition: chrono.h:64
static void vout_chrono_Stop(vout_chrono_t *chrono)
Definition: chrono.h:69
vlc_tick_t vlc_tick_now(void)
Precision monotonic clock.
Definition: thread.c:234
Definition: chrono.h:28
unsigned mad_count
Definition: chrono.h:35
vlc_tick_t avg
Definition: chrono.h:30
vlc_tick_t start
Definition: chrono.h:37
int shift
Definition: chrono.h:29
vlc_tick_t mad
Definition: chrono.h:34
unsigned avg_count
Definition: chrono.h:31
int shift_mad
Definition: chrono.h:33
#define VLC_TICK_INVALID
Definition: vlc_config.h:44
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_tick.h:45