FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
M4.9: performance docs, h264 README and roadmap for 4K H.264 playback
Brandon Thomas7e991ea95b6f
diff --git a/ROADMAP.md b/ROADMAP.mdindex 6f4d58113..3fcac3d52 100644--- a/ROADMAP.md+++ b/ROADMAP.md@@ -22,7 +22,7 @@ matched against `filmcraft-cli commands` and the UI command table. Effects: the| Graphics and captions | 8% | text engine, shapes, captions, transcripts; no MOGRT, rolls/crawls, responsive design | ~60% || Export | 8% | own H.264/AAC, ProRes, DNxHR, image sequences, GIF, WAV; preset library + user presets, full Export-mode settings (loudness normalization, overlays, captions), export queue, Quick Export; no AAF/OMF, MXF export | ~80% || Preferences and project management | 5% | Settings dialog with 16 categories (most settings wired), project settings, scratch disks, search bins, templates | ~80% |-| Performance | 5% | 1080p real-time, 3×1080p; HEVC 2.5× cheaper, catch-up decoding halves 4K CPU/frame, `cargo xtask bench` + `perf.stats`; 4K/8K and AV1 not yet real-time on a loaded machine | ~55% |+| Performance | 5% | 1080p real-time, 3×1080p; 4K H.264 real-time at load ≤140 (−23% decoder cycles, opt-in draft decoding at 1/2–1/4); HEVC 2.5× cheaper, catch-up decoding, `cargo xtask bench` + `perf.stats`; 8K and AV1 not yet real-time on a loaded machine | ~60% || **Weighted total** | | | **~77%** |## Estimate to parity@@ -77,6 +77,8 @@ Status: ✅ done · 🟡 in progress · ⬜ not started. Estimates are remaining## Log+- **2026-10-03:** 4K H.264 playback (M4.9): vectorised deblocking, branch-free CABAC bins and column-parallel inverse transforms cut H.264 decoding by 23 % (cycles per frame, bit-exact; 4K cold seeks −27 % CPU); opt-in draft decoding (Settings ▸ Playback) skips deblocking of non-reference pictures and uploads decimated planes while playing at 1/2–1/4, never for paused frames or exports; conformance runs 1, 3 and all threads. 4K plays 192/0 at load 100–140 (before: only at load ~90).+- **2026-10-02 (late):** export parity (M6.5): built-in preset library + user presets, full Export-mode settings (loudness normalization, image/name/timecode overlays, captions, range), export queue, Quick Export, Premiere-style image-sequence export, `filmcraft-cli export --preset`; media import (M9.14): MXF OP1a/OP-Atom (new `crates/mxf`, H.264/DNx/ProRes/PCM, timecode), Ogg Opus (new `crates/ogg`), image sequences as clips, BWF timecode. 1373 tests.- **2026-10-02 (panels):** complete Lumetri Scopes (new `filmcraft-scopes` crate: Vectorscope YUV/HLS, Histogram, Parade RGB/YUV/RGB-White, Waveform RGB/Luma/YC/YC no Chroma; wrench menu with presets, colour space, brightness, scale; < 1 ms per scope at 1080p; `scopes.read` returns the numbers); Metadata panel (editable log fields, undoable `metadata.set`), Timecode panel, Events panel (event log of failed commands, jobs, auto-save), Progress panel (jobs with Cancel), Reference Monitor (parked or ganged, picture or scopes).diff --git a/crates/h264/README.md b/crates/h264/README.mdindex ec9897282..21e7a10f1 100644--- a/crates/h264/README.md+++ b/crates/h264/README.md@@ -34,6 +34,8 @@ oracles and fixture generators.- Output: cropped 8-bit planar 4:2:0 pictures in output order with the caller's `pts`, POC, keyflag, VUI colour description (range, primaries, transfer, matrix) and sample aspect ratio.- Frame-level multithreading (see below).+- Draft mode for reduced-resolution playback (off by default): non-reference pictures skip the+ deblocking filter and are flagged `Picture::draft`; every other picture stays bit-exact.## API@@ -41,6 +43,7 @@ oracles and fixture generators.use filmcraft_h264::Decoder;let mut dec = Decoder::new(); // or Decoder::with_threads(n), Decoder::from_avcc(&avcc)?+// dec.threads(): worker threads in use; dec.set_draft(true): draft mode (playback only)for (pts, access_unit) in access_units {for pic in dec.decode(access_unit, pts)? {// pic.width / pic.height (cropped), pic.y / pic.u / pic.v, pic.y_stride / pic.uv_stride,@@ -67,6 +70,29 @@ bit-exact. Without the `threads` feature (or on wasm, or with `with_threads(1)`)With threads, `decode` returns pictures once their job has finished, so output lags input by up toroughly the number of threads.+Deblocking is row-parallel in this pipelined form: each picture's job filters macroblock row r as+soon as row r + 1 is reconstructed (intra prediction of row r + 1 reads the unfiltered bottom+line of row r) and publishes it one row later, while the jobs of other pictures run on other+threads. A picture waiting on a reference therefore waits only for that reference's rows its+motion vectors reach plus about two rows, so a chain of reference pictures (a seek from an IDR+picture) advances a few rows behind its predecessor instead of a whole picture behind it. A+separate deblocking thread per picture would shorten that lag by about one row and would need its+own copy of the reconstructed rows and macroblock state; it is not worth it while the pool has+more pictures in flight than cores. The hot loops are written to vectorise without `unsafe` or+intrinsics: the edge filters process all 16 (luma) or 8 (chroma) lines of an edge at once with+per-lane masks, the inverse transforms do their column pass across all columns, and the CABAC+engine decodes a context-coded bin without branching on its outcome.++### Draft mode++`Decoder::set_draft(true)` (reduced-resolution playback only; FilmCraft enables it with Settings ▸+Playback ▸ Draft decoding while the Program monitor plays at 1/2 or 1/4 resolution) skips the+deblocking filter (8.7) of non-reference pictures (`nal_ref_idc` 0). No picture is predicted from+a non-reference picture, and deblocking never changes the motion data that direct prediction+reads, so every other picture is unchanged; the skipped pictures are flagged `Picture::draft` and+the media stack never hands them to a paused monitor, a render or an export. On the 4K fixture+below it saves ~9 % of the decoding work (the B-pyramid's non-reference half of the B pictures).+## Tests`cargo test -p filmcraft-h264` (fixtures are generated on first use into `target/fixtures/h264/`@@ -82,10 +108,16 @@ with ffmpeg + libx264; tests print a message and skip when `/opt/homebrew/bin/ffexplicit bi-prediction. The expected output is known exactly and is also checked against ffmpeg.- Robustness: avcC input with pts round trip; randomly corrupted, truncated and garbage input neverpanics or deadlocks (single- and multi-threaded).-- Conformance (`tests/conformance.rs`): every fixture is decoded single-threaded and frame-threaded- and compared byte-for-byte with `ffmpeg -f rawvideo -pix_fmt yuv420p`; on mismatch the first- differing frame, plane, sample position and macroblock are reported. Output pts/POC order is- checked too.+- Conformance (`tests/conformance.rs`): every fixture is decoded single-threaded, with 3 threads+ and with every core, and compared byte-for-byte with `ffmpeg -f rawvideo -pix_fmt yuv420p`; on+ mismatch the first differing frame, plane, sample position and macroblock are reported. Output+ pts/POC order is checked too, and that no picture is flagged draft without draft mode. Draft+ mode (`draft_mode_changes_only_flagged_non_reference_pictures`): on five B-pyramid / B-frame+ fixtures, single- and frame-threaded, every unflagged picture is bit-exact and only flagged+ pictures differ.+- Vectorised kernels against their straightforward forms on random input: the deblocking edge+ filter against the per-line filter, the 4x4 / 8x8 inverse transforms against the per-column+ formulation.### Fixture matrix (all bit-exact, single- and multi-threaded)@@ -146,9 +178,26 @@ Pro (14 cores) while the machine was heavily shared (load average 40-180), so th| 1 | ~105-122 (≈ 33 Mcycles per frame) || 14 | ~500-600 |+4K High profile (testsrc2 + grain, 3840x2160, CABAC, B-pyramid, ~170 Mbit/s; the first 48 frames+of the playback benchmark's `a2160.mp4`), CPU cycles per frame counted by the kernel+(`proc_pid_rusage`; independent of machine load, all threads summed; bit-exact with ffmpeg):++| build | Mcycles / frame | instructions / frame |+|---|---|---|+| before M4.9 | 505 | 1232 M |+| M4.9 | **390** (−23 %) | 1195 M |+| M4.9, draft mode | **356** (−30 %) | 1068 M |++The same on the benchmark's 1080p fixture (`dec_h264_1080.mp4`, 120 frames): 132 → 100 Mcycles+per frame (−24 %), 90 in draft mode. Single-threaded 4K time (`sample` profile) splits into CABAC+residual decoding 41 %, deblocking 16 % (25-30 % before), other macroblock-layer parsing 11 %,+inverse transforms and dequantisation 9 %, motion compensation 8 %, intra prediction 6 %, row and+picture copies 4 %. At ~4.5 GHz a 4K frame takes ~85 ms of one+core, so 23.976 fps needs about two cores' worth of frame threads.+The example `cargo run --release -p filmcraft-h264 --example h264dec -- in.h264 out.yuv` decodes a-file; `H264_BENCH_ITERS=n` (and `H264_THREADS=t`) turns it into a benchmark, `H264_STATS=1` prints-the coverage counters.+file; `H264_BENCH_ITERS=n` (and `H264_THREADS=t`, `H264_DRAFT=1`) turns it into a benchmark,+`H264_STATS=1` prints the coverage counters.## Known gaps@@ -159,7 +208,8 @@ the coverage counters.- FMO (slice groups), SP/SI slices, data partitioning (Extended profile), MVC/SVC NAL units.- Error concealment is minimal: undecodable slices leave the prediction/previous content; missingreferences are replaced by other references.-- Single-threaded speed has no SIMD-specific code yet (the hot loops are written to auto-vectorise).+- No SIMD intrinsics (the hot loops are written to auto-vectorise); CABAC residual decoding is+ inherently serial and now dominates.### Extending to High 10 and 4:2:2diff --git a/docs/performance.md b/docs/performance.mdindex 3e2063eeb..6a4fc4659 100644--- a/docs/performance.md+++ b/docs/performance.md@@ -9,11 +9,104 @@ and `.md` (options and sections: [testing.md](testing.md) §5). Agents read live**75–200** during every run below. Wall-clock columns (fps, shown/dropped frames, seek latency)swing by 2–5× between back-to-back runs at that load and are only comparable within one run. CPUtime per frame, samples decoded and skipped, and peak RSS are much less sensitive, and are what-the before/after comparison relies on. Baseline = commit `457aca1` (the harness on unchanged-code); after = M4.8. Runs alternated base / after (2 rounds each, 2026-10-02).+the before/after comparison relies on. Each milestone below alternated base / after runs (2 rounds+each); the most load-independent figure is the decoder's own cycle count (`proc_pid_rusage`+instructions and cycles of a process, all threads summed).++## Results (M4.9: 4K H.264, before → after)++Baseline = commit `3d14099` (M3.12, unchanged decoder); after = M4.9. Interleaved runs on+2026-10-02/03, load average 92–178 (the playback rows say which load each run saw).++### Decoder work per frame (load-independent)++`h264dec` example, one decoder, CPU cycles and instructions counted by the kernel (all threads),+output bit-exact with ffmpeg:++| stream | Mcycles / frame before | after | after, draft mode |+|---|---|---|---|+| 2160p (`a2160.mp4`, 170 Mbit/s, first 48 frames) | 505 | **390** (−23 %) | **356** (−30 %) |+| 1080p (`dec_h264_1080.mp4`, 120 frames) | 132 | **100** (−24 %) | **90** (−32 %) |++The cycle count is the same with 1, 4 or 14 frame threads (no spinning or contention overhead):+at ~4.5 GHz a 4K frame is ~85 ms of one core, so 23.976 fps needs about two cores.++### Decode (every frame through the media stack)++| codec | size | CPU ms/frame before (2 rounds) | after (2 rounds) |+|---|---|---|---|+| H.264 | 1080p | 35.8, 39.5 | **33.0, 29.4** |+| H.264 | 2160p | 138.9, 152.1 | **125.7, 112.3** |++(A run at load ~190 before any change gave 41.6 / 160.2 ms; an intermediate build 32.2 / 118.3.)++### Cold seeks on the 4K fixture (12 random targets, GOP 250, fresh source per seek)++| mode | CPU ms per seek before | after | wall p50 ms before → after |+|---|---|---|---|+| full decode from the keyframe | 7529, 7418 | **5531, 5268** (−27 %) | 2757, 1848 → 924, 739 |+| keep 2 s (scrub) | 7488, 7373 | **5442, 5397** | 2408, 1995 → 872, 795 |+| late (playback catch-up) | 5597, 5487 | **4079, 3949** | 2014, 1248 → 735, 526 |++Samples decoded per seek are identical (~48, ~34 in catch-up): the same pictures, decoded with+less work. The cold seek is a chain of reference pictures, so it shows the per-picture cost+directly.++### Program-monitor playback, 2160p (8 s, GPU path)++| case | before: shown/dropped (load) | after: shown/dropped (load) | CPU ms/frame before → after |+|---|---|---|---|+| Full | 192/0 (92), 9/183 (167) | 113/79 (171), **192/0** (139) | 159 (no skips), 133 (17 skipped) → 131, 130 |+| 1/2 | 12/180 (95), 5/187 (169) | 173/19 (172), **192/0** (124) | 143, 127 → 133, 132 |+| 1/2 draft | – | 183/9 (175), **192/0** (119) | – → 144, 134 |+| 1/4 | – | 186/6 (177), **192/0** (111) | – → 134, 125 |+| 1/4 draft | – | **192/0** (178), 190/2 (101) | – → 132, 131 |+| 1080p Full | 192/0, 192/0 | 192/0, 192/0 | 41, 48 → 39, 36 |++Before, the 4K clip kept up in one run (Full at load 92; 1/2 dropped 94 % at load 95) and dropped+95–97 % at load ~168. After, every 4K case plays 192/0 at load 100–140; at load 171–178 Full drops+41 %, 1/2 10 % (5 % with draft decoding), 1/4 3 % (none with draft decoding). CPU per+displayed frame (whole process: decode, frame workers, compositing) drops ~15 % where no frames+were skipped; draft decoding at 1/2 and 1/4 is within run-to-run noise in this column (its saving+is the ~9 % of decoder cycles above, plus a quarter / sixteenth of the texture upload), while the+frame jobs wait for the decoder less (decode ms/job 212 → 67 at 1/2, 98 → 44 at 1/4 in the busy+round). On an idle machine the 4K clip needs about two cores.++## What M4.9 changed++1. **Deblocking** (25–30 % of 4K decode time before): the edge filter runs on all 16 luma / 8+ chroma lines of an edge at once with per-lane masks and min/max clipping (no `clamp`, whose+ bounds assert blocked vectorisation), so it compiles to NEON; edges whose alpha or beta index+ is 0 are skipped. Bit-exact with the per-line filter (random-edge unit test) and ffmpeg.+2. **CABAC**: `decode_decision` / `decode_bypass` select instead of branching on the bin value+ (the MPS/LPS outcome is close to random at high bit rates), renormalisation has no branch on+ the range, and context / state indexing has no bounds checks.+3. **Inverse transforms**: the 4x4 / 8x8 column pass and the add/clip run across all columns.+4. **Frame threading** was already in place (M2): pictures decode concurrently and wait per+ macroblock row for the reference rows their motion vectors reach, with deblocking pipelined row+ by row inside each picture's job. The conformance suite now decodes every fixture with 1, 3 and+ all threads (bit-exact each time). A separate deblocking thread was not added: a picture's rows+ are published about two rows after they are reconstructed, so a reference chain advances a few+ rows behind its predecessor and the pool already has more pictures in flight than cores+ (crates/h264/README.md, "Threading model").+5. **Draft decoding** (Settings ▸ Playback ▸ Draft decoding, off by default; `perf.stats`+ `playback.draftDecode`, `decode.draftFrames`, `decode.h264Threads`): while the Program monitor+ plays at 1/2 or 1/4, H.264 non-reference pictures skip deblocking (no other picture can change),+ and draft plans hand the GPU box-decimated Y'CbCr planes at the drawn size. Draft frames carry+ their own frame-cache keys and the GOP cache serves them to draft requests only, so pausing,+ rendering and exporting always decode exact pictures (tested through the media stack against+ ffmpeg after draft playback). The CPU renderer already converted reduced-resolution frames+ straight to the decimated size (`to_linear_f32_decimated`).++Not done: CABAC residual decoding (now 41 % of the work, ~19 cycles per bin) is inherently serial;+the remaining deblocking cost is mostly the vertical-edge transposes; motion compensation and the+per-row / per-picture copies (4 %) are unchanged.## Results (M4.8, before → after)+Baseline = commit `457aca1` (the harness on unchanged code); after = M4.8. Runs alternated base /+after (2 rounds each, 2026-10-02).+### Decode (every frame through the media stack: container, GOP cache, decoder, conversion)CPU ms per frame (both rounds; lower is better). fps is the best of the run at that moment's load.diff --git a/docs/testing.md b/docs/testing.mdindex 710684a1e..09f3a6f36 100644--- a/docs/testing.md+++ b/docs/testing.md@@ -231,7 +231,10 @@ wgpu render ms per frame), `export` (H.264 + AAC and ProRes) and `project` (saveOptions: `--sections decode,scrub`, `--only <substring>` (fixture or scenario), `--repeat N`,`--quick`, `--cpu`, `--label NAME` (output `target/bench/bench-<label>.{json,md}`),`--section NAME --json FILE` (one section in-process, e.g. under a profiler). Fixtures are made-with ffmpeg in `target/fixtures/playback/` (or `$FILMCRAFT_FIXTURES/playback`).+with ffmpeg in `target/fixtures/playback/` (or `$FILMCRAFT_FIXTURES/playback`). The suite's+`playback` section plays `h264-1080` and `stack3` at Full, `h264-2160` at Full, 1/2 and 1/4 and with+draft decoding (Settings ▸ Playback ▸ Draft decoding) at 1/2 and 1/4, and `hevc-2160` at Full and+1/2; its `draft` column counts the frames decoded in draft mode.The perf tests check bit-exactness before they time anything. Results go in the crate README'sperformance table, with machine and thread count. Headline numbers go in