-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrx.html
More file actions
1274 lines (1245 loc) · 50.7 KB
/
Copy pathrx.html
File metadata and controls
1274 lines (1245 loc) · 50.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html><head><meta charset="utf-8"><title>heliogram rx</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body{margin:0;background:#000;color:#0f0;font:14px monospace}
/* CONTAIN, not cover: the preview must show exactly the frame the decoder
samples. `cover` crops the edges, so the code could be clipped out of the
real capture while still looking fully in frame here. Letterboxing is the
honest choice when you are aiming by eye. */
#v{width:100vw;height:52vh;object-fit:contain;background:#000;display:block}
#hud{padding:10px;white-space:pre-wrap}
/* Landscape is the RIGHT way to hold this (a wide code in a wide frame),
so the layout must not waste it: shrink the video band and the chrome so
the numbers and guidance stay readable beside it. */
@media (orientation: landscape){
#v{height:58vh}
body{font-size:12px}
#guide{font-size:13px !important;padding:4px 8px !important}
#hud{padding:4px 8px;font-size:12px}
#bar{height:8px;margin:4px 8px}
button{font-size:12px;margin:4px 6px;padding:5px 9px}
}
#bar{height:14px;background:#123;margin:8px 10px;border:1px solid #0f0}
#fill{height:100%;width:0%;background:#0f0}
button{font:16px monospace;margin:8px 10px;padding:8px 14px}
a{color:#8ff}
</style></head><body>
<video id="v" autoplay playsinline muted></video>
<div id="guide" style="padding:8px 10px;font-size:17px;color:#f80">point the camera at the sending screen</div>
<div id="bar"><div id="fill"></div></div>
<div id="hud">loading rxmeta…</div>
<button id="go">START CAMERA</button>
<button id="st">RS SELF-TEST</button>
<button id="sf">OPTICS SELF-TEST</button>
<div id="out"></div>
<script>
// heliogram browser receiver: realtime screen-to-camera file transfer.
// Every deterministic artifact (fountain composition, whitening masks,
// cell orders) is precomputed server-side into rxmeta.json; this page
// does signal processing, RS+CRC certification, ghost defenses, and the
// fountain, live on every camera frame.
const $ = id => document.getElementById(id);
let M = null; // rxmeta
// JS-SENDER MODE (rx.html?js=1): pairs with hgtx.html, where the file is
// chosen in the browser. Sender and receiver then share ONE deterministic
// PRNG for the fountain instead of consulting precomputed numpy tables, so
// any dropped file works; k and size come from the header.
let JSMODE = new URLSearchParams(location.search).get("js") === "1";
// A build without the precomputed composition table can ONLY be the
// browser-sender pair, so imply the mode rather than making a public
// visitor know to add ?js=1.
let dynK = 0, dynCum = null, dynSize = 0;
function mulberry32(a){
return function(){
a |= 0; a = (a + 0x6D2B79F5) | 0;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function robustSoliton(k){
const c = 0.03, delta = 0.5;
const s = c * Math.log(k / delta) * Math.sqrt(k);
const pmf = new Float64Array(k + 1);
pmf[1] = 1 / k;
for (let d = 2; d <= k; d++) pmf[d] = 1 / (d * (d - 1));
const pivot = Math.max(1, Math.min(k, Math.round(k / s)));
for (let d = 1; d < pivot; d++) pmf[d] += s / (k * d);
if (s > 1) pmf[pivot] += s * Math.log(s / delta) / k;
let tot = 0;
for (let d = 1; d <= k; d++) tot += pmf[d];
const cum = new Float64Array(k + 1);
let run = 0;
for (let d = 1; d <= k; d++){ run += pmf[d] / tot; cum[d] = run; }
return cum;
}
function blockIndices(seq, k, cum){
const rng = mulberry32(seq ^ 0x9E3779B9);
const u = rng();
let d = 1;
while (d < k && cum[d] < u) d++;
const idx = [], seen = new Set();
while (idx.length < d){
const i = Math.floor(rng() * k) % k;
if (!seen.has(i)){ seen.add(i); idx.push(i); }
}
return idx;
}
function idxFor(flat){
if (JSMODE) return dynK ? blockIndices(flat, dynK, dynCum) : null;
return flat < M.indices.length ? M.indices[flat] : null;
}
const b64 = s => Uint8Array.from(atob(s), c => c.charCodeAt(0));
// ---------- GF(256), prim 0x11d ----------
const EXP = new Uint8Array(512), LOG = new Int32Array(256);
(() => { let x = 1;
for (let i = 0; i < 255; i++){ EXP[i] = x; LOG[x] = i;
x <<= 1; if (x & 0x100) x ^= 0x11d; }
for (let i = 255; i < 512; i++) EXP[i] = EXP[i - 255]; })();
const gmul = (a,b) => (a===0||b===0) ? 0 : EXP[LOG[a]+LOG[b]];
const gdiv = (a,b) => a===0 ? 0 : EXP[(LOG[a]+255-LOG[b])%255];
const ginv = x => EXP[255-LOG[x]];
const gpowg = p => { let e = p % 255; if (e < 0) e += 255; return EXP[e]; };
function polyEval(p, lp, x){
let y = p[0];
for (let i = 1; i < lp; i++) y = gmul(y, x) ^ p[i];
return y;
}
function polyMul(a, la, b, lb, out){
const lo = la + lb - 1; out.fill(0, 0, lo);
for (let i = 0; i < la; i++) for (let j = 0; j < lb; j++)
out[i+j] ^= gmul(a[i], b[j]);
return lo;
}
function calcSynd(msg, n, nsym, synd){
let mx = 0; synd[0] = 0;
for (let i = 0; i < nsym; i++){
const v = polyEval(msg, n, gpowg(i)); synd[1+i] = v; if (v > mx) mx = v;
}
return mx;
}
// exact port of crs2.c correct_msg (itself a line-by-line reedsolo port)
function correctMsg(msg, n, nsym, erase, nErase){
const synd = new Uint8Array(64), fsynd = new Uint8Array(64);
let errLoc = new Uint8Array(128), oldLoc = new Uint8Array(128);
const tmp = new Uint8Array(128);
if (nErase > nsym) return false;
for (let i = 0; i < nErase; i++) msg[erase[i]] = 0;
if (calcSynd(msg, n, nsym, synd) === 0) return true;
const lf = nsym;
for (let i = 0; i < lf; i++) fsynd[i] = synd[1+i];
for (let i = 0; i < nErase; i++){
const x = gpowg(n - 1 - erase[i]);
for (let j = 0; j < lf - 1; j++)
fsynd[j] = gmul(fsynd[j], x) ^ fsynd[j+1];
}
let le = 1, lo_ = 1; errLoc[0] = 1; oldLoc[0] = 1;
for (let i = 0; i < nsym - nErase; i++){
const K = i;
let delta = fsynd[K];
for (let j = 1; j < le; j++)
delta ^= gmul(errLoc[le-1-j], fsynd[K-j]);
oldLoc[lo_++] = 0;
if (delta !== 0){
if (lo_ > le){
for (let j = 0; j < lo_; j++) tmp[j] = gmul(oldLoc[j], delta);
const di = ginv(delta);
for (let j = 0; j < le; j++) oldLoc[j] = gmul(errLoc[j], di);
const t = lo_; lo_ = le; le = t;
errLoc.set(tmp.subarray(0, le));
}
const lm = Math.max(le, lo_);
const sum = new Uint8Array(lm);
for (let j = 0; j < le; j++) sum[lm-le+j] ^= errLoc[j];
for (let j = 0; j < lo_; j++) sum[lm-lo_+j] ^= gmul(oldLoc[j], delta);
errLoc.set(sum.subarray(0, lm)); le = lm;
}
}
let lead = 0; while (lead < le && errLoc[lead] === 0) lead++;
le -= lead;
const eloc = errLoc.subarray(lead, lead + le);
const errs = le - 1;
if ((errs - nErase) * 2 + nErase > nsym) return false;
const rev = new Uint8Array(le);
for (let i = 0; i < le; i++) rev[i] = eloc[le-1-i];
const errPos = [];
for (let i = 0; i < n; i++)
if (polyEval(rev, le, gpowg(i)) === 0) errPos.push(n - 1 - i);
if (errPos.length !== errs) return false;
const pos = [];
for (let i = 0; i < nErase; i++) pos.push(erase[i]);
for (const p of errPos) pos.push(p);
const np = pos.length;
const coef = pos.map(p => n - 1 - p);
let eloc2 = new Uint8Array(220); eloc2[0] = 1; let lel = 1;
const prod = new Uint8Array(300);
for (let i = 0; i < np; i++){
const term = new Uint8Array([gpowg(coef[i]), 1]);
const lp = polyMul(eloc2, lel, term, 2, prod);
eloc2.set(prod.subarray(0, lp)); lel = lp;
}
const ls = nsym + 1;
const srev = new Uint8Array(ls);
for (let i = 0; i < ls; i++) srev[i] = synd[ls-1-i];
const prod2 = new Uint8Array(400);
const lp2 = polyMul(srev, ls, eloc2, lel, prod2);
const lev = lel;
const evalR = new Uint8Array(lev);
for (let i = 0; i < lev; i++) evalR[i] = prod2[lp2-1-i];
const X = coef.map(c => gpowg(-(255 - c)));
const E = new Uint8Array(n);
const evrev = new Uint8Array(lev);
for (let j = 0; j < lev; j++) evrev[j] = evalR[lev-1-j];
for (let i = 0; i < np; i++){
const Xi = X[i], XiInv = ginv(Xi);
let prime = 1;
for (let j = 0; j < np; j++)
if (j !== i) prime = gmul(prime, 1 ^ gmul(XiInv, X[j]));
if (prime === 0) return false;
let y = polyEval(evrev, lev, XiInv);
y = gmul(Xi, y);
E[pos[i]] = gdiv(y, prime);
}
for (let i = 0; i < n; i++) msg[i] ^= E[i];
return calcSynd(msg, n, nsym, synd) === 0;
}
// ---------- CRC32 ----------
const CRC_T = new Uint32Array(256);
(() => { for (let n = 0; n < 256; n++){ let c = n;
for (let k = 0; k < 8; k++) c = (c & 1) ? 0xEDB88320 ^ (c >>> 1) : c >>> 1;
CRC_T[n] = c >>> 0; } })();
function crc32(buf, off, len){
let c = 0xFFFFFFFF;
for (let i = 0; i < len; i++)
c = CRC_T[(c ^ buf[off+i]) & 0xFF] ^ (c >>> 8);
return (c ^ 0xFFFFFFFF) >>> 0;
}
function crc16z(buf, len){ // zlib.crc32 & 0xFFFF over the body
return crc32(buf, 0, len) & 0xFFFF;
}
// certify one codeword: hard, then erasure ladder, then CRC32 gate
function certify(chunk, order, ecc, sub){
const msg = new Uint8Array(255);
msg.set(chunk);
let ok = correctMsg(msg, 255, ecc, null, 0);
if (!ok && order){
// order may be a factory: the confidence sort costs a 255-element sort
// per codeword and is only needed once hard decode has failed.
const ord = typeof order === "function" ? order() : order;
if (ord){
const top = Math.floor(ecc * 0.7);
for (let nEr = 4; nEr <= top && !ok; nEr += 6){
msg.set(chunk);
ok = correctMsg(msg, 255, ecc, ord, nEr);
}
}
}
if (!ok) return null;
const want = (msg[0] | (msg[1]<<8) | (msg[2]<<16) | (msg[3]<<24)) >>> 0;
if (crc32(msg, 4, sub) !== want) return null;
return msg.slice(4, 4 + sub);
}
// ---------- header ----------
function tryHeader(bits, conf, capEr){
// bits: Uint8Array over header cells (MSB-first bytes). conf, when given,
// is the per-cell |lum - local mean| margin for the same cells, and buys
// the header the SAME confidence-ordered erasure ladder the payload gets.
// The header is the bootstrap: nothing pools, nothing tracks, and the bar
// never moves until it certifies, so it must not be the weakest code on
// the screen.
const nB = M.hdr_len_enc;
const raw = new Uint8Array(nB);
for (let i = 0; i < nB; i++){
let v = 0;
for (let b = 0; b < 8; b++) v = (v << 1) | bits[i*8+b];
raw[i] = v;
}
let order = null;
if (conf){
const bc = new Float32Array(nB);
for (let i = 0; i < nB; i++){
let m = 1e9;
for (let b = 0; b < 8; b++) if (conf[i*8+b] < m) m = conf[i*8+b];
bc[i] = m;
}
order = Array.from({length: nB}, (_, i) => i)
.sort((a, b) => bc[a] - bc[b]);
}
const attempt = nEr => {
for (let ph = 0; ph < M.hdr_phases; ph++){
const mask = HDR_MASKS[ph];
const dew = new Uint8Array(nB);
for (let i = 0; i < nB; i++) dew[i] = raw[i] ^ mask[i];
if (!correctMsg(dew, nB, M.hdr_ecc, nEr ? order : null, nEr)) continue;
const body = dew.subarray(0, M.hdr_len_pre);
if (body[0]!==0x53||body[1]!==0x43||body[2]!==0x50||body[3]!==0x43)
continue; // "SCPC"
const crc = body[M.hdr_len_pre-2] | (body[M.hdr_len_pre-1] << 8);
if (crc16z(body, M.hdr_len_pre - 2) !== crc) continue;
const dv = new DataView(body.buffer, body.byteOffset);
const seq = dv.getUint32(6, true);
if (seq % M.hdr_phases !== ph) continue;
return { seq, k: dv.getUint32(10, true),
blockSize: dv.getUint16(14, true),
fileSize: dv.getUint32(16, true) };
}
return null;
};
let hd = attempt(0);
if (!hd && order){
// capEr bounds the ladder on paths that run every frame (tracking);
// the full ladder is for the once-per-frame chosen geometry.
let top = Math.floor(M.hdr_ecc * 0.7);
if (capEr && capEr < top) top = capEr;
for (let nEr = 4; !hd && nEr <= top; nEr += 6) hd = attempt(nEr);
}
return hd;
}
// ---------- finder detection: QR-style 1:1:3:1:1 run scan ----------
function runsAt(get, n, th){
const runs = []; let last = get(0) < th ? 1 : 0, len = 1;
for (let i = 1; i < n; i++){
const d = get(i) < th ? 1 : 0;
if (d === last) len++; else { runs.push([last, len, i-len]); last = d; len = 1; }
}
runs.push([last, len, n-len]);
return runs;
}
// centre and module size of a 1:1:3:1:1 dark-light-dark-light-dark pattern
// containing index p, or null. This is the standard QR finder test.
function ratioAt(runs, p){
for (let i = 0; i + 4 < runs.length; i++){
if (runs[i][0] !== 1) continue;
const q = runs.slice(i, i+5);
const [a,b,c,d,e] = q.map(r => r[1]);
const u = (a+b+c+d+e) / 7;
if (u < 1.5) continue;
const s0 = q[0][2], s5 = q[4][2] + q[4][1];
if (p < s0 || p > s5) continue;
if (Math.abs(a-u) < u*.6 && Math.abs(b-u) < u*.6 &&
Math.abs(c-3*u) < 3*u*.45 && Math.abs(d-u) < u*.6 &&
Math.abs(e-u) < u*.6)
return { c: q[0][2] + a + b + c/2, u };
}
return null;
}
let lastCands = 0;
function findFinders(G, w, h){
const th = otsu(G, w * h);
const cands = [];
const step = Math.max(1, Math.floor(h / 500));
for (let y = 0; y < h; y += step){
const rowGet = i => G(y*w+i);
const runs = runsAt(rowGet, w, th);
for (let i = 0; i + 4 < runs.length; i++){
if (runs[i][0] !== 1) continue;
const q = runs.slice(i, i+5);
const [a,b,c,d,e] = q.map(r => r[1]);
const u = (a+b+c+d+e) / 7;
if (u < 1.5) continue;
if (!(Math.abs(a-u) < u*.6 && Math.abs(b-u) < u*.6 &&
Math.abs(c-3*u) < 3*u*.45 && Math.abs(d-u) < u*.6 &&
Math.abs(e-u) < u*.6)) continue;
const cx = Math.round(q[0][2] + a + b + c/2);
// VERTICAL CROSS-CHECK. Dense pseudorandom payload throws accidental
// horizontal 1:1:3:1:1 runs constantly; a true finder must show the
// same ratio vertically through the same centre, with a matching
// module size. This is what separates a finder from noise.
const colGet = j => G(j*w+cx);
const vr = ratioAt(runsAt(colGet, h, th), y);
if (!vr) continue;
if (Math.abs(vr.u - u) > Math.max(u, vr.u) * 0.5) continue;
cands.push([cx, vr.c, (u + vr.u) / 2]);
}
}
lastCands = cands.length;
if (cands.length < 4) return null;
// group by proximity (a real finder yields many near-identical hits)
const groups = [];
for (const c of cands){
let g = groups.find(g0 => Math.hypot(g0.x - c[0], g0.y - c[1]) < c[2] * 6);
if (!g){ g = { x: c[0], y: c[1], n: 0, sx: 0, sy: 0 }; groups.push(g); }
g.n++; g.sx += c[0]; g.sy += c[1];
g.x = g.sx / g.n; g.y = g.sy / g.n;
}
// The payload is pseudorandom, so it throws false 1:1:3:1:1 candidates
// all over the interior; counting hits just finds the densest noise. But
// the real finders are the EXTREME points of the candidate cloud (they
// sit at the code's four corners), so take extremes of x+y and x-y. This
// is the standard robust corner extraction and it does not care how much
// interior noise there is.
const pts = groups.filter(g => g.n >= 2);
if (pts.length < 4) return null;
let tl = pts[0], br = pts[0], tr = pts[0], bl = pts[0];
for (const g of pts){
if (g.x + g.y < tl.x + tl.y) tl = g;
if (g.x + g.y > br.x + br.y) br = g;
if (g.x - g.y > tr.x - tr.y) tr = g;
if (g.x - g.y < bl.x - bl.y) bl = g;
}
const four = [tl, tr, bl, br];
// sanity: four distinct points spanning a plausible size. NO aspect gate
// here: a portrait phone sees the landscape code transposed, and gating
// on upright aspect made that unlockable (243 candidate hits, 0 locks in
// the field). Orientation is detectH's job.
const wpx = Math.hypot(tr.x-tl.x, tr.y-tl.y);
const hpx = Math.hypot(bl.x-tl.x, bl.y-tl.y);
if (wpx < 40 || hpx < 30) return null;
return four.map(g => [g.x, g.y]);
}
// Try the four orientations of the detected corner quad. Aspect prefilter
// keeps it to the plausible ones (upright: 0/180, transposed: 90/270), and
// the header, being a zero-false-accept oracle, adjudicates between the
// survivors. If no orientation certifies a header (countdown, glare), the
// first aspect-valid one still gives the guide a geometry to report.
function detectH(G, w, h){
const four = findFinders(G, w, h);
if (!four) return null;
const [tl, tr, bl, br] = four;
const A = (M.gw - 9) / (M.gh - 9);
const quads = [[[tl,tr,bl,br], 0], [[tr,br,tl,bl], 90],
[[bl,tl,br,tr], 270], [[br,bl,tr,tl], 180]];
// Hard-decode only in here: detectH may try many (orientation, k1)
// candidates per frame and the erasure ladder is the expensive path. A
// correct geometry usually decodes hard; the ladder gets ONE shot per
// frame, in finishFrame, on the chosen geometry.
const headerOf = sb => {
const hb = new Uint8Array(HC.length);
for (let i = 0; i < HC.length; i++) hb[i] = sb.bitAt[HC[i]];
return tryHeader(hb);
};
// Geometry model: the homography lives in UNDISTORTED space and each
// sample point is re-distorted by k1 (sampleBits). Fitting H on the raw
// corner points and then bending them too would perturb the four exact
// registrations; undistort-fit-redistort pins the corners by
// construction, which is how the film-pipeline decoder applies k1.
const undist = (q, k1) => {
if (!k1) return q;
const cx = w / 2, cy = h / 2, rn2 = (w*w + h*h) / 4;
return q.map(([x, y]) => {
const dx = x - cx, dy = y - cy;
const s = 1 + k1 * (dx*dx + dy*dy) / rn2;
return [cx + dx / s, cy + dy / s];
});
};
// K1 refinement, film-decoder style but throttled: the sweep accepted
// the FIRST k1 that decoded a header, and the payload optimum can sit a
// rung away. Judge = syndrome-clean codewords on this same frame, same
// quad refit per candidate, so corners stay pinned and camera motion
// cancels out of the comparison.
const refine = start => {
if (performance.now() - k1RefineAt < 2000) return start;
k1RefineAt = performance.now();
let best = start, bestN = certCount(start.sb, 12), bestK = K1;
for (const dk of [-0.005, 0.005]){
const k = +(K1 + dk).toFixed(3);
const H2 = homography(M.finders, undist(start.q, k));
if (!H2) continue;
const sb2 = sampleBits(G, w, h, H2, k);
const n2 = certCount(sb2, 12);
if (n2 > bestN + 1){
const hd2 = headerOf(sb2);
if (hd2){ best = { H: H2, sb: sb2, rot: start.rot, hd: hd2,
q: start.q };
bestN = n2; bestK = k; }
}
}
K1 = bestK;
return best;
};
let fallback = null, prefQ = null, prefRot = 0;
for (const [q, rot] of quads){
const wpx = Math.hypot(q[1][0]-q[0][0], q[1][1]-q[0][1]);
const hpx = Math.hypot(q[2][0]-q[0][0], q[2][1]-q[0][1]);
const r = (wpx / hpx) / A;
if (r < 0.6 || r > 1.7) continue;
const H = homography(M.finders, undist(q, K1));
if (!H) continue;
if (!prefQ){ prefQ = q; prefRot = rot; }
const sb = sampleBits(G, w, h, H, K1);
const hd = headerOf(sb);
if (hd) return refine({ H, sb, rot, hd, q });
if (!fallback) fallback = { H, sb, rot, hd: null, q };
}
// k1 sweep, header-certified, rate-limited. Runs only while nothing
// decodes at the current K1, on the first plausible orientation, with
// the homography refit per candidate.
if (prefQ && performance.now() - k1SweepAt > 800){
k1SweepAt = performance.now();
for (const k1 of K1LADDER){
if (k1 === K1) continue;
const H = homography(M.finders, undist(prefQ, k1));
if (!H) continue;
const sb = sampleBits(G, w, h, H, k1);
const hd = headerOf(sb);
if (hd){ K1 = k1; return { H, sb, rot: prefRot, hd, q: prefQ }; }
}
}
return fallback;
}
function otsu(G, n){
const hist = new Uint32Array(256);
for (let i = 0; i < n; i += 13) hist[G(i)]++;
let total = 0, sum = 0;
for (let i = 0; i < 256; i++){ total += hist[i]; sum += i * hist[i]; }
let sumB = 0, wB = 0, best = 127, bv = -1;
for (let t = 0; t < 256; t++){
wB += hist[t]; if (!wB) continue;
const wF = total - wB; if (!wF) break;
sumB += t * hist[t];
const mB = sumB / wB, mF = (sum - sumB) / wF;
const v = wB * wF * (mB - mF) * (mB - mF);
// Return the MIDPOINT BETWEEN CLASS MEANS, not the argmax index. On a
// two-level image (which a rendered code frame is) every split between
// the levels has identical between-class variance, so argmax returns
// t=0 and `pixel < 0` classifies nothing as dark: the whole frame reads
// as one light run and no finder can ever be found.
if (v > bv){ bv = v; best = (mB + mF) / 2; }
}
return best;
}
const median = a => { const s = [...a].sort((x,y)=>x-y);
return s[s.length >> 1]; };
// homography from 4 correspondences (DLT, 8x8 gaussian elimination)
function homography(src, dst){
const A = [], b = [];
for (let i = 0; i < 4; i++){
const [x,y] = src[i], [u,v] = dst[i];
A.push([x,y,1,0,0,0,-u*x,-u*y]); b.push(u);
A.push([0,0,0,x,y,1,-v*x,-v*y]); b.push(v);
}
const n = 8;
for (let c = 0; c < n; c++){
let p = c;
for (let r = c+1; r < n; r++)
if (Math.abs(A[r][c]) > Math.abs(A[p][c])) p = r;
if (Math.abs(A[p][c]) < 1e-9) return null;
[A[c],A[p]] = [A[p],A[c]]; [b[c],b[p]] = [b[p],b[c]];
for (let r = 0; r < n; r++){
if (r === c) continue;
const f = A[r][c] / A[c][c];
for (let k = c; k < n; k++) A[r][k] -= f * A[c][k];
b[r] -= f * b[c];
}
}
const hcoef = b.map((v,i) => v / A[i][i]);
return [...hcoef, 1];
}
const applyH = (H, x, y) => {
const w = H[6]*x + H[7]*y + H[8];
return [(H[0]*x + H[1]*y + H[2]) / w, (H[3]*x + H[4]*y + H[5]) / w];
};
// ---------- fountain ----------
let dec = null;
function newDecoder(){
return { decoded: new Map(), pending: [], seen: new Set(),
pool: new Map(), inferred: new Set() };
}
function fountainAdd(idx, block){
if (dec.seen.has(idx)) return;
// pool ghost sweep, live: content equal to same position one seq
// earlier is a straddle ghost
const prev = dec.pool.get(idx - M.n_sub);
if (prev && eqBytes(prev, block)) return;
dec.pool.set(idx, block);
dec.seen.add(idx);
const comp = idxFor(idx);
if (!comp) return;
let idxs = new Set(comp);
let data = Uint8Array.from(block);
for (const i of [...idxs])
if (dec.decoded.has(i)){ xorInto(data, dec.decoded.get(i)); idxs.delete(i); }
if (idxs.size === 0) return;
if (idxs.size === 1) resolve([...idxs][0], data);
else dec.pending.push([idxs, data]);
}
function resolve(i, data){
const stack = [[i, data]];
while (stack.length){
const [j, d] = stack.pop();
if (dec.decoded.has(j)) continue;
dec.decoded.set(j, d);
const still = [];
for (const [idxs, pd] of dec.pending){
if (idxs.has(j)){ xorInto(pd, d); idxs.delete(j); }
if (idxs.size === 1) stack.push([[...idxs][0], pd]);
else if (idxs.size) still.push([idxs, pd]);
}
dec.pending = still;
}
}
function xorInto(a, b){ for (let i = 0; i < a.length; i++) a[i] ^= b[i]; }
const eqBytes = (a,b) => { if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
return true; };
// ---------- frame pipeline ----------
let HDR_MASKS = [], PC = null, HC = null;
let prevBlocks = null, prevSeq = -10;
let stats = { frames: 0, located: 0, tracked: 0, certified: 0, t0: 0, firstHit: 0 };
const cvs = document.createElement("canvas");
const cctx = cvs.getContext("2d", { willReadFrequently: true });
// Tracked frames draw and read ONLY the code's bbox through this canvas;
// the full-frame canvas is touched on detection frames alone.
const tCvs = document.createElement("canvas");
const tCtx = tCvs.getContext("2d", { willReadFrequently: true });
let trackMiss = 0;
// PROCESSING SPACE. Field A/B verdict (2026-08-06): half resolution
// KILLED it on glass: located 87/763, yield 27% at ~5 scaled px/cell.
// Matches the film-pipeline finding that the payload starves below ~8
// real px/cell; the camera model was too kind about pre-filtered
// downscaling. Full resolution is the default; ?half=1 keeps the
// experiment reachable.
const SCALE = new URLSearchParams(location.search).get("half") === "1"
? 0.5 : 1;
function processFrame(){
const v = $("v");
if (!v.videoWidth) return;
const w = Math.round(v.videoWidth * SCALE);
const h = Math.round(v.videoHeight * SCALE);
stats.frames++;
// Draw + sample + header at geometry Hc, bbox-only, in the scaled
// space. The header is the zero-false-accept gate: no geometry is
// installed or harvested unless it certifies here.
const attemptAt = (Hc, sg) => {
sg = sg || buildGrid(Hc, K1, w, h);
if (tCvs.width !== sg.bw || tCvs.height !== sg.bh){
tCvs.width = sg.bw; tCvs.height = sg.bh;
}
tCtx.drawImage(v, sg.bx / SCALE, sg.by / SCALE,
sg.bw / SCALE, sg.bh / SCALE, 0, 0, sg.bw, sg.bh);
const data = tCtx.getImageData(0, 0, sg.bw, sg.bh).data;
const sb = sampleGrid(data, sg);
const hb = new Uint8Array(HC.length);
const hc = new Float32Array(HC.length);
for (let i = 0; i < HC.length; i++){
hb[i] = sb.bitAt[HC[i]]; hc[i] = sb.confAt[HC[i]];
}
return { sg, data, sb, hd: tryHeader(hb, hc, 22) };
};
let H = null, tracked = false, hdKnown, stageG = null;
if (lastH && trackMiss < 8){
const a = attemptAt(lastH, SG);
SG = a.sg;
if (a.hd){ H = lastH; tracked = true; cachedBits = a.sb; hdKnown = a.hd;
trackMiss = 0; }
else {
// Headerless frame. The header is ONE contiguous band, so rolling-
// shutter shear or a seam kills it as a unit while the 31
// distributed payload bands still certify at 40%+. Those frames are
// most of the transfer. With a fresh clock fit, run the FULL
// ladder-assisted decode as both geometry judge and harvest: one
// certified codeword is a zero-false-accept proof, and the blocks
// go to quarantine under the inferred seq.
stageG = inferSeq(performance.now());
if (stageG !== null && (JSMODE ? dynK : M.k)){
H = lastH; tracked = true; cachedBits = a.sb; hdKnown = undefined;
} else if (certCount(a.sb, 10) >= 1){
// no usable fit yet; geometry still proven, keep the lock cheap
trackMiss = 0;
guide("STRADDLE", "phase seam, riding it out", lastPx);
return;
}
if (!H){
trackMiss++;
// Drift vs blur: re-locate the finders locally and refit, but TRUST
// NOTHING until the header certifies at the refreshed geometry. The
// uncertified version of this refresh installed slightly-wrong fits,
// blocked detection behind them, and fell to ~2.5 KB/s in the field.
// A certified refresh repairs drift AND harvests this very frame.
const q = cornerRefresh(a.data, SG, lastPxS || 5);
if (q){
const cx = w / 2, cy = h / 2, rn2 = (w*w + h*h) / 4;
const H2 = homography(M.finders, q.map(([x, y]) => {
const dx = x - cx, dy = y - cy;
const s = 1 + K1 * (dx*dx + dy*dy) / rn2;
return [cx + dx / s, cy + dy / s];
}));
const a2 = H2 && attemptAt(H2);
if (a2 && a2.hd){
lastH = H2; SG = a2.sg;
H = H2; tracked = true; cachedBits = a2.sb; hdKnown = a2.hd;
trackMiss = 0;
}
}
if (!H){ guide("HOLD", "hold steady", lastPx); return; }
}
}
}
if (!H){
SG = null;
if (cvs.width !== w){ cvs.width = w; cvs.height = h; }
cctx.drawImage(v, 0, 0, w, h);
const img = cctx.getImageData(0, 0, w, h).data;
const G = p => (img[p<<2]*77 + img[(p<<2)+1]*150 + img[(p<<2)+2]*29) >> 8;
const d = detectH(G, w, h);
if (!d){
trackMiss = 0;
if (++lockMiss > 10) lastH = null;
guide("NO LOCK", `hold the whole code in frame (${lastCands} hits)`, 0);
return; }
H = d.H; cachedBits = d.sb; hdKnown = d.hd || undefined;
if (!d.hd) stageG = inferSeq(performance.now());
trackMiss = 0; // re-arm the cheap tracked attempts
}
lockMiss = 0;
lastH = H;
stats.located++;
if (tracked) stats.tracked++;
const p0 = applyH(H, 0, 0), p1 = applyH(H, 1, 0), p2 = applyH(H, 0, 1);
const pxS = (Math.hypot(p1[0]-p0[0], p1[1]-p0[1]) +
Math.hypot(p2[0]-p0[0], p2[1]-p0[1])) / 2;
lastPxS = pxS;
lastPx = pxS / SCALE; // native cam-px/cell for the operator
const got = finishFrame(cachedBits.bitAt, cachedBits.confAt, lastPx,
hdKnown, stageG);
// On staging frames the certify result IS the lock verdict: zero
// certified blocks means the geometry (or the frame) is truly gone.
if (stageG !== null && hdKnown === undefined){
if (got > 0) trackMiss = 0; else trackMiss++;
}
}
// Radial term. The homography is fit on four corner pilots, but the phone
// lens is not a pinhole: mid-field sample points drift off-grid by several
// px, which at 8-10 cam-px/cell corrupts most codewords while the corners
// (and often the header band) still read. The film-pipeline decoder proved
// this exact mechanism: geometry chosen by border pilots alone loses 87.3%
// of codewords vs geometry chosen by the code (k1 hill-climb). K1 is swept
// by detectH with the header as the certifier and then held.
let K1 = 0, k1SweepAt = 0, k1RefineAt = 0, lockMiss = 0;
const K1LADDER = [0.005, 0.01, 0.015, 0.02, 0.03, 0.045,
-0.005, -0.01, -0.02, -0.03];
// Certified-codeword count over a sample of bands: the judge for
// geometry candidates. NOT syndrome-zero: on real glass at ~10 px/cell
// almost no codeword is error-free, so a zero-syndrome judge reads 0 on
// perfectly workable frames (which is why STRADDLE never showed in the
// field). Hard RS decode + CRC32 is the same zero-false-accept oracle the
// whole decoder is built on.
function certCount(sb, nSample){
// Includes a two-rung erasure ladder: on real glass most certifications
// come through erasure rescues, so a hard-only judge reads 0 on frames
// the full decoder handles at 40%+ yield.
const msg = new Uint8Array(255);
let n = 0;
const step = Math.max(1, Math.floor(M.n_sub / nSample));
for (let j = 0; j < M.n_sub; j += step){
const conf = new Float32Array(255);
for (let i = 0; i < 255; i++){
let v = 0, cmin = 1e9;
const o = (j*255 + i) * 8;
for (let b = 0; b < 8; b++){
const cell = PC[o + b];
v = (v << 1) | sb.bitAt[cell];
if (sb.confAt[cell] < cmin) cmin = sb.confAt[cell];
}
msg[i] = v; conf[i] = cmin;
}
let ok = null;
const m2 = Uint8Array.from(msg);
if (correctMsg(m2, 255, M.ecc, null, 0)) ok = m2;
if (!ok){
const order = Array.from({length:255}, (_,i)=>i)
.sort((a,b) => conf[a] - conf[b]);
for (const nEr of [4, 10]){
const m3 = Uint8Array.from(msg);
if (correctMsg(m3, 255, M.ecc, order, nEr)){ ok = m3; break; }
}
}
if (ok){
const want = (ok[0] | (ok[1]<<8) | (ok[2]<<16) | (ok[3]<<24)) >>> 0;
if (crc32(ok, 4, M.sub) === want) n++;
}
}
return n;
}
// ---- seq inference: the dual-seq harvest, browser edition. The header
// band can die for long stretches (phase seam or glare parked on its
// rows) while payload bands still certify at 40%+. Those frames used to
// be discarded; they are most of the transfer. A linear clock fit over
// recent certified headers predicts seq for headerless frames; harvested
// blocks are QUARANTINED and only enter the fountain when the next real
// header validates the fit. Zero false accepts end to end, and finish()
// still sha256-gates the file.
let seqAnchors = []; // [t_ms, seq] from certified headers
let staged = []; // {g, blocks: [[j, blk]...]}
function seqFit(){
if (seqAnchors.length < 3) return null;
const [t0, s0] = seqAnchors[0];
const [tN, sN] = seqAnchors[seqAnchors.length - 1];
if (tN - t0 < 900) return null;
const b = (sN - s0) / (tN - t0); // seq per ms
if (b <= 0) return null;
return { b, tN, sN };
}
function inferSeq(now){
const f = seqFit();
if (!f || now - f.tN > 4000) return null; // stale fit: refuse
const g = f.sN + f.b * (now - f.tN);
const r = Math.round(g);
return Math.abs(g - r) < 0.35 ? r : null; // reject only seam-riders
}
function anchorSeq(now, seq){
// validate + flush quarantine against this REAL header
const f = seqFit();
if (f && staged.length){
const pred = f.sN + f.b * (now - f.tN);
if (Math.abs(pred - seq) < 0.5){
for (const s of staged)
for (const [j, blk] of s.blocks){
const idx = s.g * M.n_sub + j;
dec.inferred.add(idx);
fountainAdd(idx, blk);
}
}
staged = [];
}
seqAnchors.push([now, seq]);
if (seqAnchors.length > 8) seqAnchors.shift();
}
function sampleBits(G, w, h, H, k1){
const nCells = M.gw * M.gh;
const lum = new Float32Array(nCells);
const cx = w / 2, cy = h / 2, rn2 = (w*w + h*h) / 4;
for (let r = 0; r < M.gh; r++){
for (let c = 0; c < M.gw; c++){
let [px, py] = applyH(H, c + 0.5, r + 0.5);
if (k1){
const dx = px - cx, dy = py - cy;
const s = 1 + k1 * (dx*dx + dy*dy) / rn2;
px = cx + dx * s; py = cy + dy * s;
}
const xi = Math.min(w-2, Math.max(0, px|0));
const yi = Math.min(h-2, Math.max(0, py|0));
lum[r*M.gw+c] = (G(yi*w+xi) + G(yi*w+xi+1) +
G((yi+1)*w+xi) + G((yi+1)*w+xi+1)) / 4;
}
}
return threshold(lum);
}
// ---- tracked fast path: the cell->pixel map is CONSTANT while H and k1
// hold, so precompute it once per lock and sample by table lookup, reading
// only the code's bounding box out of the canvas instead of the whole 4K
// frame. Rebuilt on every re-detection (SG = null there).
let SG = null;
function buildGrid(H, k1, w, h){
const nCells = M.gw * M.gh;
const xs = new Int32Array(nCells), ys = new Int32Array(nCells);
const cx = w / 2, cy = h / 2, rn2 = (w*w + h*h) / 4;
let minx = w, miny = h, maxx = 0, maxy = 0;
for (let r = 0; r < M.gh; r++){
for (let c = 0; c < M.gw; c++){
let [px, py] = applyH(H, c + 0.5, r + 0.5);
if (k1){
const dx = px - cx, dy = py - cy;
const s = 1 + k1 * (dx*dx + dy*dy) / rn2;
px = cx + dx * s; py = cy + dy * s;
}
const xi = Math.min(w-2, Math.max(0, px|0));
const yi = Math.min(h-2, Math.max(0, py|0));
const i = r*M.gw+c;
xs[i] = xi; ys[i] = yi;
if (xi < minx) minx = xi; if (xi > maxx) maxx = xi;
if (yi < miny) miny = yi; if (yi > maxy) maxy = yi;
}
}
// 18px margin: the corner-refresh search needs room around the finder
// centres, which sit at the grid's edge.
const bx = Math.max(0, minx - 18), by = Math.max(0, miny - 18);
const bw = Math.min(w, maxx + 36) - bx, bh = Math.min(h, maxy + 36) - by;
const base = new Int32Array(nCells);
for (let i = 0; i < nCells; i++)
base[i] = (ys[i] - by) * bw + (xs[i] - bx);
// finder centres in bbox px, for drift refresh
const f4 = M.finders.map(([gx, gy]) => {
let [px, py] = applyH(H, gx, gy);
if (k1){
const dx = px - cx, dy = py - cy;
const s = 1 + k1 * (dx*dx + dy*dy) / rn2;
px = cx + dx * s; py = cy + dy * s;
}
return [px - bx, py - by];
});
return { base, bx, by, bw, bh, f4 };
}
// Drift refresh: hand-held tracking loses the header not to blur but to a
// few px of slow drift, and the frame itself is fine. Re-locate each
// finder inside a small window around its predicted position (normalized
// correlation against the 7x7-cell finder pattern), and refit. ~1 ms in
// the already-read bbox, vs ~100 ms of full detection that mostly failed
// on the same frames. Blur genuinely fails this search, which is exactly
// the skip signal the duty cycle wants.
function cornerRefresh(data, sg, cell){
const { bw, bh, f4 } = sg;
const lumAt = (x, y) => {
const p = (y * bw + x) << 2;
return (data[p]*77 + data[p+1]*150 + data[p+2]*29) >> 8;
};
// 7x7 finder: dark border ring, light inner ring, dark 3x3 core
const sign = (i, j) => {
const a = Math.max(Math.abs(i), Math.abs(j)); // rings at radius 3,2,1
return a === 3 ? 1 : (a === 2 ? -1 : 1);
};
const out = [];
let good = 0;
for (const [fx0, fy0] of f4){
let best = -1, bx2 = fx0, by2 = fy0;
for (let dy = -12; dy <= 12; dy += 2){
for (let dx = -12; dx <= 12; dx += 2){
let sum = 0, taps = [];
for (let j = -3; j <= 3; j++)
for (let i = -3; i <= 3; i++){
const x = Math.round(fx0 + dx + i * cell);
const y = Math.round(fy0 + dy + j * cell);
const l = (x >= 0 && x < bw && y >= 0 && y < bh)
? lumAt(x, y) : 128;
taps.push([sign(i, j), l]); sum += l;
}
const mean = sum / 49;
let sc = 0, mag = 1e-6;
for (const [e, l] of taps){ sc += e * (mean - l);
mag += Math.abs(l - mean); }
const n = sc / mag;
if (n > best){ best = n; bx2 = fx0 + dx; by2 = fy0 + dy; }
}
}
if (best > 0.45) good++;
out.push([bx2 + sg.bx, by2 + sg.by]);
}
return good >= 3 ? out : null;
}
function sampleGrid(data, sg){
const { base, bw } = sg;
const nCells = M.gw * M.gh;
const lum = new Float32Array(nCells);
for (let i = 0; i < nCells; i++){
const p0 = base[i] << 2, p1 = (base[i] + 1) << 2;
const p2 = (base[i] + bw) << 2, p3 = (base[i] + bw + 1) << 2;
lum[i] = ((data[p0]*77 + data[p0+1]*150 + data[p0+2]*29 >> 8) +
(data[p1]*77 + data[p1+1]*150 + data[p1+2]*29 >> 8) +
(data[p2]*77 + data[p2+1]*150 + data[p2+2]*29 >> 8) +
(data[p3]*77 + data[p3+1]*150 + data[p3+2]*29 >> 8)) / 4;
}
return threshold(lum);
}
function threshold(lum){
const nCells = M.gw * M.gh;
// local threshold over cell space, 15-cell box via integral image
const II = new Float64Array((M.gw+1) * (M.gh+1));
for (let r = 0; r < M.gh; r++){
let rs = 0;
for (let c = 0; c < M.gw; c++){
rs += lum[r*M.gw+c];
II[(r+1)*(M.gw+1)+c+1] = II[r*(M.gw+1)+c+1] + rs;
}
}
const K = 15 >> 1;
const bitAt = new Uint8Array(nCells), confAt = new Float32Array(nCells);
for (let r = 0; r < M.gh; r++){
const r0 = Math.max(0, r-K), r1 = Math.min(M.gh, r+K+1);
for (let c = 0; c < M.gw; c++){
const c0 = Math.max(0, c-K), c1 = Math.min(M.gw, c+K+1);
const s = II[r1*(M.gw+1)+c1] - II[r0*(M.gw+1)+c1]
- II[r1*(M.gw+1)+c0] + II[r0*(M.gw+1)+c0];
const mean = s / ((r1-r0) * (c1-c0));
const l = lum[r*M.gw+c];
bitAt[r*M.gw+c] = l > mean ? 1 : 0;
confAt[r*M.gw+c] = Math.abs(l - mean);
}
}
return { bitAt, confAt };
}
function finishFrame(bitAt, confAt, pxPerCell, hdIn, stageG){
let hd = hdIn;
if (hd === undefined && stageG == null){
const hb = new Uint8Array(HC.length);
const hcf = new Float32Array(HC.length);
for (let i = 0; i < HC.length; i++){
hb[i] = bitAt[HC[i]];
hcf[i] = confAt[HC[i]];
}
hd = tryHeader(hb, hcf);
}
if (hd && JSMODE && hd.k !== dynK){
dynK = hd.k; dynSize = hd.fileSize; dynCum = robustSoliton(dynK);
}
const staging = !hd && stageG != null;
if (!staging && (!hd || (!JSMODE && hd.k !== M.k))){
guide("LOCKED, no header",
pxPerCell < 7 ? "MOVE CLOSER" :
(pxPerCell > 16 ? "back off a little" :
"hold still, square to the screen"), pxPerCell);
return 0;
}
const seq = staging ? stageG : hd.seq;
if (!staging) anchorSeq(performance.now(), hd.seq);
const stagedBlocks = [];
// payload codewords
const curBlocks = new Map();
const nb = M.n_sub * 255;