worker-parse-dist.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
  2. "use strict";
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = void 0;
  7. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  9. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  10. /*********************************************************
  11. * LICENSE: LICENSE-Free_CN.MD
  12. *
  13. * Author: Numberwolf - ChangYanlong
  14. * QQ: 531365872
  15. * QQ Group:925466059
  16. * Wechat: numberwolf11
  17. * Discord: numberwolf#8694
  18. * E-Mail: porschegt23@foxmail.com
  19. * Github: https://github.com/numberwolf/h265web.js
  20. *
  21. * 作者: 小老虎(Numberwolf)(常炎隆)
  22. * QQ: 531365872
  23. * QQ群: 531365872
  24. * 微信: numberwolf11
  25. * Discord: numberwolf#8694
  26. * 邮箱: porschegt23@foxmail.com
  27. * 博客: https://www.jianshu.com/u/9c09c1e00fd1
  28. * Github: https://github.com/numberwolf/h265web.js
  29. *
  30. **********************************************************/
  31. /**
  32. * codecImp Obj
  33. * Video Raw 265 264 Parser
  34. */
  35. var AfterGetNalThenMvLen = 3;
  36. var RawParserModule =
  37. /*#__PURE__*/
  38. function () {
  39. function RawParserModule() {
  40. _classCallCheck(this, RawParserModule);
  41. this.frameList = [];
  42. this.stream = null;
  43. }
  44. /*
  45. *****************************************************
  46. * *
  47. * *
  48. * HEVC Frames *
  49. * *
  50. * *
  51. *****************************************************
  52. */
  53. _createClass(RawParserModule, [{
  54. key: "pushFrameRet",
  55. value: function pushFrameRet(streamPushInput) {
  56. if (!streamPushInput || streamPushInput == undefined || streamPushInput == null) {
  57. return false;
  58. }
  59. if (!this.frameList || this.frameList == undefined || this.frameList == null) {
  60. this.frameList = [];
  61. this.frameList.push(streamPushInput);
  62. } else {
  63. this.frameList.push(streamPushInput);
  64. }
  65. return true;
  66. }
  67. }, {
  68. key: "nextFrame",
  69. value: function nextFrame() {
  70. if (!this.frameList && this.frameList == undefined || this.frameList == null && this.frameList.length < 1) {
  71. return null;
  72. }
  73. return this.frameList.shift();
  74. }
  75. }, {
  76. key: "clearFrameRet",
  77. value: function clearFrameRet() {
  78. this.frameList = null;
  79. }
  80. /*
  81. *****************************************************
  82. * *
  83. * *
  84. * HEVC stream *
  85. * *
  86. * *
  87. *****************************************************
  88. */
  89. }, {
  90. key: "setStreamRet",
  91. value: function setStreamRet(streamBufInput) {
  92. this.stream = streamBufInput;
  93. }
  94. }, {
  95. key: "getStreamRet",
  96. value: function getStreamRet() {
  97. return this.stream;
  98. }
  99. /**
  100. * push stream nalu, for live, not vod
  101. * @param Uint8Array
  102. * @return bool
  103. */
  104. }, {
  105. key: "appendStreamRet",
  106. value: function appendStreamRet(input) {
  107. if (!input || input === undefined || input == null) {
  108. return false;
  109. }
  110. if (!this.stream || this.stream === undefined || this.stream == null) {
  111. this.stream = input;
  112. return true;
  113. }
  114. var lenOld = this.stream.length;
  115. var lenPush = input.length;
  116. var mergeStream = new Uint8Array(lenOld + lenPush);
  117. mergeStream.set(this.stream, 0);
  118. mergeStream.set(input, lenOld);
  119. this.stream = mergeStream; // let retList = this.nextNaluList(9000);
  120. // if (retList !== false && retList.length > 0) {
  121. // this.frameList.push(...retList);
  122. // }
  123. for (var i = 0; i < 9999; i++) {
  124. var nalBuf = this.nextNalu();
  125. if (nalBuf !== false && nalBuf !== null && nalBuf !== undefined) {
  126. this.frameList.push(nalBuf);
  127. } else {
  128. break;
  129. }
  130. }
  131. return true;
  132. }
  133. /**
  134. * sub nalu stream, and get Nalu unit
  135. */
  136. }, {
  137. key: "subBuf",
  138. value: function subBuf(startOpen, endOpen) {
  139. // sub block [m,n]
  140. // nal
  141. var returnBuf = new Uint8Array(this.stream.subarray(startOpen, endOpen + 1)); // streamBuf sub
  142. this.stream = new Uint8Array(this.stream.subarray(endOpen + 1));
  143. return returnBuf;
  144. }
  145. /**
  146. * @param onceGetNalCount: once use get nal count, defult 1
  147. * @return uint8array OR false
  148. */
  149. }, {
  150. key: "nextNalu",
  151. value: function nextNalu() {
  152. var onceGetNalCount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
  153. // check params
  154. if (this.stream == null || this.stream.length <= 4) {
  155. return false;
  156. } // start nal pos
  157. var startTag = -1; // return nalBuf
  158. var returnNalBuf = null;
  159. for (var i = 0; i < this.stream.length; i++) {
  160. if (i + 5 >= this.stream.length) {
  161. return false; // if (startTag == -1) {
  162. // return false;
  163. // } else {
  164. // // 如果结尾不到判断的字节位置 就直接全量输出最后一个nal
  165. // returnNalBuf = this.subBuf(startTag, this.stream.length-1);
  166. // return returnNalBuf;
  167. // }
  168. } // find nal
  169. if ( // 0x00 00 01
  170. this.stream[i] == 0 && this.stream[i + 1] == 0 && this.stream[i + 2] == 1 || // 0x00 00 00 01
  171. this.stream[i] == 0 && this.stream[i + 1] == 0 && this.stream[i + 2] == 0 && this.stream[i + 3] == 1) {
  172. // console.log(
  173. // "enter find nal , now startTag:" + startTag
  174. // + ", now pos:" + i
  175. // );
  176. var nowPos = i;
  177. i += AfterGetNalThenMvLen; // 移出去
  178. // begin pos
  179. if (startTag == -1) {
  180. startTag = nowPos;
  181. } else {
  182. if (onceGetNalCount <= 1) {
  183. // startCode - End
  184. // [startTag,nowPos)
  185. // console.log("[===>] last code hex is :" + this.stream[nowPos-1].toString(16))
  186. returnNalBuf = this.subBuf(startTag, nowPos - 1);
  187. return returnNalBuf;
  188. } else {
  189. onceGetNalCount -= 1;
  190. }
  191. }
  192. }
  193. } // end for
  194. return false;
  195. }
  196. }, {
  197. key: "nextNalu2",
  198. value: function nextNalu2() {
  199. var onceGetNalCount = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
  200. // check params
  201. if (this.stream == null || this.stream.length <= 4) {
  202. return false;
  203. } // start nal pos
  204. var startTag = -1; // return nalBuf
  205. var returnNalBuf = null;
  206. for (var i = 0; i < this.stream.length; i++) {
  207. if (i + 5 >= this.stream.length) {
  208. if (startTag == -1) {
  209. return false;
  210. } else {
  211. // 如果结尾不到判断的字节位置 就直接全量输出最后一个nal
  212. returnNalBuf = this.subBuf(startTag, this.stream.length - 1);
  213. return returnNalBuf;
  214. }
  215. } // find nal
  216. var is3BitHeader = this.stream.slice(i, i + 3).join(' ') == '0 0 1';
  217. var is4BitHeader = this.stream.slice(i, i + 4).join(' ') == '0 0 0 1';
  218. if (is3BitHeader || is4BitHeader) {
  219. var nowPos = i;
  220. i += AfterGetNalThenMvLen; // 移出去
  221. // begin pos
  222. if (startTag == -1) {
  223. startTag = nowPos;
  224. } else {
  225. if (onceGetNalCount <= 1) {
  226. // startCode - End
  227. // [startTag,nowPos)
  228. // console.log("[===>] last code hex is :" + this.stream[nowPos-1].toString(16))
  229. returnNalBuf = this.subBuf(startTag, nowPos - 1);
  230. return returnNalBuf;
  231. } else {
  232. onceGetNalCount -= 1;
  233. }
  234. }
  235. }
  236. } // end for
  237. return false;
  238. }
  239. /**
  240. * @brief sub nalu stream, and get Nalu unit
  241. * to parse:
  242. * typedef struct {
  243. * uint32_t width;
  244. * uint32_t height;
  245. * uint8_t *dataY;
  246. * uint8_t *dataChromaB;
  247. * uint8_t *dataChromaR;
  248. * } ImageData;
  249. * @params struct_ptr: Module.cwrap('getFrame', 'number', [])
  250. * @return Dict
  251. */
  252. }, {
  253. key: "parseYUVFrameStruct",
  254. value: function parseYUVFrameStruct() {
  255. var struct_ptr = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  256. // sub block [m,n]
  257. if (struct_ptr == null || !struct_ptr || struct_ptr == undefined) {
  258. return null;
  259. }
  260. var width = Module.HEAPU32[struct_ptr / 4];
  261. var height = Module.HEAPU32[struct_ptr / 4 + 1]; // let imgBufferPtr = Module.HEAPU32[ptr / 4 + 2];
  262. // let imageBuffer = Module.HEAPU8.subarray(imgBufferPtr, imgBufferPtr + width * height * 3);
  263. // console.log("width:",width," height:",height);
  264. var sizeWH = width * height; // let imgBufferYPtr = Module.HEAPU32[ptr / 4 + 2];
  265. // let imageBufferY = Module.HEAPU8.subarray(imgBufferYPtr, imgBufferYPtr + sizeWH);
  266. // let imgBufferBPtr = Module.HEAPU32[ptr/4+ 2 + sizeWH/4 + 1];
  267. // let imageBufferB = Module.HEAPU8.subarray(
  268. // imgBufferBPtr,
  269. // imgBufferBPtr + sizeWH/4
  270. // );
  271. // console.log(imageBufferB);
  272. // let imgBufferRPtr = Module.HEAPU32[imgBufferBPtr + sizeWH/16 + 1];
  273. // let imageBufferR = Module.HEAPU8.subarray(
  274. // imgBufferRPtr,
  275. // imgBufferRPtr + sizeWH/4
  276. // );
  277. var imgBufferPtr = Module.HEAPU32[struct_ptr / 4 + 1 + 1];
  278. var imageBufferY = Module.HEAPU8.subarray(imgBufferPtr, imgBufferPtr + sizeWH);
  279. var imageBufferB = Module.HEAPU8.subarray(imgBufferPtr + sizeWH + 8, imgBufferPtr + sizeWH + 8 + sizeWH / 4);
  280. var imageBufferR = Module.HEAPU8.subarray(imgBufferPtr + sizeWH + 8 + sizeWH / 4 + 8, imgBufferPtr + sizeWH + 8 + sizeWH / 2 + 8);
  281. return {
  282. width: width,
  283. height: height,
  284. sizeWH: sizeWH,
  285. imageBufferY: imageBufferY,
  286. imageBufferB: imageBufferB,
  287. imageBufferR: imageBufferR
  288. };
  289. }
  290. }]);
  291. return RawParserModule;
  292. }();
  293. exports["default"] = RawParserModule;
  294. },{}],2:[function(require,module,exports){
  295. "use strict";
  296. var _rawParser = _interopRequireDefault(require("./dist/raw-parser.js"));
  297. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  298. // console.log("import parse worker!!!", RawParserModule);
  299. var g_RawParser = new _rawParser["default"]();
  300. onmessage = function onmessage(event) {
  301. // console.log("parse - worker.onmessage", event);
  302. var body = event.data;
  303. var cmd = null;
  304. if (body.cmd === undefined || body.cmd === null) {
  305. cmd = '';
  306. } else {
  307. cmd = body.cmd;
  308. } // console.log("parse - worker recv cmd:", cmd);
  309. switch (cmd) {
  310. case 'append-chunk':
  311. // console.log("parse - worker append-chunk");
  312. var chunk = body.data;
  313. g_RawParser.appendStreamRet(chunk);
  314. break;
  315. case 'get-nalu':
  316. // let nalBuf = g_RawParser.nextNalu();
  317. var nalBuf = g_RawParser.nextFrame(); // console.log("parse - worker get-nalu", nalBuf);
  318. // if (nalBuf != false) {
  319. postMessage({
  320. cmd: "return-nalu",
  321. data: nalBuf,
  322. msg: "return-nalu"
  323. }); // }
  324. break;
  325. case 'stop':
  326. // console.log("parse - worker stop");
  327. postMessage('parse - WORKER STOPPED: ' + body);
  328. close(); // Terminates the worker.
  329. break;
  330. default:
  331. // console.log("parse - worker default");
  332. // console.log("parse - worker.body -> default: ", body);
  333. // worker.postMessage('Unknown command: ' + data.msg);
  334. break;
  335. }
  336. ;
  337. };
  338. },{"./dist/raw-parser.js":1}]},{},[2]);