code wiki / _hdl_build / nx_vcodec_realframe_gate.nx

nx_vcodec_realframe_gate.nx source

↩ module page · 75 lines · 5228 B

1// nx_vcodec_realframe_gate.nx -- MEASURES our sovereign video codec on REAL video content, not a synthetic frame. 2// The luma plane of ref_frame0.yuv is a real H.264 I-frame our own decoder (nx_h264_decode_frame) reconstructed 3// 100% bit-exact vs ffmpeg (768x768 YUV420 -> first 589824 bytes = the Y plane). We encode that real frame as a 4// keyframe at three quality points, prove an INDEPENDENT decoder reconstructs bit-exact, and report bytes + ratio + 5// PSNR (centi-dB, via the shared qm_psnr_cdb). This closes RT-003's "synthetic frames" gap with zero third-party code. 6import "nx_syscalls.nx" 7import "nx_gate_emit_lib.nx" 8import "nx_vcodec.nx" 9import "nx_quality_metric.nx" 10 11func g_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v } 12func 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 } 13func 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 } 14 15// encode the real frame at one QP; print measured bytes/ratio/PSNR; return 1 if the independent decode is bit-exact. 16func measure_qp(f0: *u8, W: i64, H: i64, N: i64, qp: i64, eR: *u8, dR: *u8, stream: *u8, blk: *i64, mv: *i64) -> i64 { 17 let bits: i64 = vc_enc_frame_packed(f0, dR, eR, W, H, qp, 1, 64, stream, blk, mv) 18 let bytes: i64 = (bits + 7) / 8 19 vc_dec_frame_packed(dR, dR, W, H, qp, stream, blk, mv) 20 let exact: i64 = frame_equal(eR, dR, N) 21 let mx: i64 = frame_maxerr(eR, f0, N) 22 let psnr: i64 = qm_psnr_cdb(eR, f0, N) 23 let ratio_x10: i64 = (N * 10) / bytes 24 g_puts(" [measure] QP=" as *u8); g_pn(qp); g_puts(": stream=" as *u8); g_pn(bytes) 25 g_puts(" B ratio=" as *u8); g_pn(ratio_x10/10); g_puts("." as *u8); g_pn(ratio_x10%10); g_puts("x") 26 g_puts(" PSNR=" as *u8); g_pn(psnr/100); g_puts("." as *u8); g_pn(psnr%100); g_puts("dB max-err=" as *u8); g_pn(mx) 27 g_puts(" bit-exact=" as *u8); g_pn(exact); g_puts("\n" as *u8) 28 return exact 29} 30 31func main() -> i64 { 32 g_puts("nx_vcodec REAL-FRAME gate (our codec on a real 576x1024 H.264-decoded luma frame, MEASURED)\n" as *u8) 33 var pass: i64 = 0; var total: i64 = 0 34 35 let box: *i64 = sys_mmap(16) as *i64 36 let yuv: *u8 = sys_read_file("/mnt/c/Users/elder/nishi-core/nxc2/knowledge/staging/media/ref_frame0.yuv" as *u8, box) 37 if (yuv as i64) == 0 { g_puts(" FAIL cannot read ref_frame0.yuv\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 38 let flen: i64 = box[0] 39 // TRUE geometry is 576x1024 (ffprobe-confirmed on realtest.mp4; 36x64=2304 MBs matches the decoder). 40 // The old 768x768 was a latent bug: same byte count (589824) but wrong layout -> scrambled neighbors 41 // that break intra prediction. Fixed for an honest, fair-vs-x264 measurement (CLAUDE.md rule #1). 42 let W: i64 = 576; let H: i64 = 1024; let N: i64 = W*H // luma plane = first 589824 bytes of the YUV420 file 43 44 g_puts(" [source] ref_frame0.yuv bytes=" as *u8); g_pn(flen); g_puts(" (576x1024 YUV420; luma N=" as *u8); g_pn(N); g_puts(")\n" as *u8) 45 pass = pass + g_check("real asset present and large enough for the 576x1024 luma plane" as *u8, flen >= N); total=total+1 46 if flen < N { g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 } 47 48 let f0: *u8 = yuv // the real luma plane (first N bytes) 49 let eR: *u8 = sys_mmap(N) as *u8 50 let dR: *u8 = sys_mmap(N) as *u8 51 let stream: *u8 = sys_mmap(4194304) as *u8 // 4 MB: real detailed content costs more bits than a synthetic flat frame 52 let blk: *i64 = sys_mmap(16*8) as *i64 53 let mv: *i64 = sys_mmap(2*8) as *i64 54 55 let e1: i64 = measure_qp(f0, W, H, N, 12, eR, dR, stream, blk, mv) // high quality 56 let e2: i64 = measure_qp(f0, W, H, N, 22, eR, dR, stream, blk, mv) // medium 57 let e3: i64 = measure_qp(f0, W, H, N, 32, eR, dR, stream, blk, mv) // low bitrate 58 let e4: i64 = measure_qp(f0, W, H, N, 40, eR, dR, stream, blk, mv) // aggressive 59 let e5: i64 = measure_qp(f0, W, H, N, 48, eR, dR, stream, blk, mv) // most aggressive 60 61 pass = pass + g_check("QP=12 independent decode bit-exact on the real frame" as *u8, e1); total=total+1 62 pass = pass + g_check("QP=22 independent decode bit-exact on the real frame" as *u8, e2); total=total+1 63 pass = pass + g_check("QP=32 independent decode bit-exact on the real frame" as *u8, e3); total=total+1 64 pass = pass + g_check("QP=40 independent decode bit-exact on the real frame" as *u8, e4); total=total+1 65 pass = pass + g_check("QP=48 independent decode bit-exact on the real frame" as *u8, e5); total=total+1 66 67 // compression must be real (smaller than raw) at the low-bitrate point 68 let bits32: i64 = vc_enc_frame_packed(f0, dR, eR, W, H, 32, 1, 64, stream, blk, mv) 69 let bytes32: i64 = (bits32 + 7) / 8 70 pass = pass + g_check("compresses the real frame below raw at QP=32" as *u8, bytes32 < N); total=total+1 71 72 g_puts("---- vcodec real-frame gate: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8) 73 if pass == total { g_puts("verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 74 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 75}