2
0

arithmetic_decoder.js 6.2 KB

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