Skip to content

Speed up WebP and JPEG XL, and follow the libwebp rules - #3293

Merged
JimBobSquarePants merged 7 commits into
mainfrom
js/perf-webp
Dec 1, 2026
Merged

JimBobSquarePants merged 7 commits into
mainfrom
js/perf-webp

Conversation

@JimBobSquarePants

Copy link
Copy Markdown
Member

Summary

This PR makes the WebP codec faster than libwebp in each benchmark layout. The output follows the libwebp rules. It also speeds up JPEG XL and fixes the TIFF YCbCr subsampling default.

WebP

  • Lossy encode: SIMD quantization of block groups, the libwebp token loop, a faster bit writer, and inline work buffers.
  • Lossy encode rules from libwebp:
    • Equal segments merge.
    • Unused segments get the base quantizer.
    • The alpha filter uses the fast filter selection.
  • Lossy decode: the alpha rows decode per macroblock row, as in libwebp. The gradient unfilter uses a vector operator.
  • Lossless decode: fewer copies and a fill for short repeat copies.
  • Frame delays that the WebP, GIF, APNG and AVIF fields cannot hold now saturate. Before, the encoders threw an exception or wrapped the value.

Other codecs

  • JPEG XL: faster encode and decode.
  • TIFF: each directory without the tag uses the default YCbCrSubSampling value 2, 2 (TIFF 6.0, section 21).

Tests

  • Crafted fixtures replace the hand-written bitstream builders.
  • Image comparisons replace the pixel loops.
  • A new test checks that the lossy encoder output is byte-equal to libwebp for two fixtures.

Benchmarks

Ratio to libwebp (Magick). A value below 1.00 means ImageSharp is faster.

Benchmark Lossless LossyAlpha LossyOpaque
Decode 0.60 0.48 0.55
Encode 0.54 0.95 0.91

The BenchmarkDotNet tables are in the results blocks of EncodeWebp.cs and DecodeWebp.cs.

- JPEG XL lossless: port the libjxl effort 1 fast lossless mode (global
  per-channel tree, YCoCg, sampled histograms, bounded code lengths, zero
  runs, palette detection), with SIMD field operators.
- JPEG XL entropy: SIMD token count and 64-bit field writer for the
  general sample streams.
- WebP: codec performance work and simplification.
- Shared: vector helpers (variable shifts with SSE fallbacks, gather),
  modular forward YCoCg, hybrid integer 0/0/0 vector split, tests.
- Analysis histogram: Vp8Histogram.BinOperator with one CountBins
  traversal; zero bins counted with a vector compare and popcount.
- Mode trials: exact early-outs with Vp8ModeScore.GetScoreBound in the
  I4, I16 and UV searches. A mode whose header and distortion already
  reach the best score skips its spectral distortion and rate.
- I4 predictors DC, HE, HD and HU: packed rows stored by ref.
- Update shared-infrastructure to the latest main.

The encoded bytes are identical to the previous build.
An IFD without the YCbCrSubSampling tag now uses 2, 2, the TIFF 6.0 default, not 1, 1. The value is never null, so the null checks in the parser, the decoder and the YCbCr readers are removed.
The token loop gets the output buffer once for each call, not once for each flushed byte, and both bit writers are sealed. The scalar 4x4 vertical predictor needs no scratch. The alpha encoder selects its filter as libwebp does, records it in the ALPH header, and keeps compressed data of equal size.
- Decode the lossy alpha rows per macroblock row, as libwebp does.
- Merge equal segments and give unused segments the base quantizer, as libwebp does.
- Quantize groups of blocks with 16-bit lanes for 256-bit and 512-bit vectors.
- Saturate frame delays that the WebP, GIF, APNG and AVIF fields cannot hold.
- Add XML docs to the undocumented members of the touched files.
- Replace bitstream builders and pixel loops in the tests with fixtures and image comparers.
- Update the WebP benchmark results.
@JimBobSquarePants JimBobSquarePants added this to the v5.0.0 milestone Dec 1, 2026
@JimBobSquarePants
JimBobSquarePants merged commit 00e6395 into main Dec 1, 2026
12 checks passed
@JimBobSquarePants
JimBobSquarePants deleted the js/perf-webp branch December 1, 2026 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant