123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- #include "../idlib/precompiled.h"
- #pragma hdrstop
- #include "snd_local.h"
- #include "OggVorbis/vorbis/codec.h"
- #include "OggVorbis/vorbis/vorbisfile.h"
- idDynamicBlockAlloc<byte, 1<<20, 128> decoderMemoryAllocator;
- const int MIN_OGGVORBIS_MEMORY = 768 * 1024;
- extern "C" {
- void *_decoder_malloc( size_t size );
- void *_decoder_calloc( size_t num, size_t size );
- void *_decoder_realloc( void *memblock, size_t size );
- void _decoder_free( void *memblock );
- }
- void *_decoder_malloc( size_t size ) {
- void *ptr = decoderMemoryAllocator.Alloc( size );
- assert( size == 0 || ptr != NULL );
- return ptr;
- }
- void *_decoder_calloc( size_t num, size_t size ) {
- void *ptr = decoderMemoryAllocator.Alloc( num * size );
- assert( ( num * size ) == 0 || ptr != NULL );
- memset( ptr, 0, num * size );
- return ptr;
- }
- void *_decoder_realloc( void *memblock, size_t size ) {
- void *ptr = decoderMemoryAllocator.Resize( (byte *)memblock, size );
- assert( size == 0 || ptr != NULL );
- return ptr;
- }
- void _decoder_free( void *memblock ) {
- decoderMemoryAllocator.Free( (byte *)memblock );
- }
- size_t FS_ReadOGG( void *dest, size_t size1, size_t size2, void *fh ) {
- idFile *f = reinterpret_cast<idFile *>(fh);
- return f->Read( dest, size1 * size2 );
- }
- int FS_SeekOGG( void *fh, ogg_int64_t to, int type ) {
- fsOrigin_t retype = FS_SEEK_SET;
- if ( type == SEEK_CUR ) {
- retype = FS_SEEK_CUR;
- } else if ( type == SEEK_END ) {
- retype = FS_SEEK_END;
- } else if ( type == SEEK_SET ) {
- retype = FS_SEEK_SET;
- } else {
- common->FatalError( "fs_seekOGG: seek without type\n" );
- }
- idFile *f = reinterpret_cast<idFile *>(fh);
- return f->Seek( to, retype );
- }
- int FS_CloseOGG( void *fh ) {
- return 0;
- }
- long FS_TellOGG( void *fh ) {
- idFile *f = reinterpret_cast<idFile *>(fh);
- return f->Tell();
- }
- int ov_openFile( idFile *f, OggVorbis_File *vf ) {
- ov_callbacks callbacks;
- memset( vf, 0, sizeof( OggVorbis_File ) );
- callbacks.read_func = FS_ReadOGG;
- callbacks.seek_func = FS_SeekOGG;
- callbacks.close_func = FS_CloseOGG;
- callbacks.tell_func = FS_TellOGG;
- return ov_open_callbacks((void *)f, vf, NULL, -1, callbacks);
- }
- int idWaveFile::OpenOGG( const char* strFileName, waveformatex_t *pwfx ) {
- OggVorbis_File *ov;
- memset( pwfx, 0, sizeof( waveformatex_t ) );
- mhmmio = fileSystem->OpenFileRead( strFileName );
- if ( !mhmmio ) {
- return -1;
- }
- Sys_EnterCriticalSection( CRITICAL_SECTION_ONE );
- ov = new OggVorbis_File;
- if( ov_openFile( mhmmio, ov ) < 0 ) {
- delete ov;
- Sys_LeaveCriticalSection( CRITICAL_SECTION_ONE );
- fileSystem->CloseFile( mhmmio );
- mhmmio = NULL;
- return -1;
- }
- mfileTime = mhmmio->Timestamp();
- vorbis_info *vi = ov_info( ov, -1 );
- mpwfx.Format.nSamplesPerSec = vi->rate;
- mpwfx.Format.nChannels = vi->channels;
- mpwfx.Format.wBitsPerSample = sizeof(short) * 8;
- mdwSize = ov_pcm_total( ov, -1 ) * vi->channels;
- mbIsReadingFromMemory = false;
- if ( idSoundSystemLocal::s_realTimeDecoding.GetBool() ) {
- ov_clear( ov );
- fileSystem->CloseFile( mhmmio );
- mhmmio = NULL;
- delete ov;
- mpwfx.Format.wFormatTag = WAVE_FORMAT_TAG_OGG;
- mhmmio = fileSystem->OpenFileRead( strFileName );
- mMemSize = mhmmio->Length();
- } else {
- ogg = ov;
- mpwfx.Format.wFormatTag = WAVE_FORMAT_TAG_PCM;
- mMemSize = mdwSize * sizeof( short );
- }
- memcpy( pwfx, &mpwfx, sizeof( waveformatex_t ) );
- Sys_LeaveCriticalSection( CRITICAL_SECTION_ONE );
- isOgg = true;
- return 0;
- }
- int idWaveFile::ReadOGG( byte* pBuffer, int dwSizeToRead, int *pdwSizeRead ) {
- int total = dwSizeToRead;
- char *bufferPtr = (char *)pBuffer;
- OggVorbis_File *ov = (OggVorbis_File *) ogg;
- do {
- int ret = ov_read( ov, bufferPtr, total >= 4096 ? 4096 : total, Swap_IsBigEndian(), 2, 1, &ov->stream );
- if ( ret == 0 ) {
- break;
- }
- if ( ret < 0 ) {
- return -1;
- }
- bufferPtr += ret;
- total -= ret;
- } while( total > 0 );
- dwSizeToRead = (byte *)bufferPtr - pBuffer;
- if ( pdwSizeRead != NULL ) {
- *pdwSizeRead = dwSizeToRead;
- }
- return dwSizeToRead;
- }
- int idWaveFile::CloseOGG( void ) {
- OggVorbis_File *ov = (OggVorbis_File *) ogg;
- if ( ov != NULL ) {
- Sys_EnterCriticalSection( CRITICAL_SECTION_ONE );
- ov_clear( ov );
- delete ov;
- Sys_LeaveCriticalSection( CRITICAL_SECTION_ONE );
- fileSystem->CloseFile( mhmmio );
- mhmmio = NULL;
- ogg = NULL;
- return 0;
- }
- return -1;
- }
- class idSampleDecoderLocal : public idSampleDecoder {
- public:
- virtual void Decode( idSoundSample *sample, int sampleOffset44k, int sampleCount44k, float *dest );
- virtual void ClearDecoder( void );
- virtual idSoundSample * GetSample( void ) const;
- virtual int GetLastDecodeTime( void ) const;
- void Clear( void );
- int DecodePCM( idSoundSample *sample, int sampleOffset44k, int sampleCount44k, float *dest );
- int DecodeOGG( idSoundSample *sample, int sampleOffset44k, int sampleCount44k, float *dest );
- private:
- bool failed;
- int lastFormat;
- idSoundSample * lastSample;
- int lastSampleOffset;
- int lastDecodeTime;
- idFile_Memory file;
- OggVorbis_File ogg;
- };
- idBlockAlloc<idSampleDecoderLocal, 64> sampleDecoderAllocator;
- void idSampleDecoder::Init( void ) {
- decoderMemoryAllocator.Init();
- decoderMemoryAllocator.SetLockMemory( true );
- decoderMemoryAllocator.SetFixedBlocks( idSoundSystemLocal::s_realTimeDecoding.GetBool() ? 10 : 1 );
- }
- void idSampleDecoder::Shutdown( void ) {
- decoderMemoryAllocator.Shutdown();
- sampleDecoderAllocator.Shutdown();
- }
- idSampleDecoder *idSampleDecoder::Alloc( void ) {
- idSampleDecoderLocal *decoder = sampleDecoderAllocator.Alloc();
- decoder->Clear();
- return decoder;
- }
- void idSampleDecoder::Free( idSampleDecoder *decoder ) {
- idSampleDecoderLocal *localDecoder = static_cast<idSampleDecoderLocal *>( decoder );
- localDecoder->ClearDecoder();
- sampleDecoderAllocator.Free( localDecoder );
- }
- int idSampleDecoder::GetNumUsedBlocks( void ) {
- return decoderMemoryAllocator.GetNumUsedBlocks();
- }
- int idSampleDecoder::GetUsedBlockMemory( void ) {
- return decoderMemoryAllocator.GetUsedBlockMemory();
- }
- void idSampleDecoderLocal::Clear( void ) {
- failed = false;
- lastFormat = WAVE_FORMAT_TAG_PCM;
- lastSample = NULL;
- lastSampleOffset = 0;
- lastDecodeTime = 0;
- }
- void idSampleDecoderLocal::ClearDecoder( void ) {
- Sys_EnterCriticalSection( CRITICAL_SECTION_ONE );
- switch( lastFormat ) {
- case WAVE_FORMAT_TAG_PCM: {
- break;
- }
- case WAVE_FORMAT_TAG_OGG: {
- ov_clear( &ogg );
- memset( &ogg, 0, sizeof( ogg ) );
- break;
- }
- }
- Clear();
- Sys_LeaveCriticalSection( CRITICAL_SECTION_ONE );
- }
- idSoundSample *idSampleDecoderLocal::GetSample( void ) const {
- return lastSample;
- }
- int idSampleDecoderLocal::GetLastDecodeTime( void ) const {
- return lastDecodeTime;
- }
- void idSampleDecoderLocal::Decode( idSoundSample *sample, int sampleOffset44k, int sampleCount44k, float *dest ) {
- int readSamples44k;
- if ( sample->objectInfo.wFormatTag != lastFormat || sample != lastSample ) {
- ClearDecoder();
- }
- lastDecodeTime = soundSystemLocal.CurrentSoundTime;
- if ( failed ) {
- memset( dest, 0, sampleCount44k * sizeof( dest[0] ) );
- return;
- }
-
- Sys_EnterCriticalSection( CRITICAL_SECTION_ONE );
- switch( sample->objectInfo.wFormatTag ) {
- case WAVE_FORMAT_TAG_PCM: {
- readSamples44k = DecodePCM( sample, sampleOffset44k, sampleCount44k, dest );
- break;
- }
- case WAVE_FORMAT_TAG_OGG: {
- readSamples44k = DecodeOGG( sample, sampleOffset44k, sampleCount44k, dest );
- break;
- }
- default: {
- readSamples44k = 0;
- break;
- }
- }
- Sys_LeaveCriticalSection( CRITICAL_SECTION_ONE );
- if ( readSamples44k < sampleCount44k ) {
- memset( dest + readSamples44k, 0, ( sampleCount44k - readSamples44k ) * sizeof( dest[0] ) );
- }
- }
- int idSampleDecoderLocal::DecodePCM( idSoundSample *sample, int sampleOffset44k, int sampleCount44k, float *dest ) {
- const byte *first;
- int pos, size, readSamples;
- lastFormat = WAVE_FORMAT_TAG_PCM;
- lastSample = sample;
- int shift = 22050 / sample->objectInfo.nSamplesPerSec;
- int sampleOffset = sampleOffset44k >> shift;
- int sampleCount = sampleCount44k >> shift;
- if ( sample->nonCacheData == NULL ) {
-
-
- failed = true;
- return 0;
- }
- if ( !sample->FetchFromCache( sampleOffset * sizeof( short ), &first, &pos, &size, false ) ) {
- failed = true;
- return 0;
- }
- if ( size - pos < sampleCount * sizeof( short ) ) {
- readSamples = ( size - pos ) / sizeof( short );
- } else {
- readSamples = sampleCount;
- }
-
- SIMDProcessor->UpSamplePCMTo44kHz( dest, (const short *)(first+pos), readSamples, sample->objectInfo.nSamplesPerSec, sample->objectInfo.nChannels );
- return ( readSamples << shift );
- }
- int idSampleDecoderLocal::DecodeOGG( idSoundSample *sample, int sampleOffset44k, int sampleCount44k, float *dest ) {
- int readSamples, totalSamples;
- int shift = 22050 / sample->objectInfo.nSamplesPerSec;
- int sampleOffset = sampleOffset44k >> shift;
- int sampleCount = sampleCount44k >> shift;
-
- if ( lastSample == NULL ) {
-
- if ( decoderMemoryAllocator.GetFreeBlockMemory() < MIN_OGGVORBIS_MEMORY ) {
- return 0;
- }
- if ( sample->nonCacheData == NULL ) {
- assert( false );
- failed = true;
- return 0;
- }
- file.SetData( (const char *)sample->nonCacheData, sample->objectMemSize );
- if ( ov_openFile( &file, &ogg ) < 0 ) {
- failed = true;
- return 0;
- }
- lastFormat = WAVE_FORMAT_TAG_OGG;
- lastSample = sample;
- }
-
- if ( sampleOffset != lastSampleOffset ) {
- if ( ov_pcm_seek( &ogg, sampleOffset / sample->objectInfo.nChannels ) != 0 ) {
- failed = true;
- return 0;
- }
- }
- lastSampleOffset = sampleOffset;
-
- totalSamples = sampleCount;
- readSamples = 0;
- do {
- float **samples;
- int ret = ov_read_float( &ogg, &samples, totalSamples / sample->objectInfo.nChannels, &ogg.stream );
- if ( ret == 0 ) {
- failed = true;
- break;
- }
- if ( ret < 0 ) {
- failed = true;
- return 0;
- }
- ret *= sample->objectInfo.nChannels;
- SIMDProcessor->UpSampleOGGTo44kHz( dest + ( readSamples << shift ), samples, ret, sample->objectInfo.nSamplesPerSec, sample->objectInfo.nChannels );
- readSamples += ret;
- totalSamples -= ret;
- } while( totalSamples > 0 );
- lastSampleOffset += readSamples;
- return ( readSamples << shift );
- }
|