]> git.sesse.net Git - vlc/blob - src/misc/mtime.c
da0d8bfc374f0f4f5fe17760b8001cd88bcea91f
[vlc] / src / misc / mtime.c
1 /*****************************************************************************
2  * mtime.c: high resolution time management functions
3  * Functions are prototyped in vlc_mtime.h.
4  *****************************************************************************
5  * Copyright (C) 1998-2007 the VideoLAN team
6  * Copyright © 2006-2007 Rémi Denis-Courmont
7  * $Id$
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *          Rémi Denis-Courmont <rem$videolan,org>
11  *          Gisle Vanem
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <assert.h>
38
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #if !defined (_POSIX_TIMERS)
43 # define _POSIX_TIMERS (-1)
44 #endif
45 #if (_POSIX_TIMERS > 0)
46 # include <time.h> /* clock_gettime() */
47 #else
48 # include <sys/time.h>
49 #endif
50
51 /**
52  * Return a date in a readable format
53  *
54  * This function converts a mtime date into a string.
55  * psz_buffer should be a buffer long enough to store the formatted
56  * date.
57  * \param date to be converted
58  * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
59  * \return psz_buffer is returned so this can be used as printf parameter.
60  */
61 char *mstrtime( char *psz_buffer, mtime_t date )
62 {
63     static const mtime_t ll1000 = 1000, ll60 = 60, ll24 = 24;
64
65     snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%02d:%02d:%02d-%03d.%03d",
66              (int) (date / (ll1000 * ll1000 * ll60 * ll60) % ll24),
67              (int) (date / (ll1000 * ll1000 * ll60) % ll60),
68              (int) (date / (ll1000 * ll1000) % ll60),
69              (int) (date / ll1000 % ll1000),
70              (int) (date % ll1000) );
71     return( psz_buffer );
72 }
73
74 /**
75  * Convert seconds to a time in the format h:mm:ss.
76  *
77  * This function is provided for any interface function which need to print a
78  * time string in the format h:mm:ss
79  * date.
80  * \param secs  the date to be converted
81  * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
82  * \return psz_buffer is returned so this can be used as printf parameter.
83  */
84 char *secstotimestr( char *psz_buffer, int32_t i_seconds )
85 {
86     if( unlikely(i_seconds < 0) )
87     {
88         secstotimestr( psz_buffer + 1, -i_seconds );
89         *psz_buffer = '-';
90         return psz_buffer;
91     }
92
93     div_t d;
94
95     d = div( i_seconds, 60 );
96     i_seconds = d.rem;
97     d = div( d.quot, 60 );
98
99     if( d.quot )
100         snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%u:%02u:%02u",
101                  d.quot, d.rem, i_seconds );
102     else
103         snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%02u:%02u",
104                   d.rem, i_seconds );
105     return psz_buffer;
106 }
107
108 /*
109  * Date management (internal and external)
110  */
111
112 /**
113  * Initialize a date_t.
114  *
115  * \param date to initialize
116  * \param divider (sample rate) numerator
117  * \param divider (sample rate) denominator
118  */
119
120 void date_Init( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
121 {
122     p_date->date = 0;
123     p_date->i_divider_num = i_divider_n;
124     p_date->i_divider_den = i_divider_d;
125     p_date->i_remainder = 0;
126 }
127
128 /**
129  * Change a date_t.
130  *
131  * \param date to change
132  * \param divider (sample rate) numerator
133  * \param divider (sample rate) denominator
134  */
135
136 void date_Change( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
137 {
138     /* change time scale of remainder */
139     p_date->i_remainder = p_date->i_remainder * i_divider_n / p_date->i_divider_num;
140     p_date->i_divider_num = i_divider_n;
141     p_date->i_divider_den = i_divider_d;
142 }
143
144 /**
145  * Set the date value of a date_t.
146  *
147  * \param date to set
148  * \param date value
149  */
150 void date_Set( date_t *p_date, mtime_t i_new_date )
151 {
152     p_date->date = i_new_date;
153     p_date->i_remainder = 0;
154 }
155
156 /**
157  * Get the date of a date_t
158  *
159  * \param date to get
160  * \return date value
161  */
162 mtime_t date_Get( const date_t *p_date )
163 {
164     return p_date->date;
165 }
166
167 /**
168  * Move forwards or backwards the date of a date_t.
169  *
170  * \param date to move
171  * \param difference value
172  */
173 void date_Move( date_t *p_date, mtime_t i_difference )
174 {
175     p_date->date += i_difference;
176 }
177
178 /**
179  * Increment the date and return the result, taking into account
180  * rounding errors.
181  *
182  * \param date to increment
183  * \param incrementation in number of samples
184  * \return date value
185  */
186 mtime_t date_Increment( date_t *p_date, uint32_t i_nb_samples )
187 {
188     mtime_t i_dividend = i_nb_samples * CLOCK_FREQ * p_date->i_divider_den;
189     lldiv_t d = lldiv( i_dividend, p_date->i_divider_num );
190
191     p_date->date += d.quot;
192     p_date->i_remainder += (int)d.rem;
193
194     if( p_date->i_remainder >= p_date->i_divider_num )
195     {
196         /* This is Bresenham algorithm. */
197         assert( p_date->i_remainder < 2*p_date->i_divider_num);
198         p_date->date += 1;
199         p_date->i_remainder -= p_date->i_divider_num;
200     }
201
202     return p_date->date;
203 }
204
205 /**
206  * Decrement the date and return the result, taking into account
207  * rounding errors.
208  *
209  * \param date to decrement
210  * \param decrementation in number of samples
211  * \return date value
212  */
213 mtime_t date_Decrement( date_t *p_date, uint32_t i_nb_samples )
214 {
215     mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000 * p_date->i_divider_den;
216     p_date->date -= i_dividend / p_date->i_divider_num;
217     unsigned i_rem_adjust = i_dividend % p_date->i_divider_num;
218
219     if( p_date->i_remainder < i_rem_adjust )
220     {
221         /* This is Bresenham algorithm. */
222         assert( p_date->i_remainder > -p_date->i_divider_num);
223         p_date->date -= 1;
224         p_date->i_remainder += p_date->i_divider_num;
225     }
226
227     p_date->i_remainder -= i_rem_adjust;
228
229     return p_date->date;
230 }
231
232 /**
233  * @return NTP 64-bits timestamp in host byte order.
234  */
235 uint64_t NTPtime64 (void)
236 {
237 #if (_POSIX_TIMERS > 0)
238     struct timespec ts;
239
240     clock_gettime (CLOCK_REALTIME, &ts);
241 #else
242     struct timeval tv;
243     struct
244     {
245         uint32_t tv_sec;
246         uint32_t tv_nsec;
247     } ts;
248
249     gettimeofday (&tv, NULL);
250     ts.tv_sec = tv.tv_sec;
251     ts.tv_nsec = tv.tv_usec * 1000;
252 #endif
253
254     /* Convert nanoseconds to 32-bits fraction (232 picosecond units) */
255     uint64_t t = (uint64_t)(ts.tv_nsec) << 32;
256     t /= 1000000000;
257
258
259     /* There is 70 years (incl. 17 leap ones) offset to the Unix Epoch.
260      * No leap seconds during that period since they were not invented yet.
261      */
262     assert (t < 0x100000000);
263     t |= ((70LL * 365 + 17) * 24 * 60 * 60 + ts.tv_sec) << 32;
264     return t;
265 }
266