blob: 7ef8e7b27a096f4db22e0efa1a1c0a6644fbf6ff [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.
DRC8e9cef22015-09-21 12:57:41 -05006 * libjpeg-turbo Modifications:
7 * Copyright (C) 2015, D. R. Commander.
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;
DRC8e9cef22015-09-21 12:57:41 -050099 d_derived_tbl **pdtbl;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000100 int *coef_bit_ptr;
101 jpeg_component_info * compptr;
102
103 is_DC_band = (cinfo->Ss == 0);
104
105 /* Validate scan parameters */
106 bad = FALSE;
107 if (is_DC_band) {
108 if (cinfo->Se != 0)
109 bad = TRUE;
110 } else {
111 /* need not check Ss/Se < 0 since they came from unsigned bytes */
112 if (cinfo->Ss > cinfo->Se || cinfo->Se >= DCTSIZE2)
113 bad = TRUE;
114 /* AC scans may have only one component */
115 if (cinfo->comps_in_scan != 1)
116 bad = TRUE;
117 }
118 if (cinfo->Ah != 0) {
119 /* Successive approximation refinement scan: must have Al = Ah-1. */
120 if (cinfo->Al != cinfo->Ah-1)
121 bad = TRUE;
122 }
DRCe5eaf372014-05-09 18:00:32 +0000123 if (cinfo->Al > 13) /* need not check for < 0 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000124 bad = TRUE;
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000125 /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
126 * but the spec doesn't say so, and we try to be liberal about what we
127 * accept. Note: large Al values could result in out-of-range DC
128 * coefficients during early scans, leading to bizarre displays due to
129 * overflows in the IDCT math. But we won't crash.
130 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000131 if (bad)
132 ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
DRCe5eaf372014-05-09 18:00:32 +0000133 cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000134 /* Update progression status, and verify that scan order is legal.
135 * Note that inter-scan inconsistencies are treated as warnings
136 * not fatal errors ... not clear if this is right way to behave.
137 */
138 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
139 int cindex = cinfo->cur_comp_info[ci]->component_index;
140 coef_bit_ptr = & cinfo->coef_bits[cindex][0];
141 if (!is_DC_band && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
142 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
143 for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
144 int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
145 if (cinfo->Ah != expected)
DRCe5eaf372014-05-09 18:00:32 +0000146 WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000147 coef_bit_ptr[coefi] = cinfo->Al;
148 }
149 }
150
151 /* Select MCU decoding routine */
152 if (cinfo->Ah == 0) {
153 if (is_DC_band)
154 entropy->pub.decode_mcu = decode_mcu_DC_first;
155 else
156 entropy->pub.decode_mcu = decode_mcu_AC_first;
157 } else {
158 if (is_DC_band)
159 entropy->pub.decode_mcu = decode_mcu_DC_refine;
160 else
161 entropy->pub.decode_mcu = decode_mcu_AC_refine;
162 }
163
164 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
165 compptr = cinfo->cur_comp_info[ci];
166 /* Make sure requested tables are present, and compute derived tables.
167 * We may build same derived table more than once, but it's not expensive.
168 */
169 if (is_DC_band) {
DRCe5eaf372014-05-09 18:00:32 +0000170 if (cinfo->Ah == 0) { /* DC refinement needs no table */
171 tbl = compptr->dc_tbl_no;
DRC8e9cef22015-09-21 12:57:41 -0500172 pdtbl = entropy->derived_tbls + tbl;
173 jpeg_make_d_derived_tbl(cinfo, TRUE, tbl, pdtbl);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000174 }
175 } else {
176 tbl = compptr->ac_tbl_no;
DRC8e9cef22015-09-21 12:57:41 -0500177 pdtbl = entropy->derived_tbls + tbl;
178 jpeg_make_d_derived_tbl(cinfo, FALSE, tbl, pdtbl);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000179 /* remember the single active table */
180 entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
181 }
182 /* Initialize DC predictions to 0 */
183 entropy->saved.last_dc_val[ci] = 0;
184 }
185
186 /* Initialize bitread state variables */
187 entropy->bitstate.bits_left = 0;
188 entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000189 entropy->pub.insufficient_data = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000190
191 /* Initialize private state variables */
192 entropy->saved.EOBRUN = 0;
193
194 /* Initialize restart counter */
195 entropy->restarts_to_go = cinfo->restart_interval;
196}
197
198
199/*
200 * Figure F.12: extend sign bit.
201 * On some machines, a shift and add will be faster than a table lookup.
202 */
203
DRCd4ab63d2014-02-06 19:31:50 +0000204#define AVOID_TABLES
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000205#ifdef AVOID_TABLES
206
DRC8e9cef22015-09-21 12:57:41 -0500207#define NEG_1 ((unsigned)-1)
208#define HUFF_EXTEND(x,s) ((x) < (1<<((s)-1)) ? (x) + (((NEG_1)<<(s)) + 1) : (x))
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000209
210#else
211
212#define HUFF_EXTEND(x,s) ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
213
214static const int extend_test[16] = /* entry n is 2**(n-1) */
215 { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
216 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
217
218static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
219 { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
220 ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1,
221 ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1,
222 ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
223
224#endif /* AVOID_TABLES */
225
226
227/*
228 * Check for a restart marker & resynchronize decoder.
229 * Returns FALSE if must suspend.
230 */
231
Thomas G. Lane489583f1996-02-07 00:00:00 +0000232LOCAL(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000233process_restart (j_decompress_ptr cinfo)
234{
235 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
236 int ci;
237
238 /* Throw away any unused bits remaining in bit buffer; */
239 /* include any full bytes in next_marker's count of discarded bytes */
240 cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
241 entropy->bitstate.bits_left = 0;
242
243 /* Advance past the RSTn marker */
244 if (! (*cinfo->marker->read_restart_marker) (cinfo))
245 return FALSE;
246
247 /* Re-initialize DC predictions to 0 */
248 for (ci = 0; ci < cinfo->comps_in_scan; ci++)
249 entropy->saved.last_dc_val[ci] = 0;
250 /* Re-init EOB run count, too */
251 entropy->saved.EOBRUN = 0;
252
253 /* Reset restart counter */
254 entropy->restarts_to_go = cinfo->restart_interval;
255
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000256 /* Reset out-of-data flag, unless read_restart_marker left us smack up
257 * against a marker. In that case we will end up treating the next data
258 * segment as empty, and we can avoid producing bogus output pixels by
259 * leaving the flag set.
260 */
261 if (cinfo->unread_marker == 0)
262 entropy->pub.insufficient_data = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000263
264 return TRUE;
265}
266
267
268/*
269 * Huffman MCU decoding.
270 * Each of these routines decodes and returns one MCU's worth of
DRCe5eaf372014-05-09 18:00:32 +0000271 * Huffman-compressed coefficients.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000272 * The coefficients are reordered from zigzag order into natural array order,
273 * but are not dequantized.
274 *
275 * The i'th block of the MCU is stored into the block pointed to by
276 * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
277 *
278 * We return FALSE if data source requested suspension. In that case no
279 * changes have been made to permanent state. (Exception: some output
280 * coefficients may already have been assigned. This is harmless for
281 * spectral selection, since we'll just re-assign them on the next call.
282 * Successive approximation AC refinement has to be more careful, however.)
283 */
284
285/*
286 * MCU decoding for DC initial scan (either spectral selection,
287 * or first pass of successive approximation).
288 */
289
Thomas G. Lane489583f1996-02-07 00:00:00 +0000290METHODDEF(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000291decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
DRCe5eaf372014-05-09 18:00:32 +0000292{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000293 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
294 int Al = cinfo->Al;
295 register int s, r;
296 int blkn, ci;
297 JBLOCKROW block;
298 BITREAD_STATE_VARS;
299 savable_state state;
300 d_derived_tbl * tbl;
301 jpeg_component_info * compptr;
302
303 /* Process restart marker if needed; may have to suspend */
304 if (cinfo->restart_interval) {
305 if (entropy->restarts_to_go == 0)
306 if (! process_restart(cinfo))
DRCe5eaf372014-05-09 18:00:32 +0000307 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000308 }
309
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000310 /* If we've run out of data, just leave the MCU set to zeroes.
311 * This way, we return uniform gray for the remainder of the segment.
312 */
313 if (! entropy->pub.insufficient_data) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000314
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000315 /* Load up working state */
316 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
317 ASSIGN_STATE(state, entropy->saved);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000318
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000319 /* Outer loop handles each block in the MCU */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000320
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000321 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
322 block = MCU_data[blkn];
323 ci = cinfo->MCU_membership[blkn];
324 compptr = cinfo->cur_comp_info[ci];
325 tbl = entropy->derived_tbls[compptr->dc_tbl_no];
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000326
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000327 /* Decode a single block's worth of coefficients */
328
329 /* Section F.2.2.1: decode the DC coefficient difference */
330 HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
331 if (s) {
DRCe5eaf372014-05-09 18:00:32 +0000332 CHECK_BIT_BUFFER(br_state, s, return FALSE);
333 r = GET_BITS(s);
334 s = HUFF_EXTEND(r, s);
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000335 }
336
337 /* Convert DC difference to actual value, update last_dc_val */
338 s += state.last_dc_val[ci];
339 state.last_dc_val[ci] = s;
340 /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
DRC8e9cef22015-09-21 12:57:41 -0500341 (*block)[0] = (JCOEF) LEFT_SHIFT(s, Al);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000342 }
343
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000344 /* Completed MCU, so update state */
345 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
346 ASSIGN_STATE(entropy->saved, state);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000347 }
348
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000349 /* Account for restart interval (no-op if not using restarts) */
350 entropy->restarts_to_go--;
351
352 return TRUE;
353}
354
355
356/*
357 * MCU decoding for AC initial scan (either spectral selection,
358 * or first pass of successive approximation).
359 */
360
Thomas G. Lane489583f1996-02-07 00:00:00 +0000361METHODDEF(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000362decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
DRCe5eaf372014-05-09 18:00:32 +0000363{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000364 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
365 int Se = cinfo->Se;
366 int Al = cinfo->Al;
367 register int s, k, r;
368 unsigned int EOBRUN;
369 JBLOCKROW block;
370 BITREAD_STATE_VARS;
371 d_derived_tbl * tbl;
372
373 /* Process restart marker if needed; may have to suspend */
374 if (cinfo->restart_interval) {
375 if (entropy->restarts_to_go == 0)
376 if (! process_restart(cinfo))
DRCe5eaf372014-05-09 18:00:32 +0000377 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000378 }
379
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000380 /* If we've run out of data, just leave the MCU set to zeroes.
381 * This way, we return uniform gray for the remainder of the segment.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000382 */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000383 if (! entropy->pub.insufficient_data) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000384
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000385 /* Load up working state.
386 * We can avoid loading/saving bitread state if in an EOB run.
387 */
DRCe5eaf372014-05-09 18:00:32 +0000388 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000389
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000390 /* There is always only one block per MCU */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000391
DRCe5eaf372014-05-09 18:00:32 +0000392 if (EOBRUN > 0) /* if it's a band of zeroes... */
393 EOBRUN--; /* ...process it now (we do nothing) */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000394 else {
395 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
396 block = MCU_data[0];
397 tbl = entropy->ac_derived_tbl;
398
399 for (k = cinfo->Ss; k <= Se; k++) {
DRCe5eaf372014-05-09 18:00:32 +0000400 HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
401 r = s >> 4;
402 s &= 15;
403 if (s) {
404 k += r;
405 CHECK_BIT_BUFFER(br_state, s, return FALSE);
406 r = GET_BITS(s);
407 s = HUFF_EXTEND(r, s);
408 /* Scale and output coefficient in natural (dezigzagged) order */
DRC8e9cef22015-09-21 12:57:41 -0500409 (*block)[jpeg_natural_order[k]] = (JCOEF) LEFT_SHIFT(s, Al);
DRCe5eaf372014-05-09 18:00:32 +0000410 } else {
411 if (r == 15) { /* ZRL */
412 k += 15; /* skip 15 zeroes in band */
413 } else { /* EOBr, run length is 2^r + appended bits */
414 EOBRUN = 1 << r;
415 if (r) { /* EOBr, r > 0 */
416 CHECK_BIT_BUFFER(br_state, r, return FALSE);
417 r = GET_BITS(r);
418 EOBRUN += r;
419 }
420 EOBRUN--; /* this band is processed at this moment */
421 break; /* force end-of-band */
422 }
423 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000424 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000425
426 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000427 }
428
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000429 /* Completed MCU, so update state */
DRCe5eaf372014-05-09 18:00:32 +0000430 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000431 }
432
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000433 /* Account for restart interval (no-op if not using restarts) */
434 entropy->restarts_to_go--;
435
436 return TRUE;
437}
438
439
440/*
441 * MCU decoding for DC successive approximation refinement scan.
442 * Note: we assume such scans can be multi-component, although the spec
443 * is not very clear on the point.
444 */
445
Thomas G. Lane489583f1996-02-07 00:00:00 +0000446METHODDEF(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000447decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
DRCe5eaf372014-05-09 18:00:32 +0000448{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000449 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
DRCe5eaf372014-05-09 18:00:32 +0000450 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000451 int blkn;
452 JBLOCKROW block;
453 BITREAD_STATE_VARS;
454
455 /* Process restart marker if needed; may have to suspend */
456 if (cinfo->restart_interval) {
457 if (entropy->restarts_to_go == 0)
458 if (! process_restart(cinfo))
DRCe5eaf372014-05-09 18:00:32 +0000459 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000460 }
461
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000462 /* Not worth the cycles to check insufficient_data here,
463 * since we will not change the data anyway if we read zeroes.
464 */
465
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000466 /* Load up working state */
467 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
468
469 /* Outer loop handles each block in the MCU */
470
471 for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
472 block = MCU_data[blkn];
473
474 /* Encoded data is simply the next bit of the two's-complement DC value */
475 CHECK_BIT_BUFFER(br_state, 1, return FALSE);
476 if (GET_BITS(1))
477 (*block)[0] |= p1;
478 /* Note: since we use |=, repeating the assignment later is safe */
479 }
480
481 /* Completed MCU, so update state */
482 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
483
484 /* Account for restart interval (no-op if not using restarts) */
485 entropy->restarts_to_go--;
486
487 return TRUE;
488}
489
490
491/*
492 * MCU decoding for AC successive approximation refinement scan.
493 */
494
Thomas G. Lane489583f1996-02-07 00:00:00 +0000495METHODDEF(boolean)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000496decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
DRCe5eaf372014-05-09 18:00:32 +0000497{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000498 phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
499 int Se = cinfo->Se;
DRC8e9cef22015-09-21 12:57:41 -0500500 int p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
501 int m1 = (NEG_1) << cinfo->Al; /* -1 in the bit position being coded */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000502 register int s, k, r;
503 unsigned int EOBRUN;
504 JBLOCKROW block;
505 JCOEFPTR thiscoef;
506 BITREAD_STATE_VARS;
507 d_derived_tbl * tbl;
508 int num_newnz;
509 int newnz_pos[DCTSIZE2];
510
511 /* Process restart marker if needed; may have to suspend */
512 if (cinfo->restart_interval) {
513 if (entropy->restarts_to_go == 0)
514 if (! process_restart(cinfo))
DRCe5eaf372014-05-09 18:00:32 +0000515 return FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000516 }
517
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000518 /* If we've run out of data, don't modify the MCU.
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000519 */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000520 if (! entropy->pub.insufficient_data) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000521
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000522 /* Load up working state */
523 BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
524 EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000525
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000526 /* There is always only one block per MCU */
527 block = MCU_data[0];
528 tbl = entropy->ac_derived_tbl;
529
530 /* If we are forced to suspend, we must undo the assignments to any newly
531 * nonzero coefficients in the block, because otherwise we'd get confused
532 * next time about which coefficients were already nonzero.
533 * But we need not undo addition of bits to already-nonzero coefficients;
534 * instead, we can test the current bit to see if we already did it.
535 */
536 num_newnz = 0;
537
538 /* initialize coefficient loop counter to start of band */
539 k = cinfo->Ss;
540
541 if (EOBRUN == 0) {
542 for (; k <= Se; k++) {
DRCe5eaf372014-05-09 18:00:32 +0000543 HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
544 r = s >> 4;
545 s &= 15;
546 if (s) {
547 if (s != 1) /* size of new coef should always be 1 */
548 WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
549 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
550 if (GET_BITS(1))
551 s = p1; /* newly nonzero coef is positive */
552 else
553 s = m1; /* newly nonzero coef is negative */
554 } else {
555 if (r != 15) {
556 EOBRUN = 1 << r; /* EOBr, run length is 2^r + appended bits */
557 if (r) {
558 CHECK_BIT_BUFFER(br_state, r, goto undoit);
559 r = GET_BITS(r);
560 EOBRUN += r;
561 }
562 break; /* rest of block is handled by EOB logic */
563 }
564 /* note s = 0 for processing ZRL */
565 }
566 /* Advance over already-nonzero coefs and r still-zero coefs,
567 * appending correction bits to the nonzeroes. A correction bit is 1
568 * if the absolute value of the coefficient must be increased.
569 */
570 do {
571 thiscoef = *block + jpeg_natural_order[k];
572 if (*thiscoef != 0) {
573 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
574 if (GET_BITS(1)) {
575 if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
576 if (*thiscoef >= 0)
577 *thiscoef += p1;
578 else
579 *thiscoef += m1;
580 }
581 }
582 } else {
583 if (--r < 0)
584 break; /* reached target zero coefficient */
585 }
586 k++;
587 } while (k <= Se);
588 if (s) {
589 int pos = jpeg_natural_order[k];
590 /* Output newly nonzero coefficient */
591 (*block)[pos] = (JCOEF) s;
592 /* Remember its position in case we have to suspend */
593 newnz_pos[num_newnz++] = pos;
594 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000595 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000596 }
597
598 if (EOBRUN > 0) {
599 /* Scan any remaining coefficient positions after the end-of-band
600 * (the last newly nonzero coefficient, if any). Append a correction
601 * bit to each already-nonzero coefficient. A correction bit is 1
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000602 * if the absolute value of the coefficient must be increased.
603 */
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000604 for (; k <= Se; k++) {
DRCe5eaf372014-05-09 18:00:32 +0000605 thiscoef = *block + jpeg_natural_order[k];
606 if (*thiscoef != 0) {
607 CHECK_BIT_BUFFER(br_state, 1, goto undoit);
608 if (GET_BITS(1)) {
609 if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
610 if (*thiscoef >= 0)
611 *thiscoef += p1;
612 else
613 *thiscoef += m1;
614 }
615 }
616 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000617 }
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000618 /* Count one block completed in EOB run */
619 EOBRUN--;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000620 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000621
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000622 /* Completed MCU, so update state */
623 BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
624 entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
625 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000626
627 /* Account for restart interval (no-op if not using restarts) */
628 entropy->restarts_to_go--;
629
630 return TRUE;
631
632undoit:
633 /* Re-zero any output coefficients that we made newly nonzero */
634 while (num_newnz > 0)
635 (*block)[newnz_pos[--num_newnz]] = 0;
636
637 return FALSE;
638}
639
640
641/*
642 * Module initialization routine for progressive Huffman entropy decoding.
643 */
644
Thomas G. Lane489583f1996-02-07 00:00:00 +0000645GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000646jinit_phuff_decoder (j_decompress_ptr cinfo)
647{
648 phuff_entropy_ptr entropy;
649 int *coef_bit_ptr;
650 int ci, i;
651
652 entropy = (phuff_entropy_ptr)
653 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000654 sizeof(phuff_entropy_decoder));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000655 cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
656 entropy->pub.start_pass = start_pass_phuff_decoder;
657
658 /* Mark derived tables unallocated */
659 for (i = 0; i < NUM_HUFF_TBLS; i++) {
660 entropy->derived_tbls[i] = NULL;
661 }
662
663 /* Create progression status table */
664 cinfo->coef_bits = (int (*)[DCTSIZE2])
665 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
DRC5de454b2014-05-18 19:04:03 +0000666 cinfo->num_components*DCTSIZE2*sizeof(int));
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000667 coef_bit_ptr = & cinfo->coef_bits[0][0];
DRCe5eaf372014-05-09 18:00:32 +0000668 for (ci = 0; ci < cinfo->num_components; ci++)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000669 for (i = 0; i < DCTSIZE2; i++)
670 *coef_bit_ptr++ = -1;
671}
672
673#endif /* D_PROGRESSIVE_SUPPORTED */