1/*
2 * Copyright (c) 2005, David McPaul
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 *  * Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright notice,
11 *    this list of conditions and the following disclaimer in the documentation
12 *    and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25#ifndef _MOV_ATOM_H
26#define _MOV_ATOM_H
27
28#include "QTStructs.h"
29
30#include <File.h>
31#include <MediaDefs.h>
32#include <MediaFormats.h>
33#include <SupportDefs.h>
34
35#include <map>
36
37
38/*
39AtomBase
40	theStream : Stream;
41	streamOffset : FilePtr;
42	atomOffset : FilePtr;
43	atomSize : int64;
44	atomType : int32;  // Can be a number or a 4 char FOURCC
45	atomChildren : Array of AtomBase;
46
47public
48	ProcessMetaData() - Reads in the basic Atom Meta Data
49				- Calls OnProcessMetaData()
50				- Calls ProcessMetaData on each child atom
51(ensures stream is correct for child via offset)
52	OnProcessMetaData() - Subclass override to read/set meta data
53
54	AddChild(AtomBase) - Adds a child atom to the children array
55
56	MoveToEnd() - Moves stream ptr to end of atom
57
58	GetTypeAsString() - returns the type as something the user can read*/
59
60class AtomBase;
61
62typedef AtomBase* AtomBasePtr;
63typedef std::map<uint32, AtomBasePtr, std::less<uint32> > AtomArray;
64
65class AtomBase {
66
67/*
68
69	This is the basic or standard atom.  It contains data describing some aspect of the file/stream
70
71*/
72
73private:
74	off_t	streamOffset;
75	off_t	atomOffset;
76	uint32	atomType;
77	uint64	atomSize;
78	char	fourcc[5];		// make this an alias to atomType
79	AtomBase *parentAtom;
80
81protected:
82	BPositionIO	*theStream;
83	void	Indent(uint32 pindent);
84
85public:
86			AtomBase(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
87	virtual	~AtomBase();
88
89	virtual bool IsContainer() {return false;};
90
91	virtual BPositionIO *OnGetStream();
92	BPositionIO *getStream();
93
94	bool IsExtended() {return false;};
95	bool IsEndOfAtom() {return (getStream()->Position() >= off_t(streamOffset + atomSize));};
96
97	// Is this a known atom type
98	bool IsKnown();
99
100	virtual void	DisplayAtoms(uint32 pindent);
101
102	uint64	getAtomSize() {return atomSize;};
103	uint32	getAtomType() {return atomType;};
104	off_t	getAtomOffset() {return atomOffset;};
105	off_t	getStreamOffset() {return streamOffset;};
106
107	uint64	getDataSize() {return atomSize - 8;};
108
109	uint64	getBytesRemaining();
110
111	bool	IsType(uint32 patomType) {return patomType == atomType;};
112
113	void	setAtomOffset(off_t patomOffset) {atomOffset = patomOffset;};
114	void	setStreamOffset(off_t pstreamOffset) {streamOffset = pstreamOffset;};
115
116	char 	*getAtomName();
117
118	virtual char	*OnGetAtomName();
119
120	//	ProcessMetaData() - Reads in the basic Atom Meta Data
121	//				- Calls OnProcessMetaData()
122	virtual void	ProcessMetaData();
123
124	//	OnProcessMetaData() - Subclass override to read/set meta data
125	virtual void	OnProcessMetaData();
126
127	// Move stream ptr to where atom ends in stream (return false on failure)
128	bool	MoveToEnd();
129
130	void	DisplayAtoms();
131
132	// Many atoms use an array header
133	void	ReadArrayHeader(array_header *pHeader);
134
135	void	setParent(AtomBase *pParent) {parentAtom = pParent;};
136	AtomBase *getParent() { return parentAtom;};
137
138	void	Read(uint64	*value);
139	void	Read(uint32	*value);
140	void	Read(uint16	*value);
141	void	Read(uint8	*value);
142	void	Read(char	*value, uint32 maxread);
143	void	Read(uint8	*value, uint32 maxread);
144};
145
146class AtomContainer : public AtomBase {
147
148/*
149
150	This is an Atom that contains other atoms.  It has children that may be Containter Atoms or Standard Atoms
151
152*/
153
154private:
155	AtomArray atomChildren;
156	uint32	TotalChildren;
157
158	virtual void DisplayAtoms(uint32 pindent);
159
160public:
161			AtomContainer(BPositionIO *pStream, off_t pstreamOffset, uint32 patomType, uint64 patomSize);
162	virtual	~AtomContainer();
163
164	virtual bool IsContainer() {return true;};
165	AtomBase *GetChildAtom(uint32 patomType, uint32 offset=0);
166	uint32	CountChildAtoms(uint32 patomType);
167
168	//	ProcessMetaData() - Reads in the basic Atom Meta Data
169	//				- Calls OnProcessMetaData()
170	//				- Calls ProcessMetaData on each child atom
171	//				(ensures stream is correct for child via offset)
172	virtual void	ProcessMetaData();
173
174	// Add a atom to the children array (return false on failure)
175	bool	AddChild(AtomBase *pChildAtom);
176
177	//	OnProcessMetaData() - Subclass override to read/set meta data
178	virtual void	OnProcessMetaData();
179	virtual void	OnChildProcessingComplete() {};
180};
181
182extern AtomBase *getAtom(BPositionIO *pStream);
183
184#endif // _MOV_ATOM_H
185