code wiki / _hdl_build / nx_vcodec_look_dump.nx
nx_vcodec_look_dump.nx source
↩ module page · 135 lines · 6808 B
1// nx_vcodec_look_dump.nx -- THE LOOK GATE's frame extractor (2026-07-29, seq1262/seq1247 class).
2// PSNR-BD is blind to structured texture loss: a lambda/quant change can be BD-GREEN while making
3// real frames smeary (the 786/787 field-blockiness class). This organ produces the artifact a
4// visual verdict needs: encode the SHIPPED CHAIN (key=vv_enc_rc qp*188, P=vv_enc_rct9 qp*94,
5// same seed discipline as nx_vcodec_msu_ours13) up to a chosen frame, then write SOURCE and RECON
6// luma as 8-bit grayscale BMPs (browser/tool-viewable, zero external deps) and print the cumulative
7// stream bytes -- matched-RATE pairs across encoder arms are selected on that number, never on qp.
8// CIF only (352x288; width divisible by 4 so BMP rows need no padding).
9// usage: nx_vcodec_look_dump <emode> <y4m> <qp> <dumpframe 1..47> <outprefix>
10// license: ORIGINAL
11import "nx_syscalls.nx"
12import "nx_video_codec_wasm.nx"
13const BMP_MAGIC_2835: i64 = 2835
14const BMP_MAGIC_4194304: i64 = 4194304
15const BMP_MAGIC_8192: i64 = 8192
16const BMP_MAGIC_65536: i64 = 65536
17
18const SW: i64 = 352
19const SH: i64 = 288
20const NF: i64 = 48
21const KEYMUL: i64 = 188
22const PMUL: i64 = 94
23const BMP_HDR: i64 = 1078 // 14 file + 40 info + 256*4 grayscale palette
24
25func gw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
26func gn(v: i64) -> i64 {
27 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m}
28 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}
29 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
30func cpb(d: *u8, s: *u8, n: i64) -> i64 { var i: i64=0; while i<n { d[i]=s[i]; i=i+1 } return 0 }
31func atoi(a: *u8) -> i64 { var v: i64=0; var i: i64=0
32 while a[i] != (0 as u8) { v = v*10 + ((a[i]&0xff)-48); i=i+1 } return v }
33func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
34
35func wr32le(b: *u8, off: i64, v: i64) -> i64 {
36 b[off]=(v&0xff) as u8; b[off+1]=((v>>8)&0xff) as u8
37 b[off+2]=((v>>16)&0xff) as u8; b[off+3]=((v>>24)&0xff) as u8; return 0 }
38func wr16le(b: *u8, off: i64, v: i64) -> i64 {
39 b[off]=(v&0xff) as u8; b[off+1]=((v>>8)&0xff) as u8; return 0 }
40
41// 8-bit grayscale BMP, bottom-up rows, no padding needed at SW=352.
42func bmp_write(path: *u8, luma: *u8) -> i64 {
43 let isz: i64 = SW*SH
44 let fsz: i64 = BMP_HDR + isz
45 let buf: *u8 = sys_mmap(fsz + 64)
46 buf[0]=66 as u8; buf[1]=77 as u8 // 'B','M'
47 wr32le(buf, 2, fsz); wr32le(buf, 6, 0); wr32le(buf, 10, BMP_HDR)
48 wr32le(buf, 14, 40); wr32le(buf, 18, SW); wr32le(buf, 22, SH)
49 wr16le(buf, 26, 1); wr16le(buf, 28, 8)
50 wr32le(buf, 30, 0); wr32le(buf, 34, isz)
51 wr32le(buf, 38, BMP_MAGIC_2835); wr32le(buf, 42, BMP_MAGIC_2835)
52 wr32le(buf, 46, 256); wr32le(buf, 50, 0)
53 var c: i64=0
54 while c<256 { let o: i64=54+c*4
55 buf[o]=c as u8; buf[o+1]=c as u8; buf[o+2]=c as u8; buf[o+3]=0 as u8; c=c+1 }
56 var y: i64=0
57 while y<SH { var x: i64=0
58 while x<SW { buf[BMP_HDR + (SH-1-y)*SW + x] = luma[y*SW + x]; x=x+1 } y=y+1 }
59 let fd: i64 = sys_openat_wr(path, 0x1a4) // 0644
60 if fd < 0 { gw("bmp open fail\n" as *u8); return 1 }
61 sys_write(fd, buf, fsz)
62 sys_close(fd)
63 return 0 }
64
65func frames(yuv: *u8, hi: i64, total: i64, fp: *i64, fdata: i64) -> i64 {
66 var nf: i64=0; var p: i64=hi
67 while nf < NF { if p>=total { break }
68 var q: i64=p; while q<total { if (yuv[q]&0xff)==10 { break } q=q+1 } q=q+1
69 if q+fdata>total { break } fp[nf]=(yuv as i64)+q; nf=nf+1; p=q+fdata }
70 return nf }
71
72func seed(rctx: *i64, emode: i64, est: *i64, probs: *i64, rcbuf: *u8, t8c: *i64, plane: *i64) -> i64 {
73 rctx[0]=emode; rctx[1]=est as i64; rctx[2]=probs as i64; rctx[3]=rcbuf as i64; rctx[4]=t8c as i64
74 rctx[5]=0; rctx[6]=0; rctx[7]=0; rctx[8]=0; rctx[9]=plane as i64
75 return 0 }
76
77func pathcat(dst: *u8, pre: *u8, suf: *u8) -> i64 {
78 let n: i64 = slen(pre)
79 cpb(dst, pre, n)
80 let m: i64 = slen(suf)
81 cpb(((dst as i64)+n) as *u8, suf, m)
82 dst[n+m] = 0 as u8
83 return 0 }
84
85func main(argc: i64, argv: *i64) -> i64 {
86 if argc < 6 { gw("usage: nx_vcodec_look_dump <emode> <y4m> <qp> <dumpframe> <outprefix>\n" as *u8); return 1 }
87 let emode: i64 = atoi(argv[1] as *u8)
88 let ypath: *u8 = argv[2] as *u8
89 let qp: i64 = atoi(argv[3] as *u8)
90 let df: i64 = atoi(argv[4] as *u8)
91 let pre: *u8 = argv[5] as *u8
92 let box: *i64 = sys_mmap(16) as *i64
93 let yuv: *u8 = sys_read_file(ypath, box)
94 if (yuv as i64)==0 { gw("cannot read seq y4m -> RED\n" as *u8); return 1 }
95 let total: i64=box[0]
96 var hi: i64=0; while hi<total { if (yuv[hi]&0xff)==10 { break } hi=hi+1 } hi=hi+1
97 let C2: i64=(SW/2)*(SH/2); let fdata: i64=SW*SH+2*C2; let sz: i64=fdata
98 let fp: *i64=sys_mmap(128*8) as *i64
99 let nf: i64=frames(yuv, hi, total, fp, fdata)
100 if nf <= df { gw("too few frames -> RED\n" as *u8); return 1 }
101 let prevE: *u8=sys_mmap(sz+64); let reconE: *u8=sys_mmap(sz+64)
102 let wire: *u8=sys_mmap(BMP_MAGIC_4194304); let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64
103 let est: *i64=sys_mmap(64) as *i64; let probs: *i64=sys_mmap(64*8) as *i64; let rcbuf: *u8=sys_mmap(BMP_MAGIC_4194304)
104 let t8c: *i64=sys_mmap(BMP_MAGIC_8192) as *i64; let rctx: *i64=sys_mmap(128) as *i64
105 let plane: *i64=sys_mmap(BMP_MAGIC_65536) as *i64
106 vc_t8_init(t8c)
107 var z: i64=0; while z<sz { prevE[z]=0 as u8; z=z+1 }
108 var cum: i64=0
109 let NL: i64=SW*SH
110 var f: i64=0
111 while f <= df {
112 let cur: *u8=fp[f] as *u8
113 var key: i64=0; if f==0 { key=1 }
114 seed(rctx, emode, est, probs, rcbuf, t8c, plane)
115 var nb: i64 = 0
116 if key==1 { nb = vv_enc_rc(cur, prevE, reconE, SW, SH, qp, 1, qp*KEYMUL, wire, BMP_MAGIC_4194304, blk, mv, rctx) }
117 else { nb = vv_enc_rct9(cur, prevE, reconE, SW, SH, qp, qp*PMUL, wire, BMP_MAGIC_4194304, blk, mv, rctx) }
118 if nb<=0 { gw("enc fail\n" as *u8); return 1 }
119 cum=cum+nb
120 if f == df {
121 var pxi: i64=0; var fsse: i64=0
122 while pxi<NL { let d: i64=(reconE[pxi]&0xff)-(cur[pxi]&0xff); fsse=fsse+d*d; pxi=pxi+1 }
123 let pbuf: *u8=sys_mmap(512)
124 pathcat(pbuf, pre, "_src.bmp" as *u8)
125 if bmp_write(pbuf, cur) != 0 { return 1 }
126 pathcat(pbuf, pre, "_rec.bmp" as *u8)
127 if bmp_write(pbuf, reconE) != 0 { return 1 }
128 gw("LOOKDUMP emode=" as *u8); gn(emode); gw(" q=" as *u8); gn(qp)
129 gw(" frame=" as *u8); gn(f); gw(" cumbytes=" as *u8); gn(cum)
130 gw(" framebytes=" as *u8); gn(nb); gw(" fsse=" as *u8); gn(fsse); gw("\n" as *u8)
131 }
132 cpb(prevE, reconE, sz)
133 f=f+1
134 }
135 return 0 }