ebml-helpers.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. import { toUint8, bytesToNumber, bytesMatch, bytesToString, numberToBytes, padStart } from './byte-helpers';
  2. import { getAvcCodec, getHvcCodec, getAv1Codec } from './codec-helpers.js'; // relevant specs for this parser:
  3. // https://matroska-org.github.io/libebml/specs.html
  4. // https://www.matroska.org/technical/elements.html
  5. // https://www.webmproject.org/docs/container/
  6. export var EBML_TAGS = {
  7. EBML: toUint8([0x1A, 0x45, 0xDF, 0xA3]),
  8. DocType: toUint8([0x42, 0x82]),
  9. Segment: toUint8([0x18, 0x53, 0x80, 0x67]),
  10. SegmentInfo: toUint8([0x15, 0x49, 0xA9, 0x66]),
  11. Tracks: toUint8([0x16, 0x54, 0xAE, 0x6B]),
  12. Track: toUint8([0xAE]),
  13. TrackNumber: toUint8([0xd7]),
  14. DefaultDuration: toUint8([0x23, 0xe3, 0x83]),
  15. TrackEntry: toUint8([0xAE]),
  16. TrackType: toUint8([0x83]),
  17. FlagDefault: toUint8([0x88]),
  18. CodecID: toUint8([0x86]),
  19. CodecPrivate: toUint8([0x63, 0xA2]),
  20. VideoTrack: toUint8([0xe0]),
  21. AudioTrack: toUint8([0xe1]),
  22. // Not used yet, but will be used for live webm/mkv
  23. // see https://www.matroska.org/technical/basics.html#block-structure
  24. // see https://www.matroska.org/technical/basics.html#simpleblock-structure
  25. Cluster: toUint8([0x1F, 0x43, 0xB6, 0x75]),
  26. Timestamp: toUint8([0xE7]),
  27. TimestampScale: toUint8([0x2A, 0xD7, 0xB1]),
  28. BlockGroup: toUint8([0xA0]),
  29. BlockDuration: toUint8([0x9B]),
  30. Block: toUint8([0xA1]),
  31. SimpleBlock: toUint8([0xA3])
  32. };
  33. /**
  34. * This is a simple table to determine the length
  35. * of things in ebml. The length is one based (starts at 1,
  36. * rather than zero) and for every zero bit before a one bit
  37. * we add one to length. We also need this table because in some
  38. * case we have to xor all the length bits from another value.
  39. */
  40. var LENGTH_TABLE = [128, 64, 32, 16, 8, 4, 2, 1];
  41. var getLength = function getLength(byte) {
  42. var len = 1;
  43. for (var i = 0; i < LENGTH_TABLE.length; i++) {
  44. if (byte & LENGTH_TABLE[i]) {
  45. break;
  46. }
  47. len++;
  48. }
  49. return len;
  50. }; // length in ebml is stored in the first 4 to 8 bits
  51. // of the first byte. 4 for the id length and 8 for the
  52. // data size length. Length is measured by converting the number to binary
  53. // then 1 + the number of zeros before a 1 is encountered starting
  54. // from the left.
  55. var getvint = function getvint(bytes, offset, removeLength, signed) {
  56. if (removeLength === void 0) {
  57. removeLength = true;
  58. }
  59. if (signed === void 0) {
  60. signed = false;
  61. }
  62. var length = getLength(bytes[offset]);
  63. var valueBytes = bytes.subarray(offset, offset + length); // NOTE that we do **not** subarray here because we need to copy these bytes
  64. // as they will be modified below to remove the dataSizeLen bits and we do not
  65. // want to modify the original data. normally we could just call slice on
  66. // uint8array but ie 11 does not support that...
  67. if (removeLength) {
  68. valueBytes = Array.prototype.slice.call(bytes, offset, offset + length);
  69. valueBytes[0] ^= LENGTH_TABLE[length - 1];
  70. }
  71. return {
  72. length: length,
  73. value: bytesToNumber(valueBytes, {
  74. signed: signed
  75. }),
  76. bytes: valueBytes
  77. };
  78. };
  79. var normalizePath = function normalizePath(path) {
  80. if (typeof path === 'string') {
  81. return path.match(/.{1,2}/g).map(function (p) {
  82. return normalizePath(p);
  83. });
  84. }
  85. if (typeof path === 'number') {
  86. return numberToBytes(path);
  87. }
  88. return path;
  89. };
  90. var normalizePaths = function normalizePaths(paths) {
  91. if (!Array.isArray(paths)) {
  92. return [normalizePath(paths)];
  93. }
  94. return paths.map(function (p) {
  95. return normalizePath(p);
  96. });
  97. };
  98. var getInfinityDataSize = function getInfinityDataSize(id, bytes, offset) {
  99. if (offset >= bytes.length) {
  100. return bytes.length;
  101. }
  102. var innerid = getvint(bytes, offset, false);
  103. if (bytesMatch(id.bytes, innerid.bytes)) {
  104. return offset;
  105. }
  106. var dataHeader = getvint(bytes, offset + innerid.length);
  107. return getInfinityDataSize(id, bytes, offset + dataHeader.length + dataHeader.value + innerid.length);
  108. };
  109. /**
  110. * Notes on the EBLM format.
  111. *
  112. * EBLM uses "vints" tags. Every vint tag contains
  113. * two parts
  114. *
  115. * 1. The length from the first byte. You get this by
  116. * converting the byte to binary and counting the zeros
  117. * before a 1. Then you add 1 to that. Examples
  118. * 00011111 = length 4 because there are 3 zeros before a 1.
  119. * 00100000 = length 3 because there are 2 zeros before a 1.
  120. * 00000011 = length 7 because there are 6 zeros before a 1.
  121. *
  122. * 2. The bits used for length are removed from the first byte
  123. * Then all the bytes are merged into a value. NOTE: this
  124. * is not the case for id ebml tags as there id includes
  125. * length bits.
  126. *
  127. */
  128. export var findEbml = function findEbml(bytes, paths) {
  129. paths = normalizePaths(paths);
  130. bytes = toUint8(bytes);
  131. var results = [];
  132. if (!paths.length) {
  133. return results;
  134. }
  135. var i = 0;
  136. while (i < bytes.length) {
  137. var id = getvint(bytes, i, false);
  138. var dataHeader = getvint(bytes, i + id.length);
  139. var dataStart = i + id.length + dataHeader.length; // dataSize is unknown or this is a live stream
  140. if (dataHeader.value === 0x7f) {
  141. dataHeader.value = getInfinityDataSize(id, bytes, dataStart);
  142. if (dataHeader.value !== bytes.length) {
  143. dataHeader.value -= dataStart;
  144. }
  145. }
  146. var dataEnd = dataStart + dataHeader.value > bytes.length ? bytes.length : dataStart + dataHeader.value;
  147. var data = bytes.subarray(dataStart, dataEnd);
  148. if (bytesMatch(paths[0], id.bytes)) {
  149. if (paths.length === 1) {
  150. // this is the end of the paths and we've found the tag we were
  151. // looking for
  152. results.push(data);
  153. } else {
  154. // recursively search for the next tag inside of the data
  155. // of this one
  156. results = results.concat(findEbml(data, paths.slice(1)));
  157. }
  158. }
  159. var totalLength = id.length + dataHeader.length + data.length; // move past this tag entirely, we are not looking for it
  160. i += totalLength;
  161. }
  162. return results;
  163. }; // see https://www.matroska.org/technical/basics.html#block-structure
  164. export var decodeBlock = function decodeBlock(block, type, timestampScale, clusterTimestamp) {
  165. var duration;
  166. if (type === 'group') {
  167. duration = findEbml(block, [EBML_TAGS.BlockDuration])[0];
  168. if (duration) {
  169. duration = bytesToNumber(duration);
  170. duration = 1 / timestampScale * duration * timestampScale / 1000;
  171. }
  172. block = findEbml(block, [EBML_TAGS.Block])[0];
  173. type = 'block'; // treat data as a block after this point
  174. }
  175. var dv = new DataView(block.buffer, block.byteOffset, block.byteLength);
  176. var trackNumber = getvint(block, 0);
  177. var timestamp = dv.getInt16(trackNumber.length, false);
  178. var flags = block[trackNumber.length + 2];
  179. var data = block.subarray(trackNumber.length + 3); // pts/dts in seconds
  180. var ptsdts = 1 / timestampScale * (clusterTimestamp + timestamp) * timestampScale / 1000; // return the frame
  181. var parsed = {
  182. duration: duration,
  183. trackNumber: trackNumber.value,
  184. keyframe: type === 'simple' && flags >> 7 === 1,
  185. invisible: (flags & 0x08) >> 3 === 1,
  186. lacing: (flags & 0x06) >> 1,
  187. discardable: type === 'simple' && (flags & 0x01) === 1,
  188. frames: [],
  189. pts: ptsdts,
  190. dts: ptsdts,
  191. timestamp: timestamp
  192. };
  193. if (!parsed.lacing) {
  194. parsed.frames.push(data);
  195. return parsed;
  196. }
  197. var numberOfFrames = data[0] + 1;
  198. var frameSizes = [];
  199. var offset = 1; // Fixed
  200. if (parsed.lacing === 2) {
  201. var sizeOfFrame = (data.length - offset) / numberOfFrames;
  202. for (var i = 0; i < numberOfFrames; i++) {
  203. frameSizes.push(sizeOfFrame);
  204. }
  205. } // xiph
  206. if (parsed.lacing === 1) {
  207. for (var _i = 0; _i < numberOfFrames - 1; _i++) {
  208. var size = 0;
  209. do {
  210. size += data[offset];
  211. offset++;
  212. } while (data[offset - 1] === 0xFF);
  213. frameSizes.push(size);
  214. }
  215. } // ebml
  216. if (parsed.lacing === 3) {
  217. // first vint is unsinged
  218. // after that vints are singed and
  219. // based on a compounding size
  220. var _size = 0;
  221. for (var _i2 = 0; _i2 < numberOfFrames - 1; _i2++) {
  222. var vint = _i2 === 0 ? getvint(data, offset) : getvint(data, offset, true, true);
  223. _size += vint.value;
  224. frameSizes.push(_size);
  225. offset += vint.length;
  226. }
  227. }
  228. frameSizes.forEach(function (size) {
  229. parsed.frames.push(data.subarray(offset, offset + size));
  230. offset += size;
  231. });
  232. return parsed;
  233. }; // VP9 Codec Feature Metadata (CodecPrivate)
  234. // https://www.webmproject.org/docs/container/
  235. var parseVp9Private = function parseVp9Private(bytes) {
  236. var i = 0;
  237. var params = {};
  238. while (i < bytes.length) {
  239. var id = bytes[i] & 0x7f;
  240. var len = bytes[i + 1];
  241. var val = void 0;
  242. if (len === 1) {
  243. val = bytes[i + 2];
  244. } else {
  245. val = bytes.subarray(i + 2, i + 2 + len);
  246. }
  247. if (id === 1) {
  248. params.profile = val;
  249. } else if (id === 2) {
  250. params.level = val;
  251. } else if (id === 3) {
  252. params.bitDepth = val;
  253. } else if (id === 4) {
  254. params.chromaSubsampling = val;
  255. } else {
  256. params[id] = val;
  257. }
  258. i += 2 + len;
  259. }
  260. return params;
  261. };
  262. export var parseTracks = function parseTracks(bytes) {
  263. bytes = toUint8(bytes);
  264. var decodedTracks = [];
  265. var tracks = findEbml(bytes, [EBML_TAGS.Segment, EBML_TAGS.Tracks, EBML_TAGS.Track]);
  266. if (!tracks.length) {
  267. tracks = findEbml(bytes, [EBML_TAGS.Tracks, EBML_TAGS.Track]);
  268. }
  269. if (!tracks.length) {
  270. tracks = findEbml(bytes, [EBML_TAGS.Track]);
  271. }
  272. if (!tracks.length) {
  273. return decodedTracks;
  274. }
  275. tracks.forEach(function (track) {
  276. var trackType = findEbml(track, EBML_TAGS.TrackType)[0];
  277. if (!trackType || !trackType.length) {
  278. return;
  279. } // 1 is video, 2 is audio, 17 is subtitle
  280. // other values are unimportant in this context
  281. if (trackType[0] === 1) {
  282. trackType = 'video';
  283. } else if (trackType[0] === 2) {
  284. trackType = 'audio';
  285. } else if (trackType[0] === 17) {
  286. trackType = 'subtitle';
  287. } else {
  288. return;
  289. } // todo parse language
  290. var decodedTrack = {
  291. rawCodec: bytesToString(findEbml(track, [EBML_TAGS.CodecID])[0]),
  292. type: trackType,
  293. codecPrivate: findEbml(track, [EBML_TAGS.CodecPrivate])[0],
  294. number: bytesToNumber(findEbml(track, [EBML_TAGS.TrackNumber])[0]),
  295. defaultDuration: bytesToNumber(findEbml(track, [EBML_TAGS.DefaultDuration])[0]),
  296. default: findEbml(track, [EBML_TAGS.FlagDefault])[0],
  297. rawData: track
  298. };
  299. var codec = '';
  300. if (/V_MPEG4\/ISO\/AVC/.test(decodedTrack.rawCodec)) {
  301. codec = "avc1." + getAvcCodec(decodedTrack.codecPrivate);
  302. } else if (/V_MPEGH\/ISO\/HEVC/.test(decodedTrack.rawCodec)) {
  303. codec = "hev1." + getHvcCodec(decodedTrack.codecPrivate);
  304. } else if (/V_MPEG4\/ISO\/ASP/.test(decodedTrack.rawCodec)) {
  305. if (decodedTrack.codecPrivate) {
  306. codec = 'mp4v.20.' + decodedTrack.codecPrivate[4].toString();
  307. } else {
  308. codec = 'mp4v.20.9';
  309. }
  310. } else if (/^V_THEORA/.test(decodedTrack.rawCodec)) {
  311. codec = 'theora';
  312. } else if (/^V_VP8/.test(decodedTrack.rawCodec)) {
  313. codec = 'vp8';
  314. } else if (/^V_VP9/.test(decodedTrack.rawCodec)) {
  315. if (decodedTrack.codecPrivate) {
  316. var _parseVp9Private = parseVp9Private(decodedTrack.codecPrivate),
  317. profile = _parseVp9Private.profile,
  318. level = _parseVp9Private.level,
  319. bitDepth = _parseVp9Private.bitDepth,
  320. chromaSubsampling = _parseVp9Private.chromaSubsampling;
  321. codec = 'vp09.';
  322. codec += padStart(profile, 2, '0') + ".";
  323. codec += padStart(level, 2, '0') + ".";
  324. codec += padStart(bitDepth, 2, '0') + ".";
  325. codec += "" + padStart(chromaSubsampling, 2, '0'); // Video -> Colour -> Ebml name
  326. var matrixCoefficients = findEbml(track, [0xE0, [0x55, 0xB0], [0x55, 0xB1]])[0] || [];
  327. var videoFullRangeFlag = findEbml(track, [0xE0, [0x55, 0xB0], [0x55, 0xB9]])[0] || [];
  328. var transferCharacteristics = findEbml(track, [0xE0, [0x55, 0xB0], [0x55, 0xBA]])[0] || [];
  329. var colourPrimaries = findEbml(track, [0xE0, [0x55, 0xB0], [0x55, 0xBB]])[0] || []; // if we find any optional codec parameter specify them all.
  330. if (matrixCoefficients.length || videoFullRangeFlag.length || transferCharacteristics.length || colourPrimaries.length) {
  331. codec += "." + padStart(colourPrimaries[0], 2, '0');
  332. codec += "." + padStart(transferCharacteristics[0], 2, '0');
  333. codec += "." + padStart(matrixCoefficients[0], 2, '0');
  334. codec += "." + padStart(videoFullRangeFlag[0], 2, '0');
  335. }
  336. } else {
  337. codec = 'vp9';
  338. }
  339. } else if (/^V_AV1/.test(decodedTrack.rawCodec)) {
  340. codec = "av01." + getAv1Codec(decodedTrack.codecPrivate);
  341. } else if (/A_ALAC/.test(decodedTrack.rawCodec)) {
  342. codec = 'alac';
  343. } else if (/A_MPEG\/L2/.test(decodedTrack.rawCodec)) {
  344. codec = 'mp2';
  345. } else if (/A_MPEG\/L3/.test(decodedTrack.rawCodec)) {
  346. codec = 'mp3';
  347. } else if (/^A_AAC/.test(decodedTrack.rawCodec)) {
  348. if (decodedTrack.codecPrivate) {
  349. codec = 'mp4a.40.' + (decodedTrack.codecPrivate[0] >>> 3).toString();
  350. } else {
  351. codec = 'mp4a.40.2';
  352. }
  353. } else if (/^A_AC3/.test(decodedTrack.rawCodec)) {
  354. codec = 'ac-3';
  355. } else if (/^A_PCM/.test(decodedTrack.rawCodec)) {
  356. codec = 'pcm';
  357. } else if (/^A_MS\/ACM/.test(decodedTrack.rawCodec)) {
  358. codec = 'speex';
  359. } else if (/^A_EAC3/.test(decodedTrack.rawCodec)) {
  360. codec = 'ec-3';
  361. } else if (/^A_VORBIS/.test(decodedTrack.rawCodec)) {
  362. codec = 'vorbis';
  363. } else if (/^A_FLAC/.test(decodedTrack.rawCodec)) {
  364. codec = 'flac';
  365. } else if (/^A_OPUS/.test(decodedTrack.rawCodec)) {
  366. codec = 'opus';
  367. }
  368. decodedTrack.codec = codec;
  369. decodedTracks.push(decodedTrack);
  370. });
  371. return decodedTracks.sort(function (a, b) {
  372. return a.number - b.number;
  373. });
  374. };
  375. export var parseData = function parseData(data, tracks) {
  376. var allBlocks = [];
  377. var segment = findEbml(data, [EBML_TAGS.Segment])[0];
  378. var timestampScale = findEbml(segment, [EBML_TAGS.SegmentInfo, EBML_TAGS.TimestampScale])[0]; // in nanoseconds, defaults to 1ms
  379. if (timestampScale && timestampScale.length) {
  380. timestampScale = bytesToNumber(timestampScale);
  381. } else {
  382. timestampScale = 1000000;
  383. }
  384. var clusters = findEbml(segment, [EBML_TAGS.Cluster]);
  385. if (!tracks) {
  386. tracks = parseTracks(segment);
  387. }
  388. clusters.forEach(function (cluster, ci) {
  389. var simpleBlocks = findEbml(cluster, [EBML_TAGS.SimpleBlock]).map(function (b) {
  390. return {
  391. type: 'simple',
  392. data: b
  393. };
  394. });
  395. var blockGroups = findEbml(cluster, [EBML_TAGS.BlockGroup]).map(function (b) {
  396. return {
  397. type: 'group',
  398. data: b
  399. };
  400. });
  401. var timestamp = findEbml(cluster, [EBML_TAGS.Timestamp])[0] || 0;
  402. if (timestamp && timestamp.length) {
  403. timestamp = bytesToNumber(timestamp);
  404. } // get all blocks then sort them into the correct order
  405. var blocks = simpleBlocks.concat(blockGroups).sort(function (a, b) {
  406. return a.data.byteOffset - b.data.byteOffset;
  407. });
  408. blocks.forEach(function (block, bi) {
  409. var decoded = decodeBlock(block.data, block.type, timestampScale, timestamp);
  410. allBlocks.push(decoded);
  411. });
  412. });
  413. return {
  414. tracks: tracks,
  415. blocks: allBlocks
  416. };
  417. };