chrono.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * chrono.h: vout chrono
00003  *****************************************************************************
00004  * Copyright (C) 2009-2010 Laurent Aimar
00005  * $Id: 8a22511c5a72b3164f4b3ab6798fb1d7e879c1b4 $
00006  *
00007  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
00008  *
00009  * This program is free software; you can redistribute it and/or modify it
00010  * under the terms of the GNU Lesser General Public License as published by
00011  * the Free Software Foundation; either version 2.1 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00017  * GNU Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public License
00020  * along with this program; if not, write to the Free Software Foundation,
00021  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
00022  *****************************************************************************/
00023 
00024 #ifndef LIBVLC_VOUT_CHRONO_H
00025 #define LIBVLC_VOUT_CHRONO_H
00026 
00027 typedef struct {
00028     int     shift;
00029     mtime_t avg;
00030     mtime_t avg_initial;
00031 
00032     int     shift_var;
00033     mtime_t var;
00034 
00035     mtime_t start;
00036 } vout_chrono_t;
00037 
00038 static inline void vout_chrono_Init(vout_chrono_t *chrono, int shift, mtime_t avg_initial)
00039 {
00040     chrono->shift       = shift;
00041     chrono->avg_initial =
00042     chrono->avg         = avg_initial;
00043 
00044     chrono->shift_var   = shift+1;
00045     chrono->var         = avg_initial / 2;
00046 
00047     chrono->start = VLC_TS_INVALID;
00048 }
00049 static inline void vout_chrono_Clean(vout_chrono_t *chrono)
00050 {
00051     VLC_UNUSED(chrono);
00052 }
00053 static inline void vout_chrono_Start(vout_chrono_t *chrono)
00054 {
00055     chrono->start = mdate();
00056 }
00057 static inline mtime_t vout_chrono_GetHigh(vout_chrono_t *chrono)
00058 {
00059     return chrono->avg + 2 * chrono->var;
00060 }
00061 static inline mtime_t vout_chrono_GetLow(vout_chrono_t *chrono)
00062 {
00063     return __MAX(chrono->avg - 2 * chrono->var, 0);
00064 }
00065 
00066 static inline void vout_chrono_Stop(vout_chrono_t *chrono)
00067 {
00068     assert(chrono->start != VLC_TS_INVALID);
00069 
00070     /* */
00071     const mtime_t duration = mdate() - chrono->start;
00072     const mtime_t var = llabs( duration - chrono->avg );
00073 
00074     /* Update average only if the current point is 'valid' */
00075     if( duration < vout_chrono_GetHigh( chrono ) )
00076         chrono->avg = (((1 << chrono->shift) - 1) * chrono->avg + duration) >> chrono->shift;
00077     /* Always update the variance */
00078     chrono->var = (((1 << chrono->shift_var) - 1) * chrono->var + var) >> chrono->shift_var;
00079 
00080     /* For assert */
00081     chrono->start = VLC_TS_INVALID;
00082 }
00083 static inline void vout_chrono_Reset(vout_chrono_t *chrono)
00084 {
00085     vout_chrono_t ch = *chrono;
00086     vout_chrono_Clean(chrono);
00087     vout_chrono_Init(chrono, ch.shift, ch.avg_initial);
00088 }
00089 
00090 #endif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines