blob: eae1538360a2e55ebcbe39b85b7d34073fb065d7 [file] [log] [blame]
Thomas G. Lanebc79e061995-08-02 00:00:00 +00001/*
2 * jdphuff.c
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1995-1997, Thomas G. Lane.
DRC5033f3e2014-05-18 18:33:44 +00006 * It was modified by The libjpeg-turbo Project to include only code relevant
7 * to libjpeg-turbo.
Thomas G. Lanebc79e061995-08-02 00:00:00 +00008 * For conditions of distribution and use, see the accompanying README file.
9 *
10 * This file contains Huffman entropy decoding routines for progressive JPEG.
11 *
12 * Much of the complexity here has to do with supporting input suspension.
13 * If the data source module demands suspension, we want to be able to back
14 * up to the start of the current MCU. To do this, we copy state variables
15 * into local working storage, and update them back to the permanent
16 * storage only upon successful completion of an MCU.
17 */
18
19#define JPEG_INTERNALS
20#include "jinclude.h"
21#include "jpeglib.h"
DRCe5eaf372014-05-09 18:00:32 +000022#include "jdhuff.h" /* Declarations shared with jdhuff.c */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000023
24
25#ifdef D_PROGRESSIVE_SUPPORTED
26
27/*
28 * Expanded entropy decoder object for progressive Huffman decoding.
29 *
30 * The savable_state subrecord contains fields that change within an MCU,
31 * but must not be updated permanently until we complete the MCU.
32 */
33
34typedef struct {
DRCe5eaf372014-05-09 18:00:32 +000035 unsigned int EOBRUN; /* remaining EOBs in EOBRUN */
36 int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000037} savable_state;
38
39/* This macro is to work around compilers with missing or broken
40 * structure assignment. You'll need to fix this code if you have
41 * such a compiler and you change MAX_COMPS_IN_SCAN.
42 */
43
44#ifndef NO_STRUCT_ASSIGN
45#define ASSIGN_STATE(dest,src) ((dest) = (src))
46#else
47#if MAX_COMPS_IN_SCAN == 4
48#define ASSIGN_STATE(dest,src) \
DRCe5eaf372014-05-09 18:00:32 +000049 ((dest).EOBRUN = (src).EOBRUN, \
50 (dest).last_dc_val[0] = (src).last_dc_val[0], \
51 (dest).last_dc_val[1] = (src).last_dc_val[1], \
52 (dest).last_dc_val[2] = (src).last_dc_val[2], \
53 (dest).last_dc_val[3] = (src).last_dc_val[3])
Thomas G. Lanebc79e061995-08-02 00:00:00 +000054#endif
55#endif
56
57
58typedef struct {
59 struct jpeg_entropy_decoder pub; /* public fields */
60
61 /* These fields are loaded into local variables at start of each MCU.
62 * In case of suspension, we exit WITHOUT updating them.
63 */
DRCe5eaf372014-05-09 18:00:32 +000064 bitread_perm_state bitstate; /* Bit buffer at start of MCU */
65 savable_state saved; /* Other state at start of MCU */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000066
67 /* These fields are NOT loaded into local working state. */
DRCe5eaf372014-05-09 18:00:32 +000068 unsigned int restarts_to_go; /* MCUs left in this restart interval */
Thomas G. Lanebc79e061995-08-02 00:00:00 +000069
70 /* Pointers to derived tables (these workspaces have image lifespan) */
71 d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
72
73 d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
74} phuff_entropy_decoder;
75
76typedef phuff_entropy_decoder * phuff_entropy_ptr;
77
78/* Forward declarations */
DRCbc56b752014-05-16 10:43:44 +000079METHODDEF(boolean) decode_mcu_DC_first (j_decompress_ptr cinfo,
80 JBLOCKROW *MCU_data);
81METHODDEF(boolean) decode_mcu_AC_first (j_decompress_ptr cinfo,
82 JBLOCKROW *MCU_data);
83METHODDEF(boolean) decode_mcu_DC_refine (j_decompress_ptr cinfo,
84 JBLOCKROW *MCU_data);
85METHODDEF(boolean) decode_mcu_AC_refine (j_decompress_ptr cinfo,
86 JBLOCKROW *MCU_data);
Thomas G. Lanebc79e061995-08-02 00:00:00 +000087
88
89/*
90 * Initialize for a Huffman-compressed scan.
91 */
92
Thomas G. Lane489583f1996-02-07 00:00:00 +000093METHODDEF(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +000094start_pass_phuff_decoder (j_decompress_ptr cinfo)
95{
96 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
97 boolean is_DC_band, bad;
98 int ci, coefi, tbl;
99 int *coef_bit_ptr;
100 jpeg_component_info * compptr;
101
102 is_DC_band = (cinfo->Ss == 0);
103
104 /* Validate scan parameters */
105 bad = FALSE;
106 if (is_DC_band) {
107 if (cinfo->Se != 0)
108 bad = TRUE;
109 } else {
110 /* need not check Ss/Se < 0 since they came from unsigned bytes */
111 if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
112 bad = TRUE;
113 /* AC scans may have only one component */
114 if (cinfo->comps_in_scan != 1)
115 bad = TRUE;
116 }
117 if (cinfo->Ah != 0) {
118 /* Successive approximation refinement scan: must have Al = Ah-1. */
119 if (cinfo->Al != cinfo->Ah-1)
120 bad = TRUE;
121 }
DRCe5eaf372014-05-09 18:00:32 +0000122 if (cinfo->Al > 13) /* need not check for < 0 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000123 bad = TRUE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000124 /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
125 * but the spec doesn't say so, and we try to be liberal about what we
126 * accept. Note: large Al values could result in out-of-range DC
127 * coefficients during early scans, leading to bizarre displays due to
128 * overflows in the IDCT math. But we won't crash.
129 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000130 if (bad)
131 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
DRCe5eaf372014-05-09 18:00:32 +0000132 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000133 /* 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)
DRCe5eaf372014-05-09 18:00:32 +0000145 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000146 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) {
DRCe5eaf372014-05-09 18:00:32 +0000169 if (cinfo->Ah == 0) { /* DC refinement needs no table */
170 tbl = compptr->dc_tbl_no;
171 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
172 & entropy->derived_tbls[tbl]);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000173 }
174 } else {
175 tbl = compptr->ac_tbl_no;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000176 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
DRCe5eaf372014-05-09 18:00:32 +0000177 & entropy->derived_tbls[tbl]);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000178 /* remember the single active table */
179 entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
180 }
181 /* Initialize DC predictions to 0 */
182 entropy->saved.last_dc_val[ci] = 0;
183 }
184
185 /* Initialize bitread state variables */
186 entropy->bitstate.bits_left = 0;
187 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000188 entropy->pub.insufficient_data = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000189
190 /* Initialize private state variables */
191 entropy->saved.EOBRUN = 0;
192
193 /* Initialize restart counter */
194 entropy->restarts_to_go = cinfo->restart_interval;
195}
196
197
198/*
199 * Figure F.12: extend sign bit.
200 * On some machines, a shift and add will be faster than a table lookup.
201 */
202
DRCd4ab63d2014-02-06 19:31:50 +0000203#define AVOID_TABLES
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000204#ifdef AVOID_TABLES
205
206#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((-1)<<(s)) + 1) : (x))
207
208#else
209
210#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
211
212static const int extend_test[16] = /* entry n is 2**(n-1) */
213 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
214 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
215
216static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
217 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
218 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
219 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
220 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
221
222#endif /* AVOID_TABLES */
223
224
225/*
226 * Check for a restart marker & resynchronize decoder.
227 * Returns FALSE if must suspend.
228 */
229
Thomas G. Lane489583f1996-02-07 00:00:00 +0000230LOCAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000231process_restart (j_decompress_ptr cinfo)
232{
233 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
234 int ci;
235
236 /* Throw away any unused bits remaining in bit buffer; */
237 /* include any full bytes in next_marker's count of discarded bytes */
238 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
239 entropy->bitstate.bits_left = 0;
240
241 /* Advance past the RSTn marker */
242 if (! (*cinfo->marker->read_restart_marker) (cinfo))
243 return FALSE;
244
245 /* Re-initialize DC predictions to 0 */
246 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
247 entropy->saved.last_dc_val[ci] = 0;
248 /* Re-init EOB run count, too */
249 entropy->saved.EOBRUN = 0;
250
251 /* Reset restart counter */
252 entropy->restarts_to_go = cinfo->restart_interval;
253
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000254 /* Reset out-of-data flag, unless read_restart_marker left us smack up
255 * against a marker. In that case we will end up treating the next data
256 * segment as empty, and we can avoid producing bogus output pixels by
257 * leaving the flag set.
258 */
259 if (cinfo->unread_marker == 0)
260 entropy->pub.insufficient_data = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000261
262 return TRUE;
263}
264
265
266/*
267 * Huffman MCU decoding.
268 * Each of these routines decodes and returns one MCU's worth of
DRCe5eaf372014-05-09 18:00:32 +0000269 * Huffman-compressed coefficients.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000270 * 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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000288METHODDEF(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000289decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
DRCe5eaf372014-05-09 18:00:32 +0000290{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000291 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))
DRCe5eaf372014-05-09 18:00:32 +0000305 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000306 }
307
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000308 /* If we've run out of data, just leave the MCU set to zeroes.
309 * This way, we return uniform gray for the remainder of the segment.
310 */
311 if (! entropy->pub.insufficient_data) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000312
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000313 /* Load up working state */
314 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
315 ASSIGN_STATE(state, entropy->saved);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000316
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000317 /* Outer loop handles each block in the MCU */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000318
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000319 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
320 block = MCU_data[blkn];
321 ci = cinfo->MCU_membership[blkn];
322 compptr = cinfo->cur_comp_info[ci];
323 tbl = entropy->derived_tbls[compptr->dc_tbl_no];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000324
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000325 /* Decode a single block's worth of coefficients */
326
327 /* Section F.2.2.1: decode the DC coefficient difference */
328 HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
329 if (s) {
DRCe5eaf372014-05-09 18:00:32 +0000330 CHECK_BIT_BUFFER(br_state, s, return FALSE);
331 r = GET_BITS(s);
332 s = HUFF_EXTEND(r, s);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000333 }
334
335 /* Convert DC difference to actual value, update last_dc_val */
336 s += state.last_dc_val[ci];
337 state.last_dc_val[ci] = s;
338 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
339 (*block)[0] = (JCOEF) (s << Al);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000340 }
341
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000342 /* Completed MCU, so update state */
343 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
344 ASSIGN_STATE(entropy->saved, state);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000345 }
346
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000347 /* Account for restart interval (no-op if not using restarts) */
348 entropy->restarts_to_go--;
349
350 return TRUE;
351}
352
353
354/*
355 * MCU decoding for AC initial scan (either spectral selection,
356 * or first pass of successive approximation).
357 */
358
Thomas G. Lane489583f1996-02-07 00:00:00 +0000359METHODDEF(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000360decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
DRCe5eaf372014-05-09 18:00:32 +0000361{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000362 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
363 int Se = cinfo->Se;
364 int Al = cinfo->Al;
365 register int s, k, r;
366 unsigned int EOBRUN;
367 JBLOCKROW block;
368 BITREAD_STATE_VARS;
369 d_derived_tbl * tbl;
370
371 /* Process restart marker if needed; may have to suspend */
372 if (cinfo->restart_interval) {
373 if (entropy->restarts_to_go == 0)
374 if (! process_restart(cinfo))
DRCe5eaf372014-05-09 18:00:32 +0000375 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000376 }
377
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000378 /* If we've run out of data, just leave the MCU set to zeroes.
379 * This way, we return uniform gray for the remainder of the segment.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000380 */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000381 if (! entropy->pub.insufficient_data) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000382
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000383 /* Load up working state.
384 * We can avoid loading/saving bitread state if in an EOB run.
385 */
DRCe5eaf372014-05-09 18:00:32 +0000386 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000387
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000388 /* There is always only one block per MCU */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000389
DRCe5eaf372014-05-09 18:00:32 +0000390 if (EOBRUN > 0) /* if it's a band of zeroes... */
391 EOBRUN--; /* ...process it now (we do nothing) */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000392 else {
393 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
394 block = MCU_data[0];
395 tbl = entropy->ac_derived_tbl;
396
397 for (k = cinfo->Ss; k <= Se; k++) {
DRCe5eaf372014-05-09 18:00:32 +0000398 HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
399 r = s >> 4;
400 s &= 15;
401 if (s) {
402 k += r;
403 CHECK_BIT_BUFFER(br_state, s, return FALSE);
404 r = GET_BITS(s);
405 s = HUFF_EXTEND(r, s);
406 /* Scale and output coefficient in natural (dezigzagged) order */
407 (*block)[jpeg_natural_order[k]] = (JCOEF) (s << Al);
408 } else {
409 if (r == 15) { /* ZRL */
410 k += 15; /* skip 15 zeroes in band */
411 } else { /* EOBr, run length is 2^r + appended bits */
412 EOBRUN = 1 << r;
413 if (r) { /* EOBr, r > 0 */
414 CHECK_BIT_BUFFER(br_state, r, return FALSE);
415 r = GET_BITS(r);
416 EOBRUN += r;
417 }
418 EOBRUN--; /* this band is processed at this moment */
419 break; /* force end-of-band */
420 }
421 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000422 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000423
424 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000425 }
426
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000427 /* Completed MCU, so update state */
DRCe5eaf372014-05-09 18:00:32 +0000428 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000429 }
430
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000431 /* Account for restart interval (no-op if not using restarts) */
432 entropy->restarts_to_go--;
433
434 return TRUE;
435}
436
437
438/*
439 * MCU decoding for DC successive approximation refinement scan.
440 * Note: we assume such scans can be multi-component, although the spec
441 * is not very clear on the point.
442 */
443
Thomas G. Lane489583f1996-02-07 00:00:00 +0000444METHODDEF(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000445decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
DRCe5eaf372014-05-09 18:00:32 +0000446{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000447 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
DRCe5eaf372014-05-09 18:00:32 +0000448 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000449 int blkn;
450 JBLOCKROW block;
451 BITREAD_STATE_VARS;
452
453 /* Process restart marker if needed; may have to suspend */
454 if (cinfo->restart_interval) {
455 if (entropy->restarts_to_go == 0)
456 if (! process_restart(cinfo))
DRCe5eaf372014-05-09 18:00:32 +0000457 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000458 }
459
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000460 /* Not worth the cycles to check insufficient_data here,
461 * since we will not change the data anyway if we read zeroes.
462 */
463
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000464 /* Load up working state */
465 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
466
467 /* Outer loop handles each block in the MCU */
468
469 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
470 block = MCU_data[blkn];
471
472 /* Encoded data is simply the next bit of the two's-complement DC value */
473 CHECK_BIT_BUFFER(br_state, 1, return FALSE);
474 if (GET_BITS(1))
475 (*block)[0] |= p1;
476 /* Note: since we use |=, repeating the assignment later is safe */
477 }
478
479 /* Completed MCU, so update state */
480 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
481
482 /* Account for restart interval (no-op if not using restarts) */
483 entropy->restarts_to_go--;
484
485 return TRUE;
486}
487
488
489/*
490 * MCU decoding for AC successive approximation refinement scan.
491 */
492
Thomas G. Lane489583f1996-02-07 00:00:00 +0000493METHODDEF(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000494decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
DRCe5eaf372014-05-09 18:00:32 +0000495{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000496 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
497 int Se = cinfo->Se;
DRCe5eaf372014-05-09 18:00:32 +0000498 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
499 int m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000500 register int s, k, r;
501 unsigned int EOBRUN;
502 JBLOCKROW block;
503 JCOEFPTR thiscoef;
504 BITREAD_STATE_VARS;
505 d_derived_tbl * tbl;
506 int num_newnz;
507 int newnz_pos[DCTSIZE2];
508
509 /* Process restart marker if needed; may have to suspend */
510 if (cinfo->restart_interval) {
511 if (entropy->restarts_to_go == 0)
512 if (! process_restart(cinfo))
DRCe5eaf372014-05-09 18:00:32 +0000513 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000514 }
515
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000516 /* If we've run out of data, don't modify the MCU.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000517 */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000518 if (! entropy->pub.insufficient_data) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000519
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000520 /* Load up working state */
521 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
522 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000523
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000524 /* There is always only one block per MCU */
525 block = MCU_data[0];
526 tbl = entropy->ac_derived_tbl;
527
528 /* If we are forced to suspend, we must undo the assignments to any newly
529 * nonzero coefficients in the block, because otherwise we'd get confused
530 * next time about which coefficients were already nonzero.
531 * But we need not undo addition of bits to already-nonzero coefficients;
532 * instead, we can test the current bit to see if we already did it.
533 */
534 num_newnz = 0;
535
536 /* initialize coefficient loop counter to start of band */
537 k = cinfo->Ss;
538
539 if (EOBRUN == 0) {
540 for (; k <= Se; k++) {
DRCe5eaf372014-05-09 18:00:32 +0000541 HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
542 r = s >> 4;
543 s &= 15;
544 if (s) {
545 if (s != 1) /* size of new coef should always be 1 */
546 WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
547 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
548 if (GET_BITS(1))
549 s = p1; /* newly nonzero coef is positive */
550 else
551 s = m1; /* newly nonzero coef is negative */
552 } else {
553 if (r != 15) {
554 EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
555 if (r) {
556 CHECK_BIT_BUFFER(br_state, r, goto undoit);
557 r = GET_BITS(r);
558 EOBRUN += r;
559 }
560 break; /* rest of block is handled by EOB logic */
561 }
562 /* note s = 0 for processing ZRL */
563 }
564 /* Advance over already-nonzero coefs and r still-zero coefs,
565 * appending correction bits to the nonzeroes. A correction bit is 1
566 * if the absolute value of the coefficient must be increased.
567 */
568 do {
569 thiscoef = *block + jpeg_natural_order[k];
570 if (*thiscoef != 0) {
571 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
572 if (GET_BITS(1)) {
573 if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
574 if (*thiscoef >= 0)
575 *thiscoef += p1;
576 else
577 *thiscoef += m1;
578 }
579 }
580 } else {
581 if (--r < 0)
582 break; /* reached target zero coefficient */
583 }
584 k++;
585 } while (k <= Se);
586 if (s) {
587 int pos = jpeg_natural_order[k];
588 /* Output newly nonzero coefficient */
589 (*block)[pos] = (JCOEF) s;
590 /* Remember its position in case we have to suspend */
591 newnz_pos[num_newnz++] = pos;
592 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000593 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000594 }
595
596 if (EOBRUN > 0) {
597 /* Scan any remaining coefficient positions after the end-of-band
598 * (the last newly nonzero coefficient, if any). Append a correction
599 * bit to each already-nonzero coefficient. A correction bit is 1
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000600 * if the absolute value of the coefficient must be increased.
601 */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000602 for (; k <= Se; k++) {
DRCe5eaf372014-05-09 18:00:32 +0000603 thiscoef = *block + jpeg_natural_order[k];
604 if (*thiscoef != 0) {
605 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
606 if (GET_BITS(1)) {
607 if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
608 if (*thiscoef >= 0)
609 *thiscoef += p1;
610 else
611 *thiscoef += m1;
612 }
613 }
614 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000615 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000616 /* Count one block completed in EOB run */
617 EOBRUN--;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000618 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000619
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000620 /* Completed MCU, so update state */
621 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
622 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
623 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000624
625 /* Account for restart interval (no-op if not using restarts) */
626 entropy->restarts_to_go--;
627
628 return TRUE;
629
630undoit:
631 /* Re-zero any output coefficients that we made newly nonzero */
632 while (num_newnz > 0)
633 (*block)[newnz_pos[--num_newnz]] = 0;
634
635 return FALSE;
636}
637
638
639/*
640 * Module initialization routine for progressive Huffman entropy decoding.
641 */
642
Thomas G. Lane489583f1996-02-07 00:00:00 +0000643GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000644jinit_phuff_decoder (j_decompress_ptr cinfo)
645{
646 phuff_entropy_ptr entropy;
647 int *coef_bit_ptr;
648 int ci, i;
649
650 entropy = (phuff_entropy_ptr)
651 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000652 sizeof(phuff_entropy_decoder));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000653 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
654 entropy->pub.start_pass = start_pass_phuff_decoder;
655
656 /* Mark derived tables unallocated */
657 for (i = 0; i < NUM_HUFF_TBLS; i++) {
658 entropy->derived_tbls[i] = NULL;
659 }
660
661 /* Create progression status table */
662 cinfo->coef_bits = (int (*)[DCTSIZE2])
663 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000664 cinfo->num_components*DCTSIZE2*sizeof(int));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000665 coef_bit_ptr = & cinfo->coef_bits[0][0];
DRCe5eaf372014-05-09 18:00:32 +0000666 for (ci = 0; ci < cinfo->num_components; ci++)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000667 for (i = 0; i < DCTSIZE2; i++)
668 *coef_bit_ptr++ = -1;
669}
670
671#endif /* D_PROGRESSIVE_SUPPORTED */