summaryrefslogtreecommitdiff
path: root/XMPFiles/source/FormatSupport/WAVE/WAVEBehavior.h
blob: 4a7be500fb53821137af37c4ba0ae3afc86dee46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// =================================================================================================
// Copyright Adobe
// Copyright 2010 Adobe
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it. If you have received this file from a source other 
// than Adobe, then your use, modification, or distribution of it requires the prior written permission
// of Adobe.
// =================================================================================================

#ifndef _WAVEBEHAVIOR_h_
#define _WAVEBEHAVIOR_h_

#include "public/include/XMP_Environment.h"	// ! XMP_Environment.h must be the first included header.

#include "public/include/XMP_Const.h"
#include "public/include/XMP_IO.hpp"

#include "XMPFiles/source/FormatSupport/IFF/IChunkBehavior.h"
#include "XMPFiles/source/FormatSupport/IFF/ChunkPath.h"
#include "source/Endian.h"

namespace IFF_RIFF
{

/**
	WAVE behavior class.
	
	Implements the IChunkBehavior interface
*/


class WAVEBehavior : public IChunkBehavior
{
// Internal structure to hold RF64 related data
public:
#if SUNOS_SPARC || SUNOS_X86
#pragma pack ( 1 )
#else
#pragma pack ( push, 1 )
#endif //#if SUNOS_SPARC || SUNOS_X86
	struct ChunkSize64
	{
		XMP_Uns64 size;
		XMP_Uns32 id;
		// Ctor
		ChunkSize64(): size(0), id(0) {}
	};

	struct DS64
	{
		XMP_Uns64 riffSize;
		XMP_Uns64 dataSize;
		XMP_Uns64 sampleCount;
		XMP_Uns32 tableLength;
		// fix part ends here
		XMP_Uns32 trailingBytes;
		std::vector<ChunkSize64> table;

		// ctor
		DS64(): riffSize(0), dataSize(0), sampleCount(0), tableLength(0), trailingBytes(0) {}
	};
#if SUNOS_SPARC || SUNOS_X86
#pragma pack (  )
#else
#pragma pack ( pop )
#endif //#if SUNOS_SPARC || SUNOS_X86


	/**
		ctor/dtor
	*/
	WAVEBehavior() : mChunksAdded(0), mIsRF64(false), mDS64Data(NULL) {}
	
	virtual ~WAVEBehavior() 
	{
		if( mDS64Data != NULL )
		{
			delete mDS64Data;
		}
	}

	/**
		Validate the passed in size value, identify the valid size if the passed in isn't valid
		and return the valid size.
		throw an exception if the passed in size isn't valid and there's no way to identify a
		valid size.

		@param	size	Size value
		@param	id		Identifier of chunk
		@param	tree	Chunk tree
		@param	stream	Stream handle

		@return		Valid size value.
	*/
	XMP_Uns64		getRealSize( const XMP_Uns64 size, const ChunkIdentifier& id, IChunkContainer& tree, XMP_IO* stream );

	/**
		Return the maximum size of a single chunk, i.e. the maximum size of a top-level chunk.

		@return		Maximum size
	*/
	XMP_Uns64		getMaxChunkSize() const;

	/**
		Return true if the passed identifier is valid for top-level chunks of a certain format.

		@param	id		Chunk identifier
		@param	chunkNo	order number of top-level chunk
		@return			true, if passed id is a valid top-level chunk
	*/
	bool			isValidTopLevelChunk( const ChunkIdentifier& id, XMP_Uns32 chunkNo );

	/**
		Fix the hierarchy of chunks depending ones based on size changes of one or more chunks
		and second based on format specific rules.
		Throw an exception if the hierarchy can't be fixed.

		@param	tree		Vector of root chunks.
	*/
	void			fixHierarchy( IChunkContainer& tree );

	/**
		Insert a new chunk into the hierarchy of chunks. The behavior needs to decide the position
		of the new chunk and has to do the insertion.

		@param	tree	Chunk tree
		@param	chunk	New chunk
	*/
	void			insertChunk( IChunkContainer& tree, Chunk& chunk )	;

	/**
		Remove the chunk described by the passed ChunkPath.

		@param	tree	Chunk tree
		@param	path	Path to the chunk that needs to be removed

		@return			true if the chunk was removed and need to be deleted
	*/
	bool			removeChunk( IChunkContainer& tree, Chunk& chunk )	;

protected:
		/**
		Create a FREE chunk.
		If the chunkSize is smaller than the header+type - size then create an annotation chunk.
		If the passed size is odd, then add a pad byte.

		@param chunkSize	Total size including header
		@return				New FREE chunk
	*/
	Chunk*			createFREE( XMP_Uns64 chunkSize );
	
	/**
		Check if the passed chunk is a FREE chunk.
		(Could be also a small annotation chunk with zero bytes in its data)

		@param	chunk	A chunk

		@return			true if the passed chunk is a FREE chunk
	*/
	XMP_Bool		isFREEChunk( const Chunk& chunk ) const;

	/**
		Return the minimum size of a FREE chunk
	*/
	XMP_Uns64		getMinFREESize( ) const;

	/**
		Is the current file a RF64 file?

		@param tree		The whole chunk tree beginning with the root node

		@return			true if the current file is in the RF64 format
	*/
	bool			isRF64( const IChunkContainer& tree );

	/**
		Return RF64 structure.

		If the related chunk ('ds64') is not yet parsed then it should be parsed by this method.

		@param tree		The chunk tree (probably a subtree)
		@param stream	File stream

		@return			DS64 structure
	*/
	DS64*			getDS64( IChunkContainer& tree, XMP_IO* stream );

	/**
		update the RF64 chunk (if this is RF64) based on the current chunk sizes
	*/
	void			updateRF64( IChunkContainer& tree );

	/**
	 * Parses the data block of the given RF64 chunk into the internal data structures
	 * @param rf64Chunk the RF64 Chunk
	 * @param rf64 OUT the RF64 data structure 
	 * @return The parsing was successful (true) or not (false)
	 */
	bool			parseDS64Chunk( const Chunk& ds64Chunk, DS64& rf64 );

	/**
	 * Serializes the internal RF64 data structures into the data part of the given chunk
	 * @param rf64 the RF64 data structure
	 * @param rf64Chunk OUT the RF64 Chunk
	 * @return The serialization was successful (true) or not (false)
	 */
	bool			serializeDS64Chunk( const WAVEBehavior::DS64& rf64, Chunk& ds64Chunk );

private:
	void			doUpdateRF64( Chunk& chunk );

private:
	XMP_Uns32	mChunksAdded;
	bool		mIsRF64;
	DS64*		mDS64Data;
	
	static const LittleEndian& mEndian;				// WAVE is always Little Endian
	static const XMP_Uns32 kNormalRF64ChunkSize = 0xFFFFFFFF;
	static const XMP_Uns32 kMinimumDS64ChunkSize = 28;
}; // IFF_RIFF

}
#endif