nanoodle 0.13.3 → 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.
@@ -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) => {