mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Merge claude/project-thread-0izcgq (large window at qualities 3 to 9)
Put the q10/q11 variable region before the ring buffer (now the last region), growing it (and moving the ring buffer) at the first transform_io call to use a workbuf longer than workbuf_len's minimum. Large window now also works at qualities 10 and 11.
This commit is contained in:
11 files changed
+1664
-216
No files matched your search
@@ -37,4 +37,10 @@ encoder has `request_flush`, `request_emit_metadata` and
|
||||
|
||||
## Implementations
|
||||
|
||||
- [std/brotli](/std/brotli/README.md#encoder) (qualities 0 to 11, without encoder dictionaries).
|
||||
- [std/brotli](/std/brotli/README.md#encoder) (qualities 0 to 11, without
|
||||
encoder dictionaries, including large-window brotli, with windows of up to
|
||||
1 GiB, at qualities 3 to 11). Its `workbuf_len` is the worst case for the
|
||||
parameters (more than 2 GiB for large-window brotli at lgwin 30; at
|
||||
qualities 10 and 11, `min_incl` is a smaller practical bound), but a
|
||||
shorter workbuf works for short inputs: see the README's "Encoder Workbuf
|
||||
Layout" section.
|
||||
@@ -10010,6 +10010,7 @@ struct wuffs_brotli__encoder__struct {
|
||||
uint32_t f_rb_total_size;
|
||||
uint32_t f_rb_cur_size;
|
||||
uint32_t f_rb_pos;
|
||||
bool f_rb_short_workbuf;
|
||||
uint32_t f_dist_alphabet_size_max;
|
||||
uint32_t f_dist_alphabet_size_limit;
|
||||
uint64_t f_dist_max_distance;
|
||||
@@ -10025,6 +10026,12 @@ struct wuffs_brotli__encoder__struct {
|
||||
uint64_t f_dict_num_lookups;
|
||||
uint64_t f_dict_num_matches;
|
||||
uint64_t f_fc_max_hops;
|
||||
uint32_t f_hr_jump;
|
||||
bool f_hr_fresh;
|
||||
uint32_t f_hr_state;
|
||||
uint32_t f_hr_factor_remove;
|
||||
uint64_t f_hr_next_ix;
|
||||
uint64_t f_hr_off;
|
||||
uint64_t f_sr_len;
|
||||
uint64_t f_sr_distance;
|
||||
uint64_t f_sr_score;
|
||||
@@ -10038,6 +10045,8 @@ struct wuffs_brotli__encoder__struct {
|
||||
uint64_t f_mb_rle_off;
|
||||
uint32_t f_literal_context_mode;
|
||||
uint64_t f_var_off;
|
||||
uint64_t f_var_end;
|
||||
bool f_hq_layout_fixed;
|
||||
bool f_hq_insufficient;
|
||||
uint64_t f_hq_wb_high;
|
||||
uint64_t f_h10_window_mask;
|
||||
@@ -76593,32 +76602,34 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_STORAGE 0u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_RB 1u
|
||||
#define WUFFS_BROTLI__LAYOUT_HASH 1u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_HASH 2u
|
||||
#define WUFFS_BROTLI__LAYOUT_CMDS 2u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_CMDS 3u
|
||||
#define WUFFS_BROTLI__LAYOUT_TYPES_0 3u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_TYPES_0 4u
|
||||
#define WUFFS_BROTLI__LAYOUT_LENGTHS_0 6u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_LENGTHS_0 7u
|
||||
#define WUFFS_BROTLI__LAYOUT_HISTO_0 9u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_HISTO_0 10u
|
||||
#define WUFFS_BROTLI__LAYOUT_COMB 12u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_COMB 13u
|
||||
#define WUFFS_BROTLI__LAYOUT_CMAP 13u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_CMAP 14u
|
||||
#define WUFFS_BROTLI__LAYOUT_RLE 14u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_RLE 15u
|
||||
#define WUFFS_BROTLI__LAYOUT_BE_DEPTH_0 15u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_BE_DEPTH_0 16u
|
||||
#define WUFFS_BROTLI__LAYOUT_BE_BITS_0 18u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_BE_BITS_0 19u
|
||||
#define WUFFS_BROTLI__LAYOUT_VAR 21u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_VAR 22u
|
||||
#define WUFFS_BROTLI__LAYOUT_RB 22u
|
||||
|
||||
#define WUFFS_BROTLI__LAYOUT_END 23u
|
||||
|
||||
#define WUFFS_BROTLI__RB_SHORT_SLACK 16u
|
||||
|
||||
#define WUFFS_BROTLI__MAX_NUM_DELAYED_SYMBOLS 12287u
|
||||
|
||||
#define WUFFS_BROTLI__F64_0_99 4607092346807469998u
|
||||
@@ -76661,6 +76672,16 @@ WUFFS_BROTLI__DICTIONARY[122784] WUFFS_BASE__POTENTIALLY_UNUSED = {
|
||||
|
||||
#define WUFFS_BROTLI__SCORE_BASE 1920u
|
||||
|
||||
#define WUFFS_BROTLI__ROLLING_CHUNKLEN 32u
|
||||
|
||||
#define WUFFS_BROTLI__ROLLING_NUMBUCKETS 16777216u
|
||||
|
||||
#define WUFFS_BROTLI__ROLLING_MASK 1073741823u
|
||||
|
||||
#define WUFFS_BROTLI__ROLLING_HASH_MUL_32 69069u
|
||||
|
||||
#define WUFFS_BROTLI__ROLLING_INVALID_POS 4294967295u
|
||||
|
||||
#define WUFFS_BROTLI__LC_F64_0_02905 4584030313835631162u
|
||||
|
||||
#define WUFFS_BROTLI__LC_F64_0_029 4584015902316823577u
|
||||
@@ -78851,6 +78872,42 @@ wuffs_brotli__encoder__find_longest_match_forgetful(
|
||||
uint64_t a_dictionary_distance,
|
||||
uint64_t a_max_distance);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__hasher_rolling_alloc_size(
|
||||
const wuffs_brotli__encoder* self);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_rolling_initialize(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_rolling_prepare(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_input_size,
|
||||
uint64_t a_data_off);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_rolling_stitch_to_previous_block(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_num_bytes,
|
||||
uint64_t a_position);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_rolling_find_longest_match(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_cur_ix,
|
||||
uint64_t a_max_length,
|
||||
uint64_t a_max_backward);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__bit_cost__histogram_clear(
|
||||
@@ -79549,6 +79606,20 @@ wuffs_brotli__encoder__workbuf_len_hq(
|
||||
uint32_t a_quality,
|
||||
uint32_t a_lgwin);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__hq_var_extra_max(
|
||||
const wuffs_brotli__encoder* self,
|
||||
uint32_t a_quality,
|
||||
uint32_t a_lgwin,
|
||||
uint32_t a_lgblock);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hq_extend_var_region(
|
||||
wuffs_brotli__encoder* self,
|
||||
uint64_t a_wb_len);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint8_t
|
||||
wuffs_brotli__encoder__byte_at(
|
||||
@@ -87604,13 +87675,13 @@ wuffs_brotli__encoder__workbuf_len(
|
||||
v_lgwin = wuffs_brotli__encoder__sanitized_lgwin(self, v_q);
|
||||
if (v_q <= 1u) {
|
||||
v_n = ((((uint64_t)(2u)) << v_lgwin) + 503u + 16u);
|
||||
} else if ((v_q <= 9u) && ((v_q == 2u) || (self->private_impl.f_p_large_window == 0u))) {
|
||||
} else if (v_q <= 9u) {
|
||||
v_n = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lgwin,
|
||||
wuffs_brotli__encoder__computed_lgblock(self, v_q, v_lgwin),
|
||||
23u);
|
||||
} else if ((v_q >= 10u) && (self->private_impl.f_p_large_window == 0u)) {
|
||||
} else {
|
||||
return wuffs_brotli__encoder__workbuf_len_hq(self, v_q, v_lgwin);
|
||||
}
|
||||
return wuffs_base__utility__make_range_ii_u64(v_n, v_n);
|
||||
@@ -88090,6 +88161,7 @@ wuffs_brotli__encoder__transform_io(
|
||||
uint64_t v_remaining = 0;
|
||||
bool v_injected = false;
|
||||
uint64_t v_copy_input_size = 0;
|
||||
uint64_t v_total = 0;
|
||||
|
||||
uint8_t* iop_a_dst = NULL;
|
||||
uint8_t* io0_a_dst WUFFS_BASE__POTENTIALLY_UNUSED = NULL;
|
||||
@@ -88129,12 +88201,13 @@ wuffs_brotli__encoder__transform_io(
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0;
|
||||
|
||||
wuffs_brotli__encoder__ensure_initialized(self);
|
||||
if (self->private_impl.f_large_window && (self->private_impl.f_quality > 2u)) {
|
||||
status = wuffs_base__make_status(wuffs_brotli__error__unsupported_encoder_quality);
|
||||
goto exit;
|
||||
} else if ((self->private_impl.f_quality >= 2u) && (((uint64_t)(a_workbuf.len)) < self->private_impl.f_workbuf_end)) {
|
||||
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
goto exit;
|
||||
wuffs_brotli__encoder__hq_extend_var_region(self, ((uint64_t)(a_workbuf.len)));
|
||||
if ((self->private_impl.f_quality >= 2u) && (((uint64_t)(a_workbuf.len)) < self->private_impl.f_workbuf_end)) {
|
||||
self->private_impl.f_rb_short_workbuf = true;
|
||||
if (((uint64_t)(a_workbuf.len)) < ((uint64_t)(self->private_impl.f_rb_buf_off + 16u))) {
|
||||
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
while (true) {
|
||||
if (self->private_impl.f_metadata_requested) {
|
||||
@@ -88232,6 +88305,13 @@ wuffs_brotli__encoder__transform_io(
|
||||
}
|
||||
if ((v_remaining != 0u) && (((uint64_t)(io2_a_src - iop_a_src)) > 0u)) {
|
||||
v_copy_input_size = wuffs_base__u64__min(v_remaining, ((uint64_t)(io2_a_src - iop_a_src)));
|
||||
if (self->private_impl.f_rb_short_workbuf) {
|
||||
v_total = ((uint64_t)(((uint64_t)(self->private_impl.f_input_pos + v_copy_input_size)) + 16u));
|
||||
if ((v_total > ((uint64_t)(self->private_impl.f_rb_size))) || (((uint64_t)(self->private_impl.f_rb_buf_off + v_total)) > ((uint64_t)(a_workbuf.len)))) {
|
||||
status = wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
goto exit;
|
||||
}
|
||||
}
|
||||
v_mark = ((uint64_t)(iop_a_src - io0_a_src));
|
||||
self->private_data.s_transform_io.scratch = v_copy_input_size;
|
||||
WUFFS_BASE__COROUTINE_SUSPENSION_POINT(4);
|
||||
@@ -89125,6 +89205,7 @@ wuffs_brotli__encoder__hasher_total_size(
|
||||
uint32_t v_htype = 0;
|
||||
uint32_t v_bucket_bits = 0;
|
||||
uint32_t v_block_bits = 0;
|
||||
uint64_t v_rolling = 0;
|
||||
|
||||
if (a_quality >= 10u) {
|
||||
return (524288u + wuffs_brotli__encoder__h10_alloc_size_1(self, a_lgwin));
|
||||
@@ -89155,7 +89236,16 @@ wuffs_brotli__encoder__hasher_total_size(
|
||||
}
|
||||
v_block_bits = (a_quality - 1u);
|
||||
}
|
||||
return ((uint64_t)(((uint64_t)(wuffs_brotli__encoder__hasher_alloc_size(self,
|
||||
v_rolling = 0u;
|
||||
if (a_lgwin > 24u) {
|
||||
if ((v_htype == 3u) || (v_htype == 54u)) {
|
||||
v_rolling = wuffs_brotli__encoder__hasher_rolling_alloc_size(self);
|
||||
} else if ((v_htype == 6u) || (v_htype == 68u)) {
|
||||
v_htype = 6u;
|
||||
v_rolling = wuffs_brotli__encoder__hasher_rolling_alloc_size(self);
|
||||
}
|
||||
}
|
||||
return ((uint64_t)(((uint64_t)(((uint64_t)(wuffs_brotli__encoder__hasher_alloc_size(self,
|
||||
v_htype,
|
||||
v_bucket_bits,
|
||||
v_block_bits,
|
||||
@@ -89167,7 +89257,7 @@ wuffs_brotli__encoder__hasher_total_size(
|
||||
v_htype,
|
||||
v_bucket_bits,
|
||||
v_block_bits,
|
||||
2u)));
|
||||
2u))) + v_rolling));
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hash_region_size
|
||||
@@ -89235,15 +89325,15 @@ wuffs_brotli__encoder__compute_layout_offset(
|
||||
v_size = 0u;
|
||||
if (v_i == 0u) {
|
||||
v_size = ((uint64_t)(((uint64_t)(((uint64_t)(2u * v_max_len)) + 503u)) + 16u));
|
||||
} else if (v_i == 1u) {
|
||||
v_size = ((uint64_t)(((uint64_t)((2u + (((uint64_t)(1u)) << wuffs_brotli__encoder__rb_bits(self, a_lgwin, a_lgblock))) + (((uint64_t)(1u)) << a_lgblock))) + 7u));
|
||||
} else if (v_i == 2u) {
|
||||
v_size = wuffs_brotli__encoder__hash_region_size(self, a_quality, a_lgwin);
|
||||
} else if (v_i == 3u) {
|
||||
v_size = ((uint64_t)(16u * v_cmds_cap));
|
||||
} else if ((a_quality >= 10u) && (v_i < 14u)) {
|
||||
v_size = 0u;
|
||||
} else if (v_i == 22u) {
|
||||
v_size = ((uint64_t)(((uint64_t)((2u + (((uint64_t)(1u)) << wuffs_brotli__encoder__rb_bits(self, a_lgwin, a_lgblock))) + (((uint64_t)(1u)) << a_lgblock))) + 7u));
|
||||
} else if (v_i == 1u) {
|
||||
v_size = wuffs_brotli__encoder__hash_region_size(self, a_quality, a_lgwin);
|
||||
} else if (v_i == 2u) {
|
||||
v_size = ((uint64_t)(16u * v_cmds_cap));
|
||||
} else if ((a_quality >= 10u) && (v_i < 13u)) {
|
||||
v_size = 0u;
|
||||
} else if (v_i == 21u) {
|
||||
if (a_quality >= 10u) {
|
||||
v_size = wuffs_brotli__encoder__hq_var_len(self,
|
||||
a_quality,
|
||||
@@ -89252,42 +89342,42 @@ wuffs_brotli__encoder__compute_layout_offset(
|
||||
false);
|
||||
}
|
||||
} else if (a_quality >= 4u) {
|
||||
if (v_i == 4u) {
|
||||
if (v_i == 3u) {
|
||||
v_size = ((v_max_len / 512u) + 1u);
|
||||
} else if (v_i == 5u) {
|
||||
} else if (v_i == 4u) {
|
||||
v_size = ((uint64_t)((v_cmds_cap / 1024u) + 1u));
|
||||
} else if (v_i == 6u) {
|
||||
} else if (v_i == 5u) {
|
||||
v_size = ((uint64_t)((v_cmds_cap / 512u) + 1u));
|
||||
} else if (v_i == 7u) {
|
||||
} else if (v_i == 6u) {
|
||||
v_size = (4u * ((v_max_len / 512u) + 1u));
|
||||
} else if (v_i == 8u) {
|
||||
} else if (v_i == 7u) {
|
||||
v_size = ((uint64_t)(4u * ((uint64_t)((v_cmds_cap / 1024u) + 1u))));
|
||||
} else if (v_i == 9u) {
|
||||
} else if (v_i == 8u) {
|
||||
v_size = ((uint64_t)(4u * ((uint64_t)((v_cmds_cap / 512u) + 1u))));
|
||||
} else if ((10u <= v_i) && (v_i < 13u)) {
|
||||
if (v_i == 10u) {
|
||||
} else if ((9u <= v_i) && (v_i < 12u)) {
|
||||
if (v_i == 9u) {
|
||||
v_size = 266240u;
|
||||
} else if (v_i == 11u) {
|
||||
} else if (v_i == 10u) {
|
||||
v_size = 723712u;
|
||||
} else {
|
||||
v_size = 559232u;
|
||||
}
|
||||
} else if (v_i == 13u) {
|
||||
} else if (v_i == 12u) {
|
||||
v_size = 26624u;
|
||||
} else if (v_i == 14u) {
|
||||
} else if (v_i == 13u) {
|
||||
v_size = 16384u;
|
||||
} else if (v_i == 15u) {
|
||||
} else if (v_i == 14u) {
|
||||
v_size = 65536u;
|
||||
} else if ((16u <= v_i) && (v_i < 22u)) {
|
||||
} else if ((15u <= v_i) && (v_i < 21u)) {
|
||||
v_num_histo = 256u;
|
||||
v_hlen = 256u;
|
||||
if ((v_i == 17u) || (v_i == 20u)) {
|
||||
if ((v_i == 16u) || (v_i == 19u)) {
|
||||
v_hlen = 704u;
|
||||
} else if ((v_i == 18u) || (v_i == 21u)) {
|
||||
} else if ((v_i == 17u) || (v_i == 20u)) {
|
||||
v_hlen = 544u;
|
||||
}
|
||||
v_size = ((uint64_t)(v_num_histo * v_hlen));
|
||||
if (v_i >= 19u) {
|
||||
if (v_i >= 18u) {
|
||||
v_size = ((uint64_t)(2u * v_size));
|
||||
}
|
||||
}
|
||||
@@ -89316,18 +89406,18 @@ wuffs_brotli__encoder__compute_layout(
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
1u);
|
||||
22u);
|
||||
self->private_impl.f_rb_buf_off = ((uint64_t)(self->private_impl.f_rb_off + 2u));
|
||||
self->private_impl.f_h_off0 = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
2u);
|
||||
1u);
|
||||
self->private_impl.f_cmds_off = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
3u);
|
||||
2u);
|
||||
self->private_impl.f_cmds_cap = wuffs_brotli__encoder__commands_capacity(self, v_q, v_lw, v_lb);
|
||||
v_c = 0u;
|
||||
while (v_c < 3u) {
|
||||
@@ -89335,49 +89425,50 @@ wuffs_brotli__encoder__compute_layout(
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
(4u + v_c));
|
||||
(3u + v_c));
|
||||
self->private_data.f_gbs_lengths_off[v_c] = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
(7u + v_c));
|
||||
(6u + v_c));
|
||||
self->private_data.f_gbs_histo_off_base[v_c] = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
(10u + v_c));
|
||||
(9u + v_c));
|
||||
self->private_data.f_be_depth_off[v_c] = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
(16u + v_c));
|
||||
(15u + v_c));
|
||||
self->private_data.f_be_bits_off[v_c] = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
(19u + v_c));
|
||||
(18u + v_c));
|
||||
v_c += 1u;
|
||||
}
|
||||
self->private_impl.f_mb_comb_off = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
13u);
|
||||
12u);
|
||||
self->private_impl.f_mb_cmap_off = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
14u);
|
||||
13u);
|
||||
self->private_impl.f_mb_rle_off = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
15u);
|
||||
14u);
|
||||
self->private_impl.f_var_off = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
v_lb,
|
||||
22u);
|
||||
21u);
|
||||
self->private_impl.f_var_end = self->private_impl.f_rb_off;
|
||||
self->private_impl.f_workbuf_end = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_q,
|
||||
v_lw,
|
||||
@@ -93068,6 +93159,15 @@ wuffs_brotli__encoder__choose_hasher(
|
||||
self->private_impl.f_h_num_last_distances = 10u;
|
||||
}
|
||||
}
|
||||
self->private_impl.f_hr_jump = 0u;
|
||||
if (self->private_impl.f_lgwin > 24u) {
|
||||
if ((self->private_impl.f_hasher_type == 3u) || (self->private_impl.f_hasher_type == 54u)) {
|
||||
self->private_impl.f_hr_jump = 4u;
|
||||
} else if ((self->private_impl.f_hasher_type == 6u) || (self->private_impl.f_hasher_type == 68u)) {
|
||||
self->private_impl.f_hasher_type = 6u;
|
||||
self->private_impl.f_hr_jump = 1u;
|
||||
}
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
@@ -93165,6 +93265,8 @@ wuffs_brotli__encoder__hasher_setup(
|
||||
self->private_impl.f_h_bucket_bits,
|
||||
self->private_impl.f_h_block_bits,
|
||||
1u)));
|
||||
self->private_impl.f_hr_off = self->private_impl.f_h_off2;
|
||||
self->private_impl.f_hr_fresh = true;
|
||||
self->private_impl.f_h_is_prepared = false;
|
||||
self->private_impl.f_h_is_setup = true;
|
||||
}
|
||||
@@ -93286,6 +93388,13 @@ wuffs_brotli__encoder__hasher_prepare(
|
||||
v_j += 1u;
|
||||
}
|
||||
}
|
||||
if (self->private_impl.f_hr_jump != 0u) {
|
||||
if (self->private_impl.f_hr_fresh) {
|
||||
self->private_impl.f_hr_fresh = false;
|
||||
wuffs_brotli__encoder__hasher_rolling_initialize(self, a_wb);
|
||||
}
|
||||
wuffs_brotli__encoder__hasher_rolling_prepare(self, a_wb, a_input_size, self->private_impl.f_rb_buf_off);
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
@@ -93463,6 +93572,9 @@ wuffs_brotli__encoder__hasher_stitch_to_previous_block(
|
||||
wuffs_brotli__encoder__hasher_store(self, a_wb, ((uint64_t)(a_position - 2u)));
|
||||
wuffs_brotli__encoder__hasher_store(self, a_wb, ((uint64_t)(a_position - 1u)));
|
||||
}
|
||||
if (self->private_impl.f_hr_jump != 0u) {
|
||||
wuffs_brotli__encoder__hasher_rolling_stitch_to_previous_block(self, a_wb, a_num_bytes, a_position);
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
@@ -93715,6 +93827,13 @@ wuffs_brotli__encoder__find_longest_match(
|
||||
a_dictionary_distance,
|
||||
a_max_distance);
|
||||
}
|
||||
if (self->private_impl.f_hr_jump != 0u) {
|
||||
wuffs_brotli__encoder__hasher_rolling_find_longest_match(self,
|
||||
a_wb,
|
||||
a_cur_ix,
|
||||
a_max_length,
|
||||
a_max_backward);
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
@@ -94311,6 +94430,177 @@ wuffs_brotli__encoder__find_longest_match_forgetful(
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hasher_rolling_alloc_size
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__hasher_rolling_alloc_size(
|
||||
const wuffs_brotli__encoder* self) {
|
||||
return (4u * ((uint64_t)(16777216u)));
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hasher_rolling_initialize
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_rolling_initialize(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb) {
|
||||
uint64_t v_i = 0;
|
||||
|
||||
self->private_impl.f_hr_state = 0u;
|
||||
self->private_impl.f_hr_next_ix = 0u;
|
||||
self->private_impl.f_hr_factor_remove = 1u;
|
||||
v_i = 0u;
|
||||
while (v_i < 32u) {
|
||||
self->private_impl.f_hr_factor_remove *= 69069u;
|
||||
v_i += ((uint64_t)(self->private_impl.f_hr_jump));
|
||||
}
|
||||
wuffs_brotli__encoder__fill_bytes(self,
|
||||
a_wb,
|
||||
self->private_impl.f_hr_off,
|
||||
wuffs_brotli__encoder__hasher_rolling_alloc_size(self),
|
||||
255u);
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hasher_rolling_prepare
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_rolling_prepare(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_input_size,
|
||||
uint64_t a_data_off) {
|
||||
uint64_t v_i = 0;
|
||||
|
||||
if (a_input_size < 32u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
self->private_impl.f_hr_state = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i < 32u) {
|
||||
self->private_impl.f_hr_state = ((uint32_t)(((uint32_t)(69069u * self->private_impl.f_hr_state)) + (((uint32_t)(wuffs_brotli__encoder__byte_at(self, a_wb, ((uint64_t)(a_data_off + v_i))))) + 1u)));
|
||||
v_i += ((uint64_t)(self->private_impl.f_hr_jump));
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hasher_rolling_stitch_to_previous_block
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_rolling_stitch_to_previous_block(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_num_bytes,
|
||||
uint64_t a_position) {
|
||||
uint64_t v_position = 0;
|
||||
uint64_t v_position_masked = 0;
|
||||
uint64_t v_available = 0;
|
||||
uint64_t v_jm1 = 0;
|
||||
uint64_t v_diff = 0;
|
||||
uint64_t v_mask = 0;
|
||||
|
||||
v_position = a_position;
|
||||
v_available = a_num_bytes;
|
||||
v_jm1 = ((uint64_t)(((uint64_t)(self->private_impl.f_hr_jump)) - 1u));
|
||||
if ((v_position & v_jm1) != 0u) {
|
||||
v_diff = ((uint64_t)(((uint64_t)(self->private_impl.f_hr_jump)) - (v_position & v_jm1)));
|
||||
if (v_diff > v_available) {
|
||||
v_available = 0u;
|
||||
} else {
|
||||
v_available -= v_diff;
|
||||
}
|
||||
v_position += v_diff;
|
||||
}
|
||||
v_mask = ((uint64_t)(self->private_impl.f_rb_mask));
|
||||
v_position_masked = (v_position & v_mask);
|
||||
if (v_available > ((uint64_t)(v_mask - v_position_masked))) {
|
||||
v_available = ((uint64_t)(v_mask - v_position_masked));
|
||||
}
|
||||
wuffs_brotli__encoder__hasher_rolling_prepare(self, a_wb, v_available, ((uint64_t)(self->private_impl.f_rb_buf_off + v_position_masked)));
|
||||
self->private_impl.f_hr_next_ix = v_position;
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hasher_rolling_find_longest_match
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hasher_rolling_find_longest_match(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_cur_ix,
|
||||
uint64_t a_max_length,
|
||||
uint64_t a_max_backward) {
|
||||
wuffs_base__slice_u8 v_data = {0};
|
||||
uint64_t v_mask = 0;
|
||||
uint64_t v_cur_ix_masked = 0;
|
||||
uint64_t v_jump = 0;
|
||||
uint64_t v_pos = 0;
|
||||
uint32_t v_code = 0;
|
||||
uint8_t v_rem = 0;
|
||||
uint8_t v_add = 0;
|
||||
uint64_t v_found_ix = 0;
|
||||
uint64_t v_backward = 0;
|
||||
uint64_t v_found_ix_masked = 0;
|
||||
uint64_t v_len = 0;
|
||||
uint64_t v_score = 0;
|
||||
uint64_t v_t = 0;
|
||||
|
||||
if (self->private_impl.f_rb_buf_off > ((uint64_t)(a_wb.len))) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_data = wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_rb_buf_off);
|
||||
v_mask = ((uint64_t)(self->private_impl.f_rb_mask));
|
||||
v_cur_ix_masked = (a_cur_ix & v_mask);
|
||||
v_jump = ((uint64_t)(self->private_impl.f_hr_jump));
|
||||
if ((a_cur_ix & ((uint64_t)(v_jump - 1u))) != 0u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
if (a_max_length < 32u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_pos = self->private_impl.f_hr_next_ix;
|
||||
while (v_pos <= a_cur_ix) {
|
||||
v_code = (self->private_impl.f_hr_state & 1073741823u);
|
||||
v_rem = wuffs_brotli__encoder__byte_at(self, v_data, (v_pos & v_mask));
|
||||
v_add = wuffs_brotli__encoder__byte_at(self, v_data, (((uint64_t)(v_pos + 32u)) & v_mask));
|
||||
v_found_ix = ((uint64_t)(4294967295u));
|
||||
self->private_impl.f_hr_state = ((uint32_t)(((uint32_t)(((uint32_t)(69069u * self->private_impl.f_hr_state)) + (((uint32_t)(v_add)) + 1u))) - ((uint32_t)(self->private_impl.f_hr_factor_remove * (((uint32_t)(v_rem)) + 1u)))));
|
||||
if (v_code < 16777216u) {
|
||||
v_t = ((uint64_t)(self->private_impl.f_hr_off + (4u * ((uint64_t)(v_code)))));
|
||||
v_found_ix = ((uint64_t)(wuffs_brotli__encoder__load32le(self, a_wb, v_t)));
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, v_t, ((uint32_t)(v_pos)));
|
||||
if ((v_pos == a_cur_ix) && (v_found_ix != ((uint64_t)(4294967295u)))) {
|
||||
v_backward = (((uint64_t)(a_cur_ix - v_found_ix)) & 4294967295u);
|
||||
if (v_backward <= a_max_backward) {
|
||||
v_found_ix_masked = (v_found_ix & v_mask);
|
||||
v_len = wuffs_brotli__encoder__find_match_length_with_limit(self,
|
||||
v_data,
|
||||
v_found_ix_masked,
|
||||
v_cur_ix_masked,
|
||||
a_max_length);
|
||||
if ((v_len >= 4u) && (v_len > self->private_impl.f_sr_len)) {
|
||||
v_score = wuffs_brotli__encoder__backward_reference_score(self, v_len, v_backward);
|
||||
if (v_score > self->private_impl.f_sr_score) {
|
||||
self->private_impl.f_sr_len = v_len;
|
||||
self->private_impl.f_sr_distance = v_backward;
|
||||
self->private_impl.f_sr_score = v_score;
|
||||
self->private_impl.f_sr_len_code_delta = 0u;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
v_pos += v_jump;
|
||||
}
|
||||
self->private_impl.f_hr_next_ix = ((uint64_t)(a_cur_ix + v_jump));
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.bit_cost.histogram_clear
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -97506,9 +97796,11 @@ wuffs_brotli__encoder__write_meta_block_hq(
|
||||
uint64_t v_cmds_hi = 0;
|
||||
uint32_t v_c = 0;
|
||||
uint32_t v_ds = 0;
|
||||
uint64_t v_var_hi = 0;
|
||||
|
||||
v_cmds_hi = ((uint64_t)(self->private_impl.f_cmds_off + ((uint64_t)(self->private_impl.f_num_cmds * 16u))));
|
||||
if ((self->private_impl.f_var_off > ((uint64_t)(a_wb.len))) ||
|
||||
v_var_hi = wuffs_base__u64__min(self->private_impl.f_var_end, ((uint64_t)(a_wb.len)));
|
||||
if ((self->private_impl.f_var_off > v_var_hi) ||
|
||||
(self->private_impl.f_rb_buf_off > ((uint64_t)(a_wb.len))) ||
|
||||
(self->private_impl.f_cmds_off > v_cmds_hi) ||
|
||||
(v_cmds_hi > ((uint64_t)(a_wb.len)))) {
|
||||
@@ -97518,7 +97810,7 @@ wuffs_brotli__encoder__write_meta_block_hq(
|
||||
wuffs_brotli__metablock_builder__set_params(&self->private_data.f_mb, self->private_impl.f_quality, self->private_impl.f_large_window, (self->private_impl.f_p_disable_lcm != 0u));
|
||||
wuffs_brotli__metablock_builder__init_distance_params(&self->private_data.f_mb, self->private_impl.f_distance_postfix_bits, self->private_impl.f_num_direct_distance_codes, self->private_impl.f_large_window);
|
||||
v_status = wuffs_brotli__metablock_builder__build_meta_block(&self->private_data.f_mb,
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_var_off),
|
||||
wuffs_base__slice_u8__subslice_ij(a_wb, self->private_impl.f_var_off, v_var_hi),
|
||||
wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_rb_buf_off),
|
||||
a_wrapped_last_flush_pos,
|
||||
((uint64_t)(self->private_impl.f_rb_mask)),
|
||||
@@ -97534,7 +97826,7 @@ wuffs_brotli__encoder__write_meta_block_hq(
|
||||
self->private_impl.f_hq_insufficient = true;
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
wuffs_brotli__metablock_builder__optimize_histograms(&self->private_data.f_mb, wuffs_base__slice_u8__subslice_i(a_wb, self->private_impl.f_var_off), self->private_data.f_mb.private_impl.f_dist_alphabet_size_limit);
|
||||
wuffs_brotli__metablock_builder__optimize_histograms(&self->private_data.f_mb, wuffs_base__slice_u8__subslice_ij(a_wb, self->private_impl.f_var_off, v_var_hi), self->private_data.f_mb.private_impl.f_dist_alphabet_size_limit);
|
||||
v_c = 0u;
|
||||
while (v_c < 3u) {
|
||||
v_ds = 256u;
|
||||
@@ -97791,23 +98083,68 @@ wuffs_brotli__encoder__workbuf_len_hq(
|
||||
uint32_t a_quality,
|
||||
uint32_t a_lgwin) {
|
||||
uint32_t v_lgblock = 0;
|
||||
uint64_t v_off = 0;
|
||||
uint64_t v_n = 0;
|
||||
|
||||
v_lgblock = wuffs_brotli__encoder__computed_lgblock(self, a_quality, a_lgwin);
|
||||
v_off = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
v_n = wuffs_brotli__encoder__compute_layout_offset(self,
|
||||
a_quality,
|
||||
a_lgwin,
|
||||
v_lgblock,
|
||||
22u);
|
||||
return wuffs_base__utility__make_range_ii_u64(wuffs_base__u64__sat_add(v_off, wuffs_brotli__encoder__hq_var_len(self,
|
||||
23u);
|
||||
return wuffs_base__utility__make_range_ii_u64(v_n, wuffs_base__u64__sat_add(v_n, wuffs_brotli__encoder__hq_var_extra_max(self, a_quality, a_lgwin, v_lgblock)));
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hq_var_extra_max
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__hq_var_extra_max(
|
||||
const wuffs_brotli__encoder* self,
|
||||
uint32_t a_quality,
|
||||
uint32_t a_lgwin,
|
||||
uint32_t a_lgblock) {
|
||||
uint64_t v_lo = 0;
|
||||
uint64_t v_hi = 0;
|
||||
|
||||
v_lo = wuffs_brotli__encoder__hq_var_len(self,
|
||||
a_quality,
|
||||
a_lgwin,
|
||||
v_lgblock,
|
||||
false)), wuffs_base__u64__sat_add(v_off, wuffs_brotli__encoder__hq_var_len(self,
|
||||
a_lgblock,
|
||||
false);
|
||||
v_hi = wuffs_brotli__encoder__hq_var_len(self,
|
||||
a_quality,
|
||||
a_lgwin,
|
||||
v_lgblock,
|
||||
true)));
|
||||
a_lgblock,
|
||||
true);
|
||||
if (v_hi <= v_lo) {
|
||||
return 0u;
|
||||
}
|
||||
return (v_hi - v_lo);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.hq_extend_var_region
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__hq_extend_var_region(
|
||||
wuffs_brotli__encoder* self,
|
||||
uint64_t a_wb_len) {
|
||||
uint64_t v_extra = 0;
|
||||
|
||||
if ((self->private_impl.f_quality < 10u) || self->private_impl.f_hq_layout_fixed) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
self->private_impl.f_hq_layout_fixed = true;
|
||||
if (a_wb_len <= self->private_impl.f_workbuf_end) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_extra = wuffs_brotli__encoder__hq_var_extra_max(self, self->private_impl.f_quality, self->private_impl.f_lgwin, self->private_impl.f_lgblock);
|
||||
v_extra = wuffs_base__u64__min(v_extra, (a_wb_len - self->private_impl.f_workbuf_end));
|
||||
self->private_impl.f_var_end += v_extra;
|
||||
self->private_impl.f_rb_off += v_extra;
|
||||
self->private_impl.f_rb_buf_off += v_extra;
|
||||
self->private_impl.f_workbuf_end += v_extra;
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_tester.bits_entropy
|
||||
@@ -99450,7 +99787,7 @@ wuffs_brotli__encoder__create_zopfli_backward_references(
|
||||
uint64_t v_n = 0;
|
||||
bool v_ok_offsets = false;
|
||||
|
||||
v_ok_offsets = wuffs_brotli__encoder__z_set_offsets(self, ((uint64_t)(a_wb.len)), a_num_bytes);
|
||||
v_ok_offsets = wuffs_brotli__encoder__z_set_offsets(self, wuffs_base__u64__min(self->private_impl.f_var_end, ((uint64_t)(a_wb.len))), a_num_bytes);
|
||||
if ( ! v_ok_offsets) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
}
|
||||
@@ -99500,7 +99837,7 @@ wuffs_brotli__encoder__create_hq_zopfli_backward_references(
|
||||
uint64_t v_n = 0;
|
||||
bool v_ok_offsets = false;
|
||||
|
||||
v_ok_offsets = wuffs_brotli__encoder__z_set_offsets(self, ((uint64_t)(a_wb.len)), a_num_bytes);
|
||||
v_ok_offsets = wuffs_brotli__encoder__z_set_offsets(self, wuffs_base__u64__min(self->private_impl.f_var_end, ((uint64_t)(a_wb.len))), a_num_bytes);
|
||||
if ( ! v_ok_offsets) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_workbuf_length);
|
||||
}
|
||||
|
||||
@@ -38,6 +38,13 @@
|
||||
// so that N processes can split the work.
|
||||
// -oneshot=W instead of the grid, only check one configuration per input
|
||||
// and quality: lgwin W, the whole input in one FINISH step.
|
||||
// -lw only run the large-window grid (BROTLI_PARAM_LARGE_WINDOW
|
||||
// with lgwin 10 to 30, see run_input_lw), at qualities 2 and
|
||||
// above.
|
||||
// -big instead of the usual inputs, check synthetic inputs of 3 to
|
||||
// 12 MiB (with the usual or, with -lw, the large-window grid)
|
||||
// and of 24 to 70 MiB (with a few large-window configurations
|
||||
// each), with repeats at distances of up to 40 MiB.
|
||||
//
|
||||
// The test/c/std/brotli.c test program (run by "wuffs test -mimic
|
||||
// std/brotli") checks a subset of this grid.
|
||||
@@ -80,8 +87,8 @@
|
||||
|
||||
#define MAX_STEPS (1 << 22)
|
||||
#define MAX_PARAMS 16
|
||||
#define MAX_INPUT (16 << 20)
|
||||
#define MAX_OUTPUT (64 << 20)
|
||||
#define MAX_INPUT (80 << 20)
|
||||
#define MAX_OUTPUT (96 << 20)
|
||||
|
||||
static uint32_t g_step_ops[MAX_STEPS];
|
||||
static size_t g_step_lens[MAX_STEPS];
|
||||
@@ -95,11 +102,17 @@ static uint8_t* g_workbuf; // Wuffs' workbuf.
|
||||
static size_t g_workbuf_len;
|
||||
|
||||
static uint64_t g_num_configs;
|
||||
static uint64_t g_num_short_checks;
|
||||
static uint64_t g_num_skipped;
|
||||
static const char g_too_large[] = "workbuf_len is too large";
|
||||
static size_t g_short_workbuf_len; // 0 means use workbuf_len.
|
||||
static size_t g_last_want_len;
|
||||
static uint64_t g_num_mismatches;
|
||||
|
||||
static uint32_t g_qmin = 0;
|
||||
static uint32_t g_qmax = 9;
|
||||
static uint32_t g_oneshot_lgwin = 0;
|
||||
static bool g_lw_only = false;
|
||||
|
||||
typedef struct {
|
||||
uint32_t keys[MAX_PARAMS];
|
||||
@@ -111,6 +124,9 @@ typedef struct {
|
||||
size_t metadata_len;
|
||||
size_t dst_chunk; // 0 means unlimited.
|
||||
uint64_t seed;
|
||||
// try_short is whether to also check (at qualities 2 to 11) Wuffs' output
|
||||
// with the shortest workbuf that should work: see short_workbuf_len.
|
||||
bool try_short;
|
||||
} config;
|
||||
|
||||
static void //
|
||||
@@ -139,6 +155,77 @@ fill_random(uint8_t* p, size_t n, uint64_t seed) {
|
||||
}
|
||||
}
|
||||
|
||||
// fill_mixed fills p[0 .. n] with a mixture of text (from a small
|
||||
// vocabulary, so that there are many matches), runs and binary noise.
|
||||
static void //
|
||||
fill_mixed(uint8_t* p, size_t n_max, uint64_t seed) {
|
||||
static const char* words[] = {"the ", "brotli ", "wuffs ", "of ",
|
||||
"and ", "data\n", "time ", "<a>",
|
||||
"encoder", ", ", "zebra ", "1234"};
|
||||
uint64_t state = seed;
|
||||
size_t n = 0;
|
||||
while (n < n_max) {
|
||||
uint64_t r = rand64(&state);
|
||||
if ((r & 0xFF) < 200) {
|
||||
const char* w = words[(r >> 8) % 12];
|
||||
size_t k = strlen(w);
|
||||
while (k-- && (n < n_max)) {
|
||||
p[n++] = (uint8_t)(*w++);
|
||||
}
|
||||
} else if ((r & 0xFF) < 230) {
|
||||
size_t k = (size_t)((r >> 8) % 300);
|
||||
while (k-- && (n < n_max)) {
|
||||
p[n++] = (uint8_t)(r >> 40);
|
||||
}
|
||||
} else {
|
||||
size_t k = (size_t)((r >> 8) % 2000);
|
||||
while (k-- && (n < n_max)) {
|
||||
p[n++] = (uint8_t)(rand64(&state) >> 56);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fill_far_repeats fills p[0 .. n] with 1 MiB blocks: most are fresh
|
||||
// pseudo-random bytes or fill_mixed data (with a per-block seed), but some
|
||||
// repeat (with sparse changes) the block that is near or far
|
||||
// (near_mib or far_mib MiB) before them, so that encoders with a large
|
||||
// enough window find matches at those distances. When far_mib is a multiple
|
||||
// of 4 and near_mib is not 2 modulo 4, the far copies' sources are fresh
|
||||
// blocks that are not also copied (or copies) nearer by.
|
||||
static void //
|
||||
fill_far_repeats(uint8_t* p, size_t n, size_t near_mib, size_t far_mib,
|
||||
uint64_t seed) {
|
||||
const size_t mib = 1 << 20;
|
||||
for (size_t b = 0; (b * mib) < n; b++) {
|
||||
uint8_t* q = p + (b * mib);
|
||||
size_t k = ((n - (b * mib)) < mib) ? (n - (b * mib)) : mib;
|
||||
size_t from = SIZE_MAX;
|
||||
if ((b >= near_mib) && ((b % 4) == 1)) {
|
||||
from = b - near_mib;
|
||||
} else if ((b >= far_mib) && ((b % 4) == 3)) {
|
||||
from = b - far_mib;
|
||||
}
|
||||
if (from != SIZE_MAX) {
|
||||
// A shifted copy: the distance is not a multiple of 1 MiB (but, for
|
||||
// odd b, it is a multiple of 4, which the HROLLING_FAST hasher, used
|
||||
// by H35 and H55, needs to find it).
|
||||
size_t shift = (size_t)(seed % 250) + (b * 7);
|
||||
if (b & 1) {
|
||||
shift *= 4;
|
||||
}
|
||||
memcpy(q, p + (from * mib) + shift, k);
|
||||
for (size_t i = 4093; i < k; i += 65521) {
|
||||
q[i] ^= 0x55;
|
||||
}
|
||||
} else if ((b % 3) == 0) {
|
||||
fill_random(q, k, seed + b);
|
||||
} else {
|
||||
fill_mixed(q, k, seed + b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool //
|
||||
add_step(uint32_t op, size_t len) {
|
||||
if (g_num_steps >= MAX_STEPS) {
|
||||
@@ -285,8 +372,11 @@ encode_wuffs(const config* c, size_t* have_len) {
|
||||
// Use the minimum, so that the qualities 10 and 11 practical bound is
|
||||
// exercised (at other qualities, min_incl == max_incl).
|
||||
uint64_t wlen = wuffs_brotli__encoder__workbuf_len(enc).min_incl;
|
||||
if (wlen > ((uint64_t)1 << 32)) {
|
||||
ret = "workbuf_len is too large";
|
||||
// Large-window brotli at qualities 10 and 11 with lgwin 30 needs about
|
||||
// 10.6 GiB (8 GiB of it for the H10 hasher's forest, as in the C code).
|
||||
// The memory is only touched as needed.
|
||||
if (wlen > ((uint64_t)12 << 30)) {
|
||||
ret = g_too_large;
|
||||
goto cleanup;
|
||||
} else if (wlen > g_workbuf_len) {
|
||||
free(g_workbuf);
|
||||
@@ -297,6 +387,9 @@ encode_wuffs(const config* c, size_t* have_len) {
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
if ((g_short_workbuf_len > 0) && (g_short_workbuf_len < wlen)) {
|
||||
wlen = g_short_workbuf_len;
|
||||
}
|
||||
wuffs_base__slice_u8 workbuf =
|
||||
wuffs_base__make_slice_u8(g_workbuf, (size_t)wlen);
|
||||
|
||||
@@ -418,6 +511,63 @@ describe(char* buf, size_t n, const char* name, const config* c) {
|
||||
}
|
||||
}
|
||||
|
||||
// short_workbuf_len returns the shortest workbuf (at qualities 2 to 9) that
|
||||
// should work for the config c and an input of len bytes, given that
|
||||
// workbuf_len is wlen, or 0 if it is not shorter than wlen. The ring buffer
|
||||
// is the workbuf's last region, of (2 + (1 << rb_bits) + (1 << lgblock) + 7)
|
||||
// bytes, and a shorter workbuf works if it holds (2 + len + 16) bytes of it
|
||||
// and (len + 16) is at most (1 << rb_bits). This mirrors SanitizeParams,
|
||||
// ComputeLgBlock and ComputeRbBits in quality.h.
|
||||
static size_t //
|
||||
short_workbuf_len(const config* c, size_t len, uint64_t wlen) {
|
||||
int32_t quality = 11;
|
||||
int32_t lgwin = 22;
|
||||
int32_t lgblock = 0;
|
||||
bool large_window = false;
|
||||
for (size_t j = 0; j < c->num_params; j++) {
|
||||
int32_t v = (int32_t)(c->values[j]);
|
||||
switch (c->keys[j]) {
|
||||
case 1:
|
||||
quality = v;
|
||||
break;
|
||||
case 2:
|
||||
lgwin = v;
|
||||
break;
|
||||
case 3:
|
||||
lgblock = v;
|
||||
break;
|
||||
case 6:
|
||||
large_window = v != 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
quality = (quality < 0) ? 0 : (quality > 11) ? 11 : quality;
|
||||
if (quality < 2) {
|
||||
return 0;
|
||||
}
|
||||
large_window = large_window && (quality > 2);
|
||||
int32_t max_lgwin = large_window ? 30 : 24;
|
||||
lgwin = (lgwin < 10) ? 10 : (lgwin > max_lgwin) ? max_lgwin : lgwin;
|
||||
if (quality < 4) {
|
||||
lgblock = 14;
|
||||
} else if (lgblock == 0) {
|
||||
lgblock = 16;
|
||||
if ((quality >= 9) && (lgwin > lgblock)) {
|
||||
lgblock = (lgwin < 18) ? lgwin : 18;
|
||||
}
|
||||
} else {
|
||||
lgblock = (lgblock < 16) ? 16 : (lgblock > 24) ? 24 : lgblock;
|
||||
}
|
||||
int32_t rb_bits = 1 + ((lgwin > lgblock) ? lgwin : lgblock);
|
||||
uint64_t rb_size = (uint64_t)1 << rb_bits;
|
||||
uint64_t rb_region = 2 + rb_size + ((uint64_t)1 << lgblock) + 7;
|
||||
if (((len + 16) > rb_size) || (rb_region > wlen)) {
|
||||
return 0;
|
||||
}
|
||||
uint64_t n = wlen - rb_region + 2 + len + 16;
|
||||
return (n < wlen) ? (size_t)n : 0;
|
||||
}
|
||||
|
||||
static void //
|
||||
check(const char* name, const config* c, const uint8_t* data, size_t len) {
|
||||
char desc[512];
|
||||
@@ -432,6 +582,50 @@ check(const char* name, const config* c, const uint8_t* data, size_t len) {
|
||||
size_t want_len = 0;
|
||||
const char* have_err = encode_wuffs(c, &have_len);
|
||||
const char* want_err = encode_mimic(c, &want_len);
|
||||
if (have_err == g_too_large) {
|
||||
// Only large-window brotli at qualities 10 and 11 with lgwin 30 and
|
||||
// lgblock 24 needs that much.
|
||||
g_num_skipped++;
|
||||
describe(desc, sizeof desc, name, c);
|
||||
printf("SKIP %s: %s\n", desc, have_err);
|
||||
return;
|
||||
}
|
||||
g_last_want_len = want_len;
|
||||
if (c->try_short && !have_err && !want_err) {
|
||||
// Also check the shortest workbuf that should work, and that one byte
|
||||
// less fails.
|
||||
wuffs_brotli__encoder* enc = wuffs_brotli__encoder__alloc();
|
||||
uint64_t wlen = 0;
|
||||
if (enc) {
|
||||
for (size_t j = 0; j < c->num_params; j++) {
|
||||
wuffs_brotli__encoder__set_quirk(enc, brotli_enc_quirk_key(c->keys[j]),
|
||||
c->values[j]);
|
||||
}
|
||||
wlen = wuffs_brotli__encoder__workbuf_len(enc).min_incl;
|
||||
free(enc);
|
||||
}
|
||||
size_t short_len = short_workbuf_len(c, len, wlen);
|
||||
if (short_len > 0) {
|
||||
g_num_short_checks++;
|
||||
g_short_workbuf_len = short_len - 1;
|
||||
size_t too_short_len = 0;
|
||||
const char* too_short_err = encode_wuffs(c, &too_short_len);
|
||||
g_short_workbuf_len = short_len;
|
||||
have_err = encode_wuffs(c, &have_len);
|
||||
g_short_workbuf_len = 0;
|
||||
if (!too_short_err ||
|
||||
(too_short_err != wuffs_base__error__bad_workbuf_length)) {
|
||||
g_num_mismatches++;
|
||||
describe(desc, sizeof desc, name, c);
|
||||
printf("MISMATCH %s: a workbuf of %zu bytes did not fail: %s\n",
|
||||
desc, short_len - 1, too_short_err ? too_short_err : "ok");
|
||||
fflush(stdout);
|
||||
return;
|
||||
} else if (have_err) {
|
||||
have_err = "short workbuf failed";
|
||||
}
|
||||
}
|
||||
}
|
||||
if (have_err || want_err) {
|
||||
g_num_mismatches++;
|
||||
describe(desc, sizeof desc, name, c);
|
||||
@@ -549,6 +743,95 @@ run_input_q01(const char* name, const uint8_t* data, size_t len, uint32_t q) {
|
||||
}
|
||||
}
|
||||
|
||||
// run_input_lw runs the large-window (BROTLI_PARAM_LARGE_WINDOW) grid for a
|
||||
// quality from 2 to 11. At quality 2 (and below), the C code ignores the
|
||||
// large-window parameter. At qualities 3 to 9, lgwin can be up to 30 and lgwin
|
||||
// above 24 selects the composite hashers (H35, H55 and H65) instead of H3,
|
||||
// H54, H6 and H68 (and only those: H5, H58 and H40 to H42 stay as they are).
|
||||
static void //
|
||||
run_input_lw(const char* name, const uint8_t* data, size_t len, uint32_t q) {
|
||||
static const size_t metadata_lens[] = {0, 1, 100, 70000};
|
||||
bool big = len > (512 << 10);
|
||||
|
||||
// lgwin and chunking.
|
||||
static const uint32_t lgwins[] = {10, 18, 24, 25, 26, 27, 28, 29, 30};
|
||||
for (size_t w = 0; w < 9; w++) {
|
||||
if (big && (lgwins[w] != 24) && (lgwins[w] != 25) && (lgwins[w] != 28) &&
|
||||
(lgwins[w] != 30)) {
|
||||
continue;
|
||||
}
|
||||
for (uint32_t ch = 0; ch < 5; ch += 4) {
|
||||
for (uint32_t fl = 0; fl < 3; fl += 2) {
|
||||
if (big && (fl == 0)) {
|
||||
continue;
|
||||
}
|
||||
config c = make_config(q, lgwins[w]);
|
||||
add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW.
|
||||
c.chunking = ch;
|
||||
c.flush = fl;
|
||||
c.seed = 0x10000 | (w << 8) | (ch << 4) | fl | (q << 12);
|
||||
c.try_short = true;
|
||||
check(name, &c, data, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Other parameters, alone and in combination.
|
||||
static const uint32_t extras[][6] = {
|
||||
// Pairs of (BrotliEncoderParameter, value). A zero key ends the list.
|
||||
{5, 1u << 20}, // BROTLI_PARAM_SIZE_HINT: H55 and H65.
|
||||
{5, 0xFFFFFFFF}, // Replaced by the exact size.
|
||||
{5, 1u << 30}, //
|
||||
{3, 16}, // BROTLI_PARAM_LGBLOCK.
|
||||
{3, 18}, //
|
||||
{3, 24, 5, 1u << 21}, // ... and a big size hint.
|
||||
{4, 1, 5, 1u << 20}, // DISABLE_LITERAL_CONTEXT_MODELING.
|
||||
{7, 3, 8, 120}, // BROTLI_PARAM_NPOSTFIX and NDIRECT.
|
||||
{7, 1, 8, 4, 5, 1u << 20}, //
|
||||
{0, 2}, // FONT mode.
|
||||
{0, 1, 5, 1u << 22}, // TEXT mode and a big size hint.
|
||||
{9, 1000}, // BROTLI_PARAM_STREAM_OFFSET.
|
||||
{9, 1u << 30, 5, 1u << 20},
|
||||
{2, 0xFFFFFFFF}, // LGWIN (int)-1, so 10.
|
||||
{2, 31}, // LGWIN 31, so 30.
|
||||
};
|
||||
for (size_t i = 0; i < sizeof extras / sizeof extras[0]; i++) {
|
||||
for (size_t w = 0; w < 2; w++) {
|
||||
static const uint32_t ws[] = {25, 30};
|
||||
if (big && (w != 0)) {
|
||||
continue;
|
||||
}
|
||||
config c = make_config(q, ws[w]);
|
||||
add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW.
|
||||
for (size_t k = 0; (k < 6) && ((k == 0) || extras[i][k]); k += 2) {
|
||||
uint32_t v = extras[i][k + 1];
|
||||
if ((extras[i][k] == 5) && (v == 0xFFFFFFFF)) {
|
||||
v = (uint32_t)len;
|
||||
}
|
||||
add_param(&c, extras[i][k], v);
|
||||
}
|
||||
c.chunking = 4;
|
||||
c.flush = 2;
|
||||
c.seed = 0x20000 | i | (w << 8) | (q << 12);
|
||||
c.try_short = true;
|
||||
check(name, &c, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
// Metadata blocks and limited dst lengths.
|
||||
for (size_t i = 0; i < 4; i += 3) {
|
||||
config c = make_config(q, 26);
|
||||
add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW.
|
||||
c.chunking = 4;
|
||||
c.flush = 2;
|
||||
c.metadata_position = 2;
|
||||
c.metadata_len = metadata_lens[i];
|
||||
c.dst_chunk = (len <= (32 << 10)) ? 3 : 4096;
|
||||
c.seed = 0x30000 | i | (q << 12);
|
||||
check(name, &c, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
// run_input_q2 runs the grid for a quality from 2 to 11.
|
||||
static void //
|
||||
run_input_q2(const char* name, const uint8_t* data, size_t len, uint32_t q) {
|
||||
@@ -657,17 +940,7 @@ run_input_q2(const char* name, const uint8_t* data, size_t len, uint32_t q) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (q == 2) {
|
||||
// Large window is supported (by the Wuffs encoder) only at quality 2.
|
||||
for (uint32_t w = 22; w <= 30; w += 4) {
|
||||
config c = make_config(q, w);
|
||||
add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW.
|
||||
c.chunking = 4;
|
||||
c.flush = 2;
|
||||
c.seed = w;
|
||||
check(name, &c, data, len);
|
||||
}
|
||||
}
|
||||
run_input_lw(name, data, len, q);
|
||||
if (len <= (256 << 10)) {
|
||||
static const size_t dst_chunks[] = {1, 2, 3, 7, 64, 4096};
|
||||
for (size_t i = 0; i < 6; i++) {
|
||||
@@ -695,6 +968,10 @@ run_input(const char* name, const uint8_t* data, size_t len) {
|
||||
if (g_oneshot_lgwin) {
|
||||
config c = make_config(q, g_oneshot_lgwin);
|
||||
check(name, &c, data, len);
|
||||
} else if (g_lw_only) {
|
||||
if (q >= 2) {
|
||||
run_input_lw(name, data, len, q);
|
||||
}
|
||||
} else if (q <= 1) {
|
||||
run_input_q01(name, data, len, q);
|
||||
} else {
|
||||
@@ -708,6 +985,41 @@ run_input(const char* name, const uint8_t* data, size_t len) {
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
// run_input_far checks a few large-window configurations (one-shot and
|
||||
// streaming) for a very long input.
|
||||
static void //
|
||||
run_input_far(const char* name, const uint8_t* data, size_t len) {
|
||||
uint64_t configs0 = g_num_configs;
|
||||
uint64_t mismatches0 = g_num_mismatches;
|
||||
time_t start = time(NULL);
|
||||
for (uint32_t q = (g_qmin > 2) ? g_qmin : 2; q <= g_qmax; q++) {
|
||||
static const uint32_t lgwins[] = {24, 25, 26, 30};
|
||||
for (size_t w = 0; w < 4; w++) {
|
||||
config c = make_config(q, lgwins[w]);
|
||||
add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW.
|
||||
c.seed = 0x40000 | (w << 8) | (q << 12);
|
||||
c.try_short = true;
|
||||
check(name, &c, data, len);
|
||||
printf(" q=%u lgwin=%u: %zu compressed bytes\n", q, lgwins[w],
|
||||
g_last_want_len);
|
||||
}
|
||||
// Streaming, with a flush and a size hint below 1 MiB (so H35 at
|
||||
// quality 3 and H58 or H5 at qualities 5 to 9, but H3 at quality 4).
|
||||
config c = make_config(q, 30);
|
||||
add_param(&c, 6, 1); // BROTLI_PARAM_LARGE_WINDOW.
|
||||
add_param(&c, 5, 12345); // BROTLI_PARAM_SIZE_HINT.
|
||||
c.chunking = 4;
|
||||
c.flush = 2;
|
||||
c.seed = 0x50000 | (q << 12);
|
||||
check(name, &c, data, len);
|
||||
}
|
||||
printf("%-50s %8zu bytes: %6" PRIu64 " configs, %" PRIu64
|
||||
" mismatches (%lds)\n",
|
||||
name, len, g_num_configs - configs0, g_num_mismatches - mismatches0,
|
||||
(long)(time(NULL) - start));
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
static uint8_t* //
|
||||
read_whole_file(const char* path, size_t* len) {
|
||||
FILE* f = fopen(path, "rb");
|
||||
@@ -746,6 +1058,7 @@ main(int argc, char** argv) {
|
||||
|
||||
int shard_k = 0;
|
||||
int shard_n = 1;
|
||||
bool big = false;
|
||||
int argi = 1;
|
||||
for (; argi < argc; argi++) {
|
||||
unsigned a = 0;
|
||||
@@ -755,6 +1068,10 @@ main(int argc, char** argv) {
|
||||
g_qmax = (b < 11) ? b : 11;
|
||||
} else if (sscanf(argv[argi], "-oneshot=%u", &a) == 1) {
|
||||
g_oneshot_lgwin = a;
|
||||
} else if (strcmp(argv[argi], "-lw") == 0) {
|
||||
g_lw_only = true;
|
||||
} else if (strcmp(argv[argi], "-big") == 0) {
|
||||
big = true;
|
||||
} else if (sscanf(argv[argi], "-shard=%u/%u", &a, &b) == 2) {
|
||||
shard_k = (int)a;
|
||||
shard_n = (b > 0) ? (int)b : 1;
|
||||
@@ -799,6 +1116,42 @@ main(int argc, char** argv) {
|
||||
}
|
||||
|
||||
int input_index = 0;
|
||||
if (big) {
|
||||
// Synthetic inputs of several MiB (with the usual grid) and of 24 to 70
|
||||
// MiB (with run_input_far's configurations), with repeats at distances
|
||||
// above 16 MiB (1 << 24, the largest non-large-window window).
|
||||
num_names = 0;
|
||||
uint8_t* p = malloc(70 << 20);
|
||||
if (!p) {
|
||||
return 1;
|
||||
}
|
||||
static const struct {
|
||||
size_t mib;
|
||||
size_t near_mib;
|
||||
size_t far_mib;
|
||||
bool far;
|
||||
} bigs[] = {
|
||||
{3, 2, 2, false}, {6, 2, 5, false}, {12, 3, 10, false},
|
||||
{24, 3, 20, true}, {40, 3, 32, true}, {70, 5, 40, true},
|
||||
};
|
||||
for (size_t j = 0; j < sizeof bigs / sizeof bigs[0]; j++) {
|
||||
if (((input_index++) % shard_n) != shard_k) {
|
||||
continue;
|
||||
}
|
||||
size_t n = (bigs[j].mib << 20) - 12345;
|
||||
fill_far_repeats(p, n, bigs[j].near_mib, bigs[j].far_mib, 0x777 + j);
|
||||
char name[96];
|
||||
snprintf(name, sizeof name,
|
||||
"synthetic: %zu MiB with repeats at %zu and %zu MiB",
|
||||
bigs[j].mib, bigs[j].near_mib, bigs[j].far_mib);
|
||||
if (bigs[j].far) {
|
||||
run_input_far(name, p, n);
|
||||
} else {
|
||||
run_input(name, p, n);
|
||||
}
|
||||
}
|
||||
free(p);
|
||||
}
|
||||
for (int i = 0; i < num_names; i++) {
|
||||
if (((input_index++) % shard_n) != shard_k) {
|
||||
continue;
|
||||
@@ -813,7 +1166,7 @@ main(int argc, char** argv) {
|
||||
free(data);
|
||||
}
|
||||
|
||||
if (argi >= argc) {
|
||||
if ((argi >= argc) && !big) {
|
||||
// Synthetic inputs.
|
||||
uint8_t* p = malloc(1 << 20);
|
||||
if (!p) {
|
||||
@@ -836,34 +1189,8 @@ main(int argc, char** argv) {
|
||||
run_input("synthetic: a repeated 100-byte pattern", p, 64 << 10);
|
||||
}
|
||||
if (((input_index++) % shard_n) == shard_k) {
|
||||
// A mixture of text (from a small vocabulary, so that there are many
|
||||
// matches), runs and binary noise.
|
||||
static const char* words[] = {"the ", "brotli ", "wuffs ", "of ",
|
||||
"and ", "data\n", "time ", "<a>",
|
||||
"encoder", ", ", "zebra ", "1234"};
|
||||
uint64_t state = 0xABCD;
|
||||
size_t n = 0;
|
||||
while (n < (600 << 10)) {
|
||||
uint64_t r = rand64(&state);
|
||||
if ((r & 0xFF) < 200) {
|
||||
const char* w = words[(r >> 8) % 12];
|
||||
size_t k = strlen(w);
|
||||
while (k-- && (n < (600 << 10))) {
|
||||
p[n++] = (uint8_t)(*w++);
|
||||
}
|
||||
} else if ((r & 0xFF) < 230) {
|
||||
size_t k = (size_t)((r >> 8) % 300);
|
||||
while (k-- && (n < (600 << 10))) {
|
||||
p[n++] = (uint8_t)(r >> 40);
|
||||
}
|
||||
} else {
|
||||
size_t k = (size_t)((r >> 8) % 2000);
|
||||
while (k-- && (n < (600 << 10))) {
|
||||
p[n++] = (uint8_t)(rand64(&state) >> 56);
|
||||
}
|
||||
}
|
||||
}
|
||||
run_input("synthetic: 600 KiB of mixed text and binary", p, n);
|
||||
fill_mixed(p, 600 << 10, 0xABCD);
|
||||
run_input("synthetic: 600 KiB of mixed text and binary", p, 600 << 10);
|
||||
}
|
||||
for (size_t n = 0; n <= 40; n++) {
|
||||
if (((input_index++) % shard_n) != shard_k) {
|
||||
@@ -877,7 +1204,9 @@ main(int argc, char** argv) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
printf("TOTAL: %" PRIu64 " configs, %" PRIu64 " mismatches\n", g_num_configs,
|
||||
g_num_mismatches);
|
||||
printf("TOTAL: %" PRIu64 " configs (%" PRIu64
|
||||
" also with a short workbuf, %" PRIu64
|
||||
" skipped as too large), %" PRIu64 " mismatches\n",
|
||||
g_num_configs, g_num_short_checks, g_num_skipped, g_num_mismatches);
|
||||
return g_num_mismatches ? 1 : 0;
|
||||
}
|
||||
+154
-36
@@ -154,8 +154,7 @@ 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 and large-window encoding at qualities 3 and above
|
||||
(see the "Encoder" section below).
|
||||
- Encoder dictionaries (see the "Encoder" section below).
|
||||
|
||||
|
||||
## Encoder
|
||||
@@ -182,11 +181,9 @@ literal context modeling heuristics, `BrotliBuildMetaBlockGreedy`,
|
||||
hasher, `BrotliCreateZopfliBackwardReferences` at quality 10 and
|
||||
`BrotliCreateHqZopfliBackwardReferences` at quality 11, and then
|
||||
`ChooseContextMode`, `BrotliBuildMetaBlock`, `BrotliOptimizeHistograms` and
|
||||
`BrotliStoreMetaBlock`). For large-window brotli at qualities 3 and above,
|
||||
`transform_io` returns `"#unsupported encoder quality"` (and `workbuf_len`
|
||||
is 0). Large-window
|
||||
brotli works at quality 2 (and is ignored at qualities 0 and 1, like the C
|
||||
code).
|
||||
`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.
|
||||
|
||||
The source files are:
|
||||
|
||||
@@ -217,6 +214,10 @@ The source files are:
|
||||
H54, H5, H6, H58, H68, H40, H41 and H42) and `SearchInStaticDictionary`
|
||||
(with `static_dict.c`'s matching), as one set of functions selected by the
|
||||
hasher type at run time.
|
||||
- `encode_hasher_composite.wuffs`: the large-window hashers:
|
||||
`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_backward_references.wuffs`: `backward_references.c` and
|
||||
`backward_references_inc.h` (`BrotliCreateBackwardReferences`),
|
||||
`command.h` and `ComputeDistanceCode`.
|
||||
@@ -306,7 +307,7 @@ first `transform_io` call, after which `set_quirk` returns
|
||||
| `QUIRK_MODE` | `BROTLI_PARAM_MODE` |
|
||||
| `QUIRK_DISABLE_LITERAL_CONTEXT_MODELING` | `BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING` (0 or 1) |
|
||||
| `QUIRK_SIZE_HINT` | `BROTLI_PARAM_SIZE_HINT` |
|
||||
| `QUIRK_LARGE_WINDOW` (shared with the decoder) | `BROTLI_PARAM_LARGE_WINDOW` |
|
||||
| `QUIRK_LARGE_WINDOW` (shared with the decoder) | `BROTLI_PARAM_LARGE_WINDOW` (0 or 1) |
|
||||
| `QUIRK_NPOSTFIX` | `BROTLI_PARAM_NPOSTFIX` |
|
||||
| `QUIRK_NDIRECT` | `BROTLI_PARAM_NDIRECT` |
|
||||
| `QUIRK_STREAM_OFFSET` | `BROTLI_PARAM_STREAM_OFFSET` (at most `1 << 30`) |
|
||||
@@ -316,7 +317,8 @@ with `base.QUIRK_QUALITY`. At qualities 0 and 1, only the quality, lgwin and
|
||||
stream offset affect the output (lgblock, mode, size hint, large window,
|
||||
npostfix, ndirect and literal context modeling are stored, sanitized and then
|
||||
ignored, as in the C code). At qualities 2 to 9, they have the same effect as
|
||||
in the C code: lgblock (at qualities 4 and above; the C code's
|
||||
in the C code: large window (see "Encoder Large Window" below), lgblock (at
|
||||
qualities 4 and above; the C code's
|
||||
`ComputeLgBlock` fixes it to 14 below that), npostfix, ndirect and the mode
|
||||
(only `BROTLI_MODE_FONT` matters, through `ChooseDistanceParams`, at
|
||||
qualities 4 and above), disabling literal context modeling (at qualities 5
|
||||
@@ -342,17 +344,15 @@ code's `ComputeRbBits`), `max_len` is `1 << min(rb_bits, 24)` (the C code's
|
||||
|
||||
1. The metablock storage: `(2 * max_len) + 503 + 16` bytes, like at
|
||||
qualities 0 and 1 but for the largest metablock.
|
||||
2. The ring buffer (the C code's `RingBuffer.buffer_`): `2 + (1 << rb_bits) +
|
||||
(1 << lgblock) + 7` bytes. The first 2 bytes are the copy of the last two
|
||||
bytes that precede `data_`, the tail (`1 << lgblock` bytes) mirrors the
|
||||
start of the buffer and the 7 trailing bytes are the C code's slack for
|
||||
8-byte loads.
|
||||
3. The hasher's memory (the C code's `common->extra[0 .. 3]`): the sum of
|
||||
2. The hasher's memory (the C code's `common->extra[0 .. 3]`): the sum of
|
||||
`FN(HashMemAllocInBytes)` for the hasher that `ChooseHasher` picks. When
|
||||
the size hint quirk is unset (zero), that choice depends on the input's
|
||||
length (through `UpdateSizeHint`), so this is the larger of the two
|
||||
possible choices (for a size hint below or at least 1 MiB).
|
||||
4. The commands: `16 * cmds_cap` bytes, in the C struct order: `insert_len_`
|
||||
possible choices (for a size hint below or at least 1 MiB). With lgwin
|
||||
above 24 (large window), the composite hashers H35, H55 and H65 add their
|
||||
rolling hasher's table (`(1 << 24)` u32le values, 64 MiB) after H3's,
|
||||
H54's or H6's memory.
|
||||
3. The commands: `16 * cmds_cap` bytes, in the C struct order: `insert_len_`
|
||||
(u32le), `copy_len_` (u32le, the copy length in the low 25 bits and
|
||||
`copy_code - copy_len` in the high 7 bits), `dist_extra_` (u32le),
|
||||
`cmd_prefix_` (u16le) and `dist_prefix_` (u16le). `cmds_cap` is
|
||||
@@ -362,7 +362,7 @@ code's `ComputeRbBits`), `max_len` is `1 << min(rb_bits, 24)` (the C code's
|
||||
input block) fewer than that many commands and, like the C code's
|
||||
`num_commands_ + bytes / 2 + 1` allocation, each input block of `n` bytes
|
||||
adds at most `(n / 2) + 1` commands.
|
||||
5. At qualities 4 and above, `BrotliBuildMetaBlockGreedy`'s `MetaBlockSplit`
|
||||
4. At qualities 4 and above, `BrotliBuildMetaBlockGreedy`'s `MetaBlockSplit`
|
||||
and `BrotliStoreMetaBlock`'s scratch space:
|
||||
- the block types (u8) and then the block lengths (u32le) of the literal,
|
||||
command and distance block splits, with room for `(max_len / 512) + 1`,
|
||||
@@ -378,13 +378,39 @@ code's `ComputeRbBits`), `max_len` is `1 << min(rb_bits, 24)` (the C code's
|
||||
- `EncodeContextMap`'s `rle_symbols` (256 * 64 u32le values) and
|
||||
- the block encoders' depths (u8) and then bits (u16le) for up to 256
|
||||
histograms per category.
|
||||
5. The ring buffer (the C code's `RingBuffer.buffer_`): `2 + (1 << rb_bits) +
|
||||
(1 << lgblock) + 7` bytes. The first 2 bytes are the copy of the last two
|
||||
bytes that precede `data_`, the tail (`1 << lgblock` bytes) mirrors the
|
||||
start of the buffer and the 7 trailing bytes are the C code's slack for
|
||||
8-byte loads.
|
||||
|
||||
Unlike the C code, which allocates its storage and command buffers as it
|
||||
goes (for the actual metablock and block sizes), `workbuf_len` is the worst
|
||||
case for the sanitized parameters, so that one workbuf works for any input.
|
||||
At qualities 2 to 9, `transform_io` returns `"#bad workbuf length"` if the
|
||||
workbuf is shorter than that. Some examples (in MiB, with the default
|
||||
lgblock and an unset size hint):
|
||||
Unlike the C code, which allocates its storage, ring buffer and command
|
||||
buffers as it goes (for the actual metablock and block sizes, and the actual
|
||||
input length), `workbuf_len` is the worst case for the sanitized parameters,
|
||||
so that one workbuf works for any input. A shorter workbuf still works for
|
||||
short inputs, because the ring buffer is the last region and the encoder
|
||||
only touches its first `2 + n + 7` bytes for an input of `n` bytes, as long
|
||||
as the ring buffer never wraps around. Precisely, at qualities 2 to 11
|
||||
(where, at qualities 10 and 11, `workbuf_len` means its `min_incl`):
|
||||
|
||||
- `transform_io` returns `"#bad workbuf length"` if the workbuf is shorter
|
||||
than `workbuf_len - R + 18`, where `R` is the ring buffer region's length
|
||||
(`2 + (1 << rb_bits) + (1 << lgblock) + 7`), i.e. if it does not reach 16
|
||||
bytes into the ring buffer's data.
|
||||
- If any `transform_io` call gets a workbuf shorter than `workbuf_len`, then
|
||||
from then on (for the rest of the stream), before copying input into the
|
||||
ring buffer, `transform_io` returns `"#bad workbuf length"` (without
|
||||
consuming that input) if the total input so far (not counting metadata)
|
||||
would be `n` bytes with `n + 16 > (1 << rb_bits)` (the ring buffer would
|
||||
wrap, or its tail would be needed) or with the workbuf shorter than
|
||||
`workbuf_len - R + 2 + n + 16`.
|
||||
|
||||
So, for a total input of `n` bytes with `n + 16 <= (1 << rb_bits)`, a
|
||||
workbuf of `workbuf_len - R + n + 18` bytes produces the same output as one
|
||||
of `workbuf_len` bytes. The parity test checks this with exactly that length
|
||||
(and that one byte less fails). This matters most for large-window brotli,
|
||||
whose ring buffer is up to 2 GiB. Some examples of `workbuf_len` (in MiB,
|
||||
with the default lgblock and an unset size hint):
|
||||
|
||||
| quality | lgwin 10 | lgwin 16 | lgwin 18 | lgwin 22 | lgwin 24 |
|
||||
| ------- | -------- | -------- | -------- | -------- | -------- |
|
||||
@@ -395,6 +421,21 @@ lgblock and an unset size hint):
|
||||
| 7 | 4.4 | 4.4 | 13.8 | 51.4 | 107.5 |
|
||||
| 9 | 5.1 | 5.1 | 39.5 | 77.1 | 133.2 |
|
||||
|
||||
With the large-window quirk (at qualities 3 and above; lgwin up to 24 gives
|
||||
the same numbers as without it), larger lgwin values double the ring buffer
|
||||
for each extra bit, and lgwin above 24 adds the composite hashers' 64 MiB
|
||||
rolling hash table (at quality 4 and at qualities 5 to 9 with lgwin of at
|
||||
least 19, only for a size hint of at least 1 MiB, or an unset one, as
|
||||
`ChooseHasher` otherwise keeps H3, H5 or H58):
|
||||
|
||||
| quality | lgwin 25 | lgwin 26 | lgwin 28 | lgwin 30 |
|
||||
| ------- | -------- | -------- | -------- | -------- |
|
||||
| 3 | 160.6 | 224.6 | 608.6 | 2144.6 |
|
||||
| 4 | 199.4 | 263.4 | 647.4 | 2183.4 |
|
||||
| 5 | 197.5 | 261.5 | 645.5 | 2181.5 |
|
||||
| 7 | 203.5 | 267.5 | 651.5 | 2187.5 |
|
||||
| 9 | 229.2 | 293.2 | 677.2 | 2213.2 |
|
||||
|
||||
At qualities 10 and 11, `workbuf_len` is a range instead (see the next
|
||||
section).
|
||||
|
||||
@@ -413,11 +454,19 @@ copied into the ring buffer.
|
||||
At qualities 10 and 11, the workbuf has the same regions as at quality 9
|
||||
(with `cmds_cap` computed the same way), except that:
|
||||
|
||||
- region 3 is the H10 hasher: `4 << 17` bytes of buckets (u32le) and then
|
||||
the binary tree forest (`8 << lgwin` bytes, u32le), and
|
||||
- region 5 keeps only the literal context map, `rle_symbols` and the block
|
||||
encoders' depths and bits, followed by a variable region, which runs from
|
||||
there to the end of the workbuf (however long that is).
|
||||
- region 2 is the H10 hasher: `4 << 17` bytes of buckets (u32le) and then
|
||||
the binary tree forest (`8 << lgwin` bytes, u32le),
|
||||
- region 4 keeps only the literal context map, `rle_symbols` and the block
|
||||
encoders' depths and bits, followed by a variable region, and
|
||||
- the ring buffer (region 5) is still the last region, after the variable
|
||||
region.
|
||||
|
||||
The variable region's length is set at the first `transform_io` call: it is
|
||||
its minimum length (see below) plus however much of the workbuf beyond
|
||||
`workbuf_len().min_incl` there is (up to the worst case), and the ring
|
||||
buffer moves along with it. Later `transform_io` calls should pass a
|
||||
workbuf of the same length. A workbuf shorter than `min_incl` follows the
|
||||
short workbuf rules above (with the minimum variable region).
|
||||
|
||||
The variable region holds, at different times, the Zopfli scratch space
|
||||
(while `EncodeData` computes the backward references: `(n + 1)` 16-byte
|
||||
@@ -431,8 +480,8 @@ The C code allocates these as it goes, for the actual input, and their worst
|
||||
cases are huge (see `workbuf_len_worst_case` below), so `workbuf_len` returns
|
||||
a range whose `max_incl` is the C code's worst case for the sanitized
|
||||
parameters (saturating at `0xFFFF_FFFF_FFFF_FFFF`) and whose `min_incl` is a
|
||||
practical bound. Both have the same fixed regions, followed by a variable
|
||||
region of `max(Z, M)` bytes, where `L` is the most literals that a metablock
|
||||
practical bound. Both have the same fixed regions, with a variable region of
|
||||
`max(Z, M)` bytes, where `L` is the most literals that a metablock
|
||||
can hold (`min(max_len, (max_len / 8) + (1 << lgblock))`, since `EncodeData`
|
||||
merges another input block only while there are fewer than `max_len / 8`
|
||||
literals) and:
|
||||
@@ -531,20 +580,73 @@ encoder makes the same `EncodeData` calls, at the same points. One
|
||||
emitting the flint, and its callers then call it again.
|
||||
|
||||
|
||||
### Encoder Large Window
|
||||
|
||||
The `QUIRK_LARGE_WINDOW` quirk mirrors `BROTLI_PARAM_LARGE_WINDOW`
|
||||
(large-window brotli is an incompatible extension of RFC 7932, decodable by
|
||||
this package's decoder with the same quirk, see "Large Window" above, and by
|
||||
libbrotlidec with `BROTLI_DECODER_PARAM_LARGE_WINDOW`). Like the C code's
|
||||
`SanitizeParams`, `EnsureInitialized` and `ChooseHasher`:
|
||||
|
||||
- At qualities 0 to 2 (the C code's `MAX_QUALITY_FOR_STATIC_ENTROPY_CODES`
|
||||
and below), the quirk is ignored: lgwin is clamped to 24 and the output is
|
||||
standard brotli.
|
||||
- At qualities 3 and above, lgwin is clamped to `[10 ..= 30]` (instead of
|
||||
`[10 ..= 24]`) and the stream starts with the 14-bit large-window header
|
||||
(`EncodeWindowBits`), even for lgwin 24 and below. The distance parameters
|
||||
(`BrotliInitDistanceParams` with `large_window`) allow distances up to
|
||||
`BROTLI_MAX_ALLOWED_DISTANCE` with an alphabet of up to
|
||||
`BROTLI_LARGE_MAX_DISTANCE_BITS` (62) distance bits, so the distance
|
||||
Huffman codes are stored with a larger alphabet size than without the
|
||||
quirk, even when no distance is large. Backward distances are at most
|
||||
`(1 << lgwin) - 16`, so their extra bits (at most 30) fit in a command's
|
||||
32-bit `dist_extra_`, as in the C code.
|
||||
- lgwin above 24 changes the hasher (only at qualities 3 to 9; the C code's
|
||||
qualities 10 and 11 use H10 regardless): H3 becomes H35 (H3 plus
|
||||
`HROLLING_FAST`), H54 becomes H55 (H54 plus `HROLLING_FAST`) and H6 and
|
||||
H68 become H65 (H6, not H68, plus `HROLLING`). H5, H58 and H40 to H42 stay
|
||||
as they are, so qualities 5 to 9 only use H65 when the size hint (set or
|
||||
estimated by `UpdateSizeHint`) is at least 1 MiB (and lgwin is at least 19,
|
||||
which it is). The rolling hashers find matches of at least 32 bytes at any
|
||||
distance in the window (`HROLLING_FAST` only at positions and distances
|
||||
that are multiples of 4) and do not search the static dictionary.
|
||||
- lgblock is unaffected (it is at most 24), but `rb_bits` (and so the ring
|
||||
buffer) grows with lgwin. Metablocks are still at most `1 << 24` bytes.
|
||||
- At qualities 10 and 11, the H10 hasher's binary tree forest grows with
|
||||
lgwin (`8 << lgwin` bytes, so 8 GiB at lgwin 30, as in the C code, which
|
||||
allocates the same), and the Zopfli and static dictionary distance limits
|
||||
use the large-window `max_distance`.
|
||||
|
||||
Positions (the C code's `size_t` and the `WrapPosition`'ed `uint32_t`
|
||||
values) and distances are `base.u64` throughout the encoder, so nothing is
|
||||
truncated for windows up to `1 << 30`. The rolling hashers' table holds
|
||||
32-bit positions, like the C code's.
|
||||
|
||||
|
||||
### Encoder Testing
|
||||
|
||||
`wuffs test -mimic std/brotli` 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 at quality 2),
|
||||
as well as round trips through the decoder.
|
||||
disabling literal context modeling, stream offset, large window), as well as
|
||||
round trips through the decoder. Its large-window tests cover qualities 3 to
|
||||
9 with lgwin 18 to 30 (and the H35, H55 and H65 hashers) on small files and
|
||||
on a 46 MiB input with repeats at distances of about 20 and 40 MiB, decoded
|
||||
by both this package's decoder and libbrotlidec.
|
||||
`script/manual-test-brotli-encoder-parity.c` runs a larger grid over every
|
||||
file in `test/data/brotli`, some larger files in `test/data` (up to about
|
||||
400 KiB) and some synthetic inputs (up to 1 MiB). Its `-q=LO-HI` and
|
||||
`-shard=K/N` options select the qualities and split the inputs over several
|
||||
processes. With `-q=0-9` (the default) it runs 718,555 configurations
|
||||
(about 86 thousand of them at qualities 0 and 1), all byte-exact against
|
||||
libbrotlienc v1.2.1 on x86-64.
|
||||
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
|
||||
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
|
||||
70 MiB inputs with one-shot and streaming large-window configurations at
|
||||
lgwin 24 to 30), also all byte-exact.
|
||||
|
||||
|
||||
### Encoder Differences from the C Code
|
||||
@@ -564,6 +666,22 @@ libbrotlienc v1.2.1 on x86-64.
|
||||
`BROTLI_MAX_SIMD_QUALITY` is undefined (no 64-bit count-trailing-zeros
|
||||
builtin) or 7 (MSVC on x86-64) choose differently and so produce
|
||||
different (but valid) output at those qualities.
|
||||
- The composite hashers (H35, H55 and H65) are not separate hasher types
|
||||
here: `hasher_type` is their first hasher (H3, H54 or H6) and a non-zero
|
||||
`hr_jump` (4 for `HROLLING_FAST`, 1 for `HROLLING`) adds the rolling
|
||||
hasher, whose functions run after the first hasher's, like the C code's
|
||||
composite `FN(Prepare)`, `FN(StitchToPreviousBlock)` and
|
||||
`FN(FindLongestMatch)`. The C code's composite hasher defers its inner
|
||||
hashers' `FN(Initialize)` to its first `FN(Prepare)`. Here, the first
|
||||
hasher's `FN(Initialize)` runs in `hasher_setup` (it does not touch the
|
||||
hasher's memory, so this is equivalent) and the rolling hasher's at the
|
||||
first `hasher_prepare`. The C code's composite gives its first hasher a
|
||||
copy of its `HasherCommon` (with zeroed static dictionary statistics);
|
||||
here, the first hasher uses the encoder's statistics, which the composite
|
||||
itself never uses, so the result is the same.
|
||||
- The workbuf's regions are in a different order than the C code's
|
||||
allocations and the ring buffer is fully reserved from the start (see the
|
||||
workbuf layout above). This does not change the output.
|
||||
- `BrotliBuildMetaBlockGreedy`'s two kinds of block splitter (one for a
|
||||
single histogram per block type, `metablock_inc.h`, and one for several
|
||||
literal contexts, `ContextBlockSplitter` in `metablock.c`) are one
|
||||
|
||||
@@ -157,6 +157,12 @@ pub struct encoder? implements base.io_transformer(
|
||||
rb_cur_size : base.u32,
|
||||
rb_pos : base.u32,
|
||||
|
||||
// rb_short_workbuf is whether any transform_io call (at qualities 2
|
||||
// to 9) had a workbuf shorter than workbuf_len. If so, the ring
|
||||
// buffer's tail may be incomplete, so the input must stay short
|
||||
// enough for the ring buffer never to wrap.
|
||||
rb_short_workbuf : base.bool,
|
||||
|
||||
// The C code's BrotliDistanceParams fields (other than
|
||||
// distance_postfix_bits and num_direct_distance_codes).
|
||||
dist_alphabet_size_max : base.u32,
|
||||
@@ -178,6 +184,16 @@ pub struct encoder? implements base.io_transformer(
|
||||
dict_num_matches : base.u64,
|
||||
fc_max_hops : base.u64,
|
||||
|
||||
// The large-window composite hashers' rolling hasher (the C code's
|
||||
// HashRolling). See encode_hasher_composite.wuffs. hr_jump is 0 when
|
||||
// the hasher is not a composite one.
|
||||
hr_jump : base.u32[..= 4],
|
||||
hr_fresh : base.bool,
|
||||
hr_state : base.u32,
|
||||
hr_factor_remove : base.u32,
|
||||
hr_next_ix : base.u64,
|
||||
hr_off : base.u64,
|
||||
|
||||
// The C code's HasherSearchResult.
|
||||
sr_len : base.u64,
|
||||
sr_distance : base.u64,
|
||||
@@ -205,6 +221,8 @@ pub struct encoder? implements base.io_transformer(
|
||||
// and the StartPosQueue's idx_ and the PosData being pushed.
|
||||
literal_context_mode : base.u32,
|
||||
var_off : base.u64,
|
||||
var_end : base.u64,
|
||||
hq_layout_fixed : base.bool,
|
||||
hq_insufficient : base.bool,
|
||||
hq_wb_high : base.u64,
|
||||
h10_window_mask : base.u64,
|
||||
@@ -464,10 +482,10 @@ pub func encoder.workbuf_len() base.range_ii_u64 {
|
||||
lgwin = this.sanitized_lgwin(quality: q)
|
||||
if q <= 1 {
|
||||
n = ((2 as base.u64) << lgwin) + 503 + STORAGE_SLACK
|
||||
} else if (q <= 9) and ((q == 2) or (this.p_large_window == 0)) {
|
||||
} else if q <= 9 {
|
||||
n = this.compute_layout_offset(quality: q, lgwin: lgwin,
|
||||
lgblock: this.computed_lgblock(quality: q, lgwin: lgwin), idx: LAYOUT_END)
|
||||
} else if (q >= 10) and (this.p_large_window == 0) {
|
||||
} else {
|
||||
return this.workbuf_len_hq(quality: q, lgwin: lgwin)
|
||||
}
|
||||
return this.util.make_range_ii_u64(min_incl: n, max_incl: n)
|
||||
@@ -847,13 +865,18 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
var injected : base.bool
|
||||
|
||||
var copy_input_size : base.u64
|
||||
var total : base.u64
|
||||
|
||||
this.ensure_initialized!()
|
||||
if this.large_window and (this.quality > 2) {
|
||||
// TODO: implement large-window brotli at qualities 3 and above.
|
||||
return "#unsupported encoder quality"
|
||||
} else if (this.quality >= 2) and (args.workbuf.length() < this.workbuf_end) {
|
||||
return base."#bad workbuf length"
|
||||
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
|
||||
// the input so far (see the rb_short_workbuf check below), as long
|
||||
// as the ring buffer never wraps.
|
||||
this.rb_short_workbuf = true
|
||||
if args.workbuf.length() < (this.rb_buf_off ~mod+ RB_SHORT_SLACK) {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
}
|
||||
|
||||
while true {
|
||||
@@ -953,6 +976,16 @@ pub func encoder.transform_io?(dst: base.io_writer, src: base.io_reader, workbuf
|
||||
|
||||
if (remaining <> 0) and (args.src.length() > 0) {
|
||||
copy_input_size = remaining.min(no_more_than: args.src.length())
|
||||
if this.rb_short_workbuf {
|
||||
// The ring buffer must not wrap (or reach its tail) and
|
||||
// the workbuf must hold the input so far plus some
|
||||
// slack.
|
||||
total = (this.input_pos ~mod+ copy_input_size) ~mod+ RB_SHORT_SLACK
|
||||
if (total > (this.rb_size as base.u64)) or
|
||||
((this.rb_buf_off ~mod+ total) > args.workbuf.length()) {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
}
|
||||
mark = args.src.mark()
|
||||
args.src.skip?(n: copy_input_size)
|
||||
this.copy_input_to_ring_buffer!(wb: args.workbuf, bytes: args.src.since(mark: mark))
|
||||
|
||||
@@ -17,30 +17,37 @@
|
||||
// The workbuf holds, in this order (see README.md's "Encoder Workbuf Layout"
|
||||
// section, which compute_layout_offset mirrors):
|
||||
// - the storage (the C code's GetBrotliStorage) for one metablock,
|
||||
// - the ring buffer (the C code's RingBuffer.data_),
|
||||
// - the hasher's memory (the C code's common->extra),
|
||||
// - the commands (16 bytes each), and
|
||||
// - the commands (16 bytes each),
|
||||
// - at qualities 4 and above, the metablock split (see
|
||||
// encode_metablock_greedy.wuffs), the literal context map, the
|
||||
// EncodeContextMap symbols and the block encoders' depths and bits.
|
||||
// EncodeContextMap symbols and the block encoders' depths and bits, and
|
||||
// - the ring buffer (the C code's RingBuffer.data_).
|
||||
|
||||
// The workbuf layout's region indexes. LAYOUT_END's offset is the total
|
||||
// length.
|
||||
// The ring buffer is last, so that a workbuf shorter than workbuf_len works
|
||||
// for short inputs (see transform_io's rb_short_workbuf check).
|
||||
pri const LAYOUT_STORAGE : base.u32 = 0
|
||||
pri const LAYOUT_RB : base.u32 = 1
|
||||
pri const LAYOUT_HASH : base.u32 = 2
|
||||
pri const LAYOUT_CMDS : base.u32 = 3
|
||||
pri const LAYOUT_TYPES_0 : base.u32 = 4
|
||||
pri const LAYOUT_LENGTHS_0 : base.u32 = 7
|
||||
pri const LAYOUT_HISTO_0 : base.u32 = 10
|
||||
pri const LAYOUT_COMB : base.u32 = 13
|
||||
pri const LAYOUT_CMAP : base.u32 = 14
|
||||
pri const LAYOUT_RLE : base.u32 = 15
|
||||
pri const LAYOUT_BE_DEPTH_0 : base.u32 = 16
|
||||
pri const LAYOUT_BE_BITS_0 : base.u32 = 19
|
||||
pri const LAYOUT_VAR : base.u32 = 22
|
||||
pri const LAYOUT_HASH : base.u32 = 1
|
||||
pri const LAYOUT_CMDS : base.u32 = 2
|
||||
pri const LAYOUT_TYPES_0 : base.u32 = 3
|
||||
pri const LAYOUT_LENGTHS_0 : base.u32 = 6
|
||||
pri const LAYOUT_HISTO_0 : base.u32 = 9
|
||||
pri const LAYOUT_COMB : base.u32 = 12
|
||||
pri const LAYOUT_CMAP : base.u32 = 13
|
||||
pri const LAYOUT_RLE : base.u32 = 14
|
||||
pri const LAYOUT_BE_DEPTH_0 : base.u32 = 15
|
||||
pri const LAYOUT_BE_BITS_0 : base.u32 = 18
|
||||
pri const LAYOUT_VAR : base.u32 = 21
|
||||
pri const LAYOUT_RB : base.u32 = 22
|
||||
pri const LAYOUT_END : base.u32 = 23
|
||||
|
||||
// RB_SHORT_SLACK is how many bytes past the input's end (in the ring buffer)
|
||||
// a short workbuf must have: the C code reads at most 7 bytes past the
|
||||
// input's end (its kSlackForEightByteHashingEverywhere), rounded up.
|
||||
pri const RB_SHORT_SLACK : base.u64 = 16
|
||||
|
||||
// The C code's MAX_NUM_DELAYED_SYMBOLS (in quality.h).
|
||||
pri const MAX_NUM_DELAYED_SYMBOLS : base.u64 = 0x2FFF
|
||||
|
||||
@@ -91,11 +98,13 @@ pri func encoder.max_metablock_size(lgwin: base.u32[..= 30], lgblock: base.u32[.
|
||||
|
||||
// hasher_total_size returns the sum of FN(HashMemAllocInBytes)'s
|
||||
// alloc_size[i] for the hasher that ChooseHasher picks for the given
|
||||
// quality, lgwin and size_hint.
|
||||
// quality, lgwin and size_hint (including, for lgwin above 24, the
|
||||
// large-window composite hashers' rolling hasher table).
|
||||
pri func encoder.hasher_total_size(quality: base.u32[..= 11], lgwin: base.u32[..= 30], size_hint: base.u32) base.u64 {
|
||||
var htype : base.u32
|
||||
var bucket_bits : base.u32[..= 24]
|
||||
var block_bits : base.u32[..= 16]
|
||||
var rolling : base.u64
|
||||
|
||||
if args.quality >= 10 {
|
||||
return (4 * H10_BUCKET_SIZE) + this.h10_alloc_size_1(lgwin: args.lgwin)
|
||||
@@ -126,9 +135,21 @@ pri func encoder.hasher_total_size(quality: base.u32[..= 11], lgwin: base.u32[..
|
||||
}
|
||||
block_bits = args.quality - 1
|
||||
}
|
||||
return (this.hasher_alloc_size(htype: htype, bucket_bits: bucket_bits, block_bits: block_bits, i: 0) ~mod+
|
||||
rolling = 0
|
||||
if args.lgwin > 24 {
|
||||
// The large-window composite hashers H35, H55 and H65 (see
|
||||
// choose_hasher): HASHER_A plus the rolling hasher's table.
|
||||
if (htype == 3) or (htype == 54) {
|
||||
rolling = this.hasher_rolling_alloc_size()
|
||||
} else if (htype == 6) or (htype == 68) {
|
||||
htype = 6
|
||||
rolling = this.hasher_rolling_alloc_size()
|
||||
}
|
||||
}
|
||||
return ((this.hasher_alloc_size(htype: htype, bucket_bits: bucket_bits, block_bits: block_bits, i: 0) ~mod+
|
||||
this.hasher_alloc_size(htype: htype, bucket_bits: bucket_bits, block_bits: block_bits, i: 1)) ~mod+
|
||||
this.hasher_alloc_size(htype: htype, bucket_bits: bucket_bits, block_bits: block_bits, i: 2)
|
||||
this.hasher_alloc_size(htype: htype, bucket_bits: bucket_bits, block_bits: block_bits, i: 2)) ~mod+
|
||||
rolling
|
||||
}
|
||||
|
||||
// hash_region_size returns the workbuf length reserved for the hasher's
|
||||
@@ -279,6 +300,7 @@ pri func encoder.compute_layout!() {
|
||||
this.mb_cmap_off = this.compute_layout_offset(quality: q, lgwin: lw, lgblock: lb, idx: LAYOUT_CMAP)
|
||||
this.mb_rle_off = this.compute_layout_offset(quality: q, lgwin: lw, lgblock: lb, idx: LAYOUT_RLE)
|
||||
this.var_off = this.compute_layout_offset(quality: q, lgwin: lw, lgblock: lb, idx: LAYOUT_VAR)
|
||||
this.var_end = this.rb_off
|
||||
this.workbuf_end = this.compute_layout_offset(quality: q, lgwin: lw, lgblock: lb, idx: LAYOUT_END)
|
||||
|
||||
// RingBufferSetup.
|
||||
|
||||
@@ -102,7 +102,7 @@ pri func encoder.fc_bank_bits() base.u32[..= 16] {
|
||||
}
|
||||
|
||||
// Mirrors ChooseHasher in quality.h (with BROTLI_MAX_SIMD_QUALITY defined as
|
||||
// 6), for qualities 2 to 9 and lgwin up to 24.
|
||||
// 6), for qualities 2 to 9.
|
||||
pri func encoder.choose_hasher!() {
|
||||
var q : base.u32[..= 11]
|
||||
|
||||
@@ -149,6 +149,25 @@ pri func encoder.choose_hasher!() {
|
||||
this.h_num_last_distances = 10
|
||||
}
|
||||
}
|
||||
|
||||
this.hr_jump = 0
|
||||
if this.lgwin > 24 {
|
||||
// Different hashers for large window brotli: not for qualities <= 2,
|
||||
// these are too fast for large window. Not for qualities >= 10:
|
||||
// their hasher already works well with large window. So the changes
|
||||
// are:
|
||||
// H3 --> H35: for quality 3.
|
||||
// H54 --> H55: for quality 4 with size hint > 1MB
|
||||
// H6/H68 --> H65: for qualities 5, 6, 7, 8, 9.
|
||||
// See encode_hasher_composite.wuffs: the composite hasher's
|
||||
// HASHER_A stays in this.hasher_type.
|
||||
if (this.hasher_type == 3) or (this.hasher_type == 54) {
|
||||
this.hr_jump = 4
|
||||
} else if (this.hasher_type == 6) or (this.hasher_type == 68) {
|
||||
this.hasher_type = 6
|
||||
this.hr_jump = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hasher_alloc_size mirrors FN(HashMemAllocInBytes) (alloc_size[i], for i in
|
||||
@@ -226,6 +245,9 @@ pri func encoder.hasher_setup!(wb: slice base.u8, position: base.u64, input_size
|
||||
htype: this.hasher_type, bucket_bits: this.h_bucket_bits, block_bits: this.h_block_bits, i: 0)
|
||||
this.h_off2 = this.h_off1 ~mod+ this.hasher_alloc_size(
|
||||
htype: this.hasher_type, bucket_bits: this.h_bucket_bits, block_bits: this.h_block_bits, i: 1)
|
||||
// The composite hashers' HASHER_B memory follows HASHER_A's.
|
||||
this.hr_off = this.h_off2
|
||||
this.hr_fresh = true
|
||||
this.h_is_prepared = false
|
||||
this.h_is_setup = true
|
||||
}
|
||||
@@ -314,6 +336,18 @@ pri func encoder.hasher_prepare!(wb: slice base.u8, one_shot: base.bool, input_s
|
||||
j ~mod+= 1
|
||||
}
|
||||
}
|
||||
|
||||
if this.hr_jump <> 0 {
|
||||
// The composite hasher's FN(Prepare) (in hash_composite_inc.h), for
|
||||
// HASHER_B. HASHER_B's FN(Initialize) is deferred to the first
|
||||
// FN(Prepare). HASHER_A's FN(Initialize) does not touch the hasher
|
||||
// memory, so doing it earlier (in hasher_setup) is equivalent.
|
||||
if this.hr_fresh {
|
||||
this.hr_fresh = false
|
||||
this.hasher_rolling_initialize!(wb: args.wb)
|
||||
}
|
||||
this.hasher_rolling_prepare!(wb: args.wb, input_size: args.input_size, data_off: this.rb_buf_off)
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors FN(HashBytes) in hash_longest_match_quickly_inc.h.
|
||||
@@ -449,6 +483,10 @@ pri func encoder.hasher_stitch_to_previous_block!(wb: slice base.u8, num_bytes:
|
||||
this.hasher_store!(wb: args.wb, ix: args.position ~mod- 2)
|
||||
this.hasher_store!(wb: args.wb, ix: args.position ~mod- 1)
|
||||
}
|
||||
if this.hr_jump <> 0 {
|
||||
// The composite hasher's HASHER_B.
|
||||
this.hasher_rolling_stitch_to_previous_block!(wb: args.wb, num_bytes: args.num_bytes, position: args.position)
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors PrepareDistanceCache in hash.h, via each hasher's
|
||||
@@ -614,6 +652,11 @@ pri func encoder.find_longest_match!(wb: slice base.u8, cur_ix: base.u64, max_le
|
||||
this.find_longest_match_forgetful!(wb: args.wb, cur_ix: args.cur_ix, max_length: args.max_length,
|
||||
max_backward: args.max_backward, dictionary_distance: args.dictionary_distance, max_distance: args.max_distance)
|
||||
}
|
||||
if this.hr_jump <> 0 {
|
||||
// The composite hasher's HASHER_B.
|
||||
this.hasher_rolling_find_longest_match!(wb: args.wb, cur_ix: args.cur_ix, max_length: args.max_length,
|
||||
max_backward: args.max_backward)
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors FN(FindLongestMatch) in hash_longest_match_quickly_inc.h.
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
// 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 C code's large-window hashers: the composite hashers
|
||||
// (hash_composite_inc.h) H35 (H3 + HROLLING_FAST), H55 (H54 + HROLLING_FAST)
|
||||
// and H65 (H6 + HROLLING), and the rolling hashers (hash_rolling_inc.h)
|
||||
// HROLLING_FAST (JUMP 4) and HROLLING (JUMP 1), both with CHUNKLEN 32,
|
||||
// NUMBUCKETS (1 << 24) and MASK ((NUMBUCKETS * 64) - 1).
|
||||
//
|
||||
// A composite hasher is represented by its first hasher (HASHER_A, H3, H54 or
|
||||
// H6) in this.hasher_type and its rolling hasher (HASHER_B) by a non-zero
|
||||
// this.hr_jump (4 for HROLLING_FAST, 1 for HROLLING). The composite's FN(etc)
|
||||
// functions call HASHER_A's and then HASHER_B's, so the encode_hasher.wuffs
|
||||
// functions (which implement HASHER_A) call the hasher_rolling_etc functions
|
||||
// below after their own work when this.hr_jump is non-zero.
|
||||
//
|
||||
// The composite hasher's memory is HASHER_A's alloc_size[0] and [1] followed
|
||||
// by HASHER_B's alloc_size[0] (the rolling table of NUMBUCKETS u32le values),
|
||||
// at the this.hr_off workbuf offset. HASHER_A's HasherCommon is a copy of the
|
||||
// composite's (the C code's ha_common), whose dict_num_lookups and
|
||||
// dict_num_matches start at zero, like the composite's own, so HASHER_A can
|
||||
// keep using this.dict_num_lookups and this.dict_num_matches.
|
||||
|
||||
// Mirrors CHUNKLEN in hash.h, for the rolling hashers.
|
||||
pri const ROLLING_CHUNKLEN : base.u64 = 32
|
||||
|
||||
// Mirrors NUMBUCKETS in hash.h, for the rolling hashers.
|
||||
pri const ROLLING_NUMBUCKETS : base.u32 = 0x100_0000
|
||||
|
||||
// Mirrors MASK in hash.h, for the rolling hashers.
|
||||
pri const ROLLING_MASK : base.u32 = 0x3FFF_FFFF
|
||||
|
||||
// Mirrors FN(kRollingHashMul32) in hash_rolling_inc.h.
|
||||
pri const ROLLING_HASH_MUL_32 : base.u32 = 69069
|
||||
|
||||
// Mirrors FN(kInvalidPos) in hash_rolling_inc.h.
|
||||
pri const ROLLING_INVALID_POS : base.u32 = 0xFFFF_FFFF
|
||||
|
||||
// hasher_rolling_alloc_size mirrors FN(HashMemAllocInBytes) in
|
||||
// hash_rolling_inc.h: alloc_size[0], the table.
|
||||
pri func encoder.hasher_rolling_alloc_size() base.u64 {
|
||||
return 4 * (ROLLING_NUMBUCKETS as base.u64)
|
||||
}
|
||||
|
||||
// Mirrors FN(Initialize) in hash_rolling_inc.h. The C code's composite
|
||||
// hashers call it (via their deferred initialization) in their first
|
||||
// FN(Prepare), which HasherSetup calls right after FN(Initialize).
|
||||
pri func encoder.hasher_rolling_initialize!(wb: slice base.u8) {
|
||||
var i : base.u64
|
||||
|
||||
this.hr_state = 0
|
||||
this.hr_next_ix = 0
|
||||
|
||||
// Compute the factor of the oldest byte to remove: factor**steps modulo
|
||||
// 0xffffffff (the multiplications rely on 32-bit overflow).
|
||||
this.hr_factor_remove = 1
|
||||
i = 0
|
||||
while i < ROLLING_CHUNKLEN {
|
||||
this.hr_factor_remove ~mod*= ROLLING_HASH_MUL_32
|
||||
i ~mod+= this.hr_jump as base.u64
|
||||
}
|
||||
|
||||
// Every table entry is ROLLING_INVALID_POS.
|
||||
this.fill_bytes!(s: args.wb, i: this.hr_off, n: this.hasher_rolling_alloc_size(), v: 0xFF)
|
||||
}
|
||||
|
||||
// Mirrors FN(Prepare) in hash_rolling_inc.h, where data is the workbuf's
|
||||
// bytes starting at offset data_off.
|
||||
pri func encoder.hasher_rolling_prepare!(wb: slice base.u8, input_size: base.u64, data_off: base.u64) {
|
||||
var i : base.u64
|
||||
|
||||
// Too small size, cannot use this hasher.
|
||||
if args.input_size < ROLLING_CHUNKLEN {
|
||||
return nothing
|
||||
}
|
||||
this.hr_state = 0
|
||||
i = 0
|
||||
while i < ROLLING_CHUNKLEN {
|
||||
// FN(HashRollingFunctionInitial).
|
||||
this.hr_state = (ROLLING_HASH_MUL_32 ~mod* this.hr_state) ~mod+
|
||||
((this.byte_at(s: args.wb, i: args.data_off ~mod+ i) as base.u32) + 1)
|
||||
i ~mod+= this.hr_jump as base.u64
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors FN(StitchToPreviousBlock) in hash_rolling_inc.h.
|
||||
pri func encoder.hasher_rolling_stitch_to_previous_block!(wb: slice base.u8, num_bytes: base.u64, position: base.u64) {
|
||||
var position : base.u64
|
||||
var position_masked : base.u64
|
||||
var available : base.u64
|
||||
var jm1 : base.u64
|
||||
var diff : base.u64
|
||||
var mask : base.u64
|
||||
|
||||
// In this case we must re-initialize the hasher from scratch from the
|
||||
// current position.
|
||||
position = args.position
|
||||
available = args.num_bytes
|
||||
jm1 = (this.hr_jump as base.u64) ~mod- 1
|
||||
if (position & jm1) <> 0 {
|
||||
diff = (this.hr_jump as base.u64) ~mod- (position & jm1)
|
||||
if diff > available {
|
||||
available = 0
|
||||
} else {
|
||||
available ~mod-= diff
|
||||
}
|
||||
position ~mod+= diff
|
||||
}
|
||||
mask = this.rb_mask as base.u64
|
||||
position_masked = position & mask
|
||||
// Wrapping around ringbuffer not handled.
|
||||
if available > (mask ~mod- position_masked) {
|
||||
available = mask ~mod- position_masked
|
||||
}
|
||||
this.hasher_rolling_prepare!(wb: args.wb, input_size: available,
|
||||
data_off: this.rb_buf_off ~mod+ position_masked)
|
||||
this.hr_next_ix = position
|
||||
}
|
||||
|
||||
// Mirrors FN(FindLongestMatch) in hash_rolling_inc.h. The search result is in
|
||||
// the this.sr_etc fields.
|
||||
pri func encoder.hasher_rolling_find_longest_match!(wb: slice base.u8, cur_ix: base.u64, max_length: base.u64, max_backward: base.u64) {
|
||||
var data : roslice base.u8
|
||||
var mask : base.u64
|
||||
var cur_ix_masked : base.u64
|
||||
var jump : base.u64
|
||||
var pos : base.u64
|
||||
var code : base.u32
|
||||
var rem : base.u8
|
||||
var add : base.u8
|
||||
var found_ix : base.u64
|
||||
var backward : base.u64
|
||||
var found_ix_masked : base.u64
|
||||
var len : base.u64
|
||||
var score : base.u64
|
||||
var t : base.u64
|
||||
|
||||
if this.rb_buf_off > args.wb.length() {
|
||||
return nothing
|
||||
}
|
||||
data = args.wb[this.rb_buf_off ..]
|
||||
mask = this.rb_mask as base.u64
|
||||
cur_ix_masked = args.cur_ix & mask
|
||||
jump = this.hr_jump as base.u64
|
||||
|
||||
if (args.cur_ix & (jump ~mod- 1)) <> 0 {
|
||||
return nothing
|
||||
}
|
||||
|
||||
// Not enough lookahead.
|
||||
if args.max_length < ROLLING_CHUNKLEN {
|
||||
return nothing
|
||||
}
|
||||
|
||||
pos = this.hr_next_ix
|
||||
while pos <= args.cur_ix {
|
||||
code = this.hr_state & ROLLING_MASK
|
||||
|
||||
rem = this.byte_at(s: data, i: pos & mask)
|
||||
add = this.byte_at(s: data, i: (pos ~mod+ ROLLING_CHUNKLEN) & mask)
|
||||
found_ix = ROLLING_INVALID_POS as base.u64
|
||||
|
||||
// FN(HashRollingFunction).
|
||||
this.hr_state = ((ROLLING_HASH_MUL_32 ~mod* this.hr_state) ~mod+ ((add as base.u32) + 1)) ~mod-
|
||||
(this.hr_factor_remove ~mod* ((rem as base.u32) + 1))
|
||||
|
||||
if code < ROLLING_NUMBUCKETS {
|
||||
t = this.hr_off ~mod+ (4 * (code as base.u64))
|
||||
found_ix = this.load32le(s: args.wb, i: t) as base.u64
|
||||
this.poke_u32!(s: args.wb, i: t, v: (pos & 0xFFFF_FFFF) as base.u32)
|
||||
if (pos == args.cur_ix) and (found_ix <> (ROLLING_INVALID_POS as base.u64)) {
|
||||
// The cast to 32-bit makes backward distances up to 4GB work
|
||||
// even if cur_ix is above 4GB, despite using 32-bit values in
|
||||
// the table.
|
||||
backward = (args.cur_ix ~mod- found_ix) & 0xFFFF_FFFF
|
||||
if backward <= args.max_backward {
|
||||
found_ix_masked = found_ix & mask
|
||||
len = this.find_match_length_with_limit(s: data,
|
||||
i1: found_ix_masked, i2: cur_ix_masked, limit: args.max_length)
|
||||
if (len >= 4) and (len > this.sr_len) {
|
||||
score = this.backward_reference_score(copy_length: len, backward_reference_offset: backward)
|
||||
if score > this.sr_score {
|
||||
this.sr_len = len
|
||||
this.sr_distance = backward
|
||||
this.sr_score = score
|
||||
this.sr_len_code_delta = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pos ~mod+= jump
|
||||
}
|
||||
|
||||
this.hr_next_ix = args.cur_ix ~mod+ jump
|
||||
|
||||
// NOTE: this hasher does not search in the dictionary. It is used as
|
||||
// backup-hasher, the main hasher already searches in it.
|
||||
}
|
||||
@@ -105,9 +105,11 @@ pri func encoder.write_meta_block_hq!(wb: slice base.u8, wrapped_last_flush_pos:
|
||||
var cmds_hi : base.u64
|
||||
var c : base.u32
|
||||
var ds : base.u32[..= 704]
|
||||
var var_hi : base.u64
|
||||
|
||||
cmds_hi = this.cmds_off ~mod+ (this.num_cmds ~mod* 16)
|
||||
if (this.var_off > args.wb.length()) or (this.rb_buf_off > args.wb.length()) or
|
||||
var_hi = this.var_end.min(no_more_than: args.wb.length())
|
||||
if (this.var_off > var_hi) or (this.rb_buf_off > args.wb.length()) or
|
||||
(this.cmds_off > cmds_hi) or (cmds_hi > args.wb.length()) {
|
||||
this.hq_insufficient = true
|
||||
return nothing
|
||||
@@ -117,7 +119,7 @@ pri func encoder.write_meta_block_hq!(wb: slice base.u8, wrapped_last_flush_pos:
|
||||
disable_literal_context_modeling: this.p_disable_lcm <> 0)
|
||||
this.mb.init_distance_params!(npostfix: this.distance_postfix_bits,
|
||||
ndirect: this.num_direct_distance_codes, large_window: this.large_window)
|
||||
status = this.mb.build_meta_block!(wb: args.wb[this.var_off ..], ringbuffer: args.wb[this.rb_buf_off ..],
|
||||
status = this.mb.build_meta_block!(wb: args.wb[this.var_off .. var_hi], ringbuffer: args.wb[this.rb_buf_off ..],
|
||||
pos: args.wrapped_last_flush_pos, mask: this.rb_mask as base.u64,
|
||||
prev_byte: args.prev_byte, prev_byte2: args.prev_byte2,
|
||||
cmds: args.wb[this.cmds_off .. cmds_hi], num_commands: this.num_cmds,
|
||||
@@ -131,7 +133,7 @@ pri func encoder.write_meta_block_hq!(wb: slice base.u8, wrapped_last_flush_pos:
|
||||
}
|
||||
// MIN_QUALITY_FOR_OPTIMIZE_HISTOGRAMS is 4. The number of distance
|
||||
// symbols effectively used for distance histograms.
|
||||
this.mb.optimize_histograms!(wb: args.wb[this.var_off ..], num_distance_codes: this.mb.dist_alphabet_size_limit)
|
||||
this.mb.optimize_histograms!(wb: args.wb[this.var_off .. var_hi], num_distance_codes: this.mb.dist_alphabet_size_limit)
|
||||
|
||||
// Point the block encoders' inputs (the gbs_etc fields, which
|
||||
// build_meta_block_greedy sets at qualities 4 to 9) at the
|
||||
@@ -328,11 +330,51 @@ pri func encoder.store_meta_block_hq!(wb: slice base.u8, start_pos: base.u64, le
|
||||
// fixed regions plus the minimum or the worst case variable region.
|
||||
pri func encoder.workbuf_len_hq(quality: base.u32[..= 11], lgwin: base.u32[..= 30]) base.range_ii_u64 {
|
||||
var lgblock : base.u32[..= 30]
|
||||
var off : base.u64
|
||||
var n : base.u64
|
||||
|
||||
lgblock = this.computed_lgblock(quality: args.quality, lgwin: args.lgwin)
|
||||
off = this.compute_layout_offset(quality: args.quality, lgwin: args.lgwin, lgblock: lgblock, idx: LAYOUT_VAR)
|
||||
n = this.compute_layout_offset(quality: args.quality, lgwin: args.lgwin, lgblock: lgblock, idx: LAYOUT_END)
|
||||
return this.util.make_range_ii_u64(
|
||||
min_incl: off ~sat+ this.hq_var_len(quality: args.quality, lgwin: args.lgwin, lgblock: lgblock, worst_case: false),
|
||||
max_incl: off ~sat+ this.hq_var_len(quality: args.quality, lgwin: args.lgwin, lgblock: lgblock, worst_case: true))
|
||||
min_incl: n,
|
||||
max_incl: n ~sat+ this.hq_var_extra_max(quality: args.quality, lgwin: args.lgwin, lgblock: lgblock))
|
||||
}
|
||||
|
||||
// hq_var_extra_max returns how much longer than its minimum length (the
|
||||
// LAYOUT_VAR region's length) the variable region can usefully be: the
|
||||
// difference between its worst case and minimum lengths.
|
||||
pri func encoder.hq_var_extra_max(quality: base.u32[..= 11], lgwin: base.u32[..= 30], lgblock: base.u32[..= 30]) base.u64 {
|
||||
var lo : base.u64
|
||||
var hi : base.u64
|
||||
|
||||
lo = this.hq_var_len(quality: args.quality, lgwin: args.lgwin, lgblock: args.lgblock, worst_case: false)
|
||||
hi = this.hq_var_len(quality: args.quality, lgwin: args.lgwin, lgblock: args.lgblock, worst_case: true)
|
||||
if hi <= lo {
|
||||
return 0
|
||||
}
|
||||
return hi - lo
|
||||
}
|
||||
|
||||
// hq_extend_var_region grows the variable region (and moves the ring buffer,
|
||||
// which is the last region, along with this.workbuf_end) to use as much of a
|
||||
// workbuf of wb_len bytes as the worst case can need. It only does this
|
||||
// once, at the first transform_io call (before the ring buffer is used), at
|
||||
// qualities 10 and 11. Later calls should pass a workbuf of the same length
|
||||
// (a shorter one is handled like any short workbuf, and the variable region
|
||||
// is then cut short).
|
||||
pri func encoder.hq_extend_var_region!(wb_len: base.u64) {
|
||||
var extra : base.u64
|
||||
|
||||
if (this.quality < 10) or this.hq_layout_fixed {
|
||||
return nothing
|
||||
}
|
||||
this.hq_layout_fixed = true
|
||||
if args.wb_len <= this.workbuf_end {
|
||||
return nothing
|
||||
}
|
||||
extra = this.hq_var_extra_max(quality: this.quality, lgwin: this.lgwin, lgblock: this.lgblock)
|
||||
extra = extra.min(no_more_than: args.wb_len - this.workbuf_end)
|
||||
this.var_end ~mod+= extra
|
||||
this.rb_off ~mod+= extra
|
||||
this.rb_buf_off ~mod+= extra
|
||||
this.workbuf_end ~mod+= extra
|
||||
}
|
||||
@@ -1007,7 +1007,7 @@ pri func encoder.create_zopfli_backward_references!(wb: slice base.u8, num_bytes
|
||||
var n : base.u64
|
||||
var ok_offsets : base.bool
|
||||
|
||||
ok_offsets = this.z_set_offsets!(wb_len: args.wb.length(), num_bytes: args.num_bytes)
|
||||
ok_offsets = this.z_set_offsets!(wb_len: this.var_end.min(no_more_than: args.wb.length()), num_bytes: args.num_bytes)
|
||||
if not ok_offsets {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
@@ -1047,7 +1047,7 @@ pri func encoder.create_hq_zopfli_backward_references!(wb: slice base.u8, num_by
|
||||
var n : base.u64
|
||||
var ok_offsets : base.bool
|
||||
|
||||
ok_offsets = this.z_set_offsets!(wb_len: args.wb.length(), num_bytes: args.num_bytes)
|
||||
ok_offsets = this.z_set_offsets!(wb_len: this.var_end.min(no_more_than: args.wb.length()), num_bytes: args.num_bytes)
|
||||
if not ok_offsets {
|
||||
return base."#bad workbuf length"
|
||||
}
|
||||
|
||||
+337
-27
@@ -896,7 +896,9 @@ wuffs_brotli_encode_plan(wuffs_base__io_buffer* dst,
|
||||
wuffs_base__slice_u8 workbuf = g_work_slice_u8;
|
||||
// Use the minimum, so that the qualities 10 and 11 practical bound (see
|
||||
// the README) is exercised. At other qualities, min_incl == max_incl.
|
||||
if (workbuf_len.min_incl > (1ull << 32)) {
|
||||
// Large-window brotli at lgwin 30 needs more than 2 GiB.
|
||||
if ((workbuf_len.min_incl > (3ull << 30)) ||
|
||||
(workbuf_len.min_incl > SIZE_MAX)) {
|
||||
ret = "workbuf_len is too large";
|
||||
goto cleanup;
|
||||
} else if (workbuf_len.min_incl > g_work_slice_u8.len) {
|
||||
@@ -1331,15 +1333,103 @@ test_wuffs_brotli_encode_parameters() {
|
||||
dst.meta.wi = 0;
|
||||
status = wuffs_brotli__encoder__transform_io(
|
||||
enc, &dst, &src,
|
||||
wuffs_base__make_slice_u8(g_work_slice_u8.ptr, (size_t)(wlen - 1)));
|
||||
wuffs_base__make_slice_u8(g_work_slice_u8.ptr, (size_t)(wlen / 2)));
|
||||
BROTLI_ENC_CHECK(status.repr == wuffs_base__error__bad_workbuf_length,
|
||||
"short workbuf at quality 2..9");
|
||||
}
|
||||
src = make_io_buffer_from_string("", 0);
|
||||
dst.meta.wi = 0;
|
||||
|
||||
// With the large-window quirk, lgwin can be up to 30 (instead of 24) at
|
||||
// qualities 3 and above. Like the C code, quality 2 and below ignore the
|
||||
// quirk. Above lgwin 24, the ring buffer is (2 << lgwin) bytes and, at
|
||||
// quality 3, the H35 hasher has a 64 MiB rolling hash table on top of
|
||||
// H3's 256 KiB.
|
||||
for (uint32_t q = 2; q <= 3; q++) {
|
||||
uint64_t wlens[3] = {0};
|
||||
for (int k = 0; k < 3; k++) {
|
||||
free(enc);
|
||||
enc = wuffs_brotli__encoder__alloc();
|
||||
if (!enc) {
|
||||
RETURN_FAIL("alloc failed");
|
||||
}
|
||||
wuffs_brotli__encoder__set_quirk(
|
||||
enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY, q);
|
||||
wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_LGWIN,
|
||||
(k == 0) ? 24 : 30);
|
||||
if (k > 0) {
|
||||
wuffs_brotli__encoder__set_quirk(
|
||||
enc, WUFFS_BROTLI__QUIRK_LARGE_WINDOW, (k == 2) ? 1 : 0);
|
||||
}
|
||||
wlens[k] = wuffs_brotli__encoder__workbuf_len(enc).max_incl;
|
||||
}
|
||||
BROTLI_ENC_CHECK(wlens[0] == wlens[1],
|
||||
"lgwin 30 without large window is lgwin 24");
|
||||
if (q == 2) {
|
||||
BROTLI_ENC_CHECK(wlens[0] == wlens[2],
|
||||
"large window is ignored at quality 2");
|
||||
} else {
|
||||
BROTLI_ENC_CHECK(
|
||||
wlens[2] == (wlens[0] + (2ull << 30) - (2ull << 24) + (64 << 20)),
|
||||
"workbuf_len at q3w30 with large window");
|
||||
}
|
||||
}
|
||||
|
||||
// At qualities 2 to 9, the ring buffer is the workbuf's last region (of
|
||||
// (2 + (1 << rb_bits) + (1 << lgblock) + 7) bytes) and a shorter workbuf
|
||||
// works if it holds (2 + input_length + 16) bytes of it (and the input is
|
||||
// short enough for the ring buffer never to wrap).
|
||||
{
|
||||
free(enc);
|
||||
enc = wuffs_brotli__encoder__alloc();
|
||||
if (!enc) {
|
||||
RETURN_FAIL("alloc failed");
|
||||
}
|
||||
wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY,
|
||||
3);
|
||||
wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_LGWIN, 30);
|
||||
wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_LARGE_WINDOW, 1);
|
||||
uint64_t wlen = wuffs_brotli__encoder__workbuf_len(enc).max_incl;
|
||||
uint64_t rb_region = 2 + (2ull << 30) + (1 << 14) + 7;
|
||||
BROTLI_ENC_CHECK(wlen > rb_region, "workbuf_len at q3w30");
|
||||
size_t short_len = (size_t)(wlen - rb_region + 2 + 3 + 16);
|
||||
uint8_t* short_workbuf = malloc(short_len);
|
||||
if (!short_workbuf) {
|
||||
RETURN_FAIL("malloc failed");
|
||||
}
|
||||
for (int k = 0; k < 2; k++) {
|
||||
wuffs_brotli__encoder* e = wuffs_brotli__encoder__alloc();
|
||||
if (!e) {
|
||||
free(short_workbuf);
|
||||
RETURN_FAIL("alloc failed");
|
||||
}
|
||||
wuffs_brotli__encoder__set_quirk(e, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY,
|
||||
3);
|
||||
wuffs_brotli__encoder__set_quirk(e, WUFFS_BROTLI__QUIRK_LGWIN, 30);
|
||||
wuffs_brotli__encoder__set_quirk(e, WUFFS_BROTLI__QUIRK_LARGE_WINDOW,
|
||||
1);
|
||||
wuffs_base__io_buffer s = make_io_buffer_from_string("abc", 3);
|
||||
s.meta.closed = true;
|
||||
dst.meta.wi = 0;
|
||||
status = wuffs_brotli__encoder__transform_io(
|
||||
e, &dst, &s,
|
||||
wuffs_base__make_slice_u8(short_workbuf, short_len - (k ? 0 : 1)));
|
||||
free(e);
|
||||
if (k == 0) {
|
||||
if (status.repr != wuffs_base__error__bad_workbuf_length) {
|
||||
free(short_workbuf);
|
||||
RETURN_FAIL("too short workbuf at q3w30: %s", status.repr);
|
||||
}
|
||||
} else if (status.repr) {
|
||||
free(short_workbuf);
|
||||
RETURN_FAIL("short workbuf at q3w30: %s", status.repr);
|
||||
}
|
||||
}
|
||||
free(short_workbuf);
|
||||
}
|
||||
|
||||
// At qualities 10 and 11, workbuf_len is a range (see the README's
|
||||
// "Qualities 10 and 11 workbuf length" section).
|
||||
// "Qualities 10 and 11 Workbuf Length" section).
|
||||
for (uint32_t q = 10; q <= 12; q++) {
|
||||
static const struct {
|
||||
uint32_t lgwin;
|
||||
@@ -1369,7 +1459,7 @@ test_wuffs_brotli_encode_parameters() {
|
||||
want[i].min_incl, want[i].max_incl);
|
||||
}
|
||||
}
|
||||
// A workbuf shorter than the minimum is rejected up front.
|
||||
// A workbuf that does not even reach the ring buffer is rejected.
|
||||
src = make_io_buffer_from_string("abc", 3);
|
||||
dst.meta.wi = 0;
|
||||
status = wuffs_brotli__encoder__transform_io(
|
||||
@@ -1380,29 +1470,6 @@ test_wuffs_brotli_encode_parameters() {
|
||||
src = make_io_buffer_from_string("", 0);
|
||||
dst.meta.wi = 0;
|
||||
|
||||
// Large-window brotli at qualities 3 to 9 is unsupported.
|
||||
for (uint32_t q = 3; q <= 9; q++) {
|
||||
bool large_window = true;
|
||||
free(enc);
|
||||
enc = wuffs_brotli__encoder__alloc();
|
||||
if (!enc) {
|
||||
RETURN_FAIL("alloc failed");
|
||||
}
|
||||
wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_ENCODER_QUALITY,
|
||||
q);
|
||||
if (large_window) {
|
||||
wuffs_brotli__encoder__set_quirk(enc, WUFFS_BROTLI__QUIRK_LARGE_WINDOW,
|
||||
1);
|
||||
}
|
||||
BROTLI_ENC_CHECK(wuffs_brotli__encoder__workbuf_len(enc).max_incl == 0,
|
||||
"workbuf_len for an unsupported quality");
|
||||
status = wuffs_brotli__encoder__transform_io(enc, &dst, &src,
|
||||
g_work_slice_u8);
|
||||
BROTLI_ENC_CHECK(
|
||||
status.repr == wuffs_brotli__error__unsupported_encoder_quality,
|
||||
large_window ? "large window at quality 3..9" : "quality 10..11");
|
||||
}
|
||||
|
||||
// Requests.
|
||||
free(enc);
|
||||
enc = wuffs_brotli__encoder__alloc();
|
||||
@@ -3333,6 +3400,245 @@ test_mimic_brotli_decode_large_window_far() {
|
||||
|
||||
#endif // WUFFS_MIMIC
|
||||
|
||||
// ---------------- Brotli Large Window Encoder Tests
|
||||
|
||||
// do_test_brotli_encode_large_window encodes data[:data_len] with the plan's
|
||||
// config c (which should set the large-window quirk) and checks that
|
||||
// decoding the result (with Wuffs' std/brotli decoder with the large-window
|
||||
// quirk set, and, if mimic is true, with libbrotlidec) gives back the
|
||||
// original data. If mimic is true, it also checks that libbrotlienc's output
|
||||
// is the same. If max_compressed_len is non-zero, it checks that the
|
||||
// compressed length is at most that. The decoded data goes in g_want.
|
||||
const char* //
|
||||
do_test_brotli_encode_large_window(const char* name,
|
||||
const brotli_enc_config* c,
|
||||
const uint8_t* data,
|
||||
size_t data_len,
|
||||
bool mimic,
|
||||
size_t max_compressed_len) {
|
||||
wuffs_base__io_buffer src = ((wuffs_base__io_buffer){
|
||||
.data = g_src_slice_u8,
|
||||
});
|
||||
CHECK_STRING(make_brotli_enc_plan(&src, c, data, data_len));
|
||||
wuffs_base__io_buffer have = ((wuffs_base__io_buffer){
|
||||
.data = g_have_slice_u8,
|
||||
});
|
||||
wuffs_base__io_buffer src0 = src;
|
||||
const char* status = wuffs_brotli_encode_plan(
|
||||
&have, &src0, c->param_keys, c->param_values, c->num_params,
|
||||
g_brotli_enc_step_ops, g_brotli_enc_step_lens, g_brotli_enc_num_steps,
|
||||
c->dst_chunk);
|
||||
if (status) {
|
||||
RETURN_FAIL("%s: wuffs encode: %s", name, status);
|
||||
} else if (max_compressed_len && (have.meta.wi > max_compressed_len)) {
|
||||
RETURN_FAIL("%s: compressed length: have %zu, want at most %zu", name,
|
||||
have.meta.wi, max_compressed_len);
|
||||
}
|
||||
have.meta.closed = true;
|
||||
|
||||
for (int decoder = 0; decoder < (mimic ? 3 : 1); decoder++) {
|
||||
wuffs_base__io_buffer want = ((wuffs_base__io_buffer){
|
||||
.data = g_want_slice_u8,
|
||||
});
|
||||
wuffs_base__io_buffer have1 = have;
|
||||
if (decoder == 0) {
|
||||
status = wuffs_brotli_decode_with_options(
|
||||
&want, &have1, UINT64_MAX, UINT64_MAX, true, BROTLI_DICT_SET_NONE);
|
||||
#ifdef WUFFS_MIMIC
|
||||
} else if (decoder == 1) {
|
||||
status = mimic_brotli_decode_large_window(
|
||||
&want, &have1, UINT64_MAX, UINT64_MAX, true, BROTLI_DICT_SET_NONE);
|
||||
} else {
|
||||
wuffs_base__io_buffer src1 = src;
|
||||
status = mimic_brotli_encode(
|
||||
&want, &src1, c->param_keys, c->param_values, c->num_params,
|
||||
g_brotli_enc_step_ops, g_brotli_enc_step_lens,
|
||||
g_brotli_enc_num_steps, c->dst_chunk);
|
||||
if (status) {
|
||||
RETURN_FAIL("%s: mimic encode: %s", name, status);
|
||||
}
|
||||
status = check_io_buffers_equal("", &have, &want);
|
||||
if (status) {
|
||||
RETURN_FAIL("%s: %s", name, status);
|
||||
}
|
||||
continue;
|
||||
#endif // WUFFS_MIMIC
|
||||
}
|
||||
const char* which = (decoder == 0) ? "wuffs" : "mimic";
|
||||
if (status) {
|
||||
RETURN_FAIL("%s: %s decode: %s", name, which, status);
|
||||
} else if (have1.meta.ri != have1.meta.wi) {
|
||||
RETURN_FAIL("%s: %s decode: trailing data", name, which);
|
||||
} else if (want.meta.wi != data_len) {
|
||||
RETURN_FAIL("%s: %s decoded length: have %zu, want %zu", name, which,
|
||||
want.meta.wi, data_len);
|
||||
} else if (memcmp(want.data.ptr, data, data_len)) {
|
||||
RETURN_FAIL("%s: %s decoded bytes differ", name, which);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// do_test_brotli_encode_large_window_files runs a grid of large-window
|
||||
// configurations (qualities 3 to 9, lgwin 18 to 30) over a few small files.
|
||||
const char* //
|
||||
do_test_brotli_encode_large_window_files(bool mimic) {
|
||||
static const char* filenames[] = {
|
||||
"test/data/romeo.txt",
|
||||
"test/data/brotli/alice29.txt",
|
||||
"test/data/brotli/random_org_10k.bin",
|
||||
"test/data/brotli/quickfox_repeated",
|
||||
};
|
||||
static const uint32_t lgwins[] = {18, 25, 26, 28, 30};
|
||||
wuffs_base__io_buffer data = ((wuffs_base__io_buffer){
|
||||
.data = g_work_slice_u8,
|
||||
});
|
||||
for (size_t f = 0; f < WUFFS_TESTLIB_ARRAY_SIZE(filenames); f++) {
|
||||
// g_work is free to use for the input: each config's workbuf (of more
|
||||
// than 64 MiB) is malloc'ed.
|
||||
data.meta.wi = 0;
|
||||
data.meta.ri = 0;
|
||||
data.meta.closed = false;
|
||||
CHECK_STRING(read_file(&data, filenames[f]));
|
||||
uint8_t* copy = malloc(data.meta.wi ? data.meta.wi : 1);
|
||||
if (!copy) {
|
||||
return "malloc failed";
|
||||
}
|
||||
memcpy(copy, data.data.ptr, data.meta.wi);
|
||||
const char* ret = NULL;
|
||||
for (uint32_t q = 3; (q <= 9) && !ret; q++) {
|
||||
for (size_t w = 0; (w < 5) && !ret; w++) {
|
||||
for (int k = 0; (k < 2) && !ret; k++) {
|
||||
if ((k == 1) && (lgwins[w] != 26)) {
|
||||
continue;
|
||||
}
|
||||
brotli_enc_config c = make_brotli_enc_config(q, lgwins[w]);
|
||||
brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_LARGE_WINDOW, 1);
|
||||
if (k == 1) {
|
||||
// A big size hint selects H55 (at quality 4) and H65 (at
|
||||
// qualities 5 to 9).
|
||||
brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_SIZE_HINT,
|
||||
1 << 20);
|
||||
}
|
||||
c.chunking = (w & 1) ? BROTLI_ENC_CHUNK_SINGLE
|
||||
: BROTLI_ENC_CHUNK_RANDOM;
|
||||
c.flush = (w & 1) ? BROTLI_ENC_FLUSH_NONE : BROTLI_ENC_FLUSH_MIDDLE;
|
||||
c.seed = (q << 8) | w;
|
||||
char s[256];
|
||||
snprintf(s, sizeof s, "%s: q=%u lgwin=%u size_hint=%s",
|
||||
filenames[f], q, lgwins[w], k ? "1<<20" : "0");
|
||||
ret = do_test_brotli_encode_large_window(s, &c, copy, data.meta.wi,
|
||||
mimic, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(copy);
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// make_brotli_enc_far_data returns a malloc'ed buffer of pseudo-random bytes
|
||||
// with two 2 MiB repeats at distances of (20 MiB + 64) and (40 MiB - 4096).
|
||||
// The repeats' sources are not repeated elsewhere, so only an encoder with a
|
||||
// large enough window (lgwin 25 or more for the first, 26 or more for the
|
||||
// second) can compress it.
|
||||
uint8_t* //
|
||||
make_brotli_enc_far_data(size_t* len) {
|
||||
const size_t mib = 1 << 20;
|
||||
const size_t n = (46 * mib) + 12345;
|
||||
uint8_t* p = malloc(n);
|
||||
if (!p) {
|
||||
return NULL;
|
||||
}
|
||||
brotli_enc_fill_random(p, n, 0xFA12);
|
||||
memcpy(p + (21 * mib) + 64, p + (1 * mib), 2 * mib);
|
||||
memcpy(p + (44 * mib) - 4096, p + (4 * mib), 2 * mib);
|
||||
*len = n;
|
||||
return p;
|
||||
}
|
||||
|
||||
const char* //
|
||||
do_test_brotli_encode_large_window_far(bool mimic) {
|
||||
size_t len = 0;
|
||||
uint8_t* data = make_brotli_enc_far_data(&len);
|
||||
if (!data) {
|
||||
return "malloc failed";
|
||||
}
|
||||
static const struct {
|
||||
uint32_t quality;
|
||||
uint32_t lgwin;
|
||||
uint32_t size_hint;
|
||||
uint32_t chunking;
|
||||
size_t num_repeats;
|
||||
} configs[] = {
|
||||
// H35.
|
||||
{3, 25, 0, BROTLI_ENC_CHUNK_SINGLE, 1},
|
||||
{3, 30, 0, BROTLI_ENC_CHUNK_RANDOM, 2},
|
||||
// H55.
|
||||
{4, 26, 1 << 30, BROTLI_ENC_CHUNK_SINGLE, 2},
|
||||
// H65.
|
||||
{5, 30, 1 << 30, BROTLI_ENC_CHUNK_RANDOM, 2},
|
||||
{7, 28, 0, BROTLI_ENC_CHUNK_SINGLE, 2},
|
||||
{9, 26, 0, BROTLI_ENC_CHUNK_SINGLE, 2},
|
||||
};
|
||||
const char* ret = NULL;
|
||||
for (size_t i = 0; (i < WUFFS_TESTLIB_ARRAY_SIZE(configs)) && !ret; i++) {
|
||||
brotli_enc_config c =
|
||||
make_brotli_enc_config(configs[i].quality, configs[i].lgwin);
|
||||
brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_LARGE_WINDOW, 1);
|
||||
if (configs[i].size_hint) {
|
||||
brotli_enc_config__add_param(&c, BROTLI_ENC_PARAM_SIZE_HINT,
|
||||
configs[i].size_hint);
|
||||
}
|
||||
c.chunking = configs[i].chunking;
|
||||
c.flush = (configs[i].chunking == BROTLI_ENC_CHUNK_RANDOM)
|
||||
? BROTLI_ENC_FLUSH_MIDDLE
|
||||
: BROTLI_ENC_FLUSH_NONE;
|
||||
c.seed = i;
|
||||
char s[256];
|
||||
snprintf(s, sizeof s, "far data: q=%u lgwin=%u", configs[i].quality,
|
||||
configs[i].lgwin);
|
||||
// Pseudo-random data is incompressible, except for the repeats (of
|
||||
// 2 MiB each), which must be found (mostly) to meet this limit.
|
||||
size_t max_compressed_len = len - (configs[i].num_repeats * (3 << 19));
|
||||
ret = do_test_brotli_encode_large_window(s, &c, data, len, mimic,
|
||||
max_compressed_len);
|
||||
}
|
||||
free(data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_brotli_encode_large_window_files() {
|
||||
CHECK_FOCUS(__func__);
|
||||
return do_test_brotli_encode_large_window_files(false);
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_wuffs_brotli_encode_large_window_far() {
|
||||
CHECK_FOCUS(__func__);
|
||||
return do_test_brotli_encode_large_window_far(false);
|
||||
}
|
||||
|
||||
#ifdef WUFFS_MIMIC
|
||||
|
||||
const char* //
|
||||
test_mimic_brotli_encode_large_window_files() {
|
||||
CHECK_FOCUS(__func__);
|
||||
return do_test_brotli_encode_large_window_files(true);
|
||||
}
|
||||
|
||||
const char* //
|
||||
test_mimic_brotli_encode_large_window_far() {
|
||||
CHECK_FOCUS(__func__);
|
||||
return do_test_brotli_encode_large_window_far(true);
|
||||
}
|
||||
|
||||
#endif // WUFFS_MIMIC
|
||||
|
||||
// ---------------- Brotli Benches
|
||||
|
||||
const char* //
|
||||
@@ -3404,6 +3710,8 @@ proc g_tests[] = {
|
||||
// Encoder tests.
|
||||
|
||||
test_wuffs_brotli_encode_interface,
|
||||
test_wuffs_brotli_encode_large_window_far,
|
||||
test_wuffs_brotli_encode_large_window_files,
|
||||
test_wuffs_brotli_encode_metadata_round_trip,
|
||||
test_wuffs_brotli_encode_parameters,
|
||||
test_wuffs_brotli_encode_round_trip_corpus,
|
||||
@@ -3412,6 +3720,8 @@ proc g_tests[] = {
|
||||
|
||||
#ifdef WUFFS_MIMIC
|
||||
|
||||
test_mimic_brotli_encode_large_window_far,
|
||||
test_mimic_brotli_encode_large_window_files,
|
||||
test_mimic_brotli_encode_parity_corpus,
|
||||
test_mimic_brotli_encode_parity_grid,
|
||||
test_mimic_brotli_encode_parity_metadata,
|
||||
|
||||
Reference in new issue
Block a user