Skip to content

Commit

Permalink
SOUND: Use ScopedPtr/ScopedArray in the WMA decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Oct 26, 2016
1 parent 2baf890 commit 188b76f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 60 deletions.
81 changes: 29 additions & 52 deletions src/sound/decoders/wma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,10 @@ WMACodec::WMACodec(int version, uint32 sampleRate, uint8 channels,
_resetBlockLengths(true), _curFrame(0), _frameLen(0), _frameLenBits(0),
_blockSizeCount(0), _framePos(0), _curBlock(0), _blockLen(0), _blockLenBits(0),
_nextBlockLenBits(0), _prevBlockLenBits(0), _byteOffsetBits(0),
_hgainHuffman(0), _expHuffman(0), _lastSuperframeLen(0), _lastBitoffset(0) {
_lastSuperframeLen(0), _lastBitoffset(0) {

for (int i = 0; i < 2; i++) {
_coefHuffman[i] = 0;

_coefHuffmanRunTable [i] = 0;
_coefHuffmanLevelTable[i] = 0;
_coefHuffmanIntTable [i] = 0;
}
for (int i = 0; i < 2; i++)
_coefHuffmanParam[i] = 0;

if ((_version != 1) && (_version != 2))
throw Common::Exception("WMACodec::init(): Unsupported WMA version %d", _version);
Expand All @@ -131,17 +126,6 @@ WMACodec::WMACodec(int version, uint32 sampleRate, uint8 channels,
}

WMACodec::~WMACodec() {
delete _expHuffman;
delete _hgainHuffman;

for (int i = 0; i < 2; i++) {
delete[] _coefHuffmanRunTable [i];
delete[] _coefHuffmanLevelTable[i];
delete[] _coefHuffmanIntTable [i];

delete _coefHuffman[i];
}

for (std::vector<Common::MDCT *>::iterator m = _mdct.begin(); m != _mdct.end(); ++m)
delete *m;
}
Expand Down Expand Up @@ -460,8 +444,7 @@ void WMACodec::initNoise() {
_noiseTable[i] = (float)((int)seed) * norm;
}

_hgainHuffman = new Common::Huffman(0, ARRAYSIZE(hgainHuffCodes),
hgainHuffCodes, hgainHuffBits);
_hgainHuffman.reset(new Common::Huffman(0, ARRAYSIZE(hgainHuffCodes), hgainHuffCodes, hgainHuffBits));
}

void WMACodec::initCoefHuffman(float bps) {
Expand All @@ -478,10 +461,10 @@ void WMACodec::initCoefHuffman(float bps) {
_coefHuffmanParam[0] = &coefHuffmanParam[coefHuffTable * 2 ];
_coefHuffmanParam[1] = &coefHuffmanParam[coefHuffTable * 2 + 1];

_coefHuffman[0] = initCoefHuffman(_coefHuffmanRunTable[0], _coefHuffmanLevelTable[0],
_coefHuffmanIntTable[0], *_coefHuffmanParam[0]);
_coefHuffman[1] = initCoefHuffman(_coefHuffmanRunTable[1], _coefHuffmanLevelTable[1],
_coefHuffmanIntTable[1], *_coefHuffmanParam[1]);
_coefHuffman[0].reset(initCoefHuffman(_coefHuffmanRunTable[0], _coefHuffmanLevelTable[0],
_coefHuffmanIntTable[0], *_coefHuffmanParam[0]));
_coefHuffman[1].reset(initCoefHuffman(_coefHuffmanRunTable[1], _coefHuffmanLevelTable[1],
_coefHuffmanIntTable[1], *_coefHuffmanParam[1]));
}

void WMACodec::initMDCT() {
Expand All @@ -497,23 +480,24 @@ void WMACodec::initMDCT() {

void WMACodec::initExponents() {
if (_useExpHuffman)
_expHuffman = new Common::Huffman(0, ARRAYSIZE(scaleHuffCodes),
scaleHuffCodes, scaleHuffBits);
_expHuffman.reset(new Common::Huffman(0, ARRAYSIZE(scaleHuffCodes), scaleHuffCodes, scaleHuffBits));
else
initLSPToCurve();
}

Common::Huffman *WMACodec::initCoefHuffman(uint16 *&runTable, float *&levelTable,
uint16 *&intTable, const WMACoefHuffmanParam &params) {
Common::Huffman *WMACodec::initCoefHuffman(Common::ScopedArray<uint16> &runTable,
Common::ScopedArray<float> &levelTable,
Common::ScopedArray<uint16> &intTable,
const WMACoefHuffmanParam &params) {

Common::Huffman *huffman =
new Common::Huffman(0, params.n, params.huffCodes, params.huffBits);

runTable = new uint16[params.n];
levelTable = new float[params.n];
intTable = new uint16[params.n];
runTable.reset (new uint16[params.n]);
levelTable.reset(new float[params.n]);
intTable.reset (new uint16[params.n]);

uint16 *iLevelTable = new uint16[params.n];
Common::ScopedArray<uint16> iLevelTable(new uint16[params.n]);

int i = 2;
int level = 1;
Expand All @@ -535,8 +519,6 @@ Common::Huffman *WMACodec::initCoefHuffman(uint16 *&runTable, float *&levelTable
level++;
}

delete[] iLevelTable;

return huffman;
}

Expand Down Expand Up @@ -588,8 +570,8 @@ Common::SeekableReadStream *WMACodec::decodeSuperFrame(Common::SeekableReadStrea

Common::BitStream8MSB bits(data);

int outputDataSize = 0;
int16 *outputData = 0;
int outputDataSize = 0;
Common::ScopedArray<int16> outputData;

_curFrame = 0;

Expand Down Expand Up @@ -617,9 +599,9 @@ Common::SeekableReadStream *WMACodec::decodeSuperFrame(Common::SeekableReadStrea

// PCM output data
outputDataSize = frameCount * _channels * _frameLen;
outputData = new int16[outputDataSize];
outputData.reset(new int16[outputDataSize]);

std::memset(outputData, 0, outputDataSize * 2);
std::memset(outputData.get(), 0, outputDataSize * 2);

// Number of bits data that completes the last superframe's overhang.
int bitOffset = bits.getBits(_byteOffsetBits + 3);
Expand Down Expand Up @@ -650,7 +632,7 @@ Common::SeekableReadStream *WMACodec::decodeSuperFrame(Common::SeekableReadStrea

lastBits.skip(_lastBitoffset);

decodeFrame(lastBits, outputData);
decodeFrame(lastBits, outputData.get());

_curFrame++;
}
Expand All @@ -662,12 +644,9 @@ Common::SeekableReadStream *WMACodec::decodeSuperFrame(Common::SeekableReadStrea
_resetBlockLengths = true;

// Decode the frames
for (int i = 0; i < newFrameCount; i++, _curFrame++) {
if (!decodeFrame(bits, outputData)) {
delete[] outputData;
for (int i = 0; i < newFrameCount; i++, _curFrame++)
if (!decodeFrame(bits, outputData.get()))
return 0;
}
}

// Check if we've got new overhang data
int remainingBits = bits.size() - bits.pos();
Expand All @@ -694,15 +673,13 @@ Common::SeekableReadStream *WMACodec::decodeSuperFrame(Common::SeekableReadStrea

// PCM output data
outputDataSize = _channels * _frameLen;
outputData = new int16[outputDataSize];
outputData.reset(new int16[outputDataSize]);

std::memset(outputData, 0, outputDataSize * 2);
std::memset(outputData.get(), 0, outputDataSize * 2);

// Decode the frame
if (!decodeFrame(bits, outputData)) {
delete[] outputData;
if (!decodeFrame(bits, outputData.get()))
return 0;
}
}

// And return our PCM output data as a stream, if available
Expand All @@ -711,7 +688,7 @@ Common::SeekableReadStream *WMACodec::decodeSuperFrame(Common::SeekableReadStrea
return 0;

// TODO: This might be a problem alignment-wise?
return new Common::MemoryReadStream(reinterpret_cast<byte *>(outputData), outputDataSize * 2, true);
return new Common::MemoryReadStream(reinterpret_cast<byte *>(outputData.release()), outputDataSize * 2, true);
}

bool WMACodec::decodeFrame(Common::BitStream &bits, int16 *outputData) {
Expand Down Expand Up @@ -1017,7 +994,7 @@ bool WMACodec::decodeSpectralCoef(Common::BitStream &bits, bool msStereo, bool *
std::memset(ptr, 0, _blockLen * sizeof(float));

if (!decodeRunLevel(bits, *_coefHuffman[tindex],
_coefHuffmanLevelTable[tindex], _coefHuffmanRunTable[tindex],
_coefHuffmanLevelTable[tindex].get(), _coefHuffmanRunTable[tindex].get(),
0, ptr, 0, coefCount[i], _blockLen, _frameLenBits, coefBitCount))
return false;
}
Expand Down
22 changes: 14 additions & 8 deletions src/sound/decoders/wma.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@

#include <vector>

#include "src/common/types.h"
#include "src/common/scopedptr.h"

#include "src/sound/decoders/codec.h"

namespace Common {
Expand Down Expand Up @@ -134,26 +137,27 @@ class WMACodec : public Codec {
int _exponentHighSizes[kBlockNBSizes];
int _exponentHighBands[kBlockNBSizes][kHighBandSizeMax];

Common::Huffman *_coefHuffman[2]; ///< Coefficients Huffman codes.
Common::ScopedPtr<Common::Huffman> _coefHuffman[2]; ///< Coefficients Huffman codes.

const WMACoefHuffmanParam *_coefHuffmanParam[2]; ///< Params for coef Huffman codes.

uint16 *_coefHuffmanRunTable[2]; ///< Run table for the coef Huffman.
float *_coefHuffmanLevelTable[2]; ///< Level table for the coef Huffman.
uint16 *_coefHuffmanIntTable[2]; ///< Int table for the coef Huffman.
Common::ScopedArray<uint16> _coefHuffmanRunTable[2]; ///< Run table for the coef Huffman.
Common::ScopedArray<float> _coefHuffmanLevelTable[2]; ///< Level table for the coef Huffman.
Common::ScopedArray<uint16> _coefHuffmanIntTable[2]; ///< Int table for the coef Huffman.

// Noise
float _noiseMult; ///< Noise multiplier.
float _noiseTable[kNoiseTabSize]; ///< Noise table.
int _noiseIndex;

Common::Huffman *_hgainHuffman; ///< Perceptual noise Huffman code.
Common::ScopedPtr<Common::Huffman> _hgainHuffman; ///< Perceptual noise Huffman code.

// Exponents
int _exponentsBSize[kChannelsMax];
float _exponents[kChannelsMax][kBlockSizeMax];
float _maxExponent[kChannelsMax];

Common::Huffman *_expHuffman; ///< Exponents Huffman code.
Common::ScopedPtr<Common::Huffman> _expHuffman; ///< Exponents Huffman code.

// Coded values in high bands
bool _highBandCoded [kChannelsMax][kHighBandSizeMax];
Expand Down Expand Up @@ -199,8 +203,10 @@ class WMACodec : public Codec {
void initMDCT();
void initExponents();

Common::Huffman *initCoefHuffman(uint16 *&runTable, float *&levelTable,
uint16 *&intTable, const WMACoefHuffmanParam &params);
Common::Huffman *initCoefHuffman(Common::ScopedArray<uint16> &runTable,
Common::ScopedArray<float> &levelTable,
Common::ScopedArray<uint16> &intTable,
const WMACoefHuffmanParam &params);
void initLSPToCurve();

// Decoding
Expand Down

0 comments on commit 188b76f

Please sign in to comment.