0N/A * reserved comment block 0N/A * DO NOT REMOVE OR ALTER! 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 * This file contains Huffman entropy decoding routines. 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 * Expanded entropy decoder object for Huffman decoding. 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/* 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 /* 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 /* These fields are NOT loaded into local working state. */ 0N/A /* Pointers to derived tables (these workspaces have image lifespan) */ 0N/A /* Precalculated info set up by start_pass for use in decode_mcu: */ 0N/A /* Pointers to derived tables to be used for each block within an MCU */ 0N/A /* Whether we care about the DC and AC coefficient values for each block */ 0N/A * Initialize for a Huffman-compressed scan. 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 /* 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 /* Initialize DC predictions to 0 */ 0N/A /* Precalculate decoding info for each block in an MCU of this scan */ 0N/A /* Precalculate which table to use for each block */ 0N/A /* Decide whether we really care about the coefficient values */ 0N/A /* we don't need the ACs if producing a 1/8th-size image */ 0N/A /* Initialize bitread state variables */ 0N/A /* Initialize restart counter */ 0N/A * Compute the derived values for a Huffman table. 0N/A * This routine also performs some validation checks on the table. 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 /* Find the input Huffman table */ 0N/A /* Allocate a workspace if we haven't already done so. */ 0N/A /* Figure C.1: make table of Huffman code length for each symbol */ 0N/A for (l =
1; l <=
16; l++) {
0N/A if (i < 0 || p + i >
256)
/* protect against table overrun */ 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 /* 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 /* Figure F.15: generate decoding tables for bit-sequential decoding */ 0N/A for (l =
1; l <=
16; 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 dtbl->
maxcode[
17] =
0xFFFFFL;
/* ensures jpeg_huff_decode terminates */ 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 /* 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 /* 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 * Out-of-line code for bit fetching (shared with jdphuff.c). 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 * 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/* Load up the bit buffer to a depth of at least nbits */ 0N/A /* Copy heavily used state fields into locals (hopefully registers) */ 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 /* Attempt to read a byte */ 0N/A /* If it's 0xFF, check and discard stuffed zero byte */ 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 }
while (c ==
0xFF);
0N/A /* Found FF/00, which represents an FF data byte */ 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 /* See if we need to insert some fake zero bits. */ 0N/A /* OK, load c into get_buffer */ 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 /* 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 /* Fill the buffer with zero bits */ 0N/A /* Unload the local registers */ 0N/A * Out-of-line code for Huffman code decoding. 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 /* 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 /* Unload the local registers */ 0N/A /* With garbage input we may reach the sentinel value l = 17. */ 0N/A return 0;
/* fake a zero as the safest result */ 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#
define HUFF_EXTEND(x,s) ((x) < (
1<<((s)-
1)) ? (x) + (((-
1)<<(s)) +
1) : (x))
0N/A { 0,
0x0001,
0x0002,
0x0004,
0x0008,
0x0010,
0x0020,
0x0040,
0x0080,
0N/A 0x0100,
0x0200,
0x0400,
0x0800,
0x1000,
0x2000,
0x4000 };
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#
endif /* AVOID_TABLES */ 0N/A * Check for a restart marker & resynchronize decoder. 0N/A * Returns FALSE if must suspend. 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 /* Advance past the RSTn marker */ 0N/A /* Re-initialize DC predictions to 0 */ 0N/A /* Reset restart counter */ 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 * 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 * 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 * 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 /* Process restart marker if needed; may have to suspend */ 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 /* Load up working state */ 0N/A /* Outer loop handles each block in the MCU */ 0N/A register int s, k, r;
0N/A /* Decode a single block's worth of coefficients */ 0N/A /* Section F.2.2.1: decode the DC coefficient difference */ 0N/A /* Convert DC difference to actual value, update last_dc_val */ 0N/A /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */ 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 /* 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 /* Section F.2.2.2: decode the AC coefficients */ 0N/A /* In this path we just discard the values */ 0N/A /* Completed MCU, so update state */ 0N/A /* Account for restart interval (no-op if not using restarts) */ 0N/A * Module initialization routine for Huffman entropy decoding. 0N/A /* Mark tables unallocated */