Bump patch version post tag.
[asdcplib.git] / src / MXFTypes.h
index 77c0f5fdf8cb1a240c71b0efe3c0dbcf6769f210..baf27c4ea147692618a030f7332e001e92f22263 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005-2015, John Hurst
+Copyright (c) 2005-2019, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -33,8 +33,10 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #define _MXFTYPES_H_
 
 #include "KLV.h"
+#include "AS_DCP.h"
 #include <list>
 #include <vector>
+#include <set>
 #include <map>
 #include <wchar.h>
 
@@ -64,10 +66,10 @@ namespace ASDCP
 
          TLVReader();
          ASDCP_NO_COPY_CONSTRUCT(TLVReader);
-         bool FindTL(const MDDEntry&);
 
        public:
          TLVReader(const byte_t* p, ui32_t c, IPrimerLookup* = 0);
+         bool FindTL(const MDDEntry&);
          Result_t ReadObject(const MDDEntry&, Kumu::IArchive*);
          Result_t ReadUi8(const MDDEntry&, ui8_t*);
          Result_t ReadUi16(const MDDEntry&, ui16_t*);
@@ -96,97 +98,117 @@ namespace ASDCP
        };
 
       //
-      template <class T>
-       class Batch : public std::vector<T>, public Kumu::IArchive
+      template <class ContainerType>
+       class FixedSizeItemCollection : public ContainerType, public Kumu::IArchive
        {
        public:
-         Batch() {}
-         virtual ~Batch() {}
+         FixedSizeItemCollection() {}
+         virtual ~FixedSizeItemCollection() {}
 
-         //
-         virtual bool Unarchive(Kumu::MemIOReader* Reader) {
-           ui32_t ItemCount, ItemSize;
-           if ( ! Reader->ReadUi32BE(&ItemCount) ) return false;
-           if ( ! Reader->ReadUi32BE(&ItemSize) ) return false;
+         ui32_t ItemSize() const {
+           typename ContainerType::value_type tmp_item;
+           return tmp_item.ArchiveLength();
+         }
+
+         bool HasValue() const { return ! this->empty(); }
 
-           if ( ( ItemCount > 65536 ) || ( ItemSize > 1024 ) )
-             return false;
+         ui32_t ArchiveLength() const {
+           return ( sizeof(ui32_t) * 2 ) +  ( (ui32_t)this->size() * this->ItemSize() );
+         }
 
+         bool Archive(Kumu::MemIOWriter* Writer) const {
+           if ( ! Writer->WriteUi32BE((ui32_t)this->size()) ) return false;
+           if ( ! Writer->WriteUi32BE((ui32_t)this->ItemSize()) ) return false;
+           if ( this->empty() ) return true;
+           
+           typename ContainerType::const_iterator i;
            bool result = true;
-           for ( ui32_t i = 0; i < ItemCount && result; i++ )
+           for ( i = this->begin(); i != this->end() && result; ++i )
              {
-               T Tmp;
-               result = Tmp.Unarchive(Reader);
-
-               if ( result )
-                 this->push_back(Tmp);
+               result = i->Archive(Writer);
              }
 
            return result;
          }
 
-         inline virtual bool HasValue() const { return ! this->empty(); }
-
-         virtual ui32_t ArchiveLength() const {
-           ui32_t arch_size = sizeof(ui32_t)*2;
-
-           typename std::vector<T>::const_iterator l_i = this->begin();
-           assert(l_i != this->end());
-
-           for ( ; l_i != this->end(); l_i++ )
-             arch_size += l_i->ArchiveLength();
-           
-           return arch_size;
-         }
-
          //
-         virtual bool Archive(Kumu::MemIOWriter* Writer) const {
-           if ( ! Writer->WriteUi32BE(this->size()) ) return false;
-           byte_t* p = Writer->CurrentData();
-
-           if ( ! Writer->WriteUi32BE(0) ) return false;
-           if ( this->empty() ) return true;
-           
-           typename std::vector<T>::const_iterator l_i = this->begin();
-           assert(l_i != this->end());
+         bool Unarchive(Kumu::MemIOReader* Reader) {
+           ui32_t item_count, item_size;
+           if ( ! Reader->ReadUi32BE(&item_count) ) return false;
+           if ( ! Reader->ReadUi32BE(&item_size) ) return false;
 
-           ui32_t ItemSize = Writer->Remainder();
-           if ( ! (*l_i).Archive(Writer) ) return false;
-           ItemSize -= Writer->Remainder();
-           Kumu::i2p<ui32_t>(KM_i32_BE(ItemSize), p);
-           l_i++;
+           if ( item_count > 0 )
+             {
+               if ( this->ItemSize() != item_size ) return false;
+             }
 
            bool result = true;
-           for ( ; l_i != this->end() && result; l_i++ )
-             result = (*l_i).Archive(Writer);
+           for ( ui32_t i = 0; i < item_count && result; ++i )
+             {
+               typename ContainerType::value_type tmp_item;
+               result = tmp_item.Unarchive(Reader);
+
+               if ( result )
+                 {
+                   this->push_back(tmp_item);
+                 }
+             }
 
            return result;
          }
 
-         //
-         void Dump(FILE* stream = 0, ui32_t depth = 0)
-           {
-             char identbuf[IdentBufferLen];
+         void Dump(FILE* stream = 0, ui32_t depth = 0) {
+           char identbuf[IdentBufferLen];
 
-             if ( stream == 0 )
+           if ( stream == 0 )
+             {
                stream = stderr;
-
-             typename std::vector<T>::iterator i = this->begin();
-             for ( ; i != this->end(); i++ )
+             }
+           
+           typename ContainerType::const_iterator i;
+           for ( i = this->begin(); i != this->end(); ++i )
+             {
                fprintf(stream, "  %s\n", (*i).EncodeString(identbuf, IdentBufferLen));
-           }
+             }
+         }
        };
 
+
+      template <class item_type>
+       class PushSet : public std::set<item_type>
+      {
+      public:
+       PushSet() {}
+       virtual ~PushSet() {}
+       void push_back(const item_type& item) { this->insert(item); }
+      };
+
+      template <class ItemType>
+       class Batch : public FixedSizeItemCollection<PushSet<ItemType> >
+      {
+      public:
+       Batch() {}
+       virtual ~Batch() {}
+      };
+
+      template <class ItemType>
+       class Array : public FixedSizeItemCollection<std::vector<ItemType> >
+      {
+      public:
+       Array() {}
+       virtual ~Array() {}
+      };
+
       //
       template <class T>
-       class Array : public std::list<T>, public Kumu::IArchive
+       class SimpleArray : public std::list<T>, public Kumu::IArchive
        {
        public:
-         Array() {}
-         virtual ~Array() {}
+         SimpleArray() {}
+         virtual ~SimpleArray() {}
 
          //
-         virtual bool Unarchive(Kumu::MemIOReader* Reader)
+         bool Unarchive(Kumu::MemIOReader* Reader)
            {
              bool result = true;
 
@@ -194,15 +216,19 @@ namespace ASDCP
                {
                  T Tmp;
                  result = Tmp.Unarchive(Reader);
-                 this->push_back(Tmp);
+
+                 if ( result )
+                   {
+                     this->push_back(Tmp);
+                   }
                }
 
              return result;
            }
 
-         inline virtual bool HasValue() const { return ! this->empty(); }
+         inline bool HasValue() const { return ! this->empty(); }
 
-         virtual ui32_t ArchiveLength() const {
+         ui32_t ArchiveLength() const {
            ui32_t arch_size = 0;
 
            typename std::list<T>::const_iterator l_i = this->begin();
@@ -214,7 +240,7 @@ namespace ASDCP
          }
 
          //
-         virtual bool Archive(Kumu::MemIOWriter* Writer) const {
+         bool Archive(Kumu::MemIOWriter* Writer) const {
            bool result = true;
            typename std::list<T>::const_iterator l_i = this->begin();
 
@@ -252,7 +278,7 @@ namespace ASDCP
 
          const char* EncodeString(char* str_buf, ui32_t buf_len) const;
          inline virtual bool HasValue() const { return ! empty(); }
-         inline virtual ui32_t ArchiveLength() const { return sizeof(ui32_t) + size(); }
+         inline virtual ui32_t ArchiveLength() const { return (ui32_t)(sizeof(ui32_t) + size()); }
          virtual bool Unarchive(Kumu::MemIOReader* Reader);
          virtual bool Archive(Kumu::MemIOWriter* Writer) const;
        };
@@ -271,7 +297,7 @@ namespace ASDCP
 
          const char* EncodeString(char* str_buf, ui32_t buf_len) const;
          inline virtual bool HasValue() const { return ! empty(); }
-         inline virtual ui32_t ArchiveLength() const { return sizeof(ui32_t) + size(); }
+         inline virtual ui32_t ArchiveLength() const { return (ui32_t)(sizeof(ui32_t) + size()); }
          virtual bool Unarchive(Kumu::MemIOReader* Reader);
          virtual bool Archive(Kumu::MemIOWriter* Writer) const;
        };
@@ -327,6 +353,157 @@ namespace ASDCP
          }
        };
 
+      //
+      class LineMapPair : public Kumu::IArchive
+       {
+       public:
+         ui32_t First;
+         ui32_t Second;
+
+       LineMapPair() : First(0), Second() {}
+         ~LineMapPair() {}
+
+       LineMapPair(const ui32_t& first, const ui32_t& second) : IArchive() {
+           First = first;
+           Second = second;
+         }
+
+         LineMapPair(const LineMapPair& rhs) : IArchive() {
+           First = rhs.First;
+           Second = rhs.Second;
+         }
+
+         const LineMapPair& operator=(const LineMapPair& rhs) {
+           First = rhs.First;
+           Second = rhs.Second;
+           return *this;
+         }
+
+         //
+         inline const char* EncodeString(char* str_buf, ui32_t buf_len) const {
+           snprintf(str_buf, buf_len, "%d,%d", First, Second);
+           return str_buf;
+         }
+
+         inline virtual bool Unarchive(Kumu::MemIOReader* Reader) {
+           ui32_t n;
+           if ( ! Reader->ReadUi32BE(&n) ) return false;
+           if ( n != 2 ) return false;
+           if ( ! Reader->ReadUi32BE(&n) ) return false;
+           if ( n != 4 ) return false;
+           if ( ! Reader->ReadUi32BE((ui32_t*)&First) ) return false;
+           if ( ! Reader->ReadUi32BE((ui32_t*)&Second) ) return false;
+           return true;
+         }
+
+         inline virtual bool HasValue() const { return true; }
+         inline virtual ui32_t ArchiveLength() const { return sizeof(ui32_t)*4; }
+
+         inline virtual bool Archive(Kumu::MemIOWriter* Writer) const {
+           if ( ! Writer->WriteUi32BE(2UL) ) return false;
+           if ( ! Writer->WriteUi32BE(4UL) ) return false;
+           if ( ! Writer->WriteUi32BE((ui32_t)First) ) return false;
+           if ( ! Writer->WriteUi32BE((ui32_t)Second) ) return false;
+           return true;
+         }
+       };
+
+      //
+      class ColorPrimary : public Kumu::IArchive
+       {
+       public:
+         ui16_t X;
+         ui16_t Y;
+
+       ColorPrimary() : X(0), Y(0) {}
+         ~ColorPrimary() {}
+
+         ColorPrimary(const ui16_t& x, const ui16_t& y) : X(x), Y(y) {}
+
+         ColorPrimary(const ColorPrimary& rhs) { Copy(rhs); }
+         const ColorPrimary& operator=(const ColorPrimary& rhs) { Copy(rhs); return *this; }
+         
+         void Copy(const ColorPrimary& rhs) {
+           X = rhs.X;
+           Y = rhs.Y;
+         }
+
+         //
+         inline const char* EncodeString(char* str_buf, ui32_t buf_len) const {
+           snprintf(str_buf, buf_len, "%d,%d", X, Y);
+           return str_buf;
+         }
+
+         inline virtual bool Unarchive(Kumu::MemIOReader* Reader) {
+           if ( ! Reader->ReadUi16BE((ui16_t*)&X) ) return false;
+           if ( ! Reader->ReadUi16BE((ui16_t*)&Y) ) return false;
+           return true;
+         }
+
+         inline virtual bool HasValue() const { return X || Y; }
+         inline virtual ui32_t ArchiveLength() const { return sizeof(ui16_t)*2; }
+
+         inline virtual bool Archive(Kumu::MemIOWriter* Writer) const {
+           if ( ! Writer->WriteUi16BE((ui16_t)X) ) return false;
+           if ( ! Writer->WriteUi16BE((ui16_t)Y) ) return false;
+           return true;
+         }
+       };
+
+      //
+      class ThreeColorPrimaries : public Kumu::IArchive
+       {
+       public:
+         ColorPrimary First;
+         ColorPrimary Second;
+         ColorPrimary Third;
+
+         ThreeColorPrimaries() {}
+         ~ThreeColorPrimaries() {}
+
+         ThreeColorPrimaries(const ColorPrimary& first, const ColorPrimary& second, const ColorPrimary& third) :
+           First(first), Second(second), Third(third) {}
+
+         ThreeColorPrimaries(const ThreeColorPrimaries& rhs) { Copy(rhs); }
+         const ThreeColorPrimaries& operator=(const ThreeColorPrimaries& rhs) { Copy(rhs); return *this; }
+         
+         void Copy(const ThreeColorPrimaries& rhs) {
+           First = rhs.First;
+           Second = rhs.Second;
+           Third = rhs.Third;
+         }
+
+         //
+         inline const char* EncodeString(char* str_buf, ui32_t buf_len) const {
+           snprintf(str_buf, buf_len, "%d,%d;%d,%d;%d,%d", First.X, First.Y, Second.X, Second.Y, Third.X, Third.Y);
+           return str_buf;
+         }
+
+         inline virtual bool Unarchive(Kumu::MemIOReader* Reader) {
+           First.Unarchive(Reader);
+           Second.Unarchive(Reader);
+           Third.Unarchive(Reader);
+           return true;
+         }
+
+         inline virtual bool HasValue() const {
+           return First.HasValue() || Second.HasValue() || Third.HasValue();
+         }
+
+         inline virtual ui32_t ArchiveLength() const {
+           return First.ArchiveLength()
+             + Second.ArchiveLength()
+             + Third.ArchiveLength();
+         }
+
+         inline virtual bool Archive(Kumu::MemIOWriter* Writer) const {
+           First.Archive(Writer);
+           Second.Archive(Writer);
+           Third.Archive(Writer);
+           return true;
+         }
+       };
+
       //
       class VersionType : public Kumu::IArchive
        {
@@ -512,6 +689,21 @@ namespace ASDCP
          const char* EncodeString(char* str_buf, ui32_t buf_len) const;
        };
 
+      //
+      class J2KExtendedCapabilitiesType : public Kumu::IArchive
+        {
+        public:
+         ui32_t Pcap;
+         Array<Kumu::ArchivableUi16> Ccap;
+       
+         bool HasValue() const { return true; }
+         ui32_t ArchiveLength() const { return 0; }
+
+         bool Archive(Kumu::MemIOWriter* Writer) const;
+         bool Unarchive(Kumu::MemIOReader* Reader);
+         const char* EncodeString(char* str_buf, ui32_t buf_len) const;
+      };
+
     } // namespace MXF
 } // namespace ASDCP