-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrack.cpp
More file actions
279 lines (251 loc) · 7.57 KB
/
Copy pathTrack.cpp
File metadata and controls
279 lines (251 loc) · 7.57 KB
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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Daniele Terdina
#include "MdvDecode.h"
#include "Track.h"
bool Track::StartPhaseLock(float maxDeviation, int minNumBytes)
{
const int period = (int)(bitPeriod + 0.5f);
const int minPeriod = (int)(bitPeriod * (1.0f - maxDeviation) + 0.5f);
const int maxPeriod = (int)(bitPeriod * (1.0f + maxDeviation) + 0.5f);
const int numBits = minNumBytes * 8;
if (track.size() < numBits)
return false;
vector<int> currentAlignment;
vector<int> bestAlignment;
#ifdef _DEBUG
const bool wantDebug = true;
#else
const bool wantDebug = debug;
#endif
__int64 bestError = INT64_MAX;
vector<bool> visitedStart(10);
size_t offset = index;
int offsetTime = (int)currentTime;
for (size_t k = 0; k < visitedStart.size(); k++)
{
if (visitedStart[k])
continue;
if (wantDebug)
{
currentAlignment.clear();
currentAlignment.push_back(0);
}
size_t startIndex = offset;
int time = offsetTime;
while (startIndex < offset + k)
time += track[startIndex++];
__int64 totError = 0;
int lastTime = time;
int startTime = time;
int n = 0;
size_t i = startIndex + 1;
while (i < track.size())
{
int newTime = time + track[i - 1];
int duration = newTime - lastTime;
if (abs(duration + track[i] - period) < abs(duration - period) || duration == 0)
{
// We can get closer to the target duration by moving ahead
i++;
time = newTime;
}
else if (duration >= minPeriod && duration <= maxPeriod)
{
// Closest duration looks sensible
n++;
lastTime = newTime;
if (i < visitedStart.size())
visitedStart[i] = true;
__int64 err = duration - period;
totError += err * err;
if (wantDebug)
{
currentAlignment.push_back(lastTime);
if (currentAlignment.size() > bestAlignment.size())
bestAlignment = currentAlignment;
}
if (n >= numBits)
{
if (totError < bestError)
{
bestError = totError;
index = startIndex;
currentTime = startTime;
bitPeriod = (float)(lastTime - startTime) / n;
}
break;
}
i++;
time = newTime;
}
else
{
// Incorrect duration; Try starting later
n = 0;
startTime += track[startIndex++];
lastTime = startTime;
if (wantDebug)
{
currentAlignment.clear();
totError = 0;
}
}
}
}
if (bestError != INT64_MAX)
{
if (debug)
{
char path[256];
snprintf(path, sizeof(path), "flux_%s_phaselock.jpg", debugTag);
DrawErrorNamed(track, bestAlignment, path);
}
return true;
}
#if 0
if (bestAlignment.size() < 100)
return false;
int leftAnchor = (bestAlignment[5] + bestAlignment[6]) / 2;
int rightAnchor = (bestAlignment[98] + bestAlignment[99]) / 2;
int anchorDiff = 98 - 5;
double duration = (double)(rightAnchor - leftAnchor) / anchorDiff;
bestAlignment.clear();
for (int k = -4; k < 100; k++)
bestAlignment.push_back((int)(((k + 0.5) * duration) + leftAnchor + 0.5));
#endif
if (debug)
{
char path[256];
snprintf(path, sizeof(path), "flux_%s_phaselock.jpg", debugTag);
DrawErrorNamed(track, bestAlignment, path);
}
#ifdef _DEBUG
// Kept for compatibility with existing Debug workflows
if (!debug)
DrawError(track, bestAlignment);
#endif
return false;
}
BYTE bit_reverse(BYTE b)
{
BYTE result = 0;
BYTE mask = 128;
while (b)
{
if (b & 1)
result |= mask;
b >>= 1;
mask >>= 1;
}
return result;
}
bool Track::FindPreamble(int numZeros, int numOnes, int maxSearchLen, vector<BYTE>& preamble, int* pStartTime)
{
numZeros <<= 3;
numOnes <<= 3;
maxSearchLen <<= 3;
preamble.clear();
float timeOrigin = currentTime;
vector<int> currentAlignment;
vector<int> bestAlignment;
#ifdef _DEBUG
const bool wantDebug = true;
#else
const bool wantDebug = debug;
#endif
BYTE b = 0;
int n = 0;
int numBitsRead = 0;
int bitStartTime[8];
bool found = false;
while (!found && !EndOfTrack())
{
while (!EndOfTrack())
{
if ((numBitsRead & 7) == 0 && numBitsRead > 0)
{
preamble.push_back(b);
b = 0;
}
if (wantDebug)
currentAlignment.push_back((int)(expectedTime + 0.5));
if (numBitsRead < 8)
bitStartTime[numBitsRead] = (int)(currentTime - timeOrigin + 0.5f);
numBitsRead++;
b <<= 1;
if (ReadBit())
{
b |= 1;
break;
}
else
{
n++;
}
}
if (n >= numZeros)
{
n = 1;
while (!EndOfTrack() && !found)
{
#ifdef _DEBUG
currentAlignment.push_back((int)(expectedTime + 0.5));
#endif
numBitsRead++;
b <<= 1;
if (!ReadBit())
{
break;
}
else
{
n++;
b |= 1;
found = n == numOnes && numBitsRead <= maxSearchLen;
}
if ((numBitsRead & 7) == 0)
{
preamble.push_back(b);
b = 0;
}
}
}
n = 1;
if (wantDebug && !found && numBitsRead < maxSearchLen)
{
if (currentAlignment.size() > bestAlignment.size())
bestAlignment = currentAlignment;
currentAlignment.clear();
}
}
if (debug)
{
// Prefer the actual successful alignment when the preamble was found;
// otherwise show the longest partial run so the failure is visible.
const vector<int>& toDraw = (found && !currentAlignment.empty()) ? currentAlignment : bestAlignment;
char path[256];
snprintf(path, sizeof(path), "flux_%s_preamble_%s.jpg",
debugTag, found ? "ok" : "fail");
DrawErrorNamed(track, toDraw, path);
}
#ifdef _DEBUG
if (!debug && !found)
DrawError(track, bestAlignment);
#endif
//preamble.clear();
//*pStartTime = 0;
int numPendingBits = numBitsRead & 7;
if (numPendingBits)
{
// Re-align using correct byte boundary
int complement = 8 - numPendingBits;
size_t last = preamble.size() - 1;
for (size_t i = 0; i < last; i++)
preamble[i] = (preamble[i] << numPendingBits) | (preamble[i + 1] >> complement);
preamble[last] = (preamble[last] << numPendingBits) | b;
}
*pStartTime = bitStartTime[numPendingBits];
for (size_t i = 0; i < preamble.size(); i++)
preamble[i] = bit_reverse(preamble[i]);
return found;
}