@alpha2-sh/editor 0.4.6 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/dist/browser/bridge.d.ts +15 -16
  2. package/dist/browser/bridge.js +393 -312
  3. package/dist/browser/extensions-worker.d.ts +12 -16
  4. package/dist/browser/extensions-worker.js +17 -24
  5. package/dist/browser/hash-worker.d.ts +4 -5
  6. package/dist/browser/hash-worker.js +7 -8
  7. package/dist/browser/jobs.d.ts +17 -19
  8. package/dist/browser/jobs.js +41 -28
  9. package/dist/browser/resources.d.ts +6 -11
  10. package/dist/browser/resources.js +5 -9
  11. package/dist/browser/runtime/assets.d.ts +52 -28
  12. package/dist/browser/runtime/assets.js +136 -45
  13. package/dist/browser/runtime/audio.d.ts +16 -11
  14. package/dist/browser/runtime/audio.js +102 -59
  15. package/dist/browser/runtime/budget.d.ts +4 -5
  16. package/dist/browser/runtime/budget.js +4 -5
  17. package/dist/browser/runtime/caption-fonts.d.ts +3 -7
  18. package/dist/browser/runtime/caption-fonts.js +4 -8
  19. package/dist/browser/runtime/engine.d.ts +44 -44
  20. package/dist/browser/runtime/engine.js +87 -64
  21. package/dist/browser/runtime/export-audio.d.ts +6 -6
  22. package/dist/browser/runtime/export-audio.js +8 -9
  23. package/dist/browser/runtime/exporter.d.ts +33 -66
  24. package/dist/browser/runtime/exporter.js +111 -137
  25. package/dist/browser/runtime/extension-import.d.ts +28 -35
  26. package/dist/browser/runtime/extension-import.js +36 -46
  27. package/dist/browser/runtime/fingerprint.d.ts +11 -14
  28. package/dist/browser/runtime/fingerprint.js +20 -24
  29. package/dist/browser/runtime/generators.d.ts +2 -3
  30. package/dist/browser/runtime/generators.js +5 -7
  31. package/dist/browser/runtime/locks.d.ts +5 -7
  32. package/dist/browser/runtime/locks.js +5 -7
  33. package/dist/browser/runtime/output.d.ts +7 -9
  34. package/dist/browser/runtime/output.js +17 -21
  35. package/dist/browser/runtime/preview-utils.js +10 -6
  36. package/dist/browser/runtime/proxies.d.ts +19 -19
  37. package/dist/browser/runtime/proxies.js +72 -51
  38. package/dist/browser/runtime/proxy-storage.d.ts +15 -19
  39. package/dist/browser/runtime/proxy-storage.js +20 -24
  40. package/dist/browser/runtime/sources.d.ts +23 -31
  41. package/dist/browser/runtime/sources.js +85 -103
  42. package/dist/browser/runtime/staging.d.ts +2 -6
  43. package/dist/browser/runtime/staging.js +7 -11
  44. package/dist/browser/runtime/still.d.ts +2 -6
  45. package/dist/browser/runtime/still.js +6 -12
  46. package/dist/browser/runtime/text.d.ts +14 -0
  47. package/dist/browser/runtime/text.js +27 -0
  48. package/dist/browser/runtime/wasm.d.ts +4 -5
  49. package/dist/browser/runtime/wasm.js +4 -5
  50. package/dist/browser/types.d.ts +244 -239
  51. package/dist/browser/worker.d.ts +10 -10
  52. package/dist/browser/worker.js +64 -22
  53. package/dist/core/captions.d.ts +8 -10
  54. package/dist/core/captions.js +10 -18
  55. package/dist/core/clip-time.d.ts +8 -10
  56. package/dist/core/clip-time.js +12 -15
  57. package/dist/core/commands.d.ts +46 -46
  58. package/dist/core/commands.js +38 -36
  59. package/dist/core/compositions.d.ts +78 -112
  60. package/dist/core/compositions.js +66 -94
  61. package/dist/core/document.d.ts +59 -92
  62. package/dist/core/document.js +65 -90
  63. package/dist/core/keyframes.d.ts +11 -12
  64. package/dist/core/keyframes.js +11 -13
  65. package/dist/core/presets.d.ts +14 -20
  66. package/dist/core/presets.js +12 -18
  67. package/dist/core/session.d.ts +26 -38
  68. package/dist/core/session.js +38 -53
  69. package/dist/core/text.d.ts +2 -3
  70. package/dist/core/text.js +2 -3
  71. package/dist/core/timeline-index.d.ts +2 -3
  72. package/dist/core/timeline-index.js +11 -7
  73. package/dist/core/transitions.js +4 -5
  74. package/dist/core/validated.d.ts +6 -7
  75. package/dist/core/validated.js +8 -11
  76. package/dist/native-client/index.d.ts +31 -32
  77. package/dist/native-client/index.js +40 -41
  78. package/dist/ports/index.d.ts +19 -20
  79. package/dist/react/index.d.ts +5 -6
  80. package/dist/react/index.js +5 -6
  81. package/package.json +4 -4
