text
stringlengths
0
234
-- We count bits taken by literals, for each block format variant
for I in 0 .. 255 loop
C := Stats_Lit_Len (I); -- This literal appears c times in the LZ buffer
Stored_Format_Bits := Stored_Format_Bits + 8 * C;
Fixed_Format_Bits := Fixed_Format_Bits + Count_Type (Default_Lit_Len_Bl (I)) * C;
Dynamic_Format_Bits :=
Dynamic_Format_Bits + Count_Type (New_Descr.Lit_Len (I).Bit_Length) * C;
Dynamic_Format_Bits_2 :=
Dynamic_Format_Bits_2 + Count_Type (New_Descr_2.Lit_Len (I).Bit_Length) * C;
Recycled_Format_Bits :=
Recycled_Format_Bits + Count_Type (Curr_Descr.Lit_Len (I).Bit_Length) * C;
end loop;
-- We count bits taken by DL codes
if Stored_Format_Possible then
for I in Lzb'Range loop
case Lzb (I).Kind is
when Plain_Byte =>
null; -- Already counted
when Distance_Length =>
-- In the stored format, DL codes are expanded
Stored_Format_Bits := Stored_Format_Bits + 8 * Count_Type (Lzb (I).Lz_Length);
end case;
end loop;
end if;
-- For compressed formats, count Huffman bits and extra bits
for I in 257 .. 285 loop
C := Stats_Lit_Len (I); -- This length code appears c times in the LZ buffer
Extra := Extra_Bits_For_Lz_Length_Code (I);
Fixed_Format_Bits :=
Fixed_Format_Bits + Count_Type (Default_Lit_Len_Bl (I) + Extra) * C;
Dynamic_Format_Bits :=
Dynamic_Format_Bits + Count_Type (New_Descr.Lit_Len (I).Bit_Length + Extra) * C;
Dynamic_Format_Bits_2 :=
Dynamic_Format_Bits_2 + Count_Type (New_Descr_2.Lit_Len (I).Bit_Length + Extra) * C;
Recycled_Format_Bits :=
Recycled_Format_Bits + Count_Type (Curr_Descr.Lit_Len (I).Bit_Length + Extra) * C;
end loop;
for I in 0 .. 29 loop
C := Stats_Dis (I); -- This distance code appears c times in the LZ buffer
Extra := Extra_Bits_For_Lz_Distance_Code (I);
Fixed_Format_Bits := Fixed_Format_Bits + Count_Type (Default_Dis_Bl (I) + Extra) * C;
Dynamic_Format_Bits :=
Dynamic_Format_Bits + Count_Type (New_Descr.Dis (I).Bit_Length + Extra) * C;
Dynamic_Format_Bits_2 :=
Dynamic_Format_Bits_2 + Count_Type (New_Descr_2.Dis (I).Bit_Length + Extra) * C;
Recycled_Format_Bits :=
Recycled_Format_Bits + Count_Type (Curr_Descr.Dis (I).Bit_Length + Extra) * C;
end loop;
-- Supplemental bits to be counted
Stored_Format_Bits :=
Stored_Format_Bits +
(1 + (Stored_Format_Bits / 8) / 65_535) -- Number of stored blocks needed
* 5 -- 5 bytes per header
* 8; -- ... converted into bits
C := 1; -- Is-last-block flag
if Block_To_Finish and Last_Block_Type in Fixed .. Dynamic then
C := C + Count_Type (Curr_Descr.Lit_Len (End_Of_Block).Bit_Length);
end if;
Stored_Format_Bits := Stored_Format_Bits + C;
Fixed_Format_Bits := Fixed_Format_Bits + C + 2;
Dynamic_Format_Bits := Dynamic_Format_Bits + C + 2;
Dynamic_Format_Bits_2 := Dynamic_Format_Bits_2 + C + 2;
-- For both dynamic formats, we also counts the bits taken by
-- the compression header!
Put_Compression_Structure (New_Descr, Cost_Analysis => True, Bits => Dynamic_Format_Bits);
Put_Compression_Structure
(New_Descr_2,
Cost_Analysis => True,
Bits => Dynamic_Format_Bits_2);
end Compute_Sizes_Of_Variants;
Optimal_Format_Bits : Count_Type;
begin
Get_Statistics (Lzb, Stats_Lit_Len, Stats_Dis);
New_Descr := Build_Descriptors (Stats_Lit_Len, Stats_Dis);
Stats_Lit_Len_2 := Stats_Lit_Len;
Stats_Dis_2 := Stats_Dis;
Tweak_For_Better_Rle (Stats_Lit_Len_2);
Tweak_For_Better_Rle (Stats_Dis_2);
New_Descr_2 := Build_Descriptors (Stats_Lit_Len_2, Stats_Dis_2);
-- For "stored" block format, prevent expansion of DL codes with length > max_expand
-- We check stats are all 0 for long length codes:
Stored_Format_Possible := Stats_Lit_Len (Long_Length_Codes) = Zero_Bl_Long_Lengths;
Recycling_Possible :=
Last_Block_Type = Fixed -- The "fixed" alphabets use all symbols, then always recyclable
or else (Last_Block_Type = Dynamic and then Recyclable (Curr_Descr, New_Descr));
Compute_Sizes_Of_Variants;
if not Stored_Format_Possible then
Stored_Format_Bits := Count_Type'Last;
end if;
if not Recycling_Possible then
Recycled_Format_Bits := Count_Type'Last;
end if;