1 /*****************************************************************
2  * gavl - a general purpose audio/video processing library
3  *
4  * Copyright (c) 2001 - 2011 Members of the Gmerlin project
5  * gmerlin-general@lists.sourceforge.net
6  * http://gmerlin.sourceforge.net
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  * *****************************************************************/
21 
22 #ifndef GAVLTIME_H_INCLUDED
23 #define GAVLTIME_H_INCLUDED
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #include <inttypes.h>
30 #include <gavl/gavldefs.h>
31 
32 /*! \defgroup time Time
33  */
34 
35 /*! \ingroup time
36  */
37 
38 #define GAVL_TIME_SCALE     1000000 /*!< Generic time scale: Microsecond tics */
39 
40 /*! \ingroup time
41  */
42 
43 #define GAVL_TIME_UNDEFINED 0x8000000000000000LL /*!< Unknown or undefined time */
44 /*! \ingroup time
45  */
46 #define GAVL_TIME_MAX       0x7fffffffffffffffLL /*!< Maximum possible value */
47 
48 /*! \ingroup time
49  * \brief Times in gavl are 64 bit signed integers
50  *
51  */
52 
53 typedef int64_t gavl_time_t;
54 
55 /* Utility functions */
56 
57 /*! \ingroup time
58  * \brief Convert a number of samples to a time for a given samplerate
59  */
60 
61 GAVL_PUBLIC
62 gavl_time_t gavl_samples_to_time(int samplerate, int64_t samples);
63 
64 /*! \ingroup time
65  * \brief Convert a time to a number of audio samples for a given samplerate
66  * \param samplerate Samplerate
67  * \param time Time
68  * \returns Number of audio samples
69  */
70 
71 GAVL_PUBLIC
72 int64_t gavl_time_to_samples(int samplerate, gavl_time_t time);
73 
74 /*! \ingroup time
75  * \brief Convert a number of video frames to a time for a given framerate
76  */
77 
78 GAVL_PUBLIC
79 gavl_time_t gavl_frames_to_time(int rate_num, int rate_den, int64_t frames);
80 
81 
82 /*! \ingroup time
83  * \brief Convert a time to a number of video frames for a given framerate
84  * \param rate_num Numerator of the framerate
85  * \param rate_den Denominator of the framerate
86  * \param time Time
87  * \returns Number of frames
88  */
89 
90 GAVL_PUBLIC
91 int64_t gavl_time_to_frames(int rate_num, int rate_den, gavl_time_t time);
92 
93 /*! \ingroup time
94  * \brief Convert a gavl time to a time scaled by another base
95  * \param scale Time scale
96  * \param time Time scaled by \ref GAVL_TIME_SCALE
97  * \returns Time scaled by scale
98  */
99 
100 GAVL_PUBLIC
101 int64_t gavl_time_scale(int scale, gavl_time_t time);
102 
103 /*! \ingroup time
104  * \brief Convert a time scaled by another base to a gavl time
105  * \param scale Time scale
106  * \param time Time scaled by scale
107  * \returns Time scaled by \ref GAVL_TIME_SCALE
108  */
109 
110 GAVL_PUBLIC
111 gavl_time_t gavl_time_unscale(int scale, int64_t time);
112 
113 /*! \ingroup time
114  * \brief Convert a time scaled by one base to a time scaled by another base
115  * \param scale1 Initial time base
116  * \param scale2 New time base
117  * \param time Time scaled by scale1
118  * \returns Time scaled by scale2
119  */
120 
121 GAVL_PUBLIC
122 int64_t gavl_time_rescale(int scale1, int scale2, int64_t time);
123 
124 /*! \ingroup time
125  * \brief Convert seconds (as double) to a gavl time
126  * \param s Seconds as double
127  * \returns Integer time scaled by \ref GAVL_TIME_SCALE
128  */
129 
130 #define gavl_seconds_to_time(s) (gavl_time_t)((s)*(double)(GAVL_TIME_SCALE))
131 
132 /*! \ingroup time
133  * \brief Convert a gavl time to seconds (as double)
134  * \param t Integer time scaled by \ref GAVL_TIME_SCALE
135  * \returns Seconds as double
136  */
137 
138 #define gavl_time_to_seconds(t) ((double)(t)/(double)(GAVL_TIME_SCALE))
139 
140 /*! \ingroup time
141  * \brief Sleep for a specified time
142  * \param time Time after which execution of the current thread is resumed
143  */
144 
145 GAVL_PUBLIC
146 void gavl_time_delay(gavl_time_t * time);
147 
148 /*! \ingroup time
149  * \brief Length of the string passed to \ref gavl_time_prettyprint
150  */
151 
152 #define GAVL_TIME_STRING_LEN 11
153 
154 /*! \ingroup time
155  * \brief Convert a time to a string
156  * \param time Time to print
157  * \param str String
158  *
159  * This prints a gavl_time into ASCII string if a format suitable for player displays.
160  * The format is: -hhh:mm:ss
161  */
162 
163 GAVL_PUBLIC void
164 gavl_time_prettyprint(gavl_time_t time, char str[GAVL_TIME_STRING_LEN]);
165 
166 /*! \ingroup time
167  * \brief Length of the string passed to \ref gavl_time_prettyprint_ms
168  */
169 
170 #define GAVL_TIME_STRING_LEN_MS 15
171 
172 /*! \ingroup time
173  * \brief Convert a time to a string with millisecond precision
174  * \param time Time to print
175  * \param str String
176  *
177  * This prints a gavl_time into ASCII string if a format suitable for player displays.
178  * The format is: -hhh:mm:ss.MMM
179  *
180  * Since 1.1.1
181  */
182 
183 GAVL_PUBLIC void
184 gavl_time_prettyprint_ms(gavl_time_t time, char str[GAVL_TIME_STRING_LEN_MS]);
185 
186 
187 /* Scan time: format is hhh:mm:ss with hh: hours, mm: minutes, ss: seconds. Seconds can be a fractional
188    value (i.e. with decimal point) */
189 
190 GAVL_PUBLIC
191 int gavl_time_parse(const char * str, gavl_time_t * ret);
192 
193 
194 /* Simple software timer */
195 
196 /*! \defgroup timer Software timer
197  * \ingroup time
198  *
199  *  This is a simple software timer, which can be used for synchronization
200  *  purposes for cases wherer there is no synchronization with hardware devices
201  *  available.
202  */
203 
204 /*! \ingroup timer
205  * \brief Opaque timer structure
206  *
207  * You don't want to know what's inside.
208  */
209 
210 typedef struct gavl_timer_s gavl_timer_t;
211 
212 /*! \ingroup timer
213  * \brief Create a timer
214  * \returns A newly allocated timer
215  */
216 
217 GAVL_PUBLIC
218 gavl_timer_t * gavl_timer_create();
219 
220 /*! \ingroup timer
221  * \brief Destroy a timer
222  * \param timer A timer
223  *
224  * Destroys a timer and frees all associated memory
225  */
226 
227 GAVL_PUBLIC
228 void gavl_timer_destroy(gavl_timer_t * timer);
229 
230 /*! \ingroup timer
231  * \brief Start a timer
232  * \param timer A timer
233  */
234 
235 GAVL_PUBLIC
236 void gavl_timer_start(gavl_timer_t * timer);
237 
238 /*! \ingroup timer
239  * \brief Stop a timer
240  * \param timer A timer
241  */
242 
243 GAVL_PUBLIC
244 void gavl_timer_stop(gavl_timer_t * timer);
245 
246 /*! \ingroup timer
247  * \brief Get the current time of the timer
248  * \param timer A timer
249  * \returns Current time
250  */
251 
252 GAVL_PUBLIC
253 gavl_time_t gavl_timer_get(gavl_timer_t * timer);
254 
255 /*! \ingroup timer
256  * \brief Set the current time of the timer
257  * \param timer A timer
258  * \param t New time
259  */
260 
261 GAVL_PUBLIC
262 void gavl_timer_set(gavl_timer_t * timer, gavl_time_t t);
263 
264 /*! \ingroup timer
265  * \brief Get the current time for benchmarking
266  * \param flags Flags returned by \ref gavl_accel_supported
267  * \returns The time in arbitrary units
268  *
269  * The returned value itself is meaningless since the
270  * timescale depends on the system. Use this only for relative comparisons
271  * for benchmarks. A textual description on how the values can be
272  * interpreted can be ontained with \ref gavl_benchmark_get_desc
273  */
274 
275 GAVL_PUBLIC
276 uint64_t gavl_benchmark_get_time(int flags);
277 
278 /*! \ingroup timer
279  * \brief Get a description about the value returned by
280  *        \ref gavl_benchmark_get_time
281  * \param flags Flags returned by \ref gavl_accel_supported
282  * \returns A string describing what the time value means
283  *
284  */
285 
286 GAVL_PUBLIC
287 const char * gavl_benchmark_get_desc(int flags);
288 
289 
290 #ifdef __cplusplus
291 }
292 #endif
293 
294 #endif /* GAVLTIME_H_INCLUDED */
295 
296