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