Fix crashes from AFL run
[asdcplib.git] / src / MXF.cpp
index f113444b4654a124b46ab183def9f8ba6475630d..df8bb2846cd5bf9c21250b7383129c51f3db7d9b 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005-2013, John Hurst
+Copyright (c) 2005-2015, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -100,20 +100,20 @@ ASDCP::MXF::SeekToRIP(const Kumu::FileReader& Reader)
 }
 
 //
-ASDCP::Result_t
-ASDCP::MXF::RIP::GetPairBySID(ui32_t SID, Pair& outPair) const
+bool
+ASDCP::MXF::RIP::GetPairBySID(ui32_t SID, PartitionPair& outPair) const
 {
-  Array<Pair>::const_iterator pi = PairArray.begin();
-  for ( ; pi != PairArray.end(); pi++ )
+  RIP::const_pair_iterator i;
+  for ( i = PairArray.begin(); i != PairArray.end(); ++i )
     {
-      if ( (*pi).BodySID == SID )
+      if ( i->BodySID == SID )
        {
-         outPair = *pi;
-         return RESULT_OK;
+         outPair = *i;
+         return true;
        }
     }
 
-  return RESULT_FAIL;
+  return false;
 }
 
 //
@@ -125,12 +125,17 @@ ASDCP::MXF::RIP::InitFromFile(const Kumu::FileReader& Reader)
 
   if ( ASDCP_SUCCESS(result) )
     {
+      if (m_ValueLength < 4)
+      {
+        DefaultLogSink().Error("RIP is too short.\n");
+        return RESULT_FAIL;
+      }
       Kumu::MemIOReader MemRDR(m_ValueStart, m_ValueLength - 4);
-      result = PairArray.Unarchive(&MemRDR) ? RESULT_OK : RESULT_KLV_CODING;
+      result = PairArray.Unarchive(&MemRDR) ? RESULT_OK : RESULT_KLV_CODING(__LINE__, __FILE__);
     }
 
   if ( ASDCP_FAILURE(result) )
-    DefaultLogSink().Error("Failed to initialize RIP\n");
+    DefaultLogSink().Error("Failed to initialize RIP.\n");
 
   return result;
 }
@@ -149,7 +154,7 @@ ASDCP::MXF::RIP::WriteToFile(Kumu::FileWriter& Writer)
 
   if ( ASDCP_SUCCESS(result) )
     {
-      result = RESULT_KLV_CODING;
+      result = RESULT_KLV_CODING(__LINE__, __FILE__);
 
       Kumu::MemIOWriter MemWRT(Buffer.Data(), Buffer.Capacity());
       if ( PairArray.Archive(&MemWRT) )
@@ -301,7 +306,7 @@ 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;
+  Result_t result = RESULT_KLV_CODING(__LINE__, __FILE__);
 
   if ( MemRDR.ReadUi16BE(&MajorVersion) )
     if ( MemRDR.ReadUi16BE(&MinorVersion) )
@@ -319,7 +324,7 @@ ASDCP::MXF::Partition::InitFromBuffer(const byte_t* p, ui32_t l)
                            result = RESULT_OK;
 
   if ( ASDCP_FAILURE(result) )
-    DefaultLogSink().Error("Failed to initialize Partition\n");
+    DefaultLogSink().Error("Failed to initialize Partition.\n");
 
   return result;
 }
