vrshoot

annotate libs/libjpeg/jdphuff.c @ 0:b2f14e535253

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