text stringlengths 0 234 |
|---|
-- Step 3) |
for N in Hd'Range loop |
Bl := Hd (N).Bit_Length; |
if Bl > 0 then |
Hd (N).Code := Next_Code (Bl); |
Next_Code (Bl) := Next_Code (Bl) + 1; |
else |
Hd (N).Code := 0; |
end if; |
end loop; |
-- Invert bit order for output: |
for I in Hd'Range loop |
Invert (Hd (I)); |
end loop; |
end Prepare_Huffman_Codes; |
-- This is the phase (C) for the pair of alphabets used in the Deflate format |
function Prepare_Huffman_Codes |
(Dhd : Deflate_Huff_Descriptors) return Deflate_Huff_Descriptors |
is |
Dhd_Var : Deflate_Huff_Descriptors := Dhd; |
begin |
Prepare_Huffman_Codes (Dhd_Var.Lit_Len); |
Prepare_Huffman_Codes (Dhd_Var.Dis); |
return Dhd_Var; |
end Prepare_Huffman_Codes; |
-- Emit a variable length Huffman code |
procedure Put_Huffman_Code (Lc : Length_Code_Pair) is |
pragma Inline (Put_Huffman_Code); |
begin |
-- Huffman code of length 0 should never occur: when constructing |
-- the code lengths (LLHCL) any single occurrence in the statistics |
-- will trigger the build of a code length of 1 or more. |
Put_Code |
(Code => U32 (Lc.Code), |
Code_Size => Code_Size_Type (Lc.Bit_Length) -- Range check for length 0 (if enabled). |
); |
end Put_Huffman_Code; |
-- This is where the "dynamic" Huffman trees are sent before the block's data are sent. |
-- |
-- The decoder needs to know in advance the pair of trees (first tree for literals-eob-LZ |
-- lengths, second tree for LZ distances) for decoding the compressed data. |
-- But this information takes some room. Fortunately Deflate allows for compressing it |
-- with a combination of Huffman and Run-Length Encoding (RLE) to make this header smaller. |
-- Concretely, the trees are described by the bit length of each symbol, so the header's |
-- content is a vector of length max 320, whose contents are in the 0 .. 18 range and typically |
-- look like: ... 8, 8, 9, 7, 8, 10, 6, 8, 8, 8, 8, 8, 11, 8, 9, 8, ... |
-- Clearly this vector has redundancies and can be sent in a compressed form. In this example, |
-- the RLE will compress the string of 8's with a single code 8, then a code 17 |
-- (repeat x times). Anyway, the very frequent 8's will be encoded with a small number of |
-- bits (less than the 5 plain bits, or maximum 7 Huffman-encoded bits |
-- needed for encoding integers in the 0 .. 18 range). |
procedure Put_Compression_Structure |
(Dhd : Deflate_Huff_Descriptors; |
Cost_Analysis : Boolean; -- If True: just simulate the whole, and count needed bits |
Bits : in out Count_Type) -- This is incremented when cost_analysis = True |
is |
subtype Alphabet is Integer range 0 .. 18; |
type Alpha_Array is new Bit_Length_Array (Alphabet); |
Truc_Freq, Truc_Bl : Alpha_Array; |
Truc : Huff_Descriptor (Alphabet); |
-- Compression structure: cs_bl is the "big" array with all bit lengths |
-- for compressing data. cs_bl will be sent compressed, too. |
Cs_Bl : array (1 .. Dhd.Lit_Len'Length + Dhd.Dis'Length) of Natural; |
Last_Cs_Bl : Natural; |
Max_Used_Lln_Code : Alphabet_Lit_Len := 0; |
Max_Used_Dis_Code : Alphabet_Dis := 0; |
procedure Concatenate_All_Bit_Lengths is |
Idx : Natural := 0; |
begin |
for A in reverse Alphabet_Lit_Len loop |
if Dhd.Lit_Len (A).Bit_Length > 0 then |
Max_Used_Lln_Code := A; |
exit; |
end if; |
end loop; |
for A in reverse Alphabet_Dis loop |
if Dhd.Dis (A).Bit_Length > 0 then |
Max_Used_Dis_Code := A; |
exit; |
end if; |
end loop; |
-- Copy bit lengths for both trees into one array, cs_bl. |
for A in 0 .. Max_Used_Lln_Code loop |
Idx := Idx + 1; |
Cs_Bl (Idx) := Dhd.Lit_Len (A).Bit_Length; |
end loop; |
for A in 0 .. Max_Used_Dis_Code loop |
Idx := Idx + 1; |
Cs_Bl (Idx) := Dhd.Dis (A).Bit_Length; |
end loop; |
Last_Cs_Bl := Idx; |
end Concatenate_All_Bit_Lengths; |
Extra_Bits_Needed : constant array (Alphabet) of Natural := |
(16 => 2, 17 => 3, 18 => 7, others => 0); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.