Skip to content

feat(video): scaffold @dcmjs-org/video with the JPEG and MP4 header parsers - #576

Merged
awatson1978 merged 4 commits into
release/1.0-beta-videofrom
video-pr1-image-info
Oct 2, 2026
Merged

awatson1978 merged 4 commits into
release/1.0-beta-videofrom
video-pr1-image-info

Conversation

@awatson1978

Copy link
Copy Markdown
Collaborator

This scaffolds @dcmjs-org/video and ports the JPEG and MP4 header parsers, fixing review findings 9 and 15 inside the port. The two parsers are the codec-free front door of the video story: parseJpegInfo reads the SOF segment to recover Rows/Columns/BitsAllocated/SamplesPerPixel and the matching JPEG transfer syntax, and parseMp4Info walks the ISO BMFF box structure — against a random-access reader, so a 20 GB file costs a few 16-byte header probes plus one moov read — to recover geometry, frame count, frame rate, and the H.264 transfer syntax. Neither decodes a pixel; the whole point is that the compressed stream travels into encapsulated PixelData verbatim.

On provenance: the package scaffold (package.json, the two index files) is hand-written; jpegInfo.js, mp4Info.js, the makeTinyMp4 synthesized-fixture helper, and the mp4Info suite are verbatim ports from the v2 branch, isolated in their own commit with only the import paths moved. On top of that sit the two finding fixes, each with its regression observed red against the ported-unfixed code first:

  • Finding 9 (HIGH). The marker-walk guard only required four readable bytes, but the SOF fields span bytes[offset+4 .. offset+9], and out-of-range reads coerce to 0 in the bitwise math. The review map's vector — FF D8 FF C0 00 11 08 01 E0 02, a 480×640 JPEG cut one byte into the width field — returned columns: 512 instead of throwing, and a file cut right after the length field returned rows: 0, columns: 0. The reader now validates that the declared SOF segment fits inside the input before reading any field. v2 shipped no jpegInfo suite at all, so the new jpegInfo.test.js is hand-written: the exact review vector, the cut-after-length shape, and happy paths for SOF0/SOF1/SOF2/SOF3. Both truncation tests failed ("Received function did not throw") before the fix.
  • Finding 15 (MED). The stts fallback loop trusted the raw uint32 entry_count. With stsz sample_count = 0 and stts entry_count = 0x00FFFFFF the parser spun 16.7 million iterations over zero-coerced reads past the box and fabricated numberOfFrames/frameRate from unrelated moov bytes — at 0xFFFFFFFF, roughly 40 seconds of blocked thread. The loop now requires 16 + entryCount * 8 to fit inside the stts box and throws on the mismatch. The regression corrupts a synthesized MP4 exactly that way and asserts rejection in under two seconds; before the fix it resolved with fabricated values.

One open question I'm raising rather than fixing: a 12-bit extended JPEG currently reports bitsAllocated: 12 straight from the SOF precision byte, but DICOM allows only 8 or 16 for BitsAllocated — the conformant encoding is BitsAllocated 16 with BitsStored 12. The right place for that is buildImageDataset (about five lines) when it lands in the next PR of this wave; I'd like a maintainer nod before encoding that policy.

Seventeen tests in the package — the nine ported mp4Info ones plus the hand-written jpegInfo suite and the stts regression — all green, with no binary fixtures committed (everything is synthesized in memory).

🤖 Generated with Claude Code

awatson1978 and others added 4 commits October 2, 2026 13:02
The package shell for the video wave: name, entry point, MIT license,
repository directory, and the @dcmjs-org/core workspace dependency. The
image-module index exports only the header parsers for now; the dataset
builder joins it in the next slice.

Co-Authored-By: Claude Fable 5 <[email protected]>
Verbatim from v2.0-development: src/image/jpegInfo.js (SOF geometry and
the JPEG transfer syntax mapping), src/image/mp4Info.js (the ISO BMFF box
walk over a random-access reader), the makeTinyMp4 synthesized-fixture
helper, and the ported mp4Info suite. Only the test import paths moved
with the files; no behavior changed in this commit — review findings 9
and 15 are fixed on top, each with its regression observed red first.

Co-Authored-By: Claude Fable 5 <[email protected]>
Review finding 9 (HIGH): the marker-walk guard only required four readable
bytes, but the SOF fields span bytes[offset+4 .. offset+9]. Out-of-range
reads coerce to 0 in the bitwise math, so the review map's vector — a
480x640 JPEG cut one byte into the width field — returned columns 512
instead of throwing, and a file cut right after the length field returned
rows 0, columns 0. The reader now validates that the declared SOF segment
fits inside the input (and is at least 8 bytes) before reading any field.

The new jpegInfo suite is hand-written (v2 carried no suite for this
file): the exact review vector, the cut-after-length shape, and valid
SOF0/SOF1/SOF2/SOF3 paths. Both finding tests were observed red against
the ported-unfixed reader before this fix.

Co-Authored-By: Claude Fable 5 <[email protected]>
Review finding 15 (MED): the stts fallback loop trusted the raw uint32
entry_count. With stsz sample_count 0 and stts entry_count 0x00FFFFFF the
parser spun 16.7 million iterations over zero-coerced reads past the box
and fabricated numberOfFrames/frameRate from unrelated moov bytes; at
0xFFFFFFFF that is roughly 40 seconds of blocked thread. The loop now
requires 16 + entryCount * 8 to fit inside the stts box and throws on the
mismatch. The regression corrupts a synthesized MP4 exactly that way and
asserts the parse rejects in under two seconds — observed red (the
unfixed parser resolved with fabricated values) before this fix.

Co-Authored-By: Claude Fable 5 <[email protected]>
@awatson1978
awatson1978 merged commit 8d0e314 into release/1.0-beta-video Oct 2, 2026
10 checks passed
@awatson1978
awatson1978 deleted the video-pr1-image-info branch October 2, 2026 18:12
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