/
mp4_parser.js
329 lines (283 loc) · 8.94 KB
/
mp4_parser.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.Mp4Parser');
goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.util.DataViewReader');
/**
* @export
*/
shaka.util.Mp4Parser = class {
/** */
constructor() {
/** @private {!Object.<number, shaka.util.Mp4Parser.BoxType_>} */
this.headers_ = [];
/** @private {!Object.<number, !shaka.util.Mp4Parser.CallbackType>} */
this.boxDefinitions_ = [];
/** @private {boolean} */
this.done_ = false;
}
/**
* Declare a box type as a Box.
*
* @param {string} type
* @param {!shaka.util.Mp4Parser.CallbackType} definition
* @return {!shaka.util.Mp4Parser}
* @export
*/
box(type, definition) {
const typeCode = shaka.util.Mp4Parser.typeFromString_(type);
this.headers_[typeCode] = shaka.util.Mp4Parser.BoxType_.BASIC_BOX;
this.boxDefinitions_[typeCode] = definition;
return this;
}
/**
* Declare a box type as a Full Box.
*
* @param {string} type
* @param {!shaka.util.Mp4Parser.CallbackType} definition
* @return {!shaka.util.Mp4Parser}
* @export
*/
fullBox(type, definition) {
const typeCode = shaka.util.Mp4Parser.typeFromString_(type);
this.headers_[typeCode] = shaka.util.Mp4Parser.BoxType_.FULL_BOX;
this.boxDefinitions_[typeCode] = definition;
return this;
}
/**
* Stop parsing. Useful for extracting information from partial segments and
* avoiding an out-of-bounds error once you find what you are looking for.
*
* @export
*/
stop() {
this.done_ = true;
}
/**
* Parse the given data using the added callbacks.
*
* @param {!BufferSource} data
* @param {boolean=} partialOkay If true, allow reading partial payloads
* from some boxes. If the goal is a child box, we can sometimes find it
* without enough data to find all child boxes.
* @param {boolean=} stopOnPartial If true, stop reading if an incomplete
* box is detected.
* @export
*/
parse(data, partialOkay, stopOnPartial) {
const reader = new shaka.util.DataViewReader(
data, shaka.util.DataViewReader.Endianness.BIG_ENDIAN);
this.done_ = false;
while (reader.hasMoreData() && !this.done_) {
this.parseNext(0, reader, partialOkay, stopOnPartial);
}
}
/**
* Parse the next box on the current level.
*
* @param {number} absStart The absolute start position in the original
* byte array.
* @param {!shaka.util.DataViewReader} reader
* @param {boolean=} partialOkay If true, allow reading partial payloads
* from some boxes. If the goal is a child box, we can sometimes find it
* without enough data to find all child boxes.
* @param {boolean=} stopOnPartial If true, stop reading if an incomplete
* box is detected.
* @export
*/
parseNext(absStart, reader, partialOkay, stopOnPartial) {
const start = reader.getPosition();
// size(4 bytes) + type(4 bytes) = 8 bytes
if (stopOnPartial && start + 8 > reader.getLength()) {
this.done_ = true;
return;
}
let size = reader.readUint32();
const type = reader.readUint32();
const name = shaka.util.Mp4Parser.typeToString(type);
let has64BitSize = false;
shaka.log.v2('Parsing MP4 box', name);
switch (size) {
case 0:
size = reader.getLength() - start;
break;
case 1:
if (stopOnPartial && reader.getPosition() + 8 > reader.getLength()) {
this.done_ = true;
return;
}
size = reader.readUint64();
has64BitSize = true;
break;
}
const boxDefinition = this.boxDefinitions_[type];
if (boxDefinition) {
let version = null;
let flags = null;
if (this.headers_[type] == shaka.util.Mp4Parser.BoxType_.FULL_BOX) {
if (stopOnPartial && reader.getPosition() + 4 > reader.getLength()) {
this.done_ = true;
return;
}
const versionAndFlags = reader.readUint32();
version = versionAndFlags >>> 24;
flags = versionAndFlags & 0xFFFFFF;
}
// Read the whole payload so that the current level can be safely read
// regardless of how the payload is parsed.
let end = start + size;
if (partialOkay && end > reader.getLength()) {
// For partial reads, truncate the payload if we must.
end = reader.getLength();
}
if (stopOnPartial && end > reader.getLength()) {
this.done_ = true;
return;
}
const payloadSize = end - reader.getPosition();
const payload =
(payloadSize > 0) ? reader.readBytes(payloadSize) : new Uint8Array(0);
const payloadReader = new shaka.util.DataViewReader(
payload, shaka.util.DataViewReader.Endianness.BIG_ENDIAN);
/** @type {shaka.extern.ParsedBox} */
const box = {
parser: this,
partialOkay: partialOkay || false,
version,
flags,
reader: payloadReader,
size,
start: start + absStart,
has64BitSize,
};
boxDefinition(box);
} else {
// Move the read head to be at the end of the box.
// If the box is longer than the remaining parts of the file, e.g. the
// mp4 is improperly formatted, or this was a partial range request that
// ended in the middle of a box, just skip to the end.
const skipLength = Math.min(
start + size - reader.getPosition(),
reader.getLength() - reader.getPosition());
reader.skip(skipLength);
}
}
/**
* A callback that tells the Mp4 parser to treat the body of a box as a series
* of boxes. The number of boxes is limited by the size of the parent box.
*
* @param {!shaka.extern.ParsedBox} box
* @export
*/
static children(box) {
// The "reader" starts at the payload, so we need to add the header to the
// start position. The header size varies.
const headerSize = shaka.util.Mp4Parser.headerSize(box);
while (box.reader.hasMoreData() && !box.parser.done_) {
box.parser.parseNext(box.start + headerSize, box.reader, box.partialOkay);
}
}
/**
* A callback that tells the Mp4 parser to treat the body of a box as a sample
* description. A sample description box has a fixed number of children. The
* number of children is represented by a 4 byte unsigned integer. Each child
* is a box.
*
* @param {!shaka.extern.ParsedBox} box
* @export
*/
static sampleDescription(box) {
// The "reader" starts at the payload, so we need to add the header to the
// start position. The header size varies.
const headerSize = shaka.util.Mp4Parser.headerSize(box);
const count = box.reader.readUint32();
for (let i = 0; i < count; i++) {
box.parser.parseNext(box.start + headerSize, box.reader, box.partialOkay);
if (box.parser.done_) {
break;
}
}
}
/**
* Create a callback that tells the Mp4 parser to treat the body of a box as a
* binary blob and to parse the body's contents using the provided callback.
*
* @param {function(!Uint8Array)} callback
* @return {!shaka.util.Mp4Parser.CallbackType}
* @export
*/
static allData(callback) {
return (box) => {
const all = box.reader.getLength() - box.reader.getPosition();
callback(box.reader.readBytes(all));
};
}
/**
* Convert an ascii string name to the integer type for a box.
*
* @param {string} name The name of the box. The name must be four
* characters long.
* @return {number}
* @private
*/
static typeFromString_(name) {
goog.asserts.assert(
name.length == 4,
'Mp4 box names must be 4 characters long');
let code = 0;
for (const chr of name) {
code = (code << 8) | chr.charCodeAt(0);
}
return code;
}
/**
* Convert an integer type from a box into an ascii string name.
* Useful for debugging.
*
* @param {number} type The type of the box, a uint32.
* @return {string}
* @export
*/
static typeToString(type) {
const name = String.fromCharCode(
(type >> 24) & 0xff,
(type >> 16) & 0xff,
(type >> 8) & 0xff,
type & 0xff);
return name;
}
/**
* Find the header size of the box.
* Useful for modifying boxes in place or finding the exact offset of a field.
*
* @param {shaka.extern.ParsedBox} box
* @return {number}
* @export
*/
static headerSize(box) {
const basicHeaderSize = 8;
const _64BitFieldSize = box.has64BitSize ? 8 : 0;
const versionAndFlagsSize = box.flags != null ? 4 : 0;
return basicHeaderSize + _64BitFieldSize + versionAndFlagsSize;
}
};
/**
* @typedef {function(!shaka.extern.ParsedBox)}
* @exportInterface
*/
shaka.util.Mp4Parser.CallbackType;
/**
* An enum used to track the type of box so that the correct values can be
* read from the header.
*
* @enum {number}
* @private
*/
shaka.util.Mp4Parser.BoxType_ = {
BASIC_BOX: 0,
FULL_BOX: 1,
};