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