michael@0: /* michael@0: * jdphuff.c michael@0: * michael@0: * Copyright (C) 1995-1997, Thomas G. Lane. michael@0: * This file is part of the Independent JPEG Group's software. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains Huffman entropy decoding routines for progressive JPEG. michael@0: * michael@0: * Much of the complexity here has to do with supporting input suspension. michael@0: * If the data source module demands suspension, we want to be able to back michael@0: * up to the start of the current MCU. To do this, we copy state variables michael@0: * into local working storage, and update them back to the permanent michael@0: * storage only upon successful completion of an MCU. michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: #include "jdhuff.h" /* Declarations shared with jdhuff.c */ michael@0: michael@0: michael@0: #ifdef D_PROGRESSIVE_SUPPORTED michael@0: michael@0: /* michael@0: * Expanded entropy decoder object for progressive Huffman decoding. michael@0: * michael@0: * The savable_state subrecord contains fields that change within an MCU, michael@0: * but must not be updated permanently until we complete the MCU. michael@0: */ michael@0: michael@0: typedef struct { michael@0: unsigned int EOBRUN; /* remaining EOBs in EOBRUN */ michael@0: int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ michael@0: } savable_state; michael@0: michael@0: /* This macro is to work around compilers with missing or broken michael@0: * structure assignment. You'll need to fix this code if you have michael@0: * such a compiler and you change MAX_COMPS_IN_SCAN. michael@0: */ michael@0: michael@0: #ifndef NO_STRUCT_ASSIGN michael@0: #define ASSIGN_STATE(dest,src) ((dest) = (src)) michael@0: #else michael@0: #if MAX_COMPS_IN_SCAN == 4 michael@0: #define ASSIGN_STATE(dest,src) \ michael@0: ((dest).EOBRUN = (src).EOBRUN, \ michael@0: (dest).last_dc_val[0] = (src).last_dc_val[0], \ michael@0: (dest).last_dc_val[1] = (src).last_dc_val[1], \ michael@0: (dest).last_dc_val[2] = (src).last_dc_val[2], \ michael@0: (dest).last_dc_val[3] = (src).last_dc_val[3]) michael@0: #endif michael@0: #endif michael@0: michael@0: michael@0: typedef struct { michael@0: struct jpeg_entropy_decoder pub; /* public fields */ michael@0: michael@0: /* These fields are loaded into local variables at start of each MCU. michael@0: * In case of suspension, we exit WITHOUT updating them. michael@0: */ michael@0: bitread_perm_state bitstate; /* Bit buffer at start of MCU */ michael@0: savable_state saved; /* Other state at start of MCU */ michael@0: michael@0: /* These fields are NOT loaded into local working state. */ michael@0: unsigned int restarts_to_go; /* MCUs left in this restart interval */ michael@0: michael@0: /* Pointers to derived tables (these workspaces have image lifespan) */ michael@0: d_derived_tbl * derived_tbls[NUM_HUFF_TBLS]; michael@0: michael@0: d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */ michael@0: } phuff_entropy_decoder; michael@0: michael@0: typedef phuff_entropy_decoder * phuff_entropy_ptr; michael@0: michael@0: /* Forward declarations */ michael@0: METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo, michael@0: JBLOCKROW *MCU_data)); michael@0: METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo, michael@0: JBLOCKROW *MCU_data)); michael@0: METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo, michael@0: JBLOCKROW *MCU_data)); michael@0: METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo, michael@0: JBLOCKROW *MCU_data)); michael@0: michael@0: michael@0: /* michael@0: * Initialize for a Huffman-compressed scan. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: start_pass_phuff_decoder (j_decompress_ptr cinfo) michael@0: { michael@0: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; michael@0: boolean is_DC_band, bad; michael@0: int ci, coefi, tbl; michael@0: int *coef_bit_ptr; michael@0: jpeg_component_info * compptr; michael@0: michael@0: is_DC_band = (cinfo->Ss == 0); michael@0: michael@0: /* Validate scan parameters */ michael@0: bad = FALSE; michael@0: if (is_DC_band) { michael@0: if (cinfo->Se != 0) michael@0: bad = TRUE; michael@0: } else { michael@0: /* need not check Ss/Se < 0 since they came from unsigned bytes */ michael@0: if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2) michael@0: bad = TRUE; michael@0: /* AC scans may have only one component */ michael@0: if (cinfo->comps_in_scan != 1) michael@0: bad = TRUE; michael@0: } michael@0: if (cinfo->Ah != 0) { michael@0: /* Successive approximation refinement scan: must have Al = Ah-1. */ michael@0: if (cinfo->Al != cinfo->Ah-1) michael@0: bad = TRUE; michael@0: } michael@0: if (cinfo->Al > 13) /* need not check for < 0 */ michael@0: bad = TRUE; michael@0: /* Arguably the maximum Al value should be less than 13 for 8-bit precision, michael@0: * but the spec doesn't say so, and we try to be liberal about what we michael@0: * accept. Note: large Al values could result in out-of-range DC michael@0: * coefficients during early scans, leading to bizarre displays due to michael@0: * overflows in the IDCT math. But we won't crash. michael@0: */ michael@0: if (bad) michael@0: ERREXIT4(cinfo, JERR_BAD_PROGRESSION, michael@0: cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al); michael@0: /* Update progression status, and verify that scan order is legal. michael@0: * Note that inter-scan inconsistencies are treated as warnings michael@0: * not fatal errors ... not clear if this is right way to behave. michael@0: */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: int cindex = cinfo->cur_comp_info[ci]->component_index; michael@0: coef_bit_ptr = & cinfo->coef_bits[cindex][0]; michael@0: if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */ michael@0: WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0); michael@0: for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { michael@0: int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi]; michael@0: if (cinfo->Ah != expected) michael@0: WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi); michael@0: coef_bit_ptr[coefi] = cinfo->Al; michael@0: } michael@0: } michael@0: michael@0: /* Select MCU decoding routine */ michael@0: if (cinfo->Ah == 0) { michael@0: if (is_DC_band) michael@0: entropy->pub.decode_mcu = decode_mcu_DC_first; michael@0: else michael@0: entropy->pub.decode_mcu = decode_mcu_AC_first; michael@0: } else { michael@0: if (is_DC_band) michael@0: entropy->pub.decode_mcu = decode_mcu_DC_refine; michael@0: else michael@0: entropy->pub.decode_mcu = decode_mcu_AC_refine; michael@0: } michael@0: michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: /* Make sure requested tables are present, and compute derived tables. michael@0: * We may build same derived table more than once, but it's not expensive. michael@0: */ michael@0: if (is_DC_band) { michael@0: if (cinfo->Ah == 0) { /* DC refinement needs no table */ michael@0: tbl = compptr->dc_tbl_no; michael@0: jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, michael@0: & entropy->derived_tbls[tbl]); michael@0: } michael@0: } else { michael@0: tbl = compptr->ac_tbl_no; michael@0: jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, michael@0: & entropy->derived_tbls[tbl]); michael@0: /* remember the single active table */ michael@0: entropy->ac_derived_tbl = entropy->derived_tbls[tbl]; michael@0: } michael@0: /* Initialize DC predictions to 0 */ michael@0: entropy->saved.last_dc_val[ci] = 0; michael@0: } michael@0: michael@0: /* Initialize bitread state variables */ michael@0: entropy->bitstate.bits_left = 0; michael@0: entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ michael@0: entropy->pub.insufficient_data = FALSE; michael@0: michael@0: /* Initialize private state variables */ michael@0: entropy->saved.EOBRUN = 0; michael@0: michael@0: /* Initialize restart counter */ michael@0: entropy->restarts_to_go = cinfo->restart_interval; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Figure F.12: extend sign bit. michael@0: * On some machines, a shift and add will be faster than a table lookup. michael@0: */ michael@0: michael@0: #define AVOID_TABLES michael@0: #ifdef AVOID_TABLES michael@0: michael@0: #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x)) michael@0: michael@0: #else michael@0: michael@0: #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) michael@0: michael@0: static const int extend_test[16] = /* entry n is 2**(n-1) */ michael@0: { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, michael@0: 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; michael@0: michael@0: static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ michael@0: { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, michael@0: ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, michael@0: ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, michael@0: ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 }; michael@0: michael@0: #endif /* AVOID_TABLES */ michael@0: michael@0: michael@0: /* michael@0: * Check for a restart marker & resynchronize decoder. michael@0: * Returns FALSE if must suspend. michael@0: */ michael@0: michael@0: LOCAL(boolean) michael@0: process_restart (j_decompress_ptr cinfo) michael@0: { michael@0: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; michael@0: int ci; michael@0: michael@0: /* Throw away any unused bits remaining in bit buffer; */ michael@0: /* include any full bytes in next_marker's count of discarded bytes */ michael@0: cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; michael@0: entropy->bitstate.bits_left = 0; michael@0: michael@0: /* Advance past the RSTn marker */ michael@0: if (! (*cinfo->marker->read_restart_marker) (cinfo)) michael@0: return FALSE; michael@0: michael@0: /* Re-initialize DC predictions to 0 */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) michael@0: entropy->saved.last_dc_val[ci] = 0; michael@0: /* Re-init EOB run count, too */ michael@0: entropy->saved.EOBRUN = 0; michael@0: michael@0: /* Reset restart counter */ michael@0: entropy->restarts_to_go = cinfo->restart_interval; michael@0: michael@0: /* Reset out-of-data flag, unless read_restart_marker left us smack up michael@0: * against a marker. In that case we will end up treating the next data michael@0: * segment as empty, and we can avoid producing bogus output pixels by michael@0: * leaving the flag set. michael@0: */ michael@0: if (cinfo->unread_marker == 0) michael@0: entropy->pub.insufficient_data = FALSE; michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Huffman MCU decoding. michael@0: * Each of these routines decodes and returns one MCU's worth of michael@0: * Huffman-compressed coefficients. michael@0: * The coefficients are reordered from zigzag order into natural array order, michael@0: * but are not dequantized. michael@0: * michael@0: * The i'th block of the MCU is stored into the block pointed to by michael@0: * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER. michael@0: * michael@0: * We return FALSE if data source requested suspension. In that case no michael@0: * changes have been made to permanent state. (Exception: some output michael@0: * coefficients may already have been assigned. This is harmless for michael@0: * spectral selection, since we'll just re-assign them on the next call. michael@0: * Successive approximation AC refinement has to be more careful, however.) michael@0: */ michael@0: michael@0: /* michael@0: * MCU decoding for DC initial scan (either spectral selection, michael@0: * or first pass of successive approximation). michael@0: */ michael@0: michael@0: METHODDEF(boolean) michael@0: decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; michael@0: int Al = cinfo->Al; michael@0: register int s, r; michael@0: int blkn, ci; michael@0: JBLOCKROW block; michael@0: BITREAD_STATE_VARS; michael@0: savable_state state; michael@0: d_derived_tbl * tbl; michael@0: jpeg_component_info * compptr; michael@0: michael@0: /* Process restart marker if needed; may have to suspend */ michael@0: if (cinfo->restart_interval) { michael@0: if (entropy->restarts_to_go == 0) michael@0: if (! process_restart(cinfo)) michael@0: return FALSE; michael@0: } michael@0: michael@0: /* If we've run out of data, just leave the MCU set to zeroes. michael@0: * This way, we return uniform gray for the remainder of the segment. michael@0: */ michael@0: if (! entropy->pub.insufficient_data) { michael@0: michael@0: /* Load up working state */ michael@0: BITREAD_LOAD_STATE(cinfo,entropy->bitstate); michael@0: ASSIGN_STATE(state, entropy->saved); michael@0: michael@0: /* Outer loop handles each block in the MCU */ michael@0: michael@0: for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { michael@0: block = MCU_data[blkn]; michael@0: ci = cinfo->MCU_membership[blkn]; michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: tbl = entropy->derived_tbls[compptr->dc_tbl_no]; michael@0: michael@0: /* Decode a single block's worth of coefficients */ michael@0: michael@0: /* Section F.2.2.1: decode the DC coefficient difference */ michael@0: HUFF_DECODE(s, br_state, tbl, return FALSE, label1); michael@0: if (s) { michael@0: CHECK_BIT_BUFFER(br_state, s, return FALSE); michael@0: r = GET_BITS(s); michael@0: s = HUFF_EXTEND(r, s); michael@0: } michael@0: michael@0: /* Convert DC difference to actual value, update last_dc_val */ michael@0: s += state.last_dc_val[ci]; michael@0: state.last_dc_val[ci] = s; michael@0: /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */ michael@0: (*block)[0] = (JCOEF) (s << Al); michael@0: } michael@0: michael@0: /* Completed MCU, so update state */ michael@0: BITREAD_SAVE_STATE(cinfo,entropy->bitstate); michael@0: ASSIGN_STATE(entropy->saved, state); michael@0: } michael@0: michael@0: /* Account for restart interval (no-op if not using restarts) */ michael@0: entropy->restarts_to_go--; michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * MCU decoding for AC initial scan (either spectral selection, michael@0: * or first pass of successive approximation). michael@0: */ michael@0: michael@0: METHODDEF(boolean) michael@0: decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; michael@0: int Se = cinfo->Se; michael@0: int Al = cinfo->Al; michael@0: register int s, k, r; michael@0: unsigned int EOBRUN; michael@0: JBLOCKROW block; michael@0: BITREAD_STATE_VARS; michael@0: d_derived_tbl * tbl; michael@0: michael@0: /* Process restart marker if needed; may have to suspend */ michael@0: if (cinfo->restart_interval) { michael@0: if (entropy->restarts_to_go == 0) michael@0: if (! process_restart(cinfo)) michael@0: return FALSE; michael@0: } michael@0: michael@0: /* If we've run out of data, just leave the MCU set to zeroes. michael@0: * This way, we return uniform gray for the remainder of the segment. michael@0: */ michael@0: if (! entropy->pub.insufficient_data) { michael@0: michael@0: /* Load up working state. michael@0: * We can avoid loading/saving bitread state if in an EOB run. michael@0: */ michael@0: EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ michael@0: michael@0: /* There is always only one block per MCU */ michael@0: michael@0: if (EOBRUN > 0) /* if it's a band of zeroes... */ michael@0: EOBRUN--; /* ...process it now (we do nothing) */ michael@0: else { michael@0: BITREAD_LOAD_STATE(cinfo,entropy->bitstate); michael@0: block = MCU_data[0]; michael@0: tbl = entropy->ac_derived_tbl; michael@0: michael@0: for (k = cinfo->Ss; k <= Se; k++) { michael@0: HUFF_DECODE(s, br_state, tbl, return FALSE, label2); michael@0: r = s >> 4; michael@0: s &= 15; michael@0: if (s) { michael@0: k += r; michael@0: CHECK_BIT_BUFFER(br_state, s, return FALSE); michael@0: r = GET_BITS(s); michael@0: s = HUFF_EXTEND(r, s); michael@0: /* Scale and output coefficient in natural (dezigzagged) order */ michael@0: (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al); michael@0: } else { michael@0: if (r == 15) { /* ZRL */ michael@0: k += 15; /* skip 15 zeroes in band */ michael@0: } else { /* EOBr, run length is 2^r + appended bits */ michael@0: EOBRUN = 1 << r; michael@0: if (r) { /* EOBr, r > 0 */ michael@0: CHECK_BIT_BUFFER(br_state, r, return FALSE); michael@0: r = GET_BITS(r); michael@0: EOBRUN += r; michael@0: } michael@0: EOBRUN--; /* this band is processed at this moment */ michael@0: break; /* force end-of-band */ michael@0: } michael@0: } michael@0: } michael@0: michael@0: BITREAD_SAVE_STATE(cinfo,entropy->bitstate); michael@0: } michael@0: michael@0: /* Completed MCU, so update state */ michael@0: entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ michael@0: } michael@0: michael@0: /* Account for restart interval (no-op if not using restarts) */ michael@0: entropy->restarts_to_go--; michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * MCU decoding for DC successive approximation refinement scan. michael@0: * Note: we assume such scans can be multi-component, although the spec michael@0: * is not very clear on the point. michael@0: */ michael@0: michael@0: METHODDEF(boolean) michael@0: decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; michael@0: int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ michael@0: int blkn; michael@0: JBLOCKROW block; michael@0: BITREAD_STATE_VARS; michael@0: michael@0: /* Process restart marker if needed; may have to suspend */ michael@0: if (cinfo->restart_interval) { michael@0: if (entropy->restarts_to_go == 0) michael@0: if (! process_restart(cinfo)) michael@0: return FALSE; michael@0: } michael@0: michael@0: /* Not worth the cycles to check insufficient_data here, michael@0: * since we will not change the data anyway if we read zeroes. michael@0: */ michael@0: michael@0: /* Load up working state */ michael@0: BITREAD_LOAD_STATE(cinfo,entropy->bitstate); michael@0: michael@0: /* Outer loop handles each block in the MCU */ michael@0: michael@0: for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { michael@0: block = MCU_data[blkn]; michael@0: michael@0: /* Encoded data is simply the next bit of the two's-complement DC value */ michael@0: CHECK_BIT_BUFFER(br_state, 1, return FALSE); michael@0: if (GET_BITS(1)) michael@0: (*block)[0] |= p1; michael@0: /* Note: since we use |=, repeating the assignment later is safe */ michael@0: } michael@0: michael@0: /* Completed MCU, so update state */ michael@0: BITREAD_SAVE_STATE(cinfo,entropy->bitstate); michael@0: michael@0: /* Account for restart interval (no-op if not using restarts) */ michael@0: entropy->restarts_to_go--; michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * MCU decoding for AC successive approximation refinement scan. michael@0: */ michael@0: michael@0: METHODDEF(boolean) michael@0: decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy; michael@0: int Se = cinfo->Se; michael@0: int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */ michael@0: int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */ michael@0: register int s, k, r; michael@0: unsigned int EOBRUN; michael@0: JBLOCKROW block; michael@0: JCOEFPTR thiscoef; michael@0: BITREAD_STATE_VARS; michael@0: d_derived_tbl * tbl; michael@0: int num_newnz; michael@0: int newnz_pos[DCTSIZE2]; michael@0: michael@0: /* Process restart marker if needed; may have to suspend */ michael@0: if (cinfo->restart_interval) { michael@0: if (entropy->restarts_to_go == 0) michael@0: if (! process_restart(cinfo)) michael@0: return FALSE; michael@0: } michael@0: michael@0: /* If we've run out of data, don't modify the MCU. michael@0: */ michael@0: if (! entropy->pub.insufficient_data) { michael@0: michael@0: /* Load up working state */ michael@0: BITREAD_LOAD_STATE(cinfo,entropy->bitstate); michael@0: EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */ michael@0: michael@0: /* There is always only one block per MCU */ michael@0: block = MCU_data[0]; michael@0: tbl = entropy->ac_derived_tbl; michael@0: michael@0: /* If we are forced to suspend, we must undo the assignments to any newly michael@0: * nonzero coefficients in the block, because otherwise we'd get confused michael@0: * next time about which coefficients were already nonzero. michael@0: * But we need not undo addition of bits to already-nonzero coefficients; michael@0: * instead, we can test the current bit to see if we already did it. michael@0: */ michael@0: num_newnz = 0; michael@0: michael@0: /* initialize coefficient loop counter to start of band */ michael@0: k = cinfo->Ss; michael@0: michael@0: if (EOBRUN == 0) { michael@0: for (; k <= Se; k++) { michael@0: HUFF_DECODE(s, br_state, tbl, goto undoit, label3); michael@0: r = s >> 4; michael@0: s &= 15; michael@0: if (s) { michael@0: if (s != 1) /* size of new coef should always be 1 */ michael@0: WARNMS(cinfo, JWRN_HUFF_BAD_CODE); michael@0: CHECK_BIT_BUFFER(br_state, 1, goto undoit); michael@0: if (GET_BITS(1)) michael@0: s = p1; /* newly nonzero coef is positive */ michael@0: else michael@0: s = m1; /* newly nonzero coef is negative */ michael@0: } else { michael@0: if (r != 15) { michael@0: EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */ michael@0: if (r) { michael@0: CHECK_BIT_BUFFER(br_state, r, goto undoit); michael@0: r = GET_BITS(r); michael@0: EOBRUN += r; michael@0: } michael@0: break; /* rest of block is handled by EOB logic */ michael@0: } michael@0: /* note s = 0 for processing ZRL */ michael@0: } michael@0: /* Advance over already-nonzero coefs and r still-zero coefs, michael@0: * appending correction bits to the nonzeroes. A correction bit is 1 michael@0: * if the absolute value of the coefficient must be increased. michael@0: */ michael@0: do { michael@0: thiscoef = *block + jpeg_natural_order[k]; michael@0: if (*thiscoef != 0) { michael@0: CHECK_BIT_BUFFER(br_state, 1, goto undoit); michael@0: if (GET_BITS(1)) { michael@0: if ((*thiscoef & p1) == 0) { /* do nothing if already set it */ michael@0: if (*thiscoef >= 0) michael@0: *thiscoef += p1; michael@0: else michael@0: *thiscoef += m1; michael@0: } michael@0: } michael@0: } else { michael@0: if (--r < 0) michael@0: break; /* reached target zero coefficient */ michael@0: } michael@0: k++; michael@0: } while (k <= Se); michael@0: if (s) { michael@0: int pos = jpeg_natural_order[k]; michael@0: /* Output newly nonzero coefficient */ michael@0: (*block)[pos] = (JCOEF) s; michael@0: /* Remember its position in case we have to suspend */ michael@0: newnz_pos[num_newnz++] = pos; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (EOBRUN > 0) { michael@0: /* Scan any remaining coefficient positions after the end-of-band michael@0: * (the last newly nonzero coefficient, if any). Append a correction michael@0: * bit to each already-nonzero coefficient. A correction bit is 1 michael@0: * if the absolute value of the coefficient must be increased. michael@0: */ michael@0: for (; k <= Se; k++) { michael@0: thiscoef = *block + jpeg_natural_order[k]; michael@0: if (*thiscoef != 0) { michael@0: CHECK_BIT_BUFFER(br_state, 1, goto undoit); michael@0: if (GET_BITS(1)) { michael@0: if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */ michael@0: if (*thiscoef >= 0) michael@0: *thiscoef += p1; michael@0: else michael@0: *thiscoef += m1; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: /* Count one block completed in EOB run */ michael@0: EOBRUN--; michael@0: } michael@0: michael@0: /* Completed MCU, so update state */ michael@0: BITREAD_SAVE_STATE(cinfo,entropy->bitstate); michael@0: entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */ michael@0: } michael@0: michael@0: /* Account for restart interval (no-op if not using restarts) */ michael@0: entropy->restarts_to_go--; michael@0: michael@0: return TRUE; michael@0: michael@0: undoit: michael@0: /* Re-zero any output coefficients that we made newly nonzero */ michael@0: while (num_newnz > 0) michael@0: (*block)[newnz_pos[--num_newnz]] = 0; michael@0: michael@0: return FALSE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Module initialization routine for progressive Huffman entropy decoding. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_phuff_decoder (j_decompress_ptr cinfo) michael@0: { michael@0: phuff_entropy_ptr entropy; michael@0: int *coef_bit_ptr; michael@0: int ci, i; michael@0: michael@0: entropy = (phuff_entropy_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(phuff_entropy_decoder)); michael@0: cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; michael@0: entropy->pub.start_pass = start_pass_phuff_decoder; michael@0: michael@0: /* Mark derived tables unallocated */ michael@0: for (i = 0; i < NUM_HUFF_TBLS; i++) { michael@0: entropy->derived_tbls[i] = NULL; michael@0: } michael@0: michael@0: /* Create progression status table */ michael@0: cinfo->coef_bits = (int (*)[DCTSIZE2]) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: cinfo->num_components*DCTSIZE2*SIZEOF(int)); michael@0: coef_bit_ptr = & cinfo->coef_bits[0][0]; michael@0: for (ci = 0; ci < cinfo->num_components; ci++) michael@0: for (i = 0; i < DCTSIZE2; i++) michael@0: *coef_bit_ptr++ = -1; michael@0: } michael@0: michael@0: #endif /* D_PROGRESSIVE_SUPPORTED */