ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

wma.h

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 // Based on xoreos' WMA code which is in turn
00024 // Largely based on the WMA implementation found in FFmpeg.
00025 
00026 #ifndef AUDIO_DECODERS_WMA_H
00027 #define AUDIO_DECODERS_WMA_H
00028 
00029 #include "common/array.h"
00030 #include "common/bitstream.h"
00031 
00032 #include "audio/decoders/codec.h"
00033 
00034 namespace Common {
00035 template <class BITSTREAM>
00036 class Huffman;
00037 class MDCT;
00038 }
00039 
00040 namespace Audio {
00041 
00042 struct WMACoefHuffmanParam;
00043 
00044 class WMACodec : public Codec {
00045 public:
00046     WMACodec(int version, uint32 sampleRate, uint8 channels,
00047              uint32 bitRate, uint32 blockAlign, Common::SeekableReadStream *extraData = 0);
00048     ~WMACodec();
00049 
00050     AudioStream *decodeFrame(Common::SeekableReadStream &data);
00051 
00052 private:
00053     static const int kChannelsMax = 2; 
00054 
00055     static const int kBlockBitsMin =  7; 
00056     static const int kBlockBitsMax = 11; 
00057 
00059     static const int kBlockSizeMax = (1 << kBlockBitsMax);
00060 
00061     static const int kBlockNBSizes = (kBlockBitsMax - kBlockBitsMin + 1);
00062 
00064     static const int kSuperframeSizeMax = 16384;
00065 
00067     static const int kHighBandSizeMax = 16;
00068 
00070     static const int kNoiseTabSize = 8192;
00071 
00073     static const int kLSPPowBits = 7;
00074 
00075     int _version; 
00076 
00077     uint32 _sampleRate; 
00078     uint8  _channels;   
00079     uint32 _bitRate;    
00080     uint32 _blockAlign; 
00081     byte   _audioFlags; 
00082 
00083     bool _useExpHuffman;       
00084     bool _useBitReservoir;     
00085     bool _useVariableBlockLen; 
00086     bool _useNoiseCoding;      
00087 
00088     bool _resetBlockLengths; 
00089 
00090     int _curFrame;       
00091     int _frameLen;       
00092     int _frameLenBits;   
00093     int _blockSizeCount; 
00094     int _framePos;       
00095 
00096     int _curBlock;         
00097     int _blockLen;         
00098     int _blockLenBits;     
00099     int _nextBlockLenBits; 
00100     int _prevBlockLenBits; 
00101 
00102     int _byteOffsetBits;
00103 
00104     // Coefficients
00105     int    _coefsStart;                       
00106     int    _coefsEnd[kBlockNBSizes];          
00107     int    _exponentSizes[kBlockNBSizes];
00108     uint16 _exponentBands[kBlockNBSizes][25];
00109     int    _highBandStart[kBlockNBSizes];     
00110     int    _exponentHighSizes[kBlockNBSizes];
00111     int    _exponentHighBands[kBlockNBSizes][kHighBandSizeMax];
00112 
00113     typedef Common::Huffman<Common::BitStream8MSB> HuffmanDecoder;
00114     HuffmanDecoder *_coefHuffman[2];                
00115     const WMACoefHuffmanParam *_coefHuffmanParam[2]; 
00116 
00117     uint16 *_coefHuffmanRunTable[2];   
00118     float  *_coefHuffmanLevelTable[2]; 
00119     uint16 *_coefHuffmanIntTable[2];   
00120 
00121     // Noise
00122     float _noiseMult;                 
00123     float _noiseTable[kNoiseTabSize]; 
00124     int   _noiseIndex;
00125 
00126     HuffmanDecoder *_hgainHuffman; 
00127 
00128     // Exponents
00129     int   _exponentsBSize[kChannelsMax];
00130     float _exponents[kChannelsMax][kBlockSizeMax];
00131     float _maxExponent[kChannelsMax];
00132 
00133     HuffmanDecoder *_expHuffman; 
00134 
00135     // Coded values in high bands
00136     bool _highBandCoded [kChannelsMax][kHighBandSizeMax];
00137     int  _highBandValues[kChannelsMax][kHighBandSizeMax];
00138 
00139     // Coefficients
00140     float _coefs1[kChannelsMax][kBlockSizeMax];
00141     float _coefs [kChannelsMax][kBlockSizeMax];
00142 
00143     // Line spectral pairs
00144     float _lspCosTable[kBlockSizeMax];
00145     float _lspPowETable[256];
00146     float _lspPowMTable1[(1 << kLSPPowBits)];
00147     float _lspPowMTable2[(1 << kLSPPowBits)];
00148 
00149     // MDCT
00150     Common::Array<Common::MDCT *> _mdct;       
00151     Common::Array<const float *>  _mdctWindow; 
00152 
00154     byte _lastSuperframe[kSuperframeSizeMax + 4];
00155     int  _lastSuperframeLen; 
00156     int  _lastBitoffset;     
00157 
00158     // Output
00159     float _output[kBlockSizeMax * 2];
00160     float _frameOut[kChannelsMax][kBlockSizeMax * 2];
00161 
00162 
00163     // Init helpers
00164 
00165     void init(Common::SeekableReadStream *extraData);
00166 
00167     uint16 getFlags(Common::SeekableReadStream *extraData);
00168     void evalFlags(uint16 flags, Common::SeekableReadStream *extraData);
00169     int getFrameBitLength();
00170     int getBlockSizeCount(uint16 flags);
00171     uint32 getNormalizedSampleRate();
00172     bool useNoiseCoding(float &highFreq, float &bps);
00173     void evalMDCTScales(float highFreq);
00174     void initNoise();
00175     void initCoefHuffman(float bps);
00176     void initMDCT();
00177     void initExponents();
00178 
00179     HuffmanDecoder *initCoefHuffman(uint16 *&runTable, float *&levelTable,
00180                                      uint16 *&intTable, const WMACoefHuffmanParam &params);
00181     void initLSPToCurve();
00182 
00183     // Decoding
00184 
00185     Common::SeekableReadStream *decodeSuperFrame(Common::SeekableReadStream &data);
00186     bool decodeFrame(Common::BitStream8MSB &bits, int16 *outputData);
00187     int decodeBlock(Common::BitStream8MSB &bits);
00188 
00189     // Decoding helpers
00190 
00191     bool evalBlockLength(Common::BitStream8MSB &bits);
00192     bool decodeChannels(Common::BitStream8MSB &bits, int bSize, bool msStereo, bool *hasChannel);
00193     bool calculateIMDCT(int bSize, bool msStereo, bool *hasChannel);
00194 
00195     void calculateCoefCount(int *coefCount, int bSize) const;
00196     bool decodeNoise(Common::BitStream8MSB &bits, int bSize, bool *hasChannel, int *coefCount);
00197     bool decodeExponents(Common::BitStream8MSB &bits, int bSize, bool *hasChannel);
00198     bool decodeSpectralCoef(Common::BitStream8MSB &bits, bool msStereo, bool *hasChannel,
00199                             int *coefCount, int coefBitCount);
00200     float getNormalizedMDCTLength() const;
00201     void calculateMDCTCoefficients(int bSize, bool *hasChannel,
00202                                    int *coefCount, int totalGain, float mdctNorm);
00203 
00204     bool decodeExpHuffman(Common::BitStream8MSB &bits, int ch);
00205     bool decodeExpLSP(Common::BitStream8MSB &bits, int ch);
00206     bool decodeRunLevel(Common::BitStream8MSB &bits, const HuffmanDecoder &huffman,
00207         const float *levelTable, const uint16 *runTable, int version, float *ptr,
00208         int offset, int numCoefs, int blockLen, int frameLenBits, int coefNbBits);
00209 
00210     void lspToCurve(float *out, float *val_max_ptr, int n, float *lsp);
00211 
00212     void window(float *out) const;
00213 
00214     float pow_m1_4(float x) const;
00215 
00216     static int readTotalGain(Common::BitStream8MSB &bits);
00217     static int totalGainToBits(int totalGain);
00218     static uint32 getLargeVal(Common::BitStream8MSB &bits);
00219 };
00220 
00221 } // End of namespace Audio
00222 
00223 #endif // AUDIO_DECODERS_WMA_H


Generated on Sat Jun 15 2019 05:01:02 for ResidualVM by doxygen 1.7.1
curved edge   curved edge