2 Copyright (c) 2005-2009, 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>
44 const ui32_t MXF_BER_LENGTH = 4;
45 const ui32_t MXF_TAG_LENGTH = 2;
46 const ui32_t SMPTE_UL_LENGTH = 16;
47 const ui32_t SMPTE_UMID_LENGTH = 32;
48 const byte_t SMPTE_UL_START[4] = { 0x06, 0x0e, 0x2b, 0x34 };
50 #ifndef MAX_KLV_PACKET_LENGTH
51 const ui32_t MAX_KLV_PACKET_LENGTH = 1024*1024*64;
54 const ui32_t IdentBufferLen = 128;
55 const ui32_t IntBufferLen = 64;
57 inline const char* i64sz(i64_t i, char* buf)
61 snprintf(buf, IntBufferLen, "%I64d", i);
63 snprintf(buf, IntBufferLen, "%lld", i);
68 inline const char* ui64sz(ui64_t i, char* buf)
72 snprintf(buf, IntBufferLen, "%I64u", i);
74 snprintf(buf, IntBufferLen, "%llu", i);
84 inline bool operator<(const TagValue& rhs) const {
85 if ( a < rhs.a ) return true;
86 if ( a == rhs.a && b < rhs.b ) return true;
90 inline bool operator==(const TagValue& rhs) const {
91 if ( a != rhs.a ) return false;
92 if ( b != rhs.b ) return false;
100 class UL : public Kumu::Identifier<SMPTE_UL_LENGTH>
104 UL(const UL& rhs) : Kumu::Identifier<SMPTE_UL_LENGTH>(rhs) {}
105 UL(const byte_t* value) : Kumu::Identifier<SMPTE_UL_LENGTH>(value) {}
108 const char* EncodeString(char* str_buf, ui32_t buf_len) const;
112 class UMID : public Kumu::Identifier<SMPTE_UMID_LENGTH>
116 UMID(const UMID& rhs) : Kumu::Identifier<SMPTE_UMID_LENGTH>(rhs) {}
117 UMID(const byte_t* value) : Kumu::Identifier<SMPTE_UMID_LENGTH>(value) {}
120 void MakeUMID(int Type);
121 void MakeUMID(int Type, const UUID& ID);
122 const char* EncodeString(char* str_buf, ui32_t buf_len) const;
125 const byte_t nil_UMID[SMPTE_UMID_LENGTH] = {0};
126 const UMID NilUMID(nil_UMID);
131 byte_t ul[SMPTE_UL_LENGTH];
137 const MDDEntry& MXFInterop_OPAtom_Entry();
138 const MDDEntry& SMPTE_390_OPAtom_Entry();
143 std::map<ASDCP::UL, ui32_t> m_md_lookup;
144 std::map<ui32_t, ASDCP::UL> m_md_rev_lookup;
145 MDDEntry m_MDD_Table[(ui32_t)ASDCP::MDD_Max];
147 ASDCP_NO_COPY_CONSTRUCT(Dictionary);
153 bool operator==(const Dictionary& rhs) const { return this == &rhs; }
156 bool AddEntry(const MDDEntry& Entry, ui32_t index);
157 bool DeleteEntry(ui32_t index);
159 const MDDEntry* FindUL(const byte_t*) const;
160 const MDDEntry& Type(MDD_t type_id) const;
162 inline const byte_t* ul(MDD_t type_id) const {
163 return Type(type_id).ul;
166 void Dump(FILE* = 0) const;
170 const Dictionary& DefaultSMPTEDict();
171 const Dictionary& DefaultInteropDict();
172 const Dictionary& DefaultCompositeDict();
179 virtual ~IPrimerLookup() {}
180 virtual void ClearTagList() = 0;
181 virtual Result_t InsertTag(const MDDEntry& Entry, ASDCP::TagValue& Tag) = 0;
182 virtual Result_t TagForKey(const ASDCP::UL& Key, ASDCP::TagValue& Tag) = 0;
188 ASDCP_NO_COPY_CONSTRUCT(KLVPacket);
191 const byte_t* m_KeyStart;
193 const byte_t* m_ValueStart;
194 ui32_t m_ValueLength;
197 KLVPacket() : m_KeyStart(0), m_KLLength(0), m_ValueStart(0), m_ValueLength(0) {}
198 virtual ~KLVPacket() {}
200 ui32_t PacketLength() {
201 return m_KLLength + m_ValueLength;
204 virtual bool HasUL(const byte_t*);
205 virtual Result_t InitFromBuffer(const byte_t*, ui32_t);
206 virtual Result_t InitFromBuffer(const byte_t*, ui32_t, const UL& label);
207 virtual Result_t WriteKLToBuffer(ASDCP::FrameBuffer&, const UL& label, ui32_t length);
208 virtual void Dump(FILE*, const Dictionary& Dict, bool show_value);
212 class KLVFilePacket : public KLVPacket
214 ASDCP_NO_COPY_CONSTRUCT(KLVFilePacket);
217 ASDCP::FrameBuffer m_Buffer;
221 virtual ~KLVFilePacket() {}
223 virtual Result_t InitFromFile(const Kumu::FileReader&);
224 virtual Result_t InitFromFile(const Kumu::FileReader&, const UL& label);
225 virtual Result_t WriteKLToFile(Kumu::FileWriter& Writer, const UL& label, ui32_t length);