1e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* 2e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * jdhuff.c 3e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * 4e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Copyright (C) 1991-1997, Thomas G. Lane. 5e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This file is part of the Independent JPEG Group's software. 6e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * For conditions of distribution and use, see the accompanying README file. 7e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * 8e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This file contains Huffman entropy decoding routines. 9e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * 10e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Much of the complexity here has to do with supporting input suspension. 11e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * If the data source module demands suspension, we want to be able to back 12e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * up to the start of the current MCU. To do this, we copy state variables 13e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * into local working storage, and update them back to the permanent 14e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * storage only upon successful completion of an MCU. 15e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 16e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 17e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#define JPEG_INTERNALS 18e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#include "jinclude.h" 19e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#include "jpeglib.h" 20e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#include "jdhuff.h" /* Declarations shared with jdphuff.c */ 21e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 22e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#ifdef _FX_MANAGED_CODE_ 23e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#define savable_state savable_state_d 24e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#endif 25e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 26e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* 27e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Expanded entropy decoder object for Huffman decoding. 28e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * 29e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * The savable_state subrecord contains fields that change within an MCU, 30e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * but must not be updated permanently until we complete the MCU. 31e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 32e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 33e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovtypedef struct { 34e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ 35e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov} savable_state; 36e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 37e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* This macro is to work around compilers with missing or broken 38e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * structure assignment. You'll need to fix this code if you have 39e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * such a compiler and you change MAX_COMPS_IN_SCAN. 40e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 41e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 42e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#ifndef NO_STRUCT_ASSIGN 43e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#define ASSIGN_STATE(dest,src) ((dest) = (src)) 44e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#else 45e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#if MAX_COMPS_IN_SCAN == 4 46e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#define ASSIGN_STATE(dest,src) \ 47e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ((dest).last_dc_val[0] = (src).last_dc_val[0], \ 48e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov (dest).last_dc_val[1] = (src).last_dc_val[1], \ 49e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov (dest).last_dc_val[2] = (src).last_dc_val[2], \ 50e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov (dest).last_dc_val[3] = (src).last_dc_val[3]) 51e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#endif 52e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#endif 53e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 54e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 55e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovtypedef struct { 56e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov struct jpeg_entropy_decoder pub; /* public fields */ 57e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 58e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* These fields are loaded into local variables at start of each MCU. 59e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * In case of suspension, we exit WITHOUT updating them. 60e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 61e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov bitread_perm_state bitstate; /* Bit buffer at start of MCU */ 62e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov savable_state saved; /* Other state at start of MCU */ 63e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 64e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* These fields are NOT loaded into local working state. */ 65e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov unsigned int restarts_to_go; /* MCUs left in this restart interval */ 66e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 67e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Pointers to derived tables (these workspaces have image lifespan) */ 68e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; 69e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; 70e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 71e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Precalculated info set up by start_pass for use in decode_mcu: */ 72e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 73e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Pointers to derived tables to be used for each block within an MCU */ 74e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU]; 75e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU]; 76e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Whether we care about the DC and AC coefficient values for each block */ 77e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov boolean dc_needed[D_MAX_BLOCKS_IN_MCU]; 78e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov boolean ac_needed[D_MAX_BLOCKS_IN_MCU]; 79e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov} huff_entropy_decoder; 80e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 81e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovtypedef huff_entropy_decoder * huff_entropy_ptr; 82e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 83e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 84e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* 85e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Initialize for a Huffman-compressed scan. 86e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 87e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 88e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovMETHODDEF(void) 89e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovstart_pass_huff_decoder (j_decompress_ptr cinfo) 90e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{ 91e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 92e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov int ci, blkn, dctbl, actbl; 93e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov jpeg_component_info * compptr; 94e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 95e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. 96e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This ought to be an error condition, but we make it a warning because 97e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * there are some baseline files out there with all zeroes in these bytes. 98e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 99e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 || 100e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov cinfo->Ah != 0 || cinfo->Al != 0) 101e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); 102e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 103e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 104e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov compptr = cinfo->cur_comp_info[ci]; 105e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov dctbl = compptr->dc_tbl_no; 106e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov actbl = compptr->ac_tbl_no; 107e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Compute derived values for Huffman tables */ 108e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* We may do this more than once for a table, but it's not expensive */ 109e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, 110e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov & entropy->dc_derived_tbls[dctbl]); 111e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, 112e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov & entropy->ac_derived_tbls[actbl]); 113e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Initialize DC predictions to 0 */ 114e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->saved.last_dc_val[ci] = 0; 115e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 116e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 117e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Precalculate decoding info for each block in an MCU of this scan */ 118e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 119e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ci = cinfo->MCU_membership[blkn]; 120e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov compptr = cinfo->cur_comp_info[ci]; 121e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Precalculate which table to use for each block */ 122e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; 123e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no]; 124e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Decide whether we really care about the coefficient values */ 125e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (compptr->component_needed) { 126e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->dc_needed[blkn] = TRUE; 127e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* we don't need the ACs if producing a 1/8th-size image */ 128e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->ac_needed[blkn] = (compptr->DCT_scaled_size > 1); 129e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } else { 130e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE; 131e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 132e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 133e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 134e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Initialize bitread state variables */ 135e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->bitstate.bits_left = 0; 136e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ 137e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->pub.insufficient_data = FALSE; 138e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 139e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Initialize restart counter */ 140e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->restarts_to_go = cinfo->restart_interval; 141e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov} 142e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 143e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 144e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* 145e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Compute the derived values for a Huffman table. 146e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This routine also performs some validation checks on the table. 147e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * 148e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Note this is also used by jdphuff.c. 149e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 150e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 151e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovGLOBAL(void) 152e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovjpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno, 153e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov d_derived_tbl ** pdtbl) 154e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{ 155e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov JHUFF_TBL *htbl; 156e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov d_derived_tbl *dtbl; 157e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov int p, i, l, _si, numsymbols; 158e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov int lookbits, ctr; 159e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov char huffsize[257]; 160e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov unsigned int huffcode[257]; 161e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov unsigned int code; 162e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 163e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Note that huffsize[] and huffcode[] are filled in code-length order, 164e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * paralleling the order of the symbols themselves in htbl->huffval[]. 165e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 166e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 167e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Find the input Huffman table */ 168e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (tblno < 0 || tblno >= NUM_HUFF_TBLS) 169e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); 170e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov htbl = 171e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; 172e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (htbl == NULL) 173e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); 174e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 175e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Allocate a workspace if we haven't already done so. */ 176e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (*pdtbl == NULL) 177e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov *pdtbl = (d_derived_tbl *) 178e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 179e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov SIZEOF(d_derived_tbl)); 180e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov dtbl = *pdtbl; 181e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov dtbl->pub = htbl; /* fill in back link */ 182e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 183e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Figure C.1: make table of Huffman code length for each symbol */ 184e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 185e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov p = 0; 186e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (l = 1; l <= 16; l++) { 187e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov i = (int) htbl->bits[l]; 188e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (i < 0 || p + i > 256) /* protect against table overrun */ 189e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 190e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov while (i--) 191e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov huffsize[p++] = (char) l; 192e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 193e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov huffsize[p] = 0; 194e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov numsymbols = p; 195e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 196e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Figure C.2: generate the codes themselves */ 197e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* We also validate that the counts represent a legal Huffman code tree. */ 198e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 199e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov code = 0; 200e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov _si = huffsize[0]; 201e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov p = 0; 202e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov while (huffsize[p]) { 203e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov while (((int) huffsize[p]) == _si) { 204e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov huffcode[p++] = code; 205e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov code++; 206e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 207e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* code is now 1 more than the last code used for codelength si; but 208e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * it must still fit in si bits, since no code is allowed to be all ones. 209e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 210e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (((INT32) code) >= (((INT32) 1) << _si)) 211e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 212e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov code <<= 1; 213e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov _si++; 214e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 215e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 216e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Figure F.15: generate decoding tables for bit-sequential decoding */ 217e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 218e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov p = 0; 219e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (l = 1; l <= 16; l++) { 220e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (htbl->bits[l]) { 221e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* valoffset[l] = huffval[] index of 1st symbol of code length l, 222e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * minus the minimum code of length l 223e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 224e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p]; 225e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov p += htbl->bits[l]; 226e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */ 227e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } else { 228e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov dtbl->maxcode[l] = -1; /* -1 if no codes of this length */ 229e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 230e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 231e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */ 232e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 233e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Compute lookahead tables to speed up decoding. 234e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * First we set all the table entries to 0, indicating "too long"; 235e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * then we iterate through the Huffman codes that are short enough and 236e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * fill in all the entries that correspond to bit sequences starting 237e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * with that code. 238e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 239e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 240e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits)); 241e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 242e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov p = 0; 243e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (l = 1; l <= HUFF_LOOKAHEAD; l++) { 244e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (i = 1; i <= (int) htbl->bits[l]; i++, p++) { 245e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* l = current code's length, p = its index in huffcode[] & huffval[]. */ 246e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Generate left-justified code followed by all possible bit sequences */ 247e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l); 248e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) { 249e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov dtbl->look_nbits[lookbits] = l; 250e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov dtbl->look_sym[lookbits] = htbl->huffval[p]; 251e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov lookbits++; 252e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 253e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 254e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 255e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 256e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Validate symbols as being reasonable. 257e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * For AC tables, we make no check, but accept all byte values 0..255. 258e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * For DC tables, we require the symbols to be in range 0..15. 259e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * (Tighter bounds could be applied depending on the data depth and mode, 260e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * but this is sufficient to ensure safe decoding.) 261e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 262e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (isDC) { 263e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (i = 0; i < numsymbols; i++) { 264e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov int sym = htbl->huffval[i]; 265e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (sym < 0 || sym > 15) 266e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); 267e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 268e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 269e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov} 270e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 271e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 272e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* 273e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Out-of-line code for bit fetching (shared with jdphuff.c). 274e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * See jdhuff.h for info about usage. 275e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Note: current values of get_buffer and bits_left are passed as parameters, 276e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * but are returned in the corresponding fields of the state struct. 277e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * 278e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width 279e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * of get_buffer to be used. (On machines with wider words, an even larger 280e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * buffer could be used.) However, on some machines 32-bit shifts are 281e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * quite slow and take time proportional to the number of places shifted. 282e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * (This is true with most PC compilers, for instance.) In this case it may 283e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the 284e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. 285e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 286e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 287e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#ifdef SLOW_SHIFT_32 288e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#define MIN_GET_BITS 15 /* minimum allowable value */ 289e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#else 290e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#define MIN_GET_BITS (BIT_BUF_SIZE-7) 291e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#endif 292e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 293e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 294e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovGLOBAL(boolean) 295e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovjpeg_fill_bit_buffer (bitread_working_state * state, 296e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov register bit_buf_type get_buffer, register int bits_left, 297e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov int nbits) 298e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* Load up the bit buffer to a depth of at least nbits */ 299e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{ 300e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Copy heavily used state fields into locals (hopefully registers) */ 301e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov register const JOCTET * next_input_byte = state->next_input_byte; 302e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov register size_t bytes_in_buffer = state->bytes_in_buffer; 303e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov j_decompress_ptr cinfo = state->cinfo; 304e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 305e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ 306e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* (It is assumed that no request will be for more than that many bits.) */ 307e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* We fail to do so only if we hit a marker or are forced to suspend. */ 308e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 309e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (cinfo->unread_marker == 0) { /* cannot advance past a marker */ 310e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov while (bits_left < MIN_GET_BITS) { 311e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov register int c; 312e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 313e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Attempt to read a byte */ 314e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (bytes_in_buffer == 0) { 315e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (! (*cinfo->src->fill_input_buffer) (cinfo)) 316e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov return FALSE; 317e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov next_input_byte = cinfo->src->next_input_byte; 318e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov bytes_in_buffer = cinfo->src->bytes_in_buffer; 319e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 320e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov bytes_in_buffer--; 321e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov c = GETJOCTET(*next_input_byte++); 322e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 323e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* If it's 0xFF, check and discard stuffed zero byte */ 324e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (c == 0xFF) { 325e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Loop here to discard any padding FF's on terminating marker, 326e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * so that we can save a valid unread_marker value. NOTE: we will 327e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * accept multiple FF's followed by a 0 as meaning a single FF data 328e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * byte. This data pattern is not valid according to the standard. 329e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 330e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov do { 331e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (bytes_in_buffer == 0) { 332e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (! (*cinfo->src->fill_input_buffer) (cinfo)) 333e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov return FALSE; 334e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov next_input_byte = cinfo->src->next_input_byte; 335e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov bytes_in_buffer = cinfo->src->bytes_in_buffer; 336e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 337e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov bytes_in_buffer--; 338e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov c = GETJOCTET(*next_input_byte++); 339e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } while (c == 0xFF); 340e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 341e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (c == 0) { 342e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Found FF/00, which represents an FF data byte */ 343e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov c = 0xFF; 344e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } else { 345e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Oops, it's actually a marker indicating end of compressed data. 346e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Save the marker code for later use. 347e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Fine point: it might appear that we should save the marker into 348e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * bitread working state, not straight into permanent state. But 349e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * once we have hit a marker, we cannot need to suspend within the 350e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * current MCU, because we will read no more bytes from the data 351e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * source. So it is OK to update permanent state right away. 352e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 353e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov cinfo->unread_marker = c; 354e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* See if we need to insert some fake zero bits. */ 355e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov goto no_more_bytes; 356e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 357e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 358e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 359e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* OK, load c into get_buffer */ 360e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov get_buffer = (get_buffer << 8) | c; 361e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov bits_left += 8; 362e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } /* end while */ 363e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } else { 364e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov no_more_bytes: 365e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* We get here if we've read the marker that terminates the compressed 366e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * data segment. There should be enough bits in the buffer register 367e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * to satisfy the request; if so, no problem. 368e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 369e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (nbits > bits_left) { 370e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Uh-oh. Report corrupted data to user and stuff zeroes into 371e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * the data stream, so that we can produce some kind of image. 372e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * We use a nonvolatile flag to ensure that only one warning message 373e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * appears per data segment. 374e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 375e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (! cinfo->entropy->insufficient_data) { 376e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov WARNMS(cinfo, JWRN_HIT_MARKER); 377e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov cinfo->entropy->insufficient_data = TRUE; 378e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 379e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Fill the buffer with zero bits */ 380e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov get_buffer <<= MIN_GET_BITS - bits_left; 381e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov bits_left = MIN_GET_BITS; 382e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 383e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 384e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 385e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Unload the local registers */ 386e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov state->next_input_byte = next_input_byte; 387e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov state->bytes_in_buffer = bytes_in_buffer; 388e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov state->get_buffer = get_buffer; 389e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov state->bits_left = bits_left; 390e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 391e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov return TRUE; 392e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov} 393e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 394e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 395e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* 396e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Out-of-line code for Huffman code decoding. 397e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * See jdhuff.h for info about usage. 398e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 399e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 400e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovGLOBAL(int) 401e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovjpeg_huff_decode (bitread_working_state * state, 402e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov register bit_buf_type get_buffer, register int bits_left, 403e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov d_derived_tbl * htbl, int min_bits) 404e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{ 405e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov register int l = min_bits; 406e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov register INT32 code; 407e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 408e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* HUFF_DECODE has determined that the code is at least min_bits */ 409e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* bits long, so fetch that many bits in one swoop. */ 410e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 411e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov CHECK_BIT_BUFFER(*state, l, return -1); 412e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov code = GET_BITS(l); 413e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 414e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Collect the rest of the Huffman code one bit at a time. */ 415e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* This is per Figure F.16 in the JPEG spec. */ 416e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 417e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov while (code > htbl->maxcode[l]) { 418e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov code <<= 1; 419e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov CHECK_BIT_BUFFER(*state, 1, return -1); 420e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov code |= GET_BITS(1); 421e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov l++; 422e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 423e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 424e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Unload the local registers */ 425e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov state->get_buffer = get_buffer; 426e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov state->bits_left = bits_left; 427e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 428e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* With garbage input we may reach the sentinel value l = 17. */ 429e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 430e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (l > 16) { 431e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE); 432e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov return 0; /* fake a zero as the safest result */ 433e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 434e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 435e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ]; 436e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov} 437e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 438e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 439e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* 440e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Figure F.12: extend sign bit. 441e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * On some machines, a shift and add will be faster than a table lookup. 442e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 443e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 444e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#ifdef AVOID_TABLES 445e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 446e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x)) 447e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 448e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#else 449e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 450e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) 451e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 452e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovstatic const int extend_test[16] = /* entry n is 2**(n-1) */ 453e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 454e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; 455e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 456e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovstatic const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ 457e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, 458e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, 459e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, 460e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 }; 461e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 462e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov#endif /* AVOID_TABLES */ 463e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 464e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 465e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* 466e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Check for a restart marker & resynchronize decoder. 467e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Returns FALSE if must suspend. 468e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 469e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 470e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovLOCAL(boolean) 471e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovprocess_restart (j_decompress_ptr cinfo) 472e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{ 473e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 474e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov int ci; 475e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 476e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Throw away any unused bits remaining in bit buffer; */ 477e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* include any full bytes in next_marker's count of discarded bytes */ 478e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; 479e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->bitstate.bits_left = 0; 480e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 481e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Advance past the RSTn marker */ 482e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (! (*cinfo->marker->read_restart_marker) (cinfo)) 483e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov return FALSE; 484e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 485e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Re-initialize DC predictions to 0 */ 486e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (ci = 0; ci < cinfo->comps_in_scan; ci++) 487e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->saved.last_dc_val[ci] = 0; 488e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 489e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Reset restart counter */ 490e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->restarts_to_go = cinfo->restart_interval; 491e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 492e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Reset out-of-data flag, unless read_restart_marker left us smack up 493e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * against a marker. In that case we will end up treating the next data 494e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * segment as empty, and we can avoid producing bogus output pixels by 495e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * leaving the flag set. 496e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 497e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (cinfo->unread_marker == 0) 498e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->pub.insufficient_data = FALSE; 499e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 500e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov return TRUE; 501e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov} 502e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 503e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 504e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* 505e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Decode and return one MCU's worth of Huffman-compressed coefficients. 506e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * The coefficients are reordered from zigzag order into natural array order, 507e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * but are not dequantized. 508e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * 509e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * The i'th block of the MCU is stored into the block pointed to by 510e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. 511e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * (Wholesale zeroing is usually a little faster than retail...) 512e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * 513e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Returns FALSE if data source requested suspension. In that case no 514e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * changes have been made to permanent state. (Exception: some output 515e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * coefficients may already have been assigned. This is harmless for 516e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * this module, since we'll just re-assign them on the next call.) 517e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 518e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 519e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovMETHODDEF(boolean) 520e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovdecode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) 521e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{ 522e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; 523e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov int blkn; 524e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov BITREAD_STATE_VARS; 525e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov savable_state state; 526e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 527e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Process restart marker if needed; may have to suspend */ 528e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (cinfo->restart_interval) { 529e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (entropy->restarts_to_go == 0) 530e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (! process_restart(cinfo)) 531e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov return FALSE; 532e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 533e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 534e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* If we've run out of data, just leave the MCU set to zeroes. 535e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * This way, we return uniform gray for the remainder of the segment. 536e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 537e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (! entropy->pub.insufficient_data) { 538e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 539e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Load up working state */ 540e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov BITREAD_LOAD_STATE(cinfo,entropy->bitstate); 541e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ASSIGN_STATE(state, entropy->saved); 542e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 543e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Outer loop handles each block in the MCU */ 544e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 545e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { 546e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov JBLOCKROW block = MCU_data[blkn]; 547e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn]; 548e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn]; 549e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov register int s, k, r; 550e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 551e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Decode a single block's worth of coefficients */ 552e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 553e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Section F.2.2.1: decode the DC coefficient difference */ 554e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov HUFF_DECODE(s, br_state, dctbl, return FALSE, label1); 555e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (s) { 556e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov CHECK_BIT_BUFFER(br_state, s, return FALSE); 557e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov r = GET_BITS(s); 558e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov s = HUFF_EXTEND(r, s); 559e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 560e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 561e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (entropy->dc_needed[blkn]) { 562e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Convert DC difference to actual value, update last_dc_val */ 563e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov int ci = cinfo->MCU_membership[blkn]; 564e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov s += state.last_dc_val[ci]; 565e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov state.last_dc_val[ci] = s; 566e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */ 567e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov (*block)[0] = (JCOEF) s; 568e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 569e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 570e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (entropy->ac_needed[blkn]) { 571e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 572e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Section F.2.2.2: decode the AC coefficients */ 573e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Since zeroes are skipped, output area must be cleared beforehand */ 574e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (k = 1; k < DCTSIZE2; k++) { 575e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov HUFF_DECODE(s, br_state, actbl, return FALSE, label2); 576e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 577e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov r = s >> 4; 578e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov s &= 15; 579e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 580e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (s) { 581e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov k += r; 582e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov CHECK_BIT_BUFFER(br_state, s, return FALSE); 583e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov r = GET_BITS(s); 584e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov s = HUFF_EXTEND(r, s); 585e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Output coefficient in natural (dezigzagged) order. 586e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Note: the extra entries in jpeg_natural_order[] will save us 587e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * if k >= DCTSIZE2, which could happen if the data is corrupted. 588e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 589e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov (*block)[jpeg_natural_order[k]] = (JCOEF) s; 590e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } else { 591e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (r != 15) 592e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov break; 593e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov k += 15; 594e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 595e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 596e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 597e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } else { 598e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 599e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Section F.2.2.2: decode the AC coefficients */ 600e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* In this path we just discard the values */ 601e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (k = 1; k < DCTSIZE2; k++) { 602e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov HUFF_DECODE(s, br_state, actbl, return FALSE, label3); 603e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 604e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov r = s >> 4; 605e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov s &= 15; 606e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 607e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (s) { 608e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov k += r; 609e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov CHECK_BIT_BUFFER(br_state, s, return FALSE); 610e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov DROP_BITS(s); 611e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } else { 612e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov if (r != 15) 613e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov break; 614e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov k += 15; 615e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 616e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 617e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 618e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 619e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 620e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 621e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Completed MCU, so update state */ 622e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov BITREAD_SAVE_STATE(cinfo,entropy->bitstate); 623e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov ASSIGN_STATE(entropy->saved, state); 624e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 625e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 626e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Account for restart interval (no-op if not using restarts) */ 627e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->restarts_to_go--; 628e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 629e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov return TRUE; 630e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov} 631e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 632e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 633e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov/* 634e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov * Module initialization routine for Huffman entropy decoding. 635e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov */ 636e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 637e6986e1e8d4a57987f47c215490cb080a65ee29aSvet GanovGLOBAL(void) 638e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganovjinit_huff_decoder (j_decompress_ptr cinfo) 639e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov{ 640e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov huff_entropy_ptr entropy; 641e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov int i; 642e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 643e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy = (huff_entropy_ptr) 644e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 645e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov SIZEOF(huff_entropy_decoder)); 646e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov cinfo->entropy = (struct jpeg_entropy_decoder *) entropy; 647e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->pub.start_pass = start_pass_huff_decoder; 648e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->pub.decode_mcu = decode_mcu; 649e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov 650e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov /* Mark tables unallocated */ 651e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov for (i = 0; i < NUM_HUFF_TBLS; i++) { 652e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; 653e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov } 654e6986e1e8d4a57987f47c215490cb080a65ee29aSvet Ganov} 655