code wiki / _hdl_build / _codec_real_med.nx

_codec_real_med.nx source

↩ module page · 279 lines · 12376 B

1// _codec_real_med.nx -- X-CDC-FRAME-LZ-001 representative-corpus rung: the SAME predictor-isolation 2// head-to-head as _codec_bench_med, but on REAL PHOTO pixels (a decoded gallery PNG) instead of 3// synthetic frames -- closing the "synthetic is unrepresentative" caveat with a no-wave verdict. 4// Pipeline: read a real 1024x768 8-bit RGB PNG -> nx_png_decode -> raw pixels -> sample 3 deterministic 5// 64x64 patches (1/4, center, 3/4 positions; no cherry-pick) per RGB channel -> for each plane compare 6// MED(JPEG-LS)-prefilter+deflate vs PNG-Sub-prefilter+deflate (SAME sovereign deflate, SAME pixels). 7// No-false-green: MED+deflate round-trips end-to-end on each plane + a tamper check that MUST fail. 8// GREEN iff round-trip exact on all planes AND tamper detected; the win COUNT is reported, never forced. 9// Dimensions/color are read from raw IHDR bytes (avoids the nx_int struct-field-alias hazard); only 10// res.pixels (a pointer field at offset 8, before any nx_int field) is read from the result struct. 11import "nx_frame_codec.nx" 12import "nx_deflate_fixed.nx" 13import "nx_png_decoder.nx" 14import "nx_syscalls.nx" 15 16func bp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 17func 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 } 18func 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 } 19func 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 } 20 21func be32(b: *u8, o: i64) -> i64 { return ((b[o] as i64 & 255)<<24)|((b[o+1] as i64 & 255)<<16)|((b[o+2] as i64 & 255)<<8)|(b[o+3] as i64 & 255) } 22 23// PNG "Sub" filter (per-row left predictor) -> residual 24func sub_filter(buf: *u8, w: i64, h: i64, res: *u8) -> i64 { 25 var y: i64 = 0 26 while y < h { 27 var x: i64 = 0 28 while x < w { 29 if x == 0 { res[y*w] = buf[y*w] } 30 if x > 0 { res[y*w+x] = (((buf[y*w+x] & 255) - (buf[y*w+x-1] & 255)) & 255) as u8 } 31 x = x + 1 32 } 33 y = y + 1 34 } 35 return 0 36} 37func med_filter(buf: *u8, w: i64, h: i64, res: *u8) -> i64 { 38 var y: i64 = 0 39 while y < h { 40 var x: i64 = 0 41 while x < w { 42 let p: i64 = fc_pred(buf, w, x, y) 43 res[y*w+x] = (((buf[y*w+x] & 255) - (p & 255)) & 255) as u8 44 x = x + 1 45 } 46 y = y + 1 47 } 48 return 0 49} 50func med_unfilter(res: *u8, w: i64, h: i64, out: *u8) -> i64 { 51 var y: i64 = 0 52 while y < h { 53 var x: i64 = 0 54 while x < w { 55 let p: i64 = fc_pred(out, w, x, y) 56 out[y*w+x] = (((res[y*w+x] & 255) + (p & 255)) & 255) as u8 57 x = x + 1 58 } 59 y = y + 1 60 } 61 return 0 62} 63// PNG "Paeth" filter (PNG's STRONGEST fixed filter) -> residual; out-of-bounds neighbors = 0 64func paeth_filter(buf: *u8, w: i64, h: i64, res: *u8) -> i64 { 65 var y: i64 = 0 66 while y < h { 67 var x: i64 = 0 68 while x < w { 69 var a: i64 = 0 70 if x > 0 { a = buf[y*w+x-1] as i64 & 255 } 71 var b: i64 = 0 72 if y > 0 { b = buf[(y-1)*w+x] as i64 & 255 } 73 var c: i64 = 0 74 if x > 0 { if y > 0 { c = buf[(y-1)*w+x-1] as i64 & 255 } } 75 let p: i64 = _png_paeth(a, b, c) 76 res[y*w+x] = (((buf[y*w+x] & 255) - (p & 255)) & 255) as u8 77 x = x + 1 78 } 79 y = y + 1 80 } 81 return 0 82} 83// per-row ADAPTIVE PNG filtering = what real PNG encoders do: pick, per scanline, the filter 84// (None/Sub/Up/Avg/Paeth) that minimizes the sum of absolute (signed-byte) residuals, then emit 85// that row's residual. This is the strongest fixed-filter baseline (upper bound over PNG's 5). 86func absb(r: i64) -> i64 { if r < 128 { return r } return 256 - r } 87func apred(f: i64, left: i64, above: i64, ul: i64) -> i64 { 88 if f == 0 { return 0 } 89 if f == 1 { return left } 90 if f == 2 { return above } 91 if f == 3 { return (left + above) / 2 } 92 if f == 4 { return _png_paeth(left, above, ul) } 93 return fc_med(left, above, ul) // f==5: the MED (JPEG-LS) predictor as a 6th per-row candidate 94} 95// nf=5 = real PNG (None/Sub/Up/Avg/Paeth); nf=6 = sovereign MED-augmented (adds MED) -> <= PNG by construction 96func adaptive_filter(buf: *u8, w: i64, h: i64, res: *u8, nf: i64) -> i64 { 97 var y: i64 = 0 98 while y < h { 99 var bestf: i64 = 0 100 var bestsum: i64 = 0 101 var f: i64 = 0 102 while f < nf { 103 var sum: i64 = 0 104 var x: i64 = 0 105 while x < w { 106 var left: i64 = 0 107 if x > 0 { left = buf[y*w+x-1] as i64 & 255 } 108 var above: i64 = 0 109 if y > 0 { above = buf[(y-1)*w+x] as i64 & 255 } 110 var ul: i64 = 0 111 if x > 0 { if y > 0 { ul = buf[(y-1)*w+x-1] as i64 & 255 } } 112 let pred: i64 = apred(f, left, above, ul) 113 let r: i64 = ((buf[y*w+x] as i64 & 255) - pred) & 255 114 sum = sum + absb(r) 115 x = x + 1 116 } 117 if f == 0 { bestsum = sum; bestf = 0 } 118 if sum < bestsum { bestsum = sum; bestf = f } 119 f = f + 1 120 } 121 var x2: i64 = 0 122 while x2 < w { 123 var left: i64 = 0 124 if x2 > 0 { left = buf[y*w+x2-1] as i64 & 255 } 125 var above: i64 = 0 126 if y > 0 { above = buf[(y-1)*w+x2] as i64 & 255 } 127 var ul: i64 = 0 128 if x2 > 0 { if y > 0 { ul = buf[(y-1)*w+x2-1] as i64 & 255 } } 129 let pred: i64 = apred(bestf, left, above, ul) 130 res[y*w+x2] = (((buf[y*w+x2] as i64 & 255) - pred) & 255) as u8 131 x2 = x2 + 1 132 } 133 y = y + 1 134 } 135 return 0 136} 137// copy a PW x PH patch of channel c (at image x0,y0; image is w wide, nch channels interleaved) into plane 138func extract(px: *u8, w: i64, nch: i64, c: i64, x0: i64, y0: i64, pw: i64, ph: i64, plane: *u8) -> i64 { 139 var j: i64 = 0 140 while j < ph { 141 var i: i64 = 0 142 while i < pw { 143 plane[j*pw+i] = px[((y0+j)*w + (x0+i))*nch + c] 144 i = i + 1 145 } 146 j = j + 1 147 } 148 return 0 149} 150// bench one plane: acc[0]+=MED size, acc[1]+=Sub size, acc[2]+=win(MED<Sub); acc[3]=0 if round-trip fails 151func bench_plane(label: *u8, plane: *u8, pw: i64, ph: i64, scratch: *u8, cap: i64, acc: *i64) -> i64 { 152 let n: i64 = pw*ph 153 let med: *u8 = scratch 154 let sub: *u8 = (scratch as i64 + cap) as *u8 155 let dfo: *u8 = (scratch as i64 + 2*cap) as *u8 156 let dec: *u8 = (scratch as i64 + 3*cap) as *u8 157 let rec: *u8 = (scratch as i64 + 4*cap) as *u8 158 med_filter(plane, pw, ph, med) 159 let dm: i64 = dfl_encode(med, n, dfo, cap) 160 let k: i64 = dfl_decode(dfo, dm, dec, cap) 161 med_unfilter(dec, pw, ph, rec) 162 var ok: i64 = 1 163 if k != n { ok = 0 } 164 var i: i64 = 0 165 while i < n { if rec[i] != plane[i] { ok = 0 } i = i + 1 } 166 if ok == 0 { acc[3] = 0 } 167 sub_filter(plane, pw, ph, sub) 168 let dp: i64 = dfl_encode(sub, n, dfo, cap) 169 paeth_filter(plane, pw, ph, sub) 170 let dpa: i64 = dfl_encode(sub, n, dfo, cap) 171 adaptive_filter(plane, pw, ph, sub, 5) 172 let dad: i64 = dfl_encode(sub, n, dfo, cap) 173 adaptive_filter(plane, pw, ph, sub, 6) 174 let dad6: i64 = dfl_encode(sub, n, dfo, cap) 175 acc[0] = acc[0] + dm 176 acc[1] = acc[1] + dp 177 acc[4] = acc[4] + dpa 178 acc[6] = acc[6] + dad 179 acc[8] = acc[8] + dad6 180 if dm < dp { acc[2] = acc[2] + 1 } 181 var winp: i64 = 0 182 if dm < dpa { winp = 1; acc[5] = acc[5] + 1 } 183 if dm < dad { acc[7] = acc[7] + 1 } 184 var wins6: i64 = 0 185 if dad6 < dad { wins6 = 1; acc[9] = acc[9] + 1 } 186 bp(label); bp(" PNGadaptive="); bn(dad); bp(" sovMEDaug="); bn(dad6); bp(" (MEDalone="); bn(dm); bp(" Paeth="); bn(dpa); bp(")") 187 bp(" sov<PNG="); if wins6==1 { bp("Y") } else { bp("=") } 188 bp(" rt="); bn(ok); bp("\n") 189 return 0 190} 191 192func main() -> i64 { 193 let path: *u8 = "knowledge/staging/media/gallery/20260612_000008_c21f8b7c_3860894667_laptop.png" as *u8 194 let lp: *i64 = sys_mmap(64) as *i64 195 let data: *u8 = sys_read_file(path, lp) 196 let len: i64 = lp[0] 197 if len <= 64 { bp("READ-FAIL\n"); sys_exit(2) } 198 let w: i64 = be32(data, 16) 199 let h: i64 = be32(data, 20) 200 let bd: i64 = data[24] as i64 & 255 201 let ct: i64 = data[25] as i64 & 255 202 bp("image w="); bn(w); bp(" h="); bn(h); bp(" bit_depth="); bn(bd); bp(" color_type="); bn(ct); bp("\n") 203 if bd != 8 { bp("UNSUPPORTED-DEPTH\n"); sys_exit(2) } 204 if ct != 2 { bp("NOT-RGB\n"); sys_exit(2) } 205 let nch: i64 = 3 206 let res: *NxPngResult = nx_png_decode(data, len) 207 let px: *u8 = res.pixels 208 if px == (0 as *u8) { bp("DECODE-FAIL\n"); sys_exit(2) } 209 let pw: i64 = 64 210 let ph: i64 = 64 211 let cap: i64 = pw*ph*4 + 64 212 let plane: *u8 = sys_mmap(pw*ph + 64) 213 let scratch: *u8 = sys_mmap(cap*5 + 64) 214 let acc: *i64 = sys_mmap(128) as *i64 215 acc[0] = 0; acc[1] = 0; acc[2] = 0; acc[3] = 1; acc[4] = 0; acc[5] = 0; acc[6] = 0; acc[7] = 0; acc[8] = 0; acc[9] = 0 216 // 3 deterministic patch origins (1/4, center, 3/4) -- no cherry-pick 217 let xs: *i64 = sys_mmap(64) as *i64 218 let ys: *i64 = sys_mmap(64) as *i64 219 xs[0] = w/4 - pw/2; ys[0] = h/4 - ph/2 220 xs[1] = w/2 - pw/2; ys[1] = h/2 - ph/2 221 xs[2] = (3*w)/4 - pw/2; ys[2] = (3*h)/4 - ph/2 222 var planes: i64 = 0 223 var pidx: i64 = 0 224 while pidx < 3 { 225 var c: i64 = 0 226 while c < nch { 227 extract(px, w, nch, c, xs[pidx], ys[pidx], pw, ph, plane) 228 bp("patch"); bn(pidx); bp(" ch"); bn(c); 229 bench_plane(" " as *u8, plane, pw, ph, scratch, cap, acc) 230 planes = planes + 1 231 c = c + 1 232 } 233 pidx = pidx + 1 234 } 235 // tamper check (no-false-green) on the center green-channel plane 236 extract(px, w, nch, 1, xs[1], ys[1], pw, ph, plane) 237 let n: i64 = pw*ph 238 let med: *u8 = scratch 239 let dfo: *u8 = (scratch as i64 + 2*cap) as *u8 240 let dec: *u8 = (scratch as i64 + 3*cap) as *u8 241 let rec: *u8 = (scratch as i64 + 4*cap) as *u8 242 med_filter(plane, pw, ph, med) 243 let dm: i64 = dfl_encode(med, n, dfo, cap) 244 let k: i64 = dfl_decode(dfo, dm, dec, cap) 245 dec[10] = (dec[10] + 1) as u8 246 med_unfilter(dec, pw, ph, rec) 247 var tamper_detected: i64 = 0 248 var ti: i64 = 0 249 while ti < n { if rec[ti] != plane[ti] { tamper_detected = 1 } ti = ti + 1 } 250 var green: i64 = 0 251 if acc[3] == 1 { if tamper_detected == 1 { green = 1 } } 252 bp("=== REAL-IMAGE (same sovereign deflate): sovereign MED-augmented-adaptive beats real-PNG-adaptive on "); bn(acc[9]); bp("/"); bn(planes) 253 bp(" planes totals: PNGadaptive="); bn(acc[6]); bp(" sovMEDaug="); bn(acc[8]) 254 bp(" (ref MEDalone="); bn(acc[0]); bp(" Paeth="); bn(acc[4]); bp(" Sub="); bn(acc[1]); bp(")") 255 bp(" round_trip_all="); bn(acc[3]); bp(" tamper_detected="); bn(tamper_detected); bp(" ===\n") 256 let lf: i64 = sys_openat_wr("knowledge/status/codec_real_med.log" as *u8, 0x1a4) 257 if lf >= 0 { 258 lw(lf, "CODECREALGATE verdict=" as *u8) 259 if green == 1 { lw(lf, "GREEN" as *u8) } else { lw(lf, "RED" as *u8) } 260 lw(lf, " gap=X-CDC-FRAME-LZ-001 corpus=REAL-photo-1024x768-RGB patches=3x64x64-deterministic test=MED-vs-PNG-fixed-filters-same-sovereign-deflate MED_lt_Sub=" as *u8); ln(lf, acc[2]) 261 lw(lf, "/"); ln(lf, planes) 262 lw(lf, " sovMEDaug_lt_realPNGadaptive="); ln(lf, acc[9]) 263 lw(lf, "/"); ln(lf, planes) 264 lw(lf, " sovMEDaug_total="); ln(lf, acc[8]) 265 lw(lf, " PNGadaptive_total="); ln(lf, acc[6]) 266 lw(lf, " MEDalone_total="); ln(lf, acc[0]) 267 lw(lf, " Paeth_total="); ln(lf, acc[4]) 268 lw(lf, " Sub_total="); ln(lf, acc[1]) 269 lw(lf, " MED_lt_Paeth="); ln(lf, acc[5]) 270 lw(lf, "/"); ln(lf, planes) 271 lw(lf, " round_trip_all="); ln(lf, acc[3]) 272 lw(lf, " tamper_detected="); ln(lf, tamper_detected) 273 lw(lf, " sovereign=both-sides no-wave\n" as *u8) 274 sys_close(lf) 275 } 276 if green == 1 { sys_exit(0) } 277 sys_exit(1) 278 return 1 279}