@@ -1,12 +1,11 @@
1
1
  // Answers per-frame source requests with decoded VideoFrames; Canvas2D premultiplied RGBA remains
2
- // for text rasters and for an engine that cannot take a VideoFrame.
2
+ // for text rasters and for engines that cannot take a VideoFrame.
3
3
  //
4
- // Still and text rasters share one LRU cache bounded by bytes, but what the frame being drawn reads
5
- // is never evicted, even past the budget. Stills are decoded per read and closed once read.
6
- //
7
- // Decoder cursors are scoped by clip consumer so overlapping uses of one source cannot seek each
8
- // other. WebCodecs is the primary path; an `HTMLVideoElement` seek is the fallback for codecs it
9
- // cannot open or frames that keep failing (`decodeFallbacks`).
4
+ // Still and text rasters share one byte-bounded LRU cache, but rasters the current frame reads are
5
+ // never evicted, even past the budget. Decoder cursors are scoped per clip consumer so overlapping
6
+ // uses of one source cannot seek each other. An `HTMLVideoElement` seek is the fallback for codecs
7
+ // WebCodecs cannot open or frames that keep failing.
8
+ import { activeTimeout } from "alpha2/active-clock";
10
9
  import { decoderCursorKey } from "alpha2/frame-supply";
11
10
  import { MediabunnyVideoDecoder } from "alpha2/mediabunny";
12
11
  import { containRect, seekVideoFrame } from "./preview-utils.js";
@@ -17,8 +16,8 @@ const MAX_DECODE_FAILURES = 3;
17
16
  /** Default byte budget of still and text rasters, matching the frame supply's image cache. */
18
17
  export const DEFAULT_RASTER_BUDGET_BYTES = 256 * 1024 * 1024;
19
18
  /**
20
- * The default byte budget of what open extension sources hold (their decoded frames in the provider
21
- * module and the frame each showed last): the native host's `providerSourceBytes` default.
19
+ * Default byte budget for open extension sources (decoded frames in the provider module plus the
20
+ * last shown frame), matching the native host's `providerSourceBytes`.
22
21
  */
23
22
  export const DEFAULT_PROVIDER_SOURCE_BYTES = 512 * 1024 * 1024;
24
23
  /** The host side of the engine's per-frame source requests for one renderer (preview or export). */