@@ -334,7 +339,7 @@ ASDCP::MXF::Partition::WriteToFile(Kumu::FileWriter& Writer, UL& PartitionLabel)
   if ( ASDCP_SUCCESS(result) )
     {
       Kumu::MemIOWriter MemWRT(Buffer.Data(), Buffer.Capacity());
-      result = RESULT_KLV_CODING;
+      result = RESULT_KLV_CODING(__LINE__, __FILE__);
       if ( MemWRT.WriteUi16BE(MajorVersion) )
        if ( MemWRT.WriteUi16BE(MinorVersion) )
          if ( MemWRT.WriteUi32BE(KAGSize) )
@@ -448,8 +453,13 @@ ASDCP::MXF::Primer::InitFromBuffer(const byte_t* p, ui32_t l)
 
   if ( ASDCP_SUCCESS(result) )
     {
+      if (m_ValueStart + m_ValueLength > p + l)
+      {
+        DefaultLogSink().Error("Primer entry too long.\n");
+        return RESULT_FAIL;
+      }
       Kumu::MemIOReader MemRDR(m_ValueStart, m_ValueLength);
-      result = LocalTagEntryBatch.Unarchive(&MemRDR) ? RESULT_OK : RESULT_KLV_CODING;
+      result = LocalTagEntryBatch.Unarchive(&MemRDR) ? RESULT_OK : RESULT_KLV_CODING(__LINE__, __FILE__);
     }
 
   if ( ASDCP_SUCCESS(result) )
@@ -459,7 +469,7 @@ ASDCP::MXF::Primer::InitFromBuffer(const byte_t* p, ui32_t l)
     }
 
   if ( ASDCP_FAILURE(result) )
-    DefaultLogSink().Error("Failed to initialize Primer\n");
+    DefaultLogSink().Error("Failed to initialize Primer.\n");
 
   return result;
 }
@@ -487,7 +497,7 @@ 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;
+  Result_t result = LocalTagEntryBatch.Archive(&MemWRT) ? RESULT_OK : RESULT_KLV_CODING(__LINE__, __FILE__);
 
   if ( ASDCP_SUCCESS(result) )
     {
@@ -526,7 +536,7 @@ ASDCP::MXF::Primer::InsertTag(const MDDEntry& Entry, ASDCP::TagValue& Tag)
       TmpEntry.UL = TestUL;
       TmpEntry.Tag = Tag;
 
-      LocalTagEntryBatch.push_back(TmpEntry);
+      LocalTagEntryBatch.insert(TmpEntry);
       m_Lookup->insert(std::map<UL, TagValue>::value_type(TmpEntry.UL, TmpEntry.Tag));
     }
   else
@@ -575,7 +585,7 @@ ASDCP::MXF::Primer::Dump(FILE* stream)
   Batch<LocalTagEntry>::iterator i = LocalTagEntryBatch.begin();
   for ( ; i != LocalTagEntryBatch.end(); i++ )
     {
-      const MDDEntry* Entry = m_Dict->FindUL((*i).UL.Value());
+      const MDDEntry* Entry = m_Dict->FindULAnyVersion((*i).UL.Value());
       fprintf(stream, "  %s %s\n", (*i).EncodeString(identbuf, IdentBufferLen), (Entry ? Entry->name : "Unknown"));
     }
 }
@@ -608,6 +618,8 @@ ASDCP::MXF::Preface::Copy(const Preface& rhs)
   OperationalPattern = rhs.OperationalPattern;
   EssenceContainers = rhs.EssenceContainers;
   DMSchemes = rhs.DMSchemes;
+  ApplicationSchemes = rhs.ApplicationSchemes;
+  ConformsToSpecifications = rhs.ConformsToSpecifications;
 }
 
 //
@@ -624,6 +636,16 @@ ASDCP::MXF::Preface::InitFromTLVSet(TLVReader& TLVSet)
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(Preface, OperationalPattern));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(Preface, EssenceContainers));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(Preface, DMSchemes));
+  if ( ASDCP_SUCCESS(result) ) 
+    {
+      result = TLVSet.ReadObject(OBJ_READ_ARGS_OPT(Preface, ApplicationSchemes));
+      ApplicationSchemes.set_has_value( result == RESULT_OK);
+    }
+  if ( ASDCP_SUCCESS(result) )
+    {
+      result = TLVSet.ReadObject(OBJ_READ_ARGS_OPT(Preface, ConformsToSpecifications));
+      ConformsToSpecifications.set_has_value( result == RESULT_OK);
+    }
   return result;
 }
 
@@ -641,6 +663,14 @@ ASDCP::MXF::Preface::WriteToTLVSet(TLVWriter& TLVSet)
   if ( ASDCP_SUCCESS(result) )  result = TLVSet.WriteObject(OBJ_WRITE_ARGS(Preface, OperationalPattern));
   if ( ASDCP_SUCCESS(result) )  result = TLVSet.WriteObject(OBJ_WRITE_ARGS(Preface, EssenceContainers));
   if ( ASDCP_SUCCESS(result) )  result = TLVSet.WriteObject(OBJ_WRITE_ARGS(Preface, DMSchemes));
