summaryrefslogtreecommitdiff
path: root/engine/audio/private/VBRHeader.cpp
blob: ce554d43ad550908b31398c8b3f8fdad0d5ef726 (plain) (blame)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include "audio_pch.h"
#include "tier0/platform.h"
#include "MPAFile.h"	// also includes vbrheader.h
#include "tier0/dbg.h"

#ifndef MAKEFOURCC
    #define MAKEFOURCC(ch0, ch1, ch2, ch3)                              \
                ((uint32)(BYTE)(ch0) | ((uint32)(BYTE)(ch1) << 8) |   \
                ((uint32)(BYTE)(ch2) << 16) | ((uint32)(BYTE)(ch3) << 24 ))
#endif //defined(MAKEFOURCC)

// XING Header offset: 1. index = lsf, 2. index = mono
uint32 CVBRHeader::m_dwXINGOffsets[2][2] =
{
	// MPEG 1 (not mono, mono)
	{ 32 + MPA_HEADER_SIZE, 17 + MPA_HEADER_SIZE },
	// MPEG 2/2.5
	{  17 + MPA_HEADER_SIZE, 9 + MPA_HEADER_SIZE }
};

// first test with this static method, if it does exist
bool CVBRHeader::IsVBRHeaderAvailable( CMPAFile* pMPAFile, VBRHeaderType& HeaderType, uint32& dwOffset )
{
	Assert(pMPAFile);
	
	// where does VBR header begin (XING)
	uint32 dwNewOffset = dwOffset + m_dwXINGOffsets[pMPAFile->m_pMPAHeader->IsLSF()][pMPAFile->m_pMPAHeader->IsMono()];

	// check for XING header first
	if( CheckXING( pMPAFile, dwNewOffset ) )
	{
		HeaderType = XINGHeader;
		// seek offset back to header begin
		dwOffset = dwNewOffset - 4;
		return true;
	}

	// VBRI header always at fixed offset
	dwNewOffset = dwOffset + 32 + MPA_HEADER_SIZE;
	if( CheckVBRI( pMPAFile, dwNewOffset ) )
	{
		HeaderType = VBRIHeader;
		// seek offset back to header begin
		dwOffset = dwNewOffset - 4;
		return true;
	}
	HeaderType = NoHeader;
	return false;
}

CVBRHeader::CVBRHeader( CMPAFile* pMPAFile, VBRHeaderType HeaderType, uint32 dwOffset ) :
	m_pMPAFile( pMPAFile ), m_pnToc(NULL), m_HeaderType( HeaderType ), m_dwOffset(dwOffset), m_dwFrames(0), m_dwBytes(0)
{
	switch( m_HeaderType )
	{
		case NoHeader:
			// no Header found
			throw CMPAException( CMPAException::NoVBRHeader, pMPAFile->GetFilename(), NULL, false );
			break;
		case XINGHeader:
			if(	!ExtractXINGHeader( m_dwOffset ) )
				throw CMPAException( CMPAException::NoVBRHeader, pMPAFile->GetFilename(), NULL, false );
			break;
		case VBRIHeader:
			if( !ExtractVBRIHeader( m_dwOffset ) ) 
				throw CMPAException( CMPAException::NoVBRHeader, pMPAFile->GetFilename(), NULL, false );
			break;
	}
	// calc bitrate
	if( m_dwBytes > 0 && m_dwFrames > 0 )
	{
		// calc number of seconds
		m_dwBytesPerSec = m_pMPAFile->m_pMPAHeader->GetBytesPerSecond( m_dwFrames, m_dwBytes );
	}
	else	// incomplete header found
	{
		throw CMPAException( CMPAException::IncompleteVBRHeader, pMPAFile->GetFilename(), NULL, false );
	}
}

