02efbb1e54e2dc7fb63088040c1dd9fe4f429402
[asdcplib.git] / src / MXFTypes.h
1 //
2 // MXFTypes.h
3 //
4
5 #ifndef _MXFTYPES_H_
6 #define _MXFTYPES_H_
7
8
9
10 #endif //_MXFTYPES_H_
11
12 #include "KLV.h"
13 #include <list>
14 #include <vector>
15 #include <map>
16 #include <wchar.h>
17
18 // used with TLVReader::Read*
19 //
20 // these are used below to manufacture arguments
21 #define OBJ_READ_ARGS(s,l) s_MDD_Table[MDDindex_##s##_##l], &l
22 #define OBJ_READ_ARGS_R(s,l,r) s_MDD_Table[MDDindex_##s##_##l], &r
23
24 #define OBJ_WRITE_ARGS(s,l) s_MDD_Table[MDDindex_##s##_##l], &l
25
26 #define OBJ_TYPE_ARGS(t) s_MDD_Table[MDDindex_##t].ul
27
28
29 namespace ASDCP
30 {
31   namespace MXF
32     {
33       typedef std::pair<ui32_t, ui32_t> ItemInfo;
34       typedef std::map<TagValue, ItemInfo> TagMap;
35
36       //      
37       class TLVReader : public ASDCP::MemIOReader
38         {
39
40           TagMap         m_ElementMap;
41           IPrimerLookup* m_Lookup;
42
43           TLVReader();
44           ASDCP_NO_COPY_CONSTRUCT(TLVReader);
45           bool FindTL(const MDDEntry&);
46
47         public:
48           TLVReader(const byte_t* p, ui32_t c, IPrimerLookup* = 0);
49           Result_t ReadObject(const MDDEntry&, IArchive*);
50           Result_t ReadUi8(const MDDEntry&, ui8_t*);
51           Result_t ReadUi16(const MDDEntry&, ui16_t*);
52           Result_t ReadUi32(const MDDEntry&, ui32_t*);
53           Result_t ReadUi64(const MDDEntry&, ui64_t*);
54         };
55
56       //      
57       class TLVWriter : public ASDCP::MemIOWriter
58         {
59
60           TagMap         m_ElementMap;
61           IPrimerLookup* m_Lookup;
62
63           TLVWriter();
64           ASDCP_NO_COPY_CONSTRUCT(TLVWriter);
65           Result_t WriteTag(const MDDEntry&);
66
67         public:
68           TLVWriter(byte_t* p, ui32_t c, IPrimerLookup* = 0);
69           Result_t WriteObject(const MDDEntry&, IArchive*);
70           Result_t WriteUi8(const MDDEntry&, ui8_t*);
71           Result_t WriteUi16(const MDDEntry&, ui16_t*);
72           Result_t WriteUi32(const MDDEntry&, ui32_t*);
73           Result_t WriteUi64(const MDDEntry&, ui64_t*);
74         };
75
76       //
77       template <class T>
78         class Batch : public std::vector<T>, public IArchive
79         {
80         public:
81           ui32_t ItemCount;
82           ui32_t ItemSize;
83
84           Batch() : ItemCount(0), ItemSize(0) { ItemSize = sizeof(T); }
85           ~Batch() {}
86
87           //
88           Result_t ReadFrom(ASDCP::MemIOReader& Reader) {
89             Result_t result = Reader.ReadUi32BE(&ItemCount);
90
91             if ( ASDCP_SUCCESS(result) )
92               result = Reader.ReadUi32BE(&ItemSize);
93
94             if ( ( ItemCount > 65536 ) || ( ItemSize > 1024 ) )
95               return RESULT_FAIL;
96
97             for ( ui32_t i = 0; i < ItemCount && ASDCP_SUCCESS(result); i++ )
98               {
99                 T Tmp;
100                 result = Tmp.ReadFrom(Reader);
101
102                 if ( ASDCP_SUCCESS(result) )
103                   push_back(Tmp);
104               }
105
106             return result;
107           }
108
109           //
110           Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
111             Result_t result = Writer.WriteUi32BE(size());
112
113             if ( ASDCP_SUCCESS(result) )
114               result = Writer.WriteUi32BE(ItemSize);
115
116             typename std::vector<T>::iterator l_i = begin();
117             for ( ; l_i != end() && ASDCP_SUCCESS(result); l_i++ )
118               result = (*l_i).WriteTo(Writer);
119
120             return result;
121           }
122
123           //
124           void Dump(FILE* stream = 0, ui32_t depth = 0)
125             {
126               char identbuf[IdentBufferLen];
127
128               if ( stream == 0 )
129                 stream = stderr;
130
131               typename std::vector<T>::iterator i = this->begin();
132               for ( ; i != this->end(); i++ )
133                 fprintf(stream, "  %s\n", (*i).ToString(identbuf));
134             }
135         };
136
137       //
138       template <class T>
139         class Array : public std::list<T>, public IArchive
140         {
141         public:
142           Array() {}
143           ~Array() {}
144
145           //
146           Result_t ReadFrom(ASDCP::MemIOReader& Reader)
147             {
148               while ( Reader.Remainder() > 0 )
149                 {
150                   T Tmp;
151                   Tmp.ReadFrom(Reader);
152                   push_back(Tmp);
153                 }
154
155               return RESULT_OK;
156             }
157
158           //
159           Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
160             Result_t result = RESULT_OK;
161             typename std::list<T>::iterator l_i = begin();
162
163             for ( ; l_i != end() && ASDCP_SUCCESS(result); l_i++ )
164               result = (*l_i).WriteTo(Writer);
165
166             return result;
167           }
168
169           //
170           void Dump(FILE* stream = 0, ui32_t depth = 0)
171             {
172               char identbuf[IdentBufferLen];
173
174               if ( stream == 0 )
175                 stream = stderr;
176
177               typename std::list<T>::iterator i = this->begin();
178               for ( ; i != this->end(); i++ )
179                 fprintf(stream, "  %s\n", (*i).ToString(identbuf));
180             }
181         };
182
183       //
184       class Timestamp : public IArchive
185         {
186         public:
187           ui16_t Year;
188           ui8_t  Month;
189           ui8_t  Day;
190           ui8_t  Hour;
191           ui8_t  Minute;
192           ui8_t  Second;
193           ui8_t  mSec_4;
194
195           Timestamp() :
196             Year(0), Month(0),  Day(0),
197             Hour(0), Minute(0), Second(0), mSec_4(0) {}
198
199           //
200           inline const char* ToString(char* str_buf) const {
201             sprintf(str_buf, "%04hu-%02hu-%02hu %02hu:%02hu:%02hu.%03hu",
202                     Year, Month, Day, Hour, Minute, Second, mSec_4);
203             return str_buf;
204           }
205
206           //
207           inline Result_t ReadFrom(ASDCP::MemIOReader& Reader) {
208             Result_t result = Reader.ReadUi16BE(&Year);
209
210             if ( ASDCP_SUCCESS(result) )
211               result = Reader.ReadRaw(&Month, 6);
212
213             return result;
214           }
215
216           //
217           inline Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
218             Result_t result = Writer.WriteUi16BE(Year);
219
220             if ( ASDCP_SUCCESS(result) )
221               result = Writer.WriteRaw(&Month, 6);
222
223             return result;
224           }
225         };
226
227       //
228       class UTF16String : public IArchive
229         {
230           ui16_t m_length;
231           char   m_buffer[IdentBufferLen];
232           
233         public:
234           UTF16String() : m_length(0) { *m_buffer = 0; }
235           ~UTF16String() {}
236
237           //
238           const char* ToString(char* str_buf) const {
239             strncpy(str_buf, m_buffer, m_length+1);
240             return str_buf;
241           }
242
243           Result_t ReadFrom(ASDCP::MemIOReader& Reader);
244           Result_t WriteTo(ASDCP::MemIOWriter& Writer);
245         };
246
247       //
248       class Rational : public ASDCP::Rational, public IArchive
249         {
250         public:
251           Rational() {}
252           ~Rational() {}
253
254           //
255           const char* ToString(char* str_buf) const {
256             sprintf(str_buf, "%lu/%lu", Numerator, Denominator);
257             return str_buf;
258           }
259
260           Result_t ReadFrom(ASDCP::MemIOReader& Reader) {
261             Result_t result = Reader.ReadUi32BE((ui32_t*)&Numerator);
262
263             if ( ASDCP_SUCCESS(result) )
264               result = Reader.ReadUi32BE((ui32_t*)&Denominator);
265             
266             return result;
267           }
268
269           Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
270             Result_t result = Writer.WriteUi32BE((ui32_t)Numerator);
271
272             if ( ASDCP_SUCCESS(result) )
273               result = Writer.WriteUi32BE((ui32_t)Denominator);
274             
275             return result;
276           }
277         };
278
279     } // namespace MXF
280 } // namespace ASDCP
281
282
283 //
284 // end MXFTypes.h
285 //