code wiki / _hdl_build / nx_vcodec_coverage_gate.nx

nx_vcodec_coverage_gate.nx source

↩ module page · 67 lines · 4115 B

1import "nx_gate_base.nx" 2// nx_vcodec_coverage_gate.nx -- THE GREEN-LINE catcher. The vcodec uses 16x16 macroblocks with TRUNCATING 3// block counts (BH=H/16). A plane whose height is NOT a multiple of 16 loses its bottom partial-block row 4// -> those pixels are never coded -> garbage (green) at the bottom. The closed-loop bit-exact gate MISSED 5// this because encode+decode drop the SAME rows symmetrically. This gate checks the ACTUAL invariant: after 6// decode, is the bottom CHROMA row covered? (chroma H = H/2; for 320x240 chroma is 160x120, 120/16=7.5 -> 7// bottom 8 chroma rows dropped -> the ~16px green band the operator saw). A production geometry must be 8// codec-safe: BOTH W/2 and H/2 multiples of 16 (i.e. W,H multiples of 32). license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_video_codec_wasm.nx" 11 12func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 13" as *u8); return ok } 14func gn(v: i64) -> i64 { 15 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 16 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} 17 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 18 19// encode a flat frame at W,H (key), decode into a 0xAA-initialised recon; return 1 if the bottom CHROMA 20// row was COVERED (not still 0xAA), 0 if dropped (green-line). 21func covered(W: i64, H: i64) -> i64 { 22 let N: i64 = W * H 23 let C: i64 = (W/2) * (H/2) 24 let sz: i64 = N + 2*C 25 let yuv: *u8 = sys_mmap(sz) 26 var i: i64 = 0 27 while i < N { yuv[i] = 128 as u8; i = i + 1 } // Y grey 28 while i < N + C { yuv[i] = 100 as u8; i = i + 1 } // U = 100 29 while i < sz { yuv[i] = 150 as u8; i = i + 1 } // V = 150 30 let prevE: *u8 = sys_mmap(sz); let reconE: *u8 = sys_mmap(sz) 31 let prevD: *u8 = sys_mmap(sz) 32 let reconD: *u8 = sys_mmap(sz) 33 i = 0; while i < sz { reconD[i] = 0xAA as u8; prevE[i] = 0 as u8; prevD[i] = 0 as u8; i = i + 1 } 34 let stream: *u8 = sys_mmap(262144) 35 let blk: *i64 = sys_mmap(512) as *i64; let mv: *i64 = sys_mmap(128) as *i64 36 let kb: i64 = vv_enc(yuv, prevE, reconE, W, H, 32, 1, 6016, stream, 262144, blk, mv) 37 if kb <= 0 { return 0 - 1 } 38 vv_dec(prevD, reconD, W, H, 32, stream, kb, blk, mv) 39 // bottom U row = reconD[N + (H/2 - 1)*(W/2) .. + W/2) 40 let cw: i64 = W/2 41 let off: i64 = N + ((H/2) - 1) * cw 42 var touched: i64 = 0 43 var x: i64 = 0 44 while x < cw { if (reconD[off + x] & 0xff) != 0xAA { touched = 1; x = cw } else { x = x + 1 } } 45 return touched } 46 47func row(name: *u8, W: i64, H: i64, want_covered: i64, p: *i64, t: *i64) -> i64 { 48 t[0] = t[0] + 1 49 let c: i64 = covered(W, H) 50 gw(" " as *u8); gw(name); gw(" " as *u8); gn(W); gw("x" as *u8); gn(H); gw(" -> " as *u8) 51 if c == 1 { gw("COVERED" as *u8) } else { gw("GREEN-LINE (bottom chroma dropped)" as *u8) } 52 if c == want_covered { gw(" PASS\n" as *u8); p[0] = p[0] + 1 } else { gw(" FAIL\n" as *u8) } 53 return 0 } 54 55func main() -> i64 { 56 gw("=== nx_vcodec_coverage_gate: bottom-chroma coverage (the green-line invariant) ===\n" as *u8) 57 let p: *i64 = sys_mmap(16) as *i64; let t: *i64 = sys_mmap(16) as *i64 58 // PRODUCTION geometries MUST be covered (codec-safe: W,H multiples of 32 -> chroma W/2,H/2 mult of 16) 59 row("PROD main \x00" as *u8, 320, 256, 1, p, t) // buffer max, chroma 160x128 -> both /16 (live encode ceiling) 60 row("PROD LO \x00" as *u8, 128, 96, 1, p, t) // 4:3, chroma 64x48 -> both /16 61 // DIAGNOSTIC: the OLD geometries the operator saw fail (bottom chroma not a /16 height) 62 row("OLD main \x00" as *u8, 320, 240, 0, p, t) // chroma 160x120, 120/16=7.5 -> DROPPED 63 row("OLD flex/LO \x00" as *u8, 160, 120, 0, p, t) // chroma 80x60, 60/16=3.75 -> DROPPED 64 gw("VCODEC-COVERAGE: " as *u8); gn(p[0]); gw("/" as *u8); gn(t[0]) 65 if p[0]==t[0] { gw(" ALL GREEN (production geometries codec-safe; old confirmed green-lined)\n" as *u8); return 0 } 66 gw(" RED\n" as *u8) 67 return 1 }