← the effing blog

Effing 0.46.0: size isn't everything

We've added QOI as an image and frame format that encodes up to 20 times faster than PNG, and reworked how FFS handles images, so renders with large images take half the time or less.

Effing 0.46.0 is out, with two headlines. Canvases can now be encoded as QOI, a lossless image format that encodes many times faster than PNG, and QOI works everywhere a PNG or JPEG does, from image fns and annies through FFS to the players and previews in the browser. And FFS changes how it reads and scales images, which makes renders with large images two to three times faster and scaled preview renders look sharper.

Quite OK frames

An annie is a series of frames, and for most annies the time it takes to generate one is dominated by encoding those frames. PNG is a good format for the text, shapes and transparency that annies are usually made of, but its compression is slow: a 1080×1920 frame takes about 35 to 40 ms to encode.

QOI, short for “Quite OK Image Format”, makes the opposite trade. It’s lossless and keeps alpha like PNG, but it compresses with a handful of simple rules instead of deflate, so it encodes in a fraction of the time and comes out larger. @effing/canvas now depends on @effing/skia 1.0.10-effing.8, which can produce it:

yield canvas.encode("qoi");

imageResponse() in @effing/fn serves QOI bytes as image/qoi, annieFrames() recognizes QOI frames, FFS renders QOI image layers, backgrounds and annies, and effing render image saves a QOI image fn’s output as a .qoi file.

Whether QOI is the right choice depends on what’s in the frames. Here’s the same 150-frame 1080×1920 annie encoded three ways:

ContentFormatEncode per frameAnnie size
Text on transparencyPNG38.6 ms4.1 MB
QOI4.7 ms8.6 MB
Opaque graphicsPNG35.4 ms5.6 MB
JPEG q904.8 ms6.1 MB
QOI4.5 ms7.9 MB
Photo zoomPNG299 ms302 MB
JPEG q9014.1 ms51 MB
QOI14.0 ms363 MB

FFS rendered all of these in about the same time, so the difference is in generating the annie. That gives a simple rule of thumb, which the fundamentals guide and effing manual now spell out:

  • QOI for text, shapes and transparency. It encodes 8 to 20 times faster than PNG, at about 1.5 to 3 times PNG’s size. The emptier the frame, the bigger that ratio gets, since a mostly transparent frame still takes some space in QOI where PNG shrinks it to almost nothing.
  • JPEG for photos. PNG and QOI photo frames are 6 to 10 times larger than JPEG ones, and QOI encodes them no faster.
  • PNG when bytes matter more than encode time.

The frames of an annie may differ in size and channels, but they must all be in the same format, so pick one per annie.

The starter project follows its own advice: the text-typewriter annie in new projects now yields QOI frames. Generating its 180 frames went from 1.13 seconds to 0.14, and effing render annie text-typewriter from about 1.75 seconds to 0.76. The video it ends up in is identical.

QOI in the browser

Browsers can’t show QOI, so @effing/annie-player now comes with a decoder of its own. The player recognizes QOI frames by their bytes and plays them, alone or mixed with PNG and JPEG frames. It keeps each QOI frame compressed and decodes it when it’s drawn, since a decoded 1080p frame takes 8 MB. On a recent laptop that takes about 1 ms for flat graphics and 15 ms for photographic content, well within a frame at 30 fps. 4K photos (about 45 ms) or slower devices can exceed that budget, in which case playback slows down.

For still images there’s QoiAwareImg, from @effing/annie-player/react. It renders a plain <img>, so other images load exactly as before. Only when the browser fails to show the image does it fetch it again, and if it turns out to be QOI, swap in a PNG converted from it. @effing/effie-preview and the effing dev previews use it for covers, backgrounds and image layers, so QOI sources show up there without any work on your end. A cross-origin QOI image needs CORS for this to work.

That said, an effie’s cover should stay a PNG or JPEG. It’s meant to be shown in browsers, and QoiAwareImg can’t help with a <video> poster or with whatever other page ends up showing it.

