code wiki / _hdl_build / nx_vcodec_color_codec_test.nx
nx_vcodec_color_codec_test.nx source
↩ module page · 58 lines · 3540 B
1// nx_vcodec_color_codec_test.nx -- gate: COLOR through the REAL nx_vcodec engine (the wasm-proven,
2// live-deployed H.264-class codec), one encode per Y/Cb/Cr plane at 4:2:0, fed by the H4 transform. Proves
3// a color frame round-trips within codec error and compresses -- via the engine that already ships, not a
4// parallel codec. license_tier: ORIGINAL
5import "nx_vcodec_color_codec.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_color_codec gate -- COLOR via the REAL nx_vcodec engine (Y/Cb/Cr @ 4:2:0)\n" as *u8)
25 var pass: i64=0; var total: i64=0
26 let W: i64=32; let H: i64=32; let N: i64=1024; let QP: i64=20; let SAD: i64=64
27
28 // smooth color frame (gradient in R and G, fixed-ish B) -> real chroma
29 let rgb: *u8 = sys_mmap(N*3)
30 var y: i64=0; while y<H { var x: i64=0; while x<W { let p: i64=(y*W+x)*3; rgb[p]=(40+x*5) as u8; rgb[p+1]=(50+y*5) as u8; rgb[p+2]=128 as u8; x=x+1 } y=y+1 }
31
32 let strY: *u8 = sys_mmap(8192); let strCb: *u8 = sys_mmap(8192); let strCr: *u8 = sys_mmap(8192)
33 let bits: *i64 = sys_mmap(3*8) as *i64
34 let total_bits: i64 = vc_color_encode_key(rgb, W, H, QP, SAD, strY, strCb, strCr, bits)
35
36 let rgb_out: *u8 = sys_mmap(N*3)
37 vc_color_decode_key(W, H, QP, strY, strCb, strCr, rgb_out)
38
39 var err: i64=0; var i: i64=0; while i<N*3 { err=err+iabs((rgb_out[i] as i64)-(rgb[i] as i64)); i=i+1 }
40 let mean_err: i64 = err/(N*3)
41 let raw_bits: i64 = N*3*8 // 24576 raw RGB bits
42
43 g_puts(" Y_bits="); g_pn(bits[0]); g_puts(" Cb_bits="); g_pn(bits[1]); g_puts(" Cr_bits="); g_pn(bits[2]); g_puts(" total="); g_pn(total_bits); g_puts("/"); g_pn(raw_bits); g_puts(" raw mean_err="); g_pn(mean_err); g_puts("\n")
44 pass = pass + g_check("color frame round-trips through the REAL engine within codec error (mean <= 20)" as *u8, (mean_err <= 20) as i64); total=total+1
45 pass = pass + g_check("compresses vs raw RGB (total coded bits < raw bits)" as *u8, (total_bits < raw_bits) as i64); total=total+1
46 pass = pass + g_check("all 3 planes coded (Y, Cb, Cr each produced bits)" as *u8, ((bits[0]>0) as i64) * ((bits[1]>0) as i64) * ((bits[2]>0) as i64)); total=total+1
47
48 var has_color: i64=0
49 let Yc: *u8 = sys_mmap(N); let Cbc: *u8 = sys_mmap(N); let Crc: *u8 = sys_mmap(N)
50 rgb_frame_to_yuv(rgb, N, Yc, Cbc, Crc)
51 i=0; while i<N { if iabs((Cbc[i] as i64)-128) > 4 { has_color=1; i=N } else { i=i+1 } }
52 pass = pass + g_check("real chroma present (color, not grayscale)" as *u8, has_color); total=total+1
53
54 g_puts("---- vcodec_color_codec gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
55 if pass==total { g_puts("VERDICT: GREEN (COLOR runs on the proven nx_vcodec engine -- the right S-class path, DRY, ready for the wasm color wrapper)\n" as *u8); return 0 }
56 g_puts("VERDICT: RED\n" as *u8)
57 return 1
58}