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