X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2FMXFTypes.h;h=1fa6176d9cf253f0d260edbf8cdf78a8a29f6c02;hb=830570c46c4d39a8a5767f83875e3ef2f79ecc98;hp=02efbb1e54e2dc7fb63088040c1dd9fe4f429402;hpb=8095eaa320551b6795d0368c0ad0c227a3167caa;p=asdcplib.git diff --git a/src/MXFTypes.h b/src/MXFTypes.h index 02efbb1..1fa6176 100755 --- a/src/MXFTypes.h +++ b/src/MXFTypes.h @@ -1,14 +1,37 @@ -// -// MXFTypes.h -// +/* +Copyright (c) 2005-2006, John Hurst +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/*! \file MXFTypes.h + \version $Id$ + \brief MXF objects +*/ #ifndef _MXFTYPES_H_ #define _MXFTYPES_H_ - - -#endif //_MXFTYPES_H_ - #include "KLV.h" #include #include @@ -18,12 +41,9 @@ // used with TLVReader::Read* // // these are used below to manufacture arguments -#define OBJ_READ_ARGS(s,l) s_MDD_Table[MDDindex_##s##_##l], &l -#define OBJ_READ_ARGS_R(s,l,r) s_MDD_Table[MDDindex_##s##_##l], &r - -#define OBJ_WRITE_ARGS(s,l) s_MDD_Table[MDDindex_##s##_##l], &l - -#define OBJ_TYPE_ARGS(t) s_MDD_Table[MDDindex_##t].ul +#define OBJ_READ_ARGS(s,l) Dict::Type(MDD_##s##_##l), &l +#define OBJ_WRITE_ARGS(s,l) Dict::Type(MDD_##s##_##l), &l +#define OBJ_TYPE_ARGS(t) Dict::Type(MDD_##t).ul namespace ASDCP @@ -34,7 +54,7 @@ namespace ASDCP typedef std::map TagMap; // - class TLVReader : public ASDCP::MemIOReader + class TLVReader : public Kumu::MemIOReader { TagMap m_ElementMap; @@ -46,7 +66,7 @@ namespace ASDCP public: TLVReader(const byte_t* p, ui32_t c, IPrimerLookup* = 0); - Result_t ReadObject(const MDDEntry&, IArchive*); + Result_t ReadObject(const MDDEntry&, Kumu::IArchive*); Result_t ReadUi8(const MDDEntry&, ui8_t*); Result_t ReadUi16(const MDDEntry&, ui16_t*); Result_t ReadUi32(const MDDEntry&, ui32_t*); @@ -54,7 +74,7 @@ namespace ASDCP }; // - class TLVWriter : public ASDCP::MemIOWriter + class TLVWriter : public Kumu::MemIOWriter { TagMap m_ElementMap; @@ -66,7 +86,7 @@ namespace ASDCP public: TLVWriter(byte_t* p, ui32_t c, IPrimerLookup* = 0); - Result_t WriteObject(const MDDEntry&, IArchive*); + Result_t WriteObject(const MDDEntry&, Kumu::IArchive*); Result_t WriteUi8(const MDDEntry&, ui8_t*); Result_t WriteUi16(const MDDEntry&, ui16_t*); Result_t WriteUi32(const MDDEntry&, ui32_t*); @@ -75,47 +95,68 @@ namespace ASDCP // template - class Batch : public std::vector, public IArchive + class Batch : public std::vector, public Kumu::IArchive { public: - ui32_t ItemCount; - ui32_t ItemSize; - - Batch() : ItemCount(0), ItemSize(0) { ItemSize = sizeof(T); } + Batch() {} ~Batch() {} // - Result_t ReadFrom(ASDCP::MemIOReader& Reader) { - Result_t result = Reader.ReadUi32BE(&ItemCount); - - if ( ASDCP_SUCCESS(result) ) - result = Reader.ReadUi32BE(&ItemSize); + virtual bool Unarchive(Kumu::MemIOReader* Reader) { + ui32_t ItemCount, ItemSize; + if ( ! Reader->ReadUi32BE(&ItemCount) ) return false; + if ( ! Reader->ReadUi32BE(&ItemSize) ) return false; if ( ( ItemCount > 65536 ) || ( ItemSize > 1024 ) ) - return RESULT_FAIL; + return false; - for ( ui32_t i = 0; i < ItemCount && ASDCP_SUCCESS(result); i++ ) + bool result = true; + for ( ui32_t i = 0; i < ItemCount && result; i++ ) { T Tmp; - result = Tmp.ReadFrom(Reader); + result = Tmp.Unarchive(Reader); - if ( ASDCP_SUCCESS(result) ) + if ( result ) push_back(Tmp); } 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::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; + } + // - Result_t WriteTo(ASDCP::MemIOWriter& Writer) { - Result_t result = Writer.WriteUi32BE(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::const_iterator l_i = this->begin(); + assert(l_i != this->end()); - if ( ASDCP_SUCCESS(result) ) - result = Writer.WriteUi32BE(ItemSize); + ui32_t ItemSize = Writer->Remainder(); + if ( ! (*l_i).Archive(Writer) ) return false; + ItemSize -= Writer->Remainder(); + Kumu::i2p(KM_i32_BE(ItemSize), p); + l_i++; - typename std::vector::iterator l_i = begin(); - for ( ; l_i != end() && ASDCP_SUCCESS(result); l_i++ ) - result = (*l_i).WriteTo(Writer); + bool result = true; + for ( ; l_i != this->end() && result; l_i++ ) + result = (*l_i).Archive(Writer); return result; } @@ -130,38 +171,53 @@ namespace ASDCP typename std::vector::iterator i = this->begin(); for ( ; i != this->end(); i++ ) - fprintf(stream, " %s\n", (*i).ToString(identbuf)); + fprintf(stream, " %s\n", (*i).EncodeString(identbuf, IdentBufferLen)); } }; // template - class Array : public std::list, public IArchive + class Array : public std::list, public Kumu::IArchive { public: Array() {} ~Array() {} // - Result_t ReadFrom(ASDCP::MemIOReader& Reader) + virtual bool Unarchive(Kumu::MemIOReader* Reader) { - while ( Reader.Remainder() > 0 ) + bool result = true; + + while ( Reader->Remainder() > 0 && result ) { T Tmp; - Tmp.ReadFrom(Reader); + result = Tmp.Unarchive(Reader); push_back(Tmp); } - return RESULT_OK; + return result; } + inline virtual bool HasValue() const { return ! this->empty(); } + + virtual ui32_t ArchiveLength() const { + ui32_t arch_size = 0; + + typename std::list::const_iterator l_i = this->begin(); + + for ( ; l_i != this->end(); l_i++ ) + arch_size += l_i->ArchiveLength(); + + return arch_size; + } + // - Result_t WriteTo(ASDCP::MemIOWriter& Writer) { - Result_t result = RESULT_OK; - typename std::list::iterator l_i = begin(); + virtual bool Archive(Kumu::MemIOWriter* Writer) const { + bool result = true; + typename std::list::const_iterator l_i = this->begin(); - for ( ; l_i != end() && ASDCP_SUCCESS(result); l_i++ ) - result = (*l_i).WriteTo(Writer); + for ( ; l_i != this->end() && result; l_i++ ) + result = (*l_i).Archive(Writer); return result; } @@ -176,12 +232,12 @@ namespace ASDCP typename std::list::iterator i = this->begin(); for ( ; i != this->end(); i++ ) - fprintf(stream, " %s\n", (*i).ToString(identbuf)); + fprintf(stream, " %s\n", (*i).EncodeString(identbuf, IdentBufferLen)); } }; // - class Timestamp : public IArchive + class Timestamp : public Kumu::IArchive { public: ui16_t Year; @@ -190,96 +246,183 @@ namespace ASDCP ui8_t Hour; ui8_t Minute; ui8_t Second; - ui8_t mSec_4; - - Timestamp() : - Year(0), Month(0), Day(0), - Hour(0), Minute(0), Second(0), mSec_4(0) {} + ui8_t Tick; - // - inline const char* ToString(char* str_buf) const { - sprintf(str_buf, "%04hu-%02hu-%02hu %02hu:%02hu:%02hu.%03hu", - Year, Month, Day, Hour, Minute, Second, mSec_4); - return str_buf; - } + Timestamp(); + Timestamp(const Timestamp& rhs); + Timestamp(const char* datestr); + virtual ~Timestamp(); - // - inline Result_t ReadFrom(ASDCP::MemIOReader& Reader) { - Result_t result = Reader.ReadUi16BE(&Year); + const Timestamp& operator=(const Timestamp& rhs); + bool operator<(const Timestamp& rhs) const; + bool operator==(const Timestamp& rhs) const; + bool operator!=(const Timestamp& rhs) const; - if ( ASDCP_SUCCESS(result) ) - result = Reader.ReadRaw(&Month, 6); + // decode and set value from string formatted by EncodeAsString + Result_t SetFromString(const char* datestr); + + // add the given number of days or hours to the timestamp value. Values less than zero + // will cause the value to decrease + void AddDays(i32_t); + void AddHours(i32_t); - return result; - } + // Write the timestamp value to the given buffer in the form 2004-05-01 13:20:00.000 + // returns 0 if the buffer is smaller than DateTimeLen + const char* EncodeString(char* str_buf, ui32_t buf_len) const; // - inline Result_t WriteTo(ASDCP::MemIOWriter& Writer) { - Result_t result = Writer.WriteUi16BE(Year); + inline virtual bool Unarchive(Kumu::MemIOReader* Reader) { + if ( ! Reader->ReadUi16BE(&Year) ) return false; + if ( ! Reader->ReadRaw(&Month, 6) ) return false; + return true; + } - if ( ASDCP_SUCCESS(result) ) - result = Writer.WriteRaw(&Month, 6); + inline virtual bool HasValue() const { return true; } + inline virtual ui32_t ArchiveLength() const { return 8L; } - return result; + // + inline virtual bool Archive(Kumu::MemIOWriter* Writer) const { + if ( ! Writer->WriteUi16BE(Year) ) return false; + if ( ! Writer->WriteRaw(&Month, 6) ) return false; + return true; } }; // - class UTF16String : public IArchive + class UTF16String : public std::string, public Kumu::IArchive { - ui16_t m_length; - char m_buffer[IdentBufferLen]; - public: - UTF16String() : m_length(0) { *m_buffer = 0; } + UTF16String() {} ~UTF16String() {} - // - const char* ToString(char* str_buf) const { - strncpy(str_buf, m_buffer, m_length+1); - return str_buf; - } + const UTF16String& operator=(const char*); + const UTF16String& operator=(const std::string&); - Result_t ReadFrom(ASDCP::MemIOReader& Reader); - Result_t WriteTo(ASDCP::MemIOWriter& Writer); + 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(); } + virtual bool Unarchive(Kumu::MemIOReader* Reader); + virtual bool Archive(Kumu::MemIOWriter* Writer) const; }; // - class Rational : public ASDCP::Rational, public IArchive + class Rational : public ASDCP::Rational, public Kumu::IArchive { public: Rational() {} ~Rational() {} + Rational(const Rational& rhs) { + Numerator = rhs.Numerator; + Denominator = rhs.Denominator; + } + + const Rational& operator=(const Rational& rhs) { + Numerator = rhs.Numerator; + Denominator = rhs.Denominator; + return *this; + } + + Rational(const ASDCP::Rational& rhs) { + Numerator = rhs.Numerator; + Denominator = rhs.Denominator; + } + + const Rational& operator=(const ASDCP::Rational& rhs) { + Numerator = rhs.Numerator; + Denominator = rhs.Denominator; + return *this; + } + // - const char* ToString(char* str_buf) const { - sprintf(str_buf, "%lu/%lu", Numerator, Denominator); + inline const char* EncodeString(char* str_buf, ui32_t buf_len) const { + snprintf(str_buf, buf_len, "%d/%d", Numerator, Denominator); return str_buf; } - Result_t ReadFrom(ASDCP::MemIOReader& Reader) { - Result_t result = Reader.ReadUi32BE((ui32_t*)&Numerator); + inline virtual bool Unarchive(Kumu::MemIOReader* Reader) { + if ( ! Reader->ReadUi32BE((ui32_t*)&Numerator) ) return false; + if ( ! Reader->ReadUi32BE((ui32_t*)&Denominator) ) return false; + return true; + } - if ( ASDCP_SUCCESS(result) ) - result = Reader.ReadUi32BE((ui32_t*)&Denominator); - - return result; + inline virtual bool HasValue() const { return true; } + inline virtual ui32_t ArchiveLength() const { return sizeof(ui32_t)*2; } + + inline virtual bool Archive(Kumu::MemIOWriter* Writer) const { + if ( ! Writer->WriteUi32BE((ui32_t)Numerator) ) return false; + if ( ! Writer->WriteUi32BE((ui32_t)Denominator) ) return false; + return true; } + }; - Result_t WriteTo(ASDCP::MemIOWriter& Writer) { - Result_t result = Writer.WriteUi32BE((ui32_t)Numerator); + // + class VersionType : public Kumu::IArchive + { + ASDCP_NO_COPY_CONSTRUCT(VersionType); - if ( ASDCP_SUCCESS(result) ) - result = Writer.WriteUi32BE((ui32_t)Denominator); - - return result; + public: + enum Release_t { RL_UNKNOWN, RL_RELEASE, RL_DEVELOPMENT, RL_PATCHED, RL_BETA, RL_PRIVATE }; + ui16_t Major; + ui16_t Minor; + ui16_t Patch; + ui16_t Build; + Release_t Release; + + VersionType() : Major(0), Minor(0), Patch(0), Build(0), Release(RL_UNKNOWN) {} + ~VersionType() {} + void Dump(FILE* = 0); + + const char* EncodeString(char* str_buf, ui32_t buf_len) const { + snprintf(str_buf, buf_len, "%hu.%hu.%hu.%hur%hu", Major, Minor, Patch, Build, Release); + return str_buf; + } + + virtual bool Unarchive(Kumu::MemIOReader* Reader) { + if ( ! Reader->ReadUi16BE(&Major) ) return false; + if ( ! Reader->ReadUi16BE(&Minor) ) return false; + if ( ! Reader->ReadUi16BE(&Patch) ) return false; + if ( ! Reader->ReadUi16BE(&Build) ) return false; + ui16_t tmp_release; + if ( ! Reader->ReadUi16BE(&tmp_release) ) return false; + Release = (Release_t)tmp_release; + return true; } + + inline virtual bool HasValue() const { return true; } + inline virtual ui32_t ArchiveLength() const { return sizeof(ui16_t)*5; } + + virtual bool Archive(Kumu::MemIOWriter* Writer) const { + if ( ! Writer->WriteUi16BE(Major) ) return false; + if ( ! Writer->WriteUi16BE(Minor) ) return false; + if ( ! Writer->WriteUi16BE(Patch) ) return false; + if ( ! Writer->WriteUi16BE(Build) ) return false; + if ( ! Writer->WriteUi16BE((ui16_t)(Release & 0x0000ffffL)) ) return false; + return true; + } + }; + + // + class Raw : public Kumu::ByteString + { + ASDCP_NO_COPY_CONSTRUCT(Raw); + + public: + Raw(); + ~Raw(); + + // + virtual bool Unarchive(Kumu::MemIOReader* Reader); + virtual bool Archive(Kumu::MemIOWriter* Writer) const; + const char* EncodeString(char* str_buf, ui32_t buf_len) const; }; } // namespace MXF } // namespace ASDCP +#endif //_MXFTYPES_H_ + // // end MXFTypes.h //