Skip to content

Commit

Permalink
Reduce globals. Only AACDecoder is available externally now.
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jul 1, 2012
1 parent ed0932b commit 9bce9e7
Show file tree
Hide file tree
Showing 9 changed files with 2,471 additions and 2,385 deletions.
248 changes: 127 additions & 121 deletions src/cce.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,140 @@
function CCEElement(frameLength) {
this.ics = new ICStream(frameLength);
this.channelPair = new Array(8);
this.idSelect = new Int32Array(8);
this.chSelect = new Int32Array(8);
this.gain = new Array(16);
}

CCEElement.BEFORE_TNS = 0;
CCEElement.AFTER_TNS = 1;
CCEElement.AFTER_IMDCT = 2;

const CCE_SCALE = new Float32Array([
1.09050773266525765921,
1.18920711500272106672,
1.4142135623730950488016887,
2.0
]);

CCEElement.prototype = {
decode: function(stream, config) {
var channelPair = this.channelPair,
idSelect = this.idSelect,
chSelect = this.chSelect;

this.couplingPoint = 2 * stream.readOne();
this.coupledCount = stream.readSmall(3);

var gainCount = 0;
for (var i = 0; i <= this.coupledCount; i++) {
gainCount++;
channelPair[i] = stream.readOne();
idSelect[i] = stream.readSmall(4);

if (channelPair[i]) {
chSelect[i] = stream.readSmall(2);
if (chSelect[i] === 3)
gainCount++;

} else {
chSelect[i] = 2;
}
}

this.couplingPoint += stream.readOne() || (this.couplingPoint >>> 1);

var sign = stream.readOne(),
scale = CCE_SCALE[stream.readSmall(2)];

this.ics.decode(stream, config, false);

var groupCount = this.ics.info.groupCount,
maxSFB = this.ics.info.maxSFB,
bandTypes = this.ics.bandTypes;

for (var i = 0; i < gainCount; i++) {
var idx = 0,
cge = 1,
gain = 0,
gainCache = 1;

if (i > 0) {
cge = this.couplingPoint === CCEElement.AFTER_IMDCT ? 1 : stream.readOne();
gain = cge ? Huffman.decodeScaleFactor(stream) - 60 : 0;
gainCache = Math.pow(scale, -gain);
var CCEElement = (function() {

function CCEElement(config) {
this.ics = new ICStream(config);
this.channelPair = new Array(8);
this.idSelect = new Int32Array(8);
this.chSelect = new Int32Array(8);
this.gain = new Array(16);
}

CCEElement.BEFORE_TNS = 0;
CCEElement.AFTER_TNS = 1;
CCEElement.AFTER_IMDCT = 2;

const CCE_SCALE = new Float32Array([
1.09050773266525765921,
1.18920711500272106672,
1.4142135623730950488016887,
2.0
]);

CCEElement.prototype = {
decode: function(stream, config) {
var channelPair = this.channelPair,
idSelect = this.idSelect,
chSelect = this.chSelect;

this.couplingPoint = 2 * stream.readOne();
this.coupledCount = stream.readSmall(3);

var gainCount = 0;
for (var i = 0; i <= this.coupledCount; i++) {
gainCount++;
channelPair[i] = stream.readOne();
idSelect[i] = stream.readSmall(4);

if (channelPair[i]) {
chSelect[i] = stream.readSmall(2);
if (chSelect[i] === 3)
gainCount++;

} else {
chSelect[i] = 2;
}
}

var gain_i = this.gain[i] = new Float32Array(120);

if (this.couplingPoint === CCEElement.AFTER_IMDCT) {
gain_i[0] = gainCache;
} else {
for (var g = 0; g < groupCount; g++) {
for (var sfb = 0; sfb < maxSFB; sfb++) {
if (bandTypes[idx] !== ZERO_BT) {
if (cge === 0) {
var t = Huffman.decodeScaleFactor(stream) - 60;
if (t !== 0) {
var s = 1;
t = gain += t;
if (sign) {
s -= 2 * (t * 0x1);
t >>>= 1;

this.couplingPoint += stream.readOne() || (this.couplingPoint >>> 1);

var sign = stream.readOne(),
scale = CCE_SCALE[stream.readSmall(2)];

this.ics.decode(stream, config, false);

var groupCount = this.ics.info.groupCount,
maxSFB = this.ics.info.maxSFB,
bandTypes = this.ics.bandTypes;

for (var i = 0; i < gainCount; i++) {
var idx = 0,
cge = 1,
gain = 0,
gainCache = 1;

if (i > 0) {
cge = this.couplingPoint === CCEElement.AFTER_IMDCT ? 1 : stream.readOne();
gain = cge ? Huffman.decodeScaleFactor(stream) - 60 : 0;
gainCache = Math.pow(scale, -gain);
}

var gain_i = this.gain[i] = new Float32Array(120);

if (this.couplingPoint === CCEElement.AFTER_IMDCT) {
gain_i[0] = gainCache;
} else {
for (var g = 0; g < groupCount; g++) {
for (var sfb = 0; sfb < maxSFB; sfb++) {
if (bandTypes[idx] !== ICStream.ZERO_BT) {
if (cge === 0) {
var t = Huffman.decodeScaleFactor(stream) - 60;
if (t !== 0) {
var s = 1;
t = gain += t;
if (sign) {
s -= 2 * (t * 0x1);
t >>>= 1;
}
gainCache = Math.pow(scale, -t) * s;
}
gainCache = Math.pow(scale, -t) * s;
}
gain_i[idx++] = gainCache;
}
gain_i[idx++] = gainCache;
}
}
}
}
}
},

applyIndependentCoupling: function(index, data) {
var gain = this.gain[index][0],
iqData = this.ics.data;

for (var i = 0; i < data.length; i++) {
data[i] += gain * iqData[i];
}
},

applyDependentCoupling: function(index, data) {
var info = this.ics.info,
swbOffsets = info.swbOffsets,
groupCount = info.groupCount,
maxSFB = info.maxSFB,
bandTypes = this.ics.bandTypes,
iqData = this.ics.data;

var idx = 0,
offset = 0,
gains = this.gain[index];

for (var g = 0; g < groupCount; g++) {
var len = info.groupLength[g];

for (var sfb = 0; sfb < maxSFB; sfb++, idx++) {
if (bandTypes[idx] !== ZERO_BT) {
var gain = gains[idx];
for (var group = 0; group < len; group++) {
for (var k = swbOffsets[sfb]; k < swbOffsets[swb + 1]; k++) {
data[offset + group * 128 + k] += gain * iqData[offset + group * 128 + k];
},

applyIndependentCoupling: function(index, data) {
var gain = this.gain[index][0],
iqData = this.ics.data;

for (var i = 0; i < data.length; i++) {
data[i] += gain * iqData[i];
}
},

applyDependentCoupling: function(index, data) {
var info = this.ics.info,
swbOffsets = info.swbOffsets,
groupCount = info.groupCount,
maxSFB = info.maxSFB,
bandTypes = this.ics.bandTypes,
iqData = this.ics.data;

var idx = 0,
offset = 0,
gains = this.gain[index];

for (var g = 0; g < groupCount; g++) {
var len = info.groupLength[g];

for (var sfb = 0; sfb < maxSFB; sfb++, idx++) {
if (bandTypes[idx] !== ICStream.ZERO_BT) {
var gain = gains[idx];
for (var group = 0; group < len; group++) {
for (var k = swbOffsets[sfb]; k < swbOffsets[swb + 1]; k++) {
data[offset + group * 128 + k] += gain * iqData[offset + group * 128 + k];
}
}
}
}

offset += len * 128;
}

offset += len * 128;
}
}
}
};

return CCEElement;

})();
106 changes: 54 additions & 52 deletions src/cpe.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,60 @@
//import "ics.js"

const MAX_MS_MASK = 128;

const MASK_TYPE_ALL_0 = 0,
MASK_TYPE_USED = 1,
MASK_TYPE_ALL_1 = 2,
MASK_TYPE_RESERVED = 3;

/*
* CPEElement - represents a channel pair element
* Table 4.5
*/
function CPEElement(frameLength) {
this.ms_used = [];
this.left = new ICStream(frameLength);
this.right = new ICStream(frameLength);
}

CPEElement.prototype.decode = function(stream, config) {
var left = this.left,
right = this.right,
ms_used = this.ms_used;

if (this.commonWindow = !!stream.readOne()) {
left.info.decode(stream, config, true);
right.info.set(left.info)

var mask = stream.readSmall(2);
this.maskPresent = !!mask;

switch (mask) {
case MASK_TYPE_USED:
var len = left.info.groupCount * left.info.maxSFB;
for (var i = 0; i < len; i++) {
ms_used[i] = !!stream.readOne();
}
break;
var CPEElement = (function() {

function CPEElement(config) {
this.ms_used = [];
this.left = new ICStream(config);
this.right = new ICStream(config);
}

const MAX_MS_MASK = 128;

const MASK_TYPE_ALL_0 = 0,
MASK_TYPE_USED = 1,
MASK_TYPE_ALL_1 = 2,
MASK_TYPE_RESERVED = 3;

CPEElement.prototype.decode = function(stream, config) {
var left = this.left,
right = this.right,
ms_used = this.ms_used;

if (this.commonWindow = !!stream.readOne()) {
left.info.decode(stream, config, true);
right.info.set(left.info)

var mask = stream.readSmall(2);
this.maskPresent = !!mask;

case MASK_TYPE_ALL_0:
case MASK_TYPE_ALL_1:
var val = !!mask;
for (var i = 0; i < MAX_MS_MASK; i++) {
ms_used[i] = val;
}
break;
switch (mask) {
case MASK_TYPE_USED:
var len = left.info.groupCount * left.info.maxSFB;
for (var i = 0; i < len; i++) {
ms_used[i] = !!stream.readOne();
}
break;

default:
throw new Error("Reserved ms mask type: " + mask);
case MASK_TYPE_ALL_0:
case MASK_TYPE_ALL_1:
var val = !!mask;
for (var i = 0; i < MAX_MS_MASK; i++) {
ms_used[i] = val;
}
break;

default:
throw new Error("Reserved ms mask type: " + mask);
}
} else {
for (var i = 0; i < MAX_MS_MASK; i++)
ms_used[i] = false;
}
} else {
for (var i = 0; i < MAX_MS_MASK; i++)
ms_used[i] = false;
}

left.decode(stream, config, this.commonWindow);
right.decode(stream, config, this.commonWindow);
};

return CPEElement;

left.decode(stream, config, this.commonWindow);
right.decode(stream, config, this.commonWindow);
}
})();
Loading

0 comments on commit 9bce9e7

Please sign in to comment.