media/libjpeg/jdhuff.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libjpeg/jdhuff.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,235 @@
     1.4 +/*
     1.5 + * jdhuff.h
     1.6 + *
     1.7 + * This file was part of the Independent JPEG Group's software:
     1.8 + * Copyright (C) 1991-1997, Thomas G. Lane.
     1.9 + * Modifications:
    1.10 + * Copyright (C) 2010-2011, D. R. Commander.
    1.11 + * For conditions of distribution and use, see the accompanying README file.
    1.12 + *
    1.13 + * This file contains declarations for Huffman entropy decoding routines
    1.14 + * that are shared between the sequential decoder (jdhuff.c) and the
    1.15 + * progressive decoder (jdphuff.c).  No other modules need to see these.
    1.16 + */
    1.17 +
    1.18 +/* Short forms of external names for systems with brain-damaged linkers. */
    1.19 +
    1.20 +#ifdef NEED_SHORT_EXTERNAL_NAMES
    1.21 +#define jpeg_make_d_derived_tbl	jMkDDerived
    1.22 +#define jpeg_fill_bit_buffer	jFilBitBuf
    1.23 +#define jpeg_huff_decode	jHufDecode
    1.24 +#endif /* NEED_SHORT_EXTERNAL_NAMES */
    1.25 +
    1.26 +
    1.27 +/* Derived data constructed for each Huffman table */
    1.28 +
    1.29 +#define HUFF_LOOKAHEAD	8	/* # of bits of lookahead */
    1.30 +
    1.31 +typedef struct {
    1.32 +  /* Basic tables: (element [0] of each array is unused) */
    1.33 +  INT32 maxcode[18];		/* largest code of length k (-1 if none) */
    1.34 +  /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
    1.35 +  INT32 valoffset[18];		/* huffval[] offset for codes of length k */
    1.36 +  /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
    1.37 +   * the smallest code of length k; so given a code of length k, the
    1.38 +   * corresponding symbol is huffval[code + valoffset[k]]
    1.39 +   */
    1.40 +
    1.41 +  /* Link to public Huffman table (needed only in jpeg_huff_decode) */
    1.42 +  JHUFF_TBL *pub;
    1.43 +
    1.44 +  /* Lookahead table: indexed by the next HUFF_LOOKAHEAD bits of
    1.45 +   * the input data stream.  If the next Huffman code is no more
    1.46 +   * than HUFF_LOOKAHEAD bits long, we can obtain its length and
    1.47 +   * the corresponding symbol directly from this tables.
    1.48 +   *
    1.49 +   * The lower 8 bits of each table entry contain the number of
    1.50 +   * bits in the corresponding Huffman code, or HUFF_LOOKAHEAD + 1
    1.51 +   * if too long.  The next 8 bits of each entry contain the
    1.52 +   * symbol.
    1.53 +   */
    1.54 +  int lookup[1<<HUFF_LOOKAHEAD];
    1.55 +} d_derived_tbl;
    1.56 +
    1.57 +/* Expand a Huffman table definition into the derived format */
    1.58 +EXTERN(void) jpeg_make_d_derived_tbl
    1.59 +	JPP((j_decompress_ptr cinfo, boolean isDC, int tblno,
    1.60 +	     d_derived_tbl ** pdtbl));
    1.61 +
    1.62 +
    1.63 +/*
    1.64 + * Fetching the next N bits from the input stream is a time-critical operation
    1.65 + * for the Huffman decoders.  We implement it with a combination of inline
    1.66 + * macros and out-of-line subroutines.  Note that N (the number of bits
    1.67 + * demanded at one time) never exceeds 15 for JPEG use.
    1.68 + *
    1.69 + * We read source bytes into get_buffer and dole out bits as needed.
    1.70 + * If get_buffer already contains enough bits, they are fetched in-line
    1.71 + * by the macros CHECK_BIT_BUFFER and GET_BITS.  When there aren't enough
    1.72 + * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
    1.73 + * as full as possible (not just to the number of bits needed; this
    1.74 + * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
    1.75 + * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
    1.76 + * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
    1.77 + * at least the requested number of bits --- dummy zeroes are inserted if
    1.78 + * necessary.
    1.79 + */
    1.80 +
    1.81 +#if __WORDSIZE == 64 || defined(_WIN64)
    1.82 +
    1.83 +typedef size_t bit_buf_type;	/* type of bit-extraction buffer */
    1.84 +#define BIT_BUF_SIZE  64		/* size of buffer in bits */
    1.85 +
    1.86 +#else
    1.87 +
    1.88 +typedef INT32 bit_buf_type;	/* type of bit-extraction buffer */
    1.89 +#define BIT_BUF_SIZE  32		/* size of buffer in bits */
    1.90 +
    1.91 +#endif
    1.92 +
    1.93 +/* If long is > 32 bits on your machine, and shifting/masking longs is
    1.94 + * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
    1.95 + * appropriately should be a win.  Unfortunately we can't define the size
    1.96 + * with something like  #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
    1.97 + * because not all machines measure sizeof in 8-bit bytes.
    1.98 + */
    1.99 +
   1.100 +typedef struct {		/* Bitreading state saved across MCUs */
   1.101 +  bit_buf_type get_buffer;	/* current bit-extraction buffer */
   1.102 +  int bits_left;		/* # of unused bits in it */
   1.103 +} bitread_perm_state;
   1.104 +
   1.105 +typedef struct {		/* Bitreading working state within an MCU */
   1.106 +  /* Current data source location */
   1.107 +  /* We need a copy, rather than munging the original, in case of suspension */
   1.108 +  const JOCTET * next_input_byte; /* => next byte to read from source */
   1.109 +  size_t bytes_in_buffer;	/* # of bytes remaining in source buffer */
   1.110 +  /* Bit input buffer --- note these values are kept in register variables,
   1.111 +   * not in this struct, inside the inner loops.
   1.112 +   */
   1.113 +  bit_buf_type get_buffer;	/* current bit-extraction buffer */
   1.114 +  int bits_left;		/* # of unused bits in it */
   1.115 +  /* Pointer needed by jpeg_fill_bit_buffer. */
   1.116 +  j_decompress_ptr cinfo;	/* back link to decompress master record */
   1.117 +} bitread_working_state;
   1.118 +
   1.119 +/* Macros to declare and load/save bitread local variables. */
   1.120 +#define BITREAD_STATE_VARS  \
   1.121 +	register bit_buf_type get_buffer;  \
   1.122 +	register int bits_left;  \
   1.123 +	bitread_working_state br_state
   1.124 +
   1.125 +#define BITREAD_LOAD_STATE(cinfop,permstate)  \
   1.126 +	br_state.cinfo = cinfop; \
   1.127 +	br_state.next_input_byte = cinfop->src->next_input_byte; \
   1.128 +	br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
   1.129 +	get_buffer = permstate.get_buffer; \
   1.130 +	bits_left = permstate.bits_left;
   1.131 +
   1.132 +#define BITREAD_SAVE_STATE(cinfop,permstate)  \
   1.133 +	cinfop->src->next_input_byte = br_state.next_input_byte; \
   1.134 +	cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
   1.135 +	permstate.get_buffer = get_buffer; \
   1.136 +	permstate.bits_left = bits_left
   1.137 +
   1.138 +/*
   1.139 + * These macros provide the in-line portion of bit fetching.
   1.140 + * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
   1.141 + * before using GET_BITS, PEEK_BITS, or DROP_BITS.
   1.142 + * The variables get_buffer and bits_left are assumed to be locals,
   1.143 + * but the state struct might not be (jpeg_huff_decode needs this).
   1.144 + *	CHECK_BIT_BUFFER(state,n,action);
   1.145 + *		Ensure there are N bits in get_buffer; if suspend, take action.
   1.146 + *      val = GET_BITS(n);
   1.147 + *		Fetch next N bits.
   1.148 + *      val = PEEK_BITS(n);
   1.149 + *		Fetch next N bits without removing them from the buffer.
   1.150 + *	DROP_BITS(n);
   1.151 + *		Discard next N bits.
   1.152 + * The value N should be a simple variable, not an expression, because it
   1.153 + * is evaluated multiple times.
   1.154 + */
   1.155 +
   1.156 +#define CHECK_BIT_BUFFER(state,nbits,action) \
   1.157 +	{ if (bits_left < (nbits)) {  \
   1.158 +	    if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \
   1.159 +	      { action; }  \
   1.160 +	    get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
   1.161 +
   1.162 +#define GET_BITS(nbits) \
   1.163 +	(((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1))
   1.164 +
   1.165 +#define PEEK_BITS(nbits) \
   1.166 +	(((int) (get_buffer >> (bits_left -  (nbits)))) & ((1<<(nbits))-1))
   1.167 +
   1.168 +#define DROP_BITS(nbits) \
   1.169 +	(bits_left -= (nbits))
   1.170 +
   1.171 +/* Load up the bit buffer to a depth of at least nbits */
   1.172 +EXTERN(boolean) jpeg_fill_bit_buffer
   1.173 +	JPP((bitread_working_state * state, register bit_buf_type get_buffer,
   1.174 +	     register int bits_left, int nbits));
   1.175 +
   1.176 +
   1.177 +/*
   1.178 + * Code for extracting next Huffman-coded symbol from input bit stream.
   1.179 + * Again, this is time-critical and we make the main paths be macros.
   1.180 + *
   1.181 + * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
   1.182 + * without looping.  Usually, more than 95% of the Huffman codes will be 8
   1.183 + * or fewer bits long.  The few overlength codes are handled with a loop,
   1.184 + * which need not be inline code.
   1.185 + *
   1.186 + * Notes about the HUFF_DECODE macro:
   1.187 + * 1. Near the end of the data segment, we may fail to get enough bits
   1.188 + *    for a lookahead.  In that case, we do it the hard way.
   1.189 + * 2. If the lookahead table contains no entry, the next code must be
   1.190 + *    more than HUFF_LOOKAHEAD bits long.
   1.191 + * 3. jpeg_huff_decode returns -1 if forced to suspend.
   1.192 + */
   1.193 +
   1.194 +#define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \
   1.195 +{ register int nb, look; \
   1.196 +  if (bits_left < HUFF_LOOKAHEAD) { \
   1.197 +    if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
   1.198 +    get_buffer = state.get_buffer; bits_left = state.bits_left; \
   1.199 +    if (bits_left < HUFF_LOOKAHEAD) { \
   1.200 +      nb = 1; goto slowlabel; \
   1.201 +    } \
   1.202 +  } \
   1.203 +  look = PEEK_BITS(HUFF_LOOKAHEAD); \
   1.204 +  if ((nb = (htbl->lookup[look] >> HUFF_LOOKAHEAD)) <= HUFF_LOOKAHEAD) { \
   1.205 +    DROP_BITS(nb); \
   1.206 +    result = htbl->lookup[look] & ((1 << HUFF_LOOKAHEAD) - 1); \
   1.207 +  } else { \
   1.208 +slowlabel: \
   1.209 +    if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
   1.210 +	{ failaction; } \
   1.211 +    get_buffer = state.get_buffer; bits_left = state.bits_left; \
   1.212 +  } \
   1.213 +}
   1.214 +
   1.215 +#define HUFF_DECODE_FAST(s,nb,htbl) \
   1.216 +  FILL_BIT_BUFFER_FAST; \
   1.217 +  s = PEEK_BITS(HUFF_LOOKAHEAD); \
   1.218 +  s = htbl->lookup[s]; \
   1.219 +  nb = s >> HUFF_LOOKAHEAD; \
   1.220 +  /* Pre-execute the common case of nb <= HUFF_LOOKAHEAD */ \
   1.221 +  DROP_BITS(nb); \
   1.222 +  s = s & ((1 << HUFF_LOOKAHEAD) - 1); \
   1.223 +  if (nb > HUFF_LOOKAHEAD) { \
   1.224 +    /* Equivalent of jpeg_huff_decode() */ \
   1.225 +    /* Don't use GET_BITS() here because we don't want to modify bits_left */ \
   1.226 +    s = (get_buffer >> bits_left) & ((1 << (nb)) - 1); \
   1.227 +    while (s > htbl->maxcode[nb]) { \
   1.228 +      s <<= 1; \
   1.229 +      s |= GET_BITS(1); \
   1.230 +      nb++; \
   1.231 +    } \
   1.232 +    s = htbl->pub->huffval[ (int) (s + htbl->valoffset[nb]) & 0xFF ]; \
   1.233 +  }
   1.234 +
   1.235 +/* Out-of-line case for Huffman code fetching */
   1.236 +EXTERN(int) jpeg_huff_decode
   1.237 +	JPP((bitread_working_state * state, register bit_buf_type get_buffer,
   1.238 +	     register int bits_left, d_derived_tbl * htbl, int min_bits));

mercurial