Main MRPT website > C++ reference for MRPT 1.9.9
jdhuff.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 /* Short forms of external names for systems with brain-damaged linkers. */
11 
12 #ifdef NEED_SHORT_EXTERNAL_NAMES
13 #define jpeg_make_d_derived_tbl jMkDDerived
14 #define jpeg_fill_bit_buffer jFilBitBuf
15 #define jpeg_huff_decode jHufDecode
16 #endif /* NEED_SHORT_EXTERNAL_NAMES */
17 
18 /* Derived data constructed for each Huffman table */
19 
20 #define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */
21 
22 typedef struct
23 {
24  /* Basic tables: (element [0] of each array is unused) */
25  INT32 maxcode[18]; /* largest code of length k (-1 if none) */
26  /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
27  INT32 valoffset[17]; /* huffval[] offset for codes of length k */
28  /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
29  * the smallest code of length k; so given a code of length k, the
30  * corresponding symbol is huffval[code + valoffset[k]]
31  */
32 
33  /* Link to public Huffman table (needed only in jpeg_huff_decode) */
35 
36  /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
37  * the input data stream. If the next Huffman code is no more
38  * than HUFF_LOOKAHEAD bits long, we can obtain its length and
39  * the corresponding symbol directly from these tables.
40  */
41  int look_nbits[1 << HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
42  UINT8 look_sym[1 << HUFF_LOOKAHEAD]; /* symbol, or unused */
44 
45 /* Expand a Huffman table definition into the derived format */
46 EXTERN(void)
48  (j_decompress_ptr cinfo, boolean isDC, int tblno, d_derived_tbl** pdtbl));
49 
50 /*
51  * Fetching the next N bits from the input stream is a time-critical operation
52  * for the Huffman decoders. We implement it with a combination of inline
53  * macros and out-of-line subroutines. Note that N (the number of bits
54  * demanded at one time) never exceeds 15 for JPEG use.
55  *
56  * We read source bytes into get_buffer and dole out bits as needed.
57  * If get_buffer already contains enough bits, they are fetched in-line
58  * by the macros CHECK_BIT_BUFFER and GET_BITS. When there aren't enough
59  * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
60  * as full as possible (not just to the number of bits needed; this
61  * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
62  * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
63  * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
64  * at least the requested number of bits --- dummy zeroes are inserted if
65  * necessary.
66  */
67 
68 typedef INT32 bit_buf_type; /* type of bit-extraction buffer */
69 #define BIT_BUF_SIZE 32 /* size of buffer in bits */
70 
71 /* If long is > 32 bits on your machine, and shifting/masking longs is
72  * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
73  * appropriately should be a win. Unfortunately we can't define the size
74  * with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
75  * because not all machines measure sizeof in 8-bit bytes.
76  */
77 
78 typedef struct
79 { /* Bitreading state saved across MCUs */
80  bit_buf_type get_buffer; /* current bit-extraction buffer */
81  int bits_left; /* # of unused bits in it */
83 
84 typedef struct
85 { /* Bitreading working state within an MCU */
86  /* Current data source location */
87  /* We need a copy, rather than munging the original, in case of suspension
88  */
89  const JOCTET* next_input_byte; /* => next byte to read from source */
90  size_t bytes_in_buffer; /* # of bytes remaining in source buffer */
91  /* Bit input buffer --- note these values are kept in variables,
92  * not in this struct, inside the inner loops.
93  */
94  bit_buf_type get_buffer; /* current bit-extraction buffer */
95  int bits_left; /* # of unused bits in it */
96  /* Pointer needed by jpeg_fill_bit_buffer. */
97  j_decompress_ptr cinfo; /* back link to decompress master record */
99 
100 /* Macros to declare and load/save bitread local variables. */
101 #define BITREAD_STATE_VARS \
102  bit_buf_type get_buffer; \
103  int bits_left; \
104  bitread_working_state br_state
105 
106 #define BITREAD_LOAD_STATE(cinfop, permstate) \
107  br_state.cinfo = cinfop; \
108  br_state.next_input_byte = cinfop->src->next_input_byte; \
109  br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
110  get_buffer = permstate.get_buffer; \
111  bits_left = permstate.bits_left;
112 
113 #define BITREAD_SAVE_STATE(cinfop, permstate) \
114  cinfop->src->next_input_byte = br_state.next_input_byte; \
115  cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
116  permstate.get_buffer = get_buffer; \
117  permstate.bits_left = bits_left
118 
119 /*
120  * These macros provide the in-line portion of bit fetching.
121  * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
122  * before using GET_BITS, PEEK_BITS, or DROP_BITS.
123  * The variables get_buffer and bits_left are assumed to be locals,
124  * but the state struct might not be (jpeg_huff_decode needs this).
125  * CHECK_BIT_BUFFER(state,n,action);
126  * Ensure there are N bits in get_buffer; if suspend, take action.
127  * val = GET_BITS(n);
128  * Fetch next N bits.
129  * val = PEEK_BITS(n);
130  * Fetch next N bits without removing them from the buffer.
131  * DROP_BITS(n);
132  * Discard next N bits.
133  * The value N should be a simple variable, not an expression, because it
134  * is evaluated multiple times.
135  */
136 
137 #define CHECK_BIT_BUFFER(state, nbits, action) \
138  { \
139  if (bits_left < (nbits)) \
140  { \
141  if (!jpeg_fill_bit_buffer(&(state), get_buffer, bits_left, nbits)) \
142  { \
143  action; \
144  } \
145  get_buffer = (state).get_buffer; \
146  bits_left = (state).bits_left; \
147  } \
148  }
149 
150 #define GET_BITS(nbits) \
151  (((int)(get_buffer >> (bits_left -= (nbits)))) & ((1 << (nbits)) - 1))
152 
153 #define PEEK_BITS(nbits) \
154  (((int)(get_buffer >> (bits_left - (nbits)))) & ((1 << (nbits)) - 1))
155 
156 #define DROP_BITS(nbits) (bits_left -= (nbits))
157 
158 /* Load up the bit buffer to a depth of at least nbits */
159 EXTERN(boolean)
161  (bitread_working_state * state, bit_buf_type get_buffer, int bits_left,
162  int nbits));
163 
164 /*
165  * Code for extracting next Huffman-coded symbol from input bit stream.
166  * Again, this is time-critical and we make the main paths be macros.
167  *
168  * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
169  * without looping. Usually, more than 95% of the Huffman codes will be 8
170  * or fewer bits long. The few overlength codes are handled with a loop,
171  * which need not be inline code.
172  *
173  * Notes about the HUFF_DECODE macro:
174  * 1. Near the end of the data segment, we may fail to get enough bits
175  * for a lookahead. In that case, we do it the hard way.
176  * 2. If the lookahead table contains no entry, the next code must be
177  * more than HUFF_LOOKAHEAD bits long.
178  * 3. jpeg_huff_decode returns -1 if forced to suspend.
179  */
180 
181 #define HUFF_DECODE(result, state, htbl, failaction, slowlabel) \
182  { \
183  int nb, look; \
184  if (bits_left < HUFF_LOOKAHEAD) \
185  { \
186  if (!jpeg_fill_bit_buffer(&state, get_buffer, bits_left, 0)) \
187  { \
188  failaction; \
189  } \
190  get_buffer = state.get_buffer; \
191  bits_left = state.bits_left; \
192  if (bits_left < HUFF_LOOKAHEAD) \
193  { \
194  nb = 1; \
195  goto slowlabel; \
196  } \
197  } \
198  look = PEEK_BITS(HUFF_LOOKAHEAD); \
199  if ((nb = htbl->look_nbits[look]) != 0) \
200  { \
201  DROP_BITS(nb); \
202  result = htbl->look_sym[look]; \
203  } \
204  else \
205  { \
206  nb = HUFF_LOOKAHEAD + 1; \
207  slowlabel: \
208  if ((result = jpeg_huff_decode( \
209  &state, get_buffer, bits_left, htbl, nb)) < 0) \
210  { \
211  failaction; \
212  } \
213  get_buffer = state.get_buffer; \
214  bits_left = state.bits_left; \
215  } \
216  }
217 
218 /* Out-of-line case for Huffman code fetching */
219 EXTERN(int)
221  (bitread_working_state * state, bit_buf_type get_buffer, int bits_left,
222  d_derived_tbl* htbl, int min_bits));
#define EXTERN(type)
Definition: jmorecfg.h:179
bit_buf_type get_buffer
Definition: jdhuff.h:94
jpeg_huff_decode(bitread_working_state *state, bit_buf_type get_buffer, int bits_left, d_derived_tbl *htbl, int min_bits)
Definition: jdhuff.cpp:425
jpeg_fill_bit_buffer(bitread_working_state *state, bit_buf_type get_buffer, int bits_left, int nbits)
Definition: jdhuff.cpp:299
const JOCTET * next_input_byte
Definition: jdhuff.h:89
size_t bytes_in_buffer
Definition: jdhuff.h:90
jpeg_make_d_derived_tbl JPP((j_decompress_ptr cinfo, boolean isDC, int tblno, d_derived_tbl **pdtbl))
long INT32
Definition: jmorecfg.h:151
j_decompress_ptr cinfo
Definition: jdhuff.h:97
bit_buf_type get_buffer
Definition: jdhuff.h:80
short UINT8
Definition: jmorecfg.h:130
INT32 bit_buf_type
Definition: jdhuff.h:68
#define HUFF_LOOKAHEAD
Definition: jdhuff.h:20
jpeg_make_d_derived_tbl(j_decompress_ptr cinfo, boolean isDC, int tblno, d_derived_tbl **pdtbl)
Definition: jdhuff.cpp:147
char JOCTET
Definition: jmorecfg.h:106
JHUFF_TBL * pub
Definition: jdhuff.h:34



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019