H264SPSParser.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //import Map from './Map.js';
  2. let BITWISE0x00000007 = 0x00000007;
  3. let BITWISE0x7 = 0x7;
  4. let BITWISE2 = 2;
  5. let BITWISE3 = 3;
  6. let BITWISE4 = 4;
  7. let BITWISE5 = 5;
  8. let BITWISE6 = 6;
  9. let BITWISE8 = 8;
  10. let BITWISE12 = 12;
  11. let BITWISE15 = 15;
  12. let BITWISE16 = 16;
  13. let BITWISE32 = 32;
  14. let BITWISE64 = 64;
  15. let BITWISE255 = 255;
  16. let BITWISE256 = 256;
  17. function H264SPSParser() {
  18. let vBitCount = 0;
  19. let spsMap = null;
  20. let fps = null;
  21. function constructor() {
  22. spsMap = new Map();
  23. }
  24. constructor.prototype = {
  25. parse (pSPSBytes) {
  26. //console.log("=========================SPS START=========================");
  27. vBitCount = 0;
  28. spsMap.clear();
  29. // forbidden_zero_bit, nal_ref_idc, nal_unit_type
  30. spsMap.set("forbidden_zero_bit", readBits(pSPSBytes, 1));
  31. spsMap.set("nal_ref_idc", readBits(pSPSBytes, BITWISE2));
  32. spsMap.set("nal_unit_type", readBits(pSPSBytes, BITWISE5));
  33. // profile_idc
  34. spsMap.set("profile_idc", readBits(pSPSBytes, BITWISE8));
  35. spsMap.set("profile_compatibility", readBits(pSPSBytes, BITWISE8));
  36. // spsMap.set("constrained_set0_flag", readBits(pSPSBytes, 1));
  37. // spsMap.set("constrained_set1_flag", readBits(pSPSBytes, 1));
  38. // spsMap.set("constrained_set2_flag", readBits(pSPSBytes, 1));
  39. // spsMap.set("constrained_set3_flag", readBits(pSPSBytes, 1));
  40. // spsMap.set("constrained_set4_flag", readBits(pSPSBytes, 1));
  41. // spsMap.set("constrained_set5_flag", readBits(pSPSBytes, 1));
  42. // spsMap.set("reserved_zero_2bits", readBits(pSPSBytes, 2));
  43. // level_idc
  44. spsMap.set("level_idc", readBits(pSPSBytes, BITWISE8));
  45. spsMap.set("seq_parameter_set_id", ue(pSPSBytes, 0));
  46. let profileIdc = spsMap.get("profile_idc");
  47. let BITWISE100 = 100;
  48. let BITWISE110 = 110;
  49. let BITWISE122 = 122;
  50. let BITWISE244 = 244;
  51. let BITWISE44 = 44;
  52. let BITWISE83 = 83;
  53. let BITWISE86 = 86;
  54. let BITWISE118 = 118;
  55. let BITWISE128 = 128;
  56. let BITWISE138 = 138;
  57. let BITWISE139 = 139;
  58. let BITWISE134 = 134;
  59. if ((profileIdc === BITWISE100) || (profileIdc === BITWISE110) ||
  60. (profileIdc === BITWISE122) || (profileIdc === BITWISE244) ||
  61. (profileIdc === BITWISE44) || (profileIdc === BITWISE83) ||
  62. (profileIdc === BITWISE86) || (profileIdc === BITWISE118) ||
  63. (profileIdc === BITWISE128) || (profileIdc === BITWISE138) ||
  64. (profileIdc === BITWISE139) || (profileIdc === BITWISE134)) {
  65. spsMap.set("chroma_format_idc", ue(pSPSBytes, 0));
  66. if (spsMap.get("chroma_format_idc") === BITWISE3) {
  67. spsMap.set("separate_colour_plane_flag", readBits(pSPSBytes, 1));
  68. }
  69. spsMap.set("bit_depth_luma_minus8", ue(pSPSBytes, 0));
  70. spsMap.set("bit_depth_chroma_minus8", ue(pSPSBytes, 0));
  71. spsMap.set("qpprime_y_zero_transform_bypass_flag", readBits(pSPSBytes, 1));
  72. spsMap.set("seq_scaling_matrix_present_flag", readBits(pSPSBytes, 1));
  73. if (spsMap.get("seq_scaling_matrix_present_flag")) {
  74. let num = spsMap.get("chroma_format_idc") !== BITWISE3 ? BITWISE8 : BITWISE12;
  75. let seqScalingListPresentFlag = new Array(num);
  76. for (let i = 0; i < num; i++) {
  77. seqScalingListPresentFlag[i] = readBits(pSPSBytes, 1);
  78. if (seqScalingListPresentFlag[i]) {
  79. let slNumber = i < BITWISE6 ? BITWISE16 : BITWISE64;
  80. let lastScale = 8;
  81. let nextScale = 8;
  82. let deltaScale = 0;
  83. for (let j = 0; j < slNumber; j++) {
  84. if (nextScale) {
  85. deltaScale = se(pSPSBytes, 0);
  86. nextScale = (lastScale + deltaScale + BITWISE256) % BITWISE256;
  87. }
  88. lastScale = (nextScale === 0) ? lastScale : nextScale;
  89. }
  90. }
  91. }
  92. spsMap.set("seq_scaling_list_present_flag", seqScalingListPresentFlag);
  93. }
  94. }
  95. spsMap.set("log2_max_frame_num_minus4", ue(pSPSBytes, 0));
  96. spsMap.set("pic_order_cnt_type", ue(pSPSBytes, 0));
  97. if (spsMap.get("pic_order_cnt_type") === 0) {
  98. spsMap.set("log2_max_pic_order_cnt_lsb_minus4", ue(pSPSBytes, 0));
  99. } else if (spsMap.get("pic_order_cnt_type") === 1) {
  100. spsMap.set("delta_pic_order_always_zero_flag", readBits(pSPSBytes, 1));
  101. spsMap.set("offset_for_non_ref_pic", se(pSPSBytes, 0));
  102. spsMap.set("offset_for_top_to_bottom_field", se(pSPSBytes, 0));
  103. spsMap.set("num_ref_frames_in_pic_order_cnt_cycle", ue(pSPSBytes, 0));
  104. for (let numR = 0; numR < spsMap.get("num_ref_frames_in_pic_order_cnt_cycle"); numR++) {
  105. spsMap.set("num_ref_frames_in_pic_order_cnt_cycle", se(pSPSBytes, 0));
  106. }
  107. }
  108. spsMap.set("num_ref_frames", ue(pSPSBytes, 0));
  109. spsMap.set("gaps_in_frame_num_value_allowed_flag", readBits(pSPSBytes, 1));
  110. spsMap.set("pic_width_in_mbs_minus1", ue(pSPSBytes, 0));
  111. spsMap.set("pic_height_in_map_units_minus1", ue(pSPSBytes, 0));
  112. spsMap.set("frame_mbs_only_flag", readBits(pSPSBytes, 1));
  113. if (spsMap.get("frame_mbs_only_flag") === 0) {
  114. spsMap.set("mb_adaptive_frame_field_flag", readBits(pSPSBytes, 1));
  115. }
  116. spsMap.set("direct_8x8_interence_flag", readBits(pSPSBytes, 1));
  117. spsMap.set("frame_cropping_flag", readBits(pSPSBytes, 1));
  118. if (spsMap.get("frame_cropping_flag") === 1) {
  119. spsMap.set("frame_cropping_rect_left_offset", ue(pSPSBytes, 0));
  120. spsMap.set("frame_cropping_rect_right_offset", ue(pSPSBytes, 0));
  121. spsMap.set("frame_cropping_rect_top_offset", ue(pSPSBytes, 0));
  122. spsMap.set("frame_cropping_rect_bottom_offset", ue(pSPSBytes, 0));
  123. }
  124. //vui parameters
  125. spsMap.set("vui_parameters_present_flag", readBits(pSPSBytes, 1));
  126. if (spsMap.get("vui_parameters_present_flag")) {
  127. vuiParameters(pSPSBytes);
  128. }
  129. //console.log("=========================SPS END=========================");
  130. return true;
  131. },
  132. getSizeInfo () {
  133. let SubWidthC = 0;
  134. let SubHeightC = 0;
  135. if (spsMap.get("chroma_format_idc") === 0) { //monochrome
  136. SubWidthC = SubHeightC = 0;
  137. } else if (spsMap.get("chroma_format_idc") === 1) { //4:2:0
  138. SubWidthC = SubHeightC = BITWISE2;
  139. } else if (spsMap.get("chroma_format_idc") === BITWISE2) { //4:2:2
  140. SubWidthC = BITWISE2;
  141. SubHeightC = 1;
  142. } else if (spsMap.get("chroma_format_idc") === BITWISE3) { //4:4:4
  143. if (spsMap.get("separate_colour_plane_flag") === 0) {
  144. SubWidthC = SubHeightC = 1;
  145. } else if (spsMap.get("separate_colour_plane_flag") === 1) {
  146. SubWidthC = SubHeightC = 0;
  147. }
  148. }
  149. let PicWidthInMbs = spsMap.get("pic_width_in_mbs_minus1") + 1;
  150. let PicHeightInMapUnits = spsMap.get("pic_height_in_map_units_minus1") + 1;
  151. let FrameHeightInMbs = (BITWISE2 - spsMap.get("frame_mbs_only_flag")) * PicHeightInMapUnits;
  152. let cropLeft = 0;
  153. let cropRight = 0;
  154. let cropTop = 0;
  155. let cropBottom = 0;
  156. if (spsMap.get("frame_cropping_flag") === 1) {
  157. cropLeft = spsMap.get("frame_cropping_rect_left_offset");
  158. cropRight = spsMap.get("frame_cropping_rect_right_offset");
  159. cropTop = spsMap.get("frame_cropping_rect_top_offset");
  160. cropBottom = spsMap.get("frame_cropping_rect_bottom_offset");
  161. }
  162. let decodeSize = (PicWidthInMbs * BITWISE16) * (FrameHeightInMbs * BITWISE16);
  163. let width = (PicWidthInMbs * BITWISE16) - (SubWidthC * (cropLeft + cropRight));
  164. let height = (FrameHeightInMbs * BITWISE16) -
  165. (SubHeightC * (BITWISE2 - spsMap.get("frame_mbs_only_flag")) * (cropTop + cropBottom));
  166. let sizeInfo = {
  167. 'width': width,
  168. 'height': height,
  169. 'decodeSize': decodeSize,
  170. };
  171. return sizeInfo;
  172. },
  173. getSpsValue (key) {
  174. return spsMap.get(key);
  175. },
  176. getCodecInfo () {
  177. let profileIdc = spsMap.get("profile_idc").toString(BITWISE16);
  178. let profileCompatibility = spsMap.get("profile_compatibility") < BITWISE15 ?
  179. "0" + spsMap.get("profile_compatibility").toString(BITWISE16) :
  180. spsMap.get("profile_compatibility").toString(BITWISE16);
  181. let levelIdc = spsMap.get("level_idc").toString(BITWISE16);
  182. //console.log("getCodecInfo = " + (profile_idc + profile_compatibility + level_idc));
  183. return profileIdc + profileCompatibility + levelIdc;
  184. },
  185. getSpsMap() {
  186. return spsMap;
  187. },
  188. getFPS() {
  189. return fps;
  190. }
  191. }
  192. return new constructor();
  193. function getBit(base, offset) {
  194. let offsetData = offset;
  195. let vCurBytes = (vBitCount + offsetData) >> BITWISE3;
  196. offsetData = (vBitCount + offset) & BITWISE0x00000007;
  197. return (((base[(vCurBytes)])) >> (BITWISE0x7 - (offsetData & BITWISE0x7))) & 0x1;
  198. }
  199. function readBits(pBuf, vReadBits) {
  200. let vOffset = 0;
  201. let vTmp = 0,
  202. vTmp2 = 0;
  203. if (vReadBits === 1) {
  204. vTmp = getBit(pBuf, vOffset);
  205. } else {
  206. for (let i = 0; i < vReadBits; i++) {
  207. vTmp2 = getBit(pBuf, i);
  208. vTmp = (vTmp << 1) + vTmp2;
  209. }
  210. }
  211. vBitCount += vReadBits;
  212. return vTmp;
  213. }
  214. function ue(base, offset) {
  215. let zeros = 0,
  216. vTmp = 0,
  217. vReturn = 0;
  218. let vIdx = offset;
  219. do {
  220. vTmp = getBit(base, vIdx++);
  221. if (vTmp === 0) {
  222. zeros++;
  223. }
  224. } while (0 === vTmp);
  225. if (zeros === 0) {
  226. vBitCount += 1;
  227. return 0;
  228. }
  229. vReturn = 1 << zeros;
  230. for (let i = zeros - 1; i >= 0; i--, vIdx++) {
  231. vTmp = getBit(base, vIdx);
  232. vReturn |= vTmp << i;
  233. }
  234. let addBitCount = (zeros * BITWISE2) + 1;
  235. vBitCount += addBitCount;
  236. return (vReturn - 1);
  237. }
  238. function se(base, offset) {
  239. let vReturn = ue(base, offset);
  240. if (vReturn & 0x1) {
  241. return (vReturn + 1) / BITWISE2;
  242. } else {
  243. return -vReturn / BITWISE2;
  244. }
  245. }
  246. function hrdParameters(pSPSBytes) {
  247. spsMap.set("cpb_cnt_minus1", ue(pSPSBytes, 0));
  248. spsMap.set("bit_rate_scale", readBits(pSPSBytes, BITWISE4));
  249. spsMap.set("cpb_size_scale", readBits(pSPSBytes, BITWISE4));
  250. let cpdCntMinus1 = spsMap.get("cpb_cnt_minus1");
  251. let bitRateValueMinus1 = new Array(cpdCntMinus1);
  252. let cpbSizeValueMinus1 = new Array(cpdCntMinus1);
  253. let cbrFlag = new Array(cpdCntMinus1);
  254. //Todo: 原本为i <= cpdCntMinus1,运行到此处时直接停住,原因不明,改为<后正常
  255. for (let i = 0; i < cpdCntMinus1; i++) {
  256. bitRateValueMinus1[i] = ue(pSPSBytes, 0);
  257. cpbSizeValueMinus1[i] = ue(pSPSBytes, 0);
  258. cbrFlag[i] = readBits(pSPSBytes, 1);
  259. }
  260. spsMap.set("bit_rate_value_minus1", bitRateValueMinus1);
  261. spsMap.set("cpb_size_value_minus1", cpbSizeValueMinus1);
  262. spsMap.set("cbr_flag", cbrFlag);
  263. spsMap.set("initial_cpb_removal_delay_length_minus1", readBits(pSPSBytes, BITWISE4));
  264. spsMap.set("cpb_removal_delay_length_minus1", readBits(pSPSBytes, BITWISE4));
  265. spsMap.set("dpb_output_delay_length_minus1", readBits(pSPSBytes, BITWISE4));
  266. spsMap.set("time_offset_length", readBits(pSPSBytes, BITWISE4));
  267. }
  268. function vuiParameters(pSPSBytes) {
  269. spsMap.set("aspect_ratio_info_present_flag", readBits(pSPSBytes, 1));
  270. if (spsMap.get("aspect_ratio_info_present_flag")) {
  271. spsMap.set("aspect_ratio_idc", readBits(pSPSBytes, BITWISE8));
  272. //Extended_SAR
  273. if (spsMap.get("aspect_ratio_idc") === BITWISE255) {
  274. spsMap.set("sar_width", readBits(pSPSBytes, BITWISE16));
  275. spsMap.set("sar_height", readBits(pSPSBytes, BITWISE16));
  276. }
  277. }
  278. spsMap.set("overscan_info_present_flag", readBits(pSPSBytes, 1));
  279. if (spsMap.get("overscan_info_present_flag")) {
  280. spsMap.set("overscan_appropriate_flag", readBits(pSPSBytes, 1));
  281. }
  282. spsMap.set("video_signal_type_present_flag", readBits(pSPSBytes, 1));
  283. if (spsMap.get("video_signal_type_present_flag")) {
  284. spsMap.set("video_format", readBits(pSPSBytes, BITWISE3));
  285. spsMap.set("video_full_range_flag", readBits(pSPSBytes, 1));
  286. spsMap.set("colour_description_present_flag", readBits(pSPSBytes, 1));
  287. if (spsMap.get("colour_description_present_flag")) {
  288. spsMap.set("colour_primaries", readBits(pSPSBytes, BITWISE8));
  289. spsMap.set("transfer_characteristics", readBits(pSPSBytes, BITWISE8));
  290. spsMap.set("matrix_coefficients", readBits(pSPSBytes, BITWISE8));
  291. }
  292. }
  293. spsMap.set("chroma_loc_info_present_flag", readBits(pSPSBytes, 1));
  294. if (spsMap.get("chroma_loc_info_present_flag")) {
  295. spsMap.set("chroma_sample_loc_type_top_field", ue(pSPSBytes, 0));
  296. spsMap.set("chroma_sample_loc_type_bottom_field", ue(pSPSBytes, 0));
  297. }
  298. spsMap.set("timing_info_present_flag", readBits(pSPSBytes, 1));
  299. if (spsMap.get("timing_info_present_flag")) {
  300. spsMap.set("num_units_in_tick", readBits(pSPSBytes, BITWISE32));
  301. spsMap.set("time_scale", readBits(pSPSBytes, BITWISE32));
  302. spsMap.set("fixed_frame_rate_flag", readBits(pSPSBytes, 1));
  303. fps = spsMap.get("time_scale") / spsMap.get("num_units_in_tick");
  304. if(spsMap.get("fixed_frame_rate_flag")) {
  305. fps = fps / 2;
  306. }
  307. }
  308. spsMap.set("nal_hrd_parameters_present_flag", readBits(pSPSBytes, 1));
  309. if (spsMap.get("nal_hrd_parameters_present_flag")) {
  310. hrdParameters(pSPSBytes);
  311. }
  312. spsMap.set("vcl_hrd_parameters_present_flag", readBits(pSPSBytes, 1));
  313. if (spsMap.get("vcl_hrd_parameters_present_flag")) {
  314. hrdParameters(pSPSBytes);
  315. }
  316. if (spsMap.get("nal_hrd_parameters_present_flag") ||
  317. spsMap.get("vcl_hrd_parameters_present_flag")) {
  318. spsMap.set("low_delay_hrd_flag", readBits(pSPSBytes, 1));
  319. }
  320. spsMap.set("pic_struct_present_flag", readBits(pSPSBytes, 1));
  321. spsMap.set("bitstream_restriction_flag", readBits(pSPSBytes, 1));
  322. if (spsMap.get("bitstream_restriction_flag")) {
  323. spsMap.set("motion_vectors_over_pic_boundaries_flag", readBits(pSPSBytes, 1));
  324. spsMap.set("max_bytes_per_pic_denom", ue(pSPSBytes, 0));
  325. spsMap.set("max_bits_per_mb_denom", ue(pSPSBytes, 0));
  326. spsMap.set("log2_max_mv_length_horizontal", ue(pSPSBytes, 0));
  327. spsMap.set("log2_max_mv_length_vertical", ue(pSPSBytes, 0));
  328. spsMap.set("max_num_reorder_frames", ue(pSPSBytes, 0));
  329. spsMap.set("max_dec_frame_buffering", ue(pSPSBytes, 0));
  330. }
  331. }
  332. }
  333. //export default H264SPSParser;