mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
std/brotli: use metablock_builder's distance and RLE helpers in the encoder
This removes the encoder's copies of PrefixEncodeCopyDistance, CommandRestoreDistanceCode, BrotliInitDistanceParams and BrotliOptimizeHuffmanCountsForRle. The shared BrotliInitDistanceParams also handles large-window distance params (used at quality 2).
This commit is contained in:
5 files changed
+179
-674
No files matched your search
@@ -9279,10 +9279,10 @@ typedef struct wuffs_brotli__decoder__struct wuffs_brotli__decoder;
|
||||
|
||||
typedef struct wuffs_brotli__bit_cost__struct wuffs_brotli__bit_cost;
|
||||
|
||||
typedef struct wuffs_brotli__encoder__struct wuffs_brotli__encoder;
|
||||
|
||||
typedef struct wuffs_brotli__metablock_builder__struct wuffs_brotli__metablock_builder;
|
||||
|
||||
typedef struct wuffs_brotli__encoder__struct wuffs_brotli__encoder;
|
||||
|
||||
typedef struct wuffs_brotli__metablock_tester__struct wuffs_brotli__metablock_tester;
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -9889,6 +9889,54 @@ struct wuffs_brotli__bit_cost__struct {
|
||||
|
||||
}; // struct wuffs_brotli__bit_cost__struct
|
||||
|
||||
struct wuffs_brotli__metablock_builder__struct {
|
||||
// Do not access the private_impl's or private_data's fields directly. There
|
||||
// is no API/ABI compatibility or safety guarantee if you do so. Instead, use
|
||||
// the wuffs_foo__bar__baz functions.
|
||||
//
|
||||
// It is a struct, not a struct*, so that the outermost wuffs_foo__bar struct
|
||||
// can be stack allocated when WUFFS_IMPLEMENTATION is defined.
|
||||
|
||||
struct {
|
||||
uint32_t magic;
|
||||
uint32_t active_coroutine;
|
||||
wuffs_base__vtable null_vtable;
|
||||
|
||||
uint32_t f_quality;
|
||||
bool f_large_window;
|
||||
bool f_disable_lcm;
|
||||
uint32_t f_dist_postfix_bits;
|
||||
uint32_t f_dist_num_direct_codes;
|
||||
uint32_t f_dist_alphabet_size_max;
|
||||
uint32_t f_dist_alphabet_size_limit;
|
||||
uint64_t f_dist_max_distance;
|
||||
uint64_t f_split_num_types[3];
|
||||
uint64_t f_split_num_blocks[3];
|
||||
uint64_t f_split_types_off[3];
|
||||
uint64_t f_split_lengths_off[3];
|
||||
uint64_t f_context_map_off[3];
|
||||
uint64_t f_context_map_size[3];
|
||||
uint64_t f_histograms_off[3];
|
||||
uint64_t f_histograms_size[3];
|
||||
uint64_t f_wb_len;
|
||||
uint64_t f_wb_top;
|
||||
uint64_t f_wb_high;
|
||||
bool f_oom;
|
||||
uint64_t f_num_pairs;
|
||||
uint64_t f_dist_cost;
|
||||
} private_impl;
|
||||
|
||||
struct {
|
||||
wuffs_softfloat__arith f_sf;
|
||||
wuffs_brotli__bit_cost f_bc;
|
||||
uint64_t f_cost[256];
|
||||
uint64_t f_insert_cost[35200];
|
||||
uint16_t f_new_id[256];
|
||||
uint8_t f_good_for_rle[704];
|
||||
} private_data;
|
||||
|
||||
}; // struct wuffs_brotli__metablock_builder__struct
|
||||
|
||||
struct wuffs_brotli__encoder__struct {
|
||||
// Do not access the private_impl's or private_data's fields directly. There
|
||||
// is no API/ABI compatibility or safety guarantee if you do so. Instead, use
|
||||
@@ -9995,6 +10043,7 @@ struct wuffs_brotli__encoder__struct {
|
||||
struct {
|
||||
wuffs_softfloat__arith f_sf;
|
||||
wuffs_brotli__bit_cost f_bc;
|
||||
wuffs_brotli__metablock_builder f_mb;
|
||||
uint8_t f_tiny_buf[32];
|
||||
uint8_t f_lit_depth[256];
|
||||
uint16_t f_lit_bits[256];
|
||||
@@ -10042,7 +10091,6 @@ struct wuffs_brotli__encoder__struct {
|
||||
uint64_t f_gbs_last_entropy[128];
|
||||
uint64_t f_gbs_entropy_tmp[16];
|
||||
uint64_t f_gbs_comb_entropy[32];
|
||||
uint8_t f_good_for_rle[1024];
|
||||
uint32_t f_be_histogram_length[4];
|
||||
uint32_t f_be_num_block_types[4];
|
||||
uint64_t f_be_num_blocks[4];
|
||||
@@ -10193,54 +10241,6 @@ struct wuffs_brotli__encoder__struct {
|
||||
#endif // __cplusplus
|
||||
}; // struct wuffs_brotli__encoder__struct
|
||||
|
||||
struct wuffs_brotli__metablock_builder__struct {
|
||||
// Do not access the private_impl's or private_data's fields directly. There
|
||||
// is no API/ABI compatibility or safety guarantee if you do so. Instead, use
|
||||
// the wuffs_foo__bar__baz functions.
|
||||
//
|
||||
// It is a struct, not a struct*, so that the outermost wuffs_foo__bar struct
|
||||
// can be stack allocated when WUFFS_IMPLEMENTATION is defined.
|
||||
|
||||
struct {
|
||||
uint32_t magic;
|
||||
uint32_t active_coroutine;
|
||||
wuffs_base__vtable null_vtable;
|
||||
|
||||
uint32_t f_quality;
|
||||
bool f_large_window;
|
||||
bool f_disable_lcm;
|
||||
uint32_t f_dist_postfix_bits;
|
||||
uint32_t f_dist_num_direct_codes;
|
||||
uint32_t f_dist_alphabet_size_max;
|
||||
uint32_t f_dist_alphabet_size_limit;
|
||||
uint64_t f_dist_max_distance;
|
||||
uint64_t f_split_num_types[3];
|
||||
uint64_t f_split_num_blocks[3];
|
||||
uint64_t f_split_types_off[3];
|
||||
uint64_t f_split_lengths_off[3];
|
||||
uint64_t f_context_map_off[3];
|
||||
uint64_t f_context_map_size[3];
|
||||
uint64_t f_histograms_off[3];
|
||||
uint64_t f_histograms_size[3];
|
||||
uint64_t f_wb_len;
|
||||
uint64_t f_wb_top;
|
||||
uint64_t f_wb_high;
|
||||
bool f_oom;
|
||||
uint64_t f_num_pairs;
|
||||
uint64_t f_dist_cost;
|
||||
} private_impl;
|
||||
|
||||
struct {
|
||||
wuffs_softfloat__arith f_sf;
|
||||
wuffs_brotli__bit_cost f_bc;
|
||||
uint64_t f_cost[256];
|
||||
uint64_t f_insert_cost[35200];
|
||||
uint16_t f_new_id[256];
|
||||
uint8_t f_good_for_rle[704];
|
||||
} private_data;
|
||||
|
||||
}; // struct wuffs_brotli__metablock_builder__struct
|
||||
|
||||
struct wuffs_brotli__metablock_tester__struct {
|
||||
// Do not access the private_impl's or private_data's fields directly. There
|
||||
// is no API/ABI compatibility or safety guarantee if you do so. Instead, use
|
||||
@@ -69028,14 +69028,6 @@ wuffs_brotli__encoder__get_length_code(
|
||||
uint64_t a_copylen,
|
||||
bool a_use_last_distance);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__prefix_encode_copy_distance(
|
||||
const wuffs_brotli__encoder* self,
|
||||
uint64_t a_distance_code,
|
||||
uint64_t a_num_direct_codes,
|
||||
uint32_t a_postfix_bits);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__init_command(
|
||||
@@ -71078,30 +71070,6 @@ wuffs_brotli__encoder__optimize_histograms(
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint32_t a_num_distance_codes);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__optimize_huffman_counts_for_rle(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_off,
|
||||
uint64_t a_length);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__encoder__ctr(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_off,
|
||||
uint64_t a_i);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__rle_limit3(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_off,
|
||||
uint64_t a_i);
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint8_t
|
||||
wuffs_brotli__encoder__byte_at(
|
||||
@@ -71320,87 +71288,6 @@ wuffs_brotli__bit_cost__initialize(
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
|
||||
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
|
||||
wuffs_brotli__encoder__initialize(
|
||||
wuffs_brotli__encoder* self,
|
||||
size_t sizeof_star_self,
|
||||
uint64_t wuffs_version,
|
||||
uint32_t options){
|
||||
if (!self) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
|
||||
}
|
||||
if (sizeof(*self) != sizeof_star_self) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_sizeof_receiver);
|
||||
}
|
||||
if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) ||
|
||||
(((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_wuffs_version);
|
||||
}
|
||||
|
||||
if ((options & WUFFS_INITIALIZE__ALREADY_ZEROED) != 0) {
|
||||
// The whole point of this if-check is to detect an uninitialized *self.
|
||||
// We disable the warning on GCC. Clang-5.0 does not have this warning.
|
||||
#if !defined(__clang__) && defined(__GNUC__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
||||
#endif
|
||||
if (self->private_impl.magic != 0) {
|
||||
return wuffs_base__make_status(wuffs_base__error__initialize_falsely_claimed_already_zeroed);
|
||||
}
|
||||
#if !defined(__clang__) && defined(__GNUC__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
} else {
|
||||
if ((options & WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED) == 0) {
|
||||
memset(self, 0, sizeof(*self));
|
||||
options |= WUFFS_INITIALIZE__ALREADY_ZEROED;
|
||||
} else {
|
||||
memset(&(self->private_impl), 0, sizeof(self->private_impl));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
wuffs_base__status z = wuffs_softfloat__arith__initialize(
|
||||
&self->private_data.f_sf, sizeof(self->private_data.f_sf), WUFFS_VERSION, options);
|
||||
if (z.repr) {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
{
|
||||
wuffs_base__status z = wuffs_brotli__bit_cost__initialize(
|
||||
&self->private_data.f_bc, sizeof(self->private_data.f_bc), WUFFS_VERSION, options);
|
||||
if (z.repr) {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
||||
self->private_impl.vtable_for__wuffs_base__io_transformer.vtable_name =
|
||||
wuffs_base__io_transformer__vtable_name;
|
||||
self->private_impl.vtable_for__wuffs_base__io_transformer.function_pointers =
|
||||
(const void*)(&wuffs_brotli__encoder__func_ptrs_for__wuffs_base__io_transformer);
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
|
||||
wuffs_brotli__encoder*
|
||||
wuffs_brotli__encoder__alloc(void) {
|
||||
wuffs_brotli__encoder* x =
|
||||
(wuffs_brotli__encoder*)(calloc(1, sizeof(wuffs_brotli__encoder)));
|
||||
if (!x) {
|
||||
return NULL;
|
||||
}
|
||||
if (wuffs_brotli__encoder__initialize(
|
||||
x, sizeof(wuffs_brotli__encoder), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) {
|
||||
free(x);
|
||||
return NULL;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
size_t
|
||||
sizeof__wuffs_brotli__encoder(void) {
|
||||
return sizeof(wuffs_brotli__encoder);
|
||||
}
|
||||
|
||||
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
|
||||
wuffs_brotli__metablock_builder__initialize(
|
||||
wuffs_brotli__metablock_builder* self,
|
||||
@@ -71458,6 +71345,94 @@ wuffs_brotli__metablock_builder__initialize(
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
|
||||
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
|
||||
wuffs_brotli__encoder__initialize(
|
||||
wuffs_brotli__encoder* self,
|
||||
size_t sizeof_star_self,
|
||||
uint64_t wuffs_version,
|
||||
uint32_t options){
|
||||
if (!self) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_receiver);
|
||||
}
|
||||
if (sizeof(*self) != sizeof_star_self) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_sizeof_receiver);
|
||||
}
|
||||
if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) ||
|
||||
(((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) {
|
||||
return wuffs_base__make_status(wuffs_base__error__bad_wuffs_version);
|
||||
}
|
||||
|
||||
if ((options & WUFFS_INITIALIZE__ALREADY_ZEROED) != 0) {
|
||||
// The whole point of this if-check is to detect an uninitialized *self.
|
||||
// We disable the warning on GCC. Clang-5.0 does not have this warning.
|
||||
#if !defined(__clang__) && defined(__GNUC__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
||||
#endif
|
||||
if (self->private_impl.magic != 0) {
|
||||
return wuffs_base__make_status(wuffs_base__error__initialize_falsely_claimed_already_zeroed);
|
||||
}
|
||||
#if !defined(__clang__) && defined(__GNUC__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
} else {
|
||||
if ((options & WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED) == 0) {
|
||||
memset(self, 0, sizeof(*self));
|
||||
options |= WUFFS_INITIALIZE__ALREADY_ZEROED;
|
||||
} else {
|
||||
memset(&(self->private_impl), 0, sizeof(self->private_impl));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
wuffs_base__status z = wuffs_softfloat__arith__initialize(
|
||||
&self->private_data.f_sf, sizeof(self->private_data.f_sf), WUFFS_VERSION, options);
|
||||
if (z.repr) {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
{
|
||||
wuffs_base__status z = wuffs_brotli__bit_cost__initialize(
|
||||
&self->private_data.f_bc, sizeof(self->private_data.f_bc), WUFFS_VERSION, options);
|
||||
if (z.repr) {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
{
|
||||
wuffs_base__status z = wuffs_brotli__metablock_builder__initialize(
|
||||
&self->private_data.f_mb, sizeof(self->private_data.f_mb), WUFFS_VERSION, options);
|
||||
if (z.repr) {
|
||||
return z;
|
||||
}
|
||||
}
|
||||
self->private_impl.magic = WUFFS_BASE__MAGIC;
|
||||
self->private_impl.vtable_for__wuffs_base__io_transformer.vtable_name =
|
||||
wuffs_base__io_transformer__vtable_name;
|
||||
self->private_impl.vtable_for__wuffs_base__io_transformer.function_pointers =
|
||||
(const void*)(&wuffs_brotli__encoder__func_ptrs_for__wuffs_base__io_transformer);
|
||||
return wuffs_base__make_status(NULL);
|
||||
}
|
||||
|
||||
wuffs_brotli__encoder*
|
||||
wuffs_brotli__encoder__alloc(void) {
|
||||
wuffs_brotli__encoder* x =
|
||||
(wuffs_brotli__encoder*)(calloc(1, sizeof(wuffs_brotli__encoder)));
|
||||
if (!x) {
|
||||
return NULL;
|
||||
}
|
||||
if (wuffs_brotli__encoder__initialize(
|
||||
x, sizeof(wuffs_brotli__encoder), WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) {
|
||||
free(x);
|
||||
return NULL;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
size_t
|
||||
sizeof__wuffs_brotli__encoder(void) {
|
||||
return sizeof(wuffs_brotli__encoder);
|
||||
}
|
||||
|
||||
wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT
|
||||
wuffs_brotli__metablock_tester__initialize(
|
||||
wuffs_brotli__metablock_tester* self,
|
||||
@@ -75594,40 +75569,6 @@ wuffs_brotli__encoder__get_length_code(
|
||||
return wuffs_brotli__encoder__combine_length_codes(self, wuffs_brotli__encoder__get_insert_length_code(self, a_insertlen), wuffs_brotli__encoder__get_copy_length_code(self, a_copylen), a_use_last_distance);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.prefix_encode_copy_distance
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__prefix_encode_copy_distance(
|
||||
const wuffs_brotli__encoder* self,
|
||||
uint64_t a_distance_code,
|
||||
uint64_t a_num_direct_codes,
|
||||
uint32_t a_postfix_bits) {
|
||||
uint64_t v_dist = 0;
|
||||
uint64_t v_bucket = 0;
|
||||
uint64_t v_postfix_mask = 0;
|
||||
uint64_t v_postfix = 0;
|
||||
uint64_t v_prefix = 0;
|
||||
uint64_t v_offset = 0;
|
||||
uint64_t v_nbits = 0;
|
||||
uint64_t v_code = 0;
|
||||
uint64_t v_extra = 0;
|
||||
|
||||
if (a_distance_code < ((uint64_t)(16u + a_num_direct_codes))) {
|
||||
return (a_distance_code & 65535u);
|
||||
}
|
||||
v_dist = ((uint64_t)((((uint64_t)(1u)) << ((a_postfix_bits + 2u) & 63u)) + ((uint64_t)(((uint64_t)(a_distance_code - 16u)) - a_num_direct_codes))));
|
||||
v_bucket = ((uint64_t)(((uint64_t)(wuffs_brotli__encoder__log2_floor_non_zero(self, v_dist))) - 1u));
|
||||
v_postfix_mask = ((uint64_t)((((uint64_t)(1u)) << (a_postfix_bits & 63u)) - 1u));
|
||||
v_postfix = (v_dist & v_postfix_mask);
|
||||
v_prefix = ((v_dist >> (v_bucket & 63u)) & 1u);
|
||||
v_offset = ((uint64_t)(((uint64_t)(2u + v_prefix)) << (v_bucket & 63u)));
|
||||
v_nbits = ((uint64_t)(v_bucket - ((uint64_t)(a_postfix_bits))));
|
||||
v_code = (((uint64_t)(v_nbits << 10u)) | ((uint64_t)(((uint64_t)(((uint64_t)(16u + a_num_direct_codes)) + ((uint64_t)(((uint64_t)(((uint64_t)(2u * ((uint64_t)(v_nbits - 1u)))) + v_prefix)) << (a_postfix_bits & 63u))))) + v_postfix)));
|
||||
v_extra = ((((uint64_t)(v_dist - v_offset)) >> (a_postfix_bits & 63u)) & 4294967295u);
|
||||
return ((v_code & 65535u) | (v_extra << 32u));
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.init_command
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
@@ -75646,7 +75587,7 @@ wuffs_brotli__encoder__init_command(
|
||||
uint16_t v_cmd_prefix = 0;
|
||||
|
||||
v_delta = ((uint32_t)((a_copylen_code_delta & 255u)));
|
||||
v_pe = wuffs_brotli__encoder__prefix_encode_copy_distance(self, a_distance_code, ((uint64_t)(self->private_impl.f_num_direct_distance_codes)), (self->private_impl.f_distance_postfix_bits & 3u));
|
||||
v_pe = wuffs_brotli__metablock_builder__prefix_encode_copy_distance(&self->private_data.f_mb, a_distance_code, ((uint64_t)(self->private_impl.f_num_direct_distance_codes)), self->private_impl.f_distance_postfix_bits);
|
||||
v_dist_prefix = ((uint16_t)(v_pe));
|
||||
v_cmd_prefix = wuffs_brotli__encoder__get_length_code(self, a_insertlen, ((uint64_t)(a_copylen + a_copylen_code_delta)), (((uint16_t)(v_dist_prefix & 1023u)) == 0u));
|
||||
wuffs_brotli__encoder__set_cmd(self,
|
||||
@@ -75688,29 +75629,11 @@ wuffs_brotli__encoder__cmd_restore_distance_code(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_i) {
|
||||
uint32_t v_dist_prefix = 0;
|
||||
uint32_t v_dcode = 0;
|
||||
uint32_t v_nbits = 0;
|
||||
uint32_t v_extra = 0;
|
||||
uint32_t v_postfix_mask = 0;
|
||||
uint32_t v_hcode = 0;
|
||||
uint32_t v_lcode = 0;
|
||||
uint32_t v_offset = 0;
|
||||
uint32_t v_npostfix = 0;
|
||||
|
||||
v_dist_prefix = ((uint32_t)(wuffs_brotli__encoder__cmd_dist_prefix(self, a_wb, a_i)));
|
||||
v_npostfix = (self->private_impl.f_distance_postfix_bits & 31u);
|
||||
if ((v_dist_prefix & 1023u) < ((uint32_t)(16u + self->private_impl.f_num_direct_distance_codes))) {
|
||||
return (v_dist_prefix & 1023u);
|
||||
}
|
||||
v_dcode = (v_dist_prefix & 1023u);
|
||||
v_nbits = (v_dist_prefix >> 10u);
|
||||
v_extra = wuffs_brotli__encoder__cmd_dist_extra(self, a_wb, a_i);
|
||||
v_postfix_mask = ((uint32_t)((((uint32_t)(1u)) << v_npostfix) - 1u));
|
||||
v_hcode = (((uint32_t)(((uint32_t)(v_dcode - self->private_impl.f_num_direct_distance_codes)) - 16u)) >> v_npostfix);
|
||||
v_lcode = (((uint32_t)(((uint32_t)(v_dcode - self->private_impl.f_num_direct_distance_codes)) - 16u)) & v_postfix_mask);
|
||||
v_offset = ((uint32_t)(((uint32_t)(((uint32_t)(2u + (v_hcode & 1u))) << (v_nbits & 31u))) - 4u));
|
||||
return ((uint32_t)(((uint32_t)(((uint32_t)(((uint32_t)(((uint32_t)(v_offset + v_extra)) << v_npostfix)) + v_lcode)) + self->private_impl.f_num_direct_distance_codes)) + 16u));
|
||||
return wuffs_brotli__metablock_builder__command_restore_distance_code(&self->private_data.f_mb,
|
||||
a_wb,
|
||||
((uint64_t)(self->private_impl.f_cmds_off + ((uint64_t)(a_i * 16u)))),
|
||||
self->private_impl.f_distance_postfix_bits,
|
||||
self->private_impl.f_num_direct_distance_codes);
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.compute_distance_code
|
||||
@@ -80663,14 +80586,10 @@ WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__init_distance_params(
|
||||
wuffs_brotli__encoder* self) {
|
||||
uint32_t v_npostfix = 0;
|
||||
uint32_t v_ndirect = 0;
|
||||
|
||||
v_npostfix = (self->private_impl.f_distance_postfix_bits & 3u);
|
||||
v_ndirect = self->private_impl.f_num_direct_distance_codes;
|
||||
self->private_impl.f_dist_alphabet_size_max = ((uint32_t)(((uint32_t)(16u + v_ndirect)) + (((uint32_t)(24u)) << (v_npostfix + 1u))));
|
||||
self->private_impl.f_dist_alphabet_size_limit = self->private_impl.f_dist_alphabet_size_max;
|
||||
self->private_impl.f_dist_max_distance = ((uint64_t)(((uint32_t)(((uint32_t)(v_ndirect + (((uint32_t)(1u)) << (24u + v_npostfix + 2u)))) - (((uint32_t)(1u)) << (v_npostfix + 2u))))));
|
||||
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);
|
||||
self->private_impl.f_dist_alphabet_size_max = self->private_data.f_mb.private_impl.f_dist_alphabet_size_max;
|
||||
self->private_impl.f_dist_alphabet_size_limit = self->private_data.f_mb.private_impl.f_dist_alphabet_size_limit;
|
||||
self->private_impl.f_dist_max_distance = self->private_data.f_mb.private_impl.f_dist_max_distance;
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
@@ -87108,204 +87027,22 @@ wuffs_brotli__encoder__optimize_histograms(
|
||||
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_data.f_gbs_histograms_size[0u]) {
|
||||
wuffs_brotli__encoder__optimize_huffman_counts_for_rle(self, a_wb, wuffs_brotli__encoder__gbs_histo_off(self, 0u, v_i), 256u);
|
||||
wuffs_brotli__metablock_builder__optimize_huffman_counts_for_rle(&self->private_data.f_mb, a_wb, wuffs_brotli__encoder__gbs_histo_off(self, 0u, v_i), 256u);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_data.f_gbs_histograms_size[1u]) {
|
||||
wuffs_brotli__encoder__optimize_huffman_counts_for_rle(self, a_wb, wuffs_brotli__encoder__gbs_histo_off(self, 1u, v_i), 704u);
|
||||
wuffs_brotli__metablock_builder__optimize_huffman_counts_for_rle(&self->private_data.f_mb, a_wb, wuffs_brotli__encoder__gbs_histo_off(self, 1u, v_i), 704u);
|
||||
v_i += 1u;
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < self->private_data.f_gbs_histograms_size[2u]) {
|
||||
wuffs_brotli__encoder__optimize_huffman_counts_for_rle(self, a_wb, wuffs_brotli__encoder__gbs_histo_off(self, 2u, v_i), ((uint64_t)(a_num_distance_codes)));
|
||||
wuffs_brotli__metablock_builder__optimize_huffman_counts_for_rle(&self->private_data.f_mb, a_wb, wuffs_brotli__encoder__gbs_histo_off(self, 2u, v_i), ((uint64_t)(a_num_distance_codes)));
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.optimize_huffman_counts_for_rle
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static wuffs_base__empty_struct
|
||||
wuffs_brotli__encoder__optimize_huffman_counts_for_rle(
|
||||
wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_off,
|
||||
uint64_t a_length) {
|
||||
uint64_t v_length = 0;
|
||||
uint64_t v_nonzero_count = 0;
|
||||
uint64_t v_stride = 0;
|
||||
uint64_t v_limit = 0;
|
||||
uint64_t v_sum = 0;
|
||||
uint64_t v_i = 0;
|
||||
uint64_t v_k = 0;
|
||||
uint64_t v_nonzeros = 0;
|
||||
uint32_t v_smallest_nonzero = 0;
|
||||
uint64_t v_zeros = 0;
|
||||
uint32_t v_symbol = 0;
|
||||
uint64_t v_step = 0;
|
||||
uint64_t v_count = 0;
|
||||
uint32_t v_c = 0;
|
||||
bool v_cond = false;
|
||||
|
||||
v_length = wuffs_base__u64__min(a_length, 704u);
|
||||
v_i = 0u;
|
||||
while (v_i < v_length) {
|
||||
if (wuffs_brotli__encoder__ctr(self, a_wb, a_off, v_i) != 0u) {
|
||||
v_nonzero_count += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
if (v_nonzero_count < 16u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
while ((v_length != 0u) && (wuffs_brotli__encoder__ctr(self, a_wb, a_off, ((uint64_t)(v_length - 1u))) == 0u)) {
|
||||
v_length -= 1u;
|
||||
}
|
||||
if (v_length == 0u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_smallest_nonzero = 1073741824u;
|
||||
v_i = 0u;
|
||||
while (v_i < v_length) {
|
||||
v_c = wuffs_brotli__encoder__ctr(self, a_wb, a_off, v_i);
|
||||
if (v_c != 0u) {
|
||||
v_nonzeros += 1u;
|
||||
if (v_smallest_nonzero > v_c) {
|
||||
v_smallest_nonzero = v_c;
|
||||
}
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
if (v_nonzeros < 5u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
if (v_smallest_nonzero < 4u) {
|
||||
v_zeros = ((uint64_t)(v_length - v_nonzeros));
|
||||
if (v_zeros < 6u) {
|
||||
v_i = 1u;
|
||||
while (((uint64_t)(v_i + 1u)) < v_length) {
|
||||
if ((wuffs_brotli__encoder__ctr(self, a_wb, a_off, ((uint64_t)(v_i - 1u))) != 0u) && (wuffs_brotli__encoder__ctr(self, a_wb, a_off, v_i) == 0u) && (wuffs_brotli__encoder__ctr(self, a_wb, a_off, ((uint64_t)(v_i + 1u))) != 0u)) {
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(a_off + ((uint64_t)(v_i * 4u)))), 1u);
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (v_nonzeros < 28u) {
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
v_i = 0u;
|
||||
while (v_i < v_length) {
|
||||
self->private_data.f_good_for_rle[(v_i & 1023u)] = 0u;
|
||||
v_i += 1u;
|
||||
}
|
||||
v_symbol = wuffs_brotli__encoder__ctr(self, a_wb, a_off, 0u);
|
||||
v_step = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i <= v_length) {
|
||||
if ((v_i == v_length) || (wuffs_brotli__encoder__ctr(self, a_wb, a_off, v_i) != v_symbol)) {
|
||||
if (((v_symbol == 0u) && (v_step >= 5u)) || ((v_symbol != 0u) && (v_step >= 7u))) {
|
||||
v_k = 0u;
|
||||
while (v_k < v_step) {
|
||||
self->private_data.f_good_for_rle[(((uint64_t)(((uint64_t)(v_i - v_k)) - 1u)) & 1023u)] = 1u;
|
||||
v_k += 1u;
|
||||
}
|
||||
}
|
||||
v_step = 1u;
|
||||
if (v_i != v_length) {
|
||||
v_symbol = wuffs_brotli__encoder__ctr(self, a_wb, a_off, v_i);
|
||||
}
|
||||
} else {
|
||||
v_step += 1u;
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
v_stride = 0u;
|
||||
v_limit = wuffs_brotli__encoder__rle_limit3(self, a_wb, a_off, 0u);
|
||||
v_sum = 0u;
|
||||
v_i = 0u;
|
||||
while (v_i <= v_length) {
|
||||
v_cond = (v_i == v_length);
|
||||
if ( ! v_cond) {
|
||||
v_cond = (self->private_data.f_good_for_rle[(v_i & 1023u)] != 0u);
|
||||
}
|
||||
if ( ! v_cond && (v_i != 0u)) {
|
||||
v_cond = (self->private_data.f_good_for_rle[((v_i - 1u) & 1023u)] != 0u);
|
||||
}
|
||||
if ( ! v_cond) {
|
||||
v_cond = (((uint64_t)(((uint64_t)(((uint64_t)(((uint32_t)(wuffs_brotli__encoder__ctr(self, a_wb, a_off, v_i) * 256u)))) - v_limit)) + 1240u)) >= 2480u);
|
||||
}
|
||||
if (v_cond) {
|
||||
if ((v_stride >= 4u) || ((v_stride >= 3u) && (v_sum == 0u))) {
|
||||
v_count = (((uint64_t)(v_sum + (v_stride / 2u))) / wuffs_base__u64__max(v_stride, 1u));
|
||||
if (v_count == 0u) {
|
||||
v_count = 1u;
|
||||
}
|
||||
if (v_sum == 0u) {
|
||||
v_count = 0u;
|
||||
}
|
||||
v_k = 0u;
|
||||
while (v_k < v_stride) {
|
||||
wuffs_brotli__encoder__poke_u32(self, a_wb, ((uint64_t)(a_off + ((uint64_t)(((uint64_t)(((uint64_t)(v_i - v_k)) - 1u)) * 4u)))), ((uint32_t)(v_count)));
|
||||
v_k += 1u;
|
||||
}
|
||||
}
|
||||
v_stride = 0u;
|
||||
v_sum = 0u;
|
||||
if (((uint64_t)(v_i + 2u)) < v_length) {
|
||||
v_limit = wuffs_brotli__encoder__rle_limit3(self, a_wb, a_off, v_i);
|
||||
} else if (v_i < v_length) {
|
||||
v_limit = ((uint64_t)(((uint32_t)(wuffs_brotli__encoder__ctr(self, a_wb, a_off, v_i) * 256u))));
|
||||
} else {
|
||||
v_limit = 0u;
|
||||
}
|
||||
}
|
||||
v_stride += 1u;
|
||||
if (v_i != v_length) {
|
||||
v_sum += ((uint64_t)(wuffs_brotli__encoder__ctr(self, a_wb, a_off, v_i)));
|
||||
if (v_stride >= 4u) {
|
||||
v_limit = (((uint64_t)(((uint64_t)(v_sum * 256u)) + (v_stride / 2u))) / wuffs_base__u64__max(v_stride, 1u));
|
||||
}
|
||||
if (v_stride == 4u) {
|
||||
v_limit += 120u;
|
||||
}
|
||||
}
|
||||
v_i += 1u;
|
||||
}
|
||||
return wuffs_base__make_empty_struct();
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.ctr
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint32_t
|
||||
wuffs_brotli__encoder__ctr(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_off,
|
||||
uint64_t a_i) {
|
||||
return wuffs_brotli__encoder__load32le(self, a_wb, ((uint64_t)(a_off + ((uint64_t)(a_i * 4u)))));
|
||||
}
|
||||
|
||||
// -------- func brotli.encoder.rle_limit3
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
static uint64_t
|
||||
wuffs_brotli__encoder__rle_limit3(
|
||||
const wuffs_brotli__encoder* self,
|
||||
wuffs_base__slice_u8 a_wb,
|
||||
uint64_t a_off,
|
||||
uint64_t a_i) {
|
||||
uint32_t v_s = 0;
|
||||
|
||||
v_s = wuffs_brotli__encoder__ctr(self, a_wb, a_off, a_i);
|
||||
v_s += wuffs_brotli__encoder__ctr(self, a_wb, a_off, ((uint64_t)(a_i + 1u)));
|
||||
v_s += wuffs_brotli__encoder__ctr(self, a_wb, a_off, ((uint64_t)(a_i + 2u)));
|
||||
return ((uint64_t)(((uint32_t)((((uint32_t)(v_s * 256u)) / 3u) + 420u))));
|
||||
}
|
||||
|
||||
// -------- func brotli.metablock_tester.bits_entropy
|
||||
|
||||
WUFFS_BASE__GENERATED_C_CODE
|
||||
|
||||
@@ -153,37 +153,6 @@ pri func encoder.get_length_code(insertlen: base.u64, copylen: base.u64, use_las
|
||||
use_last_distance: args.use_last_distance)
|
||||
}
|
||||
|
||||
// Mirrors PrefixEncodeCopyDistance in prefix.h. It returns the code (the C
|
||||
// code's *code, in the low 16 bits) and the extra bits (the C code's
|
||||
// *extra_bits, in the high 32 bits).
|
||||
pri func encoder.prefix_encode_copy_distance(distance_code: base.u64, num_direct_codes: base.u64, postfix_bits: base.u32[..= 3]) base.u64 {
|
||||
var dist : base.u64
|
||||
var bucket : base.u64
|
||||
var postfix_mask : base.u64
|
||||
var postfix : base.u64
|
||||
var prefix : base.u64
|
||||
var offset : base.u64
|
||||
var nbits : base.u64
|
||||
var code : base.u64
|
||||
var extra : base.u64
|
||||
|
||||
if args.distance_code < (16 ~mod+ args.num_direct_codes) {
|
||||
return args.distance_code & 0xFFFF
|
||||
}
|
||||
dist = ((1 as base.u64) << ((args.postfix_bits + 2) & 63)) ~mod+
|
||||
((args.distance_code ~mod- 16) ~mod- args.num_direct_codes)
|
||||
bucket = (this.log2_floor_non_zero(n: dist) as base.u64) ~mod- 1
|
||||
postfix_mask = ((1 as base.u64) << (args.postfix_bits & 63)) ~mod- 1
|
||||
postfix = dist & postfix_mask
|
||||
prefix = (dist >> (bucket & 63)) & 1
|
||||
offset = (2 ~mod+ prefix) ~mod<< (bucket & 63)
|
||||
nbits = bucket ~mod- (args.postfix_bits as base.u64)
|
||||
code = (nbits ~mod<< 10) | (((16 ~mod+ args.num_direct_codes) ~mod+
|
||||
(((2 ~mod* (nbits ~mod- 1)) ~mod+ prefix) ~mod<< (args.postfix_bits & 63))) ~mod+ postfix)
|
||||
extra = ((dist ~mod- offset) >> (args.postfix_bits & 63)) & 0xFFFF_FFFF
|
||||
return (code & 0xFFFF) | (extra << 32)
|
||||
}
|
||||
|
||||
// Mirrors InitCommand in command.h, storing the n'th command.
|
||||
pri func encoder.init_command!(wb: slice base.u8, n: base.u64, insertlen: base.u64, copylen: base.u64, copylen_code_delta: base.u64, distance_code: base.u64) {
|
||||
var delta : base.u32
|
||||
@@ -192,10 +161,10 @@ pri func encoder.init_command!(wb: slice base.u8, n: base.u64, insertlen: base.u
|
||||
var cmd_prefix : base.u16
|
||||
|
||||
delta = (args.copylen_code_delta & 0xFF) as base.u32
|
||||
pe = this.prefix_encode_copy_distance(
|
||||
pe = this.mb.prefix_encode_copy_distance(
|
||||
distance_code: args.distance_code,
|
||||
num_direct_codes: this.num_direct_distance_codes as base.u64,
|
||||
postfix_bits: this.distance_postfix_bits & 3)
|
||||
postfix_bits: this.distance_postfix_bits)
|
||||
dist_prefix = (pe & 0xFFFF) as base.u16
|
||||
cmd_prefix = this.get_length_code(
|
||||
insertlen: args.insertlen,
|
||||
@@ -219,31 +188,12 @@ pri func encoder.init_insert_command!(wb: slice base.u8, n: base.u64, insertlen:
|
||||
dist_prefix: 16)
|
||||
}
|
||||
|
||||
// Mirrors CommandRestoreDistanceCode in command.h.
|
||||
// Mirrors CommandRestoreDistanceCode in command.h, for the i'th command.
|
||||
pri func encoder.cmd_restore_distance_code(wb: roslice base.u8, i: base.u64) base.u32 {
|
||||
var dist_prefix : base.u32
|
||||
var dcode : base.u32
|
||||
var nbits : base.u32
|
||||
var extra : base.u32
|
||||
var postfix_mask : base.u32
|
||||
var hcode : base.u32
|
||||
var lcode : base.u32
|
||||
var offset : base.u32
|
||||
var npostfix : base.u32
|
||||
|
||||
dist_prefix = this.cmd_dist_prefix(wb: args.wb, i: args.i) as base.u32
|
||||
npostfix = this.distance_postfix_bits & 31
|
||||
if (dist_prefix & 0x3FF) < (16 ~mod+ this.num_direct_distance_codes) {
|
||||
return dist_prefix & 0x3FF
|
||||
}
|
||||
dcode = dist_prefix & 0x3FF
|
||||
nbits = dist_prefix >> 10
|
||||
extra = this.cmd_dist_extra(wb: args.wb, i: args.i)
|
||||
postfix_mask = ((1 as base.u32) << npostfix) ~mod- 1
|
||||
hcode = ((dcode ~mod- this.num_direct_distance_codes) ~mod- 16) >> npostfix
|
||||
lcode = ((dcode ~mod- this.num_direct_distance_codes) ~mod- 16) & postfix_mask
|
||||
offset = ((2 ~mod+ (hcode & 1)) ~mod<< (nbits & 31)) ~mod- 4
|
||||
return ((((offset ~mod+ extra) ~mod<< npostfix) ~mod+ lcode) ~mod+ this.num_direct_distance_codes) ~mod+ 16
|
||||
return this.mb.command_restore_distance_code(wb: args.wb,
|
||||
c: this.cmds_off ~mod+ (args.i ~mod* 16),
|
||||
npostfix: this.distance_postfix_bits,
|
||||
ndirect: this.num_direct_distance_codes)
|
||||
}
|
||||
|
||||
// Mirrors ComputeDistanceCode in backward_references.c.
|
||||
|
||||
@@ -200,6 +200,10 @@ pub struct encoder? implements base.io_transformer(
|
||||
) + (
|
||||
bc : bit_cost,
|
||||
|
||||
// mb provides some metablock.c, command.h and prefix.h functions
|
||||
// (see encode_metablock.wuffs).
|
||||
mb : metablock_builder,
|
||||
|
||||
// tiny_buf is the C code's tiny_buf_ (plus slack for the 8-byte
|
||||
// stores of write_bits).
|
||||
tiny_buf : array[32] base.u8,
|
||||
@@ -269,9 +273,6 @@ pub struct encoder? implements base.io_transformer(
|
||||
gbs_entropy_tmp : array[16] base.u64,
|
||||
gbs_comb_entropy : array[32] base.u64,
|
||||
|
||||
// BrotliOptimizeHistograms' good_for_rle.
|
||||
good_for_rle : array[1024] base.u8,
|
||||
|
||||
// The BrotliStoreMetaBlock block encoders' state (the C code's
|
||||
// BlockEncoder and BlockSplitCode), indexed by block category (with
|
||||
// index 3 for a local BlockTypeCodeCalculator).
|
||||
|
||||
@@ -276,17 +276,17 @@ pri func encoder.compute_layout!() {
|
||||
this.rb_total_size = this.rb_size ~mod+ this.rb_tail_size
|
||||
}
|
||||
|
||||
// Mirrors BrotliInitDistanceParams in metablock.c (without large window).
|
||||
// Mirrors BrotliInitDistanceParams in metablock.c (see
|
||||
// metablock_builder.init_distance_params), for this.distance_postfix_bits and
|
||||
// this.num_direct_distance_codes.
|
||||
pri func encoder.init_distance_params!() {
|
||||
var npostfix : base.u32
|
||||
var ndirect : base.u32
|
||||
|
||||
npostfix = this.distance_postfix_bits & 3
|
||||
ndirect = this.num_direct_distance_codes
|
||||
this.dist_alphabet_size_max = (16 ~mod+ ndirect) ~mod+ ((24 as base.u32) << (npostfix + 1))
|
||||
this.dist_alphabet_size_limit = this.dist_alphabet_size_max
|
||||
this.dist_max_distance = ((ndirect ~mod+ ((1 as base.u32) << (24 + npostfix + 2))) ~mod-
|
||||
((1 as base.u32) << (npostfix + 2))) as base.u64
|
||||
this.mb.init_distance_params!(
|
||||
npostfix: this.distance_postfix_bits,
|
||||
ndirect: this.num_direct_distance_codes,
|
||||
large_window: this.large_window)
|
||||
this.dist_alphabet_size_max = this.mb.dist_alphabet_size_max
|
||||
this.dist_alphabet_size_limit = this.mb.dist_alphabet_size_limit
|
||||
this.dist_max_distance = this.mb.dist_max_distance
|
||||
}
|
||||
|
||||
// Mirrors WrapPosition in encode.c.
|
||||
|
||||
@@ -12,8 +12,9 @@
|
||||
|
||||
// This file mirrors BrotliBuildMetaBlockGreedy (metablock.c and
|
||||
// metablock_inc.h), the metablock builder for qualities 4 to 9, and
|
||||
// BrotliOptimizeHistograms (metablock.c) with
|
||||
// BrotliOptimizeHuffmanCountsForRle (entropy_encode.c).
|
||||
// BrotliOptimizeHistograms (metablock.c) for its output. The latter uses
|
||||
// metablock_builder.optimize_huffman_counts_for_rle (see
|
||||
// encode_metablock.wuffs) for BrotliOptimizeHuffmanCountsForRle.
|
||||
//
|
||||
// The C code's three FN(BlockSplitter)s and its ContextBlockSplitter are one
|
||||
// greedy block splitter here (the gbs_etc functions and fields), indexed by
|
||||
@@ -424,202 +425,18 @@ pri func encoder.optimize_histograms!(wb: slice base.u8, num_distance_codes: bas
|
||||
|
||||
i = 0
|
||||
while i < this.gbs_histograms_size[0] {
|
||||
this.optimize_huffman_counts_for_rle!(wb: args.wb, off: this.gbs_histo_off(c: 0, ix: i), length: 256)
|
||||
this.mb.optimize_huffman_counts_for_rle!(wb: args.wb, counts: this.gbs_histo_off(c: 0, ix: i), length: 256)
|
||||
i ~mod+= 1
|
||||
}
|
||||
i = 0
|
||||
while i < this.gbs_histograms_size[1] {
|
||||
this.optimize_huffman_counts_for_rle!(wb: args.wb, off: this.gbs_histo_off(c: 1, ix: i), length: 704)
|
||||
this.mb.optimize_huffman_counts_for_rle!(wb: args.wb, counts: this.gbs_histo_off(c: 1, ix: i), length: 704)
|
||||
i ~mod+= 1
|
||||
}
|
||||
i = 0
|
||||
while i < this.gbs_histograms_size[2] {
|
||||
this.optimize_huffman_counts_for_rle!(wb: args.wb, off: this.gbs_histo_off(c: 2, ix: i),
|
||||
this.mb.optimize_huffman_counts_for_rle!(wb: args.wb, counts: this.gbs_histo_off(c: 2, ix: i),
|
||||
length: args.num_distance_codes as base.u64)
|
||||
i ~mod+= 1
|
||||
}
|
||||
}
|
||||
|
||||
// Mirrors BrotliOptimizeHuffmanCountsForRle in entropy_encode.c, for the
|
||||
// length u32le counts at args.wb[off ..]. The good_for_rle array is
|
||||
// this.good_for_rle.
|
||||
pri func encoder.optimize_huffman_counts_for_rle!(wb: slice base.u8, off: base.u64, length: base.u64) {
|
||||
var length : base.u64
|
||||
var nonzero_count : base.u64
|
||||
var stride : base.u64
|
||||
var limit : base.u64
|
||||
var sum : base.u64
|
||||
var i : base.u64
|
||||
var k : base.u64
|
||||
var nonzeros : base.u64
|
||||
var smallest_nonzero : base.u32
|
||||
var zeros : base.u64
|
||||
var symbol : base.u32
|
||||
var step : base.u64
|
||||
var count : base.u64
|
||||
var c : base.u32
|
||||
var cond : base.bool
|
||||
|
||||
length = args.length.min(no_more_than: 704)
|
||||
i = 0
|
||||
while i < length {
|
||||
if this.ctr(wb: args.wb, off: args.off, i: i) <> 0 {
|
||||
nonzero_count ~mod+= 1
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
if nonzero_count < 16 {
|
||||
return nothing
|
||||
}
|
||||
while (length <> 0) and (this.ctr(wb: args.wb, off: args.off, i: length ~mod- 1) == 0) {
|
||||
length ~mod-= 1
|
||||
}
|
||||
if length == 0 {
|
||||
return nothing // All zeros.
|
||||
}
|
||||
// Now counts[0..length - 1] does not have trailing zeros.
|
||||
smallest_nonzero = 0x4000_0000
|
||||
i = 0
|
||||
while i < length {
|
||||
c = this.ctr(wb: args.wb, off: args.off, i: i)
|
||||
if c <> 0 {
|
||||
nonzeros ~mod+= 1
|
||||
if smallest_nonzero > c {
|
||||
smallest_nonzero = c
|
||||
}
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
if nonzeros < 5 {
|
||||
// Small histogram will model it well.
|
||||
return nothing
|
||||
}
|
||||
if smallest_nonzero < 4 {
|
||||
zeros = length ~mod- nonzeros
|
||||
if zeros < 6 {
|
||||
i = 1
|
||||
while (i ~mod+ 1) < length {
|
||||
if (this.ctr(wb: args.wb, off: args.off, i: i ~mod- 1) <> 0) and
|
||||
(this.ctr(wb: args.wb, off: args.off, i: i) == 0) and
|
||||
(this.ctr(wb: args.wb, off: args.off, i: i ~mod+ 1) <> 0) {
|
||||
this.poke_u32!(s: args.wb, i: args.off ~mod+ (i ~mod* 4), v: 1)
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
}
|
||||
}
|
||||
if nonzeros < 28 {
|
||||
return nothing
|
||||
}
|
||||
|
||||
// 2) Let's mark all population counts that already can be encoded with
|
||||
// an RLE code.
|
||||
i = 0
|
||||
while i < length {
|
||||
this.good_for_rle[i & 1023] = 0
|
||||
i ~mod+= 1
|
||||
}
|
||||
// Let's not spoil any of the existing good RLE codes. Mark any seq of 0's
|
||||
// that is longer as 5 as a good_for_rle. Mark any seq of non-0's that is
|
||||
// longer as 7 as a good_for_rle.
|
||||
symbol = this.ctr(wb: args.wb, off: args.off, i: 0)
|
||||
step = 0
|
||||
i = 0
|
||||
while i <= length {
|
||||
if (i == length) or (this.ctr(wb: args.wb, off: args.off, i: i) <> symbol) {
|
||||
if ((symbol == 0) and (step >= 5)) or ((symbol <> 0) and (step >= 7)) {
|
||||
k = 0
|
||||
while k < step {
|
||||
this.good_for_rle[((i ~mod- k) ~mod- 1) & 1023] = 1
|
||||
k ~mod+= 1
|
||||
}
|
||||
}
|
||||
step = 1
|
||||
if i <> length {
|
||||
symbol = this.ctr(wb: args.wb, off: args.off, i: i)
|
||||
}
|
||||
} else {
|
||||
step ~mod+= 1
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
|
||||
// 3) Let's replace those population counts that lead to more RLE codes.
|
||||
// Math here is in 24.8 fixed point representation.
|
||||
stride = 0
|
||||
limit = this.rle_limit3(wb: args.wb, off: args.off, i: 0)
|
||||
sum = 0
|
||||
i = 0
|
||||
while i <= length {
|
||||
cond = (i == length)
|
||||
if not cond {
|
||||
cond = (this.good_for_rle[i & 1023] <> 0)
|
||||
}
|
||||
if (not cond) and (i <> 0) {
|
||||
cond = (this.good_for_rle[(i - 1) & 1023] <> 0)
|
||||
}
|
||||
if not cond {
|
||||
cond = ((((this.ctr(wb: args.wb, off: args.off, i: i) ~mod* 256) as base.u64) ~mod- limit) ~mod+ 1240) >= 2480
|
||||
}
|
||||
if cond {
|
||||
if (stride >= 4) or ((stride >= 3) and (sum == 0)) {
|
||||
// The stride must end, collapse what we have, if we have
|
||||
// enough (4).
|
||||
count = (sum ~mod+ (stride / 2)) / stride.max(no_less_than: 1)
|
||||
if count == 0 {
|
||||
count = 1
|
||||
}
|
||||
if sum == 0 {
|
||||
// Don't make an all zeros stride to be upgraded to ones.
|
||||
count = 0
|
||||
}
|
||||
k = 0
|
||||
while k < stride {
|
||||
// We don't want to change value at counts[i], that is
|
||||
// already belonging to the next stride. Thus - 1.
|
||||
this.poke_u32!(s: args.wb, i: args.off ~mod+ (((i ~mod- k) ~mod- 1) ~mod* 4),
|
||||
v: (count & 0xFFFF_FFFF) as base.u32)
|
||||
k ~mod+= 1
|
||||
}
|
||||
}
|
||||
stride = 0
|
||||
sum = 0
|
||||
if (i ~mod+ 2) < length {
|
||||
// All interesting strides have a count of at least 4, at
|
||||
// least when non-zeros.
|
||||
limit = this.rle_limit3(wb: args.wb, off: args.off, i: i)
|
||||
} else if i < length {
|
||||
limit = (this.ctr(wb: args.wb, off: args.off, i: i) ~mod* 256) as base.u64
|
||||
} else {
|
||||
limit = 0
|
||||
}
|
||||
}
|
||||
stride ~mod+= 1
|
||||
if i <> length {
|
||||
sum ~mod+= this.ctr(wb: args.wb, off: args.off, i: i) as base.u64
|
||||
if stride >= 4 {
|
||||
limit = ((sum ~mod* 256) ~mod+ (stride / 2)) / stride.max(no_less_than: 1)
|
||||
}
|
||||
if stride == 4 {
|
||||
limit ~mod+= 120
|
||||
}
|
||||
}
|
||||
i ~mod+= 1
|
||||
}
|
||||
}
|
||||
|
||||
// ctr returns counts[i], the u32le at args.wb[off + (4 * i) ..].
|
||||
pri func encoder.ctr(wb: roslice base.u8, off: base.u64, i: base.u64) base.u32 {
|
||||
return this.load32le(s: args.wb, i: args.off ~mod+ (args.i ~mod* 4))
|
||||
}
|
||||
|
||||
// rle_limit3 returns the C code's (size_t)(256 * (counts[i] + counts[i + 1]
|
||||
// + counts[i + 2]) / 3 + 420), computed in uint32_t arithmetic.
|
||||
pri func encoder.rle_limit3(wb: roslice base.u8, off: base.u64, i: base.u64) base.u64 {
|
||||
var s : base.u32
|
||||
|
||||
s = this.ctr(wb: args.wb, off: args.off, i: args.i)
|
||||
s ~mod+= this.ctr(wb: args.wb, off: args.off, i: args.i ~mod+ 1)
|
||||
s ~mod+= this.ctr(wb: args.wb, off: args.off, i: args.i ~mod+ 2)
|
||||
return (((s ~mod* 256) / 3) ~mod+ 420) as base.u64
|
||||
}
|
||||
Reference in new issue
Block a user