make way for MCA
[asdcplib.git] / src / MXF.cpp
index fb32e8af2a31800a75cf0cf4c9a7c80380b38801..d1dd6d01808cca2b1efeb0da7442a2c29f1420b7 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005-2007, John Hurst
+Copyright (c) 2005-2012, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -30,8 +30,11 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
 #include "MXF.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.
@@ -89,11 +92,29 @@ ASDCP::MXF::SeekToRIP(const Kumu::FileReader& Reader)
   return result;
 }
 
+//
+ASDCP::Result_t
+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));
+  assert(m_Dict);
+  Result_t result = KLVFilePacket::InitFromFile(Reader, m_Dict->ul(MDD_RandomIndexMetadata));
 
   if ( ASDCP_SUCCESS(result) )
     {
@@ -111,12 +132,13 @@ ASDCP::MXF::RIP::InitFromFile(const Kumu::FileReader& Reader)
 ASDCP::Result_t
 ASDCP::MXF::RIP::WriteToFile(Kumu::FileWriter& Writer)
 {
+  assert(m_Dict);
   ASDCP::FrameBuffer Buffer;
   ui32_t RIPSize = ( PairArray.size() * (sizeof(ui32_t) + sizeof(ui64_t)) ) + 4;
   Result_t result = Buffer.Capacity(RIPSize);
 
   if ( ASDCP_SUCCESS(result) )
-    result = WriteKLToFile(Writer, Dict::ul(MDD_RandomIndexMetadata), RIPSize);
+    result = WriteKLToFile(Writer, m_Dict->ul(MDD_RandomIndexMetadata), RIPSize);
 
   if ( ASDCP_SUCCESS(result) )
     {
@@ -144,10 +166,8 @@ ASDCP::MXF::RIP::Dump(FILE* stream)
   if ( stream == 0 )
     stream = stderr;
 
-  KLVFilePacket::Dump(stream, false);
+  KLVFilePacket::Dump(stream, *m_Dict, false);
   PairArray.Dump(stream, false);
-
-  fputs("==========================================================================\n", stream);
 }
 
 //------------------------------------------------------------------------------------------
@@ -169,13 +189,30 @@ public:
   }
 
   //
-  void AddPacket(InterchangeObject* ThePacket)
+  void AddPacket(InterchangeObject* ThePacket) // takes ownership
   {
     assert(ThePacket);
     m_Map.insert(std::map<UUID, InterchangeObject*>::value_type(ThePacket->InstanceUID, ThePacket));
     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)
   {
@@ -195,13 +232,29 @@ 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;
+  }
 };
 
 //------------------------------------------------------------------------------------------
 //
 
 
-ASDCP::MXF::Partition::Partition() :
+ASDCP::MXF::Partition::Partition(const Dictionary*& d) :
+  m_Dict(d),
   MajorVersion(1), MinorVersion(2),
   KAGSize(1), ThisPartition(0), PreviousPartition(0),
   FooterPartition(0), HeaderByteCount(0), IndexByteCount(0), IndexSID(0),
@@ -214,7 +267,7 @@ ASDCP::MXF::Partition::~Partition()
 {
 }
 
-//
+// takes ownership
 void
 ASDCP::MXF::Partition::AddChildObject(InterchangeObject* Object)
 {
@@ -233,27 +286,33 @@ ASDCP::MXF::Partition::InitFromFile(const Kumu::FileReader& Reader)
   Result_t result = KLVFilePacket::InitFromFile(Reader);
   // test the UL
   // could be one of several values
-
   if ( ASDCP_SUCCESS(result) )
-    {
-      Kumu::MemIOReader MemRDR(m_ValueStart, m_ValueLength);
-      result = RESULT_KLV_CODING;
+    result = ASDCP::MXF::Partition::InitFromBuffer(m_ValueStart, m_ValueLength);
+  
+  return result;
+}
 
-      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;
-    }
+//
+ASDCP::Result_t
+ASDCP::MXF::Partition::InitFromBuffer(const byte_t* p, ui32_t l)
+{
+  Kumu::MemIOReader MemRDR(p, l);
+  Result_t 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) )
     DefaultLogSink().Error("Failed to initialize Partition\n");
@@ -327,7 +386,7 @@ ASDCP::MXF::Partition::Dump(FILE* stream)
   if ( stream == 0 )
     stream = stderr;
 
