spsParser.js 5.0 KB

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