2 Copyright (c) 2018, Bjoern Stresing, Patrick Bichiou, Wolfgang Ruppel,
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 3. The name of the author may not be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 This module implements MXF AS-02 IMF App #1 ACES. It's a set of file access objects that
31 offer simplified access to files conforming to the draft IMF App #1 ACES. The file
32 format, labeled IMF Essence Component (AKA "AS-02" for historical
33 reasons), is described in the following document:
35 o SMPTE 2067-5:2013 IMF Essence Component
37 The following use cases are supported by the module:
39 o Write essence to a plaintext or ciphertext AS-02 file:
42 o Read essence from a plaintext or ciphertext AS-02 file:
45 o Read header metadata from an AS-02 file
48 #ifndef AS_02_ACES_h__
49 #define AS_02_ACES_h__
57 typedef ui16_t real16_t;
58 typedef float real32_t;
59 typedef double real64_t;
65 using Kumu::RESULT_FALSE;
66 using Kumu::RESULT_OK;
67 using Kumu::RESULT_FAIL;
68 using Kumu::RESULT_PTR;
69 using Kumu::RESULT_NULL_STR;
70 using Kumu::RESULT_ALLOC;
71 using Kumu::RESULT_PARAM;
72 using Kumu::RESULT_NOTIMPL;
73 using Kumu::RESULT_SMALLBUF;
74 using Kumu::RESULT_INIT;
75 using Kumu::RESULT_NOT_FOUND;
76 using Kumu::RESULT_NO_PERM;
77 using Kumu::RESULT_FILEOPEN;
78 using Kumu::RESULT_BADSEEK;
79 using Kumu::RESULT_READFAIL;
80 using Kumu::RESULT_WRITEFAIL;
81 using Kumu::RESULT_STATE;
82 using Kumu::RESULT_ENDOFFILE;
83 using Kumu::RESULT_CONFIG;
85 ASDCP::Rational ConvertToRational(double in);
90 static const byte_t ACESPixelLayoutMonoscopicWOAlpha[ASDCP::MXF::RGBAValueLength] = {0x42, 0xfd, 0x47, 0xfd, 0x52, 0xfd, 0x00};
91 static const byte_t ACESPixelLayoutMonoscopicWAlpha[ASDCP::MXF::RGBAValueLength] = {0x41, 0xfd, 0x42, 0xfd, 0x47, 0xfd, 0x52, 0xfd, 0x00};
97 AcesImageContainerFlag,
139 std::string name; // Shall be one of R, G, B, Left.R, Left.G, Left.B
144 bool operator==(const channel &Other) const;
145 bool operator!=(const channel &Other) const { return !(*this == Other); }
154 bool operator==(const box2i &Other) const;
155 bool operator!=(const box2i &Other) const { return !(*this == Other); }
160 i32_t filmMfcCode; // 0 .. 99
161 i32_t filmType; // 0 .. 99
162 i32_t prefix; // 0 .. 999999
163 i32_t count; // 0 .. 9999
164 i32_t perfOffset; // 1 .. 119
165 i32_t perfsPerFrame; // 1 .. 15
166 i32_t perfsPerCount; // 20 .. 120
167 bool operator==(const keycode &Other) const;
168 bool operator!=(const keycode &Other) const { return !(*this == Other); }
175 bool operator==(const v2f &Other) const;
176 bool operator!=(const v2f &Other) const { return !(*this == Other); }
184 bool operator==(const v3f &Other) const;
185 bool operator!=(const v3f &Other) const { return !(*this == Other); }
188 struct chromaticities
194 bool operator==(const chromaticities &Other) const;
195 bool operator!=(const chromaticities &Other) const { return !(*this == Other); }
202 bool operator==(const timecode &Other) const;
203 bool operator!=(const timecode &Other) const { return !(*this == Other); }
206 // Extract optional metadata with ACESDataAccessor functions.
209 generic() : type(Unknown_t), size(0) {}
210 std::string attributeName;
216 typedef std::vector<channel> chlist;
217 typedef std::vector<std::string> stringVector;
218 typedef std::vector<generic> other;
220 enum MIMEType_t { MT_PNG, MT_TIFF, MT_UNDEF };
223 MIME2str(AS_02::ACES::MIMEType_t m);
225 struct AncillaryResourceDescriptor
227 byte_t ResourceID[16];
229 std::string filePath;
231 AncillaryResourceDescriptor() : Type(MT_UNDEF) {}
234 typedef std::list<AncillaryResourceDescriptor> ResourceList_t;
237 struct PictureDescriptor
239 ASDCP::Rational EditRate;
240 ui32_t ContainerDuration;
241 ASDCP::Rational SampleRate;
243 i32_t AcesImageContainerFlag;
244 chromaticities Chromaticities;
245 ui8_t Compression; // shall be 0
246 ui8_t LineOrder; // 0 increasing Y line order, 1 decreasing Y line order. Should be 0
249 real32_t PixelAspectRatio;
250 v2f ScreenWindowCenter;
251 real32_t ScreenWindowWidth;
252 chlist Channels; // vector
253 other Other; // vector
254 //ResourceList_t ResourceList;
256 // Doesn't compare Other.
257 bool operator==(const PictureDescriptor &Other) const;
258 // Doesn't compare Other.
259 bool operator!=(const PictureDescriptor &Other) const { return !(*this == Other); }
262 // Print debugging information to std::ostream
263 std::ostream& operator << (std::ostream &strm, const PictureDescriptor &PDesc);
264 // Print debugging information to stream (stderr default)
265 void PictureDescriptorDump(const PictureDescriptor &PDesc, FILE *stream = NULL);
266 // Convert a PictureDescriptor to MXF RGBA Picture Essence Descriptor.
267 // ACESVersion defaults to 0.0.0 Unknown
268 // OdtId defaults to 0 == None
269 // You may want to change ACESVersion and/or OdtId afterwards
270 Result_t ACES_PDesc_to_MD(const PictureDescriptor &PDesc,
271 const ASDCP::Dictionary &dict,
272 ASDCP::MXF::RGBAEssenceDescriptor &EssenceDescriptor);
274 const byte_t PNGMagic[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a };
275 const byte_t TIFFMagicLE[4] = { 0x49, 0x49, 0x2a, 0x00 };
276 const byte_t TIFFMagicBE[4] = { 0x4d, 0x4d, 0x00, 0x2a };
279 int const NS_ID_LENGTH = 16;
281 static byte_t s_ns_id_target_frame_prefix[NS_ID_LENGTH] =
284 // 2067-50 8.2.5.4 / RFC4122 Appendix C
285 //urn:uuid:bba41561-c505-4c9c-ab5a-71c68c2d70ea
286 0xbb, 0xa4, 0x15, 0x61, 0xc5, 0x05, 0x4c, 0x9c,
287 0xab, 0x5a, 0x71, 0xc6, 0x8c, 0x2d, 0x70, 0xea
289 static byte_t s_asset_id_prefix[NS_ID_LENGTH] =
293 0xaf, 0x86, 0xb7, 0xec, 0x4c, 0xdf, 0x4f, 0x9f,
294 0x82, 0x0f, 0x6f, 0xd8, 0xd3, 0x00, 0x30, 0x23
296 // Generate UUID asset ID values from target frame file contents
297 AS_02::Result_t CreateTargetFrameAssetId(Kumu::UUID& rID, const std::string& target_frame_file);
298 static Kumu::UUID create_4122_type5_id(const byte_t* subject_name, Kumu::fsize_t size, const byte_t* ns_id);
300 class FrameBuffer : public ASDCP::FrameBuffer
304 FrameBuffer(ui32_t size) { Capacity(size); }
305 virtual ~FrameBuffer() {}
307 // Print debugging information to stream (stderr default)
308 void Dump(FILE *stream = NULL, ui32_t dump_bytes = 0) const;
311 // Parses the data in the frame buffer to fill in the picture descriptor. Copies
312 // the offset of the image data into start_of_data. Returns error if the parser fails.
313 Result_t ParseMetadataIntoDesc(const FrameBuffer &FB, PictureDescriptor &PDesc, byte_t *start_of_data = NULL);
316 // An object which opens and reads a ACES codestream file. The file is expected
317 // to contain exactly one complete frame of picture essence as an unwrapped codestream.
318 class CodestreamParser
320 class h__CodestreamParser;
321 ASDCP::mem_ptr<h__CodestreamParser> m_Parser;
322 ASDCP_NO_COPY_CONSTRUCT(CodestreamParser);
326 virtual ~CodestreamParser();
328 // Opens a file for reading, parses enough data to provide a complete
329 // set of stream metadata for the MXFWriter below.
330 // The frame buffer's PlaintextOffset parameter will be set to the first
331 // byte of the data segment. Set this value to zero if you want
332 // encrypted headers.
333 Result_t OpenReadFrame(const std::string &filename, FrameBuffer &FB) const;
335 // Fill a PictureDescriptor struct with the values from the file's codestream.
336 // Returns RESULT_INIT if the file is not open.
337 Result_t FillPictureDescriptor(PictureDescriptor &PDesc) const;
343 class h__SequenceParser;
344 ASDCP::mem_ptr<h__SequenceParser> m_Parser;
345 ASDCP_NO_COPY_CONSTRUCT(SequenceParser);
349 virtual ~SequenceParser();
351 // Opens a directory for reading. The directory is expected to contain one or
352 // more files, each containing the codestream for exactly one picture. The
353 // files must be named such that the frames are in temporal order when sorted
354 // alphabetically by filename. The parser will automatically parse enough data
355 // from the first file to provide a complete set of stream metadata for the
356 // MXFWriter below. If the "pedantic" parameter is given and is true, the
357 // parser will check the metadata for each codestream and fail if a
358 // mismatch is detected.
359 Result_t OpenRead(const std::string &directory, bool pedantic = false, const std::list<std::string> &target_frame_file_list = std::list<std::string>()) const;
361 // Opens a file sequence for reading. The sequence is expected to contain one or
362 // more filenames, each naming a file containing the codestream for exactly one
363 // picture. The parser will automatically parse enough data
364 // from the first file to provide a complete set of stream metadata for the
365 // MXFWriter below. If the "pedantic" parameter is given and is true, the
366 // parser will check the metadata for each codestream and fail if a
367 // mismatch is detected.
368 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;
370 // Fill a PictureDescriptor struct with the values from the first file's codestream.
371 // Returns RESULT_INIT if the directory is not open.
372 Result_t FillPictureDescriptor(PictureDescriptor &PDesc) const;
374 // Fill a ResourceList_t struct with the value from the sequence parser.
375 // Returns RESULT_INIT if empty.
376 Result_t FillResourceList(ResourceList_t &rResourceList_t) const;
378 // Rewind the directory to the beginning.
379 Result_t Reset() const;
381 // Reads the next sequential frame in the directory and places it in the
382 // frame buffer. Fails if the buffer is too small or the directory
383 // contains no more files.
384 // The frame buffer's PlaintextOffset parameter will be set to the first
385 // byte of the data segment. Set this value to zero if you want
386 // encrypted headers.
387 Result_t ReadFrame(FrameBuffer &FB) const;
389 Result_t ReadAncillaryResource(const std::string &filename, FrameBuffer &FB) const;
396 ASDCP::mem_ptr<h__Writer> m_Writer;
397 ASDCP_NO_COPY_CONSTRUCT(MXFWriter);
401 virtual ~MXFWriter();
403 // Warning: direct manipulation of MXF structures can interfere
404 // with the normal operation of the wrapper. Caveat emptor!
405 virtual ASDCP::MXF::OP1aHeader& OP1aHeader();
406 virtual ASDCP::MXF::RIP& RIP();
408 // Open the file for writing. The file must not exist. Returns error if
409 // the operation cannot be completed or if nonsensical data is discovered
410 // in the essence descriptor.
411 Result_t OpenWrite(const std::string &filename, const ASDCP::WriterInfo &Info,
412 ASDCP::MXF::FileDescriptor *essence_descriptor,
413 ASDCP::MXF::InterchangeObject_list_t& essence_sub_descriptor_list,
414 const ASDCP::Rational &edit_rate,
415 const ResourceList_t &ancillary_resources = ResourceList_t(),
416 const ui32_t &header_size = 16384,
417 const AS_02::IndexStrategy_t &strategy = AS_02::IS_FOLLOW,
418 const ui32_t &partition_space = 10);
420 // Writes a frame of essence to the MXF file. If the optional AESEncContext
421 // argument is present, the essence is encrypted prior to writing.
422 // Fails if the file is not open, is finalized, or an operating system
424 Result_t WriteFrame(const FrameBuffer &FrameBuf, ASDCP::AESEncContext *Ctx = NULL, ASDCP::HMACContext *HMAC = NULL);
426 // Writes an Ancillary Resource to the MXF file. If the optional AESEncContext
427 // argument is present, the essence is encrypted prior to writing.
428 // Fails if the file is not open, is finalized, or an operating system
429 // error occurs. RESULT_STATE will be returned if the method is called before
431 Result_t WriteAncillaryResource(const AS_02::ACES::FrameBuffer &rBuf, ASDCP::AESEncContext* = 0, ASDCP::HMACContext* = 0);
433 // Closes the MXF file, writing the index and revised header.
441 ASDCP::mem_ptr<h__Reader> m_Reader;
442 ASDCP_NO_COPY_CONSTRUCT(MXFReader);
446 virtual ~MXFReader();
448 // Warning: direct manipulation of MXF structures can interfere
449 // with the normal operation of the wrapper. Caveat emptor!
450 virtual ASDCP::MXF::OP1aHeader& OP1aHeader();
451 virtual AS_02::MXF::AS02IndexReader& AS02IndexReader();
452 virtual ASDCP::MXF::RIP& RIP();
454 // Open the file for reading. The file must exist. Returns error if the
455 // operation cannot be completed.
456 Result_t OpenRead(const std::string &filename) const;
458 // Fill a ResourceList_t struct with the ancillary resources that are present in the file.
459 // Returns RESULT_INIT if the file is not open.
460 Result_t FillAncillaryResourceList(AS_02::ACES::ResourceList_t &ancillary_resources) const;
462 // Returns RESULT_INIT if the file is not open.
463 Result_t Close() const;
465 // Fill a WriterInfo struct with the values from the file's header.
466 // Returns RESULT_INIT if the file is not open.
467 Result_t FillWriterInfo(ASDCP::WriterInfo &Info) const;
469 // Reads a frame of essence from the MXF file. If the optional AESEncContext
470 // argument is present, the essence is decrypted after reading. If the MXF
471 // file is encrypted and the AESDecContext argument is NULL, the frame buffer
472 // will contain the ciphertext frame data. If the HMACContext argument is
473 // not NULL, the HMAC will be calculated (if the file supports it).
474 // Returns RESULT_INIT if the file is not open, failure if the frame number is
475 // out of range, or if optional decrypt or HAMC operations fail.
476 Result_t ReadFrame(ui32_t FrameNum, AS_02::ACES::FrameBuffer &FrameBuf, ASDCP::AESDecContext *Ctx = 0, ASDCP::HMACContext *HMAC = 0) const;
478 // Reads the ancillary resource having the given UUID from the MXF file. If the
479 // optional AESEncContext argument is present, the resource is decrypted after
480 // reading. If the MXF file is encrypted and the AESDecContext argument is NULL,
481 // the frame buffer will contain the ciphertext frame data. If the HMACContext
482 // argument is not NULL, the HMAC will be calculated (if the file supports it).
483 // Returns RESULT_INIT if the file is not open, failure if the frame number is
484 // out of range, or if optional decrypt or HAMC operations fail.
485 Result_t ReadAncillaryResource(const Kumu::UUID&, AS_02::ACES::FrameBuffer&, ASDCP::AESDecContext* = 0, ASDCP::HMACContext* = 0) const;
487 // Print debugging information to stream
488 void DumpHeaderMetadata(FILE* = 0) const;
489 void DumpIndex(FILE* = 0) const;
496 #endif // AS_02_ACES_h__