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