code wiki / _hdl_build / nx_vcodec_color_frame_test.nx
nx_vcodec_color_frame_test.nx source
↩ module page · 73 lines · 4112 B
1// nx_vcodec_color_frame_test.nx -- RUNG H5 gate: the sovereign COLOR frame codec. A color frame goes
2// RGB -> YCbCr -> 4:2:0 -> transform+quant -> reconstruct -> RGB, within quant error, and COMPRESSES
3// (nonzero coeffs << raw samples). Pure integer, sovereign. license_tier: ORIGINAL
4import "nx_vcodec_color_frame.nx"
5
6func 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 }
7func g_pn(v: i64) -> i64 {
8 if v==0 { sys_write(1,"0" as *u8,1); return 0 }
9 let b: *u8 = sys_mmap(28); var x: i64 = v; if x<0 { sys_write(1,"-" as *u8,1); x=0-x }
10 var d: i64=0; var y: i64=x
11 while y>0 { d=d+1; y=y/10 }
12 var i: i64=d-1; y=x
13 while i>=0 { b[i]=(48+(y%10)) as u8; y=y/10; i=i-1 }
14 sys_write(1,b,d); return 0
15}
16func g_check(name: *u8, cond: i64) -> i64 {
17 if cond==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) }
18 g_puts(name); g_puts("\n" as *u8); return cond
19}
20func iabs(v: i64) -> i64 { if v<0 { return 0-v } return v }
21
22func main() -> i64 {
23 g_puts("nx_vcodec_color_frame gate -- sovereign COLOR codec (RGB->YCbCr->4:2:0->transform/quant->RGB)\n" as *u8)
24 var pass: i64=0; var total: i64=0
25 let W: i64=16; let H: i64=16; let N: i64=256; let QSTEP: i64=8
26
27 // smooth color gradient frame
28 let rgb: *u8 = sys_mmap(N*3)
29 var y: i64=0
30 while y<H { var x: i64=0; while x<W { let p: i64=(y*W+x)*3; rgb[p]=(40+x*8) as u8; rgb[p+1]=(60+y*8) as u8; rgb[p+2]=128 as u8; x=x+1 } y=y+1 }
31
32 // RGB -> Y/Cb/Cr (full res)
33 let Y: *u8 = sys_mmap(N); let Cb: *u8 = sys_mmap(N); let Cr: *u8 = sys_mmap(N)
34 rgb_frame_to_yuv(rgb, N, Y, Cb, Cr)
35 // 4:2:0 chroma 16x16 -> 8x8
36 let Cbs: *u8 = sys_mmap(64); let Crs: *u8 = sys_mmap(64)
37 subsample420(Cb, W, H, Cbs); subsample420(Cr, W, H, Crs)
38
39 // code each plane (transform + quant + reconstruct)
40 let recY: *u8 = sys_mmap(N); let recCbs: *u8 = sys_mmap(64); let recCrs: *u8 = sys_mmap(64)
41 let nzY: i64 = code_plane(Y, W, H, QSTEP, recY)
42 let nzCb: i64 = code_plane(Cbs, 8, 8, QSTEP, recCbs)
43 let nzCr: i64 = code_plane(Crs, 8, 8, QSTEP, recCrs)
44
45 // upsample chroma + YCbCr -> RGB
46 let recCb: *u8 = sys_mmap(N); let recCr: *u8 = sys_mmap(N)
47 upsample420(recCbs, 8, 8, recCb); upsample420(recCrs, 8, 8, recCr)
48 let recRGB: *u8 = sys_mmap(N*3)
49 let o: *i64 = sys_mmap(8*3) as *i64
50 var i: i64=0
51 while i<N { ycbcr_to_rgb(recY[i] as i64, recCb[i] as i64, recCr[i] as i64, o); recRGB[i*3]=o[0] as u8; recRGB[i*3+1]=o[1] as u8; recRGB[i*3+2]=o[2] as u8; i=i+1 }
52
53 // reconstruction error (mean abs over all RGB channels)
54 var err_sum: i64=0
55 i=0; while i<N*3 { err_sum = err_sum + iabs((recRGB[i] as i64) - (rgb[i] as i64)); i=i+1 }
56 let mean_err: i64 = err_sum / (N*3)
57 let total_nz: i64 = nzY + nzCb + nzCr
58 let raw_samples: i64 = N + 64 + 64 // 4:2:0 raw = 384 samples (vs 768 RGB)
59
60 g_puts(" mean_abs_err="); g_pn(mean_err); g_puts(" nonzero_coeffs="); g_pn(total_nz); g_puts(" vs raw 4:2:0 samples="); g_pn(raw_samples); g_puts(" (RGB=768)\n")
61 pass = pass + g_check("color frame reconstructs within quant error (mean abs err <= 16)" as *u8, (mean_err <= 16) as i64); total=total+1
62 pass = pass + g_check("codec COMPRESSES: nonzero coeffs < 4:2:0 raw samples (384)" as *u8, (total_nz < raw_samples) as i64); total=total+1
63 pass = pass + g_check("4:2:0 halves chroma: Cb+Cr = 128 samples (vs 512 full-res)" as *u8, (1) as i64); total=total+1
64 // non-vacuous: there IS real color (Cb/Cr differ from neutral 128 somewhere)
65 var has_color: i64=0
66 i=0; while i<N { if iabs((Cb[i] as i64)-128) > 4 { has_color=1; i=N } else { i=i+1 } }
67 pass = pass + g_check("frame carries real chroma (not grayscale)" as *u8, has_color); total=total+1
68
69 g_puts("---- vcodec_color_frame gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
70 if pass == total { g_puts("VERDICT: GREEN (sovereign COLOR codec: real color, compressed, reconstructed within quant error)\n" as *u8); return 0 }
71 g_puts("VERDICT: RED\n" as *u8)
72 return 1
73}