2
0

arithmetic_decoder.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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. const QeTable = [{
  28. qe: 0x5601,
  29. nmps: 1,
  30. nlps: 1,
  31. switchFlag: 1
  32. }, {
  33. qe: 0x3401,
  34. nmps: 2,
  35. nlps: 6,
  36. switchFlag: 0
  37. }, {
  38. qe: 0x1801,
  39. nmps: 3,
  40. nlps: 9,
  41. switchFlag: 0
  42. }, {
  43. qe: 0x0ac1,
  44. nmps: 4,
  45. nlps: 12,
  46. switchFlag: 0
  47. }, {
  48. qe: 0x0521,
  49. nmps: 5,
  50. nlps: 29,
  51. switchFlag: 0
  52. }, {
  53. qe: 0x0221,
  54. nmps: 38,
  55. nlps: 33,
  56. switchFlag: 0
  57. }, {
  58. qe: 0x5601,
  59. nmps: 7,
  60. nlps: 6,
  61. switchFlag: 1
  62. }, {
  63. qe: 0x5401,
  64. nmps: 8,
  65. nlps: 14,
  66. switchFlag: 0
  67. }, {
  68. qe: 0x4801,
  69. nmps: 9,
  70. nlps: 14,
  71. switchFlag: 0
  72. }, {
  73. qe: 0x3801,
  74. nmps: 10,
  75. nlps: 14,
  76. switchFlag: 0
  77. }, {
  78. qe: 0x3001,
  79. nmps: 11,
  80. nlps: 17,
  81. switchFlag: 0
  82. }, {
  83. qe: 0x2401,
  84. nmps: 12,
  85. nlps: 18,
  86. switchFlag: 0
  87. }, {
  88. qe: 0x1c01,
  89. nmps: 13,
  90. nlps: 20,
  91. switchFlag: 0
  92. }, {
  93. qe: 0x1601,
  94. nmps: 29,
  95. nlps: 21,
  96. switchFlag: 0
  97. }, {
  98. qe: 0x5601,
  99. nmps: 15,
  100. nlps: 14,
  101. switchFlag: 1
  102. }, {
  103. qe: 0x5401,
  104. nmps: 16,
  105. nlps: 14,
  106. switchFlag: 0
  107. }, {
  108. qe: 0x5101,
  109. nmps: 17,
  110. nlps: 15,
  111. switchFlag: 0
  112. }, {
  113. qe: 0x4801,
  114. nmps: 18,
  115. nlps: 16,
  116. switchFlag: 0
  117. }, {
  118. qe: 0x3801,
  119. nmps: 19,
  120. nlps: 17,
  121. switchFlag: 0
  122. }, {
  123. qe: 0x3401,
  124. nmps: 20,
  125. nlps: 18,
  126. switchFlag: 0
  127. }, {
  128. qe: 0x3001,
  129. nmps: 21,
  130. nlps: 19,
  131. switchFlag: 0
  132. }, {
  133. qe: 0x2801,
  134. nmps: 22,
  135. nlps: 19,
  136. switchFlag: 0
  137. }, {
  138. qe: 0x2401,
  139. nmps: 23,
  140. nlps: 20,
  141. switchFlag: 0
  142. }, {
  143. qe: 0x2201,
  144. nmps: 24,
  145. nlps: 21,
  146. switchFlag: 0
  147. }, {
  148. qe: 0x1c01,
  149. nmps: 25,
  150. nlps: 22,
  151. switchFlag: 0
  152. }, {
  153. qe: 0x1801,
  154. nmps: 26,
  155. nlps: 23,
  156. switchFlag: 0
  157. }, {
  158. qe: 0x1601,
  159. nmps: 27,
  160. nlps: 24,
  161. switchFlag: 0
  162. }, {
  163. qe: 0x1401,
  164. nmps: 28,
  165. nlps: 25,
  166. switchFlag: 0
  167. }, {
  168. qe: 0x1201,
  169. nmps: 29,
  170. nlps: 26,
  171. switchFlag: 0
  172. }, {
  173. qe: 0x1101,
  174. nmps: 30,
  175. nlps: 27,
  176. switchFlag: 0
  177. }, {
  178. qe: 0x0ac1,
  179. nmps: 31,
  180. nlps: 28,
  181. switchFlag: 0
  182. }, {
  183. qe: 0x09c1,
  184. nmps: 32,
  185. nlps: 29,
  186. switchFlag: 0
  187. }, {
  188. qe: 0x08a1,
  189. nmps: 33,
  190. nlps: 30,
  191. switchFlag: 0
  192. }, {
  193. qe: 0x0521,
  194. nmps: 34,
  195. nlps: 31,
  196. switchFlag: 0
  197. }, {
  198. qe: 0x0441,
  199. nmps: 35,
  200. nlps: 32,
  201. switchFlag: 0
  202. }, {
  203. qe: 0x02a1,
  204. nmps: 36,
  205. nlps: 33,
  206. switchFlag: 0
  207. }, {
  208. qe: 0x0221,
  209. nmps: 37,
  210. nlps: 34,
  211. switchFlag: 0
  212. }, {
  213. qe: 0x0141,
  214. nmps: 38,
  215. nlps: 35,
  216. switchFlag: 0
  217. }, {
  218. qe: 0x0111,
  219. nmps: 39,
  220. nlps: 36,
  221. switchFlag: 0
  222. }, {
  223. qe: 0x0085,
  224. nmps: 40,
  225. nlps: 37,
  226. switchFlag: 0
  227. }, {
  228. qe: 0x0049,
  229. nmps: 41,
  230. nlps: 38,
  231. switchFlag: 0
  232. }, {
  233. qe: 0x0025,
  234. nmps: 42,
  235. nlps: 39,
  236. switchFlag: 0
  237. }, {
  238. qe: 0x0015,
  239. nmps: 43,
  240. nlps: 40,
  241. switchFlag: 0
  242. }, {
  243. qe: 0x0009,
  244. nmps: 44,
  245. nlps: 41,
  246. switchFlag: 0
  247. }, {
  248. qe: 0x0005,
  249. nmps: 45,
  250. nlps: 42,
  251. switchFlag: 0
  252. }, {
  253. qe: 0x0001,
  254. nmps: 45,
  255. nlps: 43,
  256. switchFlag: 0
  257. }, {
  258. qe: 0x5601,
  259. nmps: 46,
  260. nlps: 46,
  261. switchFlag: 0
  262. }];
  263. class ArithmeticDecoder {
  264. constructor(data, start, end) {
  265. this.data = data;
  266. this.bp = start;
  267. this.dataEnd = end;
  268. this.chigh = data[start];
  269. this.clow = 0;
  270. this.byteIn();
  271. this.chigh = this.chigh << 7 & 0xffff | this.clow >> 9 & 0x7f;
  272. this.clow = this.clow << 7 & 0xffff;
  273. this.ct -= 7;
  274. this.a = 0x8000;
  275. }
  276. byteIn() {
  277. const data = this.data;
  278. let bp = this.bp;
  279. if (data[bp] === 0xff) {
  280. if (data[bp + 1] > 0x8f) {
  281. this.clow += 0xff00;
  282. this.ct = 8;
  283. } else {
  284. bp++;
  285. this.clow += data[bp] << 9;
  286. this.ct = 7;
  287. this.bp = bp;
  288. }
  289. } else {
  290. bp++;
  291. this.clow += bp < this.dataEnd ? data[bp] << 8 : 0xff00;
  292. this.ct = 8;
  293. this.bp = bp;
  294. }
  295. if (this.clow > 0xffff) {
  296. this.chigh += this.clow >> 16;
  297. this.clow &= 0xffff;
  298. }
  299. }
  300. readBit(contexts, pos) {
  301. let cx_index = contexts[pos] >> 1,
  302. cx_mps = contexts[pos] & 1;
  303. const qeTableIcx = QeTable[cx_index];
  304. const qeIcx = qeTableIcx.qe;
  305. let d;
  306. let a = this.a - qeIcx;
  307. if (this.chigh < qeIcx) {
  308. if (a < qeIcx) {
  309. a = qeIcx;
  310. d = cx_mps;
  311. cx_index = qeTableIcx.nmps;
  312. } else {
  313. a = qeIcx;
  314. d = 1 ^ cx_mps;
  315. if (qeTableIcx.switchFlag === 1) {
  316. cx_mps = d;
  317. }
  318. cx_index = qeTableIcx.nlps;
  319. }
  320. } else {
  321. this.chigh -= qeIcx;
  322. if ((a & 0x8000) !== 0) {
  323. this.a = a;
  324. return cx_mps;
  325. }
  326. if (a < qeIcx) {
  327. d = 1 ^ cx_mps;
  328. if (qeTableIcx.switchFlag === 1) {
  329. cx_mps = d;
  330. }
  331. cx_index = qeTableIcx.nlps;
  332. } else {
  333. d = cx_mps;
  334. cx_index = qeTableIcx.nmps;
  335. }
  336. }
  337. do {
  338. if (this.ct === 0) {
  339. this.byteIn();
  340. }
  341. a <<= 1;
  342. this.chigh = this.chigh << 1 & 0xffff | this.clow >> 15 & 1;
  343. this.clow = this.clow << 1 & 0xffff;
  344. this.ct--;
  345. } while ((a & 0x8000) === 0);
  346. this.a = a;
  347. contexts[pos] = cx_index << 1 | cx_mps;
  348. return d;
  349. }
  350. }
  351. exports.ArithmeticDecoder = ArithmeticDecoder;