1 /*
2  * jdshuff.c
3  *
4  * Copyright (C) 1991-1998, Thomas G. Lane.
5  * This file is part of the Independent JPEG Group's software.
6  * For conditions of distribution and use, see the accompanying README file.
7  *
8  * This file contains Huffman entropy decoding routines for sequential JPEG.
9  *
10  * Much of the complexity here has to do with supporting input suspension.
11  * If the data source module demands suspension, we want to be able to back
12  * up to the start of the current MCU.  To do this, we copy state variables
13  * into local working storage, and update them back to the permanent
14  * storage only upon successful completion of an MCU.
15  */
16 
17 #define JPEG_INTERNALS
18 #include "jinclude.h"
19 #include "jpeglib.h"
20 #include "jlossy.h"    /* Private declarations for lossy codec */
21 #include "jdhuff.h"    /* Declarations shared with jd*huff.c */
22 
23 
24 /*
25  * Private entropy decoder object for Huffman decoding.
26  *
27  * The savable_state subrecord contains fields that change within an MCU,
28  * but must not be updated permanently until we complete the MCU.
29  */
30 
31 typedef struct {
32   int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
33 } savable_state;
34 
35 /* This macro is to work around compilers with missing or broken
36  * structure assignment.  You'll need to fix this code if you have
37  * such a compiler and you change MAX_COMPS_IN_SCAN.
38  */
39 
40 #ifndef NO_STRUCT_ASSIGN
41 #define ASSIGN_STATE(dest,src)  ((dest) = (src))
42 #else
43 #if MAX_COMPS_IN_SCAN == 4
44 #define ASSIGN_STATE(dest,src)  \
45   ((dest).last_dc_val[0] = (src).last_dc_val[0], \
46    (dest).last_dc_val[1] = (src).last_dc_val[1], \
47    (dest).last_dc_val[2] = (src).last_dc_val[2], \
48    (dest).last_dc_val[3] = (src).last_dc_val[3])
49 #endif
50 #endif
51 
52 
53 typedef struct {
54   huffd_common_fields;    /* Fields shared with other entropy decoders */
55 
56   /* These fields are loaded into local variables at start of each MCU.
57    * In case of suspension, we exit WITHOUT updating them.
58    */
59   savable_state saved;    /* Other state at start of MCU */
60 
61   /* These fields are NOT loaded into local working state. */
62   unsigned int restarts_to_go;  /* MCUs left in this restart interval */
63 
64   /* Pointers to derived tables (these workspaces have image lifespan) */
65   d_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
66   d_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
67 
68   /* Precalculated info set up by start_pass for use in decode_mcu: */
69 
70   /* Pointers to derived tables to be used for each block within an MCU */
71   d_derived_tbl * dc_cur_tbls[D_MAX_DATA_UNITS_IN_MCU];
72   d_derived_tbl * ac_cur_tbls[D_MAX_DATA_UNITS_IN_MCU];
73   /* Whether we care about the DC and AC coefficient values for each block */
74   boolean dc_needed[D_MAX_DATA_UNITS_IN_MCU];
75   boolean ac_needed[D_MAX_DATA_UNITS_IN_MCU];
76 } shuff_entropy_decoder;
77 
78 typedef shuff_entropy_decoder * shuff_entropy_ptr;
79 
80 
81 /*
82  * Initialize for a Huffman-compressed scan.
83  */
84 
85 METHODDEF(void)
start_pass_huff_decoder(j_decompress_ptr cinfo)86 start_pass_huff_decoder (j_decompress_ptr cinfo)
87 {
88   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
89   shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
90   int ci, blkn, dctbl, actbl;
91   jpeg_component_info * compptr;
92 
93   /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
94    * This ought to be an error condition, but we make it a warning because
95    * there are some baseline files out there with all zeroes in these bytes.
96    */
97   if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
98       cinfo->Ah != 0 || cinfo->Al != 0)
99     WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
100 
101   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
102     compptr = cinfo->cur_comp_info[ci];
103     dctbl = compptr->dc_tbl_no;
104     actbl = compptr->ac_tbl_no;
105     /* Compute derived values for Huffman tables */
106     /* We may do this more than once for a table, but it's not expensive */
107     jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl,
108           & entropy->dc_derived_tbls[dctbl]);
109     jpeg_make_d_derived_tbl(cinfo, FALSE, actbl,
110           & entropy->ac_derived_tbls[actbl]);
111     /* Initialize DC predictions to 0 */
112     entropy->saved.last_dc_val[ci] = 0;
113   }
114 
115   /* Precalculate decoding info for each block in an MCU of this scan */
116   for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
117     ci = cinfo->MCU_membership[blkn];
118     compptr = cinfo->cur_comp_info[ci];
119     /* Precalculate which table to use for each block */
120     entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no];
121     entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no];
122     /* Decide whether we really care about the coefficient values */
123     if (compptr->component_needed) {
124       entropy->dc_needed[blkn] = TRUE;
125       /* we don't need the ACs if producing a 1/8th-size image */
126       entropy->ac_needed[blkn] = (compptr->codec_data_unit > 1);
127     } else {
128       entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE;
129     }
130   }
131 
132   /* Initialize bitread state variables */
133   entropy->bitstate.bits_left = 0;
134   entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */
135   entropy->insufficient_data = FALSE;
136 
137   /* Initialize restart counter */
138   entropy->restarts_to_go = cinfo->restart_interval;
139 }
140 
141 
142 /*
143  * Figure F.12: extend sign bit.
144  * On some machines, a shift and add will be faster than a table lookup.
145  */
146 
147 #ifdef AVOID_TABLES
148 
149 #define HUFF_EXTEND(x,s)  ((x) < (1<<((s)-1)) ? (x) + (((-1u)<<(s)) + 1) : (x))
150 
151 #else
152 
153 #define HUFF_EXTEND(x,s)  ((x) < extend_test[s] ? (x) + extend_offset[s] : (x))
154 
155 static const int extend_test[16] =   /* entry n is 2**(n-1) */
156   { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
157     0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
158 
159 static const int extend_offset[16] = /* entry n is (-1u << n) + 1 */
160   { 0, ((-1u)<<1) + 1, ((-1u)<<2) + 1, ((-1u)<<3) + 1, ((-1u)<<4) + 1,
161     ((-1u)<<5) + 1, ((-1u)<<6) + 1, ((-1u)<<7) + 1, ((-1u)<<8) + 1,
162     ((-1u)<<9) + 1, ((-1u)<<10) + 1, ((-1u)<<11) + 1, ((-1u)<<12) + 1,
163     ((-1u)<<13) + 1, ((-1u)<<14) + 1, ((-1u)<<15) + 1 };
164 
165 #endif /* AVOID_TABLES */
166 
167 
168 /*
169  * Check for a restart marker & resynchronize decoder.
170  * Returns FALSE if must suspend.
171  */
172 
173 LOCAL(boolean)
process_restart(j_decompress_ptr cinfo)174 process_restart (j_decompress_ptr cinfo)
175 {
176   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
177   shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
178   int ci;
179 
180   /* Throw away any unused bits remaining in bit buffer; */
181   /* include any full bytes in next_marker's count of discarded bytes */
182   cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8;
183   entropy->bitstate.bits_left = 0;
184 
185   /* Advance past the RSTn marker */
186   if (! (*cinfo->marker->read_restart_marker) (cinfo))
187     return FALSE;
188 
189   /* Re-initialize DC predictions to 0 */
190   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
191     entropy->saved.last_dc_val[ci] = 0;
192 
193   /* Reset restart counter */
194   entropy->restarts_to_go = cinfo->restart_interval;
195 
196   /* Reset out-of-data flag, unless read_restart_marker left us smack up
197    * against a marker.  In that case we will end up treating the next data
198    * segment as empty, and we can avoid producing bogus output pixels by
199    * leaving the flag set.
200    */
201   if (cinfo->unread_marker == 0)
202     entropy->insufficient_data = FALSE;
203 
204   return TRUE;
205 }
206 
207 
208 /*
209  * Decode and return one MCU's worth of Huffman-compressed coefficients.
210  * The coefficients are reordered from zigzag order into natural array order,
211  * but are not dequantized.
212  *
213  * The i'th block of the MCU is stored into the block pointed to by
214  * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
215  * (Wholesale zeroing is usually a little faster than retail...)
216  *
217  * Returns FALSE if data source requested suspension.  In that case no
218  * changes have been made to permanent state.  (Exception: some output
219  * coefficients may already have been assigned.  This is harmless for
220  * this module, since we'll just re-assign them on the next call.)
221  */
222 
223 METHODDEF(boolean)
decode_mcu(j_decompress_ptr cinfo,JBLOCKROW * MCU_data)224 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
225 {
226   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
227   shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
228   int blkn;
229   BITREAD_STATE_VARS;
230   savable_state state;
231 
232   /* Process restart marker if needed; may have to suspend */
233   if (cinfo->restart_interval) {
234     if (entropy->restarts_to_go == 0)
235       if (! process_restart(cinfo))
236   return FALSE;
237   }
238 
239   /* If we've run out of data, just leave the MCU set to zeroes.
240    * This way, we return uniform gray for the remainder of the segment.
241    */
242   if (! entropy->insufficient_data) {
243 
244     /* Load up working state */
245     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
246     ASSIGN_STATE(state, entropy->saved);
247 
248     /* Outer loop handles each block in the MCU */
249 
250     for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
251       JBLOCKROW block = MCU_data[blkn];
252       d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
253       d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
254       register int s, k, r;
255 
256       /* Decode a single block's worth of coefficients */
257 
258       /* Section F.2.2.1: decode the DC coefficient difference */
259       HUFF_DECODE(s, br_state, dctbl, return FALSE, label1);
260       if (s) {
261   CHECK_BIT_BUFFER(br_state, s, return FALSE);
262   r = GET_BITS(s);
263   s = HUFF_EXTEND(r, s);
264       }
265 
266       if (entropy->dc_needed[blkn]) {
267   /* Convert DC difference to actual value, update last_dc_val */
268   int ci = cinfo->MCU_membership[blkn];
269   s += state.last_dc_val[ci];
270   state.last_dc_val[ci] = s;
271   /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
272   (*block)[0] = (JCOEF) s;
273       }
274 
275       if (entropy->ac_needed[blkn]) {
276 
277   /* Section F.2.2.2: decode the AC coefficients */
278   /* Since zeroes are skipped, output area must be cleared beforehand */
279   for (k = 1; k < DCTSIZE2; k++) {
280     HUFF_DECODE(s, br_state, actbl, return FALSE, label2);
281 
282     r = s >> 4;
283     s &= 15;
284 
285     if (s) {
286       k += r;
287       CHECK_BIT_BUFFER(br_state, s, return FALSE);
288       r = GET_BITS(s);
289       s = HUFF_EXTEND(r, s);
290       /* Output coefficient in natural (dezigzagged) order.
291        * Note: the extra entries in jpeg_natural_order[] will save us
292        * if k >= DCTSIZE2, which could happen if the data is corrupted.
293        */
294       (*block)[jpeg_natural_order[k]] = (JCOEF) s;
295     } else {
296       if (r != 15)
297         break;
298       k += 15;
299     }
300   }
301 
302       } else {
303 
304   /* Section F.2.2.2: decode the AC coefficients */
305   /* In this path we just discard the values */
306   for (k = 1; k < DCTSIZE2; k++) {
307     HUFF_DECODE(s, br_state, actbl, return FALSE, label3);
308 
309     r = s >> 4;
310     s &= 15;
311 
312     if (s) {
313       k += r;
314       CHECK_BIT_BUFFER(br_state, s, return FALSE);
315       DROP_BITS(s);
316     } else {
317       if (r != 15)
318         break;
319       k += 15;
320     }
321   }
322 
323       }
324     }
325 
326     /* Completed MCU, so update state */
327     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
328     ASSIGN_STATE(entropy->saved, state);
329   }
330 
331   /* Account for restart interval (no-op if not using restarts) */
332   entropy->restarts_to_go--;
333 
334   return TRUE;
335 }
336 
337 
338 /*
339  * Module initialization routine for Huffman entropy decoding.
340  */
341 
342 GLOBAL(void)
jinit_shuff_decoder(j_decompress_ptr cinfo)343 jinit_shuff_decoder (j_decompress_ptr cinfo)
344 {
345   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
346   shuff_entropy_ptr entropy;
347   int i;
348 
349   entropy = (shuff_entropy_ptr)
350     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
351         SIZEOF(shuff_entropy_decoder));
352   lossyd->entropy_private = (void *) entropy;
353   lossyd->entropy_start_pass = start_pass_huff_decoder;
354   lossyd->entropy_decode_mcu = decode_mcu;
355 
356   /* Mark tables unallocated */
357   for (i = 0; i < NUM_HUFF_TBLS; i++) {
358     entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
359   }
360 }
361