Documentation

Last updated: 2026-09-12

Decoding HEIC without C

Everlock's photo backend has to decode HEIC.

That is not an exotic edge case.

It is the format produced by a huge number of iPhones, which means a photo system that claims to support ordinary personal libraries eventually has to deal with it.

The first implementation used libheif through libheif-rs.

That looked like the safe choice.

It was mature, widely used, and the Rust wrapper could vendor the C library.

Then a Linux release shipped with HEIC support compiled in and no usable HEIC decoder behind it.

The build succeeded.

The binary started.

The feature was broken.

Vendoring libheif did not vendor the codec

The important detail is that libheif is not itself the HEVC decoder.

It understands the HEIF container.

To turn the encoded image data into pixels, it needs a codec implementation, commonly libde265.

The embedded-libheif feature vendored libheif.

It did not magically vendor every codec libheif might use.

That distinction mattered during a musl cross-build.

The build environment did not have a musl version of libde265 for CMake to discover.

So libheif configured itself without that decoder.

The configuration summary effectively said:

HEIC NO
AVIF NO

The build did not fail.

It produced a library with less capability.

A successful build was not proof of a working feature

This failure is interesting because most obvious checks pass.

Does the library exist?

Yes.

Does pkg-config find what you expect?

Potentially yes.

Are the libheif symbols present?

Yes.

Can the wrapper call into the library?

Yes.

Can libheif describe decoders in general?

Yes.

None of those questions proves that the release binary can decode the actual file format the product claims to support.

The only meaningful test is:

take a real HEIC file
decode it
verify pixels came out

That is much less abstract, and much more useful.

Capability needs an end-to-end test

The immediate fix was a canary binary.

It decoded bundled HEIC fixtures during the release process.

The purpose was intentionally narrow.

Not:

does libheif compile?

But:

can this exact release build decode this exact class of file?

That test would catch a library compiled without the needed codec even when all dependency and symbol checks looked healthy.

This is a useful pattern whenever a dependency has optional capabilities selected by its build environment.

The presence of the dependency is not the same as the presence of the feature you need.

Then the better fix was to remove the failure class

The canary was useful.

It was also evidence that the dependency chain had become more complex than the feature deserved.

Everlock eventually replaced the libheif path with heif-oxide, using rust_h265 for HEVC decoding.

That changed the build model completely.

Instead of:

Rust
  |
  v
libheif
  |
  v
external HEVC codec discovered by CMake

the release could use a pure-Rust path.

The codec implementation was no longer an optional external component that might silently disappear depending on what CMake found.

Either the decoder code was part of the build or the Rust build failed.

That is a much stronger failure mode.

Build failures are often better than capability failures

A missing compiler error sounds like a benefit until it produces a broken binary.

The original dependency stack had a dangerous property:

missing capability -> successful build -> runtime failure

The pure-Rust path moves the failure boundary earlier:

missing code -> build failure

For release engineering, that is a major improvement.

A red build is noisy.

A green build with a dead feature is expensive.

Replacing the decoder exposed hidden responsibilities

Removing libheif was not just swapping one function call for another.

Libheif had been doing more than pixel decoding.

Everlock also relied on it for embedded metadata.

Once the dependency disappeared, those responsibilities became visible.

One of them was XMP extraction.

No existing Rust crate in the dependency set provided the exact behavior needed, so Everlock had to read the relevant HEIF structure directly.

XMP is another item in the container

HEIF stores metadata through its own item structures.

For Everlock, extracting the XMP meant following the container relationships:

meta
  |
  v
iinf
  |
  v
iref (cdsc)
  |
  v
iloc
  |
  v
metadata byte range

The goal was not to implement all of HEIF metadata handling.

It was to locate the metadata item associated with the primary image and resolve the bytes that belong to it.

That is exactly the kind of replacement work dependency removal tends to reveal.

The wrapper call looked simple because someone else owned the complexity.

Once the wrapper disappears, the real boundary becomes visible.

Negative tests were not enough

Metadata testing had another subtle trap.

A file with no XMP should return no XMP.

If both the old and new implementations return None, that proves almost nothing.

They could both simply be failing to find metadata.

So Everlock tested both directions.

A fixture without user XMP returned no XMP from both implementations.