Images in FFS, decoded once

FFS used to read every image layer and image background with FFmpeg’s -loop 1, which meant decoding the full image again for every frame of the output, and converting or scaling it again too. For large images that dominated the render. Each image is now decoded and converted once, and repeated inside the filter graph instead:

CompositionBeforeAfterFFmpeg peak memory
4K image layers13.7 s5.9 s8.6 GB → 4.2 GB
1080p image layers3.9 s1.6 s
Mixed8.7 s3.2 s

The rendered video is unchanged, frame for frame. A corrupt or truncated PNG also fails the render right away now, where FFmpeg used to keep re-reading it and never finish.

The same change fixed annies whose frames switch pixel format or size partway, such as a mix of PNGs with and without alpha. Those used to restart FFmpeg’s filter graph, so their later frames showed up early and the segment came out a frame or two too long. They now render with the right timing, and a frame of a different size is resized to the size of the annie’s first frame.

Scaling in FFmpeg

When you render at a scale other than 1, for a quick lower-resolution preview, FFS used to shrink every image layer, image background and annie frame with sharp before FFmpeg read it. That step is gone. Layers are now scaled inside the FFmpeg filter graph, and sharp is no longer a dependency of @effing/ffs. Scaled renders get better in a few ways:

  • Sharper JPEGs. Sharp re-encoded resized JPEGs at quality 80, which added visible block artifacts.
  • Rotated photos stay rotated. JPEGs with an EXIF orientation were rotated at scale 1 but not in scaled renders. They now are in both.
  • Faster scaled annies. Sharp resized annie frames one by one before FFmpeg could start. A 150-frame 1080p annie now renders at half scale in 0.5 seconds instead of 2.1 with PNG frames, and 1.0 instead of 1.7 with JPEG frames.

Layers are scaled with premultiplied alpha, so transparent edges don’t darken, and QOI works at every scale, which it couldn’t have with sharp.

There’s a cost, though. Sharp shrank images before FFmpeg saw them, and now FFmpeg decodes them at full size, so a scaled render uses close to as much memory as a full-size one. With eight 4000×3000 image layers, FFmpeg peaked at 1212 MB at half scale, against 371 MB before and 1333 MB at full scale. Compositions of only static images also take about 0.2 seconds longer.

Scaling with premultiplied alpha relies on alpha mode negotiation that FFmpeg added in 8.1, so that’s the new minimum version. The bundled FFmpeg is now 9.0.2.

Also in this release

  • Annie player fixes. While adding QOI, a few things in the React AnniePlayer got fixed. Changing src or autoPlay now loads the new annie, where the player used to keep showing the old one or get stuck on “Loading…”. A loaded player shows its first frame as a poster instead of a blank canvas. Changing height or fps no longer creates a new player that fetches the annie again; fps goes through the new AnniePlayerCore.setFps(). And an fps that isn’t a positive number is now rejected with a RangeError instead of freezing playback.
  • Covers get the right Content-Type. With upload.coverUrl, FFS uploaded every cover as image/png, whatever its format. It now sniffs the cover’s bytes and uploads JPEG and QOI covers as image/jpeg and image/qoi.

Upgrading

Bump your @effing/* packages to 0.46.0. Things to check:

  • If you point FFMPEG at your own binary, scaled renders need FFmpeg 8.1 or newer. FFmpeg 8.0 still renders at scale 1.
  • If you call FFmpegRunner.run() directly, it no longer takes an imageTransformer. Its signature is now run(sourceFetcher, referenceResolver?, urlTransformer?).
  • If you presign upload.coverUrl with a fixed Content-Type, it has to match the cover’s real format, or the upload now fails. Presign it without one to be safe.
  • Scaled renders now use close to as much memory as full-size ones. If you run many preview renders side by side, check that they still fit.
  • If you pass an fps of 0 or NaN to the annie player, it now throws (or, in React, shows an error) instead of silently freezing.

The full per-package changelogs are in the release PR.