Home | History | Annotate | Download | only in libjpeg
      1 /*
      2  * jdhuff.c
      3  *
      4  * Copyright (C) 1991-1997, Thomas G. Lane.
      5  * Modified 2006-2012 by Guido Vollbeding.
      6  * This file is part of the Independent JPEG Group's software.
      7  * For conditions of distribution and use, see the accompanying README file.
      8  *
      9  * This file contains Huffman entropy decoding routines.
     10  * Both sequential and progressive modes are supported in this single module.
     11  *
     12  * Much of the complexity here has to do with supporting input suspension.
     13  * If the data source module demands suspension, we want to be able to back
     14  * up to the start of the current MCU.  To do this, we copy state variables
     15  * into local working storage, and update them back to the permanent
     16  * storage only upon successful completion of an MCU.
     17  */
     18 
     19 #define JPEG_INTERNALS
     20 #include "jinclude.h"
     21 #include "jpeglib.h"
     22 
     23 
     24 /* Derived data constructed for each Huffman table */
     25 
     26 #define HUFF_LOOKAHEAD	8	/* # of bits of lookahead */
     27 
     28 typedef struct {
     29   /* Basic tables: (element [0] of each array is unused) */
     30   INT32 maxcode[18];		/* largest code of length k (-1 if none) */
     31   /* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */
     32   INT32 valoffset[17];		/* huffval[] offset for codes of length k */
     33   /* valoffset[k] = huffval[] index of 1st symbol of code length k, less
     34    * the smallest code of length k; so given a code of length k, the
     35    * corresponding symbol is huffval[code + valoffset[k]]
     36    */
     37 
     38   /* Link to public Huffman table (needed only in jpeg_huff_decode) */
     39   JHUFF_TBL *pub;
     40 
     41   /* Lookahead tables: indexed by the next HUFF_LOOKAHEAD bits of
     42    * the input data stream.  If the next Huffman code is no more
     43    * than HUFF_LOOKAHEAD bits long, we can obtain its length and
     44    * the corresponding symbol directly from these tables.
     45    */
     46   int look_nbits[1<<HUFF_LOOKAHEAD]; /* # bits, or 0 if too long */
     47   UINT8 look_sym[1<<HUFF_LOOKAHEAD]; /* symbol, or unused */
     48 } d_derived_tbl;
     49 
     50 
     51 /*
     52  * Fetching the next N bits from the input stream is a time-critical operation
     53  * for the Huffman decoders.  We implement it with a combination of inline
     54  * macros and out-of-line subroutines.  Note that N (the number of bits
     55  * demanded at one time) never exceeds 15 for JPEG use.
     56  *
     57  * We read source bytes into get_buffer and dole out bits as needed.
     58  * If get_buffer already contains enough bits, they are fetched in-line
     59  * by the macros CHECK_BIT_BUFFER and GET_BITS.  When there aren't enough
     60  * bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer
     61  * as full as possible (not just to the number of bits needed; this
     62  * prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).
     63  * Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.
     64  * On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains
     65  * at least the requested number of bits --- dummy zeroes are inserted if
     66  * necessary.
     67  */
     68 
     69 typedef INT32 bit_buf_type;	/* type of bit-extraction buffer */
     70 #define BIT_BUF_SIZE  32	/* size of buffer in bits */
     71 
     72 /* If long is > 32 bits on your machine, and shifting/masking longs is
     73  * reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE
     74  * appropriately should be a win.  Unfortunately we can't define the size
     75  * with something like  #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)
     76  * because not all machines measure sizeof in 8-bit bytes.
     77  */
     78 
     79 typedef struct {		/* Bitreading state saved across MCUs */
     80   bit_buf_type get_buffer;	/* current bit-extraction buffer */
     81   int bits_left;		/* # of unused bits in it */
     82 } bitread_perm_state;
     83 
     84 typedef struct {		/* Bitreading working state within an MCU */
     85   /* Current data source location */
     86   /* We need a copy, rather than munging the original, in case of suspension */
     87   const JOCTET * next_input_byte; /* => next byte to read from source */
     88   size_t bytes_in_buffer;	/* # of bytes remaining in source buffer */
     89   /* Bit input buffer --- note these values are kept in register variables,
     90    * not in this struct, inside the inner loops.
     91    */
     92   bit_buf_type get_buffer;	/* current bit-extraction buffer */
     93   int bits_left;		/* # of unused bits in it */
     94   /* Pointer needed by jpeg_fill_bit_buffer. */
     95   j_decompress_ptr cinfo;	/* back link to decompress master record */
     96 } bitread_working_state;
     97 
     98 /* Macros to declare and load/save bitread local variables. */
     99 #define BITREAD_STATE_VARS  \
    100         register bit_buf_type get_buffer;  \
    101         register int bits_left;  \
    102         bitread_working_state br_state
    103 
    104 #define BITREAD_LOAD_STATE(cinfop,permstate)  \
    105         br_state.cinfo = cinfop; \
    106         br_state.next_input_byte = cinfop->src->next_input_byte; \
    107         br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \
    108         get_buffer = permstate.get_buffer; \
    109         bits_left = permstate.bits_left;
    110 
    111 #define BITREAD_SAVE_STATE(cinfop,permstate)  \
    112         cinfop->src->next_input_byte = br_state.next_input_byte; \
    113         cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \
    114         permstate.get_buffer = get_buffer; \
    115         permstate.bits_left = bits_left
    116 
    117 /*
    118  * These macros provide the in-line portion of bit fetching.
    119  * Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer
    120  * before using GET_BITS, PEEK_BITS, or DROP_BITS.
    121  * The variables get_buffer and bits_left are assumed to be locals,
    122  * but the state struct might not be (jpeg_huff_decode needs this).
    123  *	CHECK_BIT_BUFFER(state,n,action);
    124  *		Ensure there are N bits in get_buffer; if suspend, take action.
    125  *      val = GET_BITS(n);
    126  *		Fetch next N bits.
    127  *      val = PEEK_BITS(n);
    128  *		Fetch next N bits without removing them from the buffer.
    129  *	DROP_BITS(n);
    130  *		Discard next N bits.
    131  * The value N should be a simple variable, not an expression, because it
    132  * is evaluated multiple times.
    133  */
    134 
    135 #define CHECK_BIT_BUFFER(state,nbits,action) \
    136         { if (bits_left < (nbits)) {  \
    137             if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \
    138               { action; }  \
    139             get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }
    140 
    141 #define GET_BITS(nbits) \
    142         (((int) (get_buffer >> (bits_left -= (nbits)))) & BIT_MASK(nbits))
    143 
    144 #define PEEK_BITS(nbits) \
    145         (((int) (get_buffer >> (bits_left -  (nbits)))) & BIT_MASK(nbits))
    146 
    147 #define DROP_BITS(nbits) \
    148         (bits_left -= (nbits))
    149 
    150 
    151 /*
    152  * Code for extracting next Huffman-coded symbol from input bit stream.
    153  * Again, this is time-critical and we make the main paths be macros.
    154  *
    155  * We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits
    156  * without looping.  Usually, more than 95% of the Huffman codes will be 8
    157  * or fewer bits long.  The few overlength codes are handled with a loop,
    158  * which need not be inline code.
    159  *
    160  * Notes about the HUFF_DECODE macro:
    161  * 1. Near the end of the data segment, we may fail to get enough bits
    162  *    for a lookahead.  In that case, we do it the hard way.
    163  * 2. If the lookahead table contains no entry, the next code must be
    164  *    more than HUFF_LOOKAHEAD bits long.
    165  * 3. jpeg_huff_decode returns -1 if forced to suspend.
    166  */
    167 
    168 #define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \
    169 { register int nb, look; \
    170   if (bits_left < HUFF_LOOKAHEAD) { \
    171     if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \
    172     get_buffer = state.get_buffer; bits_left = state.bits_left; \
    173     if (bits_left < HUFF_LOOKAHEAD) { \
    174       nb = 1; goto slowlabel; \
    175     } \
    176   } \
    177   look = PEEK_BITS(HUFF_LOOKAHEAD); \
    178   if ((nb = htbl->look_nbits[look]) != 0) { \
    179     DROP_BITS(nb); \
    180     result = htbl->look_sym[look]; \
    181   } else { \
    182     nb = HUFF_LOOKAHEAD+1; \
    183 slowlabel: \
    184     if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \
    185         { failaction; } \
    186     get_buffer = state.get_buffer; bits_left = state.bits_left; \
    187   } \
    188 }
    189 
    190 
    191 /*
    192  * Expanded entropy decoder object for Huffman decoding.
    193  *
    194  * The savable_state subrecord contains fields that change within an MCU,
    195  * but must not be updated permanently until we complete the MCU.
    196  */
    197 
    198 typedef struct {
    199   unsigned int EOBRUN;			/* remaining EOBs in EOBRUN */
    200   int last_dc_val[MAX_COMPS_IN_SCAN];	/* last DC coef for each component */
    201 } savable_state;
    202 
    203 /* This macro is to work around compilers with missing or broken
    204  * structure assignment.  You'll need to fix this code if you have
    205  * such a compiler and you change MAX_COMPS_IN_SCAN.
    206  */
    207 
    208 #ifndef NO_STRUCT_ASSIGN
    209 #define ASSIGN_STATE(dest,src)  ((dest) = (src))
    210 #else
    211 #if MAX_COMPS_IN_SCAN == 4
    212 #define ASSIGN_STATE(dest,src)  \
    213         ((dest).EOBRUN = (src).EOBRUN, \
    214          (dest).last_dc_val[0] = (src).last_dc_val[0], \
    215          (dest).last_dc_val[1] = (src).last_dc_val[1], \
    216          (dest).last_dc_val[2] = (src).last_dc_val[2], \
    217          (dest).last_dc_val[3] = (src).last_dc_val[3])
    218 #endif
    219 #endif
    220 
    221 
    222 typedef struct {
    223   struct jpeg_entropy_decoder pub; /* public fields */
    224 
    225   /* These fields are loaded into local variables at start of each MCU.
    226    * In case of suspension, we exit WITHOUT updating them.
    227    */
    228   bitread_perm_state bitstate;	/* Bit buffer at start of MCU */
    229   savable_state saved;		/* Other state at start of MCU */
    230 
    231   /* These fields are NOT loaded into local working state. */
    232   boolean insufficient_data;	/* set TRUE after emitting warning */
    233   unsigned int restarts_to_go;	/* MCUs left in this restart interval */
    234 
    235   /* Following two fields used only in progressive mode */
    236 
    237   /* Pointers to derived tables (these workspaces have image lifespan) */
    238   d_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
    239 
    240   d_derived_tbl * ac_derived_tbl; /* active table during an AC scan */
    241 
    242   /* Following fields used only in sequential mode */
    243 
    244   /* Pointers to derived tables (these workspaces have image lifespan) */
    245   d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
    246   d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
    247 
    248   /* Precalculated info set up by start_pass for use in decode_mcu: */
    249 
    250   /* Pointers to derived tables to be used for each block within an MCU */
    251   d_derived_tbl * dc_cur_tbls[D_MAX_BLOCKS_IN_MCU];
    252   d_derived_tbl * ac_cur_tbls[D_MAX_BLOCKS_IN_MCU];
    253   /* Whether we care about the DC and AC coefficient values for each block */
    254   int coef_limit[D_MAX_BLOCKS_IN_MCU];
    255 } huff_entropy_decoder;
    256 
    257 typedef huff_entropy_decoder * huff_entropy_ptr;
    258 
    259 
    260 static const int jpeg_zigzag_order[8][8] = {
    261   {  0,  1,  5,  6, 14, 15, 27, 28 },
    262   {  2,  4,  7, 13, 16, 26, 29, 42 },
    263   {  3,  8, 12, 17, 25, 30, 41, 43 },
    264   {  9, 11, 18, 24, 31, 40, 44, 53 },
    265   { 10, 19, 23, 32, 39, 45, 52, 54 },
    266   { 20, 22, 33, 38, 46, 51, 55, 60 },
    267   { 21, 34, 37, 47, 50, 56, 59, 61 },
    268   { 35, 36, 48, 49, 57, 58, 62, 63 }
    269 };
    270 
    271 static const int jpeg_zigzag_order7[7][7] = {
    272   {  0,  1,  5,  6, 14, 15, 27 },
    273   {  2,  4,  7, 13, 16, 26, 28 },
    274   {  3,  8, 12, 17, 25, 29, 38 },
    275   {  9, 11, 18, 24, 30, 37, 39 },
    276   { 10, 19, 23, 31, 36, 40, 45 },
    277   { 20, 22, 32, 35, 41, 44, 46 },
    278   { 21, 33, 34, 42, 43, 47, 48 }
    279 };
    280 
    281 static const int jpeg_zigzag_order6[6][6] = {
    282   {  0,  1,  5,  6, 14, 15 },
    283   {  2,  4,  7, 13, 16, 25 },
    284   {  3,  8, 12, 17, 24, 26 },
    285   {  9, 11, 18, 23, 27, 32 },
    286   { 10, 19, 22, 28, 31, 33 },
    287   { 20, 21, 29, 30, 34, 35 }
    288 };
    289 
    290 static const int jpeg_zigzag_order5[5][5] = {
    291   {  0,  1,  5,  6, 14 },
    292   {  2,  4,  7, 13, 15 },
    293   {  3,  8, 12, 16, 21 },
    294   {  9, 11, 17, 20, 22 },
    295   { 10, 18, 19, 23, 24 }
    296 };
    297 
    298 static const int jpeg_zigzag_order4[4][4] = {
    299   { 0,  1,  5,  6 },
    300   { 2,  4,  7, 12 },
    301   { 3,  8, 11, 13 },
    302   { 9, 10, 14, 15 }
    303 };
    304 
    305 static const int jpeg_zigzag_order3[3][3] = {
    306   { 0, 1, 5 },
    307   { 2, 4, 6 },
    308   { 3, 7, 8 }
    309 };
    310 
    311 static const int jpeg_zigzag_order2[2][2] = {
    312   { 0, 1 },
    313   { 2, 3 }
    314 };
    315 
    316 
    317 /*
    318  * Compute the derived values for a Huffman table.
    319  * This routine also performs some validation checks on the table.
    320  */
    321 
    322 LOCAL(void)
    323 jpeg_make_d_derived_tbl (j_decompress_ptr cinfo, boolean isDC, int tblno,
    324                          d_derived_tbl ** pdtbl)
    325 {
    326   JHUFF_TBL *htbl;
    327   d_derived_tbl *dtbl;
    328   int p, i, l, si, numsymbols;
    329   int lookbits, ctr;
    330   char huffsize[257];
    331   unsigned int huffcode[257];
    332   unsigned int code;
    333 
    334   /* Note that huffsize[] and huffcode[] are filled in code-length order,
    335    * paralleling the order of the symbols themselves in htbl->huffval[].
    336    */
    337 
    338   /* Find the input Huffman table */
    339   if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
    340     ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
    341   htbl =
    342     isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
    343   if (htbl == NULL)
    344     ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
    345 
    346   /* Allocate a workspace if we haven't already done so. */
    347   if (*pdtbl == NULL)
    348     *pdtbl = (d_derived_tbl *)
    349       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
    350                                   SIZEOF(d_derived_tbl));
    351   dtbl = *pdtbl;
    352   dtbl->pub = htbl;		/* fill in back link */
    353 
    354   /* Figure C.1: make table of Huffman code length for each symbol */
    355 
    356   p = 0;
    357   for (l = 1; l <= 16; l++) {
    358     i = (int) htbl->bits[l];
    359     if (i < 0 || p + i > 256)	/* protect against table overrun */
    360       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
    361     while (i--)
    362       huffsize[p++] = (char) l;
    363   }
    364   huffsize[p] = 0;
    365   numsymbols = p;
    366 
    367   /* Figure C.2: generate the codes themselves */
    368   /* We also validate that the counts represent a legal Huffman code tree. */
    369 
    370   code = 0;
    371   si = huffsize[0];
    372   p = 0;
    373   while (huffsize[p]) {
    374     while (((int) huffsize[p]) == si) {
    375       huffcode[p++] = code;
    376       code++;
    377     }
    378     /* code is now 1 more than the last code used for codelength si; but
    379      * it must still fit in si bits, since no code is allowed to be all ones.
    380      */
    381     if (((INT32) code) >= (((INT32) 1) << si))
    382       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
    383     code <<= 1;
    384     si++;
    385   }
    386 
    387   /* Figure F.15: generate decoding tables for bit-sequential decoding */
    388 
    389   p = 0;
    390   for (l = 1; l <= 16; l++) {
    391     if (htbl->bits[l]) {
    392       /* valoffset[l] = huffval[] index of 1st symbol of code length l,
    393        * minus the minimum code of length l
    394        */
    395       dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];
    396       p += htbl->bits[l];
    397       dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */
    398     } else {
    399       dtbl->maxcode[l] = -1;	/* -1 if no codes of this length */
    400     }
    401   }
    402   dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */
    403 
    404   /* Compute lookahead tables to speed up decoding.
    405    * First we set all the table entries to 0, indicating "too long";
    406    * then we iterate through the Huffman codes that are short enough and
    407    * fill in all the entries that correspond to bit sequences starting
    408    * with that code.
    409    */
    410 
    411   MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));
    412 
    413   p = 0;
    414   for (l = 1; l <= HUFF_LOOKAHEAD; l++) {
    415     for (i = 1; i <= (int) htbl->bits[l]; i++, p++) {
    416       /* l = current code's length, p = its index in huffcode[] & huffval[]. */
    417       /* Generate left-justified code followed by all possible bit sequences */
    418       lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);
    419       for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) {
    420         dtbl->look_nbits[lookbits] = l;
    421         dtbl->look_sym[lookbits] = htbl->huffval[p];
    422         lookbits++;
    423       }
    424     }
    425   }
    426 
    427   /* Validate symbols as being reasonable.
    428    * For AC tables, we make no check, but accept all byte values 0..255.
    429    * For DC tables, we require the symbols to be in range 0..15.
    430    * (Tighter bounds could be applied depending on the data depth and mode,
    431    * but this is sufficient to ensure safe decoding.)
    432    */
    433   if (isDC) {
    434     for (i = 0; i < numsymbols; i++) {
    435       int sym = htbl->huffval[i];
    436       if (sym < 0 || sym > 15)
    437         ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
    438     }
    439   }
    440 }
    441 
    442 
    443 /*
    444  * Out-of-line code for bit fetching.
    445  * Note: current values of get_buffer and bits_left are passed as parameters,
    446  * but are returned in the corresponding fields of the state struct.
    447  *
    448  * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
    449  * of get_buffer to be used.  (On machines with wider words, an even larger
    450  * buffer could be used.)  However, on some machines 32-bit shifts are
    451  * quite slow and take time proportional to the number of places shifted.
    452  * (This is true with most PC compilers, for instance.)  In this case it may
    453  * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the
    454  * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.
    455  */
    456 
    457 #ifdef SLOW_SHIFT_32
    458 #define MIN_GET_BITS  15	/* minimum allowable value */
    459 #else
    460 #define MIN_GET_BITS  (BIT_BUF_SIZE-7)
    461 #endif
    462 
    463 
    464 LOCAL(boolean)
    465 jpeg_fill_bit_buffer (bitread_working_state * state,
    466                       register bit_buf_type get_buffer, register int bits_left,
    467                       int nbits)
    468 /* Load up the bit buffer to a depth of at least nbits */
    469 {
    470   /* Copy heavily used state fields into locals (hopefully registers) */
    471   register const JOCTET * next_input_byte = state->next_input_byte;
    472   register size_t bytes_in_buffer = state->bytes_in_buffer;
    473   j_decompress_ptr cinfo = state->cinfo;
    474 
    475   /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */
    476   /* (It is assumed that no request will be for more than that many bits.) */
    477   /* We fail to do so only if we hit a marker or are forced to suspend. */
    478 
    479   if (cinfo->unread_marker == 0) {	/* cannot advance past a marker */
    480     while (bits_left < MIN_GET_BITS) {
    481       register int c;
    482 
    483       /* Attempt to read a byte */
    484       if (bytes_in_buffer == 0) {
    485         if (! (*cinfo->src->fill_input_buffer) (cinfo))
    486           return FALSE;
    487         next_input_byte = cinfo->src->next_input_byte;
    488         bytes_in_buffer = cinfo->src->bytes_in_buffer;
    489       }
    490       bytes_in_buffer--;
    491       c = GETJOCTET(*next_input_byte++);
    492 
    493       /* If it's 0xFF, check and discard stuffed zero byte */
    494       if (c == 0xFF) {
    495         /* Loop here to discard any padding FF's on terminating marker,
    496          * so that we can save a valid unread_marker value.  NOTE: we will
    497          * accept multiple FF's followed by a 0 as meaning a single FF data
    498          * byte.  This data pattern is not valid according to the standard.
    499          */
    500         do {
    501           if (bytes_in_buffer == 0) {
    502             if (! (*cinfo->src->fill_input_buffer) (cinfo))
    503               return FALSE;
    504             next_input_byte = cinfo->src->next_input_byte;
    505             bytes_in_buffer = cinfo->src->bytes_in_buffer;
    506           }
    507           bytes_in_buffer--;
    508           c = GETJOCTET(*next_input_byte++);
    509         } while (c == 0xFF);
    510 
    511         if (c == 0) {
    512           /* Found FF/00, which represents an FF data byte */
    513           c = 0xFF;
    514         } else {
    515           /* Oops, it's actually a marker indicating end of compressed data.
    516            * Save the marker code for later use.
    517            * Fine point: it might appear that we should save the marker into
    518            * bitread working state, not straight into permanent state.  But
    519            * once we have hit a marker, we cannot need to suspend within the
    520            * current MCU, because we will read no more bytes from the data
    521            * source.  So it is OK to update permanent state right away.
    522            */
    523           cinfo->unread_marker = c;
    524           /* See if we need to insert some fake zero bits. */
    525           goto no_more_bytes;
    526         }
    527       }
    528 
    529       /* OK, load c into get_buffer */
    530       get_buffer = (get_buffer << 8) | c;
    531       bits_left += 8;
    532     } /* end while */
    533   } else {
    534   no_more_bytes:
    535     /* We get here if we've read the marker that terminates the compressed
    536      * data segment.  There should be enough bits in the buffer register
    537      * to satisfy the request; if so, no problem.
    538      */
    539     if (nbits > bits_left) {
    540       /* Uh-oh.  Report corrupted data to user and stuff zeroes into
    541        * the data stream, so that we can produce some kind of image.
    542        * We use a nonvolatile flag to ensure that only one warning message
    543        * appears per data segment.
    544        */
    545       if (! ((huff_entropy_ptr) cinfo->entropy)->insufficient_data) {
    546         WARNMS(cinfo, JWRN_HIT_MARKER);
    547         ((huff_entropy_ptr) cinfo->entropy)->insufficient_data = TRUE;
    548       }
    549       /* Fill the buffer with zero bits */
    550       get_buffer <<= MIN_GET_BITS - bits_left;
    551       bits_left = MIN_GET_BITS;
    552     }
    553   }
    554 
    555   /* Unload the local registers */
    556   state->next_input_byte = next_input_byte;
    557   state->bytes_in_buffer = bytes_in_buffer;
    558   state->get_buffer = get_buffer;
    559   state->bits_left = bits_left;
    560 
    561   return TRUE;
    562 }
    563 
    564 
    565 /*
    566  * Figure F.12: extend sign bit.
    567  * On some machines, a shift and sub will be faster than a table lookup.
    568  */
    569 
    570 #ifdef AVOID_TABLES
    571 
    572 #define BIT_MASK(nbits)   ((1<<(nbits))-1)
    573 #define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) - ((1<<(s))-1) : (x))
    574 
    575 #else
    576 
    577 #define BIT_MASK(nbits)   bmask[nbits]
    578 #define HUFF_EXTEND(x,s)  ((x) <= bmask[(s) - 1] ? (x) - bmask[s] : (x))
    579 
    580 static const int bmask[16] =	/* bmask[n] is mask for n rightmost bits */
    581   { 0, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF,
    582     0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF };
    583 
    584 #endif /* AVOID_TABLES */
    585 
    586 
    587 /*
    588  * Out-of-line code for Huffman code decoding.
    589  */
    590 
    591 LOCAL(int)
    592 jpeg_huff_decode (bitread_working_state * state,
    593                   register bit_buf_type get_buffer, register int bits_left,
    594                   d_derived_tbl * htbl, int min_bits)
    595 {
    596   register int l = min_bits;
    597   register INT32 code;
    598 
    599   /* HUFF_DECODE has determined that the code is at least min_bits */
    600   /* bits long, so fetch that many bits in one swoop. */
    601 
    602   CHECK_BIT_BUFFER(*state, l, return -1);
    603   code = GET_BITS(l);
    604 
    605   /* Collect the rest of the Huffman code one bit at a time. */
    606   /* This is per Figure F.16 in the JPEG spec. */
    607 
    608   while (code > htbl->maxcode[l]) {
    609     code <<= 1;
    610     CHECK_BIT_BUFFER(*state, 1, return -1);
    611     code |= GET_BITS(1);
    612     l++;
    613   }
    614 
    615   /* Unload the local registers */
    616   state->get_buffer = get_buffer;
    617   state->bits_left = bits_left;
    618 
    619   /* With garbage input we may reach the sentinel value l = 17. */
    620 
    621   if (l > 16) {
    622     WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);
    623     return 0;			/* fake a zero as the safest result */
    624   }
    625 
    626   return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];
    627 }
    628 
    629 
    630 /*
    631  * Check for a restart marker & resynchronize decoder.
    632  * Returns FALSE if must suspend.
    633  */
    634 
    635 LOCAL(boolean)
    636 process_restart (j_decompress_ptr cinfo)
    637 {
    638   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    639   int ci;
    640 
    641   /* Throw away any unused bits remaining in bit buffer; */
    642   /* include any full bytes in next_marker's count of discarded bytes */
    643   cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
    644   entropy->bitstate.bits_left = 0;
    645 
    646   /* Advance past the RSTn marker */
    647   if (! (*cinfo->marker->read_restart_marker) (cinfo))
    648     return FALSE;
    649 
    650   /* Re-initialize DC predictions to 0 */
    651   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
    652     entropy->saved.last_dc_val[ci] = 0;
    653   /* Re-init EOB run count, too */
    654   entropy->saved.EOBRUN = 0;
    655 
    656   /* Reset restart counter */
    657   entropy->restarts_to_go = cinfo->restart_interval;
    658 
    659   /* Reset out-of-data flag, unless read_restart_marker left us smack up
    660    * against a marker.  In that case we will end up treating the next data
    661    * segment as empty, and we can avoid producing bogus output pixels by
    662    * leaving the flag set.
    663    */
    664   if (cinfo->unread_marker == 0)
    665     entropy->insufficient_data = FALSE;
    666 
    667   return TRUE;
    668 }
    669 
    670 
    671 /*
    672  * Huffman MCU decoding.
    673  * Each of these routines decodes and returns one MCU's worth of
    674  * Huffman-compressed coefficients.
    675  * The coefficients are reordered from zigzag order into natural array order,
    676  * but are not dequantized.
    677  *
    678  * The i'th block of the MCU is stored into the block pointed to by
    679  * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
    680  * (Wholesale zeroing is usually a little faster than retail...)
    681  *
    682  * We return FALSE if data source requested suspension.  In that case no
    683  * changes have been made to permanent state.  (Exception: some output
    684  * coefficients may already have been assigned.  This is harmless for
    685  * spectral selection, since we'll just re-assign them on the next call.
    686  * Successive approximation AC refinement has to be more careful, however.)
    687  */
    688 
    689 /*
    690  * MCU decoding for DC initial scan (either spectral selection,
    691  * or first pass of successive approximation).
    692  */
    693 
    694 METHODDEF(boolean)
    695 decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
    696 {
    697   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    698   int Al = cinfo->Al;
    699   register int s, r;
    700   int blkn, ci;
    701   JBLOCKROW block;
    702   BITREAD_STATE_VARS;
    703   savable_state state;
    704   d_derived_tbl * tbl;
    705   jpeg_component_info * compptr;
    706 
    707   /* Process restart marker if needed; may have to suspend */
    708   if (cinfo->restart_interval) {
    709     if (entropy->restarts_to_go == 0)
    710       if (! process_restart(cinfo))
    711         return FALSE;
    712   }
    713 
    714   /* If we've run out of data, just leave the MCU set to zeroes.
    715    * This way, we return uniform gray for the remainder of the segment.
    716    */
    717   if (! entropy->insufficient_data) {
    718 
    719     /* Load up working state */
    720     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
    721     ASSIGN_STATE(state, entropy->saved);
    722 
    723     /* Outer loop handles each block in the MCU */
    724 
    725     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
    726       block = MCU_data[blkn];
    727       ci = cinfo->MCU_membership[blkn];
    728       compptr = cinfo->cur_comp_info[ci];
    729       tbl = entropy->derived_tbls[compptr->dc_tbl_no];
    730 
    731       /* Decode a single block's worth of coefficients */
    732 
    733       /* Section F.2.2.1: decode the DC coefficient difference */
    734       HUFF_DECODE(s, br_state, tbl, return FALSE, label1);
    735       if (s) {
    736         CHECK_BIT_BUFFER(br_state, s, return FALSE);
    737         r = GET_BITS(s);
    738         s = HUFF_EXTEND(r, s);
    739       }
    740 
    741       /* Convert DC difference to actual value, update last_dc_val */
    742       s += state.last_dc_val[ci];
    743       state.last_dc_val[ci] = s;
    744       /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */
    745       (*block)[0] = (JCOEF) (s << Al);
    746     }
    747 
    748     /* Completed MCU, so update state */
    749     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
    750     ASSIGN_STATE(entropy->saved, state);
    751   }
    752 
    753   /* Account for restart interval (no-op if not using restarts) */
    754   entropy->restarts_to_go--;
    755 
    756   return TRUE;
    757 }
    758 
    759 
    760 /*
    761  * MCU decoding for AC initial scan (either spectral selection,
    762  * or first pass of successive approximation).
    763  */
    764 
    765 METHODDEF(boolean)
    766 decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
    767 {
    768   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    769   register int s, k, r;
    770   unsigned int EOBRUN;
    771   int Se, Al;
    772   const int * natural_order;
    773   JBLOCKROW block;
    774   BITREAD_STATE_VARS;
    775   d_derived_tbl * tbl;
    776 
    777   /* Process restart marker if needed; may have to suspend */
    778   if (cinfo->restart_interval) {
    779     if (entropy->restarts_to_go == 0)
    780       if (! process_restart(cinfo))
    781         return FALSE;
    782   }
    783 
    784   /* If we've run out of data, just leave the MCU set to zeroes.
    785    * This way, we return uniform gray for the remainder of the segment.
    786    */
    787   if (! entropy->insufficient_data) {
    788 
    789     Se = cinfo->Se;
    790     Al = cinfo->Al;
    791     natural_order = cinfo->natural_order;
    792 
    793     /* Load up working state.
    794      * We can avoid loading/saving bitread state if in an EOB run.
    795      */
    796     EOBRUN = entropy->saved.EOBRUN;	/* only part of saved state we need */
    797 
    798     /* There is always only one block per MCU */
    799 
    800     if (EOBRUN)			/* if it's a band of zeroes... */
    801       EOBRUN--;			/* ...process it now (we do nothing) */
    802     else {
    803       BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
    804       block = MCU_data[0];
    805       tbl = entropy->ac_derived_tbl;
    806 
    807       for (k = cinfo->Ss; k <= Se; k++) {
    808         HUFF_DECODE(s, br_state, tbl, return FALSE, label2);
    809         r = s >> 4;
    810         s &= 15;
    811         if (s) {
    812           k += r;
    813           CHECK_BIT_BUFFER(br_state, s, return FALSE);
    814           r = GET_BITS(s);
    815           s = HUFF_EXTEND(r, s);
    816           /* Scale and output coefficient in natural (dezigzagged) order */
    817           (*block)[natural_order[k]] = (JCOEF) (s << Al);
    818         } else {
    819           if (r != 15) {	/* EOBr, run length is 2^r + appended bits */
    820             if (r) {		/* EOBr, r > 0 */
    821               EOBRUN = 1 << r;
    822               CHECK_BIT_BUFFER(br_state, r, return FALSE);
    823               r = GET_BITS(r);
    824               EOBRUN += r;
    825               EOBRUN--;		/* this band is processed at this moment */
    826             }
    827             break;		/* force end-of-band */
    828           }
    829           k += 15;		/* ZRL: skip 15 zeroes in band */
    830         }
    831       }
    832 
    833       BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
    834     }
    835 
    836     /* Completed MCU, so update state */
    837     entropy->saved.EOBRUN = EOBRUN;	/* only part of saved state we need */
    838   }
    839 
    840   /* Account for restart interval (no-op if not using restarts) */
    841   entropy->restarts_to_go--;
    842 
    843   return TRUE;
    844 }
    845 
    846 
    847 /*
    848  * MCU decoding for DC successive approximation refinement scan.
    849  * Note: we assume such scans can be multi-component, although the spec
    850  * is not very clear on the point.
    851  */
    852 
    853 METHODDEF(boolean)
    854 decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
    855 {
    856   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    857   int p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
    858   int blkn;
    859   JBLOCKROW block;
    860   BITREAD_STATE_VARS;
    861 
    862   /* Process restart marker if needed; may have to suspend */
    863   if (cinfo->restart_interval) {
    864     if (entropy->restarts_to_go == 0)
    865       if (! process_restart(cinfo))
    866         return FALSE;
    867   }
    868 
    869   /* Not worth the cycles to check insufficient_data here,
    870    * since we will not change the data anyway if we read zeroes.
    871    */
    872 
    873   /* Load up working state */
    874   BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
    875 
    876   /* Outer loop handles each block in the MCU */
    877 
    878   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
    879     block = MCU_data[blkn];
    880 
    881     /* Encoded data is simply the next bit of the two's-complement DC value */
    882     CHECK_BIT_BUFFER(br_state, 1, return FALSE);
    883     if (GET_BITS(1))
    884       (*block)[0] |= p1;
    885     /* Note: since we use |=, repeating the assignment later is safe */
    886   }
    887 
    888   /* Completed MCU, so update state */
    889   BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
    890 
    891   /* Account for restart interval (no-op if not using restarts) */
    892   entropy->restarts_to_go--;
    893 
    894   return TRUE;
    895 }
    896 
    897 
    898 /*
    899  * MCU decoding for AC successive approximation refinement scan.
    900  */
    901 
    902 METHODDEF(boolean)
    903 decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
    904 {
    905   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
    906   register int s, k, r;
    907   unsigned int EOBRUN;
    908   int Se, p1, m1;
    909   const int * natural_order;
    910   JBLOCKROW block;
    911   JCOEFPTR thiscoef;
    912   BITREAD_STATE_VARS;
    913   d_derived_tbl * tbl;
    914   int num_newnz;
    915   int newnz_pos[DCTSIZE2];
    916 
    917   /* Process restart marker if needed; may have to suspend */
    918   if (cinfo->restart_interval) {
    919     if (entropy->restarts_to_go == 0)
    920       if (! process_restart(cinfo))
    921         return FALSE;
    922   }
    923 
    924   /* If we've run out of data, don't modify the MCU.
    925    */
    926   if (! entropy->insufficient_data) {
    927 
    928     Se = cinfo->Se;
    929     p1 = 1 << cinfo->Al;	/* 1 in the bit position being coded */
    930     m1 = (-1) << cinfo->Al;	/* -1 in the bit position being coded */
    931     natural_order = cinfo->natural_order;
    932 
    933     /* Load up working state */
    934     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
    935     EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */
    936 
    937     /* There is always only one block per MCU */
    938     block = MCU_data[0];
    939     tbl = entropy->ac_derived_tbl;
    940 
    941     /* If we are forced to suspend, we must undo the assignments to any newly
    942      * nonzero coefficients in the block, because otherwise we'd get confused
    943      * next time about which coefficients were already nonzero.
    944      * But we need not undo addition of bits to already-nonzero coefficients;
    945      * instead, we can test the current bit to see if we already did it.
    946      */
    947     num_newnz = 0;
    948 
    949     /* initialize coefficient loop counter to start of band */
    950     k = cinfo->Ss;
    951 
    952     if (EOBRUN == 0) {
    953       do {
    954         HUFF_DECODE(s, br_state, tbl, goto undoit, label3);
    955         r = s >> 4;
    956         s &= 15;
    957         if (s) {
    958           if (s != 1)		/* size of new coef should always be 1 */
    959             WARNMS(cinfo, JWRN_HUFF_BAD_CODE);
    960           CHECK_BIT_BUFFER(br_state, 1, goto undoit);
    961           if (GET_BITS(1))
    962             s = p1;		/* newly nonzero coef is positive */
    963           else
    964             s = m1;		/* newly nonzero coef is negative */
    965         } else {
    966           if (r != 15) {
    967             EOBRUN = 1 << r;	/* EOBr, run length is 2^r + appended bits */
    968             if (r) {
    969               CHECK_BIT_BUFFER(br_state, r, goto undoit);
    970               r = GET_BITS(r);
    971               EOBRUN += r;
    972             }
    973             break;		/* rest of block is handled by EOB logic */
    974           }
    975           /* note s = 0 for processing ZRL */
    976         }
    977         /* Advance over already-nonzero coefs and r still-zero coefs,
    978          * appending correction bits to the nonzeroes.  A correction bit is 1
    979          * if the absolute value of the coefficient must be increased.
    980          */
    981         do {
    982           thiscoef = *block + natural_order[k];
    983           if (*thiscoef) {
    984             CHECK_BIT_BUFFER(br_state, 1, goto undoit);
    985             if (GET_BITS(1)) {
    986               if ((*thiscoef & p1) == 0) { /* do nothing if already set it */
    987                 if (*thiscoef >= 0)
    988                   *thiscoef += p1;
    989                 else
    990                   *thiscoef += m1;
    991               }
    992             }
    993           } else {
    994             if (--r < 0)
    995               break;		/* reached target zero coefficient */
    996           }
    997           k++;
    998         } while (k <= Se);
    999         if (s) {
   1000           int pos = natural_order[k];
   1001           /* Output newly nonzero coefficient */
   1002           (*block)[pos] = (JCOEF) s;
   1003           /* Remember its position in case we have to suspend */
   1004           newnz_pos[num_newnz++] = pos;
   1005         }
   1006         k++;
   1007       } while (k <= Se);
   1008     }
   1009 
   1010     if (EOBRUN) {
   1011       /* Scan any remaining coefficient positions after the end-of-band
   1012        * (the last newly nonzero coefficient, if any).  Append a correction
   1013        * bit to each already-nonzero coefficient.  A correction bit is 1
   1014        * if the absolute value of the coefficient must be increased.
   1015        */
   1016       do {
   1017         thiscoef = *block + natural_order[k];
   1018         if (*thiscoef) {
   1019           CHECK_BIT_BUFFER(br_state, 1, goto undoit);
   1020           if (GET_BITS(1)) {
   1021             if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */
   1022               if (*thiscoef >= 0)
   1023                 *thiscoef += p1;
   1024               else
   1025                 *thiscoef += m1;
   1026             }
   1027           }
   1028         }
   1029         k++;
   1030       } while (k <= Se);
   1031       /* Count one block completed in EOB run */
   1032       EOBRUN--;
   1033     }
   1034 
   1035     /* Completed MCU, so update state */
   1036     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
   1037     entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */
   1038   }
   1039 
   1040   /* Account for restart interval (no-op if not using restarts) */
   1041   entropy->restarts_to_go--;
   1042 
   1043   return TRUE;
   1044 
   1045 undoit:
   1046   /* Re-zero any output coefficients that we made newly nonzero */
   1047   while (num_newnz)
   1048     (*block)[newnz_pos[--num_newnz]] = 0;
   1049 
   1050   return FALSE;
   1051 }
   1052 
   1053 
   1054 /*
   1055  * Decode one MCU's worth of Huffman-compressed coefficients,
   1056  * partial blocks.
   1057  */
   1058 
   1059 METHODDEF(boolean)
   1060 decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1061 {
   1062   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   1063   const int * natural_order;
   1064   int Se, blkn;
   1065   BITREAD_STATE_VARS;
   1066   savable_state state;
   1067 
   1068   /* Process restart marker if needed; may have to suspend */
   1069   if (cinfo->restart_interval) {
   1070     if (entropy->restarts_to_go == 0)
   1071       if (! process_restart(cinfo))
   1072         return FALSE;
   1073   }
   1074 
   1075   /* If we've run out of data, just leave the MCU set to zeroes.
   1076    * This way, we return uniform gray for the remainder of the segment.
   1077    */
   1078   if (! entropy->insufficient_data) {
   1079 
   1080     natural_order = cinfo->natural_order;
   1081     Se = cinfo->lim_Se;
   1082 
   1083     /* Load up working state */
   1084     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
   1085     ASSIGN_STATE(state, entropy->saved);
   1086 
   1087     /* Outer loop handles each block in the MCU */
   1088 
   1089     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   1090       JBLOCKROW block = MCU_data[blkn];
   1091       d_derived_tbl * htbl;
   1092       register int s, k, r;
   1093       int coef_limit, ci;
   1094 
   1095       /* Decode a single block's worth of coefficients */
   1096 
   1097       /* Section F.2.2.1: decode the DC coefficient difference */
   1098       htbl = entropy->dc_cur_tbls[blkn];
   1099       HUFF_DECODE(s, br_state, htbl, return FALSE, label1);
   1100 
   1101       htbl = entropy->ac_cur_tbls[blkn];
   1102       k = 1;
   1103       coef_limit = entropy->coef_limit[blkn];
   1104       if (coef_limit) {
   1105         /* Convert DC difference to actual value, update last_dc_val */
   1106         if (s) {
   1107           CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1108           r = GET_BITS(s);
   1109           s = HUFF_EXTEND(r, s);
   1110         }
   1111         ci = cinfo->MCU_membership[blkn];
   1112         s += state.last_dc_val[ci];
   1113         state.last_dc_val[ci] = s;
   1114         /* Output the DC coefficient */
   1115         (*block)[0] = (JCOEF) s;
   1116 
   1117         /* Section F.2.2.2: decode the AC coefficients */
   1118         /* Since zeroes are skipped, output area must be cleared beforehand */
   1119         for (; k < coef_limit; k++) {
   1120           HUFF_DECODE(s, br_state, htbl, return FALSE, label2);
   1121 
   1122           r = s >> 4;
   1123           s &= 15;
   1124 
   1125           if (s) {
   1126             k += r;
   1127             CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1128             r = GET_BITS(s);
   1129             s = HUFF_EXTEND(r, s);
   1130             /* Output coefficient in natural (dezigzagged) order.
   1131              * Note: the extra entries in natural_order[] will save us
   1132              * if k > Se, which could happen if the data is corrupted.
   1133              */
   1134             (*block)[natural_order[k]] = (JCOEF) s;
   1135           } else {
   1136             if (r != 15)
   1137               goto EndOfBlock;
   1138             k += 15;
   1139           }
   1140         }
   1141       } else {
   1142         if (s) {
   1143           CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1144           DROP_BITS(s);
   1145         }
   1146       }
   1147 
   1148       /* Section F.2.2.2: decode the AC coefficients */
   1149       /* In this path we just discard the values */
   1150       for (; k <= Se; k++) {
   1151         HUFF_DECODE(s, br_state, htbl, return FALSE, label3);
   1152 
   1153         r = s >> 4;
   1154         s &= 15;
   1155 
   1156         if (s) {
   1157           k += r;
   1158           CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1159           DROP_BITS(s);
   1160         } else {
   1161           if (r != 15)
   1162             break;
   1163           k += 15;
   1164         }
   1165       }
   1166 
   1167       EndOfBlock: ;
   1168     }
   1169 
   1170     /* Completed MCU, so update state */
   1171     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
   1172     ASSIGN_STATE(entropy->saved, state);
   1173   }
   1174 
   1175   /* Account for restart interval (no-op if not using restarts) */
   1176   entropy->restarts_to_go--;
   1177 
   1178   return TRUE;
   1179 }
   1180 
   1181 
   1182 /*
   1183  * Decode one MCU's worth of Huffman-compressed coefficients,
   1184  * full-size blocks.
   1185  */
   1186 
   1187 METHODDEF(boolean)
   1188 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
   1189 {
   1190   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   1191   int blkn;
   1192   BITREAD_STATE_VARS;
   1193   savable_state state;
   1194 
   1195   /* Process restart marker if needed; may have to suspend */
   1196   if (cinfo->restart_interval) {
   1197     if (entropy->restarts_to_go == 0)
   1198       if (! process_restart(cinfo))
   1199         return FALSE;
   1200   }
   1201 
   1202   /* If we've run out of data, just leave the MCU set to zeroes.
   1203    * This way, we return uniform gray for the remainder of the segment.
   1204    */
   1205   if (! entropy->insufficient_data) {
   1206 
   1207     /* Load up working state */
   1208     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
   1209     ASSIGN_STATE(state, entropy->saved);
   1210 
   1211     /* Outer loop handles each block in the MCU */
   1212 
   1213     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   1214       JBLOCKROW block = MCU_data[blkn];
   1215       d_derived_tbl * htbl;
   1216       register int s, k, r;
   1217       int coef_limit, ci;
   1218 
   1219       /* Decode a single block's worth of coefficients */
   1220 
   1221       /* Section F.2.2.1: decode the DC coefficient difference */
   1222       htbl = entropy->dc_cur_tbls[blkn];
   1223       HUFF_DECODE(s, br_state, htbl, return FALSE, label1);
   1224 
   1225       htbl = entropy->ac_cur_tbls[blkn];
   1226       k = 1;
   1227       coef_limit = entropy->coef_limit[blkn];
   1228       if (coef_limit) {
   1229         /* Convert DC difference to actual value, update last_dc_val */
   1230         if (s) {
   1231           CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1232           r = GET_BITS(s);
   1233           s = HUFF_EXTEND(r, s);
   1234         }
   1235         ci = cinfo->MCU_membership[blkn];
   1236         s += state.last_dc_val[ci];
   1237         state.last_dc_val[ci] = s;
   1238         /* Output the DC coefficient */
   1239         (*block)[0] = (JCOEF) s;
   1240 
   1241         /* Section F.2.2.2: decode the AC coefficients */
   1242         /* Since zeroes are skipped, output area must be cleared beforehand */
   1243         for (; k < coef_limit; k++) {
   1244           HUFF_DECODE(s, br_state, htbl, return FALSE, label2);
   1245 
   1246           r = s >> 4;
   1247           s &= 15;
   1248 
   1249           if (s) {
   1250             k += r;
   1251             CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1252             r = GET_BITS(s);
   1253             s = HUFF_EXTEND(r, s);
   1254             /* Output coefficient in natural (dezigzagged) order.
   1255              * Note: the extra entries in jpeg_natural_order[] will save us
   1256              * if k >= DCTSIZE2, which could happen if the data is corrupted.
   1257              */
   1258             (*block)[jpeg_natural_order[k]] = (JCOEF) s;
   1259           } else {
   1260             if (r != 15)
   1261               goto EndOfBlock;
   1262             k += 15;
   1263           }
   1264         }
   1265       } else {
   1266         if (s) {
   1267           CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1268           DROP_BITS(s);
   1269         }
   1270       }
   1271 
   1272       /* Section F.2.2.2: decode the AC coefficients */
   1273       /* In this path we just discard the values */
   1274       for (; k < DCTSIZE2; k++) {
   1275         HUFF_DECODE(s, br_state, htbl, return FALSE, label3);
   1276 
   1277         r = s >> 4;
   1278         s &= 15;
   1279 
   1280         if (s) {
   1281           k += r;
   1282           CHECK_BIT_BUFFER(br_state, s, return FALSE);
   1283           DROP_BITS(s);
   1284         } else {
   1285           if (r != 15)
   1286             break;
   1287           k += 15;
   1288         }
   1289       }
   1290 
   1291       EndOfBlock: ;
   1292     }
   1293 
   1294     /* Completed MCU, so update state */
   1295     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
   1296     ASSIGN_STATE(entropy->saved, state);
   1297   }
   1298 
   1299   /* Account for restart interval (no-op if not using restarts) */
   1300   entropy->restarts_to_go--;
   1301 
   1302   return TRUE;
   1303 }
   1304 
   1305 
   1306 /*
   1307  * Initialize for a Huffman-compressed scan.
   1308  */
   1309 
   1310 METHODDEF(void)
   1311 start_pass_huff_decoder (j_decompress_ptr cinfo)
   1312 {
   1313   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
   1314   int ci, blkn, tbl, i;
   1315   jpeg_component_info * compptr;
   1316 
   1317   if (cinfo->progressive_mode) {
   1318     /* Validate progressive scan parameters */
   1319     if (cinfo->Ss == 0) {
   1320       if (cinfo->Se != 0)
   1321         goto bad;
   1322     } else {
   1323       /* need not check Ss/Se < 0 since they came from unsigned bytes */
   1324       if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)
   1325         goto bad;
   1326       /* AC scans may have only one component */
   1327       if (cinfo->comps_in_scan != 1)
   1328         goto bad;
   1329     }
   1330     if (cinfo->Ah != 0) {
   1331       /* Successive approximation refinement scan: must have Al = Ah-1. */
   1332       if (cinfo->Ah-1 != cinfo->Al)
   1333         goto bad;
   1334     }
   1335     if (cinfo->Al > 13) {	/* need not check for < 0 */
   1336       /* Arguably the maximum Al value should be less than 13 for 8-bit precision,
   1337        * but the spec doesn't say so, and we try to be liberal about what we
   1338        * accept.  Note: large Al values could result in out-of-range DC
   1339        * coefficients during early scans, leading to bizarre displays due to
   1340        * overflows in the IDCT math.  But we won't crash.
   1341        */
   1342       bad:
   1343       ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
   1344                cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
   1345     }
   1346     /* Update progression status, and verify that scan order is legal.
   1347      * Note that inter-scan inconsistencies are treated as warnings
   1348      * not fatal errors ... not clear if this is right way to behave.
   1349      */
   1350     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1351       int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
   1352       int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
   1353       if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
   1354         WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
   1355       for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
   1356         int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
   1357         if (cinfo->Ah != expected)
   1358           WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
   1359         coef_bit_ptr[coefi] = cinfo->Al;
   1360       }
   1361     }
   1362 
   1363     /* Select MCU decoding routine */
   1364     if (cinfo->Ah == 0) {
   1365       if (cinfo->Ss == 0)
   1366         entropy->pub.decode_mcu = decode_mcu_DC_first;
   1367       else
   1368         entropy->pub.decode_mcu = decode_mcu_AC_first;
   1369     } else {
   1370       if (cinfo->Ss == 0)
   1371         entropy->pub.decode_mcu = decode_mcu_DC_refine;
   1372       else
   1373         entropy->pub.decode_mcu = decode_mcu_AC_refine;
   1374     }
   1375 
   1376     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1377       compptr = cinfo->cur_comp_info[ci];
   1378       /* Make sure requested tables are present, and compute derived tables.
   1379        * We may build same derived table more than once, but it's not expensive.
   1380        */
   1381       if (cinfo->Ss == 0) {
   1382         if (cinfo->Ah == 0) {	/* DC refinement needs no table */
   1383           tbl = compptr->dc_tbl_no;
   1384           jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
   1385                                   & entropy->derived_tbls[tbl]);
   1386         }
   1387       } else {
   1388         tbl = compptr->ac_tbl_no;
   1389         jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
   1390                                 & entropy->derived_tbls[tbl]);
   1391         /* remember the single active table */
   1392         entropy->ac_derived_tbl = entropy->derived_tbls[tbl];
   1393       }
   1394       /* Initialize DC predictions to 0 */
   1395       entropy->saved.last_dc_val[ci] = 0;
   1396     }
   1397 
   1398     /* Initialize private state variables */
   1399     entropy->saved.EOBRUN = 0;
   1400   } else {
   1401     /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
   1402      * This ought to be an error condition, but we make it a warning because
   1403      * there are some baseline files out there with all zeroes in these bytes.
   1404      */
   1405     if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||
   1406         ((cinfo->is_baseline || cinfo->Se < DCTSIZE2) &&
   1407         cinfo->Se != cinfo->lim_Se))
   1408       WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
   1409 
   1410     /* Select MCU decoding routine */
   1411     /* We retain the hard-coded case for full-size blocks.
   1412      * This is not necessary, but it appears that this version is slightly
   1413      * more performant in the given implementation.
   1414      * With an improved implementation we would prefer a single optimized
   1415      * function.
   1416      */
   1417     if (cinfo->lim_Se != DCTSIZE2-1)
   1418       entropy->pub.decode_mcu = decode_mcu_sub;
   1419     else
   1420       entropy->pub.decode_mcu = decode_mcu;
   1421 
   1422     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
   1423       compptr = cinfo->cur_comp_info[ci];
   1424       /* Compute derived values for Huffman tables */
   1425       /* We may do this more than once for a table, but it's not expensive */
   1426       tbl = compptr->dc_tbl_no;
   1427       jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
   1428                               & entropy->dc_derived_tbls[tbl]);
   1429       if (cinfo->lim_Se) {	/* AC needs no table when not present */
   1430         tbl = compptr->ac_tbl_no;
   1431         jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
   1432                                 & entropy->ac_derived_tbls[tbl]);
   1433       }
   1434       /* Initialize DC predictions to 0 */
   1435       entropy->saved.last_dc_val[ci] = 0;
   1436     }
   1437 
   1438     /* Precalculate decoding info for each block in an MCU of this scan */
   1439     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
   1440       ci = cinfo->MCU_membership[blkn];
   1441       compptr = cinfo->cur_comp_info[ci];
   1442       /* Precalculate which table to use for each block */
   1443       entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
   1444       entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
   1445       /* Decide whether we really care about the coefficient values */
   1446       if (compptr->component_needed) {
   1447         ci = compptr->DCT_v_scaled_size;
   1448         i = compptr->DCT_h_scaled_size;
   1449         switch (cinfo->lim_Se) {
   1450         case (1*1-1):
   1451           entropy->coef_limit[blkn] = 1;
   1452           break;
   1453         case (2*2-1):
   1454           if (ci <= 0 || ci > 2) ci = 2;
   1455           if (i <= 0 || i > 2) i = 2;
   1456           entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order2[ci - 1][i - 1];
   1457           break;
   1458         case (3*3-1):
   1459           if (ci <= 0 || ci > 3) ci = 3;
   1460           if (i <= 0 || i > 3) i = 3;
   1461           entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order3[ci - 1][i - 1];
   1462           break;
   1463         case (4*4-1):
   1464           if (ci <= 0 || ci > 4) ci = 4;
   1465           if (i <= 0 || i > 4) i = 4;
   1466           entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order4[ci - 1][i - 1];
   1467           break;
   1468         case (5*5-1):
   1469           if (ci <= 0 || ci > 5) ci = 5;
   1470           if (i <= 0 || i > 5) i = 5;
   1471           entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order5[ci - 1][i - 1];
   1472           break;
   1473         case (6*6-1):
   1474           if (ci <= 0 || ci > 6) ci = 6;
   1475           if (i <= 0 || i > 6) i = 6;
   1476           entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order6[ci - 1][i - 1];
   1477           break;
   1478         case (7*7-1):
   1479           if (ci <= 0 || ci > 7) ci = 7;
   1480           if (i <= 0 || i > 7) i = 7;
   1481           entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order7[ci - 1][i - 1];
   1482           break;
   1483         default:
   1484           if (ci <= 0 || ci > 8) ci = 8;
   1485           if (i <= 0 || i > 8) i = 8;
   1486           entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order[ci - 1][i - 1];
   1487           break;
   1488         }
   1489       } else {
   1490         entropy->coef_limit[blkn] = 0;
   1491       }
   1492     }
   1493   }
   1494 
   1495   /* Initialize bitread state variables */
   1496   entropy->bitstate.bits_left = 0;
   1497   entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
   1498   entropy->insufficient_data = FALSE;
   1499 
   1500   /* Initialize restart counter */
   1501   entropy->restarts_to_go = cinfo->restart_interval;
   1502 }
   1503 
   1504 
   1505 /*
   1506  * Module initialization routine for Huffman entropy decoding.
   1507  */
   1508 
   1509 GLOBAL(void)
   1510 jinit_huff_decoder (j_decompress_ptr cinfo)
   1511 {
   1512   huff_entropy_ptr entropy;
   1513   int i;
   1514 
   1515   entropy = (huff_entropy_ptr)
   1516     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1517                                 SIZEOF(huff_entropy_decoder));
   1518   cinfo->entropy = &entropy->pub;
   1519   entropy->pub.start_pass = start_pass_huff_decoder;
   1520 
   1521   if (cinfo->progressive_mode) {
   1522     /* Create progression status table */
   1523     int *coef_bit_ptr, ci;
   1524     cinfo->coef_bits = (int (*)[DCTSIZE2])
   1525       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1526                                   cinfo->num_components*DCTSIZE2*SIZEOF(int));
   1527     coef_bit_ptr = & cinfo->coef_bits[0][0];
   1528     for (ci = 0; ci < cinfo->num_components; ci++)
   1529       for (i = 0; i < DCTSIZE2; i++)
   1530         *coef_bit_ptr++ = -1;
   1531 
   1532     /* Mark derived tables unallocated */
   1533     for (i = 0; i < NUM_HUFF_TBLS; i++) {
   1534       entropy->derived_tbls[i] = NULL;
   1535     }
   1536   } else {
   1537     /* Mark tables unallocated */
   1538     for (i = 0; i < NUM_HUFF_TBLS; i++) {
   1539       entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
   1540     }
   1541   }
   1542 }
   1543