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