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