@bubo-squared/gyroview 0.2.0 → 0.3.0

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/README.md +16 -5
  3. package/dist/index.d.ts +1 -0
  4. package/dist/packages/adapters/fetch/src/HttpByteStream.js +174 -0
  5. package/dist/packages/adapters/fetch/src/HttpRangeSource.js +14 -170
  6. package/dist/packages/adapters/fetch/src/HttpResource.js +192 -0
  7. package/dist/packages/adapters/fetch/src/contentRange.js +15 -0
  8. package/dist/packages/adapters/fetch/src/httpRequest.js +5 -3
  9. package/dist/packages/adapters/fetch/src/index.js +3 -1
  10. package/dist/packages/adapters/fetch/src/passingFailures.js +24 -0
  11. package/dist/packages/adapters/fetch/src/rangeFailures.js +17 -0
  12. package/dist/packages/adapters/fetch/src/recordingVersion.js +37 -0
  13. package/dist/packages/adapters/mediabunny/src/MediabunnyAudioPackager.js +129 -0
  14. package/dist/packages/adapters/mediabunny/src/MediabunnyCodecReader.js +80 -0
  15. package/dist/packages/adapters/mediabunny/src/SegmentChannel.js +58 -6
  16. package/dist/packages/adapters/mediabunny/src/decoderConfigurations.js +28 -0
  17. package/dist/packages/adapters/mediabunny/src/index.js +3 -2
  18. package/dist/packages/core/src/application/download/BlockStore.js +97 -0
  19. package/dist/packages/core/src/application/download/DownloadedAudioSamples.js +50 -0
  20. package/dist/packages/core/src/application/download/DownloadedVideoTrack.js +122 -0
  21. package/dist/packages/core/src/application/download/FileDownload.js +183 -0
  22. package/dist/packages/core/src/application/download/RecordingBuffer.js +22 -0
  23. package/dist/packages/core/src/application/download/SampleCursor.js +88 -0
  24. package/dist/packages/core/src/application/download/SourceByteStream.js +46 -0
  25. package/dist/packages/core/src/application/download/Transfers.js +75 -0
  26. package/dist/packages/core/src/application/download/startFileDownload.js +37 -0
  27. package/dist/packages/core/src/application/playback/Buffering.js +53 -0
  28. package/dist/packages/core/src/application/playback/DecodePipeline.js +11 -7
  29. package/dist/packages/core/src/application/playback/DecodeRun.js +15 -0
  30. package/dist/packages/core/src/application/playback/PlaybackSession.js +18 -27
  31. package/dist/packages/core/src/application/playback/keyframeTimeAt.js +1 -1
  32. package/dist/packages/core/src/application/playback/probeDecoding.js +15 -1
  33. package/dist/packages/core/src/application/recording/readSampleTable.js +27 -0
  34. package/dist/packages/core/src/application/recording/timeRecording.js +3 -3
  35. package/dist/packages/core/src/domain/container/KeyframeRule.js +7 -0
  36. package/dist/packages/core/src/domain/container/SampleTable.js +29 -0
  37. package/dist/packages/core/src/domain/container/TrackSampleTable.js +96 -0
  38. package/dist/packages/core/src/domain/container/presentationOrder.js +10 -0
  39. package/dist/packages/core/src/domain/download/DownloadPolicy.js +41 -0
  40. package/dist/packages/core/src/domain/download/isReadyToResume.js +27 -0
  41. package/dist/packages/core/src/domain/download/planDownloads.js +175 -0
  42. package/dist/packages/core/src/domain/download/trackNeeds.js +32 -0
  43. package/dist/packages/core/src/domain/format/boxes/boxHeader.js +44 -0
  44. package/dist/packages/core/src/domain/format/boxes/scanBoxes.js +7 -34
  45. package/dist/packages/core/src/domain/format/mp4/keyframeRules.js +81 -0
  46. package/dist/packages/core/src/domain/format/mp4/movieBoxes.js +52 -0
  47. package/dist/packages/core/src/domain/format/mp4/mp4BoxTypes.js +46 -0
  48. package/dist/packages/core/src/domain/format/mp4/mp4Layouts.js +183 -0
  49. package/dist/packages/core/src/domain/format/mp4/parseMovie.js +67 -0
  50. package/dist/packages/core/src/domain/format/mp4/readTable.js +31 -0
  51. package/dist/packages/core/src/domain/format/mp4/sampleLocations.js +69 -0
  52. package/dist/packages/core/src/domain/format/mp4/sampleTiming.js +109 -0
  53. package/dist/packages/core/src/domain/format/mp4/trackHeaders.js +75 -0
  54. package/dist/packages/core/src/index.js +9 -1
  55. package/dist/packages/core/src/shared/async/iteration.js +24 -0
  56. package/dist/packages/core/src/shared/binary/ByteRange.js +12 -0
  57. package/dist/packages/core/src/shared/binary/ByteRangeSet.js +85 -0
  58. package/dist/packages/core/src/shared/binary/ByteReader.js +21 -3
  59. package/dist/packages/core/src/shared/binary/concatenated.js +15 -0
  60. package/dist/packages/core/src/shared/errors/GyroViewError.js +1 -0
  61. package/dist/packages/core/src/shared/math/countAtOrBelow.js +17 -0
  62. package/dist/packages/player/src/composition/browserPorts.js +23 -4
  63. package/dist/packages/player/src/composition/buildPipeline.js +15 -3
  64. package/dist/packages/player/src/composition/openInputs.js +51 -107
  65. package/dist/packages/player/src/composition/readInputs.js +111 -0
  66. package/dist/packages/player/src/controls/messages.js +1 -0
  67. package/dist/packages/player/src/inspectRecording.js +1 -1
  68. package/dist/standalone.js +58 -58
  69. package/package.json +1 -1
  70. package/dist/packages/adapters/mediabunny/src/MediabunnyAudioSegments.js +0 -133
  71. package/dist/packages/adapters/mediabunny/src/MediabunnyDemuxer.js +0 -63
  72. package/dist/packages/adapters/mediabunny/src/MediabunnyVideoTrackReader.js +0 -84
