code wiki / _hdl_build / nx_lossless_codec_gate.nx
nx_lossless_codec_gate.nx source
↩ module page · 100 lines · 6123 B
1// nx_lossless_codec_gate.nx -- sovereign LOSSLESS image codec (the "lossless" S-class capability): PAETH
2// spatial predictor (PNG filter type 4 -- DEFINITIVELY patent-free, PNG was designed patent-free) -> residual
3// -> rANS (public-domain). Decode replays the Paeth prediction from already-decoded neighbors + adds the
4// residual = BIT-EXACT reconstruction by construction. Measured on the REAL frame luma (576x1024). All tools
5// provably clean (Paeth=PD/PNG, rANS=PD) -> passes nx_codec_provenance_gate. The lossless head-to-head vs
6// PNG/FFV1 (3rd-party reference) is the next measured-exceed step. license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_gate_emit_lib.nx"
9import "nx_rans.nx"
10
11func g_num(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)}; 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}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(1,bb,k); return 0 }
12func g_w(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 }
13func g_wn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; 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}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 }
14func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v }
15// Paeth predictor (PNG filter 4): a=left, b=up, c=up-left
16func paeth(a: i64, b: i64, c: i64) -> i64 {
17 let p: i64=a+b-c
18 let pa: i64=iabs(p-a); let pb: i64=iabs(p-b); let pc: i64=iabs(p-c)
19 if pa<=pb { if pa<=pc { return a } return c }
20 if pb<=pc { return b }
21 return c
22}
23
24func main() -> i64 {
25 g_puts("=== LOSSLESS CODEC GATE: sovereign Paeth + rANS, bit-exact on a real frame (576x1024 luma) ===\n" as *u8)
26 let W: i64=576; let H: i64=1024; let N: i64=W*H; let sb: i64=12
27 let flen: *i64=sys_mmap(8) as *i64
28 let fb: *u8=sys_read_file("knowledge/staging/media/ref_frame0.yuv" as *u8, flen)
29 if flen[0] < N { g_puts("FATAL: short read\n" as *u8); sys_exit(2); return 2 }
30
31 // ENCODE: Paeth-predict from original (=lossless) neighbors -> residual bytes
32 let res: *u8=sys_mmap(N+16)
33 var y: i64=0
34 while y<H {
35 var x: i64=0
36 while x<W {
37 var a: i64=0; var b: i64=0; var c: i64=0
38 if x>0 { a=fb[y*W+x-1] as i64 }
39 if y>0 { b=fb[(y-1)*W+x] as i64 }
40 if x>0 { if y>0 { c=fb[(y-1)*W+x-1] as i64 } }
41 let pred: i64=paeth(a,b,c)
42 res[y*W+x]=((fb[y*W+x] as i64)-pred) as u8
43 x=x+1
44 }
45 y=y+1
46 }
47 // rANS the residuals
48 let counts: *i64=sys_mmap(257*8) as *i64; let fr: *i64=sys_mmap(257*8) as *i64; let cu: *i64=sys_mmap(257*8) as *i64; let s2s: *i64=sys_mmap((1<<sb)*8) as *i64
49 rans_count(res, N, counts); rans_normalize(counts, fr, sb); rans_cum(fr, cu); rans_build_slot2sym(fr, cu, s2s)
50 let cap: i64=N + (N/2) + 1024; let enc: *u8=sys_mmap(cap)
51 let enclen: i64=rans_encode(res, N, fr, cu, sb, enc, cap)
52 let total: i64=512+8+enclen // honest: rANS freq table (512) + small header included
53
54 // DECODE: rANS -> residuals -> Paeth-replay from DECODED neighbors -> reconstruct
55 let res2: *u8=sys_mmap(N+16)
56 rans_decode(enc, enclen, N, fr, cu, sb, s2s, res2)
57 var rans_ok: i64=1; var i: i64=0; while i<N { if res2[i]!=res[i] { rans_ok=0 } i=i+1 }
58 let rec: *u8=sys_mmap(N+16)
59 y=0
60 while y<H {
61 var x: i64=0
62 while x<W {
63 var a: i64=0; var b: i64=0; var c: i64=0
64 if x>0 { a=rec[y*W+x-1] as i64 }
65 if y>0 { b=rec[(y-1)*W+x] as i64 }
66 if x>0 { if y>0 { c=rec[(y-1)*W+x-1] as i64 } }
67 let pred: i64=paeth(a,b,c)
68 rec[y*W+x]=((res2[y*W+x] as i64)+pred) as u8
69 x=x+1
70 }
71 y=y+1
72 }
73 var exact: i64=1; var nbad: i64=0
74 i=0; while i<N { if rec[i]!=fb[i] { exact=0; nbad=nbad+1 } i=i+1 }
75
76 // liar-kill: corrupt the stream -> decode must diverge from the residuals
77 let ec: *u8=sys_mmap(cap); i=0; while i<enclen { ec[i]=enc[i]; i=i+1 }
78 let mid: i64=enclen/2; ec[mid]=(((ec[mid] as i64)+1)&0xff) as u8
79 let res3: *u8=sys_mmap(N+16); rans_decode(ec, enclen, N, fr, cu, sb, s2s, res3)
80 var lk: i64=0; i=0; while i<N { if res3[i]!=res2[i] { lk=1 } i=i+1 }
81
82 let ratiox100: i64=(N*100)/total
83 g_puts("-- raw=" as *u8); g_num(N); g_puts("B lossless=" as *u8); g_num(total); g_puts("B (payload=" as *u8); g_num(enclen)
84 g_puts(") ratio=" as *u8); g_num(ratiox100/100); g_puts("." as *u8); let f2: i64=ratiox100%100; if f2<10 { g_puts("0" as *u8) } g_num(f2); g_puts("x bits/px=" as *u8); g_num((total*8*100)/N); g_puts("/100\n" as *u8)
85
86 var pass: i64=0; let rows: i64=5
87 pass=pass+g_check(" T1 rANS residual round-trip lossless" as *u8, rans_ok)
88 pass=pass+g_check(" T2 FULL reconstruction BIT-EXACT vs original (true lossless)" as *u8, exact)
89 var t3: i64=0; if total<N { t3=1 }
90 pass=pass+g_check(" T3 compresses below raw (lossless gain)" as *u8, t3)
91 pass=pass+g_check(" T4 liar-kill: corrupted stream diverges" as *u8, lk)
92 var t5: i64=0; if ratiox100>=130 { t5=1 }
93 pass=pass+g_check(" T5 meaningful lossless ratio (>=1.3x on real photo luma)" as *u8, t5)
94
95 g_puts("----\nLOSSLESS rows=" as *u8); g_num(rows); g_puts(" pass=" as *u8); g_num(pass); g_puts("\n" as *u8)
96 let lg: i64=sys_openat_append("knowledge/status/lossless_codec_gate.log" as *u8, 0x1a4)
97 if lg>=0 { g_w(lg,"LOSSLESS rows=" as *u8); g_wn(lg,rows); g_w(lg," pass=" as *u8); g_wn(lg,pass); g_w(lg," total=" as *u8); g_wn(lg,total); g_w(lg," ratiox100=" as *u8); g_wn(lg,ratiox100); g_w(lg," exact=" as *u8); g_wn(lg,exact); if pass==rows { g_w(lg," verdict=GREEN\n" as *u8) } else { g_w(lg," verdict=RED\n" as *u8) } sys_close(lg) }
98 if pass==rows { g_puts("LOSSLESS GREEN (sovereign Paeth+rANS lossless codec, bit-exact on a real frame, patent-clean)\n" as *u8); sys_exit(0); return 0 }
99 g_puts("LOSSLESS RED\n" as *u8); sys_exit(1); return 1
100}