code-generated metadata!
[asdcplib.git] / src / MXF.h
1 /*
2 Copyright (c) 2005-2006, 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    MXF.h
28     \version $Id$
29     \brief   MXF objects
30 */
31
32 #ifndef _MXF_H_
33 #define _MXF_H_
34
35 #include "MXFTypes.h"
36
37 namespace ASDCP
38 {
39   namespace MXF
40     {
41       // seek an open file handle to the start of the RIP KLV packet
42       Result_t SeekToRIP(const FileReader&);
43       
44       //
45       class RIP : public ASDCP::KLVFilePacket
46         {
47           ASDCP_NO_COPY_CONSTRUCT(RIP);
48
49         public:
50           //
51           class Pair {
52           public:
53             ui32_t BodySID;
54             ui64_t ByteOffset;
55
56             ui32_t Size() { return sizeof(ui32_t) + sizeof(ui64_t); }
57
58             inline const char* ToString(char* str_buf) const {
59               char intbuf[IntBufferLen];
60               sprintf(str_buf, "%-6lu: %s", BodySID, ui64sz(ByteOffset, intbuf));
61               return str_buf;
62             }
63
64             inline Result_t ReadFrom(ASDCP::MemIOReader& Reader) {
65               Result_t result = Reader.ReadUi32BE(&BodySID);
66
67               if ( ASDCP_SUCCESS(result) )
68                 result = Reader.ReadUi64BE(&ByteOffset);
69
70               return result;
71             }
72
73             inline Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
74               Result_t result = Writer.WriteUi32BE(BodySID);
75
76               if ( ASDCP_SUCCESS(result) )
77                 result = Writer.WriteUi64BE(ByteOffset);
78
79               return result;
80             }
81           };
82
83           Array<Pair> PairArray;
84
85           RIP() {}
86           virtual ~RIP() {}
87           virtual Result_t InitFromFile(const ASDCP::FileReader& Reader);
88           virtual Result_t WriteToFile(ASDCP::FileWriter& Writer);
89           virtual void     Dump(FILE* = 0);
90         };
91
92
93       //
94       class Partition : public ASDCP::KLVFilePacket
95         {       
96           ASDCP_NO_COPY_CONSTRUCT(Partition);
97
98         public:
99           ui16_t    MajorVersion;
100           ui16_t    MinorVersion;
101           ui32_t    KAGSize;
102           ui64_t    ThisPartition;
103           ui64_t    PreviousPartition;
104           ui64_t    FooterPartition;
105           ui64_t    HeaderByteCount;
106           ui64_t    IndexByteCount;
107           ui32_t    IndexSID;
108           ui64_t    BodyOffset;
109           ui32_t    BodySID;
110           UL        OperationalPattern;
111           Batch<UL> EssenceContainers;
112
113           Partition() {}
114           virtual ~Partition() {}
115           virtual Result_t InitFromFile(const ASDCP::FileReader& Reader);
116           virtual Result_t WriteToFile(ASDCP::FileWriter& Writer);
117           virtual void     Dump(FILE* = 0);
118         };
119
120
121       //
122       class Primer : public ASDCP::KLVPacket, public ASDCP::IPrimerLookup
123         {
124           class h__PrimerLookup;
125           mem_ptr<h__PrimerLookup> m_Lookup;
126           ASDCP_NO_COPY_CONSTRUCT(Primer);
127
128         public:
129           //
130           class LocalTagEntry
131             {
132             public:
133               TagValue    Tag;
134               ASDCP::UL   UL;
135
136               inline const char* ToString(char* str_buf) const {
137                 sprintf(str_buf, "%02x %02x: ", Tag.a, Tag.b);
138                 UL.ToString(str_buf + strlen(str_buf));
139                 return str_buf;
140               }
141
142               inline Result_t ReadFrom(ASDCP::MemIOReader& Reader) {
143                 Result_t result = Reader.ReadUi8(&Tag.a);
144                 
145                 if ( ASDCP_SUCCESS(result) )
146                   result = Reader.ReadUi8(&Tag.b);
147
148                 if ( ASDCP_SUCCESS(result) )
149                   result = UL.ReadFrom(Reader);
150
151                 return result;
152               }
153
154               inline Result_t WriteTo(ASDCP::MemIOWriter& Writer) {
155                 Result_t result = Writer.WriteUi8(Tag.a);
156                 
157                 if ( ASDCP_SUCCESS(result) )
158                   result = Writer.WriteUi8(Tag.b);
159
160                 if ( ASDCP_SUCCESS(result) )
161                   result = UL.WriteTo(Writer);
162
163                 return result;
164               }
165             };
166
167           Batch<LocalTagEntry> LocalTagEntryBatch;
168
169           Primer();
170           virtual ~Primer();
171
172           virtual void     ClearTagList();
173           virtual Result_t InsertTag(const ASDCP::UL& Key, ASDCP::TagValue& Tag);
174           virtual Result_t TagForKey(const ASDCP::UL& Key, ASDCP::TagValue& Tag);
175
176           virtual Result_t InitFromBuffer(const byte_t* p, ui32_t l);
177           virtual Result_t WriteToBuffer(ASDCP::FrameBuffer&);
178           virtual void     Dump(FILE* = 0);
179         };
180
181
182       //
183       class InterchangeObject : public ASDCP::KLVPacket
184         {
185         public:
186           IPrimerLookup* m_Lookup;
187           UID            InstanceUID;
188           UUID           GenerationUID;
189
190           InterchangeObject() : m_Lookup(0) {}
191           virtual ~InterchangeObject() {}
192           virtual Result_t InitFromTLVSet(TLVReader& TLVSet);
193           virtual Result_t WriteToBuffer(ASDCP::FrameBuffer&);
194           virtual bool     IsA(const byte_t* label);
195
196           virtual void Dump(FILE* stream = 0) {
197             KLVPacket::Dump(stream, true);
198           }
199         };
200
201       //
202       InterchangeObject* CreateObject(const byte_t* label);
203
204
205       //
206       class Preface : public InterchangeObject
207         {
208           ASDCP_NO_COPY_CONSTRUCT(Preface);
209
210         public:
211           UUID         GenerationUID;
212           Timestamp    LastModifiedDate;
213           ui16_t       Version;
214           ui32_t       ObjectModelVersion;
215           UID          PrimaryPackage;
216           Batch<UID>   Identifications;
217           UID          ContentStorage;
218           UL           OperationalPattern;
219           Batch<UL>    EssenceContainers;
220           Batch<UL>    DMSchemes;
221
222           Preface() {}
223           virtual ~Preface() {}
224           virtual Result_t InitFromBuffer(const byte_t* p, ui32_t l);
225           virtual Result_t WriteToBuffer(ASDCP::FrameBuffer& Buffer);
226           virtual void     Dump(FILE* = 0);
227         };
228
229       //
230       class IndexTableSegment : public InterchangeObject
231         {
232           ASDCP_NO_COPY_CONSTRUCT(IndexTableSegment);
233
234         public:
235           //
236           class DeltaEntry
237             {
238             public:
239               i8_t    PosTableIndex;
240               ui8_t   Slice;
241               ui32_t  ElementData;
242
243               Result_t ReadFrom(ASDCP::MemIOReader& Reader);
244               Result_t WriteTo(ASDCP::MemIOWriter& Writer);
245               inline const char* ToString(char* str_buf) const;
246             };
247
248           //
249           class IndexEntry
250             {
251             public:
252               i8_t               TemporalOffset;
253               i8_t               KeyFrameOffset;
254               ui8_t              Flags;
255               ui64_t             StreamOffset;
256               std::list<ui32_t>  SliceOffset;
257               Array<Rational>    PosTable;
258
259               Result_t ReadFrom(ASDCP::MemIOReader& Reader);
260               Result_t WriteTo(ASDCP::MemIOWriter& Writer);
261               inline const char* ToString(char* str_buf) const;
262             };
263
264           Rational    IndexEditRate;
265           ui64_t      IndexStartPosition;
266           ui64_t      IndexDuration;
267           ui32_t      EditUnitByteCount;
268           ui32_t      IndexSID;
269           ui32_t      BodySID;
270           ui8_t       SliceCount;
271           ui8_t       PosTableCount;
272           Batch<DeltaEntry> DeltaEntryArray;
273           Batch<IndexEntry> IndexEntryArray;
274
275           IndexTableSegment();
276           virtual ~IndexTableSegment();
277           virtual Result_t InitFromBuffer(const byte_t* p, ui32_t l);
278           virtual Result_t WriteToBuffer(ASDCP::FrameBuffer& Buffer);
279           virtual void     Dump(FILE* = 0);
280         };
281
282       //---------------------------------------------------------------------------------
283       //
284       class h__PacketList; // See MXF.cpp
285       class Identification;
286       class SourcePackage;
287
288       //
289       class OPAtomHeader : public Partition
290         {
291           mem_ptr<h__PacketList>   m_PacketList;
292           ASDCP_NO_COPY_CONSTRUCT(OPAtomHeader);
293
294         public:
295           ASDCP::MXF::RIP          m_RIP;
296           ASDCP::MXF::Primer       m_Primer;
297           InterchangeObject*       m_Preface;
298           ASDCP::FrameBuffer       m_Buffer;
299           bool                     m_HasRIP;
300
301           OPAtomHeader();
302           virtual ~OPAtomHeader();
303           virtual Result_t InitFromFile(const ASDCP::FileReader& Reader);
304           virtual Result_t WriteToFile(ASDCP::FileWriter& Writer, ui32_t HeaderLength = 16384);
305           virtual void     Dump(FILE* = 0);
306           virtual Result_t GetMDObjectByType(const byte_t*, InterchangeObject** = 0);
307           Identification*  GetIdentification();
308           SourcePackage*   GetSourcePackage();
309         };
310
311       //
312       class OPAtomIndexFooter : public Partition
313         {
314           mem_ptr<h__PacketList>   m_PacketList;
315           ASDCP::FrameBuffer       m_Buffer;
316           ASDCP_NO_COPY_CONSTRUCT(OPAtomIndexFooter);
317
318         public:
319           IPrimerLookup* m_Lookup;
320          
321           OPAtomIndexFooter();
322           virtual ~OPAtomIndexFooter();
323           virtual Result_t InitFromFile(const ASDCP::FileReader& Reader);
324           virtual Result_t WriteToFile(ASDCP::FileWriter& Writer);
325           virtual void     Dump(FILE* = 0);
326
327           virtual Result_t Lookup(ui32_t frame_num, IndexTableSegment::IndexEntry&);
328         };
329
330     } // namespace MXF
331 } // namespace ASDCP
332
333
334 #endif // _MXF_H_
335
336 //
337 // end MXF.h
338 //