Skip to content

Match libheif output: deblocking, SAO, scaling lists, clap crop - #15

Open
ymajoros wants to merge 3 commits into
openize-com:mainfrom
Valuya:upstream/libheif-alignment
Open

ymajoros wants to merge 3 commits into
openize-com:mainfrom
Valuya:upstream/libheif-alignment

Conversation

@ymajoros

@ymajoros ymajoros commented Oct 23, 2026 •

Copy link
Copy Markdown

Thanks for Openize.HEIC. We compared its output with libheif on real-world photos and found a few gaps. This PR closes them, so decoded images now match libheif up to YCbCr to RGB rounding.

Changes

Gap Visible effect Change
No deblocking filter (ITU-T H.265 8.7.2) block edges on every photo new LoopFilter, run once a picture is reconstructed
SAO parameters parsed but never applied (8.7.3) banding and ringing kept; SaoOffsetVal also stayed zero without a PPS range extension applied in the same pass, on the deblocked picture
Explicit scaling lists parsed then discarded drift up to 31 levels on files that code their own lists (e.g. some iOS photos): default matrices were used, the DC coefficient started 8 too low, matrices predicted from a reference were ignored scaling_list_data derives ScalingFactor (7.4.5) per SPS/PPS; a PPS list overrides the SPS one
Scaling factors held in a static field the first image's table is reused for every later image, and concurrent decoding shares it stored per parameter set
Slice header inference (7.4.7.1) deblocking flags, beta/tc offsets and loop-filter-across-slices not inherited from the PPS inferred
clap not applied e.g. gimp_with_clap.heic decoded as 1280x1240 instead of 1280x1239 crop (libheif's rounding) before irot/imir; width/height report the cropped size
180° rotation rows mapped with the width, wrong on non-square images fixed
Test paths TestsCore strips the first character of the class path (a Windows drive-letter workaround), so no test finds its samples on Linux/macOS path resolved from the code source URI

The byte-exact reference dumps (308 MB) had to be regenerated anyway, since the output changed. They are now SHA-256 digests of the same data: the same regression lock without the blobs. -Dopenize.updateReferences=true rewrites them. If you'd rather keep .bin files, the old comparison can come back with regenerated dumps.

Verification

LibheifComparisonTest decodes every sample with Openize.HEIC and with libheif's command line decoder (heif-dec / heif-convert), and requires the same dimensions and at most 2 levels of difference per channel. It is skipped when libheif isn't installed, so mvn test needs no native library. It passes with libheif 1.17.6, and a one-off comparison against libheif 1.23.6 gave:

Sample Before After
iphone_rotated_and_mirrored 33.9 dB, max 31 51.6 dB, max 1
nokia random_collection_1440x960 44.4 dB, max 17 49.1 dB, max 2
iphone_telephoto_with_scaling_list 50.8 dB, max 6 51.6 dB, max 1
gimp_with_clap size 1280x1240 (libheif 1280x1239) 51.8 dB, max 1
all 12 bundled samples blockiness above libheif's blockiness equal, max diff 1-2

The same holds for a dozen real iPhone uploads (not included). The remaining 1-2 levels come from YCbCr to RGB rounding. mvn test: 14/14.

The new code follows the spec section by section, with the section numbers in its comments. It's written from the specification, not ported from another decoder.

🤖 Generated with Claude Code

ymajoros and others added 3 commits October 23, 2026 11:29
…clap)

Decoded images differed visibly from libheif's: blocking artefacts on every
photo, and drift up to 31 levels on files with explicit scaling lists.

- Add the in-loop filters every HEVC encoder relies on: the deblocking filter
  (H.265 8.7.2) and sample adaptive offset (8.7.3), applied once a picture is
  reconstructed. SAO parameters were parsed but never applied, and the
  SaoOffsetVal they produced stayed zero without a PPS range extension.
- Apply explicit scaling lists. Their coefficients were parsed and discarded,
  so the default matrices were used, the DC coefficient started 8 too low and
  matrices predicted from a reference were ignored. Each SPS/PPS now keeps its
  own lists (a PPS list overrides the SPS one) instead of a static table
  shared by every image, which also made concurrent decoding unsafe.
- Infer slice_deblocking_filter_disabled_flag, the beta/tc offsets and
  slice_loop_filter_across_slices_enabled_flag from the PPS when the slice
  header omits them (7.4.7.1).
- Apply the clean aperture (clap) crop, with libheif's rounding, before
  rotation and mirroring; width and height report the cropped size.
- Fix 180-degree rotation, which mapped rows with the width.

Checked against libheif 1.23.6 on the 12 bundled samples and on real
iPhone uploads: same dimensions, max difference 1-2 levels per channel
(YCbCr to RGB rounding), blockiness equal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…on tool

- TestsCore stripped the first character of the class path location (a
  Windows drive-letter workaround), so no test could find its samples on
  Linux or macOS.
- The 308 MB of raw reference dumps become SHA-256 digests of the decoded
  output: the same byte-exact regression lock without the blobs. Regenerate
  them with -Dopenize.updateReferences=true after an intended change.
- tools/compare-with-libheif decodes a directory with Openize.HEIC and with
  libheif (pillow-heif) and reports PSNR, max difference and blockiness per
  file; it fails above MAX_DIFF (default 2).
- README lists the newly supported features.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LibheifComparisonTest decodes every sample with Openize.HEIC and with
libheif's command line decoder (heif-dec, or heif-convert before libheif
1.18), and requires the same dimensions and at most 2 levels of difference
per channel. When a file holds several top-level images, libheif writes
them all and the primary image has to match one of them.

It is skipped when neither decoder is installed, so a plain mvn test needs
no native library. -Dopenize.libheif.decoder points at another command and
-Dopenize.libheif.corpus adds a directory of files.

Passes on all 12 samples with libheif 1.17.6 (Ubuntu 24.04).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant