3 { This file contains Huffman entropy decoding routines for progressive JPEG.
5 Much of the complexity here has to do with supporting input suspension.
6 If the data source module demands suspension, we want to be able to back
7 up to the start of the current MCU. To do this, we copy state variables
8 into local working storage, and update them back to the permanent
9 storage only upon successful completion of an MCU. }
11 { Original: jdphuff.c ; Copyright (C) 1995-1997, Thomas G. Lane. }
13 interface
15 {$I imjconfig.inc}
17 uses
18 imjmorecfg,
19 imjinclude,
20 imjpeglib,
21 imjdeferr,
22 imjerror,
23 imjutils,
27 {GLOBAL}
30 implementation
32 { Expanded entropy decoder object for progressive Huffman decoding.
34 The savable_state subrecord contains fields that change within an MCU,
35 but must not be updated permanently until we complete the MCU. }
37 type
41 { last DC coef for each component }
45 type
50 { These fields are loaded into local variables at start of each MCU.
51 In case of suspension, we exit WITHOUT updating them. }
56 { These fields are NOT loaded into local working state. }
59 { Pointers to derived tables (these workspaces have image lifespan) }
67 { Forward declarations }
68 {METHODDEF}
72 {METHODDEF}
76 {METHODDEF}
80 {METHODDEF}
85 { Initialize for a Huffman-compressed scan. }
87 {METHODDEF}
89 var
95 var
98 begin
103 { Validate scan parameters }
106 begin
109 end
110 else
111 begin
112 { need not check Ss/Se < 0 since they came from unsigned bytes }
115 { AC scans may have only one component }
120 begin
121 { Successive approximation refinement scan: must have Al = Ah-1. }
127 { Arguably the maximum Al value should be less than 13 for 8-bit precision,
128 but the spec doesn't say so, and we try to be liberal about what we
129 accept. Note: large Al values could result in out-of-range DC
130 coefficients during early scans, leading to bizarre displays due to
131 overflows in the IDCT math. But we won't crash. }
136 { Update progression status, and verify that scan order is legal.
137 Note that inter-scan inconsistencies are treated as warnings
138 not fatal errors ... not clear if this is right way to behave. }
141 begin
144 Nomssi }
146 { AC without prior DC scan }
149 begin
152 else
160 { Select MCU decoding routine }
162 begin
165 else
167 end
168 else
169 begin
172 else
177 begin
179 { Make sure requested tables are present, and compute derived tables.
180 We may build same derived table more than once, but it's not expensive. }
183 begin
190 end
191 else
192 begin
196 { remember the single active table }
199 { Initialize DC predictions to 0 }
203 { Initialize bitread state variables }
208 { Initialize private state variables }
211 { Initialize restart counter }
216 { Figure F.12: extend sign bit.
217 On some machines, a shift and add will be faster than a table lookup. }
219 {$ifdef AVOID_TABLES}
224 {$else}
226 { #define HUFF_EXTEND(x,s)
227 if (x) < extend_test[s] then
228 (x) + extend_offset[s]
229 else
230 (x)}
232 const
237 const
247 { Check for a restart marker & resynchronize decoder.
248 return:=s FALSE if must suspend. }
250 {LOCAL}
252 var
255 begin
258 { Throw away any unused bits remaining in bit buffer; }
259 { include any full bytes in next_marker's count of discarded bytes }
263 { Advance past the RSTn marker }
265 begin
267 exit;
270 { Re-initialize DC predictions to 0 }
273 { Re-init EOB run count, too }
276 { Reset restart counter }
279 { Reset out-of-data flag, unless read_restart_marker left us smack up
280 against a marker. In that case we will end up treating the next data
281 segment as empty, and we can avoid producing bogus output pixels by
282 leaving the flag set. }
290 { Huffman MCU decoding.
291 Each of these routines decodes and returns one MCU's worth of
292 Huffman-compressed coefficients.
293 The coefficients are reordered from zigzag order into natural array order,
294 but are not dequantized.
296 The i'th block of the MCU is stored into the block pointed to by
297 MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
299 We return FALSE if data source requested suspension. In that case no
300 changes have been made to permanent state. (Exception: some output
301 coefficients may already have been assigned. This is harmless for
302 spectral selection, since we'll just re-assign them on the next call.
303 Successive approximation AC refinement has to be more careful, however.) }
306 { MCU decoding for DC initial scan (either spectral selection,
307 or first pass of successive approximation). }
309 {METHODDEF}
312 label
313 label1;
314 var
320 {BITREAD_STATE_VARS;}
328 var
330 begin
334 { Process restart marker if needed; may have to suspend }
336 begin
339 begin
341 exit;
345 { If we've run out of data, just leave the MCU set to zeroes.
346 This way, we return uniform gray for the remainder of the segment. }
349 begin
351 { Load up working state }
352 {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
359 {ASSIGN_STATE(state, entropy^.saved);}
362 { Outer loop handles each block in the MCU }
365 begin
371 { Decode a single block's worth of coefficients }
373 { Section F.2.2.1: decode the DC coefficient difference }
374 {HUFF_DECODE(s, br_state, tbl, return FALSE, label1);}
376 begin
378 begin
380 exit;
385 begin
390 {look := PEEK_BITS(HUFF_LOOKAHEAD);}
396 begin
397 {DROP_BITS(nb);}
401 end
402 else
403 begin
405 label1:
408 begin
410 exit;
417 begin
418 {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
420 begin
422 begin
424 exit;
430 {r := GET_BITS(s);}
434 {s := HUFF_EXTEND(r, s);}
437 else
441 { Convert DC difference to actual value, update last_dc_val }
444 { Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) }
448 { Completed MCU, so update state }
449 {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
455 {ASSIGN_STATE(entropy^.saved, state);}
459 { Account for restart interval (no-op if not using restarts) }
466 { MCU decoding for AC initial scan (either spectral selection,
467 or first pass of successive approximation). }
469 {METHODDEF}
472 label
473 label2;
474 var
481 {BITREAD_STATE_VARS;}
487 var
489 begin
494 { Process restart marker if needed; may have to suspend }
496 begin
499 begin
501 exit;
505 { If we've run out of data, just leave the MCU set to zeroes.
506 This way, we return uniform gray for the remainder of the segment. }
508 begin
510 { Load up working state.
511 We can avoid loading/saving bitread state if in an EOB run. }
515 { There is always only one block per MCU }
519 else
520 begin
521 {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
533 begin
534 {HUFF_DECODE(s, br_state, tbl, return FALSE, label2);}
536 begin
538 begin
540 exit;
545 begin
550 {look := PEEK_BITS(HUFF_LOOKAHEAD);}
556 begin
557 {DROP_BITS(nb);}
561 end
562 else
563 begin
565 label2:
568 begin
570 exit;
579 begin
581 {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
583 begin
585 begin
587 exit;
593 {r := GET_BITS(s);}
597 {s := HUFF_EXTEND(r, s);}
600 else
603 { Scale and output coefficient in natural (dezigzagged) order }
605 end
606 else
607 begin
611 end
612 else
617 {CHECK_BIT_BUFFER(br_state, r, return FALSE);}
619 begin
621 begin
623 exit;
629 {r := GET_BITS(r);}
642 {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
649 { Completed MCU, so update state }
653 { Account for restart interval (no-op if not using restarts) }
660 { MCU decoding for DC successive approximation refinement scan.
661 Note: we assume such scans can be multi-component, although the spec
662 is not very clear on the point. }
664 {METHODDEF}
668 var
673 {BITREAD_STATE_VARS;}
677 begin
681 { Process restart marker if needed; may have to suspend }
683 begin
686 begin
688 exit;
692 { Not worth the cycles to check insufficient_data here,
693 since we will not change the data anyway if we read zeroes. }
695 { Load up working state }
696 {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
703 { Outer loop handles each block in the MCU }
706 begin
709 { Encoded data is simply the next bit of the two's-complement DC value }
710 {CHECK_BIT_BUFFER(br_state, 1, return FALSE);}
712 begin
714 begin
716 exit;
722 {if (GET_BITS(1)) then}
726 { Note: since we use OR, repeating the assignment later is safe }
729 { Completed MCU, so update state }
730 {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
736 { Account for restart interval (no-op if not using restarts) }
743 { MCU decoding for AC successive approximation refinement scan. }
745 {METHODDEF}
748 label
750 var
759 {BITREAD_STATE_VARS;}
767 var
769 var
771 begin
780 { Process restart marker if needed; may have to suspend }
782 begin
785 begin
787 exit;
791 { If we've run out of data, don't modify the MCU. }
793 begin
795 { Load up working state }
796 {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
805 { There is always only one block per MCU }
809 { If we are forced to suspend, we must undo the assignments to any newly
810 nonzero coefficients in the block, because otherwise we'd get confused
811 next time about which coefficients were already nonzero.
812 But we need not undo addition of bits to already-nonzero coefficients;
813 instead, we can test the current bit position to see if we already did it.}
817 { initialize coefficient loop counter to start of band }
821 begin
823 begin
824 {HUFF_DECODE(s, br_state, tbl, goto undoit, label3);}
826 begin
832 begin
837 {look := PEEK_BITS(HUFF_LOOKAHEAD);}
843 begin
844 {DROP_BITS(nb);}
848 end
849 else
850 begin
852 label3:
863 begin
866 {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
868 begin
875 {if (GET_BITS(1)) then}
879 else
881 end
882 else
883 begin
885 begin
888 begin
889 {CHECK_BIT_BUFFER(br_state, r, goto undoit);}
891 begin
898 {r := GET_BITS(r);}
906 { note s := 0 for processing ZRL }
908 { Advance over already-nonzero coefs and r still-zero coefs,
909 appending correction bits to the nonzeroes. A correction bit is 1
910 if the absolute value of the coefficient must be increased. }
912 repeat
915 begin
916 {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
918 begin
925 {if (GET_BITS(1)) then}
928 begin
933 else
937 end
938 else
939 begin
947 begin
949 { Output newly nonzero coefficient }
951 { Remember its position in case we have to suspend }
960 begin
961 { Scan any remaining coefficient positions after the end-of-band
962 (the last newly nonzero coefficient, if any). Append a correction
963 bit to each already-nonzero coefficient. A correction bit is 1
964 if the absolute value of the coefficient must be increased. }
967 begin
970 begin
971 {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
973 begin
980 {if (GET_BITS(1)) then}
983 begin
988 else
995 { Count one block completed in EOB run }
999 { Completed MCU, so update state }
1000 {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
1009 { Account for restart interval (no-op if not using restarts) }
1013 exit;
1015 undoit:
1016 { Re-zero any output coefficients that we made newly nonzero }
1018 begin
1027 { Module initialization routine for progressive Huffman entropy decoding. }
1029 {GLOBAL}
1031 var
1035 begin
1042 { Mark derived tables unallocated }
1044 begin
1048 { Create progression status table }
1055 begin