-  KLVFilePacket::Dump(stream, false);
+  KLVFilePacket::Dump(stream, *m_Dict, false);
   fprintf(stream, "  MajorVersion       = %hu\n", MajorVersion);
   fprintf(stream, "  MinorVersion       = %hu\n", MinorVersion);
   fprintf(stream, "  KAGSize            = %u\n",  KAGSize);
@@ -340,9 +399,7 @@ ASDCP::MXF::Partition::Dump(FILE* stream)
   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);
+  fputs("Essence Containers:\n", stream); EssenceContainers.Dump(stream);
 }
 
 
@@ -363,7 +420,7 @@ public:
 
 
 //
-ASDCP::MXF::Primer::Primer() : m_LocalTag(0xff) {}
+ASDCP::MXF::Primer::Primer(const Dictionary*& d) : m_LocalTag(0xff), m_Dict(d) {}
 
 //
 ASDCP::MXF::Primer::~Primer() {}
@@ -380,7 +437,8 @@ ASDCP::MXF::Primer::ClearTagList()
 ASDCP::Result_t
 ASDCP::MXF::Primer::InitFromBuffer(const byte_t* p, ui32_t l)
 {
-  Result_t result = KLVPacket::InitFromBuffer(p, l, Dict::ul(MDD_Primer));
+  assert(m_Dict);
+  Result_t result = KLVPacket::InitFromBuffer(p, l, m_Dict->ul(MDD_Primer));
 
   if ( ASDCP_SUCCESS(result) )
     {
@@ -420,6 +478,7 @@ ASDCP::MXF::Primer::WriteToFile(Kumu::FileWriter& Writer)
 ASDCP::Result_t
 ASDCP::MXF::Primer::WriteToBuffer(ASDCP::FrameBuffer& Buffer)
 {
+  assert(m_Dict);
   ASDCP::FrameBuffer LocalTagBuffer;
   Kumu::MemIOWriter MemWRT(Buffer.Data() + kl_length, Buffer.Capacity() - kl_length);
   Result_t result = LocalTagEntryBatch.Archive(&MemWRT) ? RESULT_OK : RESULT_KLV_CODING;
@@ -427,7 +486,7 @@ ASDCP::MXF::Primer::WriteToBuffer(ASDCP::FrameBuffer& Buffer)
   if ( ASDCP_SUCCESS(result) )
     {
       ui32_t packet_length = MemWRT.Length();
-      result = WriteKLToBuffer(Buffer, Dict::ul(MDD_Primer), packet_length);
+      result = WriteKLToBuffer(Buffer, m_Dict->ul(MDD_Primer), packet_length);
 
       if ( ASDCP_SUCCESS(result) )
        Buffer.Size(Buffer.Size() + packet_length);
@@ -496,24 +555,23 @@ ASDCP::MXF::Primer::TagForKey(const ASDCP::UL& Key, ASDCP::TagValue& Tag)
 void
 ASDCP::MXF::Primer::Dump(FILE* stream)
 {
+  assert(m_Dict);
   char identbuf[IdentBufferLen];
 
   if ( stream == 0 )
     stream = stderr;
 
-  KLVPacket::Dump(stream, false);
+  KLVPacket::Dump(stream, *m_Dict, false);
   fprintf(stream, "Primer: %u %s\n",
-         LocalTagEntryBatch.size(),
+         (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());
+      const MDDEntry* Entry = m_Dict->FindUL((*i).UL.Value());
       fprintf(stream, "  %s %s\n", (*i).EncodeString(identbuf, IdentBufferLen), (Entry ? Entry->name : "Unknown"));
     }
-
-  fputs("==========================================================================\n", stream);
 }
 
 
@@ -558,7 +616,8 @@ ASDCP::MXF::Preface::WriteToTLVSet(TLVWriter& TLVSet)
 ASDCP::Result_t
 ASDCP::MXF::Preface::InitFromBuffer(const byte_t* p, ui32_t l)
 {
-  m_Typeinfo = &Dict::Type(MDD_Preface);
+  assert(m_Dict);
+  m_Typeinfo = &(m_Dict->Type(MDD_Preface));
   return InterchangeObject::InitFromBuffer(p, l);
 }
 
@@ -566,7 +625,8 @@ ASDCP::MXF::Preface::InitFromBuffer(const byte_t* p, ui32_t l)
 ASDCP::Result_t
 ASDCP::MXF::Preface::WriteToBuffer(ASDCP::FrameBuffer& Buffer)
 {
-  m_Typeinfo = &Dict::Type(MDD_Preface);
+  assert(m_Dict);
+  m_Typeinfo = &(m_Dict->Type(MDD_Preface));
   return InterchangeObject::WriteToBuffer(Buffer);
 }
 
@@ -594,7 +654,7 @@ ASDCP::MXF::Preface::Dump(FILE* stream)
 //------------------------------------------------------------------------------------------
 //
 
-ASDCP::MXF::OPAtomHeader::OPAtomHeader() : m_Preface(0), m_HasRIP(false) {}
+ASDCP::MXF::OPAtomHeader::OPAtomHeader(const Dictionary*& d) : Partition(d), m_Dict(d), m_RIP(d), m_Primer(d), m_Preface(0), m_HasRIP(false) {}
 ASDCP::MXF::OPAtomHeader::~OPAtomHeader() {}
 
 //
@@ -619,15 +679,16 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const Kumu::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: %u\n", test_s);
-         return RESULT_FORMAT;
-       }
       else
        {
+         if ( test_s < 2 )
+           {
+             // OP-Atom states that there will be either two or three partitions:
+             // one closed header and one closed footer with an optional body
+             // SMPTE 429-5 files may have many partitions, see SMPTE 410M
+             DefaultLogSink().Warn("RIP count is less than 2: %u\n", test_s);
+           }
+
          m_HasRIP = true;
       
          if ( m_RIP.PairArray.front().ByteOffset !=  0 )
@@ -648,15 +709,27 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const Kumu::FileReader& Reader)
     return result;
 
   // is it really OP-Atom?
-  UL OPAtomUL(Dict::ul(MDD_OPAtom));
-  UL InteropOPAtomUL(Dict::ul(MDD_MXFInterop_OPAtom));
+  assert(m_Dict);
+  UL OPAtomUL(SMPTE_390_OPAtom_Entry().ul);
+  UL InteropOPAtomUL(MXFInterop_OPAtom_Entry().ul);
 
-  if ( ! ( OperationalPattern == OPAtomUL  || OperationalPattern == InteropOPAtomUL ) )
+  if ( OperationalPattern.ExactMatch(OPAtomUL) ) // SMPTE
+    {
+      if ( m_Dict == &DefaultCompositeDict() )
+       m_Dict = &DefaultSMPTEDict();
+    }
+  else if ( OperationalPattern.ExactMatch(InteropOPAtomUL) ) // Interop
+    {
+      if ( m_Dict == &DefaultCompositeDict() )
+       m_Dict = &DefaultInteropDict();
+    }
+  else
     {
       char strbuf[IdentBufferLen];
-      const MDDEntry* Entry = Dict::FindUL(OperationalPattern.Value());
+      const MDDEntry* Entry = m_Dict->FindUL(OperationalPattern.Value());
       if ( Entry == 0 )
-       DefaultLogSink().Warn("Operational pattern is not OP-Atom: %s\n", OperationalPattern.EncodeString(strbuf, IdentBufferLen));
+       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);
     }
@@ -665,7 +738,8 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const Kumu::FileReader& Reader)
   if ( HeaderByteCount < 1024 )
     DefaultLogSink().Warn("Improbably small HeaderByteCount value: %u\n", HeaderByteCount);
 
-  result = m_Buffer.Capacity(HeaderByteCount);
+  assert (HeaderByteCount <= 0xFFFFFFFFL);
+  result = m_Buffer.Capacity((ui32_t) HeaderByteCount);
 
   if ( ASDCP_SUCCESS(result) )
     {
@@ -683,13 +757,42 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const Kumu::FileReader& Reader)
        }
     }
 
