/*
-Copyright (c) 2004-2006, John Hurst
+Copyright (c) 2004-2012, John Hurst
All rights reserved.
Redistribution and use in source and binary forms, with or without
\brief AS-DCP library, non-public common elements
*/
-#ifndef _AS_DCP_INTERNAL_H__
-#define _AS_DCP_INTERNAL_H__
+#ifndef _AS_DCP_INTERNAL_H_
+#define _AS_DCP_INTERNAL_H_
-#include "AS_DCP_system.h"
+#include <KM_platform.h>
+#include <KM_util.h>
+#include <KM_log.h>
#include "Metadata.h"
-#include "hex_utils.h"
-using namespace std;
+using Kumu::DefaultLogSink;
+// using namespace std;
using namespace ASDCP;
using namespace ASDCP::MXF;
+#ifdef DEFAULT_MD_DECL
+ASDCP::MXF::OPAtomHeader *g_OPAtomHeader;
+ASDCP::MXF::OPAtomIndexFooter *g_OPAtomIndexFooter;
+#else
+extern MXF::OPAtomHeader *g_OPAtomHeader;
+extern MXF::OPAtomIndexFooter *g_OPAtomIndexFooter;
+#endif
namespace ASDCP
{
- // constant values used to calculate KLV and EKLV packet sizes
- static const ui32_t klv_key_size = 16;
- static const ui32_t klv_length_size = 4;
+ void default_md_object_init();
+
+ //
+ static std::vector<int>
+ version_split(const char* str)
+ {
+ std::vector<int> result;
+ const char* pstr = str;
+ const char* r = strchr(pstr, '.');
+
+ while ( r != 0 )
+ {
+ assert(r >= pstr);
+ if ( r > pstr )
+ result.push_back(atoi(pstr));
+
+ pstr = r + 1;
+ r = strchr(pstr, '.');
+ }
+
+ if( strlen(pstr) > 0 )
+ result.push_back(atoi(pstr));
+
+ assert(result.size() == 3);
+ return result;
+ }
+ // constant values used to calculate KLV and EKLV packet sizes
static const ui32_t klv_cryptinfo_size =
- klv_length_size
+ MXF_BER_LENGTH
+ UUIDlen /* ContextID */
- + klv_length_size
+ + MXF_BER_LENGTH
+ sizeof(ui64_t) /* PlaintextOffset */
- + klv_length_size
- + klv_key_size /* SourceKey */
- + klv_length_size
+ + MXF_BER_LENGTH
+ + SMPTE_UL_LENGTH /* SourceKey */
+ + MXF_BER_LENGTH
+ sizeof(ui64_t) /* SourceLength */
- + klv_length_size /* ESV length */ ;
+ + MXF_BER_LENGTH /* ESV length */ ;
static const ui32_t klv_intpack_size =
- klv_length_size
+ MXF_BER_LENGTH
+ UUIDlen /* TrackFileID */
- + klv_length_size
+ + MXF_BER_LENGTH
+ sizeof(ui64_t) /* SequenceNumber */
- + klv_length_size
+ + MXF_BER_LENGTH
+ 20; /* HMAC length*/
- // why this value? i dunno. it was peeled from mxflib.
- static const ui32_t HeaderPadding = 16384;
-
- const byte_t GCMulti_Data[16] =
- { 0x06, 0x0E, 0x2B, 0x34, 0x04, 0x01, 0x01, 0x03,
- 0x0d, 0x01, 0x03, 0x01, 0x02, 0x7F, 0x01, 0x00 };
-
- static const byte_t CipherAlgorithm_AES[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x07,
- 0x02, 0x09, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00 };
-
- static const byte_t MICAlgorithm_NONE[klv_key_size] = {0};
- static const byte_t MICAlgorithm_HMAC_SHA1[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x07,
- 0x02, 0x09, 0x02, 0x02, 0x01, 0x00, 0x00, 0x00 };
-
-#ifdef SMPTE_LABELS
- static byte_t OPAtom_Data[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x02,
- 0x0d, 0x01, 0x02, 0x01, 0x10, 0x00, 0x00, 0x00 };
- static UL OPAtomUL(OPAtom_Data);
-#else
- static byte_t OPAtom_Data[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x01,
- 0x0d, 0x01, 0x02, 0x01, 0x10, 0x00, 0x00, 0x00 };
- static UL OPAtomUL(OPAtom_Data);
-#endif
-
- static const byte_t OP1a_Data[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x01,
- 0x0d, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x00 };
- static UL OP1aUL(OP1a_Data);
-
- // Essence element labels
- static const byte_t WAVEssenceUL_Data[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x02, 0x01, 0x01,
- 0x0d, 0x01, 0x03, 0x01, 0x16, 0x01, 0x01, 0x00 };
-
- static const byte_t MPEGEssenceUL_Data[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x02, 0x01, 0x01,
- 0x0d, 0x01, 0x03, 0x01, 0x15, 0x01, 0x05, 0x00 };
-
- static const byte_t JP2KEssenceUL_Data[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x01, 0x02, 0x01, 0x01,
- 0x0d, 0x01, 0x03, 0x01, 0x15, 0x01, 0x08, 0x01 };
-
-#ifdef SMPTE_LABELS
- static const byte_t CryptEssenceUL_Data[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x04, 0x01, 0x01,
- 0x0d, 0x01, 0x03, 0x01, 0x02, 0x7e, 0x01, 0x00 };
-#else
- static const byte_t CryptEssenceUL_Data[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x02, 0x04, 0x01, 0x07,
- 0x0d, 0x01, 0x03, 0x01, 0x02, 0x7e, 0x01, 0x00 };
-#endif
-
- // Essence Container Labels
- static const byte_t WrappingUL_Data_PCM_24b_48k[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x01,
- 0x0d, 0x01, 0x03, 0x01, 0x02, 0x06, 0x01, 0x00 };
-
- static const byte_t WrappingUL_Data_MPEG2_VES[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x02,
- 0x0d, 0x01, 0x03, 0x01, 0x02, 0x04, 0x60, 0x01 };
-
- static const byte_t WrappingUL_Data_JPEG_2000[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x07,
- 0x0d, 0x01, 0x03, 0x01, 0x02, 0x0c, 0x01, 0x00 };
-
- static const byte_t WrappingUL_Data_Crypt[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x07,
- 0x0d, 0x01, 0x03, 0x01, 0x02, 0x0b, 0x01, 0x00 };
-
-
- // the label for the Cryptographic Framework DM scheme
- static const byte_t CryptoFrameworkUL_Data[klv_key_size] =
- { 0x06, 0x0e, 0x2b, 0x34, 0x04, 0x01, 0x01, 0x07,
- 0x0d, 0x01, 0x04, 0x01, 0x02, 0x01, 0x01, 0x00 };
+ // calculate size of encrypted essence with IV, CheckValue, and padding
+ inline ui32_t
+ calc_esv_length(ui32_t source_length, ui32_t plaintext_offset)
+ {
+ ui32_t ct_size = source_length - plaintext_offset;
+ ui32_t diff = ct_size % CBC_BLOCK_SIZE;
+ ui32_t block_size = ct_size - diff;
+ return plaintext_offset + block_size + (CBC_BLOCK_SIZE * 3);
+ }
// the check value for EKLV packets
// CHUKCHUKCHUKCHUK
{ 0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b,
0x43, 0x48, 0x55, 0x4b, 0x43, 0x48, 0x55, 0x4b };
- // labels used for FilePackages
- static std::string MPEG_PACKAGE_LABEL = "File Package: SMPTE 381M frame wrapping of MPEG2 video elementary stream";
- static std::string JP2K_PACKAGE_LABEL = "File Package: SMPTE XXXM frame wrapping of JPEG 2000 codestreams";
- static std::string PCM_PACKAGE_LABEL = "File Package: SMPTE 382M frame wrapping of wave audio";
-
- // GetMDObjectByPath() allows searching for metadata object by pathname
- // This character separates the path elements.
- static const char OBJECT_PATH_SEPARATOR = '.';
-
//------------------------------------------------------------------------------------------
//
- Result_t MD_to_MPEG2_VDesc(MXF::MPEG2VideoDescriptor*, MPEG2::VideoDescriptor&);
- Result_t MD_to_JP2K_PDesc(MXF::RGBAEssenceDescriptor*, JP2K::PictureDescriptor&);
- Result_t MD_to_PCM_ADesc(MXF::WaveAudioDescriptor*, PCM::AudioDescriptor&);
Result_t MD_to_WriterInfo(MXF::Identification*, WriterInfo&);
- Result_t MD_to_CryptoInfo(MXF::CryptographicContext*, WriterInfo&);
- Result_t MPEG2_VDesc_to_MD(MPEG2::VideoDescriptor&, MXF::MPEG2VideoDescriptor*);
- Result_t JP2K_PDesc_to_MD(JP2K::PictureDescriptor&, MXF::RGBAEssenceDescriptor*);
- Result_t PCM_ADesc_to_MD(PCM::AudioDescriptor&, MXF::WaveAudioDescriptor*);
- // void AddDMScrypt(PackagePtr, WriterInfo&, const byte_t*);
+ Result_t MD_to_CryptoInfo(MXF::CryptographicContext*, WriterInfo&, const Dictionary&);
Result_t EncryptFrameBuffer(const ASDCP::FrameBuffer&, ASDCP::FrameBuffer&, AESEncContext*);
Result_t DecryptFrameBuffer(const ASDCP::FrameBuffer&, ASDCP::FrameBuffer&, AESDecContext*);
-
- // calculate size of encrypted essence with IV, CheckValue, and padding
- inline ui32_t
- calc_esv_length(ui32_t source_length, ui32_t plaintext_offset)
+ Result_t PCM_ADesc_to_MD(PCM::AudioDescriptor& ADesc, ASDCP::MXF::WaveAudioDescriptor* ADescObj);
+ Result_t MD_to_PCM_ADesc(ASDCP::MXF::WaveAudioDescriptor* ADescObj, PCM::AudioDescriptor& ADesc);
+ void AddDMScrypt(Partition& HeaderPart, SourcePackage& Package,
+ WriterInfo& Descr, const UL& WrappingUL, const Dictionary*& Dict);
+
+ Result_t Read_EKLV_Packet(Kumu::FileReader& File, const ASDCP::Dictionary& Dict, const MXF::OPAtomHeader& HeaderPart,
+ const ASDCP::WriterInfo& Info, Kumu::fpos_t& LastPosition, ASDCP::FrameBuffer& CtFrameBuf,
+ ui32_t FrameNum, ui32_t SequenceNum, ASDCP::FrameBuffer& FrameBuf,
+ const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC);
+
+ //
+ class KLReader : public ASDCP::KLVPacket
{
- ui32_t ct_size = source_length - plaintext_offset;
- ui32_t diff = ct_size % CBC_BLOCK_SIZE;
- ui32_t block_size = ct_size - diff;
- return plaintext_offset + block_size + (CBC_BLOCK_SIZE * 3);
- }
+ ASDCP_NO_COPY_CONSTRUCT(KLReader);
+ byte_t m_KeyBuf[SMPTE_UL_LENGTH*2];
+
+ public:
+ KLReader() {}
+ ~KLReader() {}
+
+ inline const byte_t* Key() { return m_KeyBuf; }
+ inline const ui64_t Length() { return m_ValueLength; }
+ inline const ui64_t KLLength() { return m_KLLength; }
+
+ Result_t ReadKLFromFile(Kumu::FileReader& Reader);
+ };
+
+ namespace MXF
+ {
+ //---------------------------------------------------------------------------------
+ //
+
+ /// void default_md_object_init();
+
+ template <class HeaderType, class FooterType>
+ class TrackFileReader
+ {
+ KM_NO_COPY_CONSTRUCT(TrackFileReader);
+ TrackFileReader();
+
+ public:
+ const Dictionary* m_Dict;
+ Kumu::FileReader m_File;
+ HeaderType m_HeaderPart;
+ FooterType m_FooterPart;
+ WriterInfo m_Info;
+ ASDCP::FrameBuffer m_CtFrameBuf;
+ Kumu::fpos_t m_LastPosition;
+
+ TrackFileReader(const Dictionary& d) :
+ m_HeaderPart(m_Dict), m_FooterPart(m_Dict), m_Dict(&d)
+ {
+ default_md_object_init();
+ }
+
+ virtual ~TrackFileReader() {
+ Close();
+ }
+
+ Result_t InitInfo()
+ {
+ assert(m_Dict);
+ InterchangeObject* Object;
+
+ // Identification
+ Result_t result = m_HeaderPart.GetMDObjectByType(OBJ_TYPE_ARGS(Identification), &Object);
+
+ // Writer Info and SourcePackage
+ if ( KM_SUCCESS(result) )
+ {
+ MD_to_WriterInfo((Identification*)Object, m_Info);
+ result = m_HeaderPart.GetMDObjectByType(OBJ_TYPE_ARGS(SourcePackage), &Object);
+ }
+
+ if ( KM_SUCCESS(result) )
+ {
+ SourcePackage* SP = (SourcePackage*)Object;
+ memcpy(m_Info.AssetUUID, SP->PackageUID.Value() + 16, UUIDlen);
+ }
+
+ // optional CryptographicContext
+ if ( KM_SUCCESS(result) )
+ {
+ Result_t cr_result = m_HeaderPart.GetMDObjectByType(OBJ_TYPE_ARGS(CryptographicContext), &Object);
+
+ if ( KM_SUCCESS(cr_result) )
+ MD_to_CryptoInfo((CryptographicContext*)Object, m_Info, *m_Dict);
+ }
+
+ return result;
+ }
+
+ //
+ Result_t OpenMXFRead(const char* filename)
+ {
+ m_LastPosition = 0;
+ Result_t result = m_File.OpenRead(filename);
+
+ if ( KM_SUCCESS(result) )
+ result = m_HeaderPart.InitFromFile(m_File);
+
+ return result;
+ }
+
+ // positions file before reading
+ Result_t ReadEKLVFrame(const ASDCP::MXF::Partition& CurrentPartition,
+ ui32_t FrameNum, ASDCP::FrameBuffer& FrameBuf,
+ const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC)
+ {
+ // look up frame index node
+ IndexTableSegment::IndexEntry TmpEntry;
+
+ if ( KM_FAILURE(m_FooterPart.Lookup(FrameNum, TmpEntry)) )
+ {
+ DefaultLogSink().Error("Frame value out of range: %u\n", FrameNum);
+ return RESULT_RANGE;
+ }
+
+ // get frame position and go read the frame's key and length
+ Kumu::fpos_t FilePosition = CurrentPartition.BodyOffset + TmpEntry.StreamOffset;
+ Result_t result = RESULT_OK;
+
+ if ( FilePosition != m_LastPosition )
+ {
+ m_LastPosition = FilePosition;
+ result = m_File.Seek(FilePosition);
+ }
+
+ if( KM_SUCCESS(result) )
+ result = ReadEKLVPacket(FrameNum, FrameNum + 1, FrameBuf, EssenceUL, Ctx, HMAC);
+
+ return result;
+ }
+
+ // reads from current position
+ Result_t ReadEKLVPacket(ui32_t FrameNum, ui32_t SequenceNum, ASDCP::FrameBuffer& FrameBuf,
+ const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC)
+ {
+ assert(m_Dict);
+ return Read_EKLV_Packet(m_File, *m_Dict, m_HeaderPart, m_Info, m_LastPosition, m_CtFrameBuf,
+ FrameNum, SequenceNum, FrameBuf, EssenceUL, Ctx, HMAC);
+ }
+
+ //
+ void Close() {
+ m_File.Close();
+ }
+ };
+
+ }/// namespace MXF
//
- class h__Reader
+ class h__ASDCPReader : public MXF::TrackFileReader<OPAtomHeader, OPAtomIndexFooter>
{
- ASDCP_NO_COPY_CONSTRUCT(h__Reader);
+ ASDCP_NO_COPY_CONSTRUCT(h__ASDCPReader);
+ h__ASDCPReader();
public:
- FileReader m_File;
- OPAtomHeader m_HeaderPart;
- Partition m_BodyPart;
- OPAtomIndexFooter m_FooterPart;
- ui64_t m_EssenceStart;
- WriterInfo m_Info;
- ASDCP::FrameBuffer m_CtFrameBuf;
+ Partition m_BodyPart;
- h__Reader();
- virtual ~h__Reader();
+ h__ASDCPReader(const Dictionary&);
+ virtual ~h__ASDCPReader();
- Result_t InitInfo(WriterInfo& Info);
Result_t OpenMXFRead(const char* filename);
+ Result_t InitInfo();
Result_t InitMXFIndex();
- Result_t ReadEKLVPacket(ui32_t FrameNum, ASDCP::FrameBuffer& FrameBuf,
- const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC);
- void Close();
+ Result_t ReadEKLVFrame(ui32_t FrameNum, ASDCP::FrameBuffer& FrameBuf,
+ const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC);
};
inline Result_t Goto_FINAL() { Goto_body(ST_RUNNING, ST_FINAL); }
};
+ typedef std::list<ui64_t*> DurationElementList_t;
+
//
class h__Writer
{
ASDCP_NO_COPY_CONSTRUCT(h__Writer);
+ h__Writer();
public:
- FileWriter m_File;
+ const Dictionary* m_Dict;
+ Kumu::FileWriter m_File;
+ ui32_t m_HeaderSize;
OPAtomHeader m_HeaderPart;
Partition m_BodyPart;
OPAtomIndexFooter m_FooterPart;
-
-#if 0
ui64_t m_EssenceStart;
- WriterInfo m_Info;
- ASDCP::FrameBuffer m_CtFrameBuf;
- mem_ptr<MXFFile> m_File;
- mem_ptr<Metadata> m_Metadata;
- mem_ptr<Package> m_FilePackage;
- mem_ptr<Partition> m_HeaderPart;
- mem_ptr<MDObject> m_EssenceDescriptor;
- mem_ptr<Package> m_MaterialPackage;
- mem_ptr<Track> m_MPTrack; //! Material Package track for each essence stream
- mem_ptr<Track> m_FPTrack; //! File Package track for each essence stream
- mem_ptr<TimecodeComponent> m_MPTimecode;
- mem_ptr<TimecodeComponent> m_FPTimecode;
- mem_ptr<IndexManager> m_IndexMan;
- SourceClip* m_MPClip; //! Material Package SourceClip for each essence stream
- SourceClip* m_FPClip; //! File Package SourceClip for each essence stream
-#endif
+ MaterialPackage* m_MaterialPackage;
+ SourcePackage* m_FilePackage;
- i32_t m_EssenceID; //! Essence stream ID for each essence stream
- ui32_t m_FramesWritten;
- ui64_t m_StreamOffset;
- ASDCP::FrameBuffer m_CtFrameBuf;
- h__WriterState m_State;
- WriterInfo m_Info;
- FrameBuffer m_CryptFrameBuf;
+ FileDescriptor* m_EssenceDescriptor;
+ std::list<InterchangeObject*> m_EssenceSubDescriptorList;
- h__Writer();
+ ui32_t m_FramesWritten;
+ ui64_t m_StreamOffset;
+ ASDCP::FrameBuffer m_CtFrameBuf;
+ h__WriterState m_State;
+ WriterInfo m_Info;
+ DurationElementList_t m_DurationUpdateList;
+
+ h__Writer(const Dictionary&);
virtual ~h__Writer();
- Result_t WriteMXFHeader(EssenceType_t EssenceType, Rational& EditRate,
+ void InitHeader();
+ void AddSourceClip(const MXF::Rational& EditRate, ui32_t TCFrameRate,
+ const std::string& TrackName, const UL& EssenceUL,
+ const UL& DataDefinition, const std::string& PackageLabel);
+ void AddDMSegment(const MXF::Rational& EditRate, ui32_t TCFrameRate,
+ const std::string& TrackName, const UL& DataDefinition,
+ const std::string& PackageLabel);
+ void AddEssenceDescriptor(const UL& WrappingUL);
+ Result_t CreateBodyPart(const MXF::Rational& EditRate, ui32_t BytesPerEditUnit = 0);
+
+ // all the above for a single source clip
+ Result_t WriteMXFHeader(const std::string& PackageLabel, const UL& WrappingUL,
+ const std::string& TrackName, const UL& EssenceUL,
+ const UL& DataDefinition, const MXF::Rational& EditRate,
ui32_t TCFrameRate, ui32_t BytesPerEditUnit = 0);
+
Result_t WriteEKLVPacket(const ASDCP::FrameBuffer& FrameBuf,
const byte_t* EssenceUL, AESEncContext* Ctx, HMACContext* HMAC);
- Result_t WriteMXFFooter(EssenceType_t EssenceType);
+
+ Result_t WriteMXFFooter();
};
~IntegrityPack() {}
- Result_t CalcValues(const ASDCP::FrameBuffer&, byte_t* AssetID, ui32_t sequence, HMACContext* HMAC);
- Result_t TestValues(const ASDCP::FrameBuffer&, byte_t* AssetID, ui32_t sequence, HMACContext* HMAC);
+ Result_t CalcValues(const ASDCP::FrameBuffer&, const byte_t* AssetID, ui32_t sequence, HMACContext* HMAC);
+ Result_t TestValues(const ASDCP::FrameBuffer&, const byte_t* AssetID, ui32_t sequence, HMACContext* HMAC);
};
- //
- class KLVReader
- {
- byte_t m_Key[32];
- ui64_t m_Length;
- ui32_t m_BERLength;
- ui32_t m_HeaderLength;
-
- ASDCP_NO_COPY_CONSTRUCT(KLVReader);
-
- public:
- KLVReader() : m_Length(0), m_BERLength(0), m_HeaderLength(0) {}
- ~KLVReader() {}
-
- inline const byte_t* Key() { return m_Key; }
- inline const ui64_t Length() { return m_Length; }
- inline const ui64_t KLLength() { return m_BERLength + klv_key_size; }
- Result_t ReadKLFromFile(ASDCP::FileReader& Reader);
- };
} // namespace ASDCP
-#endif // _AS_DCP_INTERNAL_H__
+#endif // _AS_DCP_INTERNAL_H_
//