sout: sdi: fix channels to pairs setup
[vlc.git] / src / misc / mtime.c
blobe3f87c895cb07e675bbd19e6515115085fbb21fb
1 /*****************************************************************************
2 * mtime.c: high resolution time management functions
3 * Functions are prototyped in vlc_tick.h.
4 *****************************************************************************
5 * Copyright (C) 1998-2007 VLC authors and VideoLAN
6 * Copyright © 2006-2007 Rémi Denis-Courmont
7 * $Id$
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10 * Rémi Denis-Courmont <rem$videolan,org>
11 * Gisle Vanem
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
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 Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 /*****************************************************************************
29 * Preamble
30 *****************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
36 #include <vlc_common.h>
37 #include <assert.h>
39 #include <time.h>
40 #include <stdlib.h>
42 /**
43 * Convert seconds to a time in the format h:mm:ss.
45 * This function is provided for any interface function which need to print a
46 * time string in the format h:mm:ss
47 * date.
48 * \param secs the date to be converted
49 * \param psz_buffer should be a buffer at least MSTRTIME_MAX_SIZE characters
50 * \return psz_buffer is returned so this can be used as printf parameter.
52 char *secstotimestr( char *psz_buffer, int32_t i_seconds )
54 if( unlikely(i_seconds < 0) )
56 secstotimestr( psz_buffer + 1, -i_seconds );
57 *psz_buffer = '-';
58 return psz_buffer;
61 div_t d;
63 d = div( i_seconds, 60 );
64 i_seconds = d.rem;
65 d = div( d.quot, 60 );
67 if( d.quot )
68 snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%u:%02u:%02u",
69 d.quot, d.rem, i_seconds );
70 else
71 snprintf( psz_buffer, MSTRTIME_MAX_SIZE, "%02u:%02u",
72 d.rem, i_seconds );
73 return psz_buffer;
77 * Date management (internal and external)
80 void date_Init( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
82 p_date->date = VLC_TICK_INVALID;
83 p_date->i_divider_num = i_divider_n;
84 p_date->i_divider_den = i_divider_d;
85 p_date->i_remainder = 0;
88 void date_Change( date_t *p_date, uint32_t i_divider_n, uint32_t i_divider_d )
90 /* change time scale of remainder */
91 p_date->i_remainder = p_date->i_remainder * i_divider_n / p_date->i_divider_num;
92 p_date->i_divider_num = i_divider_n;
93 p_date->i_divider_den = i_divider_d;
96 vlc_tick_t date_Increment( date_t *p_date, uint32_t i_nb_samples )
98 if(unlikely(p_date->date == VLC_TICK_INVALID))
99 return VLC_TICK_INVALID;
100 assert( p_date->i_divider_num != 0 );
101 vlc_tick_t i_dividend = i_nb_samples * CLOCK_FREQ * p_date->i_divider_den;
102 lldiv_t d = lldiv( i_dividend, p_date->i_divider_num );
104 p_date->date += d.quot;
105 p_date->i_remainder += (int)d.rem;
107 if( p_date->i_remainder >= p_date->i_divider_num )
109 /* This is Bresenham algorithm. */
110 assert( p_date->i_remainder < 2*p_date->i_divider_num);
111 p_date->date += 1;
112 p_date->i_remainder -= p_date->i_divider_num;
115 return p_date->date;
118 vlc_tick_t date_Decrement( date_t *p_date, uint32_t i_nb_samples )
120 if(unlikely(p_date->date == VLC_TICK_INVALID))
121 return VLC_TICK_INVALID;
122 vlc_tick_t i_dividend = (vlc_tick_t)i_nb_samples * CLOCK_FREQ * p_date->i_divider_den;
123 p_date->date -= i_dividend / p_date->i_divider_num;
124 unsigned i_rem_adjust = i_dividend % p_date->i_divider_num;
126 if( p_date->i_remainder < i_rem_adjust )
128 /* This is Bresenham algorithm. */
129 assert( p_date->i_remainder < p_date->i_divider_num);
130 p_date->date -= 1;
131 p_date->i_remainder += p_date->i_divider_num;
134 p_date->i_remainder -= i_rem_adjust;
136 return p_date->date;
140 * @return NTP 64-bits timestamp in host byte order.
142 uint64_t NTPtime64(void)
144 struct timespec ts;
146 timespec_get(&ts, TIME_UTC);
148 /* Convert nanoseconds to 32-bits fraction (232 picosecond units) */
149 uint64_t t = (uint64_t)(ts.tv_nsec) << 32;
150 t /= 1000000000;
152 /* The offset to Unix epoch is 70 years (incl. 17 leap ones). There were
153 * no leap seconds during that period since they had not been invented yet.
155 t |= ((UINT64_C(70) * 365 + 17) * 24 * 60 * 60 + ts.tv_sec) << 32;
156 return t;
159 struct timespec timespec_from_vlc_tick (vlc_tick_t date)
161 lldiv_t d = lldiv (date, CLOCK_FREQ);
162 struct timespec ts = { d.quot, NS_FROM_VLC_TICK( d.rem ) };
164 return ts;