The Independent JPEG Group's JPEG software v5
diff --git a/jdhuff.c b/jdhuff.c
index 3ac46cf..e92ad9a 100644
--- a/jdhuff.c
+++ b/jdhuff.c
@@ -1,35 +1,172 @@
/*
* jdhuff.c
*
- * Copyright (C) 1991, 1992, 1993, Thomas G. Lane.
+ * Copyright (C) 1991-1994, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains Huffman entropy decoding routines.
- * These routines are invoked via the methods entropy_decode
- * and entropy_decode_init/term.
+ *
+ * Much of the complexity here has to do with supporting input suspension.
+ * If the data source module demands suspension, we want to be able to back
+ * up to the start of the current MCU. To do this, we copy state variables
+ * into local working storage, and update them back to the permanent JPEG
+ * objects only upon successful completion of an MCU.
*/
+#define JPEG_INTERNALS
#include "jinclude.h"
+#include "jpeglib.h"
-/* Static variables to avoid passing 'round extra parameters */
+/* Derived data constructed for each Huffman table */
-static decompress_info_ptr dcinfo;
+#define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */
-static INT32 get_buffer; /* current bit-extraction buffer */
-static int bits_left; /* # of unused bits in it */
-static boolean printed_eod; /* flag to suppress multiple end-of-data msgs */
+typedef struct {
+ /* Basic tables: (element [0] of each array is unused) */
+ INT32 mincode[17]; /* smallest code of length k */
+ INT32 maxcode[18]; /* largest code of length k (-1 if none) */
+ /* (maxcode[17] is a sentinel to ensure huff_DECODE terminates) */
+ int valptr[17]; /* huffval[] index of 1st symbol of length k */
+
+ /* Back link to public Huffman table (needed only in slow_DECODE) */
+ JHUFF_TBL *pub;
+
+ /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
+ * the input data stream. If the next Huffman code is no more
+ * than HUFF_LOOKAHEAD bits long, we can obtain its length and
+ * the corresponding symbol directly from these tables.
+ */
+ int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
+ UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
+} D_DERIVED_TBL;
+
+/* Expanded entropy decoder object for Huffman decoding.
+ *
+ * The savable_state subrecord contains fields that change within an MCU,
+ * but must not be updated permanently until we complete the MCU.
+ */
+
+typedef struct {
+ INT32 get_buffer; /* current bit-extraction buffer */
+ int bits_left; /* # of unused bits in it */
+ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
+} savable_state;
+
+/* This macro is to work around compilers with missing or broken
+ * structure assignment. You'll need to fix this code if you have
+ * such a compiler and you change MAX_COMPS_IN_SCAN.
+ */
+
+#ifndef NO_STRUCT_ASSIGN
+#define ASSIGN_STATE(dest,src) ((dest) = (src))
+#else
+#if MAX_COMPS_IN_SCAN == 4
+#define ASSIGN_STATE(dest,src) \
+ ((dest).get_buffer = (src).get_buffer, \
+ (dest).bits_left = (src).bits_left, \
+ (dest).last_dc_val[0] = (src).last_dc_val[0], \
+ (dest).last_dc_val[1] = (src).last_dc_val[1], \
+ (dest).last_dc_val[2] = (src).last_dc_val[2], \
+ (dest).last_dc_val[3] = (src).last_dc_val[3])
+#endif
+#endif
+
+
+typedef struct {
+ struct jpeg_entropy_decoder pub; /* public fields */
+
+ savable_state saved; /* Bit buffer & DC state at start of MCU */
+
+ /* These fields are NOT loaded into local working state. */
+ unsigned int restarts_to_go; /* MCUs left in this restart interval */
+ boolean printed_eod; /* flag to suppress extra end-of-data msgs */
+
+ /* Pointers to derived tables (these workspaces have image lifespan) */
+ D_DERIVED_TBL * dc_derived_tbls[NUM_HUFF_TBLS];
+ D_DERIVED_TBL * ac_derived_tbls[NUM_HUFF_TBLS];
+} huff_entropy_decoder;
+
+typedef huff_entropy_decoder * huff_entropy_ptr;
+
+/* Working state while scanning an MCU.
+ * This struct contains all the fields that are needed by subroutines.
+ */
+
+typedef struct {
+ int unread_marker; /* nonzero if we have hit a marker */
+ const JOCTET * next_input_byte; /* => next byte to read from source */
+ size_t bytes_in_buffer; /* # of bytes remaining in source buffer */
+ savable_state cur; /* Current bit buffer & DC state */
+ j_decompress_ptr cinfo; /* fill_bit_buffer needs access to this */
+} working_state;
+
+
+/* Forward declarations */
+LOCAL void fix_huff_tbl JPP((j_decompress_ptr cinfo, JHUFF_TBL * htbl,
+ D_DERIVED_TBL ** pdtbl));
+
+
+/*
+ * Initialize for a Huffman-compressed scan.
+ */
+
+METHODDEF void
+start_pass_huff_decoder (j_decompress_ptr cinfo)
+{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int ci, dctbl, actbl;
+ jpeg_component_info * compptr;
+
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
+ compptr = cinfo->cur_comp_info[ci];
+ dctbl = compptr->dc_tbl_no;
+ actbl = compptr->ac_tbl_no;
+ /* Make sure requested tables are present */
+ if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS ||
+ cinfo->dc_huff_tbl_ptrs[dctbl] == NULL)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
+ if (actbl < 0 || actbl >= NUM_HUFF_TBLS ||
+ cinfo->ac_huff_tbl_ptrs[actbl] == NULL)
+ ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
+ /* Compute derived values for Huffman tables */
+ /* We may do this more than once for a table, but it's not expensive */
+ fix_huff_tbl(cinfo, cinfo->dc_huff_tbl_ptrs[dctbl],
+ & entropy->dc_derived_tbls[dctbl]);
+ fix_huff_tbl(cinfo, cinfo->ac_huff_tbl_ptrs[actbl],
+ & entropy->ac_derived_tbls[actbl]);
+ /* Initialize DC predictions to 0 */
+ entropy->saved.last_dc_val[ci] = 0;
+ }
+
+ /* Initialize private state variables */
+ entropy->saved.bits_left = 0;
+ entropy->printed_eod = FALSE;
+
+ /* Initialize restart counter */
+ entropy->restarts_to_go = cinfo->restart_interval;
+}
+
LOCAL void
-fix_huff_tbl (HUFF_TBL * htbl)
-/* Compute derived values for a Huffman table */
+fix_huff_tbl (j_decompress_ptr cinfo, JHUFF_TBL * htbl, D_DERIVED_TBL ** pdtbl)
+/* Compute the derived values for a Huffman table */
{
+ D_DERIVED_TBL *dtbl;
int p, i, l, si;
int lookbits, ctr;
char huffsize[257];
- UINT16 huffcode[257];
- UINT16 code;
+ unsigned int huffcode[257];
+ unsigned int code;
+
+ /* Allocate a workspace if we haven't already done so. */
+ if (*pdtbl == NULL)
+ *pdtbl = (D_DERIVED_TBL *)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(D_DERIVED_TBL));
+ dtbl = *pdtbl;
+ dtbl->pub = htbl; /* fill in back link */
/* Figure C.1: make table of Huffman code length for each symbol */
/* Note that this is in code-length order. */
@@ -61,15 +198,15 @@
p = 0;
for (l = 1; l <= 16; l++) {
if (htbl->bits[l]) {
- htbl->priv.dec.valptr[l] = p; /* huffval[] index of 1st symbol of code length l */
- htbl->priv.dec.mincode[l] = huffcode[p]; /* minimum code of length l */
+ dtbl->valptr[l] = p; /* huffval[] index of 1st symbol of code length l */
+ dtbl->mincode[l] = huffcode[p]; /* minimum code of length l */
p += htbl->bits[l];
- htbl->priv.dec.maxcode[l] = huffcode[p-1]; /* maximum code of length l */
+ dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
} else {
- htbl->priv.dec.maxcode[l] = -1; /* -1 if no codes of this length */
+ dtbl->maxcode[l] = -1; /* -1 if no codes of this length */
}
}
- htbl->priv.dec.maxcode[17] = 0xFFFFFL; /* ensures huff_DECODE terminates */
+ dtbl->maxcode[17] = 0xFFFFFL; /* ensures huff_DECODE terminates */
/* Compute lookahead tables to speed up decoding.
* First we set all the table entries to 0, indicating "too long";
@@ -78,7 +215,7 @@
* with that code.
*/
- MEMZERO(htbl->priv.dec.look_nbits, SIZEOF(htbl->priv.dec.look_nbits));
+ MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
p = 0;
for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
@@ -87,8 +224,8 @@
/* Generate left-justified code followed by all possible bit sequences */
lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
- htbl->priv.dec.look_nbits[lookbits] = l;
- htbl->priv.dec.look_sym[lookbits] = htbl->huffval[p];
+ dtbl->look_nbits[lookbits] = l;
+ dtbl->look_sym[lookbits] = htbl->huffval[p];
lookbits++;
}
}
@@ -107,13 +244,15 @@
* bits, fill_bit_buffer is called; it will attempt to fill get_buffer to
* the "high water mark" (not just to the number of bits needed; this reduces
* the function-call overhead cost of entering fill_bit_buffer).
- * On return, fill_bit_buffer guarantees that get_buffer contains at least
- * the requested number of bits --- dummy zeroes are inserted if necessary.
+ * Note that fill_bit_buffer may return FALSE to indicate suspension.
+ * On TRUE return, fill_bit_buffer guarantees that get_buffer contains
+ * at least the requested number of bits --- dummy zeroes are inserted if
+ * necessary.
*
* On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
* of get_buffer to be used. (On machines with wider words, an even larger
* buffer could be used.) However, on some machines 32-bit shifts are
- * relatively slow and take time proportional to the number of places shifted.
+ * quite slow and take time proportional to the number of places shifted.
* (This is true with most PC compilers, for instance.) In this case it may
* be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the
* average shift distance at the cost of more calls to fill_bit_buffer.
@@ -126,37 +265,70 @@
#endif
-LOCAL void
-fill_bit_buffer (int nbits)
+LOCAL boolean
+fill_bit_buffer (working_state * state, int nbits)
/* Load up the bit buffer to a depth of at least nbits */
{
+ /* Copy heavily used state fields into locals (hopefully registers) */
+ register const JOCTET * next_input_byte = state->next_input_byte;
+ register size_t bytes_in_buffer = state->bytes_in_buffer;
+ register INT32 get_buffer = state->cur.get_buffer;
+ register int bits_left = state->cur.bits_left;
+ register int c;
+
/* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
/* (It is assumed that no request will be for more than that many bits.) */
+
while (bits_left < MIN_GET_BITS) {
- register int c = JGETC(dcinfo);
-
+ /* Attempt to read a byte */
+ if (state->unread_marker != 0)
+ goto no_more_data; /* can't advance past a marker */
+
+ if (bytes_in_buffer == 0) {
+ if (! (*state->cinfo->src->fill_input_buffer) (state->cinfo))
+ return FALSE;
+ next_input_byte = state->cinfo->src->next_input_byte;
+ bytes_in_buffer = state->cinfo->src->bytes_in_buffer;
+ }
+ bytes_in_buffer--;
+ c = GETJOCTET(*next_input_byte++);
+
/* If it's 0xFF, check and discard stuffed zero byte */
if (c == 0xFF) {
- int c2 = JGETC(dcinfo);
- if (c2 != 0) {
+ do {
+ if (bytes_in_buffer == 0) {
+ if (! (*state->cinfo->src->fill_input_buffer) (state->cinfo))
+ return FALSE;
+ next_input_byte = state->cinfo->src->next_input_byte;
+ bytes_in_buffer = state->cinfo->src->bytes_in_buffer;
+ }
+ bytes_in_buffer--;
+ c = GETJOCTET(*next_input_byte++);
+ } while (c == 0xFF);
+
+ if (c == 0) {
+ /* Found FF/00, which represents an FF data byte */
+ c = 0xFF;
+ } else {
/* Oops, it's actually a marker indicating end of compressed data. */
/* Better put it back for use later */
- JUNGETC(c2,dcinfo);
- JUNGETC(c,dcinfo);
+ state->unread_marker = c;
+
+ no_more_data:
/* There should be enough bits still left in the data segment; */
- /* if so, just break out of the while loop. */
+ /* if so, just break out of the outer while loop. */
if (bits_left >= nbits)
break;
/* Uh-oh. Report corrupted data to user and stuff zeroes into
* the data stream, so that we can produce some kind of image.
* Note that this will be repeated for each byte demanded for the
- * rest of the segment; this is a bit slow but not unreasonably so.
+ * rest of the segment; this is slow but not unreasonably so.
* The main thing is to avoid getting a zillion warnings, hence
* we use a flag to ensure that only one warning appears.
*/
- if (! printed_eod) {
- WARNMS(dcinfo->emethods, "Corrupt JPEG data: premature end of data segment");
- printed_eod = TRUE;
+ if (! ((huff_entropy_ptr) state->cinfo->entropy)->printed_eod) {
+ WARNMS(state->cinfo, JWRN_HIT_MARKER);
+ ((huff_entropy_ptr) state->cinfo->entropy)->printed_eod = TRUE;
}
c = 0; /* insert a zero byte into bit buffer */
}
@@ -166,31 +338,50 @@
get_buffer = (get_buffer << 8) | c;
bits_left += 8;
}
+
+ /* Unload the local registers */
+ state->next_input_byte = next_input_byte;
+ state->bytes_in_buffer = bytes_in_buffer;
+ state->cur.get_buffer = get_buffer;
+ state->cur.bits_left = bits_left;
+
+ return TRUE;
}
/*
* These macros provide the in-line portion of bit fetching.
- * Correct usage is:
- * check_bit_buffer(n); ensure there are N bits in get_buffer
- * val = get_bits(n); fetch N bits
- * The value n should be a simple variable, not an expression, because it
+ * Use check_bit_buffer to ensure there are N bits in get_buffer
+ * before using get_bits, peek_bits, or drop_bits.
+ * check_bit_buffer(state,n,action);
+ * Ensure there are N bits in get_buffer; if suspend, take action.
+ * val = get_bits(state,n);
+ * Fetch next N bits.
+ * val = peek_bits(state,n);
+ * Fetch next N bits without removing them from the buffer.
+ * drop_bits(state,n);
+ * Discard next N bits.
+ * The value N should be a simple variable, not an expression, because it
* is evaluated multiple times.
- * peek_bits() fetches next N bits without removing them from the buffer.
*/
-#define check_bit_buffer(nbits) \
- { if (bits_left < (nbits)) fill_bit_buffer(nbits); }
+#define check_bit_buffer(state,nbits,action) \
+ { if ((state).cur.bits_left < (nbits)) \
+ if (! fill_bit_buffer(&(state), nbits)) \
+ { action; } }
-#define get_bits(nbits) \
- (((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1))
+#define get_bits(state,nbits) \
+ (((int) ((state).cur.get_buffer >> ((state).cur.bits_left -= (nbits)))) & ((1<<(nbits))-1))
-#define peek_bits(nbits) \
- (((int) (get_buffer >> (bits_left - (nbits)))) & ((1<<(nbits))-1))
+#define peek_bits(state,nbits) \
+ (((int) ((state).cur.get_buffer >> ((state).cur.bits_left - (nbits)))) & ((1<<(nbits))-1))
+
+#define drop_bits(state,nbits) \
+ ((state).cur.bits_left -= (nbits))
/*
- * Routines to extract next Huffman-coded symbol from input bit stream.
+ * Code for extracting next Huffman-coded symbol from input bit stream.
* We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
* without looping. Usually, more than 95% of the Huffman codes will be 8
* or fewer bits long. The few overlength codes are handled with a loop.
@@ -198,32 +389,37 @@
* routine slow_DECODE is rarely entered and need not be inline code.
*
* Notes about the huff_DECODE macro:
- * 1. The first if-test is coded to call fill_bit_buffer only when necessary.
- * 2. If the lookahead succeeds, we need only decrement bits_left to remove
- * the proper number of bits from get_buffer.
- * 3. If the lookahead table contains no entry, the next code must be
- * more than HUFF_LOOKAHEAD bits long.
- * 4. Near the end of the data segment, we may fail to get enough bits
+ * 1. Near the end of the data segment, we may fail to get enough bits
* for a lookahead. In that case, we do it the hard way.
+ * 2. If the lookahead table contains no entry, the next code must be
+ * more than HUFF_LOOKAHEAD bits long.
+ * 3. slow_DECODE returns -1 if forced to suspend.
*/
-#define huff_DECODE(htbl,result) \
-{ register int nb, look; \
- if (bits_left >= HUFF_LOOKAHEAD || \
- (fill_bit_buffer(0), bits_left >= HUFF_LOOKAHEAD)) { \
- look = peek_bits(HUFF_LOOKAHEAD); \
- if ((nb = htbl->priv.dec.look_nbits[look]) != 0) { \
- bits_left -= nb; \
- result = htbl->priv.dec.look_sym[look]; \
- } else \
- result = slow_DECODE(htbl, HUFF_LOOKAHEAD+1); \
- } else \
- result = slow_DECODE(htbl, 1); \
+#define huff_DECODE(result,state,htbl,donelabel) \
+{ if (state.cur.bits_left < HUFF_LOOKAHEAD) { \
+ if (! fill_bit_buffer(&state, 0)) return FALSE; \
+ if (state.cur.bits_left < HUFF_LOOKAHEAD) { \
+ if ((result = slow_DECODE(&state, htbl, 1)) < 0) return FALSE; \
+ goto donelabel; \
+ } \
+ } \
+ { register int nb, look; \
+ look = peek_bits(state, HUFF_LOOKAHEAD); \
+ if ((nb = htbl->look_nbits[look]) != 0) { \
+ drop_bits(state, nb); \
+ result = htbl->look_sym[look]; \
+ } else { \
+ if ((result = slow_DECODE(&state, htbl, HUFF_LOOKAHEAD+1)) < 0) \
+ return FALSE; \
+ } \
+ } \
+donelabel:; \
}
LOCAL int
-slow_DECODE (HUFF_TBL * htbl, int min_bits)
+slow_DECODE (working_state * state, D_DERIVED_TBL * htbl, int min_bits)
{
register int l = min_bits;
register INT32 code;
@@ -231,28 +427,28 @@
/* huff_DECODE has determined that the code is at least min_bits */
/* bits long, so fetch that many bits in one swoop. */
- check_bit_buffer(l);
- code = get_bits(l);
+ check_bit_buffer(*state, l, return -1);
+ code = get_bits(*state, l);
/* Collect the rest of the Huffman code one bit at a time. */
/* This is per Figure F.16 in the JPEG spec. */
- while (code > htbl->priv.dec.maxcode[l]) {
+ while (code > htbl->maxcode[l]) {
code <<= 1;
- check_bit_buffer(1);
- code |= get_bits(1);
+ check_bit_buffer(*state, 1, return -1);
+ code |= get_bits(*state, 1);
l++;
}
/* With garbage input we may reach the sentinel value l = 17. */
if (l > 16) {
- WARNMS(dcinfo->emethods, "Corrupt JPEG data: bad Huffman code");
+ WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
return 0; /* fake a zero as the safest result */
}
- return htbl->huffval[ htbl->priv.dec.valptr[l] +
- ((int) (code - htbl->priv.dec.mincode[l])) ];
+ return htbl->pub->huffval[ htbl->valptr[l] +
+ ((int) (code - htbl->mincode[l])) ];
}
@@ -282,87 +478,35 @@
/*
- * Initialize for a Huffman-compressed scan.
- * This is invoked after reading the SOS marker.
- */
-
-METHODDEF void
-decoder_init (decompress_info_ptr cinfo)
-{
- short ci;
- jpeg_component_info * compptr;
-
- /* Initialize static variables */
- dcinfo = cinfo;
- bits_left = 0;
- printed_eod = FALSE;
-
- for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
- compptr = cinfo->cur_comp_info[ci];
- /* Make sure requested tables are present */
- if (cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no] == NULL ||
- cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no] == NULL)
- ERREXIT(cinfo->emethods, "Use of undefined Huffman table");
- /* Compute derived values for Huffman tables */
- /* We may do this more than once for same table, but it's not a big deal */
- fix_huff_tbl(cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no]);
- fix_huff_tbl(cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no]);
- /* Initialize DC predictions to 0 */
- cinfo->last_dc_val[ci] = 0;
- }
-
- /* Initialize restart stuff */
- cinfo->restarts_to_go = cinfo->restart_interval;
- cinfo->next_restart_num = 0;
-}
-
-
-/*
* Check for a restart marker & resynchronize decoder.
+ * Returns FALSE if must suspend.
*/
-LOCAL void
-process_restart (decompress_info_ptr cinfo)
+LOCAL boolean
+process_restart (j_decompress_ptr cinfo)
{
- int c, nbytes;
- short ci;
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
+ int ci;
- /* Throw away any unused bits remaining in bit buffer */
- nbytes = bits_left / 8; /* count any full bytes loaded into buffer */
- bits_left = 0;
- printed_eod = FALSE; /* next segment can get another warning */
+ /* Throw away any unused bits remaining in bit buffer; */
+ /* include any full bytes in next_marker's count of discarded bytes */
+ cinfo->marker->discarded_bytes += entropy->saved.bits_left / 8;
+ entropy->saved.bits_left = 0;
- /* Scan for next JPEG marker */
- do {
- do { /* skip any non-FF bytes */
- nbytes++;
- c = JGETC(cinfo);
- } while (c != 0xFF);
- do { /* skip any duplicate FFs */
- /* we don't increment nbytes here since extra FFs are legal */
- c = JGETC(cinfo);
- } while (c == 0xFF);
- } while (c == 0); /* repeat if it was a stuffed FF/00 */
-
- if (nbytes != 1)
- WARNMS2(cinfo->emethods,
- "Corrupt JPEG data: %d extraneous bytes before marker 0x%02x",
- nbytes-1, c);
-
- if (c != (RST0 + cinfo->next_restart_num)) {
- /* Uh-oh, the restart markers have been messed up too. */
- /* Let the file-format module try to figure out how to resync. */
- (*cinfo->methods->resync_to_restart) (cinfo, c);
- } else
- TRACEMS1(cinfo->emethods, 2, "RST%d", cinfo->next_restart_num);
+ /* Advance past the RSTn marker */
+ if (! (*cinfo->marker->read_restart_marker) (cinfo))
+ return FALSE;
/* Re-initialize DC predictions to 0 */
for (ci = 0; ci < cinfo->comps_in_scan; ci++)
- cinfo->last_dc_val[ci] = 0;
+ entropy->saved.last_dc_val[ci] = 0;
- /* Update restart state */
- cinfo->restarts_to_go = cinfo->restart_interval;
- cinfo->next_restart_num = (cinfo->next_restart_num + 1) & 7;
+ /* Reset restart counter */
+ entropy->restarts_to_go = cinfo->restart_interval;
+
+ entropy->printed_eod = FALSE; /* next segment can get another warning */
+
+ return TRUE;
}
@@ -372,7 +516,7 @@
* we put some extra zeroes after the real entries.
*/
-static const short ZAG[DCTSIZE2+16] = {
+static const int ZAG[DCTSIZE2+16] = {
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
@@ -388,104 +532,156 @@
/*
* Decode and return one MCU's worth of Huffman-compressed coefficients.
- * This routine also handles quantization descaling and zigzag reordering
- * of coefficient values.
+ * The coefficients are reordered from zigzag order into natural array order,
+ * but are not dequantized.
*
* The i'th block of the MCU is stored into the block pointed to by
* MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
* (Wholesale zeroing is usually a little faster than retail...)
+ *
+ * Returns FALSE if data source requested suspension. In that case no
+ * changes have been made to permanent state. (Exception: some output
+ * coefficients may already have been assigned. This is harmless for
+ * this module, but would not work for decoding progressive JPEG.)
*/
-METHODDEF void
-decode_mcu (decompress_info_ptr cinfo, JBLOCKROW *MCU_data)
+METHODDEF boolean
+decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
{
+ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
register int s, k, r;
- short blkn, ci;
- register JBLOCKROW block;
- register QUANT_TBL_PTR quanttbl;
- HUFF_TBL *dctbl;
- HUFF_TBL *actbl;
+ int blkn, ci;
+ JBLOCKROW block;
+ working_state state;
+ D_DERIVED_TBL * dctbl;
+ D_DERIVED_TBL * actbl;
jpeg_component_info * compptr;
- /* Account for restart interval, process restart marker if needed */
+ /* Process restart marker if needed; may have to suspend */
if (cinfo->restart_interval) {
- if (cinfo->restarts_to_go == 0)
- process_restart(cinfo);
- cinfo->restarts_to_go--;
+ if (entropy->restarts_to_go == 0)
+ if (! process_restart(cinfo))
+ return FALSE;
}
+ /* Load up working state */
+ state.unread_marker = cinfo->unread_marker;
+ state.next_input_byte = cinfo->src->next_input_byte;
+ state.bytes_in_buffer = cinfo->src->bytes_in_buffer;
+ ASSIGN_STATE(state.cur, entropy->saved);
+ state.cinfo = cinfo;
+
/* Outer loop handles each block in the MCU */
for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
block = MCU_data[blkn];
ci = cinfo->MCU_membership[blkn];
compptr = cinfo->cur_comp_info[ci];
- quanttbl = cinfo->quant_tbl_ptrs[compptr->quant_tbl_no];
- actbl = cinfo->ac_huff_tbl_ptrs[compptr->ac_tbl_no];
- dctbl = cinfo->dc_huff_tbl_ptrs[compptr->dc_tbl_no];
+ dctbl = entropy->dc_derived_tbls[compptr->dc_tbl_no];
+ actbl = entropy->ac_derived_tbls[compptr->ac_tbl_no];
/* Decode a single block's worth of coefficients */
/* Section F.2.2.1: decode the DC coefficient difference */
- huff_DECODE(dctbl, s);
+ huff_DECODE(s, state, dctbl, label1);
if (s) {
- check_bit_buffer(s);
- r = get_bits(s);
+ check_bit_buffer(state, s, return FALSE);
+ r = get_bits(state, s);
s = huff_EXTEND(r, s);
}
+ /* Shortcut if component's values are not interesting */
+ if (! compptr->component_needed)
+ goto skip_ACs;
+
/* Convert DC difference to actual value, update last_dc_val */
- s += cinfo->last_dc_val[ci];
- cinfo->last_dc_val[ci] = (JCOEF) s;
- /* Descale and output the DC coefficient (assumes ZAG[0] = 0) */
- (*block)[0] = (JCOEF) (((JCOEF) s) * quanttbl[0]);
-
- /* Section F.2.2.2: decode the AC coefficients */
- /* Since zero values are skipped, output area must be zeroed beforehand */
- for (k = 1; k < DCTSIZE2; k++) {
- huff_DECODE(actbl, s);
+ s += state.cur.last_dc_val[ci];
+ state.cur.last_dc_val[ci] = s;
+ /* Output the DC coefficient (assumes ZAG[0] = 0) */
+ (*block)[0] = (JCOEF) s;
+
+ /* Do we need to decode the AC coefficients for this component? */
+ if (compptr->DCT_scaled_size > 1) {
+
+ /* Section F.2.2.2: decode the AC coefficients */
+ /* Since zeroes are skipped, output area must be cleared beforehand */
+ for (k = 1; k < DCTSIZE2; k++) {
+ huff_DECODE(s, state, actbl, label2);
- r = s >> 4;
- s &= 15;
+ r = s >> 4;
+ s &= 15;
- if (s) {
- k += r;
- check_bit_buffer(s);
- r = get_bits(s);
- s = huff_EXTEND(r, s);
- /* Descale coefficient and output in natural (dezigzagged) order */
- (*block)[ZAG[k]] = (JCOEF) (((JCOEF) s) * quanttbl[k]);
- } else {
- if (r != 15)
- break;
- k += 15;
+ if (s) {
+ k += r;
+ check_bit_buffer(state, s, return FALSE);
+ r = get_bits(state, s);
+ s = huff_EXTEND(r, s);
+ /* Output coefficient in natural (dezigzagged) order */
+ (*block)[ZAG[k]] = (JCOEF) s;
+ } else {
+ if (r != 15)
+ break;
+ k += 15;
+ }
}
+
+ } else {
+skip_ACs:
+
+ /* Section F.2.2.2: decode the AC coefficients */
+ /* In this path we just discard the values */
+ for (k = 1; k < DCTSIZE2; k++) {
+ huff_DECODE(s, state, actbl, label3);
+
+ r = s >> 4;
+ s &= 15;
+
+ if (s) {
+ k += r;
+ check_bit_buffer(state, s, return FALSE);
+ drop_bits(state, s);
+ } else {
+ if (r != 15)
+ break;
+ k += 15;
+ }
+ }
+
}
}
+
+ /* Completed MCU, so update state */
+ cinfo->unread_marker = state.unread_marker;
+ cinfo->src->next_input_byte = state.next_input_byte;
+ cinfo->src->bytes_in_buffer = state.bytes_in_buffer;
+ ASSIGN_STATE(entropy->saved, state.cur);
+
+ /* Account for restart interval (no-op if not using restarts) */
+ entropy->restarts_to_go--;
+
+ return TRUE;
}
/*
- * Finish up at the end of a Huffman-compressed scan.
- */
-
-METHODDEF void
-decoder_term (decompress_info_ptr cinfo)
-{
- /* No work needed */
-}
-
-
-/*
- * The method selection routine for Huffman entropy decoding.
+ * Module initialization routine for Huffman entropy decoding.
*/
GLOBAL void
-jseldhuffman (decompress_info_ptr cinfo)
+jinit_huff_decoder (j_decompress_ptr cinfo)
{
- if (! cinfo->arith_code) {
- cinfo->methods->entropy_decode_init = decoder_init;
- cinfo->methods->entropy_decode = decode_mcu;
- cinfo->methods->entropy_decode_term = decoder_term;
+ huff_entropy_ptr entropy;
+ int i;
+
+ entropy = (huff_entropy_ptr)
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
+ SIZEOF(huff_entropy_decoder));
+ cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
+ entropy->pub.start_pass = start_pass_huff_decoder;
+ entropy->pub.decode_mcu = decode_mcu;
+
+ /* Mark tables unallocated */
+ for (i = 0; i < NUM_HUFF_TBLS; i++) {
+ entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
}
}