@@ -29,29 +28,27 @@ export class SourcePixels {
29
28
  #seekVideos = new Map();
30
29
  /**
31
30
  * Rasters by asset ID in LRU order: `{ key, raster, bytes, frame }`, where `key` is what the
32
- * raster was made from and `frame` the frame that last read it.
31
+ * raster was made from and `frame` the last frame that read it.
33
32
  */
34
33
  #rasters = new Map();
35
34
  #rasterBytes = 0;
36
- /** Counts the frames drawn: the engine releases once before each frame reads its sources. */
35
+ /** Frame counter; the engine releases once before each frame reads its sources. */
37
36
  #frame = 0;
38
37
  #staging = null;
39
- /** What fell back from WebCodecs decoding, by source (see `decodeFallbacks`). */
40
38
  #fallbacks = new Map();
41
- /** Extension sources opened, by `providerKey`, in least-recently-shown order. */
39
+ /** Open extension sources by `providerKey`, least recently shown first. */
42
40
  #providers = new Map();
43
- /** Sources the provider budget closed and a later frame opened again. */
44
41
  #providerReopens = 0;
45
- /** Asset IDs by `providerKey` of sources the provider budget closed, until they reopen. */
42
+ /** Asset IDs by `providerKey` of sources the budget closed, until they reopen. */
46
43
  #providersClosed = new Map();
47
44
  /**
48
- * Per animated-image asset, `{ asset, provider, opened }`: a reader opened once whose file and
49
- * kept frames every reader made from it shares (see `#openReader`).
45
+ * Per animated-image asset, `{ asset, provider, opened }`: one opened reader whose file and kept
46
+ * frames all its readers share (see `#openReader`).
50
47
  */
51
48
  #readerMakers = new Map();
52
- /** The fonts an SVG's text draws with: the engine's caption fonts (see `setFonts`). */
49
+ /** The engine's caption fonts, which SVG text draws with (see `setFonts`). */
53
50
  #fonts = [];
54
- /** What each SVG's pixels are with the fonts it was drawn with, for `contentOf`. */
51
+ /** Each SVG's content identity with the fonts it was drawn with, for `contentOf`. */
55
52
  #fontContents = new WeakMap();
56
53
  constructor(library, { decoderLimit = 32, rasterBudgetBytes = DEFAULT_RASTER_BUDGET_BYTES, providerSourceBytes = DEFAULT_PROVIDER_SOURCE_BYTES, extensionsWasmUrl, videoDecode = "auto", } = {}) {
57
54
  if (videoDecode !== "auto" && videoDecode !== "software")
@@ -66,10 +63,9 @@ export class SourcePixels {
66
63
  this.library = library;
67
64
  this.extensionsWasmUrl = extensionsWasmUrl;
68
65
  /**
69
- * How video decodes: `"auto"` lets the browser pick, a hardware decoder where it has one;
70
- * `"software"` decodes on the CPU, so no frame needs a GPU readback, except a source the
71
- * browser has no software decoder for, which decodes as `"auto"` and is reported in
72
- * `decodeFallbacks`.
66
+ * `"auto"` lets the browser pick, hardware where available; `"software"` decodes on the CPU so
67
+ * no frame needs a GPU readback. Sources without a software decoder decode as `"auto"` and are
68
+ * reported in `decodeFallbacks`.
73
69
  */
74
70
  this.videoDecode = videoDecode;
75
71
  }
@@ -102,9 +98,8 @@ export class SourcePixels {
102
98
  this.#dropRaster(id, entry);
103
99
  }
104
100
  /**
105
- * The fonts an SVG's text draws with, in fallback order: the stack the engine draws captions with
106
- * (`{ bytes }` each), as the native host draws an SVG's text with its caption fonts. An SVG
107
- * opened with other fonts opens again on its next frame; other sources are not touched.
101
+ * Sets the fonts SVG text draws with, in fallback order (the engine's caption stack, `{ bytes }`
102
+ * each), as the native host does. An SVG opened with other fonts reopens on its next frame.
108
103
  */
109
104
  setFonts(fonts) {
110
105
  if (sameFonts(this.#fonts, fonts))
@@ -112,9 +107,8 @@ export class SourcePixels {
112
107
  this.#fonts = [...fonts];
113
108
  }
114
109
  /**
115
- * What a source's pixels are, for `FrameSupply`'s `contentOf`: its runtime asset, and for an SVG
116
- * also the fonts its text draws with, so a change of fonts draws it again. The same object while
117
- * neither changes.
110
+ * A source's content identity for `FrameSupply`'s `contentOf`: its runtime asset, plus the fonts
111
+ * for an SVG so a font change redraws it. Stable while neither changes.
118
112
  */
119
113
  contentOf(asset) {
120
114
  if (!asset || !drawsText(asset.provider))
@@ -126,17 +120,15 @@ export class SourcePixels {
126
120
  this.#fontContents.set(asset, content);
127
121
  return content;
128
122
  }
129
- /** Drops the cached raster of `assetId`, so its next read makes it again. */
130
123
  invalidate(assetId) {
131
124
  const entry = this.#rasters.get(assetId);
132
125
  if (entry)
133
126
  this.#dropRaster(assetId, entry);
134
127
  }
135
128
  /**
136
- * Bound method handed to `FrameSupply` as its `pixelsFor` callback. `sourceId` is the source the
137
- * engine asked for, which the layer may stand in for (a proxy has an ID of its own): decoders are
138
- * kept per source, as `release` names them. `decode: "software"` asks for the frame decoded on
139
- * the CPU (see `#softwareFrame`).
129
+ * `FrameSupply`'s `pixelsFor` callback. `sourceId` is the source the engine asked for, which the
130
+ * layer may stand in for (a proxy has its own ID); decoders are kept per source, as `release`
131
+ * names them. `decode: "software"` asks for a CPU-decoded frame (see `#softwareFrame`).
140
132
  */
141
133
  pixelsFor = async (layer, seconds, { preferVideoFrame = false, consumerId = layer.id, sourceId, decode } = {}) => {
142
134
  const asset = layer.asset ?? layer;
@@ -153,8 +145,8 @@ export class SourcePixels {
153
145
  /** A frame an extension provider draws at its own size; `key` is its open source. */
154
146
  async #providerPixels(asset, key, seconds, reopened = false) {
155
147
  let opened = this.#providers.get(key);
156
- // Reopened when the asset, its provider settings, or an SVG's fonts change; providers are
157
- // compared by identity first, so a shown frame does not serialize them.
148
+ // Reopen when the asset, its provider settings or an SVG's fonts change. Providers compare by
149
+ // identity first so a shown frame does not serialize them.
158
150
  const { provider } = asset;
159
151
  const fonts = drawsText(provider) ? this.#fonts : null;
160
152
  if (opened?.asset !== asset ||
@@ -170,7 +162,7 @@ export class SourcePixels {
170
162
  ? this.#openReader(asset)
171
163
  : openProvider(asset, this.extensionsWasmUrl, fonts),
172
164
  };
173
- // Keep what a provider reports, but not a load or read failure: retry next frame.
165
+ // Keep provider reports, but retry load or read failures next frame.
174
166
  opening.source.catch((error) => {
175
167
  if (retryableOpen.has(error) && this.#providers.get(key) === opening)
176
168
  this.#providers.delete(key);
@@ -180,7 +172,7 @@ export class SourcePixels {
180
172
  if (this.#providersClosed.delete(key))
181
173
  this.#providerReopens += 1;
182
174
  }
183
- // Kept by the budget for this frame and the next.
175
+ // The budget keeps it for this frame and the next.
184
176
  opened.frame = this.#frame;
185
177
  this.#providers.delete(key);
186
178
  this.#providers.set(key, opened);
@@ -188,8 +180,8 @@ export class SourcePixels {
188
180
  opened.open = source;
189
181
  const ticks = BigInt(Math.round(seconds * TICKS_PER_SECOND));
190
182
  const fit = { width: this.#width, height: this.#height };
191
- // The frame shown last answers every time it covers, as the same object, so a still is not
192
- // uploaded again each frame.
183
+ // The last frame answers every time it covers as the same object, so a still is not uploaded
184
+ // again each frame.
193
185
  const shown = opened.shown;
194
186
  if (shown &&
195
187
  shown.width === fit.width &&
@@ -199,11 +191,11 @@ export class SourcePixels {
199
191
  return shown.result;
200
192
  let frame;
201
193
  try {
202
- // An image sequence's frame enters the provider module for this render only.
194
+ // Image sequence frames enter the provider module for this render only.
203
195
  frame = await extensions.withResources(read, () => source.needs(ticks), () => source.render(ticks, fit));
204
196
  }
205
197
  catch (error) {
206
- // The provider module restarted after another source stopped it: this one opens again.
198
+ // The provider module restarted after another source stopped it, so reopen this one.
207
199
  if (error?.name !== "StaleExtensionSourceError" || reopened)
208
200
  throw error;
209
201
  if (this.#providers.get(key) === opened)
@@ -234,8 +226,8 @@ export class SourcePixels {
234
226
  return result;
235
227
  }
236
228
  /**
237
- * A reader of an animated image, of its own (see `providerKey`), made from what the asset's
238
- * readers share so the file is decoded once; as the native host does.
229
+ * An animated-image reader of its own (see `providerKey`), made from what the asset's readers
230
+ * share so the file is decoded once, as on the native host.
239
231
  */
240
232
  async #openReader(asset) {
241
233
  const { provider } = asset;
@@ -249,7 +241,7 @@ export class SourcePixels {
249
241
  return { source, extensions, read };
250
242
  }
251
243
  catch {
252
- // What made the readers did not open, or a module restart left it behind: open again.
244
+ // The maker failed to open or a module restart left it stale, so open again.
253
245
  }
254
246
  if (this.#readerMakers.get(asset.id) === made)
255
247
  this.#dropReaderMaker(asset.id);
@@ -273,7 +265,7 @@ export class SourcePixels {
273
265
  this.#readerMakers.set(asset.id, entry);
274
266
  return opening;
275
267
  }
276
- /** Lets go of what makes an animated image's readers; the readers it made stay open. */
268
+ /** Drops what makes an animated image's readers; readers already made stay open. */
277
269
  #dropReaderMaker(assetId) {
278
270
  const made = this.#readerMakers.get(assetId);
279
271
  if (!made)
@@ -281,7 +273,7 @@ export class SourcePixels {
281
273
  this.#readerMakers.delete(assetId);
282
274
  made.opened.then(({ maker }) => maker?.release()).catch(() => { });
283
275
  }
284
- /** Lets go of the reader makers of animated images no open source reads any more. */
276
+ /** Drops reader makers no open source reads. */
285
277
  #dropUnreadMakers() {
286
278
  if (this.#readerMakers.size === 0)
287
279
  return;
@@ -292,7 +284,7 @@ export class SourcePixels {
292
284
  if (!read.has(assetId))
293
285
  this.#dropReaderMaker(assetId);
294
286
  }
295
- /** What open extension sources hold; an animated image's shared file counts once per asset. */
287
+ /** Bytes open extension sources hold; an animated image's shared file counts once per asset. */
296
288
  #providerBytes() {
297
289
  let bytes = 0;
298
290
  const shared = new Map();
@@ -309,8 +301,8 @@ export class SourcePixels {
309
301
  return bytes;
310
302
  }
311
303
  /**
312
- * Keeps open extension sources within `providerSourceBytes` by closing the least recently shown
313
- * ones; sources this frame or the previous one showed stay open even past the budget.
304
+ * Closes least recently shown extension sources until within `providerSourceBytes`. Sources shown
305
+ * by this frame or the previous one stay open even past the budget.
314
306
  */
315
307
  #boundProviders() {
316
308
  let bytes = this.#providerBytes();
@@ -339,8 +331,8 @@ export class SourcePixels {
339
331
  return this.#staging.getContext("2d", { willReadFrequently: true });
340
332
  }
341
333
  /**
342
- * The raster `make` makes for `id` from `key`, cached while `key` is unchanged. A raster read by
343
- * the frame being drawn is not evicted until a later frame; failures are not kept.
334
+ * The raster `make` makes for `id` from `key`, cached while `key` is unchanged. Rasters the
335
+ * current frame read are not evicted until a later frame; failures are not cached.
344
336
  */
345
337
  #cachedRaster(id, key, make) {
346
338
  let entry = this.#rasters.get(id);
@@ -375,8 +367,8 @@ export class SourcePixels {
375
367
  this.#rasters.delete(id);
376
368
  }
377
369
  /**
378
- * Evicts the least recently used rasters the frame being drawn has not read, until the cache is
379
- * within its budget or only that frame's rasters are left.
370
+ * Evicts least recently used rasters the current frame has not read, until within budget or only
371
+ * this frame's remain.
380
372
  */
381
373
  #evictRasters() {
382
374
  for (const [id, entry] of this.#rasters) {
@@ -485,7 +477,7 @@ export class SourcePixels {
485
477
  return null;
486
478
  const cursor = decoderCursorKey(sourceId, consumerId);
487
479
  let decoder = this.#decoders.get(cursor);
488
- // The source now reads other media (its proxy, or the original again): the cursor starts over.
480
+ // The source now reads other media (proxy or original), so the cursor starts over.
489
481
  if (decoder && decoder.asset !== asset) {
490
482
  await closeDecoder(decoder);
491
483
  this.#decoders.delete(cursor);
@@ -511,7 +503,7 @@ export class SourcePixels {
511
503
  this.#fallbacks.get(sourceId).frames += 1;
512
504
  return null;
513
505
  }
514
- // A decoder that cannot open (a codec the browser cannot decode) is the fallback's for good.
506
+ // A decoder that cannot open (unsupported codec) falls back for good.
515
507
  try {
516
508
  decoder.video ??= await (decoder.opening ??= this.#openVideo(decoder));
517
509
  }
@@ -530,7 +522,7 @@ export class SourcePixels {
530
522
  decoded = await decoder.video.refill(request);
531
523
  }
532
524
  catch {
533
- // A failed read (stall, decoder error) is retried once with a new decoder, which a stalled
525
+ // Retry a failed read (stall, decoder error) once with a new decoder, which a stalled
534
526
  // decoder's surfaces do not hold up.
535
527
  closeOpened(decoder);
536
528
  try {
@@ -556,10 +548,7 @@ export class SourcePixels {
556
548
  chromaSiting: decoded.chromaSiting,
557
549
  };
558
550
  }
559
- /**
560
- * Decoder cursors the budget counts, one per consumer: a software cursor beside its consumer's
561
- * own cursor is not counted again.
562
- */
551
+ /** Decoder cursors the budget counts; a software cursor beside its consumer's is not counted. */
563
552
  #cursorCount() {
564
553
  const consumers = new Set();
565
554
  for (const decoder of this.#decoders.values())
@@ -567,8 +556,8 @@ export class SourcePixels {
567
556
  return consumers.size;
568
557
  }
569
558
  /**
570
- * Opens `decoder`'s source as `videoDecode` says. A source the browser has no software decoder
571
- * for opens as `"auto"`, for good, and is reported once in `decodeFallbacks`.
559
+ * Opens `decoder`'s source per `videoDecode`. A source with no software decoder opens as
560
+ * `"auto"` for good and is reported once in `decodeFallbacks`.
572
561
  */
573
562
  async #openVideo(decoder) {
574
563
  const { asset } = decoder;
@@ -596,14 +585,13 @@ export class SourcePixels {
596
585
  }
597
586
  }
598
587
  /**
599
- * The frame at `seconds` decoded on the CPU, on a cursor of its own beside the consumer's: what
600
- * `FrameSupply` asks for when the browser could not read a hardware frame back. Rejects rather
601
- * than falling back to an element seek, whose frame would be the browser's again.
588
+ * The frame at `seconds` decoded on the CPU, on its own cursor beside the consumer's, for when
589
+ * `FrameSupply` could not read a hardware frame back. Rejects rather than falling back to an
590
+ * element seek, whose frame would be the browser's again.
602
591
  *
603
- * The cursor shares the consumer's source and consumer IDs, so `retainConsumers` and `release`
604
- * close it with the consumer's; it stays open between frames, as readback failures come in runs
605
- * and the next one then decodes on from it. A consumer whose frames all decode here (a source
606
- * the browser decodes to opaque frames) has no other cursor, so this one opens within the budget.
592
+ * The cursor shares the consumer's IDs, so `retainConsumers` and `release` close it with the
593
+ * consumer's. It stays open between frames because readback failures come in runs. A consumer
594
+ * whose frames all decode here has no other cursor, so this one opens within the budget.
607
595
  */
608
596
  async #softwareFrame(asset, seconds, consumerId, sourceId) {
609
597
  if (!asset.file)
@@ -650,15 +638,11 @@ export class SourcePixels {
650
638
  };
651
639
  }
652
640
  catch (error) {
653
- // The next request opens it again.
654
641
  closeOpened(decoder);
655
642
  throw error;
656
643
  }
657
644
  }
658
- /**
659
- * Records that `decoder`'s frame falls back to an element seek because of `error`; `permanent`
660
- * applies it to every later frame of the cursor.
661
- */
645
+ /** Records an element-seek fallback for `decoder`; `permanent` covers its later frames too. */
662
646
  #fallBack(decoder, error, permanent) {
663
647
  if (permanent)
664
648
  decoder.failed = true;
@@ -677,11 +661,11 @@ export class SourcePixels {
677
661
  return null;
678
662
  }
679
663
  /**
680
- * What fell back, per source and kind: `"element-seek"`, frames read by element seeks rather
681
- * than decoded, with the last reason, how many frames, and whether its decoding stopped for good
682
- * (it could not open, or failed {@link MAX_DECODE_FAILURES} frames running);
683
- * `"software-unavailable"`, a source `videoDecode: "software"` decoded as `"auto"` because the
684
- * browser has no software decoder for it (`frames` 0). A browser export reports it.
664
+ * Fallbacks per source and kind, reported by browser exports. `"element-seek"`: frames read by
665
+ * element seeks instead of decoding, with the last reason, count, and whether decoding stopped
666
+ * for good (failed to open or failed {@link MAX_DECODE_FAILURES} frames running).
667
+ * `"software-unavailable"`: `videoDecode: "software"` decoded as `"auto"` for lack of a software
668
+ * decoder (`frames` 0).
685
669
  */
686
670
  get decodeFallbacks() {
687
671
  return [...this.#fallbacks.values()].map((entry) => ({ ...entry }));
@@ -718,7 +702,7 @@ export class SourcePixels {
718
702
  await seekVideoFrame(video, Math.min(Math.max(0, seconds), Math.max(0, duration - 1e-3)));
719
703
  return video;
720
704
  }
721
- /** Keeps decoder sessions bounded to consumers needed by the current frame. */
705
+ /** Keeps decoder sessions to the consumers the current frame needs. */
722
706
  retainConsumers = async (liveConsumerIds) => {
723
707
  for (const [cursor, decoder] of this.#decoders) {
724
708
  if (liveConsumerIds.has(decoder.consumerId))
@@ -734,8 +718,8 @@ export class SourcePixels {
734
718
  }
735
719
  };
736
720
  /**
737
- * Drops decoders, extension sources, and rasters for source IDs not in `liveIds`. The engine
738
- * calls it before each frame reads its sources, so the previous frame's reads may be evicted.
721
+ * Drops decoders, extension sources and rasters for source IDs not in `liveIds`. The engine calls
722
+ * it before each frame reads its sources.
739
723
  */
740
724
  async release(liveIds) {
741
725
  this.#frame += 1;
@@ -770,13 +754,13 @@ export class SourcePixels {
770
754
  await this.release(new Set());
771
755
  }
772
756
  }
773
- /** Whether two raster keys name the same raster: the same objects and sizes, field by field. */
757
+ /** Whether two raster keys match field by field (same objects and sizes). */
774
758
  function sameKey(a, b) {
775
759
  const fields = Object.keys(a);
776
760
  return (fields.length === Object.keys(b).length &&
777
761
  fields.every((field) => Object.is(a[field], b[field])));
778
762
  }
779
- /** The bytes a raster's pixels hold: a text raster is its pixels, a still a raster frame. */
763
+ /** Bytes a raster holds: a text raster's pixels, or a still's raster frame. */
780
764
  function rasterBytes(raster) {
781
765
  return (raster.pixels ?? raster).byteLength;
782
766
  }
@@ -787,13 +771,13 @@ async function closeDecoder(decoder) {
787
771
  decoder.video = null;
788
772
  decoder.opening = null;
789
773
  }
790
- /** Closes a cursor's opened decoder, so its next read opens another. */
774
+ /** Closes a cursor's opened decoder so its next read opens another. */
791
775
  function closeOpened(decoder) {
792
776
  decoder.video?.close();
793
777
  decoder.video = null;
794
778
  decoder.opening = null;
795
779
  }
796
- /** Lets a fallback seek element go of its media. */
780
+ /** Releases a fallback seek element's media. */
797
781
  function unloadVideo(video) {
798
782
  video.removeAttribute("src");
799
783
  video.load();
@@ -810,8 +794,9 @@ function premultiplyInPlace(pixels) {
810
794
  }
811
795
  function waitForLoad(video) {
812
796
  return new Promise((resolve, reject) => {
797
+ const timer = new AbortController();
813
798
  const cleanup = () => {
814
- clearTimeout(timer);
799
+ timer.abort();
815
800
  video.removeEventListener("loadeddata", ready);
816
801
  video.removeEventListener("error", failed);
817
802
  };
@@ -823,19 +808,20 @@ function waitForLoad(video) {
823
808
  cleanup();
824
809
  reject(video.error ?? new Error("video load failed"));
825
810
  };
826
- const timer = setTimeout(() => {
811
+ // Active time, so a page frozen while the element loads does not time out.
812
+ void activeTimeout(10_000, timer.signal).then(() => {
827
813
  cleanup();
828
814
  reject(new Error("video load timed out"));
829
- }, 10_000);
815
+ });
830
816
  video.addEventListener("loadeddata", ready, { once: true });
831
817
  video.addEventListener("error", failed, { once: true });
832
818
  video.load();
833
819
  });
834
820
  }
835
821
  /**
836
- * Which open extension source answers `consumerId`'s read of `asset`. An animated image decodes in
837
- * order and keeps only a window of frames, so each reader gets its own (sharing one would rewind
838
- * it); every other provider serves all readers from one source, as on the native host.
822
+ * Which open extension source answers `consumerId`'s read of `asset`. Animated images decode in
823
+ * order and keep only a window of frames, so each reader gets its own; other providers share one
824
+ * source, as on the native host.
839
825
  */
840
826
  function providerKey(asset, consumerId) {
841
827
  return asset.provider.format === "animated_image"
@@ -844,10 +830,7 @@ function providerKey(asset, consumerId) {
844
830
  }
845
831
  /** Serialized settings by provider object; settings are never mutated, so each serializes once. */
846
832
  const providerSettingsKeys = new WeakMap();
847
- /**
848
- * `provider`'s settings as one comparable string, serialized once per provider object: an image
849
- * sequence's holds every frame's locator.
850
- */
833
+ /** `provider`'s settings as one comparable string, serialized once per provider object. */
851
834
  export function providerSettings(provider) {
852
835
  if (provider === null || typeof provider !== "object")
853
836
  return JSON.stringify(provider ?? null);
@@ -858,11 +841,10 @@ export function providerSettings(provider) {
858
841
  }
859
842
  return key;
860
843
  }
861
- /** Results of {@link sameProviderSettings}, by the first provider, then the second. */
862
844
  const providerSettingsEqual = new WeakMap();
863
845
  /**
864
- * Whether two providers have the same settings: the same object, or equal ones. Two objects are
865
- * compared once, so a pair asked about for every frame costs no serialization or string compare.
846
+ * Whether two providers have equal settings. Each pair is compared once, so per-frame checks cost
847
+ * no serialization.
866
848
  */
867
849
  export function sameProviderSettings(a, b) {
868
850
  if (a === b)
@@ -889,11 +871,11 @@ function sameFonts(a, b) {
889
871
  return (a.length === b.length &&
890
872
  a.every((font, index) => font.bytes === b[index].bytes));
891
873
  }
892
- /** Errors of {@link openProvider} (module load or file read) that a later open may not meet. */
874
+ /** {@link openProvider} errors (module load or file read) a later open may not hit. */
893
875
  const retryableOpen = new WeakSet();
894
876
  /**
895
- * Opens an asset's extension source, an SVG with `fonts`. An image sequence's frames enter the
896
- * provider module only for the call that reads them.
877
+ * Opens an asset's extension source, an SVG with `fonts`. Image sequence frames enter the provider
878
+ * module only for the call that reads them.
897
879
  */
898
880
  async function openProvider(asset, wasmUrl, fonts) {
899
881
  const retryable = (error) => {
@@ -1,10 +1,6 @@
1
1
  /**
2
- * The bytes the engine's I420 readback of a `width`x`height` frame keeps staged: its planes and
3
- * the renderer's 16-byte canary, what a readback ticket reports as `retainedBytes`.
2
+ * Staged bytes of the engine's I420 readback (planes plus canary), as a ticket's `retainedBytes`.
4
3
  */
5
4
  export function estimatedStagingBytes(width: any, height: any): number;
6
- /**
7
- * The least an export of `width`x`height` frames can run with in flight: one frame's readback
8
- * staged and the packed copy of one frame the encoder holds.
9
- */
5
+ /** The minimum in-flight bytes of an export: one staged readback plus one packed encoder frame. */
10
6
  export function minimumExportWorkingSet(width: any, height: any): number;
@@ -1,11 +1,11 @@
1
- // What an export frame holds in flight, computed without loading the runtime.
2
- // `tests/cases/staging-bytes-corpus.json` holds these to the renderer's `yuv420p_staging_bytes`.
1
+ // What an export frame holds in flight, computed without loading the runtime. Checked against the
2
+ // renderer's `yuv420p_staging_bytes` by `tests/cases/staging-bytes-corpus.json`.
3
3
  /** The canary the renderer stages after the planes, to prove the GPU wrote them. */
4
4
  const CANARY_BYTES = 16;
5
5
  /**
6
- * The bytes of the planes of a `width`x`height` I420 readback: each plane's rows padded to the copy
7
- * alignment (256), each plane starting on a 512-byte placement boundary, as the renderer lays them
8
- * out. The packed frame the encoder takes is this long.
6
+ * Plane bytes of a `width`x`height` I420 readback as the renderer lays them out: rows padded to
7
+ * 256-byte copy alignment, each plane on a 512-byte boundary. The packed encoder frame is this
8
+ * long.
9
9
  */
10
10
  function planeBytes(width, height) {
11
11
  const row = (bytes) => Math.ceil(bytes / 256) * 256;
@@ -17,16 +17,12 @@ function planeBytes(width, height) {
17
17
  return v + chroma;
18
18
  }
19
19
  /**
20
- * The bytes the engine's I420 readback of a `width`x`height` frame keeps staged: its planes and
21
- * the renderer's 16-byte canary, what a readback ticket reports as `retainedBytes`.
20
+ * Staged bytes of the engine's I420 readback (planes plus canary), as a ticket's `retainedBytes`.
22
21
  */
23
22
  export function estimatedStagingBytes(width, height) {
24
23
  return planeBytes(width, height) + CANARY_BYTES;
25
24
  }
26
- /**
27
- * The least an export of `width`x`height` frames can run with in flight: one frame's readback
28
- * staged and the packed copy of one frame the encoder holds.
29
- */
25
+ /** The minimum in-flight bytes of an export: one staged readback plus one packed encoder frame. */
30
26
  export function minimumExportWorkingSet(width, height) {
31
27
  return estimatedStagingBytes(width, height) + planeBytes(width, height);
32
28
  }
@@ -1,12 +1,8 @@
1
1
  /**
2
- * Decodes an image file upright and without colour management. Rejects a picture larger than the
3
- * engine takes, as native decoding refuses one.
2
+ * Decodes an image upright without colour management, rejecting one larger than the engine takes.
4
3
  */
5
4
  export function decodeStill(file: any): Promise<ImageBitmap>;
6
- /**
7
- * A decoded still's pixels at its own size, in straight alpha as decoded, as a raster frame the
8
- * engine presents into the output.
9
- */
5
+ /** A decoded still's pixels at its own size, in straight alpha, as a raster frame. */
10
6
  export function stillRaster(bitmap: any): {
11
7
  kind: string;
12
8
  width: any;
@@ -1,16 +1,13 @@
1
- // Reads a still image by the same rule as native decoding, so it shows alike in preview and in
2
- // browser and native exports:
3
- // - Upright per Exif `Orientation` (`imageOrientation: "from-image"`; Chrome reads `"none"` as it).
4
- // - In stored sRGB code values: no ICC profile (`colorSpaceConversion: "none"`), since the engine
5
- // treats SDR input as sRGB and native decoding has no colour management.
6
- // - At its own size; the engine scales it into the output.
1
+ // Reads a still image by the same rules as native decoding, so preview and browser and native
2
+ // exports match: upright per Exif `Orientation` (`imageOrientation: "from-image"`), in stored
3
+ // sRGB code values with no ICC conversion (the engine treats SDR input as sRGB and native decoding
4
+ // has no colour management), and at its own size.
7
5
  /** The longest side the engine decodes, renders, or composes (`MAX_FRAME_DIMENSION`). */
8
6
  export const MAX_STILL_DIMENSION = 16_384;
9
7
  /** The largest area the engine decodes, renders, or composes (`MAX_FRAME_PIXELS`). */
10
8
  export const MAX_STILL_PIXELS = 67_108_864;
11
9
  /**
12
- * Decodes an image file upright and without colour management. Rejects a picture larger than the
13
- * engine takes, as native decoding refuses one.
10
+ * Decodes an image upright without colour management, rejecting one larger than the engine takes.
14
11
  */
15
12
  export async function decodeStill(file) {
16
13
  const bitmap = await createImageBitmap(file, {
@@ -26,10 +23,7 @@ export async function decodeStill(file) {
26
23
  }
27
24
  return bitmap;
28
25
  }
29
- /**
30
- * A decoded still's pixels at its own size, in straight alpha as decoded, as a raster frame the
31
- * engine presents into the output.
32
- */
26
+ /** A decoded still's pixels at its own size, in straight alpha, as a raster frame. */
33
27
  export function stillRaster(bitmap) {
34
28
  const { width, height } = bitmap;
35
29
  const canvas = new OffscreenCanvas(width, height);
@@ -0,0 +1,14 @@
1
+ /**
2
+ * The text of a user's file (a subtitle file, a `.cube` LUT) by the rule every alpha2 runtime uses:
3
+ * UTF-8 (a byte order mark is dropped), or UTF-16 when the file starts with a little- or big-endian
4
+ * byte order mark (as Windows PowerShell 5.1 writes). Anything else, such as a legacy code page,
5
+ * throws an `invalid_text` {@link EditorError} naming the file instead of decoding to replacement
6
+ * characters.
7
+ *
8
+ * @param {Blob & { name?: string }} file
9
+ * @param {string} [operation] The operation the error names. Defaults to `import`.
10
+ * @returns {Promise<string>}
11
+ */
12
+ export function readUserText(file: Blob & {
13
+ name?: string;
14
+ }, operation?: string): Promise<string>;
@@ -0,0 +1,27 @@
1
+ import { EditorError } from "../../core/document.js";
2
+ /**
3
+ * The text of a user's file (a subtitle file, a `.cube` LUT) by the rule every alpha2 runtime uses:
4
+ * UTF-8 (a byte order mark is dropped), or UTF-16 when the file starts with a little- or big-endian
5
+ * byte order mark (as Windows PowerShell 5.1 writes). Anything else, such as a legacy code page,
6
+ * throws an `invalid_text` {@link EditorError} naming the file instead of decoding to replacement
7
+ * characters.
8
+ *
9
+ * @param {Blob & { name?: string }} file
10
+ * @param {string} [operation] The operation the error names. Defaults to `import`.
11
+ * @returns {Promise<string>}
12
+ */
13
+ export async function readUserText(file, operation = "import") {
14
+ const bytes = new Uint8Array(await file.arrayBuffer());
15
+ const encoding = bytes[0] === 0xff && bytes[1] === 0xfe
16
+ ? "utf-16le"
17
+ : bytes[0] === 0xfe && bytes[1] === 0xff
18
+ ? "utf-16be"
19
+ : "utf-8";
20
+ try {
21
+ // Each decoder drops its own byte order mark.
22
+ return new TextDecoder(encoding, { fatal: true }).decode(bytes);
23
+ }
24
+ catch {
25
+ throw new EditorError("invalid_text", `${file.name ?? "the file"} is not UTF-8 text; save it as UTF-8`, operation);
26
+ }
27
+ }