new interface for in-memory metadata packets
[asdcplib.git] / src / MXF.cpp
index 168ee527d713997fe54307711d020177f7db6a87..cc09b1575cb604d7741007668b86839b0fc22c89 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005-2007, 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,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,6 +92,23 @@ 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)
@@ -146,8 +166,6 @@ ASDCP::MXF::RIP::Dump(FILE* stream)
 
   KLVFilePacket::Dump(stream, false);
   PairArray.Dump(stream, false);
-
-  fputs("==========================================================================\n", stream);
 }
 
 //------------------------------------------------------------------------------------------
@@ -176,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)
   {
@@ -195,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;
+  }
 };
 
 //------------------------------------------------------------------------------------------
@@ -233,27 +283,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");
@@ -341,8 +397,6 @@ ASDCP::MXF::Partition::Dump(FILE* stream)
   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);
 }
 
 
@@ -503,7 +557,7 @@ ASDCP::MXF::Primer::Dump(FILE* stream)
 
   KLVPacket::Dump(stream, false);
   fprintf(stream, "Primer: %u %s\n",
-         LocalTagEntryBatch.size(),
+         (ui32_t)LocalTagEntryBatch.size(),
          ( LocalTagEntryBatch.size() == 1 ? "entry" : "entries" ));
   
   Batch<LocalTagEntry>::iterator i = LocalTagEntryBatch.begin();
@@ -512,8 +566,6 @@ ASDCP::MXF::Primer::Dump(FILE* stream)
       const MDDEntry* Entry = Dict::FindUL((*i).UL.Value());
       fprintf(stream, "  %s %s\n", (*i).EncodeString(identbuf, IdentBufferLen), (Entry ? Entry->name : "Unknown"));
     }
-
-  fputs("==========================================================================\n", stream);
 }
 
 
@@ -619,15 +671,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 )
@@ -665,7 +718,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,8 +737,33 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const Kumu::FileReader& Reader)
        }
     }
 
-  const byte_t* p = m_Buffer.RoData();
-  const byte_t* end_p = p + m_Buffer.Capacity();
+  result = InitFromBuffer(m_Buffer.RoData(), m_Buffer.Capacity());
+}
+
+//
+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)
+{
+  Result_t result = RESULT_OK;
+  const byte_t* end_p = p + l;
 
   while ( ASDCP_SUCCESS(result) && p < end_p )
     {
@@ -709,14 +788,12 @@ ASDCP::MXF::OPAtomHeader::InitFromFile(const Kumu::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)) && m_Preface == 0 )
+               m_Preface = (Preface*)object;
            }
        }
       else
@@ -729,6 +806,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)
@@ -741,6 +824,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()
@@ -781,7 +871,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();
@@ -861,16 +952,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++ )
@@ -890,7 +976,7 @@ ASDCP::MXF::OPAtomIndexFooter::OPAtomIndexFooter() :
 
 ASDCP::MXF::OPAtomIndexFooter::~OPAtomIndexFooter() {}
 
-
+//
 ASDCP::Result_t
 ASDCP::MXF::OPAtomIndexFooter::InitFromFile(const Kumu::FileReader& Reader)
 {
@@ -900,7 +986,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);
@@ -912,8 +1001,36 @@ 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 )
     {
@@ -993,8 +1110,8 @@ ASDCP::MXF::OPAtomIndexFooter::WriteToFile(Kumu::FileWriter& Writer, ui64_t dura
 
   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());
     }
 
@@ -1017,7 +1134,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++ )
@@ -1041,7 +1158,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;
            }
        }
@@ -1205,6 +1324,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
 //