code wiki / (root) / nx_uxf_remux.nx

nx_uxf_remux.nx source

↩ module page · 136 lines · 7180 B

1// nx_uxf_remux.nx -- FM4: the frame-level LOSSLESS REWRAP, extracted from nx_uxf_remux_test so that it has a 2// CALLABLE ENTRY POINT (2026-09-06). 3// 4// WHY THIS EXISTS. The remux data-flow was proven byte-identical in June 2026 and the proof lived INSIDE a 5// test with no function anything could call, so the capability was real and uncomposable at the same time. 6// The formats board ranked that #1 for exactly that reason. This is the extraction; nx_uxf_remux_gate drives 7// it and asserts the same five properties the original test asserted. 8// 9// WHAT IT DOES. Parses one EBML element carrying a frame (the MKV input side) and REWRAPS those exact bytes 10// into a minimal MP4 of ftyp + mdat + stsz (the MP4 output side). The frame is COPIED, never re-encoded -- 11// that is the whole point, and it is what makes the endpoint-negotiation REMUX plan honest. 12// 13// HONEST SCOPE, UNCHANGED FROM THE ORIGINAL: one frame, flat structure. A spec-complete PLAYABLE MKV to MP4 14// needs a real SimpleBlock header decode, multi-frame and multi-track sample tables stts stsc stco, codec 15// private data in stsd, timestamps and proper moov nesting. That is FM5 and it is not claimed here. 16// Defensive at the boundary (Rule 12): a malformed vint, a frame running past the input, or an output that 17// would not fit are each REFUSED BY NAME rather than truncated. 18// PROVENANCE 2026-09-06: extracted from nx_uxf_remux_test to close formats rung FM4, which ranked first on 19// that board precisely because a capability with no callable entry point cannot be composed. This comment is 20// also the probe for a build-cache question: a comment-only edit compiles byte-identical, so if the cache key 21// of a DEPENDENT gate changes when this file changes, the key covers the import closure. 22// license_tier: ORIGINAL No hw writes (Rule 26). 23import "nx_syscalls.nx" 24 25const UXR_OK: i64 = 0 26const UXR_ERR_VINT: i64 = 0-1 // leading byte is not a legal EBML vint 27const UXR_ERR_OVERRUN: i64 = 0-2 // the declared frame runs past the input buffer 28const UXR_ERR_CAPACITY: i64 = 0-3 // the rewrapped MP4 would not fit in outcap 29 30const UXR_META_ID: i64 = 0 31const UXR_META_FRAMESIZE: i64 = 1 32const UXR_META_MDAT_OFF: i64 = 2 33const UXR_META_STSZ_OFF: i64 = 3 34const UXR_META_SLOTS: i64 = 4 35 36// a minimal rewrap is ftyp(24) + mdat(8+frame) + stsz(20); this is the fixed overhead around the frame 37const UXR_FIXED_OVERHEAD: i64 = 52 38const UXR_STSZ_SAMPLESIZE_OFF: i64 = 12 // from the stsz box start: 8 header + 4 version-and-flags 39 40// --- EBML input primitives --- 41func uxr_vint_len(b: i64) -> i64 { 42 if b >= 0x80 { return 1 } 43 if b >= 0x40 { return 2 } 44 if b >= 0x20 { return 3 } 45 if b >= 0x10 { return 4 } 46 if b >= 0x08 { return 5 } 47 if b >= 0x04 { return 6 } 48 if b >= 0x02 { return 7 } 49 if b >= 0x01 { return 8 } 50 return 0 51} 52func uxr_read_id(buf: *u8, pos: *i64) -> i64 { 53 let p: i64 = pos[0] 54 let len: i64 = uxr_vint_len(buf[p] as i64) 55 if len == 0 { return UXR_ERR_VINT } 56 var v: i64 = 0 57 var i: i64 = 0 58 while i < len { v = (v * 256) + (buf[p + i] as i64); i = i + 1 } 59 pos[0] = p + len 60 return v 61} 62func uxr_read_size(buf: *u8, pos: *i64) -> i64 { 63 let p: i64 = pos[0] 64 let len: i64 = uxr_vint_len(buf[p] as i64) 65 if len == 0 { return UXR_ERR_VINT } 66 var v: i64 = (buf[p] as i64) & (255 >> len) 67 var i: i64 = 1 68 while i < len { v = (v * 256) + (buf[p + i] as i64); i = i + 1 } 69 pos[0] = p + len 70 return v 71} 72 73// --- MP4 output primitives --- 74func uxr_w32(out: *u8, off: i64, v: i64) -> i64 { out[off] = ((v >> 24) & 0xff) as u8; out[off + 1] = ((v >> 16) & 0xff) as u8; out[off + 2] = ((v >> 8) & 0xff) as u8; out[off + 3] = (v & 0xff) as u8; return off + 4 } 75func uxr_r32(buf: *u8, off: i64) -> i64 { return ((buf[off] as i64) * 16777216) + ((buf[off + 1] as i64) * 65536) + ((buf[off + 2] as i64) * 256) + (buf[off + 3] as i64) } 76func uxr_4cc(out: *u8, off: i64, a: i64, b: i64, c: i64, d: i64) -> i64 { out[off] = a as u8; out[off + 1] = b as u8; out[off + 2] = c as u8; out[off + 3] = d as u8; return off + 4 } 77func uxr_box_begin(out: *u8, off: i64, a: i64, b: i64, c: i64, d: i64) -> i64 { uxr_w32(out, off, 0); uxr_4cc(out, off + 4, a, b, c, d); return off + 8 } 78func uxr_box_end(out: *u8, start: i64, cur: i64) -> i64 { uxr_w32(out, start, cur - start); return cur } 79func uxr_copy(out: *u8, off: i64, src: *u8, srcoff: i64, n: i64) -> i64 { var i: i64 = 0; while i < n { out[off + i] = src[srcoff + i]; i = i + 1 } return off + n } 80 81// walk the emitted boxes: every box size must be at least a header and the sizes must tile the buffer exactly 82func uxr_walk_ok(buf: *u8, n: i64) -> i64 { var off: i64 = 0; while (off + 8) <= n { let sz: i64 = uxr_r32(buf, off); if sz < 8 { return 0 } off = off + sz } if off == n { return 1 } return 0 } 83func uxr_bytes_eq(a: *u8, aoff: i64, b: *u8, boff: i64, n: i64) -> i64 { var i: i64 = 0; while i < n { if a[aoff + i] != b[boff + i] { return 0 } i = i + 1 } return 1 } 84 85// THE ENTRY POINT. Parse one EBML frame element from inb and rewrap it into out as ftyp + mdat + stsz. 86// Returns the total bytes written, or a negative UXR_ERR_* code. meta receives id, frame size, the mdat 87// payload offset and the stsz box offset, so a caller can verify the carry without re-parsing. 88func uxf_remux_frame(inb: *u8, inlen: i64, out: *u8, outcap: i64, meta: *i64) -> i64 { 89 let pos: *i64 = sys_mmap(8) as *i64 90 pos[0] = 0 91 let id: i64 = uxr_read_id(inb, pos) 92 if id < 0 { return UXR_ERR_VINT } 93 let fsize: i64 = uxr_read_size(inb, pos) 94 if fsize < 0 { return UXR_ERR_VINT } 95 let fdata_off: i64 = pos[0] 96 if (fdata_off + fsize) > inlen { return UXR_ERR_OVERRUN } 97 if (fsize + UXR_FIXED_OVERHEAD) > outcap { return UXR_ERR_CAPACITY } 98 99 var off: i64 = 0 100 let ft: i64 = off 101 off = uxr_box_begin(out, off, 102, 116, 121, 112) // ftyp 102 off = uxr_4cc(out, off, 105, 115, 111, 109) // isom 103 off = uxr_w32(out, off, 512) 104 off = uxr_4cc(out, off, 105, 115, 111, 109) 105 off = uxr_box_end(out, ft, off) 106 107 let md: i64 = off 108 off = uxr_box_begin(out, off, 109, 100, 97, 116) // mdat 109 let mdat_data: i64 = off 110 off = uxr_copy(out, off, inb, fdata_off, fsize) // REWRAPPED, never re-encoded 111 off = uxr_box_end(out, md, off) 112 113 let sz: i64 = off 114 off = uxr_box_begin(out, off, 115, 116, 115, 122) // stsz 115 off = uxr_w32(out, off, 0) // version and flags 116 off = uxr_w32(out, off, fsize) // sample_size 117 off = uxr_w32(out, off, 1) // sample_count 118 off = uxr_box_end(out, sz, off) 119 120 meta[UXR_META_ID] = id 121 meta[UXR_META_FRAMESIZE] = fsize 122 meta[UXR_META_MDAT_OFF] = mdat_data 123 meta[UXR_META_STSZ_OFF] = sz 124 return off 125} 126 127// the source offset of the frame payload inside the input element, for a caller proving byte identity 128func uxf_remux_src_off(inb: *u8) -> i64 { 129 let pos: *i64 = sys_mmap(8) as *i64 130 pos[0] = 0 131 let id: i64 = uxr_read_id(inb, pos) 132 if id < 0 { return UXR_ERR_VINT } 133 let fsize: i64 = uxr_read_size(inb, pos) 134 if fsize < 0 { return UXR_ERR_VINT } 135 return pos[0] 136}