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.
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.
Any terminal can follow planar_zigzag, as in planar>zigzag>entropy. For pfor and lanes, the fused versions below go further.
| ctid | 0x72D70F |
|---|---|
| header | uint32 · row width in samples + uint32 · plane count, when above one |
| widths | 8 · 16 · 32 · 64-bit elements, read as integers |
| decode | inverse Zigzag, then planar reconstruction |
| next | a 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
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_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.
| ctid | 0x72D710 |
|---|---|
| header | uint64 · element count + uint8 · element width + uint32 · row width + uint32 · plane count |
| values | 8 · 16 · 32 · 64-bit numeric elements |
| geometry | width = columns planes = contiguous independent rasters |
| blocks | bands 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
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.
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.
| ctid | 0x72D712 |
|---|---|
| header | uint64 · element count + uint8 · element width + uint32 · row width + uint32 · plane count + uint32 · PivCo block size + per lane uint8 mode, uint16 weights, uint32 bitstream |
| values | 8 · 16 · 32 · 64-bit numeric elements |
| lanes | one per residual byte, PivCo Huffman, or byte PFOR where that is smaller |
| geometry | width = 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
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.
It has no fused PFOR or PivCo version and decodes only on the CPU. The usual recipe is med>zigzag>transpose>entropy.
| ctid | 0x72D711 |
|---|---|
| header | uint32 · row width in samples + uint32 · plane count, when above one |
| widths | 8 · 16 · 32 · 64-bit elements, read as integers |
| decode | inverse Zigzag folded into the MED reconstruction |
| next | an 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