code wiki / _hdl_build / nx_mp4_mux_gate.nx

nx_mp4_mux_gate.nx source

↩ module page · 78 lines · 4835 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" 9 10func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 11" as *u8); return ok } 12// the SAME parser primitives nx_m4b_book uses (copied so the gate is standalone) 13func 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) } 14func 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 } 15func find_atom(z: *u8, start: i64, end: i64, t: *u8, ps: *i64, pe: *i64) -> i64 { 16 var o: i64 = start 17 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 } 18 return 0 19} 20 21func main() -> i64 { 22 gw("mp4-mux SOVEREIGN gate (mux a video track -> our own atom walker round-trips ftyp/moov/.../stbl + samples)\n" as *u8) 23 var pass: i64 = 0 24 var ttl: i64 = 0 25 26 // 3 samples: 'A'x10, 'B'x20, 'C'x15 = 45 bytes; a 12-byte fake avcC config 27 let sizes: *i64 = sys_mmap(8*3) as *i64; sizes[0]=10; sizes[1]=20; sizes[2]=15 28 let data: *u8 = sys_mmap(64); var di: i64=0 29 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 } 30 let avcc: *u8 = sys_mmap(12); var ci: i64=0; while ci<12 { avcc[ci]=(0x11+ci) as u8; ci=ci+1 } 31 32 let out: *u8 = sys_mmap(65536) 33 let total: i64 = mp4_mux_video(out, 1280, 720, 12800, 512, avcc, 12, data, sizes, 3) 34 ttl=ttl+1; pass=pass+grow("M1 mux produced non-trivial output\x00" as *u8, (total > 200) as i64) 35 36 // ftyp at 0 37 ttl=ttl+1; pass=pass+grow("M2 ftyp box at offset 0\x00" as *u8, atype(out, 0, "ftyp" as *u8)) 38 39 // walk moov/trak/mdia/minf/stbl (a wrong nested size would break find_atom) 40 let ps: *i64 = sys_mmap(8) as *i64; let pe: *i64 = sys_mmap(8) as *i64 41 var walk: i64 = 1 42 if find_atom(out, 0, total, "moov" as *u8, ps, pe) == 0 { walk = 0 } 43 let mov_s: i64 = ps[0]; let mov_e: i64 = pe[0] 44 if walk==1 { if find_atom(out, mov_s, mov_e, "trak" as *u8, ps, pe) == 0 { walk = 0 } } 45 let trk_s: i64 = ps[0]; let trk_e: i64 = pe[0] 46 if walk==1 { if find_atom(out, trk_s, trk_e, "mdia" as *u8, ps, pe) == 0 { walk = 0 } } 47 let mda_s: i64 = ps[0]; let mda_e: i64 = pe[0] 48 if walk==1 { if find_atom(out, mda_s, mda_e, "minf" as *u8, ps, pe) == 0 { walk = 0 } } 49 let mnf_s: i64 = ps[0]; let mnf_e: i64 = pe[0] 50 if walk==1 { if find_atom(out, mnf_s, mnf_e, "stbl" as *u8, ps, pe) == 0 { walk = 0 } } 51 let stb_s: i64 = ps[0]; let stb_e: i64 = pe[0] 52 ttl=ttl+1; pass=pass+grow("M3 moov/trak/mdia/minf/stbl all walkable (sizes correct)\x00" as *u8, walk) 53 54 // stsz: sample_count == 3, sizes 10/20/15 55 var m4: i64 = 0 56 if walk==1 { if find_atom(out, stb_s, stb_e, "stsz" as *u8, ps, pe) == 1 { 57 let sp: i64 = ps[0] 58 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 } } } } 59 } } 60 ttl=ttl+1; pass=pass+grow("M4 stsz: sample_count=3 + sizes 10,20,15 round-trip\x00" as *u8, m4) 61 62 // stco: chunk offset points at the mdat sample bytes; verify 'A','B','C' pattern there 63 var m5: i64 = 0 64 if walk==1 { if find_atom(out, stb_s, stb_e, "stco" as *u8, ps, pe) == 1 { 65 let off: i64 = be32(out, ps[0]+8) 66 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 } } } } 67 } } 68 ttl=ttl+1; pass=pass+grow("M5 stco offset points at the exact sample bytes in mdat\x00" as *u8, m5) 69 70 // mdat payload == the 45 sample bytes 71 var m6: i64 = 0 72 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 } } } } 73 ttl=ttl+1; pass=pass+grow("M6 mdat holds exactly the 45 sample bytes\x00" as *u8, m6) 74 75 gw("pass=" as *u8); gn(pass); gw("/" as *u8); gn(ttl); gw("\n" as *u8) 76 if pass == ttl { gw("verdict=GREEN (sovereign MP4 muxer emits valid faststart ISOBMFF; our own demuxer round-trips it)\n" as *u8); sys_exit(0); return 0 } 77 gw("verdict=RED\n" as *u8); sys_exit(1); return 1 78}