@dcmjs-org/video 0.0.0-bootstrap.0 → 1.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -0
- package/package.json +12 -3
- package/src/encapsulated/encapsulatedVideo.js +338 -0
- package/src/encapsulated/index.js +21 -0
- package/src/eventStream/fromVideo.js +184 -0
- package/src/image/index.js +8 -0
- package/src/image/mp4Info.js +296 -0
- package/src/index.js +14 -0
- package/test/encapsulatedVideo.test.js +176 -0
- package/test/eventStream/fromVideo.test.js +231 -0
- package/test/mp4Info.test.js +115 -0
- package/test/utils/makeTinyMp4.js +240 -0
package/README.md
CHANGED
|
@@ -1 +1,142 @@
|
|
|
1
1
|
# @dcmjs-org/video
|
|
2
|
+
|
|
3
|
+
MP4-to-DICOM video encapsulation for dcmjs (Supplement 225). The package
|
|
4
|
+
reads the MP4 header without a decode, and puts the H.264 stream of the MP4
|
|
5
|
+
into a Video Photographic Image instance. The stream goes into the pixel data
|
|
6
|
+
fragments without a change, so the round trip is byte-identical.
|
|
7
|
+
|
|
8
|
+
- `parseMp4Info(bytes | reader)` (async) — the geometry, the timing, and
|
|
9
|
+
the transfer syntax of an MP4, without a decode.
|
|
10
|
+
- `buildVideoDataset(mp4Info, options)` (sync) — the dataset without
|
|
11
|
+
`PixelData`. It calls `createInstance` from `@dcmjs-org/core`, then sets
|
|
12
|
+
the stream attributes.
|
|
13
|
+
- `encapsulateVideo(mp4Bytes, options)` (async) — the buffered path. It
|
|
14
|
+
returns a `Promise` of the dataset with `PixelData` as fragments.
|
|
15
|
+
- `createVideoEventSource(input, options)` (async) — the streaming path.
|
|
16
|
+
It returns a `Promise` of `{ run, info, dataset }`:
|
|
17
|
+
- `run(listener)` emits the dataset and the fragments as events, with
|
|
18
|
+
one fragment in memory at a time. It returns a `Promise`.
|
|
19
|
+
- `info` is the `parseMp4Info` result.
|
|
20
|
+
- `dataset` is the `buildVideoDataset` result, without `PixelData`.
|
|
21
|
+
- `extractEncapsulatedVideo(dataset)` — gets the MP4 stream back from a
|
|
22
|
+
naturalized video instance.
|
|
23
|
+
- `videoBase` and `videoPhotographicBase` — the default base blocks.
|
|
24
|
+
|
|
25
|
+
The functions that read an MP4 are async, because `parseMp4Info` accepts a
|
|
26
|
+
random-access reader. `input` of `createVideoEventSource` is MP4 bytes or
|
|
27
|
+
such a reader.
|
|
28
|
+
|
|
29
|
+
The codec-free image builder (`buildImageDataset`) and the JPEG and H.264
|
|
30
|
+
header inspection are in `@dcmjs-org/imaging`.
|
|
31
|
+
|
|
32
|
+
## Options
|
|
33
|
+
|
|
34
|
+
The options of `buildVideoDataset`, `encapsulateVideo`, and
|
|
35
|
+
`createVideoEventSource` follow CORE-USR-1 in `packages/core/README.md`: the
|
|
36
|
+
`createInstance` controls, `overrides`, and these control keys. All of
|
|
37
|
+
them are options. `overrides` is a naturalized DICOM provider with the
|
|
38
|
+
forms of `baseDicom`, and holds only DICOM attributes. A control key never
|
|
39
|
+
goes in `overrides`. An extra option key has no effect (CORE-USR-7).
|
|
40
|
+
|
|
41
|
+
- `baseDicom` (default `videoBase`) — base blocks. `videoBase` has no
|
|
42
|
+
`predecessorBase`, so `predecessor: true` needs a custom base
|
|
43
|
+
(VID-USR-8).
|
|
44
|
+
- `fragmentBytes` (default 256 MiB) — the size of each `PixelData`
|
|
45
|
+
fragment. `buildVideoDataset` ignores it.
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
const video = await encapsulateVideo(mp4Bytes, {
|
|
49
|
+
instance: priorInstanceInSeries,
|
|
50
|
+
dataLevel: "series",
|
|
51
|
+
overrides: { SeriesDescription: "Endoscopy" }
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Scope and change rule
|
|
56
|
+
|
|
57
|
+
The shared requirements of `createInstance` (CORE-USR-n, CORE-IMP-n in
|
|
58
|
+
`packages/core/README.md`) apply to the video builders. This README lists
|
|
59
|
+
only the requirements of this package.
|
|
60
|
+
|
|
61
|
+
The change rule of the core README applies. Change a user requirement
|
|
62
|
+
(VID-USR-n) only when it describes the behaviour wrongly, or when the
|
|
63
|
+
intended user-facing behaviour changes, and name the change in the pull
|
|
64
|
+
request. The implementation requirements (VID-IMP-n) describe the current
|
|
65
|
+
approach, and can change freely. Do not renumber an identifier.
|
|
66
|
+
|
|
67
|
+
## § User requirements
|
|
68
|
+
|
|
69
|
+
- **VID-USR-1** `instance` is where the new video goes (CORE-USR-3). The
|
|
70
|
+
default levels do not copy the identity of `instance` (CORE-USR-4).
|
|
71
|
+
Without a `SOPClassUID` override, and without a custom `levels` that
|
|
72
|
+
lists `SOPClassUID`, `videoBase` sets the Video Photographic Image
|
|
73
|
+
`SOPClassUID`.
|
|
74
|
+
- **VID-USR-2** The stream attributes come from the MP4: `CineRate`,
|
|
75
|
+
`FrameTime`, `NumberOfFrames`, `Rows`, `Columns`,
|
|
76
|
+
`EncapsulatedPixelDataValueTotalLength (7FE0,0003)`, and
|
|
77
|
+
`_meta.TransferSyntaxUID`. `buildVideoDataset` sets them after
|
|
78
|
+
`createInstance`, and replaces an override of them with no warning.
|
|
79
|
+
This rule applies to each form of `overrides`. A field that `mp4Info`
|
|
80
|
+
does not give writes no attribute, so the value of the base, of
|
|
81
|
+
`instance`, or of `overrides` stays. The `_meta` rule of CORE-USR-13
|
|
82
|
+
applies: the builder always sets `_meta.TransferSyntaxUID`, and keeps
|
|
83
|
+
the other `_meta` keys. `encapsulateVideo` and `createVideoEventSource`
|
|
84
|
+
set `PixelData` from the stream. `createVideoEventSource` does not
|
|
85
|
+
change the `_meta` of the dataset that it returns. It makes the file
|
|
86
|
+
meta group of its events in a separate object. The file meta group
|
|
87
|
+
gets each key of the dataset `_meta`, for example
|
|
88
|
+
`ImplementationVersionName`. `createVideoEventSource` always sets
|
|
89
|
+
`TransferSyntaxUID`, `MediaStorageSOPClassUID`, and
|
|
90
|
+
`MediaStorageSOPInstanceUID` from the dataset. It sets
|
|
91
|
+
`FileMetaInformationVersion`, `ImplementationClassUID`, and
|
|
92
|
+
`ImplementationVersionName` only when the dataset `_meta` has no value.
|
|
93
|
+
Thus a caller can set these three keys and other file meta keys in
|
|
94
|
+
`overrides._meta`.
|
|
95
|
+
- **VID-USR-3** A caller can set in `overrides` the Image Pixel values
|
|
96
|
+
that the MPEG-4 AVC transfer syntaxes mandate (`SamplesPerPixel`,
|
|
97
|
+
`PhotometricInterpretation`, `PlanarConfiguration`, `BitsAllocated`,
|
|
98
|
+
`BitsStored`, `HighBit`, `PixelRepresentation`), and
|
|
99
|
+
`LossyImageCompression`. The builder permits this on purpose. A
|
|
100
|
+
conformant reader uses the values of the codec stream, and these values
|
|
101
|
+
take precedence over the dataset values.
|
|
102
|
+
- **VID-USR-4** An MP4 with a codec that no DICOM video transfer syntax
|
|
103
|
+
carries throws an error. The error gives an `ffmpeg` command that makes a
|
|
104
|
+
supported stream (H.264 Baseline, Main, or High, up to Level 4.2).
|
|
105
|
+
- **VID-USR-5** `fragmentBytes` is an even integer from 2 to 2^32-2. Each
|
|
106
|
+
other value throws an error.
|
|
107
|
+
- **VID-USR-6** `extractEncapsulatedVideo` returns the stream that was
|
|
108
|
+
encapsulated, byte for byte. It removes the pad byte of an odd-length
|
|
109
|
+
last fragment through the declared total length. A dataset that is not
|
|
110
|
+
a video throws an error. A video dataset has the Video Photographic
|
|
111
|
+
Image `SOPClassUID` or a video transfer syntax, and has `PixelData`.
|
|
112
|
+
- **VID-USR-7** `parseMp4Info`, and thus `encapsulateVideo` and
|
|
113
|
+
`createVideoEventSource`, throw an error for an MP4 that they cannot
|
|
114
|
+
read (CORE-IMP-11): no `ftyp` box first, a malformed box, no `moov`
|
|
115
|
+
box, no video track, a video track without `mdhd` or a sample
|
|
116
|
+
description, a malformed `stts` box, or no frame rate.
|
|
117
|
+
- **VID-USR-8** `videoBase` does not include `predecessorBase`
|
|
118
|
+
(CORE-USR-9). With the default base, `predecessor: true` has no effect:
|
|
119
|
+
the result has no `PredecessorDocumentsSequence`, and the builder throws
|
|
120
|
+
no predecessor error. For a predecessor, give
|
|
121
|
+
`baseDicom: combineBases(videoBase, predecessorBase)`, with
|
|
122
|
+
`predecessorBase` from `@dcmjs-org/core`.
|
|
123
|
+
|
|
124
|
+
## § Implementation requirements
|
|
125
|
+
|
|
126
|
+
- **VID-IMP-1** `videoBase` is `defaultInstanceBase`,
|
|
127
|
+
`videoPhotographicBase` (SOP class, Modality `XC`, the Image Pixel values
|
|
128
|
+
of VID-USR-3, `LossyImageCompression` `"01"`) (CORE-IMP-3). `videoBase`
|
|
129
|
+
has no `predecessorBase` (VID-USR-8).
|
|
130
|
+
- **VID-IMP-2** `buildVideoDataset` removes its control key `fragmentBytes`
|
|
131
|
+
before it calls `createInstance` (CORE-IMP-9), and passes `overrides`
|
|
132
|
+
and `postProcess` through.
|
|
133
|
+
- **VID-IMP-3** `EncapsulatedPixelDataValueTotalLength` is a BigInt,
|
|
134
|
+
because VR UV is a 64-bit unsigned integer.
|
|
135
|
+
- **VID-IMP-4** `extractEncapsulatedVideo` keeps all of the bytes when the
|
|
136
|
+
dataset has no total length, and returns `declaredLength: null`, so the
|
|
137
|
+
caller can choose a warning.
|
|
138
|
+
|
|
139
|
+
## Dependencies
|
|
140
|
+
|
|
141
|
+
The package builds on `@dcmjs-org/core`, `@dcmjs-org/imaging` (the shared
|
|
142
|
+
`h264TransferSyntaxUID` map), and `@dcmjs-org/parser`.
|
package/package.json
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dcmjs-org/video",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.0-beta.3",
|
|
4
|
+
"description": "MP4-to-DICOM video encapsulation for dcmjs (Supplement 225): MP4 header inspection without decoding, and wrapping/streaming an MP4's H.264 stream into a Video Photographic Image instance. Codec-free image building lives in @dcmjs-org/imaging",
|
|
5
|
+
"main": "./src/index.js",
|
|
5
6
|
"license": "MIT",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
8
9
|
"url": "git+https://github.com/dcmjs-org/dcmjs.git",
|
|
9
10
|
"directory": "packages/video"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@dcmjs-org/core": "1.0.0-beta.3",
|
|
14
|
+
"@dcmjs-org/imaging": "1.0.0-beta.3",
|
|
15
|
+
"@dcmjs-org/parser": "1.0.0-beta.3"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@dcmjs-org/generator": "1.0.0-beta.3"
|
|
10
19
|
}
|
|
11
|
-
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// src/encapsulated/encapsulatedVideo.js
|
|
2
|
+
//
|
|
3
|
+
// Video encapsulation (Supplement 225): an MP4's H.264 stream travels inside
|
|
4
|
+
// a Video Photographic Image instance VERBATIM — the pixel data fragments are
|
|
5
|
+
// consecutive byte ranges of the one MP4 stream, so extraction is
|
|
6
|
+
// concatenation and the round trip is byte-identical. Two directions,
|
|
7
|
+
// mirroring encapsulatedPdf.js:
|
|
8
|
+
//
|
|
9
|
+
// buildVideoDataset(mp4Info, options) — dataset shell (no PixelData), used
|
|
10
|
+
// by both the buffered and the streaming encapsulation paths.
|
|
11
|
+
// encapsulateVideo(mp4Bytes, options) — MP4 in (buffered): dataset plus
|
|
12
|
+
// PixelData as fragment-sized ArrayBuffer slices.
|
|
13
|
+
// extractEncapsulatedVideo(dataset) — MP4 out: recover the stream from a
|
|
14
|
+
// naturalized video instance, truncated to the declared total length.
|
|
15
|
+
//
|
|
16
|
+
// The (7FE0,0003) Encapsulated Pixel Data Value Total Length element (VR UV,
|
|
17
|
+
// uint64) records the exact stream length so the required trailing pad byte
|
|
18
|
+
// on an odd-length final fragment can be dropped on extraction.
|
|
19
|
+
|
|
20
|
+
import {
|
|
21
|
+
combineBases,
|
|
22
|
+
createInstance,
|
|
23
|
+
defaultInstanceBase,
|
|
24
|
+
isVideoTransferSyntax
|
|
25
|
+
} from "@dcmjs-org/core";
|
|
26
|
+
import { parseMp4Info } from "../image/mp4Info.js";
|
|
27
|
+
|
|
28
|
+
const VIDEO_PHOTOGRAPHIC_SOP_CLASS_UID = "1.2.840.10008.5.1.4.1.1.77.1.4.1";
|
|
29
|
+
|
|
30
|
+
// 256 MiB: small enough that one in-flight fragment stays comfortably in
|
|
31
|
+
// memory, large enough that a 21.8 GB stream is only ~82 fragments.
|
|
32
|
+
const DEFAULT_FRAGMENT_BYTES = 256 * 1024 * 1024;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Validate/default the encapsulation fragment size. Fragment item lengths are
|
|
36
|
+
* 32-bit and must be even (PS3.5 §A.4), so the size must be an even integer
|
|
37
|
+
* below 2^32-2.
|
|
38
|
+
*
|
|
39
|
+
* @param {number} [fragmentBytes]
|
|
40
|
+
* @returns {number}
|
|
41
|
+
*/
|
|
42
|
+
function normalizeFragmentBytes(fragmentBytes) {
|
|
43
|
+
if (fragmentBytes === undefined || fragmentBytes === null) {
|
|
44
|
+
return DEFAULT_FRAGMENT_BYTES;
|
|
45
|
+
}
|
|
46
|
+
if (
|
|
47
|
+
!Number.isInteger(fragmentBytes) ||
|
|
48
|
+
fragmentBytes < 2 ||
|
|
49
|
+
fragmentBytes % 2 !== 0 ||
|
|
50
|
+
fragmentBytes >= 0xfffffffe
|
|
51
|
+
) {
|
|
52
|
+
throw new Error(
|
|
53
|
+
`fragmentBytes must be an even integer between 2 and 2^32-2 ` +
|
|
54
|
+
`(fragment item lengths are even 32-bit values); got ${fragmentBytes}`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return fragmentBytes;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The corrective error for codecs no DICOM video transfer syntax carries. */
|
|
61
|
+
function unsupportedCodecError(info) {
|
|
62
|
+
const profile =
|
|
63
|
+
info.profileIdc !== null
|
|
64
|
+
? ` profile ${info.profileIdc} level ${(info.levelIdc / 10).toFixed(
|
|
65
|
+
1
|
|
66
|
+
)}`
|
|
67
|
+
: "";
|
|
68
|
+
return new Error(
|
|
69
|
+
`unsupported video codec '${info.codec}'${profile} — DICOM video ` +
|
|
70
|
+
"encapsulation supports H.264 Baseline/Main/High up to Level 4.2. " +
|
|
71
|
+
"Transcode first, e.g.: ffmpeg -i in.mp4 -c:v libx264 " +
|
|
72
|
+
"-profile:v high -level 4.2 -c:a copy out.mp4"
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The Video Photographic Image attributes that do not depend on the stream:
|
|
78
|
+
* XC modality, and the Image Pixel values that every MPEG-4 AVC transfer
|
|
79
|
+
* syntax mandates (PS3.5 §8.2.6); the stream carries the real subsampling.
|
|
80
|
+
*/
|
|
81
|
+
const videoPhotographicBase = {
|
|
82
|
+
SOPClassUID: VIDEO_PHOTOGRAPHIC_SOP_CLASS_UID,
|
|
83
|
+
Modality: "XC",
|
|
84
|
+
SamplesPerPixel: 3,
|
|
85
|
+
PhotometricInterpretation: "YBR_PARTIAL_420",
|
|
86
|
+
PlanarConfiguration: 0,
|
|
87
|
+
BitsAllocated: 8,
|
|
88
|
+
BitsStored: 8,
|
|
89
|
+
HighBit: 7,
|
|
90
|
+
PixelRepresentation: 0,
|
|
91
|
+
LossyImageCompression: "01"
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The default base of `buildVideoDataset`: core instance defaults and the
|
|
96
|
+
* Video Photographic Image block. It has no `predecessorBase`, so
|
|
97
|
+
* `predecessor: true` has no effect; use
|
|
98
|
+
* `combineBases(videoBase, predecessorBase)` for a predecessor
|
|
99
|
+
* (CORE-USR-9). Use `.items` to build a variant.
|
|
100
|
+
*/
|
|
101
|
+
const videoBase = combineBases(defaultInstanceBase, videoPhotographicBase);
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Build the naturalized dataset shell for a Video Photographic Image
|
|
105
|
+
* instance — everything except PixelData, which the caller supplies either
|
|
106
|
+
* buffered (encapsulateVideo) or as a fragment event stream
|
|
107
|
+
* (createVideoEventSource).
|
|
108
|
+
*
|
|
109
|
+
* The dataset comes from {@link createInstance} with `videoBase`; the
|
|
110
|
+
* options follow CORE-USR-1 (`packages/core/README.md`), and the video rules
|
|
111
|
+
* are VID-USR-n in `packages/video/README.md`. The options object holds the
|
|
112
|
+
* controls; the DICOM attributes go in `options.overrides` (a naturalized
|
|
113
|
+
* DICOM provider with the forms of `baseDicom`). An extra option key has no
|
|
114
|
+
* effect (CORE-USR-7). With `dataLevel: "series"`, `instance` is the sample
|
|
115
|
+
* instance in the series (CORE-USR-5).
|
|
116
|
+
*
|
|
117
|
+
* The stream attributes are set last, and replace an override from each
|
|
118
|
+
* form of `overrides` (VID-USR-2): CineRate and FrameTime from the measured
|
|
119
|
+
* frame rate, NumberOfFrames, Rows, Columns, `_meta.TransferSyntaxUID`, and
|
|
120
|
+
* the (7FE0,0003) total length as a BigInt (VR UV is a uint64). A field
|
|
121
|
+
* that `mp4Info` does not give writes no attribute. The element list
|
|
122
|
+
* mirrors the Supplement 225 fixture.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* buildVideoDataset(mp4Info, {
|
|
126
|
+
* instance,
|
|
127
|
+
* overrides: { SeriesDescription: "Endoscopy" }
|
|
128
|
+
* });
|
|
129
|
+
*
|
|
130
|
+
* @param {Object} mp4Info - parseMp4Info result
|
|
131
|
+
* @param {Object} [options] - the createInstance options (`baseDicom`
|
|
132
|
+
* defaults to `videoBase`; `overrides` holds the DICOM attributes;
|
|
133
|
+
* createInstance calls `postProcess` before the builder sets the stream
|
|
134
|
+
* attributes), plus the control option of the callers (`fragmentBytes`),
|
|
135
|
+
* which is ignored here
|
|
136
|
+
* @returns {Object} naturalized dataset with _meta/_vrMap, no PixelData
|
|
137
|
+
*/
|
|
138
|
+
function buildVideoDataset(mp4Info, options = {}) {
|
|
139
|
+
if (!mp4Info.transferSyntaxUID) {
|
|
140
|
+
throw unsupportedCodecError(mp4Info);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// fragmentBytes is a control key of the callers (CORE-IMP-9).
|
|
144
|
+
const { baseDicom = videoBase, fragmentBytes, ...rest } = options;
|
|
145
|
+
const dataset = createInstance({ baseDicom, ...rest });
|
|
146
|
+
|
|
147
|
+
const streamAttributes = {
|
|
148
|
+
CineRate: mp4Info.cineRate,
|
|
149
|
+
FrameTime: mp4Info.frameTime,
|
|
150
|
+
NumberOfFrames: mp4Info.numberOfFrames,
|
|
151
|
+
Rows: mp4Info.rows,
|
|
152
|
+
Columns: mp4Info.columns
|
|
153
|
+
};
|
|
154
|
+
for (const [keyword, value] of Object.entries(streamAttributes)) {
|
|
155
|
+
if (value !== undefined) {
|
|
156
|
+
dataset[keyword] = value;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Exact stream length, without the pad byte of an odd-length final
|
|
160
|
+
// fragment (Sup 225).
|
|
161
|
+
dataset.EncapsulatedPixelDataValueTotalLength = BigInt(
|
|
162
|
+
mp4Info.mp4ByteLength
|
|
163
|
+
);
|
|
164
|
+
// A copy that keeps the other _meta keys (CORE-USR-13).
|
|
165
|
+
dataset._meta = {
|
|
166
|
+
...(dataset._meta || {}),
|
|
167
|
+
TransferSyntaxUID: { Value: [mp4Info.transferSyntaxUID] }
|
|
168
|
+
};
|
|
169
|
+
dataset._vrMap = { ...(dataset._vrMap || {}), PixelData: "OB" };
|
|
170
|
+
return dataset;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Wrap MP4 bytes into a naturalized video instance, buffered: PixelData is an
|
|
175
|
+
* array of fragment-sized ArrayBuffer slices ready for datasetToDict /
|
|
176
|
+
* datasetToBuffer. For files too large to buffer, use
|
|
177
|
+
* createVideoEventSource instead.
|
|
178
|
+
*
|
|
179
|
+
* @param {ArrayBuffer|Uint8Array} mp4Bytes
|
|
180
|
+
* @param {Object} [options] - the buildVideoDataset options (the DICOM
|
|
181
|
+
* attributes go in `overrides`), plus `fragmentBytes`
|
|
182
|
+
* @returns {Promise<Object>} naturalized dataset
|
|
183
|
+
*/
|
|
184
|
+
async function encapsulateVideo(mp4Bytes, options = {}) {
|
|
185
|
+
const bytes =
|
|
186
|
+
mp4Bytes instanceof ArrayBuffer
|
|
187
|
+
? new Uint8Array(mp4Bytes)
|
|
188
|
+
: new Uint8Array(
|
|
189
|
+
mp4Bytes.buffer,
|
|
190
|
+
mp4Bytes.byteOffset,
|
|
191
|
+
mp4Bytes.byteLength
|
|
192
|
+
);
|
|
193
|
+
const info = await parseMp4Info(bytes);
|
|
194
|
+
const dataset = buildVideoDataset(info, options);
|
|
195
|
+
const fragmentBytes = normalizeFragmentBytes(options.fragmentBytes);
|
|
196
|
+
|
|
197
|
+
const fragments = [];
|
|
198
|
+
for (let offset = 0; offset < bytes.byteLength; offset += fragmentBytes) {
|
|
199
|
+
const end = Math.min(offset + fragmentBytes, bytes.byteLength);
|
|
200
|
+
// slice() so each fragment is an exact, unshared ArrayBuffer.
|
|
201
|
+
fragments.push(
|
|
202
|
+
bytes.buffer.slice(
|
|
203
|
+
bytes.byteOffset + offset,
|
|
204
|
+
bytes.byteOffset + end
|
|
205
|
+
)
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
dataset.PixelData = fragments;
|
|
209
|
+
return dataset;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Unwrap one naturalized binary payload to fragments of Uint8Array. */
|
|
213
|
+
function toFragmentList(payload) {
|
|
214
|
+
if (payload === undefined || payload === null) {
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
// The event-stream naturalizer wraps binary as { InlineBinary } (an
|
|
218
|
+
// ArrayBuffer, a view, a base64 string, or an array of fragments).
|
|
219
|
+
if (
|
|
220
|
+
typeof payload === "object" &&
|
|
221
|
+
!(payload instanceof ArrayBuffer) &&
|
|
222
|
+
!ArrayBuffer.isView(payload) &&
|
|
223
|
+
!Array.isArray(payload) &&
|
|
224
|
+
payload.InlineBinary !== undefined
|
|
225
|
+
) {
|
|
226
|
+
payload = payload.InlineBinary;
|
|
227
|
+
}
|
|
228
|
+
const list = Array.isArray(payload) ? payload : [payload];
|
|
229
|
+
return list.map(fragment => {
|
|
230
|
+
if (typeof fragment === "string") {
|
|
231
|
+
const binary = atob(fragment);
|
|
232
|
+
const decoded = new Uint8Array(binary.length);
|
|
233
|
+
for (let i = 0; i < binary.length; i++) {
|
|
234
|
+
decoded[i] = binary.charCodeAt(i);
|
|
235
|
+
}
|
|
236
|
+
return decoded;
|
|
237
|
+
}
|
|
238
|
+
if (
|
|
239
|
+
fragment &&
|
|
240
|
+
typeof fragment === "object" &&
|
|
241
|
+
!(fragment instanceof ArrayBuffer) &&
|
|
242
|
+
!ArrayBuffer.isView(fragment) &&
|
|
243
|
+
fragment.InlineBinary !== undefined
|
|
244
|
+
) {
|
|
245
|
+
return toFragmentList(fragment)[0];
|
|
246
|
+
}
|
|
247
|
+
return ArrayBuffer.isView(fragment)
|
|
248
|
+
? new Uint8Array(
|
|
249
|
+
fragment.buffer,
|
|
250
|
+
fragment.byteOffset,
|
|
251
|
+
fragment.byteLength
|
|
252
|
+
)
|
|
253
|
+
: new Uint8Array(fragment);
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** The meta transfer syntax of a naturalized dataset, tolerant of shape. */
|
|
258
|
+
function metaTransferSyntax(dataset) {
|
|
259
|
+
const meta = dataset._meta;
|
|
260
|
+
if (!meta) {
|
|
261
|
+
return undefined;
|
|
262
|
+
}
|
|
263
|
+
const entry = meta.TransferSyntaxUID || meta["00020010"];
|
|
264
|
+
if (!entry) {
|
|
265
|
+
return undefined;
|
|
266
|
+
}
|
|
267
|
+
if (typeof entry === "string") {
|
|
268
|
+
return entry;
|
|
269
|
+
}
|
|
270
|
+
return Array.isArray(entry.Value) ? entry.Value[0] : entry.Value;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Recover the verbatim video stream from a naturalized video instance.
|
|
275
|
+
*
|
|
276
|
+
* Fragments are concatenated and the result truncated to the declared
|
|
277
|
+
* (7FE0,0003) total length, which drops the pad byte a Part 10 writer adds
|
|
278
|
+
* to an odd-length final fragment. When the element is absent (older
|
|
279
|
+
* producers), nothing is trimmed and `declaredLength` is null so the caller
|
|
280
|
+
* can decide how loudly to warn. A dataset that is not a video throws an
|
|
281
|
+
* error (VID-USR-6).
|
|
282
|
+
*
|
|
283
|
+
* @param {Object} dataset - naturalized video instance
|
|
284
|
+
* @returns {{ bytes: Uint8Array, transferSyntaxUID: string|undefined,
|
|
285
|
+
* declaredLength: number|null }}
|
|
286
|
+
*/
|
|
287
|
+
function extractEncapsulatedVideo(dataset) {
|
|
288
|
+
const transferSyntaxUID = metaTransferSyntax(dataset);
|
|
289
|
+
const isVideo =
|
|
290
|
+
dataset.SOPClassUID === VIDEO_PHOTOGRAPHIC_SOP_CLASS_UID ||
|
|
291
|
+
isVideoTransferSyntax(transferSyntaxUID);
|
|
292
|
+
if (!isVideo || dataset.PixelData === undefined) {
|
|
293
|
+
throw new Error(
|
|
294
|
+
"extractEncapsulatedVideo: dataset is not an encapsulated video " +
|
|
295
|
+
"instance (expected SOP Class " +
|
|
296
|
+
VIDEO_PHOTOGRAPHIC_SOP_CLASS_UID +
|
|
297
|
+
" or a video transfer syntax, with PixelData). " +
|
|
298
|
+
(transferSyntaxUID
|
|
299
|
+
? `Found transfer syntax ${transferSyntaxUID}. `
|
|
300
|
+
: "") +
|
|
301
|
+
"For an Encapsulated PDF use extractEncapsulatedPdf; for " +
|
|
302
|
+
"pixel images read the dataset with NaturalizedListener."
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const fragments = toFragmentList(dataset.PixelData);
|
|
307
|
+
const total = fragments.reduce((n, f) => n + f.byteLength, 0);
|
|
308
|
+
const out = new Uint8Array(total);
|
|
309
|
+
let offset = 0;
|
|
310
|
+
for (const fragment of fragments) {
|
|
311
|
+
out.set(fragment, offset);
|
|
312
|
+
offset += fragment.byteLength;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const declared = dataset.EncapsulatedPixelDataValueTotalLength;
|
|
316
|
+
const declaredLength =
|
|
317
|
+
declared === undefined || declared === null
|
|
318
|
+
? null
|
|
319
|
+
: Number(Array.isArray(declared) ? declared[0] : declared);
|
|
320
|
+
|
|
321
|
+
const bytes =
|
|
322
|
+
declaredLength !== null && declaredLength <= out.byteLength
|
|
323
|
+
? out.subarray(0, declaredLength)
|
|
324
|
+
: out;
|
|
325
|
+
|
|
326
|
+
return { bytes, transferSyntaxUID, declaredLength };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export {
|
|
330
|
+
videoBase,
|
|
331
|
+
videoPhotographicBase,
|
|
332
|
+
buildVideoDataset,
|
|
333
|
+
encapsulateVideo,
|
|
334
|
+
extractEncapsulatedVideo,
|
|
335
|
+
normalizeFragmentBytes,
|
|
336
|
+
VIDEO_PHOTOGRAPHIC_SOP_CLASS_UID,
|
|
337
|
+
DEFAULT_FRAGMENT_BYTES
|
|
338
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/encapsulated/index.js — the video half of the encapsulated module.
|
|
2
|
+
// The PDF half (encapsulatePdf) is in @dcmjs-org/encapsulated.
|
|
3
|
+
import {
|
|
4
|
+
videoBase,
|
|
5
|
+
videoPhotographicBase,
|
|
6
|
+
buildVideoDataset,
|
|
7
|
+
encapsulateVideo,
|
|
8
|
+
extractEncapsulatedVideo,
|
|
9
|
+
VIDEO_PHOTOGRAPHIC_SOP_CLASS_UID,
|
|
10
|
+
DEFAULT_FRAGMENT_BYTES
|
|
11
|
+
} from "./encapsulatedVideo.js";
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
videoBase,
|
|
15
|
+
videoPhotographicBase,
|
|
16
|
+
buildVideoDataset,
|
|
17
|
+
encapsulateVideo,
|
|
18
|
+
extractEncapsulatedVideo,
|
|
19
|
+
VIDEO_PHOTOGRAPHIC_SOP_CLASS_UID,
|
|
20
|
+
DEFAULT_FRAGMENT_BYTES
|
|
21
|
+
};
|