code wiki / _hdl_build / nx_vcodec_packed_gate.nx

nx_vcodec_packed_gate.nx source

↩ module page · 67 lines · 4259 B

1// nx_vcodec_packed_gate.nx -- proves the TRANSMITTABLE codec split: the encoder writes one self-describing bitstream, 2// and an INDEPENDENT decoder (given only the previous frame + that stream, never the original) reconstructs the frame 3// BIT-EXACT to the encoder's reconstruction. This is the encode->[wire]->decode the vroom daemon will relay. Tests a 4// keyframe (intra) and an inter frame (motion + skip), and that the stream is far smaller than the raw frame. 5import "nx_syscalls.nx" 6import "nx_gate_emit_lib.nx" 7import "nx_vcodec.nx" 8 9func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 10// flat backdrop + a 16x16 object panning 2px/frame (same content shape as nx_vcodec_gate) 11func mkframe(f: *u8, W: i64, H: i64, t: i64) -> i64 { 12 var y: i64 = 0 13 while y < H { var x: i64 = 0 14 while x < W { f[y*W+x] = 200 as u8; x = x + 1 } y = y + 1 } 15 let sqx: i64 = 8 + 2*t 16 var yy: i64 = 16 17 while yy < 32 { var xx: i64 = 0 18 while xx < 16 { let px: i64 = sqx + xx; if px < W { f[yy*W + px] = (140 + (xx / 4)) as u8 } xx = xx + 1 } yy = yy + 1 } 19 return 0 20} 21func frame_equal(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if a[i]!=b[i] { return 0 } i=i+1 } return 1 } 22func frame_maxerr(a: *u8, b: *u8, n: i64) -> i64 { var m: i64=0; var i: i64=0; while i<n { let e: i64=g_abs((a[i] as i64)-(b[i] as i64)); if e>m { m=e } i=i+1 } return m } 23 24func main() -> i64 { 25 g_puts("nx_vcodec packed gate (transmittable encode->stream->independent decode, BIT-EXACT)\n" as *u8) 26 var pass: i64 = 0; var total: i64 = 0 27 let W: i64 = 64; let H: i64 = 64; let N: i64 = W*H 28 let QP: i64 = 12; let SAD_THRESH: i64 = 64 29 30 let f0: *u8 = sys_mmap(N) as *u8 31 let f1: *u8 = sys_mmap(N) as *u8 32 let enc_recon0: *u8 = sys_mmap(N) as *u8 33 let enc_recon1: *u8 = sys_mmap(N) as *u8 34 let dec_recon0: *u8 = sys_mmap(N) as *u8 35 let dec_recon1: *u8 = sys_mmap(N) as *u8 36 let stream: *u8 = sys_mmap(8192) as *u8 37 let blk: *i64 = sys_mmap(16*8) as *i64 38 let mv: *i64 = sys_mmap(2*8) as *i64 39 40 mkframe(f0, W, H, 0) 41 mkframe(f1, W, H, 1) 42 43 // --- KEYFRAME: encode -> stream; decode (prev unused) -> must equal encoder recon --- 44 let kf_bits: i64 = vc_enc_frame_packed(f0, dec_recon0, enc_recon0, W, H, QP, 1, SAD_THRESH, stream, blk, mv) // prev arg unused for keyframe 45 let kf_bytes: i64 = (kf_bits + 7) / 8 46 vc_dec_frame_packed(dec_recon0, dec_recon0, W, H, QP, stream, blk, mv) // decode from stream into dec_recon0 (prev unused) 47 let kf_match: i64 = frame_equal(enc_recon0, dec_recon0, N) 48 let kf_q: i64 = frame_maxerr(enc_recon0, f0, N) 49 g_puts(" [measure] keyframe stream=" as *u8); g_pn(kf_bytes); g_puts(" B (raw=" as *u8); g_pn(N); g_puts(" B) recon max-err vs original=" as *u8); g_pn(kf_q); g_puts("\n" as *u8) 50 pass = pass + g_check("keyframe: independent decoder recon == encoder recon (BIT-EXACT)" as *u8, kf_match); total=total+1 51 pass = pass + g_check("keyframe: stream << raw frame" as *u8, kf_bytes * 4 < N); total=total+1 52 53 // --- INTER: prev = each side's keyframe recon (which are equal); encode f1 -> stream; decode -> equal --- 54 let it_bits: i64 = vc_enc_frame_packed(f1, enc_recon0, enc_recon1, W, H, QP, 0, SAD_THRESH, stream, blk, mv) 55 let it_bytes: i64 = (it_bits + 7) / 8 56 vc_dec_frame_packed(dec_recon0, dec_recon1, W, H, QP, stream, blk, mv) 57 let it_match: i64 = frame_equal(enc_recon1, dec_recon1, N) 58 let it_q: i64 = frame_maxerr(enc_recon1, f1, N) 59 g_puts(" [measure] inter stream=" as *u8); g_pn(it_bytes); g_puts(" B recon max-err vs original=" as *u8); g_pn(it_q); g_puts("\n" as *u8) 60 pass = pass + g_check("inter: independent decoder recon == encoder recon (BIT-EXACT)" as *u8, it_match); total=total+1 61 pass = pass + g_check("inter stream < keyframe stream (the delta win)" as *u8, it_bytes < kf_bytes); total=total+1 62 pass = pass + g_check("reconstruction faithful (inter max-err <= 40)" as *u8, it_q <= 40); total=total+1 63 64 g_puts("---- vcodec packed gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 65 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 66 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 67}