4 * Copyright (C) 1995-1998, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
8 * This file contains Huffman entropy decoding routines for progressive JPEG.
10 * Much of the complexity here has to do with supporting input suspension.
11 * If the data source module demands suspension, we want to be able to back
12 * up to the start of the current MCU. To do this, we copy state variables
13 * into local working storage, and update them back to the permanent
14 * storage only upon successful completion of an MCU.
17 #define JPEG_INTERNALS
20 #include "jlossy.h" /* Private declarations for lossy subsystem */
21 #include "jdhuff.h" /* Declarations shared with jd*huff.c */
24 #ifdef D_PROGRESSIVE_SUPPORTED
27 * Private entropy decoder object for progressive Huffman decoding.
29 * The savable_state subrecord contains fields that change within an MCU,
30 * but must not be updated permanently until we complete the MCU.
34 unsigned int EOBRUN; /* remaining EOBs in EOBRUN */
35 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
38 /* This macro is to work around compilers with missing or broken
39 * structure assignment. You'll need to fix this code if you have
40 * such a compiler and you change MAX_COMPS_IN_SCAN.
43 #ifndef NO_STRUCT_ASSIGN
44 #define ASSIGN_STATE(dest,src) ((dest) = (src))
46 #if MAX_COMPS_IN_SCAN == 4
47 #define ASSIGN_STATE(dest,src) \
48 ((dest).EOBRUN = (src).EOBRUN, \
49 (dest).last_dc_val[0] = (src).last_dc_val[0], \
50 (dest).last_dc_val[1] = (src).last_dc_val[1], \
51 (dest).last_dc_val[2] = (src).last_dc_val[2], \
52 (dest).last_dc_val[3] = (src).last_dc_val[3])
58 huffd_common_fields; /* Fields shared with other entropy decoders */
60 /* These fields are loaded into local variables at start of each MCU.
61 * In case of suspension, we exit WITHOUT updating them.
63 savable_state saved; /* Other state at start of MCU */
65 /* These fields are NOT loaded into local working state. */
66 unsigned int restarts_to_go; /* MCUs left in this restart interval */
68 /* Pointers to derived tables (these workspaces have image lifespan) */
69 d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
71 d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
72 } phuff_entropy_decoder;
74 typedef phuff_entropy_decoder * phuff_entropy_ptr;
76 /* Forward declarations */
77 METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
78 JBLOCKROW *MCU_data));
79 METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
80 JBLOCKROW *MCU_data));
81 METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
82 JBLOCKROW *MCU_data));
83 METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
84 JBLOCKROW *MCU_data));
88 * Initialize for a Huffman-compressed scan.
92 start_pass_phuff_decoder (j_decompress_ptr cinfo)
94 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
95 phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
96 boolean is_DC_band, bad;
99 jpeg_component_info * compptr;
101 is_DC_band = (cinfo->Ss == 0);
103 /* Validate scan parameters */
109 /* need not check Ss/Se < 0 since they came from unsigned bytes */
110 if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
112 /* AC scans may have only one component */
113 if (cinfo->comps_in_scan != 1)
116 if (cinfo->Ah != 0) {
117 /* Successive approximation refinement scan: must have Al = Ah-1. */
118 if (cinfo->Al != cinfo->Ah-1)
121 if (cinfo->Al > 13) /* need not check for < 0 */
123 /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
124 * but the spec doesn't say so, and we try to be liberal about what we
125 * accept. Note: large Al values could result in out-of-range DC
126 * coefficients during early scans, leading to bizarre displays due to
127 * overflows in the IDCT math. But we won't crash.
130 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
131 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
132 /* Update progression status, and verify that scan order is legal.
133 * Note that inter-scan inconsistencies are treated as warnings
134 * not fatal errors ... not clear if this is right way to behave.
136 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
137 int cindex = cinfo->cur_comp_info[ci]->component_index;
138 coef_bit_ptr = & cinfo->coef_bits[cindex][0];
139 if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
140 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
141 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
142 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
143 if (cinfo->Ah != expected)
144 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
145 coef_bit_ptr[coefi] = cinfo->Al;
149 /* Select MCU decoding routine */
150 if (cinfo->Ah == 0) {
152 lossyd->entropy_decode_mcu = decode_mcu_DC_first;
154 lossyd->entropy_decode_mcu = decode_mcu_AC_first;
157 lossyd->entropy_decode_mcu = decode_mcu_DC_refine;
159 lossyd->entropy_decode_mcu = decode_mcu_AC_refine;
162 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
163 compptr = cinfo->cur_comp_info[ci];
164 /* Make sure requested tables are present, and compute derived tables.
165 * We may build same derived table more than once, but it's not expensive.
168 if (cinfo->Ah == 0) { /* DC refinement needs no table */
169 tbl = compptr->dc_tbl_no;
170 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
171 & entropy->derived_tbls[tbl]);
174 tbl = compptr->ac_tbl_no;
175 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
176 & entropy->derived_tbls[tbl]);
177 /* remember the single active table */
178 entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
180 /* Initialize DC predictions to 0 */
181 entropy->saved.last_dc_val[ci] = 0;
184 /* Initialize bitread state variables */
185 entropy->bitstate.bits_left = 0;
186 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
187 entropy->insufficient_data = FALSE;
189 /* Initialize private state variables */
190 entropy->saved.EOBRUN = 0;
192 /* Initialize restart counter */
193 entropy->restarts_to_go = cinfo->restart_interval;
198 * Figure F.12: extend sign bit.
199 * On some machines, a shift and add will be faster than a table lookup.
204 #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
208 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
210 static const int extend_test[16] = /* entry n is 2**(n-1) */
211 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
212 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
214 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
215 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
216 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
217 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
218 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
220 #endif /* AVOID_TABLES */
224 * Check for a restart marker & resynchronize decoder.
225 * Returns FALSE if must suspend.
229 process_restart (j_decompress_ptr cinfo)
231 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
232 phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
235 /* Throw away any unused bits remaining in bit buffer; */
236 /* include any full bytes in next_marker's count of discarded bytes */
237 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
238 entropy->bitstate.bits_left = 0;
240 /* Advance past the RSTn marker */
241 if (! (*cinfo->marker->read_restart_marker) (cinfo))
244 /* Re-initialize DC predictions to 0 */
245 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
246 entropy->saved.last_dc_val[ci] = 0;
247 /* Re-init EOB run count, too */
248 entropy->saved.EOBRUN = 0;
250 /* Reset restart counter */
251 entropy->restarts_to_go = cinfo->restart_interval;
253 /* Reset out-of-data flag, unless read_restart_marker left us smack up
254 * against a marker. In that case we will end up treating the next data
255 * segment as empty, and we can avoid producing bogus output pixels by
256 * leaving the flag set.
258 if (cinfo->unread_marker == 0)
259 entropy->insufficient_data = FALSE;
266 * Huffman MCU decoding.
267 * Each of these routines decodes and returns one MCU's worth of
268 * Huffman-compressed coefficients.
269 * The coefficients are reordered from zigzag order into natural array order,
270 * but are not dequantized.
272 * The i'th block of the MCU is stored into the block pointed to by
273 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
275 * We return FALSE if data source requested suspension. In that case no
276 * changes have been made to permanent state. (Exception: some output
277 * coefficients may already have been assigned. This is harmless for
278 * spectral selection, since we'll just re-assign them on the next call.
279 * Successive approximation AC refinement has to be more careful, however.)
283 * MCU decoding for DC initial scan (either spectral selection,
284 * or first pass of successive approximation).
288 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
290 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
291 phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
299 jpeg_component_info * compptr;
301 /* Process restart marker if needed; may have to suspend */
302 if (cinfo->restart_interval) {
303 if (entropy->restarts_to_go == 0)
304 if (! process_restart(cinfo))
308 /* If we've run out of data, just leave the MCU set to zeroes.
309 * This way, we return uniform gray for the remainder of the segment.
311 if (! entropy->insufficient_data) {
313 /* Load up working state */
314 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
315 ASSIGN_STATE(state, entropy->saved);
317 /* Outer loop handles each block in the MCU */
319 for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
320 block = MCU_data[blkn];
321 ci = cinfo->MCU_membership[blkn];
322 compptr = cinfo->cur_comp_info[ci];
323 tbl = entropy->derived_tbls[compptr->dc_tbl_no];
325 /* Decode a single block's worth of coefficients */
327 /* Section F.2.2.1: decode the DC coefficient difference */
328 HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
330 CHECK_BIT_BUFFER(br_state, s, return FALSE);
332 s = HUFF_EXTEND(r, s);
335 /* Convert DC difference to actual value, update last_dc_val */
336 s += state.last_dc_val[ci];
337 state.last_dc_val[ci] = s;
338 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
339 (*block)[0] = (JCOEF) (s << Al);
342 /* Completed MCU, so update state */
343 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
344 ASSIGN_STATE(entropy->saved, state);
347 /* Account for restart interval (no-op if not using restarts) */
348 entropy->restarts_to_go--;
355 * MCU decoding for AC initial scan (either spectral selection,
356 * or first pass of successive approximation).
360 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
362 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
363 phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
366 register int s, k, r;
372 /* Process restart marker if needed; may have to suspend */
373 if (cinfo->restart_interval) {
374 if (entropy->restarts_to_go == 0)
375 if (! process_restart(cinfo))
379 /* If we've run out of data, just leave the MCU set to zeroes.
380 * This way, we return uniform gray for the remainder of the segment.
382 if (! entropy->insufficient_data) {
384 /* Load up working state.
385 * We can avoid loading/saving bitread state if in an EOB run.
387 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
389 /* There is always only one block per MCU */
391 if (EOBRUN > 0) /* if it's a band of zeroes... */
392 EOBRUN--; /* ...process it now (we do nothing) */
394 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
396 tbl = entropy->ac_derived_tbl;
398 for (k = cinfo->Ss; k <= Se; k++) {
399 HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
404 CHECK_BIT_BUFFER(br_state, s, return FALSE);
406 s = HUFF_EXTEND(r, s);
407 /* Scale and output coefficient in natural (dezigzagged) order */
408 (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
410 if (r == 15) { /* ZRL */
411 k += 15; /* skip 15 zeroes in band */
412 } else { /* EOBr, run length is 2^r + appended bits */
414 if (r) { /* EOBr, r > 0 */
415 CHECK_BIT_BUFFER(br_state, r, return FALSE);
419 EOBRUN--; /* this band is processed at this moment */
420 break; /* force end-of-band */
425 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
428 /* Completed MCU, so update state */
429 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
432 /* Account for restart interval (no-op if not using restarts) */
433 entropy->restarts_to_go--;
440 * MCU decoding for DC successive approximation refinement scan.
441 * Note: we assume such scans can be multi-component, although the spec
442 * is not very clear on the point.
446 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
448 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
449 phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
450 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
455 /* Process restart marker if needed; may have to suspend */
456 if (cinfo->restart_interval) {
457 if (entropy->restarts_to_go == 0)
458 if (! process_restart(cinfo))
462 /* Not worth the cycles to check insufficient_data here,
463 * since we will not change the data anyway if we read zeroes.
466 /* Load up working state */
467 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
469 /* Outer loop handles each block in the MCU */
471 for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
472 block = MCU_data[blkn];
474 /* Encoded data is simply the next bit of the two's-complement DC value */
475 CHECK_BIT_BUFFER(br_state, 1, return FALSE);
478 /* Note: since we use |=, repeating the assignment later is safe */
481 /* Completed MCU, so update state */
482 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
484 /* Account for restart interval (no-op if not using restarts) */
485 entropy->restarts_to_go--;
492 * MCU decoding for AC successive approximation refinement scan.
496 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
498 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
499 phuff_entropy_ptr entropy = (phuff_entropy_ptr) lossyd->entropy_private;
501 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
502 int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
503 register int s, k, r;
510 int newnz_pos[DCTSIZE2];
512 /* Process restart marker if needed; may have to suspend */
513 if (cinfo->restart_interval) {
514 if (entropy->restarts_to_go == 0)
515 if (! process_restart(cinfo))
519 /* If we've run out of data, don't modify the MCU.
521 if (! entropy->insufficient_data) {
523 /* Load up working state */
524 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
525 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
527 /* There is always only one block per MCU */
529 tbl = entropy->ac_derived_tbl;
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.
539 /* initialize coefficient loop counter to start of band */
543 for (; k <= Se; k++) {
544 HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
548 if (s != 1) /* size of new coef should always be 1 */
549 WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
550 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
552 s = p1; /* newly nonzero coef is positive */
554 s = m1; /* newly nonzero coef is negative */
557 EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
559 CHECK_BIT_BUFFER(br_state, r, goto undoit);
563 break; /* rest of block is handled by EOB logic */
565 /* note s = 0 for processing ZRL */
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.
572 thiscoef = *block + jpeg_natural_order[k];
573 if (*thiscoef != 0) {
574 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
576 if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
585 break; /* reached target zero coefficient */
590 int pos = jpeg_natural_order[k];
591 /* Output newly nonzero coefficient */
592 (*block)[pos] = (JCOEF) s;
593 /* Remember its position in case we have to suspend */
594 newnz_pos[num_newnz++] = pos;
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.
605 for (; k <= Se; k++) {
606 thiscoef = *block + jpeg_natural_order[k];
607 if (*thiscoef != 0) {
608 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
610 if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
619 /* Count one block completed in EOB run */
623 /* Completed MCU, so update state */
624 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
625 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
628 /* Account for restart interval (no-op if not using restarts) */
629 entropy->restarts_to_go--;
634 /* Re-zero any output coefficients that we made newly nonzero */
635 while (num_newnz > 0)
636 (*block)[newnz_pos[--num_newnz]] = 0;
643 * Module initialization routine for progressive Huffman entropy decoding.
647 jinit_phuff_decoder (j_decompress_ptr cinfo)
649 j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
650 phuff_entropy_ptr entropy;
654 entropy = (phuff_entropy_ptr)
655 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
656 SIZEOF(phuff_entropy_decoder));
657 lossyd->entropy_private = (void *) entropy;
658 lossyd->entropy_start_pass = start_pass_phuff_decoder;
660 /* Mark derived tables unallocated */
661 for (i = 0; i < NUM_HUFF_TBLS; i++) {
662 entropy->derived_tbls[i] = NULL;
665 /* Create progression status table */
666 cinfo->coef_bits = (int (*)[DCTSIZE2])
667 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
668 cinfo->num_components*DCTSIZE2*SIZEOF(int));
669 coef_bit_ptr = & cinfo->coef_bits[0][0];
670 for (ci = 0; ci < cinfo->num_components; ci++)
671 for (i = 0; i < DCTSIZE2; i++)
672 *coef_bit_ptr++ = -1;
675 #endif /* D_PROGRESSIVE_SUPPORTED */