michael@0: /* michael@0: * jdhuff.h 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: * Modifications: michael@0: * Copyright (C) 2010-2011, D. R. Commander. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains declarations for Huffman entropy decoding routines michael@0: * that are shared between the sequential decoder (jdhuff.c) and the michael@0: * progressive decoder (jdphuff.c). No other modules need to see these. michael@0: */ michael@0: michael@0: /* Short forms of external names for systems with brain-damaged linkers. */ michael@0: michael@0: #ifdef NEED_SHORT_EXTERNAL_NAMES michael@0: #define jpeg_make_d_derived_tbl jMkDDerived michael@0: #define jpeg_fill_bit_buffer jFilBitBuf michael@0: #define jpeg_huff_decode jHufDecode michael@0: #endif /* NEED_SHORT_EXTERNAL_NAMES */ michael@0: michael@0: michael@0: /* Derived data constructed for each Huffman table */ michael@0: michael@0: #define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */ michael@0: michael@0: typedef struct { michael@0: /* Basic tables: (element [0] of each array is unused) */ michael@0: INT32 maxcode[18]; /* largest code of length k (-1 if none) */ michael@0: /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */ michael@0: INT32 valoffset[18]; /* huffval[] offset for codes of length k */ michael@0: /* valoffset[k] = huffval[] index of 1st symbol of code length k, less michael@0: * the smallest code of length k; so given a code of length k, the michael@0: * corresponding symbol is huffval[code + valoffset[k]] michael@0: */ michael@0: michael@0: /* Link to public Huffman table (needed only in jpeg_huff_decode) */ michael@0: JHUFF_TBL *pub; michael@0: michael@0: /* Lookahead table: indexed by the next HUFF_LOOKAHEAD bits of michael@0: * the input data stream. If the next Huffman code is no more michael@0: * than HUFF_LOOKAHEAD bits long, we can obtain its length and michael@0: * the corresponding symbol directly from this tables. michael@0: * michael@0: * The lower 8 bits of each table entry contain the number of michael@0: * bits in the corresponding Huffman code, or HUFF_LOOKAHEAD + 1 michael@0: * if too long. The next 8 bits of each entry contain the michael@0: * symbol. michael@0: */ michael@0: int lookup[1< 32 bits on your machine, and shifting/masking longs is michael@0: * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE michael@0: * appropriately should be a win. Unfortunately we can't define the size michael@0: * with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8) michael@0: * because not all machines measure sizeof in 8-bit bytes. michael@0: */ michael@0: michael@0: typedef struct { /* Bitreading state saved across MCUs */ michael@0: bit_buf_type get_buffer; /* current bit-extraction buffer */ michael@0: int bits_left; /* # of unused bits in it */ michael@0: } bitread_perm_state; michael@0: michael@0: typedef struct { /* Bitreading working state within an MCU */ michael@0: /* Current data source location */ michael@0: /* We need a copy, rather than munging the original, in case of suspension */ michael@0: const JOCTET * next_input_byte; /* => next byte to read from source */ michael@0: size_t bytes_in_buffer; /* # of bytes remaining in source buffer */ michael@0: /* Bit input buffer --- note these values are kept in register variables, michael@0: * not in this struct, inside the inner loops. michael@0: */ michael@0: bit_buf_type get_buffer; /* current bit-extraction buffer */ michael@0: int bits_left; /* # of unused bits in it */ michael@0: /* Pointer needed by jpeg_fill_bit_buffer. */ michael@0: j_decompress_ptr cinfo; /* back link to decompress master record */ michael@0: } bitread_working_state; michael@0: michael@0: /* Macros to declare and load/save bitread local variables. */ michael@0: #define BITREAD_STATE_VARS \ michael@0: register bit_buf_type get_buffer; \ michael@0: register int bits_left; \ michael@0: bitread_working_state br_state michael@0: michael@0: #define BITREAD_LOAD_STATE(cinfop,permstate) \ michael@0: br_state.cinfo = cinfop; \ michael@0: br_state.next_input_byte = cinfop->src->next_input_byte; \ michael@0: br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \ michael@0: get_buffer = permstate.get_buffer; \ michael@0: bits_left = permstate.bits_left; michael@0: michael@0: #define BITREAD_SAVE_STATE(cinfop,permstate) \ michael@0: cinfop->src->next_input_byte = br_state.next_input_byte; \ michael@0: cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \ michael@0: permstate.get_buffer = get_buffer; \ michael@0: permstate.bits_left = bits_left michael@0: michael@0: /* michael@0: * These macros provide the in-line portion of bit fetching. michael@0: * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer michael@0: * before using GET_BITS, PEEK_BITS, or DROP_BITS. michael@0: * The variables get_buffer and bits_left are assumed to be locals, michael@0: * but the state struct might not be (jpeg_huff_decode needs this). michael@0: * CHECK_BIT_BUFFER(state,n,action); michael@0: * Ensure there are N bits in get_buffer; if suspend, take action. michael@0: * val = GET_BITS(n); michael@0: * Fetch next N bits. michael@0: * val = PEEK_BITS(n); michael@0: * Fetch next N bits without removing them from the buffer. michael@0: * DROP_BITS(n); michael@0: * Discard next N bits. michael@0: * The value N should be a simple variable, not an expression, because it michael@0: * is evaluated multiple times. michael@0: */ michael@0: michael@0: #define CHECK_BIT_BUFFER(state,nbits,action) \ michael@0: { if (bits_left < (nbits)) { \ michael@0: if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits)) \ michael@0: { action; } \ michael@0: get_buffer = (state).get_buffer; bits_left = (state).bits_left; } } michael@0: michael@0: #define GET_BITS(nbits) \ michael@0: (((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1)) michael@0: michael@0: #define PEEK_BITS(nbits) \ michael@0: (((int) (get_buffer >> (bits_left - (nbits)))) & ((1<<(nbits))-1)) michael@0: michael@0: #define DROP_BITS(nbits) \ michael@0: (bits_left -= (nbits)) michael@0: michael@0: /* Load up the bit buffer to a depth of at least nbits */ michael@0: EXTERN(boolean) jpeg_fill_bit_buffer michael@0: JPP((bitread_working_state * state, register bit_buf_type get_buffer, michael@0: register int bits_left, int nbits)); michael@0: michael@0: michael@0: /* michael@0: * Code for extracting next Huffman-coded symbol from input bit stream. michael@0: * Again, this is time-critical and we make the main paths be macros. michael@0: * michael@0: * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits michael@0: * without looping. Usually, more than 95% of the Huffman codes will be 8 michael@0: * or fewer bits long. The few overlength codes are handled with a loop, michael@0: * which need not be inline code. michael@0: * michael@0: * Notes about the HUFF_DECODE macro: michael@0: * 1. Near the end of the data segment, we may fail to get enough bits michael@0: * for a lookahead. In that case, we do it the hard way. michael@0: * 2. If the lookahead table contains no entry, the next code must be michael@0: * more than HUFF_LOOKAHEAD bits long. michael@0: * 3. jpeg_huff_decode returns -1 if forced to suspend. michael@0: */ michael@0: michael@0: #define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \ michael@0: { register int nb, look; \ michael@0: if (bits_left < HUFF_LOOKAHEAD) { \ michael@0: if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \ michael@0: get_buffer = state.get_buffer; bits_left = state.bits_left; \ michael@0: if (bits_left < HUFF_LOOKAHEAD) { \ michael@0: nb = 1; goto slowlabel; \ michael@0: } \ michael@0: } \ michael@0: look = PEEK_BITS(HUFF_LOOKAHEAD); \ michael@0: if ((nb = (htbl->lookup[look] >> HUFF_LOOKAHEAD)) <= HUFF_LOOKAHEAD) { \ michael@0: DROP_BITS(nb); \ michael@0: result = htbl->lookup[look] & ((1 << HUFF_LOOKAHEAD) - 1); \ michael@0: } else { \ michael@0: slowlabel: \ michael@0: if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \ michael@0: { failaction; } \ michael@0: get_buffer = state.get_buffer; bits_left = state.bits_left; \ michael@0: } \ michael@0: } michael@0: michael@0: #define HUFF_DECODE_FAST(s,nb,htbl) \ michael@0: FILL_BIT_BUFFER_FAST; \ michael@0: s = PEEK_BITS(HUFF_LOOKAHEAD); \ michael@0: s = htbl->lookup[s]; \ michael@0: nb = s >> HUFF_LOOKAHEAD; \ michael@0: /* Pre-execute the common case of nb <= HUFF_LOOKAHEAD */ \ michael@0: DROP_BITS(nb); \ michael@0: s = s & ((1 << HUFF_LOOKAHEAD) - 1); \ michael@0: if (nb > HUFF_LOOKAHEAD) { \ michael@0: /* Equivalent of jpeg_huff_decode() */ \ michael@0: /* Don't use GET_BITS() here because we don't want to modify bits_left */ \ michael@0: s = (get_buffer >> bits_left) & ((1 << (nb)) - 1); \ michael@0: while (s > htbl->maxcode[nb]) { \ michael@0: s <<= 1; \ michael@0: s |= GET_BITS(1); \ michael@0: nb++; \ michael@0: } \ michael@0: s = htbl->pub->huffval[ (int) (s + htbl->valoffset[nb]) & 0xFF ]; \ michael@0: } michael@0: michael@0: /* Out-of-line case for Huffman code fetching */ michael@0: EXTERN(int) jpeg_huff_decode michael@0: JPP((bitread_working_state * state, register bit_buf_type get_buffer, michael@0: register int bits_left, d_derived_tbl * htbl, int min_bits));