Files
wuffs/std/brotli/encode_bit_writer.wuffs
T
Claude ca1ba9d134 std/brotli: add an encoder for quality levels 0 and 1
Adds a brotli encoder (std/brotli's encoder struct, a base.io_transformer)
whose output is byte-identical to libbrotlienc v1.2.1 for quality levels 0
and 1, mirroring encode.c's BrotliEncoderCompressStream(Fast),
compress_fragment.c and compress_fragment_two_pass.c, plus the entropy
coding and bit stream helpers they need. Every double operation goes
through std/softfloat, following the C evaluation order exactly.

Parameters are quirks (encode_quirks.wuffs) that mirror
BrotliEncoderSetParameter, sanitized at the first transform_io call.
Each transform_io resumption corresponds to one C call with available_in
equal to the readable src length; a closed src is FINISH; request_flush!
and request_emit_metadata! make the next resumption a FLUSH or
EMIT_METADATA call. Qualities 2 to 11 return "#unsupported encoder
quality" for now. Encoder tables are generated by
script/print-brotli-tables.go -encoder.

Tests: round trips through the Wuffs decoder over the corpus and synthetic
inputs; with -mimic, byte-exact comparisons against libbrotlienc over
quality × window × mode × input chunking × flush schedule × metadata ×
size hint. script/manual-test-brotli-encoder-parity.c runs the full
grid: 86,268 configurations, zero mismatches.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011NoosQNe7DKWnqDCBeqw1p
2026-10-26 23:55:56 +00:00

260 lines
8.2 KiB
Plaintext

// 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 holds the encoder's bit writer and other small helpers shared by
// the quality 0 and quality 1 code paths. The bit position (the C code's
// *storage_ix) is the encoder.bw_ix field. The storage (the C code's uint8_t*
// storage) is a slice argument.
// Mirrors BrotliWriteBits in write_bits.h (its BROTLI_LITTLE_ENDIAN branch).
//
// Like the C code, this ORs the bits into the byte at (bw_ix >> 3) and then
// stores 8 bytes, little-endian, zeroing any bits above the newly written
// ones. The byte-exact output depends on that zeroing, as later code reads
// back (and ORs into) those bytes. The storage slice always has enough slack
// that the 8-byte store fits. If it ever did not, the bits are written byte
// by byte, as far as they fit.
pri func encoder.write_bits!(n_bits: base.u32, bits: base.u64, s: slice base.u8) {
var p : base.u64
var v : base.u64
var q : slice base.u8
p = this.bw_ix >> 3
if p < args.s.length() {
q = args.s[p ..]
v = (args.s[p] as base.u64) | (args.bits ~mod<< (this.bw_ix & 7))
if q.length() >= 8 {
q.poke_u64le!(a: v)
} else {
while q.length() > 0 {
q[0] = (v & 0xFF) as base.u8
v >>= 8
q = q[1 ..]
}
}
}
this.bw_ix ~mod+= args.n_bits as base.u64
}
// Mirrors JumpToByteBoundary in brotli_bit_stream.c, without the zeroing
// (which the C code's callers in this file rely on the 8-byte stores for).
// The "(*storage_ix + 7u) & ~7u" expressions in compress_fragment.c and
// compress_fragment_two_pass.c.
pri func encoder.align_to_byte!() {
this.bw_ix = (this.bw_ix ~mod+ 7) & 0xFFFF_FFFF_FFFF_FFF8
}
// Mirrors RewindBitPosition in compress_fragment.c and
// compress_fragment_two_pass.c.
pri func encoder.rewind_bit_position!(new_ix: base.u64, s: slice base.u8) {
var p : base.u64
var mask : base.u8
p = args.new_ix >> 3
mask = (((1 as base.u32) << (args.new_ix & 7)) - 1) as base.u8
if p < args.s.length() {
args.s[p] &= mask
}
this.bw_ix = args.new_ix
}
// Mirrors UpdateBits in compress_fragment.c.
pri func encoder.update_bits!(n_bits: base.u32, bits: base.u32, pos: base.u64, s: slice base.u8) {
var n_bits : base.u32
var bits : base.u32
var pos : base.u64
var byte_pos : base.u64
var n_unchanged_bits : base.u32[..= 7]
var n_changed_bits : base.u32[..= 8]
var total_bits : base.u32[..= 15]
var mask : base.u32
var unchanged_bits : base.u32
var changed_bits : base.u32
n_bits = args.n_bits
bits = args.bits
pos = args.pos
while n_bits > 0 {
byte_pos = pos >> 3
n_unchanged_bits = (pos & 7) as base.u32
n_changed_bits = n_bits.min(no_more_than: 8 - n_unchanged_bits)
total_bits = n_unchanged_bits + n_changed_bits
mask = (0xFFFF_FFFF ^ (((1 as base.u32) << total_bits) - 1)) |
(((1 as base.u32) << n_unchanged_bits) - 1)
if byte_pos >= args.s.length() {
return nothing
}
unchanged_bits = (args.s[byte_pos] as base.u32) & mask
changed_bits = bits & (((1 as base.u32) << n_changed_bits) - 1)
args.s[byte_pos] = (((changed_bits << n_unchanged_bits) | unchanged_bits) & 0xFF) as base.u8
n_bits ~mod-= n_changed_bits
bits = bits >> n_changed_bits
pos ~mod+= n_changed_bits as base.u64
}
}
// Mirrors BrotliStoreMetaBlockHeader in compress_fragment.c (and the identical
// function in compress_fragment_two_pass.c).
pri func encoder.store_meta_block_header!(len: base.u64, is_uncompressed: base.bool, s: slice base.u8) {
var nibbles_minus_4 : base.u32[..= 2]
nibbles_minus_4 = 2
// ISLAST.
this.write_bits!(n_bits: 1, bits: 0, s: args.s)
if args.len <= 0x1_0000 {
nibbles_minus_4 = 0
} else if args.len <= 0x10_0000 {
nibbles_minus_4 = 1
}
this.write_bits!(n_bits: 2, bits: nibbles_minus_4 as base.u64, s: args.s)
this.write_bits!(n_bits: (nibbles_minus_4 + 4) * 4, bits: args.len ~mod- 1, s: args.s)
// ISUNCOMPRESSED.
if args.is_uncompressed {
this.write_bits!(n_bits: 1, bits: 1, s: args.s)
} else {
this.write_bits!(n_bits: 1, bits: 0, s: args.s)
}
}
// Mirrors Log2FloorNonZero in fast_log.h, which (with BROTLI_BSR32) truncates
// its argument to 32 bits. It returns 0 for an argument of 0.
pri func encoder.log2_floor_non_zero(n: base.u64) base.u32[..= 31] {
var x : base.u32
var r : base.u32
x = (args.n & 0xFFFF_FFFF) as base.u32
if x >= 0x1_0000 {
x >>= 16
r = 16
}
if x >= 0x100 {
x >>= 8
r ~mod+= 8
}
if x >= 0x10 {
x >>= 4
r ~mod+= 4
}
if x >= 4 {
x >>= 2
r ~mod+= 2
}
if x >= 2 {
r ~mod+= 1
}
return r & 31
}
// Mirrors FastLog2 in fast_log.h. The result is the bit pattern of an IEEE
// 754 binary64 value.
pri func encoder.fast_log2(v: base.u64) base.u64 {
if args.v < 256 {
return LOG2_TABLE[args.v]
}
return this.sf.log2_u64(v: args.v)
}
// Mirrors BrotliBitsEntropy in bit_cost.c, over this.histogram (with size
// 256). The result is the bit pattern of an IEEE 754 binary64 value.
pri func encoder.bits_entropy_of_histogram() base.u64 {
var sum : base.u64
var retval : base.u64
var i : base.u32
var p : base.u64
i = 0
while i < 256 {
p = this.histogram[i] as base.u64
sum ~mod+= p
retval = this.sf.f64_sub(a: retval, b: this.sf.f64_mul(
a: this.sf.f64_from_u64(a: p), b: this.fast_log2(v: p)))
i += 1
}
if sum <> 0 {
retval = this.sf.f64_add(a: retval, b: this.sf.f64_mul(
a: this.sf.f64_from_u64(a: sum), b: this.fast_log2(v: sum)))
}
if this.sf.f64_lt(a: retval, b: this.sf.f64_from_u64(a: sum)) <> 0 {
retval = this.sf.f64_from_u64(a: sum)
}
return retval
}
// load64le returns the 8 bytes (little-endian) of s starting at i, like the C
// code's BROTLI_UNALIGNED_LOAD64LE. The C code never reads out of bounds, so
// the zero result for an out of bounds read should never be used.
pri func encoder.load64le(s: roslice base.u8, i: base.u64) base.u64 {
var q : roslice base.u8
if args.i < args.s.length() {
q = args.s[args.i ..]
if q.length() >= 8 {
return q.peek_u64le()
}
}
return 0
}
// load32le is like load64le but for 4 bytes.
pri func encoder.load32le(s: roslice base.u8, i: base.u64) base.u32 {
var q : roslice base.u8
if args.i < args.s.length() {
q = args.s[args.i ..]
if q.length() >= 4 {
return q.peek_u32le()
}
}
return 0
}
// Mirrors FindMatchLengthWithLimit in find_match_length.h: the length of the
// common prefix of s[i1 ..] and s[i2 ..], up to limit bytes.
pri func encoder.find_match_length_with_limit(s: roslice base.u8, i1: base.u64, i2: base.u64, limit: base.u64) base.u64 {
var matched : base.u64
var n : base.u64
var a : roslice base.u8
var b : roslice base.u8
var x : base.u64
if (args.i1 > args.s.length()) or (args.i2 > args.s.length()) {
return 0
}
a = args.s[args.i1 ..]
b = args.s[args.i2 ..]
n = args.limit
while (n >= 8) and (a.length() >= 8) and (b.length() >= 8) {
x = a.peek_u64le() ^ b.peek_u64le()
if x <> 0 {
while (x & 0xFF) == 0 {
matched ~mod+= 1
x >>= 8
}
return matched
}
matched ~mod+= 8
n -= 8
a = a[8 ..]
b = b[8 ..]
}
while (n > 0) and (a.length() > 0) and (b.length() > 0) {
if a[0] <> b[0] {
break
}
matched ~mod+= 1
n -= 1
a = a[1 ..]
b = b[1 ..]
}
return matched
}