Expand description
Translation of src/utils/bit_reader_utils.c and src/utils/bit_reader_utils.h from libwebp v1.6.0.
Work package: WP2 (bit I/O).
The readers hold no reference to their input (ADR 0003, as amended on
2026-11-06). VP8BitReader and VP8LBitReader store state and positions:
every C pointer becomes a usize position into the input that the owner holds,
and the owner passes that input to each function that reads it. A C buf that
may be NULL becomes the flag VP8BitReader::has_buf.
VP8RemapBitReader adds a signed offset to the three positions of a reader
with an input, as C adds it to the pointers. The owner calls it when the same
bytes have moved by that many indexes of its input.
The bit-trace tool (BITTRACE) is off in the reference configuration, so the
labels of the reader functions are removed, as the header’s macros do.
§Deviations from upstream
Changes of representation that the ADRs allow, or behavior that the Rust side cannot reproduce exactly. Each records the reason.
- Public fields. The fields of the four structs (
VP8BitReader,VP8LBitReader,VP8BitWriterandVP8LBitWriter) arepub, as the C structs are public in their headers, so that the tests can read the state. The C fieldbuf_endis stored asVP8BitReader::buf_end, because it no longer comes from a slice length;VP8LBitReader::lenis stored for the same reason. - Remap by an index delta.
VP8RemapBitReaderadds a signed offset to the positions. The owner passes the index delta of its bytes, not the pointer difference of two allocations, which C’s caller computes. Both sides of the remap test move the input inside one allocation, so that the C pointer difference and the index delta are the same number. The test does not move the input to a second allocation, because a pointer difference between two allocations contains the distance of their bases, which an index delta does not.
§Excluded undefined behavior
Inputs that are undefined behavior (or assertion failures) in C and are therefore excluded from the oracle comparison (PORTING rule 8). The asan-ubsan oracle found these, and the generators keep the tests away from them:
VP8LBitReaderSetBufferwith a length shorter than the reader’s position is a parameter error (the comment of the function says so), and upstream’s own debug assertion inVP8LIsEndOfStreamfires there. The Rust debug assertion mirrors it. The tests substitutefillfor such a call.VP8LDoFillBitWindowrequiresbit_pos >= VP8L_WBITS(debug assertion). The tests call it only there.VP8LReadBitswith a negative count indexeskBitMaskout of range. The Rust index panics in every profile, after the debug assertion; C reads outside the table. No test issues it.VP8RemapBitReaderwith a position that would become negative is outside the contract of the owner (the pointer would leave its allocation). The Rust version panics; no test issues it.
§Checks not applicable
These checks do not apply, or cannot be made, with the reason:
- Layout of
repr(C)mirrors. None of the four structs is exported by a public header; the C declarations are internal tobit_reader_utils.handbit_writer_utils.h. ADR 0004 gives no ABI mirror to them. - The other
BITSvalues of the header (8, 16, 32 and 48) are selected by no target that upstream names, so they are not translated.bit_tandlbit_tfollow the two selected values.
Structs§
- VP8Bit
Reader - The boolean decoder: its value window, its range, and the positions of its input. It holds no reference to the input; see the module documentation.
- VP8L
BitReader - The lossless bit reader: a window of prefetched bits and the positions of its input. It holds no reference to the input; see the module documentation.
Constants§
- BITS
- Number of bits that the boolean decoder loads at once. Upstream chooses 56 on
x86-64,
AArch64and WebAssembly, and 24 on every other target; this constant makes the same choice for the Rust target architecture. - VP8L_
LBITS - Number of bits prefetched by the lossless reader: the bit size of
vp8l_val_t. - VP8L_
MAX_ NUM_ BIT_ READ - Largest number of bits one
VP8LReadBitscall may read. - VP8L_
USE_ FAST_ LOAD - Whether
VP8LDoFillBitWindowreads four bytes at once. Upstream defines the macro for ARM,AArch64, x86, x86-64 and WebAssembly. - VP8L_
WBITS - Minimum number of bits that
VP8LFillBitWindowmakes available. - VP8_
LOG2_ RANGE kVP8Log2Range[range]is the number of renormalization shifts forrange.- VP8_
NEW_ RANGE kVP8NewRange[range]is the renormalized range:((range + 1) << shift) - 1.
Functions§
- vp8_
bit_ reader_ set_ buffer - Gives the reader an input of
sizebytes, which the owner passes from now on, and rewinds the reader to the first byte. The value, the range and the end-of-input flag are not changed. - vp8_get
- Reads one bit with probability one half. The header’s macro of the same name
expands to
VP8GetValue(br, 1). - vp8_
get_ signed_ value - Reads
num_bitsbits, then a sign bit that negates the value when set. - vp8_
get_ value - Reads
num_bitsbits, most significant first, each with probability one half. - vp8_
init_ bit_ reader - Initializes the boolean decoder over
data, which is the whole input of the reader, and loads its first bytes. - vp8_
load_ final_ bytes - Loads one byte at a time near the end of the input. Once the input is
exhausted, the first call sets
eofand shifts in zeros, and later calls only keep the state. - vp8_
remap_ bit_ reader - Adds
offsetto the positions of a reader that has an input, as C adds it to the pointers. The owner calls it when the same bytes have moved byoffsetindexes of its input. A reader without an input (Cbuf == NULL) is left as it is. - vp8l_
bit_ reader_ set_ buffer - Sets a new input length, which the owner passes from now on. The position and
the prefetched bits are kept, and the end-of-stream flag is recomputed, so a
position past the new end sets
eos. - vp8l_
do_ fill_ bit_ window - Advances the input by four bytes to make room for the next 32 bits. With
VP8L_USE_FAST_LOADit reads four bytes at once while nine or more remain; otherwise, and near the end, it shifts in byte by byte. - vp8l_
fill_ bit_ window - Makes room for the next 32 bits when fewer than 32 bits are prefetched.
- vp8l_
init_ bit_ reader - Initializes the lossless reader over
data, which is the whole input of the reader, and prefetches its first eight bytes. - vp8l_
is_ end_ of_ stream - Reports whether a bit was read past the end of the input. Does not set
eos. - vp8l_
prefetch_ bits - Returns the prefetched bits, so that they can be looked up.
- vp8l_
read_ bits - Reads
n_bitsbits. Reads with more thanVP8L_MAX_NUM_BIT_READbits, or after the end of the stream, return zero and seteos. - vp8l_
set_ bit_ pos - Jumps to the bit position
valof the prefetched bits. Does not seteos; use with care.
Type Aliases§
- bit_t
- Natural register type of the boolean decoder’s
value: 64 bits whenBITS > 24, and 32 bits otherwise. - range_t
- Register for
range, which holds eight significant bits. - vp8l_
val_ t - The prefetched bits of the lossless reader: the right register is 64 bits.