Skip to content

Commit

Permalink
Enabled AVIF encoding with libheif (#2811) (#2812)
Browse files Browse the repository at this point in the history
Requires libheif built with libaom or dav1d/rav1e
  • Loading branch information
hjmallon committed Jan 5, 2021
1 parent 5dda98c commit 9eeac26
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ NEW or CHANGED MINIMUM dependencies since the last major release are **bold**.
stability and thread safety)
* If you want support for HEIF/HEIC or AVIF images:
* libheif >= 1.3 (1.7 required for AVIF support, tested through 1.10)
* libheif must be built with an AV1 encoder/decoder for AVIF
* If you want support for DDS files:
* libsquish >= 1.13 (tested through 1.15)
* But... if not found on the system, an embedded version will be used.
Expand Down
6 changes: 3 additions & 3 deletions src/doc/builtinplugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,9 @@ control aspects of the writing itself:
- HEIF header data or explanation
* - ``Compression``
- string
- If supplied, must be ``"heic"``, but may optionally have a quality
value appended, like ``"heic:90"``. Quality can be 1-100, with 100
meaning lossless. The default is 75.
- If supplied, can be ``"heic"`` or ``"avif"``, but may optionally have a
quality value appended, like ``"heic:90"``. Quality can be 1-100, with
100 meaning lossless. The default is 75.



Expand Down
14 changes: 11 additions & 3 deletions src/heif.imageio/heifoutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ heif_output_imageio_create()
}

OIIO_EXPORT const char* heif_output_extensions[] = { "heif", "heic", "heics",
nullptr };
"avif", nullptr };

OIIO_PLUGIN_EXPORTS_END

Expand Down Expand Up @@ -130,7 +130,15 @@ HeifOutput::open(const std::string& name, const ImageSpec& newspec,
chromas[m_spec.nchannels]);
m_himage.add_plane(heif_channel_interleaved, newspec.width,
newspec.height, 8 * m_spec.nchannels /*bit depth*/);
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();
Expand Down Expand Up @@ -196,7 +204,7 @@ HeifOutput::close()
std::vector<char> exifblob;
try {
auto compqual = m_spec.decode_compression_metadata("", 75);
if (compqual.first == "heic") {
if (compqual.first == "heic" || compqual.first == "avif") {
if (compqual.second >= 100)
m_encoder.set_lossless(true);
else {
Expand Down

0 comments on commit 9eeac26

Please sign in to comment.