Post 83. Post 80’s GPU JPEG decoder
handled baseline files; this one extends the same three-pass
architecture to progressive (SOF2) JPEG — the multi-scan format
browsers render as progressively-refining previews. Spectral
selection, successive approximation, EOBRUN zero-runs and refinement
correction bits all run in the compute shader, mirrored
instruction-for-instruction from libjpeg’s jdphuff.c.
$ ./app --verify prog.jpg
coefs: 0/1179648 mismatch
rgb: max |diff| 0 mean 0.0000
Bit-exact vs libjpeg-turbo on a 180-file corpus: Pillow- and
cjpeg-encoded progressive files (all subsamplings, restart intervals
0–16, -optimize per-scan tables, grayscale), a baseline regression
set, real-world JPEGs — coefficient-for-coefficient and final
pixels.
Example: VulkanExamples/p83_progjpegdec
Progressive = N sequential scans over one coefficient grid
A baseline JPEG encodes every block in one sequential scan. A progressive file encodes the same blocks in up to ~10 scans, each covering only a slice of coefficient space:
- Spectral selection (
Ss..Se): which of the 64 zigzag coefficients this scan carries — typically DC alone first, then AC bands1..5,6..10, …56..63. - Successive approximation (
Ah/Al): a first scan (Ah=0) writes each coefficient’s top bits atvalue << Al; later refinement scans (Ah>0) append bitAland send correction bits for the already-nonzero coefficients.
So the entropy shader is one kernel with five modes — sequential, DC-first, DC-refine, AC-first, AC-refine — and the coefficient buffer simply persists GPU-side across scan dispatches. Refinement scans read-modify-write the same memory.
Non-interleaved scans (one component per scan — the typical progressive layout) count MCU = block, so restart intervals still define independent work items and the restart-interval parallelism from post 80 carries over unchanged.
EOBRUN: the state that isn’t in the bits
AC-first/refine scans encode zero runs as end-of-band runs: an
EOB n symbol means the next 2^n + bits(n) blocks end here. The
run counter is decoder state, not bitstream — it must survive across
MCUs, reset at each restart marker, and (for chunked dispatch) flow
through the generation-handoff SSBO alongside the bit-reader state
and DC predictors.
if (eobrun > 0) { eobrun--; continue; } // all-zero band
// EOBRUN symbol: run = (1 << rr) + rext(b, rr); value = ±1 << AlThe refine path additionally walks the band’s already-nonzero
coefficients consuming one correction bit each (and flips the sign
when a 0 bit catches a coefficient at exactly 1 << Al — libjpeg’s
jdphuff.c decode_mcu_AC_refine, ported verbatim).
Three bugs that took real debugging
1. cjpeg re-emits Huffman tables per scan. With -optimize
(the default), every SOS is preceded by its own DHT redefining the
tables it uses. Decoding all scans with the final table state
silently works for DC (table 0 is never redefined) and garbles every
AC scan. Fix: each Scan snapshots its DQT/DHT state at SOS-parse
time; unchanged tables share one 64K-entry fused-LUT slot so
re-uploads stay cheap.
2. Marker prefetch vs restart intervals. The bit reader greedily
buffers ≥25 bits — so near an interval’s end it prefetches the RST
marker itself, sets end, and the next few MCUs (whose real bits
were already buffered) got skipped → the last 2–5 blocks of every
interval came out zero. Fix: a pad counter records how many
buffered bits arrived after the marker; an MCU is skipped only when
end && cnt <= pad — nothing real left.
3. The 10-scan submit dies silently. Concatenating all scans'
tables/LUTs/chunk grids into shared buffers makes the whole decode a
single submit — but a ~3.5 s submit gets killed mid-flight by the
driver job timeout on some GPUs (AMD/RADV here). queueWaitIdle
returns cleanly; the tail dispatches just never execute, and the
kill point varies run to run. Fix: group (scan, generation)
dispatches into submits capped at 32768 MCU-work — typical files stay
single-submit, giant multi-scan files split.
for (auto& job : jobs) {
if (mcus + job.nMcu > kSubmitMcus && !submits.empty())
submits.push_back({}); // flush at job boundary
submits.back().push_back(job); mcus += job.nMcu;
}One more refactor gotcha: when the entropy stages split into fresh
command buffers per group, each one must re-bindDescriptorSets —
the recon submit briefly didn’t, and read zeroed component metadata
(non-deterministically, depending on pool memory). Every invocation
early-returned; nothing was written; no error anywhere.
Verification
--verify compares both stages against libjpeg itself:
jpeg_read_coefficients for the entropy output (with the
MCU-padded ↔ virt_barray layout mapping from post 80), and
jpeg_start_decompress for the final RGB. ./check.py generates the
corpus and requires 0/N mismatch + max |diff| 0 on every file —
including truncated/partial-scan files where coefficients must match
even though libjpeg’s missing-scan pixel handling differs.
Performance (Renoir iGPU vs libjpeg-turbo djpeg)
| File | GPU | CPU | Note |
|---|---|---|---|
| prog 3MP, restart=2 | 29 MP/s | ~43 | intervals parallelize per scan |
| prog gray 0.5MP | 39 MP/s | ~37 | faster than CPU |
| prog 4:2:0 0.5MP | 4.5 MP/s | ~24 | mostly-serial AC scans |
| prog 3MP, DRI=0 | 1.2 MP/s | ~43 | one serial stream per scan |
| baseline 4:2:0 0.5MP | 12 MP/s | ~48 | unchanged from post 80 |
Progressive files are worse for GPU decode than baseline in one
sense: where a baseline DRI=0 file has one serial stream, a
10-scan progressive has ten sequential serial streams — dispatch n
can’t start until n−1’s coefficients exist. Restart markers restore
the parallelism (each scan’s intervals are independent again), which
is why -restart progressive files still beat CPU. For DRI=0
progressive, speculative boundary resync — decode chunks at guessed
offsets, keep the chains that tile — is the known next step.