imagescript 3.4.0 → 3.6.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/animation.d.ts +1 -1
- package/dist/animation.js +1 -1
- package/dist/codecs/av1/cdef.js +17 -3
- package/dist/codecs/av1/encode.js +1630 -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 +224 -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 +13 -0
- package/dist/codecs/png.js +29 -26
- package/dist/codecs/vp8-enc-dsp.js +20 -13
- package/dist/codecs/vp8-enc.js +35 -14
- package/dist/image.d.ts +29 -3
- package/dist/image.js +9 -1
- package/dist/metadata.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,1630 @@
|
|
|
1
|
+
import { SymbolEncoder } from "./msac-enc.js";
|
|
2
|
+
import { RESTORATION_NONE } from "./obu.js";
|
|
3
|
+
import { HeaderWriter, obu, writeSequenceHeader, writeFrameHeader, OBU_FRAME } from "./obu-enc.js";
|
|
4
|
+
import { Frame } from "./decode.js";
|
|
5
|
+
import { CDF, BLOCK_DIMENSIONS, TXFM_DIMENSIONS, MAX_TXFM_SIZE_FOR_BS, AL_PART_CTX, BLOCK_SIZES, PARTITION_TYPE_COUNT, INTRA_MODE_CONTEXT, TXTP_FROM_UVMODE, TX_TYPES_PER_SET, LO_CTX_OFFSETS, SKIP_CTX, CFL_ALLOWED_MASK, BL_64X64, BL_8X8, PARTITION_NONE, PARTITION_H, PARTITION_V, PARTITION_SPLIT, PARTITION_T_TOP_SPLIT, PARTITION_T_LEFT_SPLIT, PARTITION_T_RIGHT_SPLIT, PARTITION_H4, PARTITION_V4, DC_PRED, VERT_PRED, HOR_PRED, VERT_LEFT_PRED, SMOOTH_PRED, SMOOTH_V_PRED, SMOOTH_H_PRED, PAETH_PRED, CFL_PRED, TX_4X4, TX_64X64, TX_16X16, RTX_4X8, DCT_DCT, IDTX, WHT_WHT, EDGE_I444_TOP_HAS_RIGHT, EDGE_I444_LEFT_HAS_BOTTOM, EDGE_I420_TOP_HAS_RIGHT, EDGE_I420_LEFT_HAS_BOTTOM, edgeTree, defaultCdfs, scan, dequant } from "./tables.js";
|
|
6
|
+
import { prepareEdges, predict, preparedAngle, cflAc, cflPredict, ANGLE_USE_EDGE_FILTER, ANGLE_SMOOTH_EDGE } from "./ipred.js";
|
|
7
|
+
import { inverseTransformAdd } from "./itx.js";
|
|
8
|
+
import { forwardTransform, forwardWht, pixelEnergy } from "./fdct.js";
|
|
9
|
+
import { deblock } from "./loopfilter.js";
|
|
10
|
+
import { cdef } from "./cdef.js";
|
|
11
|
+
import { paletteColors, colorRanks, colorIndexBits } from "./palette.js";
|
|
12
|
+
const ADST_DCT = 1, DCT_ADST = 2, ADST_ADST = 3;
|
|
13
|
+
const bdim = (bs, i) => BLOCK_DIMENSIONS[bs * 4 + i];
|
|
14
|
+
const tdim = (tx, i) => TXFM_DIMENSIONS[tx * 8 + i];
|
|
15
|
+
const W = 0, H = 1, LW = 2, LH = 3, MIN = 4, MAX = 5, SUB = 6, CTX = 7;
|
|
16
|
+
const isDirectional = (mode) => mode >= VERT_PRED && mode <= VERT_LEFT_PRED;
|
|
17
|
+
const smFlag = (mode) => mode === SMOOTH_PRED || mode === SMOOTH_H_PRED || mode === SMOOTH_V_PRED ? ANGLE_SMOOTH_EDGE : 0;
|
|
18
|
+
const ZERO_BITS = 0.6;
|
|
19
|
+
const coefBits = (q) => q === 0 ? ZERO_BITS : q === 1 ? 2.2 : q === 2 ? 3.4 : q < 15 ? 4.6 + Math.log2(q - 2) : 9 + 2 * Math.log2(q - 14);
|
|
20
|
+
const SATD_BLOCK = new Int32Array(16);
|
|
21
|
+
const mostlyFew = (counts, n) => {
|
|
22
|
+
if (counts.length <= 8)
|
|
23
|
+
return true;
|
|
24
|
+
const top = counts.slice().sort((a, b) => b - a);
|
|
25
|
+
let sum = 0;
|
|
26
|
+
for (let i = 0; i < 8; i++)
|
|
27
|
+
sum += top[i];
|
|
28
|
+
return sum >= n * 0.7;
|
|
29
|
+
};
|
|
30
|
+
class TileEncoder {
|
|
31
|
+
f;
|
|
32
|
+
src;
|
|
33
|
+
params;
|
|
34
|
+
seq;
|
|
35
|
+
hdr;
|
|
36
|
+
ssx;
|
|
37
|
+
ssy;
|
|
38
|
+
cdf;
|
|
39
|
+
w = null;
|
|
40
|
+
bx = 0;
|
|
41
|
+
by = 0;
|
|
42
|
+
colStart = 0;
|
|
43
|
+
colEnd = 0;
|
|
44
|
+
rowStart = 0;
|
|
45
|
+
rowEnd = 0;
|
|
46
|
+
dq;
|
|
47
|
+
lambda;
|
|
48
|
+
aMode;
|
|
49
|
+
lMode = new Uint8Array(32);
|
|
50
|
+
aLcoef;
|
|
51
|
+
lLcoef = new Uint8Array(32);
|
|
52
|
+
aCcoef;
|
|
53
|
+
lCcoef = [new Uint8Array(32), new Uint8Array(32)];
|
|
54
|
+
aSkip;
|
|
55
|
+
lSkip = new Uint8Array(32);
|
|
56
|
+
aUvmode;
|
|
57
|
+
lUvmode = new Uint8Array(32);
|
|
58
|
+
aPartition;
|
|
59
|
+
lPartition = new Uint8Array(16);
|
|
60
|
+
aPalSz;
|
|
61
|
+
lPalSz = new Uint8Array(32);
|
|
62
|
+
aPalSzUv;
|
|
63
|
+
lPalSzUv = new Uint8Array(32);
|
|
64
|
+
aPal;
|
|
65
|
+
lPal = new Uint16Array(32 * 24);
|
|
66
|
+
aTxIntra;
|
|
67
|
+
lTxIntra = new Int8Array(32);
|
|
68
|
+
cf = new Int32Array(32 * 32);
|
|
69
|
+
coefs = new Float64Array(32 * 32);
|
|
70
|
+
q = new Int32Array(32 * 32);
|
|
71
|
+
res = new Float64Array(64 * 64);
|
|
72
|
+
levels = new Uint8Array(36 * 36);
|
|
73
|
+
rdoKeep = new Float64Array(32 * 32);
|
|
74
|
+
whtRes = new Int32Array(16);
|
|
75
|
+
whtOut = new Int32Array(16);
|
|
76
|
+
lossless;
|
|
77
|
+
cdefChoice;
|
|
78
|
+
rdoDrop = new Float64Array(32 * 32);
|
|
79
|
+
ac = new Int16Array(32 * 32);
|
|
80
|
+
pairCounts = new Uint32Array(65536);
|
|
81
|
+
bits = 0;
|
|
82
|
+
coded = false;
|
|
83
|
+
constructor(f, src, params) {
|
|
84
|
+
this.f = f;
|
|
85
|
+
this.src = src;
|
|
86
|
+
this.params = params;
|
|
87
|
+
this.seq = f.seq;
|
|
88
|
+
this.hdr = f.hdr;
|
|
89
|
+
this.ssx = f.ssx;
|
|
90
|
+
this.ssy = f.ssy;
|
|
91
|
+
const n = f.b4Stride + 32;
|
|
92
|
+
this.aMode = new Uint8Array(n);
|
|
93
|
+
this.aLcoef = new Uint8Array(n);
|
|
94
|
+
this.aCcoef = [new Uint8Array(n), new Uint8Array(n)];
|
|
95
|
+
this.aSkip = new Uint8Array(n);
|
|
96
|
+
this.aUvmode = new Uint8Array(n);
|
|
97
|
+
this.aPartition = new Uint8Array(n >> 1);
|
|
98
|
+
this.aPalSz = new Uint8Array(n);
|
|
99
|
+
this.aPalSzUv = new Uint8Array(n);
|
|
100
|
+
this.aPal = new Uint16Array(n * 24);
|
|
101
|
+
this.aTxIntra = new Int8Array(n);
|
|
102
|
+
this.cdefChoice = new Int8Array(f.cdefIdx.length);
|
|
103
|
+
this.lossless = this.hdr.allLossless;
|
|
104
|
+
const q = this.hdr.quant;
|
|
105
|
+
this.dq = [[dequant(0, q.yac, 0), dequant(0, q.yac, 1)], [dequant(0, q.yac + q.udcDelta, 0), dequant(0, q.yac + q.uacDelta, 1)], [dequant(0, q.yac + q.vdcDelta, 0), dequant(0, q.yac + q.vacDelta, 1)]];
|
|
106
|
+
const step = this.dq[0][1] / 8;
|
|
107
|
+
this.lambda = 0.08 * step * step;
|
|
108
|
+
}
|
|
109
|
+
resetAbove() {
|
|
110
|
+
this.aUvmode.fill(DC_PRED);
|
|
111
|
+
this.aMode.fill(DC_PRED);
|
|
112
|
+
this.aPartition.fill(0);
|
|
113
|
+
this.aSkip.fill(0);
|
|
114
|
+
this.aLcoef.fill(0x40);
|
|
115
|
+
this.aCcoef[0].fill(0x40);
|
|
116
|
+
this.aCcoef[1].fill(0x40);
|
|
117
|
+
this.aPalSz.fill(0);
|
|
118
|
+
this.aPalSzUv.fill(0);
|
|
119
|
+
this.aTxIntra.fill(-1);
|
|
120
|
+
}
|
|
121
|
+
resetLeft() {
|
|
122
|
+
this.lUvmode.fill(DC_PRED);
|
|
123
|
+
this.lMode.fill(DC_PRED);
|
|
124
|
+
this.lPartition.fill(0);
|
|
125
|
+
this.lSkip.fill(0);
|
|
126
|
+
this.lLcoef.fill(0x40);
|
|
127
|
+
this.lCcoef[0].fill(0x40);
|
|
128
|
+
this.lCcoef[1].fill(0x40);
|
|
129
|
+
this.lPalSz.fill(0);
|
|
130
|
+
this.lPalSzUv.fill(0);
|
|
131
|
+
this.lTxIntra.fill(-1);
|
|
132
|
+
}
|
|
133
|
+
tile(tileRow, tileCol) {
|
|
134
|
+
const f = this.f, t = this.hdr.tiles;
|
|
135
|
+
this.colStart = t.colStarts[tileCol] << 4;
|
|
136
|
+
this.colEnd = Math.min(t.colStarts[tileCol + 1] << 4, f.bw);
|
|
137
|
+
this.rowStart = t.rowStarts[tileRow] << 4;
|
|
138
|
+
this.rowEnd = Math.min(t.rowStarts[tileRow + 1] << 4, f.bh);
|
|
139
|
+
}
|
|
140
|
+
searchTile(tileRow, tileCol) {
|
|
141
|
+
this.tile(tileRow, tileCol);
|
|
142
|
+
const root = edgeTree(false), nodes = [];
|
|
143
|
+
this.resetAbove();
|
|
144
|
+
for (this.by = this.rowStart; this.by < this.rowEnd; this.by += 16) {
|
|
145
|
+
this.resetLeft();
|
|
146
|
+
for (this.bx = this.colStart; this.bx < this.colEnd; this.bx += 16)
|
|
147
|
+
nodes.push(this.search(BL_64X64, root).node);
|
|
148
|
+
}
|
|
149
|
+
return nodes;
|
|
150
|
+
}
|
|
151
|
+
storeTile(tileRow, tileCol, nodes) {
|
|
152
|
+
this.tile(tileRow, tileCol);
|
|
153
|
+
let i = 0;
|
|
154
|
+
for (this.by = this.rowStart; this.by < this.rowEnd; this.by += 16) {
|
|
155
|
+
for (this.bx = this.colStart; this.bx < this.colEnd; this.bx += 16)
|
|
156
|
+
this.walk(BL_64X64, nodes[i++]);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
walk(bl, n) {
|
|
160
|
+
const f = this.f, hsz = 16 >> bl;
|
|
161
|
+
if (n.bp === PARTITION_NONE) {
|
|
162
|
+
const b = n.b;
|
|
163
|
+
this.storeBlock(BLOCK_SIZES[(bl * 10 + PARTITION_NONE) * 2], b);
|
|
164
|
+
if (!b.skip)
|
|
165
|
+
f.cdefIdx[(this.by >> 4) * (f.b4Stride >> 4) + (this.bx >> 4)] = 0;
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (n.bp === PARTITION_H || n.bp === PARTITION_V) {
|
|
169
|
+
const bs = BLOCK_SIZES[(bl * 10 + n.bp) * 2], dx = n.bp === PARTITION_V ? hsz : 0, dy = n.bp === PARTITION_H ? hsz : 0;
|
|
170
|
+
for (let k = 0; k < 2; k++) {
|
|
171
|
+
const b = n.kids[k].b;
|
|
172
|
+
this.storeBlock(bs, b);
|
|
173
|
+
if (!b.skip)
|
|
174
|
+
f.cdefIdx[(this.by >> 4) * (f.b4Stride >> 4) + (this.bx >> 4)] = 0;
|
|
175
|
+
this.bx += dx;
|
|
176
|
+
this.by += dy;
|
|
177
|
+
}
|
|
178
|
+
this.bx -= 2 * dx;
|
|
179
|
+
this.by -= 2 * dy;
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
for (let k = 0; k < 4; k++) {
|
|
183
|
+
if (!n.kids[k])
|
|
184
|
+
continue;
|
|
185
|
+
const dx = k & 1 ? hsz : 0, dy = k & 2 ? hsz : 0;
|
|
186
|
+
this.bx += dx;
|
|
187
|
+
this.by += dy;
|
|
188
|
+
this.walk(bl + 1, n.kids[k]);
|
|
189
|
+
this.bx -= dx;
|
|
190
|
+
this.by -= dy;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
writeTile(tileRow, tileCol, nodes) {
|
|
194
|
+
this.tile(tileRow, tileCol);
|
|
195
|
+
const root = edgeTree(false);
|
|
196
|
+
this.cdf = defaultCdfs(this.hdr.quant.yac);
|
|
197
|
+
this.w = new SymbolEncoder(!this.hdr.disableCdfUpdate);
|
|
198
|
+
this.resetAbove();
|
|
199
|
+
let i = 0;
|
|
200
|
+
for (this.by = this.rowStart; this.by < this.rowEnd; this.by += 16) {
|
|
201
|
+
this.resetLeft();
|
|
202
|
+
for (this.bx = this.colStart; this.bx < this.colEnd; this.bx += 16) {
|
|
203
|
+
this.writeSb(BL_64X64, root, nodes[i++]);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const data = this.w.finish();
|
|
207
|
+
this.w = null;
|
|
208
|
+
return data;
|
|
209
|
+
}
|
|
210
|
+
search(bl, node) {
|
|
211
|
+
const f = this.f, hsz = 16 >> bl;
|
|
212
|
+
const haveH = f.bw > this.bx + hsz, haveV = f.bh > this.by + hsz;
|
|
213
|
+
const split = (quarters) => {
|
|
214
|
+
const kids = [null, null, null, null];
|
|
215
|
+
let cost = 0;
|
|
216
|
+
for (const k of quarters) {
|
|
217
|
+
const dx = k & 1 ? hsz : 0, dy = k & 2 ? hsz : 0;
|
|
218
|
+
this.bx += dx;
|
|
219
|
+
this.by += dy;
|
|
220
|
+
const r = this.search(bl + 1, node.split[k]);
|
|
221
|
+
this.bx -= dx;
|
|
222
|
+
this.by -= dy;
|
|
223
|
+
kids[k] = r.node;
|
|
224
|
+
cost += r.cost;
|
|
225
|
+
}
|
|
226
|
+
return { node: { bp: PARTITION_SPLIT, b: null, kids }, cost };
|
|
227
|
+
};
|
|
228
|
+
if (!haveH && !haveV)
|
|
229
|
+
return split([0]);
|
|
230
|
+
if (!haveH)
|
|
231
|
+
return split([0, 2]);
|
|
232
|
+
if (!haveV)
|
|
233
|
+
return split([0, 1]);
|
|
234
|
+
const bs = BLOCK_SIZES[(bl * 10 + PARTITION_NONE) * 2];
|
|
235
|
+
if (bl === BL_8X8) {
|
|
236
|
+
const r = this.searchBlock(bs, node.o);
|
|
237
|
+
return { node: { bp: PARTITION_NONE, b: r.b, kids: [] }, cost: r.cost };
|
|
238
|
+
}
|
|
239
|
+
const saved = this.saveArea(bl), entryContexts = this.saveContexts(bs);
|
|
240
|
+
let result, noneCost, splitCost;
|
|
241
|
+
if (bl <= BL_64X64 + 1) {
|
|
242
|
+
const sp = split([0, 1, 2, 3]);
|
|
243
|
+
const deeper = sp.node.kids.filter(k => k && k.bp === PARTITION_SPLIT).length;
|
|
244
|
+
if (this.params.effort < 2 && deeper >= (bl === BL_64X64 ? 2 : 4))
|
|
245
|
+
return { node: sp.node, cost: sp.cost + this.lambda * 2 };
|
|
246
|
+
const spArea = this.saveArea(bl), contexts = this.saveContexts(bs);
|
|
247
|
+
this.restoreArea(bl, saved);
|
|
248
|
+
const none = this.searchBlock(bs, node.o);
|
|
249
|
+
noneCost = none.cost + this.lambda;
|
|
250
|
+
splitCost = sp.cost + this.lambda * 2;
|
|
251
|
+
if (noneCost < splitCost)
|
|
252
|
+
result = { node: { bp: PARTITION_NONE, b: none.b, kids: [] }, cost: noneCost };
|
|
253
|
+
else {
|
|
254
|
+
this.restoreArea(bl, spArea);
|
|
255
|
+
this.restoreContexts(contexts);
|
|
256
|
+
result = { node: sp.node, cost: splitCost };
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
const none = this.searchBlock(bs, node.o);
|
|
261
|
+
if (none.b.skip && this.params.effort < 2)
|
|
262
|
+
return { node: { bp: PARTITION_NONE, b: none.b, kids: [] }, cost: none.cost + this.lambda };
|
|
263
|
+
const noneArea = this.saveArea(bl);
|
|
264
|
+
this.restoreArea(bl, saved);
|
|
265
|
+
const sp = split([0, 1, 2, 3]);
|
|
266
|
+
noneCost = none.cost + this.lambda;
|
|
267
|
+
splitCost = sp.cost + this.lambda * 2;
|
|
268
|
+
if (noneCost < splitCost) {
|
|
269
|
+
this.restoreArea(bl, noneArea);
|
|
270
|
+
this.modeContexts(bs, none.b);
|
|
271
|
+
result = { node: { bp: PARTITION_NONE, b: none.b, kids: [] }, cost: noneCost };
|
|
272
|
+
}
|
|
273
|
+
else
|
|
274
|
+
result = { node: sp.node, cost: splitCost };
|
|
275
|
+
}
|
|
276
|
+
const ratio = noneCost / splitCost, deeper = result.node.bp === PARTITION_SPLIT ? result.node.kids.filter(k => k && k.bp !== PARTITION_NONE).length : 0;
|
|
277
|
+
if (this.params.effort && ratio >= 0.9 && ratio < 1.3 && deeper <= 1) {
|
|
278
|
+
let bestArea = this.saveArea(bl), bestContexts = this.saveContexts(bs);
|
|
279
|
+
for (const bp of this.params.effort > 1 ? [PARTITION_H, PARTITION_V] : [this.halvesDirection(bs)]) {
|
|
280
|
+
this.restoreArea(bl, saved);
|
|
281
|
+
this.restoreContexts(entryContexts);
|
|
282
|
+
const r = this.halves(bl, bp, node);
|
|
283
|
+
if (r.cost < result.cost) {
|
|
284
|
+
result = r;
|
|
285
|
+
bestArea = this.saveArea(bl);
|
|
286
|
+
bestContexts = this.saveContexts(bs);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
this.restoreArea(bl, bestArea);
|
|
290
|
+
this.restoreContexts(bestContexts);
|
|
291
|
+
}
|
|
292
|
+
return result;
|
|
293
|
+
}
|
|
294
|
+
halvesDirection(bs) {
|
|
295
|
+
const f = this.f, stride = f.strides[0], src = this.src[0];
|
|
296
|
+
const w = Math.min(bdim(bs, 0), f.bw - this.bx) * 4, h = Math.min(bdim(bs, 1), f.bh - this.by) * 4, o = this.by * 4 * stride + this.bx * 4;
|
|
297
|
+
let top = 0, left = 0, all = 0;
|
|
298
|
+
for (let y = 0; y < h; y++) {
|
|
299
|
+
for (let x = 0; x < w; x++) {
|
|
300
|
+
const v = src[o + y * stride + x];
|
|
301
|
+
all += v;
|
|
302
|
+
if (y < h >> 1)
|
|
303
|
+
top += v;
|
|
304
|
+
if (x < w >> 1)
|
|
305
|
+
left += v;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return Math.abs(2 * top - all) >= Math.abs(2 * left - all) ? PARTITION_H : PARTITION_V;
|
|
309
|
+
}
|
|
310
|
+
halves(bl, bp, node) {
|
|
311
|
+
const hsz = 16 >> bl, bs = BLOCK_SIZES[(bl * 10 + bp) * 2], edges = bp === PARTITION_H ? node.h : node.v;
|
|
312
|
+
const dx = bp === PARTITION_V ? hsz : 0, dy = bp === PARTITION_H ? hsz : 0;
|
|
313
|
+
const first = this.searchBlock(bs, edges[0]);
|
|
314
|
+
this.bx += dx;
|
|
315
|
+
this.by += dy;
|
|
316
|
+
const second = this.searchBlock(bs, edges[1]);
|
|
317
|
+
this.bx -= dx;
|
|
318
|
+
this.by -= dy;
|
|
319
|
+
const leaf = (b) => ({ bp: PARTITION_NONE, b, kids: [] });
|
|
320
|
+
return { node: { bp, b: null, kids: [leaf(first.b), leaf(second.b)] }, cost: first.cost + second.cost + this.lambda * 3 };
|
|
321
|
+
}
|
|
322
|
+
saveArea(bl) {
|
|
323
|
+
const f = this.f, n = 64 >> (bl - 1);
|
|
324
|
+
return f.planes.map((plane, p) => {
|
|
325
|
+
const sx = p ? this.ssx : 0, sy = p ? this.ssy : 0, stride = f.strides[p];
|
|
326
|
+
const w = n >> sx, h = n >> sy, x0 = this.bx * 4 >> sx, y0 = this.by * 4 >> sy, out = new Uint16Array(w * h);
|
|
327
|
+
for (let y = 0; y < h; y++)
|
|
328
|
+
out.set(plane.subarray((y0 + y) * stride + x0, (y0 + y) * stride + x0 + w), y * w);
|
|
329
|
+
return out;
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
restoreArea(bl, saved) {
|
|
333
|
+
const f = this.f, n = 64 >> (bl - 1);
|
|
334
|
+
f.planes.forEach((plane, p) => {
|
|
335
|
+
const sx = p ? this.ssx : 0, sy = p ? this.ssy : 0, stride = f.strides[p];
|
|
336
|
+
const w = n >> sx, h = n >> sy, x0 = this.bx * 4 >> sx, y0 = this.by * 4 >> sy;
|
|
337
|
+
for (let y = 0; y < h; y++)
|
|
338
|
+
plane.set(saved[p].subarray(y * w, y * w + w), (y0 + y) * stride + x0);
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
searchBlock(bs, edgeFlags) {
|
|
342
|
+
const f = this.f, layout = f.layout;
|
|
343
|
+
const lw4 = bdim(bs, 2), lh4 = bdim(bs, 3);
|
|
344
|
+
const b = {
|
|
345
|
+
skip: 0, yMode: DC_PRED, yAngle: 0, uvMode: DC_PRED, uvAngle: 0, cflAlpha: [0, 0],
|
|
346
|
+
tx: this.lossless ? TX_4X4 : MAX_TXFM_SIZE_FOR_BS[bs * 4], txtp: this.lossless ? WHT_WHT : DCT_DCT,
|
|
347
|
+
uvtx: this.lossless ? TX_4X4 : MAX_TXFM_SIZE_FOR_BS[bs * 4 + layout],
|
|
348
|
+
palSz: [0, 0], pal: new Uint16Array(24), palIdx: null, palIdxUv: null
|
|
349
|
+
};
|
|
350
|
+
const palAllowed = this.hdr.allowScreenContentTools && Math.max(bdim(bs, 0), bdim(bs, 1)) <= 16 && bdim(bs, 0) + bdim(bs, 1) >= 4;
|
|
351
|
+
const lambda = this.lambda, sadLambda = Math.sqrt(lambda);
|
|
352
|
+
const angles = lw4 + lh4 >= 2 && this.params.effort > 1 ? [-3, -2, -1, 0, 1, 2, 3] : [0];
|
|
353
|
+
const candidates = [];
|
|
354
|
+
for (let mode = DC_PRED; mode <= PAETH_PRED; mode++) {
|
|
355
|
+
for (const angle of isDirectional(mode) ? angles : [0]) {
|
|
356
|
+
b.yMode = mode;
|
|
357
|
+
b.yAngle = angle;
|
|
358
|
+
this.predictLuma(bs, edgeFlags, b);
|
|
359
|
+
candidates.push({ mode, angle, est: this.satd(0, bs) + sadLambda * (mode === DC_PRED ? 2 : 4.5 + (isDirectional(mode) && lw4 + lh4 >= 2 ? 1.5 : 0)) });
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const directional = candidates.filter(c => isDirectional(c.mode)).sort((x, y) => x.est - y.est)[0];
|
|
363
|
+
if (this.params.effort === 1 && lw4 + lh4 >= 2 && directional && directional.est <= 1.2 * Math.min(...candidates.map(c => c.est))) {
|
|
364
|
+
for (const angle of [-3, -2, -1, 1, 2, 3]) {
|
|
365
|
+
b.yMode = directional.mode;
|
|
366
|
+
b.yAngle = angle;
|
|
367
|
+
this.predictLuma(bs, edgeFlags, b);
|
|
368
|
+
candidates.push({ mode: directional.mode, angle, est: this.satd(0, bs) + sadLambda * 6 });
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
candidates.sort((x, y) => x.est - y.est);
|
|
372
|
+
let best = Infinity, bestMode = candidates[0], dist = 0, bits = 0, coded = false, kept = null, bestTx = DCT_DCT;
|
|
373
|
+
const close = this.params.effort < 2 ? 1.1 : Infinity;
|
|
374
|
+
const tries = candidates.slice(0, [1, 2, 3][this.params.effort]).filter(c => c.est <= candidates[0].est * close);
|
|
375
|
+
for (const [i, c] of tries.entries()) {
|
|
376
|
+
b.yMode = c.mode;
|
|
377
|
+
b.yAngle = c.angle;
|
|
378
|
+
const d = this.codeLuma(bs, edgeFlags, b);
|
|
379
|
+
const cost = d + lambda * (this.bits + (c.mode === DC_PRED ? 2 : 4.5));
|
|
380
|
+
if (cost < best) {
|
|
381
|
+
best = cost;
|
|
382
|
+
bestMode = c;
|
|
383
|
+
dist = d;
|
|
384
|
+
bits = this.bits;
|
|
385
|
+
coded = this.coded;
|
|
386
|
+
if (i < tries.length - 1)
|
|
387
|
+
kept = this.savePlane(0, bs);
|
|
388
|
+
else
|
|
389
|
+
kept = null;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
b.yMode = bestMode.mode;
|
|
393
|
+
b.yAngle = bestMode.angle;
|
|
394
|
+
if (kept)
|
|
395
|
+
this.restorePlane(0, bs, kept);
|
|
396
|
+
if (this.hdr.txMode === 2 && tdim(b.tx, MAX) > TX_4X4) {
|
|
397
|
+
const modeBits = b.yMode === DC_PRED ? 2 : 4.5, maxDepth = Math.min(tdim(b.tx, MAX), 2);
|
|
398
|
+
let keptDepth = null, bestDepthCost = best + lambda, bestTxSize = b.tx;
|
|
399
|
+
for (let depth = 1, tx = b.tx; depth <= maxDepth; depth++) {
|
|
400
|
+
tx = tdim(tx, SUB);
|
|
401
|
+
if (!keptDepth)
|
|
402
|
+
keptDepth = this.savePlane(0, bs);
|
|
403
|
+
b.tx = tx;
|
|
404
|
+
const d = this.codeLuma(bs, edgeFlags, b);
|
|
405
|
+
const cost = d + lambda * (this.bits + modeBits + 2 + depth);
|
|
406
|
+
if (cost < bestDepthCost) {
|
|
407
|
+
bestDepthCost = best = cost;
|
|
408
|
+
dist = d;
|
|
409
|
+
bits = this.bits + 1;
|
|
410
|
+
coded = this.coded;
|
|
411
|
+
bestTxSize = tx;
|
|
412
|
+
if (depth < maxDepth)
|
|
413
|
+
keptDepth = this.savePlane(0, bs);
|
|
414
|
+
}
|
|
415
|
+
else if (depth === maxDepth && keptDepth)
|
|
416
|
+
this.restorePlane(0, bs, keptDepth);
|
|
417
|
+
}
|
|
418
|
+
b.tx = bestTxSize;
|
|
419
|
+
}
|
|
420
|
+
if (tdim(b.tx, MAX) <= TX_16X16 && this.params.effort && coded && !this.lossless) {
|
|
421
|
+
let keptTx = this.savePlane(0, bs), bestTxCost = best + lambda;
|
|
422
|
+
for (const txtp of [ADST_ADST, ADST_DCT, DCT_ADST, IDTX]) {
|
|
423
|
+
b.txtp = txtp;
|
|
424
|
+
const d = this.codeLuma(bs, edgeFlags, b);
|
|
425
|
+
const cost = d + lambda * (this.bits + 2.5 + (b.yMode === DC_PRED ? 2 : 4.5));
|
|
426
|
+
if (cost < bestTxCost) {
|
|
427
|
+
bestTxCost = cost;
|
|
428
|
+
dist = d;
|
|
429
|
+
bits = this.bits + 1.5;
|
|
430
|
+
coded = this.coded;
|
|
431
|
+
keptTx = this.savePlane(0, bs);
|
|
432
|
+
bestTx = txtp;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
b.txtp = bestTx;
|
|
436
|
+
this.restorePlane(0, bs, keptTx);
|
|
437
|
+
}
|
|
438
|
+
if (palAllowed) {
|
|
439
|
+
const r = this.paletteLuma(bs, edgeFlags, b, dist + lambda * (bits + (b.yMode === DC_PRED ? 2 : 4.5)));
|
|
440
|
+
if (r) {
|
|
441
|
+
dist = r.dist;
|
|
442
|
+
bits = r.bits;
|
|
443
|
+
coded = r.coded;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (layout) {
|
|
447
|
+
const cflAllowed = this.cflAllowed(bs);
|
|
448
|
+
const uvCandidates = [];
|
|
449
|
+
for (let mode = DC_PRED; mode <= (cflAllowed ? CFL_PRED : PAETH_PRED); mode++) {
|
|
450
|
+
if (mode === CFL_PRED) {
|
|
451
|
+
const alpha = this.cflAlphas(bs, b);
|
|
452
|
+
if (!alpha[0] && !alpha[1])
|
|
453
|
+
continue;
|
|
454
|
+
b.uvMode = CFL_PRED;
|
|
455
|
+
b.cflAlpha = alpha;
|
|
456
|
+
this.predictChroma(bs, edgeFlags, b);
|
|
457
|
+
uvCandidates.push({ mode, angle: 0, alpha, est: this.satd(1, bs) + this.satd(2, bs) + sadLambda * 7 });
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if (this.params.effort < 2 && mode !== b.yMode && mode !== DC_PRED && mode !== VERT_PRED && mode !== HOR_PRED && mode !== SMOOTH_PRED && mode !== PAETH_PRED)
|
|
461
|
+
continue;
|
|
462
|
+
b.uvMode = mode;
|
|
463
|
+
b.uvAngle = 0;
|
|
464
|
+
this.predictChroma(bs, edgeFlags, b);
|
|
465
|
+
uvCandidates.push({ mode, angle: 0, alpha: [0, 0], est: this.satd(1, bs) + this.satd(2, bs) + sadLambda * (mode === DC_PRED ? 1.5 : 4) });
|
|
466
|
+
}
|
|
467
|
+
const uvDirectional = uvCandidates.filter(c => isDirectional(c.mode)).sort((x, y) => x.est - y.est)[0];
|
|
468
|
+
const uvNear = this.params.effort < 2 ? 1.2 : Infinity;
|
|
469
|
+
if (this.params.effort && lw4 + lh4 >= 2 && uvDirectional && uvDirectional.est <= uvNear * Math.min(...uvCandidates.map(c => c.est))) {
|
|
470
|
+
for (const angle of [-3, -2, -1, 1, 2, 3]) {
|
|
471
|
+
b.uvMode = uvDirectional.mode;
|
|
472
|
+
b.uvAngle = angle;
|
|
473
|
+
this.predictChroma(bs, edgeFlags, b);
|
|
474
|
+
uvCandidates.push({ mode: uvDirectional.mode, angle, alpha: [0, 0], est: this.satd(1, bs) + this.satd(2, bs) + sadLambda * 5.5 });
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
uvCandidates.sort((x, y) => x.est - y.est);
|
|
478
|
+
let bestUv = Infinity, bestUvMode = uvCandidates[0], uvDist = 0, uvBits = 0, uvCoded = false, keptUv = null;
|
|
479
|
+
const uvClose = this.params.effort < 2 ? 1.2 : Infinity;
|
|
480
|
+
const uvTries = uvCandidates.slice(0, [1, 2, 2][this.params.effort]).filter(c => c.est <= uvCandidates[0].est * uvClose);
|
|
481
|
+
for (const [i, c] of uvTries.entries()) {
|
|
482
|
+
b.uvMode = c.mode;
|
|
483
|
+
b.uvAngle = c.angle;
|
|
484
|
+
b.cflAlpha = c.alpha;
|
|
485
|
+
const d = this.codeChroma(bs, edgeFlags, b);
|
|
486
|
+
const cost = d + lambda * (this.bits + (c.mode === DC_PRED ? 1.5 : 4));
|
|
487
|
+
if (cost < bestUv) {
|
|
488
|
+
bestUv = cost;
|
|
489
|
+
bestUvMode = c;
|
|
490
|
+
uvDist = d;
|
|
491
|
+
uvBits = this.bits;
|
|
492
|
+
uvCoded = this.coded;
|
|
493
|
+
keptUv = i < uvTries.length - 1 ? [this.savePlane(1, bs), this.savePlane(2, bs)] : null;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
b.uvMode = bestUvMode.mode;
|
|
497
|
+
b.uvAngle = bestUvMode.angle;
|
|
498
|
+
b.cflAlpha = bestUvMode.alpha;
|
|
499
|
+
if (keptUv) {
|
|
500
|
+
this.restorePlane(1, bs, keptUv[0]);
|
|
501
|
+
this.restorePlane(2, bs, keptUv[1]);
|
|
502
|
+
}
|
|
503
|
+
if (palAllowed) {
|
|
504
|
+
const r = this.paletteChroma(bs, edgeFlags, b, bestUv);
|
|
505
|
+
if (r) {
|
|
506
|
+
uvDist = r.dist;
|
|
507
|
+
uvBits = r.bits;
|
|
508
|
+
uvCoded = r.coded;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
dist += uvDist;
|
|
512
|
+
bits += uvBits;
|
|
513
|
+
coded ||= uvCoded;
|
|
514
|
+
}
|
|
515
|
+
b.skip = coded ? 0 : 1;
|
|
516
|
+
this.modeContexts(bs, b);
|
|
517
|
+
return { b, cost: dist + lambda * (bits + 1 + 3) };
|
|
518
|
+
}
|
|
519
|
+
paletteLuma(bs, edgeFlags, b, cost) {
|
|
520
|
+
const f = this.f, stride = f.strides[0], src = this.src[0];
|
|
521
|
+
const bw = bdim(bs, 0) * 4, bh = bdim(bs, 1) * 4;
|
|
522
|
+
const w = Math.min(bw, (f.bw - this.bx) * 4), h = Math.min(bh, (f.bh - this.by) * 4), o = this.by * 4 * stride + this.bx * 4;
|
|
523
|
+
const counts = new Uint32Array(256);
|
|
524
|
+
for (let y = 0; y < h; y++)
|
|
525
|
+
for (let x = 0; x < w; x++)
|
|
526
|
+
counts[src[o + y * stride + x]]++;
|
|
527
|
+
const values = [], weights = [];
|
|
528
|
+
for (let v = 0; v < 256; v++)
|
|
529
|
+
if (counts[v]) {
|
|
530
|
+
values.push([v]);
|
|
531
|
+
weights.push(counts[v]);
|
|
532
|
+
}
|
|
533
|
+
if (values.length < 2 || values.length > (this.params.effort ? 64 : 8) || !mostlyFew(weights, w * h))
|
|
534
|
+
return null;
|
|
535
|
+
if (this.lambda * (15 + 5 * Math.min(values.length - 1, 7) + 0.08 * w * h) >= cost)
|
|
536
|
+
return null;
|
|
537
|
+
const colors = paletteColors(values, weights, 8).map(c => c[0]), k = colors.length;
|
|
538
|
+
if (k < 2)
|
|
539
|
+
return null;
|
|
540
|
+
const idx = new Uint8Array(bw * bh);
|
|
541
|
+
for (let y = 0; y < bh; y++) {
|
|
542
|
+
for (let x = 0; x < bw; x++) {
|
|
543
|
+
const v = src[o + Math.min(y, h - 1) * stride + Math.min(x, w - 1)];
|
|
544
|
+
let best = 0;
|
|
545
|
+
for (let c = 1; c < k; c++)
|
|
546
|
+
if (Math.abs(colors[c] - v) < Math.abs(colors[best] - v))
|
|
547
|
+
best = c;
|
|
548
|
+
idx[y * bw + x] = best;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
let maxDelta = 0;
|
|
552
|
+
for (let c = 1; c < k; c++)
|
|
553
|
+
maxDelta = Math.max(maxDelta, colors[c] - colors[c - 1] - 1);
|
|
554
|
+
const overhead = 3 + 10 + (k - 1) * Math.max(5, 32 - Math.clz32(maxDelta)) + colorIndexBits(idx, bw, w, h, k);
|
|
555
|
+
if (this.lambda * (overhead + 2) >= cost)
|
|
556
|
+
return null;
|
|
557
|
+
const kept = this.savePlane(0, bs), mode = [b.yMode, b.yAngle, b.txtp];
|
|
558
|
+
b.yMode = DC_PRED;
|
|
559
|
+
b.yAngle = 0;
|
|
560
|
+
b.palSz[0] = k;
|
|
561
|
+
b.pal.set(colors);
|
|
562
|
+
b.palIdx = idx;
|
|
563
|
+
let best = null, bestCost = cost;
|
|
564
|
+
const txtps = tdim(b.tx, MAX) <= TX_16X16 && this.params.effort && !this.lossless ? [DCT_DCT, IDTX] : [this.lossless ? WHT_WHT : DCT_DCT];
|
|
565
|
+
let keptPal = null, bestTx = DCT_DCT;
|
|
566
|
+
for (const txtp of txtps) {
|
|
567
|
+
b.txtp = txtp;
|
|
568
|
+
const d = this.codeLuma(bs, edgeFlags, b);
|
|
569
|
+
const bits = this.bits + overhead + (txtp === DCT_DCT || this.lossless ? 0 : 1.5);
|
|
570
|
+
const c = d + this.lambda * (bits + 2);
|
|
571
|
+
if (c < bestCost) {
|
|
572
|
+
bestCost = c;
|
|
573
|
+
best = { dist: d, bits, coded: this.coded };
|
|
574
|
+
bestTx = txtp;
|
|
575
|
+
keptPal = txtps.length > 1 && txtp !== txtps[txtps.length - 1] ? this.savePlane(0, bs) : null;
|
|
576
|
+
}
|
|
577
|
+
else if (best && keptPal)
|
|
578
|
+
this.restorePlane(0, bs, keptPal);
|
|
579
|
+
}
|
|
580
|
+
if (best) {
|
|
581
|
+
b.txtp = bestTx;
|
|
582
|
+
return best;
|
|
583
|
+
}
|
|
584
|
+
[b.yMode, b.yAngle, b.txtp] = mode;
|
|
585
|
+
b.palSz[0] = 0;
|
|
586
|
+
b.palIdx = null;
|
|
587
|
+
this.restorePlane(0, bs, kept);
|
|
588
|
+
return null;
|
|
589
|
+
}
|
|
590
|
+
paletteChroma(bs, edgeFlags, b, cost) {
|
|
591
|
+
const f = this.f, ssx = this.ssx, ssy = this.ssy, stride = f.strides[1], su = this.src[1], sv = this.src[2];
|
|
592
|
+
const w4 = Math.min(bdim(bs, 0), f.bw - this.bx), h4 = Math.min(bdim(bs, 1), f.bh - this.by);
|
|
593
|
+
const bw = (bdim(bs, 0) + ssx >> ssx) * 4, bh = (bdim(bs, 1) + ssy >> ssy) * 4;
|
|
594
|
+
const w = (w4 + ssx >> ssx) * 4, h = (h4 + ssy >> ssy) * 4, o = (this.by * 4 >> ssy) * stride + (this.bx * 4 >> ssx);
|
|
595
|
+
const counts = this.pairCounts, keys = [], limit = this.params.effort ? 64 : 8;
|
|
596
|
+
for (let y = 0; y < h && keys.length <= limit; y++) {
|
|
597
|
+
for (let x = 0; x < w; x++) {
|
|
598
|
+
const key = su[o + y * stride + x] << 8 | sv[o + y * stride + x];
|
|
599
|
+
if (!counts[key]++)
|
|
600
|
+
keys.push(key);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
const weights = keys.map(key => counts[key]);
|
|
604
|
+
for (const key of keys)
|
|
605
|
+
counts[key] = 0;
|
|
606
|
+
if (keys.length < 2 || keys.length > limit || !mostlyFew(weights, w * h))
|
|
607
|
+
return null;
|
|
608
|
+
if (this.lambda * (13.5 + 13 * Math.min(keys.length - 1, 7) + 8 + 0.08 * w * h) >= cost)
|
|
609
|
+
return null;
|
|
610
|
+
const colors = paletteColors(keys.map(key => [key >> 8, key & 255]), weights, 8), k = colors.length;
|
|
611
|
+
if (k < 2)
|
|
612
|
+
return null;
|
|
613
|
+
const idx = new Uint8Array(bw * bh);
|
|
614
|
+
for (let y = 0; y < bh; y++) {
|
|
615
|
+
for (let x = 0; x < bw; x++) {
|
|
616
|
+
const p = o + Math.min(y, h - 1) * stride + Math.min(x, w - 1), u = su[p], v = sv[p];
|
|
617
|
+
let best = 0, bestD = Infinity;
|
|
618
|
+
for (let c = 0; c < k; c++) {
|
|
619
|
+
const d = (colors[c][0] - u) ** 2 + (colors[c][1] - v) ** 2;
|
|
620
|
+
if (d < bestD) {
|
|
621
|
+
bestD = d;
|
|
622
|
+
best = c;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
idx[y * bw + x] = best;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
let maxDelta = 0;
|
|
629
|
+
for (let c = 1; c < k; c++)
|
|
630
|
+
maxDelta = Math.max(maxDelta, colors[c][0] - colors[c - 1][0]);
|
|
631
|
+
const overhead = 1 + 2 + 10 + (k - 1) * Math.max(5, 32 - Math.clz32(maxDelta)) + 8 * k + colorIndexBits(idx, bw, w, h, k);
|
|
632
|
+
if (this.lambda * (overhead + 1.5) >= cost)
|
|
633
|
+
return null;
|
|
634
|
+
const kept = [this.savePlane(1, bs), this.savePlane(2, bs)], mode = [b.uvMode, b.uvAngle], alpha = b.cflAlpha;
|
|
635
|
+
b.uvMode = DC_PRED;
|
|
636
|
+
b.uvAngle = 0;
|
|
637
|
+
b.cflAlpha = [0, 0];
|
|
638
|
+
b.palSz[1] = k;
|
|
639
|
+
for (let c = 0; c < k; c++) {
|
|
640
|
+
b.pal[8 + c] = colors[c][0];
|
|
641
|
+
b.pal[16 + c] = colors[c][1];
|
|
642
|
+
}
|
|
643
|
+
b.palIdxUv = idx;
|
|
644
|
+
const d = this.codeChroma(bs, edgeFlags, b), bits = this.bits + overhead;
|
|
645
|
+
if (d + this.lambda * (bits + 1.5) < cost)
|
|
646
|
+
return { dist: d, bits, coded: this.coded };
|
|
647
|
+
[b.uvMode, b.uvAngle] = mode;
|
|
648
|
+
b.cflAlpha = alpha;
|
|
649
|
+
b.palSz[1] = 0;
|
|
650
|
+
b.palIdxUv = null;
|
|
651
|
+
this.restorePlane(1, bs, kept[0]);
|
|
652
|
+
this.restorePlane(2, bs, kept[1]);
|
|
653
|
+
return null;
|
|
654
|
+
}
|
|
655
|
+
modeContexts(bs, b) {
|
|
656
|
+
const bx4 = this.bx, by4 = this.by & 31, aw = 1 << bdim(bs, 2), lh = 1 << bdim(bs, 3);
|
|
657
|
+
this.aMode.fill(b.yMode, bx4, bx4 + aw);
|
|
658
|
+
this.lMode.fill(b.yMode, by4, by4 + lh);
|
|
659
|
+
if (this.f.layout) {
|
|
660
|
+
const cbx4 = bx4 >> this.ssx, cby4 = by4 >> this.ssy;
|
|
661
|
+
this.aUvmode.fill(b.uvMode, cbx4, cbx4 + (bdim(bs, 0) + this.ssx >> this.ssx));
|
|
662
|
+
this.lUvmode.fill(b.uvMode, cby4, cby4 + (bdim(bs, 1) + this.ssy >> this.ssy));
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
saveContexts(bs) {
|
|
666
|
+
const bx4 = this.bx, by4 = this.by & 31, aw = bdim(bs, 0), lh = bdim(bs, 1);
|
|
667
|
+
const cbx4 = bx4 >> this.ssx, cby4 = by4 >> this.ssy, cw = aw + this.ssx >> this.ssx, ch = lh + this.ssy >> this.ssy;
|
|
668
|
+
return [this.aMode.slice(bx4, bx4 + aw), this.lMode.slice(by4, by4 + lh), this.aUvmode.slice(cbx4, cbx4 + cw), this.lUvmode.slice(cby4, cby4 + ch)];
|
|
669
|
+
}
|
|
670
|
+
restoreContexts(c) {
|
|
671
|
+
const bx4 = this.bx, by4 = this.by & 31, cbx4 = bx4 >> this.ssx, cby4 = by4 >> this.ssy;
|
|
672
|
+
this.aMode.set(c[0], bx4);
|
|
673
|
+
this.lMode.set(c[1], by4);
|
|
674
|
+
this.aUvmode.set(c[2], cbx4);
|
|
675
|
+
this.lUvmode.set(c[3], cby4);
|
|
676
|
+
}
|
|
677
|
+
cflAllowed(bs) {
|
|
678
|
+
if (!this.lossless)
|
|
679
|
+
return !!(CFL_ALLOWED_MASK & 1 << bs);
|
|
680
|
+
return (bdim(bs, 0) + this.ssx >> this.ssx) === 1 && (bdim(bs, 1) + this.ssy >> this.ssy) === 1;
|
|
681
|
+
}
|
|
682
|
+
savePlane(p, bs) {
|
|
683
|
+
const f = this.f, sx = p ? this.ssx : 0, sy = p ? this.ssy : 0, stride = f.strides[p];
|
|
684
|
+
const w = bdim(bs, 0) * 4 >> sx, h = bdim(bs, 1) * 4 >> sy, o = (this.by * 4 >> sy) * stride + (this.bx * 4 >> sx);
|
|
685
|
+
const out = new Uint16Array(w * h);
|
|
686
|
+
for (let y = 0; y < h; y++)
|
|
687
|
+
out.set(f.planes[p].subarray(o + y * stride, o + y * stride + w), y * w);
|
|
688
|
+
return out;
|
|
689
|
+
}
|
|
690
|
+
restorePlane(p, bs, saved) {
|
|
691
|
+
const f = this.f, sx = p ? this.ssx : 0, sy = p ? this.ssy : 0, stride = f.strides[p];
|
|
692
|
+
const w = bdim(bs, 0) * 4 >> sx, h = bdim(bs, 1) * 4 >> sy, o = (this.by * 4 >> sy) * stride + (this.bx * 4 >> sx);
|
|
693
|
+
for (let y = 0; y < h; y++)
|
|
694
|
+
f.planes[p].set(saved.subarray(y * w, y * w + w), o + y * stride);
|
|
695
|
+
}
|
|
696
|
+
satd(p, bs) {
|
|
697
|
+
const f = this.f, sx = p ? this.ssx : 0, sy = p ? this.ssy : 0, stride = f.strides[p];
|
|
698
|
+
const w = bdim(bs, 0) * 4 >> sx, h = bdim(bs, 1) * 4 >> sy, x0 = this.bx * 4 >> sx, y0 = this.by * 4 >> sy;
|
|
699
|
+
const pred = f.planes[p], src = this.src[p], d = SATD_BLOCK;
|
|
700
|
+
let sum = 0;
|
|
701
|
+
for (let by = 0; by < h; by += 4) {
|
|
702
|
+
for (let bx = 0; bx < w; bx += 4) {
|
|
703
|
+
for (let y = 0; y < 4; y++) {
|
|
704
|
+
const o = (y0 + by + y) * stride + x0 + bx;
|
|
705
|
+
for (let x = 0; x < 4; x++)
|
|
706
|
+
d[y * 4 + x] = src[o + x] - pred[o + x];
|
|
707
|
+
}
|
|
708
|
+
for (let y = 0; y < 4; y++) {
|
|
709
|
+
const a = d[y * 4] + d[y * 4 + 1], b = d[y * 4] - d[y * 4 + 1], c = d[y * 4 + 2] + d[y * 4 + 3], e = d[y * 4 + 2] - d[y * 4 + 3];
|
|
710
|
+
d[y * 4] = a + c;
|
|
711
|
+
d[y * 4 + 1] = b + e;
|
|
712
|
+
d[y * 4 + 2] = a - c;
|
|
713
|
+
d[y * 4 + 3] = b - e;
|
|
714
|
+
}
|
|
715
|
+
for (let x = 0; x < 4; x++) {
|
|
716
|
+
const a = d[x] + d[4 + x], b = d[x] - d[4 + x], c = d[8 + x] + d[12 + x], e = d[8 + x] - d[12 + x];
|
|
717
|
+
sum += Math.abs(a + c) + Math.abs(b + e) + Math.abs(a - c) + Math.abs(b - e);
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
return sum / 4;
|
|
722
|
+
}
|
|
723
|
+
cflAlphas(bs, b) {
|
|
724
|
+
const f = this.f, ssx = this.ssx, ssy = this.ssy, stride = f.strides[0];
|
|
725
|
+
const bw4 = bdim(bs, 0), bh4 = bdim(bs, 1);
|
|
726
|
+
const w4 = Math.min(bw4, f.bw - this.bx), h4 = Math.min(bh4, f.bh - this.by);
|
|
727
|
+
const cbw4 = bw4 + ssx >> ssx, cbh4 = bh4 + ssy >> ssy, cw4 = w4 + ssx >> ssx, ch4 = h4 + ssy >> ssy;
|
|
728
|
+
const tw = tdim(b.tx, W), th = tdim(b.tx, H);
|
|
729
|
+
const yo = 4 * (this.bx & ~ssx) + 4 * (this.by & ~ssy) * stride;
|
|
730
|
+
const furthestR = ((cw4 << ssx) + tw - 1) & ~(tw - 1), furthestB = ((ch4 << ssy) + th - 1) & ~(th - 1);
|
|
731
|
+
cflAc(this.ac, f.planes[0], yo, stride, cbw4 - (furthestR >> ssx), cbh4 - (furthestB >> ssy), cbw4 * 4, cbh4 * 4, ssx, ssy);
|
|
732
|
+
const alphas = [0, 0], w = cbw4 * 4, h = cbh4 * 4, cstride = f.strides[1];
|
|
733
|
+
const x0 = this.bx * 4 >> ssx, y0 = this.by * 4 >> ssy;
|
|
734
|
+
for (let pl = 0; pl < 2; pl++) {
|
|
735
|
+
const src = this.src[1 + pl];
|
|
736
|
+
let mean = 0;
|
|
737
|
+
for (let y = 0; y < h; y++)
|
|
738
|
+
for (let x = 0; x < w; x++)
|
|
739
|
+
mean += src[(y0 + y) * cstride + x0 + x];
|
|
740
|
+
mean /= w * h;
|
|
741
|
+
let sxy = 0, sxx = 0;
|
|
742
|
+
for (let y = 0; y < h; y++) {
|
|
743
|
+
for (let x = 0; x < w; x++) {
|
|
744
|
+
const a = this.ac[y * w + x];
|
|
745
|
+
sxy += a * (src[(y0 + y) * cstride + x0 + x] - mean);
|
|
746
|
+
sxx += a * a;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
alphas[pl] = sxx ? Math.max(-16, Math.min(16, Math.round(sxy / sxx * 64 / 8))) : 0;
|
|
750
|
+
}
|
|
751
|
+
return alphas;
|
|
752
|
+
}
|
|
753
|
+
predictLuma(bs, edgeFlags, b) {
|
|
754
|
+
this.codeLuma(bs, edgeFlags, b, true);
|
|
755
|
+
}
|
|
756
|
+
predictChroma(bs, edgeFlags, b) {
|
|
757
|
+
this.codeChroma(bs, edgeFlags, b, true);
|
|
758
|
+
}
|
|
759
|
+
codeLuma(bs, edgeFlags, b, predictOnly = false) {
|
|
760
|
+
const f = this.f, seq = this.seq;
|
|
761
|
+
const bx = this.bx, by = this.by, bx4 = bx, by4 = by & 31;
|
|
762
|
+
const bw4 = bdim(bs, 0), bh4 = bdim(bs, 1);
|
|
763
|
+
const w4 = Math.min(bw4, f.bw - bx), h4 = Math.min(bh4, f.bh - by);
|
|
764
|
+
const tx = b.tx, tw = tdim(tx, W), th = tdim(tx, H);
|
|
765
|
+
const filterFlag = seq.intraEdgeFilter ? ANGLE_USE_EDGE_FILTER : 0;
|
|
766
|
+
const pic = f.planes[0], stride = f.strides[0];
|
|
767
|
+
let dist = 0;
|
|
768
|
+
this.bits = 0;
|
|
769
|
+
this.coded = false;
|
|
770
|
+
for (let initY = 0; initY < h4; initY += 16) {
|
|
771
|
+
const subH4 = Math.min(h4, 16 + initY);
|
|
772
|
+
for (let initX = 0; initX < w4; initX += 16) {
|
|
773
|
+
const palIdx = b.palSz[0] ? b.palIdx : null;
|
|
774
|
+
if (palIdx) {
|
|
775
|
+
const d = 4 * (by * stride + bx), bw = bw4 * 4;
|
|
776
|
+
for (let y = 0; y < bh4 * 4; y++)
|
|
777
|
+
for (let x = 0; x < bw; x++)
|
|
778
|
+
pic[d + y * stride + x] = b.pal[palIdx[y * bw + x]];
|
|
779
|
+
}
|
|
780
|
+
const intraFlags = smFlag(this.aMode[bx4]) | smFlag(this.lMode[by4]) | filterFlag;
|
|
781
|
+
const sbHasTr = initX + 16 < w4 ? 1 : initY ? 0 : edgeFlags & EDGE_I444_TOP_HAS_RIGHT;
|
|
782
|
+
const sbHasBl = initX ? 0 : initY + 16 < h4 ? 1 : edgeFlags & EDGE_I444_LEFT_HAS_BOTTOM;
|
|
783
|
+
const subW4 = Math.min(w4, initX + 16);
|
|
784
|
+
for (let y = initY; y < subH4; y += th) {
|
|
785
|
+
const ty = by + y;
|
|
786
|
+
let d = 4 * (ty * stride + bx + initX);
|
|
787
|
+
for (let x = initX; x < subW4; x += tw, d += 4 * tw) {
|
|
788
|
+
const tx4 = bx + x;
|
|
789
|
+
const flags = (((y > initY || !sbHasTr) && x + tw >= subW4) ? 0 : EDGE_I444_TOP_HAS_RIGHT) |
|
|
790
|
+
((x > initX || (!sbHasBl && y + th >= subH4)) ? 0 : EDGE_I444_LEFT_HAS_BOTTOM);
|
|
791
|
+
if (!palIdx) {
|
|
792
|
+
const mode = prepareEdges(tx4, tx4 > this.colStart, ty, ty > this.rowStart, this.colEnd, this.rowEnd, flags, pic, d, stride, b.yMode, b.yAngle, tw, th, seq.intraEdgeFilter, f.bitdepth);
|
|
793
|
+
predict(mode, pic, d, stride, tw * 4, th * 4, preparedAngle | intraFlags, 4 * f.bw - 4 * tx4, 4 * f.bh - 4 * ty, f.bitdepthMax);
|
|
794
|
+
}
|
|
795
|
+
if (predictOnly)
|
|
796
|
+
continue;
|
|
797
|
+
dist += this.codeTx(0, d, stride, tx, b.txtp, b, bs, this.aLcoef, bx4 + x, this.lLcoef, by4 + y, Math.min(tw, f.bw - tx4), Math.min(th, f.bh - ty), 4 * tx4, 4 * ty);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
return dist;
|
|
803
|
+
}
|
|
804
|
+
codeChroma(bs, edgeFlags, b, predictOnly = false) {
|
|
805
|
+
const f = this.f, seq = this.seq, ssx = this.ssx, ssy = this.ssy;
|
|
806
|
+
const bx = this.bx, by = this.by, bx4 = bx, by4 = by & 31;
|
|
807
|
+
const cbx4 = bx4 >> ssx, cby4 = by4 >> ssy;
|
|
808
|
+
const bw4 = bdim(bs, 0), bh4 = bdim(bs, 1);
|
|
809
|
+
const w4 = Math.min(bw4, f.bw - bx), h4 = Math.min(bh4, f.bh - by);
|
|
810
|
+
const cw4 = w4 + ssx >> ssx, ch4 = h4 + ssy >> ssy, cbw4 = bw4 + ssx >> ssx, cbh4 = bh4 + ssy >> ssy;
|
|
811
|
+
const tx = b.tx, uvtx = b.uvtx, tw = tdim(tx, W), th = tdim(tx, H), uvtw = tdim(uvtx, W), uvth = tdim(uvtx, H);
|
|
812
|
+
const filterFlag = seq.intraEdgeFilter ? ANGLE_USE_EDGE_FILTER : 0;
|
|
813
|
+
const stride = f.strides[0], cstride = f.strides[1];
|
|
814
|
+
let dist = 0;
|
|
815
|
+
this.bits = 0;
|
|
816
|
+
this.coded = false;
|
|
817
|
+
for (let initY = 0; initY < h4; initY += 16) {
|
|
818
|
+
const subCh4 = Math.min(ch4, initY + 16 >> ssy);
|
|
819
|
+
for (let initX = 0; initX < w4; initX += 16) {
|
|
820
|
+
if (b.uvMode === CFL_PRED) {
|
|
821
|
+
const yo = 4 * (bx & ~ssx) + 4 * (by & ~ssy) * stride;
|
|
822
|
+
const furthestR = ((cw4 << ssx) + tw - 1) & ~(tw - 1), furthestB = ((ch4 << ssy) + th - 1) & ~(th - 1);
|
|
823
|
+
cflAc(this.ac, f.planes[0], yo, stride, cbw4 - (furthestR >> ssx), cbh4 - (furthestB >> ssy), cbw4 * 4, cbh4 * 4, ssx, ssy);
|
|
824
|
+
const uvOff = 4 * ((bx >> ssx) + (by >> ssy) * cstride);
|
|
825
|
+
for (let pl = 0; pl < 2; pl++) {
|
|
826
|
+
if (!b.cflAlpha[pl])
|
|
827
|
+
continue;
|
|
828
|
+
const xpos = bx >> ssx, ypos = by >> ssy;
|
|
829
|
+
const mode = prepareEdges(xpos, xpos > this.colStart >> ssx, ypos, ypos > this.rowStart >> ssy, this.colEnd >> ssx, this.rowEnd >> ssy, 0, f.planes[1 + pl], uvOff, cstride, DC_PRED, 0, uvtw, uvth, false, f.bitdepth);
|
|
830
|
+
cflPredict(mode, f.planes[1 + pl], uvOff, cstride, uvtw * 4, uvth * 4, this.ac, b.cflAlpha[pl], f.bitdepthMax);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
else if (b.palSz[1]) {
|
|
834
|
+
const uvOff = 4 * ((bx >> ssx) + (by >> ssy) * cstride), bw = cbw4 * 4, idx = b.palIdxUv;
|
|
835
|
+
for (let pl = 1; pl <= 2; pl++) {
|
|
836
|
+
const plane = f.planes[pl];
|
|
837
|
+
for (let y = 0; y < cbh4 * 4; y++)
|
|
838
|
+
for (let x = 0; x < bw; x++)
|
|
839
|
+
plane[uvOff + y * cstride + x] = b.pal[pl * 8 + idx[y * bw + x]];
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
const smUv = smFlag(this.aUvmode[cbx4]) | smFlag(this.lUvmode[cby4]);
|
|
843
|
+
const edge = f.layout === 1 ? [EDGE_I420_TOP_HAS_RIGHT, EDGE_I420_LEFT_HAS_BOTTOM] : [1, 8];
|
|
844
|
+
const uvSbHasTr = (initX + 16 >> ssx) < cw4 ? 1 : initY ? 0 : edgeFlags & edge[0];
|
|
845
|
+
const uvSbHasBl = initX ? 0 : (initY + 16 >> ssy) < ch4 ? 1 : edgeFlags & edge[1];
|
|
846
|
+
const subCw4 = Math.min(cw4, initX + 16 >> ssx);
|
|
847
|
+
const txtp = this.lossless ? WHT_WHT : tdim(uvtx, MAX) + 1 >= TX_64X64 ? DCT_DCT : TXTP_FROM_UVMODE[b.uvMode];
|
|
848
|
+
for (let pl = 0; pl < 2; pl++) {
|
|
849
|
+
const plane = f.planes[1 + pl];
|
|
850
|
+
const aC = this.aCcoef[pl], lC = this.lCcoef[pl];
|
|
851
|
+
for (let y = initY >> ssy, tyl = by + initY; y < subCh4; y += uvth, tyl += uvth << ssy) {
|
|
852
|
+
let d = 4 * ((tyl >> ssy) * cstride + (bx + initX >> ssx));
|
|
853
|
+
for (let x = initX >> ssx, txl = bx + initX; x < subCw4; x += uvtw, txl += uvtw << ssx, d += uvtw * 4) {
|
|
854
|
+
if (!(b.uvMode === CFL_PRED && b.cflAlpha[pl]) && !b.palSz[1]) {
|
|
855
|
+
const flags = (((y > (initY >> ssy) || !uvSbHasTr) && x + uvtw >= subCw4) ? 0 : EDGE_I444_TOP_HAS_RIGHT) |
|
|
856
|
+
((x > (initX >> ssx) || (!uvSbHasBl && y + uvth >= subCh4)) ? 0 : EDGE_I444_LEFT_HAS_BOTTOM);
|
|
857
|
+
const uvMode = b.uvMode === CFL_PRED ? DC_PRED : b.uvMode;
|
|
858
|
+
const xpos = txl >> ssx, ypos = tyl >> ssy;
|
|
859
|
+
const mode = prepareEdges(xpos, xpos > this.colStart >> ssx, ypos, ypos > this.rowStart >> ssy, this.colEnd >> ssx, this.rowEnd >> ssy, flags, plane, d, cstride, uvMode, b.uvAngle, uvtw, uvth, seq.intraEdgeFilter, f.bitdepth);
|
|
860
|
+
predict(mode, plane, d, cstride, uvtw * 4, uvth * 4, preparedAngle | filterFlag | smUv, (4 * f.bw + ssx - 4 * (txl & ~ssx)) >> ssx, (4 * f.bh + ssy - 4 * (tyl & ~ssy)) >> ssy, f.bitdepthMax);
|
|
861
|
+
}
|
|
862
|
+
if (predictOnly)
|
|
863
|
+
continue;
|
|
864
|
+
const ctw = Math.min(uvtw, (f.bw - txl + ssx) >> ssx), cth = Math.min(uvth, (f.bh - tyl + ssy) >> ssy);
|
|
865
|
+
dist += this.codeTx(1 + pl, d, cstride, uvtx, txtp, b, bs, aC, cbx4 + x, lC, cby4 + y, ctw, cth, 4 * txl >> ssx, 4 * tyl >> ssy);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
return dist;
|
|
872
|
+
}
|
|
873
|
+
codeTx(plane, d, stride, tx, txtp, b, bs, aArr, a, lArr, l, ctw, cth, px, py) {
|
|
874
|
+
const f = this.f, pic = f.planes[plane], src = this.src[plane];
|
|
875
|
+
const w = tdim(tx, W) * 4, h = tdim(tx, H) * 4, sw = Math.min(w, 32), sh = Math.min(h, 32);
|
|
876
|
+
const sx = plane ? this.ssx : 0, sy = plane ? this.ssy : 0;
|
|
877
|
+
const vw = Math.max(0, Math.min(w, (f.width + sx >> sx) - px)), vh = Math.max(0, Math.min(h, (f.height + sy >> sy) - py));
|
|
878
|
+
let eob = -1;
|
|
879
|
+
if (!(this.w && b.skip)) {
|
|
880
|
+
const res = this.res, coefs = this.coefs, q = this.q;
|
|
881
|
+
let sse = 0;
|
|
882
|
+
for (let y = 0; y < h; y++) {
|
|
883
|
+
for (let x = 0; x < w; x++) {
|
|
884
|
+
const e = res[y * w + x] = src[d + y * stride + x] - pic[d + y * stride + x];
|
|
885
|
+
sse += e * e;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
if (this.lossless)
|
|
889
|
+
eob = this.whtCoefs(res, q);
|
|
890
|
+
else if (sse < this.zeroBound(plane, tx))
|
|
891
|
+
eob = -1;
|
|
892
|
+
else {
|
|
893
|
+
forwardTransform(res, tx, txtp, coefs);
|
|
894
|
+
eob = this.quantize(plane, tx, txtp, coefs, q, sw * sh);
|
|
895
|
+
}
|
|
896
|
+
if (eob >= 0)
|
|
897
|
+
this.coded = true;
|
|
898
|
+
if (this.w)
|
|
899
|
+
this.writeCoefs(aArr, a, lArr, l, tx, txtp, bs, b, plane, eob, ctw, cth);
|
|
900
|
+
else
|
|
901
|
+
this.bits += this.estimateBits(tx, q, eob);
|
|
902
|
+
if (eob >= 0) {
|
|
903
|
+
this.dequantize(plane, tx, txtp, q, eob);
|
|
904
|
+
inverseTransformAdd(pic, d, stride, this.cf, eob, tx, txtp, f.bitdepthMax);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
else {
|
|
908
|
+
aArr.fill(0x40, a, a + tdim(tx, W));
|
|
909
|
+
lArr.fill(0x40, l, l + tdim(tx, H));
|
|
910
|
+
}
|
|
911
|
+
let dist = 0;
|
|
912
|
+
for (let y = 0; y < vh; y++) {
|
|
913
|
+
for (let x = 0; x < vw; x++) {
|
|
914
|
+
const e = src[d + y * stride + x] - pic[d + y * stride + x];
|
|
915
|
+
dist += e * e;
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
return dist;
|
|
919
|
+
}
|
|
920
|
+
zeroBound(plane, tx) {
|
|
921
|
+
const step = Math.min(this.dq[plane][0], this.dq[plane][1]) / 2 ** Math.max(0, tdim(tx, CTX) - 2);
|
|
922
|
+
return 0.2 * pixelEnergy(tx) * step * step;
|
|
923
|
+
}
|
|
924
|
+
whtCoefs(res, q) {
|
|
925
|
+
const r = this.whtRes, out = this.whtOut;
|
|
926
|
+
for (let i = 0; i < 16; i++)
|
|
927
|
+
r[i] = res[i];
|
|
928
|
+
forwardWht(r, out);
|
|
929
|
+
for (let y = 0; y < 4; y++)
|
|
930
|
+
for (let x = 0; x < 4; x++)
|
|
931
|
+
q[x * 4 + y] = out[y * 4 + x];
|
|
932
|
+
const order = scan(TX_4X4);
|
|
933
|
+
let eob = -1;
|
|
934
|
+
for (let i = 0; i < 16; i++)
|
|
935
|
+
if (q[order[i]])
|
|
936
|
+
eob = i;
|
|
937
|
+
return eob;
|
|
938
|
+
}
|
|
939
|
+
quantize(plane, tx, txtp, coefs, q, n) {
|
|
940
|
+
const dqShift = Math.max(0, tdim(tx, CTX) - 2), [dcq, acq] = this.dq[plane];
|
|
941
|
+
const order = scan(tx), energy = pixelEnergy(tx), lambda = this.lambda;
|
|
942
|
+
const keep = this.rdoKeep, drop = this.rdoDrop;
|
|
943
|
+
void txtp;
|
|
944
|
+
let last = -1, total = 0;
|
|
945
|
+
for (let i = 0; i < n; i++) {
|
|
946
|
+
const rc = order[i], step = (rc ? acq : dcq) / 2 ** dqShift, v = Math.abs(coefs[rc]) / step;
|
|
947
|
+
const e = energy * step * step, dropped = e * v * v;
|
|
948
|
+
drop[i] = dropped;
|
|
949
|
+
total += dropped;
|
|
950
|
+
if (v < 0.5) {
|
|
951
|
+
q[rc] = 0;
|
|
952
|
+
keep[i] = dropped + lambda * ZERO_BITS;
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
let m = Math.floor(v + 0.5);
|
|
956
|
+
const dm = v - m, dl = v - m + 1;
|
|
957
|
+
if (e * dl * dl + lambda * coefBits(m - 1) < e * dm * dm + lambda * coefBits(m))
|
|
958
|
+
m--;
|
|
959
|
+
q[rc] = coefs[rc] < 0 ? -m : m;
|
|
960
|
+
keep[i] = e * (v - m) * (v - m) + lambda * coefBits(m);
|
|
961
|
+
if (m)
|
|
962
|
+
last = i;
|
|
963
|
+
}
|
|
964
|
+
if (last < 0)
|
|
965
|
+
return -1;
|
|
966
|
+
let best = total + lambda, bestEob = -1, prefix = 0;
|
|
967
|
+
let rest = best - lambda;
|
|
968
|
+
for (let i = 0; i <= last; i++) {
|
|
969
|
+
prefix += keep[i];
|
|
970
|
+
rest -= drop[i];
|
|
971
|
+
if (!q[order[i]])
|
|
972
|
+
continue;
|
|
973
|
+
const cost = prefix + rest + lambda * (2 + 1.5 * Math.log2(i + 2));
|
|
974
|
+
if (cost < best) {
|
|
975
|
+
best = cost;
|
|
976
|
+
bestEob = i;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
for (let i = bestEob + 1; i <= last; i++)
|
|
980
|
+
q[order[i]] = 0;
|
|
981
|
+
return bestEob;
|
|
982
|
+
}
|
|
983
|
+
dequantize(plane, tx, txtp, q, eob) {
|
|
984
|
+
const dqShift = Math.max(0, tdim(tx, CTX) - 2), [dcq, acq] = this.dq[plane];
|
|
985
|
+
const cfMax = (128 << 8) - 1, order = scan(tx), cf = this.cf;
|
|
986
|
+
void txtp;
|
|
987
|
+
for (let i = 0; i <= eob; i++) {
|
|
988
|
+
const rc = order[i], v = q[rc];
|
|
989
|
+
if (!v) {
|
|
990
|
+
cf[rc] = 0;
|
|
991
|
+
continue;
|
|
992
|
+
}
|
|
993
|
+
const m = Math.abs(v), dq = rc ? acq : dcq;
|
|
994
|
+
let c;
|
|
995
|
+
if (m >= 15)
|
|
996
|
+
c = Math.min((dq * m & 0xffffff) >> dqShift, cfMax + (v < 0 ? 1 : 0));
|
|
997
|
+
else
|
|
998
|
+
c = (dq * m) >> dqShift;
|
|
999
|
+
cf[rc] = v < 0 ? -c : c;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
estimateBits(tx, q, eob) {
|
|
1003
|
+
if (eob < 0)
|
|
1004
|
+
return 1;
|
|
1005
|
+
const order = scan(tx);
|
|
1006
|
+
let bits = 2 + 1.5 * Math.log2(eob + 2);
|
|
1007
|
+
for (let i = 0; i <= eob; i++)
|
|
1008
|
+
bits += coefBits(Math.abs(q[order[i]]));
|
|
1009
|
+
return bits;
|
|
1010
|
+
}
|
|
1011
|
+
writeSb(bl, node, n) {
|
|
1012
|
+
const f = this.f, w = this.w, cdf = this.cdf;
|
|
1013
|
+
const hsz = 16 >> bl;
|
|
1014
|
+
const haveH = f.bw > this.bx + hsz, haveV = f.bh > this.by + hsz;
|
|
1015
|
+
if (!haveH && !haveV)
|
|
1016
|
+
return this.writeSb(bl + 1, node.split[0], n.kids[0]);
|
|
1017
|
+
const bx8 = this.bx >> 1, by8 = (this.by & 31) >> 1;
|
|
1018
|
+
const ctx = (this.aPartition[bx8] >> (4 - bl) & 1) + ((this.lPartition[by8] >> (4 - bl) & 1) << 1);
|
|
1019
|
+
const pc = CDF.partition + (bl * 4 + ctx) * 16;
|
|
1020
|
+
const bp = n.bp;
|
|
1021
|
+
const quarter = (k) => {
|
|
1022
|
+
const dx = k & 1 ? hsz : 0, dy = k & 2 ? hsz : 0;
|
|
1023
|
+
this.bx += dx;
|
|
1024
|
+
this.by += dy;
|
|
1025
|
+
this.writeSb(bl + 1, node.split[k], n.kids[k]);
|
|
1026
|
+
this.bx -= dx;
|
|
1027
|
+
this.by -= dy;
|
|
1028
|
+
};
|
|
1029
|
+
if (haveH && haveV) {
|
|
1030
|
+
w.symbol(cdf, pc, PARTITION_TYPE_COUNT[bl], bp);
|
|
1031
|
+
if (bp === PARTITION_NONE)
|
|
1032
|
+
this.writeB(BLOCK_SIZES[(bl * 10 + bp) * 2], node.o, n.b);
|
|
1033
|
+
else if (bp === PARTITION_H || bp === PARTITION_V) {
|
|
1034
|
+
const bs = BLOCK_SIZES[(bl * 10 + bp) * 2], edges = bp === PARTITION_H ? node.h : node.v;
|
|
1035
|
+
const dx = bp === PARTITION_V ? hsz : 0, dy = bp === PARTITION_H ? hsz : 0;
|
|
1036
|
+
this.writeB(bs, edges[0], n.kids[0].b);
|
|
1037
|
+
this.bx += dx;
|
|
1038
|
+
this.by += dy;
|
|
1039
|
+
this.writeB(bs, edges[1], n.kids[1].b);
|
|
1040
|
+
this.bx -= dx;
|
|
1041
|
+
this.by -= dy;
|
|
1042
|
+
}
|
|
1043
|
+
else
|
|
1044
|
+
for (let k = 0; k < 4; k++)
|
|
1045
|
+
quarter(k);
|
|
1046
|
+
}
|
|
1047
|
+
else if (haveH) {
|
|
1048
|
+
let prob = cdf[pc + PARTITION_V - 1] - cdf[pc + PARTITION_T_TOP_SPLIT];
|
|
1049
|
+
prob += cdf[pc + PARTITION_T_LEFT_SPLIT - 1];
|
|
1050
|
+
prob += cdf[pc + PARTITION_V4 - 1] - cdf[pc + PARTITION_T_RIGHT_SPLIT];
|
|
1051
|
+
w.boolProb(prob, 1);
|
|
1052
|
+
quarter(0);
|
|
1053
|
+
quarter(1);
|
|
1054
|
+
}
|
|
1055
|
+
else {
|
|
1056
|
+
let prob = cdf[pc + PARTITION_H - 1] - cdf[pc + PARTITION_H];
|
|
1057
|
+
prob += cdf[pc + PARTITION_SPLIT - 1] - cdf[pc + PARTITION_T_LEFT_SPLIT];
|
|
1058
|
+
prob += cdf[pc + PARTITION_H4 - 1] - cdf[pc + PARTITION_H4];
|
|
1059
|
+
w.boolProb(prob, 1);
|
|
1060
|
+
quarter(0);
|
|
1061
|
+
quarter(2);
|
|
1062
|
+
}
|
|
1063
|
+
const p = haveH && haveV ? bp : PARTITION_SPLIT;
|
|
1064
|
+
if (p !== PARTITION_SPLIT || bl === BL_8X8) {
|
|
1065
|
+
this.aPartition.fill(AL_PART_CTX[bl * 10 + p], bx8, bx8 + hsz);
|
|
1066
|
+
this.lPartition.fill(AL_PART_CTX[50 + bl * 10 + p], by8, by8 + hsz);
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
writeB(bs, edgeFlags, b) {
|
|
1070
|
+
const f = this.f, w = this.w, cdf = this.cdf;
|
|
1071
|
+
const bx = this.bx, by = this.by, bx4 = bx, by4 = by & 31;
|
|
1072
|
+
const ssx = this.ssx, ssy = this.ssy;
|
|
1073
|
+
const cbx4 = bx4 >> ssx, cby4 = by4 >> ssy;
|
|
1074
|
+
const bw4 = bdim(bs, 0), bh4 = bdim(bs, 1), lw4 = bdim(bs, 2), lh4 = bdim(bs, 3);
|
|
1075
|
+
const cbw4 = bw4 + ssx >> ssx, cbh4 = bh4 + ssy >> ssy;
|
|
1076
|
+
const hasChroma = f.layout !== 0;
|
|
1077
|
+
w.boolAdapt(cdf, CDF.skip + (this.aSkip[bx4] + this.lSkip[by4]) * 2, b.skip);
|
|
1078
|
+
if (!b.skip) {
|
|
1079
|
+
const idx = (by >> 4) * (f.b4Stride >> 4) + (bx >> 4);
|
|
1080
|
+
if (f.cdefIdx[idx] === -1) {
|
|
1081
|
+
f.cdefIdx[idx] = this.cdefChoice[idx];
|
|
1082
|
+
w.bools(this.hdr.cdef.bits, this.cdefChoice[idx]);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
w.symbol(cdf, CDF.kfYMode + (INTRA_MODE_CONTEXT[this.aMode[bx4]] * 5 + INTRA_MODE_CONTEXT[this.lMode[by4]]) * 16, 12, b.yMode);
|
|
1086
|
+
if (lw4 + lh4 >= 2 && isDirectional(b.yMode))
|
|
1087
|
+
w.symbol(cdf, CDF.angleDelta + (b.yMode - VERT_PRED) * 8, 6, b.yAngle + 3);
|
|
1088
|
+
if (hasChroma) {
|
|
1089
|
+
const cflAllowed = this.cflAllowed(bs);
|
|
1090
|
+
w.symbol(cdf, CDF.uvMode + ((cflAllowed ? 1 : 0) * 13 + b.yMode) * 16, cflAllowed ? 13 : 12, b.uvMode);
|
|
1091
|
+
if (b.uvMode === CFL_PRED) {
|
|
1092
|
+
const signU = b.cflAlpha[0] ? b.cflAlpha[0] < 0 ? 1 : 2 : 0, signV = b.cflAlpha[1] ? b.cflAlpha[1] < 0 ? 1 : 2 : 0;
|
|
1093
|
+
w.symbol(cdf, CDF.cflSign, 7, signU * 3 + signV - 1);
|
|
1094
|
+
if (signU)
|
|
1095
|
+
w.symbol(cdf, CDF.cflAlpha + ((signU === 2 ? 3 : 0) + signV) * 16, 15, Math.abs(b.cflAlpha[0]) - 1);
|
|
1096
|
+
if (signV)
|
|
1097
|
+
w.symbol(cdf, CDF.cflAlpha + ((signV === 2 ? 3 : 0) + signU) * 16, 15, Math.abs(b.cflAlpha[1]) - 1);
|
|
1098
|
+
}
|
|
1099
|
+
else if (lw4 + lh4 >= 2 && isDirectional(b.uvMode)) {
|
|
1100
|
+
w.symbol(cdf, CDF.angleDelta + (b.uvMode - VERT_PRED) * 8, 6, b.uvAngle + 3);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
if (this.hdr.allowScreenContentTools && Math.max(bw4, bh4) <= 16 && bw4 + bh4 >= 4) {
|
|
1104
|
+
const szCtx = lw4 + lh4 - 2;
|
|
1105
|
+
if (b.yMode === DC_PRED) {
|
|
1106
|
+
const palCtx = (this.aPalSz[bx4] > 0 ? 1 : 0) + (this.lPalSz[by4] > 0 ? 1 : 0);
|
|
1107
|
+
w.boolAdapt(cdf, CDF.palY + (szCtx * 3 + palCtx) * 2, b.palSz[0] ? 1 : 0);
|
|
1108
|
+
if (b.palSz[0])
|
|
1109
|
+
this.writePalPlane(b, 0, szCtx, bx4, by4);
|
|
1110
|
+
}
|
|
1111
|
+
if (hasChroma && b.uvMode === DC_PRED) {
|
|
1112
|
+
w.boolAdapt(cdf, CDF.palUv + (b.palSz[0] > 0 ? 2 : 0), b.palSz[1] ? 1 : 0);
|
|
1113
|
+
if (b.palSz[1])
|
|
1114
|
+
this.writePalUv(b, szCtx, bx4, by4);
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
const w4 = Math.min(bw4, f.bw - bx), h4 = Math.min(bh4, f.bh - by);
|
|
1118
|
+
if (b.palSz[0])
|
|
1119
|
+
this.writeColorIndices(b.palIdx, b.palSz[0], 0, w4 * 4, h4 * 4, bw4 * 4);
|
|
1120
|
+
if (hasChroma && b.palSz[1])
|
|
1121
|
+
this.writeColorIndices(b.palIdxUv, b.palSz[1], 1, (w4 + ssx >> ssx) * 4, (h4 + ssy >> ssy) * 4, cbw4 * 4);
|
|
1122
|
+
const maxTx = MAX_TXFM_SIZE_FOR_BS[bs * 4];
|
|
1123
|
+
if (this.hdr.txMode === 2 && tdim(maxTx, MAX) > TX_4X4) {
|
|
1124
|
+
const tctx = (this.lTxIntra[by4] >= tdim(maxTx, LH) ? 1 : 0) + (this.aTxIntra[bx4] >= tdim(maxTx, LW) ? 1 : 0);
|
|
1125
|
+
let depth = 0;
|
|
1126
|
+
for (let tx = maxTx; tx !== b.tx; tx = tdim(tx, SUB))
|
|
1127
|
+
depth++;
|
|
1128
|
+
w.symbol(cdf, CDF.txSize + ((tdim(maxTx, MAX) - 1) * 3 + tctx) * 4, Math.min(tdim(maxTx, MAX), 2), depth);
|
|
1129
|
+
}
|
|
1130
|
+
this.codeLuma(bs, edgeFlags, b);
|
|
1131
|
+
if (hasChroma)
|
|
1132
|
+
this.codeChroma(bs, edgeFlags, b);
|
|
1133
|
+
this.storeBlock(bs, b);
|
|
1134
|
+
const aw = 1 << lw4, lh = 1 << lh4;
|
|
1135
|
+
this.aMode.fill(b.yMode, bx4, bx4 + aw);
|
|
1136
|
+
this.lMode.fill(b.yMode, by4, by4 + lh);
|
|
1137
|
+
this.aSkip.fill(b.skip, bx4, bx4 + aw);
|
|
1138
|
+
this.lSkip.fill(b.skip, by4, by4 + lh);
|
|
1139
|
+
this.aPalSz.fill(b.palSz[0], bx4, bx4 + aw);
|
|
1140
|
+
this.lPalSz.fill(b.palSz[0], by4, by4 + lh);
|
|
1141
|
+
this.aTxIntra.fill(tdim(b.tx, LW), bx4, bx4 + aw);
|
|
1142
|
+
this.lTxIntra.fill(tdim(b.tx, LH), by4, by4 + lh);
|
|
1143
|
+
this.aPalSzUv.fill(hasChroma ? b.palSz[1] : 0, bx4, bx4 + aw);
|
|
1144
|
+
this.lPalSzUv.fill(hasChroma ? b.palSz[1] : 0, by4, by4 + lh);
|
|
1145
|
+
for (let pl = 0; pl < 3; pl++) {
|
|
1146
|
+
if (!b.palSz[pl ? 1 : 0] || pl && !hasChroma)
|
|
1147
|
+
continue;
|
|
1148
|
+
const colors = b.pal.subarray(pl * 8, pl * 8 + 8);
|
|
1149
|
+
for (let x = 0; x < bw4; x++)
|
|
1150
|
+
this.aPal.set(colors, (bx4 + x) * 24 + pl * 8);
|
|
1151
|
+
for (let y = 0; y < bh4; y++)
|
|
1152
|
+
this.lPal.set(colors, (by4 + y) * 24 + pl * 8);
|
|
1153
|
+
}
|
|
1154
|
+
if (hasChroma) {
|
|
1155
|
+
this.aUvmode.fill(b.uvMode, cbx4, cbx4 + cbw4);
|
|
1156
|
+
this.lUvmode.fill(b.uvMode, cby4, cby4 + cbh4);
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
writePalPlane(b, pl, szCtx, bx4, by4) {
|
|
1160
|
+
const w = this.w, palSz = b.palSz[pl];
|
|
1161
|
+
w.symbol(this.cdf, CDF.palSize + (pl * 7 + szCtx) * 8, 6, palSz - 2);
|
|
1162
|
+
const cache = [];
|
|
1163
|
+
let lCache = pl ? this.lPalSzUv[by4] : this.lPalSz[by4];
|
|
1164
|
+
let aCache = by4 & 15 ? pl ? this.aPalSzUv[bx4] : this.aPalSz[bx4] : 0;
|
|
1165
|
+
let l = by4 * 24 + pl * 8, a = bx4 * 24 + pl * 8;
|
|
1166
|
+
const L = this.lPal, A = this.aPal;
|
|
1167
|
+
const push = (v) => {
|
|
1168
|
+
if (!cache.length || cache[cache.length - 1] !== v)
|
|
1169
|
+
cache.push(v);
|
|
1170
|
+
};
|
|
1171
|
+
while (lCache && aCache) {
|
|
1172
|
+
if (L[l] < A[a]) {
|
|
1173
|
+
push(L[l++]);
|
|
1174
|
+
lCache--;
|
|
1175
|
+
}
|
|
1176
|
+
else {
|
|
1177
|
+
if (A[a] === L[l]) {
|
|
1178
|
+
l++;
|
|
1179
|
+
lCache--;
|
|
1180
|
+
}
|
|
1181
|
+
push(A[a++]);
|
|
1182
|
+
aCache--;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
while (lCache-- > 0)
|
|
1186
|
+
push(L[l++]);
|
|
1187
|
+
while (aCache-- > 0)
|
|
1188
|
+
push(A[a++]);
|
|
1189
|
+
const rest = Array.from(b.pal.subarray(pl * 8, pl * 8 + palSz));
|
|
1190
|
+
for (let n = 0, used = 0; n < cache.length && used < palSz; n++) {
|
|
1191
|
+
const i = rest.indexOf(cache[n]);
|
|
1192
|
+
w.bool(i >= 0 ? 1 : 0);
|
|
1193
|
+
if (i >= 0) {
|
|
1194
|
+
rest.splice(i, 1);
|
|
1195
|
+
used++;
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
if (!rest.length)
|
|
1199
|
+
return;
|
|
1200
|
+
const max = 255, minus = pl ? 0 : 1;
|
|
1201
|
+
w.bools(8, rest[0]);
|
|
1202
|
+
if (rest.length === 1)
|
|
1203
|
+
return;
|
|
1204
|
+
let maxDelta = 0;
|
|
1205
|
+
for (let i = 1; i < rest.length; i++)
|
|
1206
|
+
maxDelta = Math.max(maxDelta, rest[i] - rest[i - 1] - minus);
|
|
1207
|
+
const extra = Math.max(0, 32 - Math.clz32(maxDelta) - 5);
|
|
1208
|
+
w.bools(2, extra);
|
|
1209
|
+
let bits = 5 + extra, prev = rest[0];
|
|
1210
|
+
for (let i = 1; i < rest.length; i++) {
|
|
1211
|
+
w.bools(bits, rest[i] - prev - minus);
|
|
1212
|
+
prev = rest[i];
|
|
1213
|
+
if (prev + minus >= max)
|
|
1214
|
+
break;
|
|
1215
|
+
bits = Math.min(bits, 1 + (31 - Math.clz32(max - prev - minus)));
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
writePalUv(b, szCtx, bx4, by4) {
|
|
1219
|
+
this.writePalPlane(b, 1, szCtx, bx4, by4);
|
|
1220
|
+
const w = this.w, palSz = b.palSz[1], v = b.pal.subarray(16, 16 + palSz);
|
|
1221
|
+
const deltas = [];
|
|
1222
|
+
let maxMag = 0;
|
|
1223
|
+
for (let i = 1; i < palSz; i++) {
|
|
1224
|
+
const d = (v[i] - v[i - 1] + 128 & 255) - 128;
|
|
1225
|
+
deltas.push(d);
|
|
1226
|
+
maxMag = Math.max(maxMag, Math.abs(d));
|
|
1227
|
+
}
|
|
1228
|
+
const bits = Math.max(4, 32 - Math.clz32(maxMag));
|
|
1229
|
+
const deltaBits = bits <= 7 ? 2 + 8 + deltas.reduce((s, d) => s + bits + (d ? 1 : 0), 0) : Infinity;
|
|
1230
|
+
if (deltaBits < 8 * palSz) {
|
|
1231
|
+
w.bool(1);
|
|
1232
|
+
w.bools(2, bits - 4);
|
|
1233
|
+
w.bools(8, v[0]);
|
|
1234
|
+
for (const d of deltas) {
|
|
1235
|
+
w.bools(bits, Math.abs(d));
|
|
1236
|
+
if (d)
|
|
1237
|
+
w.bool(d < 0 ? 1 : 0);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
else {
|
|
1241
|
+
w.bool(0);
|
|
1242
|
+
for (let i = 0; i < palSz; i++)
|
|
1243
|
+
w.bools(8, v[i]);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
writeColorIndices(t, palSz, pl, w, h, bw) {
|
|
1247
|
+
const enc = this.w, cdf = this.cdf, o = CDF.colorMap + (pl * 7 + palSz - 2) * 5 * 8;
|
|
1248
|
+
enc.uniform(palSz, t[0]);
|
|
1249
|
+
colorRanks(t, bw, w, h, (ctx, rank) => enc.symbol(cdf, o + ctx * 8, palSz - 1, rank));
|
|
1250
|
+
}
|
|
1251
|
+
storeBlock(bs, b) {
|
|
1252
|
+
const f = this.f, bx = this.bx, by = this.by, ssx = this.ssx, ssy = this.ssy;
|
|
1253
|
+
const bw4 = bdim(bs, 0), bh4 = bdim(bs, 1);
|
|
1254
|
+
const w4 = Math.min(bw4, f.bw - bx), h4 = Math.min(bh4, f.bh - by);
|
|
1255
|
+
const lf = this.hdr.loopFilter, lvl = [lf.levelY[0], lf.levelY[1], lf.levelU, lf.levelV];
|
|
1256
|
+
const s4 = f.b4Stride;
|
|
1257
|
+
for (let y = 0; y < h4; y++) {
|
|
1258
|
+
for (let x = 0; x < w4; x++) {
|
|
1259
|
+
const o = (by + y) * s4 + bx + x;
|
|
1260
|
+
f.lfLevels.set(lvl, o * 4);
|
|
1261
|
+
f.txW[o] = tdim(b.tx, LW);
|
|
1262
|
+
f.txH[o] = tdim(b.tx, LH);
|
|
1263
|
+
f.skip[o] = b.skip;
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
if (f.layout) {
|
|
1267
|
+
const cx = bx >> ssx, cy = by >> ssy, cw4 = w4 + ssx >> ssx, ch4 = h4 + ssy >> ssy;
|
|
1268
|
+
for (let y = 0; y < ch4; y++) {
|
|
1269
|
+
for (let x = 0; x < cw4; x++) {
|
|
1270
|
+
const o = (cy + y) * s4 + cx + x;
|
|
1271
|
+
f.uvTxW[o] = tdim(b.uvtx, LW);
|
|
1272
|
+
f.uvTxH[o] = tdim(b.uvtx, LH);
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
writeCoefs(aArr, a, lArr, l, tx, txtp, bs, b, plane, eob, ctw, cth) {
|
|
1278
|
+
const w = this.w, cdf = this.cdf, f = this.f, q = this.q;
|
|
1279
|
+
const chroma = plane ? 1 : 0;
|
|
1280
|
+
const tlw = tdim(tx, LW), tlh = tdim(tx, LH), tctx = tdim(tx, CTX), tmin = tdim(tx, MIN), tmax = tdim(tx, MAX);
|
|
1281
|
+
let sctx;
|
|
1282
|
+
const aw = 1 << tlw, lh = 1 << tlh;
|
|
1283
|
+
if (chroma) {
|
|
1284
|
+
const bl2 = bdim(bs, 2), bl3 = bdim(bs, 3);
|
|
1285
|
+
const notOneBlk = bl2 - (bl2 && f.ssx ? 1 : 0) > tlw || bl3 - (bl3 && f.ssy ? 1 : 0) > tlh;
|
|
1286
|
+
let ca = 0, cl = 0;
|
|
1287
|
+
for (let i = 0; i < aw; i++)
|
|
1288
|
+
if (aArr[a + i] !== 0x40) {
|
|
1289
|
+
ca = 1;
|
|
1290
|
+
break;
|
|
1291
|
+
}
|
|
1292
|
+
for (let i = 0; i < lh; i++)
|
|
1293
|
+
if (lArr[l + i] !== 0x40) {
|
|
1294
|
+
cl = 1;
|
|
1295
|
+
break;
|
|
1296
|
+
}
|
|
1297
|
+
sctx = 7 + (notOneBlk ? 3 : 0) + ca + cl;
|
|
1298
|
+
}
|
|
1299
|
+
else if (bdim(bs, 2) === tlw && bdim(bs, 3) === tlh) {
|
|
1300
|
+
sctx = 0;
|
|
1301
|
+
}
|
|
1302
|
+
else {
|
|
1303
|
+
let la = 0, ll = 0;
|
|
1304
|
+
for (let i = 0; i < aw; i++)
|
|
1305
|
+
la |= aArr[a + i];
|
|
1306
|
+
for (let i = 0; i < lh; i++)
|
|
1307
|
+
ll |= lArr[l + i];
|
|
1308
|
+
sctx = SKIP_CTX[Math.min(la & 0x3f, 4) * 5 + Math.min(ll & 0x3f, 4)];
|
|
1309
|
+
}
|
|
1310
|
+
w.boolAdapt(cdf, CDF.coefSkip + (tctx * 13 + sctx) * 2, eob < 0 ? 1 : 0);
|
|
1311
|
+
if (eob < 0) {
|
|
1312
|
+
aArr.fill(0x40, a, a + ctw);
|
|
1313
|
+
lArr.fill(0x40, l, l + cth);
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
if (!chroma && tmax + 1 < TX_64X64 && !this.lossless) {
|
|
1317
|
+
if (tmin === TX_16X16)
|
|
1318
|
+
w.symbol(cdf, CDF.txtpIntra2 + (tmin * 13 + b.yMode) * 8, 4, TX_TYPES_PER_SET.indexOf(txtp));
|
|
1319
|
+
else
|
|
1320
|
+
w.symbol(cdf, CDF.txtpIntra1 + (tmin * 13 + b.yMode) * 8, 6, TX_TYPES_PER_SET.indexOf(txtp, 5) - 5);
|
|
1321
|
+
}
|
|
1322
|
+
const slw = Math.min(tlw, 3), slh = Math.min(tlh, 3), tx2d = slw + slh;
|
|
1323
|
+
const eobBin = eob < 2 ? eob : 32 - Math.clz32(eob);
|
|
1324
|
+
const binCdf = [CDF.eobBin16, CDF.eobBin32, CDF.eobBin64, CDF.eobBin128, CDF.eobBin256, CDF.eobBin512, CDF.eobBin1024][tx2d];
|
|
1325
|
+
const binCtx = tx2d < 4 ? chroma * 2 * 8 : tx2d === 4 ? chroma * 2 * 16 : chroma * 16;
|
|
1326
|
+
w.symbol(cdf, binCdf + binCtx, 4 + tx2d, eobBin);
|
|
1327
|
+
if (eobBin > 1) {
|
|
1328
|
+
w.boolAdapt(cdf, CDF.eobHiBit + ((tctx * 2 + chroma) * 11 + eobBin) * 2, eob >> (eobBin - 2) & 1);
|
|
1329
|
+
w.bools(eobBin - 2, eob & ((1 << (eobBin - 2)) - 1));
|
|
1330
|
+
}
|
|
1331
|
+
const levels = this.levels, order = scan(tx);
|
|
1332
|
+
const eobCdf = CDF.eobBaseTok + (tctx * 2 + chroma) * 16;
|
|
1333
|
+
const hiCdf = CDF.brTok + (Math.min(tctx, 3) * 2 + chroma) * 84;
|
|
1334
|
+
const tokOf = (v) => Math.min(Math.abs(v), 15);
|
|
1335
|
+
if (eob) {
|
|
1336
|
+
const loCdf = CDF.baseTok + (tctx * 2 + chroma) * 164;
|
|
1337
|
+
const nonsquare = tx >= RTX_4X8 ? 1 : 0, offsets = (nonsquare + (tx & nonsquare)) * 25;
|
|
1338
|
+
const stride = 4 << slh, shift = slh + 2, mask = (4 << slh) - 1;
|
|
1339
|
+
levels.fill(0, 0, stride * ((4 << slw) + 2));
|
|
1340
|
+
let hiMag = 0;
|
|
1341
|
+
const loCtx = (lv, x, y) => {
|
|
1342
|
+
let mg = levels[lv + 1] + levels[lv + stride];
|
|
1343
|
+
mg += levels[lv + stride + 1];
|
|
1344
|
+
hiMag = mg;
|
|
1345
|
+
mg += levels[lv + 2] + levels[lv + 2 * stride];
|
|
1346
|
+
const off = LO_CTX_OFFSETS[offsets + Math.min(y, 4) * 5 + Math.min(x, 4)];
|
|
1347
|
+
return off + (mg > 512 ? 4 : mg + 64 >> 7);
|
|
1348
|
+
};
|
|
1349
|
+
let rc = order[eob], x = rc >> shift, y = rc & mask;
|
|
1350
|
+
let tok = tokOf(q[rc]);
|
|
1351
|
+
const ctx = 1 + (eob > 2 << tx2d ? 1 : 0) + (eob > 4 << tx2d ? 1 : 0);
|
|
1352
|
+
w.symbol(cdf, eobCdf + ctx * 4, 2, Math.min(tok, 3) - 1);
|
|
1353
|
+
if (tok >= 3)
|
|
1354
|
+
w.hiTok(cdf, hiCdf + ((x | y) > 1 ? 14 : 7) * 4, tok);
|
|
1355
|
+
levels[x * stride + y] = tok >= 3 ? tok + (3 << 6) : tok * 0x41;
|
|
1356
|
+
for (let i = eob - 1; i > 0; i--) {
|
|
1357
|
+
rc = order[i];
|
|
1358
|
+
x = rc >> shift;
|
|
1359
|
+
y = rc & mask;
|
|
1360
|
+
const lv = x * stride + y;
|
|
1361
|
+
const c = loCtx(lv, x, y);
|
|
1362
|
+
tok = tokOf(q[rc]);
|
|
1363
|
+
w.symbol(cdf, loCdf + c * 4, 3, Math.min(tok, 3));
|
|
1364
|
+
if (tok >= 3) {
|
|
1365
|
+
const mag = hiMag & 63;
|
|
1366
|
+
w.hiTok(cdf, hiCdf + (((y | x) > 1 ? 14 : 7) + (mag > 12 ? 6 : mag + 1 >> 1)) * 4, tok);
|
|
1367
|
+
levels[lv] = tok + (3 << 6);
|
|
1368
|
+
}
|
|
1369
|
+
else
|
|
1370
|
+
levels[lv] = tok * 0x41;
|
|
1371
|
+
}
|
|
1372
|
+
tok = tokOf(q[0]);
|
|
1373
|
+
w.symbol(cdf, loCdf, 3, Math.min(tok, 3));
|
|
1374
|
+
if (tok >= 3) {
|
|
1375
|
+
const mag = levels[1] + levels[stride] + levels[stride + 1] & 63;
|
|
1376
|
+
w.hiTok(cdf, hiCdf + (mag > 12 ? 6 : mag + 1 >> 1) * 4, tok);
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
else {
|
|
1380
|
+
const tok = tokOf(q[0]);
|
|
1381
|
+
w.symbol(cdf, eobCdf, 2, Math.min(tok, 3) - 1);
|
|
1382
|
+
if (tok >= 3)
|
|
1383
|
+
w.hiTok(cdf, hiCdf, tok);
|
|
1384
|
+
}
|
|
1385
|
+
let culLevel = 0, dcSignLevel = 1 << 6;
|
|
1386
|
+
if (q[0]) {
|
|
1387
|
+
let sctx2 = 0;
|
|
1388
|
+
for (let i = 0; i < aw; i++)
|
|
1389
|
+
sctx2 += aArr[a + i] >> 6;
|
|
1390
|
+
for (let i = 0; i < lh; i++)
|
|
1391
|
+
sctx2 += lArr[l + i] >> 6;
|
|
1392
|
+
sctx2 -= aw + lh;
|
|
1393
|
+
const dcSignCtx = (sctx2 !== 0 ? 1 : 0) + (sctx2 > 0 ? 1 : 0);
|
|
1394
|
+
const sign = q[0] < 0 ? 1 : 0;
|
|
1395
|
+
w.boolAdapt(cdf, CDF.dcSign + (chroma * 3 + dcSignCtx) * 2, sign);
|
|
1396
|
+
dcSignLevel = sign ? 0 : 2 << 6;
|
|
1397
|
+
if (Math.abs(q[0]) >= 15)
|
|
1398
|
+
w.golomb(Math.abs(q[0]) - 15);
|
|
1399
|
+
culLevel = Math.abs(q[0]);
|
|
1400
|
+
}
|
|
1401
|
+
for (let i = 1; i <= eob; i++) {
|
|
1402
|
+
const v = q[order[i]];
|
|
1403
|
+
if (!v)
|
|
1404
|
+
continue;
|
|
1405
|
+
w.bool(v < 0 ? 1 : 0);
|
|
1406
|
+
if (Math.abs(v) >= 15)
|
|
1407
|
+
w.golomb(Math.abs(v) - 15);
|
|
1408
|
+
culLevel += Math.abs(v);
|
|
1409
|
+
}
|
|
1410
|
+
const resCtx = Math.min(culLevel, 63) | dcSignLevel;
|
|
1411
|
+
aArr.fill(resCtx, a, a + ctw);
|
|
1412
|
+
lArr.fill(resCtx, l, l + cth);
|
|
1413
|
+
}
|
|
1414
|
+
}
|
|
1415
|
+
function level(width, height) {
|
|
1416
|
+
const area = width * height;
|
|
1417
|
+
if (area <= 2228224 && width <= 4096 && height <= 2176)
|
|
1418
|
+
return 8;
|
|
1419
|
+
if (area <= 8912896 && width <= 8192 && height <= 4352)
|
|
1420
|
+
return 12;
|
|
1421
|
+
if (area <= 35651584 && width <= 16384 && height <= 8704)
|
|
1422
|
+
return 16;
|
|
1423
|
+
return 31;
|
|
1424
|
+
}
|
|
1425
|
+
export function encodeAV1(image, params) {
|
|
1426
|
+
const { width, height, layout } = image;
|
|
1427
|
+
const bitsFor = (v) => Math.max(1, 32 - Math.clz32(v - 1));
|
|
1428
|
+
const seq = {
|
|
1429
|
+
profile: layout === 3 ? 1 : 0, reducedStillPicture: true, maxWidth: width, maxHeight: height,
|
|
1430
|
+
widthBits: bitsFor(width), heightBits: bitsFor(height), frameIdBits: 0, deltaFrameIdBits: 0,
|
|
1431
|
+
decoderModelInfo: false, equalPictureInterval: false, bufferRemovalDelayLength: 0, framePresentationDelayLength: 0,
|
|
1432
|
+
operatingPoints: [{ idc: 0, decoderModel: false }], sb128: false, filterIntra: false, intraEdgeFilter: true,
|
|
1433
|
+
screenContentTools: 2, forceIntegerMv: 2, orderHintBits: 0, superres: false, cdef: true, restoration: false,
|
|
1434
|
+
bitDepth: 8, monochrome: layout === 0, colorPrimaries: image.cicp[0], transferCharacteristics: image.cicp[1],
|
|
1435
|
+
matrixCoefficients: image.cicp[2], fullRange: true, ssx: layout === 1 ? 1 : 0, ssy: layout === 1 ? 1 : 0,
|
|
1436
|
+
separateUvDeltaQ: false, filmGrain: false
|
|
1437
|
+
};
|
|
1438
|
+
const sbw = width + 63 >> 6, sbh = height + 63 >> 6;
|
|
1439
|
+
const tileLog2 = (size, target) => {
|
|
1440
|
+
let k = 0;
|
|
1441
|
+
while (size << k < target)
|
|
1442
|
+
k++;
|
|
1443
|
+
return k;
|
|
1444
|
+
};
|
|
1445
|
+
const log2Cols = tileLog2(64, sbw), log2Rows = Math.max(tileLog2(2304, sbw * sbh) - log2Cols, 0);
|
|
1446
|
+
const colStarts = [], rowStarts = [];
|
|
1447
|
+
for (let x = 0; x < sbw; x += 1 + (sbw - 1 >> log2Cols))
|
|
1448
|
+
colStarts.push(x);
|
|
1449
|
+
for (let y = 0; y < sbh; y += 1 + (sbh - 1 >> log2Rows))
|
|
1450
|
+
rowStarts.push(y);
|
|
1451
|
+
const cols = colStarts.length, rows = rowStarts.length;
|
|
1452
|
+
colStarts.push(sbw);
|
|
1453
|
+
rowStarts.push(sbh);
|
|
1454
|
+
const qidx = params.qidx;
|
|
1455
|
+
const hdr = {
|
|
1456
|
+
showExistingFrame: false, existingFrameIdx: 0, frameType: 0, showFrame: true, refreshFrameFlags: 0xff,
|
|
1457
|
+
disableCdfUpdate: false, allowScreenContentTools: true, allowIntrabc: false, width, upscaledWidth: width, height,
|
|
1458
|
+
superresDenominator: 8,
|
|
1459
|
+
tiles: { cols, rows, log2Cols, log2Rows, colStarts, rowStarts, contextUpdateId: 0, sizeBytes: 4 },
|
|
1460
|
+
quant: { yac: qidx, ydcDelta: 0, udcDelta: 0, uacDelta: 0, vdcDelta: 0, vacDelta: 0, qm: false, qmY: 15, qmU: 15, qmV: 15 },
|
|
1461
|
+
segmentation: { enabled: false, updateMap: false, data: [], preskip: false, lastActiveSegId: -1, qidx: new Array(8).fill(qidx), lossless: new Array(8).fill(!qidx) },
|
|
1462
|
+
allLossless: !qidx,
|
|
1463
|
+
deltaQ: { present: false, resLog2: 0 },
|
|
1464
|
+
deltaLf: { present: false, resLog2: 0, multi: false },
|
|
1465
|
+
loopFilter: { levelY: [0, 0], levelU: 0, levelV: 0, sharpness: 0, modeRefDeltaEnabled: false, refDeltas: [1, 0, 0, 0, -1, 0, -1, -1], modeDeltas: [0, 0] },
|
|
1466
|
+
cdef: { damping: 3, bits: 0, yStrengths: [0], uvStrengths: [0] },
|
|
1467
|
+
restoration: { types: [RESTORATION_NONE, RESTORATION_NONE, RESTORATION_NONE], unitSizeLog2: [8, 8] },
|
|
1468
|
+
txMode: qidx ? params.effort > 1 ? 2 : 1 : 0, reducedTxSet: false, filmGrain: null
|
|
1469
|
+
};
|
|
1470
|
+
const f = new Frame(seq, hdr);
|
|
1471
|
+
const src = f.planes.map((plane, p) => {
|
|
1472
|
+
const sx = p && layout === 1 ? 1 : 0, w = width + sx >> sx, h = height + sx >> sx, stride = f.strides[p];
|
|
1473
|
+
const pw = f.bw * 4 >> sx, ph = f.bh * 4 >> sx, out = new Uint16Array(plane.length), inp = image.planes[p];
|
|
1474
|
+
for (let y = 0; y < ph; y++)
|
|
1475
|
+
for (let x = 0; x < pw; x++)
|
|
1476
|
+
out[y * stride + x] = inp[Math.min(y, h - 1) * w + Math.min(x, w - 1)];
|
|
1477
|
+
return out;
|
|
1478
|
+
});
|
|
1479
|
+
const encoder = new TileEncoder(f, src, params);
|
|
1480
|
+
const nodes = [];
|
|
1481
|
+
for (let r = 0; r < rows; r++)
|
|
1482
|
+
for (let c = 0; c < cols; c++)
|
|
1483
|
+
nodes.push(encoder.searchTile(r, c));
|
|
1484
|
+
for (let r = 0; r < rows; r++)
|
|
1485
|
+
for (let c = 0; c < cols; c++)
|
|
1486
|
+
encoder.storeTile(r, c, nodes[r * cols + c]);
|
|
1487
|
+
if (qidx)
|
|
1488
|
+
chooseFilters(f, src, qidx, params.effort, encoder.cdefChoice);
|
|
1489
|
+
f.cdefIdx.fill(-1);
|
|
1490
|
+
const tiles = [];
|
|
1491
|
+
for (let r = 0; r < rows; r++)
|
|
1492
|
+
for (let c = 0; c < cols; c++)
|
|
1493
|
+
tiles.push(encoder.writeTile(r, c, nodes[r * cols + c]));
|
|
1494
|
+
const w = new HeaderWriter();
|
|
1495
|
+
writeFrameHeader(w, seq, hdr);
|
|
1496
|
+
w.align();
|
|
1497
|
+
if (cols * rows > 1)
|
|
1498
|
+
w.bit(0);
|
|
1499
|
+
w.align();
|
|
1500
|
+
const head = w.data();
|
|
1501
|
+
let size = head.length;
|
|
1502
|
+
for (let i = 0; i < tiles.length; i++)
|
|
1503
|
+
size += tiles[i].length + (i < tiles.length - 1 ? 4 : 0);
|
|
1504
|
+
const payload = new Uint8Array(size);
|
|
1505
|
+
payload.set(head);
|
|
1506
|
+
let o = head.length;
|
|
1507
|
+
for (let i = 0; i < tiles.length; i++) {
|
|
1508
|
+
if (i < tiles.length - 1) {
|
|
1509
|
+
const n = tiles[i].length - 1;
|
|
1510
|
+
payload.set([n & 0xff, n >> 8 & 0xff, n >> 16 & 0xff, n >>> 24], o);
|
|
1511
|
+
o += 4;
|
|
1512
|
+
}
|
|
1513
|
+
payload.set(tiles[i], o);
|
|
1514
|
+
o += tiles[i].length;
|
|
1515
|
+
}
|
|
1516
|
+
const seqObu = writeSequenceHeader(seq, level(width, height)), frameObu = obu(OBU_FRAME, payload);
|
|
1517
|
+
const out = new Uint8Array(seqObu.length + frameObu.length);
|
|
1518
|
+
out.set(seqObu);
|
|
1519
|
+
out.set(frameObu, seqObu.length);
|
|
1520
|
+
return out;
|
|
1521
|
+
}
|
|
1522
|
+
function blockErrors(f, planes, src, ey, euv) {
|
|
1523
|
+
const s64 = f.b4Stride >> 4;
|
|
1524
|
+
ey.fill(0);
|
|
1525
|
+
euv.fill(0);
|
|
1526
|
+
planes.forEach((plane, p) => {
|
|
1527
|
+
const sx = p ? f.ssx : 0, sy = p ? f.ssy : 0, w = f.width + sx >> sx, h = f.height + sy >> sy, stride = f.strides[p];
|
|
1528
|
+
const out = p ? euv : ey;
|
|
1529
|
+
for (let y = 0; y < h; y++) {
|
|
1530
|
+
const row = (y << sy >> 6) * s64;
|
|
1531
|
+
for (let x = 0; x < w; x++) {
|
|
1532
|
+
const d = plane[y * stride + x] - src[p][y * stride + x];
|
|
1533
|
+
out[row + (x << sx >> 6)] += d * d;
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
const sum = (a) => a.reduce((s, v) => s + v, 0);
|
|
1539
|
+
function chooseFilters(f, src, qidx, effort, choice) {
|
|
1540
|
+
const hdr = f.hdr, q = dequant(0, qidx, 1), n = f.cdefIdx.length;
|
|
1541
|
+
const recon = f.planes.map(p => p.slice());
|
|
1542
|
+
const ey = new Float64Array(n), euv = new Float64Array(n);
|
|
1543
|
+
const setLevel = (level) => {
|
|
1544
|
+
hdr.loopFilter.levelY = [level, level];
|
|
1545
|
+
hdr.loopFilter.levelU = hdr.loopFilter.levelV = level;
|
|
1546
|
+
for (let i = 0; i < f.lfLevels.length; i += 4)
|
|
1547
|
+
f.lfLevels.set([level, level, level, level], i);
|
|
1548
|
+
};
|
|
1549
|
+
const guess = Math.max(0, Math.min(63, (q * 20723 + 1015158 + (1 << 17) >> 18) - 4));
|
|
1550
|
+
let bestLevel = guess, bestError = Infinity;
|
|
1551
|
+
for (const level of [[guess], [guess - 4, guess, guess + 4], [guess - 6, guess - 3, guess, guess + 3, guess + 6]][effort]) {
|
|
1552
|
+
if (level < 0 || level > 63)
|
|
1553
|
+
continue;
|
|
1554
|
+
setLevel(level);
|
|
1555
|
+
f.planes = recon.map(p => p.slice());
|
|
1556
|
+
deblock(f);
|
|
1557
|
+
blockErrors(f, f.planes, src, ey, euv);
|
|
1558
|
+
const e = sum(ey) + sum(euv);
|
|
1559
|
+
if (e < bestError) {
|
|
1560
|
+
bestError = e;
|
|
1561
|
+
bestLevel = level;
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
setLevel(bestLevel);
|
|
1565
|
+
f.planes = recon.map(p => p.slice());
|
|
1566
|
+
deblock(f);
|
|
1567
|
+
const deblocked = f.planes;
|
|
1568
|
+
const strengths = [[0, 5, 9, 17, 33], [0, 1, 4, 5, 9, 17, 33, 61], [0, 1, 4, 5, 8, 9, 13, 17, 25, 33, 41, 61]][effort];
|
|
1569
|
+
const errY = strengths.map(() => new Float64Array(n)), errUv = strengths.map(() => new Float64Array(n));
|
|
1570
|
+
const coded = Array.from(f.cdefIdx, i => i >= 0), dirs = new Int32Array(f.b4Stride * (f.bh + 1)).fill(-1);
|
|
1571
|
+
strengths.forEach((strength, k) => {
|
|
1572
|
+
hdr.cdef.yStrengths = [strength];
|
|
1573
|
+
hdr.cdef.uvStrengths = [strength];
|
|
1574
|
+
f.planes = deblocked.map(p => p.slice());
|
|
1575
|
+
f.cdefIdx.set(Int8Array.from(coded, c => c ? 0 : -1));
|
|
1576
|
+
if (strength)
|
|
1577
|
+
cdef(f, deblocked, dirs);
|
|
1578
|
+
blockErrors(f, f.planes, src, errY[k], errUv[k]);
|
|
1579
|
+
});
|
|
1580
|
+
const lambda = 0.08 * (q / 8) ** 2, blocks = coded.filter(c => c).length;
|
|
1581
|
+
const pairs = [];
|
|
1582
|
+
for (let y = 0; y < strengths.length; y++)
|
|
1583
|
+
for (let u = 0; u < strengths.length; u++)
|
|
1584
|
+
pairs.push([y, u]);
|
|
1585
|
+
const costOf = (set) => {
|
|
1586
|
+
let e = 0;
|
|
1587
|
+
for (let i = 0; i < n; i++) {
|
|
1588
|
+
if (!coded[i])
|
|
1589
|
+
continue;
|
|
1590
|
+
let m = Infinity;
|
|
1591
|
+
for (const [y, u] of set)
|
|
1592
|
+
m = Math.min(m, errY[y][i] + errUv[u][i]);
|
|
1593
|
+
e += m;
|
|
1594
|
+
}
|
|
1595
|
+
return e;
|
|
1596
|
+
};
|
|
1597
|
+
let set = [], best = [[0, 0]], bestCost = Infinity;
|
|
1598
|
+
for (let bits = 0; bits <= (effort ? 3 : 1); bits++) {
|
|
1599
|
+
while (set.length < 1 << bits) {
|
|
1600
|
+
let add = pairs[0], addCost = Infinity;
|
|
1601
|
+
for (const pair of pairs) {
|
|
1602
|
+
const c = costOf([...set, pair]);
|
|
1603
|
+
if (c < addCost) {
|
|
1604
|
+
addCost = c;
|
|
1605
|
+
add = pair;
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
set = [...set, add];
|
|
1609
|
+
}
|
|
1610
|
+
const cost = costOf(set) + lambda * (bits * blocks + 12 * set.length);
|
|
1611
|
+
if (cost < bestCost) {
|
|
1612
|
+
bestCost = cost;
|
|
1613
|
+
best = set.slice();
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
hdr.cdef.bits = Math.log2(best.length);
|
|
1617
|
+
hdr.cdef.yStrengths = best.map(([y]) => strengths[y]);
|
|
1618
|
+
hdr.cdef.uvStrengths = best.map(([, u]) => strengths[u]);
|
|
1619
|
+
for (let i = 0; i < n; i++) {
|
|
1620
|
+
let m = Infinity;
|
|
1621
|
+
best.forEach(([y, u], k) => {
|
|
1622
|
+
const e = errY[y][i] + errUv[u][i];
|
|
1623
|
+
if (e < m) {
|
|
1624
|
+
m = e;
|
|
1625
|
+
choice[i] = k;
|
|
1626
|
+
}
|
|
1627
|
+
});
|
|
1628
|
+
}
|
|
1629
|
+
f.planes = recon;
|
|
1630
|
+
}
|