From 936b28e4c48ce83af06657ea9059942306a235b2 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 30 Sep 2026 05:33:20 -0400 Subject: [PATCH] M0.1/M0.2: workspace skeleton, time and bitstream crates Co-Authored-By: Claude Opus 5.5 --- .gitignore | 4 + CLAUDE.md | 26 ++ Cargo.lock | 446 +++++++++++++++++++++++++++++++ Cargo.toml | 56 ++++ crates/bitstream/Cargo.toml | 14 + crates/bitstream/src/lib.rs | 431 ++++++++++++++++++++++++++++++ crates/color/Cargo.toml | 15 ++ crates/color/src/lib.rs | 1 + crates/geom/Cargo.toml | 15 ++ crates/geom/src/lib.rs | 1 + crates/time/Cargo.toml | 15 ++ crates/time/src/lib.rs | 518 ++++++++++++++++++++++++++++++++++++ rustfmt.toml | 2 + xtask/Cargo.toml | 9 + xtask/src/main.rs | 1 + 15 files changed, 1554 insertions(+) create mode 100644 CLAUDE.md create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 crates/bitstream/Cargo.toml create mode 100644 crates/bitstream/src/lib.rs create mode 100644 crates/color/Cargo.toml create mode 100644 crates/color/src/lib.rs create mode 100644 crates/geom/Cargo.toml create mode 100644 crates/geom/src/lib.rs create mode 100644 crates/time/Cargo.toml create mode 100644 crates/time/src/lib.rs create mode 100644 rustfmt.toml create mode 100644 xtask/Cargo.toml create mode 100644 xtask/src/main.rs diff --git a/.gitignore b/.gitignore index 19cdc639..5d401fb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ *~ plan/ +/target +/dist +/log +.DS_Store diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 00000000..6d0d5cfc --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,26 @@ +# FilmCraft — instructions for agents + +FilmCraft is a clean-room, open-source, pure-Rust non-linear video editor targeting Adobe Premiere Pro parity (and beyond). Native on macOS, Windows, Linux; web via WASM. Sibling of `../photocraft` (Photoshop), `../printcraft` (Acrobat) and `../drawcraft` (Illustrator), with the same conventions. + +## Start every session here +1. Read `plan/STATUS.md` (current milestone, next unchecked task, blockers). +2. Read the task in `plan/execution-plan.md` §3, the relevant section of `plan/architecture.md`, and the README/docs of the crate you touch. Visual/behaviour reference: `plan/premiere/`. +3. Follow the autonomous operation protocol (`plan/execution-plan.md` §7). Don't stop to ask unless §7 lists the decision as the user's. + +`plan/` is gitignored (local-only). + +## Non-negotiables +- **Clean-room.** Never read/disassemble anything inside Adobe app bundles (names/listings only). Never copy Adobe icons, presets, LUTs, fonts. Never copy GPL/LGPL/AGPL code (FFmpeg, x264, x265, MLT, Kdenlive, Shotcut, Olive, LAME…). ffmpeg/ffprobe run only as external test oracles / fixture generators — never linked or shipped. +- **Pure Rust** in the product. OS media APIs only via Rust bindings in `crates/platform`, optional, behind traits. +- **Layering** (`plan/architecture.md` §3, enforced by `cargo xtask layers`): nothing below L5 depends on egui/eframe/winit/rfd/cpal. L0 codec/container crates depend only on `filmcraft-bitstream`. +- **Exact time:** all time is `filmcraft_time::Tick` (254 016 000 000/s). Never use f64 seconds for edit math. +- **Everything is a command** (`crates/engine`): id, label, menu path, shortcut, params, enabled(), run(). UI, CLI, control channel and MCP all dispatch by id. +- **Everything is agent-drivable:** every interactive widget registers an automation id; UI state is serde so the control channel can read/set it. +- **Quality gates** before every commit: `cargo fmt --check`, `cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace`, `cargo xtask layers`, `cargo xtask wasm` (L0–L5 changes). +- **Commits:** one task id per commit (`M2.4: CABAC residual decoding`). Only green states. End messages with the attribution line required by the environment. + +## Running and looking at the app +- `cargo run -p filmcraft -- --control 9876` opens the desktop app with the JSON-lines control server (see `docs/control-protocol.md`). +- For UI work, **look at the result**: drive via the control channel and take `ui.screenshot`, compare with `plan/premiere/screenshots/`. +- Parallel agents: separate git worktrees and `CARGO_TARGET_DIR=target/agent-`; keep every `Cargo.toml` valid at all times (the `crates/*` glob means one broken manifest breaks everyone). +- Test fixtures: generate with ffmpeg into `target/fixtures/` (never commit media). diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 00000000..4f3edd48 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,446 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "bit-set" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" +dependencies = [ + "bit-vec", +] + +[[package]] +name = "bit-vec" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" + +[[package]] +name = "filmcraft-bitstream" +version = "0.1.0" +dependencies = [ + "proptest", +] + +[[package]] +name = "filmcraft-color" +version = "0.1.0" +dependencies = [ + "proptest", + "serde", +] + +[[package]] +name = "filmcraft-geom" +version = "0.1.0" +dependencies = [ + "proptest", + "serde", +] + +[[package]] +name = "filmcraft-time" +version = "0.1.0" +dependencies = [ + "proptest", + "serde", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "libc" +version = "0.2.189" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "proptest" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b45fcc2344c680f5025fe57779faef368840d0bd1f42f216291f0dc4ace4744" +dependencies = [ + "bit-set", + "bit-vec", + "bitflags", + "num-traits", + "rand", + "rand_chacha", + "rand_xorshift", + "regex-syntax", + "rusty-fork", + "tempfile", + "unarray", +] + +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_xorshift" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "513962919efc330f829edb2535844d1b912b0fbe2ca165d613e4e8788bb05a5a" +dependencies = [ + "rand_core", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "rustix" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "rusty-fork" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6bf79ff24e648f6da1f8d1f011e9cac26491b619e6b9280f2b47f1774e6ee2" +dependencies = [ + "fnv", + "quick-error", + "tempfile", + "wait-timeout", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "serde_json" +version = "1.0.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8593e8e72159ed2257d083c7a454a85cbf854f37a0966d8d483aff8c8a3ebcee" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix", + "windows-sys", +] + +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + +[[package]] +name = "unicode-ident" +version = "1.0.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954" + +[[package]] +name = "wait-timeout" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11" +dependencies = [ + "libc", +] + +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "xtask" +version = "0.1.0" +dependencies = [ + "serde_json", +] + +[[package]] +name = "zerocopy" +version = "0.8.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6df92bf3d9227be3d53173901ddbffac2babc27ae50f397776ffd6dc33f800cb" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac4f328cf2f05d084e496c3e9c3f33ed0a183656a16e1fcec4d464d8373aec82" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..2c249a92 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,56 @@ +[workspace] +resolver = "3" +members = ["crates/*", "xtask"] +default-members = ["crates/*", "xtask"] + +[workspace.package] +version = "0.1.0" +edition = "2024" +license = "MIT OR Apache-2.0" +rust-version = "1.90" +repository = "https://github.com/storytold/filmcraft" + +[workspace.dependencies] +# internal +filmcraft-time = { path = "crates/time" } +filmcraft-geom = { path = "crates/geom" } +filmcraft-color = { path = "crates/color" } +filmcraft-bitstream = { path = "crates/bitstream" } +filmcraft-frame = { path = "crates/frame" } +filmcraft-media = { path = "crates/media" } +filmcraft-project = { path = "crates/project" } +filmcraft-edit = { path = "crates/edit" } +filmcraft-render = { path = "crates/render" } +filmcraft-engine = { path = "crates/engine" } +filmcraft-ui-egui = { path = "crates/ui-egui" } + +# foundation +thiserror = "2" +serde = { version = "1", features = ["derive", "rc"] } +serde_json = "1" +log = "0.4" +rayon = "1" +proptest = "1" + +# ui +egui = "0.36" +eframe = { version = "0.36", default-features = false } +egui_kittest = "0.36" +rfd = "0.17" + +[workspace.lints.rust] +unsafe_code = "deny" + +[workspace.lints.clippy] +dbg_macro = "warn" +todo = "warn" + +[profile.dev.package."*"] +opt-level = 2 + +[profile.dev] +opt-level = 1 + +[profile.release] +lto = "thin" +codegen-units = 1 diff --git a/crates/bitstream/Cargo.toml b/crates/bitstream/Cargo.toml new file mode 100644 index 00000000..8510e348 --- /dev/null +++ b/crates/bitstream/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "filmcraft-bitstream" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[dependencies] + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/bitstream/src/lib.rs b/crates/bitstream/src/lib.rs new file mode 100644 index 00000000..779970f1 --- /dev/null +++ b/crates/bitstream/src/lib.rs @@ -0,0 +1,431 @@ +//! Bit-level reading and writing shared by FilmCraft's codec crates. +//! +//! - [`BitReader`]: MSB-first reader with Exp-Golomb (`ue(v)`, `se(v)`), as used by H.264/HEVC/AV1 headers. +//! - [`BitWriter`]: the matching writer (for encoders and header rewriting). +//! - [`unescape_rbsp`] / [`escape_rbsp`]: remove/insert H.264/HEVC emulation-prevention bytes. +//! - [`annexb_nals`] / [`length_prefixed_nals`]: split Annex-B byte streams and AVCC/HVCC samples into NAL units. + +use std::fmt; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BitError { + /// Tried to read past the end of the buffer. + Eof, + /// An Exp-Golomb code longer than 32 leading zeros. + InvalidExpGolomb, +} + +impl fmt::Display for BitError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + BitError::Eof => f.write_str("unexpected end of bitstream"), + BitError::InvalidExpGolomb => f.write_str("invalid Exp-Golomb code"), + } + } +} +impl std::error::Error for BitError {} + +pub type Result = std::result::Result; + +/// MSB-first bit reader over a byte slice. +#[derive(Clone)] +pub struct BitReader<'a> { + data: &'a [u8], + /// Position in bits. + pos: usize, +} + +impl<'a> BitReader<'a> { + pub fn new(data: &'a [u8]) -> Self { + Self { data, pos: 0 } + } + pub fn data(&self) -> &'a [u8] { + self.data + } + /// Bit position from the start. + pub fn position(&self) -> usize { + self.pos + } + pub fn bits_left(&self) -> usize { + (self.data.len() * 8).saturating_sub(self.pos) + } + pub fn is_byte_aligned(&self) -> bool { + self.pos.is_multiple_of(8) + } + pub fn byte_align(&mut self) { + self.pos = self.pos.div_ceil(8) * 8; + } + /// Byte offset of the current position (rounded down). + pub fn byte_pos(&self) -> usize { + self.pos / 8 + } + pub fn seek_bits(&mut self, pos: usize) { + self.pos = pos; + } + + /// Peek up to 32 bits without consuming (zero-padded past the end). + #[inline] + pub fn peek(&self, n: u32) -> u32 { + debug_assert!(n <= 32); + if n == 0 { + return 0; + } + let byte = self.pos / 8; + let shift = self.pos % 8; + let mut v: u64 = 0; + for i in 0..5 { + v = (v << 8) | *self.data.get(byte + i).unwrap_or(&0) as u64; + } + // v holds 40 bits starting at `byte` + ((v << (24 + shift)) >> (64 - n)) as u32 + } + + #[inline] + pub fn read_bits(&mut self, n: u32) -> Result { + if n as usize > self.bits_left() { + return Err(BitError::Eof); + } + let v = self.peek(n); + self.pos += n as usize; + Ok(v) + } + + pub fn read_bits_u64(&mut self, n: u32) -> Result { + if n <= 32 { + return self.read_bits(n).map(u64::from); + } + let hi = self.read_bits(n - 32)? as u64; + let lo = self.read_bits(32)? as u64; + Ok((hi << 32) | lo) + } + + #[inline] + pub fn read_bit(&mut self) -> Result { + self.read_bits(1).map(|b| b == 1) + } + pub fn read_flag(&mut self) -> Result { + self.read_bit() + } + + pub fn skip(&mut self, n: usize) -> Result<()> { + if n > self.bits_left() { + return Err(BitError::Eof); + } + self.pos += n; + Ok(()) + } + + /// Unsigned Exp-Golomb `ue(v)`. + pub fn read_ue(&mut self) -> Result { + let mut zeros = 0u32; + while !self.read_bit()? { + zeros += 1; + if zeros > 32 { + return Err(BitError::InvalidExpGolomb); + } + } + if zeros == 0 { + return Ok(0); + } + let v = self.read_bits_u64(zeros)?; + let r = (1u64 << zeros) - 1 + v; + u32::try_from(r).map_err(|_| BitError::InvalidExpGolomb) + } + + /// Signed Exp-Golomb `se(v)`. + pub fn read_se(&mut self) -> Result { + let k = self.read_ue()? as i64; + Ok(if k & 1 == 1 { ((k + 1) / 2) as i32 } else { -((k / 2) as i32) }) + } + + /// Truncated Exp-Golomb `te(v)` with range `max`. + pub fn read_te(&mut self, max: u32) -> Result { + if max > 1 { self.read_ue() } else { Ok(!self.read_bit()? as u32) } + } + + /// H.264/HEVC `more_rbsp_data()`: true if there is data before the rbsp trailing bits. + pub fn more_rbsp_data(&self) -> bool { + let total = self.data.len() * 8; + if self.pos >= total { + return false; + } + // find last set bit (the rbsp_stop_one_bit) + let mut last = None; + for (i, &b) in self.data.iter().enumerate().rev() { + if b != 0 { + last = Some(i * 8 + 7 - b.trailing_zeros() as usize); + break; + } + } + match last { + Some(stop) => self.pos < stop, + None => false, + } + } +} + +/// MSB-first bit writer. +#[derive(Default, Clone)] +pub struct BitWriter { + buf: Vec, + acc: u64, + nbits: u32, +} + +impl BitWriter { + pub fn new() -> Self { + Self::default() + } + pub fn write_bits(&mut self, value: u32, n: u32) { + debug_assert!(n <= 32); + if n == 0 { + return; + } + let v = (value as u64) & ((1u64 << n) - 1); + self.acc = (self.acc << n) | v; + self.nbits += n; + while self.nbits >= 8 { + self.nbits -= 8; + self.buf.push((self.acc >> self.nbits) as u8); + } + self.acc &= (1u64 << self.nbits) - 1; + } + pub fn write_bit(&mut self, b: bool) { + self.write_bits(b as u32, 1); + } + pub fn write_ue(&mut self, v: u32) { + let x = v as u64 + 1; + let len = 64 - x.leading_zeros(); + let zeros = len - 1; + for _ in 0..zeros { + self.write_bit(false); + } + if len > 32 { + self.write_bits((x >> 32) as u32, len - 32); + self.write_bits(x as u32, 32); + } else { + self.write_bits(x as u32, len); + } + } + pub fn write_se(&mut self, v: i32) { + let k = if v > 0 { (v as u32) * 2 - 1 } else { (-(v as i64) as u32) * 2 }; + self.write_ue(k); + } + pub fn bit_len(&self) -> usize { + self.buf.len() * 8 + self.nbits as usize + } + pub fn is_byte_aligned(&self) -> bool { + self.nbits == 0 + } + /// Pad with zero bits to a byte boundary. + pub fn align_zero(&mut self) { + if self.nbits > 0 { + self.write_bits(0, 8 - self.nbits); + } + } + /// `rbsp_trailing_bits()`: a one bit then zeros to alignment. + pub fn rbsp_trailing(&mut self) { + self.write_bit(true); + self.align_zero(); + } + pub fn write_bytes(&mut self, bytes: &[u8]) { + if self.nbits == 0 { + self.buf.extend_from_slice(bytes); + } else { + for &b in bytes { + self.write_bits(b as u32, 8); + } + } + } + pub fn finish(mut self) -> Vec { + self.align_zero(); + self.buf + } + pub fn bytes(&self) -> &[u8] { + &self.buf + } +} + +/// Remove emulation-prevention bytes (`00 00 03` → `00 00`) from a NAL payload. +pub fn unescape_rbsp(data: &[u8]) -> Vec { + let mut out = Vec::with_capacity(data.len()); + let mut zeros = 0; + for &b in data { + if zeros >= 2 && b == 3 { + zeros = 0; + continue; + } + out.push(b); + zeros = if b == 0 { zeros + 1 } else { 0 }; + } + out +} + +/// Insert emulation-prevention bytes so no `00 00 0x` (x ≤ 3) sequence appears. +pub fn escape_rbsp(data: &[u8]) -> Vec { + let mut out = Vec::with_capacity(data.len() + data.len() / 64); + let mut zeros = 0; + for &b in data { + if zeros >= 2 && b <= 3 { + out.push(3); + zeros = 0; + } + out.push(b); + zeros = if b == 0 { zeros + 1 } else { 0 }; + } + out +} + +/// Split an Annex-B byte stream (start codes `00 00 01` / `00 00 00 01`) into NAL units (escaped payloads). +pub fn annexb_nals(data: &[u8]) -> Vec<&[u8]> { + let mut starts = Vec::new(); + let mut i = 0; + while i + 3 <= data.len() { + if data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1 { + starts.push(i + 3); + i += 3; + } else { + i += 1; + } + } + let mut out = Vec::with_capacity(starts.len()); + for (k, &s) in starts.iter().enumerate() { + let mut e = if k + 1 < starts.len() { starts[k + 1] - 3 } else { data.len() }; + // trim trailing zero bytes (part of a 4-byte start code or trailing_zero_8bits) + while e > s && data[e - 1] == 0 { + e -= 1; + } + if e > s { + out.push(&data[s..e]); + } + } + out +} + +/// Split a length-prefixed sample (AVCC/HVCC, `length_size` 1/2/4 bytes) into NAL units. +pub fn length_prefixed_nals(data: &[u8], length_size: usize) -> Result> { + let mut out = Vec::new(); + let mut i = 0; + while i < data.len() { + if i + length_size > data.len() { + return Err(BitError::Eof); + } + let mut len = 0usize; + for k in 0..length_size { + len = (len << 8) | data[i + k] as usize; + } + i += length_size; + if i + len > data.len() { + return Err(BitError::Eof); + } + out.push(&data[i..i + len]); + i += len; + } + Ok(out) +} + +/// CRC-32 (IEEE, reflected) — used by containers (e.g. Matroska CRC-32 elements, PNG). +pub fn crc32(data: &[u8]) -> u32 { + let mut crc = 0xFFFF_FFFFu32; + for &b in data { + crc ^= b as u32; + for _ in 0..8 { + crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 }; + } + } + !crc +} + +#[cfg(test)] +mod tests { + use super::*; + use proptest::prelude::*; + + #[test] + fn reads_bits() { + let d = [0b1010_1100, 0b0101_0011]; + let mut r = BitReader::new(&d); + assert_eq!(r.read_bits(1).unwrap(), 1); + assert_eq!(r.read_bits(3).unwrap(), 0b010); + assert_eq!(r.read_bits(8).unwrap(), 0b1100_0101); + assert_eq!(r.bits_left(), 4); + assert_eq!(r.read_bits(5), Err(BitError::Eof)); + } + + #[test] + fn exp_golomb_known() { + // 1 | 010 | 011 | 00100 | 00101 -> 0,1,2,3,4 + let mut w = BitWriter::new(); + for v in 0..5 { + w.write_ue(v); + } + let b = w.finish(); + let mut r = BitReader::new(&b); + for v in 0..5 { + assert_eq!(r.read_ue().unwrap(), v); + } + let mut w = BitWriter::new(); + for v in [0, 1, -1, 2, -2, 1000, -1000] { + w.write_se(v); + } + let b = w.finish(); + let mut r = BitReader::new(&b); + for v in [0, 1, -1, 2, -2, 1000, -1000] { + assert_eq!(r.read_se().unwrap(), v); + } + } + + #[test] + fn emulation_prevention() { + let raw = [0, 0, 1, 0, 0, 0, 5, 0, 0, 3]; + let esc = escape_rbsp(&raw); + assert_eq!(esc, vec![0, 0, 3, 1, 0, 0, 3, 0, 5, 0, 0, 3, 3]); + assert_eq!(unescape_rbsp(&esc), raw); + } + + #[test] + fn annexb_split() { + let s = [0, 0, 0, 1, 0x67, 1, 2, 0, 0, 1, 0x68, 3, 0, 0, 0, 1, 0x65, 9, 9]; + let n = annexb_nals(&s); + assert_eq!(n, vec![&[0x67, 1, 2][..], &[0x68, 3][..], &[0x65, 9, 9][..]]); + let l = [0, 0, 0, 2, 0x67, 1, 0, 0, 0, 1, 0x68]; + assert_eq!(length_prefixed_nals(&l, 4).unwrap(), vec![&[0x67, 1][..], &[0x68][..]]); + } + + #[test] + fn more_rbsp() { + let d = [0b1100_0000]; + let mut r = BitReader::new(&d); + assert!(r.more_rbsp_data()); + r.read_bit().unwrap(); + assert!(!r.more_rbsp_data()); + } + + #[test] + fn crc() { + assert_eq!(crc32(b"123456789"), 0xCBF4_3926); + } + + proptest! { + #[test] + fn roundtrip(vals in proptest::collection::vec((0u32..u32::MAX, 1u32..=32), 0..200), ues in proptest::collection::vec(0u32..u32::MAX - 1, 0..50)) { + let mut w = BitWriter::new(); + for &(v, n) in &vals { w.write_bits(v, n); } + for &u in &ues { w.write_ue(u); } + let b = w.finish(); + let mut r = BitReader::new(&b); + for &(v, n) in &vals { + let mask = if n == 32 { u32::MAX } else { (1 << n) - 1 }; + prop_assert_eq!(r.read_bits(n).unwrap(), v & mask); + } + for &u in &ues { prop_assert_eq!(r.read_ue().unwrap(), u); } + } + + #[test] + fn escape_roundtrip(data in proptest::collection::vec(0u8..4, 0..300)) { + let e = escape_rbsp(&data); + prop_assert_eq!(unescape_rbsp(&e), data); + for w in e.windows(3) { prop_assert!(!(w[0] == 0 && w[1] == 0 && w[2] < 3)); } + } + } +} diff --git a/crates/color/Cargo.toml b/crates/color/Cargo.toml new file mode 100644 index 00000000..fc9dc552 --- /dev/null +++ b/crates/color/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "filmcraft-color" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs new file mode 100644 index 00000000..179adb7b --- /dev/null +++ b/crates/color/src/lib.rs @@ -0,0 +1 @@ +//! placeholder diff --git a/crates/geom/Cargo.toml b/crates/geom/Cargo.toml new file mode 100644 index 00000000..cf55f4ca --- /dev/null +++ b/crates/geom/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "filmcraft-geom" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/geom/src/lib.rs b/crates/geom/src/lib.rs new file mode 100644 index 00000000..179adb7b --- /dev/null +++ b/crates/geom/src/lib.rs @@ -0,0 +1 @@ +//! placeholder diff --git a/crates/time/Cargo.toml b/crates/time/Cargo.toml new file mode 100644 index 00000000..979e4c78 --- /dev/null +++ b/crates/time/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "filmcraft-time" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[lints] +workspace = true + +[dependencies] +serde = { workspace = true } + +[dev-dependencies] +proptest = { workspace = true } diff --git a/crates/time/src/lib.rs b/crates/time/src/lib.rs new file mode 100644 index 00000000..184ba532 --- /dev/null +++ b/crates/time/src/lib.rs @@ -0,0 +1,518 @@ +//! Exact media time for FilmCraft. +//! +//! All time is an integer number of [`Tick`]s at [`TICKS_PER_SECOND`] = 254 016 000 000/s. That +//! rate divides evenly into every broadcast frame duration (23.976, 24, 25, 29.97, 30, 48, 50, +//! 59.94, 60, 120 …) and every common audio sample duration (8 k … 192 kHz, including the 44.1 k +//! family), so edits, frame math and audio alignment never drift. +//! +//! Timecode (SMPTE drop/non-drop, frames, feet+frames, samples) is only a *display* of ticks. + +use serde::{Deserialize, Serialize}; +use std::fmt; +use std::ops::{Add, AddAssign, Neg, Sub, SubAssign}; + +/// Ticks per second. +pub const TICKS_PER_SECOND: i64 = 254_016_000_000; + +/// A point or duration in time, in ticks. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(transparent)] +pub struct Tick(pub i64); + +impl Tick { + pub const ZERO: Tick = Tick(0); + pub const MAX: Tick = Tick(i64::MAX / 4); + pub const MIN: Tick = Tick(i64::MIN / 4); + + pub fn from_seconds_f64(s: f64) -> Tick { + Tick((s * TICKS_PER_SECOND as f64).round() as i64) + } + pub fn seconds(self) -> f64 { + self.0 as f64 / TICKS_PER_SECOND as f64 + } + /// Exact conversion from a count of `units` at `per_second` (e.g. samples at 48 000). + pub fn from_units(units: i64, per_second: i64) -> Tick { + Tick(((units as i128 * TICKS_PER_SECOND as i128) / per_second as i128) as i64) + } + /// Floor conversion to a count of units at `per_second`. + pub fn to_units_floor(self, per_second: i64) -> i64 { + (self.0 as i128 * per_second as i128).div_euclid(TICKS_PER_SECOND as i128) as i64 + } + /// Conversion from a rational timestamp `pts * num / den` seconds (container timebases). + pub fn from_rational(pts: i64, num: i64, den: i64) -> Tick { + let t = pts as i128 * num as i128 * TICKS_PER_SECOND as i128; + Tick(t.div_euclid(den as i128) as i64) + } + /// Inverse of [`Tick::from_rational`], floored to the timebase. + pub fn to_rational_floor(self, num: i64, den: i64) -> i64 { + (self.0 as i128 * den as i128).div_euclid(num as i128 * TICKS_PER_SECOND as i128) as i64 + } + pub fn abs(self) -> Tick { + Tick(self.0.abs()) + } + pub fn min(self, o: Tick) -> Tick { + Tick(self.0.min(o.0)) + } + pub fn max(self, o: Tick) -> Tick { + Tick(self.0.max(o.0)) + } + pub fn clamp(self, lo: Tick, hi: Tick) -> Tick { + Tick(self.0.clamp(lo.0, hi.0)) + } + /// Scale by a rational factor (`num/den`), flooring. + pub fn mul_ratio(self, num: i64, den: i64) -> Tick { + Tick((self.0 as i128 * num as i128).div_euclid(den as i128) as i64) + } +} + +impl Add for Tick { + type Output = Tick; + fn add(self, o: Tick) -> Tick { + Tick(self.0 + o.0) + } +} +impl Sub for Tick { + type Output = Tick; + fn sub(self, o: Tick) -> Tick { + Tick(self.0 - o.0) + } +} +impl Neg for Tick { + type Output = Tick; + fn neg(self) -> Tick { + Tick(-self.0) + } +} +impl AddAssign for Tick { + fn add_assign(&mut self, o: Tick) { + self.0 += o.0; + } +} +impl SubAssign for Tick { + fn sub_assign(&mut self, o: Tick) { + self.0 -= o.0; + } +} + +/// A half-open time range `[start, start + duration)`. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct TimeRange { + pub start: Tick, + pub duration: Tick, +} + +impl TimeRange { + pub fn new(start: Tick, duration: Tick) -> Self { + Self { start, duration } + } + pub fn from_bounds(start: Tick, end: Tick) -> Self { + Self { start, duration: end - start } + } + pub fn end(&self) -> Tick { + self.start + self.duration + } + pub fn contains(&self, t: Tick) -> bool { + t >= self.start && t < self.end() + } + pub fn overlaps(&self, o: &TimeRange) -> bool { + self.start < o.end() && o.start < self.end() + } + pub fn intersect(&self, o: &TimeRange) -> Option { + let s = self.start.max(o.start); + let e = self.end().min(o.end()); + (e > s).then(|| TimeRange::from_bounds(s, e)) + } + pub fn is_empty(&self) -> bool { + self.duration.0 <= 0 + } +} + +/// A frame rate as an exact rational (`num / den` frames per second). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct FrameRate { + pub num: i64, + pub den: i64, +} + +impl Default for FrameRate { + fn default() -> Self { + FrameRate::FPS_23_976 + } +} + +impl FrameRate { + pub const FPS_23_976: FrameRate = FrameRate { num: 24000, den: 1001 }; + pub const FPS_24: FrameRate = FrameRate { num: 24, den: 1 }; + pub const FPS_25: FrameRate = FrameRate { num: 25, den: 1 }; + pub const FPS_29_97: FrameRate = FrameRate { num: 30000, den: 1001 }; + pub const FPS_30: FrameRate = FrameRate { num: 30, den: 1 }; + pub const FPS_48: FrameRate = FrameRate { num: 48, den: 1 }; + pub const FPS_50: FrameRate = FrameRate { num: 50, den: 1 }; + pub const FPS_59_94: FrameRate = FrameRate { num: 60000, den: 1001 }; + pub const FPS_60: FrameRate = FrameRate { num: 60, den: 1 }; + pub const FPS_119_88: FrameRate = FrameRate { num: 120000, den: 1001 }; + pub const FPS_120: FrameRate = FrameRate { num: 120, den: 1 }; + + /// Rates offered in sequence settings (Premiere's list). + pub const COMMON: [FrameRate; 11] = [ + Self::FPS_23_976, + Self::FPS_24, + Self::FPS_25, + Self::FPS_29_97, + Self::FPS_30, + Self::FPS_48, + Self::FPS_50, + Self::FPS_59_94, + Self::FPS_60, + Self::FPS_119_88, + Self::FPS_120, + ]; + + pub fn new(num: i64, den: i64) -> Self { + let g = gcd(num.abs(), den.abs()).max(1); + FrameRate { num: num / g, den: den / g } + } + + /// Closest standard rate for a float (e.g. from a container's average rate). + pub fn from_f64(fps: f64) -> Self { + for r in Self::COMMON { + if (r.as_f64() - fps).abs() < 0.005 { + return r; + } + } + FrameRate::new((fps * 1000.0).round() as i64, 1000) + } + + pub fn as_f64(self) -> f64 { + self.num as f64 / self.den as f64 + } + + /// Exact duration of one frame (rounded down only for exotic rates). + pub fn frame_duration(self) -> Tick { + Tick(((TICKS_PER_SECOND as i128 * self.den as i128) / self.num as i128) as i64) + } + + /// Index of the frame containing `t` (floor). + pub fn frame_at(self, t: Tick) -> i64 { + (t.0 as i128 * self.num as i128).div_euclid(TICKS_PER_SECOND as i128 * self.den as i128) as i64 + } + + /// Start tick of frame `f`. + pub fn tick_of(self, f: i64) -> Tick { + let n = f as i128 * TICKS_PER_SECOND as i128 * self.den as i128; + Tick(n.div_euclid(self.num as i128) as i64) + } + + /// Snap `t` down to a frame boundary. + pub fn snap(self, t: Tick) -> Tick { + self.tick_of(self.frame_at(t)) + } + + /// Snap `t` to the nearest frame boundary. + pub fn snap_nearest(self, t: Tick) -> Tick { + let a = self.snap(t); + let b = self.tick_of(self.frame_at(t) + 1); + if (t - a) <= (b - t) { a } else { b } + } + + /// Timecode base (frames counted per timecode second): 30 for 29.97, 24 for 23.976. + pub fn timecode_base(self) -> i64 { + ((self.num + self.den - 1) / self.den).max(1) + } + + /// NTSC (x/1001) rates can use drop-frame timecode. + pub fn is_ntsc(self) -> bool { + self.den == 1001 + } + + /// Whether drop-frame counting applies (29.97 / 59.94 / 119.88). + pub fn supports_drop_frame(self) -> bool { + self.is_ntsc() && self.timecode_base() % 30 == 0 + } + + pub fn label(self) -> String { + if self.den == 1 { + format!("{}", self.num) + } else { + let v = self.as_f64(); + let s = format!("{v:.3}"); + let s = s.trim_end_matches('0').trim_end_matches('.'); + s.to_string() + } + } +} + +impl fmt::Display for FrameRate { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{} fps", self.label()) + } +} + +fn gcd(mut a: i64, mut b: i64) -> i64 { + while b != 0 { + (a, b) = (b, a % b); + } + a +} + +/// How time is displayed (Premiere: Timecode, Feet+Frames 16mm/35mm, Frames, Audio Samples). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum TimeDisplay { + #[default] + Timecode, + Frames, + Feet16, + Feet35, + AudioSamples, + Seconds, +} + +impl TimeDisplay { + pub const ALL: [TimeDisplay; 6] = + [TimeDisplay::Timecode, TimeDisplay::Feet35, TimeDisplay::Feet16, TimeDisplay::Frames, TimeDisplay::AudioSamples, TimeDisplay::Seconds]; + pub fn label(self) -> &'static str { + match self { + TimeDisplay::Timecode => "Timecode", + TimeDisplay::Frames => "Frames", + TimeDisplay::Feet16 => "Feet + Frames 16mm", + TimeDisplay::Feet35 => "Feet + Frames 35mm", + TimeDisplay::AudioSamples => "Audio Samples", + TimeDisplay::Seconds => "Seconds", + } + } +} + +/// Drop-frame parameters: frames dropped per minute and nominal base. +fn df_params(rate: FrameRate) -> (i64, i64) { + let base = rate.timecode_base(); + (base / 15, base) // 30 → 2, 60 → 4, 120 → 8 +} + +/// Convert a frame count to SMPTE fields `(negative, h, m, s, f)`. +pub fn frames_to_fields(frame: i64, rate: FrameRate, drop_frame: bool) -> (bool, i64, i64, i64, i64) { + let neg = frame < 0; + let mut n = frame.abs(); + let (drop, base) = df_params(rate); + if drop_frame && rate.supports_drop_frame() { + let per_min = base * 60 - drop; + let per_10 = per_min * 10 + drop; + let d = n / per_10; + let m = n % per_10; + n += drop * 9 * d; + if m > drop { + n += drop * ((m - drop) / per_min); + } + } + let f = n % base; + let s = (n / base) % 60; + let mi = (n / (base * 60)) % 60; + let h = n / (base * 3600); + (neg, h, mi, s, f) +} + +/// Convert SMPTE fields back to a frame count. +pub fn fields_to_frames(h: i64, m: i64, s: i64, f: i64, rate: FrameRate, drop_frame: bool) -> i64 { + let (drop, base) = df_params(rate); + let mut n = ((h * 3600) + m * 60 + s) * base + f; + if drop_frame && rate.supports_drop_frame() { + let total_min = h * 60 + m; + n -= drop * (total_min - total_min / 10); + } + n +} + +/// Format a frame count as SMPTE timecode (`HH:MM:SS:FF`, or `HH;MM;SS;FF` for drop-frame). +pub fn format_timecode_frames(frame: i64, rate: FrameRate, drop_frame: bool) -> String { + let df = drop_frame && rate.supports_drop_frame(); + let (neg, h, m, s, f) = frames_to_fields(frame, rate, df); + let sep = if df { ';' } else { ':' }; + let fw = if rate.timecode_base() >= 100 { 3 } else { 2 }; + format!("{}{h:02}{sep}{m:02}{sep}{s:02}{sep}{f:0fw$}", if neg { "-" } else { "" }) +} + +/// Format a tick according to a display mode. +pub fn format_time(t: Tick, rate: FrameRate, drop_frame: bool, display: TimeDisplay, sample_rate: i64) -> String { + let frame = rate.frame_at(t); + match display { + TimeDisplay::Timecode => format_timecode_frames(frame, rate, drop_frame), + TimeDisplay::Frames => format!("{frame}"), + TimeDisplay::Feet35 | TimeDisplay::Feet16 => { + let per_ft = if display == TimeDisplay::Feet35 { 16 } else { 40 }; + let neg = frame < 0; + let a = frame.abs(); + format!("{}{}+{:02}", if neg { "-" } else { "" }, a / per_ft, a % per_ft) + } + TimeDisplay::AudioSamples => { + let secs = t.0.div_euclid(TICKS_PER_SECOND); + let rem = Tick(t.0.rem_euclid(TICKS_PER_SECOND)).to_units_floor(sample_rate); + format!("{:02}:{:02}:{:02}:{rem:05}", secs / 3600, (secs / 60) % 60, secs % 60) + } + TimeDisplay::Seconds => format!("{:.3}", t.seconds()), + } +} + +/// Error from [`parse_timecode`]. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ParseError(pub String); + +impl fmt::Display for ParseError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.0) + } +} +impl std::error::Error for ParseError {} + +/// Parse user-typed timecode into a frame count, the way Premiere's timecode fields do: +/// - `01:02:03:04`, `01;02;03;04`, `1.2.3.4` (any of `:;.,` separators), +/// - bare digits are read right-aligned as `HHMMSSFF` (`1000` = 00:00:10:00), +/// - a leading `+`/`-` makes the value relative to `current` (`+15` = 15 frames later, `-1.00` = 1 s earlier). +pub fn parse_timecode(input: &str, rate: FrameRate, drop_frame: bool, current: i64) -> Result { + let s = input.trim(); + if s.is_empty() { + return Err(ParseError("empty timecode".into())); + } + let (rel, body) = match s.as_bytes()[0] { + b'+' => (Some(1), &s[1..]), + b'-' => (Some(-1), &s[1..]), + _ => (None, s), + }; + let parts: Vec<&str> = body.split([':', ';', '.', ',']).collect(); + let nums: Vec = if parts.len() == 1 { + let digits = parts[0]; + if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { + return Err(ParseError(format!("not a timecode: `{input}`"))); + } + // Right-aligned pairs: "12345" -> [1, 23, 45]; relative bare numbers are frames. + if rel.is_some() { + vec![digits.parse().map_err(|_| ParseError("number too large".into()))?] + } else { + let mut v = Vec::new(); + let b = digits.as_bytes(); + let mut end = b.len(); + while end > 0 { + let start = end.saturating_sub(2); + v.push(digits[start..end].parse::().unwrap_or(0)); + end = start; + } + v.reverse(); + v + } + } else { + parts + .iter() + .map(|p| if p.is_empty() { Ok(0) } else { p.parse::().map_err(|_| ParseError(format!("bad field `{p}`"))) }) + .collect::>()? + }; + if nums.len() > 4 { + return Err(ParseError("too many timecode fields".into())); + } + let mut f4 = [0i64; 4]; + let off = 4 - nums.len(); + f4[off..].copy_from_slice(&nums); + let [h, m, sec, fr] = f4; + // Overflowing fields (e.g. 90 frames) are allowed, as in Premiere. + let base = rate.timecode_base(); + let frames = if nums.len() == 1 && rel.is_some() { + fr + } else if drop_frame && rate.supports_drop_frame() && m < 60 && sec < 60 && fr < base { + fields_to_frames(h, m, sec, fr, rate, true) + } else { + ((h * 3600) + m * 60 + sec) * base + fr + }; + Ok(match rel { + Some(sign) => current + sign * frames, + None => frames, + }) +} + +/// Common audio sample rates offered in sequence settings. +pub const SAMPLE_RATES: [i64; 6] = [32_000, 44_100, 48_000, 88_200, 96_000, 192_000]; + +#[cfg(test)] +mod tests { + use super::*; + use proptest::prelude::*; + + #[test] + fn every_common_rate_is_exact() { + for r in FrameRate::COMMON { + let d = r.frame_duration(); + assert_eq!(d.0 as i128 * r.num as i128, TICKS_PER_SECOND as i128 * r.den as i128, "{r}"); + } + for sr in SAMPLE_RATES.iter().chain(&[8000, 11025, 16000, 22050, 176_400]) { + assert_eq!(TICKS_PER_SECOND % sr, 0, "{sr}"); + } + } + + #[test] + fn drop_frame_known_values() { + let r = FrameRate::FPS_29_97; + assert_eq!(format_timecode_frames(0, r, true), "00;00;00;00"); + assert_eq!(format_timecode_frames(1799, r, true), "00;00;59;29"); + assert_eq!(format_timecode_frames(1800, r, true), "00;01;00;02"); + assert_eq!(format_timecode_frames(17982, r, true), "00;10;00;00"); + assert_eq!(format_timecode_frames(107892, r, true), "01;00;00;00"); + assert_eq!(format_timecode_frames(1800, r, false), "00:01:00:00"); + let r60 = FrameRate::FPS_59_94; + assert_eq!(format_timecode_frames(3600, r60, true), "00;01;00;04"); + } + + #[test] + fn parse_forms() { + let r = FrameRate::FPS_25; + assert_eq!(parse_timecode("00:00:10:00", r, false, 0).unwrap(), 250); + assert_eq!(parse_timecode("1000", r, false, 0).unwrap(), 250); + assert_eq!(parse_timecode("1.00", r, false, 0).unwrap(), 25); + assert_eq!(parse_timecode("+15", r, false, 100).unwrap(), 115); + assert_eq!(parse_timecode("-1.00", r, false, 100).unwrap(), 75); + assert_eq!(parse_timecode("00;01;00;02", FrameRate::FPS_29_97, true, 0).unwrap(), 1800); + assert!(parse_timecode("abc", r, false, 0).is_err()); + } + + #[test] + fn display_modes() { + let r = FrameRate::FPS_24; + let t = r.tick_of(40); + assert_eq!(format_time(t, r, false, TimeDisplay::Feet35, 48000), "2+08"); + assert_eq!(format_time(t, r, false, TimeDisplay::Feet16, 48000), "1+00"); + assert_eq!(format_time(t, r, false, TimeDisplay::Frames, 48000), "40"); + assert_eq!(format_time(Tick::from_units(48_001, 48_000), r, false, TimeDisplay::AudioSamples, 48000), "00:00:01:00001"); + } + + #[test] + fn rational_conversions() { + // 90 kHz MPEG timebase + let t = Tick::from_rational(90_000, 1, 90_000); + assert_eq!(t.0, TICKS_PER_SECOND); + assert_eq!(t.to_rational_floor(1, 90_000), 90_000); + assert_eq!(FrameRate::from_f64(29.97), FrameRate::FPS_29_97); + assert_eq!(FrameRate::FPS_23_976.label(), "23.976"); + } + + proptest! { + #[test] + fn frame_tick_roundtrip(f in -1_000_000i64..10_000_000, ri in 0usize..11) { + let r = FrameRate::COMMON[ri]; + prop_assert_eq!(r.frame_at(r.tick_of(f)), f); + prop_assert_eq!(r.frame_at(r.tick_of(f) + r.frame_duration() - Tick(1)), f); + } + + #[test] + fn drop_frame_bijection(f in 0i64..(24 * 107_892)) { + let r = FrameRate::FPS_29_97; + let (_, h, m, s, fr) = frames_to_fields(f, r, true); + // dropped labels never appear + prop_assert!(!(s == 0 && fr < 2 && m % 10 != 0)); + prop_assert_eq!(fields_to_frames(h, m, s, fr, r, true), f); + let txt = format_timecode_frames(f, r, true); + prop_assert_eq!(parse_timecode(&txt, r, true, 0).unwrap(), f); + } + + #[test] + fn ndf_parse_roundtrip(f in 0i64..10_000_000, ri in 0usize..11) { + let r = FrameRate::COMMON[ri]; + let txt = format_timecode_frames(f, r, false); + prop_assert_eq!(parse_timecode(&txt, r, false, 0).unwrap(), f); + } + } +} diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 00000000..40d36bf9 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 160 +use_small_heuristics = "Max" diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 00000000..df7dfa87 --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "xtask" +version.workspace = true +edition.workspace = true +license.workspace = true +publish = false + +[dependencies] +serde_json = { workspace = true } diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 00000000..f328e4d9 --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1 @@ +fn main() {}