bool CVBRHeader::CheckID( CMPAFile* pMPAFile, char ch0, char ch1, char ch2, char ch3, uint32& dwOffset )
{
	return ( pMPAFile->ExtractBytes( dwOffset, 4 ) == MAKEFOURCC( ch3, ch2, ch1, ch0 ) );
}

bool CVBRHeader::CheckXING( CMPAFile* pMPAFile, uint32& dwOffset )
{
	// XING ID found?
	if( !CheckID( pMPAFile, 'X', 'i', 'n', 'g', dwOffset) && !CheckID( pMPAFile, 'I', 'n', 'f', 'o', dwOffset) )
		return false;
	return true;
}

bool CVBRHeader::CheckVBRI( CMPAFile* pMPAFile, uint32& dwOffset )
{
	// VBRI ID found?
	if( !CheckID( pMPAFile, 'V', 'B', 'R', 'I', dwOffset ) )
		return false;
	return true;
}


// currently not used
bool CVBRHeader::ExtractLAMETag( uint32 dwOffset )
{
	// LAME ID found?
	if( !CheckID( m_pMPAFile, 'L', 'A', 'M', 'E', dwOffset ) && !CheckID( m_pMPAFile, 'G', 'O', 'G', 'O', dwOffset ) )
		return false;

	return true;
}

bool CVBRHeader::ExtractXINGHeader( uint32 dwOffset )
{
	/* XING VBR-Header

	 size	description
	 4		'Xing' or 'Info'
	 4		flags (indicates which fields are used)
	 4		frames (optional)
	 4		bytes (optional)
	 100	toc (optional)
	 4		a VBR quality indicator: 0=best 100=worst (optional)
	
	*/
	if( !CheckXING( m_pMPAFile, dwOffset ) )
		return false;

	uint32 dwFlags;

	// get flags (mandatory in XING header)
	dwFlags = m_pMPAFile->ExtractBytes( dwOffset, 4 ); 

	// extract total number of frames in file
	if(dwFlags & FRAMES_FLAG)
		m_dwFrames = m_pMPAFile->ExtractBytes(dwOffset,4);

	// extract total number of bytes in file
	if(dwFlags & BYTES_FLAG) 
		m_dwBytes = m_pMPAFile->ExtractBytes(dwOffset,4);

	// extract TOC (for more accurate seeking)
	if (dwFlags & TOC_FLAG) 
	{
		m_dwTableSize = 100;
		m_pnToc = new int[m_dwTableSize];

		if( m_pnToc )
		{
			for(uint32 i=0;i<m_dwTableSize;i++)
				m_pnToc[i] = m_pMPAFile->ExtractBytes( dwOffset, 1 );
		}
	}

	m_dwQuality = (uint32)-1;
	if(dwFlags & VBR_SCALE_FLAG )
		m_dwQuality = m_pMPAFile->ExtractBytes(dwOffset, 4);
		
	return true;
}

bool CVBRHeader::ExtractVBRIHeader( uint32 dwOffset )
{
	/* FhG VBRI Header

	size	description
	4		'VBRI' (ID)
	2		version
	2		delay
	2		quality
	4		# bytes
	4		# frames
	2		table size (for TOC)
	2		table scale (for TOC)
	2		size of table entry (max. size = 4 byte (must be stored in an integer))
	2		frames per table entry
	
	??		dynamic table consisting out of frames with size 1-4
			whole length in table size! (for TOC)

	*/

	if( !CheckVBRI( m_pMPAFile, dwOffset ) )
		return false;

	// extract all fields from header (all mandatory)
	m_dwVersion = m_pMPAFile->ExtractBytes(dwOffset, 2 );
	m_fDelay = (float)m_pMPAFile->ExtractBytes(dwOffset, 2 );
	m_dwQuality = m_pMPAFile->ExtractBytes(dwOffset, 2 );
	m_dwBytes = m_pMPAFile->ExtractBytes(dwOffset, 4 );
	m_dwFrames = m_pMPAFile->ExtractBytes(dwOffset, 4 );
	m_dwTableSize = m_pMPAFile->ExtractBytes(dwOffset, 2 ) + 1;	//!!!
	m_dwTableScale = m_pMPAFile->ExtractBytes(dwOffset, 2 );
	m_dwBytesPerEntry = m_pMPAFile->ExtractBytes(dwOffset, 2 );
	m_dwFramesPerEntry = m_pMPAFile->ExtractBytes(dwOffset, 2 );

	// extract TOC  (for more accurate seeking)
	m_pnToc = new int[m_dwTableSize];
	if( m_pnToc )
	{
		for ( unsigned int i = 0 ; i < m_dwTableSize ; i++)
		{
			m_pnToc[i] = m_pMPAFile->ExtractBytes(dwOffset, m_dwBytesPerEntry );
		}
	}
	return true;
}

