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
This commit is contained in:
Claude committed 2026-10-26 23:55:56 +00:00
1 parent 4978207531
commit ca1ba9d134
17 files changed
+13990 -2772

No files matched your search

+86
View File
@@ -13,11 +13,14 @@
// The reference implementation for std/brotli is libbrotlidec v1.2.1 (from
// github.com/google/brotli). Older versions (such as v1.1.0) differ slightly
// in which error code some invalid inputs produce.
//
// The encoder's reference implementation is libbrotlienc v1.2.1.
#include <limits.h>
#include <stdint.h>
#include "brotli/decode.h"
#include "brotli/encode.h"
// mimic_brotli_decode_error_code is like mimic_brotli_decode but also stores
// the libbrotlidec error code (a BrotliDecoderErrorCode value) in
@@ -72,3 +75,86 @@ mimic_brotli_decode(wuffs_base__io_buffer* dst,
int error_code = 0;
return mimic_brotli_decode_error_code(dst, src, &error_code);
}
// ---------------- Encoder
// mimic_brotli_encode runs libbrotlienc's BrotliEncoderCompressStream over a
// "plan" of num_steps steps. Step i is the BrotliEncoderOperation step_ops[i]
// (0 = PROCESS, 1 = FLUSH, 2 = FINISH, 3 = EMIT_METADATA) applied to the next
// step_lens[i] bytes of src. Each step repeats that operation until all of its
// input is consumed and there is no more pending output (or, for FINISH,
// until the encoder is finished).
//
// Before the first step, each of the num_params (param_keys[j],
// param_values[j]) pairs is passed to BrotliEncoderSetParameter. The keys are
// BrotliEncoderParameter values.
//
// Each BrotliEncoderCompressStream call gets at most dst_chunk bytes of dst
// (or all of dst's writable bytes, if dst_chunk is zero).
const char* //
mimic_brotli_encode(wuffs_base__io_buffer* dst,
wuffs_base__io_buffer* src,
const uint32_t* param_keys,
const uint32_t* param_values,
size_t num_params,
const uint32_t* step_ops,
const size_t* step_lens,
size_t num_steps,
size_t dst_chunk) {
if (BrotliEncoderVersion() != 0x1002001) {
return "libbrotlienc: unexpected version (want v1.2.1)";
}
BrotliEncoderState* state = BrotliEncoderCreateInstance(NULL, NULL, NULL);
if (!state) {
return "libbrotlienc: BrotliEncoderCreateInstance failed";
}
const char* ret = NULL;
for (size_t j = 0; j < num_params; j++) {
if (!BrotliEncoderSetParameter(state,
(BrotliEncoderParameter)(param_keys[j]),
param_values[j])) {
ret = "libbrotlienc: BrotliEncoderSetParameter failed";
goto cleanup;
}
}
for (size_t i = 0; i < num_steps; i++) {
size_t avail_in = step_lens[i];
if (avail_in > wuffs_base__io_buffer__reader_length(src)) {
ret = "mimic_brotli_encode: step length exceeds the input";
goto cleanup;
}
const uint8_t* next_in = wuffs_base__io_buffer__reader_pointer(src);
BrotliEncoderOperation op = (BrotliEncoderOperation)(step_ops[i]);
while (true) {
size_t avail_out = wuffs_base__io_buffer__writer_length(dst);
if ((dst_chunk > 0) && (avail_out > dst_chunk)) {
avail_out = dst_chunk;
}
size_t avail_out0 = avail_out;
uint8_t* next_out = wuffs_base__io_buffer__writer_pointer(dst);
if (!BrotliEncoderCompressStream(state, op, &avail_in, &next_in,
&avail_out, &next_out, NULL)) {
ret = "libbrotlienc: BrotliEncoderCompressStream failed";
goto cleanup;
}
dst->meta.wi += avail_out0 - avail_out;
if (op == BROTLI_OPERATION_FINISH) {
if (BrotliEncoderIsFinished(state)) {
break;
}
} else if ((avail_in == 0) && !BrotliEncoderHasMoreOutput(state)) {
break;
}
if (wuffs_base__io_buffer__writer_length(dst) == 0) {
ret = "mimic_brotli_encode: dst is full";
goto cleanup;
}
}
src->meta.ri += step_lens[i];
}
cleanup:
BrotliEncoderDestroyInstance(state);
return ret;
}