forked from phoboslab/jsmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.js
196 lines (166 loc) · 4.37 KB
/
buffer.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
JSMpeg.BitBuffer = (function(){ "use strict";
var BitBuffer = function(bufferOrLength, mode) {
if (typeof(bufferOrLength) === 'object') {
this.bytes = (bufferOrLength instanceof Uint8Array)
? bufferOrLength
: new Uint8Array(bufferOrLength);
this.byteLength = this.bytes.length;
}
else {
this.bytes = new Uint8Array(bufferOrLength || 1024*1024);
this.byteLength = 0;
}
this.mode = mode || BitBuffer.MODE.EXPAND;
this.index = 0;
};
BitBuffer.prototype.resize = function(size) {
var newBytes = new Uint8Array(size);
if (this.byteLength !== 0) {
this.byteLength = Math.min(this.byteLength, size);
newBytes.set(this.bytes, 0, this.byteLength);
}
this.bytes = newBytes;
this.index = Math.min(this.index, this.byteLength << 3);
};
BitBuffer.prototype.evict = function(sizeNeeded) {
var bytePos = this.index >> 3,
available = this.bytes.length - this.byteLength;
// If the current index is the write position, we can simply reset both
// to 0. Also reset (and throw away yet unread data) if we won't be able
// to fit the new data in even after a normal eviction.
if (
this.index === this.byteLength << 3 ||
sizeNeeded > available + bytePos // emergency evac
) {
this.byteLength = 0;
this.index = 0;
return;
}
else if (bytePos === 0) {
// Nothing read yet - we can't evict anything
return;
}
// Some browsers don't support copyWithin() yet - we may have to do
// it manually using set and a subarray
if (this.bytes.copyWithin) {
this.bytes.copyWithin(0, bytePos, this.byteLength);
}
else {
this.bytes.set(this.bytes.subarray(bytePos, this.byteLength));
}
this.byteLength = this.byteLength - bytePos;
this.index -= bytePos << 3;
return;
};
BitBuffer.prototype.write = function(buffers) {
var isArrayOfBuffers = (typeof(buffers[0]) === 'object'),
totalLength = 0,
available = this.bytes.length - this.byteLength;
// Calculate total byte length
if (isArrayOfBuffers) {
var totalLength = 0;
for (var i = 0; i < buffers.length; i++) {
totalLength += buffers[i].byteLength;
}
}
else {
totalLength = buffers.byteLength;
}
// Do we need to resize or evict?
if (totalLength > available) {
if (this.mode === BitBuffer.MODE.EXPAND) {
var newSize = Math.max(
this.bytes.length * 2,
totalLength - available
);
this.resize(newSize)
}
else {
this.evict(totalLength);
}
}
if (isArrayOfBuffers) {
for (var i = 0; i < buffers.length; i++) {
this.appendSingleBuffer(buffers[i]);
}
}
else {
this.appendSingleBuffer(buffers);
}
};
BitBuffer.prototype.appendSingleBuffer = function(buffer) {
buffer = buffer instanceof Uint8Array
? buffer
: new Uint8Array(buffer);
this.bytes.set(buffer, this.byteLength);
this.byteLength += buffer.length;
};
BitBuffer.prototype.findNextStartCode = function() {
for (var i = (this.index+7 >> 3); i < this.byteLength; i++) {
if(
this.bytes[i] == 0x00 &&
this.bytes[i+1] == 0x00 &&
this.bytes[i+2] == 0x01
) {
this.index = (i+4) << 3;
return this.bytes[i+3];
}
}
this.index = (this.byteLength << 3);
return -1;
};
BitBuffer.prototype.findStartCode = function(code) {
var current = 0;
while (true) {
current = this.findNextStartCode();
if (current === code || current === -1) {
return current;
}
}
return -1;
};
BitBuffer.prototype.nextBytesAreStartCode = function() {
var i = (this.index+7 >> 3);
return (
i >= this.byteLength || (
this.bytes[i] == 0x00 &&
this.bytes[i+1] == 0x00 &&
this.bytes[i+2] == 0x01
)
);
};
BitBuffer.prototype.peek = function(count) {
var offset = this.index;
var value = 0;
while (count) {
var currentByte = this.bytes[offset >> 3],
remaining = 8 - (offset & 7), // remaining bits in byte
read = remaining < count ? remaining : count, // bits in this run
shift = remaining - read,
mask = (0xff >> (8-read));
value = (value << read) | ((currentByte & (mask << shift)) >> shift);
offset += read;
count -= read;
}
return value;
}
BitBuffer.prototype.read = function(count) {
var value = this.peek(count);
this.index += count;
return value;
};
BitBuffer.prototype.skip = function(count) {
return (this.index += count);
};
BitBuffer.prototype.rewind = function(count) {
this.index = Math.max(this.index - count, 0);
};
BitBuffer.prototype.has = function(count) {
return ((this.byteLength << 3) - this.index) >= count;
};
BitBuffer.MODE = {
EVICT: 1,
EXPAND: 2
};
return BitBuffer;
})();