@@ -0,0 +1,32 @@
1
+ import { ByteRange } from "../../shared/binary/ByteRange.js";
2
+ import { ByteRangeSet } from "../../shared/binary/ByteRangeSet.js";
3
+ //#region ../../packages/core/src/domain/download/trackNeeds.ts
4
+ /**
5
+ * The ranges of the samples a cursor will hand out, in decode order, up to the first that shows
6
+ * after `until`: with B-frames the window ends up to a reordering group early, which the plans
7
+ * after read.
8
+ */
9
+ function needsOf(cursor, until) {
10
+ const { track } = cursor;
11
+ const needs = [];
12
+ for (let sample = cursor.sample; sample < track.sampleCount; sample += 1) {
13
+ if (track.timestampOf(sample) > until) break;
14
+ needs.push(track.rangeOf(sample));
15
+ }
16
+ return needs;
17
+ }
18
+ /**
19
+ * The first `budget` bytes of `wanted`, lowest first; the gaps between its ranges cost nothing.
20
+ */
21
+ function withinBytes(wanted, budget) {
22
+ const kept = [];
23
+ let left = budget;
24
+ for (const range of wanted.ranges) {
25
+ if (left <= 0) break;
26
+ kept.push(ByteRange.of(range.offset, Math.min(range.length, left)));
27
+ left -= range.length;
28
+ }
29
+ return ByteRangeSet.of(kept);
30
+ }
31
+ //#endregion
32
+ export { needsOf, withinBytes };
@@ -0,0 +1,44 @@
1
+ import { hasErrorCode } from "../../../shared/errors/GyroViewError.js";
2
+ import { isPlausibleBoxType } from "./boxConstants.js";
3
+ //#region ../../packages/core/src/domain/format/boxes/boxHeader.ts
4
+ /**
5
+ * The header of the box whose first bytes `header` holds, in a space of `room` bytes from the
6
+ * box's start (to the end of the file, or of the box around it), which a size of zero fills.
7
+ * Undefined when those bytes form no box that fits there: the file's boxes have ended and a
8
+ * bare trailer begins, or a box inside another is malformed.
9
+ */
10
+ function boxHeaderOf(header, room) {
11
+ if (header.length < 8) return void 0;
12
+ const type = header.asciiAt(4, 4);
13
+ if (!isPlausibleBoxType(type)) return void 0;
14
+ const extent = extentOf(header, room);
15
+ return extent !== void 0 && extent.size >= extent.headerSize && extent.size <= room ? {
16
+ type,
17
+ ...extent
18
+ } : void 0;
19
+ }
20
+ /**
21
+ * Undefined when the header declares a large size that is not a safe integer: real boxes never
22
+ * do, so those bytes are not a box.
23
+ */
24
+ function extentOf(header, room) {
25
+ const declared = header.uint32BeAt(0);
26
+ if (declared === 1 && header.length >= 16) return largeExtent(header);
27
+ return {
28
+ size: declared === 0 ? room : declared,
29
+ headerSize: 8
30
+ };
31
+ }
32
+ function largeExtent(header) {
33
+ try {
34
+ return {
35
+ size: header.uint64BeAt(8),
36
+ headerSize: 16
37
+ };
38
+ } catch (error) {
39
+ if (hasErrorCode(error, "binary-unsafe-integer")) return void 0;
40
+ throw error;
41
+ }
42
+ }
43
+ //#endregion
44
+ export { boxHeaderOf };
@@ -1,7 +1,6 @@
1
- import { hasErrorCode } from "../../../shared/errors/GyroViewError.js";
2
1
  import { ByteReader } from "../../../shared/binary/ByteReader.js";
