/src/mupdf/thirdparty/libjpeg/jdhuff.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * jdhuff.c  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 1991-1997, Thomas G. Lane.  | 
5  |  |  * Modified 2006-2020 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  | 14.9M  | #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  | 9.35M  | #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  | 12.7M  |   register bit_buf_type get_buffer;  \  | 
101  | 12.7M  |   register int bits_left;  \  | 
102  | 12.7M  |   bitread_working_state br_state  | 
103  |  |  | 
104  |  | #define BITREAD_LOAD_STATE(cinfop,permstate)  \  | 
105  | 1.50M  |   br_state.cinfo = cinfop; \  | 
106  | 1.50M  |   br_state.next_input_byte = cinfop->src->next_input_byte; \  | 
107  | 1.50M  |   br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \  | 
108  | 1.50M  |   get_buffer = permstate.get_buffer; \  | 
109  | 1.50M  |   bits_left = permstate.bits_left;  | 
110  |  |  | 
111  |  | #define BITREAD_SAVE_STATE(cinfop,permstate)  \  | 
112  | 1.50M  |   cinfop->src->next_input_byte = br_state.next_input_byte; \  | 
113  | 1.50M  |   cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \  | 
114  | 1.50M  |   permstate.get_buffer = get_buffer; \  | 
115  | 1.50M  |   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  | 9.48M  |   { if (bits_left < (nbits)) {  \ | 
137  | 340k  |       if (! jpeg_fill_bit_buffer(&(state),get_buffer,bits_left,nbits))  \  | 
138  | 340k  |         { action; }  \ | 
139  | 340k  |       get_buffer = (state).get_buffer; bits_left = (state).bits_left; } }  | 
140  |  |  | 
141  |  | #define GET_BITS(nbits) \  | 
142  | 7.77M  |   (((int) (get_buffer >> (bits_left -= (nbits)))) & BIT_MASK(nbits))  | 
143  |  |  | 
144  |  | #define PEEK_BITS(nbits) \  | 
145  | 12.2M  |   (((int) (get_buffer >> (bits_left -  (nbits)))) & BIT_MASK(nbits))  | 
146  |  |  | 
147  |  | #define DROP_BITS(nbits) \  | 
148  | 13.6M  |   (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  | 12.2M  | #define HUFF_DECODE(result,state,htbl,failaction,slowlabel) \  | 
169  | 12.2M  | { register int nb, look; \ | 
170  | 12.2M  |   if (bits_left < HUFF_LOOKAHEAD) { \ | 
171  | 2.10M  |     if (! jpeg_fill_bit_buffer(&state,get_buffer,bits_left, 0)) {failaction;} \ | 
172  | 2.10M  |     get_buffer = state.get_buffer; bits_left = state.bits_left; \  | 
173  | 2.10M  |     if (bits_left < HUFF_LOOKAHEAD) { \ | 
174  | 44.4k  |       nb = 1; goto slowlabel; \  | 
175  | 44.4k  |     } \  | 
176  | 2.10M  |   } \  | 
177  | 12.2M  |   look = PEEK_BITS(HUFF_LOOKAHEAD); \  | 
178  | 12.2M  |   if ((nb = htbl->look_nbits[look]) != 0) { \ | 
179  | 11.9M  |     DROP_BITS(nb); \  | 
180  | 11.9M  |     result = htbl->look_sym[look]; \  | 
181  | 11.9M  |   } else { \ | 
182  | 360k  |     nb = HUFF_LOOKAHEAD+1; \  | 
183  | 360k  | slowlabel: \  | 
184  | 360k  |     if ((result=jpeg_huff_decode(&state,get_buffer,bits_left,htbl,nb)) < 0) \  | 
185  | 360k  |   { failaction; } \ | 
186  | 360k  |     get_buffer = state.get_buffer; bits_left = state.bits_left; \  | 
187  | 360k  |   } \  | 
188  | 12.2M  | }  | 
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  | 1.46M  | #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  | 7.12k  | { | 
326  | 7.12k  |   JHUFF_TBL *htbl;  | 
327  | 7.12k  |   d_derived_tbl *dtbl;  | 
328  | 7.12k  |   int p, i, l, si, numsymbols;  | 
329  | 7.12k  |   int lookbits, ctr;  | 
330  | 7.12k  |   char huffsize[257];  | 
331  | 7.12k  |   unsigned int huffcode[257];  | 
332  | 7.12k  |   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  | 7.12k  |   if (tblno < 0 || tblno >= NUM_HUFF_TBLS)  | 
340  | 8  |     ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);  | 
341  | 7.12k  |   htbl =  | 
342  | 7.12k  |     isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];  | 
343  | 7.12k  |   if (htbl == NULL)  | 
344  | 438  |     htbl = jpeg_std_huff_table((j_common_ptr) cinfo, isDC, tblno);  | 
345  |  |  | 
346  |  |   /* Allocate a workspace if we haven't already done so. */  | 
347  | 7.12k  |   if (*pdtbl == NULL)  | 
348  | 1.84k  |     *pdtbl = (d_derived_tbl *) (*cinfo->mem->alloc_small)  | 
349  | 1.84k  |       ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(d_derived_tbl));  | 
350  | 7.12k  |   dtbl = *pdtbl;  | 
351  | 7.12k  |   dtbl->pub = htbl;   /* fill in back link */  | 
352  |  |     | 
353  |  |   /* Figure C.1: make table of Huffman code length for each symbol */  | 
354  |  |  | 
355  | 7.12k  |   p = 0;  | 
356  | 120k  |   for (l = 1; l <= 16; l++) { | 
357  | 113k  |     i = (int) htbl->bits[l];  | 
358  | 113k  |     if (i < 0 || p + i > 256)  /* protect against table overrun */  | 
359  | 0  |       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);  | 
360  | 380k  |     while (i--)  | 
361  | 266k  |       huffsize[p++] = (char) l;  | 
362  | 113k  |   }  | 
363  | 7.12k  |   huffsize[p] = 0;  | 
364  | 7.12k  |   numsymbols = p;  | 
365  |  |     | 
366  |  |   /* Figure C.2: generate the codes themselves */  | 
367  |  |   /* We also validate that the counts represent a legal Huffman code tree. */  | 
368  |  |     | 
369  | 7.12k  |   code = 0;  | 
370  | 7.12k  |   si = huffsize[0];  | 
371  | 7.12k  |   p = 0;  | 
372  | 67.5k  |   while (huffsize[p]) { | 
373  | 327k  |     while (((int) huffsize[p]) == si) { | 
374  | 266k  |       huffcode[p++] = code;  | 
375  | 266k  |       code++;  | 
376  | 266k  |     }  | 
377  |  |     /* code is now 1 more than the last code used for codelength si; but  | 
378  |  |      * it must still fit in si bits, since no code is allowed to be all ones.  | 
379  |  |      */  | 
380  | 60.4k  |     if (((INT32) code) >= (((INT32) 1) << si))  | 
381  | 1  |       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);  | 
382  | 60.4k  |     code <<= 1;  | 
383  | 60.4k  |     si++;  | 
384  | 60.4k  |   }  | 
385  |  |  | 
386  |  |   /* Figure F.15: generate decoding tables for bit-sequential decoding */  | 
387  |  |  | 
388  | 7.12k  |   p = 0;  | 
389  | 120k  |   for (l = 1; l <= 16; l++) { | 
390  | 113k  |     if (htbl->bits[l]) { | 
391  |  |       /* valoffset[l] = huffval[] index of 1st symbol of code length l,  | 
392  |  |        * minus the minimum code of length l  | 
393  |  |        */  | 
394  | 49.0k  |       dtbl->valoffset[l] = (INT32) p - (INT32) huffcode[p];  | 
395  | 49.0k  |       p += htbl->bits[l];  | 
396  | 49.0k  |       dtbl->maxcode[l] = huffcode[p-1]; /* maximum code of length l */  | 
397  | 64.7k  |     } else { | 
398  | 64.7k  |       dtbl->maxcode[l] = -1;  /* -1 if no codes of this length */  | 
399  | 64.7k  |     }  | 
400  | 113k  |   }  | 
401  | 7.12k  |   dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */  | 
402  |  |  | 
403  |  |   /* Compute lookahead tables to speed up decoding.  | 
404  |  |    * First we set all the table entries to 0, indicating "too long";  | 
405  |  |    * then we iterate through the Huffman codes that are short enough and  | 
406  |  |    * fill in all the entries that correspond to bit sequences starting  | 
407  |  |    * with that code.  | 
408  |  |    */  | 
409  |  |  | 
410  | 7.12k  |   MEMZERO(dtbl->look_nbits, SIZEOF(dtbl->look_nbits));  | 
411  |  |  | 
412  | 7.12k  |   p = 0;  | 
413  | 64.0k  |   for (l = 1; l <= HUFF_LOOKAHEAD; l++) { | 
414  | 142k  |     for (i = 1; i <= (int) htbl->bits[l]; i++, p++) { | 
415  |  |       /* l = current code's length, p = its index in huffcode[] & huffval[]. */  | 
416  |  |       /* Generate left-justified code followed by all possible bit sequences */  | 
417  | 85.2k  |       lookbits = huffcode[p] << (HUFF_LOOKAHEAD-l);  | 
418  | 1.67M  |       for (ctr = 1 << (HUFF_LOOKAHEAD-l); ctr > 0; ctr--) { | 
419  | 1.58M  |   dtbl->look_nbits[lookbits] = l;  | 
420  | 1.58M  |   dtbl->look_sym[lookbits] = htbl->huffval[p];  | 
421  | 1.58M  |   lookbits++;  | 
422  | 1.58M  |       }  | 
423  | 85.2k  |     }  | 
424  | 56.8k  |   }  | 
425  |  |  | 
426  |  |   /* Validate symbols as being reasonable.  | 
427  |  |    * For AC tables, we make no check, but accept all byte values 0..255.  | 
428  |  |    * For DC tables, we require the symbols to be in range 0..15.  | 
429  |  |    * (Tighter bounds could be applied depending on the data depth and mode,  | 
430  |  |    * but this is sufficient to ensure safe decoding.)  | 
431  |  |    */  | 
432  | 7.12k  |   if (isDC) { | 
433  | 24.1k  |     for (i = 0; i < numsymbols; i++) { | 
434  | 21.0k  |       int sym = htbl->huffval[i];  | 
435  | 21.0k  |       if (sym < 0 || sym > 15)  | 
436  | 16  |   ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);  | 
437  | 21.0k  |     }  | 
438  | 3.06k  |   }  | 
439  | 7.12k  | }  | 
440  |  |  | 
441  |  |  | 
442  |  | /*  | 
443  |  |  * Out-of-line code for bit fetching.  | 
444  |  |  * Note: current values of get_buffer and bits_left are passed as parameters,  | 
445  |  |  * but are returned in the corresponding fields of the state struct.  | 
446  |  |  *  | 
447  |  |  * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width  | 
448  |  |  * of get_buffer to be used.  (On machines with wider words, an even larger  | 
449  |  |  * buffer could be used.)  However, on some machines 32-bit shifts are  | 
450  |  |  * quite slow and take time proportional to the number of places shifted.  | 
451  |  |  * (This is true with most PC compilers, for instance.)  In this case it may  | 
452  |  |  * be a win to set MIN_GET_BITS to the minimum value of 15.  This reduces the  | 
453  |  |  * average shift distance at the cost of more calls to jpeg_fill_bit_buffer.  | 
454  |  |  */  | 
455  |  |  | 
456  |  | #ifdef SLOW_SHIFT_32  | 
457  |  | #define MIN_GET_BITS  15  /* minimum allowable value */  | 
458  |  | #else  | 
459  | 9.35M  | #define MIN_GET_BITS  (BIT_BUF_SIZE-7)  | 
460  |  | #endif  | 
461  |  |  | 
462  |  |  | 
463  |  | LOCAL(boolean)  | 
464  |  | jpeg_fill_bit_buffer (bitread_working_state * state,  | 
465  |  |           register bit_buf_type get_buffer, register int bits_left,  | 
466  |  |           int nbits)  | 
467  |  | /* Load up the bit buffer to a depth of at least nbits */  | 
468  | 2.44M  | { | 
469  |  |   /* Copy heavily used state fields into locals (hopefully registers) */  | 
470  | 2.44M  |   register const JOCTET * next_input_byte = state->next_input_byte;  | 
471  | 2.44M  |   register size_t bytes_in_buffer = state->bytes_in_buffer;  | 
472  | 2.44M  |   j_decompress_ptr cinfo = state->cinfo;  | 
473  |  |  | 
474  |  |   /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */  | 
475  |  |   /* (It is assumed that no request will be for more than that many bits.) */  | 
476  |  |   /* We fail to do so only if we hit a marker or are forced to suspend. */  | 
477  |  |  | 
478  | 2.44M  |   if (cinfo->unread_marker == 0) { /* cannot advance past a marker */ | 
479  | 8.99M  |     while (bits_left < MIN_GET_BITS) { | 
480  | 6.78M  |       register int c;  | 
481  |  |  | 
482  |  |       /* Attempt to read a byte */  | 
483  | 6.78M  |       if (bytes_in_buffer == 0) { | 
484  | 24.4k  |   if (! (*cinfo->src->fill_input_buffer) (cinfo))  | 
485  | 0  |     return FALSE;  | 
486  | 24.4k  |   next_input_byte = cinfo->src->next_input_byte;  | 
487  | 24.4k  |   bytes_in_buffer = cinfo->src->bytes_in_buffer;  | 
488  | 24.4k  |       }  | 
489  | 6.78M  |       bytes_in_buffer--;  | 
490  | 6.78M  |       c = GETJOCTET(*next_input_byte++);  | 
491  |  |  | 
492  |  |       /* If it's 0xFF, check and discard stuffed zero byte */  | 
493  | 6.78M  |       if (c == 0xFF) { | 
494  |  |   /* Loop here to discard any padding FF's on terminating marker,  | 
495  |  |    * so that we can save a valid unread_marker value.  NOTE: we will  | 
496  |  |    * accept multiple FF's followed by a 0 as meaning a single FF data  | 
497  |  |    * byte.  This data pattern is not valid according to the standard.  | 
498  |  |    */  | 
499  | 57.9k  |   do { | 
500  | 57.9k  |     if (bytes_in_buffer == 0) { | 
501  | 1.47k  |       if (! (*cinfo->src->fill_input_buffer) (cinfo))  | 
502  | 0  |         return FALSE;  | 
503  | 1.47k  |       next_input_byte = cinfo->src->next_input_byte;  | 
504  | 1.47k  |       bytes_in_buffer = cinfo->src->bytes_in_buffer;  | 
505  | 1.47k  |     }  | 
506  | 57.9k  |     bytes_in_buffer--;  | 
507  | 57.9k  |     c = GETJOCTET(*next_input_byte++);  | 
508  | 57.9k  |   } while (c == 0xFF);  | 
509  |  |  | 
510  | 56.5k  |   if (c == 0) { | 
511  |  |     /* Found FF/00, which represents an FF data byte */  | 
512  | 38.9k  |     c = 0xFF;  | 
513  | 38.9k  |   } else { | 
514  |  |     /* Oops, it's actually a marker indicating end of compressed data.  | 
515  |  |      * Save the marker code for later use.  | 
516  |  |      * Fine point: it might appear that we should save the marker into  | 
517  |  |      * bitread working state, not straight into permanent state.  But  | 
518  |  |      * once we have hit a marker, we cannot need to suspend within the  | 
519  |  |      * current MCU, because we will read no more bytes from the data  | 
520  |  |      * source.  So it is OK to update permanent state right away.  | 
521  |  |      */  | 
522  | 17.6k  |     cinfo->unread_marker = c;  | 
523  |  |     /* See if we need to insert some fake zero bits. */  | 
524  | 17.6k  |     goto no_more_bytes;  | 
525  | 17.6k  |   }  | 
526  | 56.5k  |       }  | 
527  |  |  | 
528  |  |       /* OK, load c into get_buffer */  | 
529  | 6.76M  |       get_buffer = (get_buffer << 8) | c;  | 
530  | 6.76M  |       bits_left += 8;  | 
531  | 6.76M  |     } /* end while */  | 
532  | 2.22M  |   } else { | 
533  | 237k  |   no_more_bytes:  | 
534  |  |     /* We get here if we've read the marker that terminates the compressed  | 
535  |  |      * data segment.  There should be enough bits in the buffer register  | 
536  |  |      * to satisfy the request; if so, no problem.  | 
537  |  |      */  | 
538  | 237k  |     if (nbits > bits_left) { | 
539  |  |       /* Uh-oh.  Report corrupted data to user and stuff zeroes into  | 
540  |  |        * the data stream, so that we can produce some kind of image.  | 
541  |  |        * We use a nonvolatile flag to ensure that only one warning message  | 
542  |  |        * appears per data segment.  | 
543  |  |        */  | 
544  | 179k  |       if (! ((huff_entropy_ptr) cinfo->entropy)->insufficient_data) { | 
545  | 13.4k  |   WARNMS(cinfo, JWRN_HIT_MARKER);  | 
546  | 13.4k  |   ((huff_entropy_ptr) cinfo->entropy)->insufficient_data = TRUE;  | 
547  | 13.4k  |       }  | 
548  |  |       /* Fill the buffer with zero bits */  | 
549  | 179k  |       get_buffer <<= MIN_GET_BITS - bits_left;  | 
550  | 179k  |       bits_left = MIN_GET_BITS;  | 
551  | 179k  |     }  | 
552  | 237k  |   }  | 
553  |  |  | 
554  |  |   /* Unload the local registers */  | 
555  | 2.44M  |   state->next_input_byte = next_input_byte;  | 
556  | 2.44M  |   state->bytes_in_buffer = bytes_in_buffer;  | 
557  | 2.44M  |   state->get_buffer = get_buffer;  | 
558  | 2.44M  |   state->bits_left = bits_left;  | 
559  |  |  | 
560  | 2.44M  |   return TRUE;  | 
561  | 2.44M  | }  | 
562  |  |  | 
563  |  |  | 
564  |  | /*  | 
565  |  |  * Figure F.12: extend sign bit.  | 
566  |  |  * On some machines, a shift and sub will be faster than a table lookup.  | 
567  |  |  */  | 
568  |  |  | 
569  |  | #ifdef AVOID_TABLES  | 
570  |  |  | 
571  |  | #define BIT_MASK(nbits)   ((1<<(nbits))-1)  | 
572  |  | #define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) - ((1<<(s))-1) : (x))  | 
573  |  |  | 
574  |  | #else  | 
575  |  |  | 
576  | 19.9M  | #define BIT_MASK(nbits)   bmask[nbits]  | 
577  | 4.81M  | #define HUFF_EXTEND(x,s)  ((x) <= bmask[(s) - 1] ? (x) - bmask[s] : (x))  | 
578  |  |  | 
579  |  | static const int bmask[16] =  /* bmask[n] is mask for n rightmost bits */  | 
580  |  |   { 0, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, | 
581  |  |     0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF };  | 
582  |  |  | 
583  |  | #endif /* AVOID_TABLES */  | 
584  |  |  | 
585  |  |  | 
586  |  | /*  | 
587  |  |  * Out-of-line code for Huffman code decoding.  | 
588  |  |  */  | 
589  |  |  | 
590  |  | LOCAL(int)  | 
591  |  | jpeg_huff_decode (bitread_working_state * state,  | 
592  |  |       register bit_buf_type get_buffer, register int bits_left,  | 
593  |  |       d_derived_tbl * htbl, int min_bits)  | 
594  | 360k  | { | 
595  | 360k  |   register int l = min_bits;  | 
596  | 360k  |   register INT32 code;  | 
597  |  |  | 
598  |  |   /* HUFF_DECODE has determined that the code is at least min_bits */  | 
599  |  |   /* bits long, so fetch that many bits in one swoop. */  | 
600  |  |  | 
601  | 360k  |   CHECK_BIT_BUFFER(*state, l, return -1);  | 
602  | 360k  |   code = GET_BITS(l);  | 
603  |  |  | 
604  |  |   /* Collect the rest of the Huffman code one bit at a time. */  | 
605  |  |   /* This is per Figure F.16 in the JPEG spec. */  | 
606  |  |  | 
607  | 1.15M  |   while (code > htbl->maxcode[l]) { | 
608  | 797k  |     code <<= 1;  | 
609  | 797k  |     CHECK_BIT_BUFFER(*state, 1, return -1);  | 
610  | 797k  |     code |= GET_BITS(1);  | 
611  | 797k  |     l++;  | 
612  | 797k  |   }  | 
613  |  |  | 
614  |  |   /* Unload the local registers */  | 
615  | 360k  |   state->get_buffer = get_buffer;  | 
616  | 360k  |   state->bits_left = bits_left;  | 
617  |  |  | 
618  |  |   /* With garbage input we may reach the sentinel value l = 17. */  | 
619  |  |  | 
620  | 360k  |   if (l > 16) { | 
621  | 5.09k  |     WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE);  | 
622  | 5.09k  |     return 0;     /* fake a zero as the safest result */  | 
623  | 5.09k  |   }  | 
624  |  |  | 
625  | 354k  |   return htbl->pub->huffval[ (int) (code + htbl->valoffset[l]) ];  | 
626  | 360k  | }  | 
627  |  |  | 
628  |  |  | 
629  |  | /*  | 
630  |  |  * Finish up at the end of a Huffman-compressed scan.  | 
631  |  |  */  | 
632  |  |  | 
633  |  | METHODDEF(void)  | 
634  |  | finish_pass_huff (j_decompress_ptr cinfo)  | 
635  | 339k  | { | 
636  | 339k  |   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;  | 
637  |  |  | 
638  |  |   /* Throw away any unused bits remaining in bit buffer; */  | 
639  |  |   /* include any full bytes in next_marker's count of discarded bytes */  | 
640  | 339k  |   cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;  | 
641  | 339k  |   entropy->bitstate.bits_left = 0;  | 
642  | 339k  | }  | 
643  |  |  | 
644  |  |  | 
645  |  | /*  | 
646  |  |  * Check for a restart marker & resynchronize decoder.  | 
647  |  |  * Returns FALSE if must suspend.  | 
648  |  |  */  | 
649  |  |  | 
650  |  | LOCAL(boolean)  | 
651  |  | process_restart (j_decompress_ptr cinfo)  | 
652  | 335k  | { | 
653  | 335k  |   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;  | 
654  | 335k  |   int ci;  | 
655  |  |  | 
656  | 335k  |   finish_pass_huff(cinfo);  | 
657  |  |  | 
658  |  |   /* Advance past the RSTn marker */  | 
659  | 335k  |   if (! (*cinfo->marker->read_restart_marker) (cinfo))  | 
660  | 0  |     return FALSE;  | 
661  |  |  | 
662  |  |   /* Re-initialize DC predictions to 0 */  | 
663  | 978k  |   for (ci = 0; ci < cinfo->comps_in_scan; ci++)  | 
664  | 643k  |     entropy->saved.last_dc_val[ci] = 0;  | 
665  |  |   /* Re-init EOB run count, too */  | 
666  | 335k  |   entropy->saved.EOBRUN = 0;  | 
667  |  |  | 
668  |  |   /* Reset restart counter */  | 
669  | 335k  |   entropy->restarts_to_go = cinfo->restart_interval;  | 
670  |  |  | 
671  |  |   /* Reset out-of-data flag, unless read_restart_marker left us smack up  | 
672  |  |    * against a marker.  In that case we will end up treating the next data  | 
673  |  |    * segment as empty, and we can avoid producing bogus output pixels by  | 
674  |  |    * leaving the flag set.  | 
675  |  |    */  | 
676  | 335k  |   if (cinfo->unread_marker == 0)  | 
677  | 18.0k  |     entropy->insufficient_data = FALSE;  | 
678  |  |  | 
679  | 335k  |   return TRUE;  | 
680  | 335k  | }  | 
681  |  |  | 
682  |  |  | 
683  |  | /*  | 
684  |  |  * Huffman MCU decoding.  | 
685  |  |  * Each of these routines decodes and returns one MCU's worth of  | 
686  |  |  * Huffman-compressed coefficients.   | 
687  |  |  * The coefficients are reordered from zigzag order into natural array order,  | 
688  |  |  * but are not dequantized.  | 
689  |  |  *  | 
690  |  |  * The i'th block of the MCU is stored into the block pointed to by  | 
691  |  |  * MCU_data[i].  WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.  | 
692  |  |  * (Wholesale zeroing is usually a little faster than retail...)  | 
693  |  |  *  | 
694  |  |  * We return FALSE if data source requested suspension.  In that case no  | 
695  |  |  * changes have been made to permanent state.  (Exception: some output  | 
696  |  |  * coefficients may already have been assigned.  This is harmless for  | 
697  |  |  * spectral selection, since we'll just re-assign them on the next call.  | 
698  |  |  * Successive approximation AC refinement has to be more careful, however.)  | 
699  |  |  */  | 
700  |  |  | 
701  |  | /*  | 
702  |  |  * MCU decoding for DC initial scan (either spectral selection,  | 
703  |  |  * or first pass of successive approximation).  | 
704  |  |  */  | 
705  |  |  | 
706  |  | METHODDEF(boolean)  | 
707  |  | decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)  | 
708  | 1.30M  | { | 
709  | 1.30M  |   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;  | 
710  | 1.30M  |   int Al = cinfo->Al;  | 
711  | 1.30M  |   register int s, r;  | 
712  | 1.30M  |   int blkn, ci;  | 
713  | 1.30M  |   JBLOCKROW block;  | 
714  | 1.30M  |   BITREAD_STATE_VARS;  | 
715  | 1.30M  |   savable_state state;  | 
716  | 1.30M  |   d_derived_tbl * tbl;  | 
717  | 1.30M  |   jpeg_component_info * compptr;  | 
718  |  |  | 
719  |  |   /* Process restart marker if needed; may have to suspend */  | 
720  | 1.30M  |   if (cinfo->restart_interval) { | 
721  | 10.7k  |     if (entropy->restarts_to_go == 0)  | 
722  | 1.50k  |       if (! process_restart(cinfo))  | 
723  | 0  |   return FALSE;  | 
724  | 10.7k  |   }  | 
725  |  |  | 
726  |  |   /* If we've run out of data, just leave the MCU set to zeroes.  | 
727  |  |    * This way, we return uniform gray for the remainder of the segment.  | 
728  |  |    */  | 
729  | 1.30M  |   if (! entropy->insufficient_data) { | 
730  |  |  | 
731  |  |     /* Load up working state */  | 
732  | 43.9k  |     BITREAD_LOAD_STATE(cinfo, entropy->bitstate);  | 
733  | 43.9k  |     ASSIGN_STATE(state, entropy->saved);  | 
734  |  |  | 
735  |  |     /* Outer loop handles each block in the MCU */  | 
736  |  |  | 
737  | 293k  |     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 
738  | 249k  |       block = MCU_data[blkn];  | 
739  | 249k  |       ci = cinfo->MCU_membership[blkn];  | 
740  | 249k  |       compptr = cinfo->cur_comp_info[ci];  | 
741  | 249k  |       tbl = entropy->derived_tbls[compptr->dc_tbl_no];  | 
742  |  |  | 
743  |  |       /* Decode a single block's worth of coefficients */  | 
744  |  |  | 
745  |  |       /* Section F.2.2.1: decode the DC coefficient difference */  | 
746  | 249k  |       HUFF_DECODE(s, br_state, tbl, return FALSE, label1);  | 
747  | 249k  |       if (s) { | 
748  | 99.0k  |   CHECK_BIT_BUFFER(br_state, s, return FALSE);  | 
749  | 99.0k  |   r = GET_BITS(s);  | 
750  | 99.0k  |   s = HUFF_EXTEND(r, s);  | 
751  | 99.0k  |       }  | 
752  |  |  | 
753  |  |       /* Convert DC difference to actual value, update last_dc_val */  | 
754  | 249k  |       s += state.last_dc_val[ci];  | 
755  | 249k  |       state.last_dc_val[ci] = s;  | 
756  |  |       /* Scale and output the coefficient (assumes jpeg_natural_order[0]=0) */  | 
757  | 249k  |       (*block)[0] = (JCOEF) (s << Al);  | 
758  | 249k  |     }  | 
759  |  |  | 
760  |  |     /* Completed MCU, so update state */  | 
761  | 43.9k  |     BITREAD_SAVE_STATE(cinfo, entropy->bitstate);  | 
762  | 43.9k  |     ASSIGN_STATE(entropy->saved, state);  | 
763  | 43.9k  |   }  | 
764  |  |  | 
765  |  |   /* Account for restart interval if using restarts */  | 
766  | 1.30M  |   if (cinfo->restart_interval)  | 
767  | 10.7k  |     entropy->restarts_to_go--;  | 
768  |  |  | 
769  | 1.30M  |   return TRUE;  | 
770  | 1.30M  | }  | 
771  |  |  | 
772  |  |  | 
773  |  | /*  | 
774  |  |  * MCU decoding for AC initial scan (either spectral selection,  | 
775  |  |  * or first pass of successive approximation).  | 
776  |  |  */  | 
777  |  |  | 
778  |  | METHODDEF(boolean)  | 
779  |  | decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)  | 
780  | 5.25M  | { | 
781  | 5.25M  |   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;  | 
782  | 5.25M  |   register int s, k, r;  | 
783  | 5.25M  |   unsigned int EOBRUN;  | 
784  | 5.25M  |   int Se, Al;  | 
785  | 5.25M  |   const int * natural_order;  | 
786  | 5.25M  |   JBLOCKROW block;  | 
787  | 5.25M  |   BITREAD_STATE_VARS;  | 
788  | 5.25M  |   d_derived_tbl * tbl;  | 
789  |  |  | 
790  |  |   /* Process restart marker if needed; may have to suspend */  | 
791  | 5.25M  |   if (cinfo->restart_interval) { | 
792  | 117k  |     if (entropy->restarts_to_go == 0)  | 
793  | 7.30k  |       if (! process_restart(cinfo))  | 
794  | 0  |   return FALSE;  | 
795  | 117k  |   }  | 
796  |  |  | 
797  |  |   /* If we've run out of data, just leave the MCU set to zeroes.  | 
798  |  |    * This way, we return uniform gray for the remainder of the segment.  | 
799  |  |    */  | 
800  | 5.25M  |   if (! entropy->insufficient_data) { | 
801  |  |  | 
802  |  |     /* Load up working state.  | 
803  |  |      * We can avoid loading/saving bitread state if in an EOB run.  | 
804  |  |      */  | 
805  | 333k  |     EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */  | 
806  |  |  | 
807  |  |     /* There is always only one block per MCU */  | 
808  |  |  | 
809  | 333k  |     if (EOBRUN)      /* if it's a band of zeroes... */  | 
810  | 213k  |       EOBRUN--;      /* ...process it now (we do nothing) */  | 
811  | 119k  |     else { | 
812  | 119k  |       BITREAD_LOAD_STATE(cinfo, entropy->bitstate);  | 
813  | 119k  |       Se = cinfo->Se;  | 
814  | 119k  |       Al = cinfo->Al;  | 
815  | 119k  |       natural_order = cinfo->natural_order;  | 
816  | 119k  |       block = MCU_data[0];  | 
817  | 119k  |       tbl = entropy->ac_derived_tbl;  | 
818  |  |  | 
819  | 724k  |       for (k = cinfo->Ss; k <= Se; k++) { | 
820  | 704k  |   HUFF_DECODE(s, br_state, tbl, return FALSE, label2);  | 
821  | 704k  |   r = s >> 4;  | 
822  | 704k  |   s &= 15;  | 
823  | 704k  |   if (s) { | 
824  | 603k  |     k += r;  | 
825  | 603k  |     CHECK_BIT_BUFFER(br_state, s, return FALSE);  | 
826  | 603k  |     r = GET_BITS(s);  | 
827  | 603k  |     s = HUFF_EXTEND(r, s);  | 
828  |  |     /* Scale and output coefficient in natural (dezigzagged) order */  | 
829  | 603k  |     (*block)[natural_order[k]] = (JCOEF) (s << Al);  | 
830  | 603k  |   } else { | 
831  | 101k  |     if (r != 15) { /* EOBr, run length is 2^r + appended bits */ | 
832  | 100k  |       if (r) {   /* EOBr, r > 0 */ | 
833  | 20.8k  |         EOBRUN = 1 << r;  | 
834  | 20.8k  |         CHECK_BIT_BUFFER(br_state, r, return FALSE);  | 
835  | 20.8k  |         r = GET_BITS(r);  | 
836  | 20.8k  |         EOBRUN += r;  | 
837  | 20.8k  |         EOBRUN--;   /* this band is processed at this moment */  | 
838  | 20.8k  |       }  | 
839  | 100k  |       break;   /* force end-of-band */  | 
840  | 100k  |     }  | 
841  | 864  |     k += 15;    /* ZRL: skip 15 zeroes in band */  | 
842  | 864  |   }  | 
843  | 704k  |       }  | 
844  |  |  | 
845  | 119k  |       BITREAD_SAVE_STATE(cinfo, entropy->bitstate);  | 
846  | 119k  |     }  | 
847  |  |  | 
848  |  |     /* Completed MCU, so update state */  | 
849  | 333k  |     entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */  | 
850  | 333k  |   }  | 
851  |  |  | 
852  |  |   /* Account for restart interval if using restarts */  | 
853  | 5.25M  |   if (cinfo->restart_interval)  | 
854  | 117k  |     entropy->restarts_to_go--;  | 
855  |  |  | 
856  | 5.25M  |   return TRUE;  | 
857  | 5.25M  | }  | 
858  |  |  | 
859  |  |  | 
860  |  | /*  | 
861  |  |  * MCU decoding for DC successive approximation refinement scan.  | 
862  |  |  * Note: we assume such scans can be multi-component,  | 
863  |  |  * although the spec is not very clear on the point.  | 
864  |  |  */  | 
865  |  |  | 
866  |  | METHODDEF(boolean)  | 
867  |  | decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)  | 
868  | 305k  | { | 
869  | 305k  |   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;  | 
870  | 305k  |   JCOEF p1;  | 
871  | 305k  |   int blkn;  | 
872  | 305k  |   BITREAD_STATE_VARS;  | 
873  |  |  | 
874  |  |   /* Process restart marker if needed; may have to suspend */  | 
875  | 305k  |   if (cinfo->restart_interval) { | 
876  | 201k  |     if (entropy->restarts_to_go == 0)  | 
877  | 104k  |       if (! process_restart(cinfo))  | 
878  | 0  |   return FALSE;  | 
879  | 201k  |   }  | 
880  |  |  | 
881  |  |   /* Not worth the cycles to check insufficient_data here,  | 
882  |  |    * since we will not change the data anyway if we read zeroes.  | 
883  |  |    */  | 
884  |  |  | 
885  |  |   /* Load up working state */  | 
886  | 305k  |   BITREAD_LOAD_STATE(cinfo, entropy->bitstate);  | 
887  |  |  | 
888  | 305k  |   p1 = 1 << cinfo->Al;    /* 1 in the bit position being coded */  | 
889  |  |  | 
890  |  |   /* Outer loop handles each block in the MCU */  | 
891  |  |  | 
892  | 1.09M  |   for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 
893  |  |     /* Encoded data is simply the next bit of the two's-complement DC value */  | 
894  | 788k  |     CHECK_BIT_BUFFER(br_state, 1, return FALSE);  | 
895  | 788k  |     if (GET_BITS(1))  | 
896  | 84.7k  |       MCU_data[blkn][0][0] |= p1;  | 
897  |  |     /* Note: since we use |=, repeating the assignment later is safe */  | 
898  | 788k  |   }  | 
899  |  |  | 
900  |  |   /* Completed MCU, so update state */  | 
901  | 305k  |   BITREAD_SAVE_STATE(cinfo, entropy->bitstate);  | 
902  |  |  | 
903  |  |   /* Account for restart interval if using restarts */  | 
904  | 305k  |   if (cinfo->restart_interval)  | 
905  | 201k  |     entropy->restarts_to_go--;  | 
906  |  |  | 
907  | 305k  |   return TRUE;  | 
908  | 305k  | }  | 
909  |  |  | 
910  |  |  | 
911  |  | /*  | 
912  |  |  * MCU decoding for AC successive approximation refinement scan.  | 
913  |  |  */  | 
914  |  |  | 
915  |  | METHODDEF(boolean)  | 
916  |  | decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)  | 
917  | 1.44M  | { | 
918  | 1.44M  |   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;  | 
919  | 1.44M  |   register int s, k, r;  | 
920  | 1.44M  |   unsigned int EOBRUN;  | 
921  | 1.44M  |   int Se;  | 
922  | 1.44M  |   JCOEF p1, m1;  | 
923  | 1.44M  |   const int * natural_order;  | 
924  | 1.44M  |   JBLOCKROW block;  | 
925  | 1.44M  |   JCOEFPTR thiscoef;  | 
926  | 1.44M  |   BITREAD_STATE_VARS;  | 
927  | 1.44M  |   d_derived_tbl * tbl;  | 
928  | 1.44M  |   int num_newnz;  | 
929  | 1.44M  |   int newnz_pos[DCTSIZE2];  | 
930  |  |  | 
931  |  |   /* Process restart marker if needed; may have to suspend */  | 
932  | 1.44M  |   if (cinfo->restart_interval) { | 
933  | 850k  |     if (entropy->restarts_to_go == 0)  | 
934  | 48.8k  |       if (! process_restart(cinfo))  | 
935  | 0  |   return FALSE;  | 
936  | 850k  |   }  | 
937  |  |  | 
938  |  |   /* If we've run out of data, don't modify the MCU.  | 
939  |  |    */  | 
940  | 1.44M  |   if (! entropy->insufficient_data) { | 
941  |  |  | 
942  | 342k  |     Se = cinfo->Se;  | 
943  | 342k  |     p1 = 1 << cinfo->Al;  /* 1 in the bit position being coded */  | 
944  | 342k  |     m1 = -p1;     /* -1 in the bit position being coded */  | 
945  | 342k  |     natural_order = cinfo->natural_order;  | 
946  |  |  | 
947  |  |     /* Load up working state */  | 
948  | 342k  |     BITREAD_LOAD_STATE(cinfo, entropy->bitstate);  | 
949  | 342k  |     EOBRUN = entropy->saved.EOBRUN; /* only part of saved state we need */  | 
950  |  |  | 
951  |  |     /* There is always only one block per MCU */  | 
952  | 342k  |     block = MCU_data[0];  | 
953  | 342k  |     tbl = entropy->ac_derived_tbl;  | 
954  |  |  | 
955  |  |     /* If we are forced to suspend, we must undo the assignments to any newly  | 
956  |  |      * nonzero coefficients in the block, because otherwise we'd get confused  | 
957  |  |      * next time about which coefficients were already nonzero.  | 
958  |  |      * But we need not undo addition of bits to already-nonzero coefficients;  | 
959  |  |      * instead, we can test the current bit to see if we already did it.  | 
960  |  |      */  | 
961  | 342k  |     num_newnz = 0;  | 
962  |  |  | 
963  |  |     /* initialize coefficient loop counter to start of band */  | 
964  | 342k  |     k = cinfo->Ss;  | 
965  |  |  | 
966  | 342k  |     if (EOBRUN == 0) { | 
967  | 476k  |       do { | 
968  | 476k  |   HUFF_DECODE(s, br_state, tbl, goto undoit, label3);  | 
969  | 476k  |   r = s >> 4;  | 
970  | 476k  |   s &= 15;  | 
971  | 476k  |   if (s) { | 
972  | 386k  |     if (s != 1)    /* size of new coef should always be 1 */  | 
973  | 18.1k  |       WARNMS(cinfo, JWRN_HUFF_BAD_CODE);  | 
974  | 386k  |     CHECK_BIT_BUFFER(br_state, 1, goto undoit);  | 
975  | 386k  |     if (GET_BITS(1))  | 
976  | 190k  |       s = p1;    /* newly nonzero coef is positive */  | 
977  | 196k  |     else  | 
978  | 196k  |       s = m1;   /* newly nonzero coef is negative */  | 
979  | 386k  |   } else { | 
980  | 89.3k  |     if (r != 15) { | 
981  | 86.2k  |       EOBRUN = 1 << r;  /* EOBr, run length is 2^r + appended bits */  | 
982  | 86.2k  |       if (r) { | 
983  | 22.5k  |         CHECK_BIT_BUFFER(br_state, r, goto undoit);  | 
984  | 22.5k  |         r = GET_BITS(r);  | 
985  | 22.5k  |         EOBRUN += r;  | 
986  | 22.5k  |       }  | 
987  | 86.2k  |       break;   /* rest of block is handled by EOB logic */  | 
988  | 86.2k  |     }  | 
989  |  |     /* note s = 0 for processing ZRL */  | 
990  | 89.3k  |   }  | 
991  |  |   /* Advance over already-nonzero coefs and r still-zero coefs,  | 
992  |  |    * appending correction bits to the nonzeroes.  A correction bit is 1  | 
993  |  |    * if the absolute value of the coefficient must be increased.  | 
994  |  |    */  | 
995  | 1.50M  |   do { | 
996  | 1.50M  |     thiscoef = *block + natural_order[k];  | 
997  | 1.50M  |     if (*thiscoef) { | 
998  | 416k  |       CHECK_BIT_BUFFER(br_state, 1, goto undoit);  | 
999  | 416k  |       if (GET_BITS(1)) { | 
1000  | 166k  |         if ((*thiscoef & p1) == 0) { /* do nothing if already set it */ | 
1001  | 164k  |     if (*thiscoef >= 0)  | 
1002  | 83.1k  |       *thiscoef += p1;  | 
1003  | 80.9k  |     else  | 
1004  | 80.9k  |       *thiscoef += m1;  | 
1005  | 164k  |         }  | 
1006  | 166k  |       }  | 
1007  | 1.09M  |     } else { | 
1008  | 1.09M  |       if (--r < 0)  | 
1009  | 377k  |         break;   /* reached target zero coefficient */  | 
1010  | 1.09M  |     }  | 
1011  | 1.13M  |     k++;  | 
1012  | 1.13M  |   } while (k <= Se);  | 
1013  | 390k  |   if (s) { | 
1014  | 386k  |     int pos = natural_order[k];  | 
1015  |  |     /* Output newly nonzero coefficient */  | 
1016  | 386k  |     (*block)[pos] = (JCOEF) s;  | 
1017  |  |     /* Remember its position in case we have to suspend */  | 
1018  | 386k  |     newnz_pos[num_newnz++] = pos;  | 
1019  | 386k  |   }  | 
1020  | 390k  |   k++;  | 
1021  | 390k  |       } while (k <= Se);  | 
1022  | 100k  |     }  | 
1023  |  |  | 
1024  | 342k  |     if (EOBRUN) { | 
1025  |  |       /* Scan any remaining coefficient positions after the end-of-band  | 
1026  |  |        * (the last newly nonzero coefficient, if any).  Append a correction  | 
1027  |  |        * bit to each already-nonzero coefficient.  A correction bit is 1  | 
1028  |  |        * if the absolute value of the coefficient must be increased.  | 
1029  |  |        */  | 
1030  | 12.9M  |       do { | 
1031  | 12.9M  |   thiscoef = *block + natural_order[k];  | 
1032  | 12.9M  |   if (*thiscoef) { | 
1033  | 172k  |     CHECK_BIT_BUFFER(br_state, 1, goto undoit);  | 
1034  | 172k  |     if (GET_BITS(1)) { | 
1035  | 37.0k  |       if ((*thiscoef & p1) == 0) { /* do nothing if already changed it */ | 
1036  | 24.0k  |         if (*thiscoef >= 0)  | 
1037  | 12.2k  |     *thiscoef += p1;  | 
1038  | 11.8k  |         else  | 
1039  | 11.8k  |     *thiscoef += m1;  | 
1040  | 24.0k  |       }  | 
1041  | 37.0k  |     }  | 
1042  | 172k  |   }  | 
1043  | 12.9M  |   k++;  | 
1044  | 12.9M  |       } while (k <= Se);  | 
1045  |  |       /* Count one block completed in EOB run */  | 
1046  | 327k  |       EOBRUN--;  | 
1047  | 327k  |     }  | 
1048  |  |  | 
1049  |  |     /* Completed MCU, so update state */  | 
1050  | 342k  |     BITREAD_SAVE_STATE(cinfo, entropy->bitstate);  | 
1051  | 342k  |     entropy->saved.EOBRUN = EOBRUN; /* only part of saved state we need */  | 
1052  | 342k  |   }  | 
1053  |  |  | 
1054  |  |   /* Account for restart interval if using restarts */  | 
1055  | 1.44M  |   if (cinfo->restart_interval)  | 
1056  | 850k  |     entropy->restarts_to_go--;  | 
1057  |  |  | 
1058  | 1.44M  |   return TRUE;  | 
1059  |  |  | 
1060  | 0  | undoit:  | 
1061  |  |   /* Re-zero any output coefficients that we made newly nonzero */  | 
1062  | 0  |   while (num_newnz)  | 
1063  | 0  |     (*block)[newnz_pos[--num_newnz]] = 0;  | 
1064  |  | 
  | 
1065  | 0  |   return FALSE;  | 
1066  | 1.44M  | }  | 
1067  |  |  | 
1068  |  |  | 
1069  |  | /*  | 
1070  |  |  * Decode one MCU's worth of Huffman-compressed coefficients,  | 
1071  |  |  * partial blocks.  | 
1072  |  |  */  | 
1073  |  |  | 
1074  |  | METHODDEF(boolean)  | 
1075  |  | decode_mcu_sub (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)  | 
1076  | 1.95M  | { | 
1077  | 1.95M  |   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;  | 
1078  | 1.95M  |   const int * natural_order;  | 
1079  | 1.95M  |   int Se, blkn;  | 
1080  | 1.95M  |   BITREAD_STATE_VARS;  | 
1081  | 1.95M  |   savable_state state;  | 
1082  |  |  | 
1083  |  |   /* Process restart marker if needed; may have to suspend */  | 
1084  | 1.95M  |   if (cinfo->restart_interval) { | 
1085  | 575k  |     if (entropy->restarts_to_go == 0)  | 
1086  | 20.5k  |       if (! process_restart(cinfo))  | 
1087  | 0  |   return FALSE;  | 
1088  | 575k  |   }  | 
1089  |  |  | 
1090  |  |   /* If we've run out of data, just leave the MCU set to zeroes.  | 
1091  |  |    * This way, we return uniform gray for the remainder of the segment.  | 
1092  |  |    */  | 
1093  | 1.95M  |   if (! entropy->insufficient_data) { | 
1094  |  |  | 
1095  | 3.00k  |     natural_order = cinfo->natural_order;  | 
1096  | 3.00k  |     Se = cinfo->lim_Se;  | 
1097  |  |  | 
1098  |  |     /* Load up working state */  | 
1099  | 3.00k  |     BITREAD_LOAD_STATE(cinfo, entropy->bitstate);  | 
1100  | 3.00k  |     ASSIGN_STATE(state, entropy->saved);  | 
1101  |  |  | 
1102  |  |     /* Outer loop handles each block in the MCU */  | 
1103  |  |  | 
1104  | 7.25k  |     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 
1105  | 4.24k  |       JBLOCKROW block = MCU_data[blkn];  | 
1106  | 4.24k  |       d_derived_tbl * htbl;  | 
1107  | 4.24k  |       register int s, k, r;  | 
1108  | 4.24k  |       int coef_limit, ci;  | 
1109  |  |  | 
1110  |  |       /* Decode a single block's worth of coefficients */  | 
1111  |  |  | 
1112  |  |       /* Section F.2.2.1: decode the DC coefficient difference */  | 
1113  | 4.24k  |       htbl = entropy->dc_cur_tbls[blkn];  | 
1114  | 4.24k  |       HUFF_DECODE(s, br_state, htbl, return FALSE, label1);  | 
1115  |  |  | 
1116  | 4.24k  |       htbl = entropy->ac_cur_tbls[blkn];  | 
1117  | 4.24k  |       k = 1;  | 
1118  | 4.24k  |       coef_limit = entropy->coef_limit[blkn];  | 
1119  | 4.24k  |       if (coef_limit) { | 
1120  |  |   /* Convert DC difference to actual value, update last_dc_val */  | 
1121  | 4.24k  |   if (s) { | 
1122  | 1.72k  |     CHECK_BIT_BUFFER(br_state, s, return FALSE);  | 
1123  | 1.72k  |     r = GET_BITS(s);  | 
1124  | 1.72k  |     s = HUFF_EXTEND(r, s);  | 
1125  | 1.72k  |   }  | 
1126  | 4.24k  |   ci = cinfo->MCU_membership[blkn];  | 
1127  | 4.24k  |   s += state.last_dc_val[ci];  | 
1128  | 4.24k  |   state.last_dc_val[ci] = s;  | 
1129  |  |   /* Output the DC coefficient */  | 
1130  | 4.24k  |   (*block)[0] = (JCOEF) s;  | 
1131  |  |  | 
1132  |  |   /* Section F.2.2.2: decode the AC coefficients */  | 
1133  |  |   /* Since zeroes are skipped, output area must be cleared beforehand */  | 
1134  | 17.8k  |   for (; k < coef_limit; k++) { | 
1135  | 14.3k  |     HUFF_DECODE(s, br_state, htbl, return FALSE, label2);  | 
1136  |  |  | 
1137  | 14.3k  |     r = s >> 4;  | 
1138  | 14.3k  |     s &= 15;  | 
1139  |  |  | 
1140  | 14.3k  |     if (s) { | 
1141  | 13.5k  |       k += r;  | 
1142  | 13.5k  |       CHECK_BIT_BUFFER(br_state, s, return FALSE);  | 
1143  | 13.5k  |       r = GET_BITS(s);  | 
1144  | 13.5k  |       s = HUFF_EXTEND(r, s);  | 
1145  |  |       /* Output coefficient in natural (dezigzagged) order.  | 
1146  |  |        * Note: the extra entries in natural_order[] will save us  | 
1147  |  |        * if k > Se, which could happen if the data is corrupted.  | 
1148  |  |        */  | 
1149  | 13.5k  |       (*block)[natural_order[k]] = (JCOEF) s;  | 
1150  | 13.5k  |     } else { | 
1151  | 810  |       if (r != 15)  | 
1152  | 810  |         goto EndOfBlock;  | 
1153  | 0  |       k += 15;  | 
1154  | 0  |     }  | 
1155  | 14.3k  |   }  | 
1156  | 4.24k  |       } else { | 
1157  | 0  |   if (s) { | 
1158  | 0  |     CHECK_BIT_BUFFER(br_state, s, return FALSE);  | 
1159  | 0  |     DROP_BITS(s);  | 
1160  | 0  |   }  | 
1161  | 0  |       }  | 
1162  |  |  | 
1163  |  |       /* Section F.2.2.2: decode the AC coefficients */  | 
1164  |  |       /* In this path we just discard the values */  | 
1165  | 6.88k  |       for (; k <= Se; k++) { | 
1166  | 3.78k  |   HUFF_DECODE(s, br_state, htbl, return FALSE, label3);  | 
1167  |  |  | 
1168  | 3.78k  |   r = s >> 4;  | 
1169  | 3.78k  |   s &= 15;  | 
1170  |  |  | 
1171  | 3.78k  |   if (s) { | 
1172  | 3.44k  |     k += r;  | 
1173  | 3.44k  |     CHECK_BIT_BUFFER(br_state, s, return FALSE);  | 
1174  | 3.44k  |     DROP_BITS(s);  | 
1175  | 3.44k  |   } else { | 
1176  | 342  |     if (r != 15)  | 
1177  | 342  |       break;  | 
1178  | 0  |     k += 15;  | 
1179  | 0  |   }  | 
1180  | 3.78k  |       }  | 
1181  |  |  | 
1182  | 4.24k  |       EndOfBlock: ;  | 
1183  | 4.24k  |     }  | 
1184  |  |  | 
1185  |  |     /* Completed MCU, so update state */  | 
1186  | 3.00k  |     BITREAD_SAVE_STATE(cinfo, entropy->bitstate);  | 
1187  | 3.00k  |     ASSIGN_STATE(entropy->saved, state);  | 
1188  | 3.00k  |   }  | 
1189  |  |  | 
1190  |  |   /* Account for restart interval if using restarts */  | 
1191  | 1.95M  |   if (cinfo->restart_interval)  | 
1192  | 575k  |     entropy->restarts_to_go--;  | 
1193  |  |  | 
1194  | 1.95M  |   return TRUE;  | 
1195  | 1.95M  | }  | 
1196  |  |  | 
1197  |  |  | 
1198  |  | /*  | 
1199  |  |  * Decode one MCU's worth of Huffman-compressed coefficients,  | 
1200  |  |  * full-size blocks.  | 
1201  |  |  */  | 
1202  |  |  | 
1203  |  | METHODDEF(boolean)  | 
1204  |  | decode_mcu (j_decompress_ptr cinfo, JBLOCKARRAY MCU_data)  | 
1205  | 2.43M  | { | 
1206  | 2.43M  |   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;  | 
1207  | 2.43M  |   int blkn;  | 
1208  | 2.43M  |   BITREAD_STATE_VARS;  | 
1209  | 2.43M  |   savable_state state;  | 
1210  |  |  | 
1211  |  |   /* Process restart marker if needed; may have to suspend */  | 
1212  | 2.43M  |   if (cinfo->restart_interval) { | 
1213  | 702k  |     if (entropy->restarts_to_go == 0)  | 
1214  | 152k  |       if (! process_restart(cinfo))  | 
1215  | 0  |   return FALSE;  | 
1216  | 702k  |   }  | 
1217  |  |  | 
1218  |  |   /* If we've run out of data, just leave the MCU set to zeroes.  | 
1219  |  |    * This way, we return uniform gray for the remainder of the segment.  | 
1220  |  |    */  | 
1221  | 2.43M  |   if (! entropy->insufficient_data) { | 
1222  |  |  | 
1223  |  |     /* Load up working state */  | 
1224  | 687k  |     BITREAD_LOAD_STATE(cinfo, entropy->bitstate);  | 
1225  | 687k  |     ASSIGN_STATE(state, entropy->saved);  | 
1226  |  |  | 
1227  |  |     /* Outer loop handles each block in the MCU */  | 
1228  |  |  | 
1229  | 3.42M  |     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 
1230  | 2.73M  |       JBLOCKROW block = MCU_data[blkn];  | 
1231  | 2.73M  |       d_derived_tbl * htbl;  | 
1232  | 2.73M  |       register int s, k, r;  | 
1233  | 2.73M  |       int coef_limit, ci;  | 
1234  |  |  | 
1235  |  |       /* Decode a single block's worth of coefficients */  | 
1236  |  |  | 
1237  |  |       /* Section F.2.2.1: decode the DC coefficient difference */  | 
1238  | 2.73M  |       htbl = entropy->dc_cur_tbls[blkn];  | 
1239  | 2.73M  |       HUFF_DECODE(s, br_state, htbl, return FALSE, label1);  | 
1240  |  |  | 
1241  | 2.73M  |       htbl = entropy->ac_cur_tbls[blkn];  | 
1242  | 2.73M  |       k = 1;  | 
1243  | 2.73M  |       coef_limit = entropy->coef_limit[blkn];  | 
1244  | 2.73M  |       if (coef_limit) { | 
1245  |  |   /* Convert DC difference to actual value, update last_dc_val */  | 
1246  | 2.73M  |   if (s) { | 
1247  | 466k  |     CHECK_BIT_BUFFER(br_state, s, return FALSE);  | 
1248  | 466k  |     r = GET_BITS(s);  | 
1249  | 466k  |     s = HUFF_EXTEND(r, s);  | 
1250  | 466k  |   }  | 
1251  | 2.73M  |   ci = cinfo->MCU_membership[blkn];  | 
1252  | 2.73M  |   s += state.last_dc_val[ci];  | 
1253  | 2.73M  |   state.last_dc_val[ci] = s;  | 
1254  |  |   /* Output the DC coefficient */  | 
1255  | 2.73M  |   (*block)[0] = (JCOEF) s;  | 
1256  |  |  | 
1257  |  |   /* Section F.2.2.2: decode the AC coefficients */  | 
1258  |  |   /* Since zeroes are skipped, output area must be cleared beforehand */  | 
1259  | 6.36M  |   for (; k < coef_limit; k++) { | 
1260  | 6.14M  |     HUFF_DECODE(s, br_state, htbl, return FALSE, label2);  | 
1261  |  |  | 
1262  | 6.14M  |     r = s >> 4;  | 
1263  | 6.14M  |     s &= 15;  | 
1264  |  |  | 
1265  | 6.14M  |     if (s) { | 
1266  | 3.62M  |       k += r;  | 
1267  | 3.62M  |       CHECK_BIT_BUFFER(br_state, s, return FALSE);  | 
1268  | 3.62M  |       r = GET_BITS(s);  | 
1269  | 3.62M  |       s = HUFF_EXTEND(r, s);  | 
1270  |  |       /* Output coefficient in natural (dezigzagged) order.  | 
1271  |  |        * Note: the extra entries in jpeg_natural_order[] will save us  | 
1272  |  |        * if k >= DCTSIZE2, which could happen if the data is corrupted.  | 
1273  |  |        */  | 
1274  | 3.62M  |       (*block)[jpeg_natural_order[k]] = (JCOEF) s;  | 
1275  | 3.62M  |     } else { | 
1276  | 2.51M  |       if (r != 15)  | 
1277  | 2.50M  |         goto EndOfBlock;  | 
1278  | 3.68k  |       k += 15;  | 
1279  | 3.68k  |     }  | 
1280  | 6.14M  |   }  | 
1281  | 2.73M  |       } else { | 
1282  | 0  |   if (s) { | 
1283  | 0  |     CHECK_BIT_BUFFER(br_state, s, return FALSE);  | 
1284  | 0  |     DROP_BITS(s);  | 
1285  | 0  |   }  | 
1286  | 0  |       }  | 
1287  |  |  | 
1288  |  |       /* Section F.2.2.2: decode the AC coefficients */  | 
1289  |  |       /* In this path we just discard the values */  | 
1290  | 1.94M  |       for (; k < DCTSIZE2; k++) { | 
1291  | 1.93M  |   HUFF_DECODE(s, br_state, htbl, return FALSE, label3);  | 
1292  |  |  | 
1293  | 1.93M  |   r = s >> 4;  | 
1294  | 1.93M  |   s &= 15;  | 
1295  |  |  | 
1296  | 1.93M  |   if (s) { | 
1297  | 1.70M  |     k += r;  | 
1298  | 1.70M  |     CHECK_BIT_BUFFER(br_state, s, return FALSE);  | 
1299  | 1.70M  |     DROP_BITS(s);  | 
1300  | 1.70M  |   } else { | 
1301  | 228k  |     if (r != 15)  | 
1302  | 215k  |       break;  | 
1303  | 13.0k  |     k += 15;  | 
1304  | 13.0k  |   }  | 
1305  | 1.93M  |       }  | 
1306  |  |  | 
1307  | 2.73M  |       EndOfBlock: ;  | 
1308  | 2.73M  |     }  | 
1309  |  |  | 
1310  |  |     /* Completed MCU, so update state */  | 
1311  | 687k  |     BITREAD_SAVE_STATE(cinfo, entropy->bitstate);  | 
1312  | 687k  |     ASSIGN_STATE(entropy->saved, state);  | 
1313  | 687k  |   }  | 
1314  |  |  | 
1315  |  |   /* Account for restart interval if using restarts */  | 
1316  | 2.43M  |   if (cinfo->restart_interval)  | 
1317  | 702k  |     entropy->restarts_to_go--;  | 
1318  |  |  | 
1319  | 2.43M  |   return TRUE;  | 
1320  | 2.43M  | }  | 
1321  |  |  | 
1322  |  |  | 
1323  |  | /*  | 
1324  |  |  * Initialize for a Huffman-compressed scan.  | 
1325  |  |  */  | 
1326  |  |  | 
1327  |  | METHODDEF(void)  | 
1328  |  | start_pass_huff_decoder (j_decompress_ptr cinfo)  | 
1329  | 4.07k  | { | 
1330  | 4.07k  |   huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;  | 
1331  | 4.07k  |   int ci, blkn, tbl, i;  | 
1332  | 4.07k  |   jpeg_component_info * compptr;  | 
1333  |  |  | 
1334  | 4.07k  |   if (cinfo->progressive_mode) { | 
1335  |  |     /* Validate progressive scan parameters */  | 
1336  | 3.40k  |     if (cinfo->Ss == 0) { | 
1337  | 667  |       if (cinfo->Se != 0)  | 
1338  | 4  |   goto bad;  | 
1339  | 2.73k  |     } else { | 
1340  |  |       /* need not check Ss/Se < 0 since they came from unsigned bytes */  | 
1341  | 2.73k  |       if (cinfo->Se < cinfo->Ss || cinfo->Se > cinfo->lim_Se)  | 
1342  | 16  |   goto bad;  | 
1343  |  |       /* AC scans may have only one component */  | 
1344  | 2.71k  |       if (cinfo->comps_in_scan != 1)  | 
1345  | 1  |   goto bad;  | 
1346  | 2.71k  |     }  | 
1347  | 3.38k  |     if (cinfo->Ah != 0) { | 
1348  |  |       /* Successive approximation refinement scan: must have Al = Ah-1. */  | 
1349  | 2.03k  |       if (cinfo->Ah-1 != cinfo->Al)  | 
1350  | 17  |   goto bad;  | 
1351  | 2.03k  |     }  | 
1352  | 3.36k  |     if (cinfo->Al > 13) { /* need not check for < 0 */ | 
1353  |  |       /* Arguably the maximum Al value should be less than 13 for 8-bit  | 
1354  |  |        * precision, but the spec doesn't say so, and we try to be liberal  | 
1355  |  |        * about what we accept.  Note: large Al values could result in  | 
1356  |  |        * out-of-range DC coefficients during early scans, leading to bizarre  | 
1357  |  |        * displays due to overflows in the IDCT math.  But we won't crash.  | 
1358  |  |        */  | 
1359  | 39  |       bad:  | 
1360  | 39  |       ERREXIT4(cinfo, JERR_BAD_PROGRESSION,  | 
1361  | 39  |          cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);  | 
1362  | 39  |     }  | 
1363  |  |     /* Update progression status, and verify that scan order is legal.  | 
1364  |  |      * Note that inter-scan inconsistencies are treated as warnings  | 
1365  |  |      * not fatal errors ... not clear if this is right way to behave.  | 
1366  |  |      */  | 
1367  | 7.98k  |     for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 
1368  | 4.58k  |       int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;  | 
1369  | 4.58k  |       int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];  | 
1370  | 4.58k  |       if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */  | 
1371  | 2.44k  |   WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);  | 
1372  | 56.6k  |       for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) { | 
1373  | 52.0k  |   int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];  | 
1374  | 52.0k  |   if (cinfo->Ah != expected)  | 
1375  | 31.7k  |     WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);  | 
1376  | 52.0k  |   coef_bit_ptr[coefi] = cinfo->Al;  | 
1377  | 52.0k  |       }  | 
1378  | 4.58k  |     }  | 
1379  |  |  | 
1380  |  |     /* Select MCU decoding routine */  | 
1381  | 3.40k  |     if (cinfo->Ah == 0) { | 
1382  | 1.34k  |       if (cinfo->Ss == 0)  | 
1383  | 579  |   entropy->pub.decode_mcu = decode_mcu_DC_first;  | 
1384  | 763  |       else  | 
1385  | 763  |   entropy->pub.decode_mcu = decode_mcu_AC_first;  | 
1386  | 2.05k  |     } else { | 
1387  | 2.05k  |       if (cinfo->Ss == 0)  | 
1388  | 74  |   entropy->pub.decode_mcu = decode_mcu_DC_refine;  | 
1389  | 1.98k  |       else  | 
1390  | 1.98k  |   entropy->pub.decode_mcu = decode_mcu_AC_refine;  | 
1391  | 2.05k  |     }  | 
1392  |  |  | 
1393  | 7.97k  |     for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 
1394  | 4.56k  |       compptr = cinfo->cur_comp_info[ci];  | 
1395  |  |       /* Make sure requested tables are present, and compute derived tables.  | 
1396  |  |        * We may build same derived table more than once, but it's not expensive.  | 
1397  |  |        */  | 
1398  | 4.56k  |       if (cinfo->Ss == 0) { | 
1399  | 1.86k  |   if (cinfo->Ah == 0) { /* DC refinement needs no table */ | 
1400  | 1.68k  |     tbl = compptr->dc_tbl_no;  | 
1401  | 1.68k  |     jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,  | 
1402  | 1.68k  |           & entropy->derived_tbls[tbl]);  | 
1403  | 1.68k  |   }  | 
1404  | 2.70k  |       } else { | 
1405  | 2.70k  |   tbl = compptr->ac_tbl_no;  | 
1406  | 2.70k  |   jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,  | 
1407  | 2.70k  |         & entropy->derived_tbls[tbl]);  | 
1408  |  |   /* remember the single active table */  | 
1409  | 2.70k  |   entropy->ac_derived_tbl = entropy->derived_tbls[tbl];  | 
1410  | 2.70k  |       }  | 
1411  |  |       /* Initialize DC predictions to 0 */  | 
1412  | 4.56k  |       entropy->saved.last_dc_val[ci] = 0;  | 
1413  | 4.56k  |     }  | 
1414  |  |  | 
1415  |  |     /* Initialize private state variables */  | 
1416  | 3.40k  |     entropy->saved.EOBRUN = 0;  | 
1417  | 3.40k  |   } else { | 
1418  |  |     /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.  | 
1419  |  |      * This ought to be an error condition, but we make it a warning because  | 
1420  |  |      * there are some baseline files out there with all zeroes in these bytes.  | 
1421  |  |      */  | 
1422  | 669  |     if (cinfo->Ss != 0 || cinfo->Ah != 0 || cinfo->Al != 0 ||  | 
1423  | 669  |   ((cinfo->is_baseline || cinfo->Se < DCTSIZE2) &&  | 
1424  | 342  |   cinfo->Se != cinfo->lim_Se))  | 
1425  | 362  |       WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);  | 
1426  |  |  | 
1427  |  |     /* Select MCU decoding routine */  | 
1428  |  |     /* We retain the hard-coded case for full-size blocks.  | 
1429  |  |      * This is not necessary, but it appears that this version is slightly  | 
1430  |  |      * more performant in the given implementation.  | 
1431  |  |      * With an improved implementation we would prefer a single optimized  | 
1432  |  |      * function.  | 
1433  |  |      */  | 
1434  | 669  |     if (cinfo->lim_Se != DCTSIZE2-1)  | 
1435  | 52  |       entropy->pub.decode_mcu = decode_mcu_sub;  | 
1436  | 617  |     else  | 
1437  | 617  |       entropy->pub.decode_mcu = decode_mcu;  | 
1438  |  |  | 
1439  | 2.05k  |     for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 
1440  | 1.38k  |       compptr = cinfo->cur_comp_info[ci];  | 
1441  |  |       /* Compute derived values for Huffman tables */  | 
1442  |  |       /* We may do this more than once for a table, but it's not expensive */  | 
1443  | 1.38k  |       tbl = compptr->dc_tbl_no;  | 
1444  | 1.38k  |       jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,  | 
1445  | 1.38k  |             & entropy->dc_derived_tbls[tbl]);  | 
1446  | 1.38k  |       if (cinfo->lim_Se) { /* AC needs no table when not present */ | 
1447  | 1.34k  |   tbl = compptr->ac_tbl_no;  | 
1448  | 1.34k  |   jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,  | 
1449  | 1.34k  |         & entropy->ac_derived_tbls[tbl]);  | 
1450  | 1.34k  |       }  | 
1451  |  |       /* Initialize DC predictions to 0 */  | 
1452  | 1.38k  |       entropy->saved.last_dc_val[ci] = 0;  | 
1453  | 1.38k  |     }  | 
1454  |  |  | 
1455  |  |     /* Precalculate decoding info for each block in an MCU of this scan */  | 
1456  | 3.78k  |     for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { | 
1457  | 3.11k  |       ci = cinfo->MCU_membership[blkn];  | 
1458  | 3.11k  |       compptr = cinfo->cur_comp_info[ci];  | 
1459  |  |       /* Precalculate which table to use for each block */  | 
1460  | 3.11k  |       entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];  | 
1461  | 3.11k  |       entropy->ac_cur_tbls[blkn] =  /* AC needs no table when not present */  | 
1462  | 3.11k  |   cinfo->lim_Se ? entropy->ac_derived_tbls[compptr->ac_tbl_no] : NULL;  | 
1463  |  |       /* Decide whether we really care about the coefficient values */  | 
1464  | 3.11k  |       if (compptr->component_needed) { | 
1465  | 3.11k  |   ci = compptr->DCT_v_scaled_size;  | 
1466  | 3.11k  |   i = compptr->DCT_h_scaled_size;  | 
1467  | 3.11k  |   switch (cinfo->lim_Se) { | 
1468  | 28  |   case (1*1-1):  | 
1469  | 28  |     entropy->coef_limit[blkn] = 1;  | 
1470  | 28  |     break;  | 
1471  | 0  |   case (2*2-1):  | 
1472  | 0  |     if (ci <= 0 || ci > 2) ci = 2;  | 
1473  | 0  |     if (i <= 0 || i > 2) i = 2;  | 
1474  | 0  |     entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order2[ci - 1][i - 1];  | 
1475  | 0  |     break;  | 
1476  | 29  |   case (3*3-1):  | 
1477  | 29  |     if (ci <= 0 || ci > 3) ci = 3;  | 
1478  | 29  |     if (i <= 0 || i > 3) i = 3;  | 
1479  | 29  |     entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order3[ci - 1][i - 1];  | 
1480  | 29  |     break;  | 
1481  | 13  |   case (4*4-1):  | 
1482  | 13  |     if (ci <= 0 || ci > 4) ci = 4;  | 
1483  | 13  |     if (i <= 0 || i > 4) i = 4;  | 
1484  | 13  |     entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order4[ci - 1][i - 1];  | 
1485  | 13  |     break;  | 
1486  | 0  |   case (5*5-1):  | 
1487  | 0  |     if (ci <= 0 || ci > 5) ci = 5;  | 
1488  | 0  |     if (i <= 0 || i > 5) i = 5;  | 
1489  | 0  |     entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order5[ci - 1][i - 1];  | 
1490  | 0  |     break;  | 
1491  | 4  |   case (6*6-1):  | 
1492  | 4  |     if (ci <= 0 || ci > 6) ci = 6;  | 
1493  | 4  |     if (i <= 0 || i > 6) i = 6;  | 
1494  | 4  |     entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order6[ci - 1][i - 1];  | 
1495  | 4  |     break;  | 
1496  | 0  |   case (7*7-1):  | 
1497  | 0  |     if (ci <= 0 || ci > 7) ci = 7;  | 
1498  | 0  |     if (i <= 0 || i > 7) i = 7;  | 
1499  | 0  |     entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order7[ci - 1][i - 1];  | 
1500  | 0  |     break;  | 
1501  | 3.04k  |   default:  | 
1502  | 3.04k  |     if (ci <= 0 || ci > 8) ci = 8;  | 
1503  | 3.04k  |     if (i <= 0 || i > 8) i = 8;  | 
1504  | 3.04k  |     entropy->coef_limit[blkn] = 1 + jpeg_zigzag_order[ci - 1][i - 1];  | 
1505  | 3.11k  |   }  | 
1506  | 3.11k  |       } else { | 
1507  | 0  |   entropy->coef_limit[blkn] = 0;  | 
1508  | 0  |       }  | 
1509  | 3.11k  |     }  | 
1510  | 669  |   }  | 
1511  |  |  | 
1512  |  |   /* Initialize bitread state variables */  | 
1513  | 4.07k  |   entropy->bitstate.bits_left = 0;  | 
1514  | 4.07k  |   entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */  | 
1515  | 4.07k  |   entropy->insufficient_data = FALSE;  | 
1516  |  |  | 
1517  |  |   /* Initialize restart counter */  | 
1518  | 4.07k  |   entropy->restarts_to_go = cinfo->restart_interval;  | 
1519  | 4.07k  | }  | 
1520  |  |  | 
1521  |  |  | 
1522  |  | /*  | 
1523  |  |  * Module initialization routine for Huffman entropy decoding.  | 
1524  |  |  */  | 
1525  |  |  | 
1526  |  | GLOBAL(void)  | 
1527  |  | jinit_huff_decoder (j_decompress_ptr cinfo)  | 
1528  | 875  | { | 
1529  | 875  |   huff_entropy_ptr entropy;  | 
1530  | 875  |   int i;  | 
1531  |  |  | 
1532  | 875  |   entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small)  | 
1533  | 875  |     ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(huff_entropy_decoder));  | 
1534  | 875  |   cinfo->entropy = &entropy->pub;  | 
1535  | 875  |   entropy->pub.start_pass = start_pass_huff_decoder;  | 
1536  | 875  |   entropy->pub.finish_pass = finish_pass_huff;  | 
1537  |  |  | 
1538  | 875  |   if (cinfo->progressive_mode) { | 
1539  |  |     /* Create progression status table */  | 
1540  | 469  |     int *coef_bit_ptr, ci;  | 
1541  | 469  |     cinfo->coef_bits = (int (*)[DCTSIZE2]) (*cinfo->mem->alloc_small)  | 
1542  | 469  |       ((j_common_ptr) cinfo, JPOOL_IMAGE,  | 
1543  | 469  |        cinfo->num_components * DCTSIZE2 * SIZEOF(int));  | 
1544  | 469  |     coef_bit_ptr = & cinfo->coef_bits[0][0];  | 
1545  | 1.74k  |     for (ci = 0; ci < cinfo->num_components; ci++)  | 
1546  | 83.2k  |       for (i = 0; i < DCTSIZE2; i++)  | 
1547  | 81.9k  |   *coef_bit_ptr++ = -1;  | 
1548  |  |  | 
1549  |  |     /* Mark derived tables unallocated */  | 
1550  | 2.34k  |     for (i = 0; i < NUM_HUFF_TBLS; i++) { | 
1551  | 1.87k  |       entropy->derived_tbls[i] = NULL;  | 
1552  | 1.87k  |     }  | 
1553  | 469  |   } else { | 
1554  |  |     /* Mark derived tables unallocated */  | 
1555  | 2.03k  |     for (i = 0; i < NUM_HUFF_TBLS; i++) { | 
1556  | 1.62k  |       entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;  | 
1557  | 1.62k  |     }  | 
1558  | 406  |   }  | 
1559  | 875  | }  |