@hiroosy/baileys-lite 0.4.3 → 0.4.5

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 (61) hide show
  1. package/README.md +8 -17
  2. package/WAProto/index.d.ts +4 -0
  3. package/WAProto/parts/Message.part +42 -0
  4. package/package.json +1 -1
  5. package/src/ffmpeg/LICENSE +502 -0
  6. package/src/ffmpeg/index.js +64 -0
  7. package/src/ffmpeg/lib/bits.js +150 -0
  8. package/src/ffmpeg/lib/bsf.js +242 -0
  9. package/src/ffmpeg/lib/codec/flac.js +801 -0
  10. package/src/ffmpeg/lib/codec/index.js +108 -0
  11. package/src/ffmpeg/lib/codec/mp3.js +768 -0
  12. package/src/ffmpeg/lib/codec/mpa.js +65 -0
  13. package/src/ffmpeg/lib/codec/mpa_tables.js +27 -0
  14. package/src/ffmpeg/lib/codec/opus.js +140 -0
  15. package/src/ffmpeg/lib/codec/pcm.js +198 -0
  16. package/src/ffmpeg/lib/compat.js +42 -0
  17. package/src/ffmpeg/lib/crc.js +60 -0
  18. package/src/ffmpeg/lib/demux/adts.js +151 -0
  19. package/src/ffmpeg/lib/demux/flac.js +150 -0
  20. package/src/ffmpeg/lib/demux/image.js +47 -0
  21. package/src/ffmpeg/lib/demux/index.js +60 -0
  22. package/src/ffmpeg/lib/demux/matroska.js +525 -0
  23. package/src/ffmpeg/lib/demux/merge.js +106 -0
  24. package/src/ffmpeg/lib/demux/mp3.js +199 -0
  25. package/src/ffmpeg/lib/demux/mp4.js +622 -0
  26. package/src/ffmpeg/lib/demux/ogg.js +250 -0
  27. package/src/ffmpeg/lib/demux/ts.js +354 -0
  28. package/src/ffmpeg/lib/demux/wav.js +156 -0
  29. package/src/ffmpeg/lib/errors.js +27 -0
  30. package/src/ffmpeg/lib/filter/gain.js +69 -0
  31. package/src/ffmpeg/lib/filter/mix.js +95 -0
  32. package/src/ffmpeg/lib/filter/resample.js +187 -0
  33. package/src/ffmpeg/lib/filter/video.js +575 -0
  34. package/src/ffmpeg/lib/image/bmp.js +123 -0
  35. package/src/ffmpeg/lib/image/frame.js +216 -0
  36. package/src/ffmpeg/lib/image/gif.js +215 -0
  37. package/src/ffmpeg/lib/image/index.js +70 -0
  38. package/src/ffmpeg/lib/image/jpeg.js +964 -0
  39. package/src/ffmpeg/lib/image/png.js +256 -0
  40. package/src/ffmpeg/lib/image/run.js +67 -0
  41. package/src/ffmpeg/lib/io.js +298 -0
  42. package/src/ffmpeg/lib/mux/adts.js +75 -0
  43. package/src/ffmpeg/lib/mux/flac.js +79 -0
  44. package/src/ffmpeg/lib/mux/mp3.js +85 -0
  45. package/src/ffmpeg/lib/mux/mp4.js +450 -0
  46. package/src/ffmpeg/lib/mux/ogg.js +146 -0
  47. package/src/ffmpeg/lib/mux/wav.js +94 -0
  48. package/src/ffmpeg/lib/net.js +270 -0
  49. package/src/ffmpeg/lib/pipeline.js +579 -0
  50. package/src/ffmpeg/lib/probe.js +45 -0
  51. package/src/ffmpeg/lib/tags.js +307 -0
  52. package/src/ffmpeg/lib/util.js +104 -0
  53. package/src/foundation/image-thumb.js +4 -56
  54. package/src/socket/messages-recv.js +5 -6
  55. package/src/socket/socket-core.js +3 -3
  56. package/src/utils/media.js +8 -85
  57. package/src/utils/message-compose.js +19 -11
  58. package/src/utils/wa-protocol-core.js +5 -5
  59. package/src/voip/index.js +16 -50
  60. package/src/voip/media/WaAudioEngine.js +8 -216
  61. package/src/voip/media/WaVideoEngine.js +8 -237
