istereo

annotate libs/libjpeg/jdphuff.c @ 35:23e5d274b2a2

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