Then XMP was written into a fixture with exiftool.

Both paths returned the same 3082 bytes.

That second test matters much more.

A successful negative result can be a shared failure.

A positive fixture proves the parser actually found the intended data.

The next problem was chroma

After the decoder swap, most images worked.

A minority still failed.

The problematic files were edited and HDR exports using 4:4:4 10-bit chroma rather than the more common 4:2:0 layout of straightforward captures.

rust_h265 had assumptions about 4:2:0 threaded through its implementation.

Supporting 4:4:4 therefore required more than changing one enum.

ChromaArrayType had to flow through several parts of decoding:

prediction
transform
deblocking
SAO
QP geometry

This is the point where "remove the C dependency" stopped being a dependency-cleanup task and became codec work.

One missing mode produced tiny but real errors

One bug inside that work is worth keeping because it shows how easy image-decoder correctness can become deceptive.

predict_intra_chroma filtered reference samples for angular modes 2 through 34.

PLANAR was skipped.

For 4:2:0, that behavior was correct.

For 4:4:4, chroma follows luma's smoothing behavior, which means PLANAR also needs filtering.

The result of missing that case was not a dramatic corrupted image.

It was a consistent difference of roughly plus or minus one across chroma samples.

That is exactly the sort of bug visual inspection can miss.

The picture can look fine.

The decoder can still be wrong.

Reference decoding gave a much stronger oracle

For the 4:4:4 intra work, Everlock compared output against ffmpeg.

Not approximately.

Byte for byte.

The resulting comparison had:

0 differences
across 11,358,585 samples

That is a very different confidence level from:

the image looks correct

For codec code, especially unfamiliar codec code, a trusted reference implementation is enormously valuable.

The test does not need to prove the new decoder is universally correct.

It needs to make the specific supported path difficult to accidentally change.

Unsupported is better than subtly wrong

Inter-coded 4:4:4 remained a different problem.

The existing chroma motion-compensation path still assumed 4:2:0 geometry.

Rather than approximate behavior and produce images that might look mostly right, Everlock explicitly rejects that case.

That is an important product decision.

There is pressure in media code to "try anyway" because a partially decoded image feels better than an error.

But an incorrect image can be much worse than a clean refusal, especially if the output is then treated as authoritative.

So the supported boundary is explicit.

HEIC still images use the intra path Everlock needs.

The unsupported path fails rather than pretending.

The dependency tree became smaller

Once libheif left the release dependency graph, several build dependencies disappeared with it.

The cross-build no longer needed the same CMake, Perl, pkgconf, codec packages, or PKG_CONFIG_ALLOW_CROSS workarounds for this feature.

libheif-rs could remain as a development-only dependency for fixture generation and comparison.

The important distinction is:

needed to ship the product

versus:

useful to test the product

A library can be valuable in the second category without carrying its native build complexity into every release.

Pure Rust did not make the problem free

It is tempting to turn this into a simple "Rust good, C bad" story.

That would be wrong.

Replacing the native stack meant taking responsibility for more codec behavior.

The 4:4:4 work took days.

It was unfamiliar territory.

Correctness risk moved into code the project now had to understand and test.

Native libraries exist for good reasons.

Removing one is only a win if the new ownership boundary is acceptable.

In this case, it was.

The real benefit was a better failure boundary

The most useful result was not simply "zero C dependencies for HEIC."

It was that an entire class of release failure disappeared.

The old system could produce:

library present
build green
feature unavailable

because the decoder depended on what the build machine happened to provide.

The new system makes the capability part of the Rust dependency graph.

That is easier to reproduce.

Easier to cross-compile.

And much harder to silently disable.

Test the capability, not the dependency

The lesson I would keep from this work is not specific to HEIC.

Whenever a library is a container around optional plugins, codecs, drivers, or dynamically discovered capabilities, dependency checks are too shallow.

Ask the question the user will ask.

Not:

is libheif linked?

But:

can this release decode an iPhone HEIC?

Not:

does the metadata API return?

But:

does it return the exact XMP bytes from a positive fixture?

And when correctness becomes subtle:

compare against a trusted implementation

The first Everlock release failed because the build answered the wrong question.

The better design was not merely to add another check.

It was to make the build and the capability much harder to separate in the first place.

updates images heic rust dependencies