Skip to content

Decode HEVC tiles row by row on 32-bit targets, and add decode_bounded_tiny - #43

Merged
laurenspriem merged 7 commits into
ente:mainfrom
laurens-pilot:frame-row-decoding
Dec 2, 2026
Merged

laurenspriem merged 7 commits into
ente:mainfrom
laurens-pilot:frame-row-decoding

Conversation

@laurens-pilot

Copy link
Copy Markdown

The Ente e-ink frame (ESP32-S3 with 8 MB PSRAM) needs to decode iPhone HEIC originals without holding whole tiles in memory. This adds:

  • AVIF decoding behind a new default avif feature. With default features off, the crate builds for xtensa-esp32s3-espidf. Without avif, AVIF decodes return DecodeAvifError::DecoderUnavailable before reading the coded payload, and the image hooks leave avif/avis-branded files to other decoders. The temp spool counter is now an AtomicUsize. Builds with default features, such as ente-image, are unchanged; the new error variant means exhaustive matches on DecodeAvifError need an extra arm (ente-image has none).
  • WPP support in the row-band decoder, which every iPhone tile uses, handled the same way as in the whole-picture decoder.
  • rows::info and rows::decode_rows, which decode one coded item a CTU row at a time and hand out the Y/Cb/Cr samples.
  • decode_bounded_tiny, a separate bounded decode for small-memory devices. It takes the same options and returns the same image as decode_bounded, but decodes one coded item at a time, a CTU row at a time, on the calling thread. decode_bounded itself is unchanged.

On 452 local HEIC/HEIF/AVIF files at max side 6000, 1600 and 512, decode_bounded_tiny gave byte-identical output to decode_bounded on all 480 decodes where both succeed. It refused only the 13 10-bit files that decode_bounded now decodes, and it also decodes 8 large single images that decode_bounded refuses for memory. Under an allocator that refuses anything beyond the budget, every one of its 336 decodes at 1600 and 512 px succeeded at the smallest budget it admits.

Memory beyond the output at 1600 px, measured on the Mac:

Photo decode_bounded_tiny decode_bounded (single-threaded)
12 MP iPhone grids 0.6–1.1 MB about 1.8 MB
14604×3826 iPhone pano 1.0 MB 6.3 MB

Speed is the same or slightly faster: 186 ms vs 213 ms for a 12 MP photo. Admission is conservative: iPhone photos need a budget of output + 1.2 to 3.8 MB, of which 2 MiB is the colour-transform reservation shared with decode_bounded. It refuses a few inputs decode_bounded accepts: 10- and 12-bit and PQ/HLG images (which decode_bounded tone maps since #42), parameter sets outside hvcC, HEVC tiles, over 256 MP, and multi-tile grids needing 4:4:4 chroma. API.md lists them, and rows::decode_rows likewise refuses 10- and 12-bit items.

Not covered: WPP pictures only one or two CTUs wide, which no available encoder produces. Not yet run on the frame itself.

Source fork PR: laurens-pilot#31

@laurenspriem

rav1d needs 64-bit atomics, which 32-bit targets such as
xtensa-esp32s3-espidf lack. AVIF decoding now sits behind a default
`avif` feature; without it, AVIF decodes return DecoderUnavailable.
The temp spool counter becomes an AtomicUsize.
At the end of each CTU row, the in-memory row decoder now reads
end_of_subset_one_bit, moves to the next entry point and restores the
contexts saved after the row's second CTU, like the whole-picture
decoder. The entry point adjustment for emulation prevention bytes moves
out of decode_slice so both paths share it. The streaming path still
rejects WPP because it cannot seek.
rows::info and rows::decode_rows decode one HEVC coded item, such as a
grid tile, one CTU row band at a time. The tests compare every band with
the whole-tile decode on the in-repo Apple WPP fixtures and on a
single-substream slice.
Without the avif feature, image-integration no longer registers the
AVIF decoding hook or the avif/avis format detection hooks, so they
cannot shadow another AVIF decoder, and the registration reports the
AVIF hook as not registered. API.md now lists the real default
features and the row API's emulation prevention bookkeeping.
decode_bounded_tiny takes the same options and returns the same pixels as
decode_bounded, but decodes one coded item at a time on the calling
thread, a CTU row at a time, and reduces each row into the output straight
away. WPP items are decoded from a copy of the item's payload; other items
are streamed from the input. Preflight reads only item headers, and
admission reserves the output plus the largest row workspace instead of a
whole tile's reconstruction. decode_bounded is unchanged.
…refuses

decode_bounded_tiny now reserves header-parsing memory from the actual
parameter-set and slice-prefix sizes, so crafted 64 KiB parameter sets can
no longer push it past its budget, and it charges only the three CTU rows
of SAO state it keeps. It refuses a grid primary that carries an hvcC, as
decode_bounded does. Without the avif feature, AVIF decodes return
DecoderUnavailable before reading the coded payload. API.md lists the
inputs tiny refuses and the AVIF content-guessing limit.
decode_bounded now decodes 10- and 12-bit items and tone maps PQ and HLG,
while decode_bounded_tiny and rows::decode_rows still decode 8-bit SDR
only. API.md lists those refusals, and a test checks that decode_rows
refuses a 10-bit item instead of decoding it into 8-bit bands.
@laurens-pilot

Copy link
Copy Markdown
Author

@codex review

@laurenspriem
laurenspriem merged commit 8033ab8 into ente:main Dec 2, 2026
3 checks passed
@laurens-pilot
laurens-pilot deleted the frame-row-decoding branch December 2, 2026 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants