arithmetic_decoder.js 5.8 KB

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