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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "DVDClock.h"
#include "VideoReferenceClock.h"
#include "cores/VideoPlayer/Interface/TimingConstants.h"
#include "utils/MathUtils.h"
#include "utils/TimeUtils.h"
#include "utils/log.h"
#include <inttypes.h>
#include <math.h>
#include <mutex>
CDVDClock::CDVDClock()
{
std::unique_lock<CCriticalSection> lock(m_systemsection);
m_pauseClock = 0;
m_bReset = true;
m_paused = false;
m_speedAfterPause = DVD_PLAYSPEED_PAUSE;
m_iDisc = 0;
m_maxspeedadjust = 5.0;
m_systemAdjust = 0;
m_speedAdjust = 0;
m_startClock = 0;
m_vSyncAdjust = 0;
m_frameTime = DVD_TIME_BASE / 60.0;
m_videoRefClock.reset(new CVideoReferenceClock());
m_lastSystemTime = m_videoRefClock->GetTime();
m_systemOffset = m_videoRefClock->GetTime();
m_systemFrequency = CurrentHostFrequency();
m_systemUsed = m_systemFrequency;
}
CDVDClock::~CDVDClock() = default;
// Returns the current absolute clock in units of DVD_TIME_BASE (usually microseconds).
double CDVDClock::GetAbsoluteClock(bool interpolated /*= true*/)
{
std::unique_lock<CCriticalSection> lock(m_systemsection);
int64_t current;
current = m_videoRefClock->GetTime(interpolated);
return SystemToAbsolute(current);
}
double CDVDClock::GetClock(bool interpolated /*= true*/)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
int64_t current = m_videoRefClock->GetTime(interpolated);
m_systemAdjust += m_speedAdjust * (current - m_lastSystemTime);
m_lastSystemTime = current;
return SystemToPlaying(current);
}
double CDVDClock::GetClock(double& absolute, bool interpolated /*= true*/)
{
int64_t current = m_videoRefClock->GetTime(interpolated);
std::unique_lock<CCriticalSection> lock(m_systemsection);
absolute = SystemToAbsolute(current);
m_systemAdjust += m_speedAdjust * (current - m_lastSystemTime);
m_lastSystemTime = current;
return SystemToPlaying(current);
}
void CDVDClock::SetVsyncAdjust(double adjustment)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
m_vSyncAdjust = adjustment;
}
double CDVDClock::GetVsyncAdjust()
{
std::unique_lock<CCriticalSection> lock(m_critSection);
return m_vSyncAdjust;
}
void CDVDClock::Pause(bool pause)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (pause && !m_paused)
{
if (!m_pauseClock)
m_speedAfterPause = m_systemFrequency * DVD_PLAYSPEED_NORMAL / m_systemUsed;
else
m_speedAfterPause = DVD_PLAYSPEED_PAUSE;
SetSpeed(DVD_PLAYSPEED_PAUSE);
m_paused = true;
}
else if (!pause && m_paused)
{
m_paused = false;
SetSpeed(m_speedAfterPause);
}
}
void CDVDClock::Advance(double time)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
if (m_pauseClock)
{
m_pauseClock += time / DVD_TIME_BASE * m_systemFrequency;
}
}
void CDVDClock::SetSpeed(int iSpeed)
{
// this will sometimes be a little bit of due to rounding errors, ie clock might jump a bit when changing speed
std::unique_lock<CCriticalSection> lock(m_critSection);
if (m_paused)
{
m_speedAfterPause = iSpeed;
return;
}
if (iSpeed == DVD_PLAYSPEED_PAUSE)
{
if (!m_pauseClock)
m_pauseClock = m_videoRefClock->GetTime();
return;
}
int64_t current;
int64_t newfreq = m_systemFrequency * DVD_PLAYSPEED_NORMAL / iSpeed;
current = m_videoRefClock->GetTime();
if (m_pauseClock)
{
m_startClock += current - m_pauseClock;
m_pauseClock = 0;
}
m_startClock = current - (int64_t)((double)(current - m_startClock) * newfreq / m_systemUsed);
m_systemUsed = newfreq;
}
void CDVDClock::SetSpeedAdjust(double adjust)
{
CLog::Log(LOGDEBUG, "CDVDClock::SetSpeedAdjust - adjusted:{:f}", adjust);
std::unique_lock<CCriticalSection> lock(m_critSection);
m_speedAdjust = adjust;
}
double CDVDClock::GetSpeedAdjust()
{
std::unique_lock<CCriticalSection> lock(m_critSection);
return m_speedAdjust;
}
double CDVDClock::ErrorAdjust(double error, const char* log)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
double clock, absolute, adjustment;
clock = GetClock(absolute);
// skip minor updates while speed adjust is active
// -> adjusting buffer levels
if (m_speedAdjust != 0 && error < DVD_MSEC_TO_TIME(100))
{
return 0;
}
adjustment = error;
if (m_vSyncAdjust != 0)
{
// Audio ahead is more noticeable then audio behind video.
// Correct if aufio is more than 20ms ahead or more then
// 27ms behind. In a worst case scenario we switch from
// 20ms ahead to 21ms behind (for fps of 23.976)
if (error > 0.02 * DVD_TIME_BASE)
adjustment = m_frameTime;
else if (error < -0.027 * DVD_TIME_BASE)
adjustment = -m_frameTime;
else
adjustment = 0;
}
if (adjustment == 0)
return 0;
Discontinuity(clock+adjustment, absolute);
CLog::Log(LOGDEBUG, "CDVDClock::ErrorAdjust - {} - error:{:f}, adjusted:{:f}", log, error,
adjustment);
return adjustment;
}
void CDVDClock::Discontinuity(double clock, double absolute)
{
std::unique_lock<CCriticalSection> lock(m_critSection);
m_startClock = AbsoluteToSystem(absolute);
if(m_pauseClock)
m_pauseClock = m_startClock;
m_iDisc = clock;
m_bReset = false;
m_systemAdjust = 0;
m_speedAdjust = 0;
}
void CDVDClock::SetMaxSpeedAdjust(double speed)
{
std::unique_lock<CCriticalSection> lock(m_speedsection);
m_maxspeedadjust = speed;
}
//returns the refreshrate if the videoreferenceclock is running, -1 otherwise
int CDVDClock::UpdateFramerate(double fps, double* interval /*= NULL*/)
{
//sent with fps of 0 means we are not playing video
if(fps == 0.0)
return -1;
m_frameTime = 1/fps * DVD_TIME_BASE;
//check if the videoreferenceclock is running, will return -1 if not
double rate = m_videoRefClock->GetRefreshRate(interval);
if (rate <= 0)
return -1;
std::unique_lock<CCriticalSection> lock(m_speedsection);
double weight = (rate * 2) / fps;
//set the speed of the videoreferenceclock based on fps, refreshrate and maximum speed adjust set by user
if (m_maxspeedadjust > 0.05)
{
if (weight / MathUtils::round_int(weight) < 1.0 + m_maxspeedadjust / 100.0 &&
weight / MathUtils::round_int(weight) > 1.0 - m_maxspeedadjust / 100.0)
weight = MathUtils::round_int(weight);
}
double speed = (rate * 2.0 ) / (fps * weight);
lock.unlock();
m_videoRefClock->SetSpeed(speed);
return rate;
}
bool CDVDClock::GetClockInfo(int& MissedVblanks, double& ClockSpeed, double& RefreshRate) const
{
return m_videoRefClock->GetClockInfo(MissedVblanks, ClockSpeed, RefreshRate);
}
double CDVDClock::SystemToAbsolute(int64_t system)
{
return DVD_TIME_BASE * (double)(system - m_systemOffset) / m_systemFrequency;
}
int64_t CDVDClock::AbsoluteToSystem(double absolute)
{
return (int64_t)(absolute / DVD_TIME_BASE * m_systemFrequency) + m_systemOffset;
}
double CDVDClock::SystemToPlaying(int64_t system)
{
int64_t current;
if (m_bReset)
{
m_startClock = system;
m_systemUsed = m_systemFrequency;
if(m_pauseClock)
m_pauseClock = m_startClock;
m_iDisc = 0;
m_systemAdjust = 0;
m_speedAdjust = 0;
m_vSyncAdjust = 0;
m_bReset = false;
}
if (m_pauseClock)
current = m_pauseClock;
else
current = system;
return DVD_TIME_BASE * (double)(current - m_startClock + m_systemAdjust) / m_systemUsed + m_iDisc;
}
double CDVDClock::GetClockSpeed()
{
std::unique_lock<CCriticalSection> lock(m_critSection);
double speed = (double)m_systemFrequency / m_systemUsed;
return m_videoRefClock->GetSpeed() * speed + m_speedAdjust;
}
|