Follow-up to the baseline
and progressive
GPU JPEG decoders. Those posts parallelized entropy decode over JPEG
restart intervals — the only place the serial Huffman stream can
be re-entered safely. But restart markers are optional, and most
real-world files don’t have them: every camera JPEG I tested is
DRI=0, one serial bitstream per scan.
This post covers how DRI=0 files get the same parallel GPU decode
via a host survey pass that synthesizes virtual restart
intervals — plus three cheap wins that are nearly free once the
pipeline exists: scaled IDCT (1/2, 1/4, 1/8), MCU-aligned
region-of-interest crop, and EXIF orientation. Everything stays
bit-exact vs libjpeg-turbo on the same corpus (baseline +
progressive + all subsamplings + grayscale + real-world files).
Example: VulkanExamples/p83_progjpegdec
The problem: Huffman entropy coding is serial
JPEG’s entropy stream is a chain: each Huffman symbol’s bit length
depends on all symbols before it, and DC coefficients are
delta-coded against the previous block. Restart markers (RSTm)
periodically reset the bit reader to byte alignment and zero the DC
predictors — that’s why restart intervals decode independently and
the earlier posts could dispatch one workgroup per interval.
With DRI=0 there are no resets. A 1920×1080 photo’s luma scan is
~8100 MCUs in one uninterrupted serial run — decode it on one thread
(GPU or CPU), which is why the progressive decoder measured 1.2
MP/s on a large DRI=0 file versus 29 MP/s with restart
markers.
The fix: survey the bitstream on the host, then decode in parallel
The trick is that finding the restart boundaries is much cheaper than decoding at them. A CPU pass walks the bitstream symbol-for-symbol — Huffman table lookups only, no dequantization, no IDCT, no upsampling — and snapshots decoder state every 256 MCUs:
- bit position (byte offset + in-byte count + bit buffer contents)
- DC predictors (per component in the scan)
- EOBRUN (progressive zero-run length)
- end-of-band / padding flags
That’s 12 words per boundary. The snapshots upload as synthetic restart state; the shader gets a third chunk flag on top of the existing fresh start / continue from predecessor:
flag 0: first chunk of the scan — fresh state, seek to scan offset
flag 1: continuation — load state stored by the previous generation
flag 2: host-surveyed — load state preloaded into this interval's own slotEach virtual interval then decodes its 256 MCUs independently, in
parallel, producing output bit-identical to the serial stream —
the survey replicates the shader’s bit reader exactly (same
0xFF00-stuff handling, same byte-boundary padding rules), so the
boundaries it finds are the real ones.
Progressive wrinkle: deferred surveys
An AC-refinement scan’s correction bits depend on the coefficient values written by earlier scans — so its bitstream can’t be surveyed until those scans have actually decoded. Solution: surveys for DC-first and AC-first scans run up front on the host; surveys for AC-refine scans are deferred to dispatch time — the host waits for the previous submit’s coefficient buffer, reads it back, runs the survey against real coefficient state, uploads the snapshots, then submits that scan’s virtual intervals.
Numbers
Same AMD iGPU, ~2 megapixel test files:
| File | Before | After |
|---|---|---|
| Progressive DRI=0 (10 scans) | 1.2 MP/s | 22.2 MP/s |
| Baseline DRI=0 | (CPU fallback) | 395 MP/s |
| Progressive DRI=4 (restarts) | 29.2 MP/s | 29.2 MP/s |
The DRI=0 progressive file goes from slower-than-CPU to within
~2× of the restart-marker path; baseline DRI=0 is faster than with
restart markers because the survey’s fixed 256-MCU partition is
denser than a typical DRI=4-style restart grid.
Cheap win 1: scaled IDCT — --scale M/N
For thumbnails you don’t need every coefficient: libjpeg’s classic
jidctred.c decodes at 1/2, 1/4 or 1/8 by running a reduced
IDCT (4×4, 2×2 or 1×1 output per 8×8 block) over the low-frequency
coefficients only. Ported operation-for-operation:
// 1/8: DC-only — one dequantize + descale per block
out = descale(coef[0] * qt[0], 4 /* CONST_BITS-PASS1_BITS+3 */);
// 1/4, 1/2: the same two-pass fixed-point IDCT on a 2x2 / 4x4 grid
Two subtleties that cost real debugging time:
- The row-pass descale shifts in
jidctred.care 19 for the 4×4 path and 20 for 2×2 (CONST_BITS+PASS1_BITS+3+1 / +2) — off-by-one here shifts every pixel. libjpegdoublesDCT_scaled_sizeper component when the decoded size would fall below the component’s upsampled grid — a 4:2:0 chroma plane atscale 1/2still decodes at full (8×8) resolution. Match that or chroma misaligns.
All three scales verified bit-exact against djpeg -scale across
baseline, progressive, gray, and every subsampling.
Cheap win 2: MCU-aligned ROI crop — --crop WxH+X+Y
Restart (and virtual-survey) intervals map to MCU grids, so a crop
that doesn’t need most of the image can skip whole intervals: for
each interval’s MCU range compute its pixel bounds — with a margin
for chroma upsampling and the IDCT window — and emit nMcu=0 for
intervals that can’t contribute. Blocks outside the window get
skipped again in reconstruction.
One gotcha: a crop-skipped interval writes no coefficients, and a later AC-refine scan’s deferred survey reads coefficient state — so every scan touching a component that any deferred scan refines must decode fully. Baseline crop on the big test file: 28.5 → 42.2 MP/s.
Cheap win 3: EXIF orientation — --orient auto
APP1 → Exif\0\0 → TIFF IFD tag 0x0112, handled for both II
and MM byte orders. Orientation is a pure output-space coordinate
transform, so it folds into the upsample stage for free — each
output pixel maps back to source-plane coordinates:
case 6u: return ivec2(y, int(uH) - 1 - x); // 90° CW
case 8u: return ivec2(int(uW) - 1 - y, x); // 90° CCWOutput dims swap for orientations 5–8. Verified against Pillow’s
ImageOps.exif_transpose on all 8 values: max diff 0.
Verification
check.py generates the corpus (Pillow + cjpeg, plus real-world
files and clean rejects), then for every file runs --verify at
every scale and compares coefficients and RGB against libjpeg.
Crop/orientation cases compare against a host-side reference that
applies the same transform. Every number above is reproducible with
--bench N; build and flag list are in the example’s
README.
The same survey pass is ported to the pure-Rust wgpu/WGSL decoder in ImageCropper — including the deferred AC-refine survey — where it turns camera files from CPU-fallback into parallel GPU decode.