gavl

gavltime.h
1/*****************************************************************
2 * gavl - a general purpose audio/video processing library
3 *
4 * Copyright (c) 2001 - 2024 Members of the Gmerlin project
5 * http://github.com/bplaum
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * *****************************************************************/
20
21
22
23#ifndef GAVLTIME_H_INCLUDED
24#define GAVLTIME_H_INCLUDED
25
26#ifdef __cplusplus
27 extern "C" {
28#endif
29
30#include <inttypes.h>
31#include <gavl/gavldefs.h>
32
35
38
39#define GAVL_TIME_SCALE 1000000
40
43
44#define GAVL_TIME_UNDEFINED 0x8000000000000000LL
47#define GAVL_TIME_MAX 0x7fffffffffffffffLL
48
53
54 typedef int64_t gavl_time_t;
55
56/* Utility functions */
57
61
62GAVL_PUBLIC
63 gavl_time_t gavl_samples_to_time(int samplerate, int64_t samples);
64
71
72GAVL_PUBLIC
73int64_t gavl_time_to_samples(int samplerate, gavl_time_t time);
74
78
79GAVL_PUBLIC
80 gavl_time_t gavl_frames_to_time(int rate_num, int rate_den, int64_t frames);
81
82
90
91GAVL_PUBLIC
92int64_t gavl_time_to_frames(int rate_num, int rate_den, gavl_time_t time);
93
100
101GAVL_PUBLIC
102int64_t gavl_time_scale(int scale, gavl_time_t time);
103
110
111GAVL_PUBLIC
112 gavl_time_t gavl_time_unscale(int scale, int64_t time);
113
121
122GAVL_PUBLIC
123int64_t gavl_time_rescale(int scale1, int scale2, int64_t time);
124
130
131#define gavl_seconds_to_time(s) (gavl_time_t)((s)*(double)(GAVL_TIME_SCALE))
132
138
139#define gavl_time_to_seconds(t) ((double)(t)/(double)(GAVL_TIME_SCALE))
140
145
146GAVL_PUBLIC
147 void gavl_time_delay(gavl_time_t * time);
148
152
153#define GAVL_TIME_STRING_LEN 11
154
163
164GAVL_PUBLIC void
165 gavl_time_prettyprint(gavl_time_t time, char str[GAVL_TIME_STRING_LEN]);
166
167/*
168 * Same as gavl_time_prettyprint but prints just hh:mm:ss in 24h format,
169 * which are converted to local time from UTC
170 */
171
172GAVL_PUBLIC void
173gavl_time_prettyprint_local(gavl_time_t gavl_time, char str[GAVL_TIME_STRING_LEN]);
174
175
179
180#define GAVL_TIME_STRING_LEN_MS 15
181
182/* 2022-10-10 23:05:12.000001 */
183#define GAVL_TIME_STRING_LEN_ABSOLUTE 27
184
195
196GAVL_PUBLIC void
197 gavl_time_prettyprint_ms(gavl_time_t time, char str[GAVL_TIME_STRING_LEN_MS]);
198
199GAVL_PUBLIC void
200gavl_time_prettyprint_ms_full(gavl_time_t time, char str[GAVL_TIME_STRING_LEN_MS]);
201
202GAVL_PUBLIC void
203gavl_time_prettyprint_absolute(gavl_time_t time, char * str, int local);
204
205GAVL_PUBLIC void
206gavl_time_prettyprint_absolute_full(gavl_time_t time, char * str, int local);
207
208/* Scan time: format is hhh:mm:ss with hh: hours, mm: minutes, ss: seconds. Seconds can be a fractional
209 value (i.e. with decimal point) */
210
211GAVL_PUBLIC
212 int gavl_time_parse(const char * str, gavl_time_t * ret);
213
214GAVL_PUBLIC
215 int gavl_time_parse_iso8601(const char * str, gavl_time_t * ret);
216
217GAVL_PUBLIC
218 gavl_time_t gavl_time_get_monotonic(void);
219
220/* Returns microseconds since the epoch */
221GAVL_PUBLIC
222 gavl_time_t gavl_time_get_realtime(void);
223
224/* Simple software timer */
225
233
239
240 typedef struct gavl_timer_s gavl_timer_t;
241
246
247GAVL_PUBLIC
248 gavl_timer_t * gavl_timer_create(void);
249
256
257GAVL_PUBLIC
258 void gavl_timer_destroy(gavl_timer_t * timer);
259
264
265GAVL_PUBLIC
266 void gavl_timer_start(gavl_timer_t * timer);
267
272
273GAVL_PUBLIC
274 void gavl_timer_stop(gavl_timer_t * timer);
275
281
282GAVL_PUBLIC
283 gavl_time_t gavl_timer_get(gavl_timer_t * timer);
284
290
291GAVL_PUBLIC
292 int gavl_timer_is_running(gavl_timer_t * timer);
293
294
300
301GAVL_PUBLIC
302 void gavl_timer_set(gavl_timer_t * timer, gavl_time_t t);
303
314
315GAVL_PUBLIC
316uint64_t gavl_benchmark_get_time(int flags);
317
325
326GAVL_PUBLIC
327 const char * gavl_benchmark_get_desc(int flags);
328
329
330#ifdef __cplusplus
331}
332#endif
333
334#endif /* GAVLTIME_H_INCLUDED */
335
GAVL_TIME_STRING_LEN_MS
#define GAVL_TIME_STRING_LEN_MS
Length of the string passed to gavl_time_prettyprint_ms.
Definition gavltime.h:180
gavl_time_to_frames
GAVL_PUBLIC int64_t gavl_time_to_frames(int rate_num, int rate_den, gavl_time_t time)
Convert a time to a number of video frames for a given framerate.
gavl_time_delay
GAVL_PUBLIC void gavl_time_delay(gavl_time_t *time)
Sleep for a specified time.
GAVL_TIME_STRING_LEN
#define GAVL_TIME_STRING_LEN
Length of the string passed to gavl_time_prettyprint.
Definition gavltime.h:153
gavl_time_unscale
GAVL_PUBLIC gavl_time_t gavl_time_unscale(int scale, int64_t time)
Convert a time scaled by another base to a gavl time.
gavl_frames_to_time
GAVL_PUBLIC gavl_time_t gavl_frames_to_time(int rate_num, int rate_den, int64_t frames)
Convert a number of video frames to a time for a given framerate.
gavl_time_prettyprint_ms
GAVL_PUBLIC void gavl_time_prettyprint_ms(gavl_time_t time, char str[GAVL_TIME_STRING_LEN_MS])
Convert a time to a string with millisecond precision.
gavl_samples_to_time
GAVL_PUBLIC gavl_time_t gavl_samples_to_time(int samplerate, int64_t samples)
Convert a number of samples to a time for a given samplerate.
gavl_time_scale
GAVL_PUBLIC int64_t gavl_time_scale(int scale, gavl_time_t time)
Convert a gavl time to a time scaled by another base.
gavl_time_rescale
GAVL_PUBLIC int64_t gavl_time_rescale(int scale1, int scale2, int64_t time)
Convert a time scaled by one base to a time scaled by another base.
gavl_time_to_samples
GAVL_PUBLIC int64_t gavl_time_to_samples(int samplerate, gavl_time_t time)
Convert a time to a number of audio samples for a given samplerate.
gavl_time_t
int64_t gavl_time_t
Times in gavl are 64 bit signed integers.
Definition gavltime.h:54
gavl_time_prettyprint
GAVL_PUBLIC void gavl_time_prettyprint(gavl_time_t time, char str[GAVL_TIME_STRING_LEN])
Convert a time to a string.
gavl_timer_is_running
GAVL_PUBLIC int gavl_timer_is_running(gavl_timer_t *timer)
Check if a timer is running.
gavl_timer_stop
GAVL_PUBLIC void gavl_timer_stop(gavl_timer_t *timer)
Stop a timer.
gavl_timer_destroy
GAVL_PUBLIC void gavl_timer_destroy(gavl_timer_t *timer)
Destroy a timer.
gavl_timer_start
GAVL_PUBLIC void gavl_timer_start(gavl_timer_t *timer)
Start a timer.
gavl_benchmark_get_desc
GAVL_PUBLIC const char * gavl_benchmark_get_desc(int flags)
Get a description about the value returned by gavl_benchmark_get_time.
gavl_benchmark_get_time
GAVL_PUBLIC uint64_t gavl_benchmark_get_time(int flags)
Get the current time for benchmarking.
gavl_timer_get
GAVL_PUBLIC gavl_time_t gavl_timer_get(gavl_timer_t *timer)
Get the current time of the timer.
gavl_timer_create
GAVL_PUBLIC gavl_timer_t * gavl_timer_create(void)
Create a timer.
gavl_timer_t
struct gavl_timer_s gavl_timer_t
Opaque timer structure.
Definition gavltime.h:240
gavl_timer_set
GAVL_PUBLIC void gavl_timer_set(gavl_timer_t *timer, gavl_time_t t)
Set the current time of the timer.