diff options
| author | jhurst <jhurst@cinecert.com> | 2013-06-03 00:12:31 +0000 |
|---|---|---|
| committer | jhurst <> | 2013-06-03 00:12:31 +0000 |
| commit | f285ea055151fae2e13d71c2d0282201197a9143 (patch) | |
| tree | 718615ffacefa98c498ec15dd9e373ccb8cba16b /src/AS_02_JP2K.cpp | |
| parent | dc0a23dd428b933040c9ff1602f5d35a27cbc8cb (diff) | |
the big-pre-as-02-refactor
Diffstat (limited to 'src/AS_02_JP2K.cpp')
| -rw-r--r-- | src/AS_02_JP2K.cpp | 603 |
1 files changed, 72 insertions, 531 deletions
diff --git a/src/AS_02_JP2K.cpp b/src/AS_02_JP2K.cpp index 4b1db70..136fd27 100644 --- a/src/AS_02_JP2K.cpp +++ b/src/AS_02_JP2K.cpp @@ -1,5 +1,5 @@ /* - Copyright (c) 2011-2012, Robert Scheler, Heiko Sparenberg Fraunhofer IIS, John Hurst + Copyright (c) 2011-2013, Robert Scheler, Heiko Sparenberg Fraunhofer IIS, John Hurst All rights reserved. Redistribution and use in source and binary forms, with or without @@ -34,12 +34,13 @@ #include <iostream> #include <iomanip> +using namespace ASDCP; using namespace ASDCP::JP2K; using Kumu::GenRandomValue; //------------------------------------------------------------------------------------------ -static std::string JP2K_PACKAGE_LABEL = "File Package: SMPTE 429-4 frame wrapping of JPEG 2000 codestreams"; +static std::string JP2K_PACKAGE_LABEL = "File Package: SMPTE ST 422 frame wrapping of JPEG 2000 codestreams"; static std::string PICT_DEF_LABEL = "Image Track"; //------------------------------------------------------------------------------------------ @@ -47,7 +48,7 @@ static std::string PICT_DEF_LABEL = "Image Track"; // hidden, internal implementation of JPEG 2000 reader -class lf__Reader : public AS_02::h__Reader +class AS_02::JP2K::MXFReader::h__Reader : public AS_02::h__AS02Reader { RGBAEssenceDescriptor* m_EssenceDescriptor; JPEG2000PictureSubDescriptor* m_EssenceSubDescriptor; @@ -55,16 +56,19 @@ class lf__Reader : public AS_02::h__Reader ASDCP::Rational m_SampleRate; EssenceType_t m_Format; - ASDCP_NO_COPY_CONSTRUCT(lf__Reader); + ASDCP_NO_COPY_CONSTRUCT(h__Reader); public: PictureDescriptor m_PDesc; // codestream parameter list - lf__Reader(const Dictionary& d) : - AS_02::h__Reader(d), m_EssenceDescriptor(0), m_EssenceSubDescriptor(0), m_Format(ESS_UNKNOWN) {} + h__Reader(const Dictionary& d) : + AS_02::h__AS02Reader(d), m_EssenceDescriptor(0), m_EssenceSubDescriptor(0), m_Format(ESS_UNKNOWN) {} + + virtual ~h__Reader() {} + Result_t OpenRead(const char*, EssenceType_t); - Result_t ReadFrame(ui32_t, JP2K::FrameBuffer&, AESDecContext*, HMACContext*); - Result_t MD_to_JP2K_PDesc(JP2K::PictureDescriptor& PDesc); + Result_t ReadFrame(ui32_t, ASDCP::JP2K::FrameBuffer&, AESDecContext*, HMACContext*); + Result_t MD_to_JP2K_PDesc(ASDCP::JP2K::PictureDescriptor& PDesc); Result_t OpenMXFRead(const char* filename); // positions file before reading @@ -79,10 +83,10 @@ public: // ASDCP::Result_t -lf__Reader::MD_to_JP2K_PDesc(ASDCP::JP2K::PictureDescriptor& PDesc) +AS_02::JP2K::MXFReader::h__Reader::MD_to_JP2K_PDesc(ASDCP::JP2K::PictureDescriptor& PDesc) { memset(&PDesc, 0, sizeof(PDesc)); - MXF::RGBAEssenceDescriptor* PDescObj = (MXF::RGBAEssenceDescriptor*)m_EssenceDescriptor; + ASDCP::MXF::RGBAEssenceDescriptor* PDescObj = (ASDCP::MXF::RGBAEssenceDescriptor*)m_EssenceDescriptor; PDesc.EditRate = m_EditRate; PDesc.SampleRate = m_SampleRate; @@ -135,7 +139,7 @@ lf__Reader::MD_to_JP2K_PDesc(ASDCP::JP2K::PictureDescriptor& PDesc) // // ASDCP::Result_t -lf__Reader::OpenRead(const char* filename, ASDCP::EssenceType_t type) +AS_02::JP2K::MXFReader::h__Reader::OpenRead(const char* filename, ASDCP::EssenceType_t type) { Result_t result = OpenMXFRead(filename); @@ -176,39 +180,6 @@ lf__Reader::OpenRead(const char* filename, ASDCP::EssenceType_t type) return RESULT_FORMAT; } } - else if ( type == ASDCP::ESS_JPEG_2000_S ) - { - if ( m_EditRate == EditRate_24 ) - { - if ( m_SampleRate != EditRate_48 ) - { - DefaultLogSink().Error("EditRate and SampleRate not correct for 24/48 stereoscopic essence.\n"); - return RESULT_FORMAT; - } - } - else if ( m_EditRate == EditRate_25 ) - { - if ( m_SampleRate != EditRate_50 ) - { - DefaultLogSink().Error("EditRate and SampleRate not correct for 25/50 stereoscopic essence.\n"); - return RESULT_FORMAT; - } - } - else if ( m_EditRate == EditRate_30 ) - { - if ( m_SampleRate != EditRate_60 ) - { - DefaultLogSink().Error("EditRate and SampleRate not correct for 30/60 stereoscopic essence.\n"); - return RESULT_FORMAT; - } - } - else - { - DefaultLogSink().Error("EditRate not correct for stereoscopic essence: %d/%d.\n", - m_EditRate.Numerator, m_EditRate.Denominator); - return RESULT_FORMAT; - } - } else { DefaultLogSink().Error("'type' argument unexpected: %x\n", type); @@ -218,20 +189,14 @@ lf__Reader::OpenRead(const char* filename, ASDCP::EssenceType_t type) result = MD_to_JP2K_PDesc(m_PDesc); } - if( ASDCP_SUCCESS(result) ) - result = InitMXFIndex(); - - if( ASDCP_SUCCESS(result) ) - result = InitInfo(); - return result; } // // ASDCP::Result_t -lf__Reader::ReadFrame(ui32_t FrameNum, JP2K::FrameBuffer& FrameBuf, - AESDecContext* Ctx, HMACContext* HMAC) +AS_02::JP2K::MXFReader::h__Reader::ReadFrame(ui32_t FrameNum, ASDCP::JP2K::FrameBuffer& FrameBuf, + ASDCP::AESDecContext* Ctx, ASDCP::HMACContext* HMAC) { if ( ! m_File.IsOpen() ) return RESULT_INIT; @@ -240,16 +205,8 @@ lf__Reader::ReadFrame(ui32_t FrameNum, JP2K::FrameBuffer& FrameBuf, return ReadEKLVFrame(FrameNum, FrameBuf, m_Dict->ul(MDD_JPEG2000Essence), Ctx, HMAC); } - +//------------------------------------------------------------------------------------------ // -class AS_02::JP2K::MXFReader::h__Reader : public lf__Reader -{ - ASDCP_NO_COPY_CONSTRUCT(h__Reader); - h__Reader(); - -public: - h__Reader(const Dictionary& d) : lf__Reader(d) {} -}; AS_02::JP2K::MXFReader::MXFReader() { @@ -264,13 +221,13 @@ AS_02::JP2K::MXFReader::~MXFReader() // Warning: direct manipulation of MXF structures can interfere // with the normal operation of the wrapper. Caveat emptor! // -ASDCP::MXF::OPAtomHeader& -AS_02::JP2K::MXFReader::OPAtomHeader() +ASDCP::MXF::OP1aHeader& +AS_02::JP2K::MXFReader::OP1aHeader() { if ( m_Reader.empty() ) { - assert(g_OPAtomHeader); - return *g_OPAtomHeader; + assert(g_OP1aHeader); + return *g_OP1aHeader; } return m_Reader->m_HeaderPart; @@ -279,29 +236,44 @@ AS_02::JP2K::MXFReader::OPAtomHeader() // Warning: direct manipulation of MXF structures can interfere // with the normal operation of the wrapper. Caveat emptor! // -/* -ASDCP::MXF::OPAtomIndexFooter& -AS_02::JP2K::MXFReader::OPAtomIndexFooter() +AS_02::MXF::AS02IndexReader& +AS_02::JP2K::MXFReader::AS02IndexReader() { if ( m_Reader.empty() ) { - assert(g_OPAtomIndexFooter); - return *g_OPAtomIndexFooter; + assert(g_AS02IndexReader); + return *g_AS02IndexReader; } - return m_Reader->m_FooterPart; + return m_Reader->m_IndexAccess; +} + +// Warning: direct manipulation of MXF structures can interfere +// with the normal operation of the wrapper. Caveat emptor! +// +ASDCP::MXF::RIP& +AS_02::JP2K::MXFReader::RIP() +{ + if ( m_Reader.empty() ) + { + assert(g_RIP); + return *g_RIP; + } + + return m_Reader->m_RIP; } -*/ // Open the file for reading. The file must exist. Returns error if the // operation cannot be completed. -Result_t AS_02::JP2K::MXFReader::OpenRead(const char* filename) const +Result_t +AS_02::JP2K::MXFReader::OpenRead(const char* filename) const { return m_Reader->OpenRead(filename, ASDCP::ESS_JPEG_2000); } // -Result_t AS_02::JP2K::MXFReader::ReadFrame(ui32_t FrameNum, ASDCP::JP2K::FrameBuffer& FrameBuf, +Result_t +AS_02::JP2K::MXFReader::ReadFrame(ui32_t FrameNum, ASDCP::JP2K::FrameBuffer& FrameBuf, ASDCP::AESDecContext* Ctx, ASDCP::HMACContext* HMAC) const { if ( m_Reader && m_Reader->m_File.IsOpen() ) @@ -338,110 +310,14 @@ ASDCP::Result_t AS_02::JP2K::MXFReader::FillWriterInfo(WriterInfo& Info) const return RESULT_INIT; } -// -void -AS_02::JP2K::MXFReader::DumpHeaderMetadata(FILE* stream) const -{ - if ( m_Reader->m_File.IsOpen() ) - m_Reader->m_HeaderPart.Dump(stream); -} - - -// -void -AS_02::JP2K::MXFReader::DumpIndex(FILE* stream) const -{ - if ( m_Reader->m_File.IsOpen() ) - m_Reader->m_FooterPart.Dump(stream); -} - -// standard method of opening an MXF file for read -Result_t -lf__Reader::OpenMXFRead(const char* filename) -{ - m_LastPosition = 0; - AS_02::MXF::OP1aIndexBodyPartion* pCurrentBodyPartIndex = NULL; - Partition* pPart = NULL; - ui64_t EssenceStart = 0; - Result_t result = m_File.OpenRead(filename); - if ( ASDCP_SUCCESS(result) ) - result = m_HeaderPart.InitFromFile(m_File); - - if ( ASDCP_SUCCESS(result) ) - { - ui32_t partition_size = m_HeaderPart.m_RIP.PairArray.size(); - - if ( partition_size > 3 ) - { - //for all entry except the first and the last(header&footer) - Array<RIP::Pair>::iterator r_i = m_HeaderPart.m_RIP.PairArray.begin(); - r_i++; - ui32_t i=2; - - while(r_i != m_HeaderPart.m_RIP.PairArray.end() && i<partition_size) - { - m_File.Seek((*r_i).ByteOffset); - pPart = new Partition(this->m_Dict); - result = pPart->InitFromFile(m_File); - - if(pPart->BodySID != 0 && pPart->IndexSID == 0) - { // AS_02::IS_FOLLOW - r_i++; i++; - EssenceStart = m_File.Tell(); - - m_File.Seek((*r_i).ByteOffset); - pCurrentBodyPartIndex = new AS_02::MXF::OP1aIndexBodyPartion(this->m_Dict); - pCurrentBodyPartIndex->m_Lookup = &m_HeaderPart.m_Primer; - result = pCurrentBodyPartIndex->InitFromFile(m_File); - } - else - { // AS_02::IS_LEAD - delete pPart; - m_File.Seek((*r_i).ByteOffset); - pCurrentBodyPartIndex = new AS_02::MXF::OP1aIndexBodyPartion(this->m_Dict); - pCurrentBodyPartIndex->m_Lookup = &m_HeaderPart.m_Primer; - result = pCurrentBodyPartIndex->InitFromFile(m_File); - - if ( ASDCP_FAILURE(result) ) - { - break; - } - - r_i++; i++; - m_File.Seek((*r_i).ByteOffset); - pPart = new Partition(this->m_Dict); - result = pPart->InitFromFile(m_File); - EssenceStart = m_File.Tell(); - } - - if ( ASDCP_FAILURE(result) ) - { - break; - } - - if(i==3) - { - this->m_EssenceStart = EssenceStart; - this->m_pCurrentBodyPartition = pPart; - this->m_pCurrentIndexPartition = pCurrentBodyPartIndex; - } - - this->m_BodyPartList.push_back(pCurrentBodyPartIndex); - this->m_BodyPartList.push_back(pPart); - r_i++; i++; - } - } - } - - return result; -} +//------------------------------------------------------------------------------------------ // -class lf__Writer : public AS_02::h__Writer +class AS_02::JP2K::MXFWriter::h__Writer : public AS_02::h__AS02Writer { - ASDCP_NO_COPY_CONSTRUCT(lf__Writer); - lf__Writer(); + ASDCP_NO_COPY_CONSTRUCT(h__Writer); + h__Writer(); JPEG2000PictureSubDescriptor* m_EssenceSubDescriptor; @@ -453,19 +329,19 @@ public: AS_02::IndexStrategy_t m_IndexStrategy; //Shim parameter index_strategy_frame/clip ui32_t m_PartitionSpace; //Shim parameter partition_spacing - lf__Writer(const Dictionary& d) : h__Writer(d), m_EssenceSubDescriptor(0), m_IndexStrategy(AS_02::IS_FOLLOW), m_PartitionSpace(60) { + h__Writer(const Dictionary& d) : h__AS02Writer(d), m_EssenceSubDescriptor(0), m_IndexStrategy(AS_02::IS_FOLLOW), m_PartitionSpace(60) { memset(m_EssenceUL, 0, SMPTE_UL_LENGTH); } - ~lf__Writer(){} + virtual ~h__Writer(){} Result_t OpenWrite(const char*, EssenceType_t type, const AS_02::IndexStrategy_t& IndexStrategy, const ui32_t& PartitionSpace, const ui32_t& HeaderSize); Result_t SetSourceStream(const PictureDescriptor&, const std::string& label, ASDCP::Rational LocalEditRate = ASDCP::Rational(0,0)); - Result_t WriteFrame(const JP2K::FrameBuffer&, bool add_index, AESEncContext*, HMACContext*); + Result_t WriteFrame(const ASDCP::JP2K::FrameBuffer&, bool add_index, ASDCP::AESEncContext*, ASDCP::HMACContext*); Result_t Finalize(); - Result_t JP2K_PDesc_to_MD(JP2K::PictureDescriptor& PDesc); + Result_t JP2K_PDesc_to_MD(ASDCP::JP2K::PictureDescriptor& PDesc); //void AddSourceClip(const MXF::Rational& EditRate, ui32_t TCFrameRate, // const std::string& TrackName, const UL& EssenceUL, @@ -495,11 +371,11 @@ static const byte_t s_PixelLayoutXYZ[PixelLayoutSize] = { 0xd8, 0x0c, 0xd9, 0x0c // ASDCP::Result_t -lf__Writer::JP2K_PDesc_to_MD(JP2K::PictureDescriptor& PDesc) +AS_02::JP2K::MXFWriter::h__Writer::JP2K_PDesc_to_MD(ASDCP::JP2K::PictureDescriptor& PDesc) { assert(m_EssenceDescriptor); assert(m_EssenceSubDescriptor); - MXF::RGBAEssenceDescriptor* PDescObj = (MXF::RGBAEssenceDescriptor*)m_EssenceDescriptor; + ASDCP::MXF::RGBAEssenceDescriptor* PDescObj = (ASDCP::MXF::RGBAEssenceDescriptor*)m_EssenceDescriptor; PDescObj->ContainerDuration = PDesc.ContainerDuration; PDescObj->SampleRate = PDesc.EditRate; @@ -578,7 +454,7 @@ lf__Writer::JP2K_PDesc_to_MD(JP2K::PictureDescriptor& PDesc) // Open the file for writing. The file must not exist. Returns error if // the operation cannot be completed. ASDCP::Result_t -lf__Writer::OpenWrite(const char* filename, EssenceType_t type, const AS_02::IndexStrategy_t& IndexStrategy, +AS_02::JP2K::MXFWriter::h__Writer::OpenWrite(const char* filename, EssenceType_t type, const AS_02::IndexStrategy_t& IndexStrategy, const ui32_t& PartitionSpace, const ui32_t& HeaderSize) { if ( ! m_State.Test_BEGIN() ) @@ -609,7 +485,7 @@ lf__Writer::OpenWrite(const char* filename, EssenceType_t type, const AS_02::Ind // Automatically sets the MXF file's metadata from the first jpeg codestream stream. ASDCP::Result_t -lf__Writer::SetSourceStream(const PictureDescriptor& PDesc, const std::string& label, ASDCP::Rational LocalEditRate) +AS_02::JP2K::MXFWriter::h__Writer::SetSourceStream(const PictureDescriptor& PDesc, const std::string& label, ASDCP::Rational LocalEditRate) { assert(m_Dict); if ( ! m_State.Test_INIT() ) @@ -632,9 +508,9 @@ lf__Writer::SetSourceStream(const PictureDescriptor& PDesc, const std::string& l { ui32_t TCFrameRate = ( m_PDesc.EditRate == EditRate_23_98 ) ? 24 : m_PDesc.EditRate.Numerator; - result = WriteMXFHeader(label, UL(m_Dict->ul(MDD_JPEG_2000Wrapping)), - PICT_DEF_LABEL, UL(m_EssenceUL), UL(m_Dict->ul(MDD_PictureDataDef)), - LocalEditRate, TCFrameRate); + result = WriteAS02Header(label, UL(m_Dict->ul(MDD_JPEG_2000Wrapping)), + PICT_DEF_LABEL, UL(m_EssenceUL), UL(m_Dict->ul(MDD_PictureDataDef)), + LocalEditRate, TCFrameRate); } return result; @@ -646,7 +522,7 @@ lf__Writer::SetSourceStream(const PictureDescriptor& PDesc, const std::string& l // error occurs. // ASDCP::Result_t -lf__Writer::WriteFrame(const ASDCP::JP2K::FrameBuffer& FrameBuf, bool add_index, +AS_02::JP2K::MXFWriter::h__Writer::WriteFrame(const ASDCP::JP2K::FrameBuffer& FrameBuf, bool add_index, AESEncContext* Ctx, HMACContext* HMAC) { Result_t result = RESULT_OK; @@ -691,7 +567,7 @@ lf__Writer::WriteFrame(const ASDCP::JP2K::FrameBuffer& FrameBuf, bool add_index, // Closes the MXF file, writing the index and other closing information. // ASDCP::Result_t -lf__Writer::Finalize() +AS_02::JP2K::MXFWriter::h__Writer::Finalize() { Result_t result = RESULT_OK; @@ -709,18 +585,6 @@ lf__Writer::Finalize() return WriteMXFFooter(); } - -// -class AS_02::JP2K::MXFWriter::h__Writer : public lf__Writer -{ - ASDCP_NO_COPY_CONSTRUCT(h__Writer); - h__Writer(); - -public: - h__Writer(const Dictionary& d) : lf__Writer(d) {} -}; - - //------------------------------------------------------------------------------------------ @@ -736,13 +600,13 @@ AS_02::JP2K::MXFWriter::~MXFWriter() // Warning: direct manipulation of MXF structures can interfere // with the normal operation of the wrapper. Caveat emptor! // -ASDCP::MXF::OPAtomHeader& -AS_02::JP2K::MXFWriter::OPAtomHeader() +ASDCP::MXF::OP1aHeader& +AS_02::JP2K::MXFWriter::OP1aHeader() { if ( m_Writer.empty() ) { - assert(g_OPAtomHeader); - return *g_OPAtomHeader; + assert(g_OP1aHeader); + return *g_OP1aHeader; } return m_Writer->m_HeaderPart; @@ -751,19 +615,17 @@ AS_02::JP2K::MXFWriter::OPAtomHeader() // Warning: direct manipulation of MXF structures can interfere // with the normal operation of the wrapper. Caveat emptor! // -/* -ASDCP::MXF::OPAtomIndexFooter& -AS_02::JP2K::MXFWriter::OPAtomIndexFooter() +ASDCP::MXF::RIP& +AS_02::JP2K::MXFWriter::RIP() { if ( m_Writer.empty() ) { - assert(g_OPAtomIndexFooter); - return *g_OPAtomIndexFooter; + assert(g_RIP); + return *g_RIP; } - return m_Writer->m_FooterPart; + return m_Writer->m_RIP; } -*/ // Open the file for writing. The file must not exist. Returns error if // the operation cannot be completed. @@ -813,327 +675,6 @@ AS_02::JP2K::MXFWriter::Finalize() } -//------------------------------------------------------------------------------------------ - -// standard method of reading a plaintext or encrypted frame -Result_t -lf__Reader::ReadEKLVFrame(ui32_t FrameNum, ASDCP::FrameBuffer& FrameBuf, - const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC) -{ - Result_t result = RESULT_OK; - IndexTableSegment::IndexEntry TmpEntry; - - assert(m_pCurrentIndexPartition != NULL); - - if( m_pCurrentIndexPartition->Lookup(FrameNum, TmpEntry, m_start_pos) == false) - { - m_start_pos = 0; - - //compute new indexPartition for this FrameNum - for (ui32_t i = 0; i < m_BodyPartList.size(); i+=2 ) - { - if( m_IndexStrategy == AS_02::IS_FOLLOW ) - { - m_pCurrentBodyPartition = m_BodyPartList.at(i); - m_pCurrentIndexPartition = dynamic_cast<AS_02::MXF::OP1aIndexBodyPartion*> (m_BodyPartList.at(i+1)); - } - else if( m_IndexStrategy == AS_02::IS_LEAD ) - { - m_pCurrentIndexPartition = dynamic_cast<AS_02::MXF::OP1aIndexBodyPartion*> (m_BodyPartList.at(i)); - m_pCurrentBodyPartition = m_BodyPartList.at(i+1); - } - else - { - return RESULT_FORMAT; //return error - } - - if( m_pCurrentIndexPartition == 0 ) - return RESULT_FORMAT; - - if(m_pCurrentIndexPartition->Lookup(FrameNum, TmpEntry, m_start_pos)) - { - if ( FrameNum % m_PartitionSpace == 0 ) - { - ui64_t offset = m_pCurrentBodyPartition->ThisPartition - m_pCurrentBodyPartition->PreviousPartition; - offset += m_pCurrentBodyPartition->ArchiveSize();//Offset for the BodyPartitionHeader - Partition::ArchiveSize(); - m_EssenceStart += offset; - } - break; - } - } - } - - // get frame position and go read the frame's key and length - Kumu::fpos_t FilePosition = this->m_EssenceStart + TmpEntry.StreamOffset; - - if ( FilePosition != m_LastPosition ) - { - m_LastPosition = FilePosition; - result = m_File.Seek(FilePosition); - } - - if( ASDCP_SUCCESS(result) ) - result = ReadEKLVPacket(FrameNum, FrameNum + 1, FrameBuf, EssenceUL, Ctx, HMAC); - - return result; -} - -// -Result_t -lf__Reader::ReadEKLVPacket(ui32_t FrameNum, ui32_t SequenceNum, ASDCP::FrameBuffer& FrameBuf, - const byte_t* EssenceUL, AESDecContext* Ctx, HMACContext* HMAC) -{ - KLReader Reader; - Result_t result = Reader.ReadKLFromFile(m_File); - - if ( ASDCP_FAILURE(result) ) - return result; - - UL Key(Reader.Key()); - ui64_t PacketLength = Reader.Length(); - m_LastPosition = m_LastPosition + Reader.KLLength() + PacketLength; - assert(m_Dict); - - if ( memcmp(Key.Value(), m_Dict->ul(MDD_CryptEssence), Key.Size() - 1) == 0 ) // ignore the stream numbers - { - if ( ! m_Info.EncryptedEssence ) - { - DefaultLogSink().Error("EKLV packet found, no Cryptographic Context in header.\n"); - return RESULT_FORMAT; - } - - // read encrypted triplet value into internal buffer - assert(PacketLength <= 0xFFFFFFFFL); - m_CtFrameBuf.Capacity((ui32_t) PacketLength); - ui32_t read_count; - result = m_File.Read(m_CtFrameBuf.Data(), (ui32_t) PacketLength, - &read_count); - - if ( ASDCP_FAILURE(result) ) - return result; - - if ( read_count != PacketLength ) - { - DefaultLogSink().Error("read length is smaller than EKLV packet length.\n"); - return RESULT_FORMAT; - } - - m_CtFrameBuf.Size((ui32_t) PacketLength); - - // should be const but mxflib::ReadBER is not - byte_t* ess_p = m_CtFrameBuf.Data(); - - // read context ID length - if ( ! Kumu::read_test_BER(&ess_p, UUIDlen) ) - return RESULT_FORMAT; - - // test the context ID - if ( memcmp(ess_p, m_Info.ContextID, UUIDlen) != 0 ) - { - DefaultLogSink().Error("Packet's Cryptographic Context ID does not match the header.\n"); - return RESULT_FORMAT; - } - ess_p += UUIDlen; - - // read PlaintextOffset length - if ( ! Kumu::read_test_BER(&ess_p, sizeof(ui64_t)) ) - return RESULT_FORMAT; - - ui32_t PlaintextOffset = (ui32_t)KM_i64_BE(Kumu::cp2i<ui64_t>(ess_p)); - ess_p += sizeof(ui64_t); - - // read essence UL length - if ( ! Kumu::read_test_BER(&ess_p, SMPTE_UL_LENGTH) ) - return RESULT_FORMAT; - - // test essence UL - if ( memcmp(ess_p, EssenceUL, SMPTE_UL_LENGTH - 1) != 0 ) // ignore the stream number - { - char strbuf[IntBufferLen]; - const MDDEntry* Entry = m_Dict->FindUL(Key.Value()); - if ( Entry == 0 ) - DefaultLogSink().Warn("Unexpected Essence UL found: %s.\n", Key.EncodeString(strbuf, IntBufferLen)); - else - DefaultLogSink().Warn("Unexpected Essence UL found: %s.\n", Entry->name); - return RESULT_FORMAT; - } - ess_p += SMPTE_UL_LENGTH; - - // read SourceLength length - if ( ! Kumu::read_test_BER(&ess_p, sizeof(ui64_t)) ) - return RESULT_FORMAT; - - ui32_t SourceLength = (ui32_t)KM_i64_BE(Kumu::cp2i<ui64_t>(ess_p)); - ess_p += sizeof(ui64_t); - assert(SourceLength); - - if ( FrameBuf.Capacity() < SourceLength ) - { - DefaultLogSink().Error("FrameBuf.Capacity: %u SourceLength: %u\n", FrameBuf.Capacity(), SourceLength); - return RESULT_SMALLBUF; - } - - ui32_t esv_length = calc_esv_length(SourceLength, PlaintextOffset); - - // read ESV length - if ( ! Kumu::read_test_BER(&ess_p, esv_length) ) - { - DefaultLogSink().Error("read_test_BER did not return %u\n", esv_length); - return RESULT_FORMAT; - } - - ui32_t tmp_len = esv_length + (m_Info.UsesHMAC ? klv_intpack_size : 0); - - if ( PacketLength < tmp_len ) - { - DefaultLogSink().Error("Frame length is larger than EKLV packet length.\n"); - return RESULT_FORMAT; - } - - if ( Ctx ) - { - // wrap the pointer and length as a FrameBuffer for use by - // DecryptFrameBuffer() and TestValues() - ASDCP::FrameBuffer TmpWrapper; - TmpWrapper.SetData(ess_p, tmp_len); - TmpWrapper.Size(tmp_len); - TmpWrapper.SourceLength(SourceLength); - TmpWrapper.PlaintextOffset(PlaintextOffset); - - result = DecryptFrameBuffer(TmpWrapper, FrameBuf, Ctx); - FrameBuf.FrameNumber(FrameNum); - - // detect and test integrity pack - if ( ASDCP_SUCCESS(result) && m_Info.UsesHMAC && HMAC ) - { - IntegrityPack IntPack; - result = IntPack.TestValues(TmpWrapper, m_Info.AssetUUID, SequenceNum, HMAC); - } - } - else // return ciphertext to caller - { - if ( FrameBuf.Capacity() < tmp_len ) - { - char intbuf[IntBufferLen]; - DefaultLogSink().Error("FrameBuf.Capacity: %u FrameLength: %s\n", - FrameBuf.Capacity(), ui64sz(PacketLength, intbuf)); - return RESULT_SMALLBUF; - } - - memcpy(FrameBuf.Data(), ess_p, tmp_len); - FrameBuf.Size(tmp_len); - FrameBuf.FrameNumber(FrameNum); - FrameBuf.SourceLength(SourceLength); - FrameBuf.PlaintextOffset(PlaintextOffset); - } - } - else if ( memcmp(Key.Value(), EssenceUL, Key.Size() - 1) == 0 ) // ignore the stream number - { // read plaintext frame - if ( FrameBuf.Capacity() < PacketLength ) - { - char intbuf[IntBufferLen]; - DefaultLogSink().Error("FrameBuf.Capacity: %u FrameLength: %s\n", - FrameBuf.Capacity(), ui64sz(PacketLength, intbuf)); - return RESULT_SMALLBUF; - } - - // read the data into the supplied buffer - ui32_t read_count; - assert(PacketLength <= 0xFFFFFFFFL); - result = m_File.Read(FrameBuf.Data(), (ui32_t) PacketLength, &read_count); - - if ( ASDCP_FAILURE(result) ) - return result; - - if ( read_count != PacketLength ) - { - char intbuf1[IntBufferLen]; - char intbuf2[IntBufferLen]; - DefaultLogSink().Error("read_count: %s != FrameLength: %s\n", - ui64sz(read_count, intbuf1), - ui64sz(PacketLength, intbuf2) ); - - return RESULT_READFAIL; - } - - FrameBuf.FrameNumber(FrameNum); - FrameBuf.Size(read_count); - } - else - { - char strbuf[IntBufferLen]; - const MDDEntry* Entry = m_Dict->FindUL(Key.Value()); - if ( Entry == 0 ) - DefaultLogSink().Warn("Unexpected Essence UL found: %s.\n", Key.EncodeString(strbuf, IntBufferLen)); - else - DefaultLogSink().Warn("Unexpected Essence UL found: %s.\n", Entry->name); - return RESULT_FORMAT; - } - - return result; -} - - -// standard method of writing the header and footer of a completed MXF file -// -Result_t -lf__Writer::WriteMXFFooter() -{ - // Set top-level file package correctly for OP-Atom - - // m_MPTCSequence->Duration = m_MPTimecode->Duration = m_MPClSequence->Duration = m_MPClip->Duration = - // m_FPTCSequence->Duration = m_FPTimecode->Duration = m_FPClSequence->Duration = m_FPClip->Duration = - - DurationElementList_t::iterator dli = m_DurationUpdateList.begin(); - - for (; dli != m_DurationUpdateList.end(); dli++ ) - **dli = m_FramesWritten; - - m_EssenceDescriptor->ContainerDuration = m_FramesWritten; - m_FooterPart.PreviousPartition = m_HeaderPart.m_RIP.PairArray.back().ByteOffset; - - Kumu::fpos_t here = m_File.Tell(); - m_HeaderPart.m_RIP.PairArray.push_back(RIP::Pair(0, here)); // Last RIP Entry - m_HeaderPart.FooterPartition = here; - - assert(m_Dict); - // re-label the partition - UL OPAtomUL(m_Dict->ul(MDD_OP1a)); - m_HeaderPart.OperationalPattern = OPAtomUL; - m_HeaderPart.m_Preface->OperationalPattern = m_HeaderPart.OperationalPattern; - - m_FooterPart.OperationalPattern = m_HeaderPart.OperationalPattern; - m_FooterPart.EssenceContainers = m_HeaderPart.EssenceContainers; - m_FooterPart.FooterPartition = here; - m_FooterPart.ThisPartition = here; - - Result_t result = m_FooterPart.WriteToFile(m_File, m_FramesWritten); - - if ( ASDCP_SUCCESS(result) ) - result = m_HeaderPart.m_RIP.WriteToFile(m_File); - - if ( ASDCP_SUCCESS(result) ) - result = m_File.Seek(0); - - if ( ASDCP_SUCCESS(result) ) - result = m_HeaderPart.WriteToFile(m_File, m_HeaderSize); - - //update the value of FooterPartition in all Partitions - std::vector<Partition*>::iterator iter = this->m_BodyPartList.begin(); - for (; iter != this->m_BodyPartList.end(); iter++ ){ - (*iter)->FooterPartition = m_FooterPart.ThisPartition; - if ( ASDCP_SUCCESS(result) ) - result = m_File.Seek((*iter)->ThisPartition); - if ( ASDCP_SUCCESS(result) ){ - UL BodyUL(m_Dict->ul(MDD_ClosedCompleteBodyPartition)); - result = (*iter)->WriteToFile(m_File, BodyUL); - } - } - - m_File.Close(); - return result; -} - // // end AS_02_JP2K.cpp // |
