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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/*
* 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 "PTSTracker.h"
#include "DVDCodecs/DVDCodecUtils.h"
#include "cores/VideoPlayer/Interface/TimingConstants.h"
#include "utils/StringUtils.h"
#include "utils/log.h"
#include <algorithm>
#include <cmath>
#define MAXERR DVD_MSEC_TO_TIME(2.5)
CPtsTracker::CPtsTracker()
{
ResetVFRDetection();
Flush();
}
void CPtsTracker::ResetVFRDetection(void)
{
m_minframeduration = DVD_NOPTS_VALUE;
m_maxframeduration = DVD_NOPTS_VALUE;
m_VFRCounter = 0;
m_patternCounter = 0;
m_lastPattern.clear();
}
void CPtsTracker::Flush()
{
m_pattern.clear();
m_ringpos = 0;
m_prevpts = DVD_NOPTS_VALUE;
m_ringfill = 0;
m_haspattern = false;
m_patternlength = 0;
m_frameduration = DVD_NOPTS_VALUE;
memset(m_diffring, 0, sizeof(m_diffring));
}
void CPtsTracker::Add(double pts)
{
//can't get a diff with just one pts
if (m_prevpts == DVD_NOPTS_VALUE)
{
m_prevpts = pts;
return;
}
//increase the ringbuffer position
m_ringpos = (m_ringpos + 1) % DIFFRINGSIZE;
//add the current diff to the ringbuffer
m_diffring[m_ringpos] = pts - m_prevpts;
//save the pts
m_prevpts = pts;
if (m_ringfill < DIFFRINGSIZE)
m_ringfill++;
//only search for patterns if we have full ringbuffer
if (m_ringfill < DIFFRINGSIZE)
return;
//get the current pattern in the ringbuffer
std::vector<double> pattern;
GetPattern(pattern);
//check if the pattern is the same as the saved pattern
//and if it is actually a pattern
if (!CheckPattern(pattern))
{
if (m_haspattern)
{
m_VFRCounter++;
m_lastPattern = m_pattern;
CLog::Log(LOGDEBUG, "CPtsTracker: pattern lost on diff {:f}, number of losses {}", GetDiff(0),
m_VFRCounter);
Flush();
}
//no pattern detected or current pattern broke/changed
//save detected pattern so we can check it with the next iteration
m_pattern = pattern;
return;
}
else
{
if (!m_haspattern)
{
m_haspattern = true;
m_patternlength = m_pattern.size();
if (!m_lastPattern.empty() && !CheckPattern(m_lastPattern))
{
m_patternCounter++;
}
double frameduration = CalcFrameDuration();
CLog::Log(LOGDEBUG, "CPtsTracker: detected pattern of length {}: {}, frameduration: {:f}",
(int)pattern.size(), GetPatternStr(), frameduration);
}
}
m_frameduration = CalcFrameDuration();
}
//gets a diff diffnr into the past
inline double CPtsTracker::GetDiff(int diffnr)
{
//m_ringpos is the last added diff, so if we want to go in the past we have to move back in the ringbuffer
int pos = m_ringpos - diffnr;
if (pos < 0)
pos += DIFFRINGSIZE;
return m_diffring[pos];
}
//calculate the current pattern in the ringbuffer
void CPtsTracker::GetPattern(std::vector<double>& pattern)
{
int difftypesbuff[DIFFRINGSIZE]; //difftypes of the diffs, difftypesbuff[0] is the last added diff,
//difftypesbuff[1] the one added before that etc
//get the difftypes
std::vector<double> difftypes;
for (int i = 0; i < m_ringfill; i++)
{
bool hasmatch = false;
for (unsigned int j = 0; j < difftypes.size(); j++)
{
if (MatchDiff(GetDiff(i), difftypes[j]))
{
hasmatch = true;
break;
}
}
//if we don't have a match with a saved difftype, we add it as a new one
if (!hasmatch)
difftypes.push_back(GetDiff(i));
}
//mark each diff with what difftype it is
for (int i = 0; i < m_ringfill; i++)
{
for (unsigned int j = 0; j < difftypes.size(); j++)
{
if (MatchDiff(GetDiff(i), difftypes[j]))
{
difftypesbuff[i] = j;
break;
}
}
}
bool checkexisting = !m_pattern.empty();
//we check for patterns to the length of DIFFRINGSIZE / 2
for (int i = 1; i <= m_ringfill / 2; i++)
{
//check the existing pattern length first
int length = checkexisting ? m_pattern.size() : i;
bool hasmatch = true;
for (int j = 1; j <= m_ringfill / length; j++)
{
int nrdiffs = length;
//we want to check the full buffer to see if the pattern repeats
//but we can't go beyond the buffer
if (j * length + length > m_ringfill)
nrdiffs = m_ringfill - j * length;
if (nrdiffs < 1) //if the buffersize can be cleanly divided by i we're done here
break;
if (!MatchDifftype(difftypesbuff, difftypesbuff + j * length, nrdiffs))
{
hasmatch = false;
break;
}
}
if (checkexisting)
{
checkexisting = false;
i--;
}
if (hasmatch)
{
for (int i = 0; i < length; i++)
{
double avgdiff = 0.0;
for (int j = 0; j < m_ringfill / length; j++)
avgdiff += GetDiff(j * length + i);
avgdiff /= m_ringfill / length;
pattern.push_back(avgdiff);
}
break;
}
}
std::sort(pattern.begin(), pattern.end());
}
inline bool CPtsTracker::MatchDiff(double diff1, double diff2)
{
return fabs(diff1 - diff2) < MAXERR;
}
//check if diffs1 is the same as diffs2
inline bool CPtsTracker::MatchDifftype(int diffs1[], int diffs2[], int nrdiffs)
{
for (int i = 0; i < nrdiffs; i++)
{
if (diffs1[i] != diffs2[i])
return false;
}
return true;
}
//check if our current detected pattern is the same as the one we saved
bool CPtsTracker::CheckPattern(std::vector<double>& pattern)
{
//if no pattern was detected or if the size of the patterns differ we don't have a match
if (pattern.empty() || pattern.size() != m_pattern.size())
return false;
if (pattern.size() == 1)
{
if (pattern[0] < MAXERR)
return false; //all diffs are too close to 0, can't use this
}
//check if the current pattern matches the saved pattern, with an offset of 1
for (unsigned int i = 0; i < m_pattern.size(); i++)
{
double diff = pattern[i];
if (!MatchDiff(diff, m_pattern[i]))
return false;
}
return true;
}
//calculate how long each frame should last from the saved pattern
//also retrieve information of max and min frame rate duration, for VFR files case
double CPtsTracker::CalcFrameDuration()
{
if (!m_pattern.empty())
{
//take the average of all diffs in the pattern
double frameduration;
double current, currentmin, currentmax;
currentmin = m_pattern[0];
currentmax = currentmin;
frameduration = currentmin;
for (unsigned int i = 1; i < m_pattern.size(); i++)
{
current = m_pattern[i];
if (current>currentmax)
currentmax = current;
if (current<currentmin)
currentmin = current;
frameduration += current;
}
frameduration /= m_pattern.size();
// Update min and max frame duration, only if data is valid
bool standard = false;
double tempduration = CDVDCodecUtils::NormalizeFrameduration(currentmin, &standard);
if (m_minframeduration == DVD_NOPTS_VALUE)
{
if (standard)
m_minframeduration = tempduration;
}
else
{
if (standard && (tempduration < m_minframeduration))
m_minframeduration = tempduration;
}
tempduration = CDVDCodecUtils::NormalizeFrameduration(currentmax, &standard);
if (m_maxframeduration == DVD_NOPTS_VALUE)
{
if (standard)
m_maxframeduration = tempduration;
}
else
{
if (standard && (tempduration > m_maxframeduration))
m_maxframeduration = tempduration;
}
//frameduration is not completely correct, use a common one if it's close
return CDVDCodecUtils::NormalizeFrameduration(frameduration);
}
return DVD_NOPTS_VALUE;
}
//looks pretty in the log
std::string CPtsTracker::GetPatternStr()
{
std::string patternstr;
for (unsigned int i = 0; i < m_pattern.size(); i++)
patternstr += StringUtils::Format("{:.2f} ", m_pattern[i]);
StringUtils::Trim(patternstr);
return patternstr;
}
|