diff options
author | Matthew Hoops | 2011-06-03 01:14:16 -0400 |
---|---|---|
committer | Matthew Hoops | 2011-06-03 01:14:16 -0400 |
commit | 224c71e483e09931ba386555ff3b436b9defe63d (patch) | |
tree | 8e6178331a7bbd3ee1be318d3fc7a7c7f478468f /audio | |
parent | d4c92983920cfe3b25a22d91e12c750e591b917e (diff) | |
parent | 547fd1bdcabcba0e741eb31100ba99ff73399d24 (diff) | |
download | scummvm-rg350-224c71e483e09931ba386555ff3b436b9defe63d.tar.gz scummvm-rg350-224c71e483e09931ba386555ff3b436b9defe63d.tar.bz2 scummvm-rg350-224c71e483e09931ba386555ff3b436b9defe63d.zip |
Merge remote branch 'upstream/master' into pegasus
Diffstat (limited to 'audio')
31 files changed, 5472 insertions, 667 deletions
diff --git a/audio/audiostream.cpp b/audio/audiostream.cpp index e6587a6543..547aa77526 100644 --- a/audio/audiostream.cpp +++ b/audio/audiostream.cpp @@ -30,6 +30,7 @@ #include "audio/audiostream.h" #include "audio/decoders/flac.h" #include "audio/decoders/mp3.h" +#include "audio/decoders/quicktime.h" #include "audio/decoders/raw.h" #include "audio/decoders/vorbis.h" @@ -48,7 +49,7 @@ struct StreamFileFormat { }; static const StreamFileFormat STREAM_FILEFORMATS[] = { - /* decoderName, fileExt, openStreamFuntion */ + /* decoderName, fileExt, openStreamFunction */ #ifdef USE_FLAC { "FLAC", ".flac", makeFLACStream }, { "FLAC", ".fla", makeFLACStream }, @@ -59,6 +60,7 @@ static const StreamFileFormat STREAM_FILEFORMATS[] = { #ifdef USE_MAD { "MPEG Layer 3", ".mp3", makeMP3Stream }, #endif + { "MPEG-4 Audio", ".m4a", makeQuickTimeStream }, { NULL, NULL, NULL } // Terminator }; diff --git a/audio/decoders/aac.cpp b/audio/decoders/aac.cpp new file mode 100644 index 0000000000..7949b5b561 --- /dev/null +++ b/audio/decoders/aac.cpp @@ -0,0 +1,177 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#include "audio/decoders/aac.h" + +#ifdef USE_FAAD + +#include "common/debug.h" +#include "common/stream.h" +#include "common/textconsole.h" +#include "common/util.h" + +#include "audio/audiostream.h" + +#include <neaacdec.h> + +namespace Audio { + +class AACStream : public AudioStream { +public: + AACStream(Common::SeekableReadStream *stream, + DisposeAfterUse::Flag disposeStream, + Common::SeekableReadStream *extraData, + DisposeAfterUse::Flag disposeExtraData); + ~AACStream(); + + int readBuffer(int16 *buffer, const int numSamples); + + bool endOfData() const { return _inBufferPos >= _inBufferSize && !_remainingSamples; } + bool isStereo() const { return _channels == 2; } + int getRate() const { return _rate; } + +private: + NeAACDecHandle _handle; + byte _channels; + unsigned long _rate; + + byte *_inBuffer; + uint32 _inBufferSize; + uint32 _inBufferPos; + + int16 *_remainingSamples; + uint32 _remainingSamplesSize; + uint32 _remainingSamplesPos; + + void init(Common::SeekableReadStream *extraData); +}; + +AACStream::AACStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeStream, + Common::SeekableReadStream *extraData, DisposeAfterUse::Flag disposeExtraData) { + + _remainingSamples = 0; + _inBufferPos = 0; + + init(extraData); + + // Copy all the data to a pointer so it can be passed through + // (At least MPEG-4 chunks shouldn't be large) + _inBufferSize = stream->size(); + _inBuffer = new byte[_inBufferSize]; + stream->read(_inBuffer, _inBufferSize); + + if (disposeStream == DisposeAfterUse::YES) + delete stream; + + if (disposeExtraData == DisposeAfterUse::YES) + delete extraData; +} + +AACStream::~AACStream() { + NeAACDecClose(_handle); + delete[] _inBuffer; + delete[] _remainingSamples; +} + +void AACStream::init(Common::SeekableReadStream *extraData) { + // Open the library + _handle = NeAACDecOpen(); + + // Configure the library to our needs + NeAACDecConfigurationPtr conf = NeAACDecGetCurrentConfiguration(_handle); + conf->outputFormat = FAAD_FMT_16BIT; // We only support 16bit audio + conf->downMatrix = 1; // Convert from 5.1 to stereo if required + NeAACDecSetConfiguration(_handle, conf); + + // Copy the extra data to a buffer + extraData->seek(0); + byte *extraDataBuf = new byte[extraData->size()]; + extraData->read(extraDataBuf, extraData->size()); + + // Initialize with our extra data + // NOTE: This code assumes the extra data is coming from an MPEG-4 file! + int err = NeAACDecInit2(_handle, extraDataBuf, extraData->size(), &_rate, &_channels); + delete[] extraDataBuf; + + if (err < 0) + error("Could not initialize AAC decoder: %s", NeAACDecGetErrorMessage(err)); +} + +int AACStream::readBuffer(int16 *buffer, const int numSamples) { + int samples = 0; + + assert((numSamples % _channels) == 0); + + // Dip into our remaining samples pool if it's available + if (_remainingSamples) { + samples = MIN<int>(numSamples, _remainingSamplesSize - _remainingSamplesPos); + + memcpy(buffer, _remainingSamples + _remainingSamplesPos, samples * 2); + _remainingSamplesPos += samples; + + if (_remainingSamplesPos == _remainingSamplesSize) { + delete[] _remainingSamples; + _remainingSamples = 0; + } + } + + // Decode until we have enough samples (or there's no more left) + while (samples < numSamples && !endOfData()) { + NeAACDecFrameInfo frameInfo; + uint16 *decodedSamples = (uint16 *)NeAACDecDecode(_handle, &frameInfo, _inBuffer + _inBufferPos, _inBufferSize - _inBufferPos); + + if (frameInfo.error != 0) + error("Failed to decode AAC frame: %s", NeAACDecGetErrorMessage(frameInfo.error)); + + int decodedSampleSize = frameInfo.samples; + int copySamples = (decodedSampleSize > (numSamples - samples)) ? (numSamples - samples) : decodedSampleSize; + + memcpy(buffer + samples, decodedSamples, copySamples * 2); + samples += copySamples; + + // Copy leftover samples for use in a later readBuffer() call + if (copySamples != decodedSampleSize) { + _remainingSamplesSize = decodedSampleSize - copySamples; + _remainingSamples = new int16[_remainingSamplesSize]; + _remainingSamplesPos = 0; + memcpy(_remainingSamples, decodedSamples + copySamples, _remainingSamplesSize * 2); + } + + _inBufferPos += frameInfo.bytesconsumed; + } + + return samples; +} + +// Factory function +AudioStream *makeAACStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeStream, + Common::SeekableReadStream *extraData, DisposeAfterUse::Flag disposeExtraData) { + + return new AACStream(stream, disposeStream, extraData, disposeExtraData); +} + +} // End of namespace Audio + +#endif // #ifdef USE_FAAD diff --git a/audio/decoders/aac.h b/audio/decoders/aac.h new file mode 100644 index 0000000000..f14fa9488b --- /dev/null +++ b/audio/decoders/aac.h @@ -0,0 +1,66 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +/** + * @file + * Sound decoder used in engines: + * - groovie + */ + +#ifndef SOUND_AAC_H +#define SOUND_AAC_H + +#include "common/scummsys.h" +#include "common/types.h" + +#ifdef USE_FAAD + +namespace Common { + class SeekableReadStream; +} + +namespace Audio { + +class AudioStream; + +/** + * Create a new AudioStream from the AAC data in the given stream. + * + * @param stream the SeekableReadStream from which to read the AAC data + * @param disposeStream whether to delete the stream after use + * @param extraData the SeekableReadStream from which to read the AAC extra data + * @param disposeExtraData whether to delete the extra data stream after use + * @return a new AudioStream, or NULL, if an error occurred + */ +AudioStream *makeAACStream( + Common::SeekableReadStream *stream, + DisposeAfterUse::Flag disposeStream, + Common::SeekableReadStream *extraData, + DisposeAfterUse::Flag disposeExtraData = DisposeAfterUse::NO); + +} // End of namespace Audio + +#endif // #ifdef USE_FAAD +#endif // #ifndef SOUND_AAC_H diff --git a/audio/decoders/flac.cpp b/audio/decoders/flac.cpp index b818d4f7e9..d06a7b9c0e 100644 --- a/audio/decoders/flac.cpp +++ b/audio/decoders/flac.cpp @@ -303,7 +303,7 @@ int FLACStream::readBuffer(int16 *buffer, const int numSamples) { const uint numChannels = getChannels(); if (numChannels == 0) { - warning("FLACStream: Stream not successfully initialised, cant playback"); + warning("FLACStream: Stream not successfully initialized, cant playback"); return -1; // streaminfo wasnt read! } diff --git a/audio/decoders/qdm2.cpp b/audio/decoders/qdm2.cpp new file mode 100644 index 0000000000..a178c363b5 --- /dev/null +++ b/audio/decoders/qdm2.cpp @@ -0,0 +1,3279 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +// Based off ffmpeg's QDM2 decoder + +#include "common/scummsys.h" +#include "audio/decoders/qdm2.h" + +#ifdef AUDIO_QDM2_H + +#include "audio/audiostream.h" +#include "audio/decoders/qdm2data.h" + +#include "common/array.h" +#include "common/debug.h" +#include "common/stream.h" +#include "common/textconsole.h" + +namespace Audio { + +enum { + SOFTCLIP_THRESHOLD = 27600, + HARDCLIP_THRESHOLD = 35716, + MPA_MAX_CHANNELS = 2, + MPA_FRAME_SIZE = 1152, + FF_INPUT_BUFFER_PADDING_SIZE = 8 +}; + +typedef int8 sb_int8_array[2][30][64]; + +/* bit input */ +/* buffer, buffer_end and size_in_bits must be present and used by every reader */ +struct GetBitContext { + const uint8 *buffer, *bufferEnd; + int index; + int sizeInBits; +}; + +struct QDM2SubPacket { + int type; + unsigned int size; + const uint8 *data; // pointer to subpacket data (points to input data buffer, it's not a private copy) +}; + +struct QDM2SubPNode { + QDM2SubPacket *packet; + struct QDM2SubPNode *next; // pointer to next packet in the list, NULL if leaf node +}; + +struct QDM2Complex { + float re; + float im; +}; + +struct FFTTone { + float level; + QDM2Complex *complex; + const float *table; + int phase; + int phase_shift; + int duration; + short time_index; + short cutoff; +}; + +struct FFTCoefficient { + int16 sub_packet; + uint8 channel; + int16 offset; + int16 exp; + uint8 phase; +}; + +struct VLC { + int32 bits; + int16 (*table)[2]; // code, bits + int32 table_size; + int32 table_allocated; +}; + +#include "common/pack-start.h" +struct QDM2FFT { + QDM2Complex complex[MPA_MAX_CHANNELS][256]; +} PACKED_STRUCT; +#include "common/pack-end.h" + +enum RDFTransformType { + RDFT, + IRDFT, + RIDFT, + IRIDFT +}; + +struct FFTComplex { + float re, im; +}; + +struct FFTContext { + int nbits; + int inverse; + uint16 *revtab; + FFTComplex *exptab; + FFTComplex *tmpBuf; + int mdctSize; // size of MDCT (i.e. number of input data * 2) + int mdctBits; // n = 2^nbits + // pre/post rotation tables + float *tcos; + float *tsin; + void (*fftPermute)(struct FFTContext *s, FFTComplex *z); + void (*fftCalc)(struct FFTContext *s, FFTComplex *z); + void (*imdctCalc)(struct FFTContext *s, float *output, const float *input); + void (*imdctHalf)(struct FFTContext *s, float *output, const float *input); + void (*mdctCalc)(struct FFTContext *s, float *output, const float *input); + int splitRadix; + int permutation; +}; + +enum { + FF_MDCT_PERM_NONE = 0, + FF_MDCT_PERM_INTERLEAVE = 1 +}; + +struct RDFTContext { + int nbits; + int inverse; + int signConvention; + + // pre/post rotation tables + float *tcos; + float *tsin; + FFTContext fft; +}; + +class QDM2Stream : public AudioStream { +public: + QDM2Stream(Common::SeekableReadStream *stream, Common::SeekableReadStream *extraData); + ~QDM2Stream(); + + bool isStereo() const { return _channels == 2; } + bool endOfData() const { return _stream->pos() >= _stream->size() && _outputSamples.size() == 0 && _subPacket == 0; } + int getRate() const { return _sampleRate; } + int readBuffer(int16 *buffer, const int numSamples); + +private: + Common::SeekableReadStream *_stream; + + // Parameters from codec header, do not change during playback + uint8 _channels; + uint16 _sampleRate; + uint16 _bitRate; + uint16 _blockSize; // Group + uint16 _frameSize; // FFT + uint16 _packetSize; // Checksum + + // Parameters built from header parameters, do not change during playback + int _groupOrder; // order of frame group + int _fftOrder; // order of FFT (actually fft order+1) + int _fftFrameSize; // size of fft frame, in components (1 comples = re + im) + int _sFrameSize; // size of data frame + int _frequencyRange; + int _subSampling; // subsampling: 0=25%, 1=50%, 2=100% */ + int _coeffPerSbSelect; // selector for "num. of coeffs. per subband" tables. Can be 0, 1, 2 + int _cmTableSelect; // selector for "coding method" tables. Can be 0, 1 (from init: 0-4) + + // Packets and packet lists + QDM2SubPacket _subPackets[16]; // the packets themselves + QDM2SubPNode _subPacketListA[16]; // list of all packets + QDM2SubPNode _subPacketListB[16]; // FFT packets B are on list + int _subPacketsB; // number of packets on 'B' list + QDM2SubPNode _subPacketListC[16]; // packets with errors? + QDM2SubPNode _subPacketListD[16]; // DCT packets + + // FFT and tones + FFTTone _fftTones[1000]; + int _fftToneStart; + int _fftToneEnd; + FFTCoefficient _fftCoefs[1000]; + int _fftCoefsIndex; + int _fftCoefsMinIndex[5]; + int _fftCoefsMaxIndex[5]; + int _fftLevelExp[6]; + RDFTContext _rdftCtx; + QDM2FFT _fft; + + // I/O data + uint8 *_compressedData; + float _outputBuffer[1024]; + Common::Array<int16> _outputSamples; + + // Synthesis filter + int16 ff_mpa_synth_window[512]; + int16 _synthBuf[MPA_MAX_CHANNELS][512*2]; + int _synthBufOffset[MPA_MAX_CHANNELS]; + int32 _sbSamples[MPA_MAX_CHANNELS][128][32]; + + // Mixed temporary data used in decoding + float _toneLevel[MPA_MAX_CHANNELS][30][64]; + int8 _codingMethod[MPA_MAX_CHANNELS][30][64]; + int8 _quantizedCoeffs[MPA_MAX_CHANNELS][10][8]; + int8 _toneLevelIdxBase[MPA_MAX_CHANNELS][30][8]; + int8 _toneLevelIdxHi1[MPA_MAX_CHANNELS][3][8][8]; + int8 _toneLevelIdxMid[MPA_MAX_CHANNELS][26][8]; + int8 _toneLevelIdxHi2[MPA_MAX_CHANNELS][26]; + int8 _toneLevelIdx[MPA_MAX_CHANNELS][30][64]; + int8 _toneLevelIdxTemp[MPA_MAX_CHANNELS][30][64]; + + // Flags + bool _hasErrors; // packet has errors + int _superblocktype_2_3; // select fft tables and some algorithm based on superblock type + int _doSynthFilter; // used to perform or skip synthesis filter + + uint8 _subPacket; // 0 to 15 + uint32 _superBlockStart; + int _noiseIdx; // index for dithering noise table + + byte _emptyBuffer[FF_INPUT_BUFFER_PADDING_SIZE]; + + VLC _vlcTabLevel; + VLC _vlcTabDiff; + VLC _vlcTabRun; + VLC _fftLevelExpAltVlc; + VLC _fftLevelExpVlc; + VLC _fftStereoExpVlc; + VLC _fftStereoPhaseVlc; + VLC _vlcTabToneLevelIdxHi1; + VLC _vlcTabToneLevelIdxMid; + VLC _vlcTabToneLevelIdxHi2; + VLC _vlcTabType30; + VLC _vlcTabType34; + VLC _vlcTabFftToneOffset[5]; + bool _vlcsInitialized; + void initVlc(void); + + uint16 _softclipTable[HARDCLIP_THRESHOLD - SOFTCLIP_THRESHOLD + 1]; + void softclipTableInit(void); + + float _noiseTable[4096]; + byte _randomDequantIndex[256][5]; + byte _randomDequantType24[128][3]; + void rndTableInit(void); + + float _noiseSamples[128]; + void initNoiseSamples(void); + + void average_quantized_coeffs(void); + void build_sb_samples_from_noise(int sb); + void fix_coding_method_array(int sb, int channels, sb_int8_array coding_method); + void fill_tone_level_array(int flag); + void fill_coding_method_array(sb_int8_array tone_level_idx, sb_int8_array tone_level_idx_temp, + sb_int8_array coding_method, int nb_channels, + int c, int superblocktype_2_3, int cm_table_select); + void synthfilt_build_sb_samples(GetBitContext *gb, int length, int sb_min, int sb_max); + void init_quantized_coeffs_elem0(int8 *quantized_coeffs, GetBitContext *gb, int length); + void init_tone_level_dequantization(GetBitContext *gb, int length); + void process_subpacket_9(QDM2SubPNode *node); + void process_subpacket_10(QDM2SubPNode *node, int length); + void process_subpacket_11(QDM2SubPNode *node, int length); + void process_subpacket_12(QDM2SubPNode *node, int length); + void process_synthesis_subpackets(QDM2SubPNode *list); + void qdm2_decode_super_block(void); + void qdm2_fft_init_coefficient(int sub_packet, int offset, int duration, + int channel, int exp, int phase); + void qdm2_fft_decode_tones(int duration, GetBitContext *gb, int b); + void qdm2_decode_fft_packets(void); + void qdm2_fft_generate_tone(FFTTone *tone); + void qdm2_fft_tone_synthesizer(uint8 sub_packet); + void qdm2_calculate_fft(int channel); + void qdm2_synthesis_filter(uint8 index); + int qdm2_decodeFrame(Common::SeekableReadStream *in); +}; + +// Fix compilation for non C99-compliant compilers, like MSVC +#ifndef int64_t +typedef signed long long int int64_t; +#endif + +// Integer log2 function. This is much faster than invoking +// double precision C99 log2 math functions or equivalent, since +// this is only used to determine maximum number of bits needed +// i.e. only non-fractional part is needed. Also, the double +// version is incorrect for exact cases due to floating point +// rounding errors. +static inline int scummvm_log2(int n) { + int ret = -1; + while(n != 0) { + n /= 2; + ret++; + } + return ret; +} + +#define QDM2_LIST_ADD(list, size, packet) \ + do { \ + if (size > 0) \ + list[size - 1].next = &list[size]; \ + list[size].packet = packet; \ + list[size].next = NULL; \ + size++; \ + } while(0) + +// Result is 8, 16 or 30 +#define QDM2_SB_USED(subSampling) (((subSampling) >= 2) ? 30 : 8 << (subSampling)) + +#define FIX_NOISE_IDX(noiseIdx) \ + if ((noiseIdx) >= 3840) \ + (noiseIdx) -= 3840 \ + +#define SB_DITHERING_NOISE(sb, noiseIdx) (_noiseTable[(noiseIdx)++] * sb_noise_attenuation[(sb)]) + +static inline void initGetBits(GetBitContext *s, const uint8 *buffer, int bitSize) { + int bufferSize = (bitSize + 7) >> 3; + + if (bufferSize < 0 || bitSize < 0) { + bufferSize = bitSize = 0; + buffer = NULL; + } + + s->buffer = buffer; + s->sizeInBits = bitSize; + s->bufferEnd = buffer + bufferSize; + s->index = 0; +} + +static inline int getBitsCount(GetBitContext *s) { + return s->index; +} + +static inline unsigned int getBits1(GetBitContext *s) { + int index; + uint8 result; + + index = s->index; + result = s->buffer[index >> 3]; + + result >>= (index & 0x07); + result &= 1; + index++; + s->index = index; + + return result; +} + +static inline unsigned int getBits(GetBitContext *s, int n) { + int tmp, reCache, reIndex; + + reIndex = s->index; + + reCache = READ_LE_UINT32((const uint8 *)s->buffer + (reIndex >> 3)) >> (reIndex & 0x07); + + tmp = (reCache) & ((uint32)0xffffffff >> (32 - n)); + + s->index = reIndex + n; + + return tmp; +} + +static inline void skipBits(GetBitContext *s, int n) { + s->index += n; +} + +#define BITS_LEFT(length, gb) ((length) - getBitsCount((gb))) + +static int splitRadixPermutation(int i, int n, int inverse) { + if (n <= 2) + return i & 1; + + int m = n >> 1; + + if(!(i & m)) + return splitRadixPermutation(i, m, inverse) * 2; + + m >>= 1; + + if (inverse == !(i & m)) + return splitRadixPermutation(i, m, inverse) * 4 + 1; + + return splitRadixPermutation(i, m, inverse) * 4 - 1; +} + +// sin(2*pi*x/n) for 0<=x<n/4, followed by n/2<=x<3n/4 +float ff_sin_16[8]; +float ff_sin_32[16]; +float ff_sin_64[32]; +float ff_sin_128[64]; +float ff_sin_256[128]; +float ff_sin_512[256]; +float ff_sin_1024[512]; +float ff_sin_2048[1024]; +float ff_sin_4096[2048]; +float ff_sin_8192[4096]; +float ff_sin_16384[8192]; +float ff_sin_32768[16384]; +float ff_sin_65536[32768]; + +float *ff_sin_tabs[] = { + NULL, NULL, NULL, NULL, + ff_sin_16, ff_sin_32, ff_sin_64, ff_sin_128, ff_sin_256, ff_sin_512, ff_sin_1024, + ff_sin_2048, ff_sin_4096, ff_sin_8192, ff_sin_16384, ff_sin_32768, ff_sin_65536, +}; + +// cos(2*pi*x/n) for 0<=x<=n/4, followed by its reverse +float ff_cos_16[8]; +float ff_cos_32[16]; +float ff_cos_64[32]; +float ff_cos_128[64]; +float ff_cos_256[128]; +float ff_cos_512[256]; +float ff_cos_1024[512]; +float ff_cos_2048[1024]; +float ff_cos_4096[2048]; +float ff_cos_8192[4096]; +float ff_cos_16384[8192]; +float ff_cos_32768[16384]; +float ff_cos_65536[32768]; + +float *ff_cos_tabs[] = { + NULL, NULL, NULL, NULL, + ff_cos_16, ff_cos_32, ff_cos_64, ff_cos_128, ff_cos_256, ff_cos_512, ff_cos_1024, + ff_cos_2048, ff_cos_4096, ff_cos_8192, ff_cos_16384, ff_cos_32768, ff_cos_65536, +}; + +void initCosineTables(int index) { + int m = 1 << index; + double freq = 2 * M_PI / m; + float *tab = ff_cos_tabs[index]; + + for (int i = 0; i <= m / 4; i++) + tab[i] = cos(i * freq); + + for (int i = 1; i < m / 4; i++) + tab[m / 2 - i] = tab[i]; +} + +void fftPermute(FFTContext *s, FFTComplex *z) { + const uint16 *revtab = s->revtab; + int np = 1 << s->nbits; + + if (s->tmpBuf) { + // TODO: handle split-radix permute in a more optimal way, probably in-place + for (int j = 0; j < np; j++) + s->tmpBuf[revtab[j]] = z[j]; + memcpy(z, s->tmpBuf, np * sizeof(FFTComplex)); + return; + } + + // reverse + for (int j = 0; j < np; j++) { + int k = revtab[j]; + if (k < j) { + FFTComplex tmp = z[k]; + z[k] = z[j]; + z[j] = tmp; + } + } +} + +#define DECL_FFT(n,n2,n4) \ +static void fft##n(FFTComplex *z) { \ + fft##n2(z); \ + fft##n4(z + n4 * 2); \ + fft##n4(z + n4 * 3); \ + pass(z, ff_cos_##n, n4 / 2); \ +} + +#ifndef M_SQRT1_2 +#define M_SQRT1_2 7.0710678118654752440E-1 +#endif + +#define sqrthalf (float)M_SQRT1_2 + +#define BF(x,y,a,b) { \ + x = a - b; \ + y = a + b; \ +} + +#define BUTTERFLIES(a0, a1, a2, a3) { \ + BF(t3, t5, t5, t1); \ + BF(a2.re, a0.re, a0.re, t5); \ + BF(a3.im, a1.im, a1.im, t3); \ + BF(t4, t6, t2, t6); \ + BF(a3.re, a1.re, a1.re, t4); \ + BF(a2.im, a0.im, a0.im, t6); \ +} + +// force loading all the inputs before storing any. +// this is slightly slower for small data, but avoids store->load aliasing +// for addresses separated by large powers of 2. +#define BUTTERFLIES_BIG(a0, a1, a2, a3) { \ + float r0 = a0.re, i0 = a0.im, r1 = a1.re, i1 = a1.im; \ + BF(t3, t5, t5, t1); \ + BF(a2.re, a0.re, r0, t5); \ + BF(a3.im, a1.im, i1, t3); \ + BF(t4, t6, t2, t6); \ + BF(a3.re, a1.re, r1, t4); \ + BF(a2.im, a0.im, i0, t6); \ +} + +#define TRANSFORM(a0, a1, a2, a3, wre, wim) { \ + t1 = a2.re * wre + a2.im * wim; \ + t2 = a2.im * wre - a2.re * wim; \ + t5 = a3.re * wre - a3.im * wim; \ + t6 = a3.im * wre + a3.re * wim; \ + BUTTERFLIES(a0, a1, a2, a3) \ +} + +#define TRANSFORM_ZERO(a0, a1, a2, a3) { \ + t1 = a2.re; \ + t2 = a2.im; \ + t5 = a3.re; \ + t6 = a3.im; \ + BUTTERFLIES(a0, a1, a2, a3) \ +} + +// z[0...8n-1], w[1...2n-1] +#define PASS(name) \ +static void name(FFTComplex *z, const float *wre, unsigned int n) { \ + float t1, t2, t3, t4, t5, t6; \ + int o1 = 2 * n; \ + int o2 = 4 * n; \ + int o3 = 6 * n; \ + const float *wim = wre + o1; \ + n--; \ + \ + TRANSFORM_ZERO(z[0], z[o1], z[o2], z[o3]); \ + TRANSFORM(z[1], z[o1 + 1], z[o2 + 1], z[o3 + 1], wre[1], wim[-1]); \ + \ + do { \ + z += 2; \ + wre += 2; \ + wim -= 2; \ + TRANSFORM(z[0], z[o1], z[o2], z[o3], wre[0], wim[0]); \ + TRANSFORM(z[1], z[o1 + 1],z[o2 + 1], z[o3 + 1], wre[1], wim[-1]); \ + } while(--n); \ +} + +PASS(pass) +#undef BUTTERFLIES +#define BUTTERFLIES BUTTERFLIES_BIG +PASS(pass_big) + +static void fft4(FFTComplex *z) { + float t1, t2, t3, t4, t5, t6, t7, t8; + + BF(t3, t1, z[0].re, z[1].re); + BF(t8, t6, z[3].re, z[2].re); + BF(z[2].re, z[0].re, t1, t6); + BF(t4, t2, z[0].im, z[1].im); + BF(t7, t5, z[2].im, z[3].im); + BF(z[3].im, z[1].im, t4, t8); + BF(z[3].re, z[1].re, t3, t7); + BF(z[2].im, z[0].im, t2, t5); +} + +static void fft8(FFTComplex *z) { + float t1, t2, t3, t4, t5, t6, t7, t8; + + fft4(z); + + BF(t1, z[5].re, z[4].re, -z[5].re); + BF(t2, z[5].im, z[4].im, -z[5].im); + BF(t3, z[7].re, z[6].re, -z[7].re); + BF(t4, z[7].im, z[6].im, -z[7].im); + BF(t8, t1, t3, t1); + BF(t7, t2, t2, t4); + BF(z[4].re, z[0].re, z[0].re, t1); + BF(z[4].im, z[0].im, z[0].im, t2); + BF(z[6].re, z[2].re, z[2].re, t7); + BF(z[6].im, z[2].im, z[2].im, t8); + + TRANSFORM(z[1], z[3], z[5], z[7], sqrthalf, sqrthalf); +} + +#undef BF + +DECL_FFT(16,8,4) +DECL_FFT(32,16,8) +DECL_FFT(64,32,16) +DECL_FFT(128,64,32) +DECL_FFT(256,128,64) +DECL_FFT(512,256,128) +#define pass pass_big +DECL_FFT(1024,512,256) +DECL_FFT(2048,1024,512) +DECL_FFT(4096,2048,1024) +DECL_FFT(8192,4096,2048) +DECL_FFT(16384,8192,4096) +DECL_FFT(32768,16384,8192) +DECL_FFT(65536,32768,16384) + +void fftCalc(FFTContext *s, FFTComplex *z) { + static void (* const fftDispatch[])(FFTComplex*) = { + fft4, fft8, fft16, fft32, fft64, fft128, fft256, fft512, fft1024, + fft2048, fft4096, fft8192, fft16384, fft32768, fft65536, + }; + + fftDispatch[s->nbits - 2](z); +} + +// complex multiplication: p = a * b +#define CMUL(pre, pim, are, aim, bre, bim) \ +{\ + float _are = (are); \ + float _aim = (aim); \ + float _bre = (bre); \ + float _bim = (bim); \ + (pre) = _are * _bre - _aim * _bim; \ + (pim) = _are * _bim + _aim * _bre; \ +} + +/** + * Compute the middle half of the inverse MDCT of size N = 2^nbits, + * thus excluding the parts that can be derived by symmetry + * @param output N/2 samples + * @param input N/2 samples + */ +void imdctHalfC(FFTContext *s, float *output, const float *input) { + const uint16 *revtab = s->revtab; + const float *tcos = s->tcos; + const float *tsin = s->tsin; + FFTComplex *z = (FFTComplex *)output; + + int n = 1 << s->mdctBits; + int n2 = n >> 1; + int n4 = n >> 2; + int n8 = n >> 3; + + // pre rotation + const float *in1 = input; + const float *in2 = input + n2 - 1; + for (int k = 0; k < n4; k++) { + int j = revtab[k]; + CMUL(z[j].re, z[j].im, *in2, *in1, tcos[k], tsin[k]); + in1 += 2; + in2 -= 2; + } + + fftCalc(s, z); + + // post rotation + reordering + for (int k = 0; k < n8; k++) { + float r0, i0, r1, i1; + CMUL(r0, i1, z[n8 - k - 1].im, z[n8 - k - 1].re, tsin[n8 - k - 1], tcos[n8 - k - 1]); + CMUL(r1, i0, z[n8 + k].im, z[n8 + k].re, tsin[n8 + k], tcos[n8 + k]); + z[n8 - k - 1].re = r0; + z[n8 - k - 1].im = i0; + z[n8 + k].re = r1; + z[n8 + k].im = i1; + } +} + +/** + * Compute inverse MDCT of size N = 2^nbits + * @param output N samples + * @param input N/2 samples + */ +void imdctCalcC(FFTContext *s, float *output, const float *input) { + int n = 1 << s->mdctBits; + int n2 = n >> 1; + int n4 = n >> 2; + + imdctHalfC(s, output + n4, input); + + for (int k = 0; k < n4; k++) { + output[k] = -output[n2 - k - 1]; + output[n - k - 1] = output[n2 + k]; + } +} + +/** + * Compute MDCT of size N = 2^nbits + * @param input N samples + * @param out N/2 samples + */ +void mdctCalcC(FFTContext *s, float *out, const float *input) { + const uint16 *revtab = s->revtab; + const float *tcos = s->tcos; + const float *tsin = s->tsin; + FFTComplex *x = (FFTComplex *)out; + + int n = 1 << s->mdctBits; + int n2 = n >> 1; + int n4 = n >> 2; + int n8 = n >> 3; + int n3 = 3 * n4; + + // pre rotation + for (int i = 0; i < n8; i++) { + float re = -input[2 * i + 3 * n4] - input[n3 - 1 - 2 * i]; + float im = -input[n4 + 2 * i] + input[n4 - 1 - 2 * i]; + int j = revtab[i]; + CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]); + + re = input[2 * i] - input[n2 - 1 - 2 * i]; + im = -(input[n2 + 2 * i] + input[n - 1 - 2 * i]); + j = revtab[n8 + i]; + CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]); + } + + fftCalc(s, x); + + // post rotation + for (int i = 0; i < n8; i++) { + float r0, i0, r1, i1; + CMUL(i1, r0, x[n8 - i - 1].re, x[n8 - i - 1].im, -tsin[n8 - i - 1], -tcos[n8 - i - 1]); + CMUL(i0, r1, x[n8 + i].re, x[n8 + i].im, -tsin[n8 + i], -tcos[n8 + i]); + x[n8 - i - 1].re = r0; + x[n8 - i - 1].im = i0; + x[n8 + i].re = r1; + x[n8 + i].im = i1; + } +} + +int fftInit(FFTContext *s, int nbits, int inverse) { + int i, j, m, n; + float alpha, c1, s1, s2; + + if (nbits < 2 || nbits > 16) + goto fail; + + s->nbits = nbits; + n = 1 << nbits; + s->tmpBuf = NULL; + + s->exptab = (FFTComplex *)malloc((n / 2) * sizeof(FFTComplex)); + if (!s->exptab) + goto fail; + + s->revtab = (uint16 *)malloc(n * sizeof(uint16)); + if (!s->revtab) + goto fail; + s->inverse = inverse; + + s2 = inverse ? 1.0 : -1.0; + + s->fftPermute = fftPermute; + s->fftCalc = fftCalc; + s->imdctCalc = imdctCalcC; + s->imdctHalf = imdctHalfC; + s->mdctCalc = mdctCalcC; + s->splitRadix = 1; + + if (s->splitRadix) { + for (j = 4; j <= nbits; j++) + initCosineTables(j); + + for (i = 0; i < n; i++) + s->revtab[-splitRadixPermutation(i, n, s->inverse) & (n - 1)] = i; + + s->tmpBuf = (FFTComplex *)malloc(n * sizeof(FFTComplex)); + } else { + for (i = 0; i < n / 2; i++) { + alpha = 2 * M_PI * (float)i / (float)n; + c1 = cos(alpha); + s1 = sin(alpha) * s2; + s->exptab[i].re = c1; + s->exptab[i].im = s1; + } + + //int np = 1 << nbits; + //int nblocks = np >> 3; + //int np2 = np >> 1; + + // compute bit reverse table + for (i = 0; i < n; i++) { + m = 0; + + for (j = 0; j < nbits; j++) + m |= ((i >> j) & 1) << (nbits - j - 1); + + s->revtab[i] = m; + } + } + + return 0; + + fail: + free(&s->revtab); + free(&s->exptab); + free(&s->tmpBuf); + return -1; +} + +/** + * Sets up a real FFT. + * @param nbits log2 of the length of the input array + * @param trans the type of transform + */ +int rdftInit(RDFTContext *s, int nbits, RDFTransformType trans) { + int n = 1 << nbits; + const double theta = (trans == RDFT || trans == IRIDFT ? -1 : 1) * 2 * M_PI / n; + + s->nbits = nbits; + s->inverse = trans == IRDFT || trans == IRIDFT; + s->signConvention = trans == RIDFT || trans == IRIDFT ? 1 : -1; + + if (nbits < 4 || nbits > 16) + return -1; + + if (fftInit(&s->fft, nbits - 1, trans == IRDFT || trans == RIDFT) < 0) + return -1; + + initCosineTables(nbits); + s->tcos = ff_cos_tabs[nbits]; + s->tsin = ff_sin_tabs[nbits] + (trans == RDFT || trans == IRIDFT) * (n >> 2); + + for (int i = 0; i < n >> 2; i++) + s->tsin[i] = sin(i*theta); + + return 0; +} + +/** Map one real FFT into two parallel real even and odd FFTs. Then interleave + * the two real FFTs into one complex FFT. Unmangle the results. + * ref: http://www.engineeringproductivitytools.com/stuff/T0001/PT10.HTM + */ +void rdftCalc(RDFTContext *s, float *data) { + FFTComplex ev, od; + + const int n = 1 << s->nbits; + const float k1 = 0.5; + const float k2 = 0.5 - s->inverse; + const float *tcos = s->tcos; + const float *tsin = s->tsin; + + if (!s->inverse) { + fftPermute(&s->fft, (FFTComplex *)data); + fftCalc(&s->fft, (FFTComplex *)data); + } + + // i=0 is a special case because of packing, the DC term is real, so we + // are going to throw the N/2 term (also real) in with it. + ev.re = data[0]; + data[0] = ev.re + data[1]; + data[1] = ev.re - data[1]; + + int i; + + for (i = 1; i < n >> 2; i++) { + int i1 = i * 2; + int i2 = n - i1; + + // Separate even and odd FFTs + ev.re = k1 * (data[i1] + data[i2]); + od.im = -k2 * (data[i1] - data[i2]); + ev.im = k1 * (data[i1 + 1] - data[i2 + 1]); + od.re = k2 * (data[i1 + 1] + data[i2 + 1]); + + // Apply twiddle factors to the odd FFT and add to the even FFT + data[i1] = ev.re + od.re * tcos[i] - od.im * tsin[i]; + data[i1 + 1] = ev.im + od.im * tcos[i] + od.re * tsin[i]; + data[i2] = ev.re - od.re * tcos[i] + od.im * tsin[i]; + data[i2 + 1] = -ev.im + od.im * tcos[i] + od.re * tsin[i]; + } + + data[i * 2 + 1] = s->signConvention * data[i * 2 + 1]; + if (s->inverse) { + data[0] *= k1; + data[1] *= k1; + fftPermute(&s->fft, (FFTComplex*)data); + fftCalc(&s->fft, (FFTComplex*)data); + } +} + +// half mpeg encoding window (full precision) +const int32 ff_mpa_enwindow[257] = { + 0, -1, -1, -1, -1, -1, -1, -2, + -2, -2, -2, -3, -3, -4, -4, -5, + -5, -6, -7, -7, -8, -9, -10, -11, + -13, -14, -16, -17, -19, -21, -24, -26, + -29, -31, -35, -38, -41, -45, -49, -53, + -58, -63, -68, -73, -79, -85, -91, -97, + -104, -111, -117, -125, -132, -139, -147, -154, + -161, -169, -176, -183, -190, -196, -202, -208, + 213, 218, 222, 225, 227, 228, 228, 227, + 224, 221, 215, 208, 200, 189, 177, 163, + 146, 127, 106, 83, 57, 29, -2, -36, + -72, -111, -153, -197, -244, -294, -347, -401, + -459, -519, -581, -645, -711, -779, -848, -919, + -991, -1064, -1137, -1210, -1283, -1356, -1428, -1498, + -1567, -1634, -1698, -1759, -1817, -1870, -1919, -1962, + -2001, -2032, -2057, -2075, -2085, -2087, -2080, -2063, + 2037, 2000, 1952, 1893, 1822, 1739, 1644, 1535, + 1414, 1280, 1131, 970, 794, 605, 402, 185, + -45, -288, -545, -814, -1095, -1388, -1692, -2006, + -2330, -2663, -3004, -3351, -3705, -4063, -4425, -4788, + -5153, -5517, -5879, -6237, -6589, -6935, -7271, -7597, + -7910, -8209, -8491, -8755, -8998, -9219, -9416, -9585, + -9727, -9838, -9916, -9959, -9966, -9935, -9863, -9750, + -9592, -9389, -9139, -8840, -8492, -8092, -7640, -7134, + 6574, 5959, 5288, 4561, 3776, 2935, 2037, 1082, + 70, -998, -2122, -3300, -4533, -5818, -7154, -8540, + -9975,-11455,-12980,-14548,-16155,-17799,-19478,-21189, +-22929,-24694,-26482,-28289,-30112,-31947,-33791,-35640, +-37489,-39336,-41176,-43006,-44821,-46617,-48390,-50137, +-51853,-53534,-55178,-56778,-58333,-59838,-61289,-62684, +-64019,-65290,-66494,-67629,-68692,-69679,-70590,-71420, +-72169,-72835,-73415,-73908,-74313,-74630,-74856,-74992, + 75038 +}; + +void ff_mpa_synth_init(int16 *window) { + int i; + int32 v; + + // max = 18760, max sum over all 16 coefs : 44736 + for(i = 0; i < 257; i++) { + v = ff_mpa_enwindow[i]; + v = (v + 2) >> 2; + window[i] = v; + + if ((i & 63) != 0) + v = -v; + + if (i != 0) + window[512 - i] = v; + } +} + +static inline uint16 round_sample(int *sum) { + int sum1; + sum1 = (*sum) >> 14; + *sum &= (1 << 14)-1; + if (sum1 < (-0x7fff - 1)) + sum1 = (-0x7fff - 1); + if (sum1 > 0x7fff) + sum1 = 0x7fff; + return sum1; +} + +static inline int MULH(int a, int b) { + return ((int64_t)(a) * (int64_t)(b))>>32; +} + +// signed 16x16 -> 32 multiply add accumulate +#define MACS(rt, ra, rb) rt += (ra) * (rb) + +#define MLSS(rt, ra, rb) ((rt) -= (ra) * (rb)) + +#define SUM8(op, sum, w, p)\ +{\ + op(sum, (w)[0 * 64], (p)[0 * 64]);\ + op(sum, (w)[1 * 64], (p)[1 * 64]);\ + op(sum, (w)[2 * 64], (p)[2 * 64]);\ + op(sum, (w)[3 * 64], (p)[3 * 64]);\ + op(sum, (w)[4 * 64], (p)[4 * 64]);\ + op(sum, (w)[5 * 64], (p)[5 * 64]);\ + op(sum, (w)[6 * 64], (p)[6 * 64]);\ + op(sum, (w)[7 * 64], (p)[7 * 64]);\ +} + +#define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \ +{\ + tmp_s = p[0 * 64];\ + op1(sum1, (w1)[0 * 64], tmp_s);\ + op2(sum2, (w2)[0 * 64], tmp_s);\ + tmp_s = p[1 * 64];\ + op1(sum1, (w1)[1 * 64], tmp_s);\ + op2(sum2, (w2)[1 * 64], tmp_s);\ + tmp_s = p[2 * 64];\ + op1(sum1, (w1)[2 * 64], tmp_s);\ + op2(sum2, (w2)[2 * 64], tmp_s);\ + tmp_s = p[3 * 64];\ + op1(sum1, (w1)[3 * 64], tmp_s);\ + op2(sum2, (w2)[3 * 64], tmp_s);\ + tmp_s = p[4 * 64];\ + op1(sum1, (w1)[4 * 64], tmp_s);\ + op2(sum2, (w2)[4 * 64], tmp_s);\ + tmp_s = p[5 * 64];\ + op1(sum1, (w1)[5 * 64], tmp_s);\ + op2(sum2, (w2)[5 * 64], tmp_s);\ + tmp_s = p[6 * 64];\ + op1(sum1, (w1)[6 * 64], tmp_s);\ + op2(sum2, (w2)[6 * 64], tmp_s);\ + tmp_s = p[7 * 64];\ + op1(sum1, (w1)[7 * 64], tmp_s);\ + op2(sum2, (w2)[7 * 64], tmp_s);\ +} + +#define FIXHR(a) ((int)((a) * (1LL<<32) + 0.5)) + +// tab[i][j] = 1.0 / (2.0 * cos(pi*(2*k+1) / 2^(6 - j))) + +// cos(i*pi/64) + +#define COS0_0 FIXHR(0.50060299823519630134/2) +#define COS0_1 FIXHR(0.50547095989754365998/2) +#define COS0_2 FIXHR(0.51544730992262454697/2) +#define COS0_3 FIXHR(0.53104259108978417447/2) +#define COS0_4 FIXHR(0.55310389603444452782/2) +#define COS0_5 FIXHR(0.58293496820613387367/2) +#define COS0_6 FIXHR(0.62250412303566481615/2) +#define COS0_7 FIXHR(0.67480834145500574602/2) +#define COS0_8 FIXHR(0.74453627100229844977/2) +#define COS0_9 FIXHR(0.83934964541552703873/2) +#define COS0_10 FIXHR(0.97256823786196069369/2) +#define COS0_11 FIXHR(1.16943993343288495515/4) +#define COS0_12 FIXHR(1.48416461631416627724/4) +#define COS0_13 FIXHR(2.05778100995341155085/8) +#define COS0_14 FIXHR(3.40760841846871878570/8) +#define COS0_15 FIXHR(10.19000812354805681150/32) + +#define COS1_0 FIXHR(0.50241928618815570551/2) +#define COS1_1 FIXHR(0.52249861493968888062/2) +#define COS1_2 FIXHR(0.56694403481635770368/2) +#define COS1_3 FIXHR(0.64682178335999012954/2) +#define COS1_4 FIXHR(0.78815462345125022473/2) +#define COS1_5 FIXHR(1.06067768599034747134/4) +#define COS1_6 FIXHR(1.72244709823833392782/4) +#define COS1_7 FIXHR(5.10114861868916385802/16) + +#define COS2_0 FIXHR(0.50979557910415916894/2) +#define COS2_1 FIXHR(0.60134488693504528054/2) +#define COS2_2 FIXHR(0.89997622313641570463/2) +#define COS2_3 FIXHR(2.56291544774150617881/8) + +#define COS3_0 FIXHR(0.54119610014619698439/2) +#define COS3_1 FIXHR(1.30656296487637652785/4) + +#define COS4_0 FIXHR(0.70710678118654752439/2) + +/* butterfly operator */ +#define BF(a, b, c, s)\ +{\ + tmp0 = tab[a] + tab[b];\ + tmp1 = tab[a] - tab[b];\ + tab[a] = tmp0;\ + tab[b] = MULH(tmp1<<(s), c);\ +} + +#define BF1(a, b, c, d)\ +{\ + BF(a, b, COS4_0, 1);\ + BF(c, d,-COS4_0, 1);\ + tab[c] += tab[d];\ +} + +#define BF2(a, b, c, d)\ +{\ + BF(a, b, COS4_0, 1);\ + BF(c, d,-COS4_0, 1);\ + tab[c] += tab[d];\ + tab[a] += tab[c];\ + tab[c] += tab[b];\ + tab[b] += tab[d];\ +} + +#define ADD(a, b) tab[a] += tab[b] + +// DCT32 without 1/sqrt(2) coef zero scaling. +static void dct32(int32 *out, int32 *tab) { + int tmp0, tmp1; + + // pass 1 + BF( 0, 31, COS0_0 , 1); + BF(15, 16, COS0_15, 5); + // pass 2 + BF( 0, 15, COS1_0 , 1); + BF(16, 31,-COS1_0 , 1); + // pass 1 + BF( 7, 24, COS0_7 , 1); + BF( 8, 23, COS0_8 , 1); + // pass 2 + BF( 7, 8, COS1_7 , 4); + BF(23, 24,-COS1_7 , 4); + // pass 3 + BF( 0, 7, COS2_0 , 1); + BF( 8, 15,-COS2_0 , 1); + BF(16, 23, COS2_0 , 1); + BF(24, 31,-COS2_0 , 1); + // pass 1 + BF( 3, 28, COS0_3 , 1); + BF(12, 19, COS0_12, 2); + // pass 2 + BF( 3, 12, COS1_3 , 1); + BF(19, 28,-COS1_3 , 1); + // pass 1 + BF( 4, 27, COS0_4 , 1); + BF(11, 20, COS0_11, 2); + // pass 2 + BF( 4, 11, COS1_4 , 1); + BF(20, 27,-COS1_4 , 1); + // pass 3 + BF( 3, 4, COS2_3 , 3); + BF(11, 12,-COS2_3 , 3); + BF(19, 20, COS2_3 , 3); + BF(27, 28,-COS2_3 , 3); + // pass 4 + BF( 0, 3, COS3_0 , 1); + BF( 4, 7,-COS3_0 , 1); + BF( 8, 11, COS3_0 , 1); + BF(12, 15,-COS3_0 , 1); + BF(16, 19, COS3_0 , 1); + BF(20, 23,-COS3_0 , 1); + BF(24, 27, COS3_0 , 1); + BF(28, 31,-COS3_0 , 1); + + // pass 1 + BF( 1, 30, COS0_1 , 1); + BF(14, 17, COS0_14, 3); + // pass 2 + BF( 1, 14, COS1_1 , 1); + BF(17, 30,-COS1_1 , 1); + // pass 1 + BF( 6, 25, COS0_6 , 1); + BF( 9, 22, COS0_9 , 1); + // pass 2 + BF( 6, 9, COS1_6 , 2); + BF(22, 25,-COS1_6 , 2); + // pass 3 + BF( 1, 6, COS2_1 , 1); + BF( 9, 14,-COS2_1 , 1); + BF(17, 22, COS2_1 , 1); + BF(25, 30,-COS2_1 , 1); + + // pass 1 + BF( 2, 29, COS0_2 , 1); + BF(13, 18, COS0_13, 3); + // pass 2 + BF( 2, 13, COS1_2 , 1); + BF(18, 29,-COS1_2 , 1); + // pass 1 + BF( 5, 26, COS0_5 , 1); + BF(10, 21, COS0_10, 1); + // pass 2 + BF( 5, 10, COS1_5 , 2); + BF(21, 26,-COS1_5 , 2); + // pass 3 + BF( 2, 5, COS2_2 , 1); + BF(10, 13,-COS2_2 , 1); + BF(18, 21, COS2_2 , 1); + BF(26, 29,-COS2_2 , 1); + // pass 4 + BF( 1, 2, COS3_1 , 2); + BF( 5, 6,-COS3_1 , 2); + BF( 9, 10, COS3_1 , 2); + BF(13, 14,-COS3_1 , 2); + BF(17, 18, COS3_1 , 2); + BF(21, 22,-COS3_1 , 2); + BF(25, 26, COS3_1 , 2); + BF(29, 30,-COS3_1 , 2); + + // pass 5 + BF1( 0, 1, 2, 3); + BF2( 4, 5, 6, 7); + BF1( 8, 9, 10, 11); + BF2(12, 13, 14, 15); + BF1(16, 17, 18, 19); + BF2(20, 21, 22, 23); + BF1(24, 25, 26, 27); + BF2(28, 29, 30, 31); + + // pass 6 + ADD( 8, 12); + ADD(12, 10); + ADD(10, 14); + ADD(14, 9); + ADD( 9, 13); + ADD(13, 11); + ADD(11, 15); + + out[ 0] = tab[0]; + out[16] = tab[1]; + out[ 8] = tab[2]; + out[24] = tab[3]; + out[ 4] = tab[4]; + out[20] = tab[5]; + out[12] = tab[6]; + out[28] = tab[7]; + out[ 2] = tab[8]; + out[18] = tab[9]; + out[10] = tab[10]; + out[26] = tab[11]; + out[ 6] = tab[12]; + out[22] = tab[13]; + out[14] = tab[14]; + out[30] = tab[15]; + + ADD(24, 28); + ADD(28, 26); + ADD(26, 30); + ADD(30, 25); + ADD(25, 29); + ADD(29, 27); + ADD(27, 31); + + out[ 1] = tab[16] + tab[24]; + out[17] = tab[17] + tab[25]; + out[ 9] = tab[18] + tab[26]; + out[25] = tab[19] + tab[27]; + out[ 5] = tab[20] + tab[28]; + out[21] = tab[21] + tab[29]; + out[13] = tab[22] + tab[30]; + out[29] = tab[23] + tab[31]; + out[ 3] = tab[24] + tab[20]; + out[19] = tab[25] + tab[21]; + out[11] = tab[26] + tab[22]; + out[27] = tab[27] + tab[23]; + out[ 7] = tab[28] + tab[18]; + out[23] = tab[29] + tab[19]; + out[15] = tab[30] + tab[17]; + out[31] = tab[31]; +} + +// 32 sub band synthesis filter. Input: 32 sub band samples, Output: +// 32 samples. +// XXX: optimize by avoiding ring buffer usage +void ff_mpa_synth_filter(int16 *synth_buf_ptr, int *synth_buf_offset, + int16 *window, int *dither_state, + int16 *samples, int incr, + int32 sb_samples[32]) +{ + int16 *synth_buf; + const int16 *w, *w2, *p; + int j, offset; + int16 *samples2; + int32 tmp[32]; + int sum, sum2; + int tmp_s; + + offset = *synth_buf_offset; + synth_buf = synth_buf_ptr + offset; + + dct32(tmp, sb_samples); + for(j = 0; j < 32; j++) { + // NOTE: can cause a loss in precision if very high amplitude sound + if (tmp[j] < (-0x7fff - 1)) + synth_buf[j] = (-0x7fff - 1); + else if (tmp[j] > 0x7fff) + synth_buf[j] = 0x7fff; + else + synth_buf[j] = tmp[j]; + } + + // copy to avoid wrap + memcpy(synth_buf + 512, synth_buf, 32 * sizeof(int16)); + + samples2 = samples + 31 * incr; + w = window; + w2 = window + 31; + + sum = *dither_state; + p = synth_buf + 16; + SUM8(MACS, sum, w, p); + p = synth_buf + 48; + SUM8(MLSS, sum, w + 32, p); + *samples = round_sample(&sum); + samples += incr; + w++; + + // we calculate two samples at the same time to avoid one memory + // access per two sample + for(j = 1; j < 16; j++) { + sum2 = 0; + p = synth_buf + 16 + j; + SUM8P2(sum, MACS, sum2, MLSS, w, w2, p); + p = synth_buf + 48 - j; + SUM8P2(sum, MLSS, sum2, MLSS, w + 32, w2 + 32, p); + + *samples = round_sample(&sum); + samples += incr; + sum += sum2; + *samples2 = round_sample(&sum); + samples2 -= incr; + w++; + w2--; + } + + p = synth_buf + 32; + SUM8(MLSS, sum, w + 32, p); + *samples = round_sample(&sum); + *dither_state= sum; + + offset = (offset - 32) & 511; + *synth_buf_offset = offset; +} + +/** + * parses a vlc code, faster then get_vlc() + * @param bits is the number of bits which will be read at once, must be + * identical to nb_bits in init_vlc() + * @param max_depth is the number of times bits bits must be read to completely + * read the longest vlc code + * = (max_vlc_length + bits - 1) / bits + */ +static int getVlc2(GetBitContext *s, int16 (*table)[2], int bits, int maxDepth) { + int reIndex; + int reCache; + int index; + int code; + int n; + + reIndex = s->index; + reCache = READ_LE_UINT32(s->buffer + (reIndex >> 3)) >> (reIndex & 0x07); + index = reCache & (0xffffffff >> (32 - bits)); + code = table[index][0]; + n = table[index][1]; + + if (maxDepth > 1 && n < 0){ + reIndex += bits; + reCache = READ_LE_UINT32(s->buffer + (reIndex >> 3)) >> (reIndex & 0x07); + + int nbBits = -n; + + index = (reCache & (0xffffffff >> (32 - nbBits))) + code; + code = table[index][0]; + n = table[index][1]; + + if(maxDepth > 2 && n < 0) { + reIndex += nbBits; + reCache = READ_LE_UINT32(s->buffer + (reIndex >> 3)) >> (reIndex & 0x07); + + nbBits = -n; + + index = (reCache & (0xffffffff >> (32 - nbBits))) + code; + code = table[index][0]; + n = table[index][1]; + } + } + + reCache >>= n; + s->index = reIndex + n; + return code; +} + +static int allocTable(VLC *vlc, int size, int use_static) { + int index; + int16 (*temp)[2] = NULL; + index = vlc->table_size; + vlc->table_size += size; + if (vlc->table_size > vlc->table_allocated) { + if(use_static) + error("QDM2 cant do anything, init_vlc() is used with too little memory"); + vlc->table_allocated += (1 << vlc->bits); + temp = (int16 (*)[2])realloc(vlc->table, sizeof(int16 *) * 2 * vlc->table_allocated); + if (!temp) { + free(vlc->table); + vlc->table = NULL; + return -1; + } + vlc->table = temp; + } + return index; +} + +#define GET_DATA(v, table, i, wrap, size)\ +{\ + const uint8 *ptr = (const uint8 *)table + i * wrap;\ + switch(size) {\ + case 1:\ + v = *(const uint8 *)ptr;\ + break;\ + case 2:\ + v = *(const uint16 *)ptr;\ + break;\ + default:\ + v = *(const uint32 *)ptr;\ + break;\ + }\ +} + +static int build_table(VLC *vlc, int table_nb_bits, + int nb_codes, + const void *bits, int bits_wrap, int bits_size, + const void *codes, int codes_wrap, int codes_size, + const void *symbols, int symbols_wrap, int symbols_size, + int code_prefix, int n_prefix, int flags) +{ + int i, j, k, n, table_size, table_index, nb, n1, index, code_prefix2, symbol; + uint32 code; + int16 (*table)[2]; + + table_size = 1 << table_nb_bits; + table_index = allocTable(vlc, table_size, flags & 4); + if (table_index < 0) + return -1; + table = &vlc->table[table_index]; + + for(i = 0; i < table_size; i++) { + table[i][1] = 0; //bits + table[i][0] = -1; //codes + } + + // first pass: map codes and compute auxillary table sizes + for(i = 0; i < nb_codes; i++) { + GET_DATA(n, bits, i, bits_wrap, bits_size); + GET_DATA(code, codes, i, codes_wrap, codes_size); + // we accept tables with holes + if (n <= 0) + continue; + if (!symbols) + symbol = i; + else + GET_DATA(symbol, symbols, i, symbols_wrap, symbols_size); + // if code matches the prefix, it is in the table + n -= n_prefix; + if(flags & 2) + code_prefix2= code & (n_prefix>=32 ? 0xffffffff : (1 << n_prefix)-1); + else + code_prefix2= code >> n; + if (n > 0 && code_prefix2 == code_prefix) { + if (n <= table_nb_bits) { + // no need to add another table + j = (code << (table_nb_bits - n)) & (table_size - 1); + nb = 1 << (table_nb_bits - n); + for(k = 0; k < nb; k++) { + if(flags & 2) + j = (code >> n_prefix) + (k<<n); + if (table[j][1] /*bits*/ != 0) { + error("QDM2 incorrect codes"); + return -1; + } + table[j][1] = n; //bits + table[j][0] = symbol; + j++; + } + } else { + n -= table_nb_bits; + j = (code >> ((flags & 2) ? n_prefix : n)) & ((1 << table_nb_bits) - 1); + // compute table size + n1 = -table[j][1]; //bits + if (n > n1) + n1 = n; + table[j][1] = -n1; //bits + } + } + } + + // second pass : fill auxillary tables recursively + for(i = 0;i < table_size; i++) { + n = table[i][1]; //bits + if (n < 0) { + n = -n; + if (n > table_nb_bits) { + n = table_nb_bits; + table[i][1] = -n; //bits + } + index = build_table(vlc, n, nb_codes, + bits, bits_wrap, bits_size, + codes, codes_wrap, codes_size, + symbols, symbols_wrap, symbols_size, + (flags & 2) ? (code_prefix | (i << n_prefix)) : ((code_prefix << table_nb_bits) | i), + n_prefix + table_nb_bits, flags); + if (index < 0) + return -1; + // note: realloc has been done, so reload tables + table = &vlc->table[table_index]; + table[i][0] = index; //code + } + } + return table_index; +} + +/* Build VLC decoding tables suitable for use with get_vlc(). + + 'nb_bits' set thee decoding table size (2^nb_bits) entries. The + bigger it is, the faster is the decoding. But it should not be too + big to save memory and L1 cache. '9' is a good compromise. + + 'nb_codes' : number of vlcs codes + + 'bits' : table which gives the size (in bits) of each vlc code. + + 'codes' : table which gives the bit pattern of of each vlc code. + + 'symbols' : table which gives the values to be returned from get_vlc(). + + 'xxx_wrap' : give the number of bytes between each entry of the + 'bits' or 'codes' tables. + + 'xxx_size' : gives the number of bytes of each entry of the 'bits' + or 'codes' tables. + + 'wrap' and 'size' allows to use any memory configuration and types + (byte/word/long) to store the 'bits', 'codes', and 'symbols' tables. + + 'use_static' should be set to 1 for tables, which should be freed + with av_free_static(), 0 if free_vlc() will be used. +*/ +void initVlcSparse(VLC *vlc, int nb_bits, int nb_codes, + const void *bits, int bits_wrap, int bits_size, + const void *codes, int codes_wrap, int codes_size, + const void *symbols, int symbols_wrap, int symbols_size) { + vlc->bits = nb_bits; + + if(vlc->table_size && vlc->table_size == vlc->table_allocated) { + return; + } else if(vlc->table_size) { + error("called on a partially initialized table"); + } + + if (build_table(vlc, nb_bits, nb_codes, + bits, bits_wrap, bits_size, + codes, codes_wrap, codes_size, + symbols, symbols_wrap, symbols_size, + 0, 0, 4 | 2) < 0) { + free(&vlc->table); + return; // Error + } + + if(vlc->table_size != vlc->table_allocated) + error("QDM2 needed %d had %d", vlc->table_size, vlc->table_allocated); +} + +void QDM2Stream::softclipTableInit(void) { + uint16 i; + double dfl = SOFTCLIP_THRESHOLD - 32767; + float delta = 1.0 / -dfl; + + for (i = 0; i < ARRAYSIZE(_softclipTable); i++) + _softclipTable[i] = SOFTCLIP_THRESHOLD - ((int)(sin((float)i * delta) * dfl) & 0x0000FFFF); +} + +// random generated table +void QDM2Stream::rndTableInit(void) { + uint16 i; + uint16 j; + uint32 ldw, hdw; + // TODO: Replace Code with uint64 less version... + int64_t tmp64_1; + int64_t random_seed = 0; + float delta = 1.0 / 16384.0; + + for(i = 0; i < ARRAYSIZE(_noiseTable); i++) { + random_seed = random_seed * 214013 + 2531011; + _noiseTable[i] = (delta * (float)(((int32)random_seed >> 16) & 0x00007FFF)- 1.0) * 1.3; + } + + for (i = 0; i < 256; i++) { + random_seed = 81; + ldw = i; + for (j = 0; j < 5; j++) { + _randomDequantIndex[i][j] = (uint8)((ldw / random_seed) & 0xFF); + ldw = (uint32)ldw % (uint32)random_seed; + tmp64_1 = (random_seed * 0x55555556); + hdw = (uint32)(tmp64_1 >> 32); + random_seed = (int64_t)(hdw + (ldw >> 31)); + } + } + + for (i = 0; i < 128; i++) { + random_seed = 25; + ldw = i; + for (j = 0; j < 3; j++) { + _randomDequantType24[i][j] = (uint8)((ldw / random_seed) & 0xFF); + ldw = (uint32)ldw % (uint32)random_seed; + tmp64_1 = (random_seed * 0x66666667); + hdw = (uint32)(tmp64_1 >> 33); + random_seed = hdw + (ldw >> 31); + } + } +} + +void QDM2Stream::initNoiseSamples(void) { + uint16 i; + uint32 random_seed = 0; + float delta = 1.0 / 16384.0; + + for (i = 0; i < ARRAYSIZE(_noiseSamples); i++) { + random_seed = random_seed * 214013 + 2531011; + _noiseSamples[i] = (delta * (float)((random_seed >> 16) & 0x00007fff) - 1.0); + } +} + +static const uint16 qdm2_vlc_offs[18] = { + 0, 260, 566, 598, 894, 1166, 1230, 1294, 1678, 1950, 2214, 2278, 2310, 2570, 2834, 3124, 3448, 3838 +}; + +void QDM2Stream::initVlc(void) { + static int16 qdm2_table[3838][2]; + + if (!_vlcsInitialized) { + _vlcTabLevel.table = &qdm2_table[qdm2_vlc_offs[0]]; + _vlcTabLevel.table_allocated = qdm2_vlc_offs[1] - qdm2_vlc_offs[0]; + _vlcTabLevel.table_size = 0; + initVlcSparse(&_vlcTabLevel, 8, 24, + vlc_tab_level_huffbits, 1, 1, + vlc_tab_level_huffcodes, 2, 2, NULL, 0, 0); + + _vlcTabDiff.table = &qdm2_table[qdm2_vlc_offs[1]]; + _vlcTabDiff.table_allocated = qdm2_vlc_offs[2] - qdm2_vlc_offs[1]; + _vlcTabDiff.table_size = 0; + initVlcSparse(&_vlcTabDiff, 8, 37, + vlc_tab_diff_huffbits, 1, 1, + vlc_tab_diff_huffcodes, 2, 2, NULL, 0, 0); + + _vlcTabRun.table = &qdm2_table[qdm2_vlc_offs[2]]; + _vlcTabRun.table_allocated = qdm2_vlc_offs[3] - qdm2_vlc_offs[2]; + _vlcTabRun.table_size = 0; + initVlcSparse(&_vlcTabRun, 5, 6, + vlc_tab_run_huffbits, 1, 1, + vlc_tab_run_huffcodes, 1, 1, NULL, 0, 0); + + _fftLevelExpAltVlc.table = &qdm2_table[qdm2_vlc_offs[3]]; + _fftLevelExpAltVlc.table_allocated = qdm2_vlc_offs[4] - qdm2_vlc_offs[3]; + _fftLevelExpAltVlc.table_size = 0; + initVlcSparse(&_fftLevelExpAltVlc, 8, 28, + fft_level_exp_alt_huffbits, 1, 1, + fft_level_exp_alt_huffcodes, 2, 2, NULL, 0, 0); + + _fftLevelExpVlc.table = &qdm2_table[qdm2_vlc_offs[4]]; + _fftLevelExpVlc.table_allocated = qdm2_vlc_offs[5] - qdm2_vlc_offs[4]; + _fftLevelExpVlc.table_size = 0; + initVlcSparse(&_fftLevelExpVlc, 8, 20, + fft_level_exp_huffbits, 1, 1, + fft_level_exp_huffcodes, 2, 2, NULL, 0, 0); + + _fftStereoExpVlc.table = &qdm2_table[qdm2_vlc_offs[5]]; + _fftStereoExpVlc.table_allocated = qdm2_vlc_offs[6] - qdm2_vlc_offs[5]; + _fftStereoExpVlc.table_size = 0; + initVlcSparse(&_fftStereoExpVlc, 6, 7, + fft_stereo_exp_huffbits, 1, 1, + fft_stereo_exp_huffcodes, 1, 1, NULL, 0, 0); + + _fftStereoPhaseVlc.table = &qdm2_table[qdm2_vlc_offs[6]]; + _fftStereoPhaseVlc.table_allocated = qdm2_vlc_offs[7] - qdm2_vlc_offs[6]; + _fftStereoPhaseVlc.table_size = 0; + initVlcSparse(&_fftStereoPhaseVlc, 6, 9, + fft_stereo_phase_huffbits, 1, 1, + fft_stereo_phase_huffcodes, 1, 1, NULL, 0, 0); + + _vlcTabToneLevelIdxHi1.table = &qdm2_table[qdm2_vlc_offs[7]]; + _vlcTabToneLevelIdxHi1.table_allocated = qdm2_vlc_offs[8] - qdm2_vlc_offs[7]; + _vlcTabToneLevelIdxHi1.table_size = 0; + initVlcSparse(&_vlcTabToneLevelIdxHi1, 8, 20, + vlc_tab_tone_level_idx_hi1_huffbits, 1, 1, + vlc_tab_tone_level_idx_hi1_huffcodes, 2, 2, NULL, 0, 0); + + _vlcTabToneLevelIdxMid.table = &qdm2_table[qdm2_vlc_offs[8]]; + _vlcTabToneLevelIdxMid.table_allocated = qdm2_vlc_offs[9] - qdm2_vlc_offs[8]; + _vlcTabToneLevelIdxMid.table_size = 0; + initVlcSparse(&_vlcTabToneLevelIdxMid, 8, 24, + vlc_tab_tone_level_idx_mid_huffbits, 1, 1, + vlc_tab_tone_level_idx_mid_huffcodes, 2, 2, NULL, 0, 0); + + _vlcTabToneLevelIdxHi2.table = &qdm2_table[qdm2_vlc_offs[9]]; + _vlcTabToneLevelIdxHi2.table_allocated = qdm2_vlc_offs[10] - qdm2_vlc_offs[9]; + _vlcTabToneLevelIdxHi2.table_size = 0; + initVlcSparse(&_vlcTabToneLevelIdxHi2, 8, 24, + vlc_tab_tone_level_idx_hi2_huffbits, 1, 1, + vlc_tab_tone_level_idx_hi2_huffcodes, 2, 2, NULL, 0, 0); + + _vlcTabType30.table = &qdm2_table[qdm2_vlc_offs[10]]; + _vlcTabType30.table_allocated = qdm2_vlc_offs[11] - qdm2_vlc_offs[10]; + _vlcTabType30.table_size = 0; + initVlcSparse(&_vlcTabType30, 6, 9, + vlc_tab_type30_huffbits, 1, 1, + vlc_tab_type30_huffcodes, 1, 1, NULL, 0, 0); + + _vlcTabType34.table = &qdm2_table[qdm2_vlc_offs[11]]; + _vlcTabType34.table_allocated = qdm2_vlc_offs[12] - qdm2_vlc_offs[11]; + _vlcTabType34.table_size = 0; + initVlcSparse(&_vlcTabType34, 5, 10, + vlc_tab_type34_huffbits, 1, 1, + vlc_tab_type34_huffcodes, 1, 1, NULL, 0, 0); + + _vlcTabFftToneOffset[0].table = &qdm2_table[qdm2_vlc_offs[12]]; + _vlcTabFftToneOffset[0].table_allocated = qdm2_vlc_offs[13] - qdm2_vlc_offs[12]; + _vlcTabFftToneOffset[0].table_size = 0; + initVlcSparse(&_vlcTabFftToneOffset[0], 8, 23, + vlc_tab_fft_tone_offset_0_huffbits, 1, 1, + vlc_tab_fft_tone_offset_0_huffcodes, 2, 2, NULL, 0, 0); + + _vlcTabFftToneOffset[1].table = &qdm2_table[qdm2_vlc_offs[13]]; + _vlcTabFftToneOffset[1].table_allocated = qdm2_vlc_offs[14] - qdm2_vlc_offs[13]; + _vlcTabFftToneOffset[1].table_size = 0; + initVlcSparse(&_vlcTabFftToneOffset[1], 8, 28, + vlc_tab_fft_tone_offset_1_huffbits, 1, 1, + vlc_tab_fft_tone_offset_1_huffcodes, 2, 2, NULL, 0, 0); + + _vlcTabFftToneOffset[2].table = &qdm2_table[qdm2_vlc_offs[14]]; + _vlcTabFftToneOffset[2].table_allocated = qdm2_vlc_offs[15] - qdm2_vlc_offs[14]; + _vlcTabFftToneOffset[2].table_size = 0; + initVlcSparse(&_vlcTabFftToneOffset[2], 8, 32, + vlc_tab_fft_tone_offset_2_huffbits, 1, 1, + vlc_tab_fft_tone_offset_2_huffcodes, 2, 2, NULL, 0, 0); + + _vlcTabFftToneOffset[3].table = &qdm2_table[qdm2_vlc_offs[15]]; + _vlcTabFftToneOffset[3].table_allocated = qdm2_vlc_offs[16] - qdm2_vlc_offs[15]; + _vlcTabFftToneOffset[3].table_size = 0; + initVlcSparse(&_vlcTabFftToneOffset[3], 8, 35, + vlc_tab_fft_tone_offset_3_huffbits, 1, 1, + vlc_tab_fft_tone_offset_3_huffcodes, 2, 2, NULL, 0, 0); + + _vlcTabFftToneOffset[4].table = &qdm2_table[qdm2_vlc_offs[16]]; + _vlcTabFftToneOffset[4].table_allocated = qdm2_vlc_offs[17] - qdm2_vlc_offs[16]; + _vlcTabFftToneOffset[4].table_size = 0; + initVlcSparse(&_vlcTabFftToneOffset[4], 8, 38, + vlc_tab_fft_tone_offset_4_huffbits, 1, 1, + vlc_tab_fft_tone_offset_4_huffcodes, 2, 2, NULL, 0, 0); + + _vlcsInitialized = true; + } +} + +QDM2Stream::QDM2Stream(Common::SeekableReadStream *stream, Common::SeekableReadStream *extraData) { + uint32 tmp; + int32 tmp_s; + int tmp_val; + int i; + + debug(1, "QDM2Stream::QDM2Stream() Call"); + + _stream = stream; + _compressedData = NULL; + _subPacket = 0; + _superBlockStart = 0; + memset(_quantizedCoeffs, 0, sizeof(_quantizedCoeffs)); + memset(_fftLevelExp, 0, sizeof(_fftLevelExp)); + _noiseIdx = 0; + memset(_fftCoefsMinIndex, 0, sizeof(_fftCoefsMinIndex)); + memset(_fftCoefsMaxIndex, 0, sizeof(_fftCoefsMaxIndex)); + _fftToneStart = 0; + _fftToneEnd = 0; + for(i = 0; i < ARRAYSIZE(_subPacketListA); i++) { + _subPacketListA[i].packet = NULL; + _subPacketListA[i].next = NULL; + } + _subPacketsB = 0; + for(i = 0; i < ARRAYSIZE(_subPacketListB); i++) { + _subPacketListB[i].packet = NULL; + _subPacketListB[i].next = NULL; + } + for(i = 0; i < ARRAYSIZE(_subPacketListC); i++) { + _subPacketListC[i].packet = NULL; + _subPacketListC[i].next = NULL; + } + for(i = 0; i < ARRAYSIZE(_subPacketListD); i++) { + _subPacketListD[i].packet = NULL; + _subPacketListD[i].next = NULL; + } + memset(_synthBuf, 0, sizeof(_synthBuf)); + memset(_synthBufOffset, 0, sizeof(_synthBufOffset)); + memset(_sbSamples, 0, sizeof(_sbSamples)); + memset(_outputBuffer, 0, sizeof(_outputBuffer)); + _vlcsInitialized = false; + _superblocktype_2_3 = 0; + _hasErrors = false; + + // Rewind extraData stream from any previous calls... + extraData->seek(0, SEEK_SET); + + tmp_s = extraData->readSint32BE(); + debug(1, "QDM2Stream::QDM2Stream() extraSize: %d", tmp_s); + if ((extraData->size() - extraData->pos()) / 4 + 1 != tmp_s) + warning("QDM2Stream::QDM2Stream() extraSize mismatch - Expected %d", (extraData->size() - extraData->pos()) / 4 + 1); + if (tmp_s < 12) + error("QDM2Stream::QDM2Stream() Insufficient extraData"); + + tmp = extraData->readUint32BE(); + debug(1, "QDM2Stream::QDM2Stream() extraTag: %d", tmp); + if (tmp != MKTAG('f','r','m','a')) + warning("QDM2Stream::QDM2Stream() extraTag mismatch"); + + tmp = extraData->readUint32BE(); + debug(1, "QDM2Stream::QDM2Stream() extraType: %d", tmp); + if (tmp == MKTAG('Q','D','M','C')) + warning("QDM2Stream::QDM2Stream() QDMC stream type not supported"); + else if (tmp != MKTAG('Q','D','M','2')) + error("QDM2Stream::QDM2Stream() Unsupported stream type"); + + tmp_s = extraData->readSint32BE(); + debug(1, "QDM2Stream::QDM2Stream() extraSize2: %d", tmp_s); + if ((extraData->size() - extraData->pos()) + 4 != tmp_s) + warning("QDM2Stream::QDM2Stream() extraSize2 mismatch - Expected %d", (extraData->size() - extraData->pos()) + 4); + + tmp = extraData->readUint32BE(); + debug(1, "QDM2Stream::QDM2Stream() extraTag2: %d", tmp); + if (tmp != MKTAG('Q','D','C','A')) + warning("QDM2Stream::QDM2Stream() extraTag2 mismatch"); + + if (extraData->readUint32BE() != 1) + warning("QDM2Stream::QDM2Stream() u0 field not 1"); + + _channels = extraData->readUint32BE(); + debug(1, "QDM2Stream::QDM2Stream() channels: %d", _channels); + + _sampleRate = extraData->readUint32BE(); + debug(1, "QDM2Stream::QDM2Stream() sampleRate: %d", _sampleRate); + + _bitRate = extraData->readUint32BE(); + debug(1, "QDM2Stream::QDM2Stream() bitRate: %d", _bitRate); + + _blockSize = extraData->readUint32BE(); + debug(1, "QDM2Stream::QDM2Stream() blockSize: %d", _blockSize); + + _frameSize = extraData->readUint32BE(); + debug(1, "QDM2Stream::QDM2Stream() frameSize: %d", _frameSize); + + _packetSize = extraData->readUint32BE(); + debug(1, "QDM2Stream::QDM2Stream() packetSize: %d", _packetSize); + + if (extraData->size() - extraData->pos() != 0) { + tmp_s = extraData->readSint32BE(); + debug(1, "QDM2Stream::QDM2Stream() extraSize3: %d", tmp_s); + if (extraData->size() + 4 != tmp_s) + warning("QDM2Stream::QDM2Stream() extraSize3 mismatch - Expected %d", extraData->size() + 4); + + tmp = extraData->readUint32BE(); + debug(1, "QDM2Stream::QDM2Stream() extraTag3: %d", tmp); + if (tmp != MKTAG('Q','D','C','P')) + warning("QDM2Stream::QDM2Stream() extraTag3 mismatch"); + + if ((float)extraData->readUint32BE() != 1.0) + warning("QDM2Stream::QDM2Stream() uf0 field not 1.0"); + + if (extraData->readUint32BE() != 0) + warning("QDM2Stream::QDM2Stream() u1 field not 0"); + + if ((float)extraData->readUint32BE() != 1.0) + warning("QDM2Stream::QDM2Stream() uf1 field not 1.0"); + + if ((float)extraData->readUint32BE() != 1.0) + warning("QDM2Stream::QDM2Stream() uf2 field not 1.0"); + + if (extraData->readUint32BE() != 27) + warning("QDM2Stream::QDM2Stream() u2 field not 27"); + + if (extraData->readUint32BE() != 8) + warning("QDM2Stream::QDM2Stream() u3 field not 8"); + + if (extraData->readUint32BE() != 0) + warning("QDM2Stream::QDM2Stream() u4 field not 0"); + } + + _fftOrder = scummvm_log2(_frameSize) + 1; + _fftFrameSize = 2 * _frameSize; // complex has two floats + + // something like max decodable tones + _groupOrder = scummvm_log2(_blockSize) + 1; + _sFrameSize = _blockSize / 16; // 16 iterations per super block + + _subSampling = _fftOrder - 7; + _frequencyRange = 255 / (1 << (2 - _subSampling)); + + switch ((_subSampling * 2 + _channels - 1)) { + case 0: + tmp = 40; + break; + case 1: + tmp = 48; + break; + case 2: + tmp = 56; + break; + case 3: + tmp = 72; + break; + case 4: + tmp = 80; + break; + case 5: + tmp = 100; + break; + default: + tmp = _subSampling; + break; + } + + tmp_val = 0; + if ((tmp * 1000) < _bitRate) tmp_val = 1; + if ((tmp * 1440) < _bitRate) tmp_val = 2; + if ((tmp * 1760) < _bitRate) tmp_val = 3; + if ((tmp * 2240) < _bitRate) tmp_val = 4; + _cmTableSelect = tmp_val; + + if (_subSampling == 0) + tmp = 7999; + else + tmp = ((-(_subSampling -1)) & 8000) + 20000; + + if (tmp < 8000) + _coeffPerSbSelect = 0; + else if (tmp <= 16000) + _coeffPerSbSelect = 1; + else + _coeffPerSbSelect = 2; + + if (_fftOrder < 7 || _fftOrder > 9) + error("QDM2Stream::QDM2Stream() Unsupported fft_order: %d", _fftOrder); + + rdftInit(&_rdftCtx, _fftOrder, IRDFT); + + initVlc(); + ff_mpa_synth_init(ff_mpa_synth_window); + softclipTableInit(); + rndTableInit(); + initNoiseSamples(); + + _compressedData = new uint8[_packetSize]; +} + +QDM2Stream::~QDM2Stream() { + delete[] _compressedData; + delete _stream; +} + +static int qdm2_get_vlc(GetBitContext *gb, VLC *vlc, int flag, int depth) { + int value = getVlc2(gb, vlc->table, vlc->bits, depth); + + // stage-2, 3 bits exponent escape sequence + if (value-- == 0) + value = getBits(gb, getBits (gb, 3) + 1); + + // stage-3, optional + if (flag) { + int tmp = vlc_stage3_values[value]; + + if ((value & ~3) > 0) + tmp += getBits(gb, (value >> 2)); + value = tmp; + } + + return value; +} + +static int qdm2_get_se_vlc(VLC *vlc, GetBitContext *gb, int depth) +{ + int value = qdm2_get_vlc(gb, vlc, 0, depth); + + return (value & 1) ? ((value + 1) >> 1) : -(value >> 1); +} + +/** + * QDM2 checksum + * + * @param data pointer to data to be checksum'ed + * @param length data length + * @param value checksum value + * + * @return 0 if checksum is OK + */ +static uint16 qdm2_packet_checksum(const uint8 *data, int length, int value) { + int i; + + for (i = 0; i < length; i++) + value -= data[i]; + + return (uint16)(value & 0xffff); +} + +/** + * Fills a QDM2SubPacket structure with packet type, size, and data pointer. + * + * @param gb bitreader context + * @param sub_packet packet under analysis + */ +static void qdm2_decode_sub_packet_header(GetBitContext *gb, QDM2SubPacket *sub_packet) +{ + sub_packet->type = getBits (gb, 8); + + if (sub_packet->type == 0) { + sub_packet->size = 0; + sub_packet->data = NULL; + } else { + sub_packet->size = getBits (gb, 8); + + if (sub_packet->type & 0x80) { + sub_packet->size <<= 8; + sub_packet->size |= getBits (gb, 8); + sub_packet->type &= 0x7f; + } + + if (sub_packet->type == 0x7f) + sub_packet->type |= (getBits (gb, 8) << 8); + + sub_packet->data = &gb->buffer[getBitsCount(gb) / 8]; // FIXME: this depends on bitreader internal data + } +} + +/** + * Return node pointer to first packet of requested type in list. + * + * @param list list of subpackets to be scanned + * @param type type of searched subpacket + * @return node pointer for subpacket if found, else NULL + */ +static QDM2SubPNode* qdm2_search_subpacket_type_in_list(QDM2SubPNode *list, int type) +{ + while (list != NULL && list->packet != NULL) { + if (list->packet->type == type) + return list; + list = list->next; + } + return NULL; +} + +/** + * Replaces 8 elements with their average value. + * Called by qdm2_decode_superblock before starting subblock decoding. + */ +void QDM2Stream::average_quantized_coeffs(void) { + int i, j, n, ch, sum; + + n = coeff_per_sb_for_avg[_coeffPerSbSelect][QDM2_SB_USED(_subSampling) - 1] + 1; + + for (ch = 0; ch < _channels; ch++) { + for (i = 0; i < n; i++) { + sum = 0; + + for (j = 0; j < 8; j++) + sum += _quantizedCoeffs[ch][i][j]; + + sum /= 8; + if (sum > 0) + sum--; + + for (j = 0; j < 8; j++) + _quantizedCoeffs[ch][i][j] = sum; + } + } +} + +/** + * Build subband samples with noise weighted by q->tone_level. + * Called by synthfilt_build_sb_samples. + * + * @param sb subband index + */ +void QDM2Stream::build_sb_samples_from_noise(int sb) { + int ch, j; + + FIX_NOISE_IDX(_noiseIdx); + + if (!_channels) + return; + + for (ch = 0; ch < _channels; ch++) { + for (j = 0; j < 64; j++) { + _sbSamples[ch][j * 2][sb] = (int32)(SB_DITHERING_NOISE(sb, _noiseIdx) * _toneLevel[ch][sb][j] + .5); + _sbSamples[ch][j * 2 + 1][sb] = (int32)(SB_DITHERING_NOISE(sb, _noiseIdx) * _toneLevel[ch][sb][j] + .5); + } + } +} + +/** + * Called while processing data from subpackets 11 and 12. + * Used after making changes to coding_method array. + * + * @param sb subband index + * @param channels number of channels + * @param coding_method q->coding_method[0][0][0] + */ +void QDM2Stream::fix_coding_method_array(int sb, int channels, sb_int8_array coding_method) +{ + int j, k; + int ch; + int run, case_val; + int switchtable[23] = {0,5,1,5,5,5,5,5,2,5,5,5,5,5,5,5,3,5,5,5,5,5,4}; + + for (ch = 0; ch < channels; ch++) { + for (j = 0; j < 64; ) { + if((coding_method[ch][sb][j] - 8) > 22) { + run = 1; + case_val = 8; + } else { + switch (switchtable[coding_method[ch][sb][j]-8]) { + case 0: run = 10; case_val = 10; break; + case 1: run = 1; case_val = 16; break; + case 2: run = 5; case_val = 24; break; + case 3: run = 3; case_val = 30; break; + case 4: run = 1; case_val = 30; break; + case 5: run = 1; case_val = 8; break; + default: run = 1; case_val = 8; break; + } + } + for (k = 0; k < run; k++) + if (j + k < 128) + if (coding_method[ch][sb + (j + k) / 64][(j + k) % 64] > coding_method[ch][sb][j]) + if (k > 0) { + warning("QDM2 Untested Code: not debugged, almost never used"); + memset(&coding_method[ch][sb][j + k], case_val, k * sizeof(int8)); + memset(&coding_method[ch][sb][j + k], case_val, 3 * sizeof(int8)); + } + j += run; + } + } +} + +/** + * Related to synthesis filter + * Called by process_subpacket_10 + * + * @param flag 1 if called after getting data from subpacket 10, 0 if no subpacket 10 + */ +void QDM2Stream::fill_tone_level_array(int flag) { + int i, sb, ch, sb_used; + int tmp, tab; + + // This should never happen + if (_channels <= 0) + return; + + for (ch = 0; ch < _channels; ch++) { + for (sb = 0; sb < 30; sb++) { + for (i = 0; i < 8; i++) { + if ((tab=coeff_per_sb_for_dequant[_coeffPerSbSelect][sb]) < (last_coeff[_coeffPerSbSelect] - 1)) + tmp = _quantizedCoeffs[ch][tab + 1][i] * dequant_table[_coeffPerSbSelect][tab + 1][sb]+ + _quantizedCoeffs[ch][tab][i] * dequant_table[_coeffPerSbSelect][tab][sb]; + else + tmp = _quantizedCoeffs[ch][tab][i] * dequant_table[_coeffPerSbSelect][tab][sb]; + if(tmp < 0) + tmp += 0xff; + _toneLevelIdxBase[ch][sb][i] = (tmp / 256) & 0xff; + } + } + } + + sb_used = QDM2_SB_USED(_subSampling); + + if ((_superblocktype_2_3 != 0) && !flag) { + for (sb = 0; sb < sb_used; sb++) { + for (ch = 0; ch < _channels; ch++) { + for (i = 0; i < 64; i++) { + _toneLevelIdx[ch][sb][i] = _toneLevelIdxBase[ch][sb][i / 8]; + if (_toneLevelIdx[ch][sb][i] < 0) + _toneLevel[ch][sb][i] = 0; + else + _toneLevel[ch][sb][i] = fft_tone_level_table[0][_toneLevelIdx[ch][sb][i] & 0x3f]; + } + } + } + } else { + tab = _superblocktype_2_3 ? 0 : 1; + for (sb = 0; sb < sb_used; sb++) { + if ((sb >= 4) && (sb <= 23)) { + for (ch = 0; ch < _channels; ch++) { + for (i = 0; i < 64; i++) { + tmp = _toneLevelIdxBase[ch][sb][i / 8] - + _toneLevelIdxHi1[ch][sb / 8][i / 8][i % 8] - + _toneLevelIdxMid[ch][sb - 4][i / 8] - + _toneLevelIdxHi2[ch][sb - 4]; + _toneLevelIdx[ch][sb][i] = tmp & 0xff; + if ((tmp < 0) || (!_superblocktype_2_3 && !tmp)) + _toneLevel[ch][sb][i] = 0; + else + _toneLevel[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f]; + } + } + } else { + if (sb > 4) { + for (ch = 0; ch < _channels; ch++) { + for (i = 0; i < 64; i++) { + tmp = _toneLevelIdxBase[ch][sb][i / 8] - + _toneLevelIdxHi1[ch][2][i / 8][i % 8] - + _toneLevelIdxHi2[ch][sb - 4]; + _toneLevelIdx[ch][sb][i] = tmp & 0xff; + if ((tmp < 0) || (!_superblocktype_2_3 && !tmp)) + _toneLevel[ch][sb][i] = 0; + else + _toneLevel[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f]; + } + } + } else { + for (ch = 0; ch < _channels; ch++) { + for (i = 0; i < 64; i++) { + tmp = _toneLevelIdx[ch][sb][i] = _toneLevelIdxBase[ch][sb][i / 8]; + if ((tmp < 0) || (!_superblocktype_2_3 && !tmp)) + _toneLevel[ch][sb][i] = 0; + else + _toneLevel[ch][sb][i] = fft_tone_level_table[tab][tmp & 0x3f]; + } + } + } + } + } + } +} + +/** + * Related to synthesis filter + * Called by process_subpacket_11 + * c is built with data from subpacket 11 + * Most of this function is used only if superblock_type_2_3 == 0, never seen it in samples + * + * @param tone_level_idx + * @param tone_level_idx_temp + * @param coding_method q->coding_method[0][0][0] + * @param nb_channels number of channels + * @param c coming from subpacket 11, passed as 8*c + * @param superblocktype_2_3 flag based on superblock packet type + * @param cm_table_select q->cm_table_select + */ +void QDM2Stream::fill_coding_method_array(sb_int8_array tone_level_idx, sb_int8_array tone_level_idx_temp, + sb_int8_array coding_method, int nb_channels, + int c, int superblocktype_2_3, int cm_table_select) { + int ch, sb, j; + int tmp, acc, esp_40, comp; + int add1, add2, add3, add4; + // TODO : Remove multres 64 bit variable necessity... + int64_t multres; + + // This should never happen + if (nb_channels <= 0) + return; + if (!superblocktype_2_3) { + warning("QDM2 This case is untested, no samples available"); + for (ch = 0; ch < nb_channels; ch++) + for (sb = 0; sb < 30; sb++) { + for (j = 1; j < 63; j++) { // The loop only iterates to 63 so the code doesn't overflow the buffer + add1 = tone_level_idx[ch][sb][j] - 10; + if (add1 < 0) + add1 = 0; + add2 = add3 = add4 = 0; + if (sb > 1) { + add2 = tone_level_idx[ch][sb - 2][j] + tone_level_idx_offset_table[sb][0] - 6; + if (add2 < 0) + add2 = 0; + } + if (sb > 0) { + add3 = tone_level_idx[ch][sb - 1][j] + tone_level_idx_offset_table[sb][1] - 6; + if (add3 < 0) + add3 = 0; + } + if (sb < 29) { + add4 = tone_level_idx[ch][sb + 1][j] + tone_level_idx_offset_table[sb][3] - 6; + if (add4 < 0) + add4 = 0; + } + tmp = tone_level_idx[ch][sb][j + 1] * 2 - add4 - add3 - add2 - add1; + if (tmp < 0) + tmp = 0; + tone_level_idx_temp[ch][sb][j + 1] = tmp & 0xff; + } + tone_level_idx_temp[ch][sb][0] = tone_level_idx_temp[ch][sb][1]; + } + acc = 0; + for (ch = 0; ch < nb_channels; ch++) + for (sb = 0; sb < 30; sb++) + for (j = 0; j < 64; j++) + acc += tone_level_idx_temp[ch][sb][j]; + + multres = 0x66666667 * (acc * 10); + esp_40 = (multres >> 32) / 8 + ((multres & 0xffffffff) >> 31); + for (ch = 0; ch < nb_channels; ch++) + for (sb = 0; sb < 30; sb++) + for (j = 0; j < 64; j++) { + comp = tone_level_idx_temp[ch][sb][j]* esp_40 * 10; + if (comp < 0) + comp += 0xff; + comp /= 256; // signed shift + switch(sb) { + case 0: + if (comp < 30) + comp = 30; + comp += 15; + break; + case 1: + if (comp < 24) + comp = 24; + comp += 10; + break; + case 2: + case 3: + case 4: + if (comp < 16) + comp = 16; + } + if (comp <= 5) + tmp = 0; + else if (comp <= 10) + tmp = 10; + else if (comp <= 16) + tmp = 16; + else if (comp <= 24) + tmp = -1; + else + tmp = 0; + coding_method[ch][sb][j] = ((tmp & 0xfffa) + 30 )& 0xff; + } + for (sb = 0; sb < 30; sb++) + fix_coding_method_array(sb, nb_channels, coding_method); + for (ch = 0; ch < nb_channels; ch++) + for (sb = 0; sb < 30; sb++) + for (j = 0; j < 64; j++) + if (sb >= 10) { + if (coding_method[ch][sb][j] < 10) + coding_method[ch][sb][j] = 10; + } else { + if (sb >= 2) { + if (coding_method[ch][sb][j] < 16) + coding_method[ch][sb][j] = 16; + } else { + if (coding_method[ch][sb][j] < 30) + coding_method[ch][sb][j] = 30; + } + } + } else { // superblocktype_2_3 != 0 + for (ch = 0; ch < nb_channels; ch++) + for (sb = 0; sb < 30; sb++) + for (j = 0; j < 64; j++) + coding_method[ch][sb][j] = coding_method_table[cm_table_select][sb]; + } +} + +/** + * + * Called by process_subpacket_11 to process more data from subpacket 11 with sb 0-8 + * Called by process_subpacket_12 to process data from subpacket 12 with sb 8-sb_used + * + * @param gb bitreader context + * @param length packet length in bits + * @param sb_min lower subband processed (sb_min included) + * @param sb_max higher subband processed (sb_max excluded) + */ +void QDM2Stream::synthfilt_build_sb_samples(GetBitContext *gb, int length, int sb_min, int sb_max) { + int sb, j, k, n, ch, run, channels; + int joined_stereo, zero_encoding, chs; + int type34_first; + float type34_div = 0; + float type34_predictor; + float samples[10], sign_bits[16]; + + if (length == 0) { + // If no data use noise + for (sb = sb_min; sb < sb_max; sb++) + build_sb_samples_from_noise(sb); + + return; + } + + for (sb = sb_min; sb < sb_max; sb++) { + FIX_NOISE_IDX(_noiseIdx); + + channels = _channels; + + if (_channels <= 1 || sb < 12) + joined_stereo = 0; + else if (sb >= 24) + joined_stereo = 1; + else + joined_stereo = (BITS_LEFT(length,gb) >= 1) ? getBits1 (gb) : 0; + + if (joined_stereo) { + if (BITS_LEFT(length,gb) >= 16) + for (j = 0; j < 16; j++) + sign_bits[j] = getBits1(gb); + + for (j = 0; j < 64; j++) + if (_codingMethod[1][sb][j] > _codingMethod[0][sb][j]) + _codingMethod[0][sb][j] = _codingMethod[1][sb][j]; + + fix_coding_method_array(sb, _channels, _codingMethod); + channels = 1; + } + + for (ch = 0; ch < channels; ch++) { + zero_encoding = (BITS_LEFT(length,gb) >= 1) ? getBits1(gb) : 0; + type34_predictor = 0.0; + type34_first = 1; + + for (j = 0; j < 128; ) { + switch (_codingMethod[ch][sb][j / 2]) { + case 8: + if (BITS_LEFT(length,gb) >= 10) { + if (zero_encoding) { + for (k = 0; k < 5; k++) { + if ((j + 2 * k) >= 128) + break; + samples[2 * k] = getBits1(gb) ? dequant_1bit[joined_stereo][2 * getBits1(gb)] : 0; + } + } else { + n = getBits(gb, 8); + for (k = 0; k < 5; k++) + samples[2 * k] = dequant_1bit[joined_stereo][_randomDequantIndex[n][k]]; + } + for (k = 0; k < 5; k++) + samples[2 * k + 1] = SB_DITHERING_NOISE(sb, _noiseIdx); + } else { + for (k = 0; k < 10; k++) + samples[k] = SB_DITHERING_NOISE(sb, _noiseIdx); + } + run = 10; + break; + + case 10: + if (BITS_LEFT(length,gb) >= 1) { + double f = 0.81; + + if (getBits1(gb)) + f = -f; + f -= _noiseSamples[((sb + 1) * (j +5 * ch + 1)) & 127] * 9.0 / 40.0; + samples[0] = f; + } else { + samples[0] = SB_DITHERING_NOISE(sb, _noiseIdx); + } + run = 1; + break; + + case 16: + if (BITS_LEFT(length,gb) >= 10) { + if (zero_encoding) { + for (k = 0; k < 5; k++) { + if ((j + k) >= 128) + break; + samples[k] = (getBits1(gb) == 0) ? 0 : dequant_1bit[joined_stereo][2 * getBits1(gb)]; + } + } else { + n = getBits (gb, 8); + for (k = 0; k < 5; k++) + samples[k] = dequant_1bit[joined_stereo][_randomDequantIndex[n][k]]; + } + } else { + for (k = 0; k < 5; k++) + samples[k] = SB_DITHERING_NOISE(sb, _noiseIdx); + } + run = 5; + break; + + case 24: + if (BITS_LEFT(length,gb) >= 7) { + n = getBits(gb, 7); + for (k = 0; k < 3; k++) + samples[k] = (_randomDequantType24[n][k] - 2.0) * 0.5; + } else { + for (k = 0; k < 3; k++) + samples[k] = SB_DITHERING_NOISE(sb, _noiseIdx); + } + run = 3; + break; + + case 30: + if (BITS_LEFT(length,gb) >= 4) + samples[0] = type30_dequant[qdm2_get_vlc(gb, &_vlcTabType30, 0, 1)]; + else + samples[0] = SB_DITHERING_NOISE(sb, _noiseIdx); + + run = 1; + break; + + case 34: + if (BITS_LEFT(length,gb) >= 7) { + if (type34_first) { + type34_div = (float)(1 << getBits(gb, 2)); + samples[0] = ((float)getBits(gb, 5) - 16.0) / 15.0; + type34_predictor = samples[0]; + type34_first = 0; + } else { + samples[0] = type34_delta[qdm2_get_vlc(gb, &_vlcTabType34, 0, 1)] / type34_div + type34_predictor; + type34_predictor = samples[0]; + } + } else { + samples[0] = SB_DITHERING_NOISE(sb, _noiseIdx); + } + run = 1; + break; + + default: + samples[0] = SB_DITHERING_NOISE(sb, _noiseIdx); + run = 1; + break; + } + + if (joined_stereo) { + float tmp[10][MPA_MAX_CHANNELS]; + + for (k = 0; k < run; k++) { + tmp[k][0] = samples[k]; + tmp[k][1] = (sign_bits[(j + k) / 8]) ? -samples[k] : samples[k]; + } + for (chs = 0; chs < _channels; chs++) + for (k = 0; k < run; k++) + if ((j + k) < 128) + _sbSamples[chs][j + k][sb] = (int32)(_toneLevel[chs][sb][((j + k)/2)] * tmp[k][chs] + .5); + } else { + for (k = 0; k < run; k++) + if ((j + k) < 128) + _sbSamples[ch][j + k][sb] = (int32)(_toneLevel[ch][sb][(j + k)/2] * samples[k] + .5); + } + + j += run; + } // j loop + } // channel loop + } // subband loop +} + +/** + * Init the first element of a channel in quantized_coeffs with data from packet 10 (quantized_coeffs[ch][0]). + * This is similar to process_subpacket_9, but for a single channel and for element [0] + * same VLC tables as process_subpacket_9 are used. + * + * @param quantized_coeffs pointer to quantized_coeffs[ch][0] + * @param gb bitreader context + * @param length packet length in bits + */ +void QDM2Stream::init_quantized_coeffs_elem0(int8 *quantized_coeffs, GetBitContext *gb, int length) { + int i, k, run, level, diff; + + if (BITS_LEFT(length,gb) < 16) + return; + level = qdm2_get_vlc(gb, &_vlcTabLevel, 0, 2); + + quantized_coeffs[0] = level; + + for (i = 0; i < 7; ) { + if (BITS_LEFT(length,gb) < 16) + break; + run = qdm2_get_vlc(gb, &_vlcTabRun, 0, 1) + 1; + + if (BITS_LEFT(length,gb) < 16) + break; + diff = qdm2_get_se_vlc(&_vlcTabDiff, gb, 2); + + for (k = 1; k <= run; k++) + quantized_coeffs[i + k] = (level + ((k * diff) / run)); + + level += diff; + i += run; + } +} + +/** + * Related to synthesis filter, process data from packet 10 + * Init part of quantized_coeffs via function init_quantized_coeffs_elem0 + * Init tone_level_idx_hi1, tone_level_idx_hi2, tone_level_idx_mid with data from packet 10 + * + * @param gb bitreader context + * @param length packet length in bits + */ +void QDM2Stream::init_tone_level_dequantization(GetBitContext *gb, int length) { + int sb, j, k, n, ch; + + for (ch = 0; ch < _channels; ch++) { + init_quantized_coeffs_elem0(_quantizedCoeffs[ch][0], gb, length); + + if (BITS_LEFT(length,gb) < 16) { + memset(_quantizedCoeffs[ch][0], 0, 8); + break; + } + } + + n = _subSampling + 1; + + for (sb = 0; sb < n; sb++) + for (ch = 0; ch < _channels; ch++) + for (j = 0; j < 8; j++) { + if (BITS_LEFT(length,gb) < 1) + break; + if (getBits1(gb)) { + for (k=0; k < 8; k++) { + if (BITS_LEFT(length,gb) < 16) + break; + _toneLevelIdxHi1[ch][sb][j][k] = qdm2_get_vlc(gb, &_vlcTabToneLevelIdxHi1, 0, 2); + } + } else { + for (k=0; k < 8; k++) + _toneLevelIdxHi1[ch][sb][j][k] = 0; + } + } + + n = QDM2_SB_USED(_subSampling) - 4; + + for (sb = 0; sb < n; sb++) + for (ch = 0; ch < _channels; ch++) { + if (BITS_LEFT(length,gb) < 16) + break; + _toneLevelIdxHi2[ch][sb] = qdm2_get_vlc(gb, &_vlcTabToneLevelIdxHi2, 0, 2); + if (sb > 19) + _toneLevelIdxHi2[ch][sb] -= 16; + else + for (j = 0; j < 8; j++) + _toneLevelIdxMid[ch][sb][j] = -16; + } + + n = QDM2_SB_USED(_subSampling) - 5; + + for (sb = 0; sb < n; sb++) { + for (ch = 0; ch < _channels; ch++) { + for (j = 0; j < 8; j++) { + if (BITS_LEFT(length,gb) < 16) + break; + _toneLevelIdxMid[ch][sb][j] = qdm2_get_vlc(gb, &_vlcTabToneLevelIdxMid, 0, 2) - 32; + } + } + } +} + +/** + * Process subpacket 9, init quantized_coeffs with data from it + * + * @param node pointer to node with packet + */ +void QDM2Stream::process_subpacket_9(QDM2SubPNode *node) { + GetBitContext gb; + int i, j, k, n, ch, run, level, diff; + + initGetBits(&gb, node->packet->data, node->packet->size*8); + + n = coeff_per_sb_for_avg[_coeffPerSbSelect][QDM2_SB_USED(_subSampling) - 1] + 1; // same as averagesomething function + + for (i = 1; i < n; i++) + for (ch = 0; ch < _channels; ch++) { + level = qdm2_get_vlc(&gb, &_vlcTabLevel, 0, 2); + _quantizedCoeffs[ch][i][0] = level; + + for (j = 0; j < (8 - 1); ) { + run = qdm2_get_vlc(&gb, &_vlcTabRun, 0, 1) + 1; + diff = qdm2_get_se_vlc(&_vlcTabDiff, &gb, 2); + + for (k = 1; k <= run; k++) + _quantizedCoeffs[ch][i][j + k] = (level + ((k*diff) / run)); + + level += diff; + j += run; + } + } + + for (ch = 0; ch < _channels; ch++) + for (i = 0; i < 8; i++) + _quantizedCoeffs[ch][0][i] = 0; +} + +/** + * Process subpacket 10 if not null, else + * + * @param node pointer to node with packet + * @param length packet length in bits + */ +void QDM2Stream::process_subpacket_10(QDM2SubPNode *node, int length) { + GetBitContext gb; + + initGetBits(&gb, ((node == NULL) ? _emptyBuffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8)); + + if (length != 0) { + init_tone_level_dequantization(&gb, length); + fill_tone_level_array(1); + } else { + fill_tone_level_array(0); + } +} + +/** + * Process subpacket 11 + * + * @param node pointer to node with packet + * @param length packet length in bit + */ +void QDM2Stream::process_subpacket_11(QDM2SubPNode *node, int length) { + GetBitContext gb; + + initGetBits(&gb, ((node == NULL) ? _emptyBuffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8)); + if (length >= 32) { + int c = getBits (&gb, 13); + + if (c > 3) + fill_coding_method_array(_toneLevelIdx, _toneLevelIdxTemp, _codingMethod, + _channels, 8*c, _superblocktype_2_3, _cmTableSelect); + } + + synthfilt_build_sb_samples(&gb, length, 0, 8); +} + +/** + * Process subpacket 12 + * + * @param node pointer to node with packet + * @param length packet length in bits + */ +void QDM2Stream::process_subpacket_12(QDM2SubPNode *node, int length) { + GetBitContext gb; + + initGetBits(&gb, ((node == NULL) ? _emptyBuffer : node->packet->data), ((node == NULL) ? 0 : node->packet->size*8)); + synthfilt_build_sb_samples(&gb, length, 8, QDM2_SB_USED(_subSampling)); +} + +/* + * Process new subpackets for synthesis filter + * + * @param list list with synthesis filter packets (list D) + */ +void QDM2Stream::process_synthesis_subpackets(QDM2SubPNode *list) { + struct QDM2SubPNode *nodes[4]; + + nodes[0] = qdm2_search_subpacket_type_in_list(list, 9); + if (nodes[0] != NULL) + process_subpacket_9(nodes[0]); + + nodes[1] = qdm2_search_subpacket_type_in_list(list, 10); + if (nodes[1] != NULL) + process_subpacket_10(nodes[1], nodes[1]->packet->size << 3); + else + process_subpacket_10(NULL, 0); + + nodes[2] = qdm2_search_subpacket_type_in_list(list, 11); + if (nodes[0] != NULL && nodes[1] != NULL && nodes[2] != NULL) + process_subpacket_11(nodes[2], (nodes[2]->packet->size << 3)); + else + process_subpacket_11(NULL, 0); + + nodes[3] = qdm2_search_subpacket_type_in_list(list, 12); + if (nodes[0] != NULL && nodes[1] != NULL && nodes[3] != NULL) + process_subpacket_12(nodes[3], (nodes[3]->packet->size << 3)); + else + process_subpacket_12(NULL, 0); +} + +/* + * Decode superblock, fill packet lists. + * + */ +void QDM2Stream::qdm2_decode_super_block(void) { + GetBitContext gb; + struct QDM2SubPacket header, *packet; + int i, packet_bytes, sub_packet_size, subPacketsD; + unsigned int next_index = 0; + + memset(_toneLevelIdxHi1, 0, sizeof(_toneLevelIdxHi1)); + memset(_toneLevelIdxMid, 0, sizeof(_toneLevelIdxMid)); + memset(_toneLevelIdxHi2, 0, sizeof(_toneLevelIdxHi2)); + + _subPacketsB = 0; + subPacketsD = 0; + + average_quantized_coeffs(); // average elements in quantized_coeffs[max_ch][10][8] + + initGetBits(&gb, _compressedData, _packetSize*8); + qdm2_decode_sub_packet_header(&gb, &header); + + if (header.type < 2 || header.type >= 8) { + _hasErrors = true; + error("QDM2 : bad superblock type"); + return; + } + + _superblocktype_2_3 = (header.type == 2 || header.type == 3); + packet_bytes = (_packetSize - getBitsCount(&gb) / 8); + + initGetBits(&gb, header.data, header.size*8); + + if (header.type == 2 || header.type == 4 || header.type == 5) { + int csum = 257 * getBits(&gb, 8) + 2 * getBits(&gb, 8); + + csum = qdm2_packet_checksum(_compressedData, _packetSize, csum); + + if (csum != 0) { + _hasErrors = true; + error("QDM2 : bad packet checksum"); + return; + } + } + + _subPacketListB[0].packet = NULL; + _subPacketListD[0].packet = NULL; + + for (i = 0; i < 6; i++) + if (--_fftLevelExp[i] < 0) + _fftLevelExp[i] = 0; + + for (i = 0; packet_bytes > 0; i++) { + int j; + + _subPacketListA[i].next = NULL; + + if (i > 0) { + _subPacketListA[i - 1].next = &_subPacketListA[i]; + + // seek to next block + initGetBits(&gb, header.data, header.size*8); + skipBits(&gb, next_index*8); + + if (next_index >= header.size) + break; + } + + // decode subpacket + packet = &_subPackets[i]; + qdm2_decode_sub_packet_header(&gb, packet); + next_index = packet->size + getBitsCount(&gb) / 8; + sub_packet_size = ((packet->size > 0xff) ? 1 : 0) + packet->size + 2; + + if (packet->type == 0) + break; + + if (sub_packet_size > packet_bytes) { + if (packet->type != 10 && packet->type != 11 && packet->type != 12) + break; + packet->size += packet_bytes - sub_packet_size; + } + + packet_bytes -= sub_packet_size; + + // add subpacket to 'all subpackets' list + _subPacketListA[i].packet = packet; + + // add subpacket to related list + if (packet->type == 8) { + error("Unsupported packet type 8"); + return; + } else if (packet->type >= 9 && packet->type <= 12) { + // packets for MPEG Audio like Synthesis Filter + QDM2_LIST_ADD(_subPacketListD, subPacketsD, packet); + } else if (packet->type == 13) { + for (j = 0; j < 6; j++) + _fftLevelExp[j] = getBits(&gb, 6); + } else if (packet->type == 14) { + for (j = 0; j < 6; j++) + _fftLevelExp[j] = qdm2_get_vlc(&gb, &_fftLevelExpVlc, 0, 2); + } else if (packet->type == 15) { + error("Unsupported packet type 15"); + return; + } else if (packet->type >= 16 && packet->type < 48 && !fft_subpackets[packet->type - 16]) { + // packets for FFT + QDM2_LIST_ADD(_subPacketListB, _subPacketsB, packet); + } + } // Packet bytes loop + +// **************************************************************** + if (_subPacketListD[0].packet != NULL) { + process_synthesis_subpackets(_subPacketListD); + _doSynthFilter = 1; + } else if (_doSynthFilter) { + process_subpacket_10(NULL, 0); + process_subpacket_11(NULL, 0); + process_subpacket_12(NULL, 0); + } +// **************************************************************** +} + +void QDM2Stream::qdm2_fft_init_coefficient(int sub_packet, int offset, int duration, + int channel, int exp, int phase) { + if (_fftCoefsMinIndex[duration] < 0) + _fftCoefsMinIndex[duration] = _fftCoefsIndex; + + _fftCoefs[_fftCoefsIndex].sub_packet = ((sub_packet >= 16) ? (sub_packet - 16) : sub_packet); + _fftCoefs[_fftCoefsIndex].channel = channel; + _fftCoefs[_fftCoefsIndex].offset = offset; + _fftCoefs[_fftCoefsIndex].exp = exp; + _fftCoefs[_fftCoefsIndex].phase = phase; + _fftCoefsIndex++; +} + +void QDM2Stream::qdm2_fft_decode_tones(int duration, GetBitContext *gb, int b) { + int channel, stereo, phase, exp; + int local_int_4, local_int_8, stereo_phase, local_int_10; + int local_int_14, stereo_exp, local_int_20, local_int_28; + int n, offset; + + local_int_4 = 0; + local_int_28 = 0; + local_int_20 = 2; + local_int_8 = (4 - duration); + local_int_10 = 1 << (_groupOrder - duration - 1); + offset = 1; + + while (1) { + if (_superblocktype_2_3) { + while ((n = qdm2_get_vlc(gb, &_vlcTabFftToneOffset[local_int_8], 1, 2)) < 2) { + offset = 1; + if (n == 0) { + local_int_4 += local_int_10; + local_int_28 += (1 << local_int_8); + } else { + local_int_4 += 8*local_int_10; + local_int_28 += (8 << local_int_8); + } + } + offset += (n - 2); + } else { + offset += qdm2_get_vlc(gb, &_vlcTabFftToneOffset[local_int_8], 1, 2); + while (offset >= (local_int_10 - 1)) { + offset += (1 - (local_int_10 - 1)); + local_int_4 += local_int_10; + local_int_28 += (1 << local_int_8); + } + } + + if (local_int_4 >= _blockSize) + return; + + local_int_14 = (offset >> local_int_8); + + if (_channels > 1) { + channel = getBits1(gb); + stereo = getBits1(gb); + } else { + channel = 0; + stereo = 0; + } + + exp = qdm2_get_vlc(gb, (b ? &_fftLevelExpVlc : &_fftLevelExpAltVlc), 0, 2); + exp += _fftLevelExp[fft_level_index_table[local_int_14]]; + exp = (exp < 0) ? 0 : exp; + + phase = getBits(gb, 3); + stereo_exp = 0; + stereo_phase = 0; + + if (stereo) { + stereo_exp = (exp - qdm2_get_vlc(gb, &_fftStereoExpVlc, 0, 1)); + stereo_phase = (phase - qdm2_get_vlc(gb, &_fftStereoPhaseVlc, 0, 1)); + if (stereo_phase < 0) + stereo_phase += 8; + } + + if (_frequencyRange > (local_int_14 + 1)) { + int sub_packet = (local_int_20 + local_int_28); + + qdm2_fft_init_coefficient(sub_packet, offset, duration, channel, exp, phase); + if (stereo) + qdm2_fft_init_coefficient(sub_packet, offset, duration, (1 - channel), stereo_exp, stereo_phase); + } + + offset++; + } +} + +void QDM2Stream::qdm2_decode_fft_packets(void) { + int i, j, min, max, value, type, unknown_flag; + GetBitContext gb; + + if (_subPacketListB[0].packet == NULL) + return; + + // reset minimum indexes for FFT coefficients + _fftCoefsIndex = 0; + for (i=0; i < 5; i++) + _fftCoefsMinIndex[i] = -1; + + // process subpackets ordered by type, largest type first + for (i = 0, max = 256; i < _subPacketsB; i++) { + QDM2SubPacket *packet= NULL; + + // find subpacket with largest type less than max + for (j = 0, min = 0; j < _subPacketsB; j++) { + value = _subPacketListB[j].packet->type; + if (value > min && value < max) { + min = value; + packet = _subPacketListB[j].packet; + } + } + + max = min; + + // check for errors (?) + if (!packet) + return; + + if (i == 0 && (packet->type < 16 || packet->type >= 48 || fft_subpackets[packet->type - 16])) + return; + + // decode FFT tones + initGetBits(&gb, packet->data, packet->size*8); + + if (packet->type >= 32 && packet->type < 48 && !fft_subpackets[packet->type - 16]) + unknown_flag = 1; + else + unknown_flag = 0; + + type = packet->type; + + if ((type >= 17 && type < 24) || (type >= 33 && type < 40)) { + int duration = _subSampling + 5 - (type & 15); + + if (duration >= 0 && duration < 4) { // TODO: Should be <= 4? + qdm2_fft_decode_tones(duration, &gb, unknown_flag); + } + } else if (type == 31) { + for (j=0; j < 4; j++) { + qdm2_fft_decode_tones(j, &gb, unknown_flag); + } + } else if (type == 46) { + for (j=0; j < 6; j++) + _fftLevelExp[j] = getBits(&gb, 6); + for (j=0; j < 4; j++) { + qdm2_fft_decode_tones(j, &gb, unknown_flag); + } + } + } // Loop on B packets + + // calculate maximum indexes for FFT coefficients + for (i = 0, j = -1; i < 5; i++) + if (_fftCoefsMinIndex[i] >= 0) { + if (j >= 0) + _fftCoefsMaxIndex[j] = _fftCoefsMinIndex[i]; + j = i; + } + if (j >= 0) + _fftCoefsMaxIndex[j] = _fftCoefsIndex; +} + +void QDM2Stream::qdm2_fft_generate_tone(FFTTone *tone) +{ + float level, f[6]; + int i; + QDM2Complex c; + const double iscale = 2.0 * M_PI / 512.0; + + tone->phase += tone->phase_shift; + + // calculate current level (maximum amplitude) of tone + level = fft_tone_envelope_table[tone->duration][tone->time_index] * tone->level; + c.im = level * sin(tone->phase*iscale); + c.re = level * cos(tone->phase*iscale); + + // generate FFT coefficients for tone + if (tone->duration >= 3 || tone->cutoff >= 3) { + tone->complex[0].im += c.im; + tone->complex[0].re += c.re; + tone->complex[1].im -= c.im; + tone->complex[1].re -= c.re; + } else { + f[1] = -tone->table[4]; + f[0] = tone->table[3] - tone->table[0]; + f[2] = 1.0 - tone->table[2] - tone->table[3]; + f[3] = tone->table[1] + tone->table[4] - 1.0; + f[4] = tone->table[0] - tone->table[1]; + f[5] = tone->table[2]; + for (i = 0; i < 2; i++) { + tone->complex[fft_cutoff_index_table[tone->cutoff][i]].re += c.re * f[i]; + tone->complex[fft_cutoff_index_table[tone->cutoff][i]].im += c.im *((tone->cutoff <= i) ? -f[i] : f[i]); + } + for (i = 0; i < 4; i++) { + tone->complex[i].re += c.re * f[i+2]; + tone->complex[i].im += c.im * f[i+2]; + } + } + + // copy the tone if it has not yet died out + if (++tone->time_index < ((1 << (5 - tone->duration)) - 1)) { + memcpy(&_fftTones[_fftToneEnd], tone, sizeof(FFTTone)); + _fftToneEnd = (_fftToneEnd + 1) % 1000; + } +} + +void QDM2Stream::qdm2_fft_tone_synthesizer(uint8 sub_packet) { + int i, j, ch; + const double iscale = 0.25 * M_PI; + + for (ch = 0; ch < _channels; ch++) { + memset(_fft.complex[ch], 0, _frameSize * sizeof(QDM2Complex)); + } + + // apply FFT tones with duration 4 (1 FFT period) + if (_fftCoefsMinIndex[4] >= 0) + for (i = _fftCoefsMinIndex[4]; i < _fftCoefsMaxIndex[4]; i++) { + float level; + QDM2Complex c; + + if (_fftCoefs[i].sub_packet != sub_packet) + break; + + ch = (_channels == 1) ? 0 : _fftCoefs[i].channel; + level = (_fftCoefs[i].exp < 0) ? 0.0 : fft_tone_level_table[_superblocktype_2_3 ? 0 : 1][_fftCoefs[i].exp & 63]; + + c.re = level * cos(_fftCoefs[i].phase * iscale); + c.im = level * sin(_fftCoefs[i].phase * iscale); + _fft.complex[ch][_fftCoefs[i].offset + 0].re += c.re; + _fft.complex[ch][_fftCoefs[i].offset + 0].im += c.im; + _fft.complex[ch][_fftCoefs[i].offset + 1].re -= c.re; + _fft.complex[ch][_fftCoefs[i].offset + 1].im -= c.im; + } + + // generate existing FFT tones + for (i = _fftToneEnd; i != _fftToneStart; ) { + qdm2_fft_generate_tone(&_fftTones[_fftToneStart]); + _fftToneStart = (_fftToneStart + 1) % 1000; + } + + // create and generate new FFT tones with duration 0 (long) to 3 (short) + for (i = 0; i < 4; i++) + if (_fftCoefsMinIndex[i] >= 0) { + for (j = _fftCoefsMinIndex[i]; j < _fftCoefsMaxIndex[i]; j++) { + int offset, four_i; + FFTTone tone; + + if (_fftCoefs[j].sub_packet != sub_packet) + break; + + four_i = (4 - i); + offset = _fftCoefs[j].offset >> four_i; + ch = (_channels == 1) ? 0 : _fftCoefs[j].channel; + + if (offset < _frequencyRange) { + if (offset < 2) + tone.cutoff = offset; + else + tone.cutoff = (offset >= 60) ? 3 : 2; + + tone.level = (_fftCoefs[j].exp < 0) ? 0.0 : fft_tone_level_table[_superblocktype_2_3 ? 0 : 1][_fftCoefs[j].exp & 63]; + tone.complex = &_fft.complex[ch][offset]; + tone.table = fft_tone_sample_table[i][_fftCoefs[j].offset - (offset << four_i)]; + tone.phase = 64 * _fftCoefs[j].phase - (offset << 8) - 128; + tone.phase_shift = (2 * _fftCoefs[j].offset + 1) << (7 - four_i); + tone.duration = i; + tone.time_index = 0; + + qdm2_fft_generate_tone(&tone); + } + } + _fftCoefsMinIndex[i] = j; + } +} + +void QDM2Stream::qdm2_calculate_fft(int channel) { + int i; + + _fft.complex[channel][0].re *= 2.0f; + _fft.complex[channel][0].im = 0.0f; + + rdftCalc(&_rdftCtx, (float *)_fft.complex[channel]); + + // add samples to output buffer + for (i = 0; i < ((_fftFrameSize + 15) & ~15); i++) + _outputBuffer[_channels * i + channel] += ((float *) _fft.complex[channel])[i]; +} + +/** + * @param index subpacket number + */ +void QDM2Stream::qdm2_synthesis_filter(uint8 index) +{ + int16 samples[MPA_MAX_CHANNELS * MPA_FRAME_SIZE]; + int i, k, ch, sb_used, sub_sampling, dither_state = 0; + + // copy sb_samples + sb_used = QDM2_SB_USED(_subSampling); + + for (ch = 0; ch < _channels; ch++) + for (i = 0; i < 8; i++) + for (k = sb_used; k < 32; k++) + _sbSamples[ch][(8 * index) + i][k] = 0; + + for (ch = 0; ch < _channels; ch++) { + int16 *samples_ptr = samples + ch; + + for (i = 0; i < 8; i++) { + ff_mpa_synth_filter(_synthBuf[ch], &(_synthBufOffset[ch]), + ff_mpa_synth_window, &dither_state, + samples_ptr, _channels, + _sbSamples[ch][(8 * index) + i]); + samples_ptr += 32 * _channels; + } + } + + // add samples to output buffer + sub_sampling = (4 >> _subSampling); + + for (ch = 0; ch < _channels; ch++) + for (i = 0; i < _sFrameSize; i++) + _outputBuffer[_channels * i + ch] += (float)(samples[_channels * sub_sampling * i + ch] >> (sizeof(int16)*8-16)); +} + +int QDM2Stream::qdm2_decodeFrame(Common::SeekableReadStream *in) { + debug(1, "QDM2Stream::qdm2_decodeFrame in->pos(): %d in->size(): %d", in->pos(), in->size()); + int ch, i; + const int frame_size = (_sFrameSize * _channels); + + // If we're in any packet but the first, seek back to the first + if (_subPacket == 0) + _superBlockStart = in->pos(); + else + in->seek(_superBlockStart); + + // select input buffer + if (in->eos() || in->pos() >= in->size()) { + debug(1, "QDM2Stream::qdm2_decodeFrame End of Input Stream"); + return 0; + } + + if ((in->size() - in->pos()) < _packetSize) { + debug(1, "QDM2Stream::qdm2_decodeFrame Insufficient Packet Data in Input Stream Found: %d Need: %d", in->size() - in->pos(), _packetSize); + return 0; + } + + if (!in->eos()) { + in->read(_compressedData, _packetSize); + debug(1, "QDM2Stream::qdm2_decodeFrame constructed input data"); + } + + // copy old block, clear new block of output samples + memmove(_outputBuffer, &_outputBuffer[frame_size], frame_size * sizeof(float)); + memset(&_outputBuffer[frame_size], 0, frame_size * sizeof(float)); + debug(1, "QDM2Stream::qdm2_decodeFrame cleared outputBuffer"); + + if (!in->eos()) { + // decode block of QDM2 compressed data + debug(1, "QDM2Stream::qdm2_decodeFrame decode block of QDM2 compressed data"); + if (_subPacket == 0) { + _hasErrors = false; // reset it for a new super block + debug(1, "QDM2 : Superblock follows"); + qdm2_decode_super_block(); + } + + // parse subpackets + debug(1, "QDM2Stream::qdm2_decodeFrame parse subpackets"); + if (!_hasErrors) { + if (_subPacket == 2) { + debug(1, "QDM2Stream::qdm2_decodeFrame qdm2_decode_fft_packets()"); + qdm2_decode_fft_packets(); + } + + debug(1, "QDM2Stream::qdm2_decodeFrame qdm2_fft_tone_synthesizer(%d)", _subPacket); + qdm2_fft_tone_synthesizer(_subPacket); + } + + // sound synthesis stage 1 (FFT) + debug(1, "QDM2Stream::qdm2_decodeFrame sound synthesis stage 1 (FFT)"); + for (ch = 0; ch < _channels; ch++) { + qdm2_calculate_fft(ch); + + if (!_hasErrors && _subPacketListC[0].packet != NULL) { + error("QDM2 : has errors, and C list is not empty"); + return 0; + } + } + + // sound synthesis stage 2 (MPEG audio like synthesis filter) + debug(1, "QDM2Stream::qdm2_decodeFrame sound synthesis stage 2 (MPEG audio like synthesis filter)"); + if (!_hasErrors && _doSynthFilter) + qdm2_synthesis_filter(_subPacket); + + _subPacket = (_subPacket + 1) % 16; + + if(_hasErrors) + warning("QDM2 Packet error..."); + + // clip and convert output float[] to 16bit signed samples + debug(1, "QDM2Stream::qdm2_decodeFrame clip and convert output float[] to 16bit signed samples"); + } + + for (i = 0; i < frame_size; i++) { + int value = (int)_outputBuffer[i]; + + if (value > SOFTCLIP_THRESHOLD) + value = (value > HARDCLIP_THRESHOLD) ? 32767 : _softclipTable[ value - SOFTCLIP_THRESHOLD]; + else if (value < -SOFTCLIP_THRESHOLD) + value = (value < -HARDCLIP_THRESHOLD) ? -32767 : -_softclipTable[-value - SOFTCLIP_THRESHOLD]; + + _outputSamples.push_back(value); + } + return frame_size; +} + +int QDM2Stream::readBuffer(int16 *buffer, const int numSamples) { + debug(1, "QDM2Stream::readBuffer numSamples: %d", numSamples); + int32 decodedSamples = _outputSamples.size(); + int32 i; + + while (decodedSamples < numSamples) { + i = qdm2_decodeFrame(_stream); + if (i == 0) + break; // Out Of Decode Frames... + decodedSamples += i; + } + + if (decodedSamples > numSamples) + decodedSamples = numSamples; + + for (i = 0; i < decodedSamples; i++) + buffer[i] = _outputSamples.remove_at(0); + + return decodedSamples; +} + +AudioStream *makeQDM2Stream(Common::SeekableReadStream *stream, Common::SeekableReadStream *extraData) { + return new QDM2Stream(stream, extraData); +} + +} // End of namespace Audio + +#endif diff --git a/audio/decoders/qdm2.h b/audio/decoders/qdm2.h new file mode 100644 index 0000000000..c0ec647bfd --- /dev/null +++ b/audio/decoders/qdm2.h @@ -0,0 +1,49 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +// Only compile if Mohawk is enabled or if we're building dynamic modules +#if defined(ENABLE_MOHAWK) || defined(DYNAMIC_MODULES) + +#ifndef AUDIO_QDM2_H +#define AUDIO_QDM2_H + +namespace Common { +class SeekableReadStream; +} + +namespace Audio { + +class AudioStream; + +/** + * Create a new AudioStream from the QDM2 data in the given stream. + * + * @param stream the SeekableReadStream from which to read the FLAC data + * @param extraData the QuickTime extra data stream + * @return a new AudioStream, or NULL, if an error occurred + */ +AudioStream *makeQDM2Stream(Common::SeekableReadStream *stream, Common::SeekableReadStream *extraData); + +} // End of namespace Audio + +#endif // AUDIO_QDM2_H +#endif // Mohawk/Plugins guard diff --git a/audio/decoders/qdm2data.h b/audio/decoders/qdm2data.h new file mode 100644 index 0000000000..d92bc0ff80 --- /dev/null +++ b/audio/decoders/qdm2data.h @@ -0,0 +1,528 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#ifndef AUDIO_QDM2DATA_H +#define AUDIO_QDM2DATA_H + +#include "common/scummsys.h" + +namespace Audio { + +/// VLC TABLES + +// values in this table range from -1..23; adjust retrieved value by -1 +static const uint16 vlc_tab_level_huffcodes[24] = { + 0x037c, 0x0004, 0x003c, 0x004c, 0x003a, 0x002c, 0x001c, 0x001a, + 0x0024, 0x0014, 0x0001, 0x0002, 0x0000, 0x0003, 0x0007, 0x0005, + 0x0006, 0x0008, 0x0009, 0x000a, 0x000c, 0x00fc, 0x007c, 0x017c +}; + +static const byte vlc_tab_level_huffbits[24] = { + 10, 6, 7, 7, 6, 6, 6, 6, 6, 5, 4, 4, 4, 3, 3, 3, 3, 4, 4, 5, 7, 8, 9, 10 +}; + +// values in this table range from -1..36; adjust retrieved value by -1 +static const uint16 vlc_tab_diff_huffcodes[37] = { + 0x1c57, 0x0004, 0x0000, 0x0001, 0x0003, 0x0002, 0x000f, 0x000e, + 0x0007, 0x0016, 0x0037, 0x0027, 0x0026, 0x0066, 0x0006, 0x0097, + 0x0046, 0x01c6, 0x0017, 0x0786, 0x0086, 0x0257, 0x00d7, 0x0357, + 0x00c6, 0x0386, 0x0186, 0x0000, 0x0157, 0x0c57, 0x0057, 0x0000, + 0x0b86, 0x0000, 0x1457, 0x0000, 0x0457 +}; + +static const byte vlc_tab_diff_huffbits[37] = { + 13, 3, 3, 2, 3, 3, 4, 4, 6, 5, 6, 6, 7, 7, 8, 8, + 8, 9, 8, 11, 9, 10, 8, 10, 9, 12, 10, 0, 10, 13, 11, 0, + 12, 0, 13, 0, 13 +}; + +// values in this table range from -1..5; adjust retrieved value by -1 +static const byte vlc_tab_run_huffcodes[6] = { + 0x1f, 0x00, 0x01, 0x03, 0x07, 0x0f +}; + +static const byte vlc_tab_run_huffbits[6] = { + 5, 1, 2, 3, 4, 5 +}; + +// values in this table range from -1..19; adjust retrieved value by -1 +static const uint16 vlc_tab_tone_level_idx_hi1_huffcodes[20] = { + 0x5714, 0x000c, 0x0002, 0x0001, 0x0000, 0x0004, 0x0034, 0x0054, + 0x0094, 0x0014, 0x0114, 0x0214, 0x0314, 0x0614, 0x0e14, 0x0f14, + 0x2714, 0x0714, 0x1714, 0x3714 +}; + +static const byte vlc_tab_tone_level_idx_hi1_huffbits[20] = { + 15, 4, 2, 1, 3, 5, 6, 7, 8, 10, 10, 11, 11, 12, 12, 12, 14, 14, 15, 14 +}; + +// values in this table range from -1..23; adjust retrieved value by -1 +static const uint16 vlc_tab_tone_level_idx_mid_huffcodes[24] = { + 0x0fea, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x03ea, 0x00ea, 0x002a, 0x001a, + 0x0006, 0x0001, 0x0000, 0x0002, 0x000a, 0x006a, 0x01ea, 0x07ea +}; + +static const byte vlc_tab_tone_level_idx_mid_huffbits[24] = { + 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 9, 7, 5, 3, 1, 2, 4, 6, 8, 10, 12 +}; + +// values in this table range from -1..23; adjust retrieved value by -1 +static const uint16 vlc_tab_tone_level_idx_hi2_huffcodes[24] = { + 0x0664, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0064, 0x00e4, + 0x00a4, 0x0068, 0x0004, 0x0008, 0x0014, 0x0018, 0x0000, 0x0001, + 0x0002, 0x0003, 0x000c, 0x0028, 0x0024, 0x0164, 0x0000, 0x0264 +}; + +static const byte vlc_tab_tone_level_idx_hi2_huffbits[24] = { + 11, 0, 0, 0, 0, 0, 10, 8, 8, 7, 6, 6, 5, 5, 4, 2, 2, 2, 4, 7, 8, 9, 0, 11 +}; + +// values in this table range from -1..8; adjust retrieved value by -1 +static const byte vlc_tab_type30_huffcodes[9] = { + 0x3c, 0x06, 0x00, 0x01, 0x03, 0x02, 0x04, 0x0c, 0x1c +}; + +static const byte vlc_tab_type30_huffbits[9] = { + 6, 3, 3, 2, 2, 3, 4, 5, 6 +}; + +// values in this table range from -1..9; adjust retrieved value by -1 +static const byte vlc_tab_type34_huffcodes[10] = { + 0x18, 0x00, 0x01, 0x04, 0x05, 0x07, 0x03, 0x02, 0x06, 0x08 +}; + +static const byte vlc_tab_type34_huffbits[10] = { + 5, 4, 3, 3, 3, 3, 3, 3, 3, 5 +}; + +// values in this table range from -1..22; adjust retrieved value by -1 +static const uint16 vlc_tab_fft_tone_offset_0_huffcodes[23] = { + 0x038e, 0x0001, 0x0000, 0x0022, 0x000a, 0x0006, 0x0012, 0x0002, + 0x001e, 0x003e, 0x0056, 0x0016, 0x000e, 0x0032, 0x0072, 0x0042, + 0x008e, 0x004e, 0x00f2, 0x002e, 0x0036, 0x00c2, 0x018e +}; + +static const byte vlc_tab_fft_tone_offset_0_huffbits[23] = { + 10, 1, 2, 6, 4, 5, 6, 7, 6, 6, 7, 7, 8, 7, 8, 8, 9, 7, 8, 6, 6, 8, 10 +}; + +// values in this table range from -1..27; adjust retrieved value by -1 +static const uint16 vlc_tab_fft_tone_offset_1_huffcodes[28] = { + 0x07a4, 0x0001, 0x0020, 0x0012, 0x001c, 0x0008, 0x0006, 0x0010, + 0x0000, 0x0014, 0x0004, 0x0032, 0x0070, 0x000c, 0x0002, 0x003a, + 0x001a, 0x002c, 0x002a, 0x0022, 0x0024, 0x000a, 0x0064, 0x0030, + 0x0062, 0x00a4, 0x01a4, 0x03a4 +}; + +static const byte vlc_tab_fft_tone_offset_1_huffbits[28] = { + 11, 1, 6, 6, 5, 4, 3, 6, 6, 5, 6, 6, 7, 6, 6, 6, + 6, 6, 6, 7, 8, 6, 7, 7, 7, 9, 10, 11 +}; + +// values in this table range from -1..31; adjust retrieved value by -1 +static const uint16 vlc_tab_fft_tone_offset_2_huffcodes[32] = { + 0x1760, 0x0001, 0x0000, 0x0082, 0x000c, 0x0006, 0x0003, 0x0007, + 0x0008, 0x0004, 0x0010, 0x0012, 0x0022, 0x001a, 0x0000, 0x0020, + 0x000a, 0x0040, 0x004a, 0x006a, 0x002a, 0x0042, 0x0002, 0x0060, + 0x00aa, 0x00e0, 0x00c2, 0x01c2, 0x0160, 0x0360, 0x0760, 0x0f60 +}; + +static const byte vlc_tab_fft_tone_offset_2_huffbits[32] = { + 13, 2, 0, 8, 4, 3, 3, 3, 4, 4, 5, 5, 6, 5, 7, 7, + 7, 7, 7, 7, 8, 8, 8, 9, 8, 8, 9, 9, 10, 11, 13, 12 +}; + +// values in this table range from -1..34; adjust retrieved value by -1 +static const uint16 vlc_tab_fft_tone_offset_3_huffcodes[35] = { + 0x33ea, 0x0005, 0x0000, 0x000c, 0x0000, 0x0006, 0x0003, 0x0008, + 0x0002, 0x0001, 0x0004, 0x0007, 0x001a, 0x000f, 0x001c, 0x002c, + 0x000a, 0x001d, 0x002d, 0x002a, 0x000d, 0x004c, 0x008c, 0x006a, + 0x00cd, 0x004d, 0x00ea, 0x020c, 0x030c, 0x010c, 0x01ea, 0x07ea, + 0x0bea, 0x03ea, 0x13ea +}; + +static const byte vlc_tab_fft_tone_offset_3_huffbits[35] = { + 14, 4, 0, 10, 4, 3, 3, 4, 4, 3, 4, 4, 5, 4, 5, 6, + 6, 5, 6, 7, 7, 7, 8, 8, 8, 8, 9, 10, 10, 10, 10, 11, + 12, 13, 14 +}; + +// values in this table range from -1..37; adjust retrieved value by -1 +static const uint16 vlc_tab_fft_tone_offset_4_huffcodes[38] = { + 0x5282, 0x0016, 0x0000, 0x0136, 0x0004, 0x0000, 0x0007, 0x000a, + 0x000e, 0x0003, 0x0001, 0x000d, 0x0006, 0x0009, 0x0012, 0x0005, + 0x0025, 0x0022, 0x0015, 0x0002, 0x0076, 0x0035, 0x0042, 0x00c2, + 0x0182, 0x00b6, 0x0036, 0x03c2, 0x0482, 0x01c2, 0x0682, 0x0882, + 0x0a82, 0x0082, 0x0282, 0x1282, 0x3282, 0x2282 +}; + +static const byte vlc_tab_fft_tone_offset_4_huffbits[38] = { + 15, 6, 0, 9, 3, 3, 3, 4, 4, 3, 4, 4, 5, 4, 5, 6, + 6, 6, 6, 8, 7, 6, 8, 9, 9, 8, 9, 10, 11, 10, 11, 12, + 12, 12, 14, 15, 14, 14 +}; + +/// FFT TABLES + +// values in this table range from -1..27; adjust retrieved value by -1 +static const uint16 fft_level_exp_alt_huffcodes[28] = { + 0x1ec6, 0x0006, 0x00c2, 0x0142, 0x0242, 0x0246, 0x00c6, 0x0046, + 0x0042, 0x0146, 0x00a2, 0x0062, 0x0026, 0x0016, 0x000e, 0x0005, + 0x0004, 0x0003, 0x0000, 0x0001, 0x000a, 0x0012, 0x0002, 0x0022, + 0x01c6, 0x02c6, 0x06c6, 0x0ec6 +}; + +static const byte fft_level_exp_alt_huffbits[28] = { + 13, 7, 8, 9, 10, 10, 10, 10, 10, 9, 8, 7, 6, 5, 4, 3, + 3, 2, 3, 3, 4, 5, 7, 8, 9, 11, 12, 13 +}; + +// values in this table range from -1..19; adjust retrieved value by -1 +static const uint16 fft_level_exp_huffcodes[20] = { + 0x0f24, 0x0001, 0x0002, 0x0000, 0x0006, 0x0005, 0x0007, 0x000c, + 0x000b, 0x0014, 0x0013, 0x0004, 0x0003, 0x0023, 0x0064, 0x00a4, + 0x0024, 0x0124, 0x0324, 0x0724 +}; + +static const byte fft_level_exp_huffbits[20] = { + 12, 3, 3, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 8, 9, 10, 11, 12 +}; + +// values in this table range from -1..6; adjust retrieved value by -1 +static const byte fft_stereo_exp_huffcodes[7] = { + 0x3e, 0x01, 0x00, 0x02, 0x06, 0x0e, 0x1e +}; + +static const byte fft_stereo_exp_huffbits[7] = { + 6, 1, 2, 3, 4, 5, 6 +}; + +// values in this table range from -1..8; adjust retrieved value by -1 +static const byte fft_stereo_phase_huffcodes[9] = { + 0x35, 0x02, 0x00, 0x01, 0x0d, 0x15, 0x05, 0x09, 0x03 +}; + +static const byte fft_stereo_phase_huffbits[9] = { + 6, 2, 2, 4, 4, 6, 5, 4, 2 +}; + +static const int fft_cutoff_index_table[4][2] = { + { 1, 2 }, {-1, 0 }, {-1,-2 }, { 0, 0 } +}; + +static const int16 fft_level_index_table[256] = { + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, + 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, + 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, +}; + +static const byte last_coeff[3] = { + 4, 7, 10 +}; + +static const byte coeff_per_sb_for_avg[3][30] = { + { 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, + { 0, 1, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 }, + { 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9 } +}; + +static const uint32 dequant_table[3][10][30] = { + { { 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 256, 256, 205, 154, 102, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 51, 102, 154, 205, 256, 238, 219, 201, 183, 165, 146, 128, 110, 91, 73, 55, 37, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 18, 37, 55, 73, 91, 110, 128, 146, 165, 183, 201, 219, 238, 256, 228, 199, 171, 142, 114, 85, 57, 28 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, + { { 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 256, 171, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 85, 171, 256, 171, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 85, 171, 256, 219, 183, 146, 110, 73, 37, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 73, 110, 146, 183, 219, 256, 228, 199, 171, 142, 114, 85, 57, 28, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28, 57, 85, 114, 142, 171, 199, 228, 256, 213, 171, 128, 85, 43 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, + { { 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 256, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 256, 171, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 85, 171, 256, 192, 128, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 128, 192, 256, 205, 154, 102, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 102, 154, 205, 256, 213, 171, 128, 85, 43, 0, 0, 0, 0, 0, 0 }, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 43, 85, 128, 171, 213, 256, 213, 171, 128, 85, 43 } } +}; + +static const byte coeff_per_sb_for_dequant[3][30] = { + { 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, + { 0, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6 }, + { 0, 1, 2, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9 } +}; + +// first index is subband, 2nd index is 0, 1 or 3 (2 is unused) +static const int8 tone_level_idx_offset_table[30][4] = { + { -50, -50, 0, -50 }, + { -50, -50, 0, -50 }, + { -50, -9, 0, -19 }, + { -16, -6, 0, -12 }, + { -11, -4, 0, -8 }, + { -8, -3, 0, -6 }, + { -7, -3, 0, -5 }, + { -6, -2, 0, -4 }, + { -5, -2, 0, -3 }, + { -4, -1, 0, -3 }, + { -4, -1, 0, -2 }, + { -3, -1, 0, -2 }, + { -3, -1, 0, -2 }, + { -3, -1, 0, -2 }, + { -2, -1, 0, -1 }, + { -2, -1, 0, -1 }, + { -2, -1, 0, -1 }, + { -2, 0, 0, -1 }, + { -2, 0, 0, -1 }, + { -1, 0, 0, -1 }, + { -1, 0, 0, -1 }, + { -1, 0, 0, -1 }, + { -1, 0, 0, -1 }, + { -1, 0, 0, -1 }, + { -1, 0, 0, -1 }, + { -1, 0, 0, -1 }, + { -1, 0, 0, 0 }, + { -1, 0, 0, 0 }, + { -1, 0, 0, 0 }, + { -1, 0, 0, 0 } +}; + +/* all my samples have 1st index 0 or 1 */ +/* second index is subband, only indexes 0-29 seem to be used */ +static const int8 coding_method_table[5][30] = { + { 34, 30, 24, 24, 16, 16, 16, 16, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 + }, + { 34, 30, 24, 24, 16, 16, 16, 16, 10, 10, 10, 10, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 + }, + { 34, 30, 30, 30, 24, 24, 16, 16, 16, 16, 16, 16, 10, 10, 10, + 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 + }, + { 34, 34, 30, 30, 24, 24, 24, 24, 16, 16, 16, 16, 16, 16, 16, + 16, 16, 16, 16, 16, 16, 16, 10, 10, 10, 10, 10, 10, 10, 10 + }, + { 34, 34, 30, 30, 30, 30, 30, 30, 24, 24, 24, 24, 24, 24, 24, + 24, 24, 24, 24, 24, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16 + }, +}; + +static const int vlc_stage3_values[60] = { + 0, 1, 2, 3, 4, 6, 8, 10, 12, 16, 20, 24, + 28, 36, 44, 52, 60, 76, 92, 108, 124, 156, 188, 220, + 252, 316, 380, 444, 508, 636, 764, 892, 1020, 1276, 1532, 1788, + 2044, 2556, 3068, 3580, 4092, 5116, 6140, 7164, 8188, 10236, 12284, 14332, + 16380, 20476, 24572, 28668, 32764, 40956, 49148, 57340, 65532, 81916, 98300,114684 +}; + +static const float fft_tone_sample_table[4][16][5] = { + { { .0100000000f,-.0037037037f,-.0020000000f,-.0069444444f,-.0018416207f }, + { .0416666667f, .0000000000f, .0000000000f,-.0208333333f,-.0123456791f }, + { .1250000000f, .0558035709f, .0330687836f,-.0164473690f,-.0097465888f }, + { .1562500000f, .0625000000f, .0370370370f,-.0062500000f,-.0037037037f }, + { .1996007860f, .0781250000f, .0462962948f, .0022727272f, .0013468013f }, + { .2000000000f, .0625000000f, .0370370373f, .0208333333f, .0074074073f }, + { .2127659619f, .0555555556f, .0329218097f, .0208333333f, .0123456791f }, + { .2173913121f, .0473484844f, .0280583613f, .0347222239f, .0205761325f }, + { .2173913121f, .0347222239f, .0205761325f, .0473484844f, .0280583613f }, + { .2127659619f, .0208333333f, .0123456791f, .0555555556f, .0329218097f }, + { .2000000000f, .0208333333f, .0074074073f, .0625000000f, .0370370370f }, + { .1996007860f, .0022727272f, .0013468013f, .0781250000f, .0462962948f }, + { .1562500000f,-.0062500000f,-.0037037037f, .0625000000f, .0370370370f }, + { .1250000000f,-.0164473690f,-.0097465888f, .0558035709f, .0330687836f }, + { .0416666667f,-.0208333333f,-.0123456791f, .0000000000f, .0000000000f }, + { .0100000000f,-.0069444444f,-.0018416207f,-.0037037037f,-.0020000000f } }, + + { { .0050000000f,-.0200000000f, .0125000000f,-.3030303030f, .0020000000f }, + { .1041666642f, .0400000000f,-.0250000000f, .0333333333f,-.0200000000f }, + { .1250000000f, .0100000000f, .0142857144f,-.0500000007f,-.0200000000f }, + { .1562500000f,-.0006250000f,-.00049382716f,-.000625000f,-.00049382716f }, + { .1562500000f,-.0006250000f,-.00049382716f,-.000625000f,-.00049382716f }, + { .1250000000f,-.0500000000f,-.0200000000f, .0100000000f, .0142857144f }, + { .1041666667f, .0333333333f,-.0200000000f, .0400000000f,-.0250000000f }, + { .0050000000f,-.3030303030f, .0020000001f,-.0200000000f, .0125000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f } }, + + { { .1428571492f, .1250000000f,-.0285714287f,-.0357142873f, .0208333333f }, + { .1818181818f, .0588235296f, .0333333333f, .0212765951f, .0100000000f }, + { .1818181818f, .0212765951f, .0100000000f, .0588235296f, .0333333333f }, + { .1428571492f,-.0357142873f, .0208333333f, .1250000000f,-.0285714287f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f } }, + + { { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f }, + { .0000000000f, .0000000000f, .0000000000f, .0000000000f, .0000000000f } } +}; + +static const float fft_tone_level_table[2][64] = { { +// pow ~ (i > 46) ? 0 : (((((i & 1) ? 431 : 304) << (i >> 1))) / 1024.0); + 0.17677669f, 0.42677650f, 0.60355347f, 0.85355347f, + 1.20710683f, 1.68359375f, 2.37500000f, 3.36718750f, + 4.75000000f, 6.73437500f, 9.50000000f, 13.4687500f, + 19.0000000f, 26.9375000f, 38.0000000f, 53.8750000f, + 76.0000000f, 107.750000f, 152.000000f, 215.500000f, + 304.000000f, 431.000000f, 608.000000f, 862.000000f, + 1216.00000f, 1724.00000f, 2432.00000f, 3448.00000f, + 4864.00000f, 6896.00000f, 9728.00000f, 13792.0000f, + 19456.0000f, 27584.0000f, 38912.0000f, 55168.0000f, + 77824.0000f, 110336.000f, 155648.000f, 220672.000f, + 311296.000f, 441344.000f, 622592.000f, 882688.000f, + 1245184.00f, 1765376.00f, 2490368.00f, 0.00000000f, + 0.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, + 0.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, + 0.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, + 0.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, + }, { +// pow = (i > 45) ? 0 : ((((i & 1) ? 431 : 304) << (i >> 1)) / 512.0); + 0.59375000f, 0.84179688f, 1.18750000f, 1.68359375f, + 2.37500000f, 3.36718750f, 4.75000000f, 6.73437500f, + 9.50000000f, 13.4687500f, 19.0000000f, 26.9375000f, + 38.0000000f, 53.8750000f, 76.0000000f, 107.750000f, + 152.000000f, 215.500000f, 304.000000f, 431.000000f, + 608.000000f, 862.000000f, 1216.00000f, 1724.00000f, + 2432.00000f, 3448.00000f, 4864.00000f, 6896.00000f, + 9728.00000f, 13792.0000f, 19456.0000f, 27584.0000f, + 38912.0000f, 55168.0000f, 77824.0000f, 110336.000f, + 155648.000f, 220672.000f, 311296.000f, 441344.000f, + 622592.000f, 882688.000f, 1245184.00f, 1765376.00f, + 2490368.00f, 3530752.00f, 0.00000000f, 0.00000000f, + 0.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, + 0.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, + 0.00000000f, 0.00000000f, 0.00000000f, 0.00000000f, + 0.00000000f, 0.00000000f, 0.00000000f, 0.00000000f +} }; + +static const float fft_tone_envelope_table[4][31] = { + { .009607375f, .038060248f, .084265202f, .146446645f, .222214907f, .308658302f, + .402454883f, .500000060f, .597545207f, .691341758f, .777785182f, .853553414f, + .915734828f, .961939812f, .990392685f, 1.00000000f, .990392625f, .961939752f, + .915734768f, .853553295f, .777785063f, .691341639f, .597545087f, .500000000f, + .402454853f, .308658272f, .222214878f, .146446615f, .084265172f, .038060218f, + .009607345f }, + { .038060248f, .146446645f, .308658302f, .500000060f, .691341758f, .853553414f, + .961939812f, 1.00000000f, .961939752f, .853553295f, .691341639f, .500000000f, + .308658272f, .146446615f, .038060218f, .000000000f, .000000000f, .000000000f, + .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, + .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, + .000000000f }, + { .146446645f, .500000060f, .853553414f, 1.00000000f, .853553295f, .500000000f, + .146446615f, .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, + .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, + .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, + .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, + .000000000f }, + { .500000060f, 1.00000000f, .500000000f, .000000000f, .000000000f, .000000000f, + .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, + .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, + .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, + .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, .000000000f, + .000000000f } +}; + +static const float sb_noise_attenuation[32] = { + 0.0f, 0.0f, 0.3f, 0.4f, 0.5f, 0.7f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, + 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, +}; + +static const byte fft_subpackets[32] = { + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0 +}; + +// first index is joined_stereo, second index is 0 or 2 (1 is unused) +static const float dequant_1bit[2][3] = { + {-0.920000f, 0.000000f, 0.920000f }, + {-0.890000f, 0.000000f, 0.890000f } +}; + +static const float type30_dequant[8] = { + -1.0f,-0.625f,-0.291666656732559f,0.0f, + 0.25f,0.5f,0.75f,1.0f, +}; + +static const float type34_delta[10] = { // FIXME: covers 8 entries.. + -1.0f,-0.60947573184967f,-0.333333343267441f,-0.138071194291115f,0.0f, + 0.138071194291115f,0.333333343267441f,0.60947573184967f,1.0f,0.0f, +}; + +} // End of namespace Audio + +#endif diff --git a/audio/decoders/quicktime.cpp b/audio/decoders/quicktime.cpp new file mode 100644 index 0000000000..0f2e76658b --- /dev/null +++ b/audio/decoders/quicktime.cpp @@ -0,0 +1,421 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +#include "common/debug.h" +#include "common/util.h" +#include "common/memstream.h" +#include "common/stream.h" +#include "common/textconsole.h" + +#include "audio/audiostream.h" +#include "audio/decoders/quicktime.h" +#include "audio/decoders/quicktime_intern.h" + +// Codecs +#include "audio/decoders/aac.h" +#include "audio/decoders/adpcm.h" +#include "audio/decoders/qdm2.h" +#include "audio/decoders/raw.h" + +namespace Audio { + +QuickTimeAudioDecoder::QuickTimeAudioDecoder() : Common::QuickTimeParser() { + _audStream = 0; +} + +QuickTimeAudioDecoder::~QuickTimeAudioDecoder() { + delete _audStream; +} + +bool QuickTimeAudioDecoder::loadAudioFile(const Common::String &filename) { + if (!Common::QuickTimeParser::parseFile(filename)) + return false; + + init(); + return true; +} + +bool QuickTimeAudioDecoder::loadAudioStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeFileHandle) { + if (!Common::QuickTimeParser::parseStream(stream, disposeFileHandle)) + return false; + + init(); + return true; +} + +void QuickTimeAudioDecoder::init() { + Common::QuickTimeParser::init(); + + _audioTrackIndex = -1; + + // Find an audio stream + for (uint32 i = 0; i < _tracks.size(); i++) + if (_tracks[i]->codecType == CODEC_TYPE_AUDIO && _audioTrackIndex < 0) + _audioTrackIndex = i; + + // Initialize audio, if present + if (_audioTrackIndex >= 0) { + AudioSampleDesc *entry = (AudioSampleDesc *)_tracks[_audioTrackIndex]->sampleDescs[0]; + + if (entry->isAudioCodecSupported()) { + _audStream = makeQueuingAudioStream(entry->_sampleRate, entry->_channels == 2); + _curAudioChunk = 0; + + // Make sure the bits per sample transfers to the sample size + if (entry->getCodecTag() == MKTAG('r', 'a', 'w', ' ') || entry->getCodecTag() == MKTAG('t', 'w', 'o', 's')) + _tracks[_audioTrackIndex]->sampleSize = (entry->_bitsPerSample / 8) * entry->_channels; + } + } +} + +Common::QuickTimeParser::SampleDesc *QuickTimeAudioDecoder::readSampleDesc(Track *track, uint32 format) { + if (track->codecType == CODEC_TYPE_AUDIO) { + debug(0, "Audio Codec FourCC: \'%s\'", tag2str(format)); + + AudioSampleDesc *entry = new AudioSampleDesc(track, format); + + uint16 stsdVersion = _fd->readUint16BE(); + _fd->readUint16BE(); // revision level + _fd->readUint32BE(); // vendor + + entry->_channels = _fd->readUint16BE(); // channel count + entry->_bitsPerSample = _fd->readUint16BE(); // sample size + + _fd->readUint16BE(); // compression id = 0 + _fd->readUint16BE(); // packet size = 0 + + entry->_sampleRate = (_fd->readUint32BE() >> 16); + + debug(0, "stsd version =%d", stsdVersion); + if (stsdVersion == 0) { + // Not used, except in special cases. See below. + entry->_samplesPerFrame = entry->_bytesPerFrame = 0; + } else if (stsdVersion == 1) { + // Read QT version 1 fields. In version 0 these dont exist. + entry->_samplesPerFrame = _fd->readUint32BE(); + debug(0, "stsd samples_per_frame =%d",entry->_samplesPerFrame); + _fd->readUint32BE(); // bytes per packet + entry->_bytesPerFrame = _fd->readUint32BE(); + debug(0, "stsd bytes_per_frame =%d", entry->_bytesPerFrame); + _fd->readUint32BE(); // bytes per sample + } else { + warning("Unsupported QuickTime STSD audio version %d", stsdVersion); + delete entry; + return 0; + } + + // Version 0 videos (such as the Riven ones) don't have this set, + // but we need it later on. Add it in here. + if (format == MKTAG('i', 'm', 'a', '4')) { + entry->_samplesPerFrame = 64; + entry->_bytesPerFrame = 34 * entry->_channels; + } + + if (entry->_sampleRate == 0 && track->timeScale > 1) + entry->_sampleRate = track->timeScale; + + return entry; + } + + return 0; +} + +bool QuickTimeAudioDecoder::isOldDemuxing() const { + assert(_audioTrackIndex >= 0); + return _tracks[_audioTrackIndex]->timeToSampleCount == 1 && _tracks[_audioTrackIndex]->timeToSample[0].duration == 1; +} + +void QuickTimeAudioDecoder::queueNextAudioChunk() { + AudioSampleDesc *entry = (AudioSampleDesc *)_tracks[_audioTrackIndex]->sampleDescs[0]; + Common::MemoryWriteStreamDynamic *wStream = new Common::MemoryWriteStreamDynamic(); + + _fd->seek(_tracks[_audioTrackIndex]->chunkOffsets[_curAudioChunk]); + + // First, we have to get the sample count + uint32 sampleCount = entry->getAudioChunkSampleCount(_curAudioChunk); + assert(sampleCount); + + if (isOldDemuxing()) { + // Old-style audio demuxing + + // Then calculate the right sizes + while (sampleCount > 0) { + uint32 samples = 0, size = 0; + + if (entry->_samplesPerFrame >= 160) { + samples = entry->_samplesPerFrame; + size = entry->_bytesPerFrame; + } else if (entry->_samplesPerFrame > 1) { + samples = MIN<uint32>((1024 / entry->_samplesPerFrame) * entry->_samplesPerFrame, sampleCount); + size = (samples / entry->_samplesPerFrame) * entry->_bytesPerFrame; + } else { + samples = MIN<uint32>(1024, sampleCount); + size = samples * _tracks[_audioTrackIndex]->sampleSize; + } + + // Now, we read in the data for this data and output it + byte *data = (byte *)malloc(size); + _fd->read(data, size); + wStream->write(data, size); + free(data); + sampleCount -= samples; + } + } else { + // New-style audio demuxing + + // Find our starting sample + uint32 startSample = 0; + for (uint32 i = 0; i < _curAudioChunk; i++) + startSample += entry->getAudioChunkSampleCount(i); + + for (uint32 i = 0; i < sampleCount; i++) { + uint32 size = (_tracks[_audioTrackIndex]->sampleSize != 0) ? _tracks[_audioTrackIndex]->sampleSize : _tracks[_audioTrackIndex]->sampleSizes[i + startSample]; + + // Now, we read in the data for this data and output it + byte *data = (byte *)malloc(size); + _fd->read(data, size); + wStream->write(data, size); + free(data); + } + } + + // Now queue the buffer + _audStream->queueAudioStream(entry->createAudioStream(new Common::MemoryReadStream(wStream->getData(), wStream->size(), DisposeAfterUse::YES))); + delete wStream; + + _curAudioChunk++; +} + +void QuickTimeAudioDecoder::setAudioStreamPos(const Timestamp &where) { + if (!_audStream) + return; + + // Re-create the audio stream + delete _audStream; + Audio::QuickTimeAudioDecoder::AudioSampleDesc *entry = (Audio::QuickTimeAudioDecoder::AudioSampleDesc *)_tracks[_audioTrackIndex]->sampleDescs[0]; + _audStream = Audio::makeQueuingAudioStream(entry->_sampleRate, entry->_channels == 2); + + // First, we need to track down what audio sample we need + Audio::Timestamp curAudioTime = where.convertToFramerate(_tracks[_audioTrackIndex]->timeScale); + uint32 sample = curAudioTime.totalNumberOfFrames(); + uint32 seekSample = sample; + + if (!isOldDemuxing()) { + // We shouldn't have audio samples that are a different duration + // That would be quite bad! + if (_tracks[_audioTrackIndex]->timeToSampleCount != 1) { + warning("Failed seeking"); + return; + } + + // Note that duration is in terms of *one* channel + // This eases calculation a bit + seekSample /= _tracks[_audioTrackIndex]->timeToSample[0].duration; + } + + // Now to track down what chunk it's in + uint32 totalSamples = 0; + _curAudioChunk = 0; + for (uint32 i = 0; i < _tracks[_audioTrackIndex]->chunkCount; i++, _curAudioChunk++) { + uint32 chunkSampleCount = entry->getAudioChunkSampleCount(i); + + if (seekSample < totalSamples + chunkSampleCount) + break; + + totalSamples += chunkSampleCount; + } + + // Reposition the audio stream + queueNextAudioChunk(); + if (sample != totalSamples) { + // HACK: Skip a certain amount of samples from the stream + // (There's got to be a better way to do this!) + int skipSamples = (sample - totalSamples) * entry->_channels; + + int16 *tempBuffer = new int16[skipSamples]; + _audStream->readBuffer(tempBuffer, skipSamples); + delete[] tempBuffer; + } +} + +QuickTimeAudioDecoder::AudioSampleDesc::AudioSampleDesc(Common::QuickTimeParser::Track *parentTrack, uint32 codecTag) : Common::QuickTimeParser::SampleDesc(parentTrack, codecTag) { + _channels = 0; + _sampleRate = 0; + _samplesPerFrame = 0; + _bytesPerFrame = 0; + _bitsPerSample = 0; +} + +bool QuickTimeAudioDecoder::AudioSampleDesc::isAudioCodecSupported() const { + // Check if the codec is a supported codec + if (_codecTag == MKTAG('t', 'w', 'o', 's') || _codecTag == MKTAG('r', 'a', 'w', ' ') || _codecTag == MKTAG('i', 'm', 'a', '4')) + return true; + +#ifdef AUDIO_QDM2_H + if (_codecTag == MKTAG('Q', 'D', 'M', '2')) + return true; +#endif + + if (_codecTag == MKTAG('m', 'p', '4', 'a')) { + Common::String audioType; + switch (_parentTrack->objectTypeMP4) { + case 0x40: // AAC +#ifdef USE_FAAD + return true; +#else + audioType = "AAC"; + break; +#endif + default: + audioType = "Unknown"; + break; + } + warning("No MPEG-4 audio (%s) support", audioType.c_str()); + } else + warning("Audio Codec Not Supported: \'%s\'", tag2str(_codecTag)); + + return false; +} + +uint32 QuickTimeAudioDecoder::AudioSampleDesc::getAudioChunkSampleCount(uint chunk) const { + uint32 sampleCount = 0; + + for (uint32 j = 0; j < _parentTrack->sampleToChunkCount; j++) + if (chunk >= _parentTrack->sampleToChunk[j].first) + sampleCount = _parentTrack->sampleToChunk[j].count; + + return sampleCount; +} + +AudioStream *QuickTimeAudioDecoder::AudioSampleDesc::createAudioStream(Common::SeekableReadStream *stream) const { + if (!stream) + return 0; + + if (_codecTag == MKTAG('t', 'w', 'o', 's') || _codecTag == MKTAG('r', 'a', 'w', ' ')) { + // Fortunately, most of the audio used in Myst videos is raw... + uint16 flags = 0; + if (_codecTag == MKTAG('r', 'a', 'w', ' ')) + flags |= FLAG_UNSIGNED; + if (_channels == 2) + flags |= FLAG_STEREO; + if (_bitsPerSample == 16) + flags |= FLAG_16BITS; + uint32 dataSize = stream->size(); + byte *data = (byte *)malloc(dataSize); + stream->read(data, dataSize); + delete stream; + return makeRawStream(data, dataSize, _sampleRate, flags); + } else if (_codecTag == MKTAG('i', 'm', 'a', '4')) { + // Riven uses this codec (as do some Myst ME videos) + return makeADPCMStream(stream, DisposeAfterUse::YES, stream->size(), kADPCMApple, _sampleRate, _channels, 34); + } else if (_codecTag == MKTAG('m', 'p', '4', 'a')) { + // The 7th Guest iOS uses an MPEG-4 codec +#ifdef USE_FAAD + if (_parentTrack->objectTypeMP4 == 0x40) + return makeAACStream(stream, DisposeAfterUse::YES, _parentTrack->extraData); +#endif +#ifdef AUDIO_QDM2_H + } else if (_codecTag == MKTAG('Q', 'D', 'M', '2')) { + // Myst ME uses this codec for many videos + return makeQDM2Stream(stream, _parentTrack->extraData); +#endif + } + + error("Unsupported audio codec"); + + return NULL; +} + +/** + * A wrapper around QuickTimeAudioDecoder that implements the RewindableAudioStream API + */ +class QuickTimeAudioStream : public SeekableAudioStream, public QuickTimeAudioDecoder { +public: + QuickTimeAudioStream() {} + ~QuickTimeAudioStream() {} + + bool openFromFile(const Common::String &filename) { + return QuickTimeAudioDecoder::loadAudioFile(filename) && _audioTrackIndex >= 0 && _audStream; + } + + bool openFromStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeFileHandle) { + return QuickTimeAudioDecoder::loadAudioStream(stream, disposeFileHandle) && _audioTrackIndex >= 0 && _audStream; + } + + // AudioStream API + int readBuffer(int16 *buffer, const int numSamples) { + int samples = 0; + + while (samples < numSamples && !endOfData()) { + if (_audStream->numQueuedStreams() == 0) + queueNextAudioChunk(); + + samples += _audStream->readBuffer(buffer + samples, numSamples - samples); + } + + return samples; + } + + bool isStereo() const { return _audStream->isStereo(); } + int getRate() const { return _audStream->getRate(); } + bool endOfData() const { return _curAudioChunk >= _tracks[_audioTrackIndex]->chunkCount && _audStream->endOfData(); } + + // SeekableAudioStream API + bool seek(const Timestamp &where) { + if (where > getLength()) + return false; + + setAudioStreamPos(where); + return true; + } + + Timestamp getLength() const { + return Timestamp(0, _tracks[_audioTrackIndex]->duration, _tracks[_audioTrackIndex]->timeScale); + } +}; + +SeekableAudioStream *makeQuickTimeStream(const Common::String &filename) { + QuickTimeAudioStream *audioStream = new QuickTimeAudioStream(); + + if (!audioStream->openFromFile(filename)) { + delete audioStream; + return 0; + } + + return audioStream; +} + +SeekableAudioStream *makeQuickTimeStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse) { + QuickTimeAudioStream *audioStream = new QuickTimeAudioStream(); + + if (!audioStream->openFromStream(stream, disposeAfterUse)) { + delete audioStream; + return 0; + } + + return audioStream; +} + +} // End of namespace Audio diff --git a/audio/decoders/quicktime.h b/audio/decoders/quicktime.h new file mode 100644 index 0000000000..ff81ec9390 --- /dev/null +++ b/audio/decoders/quicktime.h @@ -0,0 +1,70 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +/** + * @file + * Sound decoder used in engines: + * - groovie + * - mohawk + * - sci + */ + +#ifndef AUDIO_QUICKTIME_H +#define AUDIO_QUICKTIME_H + +#include "common/scummsys.h" +#include "common/types.h" + +namespace Common { + class SeekableReadStream; + class String; +} + +namespace Audio { + +class SeekableAudioStream; + +/** + * Try to load a QuickTime sound file from the given file name and create a SeekableAudioStream + * from that data. + * + * @param filename the filename of the file from which to read the data + * @return a new SeekableAudioStream, or NULL, if an error occurred + */ +SeekableAudioStream *makeQuickTimeStream(const Common::String &filename); + +/** + * Try to load a QuickTime sound file from the given seekable stream and create a SeekableAudioStream + * from that data. + * + * @param stream the SeekableReadStream from which to read the data + * @param disposeAfterUse whether to delete the stream after use + * @return a new SeekableAudioStream, or NULL, if an error occurred + */ +SeekableAudioStream *makeQuickTimeStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES); + +} // End of namespace Audio + +#endif diff --git a/audio/decoders/quicktime_intern.h b/audio/decoders/quicktime_intern.h new file mode 100644 index 0000000000..f288d5604b --- /dev/null +++ b/audio/decoders/quicktime_intern.h @@ -0,0 +1,99 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + * + */ + +/** + * Internal interface to the QuickTime audio decoder. + * + * This is available so that the QuickTimeVideoDecoder can use + * this directly. + */ + +#ifndef AUDIO_QUICKTIME_INTERN_H +#define AUDIO_QUICKTIME_INTERN_H + +#include "common/quicktime.h" +#include "common/scummsys.h" +#include "common/types.h" + +namespace Common { + class SeekableReadStream; + class String; +} + +namespace Audio { + +class AudioStream; +class QueuingAudioStream; + +class QuickTimeAudioDecoder : public Common::QuickTimeParser { +public: + QuickTimeAudioDecoder(); + virtual ~QuickTimeAudioDecoder(); + + /** + * Load a QuickTime audio file + * @param filename the filename to load + */ + bool loadAudioFile(const Common::String &filename); + + /** + * Load a QuickTime audio file from a SeekableReadStream + * @param stream the stream to load + */ + bool loadAudioStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeFileHandle); + +protected: + class AudioSampleDesc : public Common::QuickTimeParser::SampleDesc { + public: + AudioSampleDesc(Common::QuickTimeParser::Track *parentTrack, uint32 codecTag); + + bool isAudioCodecSupported() const; + uint32 getAudioChunkSampleCount(uint chunk) const; + AudioStream *createAudioStream(Common::SeekableReadStream *stream) const; + + // TODO: Make private in the long run + uint16 _bitsPerSample; + uint16 _channels; + uint32 _sampleRate; + uint32 _samplesPerFrame; + uint32 _bytesPerFrame; + }; + + // Common::QuickTimeParser API + virtual Common::QuickTimeParser::SampleDesc *readSampleDesc(Track *track, uint32 format); + + void init(); + void setAudioStreamPos(const Timestamp &where); + bool isOldDemuxing() const; + void queueNextAudioChunk(); + + int _audioTrackIndex; + uint _curAudioChunk; + QueuingAudioStream *_audStream; +}; + +} // End of namespace Audio + +#endif diff --git a/audio/fmopl.h b/audio/fmopl.h index fbce36f077..b88325a52e 100644 --- a/audio/fmopl.h +++ b/audio/fmopl.h @@ -23,7 +23,10 @@ #define SOUND_FMOPL_H #include "common/scummsys.h" -#include "common/str.h" + +namespace Common { +class String; +} namespace OPL { diff --git a/audio/mididrv.cpp b/audio/mididrv.cpp index 7beb76352c..5839f5b9d7 100644 --- a/audio/mididrv.cpp +++ b/audio/mididrv.cpp @@ -197,7 +197,7 @@ MidiDriver::DeviceHandle MidiDriver::detectDevice(int flags) { // detected since they are hard coded and cannot be disabled. for (int l = (flags & (MDT_PREFER_GM | MDT_PREFER_MT32)) ? 1 : 0; l < 2; ++l) { if ((flags & MDT_MIDI) && (l == 1)) { - // If a preferred MT32 or GM device has been selected that device gets returned + // If a preferred MT32 or GM device has been selected that device gets returned. if (flags & MDT_PREFER_MT32) hdl = getDeviceHandle(ConfMan.get("mt32_device")); else if (flags & MDT_PREFER_GM) @@ -207,20 +207,20 @@ MidiDriver::DeviceHandle MidiDriver::detectDevice(int flags) { const MusicType type = getMusicType(hdl); - // If have a "Don't use GM/MT-32" setting we skip this part and jump + // If we have a "Don't use GM/MT-32" setting we skip this part and jump // to AdLib, PC Speaker etc. detection right away. if (type != MT_NULL) { if (type != MT_AUTO && type != MT_INVALID) { if (flags & MDT_PREFER_MT32) - // If we have a preferred MT32 device we disable the gm/mt32 mapping (more about this in mididrv.h) + // If we have a preferred MT32 device we disable the gm/mt32 mapping (more about this in mididrv.h). _forceTypeMT32 = true; return hdl; } - // If we have no specific device selected (neither in the scummvm nor in the game domain) - // and no preferred MT32 or GM device selected we arrive here. - // If MT32 is preferred we try for the first available device with music type 'MT_MT32' (usually the mt32 emulator) + // If no specific device is selected (neither in the scummvm nor in the game domain) + // and there is no preferred MT32 or GM device selected either we arrive here. + // If MT32 is preferred we try for the first available device with music type 'MT_MT32' (usually the mt32 emulator). if (flags & MDT_PREFER_MT32) { for (MusicPlugin::List::const_iterator m = p.begin(); m != p.end(); ++m) { MusicDevices i = (**m)->getDevices(); @@ -260,7 +260,7 @@ MidiDriver::DeviceHandle MidiDriver::detectDevice(int flags) { else if (flags & MDT_APPLEIIGS) tp = MT_APPLEIIGS; else if (l == 0) - // if we haven't tried to find a MIDI device yet we do this now. + // If we haven't tried to find a MIDI device yet we do this now. continue; else tp = MT_AUTO; @@ -292,7 +292,7 @@ MidiDriver::DeviceHandle MidiDriver::getDeviceHandle(const Common::String &ident const MusicPlugin::List p = MusicMan.getPlugins(); if (p.begin() == p.end()) - error("Music plugins must be loaded prior to calling this method"); + error("MidiDriver::getDeviceHandle: Music plugins must be loaded prior to calling this method"); for (MusicPlugin::List::const_iterator m = p.begin(); m != p.end(); m++) { MusicDevices i = (**m)->getDevices(); diff --git a/audio/midiparser_xmidi.cpp b/audio/midiparser_xmidi.cpp index 84e1aa2ec7..7c3cf102d1 100644 --- a/audio/midiparser_xmidi.cpp +++ b/audio/midiparser_xmidi.cpp @@ -237,7 +237,7 @@ bool MidiParser_XMIDI::loadMusic(byte *data, uint32 size) { pos += 4; _num_tracks = 1; } else if (memcmp(pos, "XDIR", 4)) { - // Not an XMIDI that we recognise + // Not an XMIDI that we recognize warning("Expected 'XDIR' but found '%c%c%c%c'", pos[0], pos[1], pos[2], pos[3]); return false; } else { diff --git a/audio/mods/tfmx.cpp b/audio/mods/tfmx.cpp index afc7d3b40b..a89da78af1 100644 --- a/audio/mods/tfmx.cpp +++ b/audio/mods/tfmx.cpp @@ -332,7 +332,7 @@ void Tfmx::macroRun(ChannelContext &channel) { channel.vibLength = macroPtr[1]; channel.vibCount = macroPtr[1] / 2; channel.vibDelta = macroPtr[3]; - // TODO: Perhaps a bug, vibValue could be left uninitialised + // TODO: Perhaps a bug, vibValue could be left uninitialized if (!channel.portaDelta) { channel.period = channel.refPeriod; channel.vibValue = 0; @@ -700,7 +700,7 @@ void Tfmx::noteCommand(const uint8 note, const uint8 param1, const uint8 param2, channel.relVol = param2 >> 4; channel.fineTune = (int8)param3; - // TODO: the point where the channel gets initialised varies with the games, needs more research. + // TODO: the point where the channel gets initialized varies with the games, needs more research. initMacroProgramm(channel); channel.keyUp = false; // key down = playing a Note diff --git a/audio/module.mk b/audio/module.mk index 840b6d6692..46cb9944e1 100644 --- a/audio/module.mk +++ b/audio/module.mk @@ -13,12 +13,15 @@ MODULE_OBJS := \ musicplugin.o \ null.o \ timestamp.o \ + decoders/aac.o \ decoders/adpcm.o \ decoders/aiff.o \ decoders/flac.o \ decoders/iff_sound.o \ decoders/mac_snd.o \ decoders/mp3.o \ + decoders/qdm2.o \ + decoders/quicktime.o \ decoders/raw.o \ decoders/vag.o \ decoders/voc.o \ diff --git a/audio/softsynth/adlib.cpp b/audio/softsynth/adlib.cpp index 60de8fad60..4025a667ac 100644 --- a/audio/softsynth/adlib.cpp +++ b/audio/softsynth/adlib.cpp @@ -857,6 +857,7 @@ void AdLibPercussionChannel::sysEx_customInstrument(uint32 type, const byte *ins // Allocate memory for the new instruments if (!_customInstruments[note]) { _customInstruments[note] = new AdLibInstrument; + memset(_customInstruments[note], 0, sizeof(AdLibInstrument)); } // Save the new instrument data diff --git a/audio/softsynth/fmtowns_pc98/towns_audio.cpp b/audio/softsynth/fmtowns_pc98/towns_audio.cpp index 719bc981ee..beee5f1cad 100644 --- a/audio/softsynth/fmtowns_pc98/towns_audio.cpp +++ b/audio/softsynth/fmtowns_pc98/towns_audio.cpp @@ -28,87 +28,117 @@ #include "common/textconsole.h" #include "backends/audiocd/audiocd.h" +class TownsAudio_WaveTable { +friend class TownsAudioInterfaceInternal; +friend class TownsAudio_PcmChannel; +public: + TownsAudio_WaveTable(); + ~TownsAudio_WaveTable(); + +private: + void readHeader(const uint8 *buffer); + void readData(const uint8 *buffer); + void clear(); + + char name[9]; + int32 id; + uint32 size; + uint32 loopStart; + uint32 loopLen; + uint16 rate; + uint16 rateOffs; + uint16 baseNote; + int8 *data; +}; class TownsAudio_PcmChannel { -friend class TownsAudioInterfaceIntern; public: TownsAudio_PcmChannel(); ~TownsAudio_PcmChannel(); -private: - void loadExtData(uint8 *buffer, uint32 size); - void setupLoop(uint32 start, uint32 len); void clear(); + void loadData(TownsAudio_WaveTable *w); + void loadData(uint8 *buffer, uint32 size); + + int initInstrument(uint8 ¬e, TownsAudio_WaveTable *&tables, int numTables); + void keyOn(uint8 note, uint8 velo, TownsAudio_WaveTable *w); + void keyOff(); + + void updateEnvelopeGenerator(); + + void setInstrument(uint8 *instr); + void setLevel(uint8 lvl); + void setPitch(uint32 pt); + void setBalance(uint8 blc); + + void updateOutput(); + int32 currentSampleLeft(); + int32 currentSampleRight(); + + bool _keyPressed; + bool _reserved; + bool _activeKey; + bool _activeEffect; + bool _activeOutput; + +private: + void setupLoop(uint32 loopStart, uint32 len); + void setNote(uint8 note, TownsAudio_WaveTable *w, bool stepLimit = false); + void setVelo(uint8 velo); + void envAttack(); void envDecay(); void envSustain(); void envRelease(); - uint8 *curInstrument; - uint8 note; - uint8 velo; + uint8 *_curInstrument; - int8 *data; - int8 *dataEnd; + uint8 _note; - int8 *loopEnd; - uint32 loopLen; + uint8 _velo; + uint8 _level; + uint8 _tl; - uint16 stepNote; - uint16 stepPitch; - uint16 step; + uint8 _panLeft; + uint8 _panRight; - uint8 panLeft; - uint8 panRight; + int8 *_data; + int8 *_dataEnd; - uint32 pos; + int8 *_loopEnd; + uint32 _loopLen; - uint8 envTotalLevel; - uint8 envAttackRate; - uint8 envDecayRate; - uint8 envSustainLevel; - uint8 envSustainRate; - uint8 envReleaseRate; + uint16 _stepNote; + uint16 _stepPitch; + uint16 _step; - int16 envStep; - int16 envCurrentLevel; + uint32 _pos; - EnvelopeState envState; + uint8 _envTotalLevel; + uint8 _envAttackRate; + uint8 _envDecayRate; + uint8 _envSustainLevel; + uint8 _envSustainRate; + uint8 _envReleaseRate; + int16 _envStep; + int16 _envCurrentLevel; - int8 *extData; -}; + EnvelopeState _envState; -class TownsAudio_WaveTable { -friend class TownsAudioInterfaceIntern; -public: - TownsAudio_WaveTable(); - ~TownsAudio_WaveTable(); - -private: - void readHeader(const uint8 *buffer); - void readData(const uint8 *buffer); - void clear(); + int8 *_extData; - char name[9]; - int32 id; - uint32 size; - uint32 loopStart; - uint32 loopLen; - uint16 rate; - uint16 rateOffs; - uint16 baseNote; - int8 *data; + static const uint16 _pcmPhase1[]; + static const uint16 _pcmPhase2[]; }; -class TownsAudioInterfaceIntern : public TownsPC98_FmSynth { +class TownsAudioInterfaceInternal : public TownsPC98_FmSynth { public: - TownsAudioInterfaceIntern(Audio::Mixer *mixer, TownsAudioInterfacePluginDriver *driver); - ~TownsAudioInterfaceIntern(); + TownsAudioInterfaceInternal(Audio::Mixer *mixer, TownsAudioInterface *owner, TownsAudioInterfacePluginDriver *driver, bool externalMutexHandling = false); + ~TownsAudioInterfaceInternal(); - static TownsAudioInterfaceIntern *addNewRef(Audio::Mixer *mixer, TownsAudioInterfacePluginDriver *driver); - static void releaseRef(); - bool checkPluginDriver(TownsAudioInterfacePluginDriver *driver); + static TownsAudioInterfaceInternal *addNewRef(Audio::Mixer *mixer, TownsAudioInterface *owner, TownsAudioInterfacePluginDriver *driver, bool externalMutexHandling = false); + static void releaseRef(TownsAudioInterface *owner); bool init(); @@ -122,12 +152,15 @@ public: void setSoundEffectChanMask(int mask); private: + bool assignPluginDriver(TownsAudioInterface *owner, TownsAudioInterfacePluginDriver *driver, bool externalMutexHandling = false); + void removePluginDriver(TownsAudioInterface *owner); + void nextTickEx(int32 *buffer, uint32 bufferSize); void timerCallbackA(); void timerCallbackB(); - typedef int (TownsAudioInterfaceIntern::*TownsAudioIntfCallback)(va_list &); + typedef int (TownsAudioInterfaceInternal::*TownsAudioIntfCallback)(va_list &); const TownsAudioIntfCallback *_intfOpcodes; int intf_reset(va_list &args); @@ -200,18 +233,8 @@ private: int pcmLoadInstrument(int instrId, const uint8 *data); int pcmSetPitch(int chan, int pitch); int pcmSetLevel(int chan, int lvl); - void pcmUpdateEnvelopeGenerator(int chan); TownsAudio_PcmChannel *_pcmChan; - uint8 _pcmChanOut; - uint8 _pcmChanReserved; - uint8 _pcmChanKeyPressed; - uint8 _pcmChanEffectPlaying; - uint8 _pcmChanKeyPlaying; - - uint8 _pcmChanNote[8]; - uint8 _pcmChanVelo[8]; - uint8 _pcmChanLevel[8]; uint8 _numReservedChannels; uint8 *_pcmInstruments; @@ -220,12 +243,12 @@ private: uint8 _numWaveTables; uint32 _waveTablesTotalDataSize; - void pcmCalcPhaseStep(TownsAudio_PcmChannel *p, TownsAudio_WaveTable *w); - void updateOutputVolume(); + void updateOutputVolumeInternal(); uint8 _outputVolumeFlags; uint8 _outputLevel[16]; uint8 _outputMute[16]; + bool _updateOutputVol; const float _baserate; uint32 _timerBase; @@ -237,28 +260,27 @@ private: int _pcmSfxChanMask; TownsAudioInterfacePluginDriver *_drv; + void *_drvOwner; bool _ready; - static TownsAudioInterfaceIntern *_refInstance; + static TownsAudioInterfaceInternal *_refInstance; static int _refCount; static const uint8 _chanFlags[]; static const uint16 _frequency[]; static const uint8 _carrier[]; static const uint8 _fmDefaultInstrument[]; - static const uint16 _pcmPhase1[]; - static const uint16 _pcmPhase2[]; }; -TownsAudioInterfaceIntern::TownsAudioInterfaceIntern(Audio::Mixer *mixer, TownsAudioInterfacePluginDriver *driver) : TownsPC98_FmSynth(mixer, kTypeTowns), +TownsAudioInterfaceInternal::TownsAudioInterfaceInternal(Audio::Mixer *mixer, TownsAudioInterface *owner, TownsAudioInterfacePluginDriver *driver, bool externalMutexHandling) : + TownsPC98_FmSynth(mixer, kTypeTowns, externalMutexHandling), _fmInstruments(0), _pcmInstruments(0), _pcmChan(0), _waveTables(0), _waveTablesTotalDataSize(0), - _baserate(55125.0f / (float)mixer->getOutputRate()), _tickLength(0), _timer(0), _drv(driver), + _baserate(55125.0f / (float)mixer->getOutputRate()), _tickLength(0), _timer(0), _drv(driver), _drvOwner(owner), _pcmSfxChanMask(0), _musicVolume(Audio::Mixer::kMaxMixerVolume), _sfxVolume(Audio::Mixer::kMaxMixerVolume), - _outputVolumeFlags(0), _pcmChanOut(0), _pcmChanReserved(0), _pcmChanKeyPressed(0), - _pcmChanEffectPlaying(0), _pcmChanKeyPlaying(0), _fmChanPlaying(0), - _numReservedChannels(0), _numWaveTables(0), _ready(false) { + _outputVolumeFlags(0), _fmChanPlaying(0), + _numReservedChannels(0), _numWaveTables(0), _updateOutputVol(false), _ready(false) { -#define INTCB(x) &TownsAudioInterfaceIntern::intf_##x +#define INTCB(x) &TownsAudioInterfaceInternal::intf_##x static const TownsAudioIntfCallback intfCb[] = { // 0 INTCB(reset), @@ -371,9 +393,6 @@ TownsAudioInterfaceIntern::TownsAudioInterfaceIntern(Audio::Mixer *mixer, TownsA memset(_fmSaveReg, 0, sizeof(_fmSaveReg)); memset(_fmChanNote, 0, sizeof(_fmChanNote)); memset(_fmChanPitch, 0, sizeof(_fmChanPitch)); - memset(_pcmChanNote, 0, sizeof(_pcmChanNote)); - memset(_pcmChanVelo, 0, sizeof(_pcmChanVelo)); - memset(_pcmChanLevel, 0, sizeof(_pcmChanLevel)); memset(_outputLevel, 0, sizeof(_outputLevel)); memset(_outputMute, 0, sizeof(_outputMute)); @@ -381,10 +400,12 @@ TownsAudioInterfaceIntern::TownsAudioInterfaceIntern(Audio::Mixer *mixer, TownsA _tickLength = 2 * _timerBase; } -TownsAudioInterfaceIntern::~TownsAudioInterfaceIntern() { +TownsAudioInterfaceInternal::~TownsAudioInterfaceInternal() { _ready = false; deinit(); + Common::StackLock lock(_mutex); + delete[] _fmSaveReg[0]; delete[] _fmSaveReg[1]; delete[] _fmInstruments; @@ -393,47 +414,34 @@ TownsAudioInterfaceIntern::~TownsAudioInterfaceIntern() { delete[] _pcmChan; } -TownsAudioInterfaceIntern *TownsAudioInterfaceIntern::addNewRef(Audio::Mixer *mixer, TownsAudioInterfacePluginDriver *driver) { +TownsAudioInterfaceInternal *TownsAudioInterfaceInternal::addNewRef(Audio::Mixer *mixer, TownsAudioInterface *owner, TownsAudioInterfacePluginDriver *driver, bool externalMutexHandling) { _refCount++; if (_refCount == 1 && _refInstance == 0) - _refInstance = new TownsAudioInterfaceIntern(mixer, driver); + _refInstance = new TownsAudioInterfaceInternal(mixer, owner, driver, externalMutexHandling); else if (_refCount < 2 || _refInstance == 0) - error("TownsAudioInterfaceIntern::addNewRef(): Internal reference management failure"); - else if (!_refInstance->checkPluginDriver(driver)) - error("TownsAudioInterfaceIntern::addNewRef(): Plugin driver conflict"); + error("TownsAudioInterfaceInternal::addNewRef(): Internal reference management failure"); + else if (!_refInstance->assignPluginDriver(owner, driver, externalMutexHandling)) + error("TownsAudioInterfaceInternal::addNewRef(): Plugin driver conflict"); return _refInstance; } -void TownsAudioInterfaceIntern::releaseRef() { +void TownsAudioInterfaceInternal::releaseRef(TownsAudioInterface *owner) { if (!_refCount) return; _refCount--; - if (!_refCount) { + if (_refCount) { + if (_refInstance) + _refInstance->removePluginDriver(owner); + } else { delete _refInstance; _refInstance = 0; } } -bool TownsAudioInterfaceIntern::checkPluginDriver(TownsAudioInterfacePluginDriver *driver) { - if (_refCount <= 1) - return true; - - if (_drv) { - if (driver && driver != _drv) - return false; - } else { - lock(); - _drv = driver; - unlock(); - } - - return true; -} - -bool TownsAudioInterfaceIntern::init() { +bool TownsAudioInterfaceInternal::init() { if (_ready) return true; @@ -457,7 +465,7 @@ bool TownsAudioInterfaceIntern::init() { return true; } -int TownsAudioInterfaceIntern::callback(int command, ...) { +int TownsAudioInterfaceInternal::callback(int command, ...) { if (!_ready) return 1; @@ -470,79 +478,95 @@ int TownsAudioInterfaceIntern::callback(int command, ...) { return res; } -int TownsAudioInterfaceIntern::processCommand(int command, va_list &args) { +int TownsAudioInterfaceInternal::processCommand(int command, va_list &args) { if (!_ready) return 1; if (command < 0 || command > 81) return 4; - lock(); + Common::StackLock lock(_mutex); int res = (this->*_intfOpcodes[command])(args); - unlock(); return res; } -void TownsAudioInterfaceIntern::setMusicVolume(int volume) { +void TownsAudioInterfaceInternal::setMusicVolume(int volume) { _musicVolume = CLIP<uint16>(volume, 0, Audio::Mixer::kMaxMixerVolume); setVolumeIntern(_musicVolume, _sfxVolume); } -void TownsAudioInterfaceIntern::setSoundEffectVolume(int volume) { +void TownsAudioInterfaceInternal::setSoundEffectVolume(int volume) { _sfxVolume = CLIP<uint16>(volume, 0, Audio::Mixer::kMaxMixerVolume); setVolumeIntern(_musicVolume, _sfxVolume); } -void TownsAudioInterfaceIntern::setSoundEffectChanMask(int mask) { +void TownsAudioInterfaceInternal::setSoundEffectChanMask(int mask) { _pcmSfxChanMask = mask >> 6; mask &= 0x3f; setVolumeChannelMasks(~mask, mask); } -void TownsAudioInterfaceIntern::nextTickEx(int32 *buffer, uint32 bufferSize) { +bool TownsAudioInterfaceInternal::assignPluginDriver(TownsAudioInterface *owner, TownsAudioInterfacePluginDriver *driver, bool externalMutexHandling) { + if (_refCount <= 1) + return true; + + if (_drv) { + if (driver && driver != _drv) + return false; + } else { + Common::StackLock lock(_mutex); + _drv = driver; + _drvOwner = owner; + _externalMutex = externalMutexHandling; + } + + return true; +} + +void TownsAudioInterfaceInternal::removePluginDriver(TownsAudioInterface *owner) { + if (_drvOwner == owner) { + Common::StackLock lock(_mutex); + _drv = 0; + } +} + +void TownsAudioInterfaceInternal::nextTickEx(int32 *buffer, uint32 bufferSize) { if (!_ready) return; + if (_updateOutputVol) + updateOutputVolumeInternal(); + for (uint32 i = 0; i < bufferSize; i++) { _timer += _tickLength; while (_timer > 0x514767) { _timer -= 0x514767; - for (int ii = 0; ii < 8; ii++) { - if ((_pcmChanKeyPlaying & _chanFlags[ii]) || (_pcmChanEffectPlaying & _chanFlags[ii])) { - TownsAudio_PcmChannel *s = &_pcmChan[ii]; - s->pos += s->step; - - if (&s->data[s->pos >> 11] >= s->loopEnd) { - if (s->loopLen) { - s->pos -= s->loopLen; - } else { - s->pos = 0; - _pcmChanEffectPlaying &= ~_chanFlags[ii]; - _pcmChanKeyPlaying &= ~_chanFlags[ii]; - } - } - } - } + for (int ii = 0; ii < 8; ii++) + _pcmChan[ii].updateOutput(); } int32 finOutL = 0; int32 finOutR = 0; for (int ii = 0; ii < 8; ii++) { - if (_pcmChanOut & _chanFlags[ii]) { - int32 o = _pcmChan[ii].data[_pcmChan[ii].pos >> 11] * _pcmChan[ii].velo; - if ((1 << ii) & (~_pcmSfxChanMask)) - o = (o * _musicVolume) / Audio::Mixer::kMaxMixerVolume; - if ((1 << ii) & _pcmSfxChanMask) - o = (o * _sfxVolume) / Audio::Mixer::kMaxMixerVolume; - if (_pcmChan[ii].panLeft) - finOutL += ((o * _pcmChan[ii].panLeft) >> 3); - if (_pcmChan[ii].panRight) - finOutR += ((o * _pcmChan[ii].panRight) >> 3); - if (!((_pcmChanKeyPlaying & _chanFlags[ii]) || (_pcmChanEffectPlaying & _chanFlags[ii]))) - _pcmChanOut &= ~_chanFlags[ii]; + if (_pcmChan[ii]._activeOutput) { + int32 oL = _pcmChan[ii].currentSampleLeft(); + int32 oR = _pcmChan[ii].currentSampleRight(); + if ((1 << ii) & (~_pcmSfxChanMask)) { + oL = (oR * _musicVolume) / Audio::Mixer::kMaxMixerVolume; + oR = (oR * _musicVolume) / Audio::Mixer::kMaxMixerVolume; + } + if ((1 << ii) & _pcmSfxChanMask) { + oL = (oL * _sfxVolume) / Audio::Mixer::kMaxMixerVolume; + oR = (oR * _sfxVolume) / Audio::Mixer::kMaxMixerVolume; + } + finOutL += oL; + finOutR += oR; + + if (!(_pcmChan[ii]._activeKey || _pcmChan[ii]._activeEffect)) + _pcmChan[ii]._activeOutput = false; } } @@ -551,12 +575,12 @@ void TownsAudioInterfaceIntern::nextTickEx(int32 *buffer, uint32 bufferSize) { } } -void TownsAudioInterfaceIntern::timerCallbackA() { +void TownsAudioInterfaceInternal::timerCallbackA() { if (_drv && _ready) _drv->timerCallback(0); } -void TownsAudioInterfaceIntern::timerCallbackB() { +void TownsAudioInterfaceInternal::timerCallbackB() { if (_ready) { if (_drv) _drv->timerCallback(1); @@ -564,62 +588,62 @@ void TownsAudioInterfaceIntern::timerCallbackB() { } } -int TownsAudioInterfaceIntern::intf_reset(va_list &args) { +int TownsAudioInterfaceInternal::intf_reset(va_list &args) { fmReset(); pcmReset(); callback(68); return 0; } -int TownsAudioInterfaceIntern::intf_keyOn(va_list &args) { +int TownsAudioInterfaceInternal::intf_keyOn(va_list &args) { int chan = va_arg(args, int); int note = va_arg(args, int); int velo = va_arg(args, int); return (chan & 0x40) ? pcmKeyOn(chan, note, velo) : fmKeyOn(chan, note, velo); } -int TownsAudioInterfaceIntern::intf_keyOff(va_list &args) { +int TownsAudioInterfaceInternal::intf_keyOff(va_list &args) { int chan = va_arg(args, int); return (chan & 0x40) ? pcmKeyOff(chan) : fmKeyOff(chan); } -int TownsAudioInterfaceIntern::intf_setPanPos(va_list &args) { +int TownsAudioInterfaceInternal::intf_setPanPos(va_list &args) { int chan = va_arg(args, int); int mode = va_arg(args, int); return (chan & 0x40) ? pcmSetPanPos(chan, mode) : fmSetPanPos(chan, mode); } -int TownsAudioInterfaceIntern::intf_setInstrument(va_list &args) { +int TownsAudioInterfaceInternal::intf_setInstrument(va_list &args) { int chan = va_arg(args, int); int instrId = va_arg(args, int); return (chan & 0x40) ? pcmSetInstrument(chan, instrId) : fmSetInstrument(chan, instrId); } -int TownsAudioInterfaceIntern::intf_loadInstrument(va_list &args) { +int TownsAudioInterfaceInternal::intf_loadInstrument(va_list &args) { int chanType = va_arg(args, int); int instrId = va_arg(args, int); uint8 *instrData = va_arg(args, uint8 *); return (chanType & 0x40) ? pcmLoadInstrument(instrId, instrData) : fmLoadInstrument(instrId, instrData); } -int TownsAudioInterfaceIntern::intf_setPitch(va_list &args) { +int TownsAudioInterfaceInternal::intf_setPitch(va_list &args) { int chan = va_arg(args, int); int16 pitch = (int16)(va_arg(args, int) & 0xffff); return (chan & 0x40) ? pcmSetPitch(chan, pitch) : fmSetPitch(chan, pitch); } -int TownsAudioInterfaceIntern::intf_setLevel(va_list &args) { +int TownsAudioInterfaceInternal::intf_setLevel(va_list &args) { int chan = va_arg(args, int); int lvl = va_arg(args, int); return (chan & 0x40) ? pcmSetLevel(chan, lvl) : fmSetLevel(chan, lvl); } -int TownsAudioInterfaceIntern::intf_chanOff(va_list &args) { +int TownsAudioInterfaceInternal::intf_chanOff(va_list &args) { int chan = va_arg(args, int); return (chan & 0x40) ? pcmChanOff(chan) : fmChanOff(chan); } -int TownsAudioInterfaceIntern::intf_writeReg(va_list &args) { +int TownsAudioInterfaceInternal::intf_writeReg(va_list &args) { int part = va_arg(args, int) ? 1 : 0; int reg = va_arg(args, int); int val = va_arg(args, int); @@ -630,7 +654,7 @@ int TownsAudioInterfaceIntern::intf_writeReg(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_writeRegBuffer(va_list &args) { +int TownsAudioInterfaceInternal::intf_writeRegBuffer(va_list &args) { int part = va_arg(args, int) ? 1 : 0; int reg = va_arg(args, int); int val = va_arg(args, int); @@ -642,7 +666,7 @@ int TownsAudioInterfaceIntern::intf_writeRegBuffer(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_readRegBuffer(va_list &args) { +int TownsAudioInterfaceInternal::intf_readRegBuffer(va_list &args) { int part = va_arg(args, int) ? 1 : 0; int reg = va_arg(args, int); uint8 *dst = va_arg(args, uint8 *); @@ -655,7 +679,7 @@ int TownsAudioInterfaceIntern::intf_readRegBuffer(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_setTimerA(va_list &args) { +int TownsAudioInterfaceInternal::intf_setTimerA(va_list &args) { int enable = va_arg(args, int); int tempo = va_arg(args, int); @@ -670,7 +694,7 @@ int TownsAudioInterfaceIntern::intf_setTimerA(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_setTimerB(va_list &args) { +int TownsAudioInterfaceInternal::intf_setTimerB(va_list &args) { int enable = va_arg(args, int); int tempo = va_arg(args, int); @@ -684,17 +708,17 @@ int TownsAudioInterfaceIntern::intf_setTimerB(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_enableTimerA(va_list &args) { +int TownsAudioInterfaceInternal::intf_enableTimerA(va_list &args) { bufferedWriteReg(0, 0x27, _fmSaveReg[0][0x27] | 0x15); return 0; } -int TownsAudioInterfaceIntern::intf_enableTimerB(va_list &args) { +int TownsAudioInterfaceInternal::intf_enableTimerB(va_list &args) { bufferedWriteReg(0, 0x27, _fmSaveReg[0][0x27] | 0x2a); return 0; } -int TownsAudioInterfaceIntern::intf_loadSamples(va_list &args) { +int TownsAudioInterfaceInternal::intf_loadSamples(va_list &args) { uint32 dest = va_arg(args, uint32); int size = va_arg(args, int); uint8 *src = va_arg(args, uint8*); @@ -717,7 +741,7 @@ int TownsAudioInterfaceIntern::intf_loadSamples(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_reserveEffectChannels(va_list &args) { +int TownsAudioInterfaceInternal::intf_reserveEffectChannels(va_list &args) { int numChan = va_arg(args, int); if (numChan > 8) return 3; @@ -729,27 +753,24 @@ int TownsAudioInterfaceIntern::intf_reserveEffectChannels(va_list &args) { if (numChan < _numReservedChannels) { int c = 8 - _numReservedChannels; - for (int i = numChan; i; i--) { - uint8 f = ~_chanFlags[c--]; - _pcmChanEffectPlaying &= f; - } + for (int i = numChan; i; i--) + _pcmChan[c--]._activeEffect = false; } else { int c = 7 - _numReservedChannels; for (int i = numChan - _numReservedChannels; i; i--) { - uint8 f = ~_chanFlags[c--]; - _pcmChanKeyPressed &= f; - _pcmChanKeyPlaying &= f; + _pcmChan[c]._keyPressed = false; + _pcmChan[c--]._activeKey = false; } } - static const uint8 reserveChanFlags[] = { 0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE, 0xFF }; _numReservedChannels = numChan; - _pcmChanReserved = reserveChanFlags[_numReservedChannels]; + for (int i = 0; i < 8; i++) + _pcmChan[i]._reserved = i >= (8 - _numReservedChannels) ? true : false; return 0; } -int TownsAudioInterfaceIntern::intf_loadWaveTable(va_list &args) { +int TownsAudioInterfaceInternal::intf_loadWaveTable(va_list &args) { uint8 *data = va_arg(args, uint8 *); if (_numWaveTables > 127) return 3; @@ -776,7 +797,7 @@ int TownsAudioInterfaceIntern::intf_loadWaveTable(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_unloadWaveTable(va_list &args) { +int TownsAudioInterfaceInternal::intf_unloadWaveTable(va_list &args) { int id = va_arg(args, int); if (id == -1) { @@ -803,7 +824,7 @@ int TownsAudioInterfaceIntern::intf_unloadWaveTable(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_pcmPlayEffect(va_list &args) { +int TownsAudioInterfaceInternal::intf_pcmPlayEffect(va_list &args) { int chan = va_arg(args, int); int note = va_arg(args, int); int velo = va_arg(args, int); @@ -817,10 +838,10 @@ int TownsAudioInterfaceIntern::intf_pcmPlayEffect(va_list &args) { chan -= 0x40; - if (!(_pcmChanReserved & _chanFlags[chan])) + if (!_pcmChan[chan]._reserved) return 7; - if ((_pcmChanEffectPlaying & _chanFlags[chan])) + if (_pcmChan[chan]._activeEffect) return 2; TownsAudio_WaveTable w; @@ -834,87 +855,74 @@ int TownsAudioInterfaceIntern::intf_pcmPlayEffect(va_list &args) { TownsAudio_PcmChannel *p = &_pcmChan[chan]; - _pcmChanNote[chan] = note; - _pcmChanVelo[chan] = velo; - - p->note = note; - p->velo = velo << 1; - - p->loadExtData(data + 32, w.size); - p->setupLoop(w.loopStart, w.loopLen); - - pcmCalcPhaseStep(p, &w); - if (p->step > 2048) - p->step = 2048; - - _pcmChanEffectPlaying |= _chanFlags[chan]; - _pcmChanOut |= _chanFlags[chan]; + p->loadData(data + 32, w.size); + p->keyOn(note, velo, &w); return 0; } -int TownsAudioInterfaceIntern::intf_pcmChanOff(va_list &args) { +int TownsAudioInterfaceInternal::intf_pcmChanOff(va_list &args) { int chan = va_arg(args, int); pcmChanOff(chan); return 0; } -int TownsAudioInterfaceIntern::intf_pcmEffectPlaying(va_list &args) { +int TownsAudioInterfaceInternal::intf_pcmEffectPlaying(va_list &args) { int chan = va_arg(args, int); if (chan < 0x40 || chan > 0x47) return 1; chan -= 0x40; - return (_pcmChanEffectPlaying & _chanFlags[chan]) ? 1 : 0; + return _pcmChan[chan]._activeEffect ? 1 : 0; } -int TownsAudioInterfaceIntern::intf_fmKeyOn(va_list &args) { +int TownsAudioInterfaceInternal::intf_fmKeyOn(va_list &args) { int chan = va_arg(args, int); int note = va_arg(args, int); int velo = va_arg(args, int); return fmKeyOn(chan, note, velo); } -int TownsAudioInterfaceIntern::intf_fmKeyOff(va_list &args) { +int TownsAudioInterfaceInternal::intf_fmKeyOff(va_list &args) { int chan = va_arg(args, int); return fmKeyOff(chan); } -int TownsAudioInterfaceIntern::intf_fmSetPanPos(va_list &args) { +int TownsAudioInterfaceInternal::intf_fmSetPanPos(va_list &args) { int chan = va_arg(args, int); int mode = va_arg(args, int); return fmSetPanPos(chan, mode); } -int TownsAudioInterfaceIntern::intf_fmSetInstrument(va_list &args) { +int TownsAudioInterfaceInternal::intf_fmSetInstrument(va_list &args) { int chan = va_arg(args, int); int instrId = va_arg(args, int); return fmSetInstrument(chan, instrId); } -int TownsAudioInterfaceIntern::intf_fmLoadInstrument(va_list &args) { +int TownsAudioInterfaceInternal::intf_fmLoadInstrument(va_list &args) { int instrId = va_arg(args, int); uint8 *instrData = va_arg(args, uint8 *); return fmLoadInstrument(instrId, instrData); } -int TownsAudioInterfaceIntern::intf_fmSetPitch(va_list &args) { +int TownsAudioInterfaceInternal::intf_fmSetPitch(va_list &args) { int chan = va_arg(args, int); uint16 freq = va_arg(args, int) & 0xffff; return fmSetPitch(chan, freq); } -int TownsAudioInterfaceIntern::intf_fmSetLevel(va_list &args) { +int TownsAudioInterfaceInternal::intf_fmSetLevel(va_list &args) { int chan = va_arg(args, int); int lvl = va_arg(args, int); return fmSetLevel(chan, lvl); } -int TownsAudioInterfaceIntern::intf_fmReset(va_list &args) { +int TownsAudioInterfaceInternal::intf_fmReset(va_list &args) { fmReset(); return 0; } -int TownsAudioInterfaceIntern::intf_setOutputVolume(va_list &args) { +int TownsAudioInterfaceInternal::intf_setOutputVolume(va_list &args) { int chanType = va_arg(args, int); int left = va_arg(args, int); int right = va_arg(args, int); @@ -951,14 +959,14 @@ int TownsAudioInterfaceIntern::intf_setOutputVolume(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_resetOutputVolume(va_list &args) { +int TownsAudioInterfaceInternal::intf_resetOutputVolume(va_list &args) { memset(_outputLevel, 0, sizeof(_outputLevel)); _outputVolumeFlags = 0; updateOutputVolume(); return 0; } -int TownsAudioInterfaceIntern::intf_getOutputVolume(va_list &args) { +int TownsAudioInterfaceInternal::intf_getOutputVolume(va_list &args) { int chanType = va_arg(args, int); int *left = va_arg(args, int*); int *right = va_arg(args, int*); @@ -978,7 +986,7 @@ int TownsAudioInterfaceIntern::intf_getOutputVolume(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_setOutputMute(va_list &args) { +int TownsAudioInterfaceInternal::intf_setOutputMute(va_list &args) { int flags = va_arg(args, int); _outputVolumeFlags = flags; uint8 mute = flags & 3; @@ -1007,31 +1015,31 @@ int TownsAudioInterfaceIntern::intf_setOutputMute(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_cdaToggle(va_list &args) { +int TownsAudioInterfaceInternal::intf_cdaToggle(va_list &args) { //int mode = va_arg(args, int); //_unkMask = mode ? 0x7f : 0x3f; return 0; } -int TownsAudioInterfaceIntern::intf_getOutputVolume2(va_list &args) { +int TownsAudioInterfaceInternal::intf_getOutputVolume2(va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_getOutputMute (va_list &args) { +int TownsAudioInterfaceInternal::intf_getOutputMute (va_list &args) { return 0; } -int TownsAudioInterfaceIntern::intf_pcmUpdateEnvelopeGenerator(va_list &args) { +int TownsAudioInterfaceInternal::intf_pcmUpdateEnvelopeGenerator(va_list &args) { for (int i = 0; i < 8; i++) - pcmUpdateEnvelopeGenerator(i); + _pcmChan[i].updateEnvelopeGenerator(); return 0; } -int TownsAudioInterfaceIntern::intf_notImpl(va_list &args) { +int TownsAudioInterfaceInternal::intf_notImpl(va_list &args) { return 4; } -void TownsAudioInterfaceIntern::fmReset() { +void TownsAudioInterfaceInternal::fmReset() { TownsPC98_FmSynth::reset(); _fmChanPlaying = 0; @@ -1059,7 +1067,7 @@ void TownsAudioInterfaceIntern::fmReset() { } } -int TownsAudioInterfaceIntern::fmKeyOn(int chan, int note, int velo) { +int TownsAudioInterfaceInternal::fmKeyOn(int chan, int note, int velo) { if (chan > 5) return 1; if (note < 12 || note > 107 || (velo & 0x80)) @@ -1139,7 +1147,7 @@ int TownsAudioInterfaceIntern::fmKeyOn(int chan, int note, int velo) { return 0; } -int TownsAudioInterfaceIntern::fmKeyOff(int chan) { +int TownsAudioInterfaceInternal::fmKeyOff(int chan) { if (chan > 5) return 1; _fmChanPlaying &= ~_chanFlags[chan]; @@ -1149,7 +1157,7 @@ int TownsAudioInterfaceIntern::fmKeyOff(int chan) { return 0; } -int TownsAudioInterfaceIntern::fmChanOff(int chan) { +int TownsAudioInterfaceInternal::fmChanOff(int chan) { if (chan > 5) return 1; _fmChanPlaying &= ~_chanFlags[chan]; @@ -1167,7 +1175,7 @@ int TownsAudioInterfaceIntern::fmChanOff(int chan) { return 0; } -int TownsAudioInterfaceIntern::fmSetPanPos(int chan, int value) { +int TownsAudioInterfaceInternal::fmSetPanPos(int chan, int value) { if (chan > 5) return 1; @@ -1186,7 +1194,7 @@ int TownsAudioInterfaceIntern::fmSetPanPos(int chan, int value) { return 0; } -int TownsAudioInterfaceIntern::fmSetInstrument(int chan, int instrId) { +int TownsAudioInterfaceInternal::fmSetInstrument(int chan, int instrId) { if (chan > 5) return 1; if (instrId > 127) @@ -1230,7 +1238,7 @@ int TownsAudioInterfaceIntern::fmSetInstrument(int chan, int instrId) { return 0; } -int TownsAudioInterfaceIntern::fmLoadInstrument(int instrId, const uint8 *data) { +int TownsAudioInterfaceInternal::fmLoadInstrument(int instrId, const uint8 *data) { if (instrId > 127) return 3; assert(data); @@ -1238,7 +1246,7 @@ int TownsAudioInterfaceIntern::fmLoadInstrument(int instrId, const uint8 *data) return 0; } -int TownsAudioInterfaceIntern::fmSetPitch(int chan, int pitch) { +int TownsAudioInterfaceInternal::fmSetPitch(int chan, int pitch) { if (chan > 5) return 1; @@ -1325,7 +1333,7 @@ int TownsAudioInterfaceIntern::fmSetPitch(int chan, int pitch) { return 0; } -int TownsAudioInterfaceIntern::fmSetLevel(int chan, int lvl) { +int TownsAudioInterfaceInternal::fmSetLevel(int chan, int lvl) { if (chan > 5) return 1; if (lvl > 127) @@ -1348,20 +1356,14 @@ int TownsAudioInterfaceIntern::fmSetLevel(int chan, int lvl) { return 0; } -void TownsAudioInterfaceIntern::bufferedWriteReg(uint8 part, uint8 regAddress, uint8 value) { +void TownsAudioInterfaceInternal::bufferedWriteReg(uint8 part, uint8 regAddress, uint8 value) { _fmSaveReg[part][regAddress] = value; writeReg(part, regAddress, value); } -void TownsAudioInterfaceIntern::pcmReset() { - _pcmChanOut = 0; - _pcmChanReserved = _pcmChanKeyPressed = _pcmChanEffectPlaying = _pcmChanKeyPlaying = 0; +void TownsAudioInterfaceInternal::pcmReset() { _numReservedChannels = 0; - memset(_pcmChanNote, 0, 8); - memset(_pcmChanVelo, 0, 8); - memset(_pcmChanLevel, 0, 8); - for (int i = 0; i < 8; i++) _pcmChan[i].clear(); @@ -1381,7 +1383,7 @@ void TownsAudioInterfaceIntern::pcmReset() { } } -int TownsAudioInterfaceIntern::pcmKeyOn(int chan, int note, int velo) { +int TownsAudioInterfaceInternal::pcmKeyOn(int chan, int note, int velo) { if (chan < 0x40 || chan > 0x47) return 1; @@ -1389,94 +1391,43 @@ int TownsAudioInterfaceIntern::pcmKeyOn(int chan, int note, int velo) { return 3; chan -= 0x40; - - if ((_pcmChanReserved & _chanFlags[chan]) || (_pcmChanKeyPressed & _chanFlags[chan])) - return 2; - - _pcmChanNote[chan] = note; - _pcmChanVelo[chan] = velo; - + uint8 noteT = note; TownsAudio_PcmChannel *p = &_pcmChan[chan]; - p->note = note; - uint8 *instr = _pcmChan[chan].curInstrument; - int i = 0; - for (; i < 8; i++) { - if (note <= instr[16 + 2 * i]) - break; - } - - if (i == 8) - return 8; - - int il = i << 3; - p->note += instr[il + 70]; - - p->envTotalLevel = instr[il + 64]; - p->envAttackRate = instr[il + 65]; - p->envDecayRate = instr[il + 66]; - p->envSustainLevel = instr[il + 67]; - p->envSustainRate = instr[il + 68]; - p->envReleaseRate = instr[il + 69]; - p->envStep = 0; - - int32 id = (int32)READ_LE_UINT32(&instr[i * 4 + 32]); - - for (i = 0; i < _numWaveTables; i++) { - if (id == _waveTables[i].id) - break; - } - - if (i == _numWaveTables) - return 9; - - TownsAudio_WaveTable *w = &_waveTables[i]; - - p->data = w->data; - p->dataEnd = w->data + w->size; - p->setupLoop(w->loopStart, w->loopLen); - - pcmCalcPhaseStep(p, w); - - uint32 lvl = _pcmChanLevel[chan] * _pcmChanVelo[chan]; - p->envTotalLevel = ((p->envTotalLevel * lvl) >> 14) & 0xff; - p->envSustainLevel = ((p->envSustainLevel * lvl) >> 14) & 0xff; + if (p->_reserved || p->_keyPressed) + return 2; - p->envAttack(); - p->velo = (p->envCurrentLevel >> 8) << 1; + TownsAudio_WaveTable *w = _waveTables; + int res = p->initInstrument(noteT, w, _numWaveTables); + if (res) + return res; - _pcmChanKeyPressed |= _chanFlags[chan]; - _pcmChanKeyPlaying |= _chanFlags[chan]; - _pcmChanOut |= _chanFlags[chan]; + p->loadData(w); + p->keyOn(noteT, velo, w); return 0; } -int TownsAudioInterfaceIntern::pcmKeyOff(int chan) { +int TownsAudioInterfaceInternal::pcmKeyOff(int chan) { if (chan < 0x40 || chan > 0x47) return 1; chan -= 0x40; - _pcmChanKeyPressed &= ~_chanFlags[chan]; - _pcmChan[chan].envRelease(); + _pcmChan[chan].keyOff(); return 0; } -int TownsAudioInterfaceIntern::pcmChanOff(int chan) { +int TownsAudioInterfaceInternal::pcmChanOff(int chan) { if (chan < 0x40 || chan > 0x47) return 1; chan -= 0x40; - - _pcmChanKeyPressed &= ~_chanFlags[chan]; - _pcmChanEffectPlaying &= ~_chanFlags[chan]; - _pcmChanKeyPlaying &= ~_chanFlags[chan]; - _pcmChanOut &= ~_chanFlags[chan]; + _pcmChan[chan]._keyPressed = _pcmChan[chan]._activeEffect = _pcmChan[chan]._activeKey = _pcmChan[chan]._activeOutput = false; return 0; } -int TownsAudioInterfaceIntern::pcmSetPanPos(int chan, int mode) { +int TownsAudioInterfaceInternal::pcmSetPanPos(int chan, int mode) { if (chan > 0x47) return 1; if (mode & 0x80) @@ -1493,23 +1444,23 @@ int TownsAudioInterfaceIntern::pcmSetPanPos(int chan, int mode) { blc = ((119 + mode) ^ (mode << 4)) & 0xff; } - _pcmChan[chan].panLeft = blc & 0x0f; - _pcmChan[chan].panRight = blc >> 4; + _pcmChan[chan].setBalance(blc); return 0; } -int TownsAudioInterfaceIntern::pcmSetInstrument(int chan, int instrId) { +int TownsAudioInterfaceInternal::pcmSetInstrument(int chan, int instrId) { if (chan > 0x47) return 1; if (instrId > 31) return 3; chan -= 0x40; - _pcmChan[chan].curInstrument = &_pcmInstruments[instrId * 128]; + _pcmChan[chan].setInstrument(&_pcmInstruments[instrId * 128]); + return 0; } -int TownsAudioInterfaceIntern::pcmLoadInstrument(int instrId, const uint8 *data) { +int TownsAudioInterfaceInternal::pcmLoadInstrument(int instrId, const uint8 *data) { if (instrId > 31) return 3; assert(data); @@ -1517,7 +1468,7 @@ int TownsAudioInterfaceIntern::pcmLoadInstrument(int instrId, const uint8 *data) return 0; } -int TownsAudioInterfaceIntern::pcmSetPitch(int chan, int pitch) { +int TownsAudioInterfaceInternal::pcmSetPitch(int chan, int pitch) { if (chan > 0x47) return 1; @@ -1534,20 +1485,12 @@ int TownsAudioInterfaceIntern::pcmSetPitch(int chan, int pitch) { else if (pitch > 0) pts = (((pitch + 0x2001) << 16) / 0x2000) >> 2; - p->stepPitch = pts & 0xffff; - p->step = (p->stepNote * p->stepPitch) >> 14; - -// if (_pcmChanUnkFlag & _chanFlags[chan]) -// unk[chan] = (((p->step * 1000) << 11) / 98) / 20833; - - /*else*/ - if ((_pcmChanEffectPlaying & _chanFlags[chan]) && (p->step > 2048)) - p->step = 2048; + p->setPitch(pts); return 0; } -int TownsAudioInterfaceIntern::pcmSetLevel(int chan, int lvl) { +int TownsAudioInterfaceInternal::pcmSetLevel(int chan, int lvl) { if (chan > 0x47) return 1; @@ -1555,99 +1498,21 @@ int TownsAudioInterfaceIntern::pcmSetLevel(int chan, int lvl) { return 3; chan -= 0x40; - TownsAudio_PcmChannel *p = &_pcmChan[chan]; - - if (_pcmChanReserved & _chanFlags[chan]) { - _pcmChanVelo[chan] = lvl; - p->velo = lvl << 1; - } else { - int32 t = p->envStep * lvl; - if (_pcmChanLevel[chan]) - t /= _pcmChanLevel[chan]; - p->envStep = t; - t = p->envCurrentLevel * lvl; - if (_pcmChanLevel[chan]) - t /= _pcmChanLevel[chan]; - p->envCurrentLevel = t; - _pcmChanLevel[chan] = lvl; - p->velo = p->envCurrentLevel >> 8; - } + _pcmChan[chan].setLevel(lvl); return 0; } -void TownsAudioInterfaceIntern::pcmUpdateEnvelopeGenerator(int chan) { - TownsAudio_PcmChannel *p = &_pcmChan[chan]; - if (!p->envCurrentLevel) { - _pcmChanKeyPlaying &= ~_chanFlags[chan]; - p->envState = kEnvReady; - } - - if (!(_pcmChanKeyPlaying & _chanFlags[chan])) - return; - - switch (p->envState) { - case kEnvAttacking: - if (((p->envCurrentLevel + p->envStep) >> 8) > p->envTotalLevel) { - p->envDecay(); - return; - } else { - p->envCurrentLevel += p->envStep; - } - break; - - case kEnvDecaying: - if (((p->envCurrentLevel - p->envStep) >> 8) < p->envSustainLevel) { - p->envSustain(); - return; - } else { - p->envCurrentLevel -= p->envStep; - } - break; - - case kEnvSustaining: - case kEnvReleasing: - p->envCurrentLevel -= p->envStep; - if (p->envCurrentLevel <= 0) - p->envCurrentLevel = 0; - break; - - default: - break; - } - p->velo = (p->envCurrentLevel >> 8) << 1; +void TownsAudioInterfaceInternal::updateOutputVolume() { + // Avoid calls to g_system->getAudioCDManager() functions from the main thread + // since this can cause mutex lockups. + _updateOutputVol = true; } -void TownsAudioInterfaceIntern::pcmCalcPhaseStep(TownsAudio_PcmChannel *p, TownsAudio_WaveTable *w) { - int8 diff = p->note - w->baseNote; - uint16 r = w->rate + w->rateOffs; - uint16 bl = 0; - uint32 s = 0; - - if (diff < 0) { - diff *= -1; - bl = diff % 12; - diff /= 12; - s = (r >> diff); - if (bl) - s = (s * _pcmPhase2[bl]) >> 16; - - } else if (diff > 0) { - bl = diff % 12; - diff /= 12; - s = (r << diff); - if (bl) - s += ((s * _pcmPhase1[bl]) >> 16); - - } else { - s = r; - } - - p->stepNote = s & 0xffff; - p->step = (s * p->stepPitch) >> 14; -} +void TownsAudioInterfaceInternal::updateOutputVolumeInternal() { + if (!_ready) + return; -void TownsAudioInterfaceIntern::updateOutputVolume() { // FM Towns seems to support volumes of 0 - 63 for each channel. // We recalculate sane values for our 0 to 255 volume range and // balance values for our -128 to 127 volume range @@ -1660,41 +1525,35 @@ void TownsAudioInterfaceIntern::updateOutputVolume() { g_system->getAudioCDManager()->setVolume(volume); g_system->getAudioCDManager()->setBalance(balance); + + _updateOutputVol = false; } -TownsAudioInterfaceIntern *TownsAudioInterfaceIntern::_refInstance = 0; +TownsAudioInterfaceInternal *TownsAudioInterfaceInternal::_refInstance = 0; -int TownsAudioInterfaceIntern::_refCount = 0; +int TownsAudioInterfaceInternal::_refCount = 0; -const uint8 TownsAudioInterfaceIntern::_chanFlags[] = { +const uint8 TownsAudioInterfaceInternal::_chanFlags[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; -const uint16 TownsAudioInterfaceIntern::_frequency[] = { +const uint16 TownsAudioInterfaceInternal::_frequency[] = { 0x028C, 0x02B4, 0x02DC, 0x030A, 0x0338, 0x0368, 0x039C, 0x03D4, 0x040E, 0x044A, 0x048C, 0x04D0 }; -const uint8 TownsAudioInterfaceIntern::_carrier[] = { +const uint8 TownsAudioInterfaceInternal::_carrier[] = { 0x10, 0x10, 0x10, 0x10, 0x30, 0x70, 0x70, 0xF0 }; -const uint8 TownsAudioInterfaceIntern::_fmDefaultInstrument[] = { +const uint8 TownsAudioInterfaceInternal::_fmDefaultInstrument[] = { 0x45, 0x4C, 0x45, 0x50, 0x49, 0x41, 0x4E, 0x4F, 0x01, 0x0A, 0x02, 0x01, 0x1E, 0x32, 0x05, 0x00, 0x9C, 0xDC, 0x9C, 0xDC, 0x07, 0x03, 0x14, 0x08, 0x00, 0x03, 0x05, 0x05, 0x55, 0x45, 0x27, 0xA7, 0x04, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -const uint16 TownsAudioInterfaceIntern::_pcmPhase1[] = { - 0x879B, 0x0F37, 0x1F58, 0x306E, 0x4288, 0x55B6, 0x6A08, 0x7F8F, 0x965E, 0xAE88, 0xC882, 0xE341 -}; - -const uint16 TownsAudioInterfaceIntern::_pcmPhase2[] = { - 0xFEFE, 0xF1A0, 0xE411, 0xD744, 0xCB2F, 0xBFC7, 0xB504, 0xAAE2, 0xA144, 0x9827, 0x8FAC -}; - TownsAudio_PcmChannel::TownsAudio_PcmChannel() { - extData = 0; + _extData = 0; clear(); } @@ -1702,97 +1561,313 @@ TownsAudio_PcmChannel::~TownsAudio_PcmChannel() { clear(); } -void TownsAudio_PcmChannel::loadExtData(uint8 *buffer, uint32 size) { - delete[] extData; - extData = new int8[size]; +void TownsAudio_PcmChannel::clear() { + _curInstrument = 0; + _note = _tl = _level = _velo = 0; + + _data = 0; + _dataEnd = 0; + _loopLen = 0; + + _pos = 0; + _loopEnd = 0; + + _step = 0; + _stepNote = 0x4000; + _stepPitch = 0x4000; + + _panLeft = _panRight = 7; + + _envTotalLevel = _envAttackRate = _envDecayRate = _envSustainLevel = _envSustainRate = _envReleaseRate = 0; + _envStep = _envCurrentLevel = 0; + + _envState = kEnvReady; + + _activeKey = _activeEffect = _activeOutput = _keyPressed = _reserved = false; + + delete[] _extData; + _extData = 0; +} + +void TownsAudio_PcmChannel::loadData(TownsAudio_WaveTable *w) { + _data = w->data; + _dataEnd = w->data + w->size; +} + +void TownsAudio_PcmChannel::loadData(uint8 *buffer, uint32 size) { + delete[] _extData; + _extData = new int8[size]; int8 *src = (int8 *)buffer; - int8 *dst = extData; + int8 *dst = _extData; for (uint32 i = 0; i < size; i++) *dst++ = *src & 0x80 ? (*src++ & 0x7f) : -*src++; - data = extData; - dataEnd = extData + size; - pos = 0; + _data = _extData; + _dataEnd = _extData + size; + _pos = 0; } -void TownsAudio_PcmChannel::setupLoop(uint32 start, uint32 len) { - loopLen = len << 11; - loopEnd = loopLen ? &data[(start + loopLen) >> 11] : dataEnd; - pos = start; +int TownsAudio_PcmChannel::initInstrument(uint8 ¬e, TownsAudio_WaveTable *&tables, int numTables) { + int i = 0; + for (; i < 8; i++) { + if (note <= _curInstrument[16 + 2 * i]) + break; + } + + if (i == 8) + return 8; + + uint8 *d = &_curInstrument[(i << 3) + 64]; + _envTotalLevel = d[0]; + _envAttackRate = d[1]; + _envDecayRate = d[2]; + _envSustainLevel = d[3]; + _envSustainRate = d[4]; + _envReleaseRate = d[5]; + _envStep = 0; + note += d[6]; + + int32 id = (int32)READ_LE_UINT32(&_curInstrument[i * 4 + 32]); + + for (i = 0; i < numTables; i++) { + if (id == tables[i].id) + break; + } + + if (i == numTables) + return 9; + + tables = &tables[i]; + return 0; } -void TownsAudio_PcmChannel::clear() { - curInstrument = 0; - note = 0; - velo = 0; +void TownsAudio_PcmChannel::keyOn(uint8 note, uint8 velo, TownsAudio_WaveTable *w) { + setupLoop(w->loopStart, w->loopLen); + setNote(note, w, _reserved); + setVelo(velo); - data = 0; - dataEnd = 0; - loopLen = 0; + if (_reserved) + _activeEffect = true; + else + _keyPressed = _activeKey = true; + + _activeOutput = true; +} - pos = 0; - loopEnd = 0; +void TownsAudio_PcmChannel::keyOff() { + _keyPressed = false; + envRelease(); +} - step = 0; - stepNote = 0x4000; - stepPitch = 0x4000; +void TownsAudio_PcmChannel::updateEnvelopeGenerator() { + if (!_envCurrentLevel) { + _activeKey = false; + _envState = kEnvReady; + } - panLeft = panRight = 7; + if (!_activeKey) + return; - envTotalLevel = envAttackRate = envDecayRate = envSustainLevel = envSustainRate = envReleaseRate = 0; - envStep = envCurrentLevel = 0; + switch (_envState) { + case kEnvAttacking: + if (((_envCurrentLevel + _envStep) >> 8) > _envTotalLevel) { + envDecay(); + return; + } else { + _envCurrentLevel += _envStep; + } + break; + + case kEnvDecaying: + if (((_envCurrentLevel - _envStep) >> 8) < _envSustainLevel) { + envSustain(); + return; + } else { + _envCurrentLevel -= _envStep; + } + break; + + case kEnvSustaining: + case kEnvReleasing: + _envCurrentLevel -= _envStep; + if (_envCurrentLevel <= 0) + _envCurrentLevel = 0; + break; + + default: + break; + } + _tl = (_envCurrentLevel >> 8) << 1; +} - envState = kEnvReady; +void TownsAudio_PcmChannel::setInstrument(uint8 *instr) { + _curInstrument = instr; +} + +void TownsAudio_PcmChannel::setLevel(uint8 lvl) { + if (_reserved) { + _velo = lvl; + _tl = lvl << 1; + } else { + int32 t = _envStep * lvl; + if (_level) + t /= _level; + _envStep = t; + t = _envCurrentLevel * lvl; + if (_level) + t /= _level; + _envCurrentLevel = t; + _level = lvl; + _tl = _envCurrentLevel >> 8; + } +} + +void TownsAudio_PcmChannel::setPitch(uint32 pt) { + _stepPitch = pt & 0xffff; + _step = (_stepNote * _stepPitch) >> 14; + +// if (_pcmChanUnkFlag & _chanFlags[chan]) +// unk[chan] = (((p->step * 1000) << 11) / 98) / 20833; + + /*else*/ + if (_activeEffect && (_step > 2048)) + _step = 2048; +} + +void TownsAudio_PcmChannel::setBalance(uint8 blc) { + _panLeft = blc & 0x0f; + _panRight = blc >> 4; +} + +void TownsAudio_PcmChannel::updateOutput() { + if (_activeKey || _activeEffect) { + _pos += _step; + + if (&_data[_pos >> 11] >= _loopEnd) { + if (_loopLen) { + _pos -= _loopLen; + } else { + _pos = 0; + _activeKey = _activeEffect = false; + } + } + } +} + +int32 TownsAudio_PcmChannel::currentSampleLeft() { + return (_activeOutput && _panLeft) ? (((_data[_pos >> 11] * _tl) * _panLeft) >> 3) : 0; +} - delete[] extData; - extData = 0; +int32 TownsAudio_PcmChannel::currentSampleRight() { + return (_activeOutput && _panRight) ? (((_data[_pos >> 11] * _tl) * _panRight) >> 3) : 0; +} + +void TownsAudio_PcmChannel::setupLoop(uint32 loopStart, uint32 len) { + _loopLen = len << 11; + _loopEnd = _loopLen ? &_data[(loopStart + _loopLen) >> 11] : _dataEnd; + _pos = loopStart; +} + +void TownsAudio_PcmChannel::setNote(uint8 note, TownsAudio_WaveTable *w, bool stepLimit) { + _note = note; + int8 diff = _note - w->baseNote; + uint16 r = w->rate + w->rateOffs; + uint16 bl = 0; + uint32 s = 0; + + if (diff < 0) { + diff *= -1; + bl = diff % 12; + diff /= 12; + s = (r >> diff); + if (bl) + s = (s * _pcmPhase2[bl]) >> 16; + + } else if (diff > 0) { + bl = diff % 12; + diff /= 12; + s = (r << diff); + if (bl) + s += ((s * _pcmPhase1[bl]) >> 16); + + } else { + s = r; + } + + _stepNote = s & 0xffff; + _step = (s * _stepPitch) >> 14; + + if (stepLimit && _step > 2048) + _step = 2048; +} + +void TownsAudio_PcmChannel::setVelo(uint8 velo) { + if (_reserved) { + _velo = velo; + _tl = velo << 1; + } else { + _velo = velo; + uint32 lvl = _level * _velo; + _envTotalLevel = ((_envTotalLevel * lvl) >> 14) & 0xff; + _envSustainLevel = ((_envSustainLevel * lvl) >> 14) & 0xff; + envAttack(); + _tl = (_envCurrentLevel >> 8) << 1; + } } void TownsAudio_PcmChannel::envAttack() { - envState = kEnvAttacking; - int16 t = envTotalLevel << 8; - if (envAttackRate == 127) { - envStep = 0; - } else if (envAttackRate) { - envStep = t / envAttackRate; - envCurrentLevel = 1; + _envState = kEnvAttacking; + int16 t = _envTotalLevel << 8; + if (_envAttackRate == 127) { + _envCurrentLevel = _envStep = 0; + } else if (_envAttackRate) { + _envStep = t / _envAttackRate; + _envCurrentLevel = 1; } else { - envCurrentLevel = t; + _envCurrentLevel = t; envDecay(); } } void TownsAudio_PcmChannel::envDecay() { - envState = kEnvDecaying; - int16 t = envTotalLevel - envSustainLevel; - if (t < 0 || envDecayRate == 127) { - envStep = 0; - } else if (envDecayRate) { - envStep = (t << 8) / envDecayRate; + _envState = kEnvDecaying; + int16 t = _envTotalLevel - _envSustainLevel; + if (t < 0 || _envDecayRate == 127) { + _envStep = 0; + } else if (_envDecayRate) { + _envStep = (t << 8) / _envDecayRate; } else { - envCurrentLevel = envSustainLevel << 8; + _envCurrentLevel = _envSustainLevel << 8; envSustain(); } } void TownsAudio_PcmChannel::envSustain() { - envState = kEnvSustaining; - if (envSustainLevel && envSustainRate) - envStep = (envSustainRate == 127) ? 0 : (envCurrentLevel / envSustainRate) >> 1; + _envState = kEnvSustaining; + if (_envSustainLevel && _envSustainRate) + _envStep = (_envSustainRate == 127) ? 0 : (_envCurrentLevel / _envSustainRate) >> 1; else - envStep = envCurrentLevel = 1; + _envStep = _envCurrentLevel = 1; } void TownsAudio_PcmChannel::envRelease() { - envState = kEnvReleasing; - if (envReleaseRate == 127) - envStep = 0; - else if (envReleaseRate) - envStep = envCurrentLevel / envReleaseRate; + _envState = kEnvReleasing; + if (_envReleaseRate == 127) + _envStep = 0; + else if (_envReleaseRate) + _envStep = _envCurrentLevel / _envReleaseRate; else - envStep = envCurrentLevel = 1; + _envStep = _envCurrentLevel = 1; } +const uint16 TownsAudio_PcmChannel::_pcmPhase1[] = { + 0x879B, 0x0F37, 0x1F58, 0x306E, 0x4288, 0x55B6, 0x6A08, 0x7F8F, 0x965E, 0xAE88, 0xC882, 0xE341 +}; + +const uint16 TownsAudio_PcmChannel::_pcmPhase2[] = { + 0xFEFE, 0xF1A0, 0xE411, 0xD744, 0xCB2F, 0xBFC7, 0xB504, 0xAAE2, 0xA144, 0x9827, 0x8FAC +}; + TownsAudio_WaveTable::TownsAudio_WaveTable() { data = 0; clear(); @@ -1840,12 +1915,12 @@ void TownsAudio_WaveTable::clear() { data = 0; } -TownsAudioInterface::TownsAudioInterface(Audio::Mixer *mixer, TownsAudioInterfacePluginDriver *driver) { - _intf = TownsAudioInterfaceIntern::addNewRef(mixer, driver); +TownsAudioInterface::TownsAudioInterface(Audio::Mixer *mixer, TownsAudioInterfacePluginDriver *driver, bool externalMutexHandling) { + _intf = TownsAudioInterfaceInternal::addNewRef(mixer, this, driver, externalMutexHandling); } TownsAudioInterface::~TownsAudioInterface() { - TownsAudioInterfaceIntern::releaseRef(); + TownsAudioInterfaceInternal::releaseRef(this); _intf = 0; } diff --git a/audio/softsynth/fmtowns_pc98/towns_audio.h b/audio/softsynth/fmtowns_pc98/towns_audio.h index 2c58d46d06..4af888f009 100644 --- a/audio/softsynth/fmtowns_pc98/towns_audio.h +++ b/audio/softsynth/fmtowns_pc98/towns_audio.h @@ -25,7 +25,7 @@ #include "audio/mixer.h" -class TownsAudioInterfaceIntern; +class TownsAudioInterfaceInternal; class TownsAudioInterfacePluginDriver { public: @@ -35,7 +35,7 @@ public: class TownsAudioInterface { public: - TownsAudioInterface(Audio::Mixer *mixer, TownsAudioInterfacePluginDriver *driver); + TownsAudioInterface(Audio::Mixer *mixer, TownsAudioInterfacePluginDriver *driver, bool externalMutexHandling = false); ~TownsAudioInterface(); bool init(); @@ -49,7 +49,7 @@ public: void setSoundEffectChanMask(int mask); private: - TownsAudioInterfaceIntern *_intf; + TownsAudioInterfaceInternal *_intf; }; #endif diff --git a/audio/softsynth/fmtowns_pc98/towns_midi.cpp b/audio/softsynth/fmtowns_pc98/towns_midi.cpp index e67a78e9dc..4617b0555c 100644 --- a/audio/softsynth/fmtowns_pc98/towns_midi.cpp +++ b/audio/softsynth/fmtowns_pc98/towns_midi.cpp @@ -25,8 +25,6 @@ #include "common/textconsole.h" #include "common/system.h" -enum EnvelopeState; - class TownsMidiOutputChannel { friend class TownsMidiInputChannel; public: @@ -836,20 +834,9 @@ const uint8 TownsMidiInputChannel::_programAdjustLevel[] = { MidiDriver_TOWNS::MidiDriver_TOWNS(Audio::Mixer *mixer) : _timerProc(0), _timerProcPara(0), _channels(0), _out(0), _chanState(0), _operatorLevelTable(0), _tickCounter1(0), _tickCounter2(0), _rand(1), _allocCurPos(0), _isOpen(false) { - _intf = new TownsAudioInterface(mixer, this); -} - -MidiDriver_TOWNS::~MidiDriver_TOWNS() { - close(); - delete _intf; -} - -int MidiDriver_TOWNS::open() { - if (_isOpen) - return MERR_ALREADY_OPEN; - - if (!_intf->init()) - return MERR_CANNOT_CONNECT; + // We set exteral mutex handling to true, since this driver is only suitable for use with the SCUMM engine + // which has its own mutex. This causes lockups which cannot always be avoided. + _intf = new TownsAudioInterface(mixer, this, true); _channels = new TownsMidiInputChannel*[32]; for (int i = 0; i < 32; i++) @@ -868,6 +855,38 @@ int MidiDriver_TOWNS::open() { } for (int i = 0; i < 64; i++) _operatorLevelTable[i << 5] = 0; +} + +MidiDriver_TOWNS::~MidiDriver_TOWNS() { + close(); + delete _intf; + + if (_channels) { + for (int i = 0; i < 32; i++) + delete _channels[i]; + delete[] _channels; + } + _channels = 0; + + if (_out) { + for (int i = 0; i < 6; i++) + delete _out[i]; + delete[] _out; + } + _out = 0; + + delete[] _chanState; + _chanState = 0; + delete[] _operatorLevelTable; + _operatorLevelTable = 0; +} + +int MidiDriver_TOWNS::open() { + if (_isOpen) + return MERR_ALREADY_OPEN; + + if (!_intf->init()) + return MERR_CANNOT_CONNECT; _intf->callback(0); @@ -878,9 +897,7 @@ int MidiDriver_TOWNS::open() { _intf->callback(33, 8); _intf->setSoundEffectChanMask(~0x3f); - _tickCounter1 = _tickCounter2 = 0; - _allocCurPos = 0; - _rand = 1; + _allocCurPos = 0; _isOpen = true; @@ -895,25 +912,6 @@ void MidiDriver_TOWNS::close() { setTimerCallback(0, 0); g_system->delayMillis(20); - - if (_channels) { - for (int i = 0; i < 32; i++) - delete _channels[i]; - delete[] _channels; - } - _channels = 0; - - if (_out) { - for (int i = 0; i < 6; i++) - delete _out[i]; - delete[] _out; - } - _out = 0; - - delete[] _chanState; - _chanState = 0; - delete[] _operatorLevelTable; - _operatorLevelTable = 0; } void MidiDriver_TOWNS::send(uint32 b) { diff --git a/audio/softsynth/fmtowns_pc98/towns_pc98_driver.cpp b/audio/softsynth/fmtowns_pc98/towns_pc98_driver.cpp index e35da91cbb..49fe97caf1 100644 --- a/audio/softsynth/fmtowns_pc98/towns_pc98_driver.cpp +++ b/audio/softsynth/fmtowns_pc98/towns_pc98_driver.cpp @@ -1054,6 +1054,8 @@ TownsPC98_AudioDriver::~TownsPC98_AudioDriver() { _ready = false; deinit(); + Common::StackLock lock(_mutex); + if (_channels) { for (int i = 0; i < _numChan; i++) delete _channels[i]; @@ -1145,7 +1147,7 @@ void TownsPC98_AudioDriver::loadMusicData(uint8 *data, bool loadPaused) { reset(); - lock(); + Common::StackLock lock(_mutex); uint8 *src_a = _trackPtr = _musicBuffer = data; for (uint8 i = 0; i < 3; i++) { @@ -1176,7 +1178,6 @@ void TownsPC98_AudioDriver::loadMusicData(uint8 *data, bool loadPaused) { _finishedChannelsFlag = _finishedSSGFlag = _finishedRhythmFlag = 0; _musicPlaying = (loadPaused ? false : true); - unlock(); } void TownsPC98_AudioDriver::loadSoundEffectData(uint8 *data, uint8 trackNum) { @@ -1195,17 +1196,16 @@ void TownsPC98_AudioDriver::loadSoundEffectData(uint8 *data, uint8 trackNum) { return; } - lock(); + Common::StackLock lock(_mutex); _sfxData = _sfxBuffer = data; _sfxOffsets[0] = READ_LE_UINT16(&_sfxData[(trackNum << 2)]); _sfxOffsets[1] = READ_LE_UINT16(&_sfxData[(trackNum << 2) + 2]); _sfxPlaying = true; _finishedSfxFlag = 0; - unlock(); } void TownsPC98_AudioDriver::reset() { - lock(); + Common::StackLock lock(_mutex); _musicPlaying = false; _sfxPlaying = false; @@ -1232,7 +1232,6 @@ void TownsPC98_AudioDriver::reset() { if (_rhythmChannel) _rhythmChannel->reset(); #endif - unlock(); } void TownsPC98_AudioDriver::fadeStep() { @@ -1263,37 +1262,30 @@ void TownsPC98_AudioDriver::fadeStep() { } } -void TownsPC98_AudioDriver::timerCallbackB() { - _sfxOffs = 0; - - if (_musicPlaying) { - _musicTickCounter++; - - for (int i = 0; i < _numChan; i++) { - if (_updateChannelsFlag & _channels[i]->_idFlag) { - _channels[i]->processEvents(); - _channels[i]->processFrequency(); - } - } +void TownsPC98_AudioDriver::pause() { + _musicPlaying = false; +} + +void TownsPC98_AudioDriver::cont() { + _musicPlaying = true; +} - for (int i = 0; i < _numSSG; i++) { - if (_updateSSGFlag & _ssgChannels[i]->_idFlag) { - _ssgChannels[i]->processEvents(); - _ssgChannels[i]->processFrequency(); - } - } +bool TownsPC98_AudioDriver::looping() { + return _looping == _updateChannelsFlag ? true : false; +} -#ifndef DISABLE_PC98_RHYTHM_CHANNEL - if (_hasPercussion) - if (_updateRhythmFlag & _rhythmChannel->_idFlag) - _rhythmChannel->processEvents(); -#endif - } +bool TownsPC98_AudioDriver::musicPlaying() { + return _musicPlaying; +} - toggleRegProtection(false); +void TownsPC98_AudioDriver::setMusicVolume(int volume) { + _musicVolume = volume; + setVolumeIntern(_musicVolume, _sfxVolume); +} - if (_finishedChannelsFlag == _updateChannelsFlag && _finishedSSGFlag == _updateSSGFlag && _finishedRhythmFlag == _updateRhythmFlag) - _musicPlaying = false; +void TownsPC98_AudioDriver::setSoundEffectVolume(int volume) { + _sfxVolume = volume; + setVolumeIntern(_musicVolume, _sfxVolume); } void TownsPC98_AudioDriver::timerCallbackA() { @@ -1321,15 +1313,37 @@ void TownsPC98_AudioDriver::timerCallbackA() { } } -void TownsPC98_AudioDriver::setMusicTempo(uint8 tempo) { - writeReg(0, 0x26, tempo); - writeReg(0, 0x27, 0x33); -} +void TownsPC98_AudioDriver::timerCallbackB() { + _sfxOffs = 0; -void TownsPC98_AudioDriver::setSfxTempo(uint16 tempo) { - writeReg(0, 0x24, tempo & 0xff); - writeReg(0, 0x25, tempo >> 8); - writeReg(0, 0x27, 0x33); + if (_musicPlaying) { + _musicTickCounter++; + + for (int i = 0; i < _numChan; i++) { + if (_updateChannelsFlag & _channels[i]->_idFlag) { + _channels[i]->processEvents(); + _channels[i]->processFrequency(); + } + } + + for (int i = 0; i < _numSSG; i++) { + if (_updateSSGFlag & _ssgChannels[i]->_idFlag) { + _ssgChannels[i]->processEvents(); + _ssgChannels[i]->processFrequency(); + } + } + +#ifndef DISABLE_PC98_RHYTHM_CHANNEL + if (_hasPercussion) + if (_updateRhythmFlag & _rhythmChannel->_idFlag) + _rhythmChannel->processEvents(); +#endif + } + + toggleRegProtection(false); + + if (_finishedChannelsFlag == _updateChannelsFlag && _finishedSSGFlag == _updateSSGFlag && _finishedRhythmFlag == _updateRhythmFlag) + _musicPlaying = false; } void TownsPC98_AudioDriver::startSoundEffect() { @@ -1352,6 +1366,16 @@ void TownsPC98_AudioDriver::startSoundEffect() { _sfxData = 0; } +void TownsPC98_AudioDriver::setMusicTempo(uint8 tempo) { + writeReg(0, 0x26, tempo); + writeReg(0, 0x27, 0x33); +} + +void TownsPC98_AudioDriver::setSfxTempo(uint16 tempo) { + writeReg(0, 0x24, tempo & 0xff); + writeReg(0, 0x25, tempo >> 8); + writeReg(0, 0x27, 0x33); +} const uint8 TownsPC98_AudioDriver::_drvTables[] = { // channel presets 0x00, 0x80, 0x00, 0x00, 0x00, 0x01, diff --git a/audio/softsynth/fmtowns_pc98/towns_pc98_driver.h b/audio/softsynth/fmtowns_pc98/towns_pc98_driver.h index 46ee23895b..ff58482227 100644 --- a/audio/softsynth/fmtowns_pc98/towns_pc98_driver.h +++ b/audio/softsynth/fmtowns_pc98/towns_pc98_driver.h @@ -50,33 +50,19 @@ public: void fadeStep(); - void pause() { - _musicPlaying = false; - } - void cont() { - _musicPlaying = true; - } + void pause(); + void cont(); - void timerCallbackB(); + bool looping(); + bool musicPlaying(); + + void setMusicVolume(int volume); + void setSoundEffectVolume(int volume); + +private: void timerCallbackA(); + void timerCallbackB(); - bool looping() { - return _looping == _updateChannelsFlag ? true : false; - } - bool musicPlaying() { - return _musicPlaying; - } - - void setMusicVolume(int volume) { - _musicVolume = volume; - setVolumeIntern(_musicVolume, _sfxVolume); - } - void setSoundEffectVolume(int volume) { - _sfxVolume = volume; - setVolumeIntern(_musicVolume, _sfxVolume); - } - -protected: void startSoundEffect(); void setMusicTempo(uint8 tempo); diff --git a/audio/softsynth/fmtowns_pc98/towns_pc98_fmsynth.cpp b/audio/softsynth/fmtowns_pc98/towns_pc98_fmsynth.cpp index bc5aa32823..63007ba93c 100644 --- a/audio/softsynth/fmtowns_pc98/towns_pc98_fmsynth.cpp +++ b/audio/softsynth/fmtowns_pc98/towns_pc98_fmsynth.cpp @@ -851,7 +851,7 @@ void TownsPC98_FmSynthPercussionSource::advanceInput(RhtChannel *ins) { } #endif // DISABLE_PC98_RHYTHM_CHANNEL -TownsPC98_FmSynth::TownsPC98_FmSynth(Audio::Mixer *mixer, EmuType type) : +TownsPC98_FmSynth::TownsPC98_FmSynth(Audio::Mixer *mixer, EmuType type, bool externalMutexHandling) : _mixer(mixer), _chanInternal(0), _ssg(0), #ifndef DISABLE_PC98_RHYTHM_CHANNEL @@ -861,7 +861,8 @@ TownsPC98_FmSynth::TownsPC98_FmSynth(Audio::Mixer *mixer, EmuType type) : _hasPercussion(type == kType86 ? true : false), _oprRates(0), _oprRateshift(0), _oprAttackDecay(0), _oprFrq(0), _oprSinTbl(0), _oprLevelOut(0), _oprDetune(0), _rtt(type == kTypeTowns ? 0x514767 : 0x5B8D80), _baserate(55125.0f / (float)mixer->getOutputRate()), - _volMaskA(0), _volMaskB(0), _volumeA(255), _volumeB(255), _regProtectionFlag(false), _lock(0), _ready(false) { + _volMaskA(0), _volMaskB(0), _volumeA(255), _volumeB(255), + _regProtectionFlag(false), _externalMutex(externalMutexHandling), _ready(false) { memset(&_timers[0], 0, sizeof(ChipTimer)); memset(&_timers[1], 0, sizeof(ChipTimer)); @@ -874,6 +875,8 @@ TownsPC98_FmSynth::~TownsPC98_FmSynth() { if (_ready) deinit(); + Common::StackLock lock(_mutex); + delete _ssg; #ifndef DISABLE_PC98_RHYTHM_CHANNEL delete _prc; @@ -928,7 +931,7 @@ bool TownsPC98_FmSynth::init() { } void TownsPC98_FmSynth::reset() { - lock(); + Common::StackLock lock(_mutex); for (int i = 0; i < _numChan; i++) { for (int ii = 0; ii < 4; ii++) _chanInternal[i].opr[ii]->reset(); @@ -948,14 +951,13 @@ void TownsPC98_FmSynth::reset() { if (_prc) _prc->reset(); #endif - unlock(); } void TownsPC98_FmSynth::writeReg(uint8 part, uint8 regAddress, uint8 value) { if (_regProtectionFlag || !_ready) return; - lock(); + Common::StackLock lock(_mutex); static const uint8 oprOrdr[] = { 0, 2, 1, 3 }; @@ -1137,7 +1139,6 @@ void TownsPC98_FmSynth::writeReg(uint8 part, uint8 regAddress, uint8 value) { default: warning("TownsPC98_FmSynth: UNKNOWN ADDRESS %d", regAddress); } - unlock(); } int TownsPC98_FmSynth::readBuffer(int16 *buffer, const int numSamples) { @@ -1146,15 +1147,32 @@ int TownsPC98_FmSynth::readBuffer(int16 *buffer, const int numSamples) { int32 *tmpStart = tmp; memset(tmp, 0, sizeof(int32) * numSamples); int32 samplesLeft = numSamples >> 1; - _lock |= 0x10000; - while (_ready && !(_lock & 0xffff) && samplesLeft) { + bool locked = false; + if (_ready) { + _mutex.lock(); + locked = true; + } + + while (_ready && samplesLeft) { int32 render = samplesLeft; for (int i = 0; i < 2; i++) { if (_timers[i].enabled && _timers[i].cb) { if (!_timers[i].smpTillCb) { + + if (locked && _externalMutex) { + _mutex.unlock(); + locked = false; + } + (this->*_timers[i].cb)(); + + if (!locked && _externalMutex) { + _mutex.lock(); + locked = true; + } + _timers[i].smpTillCb = _timers[i].smpPerCb; _timers[i].smpTillCbRem += _timers[i].smpPerCbRem; @@ -1197,26 +1215,43 @@ int TownsPC98_FmSynth::readBuffer(int16 *buffer, const int numSamples) { tmp += (render << 1); } - _lock &= ~0x10000; + if (locked) + _mutex.unlock(); + delete[] tmpStart; return numSamples; } +bool TownsPC98_FmSynth::isStereo() const { + return true; +} + +bool TownsPC98_FmSynth::endOfData() const { + return false; +} + +int TownsPC98_FmSynth::getRate() const { + return _mixer->getOutputRate(); +} + void TownsPC98_FmSynth::deinit() { _ready = false; - while (_lock) - g_system->delayMillis(20); _mixer->stopHandle(_soundHandle); + Common::StackLock lock(_mutex); _timers[0].cb = _timers[1].cb = &TownsPC98_FmSynth::idleTimerCallback; } +void TownsPC98_FmSynth::toggleRegProtection(bool prot) { + _regProtectionFlag = prot; +} + uint8 TownsPC98_FmSynth::readSSGStatus() { return _ssg->chanEnable(); } void TownsPC98_FmSynth::setVolumeIntern(int volA, int volB) { - lock(); + Common::StackLock lock(_mutex); _volumeA = CLIP<uint16>(volA, 0, Audio::Mixer::kMaxMixerVolume); _volumeB = CLIP<uint16>(volB, 0, Audio::Mixer::kMaxMixerVolume); if (_ssg) @@ -1225,11 +1260,10 @@ void TownsPC98_FmSynth::setVolumeIntern(int volA, int volB) { if (_prc) _prc->setVolumeIntern(_volumeA, _volumeB); #endif - unlock(); } void TownsPC98_FmSynth::setVolumeChannelMasks(int channelMaskA, int channelMaskB) { - lock(); + Common::StackLock lock(_mutex); _volMaskA = channelMaskA; _volMaskB = channelMaskB; if (_ssg) @@ -1238,17 +1272,6 @@ void TownsPC98_FmSynth::setVolumeChannelMasks(int channelMaskA, int channelMaskB if (_prc) _prc->setVolumeChannelMasks(_volMaskA >> (_numChan + _numSSG), _volMaskB >> (_numChan + _numSSG)); #endif - unlock(); -} - -void TownsPC98_FmSynth::lock() { - _mutex.lock(); - _lock++; -} - -void TownsPC98_FmSynth::unlock() { - _mutex.unlock(); - _lock--; } void TownsPC98_FmSynth::generateTables() { diff --git a/audio/softsynth/fmtowns_pc98/towns_pc98_fmsynth.h b/audio/softsynth/fmtowns_pc98/towns_pc98_fmsynth.h index f7bcc90585..4f81fa9a5c 100644 --- a/audio/softsynth/fmtowns_pc98/towns_pc98_fmsynth.h +++ b/audio/softsynth/fmtowns_pc98/towns_pc98_fmsynth.h @@ -59,7 +59,7 @@ public: kType86 }; - TownsPC98_FmSynth(Audio::Mixer *mixer, EmuType type); + TownsPC98_FmSynth(Audio::Mixer *mixer, EmuType type, bool externalMutexHandling = false); virtual ~TownsPC98_FmSynth(); virtual bool init(); @@ -69,15 +69,9 @@ public: // AudioStream interface int readBuffer(int16 *buffer, const int numSamples); - bool isStereo() const { - return true; - } - bool endOfData() const { - return false; - } - int getRate() const { - return _mixer->getOutputRate(); - } + bool isStereo() const; + bool endOfData() const; + int getRate() const; protected: void deinit(); @@ -86,29 +80,27 @@ protected: // additional output that has to be inserted into the buffer. virtual void nextTickEx(int32 *buffer, uint32 bufferSize) {} - void toggleRegProtection(bool prot) { - _regProtectionFlag = prot; - } + void toggleRegProtection(bool prot); uint8 readSSGStatus(); virtual void timerCallbackA() = 0; virtual void timerCallbackB() = 0; - // The audio driver can store and apply two different audio settings + // The audio driver can store and apply two different volume settings // (usually for music and sound effects). The channel mask will determine - // which channels get effected by the setting. The first bits will be + // which channels get effected by which setting. The first bits will be // the normal fm channels, the next bits the ssg channels and the final // bit the rhythm channel. void setVolumeIntern(int volA, int volB); void setVolumeChannelMasks(int channelMaskA, int channelMaskB); - void lock(); - void unlock(); - const int _numChan; const int _numSSG; const bool _hasPercussion; + Common::Mutex _mutex; + bool _externalMutex; + private: void generateTables(); void nextTick(int32 *buffer, uint32 bufferSize); @@ -182,9 +174,6 @@ private: Audio::Mixer *_mixer; Audio::SoundHandle _soundHandle; - int _lock; - Common::Mutex _mutex; - #ifndef DISABLE_PC98_RHYTHM_CHANNEL static const uint8 _percussionData[]; #endif diff --git a/audio/softsynth/mt32.cpp b/audio/softsynth/mt32.cpp index a9a612f410..27a5a629c4 100644 --- a/audio/softsynth/mt32.cpp +++ b/audio/softsynth/mt32.cpp @@ -63,7 +63,7 @@ protected: void generateSamples(int16 *buf, int len); public: - bool _initialising; + bool _initializing; MidiDriver_MT32(Audio::Mixer *mixer); virtual ~MidiDriver_MT32(); @@ -215,7 +215,7 @@ static MT32Emu::File *MT32_OpenFile(void *userData, const char *filename, MT32Em } static void MT32_PrintDebug(void *userData, const char *fmt, va_list list) { - if (((MidiDriver_MT32 *)userData)->_initialising) { + if (((MidiDriver_MT32 *)userData)->_initializing) { char buf[512]; vsnprintf(buf, 512, fmt, list); @@ -239,7 +239,7 @@ static int MT32_Report(void *userData, MT32Emu::ReportType type, const void *rep error("Failed to load MT32_PCM.ROM"); break; case MT32Emu::ReportType_progressInit: - if (((MidiDriver_MT32 *)userData)->_initialising) { + if (((MidiDriver_MT32 *)userData)->_initializing) { drawProgress(*((const float *)reportData)); return eatSystemEvents(); } @@ -283,7 +283,7 @@ MidiDriver_MT32::MidiDriver_MT32(Audio::Mixer *mixer) : MidiDriver_Emulated(mixe // at rates other than 32KHz, thus we produce data at 32KHz and // rely on Mixer to convert. _outputRate = 32000; //_mixer->getOutputRate(); - _initialising = false; + _initializing = false; } MidiDriver_MT32::~MidiDriver_MT32() { @@ -324,11 +324,11 @@ int MidiDriver_MT32::open() { g_system->getPaletteManager()->setPalette(dummy_palette, 0, 3); } - _initialising = true; - drawMessage(-1, _s("Initialising MT-32 Emulator")); + _initializing = true; + drawMessage(-1, _s("Initializing MT-32 Emulator")); if (!_synth->open(prop)) return MERR_DEVICE_NOT_AVAILABLE; - _initialising = false; + _initializing = false; if (screenFormat.bytesPerPixel > 1) g_system->fillScreen(screenFormat.RGBToColor(0, 0, 0)); diff --git a/audio/softsynth/mt32/partial.cpp b/audio/softsynth/mt32/partial.cpp index d06634dc91..c4f2e94ebe 100644 --- a/audio/softsynth/mt32/partial.cpp +++ b/audio/softsynth/mt32/partial.cpp @@ -164,7 +164,7 @@ void Partial::startPartial(dpoly *usePoly, const PatchCache *useCache, Partial * structurePosition = patchCache->structurePosition; play = true; - initKeyFollow(poly->freqnum); // Initialises noteVal, filtVal and realVal + initKeyFollow(poly->freqnum); // Initializes noteVal, filtVal and realVal #if MT32EMU_ACCURATENOTES == 0 noteLookup = &synth->tables.noteLookups[noteVal - LOWEST_NOTE]; #else diff --git a/audio/softsynth/mt32/synth.cpp b/audio/softsynth/mt32/synth.cpp index 5e74b262ae..322b864b6e 100644 --- a/audio/softsynth/mt32/synth.cpp +++ b/audio/softsynth/mt32/synth.cpp @@ -426,36 +426,36 @@ bool Synth::open(SynthProperties &useProp) { } } - printDebug("Initialising Timbre Bank A"); + printDebug("Initializing Timbre Bank A"); if (!initTimbres(controlROMMap->timbreAMap, controlROMMap->timbreAOffset, 0)) { return false; } - printDebug("Initialising Timbre Bank B"); + printDebug("Initializing Timbre Bank B"); if (!initTimbres(controlROMMap->timbreBMap, controlROMMap->timbreBOffset, 64)) { return false; } - printDebug("Initialising Timbre Bank R"); + printDebug("Initializing Timbre Bank R"); if (!initRhythmTimbres(controlROMMap->timbreRMap, controlROMMap->timbreRCount)) { return false; } - printDebug("Initialising Timbre Bank M"); - // CM-64 seems to initialise all bytes in this bank to 0. + printDebug("Initializing Timbre Bank M"); + // CM-64 seems to initialize all bytes in this bank to 0. memset(&mt32ram.timbres[128], 0, sizeof (mt32ram.timbres[128]) * 64); partialManager = new PartialManager(this); pcmWaves = new PCMWaveEntry[controlROMMap->pcmCount]; - printDebug("Initialising PCM List"); + printDebug("Initializing PCM List"); initPCMList(controlROMMap->pcmTable, controlROMMap->pcmCount); - printDebug("Initialising Rhythm Temp"); + printDebug("Initializing Rhythm Temp"); memcpy(mt32ram.rhythmSettings, &controlROMData[controlROMMap->rhythmSettings], controlROMMap->rhythmSettingsCount * 4); - printDebug("Initialising Patches"); + printDebug("Initializing Patches"); for (Bit8u i = 0; i < 128; i++) { PatchParam *patch = &mt32ram.patches[i]; patch->timbreGroup = i / 64; @@ -468,9 +468,9 @@ bool Synth::open(SynthProperties &useProp) { patch->dummy = 0; } - printDebug("Initialising System"); + printDebug("Initializing System"); // The MT-32 manual claims that "Standard pitch" is 442Hz. - mt32ram.system.masterTune = 0x40; // Confirmed on CM-64 as 0x4A, but SCUMM games use 0x40 and we don't want to initialise twice + mt32ram.system.masterTune = 0x40; // Confirmed on CM-64 as 0x4A, but SCUMM games use 0x40 and we don't want to initialize twice mt32ram.system.reverbMode = 0; // Confirmed mt32ram.system.reverbTime = 5; // Confirmed mt32ram.system.reverbLevel = 3; // Confirmed @@ -792,7 +792,7 @@ void Synth::writeSysex(unsigned char device, const Bit8u *sysex, Bit32u len) { for (;;) { // Find the appropriate memory region int regionNum; - const MemoryRegion *region = NULL; // Initialised to please compiler + const MemoryRegion *region = NULL; // Initialized to please compiler for (regionNum = 0; regionNum < NUM_REGIONS; regionNum++) { region = &memoryRegions[regionNum]; if (region->contains(addr)) { diff --git a/audio/softsynth/mt32/synth.h b/audio/softsynth/mt32/synth.h index edda446287..0ef2c9d135 100644 --- a/audio/softsynth/mt32/synth.h +++ b/audio/softsynth/mt32/synth.h @@ -263,7 +263,7 @@ public: Synth(); ~Synth(); - // Used to initialise the MT-32. Must be called before any other function. + // Used to initialize the MT-32. Must be called before any other function. // Returns true if initialization was sucessful, otherwise returns false. bool open(SynthProperties &useProp); diff --git a/audio/softsynth/mt32/tables.cpp b/audio/softsynth/mt32/tables.cpp index 25ee0436db..9fdb595467 100644 --- a/audio/softsynth/mt32/tables.cpp +++ b/audio/softsynth/mt32/tables.cpp @@ -203,7 +203,7 @@ void Tables::initEnvelopes(float samplerate) { void Tables::initMT32ConstantTables(Synth *synth) { int lf; - synth->printDebug("Initialising Pitch Tables"); + synth->printDebug("Initializing Pitch Tables"); for (lf = -108; lf <= 108; lf++) { tvfKeyfollowMult[lf + 108] = (int)(256 * powf(2.0f, (float)(lf / 24.0f))); //synth->printDebug("KT %d = %d", f, keytable[f+108]); @@ -668,7 +668,7 @@ bool Tables::initNotes(Synth *synth, PCMWaveEntry *pcmWaves, float rate, float m bool abort = false; synth->report(ReportType_progressInit, &progress); for (int f = LOWEST_NOTE; f <= HIGHEST_NOTE; f++) { - synth->printDebug("Initialising note %s%d", NoteNames[f % 12], (f / 12) - 2); + synth->printDebug("Initializing note %s%d", NoteNames[f % 12], (f / 12) - 2); NoteLookup *noteLookup = ¬eLookups[f - LOWEST_NOTE]; file = initNote(synth, noteLookup, (float)f, rate, masterTune, pcmWaves, file); progress = (f - LOWEST_NOTE + 1) / (float)NUM_NOTES; @@ -723,12 +723,12 @@ void Tables::freeNotes() { } } } - initialisedMasterTune = 0.0f; + initializedMasterTune = 0.0f; } Tables::Tables() { - initialisedSampleRate = 0.0f; - initialisedMasterTune = 0.0f; + initializedSampleRate = 0.0f; + initializedMasterTune = 0.0f; memset(¬eLookups, 0, sizeof(noteLookups)); } @@ -737,23 +737,23 @@ bool Tables::init(Synth *synth, PCMWaveEntry *pcmWaves, float sampleRate, float synth->printDebug("Bad sampleRate (%f <= 0.0f)", (double)sampleRate); return false; } - if (initialisedSampleRate == 0.0f) { + if (initializedSampleRate == 0.0f) { initMT32ConstantTables(synth); } - if (initialisedSampleRate != sampleRate) { + if (initializedSampleRate != sampleRate) { initFiltCoeff(sampleRate); initEnvelopes(sampleRate); for (int key = 12; key <= 108; key++) { initDep(&keyLookups[key - 12], (float)key); } } - if (initialisedSampleRate != sampleRate || initialisedMasterTune != masterTune) { + if (initializedSampleRate != sampleRate || initializedMasterTune != masterTune) { freeNotes(); if (!initNotes(synth, pcmWaves, sampleRate, masterTune)) { return false; } - initialisedSampleRate = sampleRate; - initialisedMasterTune = masterTune; + initializedSampleRate = sampleRate; + initializedMasterTune = masterTune; } return true; } diff --git a/audio/softsynth/mt32/tables.h b/audio/softsynth/mt32/tables.h index d9af5114b2..9950323e7b 100644 --- a/audio/softsynth/mt32/tables.h +++ b/audio/softsynth/mt32/tables.h @@ -69,8 +69,8 @@ struct KeyLookup { }; class Tables { - float initialisedSampleRate; - float initialisedMasterTune; + float initializedSampleRate; + float initializedMasterTune; void initMT32ConstantTables(Synth *synth); static Bit16s clampWF(Synth *synth, const char *n, float ampVal, double input); static File *initWave(Synth *synth, NoteLookup *noteLookup, float ampsize, float div2, File *file); diff --git a/audio/softsynth/opl/mame.cpp b/audio/softsynth/opl/mame.cpp index b380a15345..74699ba4c6 100644 --- a/audio/softsynth/opl/mame.cpp +++ b/audio/softsynth/opl/mame.cpp @@ -546,7 +546,7 @@ inline void OPL_CALC_RH(FM_OPL *OPL, OPL_CH *CH) { // but EG_STEP = 96.0/EG_ENT, and WHITE_NOISE_db=6.0. So, that's equivalent to // int(OPL->rnd.getRandomBit() * EG_ENT/16). We know that EG_ENT is 4096, or 1024, // or 128, so we can safely avoid any FP ops. - int whitenoise = OPL->rnd.getRandomBit() * (EG_ENT>>4); + int whitenoise = OPL->rnd->getRandomBit() * (EG_ENT>>4); int tone8; @@ -754,8 +754,10 @@ static int OPLOpenTable(void) { } static void OPLCloseTable(void) { +#ifndef __DS__ free(TL_TABLE); free(SIN_TABLE); +#endif free(AMS_TABLE); free(VIB_TABLE); free(ENV_CURVE); @@ -1126,6 +1128,15 @@ FM_OPL *OPLCreate(int type, int clock, int rate) { OPL->rate = rate; OPL->max_ch = max_ch; + // Init the random source. Note: We use a fixed name for it here. + // So if multiple FM_OPL objects exist in parallel, then their + // random sources will have an equal name. At least in the + // current EventRecorder implementation, this causes no problems; + // but this is probably not guaranteed. + // Alas, it does not seem worthwhile to bother much with this + // at the time, so I am leaving it as it is. + OPL->rnd = new Common::RandomSource("mame"); + /* init grobal tables */ OPL_initalize(OPL); @@ -1134,9 +1145,10 @@ FM_OPL *OPLCreate(int type, int clock, int rate) { return OPL; } -/* ---------- Destroy one of vietual YM3812 ---------- */ +/* ---------- Destroy one of virtual YM3812 ---------- */ void OPLDestroy(FM_OPL *OPL) { OPL_UnLockTable(); + delete OPL->rnd; free(OPL); } diff --git a/audio/softsynth/opl/mame.h b/audio/softsynth/opl/mame.h index 4c40949483..803ca897e7 100644 --- a/audio/softsynth/opl/mame.h +++ b/audio/softsynth/opl/mame.h @@ -147,7 +147,7 @@ typedef struct fm_opl_f { OPL_UPDATEHANDLER UpdateHandler; /* stream update handler */ int UpdateParam; /* stream update parameter */ - Common::RandomSource rnd; + Common::RandomSource *rnd; } FM_OPL; /* ---------- Generic interface section ---------- */ |