imagescript 3.3.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.
Files changed (67) hide show
  1. package/README.md +32 -8
  2. package/THIRD_PARTY_NOTICES.md +204 -2
  3. package/dist/animation.d.ts +18 -8
  4. package/dist/animation.js +23 -2
  5. package/dist/codecs/av1/cdef.js +211 -0
  6. package/dist/codecs/av1/data.js +141 -0
  7. package/dist/codecs/av1/decode.js +1769 -0
  8. package/dist/codecs/av1/encode.js +1512 -0
  9. package/dist/codecs/av1/fdct-lee.js +156 -0
  10. package/dist/codecs/av1/fdct.js +185 -0
  11. package/dist/codecs/av1/filmgrain.js +219 -0
  12. package/dist/codecs/av1/frame.js +114 -0
  13. package/dist/codecs/av1/ipred.js +491 -0
  14. package/dist/codecs/av1/itx.js +965 -0
  15. package/dist/codecs/av1/loopfilter.js +149 -0
  16. package/dist/codecs/av1/msac-enc.js +126 -0
  17. package/dist/codecs/av1/msac.js +161 -0
  18. package/dist/codecs/av1/obu-enc.js +153 -0
  19. package/dist/codecs/av1/obu.js +516 -0
  20. package/dist/codecs/av1/palette.js +110 -0
  21. package/dist/codecs/av1/postfilter.js +28 -0
  22. package/dist/codecs/av1/restoration.js +193 -0
  23. package/dist/codecs/av1/tables.js +160 -0
  24. package/dist/codecs/avif-enc.js +210 -0
  25. package/dist/codecs/avif.js +581 -0
  26. package/dist/codecs/bmp.js +317 -0
  27. package/dist/codecs/hdr.d.ts +25 -0
  28. package/dist/codecs/hdr.js +201 -0
  29. package/dist/codecs/ico.js +48 -0
  30. package/dist/codecs/jpeg.js +2 -1
  31. package/dist/codecs/jxl/bits.js +126 -0
  32. package/dist/codecs/jxl/blend.js +117 -0
  33. package/dist/codecs/jxl/color.js +134 -0
  34. package/dist/codecs/jxl/dct.js +369 -0
  35. package/dist/codecs/jxl/entropy.js +560 -0
  36. package/dist/codecs/jxl/fast-lossless.js +805 -0
  37. package/dist/codecs/jxl/features.js +325 -0
  38. package/dist/codecs/jxl/filters.js +153 -0
  39. package/dist/codecs/jxl/frame-header.js +195 -0
  40. package/dist/codecs/jxl/frame.js +319 -0
  41. package/dist/codecs/jxl/headers.js +249 -0
  42. package/dist/codecs/jxl/idct.js +484 -0
  43. package/dist/codecs/jxl/modular.js +715 -0
  44. package/dist/codecs/jxl/quant.js +219 -0
  45. package/dist/codecs/jxl/upsample.js +52 -0
  46. package/dist/codecs/jxl/vardct.js +472 -0
  47. package/dist/codecs/jxl.js +90 -0
  48. package/dist/codecs/netpbm.js +239 -0
  49. package/dist/codecs/qoi.js +150 -0
  50. package/dist/codecs/vp8-enc-dsp.js +932 -0
  51. package/dist/codecs/vp8-enc-tables.js +78 -0
  52. package/dist/codecs/vp8-enc.js +1930 -0
  53. package/dist/codecs/webp.js +36 -2
  54. package/dist/image-type.d.ts +15 -1
  55. package/dist/image-type.js +45 -2
  56. package/dist/image.d.ts +69 -6
  57. package/dist/image.js +56 -3
  58. package/dist/index.d.ts +3 -2
  59. package/dist/index.js +2 -2
  60. package/dist/render/hinting.js +26 -18
  61. package/dist/render/layout.js +1 -1
  62. package/dist/render/svg.js +1 -1
  63. package/dist/render/unicode.js +1 -9
  64. package/dist/util/bytes.js +9 -0
  65. package/dist/util/resample.js +4 -2
  66. package/dist/version.js +1 -1
  67. package/package.json +25 -2
