Skip to content

fix(j2k-jpeg): preserve plans for damaged region decodes - #378

Merged
jcwal1516 merged 4 commits into
mainfrom
review/jpeg-damaged-regions
Dec 2, 2026
Merged

jcwal1516 merged 4 commits into
mainfrom
review/jpeg-damaged-regions

Conversation

@jcwal1516

@jcwal1516 jcwal1516 commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

What and why

Fast-packet planning used to reject an entire JPEG as soon as entropy data
outside the requested region was malformed. Preserve the plan and its typed
packet error instead. Metal can then use its hybrid route for the exact MCU
rows a CPU region decode reads, while CUDA whole-image decode still returns
the stored packet error.

This makes explicit Metal full, scaled, and region requests succeed or fail
at the same point as CPU decode without changing the valid fast-packet route.

Impact

  • Changes decoded, encoded, or transcoded output bytes
  • Changes a public API or behavior callers can see (if breaking, add ! to the title)
  • Changes how untrusted input is parsed, or how much memory or time it can use
  • Changes performance (command, hardware, and numbers under Testing)
  • Adds a test image or fixture (small, license allows committing it, no patient
    data, source noted in a README and NOTICES.md)

Explicit Metal region decodes can now finish when damaged entropy lies after
the requested rows. Requests that reach the damage still return the CPU
decoder's error. CUDA remains a strict whole-image packet route.

Implementation API impact: the rustdoc-hidden JpegFastPacketState enum is
exhaustive. Downstream code that deliberately matches this unsupported helper
must add an arm for MalformedEntropy; supported API items are unchanged.

Testing

  • cargo test --release --locked -p j2k-jpeg damaged_entropy: 1 matched test passed.

  • cargo test --release --locked -p j2k-jpeg --test device_plan device_request_decode_accepts_exactly_what_cpu_decode_accepts -- --nocapture: 1 passed.

  • J2K_REQUIRE_METAL_RUNTIME=1 cargo test --release --locked -p j2k-jpeg-metal --test hybrid hybrid_regions_read_exactly_the_rows_cpu_region_decode_reads -- --nocapture: 1 passed on Metal hardware.

  • J2K_REQUIRE_METAL_RUNTIME=1 cargo test --release --locked -p j2k-jpeg-metal --test hybrid explicit_metal_decodes_damaged_fast_packet_jpegs_as_cpu_decode_does -- --nocapture: 1 passed on Metal hardware.

  • cargo test --release --locked -p j2k-jpeg-cuda --features cuda-runtime --lib damaged_entropy: 1 passed; CUDA kernels are not built on this macOS host.

  • cargo xtask semver: passed against the v0.13.1 baseline, including the
    updated ordinary and rustdoc-hidden API inventories.

  • Full CUDA or Metal quick validation: not run.

  • Bug fix: added a test that fails without the fix

  • User-visible change: added a CHANGELOG.md entry under Unreleased

@jcwal1516
jcwal1516 added this pull request to stack #379 December 2, 2026 04:01
Base automatically changed from review/jpeg-sampling to main December 2, 2026 19:14
…rid decode

CPU region decode of an 8-bit sequential scan entropy-decodes only the
MCU rows its route needs (fast RGB rows, generic RGB or component rows,
or the whole scan for partial 4:2:0 streaming), entered at a restart
marker or checkpoint, and checks the end of the scan only when those
rows reach it. Hybrid Metal decode used its own window: the sampled
blocks plus one MCU row above and below, and the whole scan with an EOI
check whenever that window covered the image. So a region whose CPU
decode skips a damaged MCU row or restart marker (12 malformed-input
harness cases, e.g. jpeg/gen-422-48x32-restart1/restart/17921) failed on
Metal, and a window ending at the bottom row skipped the end-of-scan
check CPU decode makes.

Decoder::sequential_decode_rows returns the rows the CPU route for a
request reads, from the same row helpers the routes now share
(JpegFastPacketFamily::decode_rows, baseline_decode_rows, and the route
predicates). The new doc(hidden) adapter function
decode_device_dct_request_into entropy-decodes exactly those rows into
the device window, zeroes stored blocks outside them (CPU decode never
samples them), checks the end of the scan and strict-mode warnings as CPU
decode does, and keeps the whole-image path for full requests. Hybrid
single-request decodes use it; multi-tile viewports keep their window.

Tests: hybrid_regions_read_exactly_the_rows_cpu_region_decode_reads
(j2k-jpeg-metal) fails before this change;
device_request_decode_accepts_exactly_what_cpu_decode_accepts (j2k-jpeg)
compares CPU and device acceptance over damaged streams, formats, scales,
and regions.
…data

Building a cached accelerator plan builds the fast packet, which walks
and validates the whole scan. Only capability mismatches became an
Unsupported plan; any entropy error failed plan construction, so
j2k_jpeg_metal::Decoder::new, Metal sessions, and batches rejected a
damaged 4:2:0, 4:2:2, or 4:4:4 JPEG outright, even for a region request
that CPU decode serves because it never reads the damaged MCUs (61
malformed-input harness cases, and case 10654, whose bogus marker after
the last MCU CPU decode now also rejects for full decodes).

A packet build that fails on malformed entropy data (Huffman, restart,
marker, truncation, or missing-EOI errors) now yields the new doc(hidden)
JpegFastPacketState::MalformedEntropy, carrying the packet error and no
fast-family flags. Header, resource, and internal errors still fail
construction. Metal sees no packet and takes hybrid decode, whose CPU
entropy stage now reads exactly the rows CPU decode reads, so a request
fails exactly when CPU decode of it fails. CUDA decodes only whole
images through packets, so its packet cache returns Error::FastPacket
with the stored error, as before. Valid images build their packet and
keep the GPU-only route unchanged.

Tests: explicit_metal_decodes_damaged_fast_packet_jpegs_as_cpu_decode_does
(j2k-jpeg-metal, 62 harness cases),
damaged_entropy_leaves_a_plan_without_a_packet_that_carries_the_error
(j2k-jpeg), and damaged_entropy_returns_the_fast_packet_error
(j2k-jpeg-cuda) fail before this change.
@jcwal1516
jcwal1516 force-pushed the review/jpeg-damaged-regions branch from 13b23e9 to 69c3414 Compare December 2, 2026 19:24
@jcwal1516
jcwal1516 marked this pull request as ready for review December 2, 2026 23:34
@jcwal1516
jcwal1516 merged commit 1505fbc into main Dec 2, 2026
33 of 36 checks passed
@jcwal1516
jcwal1516 deleted the review/jpeg-damaged-regions branch December 2, 2026 23:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant