diff --git a/.github/workflows/freebsd.yml b/.github/workflows/freebsd.yml
index a7446cac9..a3fb4af4d 100644
--- a/.github/workflows/freebsd.yml
+++ b/.github/workflows/freebsd.yml
@@ -31,10 +31,10 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 240
steps:
- - uses: actions/checkout@v4
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Build and package in a FreeBSD VM
# vmactions/freebsd-vm v1.5.9, pinned by commit (it runs our build).
- uses: vmactions/freebsd-vm@c46abacb49f09938ca4e1702d15d836285d694cc
+ uses: vmactions/freebsd-vm@c46abacb49f09938ca4e1702d15d836285d694cc # v1.5.9
with:
release: "14.3"
usesh: true
diff --git a/.github/workflows/packaging-lint.yml b/.github/workflows/packaging-lint.yml
index 650b1dc83..960f1ee38 100644
--- a/.github/workflows/packaging-lint.yml
+++ b/.github/workflows/packaging-lint.yml
@@ -23,7 +23,7 @@ jobs:
env:
ACTIONLINT_VERSION: 1.7.12
steps:
- - uses: actions/checkout@v4
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Install tools
run: |
sudo apt-get update
@@ -35,6 +35,8 @@ jobs:
"$RUNNER_TEMP/actionlint" -color
- name: shellcheck
run: shellcheck packaging/*.sh packaging/*/*.sh
+ - name: Linux tarball installer
+ run: python3 packaging/linux/test_install.py
- name: PowerShell
shell: pwsh
run: |
@@ -62,6 +64,11 @@ jobs:
assert m["id"] == "ai.storyteller.filmcraft" and m["command"] == "filmcraft", m["id"]
for key in ("runtime", "runtime-version", "sdk", "finish-args"):
assert src[key] == bundle[key], f"Flatpak manifests disagree on {key}: {src[key]} vs {bundle[key]}"
+ # Both install the app and the CLI/MCP server (#346).
+ import re
+ def programs(m):
+ return {p for c in m["modules"][0]["build-commands"] for p in re.findall(r"/app/bin/([\w.-]+)", c)}
+ assert programs(src) == programs(bundle) == {"filmcraft", "filmcraft-cli"}, f"Flatpak manifests install {programs(src)} vs {programs(bundle)}"
print("Flatpak manifests ok")
EOF
- name: FreeBSD package layout (stub binaries)
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index a65db7bfd..08eba22b6 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -43,7 +43,7 @@ jobs:
date: ${{ steps.v.outputs.date }}
prerelease: ${{ steps.v.outputs.prerelease }}
steps:
- - uses: actions/checkout@v4
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- id: v
name: Resolve version
run: |
@@ -71,17 +71,16 @@ jobs:
FILMCRAFT_VERSION: ${{ needs.version.outputs.version }}
FILMCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
steps:
- - uses: actions/checkout@v4
- # Fonts (optional build input, required for releases): docs/contributing.md#building-with-craft-fonts
- - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: storytold/craft-fonts
ref: abb83316d96aa59c1cf64784289e378fe9fa5695 # bump deliberately
path: craft-fonts
- - uses: dtolnay/rust-toolchain@stable
+ - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
with:
targets: aarch64-apple-darwin, x86_64-apple-darwin
- - uses: Swatinem/rust-cache@v2
+ - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
with:
key: release-macos-universal
- name: Apply version override
@@ -101,7 +100,7 @@ jobs:
CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts
CRAFT_FONTS_REQUIRED: "1"
run: packaging/macos/package.sh --arch universal
- - uses: actions/upload-artifact@v4
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: release-macos
path: dist/release/*
@@ -129,17 +128,16 @@ jobs:
FILMCRAFT_VERSION: ${{ needs.version.outputs.version }}
FILMCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
steps:
- - uses: actions/checkout@v4
- # Fonts (optional build input, required for releases): docs/contributing.md#building-with-craft-fonts
- - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: storytold/craft-fonts
ref: abb83316d96aa59c1cf64784289e378fe9fa5695 # bump deliberately
path: craft-fonts
- - uses: dtolnay/rust-toolchain@stable
+ - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
with:
targets: ${{ matrix.target }}
- - uses: Swatinem/rust-cache@v2
+ - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
with:
key: release-windows-${{ matrix.arch }}
- name: Apply version override
@@ -163,7 +161,7 @@ jobs:
CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts
CRAFT_FONTS_REQUIRED: "1"
run: ./packaging/windows/package.ps1 -Arch ${{ matrix.arch }}
- - uses: actions/upload-artifact@v4
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: release-windows-${{ matrix.arch }}
path: dist/release/*
@@ -187,9 +185,8 @@ jobs:
FILMCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
NFPM_VERSION: 2.47.0
steps:
- - uses: actions/checkout@v4
- # Fonts (optional build input, required for releases): docs/contributing.md#building-with-craft-fonts
- - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: storytold/craft-fonts
ref: abb83316d96aa59c1cf64784289e378fe9fa5695 # bump deliberately
@@ -203,8 +200,8 @@ jobs:
curl -fsSL -o "$RUNNER_TEMP/nfpm.deb" \
"https://github.com/goreleaser/nfpm/releases/download/v${NFPM_VERSION}/nfpm_${NFPM_VERSION}_${{ matrix.deb }}.deb"
sudo dpkg -i "$RUNNER_TEMP/nfpm.deb"
- - uses: dtolnay/rust-toolchain@stable
- - uses: Swatinem/rust-cache@v2
+ - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
+ - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
with:
key: release-linux-${{ matrix.arch }}
- name: Apply version override
@@ -228,7 +225,7 @@ jobs:
"$appimage" --appimage-updateinformation | grep -F 'gh-releases-zsync|'
test -s "$appimage.zsync"
done
- - uses: actions/upload-artifact@v4
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: release-linux-${{ matrix.arch }}
path: dist/release/*
@@ -251,8 +248,8 @@ jobs:
FILMCRAFT_VERSION: ${{ needs.version.outputs.version }}
FILMCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
steps:
- - uses: actions/checkout@v4
- - uses: actions/download-artifact@v4
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: release-linux-${{ matrix.arch }}
path: dist/linux
@@ -266,7 +263,7 @@ jobs:
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
- name: Build the Flatpak bundle
run: packaging/linux/flatpak-bundle.sh "dist/linux/filmcraft-$FILMCRAFT_VERSION-linux-${{ matrix.arch }}.tar.gz"
- - uses: actions/upload-artifact@v4
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: release-flatpak-${{ matrix.arch }}
path: dist/release/*.flatpak
@@ -284,16 +281,15 @@ jobs:
FILMCRAFT_VERSION: ${{ needs.version.outputs.version }}
FILMCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
steps:
- - uses: actions/checkout@v4
- # Fonts (optional build input, required for releases): docs/contributing.md#building-with-craft-fonts
- - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: storytold/craft-fonts
ref: abb83316d96aa59c1cf64784289e378fe9fa5695 # bump deliberately
path: craft-fonts
- name: Build and package in a FreeBSD VM (tar.gz)
# vmactions/freebsd-vm v1.5.9, pinned by commit (it runs our build).
- uses: vmactions/freebsd-vm@c46abacb49f09938ca4e1702d15d836285d694cc
+ uses: vmactions/freebsd-vm@c46abacb49f09938ca4e1702d15d836285d694cc # v1.5.9
with:
release: "14.3"
usesh: true
@@ -318,7 +314,7 @@ jobs:
bash packaging/freebsd/package.sh
tar -tzf dist/release/filmcraft-*-freebsd-x86_64.tar.gz \
| grep -E '/bin/filmcraft$|/bin/filmcraft-cli$|/share/applications/ai\.storyteller\.filmcraft\.desktop$|/share/doc/filmcraft/LICENSE-MIT$'
- - uses: actions/upload-artifact@v4
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: release-freebsd-x86_64
path: dist/release/*
@@ -334,17 +330,16 @@ jobs:
FILMCRAFT_BUILD_DATE: ${{ needs.version.outputs.date }}
WASM_BINDGEN_VERSION: 0.2.129
steps:
- - uses: actions/checkout@v4
- # Fonts (optional build input, required for releases): docs/contributing.md#building-with-craft-fonts
- - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: storytold/craft-fonts
ref: abb83316d96aa59c1cf64784289e378fe9fa5695 # bump deliberately
path: craft-fonts
- - uses: dtolnay/rust-toolchain@stable
+ - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
with:
targets: wasm32-unknown-unknown
- - uses: Swatinem/rust-cache@v2
+ - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
with:
key: release-web
- name: Install wasm-bindgen CLI
@@ -362,7 +357,7 @@ jobs:
CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts
CRAFT_FONTS_REQUIRED: "1"
run: packaging/web/package.sh
- - uses: actions/upload-artifact@v4
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: release-web
path: dist/release/*
@@ -381,7 +376,7 @@ jobs:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
steps:
- - uses: actions/download-artifact@v4
+ - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
pattern: release-*
path: artifacts
diff --git a/.github/workflows/windows-arm64.yml b/.github/workflows/windows-arm64.yml
index 22a3a54bc..ccb475e58 100644
--- a/.github/workflows/windows-arm64.yml
+++ b/.github/workflows/windows-arm64.yml
@@ -36,11 +36,11 @@ jobs:
runs-on: windows-latest
timeout-minutes: 90
steps:
- - uses: actions/checkout@v4
- - uses: dtolnay/rust-toolchain@89b12181fb390509a0842a86cc55eeb8eb928c1d # stable
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ - uses: dtolnay/rust-toolchain@686976e191b89faba57d3206551f0f330d8cb249 # stable
with:
targets: aarch64-pc-windows-msvc
- - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2
+ - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2
with:
key: windows-arm64-package
- name: Install WiX
@@ -51,7 +51,7 @@ jobs:
env:
DIST: ${{ github.workspace }}\dist\arm64
run: ./packaging/windows/package.ps1 -Arch arm64
- - uses: actions/upload-artifact@v4
+ - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: filmcraft-windows-arm64
path: dist/arm64/*
@@ -64,7 +64,7 @@ jobs:
runs-on: windows-11-arm
timeout-minutes: 30
steps:
- - uses: actions/download-artifact@v4
+ - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: filmcraft-windows-arm64
path: dist
diff --git a/AGENTS.md b/AGENTS.md
index bea60585b..684860456 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -21,7 +21,7 @@ panic path, and fix a crash before building on top of it. The cross-app standard
code alone (a literal that always parses): a single-item `#[allow(clippy::expect_used)]` with the reason.
3. **No `unsafe`** (`unsafe_code = "forbid"` for the workspace). The single exception is
`crates/platform`, which needs `unsafe` to call the operating system's media APIs (hardware
- video decoding through VideoToolbox and Media Foundation / Direct3D 11; later encoding and Linux) and does nothing else. It
+ video decoding through VideoToolbox, Media Foundation / Direct3D 11 and VA-API; hardware encoding) and does nothing else. It
uses `unsafe_code = "deny"` with `#[allow(unsafe_code)]` only on its FFI modules, a
`// SAFETY:` comment on every `unsafe` block, a safe `Result`-returning public API, and the
pure-Rust decoder as the tested fallback ([ADR 0001](docs/adr/0001-platform-ffi.md)).
diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md
index 4b28b9539..3e94fa45b 100644
--- a/ATTRIBUTION.md
+++ b/ATTRIBUTION.md
@@ -9,6 +9,7 @@ iconography, images or other Adobe assets; open licences only; every asset attri
| Asset | Author | Source | Licence |
|---|---|---|---|
+| `crates/ui-egui/src/i18n/uk.tsv` | FilmCraft contributors | Original work: clean-room Ukrainian interface translations | MIT OR Apache-2.0 |
| `assets/fonts/Inter-Regular.ttf` | The Inter Project Authors | https://github.com/rsms/inter | OFL-1.1 (`assets/fonts/OFL-Inter.txt`) |
| `assets/fonts/Inter-Medium.ttf` | The Inter Project Authors | https://github.com/rsms/inter | OFL-1.1 (`assets/fonts/OFL-Inter.txt`) |
| `assets/fonts/Inter-SemiBold.ttf` | The Inter Project Authors | https://github.com/rsms/inter | OFL-1.1 (`assets/fonts/OFL-Inter.txt`) |
@@ -26,6 +27,8 @@ iconography, images or other Adobe assets; open licences only; every asset attri
| `docs/images/filmcraft-effects.png` | FilmCraft contributors | Original work: FilmCraft screenshot crop; NASA Earth Views (US Government work, public domain) | MIT OR Apache-2.0; footage public domain |
| `docs/images/filmcraft-timeline.png` | FilmCraft contributors | Original work: FilmCraft UI crop; thumbnails from Night of the Living Dead (1968) and Carnival of Souls (1962), CC0 Chopin waveform | MIT OR Apache-2.0; film frames public domain |
| `assets/app-icon/filmcraft.svg` | Brandon Thomas (FilmCraft owner) | Original work: FilmCraft app icon (owl with ear tufts), drawn by the owner in ArtCraft and vectorised; canonical vector | MIT OR Apache-2.0 (`assets/app-icon/LICENSE.txt`) |
+| `packaging/macos/dmg/background.svg` | @XusBadia | Original work: FilmCraft macOS DMG window background, derived from the FilmCraft app icon (`assets/app-icon/filmcraft.svg`); rendered to `background.tiff` by `packaging/macos/dmg/generate.py` | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) |
+| `packaging/macos/dmg/background.tiff` | @XusBadia | Rendered from `packaging/macos/dmg/background.svg` by `packaging/macos/dmg/generate.py` | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) |
| `assets/app-icon/filmcraft-small.svg` | Brandon Thomas (FilmCraft owner) | Original work: FilmCraft app icon (owl with ear tufts), drawn by the owner in ArtCraft and vectorised; lighter vector | MIT OR Apache-2.0 (`assets/app-icon/LICENSE.txt`) |
| `assets/app-icon/filmcraft-1024.png` | Brandon Thomas (FilmCraft owner) | Original work: FilmCraft app icon (owl with ear tufts), drawn by the owner in ArtCraft and vectorised; 1024 px render | MIT OR Apache-2.0 (`assets/app-icon/LICENSE.txt`) |
| `assets/app-icon/filmcraft-macos-512.png` | Brandon Thomas (FilmCraft owner) | Original work: FilmCraft app icon (owl with ear tufts), drawn by the owner in ArtCraft and vectorised; macOS runtime Dock icon (512 px) | MIT OR Apache-2.0 (`assets/app-icon/LICENSE.txt`) |
@@ -99,6 +102,7 @@ is traced or derived from Adobe artwork.
| Asset | Where |
|---|---|
| UI icons (tools, transport, panels, header) | `crates/ui-egui/src/icons.rs`: vector paths on a 16×16 grid |
+| Source and Program range braces and trim hover cues | `crates/ui-egui/src/panels/monitor.rs` and `source_range.rs`: original generic vector shapes by FilmCraft contributors, MIT OR Apache-2.0; no third-party artwork |
| Align / distribute / paragraph-alignment glyphs in the graphics panel | `crates/ui-egui/src/panels/graphics.rs` (`align_glyph`): bars and lines drawn with the egui painter |
| Demo footage (ocean sunset, aurora, city night, dunes, forest, plasma), bars and tone, counting leader, colour matte | `crates/media/src/generators.rs` |
| Built-in graphics templates (Lower Third – Slab / Rule, Ticker – Crawl, Title – Centered / Boxed, End Credits – Roll, Callout – Pointer / Tag) and their placeholder text | `crates/project/src/gtemplate.rs` (`builtin_templates`): original designs defined in code with the bundled Inter font; thumbnails are rendered at runtime, no template, image or font files |
@@ -146,7 +150,7 @@ the original copyright and permission notice in its header.
| File | Author | Source | Licence |
|---|---|---|---|
-| `crates/platform/src/nvenc/ffi.rs` | NVIDIA Corporation (`nvEncodeAPI.h`); FilmCraft contributors (Rust transcription) | Structures, function table and constants transcribed from `nvEncodeAPI.h`, NVIDIA Video Codec SDK, API 12.1 (https://developer.nvidia.com/video-codec-sdk) | MIT (Copyright (c) 2010-2023 NVIDIA Corporation; notice kept in the file header) |
+| `crates/platform/src/nvenc/ffi.rs` | NVIDIA Corporation (`nvEncodeAPI.h`); FilmCraft contributors (Rust transcription) | Structures (including `NV_ENC_CONFIG_HEVC` and its bit-field masks, `NV_ENC_PIC_PARAMS_HEVC`, `NV_ENC_SEI_PAYLOAD`, `NV_ENC_TIME_CODE`), function table, codec / profile GUIDs (H.264, HEVC Main, HEVC Main 10) and constants (`NV_ENC_LEVEL_HEVC_*`, tier, CU size, `NV_ENC_BUFFER_FORMAT_YUV420_10BIT`, `NV_ENC_CAPS_SUPPORT_10BIT_ENCODE`, BT.2020 / PQ / HLG VUI codes) transcribed from `nvEncodeAPI.h`, NVIDIA Video Codec SDK, API 12.1 (https://developer.nvidia.com/video-codec-sdk) | MIT (Copyright (c) 2010-2023 NVIDIA Corporation; notice kept in the file header) |
## Downloaded at runtime
diff --git a/Cargo.lock b/Cargo.lock
index c2d388112..c7ae334fd 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1665,7 +1665,7 @@ dependencies = [
[[package]]
name = "filmcraft"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"chrono",
"cpal",
@@ -1681,13 +1681,14 @@ dependencies = [
"objc2-foundation 0.3.2",
"rfd",
"serde_json",
+ "sys-locale",
"winit",
"winresource",
]
[[package]]
name = "filmcraft-aac"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-bitstream",
"filmcraft-testkit",
@@ -1697,7 +1698,7 @@ dependencies = [
[[package]]
name = "filmcraft-ac3"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-testkit",
"thiserror 2.0.21",
@@ -1705,7 +1706,7 @@ dependencies = [
[[package]]
name = "filmcraft-apv"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-bitstream",
"filmcraft-testkit",
@@ -1715,14 +1716,14 @@ dependencies = [
[[package]]
name = "filmcraft-audio-dsp"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-testkit",
]
[[package]]
name = "filmcraft-automation"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"base64 0.22.1",
"filmcraft-engine",
@@ -1738,7 +1739,7 @@ dependencies = [
[[package]]
name = "filmcraft-av1"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-bitstream",
"filmcraft-testkit",
@@ -1749,14 +1750,14 @@ dependencies = [
[[package]]
name = "filmcraft-bitstream"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"proptest",
]
[[package]]
name = "filmcraft-captions"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-color",
"filmcraft-project",
@@ -1769,11 +1770,11 @@ dependencies = [
[[package]]
name = "filmcraft-cfb"
-version = "0.4.0"
+version = "0.5.0"
[[package]]
name = "filmcraft-cli"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-automation",
"filmcraft-codecs",
@@ -1790,7 +1791,7 @@ dependencies = [
[[package]]
name = "filmcraft-codecs"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-aac",
"filmcraft-ac3",
@@ -1816,6 +1817,7 @@ dependencies = [
"filmcraft-vp9",
"image",
"log",
+ "serde_json",
"symphonia",
"thiserror 2.0.21",
"web-time",
@@ -1823,7 +1825,7 @@ dependencies = [
[[package]]
name = "filmcraft-color"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"proptest",
"serde",
@@ -1831,7 +1833,7 @@ dependencies = [
[[package]]
name = "filmcraft-dnx"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-bitstream",
"filmcraft-testkit",
@@ -1841,8 +1843,9 @@ dependencies = [
[[package]]
name = "filmcraft-edit"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
+ "filmcraft-geom",
"filmcraft-media",
"filmcraft-project",
"filmcraft-time",
@@ -1853,7 +1856,7 @@ dependencies = [
[[package]]
name = "filmcraft-engine"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-audio-dsp",
"filmcraft-captions",
@@ -1885,7 +1888,7 @@ dependencies = [
[[package]]
name = "filmcraft-export"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-aac",
"filmcraft-apv",
@@ -1915,11 +1918,12 @@ dependencies = [
[[package]]
name = "filmcraft-format"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-media",
"filmcraft-project",
"filmcraft-time",
+ "log",
"serde",
"serde_json",
"thiserror 2.0.21",
@@ -1927,7 +1931,7 @@ dependencies = [
[[package]]
name = "filmcraft-frame"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-color",
"filmcraft-time",
@@ -1937,7 +1941,7 @@ dependencies = [
[[package]]
name = "filmcraft-geom"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"proptest",
"serde",
@@ -1945,7 +1949,7 @@ dependencies = [
[[package]]
name = "filmcraft-golden"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-color",
"filmcraft-frame",
@@ -1962,7 +1966,7 @@ dependencies = [
[[package]]
name = "filmcraft-gpu"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-color",
"filmcraft-frame",
@@ -1979,7 +1983,7 @@ dependencies = [
[[package]]
name = "filmcraft-h264"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-bitstream",
"filmcraft-testkit",
@@ -1989,7 +1993,7 @@ dependencies = [
[[package]]
name = "filmcraft-h264enc"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-bitstream",
"filmcraft-testkit",
@@ -1999,7 +2003,7 @@ dependencies = [
[[package]]
name = "filmcraft-hevc"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-bitstream",
"filmcraft-testkit",
@@ -2009,7 +2013,7 @@ dependencies = [
[[package]]
name = "filmcraft-interchange"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-cfb",
"filmcraft-color",
@@ -2026,7 +2030,7 @@ dependencies = [
[[package]]
name = "filmcraft-isobmff"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-bitstream",
"filmcraft-testkit",
@@ -2037,7 +2041,7 @@ dependencies = [
[[package]]
name = "filmcraft-matroska"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-testkit",
"serde_json",
@@ -2045,7 +2049,7 @@ dependencies = [
[[package]]
name = "filmcraft-media"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-color",
"filmcraft-frame",
@@ -2058,7 +2062,7 @@ dependencies = [
[[package]]
name = "filmcraft-mpeg2v"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-testkit",
"rayon",
@@ -2067,7 +2071,7 @@ dependencies = [
[[package]]
name = "filmcraft-mpegts"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-testkit",
"thiserror 2.0.21",
@@ -2075,39 +2079,43 @@ dependencies = [
[[package]]
name = "filmcraft-mxf"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-testkit",
]
[[package]]
name = "filmcraft-ogg"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-testkit",
]
[[package]]
name = "filmcraft-opus"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-testkit",
]
[[package]]
name = "filmcraft-platform"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
+ "filmcraft-bitstream",
"filmcraft-codecs",
"filmcraft-color",
"filmcraft-export",
"filmcraft-frame",
+ "filmcraft-h264",
+ "filmcraft-hevc",
"filmcraft-isobmff",
"filmcraft-media",
"filmcraft-project",
"filmcraft-render",
"filmcraft-testkit",
"filmcraft-time",
+ "libloading",
"log",
"objc2-core-foundation",
"objc2-core-media",
@@ -2118,7 +2126,7 @@ dependencies = [
[[package]]
name = "filmcraft-project"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-color",
"filmcraft-geom",
@@ -2132,7 +2140,7 @@ dependencies = [
[[package]]
name = "filmcraft-prores"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-bitstream",
"filmcraft-testkit",
@@ -2142,7 +2150,7 @@ dependencies = [
[[package]]
name = "filmcraft-render"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-audio-dsp",
"filmcraft-captions",
@@ -2162,7 +2170,7 @@ dependencies = [
[[package]]
name = "filmcraft-scopes"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-color",
"serde",
@@ -2171,7 +2179,7 @@ dependencies = [
[[package]]
name = "filmcraft-speech"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"candle-core",
"candle-nn",
@@ -2189,25 +2197,26 @@ dependencies = [
[[package]]
name = "filmcraft-testkit"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"png",
]
[[package]]
name = "filmcraft-text"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-color",
"harfrust",
"skrifa",
"unicode-bidi",
"unicode-linebreak",
+ "unicode-segmentation",
]
[[package]]
name = "filmcraft-time"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"proptest",
"serde",
@@ -2215,7 +2224,7 @@ dependencies = [
[[package]]
name = "filmcraft-tts"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"candle-core",
"candle-nn",
@@ -2229,7 +2238,7 @@ dependencies = [
[[package]]
name = "filmcraft-tts-text"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"serde",
"thiserror 2.0.21",
@@ -2237,7 +2246,7 @@ dependencies = [
[[package]]
name = "filmcraft-ui-egui"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"cpu-time",
"eframe",
@@ -2273,7 +2282,7 @@ dependencies = [
[[package]]
name = "filmcraft-vp9"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-bitstream",
"rayon",
@@ -2282,7 +2291,7 @@ dependencies = [
[[package]]
name = "filmcraft-web"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"eframe",
"egui",
@@ -5806,6 +5815,15 @@ dependencies = [
"syn 3.0.6",
]
+[[package]]
+name = "sys-locale"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8eab9a99a024a169fe8a903cf9d4a3b3601109bcc13bd9e3c6fff259138626c4"
+dependencies = [
+ "libc",
+]
+
[[package]]
name = "sysctl"
version = "0.6.0"
@@ -7263,7 +7281,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7"
[[package]]
name = "xtask"
-version = "0.4.0"
+version = "0.5.0"
dependencies = [
"filmcraft-testkit",
"serde_json",
diff --git a/Cargo.toml b/Cargo.toml
index 458ee1fd2..1d306cffa 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/*", "xtask"]
[workspace.package]
-version = "0.4.0"
+version = "0.5.0"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.95"
diff --git a/README.md b/README.md
index 0c91ed07b..a4381a8d1 100644
--- a/README.md
+++ b/README.md
@@ -83,7 +83,7 @@ FilmCraft is a non-linear editor for people who know Premiere: the same panels,
- **Three-point editing.** Mark In and Out in the Source monitor, then insert (`,`) or overwrite (`.`) onto the patched tracks. Lift (`;`) and extract (`'`) take ranges back out.
- **Every trim.** Ripple, roll, slip, slide, rate stretch and razor tools. Trim mode selects edit points as ripple, roll or trim, nudges them a frame at a time (`⌥←` `⌥→`, ×5 with `⇧`), toggles the trim type with `⌃T` and extends them to the playhead with `E`. `Q` and `W` ripple-trim to the playhead. The **Trim Monitor** shows both sides of the edit, and **dynamic trimming** trims live while it plays: `L` forward, `J` back, `K` to stop and commit as one undo step.
- **Exact time.** Every edit is computed on integer ticks: 254,016,000,000 per second, which divides evenly by every common frame rate and sample rate. 23.976, 29.97 drop-frame and 59.94 are exact, not approximate.
-- **The details pros rely on.** Markers with colours, names and durations; add edit (`⌘K`) on one or all tracks; nesting; copy, paste and paste insert; ripple delete and close gap; snapping; unlimited undo with a History panel.
+- **The details pros rely on.** Markers with colours, names and durations; export sequence review notes with **Markers ▸ Export Markers as CSV…**; add edit (`⌘K`) on one or all tracks; nesting; copy, paste and paste insert; ripple delete and close gap; snapping; unlimited undo with a History panel.
- **Your keys.** A Keyboard Shortcuts editor (`⌥⌘K`) with a drawn keyboard, panel-specific shortcuts, conflict warnings and presets for FilmCraft, Premiere Pro, Final Cut Pro and Avid key layouts.
- **Never lose work.** Saves are atomic, auto-save keeps a rolling set of versions, and a crash-recovery journal written about a second after each edit brings back unsaved changes after a crash or power cut.
@@ -137,7 +137,7 @@ FilmCraft is a non-linear editor for people who know Premiere: the same panels,
- **Premiere 26's full Video Effects bin (93 effects in 16 folders), the Legacy bin and the obsolete effects old projects use, plus around 30 transitions.** Blurs (Bokeh, Focus, Compound), keys (Ultra Key, Track Matte), distortions (Corner Pin, Turbulent Displace, Warp Stabilizer), Lights & Glows, Immersive Video (VR) effects on equirectangular footage, Posterize Time, Echo and more. Cross dissolve, dip to black or white, film dissolve, wipes, irises, pushes, slides, zooms, page peel, cube spin and more.
- **Motion and opacity on every clip:** position, scale, rotation, anchor point and anti-flicker, plus 26 blend modes.
-- **Keyframes like Premiere's:** linear, Bezier, auto and continuous Bezier, hold, ease in and ease out. Effect Controls shows a keyframe lane for every parameter, and each animated parameter opens into **value and velocity graphs** with draggable influence handles.
+- **Keyframes like Premiere's:** linear, Bezier, auto and continuous Bezier, hold, ease in and ease out. Effect Controls shows a keyframe lane for every parameter under a time ruler with the playhead's handle, and each animated parameter opens into **value and velocity graphs** with draggable influence handles. Effect Controls and the Properties panel share the keyframe navigator (◀ ◆ ▶): add or remove the keyframe at the playhead, step to the previous or next one.
- **A GPU compositor** built on wgpu (Metal, Vulkan, DirectX 12, WebGPU). It samples YUV straight from the decoder with footprint supersampling and blends in linear light. A CPU path renders the same frames, and the two are tested against each other.
@@ -185,7 +185,7 @@ No FFmpeg inside. The video codecs, AAC, Opus and the containers are our own Rus
| **Matroska / WebM** | ✓ | | Lacing, Cues, header stripping, HDR colour metadata |
| **Stills** | ✓ | ✓ | Import PNG, JPEG, GIF, WebP, TIFF and BMP; export PNG sequences and animated GIF |
-Also imported: MPEG-2 / MPEG-1 video, AC-3, MP2, MXF (OP1a / OP-Atom), MPEG transport and program streams (AVCHD, broadcast, DVD), Ogg and image sequences. Not yet: HEVC and AV1 export, camera RAW, E-AC-3. Hardware decoding works on macOS (VideoToolbox) and Windows (Media Foundation), with Linux to follow; H.264 export can use NVIDIA's encoder on Windows (opt-in) ([#30](https://github.com/storytold/filmcraft/issues/30)).
+Also imported: MPEG-2 / MPEG-1 video, AC-3, MP2, MXF (OP1a / OP-Atom), MPEG transport and program streams (AVCHD, broadcast, DVD), Ogg and image sequences. Not yet: AV1 export, camera RAW, E-AC-3. Hardware decoding works on macOS (VideoToolbox), Windows (Media Foundation) and, for H.264 and HEVC, Linux (VA-API); H.264 export can use NVIDIA's encoder on Windows and Linux (opt-in), and H.265 (HEVC Main, 8-bit) export works through it (NVENC on Windows, VideoToolbox on macOS) ([#30](https://github.com/storytold/filmcraft/issues/30)).
@@ -287,9 +287,9 @@ We track two numbers ([ROADMAP.md](ROADMAP.md#honest-assessment-2026-10-05)):
The biggest gaps today:
-- **Speed on big footage.** Hardware decoding works on macOS and Windows; Linux has no hardware path yet. Blend modes and the most common effects run on the GPU, but Lumetri, keys and export rendering still run on the CPU; H.264 encoding can use the hardware encoder on macOS and on Windows with an NVIDIA GPU (opt-in, Export ▸ Hardware encoding), and H.265 export is hardware-only there ([#30](https://github.com/storytold/filmcraft/issues/30)).
+- **Speed on big footage.** Hardware decoding works on macOS and Windows, and for H.264 and HEVC on Linux (VA-API; VP9 and AV1 decode in software there). Blend modes and the most common effects run on the GPU, but Lumetri, keys and export rendering still run on the CPU; H.264 encoding can use the hardware encoder on macOS and on Windows and Linux with an NVIDIA GPU (opt-in, Export ▸ Hardware encoding), and H.265 export is hardware-only on both (HDR sequences export Main 10 PQ / HLG with NVENC; VideoToolbox writes 8-bit SDR) ([#30](https://github.com/storytold/filmcraft/issues/30)).
- **No plugins.** No VST3 / Audio Units or OpenFX hosting.
-- **Delivery codecs.** H.264 is our only delivery-codec export; no HEVC or AV1 export yet.
+- **Delivery codecs.** H.264 is our only software delivery-codec export; H.265 exports only through a hardware encoder (NVIDIA on Windows: Main 8-bit SDR and Main 10 HDR PQ / HLG; VideoToolbox on macOS: 8-bit Main, SDR); no AV1 export yet.
- **Real-world media and platforms.** Our decoders are bit-exact on conformance streams, but camera and phone files in the wild are less tested. Windows and Linux get far less testing than macOS.
- **AI features.** Few so far; speech to text is optional and off by default.
@@ -327,6 +327,21 @@ Nothing below the front ends depends on a UI toolkit or OS API. `cargo xtask ci`
Installers and executables are code-signed.
+**If the app doesn't open on Windows:** the desktop app initializes only Vulkan and DirectX 12 by
+default, not OpenGL. Letting wgpu also create an OpenGL instance can crash some graphics drivers (AMD's
+`atio6axx.dll`) before the window appears, so the app would flash in Task Manager and quit.
+`WGPU_BACKEND` overrides the default for troubleshooting (for example `dx12` or `vulkan`). In
+PowerShell, from the folder containing the executable:
+
+```powershell
+$env:WGPU_BACKEND = "vulkan"
+& .\filmcraft.exe
+Remove-Item Env:WGPU_BACKEND # restore the default for later launches
+```
+
+An explicit `gl` override can bring the driver crash back on affected systems. The macOS, Linux
+and web backend defaults are unchanged.
+
### macOS
| Build | File | Notes |
@@ -342,7 +357,22 @@ Installers and executables are code-signed.
| Flatpak | `filmcraft--linux-x86_64.flatpak` | `filmcraft--linux-aarch64.flatpak` | Sandboxed; `flatpak install --user ` |
| Debian/Ubuntu | `filmcraft--linux-x86_64.deb` | `filmcraft--linux-aarch64.deb` | |
| Fedora/RHEL/openSUSE | `filmcraft--linux-x86_64.rpm` | `filmcraft--linux-aarch64.rpm` | |
-| Tarball | `filmcraft--linux-x86_64.tar.gz` | `filmcraft--linux-aarch64.tar.gz` | Unpack anywhere |
+| Tarball | `filmcraft--linux-x86_64.tar.gz` | `filmcraft--linux-aarch64.tar.gz` | Unpack and run `./install.sh` |
+
+The tarball installer places FilmCraft, its command-line tool and desktop integration in
+`~/.local`, without administrator permissions. For all users, run
+`sudo ./install.sh --prefix /usr/local` instead. Running the installer again updates the installation.
+
+**Gentoo (community-maintained):** the [::snakebyte overlay](https://github.com/switch87/snakebyte-overlay)
+packages the Linux release as `media-video/filmcraft-bin`. It is maintained by the community, not by the
+FilmCraft team, so report packaging problems to the overlay:
+
+```sh
+eselect repository add snakebyte git https://github.com/switch87/snakebyte-overlay.git
+emaint sync -r snakebyte
+echo 'media-video/filmcraft-bin ~amd64' >> /etc/portage/package.accept_keywords/filmcraft
+emerge --ask media-video/filmcraft-bin
+```
### FreeBSD
diff --git a/ROADMAP.md b/ROADMAP.md
index 29c0409fb..7a32f5498 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -35,8 +35,8 @@ before choosing work.
| **Feature breadth** (menus, commands, panels) | ~86–95% | **~85%** | Menu diff against Premiere 26.5; effect, transition and audio-effect catalogues complete by test. The long tail is mostly there. |
| **Correctness in depth** | not tracked | **~60–70%** | A first-time contributor found four real bugs in core paths within hours (#5–#8: AIFF import failed, Slide left linked audio behind, transitions didn't follow ripple trims, export ignored start/end times). User reports: Color Matte always grey with no picker (#29); ALSA audio error on Linux (#23). Settings are "most wired", not all. |
| **Codecs and media** | ~90% | **~75%** | Our decoders are bit-exact on conformance streams. Real camera and phone media (variable frame rate, damaged files, unusual containers) is far less tested. No camera RAW (RED, BRAW, ARRIRAW), no E-AC-3. |
-| **Export** | ~92% | **~70%** | Delivery: H.264 only (ProRes, DNxHR and image sequences cover mastering). No HEVC or AV1 export. AAF and OMF have never been validated in Avid Media Composer or Pro Tools. Export renders and encodes on the CPU only. |
-| **Performance and hardware** | ~62% | **~35–40%** | Premiere runs effects, decoding and encoding on the GPU and the hardware media engines. Here, hardware decode works on macOS (VideoToolbox, #33; active in the desktop app only since #41, so releases up to 0.2.1 decoded in software) and Windows (Media Foundation / Direct3D 11 DXVA, H.264, HEVC Main / Main 10, VP9 and AV1 4:2:0 8/10-bit, #30). The GPU does compositing, blend modes (#32) and 31 common effects (GPU2): colour adjustments, crop/flip/transform, blurs, sharpen. Lumetri, keys, masks, the remaining effects and export rendering are CPU; H.264 encoding can use VideoToolbox on macOS (opt-in per export, no B-frames) and NVENC on Windows with an NVIDIA GPU (opt-in per export), and H.265 (HEVC) export exists there only, through the same hardware encoder. Linux has no hardware path. 8K isn't real-time (AV1 is on Windows with a hardware decoder). The effect list shows Premiere's "GPU accelerated" badge, but only 31 effect ids run on the GPU here; the rest, Lumetri included, run on the CPU. |
+| **Export** | ~92% | **~70%** | Delivery: H.264, plus H.265 through a hardware encoder only (NVENC on Windows: Main 8-bit SDR and Main 10 HDR PQ / HLG; VideoToolbox on macOS: Main 8-bit SDR); ProRes, DNxHR and image sequences cover mastering. No AV1 export, no 10-bit / HDR HEVC on macOS. AAF and OMF have never been validated in Avid Media Composer or Pro Tools. Export renders and encodes on the CPU only. |
+| **Performance and hardware** | ~62% | **~35–40%** | Premiere runs effects, decoding and encoding on the GPU and the hardware media engines. Here, hardware decode works on macOS (VideoToolbox, #33; active in the desktop app only since #41, so releases up to 0.2.1 decoded in software) and Windows (Media Foundation / Direct3D 11 DXVA, H.264, HEVC Main / Main 10, VP9 and AV1 4:2:0 8/10-bit, #30), and for H.264 and HEVC on Linux (VA-API: 4K H.264 at 11 ms of CPU per frame instead of 417 ms, 4K HEVC at 10 ms instead of 203 ms). The GPU does compositing, blend modes (#32) and 31 common effects (GPU2): colour adjustments, crop/flip/transform, blurs, sharpen. Lumetri, keys, masks, the remaining effects and export rendering are CPU; H.264 encoding can use VideoToolbox on macOS (opt-in per export, no B-frames) and NVENC on Windows and Linux with an NVIDIA GPU (opt-in per export), and H.265 (HEVC) export exists on macOS and Windows only, through the same hardware encoders. Linux decodes H.264 and HEVC in hardware (VP9 and AV1 there are software); its only hardware encoder is NVENC H.264. 8K isn't real-time (AV1 is on Windows with a hardware decoder). The effect list shows Premiere's "GPU accelerated" badge, but only 31 effect ids run on the GPU here; the rest, Lumetri included, run on the CPU. |
| **Stability** | not tracked | **improving, unproven** | The never-crash pass (no panics in product code, last-resort guards) landed on 2026-10-04. There's no field record from real users' projects yet. |
| **Plugins and pro ecosystem** | not tracked | **~0–10%** | No audio plugin hosting (VST3 / Audio Units) and no third-party video effects (OpenFX). For many professional editors this alone rules FilmCraft out. Team Projects and Productions are out of scope by design. |
| **AI features** | partial | **~25–35%** | Speech to text exists but is off in default builds (the `whisper` feature). Enhance Speech is a DSP chain, not a model. Auto Reframe is approximate. No Generative Extend, and no media-intelligence search or auto colour. |
@@ -48,10 +48,10 @@ before choosing work.
In priority order. Agents should prefer this work over adding more checklist items.
1. **Hardware acceleration** (#30), the most visible gap to users:
- - hardware decode on Linux (VA-API);
+ - hardware decode on Linux (VA-API): H.264 and HEVC are in; VP9 and AV1 remain;
- zero-copy decoded frames into wgpu;
- the remaining effects on the GPU: Lumetri, keys, Vignette, Video Limiter, masks (31 common effects are done);
- - GPU export, then hardware encode: H.264 and H.265 on macOS are in (H.264 opt-in; H.265 has no other encoder), H.264 on NVENC (Windows, opt-in); HEVC on Windows and Linux remain.
+ - GPU export, then hardware encode: H.264 and H.265 on macOS are in (H.264 opt-in; H.265 has no other encoder), H.264 and H.265 (Main 8-bit, and Main 10 HDR PQ / HLG) on NVENC (Windows); Main 10 / HDR HEVC on VideoToolbox, Linux and other Windows vendors remain.
2. **A measured parity number.** Add `cargo xtask parity`, run in CI. It should cover menus,
commands, effects, transitions, panels, preferences and formats against the Premiere snapshot in
`plan/premiere/`. It should report presence and fidelity separately (exact, approximate, stub).
@@ -63,7 +63,7 @@ In priority order. Agents should prefer this work over adding more checklist ite
through XML / AAF.
5. **Windows and Linux at runtime.** CI that runs the test suite and a headless playback/export
smoke test on both, not just release builds. Fix the Linux audio path (#23).
-6. **Delivery codecs.** HEVC export is in on macOS (VideoToolbox, 8-bit, no software encoder); still to do: HEVC elsewhere, 10-bit / HDR HEVC, and AV1 (`rav1e`, BSD-2).
+6. **Delivery codecs.** HEVC export is in on macOS (VideoToolbox, 8-bit, no software encoder) and on Windows (NVENC, 8-bit and Main 10 HDR); still to do: HEVC elsewhere, 10-bit / HDR HEVC on macOS, and AV1 (`rav1e`, BSD-2).
7. **Plugin hosting** (VST3 / Audio Units, then OpenFX). Needs FFI, so it falls under the isolated
`unsafe` crate rule (AGENTS.md §0.3). **Owner decision needed** before starting.
8. **AI features** with openly licensed local models: transcription on by default, text-based
@@ -100,7 +100,7 @@ matched against `filmcraft-cli commands` and the UI command table. Effects: the
| Graphics and captions | 8% | text engine, shapes, per-character styles, our own graphics templates (.fcgt, 8 built-ins, export/install/edit), rolls/crawls, responsive pins + time, captions SRT/VTT/SCC/MCC/STL/TTML/DFXP, transcripts | ~85% |
| Export | 8% | own H.264/AAC, ProRes, DNxHR, MXF OP1a/OP-Atom, image sequences, GIF, WAV, 5.1; preset library + user presets, full Export-mode settings, queue, Quick Export; AAF (Edit Protocol) import/export, OMF 2.0 export; no AAF/OMF validation against Avid/Pro Tools yet | ~92% |
| 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; 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`; blend modes on the GPU (#32); 8K and AV1 not yet real-time on a loaded machine; no hardware path on Linux, and on Windows only decode and opt-in NVENC H.264 export (see the honest assessment: ~35–40% against Premiere) | ~62% |
+| 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`; blend modes on the GPU (#32); 8K and AV1 not yet real-time on a loaded machine; H.264 and HEVC decode in hardware on Linux (VA-API), and on Windows only decode and opt-in NVENC H.264 export (see the honest assessment: ~35–40% against Premiere) | ~62% |
| **Weighted total** | | | **~87%** |
## Estimate to parity
@@ -154,11 +154,17 @@ Status: ✅ done · 🟡 in progress · ⬜ not started. Estimates are remaining
## Running now
-- **Hardware acceleration (#30):** landed: blend modes on the GPU (#32); VideoToolbox hardware decode + the `platform` FFI crate (#33); 31 common effects on the GPU (GPU2). Windows: Media Foundation / Direct3D 11 H.264 + HEVC decode (HW2) and VP9 + AV1 (HW3), NVENC H.264 export (HW4, opt-in). Next: VA-API (Linux) decode, zero-copy upload, Lumetri on the GPU.
+- **Hardware acceleration (#30):** landed: blend modes on the GPU (#32); VideoToolbox hardware decode + the `platform` FFI crate (#33); 31 common effects on the GPU (GPU2). Windows: Media Foundation / Direct3D 11 H.264 + HEVC decode (HW2) and VP9 + AV1 (HW3), NVENC H.264 export (HW4, opt-in) and H.265 export. Linux: VA-API H.264 + HEVC decode (HW5). Next: VA-API VP9 / AV1, zero-copy upload, Lumetri on the GPU.
- **Next:** [Where we are lacking](#where-we-are-lacking) items 1–5. GPU export waits for the export-crate work (#19 and the never-crash follow-up) to land.
## Log
+- **2026-10-08 (Source range dragging):** implicit full-clip In/Out, video-only/audio-only/linked both controls, picture/waveform drag gestures and one-command undoable timeline placement. Shared Source/Program draggable frame-bounded handles and range translation, cancellation, Source marker/navigation routing and marked-range playback/looping.
+- **2026-10-08 (Source playback):** normal forward playback for video/audio media and subclips in the Source monitor, an independent Source clock, Play/Pause and focused Space, frame stepping, device-clock audio with wall-clock fallback, seeking, and end-of-clip stopping. Windows desktop review verified a real video file and standalone WAV, including In/Out marking. Reverse/shuttle Source playback and source sequences remain unsupported; no parity percentage changed.
+- **2026-10-08 (night):** HDR H.265 (HEVC Main 10, PQ and HLG, BT.2020, limited range) export on Windows through NVENC (#30): an HDR sequence now exports as real 10-bit HDR (`rgbf_to_yuv420_10` into P010 input buffers; VUI BT.2020 + PQ / HLG; the HDR10 mastering-display and content-light messages as SEI on every IDR for PQ, through the generic SEI payload array because NVENC 12.1 has no mastering-display field; `colr` / `mdcv` / `clli` in the sample entry), where the GPU reports 10-bit HEVC (new `register_hdr_probe` / `hdr_available` in the export crate; elsewhere, and on macOS, HDR sequences still tone-map to 8-bit SDR HEVC, and `settings.sdr` forces that). ffprobe reads `hevc` / `Main 10` / `hvc1` / `yuv420p10le` / `bt2020` / `smpte2084` or `arib-std-b67` / `bt2020nc` / `tv` with the mastering-display and content-light side data for PQ; ffmpeg's decode equals ours code for code; the in-process round trip is at 71.1 dB luma PSNR on the 10-bit scale with 803 distinct levels on a ramp and the peak code 940 intact (RTX 5060). 8-bit SDR H.265 is unchanged.
+- **2026-10-08:** H.265 (HEVC Main, 8-bit 4:2:0, SDR) export on Windows through NVENC (#30): the NVENC session, ring and NV12 path of the H.264 encoder now serve both codecs (the H.264 export is byte-for-byte unchanged); the format is available where the GPU has an HEVC encoder, choosing it is the opt-in, and `hvcC` is built from the SPS the encoder wrote. ffprobe reads `hevc` / `Main` / `hvc1` / `yuv420p` / BT.709; our own HEVC decoder decodes it at 48.9 dB luma PSNR (RTX 5060). Main 10 and HDR remain.
+- **2026-10-08 (#285):** `filmcraft-cli` no longer panics with "Broken pipe" when its stdout is closed early (`filmcraft-cli commands | head`): later output is dropped, the rest of the work (script lines, `--save`, `--save-as`) still runs, and the exit status still reports failures. Other stdout write errors are reported and make the exit status 1. No change to the checklist or estimates.
+
- **2026-10-06:** 21 community PRs landed (#19, #28, #38, #39, #41–#60). Highlights: hardware decoding is now actually on in the desktop app (#41: `register()` sat inside a `log::info!` that never ran, so #33's VideoToolbox path only worked in the CLI and benchmarks); decode memory on long timelines cut sharply (frame pool, shared GOP-cache budget, positional file reads); exports are byte-identical across machines (#19); crash fix for odd track names over MCP (#44); ripple trim, ripple delete and speed changes move split-edit clips once (#56–#58); Japanese interface using an installed system font (#38; no fonts bundled); MCP annotations, resources, export progress and cancellation (#28).
- **2026-10-05:** honest assessment added (checklist ~87% vs ready for real work ~50–60%). Hardware acceleration started (#30): blend modes on the GPU (#32); VideoToolbox decode + `platform` crate, the one crate allowed `unsafe` (#33). Community fixes landed: AIFF import, export start/end ranges, transitions following ripple trims, Slide moving linked audio (#5–#8).
@@ -205,4 +211,4 @@ Status: ✅ done · 🟡 in progress · ⬜ not started. Estimates are remaining
### Interface localization
-Edit > Language switches English/Japanese/Spanish/Portuguese (Brazilian) and persists in UI state. Commands `app.language.english`, `app.language.japanese`, `app.language.spanish` and `app.language.portuguese` are reachable through the control channel. Core native/in-window menu labels are translated; untranslated labels use English. Japanese text uses the Japanese fonts from [craft-fonts](https://github.com/storytold/craft-fonts) when FilmCraft is built with them (`CRAFT_FONTS_DIR`; all official releases, including the web build), otherwise a Japanese font already installed on the system; with neither, switching to Japanese is refused with a message. Dialog/panel translation coverage remains open. Existing vertical text support is preserved.
+Edit > Language switches English/Japanese/Spanish/Portuguese (Brazilian)/Ukrainian and persists across restarts through `general.interfaceLanguage` (also available in Settings > General). Until a language is chosen, System Language (the default) follows the operating system's or browser's preferred languages and falls back to English. Commands `app.language.english`, `app.language.japanese`, `app.language.spanish`, `app.language.portuguese` and `app.language.ukrainian` are reachable through the control channel. Translations live in one catalog per language (`crates/ui-egui/src/i18n/.tsv`); UI code wraps its strings in `tl!("…")` / `tlf!("…{name}", name)`, and names that come from registries (effects and their parameters, settings, commands, panels, workspaces, presets) are translated where they are drawn. **Spanish and Japanese cover the whole interface**: menus, panels, dialogs, the Settings pages, the Effects/Effect Controls registry, the Keyboard Shortcuts dialog and status messages; tests fail when a `tl!` literal, a menu label or a registry name has no Spanish entry (`ja.tsv` has the same entries, but the tests do not require them yet). Searches match the displayed labels as well as English sources. Brazilian Portuguese and Ukrainian cover the core menus; untranslated labels use English. Ukrainian uses the bundled Cyrillic interface fonts. Engine error messages, the control channel, the CLI and MCP stay in English. Japanese text uses the Japanese fonts from [craft-fonts](https://github.com/storytold/craft-fonts) when FilmCraft is built with them (`CRAFT_FONTS_DIR`; all official releases, including the web build), otherwise a Japanese font already installed on the system; with neither, switching to Japanese is refused with a message. Existing vertical text support is preserved.
diff --git a/apps/filmcraft-cli/src/main.rs b/apps/filmcraft-cli/src/main.rs
index 13bebb85d..be104b30c 100644
--- a/apps/filmcraft-cli/src/main.rs
+++ b/apps/filmcraft-cli/src/main.rs
@@ -4,6 +4,32 @@
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]
+/// `println!` that doesn't panic when stdout is closed (`filmcraft-cli commands | head`) with
+/// "failed printing to stdout: Broken pipe (os error 32)". Once stdout has failed, later output is
+/// dropped and the work goes on: see `stdout_failed`.
+macro_rules! outln {
+ ($($arg:tt)*) => {{
+ use std::io::Write as _;
+ if !$crate::stdout_gone() {
+ if let Err(e) = writeln!(std::io::stdout(), $($arg)*) {
+ $crate::stdout_failed(e);
+ }
+ }
+ }};
+}
+
+/// `print!` counterpart of `outln!`.
+macro_rules! out {
+ ($($arg:tt)*) => {{
+ use std::io::Write as _;
+ if !$crate::stdout_gone() {
+ if let Err(e) = write!(std::io::stdout(), $($arg)*) {
+ $crate::stdout_failed(e);
+ }
+ }
+ }};
+}
+
mod args;
mod probe;
@@ -11,6 +37,28 @@ use args::{Args, parse_value};
use filmcraft_automation::BridgeClient;
use filmcraft_engine::Session;
use serde_json::{Value, json};
+use std::sync::atomic::{AtomicBool, Ordering};
+
+/// Set once a write to stdout has failed; later output is dropped.
+static STDOUT_GONE: AtomicBool = AtomicBool::new(false);
+/// Set when stdout failed for a reason other than a closed reader: the exit status becomes 1.
+static STDOUT_ERROR: AtomicBool = AtomicBool::new(false);
+
+fn stdout_gone() -> bool {
+ STDOUT_GONE.load(Ordering::Relaxed)
+}
+
+/// stdout went away. The program doesn't stop there: the rest of `exec`/`run`/`import` (later
+/// script lines, `--save`, `--save-as`) still runs and the exit status still tells how it went, so
+/// `exec … --save | head -1` keeps the edit. A reader that stopped early (a closed pipe) is not an
+/// error, as in ripgrep; any other write error is reported and makes the exit status 1.
+fn stdout_failed(e: std::io::Error) {
+ if STDOUT_GONE.swap(true, Ordering::Relaxed) || e.kind() == std::io::ErrorKind::BrokenPipe {
+ return;
+ }
+ diag(format_args!("filmcraft-cli: can't write to stdout: {e}"));
+ STDOUT_ERROR.store(true, Ordering::Relaxed);
+}
const HELP: &str = "\
filmcraft-cli: drive FilmCraft from the shell (headless engine, or the live app with --bridge)
@@ -67,13 +115,23 @@ EXIT STATUS
0 success · 1 a command failed · 2 usage error
";
+/// Writes one diagnostic line to `w`, dropping any write error. `eprintln!` panics (exit 101) when stderr's
+/// reader is gone, which would turn a usage error, a failure or a finished export into a crash.
+fn write_diag(w: &mut dyn std::io::Write, msg: std::fmt::Arguments) {
+ let _ = writeln!(w, "{msg}");
+}
+
+fn diag(msg: std::fmt::Arguments) {
+ write_diag(&mut std::io::stderr(), msg);
+}
+
fn usage(msg: impl std::fmt::Display) -> ! {
- eprintln!("filmcraft-cli: {msg}\nRun `filmcraft-cli help` for usage.");
+ diag(format_args!("filmcraft-cli: {msg}\nRun `filmcraft-cli help` for usage."));
std::process::exit(2)
}
fn fail(msg: impl std::fmt::Display) -> ! {
- eprintln!("filmcraft-cli: {msg}");
+ diag(format_args!("filmcraft-cli: {msg}"));
std::process::exit(1)
}
@@ -134,7 +192,7 @@ impl Backend {
fn print(a: &Args, v: &Value) {
let s = if a.flag("--compact") { serde_json::to_string(v) } else { serde_json::to_string_pretty(v) };
- println!("{}", s.unwrap_or_default());
+ outln!("{}", s.unwrap_or_default());
}
/// Export format from the output file's extension.
@@ -162,16 +220,23 @@ fn register_hardware_decoders() -> filmcraft_platform::Availability {
#[tokio::main]
async fn main() {
+ cli().await;
+ if STDOUT_ERROR.load(Ordering::Relaxed) {
+ std::process::exit(1);
+ }
+}
+
+async fn cli() {
if matches!(std::env::args().nth(1).as_deref(), Some("--version" | "-V")) {
- println!("filmcraft-cli {}", env!("CARGO_PKG_VERSION"));
+ outln!("filmcraft-cli {}", env!("CARGO_PKG_VERSION"));
return;
}
register_hardware_decoders();
let a = Args::parse(std::env::args().skip(1));
let Some(cmd) = a.pos(0) else { usage("missing subcommand") };
match cmd {
- "help" | "--help" | "-h" => print!("{HELP}"),
- "version" => println!("filmcraft-cli {}", env!("CARGO_PKG_VERSION")),
+ "help" | "--help" | "-h" => out!("{HELP}"),
+ "version" => outln!("filmcraft-cli {}", env!("CARGO_PKG_VERSION")),
"probe" => {
let path = a.pos(1).unwrap_or_else(|| usage("probe [--image-sequence]"));
match probe::probe(path, a.flag("--image-sequence")) {
@@ -189,11 +254,11 @@ async fn main() {
src.video_frame(filmcraft_media::FrameRequest::full(rate.tick_of(f))).unwrap_or_else(|e| fail(e));
}
let dt = t0.elapsed().as_secs_f64();
- println!("{n} frames in {dt:.2}s → {:.1} fps (sequential, via Mp4Source)", n as f64 / dt);
+ outln!("{n} frames in {dt:.2}s → {:.1} fps (sequential, via Mp4Source)", n as f64 / dt);
let t1 = std::time::Instant::now();
let f = src.video_frame(filmcraft_media::FrameRequest::full(rate.tick_of(n / 2))).unwrap_or_else(|e| fail(e));
let (w, h, _) = f.to_linear_f32_decimated(2);
- println!("½-res linear conversion {w}x{h}: {:.1} ms", t1.elapsed().as_secs_f64() * 1000.0);
+ outln!("½-res linear conversion {w}x{h}: {:.1} ms", t1.elapsed().as_secs_f64() * 1000.0);
}
"commands" | "describe" => {
let mut b = Backend::open(&a);
@@ -219,7 +284,7 @@ async fn main() {
} else {
for c in &list {
let s = |k: &str| c[k].as_str().unwrap_or("").to_string();
- println!("{:<32} {:<34} {:<14} {}", s("id"), s("label"), s("shortcut"), s("params"));
+ outln!("{:<32} {:<34} {:<14} {}", s("id"), s("label"), s("shortcut"), s("params"));
}
}
}
@@ -246,9 +311,9 @@ async fn main() {
let v: Value = serde_json::from_str(line).unwrap_or_else(|e| usage(format!("line {}: {e}", n + 1)));
let id = v["id"].as_str().unwrap_or_else(|| usage(format!("line {}: missing \"id\"", n + 1)));
match b.exec(id, v.get("params").cloned().unwrap_or(json!({}))).await {
- Ok(r) => println!("{}", json!({"line": n + 1, "id": id, "ok": true, "result": r})),
+ Ok(r) => outln!("{}", json!({"line": n + 1, "id": id, "ok": true, "result": r})),
Err(e) => {
- println!("{}", json!({"line": n + 1, "id": id, "ok": false, "error": e}));
+ outln!("{}", json!({"line": n + 1, "id": id, "ok": false, "error": e}));
failed += 1;
if !a.flag("--keep-going") {
std::process::exit(1);
@@ -318,11 +383,7 @@ async fn main() {
if let Some(r) = a.opt("--range") {
p["range"] = json!(r);
}
- if let (Some(s0), Some(s1)) = (a.opt("--start"), a.opt("--end")) {
- p["range"] = json!("custom");
- p["startSeconds"] = parse_value(s0);
- p["endSeconds"] = parse_value(s1);
- }
+ set_custom_bounds(&mut p, a.opt("--start"), a.opt("--end"));
if let Some(js) = a.opt("--settings") {
p["settings"] = serde_json::from_str(js).unwrap_or_else(|e| usage(format!("--settings: {e}")));
}
@@ -344,7 +405,7 @@ async fn main() {
match r {
Ok(v) => {
print(&a, &v);
- eprintln!("exported {out} in {:.1}s", t0.elapsed().as_secs_f64());
+ diag(format_args!("exported {} in {:.1}s", written_paths(&v, out).join(", "), t0.elapsed().as_secs_f64()));
}
Err(e) => fail(format!("export: {e}")),
}
@@ -359,11 +420,11 @@ async fn main() {
let out = a.opt("--out").unwrap_or("frame.png");
s.set_playhead(filmcraft_time::Tick::from_seconds_f64(secs));
let t0 = std::time::Instant::now();
- let Some(img) = s.render_program(scale) else { fail("no sequence") };
+ let img = render_at_playhead(&s, scale).unwrap_or_else(|e| fail(e));
let dt = t0.elapsed();
let png = filmcraft_automation::png_rgba(img.w as u32, img.h as u32, img.over_black_rgba8(), 0).unwrap_or_else(|e| fail(e));
std::fs::write(out, png).unwrap_or_else(|e| fail(format!("{out}: {e}")));
- eprintln!("rendered {}x{} in {:.1} ms → {out}", img.w, img.h, dt.as_secs_f64() * 1000.0);
+ diag(format_args!("rendered {}x{} in {:.1} ms → {out}", img.w, img.h, dt.as_secs_f64() * 1000.0));
}
"mcp" => {
let server = match a.opt("--bridge") {
@@ -381,14 +442,112 @@ async fn main() {
}
}
+/// The files an export wrote, from its result: `path` of a direct export, or each queued item's
+/// `path` (the format may have changed the extension: `out.mp4` exported as APV is `out.mp4.mov`).
+/// `requested` when the result names none.
+fn written_paths(result: &Value, requested: &str) -> Vec {
+ let paths: Vec = match result.get("path").and_then(Value::as_str) {
+ Some(p) => vec![p.to_string()],
+ None => result
+ .get("items")
+ .and_then(Value::as_array)
+ .into_iter()
+ .flatten()
+ .filter_map(|i| i.get("path").and_then(Value::as_str))
+ .map(str::to_string)
+ .collect(),
+ };
+ if paths.is_empty() { vec![requested.to_string()] } else { paths }
+}
+
+/// The active sequence at the playhead, or the reason it cannot be rendered (no sequence, bad scale).
+fn render_at_playhead(s: &Session, scale: f32) -> Result {
+ s.try_render_program_at(scale, s.playhead()).map_err(|e| e.to_string())
+}
+
+/// `--start` / `--end` ask for a custom range. A lone bound is forwarded too, so the engine reports the missing one
+/// instead of the export silently running over the whole sequence.
+fn set_custom_bounds(p: &mut Value, start: Option<&str>, end: Option<&str>) {
+ if start.is_none() && end.is_none() {
+ return;
+ }
+ p["range"] = json!("custom");
+ if let Some(s0) = start {
+ p["startSeconds"] = parse_value(s0);
+ }
+ if let Some(s1) = end {
+ p["endSeconds"] = parse_value(s1);
+ }
+}
+
#[cfg(test)]
mod format_tests {
+ #[test]
+ fn render_reports_the_real_reason() {
+ let mut s = filmcraft_engine::Session::default();
+ assert!(super::render_at_playhead(&s, 0.5).unwrap_err().contains("sequence"));
+ s.execute("file.newSequence", serde_json::json!({"width": 16, "height": 16})).unwrap();
+ for scale in [0.0, -1.0, f32::NAN] {
+ let e = super::render_at_playhead(&s, scale).unwrap_err();
+ assert!(e.contains("finite and positive"), "{e}");
+ }
+ assert_eq!(super::render_at_playhead(&s, 0.5).unwrap().w, 8);
+ }
+
/// The CLI (and MCP / headless runs, which share its entry point) registers the hardware
/// decoders at start-up.
#[test]
fn startup_registers_the_hardware_decoders() {
let hardware = super::register_hardware_decoders();
- assert_eq!(filmcraft_platform::registered(), cfg!(any(target_os = "macos", target_os = "windows")), "{hardware:?}");
+ // always on macOS and Windows; on Linux when a VA-API driver is there
+ let expected = cfg!(any(target_os = "macos", target_os = "windows"))
+ || (cfg!(target_os = "linux") && matches!(hardware, filmcraft_platform::Availability::Available(_)));
+ assert_eq!(filmcraft_platform::registered(), expected, "{hardware:?}");
+ }
+
+ #[test]
+ fn a_failing_diagnostic_stream_does_not_panic() {
+ struct Closed;
+ impl std::io::Write for Closed {
+ fn write(&mut self, _: &[u8]) -> std::io::Result {
+ Err(std::io::ErrorKind::BrokenPipe.into())
+ }
+ fn flush(&mut self) -> std::io::Result<()> {
+ Err(std::io::ErrorKind::BrokenPipe.into())
+ }
+ }
+ super::write_diag(&mut Closed, format_args!("exported {}", "a.wav"));
+ let mut ok = Vec::new();
+ super::write_diag(&mut ok, format_args!("hi"));
+ assert_eq!(ok, b"hi\n");
+ }
+
+ #[test]
+ fn export_message_names_the_written_files() {
+ use serde_json::json;
+ let direct = json!({"job": 1, "path": "out-apv.mp4.mov", "result": {"path": "out-apv.mp4.mov"}});
+ assert_eq!(super::written_paths(&direct, "out-apv.mp4"), ["out-apv.mp4.mov"]);
+ let queued = json!({"added": [1, 2], "items": [{"path": "a.mp4.mov"}, {"path": "b.wav"}]});
+ assert_eq!(super::written_paths(&queued, "a.mp4"), ["a.mp4.mov", "b.wav"]);
+ assert_eq!(super::written_paths(&json!({}), "x.mov"), ["x.mov"]);
+ assert_eq!(super::written_paths(&json!({"items": []}), "x.mov"), ["x.mov"]);
+ }
+
+ #[test]
+ fn export_lone_bound_is_forwarded_as_custom_range() {
+ use serde_json::json;
+ let mut p = json!({});
+ super::set_custom_bounds(&mut p, Some("0.25"), None);
+ assert_eq!(p["range"], "custom");
+ assert_eq!(p["startSeconds"], 0.25);
+ assert!(p.get("endSeconds").is_none());
+ let mut p = json!({});
+ super::set_custom_bounds(&mut p, None, Some("0.25"));
+ assert_eq!(p["range"], "custom");
+ assert_eq!(p["endSeconds"], 0.25);
+ let mut p = json!({});
+ super::set_custom_bounds(&mut p, None, None);
+ assert!(p.get("range").is_none());
}
#[test]
diff --git a/apps/filmcraft-cli/tests/cli.rs b/apps/filmcraft-cli/tests/cli.rs
index b7fedfc2c..c3d3c979f 100644
--- a/apps/filmcraft-cli/tests/cli.rs
+++ b/apps/filmcraft-cli/tests/cli.rs
@@ -44,7 +44,7 @@ fn probe_reports_mpeg_transport_and_program_streams() {
assert!(st.success());
let v = json_out(&cli(&["probe", out.to_str().unwrap()]));
assert!(v["video"]["codec"].as_str().unwrap().starts_with("MPEG-2 Video"), "{v}");
- assert_eq!(v["audio"]["codec"], "MPEG Audio");
+ assert_eq!(v["audio_streams"][0]["codec"], "MPEG Audio");
let streams = v["mpeg"]["streams"].as_array().unwrap();
assert_eq!(streams.len(), 2, "{v}");
assert_eq!(streams[0]["codec"], "MPEG-2 Video");
@@ -179,9 +179,89 @@ fn export_with_presets() {
let v = json_out(&cli(&["--demo", "--data-dir", data_s, "export", q.to_str().unwrap(), "--preset", "Tiny WAV", "--queue", "--range", "entire"]));
assert_eq!(v["items"][0]["status"], "done", "{v}");
assert!(q.exists());
+ // a format that changes the extension: the completion message names the file written (#341)
+ let apv = dir.join("apv.mp4");
+ let o =
+ cli(&["--demo", "--data-dir", data_s, "export", apv.to_str().unwrap(), "--preset", "APV 422-10", "--start", "0", "--end", "0.1", "--scale", "0.125"]);
+ let v = json_out(&o);
+ let written = v["path"].as_str().unwrap().to_string();
+ assert!(written.ends_with("apv.mp4.mov") && std::path::Path::new(&written).exists(), "{v}");
+ let msg = String::from_utf8_lossy(&o.stderr);
+ assert!(msg.contains(&format!("exported {written} in ")), "{msg}");
+ let qa = dir.join("queued-apv.mp4");
+ let o = cli(&[
+ "--demo",
+ "--data-dir",
+ data_s,
+ "export",
+ qa.to_str().unwrap(),
+ "--preset",
+ "APV 422-10",
+ "--queue",
+ "--start",
+ "0",
+ "--end",
+ "0.1",
+ "--scale",
+ "0.125",
+ ]);
+ let v = json_out(&o);
+ let written = v["items"][0]["path"].as_str().unwrap().to_string();
+ assert!(written.ends_with("queued-apv.mp4.mov"), "{v}");
+ assert!(String::from_utf8_lossy(&o.stderr).contains(&format!("exported {written} in ")), "{}", String::from_utf8_lossy(&o.stderr));
// unknown preset: exit status 1 and a message naming it
let bad = cli(&["--demo", "--data-dir", data_s, "export", dir.join("x.mp4").to_str().unwrap(), "--preset", "No Such Preset"]);
assert_eq!(bad.status.code(), Some(1));
assert!(String::from_utf8_lossy(&bad.stderr).contains("No Such Preset"));
let _ = std::fs::remove_dir_all(&dir);
}
+
+/// `filmcraft-cli commands | head -c 10` panicked with "failed printing to stdout: Broken pipe
+/// (os error 32)" and exit status 101 (#285).
+#[test]
+fn closed_stdout_ends_quietly() {
+ use std::io::Read;
+ for args in [&["commands"][..], &["help"], &["--demo", "commands", "--json"]] {
+ let mut child = Command::new(env!("CARGO_BIN_EXE_filmcraft-cli")).args(args).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap();
+ let mut first = [0u8; 10];
+ child.stdout.as_mut().unwrap().read_exact(&mut first).unwrap();
+ drop(child.stdout.take());
+ let out = child.wait_with_output().unwrap();
+ let err = String::from_utf8_lossy(&out.stderr);
+ assert!(out.status.success() && err.is_empty(), "{args:?}: {:?}: {err}", out.status);
+ }
+}
+
+/// `run -` with stdout already closed: `run` reads its script to the end before it prints, so its
+/// first write fails for sure. The rest of the script and the save still happen, and the exit status
+/// still reports a failing command.
+fn run_with_closed_stdout(args: &[&str], script: &[u8]) -> Output {
+ let mut child =
+ Command::new(env!("CARGO_BIN_EXE_filmcraft-cli")).args(args).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap();
+ drop(child.stdout.take());
+ child.stdin.take().unwrap().write_all(script).unwrap();
+ child.wait_with_output().unwrap()
+}
+
+#[test]
+fn closed_stdout_still_runs_the_script_and_saves() {
+ let dir = std::env::temp_dir().join(format!("fc-cli-pipe-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let proj = dir.join("p.fcproj");
+ let p = proj.to_str().unwrap();
+ let script = b"{\"id\":\"file.newBin\",\"params\":{\"name\":\"Piped One\"}}\n{\"id\":\"file.newBin\",\"params\":{\"name\":\"Piped Two\"}}\n";
+ let out = run_with_closed_stdout(&["--demo", "--save-as", p, "run", "-"], script);
+ assert!(out.status.success(), "{:?}: {}", out.status, String::from_utf8_lossy(&out.stderr));
+ let tree = json_out(&cli(&["--project", p, "inspect", "project"])).to_string();
+ assert!(tree.contains("Piped One") && tree.contains("Piped Two"), "{tree}");
+
+ // a failing command is still a failure, whether or not anyone reads stdout
+ let out = run_with_closed_stdout(&["--demo", "run", "-"], b"{\"id\":\"no.such.command\"}\n");
+ assert_eq!(out.status.code(), Some(1));
+ let out = run_with_closed_stdout(
+ &["--demo", "--keep-going", "run", "-"],
+ b"{\"id\":\"no.such.command\"}\n{\"id\":\"file.newBin\",\"params\":{\"name\":\"x\"}}\n",
+ );
+ assert_eq!(out.status.code(), Some(1));
+ let _ = std::fs::remove_dir_all(&dir);
+}
diff --git a/apps/filmcraft-web/src/fs.rs b/apps/filmcraft-web/src/fs.rs
index b959bdcb7..adf417ca7 100644
--- a/apps/filmcraft-web/src/fs.rs
+++ b/apps/filmcraft-web/src/fs.rs
@@ -70,9 +70,8 @@ fn file_name(path: &str) -> &str {
path.rsplit('/').next().unwrap_or(path)
}
-/// Register a browser `File` (or `Blob` with a name) and return its virtual path. A file with
-/// the same name and size as an existing entry replaces it (the same file picked again keeps
-/// its path, so projects referencing it come back online); otherwise a numbered name is used.
+/// Register a browser `File` (or named `Blob`) at an unused virtual path; collisions get numbered names.
+/// Name + size do not identify content: replacing an occupied path changes every project item referencing it.
pub fn register_blob(name: &str, blob: web_sys::Blob) -> String {
let size = blob.size() as u64;
let clean: String = name.chars().map(|c| if c == '/' || c == '\\' { '_' } else { c }).collect();
@@ -85,18 +84,11 @@ pub fn register_blob(name: &str, blob: web_sys::Blob) -> String {
let mut n = 1;
let path = loop {
let p = if n == 1 { format!("/files/{clean}") } else { format!("/files/{stem} ({n}){ext}") };
- match fs.entries.get(&p) {
- None => break p,
- Some(Entry::Blob { size: s, .. }) if *s == size => break p,
- Some(Entry::Mem(b)) if b.len() as u64 == size => break p,
- _ => n += 1,
+ if !fs.entries.contains_key(&p) {
+ break p;
}
+ n += 1;
};
- if let Some(Entry::Blob { id, .. }) = fs.entries.get(&path) {
- let id = *id;
- fs.blobs.remove(&id);
- fs.chunks.retain(|k, _| k.0 != id);
- }
let id = fs.next_id;
fs.next_id += 1;
fs.blobs.insert(id, blob);
diff --git a/apps/filmcraft-web/src/import.rs b/apps/filmcraft-web/src/import.rs
index e837a3710..6b41a2cdd 100644
--- a/apps/filmcraft-web/src/import.rs
+++ b/apps/filmcraft-web/src/import.rs
@@ -81,12 +81,12 @@ async fn prewarm(path: &str, size: u64) {
}
/// `file.import` one path, retried while its bytes are loading.
-async fn import_path(path: String) -> Result {
+async fn import_path(path: String, bin: u64) -> Result {
for _ in 0..2000 {
let p = path.clone();
let (r, pending) = on_ui(move |w, _| {
let _ = filmcraft_media::pending::take();
- let r = w.app.session.execute("file.import", json!({"paths": [p]})).map_err(|e| e.to_string());
+ let r = w.app.session.execute("file.import", json!({"paths": [p], "bin": bin})).map_err(|e| e.to_string());
(r, filmcraft_media::pending::take())
})
.await;
@@ -105,6 +105,8 @@ async fn import_path(path: String) -> Result {
/// Import browser files (no copies): returns `{items, errors, paths}`.
pub async fn import_files(files: Vec) -> Value {
+ // Capture once per batch: navigation while prewarming must not redirect pending imports.
+ let bin = on_ui(|w, _| w.app.import_bin().0).await;
let mut items = Vec::new();
let mut errors = Vec::new();
let mut paths = Vec::new();
@@ -113,7 +115,7 @@ pub async fn import_files(files: Vec) -> Value {
let size = f.size() as u64;
let path = fs::register_blob(&name, f.clone().into());
prewarm(&path, size).await;
- match import_path(path.clone()).await {
+ match import_path(path.clone(), bin).await {
Ok(v) => {
items.extend(v.get("items").and_then(Value::as_array).cloned().unwrap_or_default());
errors.extend(v.get("errors").and_then(Value::as_array).cloned().unwrap_or_default());
diff --git a/apps/filmcraft-web/src/lib.rs b/apps/filmcraft-web/src/lib.rs
index 469d2107a..d769ea4be 100644
--- a/apps/filmcraft-web/src/lib.rs
+++ b/apps/filmcraft-web/src/lib.rs
@@ -24,6 +24,7 @@ pub mod fs;
pub mod import;
pub mod opfs;
pub mod recovery;
+pub mod recovery_policy;
pub mod webcodecs;
use std::cell::RefCell;
@@ -58,6 +59,13 @@ pub fn repaint() {
});
}
+/// Give a host that builds its own [`WebApp`] the egui context that [`post`] and [`repaint`] wake.
+/// [`start`] calls it; an embedding app that runs `WebApp` in its own eframe runner calls it once
+/// from its app creator, or posted actions and JS API requests wait for the next input event.
+pub fn set_context(ctx: egui::Context) {
+ CTX.with(|c| *c.borrow_mut() = Some(ctx));
+}
+
/// Environment facts reported by `filmcraft.info()` (backend, isolation, codecs…).
pub fn set_info(key: &str, v: serde_json::Value) {
INFO.with(|i| {
@@ -150,7 +158,7 @@ pub async fn start(canvas_id: String) -> Result<(), JsValue> {
canvas,
web_options,
Box::new(move |cc| {
- CTX.with(|c| *c.borrow_mut() = Some(cc.egui_ctx.clone()));
+ set_context(cc.egui_ctx.clone());
let ctx = cc.egui_ctx.clone();
fs::set_on_data(move || ctx.request_repaint());
fs::set_on_write(|path, data| {
@@ -177,6 +185,9 @@ pub async fn start(canvas_id: String) -> Result<(), JsValue> {
}
let mut app = FilmcraftApp::new(session);
import::install_hooks(&mut app);
+ // Settings ▸ General ▸ Interface Language ▸ System Language (#218): the browser's languages.
+ app.hooks.system_languages =
+ Some(Box::new(|| web_sys::window().map(|w| w.navigator().languages().iter().filter_map(|v| v.as_string()).collect()).unwrap_or_default()));
let (tx, rx) = std::sync::mpsc::channel();
api::set_sender(tx);
app = app.with_control(rx);
diff --git a/apps/filmcraft-web/src/opfs.rs b/apps/filmcraft-web/src/opfs.rs
index b19071082..a246a52d2 100644
--- a/apps/filmcraft-web/src/opfs.rs
+++ b/apps/filmcraft-web/src/opfs.rs
@@ -10,8 +10,13 @@ use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use web_sys::{FileSystemDirectoryHandle, FileSystemFileHandle};
+/// Called with whether a queued write reached OPFS.
+pub type Done = Box;
+
thread_local! {
- static QUEUE: RefCell<(BTreeMap>, bool)> = const { RefCell::new((BTreeMap::new(), false)) };
+ /// Pending writes per path (bytes and the callbacks of every write coalesced into them), and
+ /// whether the writer task is running.
+ static QUEUE: RefCell<(BTreeMap, Vec)>, bool)> = const { RefCell::new((BTreeMap::new(), false)) };
}
/// Whether this browser has OPFS (`navigator.storage.getDirectory`).
@@ -60,9 +65,17 @@ async fn write_now(rel: &str, data: &[u8]) -> Result<(), JsValue> {
/// Queue a write of `data` to `rel` (relative to the OPFS root).
pub fn write(rel: String, data: Vec) {
+ write_then(rel, data, None);
+}
+
+/// [`write`], then call `done` with whether it succeeded. A write replaced by a newer one to the
+/// same path reports the newer one's result.
+pub fn write_then(rel: String, data: Vec, done: Option) {
let start = QUEUE.with(|q| {
let mut q = q.borrow_mut();
- q.0.insert(rel, data);
+ let e = q.0.entry(rel).or_default();
+ e.0 = data;
+ e.1.extend(done);
!std::mem::replace(&mut q.1, true)
});
if !start {
@@ -81,10 +94,14 @@ pub fn write(rel: String, data: Vec) {
}
}
});
- let Some((rel, data)) = next else { break };
- if let Err(e) = write_now(&rel, &data).await {
+ let Some((rel, (data, done))) = next else { break };
+ let r = write_now(&rel, &data).await;
+ if let Err(e) = &r {
log::warn!("OPFS write {rel}: {e:?}");
}
+ for d in done {
+ d(r.is_ok());
+ }
}
});
}
diff --git a/apps/filmcraft-web/src/recovery.rs b/apps/filmcraft-web/src/recovery.rs
index 939559a46..6001531e8 100644
--- a/apps/filmcraft-web/src/recovery.rs
+++ b/apps/filmcraft-web/src/recovery.rs
@@ -6,24 +6,32 @@
//! - Imported media are copied to `media/` (the browser streams the copy; nothing goes
//! through wasm memory), so the snapshot's `/files/` references resolve after a reload.
//! - At start-up a dirty snapshot is reopened (`?norecover` skips it, `?fresh` also skips the media).
+//! A skipped snapshot stays dirty until this session writes its own ([`crate::recovery_policy`]).
+
+use std::cell::{Cell, RefCell};
+use std::rc::Rc;
use filmcraft_engine::Session;
use serde_json::{Value, json};
use crate::opfs;
+use crate::recovery_policy::{Action, Policy};
const SNAPSHOT: &str = "recovery/snapshot.fcproj";
const META: &str = "recovery/meta.json";
-/// Seconds between snapshots of a changing project.
-const INTERVAL_S: f64 = 5.0;
/// Media larger than this are not copied to OPFS.
const KEEP_MAX: f64 = 4.0 * 1024.0 * 1024.0 * 1024.0;
+/// A finished OPFS write, reported back to the next [`Autosave::tick`].
+enum Outcome {
+ Snapshot { revision: u64, ok: bool },
+ Clean { ok: bool },
+}
+
#[derive(Default)]
pub struct Autosave {
- last_revision: u64,
- last_time: f64,
- last_dirty: Option,
+ policy: Policy,
+ outcomes: Rc>>,
}
impl Autosave {
@@ -33,22 +41,44 @@ impl Autosave {
return;
}
let now = ctx.input(|i| i.time);
- let dirty = s.revision != s.saved_revision;
- if dirty {
- if s.revision != self.last_revision && now - self.last_time >= INTERVAL_S {
- opfs::write(SNAPSHOT.into(), filmcraft_format::encode(&s.project, false));
+ for o in self.outcomes.borrow_mut().drain(..) {
+ match o {
+ Outcome::Snapshot { revision, ok } => self.policy.written(revision, ok, now),
+ Outcome::Clean { ok } => self.policy.cleaned(ok, now),
+ }
+ }
+ match self.policy.tick(now, s.revision, s.saved_revision) {
+ Action::Nothing => {}
+ Action::WakeAfter(secs) => ctx.request_repaint_after(std::time::Duration::from_secs_f64(secs)),
+ Action::WriteSnapshot(revision) => {
+ // Snapshot and meta both have to land; either failing retries the pair.
+ let failed = Rc::new(Cell::new(false));
+ let left = Rc::new(Cell::new(2u8));
+ let done = |out: Rc>>, failed: Rc>, left: Rc>, ctx: egui::Context| -> opfs::Done {
+ Box::new(move |ok| {
+ failed.set(failed.get() || !ok);
+ left.set(left.get().saturating_sub(1));
+ if left.get() == 0 {
+ out.borrow_mut().push(Outcome::Snapshot { revision, ok: !failed.get() });
+ ctx.request_repaint();
+ }
+ })
+ };
let meta = json!({"dirty": true, "name": s.project.name, "revision": s.revision, "time": js_sys::Date::now()});
- opfs::write(META.into(), meta.to_string().into_bytes());
- self.last_revision = s.revision;
- self.last_time = now;
- } else if s.revision != self.last_revision {
- // the UI may go idle: wake up when the snapshot is due
- ctx.request_repaint_after(std::time::Duration::from_secs_f64((INTERVAL_S - (now - self.last_time)).max(0.05)));
+ let d1 = done(self.outcomes.clone(), failed.clone(), left.clone(), ctx.clone());
+ let d2 = done(self.outcomes.clone(), failed, left, ctx.clone());
+ opfs::write_then(SNAPSHOT.into(), filmcraft_format::encode(&s.project, false), Some(d1));
+ opfs::write_then(META.into(), meta.to_string().into_bytes(), Some(d2));
+ }
+ Action::WriteClean => {
+ let (out, ctx) = (self.outcomes.clone(), ctx.clone());
+ let done: opfs::Done = Box::new(move |ok| {
+ out.borrow_mut().push(Outcome::Clean { ok });
+ ctx.request_repaint();
+ });
+ opfs::write_then(META.into(), json!({"dirty": false}).to_string().into_bytes(), Some(done));
}
- } else if self.last_dirty != Some(false) {
- opfs::write(META.into(), json!({"dirty": false}).to_string().into_bytes());
}
- self.last_dirty = Some(dirty);
}
}
diff --git a/apps/filmcraft-web/src/recovery_policy.rs b/apps/filmcraft-web/src/recovery_policy.rs
new file mode 100644
index 000000000..8426d3c39
--- /dev/null
+++ b/apps/filmcraft-web/src/recovery_policy.rs
@@ -0,0 +1,88 @@
+//! When the web recovery snapshot is written or marked clean (pure, so it is tested natively in
+//! `tests/recovery_policy.rs`; the OPFS side lives in [`crate::recovery`]).
+//!
+//! - A dirty project is snapshotted at most every [`INTERVAL_S`] seconds, once per revision. A write
+//! that fails is retried after the interval, without waiting for another edit (#197).
+//! - The meta is marked clean only to retire a snapshot this session wrote. A snapshot left by an
+//! earlier session that was not reopened (`?norecover`, `?fresh`) stays dirty (#193).
+
+/// Seconds between snapshots of a changing project.
+pub const INTERVAL_S: f64 = 5.0;
+
+/// What the caller should do this frame.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub enum Action {
+ Nothing,
+ /// Write the snapshot and a dirty meta for this revision, then report with [`Policy::written`].
+ WriteSnapshot(u64),
+ /// Write a clean meta, then report with [`Policy::cleaned`].
+ WriteClean,
+ /// Nothing now; wake up after this many seconds.
+ WakeAfter(f64),
+}
+
+#[derive(Debug, Default)]
+pub struct Policy {
+ /// Revision whose snapshot was last written or is being written.
+ last_revision: Option,
+ last_time: Option,
+ /// This session wrote a dirty meta that is not yet retired.
+ owns_dirty_meta: bool,
+ /// A clean-meta write is in flight.
+ cleaning: bool,
+ /// Snapshot writes started so far, and how many had started when the clean write did (a
+ /// snapshot started after it makes the meta dirty again).
+ snapshots: u64,
+ snapshots_at_clean: u64,
+}
+
+impl Policy {
+ /// Called every frame with the session's revisions and the time in seconds.
+ pub fn tick(&mut self, now: f64, revision: u64, saved_revision: u64) -> Action {
+ if revision != saved_revision {
+ if self.last_revision == Some(revision) {
+ return Action::Nothing;
+ }
+ let since = self.last_time.map_or(f64::INFINITY, |t| now - t);
+ if since >= INTERVAL_S {
+ self.last_revision = Some(revision);
+ self.last_time = Some(now);
+ self.owns_dirty_meta = true;
+ self.snapshots += 1;
+ return Action::WriteSnapshot(revision);
+ }
+ return Action::WakeAfter((INTERVAL_S - since).max(0.05));
+ }
+ if self.owns_dirty_meta && !self.cleaning {
+ let since = self.last_time.map_or(f64::INFINITY, |t| now - t);
+ if self.last_revision.is_some() || since >= INTERVAL_S {
+ self.cleaning = true;
+ self.snapshots_at_clean = self.snapshots;
+ return Action::WriteClean;
+ }
+ return Action::WakeAfter((INTERVAL_S - since).max(0.05));
+ }
+ Action::Nothing
+ }
+
+ /// The snapshot write for `revision` finished.
+ pub fn written(&mut self, revision: u64, ok: bool, now: f64) {
+ if !ok && self.last_revision == Some(revision) {
+ // retry after the interval, even if nothing else changes
+ self.last_revision = None;
+ self.last_time = Some(now);
+ }
+ }
+
+ /// The clean-meta write finished.
+ pub fn cleaned(&mut self, ok: bool, now: f64) {
+ self.cleaning = false;
+ if ok {
+ self.owns_dirty_meta = self.snapshots != self.snapshots_at_clean;
+ } else {
+ // retry after the interval
+ self.last_revision = None;
+ self.last_time = Some(now);
+ }
+ }
+}
diff --git a/apps/filmcraft-web/src/webcodecs.rs b/apps/filmcraft-web/src/webcodecs.rs
index 612a782b2..48fa7a1c8 100644
--- a/apps/filmcraft-web/src/webcodecs.rs
+++ b/apps/filmcraft-web/src/webcodecs.rs
@@ -12,8 +12,7 @@
//! the job on a later frame, when the decoder's output callback has delivered the picture.
//! - A session that has not delivered the frame it was started for is not restarted by other
//! requests (thumbnails and the monitor would otherwise keep resetting each other).
-//! - Outputs are converted to RGBA by drawing the `VideoFrame` on an `OffscreenCanvas` (the
-//! browser applies the stream's colour description).
+//! - Native YUV planes feed the desktop frame/compositor contract. Unsupported browser formats or geometry use Canvas RGBA.
//! - Any decoder error or unsupported configuration switches the source to our decoder.
//!
//! `?nowebcodecs` disables it. Support is probed at start-up with `VideoDecoder.isConfigSupported`.
@@ -34,6 +33,20 @@ use wasm_bindgen::JsCast;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;
+mod pixels;
+
+#[derive(Default)]
+struct Transfers {
+ native: u64,
+ canvas: u64,
+ closed: u64,
+ stale: u64,
+ bytes: u64,
+ native_ms: f64,
+ canvas_ms: f64,
+ reasons: BTreeMap<&'static str, u64>,
+}
+
/// Samples fed past the wanted one (decoders hold pictures for reordering).
const LOOKAHEAD: usize = 8;
/// Decoded frames kept per source.
@@ -52,6 +65,7 @@ thread_local! {
static DECODED: Cell = const { Cell::new(0) };
static FALLBACKS: Cell = const { Cell::new(0) };
static SOURCES: Cell = const { Cell::new(0) };
+ static TRANSFERS: RefCell = RefCell::new(Transfers::default());
}
fn now_ms() -> f64 {
@@ -160,11 +174,15 @@ pub fn stats() -> serde_json::Value {
.iter()
.map(|(id, s)| {
json!({"id": id, "start": s.start, "next": s.next, "target": s.target, "needsKey": s.needs_key, "flushing": s.flushing.get(),
- "errored": s.errored.get(), "queue": s.queue(), "idleMs": now_ms() - s.last_output.get()})
+ "errored": s.errored.get(), "queue": s.queue(), "copies": s.copying.get(), "idleMs": now_ms() - s.last_output.get()})
})
.collect()
});
- json!({"sources": SOURCES.with(Cell::get), "framesDecoded": DECODED.with(Cell::get), "fallbacks": FALLBACKS.with(Cell::get), "sessions": sessions})
+ let transfers = TRANSFERS.with(|s| {
+ let s = s.borrow();
+ json!({"nativeFrames":s.native,"canvasFrames":s.canvas,"closedFrames":s.closed,"staleFrames":s.stale,"nativeBytes":s.bytes,"nativeMs":s.native_ms,"canvasMs":s.canvas_ms,"canvasReasons":s.reasons})
+ });
+ json!({"sources": SOURCES.with(Cell::get), "framesDecoded": DECODED.with(Cell::get), "fallbacks": FALLBACKS.with(Cell::get), "sessions": sessions,"transfers":transfers})
}
/// A [`SharedReader`] as the demuxer's byte source.
@@ -210,6 +228,8 @@ pub fn reader_opener(name: &str, head: &[u8], reader: &SharedReader) -> Option Option>,
@@ -229,6 +250,8 @@ struct Config {
height: u32,
/// Clockwise quarter turns from the track matrix, applied to decoded frames.
rotation: u8,
+ color: filmcraft_color::ColorInfo,
+ par: (u32, u32),
}
#[derive(Default)]
@@ -253,6 +276,9 @@ struct Session {
/// Highest presentation time output since the session (re)started.
out_max: Rc>,
out: Rc>>,
+ copying: Rc>,
+ generation: Rc>,
+ alive: Rc>,
_closures: (Closure, Closure),
}
@@ -286,17 +312,65 @@ impl Session {
let errored = Rc::new(Cell::new(false));
let last_output = Rc::new(Cell::new(now_ms()));
let out_max = Rc::new(Cell::new(i64::MIN));
+ let copying = Rc::new(Cell::new(0u32));
+ let generation = Rc::new(Cell::new(0u64));
+ let alive = Rc::new(Cell::new(true));
let (o, lo, om) = (out.clone(), last_output.clone(), out_max.clone());
+ let (copies, epoch, active, config, errors) = (copying.clone(), generation.clone(), alive.clone(), cfg.clone(), errored.clone());
let on_output = Closure::::new(move |frame: JsValue| {
let ts = get(&frame, "timestamp").as_f64().unwrap_or(0.0) as i64;
- om.set(om.get().max(ts));
- if let Some(f) = to_rgba(&frame) {
- o.borrow_mut().push((ts, f));
- DECODED.with(|c| c.set(c.get() + 1));
- }
- let _ = call(&frame, "close", &[]);
- lo.set(now_ms());
- crate::repaint();
+ let expected = epoch.get();
+ let (o, lo, om, copies, epoch, active, config, errors) =
+ (o.clone(), lo.clone(), om.clone(), copies.clone(), epoch.clone(), active.clone(), config.clone(), errors.clone());
+ copies.set(copies.get().saturating_add(1));
+ wasm_bindgen_futures::spawn_local(async move {
+ let begin = now_ms();
+ let converted = match pixels::read(&frame, &config).await {
+ Ok((f, bytes)) => {
+ TRANSFERS.with(|s| {
+ let mut s = s.borrow_mut();
+ s.native = s.native.saturating_add(1);
+ s.bytes = s.bytes.saturating_add(bytes as u64);
+ s.native_ms += now_ms() - begin;
+ });
+ Some(f)
+ }
+ Err(reason) => {
+ let begin = now_ms();
+ let f = to_rgba(&frame);
+ TRANSFERS.with(|s| {
+ let mut s = s.borrow_mut();
+ s.canvas = s.canvas.saturating_add(1);
+ s.canvas_ms += now_ms() - begin;
+ let count = s.reasons.entry(reason).or_default();
+ *count = count.saturating_add(1);
+ });
+ f
+ }
+ };
+ let _ = call(&frame, "close", &[]);
+ copies.set(copies.get().saturating_sub(1));
+ TRANSFERS.with(|s| {
+ let mut s = s.borrow_mut();
+ s.closed = s.closed.saturating_add(1);
+ });
+ if !active.get() || epoch.get() != expected {
+ TRANSFERS.with(|s| {
+ let mut s = s.borrow_mut();
+ s.stale = s.stale.saturating_add(1);
+ });
+ return;
+ }
+ if let Some(f) = converted {
+ o.borrow_mut().push((ts, f));
+ om.set(om.get().max(ts));
+ DECODED.with(|c| c.set(c.get().saturating_add(1)));
+ } else {
+ errors.set(true);
+ }
+ lo.set(now_ms());
+ crate::repaint();
+ });
});
let e = errored.clone();
let on_error = Closure::::new(move |err: JsValue| {
@@ -317,6 +391,9 @@ impl Session {
last_output,
out_max,
out,
+ copying,
+ generation,
+ alive,
_closures: (on_output, on_error),
};
s.configure(cfg)?;
@@ -341,6 +418,14 @@ impl Session {
}
}
+impl Drop for Session {
+ fn drop(&mut self) {
+ self.alive.set(false);
+ self.generation.set(self.generation.get().wrapping_add(1));
+ let _ = call(&self.decoder, "close", &[]);
+ }
+}
+
/// An MP4/MOV source whose video is decoded by the browser.
pub struct WcSource {
inner: SharedSource,
@@ -388,12 +473,15 @@ impl WcSource {
// already output but the cache dropped needs a restart).
let covers = !s.needs_key && s.start <= i && key <= s.next && (i >= s.next || want > s.out_max.get());
if !covers {
- // Don't interrupt a session that is still working towards its own frame.
- let busy = !s.needs_key && s.target != i64::MIN && s.out_max.get() < s.target && now_ms() - s.last_output.get() < STALE_MS;
+ // Don't interrupt a session that is still working towards its own frame, unless it
+ // has gone quiet for STALE_MS (a copyTo that never settles must not wedge it).
+ let fresh = now_ms() - s.last_output.get() < STALE_MS;
+ let busy = fresh && (s.copying.get() > 0 || (!s.needs_key && s.target != i64::MIN && s.out_max.get() < s.target));
if busy {
return Drive::Waiting;
}
if !s.needs_key || s.next > 0 {
+ s.generation.set(s.generation.get().wrapping_add(1));
let _ = call(&s.decoder, "reset", &[]);
if s.configure(&self.config).is_err() {
return Drive::Failed;
@@ -410,7 +498,7 @@ impl WcSource {
s.target = want;
}
let upto = (i + LOOKAHEAD).min(n - 1);
- while s.next <= upto && s.queue() < MAX_QUEUE {
+ while s.next <= upto && s.queue().saturating_add(s.copying.get()) < MAX_QUEUE {
let smp = &t.samples[s.next];
let mut data = vec![0u8; smp.size as usize];
match self.reader.read_at(smp.offset, &mut data) {
@@ -480,10 +568,8 @@ impl WcSource {
impl Drop for WcSource {
fn drop(&mut self) {
SESSIONS.with(|ss| {
- if let Ok(mut ss) = ss.try_borrow_mut()
- && let Some(s) = ss.remove(&self.id)
- {
- let _ = call(&s.decoder, "close", &[]);
+ if let Ok(mut ss) = ss.try_borrow_mut() {
+ ss.remove(&self.id);
}
});
}
@@ -532,4 +618,7 @@ impl MediaSource for WcSource {
fn audio(&self, start: i64, frames: usize, sample_rate: u32) -> Result {
self.inner.audio(start, frames, sample_rate)
}
+ fn audio_stream(&self, stream: usize, start: i64, frames: usize, sample_rate: u32) -> Result {
+ self.inner.audio_stream(stream, start, frames, sample_rate)
+ }
}
diff --git a/apps/filmcraft-web/src/webcodecs/pixels.rs b/apps/filmcraft-web/src/webcodecs/pixels.rs
new file mode 100644
index 000000000..dc99e7711
--- /dev/null
+++ b/apps/filmcraft-web/src/webcodecs/pixels.rs
@@ -0,0 +1,148 @@
+//! Browser planes -> core frames; no color conversion or chroma reconstruction here.
+
+use std::sync::Arc;
+
+use filmcraft_color::{ColorInfo, Matrix, Primaries, Range, Transfer};
+use filmcraft_frame::{Chroma, PixelData, VideoFrame};
+use filmcraft_time::Tick;
+use wasm_bindgen::{JsCast, JsValue};
+use wasm_bindgen_futures::JsFuture;
+
+use super::{Config, call, get, obj};
+
+// One 8K 4:4:4 16-bit picture + alpha fits within 256 MiB; cap browser-driven allocations.
+const MAX_COPY_BYTES: u32 = 256 * 1024 * 1024;
+
+fn integer(value: JsValue) -> Option {
+ let n = value.as_f64()?;
+ if n.is_finite() && n >= 0.0 && n <= u32::MAX as f64 && n.fract() == 0.0 { Some(n as u32) } else { None }
+}
+
+fn color(frame: &JsValue, fallback: ColorInfo) -> Option {
+ let c = get(frame, "colorSpace");
+ let matrix = match get(&c, "matrix").as_string().as_deref() {
+ Some("bt709") => Matrix::Bt709,
+ Some("bt470bg" | "smpte170m") => Matrix::Bt601,
+ Some("bt2020-ncl") => Matrix::Bt2020Ncl,
+ None => fallback.matrix,
+ _ => return None,
+ };
+ let transfer = match get(&c, "transfer").as_string().as_deref() {
+ Some("bt709") => Transfer::Bt709,
+ Some("iec61966-2-1") => Transfer::Srgb,
+ Some("smpte2084") => Transfer::Pq,
+ Some("arib-std-b67") => Transfer::Hlg,
+ None => fallback.transfer,
+ _ => return None,
+ };
+ let primaries = match get(&c, "primaries").as_string().as_deref() {
+ Some("bt709") => Primaries::Bt709,
+ Some("bt470bg") => Primaries::Bt601_625,
+ Some("smpte170m") => Primaries::Bt601_525,
+ Some("bt2020") => Primaries::Bt2020,
+ None => fallback.primaries,
+ _ => return None,
+ };
+ let range = match get(&c, "fullRange").as_bool() {
+ Some(true) => Range::Full,
+ Some(false) => Range::Limited,
+ None => fallback.range,
+ };
+ Some(ColorInfo { matrix, transfer, primaries, range })
+}
+
+fn plane(raw: &js_sys::Uint8Array, layout: &JsValue, width: u32, height: u32) -> Result, &'static str> {
+ let offset = integer(get(layout, "offset")).ok_or("layout")?;
+ let stride = integer(get(layout, "stride")).filter(|s| *s >= width).ok_or("layout")?;
+ let end =
+ height.checked_sub(1).and_then(|h| h.checked_mul(stride)).and_then(|n| offset.checked_add(n)).and_then(|n| n.checked_add(width)).ok_or("layout")?;
+ if end > raw.length() {
+ return Err("layout");
+ }
+ let len = width.checked_mul(height).filter(|n| *n <= MAX_COPY_BYTES).ok_or("allocation")? as usize;
+ let mut out = Vec::new();
+ out.try_reserve_exact(len).map_err(|_| "memory")?;
+ out.resize(len, 0);
+ if stride == width {
+ raw.subarray(offset, end).copy_to(&mut out);
+ } else {
+ for (y, row) in out.chunks_exact_mut(width as usize).enumerate() {
+ // Checked for the last row above; earlier rows are smaller, but stay checked anyway.
+ let start = u32::try_from(y).ok().and_then(|y| y.checked_mul(stride)).and_then(|n| offset.checked_add(n)).ok_or("layout")?;
+ let row_end = start.checked_add(width).filter(|e| *e <= end).ok_or("layout")?;
+ raw.subarray(start, row_end).copy_to(row);
+ }
+ }
+ Ok(out)
+}
+
+/// Copy native 8-bit YUV planes; unsupported formats/geometry return a counted Canvas fallback reason.
+pub(super) async fn read(frame: &JsValue, cfg: &Config) -> Result<(VideoFrame, u32), &'static str> {
+ let format = get(frame, "format").as_string().ok_or("format")?;
+ let (chroma, alpha, interleaved) = match format.as_str() {
+ "NV12" => (Chroma::C420, false, true),
+ "I420" => (Chroma::C420, false, false),
+ "I420A" => (Chroma::C420, true, false),
+ "I422" => (Chroma::C422, false, false),
+ "I422A" => (Chroma::C422, true, false),
+ "I444" => (Chroma::C444, false, false),
+ "I444A" => (Chroma::C444, true, false),
+ _ => return Err("format"),
+ };
+ let rect = get(frame, "visibleRect");
+ let w = integer(get(&rect, "width")).filter(|n| *n > 0).ok_or("geometry")?;
+ let h = integer(get(&rect, "height")).filter(|n| *n > 0).ok_or("geometry")?;
+ if integer(get(frame, "displayWidth")) != Some(w)
+ || integer(get(frame, "displayHeight")) != Some(h)
+ || get(frame, "rotation").as_f64().is_some_and(|r| r != 0.0)
+ || get(frame, "flip").as_bool() == Some(true)
+ {
+ return Err("geometry");
+ }
+ let color = color(frame, cfg.color).ok_or("color")?;
+ let options = obj(&[("rect", rect)]);
+ let bytes =
+ integer(call(frame, "allocationSize", &[&options]).map_err(|_| "allocation")?).filter(|n| *n > 0 && *n <= MAX_COPY_BYTES).ok_or("allocation")?;
+ let array: js_sys::Function = get(&js_sys::global(), "Uint8Array").dyn_into().map_err(|_| "allocation")?;
+ let raw: js_sys::Uint8Array =
+ js_sys::Reflect::construct(&array, &js_sys::Array::of1(&bytes.into())).and_then(|v| v.dyn_into()).map_err(|_| "allocation")?;
+ let promise = call(frame, "copyTo", &[raw.as_ref(), &options]).map_err(|_| "copy")?.dyn_into::().map_err(|_| "copy")?;
+ let layout = js_sys::Array::from(&JsFuture::from(promise).await.map_err(|_| "copy")?);
+ let count = if interleaved {
+ 2
+ } else if alpha {
+ 4
+ } else {
+ 3
+ };
+ if layout.length() != count {
+ return Err("layout");
+ }
+ let (sx, sy) = chroma.shifts();
+ let (cw, ch) = (w.div_ceil(1 << sx), h.div_ceil(1 << sy));
+ let y = plane(&raw, &layout.get(0), w, h)?;
+ let (u, v) = if interleaved {
+ let uv = plane(&raw, &layout.get(1), cw.checked_mul(2).ok_or("allocation")?, ch)?;
+ let pairs = uv.as_chunks::<2>().0;
+ let (mut u, mut v) = (Vec::new(), Vec::new());
+ u.try_reserve_exact(pairs.len()).map_err(|_| "memory")?;
+ v.try_reserve_exact(pairs.len()).map_err(|_| "memory")?;
+ u.extend(pairs.iter().map(|p| p[0]));
+ v.extend(pairs.iter().map(|p| p[1]));
+ (u, v)
+ } else {
+ (plane(&raw, &layout.get(1), cw, ch)?, plane(&raw, &layout.get(2), cw, ch)?)
+ };
+ let alpha = if alpha { Some(Arc::new(plane(&raw, &layout.get(3), w, h)?)) } else { None };
+ Ok((
+ VideoFrame {
+ width: w,
+ height: h,
+ data: PixelData::Yuv8 { planes: [Arc::new(y), Arc::new(u), Arc::new(v)], chroma, alpha },
+ color,
+ par: cfg.par,
+ pts: Tick::ZERO,
+ },
+ bytes,
+ ))
+}
diff --git a/apps/filmcraft-web/tests/import-bins.mjs b/apps/filmcraft-web/tests/import-bins.mjs
new file mode 100644
index 000000000..6e1811716
--- /dev/null
+++ b/apps/filmcraft-web/tests/import-bins.mjs
@@ -0,0 +1,67 @@
+// Web import destinations: active bin tab, browser drop + navigation during pending reads.
+// Synthetic browser drop event exercises the production handler; physical OS drag/picker UI is outside this test.
+export async function checkImportBins({ evaluate, media, timeoutMs = 120000 }) {
+ const report = {gates: {}, cases: []};
+ await evaluate('filmcraft.execute("file.newProject", {name: "Import bins"})');
+ const parent = (await evaluate('filmcraft.execute("file.newBin", {name: "Footage"})')).bin;
+ const child = (await evaluate(`filmcraft.execute("file.newBin", {name: "Selects", parent: ${parent}})`)).bin;
+ await evaluate(`(async () => {window.__importBinFixture = new Uint8Array(await (await fetch(${JSON.stringify(media)})).arrayBuffer());})()`);
+ const setBin = bin => evaluate(`filmcraft.request("ui.set", {projectPanel: {bin: ${JSON.stringify(bin)}, activeTab: null}})`);
+ const importExpression = name => `filmcraft.importFiles([new File([window.__importBinFixture], ${JSON.stringify(name)}, {type: "video/mp4"})])`;
+ const locate = async name => {
+ const deadline = Date.now() + timeoutMs;
+ const pollMs = 100;
+ for (;;) {
+ const project = await evaluate('filmcraft.execute("project.inspect")');
+ const find = bin => {
+ for (const entry of bin.children) {
+ if (entry.item !== undefined && entry.name === name) return bin.id;
+ if (entry.children) {const found = find(entry); if (found !== undefined) return found;}
+ }
+ };
+ const found = find(project.root);
+ if (found !== undefined) return found;
+ if (Date.now() >= deadline) throw new Error(`Import never appeared: ${name}`);
+ await new Promise(resolve => setTimeout(resolve, pollMs));
+ }
+ };
+ const check = async (key, expected, name) => {
+ const actual = await locate(name);
+ report.cases.push({key, expected, actual, name}); report.gates[key] = actual === expected;
+ };
+ await evaluate(`filmcraft.request("ui.set", {projectPanel: {bin: ${child}, activeTab: 0, tabs: [{bin: ${parent}}]}})`);
+ await evaluate(importExpression("tab.mp4")); await check("activeTab", parent, "tab.mp4");
+ await setBin(child);
+ await evaluate(`(() => {
+ const transfer = new DataTransfer();
+ transfer.items.add(new File([window.__importBinFixture], "drop.mp4", {type: "video/mp4"}));
+ window.dispatchEvent(new DragEvent("drop", {dataTransfer: transfer, bubbles: true, cancelable: true}));
+ })()`);
+ await check("browserDrop", child, "drop.mp4");
+ await setBin(child);
+ await evaluate(`(() => {
+ const original = Blob.prototype.arrayBuffer;
+ let release;
+ const pending = new Promise(resolve => {release = resolve;});
+ const gate = window.__importBinGate = {hits: 0, release, original};
+ Blob.prototype.arrayBuffer = function () {gate.hits++; return pending.then(() => original.call(this));};
+ window.__pendingBinImport = ${importExpression("pending.mp4")};
+ })()`);
+ try {
+ const deadline = Date.now() + timeoutMs;
+ const pollMs = 100;
+ for (;;) {
+ if (await evaluate("window.__importBinGate.hits > 0")) break;
+ if (Date.now() >= deadline) throw new Error("Read-delay probe did not intercept any Blob reads");
+ await new Promise(resolve => setTimeout(resolve, pollMs));
+ }
+ report.delayedReadHits = await evaluate("window.__importBinGate.hits");
+ await setBin(parent);
+ await evaluate("window.__importBinGate.release(); window.__pendingBinImport");
+ await check("destinationCapturedBeforeRead", child, "pending.mp4");
+ } finally {
+ await evaluate("Blob.prototype.arrayBuffer = window.__importBinGate.original; window.__importBinGate.release(); delete window.__importBinGate; delete window.__pendingBinImport; delete window.__importBinFixture");
+ }
+ report.ok = Object.values(report.gates).every(Boolean);
+ return report;
+}
diff --git a/apps/filmcraft-web/tests/import-collision.mjs b/apps/filmcraft-web/tests/import-collision.mjs
new file mode 100644
index 000000000..dfde3b664
--- /dev/null
+++ b/apps/filmcraft-web/tests/import-collision.mjs
@@ -0,0 +1,78 @@
+// #194 regression: same-name/same-size imports must retain independent sources across OPFS recovery.
+// evaluate(expression) awaits a browser expression; navigate(url) waits for FilmCraft readiness. Generated PCM16 mono WAVs only; no binary fixtures.
+import { createHash } from "node:crypto";
+
+function tone(frequency, seconds) {
+ // RIFF/WAVE PCM16 mono: 44-byte header + 2 bytes/sample; fixture matches #194 (48 kHz, amplitude 12000).
+ const rate = 48000, bytesPerSample = 2, samples = rate * seconds;
+ const b = Buffer.alloc(44 + samples * bytesPerSample);
+ b.write("RIFF", 0); b.writeUInt32LE(b.length - 8, 4); b.write("WAVEfmt ", 8);
+ b.writeUInt32LE(16, 16); b.writeUInt16LE(1, 20); b.writeUInt16LE(1, 22);
+ b.writeUInt32LE(rate, 24); b.writeUInt32LE(rate * bytesPerSample, 28);
+ b.writeUInt16LE(bytesPerSample, 32); b.writeUInt16LE(16, 34); b.write("data", 36); b.writeUInt32LE(samples * bytesPerSample, 40);
+ for (let i = 0; i < samples; i++) b.writeInt16LE(Math.round(12000 * Math.sin(2 * Math.PI * frequency * i / rate)), 44 + bytesPerSample * i);
+ return b;
+}
+
+export async function checkImportCollision({ evaluate, navigate, url, timeoutMs = 120000 }) {
+ const fixtures = [tone(440, 2), tone(880, 2)];
+ const hashes = fixtures.map(b => createHash("sha256").update(b).digest("hex"));
+ const report = { hashes, fixtureBytes: fixtures.map(b => b.length), gates: {} };
+ const hash = expression => evaluate(`(async () => Array.from(new Uint8Array(await crypto.subtle.digest("SHA-256", ${expression})), b => b.toString(16).padStart(2, "0")).join(""))()`);
+ const readHash = path => hash(`await filmcraft.readFile(${JSON.stringify(path)})`);
+ const importFile = index => evaluate(`filmcraft.importFiles([new File([Uint8Array.from(atob(${JSON.stringify(fixtures[index].toString("base64"))}), c => c.charCodeAt(0))], "collision.wav", {type: "audio/wav"})])`);
+ await evaluate('filmcraft.execute("file.newProject", {name: "Import collision"})');
+ const a = await importFile(0); report.a = a;
+ if (a.errors?.length || !a.items?.length) throw new Error("baseline import failed: " + JSON.stringify(a));
+ report.initialHash = await readHash(a.paths[0]);
+ report.gates.healthyInitial = report.initialHash === hashes[0];
+ await evaluate(`filmcraft.execute("file.newSequence", {name: "Original A", fromItem: ${a.items[0]}})`);
+ const b = await importFile(1); report.b = b;
+ if (b.errors?.length || !b.items?.length) throw new Error("second import failed: " + JSON.stringify(b));
+ report.afterBHash = await readHash(a.paths[0]);
+ report.gates.distinctPaths = a.paths[0] !== b.paths[0];
+ report.gates.originalPreserved = report.afterBHash === hashes[0];
+ report.beforeProject = await evaluate('filmcraft.execute("project.inspect")');
+ const revision = report.beforeProject.revision;
+ const deadline = Date.now() + timeoutMs;
+ const persistencePollMs = 100;
+ for (;;) {
+ const snapshot = await evaluate(`(async () => {
+ try {
+ const dir = await (await navigator.storage.getDirectory()).getDirectoryHandle("recovery");
+ const read = async name => JSON.parse(await (await (await dir.getFileHandle(name)).getFile()).text());
+ return {meta: await read("meta.json"), project: await read("snapshot.fcproj")};
+ } catch (e) {return {error: String(e)};}
+ })()`);
+ report.recoveryMeta = snapshot.meta;
+ if (snapshot.meta?.dirty && snapshot.meta.revision === revision && JSON.stringify(snapshot.project).includes("Original A")) break;
+ if (Date.now() >= deadline) throw new Error("Recovery persistence gate failed: " + JSON.stringify(snapshot));
+ await new Promise(resolve => setTimeout(resolve, persistencePollMs));
+ }
+ // Media writes run separately from the snapshot queue; gate their actual bytes before navigation.
+ for (;;) {
+ report.opfs = await evaluate(`(async () => {
+ const dir = await (await navigator.storage.getDirectory()).getDirectoryHandle("media");
+ const files = [];
+ for await (const [name, handle] of dir.entries()) {
+ const bytes = await (await handle.getFile()).arrayBuffer();
+ files.push({name, hash: Array.from(new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)), b => b.toString(16).padStart(2, "0")).join("")});
+ }
+ return files;
+ })()`);
+ // Before the fix, A and B intentionally share the same path; qualify the last write there.
+ const expected = new Map([[a.paths[0], hashes[0]], [b.paths[0], hashes[1]]]);
+ if ([...expected].every(([path, digest]) => report.opfs.some(f => f.name === path.slice("/files/".length) && f.hash === digest))) break;
+ if (Date.now() >= deadline) throw new Error("Imported media did not reach OPFS: " + JSON.stringify(report.opfs));
+ await new Promise(resolve => setTimeout(resolve, persistencePollMs));
+ }
+ const recoveryUrl = new URL(url);
+ recoveryUrl.searchParams.delete("fresh"); recoveryUrl.searchParams.delete("norecover");
+ await navigate(recoveryUrl.href);
+ report.recovered = await evaluate('filmcraft.execute("project.inspect")');
+ report.afterReload = {a: await readHash(a.paths[0]), b: await readHash(b.paths[0])};
+ report.gates.recoveredProject = JSON.stringify(report.recovered).includes("Original A");
+ report.gates.recoveredSources = report.afterReload.a === hashes[0] && report.afterReload.b === hashes[1];
+ report.ok = Object.values(report.gates).every(Boolean);
+ return report;
+}
diff --git a/apps/filmcraft-web/tests/recovery_policy.rs b/apps/filmcraft-web/tests/recovery_policy.rs
new file mode 100644
index 000000000..c60b34d5b
--- /dev/null
+++ b/apps/filmcraft-web/tests/recovery_policy.rs
@@ -0,0 +1,77 @@
+//! The web recovery snapshot policy. It is pure, so it runs natively even though the rest of the
+//! crate is wasm32-only.
+
+#[path = "../src/recovery_policy.rs"]
+mod recovery_policy;
+
+use recovery_policy::{Action, INTERVAL_S, Policy};
+
+/// #197: a snapshot write that fails is retried after the interval without another edit.
+#[test]
+fn failed_snapshot_write_is_retried_without_another_edit() {
+ let mut p = Policy::default();
+ assert_eq!(p.tick(0.0, 3, 0), Action::WriteSnapshot(3));
+ p.written(3, false, 0.1);
+ // not immediately (no spinning on a failing disk)…
+ assert!(matches!(p.tick(1.0, 3, 0), Action::WakeAfter(_)));
+ // …but once the interval has passed, with the revision unchanged
+ assert_eq!(p.tick(0.1 + INTERVAL_S, 3, 0), Action::WriteSnapshot(3));
+ p.written(3, true, 5.2);
+ assert_eq!(p.tick(20.0, 3, 0), Action::Nothing);
+}
+
+/// A failure reported for an older revision doesn't make the newer one be written again.
+#[test]
+fn stale_failure_is_ignored() {
+ let mut p = Policy::default();
+ assert_eq!(p.tick(0.0, 1, 0), Action::WriteSnapshot(1));
+ assert_eq!(p.tick(INTERVAL_S, 2, 0), Action::WriteSnapshot(2));
+ p.written(1, false, INTERVAL_S);
+ assert_eq!(p.tick(3.0 * INTERVAL_S, 2, 0), Action::Nothing);
+}
+
+/// #193: a clean session (started with `?norecover` / `?fresh`, so the snapshot left by an
+/// earlier session was not reopened) never marks that snapshot clean.
+#[test]
+fn clean_start_leaves_an_unconsumed_snapshot_dirty() {
+ let mut p = Policy::default();
+ for t in 0..100 {
+ assert_eq!(p.tick(t as f64, 0, 0), Action::Nothing);
+ }
+}
+
+/// The snapshot this session wrote is retired once the project is saved.
+#[test]
+fn saving_marks_own_snapshot_clean() {
+ let mut p = Policy::default();
+ assert_eq!(p.tick(0.0, 1, 0), Action::WriteSnapshot(1));
+ p.written(1, true, 0.1);
+ assert_eq!(p.tick(1.0, 1, 1), Action::WriteClean);
+ assert_eq!(p.tick(1.1, 1, 1), Action::Nothing);
+ p.cleaned(true, 1.2);
+ assert_eq!(p.tick(30.0, 1, 1), Action::Nothing);
+}
+
+/// A failed clean write is retried after the interval.
+#[test]
+fn failed_clean_write_is_retried() {
+ let mut p = Policy::default();
+ assert_eq!(p.tick(0.0, 1, 0), Action::WriteSnapshot(1));
+ p.written(1, true, 0.1);
+ assert_eq!(p.tick(1.0, 1, 1), Action::WriteClean);
+ p.cleaned(false, 1.1);
+ assert!(matches!(p.tick(2.0, 1, 1), Action::WakeAfter(_)));
+ assert_eq!(p.tick(1.1 + INTERVAL_S, 1, 1), Action::WriteClean);
+}
+
+/// An edit made while the clean write is in flight keeps the meta dirty after it lands.
+#[test]
+fn edit_during_clean_write_keeps_meta_owned() {
+ let mut p = Policy::default();
+ assert_eq!(p.tick(0.0, 1, 0), Action::WriteSnapshot(1));
+ assert_eq!(p.tick(10.0, 1, 1), Action::WriteClean);
+ assert_eq!(p.tick(20.0, 2, 1), Action::WriteSnapshot(2));
+ p.cleaned(true, 20.1);
+ // saved again: the newer snapshot must be retired too
+ assert_eq!(p.tick(30.0, 2, 2), Action::WriteClean);
+}
diff --git a/apps/filmcraft-web/tests/smoke.mjs b/apps/filmcraft-web/tests/smoke.mjs
index f43ceec0c..71951edf5 100644
--- a/apps/filmcraft-web/tests/smoke.mjs
+++ b/apps/filmcraft-web/tests/smoke.mjs
@@ -13,6 +13,8 @@ import { spawn } from "node:child_process";
import { mkdirSync, writeFileSync, readdirSync, statSync, mkdtempSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
+import { checkImportBins } from "./import-bins.mjs";
+import { checkImportCollision } from "./import-collision.mjs";
const arg = (k, d) => {
const i = process.argv.indexOf(`--${k}`);
@@ -162,6 +164,19 @@ try {
await sleep(500);
await send("Emulation.setDeviceMetricsOverride", { width: 1600, height: 1000, deviceScaleFactor: 1, mobile: false });
report.steps.modes = { screenshot: await shot("05-modes") };
+ report.steps.importBins = await checkImportBins({evaluate: js, media});
+ if (!report.steps.importBins.ok) throw new Error("import bin regression: " + JSON.stringify(report.steps.importBins.gates));
+ report.steps.importCollision = await checkImportCollision({
+ evaluate: js,
+ url,
+ navigate: async (nextUrl) => {
+ await send("Page.navigate", { url: nextUrl });
+ await until("!!(window.filmcraftLoad && (window.filmcraftLoad.readyMs || window.filmcraftLoad.error))", 120000);
+ const load = await js("window.filmcraftLoad");
+ if (load.error) throw new Error(load.error);
+ },
+ });
+ if (!report.steps.importCollision.ok) throw new Error("import collision regression: " + JSON.stringify(report.steps.importCollision.gates));
// still alive? (a panicked app never answers)
await Promise.race([js("filmcraft.inspect().then(() => true)"), sleep(5000).then(() => { throw new Error("app stopped answering"); })]);
const fatal = logs.filter((l) => /panicked at|RuntimeError|EXCEPTION/.test(l));
diff --git a/apps/filmcraft/Cargo.toml b/apps/filmcraft/Cargo.toml
index bcc127ce9..a7e3d6dab 100644
--- a/apps/filmcraft/Cargo.toml
+++ b/apps/filmcraft/Cargo.toml
@@ -11,7 +11,7 @@ build = "build.rs"
workspace = true
[features]
-default = ["neural-voices"]
+default = []
# Speech-to-text (Text panel ▸ Transcribe) with Whisper on the CPU, and model downloads.
whisper = ["filmcraft-engine/whisper", "filmcraft-engine/speech-download"]
# Natural Text to Speech voices (Kokoro-82M on the CPU, downloaded on first use).
@@ -28,6 +28,7 @@ log = { workspace = true }
rfd = { workspace = true }
cpal = "0.16"
chrono = { version = "0.4", default-features = false, features = ["clock"] }
+sys-locale = "0.3"
[target.'cfg(target_os = "macos")'.dependencies]
muda = "0.21"
diff --git a/apps/filmcraft/src/graphics.rs b/apps/filmcraft/src/graphics.rs
new file mode 100644
index 000000000..b2be99cbf
--- /dev/null
+++ b/apps/filmcraft/src/graphics.rs
@@ -0,0 +1,52 @@
+//! Windows graphics instance configuration, applied before wgpu can initialize any driver.
+
+use eframe::{NativeOptions, egui_wgpu::WgpuSetup, wgpu::Backends};
+
+/// Restrict the wgpu instance to the primary backends (Vulkan, DirectX 12, Metal), i.e. leave
+/// OpenGL out, unless `backend_override` (normally `Backends::from_env()`, i.e. `WGPU_BACKEND`)
+/// names other backends.
+pub fn configure(options: &mut NativeOptions, backend_override: Option) {
+ if let WgpuSetup::CreateNew(create) = &mut options.wgpu_options.wgpu_setup {
+ // eframe's default includes GL. Creating that backend can crash inside AMD's
+ // atio6axx.dll before adapter selection or any Rust error handling runs, so the
+ // window never appears. Choosing an adapter afterwards is too late: only leaving GL
+ // out of the instance avoids it. Keep Vulkan and DX12 both: forcing DX12 alone sends
+ // shaders through FXC (which rejects fx.wgsl, silently falling back to the CPU) and
+ // leaves machines without DX12 with no backend at all.
+ create.instance_descriptor.backends = backend_override.unwrap_or(Backends::PRIMARY);
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn configured_backends(backend_override: Option) -> Backends {
+ let mut options = NativeOptions::default();
+ configure(&mut options, backend_override);
+ let WgpuSetup::CreateNew(create) = options.wgpu_options.wgpu_setup else {
+ panic!("default native options must create a wgpu instance");
+ };
+ create.instance_descriptor.backends
+ }
+
+ #[test]
+ fn windows_default_leaves_out_gl() {
+ // eframe's own default (PRIMARY | GL) would initialize OpenGL as well.
+ let WgpuSetup::CreateNew(eframe_default) = NativeOptions::default().wgpu_options.wgpu_setup else {
+ panic!("default native options must create a wgpu instance");
+ };
+ assert!(eframe_default.instance_descriptor.backends.contains(Backends::GL));
+ let configured = configured_backends(None);
+ assert_eq!(configured, Backends::PRIMARY);
+ assert!(!configured.contains(Backends::GL));
+ assert!(configured.contains(Backends::DX12) && configured.contains(Backends::VULKAN));
+ }
+
+ #[test]
+ fn explicit_backend_override_is_preserved() {
+ for backends in [Backends::VULKAN, Backends::GL, Backends::DX12, Backends::VULKAN | Backends::DX12, Backends::empty()] {
+ assert_eq!(configured_backends(Some(backends)), backends);
+ }
+ }
+}
diff --git a/apps/filmcraft/src/logging.rs b/apps/filmcraft/src/logging.rs
new file mode 100644
index 000000000..3d7c713fb
--- /dev/null
+++ b/apps/filmcraft/src/logging.rs
@@ -0,0 +1,562 @@
+//! The desktop app's logger: `log::` records go to standard error and to a log file.
+//!
+//! Without a logger every `log::warn!`/`log::error!` in the workspace (GPU compositor fallbacks,
+//! audio output problems, a panicked frame job or audio mixer, container warnings) vanished. A
+//! launch from a desktop menu has no terminal, so the file is what a bug report can attach:
+//! `/Logs/filmcraft.log`, next to the crash logs (`--data-dir`, `FILMCRAFT_DATA_DIR` or
+//! `filmcraft_engine::autosave::default_data_dir`; Help ▸ Reveal Log Files opens the folder). Each
+//! start moves the previous log to `filmcraft.1.log` (and that one to `.2`), so the log of a run
+//! that crashed survives the next launch.
+//!
+//! Levels: `info` for FilmCraft's own crates, `warn` for everything else (wgpu and naga are
+//! chatty). `RUST_LOG` replaces that with env_logger-style directives: `debug`,
+//! `warn,filmcraft_codecs=trace`, `wgpu_core=info`. A directive ending in `*` matches every target
+//! that starts with it (`filmcraft*=debug`).
+//!
+//! Records logged before the data directory is known are kept (up to [`MAX_PENDING`]) and written
+//! once the file is attached. Writing never panics: a file that can't be created or written leaves
+//! standard error as the only sink.
+//!
+//! Realtime audio: the cpal callbacks (`audio.rs`, `audio_in.rs`, the play-ahead reader) do not
+//! log; they report through flags and a one-time `eprintln!`. A record takes the sink lock only for
+//! one `write_all` of an already formatted line (standard error is written outside the lock), so a
+//! logging thread such as the audio mixer never waits on more than one other line being written.
+
+use std::fs::File;
+use std::io::Write;
+use std::path::{Path, PathBuf};
+use std::sync::{Mutex, OnceLock, PoisonError};
+use std::time::SystemTime;
+
+use log::LevelFilter;
+
+/// The current log file's name inside the log directory.
+pub const LOG_FILE: &str = "filmcraft.log";
+/// The log directory inside the data directory (shared with the crash logs and Reveal Log Files).
+pub const LOG_DIR: &str = "Logs";
+/// How many previous logs are kept (`filmcraft.1.log` … `filmcraft..log`).
+pub const KEEP: usize = 2;
+/// The log file stops growing past this size (a runaway warning can't fill the disk).
+pub const MAX_FILE_BYTES: u64 = 16 * 1024 * 1024;
+/// Records kept in memory until the log file is attached.
+pub const MAX_PENDING: usize = 512;
+/// The built-in filter when `RUST_LOG` is unset or empty.
+pub const DEFAULT_FILTER: &str = "warn,filmcraft*=info";
+
+/// Per-target level filter parsed from env_logger-style directives.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct Filter {
+ default: LevelFilter,
+ directives: Vec<(String, LevelFilter)>,
+}
+
+impl Filter {
+ /// Parse `spec`; unknown levels and empty parts are skipped. Without a bare level, targets no
+ /// directive names log errors only.
+ pub fn parse(spec: &str) -> Filter {
+ let mut f = Filter { default: LevelFilter::Error, directives: Vec::new() };
+ for part in spec.split(',').map(str::trim).filter(|p| !p.is_empty()) {
+ match part.split_once('=') {
+ Some((name, level)) => {
+ let name = name.trim();
+ if let (false, Some(level)) = (name.is_empty(), parse_level(level)) {
+ f.directives.push((name.to_owned(), level));
+ }
+ }
+ None => match parse_level(part) {
+ Some(level) => f.default = level,
+ // A bare target name: everything from it (env_logger does the same).
+ None => f.directives.push((part.to_owned(), LevelFilter::Trace)),
+ },
+ }
+ }
+ f
+ }
+
+ /// The level that applies to `target` (the most specific matching directive wins).
+ pub fn level_for(&self, target: &str) -> LevelFilter {
+ self.directives.iter().filter(|(name, _)| matches(name, target)).max_by_key(|(name, _)| name.len()).map_or(self.default, |(_, level)| *level)
+ }
+
+ /// The most verbose level any target can reach (for `log::set_max_level`).
+ pub fn max(&self) -> LevelFilter {
+ self.directives.iter().map(|(_, level)| *level).fold(self.default, Ord::max)
+ }
+}
+
+fn parse_level(s: &str) -> Option {
+ s.trim().parse().ok()
+}
+
+/// `name` matches `target` itself and its submodules (`a` matches `a` and `a::b`, not `ab`);
+/// `name*` matches every target starting with `name`.
+fn matches(name: &str, target: &str) -> bool {
+ match name.strip_suffix('*') {
+ Some(prefix) => target.starts_with(prefix),
+ None => target.strip_prefix(name).is_some_and(|rest| rest.is_empty() || rest.starts_with("::")),
+ }
+}
+
+/// `2026-10-08T07:59:17.728Z` (UTC, milliseconds). A clock before 1970 reads as the epoch.
+pub fn timestamp(t: SystemTime) -> String {
+ let since = t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default();
+ let secs = since.as_secs();
+ let (days, rem) = (secs / 86_400, secs % 86_400);
+ let (y, m, d) = civil_from_days(days);
+ format!("{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}.{:03}Z", rem / 3600, rem % 3600 / 60, rem % 60, since.subsec_millis())
+}
+
+/// Days since 1970-01-01 to a proleptic Gregorian (year, month, day) (Howard Hinnant's
+/// `civil_from_days`, unsigned because `timestamp` clamps at the epoch; saturating so an absurd
+/// clock can't overflow).
+fn civil_from_days(days: u64) -> (u64, u64, u64) {
+ let z = days.saturating_add(719_468);
+ let era = z / 146_097;
+ let doe = z % 146_097;
+ let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
+ let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
+ let mp = (5 * doy + 2) / 153;
+ let d = doy - (153 * mp + 2) / 5 + 1;
+ let m = if mp < 10 { mp + 3 } else { mp - 9 };
+ let y = era.saturating_mul(400).saturating_add(yoe).saturating_add(u64::from(m <= 2));
+ (y, m, d)
+}
+
+fn numbered(dir: &Path, n: usize) -> PathBuf {
+ if n == 0 { dir.join(LOG_FILE) } else { dir.join(format!("filmcraft.{n}.log")) }
+}
+
+/// Shift the previous logs up one (`.log` → `.1.log` → … → `..log`, the oldest dropped)
+/// and create a fresh, empty log file in `dir` (created if missing).
+pub fn rotate(dir: &Path) -> std::io::Result<(PathBuf, File)> {
+ std::fs::create_dir_all(dir)?;
+ for n in (1..=KEEP).rev() {
+ match std::fs::rename(numbered(dir, n - 1), numbered(dir, n)) {
+ Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e),
+ _ => {}
+ }
+ }
+ let path = numbered(dir, 0);
+ let file = File::create(&path)?;
+ Ok((path, file))
+}
+
+/// One formatted log line: ` [] : \n`.
+pub fn format_line(ts: &str, level: log::Level, thread: &str, target: &str, message: &str) -> String {
+ let message = message.strip_suffix('\n').unwrap_or(message);
+ format!("{ts} {level:<5} [{thread}] {target}: {message}\n")
+}
+
+/// Where formatted lines go besides standard error: kept in memory until a file is attached,
+/// then appended to it until it reaches its size cap.
+pub struct Sink {
+ file: Option,
+ pending: Vec,
+ dropped: usize,
+ written: u64,
+ max_bytes: u64,
+ capped: bool,
+}
+
+impl Sink {
+ pub fn new(max_bytes: u64) -> Sink {
+ Sink { file: None, pending: Vec::new(), dropped: 0, written: 0, max_bytes, capped: false }
+ }
+
+ /// Append `line` to the file, or keep it until [`Sink::attach`].
+ pub fn write(&mut self, line: &str) {
+ if self.file.is_none() {
+ if self.capped {
+ return;
+ }
+ if self.pending.len() < MAX_PENDING {
+ self.pending.push(line.to_owned());
+ } else {
+ self.dropped = self.dropped.saturating_add(1);
+ }
+ return;
+ }
+ self.append(line);
+ }
+
+ /// Start writing to `file`, first the lines kept so far.
+ pub fn attach(&mut self, file: File) {
+ self.file = Some(file);
+ for line in std::mem::take(&mut self.pending) {
+ self.append(&line);
+ }
+ if self.dropped > 0 {
+ let note = format!("{} earlier log lines were dropped before the log file was opened\n", self.dropped);
+ self.dropped = 0;
+ self.append(¬e);
+ }
+ }
+
+ /// No file will come: drop the kept lines and keep none from now on.
+ pub fn close(&mut self) {
+ self.file = None;
+ self.pending = Vec::new();
+ self.capped = true;
+ }
+
+ fn append(&mut self, line: &str) {
+ if self.capped {
+ return;
+ }
+ let Some(file) = self.file.as_mut() else { return };
+ let len = u64::try_from(line.len()).unwrap_or(u64::MAX);
+ let text = if self.written.saturating_add(len) > self.max_bytes {
+ self.capped = true;
+ format!("log file reached {} bytes; later records go to standard error only\n", self.max_bytes)
+ } else {
+ self.written = self.written.saturating_add(len);
+ line.to_owned()
+ };
+ // A full disk or a vanished file must not take the app down: drop the file sink (and
+ // stop buffering, since no file will come).
+ if file.write_all(text.as_bytes()).is_err() {
+ self.file = None;
+ self.capped = true;
+ }
+ }
+}
+
+/// The installed logger: a [`Filter`], standard error and a [`Sink`].
+pub struct AppLogger {
+ filter: Filter,
+ stderr: bool,
+ sink: Mutex,
+}
+
+impl AppLogger {
+ pub fn new(filter: Filter, stderr: bool) -> AppLogger {
+ AppLogger { filter, stderr, sink: Mutex::new(Sink::new(MAX_FILE_BYTES)) }
+ }
+
+ /// Rotate the logs in `dir` and send records to the fresh file; returns its path.
+ /// An unusable directory leaves standard error as the only sink ([`AppLogger::stderr_only`]).
+ pub fn attach_dir(&self, dir: &Path) -> Result {
+ match rotate(dir) {
+ Ok((path, file)) => {
+ self.sink.lock().unwrap_or_else(PoisonError::into_inner).attach(file);
+ Ok(path)
+ }
+ Err(e) => {
+ self.stderr_only();
+ Err(format!("{}: {e}", dir.display()))
+ }
+ }
+ }
+
+ /// No log file this run (no data directory, or it can't be used): stop keeping records for one.
+ pub fn stderr_only(&self) {
+ self.sink.lock().unwrap_or_else(PoisonError::into_inner).close();
+ }
+}
+
+impl log::Log for AppLogger {
+ fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
+ metadata.level() <= self.filter.level_for(metadata.target())
+ }
+
+ fn log(&self, record: &log::Record<'_>) {
+ if !self.enabled(record.metadata()) {
+ return;
+ }
+ // Format before taking the lock, so the lock covers one write only.
+ let thread = std::thread::current();
+ let line = format_line(×tamp(SystemTime::now()), record.level(), thread.name().unwrap_or("?"), record.target(), &record.args().to_string());
+ if self.stderr {
+ let _ = std::io::stderr().write_all(line.as_bytes());
+ }
+ self.sink.lock().unwrap_or_else(PoisonError::into_inner).write(&line);
+ }
+
+ fn flush(&self) {}
+}
+
+/// Install the logger (filter from `RUST_LOG`, else [`DEFAULT_FILTER`]); `None` when another
+/// logger was installed first. Call [`AppLogger::attach_dir`] once the data directory is known.
+pub fn install() -> Option<&'static AppLogger> {
+ static LOGGER: OnceLock = OnceLock::new();
+ let spec = std::env::var("RUST_LOG").ok().filter(|s| !s.trim().is_empty());
+ let filter = Filter::parse(spec.as_deref().unwrap_or(DEFAULT_FILTER));
+ let max = filter.max();
+ let logger = LOGGER.get_or_init(|| AppLogger::new(filter, true));
+ log::set_logger(logger).ok()?;
+ log::set_max_level(max);
+ Some(logger)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use log::Log;
+ use std::sync::atomic::{AtomicUsize, Ordering};
+ use std::time::{Duration, UNIX_EPOCH};
+
+ fn temp_dir(tag: &str) -> PathBuf {
+ static N: AtomicUsize = AtomicUsize::new(0);
+ let d = std::env::temp_dir().join(format!("filmcraft-logging-{tag}-{}-{}", std::process::id(), N.fetch_add(1, Ordering::Relaxed)));
+ let _ = std::fs::remove_dir_all(&d);
+ d
+ }
+
+ fn read(path: &Path) -> String {
+ std::fs::read_to_string(path).expect("read log")
+ }
+
+ fn record(logger: &AppLogger, level: log::Level, target: &'static str, msg: &'static str) {
+ logger.log(&log::Record::builder().level(level).target(target).args(format_args!("{msg}")).build());
+ }
+
+ #[test]
+ fn the_default_filter_shows_filmcraft_info_and_other_crates_warnings() {
+ let f = Filter::parse(DEFAULT_FILTER);
+ assert_eq!(f.level_for("filmcraft"), LevelFilter::Info);
+ assert_eq!(f.level_for("filmcraft::audio"), LevelFilter::Info);
+ assert_eq!(f.level_for("filmcraft_ui_egui::play_ahead"), LevelFilter::Info);
+ assert_eq!(f.level_for("filmcraft_codecs::mxf"), LevelFilter::Info);
+ assert_eq!(f.level_for("wgpu_core::device"), LevelFilter::Warn);
+ assert_eq!(f.level_for("naga"), LevelFilter::Warn);
+ assert_eq!(f.max(), LevelFilter::Info);
+ }
+
+ #[test]
+ fn directives_follow_env_logger_and_the_most_specific_one_wins() {
+ let f = Filter::parse("info,wgpu_core=error,filmcraft_codecs=trace,filmcraft_codecs::mxf=off");
+ assert_eq!(f.level_for("eframe"), LevelFilter::Info);
+ assert_eq!(f.level_for("wgpu_core::instance"), LevelFilter::Error);
+ assert_eq!(f.level_for("filmcraft_codecs::ogg"), LevelFilter::Trace);
+ assert_eq!(f.level_for("filmcraft_codecs::mxf"), LevelFilter::Off);
+ assert_eq!(f.max(), LevelFilter::Trace);
+ // A module name is matched at `::` boundaries, not as a bare prefix.
+ assert_eq!(f.level_for("wgpu_core_extra"), LevelFilter::Info);
+ // A trailing `*` is a prefix.
+ assert_eq!(Filter::parse("warn,filmcraft_ui*=debug").level_for("filmcraft_ui_egui::frames"), LevelFilter::Debug);
+ assert_eq!(Filter::parse("warn,filmcraft_ui*=debug").level_for("filmcraft_engine"), LevelFilter::Warn);
+ // A bare target name sets that target to the most verbose level, as env_logger does.
+ assert_eq!(Filter::parse("naga").level_for("naga::front"), LevelFilter::Trace);
+ assert_eq!(Filter::parse("naga").level_for("eframe"), LevelFilter::Error);
+ // Levels are case-insensitive; the last bare level wins.
+ assert_eq!(Filter::parse("debug,WARN").level_for("x"), LevelFilter::Warn);
+ }
+
+ #[test]
+ fn hostile_specs_never_panic_and_fall_back_sensibly() {
+ let long = "a".repeat(100_000);
+ let specs = [
+ "",
+ ",,,",
+ "=",
+ "==",
+ "=debug",
+ "filmcraft=",
+ "nonsense=loud",
+ "🦀=info",
+ "*",
+ "*=debug",
+ "a=b=c",
+ "warn,,filmcraft=DEBUG",
+ " \t ",
+ "\u{0}",
+ "é*=trace",
+ &long,
+ ];
+ for spec in specs {
+ let f = Filter::parse(spec);
+ for target in ["filmcraft", "", "🦀", "é", "a::b"] {
+ let _ = f.level_for(target);
+ }
+ let _ = f.max();
+ }
+ assert_eq!(Filter::parse("warn,,filmcraft=DEBUG").level_for("filmcraft"), LevelFilter::Debug);
+ assert_eq!(Filter::parse("nonsense=loud").level_for("nonsense"), LevelFilter::Error);
+ assert_eq!(Filter::parse("=debug").max(), LevelFilter::Error, "a directive without a name is skipped");
+ assert_eq!(Filter::parse("*=debug").level_for("anything"), LevelFilter::Debug, "a lone `*` matches every target");
+ assert_eq!(Filter::parse("é*=trace").level_for("éa"), LevelFilter::Trace);
+ }
+
+ #[test]
+ fn timestamps_are_utc_with_milliseconds() {
+ assert_eq!(timestamp(UNIX_EPOCH), "1970-01-01T00:00:00.000Z");
+ assert_eq!(timestamp(UNIX_EPOCH + Duration::from_millis(1_791_446_357_728)), "2026-10-08T07:59:17.728Z");
+ // Leap day, and the day after it.
+ assert_eq!(timestamp(UNIX_EPOCH + Duration::from_secs(1_709_164_800)), "2024-02-29T00:00:00.000Z");
+ assert_eq!(timestamp(UNIX_EPOCH + Duration::from_secs(1_709_251_199)), "2024-02-29T23:59:59.000Z");
+ assert_eq!(timestamp(UNIX_EPOCH + Duration::from_secs(1_709_251_200)), "2024-03-01T00:00:00.000Z");
+ // 2000 is a leap year (divisible by 400), 2100 is not.
+ assert_eq!(timestamp(UNIX_EPOCH + Duration::from_secs(951_782_400)), "2000-02-29T00:00:00.000Z");
+ assert_eq!(timestamp(UNIX_EPOCH + Duration::from_secs(4_107_542_400)), "2100-03-01T00:00:00.000Z");
+ // Before the epoch (a clock set wrong) clamps instead of panicking.
+ assert_eq!(timestamp(UNIX_EPOCH - Duration::from_secs(5)), "1970-01-01T00:00:00.000Z");
+ }
+
+ #[test]
+ fn rotation_keeps_the_previous_logs_and_starts_an_empty_file() {
+ let dir = temp_dir("rotate");
+ for run in 1..=4 {
+ let (path, _file) = rotate(&dir).expect("rotate");
+ assert_eq!(path, dir.join(LOG_FILE));
+ assert_eq!(read(&path), "");
+ std::fs::write(&path, format!("run {run}")).expect("write");
+ }
+ assert_eq!(read(&dir.join(LOG_FILE)), "run 4");
+ assert_eq!(read(&dir.join("filmcraft.1.log")), "run 3");
+ assert_eq!(read(&dir.join("filmcraft.2.log")), "run 2");
+ assert!(!dir.join("filmcraft.3.log").exists(), "only {KEEP} old logs are kept");
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn rotation_into_an_unusable_directory_is_an_error_not_a_panic() {
+ let dir = temp_dir("blocked");
+ std::fs::create_dir_all(dir.parent().expect("parent")).expect("tmp");
+ std::fs::write(&dir, "a file where the directory should be").expect("block");
+ assert!(rotate(&dir).is_err());
+ assert!(rotate(&dir.join("below")).is_err());
+ let _ = std::fs::remove_file(&dir);
+ }
+
+ #[test]
+ fn a_line_carries_time_level_thread_target_and_message() {
+ assert_eq!(
+ format_line("2026-10-08T07:59:17.728Z", log::Level::Warn, "main", "filmcraft_ui_egui", "audio output unavailable: no output device"),
+ "2026-10-08T07:59:17.728Z WARN [main] filmcraft_ui_egui: audio output unavailable: no output device\n"
+ );
+ // A multi-line message keeps its lines; the record still ends in exactly one newline.
+ assert!(format_line("t", log::Level::Error, "w", "x", "a\nb\n").ends_with("x: a\nb\n"));
+ }
+
+ #[test]
+ fn lines_logged_before_the_file_exists_are_written_when_it_is_attached() {
+ let dir = temp_dir("pending");
+ let (path, file) = rotate(&dir).expect("rotate");
+ let mut sink = Sink::new(MAX_FILE_BYTES);
+ sink.write("early 1\n");
+ sink.write("early 2\n");
+ sink.attach(file);
+ sink.write("late\n");
+ assert_eq!(read(&path), "early 1\nearly 2\nlate\n");
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn the_pending_buffer_is_bounded_and_says_how_much_it_dropped() {
+ let dir = temp_dir("bounded");
+ let (path, file) = rotate(&dir).expect("rotate");
+ let mut sink = Sink::new(MAX_FILE_BYTES);
+ for i in 0..MAX_PENDING + 5 {
+ sink.write(&format!("line {i}\n"));
+ }
+ sink.attach(file);
+ let text = read(&path);
+ assert_eq!(text.lines().filter(|l| l.starts_with("line ")).count(), MAX_PENDING);
+ assert_eq!(text.matches("5 earlier log lines were dropped").count(), 1, "{text}");
+ assert!(text.contains("line 0\n") && !text.contains(&format!("line {MAX_PENDING}\n")));
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn exactly_max_pending_lines_drop_nothing() {
+ let dir = temp_dir("exact");
+ let (path, file) = rotate(&dir).expect("rotate");
+ let mut sink = Sink::new(MAX_FILE_BYTES);
+ for i in 0..MAX_PENDING {
+ sink.write(&format!("line {i}\n"));
+ }
+ sink.attach(file);
+ let text = read(&path);
+ assert_eq!(text.lines().count(), MAX_PENDING);
+ assert!(!text.contains("dropped"), "{text}");
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn the_file_stops_at_its_size_cap_with_one_note() {
+ let dir = temp_dir("cap");
+ let (path, file) = rotate(&dir).expect("rotate");
+ let mut sink = Sink::new(100);
+ sink.attach(file);
+ for i in 0..50 {
+ sink.write(&format!("line {i:02} with some padding\n"));
+ }
+ let text = read(&path);
+ assert_eq!(text.lines().filter(|l| l.starts_with("line ")).count(), 3, "{text}");
+ assert_eq!(text.matches("log file reached 100 bytes").count(), 1, "{text}");
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn the_logger_filters_records_and_writes_them_to_the_attached_file() {
+ let dir = temp_dir("logger");
+ let logger = AppLogger::new(Filter::parse(DEFAULT_FILTER), false);
+ record(&logger, log::Level::Info, "filmcraft", "FilmCraft starting");
+ record(&logger, log::Level::Info, "wgpu_core::device", "too chatty");
+ let path = logger.attach_dir(&dir).expect("attach");
+ assert_eq!(path, dir.join(LOG_FILE));
+ record(&logger, log::Level::Warn, "wgpu_hal::vulkan", "a real warning");
+ record(&logger, log::Level::Debug, "filmcraft", "below info");
+ let text = read(&path);
+ let lines: Vec<&str> = text.lines().collect();
+ assert_eq!(lines.len(), 2, "{text}");
+ assert!(lines.first().is_some_and(|l| l.contains(" INFO [") && l.ends_with("filmcraft: FilmCraft starting")), "{text}");
+ assert!(lines.get(1).is_some_and(|l| l.ends_with("wgpu_hal::vulkan: a real warning")), "{text}");
+ assert!(logger.enabled(&log::Metadata::builder().level(log::Level::Info).target("filmcraft_engine::autosave").build()));
+ assert!(!logger.enabled(&log::Metadata::builder().level(log::Level::Info).target("naga").build()));
+ // A second launch rotates: the first log becomes `.1`.
+ let second = AppLogger::new(Filter::parse(DEFAULT_FILTER), false);
+ assert_eq!(second.attach_dir(&dir).expect("attach again"), dir.join(LOG_FILE));
+ assert_eq!(read(&dir.join("filmcraft.1.log")), text);
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn records_from_many_threads_are_whole_lines() {
+ let dir = temp_dir("threads");
+ let logger = AppLogger::new(Filter::parse("info"), false);
+ let path = logger.attach_dir(&dir).expect("attach");
+ std::thread::scope(|s| {
+ for _ in 0..4 {
+ s.spawn(|| {
+ for _ in 0..100 {
+ record(&logger, log::Level::Info, "filmcraft", "from a worker");
+ }
+ });
+ }
+ });
+ let text = read(&path);
+ assert_eq!(text.lines().count(), 400);
+ assert!(text.lines().all(|l| l.ends_with("filmcraft: from a worker")), "{text}");
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn attaching_to_an_unusable_directory_reports_an_error_and_keeps_logging() {
+ let dir = temp_dir("unusable");
+ std::fs::write(&dir, "not a directory").expect("block");
+ let logger = AppLogger::new(Filter::parse("info"), false);
+ let e = logger.attach_dir(&dir).expect_err("a file is not a log directory");
+ assert!(e.contains(&dir.display().to_string()), "{e}");
+ record(&logger, log::Level::Error, "x", "still fine");
+ let sink = logger.sink.lock().expect("sink");
+ assert!(sink.file.is_none() && sink.pending.is_empty(), "nothing is kept for a file that will not come");
+ drop(sink);
+ let _ = std::fs::remove_file(&dir);
+ }
+
+ #[test]
+ fn a_poisoned_lock_does_not_stop_logging() {
+ let dir = temp_dir("poison");
+ let logger = AppLogger::new(Filter::parse("info"), false);
+ let path = logger.attach_dir(&dir).expect("attach");
+ let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ let _guard = logger.sink.lock();
+ panic!("poison the sink");
+ }));
+ assert!(logger.sink.is_poisoned());
+ record(&logger, log::Level::Info, "filmcraft", "after the panic");
+ assert!(read(&path).contains("after the panic"));
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+}
diff --git a/apps/filmcraft/src/main.rs b/apps/filmcraft/src/main.rs
index b9b279a12..18fb4d7b0 100644
--- a/apps/filmcraft/src/main.rs
+++ b/apps/filmcraft/src/main.rs
@@ -16,6 +16,9 @@
//! with unsaved changes the app asks to recover them; `--recover` recovers the newest without
//! asking, `--no-recover` starts without asking (the changes stay available via File ▸ Recover
//! Unsaved Changes…).
+//!
+//! `log` records go to standard error and `/Logs/filmcraft.log` (`RUST_LOG` sets the
+//! levels); see [`logging`].
#![cfg_attr(all(target_os = "windows", not(debug_assertions)), windows_subsystem = "windows")]
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]
@@ -24,6 +27,9 @@ mod args;
mod audio;
mod audio_in;
mod control_server;
+#[cfg(any(target_os = "windows", test))]
+mod graphics;
+mod logging;
#[cfg(target_os = "macos")]
mod native_menu;
mod window_raise;
@@ -66,6 +72,8 @@ fn tell(text: &str, error: bool) {
}
fn main() -> eframe::Result {
+ // First, so the panic hook and every start-up record are logged (`logging`).
+ let logger = logging::install();
// lossy, so a value that is not Unicode is reported like any other bad port instead of ignored
let env_port = std::env::var_os("FILMCRAFT_CONTROL_PORT").map(|p| p.to_string_lossy().into_owned());
let Launch { control_port, files, demo, startup_flag, recover, data_dir } = match args::parse(env_port, std::env::args().skip(1)) {
@@ -83,10 +91,24 @@ fn main() -> eframe::Result {
std::process::exit(2);
}
};
+ let log_dir = data_dir.clone().or_else(default_data_dir).map(|d| d.join(logging::LOG_DIR));
+ log::info!("FilmCraft {} ({} {})", env!("CARGO_PKG_VERSION"), std::env::consts::OS, std::env::consts::ARCH);
+ // The log file lives next to the crash logs; opened after the arguments, so `--help`,
+ // `--version` and usage errors leave no file behind. Records logged until now go in first.
+ if let Some(logger) = logger {
+ match log_dir.as_deref().map(|d| logger.attach_dir(d)) {
+ Some(Ok(path)) => log::info!("log file {}", path.display()),
+ Some(Err(e)) => log::warn!("no log file: {e}"),
+ None => {
+ logger.stderr_only();
+ log::warn!("no log file: no data directory (set FILMCRAFT_DATA_DIR or --data-dir)");
+ }
+ }
+ }
app_nap::disable();
// Panics anywhere go to /Logs/crash-.log with a backtrace; the UI pass and
// frame workers catch them and keep running (see filmcraft_ui_egui::crash).
- filmcraft_ui_egui::crash::install(data_dir.clone().or_else(default_data_dir).map(|d| d.join("Logs")));
+ filmcraft_ui_egui::crash::install(log_dir);
// OS hardware video decoders (VideoToolbox on macOS) in front of our own; Settings ▸ Playback ▸
// Hardware decoding switches them off. Unsupported streams and failures use our decoders.
register_hardware_decoders();
@@ -107,6 +129,13 @@ fn main() -> eframe::Result {
event_loop_builder: agent_event_loop(control_port.is_some()),
..Default::default()
};
+ // Before eframe creates the wgpu instance: leave OpenGL out on Windows (see graphics.rs).
+ #[cfg(target_os = "windows")]
+ let options = {
+ let mut options = options;
+ graphics::configure(&mut options, eframe::wgpu::Backends::from_env());
+ options
+ };
let started = eframe::run_native(
"FilmCraft",
options,
@@ -116,7 +145,7 @@ fn main() -> eframe::Result {
let mut cfg = AutosaveConfig::new(dir);
cfg.local_offset = local_offset;
if let Err(e) = session.start_autosave(cfg) {
- eprintln!("filmcraft: auto-save and crash recovery unavailable: {e}");
+ log::warn!("auto-save and crash recovery unavailable: {e}");
}
}
// voice-over recording reads the microphone through cpal
@@ -124,7 +153,7 @@ fn main() -> eframe::Result {
let project = files.iter().find(|f| f.ends_with(".fcproj")).cloned();
if let Some(p) = project {
if let Err(e) = session.execute("file.open", json!({"path": p})) {
- eprintln!("filmcraft: {e}");
+ log::error!("{e}");
}
} else if !startup_flag && session.prefs.general.at_startup == "openMostRecent" {
// Settings ▸ General ▸ At Startup ▸ Open Most Recent
@@ -132,7 +161,7 @@ fn main() -> eframe::Result {
match recent {
Some(p) => {
if let Err(e) = session.execute("file.open", json!({"path": p})) {
- eprintln!("filmcraft: {e}");
+ log::error!("{e}");
}
}
None => {
@@ -150,8 +179,8 @@ fn main() -> eframe::Result {
if recover == Some(true) && !session.recovery_candidates().is_empty() {
let id = session.recovery_candidates()[0].id.clone();
match session.execute("file.recover", json!({"id": id})) {
- Ok(r) => eprintln!("filmcraft: recovered {r}"),
- Err(e) => eprintln!("filmcraft: recovery failed: {e}"),
+ Ok(r) => log::info!("recovered {r}"),
+ Err(e) => log::error!("recovery failed: {e}"),
}
}
let mut app = FilmcraftApp::new(session);
@@ -168,27 +197,50 @@ fn main() -> eframe::Result {
// Settings ▸ Audio Hardware is applied on the first frame (`apply_prefs`).
app.audio = Some(Box::new(audio::CpalOut::new()));
app.hooks.pick_files = Some(Box::new(|exts: &[&str]| {
- rfd::FileDialog::new().add_filter("Media", exts).pick_files().unwrap_or_default().into_iter().map(|p| p.to_string_lossy().to_string()).collect()
+ rfd::FileDialog::new()
+ .add_filter(filmcraft_ui_egui::i18n::t("Media"), exts)
+ .pick_files()
+ .unwrap_or_default()
+ .into_iter()
+ .map(|p| p.to_string_lossy().to_string())
+ .collect()
}));
// Link Media ▸ Locate…, Attach Proxies, Reconnect Full Resolution: one path, not imported.
- app.hooks.pick_file_for_relink =
- Some(Box::new(|exts: &[&str], _hint| rfd::FileDialog::new().add_filter("Media", exts).pick_file().map(|p| p.to_string_lossy().to_string())));
+ app.hooks.pick_file_for_relink = Some(Box::new(|exts: &[&str], _hint| {
+ rfd::FileDialog::new().add_filter(filmcraft_ui_egui::i18n::t("Media"), exts).pick_file().map(|p| p.to_string_lossy().to_string())
+ }));
app.hooks.pick_save = Some(Box::new(|name: &str| {
- rfd::FileDialog::new().add_filter("FilmCraft Project", &["fcproj"]).set_file_name(name).save_file().map(|p| p.to_string_lossy().to_string())
+ rfd::FileDialog::new()
+ .add_filter(filmcraft_ui_egui::i18n::t("FilmCraft Project"), &["fcproj"])
+ .set_file_name(name)
+ .save_file()
+ .map(|p| p.to_string_lossy().to_string())
}));
app.hooks.pick_save_as = Some(Box::new(|filter: &str, exts: &[&str], name: &str| {
- rfd::FileDialog::new().add_filter(filter, exts).set_file_name(name).save_file().map(|p| p.to_string_lossy().to_string())
+ rfd::FileDialog::new()
+ .add_filter(filmcraft_ui_egui::i18n::t(filter), exts)
+ .set_file_name(name)
+ .save_file()
+ .map(|p| p.to_string_lossy().to_string())
}));
app.hooks.pick_folder = Some(Box::new(|| rfd::FileDialog::new().pick_folder().map(|p| p.to_string_lossy().to_string())));
+ app.hooks.pick_folder_at =
+ Some(Box::new(|dir: &str| rfd::FileDialog::new().set_directory(dir).pick_folder().map(|p| p.to_string_lossy().into_owned())));
app.hooks.open_path = Some(Box::new(open_path));
+ // Settings ▸ General ▸ Interface Language ▸ System Language (#218).
+ app.hooks.system_languages = Some(Box::new(|| sys_locale::get_locales().collect()));
app.hooks.raise_without_focus = Some(Box::new(|| {
window_raise::raise_without_focus();
}));
app.hooks.pick_open_file = Some(Box::new(|filter: &str, exts: &[&str]| {
- rfd::FileDialog::new().add_filter(filter, exts).pick_file().map(|p| p.to_string_lossy().to_string())
+ rfd::FileDialog::new().add_filter(filmcraft_ui_egui::i18n::t(filter), exts).pick_file().map(|p| p.to_string_lossy().to_string())
+ }));
+ app.hooks.pick_open_project = Some(Box::new(|| {
+ rfd::FileDialog::new()
+ .add_filter(filmcraft_ui_egui::i18n::t("FilmCraft Project"), &["fcproj"])
+ .pick_file()
+ .map(|p| p.to_string_lossy().to_string())
}));
- app.hooks.pick_open_project =
- Some(Box::new(|| rfd::FileDialog::new().add_filter("FilmCraft Project", &["fcproj"]).pick_file().map(|p| p.to_string_lossy().to_string())));
#[cfg(target_os = "macos")]
{
let (rx, update) = native_menu::install(&app, cc.egui_ctx.clone());
@@ -207,7 +259,7 @@ fn main() -> eframe::Result {
// instead of exiting silently, and keep the reason in the crash log.
if let Err(e) = &started {
let msg = format!("FilmCraft could not start its window: {e}\n\nUpdating the graphics driver usually fixes this.");
- eprintln!("filmcraft: {msg}");
+ log::error!("{msg}");
filmcraft_ui_egui::crash::record(&msg);
let _ = rfd::MessageDialog::new().set_title("FilmCraft").set_description(&msg).set_level(rfd::MessageLevel::Error).show();
}
@@ -277,6 +329,9 @@ mod tests {
fn startup_registers_the_hardware_decoders_without_a_logger() {
assert!(!log::log_enabled!(log::Level::Info));
let hardware = super::register_hardware_decoders();
- assert_eq!(filmcraft_platform::registered(), cfg!(any(target_os = "macos", target_os = "windows")), "{hardware:?}");
+ // always on macOS and Windows; on Linux when a VA-API driver is there
+ let expected = cfg!(any(target_os = "macos", target_os = "windows"))
+ || (cfg!(target_os = "linux") && matches!(hardware, filmcraft_platform::Availability::Available(_)));
+ assert_eq!(filmcraft_platform::registered(), expected, "{hardware:?}");
}
}
diff --git a/apps/filmcraft/src/native_menu.rs b/apps/filmcraft/src/native_menu.rs
index 93601b7d0..c63f8295b 100644
--- a/apps/filmcraft/src/native_menu.rs
+++ b/apps/filmcraft/src/native_menu.rs
@@ -99,6 +99,8 @@ pub fn install(app: &FilmcraftApp, ctx: egui::Context) -> (Receiver, Sho
filmcraft_ui_egui::i18n::Language::Es
} else if checked("app.language.portuguese") {
filmcraft_ui_egui::i18n::Language::PtBr
+ } else if checked("app.language.ukrainian") {
+ filmcraft_ui_egui::i18n::Language::Uk
} else {
filmcraft_ui_egui::i18n::Language::En
};
diff --git a/contributors/contributors.json b/contributors/contributors.json
index 798be44a9..29b65c4c4 100644
--- a/contributors/contributors.json
+++ b/contributors/contributors.json
@@ -2,49 +2,49 @@
"schema": 1,
"repo": "storytold/filmcraft",
"branch": "main",
- "head": "a4536bcfedf588cbef9ea24df149c70bf99a037d",
- "head_date": "2026-10-07T21:56:33Z",
- "commits": 425,
+ "head": "7b6c134472287db4367fca80bcb498240ba36567",
+ "head_date": "2026-10-09T15:02:38Z",
+ "commits": 539,
"unattributed_commits": 0,
"contributors": [
{
"login": "echelon",
"people_entry": true,
"github_name": null,
- "prs": 39,
- "commits": 312,
- "lines_added": 309158,
- "lines_deleted": 12778,
+ "prs": 48,
+ "commits": 337,
+ "lines_added": 311167,
+ "lines_deleted": 13200,
"binary_added": 74,
"binary_deleted": 0,
"first_commit": "2026-09-30T09:22:00Z",
- "last_commit": "2026-10-07T21:56:33Z"
+ "last_commit": "2026-10-09T15:02:38Z"
},
{
"login": "storybored379",
"people_entry": false,
"github_name": null,
- "prs": 11,
- "commits": 13,
- "lines_added": 1501,
- "lines_deleted": 87,
+ "prs": 15,
+ "commits": 17,
+ "lines_added": 1960,
+ "lines_deleted": 179,
"binary_added": 0,
"binary_deleted": 0,
"first_commit": "2026-10-03T16:31:42Z",
- "last_commit": "2026-10-05T04:48:25Z"
+ "last_commit": "2026-10-09T04:35:58Z"
},
{
"login": "justjohn12345",
"people_entry": false,
"github_name": null,
- "prs": 13,
- "commits": 35,
- "lines_added": 8315,
- "lines_deleted": 839,
+ "prs": 15,
+ "commits": 37,
+ "lines_added": 9087,
+ "lines_deleted": 938,
"binary_added": 0,
"binary_deleted": 0,
"first_commit": "2026-10-05T19:57:28Z",
- "last_commit": "2026-10-07T17:24:43Z"
+ "last_commit": "2026-10-08T17:06:13Z"
},
{
"login": "punkhop",
@@ -63,14 +63,14 @@
"login": "midasdf",
"people_entry": false,
"github_name": null,
- "prs": 2,
- "commits": 2,
- "lines_added": 480,
- "lines_deleted": 24,
+ "prs": 4,
+ "commits": 4,
+ "lines_added": 618,
+ "lines_deleted": 32,
"binary_added": 0,
"binary_deleted": 0,
"first_commit": "2026-10-06T12:41:17Z",
- "last_commit": "2026-10-07T21:13:41Z"
+ "last_commit": "2026-10-08T15:40:01Z"
},
{
"login": "EthanSuquet",
@@ -89,14 +89,14 @@
"login": "dcarnelutti",
"people_entry": false,
"github_name": null,
- "prs": 5,
- "commits": 5,
- "lines_added": 3815,
- "lines_deleted": 229,
+ "prs": 7,
+ "commits": 7,
+ "lines_added": 4069,
+ "lines_deleted": 240,
"binary_added": 0,
"binary_deleted": 0,
"first_commit": "2026-10-06T21:04:48Z",
- "last_commit": "2026-10-07T20:00:08Z"
+ "last_commit": "2026-10-08T16:17:58Z"
},
{
"login": "mweinbach",
@@ -154,40 +154,40 @@
"login": "carlitosteso3",
"people_entry": false,
"github_name": null,
- "prs": 8,
- "commits": 9,
- "lines_added": 1196,
- "lines_deleted": 141,
+ "prs": 15,
+ "commits": 15,
+ "lines_added": 1740,
+ "lines_deleted": 207,
"binary_added": 0,
"binary_deleted": 0,
"first_commit": "2026-10-07T03:06:02Z",
- "last_commit": "2026-10-07T17:37:50Z"
+ "last_commit": "2026-10-09T14:21:09Z"
},
{
"login": "SaymonMaster14",
"people_entry": false,
"github_name": null,
- "prs": 3,
- "commits": 6,
- "lines_added": 5514,
- "lines_deleted": 296,
+ "prs": 8,
+ "commits": 14,
+ "lines_added": 10039,
+ "lines_deleted": 651,
"binary_added": 0,
"binary_deleted": 0,
"first_commit": "2026-10-07T06:59:57Z",
- "last_commit": "2026-10-07T18:42:38Z"
+ "last_commit": "2026-10-09T07:32:43Z"
},
{
"login": "Luna161",
"people_entry": false,
"github_name": null,
- "prs": 2,
- "commits": 2,
- "lines_added": 65,
- "lines_deleted": 1,
+ "prs": 3,
+ "commits": 3,
+ "lines_added": 195,
+ "lines_deleted": 9,
"binary_added": 0,
"binary_deleted": 0,
"first_commit": "2026-10-07T18:20:23Z",
- "last_commit": "2026-10-07T20:21:49Z"
+ "last_commit": "2026-10-08T00:35:58Z"
},
{
"login": "RicSchonfelder",
@@ -214,6 +214,305 @@
"binary_deleted": 0,
"first_commit": "2026-10-07T20:35:41Z",
"last_commit": "2026-10-07T20:35:41Z"
+ },
+ {
+ "login": "cirocueto2013-wq",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 3,
+ "commits": 3,
+ "lines_added": 1127,
+ "lines_deleted": 226,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-07T22:56:58Z",
+ "last_commit": "2026-10-07T23:50:33Z"
+ },
+ {
+ "login": "DWAA1660",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 2,
+ "commits": 2,
+ "lines_added": 209,
+ "lines_deleted": 0,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-07T22:57:38Z",
+ "last_commit": "2026-10-08T23:52:30Z"
+ },
+ {
+ "login": "txiki739",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 5,
+ "commits": 8,
+ "lines_added": 7976,
+ "lines_deleted": 3333,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-07T23:41:48Z",
+ "last_commit": "2026-10-09T09:28:24Z"
+ },
+ {
+ "login": "wstock",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 5,
+ "commits": 5,
+ "lines_added": 288,
+ "lines_deleted": 24,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T07:45:37Z",
+ "last_commit": "2026-10-08T12:14:46Z"
+ },
+ {
+ "login": "WavJaby",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 6,
+ "commits": 6,
+ "lines_added": 1090,
+ "lines_deleted": 110,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T08:18:07Z",
+ "last_commit": "2026-10-09T05:47:06Z"
+ },
+ {
+ "login": "MrCloudy2",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 1,
+ "commits": 1,
+ "lines_added": 132,
+ "lines_deleted": 4,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T08:53:54Z",
+ "last_commit": "2026-10-08T08:53:54Z"
+ },
+ {
+ "login": "switch87",
+ "people_entry": true,
+ "github_name": null,
+ "prs": 2,
+ "commits": 3,
+ "lines_added": 629,
+ "lines_deleted": 9,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T09:25:54Z",
+ "last_commit": "2026-10-08T09:37:02Z"
+ },
+ {
+ "login": "h-a-n-a-s-h-i",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 0,
+ "commits": 2,
+ "lines_added": 96,
+ "lines_deleted": 96,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T12:12:49Z",
+ "last_commit": "2026-10-08T14:01:39Z"
+ },
+ {
+ "login": "XusBadia",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 1,
+ "commits": 1,
+ "lines_added": 260,
+ "lines_deleted": 1,
+ "binary_added": 2,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T13:12:32Z",
+ "last_commit": "2026-10-08T13:12:32Z"
+ },
+ {
+ "login": "MarekSpodBiedry",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 2,
+ "commits": 2,
+ "lines_added": 74,
+ "lines_deleted": 6,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T13:44:56Z",
+ "last_commit": "2026-10-09T08:45:28Z"
+ },
+ {
+ "login": "Tom1tk",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 2,
+ "commits": 2,
+ "lines_added": 47,
+ "lines_deleted": 26,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T13:47:40Z",
+ "last_commit": "2026-10-08T14:56:39Z"
+ },
+ {
+ "login": "Pathos0925",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 1,
+ "commits": 1,
+ "lines_added": 68,
+ "lines_deleted": 8,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T17:09:15Z",
+ "last_commit": "2026-10-08T17:09:15Z"
+ },
+ {
+ "login": "mjohnson7cd",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 2,
+ "commits": 1,
+ "lines_added": 100,
+ "lines_deleted": 2,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T20:36:28Z",
+ "last_commit": "2026-10-08T20:36:28Z"
+ },
+ {
+ "login": "Oldrich333",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 0,
+ "commits": 1,
+ "lines_added": 119,
+ "lines_deleted": 9,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T20:58:16Z",
+ "last_commit": "2026-10-08T20:58:16Z"
+ },
+ {
+ "login": "cshapiro",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 3,
+ "commits": 5,
+ "lines_added": 3128,
+ "lines_deleted": 111,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-08T23:28:32Z",
+ "last_commit": "2026-10-09T02:57:30Z"
+ },
+ {
+ "login": "rossaai",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 1,
+ "commits": 3,
+ "lines_added": 112,
+ "lines_deleted": 10,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-09T00:57:43Z",
+ "last_commit": "2026-10-09T00:57:43Z"
+ },
+ {
+ "login": "wisegamesyt-droid",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 1,
+ "commits": 2,
+ "lines_added": 758,
+ "lines_deleted": 275,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-09T01:11:12Z",
+ "last_commit": "2026-10-09T02:33:35Z"
+ },
+ {
+ "login": "JordyUSA",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 2,
+ "commits": 3,
+ "lines_added": 4158,
+ "lines_deleted": 283,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-09T01:35:57Z",
+ "last_commit": "2026-10-09T02:53:28Z"
+ },
+ {
+ "login": "jerrykimo",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 1,
+ "commits": 1,
+ "lines_added": 117,
+ "lines_deleted": 6,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-09T01:50:09Z",
+ "last_commit": "2026-10-09T01:50:09Z"
+ },
+ {
+ "login": "swillunderscore",
+ "people_entry": true,
+ "github_name": null,
+ "prs": 1,
+ "commits": 3,
+ "lines_added": 1008,
+ "lines_deleted": 13,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-09T01:58:37Z",
+ "last_commit": "2026-10-09T02:19:21Z"
+ },
+ {
+ "login": "IppSec",
+ "people_entry": false,
+ "github_name": null,
+ "prs": 1,
+ "commits": 2,
+ "lines_added": 486,
+ "lines_deleted": 54,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-09T02:29:23Z",
+ "last_commit": "2026-10-09T02:30:49Z"
+ },
+ {
+ "login": "sanyamsingh001",
+ "people_entry": true,
+ "github_name": null,
+ "prs": 2,
+ "commits": 2,
+ "lines_added": 323,
+ "lines_deleted": 25,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-09T04:06:37Z",
+ "last_commit": "2026-10-09T05:44:39Z"
+ },
+ {
+ "login": "ItzBloxy",
+ "people_entry": true,
+ "github_name": null,
+ "prs": 1,
+ "commits": 4,
+ "lines_added": 887,
+ "lines_deleted": 167,
+ "binary_added": 0,
+ "binary_deleted": 0,
+ "first_commit": "2026-10-09T09:27:48Z",
+ "last_commit": "2026-10-09T10:17:08Z"
}
],
"models": [
@@ -222,18 +521,45 @@
"company": "Anthropic",
"model": "Claude Opus",
"version": "5.5",
- "commits": 400,
- "lines_added": 324452,
- "lines_deleted": 14207
+ "commits": 474,
+ "lines_added": 346706,
+ "lines_deleted": 19057
},
{
"id": "anthropic/claude-sonnet/5.5",
"company": "Anthropic",
"model": "Claude Sonnet",
"version": "5.5",
- "commits": 10,
- "lines_added": 9328,
- "lines_deleted": 524
+ "commits": 18,
+ "lines_added": 13935,
+ "lines_deleted": 854
+ },
+ {
+ "id": "openai/codex",
+ "company": "OpenAI",
+ "model": "Codex",
+ "version": "",
+ "commits": 11,
+ "lines_added": 2057,
+ "lines_deleted": 361
+ },
+ {
+ "id": "anthropic/claude-fable/5.1",
+ "company": "Anthropic",
+ "model": "Claude Fable",
+ "version": "5.1",
+ "commits": 5,
+ "lines_added": 322,
+ "lines_deleted": 32
+ },
+ {
+ "id": "anthropic/claude",
+ "company": "Anthropic",
+ "model": "Claude",
+ "version": "",
+ "commits": 1,
+ "lines_added": 235,
+ "lines_deleted": 22
},
{
"id": "anthropic/claude-opus/4.8",
@@ -243,15 +569,6 @@
"commits": 1,
"lines_added": 1632,
"lines_deleted": 3
- },
- {
- "id": "openai/codex",
- "company": "OpenAI",
- "model": "Codex",
- "version": "",
- "commits": 1,
- "lines_added": 70,
- "lines_deleted": 15
}
]
}
diff --git a/contributors/people.toml b/contributors/people.toml
index 89c6db208..9762e2ac7 100644
--- a/contributors/people.toml
+++ b/contributors/people.toml
@@ -21,3 +21,29 @@
real_name = "Brandon Thomas"
display_name = "Brandon Thomas"
sync_github_name = false
+
+[people.sanyamsingh001]
+display_name = "sanyamsingh001"
+sync_github_name = false
+
+[people.switch87]
+real_name = "Gert Pellin"
+display_name = "Gert Pellin"
+sync_github_name = false
+
+[people.swillunderscore]
+display_name = "Swill Software"
+
+
+[people.IppSec]
+sync_github_name = true
+
+[people.ItzBloxy]
+display_name = "Bloxy"
+
+[people.ReReddo]
+display_name = "ReReddo"
+sync_github_name = false
+
+[people.liangforstudy]
+sync_github_name = true
diff --git a/crates/automation/src/long_job.rs b/crates/automation/src/long_job.rs
index d03c40673..8d4035ab3 100644
--- a/crates/automation/src/long_job.rs
+++ b/crates/automation/src/long_job.rs
@@ -107,10 +107,11 @@ impl FilmcraftMcp {
}
/// Delete what an interrupted export wrote, and nothing else: the output file itself, the numbered
-/// stills of an image sequence (`.`) and the caption sidecar
+/// stills of an image sequence (`<3+ digits>.`, only when the output is a
+/// PNG/TIFF/BMP sequence) and the caption sidecar
/// (`.srt` / `.vtt`), each only if it was written since `since`. Other files that
-/// merely share the name's stem (`.aep`, `2.psd`, …) are never touched, even when they
-/// were saved during the export.
+/// merely share the name's stem (`.aep`, `2.psd`, `1.mp4` beside a movie, …) are
+/// never touched, even when they were saved during the export.
pub fn remove_partial(path: &str, since: SystemTime) {
let p = Path::new(path);
let (Some(dir), Some(stem), Some(file)) = (p.parent(), p.file_stem().and_then(|s| s.to_str()), p.file_name().and_then(|s| s.to_str())) else {
@@ -139,9 +140,19 @@ fn is_export_output(name: &str, file: &str, stem: &str, ext: &str) -> bool {
if let Some(sidecar) = rest.strip_prefix('.') {
return sidecar.eq_ignore_ascii_case("srt") || sidecar.eq_ignore_ascii_case("vtt");
}
- // image sequence: one or more digits, then the output's own extension
+ // image sequence: the frame number (at least three digits, like `image_sequence_path`), then the
+ // output's own extension; movies and audio are a single file, so numbered neighbours are not theirs
+ if !is_sequence_extension(ext) {
+ return false;
+ }
let digits = rest.len() - rest.trim_start_matches(|c: char| c.is_ascii_digit()).len();
- digits > 0 && !ext.is_empty() && rest.get(digits..).is_some_and(|r| r.strip_prefix('.').is_some_and(|x| x == ext))
+ digits >= 3 && rest.get(digits..).is_some_and(|r| r.strip_prefix('.').is_some_and(|x| x == ext))
+}
+
+/// Whether an export to a file with extension `ext` writes an image sequence (one still per frame).
+fn is_sequence_extension(ext: &str) -> bool {
+ use filmcraft_engine::export::Format;
+ Format::from_name(ext).is_some_and(|f| matches!(f, Format::PngSequence | Format::TiffSequence | Format::BmpSequence))
}
#[cfg(test)]
@@ -170,6 +181,50 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
+ #[test]
+ fn numbered_files_beside_a_movie_are_kept() {
+ let dir = std::env::temp_dir().join(format!("fc-remove-partial-movie-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+ let started = SystemTime::now();
+ // a movie export writes one file: `clip1.mp4` and `clip002.mp4` are other exports
+ let ours = ["clip.mp4", "clip.srt"];
+ let theirs = ["clip1.mp4", "clip002.mp4", "clip2.mov"];
+ for f in ours.iter().chain(theirs.iter()) {
+ std::fs::write(dir.join(f), b"x").unwrap();
+ }
+ remove_partial(&dir.join("clip.mp4").to_string_lossy(), started);
+ for f in ours {
+ assert!(!dir.join(f).exists(), "{f} should be removed");
+ }
+ for f in theirs {
+ assert!(dir.join(f).exists(), "{f} must not be removed");
+ }
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
+ #[test]
+ fn sequence_frames_have_at_least_three_digits() {
+ let dir = std::env::temp_dir().join(format!("fc-remove-partial-digits-{}", std::process::id()));
+ let _ = std::fs::remove_dir_all(&dir);
+ std::fs::create_dir_all(&dir).unwrap();
+ let started = SystemTime::now();
+ // frames are numbered with at least three digits (`shot000.tiff`); `shot2.tiff` is not one
+ let ours = ["shot.tiff", "shot000.tiff", "shot1234.tiff"];
+ let theirs = ["shot2.tiff", "shot12.tiff"];
+ for f in ours.iter().chain(theirs.iter()) {
+ std::fs::write(dir.join(f), b"x").unwrap();
+ }
+ remove_partial(&dir.join("shot.tiff").to_string_lossy(), started);
+ for f in ours {
+ assert!(!dir.join(f).exists(), "{f} should be removed");
+ }
+ for f in theirs {
+ assert!(dir.join(f).exists(), "{f} must not be removed");
+ }
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+
#[test]
fn files_older_than_the_export_are_kept() {
let dir = std::env::temp_dir().join(format!("fc-remove-partial-old-{}", std::process::id()));
diff --git a/crates/automation/src/server.rs b/crates/automation/src/server.rs
index 4a43cd2cc..4c705c3c2 100644
--- a/crates/automation/src/server.rs
+++ b/crates/automation/src/server.rs
@@ -402,7 +402,7 @@ impl FilmcraftMcp {
let t = p.seconds.map_or_else(|| g.playhead(), filmcraft_time::Tick::from_seconds_f64);
let (w, h) =
g.active_sequence().map(|q| (q.settings.width, q.settings.height)).ok_or_else(|| AutomationError::Other("no sequence".into()))?;
- let scale = (max as f32 / w.max(h) as f32).min(1.0);
+ let scale = render_scale(max, w, h);
let img = g.try_render_program_at(scale, t).map_err(|e| AutomationError::Other(e.to_string()))?;
png_rgba(img.w as u32, img.h as u32, img.over_black_rgba8(), max)
})
@@ -648,10 +648,26 @@ impl ServerHandler for FilmcraftMcp {
}
}
+/// Render scale that fits the longest side of a `w` x `h` sequence into `max_side` (never upscales; 0 = no limit, as in `png_rgba`).
+fn render_scale(max_side: u32, w: u32, h: u32) -> f32 {
+ if max_side == 0 {
+ return 1.0;
+ }
+ (max_side as f32 / w.max(h).max(1) as f32).min(1.0)
+}
+
#[cfg(test)]
mod tests {
use super::*;
+ #[test]
+ fn render_scale_zero_means_no_limit() {
+ assert_eq!(render_scale(0, 1920, 1080), 1.0);
+ assert_eq!(render_scale(960, 1920, 1080), 0.5);
+ assert_eq!(render_scale(960, 640, 360), 1.0);
+ assert_eq!(render_scale(960, 0, 0), 1.0);
+ }
+
#[tokio::test]
async fn headless_commands_and_render() {
let mut s = Session::default();
diff --git a/crates/av1/tests/adhoc.rs b/crates/av1/tests/adhoc.rs
index d72a220e8..e140b9768 100644
--- a/crates/av1/tests/adhoc.rs
+++ b/crates/av1/tests/adhoc.rs
@@ -7,7 +7,7 @@ use common::*;
#[test]
#[ignore]
fn adhoc_compare() {
- let Some(ff) = ffmpeg() else { return };
+ let Some(ff) = ffmpeg_dav1d() else { return };
let Some(path) = std::env::var_os("AV1_IVF") else { return };
let path = std::path::PathBuf::from(path);
let pics = decode_all(&path).unwrap();
diff --git a/crates/av1/tests/common/mod.rs b/crates/av1/tests/common/mod.rs
index 7998dbd13..0892fd697 100644
--- a/crates/av1/tests/common/mod.rs
+++ b/crates/av1/tests/common/mod.rs
@@ -6,8 +6,27 @@ use filmcraft_av1::{Decoder, Picture};
use std::path::{Path, PathBuf};
use std::process::Command;
+/// ffmpeg with libsvtav1 (fixtures) and libdav1d (reference), or `None` after reporting a skip.
pub fn ffmpeg() -> Option {
- filmcraft_testkit::ffmpeg_or_skip("av1 oracle")
+ ffmpeg_with(&[("-encoders", "libsvtav1"), ("-decoders", "libdav1d")])
+}
+
+/// ffmpeg with libdav1d, for checks that decode existing files only.
+pub fn ffmpeg_dav1d() -> Option {
+ ffmpeg_with(&[("-decoders", "libdav1d")])
+}
+
+/// Builds such as the Windows "essentials" ones lack these libraries; skip rather than fail.
+fn ffmpeg_with(needs: &[(&str, &str)]) -> Option {
+ let ff = filmcraft_testkit::ffmpeg_or_skip("av1 oracle")?;
+ for &(list, lib) in needs {
+ let out = Command::new(&ff).args(["-hide_banner", list]).output().map(|o| String::from_utf8_lossy(&o.stdout).into_owned()).unwrap_or_default();
+ if !out.contains(lib) {
+ filmcraft_testkit::skip("av1 oracle", &format!("ffmpeg has no {lib} (see `ffmpeg {list}`)"));
+ return None;
+ }
+ }
+ Some(ff)
}
pub fn fixture_dir() -> PathBuf {
diff --git a/crates/av1/tests/conformance.rs b/crates/av1/tests/conformance.rs
index d309bac6f..033f590e2 100644
--- a/crates/av1/tests/conformance.rs
+++ b/crates/av1/tests/conformance.rs
@@ -9,7 +9,7 @@ const VECTORS: &[&str] =
#[test]
fn conformance_vectors() {
- let Some(ff) = ffmpeg() else { return };
+ let Some(ff) = ffmpeg_dav1d() else { return };
for name in VECTORS {
let Some(path) = test_vector(name) else { return };
check_file(&ff, name, &path);
@@ -21,7 +21,7 @@ fn conformance_vectors() {
#[test]
#[ignore]
fn conformance_vectors_extended() {
- let Some(ff) = ffmpeg() else { return };
+ let Some(ff) = ffmpeg_dav1d() else { return };
let list: Vec = match std::env::var("AV1_VECTORS") {
Ok(v) => v.split(',').map(str::to_string).collect(),
Err(_) => EXTENDED.iter().map(|s| s.to_string()).collect(),
diff --git a/crates/captions/src/mcc.rs b/crates/captions/src/mcc.rs
index b5da4e295..3004872bb 100644
--- a/crates/captions/src/mcc.rs
+++ b/crates/captions/src/mcc.rs
@@ -17,7 +17,8 @@
//! - **CDP (SMPTE ST 334-2):** identifier `96 69`, length, frame-rate code, flags, sequence
//! counter, a `cc_data` section (`72`, `E0 | cc_count`, cc_count × (marker/valid/type, two
//! bytes)), optional time code / service info sections, footer `74`, the counter again and a
-//! checksum that makes the packet's byte sum 0 mod 256.
+//! checksum that makes the packet's byte sum 0 mod 256. The ancillary packet then ends with one ST 291 checksum byte
+//! (low 8 bits of DID + SDID + data count + CDP bytes).
//! - **Writing** (29.97 fps, `30DF` or `30` timecode labels): every frame from the first caption
//! data to one frame after the last carries a CDP with 20 cc_data triplets: field 1 CEA-608
//! pop-on data (the same schedule as [SCC](crate::scc)), a null field-2 pair, CEA-708
@@ -161,6 +162,9 @@ pub fn build_cdp(seq: u16, data: &FrameData) -> Vec {
cdp.push(((256 - sum % 256) % 256) as u8);
let mut out = vec![0x61, 0x01, cdp.len() as u8];
out.extend(cdp);
+ // SMPTE ST 291 ancillary checksum: low 8 bits of the sum of DID, SDID, data count and the user data words.
+ let anc_sum: u32 = out.iter().map(|&b| b as u32).sum();
+ out.push((anc_sum & 0xff) as u8);
out
}
@@ -661,7 +665,11 @@ mod tests {
let d = FrameData { f1: Some((0x14, 0x20)), dtvcc: vec![(true, 0x02, 0x21), (false, 0x89, 0x01)] };
let b = build_cdp(7, &d);
assert_eq!(&b[..3], &[0x61, 0x01, 73]);
- let cdp = &b[3..];
+ // prefix (3) + CDP (73) + ancillary checksum byte (1)
+ assert_eq!(b.len(), 77);
+ let sum: u32 = b[..76].iter().map(|&x| x as u32).sum();
+ assert_eq!(b[76], (sum & 0xff) as u8, "ancillary checksum");
+ let cdp = &b[3..76];
assert_eq!(cdp.len(), 73);
assert_eq!(&cdp[..9], &[0x96, 0x69, 73, 0x4f, 0x43, 0, 7, 0x72, 0xf4]);
assert_eq!(cdp.iter().map(|&x| x as u32).sum::() % 256, 0, "checksum");
diff --git a/crates/codecs/Cargo.toml b/crates/codecs/Cargo.toml
index 8765f3eb4..3a490e2c4 100644
--- a/crates/codecs/Cargo.toml
+++ b/crates/codecs/Cargo.toml
@@ -41,3 +41,4 @@ symphonia = { version = "0.5.5", default-features = false, features = ["aac", "m
[dev-dependencies]
filmcraft-testkit = { workspace = true }
+serde_json = { workspace = true }
diff --git a/crates/codecs/src/apv.rs b/crates/codecs/src/apv.rs
index 0001e2a30..c7068ccb4 100644
--- a/crates/codecs/src/apv.rs
+++ b/crates/codecs/src/apv.rs
@@ -114,7 +114,7 @@ impl ApvSource {
bitrate,
hdr: None,
}),
- audio: None,
+ audio_streams: Vec::new(),
container: "APV".into(),
start_timecode: None,
file_size: Some(len),
diff --git a/crates/codecs/src/audio.rs b/crates/codecs/src/audio.rs
index 4942f6a6c..5f7c02f4a 100644
--- a/crates/codecs/src/audio.rs
+++ b/crates/codecs/src/audio.rs
@@ -87,7 +87,7 @@ impl PacketDecoder {
/// (HE-AAC, see [`aac_output_rate`]) the decoded core is upsampled to it.
pub fn aac(asc: &[u8], sample_rate: u32) -> Result {
if let Ok(dec) = filmcraft_aac::Decoder::new(asc) {
- let up = (dec.sample_rate().checked_mul(2) == Some(sample_rate)).then(|| Box::new(Upsample2x::new(dec.channels())));
+ let up = upsamples_core(dec.sample_rate(), sample_rate).then(|| Box::new(Upsample2x::new(dec.channels())));
return Ok(Self { inner: Inner::Aac { dec: Box::new(dec), asc: asc.to_vec(), up }, channels: 0 });
}
Self::new(CODEC_TYPE_AAC, sample_rate, Some(asc.to_vec()))
@@ -244,6 +244,77 @@ pub fn aac_output_rate<'a>(asc: &[u8], first_units: impl IntoIterator- bool {
+ core.checked_mul(2) == Some(rate)
+}
+
+/// A codec whose packets all decode to the same number of samples (see [`fixed_packet_samples`]).
+#[derive(Clone, Copy, Debug)]
+pub enum FixedFrames<'a> {
+ /// AAC with this AudioSpecificConfig.
+ Aac(&'a [u8]),
+ /// MPEG-1/2 audio, layer I, II or III.
+ MpegAudio,
+ /// AC-3 (and E-AC-3).
+ Ac3,
+}
+
+/// Samples each packet decodes to at the stream's output `rate`, for codecs where that is the same
+/// for every packet: AAC 1024 (2048 when [`PacketDecoder::aac`] upsamples an HE-AAC core); MPEG
+/// audio and AC-3 from the frame header of `first`, the stream's first packet. `None` when it is
+/// not known: an AAC configuration our decoder does not read, a header that does not parse, or a
+/// header whose rate is not `rate`.
+pub fn fixed_packet_samples(codec: FixedFrames, first: &[u8], rate: u32) -> Option {
+ let mpegts = match codec {
+ FixedFrames::Aac(asc) => {
+ let core = filmcraft_aac::Decoder::new(asc).ok()?.sample_rate();
+ return Some(if upsamples_core(core, rate) { 2048 } else { 1024 });
+ }
+ FixedFrames::MpegAudio => filmcraft_mpegts::Codec::MpegAudio,
+ FixedFrames::Ac3 => filmcraft_mpegts::Codec::Ac3,
+ };
+ let f = filmcraft_mpegts::frame_info(&mpegts, first)?;
+ (f.sample_rate == rate && f.samples > 0).then_some(f.samples as i64)
+}
+
+/// Where the container puts one audio packet, for [`contiguous_starts`].
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct PacketTime {
+ /// Its timestamp, in output samples.
+ pub stamp: i64,
+ /// Whether `stamp` is the packet's own. A Matroska frame after the first in a laced block
+ /// without a duration repeats the block's timestamp.
+ pub stamped: bool,
+ /// Samples it decodes to, when known.
+ pub samples: Option,
+}
+
+/// Packet start positions in output samples: each packet starts where the previous one ends,
+/// unless its own timestamp is more than `tolerance` samples from there (a real gap or overlap in
+/// the recording) or the previous packet's length is unknown; then it starts at its timestamp.
+///
+/// Container timestamps are coarser than a sample. Matroska stores milliseconds, and a remux to MP4
+/// (OBS's) turns that rounding into uneven sample durations: a 1024-sample AAC frame at 48 kHz lasts
+/// 21.33 ms, so frames are stamped at 0, 1008, 2064, 3072… Placing each decoded packet at its
+/// stamp overwrote the end of one packet and left a hole of silence after another, a click every
+/// few packets: crackling throughout playback and export (#236).
+pub fn contiguous_starts(packets: impl IntoIterator
- , tolerance: i64) -> Vec {
+ let tolerance = tolerance.max(0).unsigned_abs();
+ let mut next: Option = None;
+ packets
+ .into_iter()
+ .map(|p| {
+ let start = match next {
+ Some(n) if !p.stamped || n.abs_diff(p.stamp) <= tolerance => n,
+ _ => p.stamp,
+ };
+ next = p.samples.and_then(|k| start.checked_add(k));
+ start
+ })
+ .collect()
+}
+
/// Taps on each side of the interpolation point in [`Upsample2x`].
const UPSAMPLE_HALF: usize = 12;
@@ -666,12 +737,12 @@ impl AudioFileSource {
kind: MediaKind::AudioOnly,
duration: Tick::from_units(frames as i64, rate as i64),
video: None,
- audio: Some(AudioStreamInfo {
+ audio_streams: vec![AudioStreamInfo {
sample_rate: rate,
channels: samples.len() as u32,
codec: codec_name,
bits_per_sample: track.codec_params.bits_per_sample,
- }),
+ }],
container: ext.to_uppercase(),
start_timecode: None,
file_size: Some(bytes.len() as u64),
@@ -739,3 +810,82 @@ mod he_aac_tests {
}
}
}
+
+#[cfg(test)]
+mod packet_time_tests {
+ use super::*;
+
+ /// 1024-sample frames at 48 kHz stamped to the nearest millisecond, then back in samples.
+ fn ms_stamps(count: i64) -> Vec {
+ (0..count).map(|k| (k * 1024 * 1000 + 24_000) / 48_000 * 48).collect()
+ }
+
+ fn timed(stamps: &[i64], samples: Option) -> Vec {
+ stamps.iter().map(|&stamp| PacketTime { stamp, stamped: true, samples }).collect()
+ }
+
+ #[test]
+ fn rounded_stamps_give_back_to_back_packets() {
+ let stamps = ms_stamps(200);
+ assert_eq!(&stamps[..4], [0, 1008, 2064, 3072]);
+ let starts = contiguous_starts(timed(&stamps, Some(1024)), 512);
+ assert_eq!(starts, (0..200).map(|k| k * 1024).collect::>());
+ }
+
+ #[test]
+ fn real_gaps_and_overlaps_resynchronise() {
+ // a 100 ms gap after packet 3, then a packet stamped 600 samples early
+ let mut stamps: Vec = (0..8).map(|k| k * 1024).collect();
+ for s in &mut stamps[4..] {
+ *s += 4800;
+ }
+ stamps[7] -= 600;
+ let starts = contiguous_starts(timed(&stamps, Some(1024)), 512);
+ assert_eq!(starts, [0, 1024, 2048, 3072, 8896, 9920, 10944, 11368]);
+ // within the tolerance: runs on
+ stamps[7] += 200;
+ assert_eq!(contiguous_starts(timed(&stamps, Some(1024)), 512)[7], 11968);
+ }
+
+ #[test]
+ fn unstamped_packets_follow_on_and_unknown_lengths_resynchronise() {
+ // a laced block of three frames that repeat its timestamp
+ let packets = [0, 1024, 1024, 1024, 4096].map(|stamp| PacketTime { stamp, stamped: true, samples: Some(1024) });
+ let mut laced = packets;
+ laced[2].stamped = false;
+ laced[3].stamped = false;
+ assert_eq!(contiguous_starts(laced, 512), [0, 1024, 2048, 3072, 4096]);
+ // a packet whose length is unknown: the next one starts at its own stamp
+ let mut unknown = timed(&[0, 1024, 2100, 3124], Some(1024));
+ unknown[1].samples = None;
+ assert_eq!(contiguous_starts(unknown, 512), [0, 1024, 2100, 3124]);
+ }
+
+ #[test]
+ fn hostile_values_stay_finite() {
+ let packets = [
+ PacketTime { stamp: i64::MAX, stamped: true, samples: Some(i64::MAX) },
+ PacketTime { stamp: i64::MIN, stamped: false, samples: Some(-5) },
+ PacketTime { stamp: 0, stamped: true, samples: Some(i64::MIN) },
+ PacketTime { stamp: 7, stamped: true, samples: None },
+ ];
+ assert_eq!(contiguous_starts(packets, i64::MIN).len(), 4);
+ assert_eq!(contiguous_starts(packets, i64::MAX).len(), 4);
+ assert!(contiguous_starts([], 512).is_empty());
+ }
+
+ #[test]
+ fn fixed_frame_lengths() {
+ // AAC-LC 48 kHz stereo; HE-AAC (24 kHz core) played at 48 kHz is upsampled
+ assert_eq!(fixed_packet_samples(FixedFrames::Aac(&[0x11, 0x90]), &[], 48_000), Some(1024));
+ assert_eq!(fixed_packet_samples(FixedFrames::Aac(&[0x13, 0x10]), &[], 48_000), Some(2048));
+ assert_eq!(fixed_packet_samples(FixedFrames::Aac(&[]), &[], 48_000), None);
+ // MPEG-1 layer III 44.1 kHz, 128 kbps; MPEG-2 layer III 22.05 kHz
+ assert_eq!(fixed_packet_samples(FixedFrames::MpegAudio, &[0xFF, 0xFB, 0x90, 0x44], 44_100), Some(1152));
+ assert_eq!(fixed_packet_samples(FixedFrames::MpegAudio, &[0xFF, 0xF3, 0x90, 0x44], 22_050), Some(576));
+ // the header's rate must be the stream's
+ assert_eq!(fixed_packet_samples(FixedFrames::MpegAudio, &[0xFF, 0xFB, 0x90, 0x44], 48_000), None);
+ assert_eq!(fixed_packet_samples(FixedFrames::MpegAudio, &[0xFF], 44_100), None);
+ assert_eq!(fixed_packet_samples(FixedFrames::Ac3, &[0x0B, 0x77], 48_000), None);
+ }
+}
diff --git a/crates/codecs/src/audio_timing_tests.rs b/crates/codecs/src/audio_timing_tests.rs
new file mode 100644
index 000000000..40e43a2d8
--- /dev/null
+++ b/crates/codecs/src/audio_timing_tests.rs
@@ -0,0 +1,178 @@
+//! Audio packets placed by container timestamps (#236).
+//!
+//! An AAC access unit decodes to 1024 samples, 21.33 ms at 48 kHz. Matroska stores timestamps in
+//! whole milliseconds, and OBS's remux to MP4 carries that rounding into sample durations of 1008,
+//! 1008, 1056… Placing each decoded packet at its own timestamp overwrote the end of one packet and
+//! left a hole of silence after another: playback and export crackled throughout. The streams here
+//! are AAC from our own encoder muxed with such timestamps; read back block by block, as playback
+//! reads them, they must be exactly the access units decoded back to back.
+
+use std::io::Cursor;
+use std::sync::Arc;
+
+use filmcraft_isobmff::{Brand, Mp4Writer, SampleEntry, TrackConfig, WriteSample, WriterOptions};
+use filmcraft_matroska::{LacingMode, MkvWriter, MuxOptions, TrackKind, TrackSpec};
+use filmcraft_media::MediaSource;
+
+use crate::audio::PacketDecoder;
+use crate::{MkvSource, Mp4Source};
+
+/// A second of a stereo tone (440 Hz left, 660 Hz right) encoded with our AAC encoder: the
+/// AudioSpecificConfig and the access units.
+fn aac(rate: u32) -> (Vec, Vec>) {
+ let tone = |hz: f32| (0..rate as usize).map(|i| 0.5 * (std::f32::consts::TAU * hz * i as f32 / rate as f32).sin()).collect::>();
+ let (l, r) = (tone(440.0), tone(660.0));
+ let mut enc = filmcraft_aac::Encoder::new(filmcraft_aac::EncoderConfig::cbr(rate, 2, 160_000)).unwrap();
+ let mut units = enc.encode(&[&l, &r]);
+ units.extend(enc.flush());
+ (enc.audio_specific_config(), units)
+}
+
+/// The access units decoded back to back.
+fn continuous(asc: &[u8], rate: u32, units: &[Vec]) -> Vec> {
+ let mut dec = PacketDecoder::aac(asc, rate).unwrap();
+ let mut out = vec![Vec::new(), Vec::new()];
+ for u in units {
+ for (o, c) in out.iter_mut().zip(dec.decode(u, 0).unwrap()) {
+ o.extend(c);
+ }
+ }
+ out
+}
+
+/// Access unit `k`'s start to the nearest millisecond, as muxers stamp it.
+fn stamp_ms(rate: u32, k: usize) -> i64 {
+ (k as i64 * 1024 * 2000 + rate as i64) / (2 * rate as i64)
+}
+
+/// Matroska (1 ms timestamps), with the units after `gap` delayed by `gap_ms` (a real gap).
+fn mkv(asc: &[u8], rate: u32, units: &[Vec], lacing: LacingMode, gap: usize, gap_ms: i64) -> Arc<[u8]> {
+ let mut spec = TrackSpec::new(TrackKind::Audio, "A_AAC");
+ spec.codec_private = asc.to_vec();
+ spec.audio = Some((rate as f64, 2, None));
+ let opts = MuxOptions { lacing, ..MuxOptions::default() };
+ assert_eq!(opts.timestamp_scale, 1_000_000, "1 ms timestamps");
+ let mut w = MkvWriter::new(Cursor::new(Vec::new()), vec![spec], opts).unwrap();
+ for (k, u) in units.iter().enumerate() {
+ let ms = stamp_ms(rate, k) + if k >= gap { gap_ms } else { 0 };
+ w.write_frame(0, ms * 1_000_000, true, u, None).unwrap();
+ }
+ w.finish().unwrap().into_inner().into()
+}
+
+/// MP4 as OBS remuxes Matroska: each millisecond timestamp rescaled to the sample rate, the
+/// differences written as sample durations.
+fn mp4_remuxed(asc: &[u8], rate: u32, units: &[Vec]) -> Arc<[u8]> {
+ let mut w = Mp4Writer::new(Cursor::new(Vec::new()), WriterOptions::new(Brand::Mp4)).unwrap();
+ let t = w.add_track(TrackConfig::new(SampleEntry::aac(asc.to_vec(), 2, rate), rate)).unwrap();
+ let at = |k: usize| (stamp_ms(rate, k) * rate as i64 + 500) / 1000;
+ for (k, u) in units.iter().enumerate() {
+ let duration = (at(k + 1) - at(k)) as u32;
+ w.write_sample(t, WriteSample { data: u, duration, composition_offset: 0, is_sync: true }).unwrap();
+ }
+ w.finish().unwrap().into_inner().into()
+}
+
+/// `frames` samples per channel read in 512-sample blocks at `rate`, as playback reads them.
+fn played(src: &dyn MediaSource, rate: u32, frames: usize) -> Vec> {
+ let mut out = vec![Vec::new(), Vec::new()];
+ let mut pos = 0;
+ while pos < frames {
+ let n = 512.min(frames - pos);
+ let b = src.audio(pos as i64, n, rate).unwrap();
+ for (o, c) in out.iter_mut().zip(&b.channels) {
+ o.extend_from_slice(c);
+ }
+ pos += n;
+ }
+ out
+}
+
+/// Samples of `got` that differ from `want[k - shift]` (silence where that is out of range), and
+/// the first few.
+fn mismatches(got: &[Vec], want: &[Vec], shift: usize) -> (usize, Vec<(usize, usize)>) {
+ let mut bad = Vec::new();
+ for (c, (g, w)) in got.iter().zip(want).enumerate() {
+ for (k, x) in g.iter().enumerate() {
+ let expect = k.checked_sub(shift).and_then(|j| w.get(j)).copied().unwrap_or(0.0);
+ if x.to_bits() != expect.to_bits() {
+ bad.push((c, k));
+ }
+ }
+ }
+ (bad.len(), bad.into_iter().take(4).collect())
+}
+
+/// Runs of two or more exact zeros inside the signal: holes left between packets.
+fn holes(got: &[Vec]) -> usize {
+ got.iter().map(|c| c.windows(2).filter(|w| w[0] == 0.0 && w[1] == 0.0).count()).sum()
+}
+
+#[test]
+fn matroska_millisecond_stamps_play_back_to_back() {
+ for rate in [48_000, 44_100] {
+ let (asc, units) = aac(rate);
+ let want = continuous(&asc, rate, &units);
+ let frames = units.len().saturating_sub(1) * 1024;
+ for lacing in [LacingMode::None, LacingMode::Xiph] {
+ let bytes = mkv(&asc, rate, &units, lacing, usize::MAX, 0);
+ let src = MkvSource::open("tone.mkv", bytes.clone()).unwrap();
+ let got = played(&src, rate, frames);
+ let (n, first) = mismatches(&got, &want, 0);
+ assert_eq!(n, 0, "{rate} Hz, {lacing:?}: {n} samples differ from the continuous decode, first (channel, sample) {first:?}");
+ // random access (a fresh source, so a cold decoder primed with the packet before) gives
+ // the same samples
+ let src = MkvSource::open("tone.mkv", bytes).unwrap();
+ for start in [30_011usize, 7_000] {
+ let got = src.audio(start as i64, 4_800, rate).unwrap().channels;
+ let at: Vec> = want.iter().map(|c| c[start..start + 4_800].to_vec()).collect();
+ assert_eq!(mismatches(&got, &at, 0).0, 0, "{rate} Hz, {lacing:?}: seek to {start}");
+ }
+ }
+ }
+}
+
+#[test]
+fn obs_remuxed_mp4_plays_back_to_back() {
+ for rate in [48_000, 44_100] {
+ let (asc, units) = aac(rate);
+ let want = continuous(&asc, rate, &units);
+ let bytes = mp4_remuxed(&asc, rate, &units);
+ let src = Mp4Source::open("tone.mp4", bytes).unwrap();
+ let got = played(&src, rate, units.len().saturating_sub(1) * 1024);
+ let (n, first) = mismatches(&got, &want, 0);
+ assert_eq!(n, 0, "{rate} Hz: {n} samples differ from the continuous decode, first (channel, sample) {first:?}");
+ }
+}
+
+#[test]
+fn a_real_gap_stays_where_the_timestamps_put_it() {
+ let rate = 48_000;
+ let (asc, units) = aac(rate);
+ let want = continuous(&asc, rate, &units);
+ // 100 ms of the recording missing before unit 20: it plays as silence, and the units after it
+ // keep their timestamps (to the millisecond)
+ let src = MkvSource::open("gap.mkv", mkv(&asc, rate, &units, LacingMode::None, 20, 100)).unwrap();
+ let shift = ((stamp_ms(rate, 20) + 100) * 48 - 20 * 1024) as usize;
+ assert!((4_700..4_900).contains(&shift), "{shift}");
+ let got = played(&src, rate, units.len().saturating_sub(1) * 1024 + shift);
+ let before: Vec> = got.iter().map(|c| c[..20 * 1024].to_vec()).collect();
+ assert_eq!(mismatches(&before, &want, 0).0, 0);
+ assert!(got.iter().all(|c| c[20 * 1024..20 * 1024 + shift].iter().all(|x| *x == 0.0)), "the gap is silent");
+ let after: Vec> = got.iter().map(|c| c[20 * 1024 + shift..].to_vec()).collect();
+ let rest: Vec> = want.iter().map(|c| c[20 * 1024..].to_vec()).collect();
+ let (n, first) = mismatches(&after, &rest, 0);
+ assert_eq!(n, 0, "{n} samples after the gap differ, first {first:?}");
+}
+
+#[test]
+fn playing_at_another_rate_leaves_no_holes() {
+ // a 44.1 kHz recording in a 48 kHz sequence (export's default rate)
+ let (asc, units) = aac(44_100);
+ let src = MkvSource::open("tone.mkv", mkv(&asc, 44_100, &units, LacingMode::None, usize::MAX, 0)).unwrap();
+ let frames = (units.len().saturating_sub(2) * 1024) * 48_000 / 44_100;
+ let got = played(&src, 48_000, frames);
+ // skip the encoder's priming (the first frame decodes to near-silence)
+ let tail: Vec> = got.iter().map(|c| c[2048..].to_vec()).collect();
+ assert_eq!(holes(&tail), 0);
+}
diff --git a/crates/codecs/src/gop.rs b/crates/codecs/src/gop.rs
index b1ddd9a2e..3972998ef 100644
--- a/crates/codecs/src/gop.rs
+++ b/crates/codecs/src/gop.rs
@@ -140,6 +140,10 @@ struct State {
bytes: usize,
/// When a frame was last asked of this cache.
last_used: Instant,
+ /// Only background requests (thumbnails, [`filmcraft_media::cancel::with_background`]) have
+ /// used the decoder since it was made: it is given up as soon as another background request
+ /// is done, whatever the caches' recency.
+ background: bool,
}
impl State {
@@ -212,10 +216,27 @@ impl Drop for DecodingGuard {
/// new one when the playhead reaches them, which costs what a seek costs.
const MAX_LIVE_DECODERS: usize = 4;
-/// A cache asked for a frame this recently is in use (a layer of the frame being composited,
-/// the next clip being prefetched): it keeps its decoder whatever the count.
+/// A cache asked for a frame this recently may be needed again soon (the next clip being
+/// prefetched, a clip the user scrubs back to): it keeps its decoder up to [`HARD_DECODERS`], and
+/// beyond [`MAX_LIVE_DECODERS`] only until it has been idle this long. Its frames are only kept
+/// within [`FRAME_BUDGET`] once it has been idle for [`RECENT`]: going back to a clip left (or
+/// paused) more than that long ago may cost a re-decode from its keyframe when other clips have
+/// filled the budget meanwhile. Frames go least recently used cache first, earliest frames first.
const IDLE: Duration = Duration::from_secs(3);
+/// A cache asked for a frame this recently is in use (a layer of the frame being composited): it
+/// keeps its decoder and frames whatever the count. Caches idle for less than [`IDLE`] but longer
+/// than this are a burst of navigation (thumbnails of every clip, the playhead jumping from clip
+/// to clip) that the playhead has already left: they give up decoders beyond [`HARD_DECODERS`]
+/// and frames beyond [`FRAME_BUDGET`]. Without this, everything touched within [`IDLE`] counted
+/// as in use, so a burst held every clip's decoder (42 live hardware sessions, 6.5 GB of VRAM)
+/// and hundreds of MB of frames per clip.
+const RECENT: Duration = Duration::from_millis(500);
+
+/// Most decoders caches idle for [`RECENT`] to [`IDLE`] keep together (a composite of a few
+/// layers plus the clips just left behind).
+const HARD_DECODERS: usize = 8;
+
/// Decoded frames all caches may hold together before idle ones give theirs up. Each cache also
/// has its own budget, which alone let every clip on a timeline keep hundreds of MB of frames
/// nobody was looking at.
@@ -228,6 +249,10 @@ struct Pool {
decoders: AtomicUsize,
max_decoders: usize,
idle: Duration,
+ /// Caches idle at least this long are no longer in use (see [`RECENT`]).
+ recent: Duration,
+ /// Decoders the caches idle for [`Self::recent`] but not [`Self::idle`] may keep.
+ hard_decoders: usize,
/// Bytes of the frames the caches hold.
bytes: AtomicUsize,
budget: usize,
@@ -258,13 +283,29 @@ fn try_state(c: &Shared) -> Option> {
impl Pool {
fn new(max_decoders: usize, idle: Duration, budget: usize) -> Self {
- Self { caches: Mutex::new(Vec::new()), decoders: AtomicUsize::new(0), max_decoders, idle, bytes: AtomicUsize::new(0), budget }
+ Self {
+ caches: Mutex::new(Vec::new()),
+ decoders: AtomicUsize::new(0),
+ max_decoders,
+ idle,
+ recent: idle,
+ hard_decoders: max_decoders,
+ bytes: AtomicUsize::new(0),
+ budget,
+ }
+ }
+
+ /// Caches idle for `recent` (< the idle time) are still trimmed, down to `hard_decoders`.
+ fn tiered(mut self, recent: Duration, hard_decoders: usize) -> Self {
+ self.recent = recent.min(self.idle);
+ self.hard_decoders = hard_decoders.max(self.max_decoders);
+ self
}
/// The pool of every cache in the process.
fn global() -> Arc {
static POOL: std::sync::OnceLock> = std::sync::OnceLock::new();
- POOL.get_or_init(|| Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET))).clone()
+ POOL.get_or_init(|| Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET).tiered(RECENT, HARD_DECODERS))).clone()
}
fn register(&self, c: &Arc) {
@@ -273,15 +314,15 @@ impl Pool {
g.push(Arc::downgrade(c));
}
- /// The idle caches other than `me` that `keep` selects, least recently used first. Other
+ /// The caches other than `me` idle for at least `idle` that `keep` selects, least recently used first. Other
/// caches are only ever try-locked, so two caches trimming each other cannot deadlock.
- fn idle_others(&self, me: &Shared, keep: impl Fn(&State) -> bool) -> Vec> {
+ fn idle_others(&self, me: &Shared, idle: Duration, keep: impl Fn(&State) -> bool) -> Vec> {
let all: Vec> = self.caches.lock().unwrap_or_else(PoisonError::into_inner).iter().filter_map(Weak::upgrade).collect();
let mut idle: Vec<(Instant, Arc)> = all
.into_iter()
.filter(|c| !std::ptr::eq(Arc::as_ptr(c), me))
.filter_map(|c| {
- let used = try_state(&c).filter(|st| keep(st) && st.last_used.elapsed() >= self.idle).map(|st| st.last_used)?;
+ let used = try_state(&c).filter(|st| keep(st) && st.last_used.elapsed() >= idle).map(|st| st.last_used)?;
Some((used, c))
})
.collect();
@@ -295,29 +336,53 @@ impl Pool {
#[must_use = "drop the released decoders with no cache lock held"]
fn trim_decoders(&self, me: &Shared) -> Vec> {
let mut released = Vec::new();
- if self.decoders.load(Ordering::Relaxed) <= self.max_decoders {
- return released;
- }
- for c in self.idle_others(me, |st| st.decoder.is_some()) {
- if self.decoders.load(Ordering::Relaxed) <= self.max_decoders {
- break;
+ // first the caches idle for long, down to the cap; then those a burst has left behind
+ for (idle, cap) in [(self.idle, self.max_decoders), (self.recent, self.hard_decoders)] {
+ if self.decoders.load(Ordering::Relaxed) <= cap {
+ continue;
}
- if let Some(mut st) = try_state(&c) {
+ for c in self.idle_others(me, idle, |st| st.decoder.is_some()) {
+ if self.decoders.load(Ordering::Relaxed) <= cap {
+ break;
+ }
+ if let Some(mut st) = try_state(&c) {
+ released.extend(st.release_decoder(self));
+ }
+ }
+ }
+ released
+ }
+
+ /// Take the decoders of the caches other than `me` that only background requests have used
+ /// (see [`State::background`]) and that are not decoding now. A burst of thumbnails (an import
+ /// of many clips) touches every clip within moments, so the recency rules treat them all as
+ /// in use; each software 4K decoder holds ~0.5 GB. The cache `me` just served keeps its
+ /// decoder (hovering a clip asks for more of its frames). Drop the result with no cache lock
+ /// held.
+ #[must_use = "drop the released decoders with no cache lock held"]
+ fn release_background(&self, me: &Shared) -> Vec> {
+ let all: Vec> = self.caches.lock().unwrap_or_else(PoisonError::into_inner).iter().filter_map(Weak::upgrade).collect();
+ let mut released = Vec::new();
+ for c in all.iter().filter(|c| !std::ptr::eq(Arc::as_ptr(c), me)) {
+ if let Some(mut st) = try_state(c)
+ && st.background
+ && st.decoder.is_some()
+ {
released.extend(st.release_decoder(self));
}
}
released
}
- /// Evict the frames of idle caches, least recently used first, down to the budget. Caches in
- /// use keep theirs (each within its own budget): a frame evicted before it is shown costs a
+ /// Evict the frames of caches idle for [`RECENT`], least recently used first, down to the
+ /// budget. Caches in use keep theirs (each within its own budget): a frame evicted before it is shown costs a
/// re-decode from the keyframe.
fn trim_frames(&self, me: &Shared) {
let over = || self.bytes.load(Ordering::Relaxed) > self.budget;
if !over() {
return;
}
- for c in self.idle_others(me, |st| !st.frames.is_empty()) {
+ for c in self.idle_others(me, self.recent, |st| !st.frames.is_empty()) {
if !over() {
break;
}
@@ -361,7 +426,7 @@ const CONTINUE_THROUGH: usize = 48;
impl GopCache {
pub fn new(explicit_color: Option) -> Self {
// unit tests count decoder restarts: each of their caches gets a pool of its own
- let pool = if cfg!(test) { Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET)) } else { Pool::global() };
+ let pool = if cfg!(test) { Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET).tiered(RECENT, HARD_DECODERS)) } else { Pool::global() };
Self::in_pool(explicit_color, pool)
}
@@ -378,6 +443,7 @@ impl GopCache {
drafts: Default::default(),
bytes: 0,
last_used: Instant::now(),
+ background: false,
}),
pool: pool.clone(),
});
@@ -405,6 +471,7 @@ impl GopCache {
fn store(&self, st: &mut State, pts: i64, f: VideoFrame, draft: bool) {
let f = self.finish(f);
+ st.last_used = Instant::now();
if draft {
st.drafts.insert(pts);
DRAFT.fetch_add(1, Ordering::Relaxed);
@@ -467,6 +534,16 @@ impl GopCache {
/// ([`VideoDecoder::set_draft`]); its draft frames are cached for draft requests only, and an
/// exact request for one re-decodes it.
pub fn frame_late(&self, s: &dyn VideoSamples, target: i64, late_before: Option) -> crate::Result> {
+ let r = self.frame_late_locked(s, target, late_before);
+ if filmcraft_media::cancel::background() {
+ // Outside the cache's lock (see `release_background`). A thumbnail leaves the decoders
+ // of earlier thumbnails no reason to stay: each costs hundreds of MB at 4K.
+ drop(self.shared.pool.release_background(&self.shared));
+ }
+ r
+ }
+
+ fn frame_late_locked(&self, s: &dyn VideoSamples, target: i64, late_before: Option) -> crate::Result> {
let n = s.count();
let i = s.sample_at(target.max(0)).or_else(|| (n > 0).then(|| n - 1)).ok_or_else(|| CodecError::Decode("empty track".into()))?;
let want_pts = s.pts(i);
@@ -482,11 +559,23 @@ impl GopCache {
let mut st = self.shared.state.lock().unwrap_or_else(|e| e.into_inner());
let _decoding = DecodingGuard::enter(me);
st.last_used = Instant::now();
- if let Some(f) = st.cached(want_pts, draft) {
+ let background = filmcraft_media::cancel::background();
+ if let Some(f) = st.cached(want_pts, draft).cloned() {
HITS.fetch_add(1, Ordering::Relaxed);
- return Ok(f.clone());
+ // a monitor asking for a frame of a thumbnail's clip makes its decoder one in use
+ st.background &= background;
+ return Ok(f);
}
MISSES.fetch_add(1, Ordering::Relaxed);
+ if !background {
+ st.background = false;
+ } else if st.decoder.is_none() {
+ st.background = true;
+ }
+ if !background && st.decoder.as_ref().is_some_and(|d| d.thread_limited()) {
+ // made for a thumbnail (few threads): a monitor gets a full one
+ released.extend(st.release_decoder(&self.shared.pool));
+ }
if st.decoder.is_none() {
let d = s.make_decoder()?;
st.intra = d.intra_only();
@@ -683,6 +772,8 @@ mod tests {
/// (pts, decoded as a draft picture)
held: VecDeque<(i64, bool)>,
draft: bool,
+ /// made for background work (few threads, like the H.264 decoder's)
+ limited: bool,
resets: Arc,
decodes: Arc,
}
@@ -717,6 +808,9 @@ mod tests {
fn name(&self) -> &str {
"test"
}
+ fn thread_limited(&self) -> bool {
+ self.limited
+ }
fn intra_only(&self) -> bool {
self.intra
}
@@ -749,6 +843,7 @@ mod tests {
intra: self.intra,
held: VecDeque::new(),
draft: false,
+ limited: filmcraft_media::cancel::background(),
resets: self.resets.clone(),
decodes: self.decodes.clone(),
}))
@@ -944,6 +1039,99 @@ mod tests {
assert!(s.iter().all(|s| s.resets.load(Ordering::Relaxed) == 1), "no decoder restarted");
}
+ /// A burst of navigation (every clip of a timeline asked once within moments, as when
+ /// thumbnails are made or the playhead jumps clip to clip) must not keep every clip's decoder
+ /// and frames: only the caches of the frame being composited are in use.
+ #[test]
+ fn a_burst_of_single_requests_does_not_keep_every_decoder_and_frame() {
+ // the real decoder caps and idle time; RECENT scaled down from 500 ms to the test's pace
+ // (3 ms per clip). With the old rule (recent = idle) all 24 decoders stay live.
+ let pool = Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET).tiered(Duration::from_millis(2), HARD_DECODERS));
+ let n = 24;
+ let s: Vec = (0..n).map(|_| samples(300, 250, 3, false)).collect();
+ let c: Vec = (0..n).map(|_| GopCache::in_pool(None, pool.clone())).collect();
+ for k in 0..n {
+ assert_eq!(index_of(&c[k].frame(&s[k], 10_000).expect("frame")), 10);
+ std::thread::sleep(Duration::from_millis(3));
+ }
+ assert!(
+ pool.decoders.load(Ordering::Relaxed) <= HARD_DECODERS + 1,
+ "{} live decoders after a burst over {n} clips",
+ pool.decoders.load(Ordering::Relaxed)
+ );
+ // the clip asked last, and a clip in use together with it, keep theirs and carry on
+ let resets = s[n - 1].resets.load(Ordering::Relaxed);
+ assert_eq!(index_of(&c[n - 1].frame(&s[n - 1], 11_000).expect("frame")), 11);
+ assert_eq!(s[n - 1].resets.load(Ordering::Relaxed), resets);
+ }
+
+ /// The thumbnails an import queues: every clip asked once, quickly (the real caps and recency,
+ /// no sleeping: all of them are "recent"). Each 4K software decoder costs ~0.5 GB, and 42
+ /// clips used to hold 6+ GB at once.
+ #[test]
+ fn background_requests_leave_no_decoders_behind() {
+ use filmcraft_media::cancel::with_background;
+ let pool = Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET).tiered(RECENT, HARD_DECODERS));
+ let n = 24;
+ let s: Vec = (0..n).map(|_| samples(300, 250, 3, false)).collect();
+ let c: Vec = (0..n).map(|_| GopCache::in_pool(None, pool.clone())).collect();
+ // a clip in use by a monitor
+ assert_eq!(index_of(&c[0].frame(&s[0], 10_000).expect("frame")), 10);
+ for k in 1..n {
+ assert_eq!(index_of(&with_background(true, || c[k].frame(&s[k], 10_000)).expect("frame")), 10);
+ assert!(pool.decoders.load(Ordering::Relaxed) <= 2, "{} live decoders after thumbnail {k}", pool.decoders.load(Ordering::Relaxed));
+ }
+ // the monitor's clip and the last thumbnail keep theirs and carry on without a seek
+ assert_eq!(pool.decoders.load(Ordering::Relaxed), 2);
+ assert_eq!(index_of(&c[0].frame(&s[0], 11_000).expect("frame")), 11);
+ assert_eq!(s[0].resets.load(Ordering::Relaxed), 1);
+ assert_eq!(index_of(&with_background(true, || c[n - 1].frame(&s[n - 1], 12_000)).expect("frame")), 12);
+ assert_eq!(s[n - 1].resets.load(Ordering::Relaxed), 1);
+ // a monitor asking for a frame of a thumbnail's clip makes its decoder one in use
+ assert_eq!(index_of(&c[n - 1].frame(&s[n - 1], 13_000).expect("frame")), 13);
+ for k in 1..4 {
+ with_background(true, || c[k].frame(&s[k], 20_000)).expect("frame");
+ }
+ assert_eq!(index_of(&c[n - 1].frame(&s[n - 1], 14_000).expect("frame")), 14);
+ assert!(c[n - 1].shared.state.lock().unwrap().decoder.is_some(), "the thumbnails left its decoder");
+ // a cache that lost its decoder still decodes (from the sync sample)
+ assert_eq!(index_of(&with_background(true, || c[1].frame(&s[1], 30_000)).expect("frame")), 30);
+ }
+
+ #[test]
+ fn a_monitor_replaces_a_decoder_made_for_a_thumbnail() {
+ use filmcraft_media::cancel::with_background;
+ let pool = Arc::new(Pool::new(MAX_LIVE_DECODERS, IDLE, FRAME_BUDGET).tiered(RECENT, HARD_DECODERS));
+ let (s, c) = (samples(300, 250, 3, false), GopCache::in_pool(None, pool.clone()));
+ assert_eq!(index_of(&with_background(true, || c.frame(&s, 10_000)).expect("frame")), 10);
+ // another thumbnail of the same clip carries on with the thumbnail decoder
+ assert_eq!(index_of(&with_background(true, || c.frame(&s, 12_000)).expect("frame")), 12);
+ assert_eq!(s.resets.load(Ordering::Relaxed), 1);
+ // a frame a monitor waits for (not cached: far ahead) is decoded by a full-thread decoder
+ assert_eq!(index_of(&c.frame(&s, 100_000).expect("frame")), 100);
+ assert!(!c.shared.state.lock().unwrap().decoder.as_ref().is_some_and(|d| d.thread_limited()));
+ assert_eq!(pool.decoders.load(Ordering::Relaxed), 1);
+ // and keeps it for the next frames
+ let resets = s.resets.load(Ordering::Relaxed);
+ assert_eq!(index_of(&c.frame(&s, 101_000).expect("frame")), 101);
+ assert_eq!(s.resets.load(Ordering::Relaxed), resets);
+ }
+
+ #[test]
+ fn a_burst_of_single_requests_does_not_keep_every_clips_frames() {
+ // 1×1 RGBA pictures (4 bytes): a shared budget of ten of them
+ let pool = Arc::new(Pool::new(usize::MAX, Duration::from_secs(3600), 40).tiered(Duration::from_millis(2), usize::MAX));
+ let n = 12;
+ let s: Vec = (0..n).map(|_| samples(300, 250, 0, false)).collect();
+ let c: Vec = (0..n).map(|_| GopCache::in_pool(None, pool.clone())).collect();
+ for k in 0..n {
+ assert_eq!(index_of(&c[k].frame(&s[k], 10_000).expect("frame")), 10);
+ std::thread::sleep(Duration::from_millis(5));
+ }
+ let held = pool.bytes.load(Ordering::Relaxed);
+ assert!(held <= 40 + 11 * 4, "{held} bytes held after a burst over {n} clips");
+ }
+
#[test]
fn intra_caches_give_up_their_spare_decoders_too() {
let pool = Arc::new(Pool::new(1, Duration::ZERO, FRAME_BUDGET));
diff --git a/crates/codecs/src/hw_frame.rs b/crates/codecs/src/hw_frame.rs
index 59537d808..b37fc4d01 100644
--- a/crates/codecs/src/hw_frame.rs
+++ b/crates/codecs/src/hw_frame.rs
@@ -53,7 +53,7 @@ pub struct FrameStreamInfo {
}
/// The OBUs of a low-overhead AV1 sample: `(type, payload)`, stopping at the first malformed one.
-fn obus(data: &[u8]) -> Vec<(u8, &[u8])> {
+pub(crate) fn obus(data: &[u8]) -> Vec<(u8, &[u8])> {
let mut out = Vec::new();
let mut pos = 0usize;
while let Some(&h) = data.get(pos) {
diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rs
index a14f74d3d..a31da3a7e 100644
--- a/crates/codecs/src/lib.rs
+++ b/crates/codecs/src/lib.rs
@@ -28,6 +28,7 @@ pub mod mp4;
pub mod mpeg;
pub mod mxf;
pub mod ogg;
+mod stream_color;
pub mod video;
use std::sync::{Arc, RwLock};
@@ -173,7 +174,11 @@ pub fn open_bytes(name: &str, bytes: Arc<[u8]>) -> std::result::Result,
}
+/// One playable audio track with its own decoder state and timing tables.
+struct MkvAudio {
+ /// Index into `MkvFile::tracks`.
+ track: usize,
+ state: Mutex,
+ /// Packet start positions in source sample frames.
+ starts: Vec,
+ /// Decoder pre-roll after a seek, in source sample frames (0: prime with one packet).
+ preroll: i64,
+}
+
pub struct MkvSource {
info: MediaInfo,
bytes: crate::Src,
file: MkvFile,
vtrack: Option,
- atrack: Option,
/// Video codec as an ISO-BMFF sample entry (for the decoder factories).
ventry: Option,
video: GopCache,
- audio: Mutex,
- /// Audio packet start positions in source sample frames.
- audio_starts: Vec,
- /// Decoder pre-roll after a seek, in source sample frames (0: prime with one packet).
- audio_preroll: i64,
+ /// The playable audio tracks in file order: `info.audio_streams[k]` describes `audios[k]`.
+ audios: Vec,
+}
+
+/// Audio packet start positions in source sample frames.
+///
+/// Block timestamps are quantised to `TimestampScale` (usually 1 ms), so they are only accurate to a
+/// tick: a 1024-sample AAC frame at 48 kHz lasts 21.33 ms. Packets that all decode to the same
+/// length (AAC, MPEG audio, AC-3) therefore run on from one another, resynchronising to the
+/// timestamp only across gaps of more than half a packet (and more than two ticks); see
+/// [`crate::audio::contiguous_starts`]. Opus packets: [`opus_starts`]. Other codecs start at their
+/// timestamps.
+fn audio_starts(file: &MkvFile, bytes: &crate::Src, ti: usize, rate: i64) -> Vec {
+ use crate::audio::{FixedFrames, PacketTime, contiguous_starts, fixed_packet_samples};
+ let Some(t) = file.tracks.get(ti) else { return Vec::new() };
+ if let Some(h) = opus_head(&t.codec) {
+ return opus_starts(file, bytes, ti, &h, rate);
+ }
+ let (n, d) = tb(t);
+ let at = |pts: i64| (pts as i128 * n as i128 * rate as i128 / d as i128) as i64;
+ let first = || file.read_sample(bytes, ti, 0).unwrap_or_default();
+ let out_rate = u32::try_from(rate).unwrap_or(0);
+ let frame = match &t.codec {
+ Codec::Aac { asc } => fixed_packet_samples(FixedFrames::Aac(asc), &[], out_rate),
+ Codec::Mp3 | Codec::Mp2 => fixed_packet_samples(FixedFrames::MpegAudio, &first(), out_rate),
+ Codec::Ac3 => fixed_packet_samples(FixedFrames::Ac3, &first(), out_rate),
+ _ => None,
+ };
+ let Some(frame) = frame else {
+ return t.samples.iter().map(|s| at(s.pts)).collect();
+ };
+ let packets = t.samples.iter().map(|s| PacketTime { stamp: at(s.pts), stamped: own_stamp(s), samples: Some(frame) });
+ contiguous_starts(packets, (frame / 2).max(stamp_tolerance(t, rate)))
}
/// Sample-exact Opus packet start positions (48 kHz frames, pre-skip removed).
///
-/// Block timestamps are quantised to `TimestampScale` (usually 1 ms) and the demuxer subtracts a
-/// rounded `CodecDelay`, so they are only accurate to a tick. Starts are therefore accumulated from
-/// each packet's TOC duration, resynchronising to the timestamp only across real gaps (more than
-/// two ticks off). Pre-skip is the exact `CodecDelay` (or the header's pre-skip when it is absent).
+/// The demuxer also subtracts a rounded `CodecDelay` from the timestamps. Starts are accumulated
+/// from each packet's TOC duration, resynchronising to the timestamp only across real gaps (more
+/// than two ticks off). Pre-skip is the exact `CodecDelay` (or the header's pre-skip when it is
+/// absent).
fn opus_starts(file: &MkvFile, bytes: &crate::Src, ti: usize, head: &filmcraft_opus::OpusHead, rate: i64) -> Vec {
- let t = &file.tracks[ti];
+ let Some(t) = file.tracks.get(ti) else { return Vec::new() };
let (n, d) = tb(t);
let scale_ns = (n as i128 * 1_000_000_000 / d as i128).max(1);
let delay_ns = t.codec_delay_ns as i128;
// The demuxer's rounding of CodecDelay to ticks (half away from zero), undone here.
let delay_ticks = ((delay_ns + scale_ns / 2) / scale_ns) as i64;
let skip = if t.codec_delay_ns > 0 { (delay_ns * rate as i128 / 1_000_000_000) as i64 } else { head.pre_skip as i64 };
- let at = |pts: i64| ((pts + delay_ticks) as i128 * n as i128 * rate as i128 / d as i128) as i64 - skip;
- let tolerance = (2 * scale_ns * rate as i128 / 1_000_000_000) as i64 + 1;
- let mut starts = Vec::with_capacity(t.samples.len());
- let mut next: Option = None;
- for (i, s) in t.samples.iter().enumerate() {
- let stamped = at(s.pts);
- let start = match next {
- Some(p) if (p - stamped).abs() <= tolerance => p,
- _ => stamped,
- };
- starts.push(start);
- let dur = file.read_sample(bytes, ti, i).ok().and_then(|p| crate::audio::opus_packet_samples(&p));
- next = dur.map(|k| start + k as i64);
- }
- starts
+ let at = |pts: i64| ((pts.saturating_add(delay_ticks) as i128 * n as i128 * rate as i128 / d as i128) as i64).saturating_sub(skip);
+ let packets = t.samples.iter().enumerate().map(|(i, s)| crate::audio::PacketTime {
+ stamp: at(s.pts),
+ stamped: own_stamp(s),
+ samples: file.read_sample(bytes, ti, i).ok().and_then(|p| crate::audio::opus_packet_samples(&p)).map(|k| k as i64),
+ });
+ crate::audio::contiguous_starts(packets, stamp_tolerance(t, rate))
+}
+
+/// Two timestamp ticks in sample frames at `rate`, plus one: how far a packet's Matroska timestamp
+/// can be from its exact start.
+fn stamp_tolerance(t: &filmcraft_matroska::Track, rate: i64) -> i64 {
+ let (n, d) = tb(t);
+ i64::try_from(2 * n as i128 * rate as i128 / d as i128).unwrap_or(i64::MAX).saturating_add(1)
+}
+
+/// Whether a sample's timestamp is its own: frames after the first in a laced block without a
+/// duration repeat the block's timestamp.
+fn own_stamp(s: &filmcraft_matroska::Sample) -> bool {
+ s.lace == 0 || s.duration > 0
}
/// The parsed `OpusHead` of an `A_OPUS` track.
@@ -172,32 +219,24 @@ fn hdr_metadata(c: &filmcraft_matroska::Colour) -> Option (ColorInfo, bool) {
- let mut c = ColorInfo { matrix: filmcraft_frame::default_matrix(w, h), transfer: Transfer::Bt709, primaries: Primaries::Bt709, range: Range::Limited };
- let Some(col) = &v.colour else { return (c, false) };
- let mut explicit = false;
- if let Some(m) = col.matrix_coefficients.and_then(|m| Matrix::from_code(m as u8)) {
- c.matrix = m;
- explicit = true;
- }
- if let Some(t) = col.transfer_characteristics.and_then(|t| Transfer::from_code(t as u8)) {
- c.transfer = t;
- explicit = true;
- }
- if let Some(p) = col.primaries {
- c.primaries = match p {
- 9 => Primaries::Bt2020,
- 12 => Primaries::P3D65,
- 5 => Primaries::Bt601_625,
- 6 => Primaries::Bt601_525,
- _ => Primaries::Bt709,
+/// Colour of a video track: the `Colour` element wins; what it leaves unspecified, or all of it
+/// when there is none, comes from the stream's own description (the SPS VUI in an `avcC` /
+/// `hvcC`, the sequence header in an `av1C`); the rest defaults by frame size. The flag says
+/// whether `Colour` signals anything that overrides the decoder.
+fn color_of(v: &filmcraft_matroska::VideoInfo, w: u32, h: u32, stream: Option) -> (ColorInfo, bool) {
+ // H.273 code points; `Range`: 1 broadcast (limited), 2 full, 0 / 3 not signalled here
+ let code = |c: Option| c.unwrap_or(2);
+ let container = v.colour.as_ref().map(|col| {
+ let range = match col.range {
+ Some(1) => Some(false),
+ Some(2) => Some(true),
+ _ => None,
};
- }
- if col.full_range() {
- c.range = Range::Full;
- explicit = true;
- }
- (c, explicit)
+ ColorCodes::from_wide(code(col.primaries), code(col.transfer_characteristics), code(col.matrix_coefficients), range)
+ });
+ let explicit = container.is_some_and(|c| Matrix::from_code(c.matrix).is_some() || Transfer::from_code(c.transfer).is_some() || c.full_range == Some(true));
+ let sources: Vec = [container, stream].into_iter().flatten().collect();
+ (crate::stream_color::resolve(w, h, &sources), explicit)
}
fn codec_label(c: &Codec) -> String {
@@ -236,8 +275,16 @@ impl MkvSource {
let bytes = crate::Src(reader);
let file = filmcraft_matroska::open(&bytes).map_err(|e| CodecError::Container(e.to_string()))?;
let vtrack = file.tracks.iter().position(|t| t.kind == TrackKind::Video && !t.samples.is_empty());
- let atrack = file.tracks.iter().position(|t| t.kind == TrackKind::Audio && !t.samples.is_empty());
- if vtrack.is_none() && atrack.is_none() {
+ // every audio track is a stream (the track count comes from the file: capped)
+ let atracks: Vec = file
+ .tracks
+ .iter()
+ .enumerate()
+ .filter(|(_, t)| t.kind == TrackKind::Audio && !t.samples.is_empty())
+ .map(|(i, _)| i)
+ .take(filmcraft_media::MAX_AUDIO_STREAMS)
+ .collect();
+ if vtrack.is_none() && atracks.is_empty() {
return Err(CodecError::Unsupported("no playable tracks".into()));
}
let mut explicit_color = None;
@@ -258,11 +305,11 @@ impl MkvSource {
FrameRate::from_f64(1.0 / step)
}
};
- let (color, explicit) = color_of(&v, w, h);
+ ventry = sample_entry(&t.codec, &t.codec_private, t.video.as_ref(), w as u16, h as u16);
+ let (color, explicit) = color_of(&v, w, h, ventry.as_ref().and_then(|e| crate::stream_color::from_codec_config(&e.codec)));
if explicit {
explicit_color = Some(color);
}
- ventry = sample_entry(&t.codec, &t.codec_private, t.video.as_ref(), w as u16, h as u16);
let secs = file.duration_ns().unwrap_or(0) as f64 / 1e9;
let bitrate = (secs > 0.0).then(|| (t.samples.iter().map(|s| s.size as u64).sum::() as f64 * 8.0 / secs) as u64);
let ((w, h), par) = if rotation % 2 == 1 { ((h, w), (v.pixel_aspect().1, v.pixel_aspect().0)) } else { ((w, h), v.pixel_aspect()) };
@@ -279,21 +326,24 @@ impl MkvSource {
hdr: v.colour.as_ref().and_then(hdr_metadata),
}
});
- let audio = atrack.map(|i| {
- let t = &file.tracks[i];
- let a = t.audio.clone().unwrap_or_default();
- let mut rate = a.output_sampling_frequency.unwrap_or(a.sampling_frequency).round().max(1.0) as u32;
- let mut channels = (a.channels as u32).max(1);
- if let Some(h) = opus_head(&t.codec) {
- rate = crate::audio::OPUS_RATE;
- channels = h.channels as u32;
- }
- AudioStreamInfo { sample_rate: rate, channels, codec: codec_label(&t.codec), bits_per_sample: a.bit_depth.map(|b| b as u32) }
- });
+ let audio_streams: Vec = atracks
+ .iter()
+ .filter_map(|&i| file.tracks.get(i))
+ .map(|t| {
+ let a = t.audio.clone().unwrap_or_default();
+ let mut rate = a.output_sampling_frequency.unwrap_or(a.sampling_frequency).round().max(1.0) as u32;
+ let mut channels = (a.channels as u32).max(1);
+ if let Some(h) = opus_head(&t.codec) {
+ rate = crate::audio::OPUS_RATE;
+ channels = h.channels as u32;
+ }
+ AudioStreamInfo { sample_rate: rate, channels, codec: codec_label(&t.codec), bits_per_sample: a.bit_depth.map(|b| b as u32) }
+ })
+ .collect();
let duration = match file.duration_ns() {
Some(ns) => Tick::from_rational(ns as i64, 1, 1_000_000_000),
None => vtrack
- .or(atrack)
+ .or(atracks.first().copied())
.map(|i| {
let t = &file.tracks[i];
let end = t.samples.iter().map(|s| s.pts + s.duration as i64).max().unwrap_or(0);
@@ -306,48 +356,31 @@ impl MkvSource {
kind: if video.is_some() { MediaKind::Movie } else { MediaKind::AudioOnly },
duration,
video,
- audio,
+ audio_streams,
container: if file.is_webm() { "WebM".into() } else { "Matroska".into() },
start_timecode: None,
file_size: Some(bytes.0.len()),
};
- let audio_starts = atrack
- .map(|i| {
- let t = &file.tracks[i];
- let rate = info.audio.as_ref().map_or(48_000, |a| a.sample_rate) as i64;
- let (n, d) = tb(t);
- let at = |pts: i64| (pts as i128 * n as i128 * rate as i128 / d as i128) as i64;
- match opus_head(&t.codec) {
- Some(h) => opus_starts(&file, &bytes, i, &h, rate),
- None => t.samples.iter().map(|s| at(s.pts)).collect(),
- }
- })
- .unwrap_or_default();
- let audio_preroll = atrack
- .map(|i| {
- let t = &file.tracks[i];
- let rate = info.audio.as_ref().map_or(48_000, |a| a.sample_rate) as i64;
- match t.codec {
+ // `info.audio_streams[k]` was made from `atracks[k]`, so the two stay index-aligned
+ let audios: Vec = atracks
+ .iter()
+ .zip(&info.audio_streams)
+ .filter_map(|(&i, ainfo)| {
+ let t = file.tracks.get(i)?;
+ let rate = ainfo.sample_rate as i64;
+ let starts = audio_starts(&file, &bytes, i, rate);
+ let preroll = match t.codec {
Codec::Opus { .. } => {
let container = (t.seek_pre_roll_ns as u128 * rate as u128).div_ceil(1_000_000_000) as i64;
container.max(crate::audio::OPUS_PRE_ROLL as i64 * rate / 48_000)
}
_ => 0,
- }
+ };
+ let state = Mutex::new(AudioState { decoder: None, packets: HashMap::new(), order: Vec::new(), last_decoded: None });
+ Some(MkvAudio { track: i, state, starts, preroll })
})
- .unwrap_or(0);
- Ok(Self {
- info,
- bytes,
- file,
- vtrack,
- atrack,
- ventry,
- video: GopCache::new(explicit_color).with_rotation(rotation),
- audio: Mutex::new(AudioState { decoder: None, packets: HashMap::new(), order: Vec::new(), last_decoded: None }),
- audio_starts,
- audio_preroll,
- })
+ .collect();
+ Ok(Self { info, bytes, file, vtrack, ventry, video: GopCache::new(explicit_color).with_rotation(rotation), audios })
}
fn read(&self, track: usize, i: usize) -> crate::Result> {
@@ -373,13 +406,14 @@ impl MkvSource {
}
}
- fn audio_packet(&self, st: &mut AudioState, i: usize) -> crate::Result>>> {
+ /// Decoded packet `i` of audio stream `a` (`ainfo` is its `AudioStreamInfo`; `st` its decoder state).
+ fn audio_packet(&self, a: &MkvAudio, ainfo: &AudioStreamInfo, st: &mut AudioState, i: usize) -> crate::Result>>> {
if let Some(p) = st.packets.get(&i) {
return Ok(p.clone());
}
- let ti = self.atrack.ok_or_else(|| CodecError::Unsupported("no audio track".into()))?;
- let track = &self.file.tracks[ti];
- let ainfo = self.info.audio.as_ref().ok_or_else(|| CodecError::Unsupported("no audio track".into()))?;
+ let ti = a.track;
+ let track = self.file.tracks.get(ti).ok_or_else(|| CodecError::Unsupported("no audio track".into()))?;
+ let pk_start = a.starts.get(i).copied().ok_or_else(|| CodecError::Container("audio packet out of range".into()))?;
let data = self.read(ti, i)?;
let decoded = match &track.codec {
Codec::Pcm { float, big_endian, bits } => {
@@ -402,8 +436,8 @@ impl MkvSource {
if st.last_decoded.is_none_or(|l| l + 1 != i) {
let d = st.decoder.as_mut().ok_or_else(|| CodecError::Decode("no audio decoder".into()))?;
d.reset();
- let from = if self.audio_preroll > 0 {
- self.audio_starts.partition_point(|&x| x <= self.audio_starts[i] - self.audio_preroll).saturating_sub(1)
+ let from = if a.preroll > 0 {
+ a.starts.partition_point(|&x| x <= pk_start.saturating_sub(a.preroll)).saturating_sub(1)
} else {
i.saturating_sub(1)
};
@@ -413,7 +447,7 @@ impl MkvSource {
}
}
}
- let r = st.decoder.as_mut().ok_or_else(|| CodecError::Decode("no audio decoder".into()))?.decode(&data, self.audio_starts[i].max(0) as u64);
+ let r = st.decoder.as_mut().ok_or_else(|| CodecError::Decode("no audio decoder".into()))?.decode(&data, pk_start.max(0) as u64);
st.last_decoded = Some(i);
r.unwrap_or_default()
}
@@ -473,23 +507,27 @@ impl MediaSource for MkvSource {
}
fn audio(&self, start: i64, frames: usize, sample_rate: u32) -> Result {
- let ti = self.atrack.ok_or(MediaError::NoStream("audio"))?;
- let n_packets = self.file.tracks[ti].samples.len();
- let ainfo = self.info.audio.as_ref().ok_or(MediaError::NoStream("audio"))?;
+ self.audio_stream(0, start, frames, sample_rate)
+ }
+
+ fn audio_stream(&self, stream: usize, start: i64, frames: usize, sample_rate: u32) -> Result {
+ let a = self.audios.get(stream).ok_or(MediaError::NoStream("audio"))?;
+ let n_packets = self.file.tracks.get(a.track).ok_or(MediaError::NoStream("audio"))?.samples.len();
+ let ainfo = self.info.audio_streams.get(stream).ok_or(MediaError::NoStream("audio"))?;
let src_rate = ainfo.sample_rate;
let ch = ainfo.channels.max(1) as usize;
let ratio = src_rate as f64 / sample_rate as f64;
let s0 = (start as f64 * ratio).floor() as i64;
let need = (frames as f64 * ratio).ceil() as i64 + 2;
let mut src: Vec> = vec![vec![0.0; need.max(0) as usize]; ch];
- let mut st = self.audio.lock().unwrap_or_else(|e| e.into_inner());
- let mut i = self.audio_starts.partition_point(|&x| x <= s0).saturating_sub(1);
+ let mut st = a.state.lock().unwrap_or_else(|e| e.into_inner());
+ let mut i = a.starts.partition_point(|&x| x <= s0).saturating_sub(1);
while i < n_packets {
- let pk_start = self.audio_starts[i];
+ let Some(&pk_start) = a.starts.get(i) else { break };
if pk_start >= s0 + need {
break;
}
- let pk = self.audio_packet(&mut st, i)?;
+ let pk = self.audio_packet(a, ainfo, &mut st, i)?;
for (c, dst) in src.iter_mut().enumerate() {
let Some(chan) = pk.get(c.min(pk.len().saturating_sub(1))) else { continue };
for (k, v) in chan.iter().enumerate() {
@@ -532,3 +570,34 @@ pub fn reader_opener(name: &str, head: &[u8], reader: &filmcraft_media::SharedRe
}
Some(MkvSource::open_reader(name, reader.clone()).map(|s| Arc::new(s) as SharedSource).map_err(Into::into))
}
+
+#[cfg(test)]
+mod multi_audio_tests {
+ use filmcraft_matroska::{MkvWriter, MuxOptions, TrackKind, TrackSpec};
+ use filmcraft_media::MediaSource;
+
+ #[test]
+ fn every_audio_track_is_inspected_and_decoded_independently() {
+ let tracks: Vec = [(48_000.0, "first"), (24_000.0, "second")]
+ .into_iter()
+ .map(|(rate, name)| {
+ let mut t = TrackSpec::new(TrackKind::Audio, "A_PCM/INT/LIT");
+ t.audio = Some((rate, 1, Some(16)));
+ t.name = Some(name.into());
+ t
+ })
+ .collect();
+ let mut writer = MkvWriter::new(std::io::Cursor::new(Vec::new()), tracks, MuxOptions::default()).unwrap();
+ for (stream, value) in [(0, 8192i16), (1, -16384)] {
+ let data: Vec = (0..100).flat_map(|_| value.to_le_bytes()).collect();
+ writer.write_frame(stream, 0, true, &data, Some(4_000_000)).unwrap();
+ }
+ let source = super::MkvSource::open("two.mkv", writer.finish().unwrap().into_inner().into()).unwrap();
+ assert_eq!(source.info().audio_streams.iter().map(|a| a.sample_rate).collect::>(), vec![48_000, 24_000]);
+ let primary = source.audio(0, 50, 48_000).unwrap();
+ let secondary = source.audio_stream(1, 0, 50, 48_000).unwrap();
+ assert!((primary.channels[0][20] - 0.25).abs() < 0.001);
+ assert!((secondary.channels[0][20] + 0.5).abs() < 0.001);
+ assert!(source.audio_stream(2, 0, 50, 48_000).is_err());
+ }
+}
diff --git a/crates/codecs/src/mp4.rs b/crates/codecs/src/mp4.rs
index 2fedf3be1..9ef2792be 100644
--- a/crates/codecs/src/mp4.rs
+++ b/crates/codecs/src/mp4.rs
@@ -7,7 +7,7 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
-use filmcraft_color::{ColorInfo, Matrix, Primaries, Range, Transfer};
+use filmcraft_color::ColorInfo;
use filmcraft_frame::{AudioBuffer, VideoFrame};
use filmcraft_isobmff::{CodecConfig, Mp4File, TrackKind};
use filmcraft_media::{AudioStreamInfo, FrameRequest, MediaError, MediaInfo, MediaKind, MediaSource, SharedSource, VideoStreamInfo};
@@ -29,26 +29,67 @@ struct AudioState {
last_decoded: Option,
}
+/// One playable audio track with its own decoder state and timing tables.
+struct Mp4Audio {
+ /// Index into `Mp4File::tracks`.
+ track: usize,
+ state: Mutex,
+ /// Packet start positions in source sample frames (see [`audio_starts`]).
+ starts: Vec,
+ /// Presentation offset of the track in its timescale (edit list, or Opus pre-skip).
+ offset: i64,
+ /// Decoder pre-roll after a seek, in source sample frames (0: prime with one packet).
+ preroll: i64,
+}
+
pub struct Mp4Source {
info: MediaInfo,
bytes: crate::Src,
file: Mp4File,
vtrack: Option,
- atrack: Option,
video: GopCache,
- audio: Mutex,
- /// Cumulative sample start frames for the audio track (for packet lookup).
- audio_starts: Vec,
- /// Presentation offset of the audio track in its timescale (edit list, or Opus pre-skip).
- audio_offset: i64,
- /// Decoder pre-roll after a seek, in audio track timescale units (0: prime with one packet).
- audio_preroll: i64,
+ /// The playable audio tracks in file order: `info.audio_streams[k]` describes `audios[k]`.
+ audios: Vec,
}
pub fn sniff(b: &[u8]) -> bool {
b.len() >= 12 && matches!(&b[4..8], b"ftyp" | b"moov" | b"mdat" | b"wide" | b"free" | b"skip")
}
+/// Audio packet start positions in source sample frames at `rate`: the running total of the sample
+/// durations, except that packets which all decode to the same length (AAC, MPEG audio, AC-3) run
+/// on from one another, resynchronising to the total only across gaps of more than half a packet.
+/// A remux from Matroska (OBS's) carries the millisecond rounding of its timestamps into the
+/// durations: 1008, 1008, 1056… for 1024-sample AAC frames at 48 kHz (see
+/// [`crate::audio::contiguous_starts`]).
+fn audio_starts(file: &Mp4File, bytes: &crate::Src, ti: usize, rate: u32) -> Vec {
+ use crate::audio::{FixedFrames, PacketTime, contiguous_starts, fixed_packet_samples};
+ let Some(t) = file.tracks.get(ti) else { return Vec::new() };
+ let ts = i128::from(t.timescale.max(1));
+ let mut total = 0i128;
+ let stamps: Vec = t
+ .samples
+ .iter()
+ .map(|s| {
+ let at = total * i128::from(rate) / ts;
+ total += i128::from(s.duration);
+ i64::try_from(at).unwrap_or(i64::MAX)
+ })
+ .collect();
+ let first = || file.read_sample(bytes, ti, 0).unwrap_or_default();
+ let frame = match t.entries.first().map(|e| &e.codec) {
+ Some(CodecConfig::Aac(a)) => fixed_packet_samples(FixedFrames::Aac(&a.asc), &[], rate),
+ Some(CodecConfig::Mp3) => fixed_packet_samples(FixedFrames::MpegAudio, &first(), rate),
+ Some(CodecConfig::Ac3 { .. }) => fixed_packet_samples(FixedFrames::Ac3, &first(), rate),
+ _ => None,
+ };
+ let Some(frame) = frame else { return stamps };
+ // samples per track timescale unit, rounded up: how coarse the running total is
+ let unit = i64::try_from((i128::from(rate) + ts - 1) / ts).unwrap_or(i64::MAX);
+ let packets = stamps.iter().map(|&stamp| PacketTime { stamp, stamped: true, samples: Some(frame) });
+ contiguous_starts(packets, (frame / 2).max(unit.saturating_mul(2).saturating_add(1)))
+}
+
/// HDR static metadata from the sample entry's `mdcv` / `clli` boxes (0 = unknown).
fn hdr_metadata(md: Option<&filmcraft_isobmff::MasteringDisplay>, cll: Option<(u16, u16)>) -> Option {
if md.is_none() && cll.is_none() {
@@ -63,48 +104,40 @@ fn hdr_metadata(md: Option<&filmcraft_isobmff::MasteringDisplay>, cll: Option<(u
})
}
+/// Colour of a video sample entry. The `colr` box (`nclx` / `nclc`) wins; what it leaves
+/// unspecified, or all of it when there is none (ffmpeg writes MP4 without `colr` by default),
+/// comes from the stream's own description: `vpcC` / `apvC`, or the SPS VUI in `avcC` / `hvcC`
+/// and the sequence header in `av1C`. The rest defaults by frame size.
fn color_from(entry: &filmcraft_isobmff::SampleEntry, w: u32, h: u32) -> ColorInfo {
- let mut c = ColorInfo { matrix: filmcraft_frame::default_matrix(w, h), transfer: Transfer::Bt709, primaries: Primaries::Bt709, range: Range::Limited };
- // VP9 and APV carry their colour description in vpcC / apvC (used when there is no colr box).
- let vpc = match &entry.codec {
- CodecConfig::Vp9(v) => Some(filmcraft_isobmff::ColorInfo::Nclx {
- primaries: v.colour_primaries as u16,
- transfer: v.transfer_characteristics as u16,
- matrix: v.matrix_coefficients as u16,
- full_range: v.full_range,
- }),
- CodecConfig::Apv(a) if a.color_description_present => Some(filmcraft_isobmff::ColorInfo::Nclx {
- primaries: a.color_primaries as u16,
- transfer: a.transfer_characteristics as u16,
- matrix: a.matrix_coefficients as u16,
- full_range: a.full_range,
- }),
+ use crate::stream_color::ColorCodes;
+ use filmcraft_isobmff::ColorInfo as Colr;
+ let colr = match entry.video.as_ref().and_then(|v| v.color.as_ref()) {
+ Some(Colr::Nclx { primaries, transfer, matrix, full_range }) => {
+ Some(ColorCodes::from_wide((*primaries).into(), (*transfer).into(), (*matrix).into(), Some(*full_range)))
+ }
+ // QuickTime `nclc` has no range flag
+ Some(Colr::Nclc { primaries, transfer, matrix }) => Some(ColorCodes::from_wide((*primaries).into(), (*transfer).into(), (*matrix).into(), None)),
+ // an ICC profile, or no `colr`: no code points
_ => None,
};
- if let Some(col) = entry.video.as_ref().and_then(|v| v.color.as_ref()).or(vpc.as_ref()) {
- let (p, t, m, full) = match col {
- filmcraft_isobmff::ColorInfo::Nclx { primaries, transfer, matrix, full_range } => (*primaries, *transfer, *matrix, *full_range),
- filmcraft_isobmff::ColorInfo::Nclc { primaries, transfer, matrix } => (*primaries, *transfer, *matrix, false),
- _ => return c,
- };
- if let Some(m) = Matrix::from_code(m as u8) {
- c.matrix = m;
- }
- if let Some(t) = Transfer::from_code(t as u8) {
- c.transfer = t;
- }
- c.primaries = match p {
- 9 => Primaries::Bt2020,
- 12 => Primaries::P3D65,
- 5 => Primaries::Bt601_625,
- 6 => Primaries::Bt601_525,
- _ => Primaries::Bt709,
- };
- if full {
- c.range = Range::Full;
- }
- }
- c
+ let stream = match &entry.codec {
+ CodecConfig::Vp9(v) => Some(ColorCodes {
+ primaries: v.colour_primaries,
+ transfer: v.transfer_characteristics,
+ matrix: v.matrix_coefficients,
+ full_range: Some(v.full_range),
+ }),
+ CodecConfig::Apv(a) if a.color_description_present => Some(ColorCodes {
+ primaries: a.color_primaries,
+ transfer: a.transfer_characteristics,
+ matrix: a.matrix_coefficients,
+ full_range: Some(a.full_range),
+ }),
+ CodecConfig::Apv(_) => None,
+ codec => crate::stream_color::from_codec_config(codec),
+ };
+ let sources: Vec = [colr, stream].into_iter().flatten().collect();
+ crate::stream_color::resolve(w, h, &sources)
}
impl Mp4Source {
@@ -120,8 +153,10 @@ impl Mp4Source {
// files can lack them: indexing `entries[0]` or dividing by the timescale used to panic).
let playable = |t: &&filmcraft_isobmff::Track, kind| t.kind == kind && !t.samples.is_empty() && !t.entries.is_empty() && t.timescale > 0;
let vtrack = file.tracks.iter().position(|t| playable(&t, TrackKind::Video));
- let atrack = file.tracks.iter().position(|t| playable(&t, TrackKind::Audio));
- if vtrack.is_none() && atrack.is_none() {
+ // every audio track is a stream (the track count comes from the file: capped)
+ let atracks: Vec =
+ file.tracks.iter().enumerate().filter(|(_, t)| playable(t, TrackKind::Audio)).map(|(i, _)| i).take(filmcraft_media::MAX_AUDIO_STREAMS).collect();
+ if vtrack.is_none() && atracks.is_empty() {
return Err(CodecError::Unsupported("no playable tracks".into()));
}
let mut color = ColorInfo::REC709;
@@ -195,37 +230,11 @@ impl Mp4Source {
}
info
});
- let audio = atrack.map(|i| {
- let t = &file.tracks[i];
- let entry = &t.entries[0];
- let ap = entry.audio.clone().unwrap_or_default();
- let (rate, ch, bits) = match &entry.codec {
- CodecConfig::Aac(a) => (
- {
- // HE-AAC plays at twice the core rate the AudioSpecificConfig starts with
- // an AAC access unit is at most 6144 bits per channel: a hostile `stsz` can't make us read more
- let units = t
- .samples
- .iter()
- .take(8)
- .filter_map(|x| filmcraft_media::reader::read_range(&*bytes.0, x.offset, (x.size as usize).min(MAX_AAC_PROBE_UNIT)).ok());
- let units: Vec> = units.collect();
- match crate::audio::aac_output_rate(&a.asc, units.iter().map(Vec::as_slice)) {
- Some(r) => r,
- None if a.sample_rate > 0 => a.sample_rate,
- None => ap.sample_rate as u32,
- }
- },
- if a.channel_config > 0 { a.channel_config as u32 } else { ap.channels },
- None,
- ),
- CodecConfig::Pcm(p) => (p.sample_rate as u32, p.channels, Some(p.bits as u32)),
- CodecConfig::Opus(o) => (crate::audio::OPUS_RATE, (o.output_channels as u32).max(1), None),
- _ => (if ap.sample_rate > 0.0 { ap.sample_rate as u32 } else { t.timescale }, ap.channels.max(1), None),
- };
- AudioStreamInfo { sample_rate: rate.max(1), channels: ch.max(1), codec: codec_label(&entry.codec), bits_per_sample: bits }
- });
- let duration = vtrack.or(atrack).and_then(|i| file.tracks.get(i)).map(|t| presentation_duration(&file, t)).unwrap_or_default();
+ // one entry per playable audio track (`playable` guarantees a sample description); a track
+ // that somehow has none is dropped so `audio_streams` and `audios` stay index-aligned
+ let (atracks, audio_streams): (Vec, Vec) =
+ atracks.into_iter().filter_map(|i| Some((i, audio_stream_info(file.tracks.get(i)?, &bytes)?))).unzip();
+ let duration = vtrack.or(atracks.first().copied()).and_then(|i| file.tracks.get(i)).map(|t| presentation_duration(&file, t)).unwrap_or_default();
let start_timecode = file.tracks.iter().find_map(|t| match t.codec() {
Some(CodecConfig::Timecode(tc)) => tc.start_frame.map(|f| f as i64),
_ => None,
@@ -235,50 +244,31 @@ impl Mp4Source {
kind: if video.is_some() { MediaKind::Movie } else { MediaKind::AudioOnly },
duration,
video,
- audio,
+ audio_streams,
container: if file.is_quicktime { "QuickTime".into() } else { "MPEG-4".into() },
start_timecode,
file_size: Some(bytes.0.len()),
};
- let audio_starts = atrack
- .map(|i| {
- let t = &file.tracks[i];
- let mut acc = 0i64;
- t.samples
- .iter()
- .map(|s| {
- let st = acc;
- acc += s.duration as i64;
- st
- })
- .collect()
- })
- .unwrap_or_default();
- let (audio_offset, audio_preroll) = atrack
- .map(|i| {
- let t = &file.tracks[i];
+ let audios: Vec = atracks
+ .iter()
+ .zip(&info.audio_streams)
+ .filter_map(|(&i, ainfo)| {
+ let t = file.tracks.get(i)?;
+ let starts = audio_starts(&file, &bytes, i, ainfo.sample_rate);
let ts = t.timescale.max(1) as i64;
- match &t.entries[0].codec {
- CodecConfig::Opus(o) => {
+ let (offset, preroll) = match t.entries.first().map(|e| &e.codec) {
+ Some(CodecConfig::Opus(o)) => {
let off = if t.edit_offset != 0 { t.edit_offset } else { -(o.pre_skip as i64) * ts / 48_000 };
- (off, (crate::audio::OPUS_PRE_ROLL as u64 * ts as u64).div_ceil(48_000) as i64)
+ // (Opus plays at 48 kHz: the pre-roll is in its sample frames)
+ (off, crate::audio::OPUS_PRE_ROLL as i64)
}
_ => (t.edit_offset, 0),
- }
+ };
+ let state = Mutex::new(AudioState { decoder: None, packets: HashMap::new(), order: Vec::new(), last_decoded: None });
+ Some(Mp4Audio { track: i, state, starts, offset, preroll })
})
- .unwrap_or((0, 0));
- Ok(Self {
- info,
- bytes,
- file,
- vtrack,
- atrack,
- video: GopCache::new(explicit_color).with_rotation(rotation),
- audio: Mutex::new(AudioState { decoder: None, packets: HashMap::new(), order: Vec::new(), last_decoded: None }),
- audio_starts,
- audio_offset,
- audio_preroll,
- })
+ .collect();
+ Ok(Self { info, bytes, file, vtrack, video: GopCache::new(explicit_color).with_rotation(rotation), audios })
}
fn read(&self, track: usize, i: usize) -> crate::Result> {
@@ -294,27 +284,30 @@ impl Mp4Source {
self.video.frame_late(&Mp4Video { src: self, track: ti }, t.to_rational_round(1, ts), late)
}
- fn audio_packet(&self, st: &mut AudioState, i: usize) -> crate::Result>>> {
+ /// Decoded packet `i` of audio stream `a` (`ainfo` is its `AudioStreamInfo`; `st` its decoder state).
+ fn audio_packet(&self, a: &Mp4Audio, ainfo: &AudioStreamInfo, st: &mut AudioState, i: usize) -> crate::Result>>> {
if let Some(p) = st.packets.get(&i) {
return Ok(p.clone());
}
- let ti = self.atrack.ok_or_else(|| CodecError::Unsupported("no audio track".into()))?;
- let track = &self.file.tracks[ti];
+ let ti = a.track;
+ let track = self.file.tracks.get(ti).ok_or_else(|| CodecError::Unsupported("no audio track".into()))?;
let entry = track.entries.first().ok_or_else(|| CodecError::Container("audio track without a sample description".into()))?;
+ let sample = track.samples.get(i).ok_or_else(|| CodecError::Container("audio packet out of range".into()))?;
+ let pk_start = a.starts.get(i).copied().unwrap_or(0);
let data = self.read(ti, i)?;
let decoded = match &entry.codec {
CodecConfig::Pcm(p) => decode_pcm(&data, p),
c => {
if st.decoder.is_none() {
- st.decoder = Some(PacketDecoder::for_isobmff(c, self.info.audio.as_ref().map_or(48_000, |a| a.sample_rate))?);
+ st.decoder = Some(PacketDecoder::for_isobmff(c, ainfo.sample_rate)?);
}
// Non-sequential access: reset and prime with the preceding packets (codec pre-roll:
// one packet, or `OPUS_PRE_ROLL` worth for Opus).
if st.last_decoded.is_none_or(|l| l + 1 != i) {
let d = st.decoder.as_mut().ok_or_else(|| CodecError::Decode("no audio decoder".into()))?;
d.reset();
- let from = if self.audio_preroll > 0 {
- self.audio_starts.partition_point(|&x| x <= self.audio_starts[i] - self.audio_preroll).saturating_sub(1)
+ let from = if a.preroll > 0 {
+ a.starts.partition_point(|&x| x <= pk_start.saturating_sub(a.preroll)).saturating_sub(1)
} else {
i.saturating_sub(1)
};
@@ -324,11 +317,11 @@ impl Mp4Source {
}
}
}
- let r = st.decoder.as_mut().ok_or_else(|| CodecError::Decode("no audio decoder".into()))?.decode(&data, self.audio_starts[i].max(0) as u64);
+ let r = st.decoder.as_mut().ok_or_else(|| CodecError::Decode("no audio decoder".into()))?.decode(&data, pk_start.max(0) as u64);
st.last_decoded = Some(i);
match r {
Ok(v) => v,
- Err(_) => vec![vec![0.0; track.samples[i].duration as usize]; self.info.audio.as_ref().map_or(2, |a| a.channels as usize)],
+ Err(_) => vec![vec![0.0; sample.duration as usize]; ainfo.channels.max(1) as usize],
}
}
};
@@ -381,30 +374,33 @@ impl MediaSource for Mp4Source {
}
fn audio(&self, start: i64, frames: usize, sample_rate: u32) -> Result {
- let ti = self.atrack.ok_or(MediaError::NoStream("audio"))?;
- let track = &self.file.tracks[ti];
- let ainfo = self.info.audio.as_ref().ok_or(MediaError::NoStream("audio"))?;
+ self.audio_stream(0, start, frames, sample_rate)
+ }
+
+ fn audio_stream(&self, stream: usize, start: i64, frames: usize, sample_rate: u32) -> Result {
+ let a = self.audios.get(stream).ok_or(MediaError::NoStream("audio"))?;
+ let ti = a.track;
+ let track = self.file.tracks.get(ti).ok_or(MediaError::NoStream("audio"))?;
+ let ainfo = self.info.audio_streams.get(stream).ok_or(MediaError::NoStream("audio"))?;
let src_rate = ainfo.sample_rate;
let ch = ainfo.channels.max(1) as usize;
// Map the requested window to source samples (edit list offset applied: presentation = pts + edit_offset).
let ratio = src_rate as f64 / sample_rate as f64;
let p0 = (start as f64 * ratio).floor() as i64;
// (in i128: a hostile edit list can make the offset large enough to overflow i64)
- let offset = i128::from(self.audio_offset) * i128::from(src_rate) / i128::from(track.timescale.max(1));
+ let offset = i128::from(a.offset) * i128::from(src_rate) / i128::from(track.timescale.max(1));
let s0 = i64::try_from(i128::from(p0) - offset).unwrap_or(if offset > 0 { i64::MIN } else { i64::MAX });
let need = (frames as f64 * ratio).ceil() as i64 + 2;
- // Samples-per-unit: the track timescale is usually the sample rate for audio.
- let unit = src_rate as f64 / track.timescale.max(1) as f64;
let mut src: Vec> = vec![vec![0.0; need.max(0) as usize]; ch];
- let mut st = self.audio.lock().unwrap_or_else(|e| e.into_inner());
- let first = self.audio_starts.partition_point(|&x| (x as f64 * unit) as i64 <= s0.max(0)).saturating_sub(1);
+ let mut st = a.state.lock().unwrap_or_else(|e| e.into_inner());
+ let first = a.starts.partition_point(|&x| x <= s0.max(0)).saturating_sub(1);
let mut i = first;
while i < track.samples.len() {
- let pk_start = (self.audio_starts[i] as f64 * unit) as i64;
+ let Some(&pk_start) = a.starts.get(i) else { break };
if pk_start >= s0.saturating_add(need) {
break;
}
- let pk = self.audio_packet(&mut st, i)?;
+ let pk = self.audio_packet(a, ainfo, &mut st, i)?;
for (c, dst) in src.iter_mut().enumerate() {
let Some(chan) = pk.get(c.min(pk.len().saturating_sub(1))) else { continue };
for (k, v) in chan.iter().enumerate() {
@@ -443,6 +439,37 @@ impl MediaSource for Mp4Source {
}
}
+/// What the file says about one audio track (`None`: the track has no sample description).
+fn audio_stream_info(t: &filmcraft_isobmff::Track, bytes: &crate::Src) -> Option {
+ let entry = t.entries.first()?;
+ let ap = entry.audio.clone().unwrap_or_default();
+ let (rate, ch, bits) = match &entry.codec {
+ CodecConfig::Aac(a) => (
+ {
+ // HE-AAC plays at twice the core rate the AudioSpecificConfig starts with
+ // an AAC access unit is at most 6144 bits per channel: a hostile `stsz` can't make us read more
+ let units = t
+ .samples
+ .iter()
+ .take(8)
+ .filter_map(|x| filmcraft_media::reader::read_range(&*bytes.0, x.offset, (x.size as usize).min(MAX_AAC_PROBE_UNIT)).ok());
+ let units: Vec> = units.collect();
+ match crate::audio::aac_output_rate(&a.asc, units.iter().map(Vec::as_slice)) {
+ Some(r) => r,
+ None if a.sample_rate > 0 => a.sample_rate,
+ None => ap.sample_rate as u32,
+ }
+ },
+ if a.channel_config > 0 { a.channel_config as u32 } else { ap.channels },
+ None,
+ ),
+ CodecConfig::Pcm(p) => (p.sample_rate as u32, p.channels, Some(p.bits as u32)),
+ CodecConfig::Opus(o) => (crate::audio::OPUS_RATE, (o.output_channels as u32).max(1), None),
+ _ => (if ap.sample_rate > 0.0 { ap.sample_rate as u32 } else { t.timescale }, ap.channels.max(1), None),
+ };
+ Some(AudioStreamInfo { sample_rate: rate.max(1), channels: ch.max(1), codec: codec_label(&entry.codec), bits_per_sample: bits })
+}
+
fn codec_label(c: &CodecConfig) -> String {
match c {
CodecConfig::Avc(a) => format!(
@@ -803,6 +830,33 @@ mod tests {
}
}
+#[cfg(test)]
+mod multi_audio_tests {
+ use filmcraft_isobmff::{Brand, Mp4Writer, PcmConfig, SampleEntry, TrackConfig, WriteSample, WriterOptions};
+ use filmcraft_media::MediaSource;
+
+ /// Two mono PCM tracks with different rates and samples. Stream indices follow file order;
+ /// decoder caches, sample timing and resampling must not cross streams.
+ #[test]
+ fn every_audio_track_is_inspected_and_decoded_independently() {
+ let mut mux = Mp4Writer::new(std::io::Cursor::new(Vec::new()), WriterOptions::new(Brand::Mov)).unwrap();
+ for (rate, value) in [(48_000u32, 8192i16), (24_000, -16384)] {
+ let entry = SampleEntry::pcm(PcmConfig { bits: 16, float: false, big_endian: false, signed: true, channels: 1, sample_rate: rate as f64 });
+ let track = mux.add_track(TrackConfig::new(entry, rate)).unwrap();
+ let data: Vec = (0..rate / 10).flat_map(|_| value.to_le_bytes()).collect();
+ mux.write_sample(track, WriteSample { data: &data, duration: rate / 10, composition_offset: 0, is_sync: true }).unwrap();
+ }
+ let bytes = mux.finish().unwrap().into_inner();
+ let source = super::Mp4Source::open("two.mov", bytes.into()).unwrap();
+ assert_eq!(source.info().audio_streams.iter().map(|a| a.sample_rate).collect::>(), vec![48_000, 24_000]);
+ let primary = source.audio(0, 100, 48_000).unwrap();
+ let secondary = source.audio_stream(1, 0, 100, 48_000).unwrap();
+ assert!((primary.channels[0][50] - 0.25).abs() < 0.001);
+ assert!((secondary.channels[0][50] + 0.5).abs() < 0.001);
+ assert!(source.audio_stream(2, 0, 100, 48_000).is_err());
+ }
+}
+
#[cfg(test)]
mod he_aac_tests {
use filmcraft_isobmff::{Brand, Mp4Writer, SampleEntry, TrackConfig, WriteSample, WriterOptions};
@@ -829,7 +883,7 @@ mod he_aac_tests {
}
let b = mux.finish().unwrap().into_inner();
let src = super::Mp4Source::open("he.mp4", b.into()).unwrap();
- assert_eq!(src.info().audio.as_ref().unwrap().sample_rate, 44_100);
+ assert_eq!(src.info().audio().unwrap().sample_rate, 44_100);
// one second from 0.5 s at 44.1 kHz: the 1 kHz tone at its level (not silence, not shifted)
let buf = src.audio(22_050, 44_100, 44_100).unwrap();
let x = &buf.channels[0];
diff --git a/crates/codecs/src/mpeg.rs b/crates/codecs/src/mpeg.rs
index c2d0ba67e..ef2a69994 100644
--- a/crates/codecs/src/mpeg.rs
+++ b/crates/codecs/src/mpeg.rs
@@ -211,7 +211,7 @@ impl MpegSource {
kind: MediaKind::Movie,
duration: Tick::ZERO,
video: None,
- audio: None,
+ audio_streams: Vec::new(),
container: file.format.name().to_string(),
start_timecode: None,
file_size: Some(bytes.0.len()),
@@ -316,7 +316,7 @@ impl MpegSource {
kind: MediaKind::Movie,
duration: Tick::ZERO,
video: None,
- audio: None,
+ audio_streams: Vec::new(),
container: "MPEG video elementary stream".into(),
start_timecode: None,
file_size: Some(bytes.0.len()),
@@ -485,7 +485,8 @@ impl MpegSource {
/// Mark the audio stream as not decodable (it still shows in the media info).
fn audio_unsupported(&mut self, codec_name: &str, why: String) -> Option {
self.unsupported_audio = Some(why);
- self.info.audio = Some(AudioStreamInfo { sample_rate: 48_000, channels: 2, codec: format!("{codec_name} (unsupported)"), bits_per_sample: None });
+ self.info.audio_streams =
+ vec![AudioStreamInfo { sample_rate: 48_000, channels: 2, codec: format!("{codec_name} (unsupported)"), bits_per_sample: None }];
None
}
@@ -599,7 +600,8 @@ impl MpegSource {
self.info.kind = MediaKind::AudioOnly;
}
}
- self.info.audio = Some(AudioStreamInfo { sample_rate: rate.max(1), channels: channels.max(1) as u32, codec: codec_name, bits_per_sample: bits });
+ self.info.audio_streams =
+ vec![AudioStreamInfo { sample_rate: rate.max(1), channels: channels.max(1) as u32, codec: codec_name, bits_per_sample: bits }];
Some(AudioTrack {
stream: ai,
codec,
diff --git a/crates/codecs/src/mxf.rs b/crates/codecs/src/mxf.rs
index 5980906bc..1b230a8dc 100644
--- a/crates/codecs/src/mxf.rs
+++ b/crates/codecs/src/mxf.rs
@@ -361,7 +361,7 @@ impl MxfSource {
kind: if video.is_some() { MediaKind::Movie } else { MediaKind::AudioOnly },
duration,
video,
- audio,
+ audio_streams: audio.into_iter().collect(),
container: format!("MXF {}", file.operational_pattern.name()),
start_timecode: None,
file_size: Some(bytes.0.len()),
@@ -457,7 +457,7 @@ impl MediaSource for MxfSource {
}
fn audio(&self, start: i64, frames: usize, sample_rate: u32) -> Result {
- let ainfo = self.info.audio.as_ref().ok_or(MediaError::NoStream("audio"))?;
+ let ainfo = self.info.audio().ok_or(MediaError::NoStream("audio"))?;
let src_rate = ainfo.sample_rate;
let ch = ainfo.channels.max(1) as usize;
let mut out = AudioBuffer::silence(sample_rate, ch, frames);
diff --git a/crates/codecs/src/ogg.rs b/crates/codecs/src/ogg.rs
index 18092667d..1e729837a 100644
--- a/crates/codecs/src/ogg.rs
+++ b/crates/codecs/src/ogg.rs
@@ -110,7 +110,7 @@ impl OggSource {
kind: MediaKind::AudioOnly,
duration: Tick::from_units(frames, rate as i64),
video: None,
- audio: Some(AudioStreamInfo { sample_rate: rate, channels: channels.max(1), codec: codec.into(), bits_per_sample: None }),
+ audio_streams: vec![AudioStreamInfo { sample_rate: rate, channels: channels.max(1), codec: codec.into(), bits_per_sample: None }],
container: "Ogg".into(),
start_timecode: None,
file_size: Some(bytes.0.len()),
diff --git a/crates/codecs/src/stream_color.rs b/crates/codecs/src/stream_color.rs
new file mode 100644
index 000000000..d01d6a640
--- /dev/null
+++ b/crates/codecs/src/stream_color.rs
@@ -0,0 +1,92 @@
+//! The colour description a video stream carries in its own bitstream, for containers that don't
+//! repeat it: the H.264 / HEVC sequence parameter set's VUI (from `avcC` / `hvcC`) and the AV1
+//! sequence header's `color_config` (from `av1C`), read with the decoders' own parsers.
+//!
+//! ffmpeg writes MP4 without a `colr` box unless asked (`-movflags +write_colr`), so for most
+//! files the bitstream is the only place their colour is described. [`resolve`] merges it with
+//! what the container signals, which wins.
+
+use filmcraft_color::{ColorInfo, Matrix, Range, Transfer};
+use filmcraft_isobmff::{AvcConfig, CodecConfig, HevcConfig};
+
+use crate::video::primaries_from_code;
+
+/// ITU-T H.273 `Unspecified`.
+const UNSPECIFIED: u8 = 2;
+
+/// Colour code points (ITU-T H.273 / ISO/IEC 23091-2) and range as one place in a file (a
+/// container colour box, a sequence header) signals them.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct ColorCodes {
+ pub primaries: u8,
+ pub transfer: u8,
+ pub matrix: u8,
+ /// `None`: this place doesn't say.
+ pub full_range: Option,
+}
+
+impl ColorCodes {
+ /// From wider code points (a `colr` box or a Matroska `Colour` element): a value that doesn't
+ /// fit a byte is no H.273 code point, so it counts as unspecified.
+ pub fn from_wide(primaries: u64, transfer: u64, matrix: u64, full_range: Option) -> Self {
+ let code = |v: u64| u8::try_from(v).unwrap_or(UNSPECIFIED);
+ Self { primaries: code(primaries), transfer: code(transfer), matrix: code(matrix), full_range }
+ }
+}
+
+/// The colour a video codec configuration's bitstream signals: the VUI of the first SPS in an
+/// `avcC` / `hvcC`, or the `color_config` of the sequence header in an `av1C`. `None` for other
+/// codecs, an SPS without VUI, and a configuration that is truncated or corrupt.
+pub fn from_codec_config(codec: &CodecConfig) -> Option {
+ match codec {
+ CodecConfig::Avc(c) => avc(c),
+ CodecConfig::Hevc(c) => hevc(c),
+ CodecConfig::Av1(c) => av1(&c.config_obus),
+ _ => None,
+ }
+}
+
+fn avc(c: &AvcConfig) -> Option {
+ // NAL header byte: nal_unit_type 7 is an SPS
+ let nal = c.sps.iter().find(|n| n.first().is_some_and(|h| h & 0x1f == 7))?;
+ let sps = filmcraft_h264::params::Sps::parse(&filmcraft_bitstream::unescape_rbsp(nal.get(1..)?)).ok()?;
+ let v = sps.vui?;
+ Some(ColorCodes { primaries: v.colour_primaries, transfer: v.transfer_characteristics, matrix: v.matrix_coefficients, full_range: Some(v.full_range) })
+}
+
+fn hevc(c: &HevcConfig) -> Option {
+ // two-byte NAL header: nal_unit_type 33 is an SPS (whatever array the record files it under)
+ let nal = c.arrays.iter().flat_map(|a| &a.nalus).find(|n| n.first().is_some_and(|h| (h >> 1) & 0x3f == 33))?;
+ let sps = filmcraft_hevc::params::Sps::parse(&filmcraft_bitstream::unescape_rbsp(nal.get(2..)?)).ok()?;
+ let v = sps.vui?;
+ Some(ColorCodes { primaries: v.colour_primaries, transfer: v.transfer_characteristics, matrix: v.matrix_coefficients, full_range: Some(v.full_range) })
+}
+
+fn av1(config_obus: &[u8]) -> Option {
+ // OBU type 1: the sequence header
+ let (_, payload) = crate::hw_frame::obus(config_obus).into_iter().find(|(t, _)| *t == 1)?;
+ let c = filmcraft_av1::SequenceHeader::parse(payload).ok()?.color;
+ Some(ColorCodes { primaries: c.color_primaries, transfer: c.transfer_characteristics, matrix: c.matrix_coefficients, full_range: Some(c.color_range) })
+}
+
+/// Colour from the code points a file signals, most authoritative first (the container's colour
+/// box, then the bitstream's): the matrix, transfer, primaries and range each come from the first
+/// source that signals a value we know. What no source signals keeps the defaults: the matrix by
+/// frame size ([`filmcraft_frame::default_matrix`]: BT.601 for SD, BT.709 otherwise), BT.709
+/// transfer and primaries, limited range.
+pub fn resolve(width: u32, height: u32, sources: &[ColorCodes]) -> ColorInfo {
+ let mut c = ColorInfo { matrix: filmcraft_frame::default_matrix(width, height), ..ColorInfo::REC709 };
+ if let Some(m) = sources.iter().find_map(|s| Matrix::from_code(s.matrix)) {
+ c.matrix = m;
+ }
+ if let Some(t) = sources.iter().find_map(|s| Transfer::from_code(s.transfer)) {
+ c.transfer = t;
+ }
+ if let Some(p) = sources.iter().find_map(|s| primaries_from_code(s.primaries)) {
+ c.primaries = p;
+ }
+ if sources.iter().find_map(|s| s.full_range) == Some(true) {
+ c.range = Range::Full;
+ }
+ c
+}
diff --git a/crates/codecs/src/stream_color_tests.rs b/crates/codecs/src/stream_color_tests.rs
new file mode 100644
index 000000000..1c6526fee
--- /dev/null
+++ b/crates/codecs/src/stream_color_tests.rs
@@ -0,0 +1,340 @@
+//! Colour from the bitstream when the container has none (`crate::stream_color`): ffmpeg writes MP4
+//! without a `colr` box by default, and an HEVC PQ file imported as Rec. 709 SDR, a full-range
+//! H.264 file as limited range, and a BT.601 SD file with BT.709 primaries. The parameter sets
+//! are built here bit by bit; no media files.
+
+use std::io::Cursor;
+use std::sync::Arc;
+
+use filmcraft_bitstream::{BitWriter, escape_rbsp};
+use filmcraft_color::{ColorInfo, ColorSpace, Matrix, Primaries, Range, Transfer};
+use filmcraft_isobmff::{Av1Config, AvcConfig, Brand, CodecConfig, HevcConfig, HevcNalArray, Mp4Writer, SampleEntry, TrackConfig, WriteSample, WriterOptions};
+use filmcraft_matroska::{MkvWriter, MuxOptions, TrackKind, TrackSpec};
+use filmcraft_media::MediaSource;
+
+use crate::stream_color::{ColorCodes, from_codec_config, resolve};
+
+/// The video signal description of a VUI: (colour_primaries, transfer_characteristics,
+/// matrix_coefficients) when `colour_description_present_flag` is set, and the range flag.
+#[derive(Clone, Copy)]
+struct Signal {
+ codes: Option<(u8, u8, u8)>,
+ full_range: bool,
+}
+
+const PQ_2020: Signal = Signal { codes: Some((9, 16, 9)), full_range: false };
+const SMPTE_170M: Signal = Signal { codes: Some((6, 6, 6)), full_range: false };
+const FULL_ONLY: Signal = Signal { codes: None, full_range: true };
+
+fn signal(w: &mut BitWriter, s: Signal) {
+ w.write_bit(true); // video_signal_type_present_flag
+ w.write_bits(5, 3); // video_format: unspecified
+ w.write_bit(s.full_range);
+ w.write_bit(s.codes.is_some()); // colour_description_present_flag
+ if let Some((p, t, m)) = s.codes {
+ w.write_bits(p.into(), 8);
+ w.write_bits(t.into(), 8);
+ w.write_bits(m.into(), 8);
+ }
+}
+
+/// A Baseline H.264 SPS NAL unit (ITU-T H.264 §7.3.2.1.1, VUI per Annex E.1.1) of
+/// `mbs_w`×`mbs_h` macroblocks, with a VUI carrying `vui` (no VUI when `None`).
+fn h264_sps(mbs_w: u32, mbs_h: u32, vui: Option) -> Vec {
+ let mut w = BitWriter::new();
+ w.write_bits(66, 8); // profile_idc: Baseline
+ w.write_bits(0, 8); // constraint flags
+ w.write_bits(30, 8); // level_idc
+ w.write_ue(0); // seq_parameter_set_id
+ w.write_ue(0); // log2_max_frame_num_minus4
+ w.write_ue(2); // pic_order_cnt_type
+ w.write_ue(1); // max_num_ref_frames
+ w.write_bit(false); // gaps_in_frame_num_value_allowed_flag
+ w.write_ue(mbs_w - 1);
+ w.write_ue(mbs_h - 1);
+ w.write_bits(0b110, 3); // frame_mbs_only, direct_8x8_inference, no cropping
+ w.write_bit(vui.is_some()); // vui_parameters_present_flag
+ if let Some(s) = vui {
+ w.write_bit(false); // aspect_ratio_info_present_flag
+ w.write_bit(false); // overscan_info_present_flag
+ signal(&mut w, s);
+ w.write_bit(false); // chroma_loc_info_present_flag
+ w.write_bit(false); // timing_info_present_flag
+ w.write_bit(false); // nal_hrd_parameters_present_flag
+ w.write_bit(false); // vcl_hrd_parameters_present_flag
+ w.write_bit(false); // pic_struct_present_flag
+ w.write_bit(false); // bitstream_restriction_flag
+ }
+ w.rbsp_trailing();
+ let mut nal = vec![0x67];
+ nal.extend(escape_rbsp(&w.finish()));
+ nal
+}
+
+/// A Main 10 HEVC SPS NAL unit (ITU-T H.265 §7.3.2.2, VUI per Annex E.2.1) of `width`×`height`
+/// (multiples of 8), with a VUI carrying `vui` (no VUI when `None`).
+fn hevc_sps(width: u32, height: u32, vui: Option) -> Vec {
+ let mut w = BitWriter::new();
+ w.write_bits(0, 4); // sps_video_parameter_set_id
+ w.write_bits(0, 3); // sps_max_sub_layers_minus1
+ w.write_bit(true); // sps_temporal_id_nesting_flag
+ // profile_tier_level: Main 10, level 4.1
+ w.write_bits(0, 2);
+ w.write_bit(false);
+ w.write_bits(2, 5);
+ w.write_bits(0x2000_0000, 32);
+ w.write_bits(0b1001, 4);
+ w.write_bits(0, 32);
+ w.write_bits(0, 12);
+ w.write_bits(123, 8);
+ w.write_ue(0); // sps_seq_parameter_set_id
+ w.write_ue(1); // chroma_format_idc 4:2:0
+ w.write_ue(width);
+ w.write_ue(height);
+ w.write_bit(false); // conformance_window_flag
+ w.write_ue(2); // bit_depth_luma_minus8
+ w.write_ue(2); // bit_depth_chroma_minus8
+ w.write_ue(4); // log2_max_pic_order_cnt_lsb_minus4
+ w.write_bit(true); // sps_sub_layer_ordering_info_present_flag
+ w.write_ue(4); // sps_max_dec_pic_buffering_minus1
+ w.write_ue(0); // sps_max_num_reorder_pics
+ w.write_ue(0); // sps_max_latency_increase_plus1
+ w.write_ue(0); // log2_min_luma_coding_block_size_minus3 (8)
+ w.write_ue(1); // log2_diff_max_min_luma_coding_block_size (CTB 16)
+ w.write_ue(0); // log2_min_luma_transform_block_size_minus2 (4)
+ w.write_ue(2); // log2_diff_max_min_luma_transform_block_size (16)
+ w.write_ue(0); // max_transform_hierarchy_depth_inter
+ w.write_ue(0); // max_transform_hierarchy_depth_intra
+ w.write_bit(false); // scaling_list_enabled_flag
+ w.write_bit(false); // amp_enabled_flag
+ w.write_bit(false); // sample_adaptive_offset_enabled_flag
+ w.write_bit(false); // pcm_enabled_flag
+ w.write_ue(0); // num_short_term_ref_pic_sets
+ w.write_bit(false); // long_term_ref_pics_present_flag
+ w.write_bit(false); // sps_temporal_mvp_enabled_flag
+ w.write_bit(false); // strong_intra_smoothing_enabled_flag
+ w.write_bit(vui.is_some()); // vui_parameters_present_flag
+ if let Some(s) = vui {
+ w.write_bit(false); // aspect_ratio_info_present_flag
+ w.write_bit(false); // overscan_info_present_flag
+ signal(&mut w, s);
+ w.write_bit(false); // chroma_loc_info_present_flag
+ w.write_bit(false); // neutral_chroma_indication_flag
+ w.write_bit(false); // field_seq_flag
+ w.write_bit(false); // frame_field_info_present_flag
+ w.write_bit(false); // default_display_window_flag
+ w.write_bit(false); // vui_timing_info_present_flag
+ w.write_bit(false); // bitstream_restriction_flag
+ }
+ w.write_bit(false); // sps_extension_present_flag
+ w.rbsp_trailing();
+ let mut nal = vec![33 << 1, 1];
+ nal.extend(escape_rbsp(&w.finish()));
+ nal
+}
+
+/// An AV1 sequence header OBU (AV1 bitstream spec §5.5; reduced still-picture header, profile 0,
+/// 10-bit 4:2:0) of `width`×`height` with `color_config` signalling `codes` and `full_range`.
+fn av1_sequence_header(width: u32, height: u32, codes: Option<(u8, u8, u8)>, full_range: bool) -> Vec {
+ let mut w = BitWriter::new();
+ w.write_bits(0, 3); // seq_profile
+ w.write_bit(true); // still_picture
+ w.write_bit(true); // reduced_still_picture_header
+ w.write_bits(0, 5); // seq_level_idx[0]
+ w.write_bits(15, 4); // frame_width_bits_minus_1
+ w.write_bits(15, 4); // frame_height_bits_minus_1
+ w.write_bits(width - 1, 16);
+ w.write_bits(height - 1, 16);
+ w.write_bits(0, 3); // use_128x128_superblock, enable_filter_intra, enable_intra_edge_filter
+ w.write_bits(0, 3); // enable_superres, enable_cdef, enable_restoration
+ w.write_bit(true); // high_bitdepth
+ w.write_bit(false); // mono_chrome
+ w.write_bit(codes.is_some()); // color_description_present_flag
+ if let Some((p, t, m)) = codes {
+ w.write_bits(p.into(), 8);
+ w.write_bits(t.into(), 8);
+ w.write_bits(m.into(), 8);
+ }
+ w.write_bit(full_range); // color_range
+ w.write_bits(0, 2); // chroma_sample_position
+ w.write_bit(false); // separate_uv_delta_q
+ w.write_bit(false); // film_grain_params_present
+ w.rbsp_trailing();
+ let payload = w.finish();
+ // OBU header: type 1 (OBU_SEQUENCE_HEADER), obu_has_size_field; one-byte leb128 size
+ let mut obu = vec![(1 << 3) | 2, u8::try_from(payload.len()).expect("short payload")];
+ obu.extend(payload);
+ obu
+}
+
+fn avc(sps: Vec) -> CodecConfig {
+ CodecConfig::Avc(AvcConfig::new(vec![sps], vec![vec![0x68, 0xCE, 0x38, 0x80]], 4))
+}
+
+fn hevc(sps: Vec) -> CodecConfig {
+ CodecConfig::Hevc(HevcConfig {
+ general_profile_idc: 2,
+ chroma_format_idc: 1,
+ bit_depth_luma: 10,
+ bit_depth_chroma: 10,
+ length_size: 4,
+ arrays: vec![HevcNalArray { completeness: true, nal_type: 33, nalus: vec![sps] }],
+ ..Default::default()
+ })
+}
+
+fn av1(obus: Vec) -> CodecConfig {
+ CodecConfig::Av1(Av1Config { high_bitdepth: true, chroma_subsampling_x: true, chroma_subsampling_y: true, config_obus: obus, ..Default::default() })
+}
+
+/// A one-sample MP4 with `codec` (the sample is never decoded) and, when given, a `colr` box.
+fn mp4(codec: CodecConfig, w: u16, h: u16, colr: Option) -> Arc<[u8]> {
+ let mut entry = match codec {
+ CodecConfig::Avc(c) => SampleEntry::avc(c, w, h),
+ CodecConfig::Hevc(c) => SampleEntry::hevc(c, w, h),
+ other => SampleEntry::video(filmcraft_isobmff::FourCc(*b"av01"), other, w, h),
+ };
+ if let Some(v) = entry.video.as_mut() {
+ v.color = colr;
+ }
+ let mut mux = Mp4Writer::new(Cursor::new(Vec::new()), WriterOptions::new(Brand::Mp4)).expect("writer");
+ let t = mux.add_track(TrackConfig::new(entry, 30)).expect("track");
+ mux.write_sample(t, WriteSample { data: &[0, 0, 0, 1, 0x65], duration: 1, composition_offset: 0, is_sync: true }).expect("sample");
+ mux.finish().expect("finish").into_inner().into()
+}
+
+fn mp4_color(codec: CodecConfig, w: u16, h: u16, colr: Option) -> ColorInfo {
+ let src = crate::Mp4Source::open("clip.mp4", mp4(codec, w, h, colr)).expect("open");
+ src.info().video.as_ref().expect("video").color
+}
+
+#[test]
+fn hevc_pq_without_colr_is_rec2100_pq() {
+ let c = mp4_color(hevc(hevc_sps(1920, 1080, Some(PQ_2020))), 1920, 1080, None);
+ assert_eq!(c, ColorInfo { matrix: Matrix::Bt2020Ncl, transfer: Transfer::Pq, primaries: Primaries::Bt2020, range: Range::Limited });
+ assert_eq!(ColorSpace::from_info(&c), ColorSpace::Rec2100Pq);
+ assert!(ColorSpace::from_info(&c).is_hdr());
+}
+
+#[test]
+fn full_range_h264_without_colr_is_full_range() {
+ // video_full_range_flag with no colour description: full range, the rest by size
+ let c = mp4_color(avc(h264_sps(80, 45, Some(FULL_ONLY))), 1280, 720, None);
+ assert_eq!(c, ColorInfo { range: Range::Full, ..ColorInfo::REC709 });
+}
+
+#[test]
+fn bt601_sd_h264_without_colr_has_bt601_primaries() {
+ let c = mp4_color(avc(h264_sps(45, 30, Some(SMPTE_170M))), 720, 480, None);
+ assert_eq!(c, ColorInfo { matrix: Matrix::Bt601, transfer: Transfer::Bt709, primaries: Primaries::Bt601_525, range: Range::Limited });
+ // PAL (BT.470 BG primaries, BT.601 matrix)
+ let pal = Signal { codes: Some((5, 6, 5)), full_range: false };
+ let c = mp4_color(avc(h264_sps(45, 36, Some(pal))), 720, 576, None);
+ assert_eq!((c.primaries, c.matrix), (Primaries::Bt601_625, Matrix::Bt601));
+}
+
+#[test]
+fn unspecified_or_missing_vui_keeps_the_size_defaults() {
+ let unspecified = Signal { codes: Some((2, 2, 2)), full_range: false };
+ for vui in [None, Some(unspecified)] {
+ assert_eq!(mp4_color(avc(h264_sps(80, 45, vui)), 1280, 720, None), ColorInfo::REC709);
+ assert_eq!(mp4_color(hevc(hevc_sps(1920, 1080, vui)), 1920, 1080, None), ColorInfo::REC709);
+ // SD: the BT.601 matrix, as before
+ assert_eq!(mp4_color(avc(h264_sps(45, 30, vui)), 720, 480, None), ColorInfo { matrix: Matrix::Bt601, ..ColorInfo::REC709 });
+ }
+}
+
+#[test]
+fn colr_wins_over_the_bitstream() {
+ use filmcraft_isobmff::ColorInfo::{Nclc, Nclx};
+ let sps = || hevc(hevc_sps(1920, 1080, Some(PQ_2020)));
+ // a BT.709 nclx box beats a PQ VUI, and is what the decoder's frames are tagged with
+ let colr = Nclx { primaries: 1, transfer: 1, matrix: 1, full_range: false };
+ assert_eq!(mp4_color(sps(), 1920, 1080, Some(colr.clone())), ColorInfo::REC709);
+ // HLG in colr, PQ in the VUI: HLG
+ let hlg = Nclx { primaries: 9, transfer: 18, matrix: 9, full_range: false };
+ assert_eq!(mp4_color(sps(), 1920, 1080, Some(hlg)).transfer, Transfer::Hlg);
+ // limited range in colr beats a full-range VUI
+ assert_eq!(mp4_color(avc(h264_sps(80, 45, Some(FULL_ONLY))), 1280, 720, Some(colr)).range, Range::Limited);
+ // what colr leaves unspecified comes from the VUI
+ let partial = Nclx { primaries: 2, transfer: 16, matrix: 2, full_range: false };
+ assert_eq!(mp4_color(sps(), 1920, 1080, Some(partial)), mp4_color(sps(), 1920, 1080, None));
+ // QuickTime nclc has no range flag: the VUI's range stands
+ let nclc = Nclc { primaries: 1, transfer: 1, matrix: 1 };
+ assert_eq!(mp4_color(avc(h264_sps(80, 45, Some(FULL_ONLY))), 1280, 720, Some(nclc)).range, Range::Full);
+ // an ICC profile has no code points: the bitstream decides
+ let icc = filmcraft_isobmff::ColorInfo::Icc { kind: filmcraft_isobmff::FourCc(*b"prof"), profile: vec![0; 8] };
+ assert_eq!(mp4_color(sps(), 1920, 1080, Some(icc)).transfer, Transfer::Pq);
+}
+
+#[test]
+fn av1_sequence_header_colour() {
+ let obu = av1_sequence_header(1920, 1080, Some((9, 16, 9)), false);
+ assert_eq!(from_codec_config(&av1(obu.clone())), Some(ColorCodes { primaries: 9, transfer: 16, matrix: 9, full_range: Some(false) }));
+ let c = mp4_color(av1(obu), 1920, 1080, None);
+ assert_eq!(ColorSpace::from_info(&c), ColorSpace::Rec2100Pq);
+ // no colour description: unspecified code points, the range flag still counts
+ let obu = av1_sequence_header(640, 360, None, true);
+ assert_eq!(from_codec_config(&av1(obu)), Some(ColorCodes { primaries: 2, transfer: 2, matrix: 2, full_range: Some(true) }));
+}
+
+#[test]
+fn matroska_without_colour_uses_the_bitstream() {
+ let CodecConfig::Hevc(hvcc) = hevc(hevc_sps(1920, 1080, Some(PQ_2020))) else { panic!("an hvcC") };
+ let mut spec = TrackSpec::new(TrackKind::Video, "V_MPEGH/ISO/HEVC");
+ spec.codec_private = hvcc.to_bytes();
+ spec.video_size = Some((1920, 1080));
+ spec.default_duration_ns = Some(33_333_333);
+ let mut w = MkvWriter::new(Cursor::new(Vec::new()), vec![spec], MuxOptions::default()).expect("writer");
+ w.write_frame(0, 0, true, &[0, 0, 0, 1, 0x26], None).expect("frame");
+ let b: Arc<[u8]> = w.finish().expect("finish").into_inner().into();
+ let src = crate::MkvSource::open("clip.mkv", b).expect("open");
+ let c = src.info().video.as_ref().expect("video").color;
+ assert_eq!(ColorSpace::from_info(&c), ColorSpace::Rec2100Pq);
+ assert_eq!((c.primaries, c.matrix), (Primaries::Bt2020, Matrix::Bt2020Ncl));
+}
+
+#[test]
+fn resolve_takes_each_field_from_the_first_source_that_knows_it() {
+ let colr = ColorCodes { primaries: 1, transfer: 2, matrix: 0, full_range: None };
+ let vui = ColorCodes { primaries: 9, transfer: 18, matrix: 9, full_range: Some(true) };
+ // matrix 0 (identity) is not one we know: the VUI's BT.2020 is used
+ let c = resolve(1920, 1080, &[colr, vui]);
+ assert_eq!(c, ColorInfo { matrix: Matrix::Bt2020Ncl, transfer: Transfer::Hlg, primaries: Primaries::Bt709, range: Range::Full });
+ assert_eq!(resolve(720, 480, &[]), ColorInfo { matrix: Matrix::Bt601, ..ColorInfo::REC709 });
+ // code points that don't fit a byte are unspecified, not truncated (0x109 is not 9)
+ assert_eq!(ColorCodes::from_wide(0x109, 0x110, 0x109, None), ColorCodes { primaries: 2, transfer: 2, matrix: 2, full_range: None });
+}
+
+/// Every truncation and many bit flips of each configuration: never a panic, and the result is
+/// either some colour or none (the size defaults).
+#[test]
+fn hostile_parameter_sets_never_panic() {
+ let configs: Vec<(Vec, fn(Vec) -> CodecConfig)> = vec![
+ (h264_sps(45, 30, Some(SMPTE_170M)), avc),
+ (hevc_sps(1920, 1080, Some(PQ_2020)), hevc),
+ (av1_sequence_header(1920, 1080, Some((9, 16, 9)), false), av1),
+ ];
+ for (good, make) in configs {
+ assert!(from_codec_config(&make(good.clone())).is_some());
+ let mut variants: Vec> = (0..good.len()).map(|n| good[..n].to_vec()).collect();
+ for bit in 0..good.len() * 8 {
+ let mut b = good.clone();
+ b[bit / 8] ^= 0x80 >> (bit % 8);
+ variants.push(b);
+ }
+ variants.push(vec![0xff; 64]);
+ variants.push(Vec::new());
+ for v in variants {
+ let r = std::panic::catch_unwind(|| from_codec_config(&make(v.clone())));
+ assert!(r.is_ok(), "panicked on {v:02x?}");
+ }
+ }
+ // configurations with no parameter sets at all
+ assert_eq!(from_codec_config(&CodecConfig::Avc(AvcConfig::default())), None);
+ assert_eq!(from_codec_config(&CodecConfig::Hevc(HevcConfig::default())), None);
+ assert_eq!(from_codec_config(&CodecConfig::Av1(Av1Config::default())), None);
+ // a garbage SPS imports with the size defaults
+ assert_eq!(mp4_color(avc(vec![0x67, 0xff, 0xff]), 1280, 720, None), ColorInfo::REC709);
+ assert_eq!(mp4_color(hevc(vec![33 << 1, 1, 0xff]), 1920, 1080, None), ColorInfo::REC709);
+}
diff --git a/crates/codecs/src/tests.rs b/crates/codecs/src/tests.rs
index 819a41f90..3f1f5047d 100644
--- a/crates/codecs/src/tests.rs
+++ b/crates/codecs/src/tests.rs
@@ -70,7 +70,7 @@ fn mjpeg_mov_with_pcm() {
fn aac_mp4_decodes() {
let Some(b) = fixture("tone_aac.mp4", &["-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:d=3", "-c:a", "aac", "-b:a", "128k"]) else { return };
let src = crate::open_bytes("tone_aac.mp4", b).unwrap();
- assert!(src.info().audio.as_ref().unwrap().codec.contains("AAC"));
+ assert!(src.info().audio().unwrap().codec.contains("AAC"));
// read in the middle (random access) and sequentially
let a = src.audio(48_000, 4800, 48_000).unwrap();
let p = a.peaks()[0];
@@ -510,7 +510,7 @@ fn check_opus(name: &str, channels: usize, expr: &str, extra: &[&str], container
let src = crate::open_bytes(name, b).expect("open");
let info = src.info().clone();
assert_eq!(info.container, container);
- let a = info.audio.as_ref().expect("audio");
+ let a = info.audio().expect("audio");
assert_eq!((a.codec.as_str(), a.sample_rate, a.channels as usize), ("Opus", 48_000, channels));
let d = info.duration.seconds();
assert!((d - 3.0).abs() < 0.03, "{name}: duration {d}");
@@ -615,3 +615,32 @@ fn apv_mp4_and_raw_bitstream_decode() {
let decoded_mp4 = mp4_src.video_frame(FrameRequest::full(Tick::ZERO)).expect("decode mp4 frame");
assert_eq!(decoded_mp4.to_rgba8(), decoded_raw.to_rgba8());
}
+
+/// ffmpeg writes MP4 without a `colr` box unless asked: the colour comes from the x265 / x264
+/// SPS VUI, and matches the same stream remuxed with `colr`.
+#[test]
+fn mp4_colour_comes_from_the_sps_without_colr() {
+ use filmcraft_color::{ColorSpace, Primaries, Range};
+ let src = |name: &str, args: &[&str]| fixture(name, args).map(|b| crate::open_bytes(name, b).expect("open"));
+ let color = |s: &filmcraft_media::SharedSource| s.info().video.as_ref().expect("video").color;
+ let lavfi = |size: &'static str| ["-f", "lavfi", "-i", size, "-t", "0.2"];
+ let x265 = "colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:log-level=error";
+ let mut pq = lavfi("testsrc2=s=640x360:r=25").to_vec();
+ pq.extend(["-c:v", "libx265", "-tag:v", "hvc1", "-pix_fmt", "yuv420p10le", "-x265-params", x265]);
+ let Some(plain) = src("hevc_pq_no_colr.mp4", &pq) else { return };
+ assert_eq!(ColorSpace::from_info(&color(&plain)), ColorSpace::Rec2100Pq);
+ let colr_path = fixture_path("hevc_pq_no_colr.mp4", &pq).expect("fixture");
+ if let Some(with_colr) = src("hevc_pq_colr.mp4", &["-i", colr_path.to_str().expect("path"), "-c", "copy", "-movflags", "+write_colr"]) {
+ assert_eq!(color(&with_colr), color(&plain));
+ }
+ let mut full = lavfi("testsrc2=s=640x360:r=25").to_vec();
+ full.extend(["-c:v", "libx264", "-pix_fmt", "yuvj420p", "-color_range", "pc"]);
+ if let Some(s) = src("h264_full_no_colr.mp4", &full) {
+ assert_eq!(color(&s).range, Range::Full);
+ }
+ let mut sd = lavfi("testsrc2=s=720x480:r=30000/1001").to_vec();
+ sd.extend(["-c:v", "libx264", "-pix_fmt", "yuv420p", "-x264-params", "colorprim=smpte170m:transfer=smpte170m:colormatrix=smpte170m"]);
+ if let Some(s) = src("h264_sd601_no_colr.mp4", &sd) {
+ assert_eq!(color(&s).primaries, Primaries::Bt601_525);
+ }
+}
diff --git a/crates/codecs/src/video.rs b/crates/codecs/src/video.rs
index 8aada02e6..86b21b7d9 100644
--- a/crates/codecs/src/video.rs
+++ b/crates/codecs/src/video.rs
@@ -45,8 +45,19 @@ pub trait VideoDecoder: Send {
/// shortcuts that only change pictures nothing references, which come out flagged
/// [`DecodedFrame::draft`]. Decoders without such a mode ignore it.
fn set_draft(&mut self, _on: bool) {}
+ /// Made with fewer worker threads than a decoder for playback gets, because it was made for
+ /// background work ([`filmcraft_media::cancel::with_background`]). A request for a monitor
+ /// replaces such a decoder.
+ fn thread_limited(&self) -> bool {
+ false
+ }
}
+/// Worker threads of the H.264 decoders made for background work (thumbnails): a frame-threaded
+/// 4K decoder holds `threads + 2` pictures in flight (~0.5 GB with 16 threads), and the frame
+/// workers already decode different files in parallel.
+const BACKGROUND_H264_THREADS: usize = 2;
+
/// NAL unit headers of a length-prefixed (avcC / hvcC) sample: the first two bytes of each unit.
fn nal_headers(sample: &[u8], length_size: usize) -> impl Iterator
- + '_ {
let mut pos = 0usize;
@@ -200,19 +211,36 @@ pub struct H264Decoder {
dec: filmcraft_h264::Decoder,
length_size: usize,
draft: bool,
+ /// Worker threads (a background decoder gets fewer).
+ threads: usize,
}
impl H264Decoder {
+ /// The worker threads for a decoder made now.
+ fn threads_now() -> usize {
+ if filmcraft_media::cancel::background() { BACKGROUND_H264_THREADS.min(h264_threads()) } else { h264_threads() }
+ }
+
+ fn make_inner(avcc: &[u8], threads: usize) -> Result {
+ let mut dec = filmcraft_h264::Decoder::with_threads(threads);
+ if !avcc.is_empty() {
+ dec.configure_avcc(avcc).map_err(|e| CodecError::Decode(e.to_string()))?;
+ }
+ Ok(dec)
+ }
+
pub fn new(avcc: Vec) -> Result {
recycle_h264_planes();
- let dec = filmcraft_h264::Decoder::from_avcc(&avcc).map_err(|e| CodecError::Decode(e.to_string()))?;
+ let threads = Self::threads_now();
+ let dec = Self::make_inner(&avcc, threads)?;
let length_size = avcc_length_size(&avcc);
- Ok(Self { avcc, dec, length_size, draft: false })
+ Ok(Self { avcc, dec, length_size, draft: false, threads })
}
/// A decoder for Annex B byte-stream samples (start codes, in-band parameter sets: MXF, TS).
pub fn annexb() -> Self {
recycle_h264_planes();
- Self { avcc: Vec::new(), dec: filmcraft_h264::Decoder::new(), length_size: 0, draft: false }
+ let threads = Self::threads_now();
+ Self { avcc: Vec::new(), dec: filmcraft_h264::Decoder::with_threads(threads), length_size: 0, draft: false, threads }
}
fn convert(p: filmcraft_h264::Picture) -> DecodedFrame {
use std::sync::Arc;
@@ -245,9 +273,7 @@ impl VideoDecoder for H264Decoder {
self.dec.flush().into_iter().map(Self::convert).collect()
}
fn reset(&mut self) {
- if self.avcc.is_empty() {
- self.dec = filmcraft_h264::Decoder::new();
- } else if let Ok(d) = filmcraft_h264::Decoder::from_avcc(&self.avcc) {
+ if let Ok(d) = Self::make_inner(&self.avcc, self.threads) {
self.dec = d;
}
self.dec.set_draft(self.draft);
@@ -255,6 +281,9 @@ impl VideoDecoder for H264Decoder {
fn name(&self) -> &str {
"FilmCraft H.264"
}
+ fn thread_limited(&self) -> bool {
+ self.threads < h264_threads()
+ }
fn set_draft(&mut self, on: bool) {
self.draft = on;
self.dec.set_draft(on);
diff --git a/crates/codecs/tests/matroska_audio_oracle.rs b/crates/codecs/tests/matroska_audio_oracle.rs
new file mode 100644
index 000000000..1298ccd48
--- /dev/null
+++ b/crates/codecs/tests/matroska_audio_oracle.rs
@@ -0,0 +1,50 @@
+//! Matroska audio made by ffmpeg, against ffmpeg's decode (#236). ffmpeg stamps blocks to the
+//! millisecond, so at 44.1 kHz AAC (1024-sample), MP3 (1152) and AC-3 (1536) packets are each
+//! stamped up to half a millisecond off their exact start. Read back block by block, as playback
+//! reads it, a tone must stay as smooth as ffmpeg's decode of it: no holes or overlaps between
+//! packets.
+
+mod common;
+
+use common::*;
+
+/// Largest |second difference| of `x` from `from` on: a click shows up as a spike.
+fn roughness(x: &[f32], from: usize) -> f32 {
+ x.windows(3).skip(from).map(|w| (w[0] - 2.0 * w[1] + w[2]).abs()).fold(0.0, f32::max)
+}
+
+#[test]
+fn matroska_audio_plays_without_holes() {
+ let ff = filmcraft_testkit::require_ffmpeg!();
+ let cases: [(&str, u32, &[&str]); 5] = [
+ ("tone_aac_48k.mkv", 48_000, &["-c:a", "aac", "-b:a", "160k"]),
+ ("tone_aac_44k.mkv", 44_100, &["-c:a", "aac", "-b:a", "160k"]),
+ ("tone_mp3_44k.mkv", 44_100, &["-c:a", "libmp3lame", "-b:a", "192k"]),
+ ("tone_mp2_44k.mkv", 44_100, &["-c:a", "mp2", "-b:a", "192k"]),
+ ("tone_ac3_44k.mkv", 44_100, &["-c:a", "ac3", "-b:a", "192k"]),
+ ];
+ for (name, rate, codec) in cases {
+ let input = format!("sine=frequency=440:sample_rate={rate}:duration=3");
+ let args: Vec<&str> = ["-f", "lavfi", "-i", input.as_str(), "-af", "volume=4"].iter().copied().chain(codec.iter().copied()).collect();
+ let Some(path) = fixture(&ff, name, &args) else {
+ eprintln!("SKIPPED ({name}): this ffmpeg can't encode it");
+ continue;
+ };
+ let src = filmcraft_codecs::open_bytes(name, bytes(&path)).unwrap();
+ assert_eq!(src.info().audio().map(|a| a.sample_rate), Some(rate), "{name}");
+ let frames = rate as usize * 29 / 10;
+ let mut ours = Vec::with_capacity(frames);
+ while ours.len() < frames {
+ let n = 512.min(frames - ours.len());
+ ours.extend_from_slice(&src.audio(ours.len() as i64, n, rate).unwrap().channels[0]);
+ }
+ let theirs = ffmpeg_audio_f32(&ff, &path, &[]);
+ // past the codecs' start-up (priming, MP3's first frame)
+ let skip = rate as usize / 10;
+ let (r_ours, r_theirs) = (roughness(&ours, skip), roughness(&theirs[..theirs.len().min(frames)], skip));
+ let peak = ours.iter().skip(skip).fold(0f32, |m, x| m.max(x.abs()));
+ println!("{name}: peak {peak:.3}, max |second difference| ours {r_ours:.5}, ffmpeg {r_theirs:.5}");
+ assert!(peak > 0.4, "{name}: the tone is missing (peak {peak})");
+ assert!(r_ours < 2.0 * r_theirs + 0.005, "{name}: clicks between packets (|Δ²| {r_ours} vs ffmpeg's {r_theirs})");
+ }
+}
diff --git a/crates/codecs/tests/mpeg_oracle.rs b/crates/codecs/tests/mpeg_oracle.rs
index 5fdbc0bfc..d6a7042a0 100644
--- a/crates/codecs/tests/mpeg_oracle.rs
+++ b/crates/codecs/tests/mpeg_oracle.rs
@@ -177,15 +177,64 @@ fn open(p: &Path) -> MpegSource {
MpegSource::open(p.file_name().unwrap().to_str().unwrap(), bytes(p)).unwrap()
}
-/// ffprobe's video frames in display order: pts (90 kHz).
+/// ffprobe's video frames in display order: PTS (90 kHz). A program-stream picture
+/// need not carry its own PTS; use the decoder's presentation timestamp for that picture.
fn ffprobe_frame_pts(file: &Path) -> Option> {
- let fp = filmcraft_testkit::ffprobe()?;
+ let fp = filmcraft_testkit::ffprobe_or_skip("MPEG video frame presentation timestamps")?;
let o = std::process::Command::new(fp)
- .args(["-v", "error", "-select_streams", "v:0", "-show_entries", "frame=pts", "-of", "csv=p=0"])
+ .args(["-v", "error", "-select_streams", "v:0", "-show_entries", "frame=pts,best_effort_timestamp", "-of", "json"])
.arg(file)
.output()
- .ok()?;
- Some(String::from_utf8_lossy(&o.stdout).lines().filter_map(|l| l.trim().trim_end_matches(',').parse().ok()).collect())
+ .unwrap_or_else(|error| panic!("{}: cannot run ffprobe: {error}", file.display()));
+ assert!(o.status.success(), "{}: ffprobe failed: {}", file.display(), String::from_utf8_lossy(&o.stderr));
+ Some(parse_ffprobe_frame_pts(&o.stdout).unwrap_or_else(|error| panic!("{}: {error}", file.display())))
+}
+
+fn parse_ffprobe_frame_pts(bytes: &[u8]) -> Result, String> {
+ let data: serde_json::Value = serde_json::from_slice(bytes).map_err(|error| format!("invalid ffprobe JSON: {error}"))?;
+ let frames = data.get("frames").and_then(serde_json::Value::as_array).ok_or("ffprobe output is missing its frames array")?;
+ if frames.is_empty() {
+ return Err("ffprobe returned no video frames".into());
+ }
+ frames
+ .iter()
+ .enumerate()
+ .map(|(index, frame)| {
+ let field = if frame.get("pts").is_some() { "pts" } else { "best_effort_timestamp" };
+ frame.get(field).and_then(serde_json::Value::as_i64).ok_or_else(|| format!("ffprobe video frame {index} is missing an integer {field}"))
+ })
+ .collect()
+}
+
+#[test]
+fn ffprobe_pts_preserve_missing_raw_timestamps_and_side_data() {
+ let output = br#"{"frames":[
+ {"pts":131400,"best_effort_timestamp":131401,"side_data_list":[{},{}]},
+ {"best_effort_timestamp":135000,"side_data_list":[{}]},
+ {"pts":138600,"best_effort_timestamp":138600}
+ ]}"#;
+ assert_eq!(parse_ffprobe_frame_pts(output).unwrap(), [131400, 135000, 138600], "keep every display frame, with raw PTS preferred when present");
+ assert_eq!(parse_ffprobe_frame_pts(br#"{"frames":[{"pts":-3600},{"pts":0},{"pts":9223372036854775807}]}"#).unwrap(), [-3600, 0, i64::MAX]);
+}
+
+#[test]
+fn malformed_ffprobe_pts_fail_instead_of_dropping_frames() {
+ for output in [
+ "not JSON",
+ "{}",
+ r#"{"frames":{}}"#,
+ r#"{"frames":[]}"#,
+ r#"{"frames":[{}]}"#,
+ r#"{"frames":[null]}"#,
+ r#"{"frames":[{"pts":"N/A","best_effort_timestamp":135000}]}"#,
+ r#"{"frames":[{"pts":null,"best_effort_timestamp":135000}]}"#,
+ r#"{"frames":[{"pts":1.5}]}"#,
+ r#"{"frames":[{"pts":9223372036854775808}]}"#,
+ r#"{"frames":[{"best_effort_timestamp":"135000"}]}"#,
+ r#"{"frames":[{"pts":131400},{"best_effort_timestamp":null},{"pts":138600}]}"#,
+ ] {
+ assert!(parse_ffprobe_frame_pts(output.as_bytes()).is_err(), "malformed oracle output must fail: {output}");
+ }
}
/// Decode every frame in order and `seeks` random frames; compare with ffmpeg within `tol`.
@@ -245,7 +294,7 @@ fn check_video(ff: &Path, file: &Path, tol: u16, seeks: usize) -> (u16, f64) {
/// Our decoded audio from the first audio frame against ffmpeg's decode. Returns the max error.
fn check_audio(ff: &Path, file: &Path, tol: f32) -> f32 {
let src = open(file);
- let a = src.info().audio.clone().unwrap();
+ let a = src.info().audio().cloned().unwrap();
let (start, len) = src.audio_extent().unwrap();
let want = ffmpeg_audio_f32(ff, file, &[]);
let ch = a.channels as usize;
@@ -314,7 +363,7 @@ fn transport_stream_mpeg2_and_mp2() {
let (worst, psnr) = check_video(&ff, &f, 4, 12);
assert!(psnr >= 58.0, "PSNR {psnr}");
println!("ts_mpeg2_mp2: video max diff {worst}, PSNR {psnr:.2} dB");
- let a = src.info().audio.clone().unwrap();
+ let a = src.info().audio().cloned().unwrap();
assert_eq!((a.codec.as_str(), a.sample_rate, a.channels), ("MPEG Audio", 48_000, 2));
let e = check_audio(&ff, &f, 2e-4);
println!("ts_mpeg2_mp2: MP2 audio max error {e:.2e}");
@@ -337,7 +386,7 @@ fn bdav_h264_with_lpcm_sample_exact() {
let f = make(&ff, "m2ts_h264_lpcm.m2ts");
let src = open(&f);
assert_eq!(src.info().container, "MPEG-2 TS (BDAV/AVCHD)");
- let a = src.info().audio.clone().unwrap();
+ let a = src.info().audio().cloned().unwrap();
assert_eq!((a.codec.as_str(), a.channels, a.bits_per_sample), ("LPCM (Blu-ray)", 2, Some(24)));
check_video(&ff, &f, 0, 10);
assert_eq!(check_audio(&ff, &f, 0.0), 0.0);
@@ -349,7 +398,7 @@ fn avchd_ac3_audio() {
let f = make(&ff, "m2ts_h264_ac3.mts");
let src = open(&f);
check_video(&ff, &f, 0, 6);
- let a = src.info().audio.clone().unwrap();
+ let a = src.info().audio().cloned().unwrap();
if filmcraft_codecs::audio::AC3_DECODER {
assert_eq!((a.codec.as_str(), a.channels), ("AC-3", 2));
// AC-3 zero-bit mantissas carry decoder-specific dither (A/52 §7.3.4)
@@ -370,7 +419,7 @@ fn transport_stream_hevc_and_latm() {
let f = make(&ff, "ts_hevc_latm.ts");
let src = open(&f);
assert_eq!(src.info().video.as_ref().unwrap().codec, "HEVC");
- assert_eq!(src.info().audio.as_ref().unwrap().codec, "AAC (LATM)");
+ assert_eq!(src.info().audio().unwrap().codec, "AAC (LATM)");
check_video(&ff, &f, 0, 8);
let e = check_audio(&ff, &f, 1e-4);
println!("ts_hevc_latm: AAC (LATM) max error {e:.2e}");
@@ -389,17 +438,17 @@ fn program_streams() {
// DVD LPCM: sample-exact
let f = make(&ff, "ps_mpeg2_lpcm.vob");
let src = open(&f);
- assert_eq!(src.info().audio.as_ref().unwrap().codec, "LPCM (DVD)");
- assert_eq!(src.info().audio.as_ref().unwrap().bits_per_sample, Some(24));
+ assert_eq!(src.info().audio().unwrap().codec, "LPCM (DVD)");
+ assert_eq!(src.info().audio().unwrap().bits_per_sample, Some(24));
assert_eq!(check_audio(&ff, &f, 0.0), 0.0);
check_video(&ff, &f, 4, 4);
let f = make(&ff, "ps_mpeg2_lpcm16.vob");
- assert_eq!(open(&f).info().audio.as_ref().unwrap().bits_per_sample, Some(16));
+ assert_eq!(open(&f).info().audio().unwrap().bits_per_sample, Some(16));
assert_eq!(check_audio(&ff, &f, 0.0), 0.0);
// AC-3 in private stream 1
let f = make(&ff, "ps_mpeg2_ac3.vob");
let src = open(&f);
- assert_eq!(src.info().audio.as_ref().unwrap().codec, "AC-3");
+ assert_eq!(src.info().audio().unwrap().codec, "AC-3");
let e = check_audio(&ff, &f, 5e-3);
println!("ps_mpeg2_ac3: AC-3 max error {e:.2e}");
// MPEG-1 system stream
@@ -457,7 +506,7 @@ fn standalone_mp2_file() {
let ff = filmcraft_testkit::require_ffmpeg!();
let f = make(&ff, "audio.mp2");
let src = filmcraft_codecs::open_bytes("audio.mp2", bytes(&f)).unwrap();
- let a = src.info().audio.clone().unwrap();
+ let a = src.info().audio().cloned().unwrap();
assert_eq!((a.sample_rate, a.channels), (48_000, 2));
let want = ffmpeg_audio_f32(&ff, &f, &[]);
let n = want.len() / 2;
@@ -483,7 +532,7 @@ fn truncated_and_corrupt_files_never_panic() {
let _ = s.video_frame(FrameRequest::full(v.frame_rate.tick_of(i)));
}
}
- if let Some(a) = &info.audio {
+ if let Some(a) = info.audio() {
let _ = s.audio(rng.below(48_000) as i64, 2048, a.sample_rate);
}
};
diff --git a/crates/codecs/tests/mxf_oracle.rs b/crates/codecs/tests/mxf_oracle.rs
index 874b6e9a7..5a965b814 100644
--- a/crates/codecs/tests/mxf_oracle.rs
+++ b/crates/codecs/tests/mxf_oracle.rs
@@ -179,7 +179,7 @@ fn check_video(ff: &Path, file: &Path, pix_fmt: &str, chroma: (usize, usize), bp
/// Our audio, as interleaved f32, equals ffmpeg's decode exactly.
fn check_audio_exact(ff: &Path, file: &Path, extra: &[&str]) {
let src = open(file);
- let a = src.info().audio.clone().unwrap();
+ let a = src.info().audio().cloned().unwrap();
let ch = a.channels as usize;
let want = ffmpeg_audio_f32(ff, file, extra);
let n = want.len() / ch;
@@ -236,7 +236,7 @@ fn h264_2997_drop_frame_timecode_and_24_bit_audio() {
assert!(tc.drop_frame);
assert_eq!(tc.format(), "01:00:00;00");
assert_eq!(info.start_timecode, Some(107_892));
- assert_eq!(info.audio.as_ref().unwrap().bits_per_sample, Some(24));
+ assert_eq!(info.audio().unwrap().bits_per_sample, Some(24));
check_video(&ff, &f, "yuv420p", (2, 2), 1, 0, 10);
check_audio_exact(&ff, &f, &[]);
}
@@ -299,14 +299,14 @@ fn op_atom_video_and_audio_files() {
let v = make(&ff, "mxf_atom_dnxhr.mxf");
let src = open(&v);
assert_eq!(src.info().container, "MXF OP-Atom");
- assert!(src.info().audio.is_none());
+ assert!(!src.info().has_audio());
assert_eq!(src.file().tracks[0].wrapping, filmcraft_mxf::Wrapping::Clip);
check_video(&ff, &v, "yuv422p", (2, 1), 1, 2, 10);
let a = make(&ff, "mxf_atom_pcm.mxf");
let src = open(&a);
assert_eq!(src.info().kind, MediaKind::AudioOnly);
assert_eq!(src.info().container, "MXF OP-Atom");
- let ai = src.info().audio.clone().unwrap();
+ let ai = src.info().audio().cloned().unwrap();
assert_eq!((ai.sample_rate, ai.channels, ai.bits_per_sample), (48_000, 1, Some(24)));
assert_eq!(src.info().duration, filmcraft_time::Tick::from_units(48_000, 48_000));
check_audio_exact(&ff, &a, &[]);
@@ -317,7 +317,7 @@ fn d10_aes3_elements_sample_exact() {
let ff = filmcraft_testkit::require_ffmpeg!();
let f = make(&ff, "mxf_d10.mxf");
let src = open(&f);
- let a = src.info().audio.clone().unwrap();
+ let a = src.info().audio().cloned().unwrap();
assert_eq!(a.codec, "AES3 PCM");
assert!(a.channels >= 4, "{}", a.channels);
let v = src.info().video.as_ref().unwrap();
@@ -347,7 +347,7 @@ fn truncated_and_corrupt_files_never_panic() {
let _ = s.video_frame(FrameRequest::full(t));
}
}
- if let Some(a) = &info.audio {
+ if let Some(a) = info.audio() {
let _ = s.audio(rng.below(96_000) as i64, 4000, a.sample_rate);
}
}
@@ -364,7 +364,7 @@ fn truncated_and_corrupt_files_never_panic() {
let _ = s.video_frame(FrameRequest::full(s.info().frame_rate().tick_of(i)));
}
}
- if s.info().audio.is_some() {
+ if s.info().has_audio() {
let _ = s.audio(0, 9600, 48_000);
}
}
diff --git a/crates/codecs/tests/ogg_oracle.rs b/crates/codecs/tests/ogg_oracle.rs
index 043715a12..939407756 100644
--- a/crates/codecs/tests/ogg_oracle.rs
+++ b/crates/codecs/tests/ogg_oracle.rs
@@ -88,7 +88,7 @@ fn snr_db(reference: &[f32], ours: &[f32]) -> f64 {
/// (our continuous decode interleaved, ffmpeg's decode interleaved, channels, rate)
fn decode_both(ff: &Path, file: &Path, decoder: &[&str]) -> (OggSource, Vec, Vec, usize, u32) {
let src = OggSource::open(file.file_name().unwrap().to_str().unwrap(), bytes(file)).unwrap();
- let a = src.info().audio.clone().unwrap();
+ let a = src.info().audio().cloned().unwrap();
let ch = a.channels as usize;
let mut args: Vec<&str> = decoder.to_vec();
args.extend_from_slice(&["-i", file.to_str().unwrap(), "-map", "0:a:0", "-f", "f32le", "-"]);
@@ -134,7 +134,7 @@ fn opus_celt_music_matches_libopus() {
assert_eq!(src.info().container, "Ogg");
assert_eq!(src.info().kind, MediaKind::AudioOnly);
assert_eq!((rate, ch), (48_000, 2));
- assert_eq!(src.info().audio.as_ref().unwrap().codec, "Opus");
+ assert_eq!(src.info().audio().unwrap().codec, "Opus");
let snr = snr_db(&want, &ours);
eprintln!("{}: {} samples, SNR {snr:.1} dB vs libopus", f.display(), want.len() / ch);
assert!(snr >= 50.0, "SNR {snr:.1} dB");
@@ -173,7 +173,7 @@ fn vorbis_matches_ffmpeg() {
let ff = filmcraft_testkit::require_ffmpeg!();
let f = make(&ff, "ogg_vorbis.ogg");
let (src, ours, want, ch, rate) = decode_both(&ff, &f, &[]);
- assert_eq!(src.info().audio.as_ref().unwrap().codec, "Vorbis");
+ assert_eq!(src.info().audio().unwrap().codec, "Vorbis");
assert_eq!((rate, ch), (44_100, 2));
let snr = snr_db(&want, &ours);
eprintln!("{}: SNR {snr:.1} dB vs ffmpeg's Vorbis decoder", f.display());
diff --git a/crates/edit/Cargo.toml b/crates/edit/Cargo.toml
index fcecc05a3..bcf354768 100644
--- a/crates/edit/Cargo.toml
+++ b/crates/edit/Cargo.toml
@@ -16,5 +16,6 @@ thiserror = { workspace = true }
[dev-dependencies]
proptest = { workspace = true }
+filmcraft-geom = { workspace = true }
filmcraft-media = { workspace = true }
serde_json = { workspace = true }
diff --git a/crates/edit/src/frame_size.rs b/crates/edit/src/frame_size.rs
new file mode 100644
index 000000000..90585c9cd
--- /dev/null
+++ b/crates/edit/src/frame_size.rs
@@ -0,0 +1,213 @@
+//! Sequence Settings ▸ "Scale motion effects proportionally when changing frame size".
+//!
+//! Motion's Position is in sequence pixels and its Scale is relative to the source, so a new frame
+//! size leaves every clip where it was in pixels: smaller and off-centre in a larger frame, cropped
+//! in a smaller one. With the option on, the whole picture scales instead, by one factor about the
+//! frame centre. On an aspect change (16:9 → 9:16) that factor is the one that fits the old frame
+//! inside the new one (`min(W1/W0, H1/H0)`), so nothing is cropped and the old composition sits
+//! centred, letterboxed or pillarboxed, rather than filling the new frame.
+
+use filmcraft_project::{ParamValue, Sequence, TrackKind};
+
+/// Motion Scale's upper bound (the parameter's range, in %).
+const MAX_SCALE: f64 = 10_000.0;
+
+/// The uniform factor `k` and the old and new frame centres for `from` → `to`, or `None` for an
+/// empty frame on either side.
+fn fit(from: (u32, u32), to: (u32, u32)) -> Option<(f64, (f64, f64), (f64, f64))> {
+ if from.0 == 0 || from.1 == 0 || to.0 == 0 || to.1 == 0 {
+ return None;
+ }
+ let (w0, h0, w1, h1) = (f64::from(from.0), f64::from(from.1), f64::from(to.0), f64::from(to.1));
+ let k = (w1 / w0).min(h1 / h0);
+ (k.is_finite() && k > 0.0).then_some((k, (w0 / 2.0, h0 / 2.0), (w1 / 2.0, h1 / 2.0)))
+}
+
+/// Rescale the Motion of every video clip in `seq` for a frame-size change from `from` to `to`:
+/// static values and every keyframe. Returns how many clips were rescaled (0 when the size does not
+/// change).
+///
+/// - Position maps through `p' = c1 + k·(p − c0)`. An automatic (NaN) position stays automatic: it
+/// follows the frame centre on its own.
+/// - Scale and Scale Width are multiplied by `k`, up to the parameter's 10 000 %. A clip with Scale to
+/// Frame Size keeps its Scale, because the renderer refits it to the new frame anyway.
+/// - Anchor Point (source pixels) and Rotation do not depend on the frame size and stay as they are.
+/// - Only the intrinsic Motion effect changes, as the option's name says: the points of standard
+/// effects (a Mirror centre, the Transform effect's Position) keep their pixel values.
+pub fn scale_motion(seq: &mut Sequence, from: (u32, u32), to: (u32, u32)) -> usize {
+ if from == to {
+ return 0;
+ }
+ let Some((k, c0, c1)) = fit(from, to) else { return 0 };
+ let mut n = 0;
+ for track in seq.all_tracks_mut().filter(|t| t.kind == TrackKind::Video) {
+ for item in &mut track.items {
+ let refits = item.scale_to_frame;
+ let Some(motion) = item.effect_mut("motion") else { continue };
+ if let Some(p) = motion.param_mut("position") {
+ p.map_values(|v| match v {
+ ParamValue::Vec2(mut v) => {
+ v.x = c1.0 + k * (v.x - c0.0);
+ v.y = c1.1 + k * (v.y - c0.1);
+ ParamValue::Vec2(v)
+ }
+ other => other,
+ });
+ }
+ if !refits {
+ for id in ["scale", "scale_width"] {
+ if let Some(p) = motion.param_mut(id) {
+ p.map_values(|v| match v {
+ ParamValue::Float(s) => ParamValue::Float((s * k).clamp(0.0, MAX_SCALE)),
+ other => other,
+ });
+ }
+ }
+ }
+ n += 1;
+ }
+ }
+ n
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use filmcraft_project::{ClipId, ItemId, Keyframe, Label, Project, SequenceSettings, TrackItem, find_effect};
+ use filmcraft_time::Tick;
+
+ fn seq_with(items: Vec) -> Sequence {
+ let mut p = Project::new("t");
+ let s = p.new_sequence("s", SequenceSettings::default(), 2, 1, None);
+ let mut seq = p.sequence(s).unwrap().clone();
+ seq.video_tracks[0].items = items;
+ seq
+ }
+
+ fn clip(id: u64, pos: (f64, f64), scale: f64) -> TrackItem {
+ let mut motion = find_effect("motion").unwrap().instance();
+ motion.param_mut("position").unwrap().value = ParamValue::Vec2(filmcraft_geom::Vec2::new(pos.0, pos.1));
+ motion.param_mut("scale").unwrap().value = ParamValue::Float(scale);
+ TrackItem {
+ id: ClipId(id),
+ item: ItemId(1),
+ name: format!("c{id}"),
+ label: Label::Iris,
+ start: Tick(id as i64 * 1_000_000_000_000),
+ duration: Tick(1_000_000_000_000),
+ source_in: Tick::ZERO,
+ speed: 1.0,
+ reverse: false,
+ enabled: true,
+ link: None,
+ group: None,
+ effects: vec![motion],
+ markers: vec![],
+ gain_db: 0.0,
+ frame_hold: None,
+ scale_to_frame: false,
+ essential: None,
+ multicam: None,
+ time_interpolation: Default::default(),
+ hold_filters: false,
+ field_options: None,
+ source_channels: Vec::new(),
+ audio_stream: 0,
+ graphic: None,
+ }
+ }
+
+ fn pos(seq: &Sequence, i: usize) -> (f64, f64) {
+ match seq.video_tracks[0].items[i].effect("motion").unwrap().param("position").unwrap().value {
+ ParamValue::Vec2(v) => (v.x, v.y),
+ _ => panic!("position is a point"),
+ }
+ }
+
+ fn scale(seq: &Sequence, i: usize) -> f64 {
+ match seq.video_tracks[0].items[i].effect("motion").unwrap().param("scale").unwrap().value {
+ ParamValue::Float(s) => s,
+ _ => panic!("scale is a number"),
+ }
+ }
+
+ fn close(a: (f64, f64), b: (f64, f64)) -> bool {
+ (a.0 - b.0).abs() < 1e-9 && (a.1 - b.1).abs() < 1e-9
+ }
+
+ #[test]
+ fn same_aspect_scales_position_and_scale_together() {
+ let mut seq = seq_with(vec![clip(1, (960.0, 540.0), 100.0), clip(2, (300.0, 90.0), 50.0)]);
+ assert_eq!(scale_motion(&mut seq, (1920, 1080), (1280, 720)), 2);
+ assert!(close(pos(&seq, 0), (640.0, 360.0)), "{:?}", pos(&seq, 0));
+ assert!(close(pos(&seq, 1), (200.0, 60.0)), "{:?}", pos(&seq, 1));
+ assert!((scale(&seq, 0) - 100.0 * 2.0 / 3.0).abs() < 1e-9);
+ assert!((scale(&seq, 1) - 50.0 * 2.0 / 3.0).abs() < 1e-9);
+ }
+
+ #[test]
+ fn aspect_change_fits_the_old_frame_inside_the_new_one() {
+ // 1920x1080 -> 1080x1920: k = min(1080/1920, 1920/1080) = 0.5625, centred
+ let mut seq = seq_with(vec![clip(1, (960.0, 540.0), 100.0), clip(2, (0.0, 0.0), 100.0)]);
+ scale_motion(&mut seq, (1920, 1080), (1080, 1920));
+ assert!(close(pos(&seq, 0), (540.0, 960.0)), "{:?}", pos(&seq, 0));
+ // the old top-left corner lands on the new frame's left edge, letterboxed vertically
+ assert!(close(pos(&seq, 1), (0.0, 960.0 - 0.5625 * 540.0)), "{:?}", pos(&seq, 1));
+ assert!((scale(&seq, 0) - 56.25).abs() < 1e-9);
+ }
+
+ #[test]
+ fn keyframes_scale_too() {
+ let mut c = clip(1, (960.0, 540.0), 100.0);
+ let m = c.effect_mut("motion").unwrap();
+ m.param_mut("position").unwrap().keyframes = vec![
+ Keyframe::new(Tick::ZERO, ParamValue::Vec2(filmcraft_geom::Vec2::new(0.0, 0.0))),
+ Keyframe::new(Tick(10), ParamValue::Vec2(filmcraft_geom::Vec2::new(1920.0, 1080.0))),
+ ];
+ m.param_mut("scale").unwrap().keyframes = vec![Keyframe::new(Tick::ZERO, ParamValue::Float(100.0)), Keyframe::new(Tick(10), ParamValue::Float(200.0))];
+ let mut seq = seq_with(vec![c]);
+ scale_motion(&mut seq, (1920, 1080), (3840, 2160));
+ let m = seq.video_tracks[0].items[0].effect("motion").unwrap();
+ let kp: Vec<_> = m.param("position").unwrap().keyframes.iter().map(|k| k.value.clone()).collect();
+ assert_eq!(kp, vec![ParamValue::Vec2(filmcraft_geom::Vec2::new(0.0, 0.0)), ParamValue::Vec2(filmcraft_geom::Vec2::new(3840.0, 2160.0))]);
+ let ks: Vec<_> = m.param("scale").unwrap().keyframes.iter().map(|k| k.value.clone()).collect();
+ assert_eq!(ks, vec![ParamValue::Float(200.0), ParamValue::Float(400.0)]);
+ }
+
+ #[test]
+ fn automatic_position_stays_automatic() {
+ let mut seq = seq_with(vec![clip(1, (f64::NAN, f64::NAN), 100.0)]);
+ scale_motion(&mut seq, (1920, 1080), (1280, 720));
+ let (x, y) = pos(&seq, 0);
+ assert!(x.is_nan() && y.is_nan());
+ assert!((scale(&seq, 0) - 100.0 * 2.0 / 3.0).abs() < 1e-9);
+ }
+
+ #[test]
+ fn scale_to_frame_clips_keep_their_scale_but_move() {
+ let mut c = clip(1, (480.0, 270.0), 100.0);
+ c.scale_to_frame = true;
+ let mut seq = seq_with(vec![c]);
+ scale_motion(&mut seq, (1920, 1080), (3840, 2160));
+ assert!(close(pos(&seq, 0), (960.0, 540.0)), "{:?}", pos(&seq, 0));
+ assert_eq!(scale(&seq, 0), 100.0);
+ }
+
+ #[test]
+ fn scale_is_capped_at_the_parameter_range() {
+ let mut seq = seq_with(vec![clip(1, (1.0, 1.0), 5000.0)]);
+ scale_motion(&mut seq, (2, 2), (8, 8));
+ assert_eq!(scale(&seq, 0), MAX_SCALE);
+ }
+
+ #[test]
+ fn same_size_or_empty_frame_changes_nothing() {
+ let mut seq = seq_with(vec![clip(1, (100.0, 100.0), 100.0)]);
+ let before = seq.clone();
+ assert_eq!(scale_motion(&mut seq, (1920, 1080), (1920, 1080)), 0);
+ assert_eq!(scale_motion(&mut seq, (0, 1080), (1280, 720)), 0);
+ assert_eq!(scale_motion(&mut seq, (1920, 1080), (1280, 0)), 0);
+ // Debug, not ==: the automatic anchor is NaN, which never equals itself
+ assert_eq!(format!("{:?}", seq.video_tracks[0].items[0].effects), format!("{:?}", before.video_tracks[0].items[0].effects));
+ }
+}
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs
index 67937f854..dc228522a 100644
--- a/crates/edit/src/lib.rs
+++ b/crates/edit/src/lib.rs
@@ -18,6 +18,7 @@
#![cfg_attr(not(test), deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable))]
pub mod captions;
+pub mod frame_size;
pub mod multicam;
pub mod through;
pub mod transcript;
@@ -534,15 +535,30 @@ pub fn clamp_trim(seq: &Sequence, clip: ClipId, edge: Edge, mode: TrimMode, delt
match edge {
Edge::In => {
// extending left (d<0) needs media before source_in; shortening needs min duration
- let max_ext = Tick((head(ctx, it).0 as f64 / speed).floor() as i64);
+ // a reversed clip plays source_out down to source_in, so its In edge consumes media after source_out
+ let handle = match (it.reverse, media) {
+ (true, Some(m)) => (m - it.source_out()).max(Tick::ZERO),
+ (true, None) => Tick::MAX,
+ (false, _) => head(ctx, it),
+ };
+ let max_ext = if handle == Tick::MAX { Tick::MAX } else { Tick((handle.0 as f64 / speed).floor() as i64) };
let lo = if mode == TrimMode::Regular { (-(it.start - prev_end)).max(-max_ext) } else { -max_ext };
let hi = it.duration - ctx.min_duration;
- d = d.clamp(if it.frame_hold.is_some() { Tick::MIN } else { lo }, hi);
+ // a frame hold needs no source head, but a Regular trim still stops at the previous clip
+ let lo = match (it.frame_hold.is_some(), mode) {
+ (true, TrimMode::Regular) => -(it.start - prev_end),
+ (true, _) => Tick::MIN,
+ (false, _) => lo,
+ };
+ d = d.clamp(lo, hi);
}
Edge::Out => {
let lo = -(it.duration - ctx.min_duration);
let mut hi = if mode == TrimMode::Regular { next_start - it.end() } else { Tick::MAX };
- if let Some(m) = media {
+ if it.reverse && it.frame_hold.is_none() {
+ // a reversed clip's Out edge consumes media before source_in
+ hi = hi.min(Tick((head(ctx, it).0 as f64 / speed).floor() as i64));
+ } else if let Some(m) = media {
let remain = Tick(((m - it.source_out()).0 as f64 / speed).floor() as i64);
hi = hi.min(remain);
}
@@ -750,10 +766,19 @@ pub fn roll(seq: &mut Sequence, left: ClipId, right: ClipId, delta: Tick, ctx: &
let mut d = delta;
// left out-point limits
d = d.max(-(l.duration - ctx.min_duration)).min(r.duration - ctx.min_duration);
- if let Some(m) = media_len(ctx, l) {
+ // a reversed clip consumes media from the opposite end, so its media limit is the other one
+ if l.reverse {
+ d = d.min(Tick((head(ctx, l).0 as f64 / l.speed.abs()).floor() as i64));
+ } else if let Some(m) = media_len(ctx, l) {
d = d.min(Tick(((m - l.source_out()).0 as f64 / l.speed.abs()).floor() as i64));
}
- d = d.max(-Tick((head(ctx, r).0 as f64 / r.speed.abs()).floor() as i64));
+ if r.reverse {
+ if let Some(m) = media_len(ctx, r) {
+ d = d.max(-Tick(((m - r.source_out()).0 as f64 / r.speed.abs()).floor() as i64));
+ }
+ } else {
+ d = d.max(-Tick((head(ctx, r).0 as f64 / r.speed.abs()).floor() as i64));
+ }
if d == Tick::ZERO {
return Ok(d);
}
@@ -761,15 +786,19 @@ pub fn roll(seq: &mut Sequence, left: ClipId, right: ClipId, delta: Tick, ctx: &
let before = seq.clone();
{
let (_, l) = seq.find_item_mut(left).ok_or(EditError::NoItem(left))?;
+ if l.reverse {
+ l.source_in -= src_of(d, ls);
+ }
l.duration += d;
}
{
let (_, r) = seq.find_item_mut(right).ok_or(EditError::NoItem(right))?;
r.start += d;
r.duration -= d;
- r.source_in += src_of(d, rs);
+ if !r.reverse {
+ r.source_in += src_of(d, rs);
+ }
}
- let _ = ls;
transitions_follow_cuts(&before, seq);
Ok(d)
}
@@ -798,7 +827,10 @@ pub fn slide(seq: &mut Sequence, clip: ClipId, delta: Tick, ctx: &mut EditCtx) -
let mut d = delta;
if let Some(p) = &prev {
d = d.max(-(p.duration - ctx.min_duration));
- if let Some(m) = media_len(ctx, p) {
+ if p.reverse {
+ // a reversed neighbour grows by pulling earlier media in front of its source In
+ d = d.min(Tick((head(ctx, p).0 as f64 / p.speed.abs()).floor() as i64));
+ } else if let Some(m) = media_len(ctx, p) {
d = d.min(Tick(((m - p.source_out()).0 as f64 / p.speed.abs()).floor() as i64));
}
} else {
@@ -806,7 +838,14 @@ pub fn slide(seq: &mut Sequence, clip: ClipId, delta: Tick, ctx: &mut EditCtx) -
}
if let Some(n) = &next {
d = d.min(n.duration - ctx.min_duration);
- d = d.max(-Tick((head(ctx, n).0 as f64 / n.speed.abs()).floor() as i64));
+ if n.reverse {
+ // a reversed neighbour grows leftwards by pulling later media behind its source Out
+ if let Some(m) = media_len(ctx, n) {
+ d = d.max(-Tick(((m - n.source_out()).0 as f64 / n.speed.abs()).floor() as i64));
+ }
+ } else {
+ d = d.max(-Tick((head(ctx, n).0 as f64 / n.speed.abs()).floor() as i64));
+ }
} else {
d = d.min(neighbours(tr, clip).1 - it.end());
}
@@ -818,12 +857,17 @@ pub fn slide(seq: &mut Sequence, clip: ClipId, delta: Tick, ctx: &mut EditCtx) -
if let Some(p) = prev {
let pi = t.item_mut(p.id).ok_or(EditError::NoItem(p.id))?;
pi.duration += d;
+ if p.reverse {
+ pi.source_in -= src_of(d, p.speed);
+ }
}
if let Some(n) = next {
let ni = t.item_mut(n.id).ok_or(EditError::NoItem(n.id))?;
ni.start += d;
ni.duration -= d;
- ni.source_in += src_of(d, n.speed);
+ if !n.reverse {
+ ni.source_in += src_of(d, n.speed);
+ }
}
let me = t.item_mut(clip).ok_or(EditError::NoItem(clip))?;
me.start += d;
diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rs
index 4f49f1fad..ae258cb0f 100644
--- a/crates/edit/src/tests.rs
+++ b/crates/edit/src/tests.rs
@@ -60,6 +60,7 @@ impl Fx {
hold_filters: false,
field_options: None,
source_channels: Vec::new(),
+ audio_stream: 0,
graphic: None,
}
}
@@ -233,6 +234,27 @@ fn close_gap_works() {
assert_eq!(fx.spans(v1), vec![(0, 10), (10, 10)]);
}
+#[test]
+fn reversed_trim_uses_the_handle_its_playback_direction_consumes() {
+ let mut fx = Fx::new();
+ let v1 = fx.v(0);
+ // source frames [5, 15) played backwards; the media is 1000 frames long
+ let a = fx.put(v1, 30, 10, 5);
+ fx.seq.find_item_mut(a).unwrap().1.reverse = true;
+ let mut n = fx.next;
+ // out extends into the 5 frames before source_in, not into the 985 after source_out
+ let d = trim(&mut fx.seq, a, Edge::Out, TrimMode::Regular, f(20), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!(d, f(5));
+ let it = fx.seq.find_item(a).unwrap().1;
+ assert_eq!((it.source_in, it.source_out()), (f(0), f(15)));
+ // in extends into the media after source_out
+ let d = trim(&mut fx.seq, a, Edge::In, TrimMode::Regular, -f(10), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!(d, -f(10));
+ let it = fx.seq.find_item(a).unwrap().1;
+ assert_eq!((it.source_in, it.source_out()), (f(0), f(25)));
+ fx.seq.check().unwrap();
+}
+
#[test]
fn regular_trim_respects_neighbours_and_handles() {
let mut fx = Fx::new();
@@ -252,6 +274,24 @@ fn regular_trim_respects_neighbours_and_handles() {
fx.seq.check().unwrap();
}
+#[test]
+fn held_regular_in_trim_stops_at_previous_clip() {
+ let mut fx = Fx::new();
+ let v1 = fx.v(0);
+ fx.put(v1, 0, 10, 0);
+ let b = fx.put(v1, 15, 10, 5);
+ fx.seq.find_item_mut(b).unwrap().1.frame_hold = Some(f(5));
+ let mut n = fx.next;
+ // 5 frames of gap: a 20 frame extension is clamped to 5 instead of overlapping the previous clip
+ let d = trim(&mut fx.seq, b, Edge::In, TrimMode::Regular, -f(20), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!(d, -f(5));
+ assert_eq!(fx.spans(v1), vec![(0, 10), (10, 15)]);
+ // no space left: nothing to do, and no error
+ let d = trim(&mut fx.seq, b, Edge::In, TrimMode::Regular, -f(3), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!(d, Tick::ZERO);
+ fx.seq.check().unwrap();
+}
+
#[test]
fn ripple_trim_shifts_following() {
let mut fx = Fx::new();
@@ -358,6 +398,25 @@ fn roll_slip_slide() {
fx.seq.check().unwrap();
}
+#[test]
+fn slide_keeps_reversed_neighbours_content() {
+ let mut fx = Fx::new();
+ let v1 = fx.v(0);
+ let a = fx.put(v1, 0, 30, 20);
+ let b = fx.put(v1, 30, 30, 50);
+ let c = fx.put(v1, 60, 30, 70);
+ for id in [a, c] {
+ fx.seq.find_item_mut(id).unwrap().1.reverse = true;
+ }
+ let mut n = fx.next;
+ slide(&mut fx.seq, b, f(10), &mut Fx::ctx(&mut n)).unwrap();
+ assert_eq!(fx.spans(v1), vec![(0, 40), (40, 30), (70, 20)]);
+ assert_eq!(fx.seq.find_item(a).unwrap().1.source_in, f(10));
+ assert_eq!(fx.seq.find_item(b).unwrap().1.source_in, f(50));
+ assert_eq!(fx.seq.find_item(c).unwrap().1.source_in, f(70));
+ fx.seq.check().unwrap();
+}
+
#[test]
fn transitions_stay_on_their_cuts() {
let mut fx = Fx::new();
@@ -672,3 +731,21 @@ fn shorter_head_closes_the_stretch_after_the_cut_on_sync_locked_tracks() {
assert_eq!(fx.spans(v1), vec![(0, 10), (10, 10), (20, 10)], "unchanged on failure");
}
}
+
+#[test]
+fn roll_reversed_clips_keeps_media_mapping() {
+ let mut fx = Fx::new();
+ let v = fx.v(0);
+ let mut a = fx.item(0, 30, 20);
+ a.reverse = true;
+ let mut b = fx.item(30, 30, 50);
+ b.reverse = true;
+ let (ia, ib) = (a.id, b.id);
+ fx.seq.track_mut(v).unwrap().items.extend([a, b]);
+ let mut n = 0;
+ roll(&mut fx.seq, ia, ib, f(10), &mut Fx::ctx(&mut n)).unwrap();
+ let (_, l) = fx.seq.find_item(ia).unwrap();
+ assert_eq!((l.start, l.duration, l.source_in), (f(0), f(40), f(10)));
+ let (_, r) = fx.seq.find_item(ib).unwrap();
+ assert_eq!((r.start, r.duration, r.source_in), (f(40), f(20), f(50)));
+}
diff --git a/crates/edit/src/through.rs b/crates/edit/src/through.rs
index 0905149a7..9b662abf9 100644
--- a/crates/edit/src/through.rs
+++ b/crates/edit/src/through.rs
@@ -59,6 +59,11 @@ pub fn through_edits(seq: &Sequence) -> Vec {
/// piece is listed. The left piece absorbs the right one (keeping its own effects and attributes);
/// a transition sitting on the cut is removed. Returns the number of edits joined.
pub fn join_through_edits(seq: &mut Sequence, only: &[ClipId]) -> usize {
+ join_through_edits_where(seq, |a, b| only.is_empty() || only.contains(&a) || only.contains(&b))
+}
+
+/// Join the through edits on unlocked tracks whose (left, right) pieces `wanted` accepts.
+pub fn join_through_edits_where(seq: &mut Sequence, wanted: impl Fn(ClipId, ClipId) -> bool) -> usize {
let mut n = 0;
for tr in seq.all_tracks_mut() {
if tr.locked {
@@ -67,8 +72,7 @@ pub fn join_through_edits(seq: &mut Sequence, only: &[ClipId]) -> usize {
let mut i = 0;
while i + 1 < tr.items.len() {
let (a, b) = (&tr.items[i], &tr.items[i + 1]);
- let wanted = only.is_empty() || only.contains(&a.id) || only.contains(&b.id);
- if !(wanted && is_through_edit(a, b)) {
+ if !(wanted(a.id, b.id) && is_through_edit(a, b)) {
i += 1;
continue;
}
@@ -180,6 +184,7 @@ mod tests {
hold_filters: false,
field_options: None,
source_channels: Vec::new(),
+ audio_stream: 0,
graphic: None,
}
}
diff --git a/crates/edit/src/transcript/tests.rs b/crates/edit/src/transcript/tests.rs
index 2fd8ed171..afd8c52bf 100644
--- a/crates/edit/src/transcript/tests.rs
+++ b/crates/edit/src/transcript/tests.rs
@@ -57,6 +57,7 @@ fn clip(id: u64, start: f64, dur: f64, src_in: f64) -> TrackItem {
hold_filters: false,
field_options: None,
source_channels: Vec::new(),
+ audio_stream: 0,
graphic: None,
}
}
diff --git a/crates/engine/src/aaf_omf.rs b/crates/engine/src/aaf_omf.rs
index fd0d01965..823b895dc 100644
--- a/crates/engine/src/aaf_omf.rs
+++ b/crates/engine/src/aaf_omf.rs
@@ -143,7 +143,7 @@ fn decode(s: &Session, need: &AudioNeed, sample_rate: u32) -> Result o.extend(x.iter().copied().chain(std::iter::repeat(0.0)).take(n)),
@@ -230,8 +230,19 @@ fn ensure_dir(s: &Session, dir: &str) {
}
}
+fn check_linked_streams(s: &Session, seq: ItemId, plan: &AudioPlan) -> Result<()> {
+ if plan.mode == AudioMode::Linked && !plan.trim && !plan.render_effects {
+ let needs = audio_needs(&s.project, seq, &NeedOptions { nests: plan.nests, ..Default::default() });
+ if needs.iter().any(|need| need.audio_stream > 0) {
+ return Err(EngineError::Other("linked AAF media cannot select container audio streams; choose embedded or separate audio".into()));
+ }
+ }
+ Ok(())
+}
+
/// Prepare the audio essence of an export; returns the essence and the files written.
pub fn prepare_audio(s: &Session, seq: ItemId, plan: &AudioPlan, media_dir: &str) -> Result<(Vec, Vec)> {
+ check_linked_streams(s, seq, plan)?;
if plan.mode == AudioMode::Linked && !plan.trim && !plan.render_effects {
return Ok((Vec::new(), Vec::new()));
}
@@ -255,7 +266,7 @@ pub fn prepare_audio(s: &Session, seq: ItemId, plan: &AudioPlan, media_dir: &str
let name = audio_clip(&s.project, seq, c).map(|(_, _, ti)| ti.name.clone()).unwrap_or_default();
format!("{} {}", safe_name(&name), c.0)
}
- EssenceKey::Media(_) => safe_name(
+ EssenceKey::Media(_) | EssenceKey::MediaStream { .. } => safe_name(
need.path
.as_deref()
.map(|p| std::path::Path::new(p).file_stem().map(|x| x.to_string_lossy().into_owned()).unwrap_or_default())
@@ -380,6 +391,7 @@ pub fn export_aaf(s: &mut Session, p: &Value) -> Result {
let path = str_p(p, "path").ok_or_else(|| bad(cmd, "need `path`"))?.to_string();
let seq = crate::export_tools::sequence_param(s, p, cmd)?;
let plan = plan_from(s, p, seq, cmd, false)?;
+ check_linked_streams(s, seq, &plan)?;
let (dir, stem) = split(&path);
let media_dir = join(&dir, &format!("{stem} Media"));
let mut files = Vec::new();
diff --git a/crates/engine/src/aaf_omf_tests.rs b/crates/engine/src/aaf_omf_tests.rs
index 1bd52a8c6..83bdd05de 100644
--- a/crates/engine/src/aaf_omf_tests.rs
+++ b/crates/engine/src/aaf_omf_tests.rs
@@ -69,7 +69,7 @@ fn aaf_embedded_trimmed_audio_round_trips_sample_exact() {
assert_eq!(a, b, "{kind:?}");
}
// video links to the original movies, audio to WAV files extracted next to the AAF
- assert_eq!(path_of(&s, q.video_tracks[0].items[0].item), path_of(&s, items[0]));
+ assert_eq!(std::path::Path::new(&path_of(&s, q.video_tracks[0].items[0].item)), std::path::Path::new(&path_of(&s, items[0])));
for (c, oc) in q.audio_tracks[0].items.iter().zip(&orig.audio_tracks[0].items) {
let p = path_of(&s, c.item);
assert!(p.ends_with(".wav") && std::path::Path::new(&p).exists(), "{p}");
@@ -138,7 +138,7 @@ fn aaf_separate_aiff_breakout_and_linked() {
let n = imported_seq(&r);
let q = s.project.sequence(n).unwrap();
let a = &q.audio_tracks[0].items[0];
- assert_eq!(path_of(&s, a.item), path_of(&s, items[0]));
+ assert_eq!(std::path::Path::new(&path_of(&s, a.item)), std::path::Path::new(&path_of(&s, items[0])));
assert_eq!(a.source_in, FrameRate::FPS_24.tick_of(12));
assert!(!s.project.item(a.item).unwrap().as_media().unwrap().offline);
let _ = std::fs::remove_dir_all(dir);
@@ -159,7 +159,7 @@ fn aaf_video_mixdown() {
assert_eq!(q.video_tracks.iter().map(|t| t.items.len()).sum::(), 1);
let v = &q.video_tracks[0].items[0];
assert_eq!(v.duration, FrameRate::FPS_24.tick_of(48));
- assert_eq!(path_of(&s, v.item), mov);
+ assert_eq!(std::path::Path::new(&path_of(&s, v.item)), std::path::Path::new(mov));
let m = s.project.item(v.item).unwrap().as_media().unwrap();
assert!(!m.offline);
assert_eq!(m.info.video.as_ref().map(|v| (v.width, v.height)), Some((64, 36)));
@@ -284,7 +284,7 @@ fn aaf_keeps_a_nested_sequence_as_a_sequence() {
let want: Vec<_> = orig.tracks(kind)[0].items.iter().map(|c| (c.start, c.duration)).collect();
assert_eq!(got, want, "{kind:?}");
}
- assert_eq!(path_of(&s, n.video_tracks[0].items[0].item), path_of(&s, items[0]));
+ assert_eq!(std::path::Path::new(&path_of(&s, n.video_tracks[0].items[0].item)), std::path::Path::new(&path_of(&s, items[0])));
for (c, oc) in n.audio_tracks[0].items.iter().zip(&orig.audio_tracks[0].items) {
let p = path_of(&s, c.item);
assert!(p.ends_with(".wav") && std::path::Path::new(&p).exists(), "{p}");
@@ -311,3 +311,192 @@ fn commands_are_registered_with_menus_and_validate_params() {
assert!(s.execute("file.importAaf", json!({"path": dir.join("a.mov").to_string_lossy()})).is_err());
let _ = std::fs::remove_dir_all(dir);
}
+
+// Selected-stream essence uses only PR361 schema13 placement and decoder fields.
+mod selected_streams {
+ use crate::{Services, Session};
+ use filmcraft_frame::{AudioBuffer, VideoFrame};
+ use filmcraft_media::{FrameRequest, MediaError, MediaInfo, MediaKind, MediaSource};
+ use filmcraft_project::{ClipId, ItemId, ItemKind, Label, MediaClip, MediaRef, SequenceSettings};
+ use filmcraft_time::{FrameRate, Tick};
+ use serde_json::json;
+ use std::sync::{Arc, Mutex};
+
+ type Calls = Arc>>;
+
+ struct Tones {
+ info: MediaInfo,
+ calls: Calls,
+ }
+
+ impl Tones {
+ fn new(streams: usize) -> Self {
+ let source = filmcraft_media::generators::GeneratorSource::new(
+ filmcraft_media::Generator::BarsAndTone,
+ 32,
+ 18,
+ FrameRate::FPS_24,
+ Tick::from_seconds_f64(2.0),
+ );
+ let mut info = source.info().clone();
+ info.kind = MediaKind::AudioOnly;
+ info.video = None;
+ info.audio_streams = vec![info.audio_streams[0].clone(); streams];
+ Self { info, calls: Arc::default() }
+ }
+
+ fn sample(stream: usize, channel: usize, frame: i64, rate: u32) -> f32 {
+ let frequency = (stream * 2 + channel + 1) as f64 * 240.0;
+ let amplitude = 0.05 * (stream + 1) as f64;
+ (amplitude * (std::f64::consts::TAU * frequency * frame as f64 / rate as f64).sin()) as f32
+ }
+ }
+
+ impl MediaSource for Tones {
+ fn info(&self) -> &MediaInfo {
+ &self.info
+ }
+ fn video_frame(&self, _: FrameRequest) -> filmcraft_media::Result> {
+ Err(MediaError::NoStream("video"))
+ }
+ fn audio(&self, start: i64, frames: usize, sample_rate: u32) -> filmcraft_media::Result {
+ self.audio_stream(0, start, frames, sample_rate)
+ }
+ fn audio_stream(&self, stream: usize, start: i64, frames: usize, sample_rate: u32) -> filmcraft_media::Result {
+ if stream >= self.info.audio_streams.len() {
+ return Err(MediaError::NoStream("audio"));
+ }
+ self.calls.lock().unwrap().push((stream, start, frames, sample_rate));
+ Ok(AudioBuffer {
+ sample_rate,
+ channels: (0..2).map(|channel| (0..frames).map(|i| Self::sample(stream, channel, start + i as i64, sample_rate)).collect()).collect(),
+ })
+ }
+ }
+
+ #[derive(Default)]
+ struct MemoryFiles {
+ writes: Mutex)>>,
+ }
+ impl Services for MemoryFiles {
+ fn read_file(&self, _: &str) -> std::io::Result> {
+ Err(std::io::ErrorKind::NotFound.into())
+ }
+ fn write_file(&self, path: &str, bytes: &[u8]) -> std::io::Result<()> {
+ self.writes.lock().unwrap().push((path.to_owned(), bytes.to_vec()));
+ Ok(())
+ }
+ fn export_in_memory(&self) -> bool {
+ true
+ }
+ }
+
+ fn add_source(s: &mut Session, streams: usize) -> (ItemId, Calls) {
+ let source = Arc::new(Tones::new(streams));
+ let calls = source.calls.clone();
+ let mut project = (*s.project).clone();
+ let item = project.add_item(
+ "distinct tones",
+ Label::Iris,
+ ItemKind::Media(MediaClip {
+ media: MediaRef::File { path: format!("tone-source-{streams}.wav") },
+ info: source.info.clone(),
+ interpret: Default::default(),
+ mark_in: None,
+ mark_out: None,
+ markers: vec![],
+ offline: false,
+ proxy: None,
+ identity: None,
+ }),
+ None,
+ );
+ s.project = Arc::new(project);
+ s.media.insert(item, source);
+ (item, calls)
+ }
+
+ fn fixture() -> (Session, ItemId, ItemId, Calls, Arc) {
+ let files = Arc::new(MemoryFiles::default());
+ let mut session = Session::new(files.clone());
+ let mut project = (*session.project).clone();
+ let sequence =
+ project.new_sequence("streams", SequenceSettings { width: 32, height: 18, frame_rate: FrameRate::FPS_24, ..Default::default() }, 0, 1, None);
+ session.project = Arc::new(project);
+ session.state.active_sequence = Some(sequence);
+ session.state.open_sequences = vec![sequence];
+ let (item, calls) = add_source(&mut session, 3);
+ session.state.project_selection = vec![item];
+ session.execute("sequence.linkedSelection", json!({"on": false})).unwrap();
+ (session, item, sequence, calls, files)
+ }
+
+ fn place(s: &mut Session, item: ItemId, seconds: f64) -> Vec {
+ let result = s.execute("timeline.place", json!({"item": item.0, "track": "A1", "seconds": seconds})).unwrap();
+ result["clips"].as_array().unwrap().iter().map(|id| ClipId(id.as_u64().unwrap())).collect()
+ }
+
+ #[test]
+ fn aaf_essence_distinguishes_streams_and_linked_refusal_precedes_writes() {
+ use crate::aaf_omf::{AudioMode, AudioPlan};
+ use filmcraft_interchange::essence::{EssenceData, EssenceKey, NestNeeds};
+ let (mut s, item, sequence, calls, files) = fixture();
+ place(&mut s, item, 0.0);
+ let plan = AudioPlan {
+ mode: AudioMode::Embedded,
+ aiff: false,
+ mxf: false,
+ sample_rate: 48_000,
+ bits: 16,
+ trim: true,
+ handles: Tick::ZERO,
+ render_effects: false,
+ breakout: false,
+ nests: NestNeeds::Inside,
+ };
+ let (essences, paths) = crate::aaf_omf::prepare_audio(&s, sequence, &plan, "ignored").unwrap();
+ assert!(paths.is_empty());
+ assert_eq!(essences.len(), 3);
+ for (stream, essence) in essences.iter().enumerate() {
+ assert_eq!(essence.key, EssenceKey::media(item, stream));
+ let EssenceData::Embedded(bytes) = &essence.data else { panic!("expected embedded PCM") };
+ for channel in 0..2 {
+ let offset = (25 * 2 + channel) * 2;
+ let actual = i16::from_le_bytes([bytes[offset], bytes[offset + 1]]);
+ let expected = (Tones::sample(stream, channel, 25, 48_000) * 32767.0).round() as i16;
+ assert_eq!(actual, expected);
+ }
+ }
+ let options = filmcraft_interchange::aaf::AafOptions {
+ media: filmcraft_interchange::essence::MediaOptions { essence: essences, ..Default::default() },
+ ..Default::default()
+ };
+ let (aaf, _) = filmcraft_interchange::aaf::export(&s.project, sequence, &options).unwrap();
+ let omf_options = filmcraft_interchange::omf::OmfOptions { media: options.media.clone(), ..Default::default() };
+ let (omf, _) = filmcraft_interchange::omf::export(&s.project, sequence, &omf_options).unwrap();
+ for (kind, document) in [("AAF", aaf), ("OMF", omf)] {
+ let (imported, extracted, _) = if kind == "AAF" {
+ filmcraft_interchange::aaf::import(&document, &Default::default()).unwrap()
+ } else {
+ filmcraft_interchange::omf::import(&document, &Default::default()).unwrap()
+ };
+ assert_eq!(extracted.len(), 3, "{kind}: composition memo must retain distinct selected-stream essence");
+ let tracks = &imported.project.sequence(imported.sequences[0]).unwrap().audio_tracks;
+ assert_eq!(tracks.len(), 3, "{kind}");
+ let mut source_ids: Vec<_> = tracks.iter().map(|track| track.items[0].item).collect();
+ source_ids.sort();
+ source_ids.dedup();
+ assert_eq!(source_ids.len(), 3, "{kind}: each stream remains a distinct imported source");
+ let mut waveforms: Vec<_> = extracted.iter().map(|media| media.wav.clone()).collect();
+ waveforms.sort();
+ waveforms.dedup();
+ assert_eq!(waveforms.len(), 3, "{kind}");
+ }
+ assert!(calls.lock().unwrap().iter().any(|call| call.0 == 2));
+ assert!(files.writes.lock().unwrap().is_empty());
+ let error =
+ s.execute("file.exportAaf", json!({"path": "must-not-be-written.aaf", "sequence": sequence.0, "audio": "linked", "trimAudio": false})).unwrap_err();
+ assert!(error.to_string().contains("embedded") || error.to_string().contains("separate"));
+ assert!(files.writes.lock().unwrap().is_empty());
+ }
+}
diff --git a/crates/engine/src/audio_placement_tests.rs b/crates/engine/src/audio_placement_tests.rs
new file mode 100644
index 000000000..f765e055c
--- /dev/null
+++ b/crates/engine/src/audio_placement_tests.rs
@@ -0,0 +1,179 @@
+//! Multi-clip audio placement: `audio_placement_specs` and dynamic audio track allocation.
+
+use filmcraft_media::MediaSource;
+use filmcraft_project::{AudioChannels, ItemId, TrackKind};
+use filmcraft_time::{FrameRate, Tick, TimeRange};
+
+use serde_json::json;
+
+use crate::Session;
+use crate::commands::{AudioPlacementSpec, audio_placement_specs, ensure_audio_tracks};
+
+fn demo() -> Session {
+ let mut s = Session::default();
+ s.execute("file.openDemoProject", json!({})).unwrap();
+ s
+}
+
+fn active(s: &Session) -> ItemId {
+ s.state.active_sequence.expect("demo session has an active sequence")
+}
+
+#[test]
+fn specs_always_place_one_clip_on_the_destination() {
+ assert_eq!(audio_placement_specs(&[], 1), vec![AudioPlacementSpec { track_offset: 0, audio_stream: 0, source_channels: None }]);
+ assert_eq!(audio_placement_specs(&[vec![0, 1]], 1).len(), 1);
+}
+
+#[test]
+fn specs_put_further_clips_on_the_tracks_below() {
+ let specs = audio_placement_specs(&[vec![0], vec![1], vec![2, 3]], 1);
+ let got: Vec<(usize, Option>)> = specs.into_iter().map(|s| (s.track_offset, s.source_channels)).collect();
+ assert_eq!(got, vec![(0, None), (1, Some(vec![1])), (2, Some(vec![2, 3]))]);
+}
+
+#[test]
+fn specs_are_capped_for_hostile_channel_maps() {
+ let clips = vec![vec![0u16]; 100_000];
+ assert!(audio_placement_specs(&clips, usize::MAX).len() <= crate::sequence_tools::MAX_TRACKS);
+}
+
+#[test]
+fn specs_append_container_streams_after_channel_map_clips() {
+ let specs = audio_placement_specs(&[vec![1], vec![0]], 3);
+ assert_eq!(
+ specs.iter().map(|s| (s.track_offset, s.audio_stream, s.source_channels.clone())).collect::>(),
+ vec![(0, 0, None), (1, 0, Some(vec![0])), (2, 1, None), (3, 2, None)]
+ );
+ assert_eq!(audio_placement_specs(&[], 0).len(), 1);
+}
+
+#[test]
+fn placing_media_with_two_streams_expands_tracks_and_keeps_stream_indices() {
+ let mut s = Session::default();
+ let mut p = (*s.project).clone();
+ let rate = FrameRate::FPS_24;
+ let seq = p.new_sequence("Two streams", Default::default(), 1, 1, None);
+ let source = filmcraft_media::generators::GeneratorSource::new(filmcraft_media::Generator::BarsAndTone, 32, 32, rate, rate.tick_of(24));
+ let mut info = source.info().clone();
+ info.audio_streams.push(info.audio_streams[0].clone());
+ let item = p.add_item(
+ "two streams",
+ filmcraft_project::Label::Iris,
+ filmcraft_project::ItemKind::Media(filmcraft_project::MediaClip {
+ media: filmcraft_project::MediaRef::Generator(filmcraft_media::Generator::BarsAndTone),
+ info,
+ interpret: Default::default(),
+ mark_in: None,
+ mark_out: None,
+ markers: vec![],
+ offline: false,
+ proxy: None,
+ identity: None,
+ }),
+ None,
+ );
+ let (vdest, adest) = {
+ let q = p.sequence(seq).unwrap();
+ (q.video_tracks[0].id, q.audio_tracks[0].id)
+ };
+ s.project = std::sync::Arc::new(p);
+ s.state.active_sequence = Some(seq);
+ let ids =
+ crate::commands::place_item(&mut s, item, TimeRange::new(Tick::ZERO, rate.tick_of(24)), Tick::ZERO, Some(vdest), Some(adest), false, "test", None)
+ .unwrap();
+ assert_eq!(ids.len(), 3);
+ let q = s.project.sequence(seq).unwrap();
+ assert_eq!(q.audio_tracks.len(), 2);
+ let (a, b) = (&q.audio_tracks[0].items[0], &q.audio_tracks[1].items[0]);
+ assert_eq!((a.audio_stream, b.audio_stream), (0, 1));
+ assert_eq!(a.link, b.link);
+ assert!(a.link.is_some());
+ assert_ne!(a.id, b.id);
+ assert_eq!(a.source_in, b.source_in);
+ assert_eq!(a.start, b.start);
+}
+
+#[test]
+fn clip_peaks_measure_the_stream_the_clip_plays() {
+ let mut s = Session::default();
+ let mut p = (*s.project).clone();
+ let rate = FrameRate::FPS_24;
+ let seq = p.new_sequence("Two streams", Default::default(), 1, 1, None);
+ let source = filmcraft_media::generators::GeneratorSource::new(filmcraft_media::Generator::BarsAndTone, 32, 32, rate, rate.tick_of(24));
+ let mut info = source.info().clone();
+ info.audio_streams.push(info.audio_streams[0].clone());
+ let item = p.add_item(
+ "two streams",
+ filmcraft_project::Label::Iris,
+ filmcraft_project::ItemKind::Media(filmcraft_project::MediaClip {
+ media: filmcraft_project::MediaRef::Generator(filmcraft_media::Generator::BarsAndTone),
+ info,
+ interpret: Default::default(),
+ mark_in: None,
+ mark_out: None,
+ markers: vec![],
+ offline: false,
+ proxy: None,
+ identity: None,
+ }),
+ None,
+ );
+ let (vdest, adest) = {
+ let q = p.sequence(seq).unwrap();
+ (q.video_tracks[0].id, q.audio_tracks[0].id)
+ };
+ s.project = std::sync::Arc::new(p);
+ s.state.active_sequence = Some(seq);
+ let ids =
+ crate::commands::place_item(&mut s, item, TimeRange::new(Tick::ZERO, rate.tick_of(24)), Tick::ZERO, Some(vdest), Some(adest), false, "test", None)
+ .unwrap();
+ let q = s.project.sequence(seq).unwrap();
+ let (a, b) = (q.audio_tracks[0].items[0].id, q.audio_tracks[1].items[0].id);
+ assert!(ids.contains(&a) && ids.contains(&b));
+ let peaks = crate::mixer::clip_peaks(&s, &[a, b]);
+ let db = |c: filmcraft_project::ClipId| peaks.iter().find(|(id, _)| *id == c).map(|(_, d)| *d).unwrap();
+ // The generator only has stream 0, so the clip playing stream 1 is silent, not a copy of stream 0.
+ assert!(db(a) > -30.0, "{peaks:?}");
+ assert!(db(b) < -100.0, "{peaks:?}");
+}
+
+#[test]
+fn missing_audio_tracks_are_cloned_from_the_destination() {
+ let s = demo();
+ let seq = active(&s);
+ let mut p = (*s.project).clone();
+ let have = p.sequence(seq).map(|q| q.audio_tracks.len()).unwrap_or(0);
+ assert!(have >= 1);
+ if let Some(t) = p.sequence_mut(seq).and_then(|q| q.audio_tracks.get_mut(0)) {
+ t.channels = AudioChannels::Mono;
+ t.volume_db = -3.0;
+ t.locked = true;
+ }
+ let ids = ensure_audio_tracks(&mut p, seq, 0, have + 3, "test").unwrap();
+ assert_eq!(ids.len(), have + 3);
+ let q = p.sequence(seq).unwrap();
+ for t in &q.audio_tracks[have..] {
+ assert_eq!(t.kind, TrackKind::Audio);
+ assert_eq!(t.channels, AudioChannels::Mono);
+ assert_eq!(t.volume_db, -3.0);
+ assert!(t.items.is_empty() && !t.locked);
+ }
+ let mut unique = ids.clone();
+ unique.sort_by_key(|t| t.0);
+ unique.dedup();
+ assert_eq!(unique.len(), ids.len(), "track ids are unique");
+ // enough tracks already: nothing is added
+ let again = ensure_audio_tracks(&mut p, seq, 0, have + 3, "test").unwrap();
+ assert_eq!(again, ids);
+}
+
+#[test]
+fn hostile_track_counts_are_an_error_not_a_crash() {
+ let s = demo();
+ let seq = active(&s);
+ let mut p = (*s.project).clone();
+ assert!(ensure_audio_tracks(&mut p, seq, usize::MAX, usize::MAX, "test").is_err());
+ assert!(ensure_audio_tracks(&mut p, seq, 0, 1_000_000, "test").is_err());
+ assert!(ensure_audio_tracks(&mut p, ItemId(u64::MAX), 0, 1, "test").is_err());
+}
diff --git a/crates/engine/src/autosave.rs b/crates/engine/src/autosave.rs
index 5b87a9585..733384873 100644
--- a/crates/engine/src/autosave.rs
+++ b/crates/engine/src/autosave.rs
@@ -22,12 +22,13 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::mpsc::{Receiver, RecvTimeoutError, Sender, channel};
use std::thread::JoinHandle;
-use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
+use std::time::Duration;
use filmcraft_format::autosave as names;
use filmcraft_project::Project;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
+use web_time::{Instant, SystemTime, UNIX_EPOCH};
use crate::settings;
@@ -470,7 +471,17 @@ impl Preferences {
}
}
-/// The per-user data directory (`FILMCRAFT_DATA_DIR` overrides):
+/// A file with this name next to the executable makes the install portable: its data directory is
+/// `
| | | | | |