Update copyright dates.
[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   const ui32_t MAX_KLV_PACKET_LENGTH = 1024*1024*64;
49
50   const ui32_t IdentBufferLen = 128;
51
52 const ui32_t IntBufferLen = 64;
53
54 inline const char* i64sz(i64_t i, char* buf)
55
56   assert(buf);
57 #ifdef WIN32
58   snprintf(buf, IntBufferLen, "%I64d", i);
59 #else
60   snprintf(buf, IntBufferLen, "%lld", i);
61 #endif
62   return buf;
63 }
64
65 inline const char* ui64sz(ui64_t i, char* buf)
66
67   assert(buf);
68 #ifdef WIN32
69   snprintf(buf, IntBufferLen, "%I64u", i);
70 #else
71   snprintf(buf, IntBufferLen, "%llu", i);
72 #endif
73   return buf;
74 }
75
76   struct TagValue
77   {
78     byte_t a;
79     byte_t b;
80
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;
84       return false;
85     }
86
87     inline bool operator==(const TagValue& rhs) const {
88       if ( a != rhs.a ) return false;
89       if ( b != rhs.b ) return false;
90       return true;
91     }
92   };
93
94   using Kumu::UUID;
95
96   // Universal Label
97   class UL : public Kumu::Identifier<SMPTE_UL_LENGTH>
98     {
99     public:
100       UL() {}
101       UL(const UL& rhs) : Kumu::Identifier<SMPTE_UL_LENGTH>(rhs) {}
102       UL(const byte_t* value) : Kumu::Identifier<SMPTE_UL_LENGTH>(value) {}
103       virtual ~UL() {}
104
105       const char* EncodeString(char* str_buf, ui32_t buf_len) const;
106     };
107
108   // UMID
109   class UMID : public Kumu::Identifier<SMPTE_UMID_LENGTH>
110     {
111     public:
112       UMID() {}
113       UMID(const UMID& rhs) : Kumu::Identifier<SMPTE_UMID_LENGTH>(rhs) {}
114       UMID(const byte_t* value) : Kumu::Identifier<SMPTE_UMID_LENGTH>(value) {}
115       virtual ~UMID() {}
116
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;
120     };
121
122   const byte_t nil_UMID[SMPTE_UMID_LENGTH] = {0};
123   const UMID NilUMID(nil_UMID);
124
125   //
126   struct MDDEntry
127   {
128     byte_t        ul[SMPTE_UL_LENGTH];
129     TagValue      tag;
130     bool          optional;
131     const char*   name;
132   };
133
134   //
135   class Dict
136     {
137     public:
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();
144
145       inline static const byte_t* ul(MDD_t type_id) {
146         return Type(type_id).ul;
147       }
148
149     private:
150       Dict* m_Dict;
151       ASDCP_NO_COPY_CONSTRUCT(Dict);
152
153     protected:
154       Dict();
155
156     public:
157       ~Dict();
158     };
159
160   //
161   class IPrimerLookup
162     {
163     public:
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;
168     };
169
170   //
171   class KLVPacket
172     {
173       ASDCP_NO_COPY_CONSTRUCT(KLVPacket);
174
175     protected:
176       const byte_t* m_KeyStart;
177       ui32_t        m_KLLength;
178       const byte_t* m_ValueStart;
179       ui32_t        m_ValueLength;
180
181     public:
182       KLVPacket() : m_KeyStart(0), m_KLLength(0), m_ValueStart(0), m_ValueLength(0) {}
183       virtual ~KLVPacket() {}
184
185       ui32_t  PacketLength() {
186         return m_KLLength + m_ValueLength;
187       }
188
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);
194     };
195
196   //
197   class KLVFilePacket : public KLVPacket
198     {
199       ASDCP_NO_COPY_CONSTRUCT(KLVFilePacket);
200
201     protected:
202       ASDCP::FrameBuffer m_Buffer;
203
204     public:
205       KLVFilePacket() {}
206       virtual ~KLVFilePacket() {}
207
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);
211     };
212
213 } // namespace ASDCP
214
215 #endif // _KLV_H_
216
217
218 //
219 // end KLV.h
220 //