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