codec-helpers.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getHvcCodec = exports.getAvcCodec = exports.getAv1Codec = void 0;
  6. var _byteHelpers = require("./byte-helpers.js");
  7. // https://aomediacodec.github.io/av1-isobmff/#av1codecconfigurationbox-syntax
  8. // https://developer.mozilla.org/en-US/docs/Web/Media/Formats/codecs_parameter#AV1
  9. var getAv1Codec = function getAv1Codec(bytes) {
  10. var codec = '';
  11. var profile = bytes[1] >>> 3;
  12. var level = bytes[1] & 0x1F;
  13. var tier = bytes[2] >>> 7;
  14. var highBitDepth = (bytes[2] & 0x40) >> 6;
  15. var twelveBit = (bytes[2] & 0x20) >> 5;
  16. var monochrome = (bytes[2] & 0x10) >> 4;
  17. var chromaSubsamplingX = (bytes[2] & 0x08) >> 3;
  18. var chromaSubsamplingY = (bytes[2] & 0x04) >> 2;
  19. var chromaSamplePosition = bytes[2] & 0x03;
  20. codec += profile + "." + (0, _byteHelpers.padStart)(level, 2, '0');
  21. if (tier === 0) {
  22. codec += 'M';
  23. } else if (tier === 1) {
  24. codec += 'H';
  25. }
  26. var bitDepth;
  27. if (profile === 2 && highBitDepth) {
  28. bitDepth = twelveBit ? 12 : 10;
  29. } else {
  30. bitDepth = highBitDepth ? 10 : 8;
  31. }
  32. codec += "." + (0, _byteHelpers.padStart)(bitDepth, 2, '0'); // TODO: can we parse color range??
  33. codec += "." + monochrome;
  34. codec += "." + chromaSubsamplingX + chromaSubsamplingY + chromaSamplePosition;
  35. return codec;
  36. };
  37. exports.getAv1Codec = getAv1Codec;
  38. var getAvcCodec = function getAvcCodec(bytes) {
  39. var profileId = (0, _byteHelpers.toHexString)(bytes[1]);
  40. var constraintFlags = (0, _byteHelpers.toHexString)(bytes[2] & 0xFC);
  41. var levelId = (0, _byteHelpers.toHexString)(bytes[3]);
  42. return "" + profileId + constraintFlags + levelId;
  43. };
  44. exports.getAvcCodec = getAvcCodec;
  45. var getHvcCodec = function getHvcCodec(bytes) {
  46. var codec = '';
  47. var profileSpace = bytes[1] >> 6;
  48. var profileId = bytes[1] & 0x1F;
  49. var tierFlag = (bytes[1] & 0x20) >> 5;
  50. var profileCompat = bytes.subarray(2, 6);
  51. var constraintIds = bytes.subarray(6, 12);
  52. var levelId = bytes[12];
  53. if (profileSpace === 1) {
  54. codec += 'A';
  55. } else if (profileSpace === 2) {
  56. codec += 'B';
  57. } else if (profileSpace === 3) {
  58. codec += 'C';
  59. }
  60. codec += profileId + "."; // ffmpeg does this in big endian
  61. var profileCompatVal = parseInt((0, _byteHelpers.toBinaryString)(profileCompat).split('').reverse().join(''), 2); // apple does this in little endian...
  62. if (profileCompatVal > 255) {
  63. profileCompatVal = parseInt((0, _byteHelpers.toBinaryString)(profileCompat), 2);
  64. }
  65. codec += profileCompatVal.toString(16) + ".";
  66. if (tierFlag === 0) {
  67. codec += 'L';
  68. } else {
  69. codec += 'H';
  70. }
  71. codec += levelId;
  72. var constraints = '';
  73. for (var i = 0; i < constraintIds.length; i++) {
  74. var v = constraintIds[i];
  75. if (v) {
  76. if (constraints) {
  77. constraints += '.';
  78. }
  79. constraints += v.toString(16);
  80. }
  81. }
  82. if (constraints) {
  83. codec += "." + constraints;
  84. }
  85. return codec;
  86. };
  87. exports.getHvcCodec = getHvcCodec;