2
0

arithmetic_decoder.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.ArithmeticDecoder = void 0;
  27. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  28. 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); } }
  29. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  30. var QeTable = [{
  31. qe: 0x5601,
  32. nmps: 1,
  33. nlps: 1,
  34. switchFlag: 1
  35. }, {
  36. qe: 0x3401,
  37. nmps: 2,
  38. nlps: 6,
  39. switchFlag: 0
  40. }, {
  41. qe: 0x1801,
  42. nmps: 3,
  43. nlps: 9,
  44. switchFlag: 0
  45. }, {
  46. qe: 0x0AC1,
  47. nmps: 4,
  48. nlps: 12,
  49. switchFlag: 0
  50. }, {
  51. qe: 0x0521,
  52. nmps: 5,
  53. nlps: 29,
  54. switchFlag: 0
  55. }, {
  56. qe: 0x0221,
  57. nmps: 38,
  58. nlps: 33,
  59. switchFlag: 0
  60. }, {
  61. qe: 0x5601,
  62. nmps: 7,
  63. nlps: 6,
  64. switchFlag: 1
  65. }, {
  66. qe: 0x5401,
  67. nmps: 8,
  68. nlps: 14,
  69. switchFlag: 0
  70. }, {
  71. qe: 0x4801,
  72. nmps: 9,
  73. nlps: 14,
  74. switchFlag: 0
  75. }, {
  76. qe: 0x3801,
  77. nmps: 10,
  78. nlps: 14,
  79. switchFlag: 0
  80. }, {
  81. qe: 0x3001,
  82. nmps: 11,
  83. nlps: 17,
  84. switchFlag: 0
  85. }, {
  86. qe: 0x2401,
  87. nmps: 12,
  88. nlps: 18,
  89. switchFlag: 0
  90. }, {
  91. qe: 0x1C01,
  92. nmps: 13,
  93. nlps: 20,
  94. switchFlag: 0
  95. }, {
  96. qe: 0x1601,
  97. nmps: 29,
  98. nlps: 21,
  99. switchFlag: 0
  100. }, {
  101. qe: 0x5601,
  102. nmps: 15,
  103. nlps: 14,
  104. switchFlag: 1
  105. }, {
  106. qe: 0x5401,
  107. nmps: 16,
  108. nlps: 14,
  109. switchFlag: 0
  110. }, {
  111. qe: 0x5101,
  112. nmps: 17,
  113. nlps: 15,
  114. switchFlag: 0
  115. }, {
  116. qe: 0x4801,
  117. nmps: 18,
  118. nlps: 16,
  119. switchFlag: 0
  120. }, {
  121. qe: 0x3801,
  122. nmps: 19,
  123. nlps: 17,
  124. switchFlag: 0
  125. }, {
  126. qe: 0x3401,
  127. nmps: 20,
  128. nlps: 18,
  129. switchFlag: 0
  130. }, {
  131. qe: 0x3001,
  132. nmps: 21,
  133. nlps: 19,
  134. switchFlag: 0
  135. }, {
  136. qe: 0x2801,
  137. nmps: 22,
  138. nlps: 19,
  139. switchFlag: 0
  140. }, {
  141. qe: 0x2401,
  142. nmps: 23,
  143. nlps: 20,
  144. switchFlag: 0
  145. }, {
  146. qe: 0x2201,
  147. nmps: 24,
  148. nlps: 21,
  149. switchFlag: 0
  150. }, {
  151. qe: 0x1C01,
  152. nmps: 25,
  153. nlps: 22,
  154. switchFlag: 0
  155. }, {
  156. qe: 0x1801,
  157. nmps: 26,
  158. nlps: 23,
  159. switchFlag: 0
  160. }, {
  161. qe: 0x1601,
  162. nmps: 27,
  163. nlps: 24,
  164. switchFlag: 0
  165. }, {
  166. qe: 0x1401,
  167. nmps: 28,
  168. nlps: 25,
  169. switchFlag: 0
  170. }, {
  171. qe: 0x1201,
  172. nmps: 29,
  173. nlps: 26,
  174. switchFlag: 0
  175. }, {
  176. qe: 0x1101,
  177. nmps: 30,
  178. nlps: 27,
  179. switchFlag: 0
  180. }, {
  181. qe: 0x0AC1,
  182. nmps: 31,
  183. nlps: 28,
  184. switchFlag: 0
  185. }, {
  186. qe: 0x09C1,
  187. nmps: 32,
  188. nlps: 29,
  189. switchFlag: 0
  190. }, {
  191. qe: 0x08A1,
  192. nmps: 33,
  193. nlps: 30,
  194. switchFlag: 0
  195. }, {
  196. qe: 0x0521,
  197. nmps: 34,
  198. nlps: 31,
  199. switchFlag: 0
  200. }, {
  201. qe: 0x0441,
  202. nmps: 35,
  203. nlps: 32,
  204. switchFlag: 0
  205. }, {
  206. qe: 0x02A1,
  207. nmps: 36,
  208. nlps: 33,
  209. switchFlag: 0
  210. }, {
  211. qe: 0x0221,
  212. nmps: 37,
  213. nlps: 34,
  214. switchFlag: 0
  215. }, {
  216. qe: 0x0141,
  217. nmps: 38,
  218. nlps: 35,
  219. switchFlag: 0
  220. }, {
  221. qe: 0x0111,
  222. nmps: 39,
  223. nlps: 36,
  224. switchFlag: 0
  225. }, {
  226. qe: 0x0085,
  227. nmps: 40,
  228. nlps: 37,
  229. switchFlag: 0
  230. }, {
  231. qe: 0x0049,
  232. nmps: 41,
  233. nlps: 38,
  234. switchFlag: 0
  235. }, {
  236. qe: 0x0025,
  237. nmps: 42,
  238. nlps: 39,
  239. switchFlag: 0
  240. }, {
  241. qe: 0x0015,
  242. nmps: 43,
  243. nlps: 40,
  244. switchFlag: 0
  245. }, {
  246. qe: 0x0009,
  247. nmps: 44,
  248. nlps: 41,
  249. switchFlag: 0
  250. }, {
  251. qe: 0x0005,
  252. nmps: 45,
  253. nlps: 42,
  254. switchFlag: 0
  255. }, {
  256. qe: 0x0001,
  257. nmps: 45,
  258. nlps: 43,
  259. switchFlag: 0
  260. }, {
  261. qe: 0x5601,
  262. nmps: 46,
  263. nlps: 46,
  264. switchFlag: 0
  265. }];
  266. var ArithmeticDecoder =
  267. /*#__PURE__*/
  268. function () {
  269. function ArithmeticDecoder(data, start, end) {
  270. _classCallCheck(this, ArithmeticDecoder);
  271. this.data = data;
  272. this.bp = start;
  273. this.dataEnd = end;
  274. this.chigh = data[start];
  275. this.clow = 0;
  276. this.byteIn();
  277. this.chigh = this.chigh << 7 & 0xFFFF | this.clow >> 9 & 0x7F;
  278. this.clow = this.clow << 7 & 0xFFFF;
  279. this.ct -= 7;
  280. this.a = 0x8000;
  281. }
  282. _createClass(ArithmeticDecoder, [{
  283. key: "byteIn",
  284. value: function byteIn() {
  285. var data = this.data;
  286. var bp = this.bp;
  287. if (data[bp] === 0xFF) {
  288. if (data[bp + 1] > 0x8F) {
  289. this.clow += 0xFF00;
  290. this.ct = 8;
  291. } else {
  292. bp++;
  293. this.clow += data[bp] << 9;
  294. this.ct = 7;
  295. this.bp = bp;
  296. }
  297. } else {
  298. bp++;
  299. this.clow += bp < this.dataEnd ? data[bp] << 8 : 0xFF00;
  300. this.ct = 8;
  301. this.bp = bp;
  302. }
  303. if (this.clow > 0xFFFF) {
  304. this.chigh += this.clow >> 16;
  305. this.clow &= 0xFFFF;
  306. }
  307. }
  308. }, {
  309. key: "readBit",
  310. value: function readBit(contexts, pos) {
  311. var cx_index = contexts[pos] >> 1,
  312. cx_mps = contexts[pos] & 1;
  313. var qeTableIcx = QeTable[cx_index];
  314. var qeIcx = qeTableIcx.qe;
  315. var d;
  316. var a = this.a - qeIcx;
  317. if (this.chigh < qeIcx) {
  318. if (a < qeIcx) {
  319. a = qeIcx;
  320. d = cx_mps;
  321. cx_index = qeTableIcx.nmps;
  322. } else {
  323. a = qeIcx;
  324. d = 1 ^ cx_mps;
  325. if (qeTableIcx.switchFlag === 1) {
  326. cx_mps = d;
  327. }
  328. cx_index = qeTableIcx.nlps;
  329. }
  330. } else {
  331. this.chigh -= qeIcx;
  332. if ((a & 0x8000) !== 0) {
  333. this.a = a;
  334. return cx_mps;
  335. }
  336. if (a < qeIcx) {
  337. d = 1 ^ cx_mps;
  338. if (qeTableIcx.switchFlag === 1) {
  339. cx_mps = d;
  340. }
  341. cx_index = qeTableIcx.nlps;
  342. } else {
  343. d = cx_mps;
  344. cx_index = qeTableIcx.nmps;
  345. }
  346. }
  347. do {
  348. if (this.ct === 0) {
  349. this.byteIn();
  350. }
  351. a <<= 1;
  352. this.chigh = this.chigh << 1 & 0xFFFF | this.clow >> 15 & 1;
  353. this.clow = this.clow << 1 & 0xFFFF;
  354. this.ct--;
  355. } while ((a & 0x8000) === 0);
  356. this.a = a;
  357. contexts[pos] = cx_index << 1 | cx_mps;
  358. return d;
  359. }
  360. }]);
  361. return ArithmeticDecoder;
  362. }();
  363. exports.ArithmeticDecoder = ArithmeticDecoder;