rev |
line source |
nuclear@14
|
1 /*
|
nuclear@14
|
2 * jdphuff.c
|
nuclear@14
|
3 *
|
nuclear@14
|
4 * Copyright (C) 1995-1997, Thomas G. Lane.
|
nuclear@14
|
5 * This file is part of the Independent JPEG Group's software.
|
nuclear@14
|
6 * For conditions of distribution and use, see the accompanying README file.
|
nuclear@14
|
7 *
|
nuclear@14
|
8 * This file contains Huffman entropy decoding routines for progressive JPEG.
|
nuclear@14
|
9 *
|
nuclear@14
|
10 * Much of the complexity here has to do with supporting input suspension.
|
nuclear@14
|
11 * If the data source module demands suspension, we want to be able to back
|
nuclear@14
|
12 * up to the start of the current MCU. To do this, we copy state variables
|
nuclear@14
|
13 * into local working storage, and update them back to the permanent
|
nuclear@14
|
14 * storage only upon successful completion of an MCU.
|
nuclear@14
|
15 */
|
nuclear@14
|
16
|
nuclear@14
|
17 #define JPEG_INTERNALS
|
nuclear@14
|
18 #include "jinclude.h"
|
nuclear@14
|
19 #include "jpeglib.h"
|
nuclear@14
|
20 #include "jdhuff.h" /* Declarations shared with jdhuff.c */
|
nuclear@14
|
21
|
nuclear@14
|
22
|
nuclear@14
|
23 #ifdef D_PROGRESSIVE_SUPPORTED
|
nuclear@14
|
24
|
nuclear@14
|
25 /*
|
nuclear@14
|
26 * Expanded entropy decoder object for progressive Huffman decoding.
|
nuclear@14
|
27 *
|
nuclear@14
|
28 * The savable_state subrecord contains fields that change within an MCU,
|
nuclear@14
|
29 * but must not be updated permanently until we complete the MCU.
|
nuclear@14
|
30 */
|
nuclear@14
|
31
|
nuclear@14
|
32 typedef struct {
|
nuclear@14
|
33 unsigned int EOBRUN; /* remaining EOBs in EOBRUN */
|
nuclear@14
|
34 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
|
nuclear@14
|
35 } savable_state;
|
nuclear@14
|
36
|
nuclear@14
|
37 /* This macro is to work around compilers with missing or broken
|
nuclear@14
|
38 * structure assignment. You'll need to fix this code if you have
|
nuclear@14
|
39 * such a compiler and you change MAX_COMPS_IN_SCAN.
|
nuclear@14
|
40 */
|
nuclear@14
|
41
|
nuclear@14
|
42 #ifndef NO_STRUCT_ASSIGN
|
nuclear@14
|
43 #define ASSIGN_STATE(dest,src) ((dest) = (src))
|
nuclear@14
|
44 #else
|
nuclear@14
|
45 #if MAX_COMPS_IN_SCAN == 4
|
nuclear@14
|
46 #define ASSIGN_STATE(dest,src) \
|
nuclear@14
|
47 ((dest).EOBRUN = (src).EOBRUN, \
|
nuclear@14
|
48 (dest).last_dc_val[0] = (src).last_dc_val[0], \
|
nuclear@14
|
49 (dest).last_dc_val[1] = (src).last_dc_val[1], \
|
nuclear@14
|
50 (dest).last_dc_val[2] = (src).last_dc_val[2], \
|
nuclear@14
|
51 (dest).last_dc_val[3] = (src).last_dc_val[3])
|
nuclear@14
|
52 #endif
|
nuclear@14
|
53 #endif
|
nuclear@14
|
54
|
nuclear@14
|
55
|
nuclear@14
|
56 typedef struct {
|
nuclear@14
|
57 struct jpeg_entropy_decoder pub; /* public fields */
|
nuclear@14
|
58
|
nuclear@14
|
59 /* These fields are loaded into local variables at start of each MCU.
|
nuclear@14
|
60 * In case of suspension, we exit WITHOUT updating them.
|
nuclear@14
|
61 */
|
nuclear@14
|
62 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
|
nuclear@14
|
63 savable_state saved; /* Other state at start of MCU */
|
nuclear@14
|
64
|
nuclear@14
|
65 /* These fields are NOT loaded into local working state. */
|
nuclear@14
|
66 unsigned int restarts_to_go; /* MCUs left in this restart interval */
|
nuclear@14
|
67
|
nuclear@14
|
68 /* Pointers to derived tables (these workspaces have image lifespan) */
|
nuclear@14
|
69 d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
|
nuclear@14
|
70
|
nuclear@14
|
71 d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
|
nuclear@14
|
72 } phuff_entropy_decoder;
|
nuclear@14
|
73
|
nuclear@14
|
74 typedef phuff_entropy_decoder * phuff_entropy_ptr;
|
nuclear@14
|
75
|
nuclear@14
|
76 /* Forward declarations */
|
nuclear@14
|
77 METHODDEF(boolean) decode_mcu_DC_first JPP((j_decompress_ptr cinfo,
|
nuclear@14
|
78 JBLOCKROW *MCU_data));
|
nuclear@14
|
79 METHODDEF(boolean) decode_mcu_AC_first JPP((j_decompress_ptr cinfo,
|
nuclear@14
|
80 JBLOCKROW *MCU_data));
|
nuclear@14
|
81 METHODDEF(boolean) decode_mcu_DC_refine JPP((j_decompress_ptr cinfo,
|
nuclear@14
|
82 JBLOCKROW *MCU_data));
|
nuclear@14
|
83 METHODDEF(boolean) decode_mcu_AC_refine JPP((j_decompress_ptr cinfo,
|
nuclear@14
|
84 JBLOCKROW *MCU_data));
|
nuclear@14
|
85
|
nuclear@14
|
86
|
nuclear@14
|
87 /*
|
nuclear@14
|
88 * Initialize for a Huffman-compressed scan.
|
nuclear@14
|
89 */
|
nuclear@14
|
90
|
nuclear@14
|
91 METHODDEF(void)
|
nuclear@14
|
92 start_pass_phuff_decoder (j_decompress_ptr cinfo)
|
nuclear@14
|
93 {
|
nuclear@14
|
94 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
|
nuclear@14
|
95 boolean is_DC_band, bad;
|
nuclear@14
|
96 int ci, coefi, tbl;
|
nuclear@14
|
97 int *coef_bit_ptr;
|
nuclear@14
|
98 jpeg_component_info * compptr;
|
nuclear@14
|
99
|
nuclear@14
|
100 is_DC_band = (cinfo->Ss == 0);
|
nuclear@14
|
101
|
nuclear@14
|
102 /* Validate scan parameters */
|
nuclear@14
|
103 bad = FALSE;
|
nuclear@14
|
104 if (is_DC_band) {
|
nuclear@14
|
105 if (cinfo->Se != 0)
|
nuclear@14
|
106 bad = TRUE;
|
nuclear@14
|
107 } else {
|
nuclear@14
|
108 /* need not check Ss/Se < 0 since they came from unsigned bytes */
|
nuclear@14
|
109 if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
|
nuclear@14
|
110 bad = TRUE;
|
nuclear@14
|
111 /* AC scans may have only one component */
|
nuclear@14
|
112 if (cinfo->comps_in_scan != 1)
|
nuclear@14
|
113 bad = TRUE;
|
nuclear@14
|
114 }
|
nuclear@14
|
115 if (cinfo->Ah != 0) {
|
nuclear@14
|
116 /* Successive approximation refinement scan: must have Al = Ah-1. */
|
nuclear@14
|
117 if (cinfo->Al != cinfo->Ah-1)
|
nuclear@14
|
118 bad = TRUE;
|
nuclear@14
|
119 }
|
nuclear@14
|
120 if (cinfo->Al > 13) /* need not check for < 0 */
|
nuclear@14
|
121 bad = TRUE;
|
nuclear@14
|
122 /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
|
nuclear@14
|
123 * but the spec doesn't say so, and we try to be liberal about what we
|
nuclear@14
|
124 * accept. Note: large Al values could result in out-of-range DC
|
nuclear@14
|
125 * coefficients during early scans, leading to bizarre displays due to
|
nuclear@14
|
126 * overflows in the IDCT math. But we won't crash.
|
nuclear@14
|
127 */
|
nuclear@14
|
128 if (bad)
|
nuclear@14
|
129 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
|
nuclear@14
|
130 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
|
nuclear@14
|
131 /* Update progression status, and verify that scan order is legal.
|
nuclear@14
|
132 * Note that inter-scan inconsistencies are treated as warnings
|
nuclear@14
|
133 * not fatal errors ... not clear if this is right way to behave.
|
nuclear@14
|
134 */
|
nuclear@14
|
135 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
nuclear@14
|
136 int cindex = cinfo->cur_comp_info[ci]->component_index;
|
nuclear@14
|
137 coef_bit_ptr = & cinfo->coef_bits[cindex][0];
|
nuclear@14
|
138 if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
|
nuclear@14
|
139 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
|
nuclear@14
|
140 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
|
nuclear@14
|
141 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
|
nuclear@14
|
142 if (cinfo->Ah != expected)
|
nuclear@14
|
143 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
|
nuclear@14
|
144 coef_bit_ptr[coefi] = cinfo->Al;
|
nuclear@14
|
145 }
|
nuclear@14
|
146 }
|
nuclear@14
|
147
|
nuclear@14
|
148 /* Select MCU decoding routine */
|
nuclear@14
|
149 if (cinfo->Ah == 0) {
|
nuclear@14
|
150 if (is_DC_band)
|
nuclear@14
|
151 entropy->pub.decode_mcu = decode_mcu_DC_first;
|
nuclear@14
|
152 else
|
nuclear@14
|
153 entropy->pub.decode_mcu = decode_mcu_AC_first;
|
nuclear@14
|
154 } else {
|
nuclear@14
|
155 if (is_DC_band)
|
nuclear@14
|
156 entropy->pub.decode_mcu = decode_mcu_DC_refine;
|
nuclear@14
|
157 else
|
nuclear@14
|
158 entropy->pub.decode_mcu = decode_mcu_AC_refine;
|
nuclear@14
|
159 }
|
nuclear@14
|
160
|
nuclear@14
|
161 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
|
nuclear@14
|
162 compptr = cinfo->cur_comp_info[ci];
|
nuclear@14
|
163 /* Make sure requested tables are present, and compute derived tables.
|
nuclear@14
|
164 * We may build same derived table more than once, but it's not expensive.
|
nuclear@14
|
165 */
|
nuclear@14
|
166 if (is_DC_band) {
|
nuclear@14
|
167 if (cinfo->Ah == 0) { /* DC refinement needs no table */
|
nuclear@14
|
168 tbl = compptr->dc_tbl_no;
|
nuclear@14
|
169 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
|
nuclear@14
|
170 & entropy->derived_tbls[tbl]);
|
nuclear@14
|
171 }
|
nuclear@14
|
172 } else {
|
nuclear@14
|
173 tbl = compptr->ac_tbl_no;
|
nuclear@14
|
174 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
|
nuclear@14
|
175 & entropy->derived_tbls[tbl]);
|
nuclear@14
|
176 /* remember the single active table */
|
nuclear@14
|
177 entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
|
nuclear@14
|
178 }
|
nuclear@14
|
179 /* Initialize DC predictions to 0 */
|
nuclear@14
|
180 entropy->saved.last_dc_val[ci] = 0;
|
nuclear@14
|
181 }
|
nuclear@14
|
182
|
nuclear@14
|
183 /* Initialize bitread state variables */
|
nuclear@14
|
184 entropy->bitstate.bits_left = 0;
|
nuclear@14
|
185 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
|
nuclear@14
|
186 entropy->pub.insufficient_data = FALSE;
|
nuclear@14
|
187
|
nuclear@14
|
188 /* Initialize private state variables */
|
nuclear@14
|
189 entropy->saved.EOBRUN = 0;
|
nuclear@14
|
190
|
nuclear@14
|
191 /* Initialize restart counter */
|
nuclear@14
|
192 entropy->restarts_to_go = cinfo->restart_interval;
|
nuclear@14
|
193 }
|
nuclear@14
|
194
|
nuclear@14
|
195
|
nuclear@14
|
196 /*
|
nuclear@14
|
197 * Figure F.12: extend sign bit.
|
nuclear@14
|
198 * On some machines, a shift and add will be faster than a table lookup.
|
nuclear@14
|
199 */
|
nuclear@14
|
200
|
nuclear@14
|
201 #ifdef AVOID_TABLES
|
nuclear@14
|
202
|
nuclear@14
|
203 #define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
|
nuclear@14
|
204
|
nuclear@14
|
205 #else
|
nuclear@14
|
206
|
nuclear@14
|
207 #define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
|
nuclear@14
|
208
|
nuclear@14
|
209 static const int extend_test[16] = /* entry n is 2**(n-1) */
|
nuclear@14
|
210 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
|
nuclear@14
|
211 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
|
nuclear@14
|
212
|
nuclear@14
|
213 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
|
nuclear@14
|
214 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
|
nuclear@14
|
215 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
|
nuclear@14
|
216 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
|
nuclear@14
|
217 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
|
nuclear@14
|
218
|
nuclear@14
|
219 #endif /* AVOID_TABLES */
|
nuclear@14
|
220
|
nuclear@14
|
221
|
nuclear@14
|
222 /*
|
nuclear@14
|
223 * Check for a restart marker & resynchronize decoder.
|
nuclear@14
|
224 * Returns FALSE if must suspend.
|
nuclear@14
|
225 */
|
nuclear@14
|
226
|
nuclear@14
|
227 LOCAL(boolean)
|
nuclear@14
|
228 process_restart (j_decompress_ptr cinfo)
|
nuclear@14
|
229 {
|
nuclear@14
|
230 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
|
nuclear@14
|
231 int ci;
|
nuclear@14
|
232
|
nuclear@14
|
233 /* Throw away any unused bits remaining in bit buffer; */
|
nuclear@14
|
234 /* include any full bytes in next_marker's count of discarded bytes */
|
nuclear@14
|
235 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
|
nuclear@14
|
236 entropy->bitstate.bits_left = 0;
|
nuclear@14
|
237
|
nuclear@14
|
238 /* Advance past the RSTn marker */
|
nuclear@14
|
239 if (! (*cinfo->marker->read_restart_marker) (cinfo))
|
nuclear@14
|
240 return FALSE;
|
nuclear@14
|
241
|
nuclear@14
|
242 /* Re-initialize DC predictions to 0 */
|
nuclear@14
|
243 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
|
nuclear@14
|
244 entropy->saved.last_dc_val[ci] = 0;
|
nuclear@14
|
245 /* Re-init EOB run count, too */
|
nuclear@14
|
246 entropy->saved.EOBRUN = 0;
|
nuclear@14
|
247
|
nuclear@14
|
248 /* Reset restart counter */
|
nuclear@14
|
249 entropy->restarts_to_go = cinfo->restart_interval;
|
nuclear@14
|
250
|
nuclear@14
|
251 /* Reset out-of-data flag, unless read_restart_marker left us smack up
|
nuclear@14
|
252 * against a marker. In that case we will end up treating the next data
|
nuclear@14
|
253 * segment as empty, and we can avoid producing bogus output pixels by
|
nuclear@14
|
254 * leaving the flag set.
|
nuclear@14
|
255 */
|
nuclear@14
|
256 if (cinfo->unread_marker == 0)
|
nuclear@14
|
257 entropy->pub.insufficient_data = FALSE;
|
nuclear@14
|
258
|
nuclear@14
|
259 return TRUE;
|
nuclear@14
|
260 }
|
nuclear@14
|
261
|
nuclear@14
|
262
|
nuclear@14
|
263 /*
|
nuclear@14
|
264 * Huffman MCU decoding.
|
nuclear@14
|
265 * Each of these routines decodes and returns one MCU's worth of
|
nuclear@14
|
266 * Huffman-compressed coefficients.
|
nuclear@14
|
267 * The coefficients are reordered from zigzag order into natural array order,
|
nuclear@14
|
268 * but are not dequantized.
|
nuclear@14
|
269 *
|
nuclear@14
|
270 * The i'th block of the MCU is stored into the block pointed to by
|
nuclear@14
|
271 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
|
nuclear@14
|
272 *
|
nuclear@14
|
273 * We return FALSE if data source requested suspension. In that case no
|
nuclear@14
|
274 * changes have been made to permanent state. (Exception: some output
|
nuclear@14
|
275 * coefficients may already have been assigned. This is harmless for
|
nuclear@14
|
276 * spectral selection, since we'll just re-assign them on the next call.
|
nuclear@14
|
277 * Successive approximation AC refinement has to be more careful, however.)
|
nuclear@14
|
278 */
|
nuclear@14
|
279
|
nuclear@14
|
280 /*
|
nuclear@14
|
281 * MCU decoding for DC initial scan (either spectral selection,
|
nuclear@14
|
282 * or first pass of successive approximation).
|
nuclear@14
|
283 */
|
nuclear@14
|
284
|
nuclear@14
|
285 METHODDEF(boolean)
|
nuclear@14
|
286 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
nuclear@14
|
287 {
|
nuclear@14
|
288 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
|
nuclear@14
|
289 int Al = cinfo->Al;
|
nuclear@14
|
290 register int s, r;
|
nuclear@14
|
291 int blkn, ci;
|
nuclear@14
|
292 JBLOCKROW block;
|
nuclear@14
|
293 BITREAD_STATE_VARS;
|
nuclear@14
|
294 savable_state state;
|
nuclear@14
|
295 d_derived_tbl * tbl;
|
nuclear@14
|
296 jpeg_component_info * compptr;
|
nuclear@14
|
297
|
nuclear@14
|
298 /* Process restart marker if needed; may have to suspend */
|
nuclear@14
|
299 if (cinfo->restart_interval) {
|
nuclear@14
|
300 if (entropy->restarts_to_go == 0)
|
nuclear@14
|
301 if (! process_restart(cinfo))
|
nuclear@14
|
302 return FALSE;
|
nuclear@14
|
303 }
|
nuclear@14
|
304
|
nuclear@14
|
305 /* If we've run out of data, just leave the MCU set to zeroes.
|
nuclear@14
|
306 * This way, we return uniform gray for the remainder of the segment.
|
nuclear@14
|
307 */
|
nuclear@14
|
308 if (! entropy->pub.insufficient_data) {
|
nuclear@14
|
309
|
nuclear@14
|
310 /* Load up working state */
|
nuclear@14
|
311 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
|
nuclear@14
|
312 ASSIGN_STATE(state, entropy->saved);
|
nuclear@14
|
313
|
nuclear@14
|
314 /* Outer loop handles each block in the MCU */
|
nuclear@14
|
315
|
nuclear@14
|
316 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
nuclear@14
|
317 block = MCU_data[blkn];
|
nuclear@14
|
318 ci = cinfo->MCU_membership[blkn];
|
nuclear@14
|
319 compptr = cinfo->cur_comp_info[ci];
|
nuclear@14
|
320 tbl = entropy->derived_tbls[compptr->dc_tbl_no];
|
nuclear@14
|
321
|
nuclear@14
|
322 /* Decode a single block's worth of coefficients */
|
nuclear@14
|
323
|
nuclear@14
|
324 /* Section F.2.2.1: decode the DC coefficient difference */
|
nuclear@14
|
325 HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
|
nuclear@14
|
326 if (s) {
|
nuclear@14
|
327 CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
nuclear@14
|
328 r = GET_BITS(s);
|
nuclear@14
|
329 s = HUFF_EXTEND(r, s);
|
nuclear@14
|
330 }
|
nuclear@14
|
331
|
nuclear@14
|
332 /* Convert DC difference to actual value, update last_dc_val */
|
nuclear@14
|
333 s += state.last_dc_val[ci];
|
nuclear@14
|
334 state.last_dc_val[ci] = s;
|
nuclear@14
|
335 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
|
nuclear@14
|
336 (*block)[0] = (JCOEF) (s << Al);
|
nuclear@14
|
337 }
|
nuclear@14
|
338
|
nuclear@14
|
339 /* Completed MCU, so update state */
|
nuclear@14
|
340 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
|
nuclear@14
|
341 ASSIGN_STATE(entropy->saved, state);
|
nuclear@14
|
342 }
|
nuclear@14
|
343
|
nuclear@14
|
344 /* Account for restart interval (no-op if not using restarts) */
|
nuclear@14
|
345 entropy->restarts_to_go--;
|
nuclear@14
|
346
|
nuclear@14
|
347 return TRUE;
|
nuclear@14
|
348 }
|
nuclear@14
|
349
|
nuclear@14
|
350
|
nuclear@14
|
351 /*
|
nuclear@14
|
352 * MCU decoding for AC initial scan (either spectral selection,
|
nuclear@14
|
353 * or first pass of successive approximation).
|
nuclear@14
|
354 */
|
nuclear@14
|
355
|
nuclear@14
|
356 METHODDEF(boolean)
|
nuclear@14
|
357 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
nuclear@14
|
358 {
|
nuclear@14
|
359 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
|
nuclear@14
|
360 int Se = cinfo->Se;
|
nuclear@14
|
361 int Al = cinfo->Al;
|
nuclear@14
|
362 register int s, k, r;
|
nuclear@14
|
363 unsigned int EOBRUN;
|
nuclear@14
|
364 JBLOCKROW block;
|
nuclear@14
|
365 BITREAD_STATE_VARS;
|
nuclear@14
|
366 d_derived_tbl * tbl;
|
nuclear@14
|
367
|
nuclear@14
|
368 /* Process restart marker if needed; may have to suspend */
|
nuclear@14
|
369 if (cinfo->restart_interval) {
|
nuclear@14
|
370 if (entropy->restarts_to_go == 0)
|
nuclear@14
|
371 if (! process_restart(cinfo))
|
nuclear@14
|
372 return FALSE;
|
nuclear@14
|
373 }
|
nuclear@14
|
374
|
nuclear@14
|
375 /* If we've run out of data, just leave the MCU set to zeroes.
|
nuclear@14
|
376 * This way, we return uniform gray for the remainder of the segment.
|
nuclear@14
|
377 */
|
nuclear@14
|
378 if (! entropy->pub.insufficient_data) {
|
nuclear@14
|
379
|
nuclear@14
|
380 /* Load up working state.
|
nuclear@14
|
381 * We can avoid loading/saving bitread state if in an EOB run.
|
nuclear@14
|
382 */
|
nuclear@14
|
383 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
|
nuclear@14
|
384
|
nuclear@14
|
385 /* There is always only one block per MCU */
|
nuclear@14
|
386
|
nuclear@14
|
387 if (EOBRUN > 0) /* if it's a band of zeroes... */
|
nuclear@14
|
388 EOBRUN--; /* ...process it now (we do nothing) */
|
nuclear@14
|
389 else {
|
nuclear@14
|
390 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
|
nuclear@14
|
391 block = MCU_data[0];
|
nuclear@14
|
392 tbl = entropy->ac_derived_tbl;
|
nuclear@14
|
393
|
nuclear@14
|
394 for (k = cinfo->Ss; k <= Se; k++) {
|
nuclear@14
|
395 HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
|
nuclear@14
|
396 r = s >> 4;
|
nuclear@14
|
397 s &= 15;
|
nuclear@14
|
398 if (s) {
|
nuclear@14
|
399 k += r;
|
nuclear@14
|
400 CHECK_BIT_BUFFER(br_state, s, return FALSE);
|
nuclear@14
|
401 r = GET_BITS(s);
|
nuclear@14
|
402 s = HUFF_EXTEND(r, s);
|
nuclear@14
|
403 /* Scale and output coefficient in natural (dezigzagged) order */
|
nuclear@14
|
404 (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
|
nuclear@14
|
405 } else {
|
nuclear@14
|
406 if (r == 15) { /* ZRL */
|
nuclear@14
|
407 k += 15; /* skip 15 zeroes in band */
|
nuclear@14
|
408 } else { /* EOBr, run length is 2^r + appended bits */
|
nuclear@14
|
409 EOBRUN = 1 << r;
|
nuclear@14
|
410 if (r) { /* EOBr, r > 0 */
|
nuclear@14
|
411 CHECK_BIT_BUFFER(br_state, r, return FALSE);
|
nuclear@14
|
412 r = GET_BITS(r);
|
nuclear@14
|
413 EOBRUN += r;
|
nuclear@14
|
414 }
|
nuclear@14
|
415 EOBRUN--; /* this band is processed at this moment */
|
nuclear@14
|
416 break; /* force end-of-band */
|
nuclear@14
|
417 }
|
nuclear@14
|
418 }
|
nuclear@14
|
419 }
|
nuclear@14
|
420
|
nuclear@14
|
421 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
|
nuclear@14
|
422 }
|
nuclear@14
|
423
|
nuclear@14
|
424 /* Completed MCU, so update state */
|
nuclear@14
|
425 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
|
nuclear@14
|
426 }
|
nuclear@14
|
427
|
nuclear@14
|
428 /* Account for restart interval (no-op if not using restarts) */
|
nuclear@14
|
429 entropy->restarts_to_go--;
|
nuclear@14
|
430
|
nuclear@14
|
431 return TRUE;
|
nuclear@14
|
432 }
|
nuclear@14
|
433
|
nuclear@14
|
434
|
nuclear@14
|
435 /*
|
nuclear@14
|
436 * MCU decoding for DC successive approximation refinement scan.
|
nuclear@14
|
437 * Note: we assume such scans can be multi-component, although the spec
|
nuclear@14
|
438 * is not very clear on the point.
|
nuclear@14
|
439 */
|
nuclear@14
|
440
|
nuclear@14
|
441 METHODDEF(boolean)
|
nuclear@14
|
442 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
nuclear@14
|
443 {
|
nuclear@14
|
444 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
|
nuclear@14
|
445 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
|
nuclear@14
|
446 int blkn;
|
nuclear@14
|
447 JBLOCKROW block;
|
nuclear@14
|
448 BITREAD_STATE_VARS;
|
nuclear@14
|
449
|
nuclear@14
|
450 /* Process restart marker if needed; may have to suspend */
|
nuclear@14
|
451 if (cinfo->restart_interval) {
|
nuclear@14
|
452 if (entropy->restarts_to_go == 0)
|
nuclear@14
|
453 if (! process_restart(cinfo))
|
nuclear@14
|
454 return FALSE;
|
nuclear@14
|
455 }
|
nuclear@14
|
456
|
nuclear@14
|
457 /* Not worth the cycles to check insufficient_data here,
|
nuclear@14
|
458 * since we will not change the data anyway if we read zeroes.
|
nuclear@14
|
459 */
|
nuclear@14
|
460
|
nuclear@14
|
461 /* Load up working state */
|
nuclear@14
|
462 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
|
nuclear@14
|
463
|
nuclear@14
|
464 /* Outer loop handles each block in the MCU */
|
nuclear@14
|
465
|
nuclear@14
|
466 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
|
nuclear@14
|
467 block = MCU_data[blkn];
|
nuclear@14
|
468
|
nuclear@14
|
469 /* Encoded data is simply the next bit of the two's-complement DC value */
|
nuclear@14
|
470 CHECK_BIT_BUFFER(br_state, 1, return FALSE);
|
nuclear@14
|
471 if (GET_BITS(1))
|
nuclear@14
|
472 (*block)[0] |= p1;
|
nuclear@14
|
473 /* Note: since we use |=, repeating the assignment later is safe */
|
nuclear@14
|
474 }
|
nuclear@14
|
475
|
nuclear@14
|
476 /* Completed MCU, so update state */
|
nuclear@14
|
477 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
|
nuclear@14
|
478
|
nuclear@14
|
479 /* Account for restart interval (no-op if not using restarts) */
|
nuclear@14
|
480 entropy->restarts_to_go--;
|
nuclear@14
|
481
|
nuclear@14
|
482 return TRUE;
|
nuclear@14
|
483 }
|
nuclear@14
|
484
|
nuclear@14
|
485
|
nuclear@14
|
486 /*
|
nuclear@14
|
487 * MCU decoding for AC successive approximation refinement scan.
|
nuclear@14
|
488 */
|
nuclear@14
|
489
|
nuclear@14
|
490 METHODDEF(boolean)
|
nuclear@14
|
491 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
|
nuclear@14
|
492 {
|
nuclear@14
|
493 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
|
nuclear@14
|
494 int Se = cinfo->Se;
|
nuclear@14
|
495 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
|
nuclear@14
|
496 int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
|
nuclear@14
|
497 register int s, k, r;
|
nuclear@14
|
498 unsigned int EOBRUN;
|
nuclear@14
|
499 JBLOCKROW block;
|
nuclear@14
|
500 JCOEFPTR thiscoef;
|
nuclear@14
|
501 BITREAD_STATE_VARS;
|
nuclear@14
|
502 d_derived_tbl * tbl;
|
nuclear@14
|
503 int num_newnz;
|
nuclear@14
|
504 int newnz_pos[DCTSIZE2];
|
nuclear@14
|
505
|
nuclear@14
|
506 /* Process restart marker if needed; may have to suspend */
|
nuclear@14
|
507 if (cinfo->restart_interval) {
|
nuclear@14
|
508 if (entropy->restarts_to_go == 0)
|
nuclear@14
|
509 if (! process_restart(cinfo))
|
nuclear@14
|
510 return FALSE;
|
nuclear@14
|
511 }
|
nuclear@14
|
512
|
nuclear@14
|
513 /* If we've run out of data, don't modify the MCU.
|
nuclear@14
|
514 */
|
nuclear@14
|
515 if (! entropy->pub.insufficient_data) {
|
nuclear@14
|
516
|
nuclear@14
|
517 /* Load up working state */
|
nuclear@14
|
518 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
|
nuclear@14
|
519 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
|
nuclear@14
|
520
|
nuclear@14
|
521 /* There is always only one block per MCU */
|
nuclear@14
|
522 block = MCU_data[0];
|
nuclear@14
|
523 tbl = entropy->ac_derived_tbl;
|
nuclear@14
|
524
|
nuclear@14
|
525 /* If we are forced to suspend, we must undo the assignments to any newly
|
nuclear@14
|
526 * nonzero coefficients in the block, because otherwise we'd get confused
|
nuclear@14
|
527 * next time about which coefficients were already nonzero.
|
nuclear@14
|
528 * But we need not undo addition of bits to already-nonzero coefficients;
|
nuclear@14
|
529 * instead, we can test the current bit to see if we already did it.
|
nuclear@14
|
530 */
|
nuclear@14
|
531 num_newnz = 0;
|
nuclear@14
|
532
|
nuclear@14
|
533 /* initialize coefficient loop counter to start of band */
|
nuclear@14
|
534 k = cinfo->Ss;
|
nuclear@14
|
535
|
nuclear@14
|
536 if (EOBRUN == 0) {
|
nuclear@14
|
537 for (; k <= Se; k++) {
|
nuclear@14
|
538 HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
|
nuclear@14
|
539 r = s >> 4;
|
nuclear@14
|
540 s &= 15;
|
nuclear@14
|
541 if (s) {
|
nuclear@14
|
542 if (s != 1) /* size of new coef should always be 1 */
|
nuclear@14
|
543 WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
|
nuclear@14
|
544 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
|
nuclear@14
|
545 if (GET_BITS(1))
|
nuclear@14
|
546 s = p1; /* newly nonzero coef is positive */
|
nuclear@14
|
547 else
|
nuclear@14
|
548 s = m1; /* newly nonzero coef is negative */
|
nuclear@14
|
549 } else {
|
nuclear@14
|
550 if (r != 15) {
|
nuclear@14
|
551 EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
|
nuclear@14
|
552 if (r) {
|
nuclear@14
|
553 CHECK_BIT_BUFFER(br_state, r, goto undoit);
|
nuclear@14
|
554 r = GET_BITS(r);
|
nuclear@14
|
555 EOBRUN += r;
|
nuclear@14
|
556 }
|
nuclear@14
|
557 break; /* rest of block is handled by EOB logic */
|
nuclear@14
|
558 }
|
nuclear@14
|
559 /* note s = 0 for processing ZRL */
|
nuclear@14
|
560 }
|
nuclear@14
|
561 /* Advance over already-nonzero coefs and r still-zero coefs,
|
nuclear@14
|
562 * appending correction bits to the nonzeroes. A correction bit is 1
|
nuclear@14
|
563 * if the absolute value of the coefficient must be increased.
|
nuclear@14
|
564 */
|
nuclear@14
|
565 do {
|
nuclear@14
|
566 thiscoef = *block + jpeg_natural_order[k];
|
nuclear@14
|
567 if (*thiscoef != 0) {
|
nuclear@14
|
568 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
|
nuclear@14
|
569 if (GET_BITS(1)) {
|
nuclear@14
|
570 if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
|
nuclear@14
|
571 if (*thiscoef >= 0)
|
nuclear@14
|
572 *thiscoef += p1;
|
nuclear@14
|
573 else
|
nuclear@14
|
574 *thiscoef += m1;
|
nuclear@14
|
575 }
|
nuclear@14
|
576 }
|
nuclear@14
|
577 } else {
|
nuclear@14
|
578 if (--r < 0)
|
nuclear@14
|
579 break; /* reached target zero coefficient */
|
nuclear@14
|
580 }
|
nuclear@14
|
581 k++;
|
nuclear@14
|
582 } while (k <= Se);
|
nuclear@14
|
583 if (s) {
|
nuclear@14
|
584 int pos = jpeg_natural_order[k];
|
nuclear@14
|
585 /* Output newly nonzero coefficient */
|
nuclear@14
|
586 (*block)[pos] = (JCOEF) s;
|
nuclear@14
|
587 /* Remember its position in case we have to suspend */
|
nuclear@14
|
588 newnz_pos[num_newnz++] = pos;
|
nuclear@14
|
589 }
|
nuclear@14
|
590 }
|
nuclear@14
|
591 }
|
nuclear@14
|
592
|
nuclear@14
|
593 if (EOBRUN > 0) {
|
nuclear@14
|
594 /* Scan any remaining coefficient positions after the end-of-band
|
nuclear@14
|
595 * (the last newly nonzero coefficient, if any). Append a correction
|
nuclear@14
|
596 * bit to each already-nonzero coefficient. A correction bit is 1
|
nuclear@14
|
597 * if the absolute value of the coefficient must be increased.
|
nuclear@14
|
598 */
|
nuclear@14
|
599 for (; k <= Se; k++) {
|
nuclear@14
|
600 thiscoef = *block + jpeg_natural_order[k];
|
nuclear@14
|
601 if (*thiscoef != 0) {
|
nuclear@14
|
602 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
|
nuclear@14
|
603 if (GET_BITS(1)) {
|
nuclear@14
|
604 if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
|
nuclear@14
|
605 if (*thiscoef >= 0)
|
nuclear@14
|
606 *thiscoef += p1;
|
nuclear@14
|
607 else
|
nuclear@14
|
608 *thiscoef += m1;
|
nuclear@14
|
609 }
|
nuclear@14
|
610 }
|
nuclear@14
|
611 }
|
nuclear@14
|
612 }
|
nuclear@14
|
613 /* Count one block completed in EOB run */
|
nuclear@14
|
614 EOBRUN--;
|
nuclear@14
|
615 }
|
nuclear@14
|
616
|
nuclear@14
|
617 /* Completed MCU, so update state */
|
nuclear@14
|
618 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
|
nuclear@14
|
619 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
|
nuclear@14
|
620 }
|
nuclear@14
|
621
|
nuclear@14
|
622 /* Account for restart interval (no-op if not using restarts) */
|
nuclear@14
|
623 entropy->restarts_to_go--;
|
nuclear@14
|
624
|
nuclear@14
|
625 return TRUE;
|
nuclear@14
|
626
|
nuclear@14
|
627 undoit:
|
nuclear@14
|
628 /* Re-zero any output coefficients that we made newly nonzero */
|
nuclear@14
|
629 while (num_newnz > 0)
|
nuclear@14
|
630 (*block)[newnz_pos[--num_newnz]] = 0;
|
nuclear@14
|
631
|
nuclear@14
|
632 return FALSE;
|
nuclear@14
|
633 }
|
nuclear@14
|
634
|
nuclear@14
|
635
|
nuclear@14
|
636 /*
|
nuclear@14
|
637 * Module initialization routine for progressive Huffman entropy decoding.
|
nuclear@14
|
638 */
|
nuclear@14
|
639
|
nuclear@14
|
640 GLOBAL(void)
|
nuclear@14
|
641 jinit_phuff_decoder (j_decompress_ptr cinfo)
|
nuclear@14
|
642 {
|
nuclear@14
|
643 phuff_entropy_ptr entropy;
|
nuclear@14
|
644 int *coef_bit_ptr;
|
nuclear@14
|
645 int ci, i;
|
nuclear@14
|
646
|
nuclear@14
|
647 entropy = (phuff_entropy_ptr)
|
nuclear@14
|
648 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@14
|
649 SIZEOF(phuff_entropy_decoder));
|
nuclear@14
|
650 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
|
nuclear@14
|
651 entropy->pub.start_pass = start_pass_phuff_decoder;
|
nuclear@14
|
652
|
nuclear@14
|
653 /* Mark derived tables unallocated */
|
nuclear@14
|
654 for (i = 0; i < NUM_HUFF_TBLS; i++) {
|
nuclear@14
|
655 entropy->derived_tbls[i] = NULL;
|
nuclear@14
|
656 }
|
nuclear@14
|
657
|
nuclear@14
|
658 /* Create progression status table */
|
nuclear@14
|
659 cinfo->coef_bits = (int (*)[DCTSIZE2])
|
nuclear@14
|
660 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
|
nuclear@14
|
661 cinfo->num_components*DCTSIZE2*SIZEOF(int));
|
nuclear@14
|
662 coef_bit_ptr = & cinfo->coef_bits[0][0];
|
nuclear@14
|
663 for (ci = 0; ci < cinfo->num_components; ci++)
|
nuclear@14
|
664 for (i = 0; i < DCTSIZE2; i++)
|
nuclear@14
|
665 *coef_bit_ptr++ = -1;
|
nuclear@14
|
666 }
|
nuclear@14
|
667
|
nuclear@14
|
668 #endif /* D_PROGRESSIVE_SUPPORTED */
|