2
0

arithmetic_decoder.js 6.3 KB

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