Repository navigation
fix(j2k-jpeg): preserve plans for damaged region decodes - #378
Merged
Merged
Conversation
…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
force-pushed
the
review/jpeg-damaged-regions
branch
from
December 2, 2026 19:24
13b23e9 to
69c3414
Compare
jcwal1516
marked this pull request as ready for review
December 2, 2026 23:34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
!to the title)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
JpegFastPacketStateenum isexhaustive. 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 theupdated 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.mdentry underUnreleased