code wiki / _hdl_build / nx_vcodec_t8feat_gate.nx
nx_vcodec_t8feat_gate.nx source
↩ module page · 158 lines · 6959 B
1import "nx_gate_base.nx"
2// nx_vcodec_t8feat_gate.nx -- FEATURE-PRESERVATION proof for the t8 stream (field 786/787 "still blocky /
3// low quality", root-caused): an MB that is 90% flat skin + one small high-contrast feature (eye, nostril,
4// mouth corner) has LOW mean mad -> the old policy sent it 8x8 -> the feature's energy spread across many
5// sub-deadzone 8x8 coefficients -> detail VANISHED while whole-frame PSNR barely moved (the metric the old
6// gates watched). This gate measures what the EYE watches: SSE restricted to +-3px windows around every
7// planted feature (featSSE), t8 stream vs legacy, on smooth-skin content with a moving grid of 3x3 dark
8// features + a mouth line, at the coarse end of the live ladder (qp 28/40). GREEN iff the t8 stream's
9// featSSE stays within 115% of legacy at every qp (the vc_mb_t8_ok uniform-flatness guard keeps feature
10// MBs on 4x4) AND every frame decodes bit-exact. license_tier: ORIGINAL
11import "nx_syscalls.nx"
12import "nx_video_codec_wasm.nx"
13
14const FW: i64 = 256
15const FH: i64 = 192
16
17func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
18" as *u8); return ok }
19func gn(v: i64) -> i64 {
20 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
21 let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}
22 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
23func eqb(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if (a[i]&0xff)!=(b[i]&0xff) { return 0 } i=i+1 } return 1 }
24func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 }
25
26// smooth skin + a moving grid of small dark features (eye class) + a 2px mouth line; chroma flat
27func fill_face(yuv: *u8, sh: i64) -> i64 {
28 let N: i64=FW*FH; let C2: i64=(FW/2)*(FH/2)
29 var r: i64=0
30 while r<FH { var c: i64=0
31 while c<FW { yuv[r*FW+c]=(105 + (c+r)/8) as u8; c=c+1 } r=r+1 }
32 var fy: i64 = 24
33 while fy < FH - 8 {
34 var fx: i64 = 24
35 while fx < FW - 8 {
36 let cx: i64 = fx + sh*2
37 let cy: i64 = fy + sh
38 var dy: i64 = 0 - 1
39 while dy <= 1 { var dx: i64 = 0 - 1
40 while dx <= 1 { yuv[(cy+dy)*FW + cx+dx] = 45 as u8; dx = dx + 1 } dy = dy + 1 }
41 fx = fx + 48
42 }
43 fy = fy + 48
44 }
45 var mx: i64 = FW/2 - 10
46 while mx < FW/2 + 10 { yuv[(FH/2 + sh)*FW + mx] = 60 as u8; yuv[(FH/2 + sh + 1)*FW + mx] = 60 as u8; mx = mx + 1 }
47 var i: i64=0
48 while i<C2 { yuv[N+i]=120 as u8; yuv[N+C2+i]=135 as u8; i=i+1 }
49 return 0 }
50
51// SSE inside +-3px windows around every planted feature (same grid math as fill_face)
52func feat_sse(dec: *u8, src: *u8, sh: i64) -> i64 {
53 var sse: i64 = 0
54 var fy: i64 = 24
55 while fy < FH - 8 {
56 var fx: i64 = 24
57 while fx < FW - 8 {
58 let cx: i64 = fx + sh*2
59 let cy: i64 = fy + sh
60 var dy: i64 = 0 - 3
61 while dy <= 3 { var dx: i64 = 0 - 3
62 while dx <= 3 {
63 let d: i64 = (dec[(cy+dy)*FW + cx+dx] & 0xff) - (src[(cy+dy)*FW + cx+dx] & 0xff)
64 sse = sse + d*d
65 dx = dx + 1 } dy = dy + 1 }
66 fx = fx + 48
67 }
68 fy = fy + 48
69 }
70 return sse }
71
72func seedctx(rctx: *i64, est: *i64, probs: *i64, rcbuf: *u8, t8c: *i64) -> i64 {
73 rctx[0]=0; rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64
74 rctx[5]=0; rctx[6]=0; rctx[7]=0; return 0 }
75
76// 4-frame chain; kind 0=legacy 1=t8. out[0]=featSSE(last frame), out[1]=exact
77func run_face(qp: i64, kind: i64, bufs: *i64, out: *i64) -> i64 {
78 let N: i64=FW*FH; let C2: i64=(FW/2)*(FH/2); let sz: i64=N+2*C2
79 let cur: *u8 = bufs[12] as *u8
80 let prevE: *u8 = bufs[0] as *u8
81 let reconE: *u8 = bufs[1] as *u8
82 let prevD: *u8 = bufs[2] as *u8
83 let reconD: *u8 = bufs[3] as *u8
84 let wire: *u8 = bufs[4] as *u8
85 let blk: *i64 = bufs[5] as *i64
86 let mv: *i64 = bufs[6] as *i64
87 let est: *i64 = bufs[7] as *i64
88 let probs: *i64 = bufs[8] as *i64
89 let rcbuf: *u8 = bufs[9] as *u8
90 let t8c: *i64 = bufs[10] as *i64
91 let rctx: *i64 = bufs[11] as *i64
92 var z: i64=0
93 while z<sz { prevE[z]=0 as u8; prevD[z]=0 as u8; z=z+1 }
94 var exact: i64=0
95 var f: i64=0
96 var fs: i64=0
97 while f<4 {
98 fill_face(cur, f)
99 var key: i64=0
100 if f==0 { key=1 }
101 var nb: i64=0
102 if kind==1 { seedctx(rctx, est, probs, rcbuf, t8c)
103 nb = vv_enc_t8(cur, prevE, reconE, FW, FH, qp, key, qp*188, wire, 2097152, blk, mv, rctx) }
104 else { nb = vv_enc(cur, prevE, reconE, FW, FH, qp, key, qp*188, wire, 2097152, blk, mv) }
105 if nb <= 0 { out[0]=0; out[1]=exact; return 0-1 }
106 if kind==1 { seedctx(rctx, est, probs, rcbuf, t8c)
107 vv_dec_t8(prevD, reconD, FW, FH, qp, wire, nb, blk, mv, rctx) }
108 else { vv_dec(prevD, reconD, FW, FH, qp, wire, nb, blk, mv) }
109 if eqb(reconD, reconE, sz)==1 { exact=exact+1 }
110 fs = feat_sse(reconD, cur, f)
111 cpb(prevE, reconE, sz)
112 cpb(prevD, reconD, sz)
113 f=f+1
114 }
115 out[0]=fs
116 out[1]=exact
117 return 0 }
118
119func main() -> i64 {
120 gw("=== nx_vcodec_t8feat_gate: small-feature preservation (the pixels the EYE watches), t8 vs legacy ===\n" as *u8)
121 let N: i64=FW*FH; let C2: i64=(FW/2)*(FH/2); let sz: i64=N+2*C2
122 let bufs: *i64 = sys_mmap(16*8) as *i64
123 bufs[0]=sys_mmap(sz+64) as i64
124 bufs[1]=sys_mmap(sz+64) as i64
125 bufs[2]=sys_mmap(sz+64) as i64
126 bufs[3]=sys_mmap(sz+64) as i64
127 bufs[4]=sys_mmap(2097152) as i64
128 bufs[5]=sys_mmap(512) as i64
129 bufs[6]=sys_mmap(128) as i64
130 bufs[7]=sys_mmap(64) as i64
131 bufs[8]=sys_mmap(32*8) as i64
132 bufs[9]=sys_mmap(1048576) as i64
133 bufs[10]=sys_mmap(5120) as i64
134 bufs[11]=sys_mmap(64) as i64
135 bufs[12]=sys_mmap(sz+64) as i64
136 vc_t8_init(bufs[10] as *i64)
137 let oL: *i64=sys_mmap(32) as *i64
138 let oT: *i64=sys_mmap(32) as *i64
139 let qs: *i64=sys_mmap(32) as *i64
140 qs[0]=20; qs[1]=28; qs[2]=40
141 var pass: i64=0
142 var total: i64=0
143 var qi: i64=0
144 while qi<3 {
145 run_face(qs[qi], 0, bufs, oL)
146 run_face(qs[qi], 1, bufs, oT)
147 gw(" qp=" as *u8); gn(qs[qi])
148 gw(" featSSE legacy=" as *u8); gn(oL[0]); gw(" t8=" as *u8); gn(oT[0])
149 gw(" (" as *u8); gn(oT[0]*100/oL[0]); gw("%) exact=" as *u8); gn(oT[1]); gw("/4\n" as *u8)
150 total=total+1
151 if oT[1]==4 { pass=pass+1 }
152 total=total+1
153 if oT[0] <= (oL[0]*115)/100 { pass=pass+1 } // features must survive the t8 stream
154 qi=qi+1
155 }
156 gw("T8FEAT: " as *u8); gn(pass); gw("/" as *u8); gn(total)
157 if pass==total { gw(" GREEN -- the uniform-flatness guard keeps detail: feature MBs stay 4x4\n" as *u8); return 0 }
158 gw(" RED\n" as *u8); return 1 }