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