3
{ This file contains Huffman entropy encoding routines for progressive JPEG.
5
We do not support output suspension in this module, since the library
6
currently does not allow multiple-scan files to be written with output
9
{ Original: jcphuff.c; Copyright (C) 1995-1997, Thomas G. Lane. }
22
jchuff; { Declarations shared with jchuff.c }
25
{ Module initialization routine for progressive Huffman entropy encoding. }
28
procedure jinit_phuff_encoder (cinfo : j_compress_ptr);
32
{ Expanded entropy encoder object for progressive Huffman encoding. }
34
phuff_entropy_ptr = ^phuff_entropy_encoder;
35
phuff_entropy_encoder = record
36
pub : jpeg_entropy_encoder; { public fields }
38
{ Mode flag: TRUE for optimization, FALSE for actual data output }
39
gather_statistics : boolean;
41
{ Bit-level coding status.
42
next_output_byte/free_in_buffer are local copies of cinfo^.dest fields.}
44
next_output_byte : JOCTETptr; { => next byte to write in buffer }
45
free_in_buffer : size_t; { # of byte spaces remaining in buffer }
46
put_buffer : INT32; { current bit-accumulation buffer }
47
put_bits : int; { # of bits now in it }
48
cinfo : j_compress_ptr; { link to cinfo (needed for dump_buffer) }
50
{ Coding status for DC components }
51
last_dc_val : array[0..MAX_COMPS_IN_SCAN-1] of int;
52
{ last DC coef for each component }
54
{ Coding status for AC components }
55
ac_tbl_no : int; { the table number of the single component }
56
EOBRUN : uInt; { run length of EOBs }
57
BE : uInt; { # of buffered correction bits before MCU }
58
bit_buffer : JBytePtr; { buffer for correction bits (1 per char) }
59
{ packing correction bits tightly would save some space but cost time... }
61
restarts_to_go : uInt; { MCUs left in this restart interval }
62
next_restart_num : int; { next restart number to write (0-7) }
64
{ Pointers to derived tables (these workspaces have image lifespan).
65
Since any one scan codes only DC or only AC, we only need one set
66
of tables, not one for DC and one for AC. }
68
derived_tbls : array[0..NUM_HUFF_TBLS-1] of c_derived_tbl_ptr;
70
{ Statistics tables for optimization; again, one set is enough }
71
count_ptrs : array[0..NUM_HUFF_TBLS-1] of TLongTablePtr;
75
{ MAX_CORR_BITS is the number of bits the AC refinement correction-bit
76
buffer can hold. Larger sizes may slightly improve compression, but
77
1000 is already well into the realm of overkill.
78
The minimum safe size is 64 bits. }
81
MAX_CORR_BITS = 1000; { Max # of correction bits I can buffer }
84
{ Forward declarations }
86
function encode_mcu_DC_first (cinfo : j_compress_ptr;
87
const MCU_data: array of JBLOCKROW) : boolean;
90
function encode_mcu_AC_first (cinfo : j_compress_ptr;
91
const MCU_data: array of JBLOCKROW) : boolean;
94
function encode_mcu_DC_refine (cinfo : j_compress_ptr;
95
const MCU_data: array of JBLOCKROW) : boolean;
98
function encode_mcu_AC_refine (cinfo : j_compress_ptr;
99
const MCU_data: array of JBLOCKROW) : boolean;
103
procedure finish_pass_phuff (cinfo : j_compress_ptr); far; forward;
106
procedure finish_pass_gather_phuff (cinfo : j_compress_ptr); far; forward;
109
{ Initialize for a Huffman-compressed scan using progressive JPEG. }
112
procedure start_pass_phuff (cinfo : j_compress_ptr;
113
gather_statistics : boolean); far;
115
entropy : phuff_entropy_ptr;
116
is_DC_band : boolean;
118
compptr : jpeg_component_info_ptr;
120
entropy := phuff_entropy_ptr (cinfo^.entropy);
122
entropy^.cinfo := cinfo;
123
entropy^.gather_statistics := gather_statistics;
125
is_DC_band := (cinfo^.Ss = 0);
127
{ We assume jcmaster.c already validated the scan parameters. }
129
{ Select execution routines }
130
if (cinfo^.Ah = 0) then
133
entropy^.pub.encode_mcu := encode_mcu_DC_first
135
entropy^.pub.encode_mcu := encode_mcu_AC_first;
140
entropy^.pub.encode_mcu := encode_mcu_DC_refine
143
entropy^.pub.encode_mcu := encode_mcu_AC_refine;
144
{ AC refinement needs a correction bit buffer }
145
if (entropy^.bit_buffer = NIL) then
146
entropy^.bit_buffer := JBytePtr(
147
cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
148
MAX_CORR_BITS * SIZEOF(byte)) );
151
if (gather_statistics) then
152
entropy^.pub.finish_pass := finish_pass_gather_phuff
154
entropy^.pub.finish_pass := finish_pass_phuff;
156
{ Only DC coefficients may be interleaved, so cinfo^.comps_in_scan = 1
157
for AC coefficients. }
159
for ci := 0 to pred(cinfo^.comps_in_scan) do
161
compptr := cinfo^.cur_comp_info[ci];
162
{ Initialize DC predictions to 0 }
163
entropy^.last_dc_val[ci] := 0;
167
if (cinfo^.Ah <> 0) then { DC refinement needs no table }
169
tbl := compptr^.dc_tbl_no;
173
tbl := compptr^.ac_tbl_no;
174
entropy^.ac_tbl_no := tbl;
176
if (gather_statistics) then
178
{ Check for invalid table index }
179
{ (make_c_derived_tbl does this in the other path) }
180
if (tbl < 0) or (tbl >= NUM_HUFF_TBLS) then
181
ERREXIT1(j_common_ptr(cinfo), JERR_NO_HUFF_TABLE, tbl);
182
{ Allocate and zero the statistics tables }
183
{ Note that jpeg_gen_optimal_table expects 257 entries in each table! }
184
if (entropy^.count_ptrs[tbl] = NIL) then
185
entropy^.count_ptrs[tbl] := TLongTablePtr(
186
cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
187
257 * SIZEOF(long)) );
188
MEMZERO(entropy^.count_ptrs[tbl], 257 * SIZEOF(long));
191
{ Compute derived values for Huffman table }
192
{ We may do this more than once for a table, but it's not expensive }
193
jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
194
entropy^.derived_tbls[tbl]);
198
{ Initialize AC stuff }
199
entropy^.EOBRUN := 0;
202
{ Initialize bit buffer to empty }
203
entropy^.put_buffer := 0;
204
entropy^.put_bits := 0;
206
{ Initialize restart stuff }
207
entropy^.restarts_to_go := cinfo^.restart_interval;
208
entropy^.next_restart_num := 0;
215
procedure dump_buffer (entropy : phuff_entropy_ptr);
216
{ Empty the output buffer; we do not support suspension in this module. }
218
dest : jpeg_destination_mgr_ptr;
220
dest := entropy^.cinfo^.dest;
222
if (not dest^.empty_output_buffer (entropy^.cinfo)) then
223
ERREXIT(j_common_ptr(entropy^.cinfo), JERR_CANT_SUSPEND);
224
{ After a successful buffer dump, must reset buffer pointers }
225
entropy^.next_output_byte := dest^.next_output_byte;
226
entropy^.free_in_buffer := dest^.free_in_buffer;
230
{ Outputting bits to the file }
232
{ Only the right 24 bits of put_buffer are used; the valid bits are
233
left-justified in this part. At most 16 bits can be passed to emit_bits
234
in one call, and we never retain more than 7 bits in put_buffer
235
between calls, so 24 bits are sufficient. }
239
procedure emit_bits (entropy : phuff_entropy_ptr;
241
size : int); {INLINE}
242
{ Emit some bits, unless we are in gather mode }
244
{register} put_buffer : INT32;
245
{register} put_bits : int;
249
{ This routine is heavily used, so it's worth coding tightly. }
250
put_buffer := INT32 (code);
251
put_bits := entropy^.put_bits;
253
{ if size is 0, caller used an invalid Huffman table entry }
255
ERREXIT(j_common_ptr(entropy^.cinfo), JERR_HUFF_MISSING_CODE);
257
if (entropy^.gather_statistics) then
258
exit; { do nothing if we're only getting stats }
260
put_buffer := put_buffer and ((INT32(1) shl size) - 1);
261
{ mask off any extra bits in code }
263
Inc(put_bits, size); { new number of bits in buffer }
265
put_buffer := put_buffer shl (24 - put_bits); { align incoming bits }
267
put_buffer := put_buffer or entropy^.put_buffer;
268
{ and merge with old buffer contents }
270
while (put_bits >= 8) do
272
c := int ((put_buffer shr 16) and $FF);
274
{emit_byte(entropy, c);}
275
{ Outputting bytes to the file.
276
NB: these must be called only when actually outputting,
277
that is, entropy^.gather_statistics = FALSE. }
279
entropy^.next_output_byte^ := JOCTET(c);
280
Inc(entropy^.next_output_byte);
281
Dec(entropy^.free_in_buffer);
282
if (entropy^.free_in_buffer = 0) then
283
dump_buffer(entropy);
286
begin { need to stuff a zero byte? }
287
{emit_byte(entropy, 0);}
288
entropy^.next_output_byte^ := JOCTET(0);
289
Inc(entropy^.next_output_byte);
290
Dec(entropy^.free_in_buffer);
291
if (entropy^.free_in_buffer = 0) then
292
dump_buffer(entropy);
294
put_buffer := put_buffer shl 8;
298
entropy^.put_buffer := put_buffer; { update variables }
299
entropy^.put_bits := put_bits;
304
procedure flush_bits (entropy : phuff_entropy_ptr);
306
emit_bits(entropy, $7F, 7); { fill any partial byte with ones }
307
entropy^.put_buffer := 0; { and reset bit-buffer to empty }
308
entropy^.put_bits := 0;
311
{ Emit (or just count) a Huffman symbol. }
315
procedure emit_symbol (entropy : phuff_entropy_ptr;
317
symbol : int); {INLINE}
319
tbl : c_derived_tbl_ptr;
321
if (entropy^.gather_statistics) then
322
Inc(entropy^.count_ptrs[tbl_no]^[symbol])
325
tbl := entropy^.derived_tbls[tbl_no];
326
emit_bits(entropy, tbl^.ehufco[symbol], tbl^.ehufsi[symbol]);
331
{ Emit bits from a correction bit buffer. }
334
procedure emit_buffered_bits (entropy : phuff_entropy_ptr;
340
if (entropy^.gather_statistics) then
341
exit; { no real work }
343
bufptr := byteptr(bufstart);
346
emit_bits(entropy, uInt(bufptr^), 1);
353
{ Emit any pending EOBRUN symbol. }
356
procedure emit_eobrun (entropy : phuff_entropy_ptr);
358
{register} temp, nbits : int;
360
if (entropy^.EOBRUN > 0) then
361
begin { if there is any pending EOBRUN }
362
temp := entropy^.EOBRUN;
371
{ safety check: shouldn't happen given limited correction-bit buffer }
373
ERREXIT(j_common_ptr(entropy^.cinfo), JERR_HUFF_MISSING_CODE);
375
emit_symbol(entropy, entropy^.ac_tbl_no, nbits shl 4);
377
emit_bits(entropy, entropy^.EOBRUN, nbits);
379
entropy^.EOBRUN := 0;
381
{ Emit any buffered correction bits }
382
emit_buffered_bits(entropy, entropy^.bit_buffer, entropy^.BE);
388
{ Emit a restart marker & resynchronize predictions. }
391
procedure emit_restart (entropy : phuff_entropy_ptr;
396
emit_eobrun(entropy);
398
if (not entropy^.gather_statistics) then
401
{emit_byte(entropy, $FF);}
402
{ Outputting bytes to the file.
403
NB: these must be called only when actually outputting,
404
that is, entropy^.gather_statistics = FALSE. }
406
entropy^.next_output_byte^ := JOCTET($FF);
407
Inc(entropy^.next_output_byte);
408
Dec(entropy^.free_in_buffer);
409
if (entropy^.free_in_buffer = 0) then
410
dump_buffer(entropy);
412
{emit_byte(entropy, JPEG_RST0 + restart_num);}
413
entropy^.next_output_byte^ := JOCTET(JPEG_RST0 + restart_num);
414
Inc(entropy^.next_output_byte);
415
Dec(entropy^.free_in_buffer);
416
if (entropy^.free_in_buffer = 0) then
417
dump_buffer(entropy);
420
if (entropy^.cinfo^.Ss = 0) then
422
{ Re-initialize DC predictions to 0 }
423
for ci := 0 to pred(entropy^.cinfo^.comps_in_scan) do
424
entropy^.last_dc_val[ci] := 0;
428
{ Re-initialize all AC-related fields to 0 }
429
entropy^.EOBRUN := 0;
435
{ MCU encoding for DC initial scan (either spectral selection,
436
or first pass of successive approximation). }
439
function encode_mcu_DC_first (cinfo : j_compress_ptr;
440
const MCU_data: array of JBLOCKROW) : boolean;
442
entropy : phuff_entropy_ptr;
443
{register} temp, temp2 : int;
444
{register} nbits : int;
448
compptr : jpeg_component_info_ptr;
451
entropy := phuff_entropy_ptr (cinfo^.entropy);
454
entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
455
entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
457
{ Emit restart marker if needed }
458
if (cinfo^.restart_interval <> 0) then
459
if (entropy^.restarts_to_go = 0) then
460
emit_restart(entropy, entropy^.next_restart_num);
462
{ Encode the MCU data blocks }
463
for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
465
block := JBLOCK_PTR(MCU_data[blkn]);
466
ci := cinfo^.MCU_membership[blkn];
467
compptr := cinfo^.cur_comp_info[ci];
469
{ Compute the DC value after the required point transform by Al.
470
This is simply an arithmetic right shift. }
472
{temp2 := IRIGHT_SHIFT( int(block^[0]), Al);}
473
{IRIGHT_SHIFT_IS_UNSIGNED}
474
ishift_temp := int(block^[0]);
475
if ishift_temp < 0 then
476
temp2 := (ishift_temp shr Al) or ((not 0) shl (16-Al))
478
temp2 := ishift_temp shr Al;
481
{ DC differences are figured on the point-transformed values. }
482
temp := temp2 - entropy^.last_dc_val[ci];
483
entropy^.last_dc_val[ci] := temp2;
485
{ Encode the DC coefficient difference per section G.1.2.1 }
489
temp := -temp; { temp is abs value of input }
490
{ For a negative input, want temp2 := bitwise complement of abs(input) }
491
{ This code assumes we are on a two's complement machine }
495
{ Find the number of bits needed for the magnitude of the coefficient }
503
{ Check for out-of-range coefficient values.
504
Since we're encoding a difference, the range limit is twice as much. }
506
if (nbits > MAX_COEF_BITS+1) then
507
ERREXIT(j_common_ptr(cinfo), JERR_BAD_DCT_COEF);
509
{ Count/emit the Huffman-coded symbol for the number of bits }
510
emit_symbol(entropy, compptr^.dc_tbl_no, nbits);
512
{ Emit that number of bits of the value, if positive, }
513
{ or the complement of its magnitude, if negative. }
514
if (nbits <> 0) then { emit_bits rejects calls with size 0 }
515
emit_bits(entropy, uInt(temp2), nbits);
518
cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
519
cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
521
{ Update restart-interval state too }
522
if (cinfo^.restart_interval <> 0) then
524
if (entropy^.restarts_to_go = 0) then
526
entropy^.restarts_to_go := cinfo^.restart_interval;
527
Inc(entropy^.next_restart_num);
529
next_restart_num := next_restart_num and 7;
531
Dec(entropy^.restarts_to_go);
534
encode_mcu_DC_first := TRUE;
538
{ MCU encoding for AC initial scan (either spectral selection,
539
or first pass of successive approximation). }
542
function encode_mcu_AC_first (cinfo : j_compress_ptr;
543
const MCU_data: array of JBLOCKROW) : boolean;
545
entropy : phuff_entropy_ptr;
546
{register} temp, temp2 : int;
547
{register} nbits : int;
548
{register} r, k : int;
553
entropy := phuff_entropy_ptr (cinfo^.entropy);
557
entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
558
entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
560
{ Emit restart marker if needed }
561
if (cinfo^.restart_interval <> 0) then
562
if (entropy^.restarts_to_go = 0) then
563
emit_restart(entropy, entropy^.next_restart_num);
565
{ Encode the MCU data block }
566
block := JBLOCK_PTR(MCU_data[0]);
568
{ Encode the AC coefficients per section G.1.2.2, fig. G.3 }
570
r := 0; { r := run length of zeros }
572
for k := cinfo^.Ss to Se do
574
temp := (block^[jpeg_natural_order[k]]);
580
{ We must apply the point transform by Al. For AC coefficients this
581
is an integer division with rounding towards 0. To do this portably
582
in C, we shift after obtaining the absolute value; so the code is
583
interwoven with finding the abs value (temp) and output bits (temp2). }
587
temp := -temp; { temp is abs value of input }
588
temp := temp shr Al; { apply the point transform }
589
{ For a negative coef, want temp2 := bitwise complement of abs(coef) }
594
temp := temp shr Al; { apply the point transform }
597
{ Watch out for case that nonzero coef is zero after point transform }
604
{ Emit any pending EOBRUN }
605
if (entropy^.EOBRUN > 0) then
606
emit_eobrun(entropy);
607
{ if run length > 15, must emit special run-length-16 codes ($F0) }
610
emit_symbol(entropy, entropy^.ac_tbl_no, $F0);
614
{ Find the number of bits needed for the magnitude of the coefficient }
615
nbits := 0; { there must be at least one 1 bit }
621
{ Check for out-of-range coefficient values }
622
if (nbits > MAX_COEF_BITS) then
623
ERREXIT(j_common_ptr(cinfo), JERR_BAD_DCT_COEF);
625
{ Count/emit Huffman symbol for run length / number of bits }
626
emit_symbol(entropy, entropy^.ac_tbl_no, (r shl 4) + nbits);
628
{ Emit that number of bits of the value, if positive, }
629
{ or the complement of its magnitude, if negative. }
630
emit_bits(entropy, uInt(temp2), nbits);
632
r := 0; { reset zero run length }
636
begin { If there are trailing zeroes, }
637
Inc(entropy^.EOBRUN); { count an EOB }
638
if (entropy^.EOBRUN = $7FFF) then
639
emit_eobrun(entropy); { force it out to avoid overflow }
642
cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
643
cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
645
{ Update restart-interval state too }
646
if (cinfo^.restart_interval <> 0) then
648
if (entropy^.restarts_to_go = 0) then
650
entropy^.restarts_to_go := cinfo^.restart_interval;
651
Inc(entropy^.next_restart_num);
653
next_restart_num := next_restart_num and 7;
655
Dec(entropy^.restarts_to_go);
658
encode_mcu_AC_first := TRUE;
662
{ MCU encoding for DC successive approximation refinement scan.
663
Note: we assume such scans can be multi-component, although the spec
664
is not very clear on the point. }
667
function encode_mcu_DC_refine (cinfo : j_compress_ptr;
668
const MCU_data: array of JBLOCKROW) : boolean;
670
entropy : phuff_entropy_ptr;
671
{register} temp : int;
676
entropy := phuff_entropy_ptr (cinfo^.entropy);
679
entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
680
entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
682
{ Emit restart marker if needed }
683
if (cinfo^.restart_interval <> 0) then
684
if (entropy^.restarts_to_go = 0) then
685
emit_restart(entropy, entropy^.next_restart_num);
687
{ Encode the MCU data blocks }
688
for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
690
block := JBLOCK_PTR(MCU_data[blkn]);
692
{ We simply emit the Al'th bit of the DC coefficient value. }
694
emit_bits(entropy, uInt(temp shr Al), 1);
697
cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
698
cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
700
{ Update restart-interval state too }
701
if (cinfo^.restart_interval <> 0) then
703
if (entropy^.restarts_to_go = 0) then
705
entropy^.restarts_to_go := cinfo^.restart_interval;
706
Inc(entropy^.next_restart_num);
708
next_restart_num := next_restart_num and 7;
710
Dec(entropy^.restarts_to_go);
713
encode_mcu_DC_refine := TRUE;
717
{ MCU encoding for AC successive approximation refinement scan. }
720
function encode_mcu_AC_refine (cinfo : j_compress_ptr;
721
const MCU_data: array of JBLOCKROW) : boolean;
724
entropy : phuff_entropy_ptr;
725
{register} temp : int;
726
{register} r, k : int;
728
BR_buffer : JBytePtr;
733
absvalues : array[0..DCTSIZE2-1] of int;
735
entropy := phuff_entropy_ptr(cinfo^.entropy);
739
entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
740
entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
742
{ Emit restart marker if needed }
743
if (cinfo^.restart_interval <> 0) then
744
if (entropy^.restarts_to_go = 0) then
745
emit_restart(entropy, entropy^.next_restart_num);
747
{ Encode the MCU data block }
748
block := JBLOCK_PTR(MCU_data[0]);
750
{ It is convenient to make a pre-pass to determine the transformed
751
coefficients' absolute values and the EOB position. }
754
for k := cinfo^.Ss to Se do
756
temp := block^[jpeg_natural_order[k]];
757
{ We must apply the point transform by Al. For AC coefficients this
758
is an integer division with rounding towards 0. To do this portably
759
in C, we shift after obtaining the absolute value. }
762
temp := -temp; { temp is abs value of input }
763
temp := temp shr Al; { apply the point transform }
764
absvalues[k] := temp; { save abs value for main pass }
766
EOB := k; { EOB := index of last newly-nonzero coef }
769
{ Encode the AC coefficients per section G.1.2.3, fig. G.7 }
771
r := 0; { r := run length of zeros }
772
BR := 0; { BR := count of buffered bits added now }
773
BR_buffer := JBytePtr(@(entropy^.bit_buffer^[entropy^.BE]));
774
{ Append bits to buffer }
776
for k := cinfo^.Ss to Se do
778
temp := absvalues[k];
785
{ Emit any required ZRLs, but not if they can be folded into EOB }
786
while (r > 15) and (k <= EOB) do
788
{ emit any pending EOBRUN and the BE correction bits }
789
emit_eobrun(entropy);
791
emit_symbol(entropy, entropy^.ac_tbl_no, $F0);
793
{ Emit buffered correction bits that must be associated with ZRL }
794
emit_buffered_bits(entropy, BR_buffer, BR);
795
BR_buffer := entropy^.bit_buffer; { BE bits are gone now }
799
{ If the coef was previously nonzero, it only needs a correction bit.
800
NOTE: a straight translation of the spec's figure G.7 would suggest
801
that we also need to test r > 15. But if r > 15, we can only get here
802
if k > EOB, which implies that this coefficient is not 1. }
805
{ The correction bit is the next bit of the absolute value. }
806
BR_buffer^[BR] := byte (temp and 1);
811
{ Emit any pending EOBRUN and the BE correction bits }
812
emit_eobrun(entropy);
814
{ Count/emit Huffman symbol for run length / number of bits }
815
emit_symbol(entropy, entropy^.ac_tbl_no, (r shl 4) + 1);
817
{ Emit output bit for newly-nonzero coef }
818
if (block^[jpeg_natural_order[k]] < 0) then
822
emit_bits(entropy, uInt(temp), 1);
824
{ Emit buffered correction bits that must be associated with this code }
825
emit_buffered_bits(entropy, BR_buffer, BR);
826
BR_buffer := entropy^.bit_buffer; { BE bits are gone now }
828
r := 0; { reset zero run length }
831
if (r > 0) or (BR > 0) then
832
begin { If there are trailing zeroes, }
833
Inc(entropy^.EOBRUN); { count an EOB }
834
Inc(entropy^.BE, BR); { concat my correction bits to older ones }
835
{ We force out the EOB if we risk either:
836
1. overflow of the EOB counter;
837
2. overflow of the correction bit buffer during the next MCU. }
839
if (entropy^.EOBRUN = $7FFF) or
840
(entropy^.BE > (MAX_CORR_BITS-DCTSIZE2+1)) then
841
emit_eobrun(entropy);
844
cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
845
cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
847
{ Update restart-interval state too }
848
if (cinfo^.restart_interval <> 0) then
850
if (entropy^.restarts_to_go = 0) then
852
entropy^.restarts_to_go := cinfo^.restart_interval;
853
Inc(entropy^.next_restart_num);
855
next_restart_num := next_restart_num and 7;
857
Dec(entropy^.restarts_to_go);
860
encode_mcu_AC_refine := TRUE;
864
{ Finish up at the end of a Huffman-compressed progressive scan. }
867
procedure finish_pass_phuff (cinfo : j_compress_ptr);
869
entropy : phuff_entropy_ptr;
871
entropy := phuff_entropy_ptr (cinfo^.entropy);
873
entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
874
entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;
876
{ Flush out any buffered data }
877
emit_eobrun(entropy);
880
cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
881
cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
885
{ Finish up a statistics-gathering pass and create the new Huffman tables. }
888
procedure finish_pass_gather_phuff (cinfo : j_compress_ptr);
890
entropy : phuff_entropy_ptr;
891
is_DC_band : boolean;
893
compptr : jpeg_component_info_ptr;
894
htblptr : ^JHUFF_TBL_PTR;
895
did : array[0..NUM_HUFF_TBLS-1] of boolean;
897
entropy := phuff_entropy_ptr (cinfo^.entropy);
899
{ Flush out buffered data (all we care about is counting the EOB symbol) }
900
emit_eobrun(entropy);
902
is_DC_band := (cinfo^.Ss = 0);
904
{ It's important not to apply jpeg_gen_optimal_table more than once
905
per table, because it clobbers the input frequency counts! }
907
MEMZERO(@did, SIZEOF(did));
909
for ci := 0 to pred(cinfo^.comps_in_scan) do
911
compptr := cinfo^.cur_comp_info[ci];
914
if (cinfo^.Ah <> 0) then { DC refinement needs no table }
916
tbl := compptr^.dc_tbl_no;
920
tbl := compptr^.ac_tbl_no;
922
if (not did[tbl]) then
925
htblptr := @(cinfo^.dc_huff_tbl_ptrs[tbl])
927
htblptr := @(cinfo^.ac_huff_tbl_ptrs[tbl]);
928
if (htblptr^ = NIL) then
929
htblptr^ := jpeg_alloc_huff_table(j_common_ptr(cinfo));
930
jpeg_gen_optimal_table(cinfo, htblptr^, entropy^.count_ptrs[tbl]^);
937
{ Module initialization routine for progressive Huffman entropy encoding. }
940
procedure jinit_phuff_encoder (cinfo : j_compress_ptr);
942
entropy : phuff_entropy_ptr;
945
entropy := phuff_entropy_ptr(
946
cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
947
SIZEOF(phuff_entropy_encoder)) );
948
cinfo^.entropy := jpeg_entropy_encoder_ptr(entropy);
949
entropy^.pub.start_pass := start_pass_phuff;
951
{ Mark tables unallocated }
952
for i := 0 to pred(NUM_HUFF_TBLS) do
954
entropy^.derived_tbls[i] := NIL;
955
entropy^.count_ptrs[i] := NIL;
957
entropy^.bit_buffer := NIL; { needed only in AC refinement scan }