spsParser.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //https://blog.csdn.net/lizhijian21/article/details/80982403
  2. function ceil(val) {
  3. return Math.ceil(val);
  4. }
  5. //获取buf 的前n个bit组成的值
  6. function u(bitCount, input) {
  7. let ret = 0;
  8. for (var i = 0; i < bitCount; i++) {
  9. ret <<= 1;
  10. if (input.data[Math.floor(input.index / 8)] & (0x80 >> (input.index % 8))) {
  11. ret += 1;
  12. }
  13. input.index++;
  14. }
  15. return ret;
  16. }
  17. /*无符号指数哥伦布编码(UE)
  18. *哥伦布编码的码字code_word由三部分组成:code_word = [M个0] + [1] + [Info]
  19. *其中,Info是一个携带信息的M位数据,每个哥伦布码的长度为(2M+1)位,每个码字都可由code_num产生。
  20. *根据码字code_word解码出code_num值的过程如下:
  21. *1. 首先读入M位以"1"为结尾的0;
  22. *2. 根据得到的M,读入接下来的M位Info数据;
  23. *3. 根据这个公式得到计算结果code_num = Info – 1 + 2M
  24. */
  25. function ue(input, len) {
  26. let zeroNum = 0;
  27. while (input.index < len * 8) {
  28. if (input.data[Math.floor(input.index / 8)] & (0x80 >> (input.index % 8)))//遇到1则停止,统计0的个数
  29. {
  30. break;
  31. }
  32. zeroNum++;
  33. input.index++;
  34. }
  35. input.index++;
  36. let ret = 0;
  37. //计算
  38. for (var i = 0; i < zeroNum; i++) {
  39. ret <<= 1;
  40. if (input.data[Math.floor(input.index / 8)] & (0x80 >> input.index % 8)) {
  41. ret += 1;
  42. }
  43. input.index++;
  44. }
  45. return (1 << zeroNum) - 1 + ret;
  46. }
  47. //有符号哥伦布编码
  48. function se(input, len) {
  49. let ueVal = ue(input, len);
  50. let k = ueVal;
  51. let nValue = ceil(k / 2);
  52. if (ueVal % 2 == 0)
  53. nValue = -nValue;
  54. return nValue;
  55. }
  56. function spsParser(buf) {
  57. let startBitIndex = 0;
  58. buf = buf.slice(4);//去除00 00 00 01竞争码
  59. let len = buf.length;
  60. //输入参数
  61. let input = {
  62. data: buf,
  63. index: startBitIndex
  64. };
  65. let forbidden_zero_bit = u(1, input);
  66. let nal_ref_idc = u(2, input);
  67. let nal_unit_type = u(5, input);
  68. let chroma_format_idc;
  69. if (nal_unit_type == 7) {
  70. let profile_idc = u(8, input);
  71. let constraint_set0_flag = u(1, input);
  72. let constraint_set1_flag = u(1, input);
  73. let constraint_set2_flag = u(1, input);
  74. let constraint_set3_flag = u(1, input);
  75. let constraint_set4_flag = u(1, input);
  76. let constraint_set5_flag = u(1, input);
  77. let reserved_zero_2bits = u(2, input);
  78. let level_idc = u(8, input);
  79. let seq_parameter_set_id = ue(input, len);
  80. if (profile_idc == 100 | profile_idc == 110 || profile_idc == 122 || profile_idc == 144) {
  81. chroma_format_idc = ue(input, len);
  82. if (chroma_format_idc == 3) {
  83. var residual_colour_transform_flag = u(1, input);
  84. }
  85. let bit_depth_luma_minus8 = ue(input, len);
  86. let bit_depth_chroma_minus8 = ue(input, len);
  87. let qpprime_y_zero_transform_bypass_flag = u(1, input);
  88. let seq_scaling_matrix_present_flag = u(1, input);
  89. let seq_scaling_list_present_flag = new Uint8Array(8);
  90. if (seq_scaling_matrix_present_flag) {
  91. for (var i = 0; i < 8; i++) {
  92. seq_scaling_list_present_flag[i] = u(1, input);
  93. }
  94. }
  95. }
  96. let log2_max_frame_num_minus4 = ue(input, len);
  97. let pic_order_cnt_type = ue(input, len);
  98. if (pic_order_cnt_type == 0)
  99. log2_max_pic_order_cnt_lsb_minus4 = ue(input, len);
  100. else if (pic_order_cnt_type == 1) {
  101. let delta_pic_order_always_zero_flag = u(1, input);
  102. let offset_for_non_ref_pic = se(input, len);
  103. let offset_for_top_to_bottom_field = se(input, len);
  104. let num_ref_frames_in_pic_order_cnt_cycle = ue(input, len);
  105. let offset_for_ref_frame = new Uint8Array[num_ref_frames_in_pic_order_cnt_cycle];
  106. for (var i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++)
  107. offset_for_ref_frame[i] = se(input, len);
  108. }
  109. let num_ref_frames = ue(input, len);
  110. let gaps_in_frame_num_value_allowed_flag = u(1, input);
  111. let pic_width_in_mbs_minus1 = ue(input, len);
  112. let pic_height_in_map_units_minus1 = ue(input, len);
  113. let width = (pic_width_in_mbs_minus1 + 1) * 16;//可能还要进行裁剪处理
  114. let height = (pic_height_in_map_units_minus1 + 1) * 16;
  115. let frame_mbs_only_flag = u(1, input);
  116. if (!frame_mbs_only_flag) {
  117. u(1, input);
  118. }
  119. let direct_8x8_inference_flag = u(1, input);
  120. let frame_cropping_flag = u(1, input);
  121. if (frame_cropping_flag) {
  122. let frame_crop_left_offset = ue(input, len);
  123. let frame_crop_right_offset = ue(input, len);
  124. let frame_crop_top_offset = ue(input, len);
  125. let frame_crop_bottom_offset = ue(input, len);
  126. let crop_unit_x = 1;
  127. let crop_unit_y = 2 - frame_mbs_only_flag;
  128. if (chroma_format_idc == 1) { //4:2:0
  129. crop_unit_x = 2;
  130. crop_unit_y = 2 * (2 - frame_mbs_only_flag);
  131. }
  132. else if (chroma_format_idc == 2) { //4:2:2
  133. crop_unit_x = 2;
  134. crop_unit_y = 2 - frame_mbs_only_flag;
  135. }
  136. width -= crop_unit_x * (frame_crop_left_offset + frame_crop_right_offset);
  137. height -= crop_unit_y * (frame_crop_top_offset + frame_crop_bottom_offset);
  138. }
  139. return {
  140. width: width,
  141. height: height
  142. }
  143. }
  144. }