1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "sci/include/sfx_time.h"
#include "sci/include/resource.h"
namespace Sci {
sfx_timestamp_t sfx_new_timestamp(long secs, long usecs, int frame_rate) {
sfx_timestamp_t r;
r.secs = secs;
r.usecs = usecs;
r.frame_rate = frame_rate;
r.frame_offset = 0;
return r;
}
sfx_timestamp_t sfx_timestamp_add(sfx_timestamp_t timestamp, int frames) {
timestamp.frame_offset += frames;
if (timestamp.frame_offset < 0) {
int secsub = 1 + (-timestamp.frame_offset / timestamp.frame_rate);
timestamp.frame_offset += timestamp.frame_rate * secsub;
timestamp.secs -= secsub;
}
timestamp.secs += (timestamp.frame_offset / timestamp.frame_rate);
timestamp.frame_offset %= timestamp.frame_rate;
return timestamp;
}
int sfx_timestamp_frame_diff(sfx_timestamp_t a, sfx_timestamp_t b) {
long usecdelta = 0;
if (a.frame_rate != b.frame_rate) {
fprintf(stderr, "Fatal: The semantics of subtracting two timestamps with a different base from each other is not defined!\n");
BREAKPOINT();
}
if (a.usecs != b.usecs) {
#if (SIZEOF_LONG >= 8)
usecdelta = (a.usecs * a.frame_rate) / 1000000
- (b.usecs * b.frame_rate) / 1000000;
#else
usecdelta = ((a.usecs / 1000) * a.frame_rate) / 1000
- ((b.usecs / 1000) * b.frame_rate) / 1000;
#endif
}
return usecdelta
+ (a.secs - b.secs) * a.frame_rate
+ a.frame_offset - b.frame_offset;
}
long sfx_timestamp_usecs_diff(sfx_timestamp_t t1, sfx_timestamp_t t2) {
long secs1, secs2;
long usecs1, usecs2;
sfx_timestamp_gettime(&t1, &secs1, &usecs1);
sfx_timestamp_gettime(&t2, &secs2, &usecs2);
return (usecs1 - usecs2) + ((secs1 - secs2) * 1000000);
}
sfx_timestamp_t sfx_timestamp_renormalise(sfx_timestamp_t timestamp, int new_freq) {
sfx_timestamp_t r;
sfx_timestamp_gettime(×tamp, &r.secs, &r.usecs);
r.frame_rate = new_freq;
r.frame_offset = 0;
return r;
}
void sfx_timestamp_gettime(sfx_timestamp_t *timestamp, long *secs, long *usecs) {
long ust = timestamp->usecs;
/* On 64 bit machines, we can do an accurate computation */
#if (SIZEOF_LONG >= 8)
ust += (timestamp->frame_offset * 1000000l) / (timestamp->frame_rate);
#else
ust += (timestamp->frame_offset * 1000l) / (timestamp->frame_rate / 1000l);
#endif
if (ust > 1000000) {
ust -= 1000000;
*secs = timestamp->secs + 1;
} else
*secs = timestamp->secs;
*usecs = ust;
}
} // End of namespace Sci
|