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 "jinclude16.h"
19 #include "jpeglib16.h"
20 #include "jlossy16.h"       /* Private declarations for lossy codec */
21 #include "jdhuff16.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) + (((-1)<<(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 /*
160  * Originally, a -1 was shifted but since shifting a negative value is
161  * undefined behavior, now "~0U" (bit-wise NOT unsigned int 0) is used,
162  * shifted and casted to an int. The result is the same, of course.
163  */
164 static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
165   { 0, (int)((~0U)<<1) + 1, (int)((~0U)<<2) + 1, (int)((~0U)<<3) + 1, (int)((~0U)<<4) + 1,
166     (int)((~0U)<<5) + 1, (int)((~0U)<<6) + 1, (int)((~0U)<<7) + 1, (int)((~0U)<<8) + 1,
167     (int)((~0U)<<9) + 1, (int)((~0U)<<10) + 1, (int)((~0U)<<11) + 1, (int)((~0U)<<12) + 1,
168     (int)((~0U)<<13) + 1, (int)((~0U)<<14) + 1, (int)((~0U)<<15) + 1 };
169 
170 #endif /* AVOID_TABLES */
171 
172 
173 /*
174  * Check for a restart marker & resynchronize decoder.
175  * Returns FALSE if must suspend.
176  */
177 
178 LOCAL(boolean)
process_restart(j_decompress_ptr cinfo)179 process_restart (j_decompress_ptr cinfo)
180 {
181   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
182   shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
183   int ci;
184 
185   /* Throw away any unused bits remaining in bit buffer; */
186   /* include any full bytes in next_marker's count of discarded bytes */
187   cinfo->marker->discarded_bytes += (unsigned int)(entropy->bitstate.bits_left / 8);
188   entropy->bitstate.bits_left = 0;
189 
190   /* Advance past the RSTn marker */
191   if (! (*cinfo->marker->read_restart_marker) (cinfo))
192     return FALSE;
193 
194   /* Re-initialize DC predictions to 0 */
195   for (ci = 0; ci < cinfo->comps_in_scan; ci++)
196     entropy->saved.last_dc_val[ci] = 0;
197 
198   /* Reset restart counter */
199   entropy->restarts_to_go = cinfo->restart_interval;
200 
201   /* Reset out-of-data flag, unless read_restart_marker left us smack up
202    * against a marker.  In that case we will end up treating the next data
203    * segment as empty, and we can avoid producing bogus output pixels by
204    * leaving the flag set.
205    */
206   if (cinfo->unread_marker == 0)
207     entropy->insufficient_data = FALSE;
208 
209   return TRUE;
210 }
211 
212 
213 /*
214  * Decode and return one MCU's worth of Huffman-compressed coefficients.
215  * The coefficients are reordered from zigzag order into natural array order,
216  * but are not dequantized.
217  *
218  * The i'th block of the MCU is stored into the block pointed to by
219  * MCU_data[i].  WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER.
220  * (Wholesale zeroing is usually a little faster than retail...)
221  *
222  * Returns FALSE if data source requested suspension.  In that case no
223  * changes have been made to permanent state.  (Exception: some output
224  * coefficients may already have been assigned.  This is harmless for
225  * this module, since we'll just re-assign them on the next call.)
226  */
227 
228 METHODDEF(boolean)
decode_mcu(j_decompress_ptr cinfo,JBLOCKROW * MCU_data)229 decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
230 {
231   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
232   shuff_entropy_ptr entropy = (shuff_entropy_ptr) lossyd->entropy_private;
233   int blkn;
234   BITREAD_STATE_VARS;
235   savable_state state;
236   boolean cornell_workaround = (cinfo->workaround_options & WORKAROUND_BUGGY_CORNELL_16BIT_JPEG_ENCODER) != 0;
237 
238   /* Process restart marker if needed; may have to suspend */
239   if (cinfo->restart_interval) {
240     if (entropy->restarts_to_go == 0)
241       if (! process_restart(cinfo))
242     return FALSE;
243   }
244 
245   /* If we've run out of data, just leave the MCU set to zeroes.
246    * This way, we return uniform gray for the remainder of the segment.
247    */
248   if (! entropy->insufficient_data) {
249 
250     /* Load up working state */
251     BITREAD_LOAD_STATE(cinfo,entropy->bitstate);
252     ASSIGN_STATE(state, entropy->saved);
253 
254     /* Outer loop handles each block in the MCU */
255 
256     for (blkn = 0; blkn < cinfo->data_units_in_MCU; blkn++) {
257       JBLOCKROW block = MCU_data[blkn];
258       d_derived_tbl * dctbl = entropy->dc_cur_tbls[blkn];
259       d_derived_tbl * actbl = entropy->ac_cur_tbls[blkn];
260       register int s, k, r;
261 
262       /* Decode a single block's worth of coefficients */
263 
264       /* Section F.2.2.1: decode the DC coefficient difference */
265       HUFF_DECODE(s, br_state, dctbl, return FALSE, label1, cornell_workaround);
266       if (s) {
267     CHECK_BIT_BUFFER(br_state, s, return FALSE);
268     r = GET_BITS(s);
269     s = HUFF_EXTEND(r, s);
270       }
271 
272       if (entropy->dc_needed[blkn]) {
273     /* Convert DC difference to actual value, update last_dc_val */
274     int ci = cinfo->MCU_membership[blkn];
275     s += state.last_dc_val[ci];
276     state.last_dc_val[ci] = s;
277     /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */
278     (*block)[0] = (JCOEF) s;
279       }
280 
281       if (entropy->ac_needed[blkn]) {
282 
283     /* Section F.2.2.2: decode the AC coefficients */
284     /* Since zeroes are skipped, output area must be cleared beforehand */
285     for (k = 1; k < DCTSIZE2; k++) {
286       HUFF_DECODE(s, br_state, actbl, return FALSE, label2, cornell_workaround);
287 
288       r = s >> 4;
289       s &= 15;
290 
291       if (s) {
292         k += r;
293         CHECK_BIT_BUFFER(br_state, s, return FALSE);
294         r = GET_BITS(s);
295         s = HUFF_EXTEND(r, s);
296         /* Output coefficient in natural (dezigzagged) order.
297          * Note: the extra entries in jpeg_natural_order[] will save us
298          * if k >= DCTSIZE2, which could happen if the data is corrupted.
299          */
300         (*block)[jpeg_natural_order[k]] = (JCOEF) s;
301       } else {
302         if (r != 15)
303           break;
304         k += 15;
305       }
306     }
307 
308       } else {
309 
310     /* Section F.2.2.2: decode the AC coefficients */
311     /* In this path we just discard the values */
312     for (k = 1; k < DCTSIZE2; k++) {
313       HUFF_DECODE(s, br_state, actbl, return FALSE, label3, cornell_workaround);
314 
315       r = s >> 4;
316       s &= 15;
317 
318       if (s) {
319         k += r;
320         CHECK_BIT_BUFFER(br_state, s, return FALSE);
321         DROP_BITS(s);
322       } else {
323         if (r != 15)
324           break;
325         k += 15;
326       }
327     }
328 
329       }
330     }
331 
332     /* Completed MCU, so update state */
333     BITREAD_SAVE_STATE(cinfo,entropy->bitstate);
334     ASSIGN_STATE(entropy->saved, state);
335   }
336 
337   /* Account for restart interval (no-op if not using restarts) */
338   entropy->restarts_to_go--;
339 
340   return TRUE;
341 }
342 
343 
344 /*
345  * Module initialization routine for Huffman entropy decoding.
346  */
347 
348 GLOBAL(void)
jinit_shuff_decoder(j_decompress_ptr cinfo)349 jinit_shuff_decoder (j_decompress_ptr cinfo)
350 {
351   j_lossy_d_ptr lossyd = (j_lossy_d_ptr) cinfo->codec;
352   shuff_entropy_ptr entropy;
353   int i;
354 
355   entropy = (shuff_entropy_ptr)
356     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
357                 SIZEOF(shuff_entropy_decoder));
358   lossyd->entropy_private = (void *) entropy;
359   lossyd->entropy_start_pass = start_pass_huff_decoder;
360   lossyd->entropy_decode_mcu = decode_mcu;
361 
362   /* Mark tables unallocated */
363   for (i = 0; i < NUM_HUFF_TBLS; i++) {
364     entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
365   }
366 }
367