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
option(AVIF_CODEC_AOM "Use the AOM codec for encoding/decoding (see AVIF_CODEC_AOM_DECODE/AVIF_CODEC_AOM_ENCODE)" OFF)
option(AVIF_CODEC_DAV1D "Use the dav1d codec for decoding" ON)
option(AVIF_CODEC_LIBGAV1 "Use the libgav1 codec for decoding" OFF)
option(AVIF_CODEC_RAV1E "Use the rav1e codec for encoding" ON)
option(AVIF_CODEC_SVT "Use the SVT-AV1 codec for encoding" OFF)
as well as define the path for the selected codec.
Let’s build rav1e in Ubuntu 20.04:
git clone https://github.com/xiph/rav1e
sudo apt install cargo nasm
cargo build --release
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.
- Alternative: Use
clangassembler by setting
SSE2is always enabled on
neonis always enabled for aarch64, you may set the environment variable
rustto 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:
cargo install cargo-c
cargo cinstall --release
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:
git clone https://github.com/kornelski/cavif-rs
cargo install cavif
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:
time cavif cnxsoft-logo.png
cnxsoft-logo.avif: 30KB (29221B color, 0B alpha, 188B HEIF)
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:
ls cnxsoft-logo.* -l
-rw-rw-r-- 1 jaufranc jaufranc 29409 Feb 23 11:05 cnxsoft-logo.avif
-rw-r--r-- 1 jaufranc jaufranc 205216 Feb 23 11:03 cnxsoft-logo.png
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:
Most embedded systems will likely only use AVIF decoding/rendering, and for that, we can build dav1d C library:
git clone https://code.videolan.org/videolan/dav1d
sudo apt install nasm meson ninja-build
mkdir build && cd build
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:
git clone --recurse-submodules --recursive https://github.com/link-u/davif
The conversion from AVIF to PNG is much faster than the other way around, more precisely over 10 times faster.
time ./davif -i cnxsoft-logo.avif -o cnxsoft-logo.png
[2021/02/23 11:51:15 INFO ] davif
[2021/02/23 11:51:15 DEBUG] - dav1d ver: 0.8.0-34-g7424f8e
[2021/02/23 11:51:15 INFO ] Decoding: cnxsoft-logo.avif -> cnxsoft-logo.png
[2021/02/23 11:51:15 INFO ] Decoded: cnxsoft-logo.avif -> cnxsoft-logo.png in 93 [ms]
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.
Jean-Luc started CNX Software in 2010 as a part-time endeavor, before quitting his job as a software engineering manager, and starting to write daily news, and reviews full time later in 2011.