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     class Huffman;
00036     class MDCT;
00037 }
00038 
00039 namespace Audio {
00040 
00041 struct WMACoefHuffmanParam;
00042 
00043 class WMACodec : public Codec {
00044 public:
00045     WMACodec(int version, uint32 sampleRate, uint8 channels,
00046              uint32 bitRate, uint32 blockAlign, Common::SeekableReadStream *extraData = 0);
00047     ~WMACodec();
00048 
00049     AudioStream *decodeFrame(Common::SeekableReadStream &data);
00050 
00051 private:
00052     static const int kChannelsMax = 2; 
00053 
00054     static const int kBlockBitsMin =  7; 
00055     static const int kBlockBitsMax = 11; 
00056 
00058     static const int kBlockSizeMax = (1 << kBlockBitsMax);
00059 
00060     static const int kBlockNBSizes = (kBlockBitsMax - kBlockBitsMin + 1);
00061 
00063     static const int kSuperframeSizeMax = 16384;
00064 
00066     static const int kHighBandSizeMax = 16;
00067 
00069     static const int kNoiseTabSize = 8192;
00070 
00072     static const int kLSPPowBits = 7;
00073 
00074     int _version; 
00075 
00076     uint32 _sampleRate; 
00077     uint8  _channels;   
00078     uint32 _bitRate;    
00079     uint32 _blockAlign; 
00080     byte   _audioFlags; 
00081 
00082     bool _useExpHuffman;       
00083     bool _useBitReservoir;     
00084     bool _useVariableBlockLen; 
00085     bool _useNoiseCoding;      
00086 
00087     bool _resetBlockLengths; 
00088 
00089     int _curFrame;       
00090     int _frameLen;       
00091     int _frameLenBits;   
00092     int _blockSizeCount; 
00093     int _framePos;       
00094 
00095     int _curBlock;         
00096     int _blockLen;         
00097     int _blockLenBits;     
00098     int _nextBlockLenBits; 
00099     int _prevBlockLenBits; 
00100 
00101     int _byteOffsetBits;
00102 
00103     // Coefficients
00104     int    _coefsStart;                       
00105     int    _coefsEnd[kBlockNBSizes];          
00106     int    _exponentSizes[kBlockNBSizes];
00107     uint16 _exponentBands[kBlockNBSizes][25];
00108     int    _highBandStart[kBlockNBSizes];     
00109     int    _exponentHighSizes[kBlockNBSizes];
00110     int    _exponentHighBands[kBlockNBSizes][kHighBandSizeMax];
00111 
00112     Common::Huffman *_coefHuffman[2];                
00113     const WMACoefHuffmanParam *_coefHuffmanParam[2]; 
00114 
00115     uint16 *_coefHuffmanRunTable[2];   
00116     float  *_coefHuffmanLevelTable[2]; 
00117     uint16 *_coefHuffmanIntTable[2];   
00118 
00119     // Noise
00120     float _noiseMult;                 
00121     float _noiseTable[kNoiseTabSize]; 
00122     int   _noiseIndex;
00123 
00124     Common::Huffman *_hgainHuffman; 
00125 
00126     // Exponents
00127     int   _exponentsBSize[kChannelsMax];
00128     float _exponents[kChannelsMax][kBlockSizeMax];
00129     float _maxExponent[kChannelsMax];
00130 
00131     Common::Huffman *_expHuffman; 
00132 
00133     // Coded values in high bands
00134     bool _highBandCoded [kChannelsMax][kHighBandSizeMax];
00135     int  _highBandValues[kChannelsMax][kHighBandSizeMax];
00136 
00137     // Coefficients
00138     float _coefs1[kChannelsMax][kBlockSizeMax];
00139     float _coefs [kChannelsMax][kBlockSizeMax];
00140 
00141     // Line spectral pairs
00142     float _lspCosTable[kBlockSizeMax];
00143     float _lspPowETable[256];
00144     float _lspPowMTable1[(1 << kLSPPowBits)];
00145     float _lspPowMTable2[(1 << kLSPPowBits)];
00146 
00147     // MDCT
00148     Common::Array<Common::MDCT *> _mdct;       
00149     Common::Array<const float *>  _mdctWindow; 
00150 
00152     byte _lastSuperframe[kSuperframeSizeMax + 4];
00153     int  _lastSuperframeLen; 
00154     int  _lastBitoffset;     
00155 
00156     // Output
00157     float _output[kBlockSizeMax * 2];
00158     float _frameOut[kChannelsMax][kBlockSizeMax * 2];
00159 
00160 
00161     // Init helpers
00162 
00163     void init(Common::SeekableReadStream *extraData);
00164 
00165     uint16 getFlags(Common::SeekableReadStream *extraData);
00166     void evalFlags(uint16 flags, Common::SeekableReadStream *extraData);
00167     int getFrameBitLength();
00168     int getBlockSizeCount(uint16 flags);
00169     uint32 getNormalizedSampleRate();
00170     bool useNoiseCoding(float &highFreq, float &bps);
00171     void evalMDCTScales(float highFreq);
00172     void initNoise();
00173     void initCoefHuffman(float bps);
00174     void initMDCT();
00175     void initExponents();
00176 
00177     Common::Huffman *initCoefHuffman(uint16 *&runTable, float *&levelTable,
00178                                      uint16 *&intTable, const WMACoefHuffmanParam &params);
00179     void initLSPToCurve();
00180 
00181     // Decoding
00182 
00183     Common::SeekableReadStream *decodeSuperFrame(Common::SeekableReadStream &data);
00184     bool decodeFrame(Common::BitStream8MSB &bits, int16 *outputData);
00185     int decodeBlock(Common::BitStream8MSB &bits);
00186 
00187     // Decoding helpers
00188 
00189     bool evalBlockLength(Common::BitStream8MSB &bits);
00190     bool decodeChannels(Common::BitStream8MSB &bits, int bSize, bool msStereo, bool *hasChannel);
00191     bool calculateIMDCT(int bSize, bool msStereo, bool *hasChannel);
00192 
00193     void calculateCoefCount(int *coefCount, int bSize) const;
00194     bool decodeNoise(Common::BitStream8MSB &bits, int bSize, bool *hasChannel, int *coefCount);
00195     bool decodeExponents(Common::BitStream8MSB &bits, int bSize, bool *hasChannel);
00196     bool decodeSpectralCoef(Common::BitStream8MSB &bits, bool msStereo, bool *hasChannel,
00197                             int *coefCount, int coefBitCount);
00198     float getNormalizedMDCTLength() const;
00199     void calculateMDCTCoefficients(int bSize, bool *hasChannel,
00200                                    int *coefCount, int totalGain, float mdctNorm);
00201 
00202     bool decodeExpHuffman(Common::BitStream8MSB &bits, int ch);
00203     bool decodeExpLSP(Common::BitStream8MSB &bits, int ch);
00204     bool decodeRunLevel(Common::BitStream8MSB &bits, const Common::Huffman &huffman,
00205         const float *levelTable, const uint16 *runTable, int version, float *ptr,
00206         int offset, int numCoefs, int blockLen, int frameLenBits, int coefNbBits);
00207 
00208     void lspToCurve(float *out, float *val_max_ptr, int n, float *lsp);
00209 
00210     void window(float *out) const;
00211 
00212     float pow_m1_4(float x) const;
00213 
00214     static int readTotalGain(Common::BitStream8MSB &bits);
00215     static int totalGainToBits(int totalGain);
00216     static uint32 getLargeVal(Common::BitStream8MSB &bits);
00217 };
00218 
00219 } // End of namespace Audio
00220 
00221 #endif // AUDIO_DECODERS_WMA_H


Generated on Sat Feb 9 2019 05:00:44 for ResidualVM by doxygen 1.7.1
curved edge   curved edge