Skip to content

Tone map PQ and HLG HEICs in decode_bounded - #42

Merged
laurenspriem merged 2 commits into
ente:mainfrom
laurens-pilot:claude/heic-decoder-hdr-support-0e0715
Nov 30, 2026
Merged

laurenspriem merged 2 commits into
ente:mainfrom
laurens-pilot:claude/heic-decoder-hdr-support-0e0715

Conversation

@laurens-pilot

Copy link
Copy Markdown

decode_bounded refused every 10-bit HEIC, so HDR photos fell back to the regular decoder, which returns the raw PQ or HLG samples. Shown as sRGB, those look flat and washed out. Since the Ente viewer also uses decode_bounded, the tone mapping is aimed at viewing, not only thumbnails and ML.

decode_bounded now accepts 8-, 10- and 12-bit 4:2:0 HEVC and tone maps PQ and HLG to sRGB:

  • Signalling: the HDR transfer comes from an ICC cicp tag, otherwise the container nclx, otherwise the HEVC VUI.
  • Conversion to light: samples become display light with 203 cd/m² as SDR reference white. HLG goes through the BT.2100 OOTF for a 1000 cd/m² display.
  • Tone curve: colour is converted to BT.709, then the BT.2390 EETF is applied to max(R,G,B). The source peak is the clli or mdcv value, or 1000 cd/m² if neither is set.
  • Result: with a 1000 cd/m² peak, reference white encodes as 229/255 and highlights roll off to white.

Other high-bit-depth behaviour:

  • 10- and 12-bit SDR images decode exactly like the regular RGB8 path followed by the ICC conversion.
  • Malformed, repeated or zero clli/mdcv boxes never fail a decode.
  • The incremental experiment stays 8-bit SDR.

The regular and image-crate decodes are unchanged: their RGB8 and RGBA output hashes match main on 454 files.

The bounded oracle step in CI needed a change to the libheif reference. libheif's 8-bit output truncates samples above 8 bits (in >> shift), which put the six 10- and 12-bit stress files up to 10 levels off. The oracle now decodes those images at their coded depth and rounds each sample once. With that change they match libheif exactly at full size and within 1 when downscaled. A 10-bit stream with a negative slice QP (from a hand-edited x265 encode) also matches libheif exactly.

Not tested with real camera HDR HEICs or in the app. 4:2:2 HDR is still refused, and Apple gain-map photos still decode as their SDR base image.

Source fork PR: laurens-pilot#30

@laurenspriem

decode_bounded refused every 10-bit file, so HDR HEICs from cameras and
editing tools fell back to the full decode, which shows raw PQ or HLG
values as if they were sRGB. Bounded decoding now accepts 8-, 10- and
12-bit 4:2:0 HEVC, including the negative QPs those depths allow, and
checks that pixi matches the coded bit depth.

PQ and HLG colour, taken from the cicp tag of a valid RGB ICC profile,
the container nclx or, without container colour, the HEVC VUI, is
converted to display light with 203 cd/m2 as SDR reference white. HLG
goes through the BT.2100 OOTF for a 1000 cd/m2 display. The colour is
converted to BT.709 and the BT.2390 EETF is applied to the largest
component, using the clli or mdcv peak (1000 cd/m2 when absent) as the
source peak. Reference white lands at 229 of 255 for a 1000 cd/m2 peak
and highlights roll off to white. Malformed, repeated or zero light
levels are ignored, and grid tiles may carry different ones.
Contradictory HDR signalling is still refused, the incremental
experiment and its rolling decoder stay 8-bit SDR, and 10- and 12-bit
SDR images decode exactly like the RGB8 path followed by the ICC
conversion.

The regular RGB8 region converter now shares its setup and generic loop
with a native-depth RGB sample converter; regular output is unchanged on
a 454-file sweep.

The bounded oracle comparison now asks libheif for images above 8 bits
at their coded depth and rounds each sample once, because libheif's own
8-bit output truncates them and put the 10- and 12-bit stress files up
to 10 levels off.
@laurens-pilot

Copy link
Copy Markdown
Author

@codex review

…-hdr-support-0e0715

The 4:4:4 conversion for odd crops, rotations and mirrors assumed 8-bit
samples and RGB8 output. It now converts at the coded bit depth and tone
maps PQ and HLG bands, so 10- and 12-bit images that need it decode like
the regular decoders and HDR ones are tone mapped.
@laurenspriem
laurenspriem merged commit 90bcb93 into ente:main Nov 30, 2026
3 checks passed
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