build fixes from DTS
[asdcplib.git] / src / MXF.cpp
index d5585d3a96fa304bdba6c4a23cf1f32505f8f690..f2756abd62ca336c28b186f78cb5d549354db58a 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005-2006, John Hurst
+Copyright (c) 2005-2008, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -30,8 +30,15 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 #include "MXF.h"
-#include "hex_utils.h"
+#include "Metadata.h"
+#include <KM_log.h>
 
+using Kumu::DefaultLogSink;
+using Kumu::GenRandomValue;
+
+// index segments must be < 64K
+// NOTE: this value may too high if advanced index entry elements are used.
+const ui32_t CBRIndexEntriesPerSegment = 5000;
 
 //------------------------------------------------------------------------------------------
 //
@@ -40,12 +47,12 @@ const ui32_t kl_length = ASDCP::SMPTE_UL_LENGTH + ASDCP::MXF_BER_LENGTH;
 
 //
 ASDCP::Result_t
-ASDCP::MXF::SeekToRIP(const ASDCP::FileReader& Reader)
+ASDCP::MXF::SeekToRIP(const Kumu::FileReader& Reader)
 {
-  ASDCP::fpos_t end_pos;
+  Kumu::fpos_t end_pos;
 
   // go to the end - 4 bytes
-  Result_t result = Reader.Seek(0, ASDCP::SP_END);
+  Result_t result = Reader.Seek(0, Kumu::SP_END);
 
   if ( ASDCP_SUCCESS(result) )
     result = Reader.Tell(&end_pos);
@@ -72,7 +79,7 @@ ASDCP::MXF::SeekToRIP(const ASDCP::FileReader& Reader)
 
   if ( ASDCP_SUCCESS(result) )
     {
-      rip_size = ASDCP_i32_BE(cp2i<ui32_t>(intbuf));
+      rip_size = KM_i32_BE(Kumu::cp2i<ui32_t>(intbuf));
 
       if ( rip_size > end_pos ) // RIP can't be bigger than the file
        return RESULT_FAIL;
@@ -87,14 +94,31 @@ ASDCP::MXF::SeekToRIP(const ASDCP::FileReader& Reader)
 
 //
 ASDCP::Result_t
-ASDCP::MXF::RIP::InitFromFile(const ASDCP::FileReader& Reader)
+ASDCP::MXF::RIP::GetPairBySID(ui32_t SID, Pair& outPair) const
+{
+  Array<Pair>::const_iterator pi = PairArray.begin();
+  for ( ; pi != PairArray.end(); pi++ )
+    {
+      if ( (*pi).BodySID == SID )
+       {
+         outPair = *pi;
+         return RESULT_OK;
+       }
+    }
+
+  return RESULT_FAIL;
+}
+
+//
+ASDCP::Result_t
+ASDCP::MXF::RIP::InitFromFile(const Kumu::FileReader& Reader)
 {
   Result_t result = KLVFilePacket::InitFromFile(Reader, Dict::ul(MDD_RandomIndexMetadata));
 
   if ( ASDCP_SUCCESS(result) )
     {
-      MemIOReader MemRDR(m_ValueStart, m_ValueLength - 4);
-      result =  PairArray.Unarchive(MemRDR);
+      Kumu::MemIOReader MemRDR(m_ValueStart, m_ValueLength - 4);
+      result = PairArray.Unarchive(&MemRDR) ? RESULT_OK : RESULT_KLV_CODING;
     }
 
   if ( ASDCP_FAILURE(result) )
@@ -105,7 +129,7 @@ ASDCP::MXF::RIP::InitFromFile(const ASDCP::FileReader& Reader)
 
 //
 ASDCP::Result_t
-ASDCP::MXF::RIP::WriteToFile(ASDCP::FileWriter& Writer)
+ASDCP::MXF::RIP::WriteToFile(Kumu::FileWriter& Writer)
 {
   ASDCP::FrameBuffer Buffer;
   ui32_t RIPSize = ( PairArray.size() * (sizeof(ui32_t) + sizeof(ui64_t)) ) + 4;
@@ -116,14 +140,15 @@ ASDCP::MXF::RIP::WriteToFile(ASDCP::FileWriter& Writer)
 
   if ( ASDCP_SUCCESS(result) )
     {
-      MemIOWriter MemWRT(Buffer.Data(), Buffer.Capacity());
-      result =  PairArray.Archive(MemWRT);
+      result = RESULT_KLV_CODING;
 
-      if ( ASDCP_SUCCESS(result) )
-       MemWRT.WriteUi32BE(RIPSize + 20);
-
-      if ( ASDCP_SUCCESS(result) )
-       Buffer.Size(MemWRT.Size());
+      Kumu::MemIOWriter MemWRT(Buffer.Data(), Buffer.Capacity());
+      if ( PairArray.Archive(&MemWRT) )
+       if ( MemWRT.WriteUi32BE(RIPSize + 20) )
+         {
+           Buffer.Size(MemWRT.Length());
+           result = RESULT_OK;
+         }
     }
 
   if ( ASDCP_SUCCESS(result) )
@@ -141,8 +166,6 @@ ASDCP::MXF::RIP::Dump(FILE* stream)
 
   KLVFilePacket::Dump(stream, false);
   PairArray.Dump(stream, false);
-
-  fputs("==========================================================================\n", stream);
 }
 
 //------------------------------------------------------------------------------------------
@@ -171,6 +194,23 @@ public:
     m_List.push_back(ThePacket);
   }
 
+  //
+  Result_t GetMDObjectByID(const UUID& ObjectID, InterchangeObject** Object)
+  {
+    ASDCP_TEST_NULL(Object);
+
+    std::map<UUID, InterchangeObject*>::iterator mi = m_Map.find(ObjectID);
+
+    if ( mi == m_Map.end() )
+      {
+       *Object = 0;
+       return RESULT_FAIL;
+      }
+
+    *Object = (*mi).second;
+    return RESULT_OK;
+  }
+
   //
   Result_t GetMDObjectByType(const byte_t* ObjectID, InterchangeObject** Object)
   {
@@ -190,6 +230,21 @@ public:
 
     return RESULT_FAIL;
   }
+
+  //
+  Result_t GetMDObjectsByType(const byte_t* ObjectID, std::list<InterchangeObject*>& ObjectList)
+  {
+    ASDCP_TEST_NULL(ObjectID);
+    std::list<InterchangeObject*>::iterator li;
+
+    for ( li = m_List.begin(); li != m_List.end(); li++ )
+      {
+       if ( (*li)->HasUL(ObjectID) )
+         ObjectList.push_back(*li);
+      }
+
+    return ObjectList.empty() ? RESULT_FAIL : RESULT_OK;
+  }
 };
 
 //------------------------------------------------------------------------------------------
@@ -200,7 +255,7 @@ ASDCP::MXF::Partition::Partition() :
   MajorVersion(1), MinorVersion(2),
   KAGSize(1), ThisPartition(0), PreviousPartition(0),
   FooterPartition(0), HeaderByteCount(0), IndexByteCount(0), IndexSID(0),
-  BodyOffset(0), BodySID(1)
+  BodyOffset(0), BodySID(0)
 {
   m_PacketList = new h__PacketList;
 }
@@ -214,15 +269,16 @@ void
 ASDCP::MXF::Partition::AddChildObject(InterchangeObject* Object)
 {
   assert(Object);
-  UUID TmpID;
-  TmpID.GenRandomValue();
-  Object->InstanceUID = TmpID;
+
+  if ( ! Object->InstanceUID.HasValue() )
+    GenRandomValue(Object->InstanceUID);
+
   m_PacketList->AddPacket(Object);
 }
 
 //
 ASDCP::Result_t
-ASDCP::MXF::Partition::InitFromFile(const ASDCP::FileReader& Reader)
+ASDCP::MXF::Partition::InitFromFile(const Kumu::FileReader& Reader)
 {
   Result_t result = KLVFilePacket::InitFromFile(Reader);
   // test the UL
@@ -230,20 +286,23 @@ ASDCP::MXF::Partition::InitFromFile(const ASDCP::FileReader& Reader)
 
   if ( ASDCP_SUCCESS(result) )
     {
-      MemIOReader MemRDR(m_ValueStart, m_ValueLength);
-      result = MemRDR.ReadUi16BE(&MajorVersion);
-      if ( ASDCP_SUCCESS(result) )  result = MemRDR.ReadUi16BE(&MinorVersion);
-      if ( ASDCP_SUCCESS(result) )  result = MemRDR.ReadUi32BE(&KAGSize);
-      if ( ASDCP_SUCCESS(result) )  result = MemRDR.ReadUi64BE(&ThisPartition);
-      if ( ASDCP_SUCCESS(result) )  result = MemRDR.ReadUi64BE(&PreviousPartition);
-      if ( ASDCP_SUCCESS(result) )  result = MemRDR.ReadUi64BE(&FooterPartition);
-      if ( ASDCP_SUCCESS(result) )  result = MemRDR.ReadUi64BE(&HeaderByteCount);
-      if ( ASDCP_SUCCESS(result) )  result = MemRDR.ReadUi64BE(&IndexByteCount);
-      if ( ASDCP_SUCCESS(result) )  result = MemRDR.ReadUi32BE(&IndexSID);
-      if ( ASDCP_SUCCESS(result) )  result = MemRDR.ReadUi64BE(&BodyOffset);
-      if ( ASDCP_SUCCESS(result) )  result = MemRDR.ReadUi32BE(&BodySID);
-      if ( ASDCP_SUCCESS(result) )  result = OperationalPattern.Unarchive(MemRDR);
-      if ( ASDCP_SUCCESS(result) )  result = EssenceContainers.Unarchive(MemRDR);
+      Kumu::MemIOReader MemRDR(m_ValueStart, m_ValueLength);
+      result = RESULT_KLV_CODING;
+
+      if ( MemRDR.ReadUi16BE(&MajorVersion) )
+       if ( MemRDR.ReadUi16BE(&MinorVersion) )
+         if ( MemRDR.ReadUi32BE(&KAGSize) )
+           if ( MemRDR.ReadUi64BE(&ThisPartition) )
+             if ( MemRDR.ReadUi64BE(&PreviousPartition) )
+               if ( MemRDR.ReadUi64BE(&FooterPartition) )
+                 if ( MemRDR.ReadUi64BE(&HeaderByteCount) )
+                   if ( MemRDR.ReadUi64BE(&IndexByteCount) )
+                     if ( MemRDR.ReadUi32BE(&IndexSID) )
+                       if ( MemRDR.ReadUi64BE(&BodyOffset) )
+                         if ( MemRDR.ReadUi32BE(&BodySID) )
+                           if ( OperationalPattern.Unarchive(&MemRDR) )
+                             if ( EssenceContainers.Unarchive(&MemRDR) )
+                               result = RESULT_OK;
     }
 
   if ( ASDCP_FAILURE(result) )
@@ -254,28 +313,32 @@ ASDCP::MXF::Partition::InitFromFile(const ASDCP::FileReader& Reader)
 
 //
 ASDCP::Result_t
-ASDCP::MXF::Partition::WriteToFile(ASDCP::FileWriter& Writer, UL& PartitionLabel)
+ASDCP::MXF::Partition::WriteToFile(Kumu::FileWriter& Writer, UL& PartitionLabel)
 {
   ASDCP::FrameBuffer Buffer;
   Result_t result = Buffer.Capacity(1024);
 
   if ( ASDCP_SUCCESS(result) )
     {
-      MemIOWriter MemWRT(Buffer.Data(), Buffer.Capacity());
-      result = MemWRT.WriteUi16BE(MajorVersion);
-      if ( ASDCP_SUCCESS(result) )  result = MemWRT.WriteUi16BE(MinorVersion);
-      if ( ASDCP_SUCCESS(result) )  result = MemWRT.WriteUi32BE(KAGSize);
-      if ( ASDCP_SUCCESS(result) )  result = MemWRT.WriteUi64BE(ThisPartition);
-      if ( ASDCP_SUCCESS(result) )  result = MemWRT.WriteUi64BE(PreviousPartition);
-      if ( ASDCP_SUCCESS(result) )  result = MemWRT.WriteUi64BE(FooterPartition);
-      if ( ASDCP_SUCCESS(result) )  result = MemWRT.WriteUi64BE(HeaderByteCount);
-      if ( ASDCP_SUCCESS(result) )  result = MemWRT.WriteUi64BE(IndexByteCount);
-      if ( ASDCP_SUCCESS(result) )  result = MemWRT.WriteUi32BE(IndexSID);
-      if ( ASDCP_SUCCESS(result) )  result = MemWRT.WriteUi64BE(BodyOffset);
-      if ( ASDCP_SUCCESS(result) )  result = MemWRT.WriteUi32BE(BodySID);
-      if ( ASDCP_SUCCESS(result) )  result = OperationalPattern.Archive(MemWRT);
-      if ( ASDCP_SUCCESS(result) )  result = EssenceContainers.Archive(MemWRT);
-      if ( ASDCP_SUCCESS(result) )  Buffer.Size(MemWRT.Size());
+      Kumu::MemIOWriter MemWRT(Buffer.Data(), Buffer.Capacity());
+      result = RESULT_KLV_CODING;
+      if ( MemWRT.WriteUi16BE(MajorVersion) )
+       if ( MemWRT.WriteUi16BE(MinorVersion) )
+         if ( MemWRT.WriteUi32BE(KAGSize) )
+           if ( MemWRT.WriteUi64BE(ThisPartition) )
+             if ( MemWRT.WriteUi64BE(PreviousPartition) )
+               if ( MemWRT.WriteUi64BE(FooterPartition) )
+                 if ( MemWRT.WriteUi64BE(HeaderByteCount) )
+                   if ( MemWRT.WriteUi64BE(IndexByteCount) )
+                     if ( MemWRT.WriteUi32BE(IndexSID) )
+                       if ( MemWRT.WriteUi64BE(BodyOffset) )
+                         if ( MemWRT.WriteUi32BE(BodySID) )
+                           if ( OperationalPattern.Archive(&MemWRT) )
+                             if ( EssenceContainers.Archive(&MemWRT) )
+                               {
+                                 Buffer.Size(MemWRT.Length());
+                                 result = RESULT_OK;
+                               }
     }
 
   if ( ASDCP_SUCCESS(result) )
@@ -310,7 +373,6 @@ void
 ASDCP::MXF::Partition::Dump(FILE* stream)
 {
   char identbuf[IdentBufferLen];
-  char intbuf[IntBufferLen];
 
   if ( stream == 0 )
     stream = stderr;
@@ -318,19 +380,17 @@ ASDCP::MXF::Partition::Dump(FILE* stream)
   KLVFilePacket::Dump(stream, false);
   fprintf(stream, "  MajorVersion       = %hu\n", MajorVersion);
   fprintf(stream, "  MinorVersion       = %hu\n", MinorVersion);
-  fprintf(stream, "  KAGSize            = %lu\n", KAGSize);
-  fprintf(stream, "  ThisPartition      = %s\n",  ui64sz(ThisPartition, intbuf));
-  fprintf(stream, "  PreviousPartition  = %s\n",  ui64sz(PreviousPartition, intbuf));
-  fprintf(stream, "  FooterPartition    = %s\n",  ui64sz(FooterPartition, intbuf));
-  fprintf(stream, "  HeaderByteCount    = %s\n",  ui64sz(HeaderByteCount, intbuf));
-  fprintf(stream, "  IndexByteCount     = %s\n",  ui64sz(IndexByteCount, intbuf));
-  fprintf(stream, "  IndexSID           = %lu\n", IndexSID);
-  fprintf(stream, "  BodyOffset         = %s\n",  ui64sz(BodyOffset, intbuf));
-  fprintf(stream, "  BodySID            = %lu\n", BodySID);
-  fprintf(stream, "  OperationalPattern = %s\n",  OperationalPattern.ToString(identbuf));
+  fprintf(stream, "  KAGSize            = %u\n",  KAGSize);
+  fprintf(stream, "  ThisPartition      = %s\n",  ui64sz(ThisPartition, identbuf));
+  fprintf(stream, "  PreviousPartition  = %s\n",  ui64sz(PreviousPartition, identbuf));
+  fprintf(stream, "  FooterPartition    = %s\n",  ui64sz(FooterPartition, identbuf));
+  fprintf(stream, "  HeaderByteCount    = %s\n",  ui64sz(HeaderByteCount, identbuf));
+  fprintf(stream, "  IndexByteCount     = %s\n",  ui64sz(IndexByteCount, identbuf));
+  fprintf(stream, "  IndexSID           = %u\n",  IndexSID);
+  fprintf(stream, "  BodyOffset         = %s\n",  ui64sz(BodyOffset, identbuf));
+  fprintf(stream, "  BodySID            = %u\n",  BodySID);
+  fprintf(stream, "  OperationalPattern = %s\n",  OperationalPattern.EncodeString(identbuf, IdentBufferLen));
   fputs("Essence Containers:\n", stream); EssenceContainers.Dump(stream, false);
-
-  fputs("==========================================================================\n", stream);
 }
 
 
@@ -372,8 +432,8 @@ ASDCP::MXF::Primer::InitFromBuffer(const byte_t* p, ui32_t l)
 
   if ( ASDCP_SUCCESS(result) )
     {
-      MemIOReader MemRDR(m_ValueStart, m_ValueLength);
-      result = LocalTagEntryBatch.Unarchive(MemRDR);
+      Kumu::MemIOReader MemRDR(m_ValueStart, m_ValueLength);
+      result = LocalTagEntryBatch.Unarchive(&MemRDR) ? RESULT_OK : RESULT_KLV_CODING;
     }
 
   if ( ASDCP_SUCCESS(result) )
@@ -390,7 +450,7 @@ ASDCP::MXF::Primer::InitFromBuffer(const byte_t* p, ui32_t l)
 
 //
 ASDCP::Result_t
-ASDCP::MXF::Primer::WriteToFile(ASDCP::FileWriter& Writer)
+ASDCP::MXF::Primer::WriteToFile(Kumu::FileWriter& Writer)
 {
   ASDCP::FrameBuffer Buffer;
   Result_t result = Buffer.Capacity(128*1024);
@@ -409,12 +469,12 @@ ASDCP::Result_t
 ASDCP::MXF::Primer::WriteToBuffer(ASDCP::FrameBuffer& Buffer)
 {
   ASDCP::FrameBuffer LocalTagBuffer;
-  MemIOWriter MemWRT(Buffer.Data() + kl_length, Buffer.Capacity() - kl_length);
-  Result_t result = LocalTagEntryBatch.Archive(MemWRT);
+  Kumu::MemIOWriter MemWRT(Buffer.Data() + kl_length, Buffer.Capacity() - kl_length);
+  Result_t result = LocalTagEntryBatch.Archive(&MemWRT) ? RESULT_OK : RESULT_KLV_CODING;
 
   if ( ASDCP_SUCCESS(result) )
     {
-      ui32_t packet_length = MemWRT.Size();
+      ui32_t packet_length = MemWRT.Length();
       result = WriteKLToBuffer(Buffer, Dict::ul(MDD_Primer), packet_length);
 
       if ( ASDCP_SUCCESS(result) )
@@ -490,18 +550,16 @@ ASDCP::MXF::Primer::Dump(FILE* stream)
     stream = stderr;
 
   KLVPacket::Dump(stream, false);
-  fprintf(stream, "Primer: %lu %s\n",
-         LocalTagEntryBatch.size(),
+  fprintf(stream, "Primer: %u %s\n",
+         (ui32_t)LocalTagEntryBatch.size(),
          ( LocalTagEntryBatch.size() == 1 ? "entry" : "entries" ));
   
   Batch<LocalTagEntry>::iterator i = LocalTagEntryBatch.begin();
   for ( ; i != LocalTagEntryBatch.end(); i++ )
     {
       const MDDEntry* Entry = Dict::FindUL((*i).UL.Value());
-      fprintf(stream, "  %s %s\n", (*i).ToString(identbuf), (Entry ? Entry->name : "Unknown"));
+      fprintf(stream, "  %s %s\n", (*i).EncodeString(identbuf, IdentBufferLen), (Entry ? Entry->name : "Unknown"));
     }
-
-  fputs("==========================================================================\n", stream);
 }
 
 
@@ -568,13 +626,13 @@ ASDCP::MXF::Preface::Dump(FILE* stream)
     stream = stderr;
 
   InterchangeObject::Dump(stream);
-  fprintf(stream, "  %22s = %s\n",  "LastModifiedDate", LastModifiedDate.ToString(identbuf));
+  fprintf(stream, "  %22s = %s\n",  "LastModifiedDate", LastModifiedDate.EncodeString(identbuf, IdentBufferLen));
   fprintf(stream, "  %22s = %hu\n", "Version", Version);
-  fprintf(stream, "  %22s = %lu\n", "ObjectModelVersion", ObjectModelVersion);
-  fprintf(stream, "  %22s = %s\n",  "PrimaryPackage", PrimaryPackage.ToString(identbuf));
+  fprintf(stream, "  %22s = %u\n",  "ObjectModelVersion", ObjectModelVersion);
+  fprintf(stream, "  %22s = %s\n",  "PrimaryPackage", PrimaryPackage.EncodeHex(identbuf, IdentBufferLen));
   fprintf(stream, "  %22s:\n", "Identifications");  Identifications.Dump(stream);
-  fprintf(stream, "  %22s = %s\n",  "ContentStorage", ContentStorage.ToString(identbuf));
-  fprintf(stream, "  %22s = %s\n",  "OperationalPattern", OperationalPattern.ToString(identbuf));
+  fprintf(stream, "  %22s = %s\n",  "ContentStorage", ContentStorage.EncodeHex(identbuf, IdentBufferLen));
+  fprintf(stream, "  %22s = %s\n",  "OperationalPattern", OperationalPattern.EncodeString(identbuf, IdentBufferLen));
   fprintf(stream, "  %22s:\n", "EssenceContainers");  EssenceContainers.Dump(stream);
   fprintf(stream, "  %22s:\n", "DMSchemes");  DMSchemes.Dump(stream);
 }
@@ -587,7 +645,7 @@ ASDCP::MXF::OPAtomHeader::~OPAtomHeader() {}
 
 //
 ASDCP::Result_t
-ASDCP::MXF::OPAtomHeader::InitFromFile(const ASDCP::FileReader& Reader)
+ASDCP::MXF::OPAtomHeader::InitFromFile(const Kumu::FileReader& Reader)
 {
   m_HasRIP = false;
   Result_t result = SeekToRIP(Reader);
@@ -607,27 +665,22 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const ASDCP::FileReader& Reader)
          DefaultLogSink().Error("RIP contains no Pairs.\n");
          result = RESULT_FORMAT;
        }
-      else if ( test_s < 2 || test_s > 3 )
-       {
-         // OP-Atom states that there will be either two or three partitions,
-         // one closed header and one closed footer with an optional body
-         DefaultLogSink().Error("RIP count is not 2 or 3: %lu\n", test_s);
-         return RESULT_FORMAT;
-       }
       else
        {
-         m_HasRIP = true;
-       }
-    }
+         if ( test_s < 2 || test_s > 3 )
+           {
+             // OP-Atom states that there will be either two or three partitions:
+             // one closed header and one closed footer with an optional body
+             DefaultLogSink().Warn("RIP count is not 2 or 3: %u\n", test_s);
+           }
 
-  if ( ASDCP_SUCCESS(result) )
-    {
-      Array<RIP::Pair>::iterator r_i = m_RIP.PairArray.begin();
+         m_HasRIP = true;
       
-      if ( (*r_i).ByteOffset !=  0 )
-       {
-         DefaultLogSink().Error("First Partition in RIP is not at offset 0.\n");
-         result = RESULT_FORMAT;
+         if ( m_RIP.PairArray.front().ByteOffset !=  0 )
+           {
+             DefaultLogSink().Error("First Partition in RIP is not at offset 0.\n");
+             result = RESULT_FORMAT;
+           }
        }
     }
 
@@ -637,38 +690,42 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const ASDCP::FileReader& Reader)
   if ( ASDCP_SUCCESS(result) )
     result = Partition::InitFromFile(Reader); // test UL and OP
 
+  if ( ASDCP_FAILURE(result) )
+    return result;
+
   // is it really OP-Atom?
   UL OPAtomUL(Dict::ul(MDD_OPAtom));
+  UL InteropOPAtomUL(Dict::ul(MDD_MXFInterop_OPAtom));
 
-  if ( ! ( OperationalPattern == OPAtomUL ) )
+  if ( ! ( OperationalPattern == OPAtomUL  || OperationalPattern == InteropOPAtomUL ) )
     {
-      char strbuf[IntBufferLen];
-      const MDDEntry* Entry = Dict::FindUL(m_KeyStart);
+      char strbuf[IdentBufferLen];
+      const MDDEntry* Entry = Dict::FindUL(OperationalPattern.Value());
       if ( Entry == 0 )
-       DefaultLogSink().Warn("Operational pattern is not OP-Atom: %s.\n", OperationalPattern.ToString(strbuf));
+       DefaultLogSink().Warn("Operational pattern is not OP-Atom: %s\n", OperationalPattern.EncodeString(strbuf, IdentBufferLen));
       else
-       DefaultLogSink().Warn("Operational pattern is not OP-Atom: %s.\n", Entry->name);
+       DefaultLogSink().Warn("Operational pattern is not OP-Atom: %s\n", Entry->name);
     }
 
   // slurp up the remainder of the header
-  if ( ASDCP_SUCCESS(result) )
-    {
-      if ( HeaderByteCount < 1024 )
-       DefaultLogSink().Warn("Improbably small HeaderByteCount value: %lu\n", HeaderByteCount);
+  if ( HeaderByteCount < 1024 )
+    DefaultLogSink().Warn("Improbably small HeaderByteCount value: %u\n", HeaderByteCount);
 
-      result = m_Buffer.Capacity(HeaderByteCount);
-    }
+  result = m_Buffer.Capacity(HeaderByteCount);
 
   if ( ASDCP_SUCCESS(result) )
     {
       ui32_t read_count;
       result = Reader.Read(m_Buffer.Data(), m_Buffer.Capacity(), &read_count);
 
-      if ( ASDCP_SUCCESS(result) && read_count != m_Buffer.Capacity() )
+      if ( ASDCP_FAILURE(result) )
+       return result;
+
+      if ( read_count != m_Buffer.Capacity() )
        {
-         DefaultLogSink().Error("Short read of OP-Atom header metadata; wanted %lu, got %lu\n",
+         DefaultLogSink().Error("Short read of OP-Atom header metadata; wanted %u, got %u\n",
                                 m_Buffer.Capacity(), read_count);
-         return RESULT_FAIL;
+         return RESULT_KLV_CODING;
        }
     }
 
@@ -698,14 +755,15 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const ASDCP::FileReader& Reader)
              delete object;
              result = m_Primer.InitFromBuffer(redo_p, end_p - redo_p);
            }
-         else if ( object->IsA(Dict::ul(MDD_Preface)) )
-           {
-             assert(m_Preface == 0);
-             m_Preface = (Preface*)object;
-           }
-         else  
+         else
            {
              m_PacketList->AddPacket(object);
+
+             if ( object->IsA(Dict::ul(MDD_Preface)) )
+               {
+                 assert(m_Preface == 0);
+                 m_Preface = (Preface*)object;
+               }
            }
        }
       else
