Skip to main content

Module bit_reader_utils

Module bit_reader_utils 

Source
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.

  1. Public fields. The fields of the four structs (VP8BitReader, VP8LBitReader, VP8BitWriter and VP8LBitWriter) are pub, as the C structs are public in their headers, so that the tests can read the state. The C field buf_end is stored as VP8BitReader::buf_end, because it no longer comes from a slice length; VP8LBitReader::len is stored for the same reason.
  2. Remap by an index delta. VP8RemapBitReader adds 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:

  1. VP8LBitReaderSetBuffer with 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 in VP8LIsEndOfStream fires there. The Rust debug assertion mirrors it. The tests substitute fill for such a call.
  2. VP8LDoFillBitWindow requires bit_pos >= VP8L_WBITS (debug assertion). The tests call it only there.
  3. VP8LReadBits with a negative count indexes kBitMask out of range. The Rust index panics in every profile, after the debug assertion; C reads outside the table. No test issues it.
  4. VP8RemapBitReader with 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:

  1. Layout of repr(C) mirrors. None of the four structs is exported by a public header; the C declarations are internal to bit_reader_utils.h and bit_writer_utils.h. ADR 0004 gives no ABI mirror to them.
  2. The other BITS values of the header (8, 16, 32 and 48) are selected by no target that upstream names, so they are not translated. bit_t and lbit_t follow the two selected values.

Structs§

VP8BitReader
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.
VP8LBitReader
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, AArch64 and 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 VP8LReadBits call may read.
VP8L_USE_FAST_LOAD
Whether VP8LDoFillBitWindow reads four bytes at once. Upstream defines the macro for ARM, AArch64, x86, x86-64 and WebAssembly.
VP8L_WBITS
Minimum number of bits that VP8LFillBitWindow makes available.
VP8_LOG2_RANGE
kVP8Log2Range[range] is the number of renormalization shifts for range.
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 size bytes, 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_bits bits, then a sign bit that negates the value when set.
vp8_get_value
Reads num_bits bits, 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 eof and shifts in zeros, and later calls only keep the state.
vp8_remap_bit_reader
Adds offset to 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 by offset indexes of its input. A reader without an input (C buf == 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_LOAD it 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_bits bits. Reads with more than VP8L_MAX_NUM_BIT_READ bits, or after the end of the stream, return zero and set eos.
vp8l_set_bit_pos
Jumps to the bit position val of the prefetched bits. Does not set eos; use with care.

Type Aliases§

bit_t
Natural register type of the boolean decoder’s value: 64 bits when BITS > 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.