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. }
24
jdhuff; { Declarations shared with jdhuff.c }
28
procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
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. }
38
savable_state = record
39
EOBRUN : uInt; { remaining EOBs in EOBRUN }
40
last_dc_val : array[00..MAX_COMPS_IN_SCAN-1] of int;
41
{ last DC coef for each component }
46
phuff_entropy_ptr = ^phuff_entropy_decoder;
47
phuff_entropy_decoder = record
48
pub : jpeg_entropy_decoder; { public fields }
50
{ These fields are loaded into local variables at start of each MCU.
51
In case of suspension, we exit WITHOUT updating them. }
53
bitstate : bitread_perm_state; { Bit buffer at start of MCU }
54
saved : savable_state; { Other state at start of MCU }
56
{ These fields are NOT loaded into local working state. }
57
restarts_to_go : uInt; { MCUs left in this restart interval }
59
{ Pointers to derived tables (these workspaces have image lifespan) }
60
derived_tbls : array[0..NUM_HUFF_TBLS-1] of d_derived_tbl_ptr;
62
ac_derived_tbl : d_derived_tbl_ptr; { active table during an AC scan }
67
{ Forward declarations }
69
function decode_mcu_DC_first (cinfo : j_decompress_ptr;
70
var MCU_data : array of JBLOCKROW) : boolean;
73
function decode_mcu_AC_first (cinfo : j_decompress_ptr;
74
var MCU_data : array of JBLOCKROW) : boolean;
77
function decode_mcu_DC_refine (cinfo : j_decompress_ptr;
78
var MCU_data : array of JBLOCKROW) : boolean;
81
function decode_mcu_AC_refine (cinfo : j_decompress_ptr;
82
var MCU_data : array of JBLOCKROW) : boolean;
85
{ Initialize for a Huffman-compressed scan. }
88
procedure start_pass_phuff_decoder (cinfo : j_decompress_ptr); far;
90
entropy : phuff_entropy_ptr;
91
is_DC_band, bad : boolean;
93
coef_bit_ptr : coef_bits_ptr;
94
compptr : jpeg_component_info_ptr;
99
entropy := phuff_entropy_ptr (cinfo^.entropy);
101
is_DC_band := (cinfo^.Ss = 0);
103
{ Validate scan parameters }
107
if (cinfo^.Se <> 0) then
112
{ need not check Ss/Se < 0 since they came from unsigned bytes }
113
if (cinfo^.Ss > cinfo^.Se) or (cinfo^.Se >= DCTSIZE2) then
115
{ AC scans may have only one component }
116
if (cinfo^.comps_in_scan <> 1) then
119
if (cinfo^.Ah <> 0) then
121
{ Successive approximation refinement scan: must have Al = Ah-1. }
122
if (cinfo^.Al <> cinfo^.Ah-1) then
125
if (cinfo^.Al > 13) then { need not check for < 0 }
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. }
134
ERREXIT4(j_common_ptr(cinfo), JERR_BAD_PROGRESSION,
135
cinfo^.Ss, cinfo^.Se, cinfo^.Ah, cinfo^.Al);
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. }
140
for ci := 0 to pred(cinfo^.comps_in_scan) do
142
cindex := cinfo^.cur_comp_info[ci]^.component_index;
143
coef_bit_ptr := coef_bits_ptr(@(cinfo^.coef_bits^[cindex])); {^[0] ???
145
if (not is_DC_band) and (coef_bit_ptr^[0] < 0) then
146
{ AC without prior DC scan }
147
WARNMS2(j_common_ptr(cinfo), JWRN_BOGUS_PROGRESSION, cindex, 0);
148
for coefi := cinfo^.Ss to cinfo^.Se do
150
if (coef_bit_ptr^[coefi] < 0) then
153
expected := coef_bit_ptr^[coefi];
154
if (cinfo^.Ah <> expected) then
155
WARNMS2(j_common_ptr(cinfo), JWRN_BOGUS_PROGRESSION, cindex, coefi);
156
coef_bit_ptr^[coefi] := cinfo^.Al;
160
{ Select MCU decoding routine }
161
if (cinfo^.Ah = 0) then
164
entropy^.pub.decode_mcu := decode_mcu_DC_first
166
entropy^.pub.decode_mcu := decode_mcu_AC_first;
171
entropy^.pub.decode_mcu := decode_mcu_DC_refine
173
entropy^.pub.decode_mcu := decode_mcu_AC_refine;
176
for ci := 0 to pred(cinfo^.comps_in_scan) do
178
compptr := cinfo^.cur_comp_info[ci];
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. }
184
if (cinfo^.Ah = 0) then
185
begin { DC refinement needs no table }
186
tbl := compptr^.dc_tbl_no;
187
jpeg_make_d_derived_tbl(cinfo, TRUE, tbl,
188
entropy^.derived_tbls[tbl]);
193
tbl := compptr^.ac_tbl_no;
194
jpeg_make_d_derived_tbl(cinfo, FALSE, tbl,
195
entropy^.derived_tbls[tbl]);
196
{ remember the single active table }
197
entropy^.ac_derived_tbl := entropy^.derived_tbls[tbl];
199
{ Initialize DC predictions to 0 }
200
entropy^.saved.last_dc_val[ci] := 0;
203
{ Initialize bitread state variables }
204
entropy^.bitstate.bits_left := 0;
205
entropy^.bitstate.get_buffer := 0; { unnecessary, but keeps Purify quiet }
206
entropy^.pub.insufficient_data := FALSE;
208
{ Initialize private state variables }
209
entropy^.saved.EOBRUN := 0;
211
{ Initialize restart counter }
212
entropy^.restarts_to_go := cinfo^.restart_interval;
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}
221
#define HUFF_EXTEND(x,s)
222
((x) < (1shl((s)-1)) ? (x) + (((-1)shl(s)) + 1) : (x))
226
{ #define HUFF_EXTEND(x,s)
227
if (x) < extend_test[s] then
228
(x) + extend_offset[s]
233
extend_test : Array[0..16-1] of int = { entry n is 2**(n-1) }
234
($0000, $0001, $0002, $0004, $0008, $0010, $0020, $0040,
235
$0080, $0100, $0200, $0400, $0800, $1000, $2000, $4000);
238
extend_offset : array[0..16-1] of int = { entry n is (-1 shl n) + 1 }
239
( 0, ((-1) shl 1) + 1, ((-1) shl 2) + 1, ((-1) shl 3) + 1, ((-1) shl 4) + 1,
240
((-1) shl 5) + 1, ((-1) shl 6) + 1, ((-1) shl 7) + 1, ((-1) shl 8) + 1,
241
((-1) shl 9) + 1, ((-1) shl 10) + 1, ((-1) shl 11) + 1, ((-1) shl 12) + 1,
242
((-1) shl 13) + 1, ((-1) shl 14) + 1, ((-1) shl 15) + 1 );
244
{$endif} { AVOID_TABLES }
247
{ Check for a restart marker & resynchronize decoder.
248
return:=s FALSE if must suspend. }
251
function process_restart (cinfo : j_decompress_ptr) : boolean;
253
entropy : phuff_entropy_ptr;
256
entropy := phuff_entropy_ptr (cinfo^.entropy);
258
{ Throw away any unused bits remaining in bit buffer; }
259
{ include any full bytes in next_marker's count of discarded bytes }
260
Inc(cinfo^.marker^.discarded_bytes, entropy^.bitstate.bits_left div 8);
261
entropy^.bitstate.bits_left := 0;
263
{ Advance past the RSTn marker }
264
if (not cinfo^.marker^.read_restart_marker (cinfo)) then
266
process_restart := FALSE;
270
{ Re-initialize DC predictions to 0 }
271
for ci := 0 to pred(cinfo^.comps_in_scan) do
272
entropy^.saved.last_dc_val[ci] := 0;
273
{ Re-init EOB run count, too }
274
entropy^.saved.EOBRUN := 0;
276
{ Reset restart counter }
277
entropy^.restarts_to_go := cinfo^.restart_interval;
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. }
283
if (cinfo^.unread_marker = 0) then
284
entropy^.pub.insufficient_data := FALSE;
286
process_restart := TRUE;
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). }
310
function decode_mcu_DC_first (cinfo : j_decompress_ptr;
311
var MCU_data : array of JBLOCKROW) : boolean;
315
entropy : phuff_entropy_ptr;
317
{register} s, r : int;
320
{BITREAD_STATE_VARS;}
321
get_buffer : bit_buf_type ; {register}
322
bits_left : int; {register}
323
br_state : bitread_working_state;
325
state : savable_state;
326
tbl : d_derived_tbl_ptr;
327
compptr : jpeg_component_info_ptr;
329
nb, look : int; {register}
331
entropy := phuff_entropy_ptr (cinfo^.entropy);
334
{ Process restart marker if needed; may have to suspend }
335
if (cinfo^.restart_interval <> 0) then
337
if (entropy^.restarts_to_go = 0) then
338
if (not process_restart(cinfo)) then
340
decode_mcu_DC_first := FALSE;
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. }
348
if not entropy^.pub.insufficient_data then
351
{ Load up working state }
352
{BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
353
br_state.cinfo := cinfo;
354
br_state.next_input_byte := cinfo^.src^.next_input_byte;
355
br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
356
get_buffer := entropy^.bitstate.get_buffer;
357
bits_left := entropy^.bitstate.bits_left;
359
{ASSIGN_STATE(state, entropy^.saved);}
360
state := entropy^.saved;
362
{ Outer loop handles each block in the MCU }
364
for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
366
block := JBLOCK_PTR(MCU_data[blkn]);
367
ci := cinfo^.MCU_membership[blkn];
368
compptr := cinfo^.cur_comp_info[ci];
369
tbl := entropy^.derived_tbls[compptr^.dc_tbl_no];
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);}
375
if (bits_left < HUFF_LOOKAHEAD) then
377
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
379
decode_mcu_DC_first := FALSE;
382
get_buffer := br_state.get_buffer;
383
bits_left := br_state.bits_left;
384
if (bits_left < HUFF_LOOKAHEAD) then
390
{look := PEEK_BITS(HUFF_LOOKAHEAD);}
391
look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
392
pred(1 shl HUFF_LOOKAHEAD);
394
nb := tbl^.look_nbits[look];
400
s := tbl^.look_sym[look];
404
nb := HUFF_LOOKAHEAD+1;
406
s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
409
decode_mcu_DC_first := FALSE;
412
get_buffer := br_state.get_buffer;
413
bits_left := br_state.bits_left;
418
{CHECK_BIT_BUFFER(br_state, s, return FALSE);}
419
if (bits_left < s) then
421
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
423
decode_mcu_DC_first := FALSE;
426
get_buffer := br_state.get_buffer;
427
bits_left := br_state.bits_left;
432
r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
434
{s := HUFF_EXTEND(r, s);}
435
if (r < extend_test[s]) then
436
s := r + extend_offset[s]
441
{ Convert DC difference to actual value, update last_dc_val }
442
Inc(s, state.last_dc_val[ci]);
443
state.last_dc_val[ci] := s;
444
{ Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) }
445
block^[0] := JCOEF (s shl Al);
448
{ Completed MCU, so update state }
449
{BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
450
cinfo^.src^.next_input_byte := br_state.next_input_byte;
451
cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
452
entropy^.bitstate.get_buffer := get_buffer;
453
entropy^.bitstate.bits_left := bits_left;
455
{ASSIGN_STATE(entropy^.saved, state);}
456
entropy^.saved := state;
459
{ Account for restart interval (no-op if not using restarts) }
460
Dec(entropy^.restarts_to_go);
462
decode_mcu_DC_first := TRUE;
466
{ MCU decoding for AC initial scan (either spectral selection,
467
or first pass of successive approximation). }
470
function decode_mcu_AC_first (cinfo : j_decompress_ptr;
471
var MCU_data : array of JBLOCKROW) : boolean;
475
entropy : phuff_entropy_ptr;
478
{register} s, k, r : int;
481
{BITREAD_STATE_VARS;}
482
get_buffer : bit_buf_type ; {register}
483
bits_left : int; {register}
484
br_state : bitread_working_state;
486
tbl : d_derived_tbl_ptr;
488
nb, look : int; {register}
490
entropy := phuff_entropy_ptr (cinfo^.entropy);
494
{ Process restart marker if needed; may have to suspend }
495
if (cinfo^.restart_interval <> 0) then
497
if (entropy^.restarts_to_go = 0) then
498
if (not process_restart(cinfo)) then
500
decode_mcu_AC_first := FALSE;
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. }
507
if not entropy^.pub.insufficient_data then
510
{ Load up working state.
511
We can avoid loading/saving bitread state if in an EOB run. }
513
EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }
515
{ There is always only one block per MCU }
517
if (EOBRUN > 0) then { if it's a band of zeroes... }
518
Dec(EOBRUN) { ...process it now (we do nothing) }
521
{BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
522
br_state.cinfo := cinfo;
523
br_state.next_input_byte := cinfo^.src^.next_input_byte;
524
br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
525
get_buffer := entropy^.bitstate.get_buffer;
526
bits_left := entropy^.bitstate.bits_left;
528
block := JBLOCK_PTR(MCU_data[0]);
529
tbl := entropy^.ac_derived_tbl;
534
{HUFF_DECODE(s, br_state, tbl, return FALSE, label2);}
535
if (bits_left < HUFF_LOOKAHEAD) then
537
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
539
decode_mcu_AC_first := FALSE;
542
get_buffer := br_state.get_buffer;
543
bits_left := br_state.bits_left;
544
if (bits_left < HUFF_LOOKAHEAD) then
550
{look := PEEK_BITS(HUFF_LOOKAHEAD);}
551
look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
552
pred(1 shl HUFF_LOOKAHEAD);
554
nb := tbl^.look_nbits[look];
560
s := tbl^.look_sym[look];
564
nb := HUFF_LOOKAHEAD+1;
566
s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
569
decode_mcu_AC_first := FALSE;
572
get_buffer := br_state.get_buffer;
573
bits_left := br_state.bits_left;
581
{CHECK_BIT_BUFFER(br_state, s, return FALSE);}
582
if (bits_left < s) then
584
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
586
decode_mcu_AC_first := FALSE;
589
get_buffer := br_state.get_buffer;
590
bits_left := br_state.bits_left;
595
r := (int(get_buffer shr bits_left)) and ( pred(1 shl s) );
597
{s := HUFF_EXTEND(r, s);}
598
if (r < extend_test[s]) then
599
s := r + extend_offset[s]
603
{ Scale and output coefficient in natural (dezigzagged) order }
604
block^[jpeg_natural_order[k]] := JCOEF (s shl Al);
610
Inc(k, 15); { skip 15 zeroes in band }
613
begin { EOBr, run length is 2^r + appended bits }
616
begin { EOBr, r > 0 }
617
{CHECK_BIT_BUFFER(br_state, r, return FALSE);}
618
if (bits_left < r) then
620
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
622
decode_mcu_AC_first := FALSE;
625
get_buffer := br_state.get_buffer;
626
bits_left := br_state.bits_left;
631
r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );
635
Dec(EOBRUN); { this band is processed at this moment }
636
break; { force end-of-band }
642
{BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
643
cinfo^.src^.next_input_byte := br_state.next_input_byte;
644
cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
645
entropy^.bitstate.get_buffer := get_buffer;
646
entropy^.bitstate.bits_left := bits_left;
649
{ Completed MCU, so update state }
650
entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }
653
{ Account for restart interval (no-op if not using restarts) }
654
Dec(entropy^.restarts_to_go);
656
decode_mcu_AC_first := TRUE;
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. }
665
function decode_mcu_DC_refine (cinfo : j_decompress_ptr;
666
var MCU_data : array of JBLOCKROW) : boolean;
669
entropy : phuff_entropy_ptr;
670
p1 : int; { 1 in the bit position being coded }
673
{BITREAD_STATE_VARS;}
674
get_buffer : bit_buf_type ; {register}
675
bits_left : int; {register}
676
br_state : bitread_working_state;
678
entropy := phuff_entropy_ptr (cinfo^.entropy);
679
p1 := 1 shl cinfo^.Al;
681
{ Process restart marker if needed; may have to suspend }
682
if (cinfo^.restart_interval <> 0) then
684
if (entropy^.restarts_to_go = 0) then
685
if (not process_restart(cinfo)) then
687
decode_mcu_DC_refine := FALSE;
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);}
697
br_state.cinfo := cinfo;
698
br_state.next_input_byte := cinfo^.src^.next_input_byte;
699
br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
700
get_buffer := entropy^.bitstate.get_buffer;
701
bits_left := entropy^.bitstate.bits_left;
703
{ Outer loop handles each block in the MCU }
705
for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
707
block := JBLOCK_PTR(MCU_data[blkn]);
709
{ Encoded data is simply the next bit of the two's-complement DC value }
710
{CHECK_BIT_BUFFER(br_state, 1, return FALSE);}
711
if (bits_left < 1) then
713
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
715
decode_mcu_DC_refine := FALSE;
718
get_buffer := br_state.get_buffer;
719
bits_left := br_state.bits_left;
722
{if (GET_BITS(1)) then}
724
if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) ) <> 0 then
725
block^[0] := block^[0] or p1;
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);}
731
cinfo^.src^.next_input_byte := br_state.next_input_byte;
732
cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
733
entropy^.bitstate.get_buffer := get_buffer;
734
entropy^.bitstate.bits_left := bits_left;
736
{ Account for restart interval (no-op if not using restarts) }
737
Dec(entropy^.restarts_to_go);
739
decode_mcu_DC_refine := TRUE;
743
{ MCU decoding for AC successive approximation refinement scan. }
746
function decode_mcu_AC_refine (cinfo : j_decompress_ptr;
747
var MCU_data : array of JBLOCKROW) : boolean;
751
entropy : phuff_entropy_ptr;
753
p1 : int; { 1 in the bit position being coded }
754
m1 : int; { -1 in the bit position being coded }
755
{register} s, k, r : int;
758
thiscoef : JCOEF_PTR;
759
{BITREAD_STATE_VARS;}
760
get_buffer : bit_buf_type ; {register}
761
bits_left : int; {register}
762
br_state : bitread_working_state;
764
tbl : d_derived_tbl_ptr;
766
newnz_pos : array[0..DCTSIZE2-1] of int;
770
nb, look : int; {register}
772
entropy := phuff_entropy_ptr (cinfo^.entropy);
774
p1 := 1 shl cinfo^.Al; { 1 in the bit position being coded }
775
m1 := (-1) shl cinfo^.Al; { -1 in the bit position being coded }
777
{ Process restart marker if needed; may have to suspend }
778
if (cinfo^.restart_interval <> 0) then
780
if (entropy^.restarts_to_go = 0) then
781
if (not process_restart(cinfo)) then
783
decode_mcu_AC_refine := FALSE;
788
{ If we've run out of data, don't modify the MCU. }
789
if not entropy^.pub.insufficient_data then
792
{ Load up working state }
793
{BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
794
br_state.cinfo := cinfo;
795
br_state.next_input_byte := cinfo^.src^.next_input_byte;
796
br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
797
get_buffer := entropy^.bitstate.get_buffer;
798
bits_left := entropy^.bitstate.bits_left;
800
EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }
802
{ There is always only one block per MCU }
803
block := JBLOCK_PTR(MCU_data[0]);
804
tbl := entropy^.ac_derived_tbl;
806
{ If we are forced to suspend, we must undo the assignments to any newly
807
nonzero coefficients in the block, because otherwise we'd get confused
808
next time about which coefficients were already nonzero.
809
But we need not undo addition of bits to already-nonzero coefficients;
810
instead, we can test the current bit position to see if we already did it.}
814
{ initialize coefficient loop counter to start of band }
821
{HUFF_DECODE(s, br_state, tbl, goto undoit, label3);}
822
if (bits_left < HUFF_LOOKAHEAD) then
824
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
826
get_buffer := br_state.get_buffer;
827
bits_left := br_state.bits_left;
828
if (bits_left < HUFF_LOOKAHEAD) then
834
{look := PEEK_BITS(HUFF_LOOKAHEAD);}
835
look := int(get_buffer shr (bits_left - HUFF_LOOKAHEAD)) and
836
pred(1 shl HUFF_LOOKAHEAD);
838
nb := tbl^.look_nbits[look];
844
s := tbl^.look_sym[look];
848
nb := HUFF_LOOKAHEAD+1;
850
s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
853
get_buffer := br_state.get_buffer;
854
bits_left := br_state.bits_left;
861
if (s <> 1) then { size of new coef should always be 1 }
862
WARNMS(j_common_ptr(cinfo), JWRN_HUFF_BAD_CODE);
863
{CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
864
if (bits_left < 1) then
866
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
868
get_buffer := br_state.get_buffer;
869
bits_left := br_state.bits_left;
872
{if (GET_BITS(1)) then}
874
if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
875
s := p1 { newly nonzero coef is positive }
877
s := m1; { newly nonzero coef is negative }
883
EOBRUN := 1 shl r; { EOBr, run length is 2^r + appended bits }
886
{CHECK_BIT_BUFFER(br_state, r, goto undoit);}
887
if (bits_left < r) then
889
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
891
get_buffer := br_state.get_buffer;
892
bits_left := br_state.bits_left;
897
r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );
901
break; { rest of block is handled by EOB logic }
903
{ note s := 0 for processing ZRL }
905
{ Advance over already-nonzero coefs and r still-zero coefs,
906
appending correction bits to the nonzeroes. A correction bit is 1
907
if the absolute value of the coefficient must be increased. }
910
thiscoef :=@(block^[jpeg_natural_order[k]]);
911
if (thiscoef^ <> 0) then
913
{CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
914
if (bits_left < 1) then
916
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
918
get_buffer := br_state.get_buffer;
919
bits_left := br_state.bits_left;
922
{if (GET_BITS(1)) then}
924
if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
926
if ((thiscoef^ and p1) = 0) then
927
begin { do nothing if already set it }
928
if (thiscoef^ >= 0) then
939
break; { reached target zero coefficient }
945
pos := jpeg_natural_order[k];
946
{ Output newly nonzero coefficient }
947
block^[pos] := JCOEF (s);
948
{ Remember its position in case we have to suspend }
949
newnz_pos[num_newnz] := pos;
958
{ Scan any remaining coefficient positions after the end-of-band
959
(the last newly nonzero coefficient, if any). Append a correction
960
bit to each already-nonzero coefficient. A correction bit is 1
961
if the absolute value of the coefficient must be increased. }
965
thiscoef := @(block^[jpeg_natural_order[k]]);
966
if (thiscoef^ <> 0) then
968
{CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
969
if (bits_left < 1) then
971
if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
973
get_buffer := br_state.get_buffer;
974
bits_left := br_state.bits_left;
977
{if (GET_BITS(1)) then}
979
if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
981
if ((thiscoef^ and p1) = 0) then
982
begin { do nothing if already changed it }
983
if (thiscoef^ >= 0) then
992
{ Count one block completed in EOB run }
996
{ Completed MCU, so update state }
997
{BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
998
cinfo^.src^.next_input_byte := br_state.next_input_byte;
999
cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
1000
entropy^.bitstate.get_buffer := get_buffer;
1001
entropy^.bitstate.bits_left := bits_left;
1003
entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }
1006
{ Account for restart interval (no-op if not using restarts) }
1007
Dec(entropy^.restarts_to_go);
1009
decode_mcu_AC_refine := TRUE;
1013
{ Re-zero any output coefficients that we made newly nonzero }
1014
while (num_newnz > 0) do
1017
block^[newnz_pos[num_newnz]] := 0;
1020
decode_mcu_AC_refine := FALSE;
1024
{ Module initialization routine for progressive Huffman entropy decoding. }
1027
procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
1029
entropy : phuff_entropy_ptr;
1030
coef_bit_ptr : int_ptr;
1033
entropy := phuff_entropy_ptr(
1034
cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
1035
SIZEOF(phuff_entropy_decoder)) );
1036
cinfo^.entropy := jpeg_entropy_decoder_ptr (entropy);
1037
entropy^.pub.start_pass := start_pass_phuff_decoder;
1039
{ Mark derived tables unallocated }
1040
for i := 0 to pred(NUM_HUFF_TBLS) do
1042
entropy^.derived_tbls[i] := NIL;
1045
{ Create progression status table }
1046
cinfo^.coef_bits := coef_bits_ptrrow (
1047
cinfo^.mem^.alloc_small ( j_common_ptr (cinfo), JPOOL_IMAGE,
1048
cinfo^.num_components*DCTSIZE2*SIZEOF(int)) );
1049
coef_bit_ptr := @cinfo^.coef_bits^[0][0];
1050
for ci := 0 to pred(cinfo^.num_components) do
1051
for i := 0 to pred(DCTSIZE2) do
1053
coef_bit_ptr^ := -1;
b'\\ No newline at end of file'