@margoengine/headless 0.0.0-stage → 0.1.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/dist/video.js ADDED
@@ -0,0 +1,1114 @@
1
+ import { i as e, n as t } from "./src-VufTH2fo.js";
2
+ import { n, r, t as i } from "./errors-CDgOgzCd.js";
3
+ //#region src/avc-range.ts
4
+ function a(e) {
5
+ let t = [], n = 0;
6
+ for (let r = 1; r < e.length; r++) n >= 2 && e[r] === 3 ? n = 0 : (t.push(e[r]), n = e[r] === 0 ? n + 1 : 0);
7
+ return t;
8
+ }
9
+ function o(e) {
10
+ let t = [], n = 0;
11
+ for (let r of e) n >= 2 && r <= 3 && (t.push(3), n = 0), t.push(r), n = r === 0 ? n + 1 : 0;
12
+ return t;
13
+ }
14
+ var s = /* @__PURE__ */ new Set([
15
+ 100,
16
+ 110,
17
+ 122,
18
+ 244,
19
+ 44,
20
+ 83,
21
+ 86,
22
+ 118,
23
+ 128,
24
+ 138,
25
+ 139,
26
+ 134,
27
+ 135
28
+ ]);
29
+ function c(e, t) {
30
+ if (!e.length || (e[0] & 31) != 7) return;
31
+ let n = a(e), r = n.length * 8, i = 0, c = () => {
32
+ if (i >= r) throw RangeError("SPS ends early; it is left as the encoder wrote it");
33
+ return n[i >> 3] >> 7 - (i++ & 7) & 1;
34
+ }, u = (e) => {
35
+ let t = 0;
36
+ for (let n = 0; n < e; n++) t = t * 2 + c();
37
+ return t;
38
+ }, d = () => {
39
+ let e = 0;
40
+ for (; !c();) if (++e > 31) throw RangeError("Exp-Golomb code too long; the SPS is left as the encoder wrote it");
41
+ return 2 ** e - 1 + u(e);
42
+ }, f = (e) => {
43
+ let t = 8, n = 8;
44
+ for (let r = 0; r < e && n !== 0; r++) {
45
+ let e = d(), r = e & 1 ? (e + 1) / 2 : -e / 2;
46
+ n = (t + r + 256) % 256, n !== 0 && (t = n);
47
+ }
48
+ };
49
+ try {
50
+ let a = u(8);
51
+ if (u(16), d(), s.has(a)) {
52
+ let e = d();
53
+ if (e === 3 && c(), d(), d(), c(), c()) for (let t = 0; t < (e === 3 ? 12 : 8); t++) c() && f(t < 6 ? 16 : 64);
54
+ }
55
+ d();
56
+ let p = d();
57
+ if (p === 0) d();
58
+ else if (p === 1) {
59
+ c(), d(), d();
60
+ for (let e = d(); e > 0; e--) d();
61
+ }
62
+ if (d(), c(), d(), d(), c() || c(), c(), c()) for (let e = 0; e < 4; e++) d();
63
+ let m = r - 1;
64
+ for (; m >= 0 && !(n[m >> 3] >> 7 - (m & 7) & 1);) m--;
65
+ let h = [], g = (e, t) => {
66
+ for (let n = t - 1; n >= 0; n--) h.push(Math.floor(e / 2 ** n) % 2);
67
+ }, _ = (e, t) => {
68
+ for (let r = e; r < t; r++) h.push(n[r >> 3] >> 7 - (r & 7) & 1);
69
+ }, v = () => g(52 | (t ? 2 : 0), 6), y = i;
70
+ if (c()) {
71
+ c() && u(8) === 255 && u(32), c() && c();
72
+ let e = i;
73
+ if (c()) {
74
+ u(3);
75
+ let e = i;
76
+ if (c() === +!!t) return;
77
+ _(0, e), g(+!!t, 1), _(e + 1, m);
78
+ } else {
79
+ if (!t) return;
80
+ _(0, e), v(), _(e + 1, m);
81
+ }
82
+ } else {
83
+ if (!t) return;
84
+ _(0, y), g(1, 1), g(0, 2), v(), g(0, 6);
85
+ }
86
+ for (h.push(1); h.length % 8;) h.push(0);
87
+ let b = [];
88
+ for (let e = 0; e < h.length; e += 8) b.push(l(h, e));
89
+ return Uint8Array.from([e[0], ...o(b)]);
90
+ } catch {
91
+ return;
92
+ }
93
+ }
94
+ var l = (e, t) => {
95
+ let n = 0;
96
+ for (let r = 0; r < 8; r++) n = n * 2 + e[t + r];
97
+ return n;
98
+ };
99
+ function u(e, t) {
100
+ if (e.length < 7 || e[0] !== 1) return e;
101
+ let n = [e.subarray(0, 6)], r = 6, i = !1, a = e[5] & 31;
102
+ for (let o = 0; o < a; o++) {
103
+ if (r + 2 > e.length) return e;
104
+ let a = e[r] << 8 | e[r + 1], o = e.subarray(r + 2, r + 2 + a);
105
+ if (o.length !== a) return e;
106
+ let s = c(o, t);
107
+ s && (i = !0);
108
+ let l = s ?? o;
109
+ if (l.length > 65535) return e;
110
+ n.push(Uint8Array.of(l.length >> 8, l.length & 255), l), r += 2 + a;
111
+ }
112
+ if (!i) return e;
113
+ n.push(e.subarray(r));
114
+ let o = new Uint8Array(n.reduce((e, t) => e + t.length, 0)), s = 0;
115
+ for (let e of n) o.set(e, s), s += e.length;
116
+ return o;
117
+ }
118
+ function d(e, t, n) {
119
+ let r = [], i = 0, a = !1;
120
+ for (; i + t <= e.length;) {
121
+ let o = 0;
122
+ for (let n = 0; n < t; n++) o = o * 256 + e[i + n];
123
+ let s = e.subarray(i + t, i + t + o);
124
+ if (s.length !== o) return e;
125
+ let l = c(s, n);
126
+ l && (a = !0);
127
+ let u = l ?? s, d = new Uint8Array(t);
128
+ for (let e = t - 1, n = u.length; e >= 0; e--, n = Math.floor(n / 256)) d[e] = n % 256;
129
+ r.push(d, u), i += t + o;
130
+ }
131
+ if (!a || i !== e.length) return e;
132
+ let o = new Uint8Array(r.reduce((e, t) => e + t.length, 0)), s = 0;
133
+ for (let e of r) o.set(e, s), s += e.length;
134
+ return o;
135
+ }
136
+ //#endregion
137
+ //#region src/video-mux.ts
138
+ var f = (e) => {
139
+ let t = new Uint8Array(e.reduce((e, t) => e + t.length, 0)), n = 0;
140
+ for (let r of e) t.set(r, n), n += r.length;
141
+ return t;
142
+ }, p = (e, t) => {
143
+ let n = new Uint8Array(t);
144
+ for (let r = t - 1; r >= 0; r--) n[r] = e % 256, e = Math.floor(e / 256);
145
+ return n;
146
+ }, m = (e) => new TextEncoder().encode(e), h = {
147
+ bt709: 1,
148
+ bt470bg: 5,
149
+ smpte170m: 6,
150
+ bt2020: 9,
151
+ smpte432: 12
152
+ }, g = {
153
+ bt709: 1,
154
+ smpte170m: 6,
155
+ linear: 8,
156
+ "iec61966-2-1": 13,
157
+ pq: 16,
158
+ hlg: 18
159
+ }, _ = {
160
+ rgb: 0,
161
+ bt709: 1,
162
+ bt470bg: 5,
163
+ smpte170m: 6,
164
+ "bt2020-ncl": 9
165
+ }, v = 2;
166
+ function y(e) {
167
+ return {
168
+ primaries: h[e?.primaries ?? ""] ?? v,
169
+ transfer: g[e?.transfer ?? ""] ?? v,
170
+ matrix: _[e?.matrix ?? ""] ?? v,
171
+ fullRange: e?.fullRange ?? !1
172
+ };
173
+ }
174
+ function b(e) {
175
+ let t = [], n = 0;
176
+ for (; n < e.length;) {
177
+ let r = e[n], i = n;
178
+ n += r & 4 ? 2 : 1;
179
+ let a = e.length - n;
180
+ if (r & 2) {
181
+ a = 0;
182
+ for (let t = 0;; t++) {
183
+ if (t === 8 || n >= e.length) return;
184
+ let r = e[n++];
185
+ if (a += (r & 127) * 2 ** (7 * t), !(r & 128)) break;
186
+ }
187
+ }
188
+ if (r & 128 || n + a > e.length) return;
189
+ t.push([
190
+ r >> 3 & 15,
191
+ i,
192
+ n += a
193
+ ]);
194
+ }
195
+ return t;
196
+ }
197
+ function x(e) {
198
+ let t = 0, n = e.length * 8, r = (r) => {
199
+ let i = 0;
200
+ for (let a = 0; a < r; a++, t++) i = i * 2 + (t < n ? e[t >> 3] >> 7 - (t & 7) & 1 : 0);
201
+ return i;
202
+ }, i = () => {
203
+ let e = 0;
204
+ for (; !r(1);) if (++e >= 32) return;
205
+ r(e);
206
+ }, a = r(3);
207
+ r(1);
208
+ let o = r(1), s = 0, c = 0, l = 0;
209
+ if (o) s = r(5);
210
+ else {
211
+ let e = 0, t = 0;
212
+ r(1) && (r(64), r(1) && i(), e = r(1), e && (t = r(5) + 1, r(42)));
213
+ let n = r(1), a = r(5) + 1;
214
+ for (let i = 0; i < a; i++) {
215
+ r(12);
216
+ let a = r(5), o = a > 7 ? r(1) : 0;
217
+ i === 0 && ([s, c] = [a, o]), e && r(1) && r(2 * t + 1), n && r(1) && r(4);
218
+ }
219
+ }
220
+ r(r(4) + 1 + (r(4) + 1)), !o && r(1) && r(7), r(3), o || (r(4), l = r(1), l && r(2), (r(1) ? 2 : r(1)) > 0 && !r(1) && r(1), l && r(3)), r(3);
221
+ let u = r(1), d = a === 2 && u ? r(1) : 0, f = a === 1 ? 0 : r(1), p = 2, m = 2, h = 2;
222
+ r(1) && ([p, m, h] = [
223
+ r(8),
224
+ r(8),
225
+ r(8)
226
+ ]);
227
+ let g = 1, _ = 1, v = 0;
228
+ if (f || (p === 1 && m === 13 && h === 0 ? [g, _] = [0, 0] : (r(1), a === 1 ? [g, _] = [0, 0] : a === 2 && (d ? (g = r(1), _ = g ? r(1) : 0) : [g, _] = [1, 0]), g && _ && (v = r(2)))), !(t > n)) return Uint8Array.of(129, a << 5 | s, c << 7 | u << 6 | d << 5 | f << 4 | g << 3 | _ << 2 | v, 0);
229
+ }
230
+ function S({ codec: e, description: t }, n) {
231
+ if (t?.length) return t;
232
+ let r = n && b(n)?.find(([e]) => e === 1);
233
+ if (n && r) {
234
+ let [, e, t] = r, i = e + (n[e] & 4 ? 2 : 1);
235
+ if (n[e] & 2) for (; n[i++] & 128;);
236
+ let a = x(n.subarray(i, t));
237
+ if (a) return f([a, n.subarray(e, t)]);
238
+ }
239
+ let [, i, a, o, s] = /^av01\.(\d)\.(\d\d)([MH])\.(\d\d)/.exec(e) ?? [
240
+ ,
241
+ 0,
242
+ 0,
243
+ "M",
244
+ 8
245
+ ], c = Number(s);
246
+ return Uint8Array.of(129, Number(i) << 5 | Number(a), (o === "H" ? 128 : 0) | (c > 8 ? 64 : 0) | (c > 10 ? 32 : 0) | 12, 0);
247
+ }
248
+ function C({ description: e }) {
249
+ let t = e;
250
+ return !t || t.length < 12 || String.fromCharCode(...t.subarray(0, 8)) !== "OpusHead" ? 0 : t[10] | t[11] << 8;
251
+ }
252
+ function w(e) {
253
+ let t = e.description;
254
+ return !t || t.length < 16 || String.fromCharCode(...t.subarray(0, 8)) !== "OpusHead" ? e.sampleRate : new DataView(t.buffer, t.byteOffset, t.byteLength).getUint32(12, !0);
255
+ }
256
+ function T(e) {
257
+ let t = b(e);
258
+ return t?.some(([e]) => e === 2 || e === 15) ? f(t.filter(([e]) => e !== 2 && e !== 15).map(([, t, n]) => e.subarray(t, n))) : e;
259
+ }
260
+ var E = (e, t) => e.startsWith("av01") ? t.map((e) => ({
261
+ ...e,
262
+ data: T(e.data)
263
+ })) : t;
264
+ function D(e) {
265
+ let [, t, n, r] = e.split(".").map(Number);
266
+ return [
267
+ t,
268
+ n,
269
+ r
270
+ ].every(Number.isInteger) ? Uint8Array.of(1, 1, t, 2, 1, n, 3, 1, r, 4, 1, 0) : /* @__PURE__ */ new Uint8Array();
271
+ }
272
+ function O(e) {
273
+ if (C(e) || e.description?.length) return e.description;
274
+ let t = /* @__PURE__ */ new Uint8Array(19);
275
+ t.set(m("OpusHead"));
276
+ let n = new DataView(t.buffer);
277
+ return t[8] = 1, t[9] = e.channels, n.setUint32(12, e.sampleRate, !0), t;
278
+ }
279
+ var k = (e) => p(e, e > 16777215 ? 4 : e > 65535 ? 3 : e > 255 ? 2 : 1);
280
+ function A(e) {
281
+ let t = 1;
282
+ for (; e >= 2 ** (7 * t) - 1;) t++;
283
+ let n = p(e, t);
284
+ return n[0] |= 1 << 8 - t, n;
285
+ }
286
+ var j = (e, ...t) => {
287
+ let n = f(t);
288
+ return f([
289
+ k(e),
290
+ A(n.length),
291
+ n
292
+ ]);
293
+ }, M = (e, t, n = Math.max(1, Math.ceil(Math.log2(t + 1) / 8))) => j(e, p(t, n)), N = (e, t) => {
294
+ let n = /* @__PURE__ */ new Uint8Array(8);
295
+ return new DataView(n.buffer).setFloat64(0, t), j(e, n);
296
+ }, P = 440786851, F = 408125543, I = 290298740, L = 357149030, R = 374648427, z = 524531317, B = 475249515, V = {
297
+ vp8: "V_VP8",
298
+ vp09: "V_VP9",
299
+ av01: "V_AV1"
300
+ }, ee = 3e4, te = () => j(P, M(17030, 1), M(17143, 1), M(17138, 4), M(17139, 8), j(17026, m("webm")), M(17031, 4), M(17029, 2)), ne = (e) => j(L, M(2807729, 1e6), ...e === void 0 ? [] : [N(17545, e / 1e3)], j(19840, m("Margo Engine")), j(22337, m("Margo Engine")));
301
+ function re(e, t, n) {
302
+ let r = e.codec.split(".")[0], i = r === "vp09" ? D(e.codec) : /* @__PURE__ */ new Uint8Array(), a = y(e.colorSpace);
303
+ return j(R, j(174, M(215, 1), M(29637, 1), M(131, 1), M(156, 0), ...e.variable ? [] : [M(2352003, Math.round(1e9 / e.fps))], ...e.alpha ? [M(21998, 1)] : [], j(134, m(V[r])), ...r === "av01" ? [j(25506, S(e, n))] : [], ...i.length ? [j(25506, i)] : [], j(224, M(176, e.width), M(186, e.height), ...e.alpha ? [M(21440, 1)] : [], j(21936, M(21937, a.matrix), M(21943, 1), M(21944, 2), M(21945, a.fullRange ? 2 : 1), M(21946, a.transfer), M(21947, a.primaries)))), ...t ? [j(174, M(215, 2), M(29637, 2), M(131, 2), M(156, 0), j(134, m("A_OPUS")), j(25506, O(t)), M(22186, Math.round(C(t) * 1e9 / 48e3)), M(22203, 8e7), j(225, N(181, w(t)), M(159, t.channels)))] : []);
304
+ }
305
+ var ie = (e) => e ? j(307544935, j(29555, j(25536), j(26568, j(17827, m("MARGO_PROVENANCE")), j(17543, m(JSON.stringify(e)))))) : /* @__PURE__ */ new Uint8Array(), H = (e) => Math.round(e / 1e3);
306
+ function ae(e) {
307
+ let t = [], n = 0, r = 0, i = (e, t, r, i) => {
308
+ let a = /* @__PURE__ */ new Uint8Array(4);
309
+ if (a[0] = 128 | t, new DataView(a.buffer).setInt16(1, r - e.time), !i.alpha) {
310
+ a[3] = i.key ? 128 : 0, e.blocks.push(j(163, a, i.data));
311
+ return;
312
+ }
313
+ let o = n - r, s = o ? Math.ceil((Math.log2(-o) + 1) / 8) : 1;
314
+ e.blocks.push(j(160, j(161, a, i.data), j(30113, j(166, M(238, 1), j(165, i.alpha))), ...i.key ? [] : [j(251, p(2 ** (8 * s) + o, s))]));
315
+ }, a = (n) => {
316
+ for (; r < e.length && H(e[r].timestamp) < n; r++) {
317
+ let n = H(e[r].timestamp), a = t.at(-1);
318
+ (!a || n - a.time > ee) && t.push(a = {
319
+ time: n,
320
+ key: !1,
321
+ blocks: []
322
+ }), i(a, 2, n, e[r]);
323
+ }
324
+ };
325
+ return {
326
+ groups: t,
327
+ hear: a,
328
+ video(e) {
329
+ let r = H(e.timestamp);
330
+ a(r);
331
+ let o = t.at(-1);
332
+ (!o || e.key || r - o.time > ee) && t.push(o = {
333
+ time: r,
334
+ key: e.key,
335
+ blocks: []
336
+ }), i(o, 1, r, e), n = r;
337
+ }
338
+ };
339
+ }
340
+ var oe = (e, t) => j(187, M(179, e), j(183, M(247, 1), M(241, t)));
341
+ function se(e, t, n) {
342
+ let r = E(e.codec, t), i = ne([...r, ...n?.samples ?? []].reduce((e, t) => Math.max(e, t.timestamp + t.duration), 0)), a = re(e, n, r[0]?.data), o = ae(n?.samples ?? []);
343
+ for (let e of r) o.video(e);
344
+ o.hear(Infinity);
345
+ let s = o.groups, c = s.map(({ time: e, blocks: t }) => j(z, M(231, e), ...t)), l = (e, t, n) => j(I, ...[
346
+ [L, e],
347
+ [R, t],
348
+ [B, n]
349
+ ].map(([e, t]) => j(19899, j(21419, k(e)), M(21420, t, 8)))), u = l(0, 0, 0).length, d = [], m = u + i.length + a.length;
350
+ s.forEach((e, t) => {
351
+ e.key && d.push(oe(e.time, m)), m += c[t].length;
352
+ });
353
+ let h = [
354
+ l(u, u + i.length, m),
355
+ i,
356
+ a,
357
+ ...c,
358
+ j(B, ...d),
359
+ ie(e.provenance)
360
+ ], g = p(h.reduce((e, t) => e + t.length, 0), 8);
361
+ return g[0] = 1, f([
362
+ te(),
363
+ k(F),
364
+ g,
365
+ ...h
366
+ ]);
367
+ }
368
+ var ce = class {
369
+ #e;
370
+ #t;
371
+ #n;
372
+ #r = [];
373
+ #i = -1;
374
+ constructor(e, t) {
375
+ this.#e = e, this.#t = t, this.#n = ae(t?.samples ?? []);
376
+ }
377
+ add(e) {
378
+ let [t] = E(this.#e.codec, [e]), n = [];
379
+ if (this.#i < 0) {
380
+ let e = [
381
+ ne(),
382
+ re(this.#e, this.#t, t.data),
383
+ ie(this.#e.provenance)
384
+ ];
385
+ this.#i = e.reduce((e, t) => e + t.length, 0), n.push(f([
386
+ te(),
387
+ k(F),
388
+ Uint8Array.of(1, 255, 255, 255, 255, 255, 255, 255),
389
+ ...e
390
+ ]));
391
+ }
392
+ return t.key && (this.#n.hear(H(t.timestamp)), n.push(...this.#a())), this.#n.video(t), n;
393
+ }
394
+ finish() {
395
+ return this.#i < 0 ? [] : (this.#n.hear(Infinity), [...this.#a(), j(B, ...this.#r)]);
396
+ }
397
+ #a() {
398
+ return this.#n.groups.splice(0).map(({ time: e, key: t, blocks: n }) => {
399
+ t && this.#r.push(oe(e, this.#i));
400
+ let r = j(z, M(231, e), ...n);
401
+ return this.#i += r.length, r;
402
+ });
403
+ }
404
+ }, U = (e) => p(e, 2), W = (e) => p(e, 4), G = (e) => p(e, 8), K = (e) => W(e < 0 ? e + 2 ** 32 : e), q = (e, ...t) => {
405
+ let n = f(t);
406
+ return f([
407
+ W(n.length + 8),
408
+ m(e),
409
+ n
410
+ ]);
411
+ }, J = (e, t, n, ...r) => q(e, Uint8Array.of(t), p(n, 3), ...r), Y = f([
412
+ 65536,
413
+ 0,
414
+ 0,
415
+ 0,
416
+ 65536,
417
+ 0,
418
+ 0,
419
+ 0,
420
+ 1073741824
421
+ ].map(W));
422
+ function le(e, t) {
423
+ let n = e.codec.split(".")[0], r = y(e.colorSpace), i = [
424
+ /* @__PURE__ */ new Uint8Array(6),
425
+ U(1),
426
+ /* @__PURE__ */ new Uint8Array(16),
427
+ U(e.width),
428
+ U(e.height),
429
+ W(4718592),
430
+ W(4718592),
431
+ W(0),
432
+ U(1),
433
+ /* @__PURE__ */ new Uint8Array(32),
434
+ U(24),
435
+ U(65535)
436
+ ], a = q("colr", m("nclx"), U(r.primaries), U(r.transfer), U(r.matrix), Uint8Array.of(r.fullRange ? 128 : 0));
437
+ if (n === "avc1") return q("avc1", ...i, q("avcC", e.description), a);
438
+ if (n === "hvc1" || n === "hev1") return q(n, ...i, q("hvcC", e.description), a);
439
+ if (n === "av01") return q("av01", ...i, q("av1C", S(e, t)), a);
440
+ let [, o, s, c] = e.codec.split(".").map(Number);
441
+ return q("vp09", ...i, J("vpcC", 1, 0, Uint8Array.of(o, s, c << 4 | !!r.fullRange, r.primaries, r.transfer, r.matrix), U(0)), a);
442
+ }
443
+ function X(e) {
444
+ let t = [];
445
+ for (let n of e) {
446
+ let e = t.at(-1);
447
+ e?.[1] === n ? e[0]++ : t.push([1, n]);
448
+ }
449
+ return t;
450
+ }
451
+ function ue(e) {
452
+ let t = [
453
+ /* @__PURE__ */ new Uint8Array(6),
454
+ U(1),
455
+ /* @__PURE__ */ new Uint8Array(8),
456
+ U(e.channels),
457
+ U(16),
458
+ W(0),
459
+ W((e.codec === "opus" ? 48e3 : e.sampleRate) * 65536)
460
+ ];
461
+ if (e.codec === "opus") return q("Opus", ...t, q("dOps", Uint8Array.of(0, e.channels), U(C(e)), W(w(e)), U(0), Uint8Array.of(0)));
462
+ let n = (e, ...t) => {
463
+ let n = f(t);
464
+ return f([Uint8Array.of(e, n.length), n]);
465
+ }, r = n(4, Uint8Array.of(64, 21), /* @__PURE__ */ new Uint8Array(3), W(0), W(0), n(5, e.description));
466
+ return q("mp4a", ...t, J("esds", 0, 0, n(3, U(2), Uint8Array.of(0), r, n(6, Uint8Array.of(2)))));
467
+ }
468
+ function Z(e) {
469
+ let t = e.codec === "opus" ? 48e3 : e.sampleRate, n = e.codec === "opus" ? C(e) : e.leadIn ?? 0, r = e.samples.at(-1), i = Math.round((r.timestamp + r.duration) * t / 1e6);
470
+ return {
471
+ sampleRate: t,
472
+ cut: n,
473
+ end: i,
474
+ milliseconds: Math.round((i - n) / t * 1e3)
475
+ };
476
+ }
477
+ function de(e, t) {
478
+ let { samples: n } = e, { sampleRate: r, cut: i, end: a, milliseconds: o } = Z(e), s = (e) => Math.round(e * r / 1e6), c = n.map((e, t) => (n[t + 1] ? s(n[t + 1].timestamp) : a) - s(e.timestamp)), l = X(c);
479
+ return q("trak", J("tkhd", 0, 3, W(0), W(0), W(2), W(0), W(o), /* @__PURE__ */ new Uint8Array(12), U(256), U(0), Y, W(0), W(0)), ...i ? [q("edts", J("elst", 0, 0, W(1), W(o), W(i), U(1), U(0)))] : [], q("mdia", J("mdhd", 0, 0, W(0), W(0), W(r), W(a), U(21956), U(0)), J("hdlr", 0, 0, W(0), m("soun"), /* @__PURE__ */ new Uint8Array(12), m("Margo Engine\0")), q("minf", J("smhd", 0, 0, W(0)), q("dinf", J("dref", 0, 0, W(1), J("url ", 0, 1))), q("stbl", J("stsd", 0, 0, W(1), ue(e)), J("stts", 0, 0, W(l.length), ...l.flatMap(([e, t]) => [W(e), W(t)])), J("stsc", 0, 0, W(1), W(1), W(n.length), W(1)), J("stsz", 0, 0, W(0), W(n.length), ...n.map((e) => W(e.data.length))), J("co64", 0, 0, W(1), G(t)), J("sgpd", 1, 0, m("roll"), W(2), W(1), U(65536 - (e.codec === "aac" ? 1 : Math.max(1, Math.ceil(3840 / Math.max(1, c[0] ?? 960)))))), J("sbgp", 0, 0, m("roll"), W(1), W(n.length), W(1))))));
480
+ }
481
+ function fe(e, t) {
482
+ let n = e.colorSpace?.fullRange, r = e.description;
483
+ if (!e.codec.startsWith("avc1") || typeof n != "boolean" || !r || r.length < 5 || r[0] !== 1) return {
484
+ track: e,
485
+ samples: t
486
+ };
487
+ let i = (r[4] & 3) + 1;
488
+ return {
489
+ track: {
490
+ ...e,
491
+ description: u(r, n)
492
+ },
493
+ samples: t.map((e) => e.key ? {
494
+ ...e,
495
+ data: d(e.data, i, n)
496
+ } : e)
497
+ };
498
+ }
499
+ function pe(e, t, n) {
500
+ let { track: r, samples: i } = fe(e, E(e.codec, t)), a = r.codec.split(".")[0], o = Math.round(r.fps * 1e3), s = (e) => Math.round(e * o / 1e6), c = i.map((e) => s(e.timestamp)), l = [...c].sort((e, t) => e - t), u = i.reduce((e, t) => Math.max(e, s(t.timestamp + t.duration)), 0), d = l.map((e, t) => (l[t + 1] ?? u) - e), p = c.map((e, t) => e - l[t]), h = Math.round(u / o * 1e3), g = Math.max(h, n ? Z(n).milliseconds : 0), _ = i.flatMap((e, t) => e.key ? [t + 1] : []), v = X(d), y = X(p), b = q("ftyp", m("isom"), W(512), m("isom"), m("iso2"), m("iso6"), m("mp41"), ...a === "avc1" ? [m("avc1")] : [], ...a === "av01" ? [m("av01")] : []), x = (e, t, n) => J(e, t, 0, W(n.length), ...n.flatMap(([e, t]) => [W(e), K(t)])), S = (e) => q("moov", ...r.provenance ? [q("udta", q("marg", m(JSON.stringify(r.provenance))))] : [], J("mvhd", 0, 0, W(0), W(0), W(1e3), W(g), W(65536), U(256), /* @__PURE__ */ new Uint8Array(10), Y, /* @__PURE__ */ new Uint8Array(24), W(n ? 3 : 2)), q("trak", J("tkhd", 0, 3, W(0), W(0), W(1), W(0), W(h), /* @__PURE__ */ new Uint8Array(16), Y, W(r.width * 65536), W(r.height * 65536)), q("mdia", J("mdhd", 0, 0, W(0), W(0), W(o), W(u), U(21956), U(0)), J("hdlr", 0, 0, W(0), m("vide"), /* @__PURE__ */ new Uint8Array(12), m("Margo Engine\0")), q("minf", J("vmhd", 0, 1, /* @__PURE__ */ new Uint8Array(8)), q("dinf", J("dref", 0, 0, W(1), J("url ", 0, 1))), q("stbl", J("stsd", 0, 0, W(1), le(r, i[0]?.data)), x("stts", 0, v), ...p.some(Boolean) ? [x("ctts", 1, y)] : [], J("stss", 0, 0, W(_.length), ..._.map(W)), J("stsc", 0, 0, W(1), W(1), W(i.length), W(1)), J("stsz", 0, 0, W(0), W(i.length), ...i.map((e) => W(e.data.length))), J("co64", 0, 0, W(1), G(e)))))), ...n ? [de(n, e + C)] : []), C = i.reduce((e, t) => e + t.data.length, 0), w = (n?.samples ?? []).reduce((e, t) => e + t.data.length, 0), T = f([
501
+ W(1),
502
+ m("mdat"),
503
+ G(C + w + 16)
504
+ ]);
505
+ return f([
506
+ b,
507
+ S(b.length + S(0).length + T.length),
508
+ T,
509
+ ...i.map((e) => e.data),
510
+ ...(n?.samples ?? []).map((e) => e.data)
511
+ ]);
512
+ }
513
+ var me = 33554432, he = 16842752, ge = (e, ...t) => q("stbl", J("stsd", 0, 0, W(1), e), J("stts", 0, 0, W(0)), J("stsc", 0, 0, W(0)), J("stsz", 0, 0, W(0), W(0)), J("stco", 0, 0, W(0)), ...t);
514
+ function _e({ track: e, decodeTime: t, samples: n, rolls: r }, i) {
515
+ let a = n.some((e) => e.offset !== 0);
516
+ return q("traf", J("tfhd", 0, 131072, W(e)), J("tfdt", 1, 0, G(t)), J("trun", 1, 1793 | (a ? 2048 : 0), W(n.length), K(i), ...n.flatMap((e) => [
517
+ W(e.duration),
518
+ W(e.data.length),
519
+ W(e.flags),
520
+ ...a ? [K(e.offset)] : []
521
+ ])), ...r ? [J("sbgp", 0, 0, m("roll"), W(1), W(n.length), W(1))] : []);
522
+ }
523
+ var ve = class {
524
+ #e;
525
+ #t;
526
+ #n;
527
+ #r = [];
528
+ #i = [];
529
+ #a = [];
530
+ #o = 0;
531
+ #s = 0;
532
+ #c = 0;
533
+ #l = !1;
534
+ #u = [];
535
+ constructor(e, t) {
536
+ if (this.#e = fe(e, []).track, this.#t = t, this.#n = Math.round(e.fps * 1e3), t) {
537
+ let { sampleRate: e, end: n } = Z(t), r = t.samples.map((t) => Math.round(t.timestamp * e / 1e6));
538
+ this.#r.push(...r), this.#i.push(...r.map((e, t) => (r[t + 1] ?? n) - e));
539
+ }
540
+ }
541
+ add(e) {
542
+ let t = [];
543
+ return this.#l || (this.#l = !0, t.push(this.#d(this.#f(e)))), e.key && this.#a.length && t.push(this.#p(e.timestamp)), this.#a.push(e), t;
544
+ }
545
+ finish() {
546
+ if (!this.#l) return [];
547
+ let e = this.#a.length ? [this.#p(void 0)] : [];
548
+ return e.push(this.#d(this.#m())), e;
549
+ }
550
+ #d(e) {
551
+ return this.#c += e.length, e;
552
+ }
553
+ #f(e) {
554
+ let t = this.#e, n = this.#t, r = t.codec.split(".")[0], i = E(t.codec, [e])[0].data, a = q("ftyp", m("iso6"), W(0), m("iso6"), m("iso5"), m("isom"), m("mp41"), ...r === "avc1" ? [m("avc1")] : [], ...r === "av01" ? [m("av01")] : []), o = (e) => J("hdlr", 0, 0, W(0), m(e), /* @__PURE__ */ new Uint8Array(12), m("Margo Engine\0")), s = q("dinf", J("dref", 0, 0, W(1), J("url ", 0, 1))), c = (e) => J("trex", 0, 0, W(e), W(1), W(0), W(0), W(0)), l = [];
555
+ if (n) {
556
+ let { sampleRate: e, cut: t } = Z(n), r = 65536 - (n.codec === "aac" ? 1 : Math.max(1, Math.ceil(3840 / Math.max(1, this.#i[0] ?? 960))));
557
+ l = [q("trak", J("tkhd", 0, 3, W(0), W(0), W(2), W(0), W(0), /* @__PURE__ */ new Uint8Array(12), U(256), U(0), Y, W(0), W(0)), ...t ? [q("edts", J("elst", 0, 0, W(1), W(0), W(t), U(1), U(0)))] : [], q("mdia", J("mdhd", 0, 0, W(0), W(0), W(e), W(0), U(21956), U(0)), o("soun"), q("minf", J("smhd", 0, 0, W(0)), s, ge(ue(n), J("sgpd", 1, 0, m("roll"), W(2), W(1), U(r))))))];
558
+ }
559
+ return f([a, q("moov", ...t.provenance ? [q("udta", q("marg", m(JSON.stringify(t.provenance))))] : [], J("mvhd", 0, 0, W(0), W(0), W(1e3), W(0), W(65536), U(256), /* @__PURE__ */ new Uint8Array(10), Y, /* @__PURE__ */ new Uint8Array(24), W(n ? 3 : 2)), q("trak", J("tkhd", 0, 3, W(0), W(0), W(1), W(0), W(0), /* @__PURE__ */ new Uint8Array(16), Y, W(t.width * 65536), W(t.height * 65536)), q("mdia", J("mdhd", 0, 0, W(0), W(0), W(this.#n), W(0), U(21956), U(0)), o("vide"), q("minf", J("vmhd", 0, 1, /* @__PURE__ */ new Uint8Array(8)), s, ge(le(t, i))))), ...l, q("mvex", c(1), ...n ? [c(2)] : []))]);
560
+ }
561
+ #p(e) {
562
+ let { samples: t } = fe(this.#e, E(this.#e.codec, this.#a)), n = this.#n, r = (e) => Math.round(e * n / 1e6), i = t.map((e) => r(e.timestamp)), a = [...i].sort((e, t) => e - t), o = e === void 0 ? t.reduce((e, t) => Math.max(e, r(t.timestamp + t.duration)), 0) : r(e), s = [{
563
+ track: 1,
564
+ decodeTime: a[0],
565
+ samples: t.map((e, t) => ({
566
+ data: e.data,
567
+ duration: (a[t + 1] ?? o) - a[t],
568
+ flags: e.key ? me : he,
569
+ offset: i[t] - a[t]
570
+ }))
571
+ }], c = this.#t;
572
+ if (c) {
573
+ let t = this.#o;
574
+ for (; this.#o < c.samples.length && (e === void 0 || c.samples[this.#o].timestamp < e);) this.#o++;
575
+ this.#o > t && s.push({
576
+ track: 2,
577
+ decodeTime: this.#r[t],
578
+ rolls: !0,
579
+ samples: c.samples.slice(t, this.#o).map((e, n) => ({
580
+ data: e.data,
581
+ duration: this.#i[t + n],
582
+ flags: me,
583
+ offset: 0
584
+ }))
585
+ });
586
+ }
587
+ this.#u.push({
588
+ time: i[0],
589
+ position: this.#c
590
+ }), this.#a = [];
591
+ let l = ++this.#s, u = (e) => q("moof", J("mfhd", 0, 0, W(l)), ...s.map((t, n) => _e(t, e[n]))), d = s.map((e) => e.samples.reduce((e, t) => e + t.data.length, 0)), p = u(s.map(() => 0)).length + 16, h = d.reduce((e, t) => e + t, 0), g = d.map((e, t) => p + d.slice(0, t).reduce((e, t) => e + t, 0));
592
+ return this.#d(f([
593
+ u(g),
594
+ W(1),
595
+ m("mdat"),
596
+ G(h + 16),
597
+ ...s.flatMap((e) => e.samples.map((e) => e.data))
598
+ ]));
599
+ }
600
+ #m() {
601
+ let e = J("tfra", 1, 0, W(1), W(0), W(this.#u.length), ...this.#u.flatMap(({ time: e, position: t }) => [
602
+ G(e),
603
+ G(t),
604
+ Uint8Array.of(1, 1, 1)
605
+ ]));
606
+ return q("mfra", e, J("mfro", 0, 0, W(e.length + 8 + 16)));
607
+ }
608
+ }, ye = {
609
+ primaries: "bt709",
610
+ transfer: "iec61966-2-1",
611
+ matrix: "bt709",
612
+ fullRange: !1
613
+ };
614
+ function be(e, t, n, r = t, i = n) {
615
+ let a = r + 1 >> 1, o = i + 1 >> 1, s = r * i, c = new Uint8Array(s + 2 * a * o), l = (e, t, n) => .2126 * e + .7152 * t + .0722 * n;
616
+ for (let a = 0, o = 0; a < i; a++) for (let i = 0; i < r; i++, o++) {
617
+ let r = (Math.min(a, n - 1) * t + Math.min(i, t - 1)) * 4;
618
+ c[o] = Math.round(16 + 219 / 255 * l(e[r], e[r + 1], e[r + 2]));
619
+ }
620
+ for (let r = 0; r < o; r++) for (let i = 0; i < a; i++) {
621
+ let u = 0, d = 0, f = 0;
622
+ for (let a = 0; a < 2; a++) {
623
+ let o = Math.min(n - 1, 2 * r + a) * t;
624
+ for (let n = -1; n <= 1; n++) {
625
+ let r = (o + Math.min(t - 1, Math.max(0, 2 * i + n))) * 4, a = n ? 1 : 2;
626
+ u += a * e[r], d += a * e[r + 1], f += a * e[r + 2];
627
+ }
628
+ }
629
+ let p = l(u, d, f), m = s + r * a + i;
630
+ c[m] = Math.round(128 + 224 / 255 * ((f - p) / 8 / 1.8556)), c[m + a * o] = Math.round(128 + 224 / 255 * ((u - p) / 8 / 1.5748));
631
+ }
632
+ return c;
633
+ }
634
+ function xe(e, t, n) {
635
+ let r = t * n, i = new Uint8Array(r + 2 * (t + 1 >> 1) * (n + 1 >> 1)).fill(128, r);
636
+ for (let t = 0; t < r; t++) i[t] = e[t * 4 + 3];
637
+ return i;
638
+ }
639
+ var Se = (e) => {
640
+ if (!(e > 0) || !Number.isFinite(e)) throw n("Invalid fps", "headless");
641
+ };
642
+ async function* Ce(t, r) {
643
+ let { frames: i, fps: a = 30, camera: o, update: s, signal: c, samples: l = 1, shutter: u = 0 } = r;
644
+ if (l !== 1 || u !== 0) {
645
+ let { renderAccumulated: e } = await import("@margoengine/headless/accumulate");
646
+ yield* e(t, {
647
+ ...r,
648
+ samples: l,
649
+ shutter: u,
650
+ output: "display"
651
+ });
652
+ } else {
653
+ if (!Number.isInteger(i) || i < 1) throw n("Invalid frame count", "headless");
654
+ Se(a);
655
+ for (let n = 0; n < i; n++) {
656
+ c?.throwIfAborted();
657
+ let r = n ? 1 / a : 0, l = {
658
+ index: n,
659
+ time: n / a,
660
+ delta: r,
661
+ progress: n / i
662
+ };
663
+ await s?.(l), t.render({
664
+ camera: o,
665
+ delta: r
666
+ }), yield {
667
+ ...l,
668
+ ...await e(t, { signal: c })
669
+ };
670
+ }
671
+ }
672
+ }
673
+ async function* we(e, { prefix: n = "frame_", digits: r = 4 } = {}) {
674
+ let i = 0;
675
+ for await (let a of e) yield {
676
+ index: i,
677
+ name: `${n}${String(i).padStart(r, "0")}.png`,
678
+ bytes: await t(a)
679
+ }, i++;
680
+ }
681
+ var Te = {
682
+ webm: [
683
+ "vp09.00.51.08",
684
+ "vp8",
685
+ "av01.0.13M.08"
686
+ ],
687
+ mp4: [
688
+ "avc1.640033",
689
+ "avc1.4d0033",
690
+ "avc1.420033",
691
+ "vp09.00.51.08",
692
+ "av01.0.13M.08"
693
+ ]
694
+ }, Ee = {
695
+ webm: [
696
+ "vp8",
697
+ "vp09",
698
+ "av01"
699
+ ],
700
+ mp4: [
701
+ "avc1",
702
+ "hvc1",
703
+ "hev1",
704
+ "vp09",
705
+ "av01"
706
+ ]
707
+ }, De = /^(vp8|vp09\.)/, Oe = 4, ke = .1, Ae = 1e5;
708
+ async function je(e, t, r, a, o) {
709
+ for (let i of r === void 0 ? Te[t].filter((e) => !a || De.test(e)) : [r]) {
710
+ if (!Ee[t].includes(i.split(".")[0])) throw n("This codec does not fit this container", "headless");
711
+ let r = /^(avc1|hvc1|hev1)\./.test(i), a = {
712
+ ...o,
713
+ ...r ? {
714
+ width: o.width + (o.width & 1),
715
+ height: o.height + (o.height & 1)
716
+ } : {},
717
+ codec: i,
718
+ ...i.startsWith("avc1") ? { avc: { format: "avc" } } : {},
719
+ .../^(hvc1|hev1)\./.test(i) ? { hevc: { format: "hevc" } } : {}
720
+ };
721
+ if ((await e.isConfigSupported(a)).supported) return a;
722
+ }
723
+ throw i("unsupported", "No video codec is available for this format and size", "headless");
724
+ }
725
+ var Q = (e) => ArrayBuffer.isView(e) ? new Uint8Array(e.buffer, e.byteOffset, e.byteLength).slice() : new Uint8Array(e).slice();
726
+ function Me(e) {
727
+ if (e[0] !== 3) return e;
728
+ let t = () => {
729
+ throw i("failed", "The AAC encoder gave an ES_Descriptor without a DecSpecificInfo", "headless");
730
+ }, n = 0, r = (r) => {
731
+ e[n++] !== r && t();
732
+ let i = 0;
733
+ for (let r = 0; r < 4; r++) {
734
+ let r = e[n++] ?? t();
735
+ if (i = i * 128 + (r & 127), !(r & 128)) return n + i <= e.length ? n + i : t();
736
+ }
737
+ return t();
738
+ };
739
+ r(3);
740
+ let a = e[n + 2] ?? t();
741
+ n += 3, a & 128 && (n += 2), a & 64 && (n += 1 + (e[n] ?? t())), a & 32 && (n += 2);
742
+ let o = r(4);
743
+ n += 13, n >= o && t();
744
+ let s = r(5);
745
+ return s <= o ? e.slice(n, s) : t();
746
+ }
747
+ var Ne = {
748
+ webm: [["opus", "opus"]],
749
+ mp4: [["mp4a.40.2", "aac"], ["opus", "opus"]]
750
+ }, Pe = .5, $ = 8192, Fe = 4096;
751
+ async function Ie(e, { AudioEncoder: t, AudioData: n, AudioDecoder: r }) {
752
+ if (!t || !n || !r) return 0;
753
+ let a = new Float32Array($), o = e.numberOfChannels;
754
+ for (let e = 0, t = 1; e < $; e++) t = Math.imul(t, 1664525) + 1013904223 >>> 0, a[e] = (t / 2 ** 32 - .5) * .6;
755
+ let s = [], c = [], l, u, d = !1, f = (e) => {
756
+ u = e, d = !0;
757
+ }, p = new t({
758
+ output(e, t) {
759
+ s.push(e), t?.decoderConfig && (l = t.decoderConfig);
760
+ },
761
+ error: f
762
+ });
763
+ try {
764
+ p.configure(e);
765
+ let t = new Float32Array($ * o);
766
+ for (let e = 0; e < o; e++) t.set(a, e * $);
767
+ let r = new n({
768
+ format: "f32-planar",
769
+ sampleRate: e.sampleRate,
770
+ numberOfFrames: $,
771
+ numberOfChannels: o,
772
+ timestamp: 0,
773
+ data: t
774
+ });
775
+ try {
776
+ p.encode(r);
777
+ } finally {
778
+ r.close();
779
+ }
780
+ await p.flush();
781
+ } finally {
782
+ p.state !== "closed" && p.close();
783
+ }
784
+ if (d) throw u;
785
+ if (!l) throw i("failed", "The audio encoder gave no decoder configuration", "headless");
786
+ l.description && (l = {
787
+ ...l,
788
+ description: Me(Q(l.description))
789
+ });
790
+ let m = new r({
791
+ output(e) {
792
+ let t = new Float32Array(e.numberOfFrames);
793
+ e.copyTo(t, {
794
+ planeIndex: 0,
795
+ format: "f32-planar"
796
+ }), c.push(t), e.close();
797
+ },
798
+ error: f
799
+ });
800
+ try {
801
+ m.configure(l);
802
+ for (let e of s) m.decode(e);
803
+ await m.flush();
804
+ } finally {
805
+ m.state !== "closed" && m.close();
806
+ }
807
+ if (d) throw u;
808
+ let h = new Float32Array(c.reduce((e, t) => e + t.length, 0)), g = 4096;
809
+ c.reduce((e, t) => (h.set(t, e), e + t.length), 0);
810
+ let _ = 0, v = -Infinity;
811
+ for (let e = 0; e <= Fe && e + g <= h.length; e++) {
812
+ let t = 0;
813
+ for (let n = 0; n < g; n++) t += a[n] * h[n + e];
814
+ t > v && (v = t, _ = e);
815
+ }
816
+ return _;
817
+ }
818
+ async function Le({ channels: e, sampleRate: t, bitrate: r = 128e3 }, a, o, s) {
819
+ let { AudioEncoder: c, AudioData: l } = o;
820
+ if (!c || !l) throw i("unsupported", "The audio option needs AudioEncoder", "headless");
821
+ if (e.length !== 1 && e.length !== 2) throw n("Audio takes one or two channels", "headless");
822
+ let u = e[0].length;
823
+ if (!u || e.some((e) => e.length !== u)) throw n("Audio channels must be one length, above 0", "headless");
824
+ if (!(t > 0) || !Number.isFinite(t)) throw n("Invalid audio sampleRate", "headless");
825
+ if (!(r > 0) || !Number.isFinite(r)) throw n("Invalid audio bitrate", "headless");
826
+ let d = {
827
+ sampleRate: t,
828
+ numberOfChannels: e.length,
829
+ bitrate: r
830
+ }, f, p;
831
+ for (let [e, t] of Ne[a]) if ((await c.isConfigSupported({
832
+ ...d,
833
+ codec: e
834
+ })).supported) {
835
+ f = {
836
+ ...d,
837
+ codec: e
838
+ }, p = t;
839
+ break;
840
+ }
841
+ if (!f || !p) throw i("unsupported", "No audio codec is available for this format", "headless");
842
+ let m = [], h, g, _ = !1, v = new c({
843
+ output(e, t) {
844
+ let n = new Uint8Array(e.byteLength);
845
+ e.copyTo(n), m.push({
846
+ data: n,
847
+ timestamp: e.timestamp,
848
+ duration: e.duration ?? 0,
849
+ key: !0
850
+ });
851
+ let r = t?.decoderConfig;
852
+ r?.description && (h = p === "aac" ? Me(Q(r.description)) : Q(r.description));
853
+ },
854
+ error(e) {
855
+ g = e, _ = !0;
856
+ }
857
+ });
858
+ try {
859
+ v.configure(f);
860
+ let n = Math.round(Pe * t);
861
+ for (let r = 0; r < u; r += n) {
862
+ if (s?.throwIfAborted(), _) throw g;
863
+ let i = Math.min(n, u - r), a = new Float32Array(i * e.length);
864
+ e.forEach((e, t) => a.set(e.subarray(r, r + i), t * i));
865
+ let o = new l({
866
+ format: "f32-planar",
867
+ sampleRate: t,
868
+ numberOfFrames: i,
869
+ numberOfChannels: e.length,
870
+ timestamp: Math.round(r * 1e6 / t),
871
+ data: a
872
+ });
873
+ try {
874
+ v.encode(o);
875
+ } finally {
876
+ o.close();
877
+ }
878
+ }
879
+ if (await v.flush(), _) throw g;
880
+ } finally {
881
+ v.state !== "closed" && v.close();
882
+ }
883
+ if (m.length && m.every((e) => e.duration > 0)) {
884
+ let e = Math.round(m[0].timestamp * t / 1e6);
885
+ for (let n of m) n.timestamp = Math.round(e * 1e6 / t), e += Math.round(n.duration * t / 1e6);
886
+ }
887
+ if (p === "aac" && !h) throw i("failed", "The encoder gave no AAC description", "headless");
888
+ let y = p === "aac" ? await Ie(f, o) : 0;
889
+ return {
890
+ codec: p,
891
+ sampleRate: t,
892
+ channels: e.length,
893
+ description: h,
894
+ samples: m,
895
+ ...y ? { leadIn: y } : {}
896
+ };
897
+ }
898
+ function Re({ format: e, fps: t, maxBytes: r, keyFrameInterval: a, bitrate: o, webCodecs: s, alpha: c, codec: l }) {
899
+ if (Se(t), c && (e !== "webm" || l !== void 0 && !De.test(l))) throw i("unsupported", "Alpha video needs WebM with VP9 or VP8", "headless");
900
+ if (r !== Infinity && (!Number.isSafeInteger(r) || r < 1)) throw n("maxBytes must be a positive safe integer", "headless");
901
+ if (!(a > 0) || !Number.isFinite(a)) throw n("Invalid keyFrameInterval", "headless");
902
+ if (o !== void 0 && (!(o > 0) || !Number.isFinite(o))) throw n("Invalid bitrate", "headless");
903
+ if (!s?.VideoEncoder || !s.VideoFrame) throw i("unsupported", "Encoding video needs WebCodecs", "headless");
904
+ }
905
+ async function* ze(e, { format: t, fps: r, codec: a, bitrate: o, keyFrameInterval: s, maxBytes: c, webCodecs: l, alpha: u, signal: d }) {
906
+ let { VideoEncoder: f, VideoFrame: p } = l;
907
+ d?.throwIfAborted();
908
+ let m = [], h = Math.max(1, Math.round(s * r)), g = (e) => Math.round(e * 1e6 / r), _ = [], v = [], y, b, x, S = 0, C = 0, w, T, E = !1, D = () => {}, O = 0, k = 0, A, j = 0, M = 0, N = 0, P = -Infinity, F, I = 0, L = () => {
909
+ if (d?.throwIfAborted(), E) throw T;
910
+ }, R = () => D(), z = new Promise((e, t) => {
911
+ d?.addEventListener("abort", () => t(d.reason), { once: !0 });
912
+ }), B = () => ({
913
+ samples: m.splice(0),
914
+ config: x,
915
+ description: w,
916
+ timed: !!A
917
+ });
918
+ z.catch(() => {}), d?.addEventListener("abort", R, { once: !0 });
919
+ try {
920
+ let l = async (e, t, n, r, i) => {
921
+ for (; y.encodeQueueSize >= Oe || (b?.encodeQueueSize ?? 0) >= Oe;) await new Promise((e) => D = e), L();
922
+ let a = Math.round(n * 1e6);
923
+ for (let [o, s] of [[y, e], [b, t]]) {
924
+ if (!o || !s) continue;
925
+ let e = new p(s, {
926
+ format: "I420",
927
+ codedWidth: x.width,
928
+ codedHeight: x.height,
929
+ colorSpace: ye,
930
+ timestamp: a,
931
+ duration: Math.round((n + r) * 1e6) - a
932
+ });
933
+ try {
934
+ o.encode(e, { keyFrame: i });
935
+ } finally {
936
+ e.close();
937
+ }
938
+ }
939
+ I = n + r;
940
+ }, d = async (e) => {
941
+ let { planes: t, alpha: n, time: r } = F, i = r - P >= s - 1e-9;
942
+ i && (P = r), F = void 0, await l(t, n, r, e, i);
943
+ };
944
+ for await (let s of e) {
945
+ let { width: e, height: p, data: P } = s, I = s.timestamp !== void 0;
946
+ if (L(), A ??= I, I !== A) throw n("Give every video frame a timestamp, or none of them", "headless");
947
+ if (A) {
948
+ let { timestamp: e, duration: t } = s;
949
+ if (typeof e != "number" || !Number.isFinite(e)) throw n("A video frame timestamp is a finite number of seconds", "headless");
950
+ if (t !== void 0 && !(typeof t == "number" && t > 0 && Number.isFinite(t))) throw n("A video frame duration is a positive number of seconds", "headless");
951
+ if (O === 0) j = e;
952
+ else if (!(e - j > M)) throw n("Video frame timestamps must increase", "headless");
953
+ }
954
+ if (!y) {
955
+ [S, C] = [e, p], x = await je(f, t, a, u, {
956
+ width: e,
957
+ height: p,
958
+ framerate: r,
959
+ bitrate: o ?? Math.max(Ae, Math.round(e * p * r * ke)),
960
+ latencyMode: "quality"
961
+ }), L();
962
+ let s = (e) => {
963
+ T = e, E = !0, D();
964
+ }, l = (e) => (t, r) => {
965
+ if (k += t.byteLength, k > c) return s(n("Encoded video exceeds maxBytes", "headless"));
966
+ let a = new Uint8Array(t.byteLength);
967
+ t.copyTo(a), e.push({
968
+ data: a,
969
+ timestamp: t.timestamp,
970
+ duration: t.duration ?? 0,
971
+ key: t.type === "key"
972
+ });
973
+ let o = r?.decoderConfig;
974
+ for (e === _ && o?.description && (w = Q(o.description)); _.length && (!u || v.length);) {
975
+ let e = _.shift(), t = v.shift();
976
+ if (t && t.timestamp !== e.timestamp) return s(i("failed", "The alpha encoder gave a frame the colour encoder did not", "headless"));
977
+ m.push(t ? {
978
+ ...e,
979
+ key: e.key && t.key,
980
+ alpha: t.data
981
+ } : e);
982
+ }
983
+ D();
984
+ };
985
+ y = new f({
986
+ output: l(_),
987
+ error: s
988
+ }), y.addEventListener("dequeue", R), y.configure(x), u && (b = new f({
989
+ output: l(v),
990
+ error: s
991
+ }), b.addEventListener("dequeue", R), b.configure(x));
992
+ } else if (e !== S || p !== C) throw n("Video frames must all be one size", "headless");
993
+ if (P.length !== e * p * 4) throw n("Frame data does not match its size", "headless");
994
+ let z = be(P, e, p, x.width, x.height), V = u ? xe(P, e, p) : void 0;
995
+ if (A) {
996
+ let e = s.timestamp - j;
997
+ F && (N = e - F.time, await d(N)), F = {
998
+ planes: z,
999
+ alpha: V,
1000
+ time: e,
1001
+ duration: s.duration
1002
+ }, M = e;
1003
+ } else await l(z, V, g(O) / 1e6, (g(O + 1) - g(O)) / 1e6, O % h === 0);
1004
+ O++, m.length && (yield B());
1005
+ }
1006
+ if (!y) throw i("state", "No frames to encode", "headless");
1007
+ if (F && await d(F.duration ?? (O > 1 ? N : 1 / r)), await Promise.race([Promise.all([y.flush(), b?.flush()]), z]), L(), _.length || v.length) throw i("failed", "The alpha encoder gave a different number of frames than the colour encoder", "headless");
1008
+ m.length && (yield B());
1009
+ } finally {
1010
+ d?.removeEventListener("abort", R);
1011
+ for (let e of [y, b]) e && e.state !== "closed" && e.close();
1012
+ }
1013
+ let V = x.codec;
1014
+ if (V.startsWith("avc1") && !w) throw i("failed", "The encoder gave no H.264 description", "headless");
1015
+ if ((V.startsWith("hvc1") || V.startsWith("hev1")) && !w) throw i("failed", "The encoder gave no H.265 description", "headless");
1016
+ return {
1017
+ config: x,
1018
+ description: w,
1019
+ end: I,
1020
+ timed: !!A
1021
+ };
1022
+ }
1023
+ var Be = (e, t, n) => `video/${e}; codecs="${t}${n ? n.codec === "aac" ? ",mp4a.40.2" : ",opus" : ""}"`;
1024
+ async function Ve(e, { format: t = "webm", fps: n = 30, codec: i, bitrate: a, keyFrameInterval: o = 2, audio: s, alpha: c = !1, provenance: l, maxBytes: u = 536870912, webCodecs: d = globalThis, signal: f } = {}) {
1025
+ if (t !== "webm" && t !== "mp4") throw r("Video format: webm or mp4", "headless");
1026
+ let p = {
1027
+ format: t,
1028
+ fps: n,
1029
+ codec: i,
1030
+ bitrate: a,
1031
+ keyFrameInterval: o,
1032
+ maxBytes: u,
1033
+ webCodecs: d,
1034
+ alpha: c,
1035
+ signal: f
1036
+ };
1037
+ Re(p);
1038
+ let m = ze(e, p), h = [], g;
1039
+ for (; !(g = await m.next()).done;) for (let e of g.value.samples) h.push(e);
1040
+ let { config: { codec: _, width: v, height: y }, description: b, end: x, timed: S } = g.value, C = {
1041
+ codec: _,
1042
+ width: v,
1043
+ height: y,
1044
+ fps: n,
1045
+ description: b,
1046
+ colorSpace: ye,
1047
+ provenance: l,
1048
+ ...S ? { variable: !0 } : {},
1049
+ ...c ? { alpha: c } : {}
1050
+ }, w = s ? await Le(s, t, d, f) : void 0;
1051
+ return {
1052
+ bytes: t === "webm" ? se(C, h, w) : pe(C, h, w),
1053
+ mimeType: Be(t, _, w),
1054
+ codec: _,
1055
+ width: v,
1056
+ height: y,
1057
+ frames: h.length,
1058
+ duration: Math.round(x * 1e6) / 1e6,
1059
+ ...w ? { audioCodec: w.codec } : {},
1060
+ ...c ? { alpha: c } : {}
1061
+ };
1062
+ }
1063
+ async function* He(e, t = {}) {
1064
+ let { format: n = "mp4", fps: i = 30, codec: a, bitrate: o, keyFrameInterval: s = 2, audio: c, alpha: l = !1, provenance: u, webCodecs: d = globalThis, signal: f } = t;
1065
+ if (n !== "webm" && n !== "mp4") throw r("Video format: webm or mp4", "headless");
1066
+ let p = {
1067
+ format: n,
1068
+ fps: i,
1069
+ codec: a,
1070
+ bitrate: o,
1071
+ keyFrameInterval: s,
1072
+ maxBytes: Infinity,
1073
+ webCodecs: d,
1074
+ alpha: l,
1075
+ signal: f
1076
+ };
1077
+ Re(p);
1078
+ let m = c ? await Le(c, n, d, f) : void 0, h = ze(e, p), g, _ = 0, v;
1079
+ try {
1080
+ for (; !(v = await h.next()).done;) {
1081
+ let { samples: e, config: t, description: r, timed: a } = v.value;
1082
+ for (let o of e) {
1083
+ g ??= new (n === "webm" ? ce : ve)({
1084
+ codec: t.codec,
1085
+ width: t.width,
1086
+ height: t.height,
1087
+ fps: i,
1088
+ description: r,
1089
+ colorSpace: ye,
1090
+ provenance: u,
1091
+ ...a ? { variable: !0 } : {},
1092
+ ...l ? { alpha: l } : {}
1093
+ }, m), _++;
1094
+ for (let e of g.add(o)) yield e;
1095
+ }
1096
+ }
1097
+ } finally {
1098
+ await h.return(void 0);
1099
+ }
1100
+ let { config: { codec: y, width: b, height: x }, end: S } = v.value;
1101
+ for (let e of g.finish()) yield e;
1102
+ return {
1103
+ mimeType: Be(n, y, m),
1104
+ codec: y,
1105
+ width: b,
1106
+ height: x,
1107
+ frames: _,
1108
+ duration: Math.round(S * 1e6) / 1e6,
1109
+ ...m ? { audioCodec: m.codec } : {},
1110
+ ...l ? { alpha: l } : {}
1111
+ };
1112
+ }
1113
+ //#endregion
1114
+ export { Ve as encodeVideo, we as imageSequence, Ce as renderFrames, He as streamVideo };