@hiroosy/baileys-lite 0.4.3 → 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.
- package/README.md +8 -17
- package/package.json +1 -1
- package/src/ffmpeg/LICENSE +502 -0
- package/src/ffmpeg/index.js +64 -0
- package/src/ffmpeg/lib/bits.js +150 -0
- package/src/ffmpeg/lib/bsf.js +242 -0
- package/src/ffmpeg/lib/codec/flac.js +801 -0
- package/src/ffmpeg/lib/codec/index.js +108 -0
- package/src/ffmpeg/lib/codec/mp3.js +768 -0
- package/src/ffmpeg/lib/codec/mpa.js +65 -0
- package/src/ffmpeg/lib/codec/mpa_tables.js +27 -0
- package/src/ffmpeg/lib/codec/opus.js +140 -0
- package/src/ffmpeg/lib/codec/pcm.js +198 -0
- package/src/ffmpeg/lib/compat.js +42 -0
- package/src/ffmpeg/lib/crc.js +60 -0
- package/src/ffmpeg/lib/demux/adts.js +151 -0
- package/src/ffmpeg/lib/demux/flac.js +150 -0
- package/src/ffmpeg/lib/demux/image.js +47 -0
- package/src/ffmpeg/lib/demux/index.js +60 -0
- package/src/ffmpeg/lib/demux/matroska.js +525 -0
- package/src/ffmpeg/lib/demux/merge.js +106 -0
- package/src/ffmpeg/lib/demux/mp3.js +199 -0
- package/src/ffmpeg/lib/demux/mp4.js +622 -0
- package/src/ffmpeg/lib/demux/ogg.js +250 -0
- package/src/ffmpeg/lib/demux/ts.js +354 -0
- package/src/ffmpeg/lib/demux/wav.js +156 -0
- package/src/ffmpeg/lib/errors.js +27 -0
- package/src/ffmpeg/lib/filter/gain.js +69 -0
- package/src/ffmpeg/lib/filter/mix.js +95 -0
- package/src/ffmpeg/lib/filter/resample.js +187 -0
- package/src/ffmpeg/lib/filter/video.js +575 -0
- package/src/ffmpeg/lib/image/bmp.js +123 -0
- package/src/ffmpeg/lib/image/frame.js +216 -0
- package/src/ffmpeg/lib/image/gif.js +215 -0
- package/src/ffmpeg/lib/image/index.js +70 -0
- package/src/ffmpeg/lib/image/jpeg.js +964 -0
- package/src/ffmpeg/lib/image/png.js +256 -0
- package/src/ffmpeg/lib/image/run.js +67 -0
- package/src/ffmpeg/lib/io.js +298 -0
- package/src/ffmpeg/lib/mux/adts.js +75 -0
- package/src/ffmpeg/lib/mux/flac.js +79 -0
- package/src/ffmpeg/lib/mux/mp3.js +85 -0
- package/src/ffmpeg/lib/mux/mp4.js +450 -0
- package/src/ffmpeg/lib/mux/ogg.js +146 -0
- package/src/ffmpeg/lib/mux/wav.js +94 -0
- package/src/ffmpeg/lib/net.js +270 -0
- package/src/ffmpeg/lib/pipeline.js +579 -0
- package/src/ffmpeg/lib/probe.js +45 -0
- package/src/ffmpeg/lib/tags.js +307 -0
- package/src/ffmpeg/lib/util.js +104 -0
- package/src/foundation/image-thumb.js +4 -56
- package/src/socket/messages-recv.js +5 -6
- package/src/utils/media.js +8 -85
- package/src/utils/message-compose.js +19 -11
- package/src/voip/index.js +16 -50
- package/src/voip/media/WaAudioEngine.js +8 -216
- package/src/voip/media/WaVideoEngine.js +8 -237
|
@@ -0,0 +1,964 @@
|
|
|
1
|
+
import { badData, badArg, noSupport } from '../errors.js';
|
|
2
|
+
import { createGray, createYuv, createRgba, clamp8, toYuv, toGray } from './frame.js';
|
|
3
|
+
|
|
4
|
+
const huffSpec = {
|
|
5
|
+
dcLuma: { bits: [0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], vals: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] },
|
|
6
|
+
dcChroma: { bits: [0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0], vals: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] },
|
|
7
|
+
acLuma: { bits: [0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125], vals: [1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, 129, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240, 36, 51, 98, 114, 130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250] },
|
|
8
|
+
acChroma: { bits: [0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119], vals: [0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18, 65, 81, 7, 97, 113, 19, 34, 50, 129, 8, 20, 66, 145, 161, 177, 193, 9, 35, 51, 82, 240, 21, 98, 114, 209, 10, 22, 36, 52, 225, 37, 241, 23, 24, 25, 26, 38, 39, 40, 41, 42, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 130, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 226, 227, 228, 229, 230, 231, 232, 233, 234, 242, 243, 244, 245, 246, 247, 248, 249, 250] },
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const zigzag = Uint8Array.from([
|
|
12
|
+
0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
|
|
13
|
+
35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
const aan = [1.0, 1.387039845, 1.306562965, 1.175875602, 1.0, 0.785694958, 0.5411961, 0.275899379];
|
|
17
|
+
|
|
18
|
+
const defaultIntraMatrix = [
|
|
19
|
+
8, 16, 19, 22, 26, 27, 29, 34,
|
|
20
|
+
16, 16, 22, 24, 27, 29, 34, 37,
|
|
21
|
+
19, 22, 26, 27, 29, 34, 34, 38,
|
|
22
|
+
22, 22, 26, 27, 29, 34, 37, 40,
|
|
23
|
+
22, 26, 27, 29, 32, 35, 40, 48,
|
|
24
|
+
26, 27, 29, 32, 35, 40, 48, 58,
|
|
25
|
+
26, 27, 29, 34, 38, 46, 56, 69,
|
|
26
|
+
27, 29, 35, 38, 46, 56, 69, 83,
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
const stdLumaQuant = [
|
|
30
|
+
16, 11, 10, 16, 24, 40, 51, 61,
|
|
31
|
+
12, 12, 14, 19, 26, 58, 60, 55,
|
|
32
|
+
14, 13, 16, 24, 40, 57, 69, 56,
|
|
33
|
+
14, 17, 22, 29, 51, 87, 80, 62,
|
|
34
|
+
18, 22, 37, 56, 68, 109, 103, 77,
|
|
35
|
+
24, 35, 55, 64, 81, 104, 113, 92,
|
|
36
|
+
49, 64, 78, 87, 103, 121, 120, 101,
|
|
37
|
+
72, 92, 95, 98, 112, 100, 103, 99,
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const stdChromaQuant = [
|
|
41
|
+
17, 18, 24, 47, 99, 99, 99, 99,
|
|
42
|
+
18, 21, 26, 66, 99, 99, 99, 99,
|
|
43
|
+
24, 26, 56, 99, 99, 99, 99, 99,
|
|
44
|
+
47, 66, 99, 99, 99, 99, 99, 99,
|
|
45
|
+
99, 99, 99, 99, 99, 99, 99, 99,
|
|
46
|
+
99, 99, 99, 99, 99, 99, 99, 99,
|
|
47
|
+
99, 99, 99, 99, 99, 99, 99, 99,
|
|
48
|
+
99, 99, 99, 99, 99, 99, 99, 99,
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
function buildDecodeTable(counts, symbols) {
|
|
52
|
+
const lookup = new Int16Array(512).fill(-1);
|
|
53
|
+
const maxcode = new Int32Array(18).fill(-1);
|
|
54
|
+
const valptr = new Int32Array(18);
|
|
55
|
+
const mincode = new Int32Array(18);
|
|
56
|
+
let code = 0;
|
|
57
|
+
let k = 0;
|
|
58
|
+
for (let len = 1; len <= 16; len++) {
|
|
59
|
+
valptr[len] = k;
|
|
60
|
+
mincode[len] = code;
|
|
61
|
+
const n = counts[len - 1];
|
|
62
|
+
for (let i = 0; i < n; i++, k++, code++) {
|
|
63
|
+
if (len <= 9) {
|
|
64
|
+
const base = code << (9 - len);
|
|
65
|
+
const span = 1 << (9 - len);
|
|
66
|
+
for (let j = 0; j < span; j++) lookup[base + j] = (len << 8) | symbols[k];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
maxcode[len] = n ? code - 1 : -1;
|
|
70
|
+
code <<= 1;
|
|
71
|
+
}
|
|
72
|
+
return { lookup, maxcode, valptr, mincode, symbols };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
class BitReader {
|
|
76
|
+
constructor(data, pos) {
|
|
77
|
+
this.data = data;
|
|
78
|
+
this.pos = pos;
|
|
79
|
+
this.bits = 0;
|
|
80
|
+
this.cnt = 0;
|
|
81
|
+
this.marker = 0;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
fill() {
|
|
85
|
+
const data = this.data;
|
|
86
|
+
while (this.cnt <= 24) {
|
|
87
|
+
let b = 0;
|
|
88
|
+
if (!this.marker && this.pos < data.length) {
|
|
89
|
+
b = data[this.pos++];
|
|
90
|
+
if (b === 0xff) {
|
|
91
|
+
let n = data[this.pos];
|
|
92
|
+
while (n === 0xff) {
|
|
93
|
+
this.pos++;
|
|
94
|
+
n = data[this.pos];
|
|
95
|
+
}
|
|
96
|
+
if (n === 0) this.pos++;
|
|
97
|
+
else {
|
|
98
|
+
this.marker = n === undefined ? 0xd9 : n;
|
|
99
|
+
this.pos--;
|
|
100
|
+
b = 0;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
this.bits = (this.bits | (b << (24 - this.cnt))) >>> 0;
|
|
105
|
+
this.cnt += 8;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
receive(n) {
|
|
110
|
+
if (n === 0) return 0;
|
|
111
|
+
if (this.cnt < n) this.fill();
|
|
112
|
+
const v = this.bits >>> (32 - n);
|
|
113
|
+
this.bits = (this.bits << n) >>> 0;
|
|
114
|
+
this.cnt -= n;
|
|
115
|
+
return v;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
receiveExtend(n) {
|
|
119
|
+
const v = this.receive(n);
|
|
120
|
+
return v < 1 << (n - 1) ? v - (1 << n) + 1 : v;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
decode(table) {
|
|
124
|
+
if (this.cnt < 16) this.fill();
|
|
125
|
+
const look = table.lookup[this.bits >>> 23];
|
|
126
|
+
if (look >= 0) {
|
|
127
|
+
const len = look >> 8;
|
|
128
|
+
this.bits = (this.bits << len) >>> 0;
|
|
129
|
+
this.cnt -= len;
|
|
130
|
+
return look & 255;
|
|
131
|
+
}
|
|
132
|
+
for (let len = 10; len <= 16; len++) {
|
|
133
|
+
const code = this.bits >>> (32 - len);
|
|
134
|
+
if (table.maxcode[len] >= 0 && code <= table.maxcode[len] && code >= table.mincode[len]) {
|
|
135
|
+
this.bits = (this.bits << len) >>> 0;
|
|
136
|
+
this.cnt -= len;
|
|
137
|
+
return table.symbols[table.valptr[len] + code - table.mincode[len]];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
throw badData('invalid huffman code');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
restart() {
|
|
144
|
+
this.bits = 0;
|
|
145
|
+
this.cnt = 0;
|
|
146
|
+
if (!this.marker) {
|
|
147
|
+
const d = this.data;
|
|
148
|
+
let p = this.pos;
|
|
149
|
+
while (p + 1 < d.length && !(d[p] === 0xff && d[p + 1] !== 0 && d[p + 1] !== 0xff)) p++;
|
|
150
|
+
this.pos = p;
|
|
151
|
+
this.marker = p + 1 < d.length ? d[p + 1] : 0xd9;
|
|
152
|
+
}
|
|
153
|
+
if (this.marker >= 0xd0 && this.marker <= 0xd7) {
|
|
154
|
+
this.pos += 2;
|
|
155
|
+
this.marker = 0;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function idctBlock(coef, off, qt, out, outOff, stride, ws) {
|
|
161
|
+
for (let c = 0; c < 8; c++) {
|
|
162
|
+
const i = off + c;
|
|
163
|
+
const t0 = coef[i] * qt[c];
|
|
164
|
+
const x1 = coef[i + 8];
|
|
165
|
+
const x2 = coef[i + 16];
|
|
166
|
+
const x3 = coef[i + 24];
|
|
167
|
+
const x4 = coef[i + 32];
|
|
168
|
+
const x5 = coef[i + 40];
|
|
169
|
+
const x6 = coef[i + 48];
|
|
170
|
+
const x7 = coef[i + 56];
|
|
171
|
+
if ((x1 | x2 | x3 | x4 | x5 | x6 | x7) === 0) {
|
|
172
|
+
ws[c] = ws[c + 8] = ws[c + 16] = ws[c + 24] = ws[c + 32] = ws[c + 40] = ws[c + 48] = ws[c + 56] = t0;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const t2 = x2 * qt[c + 16];
|
|
176
|
+
const t4 = x4 * qt[c + 32];
|
|
177
|
+
const t6 = x6 * qt[c + 48];
|
|
178
|
+
const t10 = t0 + t4;
|
|
179
|
+
const t11 = t0 - t4;
|
|
180
|
+
const t13 = t2 + t6;
|
|
181
|
+
const t12 = (t2 - t6) * 1.414213562 - t13;
|
|
182
|
+
const e0 = t10 + t13;
|
|
183
|
+
const e3 = t10 - t13;
|
|
184
|
+
const e1 = t11 + t12;
|
|
185
|
+
const e2 = t11 - t12;
|
|
186
|
+
const o4 = coef[i + 8] * qt[c + 8];
|
|
187
|
+
const o5 = coef[i + 24] * qt[c + 24];
|
|
188
|
+
const o6 = coef[i + 40] * qt[c + 40];
|
|
189
|
+
const o7 = coef[i + 56] * qt[c + 56];
|
|
190
|
+
const z13 = o6 + o5;
|
|
191
|
+
const z10 = o6 - o5;
|
|
192
|
+
const z11 = o4 + o7;
|
|
193
|
+
const z12 = o4 - o7;
|
|
194
|
+
const u7 = z11 + z13;
|
|
195
|
+
const u11 = (z11 - z13) * 1.414213562;
|
|
196
|
+
const z5 = (z10 + z12) * 1.847759065;
|
|
197
|
+
const u10 = 1.0823922 * z12 - z5;
|
|
198
|
+
const u12 = -2.61312593 * z10 + z5;
|
|
199
|
+
const u6 = u12 - u7;
|
|
200
|
+
const u5 = u11 - u6;
|
|
201
|
+
const u4 = u10 + u5;
|
|
202
|
+
ws[c] = e0 + u7;
|
|
203
|
+
ws[c + 56] = e0 - u7;
|
|
204
|
+
ws[c + 8] = e1 + u6;
|
|
205
|
+
ws[c + 48] = e1 - u6;
|
|
206
|
+
ws[c + 16] = e2 + u5;
|
|
207
|
+
ws[c + 40] = e2 - u5;
|
|
208
|
+
ws[c + 32] = e3 + u4;
|
|
209
|
+
ws[c + 24] = e3 - u4;
|
|
210
|
+
}
|
|
211
|
+
for (let r = 0; r < 8; r++) {
|
|
212
|
+
const w = r * 8;
|
|
213
|
+
const t10 = ws[w] + ws[w + 4];
|
|
214
|
+
const t11 = ws[w] - ws[w + 4];
|
|
215
|
+
const t13 = ws[w + 2] + ws[w + 6];
|
|
216
|
+
const t12 = (ws[w + 2] - ws[w + 6]) * 1.414213562 - t13;
|
|
217
|
+
const e0 = t10 + t13;
|
|
218
|
+
const e3 = t10 - t13;
|
|
219
|
+
const e1 = t11 + t12;
|
|
220
|
+
const e2 = t11 - t12;
|
|
221
|
+
const z13 = ws[w + 5] + ws[w + 3];
|
|
222
|
+
const z10 = ws[w + 5] - ws[w + 3];
|
|
223
|
+
const z11 = ws[w + 1] + ws[w + 7];
|
|
224
|
+
const z12 = ws[w + 1] - ws[w + 7];
|
|
225
|
+
const u7 = z11 + z13;
|
|
226
|
+
const u11 = (z11 - z13) * 1.414213562;
|
|
227
|
+
const z5 = (z10 + z12) * 1.847759065;
|
|
228
|
+
const u10 = 1.0823922 * z12 - z5;
|
|
229
|
+
const u12 = -2.61312593 * z10 + z5;
|
|
230
|
+
const u6 = u12 - u7;
|
|
231
|
+
const u5 = u11 - u6;
|
|
232
|
+
const u4 = u10 + u5;
|
|
233
|
+
const o = outOff + r * stride;
|
|
234
|
+
out[o] = clamp8(Math.round(e0 + u7 + 128));
|
|
235
|
+
out[o + 7] = clamp8(Math.round(e0 - u7 + 128));
|
|
236
|
+
out[o + 1] = clamp8(Math.round(e1 + u6 + 128));
|
|
237
|
+
out[o + 6] = clamp8(Math.round(e1 - u6 + 128));
|
|
238
|
+
out[o + 2] = clamp8(Math.round(e2 + u5 + 128));
|
|
239
|
+
out[o + 5] = clamp8(Math.round(e2 - u5 + 128));
|
|
240
|
+
out[o + 4] = clamp8(Math.round(e3 + u4 + 128));
|
|
241
|
+
out[o + 3] = clamp8(Math.round(e3 - u4 + 128));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function parseJpeg(buf) {
|
|
246
|
+
if (buf.length < 4 || buf[0] !== 0xff || buf[1] !== 0xd8) throw badData('not a jpeg');
|
|
247
|
+
const state = { quant: [], dc: [], ac: [], frame: null, restart: 0, adobe: -1, scans: 0, progressive: false };
|
|
248
|
+
let pos = 2;
|
|
249
|
+
let reader = null;
|
|
250
|
+
while (pos < buf.length) {
|
|
251
|
+
if (buf[pos] !== 0xff) {
|
|
252
|
+
pos++;
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
while (buf[pos] === 0xff) pos++;
|
|
256
|
+
const marker = buf[pos++];
|
|
257
|
+
if (marker === undefined) break;
|
|
258
|
+
if (marker === 0 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd8)) continue;
|
|
259
|
+
if (marker === 0xd9) break;
|
|
260
|
+
const len = buf.readUInt16BE(pos);
|
|
261
|
+
const body = pos + 2;
|
|
262
|
+
const end = pos + len;
|
|
263
|
+
if (end > buf.length) throw badData('truncated jpeg segment');
|
|
264
|
+
if (marker === 0xdb) {
|
|
265
|
+
let p = body;
|
|
266
|
+
while (p < end) {
|
|
267
|
+
const pq = buf[p] >> 4;
|
|
268
|
+
const tq = buf[p] & 15;
|
|
269
|
+
p++;
|
|
270
|
+
const t = new Uint16Array(64);
|
|
271
|
+
for (let i = 0; i < 64; i++) {
|
|
272
|
+
t[zigzag[i]] = pq ? buf.readUInt16BE(p) : buf[p];
|
|
273
|
+
p += pq ? 2 : 1;
|
|
274
|
+
}
|
|
275
|
+
state.quant[tq] = t;
|
|
276
|
+
}
|
|
277
|
+
} else if (marker === 0xc4) {
|
|
278
|
+
let p = body;
|
|
279
|
+
while (p < end) {
|
|
280
|
+
const tc = buf[p] >> 4;
|
|
281
|
+
const th = buf[p] & 15;
|
|
282
|
+
p++;
|
|
283
|
+
const counts = buf.subarray(p, p + 16);
|
|
284
|
+
let total = 0;
|
|
285
|
+
for (let i = 0; i < 16; i++) total += counts[i];
|
|
286
|
+
p += 16;
|
|
287
|
+
const table = buildDecodeTable(counts, buf.subarray(p, p + total));
|
|
288
|
+
p += total;
|
|
289
|
+
if (tc === 0) state.dc[th] = table;
|
|
290
|
+
else state.ac[th] = table;
|
|
291
|
+
}
|
|
292
|
+
} else if (marker === 0xc0 || marker === 0xc1 || marker === 0xc2) {
|
|
293
|
+
if (state.frame) throw badData('multiple frames');
|
|
294
|
+
const precision = buf[body];
|
|
295
|
+
if (precision !== 8) throw noSupport('jpeg precision ' + precision + ' is not supported');
|
|
296
|
+
const height = buf.readUInt16BE(body + 1);
|
|
297
|
+
const width = buf.readUInt16BE(body + 3);
|
|
298
|
+
const n = buf[body + 5];
|
|
299
|
+
if (!width || !height) throw badData('invalid jpeg size');
|
|
300
|
+
const comps = [];
|
|
301
|
+
for (let i = 0; i < n; i++) {
|
|
302
|
+
const p = body + 6 + i * 3;
|
|
303
|
+
comps.push({ id: buf[p], h: buf[p + 1] >> 4, v: buf[p + 1] & 15, tq: buf[p + 2] });
|
|
304
|
+
if (!comps[i].h || !comps[i].v) throw badData('invalid sampling factors');
|
|
305
|
+
}
|
|
306
|
+
state.frame = { width, height, comps };
|
|
307
|
+
state.progressive = marker === 0xc2;
|
|
308
|
+
} else if (marker === 0xc3 || (marker >= 0xc5 && marker <= 0xcf && marker !== 0xc8 && marker !== 0xcc)) {
|
|
309
|
+
throw noSupport('unsupported jpeg coding process');
|
|
310
|
+
} else if (marker === 0xdd) {
|
|
311
|
+
state.restart = buf.readUInt16BE(body);
|
|
312
|
+
} else if (marker === 0xee && len >= 14 && buf.toString('latin1', body, body + 5) === 'Adobe') {
|
|
313
|
+
state.adobe = buf[body + 11];
|
|
314
|
+
} else if (marker === 0xda) {
|
|
315
|
+
if (!state.frame) throw badData('scan before frame');
|
|
316
|
+
const frame = state.frame;
|
|
317
|
+
if (!frame.ready) prepareFrame(frame, state.progressive);
|
|
318
|
+
const ns = buf[body];
|
|
319
|
+
const scanComps = [];
|
|
320
|
+
for (let i = 0; i < ns; i++) {
|
|
321
|
+
const cid = buf[body + 1 + i * 2];
|
|
322
|
+
const tabs = buf[body + 2 + i * 2];
|
|
323
|
+
const comp = frame.comps.find((c) => c.id === cid);
|
|
324
|
+
if (!comp) throw badData('invalid scan component');
|
|
325
|
+
comp.dcTable = state.dc[tabs >> 4];
|
|
326
|
+
comp.acTable = state.ac[tabs & 15];
|
|
327
|
+
scanComps.push(comp);
|
|
328
|
+
}
|
|
329
|
+
const ss = buf[body + 1 + ns * 2];
|
|
330
|
+
const se = buf[body + 2 + ns * 2];
|
|
331
|
+
const ah = buf[body + 3 + ns * 2] >> 4;
|
|
332
|
+
const al = buf[body + 3 + ns * 2] & 15;
|
|
333
|
+
reader = new BitReader(buf, end);
|
|
334
|
+
decodeScan(frame, scanComps, state, ss, se, ah, al, reader);
|
|
335
|
+
state.scans++;
|
|
336
|
+
pos = reader.pos;
|
|
337
|
+
if (reader.marker) pos = reader.pos;
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
pos = end;
|
|
341
|
+
}
|
|
342
|
+
if (!state.frame || !state.scans) throw badData('no image data');
|
|
343
|
+
return state;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function prepareFrame(frame, progressive) {
|
|
347
|
+
let hmax = 1;
|
|
348
|
+
let vmax = 1;
|
|
349
|
+
for (const c of frame.comps) {
|
|
350
|
+
hmax = Math.max(hmax, c.h);
|
|
351
|
+
vmax = Math.max(vmax, c.v);
|
|
352
|
+
}
|
|
353
|
+
frame.hmax = hmax;
|
|
354
|
+
frame.vmax = vmax;
|
|
355
|
+
frame.mcusPerLine = Math.ceil(frame.width / (8 * hmax));
|
|
356
|
+
frame.mcusPerColumn = Math.ceil(frame.height / (8 * vmax));
|
|
357
|
+
for (const c of frame.comps) {
|
|
358
|
+
c.width = Math.ceil((frame.width * c.h) / hmax);
|
|
359
|
+
c.height = Math.ceil((frame.height * c.v) / vmax);
|
|
360
|
+
c.blocksPerLine = Math.ceil(c.width / 8);
|
|
361
|
+
c.blocksPerColumn = Math.ceil(c.height / 8);
|
|
362
|
+
c.blocksPerLineForMcu = frame.mcusPerLine * c.h;
|
|
363
|
+
c.blocksPerColumnForMcu = frame.mcusPerColumn * c.v;
|
|
364
|
+
const size = c.blocksPerLineForMcu * c.blocksPerColumnForMcu * 64;
|
|
365
|
+
if (size > 400 * 1024 * 1024) throw noSupport('jpeg is too large');
|
|
366
|
+
c.coef = new Int16Array(size);
|
|
367
|
+
c.pred = 0;
|
|
368
|
+
}
|
|
369
|
+
frame.progressive = progressive;
|
|
370
|
+
frame.ready = true;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function decodeScan(frame, comps, state, ss, se, ah, al, reader) {
|
|
374
|
+
const progressive = state.progressive;
|
|
375
|
+
let eobrun = 0;
|
|
376
|
+
const ri = state.restart;
|
|
377
|
+
const decodeBlock = (comp, row, col) => {
|
|
378
|
+
const off = (row * comp.blocksPerLineForMcu + col) * 64;
|
|
379
|
+
const coef = comp.coef;
|
|
380
|
+
if (!progressive) {
|
|
381
|
+
const t = reader.decode(comp.dcTable);
|
|
382
|
+
comp.pred += t === 0 ? 0 : reader.receiveExtend(t);
|
|
383
|
+
coef[off] = comp.pred;
|
|
384
|
+
let k = 1;
|
|
385
|
+
while (k < 64) {
|
|
386
|
+
const rs = reader.decode(comp.acTable);
|
|
387
|
+
const s = rs & 15;
|
|
388
|
+
const r = rs >> 4;
|
|
389
|
+
if (s === 0) {
|
|
390
|
+
if (r < 15) break;
|
|
391
|
+
k += 16;
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
k += r;
|
|
395
|
+
if (k > 63) break;
|
|
396
|
+
coef[off + zigzag[k]] = reader.receiveExtend(s);
|
|
397
|
+
k++;
|
|
398
|
+
}
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
if (ss === 0) {
|
|
402
|
+
if (ah === 0) {
|
|
403
|
+
const t = reader.decode(comp.dcTable);
|
|
404
|
+
comp.pred += t === 0 ? 0 : reader.receiveExtend(t);
|
|
405
|
+
coef[off] = comp.pred * (1 << al);
|
|
406
|
+
} else if (reader.receive(1)) coef[off] |= 1 << al;
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (ah === 0) {
|
|
410
|
+
if (eobrun > 0) {
|
|
411
|
+
eobrun--;
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
let k = ss;
|
|
415
|
+
while (k <= se) {
|
|
416
|
+
const rs = reader.decode(comp.acTable);
|
|
417
|
+
const s = rs & 15;
|
|
418
|
+
const r = rs >> 4;
|
|
419
|
+
if (s === 0) {
|
|
420
|
+
if (r < 15) {
|
|
421
|
+
eobrun = reader.receive(r) + (1 << r) - 1;
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
k += 16;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
k += r;
|
|
428
|
+
if (k > 63) break;
|
|
429
|
+
coef[off + zigzag[k]] = reader.receiveExtend(s) * (1 << al);
|
|
430
|
+
k++;
|
|
431
|
+
}
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
const p1 = 1 << al;
|
|
435
|
+
const m1 = -1 << al;
|
|
436
|
+
let k = ss;
|
|
437
|
+
if (eobrun === 0) {
|
|
438
|
+
for (; k <= se; k++) {
|
|
439
|
+
const rs = reader.decode(comp.acTable);
|
|
440
|
+
let s = rs & 15;
|
|
441
|
+
let r = rs >> 4;
|
|
442
|
+
if (s) s = reader.receive(1) ? p1 : m1;
|
|
443
|
+
else if (r !== 15) {
|
|
444
|
+
eobrun = 1 << r;
|
|
445
|
+
if (r) eobrun += reader.receive(r);
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
while (k <= se) {
|
|
449
|
+
const idx = off + zigzag[k];
|
|
450
|
+
const cur = coef[idx];
|
|
451
|
+
if (cur !== 0) {
|
|
452
|
+
if (reader.receive(1) && (cur & p1) === 0) coef[idx] = cur >= 0 ? cur + p1 : cur + m1;
|
|
453
|
+
} else if (--r < 0) break;
|
|
454
|
+
k++;
|
|
455
|
+
}
|
|
456
|
+
if (s && k <= 63) coef[off + zigzag[k]] = s;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (eobrun > 0) {
|
|
460
|
+
for (; k <= se; k++) {
|
|
461
|
+
const idx = off + zigzag[k];
|
|
462
|
+
const cur = coef[idx];
|
|
463
|
+
if (cur !== 0 && reader.receive(1) && (cur & p1) === 0) coef[idx] = cur >= 0 ? cur + p1 : cur + m1;
|
|
464
|
+
}
|
|
465
|
+
eobrun--;
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
for (const c of comps) c.pred = 0;
|
|
470
|
+
let total;
|
|
471
|
+
if (comps.length === 1) total = comps[0].blocksPerLine * comps[0].blocksPerColumn;
|
|
472
|
+
else total = frame.mcusPerLine * frame.mcusPerColumn;
|
|
473
|
+
for (let mcu = 0; mcu < total; mcu++) {
|
|
474
|
+
if (ri && mcu > 0 && mcu % ri === 0) {
|
|
475
|
+
reader.restart();
|
|
476
|
+
eobrun = 0;
|
|
477
|
+
for (const c of comps) c.pred = 0;
|
|
478
|
+
}
|
|
479
|
+
if (reader.marker && reader.marker !== 0 && reader.cnt <= 0 && !(reader.marker >= 0xd0 && reader.marker <= 0xd7)) break;
|
|
480
|
+
if (comps.length === 1) {
|
|
481
|
+
const c = comps[0];
|
|
482
|
+
decodeBlock(c, Math.floor(mcu / c.blocksPerLine), mcu % c.blocksPerLine);
|
|
483
|
+
} else {
|
|
484
|
+
const mr = Math.floor(mcu / frame.mcusPerLine);
|
|
485
|
+
const mc = mcu % frame.mcusPerLine;
|
|
486
|
+
for (const c of comps) {
|
|
487
|
+
for (let v = 0; v < c.v; v++) for (let h = 0; h < c.h; h++) decodeBlock(c, mr * c.v + v, mc * c.h + h);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function renderComponents(state) {
|
|
494
|
+
const frame = state.frame;
|
|
495
|
+
const ws = new Float32Array(64);
|
|
496
|
+
for (const c of frame.comps) {
|
|
497
|
+
const q = state.quant[c.tq];
|
|
498
|
+
if (!q) throw badData('missing quantization table');
|
|
499
|
+
const qt = new Float32Array(64);
|
|
500
|
+
for (let i = 0; i < 64; i++) qt[i] = (q[i] * aan[i >> 3] * aan[i & 7]) / 8;
|
|
501
|
+
const stride = c.blocksPerLineForMcu * 8;
|
|
502
|
+
const full = new Uint8Array(stride * c.blocksPerColumnForMcu * 8);
|
|
503
|
+
for (let by = 0; by < c.blocksPerColumn; by++) {
|
|
504
|
+
for (let bx = 0; bx < c.blocksPerLine; bx++) {
|
|
505
|
+
idctBlock(c.coef, (by * c.blocksPerLineForMcu + bx) * 64, qt, full, by * 8 * stride + bx * 8, stride, ws);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
if (stride === c.width) {
|
|
509
|
+
c.plane = full.subarray(0, c.width * c.height);
|
|
510
|
+
} else {
|
|
511
|
+
const plane = new Uint8Array(c.width * c.height);
|
|
512
|
+
for (let y = 0; y < c.height; y++) plane.set(full.subarray(y * stride, y * stride + c.width), y * c.width);
|
|
513
|
+
c.plane = plane;
|
|
514
|
+
}
|
|
515
|
+
c.coef = null;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function decodeJpeg(buf) {
|
|
520
|
+
const state = parseJpeg(buf);
|
|
521
|
+
renderComponents(state);
|
|
522
|
+
const f = state.frame;
|
|
523
|
+
const { width, height, comps } = f;
|
|
524
|
+
if (comps.length === 1) {
|
|
525
|
+
const out = createGray(width, height);
|
|
526
|
+
out.planes[0].data.set(comps[0].plane);
|
|
527
|
+
return out;
|
|
528
|
+
}
|
|
529
|
+
if (comps.length !== 3) throw noSupport('jpeg with ' + comps.length + ' components is not supported');
|
|
530
|
+
const rgbStored = state.adobe === 0 || (comps[0].id === 0x52 && comps[1].id === 0x47 && comps[2].id === 0x42);
|
|
531
|
+
if (rgbStored) {
|
|
532
|
+
if (comps.some((c) => c.width !== width || c.height !== height)) throw noSupport('subsampled rgb jpeg is not supported');
|
|
533
|
+
const out = createRgba(width, height);
|
|
534
|
+
const d = out.planes[0].data;
|
|
535
|
+
for (let i = 0, j = 0; i < width * height; i++, j += 4) {
|
|
536
|
+
d[j] = comps[0].plane[i];
|
|
537
|
+
d[j + 1] = comps[1].plane[i];
|
|
538
|
+
d[j + 2] = comps[2].plane[i];
|
|
539
|
+
d[j + 3] = 255;
|
|
540
|
+
}
|
|
541
|
+
return out;
|
|
542
|
+
}
|
|
543
|
+
const hsub = f.hmax / comps[1].h;
|
|
544
|
+
const vsub = f.vmax / comps[1].v;
|
|
545
|
+
if (comps[0].h !== f.hmax || comps[0].v !== f.vmax || comps[2].h !== comps[1].h || comps[2].v !== comps[1].v || !Number.isInteger(hsub) || !Number.isInteger(vsub)) {
|
|
546
|
+
throw noSupport('unsupported jpeg sampling layout');
|
|
547
|
+
}
|
|
548
|
+
const out = createYuv(width, height, hsub, vsub);
|
|
549
|
+
out.planes[0].data.set(comps[0].plane);
|
|
550
|
+
out.planes[1].data.set(comps[1].plane);
|
|
551
|
+
out.planes[2].data.set(comps[2].plane);
|
|
552
|
+
return out;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function probeJpeg(buf) {
|
|
556
|
+
if (buf.length < 4 || buf[0] !== 0xff || buf[1] !== 0xd8) return null;
|
|
557
|
+
let pos = 2;
|
|
558
|
+
while (pos + 4 <= buf.length) {
|
|
559
|
+
if (buf[pos] !== 0xff) {
|
|
560
|
+
pos++;
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
while (buf[pos] === 0xff) pos++;
|
|
564
|
+
const marker = buf[pos++];
|
|
565
|
+
if (marker === 0 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd8)) continue;
|
|
566
|
+
if (marker === 0xd9 || marker === 0xda) return null;
|
|
567
|
+
const len = buf.readUInt16BE(pos);
|
|
568
|
+
if ((marker >= 0xc0 && marker <= 0xcf) && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
|
|
569
|
+
return { width: buf.readUInt16BE(pos + 5), height: buf.readUInt16BE(pos + 3), components: buf[pos + 7] };
|
|
570
|
+
}
|
|
571
|
+
pos += len;
|
|
572
|
+
}
|
|
573
|
+
return null;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
class BitWriter {
|
|
577
|
+
constructor(capacity) {
|
|
578
|
+
this.buf = Buffer.allocUnsafe(capacity);
|
|
579
|
+
this.pos = 0;
|
|
580
|
+
this.acc = 0;
|
|
581
|
+
this.n = 0;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
ensure(extra) {
|
|
585
|
+
if (this.pos + extra > this.buf.length) {
|
|
586
|
+
const next = Buffer.allocUnsafe(Math.max(this.buf.length * 2, this.pos + extra));
|
|
587
|
+
this.buf.copy(next, 0, 0, this.pos);
|
|
588
|
+
this.buf = next;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
bytes(arr) {
|
|
593
|
+
this.ensure(arr.length);
|
|
594
|
+
for (let i = 0; i < arr.length; i++) this.buf[this.pos++] = arr[i];
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
put(code, size) {
|
|
598
|
+
if (size === 0) return;
|
|
599
|
+
this.acc = (this.acc << size) | (code & ((1 << size) - 1));
|
|
600
|
+
this.n += size;
|
|
601
|
+
this.ensure(8);
|
|
602
|
+
while (this.n >= 8) {
|
|
603
|
+
const b = (this.acc >>> (this.n - 8)) & 255;
|
|
604
|
+
this.buf[this.pos++] = b;
|
|
605
|
+
if (b === 0xff) this.buf[this.pos++] = 0;
|
|
606
|
+
this.n -= 8;
|
|
607
|
+
}
|
|
608
|
+
this.acc &= (1 << this.n) - 1;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
flush() {
|
|
612
|
+
if (this.n > 0) this.put(0x7f, 8 - this.n);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
result() {
|
|
616
|
+
return this.buf.subarray(0, this.pos);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
function buildEncodeTable(spec) {
|
|
621
|
+
const code = new Uint16Array(256);
|
|
622
|
+
const size = new Uint8Array(256);
|
|
623
|
+
let c = 0;
|
|
624
|
+
let k = 0;
|
|
625
|
+
for (let len = 1; len <= 16; len++) {
|
|
626
|
+
for (let i = 0; i < spec.bits[len - 1]; i++, k++, c++) {
|
|
627
|
+
code[spec.vals[k]] = c;
|
|
628
|
+
size[spec.vals[k]] = len;
|
|
629
|
+
}
|
|
630
|
+
c <<= 1;
|
|
631
|
+
}
|
|
632
|
+
return { code, size };
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
function fdctBlock(d) {
|
|
636
|
+
for (let r = 0; r < 8; r++) {
|
|
637
|
+
const o = r * 8;
|
|
638
|
+
const t0 = d[o] + d[o + 7];
|
|
639
|
+
const t7 = d[o] - d[o + 7];
|
|
640
|
+
const t1 = d[o + 1] + d[o + 6];
|
|
641
|
+
const t6 = d[o + 1] - d[o + 6];
|
|
642
|
+
const t2 = d[o + 2] + d[o + 5];
|
|
643
|
+
const t5 = d[o + 2] - d[o + 5];
|
|
644
|
+
const t3 = d[o + 3] + d[o + 4];
|
|
645
|
+
const t4 = d[o + 3] - d[o + 4];
|
|
646
|
+
const t10 = t0 + t3;
|
|
647
|
+
const t13 = t0 - t3;
|
|
648
|
+
const t11 = t1 + t2;
|
|
649
|
+
const t12 = t1 - t2;
|
|
650
|
+
d[o] = t10 + t11;
|
|
651
|
+
d[o + 4] = t10 - t11;
|
|
652
|
+
const z1 = (t12 + t13) * 0.707106781;
|
|
653
|
+
d[o + 2] = t13 + z1;
|
|
654
|
+
d[o + 6] = t13 - z1;
|
|
655
|
+
const u10 = t4 + t5;
|
|
656
|
+
const u11 = t5 + t6;
|
|
657
|
+
const u12 = t6 + t7;
|
|
658
|
+
const z5 = (u10 - u12) * 0.382683433;
|
|
659
|
+
const z2 = 0.5411961 * u10 + z5;
|
|
660
|
+
const z4 = 1.306562965 * u12 + z5;
|
|
661
|
+
const z3 = u11 * 0.707106781;
|
|
662
|
+
const z11 = t7 + z3;
|
|
663
|
+
const z13 = t7 - z3;
|
|
664
|
+
d[o + 5] = z13 + z2;
|
|
665
|
+
d[o + 3] = z13 - z2;
|
|
666
|
+
d[o + 1] = z11 + z4;
|
|
667
|
+
d[o + 7] = z11 - z4;
|
|
668
|
+
}
|
|
669
|
+
for (let c = 0; c < 8; c++) {
|
|
670
|
+
const t0 = d[c] + d[c + 56];
|
|
671
|
+
const t7 = d[c] - d[c + 56];
|
|
672
|
+
const t1 = d[c + 8] + d[c + 48];
|
|
673
|
+
const t6 = d[c + 8] - d[c + 48];
|
|
674
|
+
const t2 = d[c + 16] + d[c + 40];
|
|
675
|
+
const t5 = d[c + 16] - d[c + 40];
|
|
676
|
+
const t3 = d[c + 24] + d[c + 32];
|
|
677
|
+
const t4 = d[c + 24] - d[c + 32];
|
|
678
|
+
const t10 = t0 + t3;
|
|
679
|
+
const t13 = t0 - t3;
|
|
680
|
+
const t11 = t1 + t2;
|
|
681
|
+
const t12 = t1 - t2;
|
|
682
|
+
d[c] = t10 + t11;
|
|
683
|
+
d[c + 32] = t10 - t11;
|
|
684
|
+
const z1 = (t12 + t13) * 0.707106781;
|
|
685
|
+
d[c + 16] = t13 + z1;
|
|
686
|
+
d[c + 48] = t13 - z1;
|
|
687
|
+
const u10 = t4 + t5;
|
|
688
|
+
const u11 = t5 + t6;
|
|
689
|
+
const u12 = t6 + t7;
|
|
690
|
+
const z5 = (u10 - u12) * 0.382683433;
|
|
691
|
+
const z2 = 0.5411961 * u10 + z5;
|
|
692
|
+
const z4 = 1.306562965 * u12 + z5;
|
|
693
|
+
const z3 = u11 * 0.707106781;
|
|
694
|
+
const z11 = t7 + z3;
|
|
695
|
+
const z13 = t7 - z3;
|
|
696
|
+
d[c + 40] = z13 + z2;
|
|
697
|
+
d[c + 24] = z13 - z2;
|
|
698
|
+
d[c + 8] = z11 + z4;
|
|
699
|
+
d[c + 56] = z11 - z4;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function qualityTables(quality) {
|
|
704
|
+
const q = Math.min(100, Math.max(1, Math.round(quality)));
|
|
705
|
+
const scale = q < 50 ? 5000 / q : 200 - q * 2;
|
|
706
|
+
const mk = (std) => std.map((v) => Math.min(255, Math.max(1, Math.floor((v * scale + 50) / 100))));
|
|
707
|
+
return { luma: mk(stdLumaQuant), chroma: mk(stdChromaQuant) };
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
function qscaleTables(qscale) {
|
|
711
|
+
const q = Math.min(31, Math.max(1, qscale));
|
|
712
|
+
const t = defaultIntraMatrix.map((v, i) => (i === 0 ? 8 : Math.min(255, (v * q) >> 3)));
|
|
713
|
+
return { luma: t, chroma: t };
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
function optimalSpec(freqIn) {
|
|
717
|
+
const freq = new Float64Array(257);
|
|
718
|
+
for (let i = 0; i < 256; i++) freq[i] = freqIn[i];
|
|
719
|
+
freq[256] = 1;
|
|
720
|
+
const codesize = new Int32Array(257);
|
|
721
|
+
const others = new Int32Array(257).fill(-1);
|
|
722
|
+
for (;;) {
|
|
723
|
+
let c1 = -1;
|
|
724
|
+
let v = Infinity;
|
|
725
|
+
for (let i = 0; i <= 256; i++) {
|
|
726
|
+
if (freq[i] && freq[i] <= v) {
|
|
727
|
+
v = freq[i];
|
|
728
|
+
c1 = i;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
let c2 = -1;
|
|
732
|
+
v = Infinity;
|
|
733
|
+
for (let i = 0; i <= 256; i++) {
|
|
734
|
+
if (freq[i] && freq[i] <= v && i !== c1) {
|
|
735
|
+
v = freq[i];
|
|
736
|
+
c2 = i;
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
if (c2 < 0) break;
|
|
740
|
+
freq[c1] += freq[c2];
|
|
741
|
+
freq[c2] = 0;
|
|
742
|
+
codesize[c1]++;
|
|
743
|
+
while (others[c1] >= 0) {
|
|
744
|
+
c1 = others[c1];
|
|
745
|
+
codesize[c1]++;
|
|
746
|
+
}
|
|
747
|
+
others[c1] = c2;
|
|
748
|
+
codesize[c2]++;
|
|
749
|
+
while (others[c2] >= 0) {
|
|
750
|
+
c2 = others[c2];
|
|
751
|
+
codesize[c2]++;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
const bits = new Int32Array(40);
|
|
755
|
+
for (let i = 0; i <= 256; i++) if (codesize[i]) bits[codesize[i]]++;
|
|
756
|
+
for (let i = 39; i > 16; i--) {
|
|
757
|
+
while (bits[i] > 0) {
|
|
758
|
+
let j = i - 2;
|
|
759
|
+
while (bits[j] === 0) j--;
|
|
760
|
+
bits[i] -= 2;
|
|
761
|
+
bits[i - 1]++;
|
|
762
|
+
bits[j + 1] += 2;
|
|
763
|
+
bits[j]--;
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
let i = 16;
|
|
767
|
+
while (bits[i] === 0) i--;
|
|
768
|
+
bits[i]--;
|
|
769
|
+
const vals = [];
|
|
770
|
+
for (let len = 1; len <= 32; len++) {
|
|
771
|
+
for (let sym = 0; sym < 256; sym++) if (codesize[sym] === len) vals.push(sym);
|
|
772
|
+
}
|
|
773
|
+
return { bits: Array.from(bits.subarray(1, 17)), vals };
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
function bitLength(v) {
|
|
777
|
+
let n = 0;
|
|
778
|
+
while (v) {
|
|
779
|
+
n++;
|
|
780
|
+
v >>= 1;
|
|
781
|
+
}
|
|
782
|
+
return n;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
function encodeJpeg(frame, options = {}) {
|
|
786
|
+
let source = frame;
|
|
787
|
+
if (options.hsub && options.vsub) source = toYuv(frame, options.hsub, options.vsub);
|
|
788
|
+
else if (frame.format === 'rgba') source = toYuv(frame, 1, 1);
|
|
789
|
+
else if (frame.format === 'yuv' && !((frame.hsub === 1 || frame.hsub === 2) && (frame.vsub === 1 || frame.vsub === 2))) source = toYuv(frame, 2, 2);
|
|
790
|
+
if (options.gray && source.format !== 'gray') source = toGray(source);
|
|
791
|
+
const isGray = source.format === 'gray';
|
|
792
|
+
if (!isGray && source.format !== 'yuv') throw badArg('unsupported frame for jpeg');
|
|
793
|
+
const useQuality = options.quality !== undefined;
|
|
794
|
+
const tables = useQuality ? qualityTables(options.quality) : qscaleTables(options.qscale === undefined ? 4 : Number(options.qscale));
|
|
795
|
+
const bias = useQuality ? 0.5 : 0.375;
|
|
796
|
+
const same = tables.luma.every((v, i) => v === tables.chroma[i]);
|
|
797
|
+
const optimal = options.optimize !== false;
|
|
798
|
+
const { width, height } = source;
|
|
799
|
+
const hs = isGray ? 1 : source.hsub;
|
|
800
|
+
const vs = isGray ? 1 : source.vsub;
|
|
801
|
+
const nc = isGray ? 1 : 3;
|
|
802
|
+
const mcusX = Math.ceil(width / (8 * hs));
|
|
803
|
+
const mcusY = Math.ceil(height / (8 * vs));
|
|
804
|
+
const blocksPerMcu = isGray ? 1 : hs * vs + 2;
|
|
805
|
+
const total = mcusX * mcusY * blocksPerMcu;
|
|
806
|
+
const coefs = new Int16Array(total * 64);
|
|
807
|
+
|
|
808
|
+
const quantInv = (table) => {
|
|
809
|
+
const t = new Float32Array(64);
|
|
810
|
+
for (let i = 0; i < 64; i++) t[i] = 1 / (table[i] * aan[i >> 3] * aan[i & 7] * 8);
|
|
811
|
+
return t;
|
|
812
|
+
};
|
|
813
|
+
const invLuma = quantInv(tables.luma);
|
|
814
|
+
const invChroma = same ? invLuma : quantInv(tables.chroma);
|
|
815
|
+
const block = new Float32Array(64);
|
|
816
|
+
|
|
817
|
+
const quantizeBlock = (plane, bx, by, ci, outOff) => {
|
|
818
|
+
const pw = plane.width;
|
|
819
|
+
const ph = plane.height;
|
|
820
|
+
const data = plane.data;
|
|
821
|
+
for (let y = 0; y < 8; y++) {
|
|
822
|
+
let yy = by * 8 + y;
|
|
823
|
+
if (yy >= ph) yy = ph - 1;
|
|
824
|
+
const row = yy * pw;
|
|
825
|
+
for (let x = 0; x < 8; x++) {
|
|
826
|
+
let xx = bx * 8 + x;
|
|
827
|
+
if (xx >= pw) xx = pw - 1;
|
|
828
|
+
block[y * 8 + x] = data[row + xx] - 128;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
fdctBlock(block);
|
|
832
|
+
const inv = ci === 0 ? invLuma : invChroma;
|
|
833
|
+
coefs[outOff] = Math.round(block[0] * inv[0]);
|
|
834
|
+
for (let k = 1; k < 64; k++) {
|
|
835
|
+
const idx = zigzag[k];
|
|
836
|
+
const x = block[idx] * inv[idx];
|
|
837
|
+
const a = Math.floor((x < 0 ? -x : x) + bias);
|
|
838
|
+
coefs[outOff + k] = x < 0 ? -a : a;
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
let bi = 0;
|
|
843
|
+
const order = [];
|
|
844
|
+
for (let my = 0; my < mcusY; my++) {
|
|
845
|
+
for (let mx = 0; mx < mcusX; mx++) {
|
|
846
|
+
for (let v = 0; v < vs; v++) {
|
|
847
|
+
for (let h = 0; h < hs; h++) {
|
|
848
|
+
quantizeBlock(source.planes[0], mx * hs + h, my * vs + v, 0, bi * 64);
|
|
849
|
+
order.push(0);
|
|
850
|
+
bi++;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
if (!isGray) {
|
|
854
|
+
quantizeBlock(source.planes[1], mx, my, 1, bi * 64);
|
|
855
|
+
order.push(1);
|
|
856
|
+
bi++;
|
|
857
|
+
quantizeBlock(source.planes[2], mx, my, 2, bi * 64);
|
|
858
|
+
order.push(2);
|
|
859
|
+
bi++;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
const walk = (visit) => {
|
|
865
|
+
const preds = [0, 0, 0];
|
|
866
|
+
for (let b = 0; b < total; b++) {
|
|
867
|
+
const ci = order[b];
|
|
868
|
+
const o = b * 64;
|
|
869
|
+
const chroma = ci === 0 ? 0 : 1;
|
|
870
|
+
const diff = coefs[o] - preds[ci];
|
|
871
|
+
preds[ci] = coefs[o];
|
|
872
|
+
visit.dc(chroma, bitLength(diff < 0 ? -diff : diff), diff);
|
|
873
|
+
let run = 0;
|
|
874
|
+
for (let k = 1; k < 64; k++) {
|
|
875
|
+
const v = coefs[o + k];
|
|
876
|
+
if (v === 0) {
|
|
877
|
+
run++;
|
|
878
|
+
continue;
|
|
879
|
+
}
|
|
880
|
+
while (run > 15) {
|
|
881
|
+
visit.ac(chroma, 0xf0, 0, 0);
|
|
882
|
+
run -= 16;
|
|
883
|
+
}
|
|
884
|
+
const nb = bitLength(v < 0 ? -v : v);
|
|
885
|
+
visit.ac(chroma, (run << 4) | nb, v, nb);
|
|
886
|
+
run = 0;
|
|
887
|
+
}
|
|
888
|
+
if (run > 0) visit.ac(chroma, 0, 0, 0);
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
|
|
892
|
+
let spec = { dcLuma: huffSpec.dcLuma, dcChroma: huffSpec.dcChroma, acLuma: huffSpec.acLuma, acChroma: huffSpec.acChroma };
|
|
893
|
+
if (optimal) {
|
|
894
|
+
const f = { dc: [new Float64Array(256), new Float64Array(256)], ac: [new Float64Array(256), new Float64Array(256)] };
|
|
895
|
+
walk({
|
|
896
|
+
dc: (c, nb) => {
|
|
897
|
+
f.dc[c][nb]++;
|
|
898
|
+
},
|
|
899
|
+
ac: (c, sym) => {
|
|
900
|
+
f.ac[c][sym]++;
|
|
901
|
+
},
|
|
902
|
+
});
|
|
903
|
+
spec = {
|
|
904
|
+
dcLuma: optimalSpec(f.dc[0]),
|
|
905
|
+
dcChroma: isGray ? huffSpec.dcChroma : optimalSpec(f.dc[1]),
|
|
906
|
+
acLuma: optimalSpec(f.ac[0]),
|
|
907
|
+
acChroma: isGray ? huffSpec.acChroma : optimalSpec(f.ac[1]),
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
const enc = {
|
|
911
|
+
dcLuma: buildEncodeTable(spec.dcLuma),
|
|
912
|
+
dcChroma: buildEncodeTable(spec.dcChroma),
|
|
913
|
+
acLuma: buildEncodeTable(spec.acLuma),
|
|
914
|
+
acChroma: buildEncodeTable(spec.acChroma),
|
|
915
|
+
};
|
|
916
|
+
|
|
917
|
+
const out = new BitWriter(Math.max(4096, (width * height) >> 1));
|
|
918
|
+
out.bytes([0xff, 0xd8]);
|
|
919
|
+
out.bytes([0xff, 0xe0, 0, 16, 0x4a, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0]);
|
|
920
|
+
const dqt = (id, table) => {
|
|
921
|
+
const seg = [0xff, 0xdb, 0, 67, id];
|
|
922
|
+
for (let i = 0; i < 64; i++) seg.push(table[zigzag[i]]);
|
|
923
|
+
out.bytes(seg);
|
|
924
|
+
};
|
|
925
|
+
dqt(0, tables.luma);
|
|
926
|
+
if (!isGray && !same) dqt(1, tables.chroma);
|
|
927
|
+
const sof = [0xff, 0xc0, 0, 8 + nc * 3, 8, height >> 8, height & 255, width >> 8, width & 255, nc];
|
|
928
|
+
sof.push(1, (hs << 4) | vs, 0);
|
|
929
|
+
if (!isGray) sof.push(2, 0x11, same ? 0 : 1, 3, 0x11, same ? 0 : 1);
|
|
930
|
+
out.bytes(sof);
|
|
931
|
+
const dht = (cls, id, sp) => {
|
|
932
|
+
out.bytes([0xff, 0xc4, 0, 19 + sp.vals.length, (cls << 4) | id]);
|
|
933
|
+
out.bytes(sp.bits);
|
|
934
|
+
out.bytes(sp.vals);
|
|
935
|
+
};
|
|
936
|
+
dht(0, 0, spec.dcLuma);
|
|
937
|
+
dht(1, 0, spec.acLuma);
|
|
938
|
+
if (!isGray) {
|
|
939
|
+
dht(0, 1, spec.dcChroma);
|
|
940
|
+
dht(1, 1, spec.acChroma);
|
|
941
|
+
}
|
|
942
|
+
const sos = [0xff, 0xda, 0, 6 + nc * 2, nc, 1, 0x00];
|
|
943
|
+
if (!isGray) sos.push(2, 0x11, 3, 0x11);
|
|
944
|
+
sos.push(0, 63, 0);
|
|
945
|
+
out.bytes(sos);
|
|
946
|
+
|
|
947
|
+
walk({
|
|
948
|
+
dc: (c, nb, diff) => {
|
|
949
|
+
const t = c === 0 ? enc.dcLuma : enc.dcChroma;
|
|
950
|
+
out.put(t.code[nb], t.size[nb]);
|
|
951
|
+
if (nb) out.put(diff < 0 ? diff - 1 : diff, nb);
|
|
952
|
+
},
|
|
953
|
+
ac: (c, sym, v, nb) => {
|
|
954
|
+
const t = c === 0 ? enc.acLuma : enc.acChroma;
|
|
955
|
+
out.put(t.code[sym], t.size[sym]);
|
|
956
|
+
if (nb) out.put(v < 0 ? v - 1 : v, nb);
|
|
957
|
+
},
|
|
958
|
+
});
|
|
959
|
+
out.flush();
|
|
960
|
+
out.bytes([0xff, 0xd9]);
|
|
961
|
+
return Buffer.from(out.result());
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
export { decodeJpeg, encodeJpeg, probeJpeg };
|