forked from StrongPC123/Far-Cry-1-Source-Full
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathADPCMDecoder.cpp
558 lines (519 loc) · 16.5 KB
/
ADPCMDecoder.cpp
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#include "StdAfx.h"
#include <CrySizer.h>
#include <ISound.h>
#include <ISystem.h>
#include <ICryPak.h>
#include "AdpcmDecoder.h"
template< typename T, typename S >
inline void lsbshortldi( T& x, S& p )
{
x = (T) ( (int) p[ 0 ] + ( (int) p[ 1 ] << 8 ) );
p += 2;
}
static const int stepAdjustTable[] =
{
230, 230, 230, 230, 307, 409, 512, 614,
768, 614, 512, 409, 307, 230, 230, 230
};
//////////////////////////////////////////////////////////////////////////
CADPCMDecoder::CADPCMDecoder(IMusicSystem *pMusicSystem) : m_bOpen(false)
{
m_pMusicSystem=pMusicSystem;
ISystem *pSystem=m_pMusicSystem->GetSystem();
ASSERT(pSystem);
m_pPak=pSystem->GetIPak();
ASSERT(m_pPak);
}
//////////////////////////////////////////////////////////////////////////
inline CADPCMDecoder::~CADPCMDecoder()
{
Close();
}
inline const bool CADPCMDecoder::IsOpen()const
{
return m_bOpen;
}
inline FILE* CADPCMDecoder::GetNewFileHandle()
{
FILE *pNewFile = m_pPak->FOpen(m_szFilename.c_str(), "rb");
if (!pNewFile)
return NULL;
return pNewFile;
}
inline void CADPCMDecoder::CloseFileHandle(FILE*& pFile)
{
if (pFile)
{
m_pPak->FClose(pFile);
pFile=NULL;
}
}
inline void CADPCMDecoder::SetFileInfo(const unsigned int cuiSampleCount, const bool cbIs44KHz)
{
m_FileInfo.nSamples = cuiSampleCount;//already doubled up
m_b44KHz = cbIs44KHz;
m_bOpen = true;
}
//////////////////////////////////////////////////////////////////////////
inline bool CADPCMDecoder::Open(const char *pszFilename)
{
m_FileInfo.sFilename = pszFilename;
m_szFilename = pszFilename;//damn it, but how can i get a const char* from the other string type, why do we have an own?
m_FileInfo.nHeaderSize = scuiSizeOfWavHeader;
return m_bOpen;
}
//////////////////////////////////////////////////////////////////////////
inline bool CADPCMDecoder::Close()
{
m_bOpen = false;
return true;
}
inline const char* CADPCMDecoder::FileName() const
{
return m_szFilename.c_str();
}
//////////////////////////////////////////////////////////////////////////
inline bool CADPCMDecoder::GetFileInfo(SMusicPatternFileInfo &FileInfo)
{
FileInfo=m_FileInfo;
return true;
}
//////////////////////////////////////////////////////////////////////////
inline void CADPCMDecoder::GetMemoryUsage(class ICrySizer* pSizer)
{
if (!pSizer->Add(*this))
return;
}
inline int CADPCMDecoder::AdpcmDecode( int c, MsState& rState, int sample1, int sample2 )
{
// Compute next step value
int step( rState.step );
int nstep( ( stepAdjustTable[ c ] * step ) >> 8 );
rState.step = ( nstep < 16 ) ? 16 : nstep;
// make linear prediction for next sample
int vlin( ( ( sample1 * rState.sCoef0 ) + ( sample2 * rState.sCoef1 ) ) >> 8 );
// then add the code * step adjustment
c -= ( c & 0x08 ) << 1;
int sample( ( c * step ) + vlin );
if( sample > 0x7fff )
sample = 0x7fff;
else if( sample < -0x8000 )
sample = -0x8000;
return( sample );
}
void CADPCMDecoder::AdpcmBlockExpandI( int nCoef, const short* iCoef, const unsigned char* ibuff, short* obuff, int n )
{
MsState state[ 2 ]; // One decompressor state for each channel
// Read the four-byte header for each channel
const unsigned char* ip( ibuff );
unsigned char bpred( *ip++ );
state[ 0 ].sCoef0 = iCoef[ (int) bpred * 2 + 0 ];
state[ 0 ].sCoef1 = iCoef[ (int) bpred * 2 + 1 ];
bpred = ( *ip++ );
state[ 1 ].sCoef0 = iCoef[ (int) bpred * 2 + 0 ];
state[ 1 ].sCoef1 = iCoef[ (int) bpred * 2 + 1 ];
lsbshortldi( state[0].step, ip );
lsbshortldi( state[1].step, ip );
// sample1's directly into obuff
lsbshortldi( obuff[ 2 ], ip );
lsbshortldi( obuff[ 2 + 1 ], ip );
// sample2's directly into obuff
lsbshortldi( obuff[ 0 ], ip );
lsbshortldi( obuff[ 1 ], ip );
// already have 1st 2 samples from block-header
short* op( obuff + 2 * 2 );
short* top( obuff + n * 2 );
{
while( op < top )
{
unsigned char b( *ip++ );
short* tmp( op );
*op++ = AdpcmDecode( b >> 4, state[0], tmp[ -2 ], tmp[ -2 * 2 ] );
tmp = op;
*op++ = AdpcmDecode( b & 0x0f, state[1], tmp[ -2 ], tmp[ -2 * 2 ] );
}
}
}
//////////////////////////////////////////////////////////////////////////
inline IMusicPatternDecoderInstance* CADPCMDecoder::CreateInstance()
{
return new CADPCMDecoderInstance(this);
}
//////////////////////////////////////////////////////////////////////////
/*** INSTANCE ***/
//////////////////////////////////////////////////////////////////////////
inline CADPCMDecoderInstance::CADPCMDecoderInstance(CADPCMDecoder *pDecoder)
: m_iFilePos(0), m_pFile(NULL),m_nPos(0), m_uiDataStartPos(0),m_uiNumSamples(0),m_sCoefs(7),m_psSamplePtr(NULL),
m_bInitialized(false), m_bCopyFromLastFrame(false),m_uiCurrentBlockSize(scuiBlockSize),m_uiCurrentSamplesPerBlock(scuiSamplesPerBlock)
{
assert(pDecoder);
assert(pDecoder->m_pPak);
m_pDecoder = pDecoder;
//init wav stream
if(!InitStreamWAV())
{
m_bInitialized = false;
}
else
{
m_bInitialized = true;
}
}
CADPCMDecoderInstance::~CADPCMDecoderInstance()
{
Close();//let the filehandle be released
}
inline void CADPCMDecoderInstance::Close()
{
if (m_pFile)
{
m_pDecoder->CloseFileHandle(m_pFile);
}
m_bInitialized = false;
}
//////////////////////////////////////////////////////////////////////////
inline bool CADPCMDecoderInstance::Seek0(int nDelay)
{
m_nPos=-nDelay; //new sample pos
m_bCopyFromLastFrame = false;
if (!m_pFile || m_uiDataStartPos == 0)
return false;
m_pDecoder->m_pPak->FSeek(m_pFile,m_uiDataStartPos,SEEK_SET);
m_iFilePos = 0;
return true;
}
inline const unsigned int CADPCMDecoderInstance::Samples()
{
return m_uiNumSamples;
}
//////////////////////////////////////////////////////////////////////////
inline int CADPCMDecoderInstance::GetPos()
{
if (!m_pFile)
return -1;
return m_nPos;
}
const bool CADPCMDecoderInstance::InitStreamWAV()
{
memset(m_aEncodedBlock,0,scuiBlockSize);
SADPCMWaveHdr adpcmHeader; //adpcm secific header
//get file handle
if(!m_pFile)
{
m_pFile = m_pDecoder->GetNewFileHandle();
if(!m_pFile)
return false;
}
ICryPak& pPak = *m_pDecoder->m_pPak; //get reference to pak file
//retrieve total size of file
pPak.FRead(&adpcmHeader,1, sizeof(SADPCMWaveHdr),m_pFile);
pPak.FRead(&m_sCoefs, 1, sizeof(short), m_pFile);
if(adpcmHeader.FormatTag == WAVE_FORMAT_PCM)
{
#ifdef LOG_MUSICFILES
m_pDecoder->m_pMusicSystem->LogMsg( "Uncompressed pcm data, use pcm decoder %s\n", m_pDecoder->FileName());
#else
TRACE("uncompressed pcm data, use pcm decoder %s\n", m_pDecoder->FileName());
#endif
return false;
}
if(adpcmHeader.FormatTag != WAVE_FORMAT_ADPCM || adpcmHeader.wChnls != 2)
{
#ifdef LOG_MUSICFILES
m_pDecoder->m_pMusicSystem->LogMsg("No stereo adpcm file: %s\n", m_pDecoder->FileName());
#else
TRACE("no stereo adpcm file: %s\n", m_pDecoder->FileName());
#endif
return false;
}
//set block alignments
m_uiCurrentBlockSize = adpcmHeader.wBlkAlign;
m_uiCurrentSamplesPerBlock = adpcmHeader.SamplesPerBlock;
if(m_sCoefs != scuiNumberOfCoefs || m_uiCurrentSamplesPerBlock > scuiSamplesPerBlock || m_uiCurrentBlockSize > scuiBlockSize)
{
#ifdef LOG_MUSICFILES
m_pDecoder->m_pMusicSystem->LogMsg("Unsupported adpcm format %s\n", m_pDecoder->FileName());
#else
TRACE("unsupported adpcm format %s\n", m_pDecoder->FileName());
#endif
return false;
}
pPak.FRead(m_aCoefs, 1, m_sCoefs/*7*/ * 2/*stereo->2 channels*/ * sizeof(short), m_pFile);
//now seek to start position (next to "data")
unsigned int uiDataLeft = 0;
for(;;)
{
char pcMagic[ 4 ];
pPak.FRead( pcMagic, 1, sizeof( unsigned char ) * 4, m_pFile );
if(0 != pPak.FEof( m_pFile ))
return false;
pPak.FRead( &uiDataLeft, 1, sizeof( unsigned int ), m_pFile );
if( 0 == strncmp( "data", pcMagic, 4 ) )
break;
pPak.FSeek( m_pFile, uiDataLeft, SEEK_CUR);
}
m_uiDataStartPos = m_pDecoder->m_pPak->FTell(m_pFile);//to make seek0 work properly, data start position
//compute number of samples contained
int m = (uiDataLeft % m_uiCurrentBlockSize);
m_uiNumSamples = (uiDataLeft / m_uiCurrentBlockSize) * m_uiCurrentSamplesPerBlock;//usually block aligned and not necessary
m -= scuiNumberOfCoefs * 2; // bytes beyond block-header
m += 2; // nibbles / channels + 2 in header
if(m > 0) //there are some samples contained in an unaligned block
{
if( m > (int)m_uiCurrentSamplesPerBlock )
{
m = (int)m_uiCurrentSamplesPerBlock;
}
m_uiNumSamples += m;
}
Seek0();
//since the total sample count is not known by the decoder, tell him
const bool cbIs44KHz = (adpcmHeader.dwSRate == 44100);
if(!cbIs44KHz)
m_uiNumSamples *= 2;
m_pDecoder->SetFileInfo(m_uiNumSamples, cbIs44KHz);
return true;
}
//////////////////////////////////////////////////////////////////////////
bool CADPCMDecoderInstance::GetPCMData(signed long *pDataOut, int nSamples, bool bLoop)
{
//prevent any asynchonisation in terms of file opening
if (!m_pFile || !m_bInitialized)
{
//try it again
if(m_pDecoder->IsOpen())
{
if(!InitStreamWAV())
return false;
}
else
return false;
}
int nOfs=0;
if (m_nPos<0)
{
if (-m_nPos >= nSamples)
{
memset(pDataOut, 0, nSamples*m_pDecoder->m_pMusicSystem->GetBytesPerSample());
m_nPos+=nSamples;
return true;
}else
{
nOfs=-m_nPos;
m_nPos=0;
Seek0();//set back to start
nSamples-=nOfs;
memset(pDataOut, 0, nOfs*m_pDecoder->m_pMusicSystem->GetBytesPerSample());
}
}
int nSamplesToRead;
for (;;)
{
if ((m_nPos+nSamples) > (int)m_uiNumSamples)
{
nSamplesToRead = m_uiNumSamples - m_nPos;
}
else
nSamplesToRead=nSamples;
if(m_pDecoder->Is44KHz())
{
if (!FillPCMBuffer(&(pDataOut[nOfs]), nSamplesToRead))
return false;
}
else
{
if (!FillPCMBuffer22KHz(&(pDataOut[nOfs]), nSamplesToRead))
return false;
}
m_nPos+=nSamplesToRead;
if (nSamplesToRead==nSamples)
break;
nOfs+=nSamplesToRead;
if (!bLoop)
{
memset(&(pDataOut[nOfs]), 0, (nSamples-nSamplesToRead)*m_pDecoder->m_pMusicSystem->GetBytesPerSample());
break;
}
nSamples-=nSamplesToRead;
Seek0();
}
return (true);
}
// AdpcmReadBlock - Grab and decode complete block of samples
inline unsigned short CADPCMDecoderInstance::AdpcmReadBlock(short* pDest)
{
//read block
unsigned short bytesRead( (unsigned short) m_pDecoder->m_pPak->FRead( m_aEncodedBlock, 1, m_uiCurrentBlockSize, m_pFile ) );
m_iFilePos += bytesRead; //internal file pos counter, counted from m_uiDataStartPos on
unsigned int samplesThisBlock = m_uiCurrentSamplesPerBlock;
if( bytesRead < m_uiCurrentBlockSize )
{
//this shouldnt be the case, but handle it just in case (most programs do block align)
samplesThisBlock = 0;
unsigned int m = bytesRead;
//more data found than just coefs
if( m >= (unsigned int) ( scuiNumberOfCoefs * 2 ) )
{
samplesThisBlock = m - scuiNumberOfCoefs * 2 + 2;// bytes beyond block-header
}
else
return( 0 );
}
//now decode read stuff
if(pDest == NULL)
CADPCMDecoder::AdpcmBlockExpandI( m_sCoefs, m_aCoefs, m_aEncodedBlock, m_aDecodedBlock, samplesThisBlock );
else
CADPCMDecoder::AdpcmBlockExpandI( m_sCoefs, m_aCoefs, m_aEncodedBlock, pDest, samplesThisBlock );
return( samplesThisBlock );
}
const bool CADPCMDecoderInstance::FillPCMBuffer(signed long *pBuffer, int nSamples)
{
//since there get always complete blocks read, fill buffer with already compressed samples
const unsigned int cuiModulo = m_nPos % m_uiCurrentSamplesPerBlock;
if(cuiModulo != 0 && m_iFilePos > 0)//to not let break anything
{ //we are not on a block boundary
unsigned int uiLeft = min((unsigned int)nSamples, (m_uiCurrentSamplesPerBlock - cuiModulo));
if(uiLeft != 0)//read as many sampels as left decompressed and not more than requested
{
//copy decompressed data
signed long *pslDecompressed = reinterpret_cast<signed long*>(m_aDecodedBlock) + cuiModulo;
for(unsigned int i=0; i<uiLeft;i++)
{
*pBuffer++ = *pslDecompressed++;
}
//set new number of remaining samples
nSamples -= uiLeft;
}
}
short *pCurrentDest = reinterpret_cast<short*>(pBuffer); //need a short buffer to decode each channel indivually (remember: 16 bit)
if(nSamples > 0)
{
while(nSamples > 0)
{
//read as many blocks as necessary
//write straight to output buffer if complete block has to read in
if(nSamples >= (int)m_uiCurrentSamplesPerBlock)
{
bool bEndOfFile = (AdpcmReadBlock( pCurrentDest ) != m_uiCurrentSamplesPerBlock);
if(bEndOfFile) //shouldnt happen
return false;
nSamples -= m_uiCurrentSamplesPerBlock;
pCurrentDest += m_uiCurrentSamplesPerBlock * 2;//two channels decoded
}
else
{
//decompress to static buffer
unsigned int uiReadSamples = AdpcmReadBlock();
if(uiReadSamples != m_uiCurrentSamplesPerBlock)
{
//we are supposed to be on the end of the file, fill with 0's
memset(m_aDecodedBlock + uiReadSamples * 2/*stereo*/, 0, m_uiCurrentSamplesPerBlock - uiReadSamples);
}
//read requested samples
pBuffer = reinterpret_cast<signed long*>(pCurrentDest);
signed long *pDecoded = reinterpret_cast<signed long*>(m_aDecodedBlock);
//if for bad reason not enough samples have been read, it will get automatically filled with 0's
for(int i=0; i<nSamples;i++)
{
*pBuffer++ = *pDecoded++;
}
nSamples = 0;
}
}
}
return true;
}
const bool CADPCMDecoderInstance::FillPCMBuffer22KHz(signed long *pBuffer, int nSamples)
{
unsigned uStartPos = (m_nPos/2/*22KHz*/);
//first check for copying some data from last frame (if not at starting position)
if(m_bCopyFromLastFrame && m_iFilePos > 0)/*22KHz*/
{
*pBuffer++ = m_lLastSample;//contains sample to start with from last encoding
nSamples -= 1;
m_bCopyFromLastFrame = false;
uStartPos++;
}
//since there get always complete blocks read, fill buffer with already compressed samples
const unsigned int cuiModulo = uStartPos % m_uiCurrentSamplesPerBlock;
unsigned int uiLeft = 0;
if(cuiModulo != 0 && m_iFilePos > 0)//to not let break anything
{ //we are not on a block boundary
uiLeft = min((unsigned int)nSamples/2, (m_uiCurrentSamplesPerBlock - cuiModulo));/*22KHz*/
signed long *pslDecompressed = reinterpret_cast<signed long*>(m_aDecodedBlock) + cuiModulo;
if(uiLeft != 0)//read as many sampels as left decompressed and not more than requested
{
//copy decompressed data
for(unsigned int i=0; i<uiLeft;i++)
{
*pBuffer++ = *pslDecompressed;/*22KHz*/
*pBuffer++ = *pslDecompressed++;
}
//set new number of remaining samples
nSamples -= 2*uiLeft;
}
if(nSamples == 1)/*22KHz*/
{
//there is one sample left
m_bCopyFromLastFrame = true;
*pBuffer++ = m_lLastSample = *pslDecompressed; //remember last sample, has to start with in next request
nSamples = 0;
}
}
short *pCurrentDest = reinterpret_cast<short*>(pBuffer); //need a short buffer to decode each channel indivually (remember: 16 bit)
if(nSamples > 0)
{
while(nSamples > 0)
{
//read as many blocks as necessary
//write straight to output buffer if complete block has to read in
if(nSamples >= (int)m_uiCurrentSamplesPerBlock*2)
{
bool bEndOfFile = (AdpcmReadBlock() != m_uiCurrentSamplesPerBlock);
if(bEndOfFile) //shouldnt happen
{
memset(pBuffer, 0, nSamples*4);//fill with 0's
return false;
}
pBuffer = reinterpret_cast<signed long*>(pCurrentDest);
signed long *pDecoded = reinterpret_cast<signed long*>(m_aDecodedBlock);
//if for bad reason not enough samples have been read, it will get automatically filled with 0's
for(unsigned int i=0; i<m_uiCurrentSamplesPerBlock;i++)
{
*pBuffer++ = *pDecoded;
*pBuffer++ = *pDecoded++;
}
nSamples -= 2*m_uiCurrentSamplesPerBlock; /*22KHz*/
pCurrentDest = reinterpret_cast<short*>(pBuffer);
}
else
{
//decompress to static buffer
unsigned int uiReadSamples = AdpcmReadBlock();
if(uiReadSamples != m_uiCurrentSamplesPerBlock)
{
//we are supposed to be on the end of the file, fill with 0's
memset(m_aDecodedBlock + uiReadSamples * 2/*stereo*/, 0, m_uiCurrentSamplesPerBlock - uiReadSamples);
}
//read requested samples
pBuffer = reinterpret_cast<signed long*>(pCurrentDest);
signed long *pDecoded = reinterpret_cast<signed long*>(m_aDecodedBlock);
//if for some bad reason not enough samples have been read, it will get automatically filled with 0's
for(int i=0; i<nSamples/2;i++)
{
*pBuffer++ = *pDecoded;
*pBuffer++ = *pDecoded++;
}
m_bCopyFromLastFrame = false; //just to make sure
if(nSamples & 0x1)
{
m_bCopyFromLastFrame = true;
//read one sample more
*pBuffer++ = m_lLastSample = *pDecoded; //remember last sample, has to start with in next request
}
nSamples = 0;
}
}
}
return true;
}