UNPKG

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