/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.35M | JArithmeticDecoderStats::JArithmeticDecoderStats(int contextSizeA) { |
21 | 2.35M | contextSize = contextSizeA; |
22 | 2.35M | cxTab = (Guchar *)gmallocn(contextSize, sizeof(Guchar)); |
23 | 2.35M | reset(); |
24 | 2.35M | } |
25 | | |
26 | 2.34M | JArithmeticDecoderStats::~JArithmeticDecoderStats() { |
27 | 2.34M | gfree(cxTab); |
28 | 2.34M | } |
29 | | |
30 | 215 | JArithmeticDecoderStats *JArithmeticDecoderStats::copy() { |
31 | 215 | JArithmeticDecoderStats *stats; |
32 | | |
33 | 215 | stats = new JArithmeticDecoderStats(contextSize); |
34 | 215 | memcpy(stats->cxTab, cxTab, contextSize); |
35 | 215 | return stats; |
36 | 215 | } |
37 | | |
38 | 3.44M | void JArithmeticDecoderStats::reset() { |
39 | 3.44M | memset(cxTab, 0, contextSize); |
40 | 3.44M | } |
41 | | |
42 | 0 | void JArithmeticDecoderStats::copyFrom(JArithmeticDecoderStats *stats) { |
43 | 0 | memcpy(cxTab, stats->cxTab, contextSize); |
44 | 0 | } |
45 | | |
46 | 3.93M | void JArithmeticDecoderStats::setEntry(Guint cx, int i, int mps) { |
47 | 3.93M | cxTab[cx] = (Guchar)((i << 1) + mps); |
48 | 3.93M | } |
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 | 403k | JArithmeticDecoder::JArithmeticDecoder() { |
88 | 403k | str = NULL; |
89 | 403k | dataLen = 0; |
90 | 403k | limitStream = gFalse; |
91 | 403k | nBytesRead = 0; |
92 | 403k | readBuf = -1; |
93 | 403k | } |
94 | | |
95 | 208M | inline Guint JArithmeticDecoder::readByte() { |
96 | 208M | Guint x; |
97 | | |
98 | 208M | if (limitStream) { |
99 | 207M | if (readBuf >= 0) { |
100 | 269k | x = (Guint)readBuf; |
101 | 269k | readBuf = -1; |
102 | 269k | return x; |
103 | 269k | } |
104 | 207M | --dataLen; |
105 | 207M | if (dataLen < 0) { |
106 | 1.04M | return 0xff; |
107 | 1.04M | } |
108 | 207M | } |
109 | 207M | ++nBytesRead; |
110 | 207M | return (Guint)str->getChar() & 0xff; |
111 | 208M | } |
112 | | |
113 | 403k | JArithmeticDecoder::~JArithmeticDecoder() { |
114 | 403k | cleanup(); |
115 | 403k | } |
116 | | |
117 | 697k | void JArithmeticDecoder::start() { |
118 | 697k | buf0 = readByte(); |
119 | 697k | buf1 = readByte(); |
120 | | |
121 | | // INITDEC |
122 | 697k | c = (buf0 ^ 0xff) << 16; |
123 | 697k | byteIn(); |
124 | 697k | c <<= 7; |
125 | 697k | ct -= 7; |
126 | 697k | a = 0x80000000; |
127 | 697k | } |
128 | | |
129 | 63.7k | void JArithmeticDecoder::restart(int dataLenA) { |
130 | 63.7k | Guint cAdd; |
131 | 63.7k | GBool prevFF; |
132 | 63.7k | int k, nBits; |
133 | | |
134 | 63.7k | if (dataLen >= 0) { |
135 | 60.5k | dataLen = dataLenA; |
136 | 60.5k | } else if (dataLen == -1) { |
137 | 248 | dataLen = dataLenA; |
138 | 248 | buf1 = readByte(); |
139 | 3.01k | } else { |
140 | 3.01k | k = (-dataLen - 1) * 8 - ct; |
141 | 3.01k | dataLen = dataLenA; |
142 | 3.01k | cAdd = 0; |
143 | 3.01k | prevFF = gFalse; |
144 | 75.1k | while (k > 0) { |
145 | 72.1k | buf0 = readByte(); |
146 | 72.1k | if (prevFF) { |
147 | 56.2k | cAdd += 0xfe00 - (buf0 << 9); |
148 | 56.2k | nBits = 7; |
149 | 56.2k | } else { |
150 | 15.8k | cAdd += 0xff00 - (buf0 << 8); |
151 | 15.8k | nBits = 8; |
152 | 15.8k | } |
153 | 72.1k | prevFF = buf0 == 0xff; |
154 | 72.1k | if (k > nBits) { |
155 | 69.1k | cAdd <<= nBits; |
156 | 69.1k | k -= nBits; |
157 | 69.1k | } else { |
158 | 3.01k | cAdd <<= k; |
159 | 3.01k | ct = nBits - k; |
160 | 3.01k | k = 0; |
161 | 3.01k | } |
162 | 72.1k | } |
163 | 3.01k | c += cAdd; |
164 | 3.01k | buf1 = readByte(); |
165 | 3.01k | } |
166 | 63.7k | } |
167 | | |
168 | 1.37M | void JArithmeticDecoder::cleanup() { |
169 | 1.37M | 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 | 205M | while (dataLen > 0) { |
177 | 203M | readBuf = -1; |
178 | 203M | readBuf = readByte(); |
179 | 203M | } |
180 | 1.24M | } |
181 | 1.37M | } |
182 | | |
183 | | int JArithmeticDecoder::decodeBit(Guint context, |
184 | 160M | JArithmeticDecoderStats *stats) { |
185 | 160M | int bit; |
186 | 160M | Guint qe; |
187 | 160M | int iCX, mpsCX; |
188 | | |
189 | 160M | iCX = stats->cxTab[context] >> 1; |
190 | 160M | mpsCX = stats->cxTab[context] & 1; |
191 | 160M | qe = qeTab[iCX]; |
192 | 160M | a -= qe; |
193 | 160M | if (c < a) { |
194 | 143M | if (a & 0x80000000) { |
195 | 125M | bit = mpsCX; |
196 | 125M | } else { |
197 | | // MPS_EXCHANGE |
198 | 18.1M | if (a < qe) { |
199 | 3.00M | bit = 1 - mpsCX; |
200 | 3.00M | if (switchTab[iCX]) { |
201 | 1.85M | stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX)); |
202 | 1.85M | } else { |
203 | 1.14M | stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX); |
204 | 1.14M | } |
205 | 15.1M | } else { |
206 | 15.1M | bit = mpsCX; |
207 | 15.1M | stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX); |
208 | 15.1M | } |
209 | | // RENORMD |
210 | 19.4M | do { |
211 | 19.4M | if (ct == 0) { |
212 | 2.48M | byteIn(); |
213 | 2.48M | } |
214 | 19.4M | a <<= 1; |
215 | 19.4M | c <<= 1; |
216 | 19.4M | --ct; |
217 | 19.4M | } while (!(a & 0x80000000)); |
218 | 18.1M | } |
219 | 143M | } else { |
220 | 16.9M | c -= a; |
221 | | // LPS_EXCHANGE |
222 | 16.9M | if (a < qe) { |
223 | 3.54M | bit = mpsCX; |
224 | 3.54M | stats->cxTab[context] = (Guchar)((nmpsTab[iCX] << 1) | mpsCX); |
225 | 13.4M | } else { |
226 | 13.4M | bit = 1 - mpsCX; |
227 | 13.4M | if (switchTab[iCX]) { |
228 | 4.46M | stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | (1 - mpsCX)); |
229 | 8.94M | } else { |
230 | 8.94M | stats->cxTab[context] = (Guchar)((nlpsTab[iCX] << 1) | mpsCX); |
231 | 8.94M | } |
232 | 13.4M | } |
233 | 16.9M | a = qe; |
234 | | // RENORMD |
235 | 25.3M | do { |
236 | 25.3M | if (ct == 0) { |
237 | 3.20M | byteIn(); |
238 | 3.20M | } |
239 | 25.3M | a <<= 1; |
240 | 25.3M | c <<= 1; |
241 | 25.3M | --ct; |
242 | 25.3M | } while (!(a & 0x80000000)); |
243 | 16.9M | } |
244 | 160M | return bit; |
245 | 160M | } |
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 | 1.88M | GBool JArithmeticDecoder::decodeInt(int *x, JArithmeticDecoderStats *stats) { |
260 | 1.88M | int s; |
261 | 1.88M | Guint v; |
262 | 1.88M | int i; |
263 | | |
264 | 1.88M | prev = 1; |
265 | 1.88M | s = decodeIntBit(stats); |
266 | 1.88M | if (decodeIntBit(stats)) { |
267 | 485k | if (decodeIntBit(stats)) { |
268 | 187k | if (decodeIntBit(stats)) { |
269 | 46.6k | if (decodeIntBit(stats)) { |
270 | 18.8k | if (decodeIntBit(stats)) { |
271 | 6.33k | v = 0; |
272 | 209k | for (i = 0; i < 32; ++i) { |
273 | 202k | v = (v << 1) | decodeIntBit(stats); |
274 | 202k | } |
275 | 6.33k | v += 4436; |
276 | 12.5k | } else { |
277 | 12.5k | v = 0; |
278 | 162k | for (i = 0; i < 12; ++i) { |
279 | 150k | v = (v << 1) | decodeIntBit(stats); |
280 | 150k | } |
281 | 12.5k | v += 340; |
282 | 12.5k | } |
283 | 27.7k | } else { |
284 | 27.7k | v = 0; |
285 | 250k | for (i = 0; i < 8; ++i) { |
286 | 222k | v = (v << 1) | decodeIntBit(stats); |
287 | 222k | } |
288 | 27.7k | v += 84; |
289 | 27.7k | } |
290 | 141k | } else { |
291 | 141k | v = 0; |
292 | 988k | for (i = 0; i < 6; ++i) { |
293 | 847k | v = (v << 1) | decodeIntBit(stats); |
294 | 847k | } |
295 | 141k | v += 20; |
296 | 141k | } |
297 | 297k | } else { |
298 | 297k | v = decodeIntBit(stats); |
299 | 297k | v = (v << 1) | decodeIntBit(stats); |
300 | 297k | v = (v << 1) | decodeIntBit(stats); |
301 | 297k | v = (v << 1) | decodeIntBit(stats); |
302 | 297k | v += 4; |
303 | 297k | } |
304 | 1.39M | } else { |
305 | 1.39M | v = decodeIntBit(stats); |
306 | 1.39M | v = (v << 1) | decodeIntBit(stats); |
307 | 1.39M | } |
308 | | |
309 | 1.88M | if (s) { |
310 | 947k | if (v == 0) { |
311 | 232k | return gFalse; |
312 | 232k | } |
313 | 714k | *x = -(int)v; |
314 | 936k | } else { |
315 | 936k | *x = (int)v; |
316 | 936k | } |
317 | 1.65M | return gTrue; |
318 | 1.88M | } |
319 | | |
320 | 9.91M | int JArithmeticDecoder::decodeIntBit(JArithmeticDecoderStats *stats) { |
321 | 9.91M | int bit; |
322 | | |
323 | 9.91M | bit = decodeBit(prev, stats); |
324 | 9.91M | if (prev < 0x100) { |
325 | 9.17M | prev = (prev << 1) | bit; |
326 | 9.17M | } else { |
327 | 736k | prev = (((prev << 1) | bit) & 0x1ff) | 0x100; |
328 | 736k | } |
329 | 9.91M | return bit; |
330 | 9.91M | } |
331 | | |
332 | | Guint JArithmeticDecoder::decodeIAID(Guint codeLen, |
333 | 965k | JArithmeticDecoderStats *stats) { |
334 | 965k | Guint i; |
335 | 965k | int bit; |
336 | | |
337 | 965k | prev = 1; |
338 | 1.11M | for (i = 0; i < codeLen; ++i) { |
339 | 151k | bit = decodeBit(prev, stats); |
340 | 151k | prev = (prev << 1) | bit; |
341 | 151k | } |
342 | 965k | return prev - (1 << codeLen); |
343 | 965k | } |
344 | | |
345 | 6.37M | void JArithmeticDecoder::byteIn() { |
346 | 6.37M | if (buf0 == 0xff) { |
347 | 3.87M | if (buf1 > 0x8f) { |
348 | 3.86M | if (limitStream) { |
349 | 769k | buf0 = buf1; |
350 | 769k | buf1 = readByte(); |
351 | 769k | c = c + 0xff00 - (buf0 << 8); |
352 | 769k | } |
353 | 3.86M | ct = 8; |
354 | 3.86M | } else { |
355 | 13.2k | buf0 = buf1; |
356 | 13.2k | buf1 = readByte(); |
357 | 13.2k | c = c + 0xfe00 - (buf0 << 9); |
358 | 13.2k | ct = 7; |
359 | 13.2k | } |
360 | 3.87M | } else { |
361 | 2.50M | buf0 = buf1; |
362 | 2.50M | buf1 = readByte(); |
363 | 2.50M | c = c + 0xff00 - (buf0 << 8); |
364 | 2.50M | ct = 8; |
365 | 2.50M | } |
366 | 6.37M | } |