shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * jdhuff.h
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
shun-iwasawa 82a8f5
 * Copyright (C) 1991-1997, Thomas G. Lane.
shun-iwasawa 82a8f5
 * libjpeg-turbo Modifications:
shun-iwasawa 82a8f5
 * Copyright (C) 2010-2011, 2015-2016, D. R. Commander.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file contains declarations for Huffman entropy decoding routines
shun-iwasawa 82a8f5
 * that are shared between the sequential decoder (jdhuff.c) and the
shun-iwasawa 82a8f5
 * progressive decoder (jdphuff.c).  No other modules need to see these.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#include "jconfigint.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Derived data constructed for each Huffman table */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define HUFF_LOOKAHEAD  8       /* # of bits of lookahead */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct {
shun-iwasawa 82a8f5
  /* Basic tables: (element [0] of each array is unused) */
shun-iwasawa 82a8f5
  JLONG maxcode[18];            /* largest code of length k (-1 if none) */
shun-iwasawa 82a8f5
  /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
shun-iwasawa 82a8f5
  JLONG valoffset[18];          /* huffval[] offset for codes of length k */
shun-iwasawa 82a8f5
  /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
shun-iwasawa 82a8f5
   * the smallest code of length k; so given a code of length k, the
shun-iwasawa 82a8f5
   * corresponding symbol is huffval[code + valoffset[k]]
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Link to public Huffman table (needed only in jpeg_huff_decode) */
shun-iwasawa 82a8f5
  JHUFF_TBL *pub;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Lookahead table: indexed by the next HUFF_LOOKAHEAD bits of
shun-iwasawa 82a8f5
   * the input data stream.  If the next Huffman code is no more
shun-iwasawa 82a8f5
   * than HUFF_LOOKAHEAD bits long, we can obtain its length and
shun-iwasawa 82a8f5
   * the corresponding symbol directly from this tables.
shun-iwasawa 82a8f5
   *
shun-iwasawa 82a8f5
   * The lower 8 bits of each table entry contain the number of
shun-iwasawa 82a8f5
   * bits in the corresponding Huffman code, or HUFF_LOOKAHEAD + 1
shun-iwasawa 82a8f5
   * if too long.  The next 8 bits of each entry contain the
shun-iwasawa 82a8f5
   * symbol.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  int lookup[1 << HUFF_LOOKAHEAD];
shun-iwasawa 82a8f5
} d_derived_tbl;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Expand a Huffman table definition into the derived format */
shun-iwasawa 82a8f5
EXTERN(void) jpeg_make_d_derived_tbl(j_decompress_ptr cinfo, boolean isDC,
shun-iwasawa 82a8f5
                                     int tblno, d_derived_tbl **pdtbl);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Fetching the next N bits from the input stream is a time-critical operation
shun-iwasawa 82a8f5
 * for the Huffman decoders.  We implement it with a combination of inline
shun-iwasawa 82a8f5
 * macros and out-of-line subroutines.  Note that N (the number of bits
shun-iwasawa 82a8f5
 * demanded at one time) never exceeds 15 for JPEG use.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * We read source bytes into get_buffer and dole out bits as needed.
shun-iwasawa 82a8f5
 * If get_buffer already contains enough bits, they are fetched in-line
shun-iwasawa 82a8f5
 * by the macros CHECK_BIT_BUFFER and GET_BITS.  When there aren't enough
shun-iwasawa 82a8f5
 * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
