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