+ 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
+ {
+ 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