/src/xpdf-4.05/xpdf/JArithmeticDecoder.cc
Line | Count | Source (jump to first uncovered line) |
1 | | //======================================================================== |
2 | | // |
3 | | // JArithmeticDecoder.cc |
4 | | // |
5 | | // Copyright 2002-2004 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | #include <aconf.h> |
10 | | |
11 | | #include "gmempp.h" |
12 | | #include "Object.h" |
13 | | #include "Stream.h" |
14 | | #include "JArithmeticDecoder.h" |
15 | | |
16 | | //------------------------------------------------------------------------ |
17 | | // JArithmeticDecoderStates |
18 | | //------------------------------------------------------------------------ |
19 | | |
20 | 2.97M | JArithmeticDecoderStats::JArithmeticDecoderStats(int contextSizeA) { |
21 | 2.97M | contextSize = contextSizeA; |
22 | 2.97M | cxTab = (Guchar *)gmallocn(contextSize, sizeof(Guchar)); |
23 | 2.97M | reset(); |
24 | 2.97M | } |
25 | | |
26 | 2.96M | JArithmeticDecoderStats::~JArithmeticDecoderStats() { |
27 | 2.96M | gfree(cxTab); |
28 | 2.96M | } |
29 | | |
30 | 1.10k | JArithmeticDecoderStats *JArithmeticDecoderStats::copy() { |
31 | 1.10k | JArithmeticDecoderStats *stats; |
32 | | |
33 | 1.10k | stats = new JArithmeticDecoderStats(contextSize); |
34 | 1.10k | memcpy(stats->cxTab, cxTab, contextSize); |
35 | 1.10k | return stats; |
36 | 1.10k | } |
37 | | |
38 | 3.55M | void JArithmeticDecoderStats::reset() { |
39 | 3.55M | memset(cxTab, 0, contextSize); |
40 | 3.55M | } |
41 | | |
42 | 0 | void JArithmeticDecoderStats::copyFrom(JArithmeticDecoderStats *stats) { |
43 | 0 | memcpy(cxTab, stats->cxTab, contextSize); |
44 | 0 | } |
45 | | |
46 | 2.35M | void JArithmeticDecoderStats::setEntry(Guint cx, int i, int mps) { |
47 | 2.35M | cxTab[cx] = (Guchar)((i << 1) + mps); |
48 | 2.35M | } |
49 | | |
50 | | //------------------------------------------------------------------------ |
51 | | // JArithmeticDecoder |
52 | | //------------------------------------------------------------------------ |
53 | | |
54 | | Guint JArithmeticDecoder::qeTab[47] = { |
55 | | 0x56010000, 0x34010000, 0x18010000, 0x0AC10000, |
56 | | 0x05210000, 0x02210000, 0x56010000, 0x54010000, |
57 | | 0x48010000, 0x38010000, 0x30010000, 0x24010000, |
58 | | 0x1C010000, 0x16010000, 0x56010000, 0x54010000, |
59 | | 0x51010000, 0x48010000, 0x38010000, 0x34010000, |
60 | | 0x30010000, 0x28010000, 0x24010000, 0x22010000, |
61 | | 0x1C010000, 0x18010000, 0x16010000, 0x14010000, |
62 | | 0x12010000, 0x11010000, 0x0AC10000, 0x09C10000, |
63 | | 0x08A10000, 0x05210000, 0x04410000, 0x02A10000, |
64 | | 0x02210000, 0x01410000, 0x01110000, 0x00850000, |
65 | | 0x00490000, 0x00250000, 0x00150000, 0x00090000, |
66 | | 0x00050000, 0x00010000, 0x56010000 |
67 | | }; |
68 | | |
69 | | int JArithmeticDecoder::nmpsTab[47] = { |
70 | | 1, 2, 3, 4, 5, 38, 7, 8, 9, 10, 11, 12, 13, 29, 15, 16, |
71 | | 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, |
72 | | 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 45, 46 |
73 | | }; |
74 | | |
75 | | int JArithmeticDecoder::nlpsTab[47] = { |
76 | | 1, 6, 9, 12, 29, 33, 6, 14, 14, 14, 17, 18, 20, 21, 14, 14, |
77 | | 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, |
78 | | 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 46 |
79 | | }; |
80 | | |
81 | | int JArithmeticDecoder::switchTab[47] = { |
82 | | 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, |
83 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
84 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
85 | | }; |
86 | | |
87 | 446k | JArithmeticDecoder::JArithmeticDecoder() { |
88 | 446k | str = NULL; |
89 | 446k | dataLen = 0; |
90 | 446k | limitStream = gFalse; |
91 | 446k | nBytesRead = 0; |
92 | 446k | readBuf = -1; |
93 | 446k | } |
94 | | |
95 | 75.6M | inline Guint JArithmeticDecoder::readByte() { |
96 | 75.6M | Guint x; |
97 | | |
98 | 75.6M | if (limitStream) { |
99 | 74.0M | if (readBuf >= 0) { |
100 | 279k | x = (Guint)readBuf; |
101 | 279k | readBuf = -1; |
102 | 279k | return x; |
103 | 279k | } |
104 | 73.7M | --dataLen; |
105 | 73.7M | if (dataLen < 0) { |
106 | 779k | return 0xff; |
107 | 779k | } |
108 | 73.7M | } |
109 | 74.5M | ++nBytesRead; |
110 | 74.5M | return (Guint)str->getChar() & 0xff; |
111 | 75.6M | } |
112 | | |
113 | 445k | JArithmeticDecoder::~JArithmeticDecoder() { |
114 | 445k | cleanup(); |
115 | 445k | } |
116 | | |
117 | 700k | void JArithmeticDecoder::start() { |
118 | 700k | buf0 = readByte(); |
119 | 700k | buf1 = readByte(); |
120 | | |
121 | | // INITDEC |
122 | 700k | c = (buf0 ^ 0xff) << 16; |
123 | 700k | byteIn(); |
124 | 700k | c <<= 7; |
125 | 700k | ct -= 7; |
126 | 700k | a = 0x80000000; |
127 | 700k | } |
128 | | |
129 | 15.5k | void JArithmeticDecoder::restart(int dataLenA) { |
130 | 15.5k | Guint cAdd; |
131 | 15.5k | GBool prevFF; |
132 | 15.5k | int k, nBits; |
133 | | |
134 | 15.5k | if (dataLen >= 0) { |
135 | 11.9k | dataLen = dataLenA; |
136 | 11.9k | } else if (dataLen == -1) { |
137 | 935 | dataLen = dataLenA; |
138 | 935 | buf1 = readByte(); |
139 | 2.59k | } else { |
140 | 2.59k | k = (-dataLen - 1) * 8 - ct; |
141 | 2.59k | dataLen = dataLenA; |
142 | 2.59k | cAdd = 0; |
143 | 2.59k | prevFF = gFalse; |
144 | 56.4k | while (k > 0) { |
145 | 53.8k | buf0 = readByte(); |
146 | 53.8k | if (prevFF) { |
147 | 42.0k | cAdd += 0xfe00 - (buf0 << 9); |
148 | 42.0k | nBits = 7; |
149 | 42.0k | } else { |
150 | 11.7k | cAdd += 0xff00 - (buf0 << 8); |
151 | 11.7k | nBits = 8; |
152 | 11.7k | } |
153 | 53.8k | prevFF = buf0 == 0xff; |
154 | 53.8k | if (k > nBits) { |
155 | 51.2k | cAdd <<= nBits; |
156 | 51.2k | k -= nBits; |
157 | 51.2k | } else { |
158 | 2.59k | cAdd <<= k; |
159 | 2.59k | ct = nBits - k; |
160 | 2.59k | k = 0; |
161 | 2.59k | } |
162 | 53.8k | } |
163 | 2.59k | c += cAdd; |
164 | 2.59k | buf1 = readByte(); |
165 | 2.59k | } |
166 | 15.5k | } |
167 | | |
168 | 1.36M | void JArithmeticDecoder::cleanup() { |
169 | 1.36M | if (limitStream) { |
170 | | // This saves one extra byte of data from the end of packet i, to |
171 | | // be used in packet i+1. It's not clear from the JPEG 2000 spec |
172 | | // exactly how this should work, but this kludge does seem to fix |
173 | | // decode of some problematic JPEG 2000 streams. It may actually |
174 | | // be necessary to buffer an arbitrary number of bytes (not just |
175 | | // one byte), but I haven't run into that case yet. |
176 | 72.0M | while (dataLen > 0) { |
177 | 70.8M | readBuf = -1; |
178 | 70.8M | readBuf = readByte(); |
179 | 70.8M | } |
180 | 1.19M | } |
181 | 1.36M | } |
182 | | |
183 | | int JArithmeticDecoder::decodeBit(Guint context, |
184 | 118M | JArithmeticDecoderStats *stats) { |
185 | 118M | int bit; |
186 | 118M | Guint qe; |
187 | 118M | int iCX, mpsCX; |
188 | | |
189 | 118M | iCX = stats->cxTab[context] >> 1; |
190 | 118M | mpsCX = stats->cxTab[context] & 1; |
191 | 118M | qe = qeTab[iCX]; |
192 | 118M | a -= qe; |
193 | 118M | if (c < a) { |
194 | 96.0M | if (a & 0x80000000) { |
195 | 71.2M | bit = mpsCX; |
196 | 71.2M | } else { |
197 | | // MPS_EXCHANGE |
198 | 24.7M | if (a < qe) { |
199 | 4.19M | bit = 1 - mpsCX; |
200 | 4.19M | if (switchTab[iCX]) { |
201 | 2.67M | stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX)); |
202 | 2.67M | } else { |
203 | 1.52M | stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX); |
204 | 1.52M | } |
205 | 20.5M | } else { |
206 | 20.5M | bit = mpsCX; |
207 | 20.5M | stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX); |
208 | 20.5M | } |
209 | | // RENORMD |
210 | 26.5M | do { |
211 | 26.5M | if (ct == 0) { |
212 | 3.37M | byteIn(); |
213 | 3.37M | } |
214 | 26.5M | a <<= 1; |
215 | 26.5M | c <<= 1; |
216 | 26.5M | --ct; |
217 | 26.5M | } while (!(a & 0x80000000)); |
218 | 24.7M | } |
219 | 96.0M | } else { |
220 | 22.5M | c -= a; |
221 | | // LPS_EXCHANGE |
222 | 22.5M | if (a < qe) { |
223 | 4.60M | bit = mpsCX; |
224 | 4.60M | stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX); |
225 | 17.9M | } else { |
226 | 17.9M | bit = 1 - mpsCX; |
227 | 17.9M | if (switchTab[iCX]) { |
228 | 6.42M | stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX)); |
229 | 11.4M | } else { |
230 | 11.4M | stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX); |
231 | 11.4M | } |
232 | 17.9M | } |
233 | 22.5M | a = qe; |
234 | | // RENORMD |
235 | 33.2M | do { |
236 | 33.2M | if (ct == 0) { |
237 | 4.17M | byteIn(); |
238 | 4.17M | } |
239 | 33.2M | a <<= 1; |
240 | 33.2M | c <<= 1; |
241 | 33.2M | --ct; |
242 | 33.2M | } while (!(a & 0x80000000)); |
243 | 22.5M | } |
244 | 118M | return bit; |
245 | 118M | } |
246 | | |
247 | | int JArithmeticDecoder::decodeByte(Guint context, |
248 | 0 | JArithmeticDecoderStats *stats) { |
249 | 0 | int byte; |
250 | 0 | int i; |
251 | |
|
252 | 0 | byte = 0; |
253 | 0 | for (i = 0; i < 8; ++i) { |
254 | 0 | byte = (byte << 1) | decodeBit(context, stats); |
255 | 0 | } |
256 | 0 | return byte; |
257 | 0 | } |
258 | | |
259 | 940k | GBool JArithmeticDecoder::decodeInt(int *x, JArithmeticDecoderStats *stats) { |
260 | 940k | int s; |
261 | 940k | Guint v; |
262 | 940k | int i; |
263 | | |
264 | 940k | prev = 1; |
265 | 940k | s = decodeIntBit(stats); |
266 | 940k | if (decodeIntBit(stats)) { |
267 | 210k | if (decodeIntBit(stats)) { |
268 | 91.8k | if (decodeIntBit(stats)) { |
269 | 20.1k | if (decodeIntBit(stats)) { |
270 | 9.39k | if (decodeIntBit(stats)) { |
271 | 3.48k | v = 0; |
272 | 115k | for (i = 0; i < 32; ++i) { |
273 | 111k | v = (v << 1) | decodeIntBit(stats); |
274 | 111k | } |
275 | 3.48k | v += 4436; |
276 | 5.90k | } else { |
277 | 5.90k | v = 0; |
278 | 76.8k | for (i = 0; i < 12; ++i) { |
279 | 70.8k | v = (v << 1) | decodeIntBit(stats); |
280 | 70.8k | } |
281 | 5.90k | v += 340; |
282 | 5.90k | } |
283 | 10.7k | } else { |
284 | 10.7k | v = 0; |
285 | 96.8k | for (i = 0; i < 8; ++i) { |
286 | 86.0k | v = (v << 1) | decodeIntBit(stats); |
287 | 86.0k | } |
288 | 10.7k | v += 84; |
289 | 10.7k | } |
290 | 71.6k | } else { |
291 | 71.6k | v = 0; |
292 | 501k | for (i = 0; i < 6; ++i) { |
293 | 430k | v = (v << 1) | decodeIntBit(stats); |
294 | 430k | } |
295 | 71.6k | v += 20; |
296 | 71.6k | } |
297 | 118k | } else { |
298 | 118k | v = decodeIntBit(stats); |
299 | 118k | v = (v << 1) | decodeIntBit(stats); |
300 | 118k | v = (v << 1) | decodeIntBit(stats); |
301 | 118k | v = (v << 1) | decodeIntBit(stats); |
302 | 118k | v += 4; |
303 | 118k | } |
304 | 730k | } else { |
305 | 730k | v = decodeIntBit(stats); |
306 | 730k | v = (v << 1) | decodeIntBit(stats); |
307 | 730k | } |
308 | | |
309 | 940k | if (s) { |
310 | 440k | if (v == 0) { |
311 | 116k | return gFalse; |
312 | 116k | } |
313 | 324k | *x = -(int)v; |
314 | 500k | } else { |
315 | 500k | *x = (int)v; |
316 | 500k | } |
317 | 824k | return gTrue; |
318 | 940k | } |
319 | | |
320 | 4.84M | int JArithmeticDecoder::decodeIntBit(JArithmeticDecoderStats *stats) { |
321 | 4.84M | int bit; |
322 | | |
323 | 4.84M | bit = decodeBit(prev, stats); |
324 | 4.84M | if (prev < 0x100) { |
325 | 4.48M | prev = (prev << 1) | bit; |
326 | 4.48M | } else { |
327 | 360k | prev = (((prev << 1) | bit) & 0x1ff) | 0x100; |
328 | 360k | } |
329 | 4.84M | return bit; |
330 | 4.84M | } |
331 | | |
332 | | Guint JArithmeticDecoder::decodeIAID(Guint codeLen, |
333 | 448k | JArithmeticDecoderStats *stats) { |
334 | 448k | Guint i; |
335 | 448k | int bit; |
336 | | |
337 | 448k | prev = 1; |
338 | 486k | for (i = 0; i < codeLen; ++i) { |
339 | 37.8k | bit = decodeBit(prev, stats); |
340 | 37.8k | prev = (prev << 1) | bit; |
341 | 37.8k | } |
342 | 448k | return prev - (1 << codeLen); |
343 | 448k | } |
344 | | |
345 | 8.24M | void JArithmeticDecoder::byteIn() { |
346 | 8.24M | if (buf0 == 0xff) { |
347 | 5.55M | if (buf1 > 0x8f) { |
348 | 5.54M | if (limitStream) { |
349 | 606k | buf0 = buf1; |
350 | 606k | buf1 = readByte(); |
351 | 606k | c = c + 0xff00 - (buf0 << 8); |
352 | 606k | } |
353 | 5.54M | ct = 8; |
354 | 5.54M | } else { |
355 | 11.4k | buf0 = buf1; |
356 | 11.4k | buf1 = readByte(); |
357 | 11.4k | c = c + 0xfe00 - (buf0 << 9); |
358 | 11.4k | ct = 7; |
359 | 11.4k | } |
360 | 5.55M | } else { |
361 | 2.69M | buf0 = buf1; |
362 | 2.69M | buf1 = readByte(); |
363 | 2.69M | c = c + 0xff00 - (buf0 << 8); |
364 | 2.69M | ct = 8; |
365 | 2.69M | } |
366 | 8.24M | } |