3
2
  import { ByteRange } from "../../../shared/binary/ByteRange.js";
4
- import { isPlausibleBoxType } from "./boxConstants.js";
3
+ import { boxHeaderOf } from "./boxHeader.js";
5
4
  //#region ../../packages/core/src/domain/format/boxes/scanBoxes.ts
6
5
  /**
7
6
  * Walks the top-level boxes of a file with one small read per box, and stops at the first bytes
@@ -20,40 +19,14 @@ async function scanBoxes(source, fileSize) {
20
19
  return boxes;
21
20
  }
22
21
  async function readBoxAt(source, offset, fileSize) {
23
- const headerLength = Math.min(16, fileSize - offset);
22
+ const room = fileSize - offset;
23
+ const headerLength = Math.min(16, room);
24
24
  const header = new ByteReader(await source.read(ByteRange.of(offset, headerLength)));
25
- const type = header.asciiAt(4, 4);
26
- if (!isPlausibleBoxType(type)) return void 0;
27
- const resolved = resolveSize(header, offset, fileSize);
28
- if (!resolved) return void 0;
29
- const { size, headerSize } = resolved;
30
- return size >= headerSize && offset + size <= fileSize ? {
31
- type,
32
- range: ByteRange.of(offset, size)
33
- } : void 0;
34
- }
35
- /**
36
- * Undefined when the header declares a large size that is not a safe integer: real boxes never
37
- * do, so those bytes are trailer data, not a box.
38
- */
39
- function resolveSize(header, offset, fileSize) {
40
- const declared = header.uint32BeAt(0);
41
- if (declared === 1 && header.length >= 16) return largeSize(header);
42
- return {
43
- size: declared === 0 ? fileSize - offset : declared,
44
- headerSize: 8
25
+ const box = boxHeaderOf(header, room);
26
+ return box && {
27
+ type: box.type,
28
+ range: ByteRange.of(offset, box.size)
45
29
  };
46
30
  }
47
- function largeSize(header) {
48
- try {
49
- return {
50
- size: header.uint64BeAt(8),
51
- headerSize: 16
52
- };
53
- } catch (error) {
54
- if (hasErrorCode(error, "binary-unsafe-integer")) return void 0;
55
- throw error;
56
- }
57
- }
58
31
  //#endregion
59
32
  export { scanBoxes };
@@ -0,0 +1,81 @@
1
+ import { AVC_SAMPLE_ENTRIES, HEVC_SAMPLE_ENTRIES } from "./mp4BoxTypes.js";
2
+ import { EVERY_SAMPLE_IS_A_KEYFRAME } from "../../container/KeyframeRule.js";
3
+ //#region ../../packages/core/src/domain/format/mp4/keyframeRules.ts
4
+ /**
5
+ * ITU-T H.264 Table 7-1: nal_unit_type is the low five bits of the header byte; types 1 to 4 are
6
+ * slices of a picture that is not an IDR picture, type 5 a slice of one.
7
+ */
8
+ var AVC_TYPE_MASK = 31;
9
+ var AVC_FIRST_NON_IDR_SLICE = 1;
10
+ var AVC_LAST_SLICE_PARTITION = 4;
11
+ var AVC_IDR_SLICE = 5;
12
+ /**
13
+ * ITU-T H.265 Table 7-1: nal_unit_type is six bits after the header's first bit; types 0 to 15
14
+ * are pictures that are no intra random access point, 16 (BLA_W_LP) to 23 (RSV_IRAP_VCL23) ones
15
+ * that are, and the rest (reserved, parameter sets, SEI) say nothing of the picture.
16
+ */
17
+ var HEVC_TYPE_SHIFT = 1;
18
+ var HEVC_TYPE_MASK = 63;
19
+ var HEVC_FIRST_IRAP = 16;
20
+ var HEVC_LAST_IRAP = 23;
21
+ /**
22
+ * Only an IDR slice starts decoding. mediabunny also takes a recovery-point SEI outside
23
+ * Chromium, where decoding from it may show broken frames first; the player never starts there.
24
+ */
25
+ var AVC = {
26
+ typeOf: (header) => header & AVC_TYPE_MASK,
27
+ verdictOn: (type) => {
28
+ if (type === AVC_IDR_SLICE) return "keyframe";
29
+ return type >= AVC_FIRST_NON_IDR_SLICE && type <= AVC_LAST_SLICE_PARTITION ? "not-a-keyframe" : "read-on";
30
+ }
31
+ };
32
+ var HEVC = {
33
+ typeOf: (header) => header >> HEVC_TYPE_SHIFT & HEVC_TYPE_MASK,
34
+ verdictOn: (type) => {
35
+ if (type < HEVC_FIRST_IRAP) return "not-a-keyframe";
36
+ return type <= HEVC_LAST_IRAP ? "keyframe" : "read-on";
37
+ }
38
+ };
39
+ var BITS_PER_BYTE = 8;
40
+ /**
41
+ * Reads a sample's NAL units one after another, each after its length, until one tells whether
42
+ * the picture is a keyframe; a sample that never tells, or whose units run past its end, is not
43
+ * one.
44
+ */
45
+ var NalKeyframeRule = class {
46
+ family;
47
+ lengthSize;
48
+ constructor(family, lengthSize) {
49
+ this.family = family;
50
+ this.lengthSize = lengthSize;
51
+ }
52
+ isKeyframe(sample) {
53
+ let offset = 0;
54
+ while (offset + this.lengthSize < sample.length) {
55
+ const unitStart = offset + this.lengthSize;
56
+ const unitEnd = unitStart + lengthAt(sample, offset, this.lengthSize);
57
+ if (unitEnd > sample.length) return false;
58
+ const verdict = this.family.verdictOn(this.family.typeOf(sample[unitStart] ?? 0));
59
+ if (verdict !== "read-on") return verdict === "keyframe";
60
+ offset = unitEnd;
61
+ }
62
+ return false;
63
+ }
64
+ };
65
+ function lengthAt(sample, offset, size) {
66
+ let length = 0;
67
+ for (let index = 0; index < size; index += 1) length = length * 2 ** BITS_PER_BYTE + (sample[offset + index] ?? 0);
68
+ return length;
69
+ }
70
+ var FAMILIES_BY_SAMPLE_ENTRY = [[AVC_SAMPLE_ENTRIES, AVC], [HEVC_SAMPLE_ENTRIES, HEVC]];
71
+ /**
72
+ * The rule for a track's codec, by its sample entry. A codec without NAL units, or whose length
73
+ * size is unknown, keeps its sample table's word.
74
+ */
75
+ function keyframeRuleFor(entry) {
76
+ const { sampleEntryType, nalLengthSize } = entry;
77
+ const [, family] = FAMILIES_BY_SAMPLE_ENTRY.find(([entries]) => entries.includes(sampleEntryType)) ?? [];
78
+ return family && nalLengthSize !== void 0 ? new NalKeyframeRule(family, nalLengthSize) : EVERY_SAMPLE_IS_A_KEYFRAME;
79
+ }
80
+ //#endregion
81
+ export { keyframeRuleFor };
@@ -0,0 +1,52 @@
1
+ import { GyroViewError } from "../../../shared/errors/GyroViewError.js";
2
+ import { ByteReader } from "../../../shared/binary/ByteReader.js";
3
+ import { boxHeaderOf } from "../boxes/boxHeader.js";
4
+ //#region ../../packages/core/src/domain/format/mp4/movieBoxes.ts
5
+ /**
6
+ * The boxes `bytes` holds one after another: the children of a container box. Fewer bytes than
7
+ * a header at the end are padding, which some muxers leave; any other bytes that form no box are
8
+ * a movie box the player cannot read.
9
+ */
10
+ function boxesIn(bytes) {
11
+ const boxes = [];
12
+ let offset = 0;
13
+ while (bytes.byteLength - offset >= 8) {
14
+ const room = bytes.byteLength - offset;
15
+ const headerBytes = bytes.subarray(offset, offset + 16);
16
+ const header = boxHeaderOf(new ByteReader(headerBytes), room);
17
+ if (!header) throw unreadableMovie(`holds no box at byte ${offset} of ${bytes.byteLength}`);
18
+ const body = bytes.subarray(offset + header.headerSize, offset + header.size);
19
+ boxes.push({
20
+ type: header.type,
21
+ body
22
+ });
23
+ offset += header.size;
24
+ }
25
+ return boxes;
26
+ }
27
+ /**
28
+ * The first box of a type the movie box cannot do without.
29
+ */
30
+ function requiredBox(boxes, type) {
31
+ const box = optionalBox(boxes, type);
32
+ if (!box) throw unreadableMovie(`has no ${type} box where one belongs`);
33
+ return box;
34
+ }
35
+ function optionalBox(boxes, type) {
36
+ return boxes.find((box) => box.type === type);
37
+ }
38
+ function fullBoxOf(box) {
39
+ if (box.body.byteLength < 4) throw unreadableMovie(`has a ${box.type} box too short for its version and flags`);
40
+ return {
41
+ version: box.body[0] ?? 0,
42
+ content: new ByteReader(box.body.subarray(4))
43
+ };
44
+ }
45
+ /**
46
+ * A movie box whose bytes do not describe the samples as ISO/IEC 14496-12 lays them out.
47
+ */
48
+ function unreadableMovie(what) {
49
+ return new GyroViewError("unsupported-container", `the movie box ${what}`);
50
+ }
51
+ //#endregion
52
+ export { boxesIn, fullBoxOf, optionalBox, requiredBox, unreadableMovie };
@@ -0,0 +1,46 @@
1
+ //#region ../../packages/core/src/domain/format/mp4/mp4BoxTypes.ts
2
+ /**
3
+ * The MP4 boxes the player reads or refuses, by the names ISO/IEC 14496-12 gives them.
4
+ */
5
+ var Mp4BoxType = {
6
+ FileType: "ftyp",
7
+ MediaData: "mdat",
8
+ Movie: "moov",
9
+ MovieHeader: "mvhd",
10
+ MovieExtends: "mvex",
11
+ Track: "trak",
12
+ TrackHeader: "tkhd",
13
+ Edit: "edts",
14
+ EditList: "elst",
15
+ Media: "mdia",
16
+ MediaHeader: "mdhd",
17
+ Handler: "hdlr",
18
+ MediaInformation: "minf",
19
+ SampleTable: "stbl",
20
+ SampleDescription: "stsd",
21
+ TimeToSample: "stts",
22
+ CompositionOffset: "ctts",
23
+ SyncSample: "stss",
24
+ SampleToChunk: "stsc",
25
+ SampleSize: "stsz",
26
+ ChunkOffset: "stco",
27
+ ChunkLargeOffset: "co64",
28
+ AvcConfiguration: "avcC",
29
+ HevcConfiguration: "hvcC"
30
+ };
31
+ /**
32
+ * A track's handler type (ISO/IEC 14496-12 §8.4.3): what its samples are. Tracks of any other
33
+ * handler (timed metadata, hints) are not played.
34
+ */
35
+ var HandlerType = {
36
+ Video: "vide",
37
+ Sound: "soun"
38
+ };
39
+ /**
40
+ * The sample entries of the codecs the cameras record, by family: the parameter sets travel in
41
+ * the sample entry (`avc1`, `hvc1`) or in the stream as well (`avc3`, `hev1`); ISO/IEC 14496-15.
42
+ */
43
+ var AVC_SAMPLE_ENTRIES = ["avc1", "avc3"];
44
+ var HEVC_SAMPLE_ENTRIES = ["hvc1", "hev1"];
45
+ //#endregion
46
+ export { AVC_SAMPLE_ENTRIES, HEVC_SAMPLE_ENTRIES, HandlerType, Mp4BoxType };
@@ -0,0 +1,183 @@
1
+ //#region ../../packages/core/src/domain/format/mp4/mp4Layouts.ts
2
+ /**
3
+ * MovieHeaderBox, `mvhd` (§8.2.2): creation and modification times, the movie timescale (in which
4
+ * edit lists count), the duration, then fields the player does not read.
5
+ */
6
+ var MOVIE_HEADER = {
7
+ 0: {
8
+ timescale: 8,
9
+ duration: {
10
+ offset: 12,
11
+ kind: "uint32"
12
+ },
13
+ size: 96
14
+ },
15
+ 1: {
16
+ timescale: 16,
17
+ duration: {
18
+ offset: 20,
19
+ kind: "uint64"
20
+ },
21
+ size: 108
22
+ }
23
+ };
24
+ /**
25
+ * TrackHeaderBox, `tkhd` (§8.3.2): the track's id sits after the creation and modification times.
26
+ */
27
+ var TRACK_HEADER_ID = {
28
+ 0: 8,
29
+ 1: 16
30
+ };
31
+ /**
32
+ * MediaHeaderBox, `mdhd` (§8.4.2): the timescale the track's sample tables count in.
33
+ */
34
+ var MEDIA_HEADER = {
35
+ 0: {
36
+ timescale: 8,
37
+ duration: {
38
+ offset: 12,
39
+ kind: "uint32"
40
+ },
41
+ size: 20
42
+ },
43
+ 1: {
44
+ timescale: 16,
45
+ duration: {
46
+ offset: 20,
47
+ kind: "uint64"
48
+ },
49
+ size: 32
50
+ }
51
+ };
52
+ /**
53
+ * TimeToSampleBox, `stts` (§8.6.1.2): runs of samples with the same decode duration.
54
+ */
55
+ var TIME_TO_SAMPLE = {
56
+ entrySize: 8,
57
+ fields: {
58
+ sampleCount: {
59
+ offset: 0,
60
+ kind: "uint32"
61
+ },
62
+ sampleDelta: {
63
+ offset: 4,
64
+ kind: "uint32"
65
+ }
66
+ }
67
+ };
68
+ /**
69
+ * CompositionOffsetBox, `ctts` (§8.6.1.3): runs of samples presented the same time after (and in
70
+ * version 1, possibly before) they are decoded. Version 0 is unsigned by the standard, but
71
+ * writers put negative offsets there too, so both versions are read signed, as mediabunny and
72
+ * FFmpeg read them.
73
+ */
74
+ var COMPOSITION_OFFSET = {
75
+ entrySize: 8,
76
+ fields: {
77
+ sampleCount: {
78
+ offset: 0,
79
+ kind: "uint32"
80
+ },
81
+ sampleOffset: {
82
+ offset: 4,
83
+ kind: "int32"
84
+ }
85
+ }
86
+ };
87
+ /**
88
+ * SyncSampleBox, `stss` (§8.6.2): the 1-based numbers of the samples decoding may start at. A
89
+ * track without one starts anywhere.
90
+ */
91
+ var SYNC_SAMPLE = {
92
+ entrySize: 4,
93
+ fields: { sampleNumber: {
94
+ offset: 0,
95
+ kind: "uint32"
96
+ } }
97
+ };
98
+ /**
99
+ * EditListBox, `elst` (§8.6.6): segments of the movie timeline, each showing the media from
100
+ * `mediaTime` (media timescale) for `segmentDuration` (movie timescale), or nothing where
101
+ * `mediaTime` is -1.
102
+ */
103
+ var EDIT_LIST = {
104
+ 0: {
105
+ entrySize: 12,
106
+ fields: {
107
+ segmentDuration: {
108
+ offset: 0,
109
+ kind: "uint32"
110
+ },
111
+ mediaTime: {
112
+ offset: 4,
113
+ kind: "int32"
114
+ },
115
+ mediaRate: {
116
+ offset: 8,
117
+ kind: "uint32"
118
+ }
119
+ }
120
+ },
121
+ 1: {
122
+ entrySize: 20,
123
+ fields: {
124
+ segmentDuration: {
125
+ offset: 0,
126
+ kind: "uint64"
127
+ },
128
+ mediaTime: {
129
+ offset: 8,
130
+ kind: "int64"
131
+ },
132
+ mediaRate: {
133
+ offset: 16,
134
+ kind: "uint32"
135
+ }
136
+ }
137
+ }
138
+ };
139
+ /**
140
+ * An edit's media rate is 16.16 fixed point; 1 plays the media at its own pace.
141
+ */
142
+ var UNIT_MEDIA_RATE = 65536;
143
+ /**
144
+ * SampleToChunkBox, `stsc` (§8.7.4): from each 1-based first chunk on, how many samples a chunk
145
+ * holds, until the next entry's first chunk.
146
+ */
147
+ var SAMPLE_TO_CHUNK = {
148
+ entrySize: 12,
149
+ fields: {
150
+ firstChunk: {
151
+ offset: 0,
152
+ kind: "uint32"
153
+ },
154
+ samplesPerChunk: {
155
+ offset: 4,
156
+ kind: "uint32"
157
+ },
158
+ sampleDescriptionIndex: {
159
+ offset: 8,
160
+ kind: "uint32"
161
+ }
162
+ }
163
+ };
164
+ /**
165
+ * ChunkOffsetBox, `stco`, and ChunkLargeOffsetBox, `co64` (§8.7.5): where each chunk starts in the
166
+ * file, in 32 or 64 bits.
167
+ */
168
+ var CHUNK_OFFSET = {
169
+ entrySize: 4,
170
+ fields: { chunkOffset: {
171
+ offset: 0,
172
+ kind: "uint32"
173
+ } }
174
+ };
175
+ var CHUNK_LARGE_OFFSET = {
176
+ entrySize: 8,
177
+ fields: { chunkOffset: {
178
+ offset: 0,
179
+ kind: "uint64"
180
+ } }
181
+ };
182
+ //#endregion
183
+ export { CHUNK_LARGE_OFFSET, CHUNK_OFFSET, COMPOSITION_OFFSET, EDIT_LIST, MEDIA_HEADER, MOVIE_HEADER, SAMPLE_TO_CHUNK, SYNC_SAMPLE, TIME_TO_SAMPLE, TRACK_HEADER_ID, UNIT_MEDIA_RATE };
@@ -0,0 +1,67 @@
1
+ import { HandlerType, Mp4BoxType } from "./mp4BoxTypes.js";
2
+ import { keyframeRuleFor } from "./keyframeRules.js";
3
+ import { boxesIn, fullBoxOf, optionalBox, unreadableMovie } from "./movieBoxes.js";
4
+ import { SYNC_SAMPLE } from "./mp4Layouts.js";
5
+ import { readTableColumns } from "./readTable.js";
6
+ import { sampleLocationsOf } from "./sampleLocations.js";
7
+ import { movieTimescaleOf, trackBoxesOf, trackHeadersOf } from "./trackHeaders.js";
8
+ import { sampleTimingOf } from "./sampleTiming.js";
9
+ import { SampleTable } from "../../container/SampleTable.js";
10
+ import { TrackSampleTable } from "../../container/TrackSampleTable.js";
11
+ //#region ../../packages/core/src/domain/format/mp4/parseMovie.ts
12
+ /**
13
+ * The tracks the player plays, by handler; the others (time codes, metadata) are left out.
14
+ */
15
+ var KIND_OF_HANDLER = {
16
+ [HandlerType.Video]: "video",
17
+ [HandlerType.Sound]: "audio"
18
+ };
19
+ /**
20
+ * Sample numbers in the sync sample table count from one.
21
+ */
22
+ var FIRST_SAMPLE_NUMBER = 1;
23
+ /**
24
+ * The sample table of a whole movie box (`moov`, header included): every sample of every track
25
+ * of picture or sound. A fragmented movie, whose samples are described in movie fragments after
26
+ * it, is refused: the cameras write none.
27
+ */
28
+ function parseMovie(movieBox) {
29
+ const [movie] = boxesIn(movieBox);
30
+ if (movie?.type !== Mp4BoxType.Movie) throw unreadableMovie("is not where the file says it is");
31
+ const children = boxesIn(movie.body);
32
+ if (optionalBox(children, Mp4BoxType.MovieExtends)) throw unreadableMovie("is fragmented: its samples are described in movie fragments after it");
33
+ const movieTimescale = movieTimescaleOf(children);
34
+ const tracks = children.filter((box) => box.type === Mp4BoxType.Track).flatMap((box) => playedTrackOf(trackBoxesOf(box), movieTimescale));
35
+ return new SampleTable(tracks);
36
+ }
37
+ function playedTrackOf(boxes, movieTimescale) {
38
+ const headers = trackHeadersOf(boxes);
39
+ const kind = KIND_OF_HANDLER[headers.handlerType];
40
+ if (!kind) return [];
41
+ const locations = sampleLocationsOf(boxes.sampleTable);
42
+ const timing = sampleTimingOf(boxes, {
43
+ media: headers.mediaTimescale,
44
+ movie: movieTimescale,
45
+ sampleCount: locations.sizes.length
46
+ });
47
+ return [new TrackSampleTable({
48
+ trackId: headers.trackId,
49
+ kind,
50
+ ...locations,
51
+ ...timing,
52
+ syncSamples: syncSamplesOf(boxes.sampleTable),
53
+ keyframeRule: keyframeRuleFor(headers)
54
+ })];
55
+ }
56
+ /**
57
+ * The samples decoding may start at, counted from zero; undefined for a track without a sync
58
+ * sample table, whose every sample is one.
59
+ */
60
+ function syncSamplesOf(sampleTable) {
61
+ const box = optionalBox(sampleTable, Mp4BoxType.SyncSample);
62
+ if (!box) return void 0;
63
+ const { sampleNumber } = readTableColumns(fullBoxOf(box).content, SYNC_SAMPLE);
64
+ return sampleNumber.map((number) => number - FIRST_SAMPLE_NUMBER);
65
+ }
66
+ //#endregion
67
+ export { parseMovie };
@@ -0,0 +1,31 @@
1
+ import { unreadableMovie } from "./movieBoxes.js";
2
+ //#region ../../packages/core/src/domain/format/mp4/readTable.ts
3
+ /**
4
+ * How each kind of integer in a box layout is read, big-endian.
5
+ */
6
+ var READERS = {
7
+ uint32: (reader, offset) => reader.uint32BeAt(offset),
8
+ uint64: (reader, offset) => reader.uint64BeAt(offset),
9
+ int32: (reader, offset) => reader.int32BeAt(offset),
10
+ int64: (reader, offset) => reader.int64BeAt(offset)
11
+ };
12
+ /**
13
+ * The integer a layout puts at `field`, counted from `base`.
14
+ */
15
+ function readInteger(reader, field, base = 0) {
16
+ return READERS[field.kind](reader, base + field.offset);
17
+ }
18
+ /**
19
+ * A table box's entries, one column per field, read from its content after the version and
20
+ * flags.
21
+ */
22
+ function readTableColumns(content, layout) {
23
+ const count = content.uint32BeAt(0);
24
+ if (4 + count * layout.entrySize > content.length) throw unreadableMovie(`has a table of ${count} entries that do not fit in its box`);
25
+ const fields = Object.keys(layout.fields);
26
+ const columns = {};
27
+ for (const field of fields) columns[field] = Array.from({ length: count }, (_, index) => readInteger(content, layout.fields[field], 4 + index * layout.entrySize));
28
+ return columns;
29
+ }
30
+ //#endregion
31
+ export { readTableColumns };
@@ -0,0 +1,69 @@
1
+ import { Mp4BoxType } from "./mp4BoxTypes.js";
2
+ import { fullBoxOf, optionalBox, requiredBox, unreadableMovie } from "./movieBoxes.js";
3
+ import { CHUNK_LARGE_OFFSET, CHUNK_OFFSET, SAMPLE_TO_CHUNK } from "./mp4Layouts.js";
4
+ import { readTableColumns } from "./readTable.js";
5
+ //#region ../../packages/core/src/domain/format/mp4/sampleLocations.ts
6
+ /**
7
+ * Chunk numbers in the sample-to-chunk table count from one.
8
+ */
9
+ var FIRST_CHUNK_NUMBER = 1;
10
+ /**
11
+ * The sample locations a sample table's boxes describe: the sizes of `stsz`, laid out one after
12
+ * another in the chunks `stsc` fills, which start where `stco` or `co64` says.
13
+ */
14
+ function sampleLocationsOf(sampleTable) {
15
+ const sizes = sampleSizesOf(requiredBox(sampleTable, Mp4BoxType.SampleSize));
16
+ const chunkOffsets = chunkOffsetsOf(sampleTable);
17
+ const sampleToChunk = fullBoxOf(requiredBox(sampleTable, Mp4BoxType.SampleToChunk)).content;
18
+ return {
19
+ offsets: offsetsOf({
20
+ sizes,
21
+ chunkOffsets,
22
+ perChunk: samplesPerChunkOf(readTableColumns(sampleToChunk, SAMPLE_TO_CHUNK), chunkOffsets.length)
23
+ }),
24
+ sizes
25
+ };
26
+ }
27
+ function sampleSizesOf(box) {
28
+ const { content } = fullBoxOf(box);
29
+ const shared = content.uint32BeAt(0);
30
+ const count = content.uint32BeAt(4);
31
+ if (shared !== 0) return new Float64Array(count).fill(shared);
32
+ if (8 + count * 4 > content.length) throw unreadableMovie(`lists ${count} sample sizes that do not fit in its box`);
33
+ return Float64Array.from({ length: count }, (_, index) => content.uint32BeAt(8 + index * 4));
34
+ }
35
+ function chunkOffsetsOf(sampleTable) {
36
+ const large = optionalBox(sampleTable, Mp4BoxType.ChunkLargeOffset);
37
+ const box = large ?? optionalBox(sampleTable, Mp4BoxType.ChunkOffset);
38
+ if (!box) throw unreadableMovie("has a sample table without chunk offsets");
39
+ const layout = large ? CHUNK_LARGE_OFFSET : CHUNK_OFFSET;
40
+ return readTableColumns(fullBoxOf(box).content, layout).chunkOffset;
41
+ }
42
+ /**
43
+ * How many samples each chunk holds: every entry holds from its first chunk to the next entry's.
44
+ */
45
+ function samplesPerChunkOf(table, chunkCount) {
46
+ const perChunk = Array.from({ length: chunkCount }, () => 0);
47
+ for (const [entry, firstChunk] of table.firstChunk.entries()) {
48
+ const nextFirst = table.firstChunk[entry + 1] ?? chunkCount + FIRST_CHUNK_NUMBER;
49
+ const count = table.samplesPerChunk[entry] ?? 0;
50
+ perChunk.fill(count, firstChunk - FIRST_CHUNK_NUMBER, nextFirst - FIRST_CHUNK_NUMBER);
51
+ }
52
+ return perChunk;
53
+ }
54
+ function offsetsOf(layout) {
55
+ const offsets = new Float64Array(layout.sizes.length);
56
+ let sample = 0;
57
+ for (const [chunk, chunkOffset] of layout.chunkOffsets.entries()) {
58
+ let offset = chunkOffset;
59
+ const last = Math.min(sample + (layout.perChunk[chunk] ?? 0), offsets.length);
60
+ for (; sample < last; sample += 1) {
61
+ offsets[sample] = offset;
62
+ offset += layout.sizes[sample] ?? 0;
63
+ }
64
+ }
65
+ if (sample < offsets.length) throw unreadableMovie(`has chunks for ${sample} of its ${offsets.length} samples`);
66
+ return offsets;
67
+ }
68
+ //#endregion
69
+ export { sampleLocationsOf };