ScummVM API documentation
wma.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 // Based on xoreos' WMA code which is in turn
23 // Largely based on the WMA implementation found in FFmpeg.
24 
25 #ifndef AUDIO_DECODERS_WMA_H
26 #define AUDIO_DECODERS_WMA_H
27 
28 #include "common/array.h"
29 #include "common/bitstream.h"
30 
31 #include "audio/decoders/codec.h"
32 
33 namespace Common {
34 template <class BITSTREAM>
35 class Huffman;
36 }
37 
38 namespace Math {
39 class MDCT;
40 }
41 
42 namespace Audio {
43 
44 struct WMACoefHuffmanParam;
45 
46 class WMACodec : public Codec {
47 public:
48  WMACodec(int version, uint32 sampleRate, uint8 channels,
49  uint32 bitRate, uint32 blockAlign, Common::SeekableReadStream *extraData = 0);
50  ~WMACodec();
51 
52  AudioStream *decodeFrame(Common::SeekableReadStream &data);
53 
54 private:
55  static const int kChannelsMax = 2;
56 
57  static const int kBlockBitsMin = 7;
58  static const int kBlockBitsMax = 11;
59 
61  static const int kBlockSizeMax = (1 << kBlockBitsMax);
62 
63  static const int kBlockNBSizes = (kBlockBitsMax - kBlockBitsMin + 1);
64 
66  static const int kSuperframeSizeMax = 16384;
67 
69  static const int kHighBandSizeMax = 16;
70 
72  static const int kNoiseTabSize = 8192;
73 
75  static const int kLSPPowBits = 7;
76 
77  int _version;
78 
79  uint32 _sampleRate;
80  uint8 _channels;
81  uint32 _bitRate;
82  uint32 _blockAlign;
83  byte _audioFlags;
84 
85  bool _useExpHuffman;
86  bool _useBitReservoir;
87  bool _useVariableBlockLen;
88  bool _useNoiseCoding;
89 
90  bool _resetBlockLengths;
91 
92  int _curFrame;
93  int _frameLen;
94  int _frameLenBits;
95  int _blockSizeCount;
96  int _framePos;
97 
98  int _curBlock;
99  int _blockLen;
100  int _blockLenBits;
101  int _nextBlockLenBits;
102  int _prevBlockLenBits;
103 
104  int _byteOffsetBits;
105 
106  // Coefficients
107  int _coefsStart;
108  int _coefsEnd[kBlockNBSizes];
109  int _exponentSizes[kBlockNBSizes];
110  uint16 _exponentBands[kBlockNBSizes][25];
111  int _highBandStart[kBlockNBSizes];
112  int _exponentHighSizes[kBlockNBSizes];
113  int _exponentHighBands[kBlockNBSizes][kHighBandSizeMax];
114 
116  HuffmanDecoder *_coefHuffman[2];
117  const WMACoefHuffmanParam *_coefHuffmanParam[2];
118 
119  uint16 *_coefHuffmanRunTable[2];
120  float *_coefHuffmanLevelTable[2];
121  uint16 *_coefHuffmanIntTable[2];
122 
123  // Noise
124  float _noiseMult;
125  float _noiseTable[kNoiseTabSize];
126  int _noiseIndex;
127 
128  HuffmanDecoder *_hgainHuffman;
129 
130  // Exponents
131  int _exponentsBSize[kChannelsMax];
132  float _exponents[kChannelsMax][kBlockSizeMax];
133  float _maxExponent[kChannelsMax];
134 
135  HuffmanDecoder *_expHuffman;
136 
137  // Coded values in high bands
138  bool _highBandCoded [kChannelsMax][kHighBandSizeMax];
139  int _highBandValues[kChannelsMax][kHighBandSizeMax];
140 
141  // Coefficients
142  float _coefs1[kChannelsMax][kBlockSizeMax];
143  float _coefs [kChannelsMax][kBlockSizeMax];
144 
145  // Line spectral pairs
146  float _lspCosTable[kBlockSizeMax];
147  float _lspPowETable[256];
148  float _lspPowMTable1[(1 << kLSPPowBits)];
149  float _lspPowMTable2[(1 << kLSPPowBits)];
150 
151  // MDCT
153  Common::Array<const float *> _mdctWindow;
154 
156  byte _lastSuperframe[kSuperframeSizeMax + 4];
157  int _lastSuperframeLen;
158  int _lastBitoffset;
159 
160  // Output
161  float _output[kBlockSizeMax * 2];
162  float _frameOut[kChannelsMax][kBlockSizeMax * 2];
163 
164 
165  // Init helpers
166 
167  void init(Common::SeekableReadStream *extraData);
168 
169  uint16 getFlags(Common::SeekableReadStream *extraData);
170  void evalFlags(uint16 flags, Common::SeekableReadStream *extraData);
171  int getFrameBitLength();
172  int getBlockSizeCount(uint16 flags);
173  uint32 getNormalizedSampleRate();
174  bool useNoiseCoding(float &highFreq, float &bps);
175  void evalMDCTScales(float highFreq);
176  void initNoise();
177  void initCoefHuffman(float bps);
178  void initMDCT();
179  void initExponents();
180 
181  HuffmanDecoder *initCoefHuffman(uint16 *&runTable, float *&levelTable,
182  uint16 *&intTable, const WMACoefHuffmanParam &params);
183  void initLSPToCurve();
184 
185  // Decoding
186 
188  bool decodeFrame(Common::BitStream8MSB &bits, int16 *outputData);
189  int decodeBlock(Common::BitStream8MSB &bits);
190 
191  // Decoding helpers
192 
193  bool evalBlockLength(Common::BitStream8MSB &bits);
194  bool decodeChannels(Common::BitStream8MSB &bits, int bSize, bool msStereo, bool *hasChannel);
195  bool calculateIMDCT(int bSize, bool msStereo, bool *hasChannel);
196 
197  void calculateCoefCount(int *coefCount, int bSize) const;
198  bool decodeNoise(Common::BitStream8MSB &bits, int bSize, bool *hasChannel, int *coefCount);
199  bool decodeExponents(Common::BitStream8MSB &bits, int bSize, bool *hasChannel);
200  bool decodeSpectralCoef(Common::BitStream8MSB &bits, bool msStereo, bool *hasChannel,
201  int *coefCount, int coefBitCount);
202  float getNormalizedMDCTLength() const;
203  void calculateMDCTCoefficients(int bSize, bool *hasChannel,
204  int *coefCount, int totalGain, float mdctNorm);
205 
206  bool decodeExpHuffman(Common::BitStream8MSB &bits, int ch);
207  bool decodeExpLSP(Common::BitStream8MSB &bits, int ch);
208  bool decodeRunLevel(Common::BitStream8MSB &bits, const HuffmanDecoder &huffman,
209  const float *levelTable, const uint16 *runTable, int version, float *ptr,
210  int offset, int numCoefs, int blockLen, int frameLenBits, int coefNbBits);
211 
212  void lspToCurve(float *out, float *val_max_ptr, int n, float *lsp);
213 
214  void window(float *out) const;
215 
216  float pow_m1_4(float x) const;
217 
218  static int readTotalGain(Common::BitStream8MSB &bits);
219  static int totalGainToBits(int totalGain);
220  static uint32 getLargeVal(Common::BitStream8MSB &bits);
221 };
222 
223 } // End of namespace Audio
224 
225 #endif // AUDIO_DECODERS_WMA_H
Definition: array.h:52
Definition: stream.h:745
Definition: wma.h:38
Definition: codec.h:40
Definition: huffman.h:59
Definition: bitstream.h:55
Definition: algorithm.h:29
Definition: audiostream.h:50
Definition: wmadata.h:1345
Definition: wma.h:46
Definition: system.h:38