omega-mp4
omega-mp4: ovideo, an MP4 reader written in Omega (Apache-2.0)
Brought by drakestapleton
omega-mp4 / README.md
| 1 | # omega-mp4 |
| 2 | |
| 3 | `ovideo` reads MP4 and QuickTime (.mov) files, cuts them and joins them without re-encoding. It is written |
| 4 | only in seeded Omega and built by the pinned compiler (see `PIN`). It follows the public standards |
| 5 | ISO/IEC 14496-12 (ISO base media file format; clauses are cited in the source as "14496-12 n.n.n") and |
| 6 | the QuickTime file format, which uses the same box (atom) layouts for the boxes read here. Sample data |
| 7 | is copied byte for byte from `mdat`; only the container is rewritten. |
| 8 | |
| 9 | ``` |
| 10 | ovideo info FILE [DEPTH] movie and track fields, then the box tree (type, offset, size) to DEPTH (0..16, default 16) |
| 11 | ovideo dump FILE one line per sample: track, index, decode time, composition offset, |
| 12 | duration, size, file offset, sync flag (media ticks) |
| 13 | ovideo trim IN START END OUT the samples needed to present START..END, moov at the front |
| 14 | ovideo join OUT A B ... 2..16 inputs with matching tracks, one after the other |
| 15 | ``` |
| 16 | |
| 17 | A time is decimal seconds (`12.48`, at most 9 fraction digits) that must be a whole number of ticks |
| 18 | of the movie timescale (mvhd), or `@N` for exactly N movie ticks. Other forms exit 1. No floating |
| 19 | point is involved, so the cut is exact. |
| 20 | |
| 21 | `info` prints, per track, the handler type, the codec fourcc from the sample description, timescale, |
| 22 | duration, sample count, sync sample count, whether ctts is present, the chunk offset form (32 = stco, |
| 23 | 64 = co64) and every edit list entry; the movie timescale and duration; and `moovpos=front|end`. |
| 24 | |
| 25 | ## What trim writes |
| 26 | |
| 27 | For every track: from the nearest sync sample at or before START (video), or one packet before the packet |
| 28 | holding START (audio, so the decoder has its overlap), to the last sample presented before END, as one |
| 29 | contiguous run in decode order, so every reference a kept frame needs is kept. Fresh `stts`, `ctts` |
| 30 | (when the source has one), `stss` (when not every sample is a sync sample), `stsc`, `stsz` and `stco`, or |
| 31 | `co64` when an offset needs 64 bits; durations in `mvhd`, `tkhd` and `mdhd`; an edit list per track |
| 32 | (14496-12 8.6.6): an empty edit when the track starts after START, then one media edit whose media time |
| 33 | skips the samples before START that were kept only for decoding. Presentation begins exactly at START. |
| 34 | A track's own edit list (an empty edit plus one media edit at rate 1) is honoured. |
| 35 | |
| 36 | A player that ignores edit lists shows every kept sample from its decode time 0: video starts at the sync |
| 37 | sample (up to one GOP before START, and with B-frames the composition offsets are not shifted back), audio |
| 38 | starts with the pre-roll packet, and an empty edit (a track that starts late) is lost, so that track is |
| 39 | shown early. |
| 40 | |
| 41 | `join` writes the inputs' samples one after the other, each part keeping its own presentation through |
| 42 | one media edit per part (plus an empty edit for a gap). ovideo reads such a file with `info` and `dump` |
| 43 | (`editform=complex`), but trim and join refuse it with 33. |
| 44 | |
| 45 | ## Build |
| 46 | |
| 47 | ``` |
| 48 | cat src/io.om src/box.om src/info.om src/plan.om src/write.om src/main.om | OMC > ovideo && chmod +x ovideo |
| 49 | ``` |
| 50 | |
| 51 | OMC is omega-mv `82e536e1` `seed/out/omc`, sha256 `6d1b1cf5...87f1f8`. |
| 52 | |
| 53 | ## Gate |
| 54 | |
| 55 | ``` |
| 56 | cat tests/gate.om tests/oracle.om | OMC > build/gate-bin && chmod +x build/gate-bin && build/gate-bin OMC |
| 57 | ``` |
| 58 | |
| 59 | It prints `OVIDEOGATE PASS cases=N mutants-killed=K/K robust-runs=R` and exits 0, prints FAIL and exits 1, |
| 60 | and prints NOT_RUN and exits 3 when the compiler's SHA-256 is not the pin. Oracles (test only, never |
| 61 | shipped): ffprobe and ffmpeg 6.1.1. The corpus is generated (`corpus/SOURCES.txt`, `tests/mkcases.om`, |
| 62 | which also builds the 64-bit cases and the refusal fixtures on every gate run). |
| 63 | |
| 64 | Before the cases the gate builds `tests/docs.om` with the same compiler and runs it: publication checks |
| 65 | of the repository's own text (rules in `tests/docrules.txt`: the licence file and its README section, |
| 66 | no local home paths, no notes left from another tool's gate, and the command's earlier name nowhere). It |
| 67 | prints `DOCS PASS ... mutants-killed=K/K` (its own mutants, `tests/docmutants.txt`), and any other |
| 68 | result fails the gate. |
| 69 | |
| 70 | - info: every field against `ffprobe -show_streams` and the oracle's own box reader (mvhd, tkhd, mdhd, |
| 71 | hdlr, elst, ctts). The box reader also refuses a version-0 `ctts` entry at or above 2^31: version 0 |
| 72 | offsets are unsigned (14496-12 8.6.1.3), so such a word is a negative offset written in the wrong |
| 73 | version, which ffmpeg decodes anyway (it reads every ctts as signed). corpus/negcts.mp4 has negative |
| 74 | composition offsets (ctts version 1), so trim, join and whole-range trim exercise the version-1 writer. |
| 75 | dump: every sample against `ffprobe -ignore_editlist 1 -show_packets` (position, size, |
| 76 | DTS, PTS, flags, SHA-256 of the data); a sample's duration is the next DTS minus this one, the last |
| 77 | one's the last stts delta, because ffprobe's packet duration is an estimate (MEASURED: 1 instead of 20 |
| 78 | on vonly.mov). ffprobe's DTS are taken back to the file's decode times (DT(0) = 0, 14496-12 8.6.1.2): |
| 79 | with negative composition offsets ffmpeg moves every DTS of the track down by the largest of them |
| 80 | (MEASURED on negcts.mp4: first video packet dts=-512, pts=0). |
| 81 | - trim and join: the output's packet list must equal the source packets the documented selection keeps, |
| 82 | in order, with equal data hash, size, flags and composition offset, decode times rebased; the output edit |
| 83 | lists and durations must be exactly the computed ones; `ffmpeg -xerror` decodes it with empty stderr; |
| 84 | the decoded video frames (framemd5) equal the source frames presented in START..END (for join, the |
| 85 | parts' frames in order); the decoded audio (s16le) equals the source samples from START. |
| 86 | - Difference class `pns-audio-not-compared`: AAC coded with perceptual noise substitution decodes from a |
| 87 | pseudo-random generator whose state runs through the stream, so a cut decodes about 1 LSB apart from the |
| 88 | same range of the whole file (MEASURED: 6358 of 123536 bytes; without PNS 0). For corpus/pns.mp4 packets |
| 89 | and video are compared, decoded audio is not. |
| 90 | - ffmpeg does not cut decoded AAC at the end of an edit, so audio is compared over the decoded length. |
| 91 | - Mutants (each must fail a named case): chunk offsets not rebased, sync table not rebuilt, edit media time |
| 92 | one sample off, ctts dropped, stsc samples per chunk wrong, a 64-bit chunk offset truncated to 32 bits, |
| 93 | negative composition offsets written in a version-0 ctts, the stderr prefix changed. |
| 94 | - Robustness: every corpus file cut at 64 points and changed at 256 single bytes, whole-range trim of each, |
| 95 | 10 s limit: exit 0 or a documented code, never a trap, signal or time-out, and no output left after a |
| 96 | refusal. The sparse file is copied with its holes. |
| 97 | |
| 98 | ## Exit codes |
| 99 | |
| 100 | | Code | Meaning | |
| 101 | |---|---| |
| 102 | | 0 | done | |
| 103 | | 1 | usage, or a time that is not a whole number of movie ticks | |
| 104 | | 2 | cannot open an input | |
| 105 | | 5 | the output exists or cannot be created (exclusive creation) | |
| 106 | | 20 | malformed box structure: a size below its header, past its parent or past the file; a version other than 0 or 1 | |
| 107 | | 22 | a required box is missing or repeated | |
| 108 | | 23 | malformed sample table: counts disagree, a count exceeds its box, a sample outside an mdat | |
| 109 | | 30 | fragmented file (mvex, moof, mfra, styp, sidx, ssix) | |
| 110 | | 31 | encrypted or protected track (encv, enca and other protected entries, pssh, senc) | |
| 111 | | 32 | trim or join with a track other than video or sound (timecode, text, metadata, hint) | |
| 112 | | 33 | unsupported structure: several sample descriptions, an external data reference, stz2, an edit entry other than an empty edit or a media edit at rate 1; in trim and join also an edit list other than one optional empty edit plus one media edit, or a negative rebased media time | |
| 113 | | 34 | a bound below is exceeded | |
| 114 | | 35 | time range: START not below END, or START not below the movie duration | |
| 115 | | 36 | a track has no sample presented between START and END | |
| 116 | | 40 | join: different numbers of tracks | |
| 117 | | 41 | join: handler types differ | |
| 118 | | 42 | join: sample descriptions are not byte-equal | |
| 119 | | 43 | join: media timescales differ, or a movie timescale does not convert exactly | |
| 120 | | 60 | I/O error reading an input, writing the output or writing standard output | |
| 121 | |
| 122 | Every refusal prints one line `ovideo: ...` on standard error. After a refusal the output file does not |
| 123 | exist (it is created only after every check, exclusively, and removed if a later write fails). |
| 124 | |
| 125 | ## Bounds (each refused with 34, never a trap) |
| 126 | |
| 127 | Samples held, all tracks and inputs together, 1048576. Tracks 8 per input, track slots 128. Inputs per |
| 128 | join 16. Edit list entries 64 per input track and 64 per output track. Chunks 1048576. Boxes 65536 per |
| 129 | walk, nesting depth 16. Sample description box 1 MiB. Output moov 16 MiB. Table entry counts 1048576. |
| 130 | Static arrays only, no heap. Every loop over file structure is bounded by a checked count or by the file |
| 131 | size; the input is read through a 1 MiB pread window and copied in 4 MiB pread/write blocks. |
| 132 | |
| 133 | ## Throughput |
| 134 | |
| 135 | MEASURED 2026-11-10, Linux AArch64 (the Spark), one core (taskset -c 10, pool-10-11), load about 14, warm |
| 136 | page cache: a 212307829-byte file (120 s, 1280x720 H.264 + AAC), whole-range trim including fsync, |
| 137 | median of 3: 357.1 ms, 566.9 MiB/s. Plain `cp` of the same bytes 96.1 ms (2106.6 MiB/s, no fsync); |
| 138 | `dd conv=fsync` 234.8 ms (862.4 MiB/s). Trim takes 1.52 times the fsynced copy. |
| 139 | |
| 140 | ## Not supported in this slice |
| 141 | |
| 142 | Fragmented files (moof), encryption, decoding or re-encoding of any codec, frame-exact cuts without an |
| 143 | edit list, subtitle, timecode and metadata tracks (refused, not dropped), HEIF and other image |
| 144 | collections, trim or join of files with several media edits per track, udta/meta/tref boxes (not copied; |
| 145 | track IDs are renumbered). The writer's co64 and largesize output paths need an output above 4 GiB and are |
| 146 | not exercised; input files above 4 GiB are (a sparse case). |
| 147 | |
| 148 | ## Licence |
| 149 | |
| 150 | Apache License 2.0; the full text is in `LICENSE`. |