Skip to content

Add the JPEG XL encoder effort levels and make JPEG XL faster than libjxl - #3295

Open
JimBobSquarePants wants to merge 11 commits into
mainfrom
js/jxl-effort
Open

JimBobSquarePants wants to merge 11 commits into
mainfrom
js/jxl-effort

Conversation

@JimBobSquarePants

Copy link
Copy Markdown
Member

Summary

This PR adds the libjxl effort levels to the JPEG XL encoder. Encode and decode are faster than libjxl in each benchmark row. It also fixes animation deduplication and frame blending across the animated encoders.

JPEG XL

  • JxlEncoder.Effort selects the libjxl effort from 1 to 10. The default is 7, as in libjxl.
  • Each effort uses the libjxl tools of that effort:
    • entropy coding: copies, clustering, the hybrid integer search, and ANS or prefix codes;
    • modular: one global tree, palettes, RCT, squeeze and the weighted predictor;
    • VarDCT: the AC strategy search, the initial quant field, chroma from luma, dynamic EPF sharpness, and the butteraugli quant field search at effort 8 and up;
    • patches and dots, LF frames, streaming by DC group, and resampling at distance 10 and up.
  • Clear is the JPEG XL default for transparent pixels. Encoders can select their own default through a protected constructor.
  • The frame blend and disposal modes round-trip.
  • When an allocation fails, the encoder returns every buffer that it owns.

Animation and other codecs

  • Animation deduplication compares each frame with the frame that the decoder draws it on. The disposal mode selects that frame.
  • The Source, Mask and Over blend modes are added. GIF uses Mask. PNG, WebP and JPEG XL use the blend mode of the frame.
  • PNG: the tRNS gray key is scaled like the pixel samples. Transparent and deduplicated pixels use the metadata transparent color.
  • GIF decoder: RestoreToPrevious after RestoreToBackground now restores the cleared canvas.
  • WebP: the lossless LZ77 search, optimal parse, clustering and entropy estimates move to Compression as shared owners for WebP and JPEG XL.

Shared math

  • The shared companding, power and exponential helpers change. The BokehBlur and Resize reference images are updated for the new output.

Benchmarks

Ratio to libjxl. A value below 1.00 means ImageSharp is faster.

Encode Distance Progressive Effort dice issue648_palette0
0 False 1 0.29 0.26
0 False 3 0.70 0.68
0 False 5 0.63 0.62
0 True 3 0.61 0.61
1 False 1 0.34 0.33
1 False 3 0.57 0.56
1 False 5 0.73 0.41
1 False 7 0.86 0.61
1 True 5 0.45 0.45
Decode dice has_permutation issue648_palette0 progressive_ac
0.61 0.33 0.87 0.50

The BenchmarkDotNet tables are in the results blocks of EncodeJxl.cs and DecodeJxl.cs.

JxlEncoder.Effort selects the libjxl effort from 1 to 10, with the libjxl
default of 7. Each stream type uses the libjxl entropy choices of the effort:
run, greedy and optimal copies, the fast and best clustering, the hybrid
integer search, and ANS or prefix codes. The fast lossless mode runs only at
effort 1.

The WebP lossless LZ77 search, optimal parse, pair-merge clustering and
entropy estimates move to Compression as shared owners for WebP and JPEG XL.
- Write every non fast lossless modular frame with one global tree and one
  shared entropy code, with libjxl group sizes, palettes, RCT, squeeze and
  weighted predictor modes by effort.
- Add the tree learner, tokenizer, properties, VarDCT search tools and the
  Butteraugli comparator.
- Share one owner per operation: JxlSignedPacking, JxlSymmetricConvolution
  (Mirror, FilterRow, Separable5), JxlQuantization.DequantizeAc,
  JxlHybridUintSearch.AddPartialCounts, Numerics.Log2Ceiling and the
  TensorPrimitives_ Log2 and Exp2 operators.
- Raise a lossy distance below 0.05 to 0.05 once in JxlEncoderCore.
- XYB forward conversion: the absorbance matrix on linear sRGB in the
  libjxl order.
- Initial quant field: the 1x1 mask blur taps for the knight and corner
  positions.
- Chroma from luma: the outlier mask of the Newton solver also masks the
  terms at x + eps and x - eps.
- AC strategy search at effort 5 and up with the frame matrices.
- Restoration filter: write the all-default form only for Gaborish with
  two EPF iterations.
- Dynamic EPF sharpness at effort 6 and up. The encoder reconstructs the
  frame with the decoder DC dequantization, DC smoothing, AC
  dequantization, inverse transforms and restoration, then selects the
  sharpness of each block from the masked block errors.