+  if ( ASDCP_SUCCESS(result) && !ApplicationSchemes.empty() )
+    {
+      result = TLVSet.WriteObject(OBJ_WRITE_ARGS_OPT(Preface, ApplicationSchemes));
+    }
+  if ( ASDCP_SUCCESS(result) && !ConformsToSpecifications.empty())
+    {
+      result = TLVSet.WriteObject(OBJ_WRITE_ARGS_OPT(Preface, ConformsToSpecifications));
+    }
   return result;
 }
 
@@ -682,6 +712,14 @@ ASDCP::MXF::Preface::Dump(FILE* stream)
   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);
+  if ( ! ApplicationSchemes.empty() )
+    {
+      fprintf(stream, "  %22s:\n", "ApplicationSchemes");  ApplicationSchemes.get().Dump(stream);
+    }
+  if ( ! ConformsToSpecifications.empty() )
+    {
+      fprintf(stream, "  %22s:\n", "ConformsToSpecifications");  ConformsToSpecifications.get().Dump(stream);
+    }
 }
 
 //------------------------------------------------------------------------------------------
@@ -702,18 +740,23 @@ ASDCP::MXF::OP1aHeader::InitFromFile(const Kumu::FileReader& Reader)
   if ( m_Dict == &DefaultCompositeDict() )
     {
       // select more explicit dictionary if one is available
-      if ( OperationalPattern.ExactMatch(MXFInterop_OPAtom_Entry().ul) )
+      if ( OperationalPattern.MatchExact(MXFInterop_OPAtom_Entry().ul) )
        {
          m_Dict = &DefaultInteropDict();
        }
-      else if ( OperationalPattern.ExactMatch(SMPTE_390_OPAtom_Entry().ul) )
+      else if ( OperationalPattern.MatchExact(SMPTE_390_OPAtom_Entry().ul) )
        {
          m_Dict = &DefaultSMPTEDict();
        }
     }
 
   // slurp up the remainder of the header
-  if ( HeaderByteCount < 1024 )
+  if ( HeaderByteCount == 0 )
+    {
+      DefaultLogSink().Warn("MXF file contents incomplete.\n");
+      return RESULT_KLV_CODING(__LINE__, __FILE__);
+    }
+  else if ( HeaderByteCount < 1024 )
     {
       DefaultLogSink().Warn("Improbably small HeaderByteCount value: %qu\n", HeaderByteCount);
     }
@@ -731,15 +774,15 @@ ASDCP::MXF::OP1aHeader::InitFromFile(const Kumu::FileReader& Reader)
 
       if ( ASDCP_FAILURE(result) )
         {
-         DefaultLogSink().Error("OP1aHeader::InitFromFile, Read failed\n");
+         DefaultLogSink().Error("OP1aHeader::InitFromFile, read failed.\n");
          return result;
         }
 
       if ( read_count != m_HeaderData.Capacity() )
        {
-         DefaultLogSink().Error("Short read of OP-Atom header metadata; wanted %u, got %u\n",
+         DefaultLogSink().Error("Short read of OP-Atom header metadata; wanted %u, got %u.\n",
                                 m_HeaderData.Capacity(), read_count);
-         return RESULT_KLV_CODING;
+         return RESULT_KLV_CODING(__LINE__, __FILE__);
        }
     }
 
@@ -783,9 +826,9 @@ ASDCP::MXF::OP1aHeader::InitFromBuffer(const byte_t* p, ui32_t l)
 
       object->m_Lookup = &m_Primer;
       result = object->InitFromBuffer(p, end_p - p);
+
       const byte_t* redo_p = p;
       p += object->PacketLength();
