code wiki / _hdl_build / _codec_bench_med.nx

_codec_bench_med.nx source

↩ module page · 143 lines · 7020 B

1// _codec_bench_med.nx -- X-CDC-FRAME-LZ-001 rung: sovereign-PREDICTOR PNG-style head-to-head. 2// Measured finding (X-BENCH-CODEC-002): fc loses to PNG-class because it has no LZ stage. But fc 3// owns the MED (LOCO-I/JPEG-LS) predictor, while PNG offers only fixed filters (None/Sub/Up/Avg/ 4// Paeth). This rung ISOLATES PREDICTOR QUALITY: feed BOTH the MED residual and the PNG-Sub residual 5// to the SAME sovereign deflate, on the SAME corpus. MED+deflate < Sub+deflate => the MED prefilter 6// EXCEEDS PNG's Sub filter (no-wave: only the prefilter differs; both sides our deflate; both sovereign). 7// No-false-green: each frame's MED+deflate pipeline is end-to-end round-trip verified 8// (MED-residual -> deflate -> our inflate -> un-MED == original), plus a tamper check that MUST fail. 9// GREEN iff round-trip exact on all 5 AND tamper detected; the exceed COUNT is reported, never forced. 10import "nx_frame_codec.nx" 11import "nx_deflate_fixed.nx" 12import "nx_syscalls.nx" 13 14func bp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 15func bn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; var k: i64=0; let t: *u8=sys_mmap(28); if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1}; sys_write(1,b,k); return 0 } 16func lw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 17func ln(fd: i64, v: i64) -> i64 { let b: *u8=sys_mmap(28); var m: i64=v; var k: i64=0; let t: *u8=sys_mmap(28); if m==0{t[0]=48;k=1}; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}; var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1}; sys_write(fd,b,k); return 0 } 18 19// PNG "Sub" filter (per-row left predictor) -> residual 20func sub_filter(buf: *u8, w: i64, h: i64, res: *u8) -> i64 { 21 var y: i64 = 0 22 while y < h { 23 var x: i64 = 0 24 while x < w { 25 if x == 0 { res[y*w] = buf[y*w] } 26 if x > 0 { res[y*w+x] = (((buf[y*w+x] & 255) - (buf[y*w+x-1] & 255)) & 255) as u8 } 27 x = x + 1 28 } 29 y = y + 1 30 } 31 return 0 32} 33// MED (JPEG-LS) prefilter residual via fc_pred (causal MED predictor) 34func med_filter(buf: *u8, w: i64, h: i64, res: *u8) -> i64 { 35 var y: i64 = 0 36 while y < h { 37 var x: i64 = 0 38 while x < w { 39 let p: i64 = fc_pred(buf, w, x, y) 40 res[y*w+x] = (((buf[y*w+x] & 255) - (p & 255)) & 255) as u8 41 x = x + 1 42 } 43 y = y + 1 44 } 45 return 0 46} 47// inverse MED prefilter: reconstruct into out from residual (causal -- uses already-rebuilt neighbors) 48func med_unfilter(res: *u8, w: i64, h: i64, out: *u8) -> i64 { 49 var y: i64 = 0 50 while y < h { 51 var x: i64 = 0 52 while x < w { 53 let p: i64 = fc_pred(out, w, x, y) 54 out[y*w+x] = (((res[y*w+x] & 255) + (p & 255)) & 255) as u8 55 x = x + 1 56 } 57 y = y + 1 58 } 59 return 0 60} 61 62// returns 1 if MED-prefilter < Sub-prefilter (MED beats PNG's filter). rt[0]=0 if round-trip fails. 63func bench_buf(label: *u8, buf: *u8, w: i64, h: i64, scratch: *u8, cap: i64, rt: *i64) -> i64 { 64 let n: i64 = w * h 65 let med: *u8 = scratch 66 let sub: *u8 = (scratch as i64 + cap) as *u8 67 let dfo: *u8 = (scratch as i64 + 2*cap) as *u8 68 let rec: *u8 = (scratch as i64 + 3*cap) as *u8 69 let dec: *u8 = (scratch as i64 + 4*cap) as *u8 70 let dr: i64 = dfl_encode(buf, n, dfo, cap) 71 sub_filter(buf, w, h, sub) 72 let dp: i64 = dfl_encode(sub, n, dfo, cap) 73 med_filter(buf, w, h, med) 74 let dm: i64 = dfl_encode(med, n, dfo, cap) 75 // round-trip the MED+deflate pipeline: deflate(med) -> inflate -> un-MED -> compare to buf 76 let k: i64 = dfl_decode(dfo, dm, dec, cap) 77 med_unfilter(dec, w, h, rec) 78 var ok: i64 = 1 79 if k != n { ok = 0 } 80 var i: i64 = 0 81 while i < n { if rec[i] != buf[i] { ok = 0 } i = i + 1 } 82 if ok == 0 { rt[0] = 0 } 83 bp(label); bp(" raw="); bn(n); bp(" deflate_raw="); bn(dr); bp(" PNGsub+deflate="); bn(dp); bp(" MED+deflate="); bn(dm) 84 var win: i64 = 0 85 if dm < dp { win = 1 } 86 bp(" MED<PNGsub="); if win==1 { bp("WIN") } else { bp("lose") } 87 bp(" rt="); bn(ok); bp("\n") 88 return win 89} 90 91func main() -> i64 { 92 let w: i64 = 32 93 let h: i64 = 32 94 let n: i64 = w * h 95 let cap: i64 = n * 4 + 64 96 let buf: *u8 = sys_mmap(n + 64) 97 let scratch: *u8 = sys_mmap(cap * 5 + 64) 98 let rtp: *i64 = sys_mmap(64) as *i64 99 rtp[0] = 1 100 var wins: i64 = 0 101 fc_gen_gradient(buf, w, h); wins = wins + bench_buf("gradient" as *u8, buf, w, h, scratch, cap, rtp) 102 fc_gen_checker(buf, w, h); wins = wins + bench_buf("checker " as *u8, buf, w, h, scratch, cap, rtp) 103 fc_gen_edge(buf, w, h); wins = wins + bench_buf("edge " as *u8, buf, w, h, scratch, cap, rtp) 104 fc_gen_extremes(buf, w, h); wins = wins + bench_buf("extremes" as *u8, buf, w, h, scratch, cap, rtp) 105 fc_gen_noise(buf, w, h); wins = wins + bench_buf("noise " as *u8, buf, w, h, scratch, cap, rtp) 106 // tamper check (no-false-green): corrupt a recovered residual byte -> reconstruction MUST differ. 107 // also recapture gradient sizes for the headline 9x. 108 fc_gen_gradient(buf, w, h) 109 let med: *u8 = scratch 110 let sub: *u8 = (scratch as i64 + cap) as *u8 111 let dfo: *u8 = (scratch as i64 + 2*cap) as *u8 112 let rec: *u8 = (scratch as i64 + 3*cap) as *u8 113 let dec: *u8 = (scratch as i64 + 4*cap) as *u8 114 sub_filter(buf, w, h, sub) 115 let dp_grad: i64 = dfl_encode(sub, n, dfo, cap) 116 med_filter(buf, w, h, med) 117 let dm_grad: i64 = dfl_encode(med, n, dfo, cap) 118 let k: i64 = dfl_decode(dfo, dm_grad, dec, cap) 119 dec[10] = (dec[10] + 1) as u8 120 med_unfilter(dec, w, h, rec) 121 var tamper_detected: i64 = 0 122 var i: i64 = 0 123 while i < n { if rec[i] != buf[i] { tamper_detected = 1 } i = i + 1 } 124 var green: i64 = 0 125 if rtp[0] == 1 { if tamper_detected == 1 { green = 1 } } 126 bp("=== MED-prefilter beats PNG-Sub on "); bn(wins); bp("/5 (same deflate, sovereign both sides, no-wave) round_trip_all="); bn(rtp[0]); bp(" tamper_detected="); bn(tamper_detected); bp(" ===\n") 127 // organ-authored durable evidence log (the marker reconcile/sponsor can read) 128 let lf: i64 = sys_openat_wr("knowledge/status/codec_med_bench.log" as *u8, 0x1a4) 129 if lf >= 0 { 130 lw(lf, "CODECMEDGATE verdict=" as *u8) 131 if green == 1 { lw(lf, "GREEN" as *u8) } else { lw(lf, "RED" as *u8) } 132 lw(lf, " gap=X-CDC-FRAME-LZ-001 test=MED-prefilter-vs-PNG-Sub-same-sovereign-deflate wins=" as *u8); ln(lf, wins) 133 lw(lf, "/5 gradient_MED=" as *u8); ln(lf, dm_grad) 134 lw(lf, " gradient_PNGsub=" as *u8); ln(lf, dp_grad) 135 lw(lf, " round_trip_all=" as *u8); ln(lf, rtp[0]) 136 lw(lf, " tamper_detected=" as *u8); ln(lf, tamper_detected) 137 lw(lf, " sovereign=both-sides corpus=synthetic-5frame note=real-image-corpus-is-next-rung no-wave\n" as *u8) 138 sys_close(lf) 139 } 140 if green == 1 { sys_exit(0) } 141 sys_exit(1) 142 return 1 143}