-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDSFrameReceiver.cc
339 lines (301 loc) · 8.71 KB
/
DDSFrameReceiver.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
//
// 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 <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "DDSFrameReceiver.h"
#include "DDSGroup1.h"
DDSFrameReceiver::DDSFrameReceiver()
: mOutputDirectory(NULL), mHaveGroup(false), mBasicGroup(NULL),
mCurrentSession(0), mDumpSession(0), mState(DATA)
{
}
DDSFrameReceiver::~DDSFrameReceiver()
{
delete mBasicGroup;
free(mOutputDirectory);
}
//
// Dump recovered data to the given directory.
//
void
DDSFrameReceiver::DumpToDirectory(const char *dirname)
{
free(mOutputDirectory);
mOutputDirectory = strdup(dirname);
}
void
DDSFrameReceiver::DumpSession(unsigned int session_number)
{
mDumpSession = session_number;
}
bool
DDSFrameReceiver::IsFrame(const Track& A, const Track& B)
{
//
// Are these two tracks a pair (a frame) or are they
// separate? We must check subcode 3, the absolute frame number.
//
const uint8_t *A_absframe;
const uint8_t *B_absframe;
bool A_good;
bool B_good;
//
// Examine their absolute frame numbers. (Subcode 3).
//
A_good = A.GetSubcode(3, &A_absframe);
B_good = B.GetSubcode(3, &B_absframe);
//
// These tracks pair if their absolute frame numbers match.
//
return (A_good && B_good && memcmp(A_absframe, B_absframe, 7) == 0);
}
void
DDSFrameReceiver::ReceiveFrame(const Track& a, const Track& b)
{
//
// Construct a new DDS Group 3 object to receive the track pair
//
DDSGroup3 frame;
//
// Combine the tracks into a frame and do all the necessary
// de-interleaving.
//
DDSGroup3::DecodeError result = frame.DecodeFrame(a, b);
//
// Dump information about the frame.
//
const char *area_name;
switch (frame.Area()) {
case DDSGroup3::DEVICE_AREA:
area_name = "DEVICE";
break;
case DDSGroup3::REFERENCE_AREA:
area_name = "REFERENCE";
break;
case DDSGroup3::SYSTEM_AREA:
area_name = "SYSTEM";
break;
case DDSGroup3::DATA_AREA:
area_name = "DATA";
break;
case DDSGroup3::EOD_AREA:
area_name = "END-OF-DATA";
break;
default:
area_name = "?";
break;
}
printf("\n");
if (result != DDSGroup3::DECODE_OK) {
printf("Group 3 decode: %s\n",
DDSGroup3::ErrorDescription(result)
);
}
printf("Area : %s\n", area_name);
printf("Absolute frame: %06d\n", frame.AbsoluteFrameID());
printf("Basic Group : %05d\n", frame.BasicGroupID());
printf("Sub frame : %02d", frame.LogicalFrameID());
if (frame.IsLastLogicalFrame())
printf(" (Last of group)");
if (frame.IsECC3Frame())
printf(" (ECC3)");
printf("\n");
printf("File : %04d\n", frame.Separator1Count());
printf("Record : 0x%08x\n", frame.RecordCount());
//
// Print out error statistics for the frame.
//
const DATFrame& underFrame = frame.Frame();
unsigned int c1_errors = underFrame.C1Errors();
unsigned int c1_uncorrectable = underFrame.C1UncorrectableErrors();
unsigned int c1_corrected = c1_errors - c1_uncorrectable;
unsigned int c2_uncorrectable = underFrame.C2UncorrectableErrors();
unsigned int c2_corrected = c1_uncorrectable - c2_uncorrectable;
printf("Errors C1/C2 : %d/%d",
c1_corrected,
c2_corrected
);
if (c2_uncorrectable > 0)
printf(" %d UNCORRECTED\n", c2_uncorrectable);
else
printf(" (all corrected)\n");
switch (mState) {
case DATA:
//
// In data state until we see an END-OF-DATA marker.
//
if (frame.Area() == DDSGroup3::EOD_AREA)
mState = EOT;
break;
case EOT:
//
// We're in EOT state until we see another area.
//
if (frame.Area() != DDSGroup3::EOD_AREA) {
//
// We're in a new tape session.
//
mCurrentSession++;
mState = DATA;
printf("------------------------ START OF SESSION %d\n", mCurrentSession);
}
}
//
// If we hit an End-of-File mark we need to stop; any data after this mark
// may have duplicate group identifiers and will corrupt any existing
// groups before it.
//
if (mOutputDirectory != NULL && mCurrentSession == mDumpSession) {
if (frame.Area() == DDSGroup3::EOD_AREA) {
if (mHaveGroup) {
DumpGroup();
}
} else if (frame.Area() == DDSGroup3::DATA_AREA) {
AddFrame(frame);
}
}
fflush(stdout);
}
void
DDSFrameReceiver::Stop()
{
if (mHaveGroup && mOutputDirectory != NULL)
DumpGroup();
}
void
DDSFrameReceiver::AddFrame(DDSGroup3& frame)
{
//
// Is this a continuation of the last group or a new group?
//
if (mHaveGroup && mGroupNumber != frame.BasicGroupID()) {
//
// We have a basic group but this frame is outside of it.
// Dump the current group.
//
DumpGroup();
}
//
// Is this the start of a new group?
//
if (!mHaveGroup) {
//
// Prepare a new group with this frame's group number.
// This may also entail loading anything we currently know about this
// group from a previous decoding process.
//
NewGroup(frame.BasicGroupID());
}
//
// De-whiten the data into a G1 group.
//
DDSGroup1 g1(frame);
//
// Copy it into the basic group in the appropriate spot.
//
mBasicGroup->AddSubFrame(g1);
//
// If this was the last frame of the group, dump the group.
//
if (frame.IsLastLogicalFrame())
DumpGroup();
}
void
DDSFrameReceiver::NewGroup(uint32_t group_id)
{
//
// Construct a new group object.
//
mBasicGroup = new BasicGroup(group_id);
//
// Figure out what file name we would have previously stored this
// group's data under, if available.
//
char *groupFilename, *validName, *eccName, *eccValidName;
groupFilename = validName = eccName = eccValidName = NULL;
GenerateGroupFilenames(group_id, groupFilename, validName, eccName,
eccValidName);
//
// Load any previous data for this group.
//
mBasicGroup->LoadFromFile(groupFilename, validName, eccName, eccValidName);
free(groupFilename);
free(validName);
free(eccName);
free(eccValidName);
//
// Mark that we now have a group.
//
mHaveGroup = true;
mGroupNumber = group_id;
}
void
DDSFrameReceiver::DumpGroup()
{
//
// Perform ECC3 correction on the group. It's likely complete now.
//
bool correct = mBasicGroup->Correct();
uint32_t group_id = mBasicGroup->BasicGroupID();
printf("Group ECC3 : %s (Group %d)\n", correct ? "GOOD" : "----BAD---",
group_id);
printf("------------------------------------------------------------\n");
//
// Figure out what file name we should store this group's data under.
//
char *groupFilename, *validName, *eccName, *eccValidName;
groupFilename = validName = eccName = eccValidName = NULL;
GenerateGroupFilenames(group_id, groupFilename, validName, eccName,
eccValidName);
//
// Dump the group data to disk.
//
mBasicGroup->DumpToFile(groupFilename, validName, eccName, eccValidName);
free(groupFilename);
free(validName);
free(eccName);
free(eccValidName);
//
// Mark that we now have no group.
//
mHaveGroup = false;
delete mBasicGroup;
mBasicGroup = NULL;
}
void
DDSFrameReceiver::GenerateGroupFilenames(uint32_t group_id,
char*& groupFilename, char*& validName, char*& eccName, char*& eccValidName)
{
asprintf(&groupFilename, "%s/g%06d.bin", mOutputDirectory, group_id);
asprintf(&validName, "%s/g%06d.val", mOutputDirectory, group_id);
asprintf(&eccName, "%s/g%06d.ecc.bin", mOutputDirectory, group_id);
asprintf(&eccValidName, "%s/g%06d.ecc.val", mOutputDirectory, group_id);
}