Skip to content

v0.23.0

Choose a tag to compare

@osamu620 osamu620 released this 01 Nov 01:11
· 166 commits to main since this release

Feature release. The Part 1 block coder gains rate control (-rate R) and
quality layers (-rate R0,R1,...), both experimental. Portable x86-64 builds
(OPENHTJ2K_TARGET_CPU=generic, as packaged builds use) take SSE2 paths in
the HT block encoder and the forward DWT, and in the decoder's HT MagSgn
decoding, inverse DWT and color conversion: encoding takes about 30% fewer
cycles, and decoding 50% fewer at Qfactor=90 and 29% fewer lossless. Their
command-line decoder takes 1.43 times the time of a native build on one
thread, from 2.83. Fixes: a cod_params::number_of_layers above 1 made broken
codestreams through the library, and decoding at a reduced resolution an image
whose origin is not a multiple of 2^r reported a size one sample too large,
which made the command-line decoder read past the end of its rows. Without the
new options, output is byte-identical to 0.22.0 except for those fixes; the
API only gains functions.

Added

Encoder

  • Experimental rate control for the Part 1 block coder: -rate R (and
    openhtj2k_encoder::set_target_size()) now also works with Cmodes=0 and
    any combination of the Part 1 mode switches. Every coding pass is a
    truncation point: while a code-block is coded, the encoder records the
    squared error each pass removes (exactly, per sample) and where a cut after
    it would end. That is the coded bytes so far plus the few bytes that
    terminate the codeword there, which a copy of the arithmetic coder (or of
    the raw coder, with BYPASS) computes. The final cut picks every code-block's
    last pass at one slope threshold over the image, with the packet headers
    measured, and terminates the code-block there, so the codestream is valid
    for any JPEG 2000 decoder. OpenJPEG decodes the result to within 1 code
    value of our decoder in every mode combination tested, including with tiles,
    odd image sizes and 4 x 1024 code-blocks. Each code-block is coded only down
    to the bit-plane under the one the running threshold estimate of the HT rate
    control points to. That estimate uses the HT models, whose rates are 8-30%
    above Part 1's, so it gets 1.4 times the budget. On a 3840x2160 12-bit image
    at 1 bpp the encode takes 2.4 times as long as a fixed-step Part 1 encode of
    the same size on one thread and about 1.1 times with all threads. It gives
    35.23 dB, against 35.25 dB for that fixed step size and 34.32 dB for HT with
    -rate; on a 16-bit 4096x2160 frame of Elephants Dream at 2 bpp, 92.1 dB
    against 86.5 dB (fixed step) and 86.7 dB (HT). Coding every pass instead
    gives the same PSNR within 0.01 dB on the images measured, except 0.1 dB
    more on that frame at 2 bpp. Without a target, Part 1 output is unchanged.
  • Experimental quality layers for the Part 1 block coder: -rate R0,R1,...
    (and the new openhtj2k_encoder::set_target_sizes(), with
    cod_params::number_of_layers set to the number of targets) makes one
    quality layer per rate, in increasing order: layer l and the layers below
    it take at most the l-th lowest rate's bytes. The layers are placed from the
    lowest, each at one slope threshold over the coding passes above the layer
    below. A cut that a later layer continues cannot be terminated, because the
    later layer's bytes follow it, so the encoder also records for every pass
    the shortest prefix of the codeword that decodes it. A decoder reads past
    the end of a codeword segment as 1 bits, so a prefix decodes every symbol
    coded so far exactly when, followed by 1 bits, it stays below the upper end
    of the arithmetic coder's interval at the end of the pass; comparing the
    final codeword with the bytes of that upper end gives the shortest such
    prefix. Each code-block's last cut is terminated, as with one layer, and
    never over bytes that a lower layer keeps. Decoding any number of layers
    with OpenJPEG (-l) gives exactly the image of an encode terminated at that
    layer's cuts, in every mode-switch combination tested, with tiles,
    precincts, every progression order and SOP/EPH markers. Each layer of a
    five-layer codestream (0.125 to 2 bpp) is within 0.05 dB of a one-layer
    encode at its rate, and 0.02 to 0.05 dB above OpenJPEG's five-layer encode
    of kodim23 at the same rates. On a 3840x2160 12-bit image, five layers take
    1.04 times the time of one layer at the top rate on one thread and 1.3
    times with all threads (placing the layers runs on one thread). With HT, a
    list of rates is rejected.

