0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * jdhuff.c
0N/A *
0N/A * Copyright (C) 1991-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.
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 jdphuff.c */
0N/A
0N/A
0N/A/*
0N/A * Expanded entropy decoder object for 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 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).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 * dc_derived_tbls[NUM_HUFF_TBLS];
0N/A d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
0N/A
0N/A /* Precalculated info set up by start_pass for use in decode_mcu: */
0N/A
0N/A /* Pointers to derived tables to be used for each block within an MCU */
0N/A d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
0N/A d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
0N/A /* Whether we care about the DC and AC coefficient values for each block */
0N/A boolean dc_needed[D_MAX_BLOCKS_IN_MCU];
0N/A boolean ac_needed[D_MAX_BLOCKS_IN_MCU];
0N/A} huff_entropy_decoder;
0N/A
0N/Atypedef huff_entropy_decoder * huff_entropy_ptr;
0N/A
0N/A
0N/A/*
0N/A * Initialize for a Huffman-compressed scan.
0N/A */
0N/A
0N/AMETHODDEF(void)
0N/Astart_pass_huff_decoder (j_decompress_ptr cinfo)
0N/A{
0N/A huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
0N/A int ci, blkn, dctbl, actbl;
0N/A jpeg_component_info * compptr;
0N/A
0N/A /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
0N/A * This ought to be an error condition, but we make it a warning because
0N/A * there are some baseline files out there with all zeroes in these bytes.
0N/A */
0N/A if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
0N/A cinfo->Ah != 0 || cinfo->Al != 0)
0N/A WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
0N/A
0N/A for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
0N/A compptr = cinfo->cur_comp_info[ci];
0N/A dctbl = compptr->dc_tbl_no;
0N/A actbl = compptr->ac_tbl_no;
0N/A /* Compute derived values for Huffman tables */
0N/A /* We may do this more than once for a table, but it's not expensive */
0N/A jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
0N/A & entropy->dc_derived_tbls[dctbl]);
0N/A jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
0N/A & entropy->ac_derived_tbls[actbl]);
0N/A /* Initialize DC predictions to 0 */
0N/A entropy->saved.last_dc_val[ci] = 0;
0N/A }
0N/A
0N/A /* Precalculate decoding info for each block in an MCU of this scan */
0N/A for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
0N/A ci = cinfo->MCU_membership[blkn];
0N/A compptr = cinfo->cur_comp_info[ci];
0N/A /* Precalculate which table to use for each block */
0N/A entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
0N/A entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
0N/A /* Decide whether we really care about the coefficient values */
0N/A if (compptr->component_needed) {
0N/A entropy->dc_needed[blkn] = TRUE;
0N/A /* we don't need the ACs if producing a 1/8th-size image */
0N/A entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1);
0N/A } else {
0N/A entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
0N/A }
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 restart counter */
0N/A entropy->restarts_to_go = cinfo->restart_interval;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Compute the derived values for a Huffman table.
0N/A * This routine also performs some validation checks on the table.
0N/A *
0N/A * Note this is also used by jdphuff.c.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
0N/A d_derived_tbl ** pdtbl)
0N/A{
0N/A JHUFF_TBL *htbl;
0N/A d_derived_tbl *dtbl;
0N/A int p, i, l, si, numsymbols;
0N/A int lookbits, ctr;
0N/A char huffsize[257];
0N/A unsigned int huffcode[257];
0N/A unsigned int code;
0N/A
0N/A /* Note that huffsize[] and huffcode[] are filled in code-length order,
0N/A * paralleling the order of the symbols themselves in htbl->huffval[].
0N/A */
0N/A
0N/A /* Find the input Huffman table */
0N/A if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
0N/A ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
0N/A htbl =
0N/A isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
0N/A if (htbl == NULL)
0N/A ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
0N/A
0N/A /* Allocate a workspace if we haven't already done so. */
0N/A if (*pdtbl == NULL)
0N/A *pdtbl = (d_derived_tbl *)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(d_derived_tbl));
0N/A dtbl = *pdtbl;
0N/A dtbl->pub = htbl; /* fill in back link */
0N/A
0N/A /* Figure C.1: make table of Huffman code length for each symbol */
0N/A
0N/A p = 0;
0N/A for (l = 1; l <= 16; l++) {
0N/A i = (int) htbl->bits[l];
0N/A if (i < 0 || p + i > 256) /* protect against table overrun */
0N/A ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
0N/A while (i--)
0N/A huffsize[p++] = (char) l;
0N/A }
0N/A huffsize[p] = 0;
0N/A numsymbols = p;
0N/A
0N/A /* Figure C.2: generate the codes themselves */
0N/A /* We also validate that the counts represent a legal Huffman code tree. */
0N/A
0N/A code = 0;
0N/A si = huffsize[0];
0N/A p = 0;
0N/A while (huffsize[p]) {
0N/A while (((int) huffsize[p]) == si) {
0N/A huffcode[p++] = code;
0N/A code++;
0N/A }
0N/A /* code is now 1 more than the last code used for codelength si; but
0N/A * it must still fit in si bits, since no code is allowed to be all ones.
0N/A */
0N/A if (((INT32) code) >= (((INT32) 1) << si))
0N/A ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
0N/A code <<= 1;
0N/A si++;
0N/A }
0N/A
0N/A /* Figure F.15: generate decoding tables for bit-sequential decoding */
0N/A
0N/A p = 0;
0N/A for (l = 1; l <= 16; l++) {
0N/A if (htbl->bits[l]) {
0N/A /* valoffset[l] = huffval[] index of 1st symbol of code length l,
0N/A * minus the minimum code of length l
0N/A */
0N/A dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
0N/A p += htbl->bits[l];
0N/A dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
0N/A } else {
0N/A dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
0N/A }
0N/A }
0N/A dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
0N/A
0N/A /* Compute lookahead tables to speed up decoding.
0N/A * First we set all the table entries to 0, indicating "too long";
0N/A * then we iterate through the Huffman codes that are short enough and
0N/A * fill in all the entries that correspond to bit sequences starting
0N/A * with that code.
0N/A */
0N/A
0N/A MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
0N/A
0N/A p = 0;
0N/A for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
0N/A for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
0N/A /* l = current code's length, p = its index in huffcode[] & huffval[]. */
0N/A /* Generate left-justified code followed by all possible bit sequences */
0N/A lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
0N/A for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
0N/A dtbl->look_nbits[lookbits] = l;
0N/A dtbl->look_sym[lookbits] = htbl->huffval[p];
0N/A lookbits++;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /* Validate symbols as being reasonable.
0N/A * For AC tables, we make no check, but accept all byte values 0..255.
0N/A * For DC tables, we require the symbols to be in range 0..15.
0N/A * (Tighter bounds could be applied depending on the data depth and mode,
0N/A * but this is sufficient to ensure safe decoding.)
0N/A */
0N/A if (isDC) {
0N/A for (i = 0; i < numsymbols; i++) {
0N/A int sym = htbl->huffval[i];
0N/A if (sym < 0 || sym > 15)
0N/A ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Out-of-line code for bit fetching (shared with jdphuff.c).
0N/A * See jdhuff.h for info about usage.
0N/A * Note: current values of get_buffer and bits_left are passed as parameters,
0N/A * but are returned in the corresponding fields of the state struct.
0N/A *
0N/A * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
0N/A * of get_buffer to be used. (On machines with wider words, an even larger
0N/A * buffer could be used.) However, on some machines 32-bit shifts are
0N/A * quite slow and take time proportional to the number of places shifted.
0N/A * (This is true with most PC compilers, for instance.) In this case it may
0N/A * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
0N/A * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
0N/A */
0N/A
0N/A#ifdef SLOW_SHIFT_32
0N/A#define MIN_GET_BITS 15 /* minimum allowable value */
0N/A#else
0N/A#define MIN_GET_BITS (BIT_BUF_SIZE-7)
0N/A#endif
0N/A
0N/A
0N/AGLOBAL(boolean)
0N/Ajpeg_fill_bit_buffer (bitread_working_state * state,
0N/A register bit_buf_type get_buffer, register int bits_left,
0N/A int nbits)
0N/A/* Load up the bit buffer to a depth of at least nbits */
0N/A{
0N/A /* Copy heavily used state fields into locals (hopefully registers) */
0N/A register const JOCTET * next_input_byte = state->next_input_byte;
0N/A register size_t bytes_in_buffer = state->bytes_in_buffer;
0N/A j_decompress_ptr cinfo = state->cinfo;
0N/A
0N/A /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
0N/A /* (It is assumed that no request will be for more than that many bits.) */
0N/A /* We fail to do so only if we hit a marker or are forced to suspend. */
0N/A
0N/A if (cinfo->unread_marker == 0) { /* cannot advance past a marker */
0N/A while (bits_left < MIN_GET_BITS) {
0N/A register int c;
0N/A
0N/A /* Attempt to read a byte */
0N/A if (bytes_in_buffer == 0) {
0N/A if (! (*cinfo->src->fill_input_buffer) (cinfo))
0N/A return FALSE;
0N/A next_input_byte = cinfo->src->next_input_byte;
0N/A bytes_in_buffer = cinfo->src->bytes_in_buffer;
0N/A }
0N/A bytes_in_buffer--;
0N/A c = GETJOCTET(*next_input_byte++);
0N/A
0N/A /* If it's 0xFF, check and discard stuffed zero byte */
0N/A if (c == 0xFF) {
0N/A /* Loop here to discard any padding FF's on terminating marker,
0N/A * so that we can save a valid unread_marker value. NOTE: we will
0N/A * accept multiple FF's followed by a 0 as meaning a single FF data
0N/A * byte. This data pattern is not valid according to the standard.
0N/A */
0N/A do {
0N/A if (bytes_in_buffer == 0) {
0N/A if (! (*cinfo->src->fill_input_buffer) (cinfo))
0N/A return FALSE;
0N/A next_input_byte = cinfo->src->next_input_byte;
0N/A bytes_in_buffer = cinfo->src->bytes_in_buffer;
0N/A }
0N/A bytes_in_buffer--;
0N/A c = GETJOCTET(*next_input_byte++);
0N/A } while (c == 0xFF);
0N/A
0N/A if (c == 0) {
0N/A /* Found FF/00, which represents an FF data byte */
0N/A c = 0xFF;
0N/A } else {
0N/A /* Oops, it's actually a marker indicating end of compressed data.
0N/A * Save the marker code for later use.
0N/A * Fine point: it might appear that we should save the marker into
0N/A * bitread working state, not straight into permanent state. But
0N/A * once we have hit a marker, we cannot need to suspend within the
0N/A * current MCU, because we will read no more bytes from the data
0N/A * source. So it is OK to update permanent state right away.
0N/A */
0N/A cinfo->unread_marker = c;
0N/A /* See if we need to insert some fake zero bits. */
0N/A goto no_more_bytes;
0N/A }
0N/A }
0N/A
0N/A /* OK, load c into get_buffer */
0N/A get_buffer = (get_buffer << 8) | c;
0N/A bits_left += 8;
0N/A } /* end while */
0N/A } else {
0N/A no_more_bytes:
0N/A /* We get here if we've read the marker that terminates the compressed
0N/A * data segment. There should be enough bits in the buffer register
0N/A * to satisfy the request; if so, no problem.
0N/A */
0N/A if (nbits > bits_left) {
0N/A /* Uh-oh. Report corrupted data to user and stuff zeroes into
0N/A * the data stream, so that we can produce some kind of image.
0N/A * We use a nonvolatile flag to ensure that only one warning message
0N/A * appears per data segment.
0N/A */
0N/A if (! cinfo->entropy->insufficient_data) {
0N/A WARNMS(cinfo, JWRN_HIT_MARKER);
0N/A cinfo->entropy->insufficient_data = TRUE;
0N/A }
0N/A /* Fill the buffer with zero bits */
0N/A get_buffer <<= MIN_GET_BITS - bits_left;
0N/A bits_left = MIN_GET_BITS;
0N/A }
0N/A }
0N/A
0N/A /* Unload the local registers */
0N/A state->next_input_byte = next_input_byte;
0N/A state->bytes_in_buffer = bytes_in_buffer;
0N/A state->get_buffer = get_buffer;
0N/A state->bits_left = bits_left;
0N/A
0N/A return TRUE;
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Out-of-line code for Huffman code decoding.
0N/A * See jdhuff.h for info about usage.
0N/A */
0N/A
0N/AGLOBAL(int)
0N/Ajpeg_huff_decode (bitread_working_state * state,
0N/A register bit_buf_type get_buffer, register int bits_left,
0N/A d_derived_tbl * htbl, int min_bits)
0N/A{
0N/A register int l = min_bits;
0N/A register INT32 code;
0N/A
0N/A /* HUFF_DECODE has determined that the code is at least min_bits */
0N/A /* bits long, so fetch that many bits in one swoop. */
0N/A
0N/A CHECK_BIT_BUFFER(*state, l, return -1);
0N/A code = GET_BITS(l);
0N/A
0N/A /* Collect the rest of the Huffman code one bit at a time. */
0N/A /* This is per Figure F.16 in the JPEG spec. */
0N/A
0N/A while (code > htbl->maxcode[l]) {
0N/A code <<= 1;
0N/A CHECK_BIT_BUFFER(*state, 1, return -1);
0N/A code |= GET_BITS(1);
0N/A l++;
0N/A }
0N/A
0N/A /* Unload the local registers */
0N/A state->get_buffer = get_buffer;
0N/A state->bits_left = bits_left;
0N/A
0N/A /* With garbage input we may reach the sentinel value l = 17. */
0N/A
0N/A if (l > 16) {
0N/A WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
0N/A return 0; /* fake a zero as the safest result */
0N/A }
0N/A
0N/A return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
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 huff_entropy_ptr entropy = (huff_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
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 * Decode and return one MCU's worth of 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 HAS BEEN ZEROED BY THE CALLER.
0N/A * (Wholesale zeroing is usually a little faster than retail...)
0N/A *
0N/A * Returns 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 * this module, since we'll just re-assign them on the next call.)
0N/A */
0N/A
0N/AMETHODDEF(boolean)
0N/Adecode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
0N/A{
0N/A huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
0N/A int blkn;
0N/A BITREAD_STATE_VARS;
0N/A savable_state state;
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 JBLOCKROW block = MCU_data[blkn];
0N/A d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
0N/A d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
0N/A register int s, k, r;
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, dctbl, 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 if (entropy->dc_needed[blkn]) {
0N/A /* Convert DC difference to actual value, update last_dc_val */
0N/A int ci = cinfo->MCU_membership[blkn];
0N/A s += state.last_dc_val[ci];
0N/A state.last_dc_val[ci] = s;
0N/A /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
0N/A (*block)[0] = (JCOEF) s;
0N/A }
0N/A
0N/A if (entropy->ac_needed[blkn]) {
0N/A
0N/A /* Section F.2.2.2: decode the AC coefficients */
0N/A /* Since zeroes are skipped, output area must be cleared beforehand */
0N/A for (k = 1; k < DCTSIZE2; k++) {
0N/A HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
0N/A
0N/A r = s >> 4;
0N/A s &= 15;
0N/A
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 /* Output coefficient in natural (dezigzagged) order.
0N/A * Note: the extra entries in jpeg_natural_order[] will save us
0N/A * if k >= DCTSIZE2, which could happen if the data is corrupted.
0N/A */
0N/A (*block)[jpeg_natural_order[k]] = (JCOEF) s;
0N/A } else {
0N/A if (r != 15)
0N/A break;
0N/A k += 15;
0N/A }
0N/A }
0N/A
0N/A } else {
0N/A
0N/A /* Section F.2.2.2: decode the AC coefficients */
0N/A /* In this path we just discard the values */
0N/A for (k = 1; k < DCTSIZE2; k++) {
0N/A HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
0N/A
0N/A r = s >> 4;
0N/A s &= 15;
0N/A
0N/A if (s) {
0N/A k += r;
0N/A CHECK_BIT_BUFFER(br_state, s, return FALSE);
0N/A DROP_BITS(s);
0N/A } else {
0N/A if (r != 15)
0N/A break;
0N/A k += 15;
0N/A }
0N/A }
0N/A
0N/A }
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 * Module initialization routine for Huffman entropy decoding.
0N/A */
0N/A
0N/AGLOBAL(void)
0N/Ajinit_huff_decoder (j_decompress_ptr cinfo)
0N/A{
0N/A huff_entropy_ptr entropy;
0N/A int i;
0N/A
0N/A entropy = (huff_entropy_ptr)
0N/A (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
0N/A SIZEOF(huff_entropy_decoder));
0N/A cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
0N/A entropy->pub.start_pass = start_pass_huff_decoder;
0N/A entropy->pub.decode_mcu = decode_mcu;
0N/A
0N/A /* Mark tables unallocated */
0N/A for (i = 0; i < NUM_HUFF_TBLS; i++) {
0N/A entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
0N/A }
0N/A}