@@ -718,6 +776,12 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const ASDCP::FileReader& Reader)
   return result;
 }
 
+ASDCP::Result_t
+ASDCP::MXF::OPAtomHeader::GetMDObjectByID(const UUID& ObjectID, InterchangeObject** Object)
+{
+  return m_PacketList->GetMDObjectByID(ObjectID, Object);
+}
+
 //
 ASDCP::Result_t
 ASDCP::MXF::OPAtomHeader::GetMDObjectByType(const byte_t* ObjectID, InterchangeObject** Object)
@@ -730,6 +794,13 @@ ASDCP::MXF::OPAtomHeader::GetMDObjectByType(const byte_t* ObjectID, InterchangeO
   return m_PacketList->GetMDObjectByType(ObjectID, Object);
 }
 
+//
+ASDCP::Result_t
+ASDCP::MXF::OPAtomHeader::GetMDObjectsByType(const byte_t* ObjectID, std::list<InterchangeObject*>& ObjectList)
+{
+  return m_PacketList->GetMDObjectsByType(ObjectID, ObjectList);
+}
+
 //
 ASDCP::MXF::Identification*
 ASDCP::MXF::OPAtomHeader::GetIdentification()
@@ -757,14 +828,14 @@ ASDCP::MXF::OPAtomHeader::GetSourcePackage()
 
 //
 ASDCP::Result_t
