Home Reference Source

src/demux/exp-golomb.ts

  1. /**
  2. * Parser for exponential Golomb codes, a variable-bitwidth number encoding scheme used by h264.
  3. */
  4.  
  5. import { logger } from '../utils/logger';
  6.  
  7. class ExpGolomb {
  8. private data: Uint8Array;
  9. public bytesAvailable: number;
  10. private word: number;
  11. private bitsAvailable: number;
  12.  
  13. constructor(data: Uint8Array) {
  14. this.data = data;
  15. // the number of bytes left to examine in this.data
  16. this.bytesAvailable = data.byteLength;
  17. // the current word being examined
  18. this.word = 0; // :uint
  19. // the number of bits left to examine in the current word
  20. this.bitsAvailable = 0; // :uint
  21. }
  22.  
  23. // ():void
  24. loadWord(): void {
  25. const data = this.data;
  26. const bytesAvailable = this.bytesAvailable;
  27. const position = data.byteLength - bytesAvailable;
  28. const workingBytes = new Uint8Array(4);
  29. const availableBytes = Math.min(4, bytesAvailable);
  30. if (availableBytes === 0) {
  31. throw new Error('no bytes available');
  32. }
  33.  
  34. workingBytes.set(data.subarray(position, position + availableBytes));
  35. this.word = new DataView(workingBytes.buffer).getUint32(0);
  36. // track the amount of this.data that has been processed
  37. this.bitsAvailable = availableBytes * 8;
  38. this.bytesAvailable -= availableBytes;
  39. }
  40.  
  41. // (count:int):void
  42. skipBits(count: number): void {
  43. let skipBytes; // :int
  44. if (this.bitsAvailable > count) {
  45. this.word <<= count;
  46. this.bitsAvailable -= count;
  47. } else {
  48. count -= this.bitsAvailable;
  49. skipBytes = count >> 3;
  50. count -= skipBytes >> 3;
  51. this.bytesAvailable -= skipBytes;
  52. this.loadWord();
  53. this.word <<= count;
  54. this.bitsAvailable -= count;
  55. }
  56. }
  57.  
  58. // (size:int):uint
  59. readBits(size: number): number {
  60. let bits = Math.min(this.bitsAvailable, size); // :uint
  61. const valu = this.word >>> (32 - bits); // :uint
  62. if (size > 32) {
  63. logger.error('Cannot read more than 32 bits at a time');
  64. }
  65.  
  66. this.bitsAvailable -= bits;
  67. if (this.bitsAvailable > 0) {
  68. this.word <<= bits;
  69. } else if (this.bytesAvailable > 0) {
  70. this.loadWord();
  71. }
  72.  
  73. bits = size - bits;
  74. if (bits > 0 && this.bitsAvailable) {
  75. return (valu << bits) | this.readBits(bits);
  76. } else {
  77. return valu;
  78. }
  79. }
  80.  
  81. // ():uint
  82. skipLZ(): number {
  83. let leadingZeroCount; // :uint
  84. for (
  85. leadingZeroCount = 0;
  86. leadingZeroCount < this.bitsAvailable;
  87. ++leadingZeroCount
  88. ) {
  89. if ((this.word & (0x80000000 >>> leadingZeroCount)) !== 0) {
  90. // the first bit of working word is 1
  91. this.word <<= leadingZeroCount;
  92. this.bitsAvailable -= leadingZeroCount;
  93. return leadingZeroCount;
  94. }
  95. }
  96. // we exhausted word and still have not found a 1
  97. this.loadWord();
  98. return leadingZeroCount + this.skipLZ();
  99. }
  100.  
  101. // ():void
  102. skipUEG(): void {
  103. this.skipBits(1 + this.skipLZ());
  104. }
  105.  
  106. // ():void
  107. skipEG(): void {
  108. this.skipBits(1 + this.skipLZ());
  109. }
  110.  
  111. // ():uint
  112. readUEG(): number {
  113. const clz = this.skipLZ(); // :uint
  114. return this.readBits(clz + 1) - 1;
  115. }
  116.  
  117. // ():int
  118. readEG(): number {
  119. const valu = this.readUEG(); // :int
  120. if (0x01 & valu) {
  121. // the number is odd if the low order bit is set
  122. return (1 + valu) >>> 1; // add 1 to make it even, and divide by 2
  123. } else {
  124. return -1 * (valu >>> 1); // divide by two then make it negative
  125. }
  126. }
  127.  
  128. // Some convenience functions
  129. // :Boolean
  130. readBoolean(): boolean {
  131. return this.readBits(1) === 1;
  132. }
  133.  
  134. // ():int
  135. readUByte(): number {
  136. return this.readBits(8);
  137. }
  138.  
  139. // ():int
  140. readUShort(): number {
  141. return this.readBits(16);
  142. }
  143.  
  144. // ():int
  145. readUInt(): number {
  146. return this.readBits(32);
  147. }
  148.  
  149. /**
  150. * Advance the ExpGolomb decoder past a scaling list. The scaling
  151. * list is optionally transmitted as part of a sequence parameter
  152. * set and is not relevant to transmuxing.
  153. * @param count the number of entries in this scaling list
  154. * @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
  155. */
  156. skipScalingList(count: number): void {
  157. let lastScale = 8;
  158. let nextScale = 8;
  159. let deltaScale;
  160. for (let j = 0; j < count; j++) {
  161. if (nextScale !== 0) {
  162. deltaScale = this.readEG();
  163. nextScale = (lastScale + deltaScale + 256) % 256;
  164. }
  165. lastScale = nextScale === 0 ? lastScale : nextScale;
  166. }
  167. }
  168.  
  169. /**
  170. * Read a sequence parameter set and return some interesting video
  171. * properties. A sequence parameter set is the H264 metadata that
  172. * describes the properties of upcoming video frames.
  173. * @param data {Uint8Array} the bytes of a sequence parameter set
  174. * @return {object} an object with configuration parsed from the
  175. * sequence parameter set, including the dimensions of the
  176. * associated video frames.
  177. */
  178. readSPS(): {
  179. width: number;
  180. height: number;
  181. pixelRatio: [number, number];
  182. } {
  183. let frameCropLeftOffset = 0;
  184. let frameCropRightOffset = 0;
  185. let frameCropTopOffset = 0;
  186. let frameCropBottomOffset = 0;
  187. let numRefFramesInPicOrderCntCycle;
  188. let scalingListCount;
  189. let i;
  190. const readUByte = this.readUByte.bind(this);
  191. const readBits = this.readBits.bind(this);
  192. const readUEG = this.readUEG.bind(this);
  193. const readBoolean = this.readBoolean.bind(this);
  194. const skipBits = this.skipBits.bind(this);
  195. const skipEG = this.skipEG.bind(this);
  196. const skipUEG = this.skipUEG.bind(this);
  197. const skipScalingList = this.skipScalingList.bind(this);
  198.  
  199. readUByte();
  200. const profileIdc = readUByte(); // profile_idc
  201. readBits(5); // profileCompat constraint_set[0-4]_flag, u(5)
  202. skipBits(3); // reserved_zero_3bits u(3),
  203. readUByte(); // level_idc u(8)
  204. skipUEG(); // seq_parameter_set_id
  205. // some profiles have more optional data we don't need
  206. if (
  207. profileIdc === 100 ||
  208. profileIdc === 110 ||
  209. profileIdc === 122 ||
  210. profileIdc === 244 ||
  211. profileIdc === 44 ||
  212. profileIdc === 83 ||
  213. profileIdc === 86 ||
  214. profileIdc === 118 ||
  215. profileIdc === 128
  216. ) {
  217. const chromaFormatIdc = readUEG();
  218. if (chromaFormatIdc === 3) {
  219. skipBits(1);
  220. } // separate_colour_plane_flag
  221.  
  222. skipUEG(); // bit_depth_luma_minus8
  223. skipUEG(); // bit_depth_chroma_minus8
  224. skipBits(1); // qpprime_y_zero_transform_bypass_flag
  225. if (readBoolean()) {
  226. // seq_scaling_matrix_present_flag
  227. scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
  228. for (i = 0; i < scalingListCount; i++) {
  229. if (readBoolean()) {
  230. // seq_scaling_list_present_flag[ i ]
  231. if (i < 6) {
  232. skipScalingList(16);
  233. } else {
  234. skipScalingList(64);
  235. }
  236. }
  237. }
  238. }
  239. }
  240. skipUEG(); // log2_max_frame_num_minus4
  241. const picOrderCntType = readUEG();
  242. if (picOrderCntType === 0) {
  243. readUEG(); // log2_max_pic_order_cnt_lsb_minus4
  244. } else if (picOrderCntType === 1) {
  245. skipBits(1); // delta_pic_order_always_zero_flag
  246. skipEG(); // offset_for_non_ref_pic
  247. skipEG(); // offset_for_top_to_bottom_field
  248. numRefFramesInPicOrderCntCycle = readUEG();
  249. for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
  250. skipEG();
  251. } // offset_for_ref_frame[ i ]
  252. }
  253. skipUEG(); // max_num_ref_frames
  254. skipBits(1); // gaps_in_frame_num_value_allowed_flag
  255. const picWidthInMbsMinus1 = readUEG();
  256. const picHeightInMapUnitsMinus1 = readUEG();
  257. const frameMbsOnlyFlag = readBits(1);
  258. if (frameMbsOnlyFlag === 0) {
  259. skipBits(1);
  260. } // mb_adaptive_frame_field_flag
  261.  
  262. skipBits(1); // direct_8x8_inference_flag
  263. if (readBoolean()) {
  264. // frame_cropping_flag
  265. frameCropLeftOffset = readUEG();
  266. frameCropRightOffset = readUEG();
  267. frameCropTopOffset = readUEG();
  268. frameCropBottomOffset = readUEG();
  269. }
  270. let pixelRatio: [number, number] = [1, 1];
  271. if (readBoolean()) {
  272. // vui_parameters_present_flag
  273. if (readBoolean()) {
  274. // aspect_ratio_info_present_flag
  275. const aspectRatioIdc = readUByte();
  276. switch (aspectRatioIdc) {
  277. case 1:
  278. pixelRatio = [1, 1];
  279. break;
  280. case 2:
  281. pixelRatio = [12, 11];
  282. break;
  283. case 3:
  284. pixelRatio = [10, 11];
  285. break;
  286. case 4:
  287. pixelRatio = [16, 11];
  288. break;
  289. case 5:
  290. pixelRatio = [40, 33];
  291. break;
  292. case 6:
  293. pixelRatio = [24, 11];
  294. break;
  295. case 7:
  296. pixelRatio = [20, 11];
  297. break;
  298. case 8:
  299. pixelRatio = [32, 11];
  300. break;
  301. case 9:
  302. pixelRatio = [80, 33];
  303. break;
  304. case 10:
  305. pixelRatio = [18, 11];
  306. break;
  307. case 11:
  308. pixelRatio = [15, 11];
  309. break;
  310. case 12:
  311. pixelRatio = [64, 33];
  312. break;
  313. case 13:
  314. pixelRatio = [160, 99];
  315. break;
  316. case 14:
  317. pixelRatio = [4, 3];
  318. break;
  319. case 15:
  320. pixelRatio = [3, 2];
  321. break;
  322. case 16:
  323. pixelRatio = [2, 1];
  324. break;
  325. case 255: {
  326. pixelRatio = [
  327. (readUByte() << 8) | readUByte(),
  328. (readUByte() << 8) | readUByte(),
  329. ];
  330. break;
  331. }
  332. }
  333. }
  334. }
  335. return {
  336. width: Math.ceil(
  337. (picWidthInMbsMinus1 + 1) * 16 -
  338. frameCropLeftOffset * 2 -
  339. frameCropRightOffset * 2
  340. ),
  341. height:
  342. (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 -
  343. (frameMbsOnlyFlag ? 2 : 4) *
  344. (frameCropTopOffset + frameCropBottomOffset),
  345. pixelRatio: pixelRatio,
  346. };
  347. }
  348.  
  349. readSliceType() {
  350. // skip NALu type
  351. this.readUByte();
  352. // discard first_mb_in_slice
  353. this.readUEG();
  354. // return slice_type
  355. return this.readUEG();
  356. }
  357. }
  358.  
  359. export default ExpGolomb;