Performance

Encoder

  • x86-64 builds without AVX2 (OPENHTJ2K_TARGET_CPU=generic, as packaged
    builds use) now use SSE2, which every x86-64 CPU has, in the HT block
    encoder and the forward DWT. The block encoder computes each quad's
    exponents, significance and EMB patterns and MagSgn codewords in the four
    lanes of a register (the bit length of a sample from the exponent of its
    float conversion, 2^m from building the float 2^m, as SSE2 has neither a
    leading-zero count nor per-lane shifts); the DWT is the 4-lane WASM SIMD
    one (planar horizontal 9/7 and 5/3 kernels, vertical lifting steps) in
    SSE2, and the code-block quantization is vectorized. The output is
    byte-identical to the scalar code, which other architectures still use and
    -DENABLE_SSE2=OFF (new option) selects. Ryzen 9 9950X, generic build,
    3840×2160 RGB, one thread: 32% fewer cycles lossless, 28% lossy and 27% at
    Qfactor=90 (8-bit), 24-26% (12-bit); the encoder takes 1.37 times the
    cycles of the AVX2 build lossless, from 2.0 times.

Decoder

  • x86-64 builds without AVX2 now use SSE2 in the decoder too: the inverse ICT
    with the float-to-integer finalize (round down emulated without SSE4.1, the
    clamp done in 16 bits when the output range fits), the planar horizontal
    inverse DWT (the 4-lane WASM SIMD kernels in SSE2), and the HT MagSgn
    decoding, which takes the four samples of a quad in the lanes of a register.
    The scalar HT MagSgn decoding, which other architectures and
    -DENABLE_SSE2=OFF use, reads a segment destuffed once and no longer
    branches on each sample's significance. The output is byte-identical to the
    scalar code. Ryzen 9 9950X, generic build, 3840×2160 8-bit RGB, one
    thread: 50% fewer cycles at Qfactor=90 and 29% lossless; the command-line
    decoder takes 1.53 times the time of the native build on one thread and
    1.67 times on eight.
  • In the same builds (and other builds without AVX2, NEON or WASM SIMD), the
    streaming decode converts each strip's rows (inverse ICT/RCT and finalize)
    on the thread pool instead of the calling thread, which the callback still
    runs on in row order: 9-10% less decode time with 8 or 16 threads. The
    command-line decoder interleaves 8-bit PPM output with SSE2 in portable
    x86-64 builds (its vector version needed SSE4.1): writing a 4K PPM takes
    about 10 ms instead of 15-18, as in the native build. With both, the
    generic command-line decoder takes 1.43 times the time of the native
    build on one thread and 1.44 times on eight.

Fixed

Encoder

  • A cod_params::number_of_layers above 1 made broken codestreams through
    the library (the command-line encoder always wrote one layer): every
    layer's packet repeated the same packet header and all of the precinct's
    data, and a Part 1 encode could fail with std::bad_alloc. The encoder now
    writes one packet per layer; without a target size, all the data goes in the
    first layer and the others are empty.

Decoder

  • Decoding at a reduced resolution (-reduce, the reduce_NL argument) an
    image whose origin is not a multiple of 2^r reported each component as
    ceil((x1 - x0) / 2^r) samples wide and high instead of ceil(x1 / 2^r) -
    ceil(x0 / 2^r) (ISO/IEC 15444-1, B.5): one column or row too many, by which
    the buffered decode laid out its output and the command-line decoder read
    past the end of every row (a heap overflow, or a crash where the reduced
    width is 0). This affects conformance codestream p1_01 and tiled images
    with an image offset. All decode APIs now report the size the samples have,
    which matches OpenJPEG. get_component_width()/get_component_height()
    had the same mistake for subsampled components.