Skip to content

Conversation

@hjanuschka
Copy link
Collaborator

@hjanuschka hjanuschka commented Dec 29, 2025

Add i16 sample type support for modular decoding, enabling 50% memory bandwidth reduction for 8-16 bit images.

Based on jxl-oxide's sealed trait pattern.

Changes

Infrastructure:

  • ModularSample trait with wrapping arithmetic and i64 conversion
  • ModularChannelGeneric<T> with ModularChannel alias for i32
  • PredictionDataGeneric<T> with PredictionData alias for i32
  • PredictionDataI64 for intermediate calculations

Decode pipeline:

  • make_pixel<T> generic over sample type
  • All decoders updated to use make_pixel::<i32>
  • ModularSample::from_i64: direct cast for i32, saturating for i16

All 481 tests pass.

Design Notes

Transforms stay i32: RCT, squeeze, and palette transforms use SIMD operations (I32SimdVec) for performance. Keeping them i32-only is intentional - the decode loop (many pixels) benefits from i16, while transforms (applied once per region) benefit from SIMD optimization.

To use i16 samples: Pass i16 as the type parameter to ModularChannelGeneric and make_pixel. For channels without transforms, this provides 50% memory bandwidth reduction. For channels with transforms, use i32.

Initial infrastructure for supporting i16 samples in modular decoding,
enabling 50% memory bandwidth reduction for 8-16 bit images.

This PR adds:
- ModularSample trait with wrapping arithmetic and i64 conversion
- Implementations for i16 and i32

TODO (full implementation requires ~2000 lines across 13 files):
- Make ModularChannel<T> generic over sample type
- Make PredictionData<T> generic
- Update ModularChannelDecoder trait
- Update decode/channel.rs implementations
- Update tree.rs and predict.rs prediction functions
- Update transform functions (rct, squeeze, palette)
- Add SIMD support for i16 vectors
- ModularChannelGeneric<T> with type alias ModularChannel = ModularChannelGeneric<i32>
- PredictionDataGeneric<T> with type alias PredictionData = PredictionDataGeneric<i32>
- PredictionDataI64 for intermediate prediction calculations
- All existing code continues to use i32 via type aliases
@github-actions
Copy link

github-actions bot commented Dec 29, 2025

Benchmark @ d4fac3e


====================================================================================================
MULTI-FILE BENCHMARK RESULTS (4 files, 5 revisions)
  https://github.com/zond/jxl-perfhistory
  CPU architecture: x86_64
  WARNING: System appears noisy: high system load (2.28). Results may be unreliable.
====================================================================================================

Statistics:
  Revisions:                        5
  Confidence:                    99.0%
  Max relative error:             3.0%

[ 1] 1f64f896 Merge d4fac3ec659023eb03e6bf5b505741b05c2e4566 into 50794...
     vs d4fac3ec Add i16 sample type support for modular decoding
----------------------------------------------------------------------------------------------------------------------------------------------------------------
     bike.jxl                   |                               ├───────│╂───────────┤                             |     16482105 px/s | 0.997 / prev 
     green_queen_modular_e3.jxl |                                   ├────│──┤                                      |      6180726 px/s | 1.001 / prev 
     green_queen_vardct_e3.jxl  |                                 ├──────│────────┤                                |     14395673 px/s | 1.000 / prev 
     sunset_logo.jxl            |                                 ├──────│───────┤                                 |      1526219 px/s | 1.001 / prev 
                                  Scale: 0.89 to 1.11 (1.0 = same speed, '┃' marks 1.0)

[ 2] d4fac3ec Add i16 sample type support for modular decoding
     vs b9b859eb Complete i16 sample support through decode pipeline
----------------------------------------------------------------------------------------------------------------------------------------------------------------
     bike.jxl                   |                                   ├───│╂┤                                        |     16523736 px/s | 0.987 / prev 
     green_queen_modular_e3.jxl |                                        ├─│┤                                      |      6173675 px/s | 1.015 / prev 
     green_queen_vardct_e3.jxl  |                                       ├│────┤                                    |     14400269 px/s | 1.000 / prev 
     sunset_logo.jxl            |    ├│┤                                 ┃                                         |      1525343 px/s | 0.688 / prev ▼ slower (0.69 / prev)
                                  Scale: 0.65 to 1.35 (1.0 = same speed, '┃' marks 1.0)

[ 3] b9b859eb Complete i16 sample support through decode pipeline
     vs 05756920 Make ModularChannel and PredictionData generic over sampl...
----------------------------------------------------------------------------------------------------------------------------------------------------------------
     bike.jxl                   |                                  ├─────╂│────────┤                               |     16736384 px/s | 1.002 / prev 
     green_queen_modular_e3.jxl |                                ├───────╂│─┤                                      |      6080318 px/s | 1.002 / prev 
     green_queen_vardct_e3.jxl  |                         ├──────│───────╂────┤                                    |     14403498 px/s | 0.979 / prev 
     sunset_logo.jxl            |                                ├────│─┤┃                                         |      2217568 px/s | 0.993 / prev ▼ slower (0.99 / prev)
                                  Scale: 0.89 to 1.11 (1.0 = same speed, '┃' marks 1.0)

[ 4] 05756920 Make ModularChannel and PredictionData generic over sampl...
     vs 20ccd761 [WIP] Add ModularSample trait for i16/i32 sample type abs...
----------------------------------------------------------------------------------------------------------------------------------------------------------------
     bike.jxl                   |                             ├──────│───╂───┤                                     |     16709290 px/s | 0.989 / prev 
     green_queen_modular_e3.jxl |                                       ├╂───│──────────┤                          |      6069007 px/s | 1.012 / prev 
     green_queen_vardct_e3.jxl  |                                        ┃├───────────│─────┤                      |     14711547 px/s | 1.036 / prev ▲ faster (1.04 / prev)
     sunset_logo.jxl            |                                       ├╂───│───────┤                             |      2233854 px/s | 1.012 / prev 
                                  Scale: 0.89 to 1.11 (1.0 = same speed, '┃' marks 1.0)

[ 5] 20ccd761 [WIP] Add ModularSample trait for i16/i32 sample type abs... (oldest, baseline for comparisons)

================================================================================================================================================================

@hjanuschka hjanuschka marked this pull request as ready for review December 29, 2025 07:35
- make_pixel<T> now generic over ModularSample type
- All decode functions use make_pixel::<i32> for backward compat
- ModularSample::from_i64 uses direct cast for i32, saturating for i16
- All 481 tests pass
@hjanuschka hjanuschka changed the title [WIP] Add i16 sample type support for modular decoding Add i16 sample type support for modular decoding Dec 29, 2025
- Add ModularChannelI16 type alias and data_i16 field to ModularBuffer
- Add ensure_i32() method for on-demand i16→i32 conversion
- Add i16 decode path (decode_modular_subbitstream_i16, decode_modular_channel_i16)
- Add i16 dispatch in read_stream() when no transforms and bit_depth <= 16
- Add ensure_i32 calls in transform apply before accessing i32 data
- 50% memory bandwidth reduction for 8-16 bit images without transforms
@veluca93
Copy link
Member

Let's do this later. We would want to implement 16-bit SIMD transforms too, and 16-bit buffers; the benefits of just using 16 bits for the entropy decoding buffers are minimal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants