GeoZL

Fused

A fused codec runs the chain in one pass. Separate nodes write each stage's full output to memory before the next stage reads it. A fused codec keeps samples in registers, or in a buffer of one 256-value block that stays in cache, between stages, so it never writes a whole intermediate stream. It reads the input once and writes the output once. Both produce the same decoded data.

Fusion helps most when cheap steps such as planar, zigzag and pfor spend more time moving data than computing. Follow one 1024 × 1024 uint16 tile of 2 MiB through separate and fused planar>zigzag>pfor.

As separate nodes, planar reads the 2 MiB tile and writes 2 MiB of residuals, zigzag reads them and writes 2 MiB of unsigned values, and pfor reads those and writes the frame, 10 MiB of memory plus the frame. Fused into one codec, the tile is read once and the frame written once, 2 MiB plus the frame, with samples kept in registers and one small block buffer.
Separate nodes pass the whole tile through memory between every step. The fused codec reads it once.

GeoZL uses a fused codec when that chain has a fused implementation. The available fused codecs are listed below.

The examples compress one 256 by 256 uint16 tile, made the same way in each language. Python also exposes individual fused nodes for custom OpenZL graphs; R and Julia use recipes.

import numpy as np
import geozl

y, x = np.mgrid[0:256, 0:256]
tile = (1000 + 3 * x + 2 * y + (x * y) % 17).astype(np.uint16)
library(geozl)

tile <- outer(0:255, 0:255, function(x, y) 1000 + 3 * x + 2 * y + (x * y) %% 17)
using GeoZL

tile = [UInt16(1000 + 3x + 2y + (x * y) % 17) for x in 0:255, y in 0:255]

11planar_zigzag

planar_zigzag computes the planar residual and maps it to unsigned with Zigzag in the same pass, so a small negative residual stays a small number.

Zigzag maps signed planar residuals 0, minus 1, 1, minus 2, 2 to unsigned values 0, 1, 2, 3, 4
Zigzag sends 0, −1, 1, −2, 2 to 0, 1, 2, 3, 4

Any terminal can follow planar_zigzag, as in planar>zigzag>entropy. For pfor and lanes, the fused versions below go further.

ctid0x72D70F
headeruint32 · row width in samples
+ uint32 · plane count, when above one
widths8 · 16 · 32 · 64-bit elements, read as integers
decodeinverse Zigzag, then planar reconstruction
nexta packer or entropy stage, also folded into planar_zigzag_pfor
// encode
r = s - (W + N - NW);
z = (r << 1) ^ (r >> (bits - 1));   // zigzag, small signed to small unsigned

// decode
r = (z >> 1) ^ -(z & 1);
o = r + W + N - NW;                  // a prefix sum per row, as planar
# Any terminal but pfor or lanes runs planar_zigzag on its own.
g = geozl.graph(tile, "planar>zigzag>entropy")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
assert np.array_equal(back, tile)

