-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrack.cc
504 lines (441 loc) · 11.2 KB
/
Track.cc
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//
// Copyright 2018, Jeremy Cooper
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include <stdio.h>
#include "Track.h"
#include "ECC_C1.h"
#include "ECC_C2.h"
#include "ECCFill_C1.h"
#include "ECCFill_C2.h"
static bool BlockHeaderIsValid(const DATBlock& block);
Track::Track(Head head)
: mHead(head), mHaveLastBlock(false), mC1Errors(0), mC1UncorrectableErrors(0),
mC2UncorrectableErrors(0), mHaveControlID(false), mHaveDataID(false)
{
int i, j;
//
// Invalidate all parsed sub-codes.
//
for (i = 0; i < 16; i++)
mSubcodeIsValid[i] = false;
//
// Invalidate the sub-code signature.
//
for (i = 0; i < 7; i++)
mSubcodeSignature[i] = 0;
//
// Invalidate all blocks.
//
for (i = 0; i < kBlocks; i++) {
for (j = 0; j < kBlockSize; j++) {
mDataIsValid[i][j] = false;
mData[i][j] = 0;
}
mHeaderIsValid[i] = false;
}
}
Track::~Track()
{
}
Track::Head
Track::GetHead() const
{
return mHead;
}
void
Track::SetHead(Head head)
{
mHead = head;
}
void
Track::AddBlock(const DATBlock& block)
{
if (!BlockHeaderIsValid(block)) {
//
// Block doesn't have a valid header, but there might reason to
// accept it.
//
if (mHaveLastBlock && block.Size() == 36) {
if ((mLastBlockNumber >= 0 && mLastBlockNumber < 0x7f) ||
(mLastBlockNumber >= 0x88 && mLastBlockNumber < 0x8f) ||
(mLastBlockNumber >= 0x80 && mLastBlockNumber < 0x88)) {
//
// This block doesn't have a valid header but we've received blocks
// before it that are ok and we're still expecting blocks in this
// region. It is likely that this block simply has a bad header but
// its byte payload is just fine. Let's interpret this block as being
// the next block in the sequence.
//
AddGuessedBlock(mLastBlockNumber + 1, block);
mLastBlockNumber++;
}
}
return;
}
//
// Block header checks out. Add the block data to the location
// that it identifies itself as.
//
AddVerifiedBlock(block);
}
//
// Get the contents of the specific sub-code, if it was correctly
// received.
//
bool
Track::GetSubcode(int id, const uint8_t **out) const
{
if (id < 0 || id > 15 || !mSubcodeIsValid[id])
return false;
*out = &mSubcode[id][0];
return true;
}
//
// Get access to the entire data block and meta-data.
//
const Track::DataArray&
Track::Data() const
{
return mData;
}
const Track::ValidityArray&
Track::DataValid() const
{
return mDataIsValid;
}
const Track::HeaderArray&
Track::Headers() const
{
return mHeader;
}
const Track::HeaderValidityArray&
Track::HeaderValid() const
{
return mHeaderIsValid;
}
Track::DataArray&
Track::ModifiableData()
{
return mData;
}
Track::ValidityArray&
Track::ModifiableDataValid()
{
return mDataIsValid;
}
const Track::SubcodeSignatureArray&
Track::SubcodeSignature() const
{
return mSubcodeSignature;
}
//
// Track is supposedly complete.
// Correct errors and evaluate sub-codes.
//
void
Track::Complete()
{
ECC_C1 Vp;
//
// Iterate over each pair of blocks, correcting the C1 errors in each.
//
for (ECCFill_C1 c1_fill(*this); !c1_fill.End(); c1_fill.Next()) {
//
// Fill the error check vector.
//
Vp.Fill(c1_fill);
//
// Detect errors in this vector and correct them.
//
ECC_C1::Status ok = Vp.Correct();
switch (ok) {
case ECC_C1::NO_ERRORS:
//
// No errors. Nothing to be done.
//
break;
case ECC_C1::UNCORRECTABLE:
mC1UncorrectableErrors++;
case ECC_C1::CORRECTED:
//
// There were errors, they were either corrected or the whole vector
// has been marked bad. Put the corrected or invalidated data back into
// the track.
//
mC1Errors++;
Vp.Dump(c1_fill);
break;
}
}
//
// Now iterate over each block 4-group to perform C2 error correction.
//
ECC_C2 Vq;
for (ECCFill_C2 c2_fill(*this); !c2_fill.End(); c2_fill.Next()) {
//
// Fill the error check vector.
//
Vq.Fill(c2_fill);
//
// Detect errors in this vector and correct them.
//
ECC_C2::Status c2ok = Vq.Correct();
switch (c2ok) {
case ECC_C2::NO_ERRORS:
//
// No errors. Nothing to be done.
//
break;
case ECC_C2::CORRECTED:
//
// There were errors but they were corrected.
// Put the corrected data back into the track.
//
Vq.Dump(c2_fill);
break;
case ECC_C2::UNCORRECTABLE:
//
// Slice was uncorrectable. Leave it as is. The next level of
// error handling (interpolation for Audio, C3 for DDS) will have
// to deal with it.
//
mC2UncorrectableErrors++;
break;
}
}
//
// Error correction is complete. Now dather data from the sub-code blocks.
//
//
// Now all the sub-code blocks have had as much error correction applied
// as they ever will. Build the sub-code signature.
//
// Examine blocks 0x80-0x8f, the sub-code blocks, and build up a complete
// list of the seven sub-code identifiers used in this track and the order
// in which they are specified.
//
bool have_subcode_slot[7];
for (size_t i = 0; i < 7; i++)
have_subcode_slot[i] = false;
for (size_t i = 0; i < 16; i++) {
//
// Odd-numbered blocks have three sub-codes. Even blocks have four.
//
size_t limit = (i & 1) ? 3 : 4;
size_t block_number = 0x80 + i;
int slot_start = (i & 1) ? 4 : 0;
//
// Grab the ControlID and DataID from the first valid even-numbered
// Subcode block.
//
if ((block_number & 1) == 0) {
if (!mHaveControlID && mHeaderIsValid[block_number]) {
mControlID = (mHeader[block_number] & 0xf0) >> 4;
mDataID = (mHeader[block_number] & 0x0f);
mHaveControlID = true;
mHaveDataID = true;
}
}
//
// Examine the individal sub-codes in this block.
//
for (size_t j = 0; j < limit; j++) {
//
// Point straight at the 8-byte item.
//
const uint8_t *item = &(mData[block_number][8*j]);
const bool *validity = &(mDataIsValid[block_number][8*j]);
//
// What's the sub-code id?
//
if (!validity[0])
//
// Sub-code id byte isn't even valid. Don't bother.
//
continue;
uint8_t subcode_id = (item[0] & 0xf0) >> 4;
//
// Do we already have this sub-code?
//
if (mSubcodeIsValid[subcode_id] && have_subcode_slot[slot_start+j])
//
// Already have it. Skip it.
//
continue;
//
// Check the whole subcode item parity and make sure there
// are no erasure symbols here.
//
uint8_t parity;
bool valid;
size_t k;
for (k = 0, parity = 0, valid = true; k < 8; k++) {
parity ^= item[k] & 0xff;
valid = valid && validity[k];
}
if (!valid || parity != 0)
//
// Item parity or validity is bad.
//
continue;
if (!mSubcodeIsValid[subcode_id]) {
//
// Copy the sub-code into its slot and mark it valid.
//
for (k = 0; k < 7; k++)
mSubcode[subcode_id][k] = item[k];
mSubcodeIsValid[subcode_id] = true;
}
if (!have_subcode_slot[slot_start+j]) {
//
// Copy the sub-code identifier into the subcode signature.
//
mSubcodeSignature[slot_start+j] = subcode_id;
have_subcode_slot[slot_start+j] = true;
}
}
}
}
void
Track::AddVerifiedBlock(const DATBlock& block)
{
//
// What's this block's block number?
//
const uint16_t *bytes = block.FlaggedBytes();
uint8_t block_number;
if (bytes[2] & 0x80) {
//
// Sub-code block. Block number is 0x80-0x8f.
//
block_number = bytes[2] & 0x8f;
} else {
//
// Data block. Block number is 0x00-0x7f
//
block_number = bytes[2] & 0xff;
}
//
// Copy the header byte away.
//
mHeader[block_number] = bytes[1] & 0xff;
mHeaderIsValid[block_number] = true;
mHaveLastBlock = true;
mLastBlockNumber = block_number;
//
// Copy the data.
//
DataFill(block_number, block);
}
void
Track::AddGuessedBlock(uint8_t block_number, const DATBlock& block)
{
mHeaderIsValid[block_number] = false;
DataFill(block_number, block);
}
void
Track::DataFill(uint8_t block_number, const DATBlock& block)
{
//
// Copy the bytes into our large block array while also marking
// those bytes which are valid and invalid.
//
size_t count = block.Size();
if (count < 4)
return;
count -= 4;
if (count > 32)
count = 32;
const uint16_t *bytes = block.FlaggedBytes();
for (size_t i = 0; i < count; i++) {
mData[block_number][i] = bytes[i+4] & 0xff;
mDataIsValid[block_number][i] = (bytes[i+4] & 0x8000) == 0;
}
}
static bool
BlockHeaderIsValid(const DATBlock& block)
{
//
// (Blocks arrive with a SYNC byte prepended, hence the header starts
// at byte 1).
//
if (block.Size() < 4) {
//
// Block doesn't even have enough bytes for a header.
//
return false;
}
const uint16_t *blockBytes = block.FlaggedBytes();
//
// Check that the header bytes are valid.
//
if ((blockBytes[1] | blockBytes[2] | blockBytes[3]) & DATBlock::INVALID) {
//
// One of the header bytes didn't even decode properly.
//
return false;
}
//
// Check the parity of the block header.
//
uint8_t parity = (blockBytes[1] ^ blockBytes[2] ^ blockBytes[3]) & 0xff;
if (parity != 0)
return false;
return true;
}
bool
Track::GetControlID(uint8_t& control_id) const
{
if (!mHaveControlID)
return false;
control_id = mControlID;
return true;
}
bool
Track::GetDataID(uint8_t& data_id) const
{
if (!mHaveDataID)
return false;
data_id = mDataID;
return true;
}
size_t
Track::C1Errors() const
{
return mC1Errors;
}
size_t
Track::C1UncorrectableErrors() const
{
return mC1UncorrectableErrors;
}
size_t
Track::C2UncorrectableErrors() const
{
return mC2UncorrectableErrors;
}