-      //      hexdump(p, object->PacketLength());
 
       if ( ASDCP_SUCCESS(result) )
        {
@@ -813,7 +856,8 @@ ASDCP::MXF::OP1aHeader::InitFromBuffer(const byte_t* p, ui32_t l)
        }
       else
        {
-         DefaultLogSink().Error("Error initializing packet\n");
+         DefaultLogSink().Error("Error initializing OP1a header packet.\n");
+         //      Kumu::hexdump(p-object->PacketLength(), object->PacketLength());
          delete object;
        }
     }
@@ -881,7 +925,7 @@ ASDCP::MXF::OP1aHeader::WriteToFile(Kumu::FileWriter& Writer, ui32_t HeaderSize)
   if ( HeaderSize < 4096 ) 
     {
       DefaultLogSink().Error("HeaderSize %u is too small. Must be >= 4096\n", HeaderSize);
-      return RESULT_FAIL;
+      return RESULT_PARAM;
     }
 
   ASDCP::FrameBuffer HeaderBuffer;
@@ -1074,13 +1118,15 @@ ASDCP::MXF::OPAtomIndexFooter::InitFromBuffer(const byte_t* p, ui32_t l)
        }
       else
        {
-         DefaultLogSink().Error("Error initializing packet\n");
+         DefaultLogSink().Error("Error initializing OPAtom footer packet.\n");
          delete object;
        }
     }
 
   if ( ASDCP_FAILURE(result) )
-    DefaultLogSink().Error("Failed to initialize OPAtomIndexFooter\n");
+    {
+      DefaultLogSink().Error("Failed to initialize OPAtomIndexFooter.\n");
+    }
 
   return result;
 }
@@ -1213,8 +1259,16 @@ ASDCP::MXF::OPAtomIndexFooter::Lookup(ui32_t frame_num, IndexTableSegment::Index
            {
              ui64_t tmp = frame_num - start_pos;
              assert(tmp <= 0xFFFFFFFFL);
-             Entry = segment->IndexEntryArray[(ui32_t) tmp];
-             return RESULT_OK;
+
+             if ( tmp < segment->IndexEntryArray.size() )
+               {
+                 Entry = segment->IndexEntryArray[(ui32_t) tmp];
+                 return RESULT_OK;
+               }
+             else
+               {
+                 DefaultLogSink().Error("Malformed index table segment, IndexDuration does not match entries.\n");
+               }
            }
        }
     }
@@ -1336,6 +1390,11 @@ ASDCP::MXF::InterchangeObject::InitFromBuffer(const byte_t* p, ui32_t l)
 
       if ( ASDCP_SUCCESS(result) )
        {
+    if (m_ValueStart + m_ValueLength > p  + l)
+    {
+      DefaultLogSink().Error("Interchange Object value extends past buffer length.\n");
+      return RESULT_FAIL;
+    }
          TLVReader MemRDR(m_ValueStart, m_ValueLength, m_Lookup);
          result = InitFromTLVSet(MemRDR);
        }
@@ -1396,9 +1455,24 @@ ASDCP::MXF::InterchangeObject::IsA(const byte_t* label)
 
 
 //------------------------------------------------------------------------------------------
+struct FactoryCompareUL
+{
+    bool operator()(const ASDCP::UL& lhs, const ASDCP::UL& rhs) const
+    {
+        ui32_t test_size = lhs.Size() < rhs.Size() ? lhs.Size() : rhs.Size();
+
+        for (ui32_t i = 0; i < test_size; i++)
+        {
+            if (i == 7) continue; // skip version to be symmetrical with UL::operator==
+            if (lhs.Value()[i] != rhs.Value()[i])
+                return lhs.Value()[i] < rhs.Value()[i];
+        }
 
+        return false;
+    }
+};
 
-typedef std::map<ASDCP::UL, ASDCP::MXF::MXFObjectFactory_t>FactoryMap_t;
+typedef std::map<ASDCP::UL, ASDCP::MXF::MXFObjectFactory_t, FactoryCompareUL>FactoryMap_t;
 typedef FactoryMap_t::iterator FLi_t;
 
 //
@@ -1459,7 +1533,7 @@ ASDCP::MXF::CreateObject(const Dictionary*& Dict, const UL& label)
        }
     }
 