@@ -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
+ }
@@ -0,0 +1,219 @@
1
+ /*!
2
+ * AV1 film grain synthesis, ported from dav1d (src/filmgrain_tmpl.c, src/fg_apply_tmpl.c). Copyright (c) 2018-2021,
3
+ * VideoLAN and dav1d authors, Copyright (c) 2018, Two Orioles, LLC, BSD-2-Clause License (see THIRD_PARTY_NOTICES.md)
4
+ */
5
+ import { gaussianSequence } from "./tables.js";
6
+ const GRAIN_WIDTH = 82, GRAIN_HEIGHT = 73, SUB_GRAIN_WIDTH = 44, SUB_GRAIN_HEIGHT = 38, BLOCK = 32;
7
+ class Random {
8
+ state;
9
+ constructor(state) {
10
+ this.state = state;
11
+ }
12
+ next(bits) {
13
+ const r = this.state;
14
+ const bit = (r ^ r >> 1 ^ r >> 3 ^ r >> 12) & 1;
15
+ this.state = r >> 1 | bit << 15;
16
+ return this.state >> (16 - bits) & (1 << bits) - 1;
17
+ }
18
+ }
19
+ const round2 = (x, shift) => (x + ((1 << shift) >> 1)) >> shift;
20
+ function generateGrainY(g, bitdepth) {
21
+ const gauss = gaussianSequence();
22
+ const buf = new Int16Array(GRAIN_HEIGHT * GRAIN_WIDTH);
23
+ const rnd = new Random(g.seed);
24
+ const shift = 4 - (bitdepth - 8) + g.grainScaleShift;
25
+ const ctr = 128 << (bitdepth - 8), min = -ctr, max = ctr - 1;
26
+ for (let i = 0; i < buf.length; i++)
27
+ buf[i] = round2(gauss[rnd.next(11)], shift);
28
+ const lag = g.arCoeffLag;
29
+ for (let y = 3; y < GRAIN_HEIGHT; y++) {
30
+ for (let x = 3; x < GRAIN_WIDTH - 3; x++) {
31
+ let sum = 0, c = 0;
32
+ outer: for (let dy = -lag; dy <= 0; dy++) {
33
+ for (let dx = -lag; dx <= lag; dx++) {
34
+ if (!dx && !dy)
35
+ break outer;
36
+ sum += g.arCoeffsY[c++] * buf[(y + dy) * GRAIN_WIDTH + x + dx];
37
+ }
38
+ }
39
+ const v = buf[y * GRAIN_WIDTH + x] + round2(sum, g.arCoeffShift);
40
+ buf[y * GRAIN_WIDTH + x] = v < min ? min : v > max ? max : v;
41
+ }
42
+ }
43
+ return buf;
44
+ }
45
+ function generateGrainUv(g, lumaBuf, uv, subx, suby, bitdepth) {
46
+ const gauss = gaussianSequence();
47
+ const buf = new Int16Array(GRAIN_HEIGHT * GRAIN_WIDTH);
48
+ const rnd = new Random(g.seed ^ (uv ? 0x49d8 : 0xb524));
49
+ const shift = 4 - (bitdepth - 8) + g.grainScaleShift;
50
+ const ctr = 128 << (bitdepth - 8), min = -ctr, max = ctr - 1;
51
+ const w = subx ? SUB_GRAIN_WIDTH : GRAIN_WIDTH, h = suby ? SUB_GRAIN_HEIGHT : GRAIN_HEIGHT;
52
+ for (let y = 0; y < h; y++)
53
+ for (let x = 0; x < w; x++)
54
+ buf[y * GRAIN_WIDTH + x] = round2(gauss[rnd.next(11)], shift);
55
+ const lag = g.arCoeffLag, coeffs = g.arCoeffsUv[uv];
56
+ for (let y = 3; y < h; y++) {
57
+ for (let x = 3; x < w - 3; x++) {
58
+ let sum = 0, c = 0;
59
+ outer: for (let dy = -lag; dy <= 0; dy++) {
60
+ for (let dx = -lag; dx <= lag; dx++) {
61
+ if (!dx && !dy) {
62
+ if (!g.yPoints.length)
63
+ break outer;
64
+ let luma = 0;
65
+ const lx = ((x - 3) << subx) + 3, ly = ((y - 3) << suby) + 3;
66
+ for (let i = 0; i <= suby; i++)
67
+ for (let j = 0; j <= subx; j++)
68
+ luma += lumaBuf[(ly + i) * GRAIN_WIDTH + lx + j];
69
+ sum += round2(luma, subx + suby) * coeffs[c];
70
+ break outer;
71
+ }
72
+ sum += coeffs[c++] * buf[(y + dy) * GRAIN_WIDTH + x + dx];
73
+ }
74
+ }
75
+ const v = buf[y * GRAIN_WIDTH + x] + round2(sum, g.arCoeffShift);
76
+ buf[y * GRAIN_WIDTH + x] = v < min ? min : v > max ? max : v;
77
+ }
78
+ }
79
+ return buf;
80
+ }
81
+ function generateScaling(bitdepth, points) {
82
+ const shiftX = bitdepth - 8, size = 1 << bitdepth;
83
+ const scaling = new Uint8Array(size);
84
+ const num = points.length;
85
+ if (!num)
86
+ return scaling;
87
+ scaling.fill(points[0][1], 0, points[0][0] << shiftX);
88
+ for (let i = 0; i < num - 1; i++) {
89
+ const [bx, by] = points[i], [ex, ey] = points[i + 1];
90
+ const dx = ex - bx, dy = ey - by;
91
+ const delta = dy * Math.floor((0x10000 + (dx >> 1)) / dx);
92
+ for (let x = 0, d = 0x8000; x < dx; x++) {
93
+ scaling[(bx + x) << shiftX] = by + (d >> 16);
94
+ d += delta;
95
+ }
96
+ }
97
+ const n = points[num - 1][0] << shiftX;
98
+ scaling.fill(points[num - 1][1], n, size);
99
+ if (bitdepth > 8) {
100
+ const pad = 1 << shiftX, rnd = pad >> 1;
101
+ for (let i = 0; i < num - 1; i++) {
102
+ const bx = points[i][0] << shiftX, ex = points[i + 1][0] << shiftX;
103
+ for (let x = 0; x < ex - bx; x += pad) {
104
+ const range = scaling[bx + x + pad] - scaling[bx + x];
105
+ for (let k = 1, r = rnd; k < pad; k++) {
106
+ r += range;
107
+ scaling[bx + x + k] = scaling[bx + x] + (r >> shiftX);
108
+ }
109
+ }
110
+ }
111
+ }
112
+ return scaling;
113
+ }
114
+ const W = [[[27, 17], [17, 27]], [[23, 22], [0, 0]]];
115
+ function applyStripe(g, dst, src, stride, pw, bh, rowNum, scaling, lut, bitdepth, plane, sx, sy, isId, luma, lumaStride) {
116
+ const rows = 1 + (g.overlap && rowNum > 0 ? 1 : 0);
117
+ const shift8 = bitdepth - 8, ctr = 128 << shift8, gmin = -ctr, gmax = ctr - 1, bdMax = (1 << bitdepth) - 1;
118
+ const minV = g.clipToRestrictedRange ? 16 << shift8 : 0;
119
+ const maxV = g.clipToRestrictedRange ? (plane && !isId ? 240 : 235) << shift8 : bdMax;
120
+ const clip = (v, lo, hi) => v < lo ? lo : v > hi ? hi : v;
121
+ const seeds = [];
122
+ for (let i = 0; i < rows; i++) {
123
+ let s = g.seed;
124
+ s ^= (((rowNum - i) * 37 + 178) & 0xff) << 8;
125
+ s ^= ((rowNum - i) * 173 + 105) & 0xff;
126
+ seeds.push(new Random(s));
127
+ }
128
+ const offsets = [[0, 0], [0, 0]];
129
+ const sample = (bx, by, x, y) => {
130
+ const randval = offsets[bx][by];
131
+ const offx = 3 + (2 >> sx) * (3 + (randval >> 4)), offy = 3 + (2 >> sy) * (3 + (randval & 0xf));
132
+ return lut[(offy + y + (BLOCK >> sy) * by) * GRAIN_WIDTH + offx + x + (BLOCK >> sx) * bx];
133
+ };
134
+ const y0 = rowNum * BLOCK >> sy;
135
+ const uv = plane - 1;
136
+ const add = (bx, x, y, grain) => {
137
+ const o = (y0 + y) * stride + bx + x;
138
+ let val;
139
+ if (!plane)
140
+ val = src[o];
141
+ else {
142
+ const lx = (bx + x) << sx, ly = (y0 + y) << sy;
143
+ let avg = luma[ly * lumaStride + lx];
144
+ if (sx)
145
+ avg = (avg + luma[ly * lumaStride + lx + 1] + 1) >> 1;
146
+ val = avg;
147
+ if (!g.chromaScalingFromLuma) {
148
+ const combined = avg * g.uvLumaMult[uv] + src[o] * g.uvMult[uv];
149
+ val = clip((combined >> 6) + g.uvOffset[uv] * (1 << shift8), 0, bdMax);
150
+ }
151
+ }
152
+ const noise = round2(scaling[val] * grain, g.scalingShift);
153
+ dst[o] = clip(src[o] + noise, minV, maxV);
154
+ };
155
+ const blockW = BLOCK >> sx;
156
+ for (let bx = 0; bx < pw; bx += blockW) {
157
+ const bw = Math.min(blockW, pw - bx);
158
+ if (g.overlap && bx)
159
+ for (let i = 0; i < rows; i++)
160
+ offsets[1][i] = offsets[0][i];
161
+ for (let i = 0; i < rows; i++)
162
+ offsets[0][i] = seeds[i].next(8);
163
+ const ystart = g.overlap && rowNum ? Math.min(2 >> sy, bh) : 0;
164
+ const xstart = g.overlap && bx ? Math.min(2 >> sx, bw) : 0;
165
+ const wx = W[sx], wy = W[sy];
166
+ for (let y = ystart; y < bh; y++) {
167
+ for (let x = xstart; x < bw; x++)
168
+ add(bx, x, y, sample(0, 0, x, y));
169
+ for (let x = 0; x < xstart; x++) {
170
+ const grain = clip(round2(sample(1, 0, x, y) * wx[x][0] + sample(0, 0, x, y) * wx[x][1], 5), gmin, gmax);
171
+ add(bx, x, y, grain);
172
+ }
173
+ }
174
+ for (let y = 0; y < ystart; y++) {
175
+ for (let x = xstart; x < bw; x++) {
176
+ const grain = clip(round2(sample(0, 1, x, y) * wy[y][0] + sample(0, 0, x, y) * wy[y][1], 5), gmin, gmax);
177
+ add(bx, x, y, grain);
178
+ }
179
+ for (let x = 0; x < xstart; x++) {
180
+ const top = clip(round2(sample(1, 1, x, y) * wx[x][0] + sample(0, 1, x, y) * wx[x][1], 5), gmin, gmax);
181
+ let grain = clip(round2(sample(1, 0, x, y) * wx[x][0] + sample(0, 0, x, y) * wx[x][1], 5), gmin, gmax);
182
+ grain = clip(round2(top * wy[y][0] + grain * wy[y][1], 5), gmin, gmax);
183
+ add(bx, x, y, grain);
184
+ }
185
+ }
186
+ }
187
+ }
188
+ export function applyFilmGrain(f, g) {
189
+ const bitdepth = f.bitdepth, w = f.hdr.upscaledWidth, h = f.height;
190
+ const sx = f.ssx, sy = f.ssy;
191
+ const lumaLut = generateGrainY(g, bitdepth);
192
+ const out = f.planes.map(p => p.slice());
193
+ const scalingY = g.yPoints.length || g.chromaScalingFromLuma ? generateScaling(bitdepth, g.yPoints) : new Uint8Array(1 << bitdepth);
194
+ const isId = f.seq.matrixCoefficients === 0;
195
+ const rows = Math.ceil(h / BLOCK);
196
+ if (g.yPoints.length) {
197
+ for (let row = 0; row < rows; row++) {
198
+ applyStripe(g, out[0], f.planes[0], f.strides[0], w, Math.min(h - row * BLOCK, BLOCK), row, scalingY, lumaLut, bitdepth, 0, 0, 0, isId, f.planes[0], f.strides[0]);
199
+ }
200
+ }
201
+ if (f.planes.length === 1 || !g.uvPoints[0].length && !g.uvPoints[1].length && !g.chromaScalingFromLuma)
202
+ return out;
203
+ const luma = f.planes[0], ls = f.strides[0];
204
+ if (w & sx)
205
+ for (let y = 0; y < h; y++)
206
+ luma[y * ls + w] = luma[y * ls + w - 1];
207
+ const cpw = w + sx >> sx;
208
+ for (let pl = 0; pl < 2; pl++) {
209
+ if (!g.chromaScalingFromLuma && !g.uvPoints[pl].length)
210
+ continue;
211
+ const lut = generateGrainUv(g, lumaLut, pl, sx, sy, bitdepth);
212
+ const scaling = g.chromaScalingFromLuma ? scalingY : generateScaling(bitdepth, g.uvPoints[pl]);
213
+ for (let row = 0; row < rows; row++) {
214
+ const bh = Math.min(h - row * BLOCK, BLOCK) + sy >> sy;
215
+ applyStripe(g, out[1 + pl], f.planes[1 + pl], f.strides[1], cpw, bh, row, scaling, lut, bitdepth, 1 + pl, sx, sy, isId, luma, ls);
216
+ }
217
+ }
218
+ return out;
219
+ }
@@ -0,0 +1,114 @@
1
+ /*!
2
+ * Decodes the first shown frame of an AV1 bitstream (intra frames only), following dav1d's handling of the OBUs
3
+ * (src/obu.c). Copyright (c) 2018-2021, VideoLAN and dav1d authors, BSD-2-Clause License (see THIRD_PARTY_NOTICES.md)
4
+ */
5
+ import { MAX_PIXELS, checkPixels } from "../../util/limits.js";
6
+ import { BitReader, InterFrameError, parseSequenceHeader, parseFrameHeader, obus, OBU_SEQUENCE_HEADER, OBU_TEMPORAL_DELIMITER, OBU_FRAME_HEADER, OBU_REDUNDANT_FRAME_HEADER, OBU_FRAME, OBU_TILE_GROUP } from "./obu.js";
7
+ import { Frame, TileDecoder, decodeTileGroup } from "./decode.js";
8
+ import { postFilter } from "./postfilter.js";
9
+ import { applyFilmGrain } from "./filmgrain.js";
10
+ export { Frame };
11
+ export function decodeAV1(data, start = 0, end = data.length, { maxPixels = MAX_PIXELS, filmGrain = true, operatingPoint = 0, layer = -1 } = {}) {
12
+ let seq = null;
13
+ let hdr = null;
14
+ let frame = null;
15
+ let decoder = null;
16
+ let tilesDone = 0;
17
+ let opIdc = 0, target = 0;
18
+ let best = null;
19
+ const refs = new Array(8).fill(null);
20
+ for (const obu of obus(data, start, end)) {
21
+ if (obu.type !== OBU_SEQUENCE_HEADER && obu.type !== OBU_TEMPORAL_DELIMITER && opIdc &&
22
+ (!(opIdc >> obu.temporalId & 1) || !(opIdc >> obu.spatialId + 8 & 1)))
23
+ continue;
24
+ if (obu.type === OBU_REDUNDANT_FRAME_HEADER && hdr)
25
+ continue;
26
+ const r = new BitReader(data, obu.start, obu.end);
27
+ switch (obu.type === OBU_REDUNDANT_FRAME_HEADER ? OBU_FRAME_HEADER : obu.type) {
28
+ case OBU_SEQUENCE_HEADER:
29
+ seq = parseSequenceHeader(r);
30
+ opIdc = seq.operatingPoints[Math.min(operatingPoint, seq.operatingPoints.length - 1)].idc;
31
+ target = opIdc >> 8 ? 31 - Math.clz32(opIdc >> 8) : 0;
32
+ if (layer >= 0 && layer < target)
33
+ target = layer;
34
+ break;
35
+ case OBU_FRAME_HEADER:
36
+ case OBU_FRAME: {
37
+ if (!seq)
38
+ throw new Error('AV1: frame before the sequence header');
39
+ try {
40
+ hdr = parseFrameHeader(r, seq, obu.temporalId, obu.spatialId);
41
+ }
42
+ catch (err) {
43
+ const lower = best;
44
+ if (!(err instanceof InterFrameError) || !lower)
45
+ throw err;
46
+ lower.incomplete = true;
47
+ return lower;
48
+ }
49
+ if (hdr.showExistingFrame) {
50
+ const shown = refs[hdr.existingFrameIdx];
51
+ if (!shown)
52
+ throw new Error('AV1: shows a frame that doesn\'t exist');
53
+ return output(shown);
54
+ }
55
+ checkPixels(hdr.upscaledWidth, hdr.height, maxPixels);
56
+ frame = new Frame(seq, hdr);
57
+ decoder = new TileDecoder(frame);
58
+ tilesDone = 0;
59
+ if (obu.type !== OBU_FRAME)
60
+ break;
61
+ r.align();
62
+ const shown = tileGroup(r, obu.end, obu.spatialId);
63
+ if (shown)
64
+ return shown;
65
+ break;
66
+ }
67
+ case OBU_TILE_GROUP: {
68
+ if (!hdr || !frame)
69
+ throw new Error('AV1: tile group without a frame header');
70
+ const shown = tileGroup(r, obu.end, obu.spatialId);
71
+ if (shown)
72
+ return shown;
73
+ break;
74
+ }
75
+ }
76
+ }
77
+ const lower = best;
78
+ if (lower)
79
+ return lower;
80
+ throw new Error('AV1: no frame');
81
+ function tileGroup(r, end, spatialId) {
82
+ const h = hdr, f = frame;
83
+ const count = h.tiles.cols * h.tiles.rows;
84
+ let first = 0, last = count - 1;
85
+ if (count > 1 && r.bit()) {
86
+ const bits = h.tiles.log2Cols + h.tiles.log2Rows;
87
+ first = r.bits(bits);
88
+ last = r.bits(bits);
89
+ }
90
+ r.align();
91
+ if (first !== tilesDone || last < first || last >= count)
92
+ throw new Error('AV1: invalid tile group');
93
+ decodeTileGroup(decoder, data, r.byte, end, first, last);
94
+ tilesDone = last + 1;
95
+ if (tilesDone < count)
96
+ return null;
97
+ postFilter(f);
98
+ for (let i = 0; i < 8; i++)
99
+ if (h.refreshFrameFlags >> i & 1)
100
+ refs[i] = f;
101
+ if (!h.showFrame)
102
+ return null;
103
+ if (opIdc && spatialId < target) {
104
+ best = output(f);
105
+ return null;
106
+ }
107
+ return output(f);
108
+ }
109
+ function output(f) {
110
+ if (filmGrain && f.hdr.filmGrain)
111
+ f.planes = applyFilmGrain(f, f.hdr.filmGrain);
112
+ return f;
113
+ }
114
+ }