shun-iwasawa 82a8f5
 * as full as possible (not just to the number of bits needed; this
shun-iwasawa 82a8f5
 * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
shun-iwasawa 82a8f5
 * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
shun-iwasawa 82a8f5
 * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
shun-iwasawa 82a8f5
 * at least the requested number of bits --- dummy zeroes are inserted if
shun-iwasawa 82a8f5
 * necessary.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if !defined(_WIN32) && !defined(SIZEOF_SIZE_T)
shun-iwasawa 82a8f5
#error Cannot determine word size
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#if SIZEOF_SIZE_T == 8 || defined(_WIN64)
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef size_t bit_buf_type;            /* type of bit-extraction buffer */
shun-iwasawa 82a8f5
#define BIT_BUF_SIZE  64                /* size of buffer in bits */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#else
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef unsigned long bit_buf_type;     /* type of bit-extraction buffer */
shun-iwasawa 82a8f5
#define BIT_BUF_SIZE  32                /* size of buffer in bits */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* If long is > 32 bits on your machine, and shifting/masking longs is
shun-iwasawa 82a8f5
 * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
shun-iwasawa 82a8f5
 * appropriately should be a win.  Unfortunately we can't define the size
shun-iwasawa 82a8f5
 * with something like  #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
shun-iwasawa 82a8f5
 * because not all machines measure sizeof in 8-bit bytes.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct {                /* Bitreading state saved across MCUs */
shun-iwasawa 82a8f5
  bit_buf_type get_buffer;      /* current bit-extraction buffer */
shun-iwasawa 82a8f5
  int bits_left;                /* # of unused bits in it */
shun-iwasawa 82a8f5
} bitread_perm_state;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
typedef struct {                /* Bitreading working state within an MCU */
shun-iwasawa 82a8f5
  /* Current data source location */
shun-iwasawa 82a8f5
  /* We need a copy, rather than munging the original, in case of suspension */
shun-iwasawa 82a8f5
  const JOCTET *next_input_byte; /* => next byte to read from source */
shun-iwasawa 82a8f5
  size_t bytes_in_buffer;       /* # of bytes remaining in source buffer */
shun-iwasawa 82a8f5
  /* Bit input buffer --- note these values are kept in register variables,
shun-iwasawa 82a8f5
   * not in this struct, inside the inner loops.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
  bit_buf_type get_buffer;      /* current bit-extraction buffer */
shun-iwasawa 82a8f5
  int bits_left;                /* # of unused bits in it */
shun-iwasawa 82a8f5
  /* Pointer needed by jpeg_fill_bit_buffer. */
shun-iwasawa 82a8f5
  j_decompress_ptr cinfo;       /* back link to decompress master record */
shun-iwasawa 82a8f5
} bitread_working_state;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Macros to declare and load/save bitread local variables. */
shun-iwasawa 82a8f5
#define BITREAD_STATE_VARS \
shun-iwasawa 82a8f5
  register bit_buf_type get_buffer; \
shun-iwasawa 82a8f5
  register int bits_left; \
shun-iwasawa 82a8f5
  bitread_working_state br_state
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define BITREAD_LOAD_STATE(cinfop, permstate) \
shun-iwasawa 82a8f5
  br_state.cinfo = cinfop; \
shun-iwasawa 82a8f5
  br_state.next_input_byte = cinfop->src->next_input_byte; \
shun-iwasawa 82a8f5
  br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
shun-iwasawa 82a8f5
  get_buffer = permstate.get_buffer; \
shun-iwasawa 82a8f5
  bits_left = permstate.bits_left;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define BITREAD_SAVE_STATE(cinfop, permstate) \
shun-iwasawa 82a8f5
  cinfop->src->next_input_byte = br_state.next_input_byte; \
shun-iwasawa 82a8f5
  cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
shun-iwasawa 82a8f5
  permstate.get_buffer = get_buffer; \
shun-iwasawa 82a8f5
  permstate.bits_left = bits_left
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * These macros provide the in-line portion of bit fetching.
shun-iwasawa 82a8f5
 * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
shun-iwasawa 82a8f5
 * before using GET_BITS, PEEK_BITS, or DROP_BITS.
shun-iwasawa 82a8f5
 * The variables get_buffer and bits_left are assumed to be locals,
shun-iwasawa 82a8f5
 * but the state struct might not be (jpeg_huff_decode needs this).
shun-iwasawa 82a8f5
 *      CHECK_BIT_BUFFER(state, n, action);
shun-iwasawa 82a8f5
 *              Ensure there are N bits in get_buffer; if suspend, take action.
shun-iwasawa 82a8f5
 *      val = GET_BITS(n);
shun-iwasawa 82a8f5
 *              Fetch next N bits.
shun-iwasawa 82a8f5
 *      val = PEEK_BITS(n);
shun-iwasawa 82a8f5
 *              Fetch next N bits without removing them from the buffer.
shun-iwasawa 82a8f5
 *      DROP_BITS(n);
shun-iwasawa 82a8f5
 *              Discard next N bits.
shun-iwasawa 82a8f5
 * The value N should be a simple variable, not an expression, because it
shun-iwasawa 82a8f5
 * is evaluated multiple times.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define CHECK_BIT_BUFFER(state, nbits, action) { \
shun-iwasawa 82a8f5
  if (bits_left < (nbits)) { \
shun-iwasawa 82a8f5
    if (!jpeg_fill_bit_buffer(&(state), get_buffer, bits_left, nbits)) \
shun-iwasawa 82a8f5
      { action; } \
shun-iwasawa 82a8f5
    get_buffer = (state).get_buffer;  bits_left = (state).bits_left; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define GET_BITS(nbits) \
shun-iwasawa 82a8f5
  (((int)(get_buffer >> (bits_left -= (nbits)))) & ((1 << (nbits)) - 1))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define PEEK_BITS(nbits) \
shun-iwasawa 82a8f5
  (((int)(get_buffer >> (bits_left -  (nbits)))) & ((1 << (nbits)) - 1))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define DROP_BITS(nbits) \
shun-iwasawa 82a8f5
  (bits_left -= (nbits))
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Load up the bit buffer to a depth of at least nbits */
shun-iwasawa 82a8f5
EXTERN(boolean) jpeg_fill_bit_buffer(bitread_working_state *state,
shun-iwasawa 82a8f5
                                     register bit_buf_type get_buffer,
shun-iwasawa 82a8f5
                                     register int bits_left, int nbits);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Code for extracting next Huffman-coded symbol from input bit stream.
shun-iwasawa 82a8f5
 * Again, this is time-critical and we make the main paths be macros.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
shun-iwasawa 82a8f5
 * without looping.  Usually, more than 95% of the Huffman codes will be 8
shun-iwasawa 82a8f5
 * or fewer bits long.  The few overlength codes are handled with a loop,
shun-iwasawa 82a8f5
 * which need not be inline code.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Notes about the HUFF_DECODE macro:
shun-iwasawa 82a8f5
 * 1. Near the end of the data segment, we may fail to get enough bits
shun-iwasawa 82a8f5
 *    for a lookahead.  In that case, we do it the hard way.
shun-iwasawa 82a8f5
 * 2. If the lookahead table contains no entry, the next code must be
shun-iwasawa 82a8f5
 *    more than HUFF_LOOKAHEAD bits long.
shun-iwasawa 82a8f5
 * 3. jpeg_huff_decode returns -1 if forced to suspend.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define HUFF_DECODE(result, state, htbl, failaction, slowlabel) { \
shun-iwasawa 82a8f5
  register int nb, look; \
shun-iwasawa 82a8f5
  if (bits_left < HUFF_LOOKAHEAD) { \
shun-iwasawa 82a8f5
    if (!jpeg_fill_bit_buffer(&state, get_buffer, bits_left, 0)) \
shun-iwasawa 82a8f5
      { failaction; } \
shun-iwasawa 82a8f5
    get_buffer = state.get_buffer;  bits_left = state.bits_left; \
shun-iwasawa 82a8f5
    if (bits_left < HUFF_LOOKAHEAD) { \
shun-iwasawa 82a8f5
      nb = 1;  goto slowlabel; \
shun-iwasawa 82a8f5
    } \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
  look = PEEK_BITS(HUFF_LOOKAHEAD); \
shun-iwasawa 82a8f5
  if ((nb = (htbl->lookup[look] >> HUFF_LOOKAHEAD)) <= HUFF_LOOKAHEAD) { \
shun-iwasawa 82a8f5
    DROP_BITS(nb); \
shun-iwasawa 82a8f5
    result = htbl->lookup[look] & ((1 << HUFF_LOOKAHEAD) - 1); \
shun-iwasawa 82a8f5
  } else { \
shun-iwasawa 82a8f5
slowlabel: \
shun-iwasawa 82a8f5
    if ((result = \
shun-iwasawa 82a8f5
         jpeg_huff_decode(&state, get_buffer, bits_left, htbl, nb)) < 0) \
shun-iwasawa 82a8f5
      { failaction; } \
shun-iwasawa 82a8f5
    get_buffer = state.get_buffer;  bits_left = state.bits_left; \
shun-iwasawa 82a8f5
  } \
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define HUFF_DECODE_FAST(s, nb, htbl) \
shun-iwasawa 82a8f5
  FILL_BIT_BUFFER_FAST; \
shun-iwasawa 82a8f5
  s = PEEK_BITS(HUFF_LOOKAHEAD); \
shun-iwasawa 82a8f5
  s = htbl->lookup[s]; \
shun-iwasawa 82a8f5
  nb = s >> HUFF_LOOKAHEAD; \
shun-iwasawa 82a8f5
  /* Pre-execute the common case of nb <= HUFF_LOOKAHEAD */ \
shun-iwasawa 82a8f5
  DROP_BITS(nb); \
shun-iwasawa 82a8f5
  s = s & ((1 << HUFF_LOOKAHEAD) - 1); \
shun-iwasawa 82a8f5
  if (nb > HUFF_LOOKAHEAD) { \
shun-iwasawa 82a8f5
    /* Equivalent of jpeg_huff_decode() */ \
shun-iwasawa 82a8f5
    /* Don't use GET_BITS() here because we don't want to modify bits_left */ \
shun-iwasawa 82a8f5
    s = (get_buffer >> bits_left) & ((1 << (nb)) - 1); \
shun-iwasawa 82a8f5
    while (s > htbl->maxcode[nb]) { \
shun-iwasawa 82a8f5
      s <<= 1; \
shun-iwasawa 82a8f5
      s |= GET_BITS(1); \
shun-iwasawa 82a8f5
      nb++; \
shun-iwasawa 82a8f5
    } \
shun-iwasawa 82a8f5
    s = htbl->pub->huffval[(int)(s + htbl->valoffset[nb]) & 0xFF]; \
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/* Out-of-line case for Huffman code fetching */
shun-iwasawa 82a8f5
EXTERN(int) jpeg_huff_decode(bitread_working_state *state,
shun-iwasawa 82a8f5
                             register bit_buf_type get_buffer,
shun-iwasawa 82a8f5
                             register int bits_left, d_derived_tbl *htbl,
shun-iwasawa 82a8f5
                             int min_bits);