Skip to content

Commit 4854b97

Browse files
committed
Add better documentation for how MTL and LTM modes work for BitStream
1 parent 95d630a commit 4854b97

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

build/io/bitstream-def.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,34 @@
1818
* Note that this stream is optimized, and thus, will *NOT* throw an error if
1919
* the end of the stream is reached. Only use this in scenarios where you
2020
* already have all the bits you need.
21+
*
22+
* Bit reading always proceeds from the first byte in the buffer, to the
23+
* second byte, and so on. The MTL flag controls which bit is considered
24+
* first *inside* the byte.
25+
*
26+
* An Example for how Most-To-Least vs Least-to-Most mode works:
27+
*
28+
* If you have an ArrayBuffer with the following two Uint8s:
29+
* 185 (0b10111001) and 66 (0b01000010)
30+
* and you perform a series of readBits: 2 bits, then 3, then 5, then 6.
31+
*
32+
* A BitStream in "mtl" mode will yield the following:
33+
* - readBits(2) => 2 ('10')
34+
* - readBits(3) => 7 ('111')
35+
* - readBits(5) => 5 ('00101')
36+
* - readBits(6) => 2 ('000010')
37+
*
38+
* A BitStream in "ltm" mode will yield the following:
39+
* - readBits(2) => 1 ('01')
40+
* - readBits(3) => 6 ('110')
41+
* - readBits(5) => 21 ('10101')
42+
* - readBits(6) => 16 ('010000')
2143
*/
2244
class BitStream {
2345
/**
2446
* @param {ArrayBuffer} ab An ArrayBuffer object or a Uint8Array.
2547
* @param {boolean} mtl Whether the stream reads bits from the byte starting with the
26-
* most-significant-bit (bit 7) to least-significant (bit 0). False means the direciton is
48+
* most-significant-bit (bit 7) to least-significant (bit 0). False means the direction is
2749
* from least-significant-bit (bit 0) to most-significant (bit 7).
2850
* @param {Number} opt_offset The offset into the ArrayBuffer
2951
* @param {Number} opt_length The length of this BitStream
@@ -69,13 +91,15 @@
6991

7092
/**
7193
* Returns how many bites have been read in the stream since the beginning of time.
94+
* @returns {number}
7295
*/
7396
getNumBitsRead() {
7497
return this.bitsRead_;
7598
}
7699

77100
/**
78101
* Returns how many bits are currently in the stream left to be read.
102+
* @returns {number}
79103
*/
80104
getNumBitsLeft() {
81105
const bitsLeftInByte = 8 - this.bitPtr;
@@ -201,6 +225,7 @@
201225
* Bit at (bytePtr,bitPtr) has the highest position in returning data.
202226
* Taken from getbits.hpp in unrar.
203227
* TODO: Move this out of BitStream and into unrar.
228+
* @returns {number}
204229
*/
205230
getBits() {
206231
return (((((this.bytes[this.bytePtr] & 0xff) << 16) +

io/bitstream-worker.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,34 @@ bitjs.io.BitStream =
2222
* Note that this stream is optimized, and thus, will *NOT* throw an error if
2323
* the end of the stream is reached. Only use this in scenarios where you
2424
* already have all the bits you need.
25+
*
26+
* Bit reading always proceeds from the first byte in the buffer, to the
27+
* second byte, and so on. The MTL flag controls which bit is considered
28+
* first *inside* the byte.
29+
*
30+
* An Example for how Most-To-Least vs Least-to-Most mode works:
31+
*
32+
* If you have an ArrayBuffer with the following two Uint8s:
33+
* 185 (0b10111001) and 66 (0b01000010)
34+
* and you perform a series of readBits: 2 bits, then 3, then 5, then 6.
35+
*
36+
* A BitStream in "mtl" mode will yield the following:
37+
* - readBits(2) => 2 ('10')
38+
* - readBits(3) => 7 ('111')
39+
* - readBits(5) => 5 ('00101')
40+
* - readBits(6) => 2 ('000010')
41+
*
42+
* A BitStream in "ltm" mode will yield the following:
43+
* - readBits(2) => 1 ('01')
44+
* - readBits(3) => 6 ('110')
45+
* - readBits(5) => 21 ('10101')
46+
* - readBits(6) => 16 ('010000')
2547
*/
2648
class BitStream {
2749
/**
2850
* @param {ArrayBuffer} ab An ArrayBuffer object or a Uint8Array.
2951
* @param {boolean} mtl Whether the stream reads bits from the byte starting with the
30-
* most-significant-bit (bit 7) to least-significant (bit 0). False means the direciton is
52+
* most-significant-bit (bit 7) to least-significant (bit 0). False means the direction is
3153
* from least-significant-bit (bit 0) to most-significant (bit 7).
3254
* @param {Number} opt_offset The offset into the ArrayBuffer
3355
* @param {Number} opt_length The length of this BitStream
@@ -73,13 +95,15 @@ bitjs.io.BitStream =
7395

7496
/**
7597
* Returns how many bites have been read in the stream since the beginning of time.
98+
* @returns {number}
7699
*/
77100
getNumBitsRead() {
78101
return this.bitsRead_;
79102
}
80103

81104
/**
82105
* Returns how many bits are currently in the stream left to be read.
106+
* @returns {number}
83107
*/
84108
getNumBitsLeft() {
85109
const bitsLeftInByte = 8 - this.bitPtr;
@@ -205,6 +229,7 @@ bitjs.io.BitStream =
205229
* Bit at (bytePtr,bitPtr) has the highest position in returning data.
206230
* Taken from getbits.hpp in unrar.
207231
* TODO: Move this out of BitStream and into unrar.
232+
* @returns {number}
208233
*/
209234
getBits() {
210235
return (((((this.bytes[this.bytePtr] & 0xff) << 16) +

io/bitstream.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,34 @@ export const BitStream =
2020
* Note that this stream is optimized, and thus, will *NOT* throw an error if
2121
* the end of the stream is reached. Only use this in scenarios where you
2222
* already have all the bits you need.
23+
*
24+
* Bit reading always proceeds from the first byte in the buffer, to the
25+
* second byte, and so on. The MTL flag controls which bit is considered
26+
* first *inside* the byte.
27+
*
28+
* An Example for how Most-To-Least vs Least-to-Most mode works:
29+
*
30+
* If you have an ArrayBuffer with the following two Uint8s:
31+
* 185 (0b10111001) and 66 (0b01000010)
32+
* and you perform a series of readBits: 2 bits, then 3, then 5, then 6.
33+
*
34+
* A BitStream in "mtl" mode will yield the following:
35+
* - readBits(2) => 2 ('10')
36+
* - readBits(3) => 7 ('111')
37+
* - readBits(5) => 5 ('00101')
38+
* - readBits(6) => 2 ('000010')
39+
*
40+
* A BitStream in "ltm" mode will yield the following:
41+
* - readBits(2) => 1 ('01')
42+
* - readBits(3) => 6 ('110')
43+
* - readBits(5) => 21 ('10101')
44+
* - readBits(6) => 16 ('010000')
2345
*/
2446
class BitStream {
2547
/**
2648
* @param {ArrayBuffer} ab An ArrayBuffer object or a Uint8Array.
2749
* @param {boolean} mtl Whether the stream reads bits from the byte starting with the
28-
* most-significant-bit (bit 7) to least-significant (bit 0). False means the direciton is
50+
* most-significant-bit (bit 7) to least-significant (bit 0). False means the direction is
2951
* from least-significant-bit (bit 0) to most-significant (bit 7).
3052
* @param {Number} opt_offset The offset into the ArrayBuffer
3153
* @param {Number} opt_length The length of this BitStream
@@ -71,13 +93,15 @@ export const BitStream =
7193

7294
/**
7395
* Returns how many bites have been read in the stream since the beginning of time.
96+
* @returns {number}
7497
*/
7598
getNumBitsRead() {
7699
return this.bitsRead_;
77100
}
78101

79102
/**
80103
* Returns how many bits are currently in the stream left to be read.
104+
* @returns {number}
81105
*/
82106
getNumBitsLeft() {
83107
const bitsLeftInByte = 8 - this.bitPtr;
@@ -203,6 +227,7 @@ export const BitStream =
203227
* Bit at (bytePtr,bitPtr) has the highest position in returning data.
204228
* Taken from getbits.hpp in unrar.
205229
* TODO: Move this out of BitStream and into unrar.
230+
* @returns {number}
206231
*/
207232
getBits() {
208233
return (((((this.bytes[this.bytePtr] & 0xff) << 16) +

tests/io-bitstream.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('bitjs.io.BitStream', () => {
2020
});
2121

2222
it('BitPeekAndRead_RTL', () => {
23-
const stream = new BitStream(array.buffer, true /* rtl */);
23+
const stream = new BitStream(array.buffer, true /* mtl */);
2424
// 0110 = 2 + 4 = 6
2525
expect(stream.readBits(4)).equals(6);
2626
// 0101 011 = 1 + 2 + 8 + 32 = 43
@@ -35,7 +35,7 @@ describe('bitjs.io.BitStream', () => {
3535
});
3636

3737
it('BitPeekAndRead_LTR', () => {
38-
const stream = new BitStream(array.buffer, false /* rtl */);
38+
const stream = new BitStream(array.buffer, false /* mtl */);
3939

4040
// 0101 = 2 + 4 = 6
4141
expect(stream.peekBits(4)).equals(5);

0 commit comments

Comments
 (0)