This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmkvdemux.js
537 lines (457 loc) · 17.4 KB
/
mkvdemux.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
* Copyright (c) 2020 Yahweasel
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
var mkvdemuxjs = (function(mkvdemuxjs) {
// A few IDs we repeat, as "globals"
var ID_CLUSTER = 0x1f43b675;
var ID_TRACKENTRY = 0xae;
// The main MKV demuxer type
function MkvDemux() {
// Queue of data to be demuxed
this.queue = [];
this.peekHi = 0;
this.peekLo = 0;
// Our overall position in the document
this.pos = 0;
// Our entire context
this.context = [];
// Our current context
this.block = null;
// Extra info used during demuxing
this.ex = {};
}
MkvDemux.prototype = {
// Push ArrayBuffer data in the queue
push: function(data) {
this.queue.push(data);
},
// Peek this many bytes from the queue
peek: function(count) {
if (this.queue.length <= this.peekHi)
return null;
// Special case for peeking at the beginning
var q = this.queue[this.peekHi];
if (this.peekLo === 0 && q.byteLength >= count) {
this.peekLo += count;
if (this.peekLo >= q.byteLength) {
this.peekHi++;
this.peekLo = 0;
}
return q;
}
// Special case for peeking with enough data left
if (q.byteLength >= this.peekLo + count) {
var ret = new Uint8Array(q).slice(this.peekLo, this.peekLo + count);
this.peekLo += count;
if (this.peekLo >= q.byteLength) {
this.peekHi++;
this.peekLo = 0;
}
return ret.buffer;
}
// Otherwise, construct our return
var ret = new Uint8Array(count);
ret.set(new Uint8Array(q).slice(this.peekLo));
var pos = q.byteLength - this.peekLo;
var left = count - pos;
var i = this.peekHi + 1;
while (left) {
if (i >= this.queue.length) return null;
// Pull out another buffer
var next = new Uint8Array(this.queue[i++]);
if (next.length >= left) {
// This has enough to fill out the remainder
if (next.length === left) {
// Exactly enough
ret.set(next, pos);
this.peekHi = i + 1;
this.peekLo = 0;
left = 0;
} else {
// More than enough
ret.set(next.slice(0, left), pos);
this.peekHi = i;
this.peekLo = left;
left = 0;
}
} else {
// Not enough, need the whole thing
ret.set(next, pos);
left -= next.length;
pos += next.length;
}
}
return ret.buffer;
},
// Commit whatever we've peeked
commit: function() {
// Remove entire elements
while (this.peekHi) {
this.pos += this.queue[0].byteLength;
this.queue.shift();
this.peekHi--;
}
// And truncate the last one
if (this.peekLo) {
this.pos += this.peekLo;
var nq = new Uint8Array(this.queue[0]).slice(this.peekLo);
if (nq.length === 0)
this.queue.shift();
else
this.queue[0] = nq.buffer;
this.peekLo = 0;
}
// If we've moved beyond any elements, pop them from our context
while (this.context.length && this.context[this.context.length-1].end <= this.pos)
this.context.pop();
if (this.context.length)
this.block = this.context[this.context.length-1];
else
this.block = null;
},
// Save our peek position
savePeek: function() {
return {hi: this.peekHi, lo: this.peekLo};
},
// Restore our peek position
restorePeek: function(from) {
this.peekHi = from.hi;
this.peekLo = from.lo;
},
// Peek a variable-sized int (VINT)
peekVint: function(keepMarker) {
/* variable-size ints have a header that describes the width, then
* the data across multiple bytes */
var pre = this.savePeek();
var header = this.peek(1);
if (header === null)
return null;
header = new Uint8Array(header)[0];
// Determine how many bytes are represented by this header
var bytes = 1;
while (!(header & 0x80)) {
bytes++;
header = (header << 1) & 0xFF;
}
// Get rid of the 1 bit
header &= 0x7F;
header >>>= bytes-1;
// Now read in the whole thing
this.restorePeek(pre);
var whole = this.peek(bytes);
if (whole === null)
return null;
// Then convert it
whole = new Uint8Array(whole).slice(0, bytes);
if (!keepMarker)
whole[0] = header;
var ret = 0;
for (var i = 0; i < whole.length; i++) {
ret *= 0x100;
ret += whole[i];
}
return ret;
},
// Read an EBML header
readEBMLHeader: function() {
var pre = this.savePeek();
var prePos = this.pos;
// ID
var id = this.peekVint(true);
if (id === null)
return null;
var length;
// Check for the special case of unknown length, which we just ignore in our context
var pre2 = this.savePeek();
var x = this.peek(1);
if (x === null) {
this.restorePeek(pre);
return null;
}
x = new Uint8Array(x)[0];
if (x === 0xBF) {
// Unknown length
length = -1;
} else {
// Normal VINT
this.restorePeek(pre2);
length = this.peekVint();
if (length === null) {
this.restorePeek(pre);
return null;
}
}
// Update our context
this.commit();
var ret = new EBML(id, length, prePos, this.pos);
if (length >= 0) {
this.context.push(ret);
this.block = ret;
}
return ret;
},
// Read the entire content of the current EBML element
readEBMLBody: function() {
if (this.block.length === 0) {
this.context.pop();
if (this.context.length)
this.block = this.context[this.context.length-1];
else
this.block = null;
return null;
}
// Read it in
var hdr = this.block;
var len = hdr.end - this.pos;
var ret = this.peek(len);
if (ret === null)
return null;
// Read it, so commit
this.commit();
return ret;
},
// Read the content of the current EBML element as an unsigned integer
readUInt: function() {
var len = this.block.length;
// Read it in
var val = this.readEBMLBody();
if (len === 0)
return 0;
if (val === null)
return null;
// Convert it
val = new DataView(val);
switch (len) {
case 1:
return val.getUint8(0);
case 2:
return val.getUint16(0);
case 4:
return val.getUint32(0);
default:
var ret = 0;
val = new Uint8Array(val.buffer);
for (var i = 0; i < len; i++) {
ret *= 0x100;
ret += val[i];
}
return ret;
}
},
// Read the content of the current EBML element as a float
readFloat: function() {
var len = this.block.length;
// Read it in
var val = this.readEBMLBody();
if (len === 0)
return 0;
if (val === null)
return null;
// Convert it
val = new DataView(val);
if (len === 4)
return val.getFloat32(0);
else if (len === 8)
return val.getFloat64(0);
else
return 0;
},
/* The main demuxer. Can return one of several structures:
* If there's not enough data to demux, returns null.
* If it received a track entry, returns
* {track: {...}}
* If it received packets, returns
* {frames: array({data: [data], track: [track number], timestamp: [timestamp]})}
* If it received anything else, returns the EBML context
*/
demux: function() {
var el;
while (true) {
// Figure out our current context
if (!this.context.length)
this.readEBMLHeader();
if (!this.context.length)
return null;
el = this.block;
// We should know what to read based on that context
switch (el.id) {
case 0x1a45dfa3: // EBML header
case 0x114d9b74: // SeekHead
case 0x1549a966: // Info
case 0x1254c367: // Tags
case 0x75a2: // DiscardPadding
case 0xec: // ???
case 0xbf: // ???
// Elements we don't care about. Skip them.
var ct = this.readEBMLBody();
if (ct === null)
return null;
el.ex.content = ct;
return el;
case 0x18538067: // Segment
case 0x1654ae6b: // Tracks
case ID_CLUSTER: // Cluster
case 0xa0: // BlockGroup
// Surrounding elements we need to dig into
if (this.readEBMLHeader() === null)
return null;
this.ex = {};
break;
case ID_TRACKENTRY: // TrackEntry
// A track description. Read it fully.
return this.readTrackEntry();
case 0xe7: // Timestamp (in cluster)
var val = this.readUInt();
if (val === null)
return null;
this.ex.clusterTimestamp = val;
break;
case 0xa1: // Block
case 0xa3: // SimpleBlock
return this.decodeBlock();
default:
// We don't know what this is!
var ct = this.readEBMLBody();
if (ct === null)
return null;
console.log("Unrecognized element " + el.id.toString(16) + " at " + el.start.toString(16) + " to " + el.end.toString(16));
console.log("Context:");
for (var i = 0; i < this.context.length; i++)
console.log(" " + this.context[i].id.toString(16));
el.ex.content = ct;
return el;
}
}
},
// Read and decode a track entry
readTrackEntry: function() {
var te = this.block;
var len = te.end - this.pos;
// Make sure it's all here
var pre = this.savePeek();
if (this.peek(len) === null)
return null;
this.restorePeek(pre);
var ret = {track: {}};
// Now read in each of the parts
while (this.block === te) {
var hdr = this.readEBMLHeader();
if (hdr === null)
return null;
switch (hdr.id) {
case 0x9c: // FlagLacing
case 0x22b59c: // Language
case 0x56bb: // SeekPreRoll
case 0x63a2: // CodecPrivate
// Irrelevant for us, just skip it
this.readEBMLBody();
break;
case 0xe1: // Audio
// We need the actual body of this
this.context.pop();
this.block = te;
break;
case 0xd7: // TrackNumber
ret.track.number = this.readUInt();
break;
case 0x73c5: // TrackUID
ret.track.uid = this.readUInt();
break;
case 0x86: // CodecID
ret.track.codec = this.readUInt();
break;
case 0x56aa: // CodecDelay
ret.track.codecDelay = this.readUInt() / 1000000000;
break;
case 0x83: // CodecType
var ct = ret.track.codecType = this.readUInt();
var t = "unknown";
switch (ct) {
case 1:
t = "video";
break;
case 2:
t = "audio";
break;
}
ret.track.type = t;
break;
case 0x9f: // Audio:Channels
ret.track.channels = this.readUInt();
break;
case 0xb5: // Audio:SamplingFrequency
ret.track.sampleRate = this.readFloat();
break;
case 0x6264: // Audio:BitDepth
ret.track.bitDepth = this.readUInt();
break;
default:
console.log("Unrecognized track entry component " + hdr.id.toString(16));
this.readEBMLBody();
}
}
return ret;
},
// Decode a single Block or SimpleBlock
decodeBlock: function() {
var len = this.block.length;
var cont = this.readEBMLBody();
if (cont === null)
return null;
cont = new DataView(cont);
// Surrounding data
var ret = {};
var clusterTimestamp = this.ex.clusterTimestamp;
if (!clusterTimestamp)
clusterTimestamp = 0;
// Byte 0: Track number
var track = cont.getUint8(0);
if (!(track & 0x80))
console.log("ERROR: Track #s greater than 127 are not supported!");
track &= 0x7F;
// Bytes 1 and 2: Timecode offset
var timeOff = cont.getInt16(1);
var timestamp = (clusterTimestamp + timeOff) / 1000;
// Byte 3: Flags
var flags = cont.getUint8(3);
if ((flags & 0x6) !== 0)
console.log("ERROR: Lacing is not supported!");
// Rest: The actual data
var frame = {
data: new Uint8Array(cont.buffer).slice(4, len).buffer,
track: track,
timestamp: timestamp
};
ret.frames = [frame];
return ret;
}
};
// An EBML "tag"
function EBML(id, length, start, bodyStart) {
this.id = id;
this.length = length;
this.start = start;
this.bodyStart = bodyStart;
if (length >= 0)
this.end = bodyStart + length;
else
this.end = Infinity;
this.ex = {}; // Extra context stored by the main demuxer
}
mkvdemuxjs.MkvDemux = MkvDemux;
return mkvdemuxjs;
})(mkvdemuxjs || {});
if (typeof module !== "undefined")
module.exports = mkvdemuxjs;