mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 00:47:28 +01:00
Merge claude/project-thread-0izcgq (encoder raw dictionaries)
At qualities 10 and 11, attaching a dictionary makes transform_io return "#unsupported encoder quality" until the Zopfli path's compound dictionary lookups are ported.
This commit is contained in:
10 files changed
+3056
-111
No files matched your search
+179
-7
@@ -154,7 +154,8 @@ invariants for memory safety but Wuffs requires explicit bounds checks.
|
||||
- Metadata callbacks (metadata blocks are currently skipped). See the
|
||||
"Metadata Blocks" section below.
|
||||
- Fast paths.
|
||||
- Encoder dictionaries (see the "Encoder" section below).
|
||||
- Compound dictionary matches at encoder qualities 10 and 11 (see the
|
||||
"Encoder" section below).
|
||||
|
||||
|
||||
## Encoder
|
||||
@@ -184,6 +185,11 @@ hasher, `BrotliCreateZopfliBackwardReferences` at quality 10 and
|
||||
`BrotliStoreMetaBlock`). Large-window brotli (see "Encoder Large Window"
|
||||
below) works at qualities 3 to 11 with lgwin 10 to 30. Like the C code,
|
||||
qualities 0 to 2 ignore the large-window quirk.
|
||||
Raw (LZ77 prefix) dictionaries (see "Encoder Dictionaries" below) work at
|
||||
qualities 0 to 9, and, like the C code, qualities 0 and 1 ignore them. At
|
||||
qualities 10 and 11, `transform_io` returns `"#unsupported encoder quality"`
|
||||
if a dictionary is attached (the Zopfli path's dictionary lookups are not
|
||||
implemented yet).
|
||||
|
||||
The source files are:
|
||||
|
||||
@@ -218,6 +224,13 @@ The source files are:
|
||||
`hash_composite_inc.h` (H35, H55 and H65, which combine H3, H54 or H6 with
|
||||
a rolling hasher) and `hash_rolling_inc.h` (`HROLLING_FAST` and
|
||||
`HROLLING`).
|
||||
- `encode_dictionary.wuffs`: `compound_dictionary.c`
|
||||
(`CreatePreparedDictionary`, `AttachPreparedDictionary`), the raw
|
||||
dictionary cases of `BrotliEncoderPrepareDictionary` and
|
||||
`BrotliEncoderAttachPreparedDictionary` (as `add_dictionary`), the
|
||||
compound dictionary search of `hash.h` (`FindCompoundDictionaryMatch`,
|
||||
`LookupCompoundDictionaryMatch`) and the compound dictionary branch of
|
||||
`ExtendLastCommand`.
|
||||
- `encode_backward_references.wuffs`: `backward_references.c` and
|
||||
`backward_references_inc.h` (`BrotliCreateBackwardReferences`),
|
||||
`command.h` and `ComputeDistanceCode`.
|
||||
@@ -286,6 +299,10 @@ In addition to the `base.io_transformer` methods, the encoder has:
|
||||
another request is pending.
|
||||
- `max_compressed_size(input_size: u64) u64`: mirrors
|
||||
`BrotliEncoderMaxCompressedSize` (0 means overflow).
|
||||
- `add_dictionary!(dict_type: u32, dict: roslice u8, workbuf: slice u8)
|
||||
status` and `add_dictionary_workbuf_len(dict_len: u64) u64`: attach a raw
|
||||
dictionary, like `BrotliEncoderPrepareDictionary` plus
|
||||
`BrotliEncoderAttachPreparedDictionary`. See "Encoder Dictionaries" below.
|
||||
|
||||
`dst_history_retain_length` is 0: the encoder never re-reads `dst`.
|
||||
|
||||
@@ -297,7 +314,8 @@ Each quirk mirrors one `BrotliEncoderParameter`, with the value semantics of
|
||||
otherwise `set_quirk` returns `"#bad argument"`). Like the C code, they are
|
||||
sanitized (`SanitizeParams`, `ComputeLgBlock`, `ChooseDistanceParams`) at the
|
||||
first `transform_io` call, after which `set_quirk` returns
|
||||
`"#bad call sequence"`.
|
||||
`"#bad call sequence"`. Quirks and dictionaries (`add_dictionary`) can be set
|
||||
in any order before that first call.
|
||||
|
||||
| Quirk | C parameter |
|
||||
| ---------------------------------------- | ---------------------------------------------- |
|
||||
@@ -329,6 +347,12 @@ size hint is replaced by the C code's `UpdateSizeHint` estimate.
|
||||
|
||||
### Encoder Workbuf Layout
|
||||
|
||||
Any attached dictionaries come first (see "Encoder Dictionaries" below): the
|
||||
workbuf's first `D` bytes hold them (with their prepared hash tables) and
|
||||
everything described in this section starts at offset `D` (so all of the
|
||||
offsets and lengths below are relative to `workbuf[D ..]`, and `workbuf_len`
|
||||
is `D` more than the lengths given here). `D` is 0 without dictionaries.
|
||||
|
||||
At qualities 0 and 1, the workbuf is the C code's metablock storage (its
|
||||
`GetBrotliStorage` buffer): `workbuf_len` is `(2 << lgwin) + 503 + 16` bytes
|
||||
(the storage for a `1 << lgwin` byte block plus 16 bytes of slack for 8-byte
|
||||
@@ -623,9 +647,139 @@ truncated for windows up to `1 << 30`. The rolling hashers' table holds
|
||||
32-bit positions, like the C code's.
|
||||
|
||||
|
||||
### Encoder Dictionaries
|
||||
|
||||
`encoder.add_dictionary!(dict_type: base.u32, dict: roslice base.u8, workbuf:
|
||||
slice base.u8) base.status` mirrors the C API's
|
||||
`BrotliEncoderPrepareDictionary(BROTLI_SHARED_DICTIONARY_RAW, ...)`
|
||||
followed by `BrotliEncoderAttachPreparedDictionary`: it attaches a raw (LZ77
|
||||
prefix) dictionary, as the next chunk of the C code's compound dictionary.
|
||||
The decoder must be given the same dictionaries, in the same order (with
|
||||
this package's `decoder.add_dictionary` or libbrotlidec's
|
||||
`BrotliDecoderAttachDictionary`, as `DICTIONARY_TYPE_RAW` /
|
||||
`BROTLI_SHARED_DICTIONARY_RAW`).
|
||||
|
||||
Ordering: like quirks, dictionaries must be added before the first
|
||||
`transform_io` call (the C API's "before the first
|
||||
`BrotliEncoderCompressStream`"). Quirks and dictionaries can be set in any
|
||||
order (preparing a raw dictionary does not depend on any parameter: the
|
||||
`quality` argument of `BrotliEncoderPrepareDictionary` is unused for raw
|
||||
dictionaries). Call `workbuf_len` after the last `add_dictionary` and
|
||||
`set_quirk` call.
|
||||
|
||||
Like the decoder, the encoder copies each dictionary into the workbuf, so
|
||||
every `add_dictionary` and `transform_io` call must be passed the same
|
||||
workbuf (or at least one whose first bytes hold the same contents). Each
|
||||
dictionary is followed by its prepared hash tables (the C code's
|
||||
`PreparedDictionary`), which `add_dictionary` builds exactly like
|
||||
`CreatePreparedDictionary` and `CreatePreparedDictionaryWithParams` in
|
||||
`compound_dictionary.c`, so that the compound dictionary search finds the
|
||||
same candidates in the same order:
|
||||
|
||||
- The hash table size depends only on the dictionary length `n`: 17 bucket
|
||||
bits and 7 slot bits for `n` up to 2 MiB, one more of each for each
|
||||
doubling of `n` (18 and 8 up to 4 MiB, etc), up to 22 and 12 (for more than
|
||||
32 MiB). Each position `i` with `i + 7 < n` is hashed (the low 40 bits of
|
||||
its 8 little-endian bytes, times `kPreparedDictionaryHashMul64Long`), with
|
||||
at most 32 positions per bucket (the most recent ones), and the bucket
|
||||
limits are lowered per slot so that each slot has fewer than 65535 items.
|
||||
- Its layout, after the `n` dictionary bytes, is `slot_offsets` (`1 <<
|
||||
slot_bits` u32le values), `heads` (`1 << bucket_bits` u16le values, 0xFFFF
|
||||
for an empty bucket) and `items` (`num_items` u32le values, each a
|
||||
dictionary position, with the high bit marking the end of a bucket's
|
||||
chain), like the C struct's dynamic members.
|
||||
|
||||
So the workbuf layout is:
|
||||
|
||||
[0 .. D) the dictionaries, each one being its n bytes, then
|
||||
(4 << slot_bits) + (2 << bucket_bits) + (4 * num_items)
|
||||
bytes of hash tables. num_items is at most max(0, n - 7).
|
||||
[D .. ) the encoder's other regions (see "Encoder Workbuf Layout").
|
||||
|
||||
Preparing a dictionary needs some scratch space (the C code's temporary
|
||||
"bloated" hasher: `(6 << bucket_bits) + (4 * n)` bytes), which `add_dictionary`
|
||||
puts after the largest possible hash tables. So `add_dictionary`'s workbuf
|
||||
must be at least `add_dictionary_workbuf_len(n)` bytes long: `D` (for the
|
||||
dictionaries added so far) plus `n + (4 << slot_bits) + (2 << bucket_bits) +
|
||||
(4 * max(0, n - 7))` plus that scratch space. For example, for a 1 MiB
|
||||
dictionary that is `D` plus about 9.8 MiB, and for a 40 MiB dictionary `D`
|
||||
plus about 384 MiB. After the call, `D` grows by the dictionary's bytes and
|
||||
actual hash tables (e.g. by at most 5.3 MiB for 1 MiB of dictionary).
|
||||
|
||||
The compound dictionary then affects compression like in the C code:
|
||||
|
||||
- Qualities 0 and 1 (`BrotliEncoderCompressStreamFast`) ignore it.
|
||||
- At qualities 2 to 9, its total size (the C code's `gap`) is added to the
|
||||
static dictionary's base distance (`dictionary_start + gap` in
|
||||
`CreateBackwardReferences`), and distances up to `dictionary_start + gap`
|
||||
(which include compound dictionary references) update the distance cache.
|
||||
`ExtendLastCommand` can extend a copy from the compound dictionary.
|
||||
- Only the hashers that `BrotliCreateBackwardReferences` instantiates with
|
||||
`ENABLE_COMPOUND_DICTIONARY` search the dictionaries (with
|
||||
`LookupCompoundDictionaryMatch`, after the hasher's own search, for each
|
||||
chunk in order): H5, H6, H58, H68, H40, H41, H42, H55 and H65. So
|
||||
qualities 5 to 9 search them, quality 4 only with the large-window H55
|
||||
(lgwin above 24 and a big size hint), and qualities 2 and 3 (H2, H3, H35)
|
||||
and quality 4 with H4 or H54 never do (the C code falls back to its
|
||||
non-dictionary variant for those hashers). The search only finds matches
|
||||
that are within `params.dist.max_distance` (`BROTLI_MAX_DISTANCE`, or
|
||||
`BROTLI_MAX_ALLOWED_DISTANCE` for large-window brotli) and that do not
|
||||
cross a chunk's end.
|
||||
- With `QUIRK_STREAM_OFFSET`, `dictionary_start` is `min(position +
|
||||
stream_offset, max_backward_limit)`, as in the C code (whose output then
|
||||
continues another stream and can only be decoded after it).
|
||||
|
||||
`add_dictionary` statuses (the C code's functions return `NULL` or
|
||||
`BROTLI_FALSE`):
|
||||
|
||||
| Condition | Wuffs status | C code |
|
||||
| ------------------------------------------------------------ | ---------------------------------------------- | ------ |
|
||||
| `transform_io` has been called | `base."#bad call sequence"` | accepted (see below) |
|
||||
| `DICTIONARY_TYPE_SERIALIZED` | `"#unsupported serialized encoder dictionary"` | `BrotliEncoderPrepareDictionary` returns `NULL` |
|
||||
| unknown `dict_type` | `base."#bad argument"` | `BrotliEncoderPrepareDictionary` returns `NULL` |
|
||||
| a 16th dictionary | `"#too many compound dictionaries"` | `BrotliEncoderAttachPreparedDictionary` fails |
|
||||
| more than `0x7FFF_FFFF` bytes of dictionaries in total | `"#too much dictionary data"` | accepted |
|
||||
| `workbuf` shorter than `add_dictionary_workbuf_len(n)` | `base."#bad workbuf length"` | n/a |
|
||||
|
||||
An empty dictionary is accepted (and counts towards the 15), like in the C
|
||||
code. On failure, the encoder's state is unchanged. A `transform_io` call
|
||||
with a workbuf shorter than `D` returns `base."#bad workbuf length"`.
|
||||
|
||||
Differences from the C code:
|
||||
|
||||
- Serialized (shared) dictionaries are rejected, exactly like the default
|
||||
(non-`BROTLI_EXPERIMENTAL`) libbrotlienc build, whose
|
||||
`BrotliEncoderPrepareDictionary` returns `NULL` for them. (The decoder, in
|
||||
contrast, accepts them, like an experimental libbrotlidec build.) With an
|
||||
experimental build, the C encoder would also use their custom static
|
||||
dictionaries; that is not implemented.
|
||||
- The C code's `BrotliEncoderAttachPreparedDictionary` accepts a raw
|
||||
dictionary even after compression has started (it only checks
|
||||
`is_initialized_` for serialized dictionaries). The compound dictionary's
|
||||
size then changes mid-stream, so the static dictionary references before
|
||||
and after that point use different distance bases, and the output is
|
||||
generally not decodable. Here, `add_dictionary` returns `"#bad call
|
||||
sequence"` once `transform_io` has been called.
|
||||
- The C code holds each dictionary's length in a `uint32_t` (and item
|
||||
offsets in 31 bits). This package rejects more than `0x7FFF_FFFF` bytes
|
||||
of dictionaries in total (like its decoder).
|
||||
- When `ExtendLastCommand` extends a copy past the end of a compound
|
||||
dictionary chunk into an empty chunk, the C code reads (one or more
|
||||
bytes) past the end of that empty chunk's (zero-length) data. Here, the
|
||||
extension stops there. This needs an empty dictionary attached after a
|
||||
non-empty one and a copy extension that reaches the non-empty one's end
|
||||
(with empty dictionaries only before the non-empty ones, the behaviors are
|
||||
the same).
|
||||
- Like the C code's `BrotliEncoderPrepareDictionary` with the default
|
||||
allocator, there is no separate "prepared dictionary" object that could be
|
||||
shared between encoders: each encoder prepares its own copy (the C code's
|
||||
"lean" prepared dictionaries reference the caller's bytes instead).
|
||||
|
||||
|
||||
### Encoder Testing
|
||||
|
||||
`wuffs test -mimic std/brotli` checks byte-exact parity with libbrotlienc
|
||||
`wuffs test -mimic std/brotli` (including its encoder dictionary tests)
|
||||
checks byte-exact parity with libbrotlienc
|
||||
over a grid of qualities (0 to 11), lgwins, modes, input chunkings, flushes,
|
||||
metadata blocks, size hints and other parameters (lgblock, npostfix, ndirect,
|
||||
disabling literal context modeling, stream offset, large window), as well as
|
||||
@@ -641,7 +795,22 @@ processes, `-lw` runs only its large-window grid (lgwin 10 to 30 at
|
||||
qualities 2 to 9, with various chunkings, flushes, size hints and other
|
||||
parameters, each also with the shortest workbuf that should work) and `-big`
|
||||
replaces the inputs with synthetic ones of 3 to 70 MiB with repeats at
|
||||
distances of up to 40 MiB. With `-q=0-9` (the default) it runs 814,706
|
||||
distances of up to 40 MiB. `-dict` replaces the usual grid with a
|
||||
dictionary grid (see "Encoder Dictionaries" above): for each input, seven
|
||||
sets of raw dictionaries (a prefix of the input, the whole input,
|
||||
`test/data/enwik5`, noise plus the input's middle third, 15 pieces of the
|
||||
input, tiny and empty dictionaries, and the input's swapped halves plus some
|
||||
text), with qualities 0 to 9, lgwins, chunkings, flushes, metadata blocks,
|
||||
size hints, stream offsets, large window and other parameters, plus
|
||||
dictionaries of 2 MiB to 40 MiB (every hash table size) with a 1 MiB input.
|
||||
Each dictionary configuration's output is also decoded by libbrotlidec and
|
||||
by this package's decoder (with the same dictionaries), except with a
|
||||
stream offset, and the whole-input and big dictionaries check that the
|
||||
output is smaller than without them (at qualities 5 and above, unless it is
|
||||
already tiny). With `-dict` (and `-q=0-9`) it runs 419,802 configurations,
|
||||
all byte-exact against libbrotlienc v1.2.1 on x86-64, with 356,546 round
|
||||
trips through both decoders. With
|
||||
`-q=0-9` (the default) and without `-dict` it runs 814,706
|
||||
configurations (96,688 of them in the large-window grid), all byte-exact
|
||||
against libbrotlienc v1.2.1 on x86-64. With `-lw -big` it runs 720 more
|
||||
(on 3, 6 and 12 MiB inputs with the large-window grid and on 24, 40 and
|
||||
@@ -828,14 +997,17 @@ metablock) for as long as it needs the results.
|
||||
|
||||
### Encoder TODO
|
||||
|
||||
- Large-window encoding at qualities 3 and above (it works at quality 2).
|
||||
- `workbuf_len` is a worst case (or, at qualities 10 and 11, a practical
|
||||
bound). A future option could take the input length (like the C code's
|
||||
size hint) to give a tighter bound.
|
||||
- Speed: at qualities 10 and 11 the Wuffs encoder is several times slower
|
||||
than libbrotlienc, mostly because of the soft float arithmetic in the
|
||||
block splitter and histogram clustering.
|
||||
- Custom and shared dictionaries (`BrotliEncoderAttachPreparedDictionary`).
|
||||
Zopfli cost model, the block splitter and histogram clustering.
|
||||
- Compound dictionary matches at qualities 10 and 11 (the Zopfli path's
|
||||
`FindCompoundDictionaryMatch` lookups); attaching a dictionary at those
|
||||
qualities currently returns `"#unsupported encoder quality"`.
|
||||
- Serialized (shared) dictionaries, which only an experimental
|
||||
(`BROTLI_EXPERIMENTAL`) libbrotlienc build supports.
|
||||
- Changing parameters mid-stream.
|
||||
|
||||
|
||||
|
||||
@@ -224,10 +224,11 @@ pri func encoder.compute_distance_code(distance: base.u64, max_distance: base.u6
|
||||
}
|
||||
|
||||
// Mirrors CreateBackwardReferences in backward_references_inc.h (via
|
||||
// BrotliCreateBackwardReferences in backward_references.c), without a
|
||||
// compound dictionary and with the built-in static dictionary. New commands
|
||||
// are appended at this.num_cmds.
|
||||
pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.u64, position: base.u64) base.status {
|
||||
// BrotliCreateBackwardReferences in backward_references.c), with the
|
||||
// built-in static dictionary. Its ENABLE_COMPOUND_DICTIONARY variant (see
|
||||
// compound_dictionary_enabled) searches the attached dictionaries (in
|
||||
// args.dwb). New commands are appended at this.num_cmds.
|
||||
pri func encoder.create_backward_references!(wb: slice base.u8, dwb: roslice base.u8, num_bytes: base.u64, position: base.u64) base.status {
|
||||
var position : base.u64
|
||||
var max_backward_limit : base.u64
|
||||
var position_offset : base.u64
|
||||
@@ -250,6 +251,9 @@ pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.
|
||||
var range_end : base.u64
|
||||
var k_margin : base.u64
|
||||
var pos_jump : base.u64
|
||||
var gap : base.u64
|
||||
var use_compound : base.bool
|
||||
var data : roslice base.u8
|
||||
|
||||
position = args.position
|
||||
max_backward_limit = ((1 as base.u64) << this.lgwin) ~mod- 16
|
||||
@@ -266,6 +270,11 @@ pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.
|
||||
random_heuristics_window_size = 512
|
||||
}
|
||||
apply_random_heuristics = position ~mod+ random_heuristics_window_size
|
||||
gap = this.dict_total_size
|
||||
use_compound = this.compound_dictionary_enabled()
|
||||
if this.rb_buf_off <= args.wb.length() {
|
||||
data = args.wb[this.rb_buf_off ..]
|
||||
}
|
||||
|
||||
this.prepare_distance_cache!()
|
||||
|
||||
@@ -278,8 +287,13 @@ pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.
|
||||
this.sr_distance = 0
|
||||
this.sr_score = SCORE_BASE + 100
|
||||
this.find_longest_match!(wb: args.wb, cur_ix: position, max_length: max_length,
|
||||
max_backward: max_distance, dictionary_distance: dictionary_start,
|
||||
max_backward: max_distance, dictionary_distance: dictionary_start ~mod+ gap,
|
||||
max_distance: this.dist_max_distance)
|
||||
if use_compound {
|
||||
this.lookup_compound_dictionary_match!(data: data, dwb: args.dwb,
|
||||
cur_ix: position, max_length: max_length,
|
||||
max_ring_buffer_distance: dictionary_start, max_distance: this.dist_max_distance)
|
||||
}
|
||||
if this.sr_score > (SCORE_BASE + 100) {
|
||||
// Found a match. Let's look for something even better ahead.
|
||||
delayed_backward_references = 0
|
||||
@@ -299,8 +313,13 @@ pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.
|
||||
max_distance = this.min64(a: position ~mod+ 1, b: max_backward_limit)
|
||||
dictionary_start = this.min64(a: (position ~mod+ 1) ~mod+ position_offset, b: max_backward_limit)
|
||||
this.find_longest_match!(wb: args.wb, cur_ix: position ~mod+ 1, max_length: max_length,
|
||||
max_backward: max_distance, dictionary_distance: dictionary_start,
|
||||
max_backward: max_distance, dictionary_distance: dictionary_start ~mod+ gap,
|
||||
max_distance: this.dist_max_distance)
|
||||
if use_compound {
|
||||
this.lookup_compound_dictionary_match!(data: data, dwb: args.dwb,
|
||||
cur_ix: position ~mod+ 1, max_length: max_length,
|
||||
max_ring_buffer_distance: dictionary_start, max_distance: this.dist_max_distance)
|
||||
}
|
||||
if this.sr_score >= (sr_score ~mod+ 175) {
|
||||
// Ok, let's just write one byte for now and start a match
|
||||
// from the next byte.
|
||||
@@ -324,8 +343,8 @@ pri func encoder.create_backward_references!(wb: slice base.u8, num_bytes: base.
|
||||
dictionary_start = this.min64(a: position ~mod+ position_offset, b: max_backward_limit)
|
||||
// The first 16 codes are special short-codes, and the minimum
|
||||
// offset is 1.
|
||||
distance_code = this.compute_distance_code(distance: this.sr_distance, max_distance: dictionary_start)
|
||||
if (this.sr_distance <= dictionary_start) and (distance_code > 0) {
|
||||
distance_code = this.compute_distance_code(distance: this.sr_distance, max_distance: dictionary_start ~mod+ gap)
|
||||
if (this.sr_distance <= (dictionary_start ~mod+ gap)) and (distance_code > 0) {
|
||||
this.dist_cache[3] = this.dist_cache[2]
|
||||
this.dist_cache[2] = this.dist_cache[1]
|
||||
this.dist_cache[1] = this.dist_cache[0]
|
||||
|
||||
@@ -194,6 +194,19 @@ pub struct encoder? implements base.io_transformer(
|
||||
hr_next_ix : base.u64,
|
||||
hr_off : base.u64,
|
||||
|
||||
// The attached raw dictionaries (the C code's CompoundDictionary, see
|
||||
// encode_dictionary.wuffs). They use the first dict_end bytes of the
|
||||
// workbuf.
|
||||
dict_num_chunks : base.u32[..= 15],
|
||||
dict_total_size : base.u64,
|
||||
dict_end : base.u64,
|
||||
dict_chunk_offsets : array[16] base.u64,
|
||||
dc_src_off : array[15] base.u64,
|
||||
dc_size : array[15] base.u64,
|
||||
dc_bucket_bits : array[15] base.u32,
|
||||
dc_slot_bits : array[15] base.u32,
|
||||
dc_num_items : array[15] base.u64,
|
||||
|
||||
// The C code's HasherSearchResult.
|
||||
sr_len : base.u64,
|
||||
sr_distance : base.u64,
|
||||
@@ -358,6 +371,12 @@ pub struct encoder? implements base.io_transformer(
|
||||
// GetHashTable result), with up to (1 << 17) elements.
|
||||
hash_table : array[131072] base.u32,
|
||||
|
||||
// CreatePreparedDictionaryWithParams' slot_size and slot_limit
|
||||
// arrays, and its slot_offsets (see encode_dictionary.wuffs).
|
||||
dp_slot_size : array[4096] base.u32,
|
||||
dp_slot_limit : array[4096] base.u32,
|
||||
dp_slot_off : array[4096] base.u32,
|
||||
|
||||
// command_buf and literal_buf are the C code's command_buf_ and
|
||||
// literal_buf_ (quality 1 only).
|
||||
command_buf : array[131072] base.u32,
|
||||
@@ -477,6 +496,7 @@ pub func encoder.workbuf_len() base.range_ii_u64 {
|
||||
var q : base.u32[..= 11]
|
||||
var lgwin : base.u32[..= 30]
|
||||
var n : base.u64
|
||||
var r : base.range_ii_u64
|
||||
|
||||
q = this.sanitized_quality()
|
||||
lgwin = this.sanitized_lgwin(quality: q)
|
||||
@@ -486,8 +506,12 @@ pub func encoder.workbuf_len() base.range_ii_u64 {
|
||||
n = this.compute_layout_offset(quality: q, lgwin: lgwin,
|
||||
lgblock: this.computed_lgblock(quality: q, lgwin: lgwin), idx: LAYOUT_END)
|
||||
} else {
|
||||
return this.workbuf_len_hq(quality: q, lgwin: lgwin)
|
||||
r = this.workbuf_len_hq(quality: q, lgwin: lgwin)
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: r.get_min_incl() ~sat+ this.dict_end,
|
||||
max_incl: r.get_max_incl() ~sat+ this.dict_end)
|
||||
}
|
||||
n ~sat+= this.dict_end
|
||||
return this.util.make_range_ii_u64(min_incl: n, max_incl: n)
|
||||
}
|
||||
|
||||
@@ -849,7 +873,31 @@ pri func encoder.write_metadata_header!(block_size: base.u32) base.u64 {
|
||||
// qualities 0 and 1, BrotliEncoderCompressStreamFast). Each (initial or
|
||||
// resumed) transform_io call corresponds to one or more C calls. See
|
||||
// README.md for the details.
|
||||
//
|
||||
// The attached dictionaries (see add_dictionary) are the first dict_end
|
||||
// bytes of the workbuf. do_transform_io gets the rest of the workbuf (whose
|
||||
// layout is described in README.md and encode_data.wuffs) and, as args.dwb,
|
||||
// the dictionaries.
|
||||
pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8) {
|
||||
var status : base.status
|
||||
|
||||
while true {
|
||||
if this.dict_end > args.workbuf.length() {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
status =? this.do_transform_io?(dst: args.dst, src: args.src,
|
||||
workbuf: args.workbuf[this.dict_end ..],
|
||||
dwb: args.workbuf[.. this.dict_end])
|
||||
if status.is_ok() {
|
||||
return ok
|
||||
} else if not status.is_suspension() {
|
||||
return status
|
||||
}
|
||||
yield? status
|
||||
}
|
||||
}
|
||||
|
||||
pri func encoder.do_transform_io?(dst: base.io_writer, src: base.io_reader, workbuf: slice base.u8, dwb: roslice base.u8) {
|
||||
var op : base.u32
|
||||
var avail : base.u64
|
||||
var block_size : base.u64
|
||||
@@ -868,6 +916,11 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
var total : base.u64
|
||||
|
||||
this.ensure_initialized!()
|
||||
if (this.quality >= 10) and (this.dict_num_chunks > 0) {
|
||||
// TODO: compound dictionaries at qualities 10 and 11 (the Zopfli
|
||||
// path's dictionary lookups).
|
||||
return "#unsupported encoder quality"
|
||||
}
|
||||
this.hq_extend_var_region!(wb_len: args.workbuf.length())
|
||||
if (this.quality >= 2) and (args.workbuf.length() < this.workbuf_end) {
|
||||
// A workbuf shorter than workbuf_len works if it is long enough for
|
||||
@@ -911,7 +964,7 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
continue
|
||||
}
|
||||
if (this.quality >= 2) and (this.input_pos <> this.last_flush_pos) {
|
||||
status = this.encode_data!(wb: args.workbuf, is_last: false, force_flush: true)
|
||||
status = this.encode_data!(wb: args.workbuf, dwb: args.dwb, is_last: false, force_flush: true)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
@@ -1027,7 +1080,7 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
force_flush = true
|
||||
}
|
||||
this.update_size_hint!(available_in: args.src.length())
|
||||
status = this.encode_data!(wb: args.workbuf, is_last: is_last, force_flush: force_flush)
|
||||
status = this.encode_data!(wb: args.workbuf, dwb: args.dwb, is_last: is_last, force_flush: force_flush)
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
}
|
||||
|
||||
@@ -487,10 +487,11 @@ pri func encoder.update_size_hint!(available_in: base.u64) {
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors ExtendLastCommand in encode.c (without a compound dictionary). It
|
||||
// returns how many bytes it extended the last command by: the caller
|
||||
// subtracts that from bytes and adds it to wrapped_last_processed_pos.
|
||||
pri func encoder.extend_last_command!(wb: slice base.u8, bytes: base.u64, wrapped_last_processed_pos: base.u64) base.u64 {
|
||||
// Mirrors ExtendLastCommand in encode.c (see extend_last_command_compound
|
||||
// for its compound dictionary branch). It returns how many bytes it extended
|
||||
// the last command by: the caller subtracts that from bytes and adds it to
|
||||
// wrapped_last_processed_pos.
|
||||
pri func encoder.extend_last_command!(wb: slice base.u8, dwb: roslice base.u8, bytes: base.u64, wrapped_last_processed_pos: base.u64) base.u64 {
|
||||
var last : base.u64
|
||||
var max_backward_distance : base.u64
|
||||
var copy_len : base.u32
|
||||
@@ -519,6 +520,11 @@ pri func encoder.extend_last_command!(wb: slice base.u8, bytes: base.u64, wrappe
|
||||
n ~mod+= 1
|
||||
p = (p ~mod+ 1) & 0xFFFF_FFFF
|
||||
}
|
||||
} else {
|
||||
n = this.extend_last_command_compound(wb: args.wb, dwb: args.dwb, bytes: args.bytes,
|
||||
wrapped_last_processed_pos: args.wrapped_last_processed_pos,
|
||||
cmd_dist: cmd_dist, max_distance: max_distance, last_copy_len: last_copy_len)
|
||||
copy_len ~mod+= (n & 0xFFFF_FFFF) as base.u32
|
||||
}
|
||||
// The copy length is at most the metablock size, and thus
|
||||
// expressible.
|
||||
@@ -876,7 +882,7 @@ pri func encoder.write_meta_block_internal!(wb: slice base.u8, last_flush_pos: b
|
||||
|
||||
// Mirrors EncodeData in encode.c, for qualities 2 to 9. On a new metablock,
|
||||
// it sets the pending output (this.out_etc) to the storage (or tiny_buf).
|
||||
pri func encoder.encode_data!(wb: slice base.u8, is_last: base.bool, force_flush: base.bool) base.status {
|
||||
pri func encoder.encode_data!(wb: slice base.u8, dwb: roslice base.u8, is_last: base.bool, force_flush: base.bool) base.status {
|
||||
var status : base.status
|
||||
var delta : base.u64
|
||||
var bytes : base.u64
|
||||
@@ -941,7 +947,7 @@ pri func encoder.encode_data!(wb: slice base.u8, is_last: base.bool, force_flush
|
||||
}
|
||||
|
||||
if (this.num_cmds > 0) and (this.last_insert_len == 0) {
|
||||
n = this.extend_last_command!(wb: args.wb, bytes: bytes,
|
||||
n = this.extend_last_command!(wb: args.wb, dwb: args.dwb, bytes: bytes,
|
||||
wrapped_last_processed_pos: wrapped_last_processed_pos)
|
||||
bytes ~mod-= n
|
||||
wrapped_last_processed_pos = (wrapped_last_processed_pos ~mod+ n) & 0xFFFF_FFFF
|
||||
@@ -952,7 +958,7 @@ pri func encoder.encode_data!(wb: slice base.u8, is_last: base.bool, force_flush
|
||||
} else if this.quality == 11 {
|
||||
status = this.create_hq_zopfli_backward_references!(wb: args.wb, num_bytes: bytes, position: wrapped_last_processed_pos)
|
||||
} else {
|
||||
status = this.create_backward_references!(wb: args.wb, num_bytes: bytes, position: wrapped_last_processed_pos)
|
||||
status = this.create_backward_references!(wb: args.wb, dwb: args.dwb, num_bytes: bytes, position: wrapped_last_processed_pos)
|
||||
}
|
||||
if not status.is_ok() {
|
||||
return status
|
||||
|
||||
@@ -0,0 +1,568 @@
|
||||
// Copyright 2026 The Wuffs Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
// --------
|
||||
|
||||
// This file mirrors the encoder's raw (prefix) dictionaries: compound
|
||||
// dictionary.c (CreatePreparedDictionary and AttachPreparedDictionary), the
|
||||
// BROTLI_SHARED_DICTIONARY_RAW cases of BrotliEncoderPrepareDictionary and
|
||||
// BrotliEncoderAttachPreparedDictionary in encode.c and the compound
|
||||
// dictionary search of hash.h (FindCompoundDictionaryMatch and
|
||||
// LookupCompoundDictionaryMatch).
|
||||
//
|
||||
// The dictionaries live at the start of the workbuf (see README.md). Each
|
||||
// one (a "chunk", the C code's PreparedDictionary) is, starting at its
|
||||
// dc_src_off workbuf offset:
|
||||
// - the dictionary bytes (the C code's source, dc_size bytes),
|
||||
// - slot_offsets: (1 << dc_slot_bits) u32le values,
|
||||
// - heads: (1 << dc_bucket_bits) u16le values and
|
||||
// - items: dc_num_items u32le values.
|
||||
|
||||
pub status "#unsupported serialized encoder dictionary"
|
||||
|
||||
// ENCODER_DICTIONARY_CHUNKS_MAX mirrors SHARED_BROTLI_MAX_COMPOUND_DICTS in
|
||||
// shared_dictionary.h.
|
||||
pri const ENCODER_DICTIONARY_CHUNKS_MAX : base.u32 = 15
|
||||
|
||||
// Mirrors kPreparedDictionaryHashMul64Long in compound_dictionary.h.
|
||||
pri const PREPARED_DICTIONARY_HASH_MUL_64_LONG : base.u64 = 0x1FE3_5A7B_D357_9BD3
|
||||
|
||||
// add_dictionary attaches a raw (LZ77 prefix) dictionary, like
|
||||
// BrotliEncoderPrepareDictionary (with BROTLI_SHARED_DICTIONARY_RAW)
|
||||
// followed by BrotliEncoderAttachPreparedDictionary in encode.c. It must be
|
||||
// called before the first transform_io call. It can be called up to 15 times
|
||||
// (the C code's SHARED_BROTLI_MAX_COMPOUND_DICTS).
|
||||
//
|
||||
// args.dict_type is DICTIONARY_TYPE_RAW or DICTIONARY_TYPE_SERIALIZED. Like
|
||||
// a default (not BROTLI_EXPERIMENTAL) libbrotlienc build, whose
|
||||
// BrotliEncoderPrepareDictionary returns NULL for a serialized dictionary,
|
||||
// serialized dictionaries are rejected.
|
||||
//
|
||||
// Wuffs structs cannot hold pointers, so the dictionary's bytes are copied
|
||||
// into the workbuf, followed by its prepared hash tables (the C code's
|
||||
// PreparedDictionary), after the dictionaries added so far. args.workbuf must
|
||||
// be at least add_dictionary_workbuf_len(args.dict.length()) bytes long
|
||||
// (preparing the hash tables needs some scratch space after them) and every
|
||||
// later add_dictionary and transform_io call must be passed the same workbuf
|
||||
// (or at least one whose prefix holds the same bytes). After the last
|
||||
// add_dictionary call, workbuf_len gives the workbuf length that
|
||||
// transform_io needs.
|
||||
//
|
||||
// On failure, the encoder's state is unchanged.
|
||||
pub func encoder.add_dictionary!(dict_type: base.u32, dict: roslice base.u8, workbuf: slice base.u8) base.status {
|
||||
var n : base.u64
|
||||
var lo : base.u64
|
||||
var hi : base.u64
|
||||
var bucket_bits : base.u32[..= 22]
|
||||
var slot_bits : base.u32[..= 12]
|
||||
var num_items : base.u64
|
||||
var c : base.u32
|
||||
|
||||
if this.is_initialized {
|
||||
return base."#bad call sequence"
|
||||
} else if args.dict_type == DICTIONARY_TYPE_SERIALIZED {
|
||||
return "#unsupported serialized encoder dictionary"
|
||||
} else if args.dict_type <> DICTIONARY_TYPE_RAW {
|
||||
return base."#bad argument"
|
||||
} else if this.dict_num_chunks >= ENCODER_DICTIONARY_CHUNKS_MAX {
|
||||
// Mirrors AttachPreparedDictionary in compound_dictionary.c.
|
||||
return "#too many compound dictionaries"
|
||||
}
|
||||
n = args.dict.length()
|
||||
if (n > DICT_TOTAL_MAX) or ((this.dict_total_size ~sat+ n) > DICT_TOTAL_MAX) {
|
||||
return "#too much dictionary data"
|
||||
} else if this.add_dictionary_workbuf_len(dict_len: n) > args.workbuf.length() {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
|
||||
// Mirrors CreatePreparedDictionary in compound_dictionary.c.
|
||||
bucket_bits = 17
|
||||
while (((0x10 as base.u64) << bucket_bits) < n) and (bucket_bits < 22) {
|
||||
bucket_bits += 1
|
||||
}
|
||||
if bucket_bits >= 10 {
|
||||
slot_bits = bucket_bits - 10
|
||||
}
|
||||
|
||||
// Copy the dictionary bytes into the workbuf.
|
||||
lo = this.dict_end
|
||||
hi = lo ~mod+ n
|
||||
if (lo > hi) or (hi > args.workbuf.length()) {
|
||||
return "#internal error: inconsistent encoder state"
|
||||
}
|
||||
args.workbuf[lo .. hi].copy_from_slice!(s: args.dict)
|
||||
|
||||
num_items = this.prepare_dictionary!(wb: args.workbuf, src_off: lo, n: n,
|
||||
bucket_bits: bucket_bits, slot_bits: slot_bits)
|
||||
|
||||
// Mirrors AttachPreparedDictionary in compound_dictionary.c.
|
||||
c = this.dict_num_chunks
|
||||
if c >= 15 {
|
||||
return "#internal error: inconsistent encoder state"
|
||||
}
|
||||
this.dc_src_off[c] = lo
|
||||
this.dc_size[c] = n
|
||||
this.dc_bucket_bits[c] = bucket_bits
|
||||
this.dc_slot_bits[c] = slot_bits
|
||||
this.dc_num_items[c] = num_items
|
||||
this.dict_total_size ~mod+= n
|
||||
this.dict_chunk_offsets[c + 1] = this.dict_total_size
|
||||
this.dict_num_chunks = c + 1
|
||||
this.dict_end = ((hi ~mod+ ((4 as base.u64) << slot_bits)) ~mod+
|
||||
((2 as base.u64) << bucket_bits)) ~mod+ (4 ~mod* num_items)
|
||||
return ok
|
||||
}
|
||||
|
||||
// add_dictionary_workbuf_len returns the workbuf length that an
|
||||
// add_dictionary call, for a dictionary of dict_len bytes, needs: the
|
||||
// dictionaries added so far, plus that dictionary's bytes, its hash tables
|
||||
// (assuming the most items) and the scratch space for preparing them. It
|
||||
// returns 0xFFFF_FFFF_FFFF_FFFF if dict_len is too large.
|
||||
pub func encoder.add_dictionary_workbuf_len(dict_len: base.u64) base.u64 {
|
||||
var bucket_bits : base.u32[..= 22]
|
||||
var slot_bits : base.u32[..= 12]
|
||||
var n : base.u64
|
||||
var max_items : base.u64
|
||||
|
||||
if args.dict_len > DICT_TOTAL_MAX {
|
||||
return 0xFFFF_FFFF_FFFF_FFFF
|
||||
}
|
||||
n = args.dict_len
|
||||
bucket_bits = 17
|
||||
while (((0x10 as base.u64) << bucket_bits) < n) and (bucket_bits < 22) {
|
||||
bucket_bits += 1
|
||||
}
|
||||
if bucket_bits >= 10 {
|
||||
slot_bits = bucket_bits - 10
|
||||
}
|
||||
if n > 7 {
|
||||
max_items = n - 7
|
||||
}
|
||||
// The dictionary bytes, slot_offsets, heads and (at most) items, then the
|
||||
// scratch space: num (u16), bucket_heads (u32) and next_bucket (u32).
|
||||
return ((((this.dict_end ~sat+ n) ~sat+ ((4 as base.u64) << slot_bits)) ~sat+
|
||||
((2 as base.u64) << bucket_bits)) ~sat+ (4 ~mod* max_items)) ~sat+
|
||||
(((6 as base.u64) << bucket_bits) ~sat+ (4 ~mod* n))
|
||||
}
|
||||
|
||||
// prepare_dictionary mirrors CreatePreparedDictionaryWithParams in
|
||||
// compound_dictionary.c, with hash_bits 40 and bucket_limit 32, for the n
|
||||
// dictionary bytes at args.wb[args.src_off ..]. It writes the slot_offsets,
|
||||
// heads and items arrays after those bytes (with the scratch space after the
|
||||
// largest possible items array) and returns the number of items.
|
||||
pri func encoder.prepare_dictionary!(wb: slice base.u8, src_off: base.u64, n: base.u64, bucket_bits: base.u32[..= 22], slot_bits: base.u32[..= 12]) base.u64 {
|
||||
var num_slots : base.u64
|
||||
var num_buckets : base.u64
|
||||
var slot_mask : base.u64
|
||||
var slot_off : base.u64
|
||||
var heads_off : base.u64
|
||||
var items_off : base.u64
|
||||
var num_off : base.u64
|
||||
var bh_off : base.u64
|
||||
var nb_off : base.u64
|
||||
var max_items : base.u64
|
||||
var i : base.u64
|
||||
var j : base.u64
|
||||
var h : base.u64
|
||||
var key : base.u64
|
||||
var count : base.u32
|
||||
var limit : base.u32
|
||||
var size : base.u32
|
||||
var overflow : base.bool
|
||||
var total_items : base.u64
|
||||
var slot : base.u64
|
||||
var cursor : base.u64
|
||||
var pos : base.u64
|
||||
var k : base.u32
|
||||
|
||||
num_slots = (1 as base.u64) << args.slot_bits
|
||||
num_buckets = (1 as base.u64) << args.bucket_bits
|
||||
slot_mask = num_slots - 1
|
||||
if args.n > 7 {
|
||||
max_items = args.n - 7
|
||||
}
|
||||
slot_off = args.src_off ~mod+ args.n
|
||||
heads_off = slot_off ~mod+ (4 ~mod* num_slots)
|
||||
items_off = heads_off ~mod+ (2 ~mod* num_buckets)
|
||||
num_off = items_off ~mod+ (4 ~mod* max_items)
|
||||
bh_off = num_off ~mod+ (2 ~mod* num_buckets)
|
||||
nb_off = bh_off ~mod+ (4 ~mod* num_buckets)
|
||||
|
||||
// Step 1: create "bloated" hasher.
|
||||
this.fill_bytes!(s: args.wb, i: num_off, n: 2 ~mod* num_buckets, v: 0)
|
||||
i = 0
|
||||
while (i ~mod+ 7) < args.n {
|
||||
h = (this.load64le(s: args.wb, i: args.src_off ~mod+ i) & 0xFF_FFFF_FFFF) ~mod*
|
||||
PREPARED_DICTIONARY_HASH_MUL_64_LONG
|
||||
key = (h >> 32) >> (32 - args.bucket_bits)
|
||||
count = this.peek_u16(s: args.wb, i: num_off ~mod+ (2 ~mod* key)) as base.u32
|
||||
if count == 0 {
|
||||
this.poke_u32!(s: args.wb, i: nb_off ~mod+ (4 ~mod* i), v: 0xFFFF_FFFF)
|
||||
} else {
|
||||
this.poke_u32!(s: args.wb, i: nb_off ~mod+ (4 ~mod* i),
|
||||
v: this.load32le(s: args.wb, i: bh_off ~mod+ (4 ~mod* key)))
|
||||
}
|
||||
this.poke_u32!(s: args.wb, i: bh_off ~mod+ (4 ~mod* key), v: (i & 0xFFFF_FFFF) as base.u32)
|
||||
count ~mod+= 1
|
||||
if count > 32 {
|
||||
count = 32
|
||||
}
|
||||
this.poke_u16!(s: args.wb, i: num_off ~mod+ (2 ~mod* key), v: (count & 0xFFFF) as base.u16)
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
// Step 2: find slot limits.
|
||||
i = 0
|
||||
while i < num_slots {
|
||||
limit = 32
|
||||
while true {
|
||||
count = 0
|
||||
overflow = false
|
||||
j = i
|
||||
while j < num_buckets {
|
||||
size = this.peek_u16(s: args.wb, i: num_off ~mod+ (2 ~mod* j)) as base.u32
|
||||
// Last chain may span behind 64K limit; overflow happens
|
||||
// only if we are about to use 0xFFFF+ as item offset.
|
||||
if count >= 0xFFFF {
|
||||
overflow = true
|
||||
break
|
||||
}
|
||||
if size > limit {
|
||||
size = limit
|
||||
}
|
||||
count ~mod+= size
|
||||
j ~mod+= num_slots
|
||||
}
|
||||
if not overflow {
|
||||
this.dp_slot_size[i & 4095] = count
|
||||
total_items ~mod+= count as base.u64
|
||||
break
|
||||
} else if limit == 0 {
|
||||
// Unreachable: with a zero limit, count stays zero.
|
||||
this.dp_slot_size[i & 4095] = 0
|
||||
break
|
||||
}
|
||||
limit -= 1
|
||||
}
|
||||
this.dp_slot_limit[i & 4095] = limit
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
// Step 3: transfer data to "slim" hasher.
|
||||
total_items = 0
|
||||
i = 0
|
||||
while i < num_slots {
|
||||
this.poke_u32!(s: args.wb, i: slot_off ~mod+ (4 ~mod* i), v: (total_items & 0xFFFF_FFFF) as base.u32)
|
||||
this.dp_slot_off[i & 4095] = (total_items & 0xFFFF_FFFF) as base.u32
|
||||
total_items ~mod+= this.dp_slot_size[i & 4095] as base.u64
|
||||
this.dp_slot_size[i & 4095] = 0
|
||||
i ~mod+= 1
|
||||
}
|
||||
i = 0
|
||||
while i < num_buckets {
|
||||
slot = i & slot_mask & 4095
|
||||
count = this.peek_u16(s: args.wb, i: num_off ~mod+ (2 ~mod* i)) as base.u32
|
||||
if count > this.dp_slot_limit[slot] {
|
||||
count = this.dp_slot_limit[slot]
|
||||
}
|
||||
if count == 0 {
|
||||
this.poke_u16!(s: args.wb, i: heads_off ~mod+ (2 ~mod* i), v: 0xFFFF)
|
||||
i ~mod+= 1
|
||||
continue
|
||||
}
|
||||
cursor = this.dp_slot_size[slot] as base.u64
|
||||
this.poke_u16!(s: args.wb, i: heads_off ~mod+ (2 ~mod* i), v: (cursor & 0xFFFF) as base.u16)
|
||||
cursor ~mod+= this.dp_slot_off[slot] as base.u64
|
||||
this.dp_slot_size[slot] ~mod+= count
|
||||
pos = this.load32le(s: args.wb, i: bh_off ~mod+ (4 ~mod* i)) as base.u64
|
||||
k = 0
|
||||
while k < count {
|
||||
this.poke_u32!(s: args.wb, i: items_off ~mod+ (4 ~mod* cursor), v: (pos & 0xFFFF_FFFF) as base.u32)
|
||||
cursor ~mod+= 1
|
||||
pos = this.load32le(s: args.wb, i: nb_off ~mod+ (4 ~mod* pos)) as base.u64
|
||||
k ~mod+= 1
|
||||
}
|
||||
cursor ~mod-= 1
|
||||
this.poke_u32!(s: args.wb, i: items_off ~mod+ (4 ~mod* cursor),
|
||||
v: this.load32le(s: args.wb, i: items_off ~mod+ (4 ~mod* cursor)) | 0x8000_0000)
|
||||
i ~mod+= 1
|
||||
}
|
||||
return total_items
|
||||
}
|
||||
|
||||
// compound_dictionary_enabled returns whether BrotliCreateBackwardReferences
|
||||
// in backward_references.c uses its ENABLE_COMPOUND_DICTIONARY variant: when
|
||||
// any dictionary is attached and the hasher is H5, H6, H58, H68, H40, H41,
|
||||
// H42, H55 or H65 (H55 and H65 are the composite hashers H54 and H6 plus a
|
||||
// rolling hasher). The other hashers (H2, H3, H4, H54 and H35) ignore the
|
||||
// compound dictionary (but not its total size).
|
||||
pri func encoder.compound_dictionary_enabled() base.bool {
|
||||
var htype : base.u32
|
||||
|
||||
if this.dict_num_chunks == 0 {
|
||||
return false
|
||||
}
|
||||
htype = this.hasher_type
|
||||
return (htype == 5) or (htype == 6) or (htype == 58) or (htype == 68) or
|
||||
(htype == 40) or (htype == 41) or (htype == 42) or
|
||||
((htype == 54) and (this.hr_jump <> 0))
|
||||
}
|
||||
|
||||
// Mirrors LookupCompoundDictionaryMatch in hash.h. args.data is the ring
|
||||
// buffer (the C code's data) and args.dwb holds the dictionaries.
|
||||
pri func encoder.lookup_compound_dictionary_match!(data: roslice base.u8, dwb: roslice base.u8, cur_ix: base.u64, max_length: base.u64, max_ring_buffer_distance: base.u64, max_distance: base.u64) {
|
||||
var base_offset : base.u64
|
||||
var d : base.u32
|
||||
|
||||
base_offset = args.max_ring_buffer_distance ~mod+ this.dict_total_size
|
||||
d = 0
|
||||
while d < this.dict_num_chunks {
|
||||
assert d < 15 via "a < b: a < c; c <= b"(c: this.dict_num_chunks)
|
||||
this.find_compound_dictionary_match!(data: args.data, dwb: args.dwb, d: d,
|
||||
cur_ix: args.cur_ix, max_length: args.max_length,
|
||||
distance_offset: base_offset ~mod- this.dict_chunk_offsets[d],
|
||||
max_distance: args.max_distance)
|
||||
d += 1
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors FindCompoundDictionaryMatch in hash.h, for the d'th chunk. The
|
||||
// search result is in (and starts from) the this.sr_etc fields.
|
||||
pri func encoder.find_compound_dictionary_match!(data: roslice base.u8, dwb: roslice base.u8, d: base.u32[..= 14], cur_ix: base.u64, max_length: base.u64, distance_offset: base.u64, max_distance: base.u64) {
|
||||
var source_size : base.u64
|
||||
var boundary : base.u64
|
||||
var src_off : base.u64
|
||||
var slot_off : base.u64
|
||||
var heads_off : base.u64
|
||||
var items_off : base.u64
|
||||
var source : roslice base.u8
|
||||
var cur : roslice base.u8
|
||||
var cur_ix_masked : base.u64
|
||||
var mask : base.u64
|
||||
var best_score : base.u64
|
||||
var best_len : base.u64
|
||||
var i : base.u32
|
||||
var h : base.u64
|
||||
var key : base.u64
|
||||
var slot : base.u64
|
||||
var head : base.u64
|
||||
var chain : base.u64
|
||||
var item : base.u32
|
||||
var distance : base.u64
|
||||
var offset : base.u64
|
||||
var limit : base.u64
|
||||
var len : base.u64
|
||||
var score : base.u64
|
||||
var num_items : base.u64
|
||||
var bb : base.u32[..= 22]
|
||||
var sb : base.u32[..= 12]
|
||||
|
||||
bb = this.dc_bucket_bits[args.d].min(no_more_than: 22)
|
||||
sb = this.dc_slot_bits[args.d].min(no_more_than: 12)
|
||||
num_items = this.dc_num_items[args.d]
|
||||
source_size = this.dc_size[args.d]
|
||||
boundary = args.distance_offset ~mod- source_size
|
||||
src_off = this.dc_src_off[args.d]
|
||||
slot_off = src_off ~mod+ source_size
|
||||
heads_off = slot_off ~mod+ ((4 as base.u64) << sb)
|
||||
items_off = heads_off ~mod+ ((2 as base.u64) << bb)
|
||||
if (src_off > slot_off) or (slot_off > args.dwb.length()) {
|
||||
return nothing
|
||||
}
|
||||
source = args.dwb[src_off .. slot_off]
|
||||
|
||||
mask = this.rb_mask as base.u64
|
||||
cur_ix_masked = args.cur_ix & mask
|
||||
if cur_ix_masked > args.data.length() {
|
||||
return nothing
|
||||
}
|
||||
cur = args.data[cur_ix_masked ..]
|
||||
best_score = this.sr_score
|
||||
best_len = this.sr_len
|
||||
h = (this.load64le(s: args.data, i: cur_ix_masked) & 0xFF_FFFF_FFFF) ~mod*
|
||||
PREPARED_DICTIONARY_HASH_MUL_64_LONG
|
||||
key = (h >> 32) >> (32 - bb)
|
||||
slot = key & (((1 as base.u64) << sb) - 1)
|
||||
head = this.peek_u16(s: args.dwb, i: heads_off ~mod+ (2 ~mod* key)) as base.u64
|
||||
chain = (this.load32le(s: args.dwb, i: slot_off ~mod+ (4 ~mod* slot)) as base.u64) ~mod+ head
|
||||
item = 0
|
||||
if head == 0xFFFF {
|
||||
item = 1
|
||||
}
|
||||
|
||||
i = 0
|
||||
while i < 4 {
|
||||
distance = this.dist_cache_u64(i: i)
|
||||
if (distance <= boundary) or (distance > args.distance_offset) {
|
||||
i ~mod+= 1
|
||||
continue
|
||||
}
|
||||
offset = args.distance_offset ~mod- distance
|
||||
limit = source_size ~mod- offset
|
||||
limit = limit.min(no_more_than: args.max_length)
|
||||
len = 0
|
||||
if offset <= source.length() {
|
||||
len = this.find_match_length_2(a: source[offset ..], b: cur, limit: limit)
|
||||
}
|
||||
if len >= 2 {
|
||||
score = this.backward_reference_score_using_last_distance(copy_length: len)
|
||||
if best_score < score {
|
||||
if i <> 0 {
|
||||
score ~mod-= this.backward_reference_penalty_using_last_distance(distance_short_code: i as base.u64)
|
||||
}
|
||||
if best_score < score {
|
||||
best_score = score
|
||||
if len > best_len {
|
||||
best_len = len
|
||||
}
|
||||
this.sr_len = len
|
||||
this.sr_len_code_delta = 0
|
||||
this.sr_distance = distance
|
||||
this.sr_score = best_score
|
||||
}
|
||||
}
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
// we require matches of len >4, so increase best_len to 3, so we can
|
||||
// compare 4 bytes all the time.
|
||||
if best_len < 3 {
|
||||
best_len = 3
|
||||
}
|
||||
while item == 0 {
|
||||
if chain >= num_items {
|
||||
// Unreachable: every chain ends with a 0x8000_0000 bit.
|
||||
break
|
||||
}
|
||||
item = this.load32le(s: args.dwb, i: items_off ~mod+ (4 ~mod* chain))
|
||||
chain ~mod+= 1
|
||||
offset = (item & 0x7FFF_FFFF) as base.u64
|
||||
item &= 0x8000_0000
|
||||
distance = args.distance_offset ~mod- offset
|
||||
limit = source_size ~mod- offset
|
||||
limit = limit.min(no_more_than: args.max_length)
|
||||
if distance > args.max_distance {
|
||||
continue
|
||||
}
|
||||
if ((cur_ix_masked ~mod+ best_len) > mask) or (best_len >= limit) or
|
||||
(this.load32le(s: args.data, i: (cur_ix_masked ~mod+ best_len) ~mod- 3) <>
|
||||
this.load32le(s: source, i: (offset ~mod+ best_len) ~mod- 3)) {
|
||||
continue
|
||||
}
|
||||
len = 0
|
||||
if offset <= source.length() {
|
||||
len = this.find_match_length_2(a: source[offset ..], b: cur, limit: limit)
|
||||
}
|
||||
if len >= 4 {
|
||||
score = this.backward_reference_score(copy_length: len, backward_reference_offset: distance)
|
||||
if best_score < score {
|
||||
best_score = score
|
||||
best_len = len
|
||||
this.sr_len = best_len
|
||||
this.sr_len_code_delta = 0
|
||||
this.sr_distance = distance
|
||||
this.sr_score = best_score
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// find_match_length_2 mirrors FindMatchLengthWithLimit in
|
||||
// find_match_length.h: the length of the common prefix of a and b, up to
|
||||
// limit bytes.
|
||||
pri func encoder.find_match_length_2(a: roslice base.u8, b: roslice base.u8, limit: base.u64) base.u64 {
|
||||
var matched : base.u64
|
||||
var n : base.u64
|
||||
var a : roslice base.u8
|
||||
var b : roslice base.u8
|
||||
var x : base.u64
|
||||
|
||||
a = args.a
|
||||
b = args.b
|
||||
n = args.limit
|
||||
while (n >= 8) and (a.length() >= 8) and (b.length() >= 8) {
|
||||
x = a.peek_u64le() ^ b.peek_u64le()
|
||||
if x <> 0 {
|
||||
while (x & 0xFF) == 0 {
|
||||
matched ~mod+= 1
|
||||
x >>= 8
|
||||
}
|
||||
return matched
|
||||
}
|
||||
matched ~mod+= 8
|
||||
n -= 8
|
||||
a = a[8 ..]
|
||||
b = b[8 ..]
|
||||
}
|
||||
while (n > 0) and (a.length() > 0) and (b.length() > 0) {
|
||||
if a[0] <> b[0] {
|
||||
break
|
||||
}
|
||||
matched ~mod+= 1
|
||||
n -= 1
|
||||
a = a[1 ..]
|
||||
b = b[1 ..]
|
||||
}
|
||||
return matched
|
||||
}
|
||||
|
||||
// extend_last_command_compound mirrors the compound dictionary branch of
|
||||
// ExtendLastCommand in encode.c: extending the last command's copy (of
|
||||
// distance cmd_dist, beyond max_distance) from the compound dictionary. It
|
||||
// returns how many bytes it extended the copy by.
|
||||
pri func encoder.extend_last_command_compound(wb: roslice base.u8, dwb: roslice base.u8, bytes: base.u64, wrapped_last_processed_pos: base.u64, cmd_dist: base.u64, max_distance: base.u64, last_copy_len: base.u64) base.u64 {
|
||||
var address : base.u64
|
||||
var br_index : base.u32
|
||||
var br_offset : base.u64
|
||||
var chunk_length : base.u64
|
||||
var n : base.u64
|
||||
var p : base.u64
|
||||
|
||||
if ((args.cmd_dist ~mod- args.max_distance) ~mod- 1) >= this.dict_total_size {
|
||||
return 0
|
||||
} else if args.last_copy_len >= (args.cmd_dist ~mod- args.max_distance) {
|
||||
return 0
|
||||
}
|
||||
address = (this.dict_total_size ~mod- (args.cmd_dist ~mod- args.max_distance)) ~mod+ args.last_copy_len
|
||||
br_index = 0
|
||||
while (br_index < 15) and (address >= this.dict_chunk_offsets[br_index + 1]) {
|
||||
br_index += 1
|
||||
}
|
||||
if br_index >= 15 {
|
||||
return 0
|
||||
}
|
||||
br_offset = address ~mod- this.dict_chunk_offsets[br_index]
|
||||
chunk_length = this.dict_chunk_offsets[br_index + 1] ~mod- this.dict_chunk_offsets[br_index]
|
||||
p = args.wrapped_last_processed_pos
|
||||
while n < args.bytes {
|
||||
// The C code reads past an empty chunk's end. Here, such a read
|
||||
// stops the extension (see README.md).
|
||||
if (br_offset >= chunk_length) or (br_index >= 15) {
|
||||
break
|
||||
}
|
||||
if this.rb_byte(wb: args.wb, pos: p) <>
|
||||
this.byte_at(s: args.dwb, i: this.dc_src_off[br_index] ~mod+ br_offset) {
|
||||
break
|
||||
}
|
||||
n ~mod+= 1
|
||||
p = (p ~mod+ 1) & 0xFFFF_FFFF
|
||||
br_offset ~mod+= 1
|
||||
if br_offset == chunk_length {
|
||||
br_index += 1
|
||||
br_offset = 0
|
||||
if (br_index < this.dict_num_chunks) and (br_index < 15) {
|
||||
chunk_length = this.dict_chunk_offsets[br_index + 1] ~mod- this.dict_chunk_offsets[br_index]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
Reference in new issue
Block a user