michael@0: /* michael@0: * jdhuff.c michael@0: * michael@0: * This file was part of the Independent JPEG Group's software: michael@0: * Copyright (C) 1991-1997, Thomas G. Lane. michael@0: * libjpeg-turbo Modifications: michael@0: * Copyright (C) 2009-2011, D. R. Commander. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains Huffman entropy decoding routines. michael@0: * michael@0: * Much of the complexity here has to do with supporting input suspension. michael@0: * If the data source module demands suspension, we want to be able to back michael@0: * up to the start of the current MCU. To do this, we copy state variables michael@0: * into local working storage, and update them back to the permanent michael@0: * storage only upon successful completion of an MCU. michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: #include "jdhuff.h" /* Declarations shared with jdphuff.c */ michael@0: #include "jpegcomp.h" michael@0: #include "jstdhuff.c" michael@0: michael@0: michael@0: /* michael@0: * Expanded entropy decoder object for Huffman decoding. michael@0: * michael@0: * The savable_state subrecord contains fields that change within an MCU, michael@0: * but must not be updated permanently until we complete the MCU. michael@0: */ michael@0: michael@0: typedef struct { michael@0: int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ michael@0: } savable_state; michael@0: michael@0: /* This macro is to work around compilers with missing or broken michael@0: * structure assignment. You'll need to fix this code if you have michael@0: * such a compiler and you change MAX_COMPS_IN_SCAN. michael@0: */ michael@0: michael@0: #ifndef NO_STRUCT_ASSIGN michael@0: #define ASSIGN_STATE(dest,src) ((dest) = (src)) michael@0: #else michael@0: #if MAX_COMPS_IN_SCAN == 4 michael@0: #define ASSIGN_STATE(dest,src) \ michael@0: ((dest).last_dc_val[0] = (src).last_dc_val[0], \ michael@0: (dest).last_dc_val[1] = (src).last_dc_val[1], \ michael@0: (dest).last_dc_val[2] = (src).last_dc_val[2], \ michael@0: (dest).last_dc_val[3] = (src).last_dc_val[3]) michael@0: #endif michael@0: #endif michael@0: michael@0: michael@0: typedef struct { michael@0: struct jpeg_entropy_decoder pub; /* public fields */ michael@0: michael@0: /* These fields are loaded into local variables at start of each MCU. michael@0: * In case of suspension, we exit WITHOUT updating them. michael@0: */ michael@0: bitread_perm_state bitstate; /* Bit buffer at start of MCU */ michael@0: savable_state saved; /* Other state at start of MCU */ michael@0: michael@0: /* These fields are NOT loaded into local working state. */ michael@0: unsigned int restarts_to_go; /* MCUs left in this restart interval */ michael@0: michael@0: /* Pointers to derived tables (these workspaces have image lifespan) */ michael@0: d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; michael@0: d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; michael@0: michael@0: /* Precalculated info set up by start_pass for use in decode_mcu: */ michael@0: michael@0: /* Pointers to derived tables to be used for each block within an MCU */ michael@0: d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU]; michael@0: d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU]; michael@0: /* Whether we care about the DC and AC coefficient values for each block */ michael@0: boolean dc_needed[D_MAX_BLOCKS_IN_MCU]; michael@0: boolean ac_needed[D_MAX_BLOCKS_IN_MCU]; michael@0: } huff_entropy_decoder; michael@0: michael@0: typedef huff_entropy_decoder * huff_entropy_ptr; michael@0: michael@0: michael@0: /* michael@0: * Initialize for a Huffman-compressed scan. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: start_pass_huff_decoder (j_decompress_ptr cinfo) michael@0: { michael@0: huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; michael@0: int ci, blkn, dctbl, actbl; michael@0: jpeg_component_info * compptr; michael@0: michael@0: /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. michael@0: * This ought to be an error condition, but we make it a warning because michael@0: * there are some baseline files out there with all zeroes in these bytes. michael@0: */ michael@0: if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 || michael@0: cinfo->Ah != 0 || cinfo->Al != 0) michael@0: WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); michael@0: michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: dctbl = compptr->dc_tbl_no; michael@0: actbl = compptr->ac_tbl_no; michael@0: /* Compute derived values for Huffman tables */ michael@0: /* We may do this more than once for a table, but it's not expensive */ michael@0: jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, michael@0: & entropy->dc_derived_tbls[dctbl]); michael@0: jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, michael@0: & entropy->ac_derived_tbls[actbl]); michael@0: /* Initialize DC predictions to 0 */ michael@0: entropy->saved.last_dc_val[ci] = 0; michael@0: } michael@0: michael@0: /* Precalculate decoding info for each block in an MCU of this scan */ michael@0: for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { michael@0: ci = cinfo->MCU_membership[blkn]; michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: /* Precalculate which table to use for each block */ michael@0: entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; michael@0: entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no]; michael@0: /* Decide whether we really care about the coefficient values */ michael@0: if (compptr->component_needed) { michael@0: entropy->dc_needed[blkn] = TRUE; michael@0: /* we don't need the ACs if producing a 1/8th-size image */ michael@0: entropy->ac_needed[blkn] = (compptr->_DCT_scaled_size > 1); michael@0: } else { michael@0: entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE; michael@0: } michael@0: } michael@0: michael@0: /* Initialize bitread state variables */ michael@0: entropy->bitstate.bits_left = 0; michael@0: entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ michael@0: entropy->pub.insufficient_data = FALSE; michael@0: michael@0: /* Initialize restart counter */ michael@0: entropy->restarts_to_go = cinfo->restart_interval; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Compute the derived values for a Huffman table. michael@0: * This routine also performs some validation checks on the table. michael@0: * michael@0: * Note this is also used by jdphuff.c. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno, michael@0: d_derived_tbl ** pdtbl) michael@0: { michael@0: JHUFF_TBL *htbl; michael@0: d_derived_tbl *dtbl; michael@0: int p, i, l, si, numsymbols; michael@0: int lookbits, ctr; michael@0: char huffsize[257]; michael@0: unsigned int huffcode[257]; michael@0: unsigned int code; michael@0: michael@0: /* Note that huffsize[] and huffcode[] are filled in code-length order, michael@0: * paralleling the order of the symbols themselves in htbl->huffval[]. michael@0: */ michael@0: michael@0: /* Find the input Huffman table */ michael@0: if (tblno < 0 || tblno >= NUM_HUFF_TBLS) michael@0: ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); michael@0: htbl = michael@0: isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; michael@0: if (htbl == NULL) michael@0: ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); michael@0: michael@0: /* Allocate a workspace if we haven't already done so. */ michael@0: if (*pdtbl == NULL) michael@0: *pdtbl = (d_derived_tbl *) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(d_derived_tbl)); michael@0: dtbl = *pdtbl; michael@0: dtbl->pub = htbl; /* fill in back link */ michael@0: michael@0: /* Figure C.1: make table of Huffman code length for each symbol */ michael@0: michael@0: p = 0; michael@0: for (l = 1; l <= 16; l++) { michael@0: i = (int) htbl->bits[l]; michael@0: if (i < 0 || p + i > 256) /* protect against table overrun */ michael@0: ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); michael@0: while (i--) michael@0: huffsize[p++] = (char) l; michael@0: } michael@0: huffsize[p] = 0; michael@0: numsymbols = p; michael@0: michael@0: /* Figure C.2: generate the codes themselves */ michael@0: /* We also validate that the counts represent a legal Huffman code tree. */ michael@0: michael@0: code = 0; michael@0: si = huffsize[0]; michael@0: p = 0; michael@0: while (huffsize[p]) { michael@0: while (((int) huffsize[p]) == si) { michael@0: huffcode[p++] = code; michael@0: code++; michael@0: } michael@0: /* code is now 1 more than the last code used for codelength si; but michael@0: * it must still fit in si bits, since no code is allowed to be all ones. michael@0: */ michael@0: if (((INT32) code) >= (((INT32) 1) << si)) michael@0: ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); michael@0: code <<= 1; michael@0: si++; michael@0: } michael@0: michael@0: /* Figure F.15: generate decoding tables for bit-sequential decoding */ michael@0: michael@0: p = 0; michael@0: for (l = 1; l <= 16; l++) { michael@0: if (htbl->bits[l]) { michael@0: /* valoffset[l] = huffval[] index of 1st symbol of code length l, michael@0: * minus the minimum code of length l michael@0: */ michael@0: dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p]; michael@0: p += htbl->bits[l]; michael@0: dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */ michael@0: } else { michael@0: dtbl->maxcode[l] = -1; /* -1 if no codes of this length */ michael@0: } michael@0: } michael@0: dtbl->valoffset[17] = 0; michael@0: dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */ michael@0: michael@0: /* Compute lookahead tables to speed up decoding. michael@0: * First we set all the table entries to 0, indicating "too long"; michael@0: * then we iterate through the Huffman codes that are short enough and michael@0: * fill in all the entries that correspond to bit sequences starting michael@0: * with that code. michael@0: */ michael@0: michael@0: for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++) michael@0: dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD; michael@0: michael@0: p = 0; michael@0: for (l = 1; l <= HUFF_LOOKAHEAD; l++) { michael@0: for (i = 1; i <= (int) htbl->bits[l]; i++, p++) { michael@0: /* l = current code's length, p = its index in huffcode[] & huffval[]. */ michael@0: /* Generate left-justified code followed by all possible bit sequences */ michael@0: lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l); michael@0: for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) { michael@0: dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p]; michael@0: lookbits++; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Validate symbols as being reasonable. michael@0: * For AC tables, we make no check, but accept all byte values 0..255. michael@0: * For DC tables, we require the symbols to be in range 0..15. michael@0: * (Tighter bounds could be applied depending on the data depth and mode, michael@0: * but this is sufficient to ensure safe decoding.) michael@0: */ michael@0: if (isDC) { michael@0: for (i = 0; i < numsymbols; i++) { michael@0: int sym = htbl->huffval[i]; michael@0: if (sym < 0 || sym > 15) michael@0: ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Out-of-line code for bit fetching (shared with jdphuff.c). michael@0: * See jdhuff.h for info about usage. michael@0: * Note: current values of get_buffer and bits_left are passed as parameters, michael@0: * but are returned in the corresponding fields of the state struct. michael@0: * michael@0: * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width michael@0: * of get_buffer to be used. (On machines with wider words, an even larger michael@0: * buffer could be used.) However, on some machines 32-bit shifts are michael@0: * quite slow and take time proportional to the number of places shifted. michael@0: * (This is true with most PC compilers, for instance.) In this case it may michael@0: * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the michael@0: * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. michael@0: */ michael@0: michael@0: #ifdef SLOW_SHIFT_32 michael@0: #define MIN_GET_BITS 15 /* minimum allowable value */ michael@0: #else michael@0: #define MIN_GET_BITS (BIT_BUF_SIZE-7) michael@0: #endif michael@0: michael@0: michael@0: GLOBAL(boolean) michael@0: jpeg_fill_bit_buffer (bitread_working_state * state, michael@0: register bit_buf_type get_buffer, register int bits_left, michael@0: int nbits) michael@0: /* Load up the bit buffer to a depth of at least nbits */ michael@0: { michael@0: /* Copy heavily used state fields into locals (hopefully registers) */ michael@0: register const JOCTET * next_input_byte = state->next_input_byte; michael@0: register size_t bytes_in_buffer = state->bytes_in_buffer; michael@0: j_decompress_ptr cinfo = state->cinfo; michael@0: michael@0: /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ michael@0: /* (It is assumed that no request will be for more than that many bits.) */ michael@0: /* We fail to do so only if we hit a marker or are forced to suspend. */ michael@0: michael@0: if (cinfo->unread_marker == 0) { /* cannot advance past a marker */ michael@0: while (bits_left < MIN_GET_BITS) { michael@0: register int c; michael@0: michael@0: /* Attempt to read a byte */ michael@0: if (bytes_in_buffer == 0) { michael@0: if (! (*cinfo->src->fill_input_buffer) (cinfo)) michael@0: return FALSE; michael@0: next_input_byte = cinfo->src->next_input_byte; michael@0: bytes_in_buffer = cinfo->src->bytes_in_buffer; michael@0: } michael@0: bytes_in_buffer--; michael@0: c = GETJOCTET(*next_input_byte++); michael@0: michael@0: /* If it's 0xFF, check and discard stuffed zero byte */ michael@0: if (c == 0xFF) { michael@0: /* Loop here to discard any padding FF's on terminating marker, michael@0: * so that we can save a valid unread_marker value. NOTE: we will michael@0: * accept multiple FF's followed by a 0 as meaning a single FF data michael@0: * byte. This data pattern is not valid according to the standard. michael@0: */ michael@0: do { michael@0: if (bytes_in_buffer == 0) { michael@0: if (! (*cinfo->src->fill_input_buffer) (cinfo)) michael@0: return FALSE; michael@0: next_input_byte = cinfo->src->next_input_byte; michael@0: bytes_in_buffer = cinfo->src->bytes_in_buffer; michael@0: } michael@0: bytes_in_buffer--; michael@0: c = GETJOCTET(*next_input_byte++); michael@0: } while (c == 0xFF); michael@0: michael@0: if (c == 0) { michael@0: /* Found FF/00, which represents an FF data byte */ michael@0: c = 0xFF; michael@0: } else { michael@0: /* Oops, it's actually a marker indicating end of compressed data. michael@0: * Save the marker code for later use. michael@0: * Fine point: it might appear that we should save the marker into michael@0: * bitread working state, not straight into permanent state. But michael@0: * once we have hit a marker, we cannot need to suspend within the michael@0: * current MCU, because we will read no more bytes from the data michael@0: * source. So it is OK to update permanent state right away. michael@0: */ michael@0: cinfo->unread_marker = c; michael@0: /* See if we need to insert some fake zero bits. */ michael@0: goto no_more_bytes; michael@0: } michael@0: } michael@0: michael@0: /* OK, load c into get_buffer */ michael@0: get_buffer = (get_buffer << 8) | c; michael@0: bits_left += 8; michael@0: } /* end while */ michael@0: } else { michael@0: no_more_bytes: michael@0: /* We get here if we've read the marker that terminates the compressed michael@0: * data segment. There should be enough bits in the buffer register michael@0: * to satisfy the request; if so, no problem. michael@0: */ michael@0: if (nbits > bits_left) { michael@0: /* Uh-oh. Report corrupted data to user and stuff zeroes into michael@0: * the data stream, so that we can produce some kind of image. michael@0: * We use a nonvolatile flag to ensure that only one warning message michael@0: * appears per data segment. michael@0: */ michael@0: if (! cinfo->entropy->insufficient_data) { michael@0: WARNMS(cinfo, JWRN_HIT_MARKER); michael@0: cinfo->entropy->insufficient_data = TRUE; michael@0: } michael@0: /* Fill the buffer with zero bits */ michael@0: get_buffer <<= MIN_GET_BITS - bits_left; michael@0: bits_left = MIN_GET_BITS; michael@0: } michael@0: } michael@0: michael@0: /* Unload the local registers */ michael@0: state->next_input_byte = next_input_byte; michael@0: state->bytes_in_buffer = bytes_in_buffer; michael@0: state->get_buffer = get_buffer; michael@0: state->bits_left = bits_left; michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* Macro version of the above, which performs much better but does not michael@0: handle markers. We have to hand off any blocks with markers to the michael@0: slower routines. */ michael@0: michael@0: #define GET_BYTE \ michael@0: { \ michael@0: register int c0, c1; \ michael@0: c0 = GETJOCTET(*buffer++); \ michael@0: c1 = GETJOCTET(*buffer); \ michael@0: /* Pre-execute most common case */ \ michael@0: get_buffer = (get_buffer << 8) | c0; \ michael@0: bits_left += 8; \ michael@0: if (c0 == 0xFF) { \ michael@0: /* Pre-execute case of FF/00, which represents an FF data byte */ \ michael@0: buffer++; \ michael@0: if (c1 != 0) { \ michael@0: /* Oops, it's actually a marker indicating end of compressed data. */ \ michael@0: cinfo->unread_marker = c1; \ michael@0: /* Back out pre-execution and fill the buffer with zero bits */ \ michael@0: buffer -= 2; \ michael@0: get_buffer &= ~0xFF; \ michael@0: } \ michael@0: } \ michael@0: } michael@0: michael@0: #if __WORDSIZE == 64 || defined(_WIN64) michael@0: michael@0: /* Pre-fetch 48 bytes, because the holding register is 64-bit */ michael@0: #define FILL_BIT_BUFFER_FAST \ michael@0: if (bits_left < 16) { \ michael@0: GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE \ michael@0: } michael@0: michael@0: #else michael@0: michael@0: /* Pre-fetch 16 bytes, because the holding register is 32-bit */ michael@0: #define FILL_BIT_BUFFER_FAST \ michael@0: if (bits_left < 16) { \ michael@0: GET_BYTE GET_BYTE \ michael@0: } michael@0: michael@0: #endif michael@0: michael@0: michael@0: /* michael@0: * Out-of-line code for Huffman code decoding. michael@0: * See jdhuff.h for info about usage. michael@0: */ michael@0: michael@0: GLOBAL(int) michael@0: jpeg_huff_decode (bitread_working_state * state, michael@0: register bit_buf_type get_buffer, register int bits_left, michael@0: d_derived_tbl * htbl, int min_bits) michael@0: { michael@0: register int l = min_bits; michael@0: register INT32 code; michael@0: michael@0: /* HUFF_DECODE has determined that the code is at least min_bits */ michael@0: /* bits long, so fetch that many bits in one swoop. */ michael@0: michael@0: CHECK_BIT_BUFFER(*state, l, return -1); michael@0: code = GET_BITS(l); michael@0: michael@0: /* Collect the rest of the Huffman code one bit at a time. */ michael@0: /* This is per Figure F.16 in the JPEG spec. */ michael@0: michael@0: while (code > htbl->maxcode[l]) { michael@0: code <<= 1; michael@0: CHECK_BIT_BUFFER(*state, 1, return -1); michael@0: code |= GET_BITS(1); michael@0: l++; michael@0: } michael@0: michael@0: /* Unload the local registers */ michael@0: state->get_buffer = get_buffer; michael@0: state->bits_left = bits_left; michael@0: michael@0: /* With garbage input we may reach the sentinel value l = 17. */ michael@0: michael@0: if (l > 16) { michael@0: WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE); michael@0: return 0; /* fake a zero as the safest result */ michael@0: } michael@0: michael@0: return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ]; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Figure F.12: extend sign bit. michael@0: * On some machines, a shift and add will be faster than a table lookup. michael@0: */ michael@0: michael@0: #define AVOID_TABLES michael@0: #ifdef AVOID_TABLES michael@0: michael@0: #define HUFF_EXTEND(x,s) ((x) + ((((x) - (1<<((s)-1))) >> 31) & (((-1)<<(s)) + 1))) michael@0: michael@0: #else michael@0: michael@0: #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) michael@0: michael@0: static const int extend_test[16] = /* entry n is 2**(n-1) */ michael@0: { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, michael@0: 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; michael@0: michael@0: static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ michael@0: { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, michael@0: ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, michael@0: ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, michael@0: ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 }; michael@0: michael@0: #endif /* AVOID_TABLES */ michael@0: michael@0: michael@0: /* michael@0: * Check for a restart marker & resynchronize decoder. michael@0: * Returns FALSE if must suspend. michael@0: */ michael@0: michael@0: LOCAL(boolean) michael@0: process_restart (j_decompress_ptr cinfo) michael@0: { michael@0: huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; michael@0: int ci; michael@0: michael@0: /* Throw away any unused bits remaining in bit buffer; */ michael@0: /* include any full bytes in next_marker's count of discarded bytes */ michael@0: cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; michael@0: entropy->bitstate.bits_left = 0; michael@0: michael@0: /* Advance past the RSTn marker */ michael@0: if (! (*cinfo->marker->read_restart_marker) (cinfo)) michael@0: return FALSE; michael@0: michael@0: /* Re-initialize DC predictions to 0 */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) michael@0: entropy->saved.last_dc_val[ci] = 0; michael@0: michael@0: /* Reset restart counter */ michael@0: entropy->restarts_to_go = cinfo->restart_interval; michael@0: michael@0: /* Reset out-of-data flag, unless read_restart_marker left us smack up michael@0: * against a marker. In that case we will end up treating the next data michael@0: * segment as empty, and we can avoid producing bogus output pixels by michael@0: * leaving the flag set. michael@0: */ michael@0: if (cinfo->unread_marker == 0) michael@0: entropy->pub.insufficient_data = FALSE; michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: LOCAL(boolean) michael@0: decode_mcu_slow (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; michael@0: BITREAD_STATE_VARS; michael@0: int blkn; michael@0: savable_state state; michael@0: /* Outer loop handles each block in the MCU */ michael@0: michael@0: /* Load up working state */ michael@0: BITREAD_LOAD_STATE(cinfo,entropy->bitstate); michael@0: ASSIGN_STATE(state, entropy->saved); michael@0: michael@0: for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { michael@0: JBLOCKROW block = MCU_data[blkn]; michael@0: d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn]; michael@0: d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn]; michael@0: register int s, k, r; michael@0: michael@0: /* Decode a single block's worth of coefficients */ michael@0: michael@0: /* Section F.2.2.1: decode the DC coefficient difference */ michael@0: HUFF_DECODE(s, br_state, dctbl, return FALSE, label1); michael@0: if (s) { michael@0: CHECK_BIT_BUFFER(br_state, s, return FALSE); michael@0: r = GET_BITS(s); michael@0: s = HUFF_EXTEND(r, s); michael@0: } michael@0: michael@0: if (entropy->dc_needed[blkn]) { michael@0: /* Convert DC difference to actual value, update last_dc_val */ michael@0: int ci = cinfo->MCU_membership[blkn]; michael@0: s += state.last_dc_val[ci]; michael@0: state.last_dc_val[ci] = s; michael@0: /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */ michael@0: (*block)[0] = (JCOEF) s; michael@0: } michael@0: michael@0: if (entropy->ac_needed[blkn]) { michael@0: michael@0: /* Section F.2.2.2: decode the AC coefficients */ michael@0: /* Since zeroes are skipped, output area must be cleared beforehand */ michael@0: for (k = 1; k < DCTSIZE2; k++) { michael@0: HUFF_DECODE(s, br_state, actbl, return FALSE, label2); michael@0: michael@0: r = s >> 4; michael@0: s &= 15; michael@0: michael@0: if (s) { michael@0: k += r; michael@0: CHECK_BIT_BUFFER(br_state, s, return FALSE); michael@0: r = GET_BITS(s); michael@0: s = HUFF_EXTEND(r, s); michael@0: /* Output coefficient in natural (dezigzagged) order. michael@0: * Note: the extra entries in jpeg_natural_order[] will save us michael@0: * if k >= DCTSIZE2, which could happen if the data is corrupted. michael@0: */ michael@0: (*block)[jpeg_natural_order[k]] = (JCOEF) s; michael@0: } else { michael@0: if (r != 15) michael@0: break; michael@0: k += 15; michael@0: } michael@0: } michael@0: michael@0: } else { michael@0: michael@0: /* Section F.2.2.2: decode the AC coefficients */ michael@0: /* In this path we just discard the values */ michael@0: for (k = 1; k < DCTSIZE2; k++) { michael@0: HUFF_DECODE(s, br_state, actbl, return FALSE, label3); michael@0: michael@0: r = s >> 4; michael@0: s &= 15; michael@0: michael@0: if (s) { michael@0: k += r; michael@0: CHECK_BIT_BUFFER(br_state, s, return FALSE); michael@0: DROP_BITS(s); michael@0: } else { michael@0: if (r != 15) michael@0: break; michael@0: k += 15; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Completed MCU, so update state */ michael@0: BITREAD_SAVE_STATE(cinfo,entropy->bitstate); michael@0: ASSIGN_STATE(entropy->saved, state); michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: LOCAL(boolean) michael@0: decode_mcu_fast (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; michael@0: BITREAD_STATE_VARS; michael@0: JOCTET *buffer; michael@0: int blkn; michael@0: savable_state state; michael@0: /* Outer loop handles each block in the MCU */ michael@0: michael@0: /* Load up working state */ michael@0: BITREAD_LOAD_STATE(cinfo,entropy->bitstate); michael@0: buffer = (JOCTET *) br_state.next_input_byte; michael@0: ASSIGN_STATE(state, entropy->saved); michael@0: michael@0: for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { michael@0: JBLOCKROW block = MCU_data[blkn]; michael@0: d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn]; michael@0: d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn]; michael@0: register int s, k, r, l; michael@0: michael@0: HUFF_DECODE_FAST(s, l, dctbl); michael@0: if (s) { michael@0: FILL_BIT_BUFFER_FAST michael@0: r = GET_BITS(s); michael@0: s = HUFF_EXTEND(r, s); michael@0: } michael@0: michael@0: if (entropy->dc_needed[blkn]) { michael@0: int ci = cinfo->MCU_membership[blkn]; michael@0: s += state.last_dc_val[ci]; michael@0: state.last_dc_val[ci] = s; michael@0: (*block)[0] = (JCOEF) s; michael@0: } michael@0: michael@0: if (entropy->ac_needed[blkn]) { michael@0: michael@0: for (k = 1; k < DCTSIZE2; k++) { michael@0: HUFF_DECODE_FAST(s, l, actbl); michael@0: r = s >> 4; michael@0: s &= 15; michael@0: michael@0: if (s) { michael@0: k += r; michael@0: FILL_BIT_BUFFER_FAST michael@0: r = GET_BITS(s); michael@0: s = HUFF_EXTEND(r, s); michael@0: (*block)[jpeg_natural_order[k]] = (JCOEF) s; michael@0: } else { michael@0: if (r != 15) break; michael@0: k += 15; michael@0: } michael@0: } michael@0: michael@0: } else { michael@0: michael@0: for (k = 1; k < DCTSIZE2; k++) { michael@0: HUFF_DECODE_FAST(s, l, actbl); michael@0: r = s >> 4; michael@0: s &= 15; michael@0: michael@0: if (s) { michael@0: k += r; michael@0: FILL_BIT_BUFFER_FAST michael@0: DROP_BITS(s); michael@0: } else { michael@0: if (r != 15) break; michael@0: k += 15; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (cinfo->unread_marker != 0) { michael@0: cinfo->unread_marker = 0; michael@0: return FALSE; michael@0: } michael@0: michael@0: br_state.bytes_in_buffer -= (buffer - br_state.next_input_byte); michael@0: br_state.next_input_byte = buffer; michael@0: BITREAD_SAVE_STATE(cinfo,entropy->bitstate); michael@0: ASSIGN_STATE(entropy->saved, state); michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Decode and return one MCU's worth of Huffman-compressed coefficients. michael@0: * The coefficients are reordered from zigzag order into natural array order, michael@0: * but are not dequantized. michael@0: * michael@0: * The i'th block of the MCU is stored into the block pointed to by michael@0: * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. michael@0: * (Wholesale zeroing is usually a little faster than retail...) michael@0: * michael@0: * Returns FALSE if data source requested suspension. In that case no michael@0: * changes have been made to permanent state. (Exception: some output michael@0: * coefficients may already have been assigned. This is harmless for michael@0: * this module, since we'll just re-assign them on the next call.) michael@0: */ michael@0: michael@0: #define BUFSIZE (DCTSIZE2 * 2) michael@0: michael@0: METHODDEF(boolean) michael@0: decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) michael@0: { michael@0: huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; michael@0: int usefast = 1; michael@0: michael@0: /* Process restart marker if needed; may have to suspend */ michael@0: if (cinfo->restart_interval) { michael@0: if (entropy->restarts_to_go == 0) michael@0: if (! process_restart(cinfo)) michael@0: return FALSE; michael@0: usefast = 0; michael@0: } michael@0: michael@0: if (cinfo->src->bytes_in_buffer < BUFSIZE * (size_t)cinfo->blocks_in_MCU michael@0: || cinfo->unread_marker != 0) michael@0: usefast = 0; michael@0: michael@0: /* If we've run out of data, just leave the MCU set to zeroes. michael@0: * This way, we return uniform gray for the remainder of the segment. michael@0: */ michael@0: if (! entropy->pub.insufficient_data) { michael@0: michael@0: if (usefast) { michael@0: if (!decode_mcu_fast(cinfo, MCU_data)) goto use_slow; michael@0: } michael@0: else { michael@0: use_slow: michael@0: if (!decode_mcu_slow(cinfo, MCU_data)) return FALSE; michael@0: } michael@0: michael@0: } michael@0: michael@0: /* Account for restart interval (no-op if not using restarts) */ michael@0: entropy->restarts_to_go--; michael@0: michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Module initialization routine for Huffman entropy decoding. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jinit_huff_decoder (j_decompress_ptr cinfo) michael@0: { michael@0: huff_entropy_ptr entropy; michael@0: int i; michael@0: michael@0: /* Motion JPEG frames typically do not include the Huffman tables if they michael@0: are the default tables. Thus, if the tables are not set by the time michael@0: the Huffman decoder is initialized (usually within the body of michael@0: jpeg_start_decompress()), we set them to default values. */ michael@0: std_huff_tables((j_common_ptr) cinfo); michael@0: michael@0: entropy = (huff_entropy_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(huff_entropy_decoder)); michael@0: cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; michael@0: entropy->pub.start_pass = start_pass_huff_decoder; michael@0: entropy->pub.decode_mcu = decode_mcu; michael@0: michael@0: /* Mark tables unallocated */ michael@0: for (i = 0; i < NUM_HUFF_TBLS; i++) { michael@0: entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; michael@0: } michael@0: }