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
1 2 3 4 5 |
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.
rav1e AVIF encoding library and cavif-rs tool
Let’s build rav1e in Ubuntu 20.04:
1 2 3 4 |
git clone https://github.com/xiph/rav1e cd 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.
x86_64
: Requiresnasm
.aarch64
- Requires
gas
- Alternative: Use
clang
assembler by settingCC=clang
NOTE:
SSE2
is always enabled onx86_64
,neon
is always enabled for aarch64, you may set the environment variableRAV1E_CPU_TARGET
torust
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:
1 2 |
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:
1 2 3 |
git clone https://github.com/kornelski/cavif-rs cd 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:
1 2 3 4 5 6 7 |
export PATH=$PATH:/home/jaufranc/.cargo/bin time cavif cnxsoft-logo.png cnxsoft-logo.avif: 30KB (29221B color, 0B alpha, 188B HEIF) real 0m4.753s user 0m17.945s sys 0m0.064s |
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:
1 2 3 |
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:
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:
1 2 3 4 5 6 |
git clone https://code.videolan.org/videolan/dav1d cd dav1d/ sudo apt install nasm meson ninja-build mkdir build && cd build meson .. ninja |
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:
1 2 3 4 |
git clone --recurse-submodules --recursive https://github.com/link-u/davif cd davif cmake . make |
The conversion from AVIF to PNG is much faster than the other way around, more precisely over 10 times faster.
1 2 3 4 5 6 7 8 9 |
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] real 0m0.392s user 0m0.431s sys 0m0.012s |
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.
Support CNX Software! Donate via cryptocurrencies, become a Patron on Patreon, or purchase goods on Amazon or Aliexpress
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.
rav1e encoder is hosted on Xiph.Org Foundation github account.
dav1d decoder is hosted on VideoLAN website.
It looks like ImageMagick also works with AVIF, but you need a really recent version (7.0.25). See tkaiser link below.
HEVC and AV1 are video codecs.
Most probably it was about HEIC vs. AVIF.
Thanks for these precisions. Oh yes, I probably mean heic and avif ?
> What about ImageMagick?
Good luck building from source 🙂
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