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
|
/*
Copyright (c) 2018, Bjoern Stresing, Patrick Bichiou, Wolfgang Ruppel,
John Hurst
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.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
/*
This module implements MXF AS-02 IMF App #1 ACES. It's a set of file access objects that
offer simplified access to files conforming to the draft IMF App #1 ACES. The file
format, labeled IMF Essence Component (AKA "AS-02" for historical
reasons), is described in the following document:
o SMPTE 2067-5:2013 IMF Essence Component
The following use cases are supported by the module:
o Write essence to a plaintext or ciphertext AS-02 file:
ACES codestreams
o Read essence from a plaintext or ciphertext AS-02 file:
ACES codestreams
o Read header metadata from an AS-02 file
*/
#ifndef AS_02_ACES_h__
#define AS_02_ACES_h__
#include "AS_DCP.h"
#include "AS_02.h"
#include "Metadata.h"
#include <vector>
typedef ui16_t real16_t;
typedef float real32_t;
typedef double real64_t;
namespace AS_02
{
using Kumu::Result_t;
using Kumu::RESULT_FALSE;
using Kumu::RESULT_OK;
using Kumu::RESULT_FAIL;
using Kumu::RESULT_PTR;
using Kumu::RESULT_NULL_STR;
using Kumu::RESULT_ALLOC;
using Kumu::RESULT_PARAM;
using Kumu::RESULT_NOTIMPL;
using Kumu::RESULT_SMALLBUF;
using Kumu::RESULT_INIT;
using Kumu::RESULT_NOT_FOUND;
using Kumu::RESULT_NO_PERM;
using Kumu::RESULT_FILEOPEN;
using Kumu::RESULT_BADSEEK;
using Kumu::RESULT_READFAIL;
using Kumu::RESULT_WRITEFAIL;
using Kumu::RESULT_STATE;
using Kumu::RESULT_ENDOFFILE;
using Kumu::RESULT_CONFIG;
ASDCP::Rational ConvertToRational(double in);
namespace ACES
{
static const byte_t ACESPixelLayoutMonoscopicWOAlpha[ASDCP::MXF::RGBAValueLength] = {0x42, 0xfd, 0x47, 0xfd, 0x52, 0xfd, 0x00};
static const byte_t ACESPixelLayoutMonoscopicWAlpha[ASDCP::MXF::RGBAValueLength] = {0x41, 0xfd, 0x42, 0xfd, 0x47, 0xfd, 0x52, 0xfd, 0x00};
enum eAttributes
{
Invalid = 0,
AcesImageContainerFlag,
Channels,
Chromaticities,
Compression,
DataWindow,
DisplayWindow,
LineOrder,
PixelAspectRatio,
ScreenWindowCenter,
SreenWindowWidth,
Other
};
enum eTypes
{
Unknown_t = 0,
UnsignedChar_t,
Short_t,
UnsignedShort_t,
Int_t,
UnsignedInt_t,
UnsignedLong_t,
Half_t,
Float_t,
Double_t,
Box2i_t,
Chlist_t,
Chromaticities_t,
Compression_t,
LineOrder_t,
Keycode_t,
Rational_t,
String_t,
StringVector_t,
Timecode_t,
V2f_t,
V3f_t
};
struct channel
{
std::string name; // Shall be one of R, G, B, Left.R, Left.G, Left.B
i32_t pixelType;
ui32_t pLinear;
i32_t xSampling;
i32_t ySampling;
bool operator==(const channel &Other) const;
bool operator!=(const channel &Other) const { return !(*this == Other); }
};
struct box2i
{
i32_t xMin;
i32_t yMin;
i32_t xMax;
i32_t yMax;
bool operator==(const box2i &Other) const;
bool operator!=(const box2i &Other) const { return !(*this == Other); }
};
struct keycode
{
i32_t filmMfcCode; // 0 .. 99
i32_t filmType; // 0 .. 99
i32_t prefix; // 0 .. 999999
i32_t count; // 0 .. 9999
i32_t perfOffset; // 1 .. 119
i32_t perfsPerFrame; // 1 .. 15
i32_t perfsPerCount; // 20 .. 120
bool operator==(const keycode &Other) const;
bool operator!=(const keycode &Other) const { return !(*this == Other); }
};
struct v2f
{
real32_t x;
real32_t y;
bool operator==(const v2f &Other) const;
bool operator!=(const v2f &Other) const { return !(*this == Other); }
};
struct v3f
{
real32_t x;
real32_t y;
real32_t z;
bool operator==(const v3f &Other) const;
bool operator!=(const v3f &Other) const { return !(*this == Other); }
};
struct chromaticities
{
v2f red;
v2f green;
v2f blue;
v2f white;
bool operator==(const chromaticities &Other) const;
bool operator!=(const chromaticities &Other) const { return !(*this == Other); }
};
struct timecode
{
ui32_t timeAndFlags;
ui32_t userData;
bool operator==(const timecode &Other) const;
bool operator!=(const timecode &Other) const { return !(*this == Other); }
};
// Extract optional metadata with ACESDataAccessor functions.
struct generic
{
generic() : type(Unknown_t), size(0) {}
std::string attributeName;
eTypes type;
ui16_t size;
byte_t data[1024];
};
typedef std::vector<channel> chlist;
typedef std::vector<std::string> stringVector;
typedef std::vector<generic> other;
enum MIMEType_t { MT_PNG, MT_TIFF, MT_UNDEF };
const char*
MIME2str(AS_02::ACES::MIMEType_t m);
struct AncillaryResourceDescriptor
{
byte_t ResourceID[16];
MIMEType_t Type;
std::string filePath;
AncillaryResourceDescriptor() : Type(MT_UNDEF) {}
};
typedef std::list<AncillaryResourceDescriptor> ResourceList_t;
struct PictureDescriptor
{
ASDCP::Rational EditRate;
ui32_t ContainerDuration;
ASDCP::Rational SampleRate;
i32_t AcesImageContainerFlag;
chromaticities Chromaticities;
ui8_t Compression; // shall be 0
ui8_t LineOrder; // 0 increasing Y line order, 1 decreasing Y line order. Should be 0
box2i DataWindow;
box2i DisplayWindow;
real32_t PixelAspectRatio;
v2f ScreenWindowCenter;
real32_t ScreenWindowWidth;
chlist Channels; // vector
other Other; // vector
//ResourceList_t ResourceList;
// Doesn't compare Other.
bool operator==(const PictureDescriptor &Other) const;
// Doesn't compare Other.
bool operator!=(const PictureDescriptor &Other) const { return !(*this == Other); }
};
// Print debugging information to std::ostream
std::ostream& operator << (std::ostream &strm, const PictureDescriptor &PDesc);
// Print debugging information to stream (stderr default)
void PictureDescriptorDump(const PictureDescriptor &PDesc, FILE *stream = NULL);
// Convert a PictureDescriptor to MXF RGBA Picture Essence Descriptor.
// ACESVersion defaults to 0.0.0 Unknown
// OdtId defaults to 0 == None
// You may want to change ACESVersion and/or OdtId afterwards
Result_t ACES_PDesc_to_MD(const PictureDescriptor &PDesc,
const ASDCP::Dictionary &dict,
ASDCP::MXF::RGBAEssenceDescriptor &EssenceDescriptor);
const byte_t PNGMagic[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
const byte_t TIFFMagicLE[4] = { 0x49, 0x49, 0x2a, 0x00 };
const byte_t TIFFMagicBE[4] = { 0x4d, 0x4d, 0x00, 0x2a };
//
int const NS_ID_LENGTH = 16;
//
static byte_t s_ns_id_target_frame_prefix[NS_ID_LENGTH] =
{
// RFC 4122 type 5
// 2067-50 8.2.5.4 / RFC4122 Appendix C
//urn:uuid:bba41561-c505-4c9c-ab5a-71c68c2d70ea
0xbb, 0xa4, 0x15, 0x61, 0xc5, 0x05, 0x4c, 0x9c,
0xab, 0x5a, 0x71, 0xc6, 0x8c, 0x2d, 0x70, 0xea
};
static byte_t s_asset_id_prefix[NS_ID_LENGTH] =
{
// RFC 4122 type 5
// 2067-2:2016 7.3.1
0xaf, 0x86, 0xb7, 0xec, 0x4c, 0xdf, 0x4f, 0x9f,
0x82, 0x0f, 0x6f, 0xd8, 0xd3, 0x00, 0x30, 0x23
};
// Generate UUID asset ID values from target frame file contents
AS_02::Result_t CreateTargetFrameAssetId(Kumu::UUID& rID, const std::string& target_frame_file);
static Kumu::UUID create_4122_type5_id(const byte_t* subject_name, Kumu::fsize_t size, const byte_t* ns_id);
class FrameBuffer : public ASDCP::FrameBuffer
{
public:
FrameBuffer() {}
FrameBuffer(ui32_t size) { Capacity(size); }
virtual ~FrameBuffer() {}
// Print debugging information to stream (stderr default)
void Dump(FILE *stream = NULL, ui32_t dump_bytes = 0) const;
};
// Parses the data in the frame buffer to fill in the picture descriptor. Copies
// the offset of the image data into start_of_data. Returns error if the parser fails.
Result_t ParseMetadataIntoDesc(const FrameBuffer &FB, PictureDescriptor &PDesc, byte_t *start_of_data = NULL);
// An object which opens and reads a ACES codestream file. The file is expected
// to contain exactly one complete frame of picture essence as an unwrapped codestream.
class CodestreamParser
{
class h__CodestreamParser;
ASDCP::mem_ptr<h__CodestreamParser> m_Parser;
ASDCP_NO_COPY_CONSTRUCT(CodestreamParser);
public:
CodestreamParser();
virtual ~CodestreamParser();
// Opens a file for reading, parses enough data to provide a complete
// set of stream metadata for the MXFWriter below.
// The frame buffer's PlaintextOffset parameter will be set to the first
// byte of the data segment. Set this value to zero if you want
// encrypted headers.
Result_t OpenReadFrame(const std::string &filename, FrameBuffer &FB) const;
// Fill a PictureDescriptor struct with the values from the file's codestream.
// Returns RESULT_INIT if the file is not open.
Result_t FillPictureDescriptor(PictureDescriptor &PDesc) const;
};
class SequenceParser
{
class h__SequenceParser;
ASDCP::mem_ptr<h__SequenceParser> m_Parser;
ASDCP_NO_COPY_CONSTRUCT(SequenceParser);
public:
SequenceParser();
virtual ~SequenceParser();
// Opens a directory for reading. The directory is expected to contain one or
// more files, each containing the codestream for exactly one picture. The
// files must be named such that the frames are in temporal order when sorted
// alphabetically by filename. The parser will automatically parse enough data
// from the first file to provide a complete set of stream metadata for the
// MXFWriter below. If the "pedantic" parameter is given and is true, the
// parser will check the metadata for each codestream and fail if a
// mismatch is detected.
Result_t OpenRead(const std::string &directory, bool pedantic = false, const std::list<std::string> &target_frame_file_list = std::list<std::string>()) const;
// Opens a file sequence for reading. The sequence is expected to contain one or
// more filenames, each naming a file containing the codestream for exactly one
// picture. The parser will automatically parse enough data
// from the first file to provide a complete set of stream metadata for the
// MXFWriter below. If the "pedantic" parameter is given and is true, the
// parser will check the metadata for each codestream and fail if a
// mismatch is detected.
Result_t OpenRead(const std::list<std::string> &file_list, bool pedantic = false, const std::list<std::string> &target_frame_file_list = std::list<std::string>()) const;
// Fill a PictureDescriptor struct with the values from the first file's codestream.
// Returns RESULT_INIT if the directory is not open.
Result_t FillPictureDescriptor(PictureDescriptor &PDesc) const;
// Fill a ResourceList_t struct with the value from the sequence parser.
// Returns RESULT_INIT if empty.
Result_t FillResourceList(ResourceList_t &rResourceList_t) const;
// Rewind the directory to the beginning.
Result_t Reset() const;
// Reads the next sequential frame in the directory and places it in the
// frame buffer. Fails if the buffer is too small or the directory
// contains no more files.
// The frame buffer's PlaintextOffset parameter will be set to the first
// byte of the data segment. Set this value to zero if you want
// encrypted headers.
Result_t ReadFrame(FrameBuffer &FB) const;
Result_t ReadAncillaryResource(const std::string &filename, FrameBuffer &FB) const;
};
class MXFWriter
{
class h__Writer;
ASDCP::mem_ptr<h__Writer> m_Writer;
ASDCP_NO_COPY_CONSTRUCT(MXFWriter);
public:
MXFWriter();
virtual ~MXFWriter();
// Warning: direct manipulation of MXF structures can interfere
// with the normal operation of the wrapper. Caveat emptor!
virtual ASDCP::MXF::OP1aHeader& OP1aHeader();
virtual ASDCP::MXF::RIP& RIP();
// Open the file for writing. The file must not exist. Returns error if
// the operation cannot be completed or if nonsensical data is discovered
// in the essence descriptor.
Result_t OpenWrite(const std::string &filename, const ASDCP::WriterInfo &Info,
ASDCP::MXF::FileDescriptor *essence_descriptor,
ASDCP::MXF::InterchangeObject_list_t& essence_sub_descriptor_list,
const ASDCP::Rational &edit_rate,
const ResourceList_t &ancillary_resources = ResourceList_t(),
const ui32_t &header_size = 16384,
const AS_02::IndexStrategy_t &strategy = AS_02::IS_FOLLOW,
const ui32_t &partition_space = 10);
// Writes a frame of essence to the MXF file. If the optional AESEncContext
// argument is present, the essence is encrypted prior to writing.
// Fails if the file is not open, is finalized, or an operating system
// error occurs.
Result_t WriteFrame(const FrameBuffer &FrameBuf, ASDCP::AESEncContext *Ctx = NULL, ASDCP::HMACContext *HMAC = NULL);
// Writes an Ancillary Resource to the MXF file. If the optional AESEncContext
// argument is present, the essence is encrypted prior to writing.
// Fails if the file is not open, is finalized, or an operating system
// error occurs. RESULT_STATE will be returned if the method is called before
// WriteFrame()
Result_t WriteAncillaryResource(const AS_02::ACES::FrameBuffer &rBuf, ASDCP::AESEncContext* = 0, ASDCP::HMACContext* = 0);
// Closes the MXF file, writing the index and revised header.
Result_t Finalize();
};
class MXFReader
{
class h__Reader;
ASDCP::mem_ptr<h__Reader> m_Reader;
ASDCP_NO_COPY_CONSTRUCT(MXFReader);
public:
MXFReader(const Kumu::IFileReaderFactory& fileReaderFactory);
virtual ~MXFReader();
// Warning: direct manipulation of MXF structures can interfere
// with the normal operation of the wrapper. Caveat emptor!
virtual ASDCP::MXF::OP1aHeader& OP1aHeader();
virtual AS_02::MXF::AS02IndexReader& AS02IndexReader();
virtual ASDCP::MXF::RIP& RIP();
// Open the file for reading. The file must exist. Returns error if the
// operation cannot be completed.
Result_t OpenRead(const std::string &filename) const;
// Fill a ResourceList_t struct with the ancillary resources that are present in the file.
// Returns RESULT_INIT if the file is not open.
Result_t FillAncillaryResourceList(AS_02::ACES::ResourceList_t &ancillary_resources) const;
// Returns RESULT_INIT if the file is not open.
Result_t Close() const;
// Fill a WriterInfo struct with the values from the file's header.
// Returns RESULT_INIT if the file is not open.
Result_t FillWriterInfo(ASDCP::WriterInfo &Info) const;
// Reads a frame of essence from the MXF file. If the optional AESEncContext
// argument is present, the essence is decrypted after reading. If the MXF
// file is encrypted and the AESDecContext argument is NULL, the frame buffer
// will contain the ciphertext frame data. If the HMACContext argument is
// not NULL, the HMAC will be calculated (if the file supports it).
// Returns RESULT_INIT if the file is not open, failure if the frame number is
// out of range, or if optional decrypt or HAMC operations fail.
Result_t ReadFrame(ui32_t FrameNum, AS_02::ACES::FrameBuffer &FrameBuf, ASDCP::AESDecContext *Ctx = 0, ASDCP::HMACContext *HMAC = 0) const;
// Reads the ancillary resource having the given UUID from the MXF file. If the
// optional AESEncContext argument is present, the resource is decrypted after
// reading. If the MXF file is encrypted and the AESDecContext argument is NULL,
// the frame buffer will contain the ciphertext frame data. If the HMACContext
// argument is not NULL, the HMAC will be calculated (if the file supports it).
// Returns RESULT_INIT if the file is not open, failure if the frame number is
// out of range, or if optional decrypt or HAMC operations fail.
Result_t ReadAncillaryResource(const Kumu::UUID&, AS_02::ACES::FrameBuffer&, ASDCP::AESDecContext* = 0, ASDCP::HMACContext* = 0) const;
// Print debugging information to stream
void DumpHeaderMetadata(FILE* = 0) const;
void DumpIndex(FILE* = 0) const;
};
} // namespace ACES
} // namespace AS_02
#endif // AS_02_ACES_h__
|