text stringlengths 0 234 |
|---|
procedure Flush_Bit_Buffer is |
begin |
while Valid_Bits > 0 loop |
Put_Byte (Byte (Bit_Buffer and 16#FF#)); |
Bit_Buffer := Shift_Right (Bit_Buffer, 8); |
Valid_Bits := Integer'Max (0, Valid_Bits - 8); |
end loop; |
Bit_Buffer := 0; |
end Flush_Bit_Buffer; |
-- Bit codes are at most 15 bits for Huffman codes, |
-- or 13 for explicit codes (distance extra bits). |
subtype Code_Size_Type is Integer range 1 .. 15; |
-- Send a value on a given number of bits. |
procedure Put_Code (Code : U32; Code_Size : Code_Size_Type) is |
pragma Inline (Put_Code); |
begin |
-- Put bits from code at the left of existing ones. They might be shifted away |
-- partially on the left side (or even entirely if valid_bits is already = 32). |
Bit_Buffer := Bit_Buffer or Shift_Left (Code, Valid_Bits); |
Valid_Bits := Valid_Bits + Code_Size; |
if Valid_Bits > 32 then |
-- Flush 32 bits to output as 4 bytes |
Put_Byte (Byte (Bit_Buffer and 16#FF#)); |
Put_Byte (Byte (Shift_Right (Bit_Buffer, 8) and 16#FF#)); |
Put_Byte (Byte (Shift_Right (Bit_Buffer, 16) and 16#FF#)); |
Put_Byte (Byte (Shift_Right (Bit_Buffer, 24) and 16#FF#)); |
Valid_Bits := Valid_Bits - 32; |
-- Empty buffer and put on it the rest of the code |
Bit_Buffer := Shift_Right (Code, Code_Size - Valid_Bits); |
end if; |
end Put_Code; |
------------------------------------------------------ |
-- Deflate, post LZ encoding, with Huffman encoding -- |
------------------------------------------------------ |
Invalid : constant := -1; |
subtype Huffman_Code_Range is Integer range Invalid .. Integer'Last; |
type Length_Code_Pair is record |
Bit_Length : Natural; -- Huffman code length, in bits |
Code : Huffman_Code_Range := Invalid; -- The code itself |
end record; |
procedure Invert (Lc : in out Length_Code_Pair) is |
pragma Inline (Invert); |
A : Natural := Lc.Code; |
B : Natural := 0; |
begin |
for I in 1 .. Lc.Bit_Length loop |
B := B * 2 + A mod 2; |
A := A / 2; |
end loop; |
Lc.Code := B; |
end Invert; |
-- The Huffman code set (and therefore the Huffman tree) is completely determined by |
-- the bit length to be used for reaching leaf nodes, thanks to two special |
-- rules (explanation in RFC 1951, section 3.2.2). |
-- |
-- So basically the process is the following: |
-- |
-- (A) Gather statistics (just counts) for the alphabet |
-- (B) Turn these counts into code lengths, by calling Length_limited_Huffman_code_lengths |
-- (C) Build Huffman codes (the bits to be sent) with a call to Prepare_Huffman_codes |
-- |
-- In short: |
-- |
-- data -> (A) -> stats -> (B) -> Huffman codes' bit lengths -> (C) -> Huffman codes |
type Huff_Descriptor is array (Natural range <>) of Length_Code_Pair; |
type Bit_Length_Array is array (Natural range <>) of Natural; |
subtype Alphabet_Lit_Len is Natural range 0 .. 287; |
subtype Bit_Length_Array_Lit_Len is Bit_Length_Array (Alphabet_Lit_Len); |
subtype Alphabet_Dis is Natural range 0 .. 31; |
subtype Bit_Length_Array_Dis is Bit_Length_Array (Alphabet_Dis); |
type Deflate_Huff_Descriptors is record |
-- Tree descriptor for Literal, EOB or Length encoding |
Lit_Len : Huff_Descriptor (0 .. 287); |
-- Tree descriptor for Distance encoding |
Dis : Huff_Descriptor (0 .. 31); |
end record; |
-- NB: Appnote: "Literal codes 286-287 and distance codes 30-31 are never used |
-- but participate in the Huffman construction." |
-- Setting upper bound to 285 for literals leads to invalid codes, sometimes. |
-- Copy bit length vectors into Deflate Huffman descriptors |
function Build_Descriptors |
(Bl_For_Lit_Len : Bit_Length_Array_Lit_Len; |
Bl_For_Dis : Bit_Length_Array_Dis) return Deflate_Huff_Descriptors |
is |
New_D : Deflate_Huff_Descriptors; |
begin |
for I in New_D.Lit_Len'Range loop |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.