-  FLi_t i = s_FactoryList.find(label.Value());
+  FLi_t i = s_FactoryList.find(label);
 
   if ( i == s_FactoryList.end() )
     return new InterchangeObject(Dict);
@@ -1477,27 +1551,40 @@ ASDCP::MXF::decode_mca_string(const std::string& s, const mca_label_map_t& label
 {
   std::string symbol_buf;
   channel_count = 0;
-  ASDCP::MXF::SoundfieldGroupLabelSubDescriptor *current_soundfield = 0;
+  ASDCP::MXF::SoundfieldGroupLabelSubDescriptor *current_soundfield = 0, *prev_soundfield = 0;
   std::string::const_iterator i;
 
   for ( i = s.begin(); i != s.end(); ++i )
     {
       if ( *i == '(' )
        {
-         if ( current_soundfield != 0 )
+         if ( current_soundfield != 0 && symbol_buf.empty() )
+           {
+             // appending to the existing soundfield group
+             continue;
+           }
+         else if ( current_soundfield != 0 )
            {
              DefaultLogSink().Error("Encountered '(', already processing a soundfield group.\n");
              return false;
            }
-
-         if ( symbol_buf.empty() )
+         else if ( symbol_buf.empty() )
            {
-             DefaultLogSink().Error("Encountered '(', without leading soundfield group symbol.\n");
-             return false;
+             if ( prev_soundfield != 0 )
+               {
+                 current_soundfield = prev_soundfield;
+                 // appending to the existing soundfield group
+                 continue;
+               }
+             else
+               {
+                 DefaultLogSink().Error("Encountered '(', without leading soundfield group symbol.\n");
+                 return false;
+               }
            }
 
          mca_label_map_t::const_iterator i = labels.find(symbol_buf);
-      
+
          if ( i == labels.end() )
            {
              DefaultLogSink().Error("Unknown symbol: '%s'\n", symbol_buf.c_str());
@@ -1511,14 +1598,14 @@ ASDCP::MXF::decode_mca_string(const std::string& s, const mca_label_map_t& label
            }
 
          current_soundfield = new ASDCP::MXF::SoundfieldGroupLabelSubDescriptor(dict);
-
-         GenRandomValue(current_soundfield->InstanceUID);
          GenRandomValue(current_soundfield->MCALinkID);
+
          current_soundfield->MCATagSymbol = (i->second.requires_prefix ? "sg" : "") + i->first;
          current_soundfield->MCATagName = i->second.tag_name;
          current_soundfield->RFC5646SpokenLanguage = language;
          current_soundfield->MCALabelDictionaryID = i->second.ul;
          descriptor_list.push_back(reinterpret_cast<ASDCP::MXF::InterchangeObject*>(current_soundfield));
+         prev_soundfield = current_soundfield;
          symbol_buf.clear();
        }
       else if ( *i == ')' )
@@ -1543,12 +1630,12 @@ ASDCP::MXF::decode_mca_string(const std::string& s, const mca_label_map_t& label
              return false;
            }
 
+         assert(current_soundfield);
+
          ASDCP::MXF::AudioChannelLabelSubDescriptor *channel_descr =
            new ASDCP::MXF::AudioChannelLabelSubDescriptor(dict);
-
-         GenRandomValue(channel_descr->InstanceUID);
          GenRandomValue(channel_descr->MCALinkID);
-         assert(current_soundfield);
+
          channel_descr->SoundfieldGroupLinkID = current_soundfield->MCALinkID;
          channel_descr->MCAChannelID = channel_count++ + 1;
          channel_descr->MCATagSymbol = (i->second.requires_prefix ? "ch" : "") + i->first;
@@ -1584,8 +1671,7 @@ ASDCP::MXF::decode_mca_string(const std::string& s, const mca_label_map_t& label
 
              ASDCP::MXF::AudioChannelLabelSubDescriptor *channel_descr =
                new ASDCP::MXF::AudioChannelLabelSubDescriptor(dict);
-
-             GenRandomValue(channel_descr->InstanceUID);
+             GenRandomValue(channel_descr->MCALinkID);
 
              if ( current_soundfield != 0 )
                {
@@ -1628,8 +1714,6 @@ ASDCP::MXF::decode_mca_string(const std::string& s, const mca_label_map_t& label
 
       ASDCP::MXF::AudioChannelLabelSubDescriptor *channel_descr =
        new ASDCP::MXF::AudioChannelLabelSubDescriptor(dict);
-
-      GenRandomValue(channel_descr->InstanceUID);
       GenRandomValue(channel_descr->MCALinkID);
 
       if ( current_soundfield != 0 )
@@ -1672,6 +1756,7 @@ ASDCP::MXF::ASDCP_MCAConfigParser::ASDCP_MCAConfigParser(const Dictionary*& d) :
   m_LabelMap.insert(pair("SDS",   label_traits("7.1SDS"                            , true,  m_Dict->ul(MDD_DCAudioSoundfield_SDS))));
   m_LabelMap.insert(pair("61",    label_traits("6.1"                               , true,  m_Dict->ul(MDD_DCAudioSoundfield_61))));
   m_LabelMap.insert(pair("M",     label_traits("1.0 Monaural"                      , true,  m_Dict->ul(MDD_DCAudioSoundfield_M))));
+  m_LabelMap.insert(pair("FSKSync",   label_traits("FSK Sync"                      , true,  m_Dict->ul(MDD_DCAudioChannel_FSKSyncSignalChannel))));
   m_LabelMap.insert(pair("DBOX",  label_traits("D-BOX Motion Code Primary Stream"  , false, m_Dict->ul(MDD_DBOXMotionCodePrimaryStream))));
   m_LabelMap.insert(pair("DBOX2", label_traits("D-BOX Motion Code Secondary Stream", false, m_Dict->ul(MDD_DBOXMotionCodeSecondaryStream))));
 }
@@ -1690,32 +1775,163 @@ ASDCP::MXF::ASDCP_MCAConfigParser::DecodeString(const std::string& s, const std:
   return decode_mca_string(s, m_LabelMap, m_Dict, language, *this, m_ChannelCount);
 }
 
-
-
+// ST(L,R),DNS(NSC001,NSC002),-,VIN
 ASDCP::MXF::AS02_MCAConfigParser::AS02_MCAConfigParser(const Dictionary*& d) : ASDCP::MXF::ASDCP_MCAConfigParser(d)
 {
   typedef mca_label_map_t::value_type pair;
-  m_LabelMap.insert(pair("M1",    label_traits("M1",  true,  m_Dict->ul(MDD_IMFAudioChannel_M1))));
-  m_LabelMap.insert(pair("M2",    label_traits("M2",  true,  m_Dict->ul(MDD_IMFAudioChannel_M2))));
-  m_LabelMap.insert(pair("Lt",    label_traits("Lt",  true,  m_Dict->ul(MDD_IMFAudioChannel_Lt))));
-  m_LabelMap.insert(pair("Rt",    label_traits("Rt",  true,  m_Dict->ul(MDD_IMFAudioChannel_Rt))));
-  m_LabelMap.insert(pair("Lst",   label_traits("Lst", true,  m_Dict->ul(MDD_IMFAudioChannel_Lst))));
-  m_LabelMap.insert(pair("Rst",   label_traits("Rst", true,  m_Dict->ul(MDD_IMFAudioChannel_Rst))));
-  m_LabelMap.insert(pair("S",     label_traits("S",   true,  m_Dict->ul(MDD_IMFAudioChannel_S))));
-  m_LabelMap.insert(pair("ST",    label_traits("ST",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_ST))));
-  m_LabelMap.insert(pair("DM",    label_traits("DM",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_DM))));
-  m_LabelMap.insert(pair("DNS",   label_traits("DNS", true,  m_Dict->ul(MDD_IMFAudioSoundfield_DNS))));
-  m_LabelMap.insert(pair("30",    label_traits("30",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_30))));
-  m_LabelMap.insert(pair("40",    label_traits("40",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_40))));
-  m_LabelMap.insert(pair("50",    label_traits("50",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_50))));
-  m_LabelMap.insert(pair("60",    label_traits("60",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_60))));
-  m_LabelMap.insert(pair("70",    label_traits("70",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_70))));
-  m_LabelMap.insert(pair("LtRt",  label_traits("LtRt",true,  m_Dict->ul(MDD_IMFAudioSoundfield_LtRt))));
-  m_LabelMap.insert(pair("51Ex",  label_traits("51Ex",true,  m_Dict->ul(MDD_IMFAudioSoundfield_51Ex))));
-  m_LabelMap.insert(pair("HI",    label_traits("HI",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_HI))));
-  m_LabelMap.insert(pair("VIN",   label_traits("VIN", true,  m_Dict->ul(MDD_IMFAudioSoundfield_VIN))));
+  m_LabelMap.insert(pair("M1",    label_traits("Mono One",  true,  m_Dict->ul(MDD_IMFAudioChannel_M1))));
+  m_LabelMap.insert(pair("M2",    label_traits("Mono Two",  true,  m_Dict->ul(MDD_IMFAudioChannel_M2))));
+  m_LabelMap.insert(pair("Lt",    label_traits("Left Total",  true,  m_Dict->ul(MDD_IMFAudioChannel_Lt))));
+  m_LabelMap.insert(pair("Rt",    label_traits("Right Total",  true,  m_Dict->ul(MDD_IMFAudioChannel_Rt))));
+  m_LabelMap.insert(pair("Lst",   label_traits("Left Surround Total", true,  m_Dict->ul(MDD_IMFAudioChannel_Lst))));
+  m_LabelMap.insert(pair("Rst",   label_traits("Right Surround Total", true,  m_Dict->ul(MDD_IMFAudioChannel_Rst))));
+  m_LabelMap.insert(pair("S",     label_traits("Surround",   true,  m_Dict->ul(MDD_IMFAudioChannel_S))));
+  m_LabelMap.insert(pair("ST",    label_traits("Standard Stereo",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_ST))));
+  m_LabelMap.insert(pair("DM",    label_traits("Dual Mono",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_DM))));
+  m_LabelMap.insert(pair("DNS",   label_traits("Discrete Numbered Sources", true,  m_Dict->ul(MDD_IMFAudioSoundfield_DNS))));
+  m_LabelMap.insert(pair("30",    label_traits("3.0",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_30))));
+  m_LabelMap.insert(pair("40",    label_traits("4.0",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_40))));
+  m_LabelMap.insert(pair("50",    label_traits("5.0",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_50))));
+  m_LabelMap.insert(pair("60",    label_traits("6.0",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_60))));
+  m_LabelMap.insert(pair("70",    label_traits("7.0DS",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_70))));
+  m_LabelMap.insert(pair("LtRt",  label_traits("Lt-Rt",true,  m_Dict->ul(MDD_IMFAudioSoundfield_LtRt))));
+  m_LabelMap.insert(pair("51Ex",  label_traits("5.1EX",true,  m_Dict->ul(MDD_IMFAudioSoundfield_51Ex))));
+  m_LabelMap.insert(pair("HA",    label_traits("Hearing Accessibility",  true,  m_Dict->ul(MDD_IMFAudioSoundfield_HI))));
+  m_LabelMap.insert(pair("VA",   label_traits("Visual Accessibility", true,  m_Dict->ul(MDD_IMFAudioSoundfield_VIN))));
+
+  // assemble the  set of Numbered Source Channel labels
+  char name_buf[64], symbol_buf[64];
+  byte_t ul_buf[16];
+  memcpy(ul_buf, m_Dict->ul(MDD_IMFNumberedSourceChannel), 16);
+
+  for ( int i = 1; i < 128; ++i )
+    {
+      snprintf(name_buf, 64, "Numbered Source Channel %03d", i);
+      snprintf(symbol_buf, 64, "NSC%03d", i);
+      ul_buf[13] = i;
+      m_LabelMap.insert(pair(symbol_buf, label_traits(name_buf, true, UL(ul_buf))));
+    }
 }
 