- The libjxl reference encoder in the tests uses the effort of each case.
- VarDCT (effort 7 and up): find text-like patches, or dots from
  distance 3, before the initial quant field. Write the lossy XYB
  patch frame and subtract the decoded reference samples.
- Lossless modular (effort 5 and up, at most 16 bits): search the
  float color planes, write a lossless patch frame, and subtract the
  patches without a clamp.
- Follow the libjxl streaming decision: streamed frames have no patch
  search and no butteraugli search.
- Tests compare the frame list with libjxl for patch images.
- Count the coefficient order zeros through the libjxl group read cursor when
  blocks are sampled, with one sampler for all passes.
- Compute the EPF block errors from per-row terms and per-block running sums,
  with the libjxl summation order and no int overflow for wide frames.
- Return the linear planes of the butteraugli search to the pool after the
  search.
- Order equal patch candidates by position, and apply patches through one
  method.
- Return every owned buffer when an encoder allocation fails: patch candidates,
  tree learner samples, palette channels, RCT candidates, entropy streams and
  codes, copy matches, squeeze channels, AC passes and the butteraugli levels.
- Add a test that fails each encoder allocation at effort 9 in turn and checks
  that no buffer stays live.
- Use real-size images for the libjxl comparisons.
- Encode a large VarDCT frame by DC group, as libjxl does at efforts 1-7
  and 8-9 at low distances: each DC group gets its own patch for the
  invisible smoothing, the initial quant field and inverse Gaborish, and
  its own histogram set. lz77 runs only with one DC group.
- Resample the frame by two at distance 10 and up: a quarter of the
  distance plus 0.25, the sharper and iterative downsamplers, box-averaged
  extra channels and upsampling 2 in the frame header.
- Reconstruct the EPF sharpness candidates of a resampled frame with the
  patches, then the upsampling, as the decoder does.
- Make Clear the JPEG XL default. Lossless zeroes the color of transparent
  pixels. Lossy smooths it from the visible neighbors. Encoders can now
  select their own default through a protected constructor.
- Write EXIF and XMP Brotli boxes without a payload copy.
- Keep the earlier nearest cluster when histograms tie, and add a
  Vector512 path to the histogram divergence.
- Use one xorshift128+ owner for the encoder and the noise generator, and
  run the 8x8 DCT through the block operator pattern.
- Benchmark encoding at every effort from 1 through 10.
- Test streaming with more than 8 DC groups, distance 10 at efforts 7 and
  10, and the Preserve mode in the source-fidelity tests.
- Compare each frame with the frame that the decoder draws it on.
  The disposal mode of each frame selects that reference frame.
- Add the Source, Mask and Over blend modes. GIF uses Mask. PNG,
  WebP and JPEG XL use the blend mode of the frame.
- With Over, unblend partly transparent pixels. Source-over blending
  cannot lower alpha, so such a frame uses Source and writes Source.
- Separate the canvas color from the replacement color.
- Write RestoreToBackground frames at full size, because the formats
  clear only the frame area.
- Use Source for PNG color types without alpha.
- Fix the GIF decoder: RestoreToPrevious after RestoreToBackground
  now restores the cleared canvas.
- Add APNG, WebP and GIF round-trip tests.
… XL encoder

- Remove the Source fallback from animation deduplication. Each encoder writes the
  blend mode of the frame metadata.
- Remove the PNG color-type check on the Over blend.
- Scale the PNG tRNS gray key on read and write like the pixel samples, and mask it
  to the bit depth on read.
- Replace transparent and deduplicated PNG pixels with the metadata transparent
  color. ReplaceTransparentPixels takes an optional replacement color.
- Write the JPEG XL Over blend for pixel types without alpha.
- Round-trip the JPEG XL frame blend and disposal modes.
- Continue the JPEG XL encoder effort work: LF frames, modular XYB, squeeze,
  coefficient orders and the VarDCT encoder.
…math

- Make the JPEG XL encoder and decoder faster than libjxl on the EncodeJxl and DecodeJxl benchmark rows.
- Update the shared companding, power and exponential helpers, and their JPEG XL, ICC and working-space callers.
- Add the sRGB companding comparison benchmark.
- Promote the BokehBlur and Resize reference images for the changed companding output.
…indings

The LosslessFloat lossy encoder cases compare against float EXR reference files. The sRGB companding range checks and an animation unblend condition are simpler, with the same results.

This branch has not been deployed

No deployments
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