-ASDCP::MXF::OPAtomHeader::WriteToFile(ASDCP::FileWriter& Writer, ui32_t HeaderSize)
+ASDCP::MXF::OPAtomHeader::WriteToFile(Kumu::FileWriter& Writer, ui32_t HeaderSize)
 {
   if ( m_Preface == 0 )
     return RESULT_STATE;
 
   if ( HeaderSize < 4096 ) 
     {
-      DefaultLogSink().Error("HeaderSize %lu is too small. Must be >= 4096\n", HeaderSize);
+      DefaultLogSink().Error("HeaderSize %u is too small. Must be >= 4096\n", HeaderSize);
       return RESULT_FAIL;
     }
 
@@ -805,12 +876,12 @@ ASDCP::MXF::OPAtomHeader::WriteToFile(ASDCP::FileWriter& Writer, ui32_t HeaderSi
   // KLV Fill
   if ( ASDCP_SUCCESS(result) )
     {
-      ASDCP::fpos_t pos = Writer.Tell();
+      Kumu::fpos_t pos = Writer.Tell();
 
-      if ( pos > (ASDCP::fpos_t)HeaderByteCount )
+      if ( pos > (Kumu::fpos_t)HeaderByteCount )
        {
          char intbuf[IntBufferLen];
-         DefaultLogSink().Error("Header size %s exceeds specified value %lu\n",
+         DefaultLogSink().Error("Header size %s exceeds specified value %u\n",
                                 ui64sz(pos, intbuf),
                                 HeaderSize);
          return RESULT_FAIL;
@@ -850,16 +921,11 @@ ASDCP::MXF::OPAtomHeader::Dump(FILE* stream)
   if ( stream == 0 )
     stream = stderr;
 
-  if ( m_HasRIP )
-    m_RIP.Dump(stream);
-
   Partition::Dump(stream);
   m_Primer.Dump(stream);
 
   if ( m_Preface == 0 )
     fputs("No Preface loaded\n", stream);
-  else
-    m_Preface->Dump(stream);
 
   std::list<InterchangeObject*>::iterator i = m_PacketList->m_List.begin();
   for ( ; i != m_PacketList->m_List.end(); i++ )
@@ -881,7 +947,7 @@ ASDCP::MXF::OPAtomIndexFooter::~OPAtomIndexFooter() {}
 
 
 ASDCP::Result_t
-ASDCP::MXF::OPAtomIndexFooter::InitFromFile(const ASDCP::FileReader& Reader)
+ASDCP::MXF::OPAtomIndexFooter::InitFromFile(const Kumu::FileReader& Reader)
 {
   Result_t result = Partition::InitFromFile(Reader); // test UL and OP
 
@@ -896,7 +962,7 @@ ASDCP::MXF::OPAtomIndexFooter::InitFromFile(const ASDCP::FileReader& Reader)
 
   if ( ASDCP_SUCCESS(result) && read_count != m_Buffer.Capacity() )
     {
-      DefaultLogSink().Error("Short read of footer partition: got %lu, expecting %lu\n",
+      DefaultLogSink().Error("Short read of footer partition: got %u, expecting %u\n",
                             read_count, m_Buffer.Capacity());
       return RESULT_FAIL;
     }
@@ -933,7 +999,7 @@ ASDCP::MXF::OPAtomIndexFooter::InitFromFile(const ASDCP::FileReader& Reader)
 
 //
 ASDCP::Result_t
-ASDCP::MXF::OPAtomIndexFooter::WriteToFile(ASDCP::FileWriter& Writer, ui64_t duration)
+ASDCP::MXF::OPAtomIndexFooter::WriteToFile(Kumu::FileWriter& Writer, ui64_t duration)
 {
   ASDCP::FrameBuffer FooterBuffer;
   ui32_t   footer_size = m_PacketList->m_List.size() * MaxIndexSegmentSize; // segment-count * max-segment-size
@@ -982,8 +1048,8 @@ ASDCP::MXF::OPAtomIndexFooter::WriteToFile(ASDCP::FileWriter& Writer, ui64_t dur
 
   if ( ASDCP_SUCCESS(result) )
     {
-      ui32_t write_count;
-      Writer.Write(FooterBuffer.RoData(), FooterBuffer.Size(), &write_count);
+      ui32_t write_count = 0;
+      result = Writer.Write(FooterBuffer.RoData(), FooterBuffer.Size(), &write_count);
       assert(write_count == FooterBuffer.Size());
     }
 
@@ -1006,7 +1072,7 @@ ASDCP::MXF::OPAtomIndexFooter::Dump(FILE* stream)
 
 //
 ASDCP::Result_t
-ASDCP::MXF::OPAtomIndexFooter::Lookup(ui32_t frame_num, IndexTableSegment::IndexEntry& Entry)
+ASDCP::MXF::OPAtomIndexFooter::Lookup(ui32_t frame_num, IndexTableSegment::IndexEntry& Entry) const
 {
   std::list<InterchangeObject*>::iterator li;
   for ( li = m_PacketList->m_List.begin(); li != m_PacketList->m_List.end(); li++ )
@@ -1056,7 +1122,7 @@ ASDCP::MXF::OPAtomIndexFooter::SetIndexParamsCBR(IPrimerLookup* lookup, ui32_t s
 
 //
 void
-ASDCP::MXF::OPAtomIndexFooter::SetIndexParamsVBR(IPrimerLookup* lookup, const Rational& Rate, fpos_t offset)
+ASDCP::MXF::OPAtomIndexFooter::SetIndexParamsVBR(IPrimerLookup* lookup, const Rational& Rate, Kumu::fpos_t offset)
 {
   assert(lookup);
   m_Lookup = lookup;
@@ -1085,7 +1151,7 @@ ASDCP::MXF::OPAtomIndexFooter::PushIndexEntry(const IndexTableSegment::IndexEntr
       m_CurrentSegment->IndexEditRate = m_EditRate;
       m_CurrentSegment->IndexStartPosition = 0;
     }
-  else if ( m_CurrentSegment->IndexEntryArray.size() >= 1486 ) // 1486 gets us 16K packets
+  else if ( m_CurrentSegment->IndexEntryArray.size() >= CBRIndexEntriesPerSegment )
     { // no, this one is full, start another
       m_CurrentSegment->IndexDuration = m_CurrentSegment->IndexEntryArray.size();
       ui64_t StartPosition = m_CurrentSegment->IndexStartPosition + m_CurrentSegment->IndexDuration;
@@ -1129,7 +1195,7 @@ ASDCP::Result_t
 ASDCP::MXF::InterchangeObject::InitFromBuffer(const byte_t* p, ui32_t l)
 {
   ASDCP_TEST_NULL(p);
-  Result_t result;
+  Result_t result = RESULT_FALSE;
 
   if ( m_Typeinfo == 0 )
     {
@@ -1161,7 +1227,7 @@ ASDCP::MXF::InterchangeObject::WriteToBuffer(ASDCP::FrameBuffer& Buffer)
 
   if ( ASDCP_SUCCESS(result) )
     {
-      ui32_t packet_length = MemWRT.Size();
+      ui32_t packet_length = MemWRT.Length();
       result = WriteKLToBuffer(Buffer, m_Typeinfo->ul, packet_length);
 
       if ( ASDCP_SUCCESS(result) )
@@ -1179,8 +1245,8 @@ ASDCP::MXF::InterchangeObject::Dump(FILE* stream)
 
   fputc('\n', stream);
   KLVPacket::Dump(stream, false);
-  fprintf(stream, "             InstanceUID = %s\n",  InstanceUID.ToString(identbuf));
-  fprintf(stream, "           GenerationUID = %s\n",  GenerationUID.ToString(identbuf));
+  fprintf(stream, "             InstanceUID = %s\n",  InstanceUID.EncodeHex(identbuf, IdentBufferLen));
+  fprintf(stream, "           GenerationUID = %s\n",  GenerationUID.EncodeHex(identbuf, IdentBufferLen));
 }
 
 //
@@ -1194,6 +1260,84 @@ ASDCP::MXF::InterchangeObject::IsA(const byte_t* label)
 }
 
 
+//------------------------------------------------------------------------------------------
+
+
+typedef std::map<ASDCP::UL, ASDCP::MXF::MXFObjectFactory_t>FactoryMap_t;
+typedef FactoryMap_t::iterator FLi_t;
+
+//
+class FactoryList : public FactoryMap_t
+{
+  Kumu::Mutex m_Lock;
+
+public:
+  FactoryList() {}
+  ~FactoryList() {}
+
+  bool Empty() {
+    Kumu::AutoMutex BlockLock(m_Lock);
+    return empty();
+  }
+
+  FLi_t Find(const byte_t* label) {
+    Kumu::AutoMutex BlockLock(m_Lock);
+    return find(label);
+  }
+
+  FLi_t End() {
+    Kumu::AutoMutex BlockLock(m_Lock);
+    return end();
+  }
+
+  void Insert(ASDCP::UL label, ASDCP::MXF::MXFObjectFactory_t factory) {
+    Kumu::AutoMutex BlockLock(m_Lock);
+    insert(FactoryList::value_type(label, factory));
+  }
+};
+
+//
+static FactoryList s_FactoryList;
+static Kumu::Mutex s_InitLock;
+static bool        s_TypesInit = false;
+
+
+//
+void
+ASDCP::MXF::SetObjectFactory(ASDCP::UL label, ASDCP::MXF::MXFObjectFactory_t factory)
+{
+  s_FactoryList.Insert(label, factory);
+}
+
+
+//
+ASDCP::MXF::InterchangeObject*
+ASDCP::MXF::CreateObject(const byte_t* label)
+{
+  if ( label == 0 )
+    return 0;
+
+  if ( ! s_TypesInit )
+    {
+      Kumu::AutoMutex BlockLock(s_InitLock);
+
+      if ( ! s_TypesInit )
+       {
+         MXF::Metadata_InitTypes();
+         s_TypesInit = true;
+       }
+    }
+
+  FLi_t i = s_FactoryList.find(label);
+
+  if ( i == s_FactoryList.end() )
+    return new InterchangeObject;
+
+  return i->second();
+}
+
+
+
 //
 // end MXF.cpp
 //