@@ -0,0 +1,575 @@
1
+ import { badArg, noSupport } from '../errors.js';
2
+ import { createRgba, createYuv, createGray, toRgba, planeSize } from '../image/frame.js';
3
+
4
+ const functions = {
5
+ trunc: Math.trunc,
6
+ floor: Math.floor,
7
+ ceil: Math.ceil,
8
+ round: (v) => (v < 0 ? -Math.round(-v) : Math.round(v)),
9
+ abs: Math.abs,
10
+ sqrt: Math.sqrt,
11
+ sin: Math.sin,
12
+ cos: Math.cos,
13
+ log: Math.log,
14
+ exp: Math.exp,
15
+ min: Math.min,
16
+ max: Math.max,
17
+ pow: Math.pow,
18
+ mod: (a, b) => a % b,
19
+ gt: (a, b) => (a > b ? 1 : 0),
20
+ gte: (a, b) => (a >= b ? 1 : 0),
21
+ lt: (a, b) => (a < b ? 1 : 0),
22
+ lte: (a, b) => (a <= b ? 1 : 0),
23
+ eq: (a, b) => (a === b ? 1 : 0),
24
+ not: (a) => (a ? 0 : 1),
25
+ if: (a, b, c) => (a ? b : c === undefined ? 0 : c),
26
+ ifnot: (a, b, c) => (a ? (c === undefined ? 0 : c) : b),
27
+ lerp: (a, b, t) => a + (b - a) * t,
28
+ clip: (v, lo, hi) => Math.min(hi, Math.max(lo, v)),
29
+ };
30
+
31
+ function evaluate(text, vars) {
32
+ const src = String(text).trim();
33
+ let pos = 0;
34
+ const peek = () => {
35
+ while (pos < src.length && src[pos] === ' ') pos++;
36
+ return src[pos];
37
+ };
38
+ const parseExpr = () => {
39
+ let v = parseTerm();
40
+ for (;;) {
41
+ const c = peek();
42
+ if (c === '+') {
43
+ pos++;
44
+ v += parseTerm();
45
+ } else if (c === '-') {
46
+ pos++;
47
+ v -= parseTerm();
48
+ } else return v;
49
+ }
50
+ };
51
+ const parseTerm = () => {
52
+ let v = parsePower();
53
+ for (;;) {
54
+ const c = peek();
55
+ if (c === '*') {
56
+ pos++;
57
+ v *= parsePower();
58
+ } else if (c === '/') {
59
+ pos++;
60
+ v /= parsePower();
61
+ } else return v;
62
+ }
63
+ };
64
+ const parsePower = () => {
65
+ const base = parseUnary();
66
+ if (peek() === '^') {
67
+ pos++;
68
+ return Math.pow(base, parsePower());
69
+ }
70
+ return base;
71
+ };
72
+ const parseUnary = () => {
73
+ const c = peek();
74
+ if (c === '-') {
75
+ pos++;
76
+ return -parseUnary();
77
+ }
78
+ if (c === '+') {
79
+ pos++;
80
+ return parseUnary();
81
+ }
82
+ return parsePrimary();
83
+ };
84
+ const parsePrimary = () => {
85
+ const c = peek();
86
+ if (c === '(') {
87
+ pos++;
88
+ const v = parseExpr();
89
+ if (peek() !== ')') throw badArg('invalid expression: ' + src);
90
+ pos++;
91
+ return v;
92
+ }
93
+ const num = /^(\d+\.?\d*|\.\d+)(e[+-]?\d+)?/i.exec(src.slice(pos));
94
+ if (num) {
95
+ pos += num[0].length;
96
+ return Number(num[0]);
97
+ }
98
+ const id = /^[A-Za-z_][A-Za-z0-9_]*/.exec(src.slice(pos));
99
+ if (!id) throw badArg('invalid expression: ' + src);
100
+ pos += id[0].length;
101
+ const name = id[0];
102
+ if (peek() === '(') {
103
+ pos++;
104
+ const args = [];
105
+ if (peek() !== ')') {
106
+ for (;;) {
107
+ args.push(parseExpr());
108
+ if (peek() === ',') {
109
+ pos++;
110
+ continue;
111
+ }
112
+ break;
113
+ }
114
+ }
115
+ if (peek() !== ')') throw badArg('invalid expression: ' + src);
116
+ pos++;
117
+ if (!functions[name]) throw badArg('unknown function in expression: ' + name);
118
+ return functions[name](...args);
119
+ }
120
+ if (name === 'PI') return Math.PI;
121
+ if (name === 'E') return Math.E;
122
+ if (!(name in vars)) throw badArg('unknown variable in expression: ' + name);
123
+ return vars[name];
124
+ };
125
+ const result = parseExpr();
126
+ if (peek() !== undefined) throw badArg('invalid expression: ' + src);
127
+ if (!Number.isFinite(result)) throw badArg('expression is not finite: ' + src);
128
+ return result;
129
+ }
130
+
131
+ function splitTop(text, sep) {
132
+ const parts = [];
133
+ let depth = 0;
134
+ let quote = false;
135
+ let cur = '';
136
+ for (let i = 0; i < text.length; i++) {
137
+ const c = text[i];
138
+ if (c === '\\' && i + 1 < text.length) {
139
+ cur += text[++i];
140
+ continue;
141
+ }
142
+ if (c === "'") {
143
+ quote = !quote;
144
+ continue;
145
+ }
146
+ if (!quote) {
147
+ if (c === '(') depth++;
148
+ else if (c === ')') depth--;
149
+ else if (c === sep && depth === 0) {
150
+ parts.push(cur);
151
+ cur = '';
152
+ continue;
153
+ }
154
+ }
155
+ cur += c;
156
+ }
157
+ parts.push(cur);
158
+ return parts;
159
+ }
160
+
161
+ const positional = {
162
+ scale: ['w', 'h', 'flags'],
163
+ crop: ['w', 'h', 'x', 'y', 'keep_aspect'],
164
+ pad: ['width', 'height', 'x', 'y', 'color'],
165
+ transpose: ['dir'],
166
+ format: ['pix_fmts'],
167
+ setsar: ['sar'],
168
+ setdar: ['dar'],
169
+ fps: ['fps'],
170
+ trim: ['start', 'end'],
171
+ };
172
+
173
+ const aliases = { w: 'w', h: 'h', out_w: 'w', out_h: 'h', width: 'width', height: 'height' };
174
+
175
+ function parseFilters(text) {
176
+ if (text === undefined || text === null || text === '') return [];
177
+ const out = [];
178
+ for (const raw of splitTop(String(text), ',')) {
179
+ const part = raw.trim();
180
+ if (!part) continue;
181
+ if (/[\[\];]/.test(part) || part.includes(' ') && /\[/.test(part)) throw noSupport('filter graphs are not supported');
182
+ const eq = part.indexOf('=');
183
+ const name = (eq < 0 ? part : part.slice(0, eq)).trim();
184
+ const args = {};
185
+ if (eq >= 0) {
186
+ const names = positional[name] || [];
187
+ let index = 0;
188
+ let named = false;
189
+ for (const item of splitTop(part.slice(eq + 1), ':')) {
190
+ const kv = /^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=(.*)$/s.exec(item);
191
+ if (kv) {
192
+ named = true;
193
+ args[aliases[kv[1]] || kv[1]] = kv[2].trim();
194
+ } else {
195
+ if (named) throw badArg('positional argument after named argument in filter ' + name);
196
+ const key = names[index++];
197
+ if (!key) throw badArg('too many arguments for filter ' + name);
198
+ args[key] = item.trim();
199
+ }
200
+ }
201
+ }
202
+ out.push({ name, args });
203
+ }
204
+ return out;
205
+ }
206
+
207
+ const bicubic = (b, c) => (x) => {
208
+ x = Math.abs(x);
209
+ if (x < 1) return ((12 - 9 * b - 6 * c) * x * x * x + (-18 + 12 * b + 6 * c) * x * x + (6 - 2 * b)) / 6;
210
+ if (x < 2) return ((-b - 6 * c) * x * x * x + (6 * b + 30 * c) * x * x + (-12 * b - 48 * c) * x + (8 * b + 24 * c)) / 6;
211
+ return 0;
212
+ };
213
+
214
+ const sinc = (x) => (x === 0 ? 1 : Math.sin(Math.PI * x) / (Math.PI * x));
215
+
216
+ const kernels = {
217
+ bicubic: { radius: 2, fn: bicubic(0, 0.6) },
218
+ bilinear: { radius: 1, fn: (x) => Math.max(0, 1 - Math.abs(x)) },
219
+ fast_bilinear: { radius: 1, fn: (x) => Math.max(0, 1 - Math.abs(x)) },
220
+ bicublin: { radius: 2, fn: bicubic(0, 0.6) },
221
+ neighbor: { radius: 0.5, fn: (x) => (x >= -0.5 && x < 0.5 ? 1 : 0), point: true },
222
+ area: { radius: 0.5, fn: (x) => (Math.abs(x) < 0.5 ? 1 : 0), box: true },
223
+ lanczos: { radius: 3, fn: (x) => (Math.abs(x) < 3 ? sinc(x) * sinc(x / 3) : 0) },
224
+ spline: { radius: 2, fn: bicubic(1, 0) },
225
+ gauss: { radius: 2, fn: (x) => Math.exp(-2 * x * x) },
226
+ };
227
+
228
+ function buildTaps(srcLen, dstLen, kernel) {
229
+ const ratio = srcLen / dstLen;
230
+ const sf = kernel.point ? 1 : Math.max(1, ratio);
231
+ const radius = kernel.radius * sf;
232
+ const maxTaps = Math.ceil(radius * 2) + 2;
233
+ const index = new Int32Array(dstLen * maxTaps);
234
+ const weight = new Float32Array(dstLen * maxTaps);
235
+ const count = new Int32Array(dstLen);
236
+ for (let d = 0; d < dstLen; d++) {
237
+ const center = (d + 0.5) * ratio - 0.5;
238
+ if (kernel.point) {
239
+ index[d * maxTaps] = Math.min(srcLen - 1, Math.max(0, Math.floor((d + 0.5) * ratio)));
240
+ weight[d * maxTaps] = 1;
241
+ count[d] = 1;
242
+ continue;
243
+ }
244
+ const first = Math.ceil(center - radius);
245
+ const last = Math.floor(center + radius);
246
+ let n = 0;
247
+ let sum = 0;
248
+ for (let s = first; s <= last && n < maxTaps; s++) {
249
+ const w = kernel.fn((s - center) / sf);
250
+ if (w === 0) continue;
251
+ index[d * maxTaps + n] = s < 0 ? 0 : s > srcLen - 1 ? srcLen - 1 : s;
252
+ weight[d * maxTaps + n] = w;
253
+ sum += w;
254
+ n++;
255
+ }
256
+ if (n === 0 || sum === 0) {
257
+ index[d * maxTaps] = Math.min(srcLen - 1, Math.max(0, Math.round(center)));
258
+ weight[d * maxTaps] = 1;
259
+ n = 1;
260
+ sum = 1;
261
+ }
262
+ for (let i = 0; i < n; i++) weight[d * maxTaps + i] /= sum;
263
+ count[d] = n;
264
+ }
265
+ return { index, weight, count, maxTaps };
266
+ }
267
+
268
+ function scalePlane(plane, dw, dh, channels, kernel) {
269
+ const sw = plane.width;
270
+ const sh = plane.height;
271
+ if (sw === dw && sh === dh) return plane;
272
+ const src = plane.data;
273
+ const hx = buildTaps(sw, dw, kernel);
274
+ const vy = buildTaps(sh, dh, kernel);
275
+ const tmp = new Float32Array(dw * sh * channels);
276
+ for (let y = 0; y < sh; y++) {
277
+ const row = y * sw * channels;
278
+ for (let x = 0; x < dw; x++) {
279
+ const n = hx.count[x];
280
+ const base = x * hx.maxTaps;
281
+ for (let c = 0; c < channels; c++) {
282
+ let acc = 0;
283
+ for (let t = 0; t < n; t++) acc += src[row + hx.index[base + t] * channels + c] * hx.weight[base + t];
284
+ tmp[(y * dw + x) * channels + c] = acc;
285
+ }
286
+ }
287
+ }
288
+ const out = new Uint8Array(dw * dh * channels);
289
+ const stride = dw * channels;
290
+ for (let y = 0; y < dh; y++) {
291
+ const n = vy.count[y];
292
+ const base = y * vy.maxTaps;
293
+ for (let i = 0; i < stride; i++) {
294
+ let acc = 0;
295
+ for (let t = 0; t < n; t++) acc += tmp[vy.index[base + t] * stride + i] * vy.weight[base + t];
296
+ const v = Math.round(acc);
297
+ out[y * stride + i] = v < 0 ? 0 : v > 255 ? 255 : v;
298
+ }
299
+ }
300
+ return { data: out, width: dw, height: dh };
301
+ }
302
+
303
+ function frameVars(frame) {
304
+ const hsub = frame.format === 'yuv' ? frame.hsub : 1;
305
+ const vsub = frame.format === 'yuv' ? frame.vsub : 1;
306
+ return { iw: frame.width, ih: frame.height, in_w: frame.width, in_h: frame.height, a: frame.width / frame.height, sar: 1, dar: frame.width / frame.height, hsub, vsub, n: 0, t: 0, pos: 0 };
307
+ }
308
+
309
+ function adjustDimensions(args, frame) {
310
+ const vars = frameVars(frame);
311
+ const evalDim = (key) => (args[key] === undefined || args[key] === '' ? 0 : evaluate(args[key], vars));
312
+ vars.ow = vars.out_w = 0;
313
+ vars.oh = vars.out_h = 0;
314
+ let w = evalDim('w');
315
+ vars.ow = vars.out_w = w;
316
+ let h = evalDim('h');
317
+ vars.oh = vars.out_h = h;
318
+ if (/\boh\b|\bout_h\b/.test(String(args.w || ''))) w = evalDim('w');
319
+ w = Math.trunc(w);
320
+ h = Math.trunc(h);
321
+ let factorW = 1;
322
+ let factorH = 1;
323
+ if (w < -1) factorW = -w;
324
+ if (h < -1) factorH = -h;
325
+ if (w < 0 && h < 0) w = h = 0;
326
+ if (!(w | h)) {
327
+ w = frame.width;
328
+ h = frame.height;
329
+ } else if (w === 0) {
330
+ w = frame.width;
331
+ } else if (h === 0) {
332
+ h = frame.height;
333
+ }
334
+ if (w < 0) w = Math.round(((h * frame.width) / frame.height) / factorW) * factorW || frame.width;
335
+ else if (h < 0) h = Math.round(((w * frame.height) / frame.width) / factorH) * factorH || frame.height;
336
+ const mode = args.force_original_aspect_ratio || 'disable';
337
+ if (mode === 'increase' || mode === 'decrease') {
338
+ const tmpW = (h * frame.width) / frame.height;
339
+ const tmpH = (w * frame.height) / frame.width;
340
+ if (mode === 'decrease') {
341
+ w = Math.min(tmpW, w);
342
+ h = Math.min(tmpH, h);
343
+ } else {
344
+ w = Math.max(tmpW, w);
345
+ h = Math.max(tmpH, h);
346
+ }
347
+ const div = args.force_divisible_by ? Math.max(1, Math.trunc(evaluate(args.force_divisible_by, vars))) : 1;
348
+ w = Math.round(w / div) * div || div;
349
+ h = Math.round(h / div) * div || div;
350
+ } else if (mode !== 'disable') throw badArg('invalid force_original_aspect_ratio: ' + mode);
351
+ return { w: Math.max(1, w), h: Math.max(1, h) };
352
+ }
353
+
354
+ function applyScale(frame, args) {
355
+ const { w, h } = adjustDimensions(args, frame);
356
+ const flags = String(args.flags || 'bicubic').split('+')[0].toLowerCase();
357
+ const kernel = kernels[flags];
358
+ if (!kernel) throw noSupport('unsupported scaling flag: ' + flags);
359
+ if (w === frame.width && h === frame.height) return frame;
360
+ if (frame.format === 'rgba') {
361
+ const p = scalePlane(frame.planes[0], w, h, 4, kernel);
362
+ return { format: 'rgba', width: w, height: h, hsub: 1, vsub: 1, planes: [p] };
363
+ }
364
+ if (frame.format === 'gray') {
365
+ const p = scalePlane(frame.planes[0], w, h, 1, kernel);
366
+ return { format: 'gray', width: w, height: h, hsub: 1, vsub: 1, planes: [p] };
367
+ }
368
+ const c = planeSize(w, h, frame.hsub, frame.vsub);
369
+ return {
370
+ format: 'yuv',
371
+ width: w,
372
+ height: h,
373
+ hsub: frame.hsub,
374
+ vsub: frame.vsub,
375
+ planes: [scalePlane(frame.planes[0], w, h, 1, kernel), scalePlane(frame.planes[1], c.width, c.height, 1, kernel), scalePlane(frame.planes[2], c.width, c.height, 1, kernel)],
376
+ };
377
+ }
378
+
379
+ function cropPlane(plane, x, y, w, h, channels) {
380
+ const out = new Uint8Array(w * h * channels);
381
+ for (let r = 0; r < h; r++) {
382
+ const s = ((y + r) * plane.width + x) * channels;
383
+ out.set(plane.data.subarray(s, s + w * channels), r * w * channels);
384
+ }
385
+ return { data: out, width: w, height: h };
386
+ }
387
+
388
+ function rint(v) {
389
+ const f = Math.floor(v);
390
+ const d = v - f;
391
+ if (d < 0.5) return f;
392
+ if (d > 0.5) return f + 1;
393
+ return f % 2 === 0 ? f : f + 1;
394
+ }
395
+
396
+ function applyCrop(frame, args) {
397
+ const vars = frameVars(frame);
398
+ const dim = (key, fallback) => (args[key] === undefined || args[key] === '' ? fallback : evaluate(args[key], vars));
399
+ vars.ow = vars.out_w = frame.width;
400
+ vars.oh = vars.out_h = frame.height;
401
+ let w = rint(dim('w', frame.width));
402
+ let h = rint(dim('h', frame.height));
403
+ vars.ow = vars.out_w = vars.w = w;
404
+ vars.oh = vars.out_h = vars.h = h;
405
+ w = rint(dim('w', frame.width));
406
+ h = rint(dim('h', frame.height));
407
+ if (w <= 0 || h <= 0 || w > frame.width || h > frame.height) throw badArg('invalid crop size ' + w + 'x' + h);
408
+ vars.ow = vars.out_w = vars.w = w;
409
+ vars.oh = vars.out_h = vars.h = h;
410
+ let x = rint(dim('x', (frame.width - w) / 2));
411
+ let y = rint(dim('y', (frame.height - h) / 2));
412
+ x = Math.min(Math.max(0, x), frame.width - w);
413
+ y = Math.min(Math.max(0, y), frame.height - h);
414
+ if (frame.format === 'yuv') {
415
+ x -= x % frame.hsub;
416
+ y -= y % frame.vsub;
417
+ }
418
+ if (x === 0 && y === 0 && w === frame.width && h === frame.height) return frame;
419
+ if (frame.format === 'rgba') return { format: 'rgba', width: w, height: h, hsub: 1, vsub: 1, planes: [cropPlane(frame.planes[0], x, y, w, h, 4)] };
420
+ if (frame.format === 'gray') return { format: 'gray', width: w, height: h, hsub: 1, vsub: 1, planes: [cropPlane(frame.planes[0], x, y, w, h, 1)] };
421
+ const c = planeSize(w, h, frame.hsub, frame.vsub);
422
+ return {
423
+ format: 'yuv',
424
+ width: w,
425
+ height: h,
426
+ hsub: frame.hsub,
427
+ vsub: frame.vsub,
428
+ planes: [
429
+ cropPlane(frame.planes[0], x, y, w, h, 1),
430
+ cropPlane(frame.planes[1], x / frame.hsub, y / frame.vsub, c.width, c.height, 1),
431
+ cropPlane(frame.planes[2], x / frame.hsub, y / frame.vsub, c.width, c.height, 1),
432
+ ],
433
+ };
434
+ }
435
+
436
+ const namedColors = { black: [0, 0, 0], white: [255, 255, 255], red: [255, 0, 0], green: [0, 128, 0], blue: [0, 0, 255], yellow: [255, 255, 0], gray: [128, 128, 128], grey: [128, 128, 128], transparent: [0, 0, 0, 0] };
437
+
438
+ function parseColor(text) {
439
+ const [base, alphaText] = String(text || 'black').split('@');
440
+ let rgb;
441
+ const lower = base.toLowerCase();
442
+ if (namedColors[lower]) rgb = namedColors[lower].slice();
443
+ else {
444
+ const hex = /^(?:#|0x)([0-9a-f]{6})([0-9a-f]{2})?$/i.exec(base);
445
+ if (!hex) throw badArg('invalid color: ' + text);
446
+ const n = parseInt(hex[1], 16);
447
+ rgb = [(n >> 16) & 255, (n >> 8) & 255, n & 255];
448
+ if (hex[2]) rgb[3] = parseInt(hex[2], 16);
449
+ }
450
+ if (rgb.length === 3) rgb.push(255);
451
+ if (alphaText !== undefined) rgb[3] = Math.round(Math.min(1, Math.max(0, Number(alphaText))) * 255);
452
+ return rgb;
453
+ }
454
+
455
+ function applyPad(frame, args) {
456
+ const src = toRgba(frame);
457
+ const vars = frameVars(src);
458
+ const dim = (key, fallback) => (args[key] === undefined || args[key] === '' ? fallback : evaluate(args[key], vars));
459
+ let w = Math.trunc(dim('width', src.width));
460
+ let h = Math.trunc(dim('height', src.height));
461
+ if (w <= 0) w = src.width;
462
+ if (h <= 0) h = src.height;
463
+ w = Math.max(w, src.width);
464
+ h = Math.max(h, src.height);
465
+ vars.ow = vars.out_w = w;
466
+ vars.oh = vars.out_h = h;
467
+ let x = Math.trunc(dim('x', 0));
468
+ let y = Math.trunc(dim('y', 0));
469
+ if (x < 0) x = Math.trunc((w - src.width) / 2);
470
+ if (y < 0) y = Math.trunc((h - src.height) / 2);
471
+ x = Math.min(x, w - src.width);
472
+ y = Math.min(y, h - src.height);
473
+ const color = parseColor(args.color);
474
+ const out = createRgba(w, h);
475
+ const d = out.planes[0].data;
476
+ for (let i = 0; i < d.length; i += 4) {
477
+ d[i] = color[0];
478
+ d[i + 1] = color[1];
479
+ d[i + 2] = color[2];
480
+ d[i + 3] = color[3];
481
+ }
482
+ const s = src.planes[0].data;
483
+ for (let r = 0; r < src.height; r++) d.set(s.subarray(r * src.width * 4, (r + 1) * src.width * 4), ((y + r) * w + x) * 4);
484
+ return out;
485
+ }
486
+
487
+ function mapPlanes(frame, fn) {
488
+ const ch = frame.format === 'rgba' ? 4 : 1;
489
+ return fn(frame, ch);
490
+ }
491
+
492
+ function flipFrame(frame, horizontal) {
493
+ const planes = frame.planes.map((p) => {
494
+ const ch = frame.format === 'rgba' ? 4 : 1;
495
+ const out = new Uint8Array(p.data.length);
496
+ for (let y = 0; y < p.height; y++) {
497
+ for (let x = 0; x < p.width; x++) {
498
+ const sx = horizontal ? p.width - 1 - x : x;
499
+ const sy = horizontal ? y : p.height - 1 - y;
500
+ for (let c = 0; c < ch; c++) out[(y * p.width + x) * ch + c] = p.data[(sy * p.width + sx) * ch + c];
501
+ }
502
+ }
503
+ return { data: out, width: p.width, height: p.height };
504
+ });
505
+ return Object.assign({}, frame, { planes });
506
+ }
507
+
508
+ function transposeFrame(frame, dir) {
509
+ const d = typeof dir === 'string' && /^\d+$/.test(dir) ? Number(dir) : { cclock_flip: 0, clock: 1, cclock: 2, clock_flip: 3 }[dir];
510
+ if (d === undefined) throw badArg('invalid transpose direction');
511
+ const ch = frame.format === 'rgba' ? 4 : 1;
512
+ const planes = frame.planes.map((p) => {
513
+ const ow = p.height;
514
+ const oh = p.width;
515
+ const out = new Uint8Array(p.data.length);
516
+ for (let y = 0; y < oh; y++) {
517
+ for (let x = 0; x < ow; x++) {
518
+ let sx;
519
+ let sy;
520
+ if (d === 0) {
521
+ sx = y;
522
+ sy = x;
523
+ sy = p.height - 1 - sy;
524
+ } else if (d === 1) {
525
+ sx = y;
526
+ sy = p.height - 1 - x;
527
+ } else if (d === 2) {
528
+ sx = p.width - 1 - y;
529
+ sy = x;
530
+ } else {
531
+ sx = p.width - 1 - y;
532
+ sy = p.height - 1 - x;
533
+ }
534
+ for (let c = 0; c < ch; c++) out[(y * ow + x) * ch + c] = p.data[(sy * p.width + sx) * ch + c];
535
+ }
536
+ }
537
+ return { data: out, width: ow, height: oh };
538
+ });
539
+ if (frame.format === 'yuv' && frame.hsub !== frame.vsub) throw noSupport('transpose of non-square chroma subsampling is not supported');
540
+ return Object.assign({}, frame, { width: frame.height, height: frame.width, planes });
541
+ }
542
+
543
+ const noopFilters = new Set(['setsar', 'setdar', 'fps', 'setpts', 'null', 'unsharp_none']);
544
+
545
+ function applyFilters(frame, filters, hints) {
546
+ let cur = frame;
547
+ for (const f of filters) {
548
+ if (f.name === 'scale') cur = applyScale(cur, f.args);
549
+ else if (f.name === 'crop') cur = applyCrop(cur, f.args);
550
+ else if (f.name === 'pad') cur = applyPad(cur, f.args);
551
+ else if (f.name === 'hflip') cur = flipFrame(cur, true);
552
+ else if (f.name === 'vflip') cur = flipFrame(cur, false);
553
+ else if (f.name === 'transpose') cur = transposeFrame(cur, f.args.dir === undefined ? '0' : f.args.dir);
554
+ else if (f.name === 'format') {
555
+ if (hints) hints.pixFmt = String(f.args.pix_fmts || '').split(/[|,]/)[0];
556
+ } else if (noopFilters.has(f.name)) continue;
557
+ else throw noSupport('unsupported filter: ' + f.name);
558
+ }
559
+ return cur;
560
+ }
561
+
562
+ function outputSize(filters, width, height) {
563
+ let w = width;
564
+ let h = height;
565
+ for (const f of filters) {
566
+ if (f.name === 'scale') {
567
+ const r = adjustDimensions(f.args, { width: w, height: h });
568
+ w = r.w;
569
+ h = r.h;
570
+ } else if (!(f.name === 'format' || noopFilters.has(f.name))) return null;
571
+ }
572
+ return { width: w, height: h };
573
+ }
574
+
575
+ export { evaluate, parseFilters, applyFilters, parseColor, kernels, outputSize };
@@ -0,0 +1,123 @@
1
+ import { badData, noSupport } from '../errors.js';
2
+ import { createRgba, toRgba, hasAlpha } from './frame.js';
3
+
4
+ function isBmp(buf) {
5
+ return buf.length >= 30 && buf[0] === 0x42 && buf[1] === 0x4d;
6
+ }
7
+
8
+ function probeBmp(buf) {
9
+ if (!isBmp(buf)) return null;
10
+ const size = buf.readUInt32LE(14);
11
+ if (size === 12) return { width: buf.readUInt16LE(18), height: buf.readUInt16LE(20) };
12
+ return { width: buf.readInt32LE(18), height: Math.abs(buf.readInt32LE(22)) };
13
+ }
14
+
15
+ function decodeBmp(buf) {
16
+ if (!isBmp(buf)) throw badData('not a bmp');
17
+ const dataOffset = buf.readUInt32LE(10);
18
+ const headerSize = buf.readUInt32LE(14);
19
+ if (headerSize < 40) throw noSupport('unsupported bmp header');
20
+ const width = buf.readInt32LE(18);
21
+ const rawHeight = buf.readInt32LE(22);
22
+ const height = Math.abs(rawHeight);
23
+ const topDown = rawHeight < 0;
24
+ const bpp = buf.readUInt16LE(28);
25
+ const compression = buf.readUInt32LE(30);
26
+ if (!(width > 0 && height > 0) || width * height > 268435456) throw badData('invalid bmp size');
27
+ if (compression !== 0 && compression !== 3) throw noSupport('compressed bmp is not supported');
28
+ let colors = buf.readUInt32LE(46);
29
+ const paletteOffset = 14 + headerSize;
30
+ let masks = null;
31
+ if (compression === 3 || bpp === 16 || bpp === 32) {
32
+ if (compression === 3) masks = [buf.readUInt32LE(54), buf.readUInt32LE(58), buf.readUInt32LE(62), headerSize >= 56 ? buf.readUInt32LE(66) : 0];
33
+ else if (bpp === 16) masks = [0x7c00, 0x03e0, 0x001f, 0];
34
+ }
35
+ if (bpp <= 8 && !colors) colors = 1 << bpp;
36
+ const out = createRgba(width, height);
37
+ const dst = out.planes[0].data;
38
+ const rowBytes = Math.floor((width * bpp + 31) / 32) * 4;
39
+ if (dataOffset + rowBytes * height > buf.length + rowBytes) throw badData('truncated bmp');
40
+ const ext = (v, mask) => {
41
+ if (!mask) return 255;
42
+ let shift = 0;
43
+ while (!((mask >>> shift) & 1)) shift++;
44
+ const bits = (mask >>> shift).toString(2).length;
45
+ const val = (v & mask) >>> shift;
46
+ return bits >= 8 ? val >> (bits - 8) : Math.round((val * 255) / ((1 << bits) - 1));
47
+ };
48
+ for (let y = 0; y < height; y++) {
49
+ const sy = topDown ? y : height - 1 - y;
50
+ const row = dataOffset + sy * rowBytes;
51
+ for (let x = 0; x < width; x++) {
52
+ const o = (y * width + x) * 4;
53
+ if (bpp === 24) {
54
+ dst[o] = buf[row + x * 3 + 2];
55
+ dst[o + 1] = buf[row + x * 3 + 1];
56
+ dst[o + 2] = buf[row + x * 3];
57
+ dst[o + 3] = 255;
58
+ } else if (bpp === 32) {
59
+ if (masks) {
60
+ const v = buf.readUInt32LE(row + x * 4);
61
+ dst[o] = ext(v, masks[0]);
62
+ dst[o + 1] = ext(v, masks[1]);
63
+ dst[o + 2] = ext(v, masks[2]);
64
+ dst[o + 3] = masks[3] ? ext(v, masks[3]) : 255;
65
+ } else {
66
+ dst[o] = buf[row + x * 4 + 2];
67
+ dst[o + 1] = buf[row + x * 4 + 1];
68
+ dst[o + 2] = buf[row + x * 4];
69
+ dst[o + 3] = 255;
70
+ }
71
+ } else if (bpp === 16) {
72
+ const v = buf.readUInt16LE(row + x * 2);
73
+ dst[o] = ext(v, masks[0]);
74
+ dst[o + 1] = ext(v, masks[1]);
75
+ dst[o + 2] = ext(v, masks[2]);
76
+ dst[o + 3] = 255;
77
+ } else if (bpp === 8 || bpp === 4 || bpp === 1) {
78
+ const bitPos = x * bpp;
79
+ const idx = (buf[row + (bitPos >> 3)] >> (8 - bpp - (bitPos & 7))) & ((1 << bpp) - 1);
80
+ const p = paletteOffset + idx * 4;
81
+ dst[o] = buf[p + 2];
82
+ dst[o + 1] = buf[p + 1];
83
+ dst[o + 2] = buf[p];
84
+ dst[o + 3] = 255;
85
+ } else throw noSupport('unsupported bmp depth');
86
+ }
87
+ }
88
+ return out;
89
+ }
90
+
91
+ function encodeBmp(frame) {
92
+ const rgba = toRgba(frame);
93
+ const alpha = hasAlpha(rgba);
94
+ const { width, height } = rgba;
95
+ const bpp = alpha ? 32 : 24;
96
+ const rowBytes = Math.floor((width * bpp + 31) / 32) * 4;
97
+ const header = 14 + 40;
98
+ const out = Buffer.alloc(header + rowBytes * height);
99
+ out.write('BM', 0, 'latin1');
100
+ out.writeUInt32LE(out.length, 2);
101
+ out.writeUInt32LE(header, 10);
102
+ out.writeUInt32LE(40, 14);
103
+ out.writeInt32LE(width, 18);
104
+ out.writeInt32LE(height, 22);
105
+ out.writeUInt16LE(1, 26);
106
+ out.writeUInt16LE(bpp, 28);
107
+ out.writeUInt32LE(rowBytes * height, 34);
108
+ const src = rgba.planes[0].data;
109
+ for (let y = 0; y < height; y++) {
110
+ const row = header + (height - 1 - y) * rowBytes;
111
+ for (let x = 0; x < width; x++) {
112
+ const i = (y * width + x) * 4;
113
+ const o = row + x * (bpp >> 3);
114
+ out[o] = src[i + 2];
115
+ out[o + 1] = src[i + 1];
116
+ out[o + 2] = src[i];
117
+ if (alpha) out[o + 3] = src[i + 3];
118
+ }
119
+ }
120
+ return out;
121
+ }
122
+
123
+ export { isBmp, probeBmp, decodeBmp, encodeBmp };