code wiki / _hdl_build / nx_vcodec_stdemit.nx
nx_vcodec_stdemit.nx source
↩ module page · 93 lines · 4823 B
1// nx_vcodec_stdemit.nx -- Encodes the akiyo_cif sequence with rct8 at varying QP values, emitting Y4M reconstructions and encoded size for external benchmarking.
2import "nx_gate_base.nx"
3// nx_vcodec_stdemit.nx -- encode the STANDARD akiyo_cif sequence with our best mode (rct8) at a qp sweep,
4// EMIT each reconstruction as a Y4M + print the encoded byte size. This lets the ffmpeg oracle score OUR
5// reconstruction with the EXACT SAME psnr/vmaf filter it uses on x264/x265/AV1 -> a bulletproof apples-to-
6// apples external head-to-head (MSU methodology). 48-frame GOP (keyframe at f=0), matches -g 48 -bf 0 on the
7// reference encoders (the low-latency / RTC-honest config, our actual competitor). license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_video_codec_wasm.nx"
10const K_MAGIC_2097152: i64 = 2097152
11const K_MAGIC_5120: i64 = 5120
12
13const SW: i64 = 352
14const SH: i64 = 288
15const NF: i64 = 48
16
17func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw("
18" as *u8); return ok }
19func gn(v: i64) -> i64 {
20 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
21 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}
22 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
23func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 }
24
25func main() -> i64 {
26 gw("=== nx_vcodec_stdemit: encode akiyo_cif rct8 @ qp sweep, emit recon Y4M for the ffmpeg oracle ===\n" as *u8)
27 let box: *i64 = sys_mmap(16) as *i64
28 let raw: *u8 = sys_read_file("/tmp/akiyo_cif.y4m" as *u8, box)
29 if (raw as i64) == 0 { gw("cannot read /tmp/akiyo_cif.y4m -> RED\n" as *u8); return 1 }
30 let total: i64 = box[0]
31 var hi: i64 = 0
32 while hi < total { if (raw[hi]&0xff)==10 { break } hi=hi+1 }
33 if hi >= total { gw("no y4m header -> RED\n" as *u8); return 1 }
34 hi = hi + 1
35 let N: i64 = SW*SH; let C2: i64 = (SW/2)*(SH/2); let fdata: i64 = N + 2*C2; let sz: i64 = fdata
36 // frame data pointers
37 let fp: *i64 = sys_mmap(128*8) as *i64
38 var nf: i64 = 0; var p: i64 = hi
39 while nf < NF {
40 if p >= total { break }
41 var q: i64 = p
42 while q < total { if (raw[q]&0xff)==10 { break } q=q+1 }
43 q = q + 1
44 if q + fdata > total { break }
45 fp[nf] = (raw as i64) + q; nf = nf + 1; p = q + fdata
46 }
47 if nf < NF { gw("only " as *u8); gn(nf); gw(" frames (<48) -> RED\n" as *u8); return 1 }
48
49 let prevE: *u8=sys_mmap(sz+64); let reconE: *u8=sys_mmap(sz+64)
50 let wire: *u8=sys_mmap(K_MAGIC_2097152); let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64
51 let est: *i64=sys_mmap(64) as *i64; let probs: *i64=sys_mmap(32*8) as *i64; let rcbuf: *u8=sys_mmap(K_MAGIC_2097152)
52 let t8c: *i64=sys_mmap(K_MAGIC_5120) as *i64; let rctx: *i64=sys_mmap(64) as *i64
53 vc_t8_init(t8c)
54 let hdr: *u8 = "YUV4MPEG2 W352 H288 F30000:1001 Ip A1:1 C420mpeg2\n" as *u8
55 var hlen: i64=0; while hdr[hlen]!=(0 as u8) { hlen=hlen+1 }
56 let fmark: *u8 = "FRAME\n" as *u8
57 let path: *u8 = sys_mmap(64)
58 let pbase: *u8 = "/tmp/ours_q0.y4m" as *u8
59
60 let qs: *i64=sys_mmap(64) as *i64; qs[0]=2; qs[1]=4; qs[2]=8; qs[3]=16
61 var qi: i64=0
62 while qi < 4 {
63 let qp: i64 = qs[qi]
64 // build path /tmp/ours_qN.y4m (N = qi digit)
65 var pc: i64=0; while pc < 16 { path[pc]=pbase[pc]; pc=pc+1 } path[16]=0 as u8
66 path[11] = (48 + qi) as u8 // the '0' in "ours_q0" -> qi
67 let fd: i64 = sys_openat_wr(path, 420) // 0644
68 if fd < 0 { gw("cannot open out\n" as *u8); return 1 }
69 sys_write(fd, hdr, hlen)
70 var z: i64=0; while z<sz { prevE[z]=0 as u8; z=z+1 }
71 rctx[0]=1; rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64; rctx[5]=0; rctx[6]=0; rctx[7]=0
72 var bytes: i64=0
73 var f: i64=0
74 while f < NF {
75 let cur: *u8 = fp[f] as *u8
76 var key: i64=0; if f==0 { key=1 }
77 rctx[0]=1
78 let nb: i64 = vv_enc_rct8(cur, prevE, reconE, SW, SH, qp, key, qp*188, wire, K_MAGIC_2097152, blk, mv, rctx)
79 if nb<=0 { gw("enc fail\n" as *u8); return 1 }
80 bytes=bytes+nb
81 sys_write(fd, fmark, 6)
82 sys_write(fd, reconE, sz)
83 cpb(prevE, reconE, sz)
84 f=f+1
85 }
86 sys_close(fd)
87 let kbps: i64 = (bytes*8*30)/(NF*1000)
88 gw(" qp=" as *u8); gn(qp); gw(" bytes=" as *u8); gn(bytes); gw(" kbps@30=" as *u8); gn(kbps)
89 gw(" -> " as *u8); gw(path); gw("\n" as *u8)
90 qi=qi+1
91 }
92 gw("STDEMIT: DONE -- 4 recon Y4Ms written for ffmpeg scoring\n" as *u8)
93 return 0 }