AOMediaCodec / libavif

libavif - Library for encoding and decoding .avif files
Other
1.44k stars 188 forks source link

Lossless AVIF animation much larger than APNG or WebP #1244

Open rkjnsn opened 1 year ago

rkjnsn commented 1 year ago

I'm not sure this is the right place for this, so please point me in the right direction if it is not.

I was playing with one of the animated stickers from the Starbucks for Life game to see how various modern web animated image formats stacked up, and was surprised to find that AVIF, the newest of the three, generated by far the largest file size.

Here's the original APNG of the sticker, at 3.4MiB: Starbucks-For-Life-Vickers-the-Cat

Here's a lossless WebP animation of the same sticker. As expected, it is smaller (only 2.1MiB) due to WebP's more efficient encoding. (Encoded using img2webp -o Starbucks-For-Life-Vickers-the-Cat.webp -min_size -loop 0 -d 40 apngframe*.png) Starbucks-For-Life-Vickers-the-Cat

Finally, here's an AVIF animation of the same sticker. As the newest format of the group, I would have expected the resulting image file to be smaller still due to further efficiency gains. Instead, the resulting file clocks in at a whopping 7.0MiB, over twice as large as the source APNG. (Encoded using avifenc --fps 25 --lossless --speed 0 apngframe*.png Starbucks-For-Life-Vickers-the-Cat.avif) Starbucks-For-Life-Vickers-the-Cat

rkjnsn commented 1 year ago

Hmm… it seems github can't mirror the AVIF image, so here's a direct link: https://www.rkjnsn.net/vickers_samples/Starbucks-For-Life-Vickers-the-Cat.avif

vrabaud commented 1 year ago

AVIF lossless is efficient for photo-like images. Anything else that is artificial (like your animation) will indeed usually perform worse.

rkjnsn commented 1 year ago

Thanks for the reply! Is this a matter of encoder tuning, or a fundamental limitation of the underlying AV1 codec?

joedrago commented 1 year ago

AV1 likes to operate on color channels where the luma and chroma are decorrelated (like YUV), but converting RGB to YUV without changing the bits per channel would introduce (a tiny bit of) loss, so to achieve true lossless currently, we instead pack RGB pixels into the channels expected to be YUV-ish by AV1 codecs, which leads to a real loss of efficiency.

If you are willing to be near lossless (very high quality but not exact), you should see significantly higher efficiency with a (likely) imperceptible loss.

rkjnsn commented 1 year ago

I see, and WebP presumably perfoms better because it has a distinct, non-VP8-based codec for lossless mode? Out of curiosity, I tried converting the animation to YUV and then encoding the result (still using the lossless setting), getting a file size of 5.3MiB, still over twice that of the WebP.

Sounds like that's still expected, though, at least with the current, photo-like-image-tuned encoder.

jzern commented 1 year ago

I see, and WebP presumably perfoms better because it has a distinct, non-VP8-based codec for lossless mode?

That's correct. VP8 didn't offer any lossless mode. For the VP9 and AV1 bitstreams the codec is limited to 4x4 blocks in lossless mode, so with graphical content it's expected to perform much worse than WebP lossless. It is an area that hasn't been focused on, though, so some improvements could almost certainly be made to bring things a little closer.

sturkmen72 commented 3 weeks ago

i used https://ezgif.com/avif-maker to convert your APNG to AVIF the result AVIF image is just 135k image

vrabaud commented 2 weeks ago

@sturkmen72 , this website does not say the parameters it uses: it most likely compresses the images lossily (you do see some compression artifacts around the cat tail), hence the smaller size.

sturkmen72 commented 2 weeks ago

the original image have similar artifacts around the tail. it is obvious they used lossy compression. but in my opinion the quality is great in such a small file size.