+//
+bool
+ASDCP::MXF::GetEditRateFromFP(ASDCP::MXF::OP1aHeader& header, ASDCP::Rational& edit_rate)
+{
+  bool has_first_item = false;
+
+  MXF::InterchangeObject* temp_item;
+  std::list<MXF::InterchangeObject*> temp_items;
+
+  Result_t result = header.GetMDObjectsByType(DefaultCompositeDict().ul(MDD_SourcePackage), temp_items);
+
+  if ( KM_FAILURE(result) )
+    {
+      DefaultLogSink().Error("The MXF header does not contain a FilePackage item.\n");
+      return false;
+    }
+
+  if ( temp_items.size() != 1 )
+    {
+      DefaultLogSink().Error("The MXF header must contain one FilePackage item, found %d.\n", temp_items.size());
+      return false;
+    }
+
+  char buf[64];
+  MXF::Array<UUID>::const_iterator i;
+  MXF::SourcePackage *source_package = dynamic_cast<MXF::SourcePackage*>(temp_items.front());
+  assert(source_package);
+
+  for ( i = source_package->Tracks.begin(); i != source_package->Tracks.end(); ++i )
+    {
+      // Track
+      result = header.GetMDObjectByID(*i, &temp_item);
+
+      if ( KM_FAILURE(result) )
+       {
+         DefaultLogSink().Error("The MXF header is incomplete: strong reference %s leads nowhere.\n",
+                                i->EncodeHex(buf, 64));
+         return false;
+       }
+
+      MXF::Track *track = dynamic_cast<MXF::Track*>(temp_item);
+
+      if ( track == 0 )
+       {
+         DefaultLogSink().Error("The MXF header is incomplete: %s is not a Track item.\n",
+                                i->EncodeHex(buf, 64));
+         return false;
+       }
+
+      // Sequence
+      result = header.GetMDObjectByID(track->Sequence, &temp_item);
+
+      if ( KM_FAILURE(result) )
+       {
+         DefaultLogSink().Error("The MXF header is incomplete: strong reference %s leads nowhere.\n",
+                                i->EncodeHex(buf, 64));
+         return false;
+       }
+
+      MXF::Sequence *sequence = dynamic_cast<MXF::Sequence*>(temp_item);
+
+      if ( sequence == 0 )
+       {
+         DefaultLogSink().Error("The MXF header is incomplete: %s is not a Sequence item.\n",
+                                track->Sequence.get().EncodeHex(buf, 64));
+         return false;
+       }
+
+      if ( sequence->StructuralComponents.size() != 1 )
+       {
+         DefaultLogSink().Error("The Sequence item must contain one reference to an esence item, found %d.\n",
+                                sequence->StructuralComponents.size());
+         return false;
+       }
+
+      // SourceClip
+      result = header.GetMDObjectByID(sequence->StructuralComponents.front(), &temp_item);
+
+      if ( KM_FAILURE(result) )
+       {
+         DefaultLogSink().Error("The MXF header is incomplete: strong reference %s leads nowhere.\n",
+                                sequence->StructuralComponents.front().EncodeHex(buf, 64));
+         return false;
+       }
+
+      if ( temp_item->IsA(DefaultCompositeDict().ul(MDD_SourceClip)) )
+       {
+         MXF::SourceClip *source_clip = dynamic_cast<MXF::SourceClip*>(temp_item);
+
+         if ( source_clip == 0 )
+           {
+             DefaultLogSink().Error("The MXF header is incomplete: %s is not a SourceClip item.\n",
+                                    sequence->StructuralComponents.front().EncodeHex(buf, 64));
+             return false;
+           }
+
+         if ( ! has_first_item )
+           {
+             edit_rate = track->EditRate;
+             has_first_item = true;
+           }
+         else if ( edit_rate != track->EditRate )
+           {
+             DefaultLogSink().Error("The MXF header is incomplete: %s EditRate value does not match others in the file.\n",
+                                    sequence->StructuralComponents.front().EncodeHex(buf, 64));
+             return false;
+           }
+       }
+      else if ( ! temp_item->IsA(DefaultCompositeDict().ul(MDD_TimecodeComponent)) )
+       {
+         DefaultLogSink().Error("Reference from Sequence to an unexpected type: %s.\n", temp_item->ObjectName());
+         return false;
+       }
+    }
+
+  return true;
+}
+
+
 //
 // end MXF.cpp
 //