2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: jdhuff.h /main/2 1996/05/09 03:48:02 drk $ */
27 * Copyright (C) 1991-1996, Thomas G. Lane.
28 * This file is part of the Independent JPEG Group's software.
29 * For conditions of distribution and use, see the accompanying README file.
31 * This file contains declarations for Huffman entropy decoding routines
32 * that are shared between the sequential decoder (jdhuff.c) and the
33 * progressive decoder (jdphuff.c). No other modules need to see these.
36 /* Short forms of external names for systems with brain-damaged linkers. */
38 #ifdef NEED_SHORT_EXTERNAL_NAMES
39 #define jpeg_make_d_derived_tbl jMkDDerived
40 #define jpeg_fill_bit_buffer jFilBitBuf
41 #define jpeg_huff_decode jHufDecode
42 #endif /* NEED_SHORT_EXTERNAL_NAMES */
45 /* Derived data constructed for each Huffman table */
47 #define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */
50 /* Basic tables: (element [0] of each array is unused) */
51 INT32 mincode[17]; /* smallest code of length k */
52 INT32 maxcode[18]; /* largest code of length k (-1 if none) */
53 /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
54 int valptr[17]; /* huffval[] index of 1st symbol of length k */
56 /* Link to public Huffman table (needed only in jpeg_huff_decode) */
59 /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
60 * the input data stream. If the next Huffman code is no more
61 * than HUFF_LOOKAHEAD bits long, we can obtain its length and
62 * the corresponding symbol directly from these tables.
64 int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
65 UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
68 /* Expand a Huffman table definition into the derived format */
69 EXTERN(void) jpeg_make_d_derived_tbl JPP((j_decompress_ptr cinfo,
70 JHUFF_TBL * htbl, d_derived_tbl ** pdtbl));
74 * Fetching the next N bits from the input stream is a time-critical operation
75 * for the Huffman decoders. We implement it with a combination of inline
76 * macros and out-of-line subroutines. Note that N (the number of bits
77 * demanded at one time) never exceeds 15 for JPEG use.
79 * We read source bytes into get_buffer and dole out bits as needed.
80 * If get_buffer already contains enough bits, they are fetched in-line
81 * by the macros CHECK_BIT_BUFFER and GET_BITS. When there aren't enough
82 * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
83 * as full as possible (not just to the number of bits needed; this
84 * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
85 * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
86 * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
87 * at least the requested number of bits --- dummy zeroes are inserted if
91 typedef INT32 bit_buf_type; /* type of bit-extraction buffer */
92 #define BIT_BUF_SIZE 32 /* size of buffer in bits */
94 /* If long is > 32 bits on your machine, and shifting/masking longs is
95 * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
96 * appropriately should be a win. Unfortunately we can't do this with
97 * something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
98 * because not all machines measure sizeof in 8-bit bytes.
101 typedef struct { /* Bitreading state saved across MCUs */
102 bit_buf_type get_buffer; /* current bit-extraction buffer */
103 int bits_left; /* # of unused bits in it */
104 boolean printed_eod; /* flag to suppress multiple warning msgs */
105 } bitread_perm_state;
107 typedef struct { /* Bitreading working state within an MCU */
108 /* current data source state */
109 const JOCTET * next_input_byte; /* => next byte to read from source */
110 size_t bytes_in_buffer; /* # of bytes remaining in source buffer */
111 int unread_marker; /* nonzero if we have hit a marker */
112 /* bit input buffer --- note these values are kept in register variables,
113 * not in this struct, inside the inner loops.
115 bit_buf_type get_buffer; /* current bit-extraction buffer */
116 int bits_left; /* # of unused bits in it */
117 /* pointers needed by jpeg_fill_bit_buffer */
118 j_decompress_ptr cinfo; /* back link to decompress master record */
119 boolean * printed_eod_ptr; /* => flag in permanent state */
120 } bitread_working_state;
122 /* Macros to declare and load/save bitread local variables. */
123 #define BITREAD_STATE_VARS \
124 register bit_buf_type get_buffer; \
125 register int bits_left; \
126 bitread_working_state br_state
128 #define BITREAD_LOAD_STATE(cinfop,permstate) \
129 br_state.cinfo = cinfop; \
130 br_state.next_input_byte = cinfop->src->next_input_byte; \
131 br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
132 br_state.unread_marker = cinfop->unread_marker; \
133 get_buffer = permstate.get_buffer; \
134 bits_left = permstate.bits_left; \
135 br_state.printed_eod_ptr = & permstate.printed_eod
137 #define BITREAD_SAVE_STATE(cinfop,permstate) \
138 cinfop->src->next_input_byte = br_state.next_input_byte; \
139 cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
140 cinfop->unread_marker = br_state.unread_marker; \
141 permstate.get_buffer = get_buffer; \
142 permstate.bits_left = bits_left
145 * These macros provide the in-line portion of bit fetching.
146 * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
147 * before using GET_BITS, PEEK_BITS, or DROP_BITS.
148 * The variables get_buffer and bits_left are assumed to be locals,
149 * but the state struct might not be (jpeg_huff_decode needs this).
150 * CHECK_BIT_BUFFER(state,n,action);
151 * Ensure there are N bits in get_buffer; if suspend, take action.
154 * val = PEEK_BITS(n);
155 * Fetch next N bits without removing them from the buffer.
157 * Discard next N bits.
158 * The value N should be a simple variable, not an expression, because it
159 * is evaluated multiple times.
162 #define CHECK_BIT_BUFFER(state,nbits,action) \
163 { if (bits_left < (nbits)) { \
164 if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits)) \
166 get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
168 #define GET_BITS(nbits) \
169 (((int) (get_buffer >> (bits_left -= (nbits)))) & ((1<<(nbits))-1))
171 #define PEEK_BITS(nbits) \
172 (((int) (get_buffer >> (bits_left - (nbits)))) & ((1<<(nbits))-1))
174 #define DROP_BITS(nbits) \
175 (bits_left -= (nbits))
177 /* Load up the bit buffer to a depth of at least nbits */
178 EXTERN(boolean) jpeg_fill_bit_buffer
179 JPP((bitread_working_state * state, register bit_buf_type get_buffer,
180 register int bits_left, int nbits));
184 * Code for extracting next Huffman-coded symbol from input bit stream.
185 * Again, this is time-critical and we make the main paths be macros.
187 * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
188 * without looping. Usually, more than 95% of the Huffman codes will be 8
189 * or fewer bits long. The few overlength codes are handled with a loop,
190 * which need not be inline code.
192 * Notes about the HUFF_DECODE macro:
193 * 1. Near the end of the data segment, we may fail to get enough bits
194 * for a lookahead. In that case, we do it the hard way.
195 * 2. If the lookahead table contains no entry, the next code must be
196 * more than HUFF_LOOKAHEAD bits long.
197 * 3. jpeg_huff_decode returns -1 if forced to suspend.
200 #define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \
201 { register int nb, look; \
202 if (bits_left < HUFF_LOOKAHEAD) { \
203 if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
204 get_buffer = state.get_buffer; bits_left = state.bits_left; \
205 if (bits_left < HUFF_LOOKAHEAD) { \
206 nb = 1; goto slowlabel; \
209 look = PEEK_BITS(HUFF_LOOKAHEAD); \
210 if ((nb = htbl->look_nbits[look]) != 0) { \
212 result = htbl->look_sym[look]; \
214 nb = HUFF_LOOKAHEAD+1; \
216 if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
218 get_buffer = state.get_buffer; bits_left = state.bits_left; \
222 /* Out-of-line case for Huffman code fetching */
223 EXTERN(int) jpeg_huff_decode
224 JPP((bitread_working_state * state, register bit_buf_type get_buffer,
225 register int bits_left, d_derived_tbl * htbl, int min_bits));