imagescript 3.4.0 → 3.5.0
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 +5 -1
- package/THIRD_PARTY_NOTICES.md +40 -3
- package/dist/codecs/av1/cdef.js +17 -3
- package/dist/codecs/av1/encode.js +1512 -0
- package/dist/codecs/av1/fdct-lee.js +156 -0
- package/dist/codecs/av1/fdct.js +185 -0
- package/dist/codecs/av1/itx.js +15 -0
- package/dist/codecs/av1/msac-enc.js +126 -0
- package/dist/codecs/av1/obu-enc.js +153 -0
- package/dist/codecs/av1/palette.js +110 -0
- package/dist/codecs/avif-enc.js +210 -0
- package/dist/codecs/jxl/color.js +28 -10
- package/dist/codecs/jxl/dct.js +3 -38
- package/dist/codecs/jxl/entropy.js +27 -23
- package/dist/codecs/jxl/fast-lossless.js +805 -0
- package/dist/codecs/jxl/filters.js +66 -32
- package/dist/codecs/jxl/modular.js +69 -42
- package/dist/codecs/jxl.js +1 -0
- package/dist/image.d.ts +22 -0
- package/dist/image.js +7 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Generated by scripts/av1-fdct.py: the 4-, 8- and 16-point DCTs of fdct.ts's recursion, unrolled
|
|
3
|
+
*/
|
|
4
|
+
export function dct4(x, o) {
|
|
5
|
+
const t0 = x[o] + x[o + 3];
|
|
6
|
+
const t1 = x[o + 1] + x[o + 2];
|
|
7
|
+
const t2 = (x[o] - x[o + 3]) * 0.541196100146197;
|
|
8
|
+
const t3 = (x[o + 1] - x[o + 2]) * 1.3065629648763764;
|
|
9
|
+
const t4 = t0 + t1;
|
|
10
|
+
const t5 = (t0 - t1) * 0.7071067811865475;
|
|
11
|
+
const t6 = t2 + t3;
|
|
12
|
+
const t7 = (t2 - t3) * 0.7071067811865475;
|
|
13
|
+
const t8 = t6 + t7;
|
|
14
|
+
x[o] = t4;
|
|
15
|
+
x[o + 1] = t8;
|
|
16
|
+
x[o + 2] = t5;
|
|
17
|
+
x[o + 3] = t7;
|
|
18
|
+
}
|
|
19
|
+
export function dct8(x, o) {
|
|
20
|
+
const t0 = x[o] + x[o + 7];
|
|
21
|
+
const t1 = x[o + 1] + x[o + 6];
|
|
22
|
+
const t2 = x[o + 2] + x[o + 5];
|
|
23
|
+
const t3 = x[o + 3] + x[o + 4];
|
|
24
|
+
const t4 = (x[o] - x[o + 7]) * 0.5097955791041592;
|
|
25
|
+
const t5 = (x[o + 1] - x[o + 6]) * 0.6013448869350453;
|
|
26
|
+
const t6 = (x[o + 2] - x[o + 5]) * 0.8999762231364156;
|
|
27
|
+
const t7 = (x[o + 3] - x[o + 4]) * 2.5629154477415055;
|
|
28
|
+
const t8 = t0 + t3;
|
|
29
|
+
const t9 = t1 + t2;
|
|
30
|
+
const t10 = (t0 - t3) * 0.541196100146197;
|
|
31
|
+
const t11 = (t1 - t2) * 1.3065629648763764;
|
|
32
|
+
const t12 = t8 + t9;
|
|
33
|
+
const t13 = (t8 - t9) * 0.7071067811865475;
|
|
34
|
+
const t14 = t10 + t11;
|
|
35
|
+
const t15 = (t10 - t11) * 0.7071067811865475;
|
|
36
|
+
const t16 = t14 + t15;
|
|
37
|
+
const t17 = t4 + t7;
|
|
38
|
+
const t18 = t5 + t6;
|
|
39
|
+
const t19 = (t4 - t7) * 0.541196100146197;
|
|
40
|
+
const t20 = (t5 - t6) * 1.3065629648763764;
|
|
41
|
+
const t21 = t17 + t18;
|
|
42
|
+
const t22 = (t17 - t18) * 0.7071067811865475;
|
|
43
|
+
const t23 = t19 + t20;
|
|
44
|
+
const t24 = (t19 - t20) * 0.7071067811865475;
|
|
45
|
+
const t25 = t23 + t24;
|
|
46
|
+
const t26 = t21 + t25;
|
|
47
|
+
const t27 = t25 + t22;
|
|
48
|
+
const t28 = t22 + t24;
|
|
49
|
+
x[o] = t12;
|
|
50
|
+
x[o + 1] = t26;
|
|
51
|
+
x[o + 2] = t16;
|
|
52
|
+
x[o + 3] = t27;
|
|
53
|
+
x[o + 4] = t13;
|
|
54
|
+
x[o + 5] = t28;
|
|
55
|
+
x[o + 6] = t15;
|
|
56
|
+
x[o + 7] = t24;
|
|
57
|
+
}
|
|
58
|
+
export function dct16(x, o) {
|
|
59
|
+
const t0 = x[o] + x[o + 15];
|
|
60
|
+
const t1 = x[o + 1] + x[o + 14];
|
|
61
|
+
const t2 = x[o + 2] + x[o + 13];
|
|
62
|
+
const t3 = x[o + 3] + x[o + 12];
|
|
63
|
+
const t4 = x[o + 4] + x[o + 11];
|
|
64
|
+
const t5 = x[o + 5] + x[o + 10];
|
|
65
|
+
const t6 = x[o + 6] + x[o + 9];
|
|
66
|
+
const t7 = x[o + 7] + x[o + 8];
|
|
67
|
+
const t8 = (x[o] - x[o + 15]) * 0.5024192861881557;
|
|
68
|
+
const t9 = (x[o + 1] - x[o + 14]) * 0.5224986149396889;
|
|
69
|
+
const t10 = (x[o + 2] - x[o + 13]) * 0.5669440348163577;
|
|
70
|
+
const t11 = (x[o + 3] - x[o + 12]) * 0.6468217833599901;
|
|
71
|
+
const t12 = (x[o + 4] - x[o + 11]) * 0.7881546234512502;
|
|
72
|
+
const t13 = (x[o + 5] - x[o + 10]) * 1.060677685990347;
|
|
73
|
+
const t14 = (x[o + 6] - x[o + 9]) * 1.7224470982383342;
|
|
74
|
+
const t15 = (x[o + 7] - x[o + 8]) * 5.101148618689155;
|
|
75
|
+
const t16 = t0 + t7;
|
|
76
|
+
const t17 = t1 + t6;
|
|
77
|
+
const t18 = t2 + t5;
|
|
78
|
+
const t19 = t3 + t4;
|
|
79
|
+
const t20 = (t0 - t7) * 0.5097955791041592;
|
|
80
|
+
const t21 = (t1 - t6) * 0.6013448869350453;
|
|
81
|
+
const t22 = (t2 - t5) * 0.8999762231364156;
|
|
82
|
+
const t23 = (t3 - t4) * 2.5629154477415055;
|
|
83
|
+
const t24 = t16 + t19;
|
|
84
|
+
const t25 = t17 + t18;
|
|
85
|
+
const t26 = (t16 - t19) * 0.541196100146197;
|
|
86
|
+
const t27 = (t17 - t18) * 1.3065629648763764;
|
|
87
|
+
const t28 = t24 + t25;
|
|
88
|
+
const t29 = (t24 - t25) * 0.7071067811865475;
|
|
89
|
+
const t30 = t26 + t27;
|
|
90
|
+
const t31 = (t26 - t27) * 0.7071067811865475;
|
|
91
|
+
const t32 = t30 + t31;
|
|
92
|
+
const t33 = t20 + t23;
|
|
93
|
+
const t34 = t21 + t22;
|
|
94
|
+
const t35 = (t20 - t23) * 0.541196100146197;
|
|
95
|
+
const t36 = (t21 - t22) * 1.3065629648763764;
|
|
96
|
+
const t37 = t33 + t34;
|
|
97
|
+
const t38 = (t33 - t34) * 0.7071067811865475;
|
|
98
|
+
const t39 = t35 + t36;
|
|
99
|
+
const t40 = (t35 - t36) * 0.7071067811865475;
|
|
100
|
+
const t41 = t39 + t40;
|
|
101
|
+
const t42 = t37 + t41;
|
|
102
|
+
const t43 = t41 + t38;
|
|
103
|
+
const t44 = t38 + t40;
|
|
104
|
+
const t45 = t8 + t15;
|
|
105
|
+
const t46 = t9 + t14;
|
|
106
|
+
const t47 = t10 + t13;
|
|
107
|
+
const t48 = t11 + t12;
|
|
108
|
+
const t49 = (t8 - t15) * 0.5097955791041592;
|
|
109
|
+
const t50 = (t9 - t14) * 0.6013448869350453;
|
|
110
|
+
const t51 = (t10 - t13) * 0.8999762231364156;
|
|
111
|
+
const t52 = (t11 - t12) * 2.5629154477415055;
|
|
112
|
+
const t53 = t45 + t48;
|
|
113
|
+
const t54 = t46 + t47;
|
|
114
|
+
const t55 = (t45 - t48) * 0.541196100146197;
|
|
115
|
+
const t56 = (t46 - t47) * 1.3065629648763764;
|
|
116
|
+
const t57 = t53 + t54;
|
|
117
|
+
const t58 = (t53 - t54) * 0.7071067811865475;
|
|
118
|
+
const t59 = t55 + t56;
|
|
119
|
+
const t60 = (t55 - t56) * 0.7071067811865475;
|
|
120
|
+
const t61 = t59 + t60;
|
|
121
|
+
const t62 = t49 + t52;
|
|
122
|
+
const t63 = t50 + t51;
|
|
123
|
+
const t64 = (t49 - t52) * 0.541196100146197;
|
|
124
|
+
const t65 = (t50 - t51) * 1.3065629648763764;
|
|
125
|
+
const t66 = t62 + t63;
|
|
126
|
+
const t67 = (t62 - t63) * 0.7071067811865475;
|
|
127
|
+
const t68 = t64 + t65;
|
|
128
|
+
const t69 = (t64 - t65) * 0.7071067811865475;
|
|
129
|
+
const t70 = t68 + t69;
|
|
130
|
+
const t71 = t66 + t70;
|
|
131
|
+
const t72 = t70 + t67;
|
|
132
|
+
const t73 = t67 + t69;
|
|
133
|
+
const t74 = t57 + t71;
|
|
134
|
+
const t75 = t71 + t61;
|
|
135
|
+
const t76 = t61 + t72;
|
|
136
|
+
const t77 = t72 + t58;
|
|
137
|
+
const t78 = t58 + t73;
|
|
138
|
+
const t79 = t73 + t60;
|
|
139
|
+
const t80 = t60 + t69;
|
|
140
|
+
x[o] = t28;
|
|
141
|
+
x[o + 1] = t74;
|
|
142
|
+
x[o + 2] = t42;
|
|
143
|
+
x[o + 3] = t75;
|
|
144
|
+
x[o + 4] = t32;
|
|
145
|
+
x[o + 5] = t76;
|
|
146
|
+
x[o + 6] = t43;
|
|
147
|
+
x[o + 7] = t77;
|
|
148
|
+
x[o + 8] = t29;
|
|
149
|
+
x[o + 9] = t78;
|
|
150
|
+
x[o + 10] = t44;
|
|
151
|
+
x[o + 11] = t79;
|
|
152
|
+
x[o + 12] = t31;
|
|
153
|
+
x[o + 13] = t80;
|
|
154
|
+
x[o + 14] = t40;
|
|
155
|
+
x[o + 15] = t69;
|
|
156
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { TXFM_DIMENSIONS } from "./tables.js";
|
|
2
|
+
import { inverseBasis, transformTypes1D, passShift } from "./itx.js";
|
|
3
|
+
import { dct4, dct8, dct16 } from "./fdct-lee.js";
|
|
4
|
+
function pseudoInverse(b, n, k) {
|
|
5
|
+
const g = new Float64Array(k * 2 * k);
|
|
6
|
+
for (let i = 0; i < k; i++) {
|
|
7
|
+
for (let j = 0; j < k; j++) {
|
|
8
|
+
let s = 0;
|
|
9
|
+
for (let r = 0; r < n; r++)
|
|
10
|
+
s += b[r * k + i] * b[r * k + j];
|
|
11
|
+
g[i * 2 * k + j] = s;
|
|
12
|
+
}
|
|
13
|
+
g[i * 2 * k + k + i] = 1;
|
|
14
|
+
}
|
|
15
|
+
for (let c = 0; c < k; c++) {
|
|
16
|
+
let p = c;
|
|
17
|
+
for (let r = c + 1; r < k; r++)
|
|
18
|
+
if (Math.abs(g[r * 2 * k + c]) > Math.abs(g[p * 2 * k + c]))
|
|
19
|
+
p = r;
|
|
20
|
+
if (p !== c) {
|
|
21
|
+
for (let j = 0; j < 2 * k; j++) {
|
|
22
|
+
const t = g[c * 2 * k + j];
|
|
23
|
+
g[c * 2 * k + j] = g[p * 2 * k + j];
|
|
24
|
+
g[p * 2 * k + j] = t;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const pivot = g[c * 2 * k + c];
|
|
28
|
+
for (let j = 0; j < 2 * k; j++)
|
|
29
|
+
g[c * 2 * k + j] /= pivot;
|
|
30
|
+
for (let r = 0; r < k; r++) {
|
|
31
|
+
if (r === c)
|
|
32
|
+
continue;
|
|
33
|
+
const f = g[r * 2 * k + c];
|
|
34
|
+
if (f)
|
|
35
|
+
for (let j = 0; j < 2 * k; j++)
|
|
36
|
+
g[r * 2 * k + j] -= f * g[c * 2 * k + j];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const out = new Float64Array(k * n);
|
|
40
|
+
for (let i = 0; i < k; i++) {
|
|
41
|
+
for (let r = 0; r < n; r++) {
|
|
42
|
+
let s = 0;
|
|
43
|
+
for (let j = 0; j < k; j++)
|
|
44
|
+
s += g[i * 2 * k + k + j] * b[r * k + j];
|
|
45
|
+
out[i * n + r] = s;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
const projections = new Map();
|
|
51
|
+
const projection = (log2, type) => {
|
|
52
|
+
const key = log2 * 4 + type;
|
|
53
|
+
let p = projections.get(key);
|
|
54
|
+
if (!p) {
|
|
55
|
+
const n = 4 << log2;
|
|
56
|
+
projections.set(key, p = pseudoInverse(inverseBasis(log2, type), n, Math.min(n, 32)));
|
|
57
|
+
}
|
|
58
|
+
return p;
|
|
59
|
+
};
|
|
60
|
+
const tmp = new Float64Array(64 * 32);
|
|
61
|
+
const TWIDDLES = new Float64Array(64);
|
|
62
|
+
for (let n = 32; n <= 64; n *= 2)
|
|
63
|
+
for (let i = 0; i < n / 2; i++)
|
|
64
|
+
TWIDDLES[n / 2 + i] = 1 / (2 * Math.cos(Math.PI * (2 * i + 1) / (2 * n)));
|
|
65
|
+
function dct(x, o, n, scratch, so) {
|
|
66
|
+
if (n === 4)
|
|
67
|
+
return dct4(x, o);
|
|
68
|
+
if (n === 8)
|
|
69
|
+
return dct8(x, o);
|
|
70
|
+
if (n === 16)
|
|
71
|
+
return dct16(x, o);
|
|
72
|
+
const half = n / 2;
|
|
73
|
+
for (let i = 0; i < half; i++) {
|
|
74
|
+
const a = x[o + i], b = x[o + n - 1 - i];
|
|
75
|
+
scratch[so + i] = a + b;
|
|
76
|
+
scratch[so + half + i] = (a - b) * TWIDDLES[half + i];
|
|
77
|
+
}
|
|
78
|
+
dct(scratch, so, half, x, o);
|
|
79
|
+
dct(scratch, so + half, half, x, o + half);
|
|
80
|
+
for (let k = 0; k < half; k++) {
|
|
81
|
+
x[o + 2 * k] = scratch[so + k];
|
|
82
|
+
x[o + 2 * k + 1] = scratch[so + half + k] + (k + 1 < half ? scratch[so + half + k + 1] : 0);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const dctScales = new Map();
|
|
86
|
+
const dctScale = (log2) => {
|
|
87
|
+
let s = dctScales.get(log2);
|
|
88
|
+
if (!s) {
|
|
89
|
+
const n = 4 << log2, k = Math.min(n, 32), p = projection(log2, 0);
|
|
90
|
+
s = Float64Array.from({ length: k }, (_, j) => p[j * n] / Math.cos(Math.PI * j / (2 * n)));
|
|
91
|
+
dctScales.set(log2, s);
|
|
92
|
+
}
|
|
93
|
+
return s;
|
|
94
|
+
};
|
|
95
|
+
const line = new Float64Array(64), scratch = new Float64Array(64);
|
|
96
|
+
function forward1D(log2, type, inp, io, step, out, oo, outStep) {
|
|
97
|
+
const n = 4 << log2, k = Math.min(n, 32);
|
|
98
|
+
if (type === 0) {
|
|
99
|
+
for (let i = 0; i < n; i++)
|
|
100
|
+
line[i] = inp[io + i * step];
|
|
101
|
+
dct(line, 0, n, scratch, 0);
|
|
102
|
+
const s = dctScale(log2);
|
|
103
|
+
for (let j = 0; j < k; j++)
|
|
104
|
+
out[oo + j * outStep] = line[j] * s[j];
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const p = projection(log2, type);
|
|
108
|
+
for (let i = 0; i < n; i++)
|
|
109
|
+
line[i] = inp[io + i * step];
|
|
110
|
+
for (let j = 0; j < k; j += 4) {
|
|
111
|
+
let s0 = 0, s1 = 0, s2 = 0, s3 = 0;
|
|
112
|
+
for (let i = 0, r = j * n; i < n; i++, r++) {
|
|
113
|
+
const v = line[i];
|
|
114
|
+
s0 += p[r] * v;
|
|
115
|
+
s1 += p[r + n] * v;
|
|
116
|
+
s2 += p[r + 2 * n] * v;
|
|
117
|
+
s3 += p[r + 3 * n] * v;
|
|
118
|
+
}
|
|
119
|
+
out[oo + j * outStep] = s0;
|
|
120
|
+
out[oo + (j + 1) * outStep] = s1;
|
|
121
|
+
out[oo + (j + 2) * outStep] = s2;
|
|
122
|
+
out[oo + (j + 3) * outStep] = s3;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export function forwardTransform(res, tx, txtp, out) {
|
|
126
|
+
const w = 4 * TXFM_DIMENSIONS[tx * 8], h = 4 * TXFM_DIMENSIONS[tx * 8 + 1];
|
|
127
|
+
const lw = TXFM_DIMENSIONS[tx * 8 + 2], lh = TXFM_DIMENSIONS[tx * 8 + 3];
|
|
128
|
+
const kw = Math.min(w, 32), kh = Math.min(h, 32);
|
|
129
|
+
const [rowType, colType] = transformTypes1D(txtp);
|
|
130
|
+
const scale = 16 * 2 ** passShift(tx) / (w * 2 === h || h * 2 === w ? 181 / 256 : 1);
|
|
131
|
+
for (let y = 0; y < h; y++)
|
|
132
|
+
forward1D(lw, rowType, res, y * w, 1, tmp, y * kw, 1);
|
|
133
|
+
for (let kx = 0; kx < kw; kx++)
|
|
134
|
+
forward1D(lh, colType, tmp, kx, kw, out, kx * kh, 1);
|
|
135
|
+
for (let i = 0; i < kw * kh; i++)
|
|
136
|
+
out[i] *= scale;
|
|
137
|
+
}
|
|
138
|
+
const energies = new Map();
|
|
139
|
+
export function pixelEnergy(tx) {
|
|
140
|
+
let e = energies.get(tx);
|
|
141
|
+
if (e === undefined) {
|
|
142
|
+
const w = 4 * TXFM_DIMENSIONS[tx * 8], h = 4 * TXFM_DIMENSIONS[tx * 8 + 1];
|
|
143
|
+
const lw = TXFM_DIMENSIONS[tx * 8 + 2], lh = TXFM_DIMENSIONS[tx * 8 + 3];
|
|
144
|
+
const norm = (log2) => {
|
|
145
|
+
const n = 4 << log2, k = Math.min(n, 32), b = inverseBasis(log2, 0);
|
|
146
|
+
let s = 0;
|
|
147
|
+
for (let i = 0; i < n * k; i++)
|
|
148
|
+
s += b[i] * b[i];
|
|
149
|
+
return s / k;
|
|
150
|
+
};
|
|
151
|
+
const scale = (w * 2 === h || h * 2 === w ? 181 / 256 : 1) / (16 * 2 ** passShift(tx));
|
|
152
|
+
energies.set(tx, e = scale * scale * norm(lw) * norm(lh));
|
|
153
|
+
}
|
|
154
|
+
return e;
|
|
155
|
+
}
|
|
156
|
+
export function forwardWht(res, out) {
|
|
157
|
+
for (let i = 0; i < 4; i++) {
|
|
158
|
+
let a = res[i], b = res[4 + i], c = res[8 + i], d = res[12 + i];
|
|
159
|
+
a += b;
|
|
160
|
+
d -= c;
|
|
161
|
+
const e = (a - d) >> 1;
|
|
162
|
+
b = e - b;
|
|
163
|
+
c = e - c;
|
|
164
|
+
a -= c;
|
|
165
|
+
d += b;
|
|
166
|
+
out[i] = a;
|
|
167
|
+
out[4 + i] = c;
|
|
168
|
+
out[8 + i] = d;
|
|
169
|
+
out[12 + i] = b;
|
|
170
|
+
}
|
|
171
|
+
for (let i = 0; i < 4; i++) {
|
|
172
|
+
let a = out[i * 4], b = out[i * 4 + 1], c = out[i * 4 + 2], d = out[i * 4 + 3];
|
|
173
|
+
a += b;
|
|
174
|
+
d -= c;
|
|
175
|
+
const e = (a - d) >> 1;
|
|
176
|
+
b = e - b;
|
|
177
|
+
c = e - c;
|
|
178
|
+
a -= c;
|
|
179
|
+
d += b;
|
|
180
|
+
out[i * 4] = a;
|
|
181
|
+
out[i * 4 + 1] = c;
|
|
182
|
+
out[i * 4 + 2] = d;
|
|
183
|
+
out[i * 4 + 3] = b;
|
|
184
|
+
}
|
|
185
|
+
}
|
package/dist/codecs/av1/itx.js
CHANGED
|
@@ -867,6 +867,21 @@ const TX1D_TYPES = [
|
|
|
867
867
|
];
|
|
868
868
|
const SHIFT = [0, 1, 2, 2, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2];
|
|
869
869
|
const tmp = new Int32Array(64 * 64);
|
|
870
|
+
export function inverseBasis(log2, type) {
|
|
871
|
+
const n = 4 << log2, k = Math.min(n, 32), out = new Float64Array(n * k), c = new Int32Array(n);
|
|
872
|
+
lo = -(2 ** 31);
|
|
873
|
+
hi = 2 ** 31 - 1;
|
|
874
|
+
for (let j = 0; j < k; j++) {
|
|
875
|
+
c.fill(0);
|
|
876
|
+
c[j] = 4096;
|
|
877
|
+
TX1D[log2][type](c, 0, 1);
|
|
878
|
+
for (let i = 0; i < n; i++)
|
|
879
|
+
out[i * k + j] = c[i] / 4096;
|
|
880
|
+
}
|
|
881
|
+
return out;
|
|
882
|
+
}
|
|
883
|
+
export const transformTypes1D = (txtp) => TX1D_TYPES[MIRROR[txtp]];
|
|
884
|
+
export const passShift = (tx) => SHIFT[tx];
|
|
870
885
|
export function inverseTransformAdd(dst, d, stride, coeff, eob, tx, txtp, bitdepthMax) {
|
|
871
886
|
if (txtp === 16)
|
|
872
887
|
return whtAdd(dst, d, stride, coeff, bitdepthMax);
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* The AV1 symbol encoder (a multi-symbol arithmetic coder with adapting CDFs), ported from libaom
|
|
3
|
+
* (aom_dsp/entenc.c: od_ec_encode_q15, od_ec_enc_normalize, od_ec_enc_done). Copyright (c) 2001-2016, Alliance for Open
|
|
4
|
+
* Media, BSD-2-Clause License (see THIRD_PARTY_NOTICES.md)
|
|
5
|
+
*/
|
|
6
|
+
const PROB_SHIFT = 6, MIN_PROB = 4;
|
|
7
|
+
export class SymbolEncoder {
|
|
8
|
+
allowUpdate;
|
|
9
|
+
buf = [];
|
|
10
|
+
low = 0;
|
|
11
|
+
rng = 0x8000;
|
|
12
|
+
cnt = -9;
|
|
13
|
+
constructor(allowUpdate = true) {
|
|
14
|
+
this.allowUpdate = allowUpdate;
|
|
15
|
+
}
|
|
16
|
+
encode(fl, fh, s, n) {
|
|
17
|
+
let l = this.low, r = this.rng;
|
|
18
|
+
const v = ((r >> 8) * (fh >> PROB_SHIFT) >> (7 - PROB_SHIFT)) + MIN_PROB * (n - s);
|
|
19
|
+
if (fl < 32768) {
|
|
20
|
+
const u = ((r >> 8) * (fl >> PROB_SHIFT) >> (7 - PROB_SHIFT)) + MIN_PROB * (n - s + 1);
|
|
21
|
+
l += r - u;
|
|
22
|
+
r = u - v;
|
|
23
|
+
}
|
|
24
|
+
else
|
|
25
|
+
r -= v;
|
|
26
|
+
this.normalize(l, r);
|
|
27
|
+
}
|
|
28
|
+
normalize(low, rng) {
|
|
29
|
+
let c = this.cnt;
|
|
30
|
+
const d = 16 - (32 - Math.clz32(rng));
|
|
31
|
+
let s = c + d;
|
|
32
|
+
if (s >= 0) {
|
|
33
|
+
c += 16;
|
|
34
|
+
let m = 2 ** c - 1;
|
|
35
|
+
if (s >= 8) {
|
|
36
|
+
this.buf.push(Math.floor(low / 2 ** c));
|
|
37
|
+
low %= m + 1;
|
|
38
|
+
c -= 8;
|
|
39
|
+
m = (m + 1) / 256 - 1;
|
|
40
|
+
}
|
|
41
|
+
this.buf.push(Math.floor(low / 2 ** c));
|
|
42
|
+
s = c + d - 24;
|
|
43
|
+
low %= m + 1;
|
|
44
|
+
}
|
|
45
|
+
this.low = low * 2 ** d;
|
|
46
|
+
this.rng = rng << d;
|
|
47
|
+
this.cnt = s;
|
|
48
|
+
}
|
|
49
|
+
symbol(cdf, o, n, val) {
|
|
50
|
+
this.encode(val ? cdf[o + val - 1] : 32768, val === n ? 0 : cdf[o + val], val, n);
|
|
51
|
+
if (this.allowUpdate) {
|
|
52
|
+
const count = cdf[o + n];
|
|
53
|
+
const rate = 4 + (count >> 4) + (n > 2 ? 1 : 0);
|
|
54
|
+
let i = 0;
|
|
55
|
+
for (; i < val; i++)
|
|
56
|
+
cdf[o + i] += 32768 - cdf[o + i] >> rate;
|
|
57
|
+
for (; i < n; i++)
|
|
58
|
+
cdf[o + i] -= cdf[o + i] >> rate;
|
|
59
|
+
cdf[o + n] = count + (count < 32 ? 1 : 0);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
boolAdapt(cdf, o, bit) {
|
|
63
|
+
this.symbol(cdf, o, 1, bit);
|
|
64
|
+
}
|
|
65
|
+
boolProb(f, bit) {
|
|
66
|
+
this.encode(bit ? f : 32768, bit ? 0 : f, bit, 1);
|
|
67
|
+
}
|
|
68
|
+
bool(bit) {
|
|
69
|
+
this.boolProb(16384, bit);
|
|
70
|
+
}
|
|
71
|
+
bools(n, v) {
|
|
72
|
+
while (n--)
|
|
73
|
+
this.bool(v >> n & 1);
|
|
74
|
+
}
|
|
75
|
+
uniform(n, v) {
|
|
76
|
+
const l = 32 - Math.clz32(n), m = (1 << l) - n;
|
|
77
|
+
if (v < m)
|
|
78
|
+
this.bools(l - 1, v);
|
|
79
|
+
else {
|
|
80
|
+
this.bools(l - 1, v + m >> 1);
|
|
81
|
+
this.bool(v + m & 1);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
hiTok(cdf, o, tok) {
|
|
85
|
+
for (let base = 3;; base += 3) {
|
|
86
|
+
const br = Math.min(tok - base, 3);
|
|
87
|
+
this.symbol(cdf, o, 3, br);
|
|
88
|
+
if (br < 3 || base === 12)
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
golomb(v) {
|
|
93
|
+
const x = v + 1, len = 31 - Math.clz32(x);
|
|
94
|
+
for (let i = 0; i < len; i++)
|
|
95
|
+
this.bool(0);
|
|
96
|
+
this.bool(1);
|
|
97
|
+
for (let i = len - 1; i >= 0; i--)
|
|
98
|
+
this.bool(x >> i & 1);
|
|
99
|
+
}
|
|
100
|
+
finish() {
|
|
101
|
+
const buf = this.buf.slice();
|
|
102
|
+
let c = this.cnt, s = c + 10;
|
|
103
|
+
const m = 0x3fff;
|
|
104
|
+
let e = Math.floor((this.low + m) / (m + 1)) * (m + 1);
|
|
105
|
+
if (Math.floor(e / (m + 1)) % 2 === 0)
|
|
106
|
+
e += m + 1;
|
|
107
|
+
if (s > 0) {
|
|
108
|
+
let n = 2 ** (c + 16);
|
|
109
|
+
do {
|
|
110
|
+
buf.push(Math.floor(e / 2 ** (c + 16)));
|
|
111
|
+
e %= n;
|
|
112
|
+
s -= 8;
|
|
113
|
+
c -= 8;
|
|
114
|
+
n /= 256;
|
|
115
|
+
} while (s > 0);
|
|
116
|
+
}
|
|
117
|
+
const out = new Uint8Array(buf.length);
|
|
118
|
+
let carry = 0;
|
|
119
|
+
for (let i = buf.length - 1; i >= 0; i--) {
|
|
120
|
+
carry += buf[i];
|
|
121
|
+
out[i] = carry & 0xff;
|
|
122
|
+
carry >>= 8;
|
|
123
|
+
}
|
|
124
|
+
return out;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
export class HeaderWriter {
|
|
2
|
+
bytes = [];
|
|
3
|
+
bitCount = 0;
|
|
4
|
+
bit(v) {
|
|
5
|
+
if (!(this.bitCount & 7))
|
|
6
|
+
this.bytes.push(0);
|
|
7
|
+
if (v)
|
|
8
|
+
this.bytes[this.bytes.length - 1] |= 0x80 >> (this.bitCount & 7);
|
|
9
|
+
this.bitCount++;
|
|
10
|
+
}
|
|
11
|
+
bits(n, v) {
|
|
12
|
+
for (let i = n - 1; i >= 0; i--)
|
|
13
|
+
this.bit(Math.floor(v / 2 ** i) & 1);
|
|
14
|
+
}
|
|
15
|
+
sbits(n, v) {
|
|
16
|
+
this.bits(n + 1, v < 0 ? v + 2 ** (n + 1) : v);
|
|
17
|
+
}
|
|
18
|
+
trailing() {
|
|
19
|
+
this.bit(1);
|
|
20
|
+
while (this.bitCount & 7)
|
|
21
|
+
this.bit(0);
|
|
22
|
+
}
|
|
23
|
+
align() {
|
|
24
|
+
while (this.bitCount & 7)
|
|
25
|
+
this.bit(0);
|
|
26
|
+
}
|
|
27
|
+
data() {
|
|
28
|
+
return Uint8Array.from(this.bytes);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export const OBU_SEQUENCE_HEADER = 1, OBU_FRAME = 6;
|
|
32
|
+
export function obu(type, payload) {
|
|
33
|
+
const size = [];
|
|
34
|
+
let n = payload.length;
|
|
35
|
+
do {
|
|
36
|
+
size.push(n & 0x7f | (n >= 0x80 ? 0x80 : 0));
|
|
37
|
+
n = Math.floor(n / 128);
|
|
38
|
+
} while (n);
|
|
39
|
+
const out = new Uint8Array(1 + size.length + payload.length);
|
|
40
|
+
out[0] = type << 3 | 2;
|
|
41
|
+
out.set(size, 1);
|
|
42
|
+
out.set(payload, 1 + size.length);
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
export function writeSequenceHeader(seq, level) {
|
|
46
|
+
const w = new HeaderWriter();
|
|
47
|
+
w.bits(3, seq.profile);
|
|
48
|
+
w.bit(1);
|
|
49
|
+
w.bit(1);
|
|
50
|
+
w.bits(5, level);
|
|
51
|
+
w.bits(4, seq.widthBits - 1);
|
|
52
|
+
w.bits(4, seq.heightBits - 1);
|
|
53
|
+
w.bits(seq.widthBits, seq.maxWidth - 1);
|
|
54
|
+
w.bits(seq.heightBits, seq.maxHeight - 1);
|
|
55
|
+
w.bit(+seq.sb128);
|
|
56
|
+
w.bit(+seq.filterIntra);
|
|
57
|
+
w.bit(+seq.intraEdgeFilter);
|
|
58
|
+
w.bit(+seq.superres);
|
|
59
|
+
w.bit(+seq.cdef);
|
|
60
|
+
w.bit(+seq.restoration);
|
|
61
|
+
w.bit(0);
|
|
62
|
+
if (seq.profile !== 1)
|
|
63
|
+
w.bit(+seq.monochrome);
|
|
64
|
+
w.bit(1);
|
|
65
|
+
w.bits(8, seq.colorPrimaries);
|
|
66
|
+
w.bits(8, seq.transferCharacteristics);
|
|
67
|
+
w.bits(8, seq.matrixCoefficients);
|
|
68
|
+
const srgb = !seq.monochrome && seq.colorPrimaries === 1 && seq.transferCharacteristics === 13 && !seq.matrixCoefficients;
|
|
69
|
+
if (!srgb)
|
|
70
|
+
w.bit(+seq.fullRange);
|
|
71
|
+
if (!seq.monochrome && seq.ssx && seq.ssy)
|
|
72
|
+
w.bits(2, 0);
|
|
73
|
+
if (!seq.monochrome)
|
|
74
|
+
w.bit(+seq.separateUvDeltaQ);
|
|
75
|
+
w.bit(+seq.filmGrain);
|
|
76
|
+
w.trailing();
|
|
77
|
+
return obu(OBU_SEQUENCE_HEADER, w.data());
|
|
78
|
+
}
|
|
79
|
+
export function writeFrameHeader(w, seq, hdr) {
|
|
80
|
+
w.bit(+hdr.disableCdfUpdate);
|
|
81
|
+
w.bit(+hdr.allowScreenContentTools);
|
|
82
|
+
if (hdr.allowScreenContentTools)
|
|
83
|
+
w.bit(0);
|
|
84
|
+
w.bit(0);
|
|
85
|
+
if (hdr.allowScreenContentTools)
|
|
86
|
+
w.bit(+hdr.allowIntrabc);
|
|
87
|
+
const t = hdr.tiles;
|
|
88
|
+
w.bit(1);
|
|
89
|
+
const sbw = hdr.width + 63 >> 6, sbh = hdr.height + 63 >> 6;
|
|
90
|
+
const tileLog2 = (size, target) => {
|
|
91
|
+
let k = 0;
|
|
92
|
+
while (size << k < target)
|
|
93
|
+
k++;
|
|
94
|
+
return k;
|
|
95
|
+
};
|
|
96
|
+
const minLog2Cols = tileLog2(64, sbw), maxLog2Cols = tileLog2(1, Math.min(sbw, 64)), maxLog2Rows = tileLog2(1, Math.min(sbh, 64));
|
|
97
|
+
const minLog2Tiles = Math.max(tileLog2(4096 * 2304 >> 12, sbw * sbh), minLog2Cols);
|
|
98
|
+
for (let i = minLog2Cols; i < maxLog2Cols; i++) {
|
|
99
|
+
w.bit(i < t.log2Cols ? 1 : 0);
|
|
100
|
+
if (i >= t.log2Cols)
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
for (let i = Math.max(minLog2Tiles - t.log2Cols, 0); i < maxLog2Rows; i++) {
|
|
104
|
+
w.bit(i < t.log2Rows ? 1 : 0);
|
|
105
|
+
if (i >= t.log2Rows)
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
if (t.log2Cols || t.log2Rows) {
|
|
109
|
+
w.bits(t.log2Cols + t.log2Rows, t.contextUpdateId);
|
|
110
|
+
w.bits(2, t.sizeBytes - 1);
|
|
111
|
+
}
|
|
112
|
+
const q = hdr.quant;
|
|
113
|
+
const delta = (v) => {
|
|
114
|
+
w.bit(v ? 1 : 0);
|
|
115
|
+
if (v)
|
|
116
|
+
w.sbits(6, v);
|
|
117
|
+
};
|
|
118
|
+
w.bits(8, q.yac);
|
|
119
|
+
delta(q.ydcDelta);
|
|
120
|
+
if (!seq.monochrome) {
|
|
121
|
+
if (seq.separateUvDeltaQ)
|
|
122
|
+
w.bit(0);
|
|
123
|
+
delta(q.udcDelta);
|
|
124
|
+
delta(q.uacDelta);
|
|
125
|
+
}
|
|
126
|
+
w.bit(0);
|
|
127
|
+
w.bit(0);
|
|
128
|
+
if (q.yac)
|
|
129
|
+
w.bit(0);
|
|
130
|
+
if (!hdr.allLossless) {
|
|
131
|
+
const lf = hdr.loopFilter;
|
|
132
|
+
w.bits(6, lf.levelY[0]);
|
|
133
|
+
w.bits(6, lf.levelY[1]);
|
|
134
|
+
if (!seq.monochrome && (lf.levelY[0] || lf.levelY[1])) {
|
|
135
|
+
w.bits(6, lf.levelU);
|
|
136
|
+
w.bits(6, lf.levelV);
|
|
137
|
+
}
|
|
138
|
+
w.bits(3, lf.sharpness);
|
|
139
|
+
w.bit(0);
|
|
140
|
+
if (seq.cdef) {
|
|
141
|
+
const c = hdr.cdef;
|
|
142
|
+
w.bits(2, c.damping - 3);
|
|
143
|
+
w.bits(2, c.bits);
|
|
144
|
+
for (let i = 0; i < 1 << c.bits; i++) {
|
|
145
|
+
w.bits(6, c.yStrengths[i]);
|
|
146
|
+
if (!seq.monochrome)
|
|
147
|
+
w.bits(6, c.uvStrengths[i]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
w.bit(hdr.txMode === 2 ? 1 : 0);
|
|
151
|
+
}
|
|
152
|
+
w.bit(+hdr.reducedTxSet);
|
|
153
|
+
}
|