istereo2

annotate libs/libjpeg/jdphuff.c @ 27:f0da8b2b61ec

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