# lane mp4mux: ec-mp4 writes every sample entry edith's MP4 export needs Goal: DONE.md wave 2, the `mp4` crate to ec-mp4 swap. ec-mp4's muxer must write the sample entries edith's MP4 export uses, verified against ffprobe, ffmpeg's decode and ec-mp4's own demuxer. Base: `main` at d271a92b. Branch `lane/mp4mux`, worktree `~/.cache/wt/mp4mux`. ## What edith's MP4 export writes (read-only survey of edith) From `crates/engine/src/mux.rs` and the vendored `mp4` crate: - video: `avc1` + `avcC` (H.264); `av01` + `av1C` (`create_av1`, patched into an `avc1` placeholder); `hvc1` + `hvcC` (`create_hevc`, same patch). - audio: `mp4a` + `esds` (AAC) with `edts/elst` priming. - subtitles: `tx3g`, with `trak/udta/name`. - boxes: `colr` (nclx), `mdcv`, `clli`, `pasp`, `moov/udta/name`. - ftyp: `isom`, `iso2`, the codec's own brand, `mp41`. - Opus is refused for MP4 (`mux.rs` line 389: "this writer declares `mp4a` only"). edith has no AC-3, E-AC-3 or ALAC path into MP4 at all. So for edith's current export, the entries that matter are avc1, hvc1, av01, mp4a and tx3g. The brief's ac-3, ec-3, alac and Opus entries are covered at the ec-mp4 level (they are the codec-general entries ec-mp4 advertises), not because edith needs them today. ## What already existed in ec-mp4 `mux.rs` already writes `avc1`, `hvc1`, `av01`, `vp09`, `vp08`, `mp4a`, `ac-3`/`dac3`, `ec-3`/`dec3`, `Opus`/`dOps`, `alac`, `fLaC`, `sowt`, `twos`, `raw `, `tx3g`, `colr`, `mdcv`, `clli`, `pasp`, `edts/elst` priming, and the ftyp brands. Every entry was checked end to end rather than assumed. ## What changed 1. `crates/ec-mp4/src/mux.rs`, `track_ticks`: the track span is now the latest end of any sample in presentation or decode time (`max(dts+dur, cts+dur) - first`), not the last sample's decode end. A reordered (B-frame) stream's last picture is shown a reorder delay after its decode time, so the `elst` that starts the picture at its priming was 1 reorder delay too short. Symptom before the fix: an ffmpeg H.264 or HEVC source of 1.000 s remuxed to 0.920 s (`avc1`, `hvc1` cases failed). No-reorder tracks compute the same value as before. 2. `crates/ec-mp4/src/mux.rs`, `opus_pre_skip`: an Opus track's priming is now at least its `OpusHead` pre-skip (or `dOps` pre-skip), written as the `elst` `media_time`, per the Opus-in-ISOBMFF convention. Without it a caller that carries no edit list gets packets shifted by the pre-skip (first pts 0 instead of -312 at 48 kHz). Verified: the new test fails with this line disabled and passes with it. 3. `crates/ec-mp4/src/mux.rs`, ALAC: a bare magic cookie (no `alac` box header) is now wrapped in an `alac` box. Before, it fell through to the `dOps` box name, which is wrong for ALAC. 4. `crates/ec-mp4/tests/mux_entries.rs` (new): one test per entry, each muxing an ffmpeg-made source through ec-mp4 and checking (a) ffprobe stderr empty and the same codec, channels, rate and duration as the source; (b) ffmpeg decode MD5 of the stream equal to the source's; (c) ec-mp4's demuxer reads the same packet count, streams, stream index, pts, dts, duration, keyframe flag and bytes. Plus `opus_pre_skip_is_written_without_a_caller_asking` and `alac_bare_cookie_is_written_inside_an_alac_box`. Skips when ffmpeg, ffprobe or the needed encoder is missing. Each test works in its own directory. Not changed: `demux.rs`, `esds.rs` (the parallel `mp4demux` lane's files). `boxes.rs` unchanged. ## Verify Run through `scripts/remote-run.sh mp4mux` (hosts a and c were both used across runs). | command | result | |---|---| | `cargo clippy -p ec-mp4 --all-targets -- -D warnings` | exit 0, no warnings | | `cargo test -p ec-mp4 --release --no-fail-fast` | lib 19 pass; `mp4.rs` 16 pass, 1 ignored (`the_real_library_demuxes_to_eof`, pre-existing); `mux_entries.rs` 10 pass; `rotation.rs` 3 pass | | `cargo test -p ec-alac --release --test encode` (mp4 muxer dependent) | 8 pass | | `cargo test -p ec-aac --release --test sbr_real_library` (mp4 muxer dependent) | 16 pass | | `cargo check` in `crates/ec-mp4/fuzz` | builds | | rustfmt `--check` on `mux_entries.rs` | clean | Fuzz: `crates/ec-mp4/fuzz` has `demux_mp4`, `esds_parse` and `is_mp4`. None links the muxer, so there is no mux roundtrip target to run for 60 s. The fuzz crate builds, and nothing was run. Entry table (`mux_entries.rs`, all from ffmpeg-made sources): | entry | codec | ffprobe-clean | decode equal (MD5) | roundtrip (packets, pts, dts, keyframes, bytes) | |---|---|---|---|---| | `avc1` | H.264 | yes | yes | yes | | `hvc1` | HEVC | yes | yes | yes | | `av01` | AV1 | yes | yes | yes | | `mp4a` | AAC | yes | yes | yes | | `ac-3` | AC-3, 5.1 | yes | yes | yes | | `ec-3` | E-AC-3, 5.1 | yes | yes | yes | | `alac` | ALAC | yes | yes | yes; bare-cookie variant passes | | `Opus` | Opus | yes | yes | yes; no-caller-shift variant passes | Each entry passes all three checks. Not checked: edith's own export through the new ec-mp4 muxer (the swap is a later step), or the `tx3g`, `colr`/HDR and `udta` paths beyond the existing tests. ## Flake seen, not fixed here `mp4.rs::a_fragmented_file_demuxes_to_the_same_packets_as_a_plain_one` failed once in a full parallel run. Two tests both write the shared `derived_fixtures()` files at the same time (`mp4.rs` `work()`), so one reads a half-written file. It passes alone and on rerun. This is pre-existing and predates this lane. A fix would give each test its own work directory, as `mux_entries.rs` now does. ## Left - The demux side of the `dOps`/`elst` pre-skip path is in `demux.rs`, which this lane does not touch. The round trip relies on it as it stands. - `vp09`/`vp08` have no ec-mp4 test here beyond the existing ones. - The edith side of the swap (switching `crates/engine` from `mp4` to ec-mp4) is out of scope for this lane.