]> git.sesse.net Git - vlc/blob - src/video_output/chrono.h
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / video_output / chrono.h
1 /*****************************************************************************
2  * chrono.h: vout chrono
3  *****************************************************************************
4  * Copyright (C) 2009-2010 Laurent Aimar
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #if defined(__PLUGIN__) || defined(__BUILTIN__) || !defined(__LIBVLC__)
25 # error This header file can only be included from LibVLC.
26 #endif
27
28 #ifndef _VOUT_CHRONO_H
29 #define _VOUT_CHRONO_H
30
31 typedef struct {
32     int     shift;
33     mtime_t avg;
34     mtime_t avg_initial;
35
36     int     shift_var;
37     mtime_t var;
38
39     mtime_t start;
40 } vout_chrono_t;
41
42 static inline void vout_chrono_Init(vout_chrono_t *chrono, int shift, mtime_t avg_initial)
43 {
44     chrono->shift       = shift;
45     chrono->avg_initial =
46     chrono->avg         = avg_initial;
47
48     chrono->shift_var   = shift+1;
49     chrono->var         = avg_initial / 2;
50
51     chrono->start = VLC_TS_INVALID;
52 }
53 static inline void vout_chrono_Clean(vout_chrono_t *chrono)
54 {
55     VLC_UNUSED(chrono);
56 }
57 static inline void vout_chrono_Start(vout_chrono_t *chrono)
58 {
59     chrono->start = mdate();
60 }
61 static inline mtime_t vout_chrono_GetHigh(vout_chrono_t *chrono)
62 {
63     return chrono->avg + 2 * chrono->var;
64 }
65 static inline mtime_t vout_chrono_GetLow(vout_chrono_t *chrono)
66 {
67     return __MAX(chrono->avg - 2 * chrono->var, 0);
68 }
69
70 static inline void vout_chrono_Stop(vout_chrono_t *chrono)
71 {
72     assert(chrono->start != VLC_TS_INVALID);
73
74     /* */
75     const mtime_t duration = mdate() - chrono->start;
76     const mtime_t var = llabs( duration - chrono->avg );
77
78     /* Update average only if the current point is 'valid' */
79     if( duration < vout_chrono_GetHigh( chrono ) )
80         chrono->avg = (((1 << chrono->shift) - 1) * chrono->avg + duration) >> chrono->shift;
81     /* Always update the variance */
82     chrono->var = (((1 << chrono->shift_var) - 1) * chrono->var + var) >> chrono->shift_var;
83
84     /* For assert */
85     chrono->start = VLC_TS_INVALID;
86 }
87 static inline void vout_chrono_Reset(vout_chrono_t *chrono)
88 {
89     vout_chrono_t ch = *chrono;
90     vout_chrono_Clean(chrono);
91     vout_chrono_Init(chrono, ch.shift, ch.avg_initial);
92 }
93
94 #endif