This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOPAtomTrackReader.cpp
357 lines (286 loc) · 10.4 KB
/
OPAtomTrackReader.cpp
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
/*
* Read a single OP-Atom file representing a clip track
*
* Copyright (C) 2009, British Broadcasting Corporation
* All Rights Reserved.
*
* Author: Philip de Nier
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * 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.
* * Neither the name of the British Broadcasting Corporation nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 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 OWNER 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <cstring>
#include <cstdio>
#include <libMXF++/MXF.h>
#include <libMXF++/extensions/TaggedValue.h>
#include <mxf/mxf_avid.h>
#include <mxf/mxf_macros.h>
#include "OPAtomTrackReader.h"
using namespace std;
using namespace mxfpp;
const char * const ERROR_STRINGS[] =
{
"success",
"general error",
"could not open for reading",
"no header partition found",
"file is not OP-Atom",
"error reading header metadata",
"no file source package found",
"essence data not found"
};
OPAtomOpenResult OPAtomTrackReader::Open(std::string filename, OPAtomTrackReader **track_reader)
{
File *file = 0;
try
{
// open the file
try
{
file = File::openRead(filename);
}
catch (...)
{
throw OP_ATOM_FILE_OPEN_READ_ERROR;
}
// read the header partition pack and check the operational pattern
if (!file->readHeaderPartition())
throw OP_ATOM_NO_HEADER_PARTITION;
Partition &header_partition = file->getPartition(0);
if (!mxf_is_op_atom(header_partition.getOperationalPattern()))
throw OP_ATOM_NOT_OP_ATOM;
*track_reader = new OPAtomTrackReader(filename, file);
return OP_ATOM_SUCCESS;
}
catch (const OPAtomOpenResult &ex)
{
delete file;
return ex;
}
catch (...)
{
delete file;
return OP_ATOM_FAIL;
}
}
string OPAtomTrackReader::ErrorToString(OPAtomOpenResult result)
{
size_t index = (size_t)(-1 * (int)result);
MXFPP_ASSERT(index < ARRAY_SIZE(ERROR_STRINGS));
return ERROR_STRINGS[index];
}
OPAtomTrackReader::OPAtomTrackReader(string filename, File *file)
{
mFilename = filename;
mTrackId = 0;
mDurationInMetadata = -1;
mIsPicture = true;
DataModel *data_model = 0;
AvidHeaderMetadata *header_metadata = 0;
FrameOffsetIndexTableSegment *index_table = 0;
try
{
int64_t essence_length = 0;
mxfUL essence_label = g_Null_UL;
mxfRational edit_rate = {0, 1};
FileDescriptor *file_descriptor = 0;
uint32_t frame_size = 0;
mxfKey key;
uint8_t llen;
uint64_t len;
Partition &header_partition = file->getPartition(0);
// get essence container label
vector<mxfUL> container_labels = header_partition.getEssenceContainers();
MXFPP_CHECK(container_labels.size() == 1);
essence_label = container_labels[0];
// read the header metadata
data_model = new DataModel();
header_metadata = new AvidHeaderMetadata(data_model);
TaggedValue::registerObjectFactory(header_metadata);
file->readNextNonFillerKL(&key, &llen, &len);
if (!mxf_is_header_metadata(&key))
throw OP_ATOM_FAIL;
header_metadata->read(file, &header_partition, &key, llen, len);
Preface *preface = header_metadata->getPreface();
ContentStorage *content = preface->getContentStorage();
vector<GenericPackage*> packages = content->getPackages();
// get the file source package and descriptor
SourcePackage *fsp = 0;
size_t i;
for (i = 0; i < packages.size(); i++) {
fsp = dynamic_cast<SourcePackage*>(packages[i]);
if (!fsp || !fsp->haveDescriptor())
continue;
file_descriptor = dynamic_cast<FileDescriptor*>(fsp->getDescriptor());
if (file_descriptor)
break;
}
if (!file_descriptor)
throw OP_ATOM_NO_FILE_PACKAGE;
// get the material track info
Track *mp_track = 0;
for (i = 0; i < packages.size(); i++) {
MaterialPackage *mp = dynamic_cast<MaterialPackage*>(packages[i]);
if (!mp)
continue;
vector<GenericTrack*> tracks = mp->getTracks();
size_t j;
for (j = 0; j < tracks.size(); j++) {
Track *track = dynamic_cast<Track*>(tracks[j]);
if (!track)
continue;
StructuralComponent *track_sequence = track->getSequence();
Sequence *sequence = dynamic_cast<Sequence*>(track_sequence);
SourceClip *source_clip = dynamic_cast<SourceClip*>(track_sequence);
if (sequence) {
vector<StructuralComponent*> components = sequence->getStructuralComponents();
if (components.size() != 1)
continue;
source_clip = dynamic_cast<SourceClip*>(components[0]);
}
if (!source_clip)
continue;
mxfUMID fsp_umid = fsp->getPackageUID();
mxfUMID sc_umid = source_clip->getSourcePackageID();
if (memcmp(&fsp_umid, &sc_umid, sizeof(fsp_umid)) == 0) {
mp_track = track;
edit_rate = mp_track->getEditRate();
mTrackId = mp_track->getTrackID();
mDurationInMetadata = source_clip->getDuration();
break;
}
}
if (mp_track)
break;
}
if (!mp_track)
throw OP_ATOM_HEADER_ERROR;
// read the index table if present and complete
int64_t file_pos = file->tell();
try
{
file->readNextNonFillerKL(&key, &llen, &len);
while (!IndexTableSegment::isIndexTableSegment(&key)) {
file->skip(len);
file->readNextNonFillerKL(&key, &llen, &len);
}
if (IndexTableSegment::isIndexTableSegment(&key)) {
index_table = FrameOffsetIndexTableSegment::read(file, len);
mxfRational index_edit_rate = index_table->getIndexEditRate();
if (memcmp(&index_edit_rate, &edit_rate, sizeof(index_edit_rate)) == 0)
frame_size = index_table->getEditUnitByteCount();
}
}
catch (...)
{
mxf_log_warn("Ignore errors - failed to find or read the index table segment\n");
// do nothing
}
file->seek(file_pos, SEEK_SET);
// position the file at the start of the essence data
try
{
file->readNextNonFillerKL(&key, &llen, &len);
while (!mxf_is_gc_essence_element(&key) && !mxf_avid_is_essence_element(&key)) {
file->skip(len);
file->readNextNonFillerKL(&key, &llen, &len);
}
}
catch (...)
{
throw OP_ATOM_ESSENCE_DATA_NOT_FOUND;
}
essence_length = len;
mEssenceParser = RawEssenceParser::Create(file, essence_length, essence_label, file_descriptor, edit_rate,
frame_size, index_table);
if (!mEssenceParser)
throw MXFException("Failed to create essence parser");
mDataModel = data_model;
mHeaderMetadata = header_metadata;
mIndexTable = index_table;
}
catch (...)
{
delete data_model;
delete header_metadata;
delete index_table;
throw;
}
}
OPAtomTrackReader::~OPAtomTrackReader()
{
delete mEssenceParser;
delete mHeaderMetadata;
delete mDataModel;
delete mIndexTable;
}
mxfUL OPAtomTrackReader::GetEssenceContainerLabel()
{
return mEssenceParser->GetEssenceContainerLabel();
}
int64_t OPAtomTrackReader::GetDuration()
{
int64_t essence_duration = mEssenceParser->GetDuration();
if (mDurationInMetadata > 0 && essence_duration > mDurationInMetadata)
return mDurationInMetadata;
else
return essence_duration;
}
int64_t OPAtomTrackReader::DetermineDuration()
{
int64_t essence_duration = mEssenceParser->DetermineDuration();
if (mDurationInMetadata > 0 && essence_duration > mDurationInMetadata)
return mDurationInMetadata;
else
return essence_duration;
}
int64_t OPAtomTrackReader::GetPosition()
{
return mEssenceParser->GetPosition();
}
bool OPAtomTrackReader::Read(OPAtomContentPackage *content)
{
OPAtomContentElement *element = content->GetEssenceData(mTrackId, false);
if (!element) {
element = content->AddElement(mTrackId);
element->mMaterialTrackId = mTrackId;
element->mIsPicture = mIsPicture;
}
int64_t essence_offset = mEssenceParser->GetEssenceOffset();
if (!mEssenceParser->Read(&element->mEssenceData, &element->mNumSamples))
return false;
element->mEssenceOffset = essence_offset;
return true;
}
bool OPAtomTrackReader::Seek(int64_t position)
{
return mEssenceParser->Seek(position);
}
bool OPAtomTrackReader::IsEOF()
{
return mEssenceParser->IsEOF();
}