wai-rs: jpegai_bitstream — codestream container + MSB-first header bit reader #16

Merged
dcharlot merged 1 commit from feat/jpegai-bitstream into main 2026-07-16 04:52:46 -04:00
Owner

The decoder entry point. A codestream is a sequence of marker segments: marker(2B BE) + expgolomb_k0(size) + byte-align + payload. Header substreams (PIH/TON) are plain bit-packed, read MSB-first — headers are NOT arithmetic-coded (only latent substreams are, → jpegai_mans).

  • BitReader — faithful port of the reference EcLibDirect::read_bits (MSB-first, bit-granular cursor)
  • read_expgolomb_k0 — segment payload lengths
  • parse_container — marker walk → [Segment]

Validated on the real test256.bin codestream: full layout SOC/PIH(49)/TON(63)/RDI(1)/SORP(97)/SORS(47)/SOZ(83)/EOC, picture-header prefix → 256x256, 4:4:4, colour_transform_idx=1. Cross-check: SORP/SORS/SOZ sizes (97/47/83) equal the mANS mem lengths — container parse and byte-exact entropy decode agree on the same bytes.

The decoder entry point. A codestream is a sequence of marker segments: `marker(2B BE) + expgolomb_k0(size) + byte-align + payload`. Header substreams (PIH/TON) are plain bit-packed, read MSB-first — headers are NOT arithmetic-coded (only latent substreams are, → jpegai_mans). - `BitReader` — faithful port of the reference `EcLibDirect::read_bits` (MSB-first, bit-granular cursor) - `read_expgolomb_k0` — segment payload lengths - `parse_container` — marker walk → `[Segment]` **Validated on the real test256.bin codestream:** full layout SOC/PIH(49)/TON(63)/RDI(1)/SORP(97)/SORS(47)/SOZ(83)/EOC, picture-header prefix → 256x256, 4:4:4, colour_transform_idx=1. **Cross-check: SORP/SORS/SOZ sizes (97/47/83) equal the mANS `mem` lengths** — container parse and byte-exact entropy decode agree on the same bytes.
The decoder's entry point. A codestream is a sequence of marker segments; each (except
the marker-only SOC/EOC) is `marker(2B big-endian) + expgolomb_k0(size) + byte-align +
payload`. The header substreams (PIH/TON) are plain bit-packed and read MSB-first — the
headers are NOT arithmetic-coded (only the latent substreams are, and those go to
jpegai_mans).

  BitReader        faithful port of the reference EcLibDirect::read_bits — MSB-first
                   within each byte, accumulated MSB-first, bit-granular cursor, reads
                   past end return the available bits (matching the bounds check)
  read_expgolomb_k0  unsigned exp-Golomb k=0, for the segment payload lengths
  parse_container  walk the markers -> [Segment{marker, start, len}]

Validated against the real test256.bin codestream: full segment layout
SOC/PIH(49)/TON(63)/RDI(1)/SORP(97)/SORS(47)/SOZ(83)/EOC, and the picture-header prefix
decodes to 256x256, 4:4:4, colour_transform_idx=1, level 52. Cross-check: the
SORP/SORS/SOZ payload sizes (97/47/83) equal the `mem` lengths the byte-exact mANS
decoder consumes — the container parse and the entropy decode agree on the same bytes.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Transaction-Science/open-standards!16
No description provided.