Metadata-Version: 2.1
Name: cujpegxl
Author: Martino Pilia
Home-page: https://github.com/m-pilia/cujpegxl
License: BSD-3-Clause
Description-Content-Type: text/markdown
Summary: CUDA GPU-accelerated JPEG XL encoder
Requires-Python: >=3.12
Version: 0.0.1

# cujpegxl - GPU CUDA-accelerated JPEG XL encoder

![logo](https://raw.githubusercontent.com/m-pilia/cujpegxl/master/doc/logo_light.svg)

[![CI](https://github.com/m-pilia/cujpegxl/actions/workflows/ci.yaml/badge.svg)](https://github.com/m-pilia/cujpegxl/actions/workflows/ci.yaml)

This project is a GPU-accelerated CUDA implementation of a JPEG XL encoder, covering a GPU- and real-time-friendly subset of the JPEG XL algorithm.

## Goals

* Conformance. The encoded image must be readable by any standard-conforming decoder.
* Real-time performance with 4K resolution inputs on a constrained device.

## Speed

| Platform   | fps  |
|------------|------|
| GTX 1080Ti |  325 |
| Tegra T234 |  64  |

On a T234 SoC (15 W mode) encoding a 4K frame uses 16 ms of GPU time and 36 ms of CPU core time.

Maximum throughput can be achieved by pipelining multiple frames. Optimal pipeline depth depends on the target hardware, the results above use a depth of 3 frames on the 1080Ti and 2 frames on the T234.

## Quality

Quality is close to libjxl. Some algorithmic approximations trade off a small amount of quality and compression rate in exchange for speed.

![ssimulacra2](https://media.githubusercontent.com/media/m-pilia/cujpegxl/master/doc/2026-11-26_ssimulacra2.png)

The results above can be reproduced with the 100-image subset of `//eval_dataset` at 2160p resolution (`bazelisk run //eval_dataset:prepare_dataset -- --resolution 2160p --limit 100 ...`). See [Evaluation tools](#evaluation-tools) below for details.

## Input formats

The encoder supports 8-bit sRGB input in these formats:
- YUV420 (`NV12` or `NV21`)
- Packed YUV422 (`UYVY` or `YUYV`)
- Packed `RGB8`/`BGR8` (or `RGBA8`/`BGRA8`)

YUV can be BT.709 or BT.601, either in full or limited range.

## Usage

### C and C++

The project exposes a versioned C API/ABI in `cujpegxl/cujpegxl.h`. See also [examples/api_usage](https://github.com/m-pilia/cujpegxl/tree/master/examples/api_usage) for an API usage example in C++.

```c
#include "cujpegxl/cujpegxl.h"

cujpegxl_config config = {.width = 3840, .height = 2160, .distance = 1.0f};

size_t capacity;
cujpegxl_max_output_size(&config, &capacity);
cujpegxl_output outputs[2] = {
    {.data = malloc(capacity), .capacity = capacity},
    {.data = malloc(capacity), .capacity = capacity},
};

/* Up to 2 frames in flight on device 0. */
cujpegxl_encoder* encoder;
cujpegxl_encoder_create(&config, 0, 2, &encoder);

cujpegxl_output* encoded;
for (int frame = 0; frame < frame_count; ++frame) {
    if (frame >= 2) {
        /* Waits for the oldest frame and frees its output for reuse. */
        cujpegxl_encoder_receive(encoder, &encoded);
        fwrite(encoded->data, 1, encoded->size, file);
    }
    /* Device pointers to the frame's NV12 planes, e.g. from a decoder or camera. */
    cujpegxl_input input = {
        .format = CUJPEGXL_FORMAT_NV12,
        .planes = {d_luma[frame], d_chroma[frame]},
        .pitches = {luma_pitch, chroma_pitch},
        .stream = stream, /* the stream the planes were written on */
    };
    cujpegxl_encoder_submit(encoder, &input, &outputs[frame % 2]);
}
while (cujpegxl_encoder_receive(encoder, &encoded) == CUJPEGXL_OK) {
    fwrite(encoded->data, 1, encoded->size, file);
}

cujpegxl_encoder_destroy(encoder);
```

### Python

Python bindings are provided in [//python:cujpegxl](https://github.com/m-pilia/cujpegxl/blob/master/python/BUILD.bazel), and published on PyPI for Linux x86_64 and CPython 3.12 or newer:

```sh
pip install cujpegxl
```

The wheel needs an NVIDIA driver that supports the GPU, at least 525.60.13 on Linux (528.33 on Windows if using WSL) for the [supported architectures](#supported-architectures). Other GPU families run the PTX through the driver's JIT compiler, which needs at least 575.57.08 on Linux (576.57 on Windows).

A `cujpegxl.Encoder` is a pipelined encoder, and takes input buffers either from device memory through DLPack or `__cuda_array_interface__` (e.g. PyTorch, CuPy, JAX or Numba arrays) or from host memory through DLPack or the [buffer protocol](https://docs.python.org/3/c-api/buffer.html) (e.g. NumPy arrays or CPU PyTorch tensors).

```python
import cujpegxl

with cujpegxl.Encoder(3840, 2160, distance=1.0, pipeline_depth=2) as encoder:
    for rgb in frames:
        if encoder.in_flight == encoder.pipeline_depth:
            file.write(encoder.receive())
        encoder.submit(rgb, format=cujpegxl.Format.RGB8, stream=stream)  # the stream the buffer was written on
    while encoder.in_flight:
        file.write(encoder.receive())
```

The pipelined asynchronous approach above is suited for applications where performance matters. As a convenience for a single image, if performance is not crucial, `cujpegxl.encode` creates an encoder of the image's size and returns the output synchronously.

```python
import cujpegxl

jxl = cujpegxl.encode(rgb, distance=1.0, format=cujpegxl.Format.RGB8)
```

## Supported architectures

cujpegxl can be built for a broad range of GPUs, targeting both x86_64 and aarch64. However, performance is currently not fine-tuned for all individual architectures, with focus being primarily on Tegra, and architecture-specific optimizations for different GPU families will be progressively expanded.

The Python wheels include SASS for the following GPU architectures, and PTX that can be used by other GPU families not in the list.

| Architecture | Compute capability | Example GPUs |
|---|---|---|
| Pascal | 6.1 | GTX 10xx |
| Turing | 7.5 | RTX 20xx, GTX 16xx, T4 |
| Ampere | 8.0, 8.6, 8.7 | A100, RTX 30xx, A10, Jetson Orin |
| Ada | 8.9 | RTX 40xx, L4, L40 |
| Hopper | 9.0 | H100, H200 |
| Blackwell | 10.0, 10.3, 12.0, 12.1 | B200, B300, RTX 50xx, GB10 |

### Build system integration

See examples for integration in Bazel ([examples/bazel_consumer](https://github.com/m-pilia/cujpegxl/tree/master/examples/bazel_consumer)) and CMake ([examples/cmake_consumer](https://github.com/m-pilia/cujpegxl/tree/master/examples/cmake_consumer)) projects.

Source builds must set the target CUDA architecture(s) with the `--@rules_cuda//cuda:archs` flag; any build of CUDA code fails without it.

Example:

```sh
bazelisk build //... '--@rules_cuda//cuda:archs=sm_86;sm_89'
```

The flag can also be set locally for a project in a `.bazelrc.user` file at the repository root:

```
build --@rules_cuda//cuda:archs=sm_86;sm_89
```

Example toolchain integrations for hermetic cross-compilation targeting Tegra T234 on JetPack 6.2 (CUDA 12.6) and 7.2 (CUDA 13.2) are provided for illustrative purpose:

```sh
bazelisk build --config=jetson_jp62 //packaging:install_tree
bazelisk build --config=jetson_jp72 //packaging:install_tree
```

## Features

Quality and compression rate are lower compared to the reference libjxl implementation. This is expected, as cujpegxl only implements a subset of the JPEG XL algorithm, excluding some parts of it (in particular, some GPU-unfriendly steps are intentionally omitted for performance reasons).

Notable exclusions include:
- rANS+Huffman coding used only for AC coefficients; DC and AcMetadata use Huffman coding only.
- LZ77 pre-pass is omitted.
- No non-square, <8 and >32 transforms.
- No iterative Butteraugli rate control.

Some numerical approximations are made for performance reasons, trading a bit of quality for speed:
- Huffman codes limited to 15 bit.
- Quantization global scale computed from histogram only rather than exact median.
- Transform selection uses a simpler heuristic.
- Fuzzy erosion is local (clamped to a 32px tile) as opposed to global.
- Context clustering uses <=32 clusters (as opposed to libjxl's 128) and a simplified cost model.
- DCT coefficients stored as FP16.
- Uniform field quant calibration.
- Fixed-strength inverse Gaborish pre-sharpening.
- Simplified CfL estimation.

Some features are not implemented:
- Modular mode and lossless compression.
- Single-group frames (or, in general, inputs 256x256 or smaller).
- Multi-pass encoding.
- Reference frames.
- ICC colour profiles.

## Evaluation tools

A small development set of freely licensed images is bundled in [//data:frames](https://github.com/m-pilia/cujpegxl/tree/master/data). See [data/LICENSE.txt](https://github.com/m-pilia/cujpegxl/blob/master/data/LICENSE.txt) for credits and respective licenses.

The `//eval_dataset` package implements tooling to generate a reproducible evaluation dataset using high-quality and free-license images, while `//tools/quality_benchmark` implements comparative compression quality benchmarking, see [eval_dataset/README.md](https://github.com/m-pilia/cujpegxl/blob/master/eval_dataset/README.md) for information.

## Code checks

To run tests and various code checks:

```sh
# Including GPU tests
bazelisk test //...

# Excluding GPU tests
bazelisk test //... --test_tag_filters=-gpu

# Sanitizers
bazelisk test --config=asan //... --test_tag_filters=-gpu,-no-sanitizers
bazelisk test --config=ubsan //... --test_tag_filters=-gpu,-no-sanitizers
bazelisk test --config=tsan //... --test_tag_filters=-gpu,-no-sanitizers

# Code style
bazelisk build --config=clang-tidy //...
bazelisk run //tools/format:format.check

# Fix formatting
bazelisk run //tools/format
```

## Credits

The implementation is a derivative work of libjxl, see the License section below for details.

The source code of this project is almost entirely LLM-generated, using a mix of GLM-5.3, GLM-5.2, Qwen 3.8, GPT-5.6 Sol, and Claude Opus 4.8.

## Disclaimer

JPEG and JPEG XL are trademarks of their respective owners. cujpegxl is an independent project and is not affiliated with or endorsed by the JPEG committee.

## License

The cujpegxl software is made available under a [BSD-3-Clause license](https://raw.githubusercontent.com/m-pilia/cujpegxl/c369abf5f4436c51bab95635b6ac3ca4ad2a7244/LICENSE?token=GHSAT0AAAAAAD7IWYDCTWYTLNESHJQFQP5S2UFY2GA).

This software is a [derivative work](https://en.wikipedia.org/wiki/Derivative_work) of [libjxl](https://github.com/libjxl/libjxl), the reference JPEG XL implementation, which is made available by its authors [under the same license type](https://github.com/libjxl/libjxl/blob/bdde644b94c125a15e532b2572b96306371a7d4e/LICENSE).

For additional patent rights, see the [PATENTS file in the libjxl source codebase](https://raw.githubusercontent.com/libjxl/libjxl/bdde644b94c125a15e532b2572b96306371a7d4e/PATENTS).

