Commit Graph

191 Commits

Author SHA1 Message Date
Nico Weber
1a9d8e8fbe LibCompress: When limiting huffman tree depth, sacrifice bottom of tree
Deflate and WebP can store at most 15 bits per symbol, meaning their
huffman trees can be at most 15 levels deep.

During construction, when we hit this level, we used to try again
with an ever lower frequency cap per symbol. This had the effect
of giving the symbols with the highest frequency lower frequencies
first, causing the most-frequent symbols to be merged. For example,
maybe the most-frequent symbol had 1 bit, and the 2nd-frequent
two bits (and everything else at least 3). With the cap, the two
most frequent symbols might both have 2 symbols, freeing up bits
for the lower levels of the tree.

This has the effect of making the most-frequent symbols longer at
first, which isn't great for file size.

Instead of using a frequency cap, ignore ever more of the low
bits of the frequency. This sacrifices resolution where it hurts
the lower levels of the tree first, and those are stored less
frequently.

For deflate, the 64 kiB block size means this doesn't have a big
effect, but for WebP it can have a big effect:

sunset-retro.png (876K): 2.02M -> 1.73M -- now (very slightly) smaller
than twice the input size! Maybe we'll be competitive one day.

(For wow.webp and 7z7c.webp, it has no effect, since we don't hit
the "tree too deep" case there, since those have relatively few
colors.)

