From 80a86ffa983949358abb6a26ef3602d72785d285 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Fri, 23 Jan 2026 18:14:03 +0100 Subject: [PATCH] fix(heif): Can not output AVIF when libheif has no HEVC support Initializing the encoder to heif_compression_HEVC by default throws an exception when libheif was built without HEVC support, which prevents it from being used entirely even if there is AVIF support. So delay that initialization until we are sure which encoder we want. Signed-off-by: Brecht Van Lommel --- src/heif.imageio/heifoutput.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/heif.imageio/heifoutput.cpp b/src/heif.imageio/heifoutput.cpp index 9998ab5a5f..309bc53ac5 100644 --- a/src/heif.imageio/heifoutput.cpp +++ b/src/heif.imageio/heifoutput.cpp @@ -49,7 +49,9 @@ class HeifOutput final : public ImageOutput { std::unique_ptr m_ctx; heif::ImageHandle m_ihandle; heif::Image m_himage; - heif::Encoder m_encoder { heif_compression_HEVC }; + // Undefined until we know the specific requested encoder, because an + // exception is thrown if libheif is built without support for it. + heif::Encoder m_encoder { heif_compression_undefined }; std::vector scratch; std::vector m_tilebuffer; int m_bitdepth = 0; @@ -140,12 +142,13 @@ HeifOutput::open(const std::string& name, const ImageSpec& newspec, m_himage.add_plane(heif_channel_interleaved, newspec.width, newspec.height, m_bitdepth); - m_encoder = heif::Encoder(heif_compression_HEVC); auto compqual = m_spec.decode_compression_metadata("", 75); auto extension = Filesystem::extension(m_filename); if (compqual.first == "avif" || (extension == ".avif" && compqual.first == "")) { m_encoder = heif::Encoder(heif_compression_AV1); + } else { + m_encoder = heif::Encoder(heif_compression_HEVC); } } catch (const heif::Error& err) { std::string e = err.get_message();