0N/A * reserved comment block 0N/A * DO NOT REMOVE OR ALTER! 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 * This file contains Huffman entropy decoding routines for progressive JPEG. 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 progressive 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 unsigned int EOBRUN;
/* remaining EOBs in EOBRUN */ 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/* Forward declarations */ 0N/A * Initialize for a Huffman-compressed scan. 0N/A /* Validate scan parameters */ 0N/A /* need not check Ss/Se < 0 since they came from unsigned bytes */ 0N/A /* AC scans may have only one component */ 0N/A /* Successive approximation refinement scan: must have Al = Ah-1. */ 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 /* 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 /* Select MCU decoding routine */ 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 if (
cinfo->
Ah == 0) {
/* DC refinement needs no table */ 0N/A /* remember the single active table */ 0N/A /* Initialize DC predictions to 0 */ 0N/A /* Initialize bitread state variables */ 0N/A /* Initialize private state variables */ 0N/A /* Initialize restart counter */ 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 /* Re-init EOB run count, too */ 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 * 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 * 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 * 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 * MCU decoding for DC initial scan (either spectral selection, 0N/A * or first pass of successive approximation). 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 /* 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 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */ 0N/A /* Completed MCU, so update state */ 0N/A /* Account for restart interval (no-op if not using restarts) */ 0N/A * MCU decoding for AC initial scan (either spectral selection, 0N/A * or first pass of successive approximation). 0N/A register int s, k, r;
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 /* There is always only one block per MCU */ 0N/A if (
EOBRUN > 0)
/* if it's a band of zeroes... */ 0N/A EOBRUN--;
/* ...process it now (we do nothing) */ 0N/A /* Scale and output coefficient in natural (dezigzagged) order */ 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 if (r) {
/* EOBr, r > 0 */ 0N/A EOBRUN--;
/* this band is processed at this moment */ 0N/A break;
/* force end-of-band */ 0N/A /* Completed MCU, so update state */ 0N/A /* Account for restart interval (no-op if not using restarts) */ 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 int p1 =
1 <<
cinfo->
Al;
/* 1 in the bit position being coded */ 0N/A /* Process restart marker if needed; may have to suspend */ 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 /* Load up working state */ 0N/A /* Outer loop handles each block in the MCU */ 0N/A /* Encoded data is simply the next bit of the two's-complement DC value */ 0N/A /* Note: since we use |=, repeating the assignment later is safe */ 0N/A /* Completed MCU, so update state */ 0N/A /* Account for restart interval (no-op if not using restarts) */ 0N/A * MCU decoding for AC successive approximation refinement scan. 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 /* Process restart marker if needed; may have to suspend */ 0N/A /* If we've run out of data, don't modify the MCU. 0N/A /* Load up working state */ 0N/A /* There is always only one block per MCU */ 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 /* initialize coefficient loop counter to start of band */ 0N/A if (s !=
1)
/* size of new coef should always be 1 */ 0N/A s =
p1;
/* newly nonzero coef is positive */ 0N/A s =
m1;
/* newly nonzero coef is negative */ 0N/A EOBRUN =
1 << r;
/* EOBr, run length is 2^r + appended bits */ 0N/A break;
/* rest of block is handled by EOB logic */ 0N/A /* note s = 0 for processing ZRL */ 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 break;
/* reached target zero coefficient */ 0N/A /* Output newly nonzero coefficient */ 0N/A /* Remember its position in case we have to suspend */ 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 if ((*
thiscoef &
p1) == 0) {
/* do nothing if already changed it */ 0N/A /* Count one block completed in EOB run */ 0N/A /* Completed MCU, so update state */ 0N/A /* Account for restart interval (no-op if not using restarts) */ 0N/A /* Re-zero any output coefficients that we made newly nonzero */ 0N/A * Module initialization routine for progressive Huffman entropy decoding. 0N/A /* Mark derived tables unallocated */ 0N/A /* Create progression status table */ 0N/A#
endif /* D_PROGRESSIVE_SUPPORTED */