No behavior change other than smaller file size. (No performance
cost either, and it's less code too.)
2024-05-26 21:00:55 +02:00
Nico Weber
2023e8d8d9 LibCompress: Use saturating add in generate_huffman_lengths()
For our deflate, block size is limited to less than 64 kiB, so the sum
of all byte frequencies always fits in a u16 by construction.

But while I haven't hit this in practice, but it can conceivably happen
when writing WebP files, which currently use a single huffman tree
(per channel) for a while image -- which is often much larger than
64 kiB.

No dramatic behavior change in practice, just feels more correct.
2024-05-26 21:00:55 +02:00
Nico Weber
a01fdca2de LibCompress: Use named EndOfBlock constant
No behavior change.
2024-05-26 19:02:49 +02:00
Nico Weber
ff6d58f321 LibCompress: Pass ReadonlyBytes to encode_huffman_lengths()
...instead of Array and length. No behavior change.
2024-05-26 19:02:49 +02:00
Nico Weber
5c81b4b269 LibCompress: Make encode_block_lengths() a bit less clever
No behavior change.
2024-05-26 19:02:49 +02:00
Nico Weber
d95e4831be LibCompress: Move generate_huffman_lengths() to a .h file
To be used in WebPWriter.

JPEGWriter currently hardcodes huffman tables; maybe it can use this
to build data-dependent huffman tables in the future as well.

Pure code move (except for removing the `DeflateCompressor::` prefix
on the function's name, and putting the default argument for the 4th
argument in the function's definition), no behavior change.
2024-05-26 19:02:49 +02:00
Nico Weber
d294f150ed LibGfx+LibCompress: WebPWriter performance regression reduction
This moves both Gfx::CanonicalCode::write_symbol() and
Compress::CanonicalCode::write_symbol() inline.

It also adds `__attribute__((always_inline))` on the arguments
to visit() in the latter. (ALWAYS_INLINE doesn't work on lambdas.)

Numbers with `ministat`: I ran once:

    Build/lagom/bin/image -o test.bmp Base/res/wallpapers/sunset-retro.png

and then ran to bench:

    ~/src/hack/bench.py -n 20 -o bench_foo1.txt \
        Build/lagom/bin/image -o test.webp test.bmp

...and then `ministat bench_foo1.txt bench_foo2.txt` to compare.

The previous commit increased the time for this command by 38% compared
to the before state.

With this, it's an 8.6% regression. So still a regression, but a smaller
one.

Or, in other words, this commit reduces times by 21% compared to the
previous commit.

Numbers with hyperfine are similar -- with this on top of the previous
commit, this is a 7-11% regression, instead of an almost 50% regression.

(A local branch that changes how we compute CanonicalCodes so that we
actually compress a bit is perf-neutral since the image writing code
doesn't change.)

`hyperfine 'image -o test.webp test.bmp'`:
* Before:          23.7 ms ± 0.7 ms (116 runs)
* Previous commit: 33.2 ms ± 0.8 ms (82 runs)
* This commit:     25.5 ms ± 0.7 ms (102 runs)

`hyperfine 'animation -o wow.webp giphy.gif'`:
* Before:           85.5 ms ± 2.0 ms (34 runs)
* Previous commit: 127.7 ms ± 4.4 ms (22 runs)
* This commit:      95.3 ms ± 2.1 ms (31 runs)

`hyperfine 'animation -o wow.webp 7z7c.gif'`:
* Before:          12.6 ms ± 0.6 ms (198 runs)
* Previous commit: 16.5 ms ± 0.9 ms (153 runs)
* This commit:     13.5 ms ± 0.6 ms (186 runs)
2024-05-20 13:17:34 -04:00
Liav A.
f474d0c316 LibCompress: Remove Gzip compress_file & decompress_file methods
The gzip utility was the last user of these methods, but now is using
the more comfortable Stream mechanism so we can just remove this code.
2024-05-14 12:35:25 -06:00
Lucas CHOLLET
54f33b43c6 LibCompress: Add an LZW compressor 2024-05-14 12:33:53 -06:00
Lucas CHOLLET
ff33fa7e8b LibCompress/Lzw: Name a magic variable 2024-05-14 12:33:53 -06:00
Lucas CHOLLET
945e09e46d LibCompress/Lzw: Move shareable code to a base class 2024-05-14 12:33:53 -06:00
Lucas CHOLLET
70a3f1f02b LibCompress: Rename LZWDecoder => LzwDecompressor
This is more idiomatic for LibCompress' decoders.
2024-05-14 12:33:53 -06:00
Lucas CHOLLET
11f53ba9cc LibCompress: Rename LZWDecoder.h => Lzw.h 2024-05-14 12:33:53 -06:00
Dan Klishch
5ed7cd6e32 Everywhere: Use east const in more places
These changes are compatible with clang-format 16 and will be mandatory
when we eventually bump clang-format version. So, since there are no
real downsides, let's commit them now.
2024-04-19 06:31:19 -04:00
Filiph Siitam Sandström
fd694e8672 AK+Lagom: Make it possible to build for iOS
This commit makes it possible to build AK and most of Lagom for iOS,
based on the work for the Ladybird build demoed on discord:
https://discord.com/channels/830522505605283862/830525031720943627/1211987732646068314
2024-03-03 13:13:42 -07:00
Lucas CHOLLET
a3d48319fe LibCompress/Deflate: Remove three useless FIXMEs 2024-01-14 21:22:35 +01:00
Lucas CHOLLET
830e6472e6 LibCompress/Deflate: Simplify DeflateDecompressor::decompress_all() 2024-01-14 21:22:35 +01:00
Ali Mohammad Pur
57ea3e160a LibCompress: Use a __FILE__-relative path for the brotli dictionary file
This makes it so the generated assembly doesn't depend on
compile-specific search paths, also making it possible to build with
LTO.
2024-01-10 09:40:51 +01:00
Tim Schumacher
707a36dd79 LibCompress/Brotli: Update the lookback buffer with uncompressed data
We previously skipped updating the lookback buffer when copying
uncompressed data, which resulted in a wrong total byte count.
With a wrong total byte count, our decompressor implementation
ended up choosing a wrong offset into the dictionary.
2024-01-03 17:54:36 +01:00
Lucas CHOLLET
d748edd994 LibCompress: Add a PackBits decoder
This compression scheme was quite popular during the 80's, and we can
still find it in use inside file formats such as TIFF or PDF.
2023-12-27 17:40:11 +01:00
Tim Schumacher
a1cf2708ee LibCompress: Implement the XZ BCJ filter for ARM64 2023-12-14 08:59:23 -07:00
Idan Horowitz
b749167506 LibCompress: Fix off-by-one error in generate_huffman_lengths
Previously we would calculate the index of the first parent node as
heap.size() (which is initialized to non_zero_freqs), so in the edge
case in which all symbols had a non-zero frequency, we would use the
Size-index entry in the array for both the first symbol's leaf node,
and the first parent node.

The result would either be a non-optimal huffman code (bad), or an
illegal huffman code that would then go on to crash due to an error
check in CanonicalCode::from_bytes. (worse)

We now store parent nodes starting at heap.size() - 1, which eliminates
the potential overlap, and resolves the issue.
2023-12-04 00:06:38 +01:00
Lucas CHOLLET
2a5cb5becb LibCompress: Add LZWDecoder::decode_all()
This method takes bytes as input and decompress everything to a
ByteBuffer. It uses two control codes (clear and end of data) as
described in the GIF, TIFF and PDF specifications.
2023-12-01 12:58:14 +01:00
Lucas CHOLLET
86ee7d219e LibCompress/LZW: Use its own debug flag
The file still used the `GIF_DEBUG` flag from when it was a part of the
GIF decoder. Let's give `LZWDecoder` its own flag.
2023-11-12 13:56:27 +01:00
Lucas CHOLLET
fcaebe56d7 LibCompress/LZW: Use a parameter to choose when to change the code size
Some users of the LZW algorithm use a different value to determine when
the size of the code changes. GIF increments the size when the number of
elements in the table is equal to 2^code_size while TIFF does it for a
count of 2^code_size - 1.

This patch adds the parameter m_offset_for_size_change with a default
value of 0 and our decoder will increment the code size when we reach
a table length of 2^code_size + m_offset_for_size_change. This allows us
to support both situations.
2023-11-12 13:56:27 +01:00
Lucas CHOLLET
0c0d7e8fd3 LibCompress/LZW: Support BigEndianInputBitStream
Let's templatize the LZWDecoder, so it can take both BigEndian and
LittleEndian variant.
2023-11-12 13:56:27 +01:00
Tim Schumacher
a2f60911fe AK: Rename GenericTraits to DefaultTraits
This feels like a more fitting name for something that provides the
default values for Traits.
2023-11-09 10:05:51 -05:00
Lucas CHOLLET
5e2b049de8 LibCompress/LZW: Use a LittleEndianBitStream
No need to manually implement bit stream logic when we have a helper for
this task.
2023-11-08 18:19:34 +01:00
Lucas CHOLLET
00ad8419cf LibGfx+LibCompress: Extract the LZW decoder and move it to LibCompress
Let's put this state-of-the-art decoder from the 80's in its own file in
order to reuse it with other formats, such as TIFF or PDF.
2023-11-08 18:19:34 +01:00
Tim Schumacher
25642dfe87 LibCompress: Implement correct validation of last filters 2023-10-29 22:00:59 +01:00
Tim Schumacher
786e654dfd LibCompress: Implement the XZ delta filter 2023-10-29 22:00:59 +01:00
Tim Schumacher
f0b08e9dea LibCompress: Process XZ filters in reverse order
XZ writes filters in the order that they are used during compression, so
we need to process them in the reverse order while decompression.

This wasn't noticed earlier because we only supported the LZMA2 filter.
2023-10-29 22:00:59 +01:00
Tim Ledbetter
2f26a7bb12 LibCompress: Avoid buffer overrun when building canonical Huffman code
Previously, decompressing a DEFLATE stream an invalid canonical
Huffman code could cause a buffer overrun. We now return an error in
this case.
2023-10-10 13:24:05 +02:00
Tim Schumacher
127f6ed6eb LibCompress: Fix a typo in m_read_final_block 2023-10-09 23:40:10 +02:00
kleines Filmröllchen
062e0db46c LibCore: Make MappedFile OwnPtr-based
Since it will become a stream in a little bit, it should behave like all
non-trivial stream classes, who are not primarily intended to have
shared ownership to make closing behavior more predictable. Across all
uses of MappedFile, there is only one use case of shared mapped files in
LibVideo, which now uses the thin SharedMappedFile wrapper.
2023-09-27 03:22:56 +02:00
Tim Schumacher
dbc25f18ec LibCompress: Let BrotliDecompressionStream take a MaybeOwned 2023-08-23 12:03:37 +01:00
Tim Schumacher
8a853278d0 LibCompress: Port ZlibDecompressor to AK::Stream 2023-08-23 12:03:37 +01:00
Tim Schumacher
90780d9ade LibCompress: Let GzipDecompressor take a MaybeOwned<Stream> 2023-08-23 12:03:37 +01:00
Lucas CHOLLET
3fdf5072ec LibCompress/Brotli: Remove CanonicalCode::clear()
This function was used in a single place and don't provide a huge
benefit over simply recreating the object.
2023-07-22 07:10:47 +02:00
Lucas CHOLLET
bb834ed765 LibCompress: Add a constructor to Brotli::CanonicalCode
This constructor will be used by the JPEG-XL decoder to support a
non-standard special case. Other user should only use other
constructors.
2023-07-21 10:47:34 -06:00
Lucas CHOLLET
96eace8390 LibCompress: Move CanonicalCode in the Brotli namespace
The class was an inner class of `BrotliDecompressionStream`, let's move
it outside the `Stream` object in order to ease the access to user only
interested in this part.
2023-07-21 10:47:34 -06:00
Lucas CHOLLET
9248fd7f33 LibCompress: Move CanonicalCode's initializers inside CanonicalCode
These routines:
 - read_prefix_code
 - read_simple_prefix_code
 - read_complex_prefix_code

 were methods of `BrotliDecompressionStream` taking a `CanonicalCode` as
 an out parameter. This patch puts them in `CanonicalCode` as static
 methods.
2023-07-21 10:47:34 -06:00
Lucas CHOLLET
d2dd4142d1 LibCompress: Make CanonicalCode::read_symbol const 2023-07-21 10:47:34 -06:00
Timothy Flynn
c911781c21 Everywhere: Remove needless trailing semi-colons after functions
This is a new option in clang-format-16.
2023-07-08 10:32:56 +01:00
Tim Schumacher
60ac254df6 AK: Use hashing to accelerate searching a CircularBuffer 2023-07-06 15:06:20 +01:00
Tim Schumacher
42d01b21d8 AK: Rewrite the hint-based CircularBuffer::find_copy_in_seekback
This now searches the memory in blocks, which should be slightly more
efficient. However, it doesn't make much difference (e.g. ~1% in LZMA
compression) in most real-world applications, as the non-hint function
is more expensive by orders of magnitude.
2023-07-06 15:06:20 +01:00
Tim Schumacher
046a9faeb3 AK: Split up CircularBuffer::find_copy_in_seekback
The "operation modes" of this function have very different focuses, and
trying to combine both in a way where we share the most amount of code
probably results in the worst performance.

Instead, split up the function into "existing distances" and "no
existing distances" so that we can optimize either case separately.
2023-07-06 15:06:20 +01:00
Tim Schumacher
9e82ad758e AK: Move parts for searching CircularBuffer into a new class
We will be adding extra logic to the CircularBuffer to optimize
searching, but this would negatively impact the performance of
CircularBuffer users that don't need that functionality.
2023-07-06 15:06:20 +01:00
tgsm
c30775522e LibCompress/Gzip: Replace usage of DeprecatedString 2023-06-17 06:44:16 +02:00
Tim Schumacher
d4b0e64825 LibCompress: Move two shared LZMA magic numbers into a common place 2023-05-19 23:40:33 +02:00