exp-golomb.js
4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(function() {
window.videojs.hls.ExpGolomb = function() {};
/*
public class ExpGolomb
{
private var workingData:ByteArray;
private var workingWord:uint;
private var workingbBitsAvailable:uint;
public function ExpGolomb(pData:ByteArray)
{
workingData = pData;
workingData.position = 0;
loadWord();
}
public function length():uint
{
return ( 8 * workingData.length );
}
public function bitsAvailable():uint
{
return ( 8 * workingData.bytesAvailable ) + workingbBitsAvailable;
}
private function loadWord():void
{
workingWord = 0; workingbBitsAvailable = 0;
switch( workingData.bytesAvailable )
{
case 0: workingbBitsAvailable = 0; break;
default: // not 0, but greater than 4
case 4: workingWord = workingData.readUnsignedByte(); workingbBitsAvailable = 8;
case 3: workingWord = ( workingWord << 8 ) | workingData.readUnsignedByte(); workingbBitsAvailable += 8;
case 2: workingWord = ( workingWord << 8 ) | workingData.readUnsignedByte(); workingbBitsAvailable += 8;
case 1: workingWord = ( workingWord << 8 ) | workingData.readUnsignedByte(); workingbBitsAvailable += 8;
}
workingWord <<= (32 - workingbBitsAvailable);
}
public function skipBits(size:int):void
{
if ( workingbBitsAvailable > size )
{
workingWord <<= size;
workingbBitsAvailable -= size;
}
else
{
size -= workingbBitsAvailable;
var skipBytes:int = size / 8;
size -= ( skipBytes * 8 );
workingData.position += skipBytes;
loadWord();
workingWord <<= size;
workingbBitsAvailable -= size;
}
}
public function readBits(size:int):uint
{
// if ( 32 < size )
// throw new Error("Can not read more than 32 bits at a time");
var bits:uint = ( workingbBitsAvailable < size ? workingbBitsAvailable : size);
var valu:uint = workingWord >>> (32 - bits);
workingbBitsAvailable -= bits;
if ( 0 < workingbBitsAvailable )
workingWord <<= bits;
else
loadWord();
bits = size - bits;
if ( 0 < bits )
return valu << bits | readBits( bits );
else
return valu;
}
private function skipLeadingZeros():uint
{
for( var clz:uint = 0 ; clz < workingbBitsAvailable ; ++clz )
{
if( 0 != ( workingWord & ( 0x80000000 >>> clz ) ) )
{
workingWord <<= clz;
workingbBitsAvailable -= clz;
return clz;
}
}
loadWord(); // we exhausted workingWord and still have not found a 1
return clz + skipLeadingZeros();
}
public function skipUnsignedExpGolomb():void
{
skipBits(1 + skipLeadingZeros() );
}
public function skipExpGolomb():void
{
skipBits(1 + skipLeadingZeros() );
}
public function readUnsignedExpGolomb():uint
{
var clz:uint = skipLeadingZeros();
return readBits(clz+1) - 1;
}
public function readExpGolomb():int
{
var valu:int = readUnsignedExpGolomb();
if ( 0x01 & valu ) // the number is odd if the low order bit is set
return (1 + valu) >>> 1; // add 1 to make it even, and devide by 2
else
return -1 * (valu >>> 1); // devide by two then make it negative
}
// Some convenience functions
public function readBoolean():Boolean
{
return 1 == readBits(1);
}
public function readUnsignedByte():int
{
return readBits(8);
}
}
*/
})();