nanoodle 0.14.0 → 0.14.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.
- package/README.md +3 -3
- package/package.json +7 -5
- package/src/graph.mjs +6 -0
- package/src/local-media.mjs +5 -94
- package/src/mp4cat.mjs +40 -8
- package/src/png-decoder.mjs +226 -0
- package/src/sprite-avif.mjs +12 -0
- package/src/sprite-bitmap.mjs +233 -0
- package/src/sprite-image.mjs +73 -0
- package/src/sprite-model.mjs +18 -8
- package/src/sprite-orientation.mjs +65 -0
- package/src/sprite-projection.mjs +71 -36
- package/src/sprite-stack.mjs +42 -69
- package/src/sprite-video.mjs +262 -0
- package/src/zlib.mjs +23 -6
- package/src/sprite-cpu.mjs +0 -89
package/src/sprite-stack.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { number, viewPlan,
|
|
2
|
-
import {
|
|
1
|
+
import { number, viewPlan, softwareView, flag, turntablePlan } from "./sprite-projection.mjs";
|
|
2
|
+
import { encodePngRgba } from "./local-media.mjs";
|
|
3
|
+
import { encodeSpriteMp4 } from "./sprite-video.mjs";
|
|
3
4
|
import { MediaRef, bytesToDataUrl } from "./media.mjs";
|
|
4
5
|
import { gaussianModel, triangleModel } from "./sprite-model.mjs";
|
|
5
6
|
import { slicePixels } from "./sprite-slices.mjs";
|
|
@@ -35,8 +36,7 @@ async function imagePixels(blob) {
|
|
|
35
36
|
}
|
|
36
37
|
async function readMedia(value, ctx) {
|
|
37
38
|
const media = new MediaRef(String(value), { fetch: ctx?.fetch });
|
|
38
|
-
|
|
39
|
-
return new Blob([bytes], { type: media.mime || "application/octet-stream" });
|
|
39
|
+
return media.bytes({ signal: ctx?.signal });
|
|
40
40
|
}
|
|
41
41
|
function png(el) {
|
|
42
42
|
const result = el.toDataURL("image/png");
|
|
@@ -47,7 +47,7 @@ function png(el) {
|
|
|
47
47
|
function stackView(sheet, fields) {
|
|
48
48
|
const { tile, layers, scale, rise, squash, w, h } = viewPlan(sheet, fields);
|
|
49
49
|
const el = canvas(w, h),
|
|
50
|
-
ctx = el.getContext("2d");
|
|
50
|
+
ctx = el.getContext("2d", { willReadFrequently: flag(fields.turntable, false) });
|
|
51
51
|
function draw(yaw) {
|
|
52
52
|
ctx.resetTransform();
|
|
53
53
|
ctx.clearRect(0, 0, w, h);
|
|
@@ -61,62 +61,28 @@ function stackView(sheet, fields) {
|
|
|
61
61
|
}
|
|
62
62
|
ctx.resetTransform();
|
|
63
63
|
}
|
|
64
|
-
return { el, draw };
|
|
64
|
+
return { el, ctx, draw };
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
view.draw(from);
|
|
81
|
-
let stream = view.el.captureStream(0);
|
|
82
|
-
if (typeof stream.getVideoTracks()[0]?.requestFrame !== "function") {
|
|
83
|
-
stream.getTracks().forEach((track) => track.stop());
|
|
84
|
-
stream = view.el.captureStream(fps);
|
|
85
|
-
}
|
|
86
|
-
const [track] = stream.getVideoTracks();
|
|
87
|
-
let rec;
|
|
88
|
-
try {
|
|
89
|
-
rec = new MediaRecorder(stream, { mimeType: mime });
|
|
90
|
-
const chunks = [];
|
|
91
|
-
const stopped = new Promise((resolve, reject) => {
|
|
92
|
-
rec.ondataavailable = (e) => {
|
|
93
|
-
if (e.data.size) chunks.push(e.data);
|
|
94
|
-
};
|
|
95
|
-
rec.onerror = (e) => reject(e.error || new Error("Turntable recording failed."));
|
|
96
|
-
rec.onstop = resolve;
|
|
97
|
-
});
|
|
98
|
-
const start = performance.now();
|
|
99
|
-
async function animate() {
|
|
100
|
-
rec.start();
|
|
101
|
-
for (let frame = 0; frame < frames; frame++) {
|
|
102
|
-
signal?.throwIfAborted();
|
|
103
|
-
view.draw(from + ((to - from) * frame) / (frames - 1));
|
|
104
|
-
track.requestFrame?.();
|
|
105
|
-
const deadline = start + ((frame + 1) * duration * 1000) / frames;
|
|
106
|
-
await new Promise((resolve) => setTimeout(resolve, Math.max(0, deadline - performance.now())));
|
|
107
|
-
if (rec.state === "inactive") break;
|
|
67
|
+
async function frameUrl(frame) {
|
|
68
|
+
return bytesToDataUrl(await encodePngRgba(frame.width, frame.height, frame.data), "image/png");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function encodeTurntable(draw, fields, signal) {
|
|
72
|
+
const { duration, from, to, count } = turntablePlan(fields);
|
|
73
|
+
async function* frames() {
|
|
74
|
+
let lastYield = performance.now();
|
|
75
|
+
for (let i = 0; i < count; i++) {
|
|
76
|
+
// Give the editor and Node event loop time to deliver cancellation between frames.
|
|
77
|
+
if (performance.now() - lastYield >= 16) {
|
|
78
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
79
|
+
lastYield = performance.now();
|
|
108
80
|
}
|
|
109
|
-
|
|
81
|
+
signal?.throwIfAborted();
|
|
82
|
+
yield draw(from + ((to - from) * i) / (count - 1));
|
|
110
83
|
}
|
|
111
|
-
await Promise.all([animate(), stopped]);
|
|
112
|
-
signal?.throwIfAborted();
|
|
113
|
-
const blob = new Blob(chunks, { type: rec.mimeType.split(";")[0] });
|
|
114
|
-
if (!blob.size) throw new Error("The browser produced an empty turntable recording.");
|
|
115
|
-
return bytesToDataUrl(new Uint8Array(await blob.arrayBuffer()), blob.type);
|
|
116
|
-
} finally {
|
|
117
|
-
if (rec && rec.state !== "inactive") rec.stop();
|
|
118
|
-
stream.getTracks().forEach((t) => t.stop());
|
|
119
84
|
}
|
|
85
|
+
return bytesToDataUrl(await encodeSpriteMp4(frames(), duration), "video/mp4");
|
|
120
86
|
}
|
|
121
87
|
|
|
122
88
|
export async function slice3d(node, inp, ctx) {
|
|
@@ -124,14 +90,15 @@ export async function slice3d(node, inp, ctx) {
|
|
|
124
90
|
const fields = node.fields || {},
|
|
125
91
|
size = number(fields, "resolution", 48, 16, 128, true),
|
|
126
92
|
layers = number(fields, "slices", 48, 8, 128, true);
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
93
|
+
const headless = typeof document === "undefined";
|
|
94
|
+
const pixels = headless
|
|
95
|
+
? (await import("./sprite-image.mjs")).decodeSpriteBytes
|
|
96
|
+
: (raw, mime) => imagePixels(new Blob([raw], { type: mime }));
|
|
97
|
+
const bytes = await readMedia(inp.model, ctx);
|
|
131
98
|
const model =
|
|
132
99
|
bytes[0] === 112 && bytes[1] === 108 && bytes[2] === 121
|
|
133
100
|
? gaussianModel(bytes)
|
|
134
|
-
: await triangleModel(bytes,
|
|
101
|
+
: await triangleModel(bytes, pixels);
|
|
135
102
|
const palette = number(fields, "palette", 16, 0, 64, true);
|
|
136
103
|
const supplied = palette && inp.paletteImage ? await pixels(await readMedia(inp.paletteImage, ctx)) : null;
|
|
137
104
|
ctx?.signal?.throwIfAborted();
|
|
@@ -140,12 +107,12 @@ export async function slice3d(node, inp, ctx) {
|
|
|
140
107
|
{ size, layers, axis: fields.upAxis || "auto", outline: flag(fields.outline, true), palette },
|
|
141
108
|
supplied,
|
|
142
109
|
);
|
|
143
|
-
if (
|
|
110
|
+
if (headless) {
|
|
144
111
|
const sheet = { width: sliced.width, height: sliced.height, data: sliced.rgba };
|
|
145
112
|
return {
|
|
146
113
|
sheet: await frameUrl(sheet),
|
|
147
114
|
layout: JSON.stringify(sliced.layout),
|
|
148
|
-
image: await frameUrl(
|
|
115
|
+
image: await frameUrl(softwareView(sheet, {})(35)),
|
|
149
116
|
};
|
|
150
117
|
}
|
|
151
118
|
const el = canvas(size * layers, size);
|
|
@@ -158,21 +125,27 @@ export async function slice3d(node, inp, ctx) {
|
|
|
158
125
|
export async function renderSpriteStack(node, inp, ctx) {
|
|
159
126
|
if (!inp.sheet) throw new Error("Wire the Slice 3D sheet into Sprite stack render.");
|
|
160
127
|
const fields = node.fields || {},
|
|
161
|
-
|
|
128
|
+
bytes = await readMedia(inp.sheet, ctx);
|
|
162
129
|
const yaw = number(fields, "yaw", 35, -3600, 3600);
|
|
163
130
|
if (typeof document === "undefined") {
|
|
164
|
-
const
|
|
165
|
-
const
|
|
131
|
+
const { decodeSpriteBytes } = await import("./sprite-image.mjs");
|
|
132
|
+
const sheet = await decodeSpriteBytes(bytes);
|
|
133
|
+
const draw = softwareView(sheet, fields);
|
|
134
|
+
const image = await frameUrl(draw(yaw));
|
|
166
135
|
if (!flag(fields.turntable, false)) return { image };
|
|
167
|
-
return { image, video: await encodeTurntable(
|
|
136
|
+
return { image, video: await encodeTurntable(draw, fields, ctx?.signal) };
|
|
168
137
|
}
|
|
169
|
-
const sheet = await bitmap(
|
|
138
|
+
const sheet = await bitmap(new Blob([bytes]));
|
|
170
139
|
try {
|
|
171
140
|
const view = stackView(sheet, fields);
|
|
172
141
|
view.draw(yaw);
|
|
173
142
|
const image = png(view.el);
|
|
174
143
|
if (!flag(fields.turntable, false)) return { image };
|
|
175
|
-
|
|
144
|
+
const draw = (angle) => {
|
|
145
|
+
view.draw(angle);
|
|
146
|
+
return view.ctx.getImageData(0, 0, view.el.width, view.el.height);
|
|
147
|
+
};
|
|
148
|
+
return { image, video: await encodeTurntable(draw, fields, ctx?.signal) };
|
|
176
149
|
} finally {
|
|
177
150
|
sheet.close?.();
|
|
178
151
|
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// A small H.264 Baseline encoder for software-rendered pixel art. I_PCM
|
|
2
|
+
// preserves changed macroblocks; P_Skip cheaply repeats stationary areas.
|
|
3
|
+
// These are standard H.264 macroblock types. MP4CAT supplies the container.
|
|
4
|
+
import { MP4CAT } from "./mp4cat.mjs";
|
|
5
|
+
|
|
6
|
+
const GOP = 60;
|
|
7
|
+
const BLOCK_BYTES = 16 * 16 + 2 * 8 * 8;
|
|
8
|
+
|
|
9
|
+
class Bits {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.buffer = new Uint8Array(128);
|
|
12
|
+
this.size = 0;
|
|
13
|
+
this.partial = 0;
|
|
14
|
+
this.used = 0;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
reserve(count) {
|
|
18
|
+
if (this.size + count <= this.buffer.length) return;
|
|
19
|
+
const next = new Uint8Array(Math.max(this.buffer.length * 2, this.size + count));
|
|
20
|
+
next.set(this.buffer.subarray(0, this.size));
|
|
21
|
+
this.buffer = next;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
bit(value) {
|
|
25
|
+
this.partial = (this.partial << 1) | Number(value);
|
|
26
|
+
if (++this.used === 8) this.align();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
align() {
|
|
30
|
+
if (!this.used) return;
|
|
31
|
+
this.reserve(1);
|
|
32
|
+
this.buffer[this.size++] = this.partial << (8 - this.used);
|
|
33
|
+
this.partial = 0;
|
|
34
|
+
this.used = 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
uint(value, length) {
|
|
38
|
+
for (let i = length - 1; i >= 0; i--) this.bit((value >>> i) & 1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
ue(value) {
|
|
42
|
+
const code = value + 1;
|
|
43
|
+
const length = 32 - Math.clz32(code);
|
|
44
|
+
this.uint(0, length - 1);
|
|
45
|
+
this.uint(code, length);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
bytes(bytes) {
|
|
49
|
+
this.align();
|
|
50
|
+
this.reserve(bytes.length);
|
|
51
|
+
this.buffer.set(bytes, this.size);
|
|
52
|
+
this.size += bytes.length;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
finish() {
|
|
56
|
+
this.bit(1);
|
|
57
|
+
this.align();
|
|
58
|
+
return this.buffer.subarray(0, this.size);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// H.264 7.4.1: escape start-code emulation even inside raw PCM. MP4 video
|
|
63
|
+
// samples carry a four-byte length; avcC stores the parameter sets separately.
|
|
64
|
+
function nal(header, rbsp, lengthPrefixed = false) {
|
|
65
|
+
let extra = 0, zeros = 0;
|
|
66
|
+
for (const b of rbsp) {
|
|
67
|
+
if (zeros === 2 && b <= 3) { extra++; zeros = 0; }
|
|
68
|
+
zeros = b === 0 ? zeros + 1 : 0;
|
|
69
|
+
}
|
|
70
|
+
const length = 1 + rbsp.length + extra;
|
|
71
|
+
const out = new Uint8Array((lengthPrefixed ? 4 : 0) + length);
|
|
72
|
+
let pos = lengthPrefixed ? 4 : 0;
|
|
73
|
+
if (lengthPrefixed) new DataView(out.buffer).setUint32(0, length);
|
|
74
|
+
out[pos++] = header;
|
|
75
|
+
if (!extra) {
|
|
76
|
+
out.set(rbsp, pos);
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
zeros = 0;
|
|
80
|
+
for (const b of rbsp) {
|
|
81
|
+
if (zeros === 2 && b <= 3) { out[pos++] = 3; zeros = 0; }
|
|
82
|
+
out[pos++] = b;
|
|
83
|
+
zeros = b === 0 ? zeros + 1 : 0;
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Annex A, Tables A-1/A-2: frame size in macroblocks, MB/s, baseline kb/s.
|
|
89
|
+
// Start at level 3, supported by conventional mobile and desktop AVC players.
|
|
90
|
+
function avcLevel(cols, rows, fps, kbps) {
|
|
91
|
+
const levels = [
|
|
92
|
+
[30, 1620, 40500, 10000], [31, 3600, 108000, 14000], [32, 5120, 216000, 20000],
|
|
93
|
+
[40, 8192, 245760, 20000], [41, 8192, 245760, 50000], [42, 8704, 522240, 50000],
|
|
94
|
+
[50, 22080, 589824, 135000], [51, 36864, 983040, 240000], [52, 36864, 2073600, 240000],
|
|
95
|
+
[60, 139264, 4177920, 240000], [61, 139264, 8355840, 480000], [62, 139264, 16711680, 800000],
|
|
96
|
+
];
|
|
97
|
+
const blocks = cols * rows;
|
|
98
|
+
const level = levels.find(([, fs, mbps, br]) => blocks <= fs && Math.max(cols, rows) ** 2 <= 8 * fs &&
|
|
99
|
+
blocks * fps <= mbps && kbps <= br);
|
|
100
|
+
if (!level) throw new Error("Sprite video exceeds H.264 Baseline level limits.");
|
|
101
|
+
return level[0];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function parameterSets(width, height, cols, rows, level) {
|
|
105
|
+
const s = new Bits();
|
|
106
|
+
s.bytes(Uint8Array.of(66, 0xc0, level));
|
|
107
|
+
s.ue(0); s.ue(0); s.ue(2); s.ue(1); // SPS id, four frame_num bits, POC type 2, one reference
|
|
108
|
+
s.bit(0); s.ue(cols - 1); s.ue(rows - 1);
|
|
109
|
+
s.bit(1); s.bit(1); // progressive, direct_8x8_inference
|
|
110
|
+
const right = (cols * 16 - width) / 2;
|
|
111
|
+
const bottom = (rows * 16 - height) / 2;
|
|
112
|
+
s.bit(!!(right || bottom));
|
|
113
|
+
if (right || bottom) { s.ue(0); s.ue(right); s.ue(0); s.ue(bottom); }
|
|
114
|
+
// Square pixels, limited-range BT.601 matrix applied to sRGB primaries
|
|
115
|
+
// and transfer. Explicit metadata keeps large and small clips consistent.
|
|
116
|
+
s.bit(1); // VUI present
|
|
117
|
+
s.bit(1); s.uint(1, 8); // 1:1 sample aspect ratio
|
|
118
|
+
s.bit(0); // no overscan information
|
|
119
|
+
s.bit(1); s.uint(5, 3); s.bit(0); s.bit(1);
|
|
120
|
+
s.uint(1, 8); s.uint(13, 8); s.uint(6, 8);
|
|
121
|
+
s.bit(1); s.ue(1); s.ue(1); // centred 2x2 chroma samples
|
|
122
|
+
s.uint(0, 5); // timing, NAL/VCL HRD, pic_struct, restrictions
|
|
123
|
+
const p = new Bits();
|
|
124
|
+
p.ue(0); p.ue(0); p.bit(0); p.bit(0); // CAVLC, one slice group
|
|
125
|
+
p.ue(0); p.ue(0); p.ue(0);
|
|
126
|
+
p.bit(0); p.uint(0, 2); // weighted prediction
|
|
127
|
+
p.ue(0); p.ue(0); p.ue(0); // signed Exp-Golomb zero QP offsets
|
|
128
|
+
p.bit(1); p.bit(0); p.bit(0); // deblock control present
|
|
129
|
+
return [nal(0x67, s.finish()), nal(0x68, p.finish())];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// BT.601 limited-range Y'CbCr. Composite onto black before subsampling;
|
|
133
|
+
// repeat the outermost pixel for partial right/bottom macroblocks. Store
|
|
134
|
+
// each block contiguously in H.264's Y, Cb, Cr order for direct I_PCM writes.
|
|
135
|
+
function packBlocks(frame, cols, rows, blocks) {
|
|
136
|
+
const { data, width, height } = frame;
|
|
137
|
+
for (let by = 0; by < rows; by++) for (let bx = 0; bx < cols; bx++) {
|
|
138
|
+
const start = (by * cols + bx) * BLOCK_BYTES;
|
|
139
|
+
let chroma = 0;
|
|
140
|
+
for (let y = 0; y < 16; y += 2) for (let x = 0; x < 16; x += 2) {
|
|
141
|
+
let red = 0, green = 0, blue = 0;
|
|
142
|
+
for (let dy = 0; dy < 2; dy++) {
|
|
143
|
+
const sy = Math.min(by * 16 + y + dy, height - 1);
|
|
144
|
+
for (let dx = 0; dx < 2; dx++) {
|
|
145
|
+
const sx = Math.min(bx * 16 + x + dx, width - 1);
|
|
146
|
+
const pixel = (sy * width + sx) * 4;
|
|
147
|
+
const alpha = data[pixel + 3];
|
|
148
|
+
let r = 0, g = 0, b = 0;
|
|
149
|
+
if (alpha === 255) {
|
|
150
|
+
r = data[pixel]; g = data[pixel + 1]; b = data[pixel + 2];
|
|
151
|
+
} else if (alpha) {
|
|
152
|
+
r = Math.floor((data[pixel] * alpha + 127) / 255);
|
|
153
|
+
g = Math.floor((data[pixel + 1] * alpha + 127) / 255);
|
|
154
|
+
b = Math.floor((data[pixel + 2] * alpha + 127) / 255);
|
|
155
|
+
}
|
|
156
|
+
blocks[start + (y + dy) * 16 + x + dx] = 16 + ((66 * r + 129 * g + 25 * b + 128) >> 8);
|
|
157
|
+
red += r; green += g; blue += b;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
blocks[start + 256 + chroma] = 128 + ((-38 * red - 74 * green + 112 * blue + 512) >> 10);
|
|
161
|
+
blocks[start + 320 + chroma] = 128 + ((112 * red - 94 * green - 18 * blue + 512) >> 10);
|
|
162
|
+
chroma++;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// A flat block can use an Intra_16x16 directional prediction when its
|
|
168
|
+
// left or top neighbour is the same colour. No transform coefficients then
|
|
169
|
+
// need transmitting. Record a colour only when every sample matches.
|
|
170
|
+
function flatColor(words, offset) {
|
|
171
|
+
const y = words[offset], cb = words[offset + 64], cr = words[offset + 80];
|
|
172
|
+
if (y !== (y >>> 24) * 0x01010101 || cb !== (cb >>> 24) * 0x01010101 ||
|
|
173
|
+
cr !== (cr >>> 24) * 0x01010101) return -1;
|
|
174
|
+
for (let i = 1; i < 64; i++) if (words[offset + i] !== y) return -1;
|
|
175
|
+
for (let i = 1; i < 16; i++) {
|
|
176
|
+
if (words[offset + 64 + i] !== cb || words[offset + 80 + i] !== cr) return -1;
|
|
177
|
+
}
|
|
178
|
+
return ((y & 255) << 16) | ((cb & 255) << 8) | (cr & 255);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function encodeSlice(index, current, previous, cols) {
|
|
182
|
+
const idr = index % GOP === 0;
|
|
183
|
+
const s = new Bits();
|
|
184
|
+
s.ue(0); s.ue(idr ? 2 : 0); s.ue(0); // first macroblock, I/P slice, PPS id
|
|
185
|
+
s.uint((index % GOP) & 15, 4);
|
|
186
|
+
if (idr) {
|
|
187
|
+
s.ue(Math.floor(index / GOP) & 1); // alternate idr_pic_id
|
|
188
|
+
s.bit(0); s.bit(0); // no_output_of_prior_pics, long_term_reference
|
|
189
|
+
} else {
|
|
190
|
+
s.bit(0); s.bit(0); s.bit(0); // default reference list and sliding reference window
|
|
191
|
+
}
|
|
192
|
+
s.ue(0); s.ue(1); // zero QP delta, disabled deblocking (exact P_Skip)
|
|
193
|
+
const curWords = new Uint32Array(current.buffer);
|
|
194
|
+
const refWords = new Uint32Array(previous.buffer);
|
|
195
|
+
const colors = new Int32Array(cols);
|
|
196
|
+
const pcm = new Uint8Array(cols);
|
|
197
|
+
let skipped = 0;
|
|
198
|
+
for (let offset = 0, block = 0; offset < current.length; offset += BLOCK_BYTES, block++) {
|
|
199
|
+
const x = block % cols;
|
|
200
|
+
const hasLeft = x !== 0, hasTop = block >= cols;
|
|
201
|
+
const color = flatColor(curWords, offset / 4);
|
|
202
|
+
const matchLeft = color >= 0 && hasLeft && colors[x - 1] === color;
|
|
203
|
+
const matchTop = color >= 0 && hasTop && colors[x] === color;
|
|
204
|
+
colors[x] = color;
|
|
205
|
+
if (!idr) {
|
|
206
|
+
let word = offset / 4;
|
|
207
|
+
const end = word + BLOCK_BYTES / 4;
|
|
208
|
+
while (word < end && curWords[word] === refWords[word]) word++;
|
|
209
|
+
if (word === end) { pcm[x] = 0; skipped++; continue; }
|
|
210
|
+
s.ue(skipped);
|
|
211
|
+
skipped = 0;
|
|
212
|
+
}
|
|
213
|
+
if (matchLeft || matchTop) {
|
|
214
|
+
const horizontal = matchLeft;
|
|
215
|
+
s.ue((idr ? 1 : 6) + Number(horizontal)); // zero AC/chroma, luma vertical or horizontal
|
|
216
|
+
s.ue(horizontal ? 1 : 2); // chroma horizontal or vertical
|
|
217
|
+
s.ue(0); // no QP delta
|
|
218
|
+
// A PCM neighbour contributes 16 to nC. With our only other modes
|
|
219
|
+
// (skipped or zero residual), the CAVLC nC table is either 0 or >=8.
|
|
220
|
+
if ((hasLeft && pcm[x - 1]) || (hasTop && pcm[x])) s.uint(3, 6);
|
|
221
|
+
else s.bit(1); // coeff_token for zero luma DC coefficients
|
|
222
|
+
pcm[x] = 0;
|
|
223
|
+
} else {
|
|
224
|
+
s.ue(idr ? 25 : 30); // I_PCM in the I/P mb_type tables
|
|
225
|
+
s.bytes(current.subarray(offset, offset + BLOCK_BYTES));
|
|
226
|
+
pcm[x] = 1;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (skipped) s.ue(skipped);
|
|
230
|
+
return { bytes: nal(idr ? 0x65 : 0x41, s.finish(), true), sync: idr };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export async function encodeSpriteMp4(frames, duration) {
|
|
234
|
+
if (!(Number.isFinite(duration) && duration > 0)) throw new Error("Invalid sprite video duration.");
|
|
235
|
+
let width, height, cols, rows, current, previous;
|
|
236
|
+
const samples = [];
|
|
237
|
+
let totalBytes = 0;
|
|
238
|
+
for await (const frame of frames) {
|
|
239
|
+
if (!samples.length) {
|
|
240
|
+
({ width, height } = frame);
|
|
241
|
+
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 2 || height < 2 ||
|
|
242
|
+
width > 65534 || height > 65534 || (width & 1) || (height & 1))
|
|
243
|
+
throw new Error("Sprite video requires even pixel dimensions.");
|
|
244
|
+
cols = Math.ceil(width / 16); rows = Math.ceil(height / 16);
|
|
245
|
+
current = new Uint8Array(cols * rows * BLOCK_BYTES);
|
|
246
|
+
previous = new Uint8Array(current.length);
|
|
247
|
+
}
|
|
248
|
+
if (frame.width !== width || frame.height !== height || frame.data.length !== width * height * 4)
|
|
249
|
+
throw new Error("Sprite video frames have inconsistent dimensions.");
|
|
250
|
+
packBlocks(frame, cols, rows, current);
|
|
251
|
+
const sample = encodeSlice(samples.length, current, previous, cols);
|
|
252
|
+
samples.push(sample);
|
|
253
|
+
totalBytes += sample.bytes.length;
|
|
254
|
+
// One 32-bit chunk offset per frame. Reject before accumulating further
|
|
255
|
+
// samples; muxTracks also checks the final layout before allocating it.
|
|
256
|
+
if (totalBytes + 40 > 0xffffffff) throw new Error("Sprite video exceeds the 32-bit MP4 size limit.");
|
|
257
|
+
[current, previous] = [previous, current];
|
|
258
|
+
}
|
|
259
|
+
if (!samples.length) throw new Error("Sprite video needs at least one frame.");
|
|
260
|
+
const level = avcLevel(cols, rows, samples.length / duration, totalBytes * 8 / duration / 1000);
|
|
261
|
+
return MP4CAT.muxAvc(samples, width, height, duration, ...parameterSets(width, height, cols, rows, level));
|
|
262
|
+
}
|
package/src/zlib.mjs
CHANGED
|
@@ -12,15 +12,32 @@ async function nodeZlib() {
|
|
|
12
12
|
return _zlib;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
async function pipeThrough(bytes, stream) {
|
|
15
|
+
async function pipeThrough(bytes, stream, maxOutputLength) {
|
|
16
16
|
const out = new Blob([bytes]).stream().pipeThrough(stream);
|
|
17
|
-
return new Uint8Array(await new Response(out).arrayBuffer());
|
|
17
|
+
if (maxOutputLength === undefined) return new Uint8Array(await new Response(out).arrayBuffer());
|
|
18
|
+
const reader = out.getReader();
|
|
19
|
+
const data = new Uint8Array(maxOutputLength);
|
|
20
|
+
let size = 0;
|
|
21
|
+
try {
|
|
22
|
+
for (;;) {
|
|
23
|
+
const {done, value} = await reader.read();
|
|
24
|
+
if (done) return data.subarray(0, size);
|
|
25
|
+
if (value.length > data.length-size) {
|
|
26
|
+
await reader.cancel();
|
|
27
|
+
throw new Error("Decompressed data exceeds the declared image size");
|
|
28
|
+
}
|
|
29
|
+
data.set(value, size);
|
|
30
|
+
size += value.length;
|
|
31
|
+
}
|
|
32
|
+
} finally {
|
|
33
|
+
reader.releaseLock();
|
|
34
|
+
}
|
|
18
35
|
}
|
|
19
36
|
|
|
20
|
-
export async function inflate(bytes) {
|
|
37
|
+
export async function inflate(bytes, maxOutputLength) {
|
|
21
38
|
const z = await nodeZlib();
|
|
22
|
-
if (z) return new Uint8Array(z.inflateSync(bytes));
|
|
23
|
-
return streamZlib.inflate(bytes);
|
|
39
|
+
if (z) return new Uint8Array(z.inflateSync(bytes, {maxOutputLength}));
|
|
40
|
+
return streamZlib.inflate(bytes, maxOutputLength);
|
|
24
41
|
}
|
|
25
42
|
|
|
26
43
|
export async function deflate(bytes, { level = 9 } = {}) {
|
|
@@ -92,7 +109,7 @@ export async function gunzipLax(bytes) {
|
|
|
92
109
|
// The browser implementations, exported so tests can exercise them in Node
|
|
93
110
|
// (which also ships Compression/DecompressionStream) without hiding node:zlib.
|
|
94
111
|
export const streamZlib = {
|
|
95
|
-
inflate: (b) => pipeThrough(b, new DecompressionStream("deflate")),
|
|
112
|
+
inflate: (b, limit) => pipeThrough(b, new DecompressionStream("deflate"), limit),
|
|
96
113
|
deflate: (b) => pipeThrough(b, new CompressionStream("deflate")),
|
|
97
114
|
gunzip: (b) => pipeThrough(b, new DecompressionStream("gzip")),
|
|
98
115
|
gunzipLax: (b) => {
|
package/src/sprite-cpu.mjs
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { bytesToDataUrl, sniffMime, extForMime } from "./media.mjs";
|
|
2
|
-
import { decodePng, encodePngRgba, runProc, withTemp } from "./local-media.mjs";
|
|
3
|
-
import { softwareFrame, turntablePlan } from "./sprite-projection.mjs";
|
|
4
|
-
|
|
5
|
-
export async function decodeSpriteImage(blob, signal) {
|
|
6
|
-
const bytes = new Uint8Array(await blob.arrayBuffer());
|
|
7
|
-
if (bytes[0] === 137 && bytes[1] === 80) {
|
|
8
|
-
try {
|
|
9
|
-
const { w, h, rgba } = await decodePng(bytes, true);
|
|
10
|
-
return { width: w, height: h, data: rgba };
|
|
11
|
-
} catch (error) {
|
|
12
|
-
if (signal?.aborted) throw error;
|
|
13
|
-
// FFmpeg handles indexed, interlaced and higher-bit-depth PNGs.
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
return withTemp(async (dir) => {
|
|
17
|
-
const { writeFile, readFile } = await import("node:fs/promises");
|
|
18
|
-
const input = dir + "/source." + extForMime(sniffMime(bytes)),
|
|
19
|
-
output = dir + "/image.png";
|
|
20
|
-
await writeFile(input, bytes);
|
|
21
|
-
await runProc(
|
|
22
|
-
"ffmpeg",
|
|
23
|
-
[
|
|
24
|
-
"-nostdin",
|
|
25
|
-
"-v",
|
|
26
|
-
"error",
|
|
27
|
-
"-protocol_whitelist",
|
|
28
|
-
"file",
|
|
29
|
-
"-f",
|
|
30
|
-
"image2",
|
|
31
|
-
"-i",
|
|
32
|
-
input,
|
|
33
|
-
"-frames:v",
|
|
34
|
-
"1",
|
|
35
|
-
"-pix_fmt",
|
|
36
|
-
"rgba",
|
|
37
|
-
"-c:v",
|
|
38
|
-
"png",
|
|
39
|
-
output,
|
|
40
|
-
],
|
|
41
|
-
{ signal },
|
|
42
|
-
);
|
|
43
|
-
const { w, h, rgba } = await decodePng(new Uint8Array(await readFile(output)), true);
|
|
44
|
-
return { width: w, height: h, data: rgba };
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export async function frameUrl(frame) {
|
|
49
|
-
return bytesToDataUrl(await encodePngRgba(frame.width, frame.height, frame.data), "image/png");
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function encodeTurntable(sheet, fields, signal) {
|
|
53
|
-
const { duration, from, to, count } = turntablePlan(fields);
|
|
54
|
-
return withTemp(async (dir) => {
|
|
55
|
-
const { writeFile, readFile } = await import("node:fs/promises");
|
|
56
|
-
for (let i = 0; i < count; i++) {
|
|
57
|
-
signal?.throwIfAborted();
|
|
58
|
-
const frame = softwareFrame(sheet, fields, from + ((to - from) * i) / (count - 1));
|
|
59
|
-
const png = await encodePngRgba(frame.width, frame.height, frame.data);
|
|
60
|
-
await writeFile(dir + "/" + String(i).padStart(5, "0") + ".png", png);
|
|
61
|
-
}
|
|
62
|
-
const args = [
|
|
63
|
-
"-nostdin",
|
|
64
|
-
"-v",
|
|
65
|
-
"error",
|
|
66
|
-
"-framerate",
|
|
67
|
-
String(count / duration),
|
|
68
|
-
"-i",
|
|
69
|
-
dir + "/%05d.png",
|
|
70
|
-
"-an",
|
|
71
|
-
];
|
|
72
|
-
const out = dir + "/turntable.mp4";
|
|
73
|
-
try {
|
|
74
|
-
await runProc(
|
|
75
|
-
"ffmpeg",
|
|
76
|
-
[...args, "-c:v", "libx264", "-pix_fmt", "yuv420p", "-movflags", "+faststart", out],
|
|
77
|
-
{ signal },
|
|
78
|
-
);
|
|
79
|
-
} catch (error) {
|
|
80
|
-
if (!/unknown encoder.*libx264|encoder.*libx264.*not found/i.test(error.message)) throw error;
|
|
81
|
-
await runProc(
|
|
82
|
-
"ffmpeg",
|
|
83
|
-
[...args, "-y", "-c:v", "mpeg4", "-q:v", "3", "-pix_fmt", "yuv420p", "-movflags", "+faststart", out],
|
|
84
|
-
{ signal },
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
return bytesToDataUrl(new Uint8Array(await readFile(out)), "video/mp4");
|
|
88
|
-
});
|
|
89
|
-
}
|