exp-golomb.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * mux.js
  3. *
  4. * Copyright (c) Brightcove
  5. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6. */
  7. 'use strict';
  8. var ExpGolomb;
  9. /**
  10. * Parser for exponential Golomb codes, a variable-bitwidth number encoding
  11. * scheme used by h264.
  12. */
  13. ExpGolomb = function ExpGolomb(workingData) {
  14. var // the number of bytes left to examine in workingData
  15. workingBytesAvailable = workingData.byteLength,
  16. // the current word being examined
  17. workingWord = 0,
  18. // :uint
  19. // the number of bits left to examine in the current word
  20. workingBitsAvailable = 0; // :uint;
  21. // ():uint
  22. this.length = function () {
  23. return 8 * workingBytesAvailable;
  24. }; // ():uint
  25. this.bitsAvailable = function () {
  26. return 8 * workingBytesAvailable + workingBitsAvailable;
  27. }; // ():void
  28. this.loadWord = function () {
  29. var position = workingData.byteLength - workingBytesAvailable,
  30. workingBytes = new Uint8Array(4),
  31. availableBytes = Math.min(4, workingBytesAvailable);
  32. if (availableBytes === 0) {
  33. throw new Error('no bytes available');
  34. }
  35. workingBytes.set(workingData.subarray(position, position + availableBytes));
  36. workingWord = new DataView(workingBytes.buffer).getUint32(0); // track the amount of workingData that has been processed
  37. workingBitsAvailable = availableBytes * 8;
  38. workingBytesAvailable -= availableBytes;
  39. }; // (count:int):void
  40. this.skipBits = function (count) {
  41. var skipBytes; // :int
  42. if (workingBitsAvailable > count) {
  43. workingWord <<= count;
  44. workingBitsAvailable -= count;
  45. } else {
  46. count -= workingBitsAvailable;
  47. skipBytes = Math.floor(count / 8);
  48. count -= skipBytes * 8;
  49. workingBytesAvailable -= skipBytes;
  50. this.loadWord();
  51. workingWord <<= count;
  52. workingBitsAvailable -= count;
  53. }
  54. }; // (size:int):uint
  55. this.readBits = function (size) {
  56. var bits = Math.min(workingBitsAvailable, size),
  57. // :uint
  58. valu = workingWord >>> 32 - bits; // :uint
  59. // if size > 31, handle error
  60. workingBitsAvailable -= bits;
  61. if (workingBitsAvailable > 0) {
  62. workingWord <<= bits;
  63. } else if (workingBytesAvailable > 0) {
  64. this.loadWord();
  65. }
  66. bits = size - bits;
  67. if (bits > 0) {
  68. return valu << bits | this.readBits(bits);
  69. }
  70. return valu;
  71. }; // ():uint
  72. this.skipLeadingZeros = function () {
  73. var leadingZeroCount; // :uint
  74. for (leadingZeroCount = 0; leadingZeroCount < workingBitsAvailable; ++leadingZeroCount) {
  75. if ((workingWord & 0x80000000 >>> leadingZeroCount) !== 0) {
  76. // the first bit of working word is 1
  77. workingWord <<= leadingZeroCount;
  78. workingBitsAvailable -= leadingZeroCount;
  79. return leadingZeroCount;
  80. }
  81. } // we exhausted workingWord and still have not found a 1
  82. this.loadWord();
  83. return leadingZeroCount + this.skipLeadingZeros();
  84. }; // ():void
  85. this.skipUnsignedExpGolomb = function () {
  86. this.skipBits(1 + this.skipLeadingZeros());
  87. }; // ():void
  88. this.skipExpGolomb = function () {
  89. this.skipBits(1 + this.skipLeadingZeros());
  90. }; // ():uint
  91. this.readUnsignedExpGolomb = function () {
  92. var clz = this.skipLeadingZeros(); // :uint
  93. return this.readBits(clz + 1) - 1;
  94. }; // ():int
  95. this.readExpGolomb = function () {
  96. var valu = this.readUnsignedExpGolomb(); // :int
  97. if (0x01 & valu) {
  98. // the number is odd if the low order bit is set
  99. return 1 + valu >>> 1; // add 1 to make it even, and divide by 2
  100. }
  101. return -1 * (valu >>> 1); // divide by two then make it negative
  102. }; // Some convenience functions
  103. // :Boolean
  104. this.readBoolean = function () {
  105. return this.readBits(1) === 1;
  106. }; // ():int
  107. this.readUnsignedByte = function () {
  108. return this.readBits(8);
  109. };
  110. this.loadWord();
  111. };
  112. module.exports = ExpGolomb;