code wiki / _hdl_build / nx_vcodec_inter_test.nx
nx_vcodec_inter_test.nx source
↩ module page · 67 lines · 3730 B
1// nx_vcodec_inter_test.nx -- RUNG H6 gate: COLOR inter-prediction (P-frames). A near-static frame is
2// coded as residual-vs-prev: unchanged blocks SKIP (exact), the changed block carries a tiny residual, and
3// the inter cost is FAR below the intra cost -- the temporal-compression / fps lever. Pure integer, sovereign.
4// license_tier: ORIGINAL
5import "nx_vcodec_inter.nx"
6
7func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
8func g_pn(v: i64) -> i64 {
9 if v==0 { sys_write(1,"0" as *u8,1); return 0 }
10 let b: *u8 = sys_mmap(28); var x: i64 = v; if x<0 { sys_write(1,"-" as *u8,1); x=0-x }
11 var d: i64=0; var y: i64=x
12 while y>0 { d=d+1; y=y/10 }
13 var i: i64=d-1; y=x
14 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 }
15 sys_write(1,b,d); return 0
16}
17func g_check(name: *u8, cond: i64) -> i64 {
18 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) }
19 g_puts(name); g_puts("\n" as *u8); return cond
20}
21func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v }
22
23func main() -> i64 {
24 g_puts("nx_vcodec_inter gate -- COLOR inter-prediction (P-frame): skip + temporal compression\n" as *u8)
25 var pass: i64=0; var total: i64=0
26 let W: i64=16; let H: i64=16; let N: i64=256; let QSTEP: i64=8
27
28 // prev = gradient plane
29 let prev: *u8 = sys_mmap(N)
30 var y: i64=0; while y<H { var x: i64=0; while x<W { prev[y*W+x]=(60+x*4+y*4) as u8; x=x+1 } y=y+1 }
31 // cur = prev with ONLY the top-left 4x4 block changed (+30) -- the rest is identical (static)
32 let cur: *u8 = sys_mmap(N)
33 var i: i64=0; while i<N { cur[i]=prev[i]; i=i+1 }
34 y=0; while y<4 { var x: i64=0; while x<4 { var v: i64=(prev[y*W+x] as i64)+30; if v>255{v=255} cur[y*W+x]=v as u8; x=x+1 } y=y+1 }
35
36 // inter-code (residual vs prev)
37 let rec: *u8 = sys_mmap(N); let changed: *i64 = sys_mmap(8) as *i64
38 let inter_nz: i64 = inter_code_plane(prev, cur, W, H, QSTEP, rec, changed)
39 // intra-code cur for comparison (transforms ALL blocks)
40 let recI: *u8 = sys_mmap(N)
41 let intra_nz: i64 = code_plane(cur, W, H, QSTEP, recI)
42
43 g_puts(" changed_blocks="); g_pn(changed[0]); g_puts("/16 inter_coeffs="); g_pn(inter_nz); g_puts(" intra_coeffs="); g_pn(intra_nz); g_puts("\n")
44
45 pass = pass + g_check("only the moving block is coded (1/16 changed, 15 skip)" as *u8, (changed[0]==1) as i64); total=total+1
46
47 // skip-blocks EXACT: outside the top-left 4x4, rec must equal cur (== prev) exactly
48 var skip_exact: i64=1
49 y=0; while y<H { var x: i64=0; while x<W {
50 var instatic: i64=1; if y<4 { if x<4 { instatic=0 } }
51 if instatic==1 { if rec[y*W+x]!=cur[y*W+x] { skip_exact=0 } }
52 x=x+1 } y=y+1 }
53 pass = pass + g_check("SKIP blocks reconstruct EXACT (static background bit-perfect)" as *u8, skip_exact); total=total+1
54
55 // changed block reconstructs within quant error
56 var err: i64=0; var cnt: i64=0
57 y=0; while y<4 { var x: i64=0; while x<4 { err=err+iabs((rec[y*W+x] as i64)-(cur[y*W+x] as i64)); cnt=cnt+1; x=x+1 } y=y+1 }
58 pass = pass + g_check("moving block reconstructs within quant error (mean <= 4)" as *u8, ((err/cnt) <= 4) as i64); total=total+1
59
60 // THE LEVER: inter cost FAR below intra cost
61 pass = pass + g_check("inter cost << intra cost (temporal compression = the fps lever)" as *u8, (inter_nz * 4 < intra_nz) as i64); total=total+1
62
63 g_puts("---- vcodec_inter gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
64 if pass == total { g_puts("VERDICT: GREEN (color inter-prediction: skip-exact + inter<<intra -- the sovereign fps lever)\n" as *u8); return 0 }
65 g_puts("VERDICT: RED\n" as *u8)
66 return 1
67}