code wiki / _hdl_build / nx_mp4_mux_gate.nx

nx_mp4_mux_gate.nx source

↩ module page · 86 lines · 5213 B

1import "nx_gate_gn.nx" 2import "nx_gate_base.nx" 3// nx_mp4_mux_gate.nx -- proves the sovereign MP4 muxer emits VALID, walkable ISOBMFF: mux a synthetic video 4// track, then parse it back with the SAME atom walker nx_m4b_book uses (be32/find_atom) -> ftyp/moov/trak/mdia/ 5// minf/stbl/stsz/stco/mdat all present + walkable (a wrong box size breaks the walk), sample count + per-sample 6// sizes round-trip, and the stco chunk offset points at the exact sample bytes in mdat. license_tier: ORIGINAL expect_exit: 0 7import "nx_syscalls.nx" 8import "nx_mp4_mux.nx" 9import "nx_gate_verdict.nx" 10 11func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 12" as *u8); return ok } 13// the SAME parser primitives nx_m4b_book uses (copied so the gate is standalone) 14func be32(b: *u8, o: i64) -> i64 { return ((b[o] as i64)<<24)|((b[o+1] as i64)<<16)|((b[o+2] as i64)<<8)|(b[o+3] as i64) } 15func atype(z: *u8, o: i64, t: *u8) -> i64 { if z[o+4]!=t[0]{return 0} if z[o+5]!=t[1]{return 0} if z[o+6]!=t[2]{return 0} if z[o+7]!=t[3]{return 0} return 1 } 16func find_atom(z: *u8, start: i64, end: i64, t: *u8, ps: *i64, pe: *i64) -> i64 { 17 var o: i64 = start 18 while o + 8 <= end { var sz: i64 = be32(z, o); if sz == 0 { sz = end - o } if sz < 8 { return 0 } if o + sz > end { return 0 } if atype(z, o, t) == 1 { ps[0]=o+8; pe[0]=o+sz; return 1 } o = o + sz } 19 return 0 20} 21 22func main() -> i64 { 23 gw("mp4-mux SOVEREIGN gate (mux a video track -> our own atom walker round-trips ftyp/moov/.../stbl + samples)\n" as *u8) 24 var pass: i64 = 0 25 var ttl: i64 = 0 26 27 // 3 samples: 'A'x10, 'B'x20, 'C'x15 = 45 bytes; a 12-byte fake avcC config 28 let sizes: *i64 = sys_mmap(8*3) as *i64; sizes[0]=10; sizes[1]=20; sizes[2]=15 29 let data: *u8 = sys_mmap(64); var di: i64=0 30 while di<10 { data[di]=65 as u8; di=di+1 } while di<30 { data[di]=66 as u8; di=di+1 } while di<45 { data[di]=67 as u8; di=di+1 } 31 let avcc: *u8 = sys_mmap(12); var ci: i64=0; while ci<12 { avcc[ci]=(0x11+ci) as u8; ci=ci+1 } 32 33 let out: *u8 = sys_mmap(65536) 34 let total: i64 = mp4_mux_video(out, 1280, 720, 12800, 512, avcc, 12, data, sizes, 3) 35 ttl=ttl+1; pass=pass+grow("M1 mux produced non-trivial output\x00" as *u8, (total > 200) as i64) 36 37 // ftyp at 0 38 ttl=ttl+1; pass=pass+grow("M2 ftyp box at offset 0\x00" as *u8, atype(out, 0, "ftyp" as *u8)) 39 40 // walk moov/trak/mdia/minf/stbl (a wrong nested size would break find_atom) 41 let ps: *i64 = sys_mmap(8) as *i64; let pe: *i64 = sys_mmap(8) as *i64 42 var walk: i64 = 1 43 if find_atom(out, 0, total, "moov" as *u8, ps, pe) == 0 { walk = 0 } 44 let mov_s: i64 = ps[0]; let mov_e: i64 = pe[0] 45 if walk==1 { if find_atom(out, mov_s, mov_e, "trak" as *u8, ps, pe) == 0 { walk = 0 } } 46 let trk_s: i64 = ps[0]; let trk_e: i64 = pe[0] 47 if walk==1 { if find_atom(out, trk_s, trk_e, "mdia" as *u8, ps, pe) == 0 { walk = 0 } } 48 let mda_s: i64 = ps[0]; let mda_e: i64 = pe[0] 49 if walk==1 { if find_atom(out, mda_s, mda_e, "minf" as *u8, ps, pe) == 0 { walk = 0 } } 50 let mnf_s: i64 = ps[0]; let mnf_e: i64 = pe[0] 51 if walk==1 { if find_atom(out, mnf_s, mnf_e, "stbl" as *u8, ps, pe) == 0 { walk = 0 } } 52 let stb_s: i64 = ps[0]; let stb_e: i64 = pe[0] 53 ttl=ttl+1; pass=pass+grow("M3 moov/trak/mdia/minf/stbl all walkable (sizes correct)\x00" as *u8, walk) 54 55 // stsz: sample_count == 3, sizes 10/20/15 56 var m4: i64 = 0 57 if walk==1 { if find_atom(out, stb_s, stb_e, "stsz" as *u8, ps, pe) == 1 { 58 let sp: i64 = ps[0] 59 if be32(out, sp+8) == 3 { if be32(out, sp+12)==10 { if be32(out, sp+16)==20 { if be32(out, sp+20)==15 { m4 = 1 } } } } 60 } } 61 ttl=ttl+1; pass=pass+grow("M4 stsz: sample_count=3 + sizes 10,20,15 round-trip\x00" as *u8, m4) 62 63 // stco: chunk offset points at the mdat sample bytes; verify 'A','B','C' pattern there 64 var m5: i64 = 0 65 if walk==1 { if find_atom(out, stb_s, stb_e, "stco" as *u8, ps, pe) == 1 { 66 let off: i64 = be32(out, ps[0]+8) 67 if (out[off]&0xff)==65 { if (out[off+10]&0xff)==66 { if (out[off+30]&0xff)==67 { if (out[off+44]&0xff)==67 { m5 = 1 } } } } 68 } } 69 ttl=ttl+1; pass=pass+grow("M5 stco offset points at the exact sample bytes in mdat\x00" as *u8, m5) 70 71 // mdat payload == the 45 sample bytes 72 var m6: i64 = 0 73 if find_atom(out, 0, total, "mdat" as *u8, ps, pe) == 1 { if (pe[0]-ps[0])==45 { if (out[ps[0]]&0xff)==65 { if (out[pe[0]-1]&0xff)==67 { m6 = 1 } } } } 74 ttl=ttl+1; pass=pass+grow("M6 mdat holds exactly the 45 sample bytes\x00" as *u8, m6) 75 76 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8) 77 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 78 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 79 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 80 let ctr__dry: *i64 = gv_ctr() 81 ctr__dry[0] = pass 82 ctr__dry[1] = ttl 83 let rc__dry: i64 = gv_verdict("MP4-MUX-GATE" as *u8, ctr__dry, "sovereign MP4 muxer emits valid faststart ISOBMFF; our own demuxer round-trips it)" as *u8) 84 sys_exit(rc__dry) 85 return rc__dry 86}