@open-artifact/wasm-motion 0.0.0-stage → 0.12.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/src/index.js ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The Motion type's pixel work, done by `crates/open-artifact-motion` compiled
3
+ * to WebAssembly: GIF palettes and encoding (in-thread, or in a Worker
4
+ * session for a page), MOV writing (QuickTime Animation with alpha, PCM
5
+ * sound) and the flash analysis of scenes.
6
+ */
7
+ export { buildGifPalette, createGifEncoder } from "./gif.js";
8
+ export { createMovWriter } from "./mov.js";
9
+ export { gifBackend, openGifSession } from "./gif-session.js";
10
+ export { FLASH_MAX_SCENE_SECONDS, FLASH_RASTER_SCALE, FLASH_SAMPLE_FPS, FLASH_SAMPLE_PADDING_SECONDS, analyzeFlashes, createFlashAnalyzer, flashSafeScale, planFlashSampling, } from "./flash.js";
@@ -0,0 +1,12 @@
1
+ /**
2
+ * The WebAssembly module (`crates/open-artifact-motion`), instantiated
3
+ * synchronously on first use from its embedded bytes, so it loads the same
4
+ * in a page, a Worker, Node tests and the CLI.
5
+ */
6
+ import * as glue from '../generated/open_artifact_motion.js';
7
+ /** RGBA pixels, four bytes a pixel, rows top to bottom (as `ImageData.data` holds them). */
8
+ export type RgbaPixels = Uint8Array | Uint8ClampedArray;
9
+ /** The module's exports, instantiating it on the first call. */
10
+ export declare function videoModule(): Omit<typeof glue, 'default' | 'initSync'>;
11
+ /** The same pixels as plain bytes (no copy). */
12
+ export declare function pixelBytes(pixels: RgbaPixels): Uint8Array;
package/src/module.js ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * The WebAssembly module (`crates/open-artifact-motion`), instantiated
3
+ * synchronously on first use from its embedded bytes, so it loads the same
4
+ * in a page, a Worker, Node tests and the CLI.
5
+ */
6
+ import * as glue from '../generated/open_artifact_motion.js';
7
+ import { WASM_BASE64 } from '../generated/bytes.js';
8
+ let ready = false;
9
+ function decode(base64) {
10
+ const binary = atob(base64);
11
+ const bytes = new Uint8Array(binary.length);
12
+ for (let index = 0; index < binary.length; index++)
13
+ bytes[index] = binary.charCodeAt(index);
14
+ return bytes;
15
+ }
16
+ /** The module's exports, instantiating it on the first call. */
17
+ export function videoModule() {
18
+ if (!ready) {
19
+ glue.initSync({ module: decode(WASM_BASE64) });
20
+ ready = true;
21
+ }
22
+ return glue;
23
+ }
24
+ /** The same pixels as plain bytes (no copy). */
25
+ export function pixelBytes(pixels) {
26
+ return pixels instanceof Uint8Array ? pixels : new Uint8Array(pixels.buffer, pixels.byteOffset, pixels.byteLength);
27
+ }
package/src/mov.d.ts ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * MOV writing in this thread: QuickTime Animation frames (32-bit ARGB, the
3
+ * alpha kept) and 16-bit PCM sound. The module lays out the movie; the
4
+ * bytes of each frame stay here, so its memory stays small, and `finish`
5
+ * hands back the file as parts in order.
6
+ */
7
+ import { type RgbaPixels } from './module.ts';
8
+ /**
9
+ * What becomes of a frame that repeats the one before: a 7-byte sample
10
+ * ("unchanged"), the bytes of a repeated key frame stored once ("shared"),
11
+ * or every frame stored whole ("whole").
12
+ */
13
+ export type MovRepeats = 'unchanged' | 'shared' | 'whole';
14
+ /** Options of a MOV. */
15
+ export interface MovWriterOptions {
16
+ width: number;
17
+ height: number;
18
+ fps: number;
19
+ /** A key frame (stored whole) every so many frames; 1: every frame. */
20
+ keyEvery?: number;
21
+ repeats?: MovRepeats;
22
+ }
23
+ /** The movie's sound: interleaved 16-bit little-endian PCM. */
24
+ export interface MovSound {
25
+ pcm: Uint8Array;
26
+ sampleRate: number;
27
+ channels: number;
28
+ }
29
+ /** A MOV being written. */
30
+ export interface MovWriter {
31
+ /** Adds a frame of `width` × `height` RGBA pixels; the bytes kept for it (0 when it shares the frame before). */
32
+ add(pixels: RgbaPixels): number;
33
+ /** Bytes of the frames kept so far. */
34
+ readonly byteLength: number;
35
+ /** Ends the movie and releases the writer; the file as parts, in order. */
36
+ finish(sound?: MovSound | null): Uint8Array<ArrayBuffer>[];
37
+ /** Releases the writer without finishing. */
38
+ dispose(): void;
39
+ }
40
+ /** A MOV written frame by frame. */
41
+ export declare function createMovWriter(options: MovWriterOptions): MovWriter;
package/src/mov.js ADDED
@@ -0,0 +1,58 @@
1
+ /**
2
+ * MOV writing in this thread: QuickTime Animation frames (32-bit ARGB, the
3
+ * alpha kept) and 16-bit PCM sound. The module lays out the movie; the
4
+ * bytes of each frame stay here, so its memory stays small, and `finish`
5
+ * hands back the file as parts in order.
6
+ */
7
+ import { pixelBytes, videoModule } from "./module.js";
8
+ const positive = (value, fallback) => Number.isFinite(value) && value >= 1 ? Math.floor(value) : fallback;
9
+ /** A MOV written frame by frame. */
10
+ export function createMovWriter(options) {
11
+ let encoder = new (videoModule().MovEncoder)(options.width, options.height, options.fps, positive(options.keyEvery, 1), options.repeats ?? 'unchanged');
12
+ const frames = [];
13
+ const open = () => {
14
+ if (encoder === null)
15
+ throw new Error('The MOV is finished.');
16
+ return encoder;
17
+ };
18
+ const release = () => {
19
+ encoder?.free();
20
+ encoder = null;
21
+ };
22
+ return {
23
+ add: (pixels) => {
24
+ const kept = open().add(pixelBytes(pixels));
25
+ frames.push(kept);
26
+ return kept.byteLength;
27
+ },
28
+ get byteLength() {
29
+ return open().byteLength;
30
+ },
31
+ finish: (sound) => {
32
+ const finishing = open();
33
+ try {
34
+ const channels = sound ? Math.max(1, Math.floor(sound.channels)) : 0;
35
+ const soundFrames = sound ? Math.floor(sound.pcm.byteLength / (channels * 2)) : 0;
36
+ const header = finishing.finish(soundFrames, sound ? Math.round(sound.sampleRate) : 0, channels);
37
+ const pieces = finishing.pieces();
38
+ const parts = [header];
39
+ const bytesPerFrame = channels * 2;
40
+ for (let at = 0; at < pieces.length; at += 3) {
41
+ if (pieces[at] === 0)
42
+ parts.push(frames[pieces[at + 1]]);
43
+ else
44
+ parts.push(sound.pcm.subarray(pieces[at + 1] * bytesPerFrame, pieces[at + 2] * bytesPerFrame));
45
+ }
46
+ return parts;
47
+ }
48
+ finally {
49
+ frames.length = 0;
50
+ release();
51
+ }
52
+ },
53
+ dispose: () => {
54
+ frames.length = 0;
55
+ release();
56
+ },
57
+ };
58
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The GIF Worker: runs GIF session steps on the WebAssembly module so the
3
+ * page's thread stays free. It says `ready` once the module is instantiated;
4
+ * finished files travel back transferred.
5
+ *
6
+ * A step that traps the module reports itself `fatal`: the page then drops
7
+ * this Worker (and the sessions in it) and starts a fresh one when needed.
8
+ */
9
+ export {};
package/src/worker.js ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * The GIF Worker: runs GIF session steps on the WebAssembly module so the
3
+ * page's thread stays free. It says `ready` once the module is instantiated;
4
+ * finished files travel back transferred.
5
+ *
6
+ * A step that traps the module reports itself `fatal`: the page then drops
7
+ * this Worker (and the sessions in it) and starts a fresh one when needed.
8
+ */
9
+ import { runGifStep } from "./gif-jobs.js";
10
+ import { videoModule } from "./module.js";
11
+ const scope = globalThis;
12
+ scope.onmessage = (event) => {
13
+ const { id, session, step } = event.data;
14
+ try {
15
+ const answer = runGifStep(session, step);
16
+ const transfer = answer instanceof Uint8Array ? [answer.buffer] : [];
17
+ scope.postMessage({ id, type: 'done', answer }, transfer);
18
+ }
19
+ catch (error) {
20
+ scope.postMessage({
21
+ id,
22
+ type: 'failed',
23
+ message: error instanceof Error ? error.message : String(error),
24
+ fatal: error instanceof WebAssembly.RuntimeError,
25
+ });
26
+ }
27
+ };
28
+ videoModule();
29
+ scope.postMessage({ type: 'ready' });