/src/libjpeg-turbo.2.0.x/jdphuff.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * jdphuff.c |
3 | | * |
4 | | * This file was part of the Independent JPEG Group's software: |
5 | | * Copyright (C) 1995-1997, Thomas G. Lane. |
6 | | * libjpeg-turbo Modifications: |
7 | | * Copyright (C) 2015-2016, 2018, 2021-2022, D. R. Commander. |
8 | | * For conditions of distribution and use, see the accompanying README.ijg |
9 | | * file. |
10 | | * |
11 | | * This file contains Huffman entropy decoding routines for progressive JPEG. |
12 | | * |
13 | | * Much of the complexity here has to do with supporting input suspension. |
14 | | * If the data source module demands suspension, we want to be able to back |
15 | | * up to the start of the current MCU. To do this, we copy state variables |
16 | | * into local working storage, and update them back to the permanent |
17 | | * storage only upon successful completion of an MCU. |
18 | | * |
19 | | * NOTE: All referenced figures are from |
20 | | * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994. |
21 | | */ |
22 | | |
23 | | #define JPEG_INTERNALS |
24 | | #include "jinclude.h" |
25 | | #include "jpeglib.h" |
26 | | #include "jdhuff.h" /* Declarations shared with jdhuff.c */ |
27 | | #include <limits.h> |
28 | | |
29 | | |
30 | | #ifdef D_PROGRESSIVE_SUPPORTED |
31 | | |
32 | | /* |
33 | | * Expanded entropy decoder object for progressive Huffman decoding. |
34 | | * |
35 | | * The savable_state subrecord contains fields that change within an MCU, |
36 | | * but must not be updated permanently until we complete the MCU. |
37 | | */ |
38 | | |
39 | | typedef struct { |
40 | | unsigned int EOBRUN; /* remaining EOBs in EOBRUN */ |
41 | | int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
42 | | } savable_state; |
43 | | |
44 | | /* This macro is to work around compilers with missing or broken |
45 | | * structure assignment. You'll need to fix this code if you have |
46 | | * such a compiler and you change MAX_COMPS_IN_SCAN. |
47 | | */ |
48 | | |
49 | | #ifndef NO_STRUCT_ASSIGN |
50 | 795k | #define ASSIGN_STATE(dest, src) ((dest) = (src)) |
51 | | #else |
52 | | #if MAX_COMPS_IN_SCAN == 4 |
53 | | #define ASSIGN_STATE(dest, src) \ |
54 | | ((dest).EOBRUN = (src).EOBRUN, \ |
55 | | (dest).last_dc_val[0] = (src).last_dc_val[0], \ |
56 | | (dest).last_dc_val[1] = (src).last_dc_val[1], \ |
57 | | (dest).last_dc_val[2] = (src).last_dc_val[2], \ |
58 | | (dest).last_dc_val[3] = (src).last_dc_val[3]) |
59 | | #endif |
60 | | #endif |
61 | | |
62 | | |
63 | | typedef struct { |
64 | | struct jpeg_entropy_decoder pub; /* public fields */ |
65 | | |
66 | | /* These fields are loaded into local variables at start of each MCU. |
67 | | * In case of suspension, we exit WITHOUT updating them. |
68 | | */ |
69 | | bitread_perm_state bitstate; /* Bit buffer at start of MCU */ |
70 | | savable_state saved; /* Other state at start of MCU */ |
71 | | |
72 | | /* These fields are NOT loaded into local working state. */ |
73 | | unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
74 | | |
75 | | /* Pointers to derived tables (these workspaces have image lifespan) */ |
76 | | d_derived_tbl *derived_tbls[NUM_HUFF_TBLS]; |
77 | | |
78 | | d_derived_tbl *ac_derived_tbl; /* active table during an AC scan */ |
79 | | } phuff_entropy_decoder; |
80 | | |
81 | | typedef phuff_entropy_decoder *phuff_entropy_ptr; |
82 | | |
83 | | /* Forward declarations */ |
84 | | METHODDEF(boolean) decode_mcu_DC_first(j_decompress_ptr cinfo, |
85 | | JBLOCKROW *MCU_data); |
86 | | METHODDEF(boolean) decode_mcu_AC_first(j_decompress_ptr cinfo, |
87 | | JBLOCKROW *MCU_data); |
88 | | METHODDEF(boolean) decode_mcu_DC_refine(j_decompress_ptr cinfo, |
89 | | JBLOCKROW *MCU_data); |
90 | | METHODDEF(boolean) decode_mcu_AC_refine(j_decompress_ptr cinfo, |
91 | | JBLOCKROW *MCU_data); |
92 | | |
93 | | |
94 | | /* |
95 | | * Initialize for a Huffman-compressed scan. |
96 | | */ |
97 | | |
98 | | METHODDEF(void) |
99 | | start_pass_phuff_decoder(j_decompress_ptr cinfo) |
100 | 13.8k | { |
101 | 13.8k | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
102 | 13.8k | boolean is_DC_band, bad; |
103 | 13.8k | int ci, coefi, tbl; |
104 | 13.8k | d_derived_tbl **pdtbl; |
105 | 13.8k | int *coef_bit_ptr; |
106 | 13.8k | jpeg_component_info *compptr; |
107 | | |
108 | 13.8k | is_DC_band = (cinfo->Ss == 0); |
109 | | |
110 | | /* Validate scan parameters */ |
111 | 13.8k | bad = FALSE; |
112 | 13.8k | if (is_DC_band) { |
113 | 6.24k | if (cinfo->Se != 0) |
114 | 16 | bad = TRUE; |
115 | 7.62k | } else { |
116 | | /* need not check Ss/Se < 0 since they came from unsigned bytes */ |
117 | 7.62k | if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2) |
118 | 82 | bad = TRUE; |
119 | | /* AC scans may have only one component */ |
120 | 7.62k | if (cinfo->comps_in_scan != 1) |
121 | 17 | bad = TRUE; |
122 | 7.62k | } |
123 | 13.8k | if (cinfo->Ah != 0) { |
124 | | /* Successive approximation refinement scan: must have Al = Ah-1. */ |
125 | 6.00k | if (cinfo->Al != cinfo->Ah - 1) |
126 | 87 | bad = TRUE; |
127 | 6.00k | } |
128 | 13.8k | if (cinfo->Al > 13) /* need not check for < 0 */ |
129 | 37 | bad = TRUE; |
130 | | /* Arguably the maximum Al value should be less than 13 for 8-bit precision, |
131 | | * but the spec doesn't say so, and we try to be liberal about what we |
132 | | * accept. Note: large Al values could result in out-of-range DC |
133 | | * coefficients during early scans, leading to bizarre displays due to |
134 | | * overflows in the IDCT math. But we won't crash. |
135 | | */ |
136 | 13.8k | if (bad) |
137 | 104 | ERREXIT4(cinfo, JERR_BAD_PROGRESSION, |
138 | 13.8k | cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); |
139 | | /* Update progression status, and verify that scan order is legal. |
140 | | * Note that inter-scan inconsistencies are treated as warnings |
141 | | * not fatal errors ... not clear if this is right way to behave. |
142 | | */ |
143 | 35.0k | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
144 | 21.1k | int cindex = cinfo->cur_comp_info[ci]->component_index; |
145 | 21.1k | coef_bit_ptr = &cinfo->coef_bits[cindex][0]; |
146 | 21.1k | if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ |
147 | 4.70k | WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); |
148 | 257k | for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { |
149 | 236k | int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; |
150 | 236k | if (cinfo->Ah != expected) |
151 | 134k | WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); |
152 | 236k | coef_bit_ptr[coefi] = cinfo->Al; |
153 | 236k | } |
154 | 21.1k | } |
155 | | |
156 | | /* Select MCU decoding routine */ |
157 | 13.8k | if (cinfo->Ah == 0) { |
158 | 7.85k | if (is_DC_band) |
159 | 4.39k | entropy->pub.decode_mcu = decode_mcu_DC_first; |
160 | 3.46k | else |
161 | 3.46k | entropy->pub.decode_mcu = decode_mcu_AC_first; |
162 | 7.85k | } else { |
163 | 6.01k | if (is_DC_band) |
164 | 1.83k | entropy->pub.decode_mcu = decode_mcu_DC_refine; |
165 | 4.18k | else |
166 | 4.18k | entropy->pub.decode_mcu = decode_mcu_AC_refine; |
167 | 6.01k | } |
168 | | |
169 | 35.0k | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
170 | 21.1k | compptr = cinfo->cur_comp_info[ci]; |
171 | | /* Make sure requested tables are present, and compute derived tables. |
172 | | * We may build same derived table more than once, but it's not expensive. |
173 | | */ |
174 | 21.1k | if (is_DC_band) { |
175 | 13.6k | if (cinfo->Ah == 0) { /* DC refinement needs no table */ |
176 | 9.99k | tbl = compptr->dc_tbl_no; |
177 | 9.99k | pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl; |
178 | 9.99k | jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, pdtbl); |
179 | 9.99k | } |
180 | 13.6k | } else { |
181 | 7.54k | tbl = compptr->ac_tbl_no; |
182 | 7.54k | pdtbl = (d_derived_tbl **)(entropy->derived_tbls) + tbl; |
183 | 7.54k | jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, pdtbl); |
184 | | /* remember the single active table */ |
185 | 7.54k | entropy->ac_derived_tbl = entropy->derived_tbls[tbl]; |
186 | 7.54k | } |
187 | | /* Initialize DC predictions to 0 */ |
188 | 21.1k | entropy->saved.last_dc_val[ci] = 0; |
189 | 21.1k | } |
190 | | |
191 | | /* Initialize bitread state variables */ |
192 | 13.8k | entropy->bitstate.bits_left = 0; |
193 | 13.8k | entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ |
194 | 13.8k | entropy->pub.insufficient_data = FALSE; |
195 | | |
196 | | /* Initialize private state variables */ |
197 | 13.8k | entropy->saved.EOBRUN = 0; |
198 | | |
199 | | /* Initialize restart counter */ |
200 | 13.8k | entropy->restarts_to_go = cinfo->restart_interval; |
201 | 13.8k | } |
202 | | |
203 | | |
204 | | /* |
205 | | * Figure F.12: extend sign bit. |
206 | | * On some machines, a shift and add will be faster than a table lookup. |
207 | | */ |
208 | | |
209 | | #define AVOID_TABLES |
210 | | #ifdef AVOID_TABLES |
211 | | |
212 | 43.5M | #define NEG_1 ((unsigned)-1) |
213 | | #define HUFF_EXTEND(x, s) \ |
214 | 1.12M | ((x) < (1 << ((s) - 1)) ? (x) + (((NEG_1) << (s)) + 1) : (x)) |
215 | | |
216 | | #else |
217 | | |
218 | | #define HUFF_EXTEND(x, s) \ |
219 | | ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) |
220 | | |
221 | | static const int extend_test[16] = { /* entry n is 2**(n-1) */ |
222 | | 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, |
223 | | 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 |
224 | | }; |
225 | | |
226 | | static const int extend_offset[16] = { /* entry n is (-1 << n) + 1 */ |
227 | | 0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1, |
228 | | ((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1, |
229 | | ((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1, |
230 | | ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1 |
231 | | }; |
232 | | |
233 | | #endif /* AVOID_TABLES */ |
234 | | |
235 | | |
236 | | /* |
237 | | * Check for a restart marker & resynchronize decoder. |
238 | | * Returns FALSE if must suspend. |
239 | | */ |
240 | | |
241 | | LOCAL(boolean) |
242 | | process_restart(j_decompress_ptr cinfo) |
243 | 80.1M | { |
244 | 80.1M | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
245 | 80.1M | int ci; |
246 | | |
247 | | /* Throw away any unused bits remaining in bit buffer; */ |
248 | | /* include any full bytes in next_marker's count of discarded bytes */ |
249 | 80.1M | cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; |
250 | 80.1M | entropy->bitstate.bits_left = 0; |
251 | | |
252 | | /* Advance past the RSTn marker */ |
253 | 80.1M | if (!(*cinfo->marker->read_restart_marker) (cinfo)) |
254 | 0 | return FALSE; |
255 | | |
256 | | /* Re-initialize DC predictions to 0 */ |
257 | 162M | for (ci = 0; ci < cinfo->comps_in_scan; ci++) |
258 | 82.2M | entropy->saved.last_dc_val[ci] = 0; |
259 | | /* Re-init EOB run count, too */ |
260 | 80.1M | entropy->saved.EOBRUN = 0; |
261 | | |
262 | | /* Reset restart counter */ |
263 | 80.1M | entropy->restarts_to_go = cinfo->restart_interval; |
264 | | |
265 | | /* Reset out-of-data flag, unless read_restart_marker left us smack up |
266 | | * against a marker. In that case we will end up treating the next data |
267 | | * segment as empty, and we can avoid producing bogus output pixels by |
268 | | * leaving the flag set. |
269 | | */ |
270 | 80.1M | if (cinfo->unread_marker == 0) |
271 | 26.4k | entropy->pub.insufficient_data = FALSE; |
272 | | |
273 | 80.1M | return TRUE; |
274 | 80.1M | } |
275 | | |
276 | | |
277 | | /* |
278 | | * Huffman MCU decoding. |
279 | | * Each of these routines decodes and returns one MCU's worth of |
280 | | * Huffman-compressed coefficients. |
281 | | * The coefficients are reordered from zigzag order into natural array order, |
282 | | * but are not dequantized. |
283 | | * |
284 | | * The i'th block of the MCU is stored into the block pointed to by |
285 | | * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. |
286 | | * |
287 | | * We return FALSE if data source requested suspension. In that case no |
288 | | * changes have been made to permanent state. (Exception: some output |
289 | | * coefficients may already have been assigned. This is harmless for |
290 | | * spectral selection, since we'll just re-assign them on the next call. |
291 | | * Successive approximation AC refinement has to be more careful, however.) |
292 | | */ |
293 | | |
294 | | /* |
295 | | * MCU decoding for DC initial scan (either spectral selection, |
296 | | * or first pass of successive approximation). |
297 | | */ |
298 | | |
299 | | METHODDEF(boolean) |
300 | | decode_mcu_DC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
301 | 26.5M | { |
302 | 26.5M | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
303 | 26.5M | int Al = cinfo->Al; |
304 | 26.5M | register int s, r; |
305 | 26.5M | int blkn, ci; |
306 | 26.5M | JBLOCKROW block; |
307 | 26.5M | BITREAD_STATE_VARS; |
308 | 26.5M | savable_state state; |
309 | 26.5M | d_derived_tbl *tbl; |
310 | 26.5M | jpeg_component_info *compptr; |
311 | | |
312 | | /* Process restart marker if needed; may have to suspend */ |
313 | 26.5M | if (cinfo->restart_interval) { |
314 | 10.3M | if (entropy->restarts_to_go == 0) |
315 | 5.52M | if (!process_restart(cinfo)) |
316 | 0 | return FALSE; |
317 | 10.3M | } |
318 | | |
319 | | /* If we've run out of data, just leave the MCU set to zeroes. |
320 | | * This way, we return uniform gray for the remainder of the segment. |
321 | | */ |
322 | 26.5M | if (!entropy->pub.insufficient_data) { |
323 | | |
324 | | /* Load up working state */ |
325 | 397k | BITREAD_LOAD_STATE(cinfo, entropy->bitstate); |
326 | 397k | ASSIGN_STATE(state, entropy->saved); |
327 | | |
328 | | /* Outer loop handles each block in the MCU */ |
329 | | |
330 | 1.33M | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
331 | 934k | block = MCU_data[blkn]; |
332 | 934k | ci = cinfo->MCU_membership[blkn]; |
333 | 934k | compptr = cinfo->cur_comp_info[ci]; |
334 | 934k | tbl = entropy->derived_tbls[compptr->dc_tbl_no]; |
335 | | |
336 | | /* Decode a single block's worth of coefficients */ |
337 | | |
338 | | /* Section F.2.2.1: decode the DC coefficient difference */ |
339 | 934k | HUFF_DECODE(s, br_state, tbl, return FALSE, label1); |
340 | 934k | if (s) { |
341 | 543k | CHECK_BIT_BUFFER(br_state, s, return FALSE); |
342 | 543k | r = GET_BITS(s); |
343 | 543k | s = HUFF_EXTEND(r, s); |
344 | 543k | } |
345 | | |
346 | | /* Convert DC difference to actual value, update last_dc_val */ |
347 | 934k | if ((state.last_dc_val[ci] >= 0 && |
348 | 934k | s > INT_MAX - state.last_dc_val[ci]) || |
349 | 934k | (state.last_dc_val[ci] < 0 && s < INT_MIN - state.last_dc_val[ci])) |
350 | 0 | ERREXIT(cinfo, JERR_BAD_DCT_COEF); |
351 | 934k | s += state.last_dc_val[ci]; |
352 | 934k | state.last_dc_val[ci] = s; |
353 | | /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */ |
354 | 934k | (*block)[0] = (JCOEF)LEFT_SHIFT(s, Al); |
355 | 934k | } |
356 | | |
357 | | /* Completed MCU, so update state */ |
358 | 397k | BITREAD_SAVE_STATE(cinfo, entropy->bitstate); |
359 | 397k | ASSIGN_STATE(entropy->saved, state); |
360 | 397k | } |
361 | | |
362 | | /* Account for restart interval (no-op if not using restarts) */ |
363 | 26.5M | if (cinfo->restart_interval) |
364 | 10.3M | entropy->restarts_to_go--; |
365 | | |
366 | 26.5M | return TRUE; |
367 | 26.5M | } |
368 | | |
369 | | |
370 | | /* |
371 | | * MCU decoding for AC initial scan (either spectral selection, |
372 | | * or first pass of successive approximation). |
373 | | */ |
374 | | |
375 | | METHODDEF(boolean) |
376 | | decode_mcu_AC_first(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
377 | 30.2M | { |
378 | 30.2M | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
379 | 30.2M | int Se = cinfo->Se; |
380 | 30.2M | int Al = cinfo->Al; |
381 | 30.2M | register int s, k, r; |
382 | 30.2M | unsigned int EOBRUN; |
383 | 30.2M | JBLOCKROW block; |
384 | 30.2M | BITREAD_STATE_VARS; |
385 | 30.2M | d_derived_tbl *tbl; |
386 | | |
387 | | /* Process restart marker if needed; may have to suspend */ |
388 | 30.2M | if (cinfo->restart_interval) { |
389 | 9.20M | if (entropy->restarts_to_go == 0) |
390 | 5.73M | if (!process_restart(cinfo)) |
391 | 0 | return FALSE; |
392 | 9.20M | } |
393 | | |
394 | | /* If we've run out of data, just leave the MCU set to zeroes. |
395 | | * This way, we return uniform gray for the remainder of the segment. |
396 | | */ |
397 | 30.2M | if (!entropy->pub.insufficient_data) { |
398 | | |
399 | | /* Load up working state. |
400 | | * We can avoid loading/saving bitread state if in an EOB run. |
401 | | */ |
402 | 731k | EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ |
403 | | |
404 | | /* There is always only one block per MCU */ |
405 | | |
406 | 731k | if (EOBRUN > 0) /* if it's a band of zeroes... */ |
407 | 532k | EOBRUN--; /* ...process it now (we do nothing) */ |
408 | 199k | else { |
409 | 199k | BITREAD_LOAD_STATE(cinfo, entropy->bitstate); |
410 | 199k | block = MCU_data[0]; |
411 | 199k | tbl = entropy->ac_derived_tbl; |
412 | | |
413 | 809k | for (k = cinfo->Ss; k <= Se; k++) { |
414 | 717k | HUFF_DECODE(s, br_state, tbl, return FALSE, label2); |
415 | 717k | r = s >> 4; |
416 | 717k | s &= 15; |
417 | 717k | if (s) { |
418 | 582k | k += r; |
419 | 582k | CHECK_BIT_BUFFER(br_state, s, return FALSE); |
420 | 582k | r = GET_BITS(s); |
421 | 582k | s = HUFF_EXTEND(r, s); |
422 | | /* Scale and output coefficient in natural (dezigzagged) order */ |
423 | 582k | (*block)[jpeg_natural_order[k]] = (JCOEF)LEFT_SHIFT(s, Al); |
424 | 582k | } else { |
425 | 134k | if (r == 15) { /* ZRL */ |
426 | 27.8k | k += 15; /* skip 15 zeroes in band */ |
427 | 106k | } else { /* EOBr, run length is 2^r + appended bits */ |
428 | 106k | EOBRUN = 1 << r; |
429 | 106k | if (r) { /* EOBr, r > 0 */ |
430 | 24.7k | CHECK_BIT_BUFFER(br_state, r, return FALSE); |
431 | 24.7k | r = GET_BITS(r); |
432 | 24.7k | EOBRUN += r; |
433 | 24.7k | } |
434 | 106k | EOBRUN--; /* this band is processed at this moment */ |
435 | 106k | break; /* force end-of-band */ |
436 | 106k | } |
437 | 134k | } |
438 | 717k | } |
439 | | |
440 | 199k | BITREAD_SAVE_STATE(cinfo, entropy->bitstate); |
441 | 199k | } |
442 | | |
443 | | /* Completed MCU, so update state */ |
444 | 731k | entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ |
445 | 731k | } |
446 | | |
447 | | /* Account for restart interval (no-op if not using restarts) */ |
448 | 30.2M | if (cinfo->restart_interval) |
449 | 9.20M | entropy->restarts_to_go--; |
450 | | |
451 | 30.2M | return TRUE; |
452 | 30.2M | } |
453 | | |
454 | | |
455 | | /* |
456 | | * MCU decoding for DC successive approximation refinement scan. |
457 | | * Note: we assume such scans can be multi-component, although the spec |
458 | | * is not very clear on the point. |
459 | | */ |
460 | | |
461 | | METHODDEF(boolean) |
462 | | decode_mcu_DC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
463 | 68.2M | { |
464 | 68.2M | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
465 | 68.2M | int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ |
466 | 68.2M | int blkn; |
467 | 68.2M | JBLOCKROW block; |
468 | 68.2M | BITREAD_STATE_VARS; |
469 | | |
470 | | /* Process restart marker if needed; may have to suspend */ |
471 | 68.2M | if (cinfo->restart_interval) { |
472 | 60.0M | if (entropy->restarts_to_go == 0) |
473 | 56.0M | if (!process_restart(cinfo)) |
474 | 0 | return FALSE; |
475 | 60.0M | } |
476 | | |
477 | | /* Not worth the cycles to check insufficient_data here, |
478 | | * since we will not change the data anyway if we read zeroes. |
479 | | */ |
480 | | |
481 | | /* Load up working state */ |
482 | 68.2M | BITREAD_LOAD_STATE(cinfo, entropy->bitstate); |
483 | | |
484 | | /* Outer loop handles each block in the MCU */ |
485 | | |
486 | 145M | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
487 | 76.8M | block = MCU_data[blkn]; |
488 | | |
489 | | /* Encoded data is simply the next bit of the two's-complement DC value */ |
490 | 76.8M | CHECK_BIT_BUFFER(br_state, 1, return FALSE); |
491 | 76.8M | if (GET_BITS(1)) |
492 | 65.4k | (*block)[0] |= p1; |
493 | | /* Note: since we use |=, repeating the assignment later is safe */ |
494 | 76.8M | } |
495 | | |
496 | | /* Completed MCU, so update state */ |
497 | 68.2M | BITREAD_SAVE_STATE(cinfo, entropy->bitstate); |
498 | | |
499 | | /* Account for restart interval (no-op if not using restarts) */ |
500 | 68.2M | if (cinfo->restart_interval) |
501 | 60.0M | entropy->restarts_to_go--; |
502 | | |
503 | 68.2M | return TRUE; |
504 | 68.2M | } |
505 | | |
506 | | |
507 | | /* |
508 | | * MCU decoding for AC successive approximation refinement scan. |
509 | | */ |
510 | | |
511 | | METHODDEF(boolean) |
512 | | decode_mcu_AC_refine(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
513 | 42.7M | { |
514 | 42.7M | phuff_entropy_ptr entropy = (phuff_entropy_ptr)cinfo->entropy; |
515 | 42.7M | int Se = cinfo->Se; |
516 | 42.7M | int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ |
517 | 42.7M | int m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */ |
518 | 42.7M | register int s, k, r; |
519 | 42.7M | unsigned int EOBRUN; |
520 | 42.7M | JBLOCKROW block; |
521 | 42.7M | JCOEFPTR thiscoef; |
522 | 42.7M | BITREAD_STATE_VARS; |
523 | 42.7M | d_derived_tbl *tbl; |
524 | 42.7M | int num_newnz; |
525 | 42.7M | int newnz_pos[DCTSIZE2]; |
526 | | |
527 | | /* Process restart marker if needed; may have to suspend */ |
528 | 42.7M | if (cinfo->restart_interval) { |
529 | 20.0M | if (entropy->restarts_to_go == 0) |
530 | 12.8M | if (!process_restart(cinfo)) |
531 | 0 | return FALSE; |
532 | 20.0M | } |
533 | | |
534 | | /* If we've run out of data, don't modify the MCU. |
535 | | */ |
536 | 42.7M | if (!entropy->pub.insufficient_data) { |
537 | | |
538 | | /* Load up working state */ |
539 | 944k | BITREAD_LOAD_STATE(cinfo, entropy->bitstate); |
540 | 944k | EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ |
541 | | |
542 | | /* There is always only one block per MCU */ |
543 | 944k | block = MCU_data[0]; |
544 | 944k | tbl = entropy->ac_derived_tbl; |
545 | | |
546 | | /* If we are forced to suspend, we must undo the assignments to any newly |
547 | | * nonzero coefficients in the block, because otherwise we'd get confused |
548 | | * next time about which coefficients were already nonzero. |
549 | | * But we need not undo addition of bits to already-nonzero coefficients; |
550 | | * instead, we can test the current bit to see if we already did it. |
551 | | */ |
552 | 944k | num_newnz = 0; |
553 | | |
554 | | /* initialize coefficient loop counter to start of band */ |
555 | 944k | k = cinfo->Ss; |
556 | | |
557 | 944k | if (EOBRUN == 0) { |
558 | 2.14M | for (; k <= Se; k++) { |
559 | 1.85M | HUFF_DECODE(s, br_state, tbl, goto undoit, label3); |
560 | 1.85M | r = s >> 4; |
561 | 1.85M | s &= 15; |
562 | 1.85M | if (s) { |
563 | 1.72M | if (s != 1) /* size of new coef should always be 1 */ |
564 | 1.23M | WARNMS(cinfo, JWRN_HUFF_BAD_CODE); |
565 | 1.72M | CHECK_BIT_BUFFER(br_state, 1, goto undoit); |
566 | 1.72M | if (GET_BITS(1)) |
567 | 203k | s = p1; /* newly nonzero coef is positive */ |
568 | 1.52M | else |
569 | 1.52M | s = m1; /* newly nonzero coef is negative */ |
570 | 1.72M | } else { |
571 | 120k | if (r != 15) { |
572 | 105k | EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */ |
573 | 105k | if (r) { |
574 | 29.7k | CHECK_BIT_BUFFER(br_state, r, goto undoit); |
575 | 29.7k | r = GET_BITS(r); |
576 | 29.7k | EOBRUN += r; |
577 | 29.7k | } |
578 | 105k | break; /* rest of block is handled by EOB logic */ |
579 | 105k | } |
580 | | /* note s = 0 for processing ZRL */ |
581 | 120k | } |
582 | | /* Advance over already-nonzero coefs and r still-zero coefs, |
583 | | * appending correction bits to the nonzeroes. A correction bit is 1 |
584 | | * if the absolute value of the coefficient must be increased. |
585 | | */ |
586 | 9.32M | do { |
587 | 9.32M | thiscoef = *block + jpeg_natural_order[k]; |
588 | 9.32M | if (*thiscoef != 0) { |
589 | 3.32M | CHECK_BIT_BUFFER(br_state, 1, goto undoit); |
590 | 3.32M | if (GET_BITS(1)) { |
591 | 1.40M | if ((*thiscoef & p1) == 0) { /* do nothing if already set it */ |
592 | 35.6k | if (*thiscoef >= 0) |
593 | 14.5k | *thiscoef += (JCOEF)p1; |
594 | 21.1k | else |
595 | 21.1k | *thiscoef += (JCOEF)m1; |
596 | 35.6k | } |
597 | 1.40M | } |
598 | 5.99M | } else { |
599 | 5.99M | if (--r < 0) |
600 | 1.51M | break; /* reached target zero coefficient */ |
601 | 5.99M | } |
602 | 7.80M | k++; |
603 | 7.80M | } while (k <= Se); |
604 | 1.74M | if (s) { |
605 | 1.72M | int pos = jpeg_natural_order[k]; |
606 | | /* Output newly nonzero coefficient */ |
607 | 1.72M | (*block)[pos] = (JCOEF)s; |
608 | | /* Remember its position in case we have to suspend */ |
609 | 1.72M | newnz_pos[num_newnz++] = pos; |
610 | 1.72M | } |
611 | 1.74M | } |
612 | 403k | } |
613 | | |
614 | 944k | if (EOBRUN > 0) { |
615 | | /* Scan any remaining coefficient positions after the end-of-band |
616 | | * (the last newly nonzero coefficient, if any). Append a correction |
617 | | * bit to each already-nonzero coefficient. A correction bit is 1 |
618 | | * if the absolute value of the coefficient must be increased. |
619 | | */ |
620 | 14.5M | for (; k <= Se; k++) { |
621 | 13.9M | thiscoef = *block + jpeg_natural_order[k]; |
622 | 13.9M | if (*thiscoef != 0) { |
623 | 807k | CHECK_BIT_BUFFER(br_state, 1, goto undoit); |
624 | 807k | if (GET_BITS(1)) { |
625 | 350k | if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */ |
626 | 21.4k | if (*thiscoef >= 0) |
627 | 9.39k | *thiscoef += (JCOEF)p1; |
628 | 12.0k | else |
629 | 12.0k | *thiscoef += (JCOEF)m1; |
630 | 21.4k | } |
631 | 350k | } |
632 | 807k | } |
633 | 13.9M | } |
634 | | /* Count one block completed in EOB run */ |
635 | 647k | EOBRUN--; |
636 | 647k | } |
637 | | |
638 | | /* Completed MCU, so update state */ |
639 | 944k | BITREAD_SAVE_STATE(cinfo, entropy->bitstate); |
640 | 944k | entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ |
641 | 944k | } |
642 | | |
643 | | /* Account for restart interval (no-op if not using restarts) */ |
644 | 42.7M | if (cinfo->restart_interval) |
645 | 20.0M | entropy->restarts_to_go--; |
646 | | |
647 | 42.7M | return TRUE; |
648 | | |
649 | 0 | undoit: |
650 | | /* Re-zero any output coefficients that we made newly nonzero */ |
651 | 0 | while (num_newnz > 0) |
652 | 0 | (*block)[newnz_pos[--num_newnz]] = 0; |
653 | |
|
654 | 0 | return FALSE; |
655 | 42.7M | } |
656 | | |
657 | | |
658 | | /* |
659 | | * Module initialization routine for progressive Huffman entropy decoding. |
660 | | */ |
661 | | |
662 | | GLOBAL(void) |
663 | | jinit_phuff_decoder(j_decompress_ptr cinfo) |
664 | 9.06k | { |
665 | 9.06k | phuff_entropy_ptr entropy; |
666 | 9.06k | int *coef_bit_ptr; |
667 | 9.06k | int ci, i; |
668 | | |
669 | 9.06k | entropy = (phuff_entropy_ptr) |
670 | 9.06k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
671 | 9.06k | sizeof(phuff_entropy_decoder)); |
672 | 9.06k | cinfo->entropy = (struct jpeg_entropy_decoder *)entropy; |
673 | 9.06k | entropy->pub.start_pass = start_pass_phuff_decoder; |
674 | | |
675 | | /* Mark derived tables unallocated */ |
676 | 45.3k | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
677 | 36.2k | entropy->derived_tbls[i] = NULL; |
678 | 36.2k | } |
679 | | |
680 | | /* Create progression status table */ |
681 | 9.06k | cinfo->coef_bits = (int (*)[DCTSIZE2]) |
682 | 9.06k | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
683 | 9.06k | cinfo->num_components * DCTSIZE2 * |
684 | 9.06k | sizeof(int)); |
685 | 9.06k | coef_bit_ptr = &cinfo->coef_bits[0][0]; |
686 | 26.4k | for (ci = 0; ci < cinfo->num_components; ci++) |
687 | 1.13M | for (i = 0; i < DCTSIZE2; i++) |
688 | 1.11M | *coef_bit_ptr++ = -1; |
689 | 9.06k | } |
690 | | |
691 | | #endif /* D_PROGRESSIVE_SUPPORTED */ |