node = geozl.lossless.PlanarZigzag(tile.shape[1], planes=1)
# Any terminal but pfor or lanes runs planar_zigzag on its own.
g <- geozl_graph(tile, "planar>zigzag>entropy", dtype = "uint16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "uint16", dim = dim(tile))
stopifnot(all(back == tile))
# Any terminal but pfor or lanes runs planar_zigzag on its own.
g = GeoZL.graph(tile, "planar>zigzag>entropy")
frame = GeoZL.compress(tile, g)
back = GeoZL.decompress(UInt16, frame, size(tile))
@assert back == tile

Source in core/src/planar_zigzag

12planar_zigzag_pfor

planar_zigzag_pfor predicts, maps and packs 256 values at a time, so the Zigzag stream of the tile never exists. It works in bands of 16 rows cut into strips of 1024 columns, so a GPU can decode every strip of every band on its own.

Planar, Zigzag and PFOR fused into one codec
planar, Zigzag and PFOR in one node, one 256-value block at a time

planar_zigzag_pfor implements planar>zigzag>pfor for small residuals that do not repeat. How PFOR works covers the blocks.

Each plane is divided into independent bands and strips, with prediction restarting at their boundaries. Block headers precede their bodies, so a decoder can locate blocks without scanning the payload. The table below gives the main dimensions. The specification defines the exact band decomposition and payload order.

ctid0x72D710
headeruint64 · element count
+ uint8 · element width
+ uint32 · row width
+ uint32 · plane count
values8 · 16 · 32 · 64-bit numeric elements
geometrywidth = columns
planes = contiguous independent rasters
blocksbands of 16 rows, then 8, 4, 2, 1
strips of 1024 columns
256 values per block, headers first
// decode, one block of h rows by 256 / h columns at a time
pfor_unpack(block, z);               // into a 2 KiB scratch buffer
for (i = 0; i < h; i++)
  for (j = 0; j < 256 / h; j++) {
    v = z[8 * (i + h * (j / 8)) + j % 8];
    r = (v >> 1) ^ -(v & 1);
    o = r + W + N - NW;              // N, NW are 0 on a band's first row,
                                     // W, NW on a strip's first column
  }
# planar>zigzag>pfor runs as this one codec.
g = geozl.graph(tile, "planar>zigzag>pfor")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
assert np.array_equal(back, tile)

node = geozl.lossless.PlanarZigzagPfor(tile.shape[1], planes=1)
# planar>zigzag>pfor runs as this one codec.
g <- geozl_graph(tile, "planar>zigzag>pfor", dtype = "uint16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "uint16", dim = dim(tile))
stopifnot(all(back == tile))
# planar>zigzag>pfor runs as this one codec.
g = GeoZL.graph(tile, "planar>zigzag>pfor")
frame = GeoZL.compress(tile, g)
back = GeoZL.decompress(UInt16, frame, size(tile))
@assert back == tile

Source in core/src/planar_zigzag_pfor

13planar_zigzag_lanes

planar_zigzag_lanes runs planar and Zigzag, then codes each residual byte lane on its own with PivCo Huffman, or with byte PFOR when that is smaller.

Planar and Zigzag followed by PivCo or PFOR byte lanes
planar and Zigzag, then one PivCo or PFOR coder per residual byte lane

Recipes spell it planar>zigzag>lanes for every sample width. It tends to beat planar_zigzag_pfor when residual bytes are skewed rather than merely small. How PivCo works explains the format.

ctid0x72D712
headeruint64 · element count
+ uint8 · element width
+ uint32 · row width
+ uint32 · plane count
+ uint32 · PivCo block size
+ per lane uint8 mode, uint16 weights, uint32 bitstream
values8 · 16 · 32 · 64-bit numeric elements
lanesone per residual byte, PivCo Huffman,
or byte PFOR where that is smaller
geometrywidth = columns
planes = contiguous independent rasters
// decode, one lane per byte of the sample
for (k = 0; k < sizeof(T); k++) decode_lane(k, lane[k]);   // PivCo or PFOR
for (i = 0; i < n; i++) {
  z = 0;
  for (k = 0; k < sizeof(T); k++) z |= (T)lane[k][i] << (8 * k);
  r = (z >> 1) ^ -(z & 1);
  o = r + W + N - NW;
}
# planar>zigzag>lanes runs as this one codec.
g = geozl.graph(tile, "planar>zigzag>lanes")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
assert np.array_equal(back, tile)
# planar>zigzag>lanes runs as this one codec.
g <- geozl_graph(tile, "planar>zigzag>lanes", dtype = "uint16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "uint16", dim = dim(tile))
stopifnot(all(back == tile))
# planar>zigzag>lanes runs as this one codec.
g = GeoZL.graph(tile, "planar>zigzag>lanes")
frame = GeoZL.compress(tile, g)
back = GeoZL.decompress(UInt16, frame, size(tile))
@assert back == tile

Source in core/src/planar_zigzag_lanes

14med_zigzag

med_zigzag writes the MED residual mapped by Zigzag in one pass, identical to med followed by Zigzag. Every med>zigzag recipe uses it.

MED clamps the prediction to the near side of an edge, then Zigzag maps the signed residuals 0, minus 1, 1, minus 2, 2, minus 3, 3 to unsigned values 0 through 6
the MED residual, mapped to unsigned in the same pass

It has no fused PFOR or PivCo version and decodes only on the CPU. The usual recipe is med>zigzag>transpose>entropy.

ctid0x72D711
headeruint32 · row width in samples
+ uint32 · plane count, when above one
widths8 · 16 · 32 · 64-bit elements, read as integers
decodeinverse Zigzag folded into the MED reconstruction
nextan entropy stage, usually after a byte transpose
// encode
r = s - med(W, N, NW);
z = (r << 1) ^ (r >> (bits - 1));

// decode, one sample at a time along each row
r = (z >> 1) ^ -(z & 1);
o = r + med(W, N, NW);
# med>zigzag runs as med_zigzag, whatever terminal follows.
g = geozl.graph(tile, "med>zigzag>entropy")
frame = geozl.compress(tile, graph=g)
back = geozl.decompress(frame).view(tile.dtype).reshape(tile.shape)
assert np.array_equal(back, tile)

node = geozl.lossless.MedZigzag(tile.shape[1], planes=1)
# med>zigzag runs as med_zigzag, whatever terminal follows.
g <- geozl_graph(tile, "med>zigzag>entropy", dtype = "uint16")
frame <- geozl_compress(tile, g)
back <- geozl_decompress(frame, "uint16", dim = dim(tile))
stopifnot(all(back == tile))
# med>zigzag runs as med_zigzag, whatever terminal follows.
g = GeoZL.graph(tile, "med>zigzag>entropy")
frame = GeoZL.compress(tile, g)
back = GeoZL.decompress(UInt16, frame, size(tile))
@assert back == tile

Source in core/src/med_zigzag