nal-helpers.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.findH265Nal = exports.findH264Nal = exports.findNal = exports.discardEmulationPreventionBytes = exports.EMULATION_PREVENTION = exports.NAL_TYPE_TWO = exports.NAL_TYPE_ONE = void 0;
  6. var _byteHelpers = require("./byte-helpers.js");
  7. var NAL_TYPE_ONE = (0, _byteHelpers.toUint8)([0x00, 0x00, 0x00, 0x01]);
  8. exports.NAL_TYPE_ONE = NAL_TYPE_ONE;
  9. var NAL_TYPE_TWO = (0, _byteHelpers.toUint8)([0x00, 0x00, 0x01]);
  10. exports.NAL_TYPE_TWO = NAL_TYPE_TWO;
  11. var EMULATION_PREVENTION = (0, _byteHelpers.toUint8)([0x00, 0x00, 0x03]);
  12. /**
  13. * Expunge any "Emulation Prevention" bytes from a "Raw Byte
  14. * Sequence Payload"
  15. *
  16. * @param data {Uint8Array} the bytes of a RBSP from a NAL
  17. * unit
  18. * @return {Uint8Array} the RBSP without any Emulation
  19. * Prevention Bytes
  20. */
  21. exports.EMULATION_PREVENTION = EMULATION_PREVENTION;
  22. var discardEmulationPreventionBytes = function discardEmulationPreventionBytes(bytes) {
  23. var positions = [];
  24. var i = 1; // Find all `Emulation Prevention Bytes`
  25. while (i < bytes.length - 2) {
  26. if ((0, _byteHelpers.bytesMatch)(bytes.subarray(i, i + 3), EMULATION_PREVENTION)) {
  27. positions.push(i + 2);
  28. i++;
  29. }
  30. i++;
  31. } // If no Emulation Prevention Bytes were found just return the original
  32. // array
  33. if (positions.length === 0) {
  34. return bytes;
  35. } // Create a new array to hold the NAL unit data
  36. var newLength = bytes.length - positions.length;
  37. var newData = new Uint8Array(newLength);
  38. var sourceIndex = 0;
  39. for (i = 0; i < newLength; sourceIndex++, i++) {
  40. if (sourceIndex === positions[0]) {
  41. // Skip this byte
  42. sourceIndex++; // Remove this position index
  43. positions.shift();
  44. }
  45. newData[i] = bytes[sourceIndex];
  46. }
  47. return newData;
  48. };
  49. exports.discardEmulationPreventionBytes = discardEmulationPreventionBytes;
  50. var findNal = function findNal(bytes, dataType, types, nalLimit) {
  51. if (nalLimit === void 0) {
  52. nalLimit = Infinity;
  53. }
  54. bytes = (0, _byteHelpers.toUint8)(bytes);
  55. types = [].concat(types);
  56. var i = 0;
  57. var nalStart;
  58. var nalsFound = 0; // keep searching until:
  59. // we reach the end of bytes
  60. // we reach the maximum number of nals they want to seach
  61. // NOTE: that we disregard nalLimit when we have found the start
  62. // of the nal we want so that we can find the end of the nal we want.
  63. while (i < bytes.length && (nalsFound < nalLimit || nalStart)) {
  64. var nalOffset = void 0;
  65. if ((0, _byteHelpers.bytesMatch)(bytes.subarray(i), NAL_TYPE_ONE)) {
  66. nalOffset = 4;
  67. } else if ((0, _byteHelpers.bytesMatch)(bytes.subarray(i), NAL_TYPE_TWO)) {
  68. nalOffset = 3;
  69. } // we are unsynced,
  70. // find the next nal unit
  71. if (!nalOffset) {
  72. i++;
  73. continue;
  74. }
  75. nalsFound++;
  76. if (nalStart) {
  77. return discardEmulationPreventionBytes(bytes.subarray(nalStart, i));
  78. }
  79. var nalType = void 0;
  80. if (dataType === 'h264') {
  81. nalType = bytes[i + nalOffset] & 0x1f;
  82. } else if (dataType === 'h265') {
  83. nalType = bytes[i + nalOffset] >> 1 & 0x3f;
  84. }
  85. if (types.indexOf(nalType) !== -1) {
  86. nalStart = i + nalOffset;
  87. } // nal header is 1 length for h264, and 2 for h265
  88. i += nalOffset + (dataType === 'h264' ? 1 : 2);
  89. }
  90. return bytes.subarray(0, 0);
  91. };
  92. exports.findNal = findNal;
  93. var findH264Nal = function findH264Nal(bytes, type, nalLimit) {
  94. return findNal(bytes, 'h264', type, nalLimit);
  95. };
  96. exports.findH264Nal = findH264Nal;
  97. var findH265Nal = function findH265Nal(bytes, type, nalLimit) {
  98. return findNal(bytes, 'h265', type, nalLimit);
  99. };
  100. exports.findH265Nal = findH265Nal;