Skip to content

Commit

Permalink
Enable jxl multithreaded compression for large enough images
Browse files Browse the repository at this point in the history
Summary: By default, jxl using only one core to compress images, but it's easy to give it a thread pool, which we should only do for large enough images.

Reviewed By: kiminoue7

Differential Revision: D52379634

fbshipit-source-id: a9d9c24ef72bd5b4a02df91ed3595c1904703f93
  • Loading branch information
Georges Berenger authored and facebook-github-bot committed Dec 23, 2023
1 parent 800033c commit 9ebdc29
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions vrs/utils/PixelFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ using std::vector;

// When additional compression options are needed, use this struct instead of overloading the API
struct CompressionOptions {
uint16_t maxCompressionThreads{0}; // max compression threads, or 0 to let encoder decide.

/// jxl specific options

/// jxlQualityIsButteraugliDistance: if false, quality is a percentage, 100% being lossless.
Expand Down
23 changes: 23 additions & 0 deletions vrs/utils/PixelFrameJxl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,29 @@ bool PixelFrame::jxlCompress(
return false;
}

JxlThreadParallelRunnerPtr runner_fixed;
uint32_t maxThreads = options.maxCompressionThreads;
if (maxThreads == 0) {
// default: only use multithreading if the image is large enough (very arbitrary cutoff limits)
uint32_t pixelCount = pixelSpec.getWidth() * pixelSpec.getHeight();
if (pixelCount >= 4000 * 4000) {
maxThreads = 16;
} else if (pixelCount >= 2000 * 2000) {
maxThreads = 8;
} else if (pixelCount >= 1024 * 768) {
maxThreads = 4;
} else {
maxThreads = 1;
}
}
if (maxThreads > 1) {
maxThreads = std::min<size_t>(JxlThreadParallelRunnerDefaultNumWorkerThreads(), maxThreads);
if (maxThreads > 1) {
runner_fixed = JxlThreadParallelRunnerMake(nullptr, maxThreads);
ENC_CHECK(JxlEncoderSetParallelRunner(enc, JxlThreadParallelRunner, runner_fixed.get()));
}
}

const uint32_t totalChannels = pixelSpec.getChannelCountPerPixel();
const uint32_t colorChannels = min<uint32_t>(totalChannels, 3);
const uint32_t extraChannels = totalChannels - colorChannels;
Expand Down

0 comments on commit 9ebdc29

Please sign in to comment.