@hiroosy/baileys-lite 0.4.2 → 0.4.4

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 (59) hide show
  1. package/README.md +8 -17
  2. package/package.json +1 -1
  3. package/src/ffmpeg/LICENSE +502 -0
  4. package/src/ffmpeg/index.js +64 -0
  5. package/src/ffmpeg/lib/bits.js +150 -0
  6. package/src/ffmpeg/lib/bsf.js +242 -0
  7. package/src/ffmpeg/lib/codec/flac.js +801 -0
  8. package/src/ffmpeg/lib/codec/index.js +108 -0
  9. package/src/ffmpeg/lib/codec/mp3.js +768 -0
  10. package/src/ffmpeg/lib/codec/mpa.js +65 -0
  11. package/src/ffmpeg/lib/codec/mpa_tables.js +27 -0
  12. package/src/ffmpeg/lib/codec/opus.js +140 -0
  13. package/src/ffmpeg/lib/codec/pcm.js +198 -0
  14. package/src/ffmpeg/lib/compat.js +42 -0
  15. package/src/ffmpeg/lib/crc.js +60 -0
  16. package/src/ffmpeg/lib/demux/adts.js +151 -0
  17. package/src/ffmpeg/lib/demux/flac.js +150 -0
  18. package/src/ffmpeg/lib/demux/image.js +47 -0
  19. package/src/ffmpeg/lib/demux/index.js +60 -0
  20. package/src/ffmpeg/lib/demux/matroska.js +525 -0
  21. package/src/ffmpeg/lib/demux/merge.js +106 -0
  22. package/src/ffmpeg/lib/demux/mp3.js +199 -0
  23. package/src/ffmpeg/lib/demux/mp4.js +622 -0
  24. package/src/ffmpeg/lib/demux/ogg.js +250 -0
  25. package/src/ffmpeg/lib/demux/ts.js +354 -0
  26. package/src/ffmpeg/lib/demux/wav.js +156 -0
  27. package/src/ffmpeg/lib/errors.js +27 -0
  28. package/src/ffmpeg/lib/filter/gain.js +69 -0
  29. package/src/ffmpeg/lib/filter/mix.js +95 -0
  30. package/src/ffmpeg/lib/filter/resample.js +187 -0
  31. package/src/ffmpeg/lib/filter/video.js +575 -0
  32. package/src/ffmpeg/lib/image/bmp.js +123 -0
  33. package/src/ffmpeg/lib/image/frame.js +216 -0
  34. package/src/ffmpeg/lib/image/gif.js +215 -0
  35. package/src/ffmpeg/lib/image/index.js +70 -0
  36. package/src/ffmpeg/lib/image/jpeg.js +964 -0
  37. package/src/ffmpeg/lib/image/png.js +256 -0
  38. package/src/ffmpeg/lib/image/run.js +67 -0
  39. package/src/ffmpeg/lib/io.js +298 -0
  40. package/src/ffmpeg/lib/mux/adts.js +75 -0
  41. package/src/ffmpeg/lib/mux/flac.js +79 -0
  42. package/src/ffmpeg/lib/mux/mp3.js +85 -0
  43. package/src/ffmpeg/lib/mux/mp4.js +450 -0
  44. package/src/ffmpeg/lib/mux/ogg.js +146 -0
  45. package/src/ffmpeg/lib/mux/wav.js +94 -0
  46. package/src/ffmpeg/lib/net.js +270 -0
  47. package/src/ffmpeg/lib/pipeline.js +579 -0
  48. package/src/ffmpeg/lib/probe.js +45 -0
  49. package/src/ffmpeg/lib/tags.js +307 -0
  50. package/src/ffmpeg/lib/util.js +104 -0
  51. package/src/foundation/image-thumb.js +4 -56
  52. package/src/socket/messages-recv.js +5 -6
  53. package/src/socket/socket-core.js +4 -0
  54. package/src/utils/media.js +8 -85
  55. package/src/utils/message-compose.js +19 -11
  56. package/src/utils/wa-protocol-core.js +5 -3
  57. package/src/voip/index.js +16 -50
  58. package/src/voip/media/WaAudioEngine.js +8 -216
  59. package/src/voip/media/WaVideoEngine.js +8 -237
