massive dictionary re-factoring
[asdcplib.git] / src / KLV.h
1 /*
2 Copyright (c) 2005-2009, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
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.
15
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.
26 */
27 /*! \file    KLV.h
28   \version $Id$
29   \brief   KLV objects
30 */
31
32 #ifndef _KLV_H_
33 #define _KLV_H_
34
35 #include <KM_fileio.h>
36 #include <KM_memio.h>
37 #include "AS_DCP.h"
38 #include "MDD.h"
39
40
41 namespace ASDCP
42 {
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
49 #ifndef MAX_KLV_PACKET_LENGTH
50   const ui32_t MAX_KLV_PACKET_LENGTH = 1024*1024*64;
51 #endif
52
53   const ui32_t IdentBufferLen = 128;
54   const ui32_t IntBufferLen = 64;
55
56 inline const char* i64sz(i64_t i, char* buf)
57
58   assert(buf);
59 #ifdef WIN32
60   snprintf(buf, IntBufferLen, "%I64d", i);
61 #else
62   snprintf(buf, IntBufferLen, "%lld", i);
63 #endif
64   return buf;
65 }
66
67 inline const char* ui64sz(ui64_t i, char* buf)
68
69   assert(buf);
70 #ifdef WIN32
71   snprintf(buf, IntBufferLen, "%I64u", i);
72 #else
73   snprintf(buf, IntBufferLen, "%llu", i);
74 #endif
75   return buf;
76 }
77
78   struct TagValue
79   {
80     byte_t a;
81     byte_t b;
82
83     inline bool operator<(const TagValue& rhs) const {
84       if ( a < rhs.a ) return true;
85       if ( a == rhs.a && b < rhs.b ) return true;
86       return false;
87     }
88
89     inline bool operator==(const TagValue& rhs) const {
90       if ( a != rhs.a ) return false;
91       if ( b != rhs.b ) return false;
92       return true;
93     }
94   };
95
96   using Kumu::UUID;
97
98   // Universal Label
99   class UL : public Kumu::Identifier<SMPTE_UL_LENGTH>
100     {
101     public:
102       UL() {}
103       UL(const UL& rhs) : Kumu::Identifier<SMPTE_UL_LENGTH>(rhs) {}
104       UL(const byte_t* value) : Kumu::Identifier<SMPTE_UL_LENGTH>(value) {}
105       virtual ~UL() {}
106
107       const char* EncodeString(char* str_buf, ui32_t buf_len) const;
108     };
109
110   // UMID
111   class UMID : public Kumu::Identifier<SMPTE_UMID_LENGTH>
112     {
113     public:
114       UMID() {}
115       UMID(const UMID& rhs) : Kumu::Identifier<SMPTE_UMID_LENGTH>(rhs) {}
116       UMID(const byte_t* value) : Kumu::Identifier<SMPTE_UMID_LENGTH>(value) {}
117       virtual ~UMID() {}
118
119       void MakeUMID(int Type);
120       void MakeUMID(int Type, const UUID& ID);
121       const char* EncodeString(char* str_buf, ui32_t buf_len) const;
122     };
123
124   const byte_t nil_UMID[SMPTE_UMID_LENGTH] = {0};
125   const UMID NilUMID(nil_UMID);
126
127   //
128   struct MDDEntry
129   {
130     byte_t        ul[SMPTE_UL_LENGTH];
131     TagValue      tag;
132     bool          optional;
133     const char*   name;
134   };
135
136   //
137   class Dictionary
138     {
139     public:
140 #if 0
141       static const MDDEntry* FindUL(const byte_t*);
142       static const MDDEntry* FindName(const char*);
143       static const MDDEntry& Type(MDD_t type_id);
144       static bool            Replace(const MDDEntry& Entry);
145       static void            Restore(const byte_t* ul);
146       static void            RestoreAll();
147
148       inline static const byte_t* ul(MDD_t type_id) {
149         return Type(type_id).ul;
150       }
151 #endif
152
153     private:
154       //      Dictionary* m_Dict;
155       ASDCP_NO_COPY_CONSTRUCT(Dictionary);
156
157
158     public:
159       Dictionary();
160       ~Dictionary();
161
162       const MDDEntry* FindUL(const byte_t*) const;
163
164       inline const byte_t* ul(MDD_t type_id) const {
165         return Type(type_id).ul;
166       }
167
168       const MDDEntry& Type(MDD_t type_id) const;
169     };
170
171
172   const Dictionary& DefaultSMPTEDict();
173   const Dictionary& DefaultInteropDict();
174   const Dictionary& DefaultCompositeDict();
175
176
177   //
178   class IPrimerLookup
179     {
180     public:
181       virtual ~IPrimerLookup() {}
182       virtual void     ClearTagList() = 0;
183       virtual Result_t InsertTag(const MDDEntry& Entry, ASDCP::TagValue& Tag) = 0;
184       virtual Result_t TagForKey(const ASDCP::UL& Key, ASDCP::TagValue& Tag) = 0;
185     };
186
187   //
188   class KLVPacket
189     {
190       ASDCP_NO_COPY_CONSTRUCT(KLVPacket);
191
192     protected:
193       const byte_t* m_KeyStart;
194       ui32_t        m_KLLength;
195       const byte_t* m_ValueStart;
196       ui32_t        m_ValueLength;
197
198     public:
199       KLVPacket() : m_KeyStart(0), m_KLLength(0), m_ValueStart(0), m_ValueLength(0) {}
200       virtual ~KLVPacket() {}
201
202       ui32_t  PacketLength() {
203         return m_KLLength + m_ValueLength;
204       }
205
206       virtual bool     HasUL(const byte_t*);
207       virtual Result_t InitFromBuffer(const byte_t*, ui32_t);
208       virtual Result_t InitFromBuffer(const byte_t*, ui32_t, const UL& label);
209       virtual Result_t WriteKLToBuffer(ASDCP::FrameBuffer&, const UL& label, ui32_t length);
210       virtual void     Dump(FILE*, const Dictionary& Dict, bool show_value);
211     };
212
213   //
214   class KLVFilePacket : public KLVPacket
215     {
216       ASDCP_NO_COPY_CONSTRUCT(KLVFilePacket);
217
218     protected:
219       ASDCP::FrameBuffer m_Buffer;
220
221     public:
222       KLVFilePacket() {}
223       virtual ~KLVFilePacket() {}
224
225       virtual Result_t InitFromFile(const Kumu::FileReader&);
226       virtual Result_t InitFromFile(const Kumu::FileReader&, const UL& label);
227       virtual Result_t WriteKLToFile(Kumu::FileWriter& Writer, const UL& label, ui32_t length);
228     };
229
230 } // namespace ASDCP
231
232 #endif // _KLV_H_
233
234
235 //
236 // end KLV.h
237 //