Line data Source code
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 : #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 0 : start_pass_phuff_decoder (j_decompress_ptr cinfo)
96 : {
97 0 : phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
98 : boolean is_DC_band, bad;
99 : int ci, coefi, tbl;
100 : d_derived_tbl **pdtbl;
101 : int *coef_bit_ptr;
102 : jpeg_component_info *compptr;
103 :
104 0 : is_DC_band = (cinfo->Ss == 0);
105 :
106 : /* Validate scan parameters */
107 0 : bad = FALSE;
108 0 : if (is_DC_band) {
109 0 : if (cinfo->Se != 0)
110 0 : bad = TRUE;
111 : } else {
112 : /* 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 : /* AC scans may have only one component */
116 0 : if (cinfo->comps_in_scan != 1)
117 0 : bad = TRUE;
118 : }
119 0 : if (cinfo->Ah != 0) {
120 : /* Successive approximation refinement scan: must have Al = Ah-1. */
121 0 : if (cinfo->Al != cinfo->Ah-1)
122 0 : bad = TRUE;
123 : }
124 0 : if (cinfo->Al > 13) /* need not check for < 0 */
125 0 : bad = TRUE;
126 : /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
127 : * but the spec doesn't say so, and we try to be liberal about what we
128 : * accept. Note: large Al values could result in out-of-range DC
129 : * coefficients during early scans, leading to bizarre displays due to
130 : * overflows in the IDCT math. But we won't crash.
131 : */
132 0 : if (bad)
133 0 : ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
134 : cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
135 : /* Update progression status, and verify that scan order is legal.
136 : * Note that inter-scan inconsistencies are treated as warnings
137 : * not fatal errors ... not clear if this is right way to behave.
138 : */
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 : }
150 : }
151 :
152 : /* 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 : else
157 0 : entropy->pub.decode_mcu = decode_mcu_AC_first;
158 : } else {
159 0 : if (is_DC_band)
160 0 : entropy->pub.decode_mcu = decode_mcu_DC_refine;
161 : else
162 0 : entropy->pub.decode_mcu = decode_mcu_AC_refine;
163 : }
164 :
165 0 : for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
166 0 : compptr = cinfo->cur_comp_info[ci];
167 : /* Make sure requested tables are present, and compute derived tables.
168 : * We may build same derived table more than once, but it's not expensive.
169 : */
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 : }
176 : } 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 : /* remember the single active table */
181 0 : entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
182 : }
183 : /* Initialize DC predictions to 0 */
184 0 : entropy->saved.last_dc_val[ci] = 0;
185 : }
186 :
187 : /* 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 :
192 : /* Initialize private state variables */
193 0 : entropy->saved.EOBRUN = 0;
194 :
195 : /* 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 : #define NEG_1 ((unsigned)-1)
209 : #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 0 : process_restart (j_decompress_ptr cinfo)
235 : {
236 0 : phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
237 : int ci;
238 :
239 : /* Throw away any unused bits remaining in bit buffer; */
240 : /* 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 :
244 : /* Advance past the RSTn marker */
245 0 : if (! (*cinfo->marker->read_restart_marker) (cinfo))
246 0 : return FALSE;
247 :
248 : /* 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 : /* Re-init EOB run count, too */
252 0 : entropy->saved.EOBRUN = 0;
253 :
254 : /* Reset restart counter */
255 0 : entropy->restarts_to_go = cinfo->restart_interval;
256 :
257 : /* Reset out-of-data flag, unless read_restart_marker left us smack up
258 : * against a marker. In that case we will end up treating the next data
259 : * segment as empty, and we can avoid producing bogus output pixels by
260 : * leaving the flag set.
261 : */
262 0 : if (cinfo->unread_marker == 0)
263 0 : entropy->pub.insufficient_data = FALSE;
264 :
265 0 : return TRUE;
266 : }
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 0 : decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
293 : {
294 0 : phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
295 0 : int Al = cinfo->Al;
296 : register int s, r;
297 : int blkn, ci;
298 : JBLOCKROW block;
299 : BITREAD_STATE_VARS;
300 : savable_state state;
301 : d_derived_tbl *tbl;
302 : jpeg_component_info *compptr;
303 :
304 : /* 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 : }
310 :
311 : /* If we've run out of data, just leave the MCU set to zeroes.
312 : * This way, we return uniform gray for the remainder of the segment.
313 : */
314 0 : if (! entropy->pub.insufficient_data) {
315 :
316 : /* Load up working state */
317 0 : BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
318 0 : ASSIGN_STATE(state, entropy->saved);
319 :
320 : /* Outer loop handles each block in the MCU */
321 :
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 :
328 : /* Decode a single block's worth of coefficients */
329 :
330 : /* 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 : }
337 :
338 : /* 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 : /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
342 0 : (*block)[0] = (JCOEF) LEFT_SHIFT(s, Al);
343 : }
344 :
345 : /* Completed MCU, so update state */
346 0 : BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
347 0 : ASSIGN_STATE(entropy->saved, state);
348 : }
349 :
350 : /* Account for restart interval (no-op if not using restarts) */
351 0 : entropy->restarts_to_go--;
352 :
353 0 : return TRUE;
354 : }
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 0 : decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
364 : {
365 0 : phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
366 0 : int Se = cinfo->Se;
367 0 : int Al = cinfo->Al;
368 : register int s, k, r;
369 : unsigned int EOBRUN;
370 : JBLOCKROW block;
371 : BITREAD_STATE_VARS;
372 : d_derived_tbl *tbl;
373 :
374 : /* 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 : }
380 :
381 : /* If we've run out of data, just leave the MCU set to zeroes.
382 : * This way, we return uniform gray for the remainder of the segment.
383 : */
384 0 : if (! entropy->pub.insufficient_data) {
385 :
386 : /* Load up working state.
387 : * We can avoid loading/saving bitread state if in an EOB run.
388 : */
389 0 : EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
390 :
391 : /* There is always only one block per MCU */
392 :
393 0 : if (EOBRUN > 0) /* if it's a band of zeroes... */
394 0 : EOBRUN--; /* ...process it now (we do nothing) */
395 : else {
396 0 : BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
397 0 : block = MCU_data[0];
398 0 : tbl = entropy->ac_derived_tbl;
399 :
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 : /* Scale and output coefficient in natural (dezigzagged) order */
410 0 : (*block)[jpeg_natural_order[k]] = (JCOEF) LEFT_SHIFT(s, Al);
411 : } else {
412 0 : if (r == 15) { /* ZRL */
413 0 : k += 15; /* skip 15 zeroes in band */
414 : } 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 : }
421 0 : EOBRUN--; /* this band is processed at this moment */
422 0 : break; /* force end-of-band */
423 : }
424 : }
425 : }
426 :
427 0 : BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
428 : }
429 :
430 : /* Completed MCU, so update state */
431 0 : entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
432 : }
433 :
434 : /* Account for restart interval (no-op if not using restarts) */
435 0 : entropy->restarts_to_go--;
436 :
437 0 : return TRUE;
438 : }
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 0 : decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
449 : {
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 : int blkn;
453 : JBLOCKROW block;
454 : BITREAD_STATE_VARS;
455 :
456 : /* 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 : }
462 :
463 : /* Not worth the cycles to check insufficient_data here,
464 : * since we will not change the data anyway if we read zeroes.
465 : */
466 :
467 : /* Load up working state */
468 0 : BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
469 :
470 : /* Outer loop handles each block in the MCU */
471 :
472 0 : for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
473 0 : block = MCU_data[blkn];
474 :
475 : /* 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 : /* Note: since we use |=, repeating the assignment later is safe */
480 : }
481 :
482 : /* Completed MCU, so update state */
483 0 : BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
484 :
485 : /* Account for restart interval (no-op if not using restarts) */
486 0 : entropy->restarts_to_go--;
487 :
488 0 : return TRUE;
489 : }
490 :
491 :
492 : /*
493 : * MCU decoding for AC successive approximation refinement scan.
494 : */
495 :
496 : METHODDEF(boolean)
497 0 : decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
498 : {
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 : register int s, k, r;
504 : unsigned int EOBRUN;
505 : JBLOCKROW block;
506 : JCOEFPTR thiscoef;
507 : BITREAD_STATE_VARS;
508 : d_derived_tbl *tbl;
509 : int num_newnz;
510 : int newnz_pos[DCTSIZE2];
511 :
512 : /* 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 : }
518 :
519 : /* If we've run out of data, don't modify the MCU.
520 : */
521 0 : if (! entropy->pub.insufficient_data) {
522 :
523 : /* 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 :
527 : /* There is always only one block per MCU */
528 0 : block = MCU_data[0];
529 0 : tbl = entropy->ac_derived_tbl;
530 :
531 : /* If we are forced to suspend, we must undo the assignments to any newly
532 : * nonzero coefficients in the block, because otherwise we'd get confused
533 : * next time about which coefficients were already nonzero.
534 : * But we need not undo addition of bits to already-nonzero coefficients;
535 : * instead, we can test the current bit to see if we already did it.
536 : */
537 0 : num_newnz = 0;
538 :
539 : /* initialize coefficient loop counter to start of band */
540 0 : k = cinfo->Ss;
541 :
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 : else
554 0 : s = m1; /* newly nonzero coef is negative */
555 : } 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 : }
563 0 : break; /* rest of block is handled by EOB logic */
564 : }
565 : /* note s = 0 for processing ZRL */
566 : }
567 : /* Advance over already-nonzero coefs and r still-zero coefs,
568 : * appending correction bits to the nonzeroes. A correction bit is 1
569 : * if the absolute value of the coefficient must be increased.
570 : */
571 : 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 : else
580 0 : *thiscoef += m1;
581 : }
582 : }
583 : } else {
584 0 : if (--r < 0)
585 0 : break; /* reached target zero coefficient */
586 : }
587 0 : k++;
588 0 : } while (k <= Se);
589 0 : if (s) {
590 0 : int pos = jpeg_natural_order[k];
591 : /* Output newly nonzero coefficient */
592 0 : (*block)[pos] = (JCOEF) s;
593 : /* Remember its position in case we have to suspend */
594 0 : newnz_pos[num_newnz++] = pos;
595 : }
596 : }
597 : }
598 :
599 0 : if (EOBRUN > 0) {
600 : /* Scan any remaining coefficient positions after the end-of-band
601 : * (the last newly nonzero coefficient, if any). Append a correction
602 : * bit to each already-nonzero coefficient. A correction bit is 1
603 : * if the absolute value of the coefficient must be increased.
604 : */
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 : else
614 0 : *thiscoef += m1;
615 : }
616 : }
617 : }
618 : }
619 : /* Count one block completed in EOB run */
620 0 : EOBRUN--;
621 : }
622 :
623 : /* 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 : }
627 :
628 : /* Account for restart interval (no-op if not using restarts) */
629 0 : entropy->restarts_to_go--;
630 :
631 0 : return TRUE;
632 :
633 : undoit:
634 : /* 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 :
638 0 : return FALSE;
639 : }
640 :
641 :
642 : /*
643 : * Module initialization routine for progressive Huffman entropy decoding.
644 : */
645 :
646 : GLOBAL(void)
647 0 : jinit_phuff_decoder (j_decompress_ptr cinfo)
648 : {
649 : phuff_entropy_ptr entropy;
650 : int *coef_bit_ptr;
651 : int ci, i;
652 :
653 0 : entropy = (phuff_entropy_ptr)
654 0 : (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
655 : sizeof(phuff_entropy_decoder));
656 0 : cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
657 0 : entropy->pub.start_pass = start_pass_phuff_decoder;
658 :
659 : /* Mark derived tables unallocated */
660 0 : for (i = 0; i < NUM_HUFF_TBLS; i++) {
661 0 : entropy->derived_tbls[i] = NULL;
662 : }
663 :
664 : /* 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 */
|