Merge pull request #20 from cinecert/htj2c
[asdcplib.git] / src / Index.cpp
index b08ad6fdfe9359552d6f65f95ad703dbb6cc5fef..6406e0f353a1cad4ac65300f04d245f57114cd2e 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005-2006, John Hurst
+Copyright (c) 2005-2012, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -34,10 +34,13 @@ const ui32_t kl_length = ASDCP::SMPTE_UL_LENGTH + ASDCP::MXF_BER_LENGTH;
 
 
 //
-ASDCP::MXF::IndexTableSegment::IndexTableSegment() :
+ASDCP::MXF::IndexTableSegment::IndexTableSegment(const Dictionary*& d) :
+  InterchangeObject(d), m_Dict(d), RtFileOffset(0), RtEntryOffset(0),
   IndexStartPosition(0), IndexDuration(0), EditUnitByteCount(0),
   IndexSID(129), BodySID(1), SliceCount(0), PosTableCount(0)
 {
+  assert(m_Dict);
+  m_UL = m_Dict->ul(MDD_IndexTableSegment);
 }
 
 //
@@ -45,6 +48,23 @@ ASDCP::MXF::IndexTableSegment::~IndexTableSegment()
 {
 }
 
+//
+void
+ASDCP::MXF::IndexTableSegment::Copy(const IndexTableSegment& rhs)
+{
+  InterchangeObject::Copy(rhs);
+  IndexEditRate = rhs.IndexEditRate;
+  IndexStartPosition = rhs.IndexStartPosition;
+  IndexDuration = rhs.IndexDuration;
+  EditUnitByteCount = rhs.EditUnitByteCount;
+  IndexSID = rhs.IndexSID;
+  BodySID = rhs.BodySID;
+  SliceCount = rhs.SliceCount;
+  PosTableCount = rhs.PosTableCount;
+  DeltaEntryArray = rhs.DeltaEntryArray;
+  IndexEntryArray = rhs.IndexEntryArray;
+}
+
 //
 ASDCP::Result_t
 ASDCP::MXF::IndexTableSegment::InitFromTLVSet(TLVReader& TLVSet)
@@ -55,11 +75,46 @@ ASDCP::MXF::IndexTableSegment::InitFromTLVSet(TLVReader& TLVSet)
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi64(OBJ_READ_ARGS(IndexTableSegmentBase, IndexDuration));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi32(OBJ_READ_ARGS(IndexTableSegmentBase, EditUnitByteCount));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi32(OBJ_READ_ARGS(IndexTableSegmentBase, IndexSID));
-  if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi32(OBJ_READ_ARGS(IndexTableSegmentBase, BodySID));
+  if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi32(OBJ_READ_ARGS(EssenceContainerData, BodySID));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi8(OBJ_READ_ARGS(IndexTableSegmentBase, SliceCount));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadUi8(OBJ_READ_ARGS(IndexTableSegmentBase, PosTableCount));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(IndexTableSegment, DeltaEntryArray));
-  if ( ASDCP_SUCCESS(result) ) result = TLVSet.ReadObject(OBJ_READ_ARGS(IndexTableSegment, IndexEntryArray));
+
+  if ( ASDCP_SUCCESS(result) )
+    {
+      bool rc = TLVSet.FindTL(m_Dict->Type(MDD_IndexTableSegment_IndexEntryArray));
+
+      if ( rc )
+       {
+         ui32_t item_count, item_size;
+         ui32_t const decoder_item_size = IndexEntryArray.ItemSize();
+
+         if ( TLVSet.ReadUi32BE(&item_count) )
+           {
+             if ( TLVSet.ReadUi32BE(&item_size) )
+               {
+                 for ( ui32_t i = 0; i < item_count && rc; ++i )
+                   {
+                     IndexEntry tmp_item;
+                     rc = tmp_item.Unarchive(&TLVSet);
+
+                     if ( rc )
+                       {
+                         IndexEntryArray.push_back(tmp_item);
+
+                         if ( decoder_item_size < item_size )
+                           {
+                             TLVSet.SkipOffset(item_size - decoder_item_size);
+                           }
+                       }
+                   }
+               }
+           }
+       }
+
+      result = rc ? RESULT_OK : RESULT_FALSE;
+    }
+
   return result;
 }
 
