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