text stringlengths 0 234 |
|---|
New_D.Lit_Len (I) := (Bit_Length => Bl_For_Lit_Len (I), Code => Invalid); |
end loop; |
for I in New_D.Dis'Range loop |
New_D.Dis (I) := (Bit_Length => Bl_For_Dis (I), Code => Invalid); |
end loop; |
return New_D; |
end Build_Descriptors; |
type Count_Type is range 0 .. File_Size_Type'Last / 2 - 1; |
type Stats_Type is array (Natural range <>) of Count_Type; |
-- The following is a translation of Zopfli's OptimizeHuffmanForRle (v. 11-May-2016). |
-- Possible gain: shorten the compression header containing the Huffman trees' bit lengths. |
-- Possible loss: since the stats do not correspond anymore exactly to the data |
-- to be compressed, the Huffman trees might be suboptimal. |
-- |
-- Zopfli comment: |
-- Changes the population counts in a way that the consequent Huffman tree |
-- compression, especially its rle-part, will be more likely to compress this data |
-- more efficiently. |
procedure Tweak_For_Better_Rle (Counts : in out Stats_Type) is |
Length : Integer := Counts'Length; |
Stride : Integer; |
Symbol, Sum, Limit, New_Count : Count_Type; |
Good_For_Rle : array (Counts'Range) of Boolean := (others => False); |
begin |
-- 1) We don't want to touch the trailing zeros. We may break the |
-- rules of the format by adding more data in the distance codes. |
loop |
if Length = 0 then |
return; |
end if; |
exit when Counts (Length - 1) /= 0; |
Length := Length - 1; |
end loop; |
-- Now counts(0..length - 1) does not have trailing zeros. |
-- |
-- 2) Let's mark all population counts that already can be encoded with an rle code. |
-- |
-- Let's not spoil any of the existing good rle codes. |
-- Mark any seq of 0's that is longer than 5 as a good_for_rle. |
-- Mark any seq of non-0's that is longer than 7 as a good_for_rle. |
Symbol := Counts (0); |
Stride := 0; |
for I in 0 .. Length loop |
if I = Length or else Counts (I) /= Symbol then |
if (Symbol = 0 and then Stride >= 5) or else (Symbol /= 0 and then Stride >= 7) then |
for K in 0 .. Stride - 1 loop |
Good_For_Rle (I - K - 1) := True; |
end loop; |
end if; |
Stride := 1; |
if I /= Length then |
Symbol := Counts (I); |
end if; |
else |
Stride := Stride + 1; |
end if; |
end loop; |
-- 3) Let's replace those population counts that lead to more rle codes. |
Stride := 0; |
Limit := Counts (0); |
Sum := 0; |
for I in 0 .. Length loop |
if I = Length |
or else Good_For_Rle (I) |
or else (I > 0 and then Good_For_Rle (I - 1)) -- Added from Brotli, item #1 |
-- Heuristic for selecting the stride ranges to collapse. |
or else abs (Counts (I) - Limit) >= 4 |
then |
if Stride >= 4 or else (Stride >= 3 and then Sum = 0) then |
-- The stride must end, collapse what we have, if we have enough (4). |
-- New_Count is the average of counts on the stride's interval, upper-rounded |
New_Count := |
Count_Type'Max (1, (Sum + Count_Type (Stride) / 2) / Count_Type (Stride)); |
if Sum = 0 then |
-- Don't make an all zeros stride to be upgraded to ones. |
New_Count := 0; |
end if; |
for K in 0 .. Stride - 1 loop |
-- We don't want to change value at counts(i), |
-- that is already belonging to the next stride. Thus - 1. |
-- Replace histogram value by averaged value |
Counts (I - K - 1) := New_Count; |
end loop; |
end if; |
Stride := 0; |
Sum := 0; |
if I < Length - 3 then |
-- All interesting strides have a count of at least 4, at |
-- least when non-zeros. Limit is the average of next 4 |
-- counts, upper-rounded |
Limit := (Counts (I) + Counts (I + 1) + Counts (I + 2) + Counts (I + 3) + 2) / 4; |
elsif I < Length then |
Limit := Counts (I); |
else |
Limit := 0; |
end if; |
end if; |
Stride := Stride + 1; |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.