Skip to content

Match libheif when transforms convert AVIF chroma to 4:4:4 - #40

Merged
laurenspriem merged 3 commits into
ente:mainfrom
laurens-pilot:fix/avif-odd-transform-chroma
Nov 25, 2026
Merged

laurenspriem merged 3 commits into
ente:mainfrom
laurens-pilot:fix/avif-odd-transform-chroma

Conversation

@laurens-pilot

Copy link
Copy Markdown

AVIF items converted to RGBA with nearest-neighbour chroma before their clap/irot/imir, so they differed from libheif wherever it first converts 4:2:0 or 4:2:2 to bilinear 4:4:4. That happens for crops with an odd origin and for rotations and mirrors of odd sizes. On 22 SVT-AV1 test files, 19 differed from heif-dec. Only the three controls that need no conversion matched. On photo content every pixel differed, by up to 21 levels at 8-bit, mostly from libheif's limited-range round trip and its late colr relabel. On a harsh chroma pattern the difference reached 211 levels.

These items now go through the HEIC code from #37 and #38. That covers plane transforms before the conversion, the 4:4:4 image built a band at a time, conversion with the coded colour, and the colr override applied afterwards. Items that need no conversion keep the existing AVIF path. An 8-bit item that needs the conversion has its planes widened to 16-bit for the decode.

The AVIF path also ignored the AV1 sequence header's colour: an item without a colr nclx property converted as full range with unspecified (BT.601) coefficients. It now uses the sequence header's range and matrix, as libheif does, and decode_primary_avif_to_image reports them. This changes RGBA output for such files, for example ICC-only AVIFs with a limited-range or BT.709 stream. A limited-range test file without colr differed by up to 47 levels before.

wrap.rb can now wrap an FFmpeg AVIF, and its HEVC output is byte-identical. generate.rb writes 16 AVIF cases to incremental-corpus/avif/: 8-bit and 10-bit, full and limited range, a colr override that differs from the stream, and items without colr. It uses libaom-av1 when FFmpeg has it and libsvtav1 otherwise. Locally, using SVT-AV1 4.2, all 16 cases match heif-dec exactly. The full default verify has 317 files, 264 exact and 0 failures. The bounded runner refuses the AVIF cases. No fixture covers 4:2:2 AVIF, which SVT-AV1 cannot encode, or alpha combined with a converting transform. Unit tests cover both through the shared code.

Source fork PR: laurens-pilot#27

@laurenspriem

AVIF items converted to RGBA with nearest-neighbour chroma before their
clap/irot/imir, so they differed from libheif wherever it first converts
4:2:0 or 4:2:2 to bilinear 4:4:4. Those items now go through the HEIC
conversion path: transforms before the conversion run on the planes, the
4:4:4 image is built in bands, the coded colour drives the conversion and
a colr nclx property applies afterwards.

The coded colour of an AV1 item is its sequence header's color_config.
AVIF items without a colr nclx property used to convert as full range with
unspecified coefficients; they now use the sequence header, as libheif does.

wrap.rb also wraps an FFmpeg AVIF, and generate.rb writes AVIF cases to
incremental-corpus/avif so default verify compares them with heif-dec.
@laurens-pilot

Copy link
Copy Markdown
Author

@codex review

@laurenspriem
laurenspriem merged commit ad8ce1b into ente:main Nov 25, 2026
3 checks passed
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