2 Copyright (c) 2005-2006, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <KM_fileio.h>
43 const ui32_t MXF_BER_LENGTH = 4;
44 const ui32_t MXF_TAG_LENGTH = 2;
45 const ui32_t SMPTE_UL_LENGTH = 16;
46 const ui32_t SMPTE_UMID_LENGTH = 32;
47 const byte_t SMPTE_UL_START[4] = { 0x06, 0x0e, 0x2b, 0x34 };
48 const ui32_t MAX_KLV_PACKET_LENGTH = 1024*1024*64;
50 const ui32_t IdentBufferLen = 128;
52 const ui32_t IntBufferLen = 64;
54 inline const char* i64sz(i64_t i, char* buf)
58 snprintf(buf, IntBufferLen, "%I64d", i);
60 snprintf(buf, IntBufferLen, "%lld", i);
65 inline const char* ui64sz(ui64_t i, char* buf)
69 snprintf(buf, IntBufferLen, "%I64u", i);
71 snprintf(buf, IntBufferLen, "%llu", i);
81 inline bool operator<(const TagValue& rhs) const {
82 if ( a < rhs.a ) return true;
83 if ( a == rhs.a && b < rhs.b ) return true;
87 inline bool operator==(const TagValue& rhs) const {
88 if ( a != rhs.a ) return false;
89 if ( b != rhs.b ) return false;
97 class UL : public Kumu::Identifier<SMPTE_UL_LENGTH>
101 UL(const UL& rhs) : Kumu::Identifier<SMPTE_UL_LENGTH>(rhs) {}
102 UL(const byte_t* value) : Kumu::Identifier<SMPTE_UL_LENGTH>(value) {}
105 const char* EncodeString(char* str_buf, ui32_t buf_len) const;
109 class UMID : public Kumu::Identifier<SMPTE_UMID_LENGTH>
113 UMID(const UMID& rhs) : Kumu::Identifier<SMPTE_UMID_LENGTH>(rhs) {}
114 UMID(const byte_t* value) : Kumu::Identifier<SMPTE_UMID_LENGTH>(value) {}
117 void MakeUMID(int Type);
118 void MakeUMID(int Type, const UUID& ID);
119 const char* EncodeString(char* str_buf, ui32_t buf_len) const;
122 const byte_t nil_UMID[SMPTE_UMID_LENGTH] = {0};
123 const UMID NilUMID(nil_UMID);
128 byte_t ul[SMPTE_UL_LENGTH];
138 static const MDDEntry* FindUL(const byte_t*);
139 static const MDDEntry* FindName(const char*);
140 static const MDDEntry& Type(MDD_t type_id);
141 static bool Replace(const MDDEntry& Entry);
142 static void Restore(const byte_t* ul);
143 static void RestoreAll();
145 inline static const byte_t* ul(MDD_t type_id) {
146 return Type(type_id).ul;
151 ASDCP_NO_COPY_CONSTRUCT(Dict);
164 virtual ~IPrimerLookup() {}
165 virtual void ClearTagList() = 0;
166 virtual Result_t InsertTag(const MDDEntry& Entry, ASDCP::TagValue& Tag) = 0;
167 virtual Result_t TagForKey(const ASDCP::UL& Key, ASDCP::TagValue& Tag) = 0;
173 ASDCP_NO_COPY_CONSTRUCT(KLVPacket);
176 const byte_t* m_KeyStart;
178 const byte_t* m_ValueStart;
179 ui32_t m_ValueLength;
182 KLVPacket() : m_KeyStart(0), m_KLLength(0), m_ValueStart(0), m_ValueLength(0) {}
183 virtual ~KLVPacket() {}
185 ui32_t PacketLength() {
186 return m_KLLength + m_ValueLength;
189 virtual bool HasUL(const byte_t*);
190 virtual Result_t InitFromBuffer(const byte_t*, ui32_t);
191 virtual Result_t InitFromBuffer(const byte_t*, ui32_t, const byte_t* label);
192 virtual Result_t WriteKLToBuffer(ASDCP::FrameBuffer&, const byte_t* label, ui32_t length);
193 virtual void Dump(FILE*, bool);
197 class KLVFilePacket : public KLVPacket
199 ASDCP_NO_COPY_CONSTRUCT(KLVFilePacket);
202 ASDCP::FrameBuffer m_Buffer;
206 virtual ~KLVFilePacket() {}
208 virtual Result_t InitFromFile(const Kumu::FileReader&);
209 virtual Result_t InitFromFile(const Kumu::FileReader&, const byte_t* label);
210 virtual Result_t WriteKLToFile(Kumu::FileWriter& Writer, const byte_t* label, ui32_t length);