-  const byte_t* p = m_Buffer.RoData();
-  const byte_t* end_p = p + m_Buffer.Capacity();
+  if ( ASDCP_SUCCESS(result) )
+    result = InitFromBuffer(m_Buffer.RoData(), m_Buffer.Capacity());
+
+  return result;
+}
+
+//
+ASDCP::Result_t
+ASDCP::MXF::OPAtomHeader::InitFromPartitionBuffer(const byte_t* p, ui32_t l)
+{
+  Result_t result = KLVPacket::InitFromBuffer(p, l);
+
+  if ( ASDCP_SUCCESS(result) )
+    result = Partition::InitFromBuffer(m_ValueStart, m_ValueLength); // test UL and OP
+
+  if ( ASDCP_SUCCESS(result) )
+    {
+      ui32_t pp_len = KLVPacket::PacketLength();
+      result = InitFromBuffer(p + pp_len, l - pp_len);
+    }
+
+  return result;
+}
+
+//
+ASDCP::Result_t
+ASDCP::MXF::OPAtomHeader::InitFromBuffer(const byte_t* p, ui32_t l)
+{
+  assert(m_Dict);
+  Result_t result = RESULT_OK;
+  const byte_t* end_p = p + l;
 
   while ( ASDCP_SUCCESS(result) && p < end_p )
     {
       // parse the packets and index them by uid, discard KLVFill items
-      InterchangeObject* object = CreateObject(p);
+      InterchangeObject* object = CreateObject(m_Dict, p);
       assert(object);
 
       object->m_Lookup = &m_Primer;
@@ -700,24 +803,21 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const Kumu::FileReader& Reader)
 
       if ( ASDCP_SUCCESS(result) )
        {
-         if ( object->IsA(Dict::ul(MDD_KLVFill)) )
+         if ( object->IsA(m_Dict->ul(MDD_KLVFill)) )
            {
              delete object;
            }
-         else if ( object->IsA(Dict::ul(MDD_Primer)) )
+         else if ( object->IsA(m_Dict->ul(MDD_Primer)) ) // TODO: only one primer should be found
            {
              delete object;
              result = m_Primer.InitFromBuffer(redo_p, end_p - redo_p);
            }
          else
            {
-             m_PacketList->AddPacket(object);
+             m_PacketList->AddPacket(object); // takes ownership
 
-             if ( object->IsA(Dict::ul(MDD_Preface)) )
-               {
-                 assert(m_Preface == 0);
-                 m_Preface = (Preface*)object;
-               }
+             if ( object->IsA(m_Dict->ul(MDD_Preface)) && m_Preface == 0 )
+               m_Preface = (Preface*)object;
            }
        }
       else
@@ -730,6 +830,12 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const Kumu::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)
@@ -742,6 +848,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()
@@ -771,6 +884,7 @@ ASDCP::MXF::OPAtomHeader::GetSourcePackage()
 ASDCP::Result_t
 ASDCP::MXF::OPAtomHeader::WriteToFile(Kumu::FileWriter& Writer, ui32_t HeaderSize)
 {
+  assert(m_Dict);
   if ( m_Preface == 0 )
     return RESULT_STATE;
 
@@ -782,7 +896,8 @@ ASDCP::MXF::OPAtomHeader::WriteToFile(Kumu::FileWriter& Writer, ui32_t HeaderSiz
 
   ASDCP::FrameBuffer HeaderBuffer;
   HeaderByteCount = HeaderSize - ArchiveSize();
-  Result_t result = HeaderBuffer.Capacity(HeaderByteCount); 
+  assert (HeaderByteCount <= 0xFFFFFFFFL);
+  Result_t result = HeaderBuffer.Capacity((ui32_t) HeaderByteCount); 
   m_Preface->m_Lookup = &m_Primer;
 
   std::list<InterchangeObject*>::iterator pl_i = m_PacketList->m_List.begin();
@@ -800,7 +915,7 @@ ASDCP::MXF::OPAtomHeader::WriteToFile(Kumu::FileWriter& Writer, ui32_t HeaderSiz
 
   if ( ASDCP_SUCCESS(result) )
     {
-      UL TmpUL(Dict::ul(MDD_ClosedCompleteHeader));
+      UL TmpUL(m_Dict->ul(MDD_ClosedCompleteHeader));
       result = Partition::WriteToFile(Writer, TmpUL);
     }
 
@@ -838,7 +953,7 @@ ASDCP::MXF::OPAtomHeader::WriteToFile(Kumu::FileWriter& Writer, ui32_t HeaderSiz
        }
 
       klv_fill_length -= kl_length;
-      result = WriteKLToFile(Writer, Dict::ul(MDD_KLVFill), klv_fill_length);
+      result = WriteKLToFile(Writer, m_Dict->ul(MDD_KLVFill), klv_fill_length);
 
       if ( ASDCP_SUCCESS(result) )
        result = NilBuf.Capacity(klv_fill_length);
@@ -862,16 +977,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 +991,8 @@ ASDCP::MXF::OPAtomHeader::Dump(FILE* stream)
 //------------------------------------------------------------------------------------------
 //
 
-ASDCP::MXF::OPAtomIndexFooter::OPAtomIndexFooter() :
+ASDCP::MXF::OPAtomIndexFooter::OPAtomIndexFooter(const Dictionary*& d) :
+  Partition(d), m_Dict(d),
   m_CurrentSegment(0), m_BytesPerEditUnit(0), m_BodySID(0),
   m_ECOffset(0), m_Lookup(0)
 {
@@ -891,7 +1002,7 @@ ASDCP::MXF::OPAtomIndexFooter::OPAtomIndexFooter() :
 
 ASDCP::MXF::OPAtomIndexFooter::~OPAtomIndexFooter() {}
 
-
+//
 ASDCP::Result_t
 ASDCP::MXF::OPAtomIndexFooter::InitFromFile(const Kumu::FileReader& Reader)
 {
@@ -901,7 +1012,10 @@ ASDCP::MXF::OPAtomIndexFooter::InitFromFile(const Kumu::FileReader& Reader)
   ui32_t read_count;
 
   if ( ASDCP_SUCCESS(result) )
-    result = m_Buffer.Capacity(IndexByteCount);
+    {
+      assert (IndexByteCount <= 0xFFFFFFFFL);
+      result = m_Buffer.Capacity((ui32_t) IndexByteCount);
+    }
 
   if ( ASDCP_SUCCESS(result) )
     result = Reader.Read(m_Buffer.Data(), m_Buffer.Capacity(), &read_count);
@@ -913,13 +1027,41 @@ ASDCP::MXF::OPAtomIndexFooter::InitFromFile(const Kumu::FileReader& Reader)
       return RESULT_FAIL;
     }
 
-  const byte_t* p = m_Buffer.RoData();
-  const byte_t* end_p = p + m_Buffer.Capacity();
+  if ( ASDCP_SUCCESS(result) )
+    result = InitFromBuffer(m_Buffer.RoData(), m_Buffer.Capacity());
+
+  return result;
+}
+
+//
+ASDCP::Result_t
+ASDCP::MXF::OPAtomIndexFooter::InitFromPartitionBuffer(const byte_t* p, ui32_t l)
+{
+  Result_t result = KLVPacket::InitFromBuffer(p, l);
+
+  if ( ASDCP_SUCCESS(result) )
+    result = Partition::InitFromBuffer(m_ValueStart, m_ValueLength); // test UL and OP
+
+  if ( ASDCP_SUCCESS(result) )
+    {
+      ui32_t pp_len = KLVPacket::PacketLength();
+      result = InitFromBuffer(p + pp_len, l - pp_len);
+    }
+
+  return result;
+}
+
+//
+ASDCP::Result_t
+ASDCP::MXF::OPAtomIndexFooter::InitFromBuffer(const byte_t* p, ui32_t l)
+{
+  Result_t result = RESULT_OK;
+  const byte_t* end_p = p + l;
   
   while ( ASDCP_SUCCESS(result) && p < end_p )
     {
       // parse the packets and index them by uid, discard KLVFill items
-      InterchangeObject* object = CreateObject(p);
+      InterchangeObject* object = CreateObject(m_Dict, p);
       assert(object);
 
       object->m_Lookup = m_Lookup;
@@ -928,7 +1070,7 @@ ASDCP::MXF::OPAtomIndexFooter::InitFromFile(const Kumu::FileReader& Reader)
 
       if ( ASDCP_SUCCESS(result) )
        {
-         m_PacketList->AddPacket(object);
+         m_PacketList->AddPacket(object); // takes ownership
        }
       else
        {
@@ -947,6 +1089,7 @@ ASDCP::MXF::OPAtomIndexFooter::InitFromFile(const Kumu::FileReader& Reader)
 ASDCP::Result_t
 ASDCP::MXF::OPAtomIndexFooter::WriteToFile(Kumu::FileWriter& Writer, ui64_t duration)
 {
+  assert(m_Dict);
   ASDCP::FrameBuffer FooterBuffer;
   ui32_t   footer_size = m_PacketList->m_List.size() * MaxIndexSegmentSize; // segment-count * max-segment-size
   Result_t result = FooterBuffer.Capacity(footer_size); 
@@ -988,14 +1131,14 @@ ASDCP::MXF::OPAtomIndexFooter::WriteToFile(Kumu::FileWriter& Writer, ui64_t dura
   if ( ASDCP_SUCCESS(result) )
     {
       IndexByteCount = FooterBuffer.Size();
-      UL FooterUL(Dict::ul(MDD_CompleteFooter));
+      UL FooterUL(m_Dict->ul(MDD_CompleteFooter));
       result = Partition::WriteToFile(Writer, FooterUL);
     }
 
   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());
     }
 
@@ -1018,7 +1161,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++ )
@@ -1042,7 +1185,9 @@ ASDCP::MXF::OPAtomIndexFooter::Lookup(ui32_t frame_num, IndexTableSegment::Index
          else if ( (ui64_t)frame_num >= start_pos
                    && (ui64_t)frame_num < (start_pos + Segment->IndexDuration) )
            {
-             Entry = Segment->IndexEntryArray[frame_num-start_pos];
+             ui64_t tmp = frame_num - start_pos;
+             assert(tmp <= 0xFFFFFFFFL);
+             Entry = Segment->IndexEntryArray[(ui32_t) tmp];
              return RESULT_OK;
            }
        }
@@ -1060,7 +1205,7 @@ ASDCP::MXF::OPAtomIndexFooter::SetIndexParamsCBR(IPrimerLookup* lookup, ui32_t s
   m_BytesPerEditUnit = size;
   m_EditRate = Rate;
 
-  IndexTableSegment* Index = new IndexTableSegment;
+  IndexTableSegment* Index = new IndexTableSegment(m_Dict);
   AddChildObject(Index);
   Index->EditUnitByteCount = m_BytesPerEditUnit;
   Index->IndexEditRate = Rate;
@@ -1090,7 +1235,7 @@ ASDCP::MXF::OPAtomIndexFooter::PushIndexEntry(const IndexTableSegment::IndexEntr
   // do we have an available segment?
   if ( m_CurrentSegment == 0 )
     { // no, set up a new segment
-      m_CurrentSegment = new IndexTableSegment;
+      m_CurrentSegment = new IndexTableSegment(m_Dict);
       assert(m_CurrentSegment);
       AddChildObject(m_CurrentSegment);
       m_CurrentSegment->DeltaEntryArray.push_back(IndexTableSegment::DeltaEntry());
@@ -1102,7 +1247,7 @@ ASDCP::MXF::OPAtomIndexFooter::PushIndexEntry(const IndexTableSegment::IndexEntr
       m_CurrentSegment->IndexDuration = m_CurrentSegment->IndexEntryArray.size();
       ui64_t StartPosition = m_CurrentSegment->IndexStartPosition + m_CurrentSegment->IndexDuration;
 
-      m_CurrentSegment = new IndexTableSegment;
+      m_CurrentSegment = new IndexTableSegment(m_Dict);
       assert(m_CurrentSegment);
       AddChildObject(m_CurrentSegment);
       m_CurrentSegment->DeltaEntryArray.push_back(IndexTableSegment::DeltaEntry());
@@ -1190,7 +1335,7 @@ ASDCP::MXF::InterchangeObject::Dump(FILE* stream)
   char identbuf[IdentBufferLen];
 
   fputc('\n', stream);
-  KLVPacket::Dump(stream, false);
+  KLVPacket::Dump(stream, *m_Dict, false);
   fprintf(stream, "             InstanceUID = %s\n",  InstanceUID.EncodeHex(identbuf, IdentBufferLen));
   fprintf(stream, "           GenerationUID = %s\n",  GenerationUID.EncodeHex(identbuf, IdentBufferLen));
 }
@@ -1206,6 +1351,79 @@ 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 Dictionary*& Dict, const UL& label)
+{
+  if ( ! s_TypesInit )
+    {
+      Kumu::AutoMutex BlockLock(s_InitLock);
+
+      if ( ! s_TypesInit )
+       {
+         MXF::Metadata_InitTypes(Dict);
+         s_TypesInit = true;
+       }
+    }
+
+  FLi_t i = s_FactoryList.find(label.Value());
+
+  if ( i == s_FactoryList.end() )
+    return new InterchangeObject(Dict);
+
+  return i->second(Dict);
+}
+
+
 //
 // end MXF.cpp
 //