/src/libjpeg-turbo.main/jdarith.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * jdarith.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Developed 1997-2015 by Guido Vollbeding. |
6 | | * libjpeg-turbo Modifications: |
7 | | * Copyright (C) 2015-2020, 2022, D. R. Commander. |
8 | | * For conditions of distribution and use, see the accompanying README.ijg |
9 | | * file. |
10 | | * |
11 | | * This file contains portable arithmetic entropy encoding routines for JPEG |
12 | | * (implementing Recommendation ITU-T T.81 | ISO/IEC 10918-1). |
13 | | * |
14 | | * Both sequential and progressive modes are supported in this single module. |
15 | | * |
16 | | * Suspension is not currently supported in this module. |
17 | | * |
18 | | * NOTE: All referenced figures are from |
19 | | * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994. |
20 | | */ |
21 | | |
22 | | #define JPEG_INTERNALS |
23 | | #include "jinclude.h" |
24 | | #include "jpeglib.h" |
25 | | |
26 | | |
27 | 25.2M | #define NEG_1 ((unsigned int)-1) |
28 | | |
29 | | |
30 | | /* Expanded entropy decoder object for arithmetic decoding. */ |
31 | | |
32 | | typedef struct { |
33 | | struct jpeg_entropy_decoder pub; /* public fields */ |
34 | | |
35 | | JLONG c; /* C register, base of coding interval + input bit buffer */ |
36 | | JLONG a; /* A register, normalized size of coding interval */ |
37 | | int ct; /* bit shift counter, # of bits left in bit buffer part of C */ |
38 | | /* init: ct = -16 */ |
39 | | /* run: ct = 0..7 */ |
40 | | /* error: ct = -1 */ |
41 | | int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
42 | | int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */ |
43 | | |
44 | | unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
45 | | |
46 | | /* Pointers to statistics areas (these workspaces have image lifespan) */ |
47 | | unsigned char *dc_stats[NUM_ARITH_TBLS]; |
48 | | unsigned char *ac_stats[NUM_ARITH_TBLS]; |
49 | | |
50 | | /* Statistics bin for coding with fixed probability 0.5 */ |
51 | | unsigned char fixed_bin[4]; |
52 | | } arith_entropy_decoder; |
53 | | |
54 | | typedef arith_entropy_decoder *arith_entropy_ptr; |
55 | | |
56 | | /* The following two definitions specify the allocation chunk size |
57 | | * for the statistics area. |
58 | | * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least |
59 | | * 49 statistics bins for DC, and 245 statistics bins for AC coding. |
60 | | * |
61 | | * We use a compact representation with 1 byte per statistics bin, |
62 | | * thus the numbers directly represent byte sizes. |
63 | | * This 1 byte per statistics bin contains the meaning of the MPS |
64 | | * (more probable symbol) in the highest bit (mask 0x80), and the |
65 | | * index into the probability estimation state machine table |
66 | | * in the lower bits (mask 0x7F). |
67 | | */ |
68 | | |
69 | 4.41M | #define DC_STAT_BINS 64 |
70 | 4.08M | #define AC_STAT_BINS 256 |
71 | | |
72 | | |
73 | | LOCAL(int) |
74 | | get_byte(j_decompress_ptr cinfo) |
75 | | /* Read next input byte; we do not support suspension in this module. */ |
76 | 10.7M | { |
77 | 10.7M | struct jpeg_source_mgr *src = cinfo->src; |
78 | | |
79 | 10.7M | if (src->bytes_in_buffer == 0) |
80 | 20.2k | if (!(*src->fill_input_buffer) (cinfo)) |
81 | 0 | ERREXIT(cinfo, JERR_CANT_SUSPEND); |
82 | 10.7M | src->bytes_in_buffer--; |
83 | 10.7M | return *src->next_input_byte++; |
84 | 10.7M | } |
85 | | |
86 | | |
87 | | /* |
88 | | * The core arithmetic decoding routine (common in JPEG and JBIG). |
89 | | * This needs to go as fast as possible. |
90 | | * Machine-dependent optimization facilities |
91 | | * are not utilized in this portable implementation. |
92 | | * However, this code should be fairly efficient and |
93 | | * may be a good base for further optimizations anyway. |
94 | | * |
95 | | * Return value is 0 or 1 (binary decision). |
96 | | * |
97 | | * Note: I've changed the handling of the code base & bit |
98 | | * buffer register C compared to other implementations |
99 | | * based on the standards layout & procedures. |
100 | | * While it also contains both the actual base of the |
101 | | * coding interval (16 bits) and the next-bits buffer, |
102 | | * the cut-point between these two parts is floating |
103 | | * (instead of fixed) with the bit shift counter CT. |
104 | | * Thus, we also need only one (variable instead of |
105 | | * fixed size) shift for the LPS/MPS decision, and |
106 | | * we can do away with any renormalization update |
107 | | * of C (except for new data insertion, of course). |
108 | | * |
109 | | * I've also introduced a new scheme for accessing |
110 | | * the probability estimation state machine table, |
111 | | * derived from Markus Kuhn's JBIG implementation. |
112 | | */ |
113 | | |
114 | | LOCAL(int) |
115 | | arith_decode(j_decompress_ptr cinfo, unsigned char *st) |
116 | 1.41G | { |
117 | 1.41G | register arith_entropy_ptr e = (arith_entropy_ptr)cinfo->entropy; |
118 | 1.41G | register unsigned char nl, nm; |
119 | 1.41G | register JLONG qe, temp; |
120 | 1.41G | register int sv, data; |
121 | | |
122 | | /* Renormalization & data input per section D.2.6 */ |
123 | 2.01G | while (e->a < 0x8000L) { |
124 | 598M | if (--e->ct < 0) { |
125 | | /* Need to fetch next data byte */ |
126 | 82.8M | if (cinfo->unread_marker) |
127 | 72.6M | data = 0; /* stuff zero data */ |
128 | 10.1M | else { |
129 | 10.1M | data = get_byte(cinfo); /* read next input byte */ |
130 | 10.1M | if (data == 0xFF) { /* zero stuff or marker code */ |
131 | 538k | do data = get_byte(cinfo); |
132 | 538k | while (data == 0xFF); /* swallow extra 0xFF bytes */ |
133 | 451k | if (data == 0) |
134 | 27.9k | data = 0xFF; /* discard stuffed zero byte */ |
135 | 423k | else { |
136 | | /* Note: Different from the Huffman decoder, hitting |
137 | | * a marker while processing the compressed data |
138 | | * segment is legal in arithmetic coding. |
139 | | * The convention is to supply zero data |
140 | | * then until decoding is complete. |
141 | | */ |
142 | 423k | cinfo->unread_marker = data; |
143 | 423k | data = 0; |
144 | 423k | } |
145 | 451k | } |
146 | 10.1M | } |
147 | 82.8M | e->c = (e->c << 8) | data; /* insert data into C register */ |
148 | 82.8M | if ((e->ct += 8) < 0) /* update bit shift counter */ |
149 | | /* Need more initial bytes */ |
150 | 8.49M | if (++e->ct == 0) |
151 | | /* Got 2 initial bytes -> re-init A and exit loop */ |
152 | 4.24M | e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */ |
153 | 82.8M | } |
154 | 598M | e->a <<= 1; |
155 | 598M | } |
156 | | |
157 | | /* Fetch values from our compact representation of Table D.2: |
158 | | * Qe values and probability estimation state machine |
159 | | */ |
160 | 1.41G | sv = *st; |
161 | 1.41G | qe = jpeg_aritab[sv & 0x7F]; /* => Qe_Value */ |
162 | 1.41G | nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */ |
163 | 1.41G | nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */ |
164 | | |
165 | | /* Decode & estimation procedures per sections D.2.4 & D.2.5 */ |
166 | 1.41G | temp = e->a - qe; |
167 | 1.41G | e->a = temp; |
168 | 1.41G | temp <<= e->ct; |
169 | 1.41G | if (e->c >= temp) { |
170 | 191M | e->c -= temp; |
171 | | /* Conditional LPS (less probable symbol) exchange */ |
172 | 191M | if (e->a < qe) { |
173 | 54.2M | e->a = qe; |
174 | 54.2M | *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ |
175 | 137M | } else { |
176 | 137M | e->a = qe; |
177 | 137M | *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ |
178 | 137M | sv ^= 0x80; /* Exchange LPS/MPS */ |
179 | 137M | } |
180 | 1.22G | } else if (e->a < 0x8000L) { |
181 | | /* Conditional MPS (more probable symbol) exchange */ |
182 | 252M | if (e->a < qe) { |
183 | 79.4M | *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */ |
184 | 79.4M | sv ^= 0x80; /* Exchange LPS/MPS */ |
185 | 172M | } else { |
186 | 172M | *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */ |
187 | 172M | } |
188 | 252M | } |
189 | | |
190 | 1.41G | return sv >> 7; |
191 | 1.41G | } |
192 | | |
193 | | |
194 | | /* |
195 | | * Check for a restart marker & resynchronize decoder. |
196 | | */ |
197 | | |
198 | | LOCAL(void) |
199 | | process_restart(j_decompress_ptr cinfo) |
200 | 3.52M | { |
201 | 3.52M | arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; |
202 | 3.52M | int ci; |
203 | 3.52M | jpeg_component_info *compptr; |
204 | | |
205 | | /* Advance past the RSTn marker */ |
206 | 3.52M | if (!(*cinfo->marker->read_restart_marker) (cinfo)) |
207 | 0 | ERREXIT(cinfo, JERR_CANT_SUSPEND); |
208 | | |
209 | | /* Re-initialize statistics areas */ |
210 | 7.46M | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
211 | 3.93M | compptr = cinfo->cur_comp_info[ci]; |
212 | 3.93M | if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { |
213 | 3.35M | memset(entropy->dc_stats[compptr->dc_tbl_no], 0, DC_STAT_BINS); |
214 | | /* Reset DC predictions to 0 */ |
215 | 3.35M | entropy->last_dc_val[ci] = 0; |
216 | 3.35M | entropy->dc_context[ci] = 0; |
217 | 3.35M | } |
218 | 3.93M | if (!cinfo->progressive_mode || cinfo->Ss) { |
219 | 3.41M | memset(entropy->ac_stats[compptr->ac_tbl_no], 0, AC_STAT_BINS); |
220 | 3.41M | } |
221 | 3.93M | } |
222 | | |
223 | | /* Reset arithmetic decoding variables */ |
224 | 3.52M | entropy->c = 0; |
225 | 3.52M | entropy->a = 0; |
226 | 3.52M | entropy->ct = -16; /* force reading 2 initial bytes to fill C */ |
227 | | |
228 | | /* Reset restart counter */ |
229 | 3.52M | entropy->restarts_to_go = cinfo->restart_interval; |
230 | 3.52M | } |
231 | | |
232 | | |
233 | | /* |
234 | | * Arithmetic MCU decoding. |
235 | | * Each of these routines decodes and returns one MCU's worth of |
236 | | * arithmetic-compressed coefficients. |
237 | | * The coefficients are reordered from zigzag order into natural array order, |
238 | | * but are not dequantized. |
239 | | * |
240 | | * The i'th block of the MCU is stored into the block pointed to by |
241 | | * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. |
242 | | */ |
243 | | |
244 | | /* |
245 | | * MCU decoding for DC initial scan (either spectral selection, |
246 | | * or first pass of successive approximation). |
247 | | */ |
248 | | |
249 | | METHODDEF(boolean) |
250 | | decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
251 | 26.4M | { |
252 | 26.4M | arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; |
253 | 26.4M | JBLOCKROW block; |
254 | 26.4M | unsigned char *st; |
255 | 26.4M | int blkn, ci, tbl, sign; |
256 | 26.4M | int v, m; |
257 | | |
258 | | /* Process restart marker if needed */ |
259 | 26.4M | if (cinfo->restart_interval) { |
260 | 1.72M | if (entropy->restarts_to_go == 0) |
261 | 111k | process_restart(cinfo); |
262 | 1.72M | entropy->restarts_to_go--; |
263 | 1.72M | } |
264 | | |
265 | 26.4M | if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
266 | | |
267 | | /* Outer loop handles each block in the MCU */ |
268 | | |
269 | 93.7M | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
270 | 68.1M | block = MCU_data[blkn]; |
271 | 68.1M | ci = cinfo->MCU_membership[blkn]; |
272 | 68.1M | tbl = cinfo->cur_comp_info[ci]->dc_tbl_no; |
273 | | |
274 | | /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ |
275 | | |
276 | | /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ |
277 | 68.1M | st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; |
278 | | |
279 | | /* Figure F.19: Decode_DC_DIFF */ |
280 | 68.1M | if (arith_decode(cinfo, st) == 0) |
281 | 29.2M | entropy->dc_context[ci] = 0; |
282 | 38.9M | else { |
283 | | /* Figure F.21: Decoding nonzero value v */ |
284 | | /* Figure F.22: Decoding the sign of v */ |
285 | 38.9M | sign = arith_decode(cinfo, st + 1); |
286 | 38.9M | st += 2; st += sign; |
287 | | /* Figure F.23: Decoding the magnitude category of v */ |
288 | 38.9M | if ((m = arith_decode(cinfo, st)) != 0) { |
289 | 22.7M | st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ |
290 | 47.9M | while (arith_decode(cinfo, st)) { |
291 | 25.1M | if ((m <<= 1) == 0x8000) { |
292 | 23.6k | WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
293 | 23.6k | entropy->ct = -1; /* magnitude overflow */ |
294 | 23.6k | return TRUE; |
295 | 23.6k | } |
296 | 25.1M | st += 1; |
297 | 25.1M | } |
298 | 22.7M | } |
299 | | /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ |
300 | 38.9M | if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1)) |
301 | 86.8k | entropy->dc_context[ci] = 0; /* zero diff category */ |
302 | 38.8M | else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1)) |
303 | 11.8M | entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ |
304 | 26.9M | else |
305 | 26.9M | entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ |
306 | 38.9M | v = m; |
307 | | /* Figure F.24: Decoding the magnitude bit pattern of v */ |
308 | 38.9M | st += 14; |
309 | 63.7M | while (m >>= 1) |
310 | 24.7M | if (arith_decode(cinfo, st)) v |= m; |
311 | 38.9M | v += 1; if (sign) v = -v; |
312 | 38.9M | entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff; |
313 | 38.9M | } |
314 | | |
315 | | /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */ |
316 | 68.1M | (*block)[0] = (JCOEF)LEFT_SHIFT(entropy->last_dc_val[ci], cinfo->Al); |
317 | 68.1M | } |
318 | | |
319 | 25.5M | return TRUE; |
320 | 25.5M | } |
321 | | |
322 | | |
323 | | /* |
324 | | * MCU decoding for AC initial scan (either spectral selection, |
325 | | * or first pass of successive approximation). |
326 | | */ |
327 | | |
328 | | METHODDEF(boolean) |
329 | | decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
330 | 44.9M | { |
331 | 44.9M | arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; |
332 | 44.9M | JBLOCKROW block; |
333 | 44.9M | unsigned char *st; |
334 | 44.9M | int tbl, sign, k; |
335 | 44.9M | int v, m; |
336 | | |
337 | | /* Process restart marker if needed */ |
338 | 44.9M | if (cinfo->restart_interval) { |
339 | 519k | if (entropy->restarts_to_go == 0) |
340 | 97.6k | process_restart(cinfo); |
341 | 519k | entropy->restarts_to_go--; |
342 | 519k | } |
343 | | |
344 | 44.9M | if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
345 | | |
346 | | /* There is always only one block per MCU */ |
347 | 19.2M | block = MCU_data[0]; |
348 | 19.2M | tbl = cinfo->cur_comp_info[0]->ac_tbl_no; |
349 | | |
350 | | /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ |
351 | | |
352 | | /* Figure F.20: Decode_AC_coefficients */ |
353 | 34.6M | for (k = cinfo->Ss; k <= cinfo->Se; k++) { |
354 | 34.6M | st = entropy->ac_stats[tbl] + 3 * (k - 1); |
355 | 34.6M | if (arith_decode(cinfo, st)) break; /* EOB flag */ |
356 | 26.1M | while (arith_decode(cinfo, st + 1) == 0) { |
357 | 10.7M | st += 3; k++; |
358 | 10.7M | if (k > cinfo->Se) { |
359 | 7.72k | WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
360 | 7.72k | entropy->ct = -1; /* spectral overflow */ |
361 | 7.72k | return TRUE; |
362 | 7.72k | } |
363 | 10.7M | } |
364 | | /* Figure F.21: Decoding nonzero value v */ |
365 | | /* Figure F.22: Decoding the sign of v */ |
366 | 15.4M | sign = arith_decode(cinfo, entropy->fixed_bin); |
367 | 15.4M | st += 2; |
368 | | /* Figure F.23: Decoding the magnitude category of v */ |
369 | 15.4M | if ((m = arith_decode(cinfo, st)) != 0) { |
370 | 8.64M | if (arith_decode(cinfo, st)) { |
371 | 7.06M | m <<= 1; |
372 | 7.06M | st = entropy->ac_stats[tbl] + |
373 | 7.06M | (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); |
374 | 16.9M | while (arith_decode(cinfo, st)) { |
375 | 9.91M | if ((m <<= 1) == 0x8000) { |
376 | 3.30k | WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
377 | 3.30k | entropy->ct = -1; /* magnitude overflow */ |
378 | 3.30k | return TRUE; |
379 | 3.30k | } |
380 | 9.90M | st += 1; |
381 | 9.90M | } |
382 | 7.06M | } |
383 | 8.64M | } |
384 | 15.4M | v = m; |
385 | | /* Figure F.24: Decoding the magnitude bit pattern of v */ |
386 | 15.4M | st += 14; |
387 | 32.3M | while (m >>= 1) |
388 | 16.9M | if (arith_decode(cinfo, st)) v |= m; |
389 | 15.4M | v += 1; if (sign) v = -v; |
390 | | /* Scale and output coefficient in natural (dezigzagged) order */ |
391 | 15.4M | (*block)[jpeg_natural_order[k]] = (JCOEF)((unsigned)v << cinfo->Al); |
392 | 15.4M | } |
393 | | |
394 | 19.2M | return TRUE; |
395 | 19.2M | } |
396 | | |
397 | | |
398 | | /* |
399 | | * MCU decoding for DC successive approximation refinement scan. |
400 | | */ |
401 | | |
402 | | METHODDEF(boolean) |
403 | | decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
404 | 2.05M | { |
405 | 2.05M | arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; |
406 | 2.05M | unsigned char *st; |
407 | 2.05M | int p1, blkn; |
408 | | |
409 | | /* Process restart marker if needed */ |
410 | 2.05M | if (cinfo->restart_interval) { |
411 | 691k | if (entropy->restarts_to_go == 0) |
412 | 215k | process_restart(cinfo); |
413 | 691k | entropy->restarts_to_go--; |
414 | 691k | } |
415 | | |
416 | 2.05M | st = entropy->fixed_bin; /* use fixed probability estimation */ |
417 | 2.05M | p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ |
418 | | |
419 | | /* Outer loop handles each block in the MCU */ |
420 | | |
421 | 11.0M | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
422 | | /* Encoded data is simply the next bit of the two's-complement DC value */ |
423 | 9.03M | if (arith_decode(cinfo, st)) |
424 | 4.99M | MCU_data[blkn][0][0] |= p1; |
425 | 9.03M | } |
426 | | |
427 | 2.05M | return TRUE; |
428 | 2.05M | } |
429 | | |
430 | | |
431 | | /* |
432 | | * MCU decoding for AC successive approximation refinement scan. |
433 | | */ |
434 | | |
435 | | METHODDEF(boolean) |
436 | | decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
437 | 34.6M | { |
438 | 34.6M | arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; |
439 | 34.6M | JBLOCKROW block; |
440 | 34.6M | JCOEFPTR thiscoef; |
441 | 34.6M | unsigned char *st; |
442 | 34.6M | int tbl, k, kex; |
443 | 34.6M | int p1, m1; |
444 | | |
445 | | /* Process restart marker if needed */ |
446 | 34.6M | if (cinfo->restart_interval) { |
447 | 1.20M | if (entropy->restarts_to_go == 0) |
448 | 190k | process_restart(cinfo); |
449 | 1.20M | entropy->restarts_to_go--; |
450 | 1.20M | } |
451 | | |
452 | 34.6M | if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
453 | | |
454 | | /* There is always only one block per MCU */ |
455 | 25.2M | block = MCU_data[0]; |
456 | 25.2M | tbl = cinfo->cur_comp_info[0]->ac_tbl_no; |
457 | | |
458 | 25.2M | p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ |
459 | 25.2M | m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */ |
460 | | |
461 | | /* Establish EOBx (previous stage end-of-block) index */ |
462 | 967M | for (kex = cinfo->Se; kex > 0; kex--) |
463 | 952M | if ((*block)[jpeg_natural_order[kex]]) break; |
464 | | |
465 | 136M | for (k = cinfo->Ss; k <= cinfo->Se; k++) { |
466 | 134M | st = entropy->ac_stats[tbl] + 3 * (k - 1); |
467 | 134M | if (k > kex) |
468 | 58.3M | if (arith_decode(cinfo, st)) break; /* EOB flag */ |
469 | 167M | for (;;) { |
470 | 167M | thiscoef = *block + jpeg_natural_order[k]; |
471 | 167M | if (*thiscoef) { /* previously nonzero coef */ |
472 | 70.3M | if (arith_decode(cinfo, st + 2)) { |
473 | 36.7M | if (*thiscoef < 0) |
474 | 15.6M | *thiscoef += (JCOEF)m1; |
475 | 21.0M | else |
476 | 21.0M | *thiscoef += (JCOEF)p1; |
477 | 36.7M | } |
478 | 70.3M | break; |
479 | 70.3M | } |
480 | 97.3M | if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */ |
481 | 40.5M | if (arith_decode(cinfo, entropy->fixed_bin)) |
482 | 18.4M | *thiscoef = (JCOEF)m1; |
483 | 22.0M | else |
484 | 22.0M | *thiscoef = (JCOEF)p1; |
485 | 40.5M | break; |
486 | 40.5M | } |
487 | 56.8M | st += 3; k++; |
488 | 56.8M | if (k > cinfo->Se) { |
489 | 88.4k | WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
490 | 88.4k | entropy->ct = -1; /* spectral overflow */ |
491 | 88.4k | return TRUE; |
492 | 88.4k | } |
493 | 56.8M | } |
494 | 110M | } |
495 | | |
496 | 25.1M | return TRUE; |
497 | 25.2M | } |
498 | | |
499 | | |
500 | | /* |
501 | | * Decode one MCU's worth of arithmetic-compressed coefficients. |
502 | | */ |
503 | | |
504 | | METHODDEF(boolean) |
505 | | decode_mcu(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
506 | 68.6M | { |
507 | 68.6M | arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; |
508 | 68.6M | jpeg_component_info *compptr; |
509 | 68.6M | JBLOCKROW block; |
510 | 68.6M | unsigned char *st; |
511 | 68.6M | int blkn, ci, tbl, sign, k; |
512 | 68.6M | int v, m; |
513 | | |
514 | | /* Process restart marker if needed */ |
515 | 68.6M | if (cinfo->restart_interval) { |
516 | 9.53M | if (entropy->restarts_to_go == 0) |
517 | 2.91M | process_restart(cinfo); |
518 | 9.53M | entropy->restarts_to_go--; |
519 | 9.53M | } |
520 | | |
521 | 68.6M | if (entropy->ct == -1) return TRUE; /* if error do nothing */ |
522 | | |
523 | | /* Outer loop handles each block in the MCU */ |
524 | | |
525 | 137M | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
526 | 77.2M | block = MCU_data ? MCU_data[blkn] : NULL; |
527 | 77.2M | ci = cinfo->MCU_membership[blkn]; |
528 | 77.2M | compptr = cinfo->cur_comp_info[ci]; |
529 | | |
530 | | /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */ |
531 | | |
532 | 77.2M | tbl = compptr->dc_tbl_no; |
533 | | |
534 | | /* Table F.4: Point to statistics bin S0 for DC coefficient coding */ |
535 | 77.2M | st = entropy->dc_stats[tbl] + entropy->dc_context[ci]; |
536 | | |
537 | | /* Figure F.19: Decode_DC_DIFF */ |
538 | 77.2M | if (arith_decode(cinfo, st) == 0) |
539 | 34.9M | entropy->dc_context[ci] = 0; |
540 | 42.2M | else { |
541 | | /* Figure F.21: Decoding nonzero value v */ |
542 | | /* Figure F.22: Decoding the sign of v */ |
543 | 42.2M | sign = arith_decode(cinfo, st + 1); |
544 | 42.2M | st += 2; st += sign; |
545 | | /* Figure F.23: Decoding the magnitude category of v */ |
546 | 42.2M | if ((m = arith_decode(cinfo, st)) != 0) { |
547 | 24.5M | st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */ |
548 | 76.7M | while (arith_decode(cinfo, st)) { |
549 | 52.1M | if ((m <<= 1) == 0x8000) { |
550 | 4.09k | WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
551 | 4.09k | entropy->ct = -1; /* magnitude overflow */ |
552 | 4.09k | return TRUE; |
553 | 4.09k | } |
554 | 52.1M | st += 1; |
555 | 52.1M | } |
556 | 24.5M | } |
557 | | /* Section F.1.4.4.1.2: Establish dc_context conditioning category */ |
558 | 42.2M | if (m < (int)((1L << cinfo->arith_dc_L[tbl]) >> 1)) |
559 | 1.00M | entropy->dc_context[ci] = 0; /* zero diff category */ |
560 | 41.2M | else if (m > (int)((1L << cinfo->arith_dc_U[tbl]) >> 1)) |
561 | 14.4M | entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */ |
562 | 26.7M | else |
563 | 26.7M | entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */ |
564 | 42.2M | v = m; |
565 | | /* Figure F.24: Decoding the magnitude bit pattern of v */ |
566 | 42.2M | st += 14; |
567 | 94.3M | while (m >>= 1) |
568 | 52.1M | if (arith_decode(cinfo, st)) v |= m; |
569 | 42.2M | v += 1; if (sign) v = -v; |
570 | 42.2M | entropy->last_dc_val[ci] = (entropy->last_dc_val[ci] + v) & 0xffff; |
571 | 42.2M | } |
572 | | |
573 | 77.2M | if (block) |
574 | 77.2M | (*block)[0] = (JCOEF)entropy->last_dc_val[ci]; |
575 | | |
576 | | /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */ |
577 | | |
578 | 77.2M | tbl = compptr->ac_tbl_no; |
579 | | |
580 | | /* Figure F.20: Decode_AC_coefficients */ |
581 | 137M | for (k = 1; k <= DCTSIZE2 - 1; k++) { |
582 | 137M | st = entropy->ac_stats[tbl] + 3 * (k - 1); |
583 | 137M | if (arith_decode(cinfo, st)) break; /* EOB flag */ |
584 | 118M | while (arith_decode(cinfo, st + 1) == 0) { |
585 | 58.6M | st += 3; k++; |
586 | 58.6M | if (k > DCTSIZE2 - 1) { |
587 | 123 | WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
588 | 123 | entropy->ct = -1; /* spectral overflow */ |
589 | 123 | return TRUE; |
590 | 123 | } |
591 | 58.6M | } |
592 | | /* Figure F.21: Decoding nonzero value v */ |
593 | | /* Figure F.22: Decoding the sign of v */ |
594 | 60.2M | sign = arith_decode(cinfo, entropy->fixed_bin); |
595 | 60.2M | st += 2; |
596 | | /* Figure F.23: Decoding the magnitude category of v */ |
597 | 60.2M | if ((m = arith_decode(cinfo, st)) != 0) { |
598 | 31.3M | if (arith_decode(cinfo, st)) { |
599 | 24.7M | m <<= 1; |
600 | 24.7M | st = entropy->ac_stats[tbl] + |
601 | 24.7M | (k <= cinfo->arith_ac_K[tbl] ? 189 : 217); |
602 | 45.7M | while (arith_decode(cinfo, st)) { |
603 | 21.0M | if ((m <<= 1) == 0x8000) { |
604 | 14.3k | WARNMS(cinfo, JWRN_ARITH_BAD_CODE); |
605 | 14.3k | entropy->ct = -1; /* magnitude overflow */ |
606 | 14.3k | return TRUE; |
607 | 14.3k | } |
608 | 21.0M | st += 1; |
609 | 21.0M | } |
610 | 24.7M | } |
611 | 31.3M | } |
612 | 60.2M | v = m; |
613 | | /* Figure F.24: Decoding the magnitude bit pattern of v */ |
614 | 60.2M | st += 14; |
615 | 105M | while (m >>= 1) |
616 | 45.5M | if (arith_decode(cinfo, st)) v |= m; |
617 | 60.2M | v += 1; if (sign) v = -v; |
618 | 60.2M | if (block) |
619 | 60.2M | (*block)[jpeg_natural_order[k]] = (JCOEF)v; |
620 | 60.2M | } |
621 | 77.2M | } |
622 | | |
623 | 60.0M | return TRUE; |
624 | 60.0M | } |
625 | | |
626 | | |
627 | | /* |
628 | | * Initialize for an arithmetic-compressed scan. |
629 | | */ |
630 | | |
631 | | METHODDEF(void) |
632 | | start_pass(j_decompress_ptr cinfo) |
633 | 722k | { |
634 | 722k | arith_entropy_ptr entropy = (arith_entropy_ptr)cinfo->entropy; |
635 | 722k | int ci, tbl; |
636 | 722k | jpeg_component_info *compptr; |
637 | | |
638 | 722k | if (cinfo->progressive_mode) { |
639 | | /* Validate progressive scan parameters */ |
640 | 348k | if (cinfo->Ss == 0) { |
641 | 217k | if (cinfo->Se != 0) |
642 | 769 | goto bad; |
643 | 217k | } else { |
644 | | /* need not check Ss/Se < 0 since they came from unsigned bytes */ |
645 | 130k | if (cinfo->Se < cinfo->Ss || cinfo->Se > DCTSIZE2 - 1) |
646 | 1.20k | goto bad; |
647 | | /* AC scans may have only one component */ |
648 | 129k | if (cinfo->comps_in_scan != 1) |
649 | 123 | goto bad; |
650 | 129k | } |
651 | 346k | if (cinfo->Ah != 0) { |
652 | | /* Successive approximation refinement scan: must have Al = Ah-1. */ |
653 | 124k | if (cinfo->Ah - 1 != cinfo->Al) |
654 | 142 | goto bad; |
655 | 124k | } |
656 | 346k | if (cinfo->Al > 13) { /* need not check for < 0 */ |
657 | 2.36k | bad: |
658 | 2.36k | ERREXIT4(cinfo, JERR_BAD_PROGRESSION, |
659 | 2.36k | cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); |
660 | 2.36k | } |
661 | | /* Update progression status, and verify that scan order is legal. |
662 | | * Note that inter-scan inconsistencies are treated as warnings |
663 | | * not fatal errors ... not clear if this is right way to behave. |
664 | | */ |
665 | 1.06M | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
666 | 717k | int coefi, cindex = cinfo->cur_comp_info[ci]->component_index; |
667 | 717k | int *coef_bit_ptr = &cinfo->coef_bits[cindex][0]; |
668 | 717k | int *prev_coef_bit_ptr = |
669 | 717k | &cinfo->coef_bits[cindex + cinfo->num_components][0]; |
670 | 717k | if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ |
671 | 93.4k | WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); |
672 | 11.4M | for (coefi = MIN(cinfo->Ss, 1); coefi <= MAX(cinfo->Se, 9); coefi++) { |
673 | 10.7M | if (cinfo->input_scan_number > 1) |
674 | 9.61M | prev_coef_bit_ptr[coefi] = coef_bit_ptr[coefi]; |
675 | 1.09M | else |
676 | 1.09M | prev_coef_bit_ptr[coefi] = 0; |
677 | 10.7M | } |
678 | 4.47M | for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { |
679 | 3.75M | int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; |
680 | 3.75M | if (cinfo->Ah != expected) |
681 | 2.44M | WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); |
682 | 3.75M | coef_bit_ptr[coefi] = cinfo->Al; |
683 | 3.75M | } |
684 | 717k | } |
685 | | /* Select MCU decoding routine */ |
686 | 348k | if (cinfo->Ah == 0) { |
687 | 221k | if (cinfo->Ss == 0) |
688 | 185k | entropy->pub.decode_mcu = decode_mcu_DC_first; |
689 | 35.4k | else |
690 | 35.4k | entropy->pub.decode_mcu = decode_mcu_AC_first; |
691 | 221k | } else { |
692 | 127k | if (cinfo->Ss == 0) |
693 | 30.8k | entropy->pub.decode_mcu = decode_mcu_DC_refine; |
694 | 96.2k | else |
695 | 96.2k | entropy->pub.decode_mcu = decode_mcu_AC_refine; |
696 | 127k | } |
697 | 373k | } else { |
698 | | /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. |
699 | | * This ought to be an error condition, but we make it a warning. |
700 | | */ |
701 | 373k | if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2 - 1 || |
702 | 373k | cinfo->Ah != 0 || cinfo->Al != 0) |
703 | 368k | WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); |
704 | | /* Select MCU decoding routine */ |
705 | 373k | entropy->pub.decode_mcu = decode_mcu; |
706 | 373k | } |
707 | | |
708 | | /* Allocate & initialize requested statistics areas */ |
709 | 1.89M | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
710 | 1.17M | compptr = cinfo->cur_comp_info[ci]; |
711 | 1.17M | if (!cinfo->progressive_mode || (cinfo->Ss == 0 && cinfo->Ah == 0)) { |
712 | 990k | tbl = compptr->dc_tbl_no; |
713 | 990k | if (tbl < 0 || tbl >= NUM_ARITH_TBLS) |
714 | 0 | ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); |
715 | 990k | if (entropy->dc_stats[tbl] == NULL) |
716 | 70.8k | entropy->dc_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small) |
717 | 70.8k | ((j_common_ptr)cinfo, JPOOL_IMAGE, DC_STAT_BINS); |
718 | 990k | memset(entropy->dc_stats[tbl], 0, DC_STAT_BINS); |
719 | | /* Initialize DC predictions to 0 */ |
720 | 990k | entropy->last_dc_val[ci] = 0; |
721 | 990k | entropy->dc_context[ci] = 0; |
722 | 990k | } |
723 | 1.17M | if (!cinfo->progressive_mode || cinfo->Ss) { |
724 | 584k | tbl = compptr->ac_tbl_no; |
725 | 584k | if (tbl < 0 || tbl >= NUM_ARITH_TBLS) |
726 | 0 | ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl); |
727 | 584k | if (entropy->ac_stats[tbl] == NULL) |
728 | 83.3k | entropy->ac_stats[tbl] = (unsigned char *)(*cinfo->mem->alloc_small) |
729 | 83.3k | ((j_common_ptr)cinfo, JPOOL_IMAGE, AC_STAT_BINS); |
730 | 584k | memset(entropy->ac_stats[tbl], 0, AC_STAT_BINS); |
731 | 584k | } |
732 | 1.17M | } |
733 | | |
734 | | /* Initialize arithmetic decoding variables */ |
735 | 722k | entropy->c = 0; |
736 | 722k | entropy->a = 0; |
737 | 722k | entropy->ct = -16; /* force reading 2 initial bytes to fill C */ |
738 | 722k | entropy->pub.insufficient_data = FALSE; |
739 | | |
740 | | /* Initialize restart counter */ |
741 | 722k | entropy->restarts_to_go = cinfo->restart_interval; |
742 | 722k | } |
743 | | |
744 | | |
745 | | /* |
746 | | * Module initialization routine for arithmetic entropy decoding. |
747 | | */ |
748 | | |
749 | | GLOBAL(void) |
750 | | jinit_arith_decoder(j_decompress_ptr cinfo) |
751 | 73.0k | { |
752 | 73.0k | arith_entropy_ptr entropy; |
753 | 73.0k | int i; |
754 | | |
755 | 73.0k | entropy = (arith_entropy_ptr) |
756 | 73.0k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
757 | 73.0k | sizeof(arith_entropy_decoder)); |
758 | 73.0k | cinfo->entropy = (struct jpeg_entropy_decoder *)entropy; |
759 | 73.0k | entropy->pub.start_pass = start_pass; |
760 | | |
761 | | /* Mark tables unallocated */ |
762 | 1.24M | for (i = 0; i < NUM_ARITH_TBLS; i++) { |
763 | 1.16M | entropy->dc_stats[i] = NULL; |
764 | 1.16M | entropy->ac_stats[i] = NULL; |
765 | 1.16M | } |
766 | | |
767 | | /* Initialize index for fixed probability estimation */ |
768 | 73.0k | entropy->fixed_bin[0] = 113; |
769 | | |
770 | 73.0k | if (cinfo->progressive_mode) { |
771 | | /* Create progression status table */ |
772 | 45.0k | int *coef_bit_ptr, ci; |
773 | 45.0k | cinfo->coef_bits = (int (*)[DCTSIZE2]) |
774 | 45.0k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
775 | 45.0k | cinfo->num_components * 2 * DCTSIZE2 * |
776 | 45.0k | sizeof(int)); |
777 | 45.0k | coef_bit_ptr = &cinfo->coef_bits[0][0]; |
778 | 176k | for (ci = 0; ci < cinfo->num_components; ci++) |
779 | 8.54M | for (i = 0; i < DCTSIZE2; i++) |
780 | 8.41M | *coef_bit_ptr++ = -1; |
781 | 45.0k | } |
782 | 73.0k | } |