Skip to content

Decode odd crops and odd-sized rotations in decode_bounded - #39

Merged
laurenspriem merged 1 commit into
ente:mainfrom
laurens-pilot:feat/bounded-odd-chroma-transforms
Nov 25, 2026
Merged

laurenspriem merged 1 commit into
ente:mainfrom
laurens-pilot:feat/bounded-odd-chroma-transforms

Conversation

@laurens-pilot

Copy link
Copy Markdown

decode_bounded refused every HEIC for which libheif converts 4:2:0 chroma to 4:4:4 first: a clap with an odd origin, or an irot/imir of an odd-sized image. It now decodes them the way the regular decoders do (bilinear chroma, the limited-range round trip, the late colr override), building 4:4:4 a band of rows at a time while each tile is converted to RGB. Whether to convert is decided with libheif_converts_to_yuv444_before, judged at the size the image has when each transform applies.

On grids, tiles are converted in units shifted one pixel up and left. Each unit then only needs the last chroma row and column of tiles decoded before it, and those edges are kept in canvas-wide strips. Grids that need no conversion keep the old single-pass decode. The incremental experiment uses the same converter. That adds the round trip it was missing and lets crops come after orientation; grids of several tiles that need the conversion are still refused there. Tile-level transforms and 4:2:2 (unsupported by the bounded HEVC decoder) stay refused.

generate-bounded.rb's refuse-* cases now decode, and against pinned libheif 1.21.2 they differ only on the converted image's edge pixels. That is the pinned border defect: decode_bounded matches the regular decoder exactly on all seven. They are replaced by pass-* cases that end with a 1-pixel trim, including 2×2 and 3×2 grids whose seams fall inside the converted area, so CI's --require-pass covers the new paths.

Evidence, on a fresh generate.rb corpus and the pinned oracle:

  • verify-bounded.rb over the CI corpora went from 106 to 148 passes with no new failures. The odd-crop and 4:2:0 orientation/ files, and the new pass-* grids, are exact at full size and within ±1 at side 97.
  • 64 MP 8×8 odd grids (odd-origin crop, 90° turn, mirror plus trim) at the default 128 MiB budget peak at 122.6 MB at side 6000, the same as an even 8000×8000 control. At side 97 they peak at 55 MB, against 43 MB for the control. They take about 30–60% more CPU than the control. The machine was heavily loaded, so I compared best wall times: the non-converting path matched main (479 vs 529 ms at side 6000, 156 vs 165 ms at 97).

Source fork PR: laurens-pilot#28

@laurenspriem

decode_bounded refused every file for which libheif converts 4:2:0
chroma to 4:4:4 first: a clean aperture with an odd origin, or a
rotation or mirror of an odd-sized image. It now converts them the way
the regular decoders do, with bilinear chroma, the limited-range round
trip and the late colr override, one band of rows at a time while each
tile is converted to RGB.

Grid tiles are converted in units shifted one pixel up and left, so each
unit only needs the last chroma row and column of tiles decoded before
it. Those edges are kept in canvas-wide strips between the decode and
render phases. Grids that need no conversion keep the single-pass path.

The incremental experiment shares the converter, which adds the
round trip it skipped and judges the conversion with libheif's walker
instead of the final crop's parity, so crops after orientation work.
generate-bounded.rb replaces its refuse-* cases with pass-* cases,
including tiled grids, that trim the converted image's edges.
@laurens-pilot

Copy link
Copy Markdown
Author

@codex review

@laurenspriem
laurenspriem merged commit befcf21 into ente:main Nov 25, 2026
3 checks passed
@laurens-pilot
laurens-pilot deleted the feat/bounded-odd-chroma-transforms branch November 25, 2026 12:39
laurenspriem added a commit to ente/ente that referenced this pull request Nov 26, 2026
Update `ente-image`'s HEIC decoder pin from `5fbe1d2` to
[`5b0a6af`](ente/heic-decoder@5fbe1d2...5b0a6af).
It brings:

- Faster bounded decoding of grid HEICs (ente/heic-decoder#41). Measured
there on a Pixel 10, `decode_bounded` took 0.62–0.77× as long as the
regular decoder on 12–24 MP phone HEICs, against 1.14–1.29× before. This
is the path ML indexing, face thumbnails, OCR and the internal Android
HEIC viewer use.
- `decode_bounded` now decodes HEICs with an odd-origin crop or an
odd-sized rotation or mirror instead of refusing them
(ente/heic-decoder#39).
- The regular decoder now matches libheif on those crops, rotations and
mirrors in HEIC and AVIF, converting chroma to 4:4:4 before the
transform (ente/heic-decoder#37, #38, #40). A grid whose tiles need
different conversions now fails, as it does in libheif.
- AVIF items without a `colr` nclx property now use the AV1 stream's
range and matrix (ente/heic-decoder#40).

On 309 HEIC, HEIF and AVIF files (ente's test fixtures, phone HEICs,
libheif's examples, fuzz and stress files, and heic-decoder's generated
corpora), both decoders give the same pixels or the same refusal under
both pins on 294, including every real phone photo. The other 15 are
generated odd-crop and odd-size rotation and mirror files: the regular
decoder now gives different pixels, and `decode_bounded` decodes them
instead of refusing, with exactly the regular decoder's output.

Source fork PR: laurens-pilot#308

@laurenspriem

Co-authored-by: laurenspriem <laurens-pilot-git@lrns.me>
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