/src/dcmtk/dcmjpeg/libijg12/jdshuff.c
Line | Count | Source |
1 | | /* |
2 | | * jdshuff.c |
3 | | * |
4 | | * Copyright (C) 1991-1998, Thomas G. Lane. |
5 | | * This file is part of the Independent JPEG Group's software. |
6 | | * For conditions of distribution and use, see the accompanying README file. |
7 | | * |
8 | | * This file contains Huffman entropy decoding routines for sequential JPEG. |
9 | | * |
10 | | * Much of the complexity here has to do with supporting input suspension. |
11 | | * If the data source module demands suspension, we want to be able to back |
12 | | * up to the start of the current MCU. To do this, we copy state variables |
13 | | * into local working storage, and update them back to the permanent |
14 | | * storage only upon successful completion of an MCU. |
15 | | */ |
16 | | |
17 | | #define JPEG_INTERNALS |
18 | | #include "jinclude12.h" |
19 | | #include "jpeglib12.h" |
20 | | #include "jlossy12.h" /* Private declarations for lossy codec */ |
21 | | #include "jdhuff12.h" /* Declarations shared with jd*huff.c */ |
22 | | |
23 | | |
24 | | /* |
25 | | * Private entropy decoder object for Huffman decoding. |
26 | | * |
27 | | * The savable_state subrecord contains fields that change within an MCU, |
28 | | * but must not be updated permanently until we complete the MCU. |
29 | | */ |
30 | | |
31 | | typedef struct { |
32 | | int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
33 | | } savable_state; |
34 | | |
35 | | /* This macro is to work around compilers with missing or broken |
36 | | * structure assignment. You'll need to fix this code if you have |
37 | | * such a compiler and you change MAX_COMPS_IN_SCAN. |
38 | | */ |
39 | | |
40 | | #ifndef NO_STRUCT_ASSIGN |
41 | | #define ASSIGN_STATE(dest,src) ((dest) = (src)) |
42 | | #else |
43 | | #if MAX_COMPS_IN_SCAN == 4 |
44 | | #define ASSIGN_STATE(dest,src) \ |
45 | | ((dest).last_dc_val[0] = (src).last_dc_val[0], \ |
46 | | (dest).last_dc_val[1] = (src).last_dc_val[1], \ |
47 | | (dest).last_dc_val[2] = (src).last_dc_val[2], \ |
48 | | (dest).last_dc_val[3] = (src).last_dc_val[3]) |
49 | | #endif |
50 | | #endif |
51 | | |
52 | | |
53 | | typedef struct { |
54 | | huffd_common_fields; /* Fields shared with other entropy decoders */ |
55 | | |
56 | | /* These fields are loaded into local variables at start of each MCU. |
57 | | * In case of suspension, we exit WITHOUT updating them. |
58 | | */ |
59 | | savable_state saved; /* Other state at start of MCU */ |
60 | | |
61 | | /* These fields are NOT loaded into local working state. */ |
62 | | unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
63 | | |
64 | | /* Pointers to derived tables (these workspaces have image lifespan) */ |
65 | | d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; |
66 | | d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS]; |
67 | | |
68 | | /* Precalculated info set up by start_pass for use in decode_mcu: */ |
69 | | |
70 | | /* Pointers to derived tables to be used for each block within an MCU */ |
71 | | d_derived_tbl * dc_cur_tbls[D_MAX_DATA_UNITS_IN_MCU]; |
72 | | d_derived_tbl * ac_cur_tbls[D_MAX_DATA_UNITS_IN_MCU]; |
73 | | /* Whether we care about the DC and AC coefficient values for each block */ |
74 | | boolean dc_needed[D_MAX_DATA_UNITS_IN_MCU]; |
75 | | boolean ac_needed[D_MAX_DATA_UNITS_IN_MCU]; |
76 | | } shuff_entropy_decoder; |
77 | | |
78 | | typedef shuff_entropy_decoder * shuff_entropy_ptr; |
79 | | |
80 | | |
81 | | /* |
82 | | * Initialize for a Huffman-compressed scan. |
83 | | */ |
84 | | |
85 | | METHODDEF(void) |
86 | | start_pass_huff_decoder (j_decompress_ptr cinfo) |
87 | | { |
88 | | j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec; |
89 | | shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private; |
90 | | int ci, blkn, dctbl, actbl; |
91 | | jpeg_component_info * compptr; |
92 | | |
93 | | /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. |
94 | | * This ought to be an error condition, but we make it a warning because |
95 | | * there are some baseline files out there with all zeroes in these bytes. |
96 | | */ |
97 | | if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 || |
98 | | cinfo->Ah != 0 || cinfo->Al != 0) |
99 | | WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); |
100 | | |
101 | | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
102 | | compptr = cinfo->cur_comp_info[ci]; |
103 | | dctbl = compptr->dc_tbl_no; |
104 | | actbl = compptr->ac_tbl_no; |
105 | | /* Compute derived values for Huffman tables */ |
106 | | /* We may do this more than once for a table, but it's not expensive */ |
107 | | jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, |
108 | | & entropy->dc_derived_tbls[dctbl]); |
109 | | jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, |
110 | | & entropy->ac_derived_tbls[actbl]); |
111 | | /* Initialize DC predictions to 0 */ |
112 | | entropy->saved.last_dc_val[ci] = 0; |
113 | | } |
114 | | |
115 | | /* Precalculate decoding info for each block in an MCU of this scan */ |
116 | | for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) { |
117 | | ci = cinfo->MCU_membership[blkn]; |
118 | | compptr = cinfo->cur_comp_info[ci]; |
119 | | /* Precalculate which table to use for each block */ |
120 | | entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; |
121 | | entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no]; |
122 | | /* Decide whether we really care about the coefficient values */ |
123 | | if (compptr->component_needed) { |
124 | | entropy->dc_needed[blkn] = TRUE; |
125 | | /* we don't need the ACs if producing a 1/8th-size image */ |
126 | | entropy->ac_needed[blkn] = (compptr->codec_data_unit > 1); |
127 | | } else { |
128 | | entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE; |
129 | | } |
130 | | } |
131 | | |
132 | | /* Initialize bitread state variables */ |
133 | | entropy->bitstate.bits_left = 0; |
134 | | entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ |
135 | | entropy->insufficient_data = FALSE; |
136 | | |
137 | | /* Initialize restart counter */ |
138 | | entropy->restarts_to_go = cinfo->restart_interval; |
139 | | } |
140 | | |
141 | | |
142 | | /* |
143 | | * Figure F.12: extend sign bit. |
144 | | * On some machines, a shift and add will be faster than a table lookup. |
145 | | */ |
146 | | |
147 | | #ifdef AVOID_TABLES |
148 | | |
149 | | #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x)) |
150 | | |
151 | | #else |
152 | | |
153 | | #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) |
154 | | |
155 | | static const int extend_test[16] = /* entry n is 2**(n-1) */ |
156 | | { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, |
157 | | 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; |
158 | | |
159 | | /* |
160 | | * Originally, a -1 was shifted but since shifting a negative value is |
161 | | * undefined behavior, now "~0U" (bit-wise NOT unsigned int 0) is used, |
162 | | * shifted and casted to an int. The result is the same, of course. |
163 | | */ |
164 | | static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */ |
165 | | { 0, (int)((~0U)<<1) + 1, (int)((~0U)<<2) + 1, (int)((~0U)<<3) + 1, (int)((~0U)<<4) + 1, |
166 | | (int)((~0U)<<5) + 1, (int)((~0U)<<6) + 1, (int)((~0U)<<7) + 1, (int)((~0U)<<8) + 1, |
167 | | (int)((~0U)<<9) + 1, (int)((~0U)<<10) + 1, (int)((~0U)<<11) + 1, (int)((~0U)<<12) + 1, |
168 | | (int)((~0U)<<13) + 1, (int)((~0U)<<14) + 1, (int)((~0U)<<15) + 1 }; |
169 | | |
170 | | #endif /* AVOID_TABLES */ |
171 | | |
172 | | |
173 | | /* |
174 | | * Check for a restart marker & resynchronize decoder. |
175 | | * Returns FALSE if must suspend. |
176 | | */ |
177 | | |
178 | | LOCAL(boolean) |
179 | | process_restart (j_decompress_ptr cinfo) |
180 | | { |
181 | | j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec; |
182 | | shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private; |
183 | | int ci; |
184 | | |
185 | | /* Throw away any unused bits remaining in bit buffer; */ |
186 | | /* include any full bytes in next_marker's count of discarded bytes */ |
187 | | cinfo->marker->discarded_bytes += (unsigned int)(entropy->bitstate.bits_left / 8); |
188 | | entropy->bitstate.bits_left = 0; |
189 | | |
190 | | /* Advance past the RSTn marker */ |
191 | | if (! (*cinfo->marker->read_restart_marker) (cinfo)) |
192 | | return FALSE; |
193 | | |
194 | | /* Re-initialize DC predictions to 0 */ |
195 | | for (ci = 0; ci < cinfo->comps_in_scan; ci++) |
196 | | entropy->saved.last_dc_val[ci] = 0; |
197 | | |
198 | | /* Reset restart counter */ |
199 | | entropy->restarts_to_go = cinfo->restart_interval; |
200 | | |
201 | | /* Reset out-of-data flag, unless read_restart_marker left us smack up |
202 | | * against a marker. In that case we will end up treating the next data |
203 | | * segment as empty, and we can avoid producing bogus output pixels by |
204 | | * leaving the flag set. |
205 | | */ |
206 | | if (cinfo->unread_marker == 0) |
207 | | entropy->insufficient_data = FALSE; |
208 | | |
209 | | return TRUE; |
210 | | } |
211 | | |
212 | | |
213 | | /* |
214 | | * Decode and return one MCU's worth of Huffman-compressed coefficients. |
215 | | * The coefficients are reordered from zigzag order into natural array order, |
216 | | * but are not dequantized. |
217 | | * |
218 | | * The i'th block of the MCU is stored into the block pointed to by |
219 | | * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. |
220 | | * (Wholesale zeroing is usually a little faster than retail...) |
221 | | * |
222 | | * Returns FALSE if data source requested suspension. In that case no |
223 | | * changes have been made to permanent state. (Exception: some output |
224 | | * coefficients may already have been assigned. This is harmless for |
225 | | * this module, since we'll just re-assign them on the next call.) |
226 | | */ |
227 | | |
228 | | METHODDEF(boolean) |
229 | | decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
230 | | { |
231 | | j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec; |
232 | | shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private; |
233 | | int blkn; |
234 | | BITREAD_STATE_VARS; |
235 | | savable_state state; |
236 | | |
237 | | /* Process restart marker if needed; may have to suspend */ |
238 | | if (cinfo->restart_interval) { |
239 | | if (entropy->restarts_to_go == 0) |
240 | | if (! process_restart(cinfo)) |
241 | | return FALSE; |
242 | | } |
243 | | |
244 | | /* If we've run out of data, just leave the MCU set to zeroes. |
245 | | * This way, we return uniform gray for the remainder of the segment. |
246 | | */ |
247 | | if (! entropy->insufficient_data) { |
248 | | |
249 | | /* Load up working state */ |
250 | | BITREAD_LOAD_STATE(cinfo,entropy->bitstate); |
251 | | ASSIGN_STATE(state, entropy->saved); |
252 | | |
253 | | /* Outer loop handles each block in the MCU */ |
254 | | |
255 | | for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) { |
256 | | JBLOCKROW block = MCU_data[blkn]; |
257 | | d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn]; |
258 | | d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn]; |
259 | | register int s, k, r; |
260 | | |
261 | | /* Decode a single block's worth of coefficients */ |
262 | | |
263 | | /* Section F.2.2.1: decode the DC coefficient difference */ |
264 | | HUFF_DECODE(s, br_state, dctbl, return FALSE, label1); |
265 | | if (s) { |
266 | | /* DC categories above 15 are undefined for DCT-based coding */ |
267 | | /* (ISO 10918-1 Tables F.1 and F.6) and would index extend_test[]/ */ |
268 | | /* extend_offset[] out of bounds; treat such a Huffman code as */ |
269 | | /* corrupt data instead of reading past the array. */ |
270 | | if (s > 15) { |
271 | | WARNMS(cinfo, JWRN_HUFF_BAD_CODE); |
272 | | s = 0; |
273 | | } else { |
274 | | CHECK_BIT_BUFFER(br_state, s, return FALSE); |
275 | | r = GET_BITS(s); |
276 | | s = HUFF_EXTEND(r, s); |
277 | | } |
278 | | } |
279 | | |
280 | | if (entropy->dc_needed[blkn]) { |
281 | | /* Convert DC difference to actual value, update last_dc_val */ |
282 | | int ci = cinfo->MCU_membership[blkn]; |
283 | | s += state.last_dc_val[ci]; |
284 | | state.last_dc_val[ci] = s; |
285 | | /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */ |
286 | | (*block)[0] = (JCOEF) s; |
287 | | } |
288 | | |
289 | | if (entropy->ac_needed[blkn]) { |
290 | | |
291 | | /* Section F.2.2.2: decode the AC coefficients */ |
292 | | /* Since zeroes are skipped, output area must be cleared beforehand */ |
293 | | for (k = 1; k < DCTSIZE2; k++) { |
294 | | HUFF_DECODE(s, br_state, actbl, return FALSE, label2); |
295 | | |
296 | | r = s >> 4; |
297 | | s &= 15; |
298 | | |
299 | | if (s) { |
300 | | k += r; |
301 | | CHECK_BIT_BUFFER(br_state, s, return FALSE); |
302 | | r = GET_BITS(s); |
303 | | s = HUFF_EXTEND(r, s); |
304 | | /* Output coefficient in natural (dezigzagged) order. |
305 | | * Note: the extra entries in jpeg_natural_order[] will save us |
306 | | * if k >= DCTSIZE2, which could happen if the data is corrupted. |
307 | | */ |
308 | | (*block)[jpeg_natural_order[k]] = (JCOEF) s; |
309 | | } else { |
310 | | if (r != 15) |
311 | | break; |
312 | | k += 15; |
313 | | } |
314 | | } |
315 | | |
316 | | } else { |
317 | | |
318 | | /* Section F.2.2.2: decode the AC coefficients */ |
319 | | /* In this path we just discard the values */ |
320 | | for (k = 1; k < DCTSIZE2; k++) { |
321 | | HUFF_DECODE(s, br_state, actbl, return FALSE, label3); |
322 | | |
323 | | r = s >> 4; |
324 | | s &= 15; |
325 | | |
326 | | if (s) { |
327 | | k += r; |
328 | | CHECK_BIT_BUFFER(br_state, s, return FALSE); |
329 | | DROP_BITS(s); |
330 | | } else { |
331 | | if (r != 15) |
332 | | break; |
333 | | k += 15; |
334 | | } |
335 | | } |
336 | | |
337 | | } |
338 | | } |
339 | | |
340 | | /* Completed MCU, so update state */ |
341 | | BITREAD_SAVE_STATE(cinfo,entropy->bitstate); |
342 | | ASSIGN_STATE(entropy->saved, state); |
343 | | } |
344 | | |
345 | | /* Account for restart interval (no-op if not using restarts) */ |
346 | | entropy->restarts_to_go--; |
347 | | |
348 | | return TRUE; |
349 | | } |
350 | | |
351 | | |
352 | | /* |
353 | | * Module initialization routine for Huffman entropy decoding. |
354 | | */ |
355 | | |
356 | | GLOBAL(void) |
357 | | jinit_shuff_decoder (j_decompress_ptr cinfo) |
358 | 0 | { |
359 | 0 | j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec; |
360 | 0 | shuff_entropy_ptr entropy; |
361 | 0 | int i; |
362 | |
|
363 | 0 | entropy = (shuff_entropy_ptr) |
364 | 0 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
365 | 0 | SIZEOF(shuff_entropy_decoder)); |
366 | 0 | lossyd->entropy_private = (void *) entropy; |
367 | 0 | lossyd->entropy_start_pass = start_pass_huff_decoder; |
368 | 0 | lossyd->entropy_decode_mcu = decode_mcu; |
369 | | |
370 | | /* Mark tables unallocated */ |
371 | 0 | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
372 | | entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; |
373 | 0 | } |
374 | 0 | } |