@file-viewer/renderer-media 2.2.8 → 2.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.
- package/README.en.md +3 -0
- package/README.md +3 -0
- package/dist/mp4.d.ts +35 -0
- package/dist/mp4.js +447 -0
- package/dist/mp4v.worker.d.ts +1 -0
- package/dist/mp4v.worker.js +228 -0
- package/dist/mp4vPlayer.d.ts +6 -0
- package/dist/mp4vPlayer.js +226 -0
- package/dist/vendor/mp4v/AOSP-NOTICE.txt +189 -0
- package/dist/vendor/mp4v/EMSCRIPTEN-LICENSE.txt +101 -0
- package/dist/vendor/mp4v/mp4v-decoder.d.mts +20 -0
- package/dist/vendor/mp4v/mp4v-decoder.mjs +2 -0
- package/dist/vendor/mp4v/mp4v-decoder.wasm +0 -0
- package/dist/video.js +128 -6
- package/package.json +8 -4
package/README.en.md
CHANGED
|
@@ -27,6 +27,9 @@ const options = {
|
|
|
27
27
|
## Features
|
|
28
28
|
|
|
29
29
|
- MP4, WebM, and common audio formats use native browser `<video>` / `<audio>` controls first.
|
|
30
|
+
- The renderer inspects MP4 video tracks before playback. If the browser rejects an MPEG-4 Part 2 (`mp4v`) Simple Profile track, it starts a dedicated Worker, uses the AAC track as the playback clock, and draws software-decoded I420 frames to a Canvas. This fixes the case where audio plays over a black frame.
|
|
31
|
+
- The fallback uses the Apache-2.0 AOSP PacketVideo decoder. Its WASM file is 111,379 bytes, or 34,410 bytes with gzip, and loads only after native decoding fails. The implementation contains no FFmpeg, libav, or LGPL/GPL/AGPL source.
|
|
32
|
+
- Files outside the decoder's current coverage, and browsers without Worker, OffscreenCanvas, or VideoFrame support, get a codec compatibility notice. H.264/AVC still uses less CPU and works in more browsers.
|
|
30
33
|
- HLS `.m3u8` uses native playback when available and dynamically imports `hls.js` only as a fallback.
|
|
31
34
|
- MIDI / MID dynamically imports `@tonejs/midi` only when opened, then renders name, duration, PPQ, tracks, and note summaries.
|
|
32
35
|
- Unmount cleanup revokes object URLs, resets media elements, and destroys the HLS instance for long-lived business applications.
|
package/README.md
CHANGED
|
@@ -27,6 +27,9 @@ const options = {
|
|
|
27
27
|
## 能力
|
|
28
28
|
|
|
29
29
|
- MP4、WebM 和常见音频格式优先使用浏览器原生 `<video>` / `<audio>` 控件。
|
|
30
|
+
- MP4 会读取视频轨的编码标识。浏览器无法解码 MPEG-4 Part 2(`mp4v`)Simple Profile 时,renderer 会按需启动独立 Worker,使用 AAC 音轨校时,并把软件解码的 I420 画面绘制到 Canvas,不再出现“有声音、画面全黑”。
|
|
31
|
+
- MP4V 后备解码器来自 AOSP PacketVideo,许可证为 Apache-2.0。WASM 为 111,379 字节(gzip 34,410 字节),仅在原生解码失败时加载。实现不包含 FFmpeg、libav 或 LGPL/GPL/AGPL 源码。
|
|
32
|
+
- 浏览器缺少 Worker、OffscreenCanvas 或 VideoFrame,或者文件超出当前解码范围时,会显示明确的兼容提示。面向更多浏览器发布时,H.264/AVC 仍是更省 CPU 的选择。
|
|
30
33
|
- HLS `.m3u8` 优先使用浏览器原生能力,不支持时才按需加载 `hls.js`。
|
|
31
34
|
- MIDI / MID 只在命中格式时按需加载 `@tonejs/midi`,展示曲名、时长、PPQ、轨道和音符摘要。
|
|
32
35
|
- 所有资源在卸载时清理对象 URL、播放器状态和 HLS 实例,适合长时间运行的后台系统。
|
package/dist/mp4.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface Mp4VideoTrackInfo {
|
|
2
|
+
codec: string;
|
|
3
|
+
label: string;
|
|
4
|
+
contentType: string;
|
|
5
|
+
}
|
|
6
|
+
export interface Mp4VideoSample {
|
|
7
|
+
offset: number;
|
|
8
|
+
size: number;
|
|
9
|
+
dts: number;
|
|
10
|
+
cts: number;
|
|
11
|
+
duration: number;
|
|
12
|
+
isSync: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface Mp4vSoftwareTrack extends Mp4VideoTrackInfo {
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
timescale: number;
|
|
18
|
+
duration: number;
|
|
19
|
+
decoderConfig: Uint8Array;
|
|
20
|
+
samples: Mp4VideoSample[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Reads the declared video sample entry from an ISO BMFF/MP4 file.
|
|
24
|
+
*
|
|
25
|
+
* Browsers can accept the generic `video/mp4` container while silently
|
|
26
|
+
* ignoring an unsupported video track. Inspecting `stsd` lets the renderer
|
|
27
|
+
* distinguish that audio-only success from a genuinely decodable video.
|
|
28
|
+
*/
|
|
29
|
+
export declare const inspectMp4VideoTrack: (buffer: ArrayBuffer) => Mp4VideoTrackInfo | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Extracts the minimal MP4V decode timeline needed by the Apache-2.0
|
|
32
|
+
* PacketVideo software decoder. This intentionally avoids a general-purpose
|
|
33
|
+
* media framework so the fallback stays small and offline-friendly.
|
|
34
|
+
*/
|
|
35
|
+
export declare const extractMp4vSoftwareTrack: (buffer: ArrayBuffer) => Mp4vSoftwareTrack | undefined;
|
package/dist/mp4.js
ADDED
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
const MAX_MP4_VIDEO_SAMPLES = 1000000;
|
|
2
|
+
const MAX_MP4V_DIMENSION = 8192;
|
|
3
|
+
const MAX_MP4V_PIXELS = 35000000;
|
|
4
|
+
const MAX_MP4V_DECODER_CONFIG_BYTES = 64 * 1024;
|
|
5
|
+
const MAX_MP4V_SAMPLE_BYTES = 64 * 1024 * 1024;
|
|
6
|
+
const VIDEO_CODEC_LABELS = {
|
|
7
|
+
av01: 'AV1 (av01)',
|
|
8
|
+
avc1: 'H.264/AVC (avc1)',
|
|
9
|
+
avc3: 'H.264/AVC (avc3)',
|
|
10
|
+
hev1: 'HEVC (hev1)',
|
|
11
|
+
hvc1: 'HEVC (hvc1)',
|
|
12
|
+
mp4v: 'MPEG-4 Part 2 (mp4v)',
|
|
13
|
+
vp09: 'VP9 (vp09)'
|
|
14
|
+
};
|
|
15
|
+
const readFourCc = (view, offset) => {
|
|
16
|
+
if (offset < 0 || offset + 4 > view.byteLength)
|
|
17
|
+
return '';
|
|
18
|
+
return String.fromCharCode(view.getUint8(offset), view.getUint8(offset + 1), view.getUint8(offset + 2), view.getUint8(offset + 3));
|
|
19
|
+
};
|
|
20
|
+
const readBox = (view, offset, limit) => {
|
|
21
|
+
if (offset < 0 || offset + 8 > limit || limit > view.byteLength)
|
|
22
|
+
return undefined;
|
|
23
|
+
const size32 = view.getUint32(offset);
|
|
24
|
+
const type = readFourCc(view, offset + 4);
|
|
25
|
+
let headerSize = 8;
|
|
26
|
+
let size = size32;
|
|
27
|
+
if (size32 === 1) {
|
|
28
|
+
if (offset + 16 > limit)
|
|
29
|
+
return undefined;
|
|
30
|
+
const high = view.getUint32(offset + 8);
|
|
31
|
+
const low = view.getUint32(offset + 12);
|
|
32
|
+
size = high * 0x100000000 + low;
|
|
33
|
+
headerSize = 16;
|
|
34
|
+
}
|
|
35
|
+
else if (size32 === 0) {
|
|
36
|
+
size = limit - offset;
|
|
37
|
+
}
|
|
38
|
+
if (!type || !Number.isSafeInteger(size) || size < headerSize || offset + size > limit) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
type,
|
|
43
|
+
start: offset,
|
|
44
|
+
payloadStart: offset + headerSize,
|
|
45
|
+
end: offset + size
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
const readChildren = (view, start, end) => {
|
|
49
|
+
const boxes = [];
|
|
50
|
+
let offset = start;
|
|
51
|
+
while (offset + 8 <= end) {
|
|
52
|
+
const box = readBox(view, offset, end);
|
|
53
|
+
if (!box)
|
|
54
|
+
break;
|
|
55
|
+
boxes.push(box);
|
|
56
|
+
if (box.end <= offset)
|
|
57
|
+
break;
|
|
58
|
+
offset = box.end;
|
|
59
|
+
}
|
|
60
|
+
return boxes;
|
|
61
|
+
};
|
|
62
|
+
const findChild = (view, parent, type) => readChildren(view, parent.payloadStart, parent.end).find(box => box.type === type);
|
|
63
|
+
const readUint64 = (view, offset) => {
|
|
64
|
+
const high = view.getUint32(offset);
|
|
65
|
+
const low = view.getUint32(offset + 4);
|
|
66
|
+
const value = high * 0x100000000 + low;
|
|
67
|
+
return Number.isSafeInteger(value) ? value : 0;
|
|
68
|
+
};
|
|
69
|
+
const getHandlerType = (view, mediaBox) => {
|
|
70
|
+
const handlerBox = findChild(view, mediaBox, 'hdlr');
|
|
71
|
+
if (!handlerBox || handlerBox.payloadStart + 12 > handlerBox.end)
|
|
72
|
+
return '';
|
|
73
|
+
return readFourCc(view, handlerBox.payloadStart + 8);
|
|
74
|
+
};
|
|
75
|
+
const getVideoSampleEntry = (view, mediaBox) => {
|
|
76
|
+
const mediaInformationBox = findChild(view, mediaBox, 'minf');
|
|
77
|
+
const sampleTableBox = mediaInformationBox
|
|
78
|
+
? findChild(view, mediaInformationBox, 'stbl')
|
|
79
|
+
: undefined;
|
|
80
|
+
const sampleDescriptionBox = sampleTableBox
|
|
81
|
+
? findChild(view, sampleTableBox, 'stsd')
|
|
82
|
+
: undefined;
|
|
83
|
+
if (!sampleDescriptionBox || sampleDescriptionBox.payloadStart + 8 > sampleDescriptionBox.end) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
const entryCount = view.getUint32(sampleDescriptionBox.payloadStart + 4);
|
|
87
|
+
const offset = sampleDescriptionBox.payloadStart + 8;
|
|
88
|
+
for (let index = 0; index < entryCount && offset + 8 <= sampleDescriptionBox.end; index += 1) {
|
|
89
|
+
const entry = readBox(view, offset, sampleDescriptionBox.end);
|
|
90
|
+
if (!entry)
|
|
91
|
+
break;
|
|
92
|
+
return entry.type;
|
|
93
|
+
}
|
|
94
|
+
return undefined;
|
|
95
|
+
};
|
|
96
|
+
const getSampleDescription = (view, mediaBox) => {
|
|
97
|
+
const mediaInformationBox = findChild(view, mediaBox, 'minf');
|
|
98
|
+
const sampleTableBox = mediaInformationBox
|
|
99
|
+
? findChild(view, mediaInformationBox, 'stbl')
|
|
100
|
+
: undefined;
|
|
101
|
+
const sampleDescriptionBox = sampleTableBox
|
|
102
|
+
? findChild(view, sampleTableBox, 'stsd')
|
|
103
|
+
: undefined;
|
|
104
|
+
if (!sampleDescriptionBox || sampleDescriptionBox.payloadStart + 16 > sampleDescriptionBox.end) {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
return readBox(view, sampleDescriptionBox.payloadStart + 8, sampleDescriptionBox.end);
|
|
108
|
+
};
|
|
109
|
+
const readMpeg4Descriptor = (view, offset, limit) => {
|
|
110
|
+
if (offset + 2 > limit)
|
|
111
|
+
return undefined;
|
|
112
|
+
const tag = view.getUint8(offset);
|
|
113
|
+
let cursor = offset + 1;
|
|
114
|
+
let length = 0;
|
|
115
|
+
for (let index = 0; index < 4 && cursor < limit; index += 1) {
|
|
116
|
+
const value = view.getUint8(cursor);
|
|
117
|
+
cursor += 1;
|
|
118
|
+
length = (length << 7) | (value & 0x7f);
|
|
119
|
+
if ((value & 0x80) === 0) {
|
|
120
|
+
const end = cursor + length;
|
|
121
|
+
if (end > limit)
|
|
122
|
+
return undefined;
|
|
123
|
+
return { tag, payloadStart: cursor, end };
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
};
|
|
128
|
+
const readDecoderSpecificInfo = (view, sampleEntry) => {
|
|
129
|
+
const visualSampleEntryHeaderSize = 78;
|
|
130
|
+
if (sampleEntry.payloadStart + visualSampleEntryHeaderSize > sampleEntry.end) {
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
const esdsBox = readChildren(view, sampleEntry.payloadStart + visualSampleEntryHeaderSize, sampleEntry.end).find(box => box.type === 'esds');
|
|
134
|
+
if (!esdsBox || esdsBox.payloadStart + 4 >= esdsBox.end)
|
|
135
|
+
return undefined;
|
|
136
|
+
const esDescriptor = readMpeg4Descriptor(view, esdsBox.payloadStart + 4, esdsBox.end);
|
|
137
|
+
if (!esDescriptor || esDescriptor.tag !== 0x03 || esDescriptor.payloadStart + 3 > esDescriptor.end) {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
const flags = view.getUint8(esDescriptor.payloadStart + 2);
|
|
141
|
+
let cursor = esDescriptor.payloadStart + 3;
|
|
142
|
+
if (flags & 0x80)
|
|
143
|
+
cursor += 2;
|
|
144
|
+
if (flags & 0x40) {
|
|
145
|
+
if (cursor >= esDescriptor.end)
|
|
146
|
+
return undefined;
|
|
147
|
+
cursor += 1 + view.getUint8(cursor);
|
|
148
|
+
}
|
|
149
|
+
if (flags & 0x20)
|
|
150
|
+
cursor += 2;
|
|
151
|
+
const decoderConfig = readMpeg4Descriptor(view, cursor, esDescriptor.end);
|
|
152
|
+
if (!decoderConfig || decoderConfig.tag !== 0x04)
|
|
153
|
+
return undefined;
|
|
154
|
+
cursor = decoderConfig.payloadStart + 13;
|
|
155
|
+
while (cursor < decoderConfig.end) {
|
|
156
|
+
const descriptor = readMpeg4Descriptor(view, cursor, decoderConfig.end);
|
|
157
|
+
if (!descriptor)
|
|
158
|
+
return undefined;
|
|
159
|
+
if (descriptor.tag === 0x05) {
|
|
160
|
+
const configSize = descriptor.end - descriptor.payloadStart;
|
|
161
|
+
if (configSize <= 0 || configSize > MAX_MP4V_DECODER_CONFIG_BYTES) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
return new Uint8Array(view.buffer.slice(descriptor.payloadStart, descriptor.end));
|
|
165
|
+
}
|
|
166
|
+
cursor = descriptor.end;
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
};
|
|
170
|
+
const getMediaTimescaleAndDuration = (view, mediaBox) => {
|
|
171
|
+
const mediaHeader = findChild(view, mediaBox, 'mdhd');
|
|
172
|
+
if (!mediaHeader || mediaHeader.payloadStart + 20 > mediaHeader.end)
|
|
173
|
+
return undefined;
|
|
174
|
+
const version = view.getUint8(mediaHeader.payloadStart);
|
|
175
|
+
if (version === 1) {
|
|
176
|
+
if (mediaHeader.payloadStart + 32 > mediaHeader.end)
|
|
177
|
+
return undefined;
|
|
178
|
+
return {
|
|
179
|
+
timescale: view.getUint32(mediaHeader.payloadStart + 20),
|
|
180
|
+
duration: readUint64(view, mediaHeader.payloadStart + 24)
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
timescale: view.getUint32(mediaHeader.payloadStart + 12),
|
|
185
|
+
duration: view.getUint32(mediaHeader.payloadStart + 16)
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
const getSampleTable = (view, mediaBox) => {
|
|
189
|
+
const mediaInformationBox = findChild(view, mediaBox, 'minf');
|
|
190
|
+
return mediaInformationBox ? findChild(view, mediaInformationBox, 'stbl') : undefined;
|
|
191
|
+
};
|
|
192
|
+
const readTimeToSample = (view, box, expectedSamples) => {
|
|
193
|
+
if (box.payloadStart + 8 > box.end)
|
|
194
|
+
return [];
|
|
195
|
+
const count = view.getUint32(box.payloadStart + 4);
|
|
196
|
+
const durations = [];
|
|
197
|
+
let cursor = box.payloadStart + 8;
|
|
198
|
+
for (let index = 0; index < count && cursor + 8 <= box.end; index += 1) {
|
|
199
|
+
const sampleCount = view.getUint32(cursor);
|
|
200
|
+
const sampleDelta = view.getUint32(cursor + 4);
|
|
201
|
+
if (sampleCount === 0 ||
|
|
202
|
+
sampleDelta === 0 ||
|
|
203
|
+
durations.length + sampleCount > expectedSamples)
|
|
204
|
+
return [];
|
|
205
|
+
for (let sample = 0; sample < sampleCount; sample += 1)
|
|
206
|
+
durations.push(sampleDelta);
|
|
207
|
+
cursor += 8;
|
|
208
|
+
}
|
|
209
|
+
return durations.length === expectedSamples ? durations : [];
|
|
210
|
+
};
|
|
211
|
+
const readCompositionOffsets = (view, box, expectedSamples) => {
|
|
212
|
+
if (!box || box.payloadStart + 8 > box.end)
|
|
213
|
+
return [];
|
|
214
|
+
const version = view.getUint8(box.payloadStart);
|
|
215
|
+
const count = view.getUint32(box.payloadStart + 4);
|
|
216
|
+
const offsets = [];
|
|
217
|
+
let cursor = box.payloadStart + 8;
|
|
218
|
+
for (let index = 0; index < count && cursor + 8 <= box.end; index += 1) {
|
|
219
|
+
const sampleCount = view.getUint32(cursor);
|
|
220
|
+
const sampleOffset = version === 1
|
|
221
|
+
? view.getInt32(cursor + 4)
|
|
222
|
+
: view.getUint32(cursor + 4);
|
|
223
|
+
if (sampleCount === 0 || offsets.length + sampleCount > expectedSamples)
|
|
224
|
+
return [];
|
|
225
|
+
for (let sample = 0; sample < sampleCount; sample += 1)
|
|
226
|
+
offsets.push(sampleOffset);
|
|
227
|
+
cursor += 8;
|
|
228
|
+
}
|
|
229
|
+
return offsets.length === expectedSamples ? offsets : [];
|
|
230
|
+
};
|
|
231
|
+
const readSampleSizes = (view, box) => {
|
|
232
|
+
if (box.payloadStart + 12 > box.end)
|
|
233
|
+
return [];
|
|
234
|
+
const fixedSize = view.getUint32(box.payloadStart + 4);
|
|
235
|
+
const count = view.getUint32(box.payloadStart + 8);
|
|
236
|
+
if (count === 0 || count > MAX_MP4_VIDEO_SAMPLES)
|
|
237
|
+
return [];
|
|
238
|
+
if (fixedSize) {
|
|
239
|
+
if (fixedSize > MAX_MP4V_SAMPLE_BYTES)
|
|
240
|
+
return [];
|
|
241
|
+
return Array.from({ length: count }, () => fixedSize);
|
|
242
|
+
}
|
|
243
|
+
const sizes = [];
|
|
244
|
+
let cursor = box.payloadStart + 12;
|
|
245
|
+
for (let index = 0; index < count && cursor + 4 <= box.end; index += 1) {
|
|
246
|
+
const size = view.getUint32(cursor);
|
|
247
|
+
if (size === 0 || size > MAX_MP4V_SAMPLE_BYTES)
|
|
248
|
+
return [];
|
|
249
|
+
sizes.push(size);
|
|
250
|
+
cursor += 4;
|
|
251
|
+
}
|
|
252
|
+
return sizes.length === count ? sizes : [];
|
|
253
|
+
};
|
|
254
|
+
const readChunkOffsets = (view, box) => {
|
|
255
|
+
if (box.payloadStart + 8 > box.end)
|
|
256
|
+
return [];
|
|
257
|
+
const count = view.getUint32(box.payloadStart + 4);
|
|
258
|
+
const entrySize = box.type === 'co64' ? 8 : 4;
|
|
259
|
+
const offsets = [];
|
|
260
|
+
let cursor = box.payloadStart + 8;
|
|
261
|
+
for (let index = 0; index < count && cursor + entrySize <= box.end; index += 1) {
|
|
262
|
+
offsets.push(entrySize === 8 ? readUint64(view, cursor) : view.getUint32(cursor));
|
|
263
|
+
cursor += entrySize;
|
|
264
|
+
}
|
|
265
|
+
return offsets;
|
|
266
|
+
};
|
|
267
|
+
const readSampleToChunk = (view, box) => {
|
|
268
|
+
if (box.payloadStart + 8 > box.end)
|
|
269
|
+
return [];
|
|
270
|
+
const count = view.getUint32(box.payloadStart + 4);
|
|
271
|
+
const entries = [];
|
|
272
|
+
let cursor = box.payloadStart + 8;
|
|
273
|
+
for (let index = 0; index < count && cursor + 12 <= box.end; index += 1) {
|
|
274
|
+
const firstChunk = view.getUint32(cursor);
|
|
275
|
+
const samplesPerChunk = view.getUint32(cursor + 4);
|
|
276
|
+
if (firstChunk === 0 ||
|
|
277
|
+
samplesPerChunk === 0 ||
|
|
278
|
+
(entries.length === 0 && firstChunk !== 1) ||
|
|
279
|
+
(entries.length > 0 && firstChunk <= entries[entries.length - 1].firstChunk))
|
|
280
|
+
return [];
|
|
281
|
+
entries.push({ firstChunk, samplesPerChunk });
|
|
282
|
+
cursor += 12;
|
|
283
|
+
}
|
|
284
|
+
return entries;
|
|
285
|
+
};
|
|
286
|
+
const readSyncSamples = (view, box, sampleCount) => {
|
|
287
|
+
if (!box)
|
|
288
|
+
return undefined;
|
|
289
|
+
if (box.payloadStart + 8 > box.end)
|
|
290
|
+
return null;
|
|
291
|
+
const count = view.getUint32(box.payloadStart + 4);
|
|
292
|
+
if (count > sampleCount)
|
|
293
|
+
return null;
|
|
294
|
+
const samples = new Set();
|
|
295
|
+
let cursor = box.payloadStart + 8;
|
|
296
|
+
for (let index = 0; index < count && cursor + 4 <= box.end; index += 1) {
|
|
297
|
+
const sample = view.getUint32(cursor);
|
|
298
|
+
if (sample === 0 || sample > sampleCount)
|
|
299
|
+
return null;
|
|
300
|
+
samples.add(sample - 1);
|
|
301
|
+
cursor += 4;
|
|
302
|
+
}
|
|
303
|
+
return samples;
|
|
304
|
+
};
|
|
305
|
+
const buildSamples = (view, sampleTable) => {
|
|
306
|
+
const timeToSampleBox = findChild(view, sampleTable, 'stts');
|
|
307
|
+
const sampleSizeBox = findChild(view, sampleTable, 'stsz');
|
|
308
|
+
const sampleToChunkBox = findChild(view, sampleTable, 'stsc');
|
|
309
|
+
const chunkOffsetBox = findChild(view, sampleTable, 'stco') || findChild(view, sampleTable, 'co64');
|
|
310
|
+
if (!timeToSampleBox || !sampleSizeBox || !sampleToChunkBox || !chunkOffsetBox)
|
|
311
|
+
return [];
|
|
312
|
+
const sizes = readSampleSizes(view, sampleSizeBox);
|
|
313
|
+
if (!sizes.length)
|
|
314
|
+
return [];
|
|
315
|
+
const durations = readTimeToSample(view, timeToSampleBox, sizes.length);
|
|
316
|
+
if (!durations.length)
|
|
317
|
+
return [];
|
|
318
|
+
const compositionOffsets = readCompositionOffsets(view, findChild(view, sampleTable, 'ctts'), sizes.length);
|
|
319
|
+
const sampleToChunk = readSampleToChunk(view, sampleToChunkBox);
|
|
320
|
+
const chunkOffsets = readChunkOffsets(view, chunkOffsetBox);
|
|
321
|
+
const syncSamples = readSyncSamples(view, findChild(view, sampleTable, 'stss'), sizes.length);
|
|
322
|
+
if (syncSamples === null)
|
|
323
|
+
return [];
|
|
324
|
+
if (!sampleToChunk.length || !chunkOffsets.length)
|
|
325
|
+
return [];
|
|
326
|
+
const offsets = [];
|
|
327
|
+
let sampleIndex = 0;
|
|
328
|
+
let tableIndex = 0;
|
|
329
|
+
for (let chunkIndex = 1; chunkIndex <= chunkOffsets.length && sampleIndex < sizes.length; chunkIndex += 1) {
|
|
330
|
+
while (tableIndex + 1 < sampleToChunk.length &&
|
|
331
|
+
sampleToChunk[tableIndex + 1].firstChunk <= chunkIndex)
|
|
332
|
+
tableIndex += 1;
|
|
333
|
+
let offset = chunkOffsets[chunkIndex - 1];
|
|
334
|
+
for (let sampleInChunk = 0; sampleInChunk < sampleToChunk[tableIndex].samplesPerChunk && sampleIndex < sizes.length; sampleInChunk += 1) {
|
|
335
|
+
offsets.push(offset);
|
|
336
|
+
offset += sizes[sampleIndex];
|
|
337
|
+
sampleIndex += 1;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
if (offsets.length !== sizes.length)
|
|
341
|
+
return [];
|
|
342
|
+
let dts = 0;
|
|
343
|
+
return sizes.map((size, index) => {
|
|
344
|
+
const duration = durations[index] || durations[durations.length - 1] || 0;
|
|
345
|
+
const sample = {
|
|
346
|
+
offset: offsets[index],
|
|
347
|
+
size,
|
|
348
|
+
dts,
|
|
349
|
+
cts: dts + (compositionOffsets[index] || 0),
|
|
350
|
+
duration,
|
|
351
|
+
isSync: syncSamples ? syncSamples.has(index) : true
|
|
352
|
+
};
|
|
353
|
+
dts += duration;
|
|
354
|
+
return sample;
|
|
355
|
+
});
|
|
356
|
+
};
|
|
357
|
+
const getCodecContentType = (codec) => {
|
|
358
|
+
const mimeCodec = codec === 'mp4v' ? 'mp4v.20' : codec;
|
|
359
|
+
return `video/mp4; codecs="${mimeCodec}"`;
|
|
360
|
+
};
|
|
361
|
+
/**
|
|
362
|
+
* Reads the declared video sample entry from an ISO BMFF/MP4 file.
|
|
363
|
+
*
|
|
364
|
+
* Browsers can accept the generic `video/mp4` container while silently
|
|
365
|
+
* ignoring an unsupported video track. Inspecting `stsd` lets the renderer
|
|
366
|
+
* distinguish that audio-only success from a genuinely decodable video.
|
|
367
|
+
*/
|
|
368
|
+
export const inspectMp4VideoTrack = (buffer) => {
|
|
369
|
+
const view = new DataView(buffer);
|
|
370
|
+
const movieBox = readChildren(view, 0, view.byteLength).find(box => box.type === 'moov');
|
|
371
|
+
if (!movieBox)
|
|
372
|
+
return undefined;
|
|
373
|
+
for (const trackBox of readChildren(view, movieBox.payloadStart, movieBox.end)) {
|
|
374
|
+
if (trackBox.type !== 'trak')
|
|
375
|
+
continue;
|
|
376
|
+
const mediaBox = findChild(view, trackBox, 'mdia');
|
|
377
|
+
if (!mediaBox || getHandlerType(view, mediaBox) !== 'vide')
|
|
378
|
+
continue;
|
|
379
|
+
const codec = getVideoSampleEntry(view, mediaBox);
|
|
380
|
+
if (!codec)
|
|
381
|
+
return undefined;
|
|
382
|
+
return {
|
|
383
|
+
codec,
|
|
384
|
+
label: VIDEO_CODEC_LABELS[codec] || codec,
|
|
385
|
+
contentType: getCodecContentType(codec)
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
return undefined;
|
|
389
|
+
};
|
|
390
|
+
/**
|
|
391
|
+
* Extracts the minimal MP4V decode timeline needed by the Apache-2.0
|
|
392
|
+
* PacketVideo software decoder. This intentionally avoids a general-purpose
|
|
393
|
+
* media framework so the fallback stays small and offline-friendly.
|
|
394
|
+
*/
|
|
395
|
+
export const extractMp4vSoftwareTrack = (buffer) => {
|
|
396
|
+
const view = new DataView(buffer);
|
|
397
|
+
const movieBox = readChildren(view, 0, view.byteLength).find(box => box.type === 'moov');
|
|
398
|
+
if (!movieBox)
|
|
399
|
+
return undefined;
|
|
400
|
+
for (const trackBox of readChildren(view, movieBox.payloadStart, movieBox.end)) {
|
|
401
|
+
if (trackBox.type !== 'trak')
|
|
402
|
+
continue;
|
|
403
|
+
const mediaBox = findChild(view, trackBox, 'mdia');
|
|
404
|
+
if (!mediaBox || getHandlerType(view, mediaBox) !== 'vide')
|
|
405
|
+
continue;
|
|
406
|
+
const sampleEntry = getSampleDescription(view, mediaBox);
|
|
407
|
+
if (!sampleEntry || sampleEntry.type !== 'mp4v')
|
|
408
|
+
return undefined;
|
|
409
|
+
const dimensionsOffset = sampleEntry.payloadStart + 24;
|
|
410
|
+
if (dimensionsOffset + 4 > sampleEntry.end)
|
|
411
|
+
return undefined;
|
|
412
|
+
const decoderConfig = readDecoderSpecificInfo(view, sampleEntry);
|
|
413
|
+
const timing = getMediaTimescaleAndDuration(view, mediaBox);
|
|
414
|
+
const sampleTable = getSampleTable(view, mediaBox);
|
|
415
|
+
const width = view.getUint16(dimensionsOffset);
|
|
416
|
+
const height = view.getUint16(dimensionsOffset + 2);
|
|
417
|
+
if (!width ||
|
|
418
|
+
!height ||
|
|
419
|
+
width > MAX_MP4V_DIMENSION ||
|
|
420
|
+
height > MAX_MP4V_DIMENSION ||
|
|
421
|
+
width * height > MAX_MP4V_PIXELS ||
|
|
422
|
+
!(decoderConfig === null || decoderConfig === void 0 ? void 0 : decoderConfig.length) ||
|
|
423
|
+
decoderConfig.length > MAX_MP4V_DECODER_CONFIG_BYTES ||
|
|
424
|
+
!(timing === null || timing === void 0 ? void 0 : timing.timescale) ||
|
|
425
|
+
!timing.duration ||
|
|
426
|
+
!sampleTable)
|
|
427
|
+
return undefined;
|
|
428
|
+
const samples = buildSamples(view, sampleTable);
|
|
429
|
+
if (!samples.length || samples.some(sample => (sample.offset < 0 ||
|
|
430
|
+
sample.size <= 0 ||
|
|
431
|
+
!Number.isSafeInteger(sample.offset + sample.size) ||
|
|
432
|
+
sample.offset + sample.size > buffer.byteLength)))
|
|
433
|
+
return undefined;
|
|
434
|
+
return {
|
|
435
|
+
codec: 'mp4v',
|
|
436
|
+
label: VIDEO_CODEC_LABELS.mp4v,
|
|
437
|
+
contentType: getCodecContentType('mp4v'),
|
|
438
|
+
width,
|
|
439
|
+
height,
|
|
440
|
+
timescale: timing.timescale,
|
|
441
|
+
duration: timing.duration,
|
|
442
|
+
decoderConfig,
|
|
443
|
+
samples
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
return undefined;
|
|
447
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|