forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp4_box_parsers.js
332 lines (294 loc) · 7.82 KB
/
mp4_box_parsers.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
328
329
330
331
332
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.Mp4BoxParsers');
goog.require('shaka.util.DataViewReader');
goog.require('shaka.util.Mp4Parser');
shaka.util.Mp4BoxParsers = class {
/**
* Parses a TFHD Box.
* @param {!shaka.util.DataViewReader} reader
* @param {number} flags
* @return {!shaka.util.ParsedTFHDBox}
*/
static parseTFHD(reader, flags) {
let defaultSampleDuration = null;
let defaultSampleSize = null;
let baseDataOffset = null;
const trackId = reader.readUint32(); // Read "track_ID"
// Skip "base_data_offset" if present.
if (flags & 0x000001) {
baseDataOffset = reader.readUint64();
}
// Skip "sample_description_index" if present.
if (flags & 0x000002) {
reader.skip(4);
}
// Read "default_sample_duration" if present.
if (flags & 0x000008) {
defaultSampleDuration = reader.readUint32();
}
// Read "default_sample_size" if present.
if (flags & 0x000010) {
defaultSampleSize = reader.readUint32();
}
return {
trackId,
defaultSampleDuration,
defaultSampleSize,
baseDataOffset,
};
}
/**
* Parses a TFDT Box.
* @param {!shaka.util.DataViewReader} reader
* @param {number} version
* @return {!shaka.util.ParsedTFDTBox}
*/
static parseTFDT(reader, version) {
const baseMediaDecodeTime = version == 1 ?
reader.readUint64() : reader.readUint32();
return {
baseMediaDecodeTime,
};
}
/**
* Parses a MDHD Box.
* @param {!shaka.util.DataViewReader} reader
* @param {number} version
* @return {!shaka.util.ParsedMDHDBox}
*/
static parseMDHD(reader, version) {
if (version == 1) {
reader.skip(8); // Skip "creation_time"
reader.skip(8); // Skip "modification_time"
} else {
reader.skip(4); // Skip "creation_time"
reader.skip(4); // Skip "modification_time"
}
const timescale = reader.readUint32();
return {
timescale,
};
}
/**
* Parses a TREX Box.
* @param {!shaka.util.DataViewReader} reader
* @return {!shaka.util.ParsedTREXBox}
*/
static parseTREX(reader) {
reader.skip(4); // Skip "track_ID"
reader.skip(4); // Skip "default_sample_description_index"
const defaultSampleDuration = reader.readUint32();
const defaultSampleSize = reader.readUint32();
return {
defaultSampleDuration,
defaultSampleSize,
};
}
/**
* Parses a TRUN Box.
* @param {!shaka.util.DataViewReader} reader
* @param {number} version
* @param {number} flags
* @return {!shaka.util.ParsedTRUNBox}
*/
static parseTRUN(reader, version, flags) {
const sampleCount = reader.readUint32();
const sampleData = [];
let dataOffset = null;
// "data_offset"
if (flags & 0x000001) {
dataOffset = reader.readUint32();
}
// Skip "first_sample_flags" if present.
if (flags & 0x000004) {
reader.skip(4);
}
for (let i = 0; i < sampleCount; i++) {
/** @type {shaka.util.ParsedTRUNSample} */
const sample = {
sampleDuration: null,
sampleSize: null,
sampleCompositionTimeOffset: null,
};
// Read "sample duration" if present.
if (flags & 0x000100) {
sample.sampleDuration = reader.readUint32();
}
// Read "sample_size" if present.
if (flags & 0x000200) {
sample.sampleSize = reader.readUint32();
}
// Skip "sample_flags" if present.
if (flags & 0x000400) {
reader.skip(4);
}
// Read "sample_time_offset" if present.
if (flags & 0x000800) {
sample.sampleCompositionTimeOffset = version == 0 ?
reader.readUint32() :
reader.readInt32();
}
sampleData.push(sample);
}
return {
sampleCount,
sampleData,
dataOffset,
};
}
/**
* Parses a TKHD Box.
* @param {!shaka.util.DataViewReader} reader
* @param {number} version
* @return {!shaka.util.ParsedTKHDBox}
*/
static parseTKHD(reader, version) {
let trackId = 0;
if (version == 1) {
reader.skip(8); // Skip "creation_time"
reader.skip(8); // Skip "modification_time"
trackId = reader.readUint32();
} else {
reader.skip(4); // Skip "creation_time"
reader.skip(4); // Skip "modification_time"
trackId = reader.readUint32();
}
return {
trackId,
};
}
/**
* Parses a FRMA box.
* @param {!shaka.util.DataViewReader} reader
* @return {!shaka.util.ParsedFRMABox}
*/
static parseFRMA(reader) {
const fourcc = reader.readUint32();
const codec = shaka.util.Mp4Parser.typeToString(fourcc);
return {codec};
}
};
/**
* @typedef {{
* trackId: number,
* defaultSampleDuration: ?number,
* defaultSampleSize: ?number,
* baseDataOffset: ?number
* }}
*
* @property {number} trackId
* As per the spec: an integer that uniquely identifies this
* track over the entire life‐time of this presentation
* @property {?number} defaultSampleDuration
* If specified via flags, this overrides the default sample
* duration in the Track Extends Box for this fragment
* @property {?number} defaultSampleSize
* If specified via flags, this overrides the default sample
* size in the Track Extends Box for this fragment
* @property {?number} baseDataOffset
* If specified via flags, this indicate the base data offset
*
* @exportDoc
*/
shaka.util.ParsedTFHDBox;
/**
* @typedef {{
* baseMediaDecodeTime: number
* }}
*
* @property {number} baseMediaDecodeTime
* As per the spec: the absolute decode time, measured on the media
* timeline, of the first sample in decode order in the track fragment
*
* @exportDoc
*/
shaka.util.ParsedTFDTBox;
/**
* @typedef {{
* timescale: number
* }}
*
* @property {number} timescale
* As per the spec: an integer that specifies the time‐scale for this media;
* this is the number of time units that pass in one second
*
* @exportDoc
*/
shaka.util.ParsedMDHDBox;
/**
* @typedef {{
* defaultSampleDuration: number,
* defaultSampleSize: number
* }}
*
* @property {number} defaultSampleDuration
* The default sample duration to be used in track fragments
* @property {number} defaultSampleSize
* The default sample size to be used in track fragments
*
* @exportDoc
*/
shaka.util.ParsedTREXBox;
/**
* @typedef {{
* sampleCount: number,
* sampleData: !Array.<shaka.util.ParsedTRUNSample>,
* dataOffset: ?number
* }}
*
* @property {number} sampleCount
* As per the spec: the number of samples being added in this run;
* @property {!Array.<shaka.util.ParsedTRUNSample>} sampleData
* An array of size <sampleCount> containing data for each sample
* @property {?number} dataOffset
* If specified via flags, this indicate the offset of the sample in bytes.
*
* @exportDoc
*/
shaka.util.ParsedTRUNBox;
/**
* @typedef {{
* sampleDuration: ?number,
* sampleSize: ?number,
* sampleCompositionTimeOffset: ?number
* }}
*
* @property {?number} sampleDuration
* The length of the sample in timescale units.
* @property {?number} sampleSize
* The size of the sample in bytes.
* @property {?number} sampleCompositionTimeOffset
* The time since the start of the sample in timescale units. Time
* offset is based of the start of the sample. If this value is
* missing, the accumulated durations preceeding this time sample will
* be used to create the start time.
*
* @exportDoc
*/
shaka.util.ParsedTRUNSample;
/**
* @typedef {{
* trackId: number
* }}
*
* @property {number} trackId
* Unique ID indicative of this track
*
* @exportDoc
*/
shaka.util.ParsedTKHDBox;
/**
* @typedef {{
* codec: string
* }}
*
* @property {string} codec
* A fourcc for a codec
*
* @exportDoc
*/
shaka.util.ParsedFRMABox;