code wiki / _hdl_build / nx_vcodec_realseq_gate.nx

nx_vcodec_realseq_gate.nx source

↩ module page · 107 lines · 5992 B

1// nx_vcodec_realseq_gate.nx -- the REAL-CONTENT P-sequence measurement (settles the ME-gate bytes caveat: 2// the synthetic bench's 4-periodic texture gifted ME lucky alignments, inflating always-search bytes-value; 3// real content is aperiodic). Encodes 12 REAL frames (bframe_test_decoded.yuv = ffmpeg-verified H.264 4// decode, 576x1024 YUV420, luma plane) as key + 11 P-frames through the closed recon chain with the ME 5// gate active, measuring per-frame: bytes, PSNR vs raw, encode/decode us, and bit-exact enc/dec parity. 6// GREEN iff every P decode is bit-exact AND P-avg bytes < key bytes (temporal compression holds on REAL 7// content) AND avg PSNR >= 30dB. Ledger-reported: the honest wire numbers for the room JPEG->vcodec swap. 8// expect_exit: 0 license_tier: ORIGINAL 9import "nx_syscalls.nx" 10import "nx_vcodec.nx" 11import "nx_quality_metric.nx" 12import "nx_metric_ledger.nx" 13 14const RS_W: i64 = 576 15const RS_H: i64 = 1024 16const RS_FRAMES: i64 = 12 17const RS_QP: i64 = 32 18const RS_SAD: i64 = 64 19 20func rw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 21func rn(v: i64) -> i64 { 22 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 23 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} 24 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 25func feq(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 } 26 27func main() -> i64 { 28 rw("=== nx_vcodec_realseq_gate: 12 REAL frames, key + 11 P, ME-gate active -- the honest wire numbers ===\n" as *u8) 29 let N: i64 = RS_W * RS_H 30 let FBYTES: i64 = N + N / 2 // YUV420 frame stride in the file 31 let box: *i64 = sys_mmap(16) as *i64 32 let yuv: *u8 = sys_read_file("/mnt/c/Users/elder/nishi-core/nxc2/knowledge/staging/media/bframe_test_decoded.yuv" as *u8, box) 33 if (yuv as i64)==0 { rw("cannot read bframe_test_decoded.yuv -> RED\n" as *u8); return 1 } 34 if box[0] < RS_FRAMES * FBYTES { rw("file too small for 12 frames -> RED\n" as *u8); return 1 } 35 36 let encR: *u8 = sys_mmap(N + 64) // encoder recon chain 37 let decPrev: *u8 = sys_mmap(N + 64) // decoder chain prev 38 let decOut: *u8 = sys_mmap(N + 64) 39 let stream: *u8 = sys_mmap(4194304) 40 let blk: *i64 = sys_mmap(16*8) as *i64 41 let mv: *i64 = sys_mmap(2*8) as *i64 42 43 // ---- keyframe (frame 0) ---- 44 let f0: *u8 = yuv 45 let tk0: i64 = sys_now_us() 46 let kbits: i64 = vc_enc_frame_packed(f0, decPrev, encR, RS_W, RS_H, RS_QP, 1, RS_SAD, stream, blk, mv) 47 let tk1: i64 = sys_now_us() 48 vc_dec_frame_packed(decPrev, decOut, RS_W, RS_H, RS_QP, stream, blk, mv) 49 let kbytes: i64 = (kbits + 7) / 8 50 let kexact: i64 = feq(encR, decOut, N) 51 let kpsnr: i64 = qm_psnr_cdb(encR, f0, N) 52 rw(" KEY: " as *u8); rn(kbytes); rw("B enc=" as *u8); rn(tk1-tk0); rw("us PSNR=" as *u8); rn(kpsnr/100); rw("." as *u8); rn(kpsnr%100); rw("dB bit-exact=" as *u8); rn(kexact); rw("\n" as *u8) 53 var ci: i64 = 0 54 while ci < N { decPrev[ci] = decOut[ci]; ci = ci + 1 } 55 56 // ---- P-frames 1..11 ---- 57 var pbytes_sum: i64 = 0 58 var psnr_sum: i64 = 0 59 var enc_sum: i64 = 0 60 var dec_sum: i64 = 0 61 var all_exact: i64 = 1 62 var fi: i64 = 1 63 while fi < RS_FRAMES { 64 let cur: *u8 = ((yuv as i64) + fi * FBYTES) as *u8 65 let t0: i64 = sys_now_us() 66 let pbits: i64 = vc_enc_frame_packed(cur, decPrev, encR, RS_W, RS_H, RS_QP, 0, RS_SAD, stream, blk, mv) 67 let t1: i64 = sys_now_us() 68 vc_dec_frame_packed(decPrev, decOut, RS_W, RS_H, RS_QP, stream, blk, mv) 69 let t2: i64 = sys_now_us() 70 if feq(encR, decOut, N) != 1 { all_exact = 0 } 71 let pb: i64 = (pbits + 7) / 8 72 pbytes_sum = pbytes_sum + pb 73 psnr_sum = psnr_sum + qm_psnr_cdb(decOut, cur, N) 74 enc_sum = enc_sum + (t1 - t0) 75 dec_sum = dec_sum + (t2 - t1) 76 if fi <= 3 { rw(" P" as *u8); rn(fi); rw(": " as *u8); rn(pb); rw("B enc=" as *u8); rn(t1-t0); rw("us\n" as *u8) } 77 ci = 0 78 while ci < N { decPrev[ci] = decOut[ci]; ci = ci + 1 } 79 fi = fi + 1 80 } 81 let NP: i64 = RS_FRAMES - 1 82 let pavg: i64 = pbytes_sum / NP 83 let psnr: i64 = psnr_sum / NP 84 let encus: i64 = enc_sum / NP 85 let decus: i64 = dec_sum / NP 86 var encfps: i64 = 0 87 if encus > 0 { encfps = 1000000 / encus } 88 let ratio_vs_key_pct: i64 = pavg * 100 / kbytes 89 rw(" P-AVG: " as *u8); rn(pavg); rw("B (" as *u8); rn(ratio_vs_key_pct); rw("% of key) PSNR=" as *u8); rn(psnr/100); rw("." as *u8); rn(psnr%100); rw("dB enc=" as *u8); rn(encus); rw("us (" as *u8); rn(encfps); rw("fps @576x1024!) dec=" as *u8); rn(decus); rw("us all-bit-exact=" as *u8); rn(all_exact); rw("\n" as *u8) 90 // wire math at CALL resolution: this frame is 8.2x the pixels of 320x224; scale bytes linearly (approx) 91 let call_pavg: i64 = pavg * (320*224) / N 92 rw(" scaled to 320x224 call res ~ " as *u8); rn(call_pavg); rw("B/P-frame -> 30fps x 7rx = " as *u8); rn(call_pavg * 30 * 7 / 1000); rw("KB/s vs 1100KB/s ceiling\n" as *u8) 93 94 rw(" -- return-and-report --\n" as *u8) 95 ml_report("vcodec_realseq_p_bytes" as *u8, pavg, 0 - 1, "B" as *u8, "nx_vcodec_realseq_gate" as *u8) 96 ml_report("vcodec_realseq_p_psnr_cdb" as *u8, psnr, 1, "cdB" as *u8, "nx_vcodec_realseq_gate" as *u8) 97 ml_report("vcodec_realseq_p_enc_us" as *u8, encus, 0 - 1, "us" as *u8, "nx_vcodec_realseq_gate" as *u8) 98 99 var ok: i64 = 1 100 if kexact != 1 { ok = 0 } 101 if all_exact != 1 { ok = 0 } 102 if pavg >= kbytes { ok = 0 } // temporal compression must hold on REAL content 103 if psnr < 3000 { ok = 0 } // >= 30dB 104 if ok == 1 { rw("REALSEQ-GATE verdict=GREEN -- real-content P-frames: temporally compressed, bit-exact, honest wire numbers above\n" as *u8); return 0 } 105 rw("REALSEQ-GATE verdict=RED\n" as *u8) 106 return 1 107}