@@ -73,7 +128,7 @@ ASDCP::MXF::IndexTableSegment::WriteToTLVSet(TLVWriter& TLVSet)
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi64(OBJ_WRITE_ARGS(IndexTableSegmentBase, IndexDuration));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi32(OBJ_WRITE_ARGS(IndexTableSegmentBase, EditUnitByteCount));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi32(OBJ_WRITE_ARGS(IndexTableSegmentBase, IndexSID));
-  if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi32(OBJ_WRITE_ARGS(IndexTableSegmentBase, BodySID));
+  if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi32(OBJ_WRITE_ARGS(EssenceContainerData, BodySID));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi8(OBJ_WRITE_ARGS(IndexTableSegmentBase, SliceCount));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteUi8(OBJ_WRITE_ARGS(IndexTableSegmentBase, PosTableCount));
   if ( ASDCP_SUCCESS(result) ) result = TLVSet.WriteObject(OBJ_WRITE_ARGS(IndexTableSegment, DeltaEntryArray));
@@ -85,7 +140,6 @@ ASDCP::MXF::IndexTableSegment::WriteToTLVSet(TLVWriter& TLVSet)
 ASDCP::Result_t
 ASDCP::MXF::IndexTableSegment::InitFromBuffer(const byte_t* p, ui32_t l)
 {
-  m_Typeinfo = &Dict::Type(MDD_IndexTableSegment);
   return InterchangeObject::InitFromBuffer(p, l);
 }
 
@@ -93,7 +147,6 @@ ASDCP::MXF::IndexTableSegment::InitFromBuffer(const byte_t* p, ui32_t l)
 ASDCP::Result_t
 ASDCP::MXF::IndexTableSegment::WriteToBuffer(ASDCP::FrameBuffer& Buffer)
 {
-  m_Typeinfo = &Dict::Type(MDD_IndexTableSegment);
   return InterchangeObject::WriteToBuffer(Buffer);
 }
 
@@ -113,19 +166,23 @@ ASDCP::MXF::IndexTableSegment::Dump(FILE* stream)
   fprintf(stream, "  EditUnitByteCount  = %u\n",  EditUnitByteCount);
   fprintf(stream, "  IndexSID           = %u\n",  IndexSID);
   fprintf(stream, "  BodySID            = %u\n",  BodySID);
-  fprintf(stream, "  SliceCount         = %hu\n", SliceCount);
-  fprintf(stream, "  PosTableCount      = %hu\n", PosTableCount);
+  fprintf(stream, "  SliceCount         = %hhu\n", SliceCount);
+  fprintf(stream, "  PosTableCount      = %hhu\n", PosTableCount);
 
   fprintf(stream, "  DeltaEntryArray:\n");  DeltaEntryArray.Dump(stream);
 
-  if ( IndexEntryArray.size() < 100 )
+  if ( IndexEntryArray.empty() )
+    {
+      fprintf(stream, "  IndexEntryArray: NO ENTRIES\n");
+    }
+  else if ( IndexEntryArray.size() < 1000 )
     {
       fprintf(stream, "  IndexEntryArray:\n");
       IndexEntryArray.Dump(stream);
     }
   else
     {
-      fprintf(stream, "  IndexEntryArray: %du entries\n", IndexEntryArray.size());
+      fprintf(stream, "  IndexEntryArray: %zu entries\n", IndexEntryArray.size());
     }
 }
 
@@ -136,7 +193,7 @@ ASDCP::MXF::IndexTableSegment::Dump(FILE* stream)
 const char*
 ASDCP::MXF::IndexTableSegment::DeltaEntry::EncodeString(char* str_buf, ui32_t buf_len) const
 {
-  snprintf(str_buf, buf_len, "%3d %-3hu %-3u", PosTableIndex, Slice, ElementData);
+  snprintf(str_buf, buf_len, "%3d %-3hhu %-3u", PosTableIndex, Slice, ElementData);
   return str_buf;
 }
 
@@ -189,7 +246,7 @@ ASDCP::MXF::IndexTableSegment::IndexEntry::EncodeString(char* str_buf, ui32_t bu
   txt_flags[4] = ( (Flags & 0x0f) == 3 ) ? 'B' : ( (Flags & 0x0f) == 2 ) ? 'P' : 'I';
   txt_flags[5] = 0;
 
-  snprintf(str_buf, buf_len, "%3i %-3hu %s %s",
+  snprintf(str_buf, buf_len, "%3i %-3hhu %s %s",
           TemporalOffset, KeyFrameOffset, txt_flags,
           i64sz(StreamOffset, intbuf));