Three Things You Didn’t Know About AVIF
Tags: avif, images, webdev, webperf
Published: 2020-12-17 08:46:44

AVIF, the file format based on the AV1 video codec, is the latest addition to the next-gen image formats. Early [reports](https://jakearchibald.com/2020/avif-has-landed/) and [comparisons](https://www.ctrl.blog/entry/webp-avif-comparison.html) show good results compared to JPEG and WebP. However, even if browser support is good, AVIF is still on the [bleeding edge](https://twitter.com/jonarnes/status/1313093339210829824) in regards to encoding and decoding. Encoding, decoding, settings and parameters has been well discussed elsewhere. 

No doubt, AVIF images generate a smaller payload and are nice looking. In this post, we’ll take a closer look at issues to be aware or before you go all in on AVIF. 

## 1) WebP is Better for Thumbnails

One interesting observation is that for small dimension images, WebP will produce lighter payload images than AVIF.

It’s probably possible to explain why, and tune the encoder to address this case. However, that is not an option for most people. Most people would probably rely on an image optimizer like [squoosh.app](https://squoosh.app) or an Image CDN like [ImageEngine](https://imageengine.io/). The below comparison uses exactly these two alternatives for AVIF conversion. 

![ImageEngine vs Squoosh app AVIF stats](https://dev-to-uploads.s3.amazonaws.com/i/1411amgc8dww539cdllh.png)

We see that WebP will generally produce images with a higher file size than AVIF. On larger dimension images Image Engine performs significantly better than squoosh.app. 

Now, to the interesting observation. On images around 100px x 100px squoosh.app passes ImageEngine on effectiveness, but then also WEBP catches up and for a 80px x 80px image, WebPis actually the most effective image measured in file size. 

The test performs relatively consistently on different types of images. For this illustration, [this image](https://picsum.photos/seed/643/50) from Picsum is used.


<table>
  <tr>
   <td>Pixels
   </td>
   <td><p style="text-align: right">
Original JPEG (bytes)</p>

   </td>
   <td><p style="text-align: right">
Optimized WebP (bytes)</p>

   </td>
   <td><p style="text-align: right">
ImageEngine AVIF (bytes)</p>

   </td>
   <td><p style="text-align: right">
squoosh.app AVIF (bytes)</p>

   </td>
  </tr>
  <tr>
   <td>50
   </td>
   <td><p style="text-align: right">
1,475</p>

   </td>
   <td><p style="text-align: right">
598</p>

   </td>
   <td><p style="text-align: right">
888</p>

   </td>
   <td><p style="text-align: right">
687</p>

   </td>
  </tr>
  <tr>
   <td>80
   </td>
   <td><p style="text-align: right">
2,090</p>

   </td>
   <td><p style="text-align: right">
1,076</p>

   </td>
   <td><p style="text-align: right">
1,234</p>

   </td>
   <td><p style="text-align: right">
1,070</p>

   </td>
  </tr>
  <tr>
   <td>110
   </td>
   <td><p style="text-align: right">
3,022</p>

   </td>
   <td><p style="text-align: right">
1,716</p>

   </td>
   <td><p style="text-align: right">
1,592</p>

   </td>
   <td><p style="text-align: right">
1,580</p>

   </td>
  </tr>
  <tr>
   <td>150
   </td>
   <td><p style="text-align: right">
4,457</p>

   </td>
   <td><p style="text-align: right">
2,808</p>

   </td>
   <td><p style="text-align: right">
2,153</p>

   </td>
   <td><p style="text-align: right">
2,275</p>

   </td>
  </tr>
  <tr>
   <td>170
   </td>
   <td><p style="text-align: right">
5,300</p>

   </td>
   <td><p style="text-align: right">
3,224</p>

   </td>
   <td><p style="text-align: right">
2,450</p>

   </td>
   <td><p style="text-align: right">
2,670</p>

   </td>
  </tr>
  <tr>
   <td>230
   </td>
   <td><p style="text-align: right">
7,792</p>

   </td>
   <td><p style="text-align: right">
4,886</p>

   </td>
   <td><p style="text-align: right">
3,189</p>

   </td>
   <td><p style="text-align: right">
3,900</p>

   </td>
  </tr>
  <tr>
   <td>290
   </td>
   <td><p style="text-align: right">
10,895</p>

   </td>
   <td><p style="text-align: right">
6,774</p>

   </td>
   <td><p style="text-align: right">
4,056</p>

   </td>
   <td><p style="text-align: right">
5,130</p>

   </td>
  </tr>
</table>

## 2) AVIF Might Not Be the Best for Product Images with High Entropy

Typically, a product page consists of images of the product, and when a user’s mouse hovers over or clicks on the product image, it zooms in to offer a closer look at the details. 

It is worth noting that AVIF will in certain cases reduce the level of detail, or perceived sharpness, of the image when zoomed in. Especially on a typical product image where the background is blurred or has low entropy while foreground, and product, has more detail and possibly higher entropy. 

Below is a zoomed in portion of a bigger product image ([JPEG](https://jolly-goodall-37651d.netlify.app/webshop/images/an-engagment-ring-sits-in-a-blue-box.jpg), [AVIF](https://jolly-goodall-37651d.netlify.app/webshop/images/squoosh%20app%20an-engagment-ring-sits-in-a-blue-box.avif)) which clearly illustrates the difference between a regularly optimized JPEG versus an AVIF image optimized by squoosh.app

![Jpeg vs AVIF comparison](https://i0.wp.com/css-tricks.com/wp-content/uploads/2020/11/ae6bed0a8d3b.gif)

The AVIF is indeed much lighter than the JPEG, but in this case the trade off between visual quality and lower file size has gone too far. This effect will not be as perceptible for all types of images, and therefore will be difficult to proactively troubleshoot in an automated build process that relies on responsive images syntax for format selection. 

Moreover, unlike JPEG, AVIF does not support progressive rendering. For a typical product detail page, progressive rendering might provide thea killer feature to improve key metrics like Largest Contentful Paint and other Core Web Vitals metrics. Even if a JPEG takes a little bit longer time to download due to its larger file size compared to AVIF, chances are that it will start rendering sooner than an AVIF thanks to its progressive rendering mechanism. This case is well illustrated by [this video](https://jakearchibald.com/c/progressive-5f94067c.mp4) from Jake Achibald.

## 3) JPEG 2000 is Giving AVIF Tough Competition

The key selling point of AVIF is its extremely low file size relative to an acceptable visual image quality. Early blogs and reports have been focusing on this. However, JPEG2000 (or JP2) may in some cases be a better tool for the job. JPEG2000 is a relatively old file format and does not get the same level of attention as AVIF, even if the [Apple side of the universe already supports JPEG2000](https://caniuse.com/jpeg2000). 

To illustrate, let’s look at this [adorable puppy](https://picsum.photos/id/237/700/700). The AVIF file size optimized by squoosh.app is 27.9KB with default settings. Converting the image to JPEG2000, again using [ImageEngine](https://imageengine.io/blog/imageengine-introduces-jp2-jpeg2000-for-apple-devices/), the file size is 26.7 KB. Not much difference, but enough to illustrate the case. 

What about the visual quality? [DSSIM](https://github.com/kornelski/dssim) is a popular way to compare how visually similar an image is to the original image. The DSSIM metric compares the original image to a converted file, with a lower value indicating better quality. Losslessly converting the AVIF and JPEG2000 version to PNG, the DSSIM score is like this:


<table>
  <tr>
   <td>
   </td>
   <td>DSSIM (0 = equal to original)
   </td>
   <td>Bytes
   </td>
  </tr>
  <tr>
   <td><a href="https://jolly-goodall-37651d.netlify.app/webshop/images/237-700x700.jpg.jp2">JPEG2000</a>
   </td>
   <td>0.019
   </td>
   <td>26.7 KB
   </td>
  </tr>
  <tr>
   <td><a href="https://jolly-goodall-37651d.netlify.app/webshop/images/orinal237-700x700.avif">AVIF</a>
   </td>
   <td>0.012
   </td>
   <td>27.9 KB
   </td>
  </tr>
</table>


AVIF has slightly better DSSIM but hardly visible to the human eye.

![puppy](https://dev-to-uploads.s3.amazonaws.com/i/kfzkvx5796ze7kijglr8.png)


### Right Tool for the Job

The key takeaway from this article is that AVIF is hardly the “silver bullet”, or the one image format to rule them all. First of all, it is still very early in the development of both encoders and decoders. In addition, AVIF is yet another format to manage. Like [Jake Archibald also concludes in his article](https://jakearchibald.com/2020/avif-has-landed/), offering 3+ versions of each image on your webpage is a bit of a pain unless the entire workflow (resize, compress, convert, select, deliver) is all automated.

Also, like we’ve seen, just because a browser supports AVIF, it doesn't mean that it is the best choice for your users.

Using responsive images and adding AVIF to the list of image formats to pre-create is better than not considering AVIF at all. A potential challenge is that the browser will then pick AVIF if it’s supported regardless of whether AVIF is the right tool or not. 

However, using an [image CDN like ImageEngine](http://imageengine.io/), will to a greater extent be able to dynamically choose between supported formats and make a qualified guess whether WEBP, JPEG2000 or AVIF will give the best user experience. Using an Image CDN to automate the image optimization process will take into account browser compatibility, image payload size and visual quality.