DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

jdphuff.cpp (23147B)


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