@hiroosy/baileys-lite 0.4.3 → 0.4.4
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 +8 -17
- package/package.json +1 -1
- package/src/ffmpeg/LICENSE +502 -0
- package/src/ffmpeg/index.js +64 -0
- package/src/ffmpeg/lib/bits.js +150 -0
- package/src/ffmpeg/lib/bsf.js +242 -0
- package/src/ffmpeg/lib/codec/flac.js +801 -0
- package/src/ffmpeg/lib/codec/index.js +108 -0
- package/src/ffmpeg/lib/codec/mp3.js +768 -0
- package/src/ffmpeg/lib/codec/mpa.js +65 -0
- package/src/ffmpeg/lib/codec/mpa_tables.js +27 -0
- package/src/ffmpeg/lib/codec/opus.js +140 -0
- package/src/ffmpeg/lib/codec/pcm.js +198 -0
- package/src/ffmpeg/lib/compat.js +42 -0
- package/src/ffmpeg/lib/crc.js +60 -0
- package/src/ffmpeg/lib/demux/adts.js +151 -0
- package/src/ffmpeg/lib/demux/flac.js +150 -0
- package/src/ffmpeg/lib/demux/image.js +47 -0
- package/src/ffmpeg/lib/demux/index.js +60 -0
- package/src/ffmpeg/lib/demux/matroska.js +525 -0
- package/src/ffmpeg/lib/demux/merge.js +106 -0
- package/src/ffmpeg/lib/demux/mp3.js +199 -0
- package/src/ffmpeg/lib/demux/mp4.js +622 -0
- package/src/ffmpeg/lib/demux/ogg.js +250 -0
- package/src/ffmpeg/lib/demux/ts.js +354 -0
- package/src/ffmpeg/lib/demux/wav.js +156 -0
- package/src/ffmpeg/lib/errors.js +27 -0
- package/src/ffmpeg/lib/filter/gain.js +69 -0
- package/src/ffmpeg/lib/filter/mix.js +95 -0
- package/src/ffmpeg/lib/filter/resample.js +187 -0
- package/src/ffmpeg/lib/filter/video.js +575 -0
- package/src/ffmpeg/lib/image/bmp.js +123 -0
- package/src/ffmpeg/lib/image/frame.js +216 -0
- package/src/ffmpeg/lib/image/gif.js +215 -0
- package/src/ffmpeg/lib/image/index.js +70 -0
- package/src/ffmpeg/lib/image/jpeg.js +964 -0
- package/src/ffmpeg/lib/image/png.js +256 -0
- package/src/ffmpeg/lib/image/run.js +67 -0
- package/src/ffmpeg/lib/io.js +298 -0
- package/src/ffmpeg/lib/mux/adts.js +75 -0
- package/src/ffmpeg/lib/mux/flac.js +79 -0
- package/src/ffmpeg/lib/mux/mp3.js +85 -0
- package/src/ffmpeg/lib/mux/mp4.js +450 -0
- package/src/ffmpeg/lib/mux/ogg.js +146 -0
- package/src/ffmpeg/lib/mux/wav.js +94 -0
- package/src/ffmpeg/lib/net.js +270 -0
- package/src/ffmpeg/lib/pipeline.js +579 -0
- package/src/ffmpeg/lib/probe.js +45 -0
- package/src/ffmpeg/lib/tags.js +307 -0
- package/src/ffmpeg/lib/util.js +104 -0
- package/src/foundation/image-thumb.js +4 -56
- package/src/socket/messages-recv.js +5 -6
- package/src/utils/media.js +8 -85
- package/src/utils/message-compose.js +19 -11
- package/src/voip/index.js +16 -50
- package/src/voip/media/WaAudioEngine.js +8 -216
- package/src/voip/media/WaVideoEngine.js +8 -237
|
@@ -0,0 +1,622 @@
|
|
|
1
|
+
import { badData, noSupport } from '../errors.js';
|
|
2
|
+
import { Vec, readU64BE } from '../util.js';
|
|
3
|
+
import { parseMp4Ilst } from '../tags.js';
|
|
4
|
+
import { buildOpusHead } from '../codec/opus.js';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
function readBox(buf, off, end) {
|
|
8
|
+
if (off + 8 > end) return null;
|
|
9
|
+
let size = buf.readUInt32BE(off);
|
|
10
|
+
const type = buf.toString('latin1', off + 4, off + 8);
|
|
11
|
+
let header = 8;
|
|
12
|
+
if (size === 1) {
|
|
13
|
+
if (off + 16 > end) return null;
|
|
14
|
+
size = readU64BE(buf, off + 8);
|
|
15
|
+
header = 16;
|
|
16
|
+
} else if (size === 0) size = end - off;
|
|
17
|
+
if (size < header || off + size > end) return null;
|
|
18
|
+
return { type, start: off + header, end: off + size, size, header };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function children(buf, start, end) {
|
|
22
|
+
const out = [];
|
|
23
|
+
let p = start;
|
|
24
|
+
while (p + 8 <= end) {
|
|
25
|
+
const b = readBox(buf, p, end);
|
|
26
|
+
if (!b) break;
|
|
27
|
+
out.push(b);
|
|
28
|
+
p = b.end;
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function find(list, type) {
|
|
34
|
+
for (const b of list) if (b.type === type) return b;
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function readDescrLen(buf, pos) {
|
|
39
|
+
let len = 0;
|
|
40
|
+
for (let i = 0; i < 4; i++) {
|
|
41
|
+
const b = buf[pos.v++];
|
|
42
|
+
len = (len << 7) | (b & 0x7f);
|
|
43
|
+
if (!(b & 0x80)) break;
|
|
44
|
+
}
|
|
45
|
+
return len;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function parseEsds(buf, start, end) {
|
|
49
|
+
const pos = { v: start + 4 };
|
|
50
|
+
const out = { oti: 0, dsi: null, avgBitrate: 0, maxBitrate: 0 };
|
|
51
|
+
if (buf[pos.v++] !== 0x03) return out;
|
|
52
|
+
readDescrLen(buf, pos);
|
|
53
|
+
pos.v += 2;
|
|
54
|
+
const flags = buf[pos.v++];
|
|
55
|
+
if (flags & 0x80) pos.v += 2;
|
|
56
|
+
if (flags & 0x40) pos.v += 1 + buf[pos.v];
|
|
57
|
+
if (flags & 0x20) pos.v += 2;
|
|
58
|
+
if (buf[pos.v++] !== 0x04) return out;
|
|
59
|
+
readDescrLen(buf, pos);
|
|
60
|
+
out.oti = buf[pos.v++];
|
|
61
|
+
pos.v += 1;
|
|
62
|
+
pos.v += 3;
|
|
63
|
+
out.maxBitrate = buf.readUInt32BE(pos.v);
|
|
64
|
+
out.avgBitrate = buf.readUInt32BE(pos.v + 4);
|
|
65
|
+
pos.v += 8;
|
|
66
|
+
if (pos.v < end && buf[pos.v] === 0x05) {
|
|
67
|
+
pos.v++;
|
|
68
|
+
const len = readDescrLen(buf, pos);
|
|
69
|
+
out.dsi = Buffer.from(buf.subarray(pos.v, Math.min(pos.v + len, end)));
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function audioCodecFromOti(oti) {
|
|
75
|
+
if (oti === 0x40 || (oti >= 0x66 && oti <= 0x68)) return 'aac';
|
|
76
|
+
if (oti === 0x69 || oti === 0x6b) return 'mp3';
|
|
77
|
+
if (oti === 0xdd) return 'vorbis';
|
|
78
|
+
return 'mp4a_0x' + oti.toString(16);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
function parseSampleEntry(buf, kind) {
|
|
83
|
+
const raw = Buffer.from(buf);
|
|
84
|
+
const fourcc = raw.toString('latin1', 4, 8);
|
|
85
|
+
const out = { raw, fourcc };
|
|
86
|
+
let childStart;
|
|
87
|
+
if (kind === 'soun') {
|
|
88
|
+
const version = raw.readUInt16BE(16);
|
|
89
|
+
out.channels = raw.readUInt16BE(24);
|
|
90
|
+
out.bits = raw.readUInt16BE(26);
|
|
91
|
+
out.sampleRate = raw.readUInt32BE(32) / 65536;
|
|
92
|
+
childStart = 36;
|
|
93
|
+
if (version === 1) childStart = 52;
|
|
94
|
+
else if (version === 2) {
|
|
95
|
+
out.sampleRate = raw.readDoubleBE(40);
|
|
96
|
+
out.channels = raw.readUInt32BE(48);
|
|
97
|
+
childStart = 72;
|
|
98
|
+
}
|
|
99
|
+
} else if (kind === 'vide') {
|
|
100
|
+
out.width = raw.readUInt16BE(32);
|
|
101
|
+
out.height = raw.readUInt16BE(34);
|
|
102
|
+
childStart = 86;
|
|
103
|
+
} else return out;
|
|
104
|
+
const walk = (start, end) => {
|
|
105
|
+
for (const b of children(raw, start, end)) {
|
|
106
|
+
if (b.type === 'wave') walk(b.start, b.end);
|
|
107
|
+
else if (b.type === 'esds') out.esds = parseEsds(raw, b.start, b.end);
|
|
108
|
+
else if (b.type === 'dOps') out.dops = raw.subarray(b.start, b.end);
|
|
109
|
+
else if (b.type === 'dfLa') out.dfla = raw.subarray(b.start, b.end);
|
|
110
|
+
else if (b.type === 'avcC' || b.type === 'hvcC' || b.type === 'av1C' || b.type === 'vpcC') out.config = Buffer.from(raw.subarray(b.start, b.end));
|
|
111
|
+
else if (b.type === 'alac') out.alac = Buffer.from(raw.subarray(b.start, b.end));
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
if (childStart < raw.length) walk(childStart, raw.length);
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function opusHeadFromDops(dops) {
|
|
119
|
+
const channels = dops[1];
|
|
120
|
+
const info = {
|
|
121
|
+
channels,
|
|
122
|
+
preSkip: dops.readUInt16BE(2),
|
|
123
|
+
inputRate: dops.readUInt32BE(4),
|
|
124
|
+
gain: dops.readInt16BE(8),
|
|
125
|
+
family: dops[10],
|
|
126
|
+
};
|
|
127
|
+
return buildOpusHead(info, info.family ? dops.subarray(11) : null);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
class Track {
|
|
131
|
+
constructor() {
|
|
132
|
+
this.sizes = new Vec(Uint32Array, 1024);
|
|
133
|
+
this.offsets = new Vec(Float64Array, 1024);
|
|
134
|
+
this.dts = new Vec(Float64Array, 1024);
|
|
135
|
+
this.cto = new Vec(Int32Array, 1024);
|
|
136
|
+
this.sync = new Vec(Uint8Array, 1024);
|
|
137
|
+
this.hasCto = false;
|
|
138
|
+
this.allSync = true;
|
|
139
|
+
this.cursor = 0;
|
|
140
|
+
this.shift = 0;
|
|
141
|
+
this.lastDelta = 0;
|
|
142
|
+
this.nextDts = 0;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
add(size, offset, dts, cto, key) {
|
|
146
|
+
this.sizes.push(size);
|
|
147
|
+
this.offsets.push(offset);
|
|
148
|
+
this.dts.push(dts);
|
|
149
|
+
this.cto.push(cto);
|
|
150
|
+
this.sync.push(key ? 1 : 0);
|
|
151
|
+
if (cto !== 0) this.hasCto = true;
|
|
152
|
+
if (!key) this.allSync = false;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
get count() {
|
|
156
|
+
return this.sizes.length;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
class Mp4Demuxer {
|
|
161
|
+
static probe(head) {
|
|
162
|
+
if (head.length < 12) return false;
|
|
163
|
+
const t = head.toString('latin1', 4, 8);
|
|
164
|
+
return t === 'ftyp' || t === 'moov' || t === 'mdat' || t === 'free' || t === 'wide' || t === 'skip' || t === 'styp';
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
static async open(reader) {
|
|
168
|
+
const demuxer = new Mp4Demuxer(reader);
|
|
169
|
+
await demuxer.init();
|
|
170
|
+
return demuxer;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
constructor(reader) {
|
|
174
|
+
this.reader = reader;
|
|
175
|
+
this.format = 'mp4';
|
|
176
|
+
this.tags = {};
|
|
177
|
+
this.streams = [];
|
|
178
|
+
this.tracks = [];
|
|
179
|
+
this.brand = '';
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async init() {
|
|
183
|
+
const r = this.reader;
|
|
184
|
+
let pos = 0;
|
|
185
|
+
let moov = null;
|
|
186
|
+
const moofs = [];
|
|
187
|
+
while (pos + 8 <= r.size) {
|
|
188
|
+
r.seek(pos);
|
|
189
|
+
const head = await r.read(16);
|
|
190
|
+
let size = head.readUInt32BE(0);
|
|
191
|
+
const type = head.toString('latin1', 4, 8);
|
|
192
|
+
if (size === 1 && head.length >= 16) size = readU64BE(head, 8);
|
|
193
|
+
else if (size === 0) size = r.size - pos;
|
|
194
|
+
if (size < 8) break;
|
|
195
|
+
if (type === 'ftyp') {
|
|
196
|
+
r.seek(pos + 8);
|
|
197
|
+
this.brand = (await r.read(4)).toString('latin1');
|
|
198
|
+
} else if (type === 'moov' && !moov) {
|
|
199
|
+
r.seek(pos);
|
|
200
|
+
moov = await r.read(Math.min(size, r.size - pos));
|
|
201
|
+
} else if (type === 'moof') moofs.push({ pos, size: Math.min(size, r.size - pos) });
|
|
202
|
+
pos += size;
|
|
203
|
+
}
|
|
204
|
+
if (!moov) throw badData('mp4: missing moov box');
|
|
205
|
+
const top = readBox(moov, 0, moov.length);
|
|
206
|
+
if (!top) throw badData('mp4: truncated moov box');
|
|
207
|
+
this.parseMoov(moov, top);
|
|
208
|
+
if (moofs.length) await this.parseFragments(moofs);
|
|
209
|
+
this.finalize();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
parseMoov(buf, moov) {
|
|
213
|
+
const kids = children(buf, moov.start, moov.end);
|
|
214
|
+
const mvhd = find(kids, 'mvhd');
|
|
215
|
+
if (mvhd) {
|
|
216
|
+
const v = buf[mvhd.start];
|
|
217
|
+
this.movieTimescale = buf.readUInt32BE(mvhd.start + (v === 1 ? 20 : 12));
|
|
218
|
+
this.movieDuration = v === 1 ? readU64BE(buf, mvhd.start + 24) : buf.readUInt32BE(mvhd.start + 16);
|
|
219
|
+
} else this.movieTimescale = 1000;
|
|
220
|
+
const mvex = find(kids, 'mvex');
|
|
221
|
+
this.trex = new Map();
|
|
222
|
+
if (mvex) {
|
|
223
|
+
for (const t of children(buf, mvex.start, mvex.end)) {
|
|
224
|
+
if (t.type !== 'trex') continue;
|
|
225
|
+
this.trex.set(buf.readUInt32BE(t.start + 4), {
|
|
226
|
+
duration: buf.readUInt32BE(t.start + 12),
|
|
227
|
+
size: buf.readUInt32BE(t.start + 16),
|
|
228
|
+
flags: buf.readUInt32BE(t.start + 20),
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
this.fragmented = Boolean(mvex);
|
|
233
|
+
for (const k of kids) {
|
|
234
|
+
if (k.type === 'trak') this.parseTrak(buf, k);
|
|
235
|
+
else if (k.type === 'udta') this.parseUdta(buf, k);
|
|
236
|
+
else if (k.type === 'meta') this.parseMeta(buf, k);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
parseUdta(buf, udta) {
|
|
241
|
+
for (const m of children(buf, udta.start, udta.end)) if (m.type === 'meta') this.parseMeta(buf, m);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
parseMeta(buf, meta) {
|
|
245
|
+
const start = buf.readUInt32BE(meta.start) === 0 ? meta.start + 4 : meta.start;
|
|
246
|
+
const ilst = find(children(buf, start, meta.end), 'ilst');
|
|
247
|
+
if (ilst) Object.assign(this.tags, parseMp4Ilst(buf.subarray(ilst.start, ilst.end)));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
parseTrak(buf, trak) {
|
|
251
|
+
const kids = children(buf, trak.start, trak.end);
|
|
252
|
+
const tkhd = find(kids, 'tkhd');
|
|
253
|
+
const mdia = find(kids, 'mdia');
|
|
254
|
+
if (!tkhd || !mdia) return;
|
|
255
|
+
const tv = buf[tkhd.start];
|
|
256
|
+
const id = buf.readUInt32BE(tkhd.start + (tv === 1 ? 20 : 12));
|
|
257
|
+
const mk = children(buf, mdia.start, mdia.end);
|
|
258
|
+
const mdhd = find(mk, 'mdhd');
|
|
259
|
+
const hdlr = find(mk, 'hdlr');
|
|
260
|
+
const minf = find(mk, 'minf');
|
|
261
|
+
if (!mdhd || !hdlr || !minf) return;
|
|
262
|
+
const mv = buf[mdhd.start];
|
|
263
|
+
const timescale = buf.readUInt32BE(mdhd.start + (mv === 1 ? 20 : 12));
|
|
264
|
+
const duration = mv === 1 ? readU64BE(buf, mdhd.start + 24) : buf.readUInt32BE(mdhd.start + 16);
|
|
265
|
+
const handler = buf.toString('latin1', hdlr.start + 8, hdlr.start + 12);
|
|
266
|
+
if (handler !== 'soun' && handler !== 'vide') return;
|
|
267
|
+
const stbl = find(children(buf, minf.start, minf.end), 'stbl');
|
|
268
|
+
if (!stbl) return;
|
|
269
|
+
const sk = children(buf, stbl.start, stbl.end);
|
|
270
|
+
const stsd = find(sk, 'stsd');
|
|
271
|
+
if (!stsd) return;
|
|
272
|
+
const entrySize = buf.readUInt32BE(stsd.start + 8);
|
|
273
|
+
const entry = parseSampleEntry(buf.subarray(stsd.start + 8, stsd.start + 8 + entrySize), handler);
|
|
274
|
+
const track = new Track();
|
|
275
|
+
track.id = id;
|
|
276
|
+
track.handler = handler;
|
|
277
|
+
track.timescale = timescale;
|
|
278
|
+
track.mediaDuration = duration;
|
|
279
|
+
track.entry = entry;
|
|
280
|
+
if (tkhd.end - tkhd.start >= 8) track.tkWidth = buf.readUInt32BE(tkhd.end - 8) / 65536;
|
|
281
|
+
if (tkhd.end - tkhd.start >= 8) track.tkHeight = buf.readUInt32BE(tkhd.end - 4) / 65536;
|
|
282
|
+
const edts = find(kids, 'edts');
|
|
283
|
+
if (edts) {
|
|
284
|
+
const elst = find(children(buf, edts.start, edts.end), 'elst');
|
|
285
|
+
if (elst) this.parseElst(buf, elst, track);
|
|
286
|
+
}
|
|
287
|
+
this.buildTable(buf, sk, track);
|
|
288
|
+
this.tracks.push(track);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
parseElst(buf, elst, track) {
|
|
292
|
+
const v = buf[elst.start];
|
|
293
|
+
const n = buf.readUInt32BE(elst.start + 4);
|
|
294
|
+
let p = elst.start + 8;
|
|
295
|
+
let empty = 0;
|
|
296
|
+
let shift = 0;
|
|
297
|
+
for (let i = 0; i < n; i++) {
|
|
298
|
+
let segDur;
|
|
299
|
+
let mediaTime;
|
|
300
|
+
if (v === 1) {
|
|
301
|
+
segDur = readU64BE(buf, p);
|
|
302
|
+
mediaTime = Number(buf.readBigInt64BE(p + 8));
|
|
303
|
+
p += 20;
|
|
304
|
+
} else {
|
|
305
|
+
segDur = buf.readUInt32BE(p);
|
|
306
|
+
mediaTime = buf.readInt32BE(p + 4);
|
|
307
|
+
p += 12;
|
|
308
|
+
}
|
|
309
|
+
if (mediaTime === -1) {
|
|
310
|
+
empty += Math.round((segDur * track.timescale) / this.movieTimescale);
|
|
311
|
+
} else {
|
|
312
|
+
shift = empty - mediaTime;
|
|
313
|
+
track.hasEdit = true;
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (!track.hasEdit) shift = empty;
|
|
318
|
+
track.shift = shift;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
buildTable(buf, sk, track) {
|
|
322
|
+
const stsz = find(sk, 'stsz');
|
|
323
|
+
const stts = find(sk, 'stts');
|
|
324
|
+
const stsc = find(sk, 'stsc');
|
|
325
|
+
const stco = find(sk, 'stco') || find(sk, 'co64');
|
|
326
|
+
if (!stsz || !stts || !stsc || !stco) {
|
|
327
|
+
if (this.fragmented) return;
|
|
328
|
+
throw badData('mp4: incomplete sample table');
|
|
329
|
+
}
|
|
330
|
+
const uniform = buf.readUInt32BE(stsz.start + 4);
|
|
331
|
+
const count = buf.readUInt32BE(stsz.start + 8);
|
|
332
|
+
if (count > 40000000) throw noSupport('mp4: too many samples');
|
|
333
|
+
const sizes = new Uint32Array(count);
|
|
334
|
+
if (uniform) sizes.fill(uniform);
|
|
335
|
+
else for (let i = 0; i < count; i++) sizes[i] = buf.readUInt32BE(stsz.start + 12 + i * 4);
|
|
336
|
+
const dts = new Float64Array(count);
|
|
337
|
+
{
|
|
338
|
+
const entries = buf.readUInt32BE(stts.start + 4);
|
|
339
|
+
let p = stts.start + 8;
|
|
340
|
+
let i = 0;
|
|
341
|
+
let t = 0;
|
|
342
|
+
let last = 0;
|
|
343
|
+
for (let e = 0; e < entries && i < count; e++, p += 8) {
|
|
344
|
+
const c = buf.readUInt32BE(p);
|
|
345
|
+
const d = buf.readUInt32BE(p + 4);
|
|
346
|
+
last = d;
|
|
347
|
+
for (let k = 0; k < c && i < count; k++) {
|
|
348
|
+
dts[i++] = t;
|
|
349
|
+
t += d;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
track.lastDelta = last;
|
|
353
|
+
}
|
|
354
|
+
let cto = null;
|
|
355
|
+
const ctts = find(sk, 'ctts');
|
|
356
|
+
if (ctts) {
|
|
357
|
+
cto = new Int32Array(count);
|
|
358
|
+
const signed = buf[ctts.start] === 1;
|
|
359
|
+
const entries = buf.readUInt32BE(ctts.start + 4);
|
|
360
|
+
let p = ctts.start + 8;
|
|
361
|
+
let i = 0;
|
|
362
|
+
for (let e = 0; e < entries && i < count; e++, p += 8) {
|
|
363
|
+
const c = buf.readUInt32BE(p);
|
|
364
|
+
const v = signed ? buf.readInt32BE(p + 4) : buf.readUInt32BE(p + 4) | 0;
|
|
365
|
+
for (let k = 0; k < c && i < count; k++) cto[i++] = v;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
let sync = null;
|
|
369
|
+
const stss = find(sk, 'stss');
|
|
370
|
+
if (stss) {
|
|
371
|
+
sync = new Uint8Array(count);
|
|
372
|
+
const n = buf.readUInt32BE(stss.start + 4);
|
|
373
|
+
for (let i = 0; i < n; i++) {
|
|
374
|
+
const idx = buf.readUInt32BE(stss.start + 8 + i * 4) - 1;
|
|
375
|
+
if (idx >= 0 && idx < count) sync[idx] = 1;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
const co64 = stco.type === 'co64';
|
|
379
|
+
const chunks = buf.readUInt32BE(stco.start + 4);
|
|
380
|
+
const stscEntries = buf.readUInt32BE(stsc.start + 4);
|
|
381
|
+
let sp = stsc.start + 8;
|
|
382
|
+
let sample = 0;
|
|
383
|
+
let runIdx = 0;
|
|
384
|
+
let perChunk = buf.readUInt32BE(sp + 4);
|
|
385
|
+
let nextFirst = stscEntries > 1 ? buf.readUInt32BE(sp + 12) : Infinity;
|
|
386
|
+
for (let c = 1; c <= chunks && sample < count; c++) {
|
|
387
|
+
while (c >= nextFirst) {
|
|
388
|
+
runIdx++;
|
|
389
|
+
sp += 12;
|
|
390
|
+
perChunk = buf.readUInt32BE(sp + 4);
|
|
391
|
+
nextFirst = runIdx + 1 < stscEntries ? buf.readUInt32BE(sp + 12) : Infinity;
|
|
392
|
+
}
|
|
393
|
+
let offset = co64 ? readU64BE(buf, stco.start + 8 + (c - 1) * 8) : buf.readUInt32BE(stco.start + 8 + (c - 1) * 4);
|
|
394
|
+
for (let k = 0; k < perChunk && sample < count; k++) {
|
|
395
|
+
const size = sizes[sample];
|
|
396
|
+
track.add(size, offset, dts[sample], cto ? cto[sample] : 0, sync ? sync[sample] === 1 : true);
|
|
397
|
+
offset += size;
|
|
398
|
+
sample++;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (count > 1) track.lastDelta = track.lastDelta || dts[count - 1] - dts[count - 2];
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
async parseFragments(moofs) {
|
|
405
|
+
const r = this.reader;
|
|
406
|
+
const byId = new Map();
|
|
407
|
+
for (const t of this.tracks) byId.set(t.id, t);
|
|
408
|
+
for (const m of moofs) {
|
|
409
|
+
r.seek(m.pos);
|
|
410
|
+
const buf = await r.read(m.size);
|
|
411
|
+
const moof = readBox(buf, 0, buf.length);
|
|
412
|
+
if (!moof) continue;
|
|
413
|
+
for (const traf of children(buf, moof.start, moof.end)) {
|
|
414
|
+
if (traf.type !== 'traf') continue;
|
|
415
|
+
const kids = children(buf, traf.start, traf.end);
|
|
416
|
+
const tfhd = find(kids, 'tfhd');
|
|
417
|
+
if (!tfhd) continue;
|
|
418
|
+
const flags = buf.readUIntBE(tfhd.start + 1, 3);
|
|
419
|
+
const id = buf.readUInt32BE(tfhd.start + 4);
|
|
420
|
+
const track = byId.get(id);
|
|
421
|
+
if (!track) continue;
|
|
422
|
+
const trex = this.trex.get(id) || { duration: 0, size: 0, flags: 0 };
|
|
423
|
+
let p = tfhd.start + 8;
|
|
424
|
+
let base = m.pos;
|
|
425
|
+
if (flags & 1) {
|
|
426
|
+
base = readU64BE(buf, p);
|
|
427
|
+
p += 8;
|
|
428
|
+
}
|
|
429
|
+
if (flags & 2) p += 4;
|
|
430
|
+
let defDur = trex.duration;
|
|
431
|
+
let defSize = trex.size;
|
|
432
|
+
let defFlags = trex.flags;
|
|
433
|
+
if (flags & 8) {
|
|
434
|
+
defDur = buf.readUInt32BE(p);
|
|
435
|
+
p += 4;
|
|
436
|
+
}
|
|
437
|
+
if (flags & 0x10) {
|
|
438
|
+
defSize = buf.readUInt32BE(p);
|
|
439
|
+
p += 4;
|
|
440
|
+
}
|
|
441
|
+
if (flags & 0x20) {
|
|
442
|
+
defFlags = buf.readUInt32BE(p);
|
|
443
|
+
p += 4;
|
|
444
|
+
}
|
|
445
|
+
const tfdt = find(kids, 'tfdt');
|
|
446
|
+
if (tfdt) track.nextDts = buf[tfdt.start] === 1 ? readU64BE(buf, tfdt.start + 4) : buf.readUInt32BE(tfdt.start + 4);
|
|
447
|
+
let cursor = base;
|
|
448
|
+
for (const trun of kids) {
|
|
449
|
+
if (trun.type !== 'trun') continue;
|
|
450
|
+
const tf = buf.readUIntBE(trun.start + 1, 3);
|
|
451
|
+
const n = buf.readUInt32BE(trun.start + 4);
|
|
452
|
+
let q = trun.start + 8;
|
|
453
|
+
if (tf & 1) {
|
|
454
|
+
cursor = base + buf.readInt32BE(q);
|
|
455
|
+
q += 4;
|
|
456
|
+
}
|
|
457
|
+
let firstFlags = null;
|
|
458
|
+
if (tf & 4) {
|
|
459
|
+
firstFlags = buf.readUInt32BE(q);
|
|
460
|
+
q += 4;
|
|
461
|
+
}
|
|
462
|
+
for (let i = 0; i < n; i++) {
|
|
463
|
+
let dur = defDur;
|
|
464
|
+
let size = defSize;
|
|
465
|
+
let sflags = defFlags;
|
|
466
|
+
let off = 0;
|
|
467
|
+
if (tf & 0x100) {
|
|
468
|
+
dur = buf.readUInt32BE(q);
|
|
469
|
+
q += 4;
|
|
470
|
+
}
|
|
471
|
+
if (tf & 0x200) {
|
|
472
|
+
size = buf.readUInt32BE(q);
|
|
473
|
+
q += 4;
|
|
474
|
+
}
|
|
475
|
+
if (tf & 0x400) {
|
|
476
|
+
sflags = buf.readUInt32BE(q);
|
|
477
|
+
q += 4;
|
|
478
|
+
} else if (i === 0 && firstFlags !== null) sflags = firstFlags;
|
|
479
|
+
if (tf & 0x800) {
|
|
480
|
+
off = buf[trun.start] === 1 ? buf.readInt32BE(q) : buf.readUInt32BE(q) | 0;
|
|
481
|
+
q += 4;
|
|
482
|
+
}
|
|
483
|
+
track.add(size, cursor, track.nextDts, off, ((sflags >> 16) & 1) === 0);
|
|
484
|
+
track.nextDts += dur;
|
|
485
|
+
track.lastDelta = dur;
|
|
486
|
+
cursor += size;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
finalize() {
|
|
494
|
+
let index = 0;
|
|
495
|
+
let longest = 0;
|
|
496
|
+
for (const t of this.tracks) {
|
|
497
|
+
const e = t.entry;
|
|
498
|
+
const n = t.count;
|
|
499
|
+
const view = {
|
|
500
|
+
sizes: t.sizes.view(),
|
|
501
|
+
offsets: t.offsets.view(),
|
|
502
|
+
dts: t.dts.view(),
|
|
503
|
+
cto: t.cto.view(),
|
|
504
|
+
sync: t.sync.view(),
|
|
505
|
+
};
|
|
506
|
+
t.v = view;
|
|
507
|
+
let duration;
|
|
508
|
+
if (n > 0) duration = (view.dts[n - 1] + t.lastDelta) / t.timescale;
|
|
509
|
+
else duration = t.mediaDuration / t.timescale;
|
|
510
|
+
if (!(duration > 0) && t.mediaDuration) duration = t.mediaDuration / t.timescale;
|
|
511
|
+
let bytes = 0;
|
|
512
|
+
for (let i = 0; i < n; i++) bytes += view.sizes[i];
|
|
513
|
+
const stream = { index: index++, type: t.handler === 'soun' ? 'audio' : 'video', timeScale: t.timescale, duration, mp4Entry: e.raw, trackId: t.id };
|
|
514
|
+
if (t.handler === 'soun') {
|
|
515
|
+
stream.channels = e.channels;
|
|
516
|
+
stream.sampleRate = Math.round(e.sampleRate) || t.timescale;
|
|
517
|
+
let codec = { 'ac-3': 'ac3', 'ec-3': 'eac3', alac: 'alac', fLaC: 'flac', Opus: 'opus', '.mp3': 'mp3', samr: 'amr_nb', sawb: 'amr_wb' }[e.fourcc];
|
|
518
|
+
stream.extradata = null;
|
|
519
|
+
if (e.fourcc === 'mp4a') {
|
|
520
|
+
const oti = e.esds ? e.esds.oti : 0x40;
|
|
521
|
+
codec = audioCodecFromOti(oti);
|
|
522
|
+
if (e.esds && e.esds.dsi) stream.extradata = e.esds.dsi;
|
|
523
|
+
if (e.esds && e.esds.avgBitrate) stream.bitRate = e.esds.avgBitrate;
|
|
524
|
+
} else if (e.fourcc === 'Opus' && e.dops) {
|
|
525
|
+
stream.extradata = opusHeadFromDops(e.dops);
|
|
526
|
+
stream.preSkip = e.dops.readUInt16BE(2);
|
|
527
|
+
stream.sampleRate = 48000;
|
|
528
|
+
} else if (e.fourcc === 'fLaC' && e.dfla) {
|
|
529
|
+
stream.extradata = Buffer.from(e.dfla.subarray(8, 42));
|
|
530
|
+
} else if (e.fourcc === 'alac' && e.alac) stream.extradata = e.alac;
|
|
531
|
+
if (!codec) codec = e.fourcc.trim().toLowerCase();
|
|
532
|
+
stream.codec = codec;
|
|
533
|
+
stream.bitsPerSample = e.bits;
|
|
534
|
+
if (codec === 'aac' && stream.extradata && stream.extradata.length >= 2) {
|
|
535
|
+
const asc = stream.extradata;
|
|
536
|
+
const sfi = ((asc[0] & 7) << 1) | (asc[1] >> 7);
|
|
537
|
+
const rates = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
|
|
538
|
+
const chan = (asc[1] >> 3) & 15;
|
|
539
|
+
if (sfi < 13 && stream.sampleRate !== rates[sfi] && (asc[0] >> 3) < 5) stream.sampleRate = rates[sfi];
|
|
540
|
+
if (chan > 0 && chan < 7 && !stream.channels) stream.channels = chan;
|
|
541
|
+
}
|
|
542
|
+
} else {
|
|
543
|
+
stream.codec = { avc1: 'h264', avc3: 'h264', hvc1: 'hevc', hev1: 'hevc', av01: 'av1', vp09: 'vp9', mp4v: 'mpeg4', jpeg: 'mjpeg', apch: 'prores', apcn: 'prores', apcs: 'prores', apco: 'prores', ap4h: 'prores' }[e.fourcc] || e.fourcc.trim().toLowerCase();
|
|
544
|
+
stream.width = e.width || Math.round(t.tkWidth || 0);
|
|
545
|
+
stream.height = e.height || Math.round(t.tkHeight || 0);
|
|
546
|
+
stream.extradata = e.config || null;
|
|
547
|
+
if (n > 1 && duration > 0) stream.frameRate = n / duration;
|
|
548
|
+
}
|
|
549
|
+
if (!stream.bitRate && duration > 0 && bytes > 0) stream.bitRate = Math.round((bytes * 8) / duration);
|
|
550
|
+
this.streams.push(stream);
|
|
551
|
+
t.stream = stream;
|
|
552
|
+
if (duration > longest) longest = duration;
|
|
553
|
+
}
|
|
554
|
+
if (!this.streams.length) throw badData('mp4: no audio or video tracks');
|
|
555
|
+
this.duration = longest;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
binarySearch(t, target) {
|
|
559
|
+
const dts = t.v.dts;
|
|
560
|
+
let lo = 0;
|
|
561
|
+
let hi = dts.length;
|
|
562
|
+
while (lo < hi) {
|
|
563
|
+
const mid = (lo + hi) >> 1;
|
|
564
|
+
if (dts[mid] + t.v.cto[mid] < target) lo = mid + 1;
|
|
565
|
+
else hi = mid;
|
|
566
|
+
}
|
|
567
|
+
return lo;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
async seek(seconds) {
|
|
571
|
+
const video = this.tracks.find((t) => t.handler === 'vide' && t.count > 0);
|
|
572
|
+
let target = seconds;
|
|
573
|
+
if (video) {
|
|
574
|
+
const goal = seconds * video.timescale - video.shift;
|
|
575
|
+
let idx = 0;
|
|
576
|
+
const v = video.v;
|
|
577
|
+
for (let i = 0; i < video.count; i++) {
|
|
578
|
+
if (v.sync[i] !== 1) continue;
|
|
579
|
+
if (v.dts[i] + v.cto[i] <= goal + 1e-6) idx = i;
|
|
580
|
+
else if (v.dts[i] > goal) break;
|
|
581
|
+
}
|
|
582
|
+
if (!video.v.sync[idx]) for (let i = 0; i < video.count; i++) if (v.sync[i] === 1) { idx = i; break; }
|
|
583
|
+
target = (v.dts[idx] + v.cto[idx] + video.shift) / video.timescale;
|
|
584
|
+
video.cursor = idx;
|
|
585
|
+
}
|
|
586
|
+
for (const t of this.tracks) {
|
|
587
|
+
if (t === video) continue;
|
|
588
|
+
const goal = target * t.timescale - t.shift;
|
|
589
|
+
t.cursor = this.binarySearch(t, goal - 0.5);
|
|
590
|
+
while (t.cursor < t.count && t.v.dts[t.cursor] + t.v.cto[t.cursor] < goal - 0.5) t.cursor++;
|
|
591
|
+
}
|
|
592
|
+
return target;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
async readPacket() {
|
|
596
|
+
let best = null;
|
|
597
|
+
for (const t of this.tracks) {
|
|
598
|
+
if (t.cursor >= t.count) continue;
|
|
599
|
+
if (!best || t.v.offsets[t.cursor] < best.v.offsets[best.cursor]) best = t;
|
|
600
|
+
}
|
|
601
|
+
if (!best) return null;
|
|
602
|
+
const i = best.cursor++;
|
|
603
|
+
const v = best.v;
|
|
604
|
+
this.reader.seek(v.offsets[i]);
|
|
605
|
+
const data = await this.reader.read(v.sizes[i]);
|
|
606
|
+
if (data.length < v.sizes[i]) {
|
|
607
|
+
best.cursor = best.count;
|
|
608
|
+
return this.readPacket();
|
|
609
|
+
}
|
|
610
|
+
const dts = v.dts[i] + best.shift;
|
|
611
|
+
const duration = i + 1 < best.count ? v.dts[i + 1] - v.dts[i] : best.lastDelta;
|
|
612
|
+
return { stream: best.stream.index, pts: dts + v.cto[i], dts, duration, key: v.sync[i] === 1, data };
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
async close() {
|
|
616
|
+
await this.reader.close();
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
export {
|
|
621
|
+
Mp4Demuxer,
|
|
622
|
+
};
|