@@ -0,0 +1,150 @@
1
+ import { badData } from './errors.js';
2
+
3
+ class BitReader {
4
+ constructor(buf, start = 0, end = buf.length) {
5
+ this.buf = buf;
6
+ this.pos = start * 8;
7
+ this.limit = end * 8;
8
+ }
9
+
10
+ read(n) {
11
+ if (n === 0) return 0;
12
+ if (n > 24) {
13
+ const hi = this.read(n - 16);
14
+ return hi * 65536 + this.read(16);
15
+ }
16
+ const p = this.pos;
17
+ const next = p + n;
18
+ if (next > this.limit) throw badData('bitstream overrun');
19
+ const i = p >>> 3;
20
+ const b = this.buf;
21
+ const w = (b[i] << 24) | (b[i + 1] << 16) | (b[i + 2] << 8) | b[i + 3];
22
+ this.pos = next;
23
+ return (w << (p & 7)) >>> (32 - n);
24
+ }
25
+
26
+ peek(n) {
27
+ const p = this.pos;
28
+ const i = p >>> 3;
29
+ const b = this.buf;
30
+ const w = (b[i] << 24) | (b[i + 1] << 16) | (b[i + 2] << 8) | b[i + 3];
31
+ return (w << (p & 7)) >>> (32 - n);
32
+ }
33
+
34
+ signed(n) {
35
+ if (n === 0) return 0;
36
+ if (n > 24) {
37
+ const v = this.read(n);
38
+ return v >= Math.pow(2, n - 1) ? v - Math.pow(2, n) : v;
39
+ }
40
+ return (this.read(n) << (32 - n)) >> (32 - n);
41
+ }
42
+
43
+ unary() {
44
+ let n = 0;
45
+ const b = this.buf;
46
+ for (;;) {
47
+ const i = this.pos >>> 3;
48
+ if (i >= b.length || this.pos >= this.limit) throw badData('bitstream overrun');
49
+ const off = this.pos & 7;
50
+ const v = (b[i] << off) & 0xff;
51
+ if (v) {
52
+ const z = Math.clz32(v) - 24;
53
+ this.pos += z + 1;
54
+ return n + z;
55
+ }
56
+ n += 8 - off;
57
+ this.pos = (i + 1) << 3;
58
+ }
59
+ }
60
+
61
+ align() {
62
+ this.pos = (this.pos + 7) & ~7;
63
+ }
64
+
65
+ get bytePos() {
66
+ return this.pos >>> 3;
67
+ }
68
+ }
69
+
70
+ const pow2 = [];
71
+ for (let i = 0; i <= 40; i++) pow2.push(Math.pow(2, i));
72
+
73
+ class BitWriter {
74
+ constructor(size = 65536) {
75
+ this.buf = Buffer.allocUnsafe(size);
76
+ this.pos = 0;
77
+ this.acc = 0;
78
+ this.bits = 0;
79
+ }
80
+
81
+ reset() {
82
+ this.pos = 0;
83
+ this.acc = 0;
84
+ this.bits = 0;
85
+ }
86
+
87
+ ensure(extra) {
88
+ if (this.pos + extra > this.buf.length) {
89
+ let cap = this.buf.length * 2;
90
+ while (cap < this.pos + extra) cap *= 2;
91
+ const next = Buffer.allocUnsafe(cap);
92
+ this.buf.copy(next, 0, 0, this.pos);
93
+ this.buf = next;
94
+ }
95
+ }
96
+
97
+ write(value, n) {
98
+ if (n === 0) return;
99
+ if (value < 0) value += pow2[n];
100
+ if (n > 16) {
101
+ this.write(Math.floor(value / 65536), n - 16);
102
+ this.write(value % 65536, 16);
103
+ return;
104
+ }
105
+ this.ensure(4);
106
+ this.acc = (this.acc << n) | (value & ((1 << n) - 1));
107
+ this.bits += n;
108
+ while (this.bits >= 8) {
109
+ this.bits -= 8;
110
+ this.buf[this.pos++] = (this.acc >>> this.bits) & 0xff;
111
+ }
112
+ this.acc &= (1 << this.bits) - 1;
113
+ }
114
+
115
+ unary(q) {
116
+ while (q >= 16) {
117
+ this.write(0, 16);
118
+ q -= 16;
119
+ }
120
+ this.write(1, q + 1);
121
+ }
122
+
123
+ rice(value, k) {
124
+ const u = value >= 0 ? value * 2 : -value * 2 - 1;
125
+ const q = u >>> k;
126
+ const low = u & ((1 << k) - 1);
127
+ if (q + k + 1 <= 31) this.write((1 << k) | low, q + k + 1);
128
+ else {
129
+ this.unary(q);
130
+ if (k > 0) this.write(low, k);
131
+ }
132
+ }
133
+
134
+ align() {
135
+ if (this.bits > 0) this.write(0, 8 - this.bits);
136
+ }
137
+
138
+ get byteLength() {
139
+ return this.pos;
140
+ }
141
+
142
+ bytes() {
143
+ return this.buf.subarray(0, this.pos);
144
+ }
145
+ }
146
+
147
+ export {
148
+ BitReader,
149
+ BitWriter,
150
+ };
@@ -0,0 +1,242 @@
1
+ import { badData } from './errors.js';
2
+
3
+ function box(type, ...parts) {
4
+ let len = 8;
5
+ for (const p of parts) len += p.length;
6
+ const out = Buffer.allocUnsafe(len);
7
+ out.writeUInt32BE(len, 0);
8
+ out.write(type, 4, 'latin1');
9
+ let o = 8;
10
+ for (const p of parts) {
11
+ Buffer.from(p.buffer, p.byteOffset, p.length).copy(out, o);
12
+ o += p.length;
13
+ }
14
+ return out;
15
+ }
16
+
17
+ function findStartCodes(buf) {
18
+ const starts = [];
19
+ const n = buf.length;
20
+ let i = 0;
21
+ while (i + 3 <= n) {
22
+ const j = buf.indexOf(0x01, i + 2);
23
+ if (j < 0) break;
24
+ if (buf[j - 1] === 0 && buf[j - 2] === 0) starts.push({ code: j - 2 > 0 && buf[j - 3] === 0 ? j - 3 : j - 2, data: j + 1 });
25
+ i = j + 1;
26
+ }
27
+ return starts;
28
+ }
29
+
30
+ function splitAnnexB(buf) {
31
+ const marks = findStartCodes(buf);
32
+ const nals = [];
33
+ for (let k = 0; k < marks.length; k++) {
34
+ const end = k + 1 < marks.length ? marks[k + 1].code : buf.length;
35
+ let e = end;
36
+ while (e > marks[k].data && buf[e - 1] === 0 && k + 1 >= marks.length) e--;
37
+ if (e > marks[k].data) nals.push(buf.subarray(marks[k].data, e));
38
+ }
39
+ return nals;
40
+ }
41
+
42
+ function unescapeRbsp(nal) {
43
+ const out = Buffer.allocUnsafe(nal.length);
44
+ let o = 0;
45
+ let zeros = 0;
46
+ for (let i = 0; i < nal.length; i++) {
47
+ const b = nal[i];
48
+ if (zeros >= 2 && b === 3) {
49
+ zeros = 0;
50
+ continue;
51
+ }
52
+ out[o++] = b;
53
+ zeros = b === 0 ? zeros + 1 : 0;
54
+ }
55
+ return out.subarray(0, o);
56
+ }
57
+
58
+ class RbspReader {
59
+ constructor(buf) {
60
+ this.buf = buf;
61
+ this.pos = 0;
62
+ }
63
+
64
+ bit() {
65
+ const i = this.pos >> 3;
66
+ if (i >= this.buf.length) throw badData('h264: sps overrun');
67
+ const v = (this.buf[i] >> (7 - (this.pos & 7))) & 1;
68
+ this.pos++;
69
+ return v;
70
+ }
71
+
72
+ bits(n) {
73
+ let v = 0;
74
+ for (let i = 0; i < n; i++) v = v * 2 + this.bit();
75
+ return v;
76
+ }
77
+
78
+ ue() {
79
+ let zeros = 0;
80
+ while (this.bit() === 0) {
81
+ zeros++;
82
+ if (zeros > 32) throw badData('h264: invalid exp-golomb');
83
+ }
84
+ return zeros === 0 ? 0 : Math.pow(2, zeros) - 1 + this.bits(zeros);
85
+ }
86
+
87
+ se() {
88
+ const k = this.ue();
89
+ return k & 1 ? (k + 1) / 2 : -k / 2;
90
+ }
91
+ }
92
+
93
+ const highProfiles = new Set([100, 110, 122, 244, 44, 83, 86, 118, 128, 138, 139, 134, 135]);
94
+
95
+ function skipScalingList(r, size) {
96
+ let last = 8;
97
+ let next = 8;
98
+ for (let j = 0; j < size; j++) {
99
+ if (next !== 0) next = (last + r.se() + 256) % 256;
100
+ last = next === 0 ? last : next;
101
+ }
102
+ }
103
+
104
+ function parseSps(nal) {
105
+ const r = new RbspReader(unescapeRbsp(nal.subarray(1)));
106
+ const profile = r.bits(8);
107
+ const compat = r.bits(8);
108
+ const level = r.bits(8);
109
+ r.ue();
110
+ let chroma = 1;
111
+ let depthLuma = 8;
112
+ let depthChroma = 8;
113
+ if (highProfiles.has(profile)) {
114
+ chroma = r.ue();
115
+ if (chroma === 3) r.bit();
116
+ depthLuma = 8 + r.ue();
117
+ depthChroma = 8 + r.ue();
118
+ r.bit();
119
+ if (r.bit()) {
120
+ const count = chroma !== 3 ? 8 : 12;
121
+ for (let i = 0; i < count; i++) if (r.bit()) skipScalingList(r, i < 6 ? 16 : 64);
122
+ }
123
+ }
124
+ r.ue();
125
+ const pocType = r.ue();
126
+ if (pocType === 0) r.ue();
127
+ else if (pocType === 1) {
128
+ r.bit();
129
+ r.se();
130
+ r.se();
131
+ const n = r.ue();
132
+ for (let i = 0; i < n; i++) r.se();
133
+ }
134
+ const refFrames = r.ue();
135
+ r.bit();
136
+ const wMbs = r.ue() + 1;
137
+ const hMapUnits = r.ue() + 1;
138
+ const frameMbsOnly = r.bit();
139
+ if (!frameMbsOnly) r.bit();
140
+ r.bit();
141
+ let cropL = 0;
142
+ let cropR = 0;
143
+ let cropT = 0;
144
+ let cropB = 0;
145
+ if (r.bit()) {
146
+ cropL = r.ue();
147
+ cropR = r.ue();
148
+ cropT = r.ue();
149
+ cropB = r.ue();
150
+ }
151
+ const cropX = chroma === 0 || chroma === 3 ? 1 : 2;
152
+ const cropY = (2 - frameMbsOnly) * (chroma === 1 ? 2 : 1);
153
+ const width = wMbs * 16 - cropX * (cropL + cropR);
154
+ const height = (2 - frameMbsOnly) * hMapUnits * 16 - cropY * (cropT + cropB);
155
+ return { profile, compat, level, chroma, bitDepth: Math.max(depthLuma, depthChroma), width, height, refFrames, frameMbsOnly };
156
+ }
157
+
158
+ function buildAvcC(sps, pps, info) {
159
+ const parts = [Buffer.from([1, sps[0][1], sps[0][2], sps[0][3], 0xff, 0xe0 | sps.length])];
160
+ for (const s of sps) parts.push(Buffer.from([s.length >> 8, s.length & 255]), s);
161
+ parts.push(Buffer.from([pps.length]));
162
+ for (const p of pps) parts.push(Buffer.from([p.length >> 8, p.length & 255]), p);
163
+ if (highProfiles.has(info.profile)) parts.push(Buffer.from([0xfc | info.chroma, 0xf8 | (info.bitDepth - 8), 0xf8 | (info.bitDepth - 8), 0]));
164
+ return Buffer.concat(parts);
165
+ }
166
+
167
+ function buildAvc1Entry(width, height, avcC) {
168
+ const head = Buffer.alloc(78);
169
+ head.writeUInt16BE(1, 6);
170
+ head.writeUInt16BE(width, 24);
171
+ head.writeUInt16BE(height, 26);
172
+ head.writeUInt32BE(0x00480000, 28);
173
+ head.writeUInt32BE(0x00480000, 32);
174
+ head.writeUInt16BE(1, 40);
175
+ head.writeUInt16BE(0x0018, 74);
176
+ head.writeInt16BE(-1, 76);
177
+ return box('avc1', head, box('avcC', avcC));
178
+ }
179
+
180
+ function annexBToAvcc(nals) {
181
+ let total = 0;
182
+ for (const n of nals) total += n.length + 4;
183
+ const out = Buffer.allocUnsafe(total);
184
+ let o = 0;
185
+ for (const n of nals) {
186
+ out.writeUInt32BE(n.length, o);
187
+ n.copy(out, o + 4);
188
+ o += n.length + 4;
189
+ }
190
+ return out;
191
+ }
192
+
193
+ function parseAvcc(avcC) {
194
+ if (avcC.length < 7 || avcC[0] !== 1) throw badData('h264: invalid avcC');
195
+ const lengthSize = (avcC[4] & 3) + 1;
196
+ const sps = [];
197
+ const pps = [];
198
+ let p = 6;
199
+ for (let i = 0; i < (avcC[5] & 31); i++) {
200
+ const n = avcC.readUInt16BE(p);
201
+ sps.push(avcC.subarray(p + 2, p + 2 + n));
202
+ p += 2 + n;
203
+ }
204
+ const np = avcC[p++];
205
+ for (let i = 0; i < np; i++) {
206
+ const n = avcC.readUInt16BE(p);
207
+ pps.push(avcC.subarray(p + 2, p + 2 + n));
208
+ p += 2 + n;
209
+ }
210
+ return { lengthSize, sps, pps };
211
+ }
212
+
213
+ function avccToAnnexB(data, lengthSize) {
214
+ const parts = [];
215
+ let p = 0;
216
+ while (p + lengthSize <= data.length) {
217
+ let n = 0;
218
+ for (let i = 0; i < lengthSize; i++) n = n * 256 + data[p + i];
219
+ p += lengthSize;
220
+ if (n <= 0 || p + n > data.length) break;
221
+ parts.push(Buffer.from([0, 0, 0, 1]), data.subarray(p, p + n));
222
+ p += n;
223
+ }
224
+ return Buffer.concat(parts);
225
+ }
226
+
227
+ const aacRates = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
228
+
229
+ function buildAsc(profile, sfi, chan) {
230
+ const aot = profile + 1;
231
+ return Buffer.from([(aot << 3) | (sfi >> 1), ((sfi & 1) << 7) | (chan << 3)]);
232
+ }
233
+
234
+ function parseAsc(asc) {
235
+ if (!asc || asc.length < 2) throw badData('aac: invalid AudioSpecificConfig');
236
+ const aot = asc[0] >> 3;
237
+ const sfi = ((asc[0] & 7) << 1) | (asc[1] >> 7);
238
+ const chan = (asc[1] >> 3) & 15;
239
+ return { aot, sfi, sampleRate: aacRates[sfi], channels: [0, 1, 2, 3, 4, 5, 6, 8][chan] || 0, chan };
240
+ }
241
+
242
+ export { box, splitAnnexB, unescapeRbsp, parseSps, buildAvcC, buildAvc1Entry, annexBToAvcc, parseAvcc, avccToAnnexB, buildAsc, parseAsc, aacRates };