forked from glinscott/lzmajs
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Prototyping a clean, fast LZMA encoder in JS
- Loading branch information
0 parents
commit eff9f78
Showing
9 changed files
with
4,702 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
function BitEncoder() { | ||
var kNumBitModelTotalBits = 11; | ||
var kBitModelTotal = 1 << kNumBitModelTotalBits; | ||
var kNumMoveBits = 5; | ||
var kNumMoveReducingBits = 2; | ||
var kNumBitPriceShiftBits = 6; | ||
|
||
var prob; | ||
this.prototype.probPrices = []; | ||
|
||
this.init = function() { | ||
prob = kBitModelTotal >> 1; | ||
}; | ||
|
||
this.updateModel = function(symbol) { | ||
if (symbol == 0) | ||
prob += (kBitModelTotal - prob) >>> kNumMoveBits; | ||
else | ||
prob -= prob >>> kNumMoveBits; | ||
}; | ||
|
||
this.encode = function(encoder, symbol) { | ||
encoder.encodeBit(prob, kNumBitModelTotalBits, symbol); | ||
this.updateModel(symbol); | ||
}; | ||
|
||
this.getPrice = function(symbol) { | ||
var priceIndex = ((prob - symbol) ^ (-symbol)) & (kBitModelTotal - 1); | ||
return this.probPrices[priceIndex >>> kNumMoveReducingBits]; | ||
}; | ||
|
||
var initializeProbPrices = function() { | ||
var kNumBits = kNumBitModelTotalBits - kNumMoveReducingBits; | ||
for (var i = kNumBits - 1; i >= 0; i--) { | ||
var start = 1 << (kNumBits - i - 1); | ||
var end = 1 << (kNumBits - i); | ||
for (var j = start; j < end; j++) | ||
this.probPrices[j] = (i << kNumBitPriceShiftBits) + | ||
(((end - j) << kNumBitPriceShiftBits) >>> (kNumBits - i - 1)); | ||
} | ||
}; | ||
|
||
if (this.probPrices.length == 0) | ||
initializeProbPrices(); | ||
}; |
Oops, something went wrong.