Encoding and decoding AVIF pictures with open-source software & libraries

One of the new features of Android 12 is support for the AVIF file format that provides much smaller files at the same level of quality. Jake Archibald has done a great job comparing AVIF format to JPEG, WebP, and other still picture formats, and the results are really impressive.

Besides Android 12, AVIF is already supported in Chrome browser, will be enabled by default in Firefox 86, but here’s we’ll look at some of the open-source programs and libraries that allow you to manipulate AVIF pictures.

With libavif library, AOMedia has published a reference implementation in C, together with avifenc and avifdec tools, that rely on various codecs, but it appears rav1e is recommended for encoding AVIF picture, while dav1d is the best choice for AVIF decoding.

libavif will not automatically build the codec, and they need to be enabled in CMakeLists.txt


as well as define the path for the selected codec.

rav1e AVIF encoding library and cavif-rs tool

Let’s build rav1e in Ubuntu 20.04:


I did so in an AMD Ryzen laptop but, rav1e is optimized for both Arm and x86:

  • asm – enabled by default. When enabled, assembly is built for the platforms supporting it.
    • x86_64: Requires nasm.
    • aarch64
      • Requires gas
      • Alternative: Use clang assembler by setting CC=clang

NOTE: SSE2 is always enabled on x86_64, neon is always enabled for aarch64, you may set the environment variable RAV1E_CPU_TARGET to rust to disable all the assembly-optimized routines at the runtime.

rav1e is based on rust, but there’s also a C library with header and pkg-config file for people wanting to integrate rav1e into their C projects. It can be generated as follows:


rav1e is made to encode raw videos, and afaik there’s no sample to encode an AVIF picture file, so we can either use libavif or cavif-rs to generate an AVIF file from a PNG or JPG file.

Here’s how cavif-rs can be built in Ubuntu 20.04:


Note that it will compile rav1e in the process, so no need to build rav1e beforehand. We can now run the program to test it out:


It took almost five seconds to convert a PNG file into an AVIF file on an AMD Ryzen 7 2700U processor, but the file is now much smaller:


In all fairness, this may not have been the optimal file for lossless PNG format. I could then open the files in Chrome and it’s hard to see differences:

avif vs png
AVIF (left) vs PNG (right)

You can also check the original PNG and AVIF files.

dav1d AVIF decoding library and davif tool

Most embedded systems will likely only use AVIF decoding/rendering, and for that, we can build dav1d C library:


Just like rav1e, dav1d is optimized for both Arm and x86 targets. At the time of working, the codec is optimized with assembly code for 64-bit Arm processors with NEON SIMD instructions,  and x86 chips with AVX2 and SSSE3+ SIMD instructions. Work is still on-going for ARMv7 (32-bit arm), and for less common architecture less PPC, SSE2 or AVX-512.

You can then use the code to integrate into your program, but if we want an example of code that will decode/convert the AVIF file using dav1d, davif tool can convert an AVIF file into PNG.

This is how I build it in Ubuntu 20.04:


The conversion from AVIF to PNG is much faster than the other way around, more precisely over 10 times faster.


Since everything is written in C and assembler it might easier to port to resources constrained embedded systems.

It should be noted that AVIF is not the only new picture format in town, as JPEG XL is also being worked on, but it may take a bit longer to be integrated into web browsers.  You’ll find a reference implementation of JPEG XL on Gitlab. The difference between AVIF and JPEG XL is discussed on encode.su forum and Cloudinary blog where it is mentioned AVIF offers better compression, but JPEG XL decoder and especially encoder should be faster, and support larger resolutions, while AVIF is limited to 3840×2160 which seems odd. There’s no royalty for JPEG XL and it remains compatible with JPEG.

Share this:

Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress

ROCK 5 ITX RK3588 mini-ITX motherboard
Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
6 Comments
oldest
newest
Schmurtz
Schmurtz
3 years ago

Interesting article.
The encoder has been done by videolan ?
What about ImageMagick ? This tool is multiplateforme and generally great to manage all the image encoding.

And what about the compare between hevc and and av1.
Hevc is in all ios devices so it concern a lot of people.

tkaiser
tkaiser
3 years ago

Most probably it was about HEIC vs. AVIF.

Schmurtz
Schmurtz
3 years ago

Thanks for these precisions. Oh yes, I probably mean heic and avif ?

tkaiser
tkaiser
3 years ago

> What about ImageMagick?

Good luck building from source 🙂

Jaroslav Svoboda
Jaroslav Svoboda
3 years ago

AVIF is not limited to 4K, just the Baseline profile is and even that can exceed it. This is to ensure compatibility with players which decode AV1 video. More here https://aomediacodec.github.io/av1-avif/#baseline-profile

Khadas VIM4 SBC