CVBRHeader::~CVBRHeader(void)
{
	if( m_pnToc )
		delete[] m_pnToc;
}

// get byte position for percentage value (fPercent) of file
bool CVBRHeader::SeekPoint(float fPercent, uint32& dwSeekPoint)
{
	if( !m_pnToc || m_dwBytes == 0 )
		return false;

	if( fPercent < 0.0f )   
		fPercent = 0.0f;
	if( fPercent > 100.0f ) 
		fPercent = 100.0f;

	switch( m_HeaderType )
	{
		case XINGHeader:
			dwSeekPoint = SeekPointXING( fPercent );
			break;
		case VBRIHeader:
			dwSeekPoint = SeekPointVBRI( fPercent );
			break;
	}
	return true;
}

uint32 CVBRHeader::SeekPointXING(float fPercent) const
{
	// interpolate in TOC to get file seek point in bytes
	int a;
	float fa, fb, fx;

	a = (int)fPercent;
	if( a > 99 ) a = 99;
	fa = (float)m_pnToc[a];
	
	if( a < 99 ) 
	{
		fb = (float)m_pnToc[a+1];
	}
	else 
	{
		fb = 256.0f;
	}

	fx = fa + (fb-fa)*(fPercent-a);

	uint32 dwSeekpoint = (int)((1.0f/256.0f)*fx*m_dwBytes); 
	return dwSeekpoint;
}

uint32 CVBRHeader::SeekPointVBRI(float fPercent) const
{
	return SeekPointByTimeVBRI( (fPercent/100.0f) * m_pMPAFile->m_pMPAHeader->GetLengthSecond( m_dwFrames ) * 1000.0f );
}

uint32 CVBRHeader::SeekPointByTimeVBRI(float fEntryTimeMS) const
{
	unsigned int i=0,  fraction = 0;
	uint32 dwSeekPoint = 0;

	float fLengthMS;
	float fLengthMSPerTOCEntry;
	float fAccumulatedTimeMS = 0.0f ;
	 
	fLengthMS = (float)m_pMPAFile->m_pMPAHeader->GetLengthSecond( m_dwFrames ) * 1000.0f ;
	fLengthMSPerTOCEntry = fLengthMS / (float)m_dwTableSize;
	 
	if ( fEntryTimeMS > fLengthMS ) 
		fEntryTimeMS = fLengthMS; 
	 
	while ( fAccumulatedTimeMS <= fEntryTimeMS )
	{
		dwSeekPoint += m_pnToc[i++];
		fAccumulatedTimeMS += fLengthMSPerTOCEntry;
	}
	  
	// Searched too far; correct result
	fraction = ( (int)(((( fAccumulatedTimeMS - fEntryTimeMS ) / fLengthMSPerTOCEntry ) 
				+ (1.0f/(2.0f*(float)m_dwFramesPerEntry))) * (float)m_dwFramesPerEntry));

	dwSeekPoint -= (uint32)((float)m_pnToc[i-1] * (float)(fraction) 
					/ (float)m_dwFramesPerEntry);

	return dwSeekPoint;
}