diff --git a/.config/nextest.toml b/.config/nextest.toml index 2a4cd2cb9..38f939c31 100644 --- a/.config/nextest.toml +++ b/.config/nextest.toml @@ -1,3 +1,14 @@ # Minimum cargo-nextest for this repository: GHSA-vc25-qg58-w57c is fixed in 0.9.150 and CI # installs exactly that version, so an older nextest refuses to run (exit 92 REQUIRED_VERSION_NOT_MET). nextest-version = "0.9.150" + +[test-groups] +gpu = { max-threads = 1 } + +# Coverage runs each test in a separate process, so the GPU suite's in-process lock +# cannot protect software adapters from competing devices and readback timeouts. +# UI harnesses can create a device lazily when rendering a screenshot, so include +# the whole UI package instead of an incomplete list of GPU-named tests. +[[profile.default.overrides]] +filter = 'package(effectcraft-gpu) | package(effectcraft-ui-egui) | package(effectcraft-sam2) | (package(effectcraft-cli) & test(/^cli::gpu_adapter_name_/))' +test-group = 'gpu' diff --git a/.github/workflows/packaging-lint.yml b/.github/workflows/packaging-lint.yml index b903f82ea..ccf74f43a 100644 --- a/.github/workflows/packaging-lint.yml +++ b/.github/workflows/packaging-lint.yml @@ -28,6 +28,10 @@ jobs: name: Windows installation policy (no app build) runs-on: windows-latest steps: + - name: Harden runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: persist-credentials: false diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 354fbd268..ffbe49afb 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,6 +1,10 @@ name: Release on: + # A nightly dry run on main: every release build except macOS, nothing signed or released + # (craft-common-actions release.yml `dry-run`). Finds release-only breakage before release day. + schedule: + - cron: "32 1 * * *" push: branches: [release] workflow_dispatch: @@ -29,6 +33,7 @@ jobs: # Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos. uses: storytold/craft-common-actions/.github/workflows/release.yml@main with: + dry-run: ${{ github.event_name == 'schedule' }} app-name: effectcraft app-title: EffectCraft version: ${{ inputs.version }} diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 93b34b9bf..3c1706cc3 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -64,7 +64,7 @@ and licence texts are in [`NOTICE`](NOTICE). | `crates/model/tests/fixtures/cube.obj` | EffectCraft contributors | Original work: Wavefront OBJ test fixture (unit cube, two materials) | MIT OR Apache-2.0 | | `crates/model/tests/fixtures/cube.mtl` | EffectCraft contributors | Original work: MTL test fixture (materials of cube.obj) | MIT OR Apache-2.0 | -One small After Effects test project of `crates/aep` is in this repository: `crates/aep/tests/fixtures/synthetic.aep` (original work, made with After Effects 2026 from own data, working space None, the embedded colour profile removed; MIT OR Apache-2.0, with its `.attribution` sidecar). The other After Effects test projects of `crates/aep` are not in this repository. They are original work of an EffectCraft contributor, generated with After Effects 2026 as own test data (MIT OR Apache-2.0, each file with its `.attribution` sidecar); they are in [storytold/effectcraft-corpus](https://github.com/storytold/effectcraft-corpus) with their sidecars, fetched by `cargo xtask aep-corpus` (commit-pinned, SHA-256-verified), and the tests read them only from the folder named by `EFFECTCRAFT_AEP_FIXTURES` (opt-in). Their embedded colour profiles are replaced by stubs made with `crates/aep/examples/stubicc.rs` (a header and a description, no colour data), so no vendor profile data is in them. +Three small After Effects test projects of `crates/aep` are in this repository: `crates/aep/tests/fixtures/synthetic.aep` (original work, made with After Effects 2026 from own data, working space None, the embedded colour profile removed; MIT OR Apache-2.0, with its `.attribution` sidecar), `crates/aep/tests/fixtures/ai-mask-3099.aep` (original work, made with After Effects 2026 from four hand-written one-page PDFs named `.ai`, footage paths and host name neutralised, the embedded colour profiles replaced by stubs made with `crates/aep/examples/stubicc.rs`; MIT OR Apache-2.0, with its `.attribution` sidecar) and `crates/aep/tests/fixtures/text-synthetic.aep` (original work, made with After Effects 2026 by script from own data, working space None, no colour profile embedded, nothing changed after the save; MIT OR Apache-2.0, with its `.attribution` sidecar). The other After Effects test projects of `crates/aep` are not in this repository. They are original work of an EffectCraft contributor, generated with After Effects 2026 as own test data (MIT OR Apache-2.0, each file with its `.attribution` sidecar); they are in [storytold/effectcraft-corpus](https://github.com/storytold/effectcraft-corpus) with their sidecars, fetched by `cargo xtask aep-corpus` (commit-pinned, SHA-256-verified), and the tests read them only from the folder named by `EFFECTCRAFT_AEP_FIXTURES` (opt-in). Their embedded colour profiles are replaced by stubs made with `crates/aep/examples/stubicc.rs` (a header and a description, no colour data), so no vendor profile data is in them. ## Screenshots @@ -99,7 +99,7 @@ Screenshots of EffectCraft itself, rendered headlessly, showing only procedurall (sidecars `.attribution`). - Bundled extension (ScriptUI panel) with its twelve built-in ease presets: `extensions/scriptui-panels/Ease Presets.jsx` (sidecar `.attribution`). -- Example effect plug-in: `examples/plugins/posterize-bands` (plug-in API v1, WebAssembly). +- Example effect plug-in: `examples/plugins/posterize-bands` (WebAssembly ABI 1). ## Trained models (optional downloads, not bundled) @@ -129,6 +129,14 @@ configuration from these Apache-2.0 projects: - `crates/segment/src/mobilesam.rs` follows the MobileSAM, TinyViT and Segment Anything architectures and their Apache-2.0 reference code. +## Frozen matte regression fixture + +| Asset | Author | Source | Licence | +|---|---|---|---| +| `crates/engine/src/tests/fixtures/analysed-0.24.ecproj` | arnav-hiwarkar | Original work, synthetic project keyed by EffectCraft 0.24.1 | MIT OR Apache-2.0 | +| `crates/engine/src/tests/fixtures/object-matte-0.22.ecproj` | lisn0 | Original work, synthetic project saved by EffectCraft 0.22.0 | MIT OR Apache-2.0 | +| `crates/engine/src/tests/fixtures/tracked-0.22.ecproj` | arnav-hiwarkar | Original work, synthetic project saved by EffectCraft 0.22.0 | MIT OR Apache-2.0 | + ## First-party brand marks The ArtCraft name, wordmark and logos are trademarks of the ArtCraft Team. They are not open source and are not diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 29521ad73..8ce4b85aa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -26,7 +26,7 @@ that variable set. ```sh cargo run -p effectcraft -- --demo # the desktop app, with the demo project cargo test --workspace -cargo xtask ci # what every commit must pass +cargo xtask ci # what every commit must pass (dev-profile checks and tests) ``` The default `dev` profile keeps debug info to line tables: builds are fast and panics/backtraces diff --git a/Cargo.lock b/Cargo.lock index f897737db..e6450c078 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1089,30 +1089,70 @@ dependencies = [ "libc", ] +[[package]] +name = "craft-aep" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" + +[[package]] +name = "craft-audio" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9)", + "thiserror 2.0.21", +] + [[package]] name = "craft-docpath" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" + +[[package]] +name = "craft-dpx" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "thiserror 2.0.21", +] [[package]] name = "craft-ecproj-format" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=ca1b5e2edb8568335c6c14f390307bd5beae8238#ca1b5e2edb8568335c6c14f390307bd5beae8238" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" dependencies = [ "serde", "serde_json", "thiserror 2.0.21", ] +[[package]] +name = "craft-eps" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" + +[[package]] +name = "craft-exr" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "serde_json", +] + [[package]] name = "craft-fonts-build" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" + +[[package]] +name = "craft-gif" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" [[package]] name = "craft-i18n" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" dependencies = [ "fixed_decimal", "icu_decimal", @@ -1132,6 +1172,23 @@ dependencies = [ "writeable", ] +[[package]] +name = "craft-lottie" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-media" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "craft-media" version = "0.1.0" @@ -1140,19 +1197,39 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-mesh" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "miniz_oxide 0.9.1", + "serde_json", +] + [[package]] name = "craft-pdf" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" dependencies = [ "hayro-syntax", "thiserror 2.0.21", ] +[[package]] +name = "craft-psd" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "encoding_rs", + "flate2", + "rayon", + "thiserror 2.0.21", +] + [[package]] name = "craft-quit" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" dependencies = [ "dispatch2", "egui", @@ -1160,10 +1237,22 @@ dependencies = [ "objc2-app-kit 0.3.2", ] +[[package]] +name = "craft-svg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "craft-xml", + "flate2", + "kurbo", + "roxmltree", + "thiserror 2.0.21", +] + [[package]] name = "craft-ui" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" dependencies = [ "craft-fonts-build", "craft-i18n", @@ -1184,7 +1273,7 @@ dependencies = [ [[package]] name = "craft-update" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" dependencies = [ "chrono", "dunce", @@ -1194,6 +1283,7 @@ dependencies = [ "image", "log", "plist", + "rustix 1.1.5", "semver", "serde", "serde_json", @@ -1207,6 +1297,33 @@ dependencies = [ "zip 9.1.3", ] +[[package]] +name = "craft-video" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9)", + "rayon", + "thiserror 2.0.21", +] + +[[package]] +name = "craft-webp" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "thiserror 2.0.21", +] + +[[package]] +name = "craft-xml" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" +dependencies = [ + "quick-xml 0.41.0", + "thiserror 2.0.21", +] + [[package]] name = "crc32fast" version = "1.5.2" @@ -1420,8 +1537,9 @@ dependencies = [ [[package]] name = "effectcraft" -version = "0.21.0" +version = "0.24.1" dependencies = [ + "chrono", "cpal", "craft-update", "effectcraft-engine", @@ -1439,6 +1557,8 @@ dependencies = [ "rfd", "serde", "serde_json", + "sysinfo", + "tempfile", "winit", "winresource", "zbus", @@ -1446,13 +1566,15 @@ dependencies = [ [[package]] name = "effectcraft-aep" -version = "0.21.0" +version = "0.24.1" dependencies = [ + "craft-aep", "effectcraft-color", "effectcraft-effects", "effectcraft-keyframe", "effectcraft-project", "effectcraft-time", + "log", "serde", "serde_json", "thiserror 2.0.21", @@ -1460,7 +1582,7 @@ dependencies = [ [[package]] name = "effectcraft-automation" -version = "0.21.0" +version = "0.24.1" dependencies = [ "effectcraft-engine", "effectcraft-host", @@ -1472,16 +1594,9 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "effectcraft-av1enc" -version = "0.21.0" -dependencies = [ - "filmcraft-av1", -] - [[package]] name = "effectcraft-cli" -version = "0.21.0" +version = "0.24.1" dependencies = [ "cpu-time", "effectcraft-automation", @@ -1500,7 +1615,7 @@ dependencies = [ [[package]] name = "effectcraft-color" -version = "0.21.0" +version = "0.24.1" dependencies = [ "proptest", "serde", @@ -1508,7 +1623,7 @@ dependencies = [ [[package]] name = "effectcraft-effects" -version = "0.21.0" +version = "0.24.1" dependencies = [ "chrono", "effectcraft-color", @@ -1526,7 +1641,7 @@ dependencies = [ [[package]] name = "effectcraft-engine" -version = "0.21.0" +version = "0.24.1" dependencies = [ "chrono", "craft-docpath", @@ -1565,12 +1680,13 @@ dependencies = [ [[package]] name = "effectcraft-export" -version = "0.21.0" +version = "0.24.1" dependencies = [ - "effectcraft-av1enc", + "craft-audio", + "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9)", + "craft-video", "effectcraft-color", "effectcraft-effects", - "effectcraft-hevcenc", "effectcraft-keyframe", "effectcraft-media", "effectcraft-opusenc", @@ -1578,25 +1694,22 @@ dependencies = [ "effectcraft-raster", "effectcraft-render", "effectcraft-time", - "effectcraft-vp9enc", "exr", - "filmcraft-aac", - "filmcraft-h264enc", - "filmcraft-isobmff", - "filmcraft-opus", - "filmcraft-prores", + "filmcraft-av1", + "filmcraft-hevc", "filmcraft-vp9", "gif", "image", "log", "rayon", + "tempfile", "thiserror 2.0.21", "web-time", ] [[package]] name = "effectcraft-expr" -version = "0.21.0" +version = "0.24.1" dependencies = [ "boa_engine", "effectcraft-color", @@ -1613,7 +1726,7 @@ dependencies = [ [[package]] name = "effectcraft-geom" -version = "0.21.0" +version = "0.24.1" dependencies = [ "proptest", "serde", @@ -1621,7 +1734,7 @@ dependencies = [ [[package]] name = "effectcraft-gpu" -version = "0.21.0" +version = "0.24.1" dependencies = [ "bytemuck", "effectcraft-color", @@ -1644,7 +1757,7 @@ dependencies = [ [[package]] name = "effectcraft-helper-host" -version = "0.21.0" +version = "0.24.1" dependencies = [ "bytemuck", "effectcraft-effects", @@ -1653,16 +1766,9 @@ dependencies = [ "serde_json", ] -[[package]] -name = "effectcraft-hevcenc" -version = "0.21.0" -dependencies = [ - "filmcraft-hevc", -] - [[package]] name = "effectcraft-host" -version = "0.21.0" +version = "0.24.1" dependencies = [ "effectcraft-engine", "effectcraft-export", @@ -1689,7 +1795,7 @@ dependencies = [ [[package]] name = "effectcraft-interchange" -version = "0.21.0" +version = "0.24.1" dependencies = [ "effectcraft-color", "effectcraft-keyframe", @@ -1708,7 +1814,7 @@ dependencies = [ [[package]] name = "effectcraft-keyframe" -version = "0.21.0" +version = "0.24.1" dependencies = [ "effectcraft-color", "effectcraft-geom", @@ -1720,9 +1826,10 @@ dependencies = [ [[package]] name = "effectcraft-lottie" -version = "0.21.0" +version = "0.24.1" dependencies = [ "craft-docpath", + "craft-lottie", "effectcraft-color", "effectcraft-effects", "effectcraft-keyframe", @@ -1738,9 +1845,15 @@ dependencies = [ [[package]] name = "effectcraft-media" -version = "0.21.0" +version = "0.24.1" dependencies = [ + "craft-audio", "craft-docpath", + "craft-dpx", + "craft-exr", + "craft-gif", + "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9)", + "craft-webp", "effectcraft-color", "effectcraft-effects", "effectcraft-model", @@ -1752,12 +1865,9 @@ dependencies = [ "effectcraft-svg", "effectcraft-time", "exr", - "filmcraft-aac", "filmcraft-codecs", "filmcraft-color", "filmcraft-frame", - "filmcraft-isobmff", - "filmcraft-matroska", "filmcraft-media", "filmcraft-mpegts", "filmcraft-time", @@ -1766,32 +1876,32 @@ dependencies = [ "log", "rayon", "serde_json", + "tempfile", "thiserror 2.0.21", ] [[package]] name = "effectcraft-model" -version = "0.21.0" +version = "0.24.1" dependencies = [ + "craft-mesh", "effectcraft-geom", "image", "log", - "miniz_oxide 0.9.1", "serde", - "serde_json", "thiserror 2.0.21", ] [[package]] name = "effectcraft-opusenc" -version = "0.21.0" +version = "0.24.1" dependencies = [ - "filmcraft-opus", + "craft-audio", ] [[package]] name = "effectcraft-path" -version = "0.21.0" +version = "0.24.1" dependencies = [ "effectcraft-geom", "effectcraft-keyframe", @@ -1803,8 +1913,9 @@ dependencies = [ [[package]] name = "effectcraft-pdf" -version = "0.21.0" +version = "0.24.1" dependencies = [ + "craft-eps", "craft-pdf", "effectcraft-raster", "effectcraft-svg", @@ -1819,7 +1930,7 @@ dependencies = [ [[package]] name = "effectcraft-plugin" -version = "0.21.0" +version = "0.24.1" dependencies = [ "effectcraft-effects", "effectcraft-keyframe", @@ -1831,7 +1942,7 @@ dependencies = [ [[package]] name = "effectcraft-project" -version = "0.21.0" +version = "0.24.1" dependencies = [ "craft-ecproj-format", "effectcraft-color", @@ -1845,15 +1956,17 @@ dependencies = [ [[package]] name = "effectcraft-psd" -version = "0.21.0" +version = "0.24.1" dependencies = [ + "craft-psd", "miniz_oxide 0.9.1", ] [[package]] name = "effectcraft-raster" -version = "0.21.0" +version = "0.24.1" dependencies = [ + "craft-exr", "effectcraft-color", "effectcraft-geom", "rayon", @@ -1862,7 +1975,7 @@ dependencies = [ [[package]] name = "effectcraft-render" -version = "0.21.0" +version = "0.24.1" dependencies = [ "effectcraft-color", "effectcraft-effects", @@ -1885,7 +1998,7 @@ dependencies = [ [[package]] name = "effectcraft-sam2" -version = "0.21.0" +version = "0.24.1" dependencies = [ "bytemuck", "candle-core", @@ -1900,7 +2013,7 @@ dependencies = [ [[package]] name = "effectcraft-script" -version = "0.21.0" +version = "0.24.1" dependencies = [ "boa_engine", "effectcraft-engine", @@ -1918,7 +2031,7 @@ dependencies = [ [[package]] name = "effectcraft-segment" -version = "0.21.0" +version = "0.24.1" dependencies = [ "miniz_oxide 0.9.1", "ndarray", @@ -1927,19 +2040,21 @@ dependencies = [ [[package]] name = "effectcraft-svg" -version = "0.21.0" +version = "0.24.1" dependencies = [ + "craft-svg", "effectcraft-geom", "effectcraft-path", "effectcraft-raster", "kurbo", + "log", "rayon", "roxmltree", ] [[package]] name = "effectcraft-text" -version = "0.21.0" +version = "0.24.1" dependencies = [ "effectcraft-keyframe", "harfrust", @@ -1951,7 +2066,7 @@ dependencies = [ [[package]] name = "effectcraft-time" -version = "0.21.0" +version = "0.24.1" dependencies = [ "proptest", "serde", @@ -1959,7 +2074,7 @@ dependencies = [ [[package]] name = "effectcraft-track" -version = "0.21.0" +version = "0.24.1" dependencies = [ "effectcraft-raster", "effectcraft-segment", @@ -1971,7 +2086,7 @@ dependencies = [ [[package]] name = "effectcraft-ui-egui" -version = "0.21.0" +version = "0.24.1" dependencies = [ "bytemuck", "chrono", @@ -2010,13 +2125,9 @@ dependencies = [ "web-time", ] -[[package]] -name = "effectcraft-vp9enc" -version = "0.21.0" - [[package]] name = "effectcraft-web" -version = "0.21.0" +version = "0.24.1" dependencies = [ "effectcraft-engine", "effectcraft-export", @@ -2481,7 +2592,7 @@ name = "filmcraft-bitstream" version = "0.12.0" source = "git+https://github.com/storytold/filmcraft?rev=d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5#d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" dependencies = [ - "craft-media", + "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f0ad6c1e1d297908915f4be315b0fcfc9be7b5d4)", ] [[package]] @@ -2582,16 +2693,6 @@ dependencies = [ "thiserror 2.0.21", ] -[[package]] -name = "filmcraft-h264enc" -version = "0.12.0" -source = "git+https://github.com/storytold/filmcraft?rev=d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5#d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" -dependencies = [ - "filmcraft-bitstream", - "rayon", - "thiserror 2.0.21", -] - [[package]] name = "filmcraft-hevc" version = "0.12.0" @@ -2624,7 +2725,7 @@ name = "filmcraft-isobmff" version = "0.12.0" source = "git+https://github.com/storytold/filmcraft?rev=d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5#d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" dependencies = [ - "craft-media", + "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f0ad6c1e1d297908915f4be315b0fcfc9be7b5d4)", ] [[package]] @@ -2632,7 +2733,7 @@ name = "filmcraft-matroska" version = "0.12.0" source = "git+https://github.com/storytold/filmcraft?rev=d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5#d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" dependencies = [ - "craft-media", + "craft-media 0.1.0 (git+https://github.com/storytold/craft-libs?rev=f0ad6c1e1d297908915f4be315b0fcfc9be7b5d4)", ] [[package]] @@ -3068,7 +3169,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8" dependencies = [ "rustix 1.1.5", - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -3321,7 +3422,7 @@ checksum = "9f4d0e94ddd48749f06bbe4e5389fb9799a0c45bcaf00495042076ef05e3241a" [[package]] name = "hayro-jbig2" version = "0.3.0" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" dependencies = [ "hayro-ccitt", ] @@ -3335,7 +3436,7 @@ checksum = "c75ab947623ef4ccaa7acf0579edf7cbb5a73838e3839a7be73335e522f433a1" [[package]] name = "hayro-syntax" version = "0.7.2" -source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9" dependencies = [ "hayro-jbig2", "hayro-jpeg2000", @@ -3668,7 +3769,7 @@ dependencies = [ "simd_cesu8", "thiserror 2.0.21", "walkdir", - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -3814,7 +3915,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" dependencies = [ "cfg-if", - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -4120,6 +4221,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ntapi" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3b335231dfd352ffb0f8017f3b6027a4917f7df785ea2143d8af2adc66980ae" +dependencies = [ + "winapi", +] + [[package]] name = "nu-ansi-term" version = "0.50.3" @@ -4434,6 +4544,16 @@ dependencies = [ "objc2-core-foundation", ] +[[package]] +name = "objc2-io-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15" +dependencies = [ + "libc", + "objc2-core-foundation", +] + [[package]] name = "objc2-io-surface" version = "0.3.2" @@ -4635,7 +4755,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.45.0", ] [[package]] @@ -4664,7 +4784,7 @@ dependencies = [ "libc", "redox_syscall 0.5.18", "smallvec", - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -6071,6 +6191,20 @@ dependencies = [ "walkdir", ] +[[package]] +name = "sysinfo" +version = "0.37.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16607d5caffd1c07ce073528f9ed972d88db15dd44023fa57142963be3feb11f" +dependencies = [ + "libc", + "memchr", + "ntapi", + "objc2-core-foundation", + "objc2-io-kit", + "windows 0.61.3", +] + [[package]] name = "tag_ptr" version = "0.1.0" @@ -7224,16 +7358,38 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows" +version = "0.61.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893" +dependencies = [ + "windows-collections 0.2.0", + "windows-core 0.61.2", + "windows-future 0.2.1", + "windows-link 0.1.3", + "windows-numerics 0.2.0", +] + [[package]] name = "windows" version = "0.62.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580" dependencies = [ - "windows-collections", + "windows-collections 0.3.2", "windows-core 0.62.2", - "windows-future", - "windows-numerics", + "windows-future 0.3.2", + "windows-numerics 0.3.1", +] + +[[package]] +name = "windows-collections" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8" +dependencies = [ + "windows-core 0.61.2", ] [[package]] @@ -7255,6 +7411,19 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-core" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link 0.1.3", + "windows-result 0.3.4", + "windows-strings 0.4.2", +] + [[package]] name = "windows-core" version = "0.62.2" @@ -7263,9 +7432,20 @@ checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" dependencies = [ "windows-implement", "windows-interface", - "windows-link", + "windows-link 0.2.1", "windows-result 0.4.1", - "windows-strings", + "windows-strings 0.5.1", +] + +[[package]] +name = "windows-future" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e" +dependencies = [ + "windows-core 0.61.2", + "windows-link 0.1.3", + "windows-threading 0.1.0", ] [[package]] @@ -7275,8 +7455,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb" dependencies = [ "windows-core 0.62.2", - "windows-link", - "windows-threading", + "windows-link 0.2.1", + "windows-threading 0.2.1", ] [[package]] @@ -7301,12 +7481,28 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "windows-link" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-numerics" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1" +dependencies = [ + "windows-core 0.61.2", + "windows-link 0.1.3", +] + [[package]] name = "windows-numerics" version = "0.3.1" @@ -7314,7 +7510,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26" dependencies = [ "windows-core 0.62.2", - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -7323,9 +7519,9 @@ version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720" dependencies = [ - "windows-link", + "windows-link 0.2.1", "windows-result 0.4.1", - "windows-strings", + "windows-strings 0.5.1", ] [[package]] @@ -7337,13 +7533,31 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-result" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" +dependencies = [ + "windows-link 0.1.3", +] + [[package]] name = "windows-result" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" dependencies = [ - "windows-link", + "windows-link 0.2.1", +] + +[[package]] +name = "windows-strings" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" +dependencies = [ + "windows-link 0.1.3", ] [[package]] @@ -7352,7 +7566,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" dependencies = [ - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -7388,7 +7602,7 @@ version = "0.61.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" dependencies = [ - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -7422,13 +7636,22 @@ dependencies = [ "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows-threading" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6" +dependencies = [ + "windows-link 0.1.3", +] + [[package]] name = "windows-threading" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37" dependencies = [ - "windows-link", + "windows-link 0.2.1", ] [[package]] @@ -7711,7 +7934,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7" [[package]] name = "xtask" -version = "0.21.0" +version = "0.24.1" dependencies = [ "chrono", "pkg-config", @@ -7959,7 +8182,6 @@ dependencies = [ "nix", "typed-path 0.13.0", "windows-sys 0.61.2", - "zopfli", ] [[package]] @@ -7974,18 +8196,6 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" -[[package]] -name = "zopfli" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aaf7fc5d30c28483d93805c4a5e12b05bbb52407fa67c5f8bd552374cd01fb11" -dependencies = [ - "bumpalo", - "crc32fast", - "log", - "simd-adler32", -] - [[package]] name = "zune-core" version = "0.5.3" @@ -8050,18 +8260,3 @@ dependencies = [ "syn 3.0.6", "winnow", ] - -[[patch.unused]] -name = "egui_demo_lib" -version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" - -[[patch.unused]] -name = "egui_inspection" -version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" - -[[patch.unused]] -name = "egui_system_font_provider" -version = "0.37.0" -source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" diff --git a/Cargo.toml b/Cargo.toml index abb2256c2..1b80048f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,19 +4,29 @@ members = ["crates/*", "apps/*", "xtask"] default-members = ["crates/*", "apps/*"] [workspace.package] -version = "0.21.0" +version = "0.24.1" edition = "2024" license = "MIT OR Apache-2.0" rust-version = "1.95" repository = "https://github.com/storytold/effectcraft" [workspace.dependencies] -craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", default-features = false, features = ["read"] } -craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } -craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } -craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "ca1b5e2edb8568335c6c14f390307bd5beae8238" } +craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", version = "=0.1.0", default-features = false, features = ["read"] } +craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-ecproj-format = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-dpx = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-eps = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-exr = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-gif = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-psd = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-svg = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-webp = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-lottie = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } +craft-mesh = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", default-features = false, features = ["stl", "obj", "gltf", "fbx"] } +craft-aep = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" } # craft-update: app-specific pretend versions apply to startup and manual checks. -craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", features = ["egui"] } +craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "47107f660191de2f0b425569506f4daa4faadac9", features = ["egui"] } # internal effectcraft-time = { path = "crates/time" } effectcraft-geom = { path = "crates/geom" } @@ -41,7 +51,6 @@ effectcraft-interchange = { path = "crates/interchange" } effectcraft-pdf = { path = "crates/pdf" } effectcraft-psd = { path = "crates/psd" } effectcraft-aep = { path = "crates/aep" } -effectcraft-vp9enc = { path = "crates/vp9enc" } effectcraft-svg = { path = "crates/svg" } effectcraft-engine = { path = "crates/engine" } effectcraft-host = { path = "crates/host" } @@ -49,8 +58,6 @@ effectcraft-ui-egui = { path = "crates/ui-egui" } effectcraft-automation = { path = "crates/automation" } effectcraft-opusenc = { path = "crates/opusenc" } effectcraft-script = { path = "crates/script" } -effectcraft-hevcenc = { path = "crates/hevcenc" } -effectcraft-av1enc = { path = "crates/av1enc" } effectcraft-plugin = { path = "crates/plugin" } effectcraft-helper-host = { path = "crates/helper-host" } @@ -82,25 +89,23 @@ image = { version = "0.25", default-features = false, features = ["png", "jpeg"] same-file = "1.0" tempfile = "3.27" -# media codecs + containers: FilmCraft's pure-Rust crates, pinned to a revision (plan/adr/0001). +# codecs + containers shared by the Craft apps (crates/media, crates/export); keep all three on one rev. +craft-media = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", default-features = false, features = ["isobmff", "matroska"] } +craft-audio = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", default-features = false, features = ["aac-decode", "aac-encode", "opus-decode"] } +craft-video = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", features = ["h264-encode", "prores", "hevc-encode", "vp9-encode", "av1", "threads"] } + +# footage decoding: FilmCraft's pure-Rust crates, pinned to a revision (plan/adr/0001). # Only `crates/media`, `crates/export` and `crates/interchange` may depend on these. filmcraft-codecs = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } filmcraft-media = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } filmcraft-frame = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } filmcraft-color = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } filmcraft-time = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } -# encoders + muxer (crates/export) -filmcraft-h264enc = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } -filmcraft-prores = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } -filmcraft-aac = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } -filmcraft-isobmff = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } -# container headers read for AAC channel layouts (crates/media) -filmcraft-matroska = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } +# MPEG TS / PS headers read for AAC channel layouts (crates/media); craft-media has no MPEG TS reader filmcraft-mpegts = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } -# decoder used as the round-trip oracle of effectcraft-opusenc (crates/export dev-dependency) -filmcraft-opus = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } +# decoders used as the round-trip oracles of craft-video's VP9, HEVC and AV1 encoders (crates/export +# dev-dependencies only); craft-video has no decoder for them filmcraft-vp9 = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } -# decoders used as the round-trip oracles of effectcraft-hevcenc / effectcraft-av1enc (dev-dependencies only) filmcraft-hevc = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } filmcraft-av1 = { git = "https://github.com/storytold/filmcraft", rev = "d5ead4ba1ee2f1cd13cb86a4239aee2730a3d9f5" } # timeline interchange (EDL, FCP7 XML, FCPXML, OTIO) for Premiere Pro interop (crates/interchange only) @@ -117,7 +122,7 @@ half = "2.7" # ui # Shared frontend components; keep this dependency above the engine boundary. # Shared UI and the complete egui family select reviewed immutable commits below. -craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", features = ["serde"] } +craft-ui = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", features = ["serde"] } # `default_fonts` and `monochrome_emoji_fonts` are explicit: the app's `FontDefinitions::default()` # and its `✔`/`▣` chrome glyphs need both, and kittest's own dev-dependencies do not propagate. egui = { version = "0.37", default-features = false, features = ["default_fonts", "monochrome_emoji_fonts"] } @@ -137,11 +142,6 @@ sys-locale = "0.3" # filmcraft-frame = { path = "../filmcraft/crates/frame" } # filmcraft-color = { path = "../filmcraft/crates/color" } # filmcraft-time = { path = "../filmcraft/crates/time" } -# filmcraft-h264enc = { path = "../filmcraft/crates/h264enc" } -# filmcraft-prores = { path = "../filmcraft/crates/prores" } -# filmcraft-aac = { path = "../filmcraft/crates/aac" } -# filmcraft-isobmff = { path = "../filmcraft/crates/isobmff" } -# filmcraft-matroska = { path = "../filmcraft/crates/matroska" } # filmcraft-mpegts = { path = "../filmcraft/crates/mpegts" } [workspace.lints.rust] @@ -200,50 +200,39 @@ opt-level = 3 [profile.dev.package.effectcraft-export] opt-level = 3 -[profile.dev.package.effectcraft-vp9enc] +# craft-libs codec hot loops. +[profile.dev.package.craft-video] opt-level = 3 -[profile.dev.package.effectcraft-hevcenc] +[profile.dev.package.craft-audio] opt-level = 3 -[profile.dev.package.effectcraft-av1enc] +[profile.dev.package.craft-media] opt-level = 3 # FilmCraft codec hot loops (same settings as FilmCraft's own workspace). [profile.dev.package.filmcraft-h264] opt-level = 3 -[profile.dev.package.filmcraft-h264enc] -opt-level = 3 - [profile.dev.package.filmcraft-hevc] opt-level = 3 [profile.dev.package.filmcraft-vp9] opt-level = 3 -[profile.dev.package.filmcraft-prores] -opt-level = 3 - [profile.dev.package.filmcraft-dnx] opt-level = 3 [profile.dev.package.filmcraft-av1] opt-level = 3 -[profile.dev.package.filmcraft-aac] -opt-level = 3 - -[profile.dev.package.filmcraft-opus] -opt-level = 3 - [profile.release] lto = "thin" codegen-units = 1 [patch.crates-io] -hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.7.2" } -hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.3.0" } +hayro-syntax = { git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", version = "=0.7.2" } +hayro-jbig2 = { git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", version = "=0.3.0" } ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } @@ -254,8 +243,7 @@ egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "6d518fc79968 egui_glow = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } egui_extras = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } -egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } eframe = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } -egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } -egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +# Patch a crate only together with the dependency that needs it: an unused patch makes the Nix +# build, which uses vendored sources, fail (`cargo xtask lock-patches`). winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" } diff --git a/README.md b/README.md index 81b5a0ba5..57eddebdc 100644 --- a/README.md +++ b/README.md @@ -129,18 +129,14 @@ or into the Timeline, between layers and at the time you point to. 308 built-in effects across its categories, each with its parameter names, order and defaults: blur and sharpen, channel, color correction (Curves, Levels, Hue/Saturation, a full -color-grading panel…), distort (Warp, Bulge, Turbulent Displace, CC Power Pin…), generate (Fractal Noise, +color-grading panel…), distort (Warp, Bulge, Turbulent Displace, Perspective Pin…), generate (Fractal Noise, Gradient Ramp, Stroke, Write-on, Audio Spectrum…), keying, matte, noise and grain, perspective, -**simulation** (CC Particle World, CC Rainfall, Shatter, Card Dance, Caustics, Wave World…), -stylize (Glow, CC Glass…), **time** (Echo, Posterize Time, Timewarp, Time Displacement…), +**simulation** (Particle Emitter 3D, Rainfall, Shatter, Card Dance, Caustics, Wave World…), +stylize (Glow, Glass Relief…), **time** (Echo, Posterize Time, Timewarp, Time Displacement…), **audio** (Reverb, Parametric EQ, Delay, Stereo Mixer…), text, transitions, utility and expression controls. All nine **Layer Styles** (Drop Shadow, Inner/Outer Glow, Bevel and Emboss, Satin, overlays, Stroke) with Global Light. Preview plays audio in sync, with meters and waveforms. -

- A CC Particle World burst with Glow, and the Effect Controls panel listing the particle system's parameters -

- ## 3D Classic 3D compositing: 3D layers with orientation and material options, @@ -160,7 +156,7 @@ MP4, **WebM** (VP9 with inter frames and alpha, or AV1; Opus audio), PNG, JPEG, rendering with 3:2 pulldown, effect/solo/guide/depth overrides, crop, region of interest and resize, Render Settings and Output Module templates with defaults, post-render actions, storage overflow and render logs. The same queue runs from the command line. Every encoder is pure -Rust (FilmCraft's H.264, ProRes and AAC; EffectCraft's own VP9, AV1, HEVC and Opus); there is no +Rust (craft-libs' H.264, ProRes, HEVC, VP9, AV1 and AAC; EffectCraft's own Opus); there is no FFmpeg inside. **Lottie** goes both ways: File ▸ Export ▸ Lottie JSON… writes a composition (precomps, shape, diff --git a/apps/effectcraft-cli/src/main.rs b/apps/effectcraft-cli/src/main.rs index 2981519e9..f52401a00 100644 --- a/apps/effectcraft-cli/src/main.rs +++ b/apps/effectcraft-cli/src/main.rs @@ -31,12 +31,13 @@ //! effectcraft-cli bench --roto [--frames N] [--sam2 model.safetensors] [--mobilesam mobile_sam.pt] [--fixes] [--clip FRAMES --truth MASKS] [--json] Roto Brush trackers on synthetic clips with known mattes //! (classical engine, MobileSAM and SAM 2.1 (any installed size) when installed; IoU, flicker, re-appearance, seconds per frame) //! effectcraft-cli script FILE.jsx [F.ecproj] | --eval CODE run an After Effects-style script +//! effectcraft-cli replay SCRIPT.jsonl [--keep-going] replay a repro script (docs/agents.md) //! effectcraft-cli mcp [--autosave | --bridge PORT] MCP server on stdio //! //! Project: --project F.ecproj (or a positional *.ecproj) | --demo | --empty (default: demo; mcp: empty) //! Saving: --save (back to --project) | --save-as F.ecproj -//! GPU: --gpu renders on the GPU compositor (Mercury GPU Acceleration) when an adapter exists; -//! the default is the CPU (Mercury Software Only) +//! GPU: --gpu renders on the GPU compositor (GPU Acceleration) when an adapter exists; +//! the default is the CPU (Software Rendering) //! Bridge: --bridge PORT drives a running `effectcraft --control PORT` instead of a headless session //! Output: --json for one compact JSON document on stdout (errors: {"error": …}, exit 1) //! ``` @@ -97,7 +98,7 @@ fn stdout_broken() -> bool { STDOUT_FAILED.get().is_some_and(|k| *k != std::io::ErrorKind::BrokenPipe) } -const USAGE: &str = "usage: effectcraft-cli [args] [--json] +const USAGE: &str = "usage: effectcraft-cli [args] [--json] info project + engine summary commands [--filter TEXT] [--enabled] list engine commands exec [--params JSON] run one engine command (exec --list: list them, as `commands`) @@ -138,6 +139,9 @@ const USAGE: &str = "usage: effectcraft-cli Result { } /// A wired session; `--gpu` attaches the GPU compositor (renders then follow the project's -/// renderer setting, Mercury GPU Acceleration by default). +/// renderer setting, GPU Acceleration by default). fn session(args: &Args) -> Result { let mut s = effectcraft_host::session(); if args.flag("--autosave") { @@ -552,6 +557,59 @@ fn script_cmd(args: &Args, json_out: bool) -> Result<(), Failure> { Ok(()) } +/// `replay SCRIPT.jsonl`: run a repro script against a headless session (its start line picks +/// the project) and report every step. Exits 1 when the replay fails. +fn replay_cmd(args: &Args, json_out: bool) -> Result<(), Failure> { + use effectcraft_engine::replay; + let Some(file) = args.pos.first() else { return usage_err("replay needs a .jsonl script") }; + if args.opt("--bridge").is_some() || args.project.is_some() || args.flag("--demo") || args.flag("--empty") { + return usage_err("replay runs headless from the script's start line ({\"start\": \"new\" | \"demo\" | {\"project\": path}})"); + } + use std::io::Read; + let path = std::path::Path::new(file); + let input = std::fs::File::open(path).map_err(|e| Failure::Error(format!("cannot read {file}: {e}")))?; + // Bound the read itself: metadata can be stale or describe a pipe rather than its contents. + let mut script = String::new(); + input.take(replay::MAX_SCRIPT_BYTES as u64 + 1).read_to_string(&mut script).map_err(|e| Failure::Error(format!("cannot read {file}: {e}")))?; + if script.len() > replay::MAX_SCRIPT_BYTES { + return Err(Failure::Error(format!("{file} is larger than {} MB", replay::MAX_SCRIPT_BYTES >> 20))); + } + let mut s = session(args)?; + let opts = replay::Options { keep_going: args.flag("--keep-going"), base_dir: path.parent().map(std::path::Path::to_path_buf) }; + let report = replay::run(&mut s, &script, &opts); + if json_out { + emit(&serde_json::to_value(&report).unwrap_or_else(|e| json!({"error": e.to_string()})), true); + } else { + for st in &report.steps { + let what = st.command.as_deref().unwrap_or(st.kind); + match (&st.error, st.ok) { + (None, _) => say!("line {:>4} ok {what}", st.line), + (Some(e), true) => say!("line {:>4} ok {what} (tolerated error: {e})", st.line), + (Some(e), false) => say!("line {:>4} FAIL {what}: {e}", st.line), + } + } + } + let Some(first) = &report.first_failure else { + if !json_out { + say!("replay ok: {} steps", report.steps.len()); + } + return Ok(()); + }; + let msg = format!( + "failed at line {} ({}){}: {}{}", + first.line, + first.command.as_deref().unwrap_or(first.kind), + if report.panicked { ", a command panicked" } else { "" }, + first.error.as_deref().unwrap_or(""), + if report.skipped > 0 { format!(" ({} later steps not run)", report.skipped) } else { String::new() } + ); + if json_out { + note!("effectcraft-cli replay: {msg}"); + std::process::exit(1); + } + Err(Failure::Error(msg)) +} + /// Print a result: compact JSON with `--json`, else pretty JSON. fn emit(v: &Value, json_out: bool) { if json_out { @@ -614,6 +672,7 @@ fn run(cmd: &str, args: &Args, json_out: bool) -> Result<(), Failure> { "commands" => list_commands_cmd(args, json_out)?, "exec" if args.flag("--list") => list_commands_cmd(args, json_out)?, "script" => script_cmd(args, json_out)?, + "replay" => replay_cmd(args, json_out)?, "exec" => { let Some(id) = args.pos.first().cloned() else { return usage_err("exec needs a command id") }; let params = match (args.opt("--params"), args.pos.get(1).map(String::as_str)) { @@ -1443,7 +1502,7 @@ fn adv3d_bench_comp(p: &mut effectcraft_engine::project::Project) -> Option Result<(), Failure> { use effectcraft_render::Backend; diff --git a/apps/effectcraft-cli/tests/it/cli.rs b/apps/effectcraft-cli/tests/it/cli.rs index 363eb8e52..4510f16c9 100644 --- a/apps/effectcraft-cli/tests/it/cli.rs +++ b/apps/effectcraft-cli/tests/it/cli.rs @@ -1223,3 +1223,39 @@ fn render_exr_compression_and_depth() { assert!(reply["error"].as_str().unwrap().contains("exrBitDepth: half|float"), "{reply}"); let _ = std::fs::remove_dir_all(&root); } + +#[test] +fn replay_reports_steps_and_the_first_failure() { + let repro = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/repros/comp-zero-frame-rate.jsonl"); + let v = ok_json(&["replay", repro.to_str().unwrap()]); + assert_eq!(v["ok"], true, "{v}"); + assert!(v["steps"].as_array().unwrap().len() > 5); + let bad = tmp("bad-repro.jsonl"); + std::fs::write( + &bad, + "{\"start\": \"new\"}\n{\"command\": \"comp.new\"}\n{\"expect\": {\"query\": \"comp.info\", \"pointer\": \"/width\", \"equals\": 1}}\nnot json\n", + ) + .unwrap(); + let (code, v) = run_json(&["replay", bad.to_str().unwrap()]); + assert_eq!(code, 1); + assert_eq!(v["ok"], false); + assert_eq!(v["first_failure"]["line"], 3, "{v}"); + assert_eq!(v["skipped"], 1); + let out = bin().args(["replay", bad.to_str().unwrap(), "--keep-going"]).output().unwrap(); + assert_eq!(out.status.code(), Some(1)); + let text = String::from_utf8_lossy(&out.stdout); + assert!(text.contains("line 2 ok comp.new") && text.contains("line 4 FAIL invalid: not JSON"), "{text}"); + assert!(String::from_utf8_lossy(&out.stderr).contains("failed at line 3 (comp.info)")); + // The start line picks the project. + assert_eq!(bin().args(["replay", bad.to_str().unwrap(), "--demo"]).output().unwrap().status.code(), Some(2)); +} + +/// Text editors can prefix an exported JSON-lines repro with a UTF-8 BOM. +#[test] +fn replay_accepts_a_utf8_bom() { + let path = tmp("bom-repro.jsonl"); + std::fs::write(&path, "\u{feff}{\"command\":\"comp.new\"}\n").unwrap(); + let (code, report) = run_json(&["replay", path.to_str().unwrap()]); + assert_eq!(code, 0, "{report}"); + assert_eq!(report["ok"], true); +} diff --git a/apps/effectcraft/Cargo.toml b/apps/effectcraft/Cargo.toml index 3030ee96d..2c8224f65 100644 --- a/apps/effectcraft/Cargo.toml +++ b/apps/effectcraft/Cargo.toml @@ -25,6 +25,9 @@ rfd = { workspace = true } pollster = { workspace = true } cpal = { workspace = true } log = { workspace = true } +chrono = { workspace = true } +# MIT; safe process handles and target-only memory observation for the crash helper. +sysinfo = { version = "=0.37.2", default-features = false, features = ["system"] } [target.'cfg(target_os = "linux")'.dependencies] # Native GNOME/Wayland controls without a bundled title font. @@ -38,6 +41,9 @@ zbus = "5.19" # Icon + VERSIONINFO for effectcraft.exe; build.rs only uses it when targeting Windows. winresource = "0.1" +[dev-dependencies] +tempfile = { workspace = true } + [target.'cfg(target_os = "macos")'.dependencies] # Keep the control channel responsive when the window is covered (App Nap / occlusion throttling). objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSProcessInfo", "NSString", "NSObject"] } diff --git a/apps/effectcraft/src/crash_observation.rs b/apps/effectcraft/src/crash_observation.rs new file mode 100644 index 000000000..b67dc07fe --- /dev/null +++ b/apps/effectcraft/src/crash_observation.rs @@ -0,0 +1,264 @@ +//! Bounded observations made outside the failed GUI. Measurements are evidence, not a cause. +use std::io::{self, Write}; +use std::sync::mpsc::{self, Receiver}; +use std::time::{Duration, Instant, SystemTime}; + +use sysinfo::{MemoryRefreshKind, Pid, ProcessRefreshKind, ProcessesToUpdate, System}; + +pub fn utc() -> String { + chrono::DateTime::::from(SystemTime::now()).to_rfc3339_opts(chrono::SecondsFormat::Millis, true) +} + +#[derive(Default)] +struct Memory { + samples: u64, + resident_peak: u64, + virtual_peak: u64, + available_min: Option, + total: u64, + last_at: Option, + last_uptime_secs: Option, +} + +impl Memory { + fn sample(&mut self, resident: u64, virtual_bytes: u64, available: u64, total: u64) { + self.samples = self.samples.saturating_add(1); + self.resident_peak = self.resident_peak.max(resident); + self.virtual_peak = self.virtual_peak.max(virtual_bytes); + // Zero total means the platform could not supply physical-memory information. + if total != 0 { + self.available_min = Some(self.available_min.map_or(available, |old| old.min(available))); + self.total = total; + } + self.last_at = Some(utc()); + } +} + +pub struct Observation { + pid: Pid, + system: System, + birth: Option, + started: Instant, + started_at: String, + detected_at: Option, + last_sample: Instant, + memory: Memory, + // Only Windows can retrieve an unrelated process's ExitStatus through a retained handle. + exit: Receiver>, + status: Option, + alive: Option, +} + +fn target_system(pid: Pid) -> System { + let mut system = System::new(); + system.refresh_processes_specifics(ProcessesToUpdate::Some(&[pid]), false, ProcessRefreshKind::nothing().with_memory()); + system +} + +impl Observation { + /// Acquire the Windows handle before acknowledging reporter readiness, so even an immediate + /// GUI abort leaves an observable process object. Never enumerate commands or environments. + pub fn start(pid: u32) -> Self { + let pid = Pid::from_u32(pid); + let system = target_system(pid); + let birth = system.process(pid).map(|p| p.start_time()); + let (send, exit) = mpsc::sync_channel(1); + #[cfg(windows)] + { + let waiting = target_system(pid); + let _ = std::thread::Builder::new().name("ec-exit-status".into()).spawn(move || { + let status = waiting.process(pid).and_then(|p| p.wait()); + let _ = send.send(status); + }); + } + #[cfg(not(windows))] + drop(send); + let now = Instant::now(); + let mut result = Self { + pid, + system, + birth, + started: now, + started_at: utc(), + detected_at: None, + last_sample: now, + memory: Memory::default(), + exit, + status: None, + alive: None, + }; + result.sample(); + result + } + + fn sample(&mut self) { + self.system.refresh_processes_specifics(ProcessesToUpdate::Some(&[self.pid]), false, ProcessRefreshKind::nothing().with_memory()); + self.system.refresh_memory_specifics(MemoryRefreshKind::nothing().with_ram()); + let process = self.system.process(self.pid).filter(|p| Some(p.start_time()) == self.birth && p.exists()); + self.alive = self.birth.map(|_| process.is_some()); + if let Some(process) = process { + self.memory.last_uptime_secs = Some(process.run_time()); + self.memory.sample(process.memory(), process.virtual_memory(), self.system.available_memory(), self.system.total_memory()); + } + if let Ok(status) = self.exit.try_recv() { + self.status = status; + } + self.last_sample = Instant::now(); + } + + pub fn tick(&mut self) { + if self.last_sample.elapsed() >= Duration::from_secs(1) { + self.sample(); + } + } + + /// Pipe closure can precede the OS signalling process termination. Bound that grace period; + /// a broken pipe or early reporter drop must never be called a confirmed process exit. + pub fn finish(&mut self) { + self.detected_at = Some(utc()); + if self.status.is_none() + && let Ok(status) = self.exit.recv_timeout(Duration::from_millis(750)) + { + self.status = status; + } + self.sample(); + } + + pub fn confirmed_exit(&self) -> bool { + self.status.is_some() + } + + pub fn write(&self, report: &mut impl Write) -> io::Result<()> { + writeln!( + report, + "Detection UTC: {}\nObserver started UTC: {}\nObserved duration: {} ms", + self.detected_at.as_deref().unwrap_or("not captured"), + self.started_at, + self.started.elapsed().as_millis() + )?; + writeln!( + report, + "Process observation: {}", + if self.confirmed_exit() { + "process termination observed" + } else if self.alive == Some(true) { + "GUI still present when the control channel ended" + } else { + "target absent from refresh or process identity/status unavailable (not proof of termination)" + } + )?; + match &self.status { + Some(status) => { + writeln!(report, "OS exit status: {status}")?; + #[cfg(windows)] + if let Some(code) = status.code() { + write_windows_status(report, code as u32)?; + } + } + None => writeln!(report, "OS exit status: unavailable (not inferred from the control pipe)")?, + } + let memory = &self.memory; + writeln!(report, "\nMemory evidence (1 s sampling; sampled maxima, not OS peaks):\nTarget-present sampling attempts: {}", memory.samples)?; + if let Some(uptime) = memory.last_uptime_secs { + writeln!(report, "GUI uptime at last target-present observation: {uptime} s")?; + } + #[cfg(windows)] + let virtual_label = "Peak sampled private committed bytes"; + #[cfg(not(windows))] + let virtual_label = "Peak sampled virtual bytes"; + if memory.samples != 0 { + writeln!( + report, + "Last target sampling attempt UTC: {}\nPeak sampled resident bytes: {}\n{virtual_label}: {}", + memory.last_at.as_deref().unwrap_or("unavailable"), + memory.resident_peak, + memory.virtual_peak + )?; + } + if let Some(available) = memory.available_min { + writeln!(report, "Minimum sampled system available RAM bytes: {available}\nSystem total RAM bytes at last sample: {}", memory.total)?; + } + writeln!( + report, + "Samples can miss short spikes or retain stale/zero readings when an OS query fails; per-field success is not exposed by sysinfo. Windows private commit is not GPU VRAM; on other platforms virtual memory is not physical RAM. System commit limit/usage and GPU memory budgets were not collected. No memory-pressure threshold establishes the cause of this incident.\nThe memory observer does not collect process command lines, environments, documents, native dumps or system event logs. The existing session-log tail below may contain paths or other logged details; review it before sharing." + ) + } +} + +#[cfg(any(windows, test))] +fn write_windows_status(report: &mut impl Write, code: u32) -> io::Result<()> { + let meaning = match code { + 0xc0000005 => "STATUS_ACCESS_VIOLATION", + 0xc0000017 => "STATUS_NO_MEMORY", + 0xc000012d => "STATUS_COMMITMENT_LIMIT", + 0xc0000374 => "STATUS_HEAP_CORRUPTION", + 0xc0000409 => "STATUS_STACK_BUFFER_OVERRUN / fast fail", + 0x40000015 => "STATUS_FATAL_APP_EXIT", + _ => "unclassified", + }; + writeln!( + report, + "Windows status bits: 0x{code:08X} ({meaning})\nThese are process exit bits, not a verified exception record. Software can set an exit code explicitly; an access violation does not identify the faulting module or prove a driver crash." + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn missing_target_without_exit_status_does_not_confirm_termination() { + let (send, exit) = mpsc::sync_channel(1); + drop(send); + let now = Instant::now(); + let observation = Observation { + pid: Pid::from_u32(1), + system: System::new(), + birth: Some(1), + started: now, + started_at: utc(), + detected_at: Some(utc()), + last_sample: now, + memory: Memory::default(), + exit, + status: None, + alive: Some(false), + }; + assert!(!observation.confirmed_exit()); + let mut report = Vec::new(); + observation.write(&mut report).unwrap(); + let report = String::from_utf8(report).unwrap(); + assert!(report.contains("not proof of termination")); + assert!(!report.contains("process termination observed")); + } + + #[test] + fn memory_high_water_marks_are_bounded_and_not_a_causal_diagnosis() { + let mut memory = Memory::default(); + memory.sample(40, 80, 30, 100); + memory.sample(20, 90, 50, 100); + assert_eq!((memory.samples, memory.resident_peak, memory.virtual_peak, memory.available_min), (2, 40, 90, Some(30))); + memory.sample(0, 0, 0, 0); + assert_eq!(memory.available_min, Some(30)); + } + + #[test] + fn windows_status_bits_preserve_unsigned_codes_without_claiming_a_driver_crash() { + for (code, name) in + [(0xc0000005, "STATUS_ACCESS_VIOLATION"), (0xc0000017, "STATUS_NO_MEMORY"), (0xc000012d, "STATUS_COMMITMENT_LIMIT"), (42, "unclassified")] + { + let mut text = Vec::new(); + write_windows_status(&mut text, code).unwrap(); + let text = String::from_utf8(text).unwrap(); + assert!(text.contains(name)); + assert!(text.contains(&format!("0x{code:08X}"))); + assert!(text.contains("not a verified exception record")); + } + } + + #[test] + fn utc_timestamps_are_parseable_and_zero_offset() { + let time = chrono::DateTime::parse_from_rfc3339(&utc()).unwrap(); + assert_eq!(time.offset().local_minus_utc(), 0); + } +} diff --git a/apps/effectcraft/src/crash_report.rs b/apps/effectcraft/src/crash_report.rs new file mode 100644 index 000000000..3080c12bb --- /dev/null +++ b/apps/effectcraft/src/crash_report.rs @@ -0,0 +1,803 @@ +//! A small, separate process reports an unexpected desktop exit (#3357). +//! +//! Allocation failure can abort without a panic hook. The GUI therefore owns a pipe to a +//! helper which never loads a project, font database or GPU. Only an explicit marker after +//! normal teardown suppresses the report. EOF alone identifies a lost control channel, not +//! a process exit or its cause. System-wide exhaustion can still prevent the helper or its native dialog working. + +use std::collections::VecDeque; +use std::ffi::{OsStr, OsString}; +use std::fs::{File, OpenOptions}; +use std::io::{self, Read, Seek, SeekFrom, Write}; +use std::path::{Path, PathBuf}; +use std::process::{Child, ChildStdin, Command, Stdio}; +use std::sync::mpsc::{self, RecvTimeoutError}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; + +#[path = "crash_observation.rs"] +mod observation; + +const FLAG: &str = "--internal-crash-reporter"; +const CLEAN: &[u8] = b"EFFECTCRAFT_CLEAN_EXIT_V1\n"; +const LOG_BYTES: usize = 64 * 1024; +const HEADER_BYTES: usize = 128; +const READY: &[u8] = b"READY\n"; +const EVENT_BYTES: usize = 128; +const EVENT_COUNT: usize = 32; + +/// Owned only by the GUI, never handed to another child. Dropping it without `finish` closes +/// the pipe and reports the incident; there is intentionally no clean-exit Drop hook. +pub struct Reporter { + pipe: ChildStdin, + child: Child, +} + +impl Reporter { + /// Call only after application teardown, including handled startup/device-loss errors. + pub fn finish(mut self) -> io::Result<()> { + let sent = self.pipe.write_all(CLEAN).and_then(|()| self.pipe.flush()); + drop(self.pipe); + // Do not let a suspended/broken helper hang shutdown. Reap it before the GUI + // exits where possible, so Windows updates do not race an old executable. + let cleanup = finish_child(&mut self.child, Duration::from_secs(2)); + match (sent, cleanup) { + (Err(sent), Err(cleanup)) => Err(io::Error::other(format!("clean marker failed: {sent}; cleanup failed: {cleanup}"))), + (Err(error), _) | (_, Err(error)) => Err(error), + (Ok(()), Ok(())) => Ok(()), + } + } +} + +fn finish_child(child: &mut Child, grace: Duration) -> io::Result<()> { + let start = Instant::now(); + while start.elapsed() < grace { + if let Some(status) = child.try_wait()? { + return if status.success() { Ok(()) } else { Err(io::Error::other(format!("crash reporter exited with {status}"))) }; + } + std::thread::sleep(Duration::from_millis(20)); + } + child.kill()?; + // Even after termination, do not introduce an unbounded wait on a stuck OS process. + let start = Instant::now(); + while start.elapsed() < Duration::from_secs(1) { + if child.try_wait()?.is_some() { + return Err(io::Error::other("crash reporter did not finish promptly and was terminated")); + } + std::thread::sleep(Duration::from_millis(20)); + } + Err(io::Error::other("crash reporter termination could not be confirmed")) +} + +/// Shared ownership lets macOS finish in on_exit, while main retains the startup-error path. +#[derive(Clone, Default)] +pub struct Shutdown(std::sync::Arc>>); + +impl Shutdown { + /// Install the one reporter created for this desktop launch. + pub fn install(&self, reporter: Reporter) -> io::Result<()> { + let mut slot = self.0.lock().map_err(|_| io::Error::other("crash reporter ownership was poisoned"))?; + *slot = Some(reporter); + Ok(()) + } + + /// Keep only fixed-vocabulary, bounded lifecycle facts outside the GUI. Callers emit each + /// category once; their combined payload fits well below the smallest OS pipe buffer. + pub fn record(&self, event: &'static str) -> io::Result<()> { + if event.len() > 100 || event.contains(['\n', '\r']) { + return Err(io::Error::other("invalid diagnostic observation")); + } + let mut slot = self.0.lock().map_err(|_| io::Error::other("crash reporter ownership was poisoned"))?; + if let Some(reporter) = slot.as_mut() { + writeln!(reporter.pipe, "EVENT {event}")?; + reporter.pipe.flush()?; + } + Ok(()) + } + + /// Consume the reporter exactly once, after the applicable platform teardown boundary. + pub fn finish(&self) -> io::Result<()> { + let reporter = self.0.lock().map_err(|_| io::Error::other("crash reporter ownership was poisoned"))?.take(); + reporter.map_or(Ok(()), Reporter::finish) + } +} + +/// A panic during teardown must leave the reporter armed. macOS terminates after this hook +/// without returning to main; other platforms pass None and finish after run_native returns. +pub fn after_teardown(reporter: Option<&Shutdown>, teardown: impl FnOnce()) -> io::Result<()> { + teardown(); + reporter.map_or(Ok(()), Shutdown::finish) +} + +/// Each process/run has its own report, including simultaneous launches. +pub fn run_id() -> String { + let nanos = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_nanos(); + format!("{}-{nanos}", std::process::id()) +} + +pub fn session_marker(id: &str) -> String { + format!("crash-report session {id}") +} + +/// Start before heavy work. A missing helper is a warning, never a reason to refuse the app. +pub fn start(id: &str, log: Option<&Path>, reports: &Path, interactive: bool) -> io::Result { + let exe = std::env::current_exe()?; + let args = [ + OsString::from(FLAG), + OsString::from(id), + log.map_or_else(OsString::new, |p| p.as_os_str().to_owned()), + reports.as_os_str().to_owned(), + OsString::from(if interactive { "1" } else { "0" }), + OsString::from(std::process::id().to_string()), + ]; + spawn(&exe, &args) +} + +fn spawn(exe: &Path, args: &[OsString]) -> io::Result { + let mut child = Command::new(exe).args(args).stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?; + if let Err(error) = ready(&mut child) { + let _ = child.kill(); + let _ = finish_child(&mut child, Duration::from_millis(100)); + return Err(error); + } + match child.stdin.take() { + Some(pipe) => Ok(Reporter { pipe, child }), + None => { + let _ = child.kill(); + let _ = child.wait(); + Err(io::Error::other("crash reporter has no control pipe")) + } + } +} + +/// A fixed acknowledgement means the helper holds the process observation before heavy GUI +/// work begins. A suspended helper cannot hold startup indefinitely. +fn ready(child: &mut Child) -> io::Result<()> { + let mut output = child.stdout.take().ok_or_else(|| io::Error::other("crash reporter has no readiness pipe"))?; + let (send, receive) = mpsc::sync_channel(1); + std::thread::Builder::new().name("ec-reporter-ready".into()).spawn(move || { + let mut bytes = [0; READY.len()]; + #[cfg(not(test))] + let result = + output.read_exact(&mut bytes).and_then(|()| if bytes == READY { Ok(()) } else { Err(io::Error::other("invalid reporter acknowledgement")) }); + // libtest prints its bounded banner before invoking the helper role. + #[cfg(test)] + let result = (|| { + for _ in 0..1024 { + bytes.rotate_left(1); + output.read_exact(&mut bytes[READY.len() - 1..])?; + if bytes == READY { + return Ok(()); + } + } + Err(io::Error::other("reporter acknowledgement missing")) + })(); + let _ = send.send(result); + // Keep the pipe open for libtest's trailing result (and future helper output), without + // storing it. Dropping it at READY would turn a clean helper exit into BrokenPipe. + let _ = io::copy(&mut output, &mut io::sink()); + })?; + receive.recv_timeout(Duration::from_secs(3)).map_err(|_| io::Error::other("crash reporter readiness timed out"))? +} + +/// Handle the internal mode before logging, argument parsing, updates or GUI initialization. +/// The callback is a native dialog on this helper's main thread, not on the failed GUI's. +pub fn maybe_run(args: &[OsString], show: impl FnOnce(&str)) -> bool { + if args.first().map(OsString::as_os_str) != Some(OsStr::new(FLAG)) { + return false; + } + let parsed = match args { + [_, id, log, reports, interactive, pid] => id + .to_str() + .filter(|id| valid_id(id)) + .zip(match interactive.to_str() { + Some("0") => Some(false), + Some("1") => Some(true), + _ => None, + }) + .zip(pid.to_str().and_then(|p| p.parse::().ok()).filter(|pid| *pid != 0)) + .map(|((id, interactive), pid)| (id, (!log.is_empty()).then(|| Path::new(log)), Path::new(reports), interactive, pid)), + _ => None, + }; + let Some((id, log, reports, interactive, pid)) = parsed else { + eprintln!("EffectCraft crash reporter: invalid internal arguments"); + return true; + }; + // Keep the exact file open while the GUI is alive: a later rotation must not substitute + // another instance's log. If rotation won the startup race, the session marker rejects it. + let mut log = SessionLog::open(log, id); + let mut observation = observation::Observation::start(pid); + // A GUI that dies during helper startup can already have closed this read end. Keep + // watching its control pipe and save the report rather than losing that early failure. + let _ = io::stdout().write_all(READY).and_then(|()| io::stdout().flush()); + // The reader has one bounded queue; the monitor never waits on a live pipe, so it can + // sample the target even when no log or lifecycle message is emitted. + let (send, receive) = mpsc::sync_channel(1); + let reader = std::thread::Builder::new().name("ec-reporter-pipe".into()).spawn(move || { + let mut input = io::stdin().lock(); + loop { + let message = read_message(&mut input); + let terminal = !matches!(message, Ok(Message::Event(_))); + if send.send(message).is_err() || terminal { + break; + } + } + }); + let mut events = VecDeque::with_capacity(EVENT_COUNT); + let failure = if reader.is_err() { + Some("control reader could not start".to_owned()) + } else { + loop { + observation.tick(); + match receive.recv_timeout(Duration::from_millis(250)) { + Ok(Ok(Message::Clean)) => return true, + Ok(Ok(Message::Event(event))) => { + if events.len() == EVENT_COUNT { + events.pop_front(); + } + events.push_back(format!("{} {event}", observation::utc())); + } + Ok(Ok(Message::Ended)) => break None, + Ok(Err(error)) => break Some(error.to_string()), + Err(RecvTimeoutError::Timeout) => continue, + Err(RecvTimeoutError::Disconnected) => break Some("control reader disconnected".to_owned()), + } + } + }; + observation.finish(); + let saved = write_report(reports, id, &mut log, failure.as_deref(), &observation, &events); + let description = description(&saved, observation.confirmed_exit()); + eprintln!("{description}"); + if interactive { + show(&description); + } + true +} + +fn valid_id(id: &str) -> bool { + !id.is_empty() && id.len() <= 80 && id.bytes().all(|c| c.is_ascii_digit() || c == b'-') +} + +enum Message { + Clean, + Event(String), + Ended, +} + +/// Every line and the retained event ring are bounded, including malformed control input. +fn read_message(input: &mut impl Read) -> io::Result { + let mut line = [0u8; EVENT_BYTES]; + for i in 0..line.len() { + let byte = line.get_mut(i).ok_or_else(|| io::Error::other("invalid control offset"))?; + loop { + match input.read(std::slice::from_mut(byte)) { + Ok(0) => return Ok(Message::Ended), + Ok(_) => break, + Err(error) if error.kind() == io::ErrorKind::Interrupted => continue, + Err(error) => return Err(error), + } + } + if *byte == b'\n' { + let text = line.get(..=i).ok_or_else(|| io::Error::other("invalid control line"))?; + if text == CLEAN { + return Ok(Message::Clean); + } + if let Some(event) = text.strip_prefix(b"EVENT ") { + let event = std::str::from_utf8(event).map_err(|_| io::Error::other("non-UTF8 diagnostic observation"))?; + return Ok(Message::Event(event.trim_end().to_owned())); + } + return Ok(Message::Ended); + } + } + Err(io::Error::other("control line exceeded its fixed limit")) +} + +#[cfg(test)] +fn clean_exit(input: &mut impl Read) -> io::Result { + Ok(matches!(read_message(input)?, Message::Clean)) +} + +enum SessionLog { + Captured { file: File, marker: String }, + Unavailable(String), +} + +impl SessionLog { + fn open(path: Option<&Path>, id: &str) -> Self { + let opened = (|| { + let path = path.ok_or_else(|| io::Error::other("no session log was opened"))?; + let mut file = File::open(path)?; + let mut header = [0u8; HEADER_BYTES]; + let marker = format!("{}\n", session_marker(id)); + let prefix = header.get_mut(..marker.len()).ok_or_else(|| io::Error::other("session marker too long"))?; + file.read_exact(prefix)?; + if prefix != marker.as_bytes() { + return Err(io::Error::other("matching session marker unavailable; the log may have rotated")); + } + Ok(file) + })(); + match opened { + Ok(file) => Self::Captured { file, marker: format!("{}\n", session_marker(id)) }, + Err(e) => Self::Unavailable(e.to_string()), + } + } + + fn write_tail(&mut self, report: &mut File) -> io::Result<()> { + match self { + Self::Unavailable(reason) => writeln!(report, "Recent log unavailable: {reason}"), + Self::Captured { file, marker } => { + // An older concurrent launcher may have truncated the captured file. + // Recheck its identity before copying any content into this report. + file.seek(SeekFrom::Start(0))?; + let mut header = [0u8; HEADER_BYTES]; + let prefix = header.get_mut(..marker.len()).ok_or_else(|| io::Error::other("session marker too long"))?; + if file.read_exact(prefix).is_err() || prefix != marker.as_bytes() { + return writeln!(report, "Recent log unavailable: session identity changed"); + } + let size = file.metadata()?.len(); + file.seek(SeekFrom::Start(size.saturating_sub(LOG_BYTES as u64)))?; + writeln!(report, "\nRecorded session log tail (last {LOG_BYTES} bytes at most; logging filters/size caps may omit later events):")?; + io::copy(&mut file.take(LOG_BYTES as u64), report)?; + Ok(()) + } + } + } +} + +fn write_report( + reports: &Path, + id: &str, + log: &mut SessionLog, + pipe_error: Option<&str>, + observation: &observation::Observation, + events: &VecDeque, +) -> io::Result { + if !valid_id(id) { + return Err(io::Error::other("invalid report identity")); + } + std::fs::create_dir_all(reports)?; + let path = reports.join(format!("crash-{id}.txt")); + let mut report = OpenOptions::new().write(true).create_new(true).open(&path)?; + writeln!( + report, + "EffectCraft unexpected-exit diagnostic\nVersion: {}\nPlatform: {} / {}\nSession: {id}", + env!("CARGO_PKG_VERSION"), + std::env::consts::OS, + std::env::consts::ARCH + )?; + writeln!( + report, + "The GUI control channel ended without its clean-exit marker.\nThe exact cause is not established. GPU fallback, memory pressure and process exit are separate observations, not a proven causal chain.\nThis report was saved locally; it was not sent anywhere." + )?; + observation.write(&mut report)?; + writeln!(report, "\nRetained lifecycle observations (last {EVENT_COUNT}; UTC is helper receipt time):")?; + for event in events { + writeln!(report, "{event}")?; + } + if let Some(error) = pipe_error { + writeln!(report, "Control pipe error: {error}")?; + } + if let Err(error) = log.write_tail(&mut report) { + writeln!(report, "\nCould not read the recent session log: {error}")?; + } + report.flush()?; + Ok(path) +} + +fn description(saved: &io::Result, confirmed_exit: bool) -> String { + let diagnostic = match saved { + Ok(path) => format!("A diagnostic report was saved to:\n{}", path.display()), + Err(error) => format!("A diagnostic report could not be saved: {error}"), + }; + let incident = + if confirmed_exit { "EffectCraft stopped unexpectedly." } else { "EffectCraft lost its diagnostic control channel; process exit is not confirmed." }; + format!( + "{incident}\n\n{diagnostic}\n\nThe exact cause is unknown. If EffectCraft has closed, reopen it to check for an existing auto-save. Changes since the last auto-save may be lost.\n\nNo report has been sent anywhere." + ) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + use std::thread; + use std::time::{Duration, Instant}; + + static NEXT: AtomicUsize = AtomicUsize::new(0); + const ROLE: &str = "EFFECTCRAFT_CRASH_REPORT_TEST_ROLE"; + + fn test_report(dir: &Path, id: &str, log: &mut SessionLog) -> io::Result { + write_report(dir, id, log, None, &observation::Observation::start(std::process::id()), &VecDeque::new()) + } + + fn folder() -> PathBuf { + let p = std::env::temp_dir().join(format!("ec-crash-report-{}-{}", run_id(), NEXT.fetch_add(1, Ordering::Relaxed))); + std::fs::create_dir_all(&p).unwrap(); + p + } + + fn test_name(name: &str) -> String { + // Also usable from an isolated harness with the reporter dependencies. + let prefix = module_path!().split_once("::").map_or("", |(_, rest)| rest); + format!("{prefix}::{name}") + } + + fn helper(dir: &Path, id: &str) -> Reporter { + helper_mode(dir, id, false) + } + + fn helper_mode(dir: &Path, id: &str, interactive: bool) -> Reporter { + let _spawn_lock = crate::test_child_spawn_lock(); + let mut child = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", &test_name("reporter_process"), "--ignored", "--nocapture"]) + .env(ROLE, "reporter") + .env("EC_REPORT_DIR", dir) + .env("EC_REPORT_ID", id) + .env("EC_REPORT_INTERACTIVE", if interactive { "1" } else { "0" }) + .env("EC_REPORT_PID", std::process::id().to_string()) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); + ready(&mut child).unwrap(); + Reporter { pipe: child.stdin.take().unwrap(), child } + } + + fn wait_file(path: &Path) { + let until = Instant::now() + Duration::from_secs(10); + while !path.is_file() && Instant::now() < until { + thread::sleep(Duration::from_millis(20)); + } + assert!(path.is_file(), "report did not arrive: {}", path.display()); + } + + #[test] + fn bounded_protocol_rejects_eof_and_partial_or_wrong_markers() { + let mut clean = CLEAN; + assert!(clean_exit(&mut clean).unwrap()); + for mut input in [b"".as_slice(), &CLEAN[..CLEAN.len() - 1], b"clean", b"EFFECTCRAFT_CLEAN_EXIT_V0\n"] { + assert!(!clean_exit(&mut input).unwrap()); + } + assert!(clean_exit(&mut &vec![b'x'; 100_000][..]).is_err()); + } + + #[test] + fn report_is_bounded_and_keeps_its_session_after_log_rotation() { + let dir = folder(); + let original = dir.join("effectcraft.log"); + let id = "123-456"; + std::fs::write(&original, format!("{}\n", session_marker(id))).unwrap(); + let mut log = SessionLog::open(Some(&original), id); + std::fs::rename(&original, dir.join("effectcraft.1.log")).unwrap(); + std::fs::write(&original, "OTHER SESSION MUST NOT APPEAR").unwrap(); + let mut old = OpenOptions::new().append(true).open(dir.join("effectcraft.1.log")).unwrap(); + old.write_all(&vec![b'a'; LOG_BYTES * 2]).unwrap(); + old.write_all(b"\nGPU context retired\n").unwrap(); + let report = test_report(&dir, id, &mut log).unwrap(); + let text = std::fs::read_to_string(report).unwrap(); + assert!(text.len() < LOG_BYTES + 4096); + assert!(text.contains("GPU context retired")); + assert!(!text.contains("OTHER SESSION")); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn wrong_session_and_missing_logs_are_reported_without_reading_other_content() { + let dir = folder(); + let path = dir.join("log"); + std::fs::write(&path, "private other session").unwrap(); + for (id, source) in [("1-1", Some(path.as_path())), ("1-2", None)] { + let report = test_report(&dir, id, &mut SessionLog::open(source, id)).unwrap(); + let text = std::fs::read_to_string(report).unwrap(); + assert!(text.contains("Recent log unavailable")); + assert!(!text.contains("private other session")); + } + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn captured_log_rejects_a_session_overwritten_after_open() { + let dir = folder(); + let path = dir.join("log"); + std::fs::write(&path, "crash-report session 1-2\noriginal session\n").unwrap(); + let mut log = SessionLog::open(Some(&path), "1-2"); + std::fs::write(&path, "crash-report session 3-4\nprivate other session\n").unwrap(); + let report = test_report(&dir, "1-2", &mut log).unwrap(); + let text = std::fs::read_to_string(report).unwrap(); + assert!(text.contains("Recent log unavailable")); + assert!(!text.contains("private other session")); + assert!(!text.contains("session 3-4")); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn session_identity_requires_the_exact_first_line() { + let dir = folder(); + let path = dir.join("log"); + for header in ["crash-report session 1-20\n", "unrelated\ncrash-report session 1-2\n"] { + std::fs::write(&path, header).unwrap(); + assert!(matches!(SessionLog::open(Some(&path), "1-2"), SessionLog::Unavailable(_))); + } + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn report_write_failure_is_described_and_existing_report_is_not_overwritten() { + let dir = folder(); + let mut log = SessionLog::Unavailable("no log".into()); + let first = test_report(&dir, "2-1", &mut log).unwrap(); + let before = std::fs::read(&first).unwrap(); + let result = test_report(&dir, "2-1", &mut log); + assert!(result.is_err()); + assert!(description(&result, false).contains("could not be saved")); + assert_eq!(std::fs::read(first).unwrap(), before); + assert!(test_report(&dir, "../escape", &mut log).is_err()); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn normal_and_handled_shutdown_send_clean_marker_without_a_report() { + for id in ["3-1", "3-2"] { + let dir = folder(); + let r = helper(&dir, id); + r.finish().unwrap(); + // A completion file proves the helper consumed the marker, not merely that it + // hadn't yet produced a report. + wait_file(&dir.join(format!("ready-{id}"))); + assert!(!dir.join(format!("crash-{id}.txt")).exists()); + std::fs::remove_dir_all(dir).unwrap(); + } + } + + #[test] + fn accepted_native_close_finishes_before_process_termination() { + let dir = folder(); + let _spawn_lock = crate::test_child_spawn_lock(); + let status = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", &test_name("aborting_parent"), "--ignored", "--nocapture"]) + .env(ROLE, "native-close") + .env("EC_REPORT_DIR", &dir) + .stdout(Stdio::null()) + .status() + .unwrap(); + assert!(status.success()); + wait_file(&dir.join("ready-8-1")); + assert!(!dir.join("crash-8-1.txt").exists()); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn a_teardown_panic_does_not_mark_the_reporter_clean() { + let dir = folder(); + let reporter = Shutdown::default(); + reporter.install(helper(&dir, "8-2")).unwrap(); + let result = std::panic::catch_unwind(|| { + let _ = after_teardown(Some(&reporter), || panic!("simulated teardown failure")); + }); + assert!(result.is_err()); + drop(reporter); + wait_file(&dir.join("ready-8-2")); + assert!(dir.join("crash-8-2.txt").exists()); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn platforms_returning_to_main_keep_the_reporter_armed_until_then() { + let dir = folder(); + let reporter = Shutdown::default(); + reporter.install(helper(&dir, "8-3")).unwrap(); + after_teardown(None, || {}).unwrap(); + assert!(reporter.0.lock().unwrap().is_some()); + reporter.clone().finish().unwrap(); + reporter.finish().unwrap(); + wait_file(&dir.join("ready-8-3")); + assert!(!dir.join("crash-8-3.txt").exists()); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn an_aborting_parent_leaves_one_diagnostic_report() { + let dir = folder(); + let _spawn_lock = crate::test_child_spawn_lock(); + let status = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", &test_name("aborting_parent"), "--ignored", "--nocapture"]) + .env(ROLE, "abort") + .env("EC_REPORT_DIR", &dir) + .stdout(Stdio::null()) + .status() + .unwrap(); + assert!(!status.success()); + wait_file(&dir.join("ready-4-1")); + let text = std::fs::read_to_string(dir.join("crash-4-1.txt")).unwrap(); + assert!(text.contains("exact cause is not established")); + assert!(text.contains("Detection UTC:")); + #[cfg(windows)] + assert!(text.contains("Windows status bits:")); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn an_unrelated_child_does_not_keep_the_control_pipe_open() { + let dir = folder(); + let r = helper(&dir, "5-1"); + let _spawn_lock = crate::test_child_spawn_lock(); + let mut unrelated = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", &test_name("lingering_child"), "--ignored", "--nocapture"]) + .env(ROLE, "linger") + .stdout(Stdio::null()) + .spawn() + .unwrap(); + drop(r); + wait_file(&dir.join("ready-5-1")); + assert!(unrelated.try_wait().unwrap().is_none()); + unrelated.kill().unwrap(); + unrelated.wait().unwrap(); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn an_unresponsive_helper_cannot_hang_clean_shutdown() { + let _spawn_lock = crate::test_child_spawn_lock(); + let mut child = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", &test_name("lingering_child"), "--ignored", "--nocapture"]) + .env(ROLE, "linger") + .stdin(Stdio::piped()) + .stdout(Stdio::null()) + .spawn() + .unwrap(); + let pipe = child.stdin.take().unwrap(); + let started = Instant::now(); + let result = Reporter { pipe, child }.finish(); + assert!(result.unwrap_err().to_string().contains("was terminated")); + assert!(started.elapsed() < Duration::from_secs(5)); + } + + #[test] + fn simultaneous_sessions_write_separate_reports() { + let dir = folder(); + let a = helper(&dir, "6-1"); + let b = helper(&dir, "6-2"); + drop(a); + drop(b); + wait_file(&dir.join("ready-6-1")); + wait_file(&dir.join("ready-6-2")); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn interactive_failure_calls_dialog_once_after_saving_the_report() { + let dir = folder(); + drop(helper_mode(&dir, "7-1", true)); + wait_file(&dir.join("ready-7-1")); + let text = std::fs::read_to_string(dir.join("dialog-7-1")).unwrap(); + assert!(text.contains("crash-7-1.txt")); + assert!(text.contains("No report has been sent")); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn lost_pipe_keeps_bounded_lifecycle_evidence_without_claiming_process_exit() { + let dir = folder(); + let reporter = Shutdown::default(); + reporter.install(helper(&dir, "9-1")).unwrap(); + for _ in 0..40 { + reporter.record("GPU observation: wgpu validation error").unwrap(); + } + drop(reporter); + wait_file(&dir.join("ready-9-1")); + let text = std::fs::read_to_string(dir.join("crash-9-1.txt")).unwrap(); + assert!(text.contains("GUI still present")); + assert!(!text.contains("process termination observed")); + assert_eq!(text.matches("GPU observation: wgpu validation error").count(), EVENT_COUNT); + assert!(text.contains("Target-present sampling attempts:")); + assert!(text.len() < 8192); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn clean_exit_after_lifecycle_events_does_not_write_a_report() { + let dir = folder(); + let reporter = Shutdown::default(); + reporter.install(helper(&dir, "9-2")).unwrap(); + reporter.record("Backend selection source: default").unwrap(); + reporter.finish().unwrap(); + wait_file(&dir.join("ready-9-2")); + assert!(!dir.join("crash-9-2.txt").exists()); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + #[cfg(windows)] + fn windows_report_preserves_real_unsigned_exit_bits() { + let dir = folder(); + let _spawn_lock = crate::test_child_spawn_lock(); + let status = Command::new(std::env::current_exe().unwrap()) + .args(["--exact", &test_name("aborting_parent"), "--ignored", "--nocapture"]) + .env(ROLE, "windows-status") + .env("EC_REPORT_DIR", &dir) + .stdout(Stdio::null()) + .status() + .unwrap(); + assert_eq!(status.code().map(|code| code as u32), Some(0xc0000017)); + wait_file(&dir.join("ready-9-3")); + let text = std::fs::read_to_string(dir.join("crash-9-3.txt")).unwrap(); + assert!(text.contains("Windows status bits: 0xC0000017 (STATUS_NO_MEMORY)")); + assert!(text.contains("not a verified exception record")); + assert!(text.contains("process termination observed")); + std::fs::remove_dir_all(dir).unwrap(); + } + + #[test] + fn internal_mode_rejects_invalid_arguments_without_launching_the_app() { + assert!(!maybe_run(&[], |_| panic!("no dialog"))); + assert!(!maybe_run(&[OsString::from("--version")], |_| panic!("no dialog"))); + assert!(maybe_run(&[OsString::from(FLAG)], |_| panic!("no dialog"))); + let args = [FLAG, "../escape", "", "", "1", "1"].map(OsString::from); + assert!(maybe_run(&args, |_| panic!("no dialog"))); + let args = [FLAG, "1-1", "", "", "invalid", "1"].map(OsString::from); + assert!(maybe_run(&args, |_| panic!("no dialog"))); + for pid in ["0", "not-a-pid", "4294967296"] { + let args = [FLAG, "1-1", "", "", "0", pid].map(OsString::from); + assert!(maybe_run(&args, |_| panic!("no dialog"))); + } + } + + #[test] + fn missing_helper_is_an_error_not_a_panic() { + assert!(spawn(Path::new("/nonexistent-effectcraft-crash-helper"), &[]).is_err()); + } + + // Roles are ignored by ordinary test runs. Explicit subprocess invocations exercise + // real EOF/abort behavior without allocating a huge image or killing the test runner. + #[test] + #[ignore] + fn reporter_process() { + assert_eq!(std::env::var(ROLE).unwrap(), "reporter"); + let dir = PathBuf::from(std::env::var_os("EC_REPORT_DIR").unwrap()); + let id = std::env::var("EC_REPORT_ID").unwrap(); + let interactive = std::env::var("EC_REPORT_INTERACTIVE").unwrap(); + let args = [ + OsString::from(FLAG), + OsString::from(&id), + OsString::new(), + dir.as_os_str().to_owned(), + OsString::from(&interactive), + std::env::var_os("EC_REPORT_PID").unwrap(), + ]; + assert!(maybe_run(&args, |description| { + assert_eq!(interactive, "1", "non-interactive reporter must not show a dialog"); + assert!(dir.join(format!("crash-{id}.txt")).exists()); + let mut shown = OpenOptions::new().write(true).create_new(true).open(dir.join(format!("dialog-{id}"))).unwrap(); + shown.write_all(description.as_bytes()).unwrap(); + })); + std::fs::write(dir.join(format!("ready-{id}")), b"done").unwrap(); + } + + #[test] + #[ignore] + fn aborting_parent() { + let role = std::env::var(ROLE).unwrap(); + let dir = PathBuf::from(std::env::var_os("EC_REPORT_DIR").unwrap()); + if role == "native-close" { + let reporter = Shutdown::default(); + reporter.install(helper(&dir, "8-1")).unwrap(); + after_teardown(Some(&reporter.clone()), || {}).unwrap(); + std::process::exit(0); // Models macOS not returning from its native event loop. + } + if role == "windows-status" { + let _reporter = helper(&dir, "9-3"); + // The real OS status is intentionally set, not actual memory exhaustion. The + // report must preserve this distinction rather than claiming a verified exception. + std::process::exit(0xc0000017u32 as i32); + } + assert_eq!(role, "abort"); + let _reporter = helper(&dir, "4-1"); + std::process::abort(); + } + + #[test] + #[ignore] + fn lingering_child() { + assert_eq!(std::env::var(ROLE).unwrap(), "linger"); + thread::sleep(Duration::from_secs(30)); + } +} diff --git a/apps/effectcraft/src/diagnostic_state.rs b/apps/effectcraft/src/diagnostic_state.rs new file mode 100644 index 000000000..042e6e88c --- /dev/null +++ b/apps/effectcraft/src/diagnostic_state.rs @@ -0,0 +1,147 @@ +//! Fixed-vocabulary observations retained by the crash helper independently of the log tail. +//! Driver messages and environment text can contain private data; never forward either here. + +use std::ffi::OsStr; +use std::sync::Arc; +use std::sync::atomic::{AtomicU8, Ordering}; + +use crate::crash_report::Shutdown; + +pub fn record(reporter: &Shutdown, event: &'static str) { + log::info!("diagnostic observation: {event}"); + if let Err(error) = reporter.record(event) { + log::warn!("could not retain diagnostic observation: {error}"); + } +} + +/// Mirror only wgpu's documented names/aliases, without calling its parser: that parser +/// logs unknown tokens verbatim. Unknown, non-Unicode and oversized values remain private. +pub fn backend_override_events(value: &OsStr) -> Vec<&'static str> { + let Some(value) = value.to_str().filter(|value| value.len() <= 4096) else { + return vec!["WGPU_BACKEND: non-Unicode or oversized value omitted"]; + }; + let mut selected = [false; 6]; + let mut unknown = false; + for token in value.split(',').map(str::trim) { + let aliases: &[&[&str]] = &[&["vulkan", "vk"], &["dx12", "d3d12"], &["metal", "mtl"], &["opengl", "gles", "gl"], &["webgpu"], &["noop"]]; + if let Some(index) = aliases.iter().position(|names| names.iter().any(|name| token.eq_ignore_ascii_case(name))) { + if let Some(flag) = selected.get_mut(index) { + *flag = true; + } + } else { + unknown = true; + } + } + let events = [ + "WGPU_BACKEND requests: vulkan", + "WGPU_BACKEND requests: dx12", + "WGPU_BACKEND requests: metal", + "WGPU_BACKEND requests: gl", + "WGPU_BACKEND requests: webgpu", + "WGPU_BACKEND requests: noop", + ]; + let mut result: Vec<_> = selected.into_iter().zip(events).filter_map(|(present, event)| present.then_some(event)).collect(); + if unknown { + result.push("WGPU_BACKEND: unrecognized or empty tokens omitted"); + } + result +} + +/// At most eight events per device, even when a driver floods its callbacks. A wgpu OOM +/// observation is not proof of system RAM exhaustion; device loss is not a driver-crash verdict. +#[derive(Clone)] +pub struct GpuObservations { + seen: Arc, + reporter: Shutdown, +} + +impl GpuObservations { + pub fn new(reporter: &Shutdown) -> Self { + Self { seen: Arc::new(AtomicU8::new(0)), reporter: reporter.clone() } + } + + pub fn uncaptured(&self, error: &eframe::wgpu::Error) { + let (bit, event) = match error { + eframe::wgpu::Error::OutOfMemory { .. } => (1, "GPU observation: wgpu out-of-memory error (not proof of system RAM exhaustion)"), + eframe::wgpu::Error::Validation { .. } => (2, "GPU observation: wgpu validation error"), + eframe::wgpu::Error::Internal { .. } => (4, "GPU observation: wgpu internal error"), + }; + self.once(bit, event); + } + + pub fn scoped(&self, error: effectcraft_ui_egui::gpu_failure::ScopedError) { + use effectcraft_ui_egui::gpu_failure::ScopedError; + let (bit, event) = match error { + ScopedError::OutOfMemory => (32, "GPU scoped observation: out-of-memory (not proof of system RAM exhaustion)"), + ScopedError::Internal => (64, "GPU scoped observation: internal error (not confirmed out-of-memory)"), + ScopedError::Validation => (128, "GPU scoped observation: validation error"), + }; + self.once(bit, event); + } + + pub fn device_lost(&self, reason: eframe::wgpu::DeviceLostReason) { + match reason { + eframe::wgpu::DeviceLostReason::Unknown => self.once(8, "GPU observation: device lost, reason unknown (not proof of a driver crash)"), + eframe::wgpu::DeviceLostReason::Destroyed => self.once(16, "GPU observation: device-lost callback, explicit device destruction"), + }; + } + + fn once(&self, bit: u8, event: &'static str) -> bool { + if self.seen.fetch_or(bit, Ordering::Relaxed) & bit != 0 { + return false; + } + record(&self.reporter, event); + true + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn override_names_are_normalized_deduplicated_and_private() { + let events = backend_override_events(OsStr::new(" Vk, VULKAN, d3d12, MTL, gles, webgpu, noop, private-token")); + assert_eq!(events.len(), 7); + assert_eq!(events.first(), Some(&"WGPU_BACKEND requests: vulkan")); + assert!(!events.join(" ").contains("private-token")); + assert!(events.iter().all(|event| event.len() <= 100)); + assert_eq!(backend_override_events(OsStr::new("")).len(), 1); + assert_eq!(backend_override_events(OsStr::new(&"x".repeat(4097))), ["WGPU_BACKEND: non-Unicode or oversized value omitted"]); + } + + #[test] + fn gpu_categories_are_latched_independently() { + let observations = GpuObservations::new(&Shutdown::default()); + observations.uncaptured(&eframe::wgpu::Error::OutOfMemory { source: Box::new(std::io::Error::other("private driver detail")) }); + observations.device_lost(eframe::wgpu::DeviceLostReason::Unknown); + observations.device_lost(eframe::wgpu::DeviceLostReason::Unknown); + assert_eq!(observations.seen.load(Ordering::Relaxed), 9); + observations.device_lost(eframe::wgpu::DeviceLostReason::Destroyed); + observations.uncaptured(&eframe::wgpu::Error::Validation { + source: Box::new(std::io::Error::other("private driver detail")), + description: "private validation detail".into(), + }); + observations.uncaptured(&eframe::wgpu::Error::Internal { + source: Box::new(std::io::Error::other("private driver detail")), + description: "private internal detail".into(), + }); + assert_eq!(observations.seen.load(Ordering::Relaxed), 31); + use effectcraft_ui_egui::gpu_failure::ScopedError; + observations.scoped(ScopedError::Internal); + assert_eq!(observations.seen.load(Ordering::Relaxed), 95, "internal scope must not set scoped OOM bit"); + observations.scoped(ScopedError::OutOfMemory); + observations.scoped(ScopedError::Validation); + observations.scoped(ScopedError::OutOfMemory); + assert_eq!(observations.seen.load(Ordering::Relaxed), 255); + assert!(!observations.once(1, "GPU observation: duplicate")); + assert!(!observations.once(8, "GPU observation: duplicate")); + } + + #[test] + #[cfg(unix)] + fn non_unicode_override_is_not_copied() { + use std::os::unix::ffi::OsStrExt; + assert_eq!(backend_override_events(OsStr::from_bytes(b"private\xff")), ["WGPU_BACKEND: non-Unicode or oversized value omitted"]); + } +} diff --git a/apps/effectcraft/src/launch_guard.rs b/apps/effectcraft/src/launch_guard.rs index fac91c05b..85d1fdd57 100644 --- a/apps/effectcraft/src/launch_guard.rs +++ b/apps/effectcraft/src/launch_guard.rs @@ -8,9 +8,9 @@ //! until its window has kept drawing for [`SETTLE`] (or the app quits normally before then). A //! launch that finds the previous one's marker switches Window Graphics to OpenGL, which those //! drivers run, and says so once the window is up. `WGPU_BACKEND` (wgpu's own override) still -//! wins over both. The marker is locked while its launch runs, so a second copy opened meanwhile -//! isn't taken for a crash; and a window OpenGL can't open either puts Window Graphics back to -//! Automatic ([`opengl_failed`]) rather than failing every launch. +//! wins over both. A separate guard file is locked while its launch runs, so a second copy opened +//! meanwhile isn't taken for a crash; and a window OpenGL can't open either puts Window Graphics +//! back to Automatic ([`opengl_failed`]) rather than failing every launch. use std::fs::File; use std::path::{Path, PathBuf}; @@ -21,6 +21,46 @@ use eframe::wgpu::Backends; /// Left in the settings folder from a launch until its window has settled. const MARKER: &str = "launch-pending"; +const LOCK: &str = "launch.lock"; + +/// Keep this file in place: unlinking a locked file lets a second inode acquire a new lock. +fn acquire_guard(dir: &Path) -> Option { + let file = match std::fs::create_dir_all(dir) + .and_then(|()| std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).open(dir.join(LOCK))) + { + Ok(file) => file, + Err(e) => { + log::warn!("launch guard unavailable: {e}"); + return None; + } + }; + match file.try_lock() { + Ok(()) => {} + Err(std::fs::TryLockError::WouldBlock) => return None, + Err(e) => { + log::warn!("launch guard lock unavailable: {e}"); + return None; + } + } + // Older versions lock the marker itself. Probe only the lock: Windows also prevents + // reads through another handle while it is held. An unreadable marker is not a stale one. + match File::open(dir.join(MARKER)) { + Ok(marker) => match marker.try_lock() { + Ok(()) => {} + Err(std::fs::TryLockError::WouldBlock) => return None, + Err(e) => { + log::warn!("launch marker lock unavailable: {e}"); + return None; + } + }, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => { + log::warn!("launch marker unavailable: {e}"); + return None; + } + } + Some(file) +} /// How long the window keeps drawing after its first frame before the launch counts as made. /// A driver that fails as the window is shown and its surface re-created does so within the @@ -28,16 +68,39 @@ const MARKER: &str = "launch-pending"; /// switch back in Settings. pub const SETTLE: Duration = Duration::from_secs(3); +/// Why this launch requested its window graphics APIs. An interrupted startup is an +/// observation, not proof that a driver caused the previous exit. +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub enum BackendSource { + #[default] + Default, + SavedPreference, + InterruptedStartup, + EnvironmentOverride, +} + +impl BackendSource { + pub fn event(self) -> &'static str { + match self { + Self::Default => "backend source: default", + Self::SavedPreference => "backend source: saved OpenGL preference", + Self::InterruptedStartup => "backend source: previous interrupted startup; OpenGL fallback", + Self::EnvironmentOverride => "backend source: WGPU_BACKEND present", + } + } +} + /// This launch's window graphics. #[derive(Debug, Default)] pub struct Launch { /// The graphics APIs the window may use (`None`: eframe's default). pub backends: Option, + pub source: BackendSource, /// Switched to OpenGL because the previous launch stopped as its window opened: the message /// to show. pub notice: Option, marker: Option, - /// Holds the marker's lock until the launch settles (or the process ends). + /// Holds the separate guard file's lock until the launch settles (or the process ends). lock: Option, /// When the window drew its first frame, until the launch settles. first_frame: Option, @@ -48,14 +111,22 @@ impl Launch { /// launch stopped as its window opened (where OpenGL exists: not macOS), and leave this /// launch's marker. `env_override`: `WGPU_BACKEND` is set. pub fn begin(dir: Option<&Path>, env_override: bool) -> Launch { - let Some(dir) = dir.filter(|_| !env_override) else { return Launch::default() }; + if env_override { + return Launch { source: BackendSource::EnvironmentOverride, ..Launch::default() }; + } + let Some(dir) = dir else { return Launch::default() }; let prefs_path = dir.join(PREFS_FILE); let mut prefs = std::fs::read_to_string(&prefs_path).map(|t| Prefs::from_json(&t)).unwrap_or_default(); let marker = dir.join(MARKER); - // Locked by another launch that is still opening its window: not a crash. - let held = File::open(&marker).is_ok_and(|f| matches!(f.try_lock(), Err(std::fs::TryLockError::WouldBlock))); + let Some(lock) = acquire_guard(dir) else { + let backends = (prefs.startup.window_graphics == "gl").then_some(Backends::GL); + let source = if backends.is_some() { BackendSource::SavedPreference } else { BackendSource::Default }; + return Launch { backends, source, ..Launch::default() }; + }; + // Read again while owning the guard: another launch may have just changed preferences. + prefs = std::fs::read_to_string(&prefs_path).map(|t| Prefs::from_json(&t)).unwrap_or_default(); let mut notice = None; - if marker.exists() && !held && prefs.startup.window_graphics == "auto" && cfg!(not(target_os = "macos")) { + if marker.exists() && prefs.startup.window_graphics == "auto" && cfg!(not(target_os = "macos")) { prefs.startup.window_graphics = "gl".into(); match std::fs::create_dir_all(dir).and_then(|()| std::fs::write(&prefs_path, prefs.to_json())) { Ok(()) => log::warn!("the last launch stopped as its window opened: switching Window Graphics to OpenGL"), @@ -69,15 +140,21 @@ impl Launch { ); } let backends = (prefs.startup.window_graphics == "gl").then_some(Backends::GL); - let (marker, lock) = match std::fs::create_dir_all(dir).and_then(|()| File::create(&marker)) { - // Where locks aren't available the guard works as before. - Ok(f) => (Some(marker), f.try_lock().is_ok().then_some(f)), + let source = if notice.is_some() { + BackendSource::InterruptedStartup + } else if backends.is_some() { + BackendSource::SavedPreference + } else { + BackendSource::Default + }; + let marker = match File::create(&marker) { + Ok(_) => Some(marker), Err(e) => { log::warn!("launch marker: {e}"); - (None, None) + None } }; - Launch { backends, notice, marker, lock, first_frame: None } + Launch { backends, source, notice, marker, lock: Some(lock), first_frame: None } } /// The window drew a frame. Until the launch has settled, returns how long that still takes @@ -104,18 +181,19 @@ impl Launch { /// The launch made it: release and remove its marker. fn settled(&mut self) { - self.lock = None; if let Some(m) = self.marker.take() && let Err(e) = std::fs::remove_file(&m) { log::warn!("launch marker: {e}"); } + self.lock = None; } } /// The window couldn't open with OpenGL (an error, not a crash): back to Automatic, and no marker. pub fn opengl_failed(dir: Option<&Path>) { let Some(dir) = dir else { return }; + let Some(_lock) = acquire_guard(dir) else { return }; let prefs_path = dir.join(PREFS_FILE); let mut prefs = std::fs::read_to_string(&prefs_path).map(|t| Prefs::from_json(&t)).unwrap_or_default(); prefs.startup.window_graphics = "auto".into(); @@ -136,12 +214,13 @@ mod tests { /// A launch that never drew switches the next one to OpenGL, which then stays (#243). #[test] fn a_launch_that_never_drew_switches_the_next_to_opengl() { - let dir = std::env::temp_dir().join(format!("ec-launch-guard-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("never-drew"); // A first launch that draws: nothing changes. Its first frames don't settle it (a driver // may still crash as the window is shown); frames for `SETTLE` do. let mut l = Launch::begin(Some(&dir), false); assert_eq!((l.backends, l.notice.is_some()), (None, false)); + assert_eq!(l.source, BackendSource::Default); assert!(dir.join(MARKER).exists()); let t0 = Instant::now(); assert_eq!(l.frame_drawn_at(t0), Some(SETTLE)); @@ -159,14 +238,18 @@ mod tests { } else { assert_eq!(l.backends, Some(Backends::GL)); assert!(l.notice.is_some()); + assert_eq!(l.source, BackendSource::InterruptedStartup); assert_eq!(window_graphics(&dir), "gl", "saved in Settings"); l.exited(); let l = Launch::begin(Some(&dir), false); assert_eq!((l.backends, l.notice.is_some()), (Some(Backends::GL), false), "it stays, without the notice"); + assert_eq!(l.source, BackendSource::SavedPreference); } // `WGPU_BACKEND` wins, and leaves no marker. let _ = std::fs::remove_file(dir.join(MARKER)); - assert_eq!(Launch::begin(Some(&dir), true).backends, None); + let overridden = Launch::begin(Some(&dir), true); + assert_eq!(overridden.backends, None); + assert_eq!(overridden.source, BackendSource::EnvironmentOverride); assert!(!dir.join(MARKER).exists()); let _ = std::fs::remove_dir_all(&dir); } @@ -176,8 +259,8 @@ mod tests { /// normal quit in that time doesn't. #[test] fn a_launch_that_stops_after_its_first_frame_switches_the_next_to_opengl() { - let dir = std::env::temp_dir().join(format!("ec-launch-guard-3-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("first-frame"); let mut l = Launch::begin(Some(&dir), false); assert!(l.frame_drawn().is_some()); drop(l); @@ -203,8 +286,8 @@ mod tests { /// open a window either goes back to Automatic instead of failing every launch. #[test] fn a_second_copy_or_failed_opengl_doesnt_strand_window_graphics() { - let dir = std::env::temp_dir().join(format!("ec-launch-guard-2-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("second-copy"); let _first = Launch::begin(Some(&dir), false); assert_eq!(Launch::begin(Some(&dir), false).backends, None); assert_eq!(window_graphics(&dir), "auto"); @@ -217,4 +300,240 @@ mod tests { } let _ = std::fs::remove_dir_all(&dir); } + + /// A fresh settings folder for one test. + fn fresh_dir(case: &str) -> PathBuf { + tempfile::Builder::new().prefix(&format!("ec-launch-guard-{case}-")).tempdir().unwrap().keep() + } + + /// Window Graphics saved as OpenGL. + fn save_opengl(dir: &Path) { + let mut prefs = Prefs::default(); + prefs.startup.window_graphics = "gl".into(); + std::fs::write(dir.join(PREFS_FILE), prefs.to_json()).unwrap(); + } + + #[test] + fn a_second_copy_reports_the_saved_backend_without_owning_the_guard() { + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("second-source"); + save_opengl(&dir); + let mut first = Launch::begin(Some(&dir), false); + let mut second = Launch::begin(Some(&dir), false); + assert!(second.lock.is_none()); + assert_eq!((second.backends, second.source), (Some(Backends::GL), BackendSource::SavedPreference)); + second.exited(); + first.exited(); + let _ = std::fs::remove_dir_all(dir); + } + + /// A second copy that quits normally while the first is still opening doesn't remove the first + /// one's marker (#477). + #[test] + fn a_second_copy_quitting_leaves_the_running_launchs_marker() { + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("second-quits"); + let mut first = Launch::begin(Some(&dir), false); + let mut second = Launch::begin(Some(&dir), false); + second.exited(); + let kept = dir.join(MARKER).exists(); + first.exited(); + let _ = std::fs::remove_dir_all(&dir); + assert!(kept, "the second copy removed the running launch's marker"); + } + + /// A second copy whose OpenGL window fails doesn't remove a running launch's marker. + #[test] + fn failed_opengl_in_a_second_copy_leaves_the_running_launchs_marker() { + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("second-gl-fails"); + let mut first = Launch::begin(Some(&dir), false); + let second = Launch::begin(Some(&dir), false); + opengl_failed(Some(&dir)); + let kept = dir.join(MARKER).exists(); + drop(second); + first.exited(); + let _ = std::fs::remove_dir_all(&dir); + assert!(kept, "OpenGL cleanup removed another running launch's marker"); + } + + /// With `WGPU_BACKEND` set, a stale marker and a saved OpenGL choice are left as they are. + #[test] + fn wgpu_backend_leaves_a_stale_marker_and_saved_opengl_alone() { + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("env-override"); + save_opengl(&dir); + std::fs::write(dir.join(MARKER), b"stale-marker").unwrap(); + let before = std::fs::read(dir.join(PREFS_FILE)).unwrap(); + let mut l = Launch::begin(Some(&dir), true); + assert_eq!(l.backends, None); + assert!(l.notice.is_none()); + l.exited(); + assert!(dir.join(MARKER).exists()); + assert_eq!(std::fs::read(dir.join(PREFS_FILE)).unwrap(), before); + let _ = std::fs::remove_dir_all(&dir); + } + + /// After OpenGL fails to open a window, the next launch is back on Automatic, without a notice. + #[test] + fn failed_opengl_puts_the_next_launch_back_on_automatic() { + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("gl-recovers"); + save_opengl(&dir); + let l = Launch::begin(Some(&dir), false); + assert_eq!(l.backends, Some(Backends::GL)); + drop(l); + opengl_failed(Some(&dir)); + assert!(!dir.join(MARKER).exists()); + assert_eq!(Prefs::from_json(&std::fs::read_to_string(dir.join(PREFS_FILE)).unwrap()).startup.window_graphics, "auto"); + let mut l = Launch::begin(Some(&dir), false); + assert_eq!(l.backends, None); + assert!(l.notice.is_none()); + l.exited(); + let _ = std::fs::remove_dir_all(&dir); + } + + /// The guard file is never removed or replaced: unlinking a locked file would let the next + /// launch lock a new one while the old lock is still held. + #[cfg(unix)] + #[test] + fn the_guard_file_stays_the_same_file_across_launches() { + let _spawn_lock = crate::test_child_spawn_lock(); + use std::os::unix::fs::MetadataExt; + let dir = fresh_dir("guard-identity"); + let mut a = Launch::begin(Some(&dir), false); + let before = std::fs::metadata(dir.join(LOCK)).unwrap(); + let t0 = Instant::now(); + assert_eq!(a.frame_drawn_at(t0), Some(SETTLE)); + assert_eq!(a.frame_drawn_at(t0 + SETTLE), None); + assert!(!dir.join(MARKER).exists()); + let after = std::fs::metadata(dir.join(LOCK)).unwrap(); + assert_eq!((before.dev(), before.ino()), (after.dev(), after.ino())); + let mut b = Launch::begin(Some(&dir), false); + assert!(b.lock.is_some()); + let reused = std::fs::metadata(dir.join(LOCK)).unwrap(); + assert_eq!((before.dev(), before.ino()), (reused.dev(), reused.ino())); + b.exited(); + let _ = std::fs::remove_dir_all(dir); + } + + /// A second copy's frames and quit neither settle nor unlock the running launch. + #[test] + fn a_second_copys_frames_dont_settle_the_running_launch() { + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("second-frames"); + let mut a = Launch::begin(Some(&dir), false); + std::fs::write(dir.join(MARKER), b"owner-marker").unwrap(); + let mut b = Launch::begin(Some(&dir), false); + assert!(b.lock.is_none()); + assert!(b.marker.is_none()); + let t0 = Instant::now(); + assert_eq!(b.frame_drawn_at(t0), None); + assert_eq!(b.frame_drawn_at(t0 + SETTLE), None); + b.exited(); + assert_eq!(std::fs::read(dir.join(MARKER)).unwrap(), b"owner-marker"); + let probe = File::open(dir.join(LOCK)).unwrap(); + assert!(matches!(probe.try_lock(), Err(std::fs::TryLockError::WouldBlock))); + a.exited(); + let _ = std::fs::remove_dir_all(dir); + } + + /// Older versions lock the marker itself: while one runs, a new launch changes nothing, and + /// once it has stopped without settling the next launch still switches to OpenGL. + #[test] + fn a_running_older_version_holding_the_marker_is_left_alone() { + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("older-version"); + std::fs::write(dir.join(MARKER), b"old-owner").unwrap(); + std::fs::write(dir.join(PREFS_FILE), Prefs::default().to_json()).unwrap(); + let old = File::open(dir.join(MARKER)).unwrap(); + old.try_lock().unwrap(); + let before = std::fs::read(dir.join(PREFS_FILE)).unwrap(); + let mut b = Launch::begin(Some(&dir), false); + assert!(b.lock.is_none()); + assert!(b.marker.is_none()); + assert!(b.backends.is_none()); + assert!(b.notice.is_none()); + b.exited(); + opengl_failed(Some(&dir)); + assert_eq!(std::fs::read(dir.join(PREFS_FILE)).unwrap(), before); + // Windows locks forbid reading through another handle. Inspect the contents only after + // releasing the old owner's lock, but before recovery can truncate the marker. + drop(old); + assert_eq!(std::fs::read(dir.join(MARKER)).unwrap(), b"old-owner"); + let mut recovered = Launch::begin(Some(&dir), false); + if cfg!(not(target_os = "macos")) { + assert_eq!(recovered.backends, Some(Backends::GL)); + } + recovered.exited(); + let _ = std::fs::remove_dir_all(dir); + } + + /// A Windows sharing violation is not evidence that a marker is stale or absent. + #[cfg(windows)] + #[test] + fn an_unopenable_marker_leaves_settings_and_marker_alone() { + let _spawn_lock = crate::test_child_spawn_lock(); + use std::os::windows::fs::OpenOptionsExt; + + let dir = fresh_dir("marker-unopenable"); + std::fs::write(dir.join(MARKER), b"old-owner").unwrap(); + std::fs::write(dir.join(PREFS_FILE), Prefs::default().to_json()).unwrap(); + let old = std::fs::OpenOptions::new().read(true).write(true).share_mode(0).open(dir.join(MARKER)).unwrap(); + let before = std::fs::read(dir.join(PREFS_FILE)).unwrap(); + let mut launch = Launch::begin(Some(&dir), false); + assert!(launch.lock.is_none()); + assert!(launch.marker.is_none()); + assert!(launch.backends.is_none()); + assert!(launch.notice.is_none()); + launch.exited(); + opengl_failed(Some(&dir)); + assert_eq!(std::fs::read(dir.join(PREFS_FILE)).unwrap(), before); + drop(old); + assert_eq!(std::fs::read(dir.join(MARKER)).unwrap(), b"old-owner"); + let _ = std::fs::remove_dir_all(dir); + } + + /// A guard file that can't be opened leaves Window Graphics and the marker as they are. + #[test] + fn an_unusable_guard_file_leaves_settings_and_marker_alone() { + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("guard-unusable"); + std::fs::create_dir(dir.join(LOCK)).unwrap(); + std::fs::write(dir.join(MARKER), b"stale-marker").unwrap(); + std::fs::write(dir.join(PREFS_FILE), Prefs::default().to_json()).unwrap(); + let before = std::fs::read(dir.join(PREFS_FILE)).unwrap(); + let mut l = Launch::begin(Some(&dir), false); + assert!(l.backends.is_none()); + assert!(l.notice.is_none()); + assert!(l.lock.is_none()); + assert!(l.marker.is_none()); + l.exited(); + opengl_failed(Some(&dir)); + assert_eq!(std::fs::read(dir.join(PREFS_FILE)).unwrap(), before); + assert_eq!(std::fs::read(dir.join(MARKER)).unwrap(), b"stale-marker"); + let _ = std::fs::remove_dir_all(dir); + } + + /// OpenGL cleanup from elsewhere waits until the running launch has let go; then it resets + /// Window Graphics to Automatic and removes the marker. + #[test] + fn failed_opengl_cleanup_waits_for_the_running_launch() { + let _spawn_lock = crate::test_child_spawn_lock(); + let dir = fresh_dir("gl-cleanup-waits"); + save_opengl(&dir); + let launch = Launch::begin(Some(&dir), false); + assert_eq!(launch.backends, Some(Backends::GL)); + assert!(launch.lock.is_some()); + let before = std::fs::read(dir.join(PREFS_FILE)).unwrap(); + opengl_failed(Some(&dir)); + assert_eq!(std::fs::read(dir.join(PREFS_FILE)).unwrap(), before); + assert!(dir.join(MARKER).exists(), "cleanup elsewhere can't remove a running launch's marker"); + drop(launch); + opengl_failed(Some(&dir)); + let after = Prefs::from_json(&std::fs::read_to_string(dir.join(PREFS_FILE)).unwrap()).startup.window_graphics; + assert_eq!(after, "auto", "once the launch has let go, failed OpenGL resets Window Graphics"); + assert!(!dir.join(MARKER).exists()); + let _ = std::fs::remove_dir_all(dir); + } } diff --git a/apps/effectcraft/src/main.rs b/apps/effectcraft/src/main.rs index 5350addbe..b07e3ce43 100644 --- a/apps/effectcraft/src/main.rs +++ b/apps/effectcraft/src/main.rs @@ -22,6 +22,8 @@ mod appimage; mod apple_events; mod audio_out; mod control_server; +mod crash_report; +mod diagnostic_state; mod launch_guard; #[cfg(target_os = "macos")] mod native_menu; @@ -33,6 +35,17 @@ mod window; use effectcraft_ui_egui::EffectcraftApp; use serde_json::json; +// On Unix, a subprocess briefly inherits every non-CLOFORK descriptor between fork and exec. +// The app's guard-lock tests hold an advisory lock while crash-reporter tests spawn child +// processes; coordinate just those intervals so an unrelated test child cannot extend a lock. +#[cfg(test)] +static TEST_CHILD_SPAWN_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +#[cfg(test)] +fn test_child_spawn_lock() -> std::sync::MutexGuard<'static, ()> { + TEST_CHILD_SPAWN_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + /// The app icon for the window, taskbar and Dock (assets/app-icon, regenerated by /// `packaging/icons.sh`). macOS gets the padded variant so it sits on Apple's icon grid in the Dock. /// The 256 px app icon (Windows, Linux; the AppImage's desktop integration installs it too). @@ -180,6 +193,23 @@ fn start_helpers_if_opted_in(session: &mut effectcraft_engine::Session) { /// Builds the session, loads settings, plugins, the requested project/media and the optional control server, then /// reports startup failure through `startup_failed`. Returns the event loop's result. fn main() -> eframe::Result { + let args: Vec<_> = std::env::args_os().skip(1).collect(); + if crash_report::maybe_run(&args, |description| { + rfd::MessageDialog::new().set_level(rfd::MessageLevel::Error).set_title("EffectCraft diagnostic report").set_description(description).show(); + }) { + return Ok(()); + } + let reporter = crash_report::Shutdown::default(); + let result = run_desktop(&reporter); + // run_desktop has dropped the app and handled any startup error with its existing dialog. + // An unwinding panic or abort never reaches this marker. + if let Err(error) = reporter.finish() { + log::warn!("could not mark crash reporter clean: {error}"); + } + result +} + +fn run_desktop(reporter: &crash_report::Shutdown) -> eframe::Result { // Windows: the update helper is a copy of this program started with `--finish-update`. #[cfg(windows)] if let Some(result) = craft_update::self_update::finish(&effectcraft_ui_egui::UPDATE_APP, &std::env::args_os().skip(1).collect::>()) { @@ -205,20 +235,38 @@ fn main() -> eframe::Result { // The session log lives in the settings folder (`Logs/effectcraft.log`, the previous two runs // kept): a launch from the Dock or a menu has no terminal. Opened after the arguments, so // `--version` leaves no file behind; records logged until now are written to it first. + let run_id = crash_report::run_id(); if let Some(dir) = effectcraft_host::config_dir() { - match effectcraft_engine::logging::attach_session_log(&effectcraft_engine::logging::log_dir(Some(&dir))) { + let header = format!("{}\n", crash_report::session_marker(&run_id)); + match effectcraft_engine::logging::attach_session_log_with_header(&effectcraft_engine::logging::log_dir(Some(&dir)), &header) { Ok(path) => log::info!("effectcraft {}, log file {}", env!("CARGO_PKG_VERSION"), path.display()), Err(e) => log::warn!("no log file: {e}"), } } + let logs = effectcraft_engine::logging::log_dir(effectcraft_host::config_dir().as_deref()); + match crash_report::start(&run_id, effectcraft_engine::logging::session_file().as_deref(), &logs.join("Crash Reports"), control_port.is_none()) { + Ok(started) => { + if let Err(error) = reporter.install(started) { + log::warn!("crash reporter could not be retained: {error}"); + } + } + Err(error) => log::warn!("crash reporter could not start: {error}"), + } // The font menus list the installed fonts: read their names while the window opens. effectcraft_engine::text::fonts::scan_system_in_background(); // Wayland shows the window's icon from its desktop entry: an AppImage brings its own. #[cfg(target_os = "linux")] appimage::integrate_from_env(ICON_PNG); // Settings ▸ Startup & Repair ▸ Window Graphics, switched to OpenGL when the last launch - // stopped as its window opened (a crashing graphics driver, #243). - let mut launch = launch_guard::Launch::begin(effectcraft_host::config_dir().as_deref(), std::env::var_os("WGPU_BACKEND").is_some()); + // stopped as its window opened (the marker alone does not establish a cause). + let backend_override = std::env::var_os("WGPU_BACKEND"); + let mut launch = launch_guard::Launch::begin(effectcraft_host::config_dir().as_deref(), backend_override.is_some()); + diagnostic_state::record(reporter, launch.source.event()); + if let Some(value) = backend_override { + for event in diagnostic_state::backend_override_events(&value) { + diagnostic_state::record(reporter, event); + } + } let on_gl = launch.backends == Some(eframe::wgpu::Backends::GL); let notice = launch.notice.take(); let options = eframe::NativeOptions { @@ -245,22 +293,40 @@ fn main() -> eframe::Result { let apple_events = apple_events::AppleEvents::install(); #[cfg(target_os = "macos")] let apple_events = &apple_events; + let desktop_reporter = reporter.clone(); let result = eframe::run_native( "EffectCraft", options, Box::new(move |cc| { EffectcraftApp::install_ime_reset_hooks(&cc.egui_ctx); match &cc.wgpu_render_state { - Some(rs) => log::info!("graphics: {:?}", rs.adapter.get_info()), - None => log::info!("graphics: no wgpu device"), + Some(rs) => { + let info = rs.adapter.get_info(); + log::info!("graphics: {info:?}"); + let event = match info.backend { + eframe::wgpu::Backend::Noop => "actual GPU backend: noop", + eframe::wgpu::Backend::Vulkan => "actual GPU backend: vulkan", + eframe::wgpu::Backend::Metal => "actual GPU backend: metal", + eframe::wgpu::Backend::Dx12 => "actual GPU backend: dx12", + eframe::wgpu::Backend::Gl => "actual GPU backend: gl", + eframe::wgpu::Backend::BrowserWebGpu => "actual GPU backend: webgpu", + }; + diagnostic_state::record(&desktop_reporter, event); + } + None => diagnostic_state::record(&desktop_reporter, "actual GPU backend: no wgpu device"), } // This executable owns the shared device's handlers. Install them before // EffectCraft's compositor pipelines are built, without changing handlers from // inside Gpu::new. eframe has already constructed its presentation renderer here. let gpu_failures = effectcraft_ui_egui::gpu_failure::GpuFailureBridge::new(&cc.egui_ctx); + let observations = diagnostic_state::GpuObservations::new(&desktop_reporter); + let scoped_observed = observations.clone(); + gpu_failures.set_scoped_error_observer(move |error| scoped_observed.scoped(error)); if let Some(rs) = &cc.wgpu_render_state { + let errors_observed = observations.clone(); let errors = gpu_failures.clone(); rs.device.on_uncaptured_error(std::sync::Arc::new(move |error| { + errors_observed.uncaptured(&error); let message = format!("uncaptured GPU error: {error}"); if errors.report(&message, false) { log::error!("{message}"); @@ -268,6 +334,7 @@ fn main() -> eframe::Result { })); let lost = gpu_failures.clone(); rs.device.set_device_lost_callback(move |reason, message| { + observations.device_lost(reason); lost.report(&format!("GPU device lost ({reason:?}): {message}"), true); }); } @@ -398,6 +465,7 @@ fn main() -> eframe::Result { window: window::Tracker::new(opened, saved_window, control_port.is_none()), launch, drawn: false, + reporter: desktop_reporter, })) }), ); @@ -491,6 +559,7 @@ struct Desktop { launch: launch_guard::Launch, /// The first frame was drawn (logged once). drawn: bool, + reporter: crash_report::Shutdown, } impl eframe::App for Desktop { @@ -525,15 +594,20 @@ impl eframe::App for Desktop { } fn on_exit(&mut self) { - // On macOS the app quits through `-[NSApplication terminate:]`, and the process ends - // before `eframe::run_native` returns (#341). - log::info!("the window is closing"); - self.launch.exited(); - self.window.save(self.app.session.config.as_deref()); - self.app.on_exit(); - effectcraft_host::stop_helpers(); - // Scripts in Scripts/Shutdown run with the project closed. - let _ = self.app.session.execute("file.runShutdownScripts", json!({})); + // macOS terminates after this hook without returning from run_native (#341). + // Other platforms keep the reporter armed until main sees complete event-loop teardown. + let reporter = cfg!(target_os = "macos").then(|| self.reporter.clone()); + if let Err(error) = crash_report::after_teardown(reporter.as_ref(), || { + log::info!("the window is closing"); + self.launch.exited(); + self.window.save(self.app.session.config.as_deref()); + self.app.on_exit(); + effectcraft_host::stop_helpers(); + // Scripts in Scripts/Shutdown run with the project closed. + let _ = self.app.session.execute("file.runShutdownScripts", json!({})); + }) { + log::warn!("could not mark crash reporter clean after teardown: {error}"); + } } fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) { diff --git a/contributors/people.toml b/contributors/people.toml index e9144f9ea..beb91a413 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -135,3 +135,6 @@ display_name = "Jonathan Cardoso" [people.nazar-gavrylyk] sync_github_name = true + +[people.andreaaazo] +sync_github_name = true diff --git a/crates/aep/Cargo.toml b/crates/aep/Cargo.toml index b1bf3f9fe..0523edb9b 100644 --- a/crates/aep/Cargo.toml +++ b/crates/aep/Cargo.toml @@ -10,18 +10,15 @@ rust-version.workspace = true workspace = true [dependencies] +craft-aep = { workspace = true } effectcraft-time = { workspace = true } effectcraft-color = { workspace = true } effectcraft-keyframe = { workspace = true } effectcraft-project = { workspace = true } effectcraft-effects = { workspace = true } +log = { workspace = true } serde = { workspace = true } thiserror = { workspace = true } [dev-dependencies] serde_json = { workspace = true } - -# The tool's own tests run with the crate's (examples are only built by default). -[[example]] -name = "stubicc" -test = true diff --git a/crates/aep/README.md b/crates/aep/README.md index 965bea317..0e65d86f8 100644 --- a/crates/aep/README.md +++ b/crates/aep/README.md @@ -3,7 +3,9 @@ After Effects projects (`.aep`, `.aepx`) in one crate, the way `effectcraft-lottie` handles Lottie: - `container`: reads and writes the container as a lossless chunk tree, with a typed, read-only - view (below; its API is re-exported at the crate root); + view (below). This is `craft-aep` from [storytold/craft-libs](https://github.com/storytold/craft-libs), + re-exported at the crate root; its container tests and the `aepdump`/`stubicc` tools are there. + Only `effect_map` and `style_map` (After Effects effects and layer styles to EffectCraft's) are here; - `import`: maps the typed view into an EffectCraft project and reports everything it does not import ([`docs/aep-import.md`](../../docs/aep-import.md)); - `export`: writes an EffectCraft project as an `.aep` with every chunk built in code, and reports everything it does not export @@ -21,7 +23,9 @@ tree; pad bytes, odd bodies and the bytes after the container, an XMP packet, ar The XML form of a project (`.aepx`) is read into the same tree by `read_aepx` (a small bounded XML tokenizer, no dependency); `parse_any` picks the reader by content, so the typed view and the -importer take either form. The mapping is in `docs/aep-format.md`, section 1. +importer take either form. The mapping is in `docs/aep-format.md`, section 1. The importer, which +only reads, does not build the owned tree: it reads either form through the flat index +`TreeIndex`, an `.aep` in place and an `.aepx` decoded into one buffer first. Bodies that look like lists but are not chunk sequences (for example `LIST btdk`, raw text) stay opaque rather than failing the parse. Input is untrusted: sizes are checked against the remaining @@ -69,25 +73,30 @@ tests reach them only through `tests/it/common/mod.rs`, which hands a test its c of its own cut from the combined file; a test whose fixture is missing (or with the variable not set) prints `SKIPPED: fixture missing` and passes. -`tests/fixtures/synthetic.aep` (115 KB) is the one project in the repository: made in After Effects 2026 +`tests/fixtures/synthetic.aep` (115 KB) is one of the two projects in the repository: made in After Effects 2026 with the working space set to None (one composition, an image sequence of three 8x8 PNGs, a solid, a folder, a Levels and a Glow effect with expressions). Every After Effects project embeds the working space's ICC profile (HP sRGB IEC61966-2.1 for the default) in a JSON settings chunk; the one `Utf8` chunk of this file that held a profile was replaced by `{}`, and nothing else was changed. The tests in `tests/it/import/synthetic.rs` damage copies of it and need no other file. +`tests/fixtures/text-synthetic.aep` is the other: made in After Effects 2026 the same way (working +space None, nothing changed after the save), one composition with two text layers that hold a +text animator's Stroke Opacity (static and keyed) and Source Text expressions (enabled and +disabled); `tests/it/import/text_synthetic.rs` reads it (#3170). + +`tests/fixtures/ai-mask-3099.aep` (131 KB, #3099) is the second: four hand-written one-page PDFs +named `.ai` with distinct page boxes, placed in a composition, one of them with two masks under a +Null parented to a Null (generated by `tests/fixtures/tools/ai-mask-3099.py` and +`ai-mask-3099.jsx`, scrubbed with `scrub.py` and `stubicc`). `tests/it/import/ai_mask.rs` checks +the import against After Effects' read-back, and the host's `vector_import` tests render it. ## Tools - `tests/fixtures/tools/`: the generator of the test projects (regenerates the corpus with After Effects; its README says how). -- `examples/aepdump.rs`: prints the chunk tree of a file (ids, sizes, short data, match names): - `cargo run -p effectcraft-aep --example aepdump -- file.aep`. -- `examples/stubicc.rs`: replaces every embedded ICC profile of a saved project by a stub that keeps - only the profile's description: the JSON `colorProfileData` of the working space (any white space - around the colon, `\/` escapes, quotes as `"`) in an `.aep` chunk or the `.aepx` text, and a raw - profile of an imported image (an `.aep` chunk, or the `bdata` hex of an `.aepx`): - `cargo run -p effectcraft-aep --example stubicc -- in.aep out.aep`. A profile it cannot replace - stops it before anything is written. The importer reads the working space's name from the - description only, so the project imports the same; no vendor colour data stays in the test - project. Check the result independently, not by the tool's count (the corpus repository has - `tools/aep/inspect-embedded.py`, which scans the raw bytes, base64 and hex). +- `aepdump` (prints the chunk tree of a file) and `stubicc` (replaces every embedded ICC profile of a + saved project by a stub) are examples of `craft-aep` in + [storytold/craft-libs](https://github.com/storytold/craft-libs): + `cargo run -p craft-aep --example stubicc -- in.aep out.aep` there. Check the result independently, + not by the tool's count (the corpus repository has `tools/aep/inspect-embedded.py`, which scans the + raw bytes, base64 and hex). diff --git a/crates/aep/examples/aepdump.rs b/crates/aep/examples/aepdump.rs deleted file mode 100644 index 2fe492696..000000000 --- a/crates/aep/examples/aepdump.rs +++ /dev/null @@ -1,75 +0,0 @@ -//! Developer tool: prints the chunk tree of an .aep (ids, sizes, short data), match names inline. -#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] -use effectcraft_aep::{Body, Chunk}; - -/// Ends this developer tool with a message instead of a panic. -trait OrExit { - fn or_exit(self, what: &str) -> T; -} - -impl OrExit for Option { - fn or_exit(self, what: &str) -> T { - match self { - Some(v) => v, - None => exit(what), - } - } -} - -impl OrExit for Result { - fn or_exit(self, what: &str) -> T { - match self { - Ok(v) => v, - Err(e) => exit(&format!("{what}: {e:?}")), - } - } -} - -fn exit(msg: &str) -> ! { - eprintln!("{msg}"); - std::process::exit(1) -} - -fn show(c: &Chunk<'_>, depth: usize, max: usize, hex: usize) { - let ind = " ".repeat(depth); - let id = String::from_utf8_lossy(&c.id).to_string(); - match &c.body { - Body::Data(d) if &c.id == b"tdmn" => { - let n: String = d.iter().take_while(|b| **b != 0).map(|b| *b as char).collect(); - println!("{ind}tdmn {n}"); - } - Body::Data(d) => { - let h: String = d.iter().take(hex).map(|b| format!("{b:02x}")).collect(); - let a: String = d.iter().take(hex).map(|b| if (32..127).contains(b) { *b as char } else { '.' }).collect(); - println!("{ind}{id} [{}] {h} |{a}|", d.len()); - } - Body::Chunks(k) => { - println!("{ind}{id} (nested)"); - if depth < max { - for x in k { - show(x, depth + 1, max, hex); - } - } - } - Body::List { list_type, children } => { - println!("{ind}LIST {} ({} children)", String::from_utf8_lossy(list_type), children.len()); - if depth < max { - for x in children { - show(x, depth + 1, max, hex); - } - } - } - Body::OpaqueList { list_type, data } => println!("{ind}LIST {} opaque [{}]", String::from_utf8_lossy(list_type), data.len()), - } -} -fn main() { - let a: Vec = std::env::args().collect(); - let bytes = std::fs::read(a.get(1).or_exit("usage: see the comment at the top of this file")).or_exit("read"); - let max: usize = a.get(2).and_then(|s| s.parse().ok()).unwrap_or(4); - let hex: usize = a.get(3).and_then(|s| s.parse().ok()).unwrap_or(32); - let f = effectcraft_aep::parse(&bytes).or_exit("parse"); - for c in &f.chunks { - show(c, 0, max, hex); - } - println!("trailer {} bytes", f.trailer.len()); -} diff --git a/crates/aep/examples/stubicc.rs b/crates/aep/examples/stubicc.rs deleted file mode 100644 index 9438da06d..000000000 --- a/crates/aep/examples/stubicc.rs +++ /dev/null @@ -1,359 +0,0 @@ -//! Developer tool: replaces every embedded ICC profile of an .aep / .aepx by a stub that keeps only the -//! profile's description. Fixture hygiene: a saved project carries the vendor's working-space profile -//! (a few KB of colour data); the importer reads only the profile's name, the `desc` tag, so the stub -//! (a 128-byte header, one `desc` tag, no colour data, made here) imports identically. -//! -//! `cargo run -p effectcraft-aep --example stubicc -- in.aep out.aep` -//! -//! It handles: -//! - the JSON value `"colorProfileData": ""` in any chunk of an .aep and anywhere in the text -//! of an .aepx (white space around the colon, `\/` escapes and quotes written as `"` allowed); -//! - a raw ICC profile stored as a chunk of its own (the profile of an imported image), in an .aep -//! as the chunk's bytes and in an .aepx as its `bdata=""` attribute. -//! -//! A profile it cannot replace (no readable description, data that does not decode) stops the tool -//! before anything is written. Check the result independently (the corpus has -//! `tools/aep/inspect-embedded.py`), not by this tool's own count. -#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] -use effectcraft_aep::{Body, Chunk}; - -const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -/// Ends this developer tool with a message instead of a panic. -trait OrExit { - fn or_exit(self, what: &str) -> T; -} - -impl OrExit for Option { - fn or_exit(self, what: &str) -> T { - match self { - Some(v) => v, - None => exit(what), - } - } -} - -impl OrExit for Result { - fn or_exit(self, what: &str) -> T { - match self { - Ok(v) => v, - Err(e) => exit(&format!("{what}: {e:?}")), - } - } -} - -fn exit(msg: &str) -> ! { - eprintln!("{msg}"); - std::process::exit(1) -} - -fn b64_decode(text: &[u8]) -> Option> { - let (mut out, mut acc, mut bits) = (Vec::new(), 0u32, 0u32); - for &c in text.iter().filter(|c| **c != b'=') { - let v = u32::try_from(B64.iter().position(|b| *b == c)?).ok()?; - acc = (acc << 6 | v) & 0x00ff_ffff; - bits += 6; - if bits >= 8 { - bits -= 8; - out.push(((acc >> bits) & 0xff) as u8); - } - } - Some(out) -} - -fn b64_encode(data: &[u8]) -> Vec { - let mut out = Vec::new(); - for ch in data.chunks(3) { - let at = |i: usize| u32::from(ch.get(i).copied().unwrap_or(0)); - let n = (at(0) << 16) | (at(1) << 8) | at(2); - for i in 0..4usize { - if i <= ch.len() { - out.push(B64.get(((n >> (18 - 6 * i)) & 63) as usize).copied().unwrap_or(b'A')); - } else { - out.push(b'='); - } - } - } - out -} - -fn be32(d: &[u8], at: usize) -> Option { - let b: [u8; 4] = d.get(at..at.checked_add(4)?)?.try_into().ok()?; - usize::try_from(u32::from_be_bytes(b)).ok() -} - -/// The profile description (ICC.1 `desc`: v2 ASCII or v4 `mluc`, first record). -fn description(icc: &[u8]) -> Option { - let count = be32(icc, 128)?; - let tag = (0..count.min(1024)).find_map(|i| { - let at = 132usize.checked_add(i.checked_mul(12)?)?; - (icc.get(at..at.checked_add(4)?)? == b"desc").then_some(at) - })?; - let off = be32(icc, tag.checked_add(4)?)?; - let body = icc.get(off..off.checked_add(be32(icc, tag.checked_add(8)?)?)?)?; - let text = match body.get(..4)? { - b"desc" => { - let n = be32(body, 8)?; - String::from_utf8_lossy(body.get(12..12usize.checked_add(n)?)?.split(|b| *b == 0).next()?).into_owned() - } - b"mluc" => { - let (n, at) = (be32(body, 20)?, be32(body, 24)?); - let units: Vec = body.get(at..at.checked_add(n)?)?.chunks(2).filter_map(|p| Some(u16::from_be_bytes([*p.first()?, *p.get(1)?]))).collect(); - String::from_utf16_lossy(&units) - } - _ => return None, - }; - Some(text.trim_end_matches('\0').trim().to_owned()) -} - -/// A profile with a header, one `desc` tag and no colour data. -fn stub(desc: &str) -> Option> { - let mut body = b"desc\0\0\0\0".to_vec(); - body.extend_from_slice(&u32::try_from(desc.len().checked_add(1)?).ok()?.to_be_bytes()); - body.extend_from_slice(desc.as_bytes()); - body.push(0); - body.extend_from_slice(&[0; 4 + 4 + 2 + 1 + 67]); - let mut p = vec![0u8; 128]; - for (at, v) in [(8, &0x0240_0000u32.to_be_bytes()[..]), (12, b"mntr"), (16, b"RGB "), (20, b"XYZ "), (36, b"acsp")] { - p.get_mut(at..at + v.len())?.copy_from_slice(v); - } - p.extend_from_slice(&1u32.to_be_bytes()); - p.extend_from_slice(b"desc"); - p.extend_from_slice(&144u32.to_be_bytes()); - p.extend_from_slice(&u32::try_from(body.len()).ok()?.to_be_bytes()); - p.extend_from_slice(&body); - let size = u32::try_from(p.len()).ok()?.to_be_bytes(); - p.get_mut(..4)?.copy_from_slice(&size); - Some(p) -} - -/// The JSON key, without its quotes. -const KEY: &[u8] = b"colorProfileData"; -/// How a JSON quote is written: as is (a `Utf8` chunk, the `` text of an .aepx) and as an -/// XML entity (JSON inside an attribute of an .aepx). -const QUOTES: [&[u8]; 2] = [b"\"", b"""]; - -/// What the tool did, counted. -#[derive(Default)] -struct Tally { - stubbed: usize, - /// Values too short to be a profile (no ICC header), kept as they are. - kept_short: usize, - /// Profiles that could not be replaced (no readable description, undecodable data): the tool - /// fails, so no profile is ever left behind silently. - failed: Vec, -} - -fn is_profile(d: &[u8]) -> bool { - d.len() > 132 && d.get(36..40) == Some(b"acsp") -} - -/// The stub for the bytes `d` of a value, or `None` (counted) when it holds no profile or the -/// profile has no readable description. -fn stub_of(d: &[u8], at: usize, tally: &mut Tally) -> Option> { - if !is_profile(d) { - tally.kept_short += 1; - return None; - } - let s = description(d).and_then(|t| stub(&t)); - if s.is_none() { - tally.failed.push(format!("profile at byte {at}: no readable description")); - } - s -} - -/// At the start of `t`: the key in quotes, optional white space, a colon, optional white space and -/// the opening quote of the value. Returns the length up to and including that quote, and the quote. -fn value_start(t: &[u8]) -> Option<(usize, &'static [u8])> { - let ws = |mut i: usize| { - while t.get(i).is_some_and(u8::is_ascii_whitespace) { - i += 1; - } - i - }; - QUOTES.into_iter().find_map(|q| { - let key_end = q.len().checked_mul(2)?.checked_add(KEY.len())?; - let quoted = t.get(..key_end)?; - if !(quoted.starts_with(q) && quoted.get(q.len()..q.len() + KEY.len())? == KEY && quoted.ends_with(q)) { - return None; - } - let colon = ws(key_end); - if t.get(colon) != Some(&b':') { - return None; - } - let open = ws(colon + 1); - t.get(open..)?.starts_with(q).then_some((open + q.len(), q)) - }) -} - -fn hex_decode(h: &[u8]) -> Option> { - if !h.len().is_multiple_of(2) { - return None; - } - h.chunks(2).map(|p| u8::from_str_radix(std::str::from_utf8(p).ok()?, 16).ok()).collect() -} - -fn hex_encode(d: &[u8], upper: bool) -> Vec { - d.iter().flat_map(|b| if upper { format!("{b:02X}") } else { format!("{b:02x}") }.into_bytes()).collect() -} - -/// Replaces, in `text`, every `colorProfileData` JSON value (base64; `\/` escapes, white space -/// around the colon and quotes written as `"` allowed) and every .aepx `bdata=""` -/// attribute that holds a raw profile. -fn stub_text(text: &[u8], tally: &mut Tally) -> Vec { - const BDATA: &[u8] = b"bdata=\""; - let (mut out, mut i) = (Vec::with_capacity(text.len()), 0); - while let Some(&b) = text.get(i) { - let rest = text.get(i..).unwrap_or_default(); - if let Some((open, q)) = value_start(rest) - && let Some(len) = rest.get(open..).and_then(|v| v.windows(q.len()).position(|w| w == q)) - { - let raw = rest.get(open..open + len).unwrap_or_default(); - let unescaped: Vec = raw.iter().copied().filter(|c| *c != b'\\').collect(); - let replaced = match b64_decode(&unescaped) { - Some(d) => stub_of(&d, i, tally).map(|s| b64_encode(&s)), - None => { - tally.failed.push(format!("colorProfileData at byte {i}: not base64")); - None - } - }; - out.extend_from_slice(rest.get(..open).unwrap_or_default()); - match replaced { - Some(enc) => { - out.extend_from_slice(&enc); - tally.stubbed += 1; - } - None => out.extend_from_slice(raw), - } - i += open + len; - continue; - } - if rest.starts_with(BDATA) - && let Some(len) = rest.get(BDATA.len()..).and_then(|v| v.iter().position(|c| *c == b'"')) - { - let hex = rest.get(BDATA.len()..BDATA.len() + len).unwrap_or_default(); - let replaced = hex_decode(hex).filter(|d| is_profile(d)).and_then(|d| stub_of(&d, i, tally)); - out.extend_from_slice(BDATA); - match replaced { - Some(s) => { - out.extend_from_slice(&hex_encode(&s, hex.iter().any(u8::is_ascii_uppercase))); - tally.stubbed += 1; - } - None => out.extend_from_slice(hex), - } - i += BDATA.len() + len; - continue; - } - out.push(b); - i += 1; - } - out -} - -fn walk(chunks: &mut [Chunk<'_>], tally: &mut Tally) { - for c in chunks.iter_mut() { - // Only a data chunk is rewritten as one: an opaque `LIST` also has bytes, but replacing it by - // a data chunk would drop its list type. - let data = if let Body::Data(d) = &c.body { Some(d.to_vec()) } else { None }; - match data { - Some(d) if d.windows(KEY.len()).any(|w| w == KEY) => { - let new = stub_text(&d, tally); - *c = Chunk::data(c.id, new); - } - Some(d) if is_profile(&d) => { - if let Some(s) = stub_of(&d, 0, tally) { - *c = Chunk::data(c.id, s); - tally.stubbed += 1; - } - } - _ => { - if let Body::List { children, .. } | Body::Chunks(children) = &mut c.body { - walk(children, tally); - } - } - } - } -} - -fn main() { - let args: Vec = std::env::args().collect(); - let (Some(input), Some(output)) = (args.get(1), args.get(2)) else { exit("usage: stubicc ") }; - let bytes = std::fs::read(input).or_exit("read the input"); - let mut tally = Tally::default(); - let out = if input.ends_with(".aepx") { - stub_text(&bytes, &mut tally) - } else { - let mut file = effectcraft_aep::parse(&bytes).or_exit("parse the input"); - walk(&mut file.chunks, &mut tally); - effectcraft_aep::write(&file).or_exit("write the output") - }; - if !tally.failed.is_empty() { - exit(&format!("nothing written: {} profile(s) could not be replaced:\n{}", tally.failed.len(), tally.failed.join("\n"))); - } - std::fs::write(output, out).or_exit("write the output"); - println!("stubbed {} profile(s); kept {} short value(s) that hold no profile", tally.stubbed, tally.kept_short); -} - -#[cfg(test)] -#[allow(clippy::unwrap_used)] -mod tests { - use super::*; - - fn profile(desc: &str) -> Vec { - // A profile with colour data after the description (like a real one): the stub of `desc` - // plus 3000 bytes, its size field updated. - let mut p = stub(desc).unwrap(); - p.extend(std::iter::repeat_n(0x5a, 3000)); - let size = u32::try_from(p.len()).unwrap().to_be_bytes(); - p[..4].copy_from_slice(&size); - p - } - - fn stubbed(text: &[u8]) -> (Vec, Tally) { - let mut tally = Tally::default(); - (stub_text(text, &mut tally), tally) - } - - #[test] - fn json_values_in_every_spelling_are_replaced() { - let b64 = String::from_utf8(b64_encode(&profile("sRGB IEC61966-2.1"))).unwrap(); - let want = String::from_utf8(b64_encode(&stub("sRGB IEC61966-2.1").unwrap())).unwrap(); - for (before, after) in [ - (format!("{{\"colorProfileData\":\"{b64}\"}}"), format!("{{\"colorProfileData\":\"{want}\"}}")), - (format!("{{\"colorProfileData\" : \n\t\"{b64}\"}}"), format!("{{\"colorProfileData\" : \n\t\"{want}\"}}")), - (format!("{{\"colorProfileData\":\"{}\"}}", b64.replace('/', "\\/")), format!("{{\"colorProfileData\":\"{want}\"}}")), - (format!("{{"colorProfileData": "{b64}"}}"), format!("{{"colorProfileData": "{want}"}}")), - ] { - let (out, tally) = stubbed(before.as_bytes()); - assert_eq!(String::from_utf8(out).unwrap(), after); - assert_eq!((tally.stubbed, tally.failed.len()), (1, 0)); - } - } - - #[test] - fn a_raw_profile_in_aepx_hex_is_replaced_and_other_hex_kept() { - let hex = String::from_utf8(hex_encode(&profile("Rec.709 Gamma 2.4"), false)).unwrap(); - let want = String::from_utf8(hex_encode(&stub("Rec.709 Gamma 2.4").unwrap(), false)).unwrap(); - let text = format!("", "ab".repeat(200)); - let (out, tally) = stubbed(text.as_bytes()); - assert_eq!(String::from_utf8(out).unwrap(), format!("", "ab".repeat(200))); - assert_eq!((tally.stubbed, tally.failed.len()), (1, 0)); - // Upper-case hex stays upper case. - let (out, _) = stubbed(format!("bdata=\"{}\"", hex.to_uppercase()).as_bytes()); - assert_eq!(String::from_utf8(out).unwrap(), format!("bdata=\"{}\"", want.to_uppercase())); - } - - #[test] - fn a_profile_that_cannot_be_replaced_is_a_failure_not_a_silent_copy() { - // An ICC header whose tag table has no `desc`. - let mut p = profile("x"); - p[132..136].copy_from_slice(b"wtpt"); - let (_, tally) = stubbed(format!("\"colorProfileData\":\"{}\"", String::from_utf8(b64_encode(&p)).unwrap()).as_bytes()); - assert_eq!((tally.stubbed, tally.failed.len()), (0, 1)); - let (_, tally) = stubbed(b"\"colorProfileData\":\"not*base64\""); - assert_eq!(tally.failed.len(), 1); - // A short value holds no profile: kept and counted, not a failure. - let (out, tally) = stubbed(b"\"colorProfileData\":\"AAAA\""); - assert_eq!((out.as_slice(), tally.kept_short, tally.failed.len()), (&b"\"colorProfileData\":\"AAAA\""[..], 1, 0)); - } -} diff --git a/crates/aep/src/container.rs b/crates/aep/src/container.rs index 531d74843..d9dbcfb14 100644 --- a/crates/aep/src/container.rs +++ b/crates/aep/src/container.rs @@ -1,61 +1,7 @@ -//! After Effects project (`.aep`) container reader and writer. -//! -//! An `.aep` is a `RIFX` container (big-endian RIFF) with form type `Egg!`. This crate reads it -//! into a lossless chunk tree ([`AepFile`]) and writes it back byte for byte. The XML form of a -//! project (`.aepx`) is read into the same tree by [`read_aepx`]; [`parse_any`] picks the reader -//! by content. See -//! `docs/aep-format.md` for the observed format and how it was established (clean room: only -//! files we generated ourselves, one change per file). -//! -//! [`Project::new`] opens a typed, read-only view over the tree: items, compositions -//! ([`CompHeader`]), layers ([`LayerHeader`], [`Transform`], effects, masks, text documents, -//! markers), properties, keyframes and footage. Only fields whose layout was verified against the -//! values the test files were made with are decoded; every accessor returns `None` (or an empty -//! list) on damaged data instead of failing. +//! The `.aep` / `.aepx` container and its typed view come from `craft-aep`; only the tables that map +//! After Effects effects and layer styles onto EffectCraft's are kept here. -mod aepx; -mod arbitrary; -mod bytes; -mod camera; -mod chunk; pub mod effect_map; -mod effects; -mod error; -mod footage; -mod header; -mod keys; -mod masks; -mod matte; -mod read; -mod settings; -mod shapes; pub mod style_map; -mod textdoc; -mod typed; -mod values; -mod version; -mod write; -pub use aepx::{is_aepx, parse_any, read_aepx, read_aepx_with_limits}; -pub use arbitrary::{ - CURVE_IDENTITY, CryptomatteData, CurvesData, ExtractorChannels, HueSaturationData, LevelsData, MeshData, curve_lut, identifier_channel_type, -}; -pub use camera::{CameraPosition, DEFAULT_CAMERA_ZOOM}; -pub use chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS, Walk}; -pub use effects::{DefinitionSource, Effect, EffectDefinitions, EffectParam, LayerRef, ParamKind, ParamValue}; -pub use error::AepError; -pub use footage::{Footage, SequenceInfo}; -pub use header::{BlendMode, CompHeader, LayerHeader, LayerType, LightType, Quality, Rational, Renderer}; -pub use keys::{ExpressionFlags, Interpolation, KeyEase, KeyframeKind, SpatialKey}; -pub use masks::{Mask, MaskAtom, MaskMode, PathVertex, ShapePath}; -pub use matte::{MatteMode, TrackMatte}; -pub use read::{Limits, parse, parse_with_limits}; -pub use settings::{ProjectSettings, TimeDisplay, WorkingSpace}; -pub use shapes::{GradientType, PolystarType, ShapeDetail, ShapeGroup, ShapeItem, Stroke, StrokeCap, StrokeJoin}; -pub use textdoc::{ - AutoKern, Caps, CueType, FontBaseline, Justification, KernRun, Marker, ParagraphRun, StyleRun, TextBox, TextDocument, TextValue, parse_text_dict, -}; -pub use typed::{Comp, Item, ItemKind, KeyLayout, KeyList, Keyframe, Layer, ListHeader, Project, PropEntry, PropGroup, PropNode, Property, TimeBase}; -pub use values::{Orientation, OrientationKey, TimeRemap, TimeRemapKey, Tracked, Transform, ValueSource}; -pub use version::AeVersion; -pub use write::{write, write_to}; +pub use craft_aep::*; diff --git a/crates/aep/src/container/aepx.rs b/crates/aep/src/container/aepx.rs deleted file mode 100644 index a45cbae1d..000000000 --- a/crates/aep/src/container/aepx.rs +++ /dev/null @@ -1,640 +0,0 @@ -//! Reading the XML form of a project (`.aepx`) into the same chunk tree as an `.aep`. -//! -//! An `.aepx` is the `RIFX` chunk tree written as XML: one element per chunk, named by the chunk -//! id (or the list type of a `LIST`), with the body as hex in a `bdata` attribute. A few chunks -//! are written as text instead (`Utf8` as ``, the numbers `numS` and `ppSn`, the footage -//! alias `alas` as ``), and the XMP packet that follows the container of an `.aep` -//! is the element ``. The mapping, with the cases it was found on, is in -//! `docs/aep-format.md` (section 1.1). -//! -//! The reader is a small XML tokenizer for exactly that subset: elements, attributes, text, the -//! five predefined entities and character references, CDATA, comments and processing -//! instructions. A document type declaration is refused (no entity expansion). It runs without -//! recursion; the nesting depth and the element count are bounded by [`Limits`], and everything -//! it allocates is bounded by the size of the input. - -use std::borrow::Cow; - -use crate::container::chunk::{AepFile, Body, Chunk, LIST, NESTED_IDS}; -use crate::container::error::AepError; -use crate::container::read::{Limits, parse}; - -/// The root element of an `.aepx`. -const ROOT: &[u8] = b"AfterEffectsProject"; -/// The element that holds the XMP packet (the trailer of an `.aep`). -const XMP: &[u8] = b"ProjectXMPMetadata"; -/// The element of a `Utf8` chunk. -const STRING: &[u8] = b"string"; -/// The element of an `alas` chunk (footage alias), written as attributes instead of JSON. -const FILE_REFERENCE: &[u8] = b"fileReference"; - -/// List types whose body is not a chunk sequence: the `.aepx` writes them like data chunks (with -/// `bdata`), the `.aep` as an opaque `LIST`. -const OPAQUE_LIST_TYPES: [[u8; 4]; 1] = [*b"btdk"]; - -/// True when `data` looks like an `.aepx`: optional UTF-8 byte order mark and white space, then -/// an XML declaration or the `AfterEffectsProject` root element. -pub fn is_aepx(data: &[u8]) -> bool { - let rest = data.strip_prefix(&[0xef, 0xbb, 0xbf][..]).unwrap_or(data); - let start = rest.iter().position(|b| !b.is_ascii_whitespace()).unwrap_or(rest.len()); - let rest = rest.get(start..).unwrap_or(&[]); - rest.starts_with(b" Result, AepError> { - if is_aepx(data) { read_aepx(data) } else { parse(data) } -} - -/// Reads an `.aepx` with the default [`Limits`] into the chunk tree an `.aep` of the same project -/// gives. -/// -/// # Errors -/// [`AepError::Xml`] for input that is not well-formed XML of the `.aepx` subset (bad UTF-8, an -/// unknown entity, an unclosed or mismatched tag, invalid or odd-length hex, a number that does -/// not parse, a document type declaration, a root element other than `AfterEffectsProject`); -/// [`AepError::TooDeep`] / [`AepError::TooManyChunks`] beyond the limits. -pub fn read_aepx(data: &[u8]) -> Result, AepError> { - read_aepx_with_limits(data, Limits::default()) -} - -/// Like [`read_aepx`] with explicit [`Limits`]: `max_depth` bounds the element nesting (the root -/// element's children are depth 1), `max_chunks` the number of elements. -/// -/// # Errors -/// See [`read_aepx`]. -pub fn read_aepx_with_limits(data: &[u8], limits: Limits) -> Result, AepError> { - let text = match std::str::from_utf8(data) { - Ok(t) => t.as_bytes(), - Err(e) => return Err(xml(e.valid_up_to(), "the file is not valid UTF-8")), - }; - Reader { data: text, pos: 0, limits, count: 0, stack: Vec::new(), top: None, trailer: None }.run() -} - -fn xml(offset: usize, what: &'static str) -> AepError { - AepError::Xml { offset, what } -} - -/// What an open element becomes when it closes. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum Kind { - /// The root element: its children are the top-level chunks. - Root, - /// A chunk with this id (or a `LIST` with this list type). - Chunk([u8; 4]), - /// A `Utf8` chunk. - Utf8, - /// An `alas` chunk built from the attributes. - Alias, - /// The XMP packet (only directly below the root). - Xmp, - /// An element without a chunk counterpart (a name that is no chunk id); skipped with its content. - Skip, -} - -struct Frame { - kind: Kind, - /// Offset of the start tag (for errors). - at: usize, - /// The name as written, to match the end tag. - name: (usize, usize), - /// The decoded `bdata` attribute. - bdata: Option>, - /// The attributes of a `fileReference`, in document order. - attrs: Vec<(String, String)>, - /// Collected text (only for `string` and the XMP packet). - text: Vec, - children: Vec>, -} - -struct Reader<'d> { - data: &'d [u8], - pos: usize, - limits: Limits, - count: usize, - stack: Vec, - /// The top-level chunks once the root element has closed. - top: Option>>, - trailer: Option>, -} - -impl Reader<'_> { - fn run(mut self) -> Result, AepError> { - if self.data.starts_with(&[0xef, 0xbb, 0xbf]) { - self.pos = 3; - } - while self.pos < self.data.len() { - if self.rest().starts_with(b"<") { - self.markup()?; - } else { - self.text()?; - } - } - if let Some(open) = self.stack.last() { - return Err(xml(open.at, "an element is not closed before the end of the file")); - } - let chunks = self.top.ok_or_else(|| xml(self.data.len(), "no AfterEffectsProject root element"))?; - Ok(AepFile { chunks, trailer: Cow::Owned(self.trailer.unwrap_or_default()) }) - } - - fn rest(&self) -> &[u8] { - self.data.get(self.pos..).unwrap_or(&[]) - } - - /// The offset of `needle` at or after `from`. - fn find(&self, from: usize, needle: &[u8]) -> Option { - let hay = self.data.get(from..)?; - if needle.is_empty() || hay.len() < needle.len() { - return None; - } - let first = *needle.first()?; - let mut i = 0; - while let Some(p) = hay.get(i..)?.iter().position(|b| *b == first) { - let at = i.checked_add(p)?; - if hay.get(at..)?.starts_with(needle) { - return from.checked_add(at); - } - i = at.checked_add(1)?; - } - None - } - - /// Skips to just after `close`, which must follow. - fn skip_past(&mut self, close: &[u8], what: &'static str) -> Result { - let at = self.find(self.pos, close).ok_or(xml(self.pos, what))?; - self.pos = at.saturating_add(close.len()); - Ok(at) - } - - fn markup(&mut self) -> Result<(), AepError> { - let rest = self.rest(); - if rest.starts_with(b"", "a processing instruction is not closed")?; - } else if rest.starts_with(b"", "a comment is not closed")?; - } else if rest.starts_with(b"", "a CDATA section is not closed")?; - let content = self.data.get(start..end).unwrap_or(&[]); - let at = start; - match self.stack.last_mut() { - Some(frame) => { - if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) { - frame.text.extend_from_slice(content); - } - } - None => return Err(xml(at, "CDATA outside the root element")), - } - } else if rest.starts_with(b" bool { - !(b.is_ascii_whitespace() || matches!(b, b'<' | b'>' | b'/' | b'=' | b'"' | b'\'' | b'&')) - } - - fn name(&mut self) -> Result<(usize, usize), AepError> { - let start = self.pos; - let len = self.rest().iter().position(|b| !Self::is_name_byte(*b)).unwrap_or(self.rest().len()); - if len == 0 { - return Err(xml(start, "a tag or attribute has no name")); - } - self.pos = start.saturating_add(len); - Ok((start, self.pos)) - } - - fn skip_ws(&mut self) { - let n = self.rest().iter().position(|b| !b.is_ascii_whitespace()).unwrap_or(self.rest().len()); - self.pos = self.pos.saturating_add(n); - } - - fn slice(&self, (a, b): (usize, usize)) -> &[u8] { - self.data.get(a..b).unwrap_or(&[]) - } - - fn start_tag(&mut self) -> Result<(), AepError> { - let at = self.pos; - self.pos = self.pos.saturating_add(1); - let name = self.name()?; - let mut attrs: Vec<((usize, usize), Vec)> = Vec::new(); - let self_closing = loop { - self.skip_ws(); - let rest = self.rest(); - if rest.starts_with(b"/>") { - self.pos = self.pos.saturating_add(2); - break true; - } - if rest.starts_with(b">") { - self.pos = self.pos.saturating_add(1); - break false; - } - if rest.is_empty() { - return Err(xml(at, "a start tag is not closed")); - } - let key = self.name()?; - self.skip_ws(); - if !self.rest().starts_with(b"=") { - return Err(xml(self.pos, "an attribute has no value")); - } - self.pos = self.pos.saturating_add(1); - self.skip_ws(); - let quote = *self.rest().first().ok_or(xml(self.pos, "a start tag is not closed"))?; - if quote != b'"' && quote != b'\'' { - return Err(xml(self.pos, "an attribute value is not quoted")); - } - let vstart = self.pos.saturating_add(1); - let vend = self.find(vstart, &[quote]).ok_or(xml(vstart, "an attribute value is not closed"))?; - let raw = self.data.get(vstart..vend).unwrap_or(&[]); - if raw.contains(&b'<') { - return Err(xml(vstart, "`<` inside an attribute value")); - } - let mut value = Vec::new(); - decode_entities(raw, vstart, &mut value)?; - self.pos = vend.saturating_add(1); - attrs.push((key, value)); - }; - let kind = self.kind_of(name, at)?; - if kind != Kind::Root { - self.count = self.count.saturating_add(1); - if self.count > self.limits.max_chunks { - return Err(AepError::TooManyChunks); - } - // The root's children are depth 1, like the top-level chunks of an `.aep`. - if self.stack.len() > self.limits.max_depth { - return Err(AepError::TooDeep); - } - } - let mut frame = Frame { kind, at, name, bdata: None, attrs: Vec::new(), text: Vec::new(), children: Vec::new() }; - if matches!(kind, Kind::Chunk(_)) { - if let Some((key, value)) = attrs.iter().find(|(k, _)| self.slice(*k) == b"bdata") { - frame.bdata = Some(decode_hex(value, key.1)?); - } - } else if kind == Kind::Alias { - frame.attrs = attrs.iter().map(|(k, v)| (String::from_utf8_lossy(self.slice(*k)).into_owned(), String::from_utf8_lossy(v).into_owned())).collect(); - } - self.stack.push(frame); - if self_closing { - self.close()?; - } - Ok(()) - } - - /// Classifies an element by its name and where it is. - fn kind_of(&self, name: (usize, usize), at: usize) -> Result { - let n = self.slice(name); - let parent = match self.stack.last() { - None => { - if self.top.is_some() { - return Err(xml(at, "a second root element")); - } - if n != ROOT { - return Err(xml(at, "the root element is not AfterEffectsProject")); - } - return Ok(Kind::Root); - } - Some(p) => p.kind, - }; - Ok(match parent { - Kind::Skip | Kind::Utf8 | Kind::Xmp | Kind::Alias => Kind::Skip, - Kind::Root | Kind::Chunk(_) => { - if n == STRING { - Kind::Utf8 - } else if n == FILE_REFERENCE { - Kind::Alias - } else if n == XMP && parent == Kind::Root { - Kind::Xmp - } else if let Some(id) = chunk_id(n) { - Kind::Chunk(id) - } else { - Kind::Skip - } - } - }) - } - - fn end_tag(&mut self) -> Result<(), AepError> { - let at = self.pos; - self.pos = self.pos.saturating_add(2); - let name = self.name()?; - self.skip_ws(); - if !self.rest().starts_with(b">") { - return Err(xml(at, "an end tag is not closed")); - } - self.pos = self.pos.saturating_add(1); - let open = self.stack.last().ok_or(xml(at, "an end tag without a start tag"))?; - if self.slice(open.name) != self.slice(name) { - return Err(xml(at, "an end tag does not match its start tag")); - } - self.close() - } - - /// Closes the innermost element and hands its chunk to the parent. - fn close(&mut self) -> Result<(), AepError> { - let frame = self.stack.pop().ok_or(xml(self.pos, "an end tag without a start tag"))?; - let chunk = match frame.kind { - Kind::Root => { - self.top = Some(frame.children); - return Ok(()); - } - Kind::Skip => return Ok(()), - Kind::Xmp => { - if self.trailer.is_none() { - self.trailer = Some(frame.text); - } - return Ok(()); - } - Kind::Utf8 => Chunk::data(*b"Utf8", frame.text), - Kind::Alias => Chunk::data(*b"alas", alias_json(&frame.attrs).into_bytes()), - Kind::Chunk(id) => chunk_of(id, frame)?, - }; - if let Some(parent) = self.stack.last_mut() { - parent.children.push(chunk); - } - Ok(()) - } - - fn text(&mut self) -> Result<(), AepError> { - let start = self.pos; - let len = self.rest().iter().position(|b| *b == b'<').unwrap_or(self.rest().len()); - let end = start.saturating_add(len); - self.pos = end; - let raw = self.data.get(start..end).unwrap_or(&[]); - match self.stack.last_mut() { - Some(frame) if matches!(frame.kind, Kind::Utf8 | Kind::Xmp) => decode_entities(raw, start, &mut frame.text), - Some(_) => Ok(()), - None if raw.iter().all(u8::is_ascii_whitespace) => Ok(()), - None => Err(xml(start, "text outside the root element")), - } - } -} - -/// The chunk id of an element name: 1 to 4 printable ASCII bytes, padded with spaces (`Pin` is -/// the list type `Pin `). -fn chunk_id(name: &[u8]) -> Option<[u8; 4]> { - if name.is_empty() || name.len() > 4 || !name.iter().all(|b| b.is_ascii_graphic()) { - return None; - } - let mut id = [b' '; 4]; - for (d, s) in id.iter_mut().zip(name) { - *d = *s; - } - Some(id) -} - -/// The chunk of a closed element named by a chunk id. -fn chunk_of(id: [u8; 4], frame: Frame) -> Result, AepError> { - if let Some(bytes) = frame.bdata { - if OPAQUE_LIST_TYPES.contains(&id) { - let pad = if bytes.len() % 2 == 1 { Some(0) } else { None }; - return Ok(Chunk { id: LIST, body: Body::OpaqueList { list_type: id, data: Cow::Owned(bytes) }, pad }); - } - return Ok(Chunk::data(id, bytes)); - } - // Numbers written as text: `0` and `150.000000000`. - if &id == b"numS" || &id == b"ppSn" { - let text = match frame.children.as_slice() { - [only] if only.is(b"Utf8") => only.bytes().unwrap_or(&[]), - _ => return Err(xml(frame.at, "a numS or ppSn element without its number")), - }; - let text = std::str::from_utf8(text).map_err(|_| xml(frame.at, "a number is not text"))?.trim(); - let body = if &id == b"numS" { - // Only `0` is sampled, so whether the field is signed is open: accept both readings of 4 bytes. - let n = text.parse::().ok().filter(|n| (i64::from(i32::MIN)..=i64::from(u32::MAX)).contains(n)); - let n = n.ok_or(xml(frame.at, "numS is not a 32-bit integer"))?; - (n as u32).to_be_bytes().to_vec() - } else { - text.parse::().map_err(|_| xml(frame.at, "ppSn is not a number"))?.to_be_bytes().to_vec() - }; - return Ok(Chunk::data(id, body)); - } - if NESTED_IDS.contains(&id) { - if frame.children.is_empty() { - return Ok(Chunk::data(id, Vec::new())); - } - return Ok(Chunk { id, body: Body::Chunks(frame.children), pad: None }); - } - Ok(Chunk::list(id, frame.children)) -} - -/// Appends `raw` with the predefined entities and character references decoded. -fn decode_entities(raw: &[u8], offset: usize, out: &mut Vec) -> Result<(), AepError> { - let mut i = 0; - while let Some(rest) = raw.get(i..) { - let Some(amp) = rest.iter().position(|b| *b == b'&') else { - out.extend_from_slice(rest); - break; - }; - out.extend_from_slice(rest.get(..amp).unwrap_or(&[])); - let at = offset.saturating_add(i).saturating_add(amp); - let after = rest.get(amp.saturating_add(1)..).unwrap_or(&[]); - let semi = after.iter().take(12).position(|b| *b == b';').ok_or(xml(at, "an entity is not terminated"))?; - let name = after.get(..semi).unwrap_or(&[]); - let ch = match name { - b"amp" => '&', - b"lt" => '<', - b"gt" => '>', - b"quot" => '"', - b"apos" => '\'', - _ => { - let code = if let Some(hex) = name.strip_prefix(b"#x").or_else(|| name.strip_prefix(b"#X")) { - std::str::from_utf8(hex).ok().and_then(|h| u32::from_str_radix(h, 16).ok()) - } else if let Some(dec) = name.strip_prefix(b"#") { - std::str::from_utf8(dec).ok().and_then(|d| d.parse::().ok()) - } else { - return Err(xml(at, "an unknown entity")); - }; - code.filter(|c| *c != 0).and_then(char::from_u32).ok_or(xml(at, "an invalid character reference"))? - } - }; - let mut buf = [0u8; 4]; - out.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes()); - i = i.saturating_add(amp).saturating_add(semi).saturating_add(2); - } - Ok(()) -} - -/// Decodes a `bdata` value (hex digits, two per byte). -fn decode_hex(text: &[u8], offset: usize) -> Result, AepError> { - if text.len() % 2 == 1 { - return Err(xml(offset, "bdata has an odd number of hex digits")); - } - let digit = |b: u8| match b { - b'0'..=b'9' => Some(b - b'0'), - b'a'..=b'f' => Some(b - b'a' + 10), - b'A'..=b'F' => Some(b - b'A' + 10), - _ => None, - }; - let (pairs, _) = text.as_chunks::<2>(); - pairs - .iter() - .map(|[hi, lo]| digit(*hi).zip(digit(*lo)).map(|(h, l)| (h << 4) | l)) - .collect::>>() - .ok_or(xml(offset, "bdata holds a character that is not a hex digit")) -} - -/// The JSON body of an `alas` chunk from the attributes of a `fileReference`, keys in document -/// order (alphabetical in every sample, as in the `.aep`): the two `ascendcount_*` values as -/// numbers, `platform` `Win` as `1`, `target_is_folder` `0` / `1` as `false` / `true`, everything -/// else as a string. -fn alias_json(attrs: &[(String, String)]) -> String { - let mut out = String::from("{"); - for (i, (key, value)) in attrs.iter().enumerate() { - if i > 0 { - out.push(','); - } - json_str(&mut out, key); - out.push(':'); - let number = value.parse::().ok(); - match (key.as_str(), value.as_str()) { - ("ascendcount_base" | "ascendcount_target", _) if number.is_some() => out.push_str(value), - ("platform", "Win") => out.push('1'), - ("platform", _) if number.is_some() => out.push_str(value), - ("target_is_folder", "0" | "false") => out.push_str("false"), - ("target_is_folder", "1" | "true") => out.push_str("true"), - _ => json_str(&mut out, value), - } - } - out.push('}'); - out -} - -fn json_str(out: &mut String, s: &str) { - out.push('"'); - for c in s.chars() { - match c { - '"' => out.push_str("\\\""), - '\\' => out.push_str("\\\\"), - '\n' => out.push_str("\\n"), - '\r' => out.push_str("\\r"), - '\t' => out.push_str("\\t"), - c if u32::from(c) < 0x20 => out.push_str(&format!("\\u{:04x}", u32::from(c))), - c => out.push(c), - } - } - out.push('"'); -} - -#[cfg(test)] -mod tests { - use super::*; - - fn doc(body: &str) -> String { - format!("\n{body}\n") - } - - #[test] - fn maps_elements_to_chunks() { - let x = doc(concat!( - "\n", - "A & BA-3", - "150.000000000x", - "]]>" - )); - let f = read_aepx(x.as_bytes()).unwrap(); - assert_eq!(f.trailer.as_ref(), b""); - let ids: Vec<_> = f.chunks.iter().map(|c| (c.id, c.list_type())).collect(); - assert_eq!( - ids, - vec![ - (*b"head", None), - (LIST, Some(*b"Pin ")), - (LIST, Some(*b"Pefl")), - (*b"tdsn", None), - (*b"numS", None), - (*b"ppSn", None), - (LIST, Some(*b"btdk")) - ] - ); - assert_eq!(f.chunks[1].children()[0].bytes(), Some(&[1u8][..])); - assert_eq!(f.chunks[1].children()[0].pad, Some(0)); - assert_eq!(f.chunks[3].children()[0].bytes(), Some(&b"A & BA"[..])); - assert_eq!(f.chunks[4].bytes(), Some(&(-3i32).to_be_bytes()[..])); - assert_eq!(f.chunks[5].bytes(), Some(&150f64.to_be_bytes()[..])); - assert_eq!(f.chunks[6].bytes(), Some(&b"ABC"[..])); - // The tree writes as a valid `.aep` that parses back to itself. - let bin = crate::container::write(&f).unwrap(); - assert_eq!(parse(&bin).unwrap(), f); - } - - #[test] - fn file_reference_becomes_alias_json() { - let x = doc(concat!( - "" - )); - let f = read_aepx(x.as_bytes()).unwrap(); - let alas = f.chunks[0].children()[0].bytes().unwrap(); - assert_eq!( - std::str::from_utf8(alas).unwrap(), - r#"{"ascendcount_base":1,"ascendcount_target":1,"fullpath":"C:\\a\\\"b.png","platform":1,"server_name":"N","server_volume_name":"V","target_is_folder":false}"# - ); - } - - #[test] - fn nums_takes_signed_and_unsigned_32_bit_values() { - for (text, bytes) in [("-2147483648", [0x80, 0, 0, 0]), ("4294967295", [0xff; 4]), (" 7 ", [0, 0, 0, 7])] { - let f = read_aepx(doc(&format!("{text}")).as_bytes()).unwrap(); - assert_eq!(f.chunks[0].bytes(), Some(&bytes[..]), "{text}"); - } - for text in ["-2147483649", "4294967296", "1.5"] { - assert!(read_aepx(doc(&format!("{text}")).as_bytes()).is_err(), "{text}"); - } - } - - #[test] - fn detection() { - assert!(is_aepx(b"\xef\xbb\xbf ")); - assert!(is_aepx(b"")); - assert!(!is_aepx(b"RIFX\0\0\0\x04Egg!")); - assert!(parse_any(b"RIFX\0\0\0\x04Egg!").is_ok()); - assert!(parse_any(doc("").as_bytes()).is_ok()); - } - - #[test] - fn bad_input_is_an_error() { - let bad = [ - doc(""), - doc(""), - doc(" "), - doc("�"), - doc("&"), - doc(""), - doc(""), - doc("x"), - doc(""), - doc(""), - doc(""), - "]>".into(), - "".into(), - "".into(), - "".into(), - "junk".into(), - "]]>"), 2); assert_eq!(super::import::json_depth(br#"{"a": "[[[{{", "b": [[1], {"c": "\\\"["}]}"#), 3); } + +/// A hostile project: footage in a folder whose parents loop back to it. Export walked the +/// folder's parents recursively and never reached the cache insert, so the stack ran out. +#[test] +fn export_survives_a_folder_parent_cycle() { + let (mut p, cid) = ae_project(); + let a = p.add_item("A", effectcraft_color::Label::None, None, ItemKind::Folder); + let b = p.add_item("B", effectcraft_color::Label::None, Some(a), ItemKind::Folder); + p.items.get_mut(&a).unwrap().parent = Some(b); + let footage: Vec = p.items.values().filter(|i| matches!(i.kind, ItemKind::Footage(_))).map(|i| i.id).collect(); + for f in footage { + p.items.get_mut(&f).unwrap().parent = Some(a); + } + let out = export(&p, cid, &ExportOptions::default(), &HashMap::new()).unwrap(); + assert!(out.warnings.iter().any(|w| w.contains("contains itself")), "{:?}", out.warnings); +} + +/// Precomps that use each other, and a chain of comps that each use the next one twice: the +/// audio check recursed forever on the first and 2^depth times on the second. +#[test] +fn export_audio_check_ends_on_precomp_cycles_and_shared_chains() { + let mut p = Project::default(); + let a = footage_item(&mut p, "/media/a.mov"); + let comp = || Comp::new(W, H, FrameRate::new(25, 1), t(2.0)); + // A chain of 60 comps, each using the next twice; the last one is silent footage. + let mut next = { + let mut c = comp(); + let l = effectcraft_project::build::layer(&mut p, &c, "a", LayerSource::Footage { item: a }, (W, H), None); + c.layers.push(l); + comp_item(&mut p, "Leaf", c) + }; + for i in 0..60 { + let mut c = comp(); + for n in 0..2 { + let l = effectcraft_project::build::layer(&mut p, &c, &format!("use {n}"), LayerSource::Comp { item: next }, (W, H), None); + c.layers.push(l); + } + next = comp_item(&mut p, &format!("Chain {i}"), c); + } + let started = std::time::Instant::now(); + export(&p, next, &ExportOptions::default(), &HashMap::new()).unwrap(); + assert!(started.elapsed() < std::time::Duration::from_secs(30), "{:?}", started.elapsed()); + // Two comps nesting each other. + let x = comp_item(&mut p, "X", comp()); + let mut y = comp(); + let l = effectcraft_project::build::layer(&mut p, &y, "x", LayerSource::Comp { item: x }, (W, H), None); + y.layers.push(l); + let y = comp_item(&mut p, "Y", y); + let mut xc = comp(); + let l = effectcraft_project::build::layer(&mut p, &xc, "y", LayerSource::Comp { item: y }, (W, H), None); + xc.layers.push(l); + if let Some(ItemKind::Comp(c)) = p.items.get_mut(&x).map(|i| &mut i.kind) { + *c = Arc::new(xc); + } + export(&p, x, &ExportOptions::default(), &HashMap::new()).unwrap(); +} + +/// A flat edit with many clips and many transitions: each clip finds its own transitions (it +/// scanned every transition of the track per clip), and they still extend and fade it. +#[test] +fn import_finds_each_clips_transitions_without_scanning_the_track() { + let mut p = fp::Project::new("Flat"); + let a = media(&mut p, "a.png", "/media/a.mov", filmcraft_media::MediaKind::Movie, true, false, None); + let settings = fp::SequenceSettings { width: W, height: H, frame_rate: rate(), ..Default::default() }; + let main = p.new_sequence("Main", settings, 1, 0, None); + const N: usize = 500; + let mut items = Vec::with_capacity(N); + let mut transitions = Vec::with_capacity(N); + for i in 0..N { + let ti = item(&mut p, a, fp::TrackKind::Video, i as f64 * 0.4, 0.0, 0.4); + if let Some(prev) = items.last().map(|x: &fp::TrackItem| x.id) { + transitions.push(fp::Transition { + id: fp::TransitionId(p.alloc_id()), + effect: fp::find_effect("cross_dissolve").unwrap().instance(), + start: secs(i as f64 * 0.4 - 0.08), + duration: secs(0.16), + from: Some(prev), + to: Some(ti.id), + align: fp::TransitionAlign::CenterAtCut, + reverse: false, + }); + } + items.push(ti); + } + let seq = p.sequence_mut(main).unwrap(); + seq.video_tracks[0].items = items; + seq.video_tracks[0].transitions = transitions; + let (bytes, _) = fc::export(&p, main, fc::Format::Otio, &Default::default()).unwrap(); + let (q, r) = import_doc(&bytes, "Flat.otio", &mut probe); + let m = q.comp(r.comps[0]).unwrap(); + assert_eq!(m.layers.len(), N); + // A middle clip reaches over both of its transitions' handles. + let mid = m.layers.iter().find(|l| near(l.in_point, t(100.0 - 0.08))).expect("clip 250 extended into its incoming transition"); + assert!(near(mid.out_point, t(100.4 + 0.08)), "{}", mid.out_point.seconds()); +} diff --git a/crates/keyframe/src/tests.rs b/crates/keyframe/src/tests.rs index 88f9f3249..68823b76b 100644 --- a/crates/keyframe/src/tests.rs +++ b/crates/keyframe/src/tests.rs @@ -165,6 +165,27 @@ fn gradient_tracks_sample_separately() { assert_eq!((empty.color_at(0.3), empty.opacity_at(0.3)), ([1.0; 4], 1.0)); } +/// A gradient's stop count multiplies every painted pixel's sampling: plain JSON with more than +/// `MAX_GRADIENT_STOPS` is refused, and a longer stored list is sampled through its first ones. +#[test] +fn gradient_stop_count_is_bounded() { + use crate::value::MAX_GRADIENT_STOPS; + let g = Value::Gradient(Gradient::default()); + let many: Vec = (0..=MAX_GRADIENT_STOPS).map(|_| serde_json::json!("#ff0000")).collect(); + assert_eq!(g.coerce_json(&serde_json::Value::Array(many.clone())), None); + assert!(g.coerce_json(&serde_json::Value::Array(many[..MAX_GRADIENT_STOPS].to_vec())).is_some()); + let ops: Vec = (0..=MAX_GRADIENT_STOPS).map(|i| serde_json::json!([i as f64 / MAX_GRADIENT_STOPS as f64, 1.0])).collect(); + assert_eq!(g.coerce_json(&serde_json::json!({"colors": ["#000000", "#ffffff"], "opacities": ops})), None); + // The tagged (typed) form is bounded the same way. + let tagged = Value::Gradient(Gradient { colors: (0..=MAX_GRADIENT_STOPS).map(|i| (i as f64, [0.0; 4])).collect(), opacities: vec![(0.0, 1.0)] }); + assert_eq!(g.coerce_json(&tagged.to_json()), None); + // A stored gradient with a huge tail: stops past the limit don't take part in sampling. + let mut colors: Vec<(f64, [f32; 4])> = (0..MAX_GRADIENT_STOPS).map(|i| (i as f64 / MAX_GRADIENT_STOPS as f64 * 0.5, [0.0, 0.0, 0.0, 1.0])).collect(); + colors.extend((0..100_000).map(|_| (1.0, [1.0, 1.0, 1.0, 1.0]))); + let big = Gradient { colors, opacities: vec![(0.0, 1.0)] }; + assert_eq!(big.sample(1.0), [0.0, 0.0, 0.0, 1.0]); +} + /// #612: the editor saves the typed form, so one colour stop (which the plain JSON parser /// rejects) and each stop's RGBA come back exactly. #[test] diff --git a/crates/keyframe/src/text_doc.rs b/crates/keyframe/src/text_doc.rs index 96a8b6be3..e8fcb52be 100644 --- a/crates/keyframe/src/text_doc.rs +++ b/crates/keyframe/src/text_doc.rs @@ -85,13 +85,13 @@ impl Justify { } pub fn parse(s: &str) -> Option { Some(match s.to_ascii_lowercase().replace(['_', ' ', '-'], "").as_str() { - "left" | "leftjustify" => Justify::Left, - "center" | "centre" | "centerjustify" => Justify::Center, - "right" | "rightjustify" => Justify::Right, + "left" | "leftjustify" | "alignleft" => Justify::Left, + "center" | "centre" | "centerjustify" | "aligncenter" => Justify::Center, + "right" | "rightjustify" | "alignright" => Justify::Right, "justify" | "justifyleft" | "justifylastleft" | "fulljustifylastlineleft" => Justify::JustifyLastLeft, "justifycenter" | "justifylastcenter" | "fulljustifylastlinecenter" => Justify::JustifyLastCenter, "justifyright" | "justifylastright" | "fulljustifylastlineright" => Justify::JustifyLastRight, - "justifyall" | "fulljustifylastlinefull" | "fulljustify" => Justify::JustifyAll, + "justifyall" | "justifyfull" | "fulljustifylastlinefull" | "fulljustify" => Justify::JustifyAll, _ => return None, }) } @@ -977,7 +977,19 @@ impl TextDoc { if r.start >= r.end && n > 0 { return Ok(true); } - self.apply_style(r.start..r.end.max(r.start + usize::from(n == 0)), |s| { + // Empty text: apply_style ignores the bounds, so a fixed range avoids `start + 1` overflow. + let range = if n == 0 { 0..1 } else { r.start..r.end }; + // Check every affected run first, so a value one run refuses changes none of them. + let mut err = None; + self.apply_style(range.clone(), |s| { + if let Err(e) = apply_char_attr(&mut s.clone(), key, v) { + err.get_or_insert(e); + } + }); + if let Some(e) = err { + return Err(e); + } + self.apply_style(range, |s| { let _ = apply_char_attr(s, key, v); }); return Ok(true); @@ -1057,6 +1069,11 @@ pub const CHAR_ATTRS: &[&str] = &[ pub const PARA_ATTRS: &[&str] = &["justify", "indentLeft", "indentRight", "indentFirst", "spaceBefore", "spaceAfter", "direction", "composer", "hangingPunctuation"]; +/// Most variable font axes one style can set. Real fonts have a handful (OpenType registers +/// five; custom axes rarely bring the total past twenty); a `variations` object with more is +/// rejected before any of it is applied. +pub const MAX_VARIATION_AXES: usize = 64; + /// Set one character attribute; Ok(false) for keys that aren't character attributes. pub fn apply_char_attr(s: &mut CharStyle, key: &str, v: &J) -> Result { match key { @@ -1173,20 +1190,28 @@ pub fn apply_char_attr(s: &mut CharStyle, key: &str, v: &J) -> Result match v { J::Null => s.variations.clear(), J::Object(m) => { - if m.is_empty() { - s.variations.clear(); + if m.len() > MAX_VARIATION_AXES { + return Err(format!("variations: at most {MAX_VARIATION_AXES} axes")); } + // One keyed merge (the result sorted by tag), applied only when every entry is valid. + let mut axes: std::collections::BTreeMap = if m.is_empty() { Default::default() } else { s.variations.iter().cloned().collect() }; for (tag, val) in m { if tag.is_empty() || tag.len() > 4 { return Err(format!("variations: `{tag}` is not an axis tag")); } - s.variations.retain(|(t, _)| t != tag); match val { - J::Null => {} - _ => s.variations.push((tag.clone(), val.as_f64().ok_or("variations: expected {tag: number}")? as f32)), + J::Null => { + axes.remove(tag); + } + _ => { + axes.insert(tag.clone(), val.as_f64().ok_or("variations: expected {tag: number}")? as f32); + } } } - s.variations.sort_by(|a, b| a.0.cmp(&b.0)); + if axes.len() > MAX_VARIATION_AXES { + return Err(format!("variations: at most {MAX_VARIATION_AXES} axes")); + } + s.variations = axes.into_iter().collect(); } _ => return Err("variations: expected {tag: number, …}".into()), }, @@ -1321,6 +1346,44 @@ pub fn word_at(text: &str, ci: usize) -> Range { mod tests { use super::*; + #[test] + fn variations_merge_by_tag_and_reject_too_many_axes() { + let mut doc = TextDoc::plain("ab"); + assert!(doc.set_attr("variations", &serde_json::json!({"wght": 650, "wdth": 90}), None).unwrap()); + assert!(doc.set_attr("variations", &serde_json::json!({"wght": 700, "wdth": null, "opsz": 12}), None).unwrap()); + assert_eq!(doc.style_at(0).variations, vec![("opsz".to_string(), 12.0), ("wght".to_string(), 700.0)]); + // More axes than any font has: refused up front, the style untouched. + let many: serde_json::Map = (0..MAX_VARIATION_AXES + 1).map(|i| (format!("a{i:03}"), J::from(1.0))).collect(); + assert!(doc.set_attr("variations", &J::Object(many), None).is_err()); + assert_eq!(doc.style_at(0).variations.len(), 2); + // A merge that would grow past the limit is refused too. + let fill: serde_json::Map = (0..MAX_VARIATION_AXES - 2).map(|i| (format!("b{i:03}"), J::from(1.0))).collect(); + assert!(doc.set_attr("variations", &J::Object(fill), None).unwrap()); + assert_eq!(doc.style_at(0).variations.len(), MAX_VARIATION_AXES); + // (The probe on an empty style passes; the styles already at the limit keep their axes.) + let _ = doc.set_attr("variations", &serde_json::json!({"zzzz": 1}), None); + assert_eq!(doc.style_at(0).variations.len(), MAX_VARIATION_AXES); + assert!(!doc.style_at(0).variations.iter().any(|(t, _)| t == "zzzz")); + } + + #[test] + fn refused_variation_leaves_every_run_unchanged() { + // "ab" is at the axis limit; "cd" has none. Three new axes over both runs must be refused + // for both, not applied to "cd" only. + let mut doc = TextDoc::plain("abcd"); + let fill: serde_json::Map = (0..MAX_VARIATION_AXES).map(|i| (format!("b{i:03}"), J::from(1.0))).collect(); + assert!(doc.set_attr("variations", &J::Object(fill), Some(0..2)).unwrap()); + assert!(doc.set_attr("variations", &serde_json::json!({"x1": 1, "x2": 1, "x3": 1}), None).is_err()); + assert_eq!(doc.style_at(3).variations.len(), 0); + assert_eq!(doc.style_at(0).variations.len(), MAX_VARIATION_AXES); + } + + #[test] + fn empty_document_accepts_an_extreme_range_without_overflow() { + let mut doc = TextDoc::plain(""); + assert!(doc.set_attr("size", &J::from(20), Some(usize::MAX..usize::MAX)).unwrap()); + } + #[test] fn mixed_line_breaks_keep_original_byte_and_character_ranges() { let text = "é\r\nβ\rx\n\n"; diff --git a/crates/keyframe/src/value.rs b/crates/keyframe/src/value.rs index 6609aed68..6e87f8933 100644 --- a/crates/keyframe/src/value.rs +++ b/crates/keyframe/src/value.rs @@ -210,7 +210,13 @@ impl Gradient { } } +/// Most stops (colour or opacity) a gradient is sampled through. Gradients are sampled per +/// pixel, so the stop count multiplies every painted pixel's work: stops past this are ignored +/// when sampling, and a gradient written as JSON with more is refused. +pub const MAX_GRADIENT_STOPS: usize = 256; + fn sample_stops(stops: &[(f64, T)], t: f64, lerp: impl Fn(T, T, f64) -> T) -> Option { + let stops = &stops[..stops.len().min(MAX_GRADIENT_STOPS)]; let first = stops.first()?; if t <= first.0 { return Some(first.1); @@ -243,7 +249,7 @@ fn gradient_json(j: &serde_json::Value) -> Option { } fn stops(a: &[J]) -> Option> { let n = a.len(); - if n < 2 { + if !(2..=MAX_GRADIENT_STOPS).contains(&n) { return None; } a.iter() @@ -259,6 +265,7 @@ fn gradient_json(j: &serde_json::Value) -> Option { J::Object(o) => { let colors = stops(o.get("colors")?.as_array()?)?; let op = match o.get("opacities") { + Some(J::Array(a)) if a.len() > MAX_GRADIENT_STOPS => return None, Some(J::Array(a)) => Some( a.iter() .map(|s| { @@ -469,9 +476,10 @@ impl Value { } Value::Layer(_) => Some(Value::Layer(j.as_u64())), Value::Path(_) => serde_json::from_value::(j.clone()).ok().map(Value::Path).or_else(|| serde_json::from_value::(j.clone()).ok()), - Value::Gradient(_) => { - serde_json::from_value::(j.clone()).ok().filter(|v| matches!(v, Value::Gradient(_))).or_else(|| gradient_json(j).map(Value::Gradient)) - } + Value::Gradient(_) => serde_json::from_value::(j.clone()) + .ok() + .filter(|v| matches!(v, Value::Gradient(g) if g.colors.len() <= MAX_GRADIENT_STOPS && g.opacities.len() <= MAX_GRADIENT_STOPS)) + .or_else(|| gradient_json(j).map(Value::Gradient)), } } /// JSON for display/automation (numbers/arrays for numeric kinds). diff --git a/crates/lottie/Cargo.toml b/crates/lottie/Cargo.toml index a9f64b3ba..9a95dca13 100644 --- a/crates/lottie/Cargo.toml +++ b/crates/lottie/Cargo.toml @@ -10,6 +10,7 @@ rust-version.workspace = true workspace = true [dependencies] +craft-lottie = { workspace = true } craft-docpath = { workspace = true } effectcraft-time = { workspace = true } effectcraft-color = { workspace = true } diff --git a/crates/lottie/src/anim.rs b/crates/lottie/src/anim.rs index 5b6dabf50..62550f86f 100644 --- a/crates/lottie/src/anim.rs +++ b/crates/lottie/src/anim.rs @@ -1,26 +1,20 @@ -//! Animated properties: EffectCraft keyframes ↔ Lottie keyframes. -//! -//! A Lottie keyframe holds the start value `s` of a segment and the segment's normalised cubic -//! Bezier ease: `o` (out tangent of this key) and `i` (in tangent of the next key), each with `x` -//! (time fraction) and `y` (value progress fraction), per dimension. After Effects' temporal ease -//! is speed (units/s) and influence (fraction of the segment): for a segment of duration `d` and -//! value change `Δv`, -//! -//! ```text -//! o.x = influence_out o.y = speed_out · influence_out · d / Δv -//! i.x = 1 − influence_in i.y = 1 − speed_in · influence_in · d / Δv -//! ``` -//! -//! which is exactly the Bezier the keyframe evaluator uses. Spatial properties ease along the -//! motion path (Δv = arc length, one dimension) and carry `to`/`ti` spatial tangents. Hold keys -//! set `h: 1`. +//! Animated properties: EffectCraft keyframes ↔ Lottie keyframes, through `craft-lottie`'s +//! property encoding ([`craft_lottie::read_prop`], [`craft_lottie::write_prop`]) and its +//! conversion between a segment's normalised Bezier ease and After Effects' speed and influence +//! ([`Tangents`]). Spatial properties ease along the motion path (Δv = arc length, one +//! dimension) and carry `to`/`ti` spatial tangents. +pub(crate) use craft_lottie::nums; +use craft_lottie::{Animated, Json, KeyOut, PropValue, Tangents, read_prop, round6}; use effectcraft_keyframe::{self as kf, Ease, Interp, Keyframe, Value}; use effectcraft_project::{Expression, Property}; -use serde_json::{Value as Json, json}; +use serde_json::json; use crate::Timebase; +/// Most distinct warnings an import or export reports. +const MAX_WARNINGS: usize = 200; + /// Export state shared by the whole document. pub(crate) struct Ex { pub tb: Timebase, @@ -31,7 +25,8 @@ pub(crate) struct Ex { impl Ex { pub fn warn(&mut self, w: impl Into) { let w = w.into(); - if !self.warnings.contains(&w) { + // Each warning is looked for among the earlier ones: past a screenful, more add nothing. + if self.warnings.len() < MAX_WARNINGS && !self.warnings.contains(&w) { self.warnings.push(w); } } @@ -46,7 +41,8 @@ pub(crate) struct Im { impl Im { pub fn warn(&mut self, w: impl Into) { let w = w.into(); - if !self.warnings.contains(&w) { + // Each warning is looked for among the earlier ones: past a screenful, more add nothing. + if self.warnings.len() < MAX_WARNINGS && !self.warnings.contains(&w) { self.warnings.push(w); } } @@ -57,15 +53,11 @@ fn secs(t: effectcraft_time::Tick) -> f64 { } fn r6(v: f64) -> f64 { - if v.is_finite() { (v * 1e6).round() / 1e6 } else { 0.0 } + round6(v) } -/// A key value as Lottie's `s` (numbers wrapped in an array, objects in a one-element array). -fn wrap(j: Json) -> Json { - match j { - Json::Array(_) => j, - other => json!([other]), - } +fn lottie_ease(e: Ease) -> craft_lottie::Ease { + craft_lottie::Ease { speed: e.speed, influence: e.influence } } fn is_spatial(p: &Property) -> bool { @@ -75,7 +67,7 @@ fn is_spatial(p: &Property) -> bool { /// Export a property as a Lottie animatable property (`{a, k}` plus `x` for expressions). /// `label` names the property in warnings. Muted keys are omitted: Lottie has no mute state, and /// the remaining keys carry the animation the renderer shows. -pub(crate) fn export_prop(ex: &mut Ex, p: &Property, conv: &dyn Fn(&Value) -> Json, label: &str) -> Json { +pub(crate) fn export_prop(ex: &mut Ex, p: &Property, conv: &dyn Fn(&Value) -> Json, label: &str) -> Animated { let muted = p.keys.iter().any(|k| k.muted); if muted { ex.warn(format!("{label}: muted keyframes are not exported")); @@ -87,146 +79,102 @@ pub(crate) fn export_prop(ex: &mut Ex, p: &Property, conv: &dyn Fn(&Value) -> Js } else { &p.keys }; - let mut out = - if keys.is_empty() { json!({"a": 0, "k": conv(&p.value)}) } else { json!({"a": 1, "k": export_keys(ex, keys, is_spatial(p), p.hold_only, conv)}) }; + let mut expression = None; if let Some(e) = p.expr.as_ref().filter(|e| e.enabled && !e.text.trim().is_empty()) { if ex.include_expressions { - out["x"] = json!(e.text); + expression = Some(e.text.as_str()); } else { ex.warn(format!("{label}: expression not exported (enable includeExpressions to export it as `x`)")); } } - out + if keys.is_empty() { + return Animated(craft_lottie::write_prop(conv(&p.value), &[], expression)); + } + Animated::keyed(&export_keys(ex, keys, is_spatial(p), p.hold_only, conv), expression) } -fn export_keys(ex: &Ex, keys: &[Keyframe], spatial: bool, hold_only: bool, conv: &dyn Fn(&Value) -> Json) -> Vec { +fn export_keys(ex: &Ex, keys: &[Keyframe], spatial: bool, hold_only: bool, conv: &dyn Fn(&Value) -> Json) -> Vec { let mut out = Vec::with_capacity(keys.len()); for (i, k) in keys.iter().enumerate() { - let mut o = json!({"t": r6(ex.tb.frame(k.time)), "s": wrap(conv(&k.value))}); - if i + 1 == keys.len() { - if k.out_interp == Interp::Hold || hold_only { - o["h"] = json!(1); + let mut o = KeyOut { t: ex.tb.frame(k.time), s: conv(&k.value), hold: false, ease: vec![], spatial: None }; + let last = i + 1 == keys.len(); + if k.out_interp == Interp::Hold || hold_only || (!last && !k.value.interpolates()) { + o.hold = true; + } else if !last { + o.ease = segment_ease(keys, i, spatial); + if spatial { + let (_, to) = kf::spatial_tangents(keys, i); + let (ti, _) = kf::spatial_tangents(keys, i + 1); + let n = if matches!(k.value, Value::Vec2(_)) { 2 } else { conv(&k.value).as_array().map(Vec::len).unwrap_or(3).min(3) }; + o.spatial = Some((to.get(..n).unwrap_or_default().to_vec(), ti.get(..n).unwrap_or_default().to_vec())); } - out.push(o); - continue; - } - if k.out_interp == Interp::Hold || hold_only || !k.value.interpolates() { - o["h"] = json!(1); - out.push(o); - continue; - } - let (ox, oy, ix, iy) = segment_ease(keys, i, spatial); - o["o"] = json!({"x": ox, "y": oy}); - o["i"] = json!({"x": ix, "y": iy}); - if spatial { - let (_, to) = kf::spatial_tangents(keys, i); - let (ti, _) = kf::spatial_tangents(keys, i + 1); - let n = if matches!(k.value, Value::Vec2(_)) { 2 } else { conv(&k.value).as_array().map(Vec::len).unwrap_or(3).min(3) }; - o["to"] = json!(to[..n].iter().map(|v| r6(*v)).collect::>()); - o["ti"] = json!(ti[..n].iter().map(|v| r6(*v)).collect::>()); } out.push(o); } out } -/// Normalised ease of segment `i → i+1`: (o.x, o.y, i.x, i.y) per dimension. -fn segment_ease(keys: &[Keyframe], i: usize, spatial: bool) -> (Vec, Vec, Vec, Vec) { - let (a, b) = (&keys[i], &keys[i + 1]); +/// Normalised ease of segment `i → i+1`, one entry per dimension. +fn segment_ease(keys: &[Keyframe], i: usize, spatial: bool) -> Vec { + let (Some(a), Some(b)) = (keys.get(i), keys.get(i + 1)) else { return vec![] }; let dur = (secs(b.time) - secs(a.time)).max(1e-12); let ca = a.value.components(); let cb = b.value.components(); let numeric = !ca.is_empty() && ca.len() == cb.len(); let dims = if spatial || !numeric { 1 } else { ca.len() }; - let (mut ox, mut oy, mut ix, mut iy) = (vec![], vec![], vec![], vec![]); - for d in 0..dims { - let (dv, eo, ei) = if spatial { - let len = kf::spatial_segment_length(keys, i); - (len, kf::side_ease(keys, i, 0, true, true), kf::side_ease(keys, i + 1, 0, true, false)) - } else if numeric { - (cb[d] - ca[d], kf::side_ease(keys, i, d, false, true), kf::side_ease(keys, i + 1, d, false, false)) - } else { - // Paths, gradients: overall progress 0 → 1. - let side = |k: &Keyframe, out: bool| { - let interp = if out { k.out_interp } else { k.in_interp }; - let list = if out { &k.out_ease } else { &k.in_ease }; - if interp == Interp::Bezier { - list.first().copied().unwrap_or_default() - } else { - Ease { speed: 1.0 / dur, influence: kf::LINEAR_SIDE_INFLUENCE } - } + (0..dims) + .map(|d| { + let (dv, eo, ei) = if spatial { + let len = kf::spatial_segment_length(keys, i); + (len, kf::side_ease(keys, i, 0, true, true), kf::side_ease(keys, i + 1, 0, true, false)) + } else if numeric { + (cb[d] - ca[d], kf::side_ease(keys, i, d, false, true), kf::side_ease(keys, i + 1, d, false, false)) + } else { + // Paths, gradients: overall progress 0 → 1. + let side = |k: &Keyframe, out: bool| { + let interp = if out { k.out_interp } else { k.in_interp }; + let list = if out { &k.out_ease } else { &k.in_ease }; + if interp == Interp::Bezier { + list.first().copied().unwrap_or_default() + } else { + Ease { speed: 1.0 / dur, influence: kf::LINEAR_SIDE_INFLUENCE } + } + }; + (1.0, Some(side(a, true)), Some(side(b, false))) }; - (1.0, Some(side(a, true)), Some(side(b, false))) - }; - let lin = Ease { speed: dv / dur, influence: kf::LINEAR_SIDE_INFLUENCE }; - let (eo, ei) = (eo.unwrap_or(lin), ei.unwrap_or(lin)); - // Crossing time handles must retain the independently specified influences. - let (i0, i1) = (eo.influence.clamp(0.0, 1.0), ei.influence.clamp(0.0, 1.0)); - let (y0, y1) = if dv.abs() > 1e-12 { (eo.speed * i0 * dur / dv, 1.0 - ei.speed * i1 * dur / dv) } else { (0.0, 1.0) }; - ox.push(r6(i0)); - oy.push(r6(y0)); - ix.push(r6(1.0 - i1)); - iy.push(r6(y1)); - } - (ox, oy, ix, iy) + let lin = Ease { speed: dv / dur, influence: kf::LINEAR_SIDE_INFLUENCE }; + // Crossing time handles keep the independently specified influences. + Tangents::from_speed(lottie_ease(eo.unwrap_or(lin)), lottie_ease(ei.unwrap_or(lin)), dv, dur) + }) + .collect() } // ---------------------------------------------------------------- import -/// Whether a Lottie property value is keyframed. -pub(crate) fn is_animated(j: &Json) -> bool { - if j.get("a").and_then(Json::as_i64) == Some(1) { - return true; - } - j.get("k").and_then(Json::as_array).and_then(|a| a.first()).is_some_and(|f| f.get("t").is_some()) -} - -/// Component `d` of an ease tangent field (`x` / `y` may be a number or an array). -fn comp(j: &Json, field: &str, d: usize) -> Option { - match j.get(field)? { - Json::Number(n) => n.as_f64(), - Json::Array(a) => a.get(d).or(a.first()).and_then(Json::as_f64), - _ => None, - } -} - -fn vec3_of(j: Option<&Json>) -> [f64; 3] { - let a = j.and_then(Json::as_array); - let g = |i: usize| a.and_then(|a| a.get(i)).and_then(Json::as_f64).unwrap_or(0.0); - [g(0), g(1), g(2)] -} - /// Import a Lottie animatable property into `p` (value, keys, expression). `conv` turns a Lottie /// value (as found in `k` or a key's `s`, possibly wrapped in a one-element array) into a value of /// the property's type. pub(crate) fn import_prop(im: &Im, j: &Json, p: &mut Property, conv: &dyn Fn(&Json) -> Option) { - if !j.is_object() { - if let Some(v) = conv(j) { - p.value = v; - } - return; - } - if let Some(x) = j.get("x").and_then(Json::as_str).filter(|x| !x.trim().is_empty()) { + let prop = read_prop(j); + if let Some(x) = prop.expression { p.expr = Some(Expression { text: x.to_string(), enabled: true }); } - let Some(k) = j.get("k") else { return }; - if !is_animated(j) { - if let Some(v) = conv(k) { - p.value = v; + let list = match prop.value { + PropValue::None => return, + PropValue::Static(v) => { + if let Some(v) = conv(v) { + p.value = v; + } + return; } - return; - } - let Some(list) = k.as_array() else { return }; + PropValue::Keys(list) => list, + }; let spatial = is_spatial(p); let mut keys: Vec = Vec::with_capacity(list.len()); - let mut kept: Vec<&Json> = Vec::with_capacity(list.len()); - let mut prev_end: Option<&Json> = None; + let mut kept = Vec::with_capacity(list.len()); for kj in list { - let t = kj.get("t").and_then(Json::as_f64).unwrap_or(0.0); - let sv = kj.get("s").or(prev_end); - prev_end = kj.get("e"); - let Some(v) = sv.and_then(conv) else { continue }; - let mut key = Keyframe::new(im.tb.tick(t), v); + let Some(v) = kj.s.and_then(conv) else { continue }; + let mut key = Keyframe::new(im.tb.tick(kj.t), v); key.spatial_auto = false; if p.hold_only || !key.value.interpolates() { key = key.hold(); @@ -240,16 +188,14 @@ pub(crate) fn import_prop(im: &Im, j: &Json, p: &mut Property, conv: &dyn Fn(&Js let n = keys.len(); for i in 0..n.saturating_sub(1) { let Some(kj) = kept.get(i) else { break }; - if kj.get("h").and_then(Json::as_i64) == Some(1) || p.hold_only { + if kj.hold || p.hold_only { keys[i].out_interp = Interp::Hold; keys[i + 1].in_interp = Interp::Hold; continue; } if spatial { - let to = vec3_of(kj.get("to")); - let ti = vec3_of(kj.get("ti")); - keys[i].spatial_out = to; - keys[i + 1].spatial_in = ti; + keys[i].spatial_out = kj.to; + keys[i + 1].spatial_in = kj.ti; } } // Eases need the spatial tangents (arc length), so they come second. @@ -258,7 +204,7 @@ pub(crate) fn import_prop(im: &Im, j: &Json, p: &mut Property, conv: &dyn Fn(&Js if keys[i].out_interp == Interp::Hold { continue; } - let (Some(o), Some(ii)) = (kj.get("o"), kj.get("i")) else { + let (Some(o), Some(ii)) = (kj.o, kj.i) else { keys[i].out_interp = Interp::Linear; keys[i + 1].in_interp = Interp::Linear; continue; @@ -272,13 +218,8 @@ pub(crate) fn import_prop(im: &Im, j: &Json, p: &mut Property, conv: &dyn Fn(&Js let mut outs = vec![]; let mut ins = vec![]; for d in 0..dims { - let ox = comp(o, "x", d).unwrap_or(1.0 / 3.0).clamp(0.0, 1.0); - let oy = comp(o, "y", d).unwrap_or(ox); - let ix = comp(ii, "x", d).unwrap_or(2.0 / 3.0).clamp(0.0, 1.0); - let iy = comp(ii, "y", d).unwrap_or(ix); - if (ox - oy).abs() > 1e-4 || (ix - iy).abs() > 1e-4 { - linear = false; - } + let t = Tangents::read(o, ii, d); + linear &= t.is_linear(); let dv = if spatial { kf::spatial_segment_length(&keys, i) } else if numeric { @@ -286,10 +227,9 @@ pub(crate) fn import_prop(im: &Im, j: &Json, p: &mut Property, conv: &dyn Fn(&Js } else { 1.0 }; - let inf_o = ox.max(0.001); - let inf_i = (1.0 - ix).max(0.001); - outs.push(Ease { speed: oy * dv / (inf_o * dur), influence: inf_o }); - ins.push(Ease { speed: (1.0 - iy) * dv / (inf_i * dur), influence: inf_i }); + let (out, into) = t.to_speed(dv, dur); + outs.push(Ease { speed: out.speed, influence: out.influence }); + ins.push(Ease { speed: into.speed, influence: into.influence }); } if linear { keys[i].out_interp = Interp::Linear; @@ -305,6 +245,13 @@ pub(crate) fn import_prop(im: &Im, j: &Json, p: &mut Property, conv: &dyn Fn(&Js p.keys = keys; } +/// [`import_prop`] for a property of the model ([`Animated`]). +pub(crate) fn import_animated(im: &Im, a: Option<&Animated>, p: &mut Property, conv: &dyn Fn(&Json) -> Option) { + if let Some(a) = a { + import_prop(im, &a.0, p, conv); + } +} + // ---------------------------------------------------------------- value converters pub(crate) fn num(v: &Value) -> Json { @@ -330,23 +277,6 @@ pub(crate) fn arr3(v: &Value) -> Json { json!([r6(*c.first().unwrap_or(&0.0)), r6(*c.get(1).unwrap_or(&0.0)), r6(*c.get(2).unwrap_or(&0.0))]) } -/// Numbers of a Lottie value (`5`, `[5]`, `[1, 2, 3]`). -pub(crate) fn nums(j: &Json) -> Option> { - match j { - Json::Number(n) => Some(vec![n.as_f64()?]), - Json::Array(a) => { - // `[[x, y]]`-style wrapping. - if a.len() == 1 - && let Some(inner @ Json::Array(_)) = a.first() - { - return nums(inner); - } - a.iter().map(Json::as_f64).collect() - } - _ => None, - } -} - pub(crate) fn to_scalar(j: &Json) -> Option { nums(j).and_then(|v| v.first().copied()).map(Value::Scalar) } @@ -363,15 +293,8 @@ pub(crate) fn to_vec3(j: &Json, z: f64) -> Option { } pub(crate) fn to_color(j: &Json) -> Option { - let v = nums(j)?; - let mut c = [*v.first()?, *v.get(1).unwrap_or(&0.0), *v.get(2).unwrap_or(&0.0), *v.get(3).unwrap_or(&1.0)]; - // Some files use 0–255. - if c[..3].iter().any(|x| *x > 1.0) { - for x in &mut c[..3] { - *x /= 255.0; - } - } - Some(Value::Color(c)) + // Some files use 0–255: `craft_lottie::color` divides those. + craft_lottie::color(j).map(Value::Color) } pub(crate) fn to_bool(j: &Json) -> Option { diff --git a/crates/lottie/src/base64.rs b/crates/lottie/src/base64.rs deleted file mode 100644 index b45f86d4a..000000000 --- a/crates/lottie/src/base64.rs +++ /dev/null @@ -1,69 +0,0 @@ -//! Standard base64 (RFC 4648 §4) for embedded image assets. - -const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -pub fn encode(data: &[u8]) -> String { - let mut out = String::with_capacity(data.len().div_ceil(3) * 4); - for chunk in data.chunks(3) { - let b = [chunk[0], *chunk.get(1).unwrap_or(&0), *chunk.get(2).unwrap_or(&0)]; - let n = (b[0] as u32) << 16 | (b[1] as u32) << 8 | b[2] as u32; - for i in 0..4 { - if i <= chunk.len() { - out.push(ALPHABET[(n >> (18 - 6 * i) & 63) as usize] as char); - } else { - out.push('='); - } - } - } - out -} - -pub fn decode(s: &str) -> Option> { - let val = |c: u8| -> Option { - Some(match c { - b'A'..=b'Z' => c - b'A', - b'a'..=b'z' => c - b'a' + 26, - b'0'..=b'9' => c - b'0' + 52, - b'+' | b'-' => 62, - b'/' | b'_' => 63, - _ => return None, - } as u32) - }; - let clean: Vec = s.bytes().filter(|c| !c.is_ascii_whitespace() && *c != b'=').collect(); - let mut out = Vec::with_capacity(clean.len() * 3 / 4); - for chunk in clean.chunks(4) { - let mut n = 0u32; - for (i, &c) in chunk.iter().enumerate() { - n |= val(c)? << (18 - 6 * i); - } - let bytes = [(n >> 16) as u8, (n >> 8) as u8, n as u8]; - match chunk.len() { - 4 => out.extend_from_slice(&bytes), - 3 => out.extend_from_slice(&bytes[..2]), - 2 => out.push(bytes[0]), - _ => return None, - } - } - Some(out) -} - -/// Split a `data:;base64,` URI into (mime, bytes). -pub fn data_uri(s: &str) -> Option<(String, Vec)> { - let rest = s.strip_prefix("data:")?; - let (head, payload) = rest.split_once(',')?; - let mime = head.strip_suffix(";base64")?; - Some((mime.to_string(), decode(payload)?)) -} - -#[cfg(test)] -mod tests { - #[test] - fn roundtrip() { - for s in ["", "f", "fo", "foo", "foob", "fooba", "foobar"] { - let e = super::encode(s.as_bytes()); - assert_eq!(super::decode(&e).unwrap(), s.as_bytes()); - } - assert_eq!(super::encode(b"foobar"), "Zm9vYmFy"); - assert_eq!(super::encode(b"fo"), "Zm8="); - } -} diff --git a/crates/lottie/src/export.rs b/crates/lottie/src/export.rs index eeb6e2737..99ca28e7e 100644 --- a/crates/lottie/src/export.rs +++ b/crates/lottie/src/export.rs @@ -2,11 +2,14 @@ use std::collections::BTreeSet; +use craft_lottie::model::{Asset, Effect, Font, Fonts, Layer as LottieLayer, LayerContent, Marker, Mask, PrecompLayer, SolidLayer, Transform}; +use craft_lottie::{Animated, Animation, Json}; use effectcraft_keyframe::Value; use effectcraft_project::{Comp, FootageKind, GroupKind, ItemId, ItemKind, Layer, LayerSource, MaskMode, MatteKind, Project, PropGroup}; -use serde_json::{Map, Value as Json, json}; +use serde_json::json; use crate::anim::{self, Ex, export_prop}; +use crate::shapes::fixed; use crate::{LottieError, Timebase, shapes, text}; /// Export settings. @@ -134,28 +137,15 @@ struct Doc<'a> { opts: &'a ExportOptions, read: &'a dyn Fn(&str) -> Option>, ex: Ex, - assets: Vec, + assets: Vec, done: BTreeSet, - fonts: Vec, + fonts: Vec, any_3d: bool, } -fn hex(c: [f32; 3]) -> String { - let b = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; - format!("#{:02x}{:02x}{:02x}", b(c[0]), b(c[1]), b(c[2])) -} - -fn mime_of(path: &str) -> &'static str { - let lower = path.to_ascii_lowercase(); - if lower.ends_with(".jpg") || lower.ends_with(".jpeg") { - "image/jpeg" - } else if lower.ends_with(".webp") { - "image/webp" - } else if lower.ends_with(".gif") { - "image/gif" - } else { - "image/png" - } +/// The image type `bytes` hold, by their signature; `None` for anything else. +pub(crate) fn image_mime(bytes: &[u8]) -> Option<&'static str> { + craft_lottie::image_mime(bytes) } impl Doc<'_> { @@ -163,84 +153,68 @@ impl Doc<'_> { self.ex.tb.frame(t) } - fn prop(&mut self, g: &PropGroup, m: &str, conv: &dyn Fn(&Value) -> Json, label: &str) -> Option { + fn prop(&mut self, g: &PropGroup, m: &str, conv: &dyn Fn(&Value) -> Json, label: &str) -> Option { let p = g.get(m)?; Some(export_prop(&mut self.ex, p, conv, &format!("{label} ▸ {}", p.name))) } - fn transform(&mut self, l: &Layer) -> Json { - let mut ks = Map::new(); - let Some(tr) = l.transform() else { return json!({}) }; + fn transform(&mut self, l: &Layer) -> Transform { + let mut ks = Transform::default(); + let Some(tr) = l.transform() else { return ks }; let label = format!("{} ▸ Transform", l.name); let three = l.is_3d(); - if let Some(a) = self.prop(tr, "anchor", &anim::arr3, &label) { - ks.insert("a".into(), a); - } + ks.anchor = self.prop(tr, "anchor", &anim::arr3, &label); if tr.get("positionX").is_some() { let mut p = json!({"s": true}); for (k, m) in [("x", "positionX"), ("y", "positionY"), ("z", "positionZ")] { if (k != "z" || three) && let Some(v) = self.prop(tr, m, &anim::num, &label) { - p[k] = v; + p[k] = v.0; } } - ks.insert("p".into(), p); - } else if let Some(p) = self.prop(tr, "position", &anim::arr3, &label) { - ks.insert("p".into(), p); - } - if let Some(s) = self.prop(tr, "scale", &anim::arr3, &label) { - ks.insert("s".into(), s); + ks.position = Some(Animated(p)); + } else { + ks.position = self.prop(tr, "position", &anim::arr3, &label); } + ks.scale = self.prop(tr, "scale", &anim::arr3, &label); if three { - for (k, m) in [("rx", "rotationX"), ("ry", "rotationY"), ("rz", "rotation")] { - if let Some(v) = self.prop(tr, m, &anim::num, &label) { - ks.insert(k.into(), v); - } - } - if let Some(v) = self.prop(tr, "orientation", &anim::arr3, &label) { - ks.insert("or".into(), v); - } - } else if let Some(r) = self.prop(tr, "rotation", &anim::num, &label) { - ks.insert("r".into(), r); + ks.rotation_x = self.prop(tr, "rotationX", &anim::num, &label); + ks.rotation_y = self.prop(tr, "rotationY", &anim::num, &label); + ks.rotation_z = self.prop(tr, "rotation", &anim::num, &label); + ks.orientation = self.prop(tr, "orientation", &anim::arr3, &label); + } else { + ks.rotation = self.prop(tr, "rotation", &anim::num, &label); } - if let Some(o) = self.prop(tr, "opacity", &anim::num, &label) { - ks.insert("o".into(), o); - } - Json::Object(ks) + ks.opacity = self.prop(tr, "opacity", &anim::num, &label); + ks } - fn masks(&mut self, l: &Layer) -> Vec { + fn masks(&mut self, l: &Layer) -> Vec { let Some(masks) = l.masks() else { return vec![] }; let mut out = vec![]; for m in masks.groups() { let GroupKind::Mask { mode, inverted, .. } = m.kind else { continue }; let label = format!("{} ▸ {}", l.name, m.name); - let mut o = json!({"nm": m.name, "mode": mask_mode(mode), "inv": inverted}); - if let Some(v) = self.prop(m, "path", &shapes::path_value, &label) { - o["pt"] = v; - } - if let Some(v) = self.prop(m, "opacity", &anim::num, &label) { - o["o"] = v; - } - if let Some(v) = self.prop(m, "expansion", &anim::num, &label) { - o["x"] = v; - } + let mut o = Mask { name: Some(m.name.clone()), mode: Some(mask_mode(mode).into()), inverted: Some(inverted), ..Default::default() }; + o.path = self.prop(m, "path", &shapes::path_value, &label); + o.opacity = self.prop(m, "opacity", &anim::num, &label); + o.expansion = self.prop(m, "expansion", &anim::num, &label); if let Some(f) = m.get("feather") { if f.is_animated() || f.value.components().iter().any(|v| *v != 0.0) { self.ex.warn(format!("{label}: mask feather is exported as `f` (not every Lottie player renders it)")); } - o["f"] = export_prop(&mut self.ex, f, &anim::arr2, &format!("{label} ▸ Mask Feather")); + o.extra.insert("f".into(), export_prop(&mut self.ex, f, &anim::arr2, &format!("{label} ▸ Mask Feather")).0); } if !m.enabled { - o["mode"] = json!("n"); + o.mode = Some("n".into()); } out.push(o); } out } - fn effects(&mut self, l: &Layer) -> Vec { + fn effects(&mut self, l: &Layer) -> Vec { let Some(fx) = l.effects() else { return vec![] }; let mut out = vec![]; for (i, g) in fx.groups().enumerate() { @@ -266,11 +240,27 @@ impl Doc<'_> { }; (p.name.clone(), export_prop(&mut self.ex, p, &conv, &format!("{label} ▸ {}", p.name))) } - None => (format!("Param {}", pi + 1), json!({"a": 0, "k": konst})), + None => (format!("Param {}", pi + 1), fixed(json!(konst))), }; - params.push(json!({"ty": ty, "nm": nm, "mn": m, "ix": pi + 1, "v": v})); + params.push(Effect { + kind: Some(i64::from(*ty)), + name: Some(nm), + match_name: Some((*m).into()), + index: Some(pi as i64 + 1), + value: Some(v), + ..Default::default() + }); } - out.push(json!({"ty": map.ty, "nm": g.name, "mn": effect, "ix": i + 1, "en": g.enabled as u8, "np": params.len(), "ef": params})); + out.push(Effect { + kind: Some(i64::from(map.ty)), + name: Some(g.name.clone()), + match_name: Some(effect.clone()), + index: Some(i as i64 + 1), + enabled: Some(i64::from(g.enabled)), + count: Some(params.len() as i64), + values: Some(params), + ..Default::default() + }); } out } @@ -286,8 +276,22 @@ impl Doc<'_> { self.ex.warn(format!("{}: cannot read image `{}` (layer exported as a null)", l.name, f.path)); return None; }; - let uri = format!("data:{};base64,{}", mime_of(&f.path), crate::base64::encode(&bytes)); - self.assets.push(json!({"id": id, "nm": it.name, "w": f.width, "h": f.height, "u": "", "p": uri, "e": 1})); + // What a project calls a still is embedded only when it is one: the path can name any + // file (an imported animation's asset), and the export would carry its bytes verbatim. + let Some(mime) = image_mime(&bytes) else { + self.ex.warn(format!("{}: `{}` is not a PNG, JPEG, GIF or WebP image (layer exported as a null)", l.name, f.path)); + return None; + }; + self.assets.push(Asset { + id: Some(id.clone()), + name: Some(it.name.clone()), + width: Some(f64::from(f.width)), + height: Some(f64::from(f.height)), + dir: Some(String::new()), + path: Some(craft_lottie::base64::to_data_uri(mime, &bytes)), + embedded: Some(1), + ..Default::default() + }); self.done.insert(id.clone()); Some(id) } @@ -301,11 +305,19 @@ impl Doc<'_> { let c = it.as_comp()?; self.done.insert(id.clone()); let layers = self.layers(c); - self.assets.push(json!({"id": id, "nm": it.name, "fr": c.frame_rate.as_f64(), "w": c.width, "h": c.height, "layers": layers})); + self.assets.push(Asset { + id: Some(id.clone()), + name: Some(it.name.clone()), + frame_rate: Some(c.frame_rate.as_f64()), + width: Some(f64::from(c.width)), + height: Some(f64::from(c.height)), + layers: Some(layers), + ..Default::default() + }); Some(id) } - fn layers(&mut self, comp: &Comp) -> Vec { + fn layers(&mut self, comp: &Comp) -> Vec { let mattes: BTreeSet<_> = comp.layers.iter().filter_map(|l| l.track_matte.map(|m| m.layer)).collect(); let mut out = vec![]; for (idx, l) in comp.layers.iter().enumerate() { @@ -314,11 +326,9 @@ impl Doc<'_> { out } - fn layer(&mut self, comp: &Comp, idx: usize, l: &Layer, is_matte: bool) -> Json { + fn layer(&mut self, comp: &Comp, idx: usize, l: &Layer, is_matte: bool) -> LottieLayer { let name = l.name.clone(); - let mut o = Map::new(); - o.insert("ind".into(), json!(idx + 1)); - o.insert("nm".into(), json!(name)); + let mut o = LottieLayer { index: Some(idx as i64 + 1), name: Some(name.clone()), ..Default::default() }; let three = l.is_3d(); if three { self.any_3d = true; @@ -326,34 +336,30 @@ impl Doc<'_> { self.ex.warn(format!("{name}: 3D layer exported with ddd: 1 (most Lottie players render 2D only)")); } } - o.insert("ddd".into(), json!(three as u8)); + o.three_d = Some(i64::from(three)); let mut hidden = !l.switches.video || l.switches.guide; - let mut ty = 3; + o.content = LayerContent::Null; match &l.source { LayerSource::Shape => { - ty = 4; let items = l.props.sub("contents").map(|c| shapes::export_items(&mut self.ex, c, &name)).unwrap_or_default(); - o.insert("shapes".into(), json!(items)); + o.content = LayerContent::Shape(items); } LayerSource::Text => { if self.opts.text_as_shapes { - ty = 4; - let items = text::glyph_shapes(&mut self.ex, l); - o.insert("shapes".into(), json!(items)); + o.content = LayerContent::Shape(text::glyph_shapes(&mut self.ex, l)); } else { - ty = 5; - let t = text::export_text(&mut self.ex, l, &mut self.fonts); - o.insert("t".into(), t); + o.content = LayerContent::Text(Box::new(text::export_text(&mut self.ex, l, &mut self.fonts))); } } LayerSource::Solid { item } => { let sol = self.project.item(*item).and_then(|i| if let ItemKind::Solid(s) = &i.kind { Some(s.clone()) } else { None }); match sol { Some(s) if !l.switches.adjustment => { - ty = 1; - o.insert("sc".into(), json!(hex(s.color))); - o.insert("sw".into(), json!(s.width)); - o.insert("sh".into(), json!(s.height)); + o.content = LayerContent::Solid(SolidLayer { + color: Some(craft_lottie::to_hex(s.color)), + width: Some(f64::from(s.width)), + height: Some(f64::from(s.height)), + }); } _ => { self.ex.warn(format!("{name}: adjustment layers are not part of Lottie (exported as a null)")); @@ -363,15 +369,9 @@ impl Doc<'_> { } LayerSource::Comp { item } => { if let Some(id) = self.precomp_asset(*item) { - ty = 0; let (w, h) = self.project.comp(*item).map(|c| (c.width, c.height)).unwrap_or((comp.width, comp.height)); - o.insert("refId".into(), json!(id)); - o.insert("w".into(), json!(w)); - o.insert("h".into(), json!(h)); - if let Some(tm) = l.props.get("timeRemap") { - let v = export_prop(&mut self.ex, tm, &anim::num, &format!("{name} ▸ Time Remap")); - o.insert("tm".into(), v); - } + let time_remap = l.props.get("timeRemap").map(|tm| export_prop(&mut self.ex, tm, &anim::num, &format!("{name} ▸ Time Remap"))); + o.content = LayerContent::Precomp(PrecompLayer { ref_id: Some(id), width: Some(f64::from(w)), height: Some(f64::from(h)), time_remap }); } } LayerSource::Footage { item } => { @@ -379,8 +379,7 @@ impl Doc<'_> { match kind { Some((FootageKind::Still, true)) => { if let Some(id) = self.image_asset(*item, l) { - ty = 2; - o.insert("refId".into(), json!(id)); + o.content = LayerContent::Image(Some(id)); } else { hidden = true; } @@ -405,20 +404,16 @@ impl Doc<'_> { hidden = true; } } - o.insert("ty".into(), json!(ty)); - if let Some(p) = l.parent.and_then(|p| comp.index_of(p)) { - o.insert("parent".into(), json!(p)); - } - o.insert("sr".into(), json!(l.stretch / 100.0)); + o.parent = l.parent.and_then(|p| comp.index_of(p)).map(|p| p as i64); + o.stretch = Some(l.stretch / 100.0); if l.stretch < 0.0 { self.ex.warn(format!("{name}: reversed time stretch is not supported by most Lottie players")); } - let ks = self.transform(l); - o.insert("ks".into(), ks); - o.insert("ao".into(), json!((l.auto_orient == effectcraft_project::AutoOrient::AlongPath) as u8)); - o.insert("ip".into(), json!(self.frame(l.in_point))); - o.insert("op".into(), json!(self.frame(l.out_point))); - o.insert("st".into(), json!(self.frame(l.start_time))); + o.transform = Some(self.transform(l)); + o.auto_orient = Some(i64::from(l.auto_orient == effectcraft_project::AutoOrient::AlongPath)); + o.in_point = Some(self.frame(l.in_point)); + o.out_point = Some(self.frame(l.out_point)); + o.start_time = Some(self.frame(l.start_time)); let bm = match crate::blend_to_lottie(l.blend_mode) { Some(b) => b, None => { @@ -426,32 +421,32 @@ impl Doc<'_> { 0 } }; - o.insert("bm".into(), json!(bm)); + o.blend_mode = Some(i64::from(bm)); let masks = self.masks(l); if !masks.is_empty() { - o.insert("hasMask".into(), json!(true)); - o.insert("masksProperties".into(), json!(masks)); + o.has_mask = Some(true); + o.masks = Some(masks); } let ef = self.effects(l); if !ef.is_empty() { - o.insert("ef".into(), json!(ef)); + o.effects = Some(ef); } if let Some(tm) = l.track_matte && let Some(mi) = comp.index_of(tm.layer) { - o.insert("tt".into(), json!(matte_code(tm.kind))); - o.insert("tp".into(), json!(mi)); + o.matte_mode = Some(i64::from(matte_code(tm.kind))); + o.matte_parent = Some(mi as i64); if mi != idx { self.ex.warn(format!("{name}: track matte layer is not directly above (players without `tp` support use the layer above)")); } } if is_matte { - o.insert("td".into(), json!(1)); + o.is_matte = Some(1); if l.switches.video { self.ex.warn(format!("{name}: visible track matte layer (Lottie always hides matte layers)")); } } else if hidden { - o.insert("hd".into(), json!(true)); + o.hidden = Some(true); } if !l.markers.is_empty() { self.ex.warn(format!("{name}: layer markers are not part of Lottie (composition markers are exported)")); @@ -463,7 +458,7 @@ impl Doc<'_> { if l.preserve_transparency { self.ex.warn(format!("{name}: Preserve Underlying Transparency is not part of Lottie")); } - Json::Object(o) + o } } @@ -485,24 +480,27 @@ pub fn export_comp(project: &Project, comp: ItemId, opts: &ExportOptions, read: }; doc.done.insert(format!("comp_{}", comp.0)); let layers = doc.layers(c); - let markers: Vec = c.markers.iter().map(|m| json!({"tm": doc.frame(m.time), "cm": m.comment, "dr": doc.frame(m.duration)})).collect(); - let mut out = json!({ - "v": "5.12.0", - "fr": c.frame_rate.as_f64(), - "ip": 0, - "op": doc.frame(c.duration), - "w": c.width, - "h": c.height, - "nm": item.name, - "ddd": doc.any_3d as u8, - "assets": doc.assets, - "layers": layers, - "markers": markers, - }); - if !doc.fonts.is_empty() { - out["fonts"] = json!({"list": doc.fonts}); - } - Ok(ExportResult { json: out, warnings: doc.ex.warnings }) + let markers: Vec = c + .markers + .iter() + .map(|m| Marker { time: Some(doc.frame(m.time)), comment: Some(m.comment.clone()), duration: Some(doc.frame(m.duration)), ..Default::default() }) + .collect(); + let animation = Animation { + version: Some("5.12.0".into()), + frame_rate: Some(c.frame_rate.as_f64()), + in_point: Some(0.0), + out_point: Some(doc.frame(c.duration)), + width: Some(f64::from(c.width)), + height: Some(f64::from(c.height)), + name: Some(item.name.clone()), + three_d: Some(i64::from(doc.any_3d)), + assets: Some(doc.assets), + layers: Some(layers), + markers: Some(markers), + fonts: (!doc.fonts.is_empty()).then(|| Fonts { list: Some(doc.fonts), ..Default::default() }), + ..Default::default() + }; + Ok(ExportResult { json: animation.to_json(), warnings: doc.ex.warnings }) } /// Package an export as a dotLottie archive (`manifest.json` + `animations/.json`). diff --git a/crates/lottie/src/import.rs b/crates/lottie/src/import.rs index 57ef6427a..6534727dc 100644 --- a/crates/lottie/src/import.rs +++ b/crates/lottie/src/import.rs @@ -1,8 +1,10 @@ //! Lottie document → EffectCraft compositions (the inverse of [`crate::export`]). -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashMap}; use std::sync::Arc; +use craft_lottie::model::{Asset, Effect, Layer as LottieLayer, LayerContent, Mask, Transform}; +use craft_lottie::{Animated, Animation, Json}; use effectcraft_color::Label; use effectcraft_keyframe::{ShapePath, Value}; use effectcraft_project::build::{self, Ids}; @@ -11,12 +13,14 @@ use effectcraft_project::{ PropGroup, Property, Solid, TrackMatte, }; use effectcraft_time::{FrameRate, Tick}; -use serde_json::Value as Json; -use crate::anim::{self, Im, import_prop}; +use crate::anim::{self, Im, import_animated, import_prop}; use crate::export::{fx_by_id, fx_by_type, fx_params, mask_mode_from}; use crate::{LottieError, Timebase, shapes, text}; +/// Longest font family or style name kept (each text key using the font holds a copy). +const MAX_FONT_NAME_CHARS: usize = 256; + /// An embedded image asset handed to the caller to store (the returned path becomes the /// footage item's file). #[derive(Clone, Debug)] @@ -37,24 +41,15 @@ pub struct ImportResult { pub warnings: Vec, } -fn f(j: &Json, k: &str) -> Option { - j.get(k).and_then(Json::as_f64) -} - -fn s<'a>(j: &'a Json, k: &str) -> Option<&'a str> { - j.get(k).and_then(Json::as_str) -} - -fn hex_color(sc: &str) -> [f32; 3] { - let h = sc.trim_start_matches('#'); - let c = |i: usize| { - h.get(i..i + 2).filter(|x| x.bytes().all(|b| b.is_ascii_hexdigit())).and_then(|x| u8::from_str_radix(x, 16).ok()).unwrap_or(0) as f32 / 255.0 +/// The Lottie document inside raw bytes (plain JSON, or a stored dotLottie archive), read into +/// `craft-lottie`'s model within its default limits. +fn document(bytes: &[u8]) -> Result { + let read = |b: &[u8]| { + Animation::read(b).map_err(|e| match e { + craft_lottie::LottieError::Parse(m) => LottieError::Parse(m), + other => LottieError::Invalid(other.to_string()), + }) }; - [c(0), c(2), c(4)] -} - -/// The Lottie JSON inside raw bytes (plain JSON, or a stored dotLottie archive). -fn document(bytes: &[u8]) -> Result { if bytes.starts_with(b"PK") { let entries = crate::zip::read_stored(bytes); let anim = entries @@ -62,9 +57,9 @@ fn document(bytes: &[u8]) -> Result { .find(|(n, _)| n.starts_with("animations/") && n.ends_with(".json")) .or_else(|| entries.iter().find(|(n, _)| n.ends_with(".json") && n != "manifest.json")) .ok_or_else(|| LottieError::Parse("dotLottie archive without an uncompressed animation".into()))?; - return serde_json::from_slice(&anim.1).map_err(|e| LottieError::Parse(e.to_string())); + return read(&anim.1); } - serde_json::from_slice(bytes).map_err(|e| LottieError::Parse(e.to_string())) + read(bytes) } struct Ctx<'a> { @@ -118,27 +113,28 @@ pub fn import( store: &mut dyn FnMut(&ImportedImage) -> Option, ) -> Result { let doc = document(bytes)?; - let (Some(w), Some(h)) = (f(&doc, "w"), f(&doc, "h")) else { return Err(LottieError::Parse("missing `w`/`h`".into())) }; - let fr = f(&doc, "fr").filter(|v| *v > 0.0).unwrap_or(30.0); + let (Some(w), Some(h)) = (doc.width, doc.height) else { return Err(LottieError::Parse("missing `w`/`h`".into())) }; + let fr = doc.frame_rate.filter(|v| *v > 0.0).unwrap_or(30.0); let rate = FrameRate::from_f64(fr); let tb = Timebase { rate }; - let name = s(&doc, "nm").filter(|n| !n.is_empty()).unwrap_or(fallback_name).to_string(); - let ip = f(&doc, "ip").unwrap_or(0.0); - let op = f(&doc, "op").unwrap_or(ip + fr).max(ip + 1.0); + let name = doc.name.as_deref().filter(|n| !n.is_empty()).unwrap_or(fallback_name).to_string(); + let ip = doc.in_point.unwrap_or(0.0); + let op = doc.out_point.unwrap_or(ip + fr).max(ip + 1.0); let mut cx = Ctx { im: Im { tb, warnings: vec![] }, assets: BTreeMap::new(), fonts: vec![], store, base_dir, items: vec![], folder: None, name: name.clone() }; - if let Some(list) = doc.get("fonts").and_then(|f| f.get("list")).and_then(Json::as_array) { - for fo in list { - let n = s(fo, "fName").unwrap_or("").to_string(); - let fam = s(fo, "fFamily").unwrap_or(&n).to_string(); - let st = s(fo, "fStyle").unwrap_or("Regular").to_string(); - cx.fonts.push((n, fam, st)); - } + for fo in doc.fonts.as_ref().and_then(|f| f.list.as_deref()).unwrap_or_default() { + // Every text key referring to the font copies its family and style: keep them to + // a font name's length. + let short = |v: &str| v.chars().take(MAX_FONT_NAME_CHARS).collect::(); + let n = fo.name.clone().unwrap_or_default(); + let fam = short(fo.family.as_deref().unwrap_or(&n)); + let st = short(fo.style.as_deref().unwrap_or("Regular")); + cx.fonts.push((n, fam, st)); } // Glyph outlines embedded for players without the fonts. Text is set in installed fonts. - if let Some(chars) = doc.get("chars").and_then(Json::as_array).filter(|c| !c.is_empty()) { + if let Some(chars) = doc.chars.as_deref().filter(|c| !c.is_empty()) { let mut fams: Vec<&str> = vec![]; - for fam in chars.iter().filter_map(|c| s(c, "fFamily")).filter(|f| !f.trim().is_empty()) { + for fam in chars.iter().filter_map(|c| c.family.as_deref()).filter(|f| !f.trim().is_empty()) { if !fams.contains(&fam) { fams.push(fam); } @@ -149,24 +145,27 @@ pub fn import( let (w, h) = (w.round().max(1.0) as u32, h.round().max(1.0) as u32); let duration = tb.tick(op); // Pass 1: asset items (precomps empty for now, images stored). - let assets = doc.get("assets").and_then(Json::as_array).cloned().unwrap_or_default(); - for a in &assets { - let Some(id) = s(a, "id") else { continue }; - if a.get("layers").is_some() { + let assets: &[Asset] = doc.assets.as_deref().unwrap_or_default(); + let top: &[LottieLayer] = doc.layers.as_deref().unwrap_or_default(); + let layer_sizes = precomp_layer_sizes(top, assets); + for a in assets { + let Some(id) = a.id.as_deref() else { continue }; + let size = a.width.zip(a.height).map(|(x, y)| (x as u32, y as u32)); + if a.layers.is_some() { let folder = cx.folder(project); - let layer = precomp_layer_size(&doc, &assets, id); - let aw = f(a, "w").map(|v| v as u32).or(layer.map(|l| l.0)).unwrap_or(w); - let ah = f(a, "h").map(|v| v as u32).or(layer.map(|l| l.1)).unwrap_or(h); - let arate = f(a, "fr").map(FrameRate::from_f64).unwrap_or(rate); + let layer = layer_sizes.get(id).copied(); + let aw = a.width.map(|v| v as u32).or(layer.map(|l| l.0)).unwrap_or(w); + let ah = a.height.map(|v| v as u32).or(layer.map(|l| l.1)).unwrap_or(h); + let arate = a.frame_rate.map(FrameRate::from_f64).unwrap_or(rate); let c = Comp::new(aw, ah, arate, duration); - let nm = s(a, "nm").unwrap_or(id).to_string(); + let nm = a.name.as_deref().unwrap_or(id).to_string(); let item = project.add_item(&nm, Label::Sandstone, Some(folder), ItemKind::Comp(Arc::new(c))); cx.items.push(item); - cx.assets.insert(id.to_string(), (item, f(a, "w").zip(f(a, "h")).map(|(x, y)| (x as u32, y as u32)))); - } else if let Some(p) = s(a, "p") { - let (aw, ah) = (f(a, "w").unwrap_or(0.0) as u32, f(a, "h").unwrap_or(0.0) as u32); - let nm = s(a, "nm").unwrap_or(id).to_string(); - let path = if let Some((mime, data)) = crate::base64::data_uri(p) { + cx.assets.insert(id.to_string(), (item, size)); + } else if let Some(p) = a.path.as_deref() { + let (aw, ah) = (a.width.unwrap_or(0.0) as u32, a.height.unwrap_or(0.0) as u32); + let nm = a.name.as_deref().unwrap_or(id).to_string(); + let path = if let Some((mime, data)) = craft_lottie::base64::data_uri(p) { let ext = match mime.as_str() { "image/jpeg" | "image/jpg" => "jpg", "image/webp" => "webp", @@ -176,7 +175,7 @@ pub fn import( }; (cx.store)(&ImportedImage { id: id.to_string(), name: nm.clone(), bytes: data, ext: ext.into() }) } else { - let u = s(a, "u").unwrap_or(""); + let u = a.dir.as_deref().unwrap_or(""); match external_image(&format!("{u}{p}"), cx.base_dir) { Ok(path) => Some(path), Err(why) => { @@ -218,11 +217,11 @@ pub fn import( } } // Pass 2: precomp contents. - for a in &assets { - let (Some(id), Some(layers)) = (s(a, "id"), a.get("layers").and_then(Json::as_array)) else { continue }; + for a in assets { + let (Some(id), Some(layers)) = (a.id.as_deref(), a.layers.as_deref()) else { continue }; let Some((item, _)) = cx.assets.get(id).copied() else { continue }; let Some(mut c) = project.comp(item).cloned() else { continue }; - let end = layers.iter().filter_map(|l| f(l, "op")).fold(0.0f64, f64::max); + let end = layers.iter().filter_map(|l| l.out_point).fold(0.0f64, f64::max); if end > 0.0 { c.duration = tb.tick(end); c.work_area = (Tick::ZERO, c.duration); @@ -237,62 +236,64 @@ pub fn import( if ip > 0.0 { comp.work_area = (tb.tick(ip), duration); } - if let Some(ms) = doc.get("markers").and_then(Json::as_array) { - for m in ms { - comp.markers.push(Marker { - time: tb.tick(f(m, "tm").unwrap_or(0.0)), - duration: tb.tick(f(m, "dr").unwrap_or(0.0)), - comment: s(m, "cm").unwrap_or("").to_string(), - ..Default::default() - }); - } + for m in doc.markers.as_deref().unwrap_or_default() { + comp.markers.push(Marker { + time: tb.tick(m.time.unwrap_or(0.0)), + duration: tb.tick(m.duration.unwrap_or(0.0)), + comment: m.comment.clone().unwrap_or_default(), + ..Default::default() + }); } - let layers = doc.get("layers").and_then(Json::as_array).cloned().unwrap_or_default(); - build_layers(&mut cx, project, &mut comp, &layers); + build_layers(&mut cx, project, &mut comp, top); let id = project.add_item(&name, Label::Sandstone, None, ItemKind::Comp(Arc::new(comp))); cx.items.push(id); project.fix_next_id(); Ok(ImportResult { comp: id, items: cx.items, warnings: cx.im.warnings }) } -/// The `w`/`h` of the first precomp layer, in the main comp or in any asset, that shows asset `id`. -/// lottie-spec gives a precomposition asset no size: the layer's `w`/`h` are its clipping rect. -fn precomp_layer_size(doc: &Json, assets: &[Json], id: &str) -> Option<(u32, u32)> { - let lists = doc.get("layers").into_iter().chain(assets.iter().filter_map(|a| a.get("layers"))); - lists.filter_map(Json::as_array).flatten().find_map(|l| { - if l.get("ty").and_then(Json::as_u64) != Some(0) || s(l, "refId") != Some(id) { - return None; - } - let (lw, lh) = f(l, "w").zip(f(l, "h"))?; - Some((lw.round().max(1.0) as u32, lh.round().max(1.0) as u32)) - }) +/// By asset id, the `w`/`h` of the first precomp layer, in the main comp or in any asset, that +/// shows it. lottie-spec gives a precomposition asset no size: the layer's `w`/`h` are its +/// clipping rect. One pass over all layers, not one per asset. +fn precomp_layer_sizes<'a>(top: &'a [LottieLayer], assets: &'a [Asset]) -> HashMap<&'a str, (u32, u32)> { + let mut out = HashMap::new(); + let lists = std::iter::once(top).chain(assets.iter().filter_map(|a| a.layers.as_deref())); + for l in lists.flatten() { + let LayerContent::Precomp(p) = &l.content else { continue }; + let (Some(id), Some((lw, lh))) = (p.ref_id.as_deref(), p.width.zip(p.height)) else { continue }; + out.entry(id).or_insert((lw.round().max(1.0) as u32, lh.round().max(1.0) as u32)); + } + out } -fn transform(im: &Im, ks: &Json, tr: &mut PropGroup, next: &mut u64) { - let set = |tr: &mut PropGroup, m: &str, j: Option<&Json>, conv: &dyn Fn(&Json) -> Option| { - if let (Some(j), Some(p)) = (j, tr.get_mut(m)) { - import_prop(im, j, p, conv); +fn transform(im: &Im, ks: &Transform, tr: &mut PropGroup, next: &mut u64) { + let set = |tr: &mut PropGroup, m: &str, j: Option<&Animated>, conv: &dyn Fn(&Json) -> Option| { + if let Some(p) = tr.get_mut(m) { + import_animated(im, j, p, conv); } }; - set(tr, "anchor", ks.get("a"), &|v| anim::to_vec3(v, 0.0)); - set(tr, "scale", ks.get("s"), &|v| anim::to_vec3(v, 100.0)); - set(tr, "rotation", ks.get("r").or(ks.get("rz")), &anim::to_scalar); - set(tr, "rotationX", ks.get("rx"), &anim::to_scalar); - set(tr, "rotationY", ks.get("ry"), &anim::to_scalar); - set(tr, "orientation", ks.get("or"), &|v| anim::to_vec3(v, 0.0)); - set(tr, "opacity", ks.get("o"), &anim::to_scalar); - let Some(p) = ks.get("p") else { return }; - if p.get("s").and_then(Json::as_bool) == Some(true) { + set(tr, "anchor", ks.anchor.as_ref(), &|v| anim::to_vec3(v, 0.0)); + set(tr, "scale", ks.scale.as_ref(), &|v| anim::to_vec3(v, 100.0)); + set(tr, "rotation", ks.rotation.as_ref().or(ks.rotation_z.as_ref()), &anim::to_scalar); + set(tr, "rotationX", ks.rotation_x.as_ref(), &anim::to_scalar); + set(tr, "rotationY", ks.rotation_y.as_ref(), &anim::to_scalar); + set(tr, "orientation", ks.orientation.as_ref(), &|v| anim::to_vec3(v, 0.0)); + set(tr, "opacity", ks.opacity.as_ref(), &anim::to_scalar); + let Some(p) = &ks.position else { return }; + if let Some(dims) = p.split() { // Separated dimensions: X/Y/Z Position after Position. let at = tr.children.iter().position(|c| c.match_id() == "position").map(|i| i + 1).unwrap_or(tr.children.len()); let base = tr.get("position").map(|p| p.value.as_vec3()).unwrap_or([0.0; 3]); - for (d, (key, m, name)) in - [("x", "positionX", "X Position"), ("y", "positionY", "Y Position"), ("z", "positionZ", "Z Position")].iter().enumerate().rev() + for (d, (j, m, name)) in dims + .iter() + .zip([("positionX", "X Position"), ("positionY", "Y Position"), ("positionZ", "Z Position")]) + .map(|(j, (m, n))| (j, m, n)) + .enumerate() + .rev() { let mut pr = Property::new(*next, m, name, Value::Scalar(base[d])).with_ui(ParamUi::Number); *next += 1; pr.three_d_only = d == 2; - if let Some(j) = p.get(*key) { + if let Some(j) = j { import_prop(im, j, &mut pr, &anim::to_scalar); } tr.children.insert(at, Node::Prop(pr)); @@ -302,50 +303,50 @@ fn transform(im: &Im, ks: &Json, tr: &mut PropGroup, next: &mut u64) { } } -fn masks(im: &Im, ids: &mut Ids, list: &[Json], g: &mut PropGroup) { +fn masks(im: &Im, ids: &mut Ids, list: &[Mask], g: &mut PropGroup) { for (i, m) in list.iter().enumerate() { - let mode = mask_mode_from(s(m, "mode").unwrap_or("a")); - let name = s(m, "nm").map(str::to_string).unwrap_or(format!("Mask {}", i + 1)); + let mode = mask_mode_from(m.mode.as_deref().unwrap_or("a")); + let name = m.name.clone().unwrap_or(format!("Mask {}", i + 1)); let color = build::MASK_COLORS[i % build::MASK_COLORS.len()]; let mut mg = build::mask(ids, &name, ShapePath::default(), mode, color); if let GroupKind::Mask { inverted, .. } = &mut mg.kind { - *inverted = m.get("inv").and_then(Json::as_bool).unwrap_or(false); + *inverted = m.inverted.unwrap_or(false); } - let mut set = |k: &str, mid: &str, conv: &dyn Fn(&Json) -> Option| { - if let (Some(j), Some(p)) = (m.get(k), mg.get_mut(mid)) { + let mut set = |j: Option<&Json>, mid: &str, conv: &dyn Fn(&Json) -> Option| { + if let (Some(j), Some(p)) = (j, mg.get_mut(mid)) { import_prop(im, j, p, conv); } }; - let closed = m.get("cl").and_then(Json::as_bool).unwrap_or(false); - set("pt", "path", &|v| shapes::to_path(v, closed)); - set("o", "opacity", &anim::to_scalar); - set("x", "expansion", &anim::to_scalar); - set("f", "feather", &anim::to_vec2); + let closed = m.closed.unwrap_or(false); + set(m.path.as_ref().map(|a| &a.0), "path", &|v| shapes::to_path(v, closed)); + set(m.opacity.as_ref().map(|a| &a.0), "opacity", &anim::to_scalar); + set(m.expansion.as_ref().map(|a| &a.0), "expansion", &anim::to_scalar); + set(m.extra.get("f"), "feather", &anim::to_vec2); g.children.push(Node::Group(mg)); } } -fn effects(im: &mut Im, ids: &mut Ids, list: &[Json], g: &mut PropGroup, size: [f64; 2]) { +fn effects(im: &mut Im, ids: &mut Ids, list: &[Effect], g: &mut PropGroup, size: [f64; 2]) { for e in list { - let ty = e.get("ty").and_then(Json::as_u64).unwrap_or(0); - let params = e.get("ef").and_then(Json::as_array).cloned().unwrap_or_default(); - let inner_ty = params.first().and_then(|p| p.get("ty")).and_then(Json::as_u64); - let map = s(e, "mn").and_then(fx_by_id).or_else(|| fx_by_type(ty, inner_ty)); - let nm = s(e, "nm").unwrap_or("Effect"); + let ty = e.kind.and_then(|t| u64::try_from(t).ok()).unwrap_or(0); + let params: &[Effect] = e.values.as_deref().unwrap_or_default(); + let inner_ty = params.first().and_then(|p| p.kind).and_then(|t| u64::try_from(t).ok()); + let map = e.match_name.as_deref().and_then(fx_by_id).or_else(|| fx_by_type(ty, inner_ty)); + let nm = e.name.as_deref().unwrap_or("Effect"); let Some(map) = map else { im.warn(format!("effect `{nm}` (type {ty}) is not supported (skipped)")); continue; }; let Some(spec) = effectcraft_effects::find(map.id) else { continue }; let mut fg = effectcraft_effects::instantiate(spec, ids, nm, size); - if e.get("en").and_then(Json::as_u64) == Some(0) { + if e.enabled == Some(0) { fg.enabled = false; } for (i, (m, pty, scale, _)) in fx_params(map).iter().enumerate() { if m.is_empty() { continue; } - let Some(pj) = params.get(i).and_then(|p| p.get("v")) else { continue }; + let Some(pj) = params.get(i).and_then(|p| p.value.as_ref()) else { continue }; let Some(p) = fg.get_mut(m) else { continue }; let (pty, scale) = (*pty, *scale); let conv = move |v: &Json| -> Option { @@ -357,79 +358,84 @@ fn effects(im: &mut Im, ids: &mut Ids, list: &[Json], g: &mut PropGroup, size: [ _ => anim::to_scalar(v).map(|x| Value::Scalar(x.as_f64() / scale)), } }; - import_prop(im, pj, p, &conv); + import_prop(im, &pj.0, p, &conv); } g.children.push(Node::Group(fg)); } } -fn layer_source(cx: &mut Ctx, project: &mut Project, comp: &Comp, j: &Json) -> Option<(LayerSource, (u32, u32))> { - let ty = j.get("ty").and_then(Json::as_u64).unwrap_or(99); +fn layer_source(cx: &mut Ctx, project: &mut Project, comp: &Comp, j: &LottieLayer) -> Option<(LayerSource, (u32, u32))> { let full = (comp.width, comp.height); - let asset = |cx: &Ctx| s(j, "refId").and_then(|r| cx.assets.get(r).copied()); - Some(match ty { - 0 => { - let Some((item, _)) = asset(cx) else { - cx.im.warn(format!("{}: missing precomp asset", s(j, "nm").unwrap_or("layer"))); + let nm = j.name.as_deref(); + let asset = |cx: &Ctx, r: Option<&str>| r.and_then(|r| cx.assets.get(r).copied()); + Some(match &j.content { + LayerContent::Precomp(p) => { + let Some((item, _)) = asset(cx, p.ref_id.as_deref()) else { + cx.im.warn(format!("{}: missing precomp asset", nm.unwrap_or("layer"))); return None; }; let size = project.comp(item).map(|c| (c.width, c.height)).unwrap_or(full); (LayerSource::Comp { item }, size) } - 1 => { - let sw = f(j, "sw").unwrap_or(comp.width as f64).round().max(1.0) as u32; - let sh = f(j, "sh").unwrap_or(comp.height as f64).round().max(1.0) as u32; - let color = hex_color(s(j, "sc").unwrap_or("#000000")); - let name = s(j, "nm").unwrap_or("Solid"); + LayerContent::Solid(sl) => { + let sw = sl.width.unwrap_or(comp.width as f64).round().max(1.0) as u32; + let sh = sl.height.unwrap_or(comp.height as f64).round().max(1.0) as u32; + let color = craft_lottie::hex_color(sl.color.as_deref().unwrap_or("#000000")); + let name = nm.unwrap_or("Solid"); let folder = cx.folder(project); let item = project.add_item(name, Label::Red, Some(folder), ItemKind::Solid(Solid { color, width: sw, height: sh, pixel_aspect: 1.0 })); cx.items.push(item); (LayerSource::Solid { item }, (sw, sh)) } - 2 => { - let Some((item, size)) = asset(cx) else { - cx.im.warn(format!("{}: missing image asset", s(j, "nm").unwrap_or("layer"))); + LayerContent::Image(r) => { + let Some((item, size)) = asset(cx, r.as_deref()) else { + cx.im.warn(format!("{}: missing image asset", nm.unwrap_or("layer"))); return None; }; (LayerSource::Footage { item }, size.unwrap_or(full)) } - 3 => (LayerSource::Null, (100, 100)), - 4 => (LayerSource::Shape, full), - 5 => (LayerSource::Text, full), - 13 => { - cx.im.warn(format!("{}: camera layers are not imported", s(j, "nm").unwrap_or("camera"))); - return None; - } - other => { - cx.im.warn(format!("{}: layer type {other} is not supported (skipped)", s(j, "nm").unwrap_or("layer"))); - return None; - } + LayerContent::Null => (LayerSource::Null, (100, 100)), + LayerContent::Shape(_) => (LayerSource::Shape, full), + LayerContent::Text(_) => (LayerSource::Text, full), + LayerContent::Other(ty) => match ty.as_u64().unwrap_or(99) { + // A shape or text layer whose content is missing or unreadable is an empty one. + 4 => (LayerSource::Shape, full), + 5 => (LayerSource::Text, full), + 13 => { + cx.im.warn(format!("{}: camera layers are not imported", nm.unwrap_or("camera"))); + return None; + } + other => { + cx.im.warn(format!("{}: layer type {other} is not supported (skipped)", nm.unwrap_or("layer"))); + return None; + } + }, }) } /// Build the layers of a comp (Lottie order: first = top = layer #1). -fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[Json]) { +fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[LottieLayer]) { let tb = cx.im.tb; let mut by_ind: BTreeMap = BTreeMap::new(); - // (layer id, parent ind, matte: (kind, explicit tp ind), json position) - let mut pending: Vec<(LayerId, Option, Option<(MatteKind, Option)>, usize)> = vec![]; + // (layer id, its index in `comp.layers`, parent ind, matte: (kind, explicit tp ind), json position) + let mut pending: Vec<(LayerId, usize, Option, Option<(MatteKind, Option)>, usize)> = vec![]; let mut pos_ind: Vec> = vec![]; for (ji, j) in layers.iter().enumerate() { - let ind = j.get("ind").and_then(Json::as_i64); + let ind = j.index; pos_ind.push(ind); let Some((source, size)) = layer_source(cx, project, comp, j) else { continue }; - let name = s(j, "nm").unwrap_or(source.type_name()).to_string(); + let name = j.name.clone().unwrap_or_else(|| source.type_name().to_string()); let mut l: Layer = build::layer(project, comp, &name, source.clone(), size, None); if let Some(i) = ind { by_ind.insert(i, l.id); } - let ip = f(j, "ip").unwrap_or(0.0); - let op = f(j, "op").unwrap_or(tb.frame(comp.duration)); + let ip = j.in_point.unwrap_or(0.0); + let op = j.out_point.unwrap_or(tb.frame(comp.duration)); l.in_point = tb.tick(ip); l.out_point = tb.tick(op.max(ip)); - l.start_time = tb.tick(f(j, "st").unwrap_or(0.0)); + l.start_time = tb.tick(j.start_time.unwrap_or(0.0)); // Below 1 % (or 0) the layer time divides by ~0, and layer.timeStretch refuses it too. - let sr = f(j, "sr").unwrap_or(1.0); + let sr = j.stretch.unwrap_or(1.0); l.stretch = if sr.is_finite() && sr.abs() >= 0.01 { // Past ±9900 % too (#3247): the largest stretch a layer supports, sign kept. let pct = effectcraft_project::limits::stretch(sr * 100.0); @@ -441,76 +447,73 @@ fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[ cx.im.warn(format!("{name}: a time stretch of {} % is not supported (100 % used)", sr * 100.0)); 100.0 }; - if j.get("ddd").and_then(Json::as_u64) == Some(1) { + if j.three_d == Some(1) { l.switches.three_d = true; } - if let Some(bm) = j.get("bm").and_then(Json::as_u64) { + if let Some(bm) = j.blend_mode.and_then(|b| u64::try_from(b).ok()) { l.blend_mode = crate::blend_from_lottie(bm); } - if j.get("ao").and_then(Json::as_u64) == Some(1) { + if j.auto_orient == Some(1) { l.auto_orient = AutoOrient::AlongPath; } - if j.get("hd").and_then(Json::as_bool) == Some(true) { + if j.hidden == Some(true) { l.switches.video = false; } - if j.get("td").and_then(Json::as_u64) == Some(1) { + if j.is_matte == Some(1) { l.switches.video = false; } let mut next = project.next_id; - if let (Some(ks), Some(tr)) = (j.get("ks"), l.props.sub_mut("transform")) { + if let (Some(ks), Some(tr)) = (&j.transform, l.props.sub_mut("transform")) { transform(&cx.im, ks, tr, &mut next); } let mut ids = Ids(&mut next); - match &source { - LayerSource::Shape => { - let items = j.get("shapes").and_then(Json::as_array).cloned().unwrap_or_default(); - let groups = shapes::import_items(&mut cx.im, &mut ids, &items); + match (&source, &j.content) { + (LayerSource::Shape, LayerContent::Shape(items)) => { + let groups = shapes::import_items(&mut cx.im, &mut ids, &mut items.iter()); if let Some(c) = l.props.sub_mut("contents") { c.children.extend(groups.into_iter().map(Node::Group)); } } - LayerSource::Text => { - if let (Some(t), Some(tg)) = (j.get("t"), l.props.sub_mut("text")) { + (LayerSource::Text, LayerContent::Text(t)) => { + if let Some(tg) = l.props.sub_mut("text") { text::import_text(&mut cx.im, &mut ids, t, tg, &cx.fonts); } } - LayerSource::Comp { .. } => { - if let Some(tm) = j.get("tm") { + (LayerSource::Comp { .. }, LayerContent::Precomp(p)) => { + if let Some(tm) = &p.time_remap { let mut pr = ids.prop("timeRemap", "Time Remap", Value::Scalar(0.0)).with_ui(ParamUi::Number); - import_prop(&cx.im, tm, &mut pr, &anim::to_scalar); + import_prop(&cx.im, &tm.0, &mut pr, &anim::to_scalar); l.props.children.insert(0, Node::Prop(pr)); } } _ => {} } - if let Some(list) = j.get("masksProperties").and_then(Json::as_array) + if let Some(list) = &j.masks && let Some(g) = l.props.sub_mut("masks") { masks(&cx.im, &mut ids, list, g); } - if let Some(list) = j.get("ef").and_then(Json::as_array) + if let Some(list) = &j.effects && let Some(g) = l.props.sub_mut("effects") { effects(&mut cx.im, &mut ids, list, g, [size.0 as f64, size.1 as f64]); } - if j.get("sy").and_then(Json::as_array).is_some_and(|a| !a.is_empty()) { + if j.extra.get("sy").and_then(Json::as_array).is_some_and(|a| !a.is_empty()) { cx.im.warn(format!("{name}: layer styles are not imported")); } project.next_id = next; - let parent = j.get("parent").and_then(Json::as_i64); - let tt = j.get("tt").and_then(Json::as_u64).unwrap_or(0); - let matte = match tt { + let matte = match j.matte_mode.unwrap_or(0) { 1 => Some(MatteKind::Alpha), 2 => Some(MatteKind::AlphaInverted), 3 => Some(MatteKind::Luma), 4 => Some(MatteKind::LumaInverted), _ => None, } - .map(|k| (k, j.get("tp").and_then(Json::as_i64))); - pending.push((l.id, parent, matte, ji)); + .map(|k| (k, j.matte_parent)); + pending.push((l.id, comp.layers.len(), j.parent, matte, ji)); comp.layers.push(l); } - for (id, parent, matte, ji) in pending { + for (id, at, parent, matte, ji) in pending { let parent_id = parent.and_then(|p| by_ind.get(&p).copied()).filter(|p| *p != id); let matte_id = matte.and_then(|(k, tp)| { let target = match tp { @@ -520,21 +523,9 @@ fn build_layers(cx: &mut Ctx, project: &mut Project, comp: &mut Comp, layers: &[ }; target.filter(|t| *t != id).map(|t| TrackMatte { layer: t, kind: k }) }); - if let Some(l) = comp.layer_mut(id) { + if let Some(l) = comp.layers.get_mut(at) { l.parent = parent_id; l.track_matte = matte_id; } } } - -#[cfg(test)] -mod hex_color_tests { - use super::hex_color; - - #[test] - fn hex_color_rejects_plus_sign() { - assert_eq!(hex_color("#+f0000"), [0.0, 0.0, 0.0]); - assert_eq!(hex_color("#zz0000"), [0.0, 0.0, 0.0]); - assert_eq!(hex_color("#ff0000"), [1.0, 0.0, 0.0]); - } -} diff --git a/crates/lottie/src/lib.rs b/crates/lottie/src/lib.rs index 1b12cc8be..0a1f4bc96 100644 --- a/crates/lottie/src/lib.rs +++ b/crates/lottie/src/lib.rs @@ -1,7 +1,7 @@ //! Lottie JSON import and export. //! -//! Implemented from the public Lottie format description (the lottie-spec 1.0 schema and the -//! LottieFiles format documentation); no player or exporter source was used. +//! The format itself (the document model, property and keyframe encoding, paths, gradients, +//! embedded images) is `craft-lottie`; this crate maps it onto EffectCraft's project. //! //! **Export** ([`export_comp`]) turns a composition into a Lottie document: nested compositions //! become precomp assets, still footage becomes embedded base64 image assets, and shape, solid, @@ -21,7 +21,6 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] mod anim; -mod base64; mod export; mod import; mod shapes; diff --git a/crates/lottie/src/shapes.rs b/crates/lottie/src/shapes.rs index fab8d3646..f28e73a04 100644 --- a/crates/lottie/src/shapes.rs +++ b/crates/lottie/src/shapes.rs @@ -1,23 +1,27 @@ //! Shape layer contents ↔ Lottie shape items (`gr`, `rc`, `el`, `sr`, `sh`, `fl`, `st`, `gf`, -//! `gs`, `tm`, `rp`, `rd`, `op`, `pb`, `tw`, `zz`, `mm`, `tr`). +//! `gs`, `tm`, `rp`, `rd`, `op`, `pb`, `tw`, `zz`, `mm`, `tr`), on `craft-lottie`'s model. +use craft_lottie::model::{ + Dash, Ellipse, Fill, GradientColors, GradientFill, GradientStroke, Group, Map, Merge, OffsetPath, Path, PolyStar, PuckerBloat, Rect, Repeater, + RepeaterTransform, RoundCorners, Shape, ShapeKind, Stroke, Transform, Trim, Twist, ZigZag, +}; +use craft_lottie::{Animated, Bezier, Json}; use effectcraft_color::BlendMode; use effectcraft_keyframe::{Gradient, ShapePath, Value}; use effectcraft_project::build::{self, EXTRA_DASHES, Ids}; use effectcraft_project::{Node, ParamUi, PropGroup}; -use serde_json::{Map, Value as Json, json}; +use serde_json::json; use crate::anim::{self, Ex, Im, export_prop, import_prop}; // ---------------------------------------------------------------- shared value converters -pub(crate) fn shape_json(p: &ShapePath) -> Json { - let pts = |v: &[[f64; 2]]| v.iter().map(|q| json!([r(q[0]), r(q[1])])).collect::>(); - json!({"c": p.closed, "v": pts(&p.vertices), "i": pts(&p.in_tangents), "o": pts(&p.out_tangents)}) +pub(crate) fn bezier(p: &ShapePath) -> Bezier { + Bezier { closed: p.closed, vertices: p.vertices.clone(), in_tangents: p.in_tangents.clone(), out_tangents: p.out_tangents.clone() } } -fn r(v: f64) -> f64 { - (v * 1e6).round() / 1e6 +pub(crate) fn shape_json(p: &ShapePath) -> Json { + bezier(p).to_json() } pub(crate) fn path_value(v: &Value) -> Json { @@ -30,29 +34,8 @@ pub(crate) fn path_value(v: &Value) -> Json { /// A Lottie path. `closed` is used when the path has no `c`: Bodymovin 3.x and early 4.x keep the /// flag outside the path, as `cl` on a mask and `closed` on an `sh` item. pub(crate) fn to_path(j: &Json, closed: bool) -> Option { - let o = match j { - Json::Array(a) => a.first()?, - o => o, - }; - let pts = |k: &str| -> Vec<[f64; 2]> { - o.get(k) - .and_then(Json::as_array) - .map(|a| a.iter().map(|p| anim::nums(p).map(|v| [*v.first().unwrap_or(&0.0), *v.get(1).unwrap_or(&0.0)]).unwrap_or([0.0; 2])).collect()) - .unwrap_or_default() - }; - let vertices = pts("v"); - let n = vertices.len(); - let fit = |mut v: Vec<[f64; 2]>| { - v.resize(n, [0.0; 2]); - v - }; - Some(Value::Path(ShapePath { - in_tangents: fit(pts("i")), - out_tangents: fit(pts("o")), - vertices, - closed: o.get("c").and_then(Json::as_bool).unwrap_or(closed), - feather: Vec::new(), - })) + let b = Bezier::from_json(j, closed)?; + Some(Value::Path(ShapePath { vertices: b.vertices, in_tangents: b.in_tangents, out_tangents: b.out_tangents, closed: b.closed, feather: Vec::new() })) } /// Gradient as Lottie's flat stop list: `[pos, r, g, b]…` then `[pos, alpha]…`. @@ -61,29 +44,19 @@ pub(crate) fn gradient_flat(v: &Value) -> Json { Value::Gradient(g) => g.clone(), _ => Gradient::default(), }; - let mut out = vec![]; - for (p, c) in &g.colors { - out.extend([r(*p), r(c[0] as f64), r(c[1] as f64), r(c[2] as f64)]); + craft_lottie::Gradient { + colors: g.colors.iter().map(|(p, c)| (*p, [f64::from(c[0]), f64::from(c[1]), f64::from(c[2])])).collect(), + opacities: g.opacities.iter().map(|(p, a)| (*p, f64::from(*a))).collect(), } - let plain = g.opacities.iter().all(|(_, a)| (*a - 1.0).abs() < 1e-6); - if !plain { - for (p, a) in &g.opacities { - out.extend([r(*p), r(*a as f64)]); - } - } - json!(out) + .to_flat() } pub(crate) fn to_gradient(j: &Json, ncolors: usize) -> Option { - let v = anim::nums(j)?; - let n = ncolors.min(v.len() / 4); - let colors: Vec<(f64, [f32; 4])> = (0..n).map(|i| (v[i * 4], [v[i * 4 + 1] as f32, v[i * 4 + 2] as f32, v[i * 4 + 3] as f32, 1.0])).collect(); - let rest = &v[n * 4..]; - let mut opacities: Vec<(f64, f32)> = rest.chunks(2).filter(|c| c.len() == 2).map(|c| (c[0], c[1] as f32)).collect(); - if opacities.is_empty() { - opacities = vec![(0.0, 1.0), (1.0, 1.0)]; - } - Some(Value::Gradient(Gradient { colors, opacities })) + let g = craft_lottie::Gradient::from_flat(j, ncolors)?; + Some(Value::Gradient(Gradient { + colors: g.colors.iter().map(|(p, [r, gr, b])| (*p, [*r as f32, *gr as f32, *b as f32, 1.0])).collect(), + opacities: g.opacities.iter().map(|(p, a)| (*p, *a as f32)).collect(), + })) } fn blend_index(m: BlendMode) -> u32 { @@ -92,22 +65,18 @@ fn blend_index(m: BlendMode) -> u32 { // ---------------------------------------------------------------- export +/// A group's properties, exported with the warnings they need. struct Out<'a, 'b> { ex: &'a mut Ex, g: &'b PropGroup, - o: Map, label: String, } impl Out<'_, '_> { - fn put(&mut self, key: &str, m: &str, conv: &dyn Fn(&Value) -> Json) { - if let Some(p) = self.g.get(m) { - let v = export_prop(self.ex, p, conv, &format!("{} ▸ {}", self.label, p.name)); - self.o.insert(key.into(), v); - } - } - fn raw(&mut self, key: &str, v: Json) { - self.o.insert(key.into(), v); + /// The property `m` of the group, when it has one. + fn put(&mut self, m: &str, conv: &dyn Fn(&Value) -> Json) -> Option { + let p = self.g.get(m)?; + Some(export_prop(self.ex, p, conv, &format!("{} ▸ {}", self.label, p.name))) } fn e(&self, m: &str) -> u32 { self.g.get(m).map(|p| p.value.as_enum()).unwrap_or(0) @@ -121,34 +90,46 @@ impl Out<'_, '_> { fn animated(&self, m: &str) -> bool { self.g.get(m).is_some_and(|p| p.is_animated() || p.has_expression()) } - fn blend(&mut self) { + fn blend(&mut self) -> i64 { let i = self.e("blend"); let m = BlendMode::ALL.get(i as usize).copied().unwrap_or_default(); match crate::blend_to_lottie(m) { - Some(bm) => self.raw("bm", json!(bm)), + Some(bm) => i64::from(bm), None => { self.ex.warn(format!("{}: blend mode {m:?} has no Lottie equivalent (Normal used)", self.label)); - self.raw("bm", json!(0)); + 0 } } } - fn done(self) -> Json { - Json::Object(self.o) + fn direction(&self) -> i64 { + if self.e("direction") == 1 { 3 } else { 1 } } } -/// Stroke fields shared by `st` and `gs`. -fn stroke_fields(o: &mut Out) { - o.put("w", "width", &anim::num); - o.raw("lc", json!(o.e("cap") + 1)); - o.raw("lj", json!(o.e("join") + 1)); - o.raw("ml", json!(o.f("miter"))); +/// A static property (`{a: 0, k}`). +pub(crate) fn fixed(k: Json) -> Animated { + Animated(json!({"a": 0, "k": k})) +} + +/// Stroke fields shared by `st` and `gs`: width, cap, join, miter limit, dashes. +struct StrokeOut { + width: Option, + cap: i64, + join: i64, + miter: f64, + dashes: Option>, +} + +fn stroke_fields(o: &mut Out) -> StrokeOut { + let width = o.put("width", &anim::num); + let (cap, join, miter) = (i64::from(o.e("cap")) + 1, i64::from(o.e("join")) + 1, o.f("miter")); let used = |g: Option<&PropGroup>, m: &str| g.and_then(|g| g.get(m)).is_some_and(|p| p.is_animated() || p.value.as_f64().abs() > 1e-9); let (taper, wave) = (o.g.sub("taper"), o.g.sub("wave")); if used(taper, "startLength") || used(taper, "endLength") || used(wave, "amount") { let msg = format!("{}: stroke Taper and Wave have no Lottie equivalent (uniform width exported)", o.label); o.ex.warn(msg); } + let mut dashes = None; if let Some(d) = o.g.sub("dashes") { let dash = d.get("dash").map(|p| p.value.as_f64()).unwrap_or(0.0); if dash > 0.0 || d.any_animated() { @@ -156,202 +137,257 @@ fn stroke_fields(o: &mut Out) { for (n, m) in [("d", "dash"), ("g", "gap"), ("d", "dash2"), ("g", "gap2"), ("d", "dash3"), ("g", "gap3"), ("o", "offset")] { if let Some(p) = d.get(m) { let v = export_prop(o.ex, p, &anim::num, &format!("{} ▸ Dashes ▸ {}", o.label, p.name)); - list.push(json!({"n": n, "nm": p.name, "v": v})); + list.push(Dash { kind: Some(n.into()), name: Some(p.name.clone()), length: Some(v), ..Default::default() }); } } - o.raw("d", json!(list)); + dashes = Some(list); } } + StrokeOut { width, cap, join, miter, dashes } } -fn shape_transform(ex: &mut Ex, tr: &PropGroup, label: &str) -> Json { - let mut o = Out { ex, g: tr, o: Map::new(), label: format!("{label} ▸ Transform") }; - o.raw("ty", json!("tr")); - o.put("a", "anchor", &anim::arr2); - o.put("p", "position", &anim::arr2); - o.put("s", "scale", &anim::arr2); - o.put("r", "rotation", &anim::num); - o.put("o", "opacity", &anim::num); - o.put("sk", "skew", &anim::num); - o.put("sa", "skewAxis", &anim::num); - o.put("so", "startOpacity", &anim::num); - o.put("eo", "endOpacity", &anim::num); - o.done() +/// The `tr` of a group or a repeater: anchor, position, scale, rotation, opacity, skew, and the +/// repeater's start and end opacity, by their Lottie keys. +fn shape_transform(ex: &mut Ex, tr: &PropGroup, label: &str) -> Vec<(&'static str, Animated)> { + let mut o = Out { ex, g: tr, label: format!("{label} ▸ Transform") }; + let mut out = vec![]; + for (k, m, conv) in [ + ("a", "anchor", anim::arr2 as fn(&Value) -> Json), + ("p", "position", anim::arr2), + ("s", "scale", anim::arr2), + ("r", "rotation", anim::num), + ("o", "opacity", anim::num), + ("sk", "skew", anim::num), + ("sa", "skewAxis", anim::num), + ("so", "startOpacity", anim::num), + ("eo", "endOpacity", anim::num), + ] { + if let Some(v) = o.put(m, &conv) { + out.push((k, v)); + } + } + out +} + +/// The `tr` item that ends a group's items. +fn group_transform(keys: Vec<(&'static str, Animated)>) -> Shape { + let (mut t, mut extra) = (Transform::default(), Map::new()); + for (k, v) in keys { + match k { + "a" => t.anchor = Some(v), + "p" => t.position = Some(v), + "s" => t.scale = Some(v), + "r" => t.rotation = Some(v), + "o" => t.opacity = Some(v), + "sk" => t.skew = Some(v), + "sa" => t.skew_axis = Some(v), + _ => { + extra.insert(k.into(), v.0); + } + } + } + Shape { kind: ShapeKind::Transform(Box::new(t)), extra, ..Default::default() } +} + +/// A repeater's `tr`, which players read with `ty: "tr"` like a group's. +fn repeater_transform(keys: Vec<(&'static str, Animated)>) -> RepeaterTransform { + let mut t = RepeaterTransform::default(); + t.extra.insert("ty".into(), json!("tr")); + for (k, v) in keys { + match k { + "a" => t.anchor = Some(v), + "p" => t.position = Some(v), + "s" => t.scale = Some(v), + "r" => t.rotation = Some(v), + "so" => t.start_opacity = Some(v), + "eo" => t.end_opacity = Some(v), + "sk" => t.skew = Some(v), + "sa" => t.skew_axis = Some(v), + _ => { + t.extra.insert(k.into(), v.0); + } + } + } + t } /// Export a contents group's items (first item = top, as in the timeline). -pub(crate) fn export_items(ex: &mut Ex, contents: &PropGroup, label: &str) -> Vec { +pub(crate) fn export_items(ex: &mut Ex, contents: &PropGroup, label: &str) -> Vec { let mut out = vec![]; for node in &contents.children { let Node::Group(g) = node else { continue }; let lbl = format!("{label} ▸ {}", g.name); - let ty = match g.match_id.as_str() { - "group" => "gr", - "rect" => "rc", - "ellipse" => "el", - "star" => "sr", - "path" => "sh", - "fill" => "fl", - "stroke" => "st", - "gfill" => "gf", - "gstroke" => "gs", - "trim" => "tm", - "repeater" => "rp", - "round" => "rd", - "offset" => "op", - "pucker" => "pb", - "twist" => "tw", - "zigzag" => "zz", - "merge" => "mm", - other => { - ex.warn(format!("{lbl}: shape item `{other}` has no Lottie equivalent (skipped)")); - continue; + let mut o = Out { ex, g, label: lbl.clone() }; + let mut blend = None; + let kind = match g.match_id.as_str() { + "group" => { + let mut items = g.sub("contents").map(|c| export_items(o.ex, c, &lbl)).unwrap_or_default(); + items.push(match g.sub("transform") { + Some(t) => group_transform(shape_transform(o.ex, t, &lbl)), + None => group_transform(vec![ + ("a", fixed(json!([0, 0]))), + ("p", fixed(json!([0, 0]))), + ("s", fixed(json!([100, 100]))), + ("r", fixed(json!(0))), + ("o", fixed(json!(100))), + ]), + }); + blend = Some(o.blend()); + ShapeKind::Group(Group { count: Some(items.len() as f64), items: Some(items) }) } - }; - let mut o = Out { ex, g, o: Map::new(), label: lbl.clone() }; - o.raw("ty", json!(ty)); - o.raw("nm", json!(g.name)); - if !g.enabled { - o.raw("hd", json!(true)); - } - let dir = |o: &mut Out| { - let d = if o.e("direction") == 1 { 3 } else { 1 }; - o.raw("d", json!(d)); - }; - match ty { - "gr" => { - let items = g.sub("contents").map(|c| export_items(o.ex, c, &lbl)).unwrap_or_default(); - let mut items = items; - let tr = match g.sub("transform") { - Some(t) => shape_transform(o.ex, t, &lbl), - None => { - json!({"ty": "tr", "a": {"a": 0, "k": [0, 0]}, "p": {"a": 0, "k": [0, 0]}, "s": {"a": 0, "k": [100, 100]}, "r": {"a": 0, "k": 0}, "o": {"a": 0, "k": 100}}) - } - }; - items.push(tr); - o.raw("np", json!(items.len())); - o.raw("it", json!(items)); - o.blend(); - } - "rc" => { - dir(&mut o); - o.put("s", "size", &anim::arr2); - o.put("p", "position", &anim::arr2); - if o.flag("perCorner") { + "rect" => { + let direction = Some(o.direction()); + let size = o.put("size", &anim::arr2); + let position = o.put("position", &anim::arr2); + let roundness = if o.flag("perCorner") { let names = ["topLeftRoundness", "topRightRoundness", "bottomRightRoundness", "bottomLeftRoundness"]; let radii = names.map(|m| o.f(m)); // Lottie `rc` has one `r`: four equal static radii export as that value. // `r` is a property (`{a, k}`) like the uniform branch writes, not a bare number. if radii.iter().all(|v| (v - radii[0]).abs() < 1e-6) && !names.iter().any(|m| o.animated(m)) && radii[0] >= 0.0 { - o.raw("r", json!({"a": 0, "k": r(radii[0])})); + Some(fixed(json!(craft_lottie::round6(radii[0])))) } else { o.ex.warn(format!("{}: per-corner roundness has no Lottie rectangle equivalent (roundness 0 exported)", o.label)); - o.raw("r", json!({"a": 0, "k": 0})); + Some(fixed(json!(0))) } } else { - o.put("r", "roundness", &anim::num); - } + o.put("roundness", &anim::num) + }; + ShapeKind::Rect(Rect { direction, position, size, roundness }) } - "el" => { - dir(&mut o); - o.put("s", "size", &anim::arr2); - o.put("p", "position", &anim::arr2); + "ellipse" => { + let direction = Some(o.direction()); + let size = o.put("size", &anim::arr2); + ShapeKind::Ellipse(Ellipse { direction, position: o.put("position", &anim::arr2), size }) } - "sr" => { - dir(&mut o); - o.raw("sy", json!(o.e("type") + 1)); - o.put("pt", "points", &anim::num); - o.put("p", "position", &anim::arr2); - o.put("r", "rotation", &anim::num); - o.put("ir", "innerRadius", &anim::num); - o.put("or", "outerRadius", &anim::num); - o.put("is", "innerRoundness", &anim::num); - o.put("os", "outerRoundness", &anim::num); + "star" => ShapeKind::PolyStar(PolyStar { + direction: Some(o.direction()), + star_type: Some(i64::from(o.e("type")) + 1), + points: o.put("points", &anim::num), + position: o.put("position", &anim::arr2), + rotation: o.put("rotation", &anim::num), + inner_radius: o.put("innerRadius", &anim::num), + outer_radius: o.put("outerRadius", &anim::num), + inner_roundness: o.put("innerRoundness", &anim::num), + outer_roundness: o.put("outerRoundness", &anim::num), + }), + "path" => ShapeKind::Path(Path { direction: Some(o.direction()), path: o.put("path", &path_value), closed: None }), + "fill" => { + let f = Fill { color: o.put("color", &anim::arr), opacity: o.put("opacity", &anim::num), rule: Some(i64::from(o.e("rule")) + 1) }; + blend = Some(o.blend()); + ShapeKind::Fill(f) } - "sh" => { - dir(&mut o); - o.put("ks", "path", &path_value); + "stroke" => { + let (color, opacity) = (o.put("color", &anim::arr), o.put("opacity", &anim::num)); + let st = stroke_fields(&mut o); + blend = Some(o.blend()); + ShapeKind::Stroke(Stroke { + color, + opacity, + width: st.width, + cap: Some(st.cap), + join: Some(st.join), + miter_limit: Some(st.miter), + miter_limit_animated: None, + dashes: st.dashes, + }) } - "fl" => { - o.put("c", "color", &anim::arr); - o.put("o", "opacity", &anim::num); - o.raw("r", json!(o.e("rule") + 1)); - o.blend(); - } - "st" => { - o.put("c", "color", &anim::arr); - o.put("o", "opacity", &anim::num); - stroke_fields(&mut o); - o.blend(); - } - "gf" | "gs" => { - o.put("o", "opacity", &anim::num); - o.put("s", "start", &anim::arr2); - o.put("e", "end", &anim::arr2); - o.raw("t", json!(o.e("type") + 1)); - o.put("h", "highlightLength", &anim::num); - o.put("a", "highlightAngle", &anim::num); + m @ ("gfill" | "gstroke") => { + let opacity = o.put("opacity", &anim::num); + let (start, end) = (o.put("start", &anim::arr2), o.put("end", &anim::arr2)); + let gradient_type = Some(i64::from(o.e("type")) + 1); + let (highlight_length, highlight_angle) = (o.put("highlightLength", &anim::num), o.put("highlightAngle", &anim::num)); let ncolors = match g.get("colors").map(|p| &p.value) { Some(Value::Gradient(gr)) => gr.colors.len(), _ => 2, }; - if let Some(p) = g.get("colors") { - let k = export_prop(o.ex, p, &gradient_flat, &format!("{lbl} ▸ Colors")); - o.raw("g", json!({"p": ncolors, "k": k})); - } - if ty == "gf" { - o.raw("r", json!(o.e("rule") + 1)); + let colors = g.get("colors").map(|p| GradientColors { + count: Some(ncolors as i64), + stops: Some(export_prop(o.ex, p, &gradient_flat, &format!("{lbl} ▸ Colors"))), + ..Default::default() + }); + let kind = if m == "gfill" { + ShapeKind::GradientFill(GradientFill { + colors, + start, + end, + gradient_type, + highlight_length, + highlight_angle, + opacity, + rule: Some(i64::from(o.e("rule")) + 1), + }) } else { - stroke_fields(&mut o); - } - o.blend(); + let st = stroke_fields(&mut o); + ShapeKind::GradientStroke(GradientStroke { + colors, + start, + end, + gradient_type, + highlight_length, + highlight_angle, + opacity, + width: st.width, + cap: Some(st.cap), + join: Some(st.join), + miter_limit: Some(st.miter), + miter_limit_animated: None, + dashes: st.dashes, + }) + }; + blend = Some(o.blend()); + kind } - "tm" => { - o.put("s", "start", &anim::num); - o.put("e", "end", &anim::num); - o.put("o", "offset", &anim::num); - o.raw("m", json!(o.e("mode") + 1)); - } - "rp" => { - o.put("c", "copies", &anim::num); - o.put("o", "offset", &anim::num); + "trim" => ShapeKind::Trim(Trim { + start: o.put("start", &anim::num), + end: o.put("end", &anim::num), + offset: o.put("offset", &anim::num), + multiple: Some(i64::from(o.e("mode")) + 1), + }), + "repeater" => { + let (copies, offset) = (o.put("copies", &anim::num), o.put("offset", &anim::num)); // Lottie: 1 = Above, 2 = Below; EffectCraft: 0 = Below, 1 = Above. - o.raw("m", json!(if o.e("composite") == 1 { 1 } else { 2 })); - if let Some(t) = g.sub("transform") { - let tr = shape_transform(o.ex, t, &lbl); - o.raw("tr", tr); - } + let composite = Some(if o.e("composite") == 1 { 1 } else { 2 }); + let transform = g.sub("transform").map(|t| repeater_transform(shape_transform(o.ex, t, &lbl))); + ShapeKind::Repeater(Repeater { copies, offset, composite, transform }) } - "rd" => o.put("r", "radius", &anim::num), - "op" => { - o.put("a", "amount", &anim::num); - o.raw("lj", json!(o.e("join") + 1)); - o.put("ml", "miter", &anim::num); + "round" => ShapeKind::RoundCorners(RoundCorners { radius: o.put("radius", &anim::num) }), + "offset" => { + let amount = o.put("amount", &anim::num); + let join = Some(i64::from(o.e("join")) + 1); + let miter_limit = o.put("miter", &anim::num); if (o.f("copies") - 1.0).abs() > 1e-9 || o.animated("copies") || o.f("copyOffset") != 0.0 { o.ex.warn(format!("{lbl}: Offset Paths copies are not part of Lottie (one copy exported)")); } + ShapeKind::OffsetPath(OffsetPath { amount, join, miter_limit }) } - "pb" => o.put("a", "amount", &anim::num), - "tw" => { - o.put("a", "angle", &anim::num); - o.put("c", "center", &anim::arr2); + "pucker" => ShapeKind::PuckerBloat(PuckerBloat { amount: o.put("amount", &anim::num) }), + "twist" => ShapeKind::Twist(Twist { angle: o.put("angle", &anim::num), center: o.put("center", &anim::arr2) }), + "zigzag" => ShapeKind::ZigZag(ZigZag { + frequency: o.put("size", &anim::num), + amount: o.put("ridges", &anim::num), + point_type: Some(fixed(json!(o.e("points") + 1))), + }), + "merge" => ShapeKind::Merge(Merge { mode: Some(i64::from(o.e("mode")) + 1) }), + other => { + o.ex.warn(format!("{lbl}: shape item `{other}` has no Lottie equivalent (skipped)")); + continue; } - "zz" => { - o.put("s", "size", &anim::num); - o.put("r", "ridges", &anim::num); - o.raw("pt", json!({"a": 0, "k": o.e("points") + 1})); - } - "mm" => o.raw("mm", json!(o.e("mode") + 1)), - _ => {} - } - out.push(o.done()); + }; + out.push(Shape { name: Some(g.name.clone()), hidden: (!g.enabled).then_some(true), blend_mode: blend, kind, ..Default::default() }); } out } // ---------------------------------------------------------------- import -fn set(im: &Im, g: &mut PropGroup, m: &str, j: Option<&Json>, conv: &dyn Fn(&Json) -> Option) { +fn set(im: &Im, g: &mut PropGroup, m: &str, j: Option<&Animated>, conv: &dyn Fn(&Json) -> Option) { + if let (Some(j), Some(p)) = (j, g.get_mut(m)) { + import_prop(im, &j.0, p, conv); + } +} + +fn set_json(im: &Im, g: &mut PropGroup, m: &str, j: Option<&Json>, conv: &dyn Fn(&Json) -> Option) { if let (Some(j), Some(p)) = (j, g.get_mut(m)) { import_prop(im, j, p, conv); } @@ -363,43 +399,60 @@ fn set_enum(g: &mut PropGroup, m: &str, v: u32) { } } -fn int(j: &Json, k: &str) -> Option { - let v = j.get(k)?; +/// A whole number however a file writes it: `2`, `2.0`, `2.4` (rounded) or a property `{k: 2}`. +fn int_json(v: Option<&Json>) -> Option { + let v = v?; v.as_i64().or_else(|| v.as_f64().map(|f| f.round() as i64)).or_else(|| v.get("k").and_then(anim::nums).and_then(|n| n.first().map(|f| f.round() as i64))) } -fn import_blend(j: &Json, g: &mut PropGroup) { - if let Some(bm) = j.get("bm").and_then(Json::as_u64) { +/// An integer key of the model, or the value the model kept in `extra` when it was written +/// another way. +fn int(field: Option, extra: &Map, key: &str) -> Option { + field.or_else(|| int_json(extra.get(key))) +} + +fn import_blend(s: &Shape, g: &mut PropGroup) { + if let Some(bm) = int(s.blend_mode, &s.extra, "bm").and_then(|b| u64::try_from(b).ok()) { set_enum(g, "blend", blend_index(crate::blend_from_lottie(bm))); } } -fn import_stroke(im: &Im, ids: &mut Ids, j: &Json, g: &mut PropGroup) { - set(im, g, "width", j.get("w"), &anim::to_scalar); - if let Some(lc) = int(j, "lc") { +/// The stroke keys of `st` and `gs`. +struct StrokeIn<'a> { + width: Option<&'a Animated>, + cap: Option, + join: Option, + miter: Option, + miter_animated: Option<&'a Animated>, + dashes: Option<&'a [Dash]>, +} + +fn import_stroke(im: &Im, ids: &mut Ids, st: StrokeIn<'_>, g: &mut PropGroup) { + set(im, g, "width", st.width, &anim::to_scalar); + if let Some(lc) = st.cap { set_enum(g, "cap", lc.saturating_sub(1).clamp(0, 2) as u32); } - if let Some(lj) = int(j, "lj") { + if let Some(lj) = st.join { set_enum(g, "join", lj.saturating_sub(1).clamp(0, 2) as u32); } if let Some(p) = g.get_mut("miter") { - if let Some(ml) = j.get("ml").and_then(Json::as_f64) { + if let Some(ml) = st.miter { p.value = Value::Scalar(ml); - } else if let Some(ml2) = j.get("ml2") { - import_prop(im, ml2, p, &anim::to_scalar); + } else if let Some(ml2) = st.miter_animated { + import_prop(im, &ml2.0, p, &anim::to_scalar); } } - if let Some(list) = j.get("d").and_then(Json::as_array) + if let Some(list) = st.dashes && let Some(d) = g.sub_mut("dashes") { // The n-th `d` and `g` are the n-th Dash/Gap pair; the extra pairs are created as the Dashes "+" does. let (mut dashes, mut gaps) = (0, 0); for e in list { - let (n, pair) = match e.get("n").and_then(Json::as_str) { + let (n, pair) = match e.kind.as_deref() { Some("d") => (&mut dashes, 0), Some("g") => (&mut gaps, 1), Some("o") => { - set(im, d, "offset", e.get("v"), &anim::to_scalar); + set(im, d, "offset", e.length.as_ref(), &anim::to_scalar); continue; } _ => continue, @@ -418,205 +471,299 @@ fn import_stroke(im: &Im, ids: &mut Ids, j: &Json, g: &mut PropGroup) { [dm, gm][pair] } }; - set(im, d, m, e.get("v"), &anim::to_scalar); + set(im, d, m, e.length.as_ref(), &anim::to_scalar); } } } -fn import_shape_transform(im: &Im, j: &Json, tr: &mut PropGroup) { - set(im, tr, "anchor", j.get("a"), &anim::to_vec2); - set(im, tr, "position", j.get("p"), &anim::to_vec2); - set(im, tr, "scale", j.get("s"), &anim::to_vec2); - set(im, tr, "rotation", j.get("r"), &anim::to_scalar); - set(im, tr, "opacity", j.get("o"), &anim::to_scalar); - set(im, tr, "skew", j.get("sk"), &anim::to_scalar); - set(im, tr, "skewAxis", j.get("sa"), &anim::to_scalar); - set(im, tr, "startOpacity", j.get("so"), &anim::to_scalar); - set(im, tr, "endOpacity", j.get("eo"), &anim::to_scalar); +/// A `tr` (of a group or a repeater), looked up by its Lottie keys. +fn import_shape_transform(im: &Im, get: &dyn Fn(&str) -> Option, tr: &mut PropGroup) { + for (k, m, conv) in [ + ("a", "anchor", anim::to_vec2 as fn(&Json) -> Option), + ("p", "position", anim::to_vec2), + ("s", "scale", anim::to_vec2), + ("r", "rotation", anim::to_scalar), + ("o", "opacity", anim::to_scalar), + ("sk", "skew", anim::to_scalar), + ("sa", "skewAxis", anim::to_scalar), + ("so", "startOpacity", anim::to_scalar), + ("eo", "endOpacity", anim::to_scalar), + ] { + set_json(im, tr, m, get(k).as_ref(), &conv); + } +} + +/// A named key of the model, or the same key kept in `extra`. +fn key(a: Option<&Animated>, extra: &Map, k: &str) -> Option { + a.map(|a| a.0.clone()).or_else(|| extra.get(k).cloned()) +} + +fn group_tr_get<'a>(t: &'a Transform, item: &'a Map) -> impl Fn(&str) -> Option + 'a { + move |k| { + let named = match k { + "a" => t.anchor.as_ref(), + "p" => t.position.as_ref(), + "s" => t.scale.as_ref(), + "r" => t.rotation.as_ref(), + "o" => t.opacity.as_ref(), + "sk" => t.skew.as_ref(), + "sa" => t.skew_axis.as_ref(), + _ => None, + }; + key(named, &t.extra, k).or_else(|| item.get(k).cloned()) + } +} + +fn repeater_tr_get(t: &RepeaterTransform) -> impl Fn(&str) -> Option + '_ { + move |k| { + let named = match k { + "a" => t.anchor.as_ref(), + "p" => t.position.as_ref(), + "s" => t.scale.as_ref(), + "r" => t.rotation.as_ref(), + "so" => t.start_opacity.as_ref(), + "eo" => t.end_opacity.as_ref(), + "sk" => t.skew.as_ref(), + "sa" => t.skew_axis.as_ref(), + _ => None, + }; + key(named, &t.extra, k) + } } /// Import Lottie shape items into shape contents groups. -pub(crate) fn import_items(im: &mut Im, ids: &mut Ids, items: &[Json]) -> Vec { +pub(crate) fn import_items(im: &mut Im, ids: &mut Ids, items: &mut dyn Iterator) -> Vec { let mut out = vec![]; - for j in items { - let ty = j.get("ty").and_then(Json::as_str).unwrap_or(""); - let dir = |g: &mut PropGroup| { - if int(j, "d") == Some(3) { + for s in items { + let x = &s.extra; + let dir = |g: &mut PropGroup, d: Option| { + if int(d, x, "d") == Some(3) { set_enum(g, "direction", 1); } }; - let mut g = match ty { - "gr" => { - let list = j.get("it").and_then(Json::as_array).cloned().unwrap_or_default(); - let inner: Vec = list.iter().filter(|i| i.get("ty").and_then(Json::as_str) != Some("tr")).cloned().collect(); - let children = import_items(im, ids, &inner); + let mut g = match &s.kind { + ShapeKind::Group(gr) => { + let list = gr.items.as_deref().unwrap_or_default(); + // Borrowed, not cloned: each nesting level copying its subtree costs memory + // quadratic in the depth. + let children = import_items(im, ids, &mut list.iter().filter(|i| !matches!(i.kind, ShapeKind::Transform(_)))); let mut g = build::shape_group(ids, "Group 1", children); - if let Some(tr) = list.iter().find(|i| i.get("ty").and_then(Json::as_str) == Some("tr")) - && let Some(t) = g.sub_mut("transform") + if let Some(tr) = list.iter().find(|i| matches!(i.kind, ShapeKind::Transform(_))) + && let ShapeKind::Transform(t) = &tr.kind + && let Some(tg) = g.sub_mut("transform") { - import_shape_transform(im, tr, t); + import_shape_transform(im, &group_tr_get(t, &tr.extra), tg); } - import_blend(j, &mut g); + import_blend(s, &mut g); g } - "rc" => { + ShapeKind::Rect(r) => { let mut g = build::shape_rect(ids, [100.0, 100.0], [0.0, 0.0], 0.0); - dir(&mut g); - set(im, &mut g, "size", j.get("s"), &anim::to_vec2); - set(im, &mut g, "position", j.get("p"), &anim::to_vec2); - set(im, &mut g, "roundness", j.get("r"), &anim::to_scalar); + dir(&mut g, r.direction); + set(im, &mut g, "size", r.size.as_ref(), &anim::to_vec2); + set(im, &mut g, "position", r.position.as_ref(), &anim::to_vec2); + set(im, &mut g, "roundness", r.roundness.as_ref(), &anim::to_scalar); // Lottie has one roundness: keep the per-corner values in step so ticking the box // on an imported rectangle keeps the shape. for m in ["topLeftRoundness", "topRightRoundness", "bottomRightRoundness", "bottomLeftRoundness"] { - set(im, &mut g, m, j.get("r"), &anim::to_scalar); + set(im, &mut g, m, r.roundness.as_ref(), &anim::to_scalar); } g } - "el" => { + ShapeKind::Ellipse(e) => { let mut g = build::shape_ellipse(ids, [100.0, 100.0], [0.0, 0.0]); - dir(&mut g); - set(im, &mut g, "size", j.get("s"), &anim::to_vec2); - set(im, &mut g, "position", j.get("p"), &anim::to_vec2); + dir(&mut g, e.direction); + set(im, &mut g, "size", e.size.as_ref(), &anim::to_vec2); + set(im, &mut g, "position", e.position.as_ref(), &anim::to_vec2); g } - "sr" => { - let star = int(j, "sy").unwrap_or(1) != 2; + ShapeKind::PolyStar(p) => { + let star = int(p.star_type, x, "sy").unwrap_or(1) != 2; let mut g = build::shape_star(ids, star, 5.0, [0.0, 0.0], 100.0, 50.0); - dir(&mut g); - set(im, &mut g, "points", j.get("pt"), &anim::to_scalar); - set(im, &mut g, "position", j.get("p"), &anim::to_vec2); - set(im, &mut g, "rotation", j.get("r"), &anim::to_scalar); - set(im, &mut g, "innerRadius", j.get("ir"), &anim::to_scalar); - set(im, &mut g, "outerRadius", j.get("or"), &anim::to_scalar); - set(im, &mut g, "innerRoundness", j.get("is"), &anim::to_scalar); - set(im, &mut g, "outerRoundness", j.get("os"), &anim::to_scalar); + dir(&mut g, p.direction); + set(im, &mut g, "points", p.points.as_ref(), &anim::to_scalar); + set(im, &mut g, "position", p.position.as_ref(), &anim::to_vec2); + set(im, &mut g, "rotation", p.rotation.as_ref(), &anim::to_scalar); + set(im, &mut g, "innerRadius", p.inner_radius.as_ref(), &anim::to_scalar); + set(im, &mut g, "outerRadius", p.outer_radius.as_ref(), &anim::to_scalar); + set(im, &mut g, "innerRoundness", p.inner_roundness.as_ref(), &anim::to_scalar); + set(im, &mut g, "outerRoundness", p.outer_roundness.as_ref(), &anim::to_scalar); g } - "sh" => { + ShapeKind::Path(p) => { let mut g = build::shape_path(ids, ShapePath::default()); - dir(&mut g); - let closed = j.get("closed").and_then(Json::as_bool).unwrap_or(false); - set(im, &mut g, "path", j.get("ks"), &|v| to_path(v, closed)); + dir(&mut g, p.direction); + let closed = p.closed.unwrap_or(false); + set(im, &mut g, "path", p.path.as_ref(), &|v| to_path(v, closed)); g } - "fl" => { + ShapeKind::Fill(f) => { let mut g = build::shape_fill(ids, [1.0, 1.0, 1.0, 1.0]); - set(im, &mut g, "color", j.get("c"), &anim::to_color); - set(im, &mut g, "opacity", j.get("o"), &anim::to_scalar); - if int(j, "r") == Some(2) { + set(im, &mut g, "color", f.color.as_ref(), &anim::to_color); + set(im, &mut g, "opacity", f.opacity.as_ref(), &anim::to_scalar); + if int(f.rule, x, "r") == Some(2) { set_enum(&mut g, "rule", 1); } - import_blend(j, &mut g); + import_blend(s, &mut g); g } - "st" => { + ShapeKind::Stroke(st) => { let mut g = build::shape_stroke(ids, [1.0, 1.0, 1.0, 1.0], 2.0); - set(im, &mut g, "color", j.get("c"), &anim::to_color); - set(im, &mut g, "opacity", j.get("o"), &anim::to_scalar); - import_stroke(im, ids, j, &mut g); - import_blend(j, &mut g); - g - } - "gf" | "gs" => { - let radial = int(j, "t") == Some(2); - let mut g = if ty == "gf" { - build::shape_gradient_fill(ids, radial, [0.0, 0.0], [100.0, 0.0], Gradient::default()) - } else { - build::shape_gradient_stroke(ids, radial, [0.0, 0.0], [100.0, 0.0], Gradient::default(), 2.0) + set(im, &mut g, "color", st.color.as_ref(), &anim::to_color); + set(im, &mut g, "opacity", st.opacity.as_ref(), &anim::to_scalar); + let fields = StrokeIn { + width: st.width.as_ref(), + cap: int(st.cap, x, "lc"), + join: int(st.join, x, "lj"), + miter: st.miter_limit, + miter_animated: st.miter_limit_animated.as_ref(), + dashes: st.dashes.as_deref(), }; - set(im, &mut g, "opacity", j.get("o"), &anim::to_scalar); - set(im, &mut g, "start", j.get("s"), &anim::to_vec2); - set(im, &mut g, "end", j.get("e"), &anim::to_vec2); - set(im, &mut g, "highlightLength", j.get("h"), &anim::to_scalar); - set(im, &mut g, "highlightAngle", j.get("a"), &anim::to_scalar); - if let Some(gj) = j.get("g") { - let n = gj.get("p").and_then(Json::as_u64).unwrap_or(2) as usize; - if let Some(k) = gj.get("k") { - set(im, &mut g, "colors", Some(k), &move |v| to_gradient(v, n)); - } - } - if ty == "gf" { - if int(j, "r") == Some(2) { - set_enum(&mut g, "rule", 1); - } - } else { - import_stroke(im, ids, j, &mut g); - } - import_blend(j, &mut g); + import_stroke(im, ids, fields, &mut g); + import_blend(s, &mut g); g } - "tm" => { + ShapeKind::GradientFill(gf) => { + let radial = int(gf.gradient_type, x, "t") == Some(2); + let mut g = build::shape_gradient_fill(ids, radial, [0.0, 0.0], [100.0, 0.0], Gradient::default()); + gradient_common( + im, + &mut g, + gf.opacity.as_ref(), + gf.start.as_ref(), + gf.end.as_ref(), + gf.highlight_length.as_ref(), + gf.highlight_angle.as_ref(), + gf.colors.as_ref(), + ); + if int(gf.rule, x, "r") == Some(2) { + set_enum(&mut g, "rule", 1); + } + import_blend(s, &mut g); + g + } + ShapeKind::GradientStroke(gs) => { + let radial = int(gs.gradient_type, x, "t") == Some(2); + let mut g = build::shape_gradient_stroke(ids, radial, [0.0, 0.0], [100.0, 0.0], Gradient::default(), 2.0); + gradient_common( + im, + &mut g, + gs.opacity.as_ref(), + gs.start.as_ref(), + gs.end.as_ref(), + gs.highlight_length.as_ref(), + gs.highlight_angle.as_ref(), + gs.colors.as_ref(), + ); + let fields = StrokeIn { + width: gs.width.as_ref(), + cap: int(gs.cap, x, "lc"), + join: int(gs.join, x, "lj"), + miter: gs.miter_limit, + miter_animated: gs.miter_limit_animated.as_ref(), + dashes: gs.dashes.as_deref(), + }; + import_stroke(im, ids, fields, &mut g); + import_blend(s, &mut g); + g + } + ShapeKind::Trim(t) => { let mut g = build::shape_trim(ids, 0.0, 100.0, 0.0); - set(im, &mut g, "start", j.get("s"), &anim::to_scalar); - set(im, &mut g, "end", j.get("e"), &anim::to_scalar); - set(im, &mut g, "offset", j.get("o"), &anim::to_scalar); - if int(j, "m") == Some(2) { + set(im, &mut g, "start", t.start.as_ref(), &anim::to_scalar); + set(im, &mut g, "end", t.end.as_ref(), &anim::to_scalar); + set(im, &mut g, "offset", t.offset.as_ref(), &anim::to_scalar); + if int(t.multiple, x, "m") == Some(2) { set_enum(&mut g, "mode", 1); } g } - "rp" => { + ShapeKind::Repeater(r) => { let mut g = build::shape_repeater(ids, 3.0, [100.0, 0.0]); - set(im, &mut g, "copies", j.get("c"), &anim::to_scalar); - set(im, &mut g, "offset", j.get("o"), &anim::to_scalar); - set_enum(&mut g, "composite", if int(j, "m") == Some(1) { 1 } else { 0 }); - if let (Some(tr), Some(t)) = (j.get("tr"), g.sub_mut("transform")) { - import_shape_transform(im, tr, t); + set(im, &mut g, "copies", r.copies.as_ref(), &anim::to_scalar); + set(im, &mut g, "offset", r.offset.as_ref(), &anim::to_scalar); + set_enum(&mut g, "composite", if int(r.composite, x, "m") == Some(1) { 1 } else { 0 }); + if let (Some(tr), Some(t)) = (r.transform.as_ref(), g.sub_mut("transform")) { + import_shape_transform(im, &repeater_tr_get(tr), t); } g } - "rd" | "op" | "pb" | "tw" | "zz" | "mm" => { - let kind = match ty { - "rd" => "round", - "op" => "offset", - "pb" => "pucker", - "tw" => "twist", - "zz" => "zigzag", - _ => "merge", - }; - let Some(mut g) = build::shape_simple_op(ids, kind) else { continue }; - match ty { - "rd" => set(im, &mut g, "radius", j.get("r"), &anim::to_scalar), - "op" => { - set(im, &mut g, "amount", j.get("a"), &anim::to_scalar); - if let Some(lj) = int(j, "lj") { - set_enum(&mut g, "join", lj.saturating_sub(1).clamp(0, 2) as u32); - } - set(im, &mut g, "miter", j.get("ml"), &anim::to_scalar); - } - "pb" => set(im, &mut g, "amount", j.get("a"), &anim::to_scalar), - "tw" => { - set(im, &mut g, "angle", j.get("a"), &anim::to_scalar); - set(im, &mut g, "center", j.get("c"), &anim::to_vec2); - } - "zz" => { - set(im, &mut g, "size", j.get("s"), &anim::to_scalar); - set(im, &mut g, "ridges", j.get("r"), &anim::to_scalar); - if int(j, "pt") == Some(2) { - set_enum(&mut g, "points", 1); - } - } - _ => { - if let Some(m) = int(j, "mm") { - set_enum(&mut g, "mode", m.saturating_sub(1).clamp(0, 4) as u32); - } - } + ShapeKind::RoundCorners(r) => { + let Some(mut g) = build::shape_simple_op(ids, "round") else { continue }; + set(im, &mut g, "radius", r.radius.as_ref(), &anim::to_scalar); + g + } + ShapeKind::OffsetPath(op) => { + let Some(mut g) = build::shape_simple_op(ids, "offset") else { continue }; + set(im, &mut g, "amount", op.amount.as_ref(), &anim::to_scalar); + if let Some(lj) = int(op.join, x, "lj") { + set_enum(&mut g, "join", lj.saturating_sub(1).clamp(0, 2) as u32); + } + set(im, &mut g, "miter", op.miter_limit.as_ref(), &anim::to_scalar); + g + } + ShapeKind::PuckerBloat(pb) => { + let Some(mut g) = build::shape_simple_op(ids, "pucker") else { continue }; + set(im, &mut g, "amount", pb.amount.as_ref(), &anim::to_scalar); + g + } + ShapeKind::Twist(tw) => { + let Some(mut g) = build::shape_simple_op(ids, "twist") else { continue }; + set(im, &mut g, "angle", tw.angle.as_ref(), &anim::to_scalar); + set(im, &mut g, "center", tw.center.as_ref(), &anim::to_vec2); + g + } + ShapeKind::ZigZag(zz) => { + let Some(mut g) = build::shape_simple_op(ids, "zigzag") else { continue }; + set(im, &mut g, "size", zz.frequency.as_ref(), &anim::to_scalar); + set(im, &mut g, "ridges", zz.amount.as_ref(), &anim::to_scalar); + if int_json(zz.point_type.as_ref().map(|a| &a.0)) == Some(2) { + set_enum(&mut g, "points", 1); } g } - "tr" => continue, - other => { - im.warn(format!("shape item `{other}` is not supported (skipped)")); + ShapeKind::Merge(mm) => { + let Some(mut g) = build::shape_simple_op(ids, "merge") else { continue }; + if let Some(m) = int(mm.mode, x, "mm") { + set_enum(&mut g, "mode", m.saturating_sub(1).clamp(0, 4) as u32); + } + g + } + ShapeKind::Transform(_) => continue, + ShapeKind::Other(ty) => { + im.warn(format!("shape item `{}` is not supported (skipped)", ty.as_str().unwrap_or(""))); continue; } }; - if let Some(nm) = j.get("nm").and_then(Json::as_str) { - g.name = nm.to_string(); + if let Some(nm) = &s.name { + g.name = nm.clone(); } - if j.get("hd").and_then(Json::as_bool) == Some(true) { + if s.hidden == Some(true) { g.enabled = false; } out.push(g); } out } + +/// The keys `gf` and `gs` share. +#[allow(clippy::too_many_arguments)] +fn gradient_common( + im: &Im, + g: &mut PropGroup, + opacity: Option<&Animated>, + start: Option<&Animated>, + end: Option<&Animated>, + highlight_length: Option<&Animated>, + highlight_angle: Option<&Animated>, + colors: Option<&GradientColors>, +) { + set(im, g, "opacity", opacity, &anim::to_scalar); + set(im, g, "start", start, &anim::to_vec2); + set(im, g, "end", end, &anim::to_vec2); + set(im, g, "highlightLength", highlight_length, &anim::to_scalar); + set(im, g, "highlightAngle", highlight_angle, &anim::to_scalar); + if let Some(c) = colors { + let n = c.count.and_then(|n| usize::try_from(n).ok()).unwrap_or(2); + set(im, g, "colors", c.stops.as_ref(), &move |v| to_gradient(v, n)); + } +} diff --git a/crates/lottie/src/tests.rs b/crates/lottie/src/tests.rs index 414c875f0..35c52026d 100644 --- a/crates/lottie/src/tests.rs +++ b/crates/lottie/src/tests.rs @@ -381,7 +381,7 @@ fn offset_paths_miter_uses_animatable_lottie_value() { assert_eq!(shapes[0]["ml"]["k"][1]["s"], json!([9.0])); assert_eq!(shapes[1]["ml"], json!({"a": 0, "k": 6.0})); assert_eq!(shapes[2]["ty"], "st"); - assert_eq!(shapes[2]["ml"], json!(7.0)); + assert_eq!(shapes[2]["ml"].as_f64(), Some(7.0)); assert!(compare_comps(&p, cid, &q, nc) > 10); let restored = &q.comp(nc).unwrap().layers[0]; assert_eq!(restored.props.group("contents/offset").unwrap().get("miter").unwrap().keys.len(), 2); @@ -725,7 +725,7 @@ fn effects_expressions_and_warnings() { #[test] fn images_embed_as_base64_and_come_back() { let (mut p, cid) = setup(); - let png: Vec = vec![0x89, b'P', b'N', b'G', 1, 2, 3, 4, 5]; + let png: Vec = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a, 1, 2, 3, 4, 5]; let fid = p.add_item( "pic.png", Label::Lavender, @@ -775,6 +775,17 @@ fn images_embed_as_base64_and_come_back() { assert!(f.path.starts_with("/tmp/image_") && f.path.ends_with(".png"), "{}", f.path); } +#[test] +fn image_signatures_are_matched_in_full() { + use crate::export::image_mime; + assert_eq!(image_mime(b"\x89PNG\r\n\x1a\n\0"), Some("image/png")); + assert_eq!(image_mime(b"GIF89a\0"), Some("image/gif")); + assert_eq!(image_mime(b"GIF87a\0"), Some("image/gif")); + // Near matches (a prefix of the signature, or text that starts with it) are not images. + assert_eq!(image_mime(b"\x89PNG\x01\x02\x03\x04"), None); + assert_eq!(image_mime(b"GIF8 is a text file"), None); +} + #[test] fn dotlottie_archive_roundtrip() { let (mut p, cid) = setup(); @@ -985,3 +996,69 @@ fn per_corner_roundness_exports_equal_radii_and_warns_when_mixed() { assert_eq!(rc(&json)["r"]["k"], json!(5.0)); assert!(!warnings.iter().any(|w| w.contains("per-corner")), "{warnings:?}"); } + +#[test] +fn many_layers_and_precomps_import_with_their_parents_and_sizes() { + // 5 000 null layers each parented to the previous, and 2 000 empty precomp assets: the + // relationship and size passes index the layers instead of searching them per layer. (Each + // has its own name: `Comp::unique_layer_name` numbering one repeated name is a separate cost.) + let n = 5_000; + let layers: Vec = (1..=n).map(|i| json!({"ty": 3, "nm": format!("N{i}"), "ind": i, "parent": i - 1, "ip": 0, "op": 30, "st": 0, "ks": {}})).collect(); + let mut assets: Vec = (0..2_000).map(|i| json!({"id": format!("c{i}"), "layers": []})).collect(); + assets.push(json!({"id": "last", "layers": []})); + let mut layers = layers; + layers.push(json!({"ty": 0, "ind": n + 1, "refId": "last", "w": 50, "h": 40, "ip": 0, "op": 30, "st": 0, "ks": {}})); + let doc = json!({"w": 200, "h": 120, "fr": 30, "ip": 0, "op": 30, "assets": assets, "layers": layers}); + let mut q = Project::default(); + let nc = import(&mut q, doc.to_string().as_bytes(), "Imported", "", &mut |_| None).unwrap().comp; + let c = q.comp(nc).unwrap(); + assert_eq!(c.layers.len(), n as usize + 1); + assert_eq!(c.layers.get(0).unwrap().parent, None); + assert_eq!(c.layers.get(1).unwrap().parent, Some(c.layers.get(0).unwrap().id)); + let LayerSource::Comp { item } = c.layers.get(n as usize).unwrap().source else { panic!("not a precomp") }; + let pc = q.comp(item).unwrap(); + assert_eq!((pc.width, pc.height), (50, 40)); +} + +#[test] +fn deeply_nested_shape_groups_and_long_font_names_import() { + let mut g = json!({"ty": "rc", "s": {"a": 0, "k": [10, 10]}, "p": {"a": 0, "k": [0, 0]}, "r": {"a": 0, "k": 0}}); + // 50 levels: each is two levels of JSON, within serde_json's recursion limit of 128. + for _ in 0..50 { + g = json!({"ty": "gr", "it": [g, {"ty": "tr", "o": {"a": 0, "k": 100}}]}); + } + let family = "F".repeat(100_000); + let keys: Vec = (0..50).map(|i| json!({"t": i, "s": {"t": "x", "f": "A", "s": 10}})).collect(); + let doc = json!({"w": 200, "h": 120, "fr": 30, "ip": 0, "op": 60, + "fonts": {"list": [{"fName": "A", "fFamily": family, "fStyle": "Regular"}]}, + "layers": [ + {"ty": 4, "ind": 1, "ip": 0, "op": 60, "st": 0, "ks": {}, "shapes": [g]}, + {"ty": 5, "ind": 2, "ip": 0, "op": 60, "st": 0, "ks": {}, "t": {"d": {"k": keys}}} + ]}); + let mut q = Project::default(); + let nc = import(&mut q, doc.to_string().as_bytes(), "Imported", "", &mut |_| None).unwrap().comp; + let c = q.comp(nc).unwrap(); + let st = c.layers.get(1).unwrap().props.prop("text/sourceText").unwrap(); + assert_eq!(st.keys.len(), 50); + let Value::Text(d) = &st.keys[0].value else { panic!("not text") }; + assert_eq!(d.font.len(), 256); +} + +/// A footage item called a still whose file is not an image is not embedded. +#[test] +fn only_image_bytes_are_embedded() { + let (mut p, cid) = setup(); + let fid = p.add_item( + "secret.png", + Label::Lavender, + None, + ItemKind::Footage(Footage { path: "/virtual/secret.png".into(), kind: FootageKind::Still, width: 8, height: 8, has_video: true, ..Default::default() }), + ); + let c = comp(&p, cid); + let l = build::layer(&mut p, &c, "secret.png", LayerSource::Footage { item: fid }, (8, 8), None); + push(&mut p, cid, l); + let read = |path: &str| (path == "/virtual/secret.png").then(|| b"root:x:0:0:root:/root:/bin/sh\n".to_vec()); + let res = export_comp(&p, cid, &ExportOptions::default(), &read).unwrap(); + assert!(res.json["assets"].as_array().is_none_or(|a| a.is_empty()), "{}", res.json["assets"]); + assert!(res.warnings.iter().any(|w| w.contains("not a PNG")), "{:?}", res.warnings); +} diff --git a/crates/lottie/src/text.rs b/crates/lottie/src/text.rs index 125835c2b..39199cfb3 100644 --- a/crates/lottie/src/text.rs +++ b/crates/lottie/src/text.rs @@ -1,14 +1,18 @@ //! Text layers ↔ Lottie text data (`t`: document keys `d`, animators `a`, more options `m`), //! and text converted to glyph shapes. +use craft_lottie::model::{ + Font, Map, Path, Shape, ShapeKind, TextAlign, TextData, TextDocKey, TextDocs, TextDocument, TextFollowPath, TextRange, TextSelector, TextStyle, +}; +use craft_lottie::{Animated, Json}; use effectcraft_keyframe::{Justify, TextDoc, Value}; use effectcraft_project::build::{self, Ids}; use effectcraft_project::{Layer, Node, PropGroup}; use effectcraft_time::Tick; -use serde_json::{Map, Value as Json, json}; +use serde_json::json; use crate::anim::{self, Ex, Im, export_prop, import_prop}; -use crate::shapes; +use crate::shapes::{self, fixed}; const JUSTIFY: [Justify; 7] = [Justify::Left, Justify::Right, Justify::Center, Justify::JustifyLastLeft, Justify::JustifyLastRight, Justify::JustifyLastCenter, Justify::JustifyAll]; @@ -18,47 +22,49 @@ pub(crate) fn font_name(family: &str, style: &str) -> String { format!("{}-{}", family.replace(' ', ""), style.replace(' ', "")) } -fn rgb(c: [f32; 4]) -> Json { - json!([c[0] as f64, c[1] as f64, c[2] as f64]) +fn rgb(c: [f32; 4]) -> Vec { + vec![f64::from(c[0]), f64::from(c[1]), f64::from(c[2])] } -fn doc_json(d: &TextDoc) -> Json { - let mut o = json!({ - "t": d.text.replace("\r\n", "\r").replace('\n', "\r"), - "f": font_name(&d.font, &d.style), - "s": d.size, - "j": JUSTIFY.iter().position(|j| *j == d.justify).unwrap_or(0), - "tr": d.tracking, - "lh": d.leading.unwrap_or(d.size * 1.2), - "ls": d.baseline_shift, - "of": d.stroke_over_fill, - }); - if d.apply_fill { - o["fc"] = rgb(d.fill); +fn doc_json(d: &TextDoc) -> TextDocument { + let stroke = d.apply_stroke && d.stroke_width > 0.0; + TextDocument { + text: Some(d.text.replace("\r\n", "\r").replace('\n', "\r")), + font: Some(font_name(&d.font, &d.style)), + size: Some(d.size), + justify: Some(JUSTIFY.iter().position(|j| *j == d.justify).unwrap_or(0) as i64), + tracking: Some(d.tracking), + line_height: Some(d.leading.unwrap_or(d.size * 1.2)), + baseline_shift: Some(d.baseline_shift), + stroke_over_fill: Some(d.stroke_over_fill), + fill: d.apply_fill.then(|| rgb(d.fill)), + stroke: stroke.then(|| rgb(d.stroke)), + stroke_width: stroke.then_some(d.stroke_width), + caps: if d.all_caps { + Some(1) + } else if d.small_caps { + Some(2) + } else { + None + }, + box_size: d.box_size.map(|b| b.to_vec()), + box_position: d.box_size.map(|_| d.box_pos.to_vec()), + ..Default::default() } - if d.apply_stroke && d.stroke_width > 0.0 { - o["sc"] = rgb(d.stroke); - o["sw"] = json!(d.stroke_width); - } - if d.all_caps { - o["ca"] = json!(1); - } else if d.small_caps { - o["ca"] = json!(2); - } - if let Some(b) = d.box_size { - o["sz"] = json!(b); - o["ps"] = json!(d.box_pos); - } - o } -fn doc_from(j: &Json, fonts: &[(String, String, String)]) -> TextDoc { +/// A colour of a text document (`fc`, `sc`), 0..=1 or 0..=255. +fn doc_color(c: Option<&Vec>, extra: &Map, key: &str) -> Option<[f32; 4]> { + let j = c.filter(|c| !c.is_empty()).map(|c| json!(c)).or_else(|| extra.get(key).cloned())?; + anim::to_color(&j).map(|v| v.as_color()) +} + +fn doc_from(j: &TextDocument, fonts: &[(String, String, String)]) -> TextDoc { let mut d = TextDoc::default(); - let f = |k: &str| j.get(k).and_then(Json::as_f64); - if let Some(t) = j.get("t").and_then(Json::as_str) { + if let Some(t) = &j.text { d.text = t.replace(['\r', '\u{3}'], "\n"); } - if let Some(name) = j.get("f").and_then(Json::as_str) { + if let Some(name) = &j.font { match fonts.iter().find(|(n, _, _)| n == name) { Some((_, fam, st)) => { d.font = fam.clone(); @@ -71,40 +77,39 @@ fn doc_from(j: &Json, fonts: &[(String, String, String)]) -> TextDoc { } } } - if let Some(s) = f("s") { + if let Some(s) = j.size { d.size = s; } - let col = |k: &str| j.get(k).and_then(anim::to_color).map(|v| v.as_color()); - match col("fc") { + match doc_color(j.fill.as_ref(), &j.extra, "fc") { Some(c) => d.fill = c, None => d.apply_fill = false, } - if let Some(c) = col("sc") { + if let Some(c) = doc_color(j.stroke.as_ref(), &j.extra, "sc") { d.stroke = c; d.apply_stroke = true; - d.stroke_width = f("sw").unwrap_or(1.0); + d.stroke_width = j.stroke_width.unwrap_or(1.0); } - if let Some(of) = j.get("of").and_then(Json::as_bool) { + if let Some(of) = j.stroke_over_fill { d.stroke_over_fill = of; } - if let Some(i) = j.get("j").and_then(Json::as_u64) { - d.justify = JUSTIFY.get(i as usize).copied().unwrap_or_default(); + if let Some(i) = j.justify.and_then(|i| usize::try_from(i).ok()) { + d.justify = JUSTIFY.get(i).copied().unwrap_or_default(); } - d.tracking = f("tr").unwrap_or(0.0); - if let Some(lh) = f("lh") + d.tracking = j.tracking.unwrap_or(0.0); + if let Some(lh) = j.line_height && (lh - d.size * 1.2).abs() > 1e-6 { d.leading = Some(lh); } - d.baseline_shift = f("ls").unwrap_or(0.0); - match j.get("ca").and_then(Json::as_u64) { + d.baseline_shift = j.baseline_shift.unwrap_or(0.0); + match j.caps { Some(1) => d.all_caps = true, Some(2) => d.small_caps = true, _ => {} } - if let Some(sz) = j.get("sz").and_then(anim::nums).filter(|v| v.len() >= 2) { + if let Some(sz) = j.box_size.as_deref().filter(|v| v.len() >= 2) { d.box_size = Some([sz[0], sz[1]]); - if let Some(ps) = j.get("ps").and_then(anim::nums).filter(|v| v.len() >= 2) { + if let Some(ps) = j.box_position.as_deref().filter(|v| v.len() >= 2) { d.box_pos = [ps[0], ps[1]]; } } @@ -136,28 +141,86 @@ fn conv_for(v: &Value) -> fn(&Value) -> Json { } } +/// The field of a range animator's style for a Lottie key. +fn style_field<'a>(st: &'a mut TextStyle, key: &str) -> Option<&'a mut Option> { + Some(match key { + "p" => &mut st.position, + "a" => &mut st.anchor, + "s" => &mut st.scale, + "r" => &mut st.rotation, + "rx" => &mut st.rotation_x, + "ry" => &mut st.rotation_y, + "sk" => &mut st.skew, + "sa" => &mut st.skew_axis, + "o" => &mut st.opacity, + "fc" => &mut st.fill, + "fh" => &mut st.fill_hue, + "fs" => &mut st.fill_saturation, + "fb" => &mut st.fill_brightness, + "fo" => &mut st.fill_opacity, + "sc" => &mut st.stroke, + "sw" => &mut st.stroke_width, + "so" => &mut st.stroke_opacity, + "t" => &mut st.tracking, + "ls" => &mut st.line_spacing, + "bl" => &mut st.blur, + _ => return None, + }) +} + +fn style_set(st: &mut TextStyle, key: &str, v: Animated) { + match style_field(st, key) { + Some(f) => *f = Some(v), + None => { + st.extra.insert(key.into(), v.0); + } + } +} + +fn style_get(st: &TextStyle, key: &str) -> Option { + let named = match key { + "p" => &st.position, + "a" => &st.anchor, + "s" => &st.scale, + "r" => &st.rotation, + "sk" => &st.skew, + "sa" => &st.skew_axis, + "o" => &st.opacity, + "fc" => &st.fill, + "fh" => &st.fill_hue, + "fs" => &st.fill_saturation, + "fb" => &st.fill_brightness, + "sc" => &st.stroke, + "sw" => &st.stroke_width, + "t" => &st.tracking, + _ => &None, + }; + named.as_ref().map(|a| a.0.clone()).or_else(|| st.extra.get(key).cloned()) +} + /// Export a text layer's `t` object. Fonts used are added to `fonts`. -pub(crate) fn export_text(ex: &mut Ex, layer: &Layer, fonts: &mut Vec) -> Json { - let Some(tg) = layer.props.sub("text") else { return json!({}) }; +pub(crate) fn export_text(ex: &mut Ex, layer: &Layer, fonts: &mut Vec) -> TextData { + let Some(tg) = layer.props.sub("text") else { return TextData::default() }; let label = &layer.name; let mut docs = vec![]; let mut add_font = |d: &TextDoc| { let n = font_name(&d.font, &d.style); - if !fonts.iter().any(|f| f["fName"] == json!(n)) { - fonts.push(json!({"fName": n, "fFamily": d.font, "fStyle": d.style, "ascent": 75})); + if !fonts.iter().any(|f| f.name.as_deref() == Some(n.as_str())) { + fonts.push(Font { name: Some(n), family: Some(d.font.clone()), style: Some(d.style.clone()), ascent: Some(75.0), ..Default::default() }); } }; + let key = |t: f64, d: &TextDoc| TextDocKey { time: Some(t), document: Some(doc_json(d)), ..Default::default() }; if let Some(st) = tg.get("sourceText") { if st.keys.is_empty() { if let Value::Text(d) = &st.value { add_font(d); - docs.push(json!({"t": 0, "s": doc_json(d)})); + docs.push(key(0.0, d)); } } else { for k in &st.keys { if let Value::Text(d) = &k.value { add_font(d); - docs.push(json!({"t": ex.tb.frame(k.time), "s": doc_json(d)})); + docs.push(key(ex.tb.frame(k.time), d)); } } } @@ -184,22 +247,28 @@ pub(crate) fn export_text(ex: &mut Ex, layer: &Layer, fonts: &mut Vec) -> animators.push(export_animator(ex, a, &format!("{label} ▸ {}", a.name))); } } - let mut m = json!({"g": 1, "a": {"a": 0, "k": [0, 0]}}); + let mut m = TextAlign { grouping: Some(1), anchor: Some(fixed(json!([0, 0]))), ..Default::default() }; if let Some(more) = tg.sub("moreOptions") { - m["g"] = json!(more.get("anchorGrouping").map(|p| p.value.as_enum()).unwrap_or(0) + 1); + m.grouping = Some(i64::from(more.get("anchorGrouping").map(|p| p.value.as_enum()).unwrap_or(0)) + 1); if let Some(p) = more.get("groupingAlignment") { - m["a"] = export_prop(ex, p, &anim::arr2, &format!("{label} ▸ Grouping Alignment")); + m.anchor = Some(export_prop(ex, p, &anim::arr2, &format!("{label} ▸ Grouping Alignment"))); } } if tg.sub("pathOptions").and_then(|p| p.get("path")).is_some_and(|p| p.value.as_enum() > 0) { ex.warn(format!("{label}: text on a path is not exported")); } - json!({"d": {"k": docs}, "a": animators, "m": m, "p": {}}) + TextData { + documents: Some(TextDocs { keys: Some(docs), ..Default::default() }), + ranges: Some(animators), + alignment: Some(m), + follow_path: Some(TextFollowPath::default()), + ..Default::default() + } } /// Text animators reproducing the style runs of `d` (fill, stroke, stroke width, size, /// tracking, baseline shift) over their character ranges. -fn style_run_animators(ex: &mut Ex, d: &TextDoc, label: &str) -> Vec { +fn style_run_animators(ex: &mut Ex, d: &TextDoc, label: &str) -> Vec { let base = d.base_style(); let mut out = vec![]; let mut start = 0usize; @@ -211,63 +280,71 @@ fn style_run_animators(ex: &mut Ex, d: &TextDoc, label: &str) -> Vec { if *s == base || run.len == 0 { continue; } - let k = |v: Json| json!({"a": 0, "k": v}); - let mut props = Map::new(); + let k = fixed; + let mut props = TextStyle::default(); if s.apply_fill && s.fill != base.fill { - props.insert("fc".into(), k(json!([s.fill[0] as f64, s.fill[1] as f64, s.fill[2] as f64, 1.0]))); + props.fill = Some(k(json!([s.fill[0] as f64, s.fill[1] as f64, s.fill[2] as f64, 1.0]))); } if s.apply_stroke && s.stroke != base.stroke { - props.insert("sc".into(), k(json!([s.stroke[0] as f64, s.stroke[1] as f64, s.stroke[2] as f64, 1.0]))); + props.stroke = Some(k(json!([s.stroke[0] as f64, s.stroke[1] as f64, s.stroke[2] as f64, 1.0]))); } if (s.stroke_width - base.stroke_width).abs() > 1e-9 { - props.insert("sw".into(), k(json!(s.stroke_width - base.stroke_width))); + props.stroke_width = Some(k(json!(s.stroke_width - base.stroke_width))); } if (s.size - base.size).abs() > 1e-9 && base.size > 0.0 { let p = s.size / base.size * 100.0; - props.insert("s".into(), k(json!([p, p, 100.0]))); + props.scale = Some(k(json!([p, p, 100.0]))); } if (s.tracking - base.tracking).abs() > 1e-9 { // Lottie tracking is in 1/1000 em like the Character panel's. - props.insert("t".into(), k(json!(s.tracking - base.tracking))); + props.tracking = Some(k(json!(s.tracking - base.tracking))); } if (s.baseline_shift - base.baseline_shift).abs() > 1e-9 { - props.insert("p".into(), k(json!([0.0, -(s.baseline_shift - base.baseline_shift), 0.0]))); + props.position = Some(k(json!([0.0, -(s.baseline_shift - base.baseline_shift), 0.0]))); } let lost = s.font != base.font || s.style != base.style || s.faux_bold != base.faux_bold || s.faux_italic != base.faux_italic; if lost && !warned { ex.warn(format!("{label}: per-character fonts and faux styles are not part of Lottie text (base font used)")); warned = true; } - if props.is_empty() { + if props == TextStyle::default() { continue; } - out.push(json!({ - "nm": format!("Style {a}–{b}"), - "s": { - "t": 0, "xe": k(json!(0)), "ne": k(json!(0)), "a": k(json!(100)), "b": 1, "rn": 0, "sh": 1, - "r": 2, "m": k(json!(1)), "s": k(json!(a)), "e": k(json!(b)), "o": k(json!(0)), - }, - "a": Json::Object(props), - })); + let mut sel = TextSelector { + kind: Some(0), + ease_high: Some(k(json!(0))), + ease_low: Some(k(json!(0))), + amount: Some(k(json!(100))), + based_on: Some(1), + randomize: Some(0), + shape: Some(1), + units: Some(2), + start: Some(k(json!(a))), + end: Some(k(json!(b))), + offset: Some(k(json!(0))), + ..Default::default() + }; + sel.extra.insert("m".into(), k(json!(1)).0); + out.push(TextRange { name: Some(format!("Style {a}–{b}")), selector: Some(sel), style: Some(props), ..Default::default() }); } out } -fn export_animator(ex: &mut Ex, a: &PropGroup, label: &str) -> Json { - let mut props = Map::new(); +fn export_animator(ex: &mut Ex, a: &PropGroup, label: &str) -> TextRange { + let mut props = TextStyle::default(); if let Some(pg) = a.sub("properties") { for p in pg.props() { match ANIM_PROPS.iter().find(|(_, _, m)| *m == p.match_id) { Some((key, _, _)) => { let v = export_prop(ex, p, &conv_for(&p.value), &format!("{label} ▸ {}", p.name)); - props.insert((*key).into(), v); + style_set(&mut props, key, v); } None if matches!(p.match_id.as_str(), "trackingType") => {} None => ex.warn(format!("{label} ▸ {}: text animator property not supported by Lottie", p.name)), } } } - let mut sel = json!({"t": 0}); + let mut sel = TextSelector { kind: Some(0), ..Default::default() }; let sels: Vec<&PropGroup> = a.sub("selectors").map(|s| s.groups().collect()).unwrap_or_default(); if sels.len() > 1 { ex.warn(format!("{label}: only the first selector is exported (Lottie animators have one)")); @@ -276,61 +353,56 @@ fn export_animator(ex: &mut Ex, a: &PropGroup, label: &str) -> Json { if s.match_id != "rangeSelector" { ex.warn(format!("{label}: {} is not supported by Lottie", s.name)); } else { - for (k, m) in [("s", "start"), ("e", "end"), ("o", "offset")] { - if let Some(p) = s.get(m) { - sel[k] = export_prop(ex, p, &anim::num, &format!("{label} ▸ {}", p.name)); - } - } + let mut prop = |m: &str| s.get(m).map(|p| export_prop(ex, p, &anim::num, &format!("{label} ▸ {}", p.name))); + (sel.start, sel.end, sel.offset) = (prop("start"), prop("end"), prop("offset")); if let Some(adv) = s.sub("advanced") { - let e = |m: &str| adv.get(m).map(|p| p.value.as_enum()).unwrap_or(0) + 1; - sel["r"] = json!(e("units")); - sel["b"] = json!(e("basedOn")); - sel["m"] = json!({"a": 0, "k": e("mode")}); - sel["sh"] = json!(e("shape")); - sel["rn"] = json!(adv.get("randomize").is_some_and(|p| p.value.as_bool()) as u8); - for (k, m) in [("a", "amount"), ("sm", "smoothness"), ("xe", "easeHigh"), ("ne", "easeLow")] { - if let Some(p) = adv.get(m) { - sel[k] = export_prop(ex, p, &anim::num, &format!("{label} ▸ {}", p.name)); - } - } + let e = |m: &str| i64::from(adv.get(m).map(|p| p.value.as_enum()).unwrap_or(0)) + 1; + sel.units = Some(e("units")); + sel.based_on = Some(e("basedOn")); + sel.extra.insert("m".into(), json!({"a": 0, "k": e("mode")})); + sel.shape = Some(e("shape")); + sel.randomize = Some(i64::from(adv.get("randomize").is_some_and(|p| p.value.as_bool()))); + let mut prop = |m: &str| adv.get(m).map(|p| export_prop(ex, p, &anim::num, &format!("{label} ▸ {}", p.name))); + (sel.amount, sel.smoothness, sel.ease_high, sel.ease_low) = (prop("amount"), prop("smoothness"), prop("easeHigh"), prop("easeLow")); } } } - json!({"nm": a.name, "s": sel, "a": Json::Object(props)}) + TextRange { name: Some(a.name.clone()), selector: Some(sel), style: Some(props), ..Default::default() } } /// Import Lottie text data into a text layer's `text` group. -pub(crate) fn import_text(im: &mut Im, ids: &mut Ids, t: &Json, tg: &mut PropGroup, fonts: &[(String, String, String)]) { - if let Some(list) = t.get("d").and_then(|d| d.get("k")).and_then(Json::as_array) +pub(crate) fn import_text(im: &mut Im, ids: &mut Ids, t: &TextData, tg: &mut PropGroup, fonts: &[(String, String, String)]) { + if let Some(list) = t.documents.as_ref().and_then(|d| d.keys.as_deref()) && let Some(st) = tg.get_mut("sourceText") { - let docs: Vec<(f64, TextDoc)> = - list.iter().filter_map(|k| Some((k.get("t").and_then(Json::as_f64).unwrap_or(0.0), doc_from(k.get("s")?, fonts)))).collect(); + let docs: Vec<(f64, TextDoc)> = list.iter().filter_map(|k| Some((k.time.unwrap_or(0.0), doc_from(k.document.as_ref()?, fonts)))).collect(); if let Some((_, first)) = docs.first() { st.value = Value::Text(Box::new(first.clone())); } if docs.len() > 1 { - st.keys = docs.iter().map(|(f, d)| effectcraft_keyframe::Keyframe::new(im.tb.tick(*f), Value::Text(Box::new(d.clone()))).hold()).collect(); + // Moved into the keys, not cloned: the documents are not kept twice. + st.keys = docs.into_iter().map(|(f, d)| effectcraft_keyframe::Keyframe::new(im.tb.tick(f), Value::Text(Box::new(d))).hold()).collect(); } } - if let Some(m) = t.get("m") + if let Some(m) = &t.alignment && let Some(more) = tg.sub_mut("moreOptions") { - if let (Some(g), Some(p)) = (m.get("g").and_then(Json::as_u64), more.get_mut("anchorGrouping")) { + let grouping = m.grouping.or_else(|| m.extra.get("g").and_then(Json::as_i64)).and_then(|g| u64::try_from(g).ok()); + if let (Some(g), Some(p)) = (grouping, more.get_mut("anchorGrouping")) { p.value = Value::Enum((g.max(1) - 1).min(3) as u32); } - if let (Some(a), Some(p)) = (m.get("a"), more.get_mut("groupingAlignment")) { - import_prop(im, a, p, &anim::to_vec2); + if let (Some(a), Some(p)) = (&m.anchor, more.get_mut("groupingAlignment")) { + import_prop(im, &a.0, p, &anim::to_vec2); } } - let Some(list) = t.get("a").and_then(Json::as_array) else { return }; + let Some(list) = &t.ranges else { return }; let mut animators = vec![]; for (i, a) in list.iter().enumerate() { - let name = a.get("nm").and_then(Json::as_str).map(str::to_string).unwrap_or(format!("Animator {}", i + 1)); - let props_j = a.get("a").cloned().unwrap_or(json!({})); + let name = a.name.clone().unwrap_or(format!("Animator {}", i + 1)); + let style = a.style.clone().unwrap_or_default(); let mut kinds: Vec<&str> = vec![]; for (key, kind, _) in ANIM_PROPS { - if props_j.get(*key).is_some() && !kinds.contains(kind) { + if style_get(&style, key).is_some() && !kinds.contains(kind) { kinds.push(kind); } } @@ -338,7 +410,7 @@ pub(crate) fn import_text(im: &mut Im, ids: &mut Ids, t: &Json, tg: &mut PropGro let mut g = build::text_animator(ids, &name, props); if let Some(pg) = g.sub_mut("properties") { for (key, _, m) in ANIM_PROPS { - let (Some(j), Some(p)) = (props_j.get(*key), pg.get_mut(m)) else { continue }; + let (Some(j), Some(p)) = (style_get(&style, key), pg.get_mut(m)) else { continue }; let conv: Box Option> = match &p.value { Value::Vec3(d) => { let z = d[2]; @@ -348,34 +420,38 @@ pub(crate) fn import_text(im: &mut Im, ids: &mut Ids, t: &Json, tg: &mut PropGro Value::Color(_) => Box::new(anim::to_color), _ => Box::new(anim::to_scalar), }; - import_prop(im, j, p, &*conv); + import_prop(im, &j, p, &*conv); } } - if let Some(s) = a.get("s") + if let Some(s) = &a.selector && let Some(sel) = g.sub_mut("selectors").and_then(|s| s.groups_mut_first()) { - for (k, m) in [("s", "start"), ("e", "end"), ("o", "offset")] { - if let (Some(j), Some(p)) = (s.get(k), sel.get_mut(m)) { - import_prop(im, j, p, &anim::to_scalar); + for (m, j) in [("start", &s.start), ("end", &s.end), ("offset", &s.offset)] { + if let (Some(j), Some(p)) = (j, sel.get_mut(m)) { + import_prop(im, &j.0, p, &anim::to_scalar); } } if let Some(adv) = sel.sub_mut("advanced") { - let mut en = |m: &str, key: &str, max: u32| { - let v = s.get(key).and_then(|v| if v.is_object() { v.get("k") } else { Some(v) }).and_then(anim::nums).and_then(|n| n.first().copied()); + let mut en = |m: &str, field: Option, key: &str, max: u32| { + let v = field.map(|f| f as f64).or_else(|| { + let v = s.extra.get(key)?; + let v = if v.is_object() { v.get("k")? } else { v }; + anim::nums(v).and_then(|n| n.first().copied()) + }); if let (Some(v), Some(p)) = (v, adv.get_mut(m)) { p.value = Value::Enum((((v.round() as i64).saturating_sub(1)).max(0) as u32).min(max)); } }; - en("units", "r", 1); - en("basedOn", "b", 3); - en("mode", "m", 5); - en("shape", "sh", 5); - if let (Some(rn), Some(p)) = (s.get("rn").and_then(Json::as_u64), adv.get_mut("randomize")) { + en("units", s.units, "r", 1); + en("basedOn", s.based_on, "b", 3); + en("mode", None, "m", 5); + en("shape", s.shape, "sh", 5); + if let (Some(rn), Some(p)) = (s.randomize.and_then(|r| u64::try_from(r).ok()), adv.get_mut("randomize")) { p.value = Value::Bool(rn != 0); } - for (k, m) in [("a", "amount"), ("sm", "smoothness"), ("xe", "easeHigh"), ("ne", "easeLow")] { - if let (Some(j), Some(p)) = (s.get(k), adv.get_mut(m)) { - import_prop(im, j, p, &anim::to_scalar); + for (m, j) in [("amount", &s.amount), ("smoothness", &s.smoothness), ("easeHigh", &s.ease_high), ("easeLow", &s.ease_low)] { + if let (Some(j), Some(p)) = (j, adv.get_mut(m)) { + import_prop(im, &j.0, p, &anim::to_scalar); } } } @@ -399,7 +475,7 @@ impl FirstGroup for PropGroup { /// Text converted to glyph shapes: one group with every glyph outline (at layer time 0's /// Source Text), a fill and a stroke, as Lottie shape items. -pub(crate) fn glyph_shapes(ex: &mut Ex, layer: &Layer) -> Vec { +pub(crate) fn glyph_shapes(ex: &mut Ex, layer: &Layer) -> Vec { let Some(tg) = layer.props.sub("text") else { return vec![] }; let Some(st) = tg.get("sourceText") else { return vec![] }; let Value::Text(doc) = st.value_at(Tick::ZERO) else { return vec![] }; @@ -411,14 +487,29 @@ pub(crate) fn glyph_shapes(ex: &mut Ex, layer: &Layer) -> Vec { for g in &lay.glyphs { let p = kurbo::Affine::translate((g.origin.x, g.origin.y)) * g.path.clone(); for sp in effectcraft_path::from_kurbo(&p) { - items.push(json!({"ty": "sh", "nm": g.ch.to_string(), "d": 1, "ks": {"a": 0, "k": shapes::shape_json(&sp)}})); + let path = Path { direction: Some(1), path: Some(fixed(shapes::shape_json(&sp))), closed: None }; + items.push(Shape { name: Some(g.ch.to_string()), kind: ShapeKind::Path(path), ..Default::default() }); } } - let col = |c: [f32; 4]| json!({"a": 0, "k": [c[0] as f64, c[1] as f64, c[2] as f64, 1.0]}); - let stroke = (doc.apply_stroke && doc.stroke_width > 0.0).then( - || json!({"ty": "st", "nm": "Stroke", "c": col(doc.stroke), "o": {"a": 0, "k": 100}, "w": {"a": 0, "k": doc.stroke_width}, "lc": 2, "lj": 2, "ml": 4}), - ); - let fill = doc.apply_fill.then(|| json!({"ty": "fl", "nm": "Fill", "c": col(doc.fill), "o": {"a": 0, "k": 100}, "r": 1})); + let col = |c: [f32; 4]| fixed(json!([c[0] as f64, c[1] as f64, c[2] as f64, 1.0])); + let stroke = (doc.apply_stroke && doc.stroke_width > 0.0).then(|| Shape { + name: Some("Stroke".into()), + kind: ShapeKind::Stroke(craft_lottie::model::Stroke { + color: Some(col(doc.stroke)), + opacity: Some(fixed(json!(100))), + width: Some(fixed(json!(doc.stroke_width))), + cap: Some(2), + join: Some(2), + miter_limit: Some(4.0), + ..Default::default() + }), + ..Default::default() + }); + let fill = doc.apply_fill.then(|| Shape { + name: Some("Fill".into()), + kind: ShapeKind::Fill(craft_lottie::model::Fill { color: Some(col(doc.fill)), opacity: Some(fixed(json!(100))), rule: Some(1) }), + ..Default::default() + }); // Items above draw on top: stroke over fill puts the stroke first. if doc.stroke_over_fill { items.extend(stroke); @@ -428,6 +519,15 @@ pub(crate) fn glyph_shapes(ex: &mut Ex, layer: &Layer) -> Vec { items.extend(stroke); } let n = items.len() + 1; - items.push(json!({"ty": "tr", "a": {"a": 0, "k": [0, 0]}, "p": {"a": 0, "k": [0, 0]}, "s": {"a": 0, "k": [100, 100]}, "r": {"a": 0, "k": 0}, "o": {"a": 0, "k": 100}})); - vec![json!({"ty": "gr", "nm": layer.name, "np": n, "it": items})] + let tr = craft_lottie::model::Transform { + anchor: Some(fixed(json!([0, 0]))), + position: Some(fixed(json!([0, 0]))), + scale: Some(fixed(json!([100, 100]))), + rotation: Some(fixed(json!(0))), + opacity: Some(fixed(json!(100))), + ..Default::default() + }; + items.push(Shape { kind: ShapeKind::Transform(Box::new(tr)), ..Default::default() }); + let group = craft_lottie::model::Group { count: Some(n as f64), items: Some(items) }; + vec![Shape { name: Some(layer.name.clone()), kind: ShapeKind::Group(group), ..Default::default() }] } diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml index 4947d928a..f744c684c 100644 --- a/crates/media/Cargo.toml +++ b/crates/media/Cargo.toml @@ -22,15 +22,17 @@ effectcraft-pdf = { workspace = true } effectcraft-model = { workspace = true } filmcraft-codecs = { workspace = true } filmcraft-media = { workspace = true } -filmcraft-aac = { workspace = true } -filmcraft-isobmff = { workspace = true } -filmcraft-matroska = { workspace = true } +craft-dpx = { workspace = true } +craft-exr = { workspace = true } +craft-gif = { workspace = true } +craft-webp = { workspace = true } +craft-audio = { workspace = true } +craft-media = { workspace = true } filmcraft-mpegts = { workspace = true } filmcraft-frame = { workspace = true } filmcraft-color = { workspace = true } filmcraft-time = { workspace = true } exr = { workspace = true } -gif = { workspace = true } image = { workspace = true, features = ["gif", "webp", "tiff", "bmp", "exr", "tga", "hdr"] } rayon = { workspace = true } serde_json = { workspace = true } @@ -38,5 +40,8 @@ thiserror = { workspace = true } log = { workspace = true } [dev-dependencies] +# Writes the animated GIFs of the tests. +gif = { workspace = true } +tempfile = { workspace = true } # (tests render comps that read layers through effects; already a dependency of effectcraft-render) effectcraft-effects = { workspace = true } diff --git a/crates/media/src/aac.rs b/crates/media/src/aac.rs index 625c37b43..a1793b7af 100644 --- a/crates/media/src/aac.rs +++ b/crates/media/src/aac.rs @@ -6,7 +6,7 @@ use std::io; -use filmcraft_aac::AudioSpecificConfig; +use craft_audio::aac::AudioSpecificConfig; use filmcraft_media::ByteReader; /// How far into an MPEG TS / PS file the audio streams are looked for: the stream is indexed as @@ -34,15 +34,16 @@ pub(crate) fn config(header: Option<&[u8]>, file: Option<&dyn ByteReader>) -> Op /// are in its fragments, not the `moov`). fn mp4(header: &[u8], file: Option<&dyn ByteReader>) -> Option> { let mp4 = match file { - Some(f) => filmcraft_isobmff::open(Head(f, f.len())), - None => filmcraft_isobmff::open(header), + Some(f) => craft_media::isobmff::open(Head(f, f.len())), + None => craft_media::isobmff::open(header), }; let mp4 = mp4.ok()?; - let playable = - |t: &&filmcraft_isobmff::Track| t.kind == filmcraft_isobmff::TrackKind::Audio && !t.samples.is_empty() && !t.entries.is_empty() && t.timescale > 0; + let playable = |t: &&craft_media::isobmff::Track| { + t.kind == craft_media::isobmff::TrackKind::Audio && !t.samples.is_empty() && !t.entries.is_empty() && t.timescale > 0 + }; let track = mp4.tracks.iter().find(playable)?; match &track.entries.first()?.codec { - filmcraft_isobmff::CodecConfig::Aac(a) => Some(a.asc.clone()), + craft_media::isobmff::CodecConfig::Aac(a) => Some(a.asc.clone()), _ => None, } } @@ -50,9 +51,9 @@ fn mp4(header: &[u8], file: Option<&dyn ByteReader>) -> Option> { /// The track FilmCraft's Matroska source plays: the first audio track with frames, found by /// indexing the clusters as that source does. fn matroska(file: &dyn ByteReader) -> Option> { - let mkv = filmcraft_matroska::open(Head(file, file.len())).ok()?; - match &mkv.tracks.iter().find(|t| t.kind == filmcraft_matroska::TrackKind::Audio && !t.samples.is_empty())?.codec { - filmcraft_matroska::Codec::Aac { asc } => Some(asc.clone()), + let mkv = craft_media::matroska::open(Head(file, file.len())).ok()?; + match &mkv.tracks.iter().find(|t| t.kind == craft_media::matroska::TrackKind::Audio && !t.samples.is_empty())?.codec { + craft_media::matroska::Codec::Aac { asc } => Some(asc.clone()), _ => None, } } @@ -88,16 +89,7 @@ impl Head<'_> { } } -impl filmcraft_isobmff::ByteSource for Head<'_> { - fn len(&self) -> u64 { - self.1 - } - fn read_at(&self, offset: u64, buf: &mut [u8]) -> io::Result<()> { - self.read(offset, buf) - } -} - -impl filmcraft_matroska::ByteSource for Head<'_> { +impl craft_media::ByteSource for Head<'_> { fn len(&self) -> u64 { self.1 } diff --git a/crates/media/src/convert.rs b/crates/media/src/convert.rs index a2b24b3ae..86fb088f9 100644 --- a/crates/media/src/convert.rs +++ b/crates/media/src/convert.rs @@ -7,8 +7,9 @@ //! are encoded in the file. Alpha follows the footage's [`AlphaMode`] interpretation. use effectcraft_project::{AlphaMode, Footage}; -use effectcraft_raster::{Image, Px}; +use effectcraft_raster::{Image, Px, quantize_px_u8}; use effectcraft_render::FloatCurve; +use effectcraft_render::disk_cache::Frame8; use filmcraft_color::{Matrix, Range}; use filmcraft_frame::{Chroma, PixelData, VideoFrame}; use rayon::prelude::*; @@ -80,14 +81,7 @@ pub(crate) fn frame_to_image_in(f: &VideoFrame, op: AlphaOp, mut buf: Vec) - return img; } match &f.data { - PixelData::Rgba8(d) => { - img.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| { - let src = &d[y * w * 4..(y + 1) * w * 4]; - for (o, s) in row.iter_mut().zip(src.as_chunks::<4>().0.iter()) { - *o = op.apply([U8[s[0] as usize], U8[s[1] as usize], U8[s[2] as usize]], U8[s[3] as usize]); - } - }); - } + PixelData::Rgba8(d) => convert_rgba8(&mut img.data, d, w, op, |p| p), PixelData::RgbaF32(d) => { // linear premultiplied → encoded premultiplied img.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| { @@ -104,7 +98,7 @@ pub(crate) fn frame_to_image_in(f: &VideoFrame, op: AlphaOp, mut buf: Vec) - let yuv = Yuv::new(f.color.matrix, f.color.range, 8); let amax = 255.0; convert_yuv( - &mut img, + &mut img.data, YuvSource { size: (w, h), chroma: *chroma, @@ -114,13 +108,14 @@ pub(crate) fn frame_to_image_in(f: &VideoFrame, op: AlphaOp, mut buf: Vec) - }, &yuv, op, + |p| p, ); } PixelData::Yuv16 { planes, chroma, bits, alpha } => { let yuv = Yuv::new(f.color.matrix, f.color.range, *bits); let amax = ((1u32 << *bits) - 1) as f32; convert_yuv( - &mut img, + &mut img.data, YuvSource { size: (w, h), chroma: *chroma, @@ -130,12 +125,52 @@ pub(crate) fn frame_to_image_in(f: &VideoFrame, op: AlphaOp, mut buf: Vec) - }, &yuv, op, + |p| p, ); } } img } +/// An 8-bit frame as [`frame_to_image_in`] converts it and an 8 bpc render then quantises it +/// ([`effectcraft_raster::quantize_px`]), kept as the 8-bit levels: expanded with +/// [`effectcraft_raster::expand_u8`] it is that quantised image to the bit, in a quarter of +/// the memory. `None` for frames with more than 8 bits (10/12-bit Y'CbCr, float). +pub(crate) fn frame_to_rgba8(f: &VideoFrame, op: AlphaOp) -> Option { + let (w, h) = (f.width as usize, f.height as usize); + let mut px = vec![[0u8; 4]; w * h]; + if w > 0 && h > 0 { + match &f.data { + PixelData::Rgba8(d) => convert_rgba8(&mut px, d, w, op, quantize_px_u8), + PixelData::Yuv8 { planes, chroma, alpha } => convert_yuv( + &mut px, + YuvSource { + size: (w, h), + chroma: *chroma, + planes: [&planes[0][..], &planes[1][..], &planes[2][..]], + alpha: alpha.as_deref().map(|a| &a[..]), + amax: 255.0, + }, + &Yuv::new(f.color.matrix, f.color.range, 8), + op, + quantize_px_u8, + ), + PixelData::RgbaF32(_) | PixelData::Yuv16 { .. } => return None, + } + } + Some(Frame8 { width: f.width, height: f.height, rgba: px.into_flattened() }) +} + +/// Straight or premultiplied 8-bit RGBA into `out` (one `O` per pixel, made by `store`). +fn convert_rgba8(out: &mut [O], d: &[u8], w: usize, op: AlphaOp, store: impl Fn(Px) -> O + Sync) { + out.par_chunks_mut(w).enumerate().for_each(|(y, row)| { + let Some(src) = d.get(y * w * 4..(y + 1) * w * 4) else { return }; + for (o, s) in row.iter_mut().zip(src.as_chunks::<4>().0.iter()) { + *o = store(op.apply([U8[s[0] as usize], U8[s[1] as usize], U8[s[2] as usize]], U8[s[3] as usize])); + } + }); +} + /// A movie frame turned the way its file says it is shown ([`crate::probe::video_turn`]: /// clockwise quarter turns; anything else leaves it as decoded). pub(crate) fn turned(img: Image, turn: image::metadata::Orientation) -> Image { @@ -311,13 +346,14 @@ struct YuvSource<'a, T> { amax: f32, } -fn convert_yuv(img: &mut Image, source: YuvSource<'_, T>, k: &Yuv, op: AlphaOp) { +/// Y'CbCr into `out` (one `O` per pixel, made by `store` from the converted pixel). +fn convert_yuv(out: &mut [O], source: YuvSource<'_, T>, k: &Yuv, op: AlphaOp, store: impl Fn(Px) -> O + Sync) { let YuvSource { size: (w, h), chroma, planes, alpha, amax } = source; let (sx, sy) = chroma.shifts(); let cw = w.div_ceil(1 << sx); let ch = h.div_ceil(1 << sy); let inv_amax = 1.0 / amax; - img.data.par_chunks_mut(w).enumerate().for_each_init( + out.par_chunks_mut(w).enumerate().for_each_init( || (vec![0f32; cw], vec![0f32; cw], vec![0f32; w], vec![0f32; w]), |(uc, vc, uf, vf), (y, row)| { // vertical chroma interpolation into normalised rows (Cb, Cr in −0.5..0.5) @@ -357,13 +393,13 @@ fn convert_yuv(img: &mut Image, source: YuvSource<'_, T>, k: &Yuv, op None => { for (x, o) in row.iter_mut().enumerate() { let c = rgb(x); - *o = [c[0], c[1], c[2], 1.0]; + *o = store([c[0], c[1], c[2], 1.0]); } } Some(al) => { let arow = &al[y * w..y * w + w]; for (x, o) in row.iter_mut().enumerate() { - *o = op.apply(rgb(x), (arow[x].f() * inv_amax).clamp(0.0, 1.0)); + *o = store(op.apply(rgb(x), (arow[x].f() * inv_amax).clamp(0.0, 1.0))); } } } @@ -552,6 +588,43 @@ mod tests { assert!((p[0] - 0.5).abs() < 1e-3 && (p[1] - 1.0).abs() < 1e-5 && p[2] > 1.5, "{p:?}"); } + /// The 8-bit levels of a frame expand to the float frame quantised to 8 bpc, bit for bit: + /// 4:2:0 and 4:4:4 Y'CbCr with and without alpha, and RGBA, under every alpha interpretation. + #[test] + fn rgba8_levels_are_the_quantised_float_frame() { + let (w, h) = (37usize, 18usize); + let noise = |n: usize, seed: usize| Arc::new((0..n).map(|i| ((i * 7919 + seed * 104_729) % 256) as u8).collect::>()); + let frame = |data| VideoFrame { + width: w as u32, + height: h as u32, + data, + color: filmcraft_color::ColorInfo::REC709, + par: (1, 1), + pts: filmcraft_time::Tick::ZERO, + }; + let yuv = |chroma: Chroma, alpha: bool| { + let n = w.div_ceil(1 << chroma.shifts().0) * h.div_ceil(1 << chroma.shifts().1); + frame(PixelData::Yuv8 { planes: [noise(w * h, 1), noise(n, 2), noise(n, 3)], chroma, alpha: alpha.then(|| noise(w * h, 4)) }) + }; + let frames = [yuv(Chroma::C420, false), yuv(Chroma::C420, true), yuv(Chroma::C444, true), frame(PixelData::Rgba8(noise(w * h * 4, 5)))]; + for f in &frames { + for mode in [AlphaMode::Straight, AlphaMode::Premultiplied, AlphaMode::Ignore] { + let op = AlphaOp::new(mode, [0.2, 0.5, 0.9]); + let q8 = frame_to_rgba8(f, op).expect("8-bit"); + let mut want = frame_to_image(f, op); + for p in &mut want.data { + effectcraft_raster::quantize_px(p, 255.0, 1.0 / 255.0); + } + assert_eq!((q8.width, q8.height), (want.width, want.height)); + for (got, want) in q8.rgba.as_chunks::<4>().0.iter().zip(&want.data) { + assert_eq!(effectcraft_raster::expand_u8(*got).map(f32::to_bits), want.map(f32::to_bits)); + } + } + } + let wide = frame(PixelData::RgbaF32(Arc::new(vec![0.5; w * h * 4]))); + assert!(frame_to_rgba8(&wide, AlphaOp::new(AlphaMode::Straight, [0.0; 3])).is_none()); + } + /// `cargo test -p effectcraft-media --lib bench -- --ignored --nocapture` #[test] #[ignore] diff --git a/crates/media/src/downmix.rs b/crates/media/src/downmix.rs index 09bb1e1f6..5e086bdaf 100644 --- a/crates/media/src/downmix.rs +++ b/crates/media/src/downmix.rs @@ -19,7 +19,7 @@ use std::f32::consts::FRAC_1_SQRT_2; use std::sync::Arc; -use filmcraft_aac::{AudioSpecificConfig, ProgramConfig}; +use craft_audio::aac::{AudioSpecificConfig, ProgramConfig}; /// A source channel's contribution to the left and right outputs, before normalisation. pub type Pair = [f32; 2]; @@ -185,7 +185,7 @@ mod tests { } } - /// AudioSpecificConfigs as ffmpeg 8.1 and FilmCraft's encoder write them (48 kHz AAC-LC). + /// AudioSpecificConfigs as ffmpeg 8.1 and craft-audio's encoder write them (48 kHz AAC-LC). const PCE_FFMPEG_61: &str = "118004c848002000c4400d4c61766336322e32382e31303256e500"; const PCE_FILMCRAFT_61: &str = "118004c845000108840000"; diff --git a/crates/media/src/dpx.rs b/crates/media/src/dpx.rs index a5b2abdaf..f08f1344d 100644 --- a/crates/media/src/dpx.rs +++ b/crates/media/src/dpx.rs @@ -1,17 +1,11 @@ -//! DPX (SMPTE 268M) and Kodak Cineon stills, the film-scan interchange formats, read from the -//! public specifications. +//! DPX (SMPTE 268M) and Kodak Cineon stills, the film-scan interchange formats, read with +//! `craft-dpx`; this module maps a frame onto footage and 16-bit pixels. //! -//! Supported: luma, RGB, RGBA and ABGR (DPX) or one / three channels (Cineon), 8-, 10-, 12- and -//! 16-bit unsigned samples, 10-bit filled into 32-bit words with the pad bits low (DPX method A, -//! Cineon "32-bit left justified") or high (method B, "right justified"), 12-bit in 16-bit words, -//! big- and little-endian files, the four unrotated image orientations and the pixel aspect ratio. //! Code values are shown as stored (a log scan looks flat, as in After Effects without a Cineon -//! conversion); the codec names the transfer (`log`, `linear`, …). Anything else (run-length -//! encoding, signed or float samples, Y'CbCr, packed 10/12-bit, transposed images) is refused -//! with an error naming it. Every field is read with bounds checks. +//! conversion); the codec names the transfer (`log`, `linear`, …). Y'CbCr elements and transposed +//! images are refused with an error naming them; so is everything `craft-dpx` does not read. use effectcraft_project::{AlphaMode, Footage}; -use rayon::prelude::*; use crate::{MediaError, Result}; @@ -21,391 +15,110 @@ pub(crate) const EXTENSIONS: &[&str] = &["dpx", "cin"]; /// Largest image accepted (8192 × 8192): bigger headers are refused before allocating. const MAX_PIXELS: u64 = 1 << 26; -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -enum Kind { - Dpx, - Cineon, +fn limits() -> craft_dpx::Limits { + let mut l = craft_dpx::Limits::default(); + l.max_pixels = MAX_PIXELS; + l } -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -enum Layout { - Luma, - Rgb, - Rgba, - Abgr, -} - -impl Layout { - fn components(self) -> usize { - match self { - Layout::Luma => 1, - Layout::Rgb => 3, - Layout::Rgba | Layout::Abgr => 4, - } - } - fn name(self) -> &'static str { - match self { - Layout::Luma => "luma", - Layout::Rgb => "RGB", - Layout::Rgba => "RGBA", - Layout::Abgr => "ABGR", - } +fn error(path: &str, e: craft_dpx::Error) -> MediaError { + match e { + craft_dpx::Error::Unsupported(_) => MediaError::Unsupported(format!("{path}: {e}")), + _ => MediaError::Decode(format!("{path}: {e}")), } } -/// How samples are stored in a line. -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -enum Packing { - /// One byte per sample. - Bytes8, - /// One 16-bit word per sample. - Words16, - /// Three 10-bit samples per 32-bit word, first in the high bits, two pad bits low. - Filled10A, - /// Three 10-bit samples per 32-bit word, two pad bits high. - Filled10B, - /// One 12-bit sample per 16-bit word, four pad bits low. - Filled12A, - /// One 12-bit sample per 16-bit word, four pad bits high. - Filled12B, +/// The header of a DPX or Cineon file this module shows (`None` for other formats). +fn info(path: &str, bytes: &[u8]) -> Result> { + if craft_dpx::sniff(bytes).is_none() { + return Ok(None); + } + let info = craft_dpx::probe(bytes, &limits()).map_err(|e| error(path, e))?; + if info.layout.is_ycbcr() { + return Err(MediaError::Unsupported(format!("{path}: DPX with Y'CbCr samples is not supported"))); + } + if info.flips().is_none() { + return Err(MediaError::Unsupported(format!("{path}: DPX orientation {} (transposed image) is not supported", info.orientation))); + } + Ok(Some(info)) } -impl Packing { - fn max(self) -> u32 { - match self { - Packing::Bytes8 => 0xff, - Packing::Words16 => 0xffff, - Packing::Filled10A | Packing::Filled10B => 0x3ff, - Packing::Filled12A | Packing::Filled12B => 0xfff, - } - } - fn bits(self) -> u32 { - match self { - Packing::Bytes8 => 8, - Packing::Words16 => 16, - Packing::Filled10A | Packing::Filled10B => 10, - Packing::Filled12A | Packing::Filled12B => 12, - } - } - /// Bytes holding `n` samples, padded to a 32-bit word (lines start on word boundaries). - fn line_bytes(self, n: u64) -> u64 { - match self { - Packing::Bytes8 => n.div_ceil(4) * 4, - Packing::Words16 | Packing::Filled12A | Packing::Filled12B => (n * 2).div_ceil(4) * 4, - Packing::Filled10A | Packing::Filled10B => n.div_ceil(3) * 4, - } +fn layout_name(info: &craft_dpx::Info) -> &'static str { + match info.layout { + craft_dpx::Layout::Luma => "luma", + craft_dpx::Layout::Rgb => "RGB", + _ if info.descriptor == 52 => "ABGR", + _ => "RGBA", } } -/// What a header says about the picture. -#[derive(Clone, Debug)] -struct Header { - kind: Kind, - big_endian: bool, - width: u32, - height: u32, - layout: Layout, - packing: Packing, - /// Byte offset of the first line. - offset: u64, - /// Bytes from one line's start to the next. - stride: u64, - flip_x: bool, - flip_y: bool, - pixel_aspect: f64, - /// Transfer characteristic for the codec name (`log`, `linear`, …), empty when unstated. - transfer: &'static str, -} - -/// Bounds-checked reads in the file's byte order. -struct Reader<'a> { - b: &'a [u8], - big: bool, -} - -impl Reader<'_> { - fn u8(&self, o: usize) -> Option { - self.b.get(o).copied() +/// The transfer characteristic for the codec name, empty when unstated. +fn transfer(info: &craft_dpx::Info) -> &'static str { + if info.kind == craft_dpx::Kind::Cineon { + return "log"; } - fn u16(&self, o: usize) -> Option { - let s: [u8; 2] = self.b.get(o..o.checked_add(2)?)?.try_into().ok()?; - Some(if self.big { u16::from_be_bytes(s) } else { u16::from_le_bytes(s) }) - } - fn u32(&self, o: usize) -> Option { - let s: [u8; 4] = self.b.get(o..o.checked_add(4)?)?.try_into().ok()?; - Some(if self.big { u32::from_be_bytes(s) } else { u32::from_le_bytes(s) }) - } -} - -const UNDEFINED32: u32 = u32::MAX; - -/// DPX or Cineon, and the byte order, from the magic number. -fn sniff(bytes: &[u8]) -> Option<(Kind, bool)> { - match bytes.get(..4)? { - b"SDPX" => Some((Kind::Dpx, true)), - b"XPDS" => Some((Kind::Dpx, false)), - [0x80, 0x2a, 0x5f, 0xd7] => Some((Kind::Cineon, true)), - [0xd7, 0x5f, 0x2a, 0x80] => Some((Kind::Cineon, false)), - _ => None, - } -} - -fn unsupported(path: &str, what: impl std::fmt::Display) -> MediaError { - MediaError::Unsupported(format!("{path}: {what}")) -} - -fn truncated(path: &str, kind: Kind) -> MediaError { - let name = if kind == Kind::Dpx { "DPX" } else { "Cineon" }; - MediaError::Decode(format!("{path}: truncated {name} header")) -} - -fn parse(path: &str, bytes: &[u8]) -> Result> { - let Some((kind, big_endian)) = sniff(bytes) else { return Ok(None) }; - let r = Reader { b: bytes, big: big_endian }; - let h = match kind { - Kind::Dpx => parse_dpx(path, &r)?, - Kind::Cineon => parse_cineon(path, &r)?, - }; - let pixels = u64::from(h.width) * u64::from(h.height); - if h.width == 0 || h.height == 0 || pixels > MAX_PIXELS { - return Err(MediaError::Decode(format!("{path}: image size {} × {} out of range", h.width, h.height))); - } - let need = h.offset.saturating_add(h.stride.saturating_mul(u64::from(h.height))); - if (bytes.len() as u64) < need { - return Err(MediaError::Decode(format!("{path}: truncated image data ({} of {need} bytes)", bytes.len()))); - } - Ok(Some(h)) -} - -fn parse_dpx(path: &str, r: &Reader) -> Result
{ - let t = || truncated(path, Kind::Dpx); - let image_offset = r.u32(4).ok_or_else(t)?; - let orientation = r.u16(768).ok_or_else(t)?; - let elements = r.u16(770).ok_or_else(t)?; - let width = r.u32(772).ok_or_else(t)?; - let height = r.u32(776).ok_or_else(t)?; - // The first image element (the picture; further elements are alternative views). - let sign = r.u32(780).ok_or_else(t)?; - let descriptor = r.u8(800).ok_or_else(t)?; - let transfer = r.u8(801).ok_or_else(t)?; - let bits = r.u8(803).ok_or_else(t)?; - let packing = r.u16(804).ok_or_else(t)?; - let encoding = r.u16(806).ok_or_else(t)?; - let data_offset = r.u32(808).ok_or_else(t)?; - let eol_padding = r.u32(812).ok_or_else(t)?; - if elements == 0 || elements == u16::MAX { - return Err(unsupported(path, "DPX file without an image element")); - } - if sign == 1 { - return Err(unsupported(path, "DPX with signed samples is not supported")); - } - if encoding == 1 { - return Err(unsupported(path, "run-length encoded DPX is not supported")); - } - let layout = match descriptor { - 6 => Layout::Luma, - 50 => Layout::Rgb, - 51 => Layout::Rgba, - 52 => Layout::Abgr, - 100..=103 => return Err(unsupported(path, "DPX with Y'CbCr samples is not supported")), - d => return Err(unsupported(path, format!("DPX image element descriptor {d} is not supported (luma, RGB, RGBA and ABGR are)"))), - }; - let packing = match (bits, packing) { - (8, _) => Packing::Bytes8, - (16, _) => Packing::Words16, - (10, 1) => Packing::Filled10A, - (10, 2) => Packing::Filled10B, - (12, 1) => Packing::Filled12A, - (12, 2) => Packing::Filled12B, - (10 | 12, _) => return Err(unsupported(path, format!("DPX with packed {bits}-bit samples (packing {packing}) is not supported"))), - (b, _) => return Err(unsupported(path, format!("DPX with {b}-bit samples is not supported (8, 10, 12 and 16 are)"))), - }; - let (flip_x, flip_y) = match orientation { - 0 | u16::MAX => (false, false), - 1 => (true, false), - 2 => (false, true), - 3 => (true, true), - o => return Err(unsupported(path, format!("DPX orientation {o} (transposed image) is not supported"))), - }; - let offset = if data_offset == 0 || data_offset == UNDEFINED32 { image_offset } else { data_offset }; - let eol = if eol_padding == UNDEFINED32 { 0 } else { eol_padding }; - let samples = u64::from(width) * layout.components() as u64; - // Pixel aspect ratio (orientation header), when stated. - let pixel_aspect = match (r.u32(1628), r.u32(1632)) { - (Some(h), Some(v)) if h != 0 && v != 0 && h != UNDEFINED32 && v != UNDEFINED32 => { - let a = f64::from(h) / f64::from(v); - if (0.1..=10.0).contains(&a) { a } else { 1.0 } - } - _ => 1.0, - }; - let transfer = match transfer { - 1 => "log (printing density)", - 2 => "linear", - 3 => "log", - 5 | 6 => "Rec. 709", - 13 => "ADX", + match info.transfer { + Some(1) => "log (printing density)", + Some(2) => "linear", + Some(3) => "log", + Some(5 | 6) => "Rec. 709", + Some(13) => "ADX", _ => "", - }; - Ok(Header { - kind: Kind::Dpx, - big_endian: r.big, - width, - height, - layout, - packing, - offset: u64::from(offset), - stride: packing.line_bytes(samples) + u64::from(eol), - flip_x, - flip_y, - pixel_aspect, - transfer, - }) -} - -fn parse_cineon(path: &str, r: &Reader) -> Result
{ - let t = || truncated(path, Kind::Cineon); - let offset = r.u32(4).ok_or_else(t)?; - let orientation = r.u8(192).ok_or_else(t)?; - let channels = r.u8(193).ok_or_else(t)?; - // Channel 0's description; every channel must match it. - let bits = r.u8(198).ok_or_else(t)?; - let width = r.u32(200).ok_or_else(t)?; - let height = r.u32(204).ok_or_else(t)?; - let interleave = r.u8(680).ok_or_else(t)?; - let packing = r.u8(681).ok_or_else(t)?; - let signed = r.u8(682).ok_or_else(t)?; - let eol_padding = r.u32(684).ok_or_else(t)?; - let layout = match channels { - 1 => Layout::Luma, - 3 => Layout::Rgb, - c => return Err(unsupported(path, format!("Cineon with {c} channels is not supported (1 and 3 are)"))), - }; - for c in 1..usize::from(channels) { - let base = 196 + 28 * c; - if r.u8(base + 2) != Some(bits) || r.u32(base + 4) != Some(width) || r.u32(base + 8) != Some(height) { - return Err(unsupported(path, "Cineon channels of different sizes or depths are not supported")); - } } - if interleave != 0 { - return Err(unsupported(path, "Cineon with line or channel interleave is not supported")); - } - if signed == 1 { - return Err(unsupported(path, "Cineon with signed samples is not supported")); - } - let packing = match (bits, packing) { - (8, _) => Packing::Bytes8, - (16, _) => Packing::Words16, - (10, 5) => Packing::Filled10A, - (10, 6) => Packing::Filled10B, - (10, p) => return Err(unsupported(path, format!("Cineon with 10-bit packing {p} is not supported (32-bit words are)"))), - (b, _) => return Err(unsupported(path, format!("Cineon with {b}-bit samples is not supported (8, 10 and 16 are)"))), - }; - let (flip_x, flip_y) = match orientation { - 0 | u8::MAX => (false, false), - 1 => (false, true), - 2 => (true, false), - 3 => (true, true), - o => return Err(unsupported(path, format!("Cineon orientation {o} (transposed image) is not supported"))), - }; - let eol = if eol_padding == UNDEFINED32 { 0 } else { eol_padding }; - let samples = u64::from(width) * layout.components() as u64; - Ok(Header { - kind: Kind::Cineon, - big_endian: r.big, - width, - height, - layout, - packing, - offset: u64::from(offset), - stride: packing.line_bytes(samples) + u64::from(eol), - flip_x, - flip_y, - pixel_aspect: 1.0, - transfer: "log", - }) } /// Footage for a DPX or Cineon file (`None` for other formats). pub(crate) fn probe(path: &str, bytes: &[u8]) -> Result> { - let Some(h) = parse(path, bytes)? else { return Ok(None) }; - let alpha = matches!(h.layout, Layout::Rgba | Layout::Abgr); + let Some(h) = info(path, bytes)? else { return Ok(None) }; + let alpha = h.layout.has_alpha(); let mut f = crate::probe::still_footage(path, h.width, h.height, image::ImageFormat::Png, alpha); f.alpha = if alpha { AlphaMode::Straight } else { AlphaMode::Ignore }; - f.pixel_aspect = h.pixel_aspect; - let name = if h.kind == Kind::Dpx { "DPX" } else { "Cineon" }; - f.codec = format!("{name} {}-bit {}", h.packing.bits(), h.layout.name()); - if !h.transfer.is_empty() { - f.codec = format!("{}, {}", f.codec, h.transfer); + f.pixel_aspect = + h.pixel_aspect.map(|(x, y)| f64::from(x) / f64::from(y)).filter(|a| (0.1..=10.0).contains(a) && h.kind == craft_dpx::Kind::Dpx).unwrap_or(1.0); + let name = if h.kind == craft_dpx::Kind::Dpx { "DPX" } else { "Cineon" }; + f.codec = format!("{name} {}-bit {}", h.bits, layout_name(&h)); + let t = transfer(&h); + if !t.is_empty() { + f.codec = format!("{}, {t}", f.codec); } Ok(Some(f)) } -/// The picture of a DPX or Cineon file as 16-bit samples (`None` for other formats). +/// The picture of a DPX or Cineon file as 16-bit samples, upright (`None` for other formats). pub(crate) fn decode(path: &str, bytes: &[u8]) -> Result> { - let Some(h) = parse(path, bytes)? else { return Ok(None) }; - let (w, ht) = (h.width as usize, h.height as usize); - // Output components: luma, RGB or RGBA (ABGR reordered). - let nc = h.layout.components(); - let row_len = w * nc; - let mut out = vec![0u16; row_len * ht]; - let max = h.packing.max(); - out.par_chunks_mut(row_len).enumerate().for_each(|(y, row)| { - let sy = if h.flip_y { ht - 1 - y } else { y }; - let start = h.offset.saturating_add(h.stride.saturating_mul(sy as u64)); - let line = usize::try_from(start).ok().and_then(|s| bytes.get(s..)).unwrap_or(&[]); - for x in 0..w { - let sx = if h.flip_x { w - 1 - x } else { x }; - for c in 0..nc { - // ABGR stores alpha first and colour reversed. - let sc = if h.layout == Layout::Abgr { 3 - c } else { c }; - let v = sample(line, h.packing, h.big_endian, sx * nc + sc); - if let Some(o) = row.get_mut(x * nc + c) { - *o = ((u64::from(v.min(max)) * 0xffff + u64::from(max) / 2) / u64::from(max)) as u16; + let Some(h) = info(path, bytes)? else { return Ok(None) }; + let (flip_x, flip_y) = h.flips().unwrap_or_default(); + let frame = craft_dpx::decode(bytes, &limits()).map_err(|e| error(path, e))?; + let (w, ht, nc) = (h.width as usize, h.height as usize, h.layout.components()); + let row_len = w.checked_mul(nc).ok_or_else(|| MediaError::Decode(format!("{path}: image dimensions overflow")))?; + let mut out = frame.codes; + if flip_y { + for y in 0..ht / 2 { + for x in 0..row_len { + out.swap(y * row_len + x, (ht - 1 - y) * row_len + x); + } + } + } + if flip_x { + for row in out.chunks_exact_mut(row_len) { + for x in 0..w / 2 { + let (a, b) = (x * nc, (w - 1 - x) * nc); + for c in 0..nc { + row.swap(a + c, b + c); } } } - }); + } + out.iter_mut().for_each(|c| *c = craft_dpx::widen(*c, h.bits)); let (w32, h32) = (h.width, h.height); let img = match h.layout { - Layout::Luma => image::ImageBuffer::from_raw(w32, h32, out).map(image::DynamicImage::ImageLuma16), - Layout::Rgb => image::ImageBuffer::from_raw(w32, h32, out).map(image::DynamicImage::ImageRgb16), - Layout::Rgba | Layout::Abgr => image::ImageBuffer::from_raw(w32, h32, out).map(image::DynamicImage::ImageRgba16), + craft_dpx::Layout::Luma => image::ImageBuffer::from_raw(w32, h32, out).map(image::DynamicImage::ImageLuma16), + craft_dpx::Layout::Rgb => image::ImageBuffer::from_raw(w32, h32, out).map(image::DynamicImage::ImageRgb16), + _ => image::ImageBuffer::from_raw(w32, h32, out).map(image::DynamicImage::ImageRgba16), }; img.map(Some).ok_or_else(|| MediaError::Decode(format!("{path}: image buffer size mismatch"))) } -/// Sample `i` of a line (0 past its end; the header check makes that unreachable for valid -/// headers). -#[inline] -fn sample(line: &[u8], packing: Packing, big: bool, i: usize) -> u32 { - let word16 = |o: usize| -> u32 { - match line.get(o..o + 2) { - Some(&[a, b]) => u32::from(if big { u16::from_be_bytes([a, b]) } else { u16::from_le_bytes([a, b]) }), - _ => 0, - } - }; - let word32 = |o: usize| -> u32 { - match line.get(o..o + 4) { - Some(&[a, b, c, d]) => { - if big { - u32::from_be_bytes([a, b, c, d]) - } else { - u32::from_le_bytes([a, b, c, d]) - } - } - _ => 0, - } - }; - match packing { - Packing::Bytes8 => line.get(i).copied().map_or(0, u32::from), - Packing::Words16 => word16(i * 2), - Packing::Filled12A => word16(i * 2) >> 4, - Packing::Filled12B => word16(i * 2) & 0xfff, - Packing::Filled10A => (word32(i / 3 * 4) >> (22 - 10 * (i % 3) as u32)) & 0x3ff, - Packing::Filled10B => (word32(i / 3 * 4) >> (20 - 10 * (i % 3) as u32)) & 0x3ff, - } -} - #[cfg(test)] mod tests { use super::*; @@ -452,7 +165,7 @@ mod tests { #[test] fn hostile_headers_never_panic() { - assert!(parse("x", b"not a dpx").expect("other").is_none()); + assert!(info("x", b"not a dpx").expect("other").is_none()); let good = { let mut a = dpx_header(4, 4, 50, 10, 1, 0); a.extend(std::iter::repeat_n(0x5a, 64)); @@ -462,14 +175,18 @@ mod tests { for n in [4, 8, 600, 790, 805, 2047, 2048, 2100] { assert!(decode("t.dpx", good.get(..n).expect("cut")).is_err(), "{n}"); } - // huge, zero, unsupported + // huge, zero, Y'CbCr, unsupported depth, transposed for (w, h, d, bits, p, o) in - [(u32::MAX, u32::MAX, 50, 10, 1, 0), (0, 4, 50, 10, 1, 0), (4, 4, 102, 10, 1, 0), (4, 4, 50, 10, 0, 0), (4, 4, 50, 32, 0, 0), (4, 4, 50, 10, 1, 5)] + [(u32::MAX, u32::MAX, 50, 10, 1, 0), (0, 4, 50, 10, 1, 0), (4, 4, 102, 10, 1, 0), (4, 4, 50, 32, 0, 0), (4, 4, 50, 10, 1, 5)] { let mut b = dpx_header(w, h, d, bits, p, o); b.extend(std::iter::repeat_n(0, 64)); - assert!(probe("h.dpx", &b).is_err() && decode("h.dpx", &b).is_err()); + assert!(probe("h.dpx", &b).is_err() && decode("h.dpx", &b).is_err(), "{w}x{h} {d} {bits} {p} {o}"); } + // 8192 × 8193 is past this module's own cap, which is below craft-dpx's default. + let mut big = dpx_header(8192, 8193, 6, 8, 0, 0); + big.extend(std::iter::repeat_n(0, 64)); + assert!(probe("big.dpx", &big).is_err()); // run-length encoding and offsets past the end are refused let mut rle = good.clone(); rle[806..808].copy_from_slice(&1u16.to_be_bytes()); diff --git a/crates/media/src/exr_channels.rs b/crates/media/src/exr_channels.rs index d0acf590f..96b0173cd 100644 --- a/crates/media/src/exr_channels.rs +++ b/crates/media/src/exr_channels.rs @@ -31,35 +31,15 @@ use crate::convert::{AlphaOp, float_to_image}; /// stop at the same size). const MAX_DISPLAY_PIXELS: usize = 1 << 28; -fn is_depth(name: &str) -> bool { - let last = name.rsplit('.').next().unwrap_or(name); - last.eq_ignore_ascii_case("z") || name.to_ascii_lowercase().contains("depth") -} - -/// Full plane name of a channel in a part: `part.channel`, except that Blender 5 writes one part -/// per pass whose channels already carry the full name (part `ViewLayer.Depth` holds -/// `ViewLayer.Depth.Z`), which is kept as is instead of doubling the prefix. -fn channel_name(part: Option<&str>, channel: &str) -> String { - match part { - Some(p) if channel.strip_prefix(p).is_some_and(|rest| rest.starts_with('.')) => channel.to_string(), - Some(p) => format!("{p}.{channel}"), - None => channel.to_string(), - } -} +// Channel naming and the Cryptomatte manifest are `craft-exr`'s. +use craft_exr::channels::{is_depth, part_channel_name as channel_name}; +pub use craft_exr::cryptomatte::parse_manifest; /// The part name channels are prefixed with (`None`: unnamed). fn part_name(header: &Header) -> Option { header.own_attributes.layer_name.as_ref().map(|t| t.to_string()).filter(|s| !s.is_empty()) } -/// Parse a Cryptomatte manifest (`{"name": "hexhash", …}`). -pub fn parse_manifest(json: &str) -> Vec<(String, u32)> { - let Ok(serde_json::Value::Object(m)) = serde_json::from_str::(json) else { return vec![] }; - let mut v: Vec<(String, u32)> = m.iter().filter_map(|(k, h)| Some((k.clone(), u32::from_str_radix(h.as_str()?.trim(), 16).ok()?))).collect(); - v.sort(); - v -} - /// Why an OpenEXR file's pixels can't be decoded when its headers name a compression this build /// can't decompress (HTJ2K, OpenEXR 3.4's High-Throughput JPEG 2000): the decoders only said /// "no non-deep rgb channels" or showed a transparent frame (#482). `None` for other files. diff --git a/crates/media/src/gif_anim.rs b/crates/media/src/gif_anim.rs index 09c0cda34..8453b50ac 100644 --- a/crates/media/src/gif_anim.rs +++ b/crates/media/src/gif_anim.rs @@ -43,18 +43,11 @@ pub(crate) fn gcd(a: u32, b: u32) -> u32 { } /// Describe an animated GIF as a movie. `None` for a GIF with fewer than two frames (a still) or -/// one whose headers can't be read. +/// one whose headers can't be read. A damaged tail keeps the frames before it. pub(crate) fn probe(path: &str, bytes: &[u8]) -> Option { - let mut dec = gif::DecodeOptions::new().read_info(Cursor::new(bytes)).ok()?; - let (width, height) = (u32::from(dec.width()), u32::from(dec.height())); - let mut delays: Vec = Vec::new(); - while delays.len() < MAX_FRAMES { - match dec.next_frame_info() { - Ok(Some(f)) => delays.push(delay_cs(u32::from(f.delay))), - // The end, or a damaged tail: keep the frames before it. - _ => break, - } - } + let info = craft_gif::scan(bytes, MAX_FRAMES)?; + let (width, height) = (u32::from(info.width), u32::from(info.height)); + let delays: Vec = info.frames.iter().map(|f| delay_cs(u32::from(f.delay_cs))).collect(); if delays.len() < 2 || width == 0 || height == 0 { return None; } @@ -93,7 +86,18 @@ impl Anim { if crate::webp_anim::is_webp(bytes) { return crate::webp_anim::load(bytes); } - let dec = GifDecoder::new(Cursor::new(bytes)).map_err(|e| MediaError::Decode(format!("GIF: {e}")))?; + let mut dec = GifDecoder::new(Cursor::new(bytes)).map_err(|e| MediaError::Decode(format!("GIF: {e}")))?; + // A frame may be far larger than the canvas, and the decoder allocates its buffer before + // decoding it: no single buffer may pass what all the frames may hold. + let mut limits = image::Limits::no_limits(); + limits.max_alloc = Some(MAX_BYTES as u64); + image::ImageDecoder::set_limits(&mut dec, limits).map_err(|e| MediaError::Decode(format!("GIF: {e}")))?; + // The decoder allocates the full logical canvas for the first frame, however small the + // frames themselves are: check it before decoding any. + let (w, h) = image::ImageDecoder::dimensions(&dec); + if u64::from(w) * u64::from(h) * 4 > MAX_BYTES as u64 { + return Err(MediaError::Unsupported(format!("GIF canvas of {w}×{h} is too large to hold in memory"))); + } let (mut frames, mut starts) = (Vec::new(), Vec::new()); let (mut at, mut size) = (0i64, 0usize); for frame in dec.into_frames() { @@ -179,6 +183,17 @@ mod tests { assert_eq!(colour_at(&pool, &f, 5.0), [0.0, 0.0, 1.0]); } + #[test] + fn a_frame_far_larger_than_the_canvas_is_refused_before_it_is_allocated() { + // A 1×1 canvas and a 20000×20000 frame (1.6 GB of RGBA); the data itself is not there. + let mut bytes = b"GIF89a".to_vec(); + // (a two-entry global colour table, so the frame gets as far as its buffer) + bytes.extend([1, 0, 1, 0, 0x80, 0, 0, 0, 0, 0, 255, 255, 255]); + bytes.extend([0x2c, 0, 0, 0, 0, 0x20, 0x4e, 0x20, 0x4e, 0, 2, 0]); + let e = crate::gif_anim::Anim::load(&bytes).err().expect("refused").to_string(); + assert!(e.to_lowercase().contains("memory"), "{e}"); + } + #[test] fn a_zero_delay_is_ten_hundredths_like_in_browsers() { let f = probe_bytes("a.gif", gif(&[(RED, 0), (GREEN, 1)]).into()).unwrap(); @@ -212,4 +227,18 @@ mod tests { assert!(pool.frame_at(&f, Tick::ZERO).is_err()); let _ = probe_bytes("a.gif", bytes.into()); } + + #[test] + fn a_huge_canvas_is_refused_before_decoding() { + // A 65535×65535 logical screen (16 GiB of RGBA) holding one 1×1 frame. + let mut out = Vec::new(); + { + let mut enc = gif::Encoder::new(&mut out, u16::MAX, u16::MAX, &[]).unwrap(); + for _ in 0..2 { + enc.write_frame(&gif::Frame::from_rgb(1, 1, &RED)).unwrap(); + } + } + let e = super::Anim::load(&out).err().unwrap(); + assert!(e.to_string().contains("too large"), "{e}"); + } } diff --git a/crates/media/src/layered.rs b/crates/media/src/layered.rs index b259c8b7f..adc23cf31 100644 --- a/crates/media/src/layered.rs +++ b/crates/media/src/layered.rs @@ -14,19 +14,20 @@ fn is_svg(path: &str, bytes: &[u8]) -> bool { } /// The vector document of an SVG / PDF / AI / EPS file (`None` for other formats): page -/// `page` of a PDF, restricted to the footage's layer when it names one. +/// `page` of a PDF framed as `framing` says, restricted to the footage's layer when it names one. pub fn vector_doc( path: &str, bytes: &[u8], layer: Option<&effectcraft_project::SourceLayer>, page: u32, + framing: effectcraft_pdf::Framing, ) -> Option> { if is_svg(path, bytes) { return Some(effectcraft_svg::parse(bytes).map_err(|e| format!("{path}: {e}"))); } effectcraft_pdf::sniff(bytes)?; Some( - effectcraft_pdf::parse_page(bytes, page as usize) + effectcraft_pdf::parse_page_framed(bytes, page as usize, framing) .map(|d| match layer { Some(l) => effectcraft_pdf::layer_doc_with_sizes(&d, l.index as usize, l.layer_size).0, None => d, @@ -111,14 +112,14 @@ pub(crate) fn decode(path: &str, bytes: &[u8], footage: &Footage, op: AlphaOp) - return Ok(Some(straight_to_image(px.width, px.height, &px.data, op))); } if is_svg(path, bytes) || is_pdf_like(path, bytes) { - let doc = vector_doc(path, bytes, footage.layer.as_ref(), footage.page).unwrap_or(Err(String::new())).map_err(MediaError::Decode)?; - let (w, h) = doc.pixel_size(); + let doc = footage_doc(path, bytes, footage).unwrap_or(Err(String::new())).map_err(MediaError::Decode)?; + let ((w, h), to_image) = fit(&doc, footage, 1.0); // A declared size up to 30000² would be a 14 GB allocation that aborts the app (#2136): // past what Continuously Rasterize can draw, the footage reports an error instead. if u64::from(w) * u64::from(h) > u64::from(MAX_VECTOR_SIDE) * u64::from(MAX_VECTOR_SIDE) { return Err(MediaError::Decode(format!("{path}: {w}×{h} pixels is larger than vector footage can be drawn ({MAX_VECTOR_SIDE}×{MAX_VECTOR_SIDE})"))); } - let img = effectcraft_svg::rasterize(&doc, w, h, 1.0); + let img = effectcraft_svg::try_rasterize_with(&doc, w, h, to_image).map_err(|e| MediaError::Decode(format!("{path}: {e}")))?; // The rasteriser produces premultiplied pixels already. let _ = op; return Ok(Some(img)); @@ -129,12 +130,34 @@ pub(crate) fn decode(path: &str, bytes: &[u8], footage: &Footage, op: AlphaOp) - /// The largest side vector footage is drawn at, decoded or continuously rasterised. const MAX_VECTOR_SIDE: u32 = 16384; -/// A vector file rasterised at `scale` × its pixel size (Continuously Rasterize). -pub(crate) fn rasterize_vector(path: &str, bytes: &[u8], layer: Option<&effectcraft_project::SourceLayer>, page: u32, scale: f64) -> Option { - let doc = vector_doc(path, bytes, layer, page)?.ok()?; - let (w, h) = doc.pixel_size(); - let (sw, sh) = (((w as f64 * scale).ceil() as u32).clamp(1, MAX_VECTOR_SIDE), ((h as f64 * scale).ceil() as u32).clamp(1, MAX_VECTOR_SIDE)); - Some(effectcraft_svg::rasterize(&doc, sw, sh, scale)) +/// The vector footage `footage` (of the file `path` with these `bytes`) rasterised at `scale` × +/// its size as [`effectcraft_render::vector_fit`] draws it (Continuously Rasterize). +/// +/// `None` when the file cannot be read as a vector document. `Some(Err(_))` when a render budget +/// refused part of the drawing (for example a layer scaled past the surface limit), so the caller +/// can report it instead of showing a partial or empty image as if it were complete. +pub(crate) fn rasterize_vector(path: &str, bytes: &[u8], footage: &Footage, scale: f64) -> Option> { + let doc = footage_doc(path, bytes, footage)?.ok()?; + let ((sw, sh), to_image) = fit(&doc, footage, scale); + let drawn = effectcraft_svg::try_rasterize_with(&doc, sw.clamp(1, MAX_VECTOR_SIDE), sh.clamp(1, MAX_VECTOR_SIDE), to_image); + Some(drawn.map_err(|e| MediaError::Decode(format!("{path}: {e}")))) +} + +/// The page framing of vector footage: as After Effects places it for footage from an After +/// Effects project ([`Footage::placed_page`]), as a PDF viewer shows it otherwise. +fn framing(footage: &Footage) -> effectcraft_pdf::Framing { + if footage.placed_page { effectcraft_pdf::Framing::Placed } else { effectcraft_pdf::Framing::Viewer } +} + +/// [`vector_doc`] of `footage`'s page, layer and framing. +fn footage_doc(path: &str, bytes: &[u8], footage: &Footage) -> Option> { + vector_doc(path, bytes, footage.layer.as_ref(), footage.page, framing(footage)) +} + +/// The image size and document → image mapping of `doc` drawn for `footage` at `scale`. +fn fit(doc: &effectcraft_svg::Doc, footage: &Footage, scale: f64) -> ((u32, u32), effectcraft_svg::Affine) { + let (size, [sx, sy]) = effectcraft_render::vector_fit(footage, [doc.width, doc.height], doc.pixel_size(), scale); + (size, effectcraft_svg::Affine::scale_non_uniform(sx, sy)) } #[cfg(test)] @@ -154,4 +177,77 @@ mod tests { let img = decode("small.svg", small, &footage, AlphaOp::new(footage.alpha, footage.premul_color)).unwrap().unwrap(); assert_eq!((img.width, img.height), (40, 20)); } + + /// Continuously Rasterize reports a layer scaled past the render budget as an error instead of + /// returning a partial or empty image, and still draws the same layer at an ordinary scale. + #[test] + fn rasterizing_vector_footage_past_the_budget_is_reported() { + let svg = br#""#; + let footage = probe("scaled.svg", svg).unwrap().unwrap(); + let ok = rasterize_vector("scaled.svg", svg, &footage, 1.0).unwrap().unwrap(); + assert_eq!((ok.width, ok.height), (100, 100)); + let e = rasterize_vector("scaled.svg", svg, &footage, 1000.0).unwrap().err().map(|e| e.to_string()).unwrap_or_default(); + assert!(e.contains("scaled.svg"), "{e}"); + } + + /// A 200 × 100 pt page: red left half, blue right half. + fn halves_pdf() -> Vec { + effectcraft_pdf::write::pdf( + &[ + (1, "<< /Type /Catalog /Pages 2 0 R >>".into(), None), + (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".into(), None), + (3, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Contents 4 0 R >>".into(), None), + (4, "<< >>".into(), Some(b"1 0 0 rg 0 0 100 100 re f 0 0 1 rg 100 0 100 100 re f".to_vec())), + ], + 1, + ) + } + + /// An opaque red pixel. + fn is_red(p: [f32; 4]) -> bool { + p[3] > 0.99 && p[0] > 0.9 && p[2] < 0.1 + } + + /// An opaque blue pixel. + fn is_blue(p: [f32; 4]) -> bool { + p[3] > 0.99 && p[2] > 0.9 && p[0] < 0.1 + } + + /// #3099: an After Effects project keeps the size After Effects measured for a placed .ai or + /// PDF, which can differ from this reader's page size. A placed page fills that size (the + /// layer's anchor and masks are measured against it), decoded and continuously rasterised; + /// other footage is drawn at the page's own size as before, whatever size it says. + #[test] + fn placed_vector_footage_fills_the_footage_size() { + let pdf = halves_pdf(); + let mut footage = probe("art.ai", &pdf).unwrap().unwrap(); + assert_eq!((footage.width, footage.height), (200, 100)); + assert!(!footage.placed_page); + let op = AlphaOp::new(footage.alpha, footage.premul_color); + let own = decode("art.ai", &pdf, &footage, op).unwrap().unwrap(); + assert_eq!((own.width, own.height), (200, 100)); + assert!(is_red(own.get(50, 50)) && is_blue(own.get(150, 50))); + + (footage.width, footage.height) = (100, 80); + let unplaced = decode("art.ai", &pdf, &footage, op).unwrap().unwrap(); + assert!(unplaced == own, "not placed: drawn as before"); + assert_eq!(rasterize_vector("art.ai", &pdf, &footage, 0.5).and_then(|r| r.ok()).map(|i| (i.width, i.height)), Some((100, 50))); + + footage.placed_page = true; + let img = decode("art.ai", &pdf, &footage, op).unwrap().unwrap(); + assert_eq!((img.width, img.height), (100, 80)); + for (x, y) in [(25, 3), (25, 76)] { + assert!(is_red(img.get(x, y)), "red at ({x}, {y}): {:?}", img.get(x, y)); + } + for (x, y) in [(75, 3), (75, 76)] { + assert!(is_blue(img.get(x, y)), "blue at ({x}, {y}): {:?}", img.get(x, y)); + } + + let sharp = rasterize_vector("art.ai", &pdf, &footage, 2.0).unwrap().unwrap(); + assert_eq!((sharp.width, sharp.height), (200, 160)); + assert!(is_red(sharp.get(50, 155)) && is_blue(sharp.get(150, 155))); + // Placed at the page's own size: 1:1. + (footage.width, footage.height) = (200, 100); + assert!(decode("art.ai", &pdf, &footage, op).unwrap().unwrap() == own, "placed at its own size: 1:1"); + } } diff --git a/crates/media/src/lib.rs b/crates/media/src/lib.rs index 0b509c45f..454c3705f 100644 --- a/crates/media/src/lib.rs +++ b/crates/media/src/lib.rs @@ -33,7 +33,7 @@ mod webp_anim; /// Video decoder counters (time in decoders, samples decoded, seeks, cache hits), for benchmarks. pub use filmcraft_codecs::{GopStats, gop_stats}; pub use pool::{DEFAULT_BUDGET, MediaPool, PoolStats}; -pub use probe::{DEFAULT_SEQUENCE_RATE, probe, probe_bytes, probe_model}; +pub use probe::{DEFAULT_SEQUENCE_RATE, probe, probe_bytes, probe_model, probe_noting}; /// Errors from probing or decoding footage. #[derive(Debug, thiserror::Error)] diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index a60048b66..f5d109797 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -6,13 +6,14 @@ use std::sync::{Arc, Condvar, Mutex, OnceLock}; use effectcraft_project::{AlphaMode, Footage, FootageKind, ItemId}; use effectcraft_raster::{Image, Px}; +use effectcraft_render::disk_cache::Frame8; use effectcraft_render::reads::{FileStamp, ReadFile, ReadFiles, ReadLog}; use effectcraft_render::{FloatCurve, FootageSource}; use effectcraft_time::{FrameRate, TICKS_PER_SECOND, Tick}; use filmcraft_media::{FrameRequest, SharedSource}; use image::metadata::Orientation; -use crate::convert::{AlphaOp, dynamic_to_image, frame_to_image_in, frame_to_image_scaled, turned}; +use crate::convert::{AlphaOp, dynamic_to_image, frame_to_image_in, frame_to_image_scaled, frame_to_rgba8, turned}; use crate::downmix::Order; use crate::gif_anim::{self, Anim}; use crate::reads; @@ -42,19 +43,39 @@ fn colour_bars(w: u32, h: u32) -> Image { struct Key { path: Arc, frame: i64, - /// The alpha interpretation (bits 0–1) and how float values are encoded (bits 2–3: sRGB, - /// as stored, a 2.4 power curve): they change the decoded pixels. + /// The alpha interpretation (bits 0–1), how float values are encoded (bits 2–3: sRGB, as + /// stored, a 2.4 power curve) and a placed PDF / Illustrator page's framing (bit 4, + /// [`Footage::placed_page`]): they change the decoded pixels. interp: u8, matte: [u32; 3], /// A movie frame made at this size for a reduced-resolution render ((0, 0): as decoded). size: (u32, u32), /// A still at a reduced `size`: the footage's nominal size, which decides whether it is - /// resampled (see `Inner::still_at_size`); (0, 0) otherwise. + /// resampled (see `Inner::still_at_size`); also at its own size for a placed PDF / + /// Illustrator page, which is drawn to fill it ([`Footage::placed_page`]); (0, 0) otherwise. nominal: (u32, u32), + /// Kept as 8-bit levels ([`FootageSource::frame8`]), not float. + q8: bool, +} + +/// A cached frame's pixels: float, or the 8-bit levels an 8 bpc render quantises it to. +#[derive(Clone)] +enum Pixels { + F32(Arc), + Q8(Arc), +} + +impl Pixels { + fn bytes(&self) -> usize { + match self { + Pixels::F32(img) => img.data.len() * std::mem::size_of::(), + Pixels::Q8(f) => f.rgba.len(), + } + } } struct Entry { - img: Arc, + img: Pixels, /// The files the frame was decoded from (see [`crate::reads`]). files: Arc, stamp: u64, @@ -110,8 +131,8 @@ struct Lru { } impl Lru { - /// The cached image and its read files for `k`, marking the entry as most recently used. - fn get(&mut self, k: &Key) -> Option<(Arc, Arc)> { + /// The cached frame and its read files for `k`, marking the entry as most recently used. + fn get(&mut self, k: &Key) -> Option<(Pixels, Arc)> { let e = self.map.get_mut(k)?; self.order.remove(&e.stamp); self.clock += 1; @@ -119,9 +140,9 @@ impl Lru { self.order.insert(e.stamp, k.clone()); Some((e.img.clone(), e.files.clone())) } - /// Stores an image with its read files under `k` and evicts the least recently used entries until the cache fits `budget` bytes. - fn insert(&mut self, k: Key, img: Arc, files: Arc, budget: usize) { - let bytes = img.data.len() * std::mem::size_of::() + 64; + /// Stores a frame with its read files under `k` and evicts the least recently used entries until the cache fits `budget` bytes. + fn insert(&mut self, k: Key, img: Pixels, files: Arc, budget: usize) { + let bytes = img.bytes() + 64; self.clock += 1; if let Some(old) = self.map.insert(k.clone(), Entry { img, files, stamp: self.clock, bytes }) { self.order.remove(&old.stamp); @@ -143,8 +164,10 @@ impl Lru { let Some((_, k)) = self.order.pop_first() else { break }; if let Some(e) = self.map.remove(&k) { self.bytes -= e.bytes; + // ponytail: float buffers only; add 8-bit spares if their allocation shows up in a profile if self.spare.len() < MAX_SPARE - && let Ok(img) = Arc::try_unwrap(e.img) + && let Pixels::F32(img) = e.img + && let Ok(img) = Arc::try_unwrap(img) { self.spare.push(img.data); } @@ -183,6 +206,8 @@ struct CacheState { inflight: HashSet, /// Last frame index requested per movie (sequential-playback detection). last: HashMap, i64>, + /// Movies whose frames can't be kept as 8-bit levels (more bits, or shown turned). + wide: HashSet>, } /// A movie/audio source by path ([`Opened`]), opened by the first caller while the others wait @@ -405,6 +430,7 @@ impl MediaPool { c.lru.remove(&k); } c.last.remove(path); + c.wide.remove(path); drop(c); crate::exr_channels::forget(path); } @@ -420,6 +446,21 @@ impl MediaPool { Inner::frame_at(&self.inner, footage, t, true, None, FloatCurve::Srgb) } + /// [`FootageSource::frame8`]: `None` (the renderer then asks for the float frame) for + /// anything but an 8-bit movie frame shown as decoded, and on errors. + fn frame8_of(&self, footage: &Footage, t: Tick, float: FloatCurve) -> Option> { + if footage.missing || footage.kind != FootageKind::Video || gif_anim::is_animated(footage) { + return None; + } + if lock(&self.inner.cache).wide.contains(footage.path.as_str()) { + return None; + } + match Inner::frame_px(&self.inner, footage, t, true, None, float, true) { + Ok(Pixels::Q8(f)) => Some(f), + _ => None, + } + } + /// [`FootageSource::frame`] / [`FootageSource::frame_at_size`]: `None` (logged) on errors. fn frame_sized(&self, _item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option> { if footage.missing { @@ -756,6 +797,8 @@ impl Inner { ((d.0 as i128 * rate.num as i128 + den / 2) / den) as i64 } + /// Where the frame of `footage` at `t` comes from (file, frame or media time) and its cache + /// key, at the footage's own size. fn locate(footage: &Footage, t: Tick, float: FloatCurve) -> Loc { let alpha = match footage.alpha { AlphaMode::Straight => 0, @@ -768,9 +811,10 @@ impl Inner { FloatCurve::Srgb => 0, FloatCurve::Linear => 1, FloatCurve::Gamma24 => 2, - } << 2); + } << 2) + | (u8::from(footage.placed_page) << 4); let matte = if footage.alpha == AlphaMode::Premultiplied { footage.premul_color.map(f32::to_bits) } else { [0; 3] }; - let key = |path: &str, frame| Key { path: path.into(), frame, interp, matte, size: (0, 0), nominal: (0, 0) }; + let key = |path: &str, frame| Key { path: path.into(), frame, interp, matte, size: (0, 0), nominal: (0, 0), q8: false }; match footage.kind { FootageKind::Sequence if !footage.sequence.is_empty() => { let i = Self::frame_index(footage.frame_rate, t, footage.sequence_frames(), footage.loop_count); @@ -806,10 +850,22 @@ impl Inner { /// The frame of `footage` at `t`, resampled to `size` (see [`FootageSource::frame_at_size`]): /// a movie's made at that size directly, a still's resampled once and kept at that size. fn frame_at(self: &Arc, footage: &Footage, t: Tick, playback: bool, size: Option<(u32, u32)>, float: FloatCurve) -> Result> { + match Self::frame_px(self, footage, t, playback, size, float, false)? { + Pixels::F32(img) => Ok(img), + // (a float request's key never holds 8-bit levels) + Pixels::Q8(_) => Err(MediaError::Decode(format!("{}: 8-bit frame for a float request", footage.path))), + } + } + + /// [`Self::frame_at`], kept as 8-bit levels when `q8` is set and the frame has 8 bits (a + /// float frame under the float key otherwise, and the movie is remembered as `wide`). + #[allow(clippy::too_many_arguments)] + fn frame_px(self: &Arc, footage: &Footage, t: Tick, playback: bool, size: Option<(u32, u32)>, float: FloatCurve, q8: bool) -> Result { if !footage.has_video { return Err(MediaError::Unsupported(format!("{}: no video", footage.path))); } let mut loc = Self::locate(footage, t, float); + loc.key.q8 = q8; let movie = loc.media_t.is_some(); // A still asked for at its own size is the unsized frame (one decode, one entry). if let Some(size) = size.filter(|s| movie || *s != (footage.width, footage.height)) { @@ -818,6 +874,11 @@ impl Inner { loc.key.nominal = (footage.width, footage.height); } } + // A placed PDF / Illustrator page is drawn to fill the footage's size (`layered::decode`, + // #3099): two items of one file stored at different sizes have different pixels. + if footage.placed_page && !movie { + loc.key.nominal = (footage.width, footage.height); + } let mut c = lock(&self.cache); // sequential playback of a movie: read the next frame ahead if playback && movie { @@ -844,7 +905,7 @@ impl Inner { drop(c); let (img, files) = reads::captured(|| self.decode(&loc, footage)); reads::note_all(&files); - return Ok(Arc::new(img?)); + return img; } c = self.done.wait(c).unwrap_or_else(std::sync::PoisonError::into_inner); } @@ -854,8 +915,14 @@ impl Inner { let _guard = Inflight { inner: self, key: loc.key.clone() }; let (img, files) = reads::captured(|| self.decode(&loc, footage)); reads::note_all(&files); - let img = Arc::new(img?); - lock(&self.cache).lru.insert(loc.key, img.clone(), Arc::new(files), self.budget()); + let img = img?; + let mut c = lock(&self.cache); + if q8 && let Pixels::F32(_) = img { + // not 8-bit: kept as the float frame it is, which the renderer asks for next + c.wide.insert(loc.key.path.clone()); + loc.key.q8 = false; + } + c.lru.insert(loc.key, img.clone(), Arc::new(files), self.budget()); Ok(img) } @@ -866,24 +933,25 @@ impl Inner { let next_t = t + footage.frame_rate.frame_duration(); let mut next = Self::locate(footage, next_t, float); next.key.size = loc.key.size; + next.key.q8 = loc.key.q8; if next.key != loc.key && !c.lru.map.contains_key(&next.key) && !c.inflight.contains(&next.key) { - self.prefetch(footage, next_t, size, float); + self.prefetch(footage, next_t, size, float, loc.key.q8); } } } #[cfg(not(target_arch = "wasm32"))] - fn prefetch(self: &Arc, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) { + fn prefetch(self: &Arc, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve, q8: bool) { let (me, f) = (self.clone(), footage.clone()); self.prefetched.fetch_add(1, Ordering::Relaxed); // A plain thread, not a rayon job: the decode below parallelises with rayon itself. std::thread::spawn(move || { - let _ = me.frame_at(&f, t, false, size, float); + let _ = me.frame_px(&f, t, false, size, float, q8); }); } #[cfg(target_arch = "wasm32")] - fn prefetch(self: &Arc, _footage: &Footage, _t: Tick, _size: Option<(u32, u32)>, _float: FloatCurve) {} + fn prefetch(self: &Arc, _footage: &Footage, _t: Tick, _size: Option<(u32, u32)>, _float: FloatCurve, _q8: bool) {} /// A still (image file or sequence frame) at `loc.key.size` when one is set: resampled as the /// renderer would ([`FootageSource::frame_at_size`]), so the pool keeps the reduced frame and a @@ -900,9 +968,9 @@ impl Inner { effectcraft_raster::resample(&img, w, h) } - fn decode(&self, loc: &Loc, footage: &Footage) -> Result { + fn decode(&self, loc: &Loc, footage: &Footage) -> Result { match loc.media_t { - None => self.decode_still(loc, footage).map(|img| Self::still_at_size(img, loc, footage)), + None => self.decode_still(loc, footage).map(|img| Pixels::F32(Arc::new(Self::still_at_size(img, loc, footage)))), Some(_) => self.decode_media(loc, footage), } } @@ -943,21 +1011,24 @@ impl Inner { crate::exr_channels::count_decode(false); match crate::probe::load_oriented(&bytes, crate::still_format(path, &bytes)) { Ok(img) => Ok(dynamic_to_image(&img, op)), + // Over the size limit: the layered fallback would allocate its planes anyway. + Err(e @ image::ImageError::Limits(_)) => Err(MediaError::Decode(format!("{path}: {e}"))), // A multi-layer OpenEXR file without an unnamed RGB layer: its colour layer. Err(e) => crate::exr_channels::layered_picture(path, bytes, &file, op, shared) .map_err(|why| MediaError::Decode(format!("{path}: {}", why.unwrap_or_else(|| e.to_string())))), } } - fn decode_media(&self, loc: &Loc, footage: &Footage) -> Result { + fn decode_media(&self, loc: &Loc, footage: &Footage) -> Result { let op = AlphaOp::of(footage, loc.float); let path = &loc.key.path; + let f32 = |img| Pixels::F32(Arc::new(img)); match loc.media_t { - None => self.decode_still(loc, footage), + None => self.decode_still(loc, footage).map(f32), Some(mt) if gif_anim::is_animated(footage) => { let anim = self.gif(path)?; let frame = anim.frame_at(Tick(mt.0)).ok_or_else(|| MediaError::Decode(format!("{path}: no frames")))?; - Ok(dynamic_to_image(frame, op)) + Ok(f32(dynamic_to_image(frame, op))) } Some(mt) => { let Opened { src, turn, .. } = self.opened(path).ok_or_else(|| MediaError::Io(format!("{path}: cannot open")))?; @@ -975,10 +1046,16 @@ impl Inner { && (vf.width, vf.height) == side((footage.width, footage.height)) && let Some(img) = frame_to_image_scaled(&vf, w, h) { - return Ok(turned(img, turn)); + return Ok(f32(turned(img, turn))); + } + if loc.key.q8 + && turn == Orientation::NoTransforms + && let Some(f) = frame_to_rgba8(&vf, op) + { + return Ok(Pixels::Q8(Arc::new(f))); } let buf = lock(&self.cache).lru.take_spare(vf.width as usize * vf.height as usize); - Ok(turned(frame_to_image_in(&vf, op, buf), turn)) + Ok(f32(turned(frame_to_image_in(&vf, op, buf), turn))) } } } @@ -1028,9 +1105,12 @@ impl FootageSource for MediaPool { fn frame_with(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option> { self.frame_sized(item, footage, t, size, float) } + fn frame8(&self, _item: ItemId, footage: &Footage, t: Tick, float: FloatCurve) -> Option> { + self.frame8_of(footage, t, float) + } fn problem(&self, footage: &Footage) -> Option { - if footage.missing || footage.kind != FootageKind::Video { + if footage.missing || (footage.kind != FootageKind::Video && !effectcraft_render::is_vector_footage(footage)) { return None; } lock(&self.inner.problems).get(footage.path.as_str()).cloned() @@ -1041,7 +1121,19 @@ impl FootageSource for MediaPool { return None; } let bytes = self.inner.read(&footage.path).ok()?; - crate::layered::rasterize_vector(&footage.path, &bytes, footage.layer.as_ref(), footage.page, scale).map(Arc::new) + let path: Arc = footage.path.as_str().into(); + match crate::layered::rasterize_vector(&footage.path, &bytes, footage, scale)? { + Ok(img) => { + lock(&self.inner.problems).remove(&path); + Some(Arc::new(img)) + } + // Refused by a render budget (a layer scaled too far): no frame, with the reason + // reported through `problem` rather than showing a partial image as complete. + Err(e) => { + self.inner.note_problem(&path, "", &e); + None + } + } } fn set_aux_footage(&self, paths: &[String]) { @@ -1265,8 +1357,8 @@ mod tests { #[test] fn lru_respects_budget_and_recycles() { let mut c = Lru::default(); - let img = || Arc::new(Image::new(16, 16)); // 4 KiB + overhead - let k = |i| Key { path: "a".into(), frame: i, interp: 0, matte: [0; 3], size: (0, 0), nominal: (0, 0) }; + let img = || Pixels::F32(Arc::new(Image::new(16, 16))); // 4 KiB + overhead + let k = |i| Key { path: "a".into(), frame: i, interp: 0, matte: [0; 3], size: (0, 0), nominal: (0, 0), q8: false }; let budget = 3 * (4096 + 64); for i in 0..10 { c.insert(k(i), img(), Arc::default(), budget); @@ -1282,6 +1374,18 @@ mod tests { assert!(c.take_spare(17).is_empty()); } + /// A frame kept as 8-bit levels is its own entry beside the float one, counted at 4 bytes + /// a pixel. + #[test] + fn lru_keeps_8_bit_frames_apart_at_a_quarter_of_the_bytes() { + let mut c = Lru::default(); + let k = |q8| Key { path: "a".into(), frame: 0, interp: 0, matte: [0; 3], size: (0, 0), nominal: (0, 0), q8 }; + c.insert(k(false), Pixels::F32(Arc::new(Image::new(16, 16))), Arc::default(), usize::MAX); + c.insert(k(true), Pixels::Q8(Arc::new(Frame8 { width: 16, height: 16, rgba: vec![0; 16 * 16 * 4] })), Arc::default(), usize::MAX); + assert!(matches!(c.get(&k(false)), Some((Pixels::F32(_), _))) && matches!(c.get(&k(true)), Some((Pixels::Q8(_), _)))); + assert_eq!(c.bytes, (16 * 16 * 16 + 64) + (16 * 16 * 4 + 64)); + } + /// #482: an OpenEXR file with HTJ2K compression (which the decoder lacks) says so on import /// and when its frame is read, instead of "no non-deep rgb channels" or a transparent frame. #[test] diff --git a/crates/media/src/probe.rs b/crates/media/src/probe.rs index 1fc945aa1..194eebc47 100644 --- a/crates/media/src/probe.rs +++ b/crates/media/src/probe.rs @@ -9,6 +9,15 @@ use image::metadata::Orientation; use crate::{MediaError, Result, ext_of, rate_from_fc}; +/// Most pixels a still may decode to: 4 GiB as the renderer's float RGBA (2^28, 16384 × 16384; +/// 150-megapixel medium-format frames, stitched panoramas and large scans decode). A browser tab's +/// wasm32 heap can't hold one allocation that large (and the decoded input beside it): 512 MiB +/// there, and on 32-bit targets, where one allocation may not pass `isize::MAX` (2 GiB). +#[cfg(not(any(target_arch = "wasm32", target_pointer_width = "32")))] +pub(crate) const MAX_STILL_PIXELS: u64 = 1 << 28; +#[cfg(any(target_arch = "wasm32", target_pointer_width = "32"))] +pub(crate) const MAX_STILL_PIXELS: u64 = 1 << 25; + /// Frame rate given to image sequences on import (After Effects' default preference). pub const DEFAULT_SEQUENCE_RATE: FrameRate = FrameRate::FPS_30; @@ -43,11 +52,25 @@ fn side_file_reader(model: &Path) -> impl Fn(&str) -> Option> + use<> { } } +thread_local! { + /// What the last model probed on this thread left out (see [`probe_noting`]). + static MODEL_WARNING: std::cell::RefCell> = const { std::cell::RefCell::new(None) }; +} + +/// [`probe`], and what the import left out of the file (a 3D model's textures over the limits), +/// for the user. +pub fn probe_noting(path: impl AsRef) -> Result<(Footage, Option)> { + MODEL_WARNING.with(|w| w.borrow_mut().take()); + let f = probe(path)?; + Ok((f, MODEL_WARNING.with(|w| w.borrow_mut().take()))) +} + /// Probe a 3D model (glTF 2.0 / OBJ / STL / FBX): a [`FootageKind::Model`] item whose duration is its /// longest animation. `resolve` reads sibling resources. pub fn probe_model(path: &str, bytes: &[u8], resolve: &dyn Fn(&str) -> Option>) -> Result { let m = effectcraft_model::load(path, bytes, resolve).map_err(|e| MediaError::Decode(format!("{path}: {e}")))?; let s = m.summary(); + MODEL_WARNING.with(|w| *w.borrow_mut() = (!m.warnings.is_empty()).then(|| format!("{path}: {}", m.warnings.join("; ")))); let dur = s.animations.iter().map(|a| a.1).fold(0.0f64, f64::max); let codec = match effectcraft_model::format_of(path) { Some("obj") => "OBJ", @@ -134,12 +157,12 @@ fn trim_to_edits(f: &mut Footage, shown: Option) { /// How long the video track's edit list shows its media (`elst` segments, movie timescale): /// `None` without edits, or with an empty edit (a delayed start keeps the reader's duration). fn video_shown(bytes: &[u8]) -> Option { - let file = filmcraft_isobmff::open(bytes).ok()?; - let t = file.tracks.iter().find(|t| t.kind == filmcraft_isobmff::TrackKind::Video && !t.samples.is_empty())?; + let file = craft_media::isobmff::open(bytes).ok()?; + let t = file.tracks.iter().find(|t| t.kind == craft_media::isobmff::TrackKind::Video && !t.samples.is_empty())?; edits_shown(&t.edits, file.timescale) } -fn edits_shown(edits: &[filmcraft_isobmff::Edit], timescale: u32) -> Option { +fn edits_shown(edits: &[craft_media::isobmff::Edit], timescale: u32) -> Option { if edits.is_empty() || timescale == 0 || edits.iter().any(|e| e.media_time < 0) { return None; } @@ -291,6 +314,12 @@ pub(crate) fn load_oriented(bytes: &[u8], fmt: Option) -> im None => image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format()?, }; let mut dec = reader.into_decoder()?; + // `from_decoder` skips the reader's allocation limit, and some decoders (BMP) allocate + // whatever size their header claims: refuse a still the renderer could not hold as floats. + let (w, h) = dec.dimensions(); + if u64::from(w) * u64::from(h) > MAX_STILL_PIXELS { + return Err(image::ImageError::Limits(image::error::LimitError::from_kind(image::error::LimitErrorKind::DimensionError))); + } let orientation = dec.orientation().unwrap_or(Orientation::NoTransforms); let mut img = image::DynamicImage::from_decoder(dec)?; img.apply_orientation(orientation); @@ -303,8 +332,8 @@ pub(crate) fn load_oriented(bytes: &[u8], fmt: Option) -> im /// containers have none. FilmCraft turns the frames of a pure 0/90/180/270° header itself, so this /// is only the quarter turn nearest to a header it does not turn. pub(crate) fn video_turn(bytes: &[u8]) -> Orientation { - let Ok(file) = filmcraft_isobmff::open(bytes) else { return Orientation::NoTransforms }; - let Some(track) = file.tracks.iter().find(|t| t.kind == filmcraft_isobmff::TrackKind::Video) else { + let Ok(file) = craft_media::isobmff::open(bytes) else { return Orientation::NoTransforms }; + let Some(track) = file.tracks.iter().find(|t| t.kind == craft_media::isobmff::TrackKind::Video) else { return Orientation::NoTransforms; }; // A pure rotation is already applied by FilmCraft's decoder (frames and size come out @@ -329,8 +358,8 @@ pub(crate) fn video_turn(bytes: &[u8]) -> Orientation { /// median reads as 30.303 or 58.824 fps. `None` for other containers and for movies the median /// reads right. fn video_rate(bytes: &[u8]) -> Option { - let file = filmcraft_isobmff::open(bytes).ok()?; - let t = file.tracks.iter().find(|t| t.kind == filmcraft_isobmff::TrackKind::Video && !t.samples.is_empty())?; + let file = craft_media::isobmff::open(bytes).ok()?; + let t = file.tracks.iter().find(|t| t.kind == craft_media::isobmff::TrackKind::Video && !t.samples.is_empty())?; averaged_rate(t.timescale, t.samples.iter().take(240).map(|s| s.duration)) } @@ -364,11 +393,28 @@ fn averaged_rate(timescale: u32, durations: impl Iterator) -> Option mod tests { use super::*; + #[test] + fn a_still_larger_than_the_renderer_holds_is_refused_before_decoding() { + // A 24-bit BMP header claiming 20000×20000 pixels (within the BMP decoder's own limit) + // over no pixel data. + let mut bmp = b"BM".to_vec(); + bmp.extend(54u32.to_le_bytes()); + bmp.extend([0u8; 4]); + bmp.extend(54u32.to_le_bytes()); + bmp.extend(40u32.to_le_bytes()); + bmp.extend(20_000i32.to_le_bytes()); + bmp.extend(20_000i32.to_le_bytes()); + bmp.extend(1u16.to_le_bytes()); + bmp.extend(24u16.to_le_bytes()); + bmp.extend([0u8; 24]); + assert!(matches!(load_oriented(&bmp, Some(image::ImageFormat::Bmp)), Err(image::ImageError::Limits(_)))); + } + /// #2592: an encoder-delayed H.264 track's edit list shows 75 frames at 25 fps (3 s at a /// 1000 timescale), though its media runs a frame longer; empty edits and no edits leave it. #[test] fn the_edit_list_trims_a_movie_to_the_frames_it_shows() { - let edit = |segment_duration, media_time| filmcraft_isobmff::Edit { segment_duration, media_time, media_rate: 0x0001_0000 }; + let edit = |segment_duration, media_time| craft_media::isobmff::Edit { segment_duration, media_time, media_rate: 0x0001_0000 }; let shown = edits_shown(&[edit(3000, 1024)], 1000); assert_eq!(shown, Some(Tick::from_units(3, 1))); let mut f = Footage { kind: FootageKind::Video, duration: Tick::from_units(76, 25), ..Footage::default() }; @@ -455,4 +501,21 @@ mod tests { assert_eq!(averaged_rate(u32::MAX, [u32::MAX; 300].into_iter()), None); assert_eq!(video_rate(b"not a movie"), None); } + + /// A 150-megapixel still (a medium-format frame) is not refused for its size: only its (absent) + /// pixel data fails, not the dimension check. A still past the limit is. + #[cfg(not(any(target_arch = "wasm32", target_pointer_width = "32")))] + #[test] + fn stills_up_to_the_limit_pass_the_size_check() { + let bmp = |w: u32, h: u32| { + let mut b = vec![b'B', b'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0]; + b.extend_from_slice(&w.to_le_bytes()); + b.extend_from_slice(&h.to_le_bytes()); + b.extend_from_slice(&[1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + b + }; + let limit = |e: image::ImageError| matches!(e, image::ImageError::Limits(_)); + assert!(!load_oriented(&bmp(14_000, 10_800), None).err().is_some_and(limit), "150 MP passes the check"); + assert!(load_oriented(&bmp(20_000, 20_000), None).err().is_some_and(limit), "400 MP is refused"); + } } diff --git a/crates/media/src/webp_anim.rs b/crates/media/src/webp_anim.rs index 9420be1ed..147fe8264 100644 --- a/crates/media/src/webp_anim.rs +++ b/crates/media/src/webp_anim.rs @@ -23,7 +23,7 @@ pub(crate) const CODEC: &str = "WebP"; /// Whether `bytes` start like a WebP file. pub(crate) fn is_webp(bytes: &[u8]) -> bool { - bytes.get(0..4) == Some(b"RIFF") && bytes.get(8..12) == Some(b"WEBP") + craft_webp::is_webp(bytes) } /// What the chunk headers of an animated WebP say. @@ -35,46 +35,11 @@ struct Header { delays: Vec, } -fn u24(b: &[u8]) -> Option { - match b { - [a, b, c, ..] => Some(u32::from(*a) | u32::from(*b) << 8 | u32::from(*c) << 16), - _ => None, - } -} - /// Read the `VP8X` header and the `ANMF` frame headers. `None` when the file isn't an animated /// WebP. A truncated tail keeps the frames before it. fn header(bytes: &[u8]) -> Option
{ - if !is_webp(bytes) { - return None; - } - let riff = bytes.get(4..8).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes)?; - let end = usize::try_from(riff).ok()?.saturating_add(8).min(bytes.len()); - let (mut at, mut canvas, mut delays) = (12usize, None, Vec::new()); - while delays.len() < MAX_FRAMES { - let Some(head) = bytes.get(at..at.saturating_add(8)).filter(|_| at.saturating_add(8) <= end) else { break }; - let size = head.get(4..8).and_then(|b| b.try_into().ok()).map(u32::from_le_bytes)?; - let size = usize::try_from(size).ok()?; - let body_end = at.saturating_add(8).saturating_add(size); - let Some(body) = bytes.get(at.saturating_add(8)..body_end).filter(|_| body_end <= end) else { break }; - match head.get(0..4) { - Some(b"VP8X") => { - let flags = *body.first()?; - if flags & 0x02 == 0 { - // No animation flag: a still. - return None; - } - let w = u24(body.get(4..)?)?.saturating_add(1); - let h = u24(body.get(7..)?)?.saturating_add(1); - canvas = Some((w, h, flags & 0x10 != 0)); - } - Some(b"ANMF") if body.len() >= 16 => delays.push(u24(body.get(12..)?)?), - _ => {} - } - at = body_end.saturating_add(size & 1); - } - let (width, height, alpha) = canvas?; - Some(Header { width, height, alpha, delays }) + let info = craft_webp::scan(bytes, MAX_FRAMES).filter(|i| i.animated)?; + Some(Header { width: info.width, height: info.height, alpha: info.alpha, delays: info.frames.iter().map(|f| f.duration_ms).collect() }) } /// The delay of a frame in milliseconds. Browsers show delays of 10 ms or less (that encoders @@ -157,6 +122,11 @@ pub(crate) fn load(bytes: &[u8]) -> Result { return Err(MediaError::Unsupported(format!("animated WebP of {} frames of {}×{} is too large to hold in memory", h.delays.len(), h.width, h.height))); } let dec = WebPDecoder::new(Cursor::new(bytes)).map_err(|e| MediaError::Decode(format!("WebP: {e}")))?; + // The budget above used the canvas this file's header scan found; the decoder must agree, or + // a second VP8X header could make it allocate a far larger canvas. + if image::ImageDecoder::dimensions(&dec) != (h.width, h.height) { + return Err(MediaError::Decode("WebP: conflicting canvas sizes".into())); + } let (mut frames, mut delays, mut size) = (Vec::new(), Vec::new(), 0usize); for frame in dec.into_frames() { let frame = match frame { @@ -326,4 +296,20 @@ mod tests { let e = super::load(&bytes).err().unwrap(); assert!(e.to_string().contains("too large"), "{e}"); } + + #[test] + fn conflicting_canvas_headers_are_refused() { + // The first VP8X claims 16384×16384; a second one, which the header scan keeps, 1×1. + let mut bytes = webp(1, 1, &[(RED, 50); 2]); + bytes[24..27].copy_from_slice(&u24(16383)); + bytes[27..30].copy_from_slice(&u24(16383)); + let mut small = vec![0x12, 0, 0, 0]; + small.extend_from_slice(&u24(0)); + small.extend_from_slice(&u24(0)); + let second = chunk(b"VP8X", &small); + bytes.splice(30..30, second.iter().copied()); + let riff = u32::try_from(bytes.len() - 8).unwrap(); + bytes[4..8].copy_from_slice(&riff.to_le_bytes()); + assert!(super::load(&bytes).is_err()); + } } diff --git a/crates/media/tests/it/aac_layout.rs b/crates/media/tests/it/aac_layout.rs index fb3e078a6..896e4f0a7 100644 --- a/crates/media/tests/it/aac_layout.rs +++ b/crates/media/tests/it/aac_layout.rs @@ -1,5 +1,5 @@ //! Multichannel AAC reaches stereo by where each channel plays, whatever order its channel -//! configuration decodes in (#1701). Fixtures are tones encoded with FilmCraft's AAC encoder (no +//! configuration decodes in (#1701). Fixtures are tones encoded with craft-audio's AAC encoder (no //! ffmpeg needed, so nothing is skipped), with the configuration rewritten where the coded //! elements fit another: configurations 11 and 12 code the same elements as the encoder's 7- and //! 8-channel output, and a program config element can name any of them. @@ -9,9 +9,9 @@ use std::io::Cursor; use std::path::PathBuf; use std::sync::Arc; +use craft_audio::aac::{AudioSpecificConfig, Encoder, EncoderConfig, ProgramConfig}; use effectcraft_media::{MediaPool, probe, probe_bytes}; use effectcraft_time::Tick; -use filmcraft_aac::{AudioSpecificConfig, Encoder, EncoderConfig, ProgramConfig}; const RATE: u32 = 48_000; /// Tone frequencies by channel; an LFE gets [`LFE_TONE`], which its element can carry. @@ -61,7 +61,9 @@ fn lc(config: u8) -> Option { fn pce(front: Vec<(bool, u8)>, side: Vec<(bool, u8)>, back: Vec<(bool, u8)>, lfe: Vec, comment: &[u8]) -> Option { let pce = ProgramConfig { object_type: 1, sf_index: 3, front, side, back, lfe, comment: comment.to_vec(), ..Default::default() }; - Some(AudioSpecificConfig { pce: Some(pce), ..AudioSpecificConfig::lc(3, 0) }) + let mut asc = AudioSpecificConfig::lc(3, 0); + asc.pce = Some(pce); + Some(asc) } use Ch::*; @@ -115,7 +117,7 @@ fn mp4(asc: Vec, channels: u32, units: &[Vec]) -> Vec { /// As [`mp4`], after an audio track with the configuration and channel count `empty` and no /// samples, if any. fn mp4_after(empty: Option<(Vec, u32)>, asc: Vec, channels: u32, units: &[Vec]) -> Vec { - use filmcraft_isobmff::{Brand, Mp4Writer, SampleEntry, TrackConfig, WriteSample, WriterOptions}; + use craft_media::isobmff::{Brand, Mp4Writer, SampleEntry, TrackConfig, WriteSample, WriterOptions}; let mut w = Mp4Writer::new(Cursor::new(Vec::new()), WriterOptions::new(Brand::Mp4)).expect("writer"); if let Some((asc, channels)) = empty { w.add_track(TrackConfig::new(SampleEntry::aac(asc, channels, RATE), RATE)).expect("empty track"); @@ -134,7 +136,7 @@ fn mkv(asc: Vec, channels: u32, units: &[Vec]) -> Vec { /// As [`mkv`], after an audio track with the configuration and channel count `empty` and no /// frames, if any. fn mkv_after(empty: Option<(Vec, u32)>, asc: Vec, channels: u32, units: &[Vec]) -> Vec { - use filmcraft_matroska::{MkvWriter, MuxOptions, TrackKind, TrackSpec}; + use craft_media::matroska::{MkvWriter, MuxOptions, TrackKind, TrackSpec}; let track = |asc: Vec, channels: u32| { let mut t = TrackSpec::new(TrackKind::Audio, "A_AAC"); t.codec_private = asc; @@ -210,7 +212,7 @@ fn lfe_is_coded(case: &Case, bytes: Vec) { fn every_aac_layout_mixes_by_speaker_in_mp4() { for case in cases() { let (asc, units) = encode(case.content, false); - let asc = case.asc.as_ref().map_or(asc, AudioSpecificConfig::to_bytes); + let asc = case.asc.as_ref().map_or(asc, |a| a.to_bytes().expect("ASC")); let bytes = mp4(asc, case.content.len() as u32, &units); lfe_is_coded(&case, bytes.clone()); let p = write(&format!("{}.mp4", case.name), &bytes); @@ -227,7 +229,7 @@ fn every_aac_layout_mixes_by_speaker_in_mp4() { fn matroska_aac_layouts_mix_by_speaker() { for case in cases().into_iter().filter(|c| matches!(c.name, "config12" | "pce61-centre-first" | "pce61-ffmpeg")) { let (asc, units) = encode(case.content, false); - let asc = case.asc.as_ref().map_or(asc, AudioSpecificConfig::to_bytes); + let asc = case.asc.as_ref().map_or(asc, |a| a.to_bytes().expect("ASC")); let p = write(&format!("{}.mkv", case.name), &mkv(asc, case.content.len() as u32, &units)); check(case.name, case.content, &mix(&MediaPool::new(), &probe(&p).expect("probe"))); } @@ -251,9 +253,9 @@ fn an_empty_audio_track_lends_its_configuration_to_no_other() { for (name, empty) in [("config12", 7), ("pce61-centre-first", 11)] { let case = cases().into_iter().find(|c| c.name == name).expect("case"); let n = case.content.len() as u32; - let empty = Some((AudioSpecificConfig::lc(3, empty).to_bytes(), n)); + let empty = Some((AudioSpecificConfig::lc(3, empty).to_bytes().expect("ASC"), n)); let (asc, units) = encode(case.content, false); - let asc = case.asc.as_ref().map_or(asc, AudioSpecificConfig::to_bytes); + let asc = case.asc.as_ref().map_or(asc, |a| a.to_bytes().expect("ASC")); let p = write(&format!("{name}-after-empty.mp4"), &mp4_after(empty.clone(), asc.clone(), n, &units)); check(&format!("{name} MP4"), case.content, &mix(&MediaPool::new(), &probe(&p).expect("probe"))); let p = write(&format!("{name}-after-empty.mkv"), &mkv_after(empty, asc, n, &units)); diff --git a/crates/media/tests/it/downmix.rs b/crates/media/tests/it/downmix.rs index 05205ab6a..f974c6cb0 100644 --- a/crates/media/tests/it/downmix.rs +++ b/crates/media/tests/it/downmix.rs @@ -174,7 +174,7 @@ fn each_channel_reaches_stereo_at_its_bs775_gain() { fn whole_pce_entry(p: &Path, channels: usize) -> PathBuf { let mut bytes = std::fs::read(p).expect("read"); let Some((asc, 2)) = aac_entry(&bytes) else { return p.to_path_buf() }; - if channels <= 2 || filmcraft_aac::AudioSpecificConfig::parse(&asc).map(|a| a.channel_config).ok() != Some(0) { + if channels <= 2 || craft_audio::aac::AudioSpecificConfig::parse(&asc).map(|a| a.channel_config).ok() != Some(0) { return p.to_path_buf(); } // the `mp4a` entry's channelcount follows its 6 reserved bytes, data reference index and @@ -189,9 +189,9 @@ fn whole_pce_entry(p: &Path, channels: usize) -> PathBuf { /// The AudioSpecificConfig and sample entry channel count of an MP4's first AAC track. fn aac_entry(bytes: &[u8]) -> Option<(Vec, u32)> { - let file = filmcraft_isobmff::open(bytes).ok()?; + let file = craft_media::isobmff::open(bytes).ok()?; file.tracks.iter().flat_map(|t| &t.entries).find_map(|e| match &e.codec { - filmcraft_isobmff::CodecConfig::Aac(a) => Some((a.asc.clone(), e.audio.as_ref()?.channels)), + craft_media::isobmff::CodecConfig::Aac(a) => Some((a.asc.clone(), e.audio.as_ref()?.channels)), _ => None, }) } diff --git a/crates/media/tests/it/file_timecode.rs b/crates/media/tests/it/file_timecode.rs index e5cbfa92d..dae3335ba 100644 --- a/crates/media/tests/it/file_timecode.rs +++ b/crates/media/tests/it/file_timecode.rs @@ -2,8 +2,8 @@ use std::io::Cursor; +use craft_media::isobmff::{Brand, Mp4Writer, SampleEntry, TimecodeConfig, TrackConfig, WriteSample, WriterOptions}; use effectcraft_project::FootageKind; -use filmcraft_isobmff::{Brand, Mp4Writer, SampleEntry, TimecodeConfig, TrackConfig, WriteSample, WriterOptions}; /// A two-frame 24 fps Motion JPEG MOV, with a timecode track from `start_frame` when given. fn mov(start_frame: Option) -> Vec { diff --git a/crates/media/tests/it/footage.rs b/crates/media/tests/it/footage.rs index 7f0de6ae4..ed152541c 100644 --- a/crates/media/tests/it/footage.rs +++ b/crates/media/tests/it/footage.rs @@ -747,8 +747,8 @@ fn sequence_gaps_hold_the_previous_frame() { /// parallax project's 21120-px plates were decoded and resampled for every frame at Quarter). #[test] fn stills_at_reduced_sizes_are_resampled_once() { - let dir = std::env::temp_dir().join(format!("ec-still-size-{}", std::process::id())); - std::fs::create_dir_all(&dir).expect("temp dir"); + let guard = tempfile::tempdir().expect("temp dir"); + let dir = guard.path().to_path_buf(); let p = dir.join("plate.png"); let px = |x: u32, y: u32| { image::Rgba([(x * 7 % 256) as u8, (y * 13 % 256) as u8, ((x ^ y) % 256) as u8, if (x + y).is_multiple_of(5) { 0 } else { 255 - (x % 128) as u8 }]) diff --git a/crates/model/Cargo.toml b/crates/model/Cargo.toml index ebe7da7e8..1dfa79430 100644 --- a/crates/model/Cargo.toml +++ b/crates/model/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "effectcraft-model" -description = "3D models: glTF 2.0 (.gltf/.glb), Wavefront OBJ/MTL, STL and FBX 7.x (binary and ASCII) import, PBR metallic-roughness materials, node hierarchy, skinning and animation, parametric primitives, and extruded/bevelled outline meshes" +description = "3D models: glTF 2.0 (.gltf/.glb), Wavefront OBJ/MTL, STL and FBX 6.x/7.x (binary and ASCII) import through craft-mesh, PBR metallic-roughness materials, node hierarchy, skinning and animation, parametric primitives, and extruded/bevelled outline meshes" version.workspace = true edition.workspace = true license.workspace = true @@ -10,10 +10,9 @@ rust-version.workspace = true workspace = true [dependencies] +craft-mesh = { workspace = true } effectcraft-geom = { workspace = true } serde = { workspace = true } -serde_json = { workspace = true } thiserror = { workspace = true } log = { workspace = true } image = { workspace = true } -miniz_oxide = "0.9" diff --git a/crates/model/src/extrude.rs b/crates/model/src/extrude.rs index 114606b95..71d550367 100644 --- a/crates/model/src/extrude.rs +++ b/crates/model/src/extrude.rs @@ -96,10 +96,14 @@ fn point_in(p: P, c: &[P]) -> bool { inside } +/// Most contours grouped into outlines; containment is tested pairwise, so the smallest are dropped. +pub const MAX_CONTOURS: usize = 10_000; + /// Group closed contours into outlines by nesting depth (even depth = outer, odd = hole of the -/// innermost containing outer contour). Degenerate contours are dropped. +/// innermost containing outer contour). Degenerate contours are dropped, as are the smallest +/// past [`MAX_CONTOURS`] (a kept hole always keeps its larger container). pub fn group_contours(contours: Vec>) -> Vec { - let cs: Vec> = contours + let mut cs: Vec> = contours .into_iter() .map(|mut c| { c.dedup(); @@ -110,26 +114,60 @@ pub fn group_contours(contours: Vec>) -> Vec { }) .filter(|c| c.len() >= 3 && signed_area(c).abs() > 1e-9) .collect(); + if cs.len() > MAX_CONTOURS { + log::warn!("model: {} contours, grouping the {MAX_CONTOURS} largest", cs.len()); + // Keep the largest, in their original order. + let area: Vec = cs.iter().map(|c| signed_area(c).abs()).collect(); + let mut keep: Vec = (0..cs.len()).collect(); + keep.sort_by(|&a, &b| area[b].total_cmp(&area[a])); + keep.truncate(MAX_CONTOURS); + keep.sort_unstable(); + cs = keep.into_iter().map(|i| std::mem::take(&mut cs[i])).collect(); + } let n = cs.len(); let area: Vec = cs.iter().map(|c| signed_area(c).abs()).collect(); - // Containers of each contour (by a vertex inside; nested contours do not cross). - let parents: Vec> = (0..n) + let bounds: Vec<[P; 2]> = cs + .iter() + .map(|c| c.iter().fold([[f64::MAX; 2], [f64::MIN; 2]], |[lo, hi], p| [[lo[0].min(p[0]), lo[1].min(p[1])], [hi[0].max(p[0]), hi[1].max(p[1])]])) + .collect(); + // The containers of each contour: the larger contours holding a vertex of it (nested contours + // do not cross); the bounding box must hold the vertex first. Depth is their count, so + // overlapping (non-nested) paths keep every container they sit inside. + let containers = |i: usize, only: &dyn Fn(usize) -> bool| -> Vec { + let p = cs[i][0]; + (0..n) + .filter(|&j| j != i && area[j] > area[i] && only(j)) + .filter(|&j| (bounds[j][0][0]..=bounds[j][1][0]).contains(&p[0]) && (bounds[j][0][1]..=bounds[j][1][1]).contains(&p[1])) + .filter(|&j| point_in(p, &cs[j])) + .collect() + }; + let depth: Vec = (0..n).map(|i| containers(i, &|_| true).len()).collect(); + // A hole belongs to the innermost containing contour that is itself an outline: with + // overlapping paths the smallest container can be a hole, which has no outline to take it. + let parent: Vec> = (0..n) .map(|i| { - let probe = cs[i][0]; - (0..n).filter(|&j| j != i && area[j] > area[i] && point_in(probe, &cs[j])).collect() + if depth[i].is_multiple_of(2) { + None + } else { + containers(i, &|j| depth[j].is_multiple_of(2)).into_iter().min_by(|&a, &b| area[a].total_cmp(&area[b])) + } }) .collect(); let mut outlines: Vec = vec![]; let mut slot = vec![usize::MAX; n]; let mut order: Vec = (0..n).collect(); - order.sort_by_key(|&i| parents[i].len()); - for i in order { - let depth = parents[i].len(); - if depth.is_multiple_of(2) { + order.sort_by_key(|&i| depth[i]); + // Every outline gets its slot before any hole is attached: with overlapping paths a hole's + // parent can be deeper than the hole itself. + for &i in &order { + if depth[i].is_multiple_of(2) { slot[i] = outlines.len(); outlines.push(Outline { outer: cs[i].clone(), holes: vec![] }); - } else if let Some(&p) = parents[i].iter().filter(|&&j| parents[j].len() == depth - 1).min_by(|&&a, &&b| area[a].total_cmp(&area[b])) - && slot[p] != usize::MAX + } + } + for &i in &order { + if !depth[i].is_multiple_of(2) + && let Some(p) = parent[i] { outlines[slot[p]].holes.push(cs[i].clone()); } diff --git a/crates/model/src/fbx.rs b/crates/model/src/fbx.rs index bca387ff2..fead03b21 100644 --- a/crates/model/src/fbx.rs +++ b/crates/model/src/fbx.rs @@ -1,953 +1,34 @@ -//! FBX 7.x scenes, binary and ASCII. -//! -//! Written from public descriptions of the format (the binary node-record layout, the ASCII -//! syntax, and the scene objects of the Autodesk FBX SDK documentation): no importer source was -//! read. -//! -//! - **Binary**: the `Kaydara FBX Binary` header and version, node records with 32-bit (before -//! 7.5) or 64-bit offsets, every property type (`Y C I F D L S R` and the `f d l i b` arrays, -//! zlib-compressed or not). Object names `Name\0\x01Class` become `Class::Name` as in ASCII. -//! - **ASCII**: `Key: value, value { children }` with `;` comments and `*N { a: … }` arrays. -//! - **Scene**: `Geometry` meshes (`Vertices`, `PolygonVertexIndex` polygons fan-triangulated, -//! the first `LayerElementNormal`, `LayerElementUV` and `LayerElementMaterial` with every -//! mapping and reference mode), `Model` nodes with the SDK transform (translation, rotation -//! offset and pivot, pre/post rotation, Euler rotation in its order, scaling offset and pivot, -//! scaling) and geometric transforms, `Material` (Lambert/Phong mapped onto -//! metallic-roughness), `Texture` and `Video` (embedded or relative image files), joined by -//! `Connections`. `GlobalSettings` axes turn the scene to +Y up. -//! -//! Not imported: FBX 6.x and older, animation stacks, skin deformers, blend shapes, cameras and -//! lights, NURBS and patches. Units stay the file's own (like OBJ). +//! FBX 6.x and 7.x scenes, binary and ASCII, read with `craft-mesh` (`craft_mesh::fbx`): +//! `Geometry` meshes, `Model` nodes with the SDK transform and geometric transforms, `Material` +//! (Lambert/Phong mapped onto metallic-roughness), embedded or relative `Texture` images, +//! joined by `Connections`. A root node turns the file's `GlobalSettings` axes to +Y up; units +//! stay the file's own (like OBJ). Meshes without normals get smooth ones. -use std::collections::HashMap; -use std::sync::Arc; +use crate::scene::{MissingNormals, error, limits, mat4, to_model}; +use crate::{Model, ModelError, Node, Result}; -use crate::obj::srgb_to_linear; -use crate::{AlphaMode, Mat4, Material, Mesh, Model, ModelError, Node, Primitive, Result, TexRef, Texture, perr, vec3}; +pub use craft_mesh::fbx::BINARY_MAGIC; -/// The binary file's magic (`Kaydara FBX Binary`, two spaces, NUL, 0x1A, 0x00). -pub const BINARY_MAGIC: &[u8] = b"Kaydara FBX Binary \0\x1a\0"; - -/// Deepest node nesting accepted (real files nest well under 10 levels). -const MAX_DEPTH: usize = 64; -/// Largest decoded array accepted (bytes). -const MAX_ARRAY_BYTES: usize = 1 << 30; -/// Most memory one import may keep: parsed arrays as stored (8 bytes per element, whatever the -/// wire type) plus the meshes built from them (#1234). -const MAX_IMPORT_BYTES: usize = 1 << 31; - -/// What is left of an import's memory budget. -struct Budget(std::cell::Cell); - -impl Budget { - fn new(bytes: usize) -> Budget { - Budget(std::cell::Cell::new(bytes)) - } - /// Take `bytes` (None: a size that overflowed) from the budget, or fail the import before - /// anything that large is allocated. - fn take(&self, bytes: Option, what: &str) -> Result<()> { - let left = self.0.get(); - match bytes.filter(|&b| b <= left) { - Some(b) => { - self.0.set(left - b); - Ok(()) - } - None => Err(perr(format!("FBX: {what} would take the import past its {} MiB memory limit", MAX_IMPORT_BYTES >> 20))), - } - } -} - -/// Bytes a mesh keeps. -fn mesh_bytes(m: &Mesh) -> Option { - m.primitives.iter().try_fold(0usize, |acc, p| { - let n = p.positions.len().checked_mul(12)?.checked_add(p.normals.len().checked_mul(12)?)?.checked_add(p.uvs.len().checked_mul(8)?)?; - let n = n.checked_add(p.tangents.len().checked_mul(16)?)?.checked_add(p.joints.len().checked_mul(8)?)?.checked_add(p.weights.len().checked_mul(16)?)?; - acc.checked_add(n)?.checked_add(p.indices.len().checked_mul(4)?) - }) -} - -/// Bytes the values under `nodes` keep. Used for ASCII files, which hold array elements as -/// one `Prop` each and are parsed before they can be counted (the text itself bounds them). -fn value_bytes(nodes: &[FbxNode]) -> Option { - nodes.iter().try_fold(0usize, |acc, n| { - let own = n.props.iter().try_fold(0usize, |a, p| { - let extra = match p { - Prop::Ints(v) => v.len().checked_mul(8)?, - Prop::Floats(v) => v.len().checked_mul(8)?, - Prop::Str(v) => v.len(), - Prop::Bytes(v) => v.len(), - Prop::Int(_) | Prop::Float(_) => 0, - }; - a.checked_add(std::mem::size_of::())?.checked_add(extra) - })?; - acc.checked_add(own)?.checked_add(value_bytes(&n.children)?) - }) -} - -/// One property value of an FBX node. -#[derive(Clone, Debug, PartialEq)] -pub enum Prop { - Int(i64), - Float(f64), - Str(String), - Bytes(Vec), - Ints(Vec), - Floats(Vec), -} - -impl Prop { - fn as_f64(&self) -> Option { - match self { - Prop::Int(i) => Some(*i as f64), - Prop::Float(f) => Some(*f), - _ => None, - } - } - fn as_i64(&self) -> Option { - match self { - Prop::Int(i) => Some(*i), - Prop::Float(f) if f.is_finite() => Some(*f as i64), - _ => None, - } - } - fn as_str(&self) -> Option<&str> { - match self { - Prop::Str(s) => Some(s), - _ => None, - } - } -} - -/// A node of the FBX document tree (the same for binary and ASCII files). -#[derive(Clone, Debug, Default, PartialEq)] -pub struct FbxNode { - pub name: String, - pub props: Vec, - pub children: Vec, -} - -impl FbxNode { - pub fn child(&self, name: &str) -> Option<&FbxNode> { - self.children.iter().find(|c| c.name == name) - } - pub fn children_named<'a>(&'a self, name: &'a str) -> impl Iterator + 'a { - self.children.iter().filter(move |c| c.name == name) - } - fn prop(&self, i: usize) -> Option<&Prop> { - self.props.get(i) - } - fn str_at(&self, i: usize) -> Option<&str> { - self.prop(i).and_then(Prop::as_str) - } - /// The node's array as floats: a binary array property, an ASCII `a:` child, or the plain - /// value list. - pub fn floats(&self) -> Vec { - if let Some(a) = self.child("a") { - return a.floats(); - } - let mut out = vec![]; - for p in &self.props { - match p { - Prop::Floats(v) => out.extend_from_slice(v), - Prop::Ints(v) => out.extend(v.iter().map(|&i| i as f64)), - Prop::Int(i) => out.push(*i as f64), - Prop::Float(f) => out.push(*f), - _ => {} - } - } - out - } - /// The node's array as integers (see [`FbxNode::floats`]). - pub fn ints(&self) -> Vec { - if let Some(a) = self.child("a") { - return a.ints(); - } - let mut out = vec![]; - for p in &self.props { - match p { - Prop::Ints(v) => out.extend_from_slice(v), - Prop::Floats(v) => out.extend(v.iter().filter(|f| f.is_finite()).map(|&f| f as i64)), - p => out.extend(p.as_i64()), - } - } - out - } -} - -/// Read an FBX file (binary or ASCII) into its top-level nodes and the file version -/// (e.g. 7400). -pub fn read(bytes: &[u8]) -> Result<(u32, Vec)> { - read_within(bytes, &Budget::new(MAX_IMPORT_BYTES)) -} - -fn read_within(bytes: &[u8], budget: &Budget) -> Result<(u32, Vec)> { - if bytes.starts_with(BINARY_MAGIC) { - read_binary(bytes, budget) - } else { - let (version, nodes) = read_ascii(&String::from_utf8_lossy(bytes))?; - budget.take(value_bytes(&nodes), "its values")?; - Ok((version, nodes)) - } -} - -// --------------------------------------------------------------------------------------------- -// Binary - -struct Bin<'a> { - d: &'a [u8], - /// 64-bit record offsets (version 7500 and later). - wide: bool, - budget: &'a Budget, -} - -impl Bin<'_> { - fn take(&self, pos: &mut usize, n: usize) -> Result<&[u8]> { - let end = pos.checked_add(n).ok_or_else(|| perr("FBX: offset overflow"))?; - let s = self.d.get(*pos..end).ok_or_else(|| perr(format!("FBX: truncated at byte {pos}")))?; - *pos = end; - Ok(s) - } - fn arr(&self, pos: &mut usize) -> Result<[u8; N]> { - let s = self.take(pos, N)?; - let mut a = [0u8; N]; - a.copy_from_slice(s); - Ok(a) - } - fn u32(&self, pos: &mut usize) -> Result { - Ok(u32::from_le_bytes(self.arr(pos)?)) - } - fn offset(&self, pos: &mut usize) -> Result { - if self.wide { Ok(u64::from_le_bytes(self.arr(pos)?)) } else { Ok(self.u32(pos)? as u64) } - } - fn to_usize(v: u64) -> Result { - usize::try_from(v).map_err(|_| perr("FBX: offset out of range")) - } - - /// Records until `end` or a null record. - fn list(&self, pos: &mut usize, end: usize, depth: usize) -> Result> { - if depth > MAX_DEPTH { - return Err(perr("FBX: nodes nested too deeply")); - } - let mut out = vec![]; - while *pos < end { - match self.record(pos, depth)? { - Some(n) => out.push(n), - None => break, - } - } - Ok(out) - } - - /// One node record; `None` for the null record that closes a list. - fn record(&self, pos: &mut usize, depth: usize) -> Result> { - let start = *pos; - let end = Self::to_usize(self.offset(pos)?)?; - let nprops = self.offset(pos)?; - let plen = Self::to_usize(self.offset(pos)?)?; - let nlen = self.arr::<1>(pos)?[0] as usize; - if end == 0 && nprops == 0 && plen == 0 && nlen == 0 { - return Ok(None); - } - if end <= start || end > self.d.len() { - return Err(perr(format!("FBX: bad record end {end} at byte {start}"))); - } - let name = String::from_utf8_lossy(self.take(pos, nlen)?).into_owned(); - let pstart = *pos; - let pend = pstart.checked_add(plen).filter(|&e| e <= end).ok_or_else(|| perr(format!("FBX: `{name}`: property list past the record")))?; - // Every property is at least one byte, so a count above the list length is corrupt. - if nprops > plen as u64 { - return Err(perr(format!("FBX: `{name}`: {nprops} properties in {plen} bytes"))); - } - let mut props = Vec::with_capacity(nprops as usize); - for _ in 0..nprops { - props.push(self.prop(pos, pend)?); - } - *pos = pend; - let children = if *pos < end { self.list(pos, end, depth + 1)? } else { vec![] }; - *pos = end; - Ok(Some(FbxNode { name, props, children })) - } - - fn prop(&self, pos: &mut usize, pend: usize) -> Result { - let sub = Bin { d: self.d.get(..pend).ok_or_else(|| perr("FBX: truncated property"))?, wide: self.wide, budget: self.budget }; - let t = sub.arr::<1>(pos)?[0]; - Ok(match t { - b'Y' => Prop::Int(i16::from_le_bytes(sub.arr(pos)?) as i64), - b'C' => Prop::Int((sub.arr::<1>(pos)?[0] != 0) as i64), - b'I' => Prop::Int(i32::from_le_bytes(sub.arr(pos)?) as i64), - b'L' => Prop::Int(i64::from_le_bytes(sub.arr(pos)?)), - b'F' => Prop::Float(f32::from_le_bytes(sub.arr(pos)?) as f64), - b'D' => Prop::Float(f64::from_le_bytes(sub.arr(pos)?)), - b'S' => { - let n = sub.u32(pos)? as usize; - Prop::Str(object_name(sub.take(pos, n)?)) - } - b'R' => { - let n = sub.u32(pos)? as usize; - Prop::Bytes(sub.take(pos, n)?.to_vec()) - } - b'f' | b'd' | b'l' | b'i' | b'b' => { - let len = sub.u32(pos)? as usize; - let enc = sub.u32(pos)?; - let clen = sub.u32(pos)? as usize; - let size = match t { - b'd' | b'l' => 8, - b'f' | b'i' => 4, - _ => 1, - }; - let want = len.checked_mul(size).filter(|&n| n <= MAX_ARRAY_BYTES).ok_or_else(|| perr("FBX: array too large"))?; - // Elements are kept as f64/i64 (booleans and 32-bit values widen), so the budget - // is charged for what is kept, not for the wire bytes. - sub.budget.take(len.checked_mul(8), "an array")?; - let data = sub.take(pos, clen)?; - let raw = match enc { - 0 => data.to_vec(), - 1 => miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(data, want).map_err(|e| perr(format!("FBX: array zlib: {e:?}")))?, - e => return Err(perr(format!("FBX: unknown array encoding {e}"))), - }; - let raw = raw.get(..want).ok_or_else(|| perr("FBX: array shorter than its length"))?; - match t { - b'f' => Prop::Floats(raw.as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes(*c) as f64).collect()), - b'd' => Prop::Floats(raw.as_chunks::<8>().0.iter().map(|c| f64::from_le_bytes(*c)).collect()), - b'l' => Prop::Ints(raw.as_chunks::<8>().0.iter().map(|c| i64::from_le_bytes(*c)).collect()), - b'i' => Prop::Ints(raw.as_chunks::<4>().0.iter().map(|c| i32::from_le_bytes(*c) as i64).collect()), - _ => Prop::Ints(raw.iter().map(|&b| (b != 0) as i64).collect()), - } - } - t => return Err(perr(format!("FBX: unknown property type 0x{t:02x}"))), - }) - } -} - -/// Binary object names are `Name\0\x01Class`; ASCII writes `Class::Name`. -fn object_name(b: &[u8]) -> String { - let s = String::from_utf8_lossy(b); - match s.split_once("\0\x01") { - Some((name, class)) => format!("{class}::{name}"), - None => s.into_owned(), - } -} - -fn read_binary(bytes: &[u8], budget: &Budget) -> Result<(u32, Vec)> { - let mut pos = BINARY_MAGIC.len(); - let version = Bin { d: bytes, wide: false, budget }.u32(&mut pos)?; - let bin = Bin { d: bytes, wide: version >= 7500, budget }; - let mut nodes = vec![]; - while pos < bytes.len() { - match bin.record(&mut pos, 0) { - Ok(Some(n)) => nodes.push(n), - Ok(None) => break, - // A damaged tail (footer, padding) after complete sections is ignored. - Err(_) if nodes.iter().any(|n| n.name == "Objects") => break, - Err(e) => return Err(e), - } - } - Ok((version, nodes)) -} - -// --------------------------------------------------------------------------------------------- -// ASCII - -#[derive(Clone, Debug, PartialEq)] -enum Tok { - Key(String), - Val(Prop), - /// `*N` array length (the values follow in an `a:` child). - Count, - Comma, - Open, - Close, -} - -fn tokenize(text: &str) -> Vec { - let mut out = vec![]; - let mut it = text.char_indices().peekable(); - while let Some(&(i, c)) = it.peek() { - match c { - c if c.is_whitespace() => { - it.next(); - } - ';' => { - for (_, c) in it.by_ref() { - if c == '\n' { - break; - } - } - } - ',' => { - it.next(); - out.push(Tok::Comma); - } - '{' => { - it.next(); - out.push(Tok::Open); - } - '}' => { - it.next(); - out.push(Tok::Close); - } - '"' => { - it.next(); - let start = i + 1; - let mut end = text.len(); - for (j, c) in it.by_ref() { - if c == '"' { - end = j; - break; - } - } - out.push(Tok::Val(Prop::Str(text.get(start..end).unwrap_or("").replace(""", "\"")))); - } - _ => { - let start = i; - let mut end = text.len(); - while let Some(&(j, c)) = it.peek() { - if c.is_whitespace() || matches!(c, ',' | '{' | '}' | '"' | ';' | ':') { - end = j; - break; - } - it.next(); - } - let word = text.get(start..end).unwrap_or(""); - if it.peek().is_some_and(|&(_, c)| c == ':') { - it.next(); - out.push(Tok::Key(word.to_string())); - } else if word.starts_with('*') { - out.push(Tok::Count); - } else if let Ok(i) = word.parse::() { - out.push(Tok::Val(Prop::Int(i))); - } else if let Ok(f) = word.parse::() { - out.push(Tok::Val(Prop::Float(f))); - } else if !word.is_empty() { - out.push(Tok::Val(Prop::Str(word.to_string()))); - } else { - // A stray `:` with no key. - it.next(); - } - } - } - } - out -} - -fn ascii_list(toks: &[Tok], i: &mut usize, depth: usize) -> Result> { - if depth > MAX_DEPTH { - return Err(perr("FBX: nodes nested too deeply")); - } - let mut out = vec![]; - while let Some(t) = toks.get(*i) { - *i += 1; - match t { - Tok::Close if depth > 0 => return Ok(out), - Tok::Key(name) => { - let mut node = FbxNode { name: name.clone(), ..FbxNode::default() }; - loop { - match toks.get(*i) { - Some(Tok::Val(v)) => node.props.push(v.clone()), - Some(Tok::Count | Tok::Comma) => {} - _ => break, - } - *i += 1; - } - if toks.get(*i) == Some(&Tok::Open) { - *i += 1; - node.children = ascii_list(toks, i, depth + 1)?; - } - out.push(node); - } - _ => {} - } - } - Ok(out) -} - -fn read_ascii(text: &str) -> Result<(u32, Vec)> { - let toks = tokenize(text); - let mut i = 0; - let nodes = ascii_list(&toks, &mut i, 0)?; - let version = nodes - .iter() - .find(|n| n.name == "FBXHeaderExtension") - .and_then(|h| h.child("FBXVersion")) - .and_then(|v| v.prop(0)) - .and_then(Prop::as_i64) - .and_then(|v| u32::try_from(v).ok()) - .unwrap_or(0); - Ok((version, nodes)) -} - -// --------------------------------------------------------------------------------------------- -// Scene - -/// `Properties70` entries by name (`P: name, type, label, flags, values…`). -struct Props70<'a>(HashMap<&'a str, &'a FbxNode>); - -impl<'a> Props70<'a> { - fn of(node: &'a FbxNode) -> Props70<'a> { - let mut m = HashMap::new(); - if let Some(p) = node.child("Properties70") { - for e in p.children_named("P") { - if let Some(name) = e.str_at(0) { - m.entry(name).or_insert(e); - } - } - } - Props70(m) - } - fn values(&self, name: &str) -> Vec { - self.0.get(name).map(|e| e.props.iter().skip(4).filter_map(Prop::as_f64).collect()).unwrap_or_default() - } - fn f(&self, name: &str) -> Option { - self.values(name).first().copied().filter(|v| v.is_finite()) - } - fn v3(&self, name: &str, default: [f64; 3]) -> [f64; 3] { - match self.values(name).as_slice() { - [x, y, z, ..] if x.is_finite() && y.is_finite() && z.is_finite() => [*x, *y, *z], - _ => default, - } - } -} - -/// An Euler rotation (degrees) in FBX rotation order (`eEulerXYZ` = 0 rotates about X first). -fn euler(d: [f64; 3], order: i64) -> Mat4 { - let (x, y, z) = (Mat4::rotate_x(d[0]), Mat4::rotate_y(d[1]), Mat4::rotate_z(d[2])); - match order { - 1 => y * z * x, - 2 => x * z * y, - 3 => z * x * y, - 4 => y * x * z, - 5 => x * y * z, - _ => z * y * x, - } -} - -fn translate(t: [f64; 3]) -> Mat4 { - Mat4::translate(vec3(t[0], t[1], t[2])) -} - -/// A Model's local transform: `T · Roff · Rp · Rpre · R · Rpost⁻¹ · Rp⁻¹ · Soff · Sp · S · Sp⁻¹`. -fn model_local(p: &Props70) -> Mat4 { - let order = p.f("RotationOrder").map_or(0, |o| o as i64); - let active = p.f("RotationActive").is_some_and(|a| a != 0.0); - let (pre, post) = - if active { (euler(p.v3("PreRotation", [0.0; 3]), 0), euler(p.v3("PostRotation", [0.0; 3]), 0).transpose()) } else { (Mat4::IDENTITY, Mat4::IDENTITY) }; - let rp = p.v3("RotationPivot", [0.0; 3]); - let sp = p.v3("ScalingPivot", [0.0; 3]); - let s = p.v3("Lcl Scaling", [1.0; 3]); - translate(p.v3("Lcl Translation", [0.0; 3])) - * translate(p.v3("RotationOffset", [0.0; 3])) - * translate(rp) - * pre - * euler(p.v3("Lcl Rotation", [0.0; 3]), order) - * post - * translate([-rp[0], -rp[1], -rp[2]]) - * translate(p.v3("ScalingOffset", [0.0; 3])) - * translate(sp) - * Mat4::scale(vec3(s[0], s[1], s[2])) - * translate([-sp[0], -sp[1], -sp[2]]) -} - -/// The geometric transform of a Model (applies to its geometry, not to its children). -fn geometric(p: &Props70) -> Option { - let t = p.v3("GeometricTranslation", [0.0; 3]); - let r = p.v3("GeometricRotation", [0.0; 3]); - let s = p.v3("GeometricScaling", [1.0; 3]); - if t == [0.0; 3] && r == [0.0; 3] && s == [1.0; 3] { - return None; - } - Some(translate(t) * euler(r, 0) * Mat4::scale(vec3(s[0], s[1], s[2]))) -} - -/// The file's axis system → +Y up, +Z front, +X right. -fn axis_matrix(settings: Option<&FbxNode>) -> Mat4 { - let p = settings.map(Props70::of); - let get = |name: &str, d: f64| p.as_ref().and_then(|p| p.f(name)).unwrap_or(d); - let axis = |a: f64, s: f64| -> Option<[f64; 4]> { - let a = a as i64; - let sign = if s < 0.0 { -1.0 } else { 1.0 }; - let mut v = [0.0; 4]; - *v.get_mut(usize::try_from(a).ok().filter(|&a| a < 3)?)? = sign; - Some(v) - }; - let (Some(right), Some(up), Some(front)) = ( - axis(get("CoordAxis", 0.0), get("CoordAxisSign", 1.0)), - axis(get("UpAxis", 1.0), get("UpAxisSign", 1.0)), - axis(get("FrontAxis", 2.0), get("FrontAxisSign", 1.0)), - ) else { - return Mat4::IDENTITY; - }; - let m = Mat4([right, up, front, [0.0, 0.0, 0.0, 1.0]]); - // Two axes on the same file axis: not a rotation. - let used = |k: usize| right[k] != 0.0 || up[k] != 0.0 || front[k] != 0.0; - if (0..3).all(used) { m } else { Mat4::IDENTITY } -} - -#[derive(Clone, Copy, PartialEq, Eq)] -enum Mapping { - PolygonVertex, - Vertex, - Polygon, - AllSame, -} - -/// A geometry layer element (normals, UVs, material indices). -struct LayerData { - mapping: Mapping, - data: Vec, - index: Option>, - width: usize, -} - -impl LayerData { - fn of(geom: &FbxNode, element: &str, data: &str, index: &str, width: usize) -> Option { - let e = geom.child(element)?; - let mapping = match e.child("MappingInformationType").and_then(|m| m.str_at(0)).unwrap_or("ByPolygonVertex") { - "ByPolygonVertex" => Mapping::PolygonVertex, - "ByVertex" | "ByVertice" | "ByControlPoint" => Mapping::Vertex, - "ByPolygon" => Mapping::Polygon, - "AllSame" => Mapping::AllSame, - _ => return None, - }; - let reference = e.child("ReferenceInformationType").and_then(|m| m.str_at(0)).unwrap_or("Direct"); - let index = (reference != "Direct").then(|| e.child(index).map(FbxNode::ints)).flatten(); - Some(LayerData { mapping, data: e.child(data)?.floats(), index, width }) - } - fn get(&self, pv: usize, v: usize, poly: usize) -> Option<&[f64]> { - let i = match self.mapping { - Mapping::PolygonVertex => pv, - Mapping::Vertex => v, - Mapping::Polygon => poly, - Mapping::AllSame => 0, - }; - let j = match &self.index { - Some(ix) => usize::try_from(*ix.get(i)?).ok()?, - None => i, - }; - let a = j.checked_mul(self.width)?; - self.data.get(a..a.checked_add(self.width)?) - } -} - -fn f32s(s: &[f64]) -> [f32; N] { - let mut a = [0.0f32; N]; - for (o, v) in a.iter_mut().zip(s) { - *o = if v.is_finite() { *v as f32 } else { 0.0 }; - } - a -} - -/// A Mesh geometry with its polygons triangulated; `materials[k]` is the model-level material -/// index of the geometry's material slot `k`. -fn build_mesh(name: &str, geom: &FbxNode, materials: &[usize], with_normal_maps: &dyn Fn(usize) -> bool) -> Option { - let positions: Vec<[f32; 3]> = geom.child("Vertices")?.floats().as_chunks::<3>().0.iter().map(|c| f32s::<3>(c)).collect(); - let pvi = geom.child("PolygonVertexIndex")?.ints(); - let normals = LayerData::of(geom, "LayerElementNormal", "Normals", "NormalsIndex", 3); - let uvs = LayerData::of(geom, "LayerElementUV", "UV", "UVIndex", 2); - let mats = geom.child("LayerElementMaterial").and_then(|e| { - let mapping = e.child("MappingInformationType").and_then(|m| m.str_at(0)).unwrap_or("AllSame"); - let ids = e.child("Materials")?.ints(); - Some((mapping == "ByPolygon", ids)) - }); - - type Key = (u32, [u32; 3], [u32; 2]); - let mut prims: Vec<(Option, Primitive, HashMap, Vec)> = vec![]; - let mut corners: Vec<(usize, usize)> = vec![]; - let mut poly = 0usize; - for (pv, &raw) in pvi.iter().enumerate() { - // The last corner of a polygon is stored as `-index - 1`. - let (v, last) = if raw < 0 { (!raw, true) } else { (raw, false) }; - corners.push((pv, usize::try_from(v).unwrap_or(usize::MAX))); - if !last { - continue; - } - let poly_corners = std::mem::take(&mut corners); - let p_index = poly; - poly += 1; - if poly_corners.len() < 3 || poly_corners.iter().any(|&(_, v)| v >= positions.len()) { - continue; - } - let slot = mats.as_ref().and_then(|(by_poly, ids)| { - let k = if *by_poly { *ids.get(p_index)? } else { *ids.first()? }; - materials.get(usize::try_from(k).ok()?).copied() - }); - let slot = slot.or_else(|| materials.first().copied()); - let at = match prims.iter().position(|p| p.0 == slot) { - Some(i) => i, - None => { - prims.push((slot, Primitive { material: slot, ..Default::default() }, HashMap::new(), vec![])); - prims.len() - 1 - } - }; - let Some((_, prim, map, need)) = prims.get_mut(at) else { continue }; - let mut ids = Vec::with_capacity(poly_corners.len()); - for &(pv, v) in &poly_corners { - let n = normals.as_ref().and_then(|l| l.get(pv, v, p_index)).map(f32s::<3>); - let t = uvs.as_ref().and_then(|l| l.get(pv, v, p_index)).map(f32s::<2>); - let key: Key = (v as u32, n.unwrap_or([f32::NAN; 3]).map(f32::to_bits), t.unwrap_or([f32::NAN; 2]).map(f32::to_bits)); - let id = *map.entry(key).or_insert_with(|| { - prim.positions.push(positions.get(v).copied().unwrap_or_default()); - prim.normals.push(n.unwrap_or_default()); - prim.uvs.push(t.unwrap_or_default()); - need.push(n.is_none()); - (prim.positions.len() - 1) as u32 - }); - ids.push(id); - } - for i in 2..ids.len() { - prim.indices.extend([ids[0], ids[i - 1], ids[i]]); - } - } - let mut mesh = Mesh { name: name.to_string(), primitives: vec![] }; - for (_, mut p, _, need) in prims { - if p.indices.is_empty() { - continue; - } - if need.iter().any(|&b| b) { - let given = p.normals.clone(); - p.compute_normals(); - for ((n, g), need) in p.normals.iter_mut().zip(given).zip(&need) { - if !need { - *n = g; - } - } - } - if uvs.is_none() { - p.uvs.clear(); - } else { - // FBX's v axis points up the image; textures are stored top row first. - for uv in p.uvs.iter_mut() { - uv[1] = 1.0 - uv[1]; - } - } - if p.material.is_some_and(with_normal_maps) { - p.compute_tangents(); - } - mesh.primitives.push(p); - } - (!mesh.primitives.is_empty()).then_some(mesh) -} - -/// Lambert/Phong surface properties onto metallic-roughness. -fn material(name: &str, node: &FbxNode) -> Material { - let p = Props70::of(node); - let phong = node.child("ShadingModel").and_then(|s| s.str_at(0)).is_some_and(|s| s.eq_ignore_ascii_case("phong")); - let factor = p.f("DiffuseFactor").unwrap_or(1.0); - let d = p.v3("DiffuseColor", p.v3("Diffuse", [0.8; 3])); - let alpha = match p.f("Opacity") { - Some(o) => o, - None => { - let tc = p.v3("TransparentColor", [0.0; 3]); - 1.0 - (tc[0] + tc[1] + tc[2]) / 3.0 * p.f("TransparencyFactor").unwrap_or(0.0) - } - } - .clamp(0.0, 1.0); - let lin = |c: f64| srgb_to_linear((c * factor).clamp(0.0, 1.0) as f32); - let e = p.v3("EmissiveColor", [0.0; 3]); - let ef = p.f("EmissiveFactor").unwrap_or(1.0); - let roughness = if phong { - let ns = p.f("ShininessExponent").or_else(|| p.f("Shininess")).unwrap_or(20.0).max(0.0); - (2.0 / (ns + 2.0)).sqrt() as f32 - } else { - 1.0 - }; - Material { - name: name.to_string(), - base_color: [lin(d[0]), lin(d[1]), lin(d[2]), alpha as f32], - metallic: 0.0, - roughness, - emissive: e.map(|c| srgb_to_linear((c * ef).clamp(0.0, 1.0) as f32)), - alpha_mode: if alpha < 0.999 { AlphaMode::Blend } else { AlphaMode::Opaque }, - ..Material::default() - } -} - -/// Decode standard base64 (ASCII FBX embeds `Video` content this way); `None` on bad input. -fn base64(s: &str) -> Option> { - let mut out = Vec::with_capacity(s.len() / 4 * 3); - let (mut acc, mut bits) = (0u32, 0u32); - for c in s.bytes() { - let v = match c { - b'A'..=b'Z' => c - b'A', - b'a'..=b'z' => c - b'a' + 26, - b'0'..=b'9' => c - b'0' + 52, - b'+' => 62, - b'/' => 63, - b'=' => break, - c if c.is_ascii_whitespace() => continue, - _ => return None, - }; - acc = (acc << 6) | v as u32; - bits += 6; - if bits >= 8 { - bits -= 8; - out.push((acc >> bits) as u8); - acc &= (1 << bits) - 1; - } - } - Some(out) -} - -fn short_name(full: &str) -> &str { - full.split_once("::").map_or(full, |(_, n)| n) -} - -/// Parse an FBX file into a model. `resolve(uri)` reads texture files next to it. +/// Parse an FBX file; `resolve(path)` reads texture files the file names but does not embed. pub fn parse(bytes: &[u8], resolve: &dyn Fn(&str) -> Option>) -> Result { - parse_within(bytes, resolve, MAX_IMPORT_BYTES) -} - -/// [`parse`] keeping at most `limit` bytes of arrays and meshes. -fn parse_within(bytes: &[u8], resolve: &dyn Fn(&str) -> Option>, limit: usize) -> Result { - let budget = Budget::new(limit); - let (version, doc) = read_within(bytes, &budget)?; - if version != 0 && version < 7000 { - return Err(ModelError::Unsupported(format!("FBX {}.{} (only FBX 7.x files are read)", version / 1000, version % 1000 / 100))); + let limits = limits(); + let doc = craft_mesh::fbx::read_document(bytes, &limits).map_err(error)?; + let mut textures = |path: &str| resolve(path); + let scene = doc.scene(&limits, &mut textures).map_err(error)?; + if scene.meshes.is_empty() { + return Err(ModelError::Parse("FBX file without meshes".into())); } - let top = |name: &str| doc.iter().find(|n| n.name == name); - let objects = top("Objects").ok_or_else(|| perr("FBX: no Objects section"))?; - let by_id: HashMap = objects.children.iter().filter_map(|o| Some((o.prop(0)?.as_i64()?, o))).collect(); - let kind = |id: i64| by_id.get(&id).map(|n| n.name.as_str()); - - // Connections: (child, parent, property) in file order. - let mut links: Vec<(i64, i64, Option<&str>)> = vec![]; - if let Some(c) = top("Connections") { - for e in c.children_named("C") { - let (Some(a), Some(b)) = (e.prop(1).and_then(Prop::as_i64), e.prop(2).and_then(Prop::as_i64)) else { continue }; - links.push((a, b, e.str_at(3))); - } + let mut m = to_model(scene, MissingNormals::Smooth, &|_| None)?; + // The axes node goes first: every node index moves up by one. + for n in &mut m.nodes { + n.children.iter_mut().for_each(|c| *c += 1); } - - let model_ids: Vec = objects.children.iter().filter(|o| o.name == "Model").filter_map(|o| o.prop(0)?.as_i64()).collect(); - let mut node_of: HashMap = HashMap::new(); - let mut nodes = vec![Node { name: "FBX axes".into(), matrix: Some(axis_matrix(top("GlobalSettings"))), ..Node::default() }]; - for &id in &model_ids { - if node_of.contains_key(&id) { - continue; - } - let Some(o) = by_id.get(&id) else { continue }; - let name = short_name(o.str_at(1).unwrap_or("")).to_string(); - node_of.insert(id, nodes.len()); - nodes.push(Node { name, matrix: Some(model_local(&Props70::of(o))), ..Node::default() }); - } - - // Hierarchy: the first Model→Model link is the parent; anything else hangs off the root. - let mut parent: HashMap = HashMap::new(); - for &(c, p, _) in &links { - if let (Some(&ci), Some(&pi)) = (node_of.get(&c), node_of.get(&p)) - && ci != pi - { - parent.entry(ci).or_insert(pi); - } - } - for i in 1..nodes.len() { - let p = parent.get(&i).copied().unwrap_or(0); - if let Some(n) = nodes.get_mut(p) { - n.children.push(i); - } - } - - // Materials (in first-use order) and their textures. - let mut materials: Vec = vec![]; - let mut textures: Vec> = vec![]; - let mut material_of: HashMap = HashMap::new(); - let mut texture_of: HashMap> = HashMap::new(); - let mut load_texture = |tid: i64, textures: &mut Vec>| -> Option { - if let Some(t) = texture_of.get(&tid) { - return *t; - } - let t = (|| { - let tex = by_id.get(&tid)?; - let embedded = links.iter().filter(|l| l.1 == tid && kind(l.0) == Some("Video")).find_map(|l| { - let content = by_id.get(&l.0)?.child("Content")?; - match content.props.first()? { - Prop::Bytes(b) if !b.is_empty() => Some(b.clone()), - Prop::Str(_) => { - let s: String = content.props.iter().filter_map(Prop::as_str).collect(); - base64(&s).filter(|b| !b.is_empty()) - } - _ => None, - } - }); - let bytes = embedded.or_else(|| { - ["RelativeFilename", "FileName"].iter().find_map(|k| { - let f = tex.child(k)?.str_at(0)?.replace('\\', "/"); - resolve(&f).or_else(|| resolve(f.rsplit('/').next()?)) - }) - })?; - let t = Texture::decode(&bytes).ok()?; - textures.push(Arc::new(t)); - Some(textures.len() - 1) - })(); - texture_of.insert(tid, t); - t - }; - let mut material_index = |mid: i64, materials: &mut Vec, textures: &mut Vec>| -> Option { - if let Some(&m) = material_of.get(&mid) { - return Some(m); - } - let node = by_id.get(&mid)?; - let mut m = material(short_name(node.str_at(1).unwrap_or("")), node); - for &(tid, _, prop) in links.iter().filter(|l| l.1 == mid && kind(l.0) == Some("Texture")) { - let slot = match prop { - Some("DiffuseColor") => &mut m.base_color_tex, - Some("NormalMap") => &mut m.normal_tex, - Some("EmissiveColor") => &mut m.emissive_tex, - _ => continue, - }; - if slot.is_none() { - *slot = load_texture(tid, textures).map(|texture| TexRef { texture, uv_set: 0 }); - } - } - if m.emissive_tex.is_some() && m.emissive == [0.0; 3] { - m.emissive = [1.0; 3]; - } - materials.push(m); - material_of.insert(mid, materials.len() - 1); - Some(materials.len() - 1) - }; - - // One mesh per Model with Mesh geometry (material slots are per Model). Models instancing the - // same geometry with the same materials share one mesh rather than each building a copy. - let mut meshes: Vec = vec![]; - let mut mesh_of: HashMap<(i64, Vec), usize> = HashMap::new(); - for &id in &model_ids { - let Some(&ni) = node_of.get(&id) else { continue }; - let Some(gid) = links.iter().find(|l| l.1 == id && kind(l.0) == Some("Geometry")).map(|l| l.0) else { continue }; - let Some(geom) = by_id.get(&gid) else { continue }; - let mats: Vec = - links.iter().filter(|l| l.1 == id && kind(l.0) == Some("Material")).filter_map(|l| material_index(l.0, &mut materials, &mut textures)).collect(); - let name = nodes.get(ni).map(|n| n.name.clone()).unwrap_or_default(); - let mesh_index = match mesh_of.get(&(gid, mats.clone())) { - Some(&i) => i, - None => { - let normal_mapped: Vec = materials.iter().map(|m| m.normal_tex.is_some()).collect(); - let Some(mesh) = build_mesh(&name, geom, &mats, &|m| normal_mapped.get(m).copied().unwrap_or(false)) else { continue }; - budget.take(mesh_bytes(&mesh), &format!("mesh `{name}`"))?; - meshes.push(mesh); - mesh_of.insert((gid, mats), meshes.len() - 1); - meshes.len() - 1 - } - }; - let geo = by_id.get(&id).and_then(|o| geometric(&Props70::of(o))); - match geo { - Some(matrix) => { - nodes.push(Node { name: format!("{name} geometry"), matrix: Some(matrix), mesh: Some(mesh_index), ..Node::default() }); - let gi = nodes.len() - 1; - if let Some(n) = nodes.get_mut(ni) { - n.children.push(gi); - } - } - None => { - if let Some(n) = nodes.get_mut(ni) { - n.mesh = Some(mesh_index); - } - } - } - } - if meshes.is_empty() { - return Err(perr("FBX file without meshes")); - } - Ok(Model { meshes, nodes, roots: vec![0], materials, textures, ..Model::default() }) + m.skins.iter_mut().flat_map(|s| s.joints.iter_mut()).for_each(|j| *j += 1); + m.animations.iter_mut().flat_map(|a| a.channels.iter_mut()).for_each(|c| c.node += 1); + let roots = m.roots.iter().map(|r| r + 1).collect(); + m.nodes.insert(0, Node { name: "FBX axes".into(), matrix: Some(mat4(&doc.axes_to_y_up())), children: roots, ..Node::default() }); + m.roots = vec![0]; + Ok(m) } #[cfg(test)] diff --git a/crates/model/src/fbx/tests.rs b/crates/model/src/fbx/tests.rs index 1ba1978ba..399b16e2d 100644 --- a/crates/model/src/fbx/tests.rs +++ b/crates/model/src/fbx/tests.rs @@ -1,5 +1,5 @@ use super::*; -use crate::load; +use crate::{AlphaMode, load, vec3}; /// A Z-up scene: a Null parent with a mesh child (rotated, scaled, with a geometric offset) /// holding a quad and a triangle with a material each. @@ -131,116 +131,12 @@ fn check_scene(m: &Model) { assert!(close(lo, [8.0, 2.0, -2.0]) && close(hi, [10.0, 4.0, 0.0]), "{lo:?} {hi:?}"); } -/// A binary FBX of the same node tree (`a:` children become typed arrays). -fn write_binary(nodes: &[FbxNode], version: u32, compress: bool) -> Vec { - fn prop(out: &mut Vec, p: &Prop, compress: bool) { - let array = |out: &mut Vec, t: u8, len: usize, raw: Vec| { - out.push(t); - out.extend((len as u32).to_le_bytes()); - let data = if compress { miniz_oxide::deflate::compress_to_vec_zlib(&raw, 6) } else { raw }; - out.extend((compress as u32).to_le_bytes()); - out.extend((data.len() as u32).to_le_bytes()); - out.extend(data); - }; - match p { - Prop::Int(i) => match i32::try_from(*i) { - Ok(v) => { - out.push(b'I'); - out.extend(v.to_le_bytes()); - } - Err(_) => { - out.push(b'L'); - out.extend(i.to_le_bytes()); - } - }, - Prop::Float(f) => { - out.push(b'D'); - out.extend(f.to_le_bytes()); - } - Prop::Str(s) => { - let s = match s.split_once("::") { - Some((class, name)) => format!("{name}\0\x01{class}"), - None => s.clone(), - }; - out.push(b'S'); - out.extend((s.len() as u32).to_le_bytes()); - out.extend(s.as_bytes()); - } - Prop::Bytes(b) => { - out.push(b'R'); - out.extend((b.len() as u32).to_le_bytes()); - out.extend(b); - } - Prop::Ints(v) => array(out, b'i', v.len(), v.iter().flat_map(|&i| (i as i32).to_le_bytes()).collect()), - Prop::Floats(v) => array(out, b'd', v.len(), v.iter().flat_map(|f| f.to_le_bytes()).collect()), - } - } - fn record(out: &mut Vec, n: &FbxNode, wide: bool, compress: bool) { - let w = if wide { 8 } else { 4 }; - let start = out.len(); - out.extend(vec![0u8; 3 * w]); - out.push(n.name.len() as u8); - out.extend(n.name.as_bytes()); - let props = match n.child("a") { - Some(a) if a.props.iter().all(|p| matches!(p, Prop::Int(_))) => vec![Prop::Ints(a.ints())], - Some(a) => vec![Prop::Floats(a.floats())], - None => n.props.clone(), - }; - let pstart = out.len(); - for p in &props { - prop(out, p, compress); - } - let plen = out.len() - pstart; - let kids: Vec<&FbxNode> = n.children.iter().filter(|c| c.name != "a").collect(); - if !kids.is_empty() { - for k in kids { - record(out, k, wide, compress); - } - out.extend(vec![0u8; 3 * w + 1]); - } - let end = out.len(); - for (k, v) in [end, props.len(), plen].into_iter().enumerate() { - let at = start + k * w; - if wide { - out[at..at + 8].copy_from_slice(&(v as u64).to_le_bytes()); - } else { - out[at..at + 4].copy_from_slice(&(v as u32).to_le_bytes()); - } - } - } - let wide = version >= 7500; - let mut out = BINARY_MAGIC.to_vec(); - out.extend(version.to_le_bytes()); - for n in nodes { - record(&mut out, n, wide, compress); - } - out.extend(vec![0u8; if wide { 25 } else { 13 }]); - // Footer bytes after the closing null record. - out.extend([0xfa; 16]); - out -} - #[test] fn ascii_fbx_scene() { let m = load("scene.fbx", ASCII.as_bytes(), &|_| None).unwrap(); check_scene(&m); } -#[test] -fn binary_fbx_matches_ascii() { - let (_, doc) = read(ASCII.as_bytes()).unwrap(); - let ascii = parse(ASCII.as_bytes(), &|_| None).unwrap(); - for (version, compress) in [(7400, false), (7400, true), (7500, true)] { - let bin = write_binary(&doc, version, compress); - let (v, nodes) = read(&bin).unwrap(); - assert_eq!(v, version); - assert_eq!(nodes.iter().map(|n| n.name.as_str()).collect::>(), ["FBXHeaderExtension", "GlobalSettings", "Objects", "Connections"]); - // Found by its magic even without the extension. - let m = load("scene.bin", &bin, &|_| None).unwrap(); - assert_eq!(m, ascii, "version {version}, compressed {compress}"); - } -} - #[test] fn embedded_and_relative_textures() { let png = { @@ -291,91 +187,6 @@ fn b64(b: &[u8]) -> String { s } -#[test] -fn hostile_fbx_files_fail_cleanly() { - let (_, doc) = read(ASCII.as_bytes()).unwrap(); - let bin = write_binary(&doc, 7500, true); - // Every truncation and every single-byte corruption returns (no panic, no runaway). - for n in 0..bin.len() { - let _ = parse(&bin[..n], &|_| None); - } - for i in 0..bin.len() { - let mut b = bin.clone(); - b[i] ^= 0xff; - let _ = parse(&b, &|_| None); - } - // An array claiming 4 G elements. - let mut huge = BINARY_MAGIC.to_vec(); - huge.extend(7400u32.to_le_bytes()); - let body: Vec = b"d".iter().copied().chain(u32::MAX.to_le_bytes()).chain(0u32.to_le_bytes()).chain(8u32.to_le_bytes()).chain([0u8; 8]).collect(); - let start = huge.len(); - let end = start + 13 + 1 + body.len(); - huge.extend((end as u32).to_le_bytes()); - huge.extend(1u32.to_le_bytes()); - huge.extend((body.len() as u32).to_le_bytes()); - huge.push(1); - huge.push(b'V'); - huge.extend(body); - assert!(read(&huge).is_err()); - // Deep nesting, both encodings. - assert!(read("A: {".repeat(100_000).as_bytes()).is_err()); - // Not an FBX scene at all, an old FBX version, and a scene without meshes. - assert!(parse(b"\x00\x01garbage", &|_| None).is_err()); - let v6 = "FBXHeaderExtension: {\n FBXVersion: 6100\n}\nObjects: {\n}\n"; - assert!(matches!(parse(v6.as_bytes(), &|_| None), Err(ModelError::Unsupported(_)))); - assert!(parse(b"Objects: {\n Model: 1, \"Model::Empty\", \"Null\" {\n }\n}\n", &|_| None).is_err()); -} - -/// A version-7400 binary FBX holding one top-level node `A` with uncompressed arrays of type -/// `t` (`b'b'`, `b'i'`, `b'f'`, `b'd'`, `b'l'`) and `len` elements each. -fn binary_arrays(arrays: &[(u8, usize)]) -> Vec { - let mut props = vec![]; - for &(t, len) in arrays { - let size = match t { - b'd' | b'l' => 8, - b'f' | b'i' => 4, - _ => 1, - }; - props.push(t); - props.extend((len as u32).to_le_bytes()); - props.extend(0u32.to_le_bytes()); - props.extend(((len * size) as u32).to_le_bytes()); - props.extend(std::iter::repeat_n(1u8, len * size)); - } - let mut out = BINARY_MAGIC.to_vec(); - out.extend(7400u32.to_le_bytes()); - let end = out.len() + 13 + 1 + props.len(); - out.extend((end as u32).to_le_bytes()); - out.extend((arrays.len() as u32).to_le_bytes()); - out.extend((props.len() as u32).to_le_bytes()); - out.push(1); - out.push(b'A'); - out.extend(props); - out -} - -#[test] -fn fbx_arrays_are_charged_for_the_memory_they_keep() { - // A boolean array is one byte per element on the wire but eight once kept as `Prop::Ints`: - // the budget counts the eight (#1234). - let n = 10_000; - let bin = binary_arrays(&[(b'b', n)]); - assert!(read_within(&bin, &Budget::new(n * 8)).is_ok()); - let e = read_within(&bin, &Budget::new(n * 8 - 1)).unwrap_err(); - assert!(e.to_string().contains("memory limit"), "{e}"); - // 32-bit floats widen to f64 the same way. - assert!(read_within(&binary_arrays(&[(b'f', n)]), &Budget::new(n * 4)).is_err()); - // Several arrays each within the budget but not together. - let bin = binary_arrays(&[(b'i', n), (b'd', n), (b'b', n)]); - assert!(read_within(&bin, &Budget::new(n * 24)).is_ok()); - assert!(read_within(&bin, &Budget::new(n * 20)).is_err()); - // ASCII values count too. - let (_, doc) = read(ASCII.as_bytes()).unwrap(); - let values = value_bytes(&doc).unwrap(); - assert!(values > 45 * std::mem::size_of::(), "{values}"); - assert!(parse_within(ASCII.as_bytes(), &|_| None, values - 1).unwrap_err().to_string().contains("memory limit")); -} - /// [`ASCII`] with two more Models instancing its geometry: `Copy` with the same two materials, /// `Red only` with just the first. fn instanced_scene() -> String { @@ -401,11 +212,94 @@ fn fbx_models_instancing_one_geometry_share_its_mesh() { assert_eq!(mesh_of("Shape"), mesh_of("Copy"), "same geometry and materials: one mesh"); assert_ne!(mesh_of("Shape"), mesh_of("Red only"), "different materials: its own mesh"); assert_eq!(m.meshes.len(), 2); - // A budget with room for one copy of the geometry's mesh, but not three, still imports. - let one = parse(ASCII.as_bytes(), &|_| None).unwrap(); - let mesh = mesh_bytes(&one.meshes[0]).unwrap(); - let (_, doc) = read(instanced_scene().as_bytes()).unwrap(); - let values = value_bytes(&doc).unwrap(); - assert!(parse_within(instanced_scene().as_bytes(), &|_| None, values + 2 * mesh).is_ok()); - assert!(parse_within(instanced_scene().as_bytes(), &|_| None, values + mesh).is_err()); +} + +#[test] +fn texture_aliases_of_one_image_share_its_pixels() { + let png = { + let img = image::RgbaImage::from_pixel(2, 1, image::Rgba([0, 255, 0, 255])); + let mut b = std::io::Cursor::new(vec![]); + img.write_to(&mut b, image::ImageFormat::Png).unwrap(); + b.into_inner() + }; + let objects = r#" + Texture: 600, "Texture::Base", "" { + RelativeFilename: "maps\base.png" + } + Texture: 700, "Texture::Base again", "" { + RelativeFilename: "maps\base.png" + } +} +Connections: {"#; + let doc = ASCII + .replace("}\nConnections: {", objects) + .replace("C: \"OO\",500,300\n", "C: \"OO\",500,300\n\tC: \"OP\",600,400, \"DiffuseColor\"\n\tC: \"OP\",700,500, \"DiffuseColor\"\n"); + let resolve = |uri: &str| (uri == "maps/base.png").then(|| png.clone()); + let m = parse(doc.as_bytes(), &resolve).unwrap(); + assert_eq!(m.textures.len(), 1); + assert!(m.materials.iter().all(|x| x.base_color_tex.map(|t| t.texture) == Some(0))); + assert!(m.warnings.is_empty()); +} + +/// A PNG that is only a header claiming `w`×`h` pixels (an empty data chunk follows). +fn png_header_only(w: u32, h: u32) -> Vec { + fn chunk(png: &mut Vec, ty: &[u8], data: &[u8]) { + let body = [ty, data].concat(); + let mut crc = 0xFFFF_FFFFu32; + for &b in &body { + crc ^= u32::from(b); + for _ in 0..8 { + crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 }; + } + } + png.extend((data.len() as u32).to_be_bytes()); + png.extend(&body); + png.extend((!crc).to_be_bytes()); + } + let mut png = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A]; + chunk(&mut png, b"IHDR", &[w.to_be_bytes(), h.to_be_bytes(), [8, 6, 0, 0]].concat().iter().copied().chain([0]).collect::>()); + chunk(&mut png, b"IDAT", &[]); + png +} + +#[test] +fn a_texture_past_the_limits_is_left_out_with_a_warning_not_a_failed_import() { + let objects = r#" + Texture: 600, "Texture::Base", "" { + RelativeFilename: "maps\base.png" + } +} +Connections: {"#; + let doc = ASCII.replace("}\nConnections: {", objects).replace("C: \"OO\",500,300\n", "C: \"OO\",500,300\n\tC: \"OP\",600,400, \"DiffuseColor\"\n"); + // 30000×30000 pixels would take ~14 GiB; the header alone is refused, before anything is allocated. + let huge = png_header_only(30000, 30000); + let resolve = |_: &str| Some(huge.clone()); + let m = parse(doc.as_bytes(), &resolve).unwrap(); + assert!(m.textures.is_empty() && m.materials.iter().all(|x| x.base_color_tex.is_none())); + assert!(m.warnings.iter().any(|w| w.contains("30000")), "{:?}", m.warnings); +} + +#[test] +fn texture_files_resolving_to_one_image_share_it() { + let png = { + let img = image::RgbaImage::from_pixel(2, 1, image::Rgba([0, 255, 0, 255])); + let mut b = std::io::Cursor::new(vec![]); + img.write_to(&mut b, image::ImageFormat::Png).unwrap(); + b.into_inner() + }; + let objects = r#" + Texture: 600, "Texture::Base", "" { + RelativeFilename: "maps\base.png" + } + Texture: 700, "Texture::Other spelling", "" { + RelativeFilename: "missing\base.png" + FileName: "maps\base.png" + } +} +Connections: {"#; + let doc = ASCII + .replace("}\nConnections: {", objects) + .replace("C: \"OO\",500,300\n", "C: \"OO\",500,300\n\tC: \"OP\",600,400, \"DiffuseColor\"\n\tC: \"OP\",700,500, \"DiffuseColor\"\n"); + let resolve = |uri: &str| (uri == "maps/base.png").then(|| png.clone()); + assert_eq!(parse(doc.as_bytes(), &resolve).unwrap().textures.len(), 1); } diff --git a/crates/model/src/gltf.rs b/crates/model/src/gltf.rs index c0b736359..68bc86764 100644 --- a/crates/model/src/gltf.rs +++ b/crates/model/src/gltf.rs @@ -1,687 +1,26 @@ -//! glTF 2.0 import, written from the Khronos glTF 2.0 specification. -//! -//! Supports `.gltf` (JSON with external `.bin`/image files or `data:` URIs) and `.glb` (binary -//! container: a JSON chunk and a BIN chunk). Imports the default scene's node hierarchy (TRS or -//! matrix), triangle primitives (`TRIANGLES`, `TRIANGLE_STRIP`, `TRIANGLE_FAN`) with POSITION, -//! NORMAL, TANGENT, TEXCOORD_0, JOINTS_0 and WEIGHTS_0, PBR metallic-roughness materials -//! (base colour, metallic-roughness, normal, occlusion and emissive textures; alpha modes; -//! double-sided; `KHR_materials_emissive_strength`, `KHR_materials_unlit`), embedded or -//! external PNG/JPEG images with sampler wrap modes, skins and animations (step, linear and -//! cubic-spline translation/rotation/scale), perspective and orthographic cameras, and -//! `KHR_lights_punctual` lights (directional, point, spot). Sparse accessors and morph targets -//! are ignored. +//! glTF 2.0 import (`.gltf` with external or embedded buffers, `.glb`), read with `craft-mesh` +//! (`craft_mesh::gltf`): meshes, PBR metallic-roughness materials, the node hierarchy, skins, +//! animations, cameras and `KHR_lights_punctual` lights. External buffers and images are read +//! through the caller's resolver by their percent-decoded URI. Primitives without normals get +//! flat normals (glTF 2.0 §3.7.2.1); normal-mapped ones get tangents from their UVs. -use std::{cell::Cell, sync::Arc}; +use crate::scene::{MissingNormals, error, limits, to_model}; +use crate::{Model, Result}; -use serde_json::Value; +pub use craft_mesh::base64_decode; +pub use craft_mesh::gltf::{split_glb, uri_decode}; -use crate::{ - AlphaMode, Animation, Channel, ChannelPath, Interp, Mat4, Material, Mesh, Model, ModelError, Node, Primitive, Result, Skin, TexRef, Texture, Wrap, perr, -}; - -const GLB_MAGIC: u32 = 0x4654_6C67; -const CHUNK_JSON: u32 = 0x4E4F_534A; -const CHUNK_BIN: u32 = 0x004E_4942; - -/// Split a GLB container into its JSON text and BIN chunk. -pub fn split_glb(b: &[u8]) -> Result<(&[u8], Option<&[u8]>)> { - let u32_at = |o: usize| -> Option { b.get(o..o + 4).map(|s| u32::from_le_bytes([s[0], s[1], s[2], s[3]])) }; - if u32_at(0) != Some(GLB_MAGIC) { - return Err(perr("not a GLB file")); - } - if u32_at(4) != Some(2) { - return Err(ModelError::Unsupported(format!("GLB version {}", u32_at(4).unwrap_or(0)))); - } - let total = (u32_at(8).unwrap_or(0) as usize).min(b.len()); - let mut o = 12; - let mut json = None; - let mut bin = None; - while o + 8 <= total { - let len = u32_at(o).unwrap_or(0) as usize; - let ty = u32_at(o + 4).unwrap_or(0); - let past_end = || perr("GLB chunk runs past the end of the file"); - let end = (o + 8).checked_add(len).ok_or_else(past_end)?; - let data = b.get(o + 8..end).ok_or_else(past_end)?; - match ty { - CHUNK_JSON if json.is_none() => json = Some(data), - CHUNK_BIN if bin.is_none() => bin = Some(data), - _ => {} - } - o = end; - } - Ok((json.ok_or_else(|| perr("GLB without a JSON chunk"))?, bin)) -} - -/// Decode standard base64 (with or without padding; whitespace ignored). -pub fn base64_decode(s: &str) -> Option> { - let mut out = Vec::with_capacity(s.len() * 3 / 4); - let mut acc = 0u32; - let mut bits = 0; - for c in s.bytes() { - let v = match c { - b'A'..=b'Z' => c - b'A', - b'a'..=b'z' => c - b'a' + 26, - b'0'..=b'9' => c - b'0' + 52, - b'+' | b'-' => 62, - b'/' | b'_' => 63, - b'=' => break, - b' ' | b'\n' | b'\r' | b'\t' => continue, - _ => return None, - } as u32; - acc = (acc << 6) | v; - bits += 6; - if bits >= 8 { - bits -= 8; - out.push((acc >> bits) as u8); - acc &= (1 << bits) - 1; - } - } - Some(out) -} - -/// Percent-decode a relative URI. -pub fn uri_decode(s: &str) -> String { - let b = s.as_bytes(); - let mut out = Vec::with_capacity(b.len()); - let mut i = 0; - while i < b.len() { - if b[i] == b'%' - && i + 2 < b.len() - && b[i + 1].is_ascii_hexdigit() - && b[i + 2].is_ascii_hexdigit() - && let Ok(v) = u8::from_str_radix(std::str::from_utf8(&b[i + 1..i + 3]).unwrap_or("zz"), 16) - { - out.push(v); - i += 3; - continue; - } - out.push(b[i]); - i += 1; - } - String::from_utf8_lossy(&out).into_owned() -} - -/// Bytes of a `data:` URI or an external resource. -fn load_uri(uri: &str, resolve: &dyn Fn(&str) -> Option>) -> Result> { - if let Some(rest) = uri.strip_prefix("data:") { - let (meta, data) = rest.split_once(',').ok_or_else(|| perr("malformed data URI"))?; - return if meta.ends_with(";base64") { base64_decode(data).ok_or_else(|| perr("bad base64 in data URI")) } else { Ok(uri_decode(data).into_bytes()) }; - } - let name = uri_decode(uri); - resolve(&name).ok_or(ModelError::Missing(name)) -} - -struct Doc<'a> { - j: &'a Value, - buffers: Vec>, - /// Zero components that buffer-less accessors may still allocate, across the whole file: - /// nothing backs them, so many references to one capped accessor must not multiply (#2139). - zero_left: Cell, - /// Components every accessor read may still allocate, across the whole file: each primitive - /// that references an accessor keeps its own copy, so references must not multiply memory. - read_left: Cell, - /// The budget `read_left` started from, for the error message. - read_cap: usize, -} - -fn arr<'v>(v: &'v Value, k: &str) -> &'v [Value] { - v.get(k).and_then(Value::as_array).map(Vec::as_slice).unwrap_or(&[]) -} -/// A non-negative integer field; one too large for `usize` counts as absent (#1235). -fn idx(v: &Value, k: &str) -> Option { - v.get(k).and_then(Value::as_u64).and_then(|x| usize::try_from(x).ok()) -} -/// Node indices in an array field (out-of-range ones are dropped). -fn indices(v: &Value, k: &str) -> Vec { - arr(v, k).iter().filter_map(Value::as_u64).filter_map(|c| usize::try_from(c).ok()).collect() -} - -/// The most components accessors without a buffer view (all zeros) may have, together: nothing -/// in the file backs their counts, so they are capped instead of checked against a buffer -/// (#1235, #2139). -const MAX_ZERO_COMPONENTS: usize = 1 << 24; - -/// The most components all accessor reads in one file may allocate together (about 1 GiB as -/// `f32`/`u32`). Each primitive keeps its own copy of what it reads, so an accessor referenced -/// N times costs N times; this bounds that by a fixed amount rather than by buffer size, because -/// real assets legitimately share one vertex buffer across many primitives. A 10M-triangle scan -/// (5M vertices with position, normal, UV and tangent, 30M indices) reads about 90M. -const MAX_READ_COMPONENTS: usize = 1 << 28; - -/// Where an accessor's elements lie in its buffer: `count` elements of `elem` bytes, `stride` -/// apart from `base`, all checked to be inside `buf`. -struct Span<'b> { - buf: &'b [u8], - base: usize, - stride: usize, -} -fn num(v: &Value, k: &str, d: f64) -> f64 { - v.get(k).and_then(Value::as_f64).unwrap_or(d) -} -fn nums(v: &Value, k: &str, d: [f64; N]) -> [f64; N] { - let mut out = d; - if let Some(a) = v.get(k).and_then(Value::as_array) { - for (i, x) in a.iter().take(N).enumerate() { - out[i] = x.as_f64().unwrap_or(d[i]); - } - } - out -} - -/// Bytes of component `c` of element `i`, with checked offsets (the span was validated, but -/// nothing derived from the file is indexed unchecked). -fn component(buf: &[u8], base: usize, stride: usize, i: usize, c: usize, csize: usize) -> Result<&[u8]> { - let o = i.checked_mul(stride).and_then(|o| o.checked_add(base)).and_then(|o| o.checked_add(c.checked_mul(csize)?)); - o.and_then(|o| buf.get(o..o.checked_add(csize)?)).ok_or_else(|| perr("accessor reads past its buffer")) -} - -impl Doc<'_> { - /// Charge `total` components to the file's read budget before allocating them. - fn charge(&self, total: usize) -> Result<()> { - let left = self.read_left.get(); - let Some(rest) = left.checked_sub(total) else { - let cap = self.read_cap; - return Err(perr(format!("accessor reads need more than {cap} components in total ({total} requested, {left} left)"))); - }; - self.read_left.set(rest); - Ok(()) - } - - /// Charge `count` items of `per` components each. - fn charge_each(&self, count: usize, per: usize) -> Result<()> { - self.charge(count.checked_mul(per).ok_or_else(|| perr("primitive is too large"))?) - } - - /// The accessor's element count and total components (`count * n`), checked; and its span in - /// the buffer, validated before anything is allocated from the count. `None` for an - /// accessor without a buffer view, whose components are all zero. - fn layout(&self, acc: &Value, n: usize, csize: usize) -> Result<(usize, usize, Option>)> { - let count = match acc.get("count") { - None => 0, - Some(c) => c.as_u64().and_then(|c| usize::try_from(c).ok()).ok_or_else(|| perr("accessor count is not a valid size"))?, - }; - let total = count.checked_mul(n).ok_or_else(|| perr("accessor count is too large"))?; - let Some(bv) = idx(acc, "bufferView") else { - let left = self.zero_left.get(); - let Some(rest) = left.checked_sub(total) else { - return Err(perr(format!("accessors without a bufferView have more than {MAX_ZERO_COMPONENTS} components ({total} requested, {left} left)"))); - }; - self.zero_left.set(rest); - self.charge(total)?; - return Ok((count, total, None)); - }; - let view = arr(self.j, "bufferViews").get(bv).ok_or_else(|| perr(format!("no bufferView {bv}")))?; - let buf = self.buffers.get(idx(view, "buffer").unwrap_or(0)).ok_or_else(|| perr("bufferView buffer out of range"))?; - let past = || perr("accessor reads past its buffer"); - let base = idx(view, "byteOffset").unwrap_or(0).checked_add(idx(acc, "byteOffset").unwrap_or(0)).ok_or_else(past)?; - let elem = csize.checked_mul(n).ok_or_else(past)?; - let stride = idx(view, "byteStride").filter(|s| *s > 0).unwrap_or(elem); - if stride < elem { - return Err(perr(format!("bufferView byteStride {stride} is smaller than its {elem}-byte elements"))); - } - if count > 0 { - let end = (count - 1).checked_mul(stride).and_then(|o| o.checked_add(base)).and_then(|o| o.checked_add(elem)).ok_or_else(past)?; - if end > buf.len() { - return Err(past()); - } - } - self.charge(total)?; - Ok((count, total, Some(Span { buf, base, stride }))) - } - - /// An accessor as `f32` rows of `n` components (normalised integers mapped to 0..1 / -1..1). - fn floats(&self, a: usize) -> Result<(Vec, usize)> { - let acc = arr(self.j, "accessors").get(a).ok_or_else(|| perr(format!("no accessor {a}")))?; - let n = match acc.get("type").and_then(Value::as_str).unwrap_or("") { - "SCALAR" => 1, - "VEC2" => 2, - "VEC3" => 3, - "VEC4" => 4, - "MAT2" => 4, - "MAT3" => 9, - "MAT4" => 16, - t => return Err(perr(format!("accessor type `{t}`"))), - }; - let ct = idx(acc, "componentType").unwrap_or(5126); - let norm = acc.get("normalized").and_then(Value::as_bool).unwrap_or(false); - let csize = match ct { - 5120 | 5121 => 1, - 5122 | 5123 => 2, - 5125 | 5126 => 4, - _ => return Err(perr(format!("componentType {ct}"))), - }; - let (count, total, span) = self.layout(acc, n, csize)?; - let mut out = vec![0.0f32; total]; - let Some(Span { buf, base, stride }) = span else { - // No buffer view: all zeros (sparse substitution is not supported). - return Ok((out, n)); - }; - // Matrices of 1/2-byte components pad columns to 4 bytes; ignored (rare). - for i in 0..count { - for c in 0..n { - let s = component(buf, base, stride, i, c, csize)?; - out[i * n + c] = match ct { - 5126 => f32::from_le_bytes([s[0], s[1], s[2], s[3]]), - 5125 => u32::from_le_bytes([s[0], s[1], s[2], s[3]]) as f32, - 5123 => { - let v = u16::from_le_bytes([s[0], s[1]]) as f32; - if norm { v / 65535.0 } else { v } - } - 5122 => { - let v = i16::from_le_bytes([s[0], s[1]]) as f32; - if norm { (v / 32767.0).max(-1.0) } else { v } - } - 5121 => { - let v = s[0] as f32; - if norm { v / 255.0 } else { v } - } - _ => { - let v = s[0] as i8 as f32; - if norm { (v / 127.0).max(-1.0) } else { v } - } - }; - } - } - Ok((out, n)) - } - - /// Integer accessor (indices, joints) read exactly. - fn ints(&self, a: usize) -> Result> { - let acc = arr(self.j, "accessors").get(a).ok_or_else(|| perr(format!("no accessor {a}")))?; - let ct = idx(acc, "componentType").unwrap_or(5125); - if ct == 5126 { - return Ok(self.floats(a)?.0.into_iter().map(|v| v as u32).collect()); - } - let n = match acc.get("type").and_then(Value::as_str).unwrap_or("SCALAR") { - "VEC4" => 4, - "VEC3" => 3, - "VEC2" => 2, - _ => 1, - }; - let csize = match ct { - 5120 | 5121 => 1, - 5122 | 5123 => 2, - _ => 4, - }; - let (count, total, span) = self.layout(acc, n, csize)?; - let Some(Span { buf, base, stride }) = span else { return Ok(vec![0; total]) }; - let mut out = Vec::with_capacity(total); - for i in 0..count { - for c in 0..n { - let s = component(buf, base, stride, i, c, csize)?; - out.push(match csize { - 1 => s[0] as u32, - 2 => u16::from_le_bytes([s[0], s[1]]) as u32, - _ => u32::from_le_bytes([s[0], s[1], s[2], s[3]]), - }); - } - } - Ok(out) - } -} - -/// Parse a `.gltf` (JSON) or `.glb` file. +/// Parse a `.gltf` (JSON) or `.glb` file; `resolve(uri)` reads external buffers and images. pub fn parse(bytes: &[u8], resolve: &dyn Fn(&str) -> Option>) -> Result { - parse_with_budget(bytes, resolve, MAX_READ_COMPONENTS) -} - -/// [`parse`] with a read budget of `read_budget` accessor components for the whole file. -pub(crate) fn parse_with_budget(bytes: &[u8], resolve: &dyn Fn(&str) -> Option>, read_budget: usize) -> Result { - let (json, bin) = if bytes.starts_with(b"glTF") { split_glb(bytes)? } else { (bytes, None) }; - let j: Value = serde_json::from_slice(json).map_err(|e| perr(format!("glTF JSON: {e}")))?; - let ver = j.get("asset").and_then(|a| a.get("version")).and_then(Value::as_str).unwrap_or(""); - if !ver.starts_with('2') { - return Err(ModelError::Unsupported(format!("glTF version `{ver}` (2.0 required)"))); - } - for ext in arr(&j, "extensionsRequired") { - let e = ext.as_str().unwrap_or(""); - if !matches!(e, "KHR_materials_unlit" | "KHR_materials_emissive_strength") { - return Err(ModelError::Unsupported(format!("required extension {e}"))); - } - } - let mut buffers = vec![]; - for (i, b) in arr(&j, "buffers").iter().enumerate() { - let data = match b.get("uri").and_then(Value::as_str) { - Some(u) => load_uri(u, resolve)?, - None if i == 0 => bin.map(<[u8]>::to_vec).ok_or_else(|| perr("buffer 0 has no uri and the file has no BIN chunk"))?, - None => vec![], - }; - buffers.push(data); - } - let d = Doc { j: &j, buffers, zero_left: Cell::new(MAX_ZERO_COMPONENTS), read_left: Cell::new(read_budget), read_cap: read_budget }; - - // Images → textures (decoded once per image; samplers set the wrap modes). - let mut images: Vec>> = vec![]; - for im in arr(&j, "images") { - let data = if let Some(u) = im.get("uri").and_then(Value::as_str) { - load_uri(u, resolve).ok() - } else if let Some(bv) = idx(im, "bufferView") { - arr(&j, "bufferViews").get(bv).and_then(|v| { - let b = d.buffers.get(idx(v, "buffer").unwrap_or(0))?; - let o = idx(v, "byteOffset").unwrap_or(0); - b.get(o..o.checked_add(idx(v, "byteLength").unwrap_or(0))?).map(<[u8]>::to_vec) - }) - } else { - None - }; - images.push(data.and_then(|b| Texture::decode(&b).ok()).map(Arc::new)); - } - let wrap = |v: u64| match v { - 33071 => Wrap::Clamp, - 33648 => Wrap::Mirror, - _ => Wrap::Repeat, - }; - let mut textures: Vec> = vec![]; - let mut tex_map: Vec> = vec![]; - for t in arr(&j, "textures") { - let Some(img) = idx(t, "source").and_then(|s| images.get(s).cloned().flatten()) else { - tex_map.push(None); - continue; - }; - let samp = idx(t, "sampler").and_then(|s| arr(&j, "samplers").get(s)); - let ws = samp.and_then(|s| s.get("wrapS")).and_then(Value::as_u64).unwrap_or(10497); - let wt = samp.and_then(|s| s.get("wrapT")).and_then(Value::as_u64).unwrap_or(10497); - let w = [wrap(ws), wrap(wt)]; - let tex = if img.wrap == w { img } else { Arc::new(Texture { wrap: w, ..(*img).clone() }) }; - tex_map.push(Some(textures.len())); - textures.push(tex); - } - let tref = |v: Option<&Value>| -> Option { - let v = v?; - let t = tex_map.get(idx(v, "index")?).copied().flatten()?; - Some(TexRef { texture: t, uv_set: idx(v, "texCoord").unwrap_or(0) as u32 }) - }; - - let mut materials = vec![]; - for m in arr(&j, "materials") { - let pbr = m.get("pbrMetallicRoughness").cloned().unwrap_or(Value::Null); - let bc = nums(&pbr, "baseColorFactor", [1.0; 4]); - let ext = m.get("extensions"); - let strength = ext.and_then(|e| e.get("KHR_materials_emissive_strength")).map_or(1.0, |e| num(e, "emissiveStrength", 1.0)); - let em = nums(m, "emissiveFactor", [0.0; 3]); - materials.push(Material { - name: m.get("name").and_then(Value::as_str).unwrap_or("").to_string(), - base_color: bc.map(|v| v as f32), - base_color_tex: tref(pbr.get("baseColorTexture")), - metallic: num(&pbr, "metallicFactor", 1.0) as f32, - roughness: num(&pbr, "roughnessFactor", 1.0) as f32, - metallic_roughness_tex: tref(pbr.get("metallicRoughnessTexture")), - normal_tex: tref(m.get("normalTexture")), - normal_scale: m.get("normalTexture").map_or(1.0, |n| num(n, "scale", 1.0)) as f32, - occlusion_tex: tref(m.get("occlusionTexture")), - occlusion_strength: m.get("occlusionTexture").map_or(1.0, |n| num(n, "strength", 1.0)) as f32, - emissive: [(em[0] * strength) as f32, (em[1] * strength) as f32, (em[2] * strength) as f32], - emissive_tex: tref(m.get("emissiveTexture")), - alpha_mode: match m.get("alphaMode").and_then(Value::as_str) { - Some("MASK") => AlphaMode::Mask, - Some("BLEND") => AlphaMode::Blend, - _ => AlphaMode::Opaque, - }, - alpha_cutoff: num(m, "alphaCutoff", 0.5) as f32, - double_sided: m.get("doubleSided").and_then(Value::as_bool).unwrap_or(false), - unlit: ext.and_then(|e| e.get("KHR_materials_unlit")).is_some(), - }); - } - - let mut meshes = vec![]; - for m in arr(&j, "meshes") { - let mut mesh = Mesh { name: m.get("name").and_then(Value::as_str).unwrap_or("").to_string(), primitives: vec![] }; - for p in arr(m, "primitives") { - if let Some(prim) = primitive(&d, p, &materials)? { - mesh.primitives.push(prim); - } - } - meshes.push(mesh); - } - - let mut nodes = vec![]; - for n in arr(&j, "nodes") { - let matrix = n.get("matrix").and_then(Value::as_array).filter(|a| a.len() == 16).map(|a| { - // Column-major in the file. - let v: Vec = a.iter().map(|x| x.as_f64().unwrap_or(0.0)).collect(); - let mut m = [[0.0; 4]; 4]; - for c in 0..4 { - for r in 0..4 { - m[r][c] = v[c * 4 + r]; - } - } - Mat4(m) - }); - nodes.push(Node { - name: n.get("name").and_then(Value::as_str).unwrap_or("").to_string(), - translation: nums(n, "translation", [0.0; 3]), - rotation: nums(n, "rotation", [0.0, 0.0, 0.0, 1.0]), - scale: nums(n, "scale", [1.0; 3]), - matrix, - mesh: idx(n, "mesh").filter(|&m| m < meshes.len()), - skin: idx(n, "skin"), - camera: idx(n, "camera"), - light: n.get("extensions").and_then(|e| e.get("KHR_lights_punctual")).and_then(|e| idx(e, "light")), - children: indices(n, "children"), - }); - } - - let scene = idx(&j, "scene").unwrap_or(0); - let roots: Vec = match arr(&j, "scenes").get(scene) { - Some(s) => indices(s, "nodes"), - None => { - // No scene: every node that is nobody's child. - let mut child = vec![false; nodes.len()]; - for n in &nodes { - for &c in &n.children { - if c < child.len() { - child[c] = true; - } - } - } - (0..nodes.len()).filter(|&i| !child[i]).collect() - } - }; - - let mut skins = vec![]; - for s in arr(&j, "skins") { - let joints: Vec = indices(s, "joints"); - let inverse_bind = match idx(s, "inverseBindMatrices") { - Some(a) => { - let (f, _) = d.floats(a)?; - f.as_chunks::<16>() - .0 - .iter() - .map(|v| { - let mut m = [[0.0; 4]; 4]; - for c in 0..4 { - for r in 0..4 { - m[r][c] = v[c * 4 + r] as f64; - } - } - Mat4(m) - }) - .collect() - } - None => vec![Mat4::IDENTITY; joints.len()], - }; - skins.push(Skin { joints, inverse_bind }); - } - - let mut animations = vec![]; - for a in arr(&j, "animations") { - let samplers = arr(a, "samplers"); - let mut anim = Animation { name: a.get("name").and_then(Value::as_str).unwrap_or("").to_string(), channels: vec![] }; - for c in arr(a, "channels") { - let Some(t) = c.get("target") else { continue }; - let Some(node) = idx(t, "node") else { continue }; - let path = match t.get("path").and_then(Value::as_str) { - Some("translation") => ChannelPath::Translation, - Some("rotation") => ChannelPath::Rotation, - Some("scale") => ChannelPath::Scale, - _ => continue, - }; - let Some(s) = idx(c, "sampler").and_then(|s| samplers.get(s)) else { continue }; - let interp = match s.get("interpolation").and_then(Value::as_str) { - Some("STEP") => Interp::Step, - Some("CUBICSPLINE") => Interp::CubicSpline, - _ => Interp::Linear, - }; - let (Some(input), Some(output)) = (idx(s, "input"), idx(s, "output")) else { continue }; - let (times, _) = d.floats(input)?; - let (values, _) = d.floats(output)?; - anim.channels.push(Channel { node, path, interp, times, values }); - } - animations.push(anim); - } - - let cameras = arr(&j, "cameras").iter().enumerate().map(|(i, c)| camera(c, i)).collect(); - let lights = j - .get("extensions") - .and_then(|e| e.get("KHR_lights_punctual")) - .map(|e| arr(e, "lights").iter().enumerate().map(|(i, l)| light(l, i)).collect()) - .unwrap_or_default(); - Ok(Model { meshes, nodes, roots, materials, textures, skins, animations, cameras, lights }) -} - -fn name_or(v: &Value, d: String) -> String { - v.get("name").and_then(Value::as_str).filter(|s| !s.is_empty()).map(str::to_string).unwrap_or(d) -} - -/// A glTF camera (glTF 2.0 §5.12): `perspective` {yfov, aspectRatio?, znear} or -/// `orthographic` {xmag, ymag}. -fn camera(c: &Value, i: usize) -> crate::ModelCamera { - let projection = match (c.get("type").and_then(Value::as_str), c.get("orthographic")) { - (Some("orthographic"), Some(o)) => crate::CameraProjection::Orthographic { xmag: num(o, "xmag", 1.0), ymag: num(o, "ymag", 1.0) }, - _ => { - let p = c.get("perspective").cloned().unwrap_or(Value::Null); - crate::CameraProjection::Perspective { - yfov: num(&p, "yfov", 0.8).clamp(1e-3, 3.1), - aspect: p.get("aspectRatio").and_then(Value::as_f64), - znear: num(&p, "znear", 0.01), - } - } - }; - crate::ModelCamera { name: name_or(c, format!("Camera {}", i + 1)), projection } -} - -/// A `KHR_lights_punctual` light: type, color, intensity, range, spot cone angles. -fn light(l: &Value, i: usize) -> crate::ModelLight { - let kind = match l.get("type").and_then(Value::as_str) { - Some("directional") => crate::ModelLightKind::Directional, - Some("spot") => { - let s = l.get("spot").cloned().unwrap_or(Value::Null); - let outer = num(&s, "outerConeAngle", std::f64::consts::FRAC_PI_4).clamp(1e-3, std::f64::consts::FRAC_PI_2); - crate::ModelLightKind::Spot { inner: num(&s, "innerConeAngle", 0.0).clamp(0.0, outer), outer } - } - _ => crate::ModelLightKind::Point, - }; - crate::ModelLight { - name: name_or(l, format!("Light {}", i + 1)), - kind, - color: nums(l, "color", [1.0; 3]), - intensity: num(l, "intensity", 1.0).max(0.0), - range: l.get("range").and_then(Value::as_f64), - } -} - -fn primitive(d: &Doc, p: &Value, materials: &[Material]) -> Result> { - let mode = idx(p, "mode").unwrap_or(4); - if !matches!(mode, 4..=6) { - return Ok(None); - } - let attrs = p.get("attributes").ok_or_else(|| perr("primitive without attributes"))?; - let Some(pa) = idx(attrs, "POSITION") else { return Ok(None) }; - let (pos, _) = d.floats(pa)?; - let positions: Vec<[f32; 3]> = pos.as_chunks::<3>().0.iter().map(|c| [c[0], c[1], c[2]]).collect(); - let n = positions.len(); - let vec3s = |a: Option| -> Result> { - Ok(match a { - Some(a) => d.floats(a)?.0.as_chunks::<3>().0.iter().map(|c| [c[0], c[1], c[2]]).collect(), - None => vec![], - }) - }; - let mut normals = vec3s(idx(attrs, "NORMAL"))?; - let uvs: Vec<[f32; 2]> = match idx(attrs, "TEXCOORD_0") { - Some(a) => d.floats(a)?.0.as_chunks::<2>().0.iter().map(|c| [c[0], c[1]]).collect(), - None => vec![], - }; - let tangents: Vec<[f32; 4]> = match idx(attrs, "TANGENT") { - Some(a) => d.floats(a)?.0.as_chunks::<4>().0.iter().map(|c| [c[0], c[1], c[2], c[3]]).collect(), - None => vec![], - }; - let joints: Vec<[u16; 4]> = match idx(attrs, "JOINTS_0") { - Some(a) => d.ints(a)?.as_chunks::<4>().0.iter().map(|c| [c[0] as u16, c[1] as u16, c[2] as u16, c[3] as u16]).collect(), - None => vec![], - }; - let weights: Vec<[f32; 4]> = match idx(attrs, "WEIGHTS_0") { - Some(a) => d.floats(a)?.0.as_chunks::<4>().0.iter().map(|c| [c[0], c[1], c[2], c[3]]).collect(), - None => vec![], - }; - let raw: Vec = match idx(p, "indices") { - Some(a) => d.ints(a)?, - None => { - // Generated indices are allocated like a read, once per referencing primitive. - d.charge(n)?; - (0..n as u32).collect() - } - }; - if raw.iter().any(|&i| i as usize >= n) { - return Err(perr("index out of range")); - } - let mut indices = Vec::with_capacity(raw.len()); - match mode { - 5 => { - for i in 2..raw.len() { - let (a, b, c) = (raw[i - 2], raw[i - 1], raw[i]); - if i % 2 == 0 { indices.extend([a, b, c]) } else { indices.extend([b, a, c]) } - } - } - 6 => { - for i in 2..raw.len() { - indices.extend([raw[0], raw[i - 1], raw[i]]); - } - } - _ => indices.extend(raw.as_chunks::<3>().0.iter().flatten()), - } - // The triangle list is kept besides the raw indices (a strip or fan of k indices becomes - // 3(k-2)), so it is charged like a read. - d.charge(indices.len())?; - if normals.len() != n { - normals.clear(); - } - let material = idx(p, "material").filter(|&m| m < materials.len()); - let skinned = joints.len() == n && weights.len() == n; - if normals.is_empty() { - // Flat normals unshare every vertex: position and normal, plus UVs and joints/weights. - let per = 6 + if uvs.len() == n { 2 } else { 0 } + if skinned { 8 } else { 0 }; - d.charge_each(indices.len(), per)?; - } - let mut prim = Primitive { - positions, - normals, - uvs: if uvs.len() == n { uvs } else { vec![] }, - tangents: if tangents.len() == n { tangents } else { vec![] }, - joints: if skinned { joints } else { vec![] }, - weights: if skinned { weights } else { vec![] }, - indices, - material, - }; - if prim.normals.is_empty() { - prim.flat_normals(); - prim.tangents.clear(); - } - let needs_tangents = material.is_some_and(|m| materials[m].normal_tex.is_some()); - if needs_tangents && prim.tangents.is_empty() { - d.charge_each(prim.positions.len(), 4)?; - prim.compute_tangents(); - } - Ok(Some(prim)) -} - -#[cfg(test)] -mod tests { - use super::uri_decode; - - #[test] - fn uri_decode_rejects_signed_hex_pair() { - assert_eq!(uri_decode("a%+1b"), "a%+1b"); - assert_eq!(uri_decode("a%-1b"), "a%-1b"); - assert_eq!(uri_decode("a%41b"), "aAb"); - } + let mut buffers = |uri: &str| resolve(uri); + let scene = craft_mesh::gltf::read(bytes, &limits(), &mut buffers).map_err(error)?; + to_model(scene, MissingNormals::Flat, &|im| { + let uri = im.uri.as_deref()?; + match uri.strip_prefix("data:") { + Some(_) => None, + // craft-mesh decodes external image URIs while constructing the scene. Decode only + // once here; a second pass changes literal percent escapes in valid filenames. + None => resolve(uri), + } + }) } diff --git a/crates/model/src/lib.rs b/crates/model/src/lib.rs index 19a0d37f3..0f25570eb 100644 --- a/crates/model/src/lib.rs +++ b/crates/model/src/lib.rs @@ -1,12 +1,14 @@ //! 3D models for the Advanced 3D renderer. //! -//! - [`gltf`]: glTF 2.0 import (`.gltf` with external or embedded buffers, `.glb`), written from -//! the Khronos glTF 2.0 specification: meshes, PBR metallic-roughness materials (base colour, -//! metallic-roughness, normal, occlusion and emissive maps), the node hierarchy, skins and -//! animations. +//! The file formats are read with `craft-mesh` (storytold/craft-libs); this crate maps its +//! scene onto [`Model`], decodes the textures and adds the normals and tangents a file leaves out. +//! +//! - [`gltf`]: glTF 2.0 import (`.gltf` with external or embedded buffers, `.glb`): meshes, PBR +//! metallic-roughness materials (base colour, metallic-roughness, normal, occlusion and +//! emissive maps), the node hierarchy, skins, animations, cameras and lights. //! - [`obj`]: Wavefront OBJ geometry with MTL materials (mapped onto the metallic-roughness model). //! - [`stl`]: STL meshes (binary and ASCII, with the binary per-facet and part colours). -//! - [`fbx`]: FBX 7.x scenes, binary and ASCII: meshes, the Model hierarchy, Lambert/Phong +//! - [`fbx`]: FBX 6.x and 7.x scenes, binary and ASCII: meshes, the Model hierarchy, Lambert/Phong //! materials and their textures. //! - [`prim`]: parametric primitives (cube, sphere, plane, torus, cone, cylinder). //! - [`extrude`]: extruded, bevelled meshes from 2D outlines (text and shape layers), with @@ -22,6 +24,7 @@ pub mod fbx; pub mod gltf; pub mod obj; pub mod prim; +mod scene; pub mod stl; pub mod triangulate; @@ -46,7 +49,7 @@ pub(crate) fn perr(s: impl Into) -> ModelError { } /// Texture coordinate wrapping (glTF sampler wrap modes). -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub enum Wrap { #[default] Repeat, @@ -64,14 +67,40 @@ pub struct Texture { pub wrap: [Wrap; 2], } +/// Most pixels one texture may hold (8192 × 8192: 1 GiB as float RGBA). +pub const MAX_TEXTURE_PIXELS: u64 = 1 << 26; +/// Most pixels the textures of one import may hold together (8 GiB as float RGBA: thirty-two 4K +/// maps). A texture past it is left out and the import says so (`Model::warnings`). +pub const MAX_IMPORT_TEXTURE_PIXELS: u64 = 1 << 29; + impl Texture { pub fn solid(c: [f32; 4]) -> Texture { Texture { width: 1, height: 1, data: vec![c], wrap: [Wrap::Repeat; 2] } } + /// The pixel size an image claims, read from its header without decoding it. + pub fn dimensions(bytes: &[u8]) -> Result<(u32, u32)> { + let reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().map_err(|e| perr(format!("image: {e}")))?; + reader.into_dimensions().map_err(|e| perr(format!("image: {e}"))) + } /// Decode a PNG or JPEG. pub fn decode(bytes: &[u8]) -> Result { - let img = image::load_from_memory(bytes).map_err(|e| perr(format!("image: {e}")))?.to_rgba32f(); + Self::decode_within(bytes, &mut MAX_IMPORT_TEXTURE_PIXELS.clone()) + } + /// [`decode`](Self::decode), of at most [`MAX_TEXTURE_PIXELS`], taking the pixels from `pixels_left`, an import's remaining + /// texture budget, before the image is decoded. + pub fn decode_within(bytes: &[u8], pixels_left: &mut u64) -> Result { + let err = |e: image::ImageError| perr(format!("image: {e}")); + let (w, h) = Self::dimensions(bytes)?; + let pixels = w as u64 * h as u64; + if pixels > MAX_TEXTURE_PIXELS { + return Err(ModelError::Unsupported(format!("a {w}×{h} texture (at most {MAX_TEXTURE_PIXELS} pixels)"))); + } + if pixels > *pixels_left { + return Err(ModelError::Unsupported(format!("a {w}×{h} texture: the model's textures would hold more than {MAX_IMPORT_TEXTURE_PIXELS} pixels"))); + } + let img = image::load_from_memory(bytes).map_err(err)?.to_rgba32f(); let (w, h) = (img.width(), img.height()); + *pixels_left -= pixels; let data = img.pixels().map(|p| p.0).collect(); Ok(Texture { width: w, height: h, data, wrap: [Wrap::Repeat; 2] }) } @@ -201,6 +230,9 @@ impl Primitive { if !self.uvs.is_empty() { p.uvs.push(self.uvs[i]); } + if !self.tangents.is_empty() { + p.tangents.push(self.tangents[i]); + } if !self.joints.is_empty() { p.joints.push(self.joints[i]); p.weights.push(self.weights[i]); @@ -512,6 +544,8 @@ pub struct Model { pub cameras: Vec, /// Punctual lights embedded in the file (glTF `KHR_lights_punctual`). pub lights: Vec, + /// Parts of the file the import left out (textures over the limits), for the user. + pub warnings: Vec, } /// A camera embedded in a model file. @@ -570,8 +604,9 @@ pub struct Instance<'m> { pub mesh: &'m Mesh, /// Node → model space. pub world: Mat4, - /// Skinned meshes: joint matrices (model space, `joint_world × inverse_bind`). - pub joints: Option>, + /// Skinned meshes: joint matrices (model space, `joint_world × inverse_bind`), shared by + /// the instances of one skin. + pub joints: Option>>, } impl Model { @@ -622,6 +657,8 @@ impl Model { let mut seen = vec![false; self.nodes.len()]; let mut stack: Vec = self.roots.clone(); let mut out = vec![]; + // The pose is one for the whole scene, so every instance of a skin shares its matrices. + let mut joints_of: std::collections::HashMap>> = std::collections::HashMap::new(); while let Some(i) = stack.pop() { if i >= self.nodes.len() || seen[i] { continue; @@ -629,12 +666,19 @@ impl Model { seen[i] = true; let n = &self.nodes[i]; if let Some(m) = n.mesh.and_then(|m| self.meshes.get(m)) { - let joints = n.skin.and_then(|s| self.skins.get(s)).map(|s| { - s.joints - .iter() - .enumerate() - .map(|(k, &j)| world.get(j).copied().unwrap_or(Mat4::IDENTITY) * s.inverse_bind.get(k).copied().unwrap_or(Mat4::IDENTITY)) - .collect() + let joints = n.skin.and_then(|si| self.skins.get(si).map(|s| (si, s))).map(|(si, s)| { + joints_of + .entry(si) + .or_insert_with(|| { + Arc::new( + s.joints + .iter() + .enumerate() + .map(|(k, &j)| world.get(j).copied().unwrap_or(Mat4::IDENTITY) * s.inverse_bind.get(k).copied().unwrap_or(Mat4::IDENTITY)) + .collect(), + ) + }) + .clone() }); // Skinned vertices are placed by their joints alone (glTF: the node transform of // a skinned mesh is ignored). diff --git a/crates/model/src/obj.rs b/crates/model/src/obj.rs index 22d9877a2..c305cf23f 100644 --- a/crates/model/src/obj.rs +++ b/crates/model/src/obj.rs @@ -1,33 +1,21 @@ -//! Wavefront OBJ geometry and MTL materials. +//! Wavefront OBJ geometry and MTL materials, read with `craft-mesh` (`craft_mesh::obj`). //! -//! OBJ: `v` (optional vertex colours ignored), `vt`, `vn`, `f` with `v`, `v/vt`, `v//vn`, -//! `v/vt/vn` references (negative = relative), polygons fan-triangulated, `o`/`g` groups, `s` -//! smoothing (normals computed per smoothing state when a face has none), `usemtl`, `mtllib`. -//! -//! MTL materials map onto metallic-roughness: `Kd` → base colour, `d`/`Tr` → alpha, `Ke` → -//! emissive, `Pr`/`Pm` (the PBR extension) → roughness/metallic, otherwise roughness from the -//! Phong exponent `Ns` (`sqrt(2 / (Ns + 2))`) and metallic 0; `map_Kd`, `map_Ke`, -//! `map_Bump`/`bump`/`norm` load base colour, emissive and normal textures. +//! The material library and the textures it names are read through the caller's resolver: a +//! texture is looked up by the name as written (a name containing spaces is one name), then by +//! its file name (an absolute path from another machine, #1274), then by the final token (the +//! options convention, `-bm 1 file.png`). Faces without normals get smooth normals within a +//! smoothing group and flat ones where smoothing is off. -use std::collections::HashMap; -use std::sync::Arc; +use crate::scene::{MissingNormals, error, limits, to_model}; +use crate::{Model, Result}; -use crate::{AlphaMode, Material, Mesh, Model, Primitive, Result, TexRef, Texture, perr}; +pub use craft_mesh::srgb_to_linear; -/// The last component of a referenced path, treating `\` as a separator too: a path from a -/// machine of another kind (`C:\textures\checker.png`) still finds `checker.png` next to the -/// model (#1274). `None` when the name carries no path at all. -fn sibling_name(name: &str) -> Option<&str> { - let file = name.rsplit(['/', '\\']).next()?; - (!file.is_empty() && file.len() < name.len()).then_some(file) -} - -/// A material reference resolved against the model's folder: the name as written (a name -/// containing spaces is one name), then its file name (an absolute path from another machine, -/// #1274), then the final token (the options convention, `-bm 1 file.png`). +/// A material reference resolved against the model's folder: the name as written, then its file +/// name, then the final token. fn resolve_reference(resolve: &dyn Fn(&str) -> Option>, name: &str) -> Option> { let mut tried: Vec<&str> = vec![]; - for candidate in [name, sibling_name(name).unwrap_or(name), name.split_whitespace().last().unwrap_or(name)] { + for candidate in [name, craft_mesh::obj::sibling_name(name).unwrap_or(name), name.split_whitespace().last().unwrap_or(name)] { if tried.contains(&candidate) { continue; } @@ -39,263 +27,11 @@ fn resolve_reference(resolve: &dyn Fn(&str) -> Option>, name: &str) -> O None } -/// Parse MTL text into named materials (textures appended to `textures`). -pub fn parse_mtl(text: &str, resolve: &dyn Fn(&str) -> Option>, textures: &mut Vec>) -> Vec { - let mut out: Vec = vec![]; - let mut loaded: HashMap> = HashMap::new(); - let mut tex = |name: &str, textures: &mut Vec>| -> Option { - let key = name.trim().to_string(); - let t = *loaded.entry(key.clone()).or_insert_with(|| { - let Some(b) = resolve_reference(resolve, &key) else { - log::warn!("model: texture `{key}` was not found next to the model"); - return None; - }; - match Texture::decode(&b) { - Ok(t) => { - textures.push(Arc::new(t)); - Some(textures.len() - 1) - } - Err(e) => { - log::warn!("model: cannot decode texture `{key}`: {e}"); - None - } - } - }); - t.map(|texture| TexRef { texture, uv_set: 0 }) - }; - let mut ns_seen = false; - let mut pr_seen = false; - for line in text.lines() { - let line = line.trim(); - let (key, rest) = line.split_once(char::is_whitespace).map(|(k, r)| (k, r.trim())).unwrap_or((line, "")); - let f3 = || -> [f32; 3] { - let v: Vec = rest.split_whitespace().filter_map(|x| x.parse().ok()).collect(); - match v.len() { - 0 => [0.0; 3], - 1 | 2 => [v[0]; 3], - _ => [v[0], v[1], v[2]], - } - }; - let f1 = || rest.split_whitespace().next().and_then(|x| x.parse::().ok()); - if key == "newmtl" { - if let Some(m) = out.last_mut() { - finish(m, ns_seen, pr_seen); - } - ns_seen = false; - pr_seen = false; - out.push(Material { name: rest.to_string(), metallic: 0.0, roughness: 0.5, ..Material::default() }); - continue; - } - let Some(m) = out.last_mut() else { continue }; - match key { - "Kd" => { - let c = f3(); - m.base_color = [srgb_to_linear(c[0]), srgb_to_linear(c[1]), srgb_to_linear(c[2]), m.base_color[3]]; - } - "Ke" => m.emissive = f3().map(srgb_to_linear), - "d" => m.base_color[3] = f1().unwrap_or(1.0), - "Tr" => m.base_color[3] = 1.0 - f1().unwrap_or(0.0), - "Ns" => { - let ns = f1().unwrap_or(0.0).max(0.0); - if !pr_seen { - m.roughness = (2.0 / (ns + 2.0)).sqrt(); - } - ns_seen = true; - } - "Pr" => { - m.roughness = f1().unwrap_or(0.5).clamp(0.0, 1.0); - pr_seen = true; - } - "Pm" => m.metallic = f1().unwrap_or(0.0).clamp(0.0, 1.0), - "map_Kd" => m.base_color_tex = tex(rest, textures), - "map_Ke" => { - m.emissive_tex = tex(rest, textures); - if m.emissive == [0.0; 3] { - m.emissive = [1.0; 3]; - } - } - "map_Bump" | "map_bump" | "bump" | "norm" => m.normal_tex = tex(rest, textures), - _ => {} - } - } - if let Some(m) = out.last_mut() { - finish(m, ns_seen, pr_seen); - } - out -} - -fn finish(m: &mut Material, _ns: bool, _pr: bool) { - if m.base_color[3] < 0.999 { - m.alpha_mode = AlphaMode::Blend; - } -} - -/// sRGB-encoded component → linear (MTL colours are display colours). -pub fn srgb_to_linear(c: f32) -> f32 { - if c <= 0.04045 { c / 12.92 } else { ((c + 0.055) / 1.055).powf(2.4) } -} - -#[derive(Clone, Copy, PartialEq, Eq, Hash)] -struct Key { - v: u32, - t: u32, - n: u32, - /// Smoothing state (faces without normals get computed normals per state). - s: u32, -} - -/// Parse OBJ text (and its MTL libraries through `resolve`). +/// Parse OBJ text (and its MTL libraries and textures through `resolve`). pub fn parse(bytes: &[u8], resolve: &dyn Fn(&str) -> Option>) -> Result { - let text = String::from_utf8_lossy(bytes); - let mut v: Vec<[f32; 3]> = vec![]; - let mut vt: Vec<[f32; 2]> = vec![]; - let mut vn: Vec<[f32; 3]> = vec![]; - let mut materials: Vec = vec![]; - let mut textures: Vec> = vec![]; - // One primitive per material, in order of first use. - let mut prims: Vec<(Option, Primitive, HashMap, Vec)> = vec![]; - let mut current: Option = None; - let mut smooth = 0u32; - let mut flat_id = 1u32 << 31; - let mut faces = 0usize; - // Join `\` line continuations. - let joined = text.replace("\\\r\n", " ").replace("\\\n", " "); - for (ln, line) in joined.lines().enumerate() { - let line = line.split('#').next().unwrap_or("").trim(); - if line.is_empty() { - continue; - } - let mut it = line.split_whitespace(); - let key = it.next().unwrap_or(""); - let rest: Vec<&str> = it.collect(); - let fl = |i: usize| rest.get(i).and_then(|x| x.parse::().ok()); - match key { - "v" => v.push([fl(0).unwrap_or(0.0), fl(1).unwrap_or(0.0), fl(2).unwrap_or(0.0)]), - "vt" => vt.push([fl(0).unwrap_or(0.0), fl(1).unwrap_or(0.0)]), - "vn" => vn.push([fl(0).unwrap_or(0.0), fl(1).unwrap_or(0.0), fl(2).unwrap_or(1.0)]), - "s" => { - smooth = match rest.first().copied() { - None | Some("off") | Some("0") => 0, - Some(x) => x.parse().unwrap_or(1).max(1), - } - } - "mtllib" => { - let name = line["mtllib".len()..].trim(); - let mut found = false; - for file in std::iter::once(name).chain(name.split_whitespace()).chain(sibling_name(name)) { - if let Some(b) = resolve(file) { - let mut ms = parse_mtl(&String::from_utf8_lossy(&b), resolve, &mut textures); - materials.append(&mut ms); - found = true; - break; - } - } - if !found && !name.is_empty() { - log::warn!("model: material library `{name}` was not found next to the model"); - } - } - "usemtl" => { - let name = line["usemtl".len()..].trim(); - current = materials.iter().position(|m| m.name == name); - if current.is_none() { - // Unknown material: a named grey default. - materials.push(Material { name: name.to_string(), metallic: 0.0, roughness: 0.5, base_color: [0.8, 0.8, 0.8, 1.0], ..Material::default() }); - current = Some(materials.len() - 1); - } - } - "f" => { - if rest.len() < 3 { - continue; - } - faces += 1; - let resolve_i = |s: &str, len: usize| -> Option { - if s.is_empty() { - return None; - } - let i: i64 = s.parse().ok()?; - let k = if i < 0 { len as i64 + i } else { i - 1 }; - (k >= 0 && (k as usize) < len).then_some(k as u32) - }; - let mut refs = vec![]; - for r in &rest { - let mut parts = r.split('/'); - let vi = resolve_i(parts.next().unwrap_or(""), v.len()).ok_or_else(|| perr(format!("line {}: bad vertex index `{r}`", ln + 1)))?; - let ti = parts.next().and_then(|s| resolve_i(s, vt.len())); - let ni = parts.next().and_then(|s| resolve_i(s, vn.len())); - refs.push((vi, ti, ni)); - } - let has_n = refs.iter().all(|r| r.2.is_some()); - // Faces without normals: shared within a smoothing group, unique otherwise. - let s = if has_n { - 0 - } else if smooth == 0 { - flat_id += 1; - flat_id - } else { - smooth - }; - let slot = match prims.iter().position(|p| p.0 == current) { - Some(i) => i, - None => { - prims.push((current, Primitive { material: current, ..Default::default() }, HashMap::new(), vec![])); - prims.len() - 1 - } - }; - let (_, prim, map, need) = &mut prims[slot]; - let mut ids = vec![]; - for (vi, ti, ni) in refs { - let k = Key { v: vi, t: ti.unwrap_or(u32::MAX), n: ni.unwrap_or(u32::MAX), s }; - let id = *map.entry(k).or_insert_with(|| { - prim.positions.push(v[vi as usize]); - prim.uvs.push(ti.map_or([0.0, 0.0], |t| vt[t as usize])); - prim.normals.push(ni.map_or([0.0; 3], |n| vn[n as usize])); - need.push(ni.is_none()); - (prim.positions.len() - 1) as u32 - }); - ids.push(id); - } - for i in 2..ids.len() { - prim.indices.extend([ids[0], ids[i - 1], ids[i]]); - } - } - _ => {} - } - } - if faces == 0 { - return Err(perr("OBJ file without faces")); - } - let any_uv = !vt.is_empty(); - let mut mesh = Mesh { name: String::new(), primitives: vec![] }; - for (_, mut p, _, need) in prims { - if need.iter().any(|&b| b) { - // Fill missing normals from the triangles (area weighted), keep given ones. - let given = p.normals.clone(); - p.compute_normals(); - for (i, n) in p.normals.iter_mut().enumerate() { - if !need[i] { - *n = given[i]; - } - } - } - if !any_uv { - p.uvs.clear(); - } else { - // OBJ's v axis points up the image; textures are stored top row first. - for uv in p.uvs.iter_mut() { - uv[1] = 1.0 - uv[1]; - } - } - if p.material.is_some_and(|m| materials[m].normal_tex.is_some()) { - p.compute_tangents(); - } - mesh.primitives.push(p); - } - Ok(Model::single(mesh, materials).with_textures(textures)) -} - -impl Model { - fn with_textures(mut self, t: Vec>) -> Model { - self.textures = t; - self - } + // `craft-mesh` splits `mtllib` names and tries each complete library path itself. Applying + // texture-option fallback here first can mistake the last library for the whole directive. + let mut library = |name: &str| resolve(name); + let scene = craft_mesh::obj::read(bytes, &limits(), &mut library).map_err(error)?; + to_model(scene, MissingNormals::BySmoothing, &|im| resolve_reference(resolve, im.uri.as_deref()?.trim())) } diff --git a/crates/model/src/scene.rs b/crates/model/src/scene.rs new file mode 100644 index 000000000..07fee0397 --- /dev/null +++ b/crates/model/src/scene.rs @@ -0,0 +1,459 @@ +//! A `craft-mesh` scene (glTF, OBJ, STL or FBX, read there) as a [`Model`]: one primitive per +//! part with one index per vertex, decoded textures, and the normals and tangents the renderer +//! needs where the file has none. + +use std::collections::HashMap; +use std::sync::Arc; + +use craft_mesh as cm; + +use crate::{ + AlphaMode, Animation, CameraProjection, Channel, ChannelPath, Interp, MAX_IMPORT_TEXTURE_PIXELS, Mat4, Material, Mesh, Model, ModelCamera, ModelError, + ModelLight, ModelLightKind, Node, Primitive, Result, Skin, TexRef, Texture, Wrap, +}; + +/// The bounds every reader keeps: `craft-mesh`'s defaults, with up to 2 GiB of buffers, +/// arrays and embedded images in one import. +pub(crate) fn limits() -> cm::Limits { + cm::Limits { max_data_bytes: 2 << 30, ..cm::Limits::default() } +} + +pub(crate) fn error(e: cm::Error) -> ModelError { + match e { + cm::Error::Missing(name) => ModelError::Missing(name), + cm::Error::Unsupported(m) => ModelError::Unsupported(m), + cm::Error::Malformed(m) | cm::Error::TooLarge(m) => ModelError::Parse(m), + } +} + +/// What a primitive without normals gets. +#[derive(Clone, Copy, PartialEq, Eq)] +pub(crate) enum MissingNormals { + /// Every triangle its own vertices and face normal (glTF 2.0 §3.7.2.1). + Flat, + /// Area-weighted vertex normals (FBX). + Smooth, + /// Smooth within an OBJ smoothing group, flat where smoothing is off. + BySmoothing, +} + +/// A column-major `craft-mesh` matrix as a [`Mat4`]. +pub(crate) fn mat4(m: &cm::Mat4) -> Mat4 { + let mut out = [[0.0; 4]; 4]; + for (c, col) in m.as_chunks::<4>().0.iter().enumerate() { + for (r, v) in col.iter().enumerate() { + out[r][c] = *v; + } + } + Mat4(out) +} + +fn wrap(w: cm::Wrap) -> Wrap { + match w { + cm::Wrap::Repeat => Wrap::Repeat, + cm::Wrap::Clamp => Wrap::Clamp, + cm::Wrap::Mirror => Wrap::Mirror, + } +} + +/// The scene as a model. `image` gives the bytes of an image the scene names but does not hold +/// (OBJ, which only names its textures); an image that cannot be read or decoded leaves its +/// material untextured, with a log line. +pub(crate) fn to_model(mut scene: cm::Scene, missing: MissingNormals, image: &dyn Fn(&cm::Image) -> Option>) -> Result { + let mut warnings = std::mem::take(&mut scene.warnings); + for w in &warnings { + log::warn!("model: {w}"); + } + // Images decode once; each (image, wrap) a material uses is one texture. + let mut pixels_left = MAX_IMPORT_TEXTURE_PIXELS; + let mut decoded_by_name: HashMap>> = HashMap::new(); + let decoded: Vec>> = scene + .images + .iter() + .enumerate() + .map(|(index, im)| { + let key = image_key(im, index); + if let Some(decoded) = decoded_by_name.get(&key) { + return decoded.clone(); + } + let external; + let bytes = if let Some(data) = im.data.as_deref() { + Some(data) + } else { + external = image(im); + external.as_deref() + }; + let decoded = match bytes.map(|b| Texture::decode_within(b, &mut pixels_left)) { + Some(Ok(t)) => Some(Arc::new(t)), + Some(Err(e)) => { + let warning = format!("texture `{}` was left out: {e}", im.uri.as_deref().unwrap_or(&im.name)); + log::warn!("model: {warning}"); + warnings.push(warning); + None + } + None => { + let warning = format!("texture `{}` was not found next to the model", im.uri.as_deref().unwrap_or(&im.name)); + log::warn!("model: {warning}"); + warnings.push(warning); + None + } + }; + decoded_by_name.insert(key, decoded.clone()); + decoded + }) + .collect(); + let mut textures: Vec> = vec![]; + let mut texture_of: HashMap<(String, [Wrap; 2]), usize> = HashMap::new(); + for texture in &scene.texture_variants { + ensure_texture(&scene, &decoded, *texture, &mut textures, &mut texture_of, &mut pixels_left, &mut warnings); + } + let mut tref = |t: Option| -> Option { + let t = t?; + let texture = ensure_texture(&scene, &decoded, t, &mut textures, &mut texture_of, &mut pixels_left, &mut warnings)?; + Some(TexRef { texture, uv_set: t.uv_set }) + }; + let materials: Vec = scene + .materials + .iter() + .map(|m| Material { + name: m.name.clone(), + base_color: m.base_color, + base_color_tex: tref(m.base_color_texture), + metallic: m.metallic, + roughness: m.roughness, + metallic_roughness_tex: tref(m.metallic_roughness_texture), + normal_tex: tref(m.normal_texture), + normal_scale: m.normal_scale, + occlusion_tex: tref(m.occlusion_texture), + occlusion_strength: m.occlusion_strength, + emissive: m.emissive, + emissive_tex: tref(m.emissive_texture), + alpha_mode: match m.alpha_mode { + cm::AlphaMode::Opaque => AlphaMode::Opaque, + cm::AlphaMode::Mask(_) => AlphaMode::Mask, + cm::AlphaMode::Blend => AlphaMode::Blend, + }, + alpha_cutoff: if let cm::AlphaMode::Mask(c) = m.alpha_mode { c } else { 0.5 }, + double_sided: m.double_sided, + unlit: m.unlit, + }) + .collect(); + let mut adapter_bytes = 0u64; + let meshes = scene.meshes.iter().map(|m| mesh(m, &materials, missing, &mut adapter_bytes)).collect::>>()?; + let nodes = scene + .nodes + .iter() + .map(|n| { + let (translation, rotation, scale, matrix) = match n.trs { + Some(t) => (t.translation, t.rotation, t.scale, None), + None => ([0.0; 3], [0.0, 0.0, 0.0, 1.0], [1.0; 3], Some(mat4(&n.transform))), + }; + Node { + name: n.name.clone(), + translation, + rotation, + scale, + matrix, + mesh: n.mesh.filter(|&m| m < scene.meshes.len()), + skin: n.skin, + camera: n.camera, + light: n.light, + children: n.children.clone(), + } + }) + .collect(); + let skins = scene + .skins + .iter() + .map(|s| Skin { + joints: s.joints.clone(), + inverse_bind: if s.inverse_bind.is_empty() { vec![Mat4::IDENTITY; s.joints.len()] } else { s.inverse_bind.iter().map(mat4).collect() }, + }) + .collect(); + let animations = scene + .animations + .iter() + .map(|a| Animation { + name: a.name.clone(), + channels: a + .channels + .iter() + .map(|c| Channel { + node: c.node, + path: match c.path { + cm::ChannelPath::Translation => ChannelPath::Translation, + cm::ChannelPath::Rotation => ChannelPath::Rotation, + cm::ChannelPath::Scale => ChannelPath::Scale, + }, + interp: match c.interpolation { + cm::Interpolation::Step => Interp::Step, + cm::Interpolation::Linear => Interp::Linear, + cm::Interpolation::CubicSpline => Interp::CubicSpline, + }, + times: c.times.clone(), + values: c.values.clone(), + }) + .collect(), + }) + .collect(); + let name_or = |n: &str, d: String| if n.is_empty() { d } else { n.to_string() }; + let cameras = scene + .cameras + .iter() + .enumerate() + .map(|(i, c)| ModelCamera { + name: name_or(&c.name, format!("Camera {}", i + 1)), + projection: match c.projection { + cm::Projection::Perspective { yfov, aspect, znear, .. } => CameraProjection::Perspective { yfov: yfov.clamp(1e-3, 3.1), aspect, znear }, + cm::Projection::Orthographic { xmag, ymag, .. } => CameraProjection::Orthographic { xmag, ymag }, + }, + }) + .collect(); + let lights = scene + .lights + .iter() + .enumerate() + .map(|(i, l)| ModelLight { + name: name_or(&l.name, format!("Light {}", i + 1)), + kind: match l.kind { + cm::LightKind::Directional => ModelLightKind::Directional, + cm::LightKind::Point => ModelLightKind::Point, + cm::LightKind::Spot { inner, outer } => { + let outer = outer.clamp(1e-3, std::f64::consts::FRAC_PI_2); + ModelLightKind::Spot { inner: inner.clamp(0.0, outer), outer } + } + }, + color: l.color, + intensity: l.intensity.max(0.0), + range: l.range, + }) + .collect(); + Ok(Model { meshes, nodes, roots: scene.roots, materials, textures, skins, animations, cameras, lights, warnings }) +} + +fn image_key(image: &cm::Image, index: usize) -> String { + image.uri.as_deref().or_else(|| (!image.name.is_empty()).then_some(image.name.as_str())).map(str::to_string).unwrap_or_else(|| format!("image:{index}")) +} + +fn ensure_texture( + scene: &cm::Scene, + decoded: &[Option>], + reference: cm::TextureRef, + textures: &mut Vec>, + texture_of: &mut HashMap<(String, [Wrap; 2]), usize>, + pixels_left: &mut u64, + warnings: &mut Vec, +) -> Option { + let image = decoded.get(reference.image)?.as_ref()?; + let wrap_mode = reference.wrap.map(wrap); + let key = (image_key(scene.images.get(reference.image)?, reference.image), wrap_mode); + if let Some(&index) = texture_of.get(&key) { + return Some(index); + } + if image.wrap != wrap_mode { + let cost = (image.width as u64).checked_mul(image.height as u64); + let Some(cost) = cost.filter(|&cost| cost <= *pixels_left) else { + let source = scene.images.get(reference.image)?; + let warning = format!("texture `{}` wrap variant exceeds the model texture pixel limit", source.uri.as_deref().unwrap_or(&source.name)); + log::warn!("model: {warning}"); + warnings.push(warning); + return None; + }; + *pixels_left -= cost; + textures.push(Arc::new(Texture { wrap: wrap_mode, ..(**image).clone() })); + } else { + textures.push(image.clone()); + } + let index = textures.len() - 1; + texture_of.insert(key, index); + Some(index) +} + +/// One primitive per part, each with one vertex per distinct corner of its triangles. Normals +/// and UVs are kept for a part when every corner of it has them; a triangle naming a vertex +/// that is not there is left out. +fn mesh(m: &cm::Mesh, materials: &[Material], missing: MissingNormals, adapter_bytes: &mut u64) -> Result { + let skinned = !m.joints.is_empty() && m.joints.len() == m.positions.len() && m.weights.len() == m.positions.len(); + let uv_set = m.uv_sets.first(); + // Include temporary hash keys, smoothing maps and the peak duplicate produced by + // flat-normal expansion. + // Account against the whole scene before building any primitive arrays. + let projected = m + .parts + .iter() + .try_fold(0u64, |sum, part| { + let has_missing = part.corners.iter().any(|c| (c.normal as usize) >= m.normals.len()); + let flat = missing == MissingNormals::Flat || (missing == MissingNormals::BySmoothing && part.smoothing == 0); + let vertices = if flat && has_missing { (part.triangle_count() as u64).checked_mul(3)? } else { part.corners.len() as u64 }; + let bytes_per_vertex = if missing == MissingNormals::Flat { 64 } else { 128 }; + sum.checked_add(vertices.checked_mul(bytes_per_vertex)?) + }) + .ok_or_else(|| ModelError::Parse("expanded mesh size overflow".into()))?; + *adapter_bytes = adapter_bytes.checked_add(projected).ok_or_else(|| ModelError::Parse("expanded mesh size overflow".into()))?; + if *adapter_bytes > (2u64 << 30) { + return Err(ModelError::Parse("expanded mesh data exceeds the 2 GiB import limit".into())); + } + let smooth = smooth_normals(m, missing); + let mut primitives = vec![]; + for part in &m.parts { + let normals = part.corners.iter().any(|c| (c.normal as usize) < m.normals.len()); + let tangents = part.has_tangents && m.tangents.len() == m.positions.len(); + let uvs = uv_set.filter(|set| part.corners.iter().any(|c| (c.uv as usize) < set.len())); + let flat_missing = missing == MissingNormals::BySmoothing && part.smoothing == 0; + let mut authored_normals = vec![]; + let mut ids: HashMap<(cm::Corner, Option), u32> = HashMap::new(); + let mut p = Primitive { material: part.material.filter(|&i| i < materials.len()), ..Primitive::default() }; + for (face, t) in part.triangles().enumerate() { + if t.iter().any(|c| c.position as usize >= m.positions.len()) { + continue; + } + let flat_face_normal = (normals && flat_missing).then(|| face_normal(m, t)); + for c in t { + let has_normal = (c.normal as usize) < m.normals.len(); + let has_uv = uvs.is_some_and(|set| (c.uv as usize) < set.len()); + let key = ( + cm::Corner { position: c.position, normal: if has_normal { c.normal } else { cm::NONE }, uv: if has_uv { c.uv } else { cm::NONE } }, + (!has_normal && flat_face_normal.is_some()).then_some(face), + ); + let next = p.positions.len() as u32; + let id = *ids.entry(key).or_insert_with(|| { + let at = c.position as usize; + p.positions.push(m.positions.get(at).copied().unwrap_or_default().map(|x| x as f32)); + if normals { + let authored = m.normals.get(c.normal as usize).copied().map(|n| n.map(|x| x as f32)); + let fallback = flat_face_normal.or_else(|| smooth.get(&(c.position, part.material, smooth_group(part, missing))).copied()); + authored_normals.push(authored.or(fallback)); + p.normals.push(authored.or(fallback).unwrap_or([0.0, 0.0, 1.0])); + } else if let Some(n) = smooth.get(&(c.position, part.material, smooth_group(part, missing))) { + p.normals.push(*n); + } + if tangents { + p.tangents.push(m.tangents.get(at).copied().unwrap_or_default().map(|x| x as f32)); + } + if let Some(set) = uvs { + p.uvs.push(set.get(c.uv as usize).copied().unwrap_or_default()); + } + if skinned { + p.joints.push(m.joints.get(at).copied().unwrap_or_default()); + p.weights.push(m.weights.get(at).copied().unwrap_or_default()); + } + next + }); + p.indices.push(id); + } + } + if p.indices.is_empty() { + continue; + } + if normals && authored_normals.iter().any(Option::is_none) { + // Keep authored corner normals, and fill only absent ones from the surrounding + // triangles. The generated normals are smooth in a mixed-authorship part. + if authored_normals.iter().any(Option::is_none) { + p.compute_normals(); + for (n, authored) in p.normals.iter_mut().zip(authored_normals) { + if let Some(authored) = authored { + *n = authored; + } + } + } + } else if p.normals.is_empty() { + match missing { + MissingNormals::Flat => p.flat_normals(), + MissingNormals::BySmoothing if part.smoothing == 0 => p.flat_normals(), + MissingNormals::Smooth | MissingNormals::BySmoothing => p.compute_normals(), + } + } + if p.tangents.is_empty() && p.material.is_some_and(|i| materials.get(i).is_some_and(|mat| mat.normal_tex.is_some())) { + p.compute_tangents(); + } + primitives.push(p); + } + Ok(Mesh { name: m.name.clone(), primitives }) +} + +fn smooth_group(part: &cm::Part, missing: MissingNormals) -> u32 { + match missing { + MissingNormals::Smooth => 1, + MissingNormals::BySmoothing => part.smoothing, + MissingNormals::Flat => 0, + } +} + +fn face_normal(m: &cm::Mesh, tri: [cm::Corner; 3]) -> [f32; 3] { + let [a, b, c] = tri.map(|corner| m.positions.get(corner.position as usize).copied().unwrap_or_default()); + let u = [b[0] - a[0], b[1] - a[1], b[2] - a[2]]; + let v = [c[0] - a[0], c[1] - a[1], c[2] - a[2]]; + let n = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]]; + let len = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt(); + if len > 0.0 { n.map(|x| (x / len) as f32) } else { [0.0, 0.0, 1.0] } +} + +/// Area weighted generated normals across parts with the same material and smoothing state. +fn smooth_normals(m: &cm::Mesh, missing: MissingNormals) -> HashMap<(u32, Option, u32), [f32; 3]> { + let mut sums: HashMap<(u32, Option, u32), [f64; 3]> = HashMap::new(); + if missing == MissingNormals::Flat { + return HashMap::new(); + } + for part in &m.parts { + let group = smooth_group(part, missing); + if group == 0 { + continue; + } + for tri in part.triangles() { + let Some(a) = m.positions.get(tri[0].position as usize) else { continue }; + let Some(b) = m.positions.get(tri[1].position as usize) else { continue }; + let Some(c) = m.positions.get(tri[2].position as usize) else { continue }; + let u = [b[0] - a[0], b[1] - a[1], b[2] - a[2]]; + let v = [c[0] - a[0], c[1] - a[1], c[2] - a[2]]; + let face = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]]; + for corner in tri { + if (corner.normal as usize) < m.normals.len() { + continue; + } + let n = sums.entry((corner.position, part.material, group)).or_default(); + for i in 0..3 { + n[i] += face[i]; + } + } + } + } + sums.into_iter() + .map(|(key, n)| { + let len = (n[0] * n[0] + n[1] * n[1] + n[2] * n[2]).sqrt(); + let n = if len > 0.0 { n.map(|x| (x / len) as f32) } else { [0.0, 0.0, 1.0] }; + (key, n) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn wrap_variants_share_and_charge_the_import_pixel_budget() { + let scene = cm::Scene { images: vec![cm::Image { name: "texture.png".into(), ..cm::Image::default() }], ..cm::Scene::default() }; + let texture = Arc::new(Texture { width: 2, height: 2, data: vec![[1.0; 4]; 4], wrap: [Wrap::Repeat; 2] }); + let decoded = vec![Some(texture)]; + let mut textures = vec![]; + let mut texture_of = HashMap::new(); + // The decoded image has already taken four pixels from this budget. + let mut pixels_left = 4; + let mut warnings = vec![]; + let clamped = cm::TextureRef { image: 0, uv_set: 0, wrap: [cm::Wrap::Clamp; 2] }; + + let first = ensure_texture(&scene, &decoded, clamped, &mut textures, &mut texture_of, &mut pixels_left, &mut warnings).unwrap(); + let shared = ensure_texture(&scene, &decoded, clamped, &mut textures, &mut texture_of, &mut pixels_left, &mut warnings).unwrap(); + assert_eq!(first, shared); + assert_eq!(pixels_left, 0); + + let repeated = cm::TextureRef { wrap: [cm::Wrap::Repeat; 2], ..clamped }; + assert!(ensure_texture(&scene, &decoded, repeated, &mut textures, &mut texture_of, &mut pixels_left, &mut warnings).is_some()); + assert_eq!(pixels_left, 0); + + let mirrored = cm::TextureRef { wrap: [cm::Wrap::Mirror; 2], ..clamped }; + assert!(ensure_texture(&scene, &decoded, mirrored, &mut textures, &mut texture_of, &mut pixels_left, &mut warnings).is_none()); + assert_eq!(textures.len(), 2); + assert_eq!(warnings.len(), 1); + } +} diff --git a/crates/model/src/stl.rs b/crates/model/src/stl.rs index 34e9a7f06..4fb8ee084 100644 --- a/crates/model/src/stl.rs +++ b/crates/model/src/stl.rs @@ -1,201 +1,23 @@ -//! STL (stereolithography) meshes, binary and ASCII. +//! STL (stereolithography) meshes, binary and ASCII, read with `craft-mesh` +//! (`craft_mesh::stl`): every facet keeps its own three vertices and its face normal, and binary +//! colour extensions become one plain material per colour. //! -//! Written from the public description of the format (3D Systems' StereoLithography Interface -//! Specification, 1989, and its widely documented colour extensions): -//! -//! - Binary: an 80-byte header, a little-endian `u32` facet count, then 50 bytes per facet: a -//! normal and three vertices (12 `f32`) and a 16-bit attribute word. Bytes after the last -//! facet are ignored. -//! - ASCII: `solid name`, then `facet normal nx ny nz` / `outer loop` / `vertex x y z`… / -//! `endloop` / `endfacet`, closed by `endsolid`; several solids may follow each other. -//! Loops with more than three vertices are fan-triangulated. -//! - Colour (binary only): with `COLOR=r g b a` (four bytes) in the header (Materialise Magics), -//! that is the part's colour and a facet whose attribute has bit 15 clear has its own colour -//! (red in bits 0–4, green 5–9, blue 10–14); otherwise (VisCAM / SolidView) a facet with bit 15 -//! set has its own colour (blue in bits 0–4, green 5–9, red 10–14). Each colour becomes a -//! plain dielectric material; uncoloured files get none (the renderer's default material). -//! -//! STL has no units, no UVs and no shared vertices: every facet keeps its own three vertices -//! with the face normal from its counter-clockwise winding (the stored normal is only used when -//! the triangle is degenerate), which gives the faceted look of an STL part. STL files are -//! Z-up (3D printing and CAD); they are turned to the +Y-up convention of [`crate::Model`] -//! (`(x, y, z)` → `(x, z, −y)`, a proper rotation, so winding is kept). Facets with non-finite -//! coordinates are skipped. +//! STL files are Z-up (3D printing and CAD); they are turned to the +Y-up convention of +//! [`crate::Model`] (`(x, y, z)` → `(x, z, −y)`, a proper rotation, so winding is kept). -use std::collections::HashMap; - -use crate::{Material, Mesh, Model, Primitive, Result, cross, normalize, obj::srgb_to_linear, perr, sub}; - -/// Header (80) plus facet count (4). -const HEADER: usize = 84; -/// Bytes per binary facet. -const FACET: usize = 50; - -/// Parse an STL file (binary or ASCII, detected from the contents). -pub fn parse(bytes: &[u8]) -> Result { - let count = bytes.get(80..HEADER).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(u32::from_le_bytes); - let needed = count.and_then(|n| (n as usize).checked_mul(FACET)).and_then(|n| n.checked_add(HEADER)); - // A binary file's size is exact; many binary headers still begin with `solid`, so the size - // decides first, then the ASCII keyword. - if needed.is_some_and(|n| n == bytes.len()) { - return binary(bytes); - } - if looks_ascii(bytes) { - return ascii(bytes); - } - if needed.is_some_and(|n| n <= bytes.len()) { - return binary(bytes); - } - Err(perr("not an STL file (truncated binary facets or no `solid` keyword)")) -} - -fn looks_ascii(bytes: &[u8]) -> bool { - let head = bytes.get(..bytes.len().min(512)).unwrap_or(&[]); - let text = String::from_utf8_lossy(head); - let t = text.trim_start(); - t.get(..5).is_some_and(|k| k.eq_ignore_ascii_case("solid")) && (t.contains("facet") || t.contains("endsolid")) -} +use crate::scene::{MissingNormals, error, limits, to_model}; +use crate::{Model, Result}; /// The Z-up → Y-up turn. -fn y_up(v: [f32; 3]) -> [f32; 3] { +fn y_up(v: [f64; 3]) -> [f64; 3] { [v[0], v[2], -v[1]] } -/// Facets grouped into primitives by colour, in order of first use. -#[derive(Default)] -struct Builder { - /// (colour key, primitive); `None` = uncoloured. - prims: Vec<(Option<[u8; 3]>, Primitive)>, - /// Colour → index into `prims` (up to 32 768 colours, so not a linear search). - slots: HashMap, usize>, - facets: usize, -} - -impl Builder { - /// Add one triangle (file coordinates); `stored` is the file's facet normal. - fn tri(&mut self, color: Option<[u8; 3]>, stored: [f32; 3], v: [[f32; 3]; 3]) { - if v.iter().flatten().any(|c| !c.is_finite()) { - return; - } - let v = v.map(y_up); - let n = normalize(cross(sub(v[1], v[0]), sub(v[2], v[0]))) - .or_else(|| stored.iter().all(|c| c.is_finite()).then(|| normalize(y_up(stored))).flatten()) - .unwrap_or([0.0, 1.0, 0.0]); - let slot = *self.slots.entry(color).or_insert_with(|| { - self.prims.push((color, Primitive::default())); - self.prims.len() - 1 - }); - let Some((_, p)) = self.prims.get_mut(slot) else { return }; - for c in v { - p.indices.push(p.positions.len() as u32); - p.positions.push(c); - p.normals.push(n); - } - self.facets += 1; - } - - fn finish(self, name: String) -> Result { - if self.facets == 0 { - return Err(perr("STL file without facets")); - } - let mut materials = vec![]; - let mut mesh = Mesh { name, primitives: vec![] }; - for (color, mut p) in self.prims { - if let Some(c) = color { - let lin = c.map(|x| srgb_to_linear(x as f32 / 255.0)); - let name = format!("#{:02X}{:02X}{:02X}", c[0], c[1], c[2]); - materials.push(Material { name, ..Material::plain([lin[0], lin[1], lin[2], 1.0], 0.0, 0.5) }); - p.material = Some(materials.len() - 1); - } - mesh.primitives.push(p); - } - // Uncoloured facets next to coloured ones: a neutral grey of their own. - if !materials.is_empty() { - for p in mesh.primitives.iter_mut().filter(|p| p.material.is_none()) { - materials.push(Material { name: "STL".into(), ..Material::plain([0.6, 0.6, 0.6, 1.0], 0.0, 0.5) }); - p.material = Some(materials.len() - 1); - } - } - Ok(Model::single(mesh, materials)) +/// Parse an STL file (binary or ASCII, detected from the contents). +pub fn parse(bytes: &[u8]) -> Result { + let mut scene = craft_mesh::stl::read(bytes, &limits()).map_err(error)?; + for m in &mut scene.meshes { + m.positions.iter_mut().chain(m.normals.iter_mut()).for_each(|v| *v = y_up(*v)); } -} - -fn f32_at(b: &[u8], at: usize) -> f32 { - b.get(at..at + 4).and_then(|s| <[u8; 4]>::try_from(s).ok()).map_or(f32::NAN, f32::from_le_bytes) -} - -/// A 5-bit channel expanded to 8 bits. -fn five(x: u16) -> u8 { - let x = (x & 31) as u8; - (x << 3) | (x >> 2) -} - -/// The Magics `COLOR=` part colour in the header (RGB; alpha is ignored). -fn header_color(header: &[u8]) -> Option<[u8; 3]> { - let at = header.windows(6).position(|w| w == b"COLOR=")?; - let c = header.get(at + 6..at + 10)?; - Some([*c.first()?, *c.get(1)?, *c.get(2)?]) -} - -fn binary(bytes: &[u8]) -> Result { - let header = bytes.get(..80).ok_or_else(|| perr("STL header truncated"))?; - let magics = header_color(header); - let body = bytes.get(HEADER..).unwrap_or(&[]); - let count = bytes.get(80..HEADER).and_then(|b| <[u8; 4]>::try_from(b).ok()).map_or(0, u32::from_le_bytes) as usize; - let mut b = Builder::default(); - for f in body.as_chunks::().0.iter().take(count) { - let n = [f32_at(f, 0), f32_at(f, 4), f32_at(f, 8)]; - let v = [0usize, 1, 2].map(|k| [f32_at(f, 12 + 12 * k), f32_at(f, 16 + 12 * k), f32_at(f, 20 + 12 * k)]); - let attr = f.get(48..50).and_then(|a| <[u8; 2]>::try_from(a).ok()).map_or(0, u16::from_le_bytes); - let color = match magics { - Some(part) => Some(if attr & 0x8000 == 0 { [five(attr), five(attr >> 5), five(attr >> 10)] } else { part }), - None => (attr & 0x8000 != 0).then(|| [five(attr >> 10), five(attr >> 5), five(attr)]), - }; - b.tri(color, n, v); - } - let text = String::from_utf8_lossy(header); - let name = text.trim_matches(|c: char| c == '\0' || c.is_whitespace()); - b.finish(name.strip_prefix("solid").unwrap_or(name).trim().to_string()) -} - -/// Three numbers from the rest of an ASCII line. -fn three<'a>(it: &mut impl Iterator) -> Option<[f32; 3]> { - Some([it.next()?.parse().ok()?, it.next()?.parse().ok()?, it.next()?.parse().ok()?]) -} - -fn ascii(bytes: &[u8]) -> Result { - let text = String::from_utf8_lossy(bytes); - let mut b = Builder::default(); - let mut name: Option = None; - let mut normal = [0.0f32; 3]; - let mut loop_v: Vec<[f32; 3]> = vec![]; - for (ln, line) in text.lines().enumerate() { - let line = line.trim(); - let mut it = line.split_whitespace(); - let Some(key) = it.next() else { continue }; - let bad = || perr(format!("STL line {}: expected three numbers", ln + 1)); - match key.to_ascii_lowercase().as_str() { - "solid" if name.is_none() => { - name = Some(line.get(5..).unwrap_or("").trim().to_string()); - } - "facet" => { - // `facet normal nx ny nz` - normal = it.next().and_then(|_| three(&mut it)).unwrap_or([0.0; 3]); - loop_v.clear(); - } - "vertex" => loop_v.push(three(&mut it).ok_or_else(bad)?), - "endloop" | "endfacet" => { - if let Some((&first, rest)) = loop_v.split_first() { - for w in rest.windows(2) { - if let [a, c] = w { - b.tri(None, normal, [first, *a, *c]); - } - } - } - loop_v.clear(); - } - _ => {} - } - } - b.finish(name.unwrap_or_default()) + to_model(scene, MissingNormals::Flat, &|_| None) } diff --git a/crates/model/src/tests.rs b/crates/model/src/tests.rs index 5ee6a6950..8c6a347fb 100644 --- a/crates/model/src/tests.rs +++ b/crates/model/src/tests.rs @@ -119,6 +119,22 @@ fn obj_without_normals_gets_them() { assert!(obj::parse(b"v 0 0 0\n", &|_| None).is_err()); } +#[test] +fn obj_mixed_authored_normals_keep_flat_missing_corners() { + let src = b"v 0 0 0\nv 1 0 0\nv 0 1 0\nv 0 0 1\nvn 0 1 0\ns off\nf 1//1 2 3\nf 1 3 4\n"; + let m = obj::parse(src, &|_| None).unwrap(); + let corners: Vec<_> = m.meshes[0] + .primitives + .iter() + .flat_map(|p| p.positions.iter().zip(&p.normals)) + .filter(|(position, _)| **position == [0.0, 1.0, 0.0]) + .map(|(_, normal)| *normal) + .collect(); + assert!(corners.contains(&[0.0, 0.0, 1.0]), "{corners:?}"); + assert!(corners.contains(&[1.0, 0.0, 0.0]), "{corners:?}"); + assert!(m.meshes[0].primitives.iter().any(|p| p.normals.contains(&[0.0, 1.0, 0.0]))); +} + #[test] fn obj_textures_from_another_machine_find_the_file_next_to_the_model() { // No committed media: the material file and its textures are written by the test itself. @@ -432,6 +448,58 @@ fn contours_group_into_outlines_by_nesting() { assert_eq!(g.iter().map(|o| o.holes.len()).sum::(), 2); } +#[test] +fn overlapping_contours_keep_every_container_in_their_depth() { + let rect = |x0: f64, y0: f64, x1: f64, y1: f64| vec![[x0, y0], [x1, y0], [x1, y1], [x0, y1]]; + // C's first vertex lies inside both A and B, which overlap without nesting: C is two deep, + // an outer contour, not a hole cut out of B. + let g = group_contours(vec![rect(0.0, 0.0, 10.0, 10.0), rect(5.0, -2.0, 13.0, 8.0), rect(9.0, 1.0, 12.0, 2.0)]); + assert_eq!(g.len(), 3); + assert_eq!(g.iter().map(|o| o.holes.len()).sum::(), 0); +} + +#[test] +fn a_contour_inside_a_hole_takes_the_innermost_outline_not_the_hole() { + let rect = |x0: f64, y0: f64, x1: f64, y1: f64| vec![[x0, y0], [x1, y0], [x1, y1], [x0, y1]]; + // A and B overlap; C lies in A only (a hole of A); D lies in A, B and C (three deep, a hole). + // The smallest container of D is the hole C, which has no outline of its own: D goes to B. + let g = group_contours(vec![rect(0.0, 0.0, 10.0, 10.0), rect(5.0, -2.0, 13.0, 8.0), rect(1.0, 1.0, 9.0, 9.0), rect(6.0, 2.0, 7.0, 3.0)]); + assert_eq!(g.len(), 2); + assert_eq!(g.iter().map(|o| o.holes.len()).collect::>(), [1, 1], "C is a hole of A, D a hole of B (not dropped)"); +} + +#[test] +fn a_hole_whose_outline_is_deeper_than_it_is_still_attached() { + let rect = |x0: f64, y0: f64, x1: f64, y1: f64| vec![[x0, y0], [x1, y0], [x1, y1], [x0, y1]]; + // D is three deep and its innermost outline A four deep: A's slot must exist before D is attached. + let a = rect(0.0, 0.0, 10.0, 10.0); + let g = group_contours(vec![ + a.clone(), + rect(-1.0, -1.0, 20.0, 20.0), + rect(-2.0, -2.0, 25.0, 25.0), + rect(5.0, 5.0, 6.0, 6.0), + rect(-100.0, -100.0, 3.0, 3.0), + rect(-0.5, -1000.0, 0.5, 1000.0), + ]); + let o = g.iter().find(|o| o.outer == a).expect("A is an outline"); + assert_eq!(o.holes.len(), 1, "D is a hole of A"); +} + +#[test] +fn gltf_images_naming_one_file_share_their_wrap_variants() { + let png = { + let img = image::RgbaImage::from_fn(2, 2, |x, y| if (x + y) % 2 == 0 { image::Rgba([255, 0, 0, 255]) } else { image::Rgba([0, 0, 255, 255]) }); + let mut out = std::io::Cursor::new(Vec::new()); + img.write_to(&mut out, image::ImageFormat::Png).unwrap(); + out.into_inner() + }; + // Two image entries with one URI share a decode; two textures clamp it, so they share one copy. + let doc = br#"{"asset":{"version":"2.0"},"images":[{"uri":"t.png"},{"uri":"t.png"}], + "samplers":[{"wrapS":33071,"wrapT":33071}],"textures":[{"source":0,"sampler":0},{"source":1,"sampler":0}]}"#; + let m = gltf::parse(doc, &|u| (u == "t.png").then(|| png.clone())).unwrap(); + assert_eq!(m.textures.len(), 1, "one clamped copy for both textures"); +} + #[test] fn gltf_cameras_and_punctual_lights() { let doc = r#"{ @@ -597,9 +665,9 @@ fn gltf_accessor_counts_are_checked_against_the_buffer_before_allocating() { let view = r#"{"buffer": 0, "byteLength": 36}"#; let cases = [ // A count far beyond the 36-byte buffer. - (r#"{"bufferView": 0, "componentType": 5126, "type": "VEC3", "count": 4000000000}"#, view, "past its buffer"), + (r#"{"bufferView": 0, "componentType": 5126, "type": "VEC3", "count": 4000000000}"#, view, "limit allows"), // count * components overflows. - (r#"{"bufferView": 0, "componentType": 5126, "type": "MAT4", "count": 9223372036854775807}"#, view, "too large"), + (r#"{"bufferView": 0, "componentType": 5126, "type": "MAT4", "count": 9223372036854775807}"#, view, "limit allows"), // Offsets that overflow when added. ( r#"{"bufferView": 0, "componentType": 5126, "type": "VEC3", "count": 3, "byteOffset": 16}"#, @@ -607,11 +675,11 @@ fn gltf_accessor_counts_are_checked_against_the_buffer_before_allocating() { "past its buffer", ), // A stride that overflows with the count. - (r#"{"bufferView": 0, "componentType": 5126, "type": "VEC3", "count": 3000000000}"#, r#"{"buffer": 0, "byteStride": 252}"#, "past its buffer"), + (r#"{"bufferView": 0, "componentType": 5126, "type": "VEC3", "count": 3000000000}"#, r#"{"buffer": 0, "byteStride": 252}"#, "limit allows"), // A stride smaller than an element would re-read the same bytes for a huge count. (r#"{"bufferView": 0, "componentType": 5126, "type": "MAT4", "count": 30}"#, r#"{"buffer": 0, "byteStride": 1}"#, "smaller than"), // No buffer view: nothing backs the count, so it is capped. - (r#"{"componentType": 5126, "type": "VEC3", "count": 1000000000}"#, view, "without a bufferView"), + (r#"{"componentType": 5126, "type": "VEC3", "count": 1000000000}"#, view, "limit allows"), ]; let t0 = std::time::Instant::now(); for (acc, v, want) in cases { @@ -669,65 +737,112 @@ fn gltf_issue_2139_huge_mat4_count_is_an_error() { } #[test] -fn gltf_buffer_less_accessors_share_one_zero_budget() { - // Each buffer-less accessor is capped, but nothing backs any of them, so referencing a capped - // one from many primitives must not multiply the allocation (#2139). +fn gltf_reused_buffer_less_accessor_is_read_once() { + // The shared reader parses a repeated accessor only once, so referencing it from several + // primitives does not multiply its one bounded allocation. let acc = r#"{"componentType": 5126, "type": "VEC3", "count": 5000000}"#; let prims = [r#"{"attributes": {"POSITION": 0}}"#; 4].join(", "); let doc = gltf_doc(acc, r#"{"buffer": 0}"#, 3).replace(r#"[{"attributes": {"POSITION": 0}}]"#, &format!("[{prims}]")); assert_eq!(doc.matches(r#""POSITION": 0"#).count(), 4); - let e = crate::gltf::parse(doc.as_bytes(), &|_| None).err().unwrap_or_else(|| panic!("imported")); - assert!(e.to_string().contains("without a bufferView"), "{e}"); - // One such accessor read once still imports. - let doc = gltf_doc(acc, r#"{"buffer": 0}"#, 3); - assert!(crate::gltf::parse(doc.as_bytes(), &|_| None).is_ok()); -} - -/// `refs` primitives sharing one buffer-backed `verts`-vertex POSITION accessor, without indices. -fn gltf_shared_positions(verts: usize, refs: usize) -> String { - let acc = format!(r#"{{"bufferView": 0, "componentType": 5126, "type": "VEC3", "count": {verts}}}"#); - let prims = vec![r#"{"attributes": {"POSITION": 0}}"#; refs].join(", "); - gltf_doc(&acc, r#"{"buffer": 0}"#, verts * 12).replace(r#"[{"attributes": {"POSITION": 0}}]"#, &format!("[{prims}]")) + let result = crate::gltf::parse(doc.as_bytes(), &|_| None); + assert!(result.is_ok(), "{result:?}"); } #[test] -fn gltf_buffer_backed_reads_share_one_read_budget() { - // A buffer-backed accessor is checked against its buffer, but each primitive that references - // it reads and keeps its own copy, so a small file listing many references must not ask for - // memory set by the reference count (#2139 review). Everything a primitive keeps is charged - // to one per-file budget: 1,000 vertices without indices or normals cost 3,000 position - // components, 1,000 generated indices, a 999-index triangle list and 999 flat-shaded - // vertices of 6 components, 10,993 in all. - let one = gltf_shared_positions(1000, 1); - let m = crate::gltf::parse_with_budget(one.as_bytes(), &|_| None, 20_000).unwrap(); - assert_eq!(m.meshes[0].primitives.len(), 1); - let three = gltf_shared_positions(1000, 3); - assert_eq!(three.matches(r#""POSITION": 0"#).count(), 3); - let e = crate::gltf::parse_with_budget(three.as_bytes(), &|_| None, 20_000).err().unwrap_or_else(|| panic!("imported")); - assert!(e.to_string().contains("more than 20000 components"), "{e}"); - // The real cap leaves room for legitimate sharing: many primitives over one vertex buffer. - let m = crate::gltf::parse(gltf_shared_positions(1000, 50).as_bytes(), &|_| None).unwrap(); - assert_eq!(m.meshes[0].primitives.len(), 50); +fn texture_decoding_is_charged_to_the_import_budget() { + let png = { + let img = image::RgbaImage::from_pixel(2, 2, image::Rgba([0, 255, 0, 255])); + let mut b = std::io::Cursor::new(vec![]); + img.write_to(&mut b, image::ImageFormat::Png).unwrap(); + b.into_inner() + }; + let mut left = 4; + assert_eq!(Texture::decode_within(&png, &mut left).unwrap().data.len(), 4); + assert_eq!(left, 0); + assert!(matches!(Texture::decode_within(&png, &mut left), Err(ModelError::Unsupported(_)))); } #[test] -fn gltf_missing_indices_are_charged_to_the_read_budget() { - // Without `indices` every vertex gets a generated index, allocated like a read: after the - // 3,000 position components, 999 left is one short for the 1,000 indices. - let doc = gltf_shared_positions(1000, 1); - let e = crate::gltf::parse_with_budget(doc.as_bytes(), &|_| None, 3999).err().unwrap_or_else(|| panic!("imported")); - assert!(e.to_string().contains("(1000 requested, 999 left)"), "{e}"); +fn obj_repeated_material_libraries_and_textures_load_once() { + let png = { + let img = image::RgbaImage::from_pixel(1, 1, image::Rgba([0, 255, 0, 255])); + let mut b = std::io::Cursor::new(vec![]); + img.write_to(&mut b, image::ImageFormat::Png).unwrap(); + b.into_inner() + }; + let mtl = b"newmtl A\nmap_Kd t.png\nnewmtl B\nmap_Kd t.png\n".to_vec(); + let src = b"mtllib m.mtl\nmtllib m.mtl\nv 0 0 0\nv 1 0 0\nv 0 1 0\nusemtl A\nf 1 2 3\nusemtl B\nf 1 2 3\nusemtl A\nf 1 2 3\n"; + let m = obj::parse(src, &|name| match name { + "m.mtl" => Some(mtl.clone()), + "t.png" => Some(png.clone()), + _ => None, + }) + .unwrap(); + assert_eq!(m.materials.iter().map(|m| m.name.as_str()).collect::>(), ["A", "B"]); + assert_eq!(m.textures.len(), 1); + assert_eq!(m.meshes[0].primitives.iter().map(|p| (p.material, p.triangle_count())).collect::>(), [(Some(0), 2), (Some(1), 1)]); } #[test] -fn gltf_strip_expansion_is_charged_to_the_read_budget() { - // A strip of k indices becomes 3(k-2) triangle indices and as many flat-shaded vertices, so - // the output is charged, not only the 1,009 components read. - let doc = gltf_doc(r#"{"bufferView": 0, "componentType": 5126, "type": "VEC3", "count": 3}"#, r#"{"buffer": 0}"#, 1036) - .replace(r#""count": 3}]"#, r#""count": 3}, {"bufferView": 0, "componentType": 5121, "type": "SCALAR", "count": 1000}]"#) - .replace(r#""attributes": {"POSITION": 0}"#, r#""attributes": {"POSITION": 0}, "indices": 1, "mode": 5"#); - let m = crate::gltf::parse_with_budget(doc.as_bytes(), &|_| None, 30_000).unwrap(); - assert_eq!(m.meshes[0].primitives[0].indices.len(), 2994); - let e = crate::gltf::parse_with_budget(doc.as_bytes(), &|_| None, 5000).err().unwrap_or_else(|| panic!("imported")); - assert!(e.to_string().contains("more than 5000 components"), "{e}"); +fn gltf_textures_naming_one_image_share_it() { + let doc = String::from_utf8(fixture("quad.gltf")).unwrap(); + // Three textures of one image: the fixture's sampler, the same sampler again, no sampler. + let doc = doc.replace( + "\"textures\": [\n {\n \"source\": 0,\n \"sampler\": 0\n }\n ]", + "\"textures\": [{\"source\": 0, \"sampler\": 0}, {\"source\": 0, \"sampler\": 0}, {\"source\": 0}]", + ); + let m = gltf::parse(doc.as_bytes(), &|_| None).unwrap(); + assert_eq!(m.textures.len(), 2); + assert_eq!(m.textures[0].wrap, [Wrap::Clamp, Wrap::Repeat]); + assert_eq!(m.textures[1].wrap, [Wrap::Repeat, Wrap::Repeat]); +} + +#[test] +fn instances_of_one_skin_share_its_joint_matrices() { + let p = mesh(PrimitiveKind::Plane, &PrimParams { size: [2.0, 2.0, 0.0], ..Default::default() }); + let mut m = Model::single(Mesh { name: "s".into(), primitives: vec![p] }, vec![Material::default()]); + m.nodes[0].skin = Some(0); + m.nodes.push(Node { mesh: Some(0), skin: Some(0), ..Node::default() }); + m.roots = vec![0, 1]; + m.skins.push(Skin { joints: vec![0], inverse_bind: vec![Mat4::IDENTITY] }); + let inst = m.instances(None, 0.0); + assert_eq!(inst.len(), 2); + assert!(Arc::ptr_eq(inst[0].joints.as_ref().unwrap(), inst[1].joints.as_ref().unwrap())); +} + +#[test] +fn contour_grouping_is_bounded() { + let sq = |x: f64, y: f64, s: f64| vec![[x, y], [x + s, y], [x + s, y + s], [x, y + s]]; + let n = extrude::MAX_CONTOURS + 1; + let cs: Vec> = (0..n).map(|i| sq(i as f64 * 2.0, 0.0, 1.0)).collect(); + let g = group_contours(cs); + assert_eq!(g.len(), extrude::MAX_CONTOURS); + assert!(g.iter().all(|o| o.holes.is_empty())); +} + +/// A texture the import leaves out (too large, or not an image) is named in `Model::warnings`, +/// which the import shows the user; the model still loads. +#[test] +fn left_out_textures_are_reported_as_import_warnings() { + // A 24-bit BMP header claiming 9000×9000 (81 MP, over one texture's limit) over no pixel data. + let mut big = b"BM".to_vec(); + big.extend_from_slice(&[0; 8]); + big.extend_from_slice(&54u32.to_le_bytes()); + big.extend_from_slice(&40u32.to_le_bytes()); + big.extend_from_slice(&9000u32.to_le_bytes()); + big.extend_from_slice(&9000u32.to_le_bytes()); + big.extend_from_slice(&[1, 0, 24, 0]); + big.extend_from_slice(&[0; 24]); + let mtl = b"newmtl A\nmap_Kd big.bmp\nnewmtl B\nmap_Kd junk.png\nnewmtl C\nmap_Kd gone.png\n".to_vec(); + let src = b"mtllib m.mtl\nv 0 0 0\nv 1 0 0\nv 0 1 0\nusemtl A\nf 1 2 3\n"; + let m = obj::parse(src, &|name| match name { + "m.mtl" => Some(mtl.clone()), + "big.bmp" => Some(big.clone()), + "junk.png" => Some(b"not an image".to_vec()), + _ => None, + }) + .unwrap(); + assert!(m.textures.is_empty()); + let all = m.warnings.join("|"); + assert!(all.contains("big.bmp") && all.contains("junk.png") && all.contains("gone.png"), "{all}"); } diff --git a/crates/opusenc/Cargo.toml b/crates/opusenc/Cargo.toml index 489c648ea..db7f2b7f8 100644 --- a/crates/opusenc/Cargo.toml +++ b/crates/opusenc/Cargo.toml @@ -12,4 +12,4 @@ workspace = true [dependencies] [dev-dependencies] -filmcraft-opus = { workspace = true } +craft-audio = { workspace = true } diff --git a/crates/opusenc/README.md b/crates/opusenc/README.md index 1ebf6fedb..8a822ceac 100644 --- a/crates/opusenc/README.md +++ b/crates/opusenc/README.md @@ -85,7 +85,7 @@ recursive band splitting, greedy PVQ search. Not implemented: transient frames, pitch post-filter, intensity stereo, dynamic allocation, VBR. All-zero CELT frames become 3-byte silence packets. -## Measured (tests in `tests/`, FilmCraft decoder) +## Measured (tests in `tests/`, craft-audio's Opus decoder) Synthetic speech (glottal pulses 100–200 Hz through three moving formants, syllable envelopes, pauses) and music (three-note harmonic chords); waveform SNR against the input band-limited to @@ -129,8 +129,8 @@ stage-1/2 codebooks and prediction weights, gain, pitch, contour, LTP and pulse weights) and the decoder-side normative arithmetic the encoder must reproduce bit-exactly (CELT allocation; SILK NLSF dequantisation and stabilisation, NLSF → LPC conversion, `log2lin`, the fixed-point division/inverse helpers and the LPC analysis filter) are mirrored from FilmCraft's -first-party, spec-derived `filmcraft-opus` decoder (MIT OR Apache-2.0), which is also the -round-trip oracle in `tests/` and `crates/export/tests/it/opus_roundtrip.rs` (ffmpeg is a second, +first-party, spec-derived `filmcraft-opus` decoder (MIT OR Apache-2.0). That decoder, now +`craft_audio::opus` in craft-libs, is also the round-trip oracle in `tests/` and `crates/export/tests/it/opus_roundtrip.rs` (ffmpeg is a second, external oracle when installed). All encoder-side analysis and search (pitch, LPC, NLSF and LTP search, noise feedback, rate control, stereo prediction, the decimator) is our own design. No third-party encoder or decoder source (libopus or otherwise) was consulted. diff --git a/crates/opusenc/tests/it/common/mod.rs b/crates/opusenc/tests/it/common/mod.rs index 9b420f31b..8f00b88e9 100644 --- a/crates/opusenc/tests/it/common/mod.rs +++ b/crates/opusenc/tests/it/common/mod.rs @@ -124,9 +124,9 @@ pub fn encode(mut enc: OpusEncoder, planar: &[Vec]) -> (OpusEncoder, Vec]) -> Vec> { - let mut dec = filmcraft_opus::Decoder::new(head).expect("OpusHead"); + let mut dec = craft_audio::opus::Decoder::new(head).expect("OpusHead"); dec.set_trim_pre_skip(true); let mut out: Vec> = vec![Vec::new(); dec.channels()]; for p in packets { diff --git a/crates/path/src/interp.rs b/crates/path/src/interp.rs index 4087b26b5..89e7edc51 100644 --- a/crates/path/src/interp.rs +++ b/crates/path/src/interp.rs @@ -311,6 +311,9 @@ fn frame(v: &[P]) -> (P, f64) { (c, r) } +/// Vertex counts above this skip the shape contexts (quadratic to build) and search the starting +/// vertex coarsely; no mask comes near it (the Add Vertices option itself stops at 2000). +const MAX_CONTEXT_VERTICES: usize = 4096; const RBINS: usize = 5; const ABINS: usize = 12; @@ -412,10 +415,18 @@ pub fn correspond(a: &ShapePath, b: &ShapePath, o: &InterpOpts) -> (ShapePath, S } if !o.first_vertices_match { let n = ra.len(); - let use_sc = o.quality > 0.0; + let large = n > MAX_CONTEXT_VERTICES; + let use_sc = o.quality > 0.0 && !large; let (sa, sb) = if use_sc { (Some(shape_contexts(&ra.vertices)), Some(shape_contexts(&rb.vertices))) } else { (None, None) }; // Quality limits the offsets examined exhaustively (coarse step), then refines. - let step = if o.quality >= 50.0 { 1 } else { ((n as f64 / 24.0).ceil() as usize).max(1) }; + // (A very large shape tries about 3 √n offsets, not n, each costing a pass over it.) + let step = if large { + (n as f64).sqrt().ceil() as usize + } else if o.quality >= 50.0 { + 1 + } else { + ((n as f64 / 24.0).ceil() as usize).max(1) + }; let mut best = (f64::MAX, 0usize); let mut k = 0; while k < n { @@ -592,4 +603,22 @@ mod tests { let l2 = (lin.vertices[1][0] - lin.vertices[0][0]).hypot(lin.vertices[1][1] - lin.vertices[0][1]); assert!(l2 < 80.0); } + + /// A shape with tens of thousands of vertices is matched without the quadratic shape contexts + /// and exhaustive offset search. + #[test] + fn very_large_shapes_are_matched_in_bounded_time() { + let n = 30_000; + let circle = |rot: f64| -> ShapePath { + let pts: Vec

= (0..n) + .map(|i| [100.0 * (rot + i as f64 * std::f64::consts::TAU / n as f64).cos(), 100.0 * (rot + i as f64 * std::f64::consts::TAU / n as f64).sin()]) + .collect(); + ShapePath::polygon(&pts, true) + }; + let o = InterpOpts { one_to_one: true, quality: 100.0, ..Default::default() }; + let t = std::time::Instant::now(); + let (a, b) = correspond(&circle(0.0), &circle(1.0), &o); + assert_eq!((a.len(), b.len()), (n, n)); + assert!(t.elapsed().as_secs() < 30, "{:?}", t.elapsed()); + } } diff --git a/crates/path/src/trace.rs b/crates/path/src/trace.rs index 01508bcaf..7b15b55c2 100644 --- a/crates/path/src/trace.rs +++ b/crates/path/src/trace.rs @@ -55,37 +55,85 @@ pub fn trace(field: &[f32], w: usize, h: usize, o: &TraceOpts) -> Vec { return vec![]; } let loops = contours(field, w, h, o.threshold); - let mut polys: Vec<(Vec<[f64; 2]>, Vec<[f64; 2]>, f64)> = loops + // (raw loop, kept raw indices, simplified loop, area) + let mut polys: Vec<(Vec<[f64; 2]>, Vec, Vec<[f64; 2]>, f64)> = loops .into_iter() .filter_map(|raw| { let a = signed_area(&raw); if a.abs() < o.min_area.max(0.0) || raw.len() < 3 { return None; } - let simp = simplify_closed(&raw, o.tolerance.max(0.01)); - if simp.len() < 3 { + let idx = simplify_idx(&raw, o.tolerance.max(0.01)); + if idx.len() < 3 { return None; } - Some((raw, simp, a)) - }) - .collect(); - // Depth: how many other loops contain a point of this loop. - let depths: Vec = (0..polys.len()) - .map(|i| { - let p = polys[i].1[0]; - (0..polys.len()).filter(|&j| j != i && polys[j].2.abs() > polys[i].2.abs() && point_in_poly(p, &polys[j].1)).count() + let simp = idx.iter().map(|&i| raw[i]).collect(); + Some((raw, idx, simp, a)) }) .collect(); + let depths = nesting_depths(&polys.iter().map(|p| (p.2.as_slice(), p.3.abs())).collect::>(), w, h); let mut out: Vec = polys .drain(..) .zip(depths) - .map(|((raw, simp, a), depth)| Traced { path: fit_loop(&raw, &simp, o.corner_roundness.clamp(0.0, 1.0)), depth, area: a }) + .map(|((raw, idx, simp, a), depth)| Traced { path: fit_loop(&raw, &idx, &simp, o.corner_roundness.clamp(0.0, 1.0)), depth, area: a }) .collect(); // Outer outlines first, then holes, islands…; larger first within a depth. out.sort_by(|a, b| a.depth.cmp(&b.depth).then(b.area.abs().total_cmp(&a.area.abs()))); out } +/// Grid cell (pixels) of the index [`nesting_depths`] finds candidate containers with. +const DEPTH_CELL: f64 = 32.0; + +/// Most grid cells one loop's box is entered into; a box covering more is kept in a short list +/// every point is tested against, so nested full-image contours can't fill the grid. +const MAX_BOX_CELLS: usize = 64; + +/// Per loop (simplified points, |area|): how many other, larger loops contain its first point. +/// Only loops whose bounding box holds that point can contain it, so each loop's box is entered +/// into a coarse grid over the `w × h` field and a point is tested against the loops of its +/// cell alone: many separate regions no longer test every pair. +fn nesting_depths(polys: &[(&[[f64; 2]], f64)], w: usize, h: usize) -> Vec { + // Loops lie within the field padded by half a pixel each side. + let (gw, gh) = (((w as f64 + 1.0) / DEPTH_CELL).ceil() as usize + 1, ((h as f64 + 1.0) / DEPTH_CELL).ceil() as usize + 1); + let cell = |v: f64, n: usize| (((v + 0.5) / DEPTH_CELL).floor().max(0.0) as usize).min(n - 1); + let boxes: Vec<[f64; 4]> = polys + .iter() + .map(|(p, _)| { + p.iter().fold([f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY], |b, q| { + [b[0].min(q[0]), b[1].min(q[1]), b[2].max(q[0]), b[3].max(q[1])] + }) + }) + .collect(); + let mut grid: Vec> = vec![Vec::new(); gw * gh]; + let mut wide: Vec = Vec::new(); + for (j, b) in boxes.iter().enumerate() { + let (x0, x1, y0, y1) = (cell(b[0], gw), cell(b[2], gw), cell(b[1], gh), cell(b[3], gh)); + if (x1 - x0 + 1).saturating_mul(y1 - y0 + 1) > MAX_BOX_CELLS { + wide.push(j); + continue; + } + for cy in y0..=y1 { + for cx in x0..=x1 { + grid[cy * gw + cx].push(j); + } + } + } + (0..polys.len()) + .map(|i| { + let p = polys[i].0[0]; + grid[cell(p[1], gh) * gw + cell(p[0], gw)] + .iter() + .chain(&wide) + .filter(|&&j| { + let b = boxes[j]; + j != i && polys[j].1 > polys[i].1 && p[0] >= b[0] && p[0] <= b[2] && p[1] >= b[1] && p[1] <= b[3] && point_in_poly(p, polys[j].0) + }) + .count() + }) + .collect() +} + /// Marching squares: closed polylines (pixel coordinates). fn contours(field: &[f32], w: usize, h: usize, thr: f32) -> Vec> { // Padded grid: sample (i, j) of the padded grid is pixel (i - 1, j - 1), outside = -inf. @@ -233,23 +281,37 @@ fn seg_dist(p: [f64; 2], a: [f64; 2], b: [f64; 2]) -> f64 { ((p[0] - a[0] - t * dx).powi(2) + (p[1] - a[1] - t * dy).powi(2)).sqrt() } -/// Douglas–Peucker on an open polyline: kept indices. -fn dp(p: &[[f64; 2]], lo: usize, hi: usize, tol: f64, keep: &mut Vec) { - if hi <= lo + 1 { - return; - } - let (mut best, mut bi) = (0.0, lo); - for (k, q) in p.iter().enumerate().take(hi).skip(lo + 1) { - let d = seg_dist(*q, p[lo], p[hi]); - if d > best { - best = d; - bi = k; +/// Distance evaluations Douglas–Peucker may spend per point of a loop. Balanced splits need +/// about log2(n); splits that keep landing next to an end would need up to n. +const DP_WORK_PER_POINT: usize = 64; + +/// Douglas–Peucker on an open polyline: kept indices (unordered). Spans wait on an explicit +/// stack (no recursion depth to overflow), and once `budget` distance evaluations are spent the +/// spans still waiting keep all their points: the outline stays exact, just less simplified. +fn dp(p: &[[f64; 2]], lo: usize, hi: usize, tol: f64, budget: &mut usize, keep: &mut Vec) { + let mut spans = vec![(lo, hi)]; + while let Some((lo, hi)) = spans.pop() { + if hi <= lo + 1 { + continue; + } + if *budget < hi - lo - 1 { + keep.extend(lo + 1..hi); + continue; + } + *budget -= hi - lo - 1; + let (mut best, mut bi) = (0.0, lo); + for (k, q) in p.iter().enumerate().take(hi).skip(lo + 1) { + let d = seg_dist(*q, p[lo], p[hi]); + if d > best { + best = d; + bi = k; + } + } + if best > tol { + keep.push(bi); + spans.push((lo, bi)); + spans.push((bi, hi)); } - } - if best > tol { - dp(p, lo, bi, tol, keep); - keep.push(bi); - dp(p, bi, hi, tol, keep); } } @@ -265,27 +327,23 @@ fn simplify_idx(p: &[[f64; 2]], tol: f64) -> Vec { let mut ext: Vec<[f64; 2]> = p.to_vec(); ext.push(p[0]); let mut keep = vec![0]; - dp(&ext, 0, far, tol, &mut keep); + let mut budget = n.saturating_mul(DP_WORK_PER_POINT); + dp(&ext, 0, far, tol, &mut budget, &mut keep); keep.push(far); - dp(&ext, far, n, tol, &mut keep); + dp(&ext, far, n, tol, &mut budget, &mut keep); keep.sort_unstable(); keep.dedup(); keep.retain(|&i| i < n); keep } -fn simplify_closed(p: &[[f64; 2]], tol: f64) -> Vec<[f64; 2]> { - simplify_idx(p, tol).into_iter().map(|i| p[i]).collect() -} - -/// Fit cubics through the simplified vertices of a loop (`raw` holds the full contour). -fn fit_loop(raw: &[[f64; 2]], simp: &[[f64; 2]], round: f64) -> ShapePath { +/// Fit cubics through the simplified vertices of a loop (`raw` holds the full contour, `idx` +/// the raw index of each simplified vertex, as [`simplify_idx`] kept them). +fn fit_loop(raw: &[[f64; 2]], idx: &[usize], simp: &[[f64; 2]], round: f64) -> ShapePath { let n = simp.len(); if round <= 1e-6 { return ShapePath::polygon(simp, true); } - // Raw index of each simplified vertex (they are raw points). - let idx: Vec = simp.iter().map(|s| raw.iter().position(|r| r == s).unwrap_or(0)).collect(); let pt = |a: [f64; 2]| Point::new(a[0], a[1]); let v = |a: [f64; 2], b: [f64; 2]| Vec2::new(b[0] - a[0], b[1] - a[1]); // Per vertex: (direction leaving, direction arriving), blended between chord and smooth. @@ -349,6 +407,86 @@ mod tests { fill_coverage(&paths, &Mat3::IDENTITY, w as u32, h as u32, FillRule::EvenOdd).data } + #[test] + fn nesting_depths_match_every_pair() { + // Rings within rings beside many separate dots: the grid lookup gives the same depths + // as testing every pair. + let (w, h) = (200, 120); + let mut field = vec![0.0f32; w * h]; + for y in 0..h { + for x in 0..w { + let (fx, fy) = (x as f64 + 0.5 - 60.0, y as f64 + 0.5 - 60.0); + let r = (fx * fx + fy * fy).sqrt(); + let ring = r < 56.0 && ((r / 8.0) as usize).is_multiple_of(2); + let dot = x >= 130 && x % 6 < 3 && y % 6 < 3; + field[y * w + x] = if ring || dot { 1.0 } else { 0.0 }; + } + } + let opts = TraceOpts { min_area: 1.0, ..Default::default() }; + let t = trace(&field, w, h, &opts); + assert!(t.len() > 200, "{}", t.len()); + let polys: Vec<(Vec<[f64; 2]>, f64)> = t.iter().map(|t| (t.path.vertices.clone(), t.area.abs())).collect(); + let refs: Vec<(&[[f64; 2]], f64)> = polys.iter().map(|(p, a)| (p.as_slice(), *a)).collect(); + let fast = nesting_depths(&refs, w, h); + let pairs: Vec = + (0..refs.len()).map(|i| (0..refs.len()).filter(|&j| j != i && refs[j].1 > refs[i].1 && point_in_poly(refs[i].0[0], refs[j].0)).count()).collect(); + assert_eq!(fast, pairs); + assert_eq!(fast, t.iter().map(|t| t.depth).collect::>()); + assert!(fast.iter().max() >= Some(&5)); + } + + #[test] + fn nesting_depths_count_wide_boxes_kept_out_of_the_grid() { + // Concentric squares whose boxes each cover far more than MAX_BOX_CELLS cells. + let (w, h) = (4000, 4000); + let squares: Vec> = (0..40) + .map(|k| { + let (a, b) = (10.0 + 40.0 * k as f64, 3990.0 - 40.0 * k as f64); + vec![[a, a], [b, a], [b, b], [a, b]] + }) + .collect(); + let refs: Vec<(&[[f64; 2]], f64)> = squares.iter().map(|p| (p.as_slice(), (p[1][0] - p[0][0]).powi(2))).collect(); + assert_eq!(nesting_depths(&refs, w, h), (0..40).collect::>()); + } + + #[test] + fn simplification_work_is_bounded_and_keeps_the_outline() { + // Out of budget, the spans still waiting keep every point: nothing is lost, the outline + // is just less simplified. + let p: Vec<[f64; 2]> = (0..12).map(|i| [i as f64, if i % 2 == 0 { 0.0 } else { 5.0 }]).collect(); + let (mut budget, mut keep) = (0, vec![]); + dp(&p, 0, 11, 0.01, &mut budget, &mut keep); + keep.sort_unstable(); + assert_eq!(keep, (1..11).collect::>()); + // With a budget, the result matches the plain recursion's on an ordinary loop. + let (mut budget, mut keep) = (usize::MAX, vec![]); + dp(&p, 0, 11, 0.01, &mut budget, &mut keep); + keep.sort_unstable(); + assert_eq!(keep, (1..11).collect::>()); + // A long, finely zig-zagging loop: simplified within a budget linear in its length. + let n = 200_000; + let ring: Vec<[f64; 2]> = (0..n) + .map(|i| { + let a = i as f64 / n as f64 * std::f64::consts::TAU; + let r = 1000.0 + if i % 2 == 0 { 0.0 } else { 3.0 }; + [r * a.cos(), r * a.sin()] + }) + .collect(); + let idx = simplify_idx(&ring, 0.5); + assert!(idx.windows(2).all(|w| w[0] < w[1]) && idx.len() >= 3 && *idx.last().unwrap() < n); + } + + #[test] + fn fit_uses_the_kept_indices() { + // A contour that passes the same point twice: the fit follows the simplifier's index, + // not the first raw point with those coordinates. + let raw = vec![[0.0, 0.0], [10.0, 0.0], [10.0, 10.0], [0.0, 0.0], [-10.0, 10.0], [-10.0, 0.0]]; + let idx = vec![0, 2, 3, 4]; + let simp: Vec<[f64; 2]> = idx.iter().map(|&i| raw[i]).collect(); + let path = fit_loop(&raw, &idx, &simp, 0.5); + assert_eq!(path.vertices, simp); + } + #[test] fn traces_a_disc_and_a_square_with_a_hole() { let (w, h) = (120, 80); diff --git a/crates/path/src/varstroke.rs b/crates/path/src/varstroke.rs index 61e6a1ab7..da00b02d0 100644 --- a/crates/path/src/varstroke.rs +++ b/crates/path/src/varstroke.rs @@ -220,6 +220,56 @@ fn disc(out: &mut BezPath, c: Point, r: f64) { } } +/// `path` with each curve whose extent is over 10⁷ × `tol` replaced by lines flattened at 10⁻⁷ of +/// that extent. `None` when a coordinate is not finite. +fn coarsen_huge(path: &BezPath, tol: f64) -> Option { + use kurbo::Shape as _; + let mut out = BezPath::new(); + let (mut cur, mut start) = (Point::ZERO, Point::ZERO); + for el in path.elements() { + let seg = match *el { + PathEl::MoveTo(p) => { + (cur, start) = (p, p); + out.push(*el); + continue; + } + PathEl::LineTo(p) => { + cur = p; + out.push(*el); + continue; + } + PathEl::ClosePath => { + cur = start; + out.push(*el); + continue; + } + PathEl::QuadTo(_, p) | PathEl::CurveTo(_, _, p) => { + let mut one = BezPath::new(); + one.move_to(cur); + one.push(*el); + (one, p) + } + }; + let (one, end) = seg; + let bb = one.bounding_box(); + let extent = bb.width().max(bb.height()); + if !extent.is_finite() { + return None; + } + if extent * 1e-7 > tol { + kurbo::flatten(one.elements().iter().copied(), extent * 1e-7, |e| { + if let PathEl::LineTo(p) = e { + out.line_to(p); + } + }); + } else { + out.push(*el); + } + cur = end; + } + Some(out) +} + /// Outline of a tapered / waved stroke (local space; `res_scale` = device/local scale). pub fn outline(paths: &[BezPath], style: &StrokeStyle, res_scale: f64) -> Option { if style.width <= 0.0 { @@ -230,7 +280,11 @@ pub fn outline(paths: &[BezPath], style: &StrokeStyle, res_scale: f64) -> Option let half = style.width / 2.0; for path in paths { // Fine enough for the width profile: ½ device pixel, at most ~20 000 samples per path. - let rough = flatten(path, 0.1 / rs, f64::INFINITY); + // Curves with huge extents are first cut into lines at a tolerance of 10⁻⁷ of their own + // extent, so none subdivides into billions of points before the step below applies; + // every other segment (small curves in a large path included) keeps the ½-pixel tolerance. + let Some(path) = coarsen_huge(path, 0.1 / rs) else { continue }; + let rough = flatten(&path, 0.1 / rs, f64::INFINITY); for poly0 in rough { let len0 = poly0.len(); if len0 <= 0.0 { @@ -396,4 +450,37 @@ mod tests { let area: f32 = m.data.iter().sum(); assert!(area > 240.0 * 6.0 * 0.8 && area < 240.0 * 6.0 * 1.05, "{area}"); } + + /// A curve with enormous coordinates is approximated within a bounded number of points. + #[test] + fn huge_coordinates_do_not_explode_the_first_approximation() { + let mut p = BezPath::new(); + p.move_to((0.0, 0.0)); + p.curve_to((1e13, 0.0), (-1e13, 1e13), (1e13, 1e13)); + let st = StrokeStyle { + width: 4.0, + taper: Some(Taper { percent: true, start_len: 50.0, end_len: 50.0, start_width: 0.0, end_width: 0.0, start_ease: 0.0, end_ease: 0.0 }), + ..Default::default() + }; + let t = std::time::Instant::now(); + let o = outline(&[p], &st, 1.0).unwrap(); + assert!(o.elements().len() < 200_000, "{}", o.elements().len()); + assert!(t.elapsed().as_secs() < 20); + } + + /// Small curves keep their shape in a path that also has a huge segment. + #[test] + fn small_curves_survive_a_huge_segment() { + let mut p = BezPath::new(); + p.move_to((0.0, 0.0)); + p.curve_to((100.0, 0.0), (0.0, 100.0), (0.0, 0.0)); + p.line_to((1e10, 0.0)); + let c = coarsen_huge(&p, 0.1).unwrap(); + assert_eq!(c.elements().len(), p.elements().len(), "nothing here is huge enough to cut"); + let mut q = BezPath::new(); + q.move_to((0.0, 0.0)); + q.curve_to((1e13, 0.0), (-1e13, 1e13), (1e13, 1e13)); + let c = coarsen_huge(&q, 0.1).unwrap(); + assert!(c.elements().iter().skip(1).all(|e| matches!(e, PathEl::LineTo(_))) && c.elements().len() < 10_000); + } } diff --git a/crates/pdf/Cargo.toml b/crates/pdf/Cargo.toml index bf6148569..68aabfbd7 100644 --- a/crates/pdf/Cargo.toml +++ b/crates/pdf/Cargo.toml @@ -10,6 +10,7 @@ rust-version.workspace = true workspace = true [dependencies] +craft-eps = { workspace = true } craft-pdf.workspace = true effectcraft-svg = { workspace = true } kurbo = { workspace = true } diff --git a/crates/pdf/README.md b/crates/pdf/README.md index 20f655611..53689ba9a 100644 --- a/crates/pdf/README.md +++ b/crates/pdf/README.md @@ -29,7 +29,13 @@ ffmpeg elsewhere) to check the code tables against an independent implementation the app's existing object model. References retain object and generation numbers. A bounded compatibility repair recovers referenced objects from object streams when damaged xrefs omit them; every recovery or unresolved reference is reported in `Doc::skipped` (up to 64 messages). - The existing page tree preserves inherited resources, MediaBox / CropBox and `/Rotate`. + The existing page tree preserves inherited resources, MediaBox / CropBox and `/Rotate`. A page + is its CropBox (else MediaBox), turned by `/Rotate` (`Framing::Viewer`, the default). Footage + from an After Effects project is framed as After Effects places a PDF or `.ai` + (`Framing::Placed`, `parse_page_framed`): the page's own ArtBox clipped to the CropBox (else + MediaBox) when it has one, else the CropBox, else the MediaBox, with `/Rotate` ignored + (measured in After Effects 2026 with own files, #3099). Not measured: a CropBox without an + ArtBox, files saved by Illustrator itself, `.pdf` names. Content/image filters remain app-owned: Flate (with PNG predictors), LZW, ASCIIHex, ASCII85, RunLength and CCITTFax. Encrypted files remain refused; `page_count` returns zero when the reader refuses a document. @@ -138,7 +144,7 @@ ffmpeg elsewhere) to check the code tables against an independent implementation ## Shared parser setup and bounds The workspace pins `craft-pdf` to `craft-libs` Git revision -`92d392cff132eaf53e13afff77f38b5c11b73aa0`, with `default-features = false` and only the explicit `read` +`091863e4a615f38743bfd5d6dc62a2e8f37cd5d5`, with `default-features = false` and only the explicit `read` feature. This excludes the backend's bundled fonts and Adobe CMaps and preserves EffectCraft's font and asset policies. The shared parser does not change the supported drawing operators, image codecs, continuously rasterized SVG tree, editable shapes, or EPS interpreter. diff --git a/crates/pdf/src/build.rs b/crates/pdf/src/build.rs index 0b1586f89..0f295a045 100644 --- a/crates/pdf/src/build.rs +++ b/crates/pdf/src/build.rs @@ -7,6 +7,9 @@ use effectcraft_svg::{Affine, BezPath, FillRule, Geom, Group, Node, Paint, Shape /// PostScript program, or the PDF content stream. pub(crate) const MAX_PATH: usize = 1 << 20; +/// The deepest the open groups nest (the render tree and its consumers recurse through them). +const MAX_DEPTH: usize = 256; + #[derive(Clone, Copy, PartialEq, Eq)] enum Kind { Plain, @@ -20,6 +23,9 @@ pub(crate) struct Builder { pub top_layers: Vec, pub skipped: Vec, pub shapes: usize, + /// Groups the finished nodes will sit under once placed (an enclosing builder's open groups + /// and the pattern or mask groups around them), counted against [`MAX_DEPTH`]. + base: usize, } pub(crate) fn group(name: &str) -> Group { @@ -28,13 +34,25 @@ pub(crate) fn group(name: &str) -> Group { impl Builder { pub fn new() -> Builder { - Builder { stack: vec![(group("page"), Kind::Plain)], top_layers: vec![], skipped: vec![], shapes: 0 } + Builder { stack: vec![(group("page"), Kind::Plain)], top_layers: vec![], skipped: vec![], shapes: 0, base: 0 } + } + + /// A builder for content that will be placed inside this one's open groups and `wrap` more + /// groups (a pattern's `Pattern` and `Tile`, a soft mask's two), so its nesting limit counts + /// them. + pub fn nested(&self, wrap: usize) -> Builder { + Builder { base: self.base.saturating_add(self.stack.len()).saturating_add(wrap), ..Builder::new() } } pub fn depth(&self) -> usize { self.stack.len() } + /// `wrap` more groups fit under the open ones within [`MAX_DEPTH`]. + pub fn has_room(&self, wrap: usize) -> bool { + self.base.saturating_add(self.stack.len()).saturating_add(wrap) <= MAX_DEPTH + } + pub fn skip(&mut self, what: &str) { if !self.skipped.iter().any(|s| s == what) { self.skipped.push(what.to_string()); @@ -62,23 +80,33 @@ impl Builder { } } + /// Open `g` as the group drawn into from now on; past [`MAX_DEPTH`] the group is dropped + /// (what would have been drawn inside it goes to the open group) and noted as skipped. + fn push_group(&mut self, g: Group, kind: Kind) { + if self.base.saturating_add(self.stack.len()) >= MAX_DEPTH { + self.skip("group nesting limit"); + return; + } + self.stack.push((g, kind)); + } + /// Children drawn from now on are clipped by `path` (page space) until the group closes. pub fn clip(&mut self, path: BezPath, rule: FillRule) { let mut g = group("Clip Group"); g.clip.push((path, rule)); // Clipping is not a transparency group: blend modes inside reach the backdrop. g.isolated = false; - self.stack.push((g, Kind::Clip)); + self.push_group(g, Kind::Clip); } /// Open an optional-content layer (or a named group). pub fn open(&mut self, name: &str, layer: bool) { - self.stack.push((group(name), if layer { Kind::Layer } else { Kind::Plain })); + self.push_group(group(name), if layer { Kind::Layer } else { Kind::Plain }); } /// Open a group (blend mode, soft mask) closed with the graphics state. pub fn open_group(&mut self, g: Group) { - self.stack.push((g, Kind::Plain)); + self.push_group(g, Kind::Plain); } /// Add a finished node (an image, pattern tiles). diff --git a/crates/pdf/src/ccitt.rs b/crates/pdf/src/ccitt.rs index fa013161f..ca6a40d21 100644 --- a/crates/pdf/src/ccitt.rs +++ b/crates/pdf/src/ccitt.rs @@ -401,8 +401,10 @@ fn mode(r: &mut Reader) -> Option { /// (index parity: even indices start black runs), and the one after it. fn b1b2(refl: &[usize], a0: i64, white: bool, columns: usize) -> (usize, usize) { let want = if white { 0 } else { 1 }; - let mut i = 0; - while i < refl.len() && (refl[i] as i64 <= a0 || i % 2 != want) { + // The changing elements are in order, so the first past `a0` is found by bisection (a scan + // from the start made every row quadratic in its transitions). + let mut i = refl.partition_point(|&c| c as i64 <= a0); + if i % 2 != want { i += 1; } let b1 = refl.get(i).copied().unwrap_or(columns); @@ -804,4 +806,24 @@ pub(crate) mod tests { let _ = decode(&garbage, &Params { k, columns: 1, rows: 0, ..Params::default() }, 1 << 20); } } + + /// `b1b2` by bisection agrees with the scan it replaced, for every position and colour + /// over a dense reference row. + #[test] + fn b1b2_by_bisection_matches_the_scan() { + let scan = |refl: &[usize], a0: i64, white: bool, columns: usize| { + let want = if white { 0 } else { 1 }; + let mut i = 0; + while i < refl.len() && (refl[i] as i64 <= a0 || i % 2 != want) { + i += 1; + } + (refl.get(i).copied().unwrap_or(columns), refl.get(i + 1).copied().unwrap_or(columns)) + }; + let refl: Vec = (0..64).map(|i| i * 3 / 2).collect(); + for a0 in -1..100 { + for white in [true, false] { + assert_eq!(b1b2(&refl, a0, white, 100), scan(&refl, a0, white, 100), "a0 {a0} white {white}"); + } + } + } } diff --git a/crates/pdf/src/cff.rs b/crates/pdf/src/cff.rs index 61bb7f9eb..cb7214cc2 100644 --- a/crates/pdf/src/cff.rs +++ b/crates/pdf/src/cff.rs @@ -538,16 +538,31 @@ impl Cff { /// A glyph's outline in glyph space (`font_matrix` maps it to text space) and its advance. pub fn outline(&self, gid: u16) -> Option<(BezPath, f64)> { - self.outline_depth(gid, 0) + self.outline_depth(gid, 0, &mut { MAX_GLYPH_OPS }) } - fn outline_depth(&self, gid: u16, depth: usize) -> Option<(BezPath, f64)> { + /// `ops` is the glyph's instruction budget, shared by the base glyph and its seac components. + fn outline_depth(&self, gid: u16, depth: usize, ops: &mut usize) -> Option<(BezPath, f64)> { let r = self.charstrings.get(gid as usize)?.clone(); let fd = self.fdselect.get(gid as usize).copied().unwrap_or(0) as usize; let fd = fd.min(self.lsubrs.len() - 1); - let mut st = - T2 { cff: self, fd, path: BezPath::new(), x: 0.0, y: 0.0, stack: vec![], stems: 0, width: None, open: false, done: false, depth: 0, seac: None }; + let mut st = T2 { + cff: self, + fd, + path: BezPath::new(), + x: 0.0, + y: 0.0, + stack: vec![], + stems: 0, + width: None, + open: false, + done: false, + depth: 0, + ops: *ops, + seac: None, + }; st.run(&self.data[r]); + *ops = st.ops; if st.open { st.path.close_path(); } @@ -563,10 +578,10 @@ impl Cff { && depth < 2 { let find = |code: u8| crate::encoding::Base::Standard.name(code).and_then(|n| self.gid_by_name(&n)); - if let Some((bp, _)) = find(b).and_then(|g| self.outline_depth(g, depth + 1)) { + if let Some((bp, _)) = find(b).and_then(|g| self.outline_depth(g, depth + 1, ops)) { path.extend(bp); } - if let Some((ap, _)) = find(a).and_then(|g| self.outline_depth(g, depth + 1)) { + if let Some((ap, _)) = find(a).and_then(|g| self.outline_depth(g, depth + 1, ops)) { path.extend(kurbo::Affine::translate((adx, ady)) * ap); } } @@ -646,9 +661,16 @@ struct T2<'a> { open: bool, done: bool, depth: usize, + /// Instructions this glyph may still run, across its subroutine calls. + ops: usize, seac: Option<(f64, f64, u8, u8)>, } +/// The most charstring instructions one glyph runs: subroutines that branch (two calls each, +/// ten levels deep) would otherwise multiply a small font into unbounded work and geometry. +/// A real glyph runs a few hundred. +pub(crate) const MAX_GLYPH_OPS: usize = 100_000; + impl T2<'_> { fn move_to(&mut self, dx: f64, dy: f64) { if self.open { @@ -698,6 +720,11 @@ impl T2<'_> { } let mut i = 0; while i < cs.len() && !self.done { + let Some(left) = self.ops.checked_sub(1) else { + self.done = true; + return; + }; + self.ops = left; let b0 = cs[i]; i += 1; match b0 { @@ -958,6 +985,12 @@ impl T2<'_> { /// named by custom strings. #[cfg(test)] pub(crate) fn write_test_cff(glyphs: &[(&str, Vec)]) -> Vec { + write_test_cff_with_gsubrs(glyphs, &[]) +} + +/// [`write_test_cff`] with global subroutines. +#[cfg(test)] +pub(crate) fn write_test_cff_with_gsubrs(glyphs: &[(&str, Vec)], gsubrs: &[Vec]) -> Vec { fn idx(items: &[Vec]) -> Vec { let mut out = (items.len() as u16).to_be_bytes().to_vec(); if items.is_empty() { @@ -983,7 +1016,7 @@ pub(crate) fn write_test_cff(glyphs: &[(&str, Vec)]) -> Vec { let names = idx(&[b"Test".to_vec()]); let strings: Vec> = glyphs.iter().skip(1).map(|(n, _)| n.as_bytes().to_vec()).collect(); let strings_idx = idx(&strings); - let gsubrs = idx(&[]); + let gsubrs = idx(gsubrs); let cs = idx(&glyphs.iter().map(|(_, c)| c.clone()).collect::>()); // charset format 0: SIDs of glyphs 1.. (custom strings from 391). let mut charset = vec![0u8]; @@ -1090,6 +1123,43 @@ mod tests { assert!(p.elements().is_empty()); } + /// A global subroutine that calls itself ten times: 10^10 calls within the depth limit of + /// ten. The glyph stops at its instruction budget instead. + #[test] + fn branching_subroutines_stop_at_the_instruction_budget() { + // `-107 callgsubr` (bias 107) is subroutine 0. + let call = [32u8, 29]; + let mut subr = call.repeat(10); + subr.push(11); + let mut cs = call.to_vec(); + cs.push(14); + let font = write_test_cff_with_gsubrs(&[(".notdef", vec![14]), ("A", cs)], &[subr]); + let cff = Cff::parse(&font).unwrap(); + let (p, _) = cff.outline(cff.gid_by_name("A").unwrap()).unwrap(); + assert!(p.elements().is_empty()); + } + + /// The instruction budget is one per glyph: a seac whose base glyph spends all of it leaves + /// nothing for the accent, instead of each component getting a fresh budget. + #[test] + fn seac_components_share_the_instruction_budget() { + let call = [32u8, 29]; + let mut subr = call.repeat(10); + subr.push(11); + // A square, then the branching subroutine that exhausts the budget. + let mut a = vec![139 + 100, 139 + 100, 139 - 100, 6]; + a.extend(call); + a.push(14); + // `adx ady bchar achar endchar` with both components 'A' (standard code 65). + let seac = vec![139 + 10, 139 + 20, 139 + 65, 139 + 65, 14]; + let font = write_test_cff_with_gsubrs(&[(".notdef", vec![14]), ("A", a), ("Aacute", seac)], &[subr]); + let cff = Cff::parse(&font).unwrap(); + let (alone, _) = cff.outline(cff.gid_by_name("A").unwrap()).unwrap(); + let (both, _) = cff.outline(cff.gid_by_name("Aacute").unwrap()).unwrap(); + assert!(!alone.elements().is_empty()); + assert_eq!(both.elements().len(), alone.elements().len(), "the accent gets no budget after the base spent it"); + } + /// A CFF whose top DICT points its Private DICT at offsets near `usize::MAX`: `size` and `off` /// come from the file, and reading the Private DICT must not overflow `off + size`. #[test] diff --git a/crates/pdf/src/color.rs b/crates/pdf/src/color.rs index 6943476bc..5a7e9af86 100644 --- a/crates/pdf/src/color.rs +++ b/crates/pdf/src/color.rs @@ -215,19 +215,44 @@ pub enum PsOp { Proc(Vec), } -fn ps_parse(lx: &mut crate::object::Lexer, depth: usize) -> Vec { +/// Parse a procedure. `ops` counts the tokens read (those skipped as unsupported included); past +/// [`MAX_CALC_OPS`] the rest is not read, so an oversized program is never fully scanned or built +/// (the caller rejects it when `ops` ends past the cap). +fn ps_parse(lx: &mut crate::object::Lexer, depth: usize, ops: &mut usize) -> Vec { let mut out = vec![]; - while let Some(o) = lx.next() { - match o { - Obj::Num(n) => out.push(PsOp::Num(n)), - Obj::Bool(b) => out.push(PsOp::Num(if b { 1.0 } else { 0.0 })), - Obj::Op(op) if op == "{" && depth < 32 => out.push(PsOp::Proc(ps_parse(lx, depth + 1))), - // Nested too deep: skip the procedure's opening brace (never push it as an operator). - Obj::Op(op) if op == "{" => {} - Obj::Op(op) if op == "}" => return out, - Obj::Op(op) => out.push(PsOp::Op(op)), - _ => {} + loop { + // A calculator holds no arrays or dictionaries: one is skipped as a single token without + // being built (the lexer would allocate every element before this counted it). + if lx.at_compound() { + if *ops > MAX_CALC_OPS { + break; + } + lx.skip_compound(); + *ops += 1; + continue; } + let Some(o) = lx.next() else { break }; + if *ops > MAX_CALC_OPS { + break; + } + let op = match o { + Obj::Num(n) => PsOp::Num(n), + Obj::Bool(b) => PsOp::Num(if b { 1.0 } else { 0.0 }), + Obj::Op(op) if op == "{" && depth < 32 => PsOp::Proc(ps_parse(lx, depth + 1, ops)), + // Nested too deep: skip the procedure's opening brace (never push it as an operator). + Obj::Op(op) if op == "{" => { + *ops += 1; + continue; + } + Obj::Op(op) if op == "}" => return out, + Obj::Op(op) => PsOp::Op(op), + _ => { + *ops += 1; + continue; + } + }; + *ops += 1; + out.push(op); } out } @@ -243,6 +268,15 @@ fn ps_push(st: &mut Vec, v: f64) { } } +/// The most operators and operands a calculator program may hold (nested procedures included). +/// It has no loops, so this bounds what one evaluation runs; real programs hold a few dozen. +const MAX_CALC_OPS: usize = 4096; + +/// The operators and operands in `code`, nested procedures included. +fn ps_len(code: &[PsOp]) -> usize { + code.iter().map(|op| if let PsOp::Proc(p) = op { 1 + ps_len(p) } else { 1 }).sum() +} + fn ps_run(code: &[PsOp], st: &mut Vec, depth: usize) { if depth > 32 { return; @@ -431,6 +465,12 @@ impl Budget { Budget { nodes: MAX_NODES, samples: MAX_SAMPLES, overrun: false } } + /// Charge `n` retained values (coefficients, bounds, program operators) as nodes, eight to a + /// node, so a cached copy of a function is charged for what it holds, not just its node. + fn take_values(&mut self, n: usize) -> bool { + self.take_nodes(n.div_ceil(8)) + } + /// Charge `n` nodes; `false` (and the read is abandoned) once the cap is passed. fn take_nodes(&mut self, n: usize) -> bool { match self.nodes.checked_sub(n) { @@ -484,7 +524,8 @@ impl Func { /// [`Func::read`] nested `depth` levels deep, charging `budget`: unsupported past [`MAX_DEPTH`] /// or once the budget is spent. fn read_at(file: &File, o: &Obj, depth: usize, budget: &mut Budget) -> Func { - if depth >= MAX_DEPTH || !budget.take_nodes(1) { + // Charged first: the fallback past the depth limit is a node a copy keeps too. + if !budget.take_nodes(1) || depth >= MAX_DEPTH { return Func::Unsupported; } let o = file.resolve(o); @@ -499,12 +540,14 @@ impl Func { let dom = file.get(d, "Domain").map(|x| file.nums(x)).unwrap_or_default(); let domain = [dom.first().copied().unwrap_or(0.0), dom.get(1).copied().unwrap_or(1.0)]; match file.get_num(d, "FunctionType").unwrap_or(-1.0) as i32 { - 2 => Func::Exp { - domain, - c0: file.get(d, "C0").map(|x| file.nums(x)).unwrap_or_else(|| vec![0.0]), - c1: file.get(d, "C1").map(|x| file.nums(x)).unwrap_or_else(|| vec![1.0]), - n: file.get_num(d, "N").unwrap_or(1.0), - }, + 2 => { + let c0 = file.get(d, "C0").map(|x| file.nums(x)).unwrap_or_else(|| vec![0.0]); + let c1 = file.get(d, "C1").map(|x| file.nums(x)).unwrap_or_else(|| vec![1.0]); + if !budget.take_values(c0.len() + c1.len()) { + return Func::Unsupported; + } + Func::Exp { domain, c0, c1, n: file.get_num(d, "N").unwrap_or(1.0) } + } 3 => { // A stitching function's pieces: no element cap (a repeating gradient lists one // piece once per repeat); the budget bounds the total work. @@ -513,12 +556,12 @@ impl Func { None => Some(vec![]), }; let Some(funcs) = funcs else { return Func::Unsupported }; - Func::Stitch { - domain, - funcs, - bounds: file.get(d, "Bounds").map(|x| file.nums(x)).unwrap_or_default(), - encode: file.get(d, "Encode").map(|x| file.nums(x)).unwrap_or_default(), + let bounds = file.get(d, "Bounds").map(|x| file.nums(x)).unwrap_or_default(); + let encode = file.get(d, "Encode").map(|x| file.nums(x)).unwrap_or_default(); + if !budget.take_values(bounds.len() + encode.len()) { + return Func::Unsupported; } + Func::Stitch { domain, funcs, bounds, encode } } 0 => { let Obj::Stream(sd, raw) = o else { return Func::Unsupported }; @@ -537,6 +580,10 @@ impl Func { if !budget.take_samples(count) { return Func::Unsupported; } + // The stream must hold the table: missing bytes are not made up as zeros. + if count.checked_mul(bps).is_none_or(|bits| bits.div_ceil(8) > data.len()) { + return Func::Unsupported; + } let enc = file.get(d, "Encode").map(|x| file.nums(x)).unwrap_or_default(); let encode = [enc.first().copied().unwrap_or(0.0), enc.get(1).copied().unwrap_or(size as f64 - 1.0)]; let decode = file.get(d, "Decode").map(|x| file.nums(x)).filter(|v| v.len() >= outputs * 2).unwrap_or_else(|| range.clone()); @@ -567,10 +614,15 @@ impl Func { let Some(data) = decode_stream(file, sd, raw) else { return Func::Unsupported }; let mut lx = crate::object::Lexer::new(&data, 0); // The program is one procedure: `{ … }`. - let code = match ps_parse(&mut lx, 0).into_iter().next() { + let mut scanned = 0; + let code = match ps_parse(&mut lx, 0, &mut scanned).into_iter().next() { Some(PsOp::Proc(p)) => p, _ => return Func::Unsupported, }; + let ops = ps_len(&code); + if scanned > MAX_CALC_OPS || ops > MAX_CALC_OPS || !budget.take_values(ops) { + return Func::Unsupported; + } Func::Calc { domain: dom, range: file.get(d, "Range").map(|x| file.nums(x)).unwrap_or_default(), code } } _ => Func::Unsupported, @@ -756,12 +808,56 @@ mod tests { assert_eq!(sample_count(1 << 22, usize::MAX / (1 << 22) + 1), None); } + #[test] + fn oversized_calculator_program_is_cut_short() { + let src = format!("{{ {}}}", "1 ".repeat(MAX_CALC_OPS + 10)); + let mut lx = crate::object::Lexer::new(src.as_bytes(), 0); + let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() }; + assert_eq!(ps_len(&code), MAX_CALC_OPS + 1); + } + + #[test] + fn arrays_and_dictionaries_in_a_calculator_are_skipped_whole() { + // Strings with brackets, nested arrays and a dictionary are one skipped token each; the + // operators around them still parse. + let src = b"{ 1 [ (a]b) [1 2] << /K [3 4] /S (>>) >> 5 ] 2 add << /A 1 >> mul }"; + let mut lx = crate::object::Lexer::new(src, 0); + let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() }; + assert_eq!(code, vec![PsOp::Num(1.0), PsOp::Num(2.0), PsOp::Op("add".into()), PsOp::Op("mul".into())]); + // A huge array is skipped once the cap is passed, not read to its end. + let src = format!("{{ [ {}] 1 }}", "0 ".repeat(MAX_CALC_OPS * 100)); + let mut lx = crate::object::Lexer::new(src.as_bytes(), 0); + let mut scanned = 0; + let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut scanned).into_iter().next() else { panic!() }; + assert_eq!(code, vec![PsOp::Num(1.0)]); + // A large string inside a skipped array is passed over, not decoded: escapes, nested + // parentheses, a hexadecimal string and names all end where the lexer would end them. + let big = format!("{{ [ ({}) <{}> /{} (a\\)b(c)) 7 ] 1 }}", "x".repeat(1 << 20), "ab".repeat(1 << 19), "N".repeat(1 << 10)); + let mut lx = crate::object::Lexer::new(big.as_bytes(), 0); + let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() }; + assert_eq!(code, vec![PsOp::Num(1.0)]); + // An unclosed one ends at the end of the data. + let mut lx = crate::object::Lexer::new(b"[ 1 2 ", 0); + lx.skip_compound(); + assert_eq!(lx.pos, 6); + } + + #[test] + fn skipped_calculator_tokens_count_toward_the_cap() { + // `null` is not a calculator token: skipped, but still scanned. + let src = format!("{{ {}}}", "null ".repeat(MAX_CALC_OPS + 10)); + let mut lx = crate::object::Lexer::new(src.as_bytes(), 0); + let mut scanned = 0; + ps_parse(&mut lx, 0, &mut scanned); + assert!(scanned > MAX_CALC_OPS && scanned < MAX_CALC_OPS + 10, "parsing stops once the cap is passed ({scanned})"); + } + #[test] fn calculator_functions() { // Two inputs → three outputs: { 2 copy mul 3 1 roll add 2 div dup 0.5 gt { pop 1 } if 0 } let code = b"{ 2 copy mul 3 1 roll add 2 div dup 0.5 gt { pop 1 } { 0.25 mul } ifelse 0 }"; let mut lx = crate::object::Lexer::new(code, 0); - let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0).into_iter().next() else { panic!() }; + let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() }; let f = Func::Calc { domain: vec![0.0, 1.0, 0.0, 1.0], range: vec![0.0, 1.0, 0.0, 1.0, 0.0, 1.0], code }; assert_eq!(f.eval(&[0.5, 0.4]), vec![0.2, 0.1125, 0.0]); assert_eq!(f.eval(&[1.0, 0.8]), vec![0.8, 1.0, 0.0]); @@ -771,7 +867,7 @@ mod tests { fn run(src: &str, mut st: Vec) -> Vec { let src = format!("{{ {src} }}"); let mut lx = crate::object::Lexer::new(src.as_bytes(), 0); - let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0).into_iter().next() else { panic!() }; + let Some(PsOp::Proc(code)) = ps_parse(&mut lx, 0, &mut 0).into_iter().next() else { panic!() }; ps_run(&code, &mut st, 0); st } diff --git a/crates/pdf/src/font.rs b/crates/pdf/src/font.rs index 64b0a1c3a..8bd91831e 100644 --- a/crates/pdf/src/font.rs +++ b/crates/pdf/src/font.rs @@ -211,6 +211,12 @@ fn utf16(b: &[u8]) -> String { String::from_utf16_lossy(&u) } +/// The most code → text mappings a CMap keeps (more than Unicode has code points), and the +/// longest destination string a `bfrange` is expanded with: a range of 65,536 codes would +/// otherwise multiply a long destination into gigabytes of text. +const MAX_UNICODE_MAPPINGS: usize = 1 << 20; +const MAX_RANGE_DST: usize = 256; + fn parse_cmap(data: &[u8]) -> CMap { let mut m = CMap::default(); let mut lx = Lexer::new(data, 0); @@ -239,6 +245,9 @@ fn parse_cmap(data: &[u8]) -> CMap { } "bfchar" => { for p in a.as_chunks::<2>().0 { + if m.unicode.len() >= MAX_UNICODE_MAPPINGS { + break; + } if let (Obj::Str(src), Obj::Str(dst)) = (&p[0], &p[1]) { m.unicode.insert(be(src), utf16(dst)); } @@ -252,9 +261,12 @@ fn parse_cmap(data: &[u8]) -> CMap { continue; } match &p[2] { - Obj::Str(dst) => { + Obj::Str(dst) if dst.len() <= MAX_RANGE_DST => { let mut d = dst.clone(); for c in lo..=hi { + if m.unicode.len() >= MAX_UNICODE_MAPPINGS { + break; + } m.unicode.insert(c, utf16(&d)); if let Some(last) = d.last_mut() { *last = last.wrapping_add(1); @@ -263,6 +275,9 @@ fn parse_cmap(data: &[u8]) -> CMap { } Obj::Array(list) => { for (k, s) in list.iter().enumerate() { + if m.unicode.len() >= MAX_UNICODE_MAPPINGS { + break; + } if let (Obj::Str(s), Some(c)) = (s, lo.checked_add(k as u32)) { m.unicode.insert(c, utf16(s)); } diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs index 567dca9c0..ff914e759 100644 --- a/crates/pdf/src/lib.rs +++ b/crates/pdf/src/lib.rs @@ -92,8 +92,25 @@ pub fn parse(bytes: &[u8]) -> Result { /// Parse page `index` of a PDF (EPS files have one page). pub fn parse_page(bytes: &[u8], index: usize) -> Result { + parse_page_framed(bytes, index, Framing::Viewer) +} + +/// Which region of a PDF page becomes the document, and whether `/Rotate` turns it. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Framing { + /// As a PDF viewer shows the page: its CropBox (else MediaBox), turned by `/Rotate`. + #[default] + Viewer, + /// As After Effects places a PDF or `.ai` as footage: the page's ArtBox clipped to the + /// CropBox (else MediaBox) when it has one, else the CropBox, else the MediaBox, with + /// `/Rotate` ignored (measured in After Effects 2026 with own files, #3099). + Placed, +} + +/// [`parse_page`] with the page framed as `framing` says (EPS files are their bounding box). +pub fn parse_page_framed(bytes: &[u8], index: usize, framing: Framing) -> Result { match sniff(bytes) { - Some(Format::Pdf) => parse_pdf(bytes, index), + Some(Format::Pdf) => parse_pdf(bytes, index, framing), Some(Format::Eps) => parse_eps(bytes), None => Err(Error::NotVector), } @@ -142,17 +159,22 @@ fn finish(nodes: Vec, flags: Vec, skipped: Vec, width: f64, Doc { width, height, root, skipped } } -fn parse_pdf(bytes: &[u8], index: usize) -> Result { +/// Page `index` of a PDF framed as `framing` says, as a document in points (y down). +fn parse_pdf(bytes: &[u8], index: usize, framing: Framing) -> Result { let file = object::File::parse(bytes)?; let pages = page::pages(&file); if pages.is_empty() { return Err(Error::NoPages); } let pg = pages.get(index).ok_or(Error::PageOutOfRange(index))?; - let [x0, y0, x1, y1] = pg.bbox; + let (frame, rotate) = match framing { + Framing::Viewer => (pg.bbox, pg.rotate), + Framing::Placed => (pg.art.unwrap_or(pg.bbox), 0), + }; + let [x0, y0, x1, y1] = frame; let (w, h) = (x1 - x0, y1 - y0); // Default user space (y up) → document pixels (y down), with the page's /Rotate. - let (to_doc, dw, dh) = match pg.rotate { + let (to_doc, dw, dh) = match rotate { 90 => (Affine::new([0.0, 1.0, 1.0, 0.0, -y0, -x0]), h, w), 180 => (Affine::new([-1.0, 0.0, 0.0, 1.0, x1, -y0]), w, h), 270 => (Affine::new([0.0, -1.0, -1.0, 0.0, y1, x1]), h, w), diff --git a/crates/pdf/src/object.rs b/crates/pdf/src/object.rs index 79a91dde8..df475a4bb 100644 --- a/crates/pdf/src/object.rs +++ b/crates/pdf/src/object.rs @@ -82,6 +82,91 @@ impl<'a> Lexer<'a> { } } + /// Whether the next token opens an array or a dictionary (skipping white space first). + pub fn at_compound(&mut self) -> bool { + self.skip_ws(); + match self.data.get(self.pos) { + Some(b'[') => true, + Some(b'<') => self.data.get(self.pos + 1) == Some(&b'<'), + _ => false, + } + } + + /// Skip the array or dictionary at the current position (see [`Lexer::at_compound`]) as + /// [`Lexer::next`] would read it, without building it: for readers that have no use for one + /// and must not allocate a vector per element of a hostile stream. + pub fn skip_compound(&mut self) { + let mut open = 0usize; + loop { + self.skip_ws(); + match self.data.get(self.pos) { + None => return, + Some(b'[') => { + open += 1; + self.pos += 1; + } + Some(b'<') if self.data.get(self.pos + 1) == Some(&b'<') => { + open += 1; + self.pos += 2; + } + Some(b']') | Some(b'>') if self.data[self.pos] == b']' || self.data.get(self.pos + 1) == Some(&b'>') => { + open = open.saturating_sub(1); + self.pos += if self.data[self.pos] == b']' { 1 } else { 2 }; + if open == 0 { + return; + } + } + // Anything else is one token (a string, name, number or word), passed over + // without building it. + Some(_) => self.skip_scalar(), + } + } + } + + /// Pass over one token that is not an array or dictionary bracket, as [`Lexer::next`] reads + /// it but without building its value. + fn skip_scalar(&mut self) { + let Some(&c) = self.data.get(self.pos) else { return }; + match c { + b'(' => { + // A literal string: balanced parentheses, a backslash escapes the next byte. + let mut depth = 0usize; + while let Some(&d) = self.data.get(self.pos) { + self.pos += 1; + match d { + b'\\' => self.pos += 1, + b'(' => depth += 1, + b')' => { + depth -= 1; + if depth == 0 { + break; + } + } + _ => {} + } + } + self.pos = self.pos.min(self.data.len()); + } + b'<' => { + // A hexadecimal string. + while self.data.get(self.pos).is_some_and(|d| *d != b'>') { + self.pos += 1; + } + self.pos = (self.pos + 1).min(self.data.len()); + } + b'/' => { + self.pos += 1; + self.word(); + } + b']' | b'{' | b'}' | b')' | b'>' => self.pos += 1, + _ => { + if self.word().is_empty() { + self.pos += 1; + } + } + } + } + fn word(&mut self) -> &'a [u8] { let s = self.pos; while self.pos < self.data.len() && !is_white(self.data[self.pos]) && !is_delim(self.data[self.pos]) { diff --git a/crates/pdf/src/page.rs b/crates/pdf/src/page.rs index b7eb70fe0..ba67b39aa 100644 --- a/crates/pdf/src/page.rs +++ b/crates/pdf/src/page.rs @@ -23,6 +23,9 @@ pub struct Page { pub resources: Dict, /// CropBox (else MediaBox) `[x0, y0, x1, y1]`. pub bbox: [f64; 4], + /// The page's own ArtBox clipped to [`Page::bbox`] (ISO 32000-1 §14.11.2), when it has one + /// that keeps an area there; not inherited (table 30). + pub art: Option<[f64; 4]>, pub rotate: i32, } @@ -31,13 +34,15 @@ pub fn pages(file: &File) -> Vec { let mut out = vec![]; let Some(cat) = file.catalog() else { return out }; let Some(root) = file.get_dict(cat, "Pages") else { return out }; + /// Collects the pages under `node`, passing on the attributes pages inherit (resources, + /// MediaBox, CropBox, /Rotate); bounded in depth and page count. fn walk(file: &File, node: &Dict, inherited: (Option, Option>, Option>, i32), out: &mut Vec, depth: usize) { if depth > 32 || out.len() > 10_000 { return; } let res = file.get_dict(node, "Resources").cloned().or(inherited.0); - let media = file.get(node, "MediaBox").map(|m| file.nums(m)).filter(|v| v.len() == 4).or(inherited.1); - let crop = file.get(node, "CropBox").map(|m| file.nums(m)).filter(|v| v.len() == 4).or(inherited.2); + let media = file.get(node, "MediaBox").map(|m| file.nums(m)).filter(|v| is_area(v)).or(inherited.1); + let crop = file.get(node, "CropBox").map(|m| file.nums(m)).filter(|v| is_area(v)).or(inherited.2); let rot = file.get_num(node, "Rotate").map(|r| r as i32).unwrap_or(inherited.3); match file.get(node, "Kids").and_then(Obj::array) { Some(kids) if file.get(node, "Type").and_then(Obj::name) != Some("Page") => { @@ -49,8 +54,16 @@ pub fn pages(file: &File) -> Vec { } _ => { let b = crop.or(media).unwrap_or_else(|| vec![0.0, 0.0, 612.0, 792.0]); - let bbox = [b[0].min(b[2]), b[1].min(b[3]), b[0].max(b[2]), b[1].max(b[3])]; - out.push(Page { dict: node.clone(), resources: res.unwrap_or_default(), bbox, rotate: rot.rem_euclid(360) }); + let bbox = normalized(&b).unwrap_or([0.0, 0.0, 612.0, 792.0]); + // The ArtBox is the page's own (not inherited), clipped to the crop box. + let art = file + .get(node, "ArtBox") + .map(|m| file.nums(m)) + .filter(|v| is_area(v)) + .and_then(|v| normalized(&v)) + .map(|a| [a[0].max(bbox[0]), a[1].max(bbox[1]), a[2].min(bbox[2]), a[3].min(bbox[3])]) + .filter(|a| a[2] > a[0] && a[3] > a[1]); + out.push(Page { dict: node.clone(), resources: res.unwrap_or_default(), bbox, art, rotate: rot.rem_euclid(360) }); } } } @@ -58,6 +71,26 @@ pub fn pages(file: &File) -> Vec { out } +/// A box `[x0, y0, x1, y1]` (corners in any order) with finite corners and a finite span that +/// encloses an area. +fn is_area(v: &[f64]) -> bool { + match v { + [x0, y0, x1, y1] => { + let (w, h) = (x1 - x0, y1 - y0); + v.iter().all(|c| c.is_finite()) && w.is_finite() && h.is_finite() && w.abs() > 0.0 && h.abs() > 0.0 + } + _ => false, + } +} + +/// A box with its corners ordered (`x0 < x1`, `y0 < y1`), `None` unless [`is_area`]. +fn normalized(v: &[f64]) -> Option<[f64; 4]> { + match v { + [x0, y0, x1, y1] if is_area(v) => Some([x0.min(*x1), y0.min(*y1), x0.max(*x1), y0.max(*y1)]), + _ => None, + } +} + /// The page's content streams, decoded and joined. pub fn page_content(file: &File, page: &Page) -> Vec { let mut out = vec![]; @@ -636,7 +669,12 @@ impl<'a> Interp<'a> { let bc = f.get(&sm, "BC").map(|b| f.nums(b)).unwrap_or_default(); let gcs = f.get_dict(&gd, "Group").and_then(|gr| f.get(gr, "CS")).map(|c| color_space(f, c, Some(res))).unwrap_or(Cs::Rgb); let backdrop = if bc.is_empty() { [0.0; 3] } else { gcs.to_rgb(&bc) }; - let saved_b = std::mem::replace(&mut self.b, Builder::new()); + // The mask it replaces closes first: its groups don't enclose the new one. + if let Some(d) = self.gs.mask_depth { + self.close_to(d); + } + let inner = self.b.nested(2); + let saved_b = std::mem::replace(&mut self.b, inner); let ctm = self.gs.ctm; let saved_gs = std::mem::replace(&mut self.gs, GState { ctm, ..GState::default() }); let saved_stack = std::mem::take(&mut self.saved); @@ -648,9 +686,6 @@ impl<'a> Interp<'a> { for s in sk { self.b.skip(&s); } - if let Some(d) = self.gs.mask_depth { - self.close_to(d); - } let mut content = Group::new("Soft Mask"); content.children = nodes; let mut grp = Group::new("Soft Mask Group"); @@ -751,8 +786,15 @@ impl<'a> Interp<'a> { if depth > 8 { return; } + // The `Pattern` and `Tile` wrappers must fit under the open groups too. + if !self.b.has_room(2) { + self.b.skip("group nesting limit"); + return; + } // The cell, drawn once in pattern space. - let saved_b = std::mem::replace(&mut self.b, Builder::new()); + // The cell ends up under the open groups and this pattern's `Pattern` and `Tile`. + let inner = self.b.nested(2); + let saved_b = std::mem::replace(&mut self.b, inner); let saved_gs = std::mem::take(&mut self.gs); let saved_stack = std::mem::take(&mut self.saved); let saved_path = std::mem::take(&mut self.path); diff --git a/crates/pdf/src/ps/builtin.rs b/crates/pdf/src/ps/builtin.rs index dcafa9417..a19aeb6da 100644 --- a/crates/pdf/src/ps/builtin.rs +++ b/crates/pdf/src/ps/builtin.rs @@ -1,4 +1,5 @@ use super::eps::find_from; +use super::exec::MAX_STACK; use super::scanner::Src; use super::text::encoding_array; use super::value::{Flow, V, array, matrix_of, matrix_val, new_dict}; @@ -181,8 +182,13 @@ impl Interp { match self.pop() { V::Num(k) => { let k = (k.max(0.0) as usize).min(self.st.len()); - let tail: Vec = self.st[self.st.len() - k..].to_vec(); - self.st.extend(tail); + // A copy past the operand limit copies nothing, and the values copied + // count as operations (one for eight): repeated doubling spends the budget. + if self.st.len().saturating_add(k) <= MAX_STACK { + self.charge(k / 8); + let tail: Vec = self.st[self.st.len() - k..].to_vec(); + self.st.extend(tail); + } } V::Array(dst) => { let s = self.pop().items().unwrap_or_default(); @@ -507,12 +513,16 @@ impl Interp { fn try_collection_op(&mut self, n: &str) -> bool { match n { // Arrays and strings. + // The storage made counts as operations (a value, or eight bytes, each), so a loop + // of large constructors spends the budget rather than the memory. "array" => { let k = self.popn().clamp(0.0, 1e6) as usize; + self.charge(k); self.push(array(vec![V::Null; k])); } "string" => { let k = self.popn().clamp(0.0, 1e7) as usize; + self.charge(k / 8); self.push(V::Str(Rc::new(RefCell::new(vec![0; k])))); } "length" => { diff --git a/crates/pdf/src/ps/eps.rs b/crates/pdf/src/ps/eps.rs index 90d60cfbe..e89b1d276 100644 --- a/crates/pdf/src/ps/eps.rs +++ b/crates/pdf/src/ps/eps.rs @@ -1,38 +1,26 @@ use super::{HashMap, Rc, is_white}; /// The PostScript section of an EPS file (DOS EPS binary header handled) and its bounding box -/// (`%%HiResBoundingBox` preferred). +/// (`%%HiResBoundingBox` preferred), read with `craft-eps`: the header comments, and the trailer +/// for a box deferred with `(atend)`. A binary header that points outside the file is refused. pub fn eps_parts(bytes: &[u8]) -> Option<(&[u8], [f64; 4])> { - let ps = if bytes.starts_with(&[0xC5, 0xD0, 0xD3, 0xC6]) && bytes.len() >= 12 { - let off = u32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]) as usize; - let len = u32::from_le_bytes([bytes[8], bytes[9], bytes[10], bytes[11]]) as usize; - bytes.get(off..off.checked_add(len)?.min(bytes.len()))? - } else { - bytes - }; - let find_box = |key: &[u8]| -> Option<[f64; 4]> { - let mut from = 0; - while let Some(p) = ps[from..].windows(key.len()).position(|w| w == key) { - let s = from + p + key.len(); - let e = ps[s..].iter().position(|c| matches!(c, b'\n' | b'\r')).map_or(ps.len(), |q| s + q); - let v: Vec = String::from_utf8_lossy(&ps[s..e]).split_whitespace().filter_map(|t| t.parse().ok()).collect(); - if v.len() == 4 && v[2] > v[0] && v[3] > v[1] { - return Some([v[0], v[1], v[2], v[3]]); - } - from = s; - } - None - }; - let bbox = find_box(b"%%HiResBoundingBox:").or_else(|| find_box(b"%%BoundingBox:"))?; - Some((ps, bbox)) + let ps = craft_eps::sections(bytes).ok()?.postscript; + Some((ps, craft_eps::Comments::read(ps).page_box()?)) } pub(super) fn find_from(hay: &[u8], needle: &[u8], from: usize) -> Option { hay.get(from..)?.windows(needle.len()).position(|w| w == needle).map(|p| p + from) } -fn rfind_before(hay: &[u8], needle: &[u8], to: usize) -> Option { - hay.get(..to)?.windows(needle.len()).rposition(|w| w == needle) +/// Every position of `needle` in `hay`, in order (one pass over the file). +fn positions(hay: &[u8], needle: &[u8]) -> Vec { + let mut out = vec![]; + let mut from = 0; + while let Some(p) = find_from(hay, needle, from) { + out.push(p); + from = p + needle.len(); + } + out } /// The name after `key` (`/FontName /Name`). @@ -48,20 +36,26 @@ fn name_after(b: &[u8], key: &[u8]) -> Option { /// … cleartomark`, hexadecimal or binary) and CFF font sets (`… StartData `). pub(super) fn embedded_fonts(ps: &[u8]) -> HashMap>, bool)> { let mut out = HashMap::new(); + // The headers are found once, in one pass each; every `eexec` then takes the last header + // before it that no earlier `eexec` has used, so no prefix of the file is searched again. + let mut heads = positions(ps, b"%!PS-AdobeFont"); + heads.extend(positions(ps, b"%!FontType1")); + heads.sort_unstable(); + let names = positions(ps, b"/FontName"); let mut from = 0; + let mut used = 0; while let Some(ee) = find_from(ps, b"eexec", from) { from = ee + 5; - let start = [b"%!PS-AdobeFont".as_slice(), b"%!FontType1"] - .iter() - .filter_map(|h| rfind_before(ps, h, ee)) - .max() - .or_else(|| rfind_before(ps, b"/FontName", ee).map(|p| p.saturating_sub(512))); + let last_before = |v: &[usize]| v[..v.partition_point(|&p| p < ee)].last().copied().filter(|&p| p >= used); + // The fallback look-back stops at the previous font's end, so it cannot pick up its header. + let start = last_before(&heads).or_else(|| last_before(&names).map(|p| p.saturating_sub(512).max(used))); let Some(start) = start else { continue }; let end = find_from(ps, b"cleartomark", ee).map_or(ps.len(), |p| p + 11); if let Some(name) = name_after(&ps[start..ee], b"/FontName") { out.insert(name, (Rc::new(ps[start..end].to_vec()), false)); } from = end; + used = end; } let mut from = 0; while let Some(sd) = find_from(ps, b"StartData", from) { @@ -77,3 +71,30 @@ pub(super) fn embedded_fonts(ps: &[u8]) -> HashMap>, bool)> } out } + +#[cfg(test)] +mod tests { + use super::embedded_fonts; + + #[test] + fn font_discovery_looks_back_once_per_header() { + // 20,000 `eexec` words before any font header: each used to search the whole prefix + // for a header again. The font after them is still found. + let mut ps = b"%!PS-Adobe-3.0 EPSF-3.0\n".to_vec(); + ps.extend_from_slice("eexec ".repeat(20_000).as_bytes()); + ps.extend_from_slice(b"\n%!PS-AdobeFont-1.0: Real\n/FontName /Real def\ncurrentfile eexec\n0000\ncleartomark\n"); + let fonts = embedded_fonts(&ps); + assert_eq!(fonts.len(), 1); + assert!(fonts.contains_key("Real")); + } + + #[test] + fn header_less_font_after_a_short_one_does_not_take_its_header() { + let a = "%!PS-AdobeFont-1.0: A\n/FontName /A def\ncurrentfile eexec\n0000\ncleartomark\n"; + let b = "/FontName /B def\ncurrentfile eexec\n0000\ncleartomark\n"; + let ps = format!("%!PS-Adobe-3.0 EPSF-3.0\n{a}{b}"); + let fonts = embedded_fonts(ps.as_bytes()); + let (data, _) = fonts.get("B").expect("second font found"); + assert!(!String::from_utf8_lossy(data).contains("AdobeFont"), "{}", String::from_utf8_lossy(data)); + } +} diff --git a/crates/pdf/src/ps/exec.rs b/crates/pdf/src/ps/exec.rs index 27697527c..233ff82b0 100644 --- a/crates/pdf/src/ps/exec.rs +++ b/crates/pdf/src/ps/exec.rs @@ -6,6 +6,10 @@ use super::{Affine, BezPath, Builder, Cap, HashMap, Interp, Join}; /// The deepest procedure nesting run (deeper recursion stops the program). const MAX_CALL_DEPTH: usize = 256; +/// The most values the operand stack holds: a push past it is dropped, and `copy` copies +/// nothing rather than grow the stack past it. +pub(super) const MAX_STACK: usize = 100_000; + impl Interp { pub fn new(page: [f64; 4]) -> Interp { Interp { @@ -56,11 +60,17 @@ impl Interp { } pub(super) fn push(&mut self, v: V) { - if self.st.len() < 100_000 { + if self.st.len() < MAX_STACK { self.st.push(v); } } + /// Count `n` units of work (values copied, storage allocated) against the budget without + /// stopping the program here: once it is spent, the next operator stops it. + pub(super) fn charge(&mut self, n: usize) { + self.budget = self.budget.saturating_sub(n); + } + pub(super) fn lookup(&self, k: &str) -> Option { self.dicts.iter().rev().find_map(|d| d.borrow().get(k).cloned()) } diff --git a/crates/pdf/src/ps/paths.rs b/crates/pdf/src/ps/paths.rs index b1fe07092..30b5a7087 100644 --- a/crates/pdf/src/ps/paths.rs +++ b/crates/pdf/src/ps/paths.rs @@ -22,7 +22,7 @@ impl Interp { } /// Stop the program when a path of `len` elements would be longer than [`MAX_PATH`]. - fn path_fits(&mut self, len: usize) -> Result<(), Flow> { + pub(super) fn path_fits(&mut self, len: usize) -> Result<(), Flow> { if len <= MAX_PATH { return Ok(()); } diff --git a/crates/pdf/src/ps/text.rs b/crates/pdf/src/ps/text.rs index b138965bb..d4cee07f8 100644 --- a/crates/pdf/src/ps/text.rs +++ b/crates/pdf/src/ps/text.rs @@ -95,7 +95,14 @@ impl Interp { let ctm = self.gs.ctm; for (i, &code) in bytes.iter().enumerate() { let code = code as u32; + // Every input character costs at least one unit, so empty outlines are not free. + self.spend(1)?; if let Some(g) = font.glyph(code) { + // Each outline element counts against the path limit and the budget before it + // is copied, so a long string cannot expand into unbounded geometry. + let n = g.elements().len(); + self.path_fits(path.elements().len().saturating_add(n))?; + self.spend(n)?; path.extend(ctm * Affine::translate(p.to_vec2()) * m * (*g).clone()); } text.push_str(&font.unicode(code)); diff --git a/crates/pdf/src/shading.rs b/crates/pdf/src/shading.rs index df5e5f9c7..aa83adebf 100644 --- a/crates/pdf/src/shading.rs +++ b/crates/pdf/src/shading.rs @@ -18,6 +18,8 @@ const MAX_SIDE: f64 = 1024.0; const MAX_SCALE: f64 = 4.0; /// The most triangles one shading draws. const MAX_TRIANGLES: usize = 2_000_000; +/// How many times over the mesh raster may be covered by triangle bounding boxes in all. +const MAX_RASTER_PASSES: usize = 64; /// A mesh vertex: page-space position and its value (RGB, or `t` in `[0]` with a function). #[derive(Clone, Copy, Debug, PartialEq)] @@ -234,13 +236,17 @@ fn free_form(r: &mut Bits, l: &Layout, colors: &Colors, m: Affine) -> Vec<[V; 3] /// Type 5: a lattice of `per_row` vertices per row, two triangles per cell. fn lattice(r: &mut Bits, l: &Layout, colors: &Colors, m: Affine, per_row: usize) -> Vec<[V; 3]> { - if per_row < 2 { + if !(2..=MAX_TRIANGLES).contains(&per_row) { return vec![]; } let mut rows: Vec> = vec![]; let mut row = vec![]; while let (Some(p), Some(c)) = (l.point(r), l.comps(r)) { r.align(); + // Vertices kept, the unfinished row's included, stay within the triangle limit. + if (rows.len() * per_row).saturating_add(row.len()) >= MAX_TRIANGLES { + break; + } row.push(V { p: m * p, c: colors.value(&c) }); if row.len() == per_row { rows.push(std::mem::take(&mut row)); @@ -447,6 +453,9 @@ fn rasterize(tris: &[[V; 3]], colors: &Colors, page_box: [f64; 4]) -> Option Option Vec { + let content = "0.5 g 0 0 420 220 re f 0 0 1 rg 64 54 20 10 re f"; + pdf( + &[ + (1, "<< /Type /Catalog /Pages 2 0 R >>".to_string(), None), + (2, format!("<< /Type /Pages /Kids [3 0 R] /Count 1 {pages} >>"), None), + (3, format!("<< /Type /Page /Parent 2 0 R /MediaBox [0 0 420 220] {page} /Contents 4 0 R >>"), None), + (4, "<< >>".to_string(), Some(content.as_bytes().to_vec())), + ], + 1, + ) +} + +/// The first page of `bytes` framed as After Effects places it. +fn placed(bytes: &[u8]) -> Doc { + parse_page_framed(bytes, 0, Framing::Placed).unwrap() +} + +/// #3099: After Effects 2026 places a .ai or PDF as footage by the page's ArtBox and shows only +/// that region (a page with MediaBox 420 × 220, CropBox 410 × 210, TrimBox 380 × 180 and ArtBox +/// [60 50 300.6 170.4] imports as 241 × 121). `Framing::Placed` frames it so; the viewer framing +/// (the default, used by File ▸ Import) stays the CropBox. +#[test] +fn placed_framing_is_the_art_box() { + let bytes = boxes_pdf("/CropBox [5 5 415 215]", "/TrimBox [20 20 400 200] /ArtBox [60 50 300.6 170.4]"); + let doc = placed(&bytes); + assert!((doc.width - 240.6).abs() < 1e-9 && (doc.height - 120.4).abs() < 1e-9, "{} × {}", doc.width, doc.height); + assert_eq!(doc.pixel_size(), (241, 121)); + // The art box's top-left corner is the document's origin: the marker (PDF x 64..84, y 54..64) + // is at x 4..24, y 106.4..116.4 from the top. + let (w, h) = doc.pixel_size(); + let img = effectcraft_svg::rasterize(&doc, w, h, 1.0); + let marker = img.get(10, 110); + assert!(marker[2] > 0.99 && marker[0] < 0.01, "{marker:?}"); + assert!(img.get(30, 110)[2] < 0.6, "{:?}", img.get(30, 110)); + let viewer = parse(&bytes).unwrap(); + assert_eq!((viewer.width, viewer.height), (410.0, 210.0), "the viewer framing is unchanged"); +} + +/// The ArtBox is clipped to the crop box (ISO 32000-1 §14.11.2); one that keeps no area there, +/// a degenerate or unreadable one, or one on the /Pages node (not inherited, table 30) leaves the +/// crop box (else the media box). +#[test] +fn placed_framing_clips_the_art_box_and_falls_back() { + let size = |pages: &str, page: &str| { + let d = placed(&boxes_pdf(pages, page)); + (d.width, d.height) + }; + let crop = "/CropBox [5 5 415 215]"; + assert_eq!(size(crop, "/ArtBox [-100 -100 1000 1000]"), (410.0, 210.0), "larger than the page"); + assert_eq!(size(crop, "/ArtBox [300 100 900 900]"), (115.0, 115.0), "partly outside"); + let (w, h) = size(crop, "/ArtBox [300.6 170.4 60 50]"); + assert!((w - 240.6).abs() < 1e-9 && (h - 120.4).abs() < 1e-9, "corners in any order: {w} × {h}"); + // Finite corners whose span is not. + let big = format!("1{}", "0".repeat(308)); + let unreadable = [format!("/ArtBox [-{big} 50 {big} 170]")]; + for page in ["/ArtBox [500 300 600 400]", "/ArtBox [60 50 60 170]", "/ArtBox [60 50 300]"].map(String::from).iter().chain(&unreadable) { + assert_eq!(size(crop, page), (410.0, 210.0), "{page}"); + } + assert_eq!(size("/ArtBox [60 50 300 170]", ""), (420.0, 220.0), "an ArtBox on /Pages is not inherited"); + // A crop box that is not a box leaves the media box. + assert_eq!(size("", &format!("/CropBox [-{big} 5 {big} 215]")), (420.0, 220.0)); +} + +/// After Effects ignores /Rotate on placed footage (a page [100 50 400 150] with /Rotate 90 +/// imports as 300 × 100, drawn unturned); the viewer framing turns it. +#[test] +fn placed_framing_ignores_rotate() { + let bytes = boxes_pdf("", "/Rotate 90"); + let doc = placed(&bytes); + assert_eq!((doc.width, doc.height), (420.0, 220.0)); + // Unturned: the marker at PDF (64..84, 54..64) is at x 64..84, y 156..166 from the top. + let img = effectcraft_svg::rasterize(&doc, 420, 220, 1.0); + assert!(img.get(70, 160)[2] > 0.99, "{:?}", img.get(70, 160)); + let viewer = parse(&bytes).unwrap(); + assert_eq!((viewer.width, viewer.height), (220.0, 420.0)); +} + const EPS: &str = "%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 100 100\n%%EndComments\n\ /m {moveto} bind def /l {lineto} bind def\n\ /cm { 6 array astore concat } bind def\n\ @@ -668,6 +748,63 @@ fn tiling_patterns_coloured_and_uncoloured() { assert_eq!(at(115, 15)[3], 0.0); } +/// A pattern cell nests under the page's open clipping groups and the pattern's own groups, and +/// the render tree stays within the group depth limit that counts all of them. +#[test] +fn a_pattern_cell_counts_the_groups_it_is_nested_in() { + fn depth(g: &effectcraft_svg::Group) -> usize { + 1 + g.children.iter().filter_map(|c| if let effectcraft_svg::Node::Group(g) = c { Some(depth(g)) } else { None }).max().unwrap_or(0) + } + let clips = "0 0 200 100 re W n ".repeat(250); + let cell = format!("{} 1 0 0 rg 0 0 10 10 re f", "0 0 20 20 re W n ".repeat(250)).into_bytes(); + let bytes = page_pdf( + &format!("{clips} /Pattern cs /P1 scn 0 0 100 100 re f"), + "/Pattern << /P1 10 0 R >>", + vec![(10, "<< /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 20 20] /XStep 20 /YStep 20 /Resources << >> >>".into(), Some(cell))], + ); + let doc = parse(&bytes).unwrap(); + assert!(depth(&doc.root) <= 260, "{}", depth(&doc.root)); + assert!(doc.skipped.iter().any(|s| s == "group nesting limit"), "{:?}", doc.skipped); +} + +/// A pattern whose `Pattern` and `Tile` wrappers would pass the depth limit is skipped, not built +/// past it. +#[test] +fn a_pattern_whose_wrappers_pass_the_depth_limit_is_skipped() { + let clips = "0 0 200 100 re W n ".repeat(255); + let bytes = page_pdf( + &format!("{clips} /Pattern cs /P1 scn 0 0 100 100 re f"), + "/Pattern << /P1 10 0 R >>", + vec![( + 10, + "<< /Type /Pattern /PatternType 1 /PaintType 1 /TilingType 1 /BBox [0 0 20 20] /XStep 20 /YStep 20 /Resources << >> >>".into(), + Some(b"0 0 10 10 re f".to_vec()), + )], + ); + let doc = parse(&bytes).unwrap(); + assert!(doc.skipped.iter().any(|s| s == "group nesting limit"), "{:?}", doc.skipped); +} + +/// A soft mask replacing another is nested under what is open once the old one has closed, so +/// groups inside the old mask don't count against the new mask's content. +#[test] +fn a_replaced_soft_mask_does_not_count_against_the_new_one() { + let form = |n: &str| { + ( + n.parse().unwrap(), + "<< /Type /XObject /Subtype /Form /BBox [0 0 200 100] /Group << /S /Transparency /CS /DeviceGray >> >>".to_string(), + Some(format!("{} 1 g 0 0 100 100 re f", "0 0 200 100 re W n ".repeat(250)).into_bytes()), + ) + }; + let bytes = page_pdf( + &format!("/GS1 gs {} /GS2 gs 1 0 0 rg 0 0 200 100 re f", "0 0 200 100 re W n ".repeat(250)), + "/ExtGState << /GS1 << /SMask << /S /Luminosity /G 10 0 R >> >> /GS2 << /SMask << /S /Luminosity /G 11 0 R >> >> >>", + vec![form("10"), form("11")], + ); + let doc = parse(&bytes).unwrap(); + assert!(doc.skipped.iter().all(|s| s != "group nesting limit"), "{:?}", doc.skipped); +} + /// Build isolated interpreter objects without presenting a function or colour space as a /// document catalog. Complete-document parsing is covered by the page and reader fixtures. fn object_file(objs: Objs) -> object::File { @@ -988,6 +1125,35 @@ fn a_fanning_out_reference_graph_is_refused_within_the_budget() { assert_eq!(read_function(objs), color::Func::Unsupported); } +#[test] +fn a_sampled_function_shorter_than_its_table_is_refused() { + // 4,096 8-bit samples declared, ten bytes given: the missing ones are not made up. + let f = "<< /FunctionType 0 /Domain [0 1] /Size [4096] /Range [0 1] /BitsPerSample 8 >>"; + assert_eq!(read_function(vec![(1, f.into(), Some(vec![7; 10]))]), color::Func::Unsupported); + assert_ne!(read_function(vec![(1, f.into(), Some(vec![7; 4096]))]), color::Func::Unsupported); +} + +#[test] +fn a_calculator_program_past_the_operator_limit_is_refused() { + let f = "<< /FunctionType 4 /Domain [0 1] /Range [0 1] >>"; + let long = format!("{{ {} }}", "1 pop ".repeat(3000)); + assert_eq!(read_function(vec![(1, f.into(), Some(long.into_bytes()))]), color::Func::Unsupported); + assert_ne!(read_function(vec![(1, f.into(), Some(b"{ 1 exch sub }".to_vec()))]), color::Func::Unsupported); +} + +#[test] +fn copies_of_a_function_are_charged_for_its_coefficients() { + // A stitching function listing one object 1,000 times, whose coefficient arrays hold 2,000 + // values: each copy is charged its values, so the budget refuses it instead of copying + // two million values. + let c = format!("[{}]", "0.5 ".repeat(1000)); + let objs: Objs = vec![ + (1, format!("<< /FunctionType 3 /Domain [0 1] /Functions [{}] >>", "2 0 R ".repeat(1000)), None), + (2, format!("<< /FunctionType 2 /Domain [0 1] /C0 {c} /C1 {c} /N 1 >>"), None), + ]; + assert_eq!(read_function(objs), color::Func::Unsupported); +} + #[test] fn colour_space_chain_of_depth_3() { // Indexed → Separation → ICCBased → DeviceRGB: entry 0 is tint 1 (blue), entry 1 tint 0 (white). diff --git a/crates/pdf/src/tests/import.rs b/crates/pdf/src/tests/import.rs index c7dcbee57..f8c6509c2 100644 --- a/crates/pdf/src/tests/import.rs +++ b/crates/pdf/src/tests/import.rs @@ -513,6 +513,9 @@ fn paths_stop_at_the_path_limit() { // The outlines of 100,000 H's, 13 elements each. "/Helvetica findfont 10 scalefont setfont /s 100000 string def 0 1 99999 { s exch 72 put } for \ 0 0 moveto s false charpath", + // The same outlines shown: the text path is bounded like the current path. + "/Helvetica findfont 10 scalefont setfont /s 100000 string def 0 1 99999 { s exch 72 put } for \ + 0 0 moveto s show", // 210,000 rectangles of five elements each. "/a 840000 array def 0 1 839999 { a exch 0 put } for a rectfill", ] { @@ -528,6 +531,46 @@ fn paths_stop_at_the_path_limit() { assert_eq!(doc.flatten().len(), 1); } +#[test] +fn copies_past_the_operand_limit_copy_nothing() { + // `count copy` doubles the operand stack: 40 doublings would be 2^40 values. A copy that would + // pass the operand limit (100,000) copies nothing, so the program runs on to its end. + let doc = eps_soon("0 0 10 10 rectfill 1 40 { count copy } repeat clear 20 20 10 10 rectfill"); + assert!(doc.skipped.is_empty(), "{:?}", doc.skipped); + assert_eq!(doc.flatten().len(), 2); +} + +#[test] +fn storage_made_in_a_loop_runs_out_of_the_budget() { + // Each value of an array and each eight bytes of a string count as an operation when they + // are made, so a loop keeping large arrays or strings spends the budget, not the memory. + for body in ["/a 1000 array def 0 1 999 { a exch 1000000 array put } for", "/a 1000 array def 0 1 999 { a exch 10000000 string put } for"] { + runs_out_of_budget(body); + } +} + +#[test] +fn groups_stop_nesting_at_the_depth_limit() { + // Every `rectclip` opens a clipping group inside the last: past 256 open groups the group is + // dropped (and noted), and what is drawn inside goes to the open one. + let doc = eps_soon("300 { 0 0 100 100 rectclip } repeat 0 0 10 10 rectfill"); + assert_eq!(doc.skipped, ["group nesting limit"]); + assert_eq!(doc.flatten().len(), 1); +} + +#[test] +fn bounding_box_comments_are_scanned_once() { + // A rejected `%%BoundingBox:` comment is not scanned again from each later marker on its + // line: 20,000 markers on one line would otherwise take quadratic work. + let mut src = b"%!PS-Adobe-3.0 EPSF-3.0\n".to_vec(); + // Keep the valid comment inside craft-eps's bounded header window. + src.extend_from_slice("%%BoundingBox: x ".repeat(3_000).as_bytes()); + src.extend_from_slice(b"\n%%BoundingBox: 0 0 50 50\n0 0 10 10 rectfill\n"); + let doc = parse(&src).unwrap(); + assert_eq!((doc.width, doc.height), (50.0, 50.0)); + assert_eq!(doc.flatten().len(), 1); +} + #[test] fn pdf_paths_stop_at_the_path_limit() { // A content stream's current path holds at most 1,048,576 elements: `re` (five elements each) diff --git a/crates/pdf/src/tests/shading.rs b/crates/pdf/src/tests/shading.rs index 8e96193ab..4b5f9645d 100644 --- a/crates/pdf/src/tests/shading.rs +++ b/crates/pdf/src/tests/shading.rs @@ -262,3 +262,32 @@ fn corrupt_mesh_data_draws_what_parses() { let doc = parse(&bytes).unwrap(); assert!(doc.skipped.iter().any(|s| s.contains("layout")), "{:?}", doc.skipped); } + +#[test] +fn overlapping_mesh_triangles_stop_at_the_raster_budget() { + // 20,000 triangles each covering the whole page would test every sample 20,000 times; the + // raster budget stops after about 64 passes, and the shading is still drawn. + let mut m = Mesh::default(); + for _ in 0..20_000 { + m.flag(0).pt(0, 0).c(&[255, 0, 0]); + m.flag(0).pt(400, 0).c(&[255, 0, 0]); + m.flag(0).pt(0, 400).c(&[255, 0, 0]); + } + let sh = format!("<< /ShadingType 4 /ColorSpace /DeviceRGB /BitsPerCoordinate 16 /BitsPerComponent 8 /BitsPerFlag 8 {RGB_DECODE} >>"); + let bytes = page_pdf("/Sh sh", "/Shading << /Sh 10 0 R >>", vec![(10, sh, Some(m.0))]); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || tx.send(parse(&bytes)).ok()); + let doc = rx.recv_timeout(std::time::Duration::from_secs(20)).expect("the mesh finishes").unwrap(); + assert!(render(&doc).get(50, 50)[0] > 0.99); +} + +#[test] +fn a_lattice_row_longer_than_the_triangle_limit_draws_nothing() { + let mut m = Mesh::default(); + for i in 0..8 { + m.pt(i * 10, 0).c(&[255, 0, 0]); + } + let sh = format!("<< /ShadingType 5 /ColorSpace /DeviceRGB /BitsPerCoordinate 16 /BitsPerComponent 8 /VerticesPerRow 3000000 {RGB_DECODE} >>"); + let doc = parse(&page_pdf("/Sh sh", "/Shading << /Sh 10 0 R >>", vec![(10, sh, Some(m.0))])).unwrap(); + assert!(doc.flatten().is_empty()); +} diff --git a/crates/pdf/src/type1.rs b/crates/pdf/src/type1.rs index 4b3e05756..399905722 100644 --- a/crates/pdf/src/type1.rs +++ b/crates/pdf/src/type1.rs @@ -148,8 +148,9 @@ impl Type1 { }; if let Some(p) = find(&priv_, b"/Subrs", 0) { let mut q = p + 6; + // The section ends at `/CharStrings`, found once rather than for every entry. + let lim = find(&priv_, b"/CharStrings", q).unwrap_or(priv_.len()); while let Some(d) = find(&priv_, b"dup", q) { - let lim = find(&priv_, b"/CharStrings", q).unwrap_or(priv_.len()); if d > lim { break; } @@ -205,10 +206,11 @@ impl Type1 { /// A glyph's outline in glyph space and its advance width. pub fn outline(&self, name: &str) -> Option<(BezPath, f64)> { - self.outline_depth(name, 0) + self.outline_depth(name, 0, &mut { crate::cff::MAX_GLYPH_OPS }) } - fn outline_depth(&self, name: &str, depth: usize) -> Option<(BezPath, f64)> { + /// `ops` is the glyph's instruction budget, shared by the base glyph and its seac components. + fn outline_depth(&self, name: &str, depth: usize, ops: &mut usize) -> Option<(BezPath, f64)> { let cs = self.charstrings.get(name)?; let mut st = T1 { font: self, @@ -223,9 +225,11 @@ impl Type1 { flex: None, done: false, depth: 0, + ops: *ops, seac: None, }; st.run(cs); + *ops = st.ops; if st.open { st.path.close_path(); } @@ -234,10 +238,10 @@ impl Type1 { && depth < 2 { let name_of = |c: u8| crate::encoding::Base::Standard.name(c); - if let Some((bp, _)) = name_of(b).and_then(|n| self.outline_depth(&n, depth + 1)) { + if let Some((bp, _)) = name_of(b).and_then(|n| self.outline_depth(&n, depth + 1, ops)) { path.extend(bp); } - if let Some((ap, _)) = name_of(a).and_then(|n| self.outline_depth(&n, depth + 1)) { + if let Some((ap, _)) = name_of(a).and_then(|n| self.outline_depth(&n, depth + 1, ops)) { // The accent's origin: its own sidebearing is replaced by `asb`. path.extend(kurbo::Affine::translate((adx - asb + st.sbx, ady)) * ap); } @@ -270,6 +274,8 @@ struct T1<'a> { flex: Option>, done: bool, depth: usize, + /// Instructions this glyph may still run, across its subroutine calls. + ops: usize, seac: Option<(f64, f64, f64, u8, u8)>, } @@ -314,6 +320,12 @@ impl T1<'_> { } let mut i = 0; while i < cs.len() && !self.done { + // Subroutines that branch would multiply the work: the budget is shared. + let Some(left) = self.ops.checked_sub(1) else { + self.done = true; + return; + }; + self.ops = left; let b0 = cs[i]; i += 1; let s = |st: &Self, k: usize| st.stack.get(k).copied().unwrap_or(0.0); @@ -533,6 +545,25 @@ mod tests { assert_eq!(p.bounding_box(), kurbo::Rect::new(50.0, 0.0, 550.0, 500.0)); } + /// A subroutine that calls itself ten times: 10^10 calls within the depth limit of ten. The + /// glyph stops at its instruction budget instead. + #[test] + fn branching_subroutines_stop_at_the_instruction_budget() { + let mut out = b"%!PS-AdobeFont-1.0: Test 001\n/FontMatrix [0.001 0 0 0.001 0 0] readonly def\ncurrentfile eexec\n".to_vec(); + // `0 callsubr` is `139 10`; `return` is 11, `endchar` 14 (lenIV -1: charstrings in clear). + let mut subr = [139u8, 10].repeat(10); + subr.push(11); + let mut priv_ = b"dup /Private 8 dict dup begin /lenIV -1 def\n/Subrs 1 array\n".to_vec(); + priv_.extend_from_slice(format!("dup 0 {} RD ", subr.len()).as_bytes()); + priv_.extend_from_slice(&subr); + priv_.extend_from_slice(b" NP\nend\n/CharStrings 1 dict dup begin\n/A 3 RD \x8b\x0a\x0e ND\nend\nend\nmark currentfile closefile\n"); + out.extend(encrypt(&priv_, EEXEC_R, 4)); + out.extend_from_slice(b"\n0000000000000000\ncleartomark\n"); + let t = Type1::parse(&out).unwrap(); + let (p, _) = t.outline("A").unwrap(); + assert!(p.elements().is_empty()); + } + /// A `/Subrs` entry whose `RD` length is `usize::MAX`: the bytes are not in the file, and /// `q + n` must not overflow. #[test] diff --git a/crates/plugin/Cargo.toml b/crates/plugin/Cargo.toml index 4081b61b6..d9b35a91a 100644 --- a/crates/plugin/Cargo.toml +++ b/crates/plugin/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "effectcraft-plugin" -description = "EffectCraft effect plug-ins: loads sandboxed WebAssembly effect modules (plug-in API v1) into the effect registry" +description = "EffectCraft effect plug-ins: loads sandboxed WebAssembly effect modules (WebAssembly ABI v1 and v2) into the effect registry" version.workspace = true edition.workspace = true license.workspace = true diff --git a/crates/plugin/src/lib.rs b/crates/plugin/src/lib.rs index 7c85e144b..da21e22a0 100644 --- a/crates/plugin/src/lib.rs +++ b/crates/plugin/src/lib.rs @@ -1,21 +1,25 @@ //! # effectcraft-plugin (L3) //! -//! Loads **WebAssembly effect plug-ins** (plug-in ABI v1, see `docs/plugins.md`; the native -//! plug-in API 2 additions such as layer parameters and host services are not part of it) into the effect -//! registry ([`effectcraft_effects::plugin`]). A plug-in is a `.wasm` (or `.wat`) module with -//! no imports that exports: +//! Loads **WebAssembly effect plug-ins** (WebAssembly ABI v1 and v2, see `docs/plugins.md`; the +//! native plug-in API 2 additions such as layer parameters and host services are not part of it) +//! into the effect registry ([`effectcraft_effects::plugin`]). A plug-in is a `.wasm` (or +//! `.wat`) module with no imports that exports: //! //! | export | signature | | //! |---|---|---| //! | `memory` | memory | linear memory shared with the host | -//! | `ec_api_version` | `() -> i32` | must return 1 | +//! | `ec_api_version` | `() -> i32` | 1 for ABI 1, 2 for ABI 2 (neighbouring frames) | //! | `ec_manifest_ptr`, `ec_manifest_len` | `() -> i32` | a UTF-8 JSON [`PluginManifest`] in memory | //! | `ec_alloc` | `(bytes: i32) -> i32` | a buffer of at least `bytes` bytes, 8-byte aligned; it may reuse the previous one | -//! | `ec_render` | `(pixels: i32, width: i32, height: i32, params: i32, nparams: i32, time: f64, scale: f64) -> i32` | process the pixels in place; 0 = ok | +//! | `ec_render` (ABI 1) | `(pixels: i32, width: i32, height: i32, params: i32, nparams: i32, time: f64, scale: f64) -> i32` | process the pixels in place; 0 = ok | +//! | `ec_render2` (ABI 2) | `ec_render`'s arguments, then `prev: i32, prev_time: f64, next: i32, next_time: f64, flags: i32` | the same, with the neighbouring frames; 0 = ok | //! //! For each render the host asks `ec_alloc` for one buffer holding the parameters (`nparams` //! little-endian `f64`s) followed by the pixels (`width × height × 4` little-endian `f32`s, -//! premultiplied RGBA, row-major), calls `ec_render`, and reads the pixels back. +//! premultiplied RGBA, row-major), calls the render export, and reads the pixels back. An ABI 2 +//! module gets two more pixel buffers after the current frame: the layer's previous and next +//! frames, resampled onto the current grid, and their layer times. The buffers are always +//! allocated (zeroed when a frame does not exist); `flags` bit 0/1 says which are real. //! //! Modules run in the [wasmi](https://github.com/wasmi-labs/wasmi) interpreter (pure Rust, //! MIT/Apache-2.0) with deterministic floats, no imports (no files, network or clock) and a @@ -26,7 +30,9 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] use effectcraft_effects::EffectSpec; -pub use effectcraft_effects::plugin::{EffectPlugin, PLUGIN_API_VERSION, PluginFrame, PluginManifest, PluginParams, WASM_API_VERSION, register_plugin}; +pub use effectcraft_effects::plugin::{ + EffectPlugin, MIN_WASM_API_VERSION, PLUGIN_API_VERSION, PluginFrame, PluginImage, PluginManifest, PluginParams, WASM_API_VERSION, register_plugin, +}; #[cfg(feature = "wasm")] mod wasm; diff --git a/crates/plugin/src/tests.rs b/crates/plugin/src/tests.rs index f68fde00a..a4fadb26b 100644 --- a/crates/plugin/src/tests.rs +++ b/crates/plugin/src/tests.rs @@ -163,11 +163,15 @@ fn wasm_plugins_are_sandboxed() { // No imports (files, clock, network…). let imports = r#"(module (import "env" "now" (func $now (result f64))) (memory (export "memory") 1))"#; assert!(load_err(load_wasm(imports.as_bytes(), "imports.wat")).contains("import")); - // Garbage, wrong API version, bad manifests. + // Garbage, an ABI this build doesn't implement, bad manifests. A module that claims ABI 2 + // must export `ec_render2` (this one still has only `ec_render`). assert!(load_wasm(b"\0asm nope", "bad.wasm").is_err()); let v2 = gain_module("org.test.v2", "") .replace("(func (export \"ec_api_version\") (result i32) i32.const 1)", "(func (export \"ec_api_version\") (result i32) i32.const 2)"); - assert!(load_err(load_wasm(v2.as_bytes(), "v2.wat")).contains("API 2")); + assert!(load_err(load_wasm(v2.as_bytes(), "v2.wat")).contains("ec_render2")); + let v3 = gain_module("org.test.v3", "") + .replace("(func (export \"ec_api_version\") (result i32) i32.const 1)", "(func (export \"ec_api_version\") (result i32) i32.const 3)"); + assert!(load_err(load_wasm(v3.as_bytes(), "v3.wat")).contains("ABI 3")); let reserved = gain_module("ec.blur.mine", ""); assert!(load_err(load_wasm(reserved.as_bytes(), "reserved.wat")).contains("reserved")); } @@ -194,14 +198,14 @@ fn abi_violations_fail_to_load_with_reasons() { assert!(load_err(load_wasm(m.as_bytes(), "x.wat")).contains("ec_api_version")); let m = edit(&base, "(param $scale f64) (result i32)", "(param $scale f32) (result i32)"); assert!(load_err(load_wasm(m.as_bytes(), "x.wat")).contains("ec_render")); - // API versions other than 1. - for v in ["0", "-1", "2147483647"] { + // WebAssembly ABIs other than 1 and 2. + for v in ["0", "-1", "3", "2147483647"] { let m = edit( &base, "(func (export \"ec_api_version\") (result i32) i32.const 1)", &format!("(func (export \"ec_api_version\") (result i32) i32.const {v})"), ); - assert!(load_err(load_wasm(m.as_bytes(), "x.wat")).contains(&format!("API {v}")), "{v}"); + assert!(load_err(load_wasm(m.as_bytes(), "x.wat")).contains(&format!("ABI {v}")), "{v}"); } // Manifest out of bounds, negative or absurd lengths, not JSON. let ptr = "(func (export \"ec_manifest_ptr\") (result i32) i32.const 16)"; @@ -317,3 +321,168 @@ fn wasm_rendering_is_deterministic() { let twin = load_wasm(gain_module("org.test.det2", body).as_bytes(), "det2.wat").unwrap(); assert_eq!(bits(&render(twin, 0.37, true)), first); } + +/// An ABI 2 module whose `ec_render2` writes what it got from the host into each pixel: red is +/// |next.red − prev.red|, green `flags / 8`, blue `prev_time`, alpha 1. +fn neighbour_module(id: &str) -> String { + let manifest = format!(r#"{{"api":2,"id":"{id}","name":"Neighbours {id}","category":"Test Plug-ins","version":"1","params":[]}}"#); + let len = manifest.len(); + let escaped = manifest.replace('\\', "\\\\").replace('"', "\\\""); + format!( + r#"(module + (memory (export "memory") 2) + (data (i32.const 16) "{escaped}") + (func (export "ec_api_version") (result i32) i32.const 2) + (func (export "ec_manifest_ptr") (result i32) i32.const 16) + (func (export "ec_manifest_len") (result i32) i32.const {len}) + (func (export "ec_alloc") (param i32) (result i32) i32.const 1024) + (func (export "ec_render2") + (param $px i32) (param $w i32) (param $h i32) (param $params i32) (param $np i32) + (param $t f64) (param $scale f64) + (param $prev i32) (param $prev_t f64) (param $next i32) (param $next_t f64) (param $flags i32) + (result i32) + (local $i i32) (local $n i32) + local.get $w + local.get $h + i32.mul + local.set $n + block $done + loop $per_pixel + local.get $i + local.get $n + i32.ge_u + br_if $done + local.get $px + local.get $i + i32.const 16 + i32.mul + i32.add + local.get $next + local.get $i + i32.const 16 + i32.mul + i32.add + f32.load + local.get $prev + local.get $i + i32.const 16 + i32.mul + i32.add + f32.load + f32.sub + f32.abs + f32.store + local.get $px + local.get $i + i32.const 16 + i32.mul + i32.add + local.get $flags + f32.convert_i32_s + f32.const 8 + f32.div + f32.store offset=4 + local.get $px + local.get $i + i32.const 16 + i32.mul + i32.add + local.get $prev_t + f32.demote_f64 + f32.store offset=8 + local.get $px + local.get $i + i32.const 16 + i32.mul + i32.add + f32.const 1 + f32.store offset=12 + local.get $i + i32.const 1 + i32.add + local.set $i + br $per_pixel + end + end + i32.const 0))"# + ) +} + +fn neighbour_image(red: f32) -> effectcraft_effects::plugin::PluginImage { + effectcraft_effects::plugin::PluginImage { width: 2, height: 1, pixels: vec![[red, 0.0, 0.0, 1.0]; 2], origin: [0.0; 2], scale: 1.0 } +} + +/// A host with frames at two layer times. +struct Host { + time: f64, + fps: f64, + prev: Option<(f64, effectcraft_effects::plugin::PluginImage)>, + next: Option<(f64, effectcraft_effects::plugin::PluginImage)>, +} + +impl effectcraft_effects::plugin::PluginHost for Host { + fn input_at(&self, layer_time: f64) -> Option { + if let Some((t, img)) = &self.prev + && (t - layer_time).abs() < 1e-9 + { + return Some(img.clone()); + } + if let Some((t, img)) = &self.next + && (t - layer_time).abs() < 1e-9 + { + return Some(img.clone()); + } + None + } + + fn layer_input(&self, _param_id: &str, _layer_time: f64) -> Option { + None + } + + fn params_at(&self, _layer_time: f64) -> Option<(Vec, Vec)> { + None + } + + fn frame_rate(&self) -> f64 { + self.fps + } + + fn time(&self) -> f64 { + self.time + } +} + +#[test] +fn abi_v2_receives_neighbour_frames_and_times() { + load_wasm(neighbour_module("org.test.neighbours").as_bytes(), "neighbours.wat").unwrap(); + let p = effectcraft_effects::plugin::plugin("org.test.neighbours").unwrap(); + assert!(effectcraft_effects::plugin::reads_other_times("org.test.neighbours")); + assert!(effectcraft_effects::is_time_dependent("org.test.neighbours")); + // The module has no parameters, so the flattened vector is empty. + let params = effectcraft_effects::plugin::PluginParams { manifest: p.manifest(), flat: &[], texts: &[] }; + let mut px = vec![[0.0f32; 4]; 2]; + let host = Host { time: 0.5, fps: 4.0, prev: Some((0.25, neighbour_image(0.1))), next: Some((0.75, neighbour_image(0.6))) }; + let mut frame = effectcraft_effects::plugin::PluginFrame { width: 2, height: 1, pixels: &mut px, scale: 1.0, origin: [0.0; 2], layer_size: [2.0, 1.0] }; + p.render_v2(&mut frame, ¶ms, 0.5, &host).unwrap(); + assert!((px[0][0] - 0.5).abs() < 1e-6, "|next − prev|: {px:?}"); + assert!((px[0][1] - 3.0 / 8.0).abs() < 1e-6, "both flags: {px:?}"); + assert!((px[0][2] - 0.25).abs() < 1e-6, "previous frame's time: {px:?}"); + assert_eq!(px[0][3], 1.0); +} + +#[test] +fn abi_v2_without_a_host_gets_zeroed_neighbours() { + let spec = load_wasm(neighbour_module("org.test.neighbours.empty").as_bytes(), "empty.wat").unwrap(); + let mut next_id = 1; + let g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next_id), spec.name, [2.0, 1.0]); + let params = effectcraft_effects::flatten_params(&g, &mut |p| p.value.clone()); + let ctx = EffectCtx { params: ¶ms, time: 0.5, layer_size: [2.0, 1.0], seed: 1, adjustment: false, env: EffectEnv::default() }; + let buf = Buf { img: Image::filled(2, 1, [0.5, 0.5, 0.5, 1.0]), offset: [0.0; 2], scale: 1.0 }; + // No host: both neighbour buffers are zeroed and no flag is set. + assert_eq!((spec.render)(&ctx, buf).img.get(0, 0), [0.0, 0.0, 0.0, 1.0]); + // ABI 1 plug-ins keep the time-independent behaviour. + let gain = load_wasm(gain_module("org.test.neighbours.v1", "").as_bytes(), "v1.wat").unwrap(); + assert!(!effectcraft_effects::plugin::reads_other_times("org.test.neighbours.v1")); + assert!(!effectcraft_effects::is_time_dependent("org.test.neighbours.v1")); + assert!(gain.id.starts_with("org.test.")); +} diff --git a/crates/plugin/src/wasm.rs b/crates/plugin/src/wasm.rs index 18a34548f..18f3fb0d0 100644 --- a/crates/plugin/src/wasm.rs +++ b/crates/plugin/src/wasm.rs @@ -1,10 +1,11 @@ -//! The wasmi-backed [`EffectPlugin`] (plug-in API v1 ABI, see the crate docs). +//! The wasmi-backed [`EffectPlugin`] (WebAssembly ABI v1 and v2, see the crate docs). use std::sync::Mutex; use wasmi::{Config, Engine, Instance, Linker, Memory, Module, Store, StoreLimits, StoreLimitsBuilder, TypedFunc, WasmParams, WasmResults}; -use crate::{EffectPlugin, PluginFrame, PluginManifest, PluginParams, WASM_API_VERSION}; +use crate::{EffectPlugin, MIN_WASM_API_VERSION, PluginFrame, PluginManifest, PluginParams, WASM_API_VERSION}; +use effectcraft_effects::plugin::{PluginHost, PluginImage}; /// Instruction budget per frame: a fixed allowance plus some per pixel. Generous for per-pixel /// work, but a runaway loop stops instead of hanging the render. @@ -12,50 +13,103 @@ const FUEL_BASE: u64 = 50_000_000; const FUEL_PER_PIXEL: u64 = 20_000; /// The most linear memory a plug-in instance may have (a 4K float frame is ~130 MB). const MAX_MEMORY: usize = 1 << 30; +/// The most elements one table of a plug-in may hold. +const MAX_TABLE_ELEMENTS: usize = 1 << 16; /// The longest manifest read. const MAX_MANIFEST: i32 = 1 << 20; +/// ABI 1: one buffer. type RenderFn = TypedFunc<(i32, i32, i32, i32, i32, f64, f64), i32>; +/// ABI 2: the same call plus the previous and next frame buffers and their layer times. +type Render2Fn = TypedFunc<(i32, i32, i32, i32, i32, f64, f64, i32, f64, i32, f64, i32), i32>; + +enum Renderer { + V1(RenderFn), + V2(Render2Fn), +} /// One instantiated module (renders running on several threads each take their own). struct Inst { + store: Store, + memory: Memory, + alloc: TypedFunc, + render: Renderer, +} + +/// An instance whose ABI version (and so its render export) is not selected yet. +struct Pending { store: Store, instance: Instance, memory: Memory, alloc: TypedFunc, - render: RenderFn, } -impl Inst { +impl Pending { + /// A typed export by name, reporting a missing or mistyped export like [`missing`]. fn func(&self, name: &str) -> Result, String> { self.instance.get_typed_func::(&self.store, name).map_err(|_| missing(name)) } + + /// Select the render export for `abi`. + fn finish(self, abi: u32) -> Result { + let render = match abi { + 1 => Renderer::V1(self.instance.get_typed_func::<_, i32>(&self.store, "ec_render").map_err(|_| missing("ec_render"))?), + 2 => Renderer::V2(self.instance.get_typed_func::<_, i32>(&self.store, "ec_render2").map_err(|_| missing("ec_render2"))?), + v => return Err(unsupported(v as i32)), + }; + Ok(Inst { store: self.store, memory: self.memory, alloc: self.alloc, render }) + } } +/// The error for a missing or mistyped export. fn missing(name: &str) -> String { format!("the module exports no `{name}` (or with the wrong signature)") } +/// The error for an ABI this build does not implement. +fn unsupported(v: i32) -> String { + format!("the module implements WebAssembly ABI {v}, this build implements ABI {MIN_WASM_API_VERSION} to {WASM_API_VERSION}") +} + +/// The module's `ec_api_version`, when this build implements it. +fn abi_of(v: i32) -> Result { + match u32::try_from(v) { + Ok(v) if (MIN_WASM_API_VERSION..=WASM_API_VERSION).contains(&v) => Ok(v), + _ => Err(unsupported(v)), + } +} + +/// The error for a wasmi or trap failure. fn err(e: impl std::fmt::Display) -> String { e.to_string() } -fn instantiate(engine: &Engine, module: &Module) -> Result { - let mut store = Store::new(engine, StoreLimitsBuilder::new().memory_size(MAX_MEMORY).instances(1).build()); +/// Instantiate `module` in a fresh, limited store and read its memory and allocator. +fn begin(engine: &Engine, module: &Module) -> Result { + // One memory and a few small tables: a module declaring many of them would otherwise make + // the host allocate and initialise each before an export or fuel check could refuse it. + let limits = StoreLimitsBuilder::new().memory_size(MAX_MEMORY).memories(1).tables(4).table_elements(MAX_TABLE_ELEMENTS).instances(1).build(); + let mut store = Store::new(engine, limits); store.limiter(|l| l); store.set_fuel(FUEL_BASE).map_err(err)?; let linker = Linker::::new(engine); let instance = linker.instantiate_and_start(&mut store, module).map_err(|e| format!("instantiation failed: {e}"))?; let memory = instance.get_memory(&store, "memory").ok_or("the module exports no `memory`")?; let alloc = instance.get_typed_func::(&store, "ec_alloc").map_err(|_| missing("ec_alloc"))?; - let render = instance.get_typed_func::<(i32, i32, i32, i32, i32, f64, f64), i32>(&store, "ec_render").map_err(|_| missing("ec_render"))?; - Ok(Inst { store, instance, memory, alloc, render }) + Ok(Pending { store, instance, memory, alloc }) +} + +/// Instantiate `module` and select the render export for `abi`. +fn instantiate(engine: &Engine, module: &Module, abi: u32) -> Result { + begin(engine, module)?.finish(abi) } /// A loaded WebAssembly effect plug-in. pub struct WasmPlugin { manifest: PluginManifest, source: String, + /// The WebAssembly ABI the module implements (`ec_api_version`). + abi: u32, engine: Engine, module: Module, /// Idle instances. @@ -63,6 +117,7 @@ pub struct WasmPlugin { } impl WasmPlugin { + /// Load `bytes` and its manifest; errors on imports, a bad manifest or a missing export. pub fn new(bytes: &[u8], source: &str) -> Result { let mut config = Config::default(); config.consume_fuel(true); @@ -71,51 +126,42 @@ impl WasmPlugin { if let Some(i) = module.imports().next() { return Err(format!("plug-ins can't import anything (the module imports `{}.{}`)", i.module(), i.name())); } - let mut inst = instantiate(&engine, &module)?; - let version = inst.func::<(), i32>("ec_api_version")?; - let v = version.call(&mut inst.store, ()).map_err(err)?; - if u32::try_from(v).ok() != Some(WASM_API_VERSION) { - return Err(format!("the module implements plug-in API {v}, this build implements API {WASM_API_VERSION} for WebAssembly")); - } - let ptr = inst.func::<(), i32>("ec_manifest_ptr")?.call(&mut inst.store, ()).map_err(err)?; - let len = inst.func::<(), i32>("ec_manifest_len")?.call(&mut inst.store, ()).map_err(err)?; + let mut pending = begin(&engine, &module)?; + let version = pending.func::<(), i32>("ec_api_version")?; + let abi = abi_of(version.call(&mut pending.store, ()).map_err(err)?)?; + let ptr = pending.func::<(), i32>("ec_manifest_ptr")?.call(&mut pending.store, ()).map_err(err)?; + let len = pending.func::<(), i32>("ec_manifest_len")?.call(&mut pending.store, ()).map_err(err)?; if !(0..=MAX_MANIFEST).contains(&len) { return Err(format!("bad manifest length {len}")); } let mut text = vec![0u8; len as usize]; - inst.memory.read(&inst.store, ptr as u32 as usize, &mut text).map_err(|e| format!("manifest out of bounds: {e}"))?; + pending.memory.read(&pending.store, ptr as u32 as usize, &mut text).map_err(|e| format!("manifest out of bounds: {e}"))?; let manifest: PluginManifest = serde_json::from_slice(&text).map_err(|e| format!("bad manifest JSON: {e}"))?; manifest.validate()?; if let Some(p) = manifest.params.iter().find(|p| p.kind.needs_v2()) { return Err(format!("parameter `{}`: WebAssembly plug-ins use plug-in API 1 parameter types (no point3, layer, text or hidden)", p.id)); } - Ok(WasmPlugin { manifest, source: source.to_string(), engine, module, pool: Mutex::new(vec![inst]) }) + let inst = pending.finish(abi)?; + Ok(WasmPlugin { manifest, source: source.to_string(), abi, engine, module, pool: Mutex::new(vec![inst]) }) } + /// An idle instance, or a fresh one when the pool is empty. fn take(&self) -> Result { if let Some(i) = self.pool.lock().ok().and_then(|mut p| p.pop()) { return Ok(i); } - instantiate(&self.engine, &self.module) + instantiate(&self.engine, &self.module, self.abi) } + /// Return a working instance to the pool. fn give_back(&self, inst: Inst) { if let Ok(mut p) = self.pool.lock() { p.push(inst); } } -} -impl EffectPlugin for WasmPlugin { - fn manifest(&self) -> &PluginManifest { - &self.manifest - } - - fn source(&self) -> String { - self.source.clone() - } - - fn render(&self, frame: &mut PluginFrame, params: &PluginParams, time: f64) -> Result<(), String> { + /// ABI 1: parameters, then the pixels; `ec_render`. + fn render_v1(&self, frame: &mut PluginFrame, params: &PluginParams, time: f64) -> Result<(), String> { let n = frame.pixels.len(); let params_bytes = params.flat.len() * 8; let total = params_bytes + n * 16; @@ -130,25 +176,19 @@ impl EffectPlugin for WasmPlugin { for v in params.flat { buf.extend_from_slice(&v.to_le_bytes()); } - for px in frame.pixels.iter() { - for c in px { - buf.extend_from_slice(&c.to_le_bytes()); - } - } + push_pixels(&mut buf, frame.pixels); inst.memory.write(&mut inst.store, base, &buf).map_err(|e| format!("ec_alloc returned a buffer out of bounds: {e}"))?; let px_ptr = (base + params_bytes) as i32; - let r = inst - .render - .call(&mut inst.store, (px_ptr, frame.width as i32, frame.height as i32, base as i32, params.flat.len() as i32, time, frame.scale)) - .map_err(|e| format!("ec_render trapped: {e}")); + let r = match &inst.render { + Renderer::V1(f) => f + .call(&mut inst.store, (px_ptr, frame.width as i32, frame.height as i32, base as i32, params.flat.len() as i32, time, frame.scale)) + .map_err(|e| format!("ec_render trapped: {e}")), + Renderer::V2(_) => return Err("the module implements WebAssembly ABI 1 but exports ec_render2".into()), + }; let r = match r { Ok(0) => { inst.memory.read(&inst.store, base + params_bytes, &mut buf[params_bytes..]).map_err(err)?; - for (px, b) in frame.pixels.iter_mut().zip(buf[params_bytes..].as_chunks::<16>().0.iter()) { - for (c, v) in px.iter_mut().zip(b.as_chunks::<4>().0.iter()) { - *c = f32::from_le_bytes([v[0], v[1], v[2], v[3]]); - } - } + read_pixels(&buf[params_bytes..], frame.pixels); Ok(()) } Ok(code) => Err(format!("ec_render returned error {code}")), @@ -159,4 +199,170 @@ impl EffectPlugin for WasmPlugin { } r } + + /// ABI 2: parameters, the current frame, then the previous and next frames; `ec_render2`. + /// The neighbour buffers are always allocated (zeroed when absent), so a module can read + /// both without a null check; `flags` says which hold real frames. + fn render_v2( + &self, + frame: &mut PluginFrame, + params: &PluginParams, + time: f64, + prev: Option<(f64, &PluginImage)>, + next: Option<(f64, &PluginImage)>, + ) -> Result<(), String> { + let n = frame.pixels.len(); + let params_bytes = params.flat.len() * 8; + let frame_bytes = n * 16; + let total = params_bytes + frame_bytes * 3; + if total > i32::MAX as usize { + return Err("frame too large for a 32-bit plug-in".into()); + } + let prev_px = prev.map(|(t, img)| (t, img.on_grid(frame.width, frame.height, frame.origin, frame.scale))); + let next_px = next.map(|(t, img)| (t, img.on_grid(frame.width, frame.height, frame.origin, frame.scale))); + let prev = prev_px.as_ref().filter(|(_, px)| px.len() == n); + let next = next_px.as_ref().filter(|(_, px)| px.len() == n); + let mut inst = self.take()?; + inst.store.set_fuel(FUEL_BASE + FUEL_PER_PIXEL * (n as u64 * 3)).map_err(err)?; + let base = inst.alloc.call(&mut inst.store, total as i32).map_err(err)? as u32 as usize; + let mut buf: Vec = Vec::with_capacity(total); + for v in params.flat { + buf.extend_from_slice(&v.to_le_bytes()); + } + push_pixels(&mut buf, frame.pixels); + push_neighbour(&mut buf, prev, frame_bytes); + push_neighbour(&mut buf, next, frame_bytes); + inst.memory.write(&mut inst.store, base, &buf).map_err(|e| format!("ec_alloc returned a buffer out of bounds: {e}"))?; + let px_ptr = (base + params_bytes) as i32; + let prev_ptr = (base + params_bytes + frame_bytes) as i32; + let next_ptr = (base + params_bytes + frame_bytes * 2) as i32; + let flags = i32::from(prev.is_some()) | (i32::from(next.is_some()) << 1); + let prev_time = prev.map_or(0.0, |(t, _)| *t); + let next_time = next.map_or(0.0, |(t, _)| *t); + let r = match &inst.render { + Renderer::V2(f) => f + .call( + &mut inst.store, + ( + px_ptr, + frame.width as i32, + frame.height as i32, + base as i32, + params.flat.len() as i32, + time, + frame.scale, + prev_ptr, + prev_time, + next_ptr, + next_time, + flags, + ), + ) + .map_err(|e| format!("ec_render2 trapped: {e}")), + Renderer::V1(_) => return Err("the module implements WebAssembly ABI 2 but exports ec_render".into()), + }; + let r = match r { + Ok(0) => { + let mut out = vec![0u8; frame_bytes]; + inst.memory.read(&inst.store, base + params_bytes, &mut out).map_err(err)?; + read_pixels(&out, frame.pixels); + Ok(()) + } + Ok(code) => Err(format!("ec_render2 returned error {code}")), + Err(e) => Err(e), + }; + if r.is_ok() { + self.give_back(inst); + } + r + } +} + +/// Append pixels to the little-endian buffer the module reads. +fn push_pixels(buf: &mut Vec, px: &[[f32; 4]]) { + for p in px { + for c in p { + buf.extend_from_slice(&c.to_le_bytes()); + } + } +} + +/// The neighbour's pixels, or `frame_bytes` zeroed bytes when there is no frame. +fn push_neighbour(buf: &mut Vec, neighbour: Option<&(f64, Vec<[f32; 4]>)>, frame_bytes: usize) { + match neighbour { + Some((_, px)) => push_pixels(buf, px), + None => buf.resize(buf.len() + frame_bytes, 0), + } +} + +/// Read the little-endian pixels the module wrote back. +fn read_pixels(bytes: &[u8], pixels: &mut [[f32; 4]]) { + for (px, b) in pixels.iter_mut().zip(bytes.as_chunks::<16>().0.iter()) { + for (c, v) in px.iter_mut().zip(b.as_chunks::<4>().0.iter()) { + *c = f32::from_le_bytes([v[0], v[1], v[2], v[3]]); + } + } +} + +/// The layer times of the frames around `time` on a `fps` grid: the frame before and after it +/// at a frame boundary, the two frames that bracket it between frames. +fn neighbour_times(time: f64, fps: f64) -> (f64, f64) { + if !fps.is_finite() || fps <= 0.0 { + return (time - 1.0 / 30.0, time + 1.0 / 30.0); + } + let step = 1.0 / fps; + let at = time * fps; + // Frame times are not exact in floating point: at NTSC rates `time * fps` often lands a few + // ulps above or below an integer. Treat a near-integer as a frame boundary, so the current + // frame is never handed back as its own neighbour. + if (at - at.round()).abs() < 1e-6 { (time - step, time + step) } else { (at.floor() / fps, at.ceil() / fps) } +} + +impl EffectPlugin for WasmPlugin { + fn manifest(&self) -> &PluginManifest { + &self.manifest + } + + fn source(&self) -> String { + self.source.clone() + } + + fn reads_other_times(&self) -> bool { + self.abi >= 2 + } + + fn render(&self, frame: &mut PluginFrame, params: &PluginParams, time: f64) -> Result<(), String> { + if self.abi == 1 { self.render_v1(frame, params, time) } else { self.render_v2(frame, params, time, None, None) } + } + + fn render_v2(&self, frame: &mut PluginFrame, params: &PluginParams, time: f64, host: &dyn PluginHost) -> Result<(), String> { + if self.abi == 1 { + return self.render_v1(frame, params, time); + } + let (prev_time, next_time) = neighbour_times(time, host.frame_rate()); + let prev = host.input_at(prev_time); + let next = host.input_at(next_time); + let prev = prev.as_ref().map(|img| (prev_time, img)); + let next = next.as_ref().map(|img| (next_time, img)); + self.render_v2(frame, params, time, prev, next) + } +} + +#[cfg(test)] +mod tests { + use super::neighbour_times; + + /// NTSC frame times do not multiply back exactly; every frame boundary must still get the + /// frames either side of it, never the frame itself. + #[test] + fn frame_boundaries_get_their_neighbours_at_ntsc_rates() { + let fps = 24000.0 / 1001.0; + for n in 0..2400 { + // 24000/1001 fps at 254 016 000 000 ticks/s, as `Tick` stores it. + let time = (n * 10_594_584_000_i64) as f64 / 254_016_000_000.0; + let (prev, next) = neighbour_times(time, fps); + assert!((prev - (time - 1.0 / fps)).abs() < 1e-9, "frame {n}: prev {prev}, current {time}"); + assert!((next - (time + 1.0 / fps)).abs() < 1e-9, "frame {n}: next {next}, current {time}"); + } + } } diff --git a/crates/project/src/ae_order.rs b/crates/project/src/ae_order.rs new file mode 100644 index 000000000..b6dce1d44 --- /dev/null +++ b/crates/project/src/ae_order.rs @@ -0,0 +1,139 @@ +//! After Effects' numeric order of a layer's top-level property groups. +//! +//! In After Effects `thisLayer(4)` and `layer.property(4)` reach the same group on every layer of a +//! kind, whatever the layer holds: Marker is always 1, and the groups a layer has not used yet +//! (Time Remap, Motion Trackers, Layer Styles…) still take their slot. EffectCraft's tree holds only +//! the groups a layer has, without Marker, so its own positions differ and move as groups are +//! added. Rigs that address groups by number (`comp("Palette").layer(1)(4)(n)(1)`) read the wrong +//! group, or none, unless numbers go through this table. +//! +//! The order was observed in After Effects 26.3 by reading `layer.property(i).matchName` from a +//! script and `thisComp.layer(..)(i).name` from an expression on new layers of each kind; both +//! agree. 3D model and primitive layers have no table: their numbers stay EffectCraft's own. + +use crate::{Layer, LayerSource, Node}; + +/// One top-level slot in After Effects' order. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct AeSlot { + /// The group's match id in EffectCraft's tree, or `""` when EffectCraft has no such group. + pub match_id: &'static str, + /// The display name After Effects gives the slot. + pub name: &'static str, + /// After Effects' match name for the slot. + pub match_name: &'static str, +} + +const fn slot(match_id: &'static str, name: &'static str, match_name: &'static str) -> AeSlot { + AeSlot { match_id, name, match_name } +} + +/// The marker slot: number 1 on every layer. Markers are a field of [`Layer`], not a tree node. +pub const MARKER: AeSlot = slot("marker", "Marker", "ADBE Marker"); + +/// Footage, solid, adjustment, precomp and null layers. +const AV: &[AeSlot] = &[ + MARKER, + slot("timeRemap", "Time Remap", "ADBE Time Remapping"), + slot("motionTrackers", "Motion Trackers", "ADBE MTrackers"), + slot("masks", "Masks", "ADBE Mask Parade"), + slot("effects", "Effects", "ADBE Effect Parade"), + slot("transform", "Transform", "ADBE Transform Group"), + slot("layerStyles", "Layer Styles", "ADBE Layer Styles"), + slot("", "Geometry Options", "ADBE Plane Options Group"), + slot("geometryOptions", "Geometry Options", "ADBE Extrsn Options Group"), + slot("materialOptions", "Material Options", "ADBE Material Options Group"), + slot("audio", "Audio", "ADBE Audio Group"), + slot("", "Data", "ADBE Data Group"), + slot("", "Essential Properties", "ADBE Layer Overrides"), + slot("", "Sets", "ADBE Layer Sets"), + slot("", "Source Options", "ADBE Source Options Group"), +]; + +const TEXT: &[AeSlot] = &[ + MARKER, + slot("text", "Text", "ADBE Text Properties"), + slot("masks", "Masks", "ADBE Mask Parade"), + slot("effects", "Effects", "ADBE Effect Parade"), + slot("transform", "Transform", "ADBE Transform Group"), + slot("layerStyles", "Layer Styles", "ADBE Layer Styles"), + slot("geometryOptions", "Geometry Options", "ADBE Extrsn Options Group"), + slot("materialOptions", "Material Options", "ADBE Material Options Group"), + slot("audio", "Audio", "ADBE Audio Group"), + slot("", "Sets", "ADBE Layer Sets"), +]; + +const SHAPE: &[AeSlot] = &[ + MARKER, + slot("contents", "Contents", "ADBE Root Vectors Group"), + slot("masks", "Masks", "ADBE Mask Parade"), + slot("effects", "Effects", "ADBE Effect Parade"), + slot("transform", "Transform", "ADBE Transform Group"), + slot("layerStyles", "Layer Styles", "ADBE Layer Styles"), + slot("geometryOptions", "Geometry Options", "ADBE Extrsn Options Group"), + slot("materialOptions", "Material Options", "ADBE Material Options Group"), + slot("audio", "Audio", "ADBE Audio Group"), + slot("", "Sets", "ADBE Layer Sets"), +]; + +const CAMERA: &[AeSlot] = + &[MARKER, slot("transform", "Transform", "ADBE Transform Group"), slot("cameraOptions", "Camera Options", "ADBE Camera Options Group")]; + +const LIGHT: &[AeSlot] = &[MARKER, slot("transform", "Transform", "ADBE Transform Group"), slot("lightOptions", "Light Options", "ADBE Light Options Group")]; + +/// After Effects' top-level slots for a layer of this kind, or `None` for 3D model and primitive +/// layers, which keep EffectCraft's own numbers. +pub fn ae_slots(source: &LayerSource) -> Option<&'static [AeSlot]> { + match source { + LayerSource::Footage { .. } | LayerSource::Comp { .. } | LayerSource::Solid { .. } | LayerSource::Null => Some(AV), + LayerSource::Text => Some(TEXT), + LayerSource::Shape => Some(SHAPE), + LayerSource::Camera => Some(CAMERA), + LayerSource::Light { .. } => Some(LIGHT), + LayerSource::Model { .. } | LayerSource::Primitive { .. } => None, + } +} + +/// What After Effects' top-level number `index` (1-based) reaches on `layer`. +#[derive(Clone, Copy, Debug)] +pub enum AeChild<'a> { + /// The layer's markers (number 1). + Marker, + /// A group or property the layer has. + Node(&'a Node), + /// A slot After Effects always has and this layer does not (an empty group there). + Absent(&'static AeSlot), +} + +/// The top-level node After Effects' number `index` (1-based) reaches, or `None` when the number is +/// past the last slot or the layer kind has no table ([`ae_slots`]). +pub fn ae_child(layer: &Layer, index: usize) -> Option> { + let s = ae_slots(&layer.source)?.get(index.checked_sub(1)?)?; + if *s == MARKER { + return Some(AeChild::Marker); + } + if s.match_id.is_empty() { + return Some(AeChild::Absent(s)); + } + Some(layer.props.children.iter().find(|n| n.match_id() == s.match_id).map_or(AeChild::Absent(s), AeChild::Node)) +} + +/// After Effects' 1-based number for the top-level node with this match id, when it has one. +pub fn ae_index(layer: &Layer, match_id: &str) -> Option { + if match_id.is_empty() { + return None; + } + ae_slots(&layer.source)?.iter().position(|s| s.match_id == match_id).map(|i| i + 1) +} + +/// How many top-level properties After Effects counts on `layer` (`numProperties`), or `None` for +/// layer kinds without a table. +pub fn ae_count(layer: &Layer) -> Option { + ae_slots(&layer.source).map(<[AeSlot]>::len) +} + +/// After Effects' 1-based number for the top-level slot with this After Effects match name +/// (`ADBE Effect Parade`), when the layer kind has one. +pub fn ae_index_of_match_name(layer: &Layer, match_name: &str) -> Option { + ae_slots(&layer.source)?.iter().position(|s| s.match_name == match_name).map(|i| i + 1) +} diff --git a/crates/project/src/comp.rs b/crates/project/src/comp.rs index d8d7cda16..c0c401c82 100644 --- a/crates/project/src/comp.rs +++ b/crates/project/src/comp.rs @@ -268,8 +268,19 @@ impl Comp { /// `base`, or the next free numbered name when a layer already has it (`Shape Layer 2`, /// `v-b1.mp4 2`; see `numbered_name`). pub fn unique_layer_name(&self, base: &str) -> String { - let taken = |n: &str| self.layers.iter().any(|l| l.name == n); - if taken(base) { crate::item::numbered_name(base, taken) } else { base.to_string() } + if !self.layers.iter().any(|l| l.name == base) { + return base.to_string(); + } + let stem = crate::item::name_stem(base); + // The suffixes in use, gathered in one pass: trying each candidate against every layer + // made importing many layers of one name cubic. + let taken: std::collections::HashSet = self + .layers + .iter() + .filter_map(|l| l.name.strip_prefix(stem)?.strip_prefix(' ')) + .filter_map(|s| s.parse::().ok().filter(|n| n.to_string() == s)) + .collect(); + (2..).find(|i| !taken.contains(i)).map(|i| format!("{stem} {i}")).unwrap_or_default() } } diff --git a/crates/project/src/footage.rs b/crates/project/src/footage.rs index 2cf887b1f..eeace5e81 100644 --- a/crates/project/src/footage.rs +++ b/crates/project/src/footage.rs @@ -269,6 +269,13 @@ pub struct Footage { /// made from the footage starts with them, and Update Markers From Source copies new ones. #[serde(default, skip_serializing_if = "Vec::is_empty")] pub markers: Vec, + /// PDF / Illustrator footage (`.pdf`, `.ai`; not EPS) from an After Effects project: the page + /// is framed as After Effects places it (the ArtBox, `/Rotate` ignored) and drawn to fill + /// `width` × `height`, the size After Effects stored, which the layers' anchors and masks are + /// measured against (#3099). Other footage (and every project saved before this field) shows + /// the page as a PDF viewer does, at its own size. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub placed_page: bool, } fn is_default(v: &T) -> bool { @@ -307,6 +314,7 @@ impl Default for Footage { start_timecode: None, file_timecode: None, markers: vec![], + placed_page: false, } } } diff --git a/crates/project/src/item.rs b/crates/project/src/item.rs index 4fcc06ab9..b055c60ba 100644 --- a/crates/project/src/item.rs +++ b/crates/project/src/item.rs @@ -8,12 +8,18 @@ use super::{Arc, BTreeMap, Deserialize, FrameRate, Label, Project, Serialize, Ti /// another layer of the same name. A number set off by a space is a counter and is replaced /// (`Red Solid 1` → `Red Solid 2`, `shot 2` → `shot 3`); digits that end a word belong to the /// name and stay (`v-b1.mp4` → `v-b1.mp4 2`, `clip2.mov` → `clip2.mov 2`). -pub(crate) fn numbered_name(base: &str, taken: impl Fn(&str) -> bool) -> String { +/// `base` without a trailing ` N`: digits are dropped only when a space separates them from a +/// nonempty stem, so `v-b1.mp4` and `Layer1` keep theirs. +pub(crate) fn name_stem(base: &str) -> &str { let head = base.trim_end_matches(|c: char| c.is_ascii_digit()); - let stem = match head.strip_suffix(' ') { + match head.strip_suffix(' ') { Some(stem) if head.len() < base.len() && !stem.trim_end().is_empty() => stem.trim_end(), _ => base.trim_end(), - }; + } +} + +pub(crate) fn numbered_name(base: &str, taken: impl Fn(&str) -> bool) -> String { + let stem = name_stem(base); (2u64..).map(|k| format!("{stem} {k}")).find(|n| !taken(n)).unwrap_or_default() } diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs index 6d0a72976..9f5f399e0 100644 --- a/crates/project/src/lib.rs +++ b/crates/project/src/lib.rs @@ -6,6 +6,7 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +pub mod ae_order; pub mod build; pub mod essential; mod file_check; diff --git a/crates/project/src/render_queue.rs b/crates/project/src/render_queue.rs index 8635bc9a8..b935c2fc8 100644 --- a/crates/project/src/render_queue.rs +++ b/crates/project/src/render_queue.rs @@ -421,7 +421,12 @@ impl RenderSettings { /// The output frame rate: the Frame Rate override, unless the comp preserves its frame rate /// (Composition Settings ▸ Preserve frame rate when nested or in render queue). pub fn rate(&self, comp: &Comp) -> FrameRate { - if comp.preserve_frame_rate { comp.frame_rate } else { self.frame_rate.unwrap_or(comp.frame_rate) } + if comp.preserve_frame_rate { + return comp.frame_rate; + } + // A saved override is held to the supported rates, as a comp's own rate is: its frame count + // is the export's work. + self.frame_rate.map_or(comp.frame_rate, |r| crate::limits::frame_rate(r, comp.frame_rate)) } /// First output-rate frame starting at or after `t`. fn ceil_frame(r: FrameRate, t: Tick) -> i64 { @@ -480,6 +485,8 @@ impl RenderSettings { TimeSpan::Custom { start, end } => format!("Custom ({:.3}s – {:.3}s)", start.seconds(), end.seconds()), }; let rate = match (self.frame_rate, comp) { + // A saved override outside the supported rates renders at the clamped one: say so. + (Some(r), Some(c)) if self.rate(c) != r => format!("{:.3} fps (renders at {:.3})", r.as_f64(), self.rate(c).as_f64()), (Some(r), _) => format!("{:.3} fps", r.as_f64()), (None, Some(c)) => format!("Use comp's frame rate ({:.3})", c.frame_rate.as_f64()), (None, None) => "Use comp's frame rate".into(), @@ -779,6 +786,27 @@ pub struct Crop { pub right: i32, } +/// The largest side of a frame an output module writes (Resize is capped there too). +pub const MAX_OUTPUT_SIDE: i32 = 30_000; + +/// Crop edges `a`/`b` of one axis of `side` pixels: padding is taken back only as far as keeps +/// the result within [`MAX_OUTPUT_SIDE`], so a mixed pair that stays inside the cap is kept as +/// saved. Each edge is also bounded (what the larger of the side and the cap allows), so the +/// arithmetic on them cannot overflow. +fn bound_edges(a: i32, b: i32, side: i32) -> (i32, i32) { + let hi = side.max(MAX_OUTPUT_SIDE); + let (mut a, mut b) = (a.clamp(-MAX_OUTPUT_SIDE, hi), b.clamp(-MAX_OUTPUT_SIDE, hi)); + let excess = i64::from(side) - i64::from(a) - i64::from(b) - i64::from(MAX_OUTPUT_SIDE); + if excess > 0 { + // Only padding (negative edges) grows the side: take back as much of it as needed. + let excess = i32::try_from(excess).unwrap_or(i32::MAX); + let from_a = excess.min(-a.min(0)); + a += from_a; + b += (excess - from_a).min(-b.min(0)); + } + (a, b) +} + impl Crop { /// Edges `(top, left, bottom, right)` in pixels of a `w`×`h` frame rendered at `scale`. pub fn edges(&self, w: u32, h: u32, scale: f64) -> (i32, i32, i32, i32) { @@ -801,7 +829,12 @@ impl Crop { let b = conv((y + rh) * scale, hi).max(t); return (t, l, hi - b, wi - r); } - (self.top, self.left, self.bottom, self.right) + // Negative edges pad the frame: keep each padded side within the largest frame an + // output module writes, or a few saved integers ask for a canvas of gigapixels. + let (wi, hi) = (i32::try_from(w).unwrap_or(i32::MAX), i32::try_from(h).unwrap_or(i32::MAX)); + let (t, b) = bound_edges(self.top, self.bottom, hi); + let (l, r) = bound_edges(self.left, self.right, wi); + (t, l, b, r) } } @@ -1271,8 +1304,8 @@ impl OutputModule { // Width rules; height follows the cropped frame's aspect. rh = ((rw as f64 * ch as f64 / cw as f64).round() as u32).max(1); } - rw = rw.min(30_000); - rh = rh.min(30_000); + rw = rw.min(MAX_OUTPUT_SIDE as u32); + rh = rh.min(MAX_OUTPUT_SIDE as u32); (rw, rh) } /// The written frame size of an item (Render Settings resolution, Crop, Resize, codec @@ -1687,17 +1720,71 @@ pub fn numbered_template(template: &str, n: u32) -> String { /// Make a name safe as a file name component. pub fn sanitize(name: &str) -> String { - name.chars().map(|c| if matches!(c, '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|') { '_' } else { c }).collect() + let mut s: String = name.chars().map(|c| if matches!(c, '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|') { '_' } else { c }).collect(); + // At most a file name's worth (255 UTF-8 bytes, whole characters only): a template repeating + // a long name token must not expand to gigabytes. A cut name ends in a hash of the whole + // name so long names that share a prefix stay distinct. + if s.len() > 255 { + // shortcut: 64-bit FNV-1a, not collision-resistant against crafted names; use a real hash if names become untrusted keys. + let hash = name.bytes().fold(0xcbf2_9ce4_8422_2325u64, |h, b| (h ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3)); + let mut end = 255 - 17; + while !s.is_char_boundary(end) { + end -= 1; + } + s.truncate(end); + s.push_str(&format!("-{hash:016x}")); + } + // `.` and `..` would name the folder itself or its parent when the token is a path component. + if !s.is_empty() && s.chars().all(|c| c == '.') { "_".repeat(s.len()) } else { s } } #[cfg(test)] mod tests { + #[test] + fn crop_padding_stays_within_the_largest_output_frame() { + use super::*; + let crop = |top, left, bottom, right| Crop { enabled: true, top, left, bottom, right, ..Default::default() }; + let m = |c: Crop| OutputModule { crop: c, ..Default::default() }; + // Saved edges padding by billions of pixels: the frame stops at the cap. + assert_eq!(m(crop(i32::MIN, i32::MIN, i32::MIN, i32::MIN)).frame_size(100, 50, 1.0), (30_000, 30_000)); + assert_eq!(m(crop(-20_000, 0, -20_000, -1)).frame_size(100, 50, 1.0), (101, 30_000)); + // Ordinary crops and pads are unchanged. + assert_eq!(crop(1, -2, 3, 4).edges(100, 50, 1.0), (1, -2, 3, 4)); + // A mixed pair beyond the side that still ends within the cap is kept as saved (50 px). + assert_eq!(crop(0, 150, 0, -100).edges(100, 50, 1.0), (0, 150, 0, -100)); + assert_eq!(m(crop(0, 150, 0, -100)).frame_size(100, 50, 1.0), (50, 50)); + assert_eq!(m(crop(10, 10, 10, 10)).frame_size(100, 50, 1.0), (80, 30)); + assert_eq!(m(crop(i32::MAX, 0, 0, 0)).frame_size(100, 50, 1.0), (100, 1)); + } + use super::*; + #[test] + fn sanitize_limits_the_name_in_bytes() { + // 255 two-byte characters are cut to whole characters plus a 17-byte hash suffix. + let s = sanitize(&"é".repeat(255)); + assert!(s.len() <= 255 && s.len() > 235, "{}", s.len()); + assert!(s.starts_with("éé") && s.is_char_boundary(s.len() - 17)); + // Names that share their first 255 bytes stay distinct, and a name that fits is untouched. + let (a, b) = (sanitize(&format!("{}A", "界".repeat(85))), sanitize(&format!("{}B", "界".repeat(85)))); + assert_ne!(a, b); + assert!(a.len() <= 255 && b.len() <= 255); + assert_eq!(sanitize(&"n".repeat(255)), "n".repeat(255)); + assert_eq!(sanitize(&"é".repeat(127)), "é".repeat(127)); + } + #[test] fn templates_and_sequences() { let v = TemplateVars { comp_name: "Main/Title", project_name: "P", width: 1920, height: 1080, frame_rate: 29.97, start_frame: 0, end_frame: 9 }; assert_eq!(expand_template(DEFAULT_TEMPLATE, OutputFormat::H264, &v), "Main_Title.mp4"); + // A dot-only name is not a parent-folder component; a long one is cut to a file name. + for name in ["..", "."] { + let v = TemplateVars { comp_name: name, ..v }; + assert_eq!(expand_template("out/[compName]/x.[fileExtension]", OutputFormat::Gif, &v), format!("out/{}/x.gif", "_".repeat(name.len()))); + } + let long = "n".repeat(100_000); + let v2 = TemplateVars { comp_name: &long, ..v }; + assert_eq!(expand_template(&"[compName]".repeat(100), OutputFormat::Gif, &v2).len(), 255 * 100); assert_eq!(expand_template("[compName]_[width]x[height]@[frameRate].[fileExtension]", OutputFormat::Gif, &v), "Main_Title_1920x1080@29.97.gif"); assert_eq!(sequence_path("out/a_[#####].png", 42), "out/a_00042.png"); assert_eq!(sequence_path("a###.tif", 7), "a007.tif"); @@ -1847,6 +1934,22 @@ mod tests { assert_eq!(p.sample(&c, 3), FrameSample::Progressive(Tick::from_seconds_f64(0.12))); } + /// A saved Frame Rate override outside the supported rates is clamped, so a short comp cannot + /// ask for billions of output frames. + #[test] + fn frame_rate_override_is_held_to_the_supported_rates() { + let c = Comp::new(64, 48, FrameRate::new(25, 1), Tick::from_seconds_f64(2.0)); + let s = |r| RenderSettings { frame_rate: Some(r), time_span: TimeSpan::LengthOfComp, ..Default::default() }; + let huge = s(FrameRate::new(1_000_000_000_000, 1)); + assert_eq!(huge.rate(&c).as_f64(), crate::limits::MAX_FPS); + assert!(huge.frame_count(&c) <= 2001, "{}", huge.frame_count(&c)); + assert_eq!(s(FrameRate::new(0, 1)).rate(&c), c.frame_rate); + assert_eq!(s(FrameRate::new(60, 1)).rate(&c), FrameRate::new(60, 1)); + let shown = |r| s(r).describe(Some(&c)).into_iter().find(|l| l.0 == "Frame Rate").map(|l| l.1).unwrap_or_default(); + assert!(shown(FrameRate::new(1_000_000_000_000, 1)).contains("renders at 1000.000"), "{}", shown(FrameRate::new(1_000_000_000_000, 1))); + assert_eq!(shown(FrameRate::new(60, 1)), "60.000 fps"); + } + #[test] fn crop_and_resize_sizes() { let c = Comp::new(1920, 1080, FrameRate::new(25, 1), Tick::from_seconds_f64(1.0)); diff --git a/crates/project/src/tests.rs b/crates/project/src/tests.rs index d040c8f0b..80ba3c771 100644 --- a/crates/project/src/tests.rs +++ b/crates/project/src/tests.rs @@ -146,6 +146,18 @@ fn unique_names_and_cycles() { let c = p.comp(cid).unwrap(); assert_eq!(c.unique_layer_name("Red Solid 1"), "Red Solid 2"); assert_eq!(c.unique_layer_name("Shape Layer 1"), "Shape Layer 1"); + // A suffix written another way (`02`) is not the number taken. + let mut d = c.clone(); + for n in ["Red Solid 02", "Red Solid 3"] { + let mut l = d.layers[0].clone(); + l.name = n.into(); + d.layers.push(l); + } + assert_eq!(d.unique_layer_name("Red Solid 1"), "Red Solid 2"); + // Digits that end a word stay in the stem. + let mut e = c.clone(); + e.layers[0].name = "v-b1.mp4".into(); + assert_eq!(e.unique_layer_name("v-b1.mp4"), "v-b1.mp4 2"); assert!(p.comp_contains(cid, cid)); } diff --git a/crates/psd/Cargo.toml b/crates/psd/Cargo.toml index b6849041b..46ad240d7 100644 --- a/crates/psd/Cargo.toml +++ b/crates/psd/Cargo.toml @@ -10,4 +10,5 @@ rust-version.workspace = true workspace = true [dependencies] +craft-psd = { workspace = true } miniz_oxide = "0.9" diff --git a/crates/psd/src/engine_data.rs b/crates/psd/src/engine_data.rs index 20fa1676d..43dec23d5 100644 --- a/crates/psd/src/engine_data.rs +++ b/crates/psd/src/engine_data.rs @@ -1,316 +1,9 @@ -//! The text engine data of type layers (`EngineData` in the `TySh` descriptor): a PostScript-like -//! tree of dictionaries (`<< /Key value >>`), arrays (`[ ]`), numbers, names, booleans and -//! strings (`(þÿ` + UTF-16BE with backslash escapes `)`). Read best-effort. +//! Engine data (the text engine document of type layers, `TySh`): `craft-psd`'s +//! `engine_data`, re-exported. [`text_style`] is its typed view of the first style run. -/// Bytes that end a bare token or a `/Name` (besides whitespace). -const DELIMITERS: &[u8] = b"[]<>/("; +pub use craft_psd::engine_data::{TextStyle, Value, minimal, parse, parse_lossy, write}; -#[derive(Clone, Debug, PartialEq)] -pub enum EValue { - Dict(Vec<(String, EValue)>), - Array(Vec), - Num(f64), - Bool(bool), - Name(String), - Str(String), -} - -impl EValue { - pub fn get(&self, key: &str) -> Option<&EValue> { - match self { - EValue::Dict(v) => v.iter().find(|(k, _)| k == key).map(|(_, v)| v), - _ => None, - } - } - pub fn idx(&self, i: usize) -> Option<&EValue> { - match self { - EValue::Array(v) => v.get(i), - _ => None, - } - } - /// Follow a `/`-separated path; numeric segments index arrays. - pub fn path(&self, p: &str) -> Option<&EValue> { - let mut cur = self; - for seg in p.split('/').filter(|s| !s.is_empty()) { - cur = match seg.parse::() { - Ok(i) => cur.idx(i)?, - Err(_) => cur.get(seg)?, - }; - } - Some(cur) - } - pub fn num(&self) -> Option { - match self { - EValue::Num(n) => Some(*n), - _ => None, - } - } - pub fn str(&self) -> Option<&str> { - match self { - EValue::Str(s) | EValue::Name(s) => Some(s), - _ => None, - } - } -} - -struct P<'a> { - b: &'a [u8], - i: usize, -} - -impl P<'_> { - fn ws(&mut self) { - while self.i < self.b.len() && (self.b[self.i].is_ascii_whitespace() || self.b[self.i] == 0) { - self.i += 1; - } - } - fn peek(&self, s: &[u8]) -> bool { - self.b[self.i..].starts_with(s) - } - fn value(&mut self, depth: usize) -> Option { - if depth > 128 { - return None; - } - self.ws(); - if self.i >= self.b.len() { - return None; - } - if self.peek(b"<<") { - self.i += 2; - let mut v = vec![]; - loop { - self.ws(); - if self.i >= self.b.len() { - return Some(EValue::Dict(v)); - } - if self.peek(b">>") { - self.i += 2; - return Some(EValue::Dict(v)); - } - if self.b[self.i] != b'/' { - // Unexpected token: skip it. - self.i += 1; - continue; - } - let key = self.name(); - let val = self.value(depth + 1)?; - v.push((key, val)); - } - } - match self.b[self.i] { - b'[' => { - self.i += 1; - let mut v = vec![]; - loop { - self.ws(); - if self.i >= self.b.len() { - return Some(EValue::Array(v)); - } - if self.b[self.i] == b']' { - self.i += 1; - return Some(EValue::Array(v)); - } - let start = self.i; - match self.value(depth + 1) { - Some(x) => v.push(x), - None => return Some(EValue::Array(v)), - } - if self.i == start { - self.i += 1; - } - } - } - b'/' => Some(EValue::Name(self.name())), - b'(' => Some(EValue::Str(self.string())), - _ => { - let start = self.i; - while self.i < self.b.len() && !self.b[self.i].is_ascii_whitespace() && !DELIMITERS.contains(&self.b[self.i]) { - self.i += 1; - } - let tok = std::str::from_utf8(&self.b[start..self.i]).unwrap_or(""); - if self.i == start { - self.i += 1; - return Some(EValue::Name(String::new())); - } - Some(match tok { - "true" => EValue::Bool(true), - "false" => EValue::Bool(false), - // "nan", "inf" and overflowing literals parse as f64 but are not numbers here. - t => match t.parse::() { - Ok(n) if n.is_finite() => EValue::Num(n), - _ => EValue::Name(t.to_string()), - }, - }) - } - } - } - fn name(&mut self) -> String { - self.i += 1; // '/' - let start = self.i; - while self.i < self.b.len() && !self.b[self.i].is_ascii_whitespace() && !DELIMITERS.contains(&self.b[self.i]) { - self.i += 1; - } - String::from_utf8_lossy(&self.b[start..self.i]).into_owned() - } - fn string(&mut self) -> String { - self.i += 1; // '(' - let mut raw = vec![]; - while self.i < self.b.len() { - let c = self.b[self.i]; - self.i += 1; - match c { - b'\\' if self.i < self.b.len() => { - let e = self.b[self.i]; - self.i += 1; - raw.push(match e { - b'n' => b'\n', - b'r' => b'\r', - b't' => b'\t', - other => other, - }); - } - b')' => break, - _ => raw.push(c), - } - } - if raw.starts_with(&[0xFE, 0xFF]) { - let u: Vec = raw[2..].as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); - String::from_utf16_lossy(&u).trim_end_matches('\0').to_string() - } else { - String::from_utf8_lossy(&raw).into_owned() - } - } -} - -/// Parse engine data (returns an empty dictionary on garbage). -pub fn parse(b: &[u8]) -> EValue { - let mut p = P { b, i: 0 }; - p.value(0).unwrap_or(EValue::Dict(vec![])) -} - -/// Encode a string the way engine data stores it (UTF-16BE with a BOM, escaped). -pub fn encode_string(s: &str) -> Vec { - let mut out = vec![b'(', 0xFE, 0xFF]; - for u in s.encode_utf16() { - for b in u.to_be_bytes() { - if matches!(b, b'(' | b')' | b'\\') { - out.push(b'\\'); - } - out.push(b); - } - } - out.push(b')'); - out -} - -/// The first style run's character attributes. -#[derive(Clone, Debug, Default, PartialEq)] -pub struct TextStyle { - pub font: Option, - pub size: Option, - /// RGB 0..1. - pub color: Option<[f64; 3]>, - /// 0 left, 1 right, 2 centre, 3+ justified variants. - pub justification: Option, - pub tracking: Option, - pub leading: Option, - pub faux_bold: bool, - pub faux_italic: bool, - /// Paragraph (box) text rather than point text. - pub box_text: bool, -} - -/// Sane upper bound for a font size in points (Photoshop's own limit is 1296 pt). -const MAX_FONT_SIZE: f64 = 10_000.0; -/// Sane bound for tracking (1/1000 em) and leading (points). -const MAX_SPACING: f64 = 100_000.0; - -pub fn text_style(ed: &EValue) -> TextStyle { - let engine = ed.get("EngineDict").unwrap_or(ed); - let resources = ed.get("ResourceDict").or_else(|| ed.get("DocumentResources")); - let run = engine.path("StyleRun/RunArray/0/StyleSheet/StyleSheetData"); - let normal = resources.and_then(|r| r.path("StyleSheetSet/0/StyleSheetData")); - let attr = |k: &str| run.and_then(|r| r.get(k)).or_else(|| normal.and_then(|n| n.get(k))); - let font = - attr("Font").and_then(EValue::num).and_then(|i| resources?.path(&format!("FontSet/{}/Name", i as usize))).and_then(EValue::str).map(str::to_string); - let color = attr("FillColor").and_then(|c| c.get("Values")).and_then(|v| match v { - EValue::Array(a) if a.len() >= 4 => Some([a[1].num()?, a[2].num()?, a[3].num()?]), - _ => None, - }); - let para = engine.path("ParagraphRun/RunArray/0/ParagraphSheet/Properties"); - let para_normal = resources.and_then(|r| r.path("ParagraphSheetSet/0/Properties")); - let justification = - para.and_then(|p| p.get("Justification")).or_else(|| para_normal.and_then(|p| p.get("Justification"))).and_then(EValue::num).map(|n| n as u8); - let auto_leading = attr("AutoLeading").is_none_or(|v| *v != EValue::Bool(false)); - let box_text = engine.path("Rendered/Shapes/Children/0/ShapeType").and_then(EValue::num) == Some(1.0); - TextStyle { - font, - size: attr("FontSize").and_then(EValue::num).filter(|s| *s > 0.0).map(|s| s.min(MAX_FONT_SIZE)), - color, - justification, - tracking: attr("Tracking").and_then(EValue::num).map(|t| t.clamp(-MAX_SPACING, MAX_SPACING)), - leading: if auto_leading { None } else { attr("Leading").and_then(EValue::num).map(|l| l.clamp(-MAX_SPACING, MAX_SPACING)) }, - faux_bold: attr("FauxBold") == Some(&EValue::Bool(true)), - faux_italic: attr("FauxItalic") == Some(&EValue::Bool(true)), - box_text, - } -} - -/// Minimal engine data for a type layer with one style run (fixtures, and what a writer needs). -pub fn minimal(text: &str, font: &str, size: f64, color: [f64; 3], justification: u8) -> Vec { - let mut out = Vec::new(); - out.extend_from_slice(b"\n\n<<\n\t/EngineDict\n\t<<\n\t\t/Editor << /Text "); - out.extend_from_slice(&encode_string(&format!("{text}\r"))); - out.extend_from_slice(b" >>\n\t\t/ParagraphRun << /RunArray [ << /ParagraphSheet << /Properties << /Justification "); - out.extend_from_slice(justification.to_string().as_bytes()); - out.extend_from_slice(b" >> >> >> ] >>\n\t\t/StyleRun << /RunArray [ << /StyleSheet << /StyleSheetData << /Font 0 /FontSize "); - out.extend_from_slice(format!("{size:.1}").as_bytes()); - out.extend_from_slice( - format!(" /FillColor << /Type 1 /Values [ 1.0 {:.5} {:.5} {:.5} ] >> /AutoLeading true >> >> >> ] >>\n\t>>\n", color[0], color[1], color[2]).as_bytes(), - ); - out.extend_from_slice(b"\t/ResourceDict << /FontSet [ << /Name "); - out.extend_from_slice(&encode_string(font)); - out.extend_from_slice(b" /Type 0 >> ] >>\n>>"); - out -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn parses_minimal() { - let b = minimal("Hi (there)", "Arial-BoldMT", 36.0, [1.0, 0.5, 0.0], 2); - let v = parse(&b); - assert_eq!(v.path("EngineDict/Editor/Text").and_then(EValue::str), Some("Hi (there)\r")); - let st = text_style(&v); - assert_eq!(st.font.as_deref(), Some("Arial-BoldMT")); - assert_eq!(st.size, Some(36.0)); - assert_eq!(st.color, Some([1.0, 0.5, 0.0])); - assert_eq!(st.justification, Some(2)); - assert!(!st.box_text); - } - - #[test] - fn garbage_is_harmless() { - let _ = parse(b"<< /A [ 1 2 << /B (unterminated"); - let _ = parse(b"]]]>>>> /x"); - let _ = parse(&[0xFF; 64]); - } - - #[test] - fn non_finite_and_out_of_range_numbers_are_not_numbers() { - let b = b"<< /FontSize nan /Tracking inf /Leading -inf /Other 1e999 >>"; - let v = parse(b); - assert_eq!(v.get("FontSize").and_then(EValue::num), None); - assert_eq!(v.get("Other").and_then(EValue::num), None); - let ed = parse(b"<< /EngineDict << /StyleRun << /RunArray [ << /StyleSheet << /StyleSheetData << /FontSize nan /Tracking inf /Leading -inf /AutoLeading false >> >> >> ] >> >> >>"); - let st = text_style(&ed); - assert_eq!((st.size, st.tracking, st.leading), (None, None, None)); - let ed = parse(b"<< /EngineDict << /StyleRun << /RunArray [ << /StyleSheet << /StyleSheetData << /FontSize 1e300 /Tracking -1e300 /AutoLeading false /Leading 1e300 >> >> >> ] >> >> >>"); - let st = text_style(&ed); - assert_eq!(st.size, Some(MAX_FONT_SIZE)); - assert_eq!(st.tracking, Some(-MAX_SPACING)); - assert_eq!(st.leading, Some(MAX_SPACING)); - } +/// The first style run's character and paragraph attributes of a parsed document. +pub fn text_style(ed: &Value) -> TextStyle { + craft_psd::engine_data::first_style(ed) } diff --git a/crates/psd/src/limits.rs b/crates/psd/src/limits.rs index c1c23b390..6db366c00 100644 --- a/crates/psd/src/limits.rs +++ b/crates/psd/src/limits.rs @@ -107,7 +107,7 @@ impl Budget { /// Inflate a zlib stream to at most `limit` bytes. pub(crate) fn inflate(src: &[u8], limit: usize) -> Result> { - miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(src, limit).map_err(|e| Error::Invalid(format!("zip: {e:?}"))) + miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(src, limit).map_err(|e| Error::Invalid(format!("zip: {:?}", e.status))) } /// Give back what a channel buffer from `Psd::channel_bytes` / `Psd::decode_plane` was charged: diff --git a/crates/psd/src/memory_tests.rs b/crates/psd/src/memory_tests.rs index 108046b66..96ac5c70c 100644 --- a/crates/psd/src/memory_tests.rs +++ b/crates/psd/src/memory_tests.rs @@ -28,10 +28,11 @@ impl Psd { } 2 | 3 => { let rest = r.bytes(r.remaining())?; - let mut v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?; + let mut v = + miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, rowb * h).map_err(|e| Error::Invalid(format!("zip: {:?}", e.status)))?; v.resize(rowb * h, 0); if comp == 3 { - self.unpredict(&mut v, w, h); + self.unpredict(&mut v, w, h, &mut Budget::new(u64::MAX))?; } v } @@ -128,11 +129,12 @@ impl Psd { } 2 | 3 => { let rest = r.bytes(r.remaining())?; - let v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, want * rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?; + let v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, want * rowb * h) + .map_err(|e| Error::Invalid(format!("zip: {:?}", e.status)))?; for (c, p) in planes.iter_mut().enumerate().take(want) { let mut plane = v.get(c * rowb * h..(c + 1) * rowb * h).ok_or(Error::Truncated)?.to_vec(); if comp == 3 { - self.unpredict(&mut plane, w, h); + self.unpredict(&mut plane, w, h, &mut Budget::new(u64::MAX))?; } *p = Some(self.ref_samples(&plane, w, h)); } diff --git a/crates/psd/src/parse.rs b/crates/psd/src/parse.rs index d221b9044..6357b0024 100644 --- a/crates/psd/src/parse.rs +++ b/crates/psd/src/parse.rs @@ -287,7 +287,7 @@ impl Psd { let d = descriptor::read_descriptor(&mut r)?; let text = d.text("Txt ").unwrap_or_default().replace("\r\n", "\n").replace('\r', "\n"); let text = text.strip_suffix('\n').map(str::to_string).unwrap_or(text); - let style = d.raw("EngineData").map(|e| engine_data::text_style(&engine_data::parse(e))).unwrap_or_default(); + let style = d.raw("EngineData").map(|e| engine_data::text_style(&engine_data::parse_lossy(e))).unwrap_or_default(); let bounds = d.obj("bounds").or_else(|| d.obj("boundingBox")).and_then(|o| Some([o.num("Left")?, o.num("Top ")?, o.num("Rght")?, o.num("Btom")?])); l.text = Some(TextInfo { text, transform: t, style, bounds }); diff --git a/crates/psd/src/pixels.rs b/crates/psd/src/pixels.rs index 21a5c8845..290a7b6f4 100644 --- a/crates/psd/src/pixels.rs +++ b/crates/psd/src/pixels.rs @@ -37,7 +37,7 @@ impl Psd { PlaneStream::Rle(counts) => Cow::Owned(Self::unpack_plane(&counts, 0, r, rowb, h, budget)?), PlaneStream::Zip(mut v) => { v.resize(size, 0); - self.zip_plane(comp, &mut v, 0, w, h)?; + self.zip_plane(comp, &mut v, 0, w, h, budget)?; Cow::Owned(v) } }) @@ -90,7 +90,7 @@ impl Psd { Ok(match planes { PlaneStream::Raw => Cow::Borrowed(r.bytes(size)?), PlaneStream::Rle(counts) => Cow::Owned(Self::unpack_plane(counts, c, r, rowb, h, budget)?), - PlaneStream::Zip(v) => Cow::Borrowed(self.zip_plane(comp, v, c, w, h)?), + PlaneStream::Zip(v) => Cow::Borrowed(self.zip_plane(comp, v, c, w, h, budget)?), }) } @@ -106,11 +106,11 @@ impl Psd { } /// Plane `c` of the inflated ZIP data of consecutive planes, un-predicted in place (`comp` 3). - pub(crate) fn zip_plane<'v>(&self, comp: u16, v: &'v mut [u8], c: usize, w: usize, h: usize) -> Result<&'v [u8]> { + pub(crate) fn zip_plane<'v>(&self, comp: u16, v: &'v mut [u8], c: usize, w: usize, h: usize, budget: &mut Budget) -> Result<&'v [u8]> { let rowb = self.row_bytes(w); let plane = v.get_mut(c * rowb * h..(c + 1) * rowb * h).ok_or(Error::Truncated)?; if comp == 3 { - self.unpredict(plane, w, h); + self.unpredict(plane, w, h, budget)?; } Ok(plane) } @@ -132,7 +132,8 @@ impl Psd { if self.depth == 1 { w.div_ceil(8) } else { w * self.bytes_per_sample() } } - pub(crate) fn unpredict(&self, v: &mut [u8], w: usize, h: usize) { + /// Undo ZIP prediction in place; the 32-bit scratch row is charged to `budget`. + pub(crate) fn unpredict(&self, v: &mut [u8], w: usize, h: usize, budget: &mut Budget) -> Result<()> { match self.depth { 16 => { for row in v.chunks_exact_mut(w * 2).take(h) { @@ -145,8 +146,9 @@ impl Psd { } } 32 => { - // One scratch row for the whole plane. - let mut planar = vec![0u8; w * 4]; + // One scratch row for the whole plane, charged: a wide, short layer passes the + // decode's peak check and would otherwise allocate it on top. + let mut planar = budget.filled(w.checked_mul(4).ok_or_else(|| too_large(w, h))?, 0u8)?; #[cfg(test)] counters::bump(&counters::SCRATCH); for row in v.chunks_exact_mut(w * 4).take(h) { @@ -161,6 +163,7 @@ impl Psd { } } } + budget.release(planar.len() as u64); } _ => { for row in v.chunks_exact_mut(w).take(h) { @@ -170,6 +173,7 @@ impl Psd { } } } + Ok(()) } /// Hand every sample of `raw` (`w`×`h`, row-major, in order) to `f` as a float. Samples a short @@ -483,15 +487,17 @@ pub(crate) fn unpack_bits(src: &[u8], row: usize, out: &mut Vec) -> Result<( while i < src.len() && out.len() - start < row { let n = src[i] as i8; i += 1; + // A packet is written only up to the row's end, so the reserved plane never grows. + let room = row - (out.len() - start); if n >= 0 { let k = n as usize + 1; let s = src.get(i..i + k).ok_or(Error::Truncated)?; - out.extend_from_slice(s); + out.extend_from_slice(&s[..k.min(room)]); i += k; } else if n != -128 { let k = (1 - n as isize) as usize; let v = *src.get(i).ok_or(Error::Truncated)?; - out.extend(std::iter::repeat_n(v, k)); + out.extend(std::iter::repeat_n(v, k.min(room))); i += 1; } } diff --git a/crates/psd/src/tests.rs b/crates/psd/src/tests.rs index 074976510..fd041138a 100644 --- a/crates/psd/src/tests.rs +++ b/crates/psd/src/tests.rs @@ -493,3 +493,29 @@ fn rle_composite_matches_the_raw_one() { assert_eq!(rle.layer_pixels(2, true).unwrap().data, raw.layer_pixels(2, true).unwrap().data, "{depth} bit"); } } + +#[test] +fn packbits_packets_stop_at_the_row_end() { + // A 128-byte repeat and a 128-byte literal into 3-byte rows: the reserved row is never + // outgrown (capacity stays what was reserved). + for src in [vec![0x81u8, 7], { + let mut v = vec![0x7fu8]; + v.extend([1u8; 128]); + v + }] { + let mut out = Vec::with_capacity(3); + unpack_bits(&src, 3, &mut out).unwrap(); + assert_eq!(out.len(), 3); + assert_eq!(out.capacity(), 3); + } +} + +#[test] +fn a_failed_inflate_reports_its_status_not_its_output() { + // A stream that inflates a megabyte of zeros, then breaks its checksum. + let mut z = miniz_oxide::deflate::compress_to_vec_zlib(&vec![0u8; 1 << 20], 6); + let n = z.len(); + z[n - 1] ^= 0xff; + let Err(Error::Invalid(msg)) = crate::limits::inflate(&z, 1 << 21) else { panic!("the checksum fails") }; + assert!(msg.len() < 100, "{} bytes", msg.len()); +} diff --git a/crates/psd/src/tests/opt.rs b/crates/psd/src/tests/opt.rs index 908026d07..44bcec071 100644 --- a/crates/psd/src/tests/opt.rs +++ b/crates/psd/src/tests/opt.rs @@ -69,7 +69,7 @@ fn ref_decode_channel(p: &Psd, offset: usize, len: usize, w: usize, h: usize) -> } 2 | 3 => { let rest = r.bytes(r.remaining())?; - let mut v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?; + let mut v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, rowb * h).map_err(|e| Error::Invalid(format!("zip: {:?}", e.status)))?; v.resize(rowb * h, 0); if comp == 3 { ref_unpredict(p.depth, &mut v, w, h); @@ -112,7 +112,8 @@ fn ref_composite(p: &Psd) -> Result { } 2 | 3 => { let rest = r.bytes(r.remaining())?; - let v = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, want * rowb * h).map_err(|e| Error::Invalid(format!("zip: {e:?}")))?; + let v = + miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(rest, want * rowb * h).map_err(|e| Error::Invalid(format!("zip: {:?}", e.status)))?; for (c, pl) in planes.iter_mut().enumerate().take(want) { let mut plane = v.get(c * rowb * h..(c + 1) * rowb * h).ok_or(Error::Truncated)?.to_vec(); if comp == 3 { @@ -448,7 +449,7 @@ fn unpredict_matches_the_reference_and_allocates_one_scratch_row() { let mut a: Vec = (0..w * h * bps).map(|i| (i * 31 % 251) as u8).collect(); let mut b = a.clone(); counters::take(&SCRATCH); - p.unpredict(&mut a, w, h); + p.unpredict(&mut a, w, h, &mut Budget::new(u64::MAX)).unwrap(); let scratch = counters::take(&SCRATCH); let old_allocs = ref_unpredict(depth, &mut b, w, h); assert_eq!(a, b, "{depth} bit"); @@ -473,11 +474,11 @@ fn raw_planes_are_borrowed_and_zip_planes_are_unpredicted_in_place() { let mut v: Vec = (0..3 * 40 * 30).map(|i| (i % 255) as u8).collect(); let range = v.as_ptr_range(); for comp in [2, 3] { - let got = q.zip_plane(comp, &mut v, 1, 40, 30).unwrap(); + let got = q.zip_plane(comp, &mut v, 1, 40, 30, &mut Budget::new(u64::MAX)).unwrap(); assert_eq!(got.len(), 1200); assert!(range.contains(&got.as_ptr()), "compression {comp}"); } - assert!(q.zip_plane(2, &mut v, 3, 40, 30).is_err()); + assert!(q.zip_plane(2, &mut v, 3, 40, 30, &mut Budget::new(u64::MAX)).is_err()); } #[test] diff --git a/crates/raster/Cargo.toml b/crates/raster/Cargo.toml index 7cd4fbe5a..9e11b781d 100644 --- a/crates/raster/Cargo.toml +++ b/crates/raster/Cargo.toml @@ -10,6 +10,7 @@ rust-version.workspace = true workspace = true [dependencies] +craft-exr = { workspace = true } effectcraft-geom = { workspace = true } effectcraft-color = { workspace = true } rayon = { workspace = true } diff --git a/crates/raster/src/blur.rs b/crates/raster/src/blur.rs index c8689a7b6..76e51f865 100644 --- a/crates/raster/src/blur.rs +++ b/crates/raster/src/blur.rs @@ -27,13 +27,25 @@ fn box_h_into(src: &[Px], out: &mut [Px], w: usize, r: usize, repeat: bool) { row[i as usize] } }; + // The starting window: the pixels inside the row once each, and the repeated edge pixels + // counted rather than visited (a radius far past the row would otherwise loop over it). let mut acc = [0.0f32; 4]; - for i in -(r as isize)..=(r as isize) { - let p = get(i); + let inside = r.min(w - 1); + for p in &row[..=inside] { for c in 0..4 { acc[c] += p[c]; } } + if repeat { + let (left, right) = (r as f32, (r - inside) as f32); + for c in 0..4 { + acc[c] += row[0][c] * left; + // A zero count adds nothing (0 * NaN would be NaN). + if right > 0.0 { + acc[c] += row[w - 1][c] * right; + } + } + } for x in 0..w { o[x] = acc.map(|v| v * norm); let add = get(x as isize + r as isize + 1); @@ -65,14 +77,27 @@ fn box_v_into(src: &[Px], out: &mut [Px], w: usize, h: usize, r: usize, repeat: out.par_chunks_mut(w * block).enumerate().for_each(|(bi, chunk)| { let ya = (bi * block) as isize; let mut acc = vec![[0.0f32; 4]; w]; - for i in ya - r as isize..=ya + r as isize { - if let Some(rw) = row(i) { - for (a, p) in acc.iter_mut().zip(rw) { - for c in 0..4 { - a[c] += p[c]; - } + // The starting window: rows inside the image once each, the repeated edge rows counted. + let (lo, hi) = (ya - r as isize, ya + r as isize); + let add = |acc: &mut [Px], rw: &[Px], k: f32| { + // A zero count adds nothing (0 * NaN would be NaN). + if k == 0.0 { + return; + } + for (a, p) in acc.iter_mut().zip(rw) { + for c in 0..4 { + a[c] += p[c] * k; } } + }; + for i in lo.max(0)..=hi.min(h as isize - 1) { + add(&mut acc, &src[i as usize * w..(i as usize + 1) * w], 1.0); + } + if repeat { + let above = (-lo).max(0) as f32; + let below = (hi - (h as isize - 1)).max(0) as f32; + add(&mut acc, &src[0..w], above); + add(&mut acc, &src[(h - 1) * w..h * w], below); } for (yy, o) in chunk.chunks_mut(w).enumerate() { let y = ya + yy as isize; @@ -166,6 +191,9 @@ pub fn gaussian_is_exact(sigma_x: f64, sigma_y: f64) -> bool { sigma_x.max(sigma_y) < EXACT_SIGMA_BELOW } +/// Pixels per tile of [`gauss_pass`]'s vertical pass. +const TILE: usize = 128; + /// One separable pass of a sampled Gaussian (radius 3 sigma) along x or y. fn gauss_pass(img: &Image, sigma: f64, vertical: bool, repeat: bool) -> Image { // Below 1e-3 the kernel is the identity in f64; tiny sigmas would also make 2σ² underflow to 0 (NaN taps). @@ -176,20 +204,65 @@ fn gauss_pass(img: &Image, sigma: f64, vertical: bool, repeat: bool) -> Image { let mut k: Vec = (-r..=r).map(|i| (-((i * i) as f64) / (2.0 * sigma * sigma)).exp()).collect(); let total: f64 = k.iter().sum(); k.iter_mut().for_each(|v| *v /= total); - let w = img.width as usize; + let (w, h) = (img.width as usize, img.height as usize); let mut out = Image::new(img.width, img.height); - out.rows_mut().for_each(|(y, row)| { - for x in 0..w { - let mut acc = [0.0f64; 4]; - for (i, wt) in k.iter().enumerate() { - let d = i as i64 - r; - let (sx, sy) = if vertical { (x as i64, y as i64 + d) } else { (x as i64 + d, y as i64) }; - let p = if repeat { img.get_clamped(sx, sy) } else { img.get(sx, sy) }; - for c in 0..4 { - acc[c] += p[c] as f64 * wt; + // Every output pixel adds its taps in kernel order, in f64, as a per-pixel loop would: the + // result is the same to the bit. Taps outside a transparent edge are skipped (adding `+0.0` + // leaves a sum that started at `+0.0` unchanged). + if vertical { + // Row by row: each output row adds whole input rows (contiguous, vectorises), instead of + // walking a column per pixel (a strided read per tap). + out.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| { + // 128 pixels at a time: the f64 sums (4 KB) stay in L1 across the taps. + for (t, out_tile) in row.chunks_mut(TILE).enumerate() { + let x0 = t * TILE; + let mut acc = [[0.0f64; 4]; TILE]; + for (i, &wt) in k.iter().enumerate() { + let sy = y as i64 + i as i64 - r; + let sy = if repeat { + sy.clamp(0, h as i64 - 1) + } else if sy < 0 || sy >= h as i64 { + continue; + } else { + sy + }; + let start = sy as usize * w + x0; + let Some(src) = img.data.get(start..start + out_tile.len()) else { continue }; + for (a, p) in acc.iter_mut().zip(src) { + for c in 0..4 { + a[c] += p[c] as f64 * wt; + } + } + } + for (o, a) in out_tile.iter_mut().zip(acc.iter()) { + *o = a.map(|v| v as f32); } } - row[x] = acc.map(|v| v as f32); + }); + return out; + } + let n = k.len(); + out.data.par_chunks_mut(w).zip(img.data.par_chunks(w)).for_each(|(row, src)| { + for (x, o) in row.iter_mut().enumerate() { + let mut acc = [0.0f64; 4]; + // Inside the row: the taps are one slice (no per-tap edge checks). + if let Some(taps) = x.checked_sub(r as usize).and_then(|x0| src.get(x0..x0 + n)) { + for (p, wt) in taps.iter().zip(&k) { + for c in 0..4 { + acc[c] += p[c] as f64 * wt; + } + } + } else { + for (i, wt) in k.iter().enumerate() { + let sx = x as i64 + i as i64 - r; + let at = if repeat { Some(sx.clamp(0, w as i64 - 1)) } else { Some(sx) }; + let Some(p) = at.and_then(|i| usize::try_from(i).ok()).and_then(|i| src.get(i)) else { continue }; + for c in 0..4 { + acc[c] += p[c] as f64 * wt; + } + } + } + *o = acc.map(|v| v as f32); } }); out @@ -264,6 +337,107 @@ pub fn radial_blur(img: &Image, center: (f64, f64), amount: f64, zoom: bool) -> mod tests { use super::*; + /// The sampled Gaussian pass as it was (one pixel at a time, bounds-checked taps): the + /// row-wise passes must match it to the bit. + fn gauss_pass_per_pixel(img: &Image, sigma: f64, vertical: bool, repeat: bool) -> Image { + let r = (sigma * 3.0).ceil() as i64; + let mut k: Vec = (-r..=r).map(|i| (-((i * i) as f64) / (2.0 * sigma * sigma)).exp()).collect(); + let total: f64 = k.iter().sum(); + k.iter_mut().for_each(|v| *v /= total); + let mut out = Image::new(img.width, img.height); + for y in 0..img.height as i64 { + for x in 0..img.width as i64 { + let mut acc = [0.0f64; 4]; + for (i, wt) in k.iter().enumerate() { + let d = i as i64 - r; + let (sx, sy) = if vertical { (x, y + d) } else { (x + d, y) }; + let p = if repeat { img.get_clamped(sx, sy) } else { img.get(sx, sy) }; + for c in 0..4 { + acc[c] += p[c] as f64 * wt; + } + } + out.set(x as u32, y as u32, acc.map(|v| v as f32)); + } + } + out + } + + #[test] + fn gauss_pass_is_bit_identical_to_the_per_pixel_kernel() { + // Sizes narrower and shorter than the kernel too (every tap near an edge), one wider + // than three tiles (tiles past the first, and a partial last one), with negative, zero + // and HDR values. + for (w, h) in [(37, 23), (3, 2), (1, 9), (64, 1), (300, 5)] { + let mut img = Image::new(w, h); + for (i, p) in img.data.iter_mut().enumerate() { + let v = ((i * 7919) % 101) as f32 / 37.0 - 0.6; + *p = [v, -v * 0.5, if i % 5 == 0 { 0.0 } else { v * 3.0 }, (v + 0.6).min(1.0)]; + } + for sigma in [0.4, 1.3, 2.9] { + for vertical in [false, true] { + for repeat in [false, true] { + let (a, b) = (gauss_pass(&img, sigma, vertical, repeat), gauss_pass_per_pixel(&img, sigma, vertical, repeat)); + let bits = |i: &Image| i.data.iter().flat_map(|p| p.map(f32::to_bits)).collect::>(); + assert!(bits(&a) == bits(&b), "{w}×{h} sigma {sigma} vertical {vertical} repeat {repeat}"); + } + } + } + } + } + + #[test] + fn a_non_finite_edge_pixel_outside_every_window_is_not_counted() { + // 9 wide, radius 1: the last pixel is in no window of the first pixels; likewise the last row. + let mut src = vec![[1.0f32; 4]; 81]; + src[80] = [f32::NAN; 4]; + let mut out = vec![[0.0f32; 4]; 81]; + box_h_into(&src, &mut out, 9, 1, true); + assert!(out[0].iter().all(|v| v.is_finite()), "{:?}", out[0]); + box_v_into(&src, &mut out, 9, 9, 1, true); + assert!(out[0].iter().all(|v| v.is_finite()), "{:?}", out[0]); + } + + #[test] + fn box_windows_count_repeated_edges_and_take_any_radius() { + // Against a window summed pixel by pixel, for radii inside and far past the image. + let (w, h) = (7usize, 5usize); + let src: Vec = (0..w * h).map(|i| [i as f32, (i % 3) as f32, 1.0, 0.5]).collect(); + let at = |x: isize, y: isize, repeat: bool| -> Px { + if !repeat && (x < 0 || y < 0 || x >= w as isize || y >= h as isize) { + return [0.0; 4]; + } + src[y.clamp(0, h as isize - 1) as usize * w + x.clamp(0, w as isize - 1) as usize] + }; + for repeat in [true, false] { + for r in [1usize, 3, 6, 9, 40] { + let (mut hx, mut vx) = (vec![[0.0; 4]; w * h], vec![[0.0; 4]; w * h]); + box_h_into(&src, &mut hx, w, r, repeat); + box_v_into(&src, &mut vx, w, h, r, repeat); + for y in 0..h as isize { + for x in 0..w as isize { + let (mut eh, mut ev) = ([0.0f32; 4], [0.0f32; 4]); + for d in -(r as isize)..=r as isize { + for c in 0..4 { + eh[c] += at(x + d, y, repeat)[c]; + ev[c] += at(x, y + d, repeat)[c]; + } + } + let n = (2 * r + 1) as f32; + let i = y as usize * w + x as usize; + for c in 0..4 { + assert!((hx[i][c] - eh[c] / n).abs() < 1e-3, "h r={r} repeat={repeat}"); + assert!((vx[i][c] - ev[c] / n).abs() < 1e-3, "v r={r} repeat={repeat}"); + } + } + } + } + } + // A radius of 2^40 finishes at once. + let mut out = vec![[0.0; 4]; w * h]; + box_h_into(&src, &mut out, w, 1 << 40, true); + box_v_into(&src, &mut out.clone(), w, h, 1 << 40, true); + } + #[test] fn gaussian_preserves_mass() { let mut img = Image::new(64, 64); diff --git a/crates/raster/src/channels3d.rs b/crates/raster/src/channels3d.rs index ae2d38303..05473ca80 100644 --- a/crates/raster/src/channels3d.rs +++ b/crates/raster/src/channels3d.rs @@ -66,36 +66,19 @@ impl AuxChannels { /// The layers of the channels (OpenEXR layers: the name before the last `.`; `""` for the /// unnamed layer), in channel order. pub fn layers(&self) -> Vec<&str> { - let mut out: Vec<&str> = vec![]; - for (n, _) in &self.channels { - let (layer, _) = split_channel(n); - if !out.contains(&layer) { - out.push(layer); - } - } - out + craft_exr::channels::layers(self.channels.iter().map(|(n, _)| n.as_str())) } /// A layer's channels as the names to show in red, green, blue and alpha (`""` = none): /// its R, G, B, A channels (or red / green / blue / alpha, X / Y / Z, U / V); a layer of one /// channel (depth, an ID) shows it in all three colours. pub fn layer_rgba(&self, layer: &str) -> [String; 4] { - let chans: Vec<&str> = self.channels.iter().map(|(n, _)| n.as_str()).filter(|n| split_channel(n).0 == layer).collect(); - let pick = |names: &[&str]| -> String { - chans.iter().find(|c| names.iter().any(|n| split_channel(c).1.eq_ignore_ascii_case(n))).map_or(String::new(), |c| c.to_string()) - }; - if let [one] = chans.as_slice() { - return [one.to_string(), one.to_string(), one.to_string(), String::new()]; - } - [pick(&["R", "red", "X", "U"]), pick(&["G", "green", "Y", "V"]), pick(&["B", "blue", "Z"]), pick(&["A", "alpha"])] + craft_exr::channels::layer_rgba(self.channels.iter().map(|(n, _)| n.as_str()), layer) + .map(|i| i.and_then(|i| self.channels.get(i)).map_or(String::new(), |(n, _)| n.clone())) } } -/// A channel name's layer and channel: `diffuse.R` → (`diffuse`, `R`), `ViewLayer.Combined.R` -/// → (`ViewLayer.Combined`, `R`), `Z` → (`""`, `Z`). -pub fn split_channel(name: &str) -> (&str, &str) { - name.rsplit_once('.').unwrap_or(("", name)) -} +pub use craft_exr::channels::split_channel; impl AuxChannels { pub fn depth(&self) -> Option<&[f32]> { @@ -206,42 +189,7 @@ impl AuxChannels { } } -/// MurmurHash3 x86 32-bit (public-domain algorithm by Austin Appleby). -pub fn murmur3_32(data: &[u8], seed: u32) -> u32 { - const C1: u32 = 0xcc9e_2d51; - const C2: u32 = 0x1b87_3593; - let mut h = seed; - let (chunks, tail) = data.as_chunks::<4>(); - for c in chunks { - let mut k = u32::from_le_bytes([c[0], c[1], c[2], c[3]]); - k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2); - h ^= k; - h = h.rotate_left(13).wrapping_mul(5).wrapping_add(0xe654_6b64); - } - let mut k = 0u32; - for (i, b) in tail.iter().enumerate() { - k ^= (*b as u32) << (8 * i); - } - if !tail.is_empty() { - k = k.wrapping_mul(C1).rotate_left(15).wrapping_mul(C2); - h ^= k; - } - h ^= data.len() as u32; - h ^= h >> 16; - h = h.wrapping_mul(0x85eb_ca6b); - h ^= h >> 13; - h = h.wrapping_mul(0xc2b2_ae35); - h ^= h >> 16; - h -} - -/// Cryptomatte hash of a name (the manifest value). -pub fn crypto_hash(name: &str) -> u32 { - let h = murmur3_32(name.as_bytes(), 0); - // Keep the float finite and normal: flip the low exponent bit when it is all 0s or 1s. - let e = (h >> 23) & 0xff; - if e == 0 || e == 255 { h ^ (1 << 23) } else { h } -} +pub use craft_exr::cryptomatte::{hash as crypto_hash, murmur3_32}; /// The ID float stored in Cryptomatte channels for a hash. pub fn crypto_float(hash: u32) -> f32 { diff --git a/crates/raster/src/flow.rs b/crates/raster/src/flow.rs index 180b235bf..3a4a9d6f8 100644 --- a/crates/raster/src/flow.rs +++ b/crates/raster/src/flow.rs @@ -113,15 +113,63 @@ pub fn block_flow(a: &Image, b: &Image, block: u32, radius: i32) -> Flow { block_flow_window(a, b, block, block, radius) } +/// Largest coarse search radius honoured (the effects use at most 64). +const MAX_RADIUS: i32 = 1024; +/// Upper bound on the pixel comparisons of one motion search (about 4·10¹²: minutes of work on +/// many cores). A finer vector grid than this allows is made coarser, by doubling the tile. +const MAX_SEARCH_WORK: f64 = 4.4e12; + +/// The pixel comparisons [`block_flow_window`] makes for tiles of `bs`: every tile, at every +/// pyramid level, tries its candidate vectors with a `window`-sized block, then refines. +/// The pyramid depth follows the requested tile `fine`, not the coarsened one, so a coarser vector +/// grid keeps the full search range. +fn search_work(w: usize, h: usize, bs: usize, fine: usize, win: i64, radius: i64) -> f64 { + let tiles = (w.div_ceil(bs).max(1) as f64) * (h.div_ceil(bs).max(1) as f64); + let reach = (fine as i64).max(win) as usize * 2; + let (mut lw, mut lh, mut top) = (w, h, 0u32); + while top < 3 && lw / 2 >= reach && lh / 2 >= reach { + (lw, lh, top) = (lw / 2, lh / 2, top + 1); + } + let mut per_tile = 5.0 * (win * win) as f64; + for level in 0..=top { + let lbs = (win >> level).max(4) as f64; + let (seeds, r) = if level == top { (1.0, radius as f64) } else { (6.0, 1.0) }; + per_tile += seeds * (2.0 * r + 1.0).powi(2) * lbs * lbs; + } + tiles * per_tile +} + +/// The tile size and radius to search with: `tile`, doubled (while it still fits the frame) and +/// then, if that is not enough, the radius halved, until the search is within [`MAX_SEARCH_WORK`]. +fn planned(w: usize, h: usize, tile: usize, win: i64, radius: i64) -> (usize, i64) { + let (mut bs, mut r) = (tile, radius); + while bs * 2 <= w.min(h) && search_work(w, h, bs, tile, win, r) > MAX_SEARCH_WORK { + bs *= 2; + } + while r > 1 && search_work(w, h, bs, tile, win, r) > MAX_SEARCH_WORK { + r /= 2; + } + (bs, r) +} + /// [`block_flow`] with separate vector spacing and matching window: one vector per /// `tile × tile` tile, each matched with the `window × window` block centred on its tile /// (Timewarp's Vector Detail and Block Size). pub fn block_flow_window(a: &Image, b: &Image, tile: u32, window: u32, radius: i32) -> Flow { - let bs = tile.max(2) as usize; - let win = window.max(4) as i64; + // The matching cost per vector grows with the window's area; 512 is far above any block size the effects offer. + let win = i64::from(window.clamp(4, 512)); + let radius = radius.min(MAX_RADIUS); + let fine = tile.max(2) as usize; + let (bs, radius) = planned(a.width as usize, a.height as usize, fine, win, i64::from(radius)); + let coarse = bs != fine; // Window origin relative to the tile origin. let wo = bs as i64 / 2 - win / 2; let (w, h) = (a.width as usize, a.height as usize); + // A coarsened grid's last (partial) tiles keep their window inside the frame. + let origin = |c: i64, dim: usize| { + let o = c * bs as i64 + wo; + if coarse { o.clamp(0, (dim as i64 - win).max(0)) } else { o } + }; let cols = w.div_ceil(bs).max(1); let rows = h.div_ceil(bs).max(1); let mut flow = Flow { width: a.width, height: a.height, block: bs as u32, cols, rows, v: vec![[0.0; 2]; cols * rows] }; @@ -131,7 +179,7 @@ pub fn block_flow_window(a: &Image, b: &Image, tile: u32, window: u32, radius: i // Pyramids (level 0 = full resolution); stop while blocks still fit. let mut pa = vec![Plane::luma(a)]; let mut pb = vec![Plane::luma(b)]; - while pa.len() < 4 && pa.last().is_some_and(|p| p.w / 2 >= (bs as i64).max(win) as usize * 2 && p.h / 2 >= (bs as i64).max(win) as usize * 2) { + while pa.len() < 4 && pa.last().is_some_and(|p| p.w / 2 >= (fine as i64).max(win) as usize * 2 && p.h / 2 >= (fine as i64).max(win) as usize * 2) { let (na, nb) = (pa.last().map(Plane::half), pb.last().map(Plane::half)); pa.extend(na); pb.extend(nb); @@ -144,14 +192,14 @@ pub fn block_flow_window(a: &Image, b: &Image, tile: u32, window: u32, radius: i let k = 1i64 << level; // The block covers the tile's footprint at this level (at least 4 pixels). let lbs = (win / k).max(4); - let r = if level == top { radius as i64 } else { 1 }; + let r = if level == top { radius } else { 1 }; let prev = cur.clone(); cur = (0..cols * rows) .into_par_iter() .map(|i| { let (c, rr) = ((i % cols) as i64, (i / cols) as i64); - let bx = (c * bs as i64 + wo) / k; - let by = (rr * bs as i64 + wo) / k; + let bx = origin(c, w) / k; + let by = origin(rr, h) / k; // Candidates: this tile's and its neighbours' vectors from the coarser level. let mut seeds: Vec<[i64; 2]> = vec![[0, 0]]; if level != top { @@ -184,12 +232,11 @@ pub fn block_flow_window(a: &Image, b: &Image, tile: u32, window: u32, radius: i } // Sub-pixel refinement (parabola through the SAD of the neighbours) at full resolution. let (la, lb) = (&pa[0], &pb[0]); - let bsi = bs as i64; let wsi = win; let fine: Vec<[f32; 2]> = (0..cols * rows) .into_par_iter() .map(|i| { - let (bx, by) = ((i % cols) as i64 * bsi + wo, (i / cols) as i64 * bsi + wo); + let (bx, by) = (origin((i % cols) as i64, w), origin((i / cols) as i64, h)); let d = cur[i]; let e0 = sad(la, lb, bx, by, wsi, d[0], d[1]); let fit = |em: f32, ep: f32| { @@ -295,6 +342,26 @@ mod tests { s / n as f32 } + #[test] + fn a_fine_vector_grid_with_a_huge_window_is_made_coarser() { + // 8K frame, a vector per 2 px, a 512-px window and a 1024-px search: far past the budget. + let (t, r) = super::planned(7680, 4320, 2, 512, 1024); + assert!(t > 2 && t <= 4320 && super::search_work(7680, 4320, t, 2, 512, r) <= super::MAX_SEARCH_WORK, "{t} {r}"); + // The tile stays inside the frame, and the pyramid (search range) follows the requested tile. + let (t, _) = super::planned(7680, 4320, 2, 512, 710); + assert!(t <= 4320); + // Pixel Motion Blur's finest grid with its largest search keeps its grid on a 1080p frame. + assert_eq!(super::planned(1920, 1080, 2, 16, 64), (2, 64)); + assert_eq!(super::planned(1920, 1080, 8, 64, 4), (8, 4)); + } + + #[test] + fn a_huge_matching_window_is_held_to_the_cap() { + let (a, b) = (texture(520, 520, 0.0), texture(520, 520, 2.0)); + let capped = block_flow_window(&a, &b, 260, 512, 2); + assert_eq!(block_flow_window(&a, &b, 260, u32::MAX, 2).v, capped.v); + } + #[test] fn flow_finds_translation() { let a = texture(128, 96, 0.0); diff --git a/crates/raster/src/lib.rs b/crates/raster/src/lib.rs index fd2fd183b..e18a24c63 100644 --- a/crates/raster/src/lib.rs +++ b/crates/raster/src/lib.rs @@ -31,15 +31,36 @@ pub const MAX_PADDED_SIDE: u32 = 30_000; /// Clamp (colour ≤ alpha ≤ 1, NaN → 0) and round half up to `levels` steps (`inv` = 1 / levels): /// one pixel of an 8/16 bpc buffer. Values are non-negative after the clamp, so the integer /// conversion rounds like `round()` and vectorises. -#[allow(clippy::manual_clamp)] // max/min map NaN to 0; clamp would keep NaN #[inline(always)] pub fn quantize_px(p: &mut Px, levels: f32, inv: f32) { - let a = round_half_up(p[3].max(0.0).min(1.0) * levels) * inv; + *p = quantize_levels(*p, levels, inv).map(|n| n * inv); +} + +/// [`quantize_px`]'s steps: the whole number of `levels` each channel rounds to (0..=levels). +#[allow(clippy::manual_clamp)] // max/min map NaN to 0; clamp would keep NaN +#[inline(always)] +pub fn quantize_levels(p: Px, levels: f32, inv: f32) -> [f32; 4] { + let na = round_half_up(p[3].max(0.0).min(1.0) * levels); + let a = na * inv; let hi = [a, a, a, a]; + let mut n = [0.0, 0.0, 0.0, na]; for c in 0..3 { - p[c] = round_half_up(p[c].max(0.0).min(hi[c]) * levels) * inv; + n[c] = round_half_up(p[c].max(0.0).min(hi[c]) * levels); } - p[3] = a; + n +} + +/// [`quantize_px`] at 8 bpc, as bytes: [`expand_u8`] gives back exactly the pixel +/// [`quantize_px`] writes, so an 8 bpc buffer can be kept in a quarter of the memory. +#[inline(always)] +pub fn quantize_px_u8(p: Px) -> [u8; 4] { + quantize_levels(p, 255.0, 1.0 / 255.0).map(|n| n as u8) +} + +/// The 8 bpc pixel of [`quantize_px_u8`]'s bytes. +#[inline(always)] +pub fn expand_u8(n: [u8; 4]) -> Px { + n.map(|v| f32::from(v) * (1.0 / 255.0)) } /// `v` (0..=65 535) rounded half up. The same either way; the fastest on each target: x86-64's @@ -403,6 +424,23 @@ impl Mask { mod tests { use super::*; + /// Pixels kept as `quantize_px_u8` bytes expand to the 8 bpc pixel `quantize_px` writes, bit + /// for bit: every level, exact halves, out-of-range, negative, NaN and premultiplied colour + /// brighter than its alpha. + #[test] + fn u8_levels_expand_to_the_quantised_pixel() { + let mut values: Vec = (0..=255 * 4).map(|i| i as f32 / (255.0 * 4.0)).collect(); + values.extend([-0.0, -1.0, 1.0, 1.5, f32::NAN, f32::INFINITY, 0.5 / 255.0, 254.5 / 255.0]); + for &a in &[1.0, 0.5, 0.2, 0.0, 2.0, f32::NAN] { + for &v in &values { + let p = [v, 1.0 - v, v * 0.5, a]; + let mut want = p; + quantize_px(&mut want, 255.0, 1.0 / 255.0); + assert_eq!(expand_u8(quantize_px_u8(p)).map(f32::to_bits), want.map(f32::to_bits), "{p:?}"); + } + } + } + #[test] fn dither_keeps_exact_codes_and_averages_the_rest() { // A value on an 8-bit code is that code at every position. diff --git a/crates/render/src/accel.rs b/crates/render/src/accel.rs index 0f271c59d..21f609574 100644 --- a/crates/render/src/accel.rs +++ b/crates/render/src/accel.rs @@ -21,16 +21,35 @@ pub enum Backend { } impl Backend { + /// The backend `s` names, whatever case it is written in: the project's own values (`cpu`, + /// `gpu`, `auto`), the labels Project Settings ▸ Video Rendering and Effects draws + /// (`GPU Acceleration`, `Software Rendering`) and the older After Effects wording. pub fn parse(s: &str) -> Option { match s.to_ascii_lowercase().as_str() { - "cpu" | "software" | "mercury software only" => Some(Backend::Cpu), - "gpu" | "mercury gpu acceleration" => Some(Backend::Gpu), + "cpu" | "software" | "mercury software only" | "software rendering" => Some(Backend::Cpu), + "gpu" | "mercury gpu acceleration" | "gpu acceleration" => Some(Backend::Gpu), "auto" => Some(Backend::Auto), _ => None, } } } +/// An accelerator's device memory, to size how many frames an export renders on it at once +/// (see [`Accelerator::frame_memory`]). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct FrameMemory { + /// Bytes the accelerator may use (a conservative share of the device, not its free memory). + pub budget: u64, + /// The most its allocator has held while frames rendered (one frame's working memory + /// included). + pub peak: u64, + /// Bytes of that peak one frame needed (what it released again), or the whole budget once + /// frames have had to wait for memory. + pub frame: u64, + /// Bytes long-lived caches (uploaded layers) may still grow by. + pub reserve: u64, +} + /// One effect of a GPU effect chain (see [`Accelerator::effects`]). pub struct FxStep<'x> { pub spec: &'static effectcraft_effects::EffectSpec, @@ -91,6 +110,12 @@ pub trait Accelerator: Send + Sync { fn auto_pick(&self) -> Option<&AutoPick> { None } + /// The device memory budget and use, after at least one frame rendered: what an export + /// sizes its frames in flight by. `None` where it isn't known (no allocator report, no + /// budget): the export then renders one frame at a time. + fn frame_memory(&self) -> Option { + None + } /// Whether the accelerator can still render frames. `false` once it has stopped for good /// (its device was lost or retired): every frame it is asked for falls back to the CPU, so /// callers that limit frames in flight for it can stop doing so. @@ -349,7 +374,7 @@ impl<'a> Renderer<'a> { pub fn adjustment_footprint(&self, ctx: &EvalCtx, layer: &Layer) -> Option> { // Cached, so an accelerator sees the same buffer (and uploads it once) every frame. let key = self.cache.and_then(|_| cache::footprint_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer))); - let key = self.mask_gate_key(ctx, layer, key); + let key = self.mask_gate_key(ctx, layer, self.clipped_key(ctx, layer, key)); if let Some((b, _)) = self.cached(key) { return Some(b); } diff --git a/crates/render/src/audio.rs b/crates/render/src/audio.rs index 00342847d..2c1538133 100644 --- a/crates/render/src/audio.rs +++ b/crates/render/src/audio.rs @@ -24,6 +24,15 @@ use crate::{EvalCtx, ExprHost, FootageSource}; const MAX_DEPTH: usize = 16; +/// The fastest a layer's audio plays (a Stretch of 1%). +const MAX_SPEED: f64 = 100.0; + +/// The most effect preroll (history before a block) mixed, in seconds, across a layer's effects. +const MAX_PREROLL_SECONDS: f64 = 30.0; + +/// The longest footage a waveform summarises (a day). +const MAX_PEAK_SECONDS: i64 = 24 * 3600; + /// `frames` stereo sample frames of `comp`'s mix from comp time `start` at `rate` Hz, /// interleaved (L R L R …). pub fn mix_comp(project: &Project, footage: &dyn FootageSource, expr: Option<&dyn ExprHost>, comp: ItemId, start: Tick, frames: usize, rate: u32) -> Vec { @@ -35,22 +44,29 @@ pub fn mix_comp(project: &Project, footage: &dyn FootageSource, expr: Option<&dy /// Whether the comp has anything audible (footage with audio, a sound generator, or a precomp /// that has). pub fn comp_has_audio(project: &Project, comp: ItemId) -> bool { - fn walk(project: &Project, comp: ItemId, depth: usize) -> bool { - let Some(c) = project.comp(comp) else { return false }; - depth < MAX_DEPTH - && c.layers.iter().any(|l| { - l.switches.audio - && (generates(l) - || match &l.source { - LayerSource::Footage { item } => { - matches!(project.item(*item).map(|i| &i.kind), Some(ItemKind::Footage(f)) if f.has_audio && !f.missing) - } - LayerSource::Comp { item } => walk(project, *item, depth + 1), - _ => false, - }) - }) + // Breadth first, so each comp is looked at once and at its shallowest level: a comp reached + // first through a long chain (past MAX_DEPTH) is still found by a shorter route, and a cycle + // cannot hide a result (nothing is cached as "silent" while it is being explored). + let mut seen = std::collections::HashSet::from([comp]); + let mut level = vec![comp]; + for _ in 0..MAX_DEPTH { + let mut next = vec![]; + for id in level { + let Some(c) = project.comp(id) else { continue }; + for l in c.layers.iter().filter(|l| l.switches.audio) { + match &l.source { + _ if generates(l) => return true, + LayerSource::Footage { item } if matches!(project.item(*item).map(|i| &i.kind), Some(ItemKind::Footage(f)) if f.has_audio && !f.missing) => { + return true; + } + LayerSource::Comp { item } if seen.insert(*item) => next.push(*item), + _ => {} + } + } + } + level = next; } - walk(project, comp, 0) + false } /// Enabled audio effects of a layer (in stack order) with their effect ids. @@ -157,7 +173,7 @@ impl MixEnv<'_> { let m = b - a; // Within ±1 % to ±9900 % (#3247): a stretch of 1e-300 % set in memory read about // usize::MAX source samples for one block. - let speed = 100.0 / l.effective_stretch(); + let speed = (100.0 / l.effective_stretch()).min(MAX_SPEED); let src_n = ((m as f64 * speed).ceil() as usize).max(1); let lt = l.layer_time(sample_tick(i0 + a as i64, rate)); let buf = match &l.source { @@ -213,7 +229,7 @@ impl MixEnv<'_> { return self.layer_raw(l, i0, n, false); } let reversed = fx.iter().any(|(id, _, _)| audio_fx::reverses(id)); - let pre_s: f64 = fx.iter().map(|(id, _, p)| audio_fx::preroll(id, p)).sum(); + let pre_s: f64 = fx.iter().map(|(id, _, p)| audio_fx::preroll(id, p)).sum::().clamp(0.0, MAX_PREROLL_SECONDS); let pre = (pre_s * self.rate as f64).ceil() as usize; let b0 = i0 - pre as i64; let mut buf = self.layer_raw(l, b0, n + pre, reversed); @@ -342,12 +358,14 @@ pub fn footage_peaks(footage: &dyn FootageSource, item: ItemId, f: &effectcraft_ let bin = (64usize).max(4000 / bins_per_sec as usize); let rate = bin as u32 * bins_per_sec; let chunk = bin * bins_per_sec as usize; - let total = units_ceil(duration, rate).max(0) as usize; + // The stated duration comes from the project: at most a day is summarised, and the summary + // ends where the audio does. + let total = units_ceil(duration, rate).clamp(0, MAX_PEAK_SECONDS.saturating_mul(rate as i64)) as usize; let mut out = Vec::with_capacity(total / bin + 1); let mut i = 0usize; while i < total { let n = chunk.min(total - i); - let buf = footage.audio(item, f, sample_tick(i as i64, rate), n, rate).unwrap_or_else(|| vec![0.0; n * 2]); + let Some(buf) = footage.audio(item, f, sample_tick(i as i64, rate), n, rate) else { break }; out.extend(peak_summary(&buf, bin)); i += n; } @@ -375,6 +393,55 @@ mod tests { } } + #[test] + fn audio_detection_answers_each_comp_once() { + // Twenty levels, each comp holding two layers of the one below: 2^20 walks without a + // memo, and all silent. A cycle is answered too. + let mut p = Project::default(); + let mk = |p: &mut Project, layers: Vec| { + let mut c = Comp::new(8, 8, FrameRate::new(25, 1), Tick::from_seconds_f64(1.0)); + c.layers = layers.into(); + p.add_item("C", effectcraft_color::Label::Sandstone, None, ItemKind::Comp(c.into())) + }; + let base = Comp::new(8, 8, FrameRate::new(25, 1), Tick::from_seconds_f64(1.0)); + let mut below = mk(&mut p, vec![]); + for _ in 0..20 { + let a = build::layer(&mut p, &base, "a", LayerSource::Comp { item: below }, (0, 0), None); + let b = build::layer(&mut p, &base, "b", LayerSource::Comp { item: below }, (0, 0), None); + below = mk(&mut p, vec![a, b]); + } + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || tx.send(comp_has_audio(&p, below)).ok()); + assert!(!rx.recv_timeout(std::time::Duration::from_secs(20)).expect("detection finishes")); + } + + #[test] + fn audio_found_by_a_short_route_is_not_hidden_by_a_deep_one() { + let mut p = Project::default(); + let base = Comp::new(8, 8, FrameRate::new(25, 1), Tick::from_seconds_f64(1.0)); + let mk = |p: &mut Project, layers: Vec| { + let mut c = base.clone(); + c.layers = layers.into(); + p.add_item("C", effectcraft_color::Label::Sandstone, None, ItemKind::Comp(c.into())) + }; + let f = Footage { kind: FootageKind::Audio, has_audio: true, ..Default::default() }; + let fid = p.add_item("x.wav", effectcraft_color::Label::SeaFoam, None, ItemKind::Footage(f)); + let sound = build::layer(&mut p, &base, "s", LayerSource::Footage { item: fid }, (0, 0), None); + let audible = mk(&mut p, vec![sound]); + // A chain of MAX_DEPTH comps ends in `audible` one level past what is searched; the root + // reaches `audible` directly as well, after the chain. + let mut chain = audible; + for _ in 0..MAX_DEPTH { + let l = build::layer(&mut p, &base, "c", LayerSource::Comp { item: chain }, (0, 0), None); + chain = mk(&mut p, vec![l]); + } + assert!(!comp_has_audio(&p, chain), "too deep to be found"); + let via_chain = build::layer(&mut p, &base, "chain", LayerSource::Comp { item: chain }, (0, 0), None); + let direct = build::layer(&mut p, &base, "direct", LayerSource::Comp { item: audible }, (0, 0), None); + let root = mk(&mut p, vec![via_chain, direct]); + assert!(comp_has_audio(&p, root)); + } + #[test] fn mixes_layers_with_levels_and_in_out() { let mut p = Project::default(); diff --git a/crates/render/src/auto.rs b/crates/render/src/auto.rs index 39b3b02e6..ce714c1f0 100644 --- a/crates/render/src/auto.rs +++ b/crates/render/src/auto.rs @@ -10,6 +10,13 @@ //! (its first frame, with layer cache misses and texture uploads, is not counted; the fastest of //! the next ones starts the average), and the slower side is re-measured every //! [`AutoPick::REPROBE`] frames so the choice follows the comp as it changes. +//! +//! Frames render several at a time (a preview's lanes), and a frame is only measured once it +//! is done: so at most one GPU warm-up frame, or re-probe frame on either side, runs at once, +//! and the frames asked for meanwhile go to the other side. And a frame's time counts per +//! frame running on its side at once: twelve CPU frames that each take 200 ms side by side cost +//! about 17 ms a frame, against a GPU frame of 175 ms on its own. Otherwise every lane of a starting preview took a +//! warm-up frame on a GPU that turned out 2–4× slower (an integrated GPU), all at once. use std::collections::HashMap; use std::sync::Mutex; @@ -51,7 +58,36 @@ pub struct AutoStats { /// accelerator, see [`Accelerator::auto_pick`](crate::Accelerator::auto_pick)). #[derive(Default)] pub struct AutoPick { - stats: Mutex>, + stats: Mutex>, +} + +/// One key's history, when its running warm-up or re-probe frame started on each side, and +/// how many of its frames are running on each side (`[CPU, GPU]`). +#[derive(Default)] +struct Entry { + stats: AutoStats, + measuring: [Option; 2], + // Lowered by `record`, `declined` and `abandoned`: every frame asked for ends in one (an + // [`AutoTicket`] makes sure of it). + running: [u32; 2], +} + +impl Entry { + /// A warm-up or re-probe frame is running on `gpu`'s side. It counts as done once + /// [`AutoPick::MEASURING_FOR`] has passed (a frame that failed without reporting back). + fn measuring(&self, gpu: bool) -> bool { + self.measuring[usize::from(gpu)].is_some_and(|t| t.elapsed() < AutoPick::MEASURING_FOR) + } + + /// Render the next warm-up or re-probe frame on `gpu`'s side, unless one is running there: + /// then the other side's (`!gpu`). + fn measure(&mut self, gpu: bool) -> bool { + if self.measuring(gpu) { + return !gpu; + } + self.measuring[usize::from(gpu)] = Some(web_time::Instant::now()); + gpu + } } impl AutoPick { @@ -66,19 +102,43 @@ impl AutoPick { pub const MARGIN: f64 = 0.85; /// Keys remembered before the history is reset. const MAX_KEYS: usize = 1024; + /// Frames counted as running on one side at most (see `Entry::running`). + const MAX_RUNNING: u32 = 256; + /// A warm-up or re-probe frame not reported back by then no longer holds its side. + const MEASURING_FOR: std::time::Duration = std::time::Duration::from_secs(10); - /// Render the frame for `key` on the GPU? + /// [`AutoPick::choose`] for a frame that ends where it started: the ticket reports back, and + /// a ticket dropped without a time (an error, a retry elsewhere, a panic, a cancelled future) + /// ends the frame with [`AutoPick::abandoned`]. + pub fn ticket(&self, key: AutoKey) -> AutoTicket<'_> { + AutoTicket { pick: self, key, gpu: self.choose(key), open: true } + } + + /// Render the frame for `key` on the GPU? Every frame asked for reports back with + /// [`AutoPick::record`] (or [`AutoPick::declined`]), or ends with [`AutoPick::abandoned`] + /// ([`AutoPick::ticket`] does that on its own). pub fn choose(&self, key: AutoKey) -> bool { let Ok(mut m) = self.stats.lock() else { return true }; if m.len() > Self::MAX_KEYS && !m.contains_key(&key) { m.clear(); } - let s = m.entry(key).or_default(); - // Warm up both sides, alternating, GPU first. + let e = m.entry(key).or_default(); + let gpu = Self::pick(e); + let n = &mut e.running[usize::from(gpu)]; + *n = n.saturating_add(1).min(Self::MAX_RUNNING); + gpu + } + + fn pick(e: &mut Entry) -> bool { + let s = e.stats; + // Warm up both sides, alternating, CPU first: the first frames asked for are the + // ones on screen, and the CPU is the side that is never slow to start (no pipelines + // to build; an integrated GPU took 2 s for the frame at the playhead). let gpu_warm = s.declined || s.gpu_frames >= Self::WARMUP; - if !gpu_warm && s.gpu_frames <= s.cpu_frames { - return true; + if !gpu_warm && s.gpu_frames < s.cpu_frames { + return e.measure(true); } + // (CPU frames already render side by side, on lanes of their own.) if s.cpu_frames < Self::WARMUP { return false; } @@ -86,12 +146,12 @@ impl AutoPick { (Some(g), Some(c)) => c >= g * Self::MARGIN, (g, _) => g.is_some(), }; - // Periodically re-measure the slower side. - if s.since_probe >= Self::REPROBE { - s.since_probe = 0; - return !gpu_faster; + // Periodically re-measure the slower side (one frame at a time). + if s.since_probe >= Self::REPROBE && !e.measuring(!gpu_faster) { + e.stats.since_probe = 0; + return e.measure(!gpu_faster); } - s.since_probe += 1; + e.stats.since_probe = s.since_probe.saturating_add(1); gpu_faster } @@ -103,7 +163,13 @@ impl AutoPick { /// period). pub fn record(&self, key: AutoKey, gpu: bool, ms: f64) { let Ok(mut m) = self.stats.lock() else { return }; - let s = m.entry(key).or_default(); + let e = m.entry(key).or_default(); + e.measuring[usize::from(gpu)] = None; + // Per frame running on this side alongside it (itself included). + let n = &mut e.running[usize::from(gpu)]; + let ms = ms / f64::from((*n).max(1)); + *n = n.saturating_sub(1); + let s = &mut e.stats; let (avg, frames) = if gpu { (&mut s.gpu_ms, &mut s.gpu_frames) } else { (&mut s.cpu_ms, &mut s.cpu_frames) }; *frames = frames.saturating_add(1); if gpu { @@ -123,16 +189,36 @@ impl AutoPick { /// The GPU did not handle a frame for `key` (it rendered on the CPU instead). pub fn declined(&self, key: AutoKey) { if let Ok(mut m) = self.stats.lock() { - let s = m.entry(key).or_default(); + let e = m.entry(key).or_default(); + e.measuring[1] = None; + // The frame renders on the CPU instead (and reports back from there). + e.running[1] = e.running[1].saturating_sub(1); + e.running[0] = e.running[0].saturating_add(1).min(Self::MAX_RUNNING); + let s = &mut e.stats; s.declined = true; s.gpu_ms = None; s.since_probe = 0; } } + /// A frame for `key` asked of `gpu`'s side ends without a time (it failed, or was replaced + /// before it finished). Without this its side stays counted as running, and every later + /// frame there is timed as one of several running side by side: that side looks cheaper. + // ponytail: clears the side's warm-up/probe mark even when another frame holds it; that + // only lets the next probe start early. + pub fn abandoned(&self, key: AutoKey, gpu: bool) { + if let Ok(mut m) = self.stats.lock() + && let Some(e) = m.get_mut(&key) + { + e.measuring[usize::from(gpu)] = None; + let n = &mut e.running[usize::from(gpu)]; + *n = n.saturating_sub(1); + } + } + /// The history of `key`. pub fn stats(&self, key: AutoKey) -> Option { - self.stats.lock().ok()?.get(&key).copied() + self.stats.lock().ok()?.get(&key).map(|e| e.stats) } /// Forget all history (settings that change both paths' costs). @@ -142,3 +228,42 @@ impl AutoPick { } } } + +/// One frame [`AutoPick`] chose a side for ([`AutoPick::ticket`]): it reports the frame's time +/// once, and ends the frame unmeasured when dropped without one. +#[must_use = "a ticket dropped at once ends its frame unmeasured"] +pub struct AutoTicket<'a> { + pick: &'a AutoPick, + key: AutoKey, + gpu: bool, + open: bool, +} + +impl AutoTicket<'_> { + /// The frame renders on the GPU. + pub fn gpu(&self) -> bool { + self.gpu + } + + /// The frame took `ms` on its side. + pub fn record(mut self, ms: f64) { + self.open = false; + self.pick.record(self.key, self.gpu, ms); + } + + /// The GPU did not take the frame: it renders on the CPU, and the ticket reports from there. + pub fn declined(&mut self) { + if self.gpu { + self.pick.declined(self.key); + self.gpu = false; + } + } +} + +impl Drop for AutoTicket<'_> { + fn drop(&mut self) { + if self.open { + self.pick.abandoned(self.key, self.gpu); + } + } +} diff --git a/crates/render/src/cache.rs b/crates/render/src/cache.rs index 8a3d7b8ae..4f72e79fa 100644 --- a/crates/render/src/cache.rs +++ b/crates/render/src/cache.rs @@ -230,6 +230,11 @@ impl LayerCache { /// used. (A render that started before a drop may publish its buffer after the drop's /// purge, Reload Footage clearing the cache while the buffer was being written: the check /// here, not the order of the two, keeps it from being served.) + /// Whether a buffer is kept in memory under `key` (a peek: no hit, no miss, no disk read). + pub(crate) fn holds(&self, key: u64) -> bool { + self.kept(key, false).is_some() + } + fn kept(&self, key: u64, hit: bool) -> Option<(Arc, Arc)> { let found = { let mut g = self.inner.lock().ok()?; @@ -356,10 +361,11 @@ pub trait LayerStore: Send + Sync { /// mid-render). An entry whose files changed, or damaged, is a miss and is dropped, so the /// buffer rendered again replaces it. `virtual_id` names files served from memory. fn get_layer(&self, key: u128, virtual_id: Option>) -> Option<(Arc, ReadFiles)>; - /// Keep a buffer that was slow to render, with the source files it was made of. `since`: the + /// Keep a buffer that was slow to render, with the source files it was made of (shared, so + /// a store can encode it off the render thread). `since`: the /// [`LayerStore::drop_generation`] when its render started (a buffer made of a file dropped /// after that is refused). - fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, since: u64); + fn put_layer(&self, key: u128, buf: &Arc, files: &EntryFiles, since: u64); /// How many times files were dropped from the store (File ▸ Reload Footage, /// [`crate::disk_cache::DiskCache::drop_files`]); `0` for a store that never drops. fn drop_generation(&self) -> u64 { @@ -535,7 +541,7 @@ impl LayerStore for PrefetchStore { } /// Queues the layer for writing to the persistent store the first time `key` is seen. - fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, _since: u64) { + fn put_layer(&self, key: u128, buf: &Arc, files: &EntryFiles, _since: u64) { let Ok(mut s) = self.st.lock() else { return }; if s.known.insert(key) { s.writes.push((key, crate::disk_cache::layer_entry(buf, files))); @@ -689,7 +695,9 @@ fn reads_other_times(layer: &Layer, effects: Option) -> bool { fx.groups().take(effects.unwrap_or(usize::MAX)).any(|g| { g.enabled && matches!(&g.kind, effectcraft_project::GroupKind::Effect { effect } - if effect.starts_with("ec.time.") || effectcraft_effects::reads_param_history(effect)) + if effect.starts_with("ec.time.") + || effectcraft_effects::reads_param_history(effect) + || (!effect.starts_with("ec.") && effectcraft_effects::plugin::reads_other_times(effect))) }) }) } @@ -976,7 +984,7 @@ mod shared_insert_tests { assert!(store.get_layer(5, Some(&two)).is_none()); assert!(!store.has(5)); assert_eq!(store.take_misses(), [5]); - store.put_layer(5, &buf(), &EntryFiles::of(&files, Some(&two)).unwrap(), 0); + store.put_layer(5, &Arc::new(buf()), &EntryFiles::of(&files, Some(&two)).unwrap(), 0); assert_eq!(store.take_writes().len(), 1, "written over the changed entry"); // Through the layer cache: a hit reports its files; incomplete lists are not queued. let cache = LayerCache::default(); @@ -1058,7 +1066,7 @@ mod shared_insert_tests { fn get_layer(&self, key: u128, virtual_id: Option>) -> Option<(Arc, crate::reads::ReadFiles)> { super::LayerStore::get_layer(&*self.dc, key, virtual_id) } - fn put_layer(&self, key: u128, buf: &Buf, files: &crate::disk_cache::EntryFiles, since: u64) { + fn put_layer(&self, key: u128, buf: &Arc, files: &crate::disk_cache::EntryFiles, since: u64) { self.arrived.wait(); self.go_on.wait(); super::LayerStore::put_layer(&*self.dc, key, buf, files, since); diff --git a/crates/render/src/cache/dependencies.rs b/crates/render/src/cache/dependencies.rs index dc4216098..6f14ddcde 100644 --- a/crates/render/src/cache/dependencies.rs +++ b/crates/render/src/cache/dependencies.rs @@ -33,6 +33,8 @@ impl Dependencies<'_> { { for g in fx.groups().take(limit.unwrap_or(usize::MAX)).filter(|g| g.enabled) { self.group(ctx, layer, g, 0)?; + // A LUT file edited on disk changes the pixels without an edit (#3493). + crate::reads::lut_files(ctx, layer, g).hash(self.h); } } self.other_times = previous; @@ -169,6 +171,14 @@ impl Dependencies<'_> { ItemKind::Comp(comp) => { let time = ctx.source_time(layer); time.0.hash(self.h); + // The nested layers are walked without this instance's Essential + // Properties overrides, which the render applies: a LUT file one of + // them names is read too (#3493). + for o in crate::fx_host::essential_overrides(ctx, layer) { + if let effectcraft_keyframe::Value::Str(src) = &o.value { + crate::reads::source_files(src).hash(self.h); + } + } let nested = EvalCtx::new(ctx.project, item, comp, time); for child in &comp.layers { self.layer(&nested, child)?; diff --git a/crates/render/src/color.rs b/crates/render/src/color.rs index c423665d2..158dde42f 100644 --- a/crates/render/src/color.rs +++ b/crates/render/src/color.rs @@ -153,6 +153,26 @@ impl Pipe { p } + /// A copy of `src` converted by `c` and quantised to the bit depth, in one pass: one read and + /// one write of the frame instead of a copy, a conversion and a quantise pass (a 1080p float + /// frame is 33 MB, and these passes are limited by memory speed). The same pixels as + /// [`convert`] then [`Pipe::quantize`]. + pub fn copy_converted(&self, src: &Image, c: Option<&Conversion>) -> Image { + let levels = self.levels; + let data = src + .data + .par_iter() + .map(|&p| { + let mut p = c.map_or(p, |c| convert_px(p, c)); + if let Some(l) = levels { + quantize_px(&mut p, l, 1.0 / l); + } + p + }) + .collect(); + Image { width: src.width, height: src.height, data } + } + /// Clamp and quantise to the bit depth (no-op in 32 bpc). pub fn quantize(&self, img: &mut Image) { self.quantize_region(img, Region::Full); @@ -265,14 +285,18 @@ fn quantize_rect(img: &mut Image, r: PxRect, levels: f32) { /// Convert premultiplied pixels (on straight colour). pub fn convert(img: &mut Image, c: &Conversion) { - img.data.par_iter_mut().for_each(|p| { - let a = p[3]; - if a <= 0.0 { - return; - } - let o = c.apply([p[0] / a, p[1] / a, p[2] / a]); - *p = [o[0] * a, o[1] * a, o[2] * a, a]; - }); + img.data.par_iter_mut().for_each(|p| *p = convert_px(*p, c)); +} + +/// [`convert`] of one pixel. +#[inline] +fn convert_px(p: [f32; 4], c: &Conversion) -> [f32; 4] { + let a = p[3]; + if a <= 0.0 { + return p; + } + let o = c.apply([p[0] / a, p[1] / a, p[2] / a]); + [o[0] * a, o[1] * a, o[2] * a, a] } /// Compand: an exponential knee from 80%, approaching 1.0 (linear light, per channel). @@ -349,6 +373,35 @@ mod tests { } } + /// The fused footage pass gives the pixels of a copy, a conversion and a quantise pass, bit + /// for bit, at every depth, with and without a conversion. + #[test] + fn copy_converted_is_convert_then_quantize() { + let mut img = Image::new(17, 5); + for (i, p) in img.data.iter_mut().enumerate() { + *p = match i % 7 { + 0 => [0.3, -0.2, 0.71, 0.9], + 1 => [-0.0, 0.0, 0.0, 0.0], + 2 => [1.7, 0.4, f32::NAN, 0.5], + 3 => [0.0; 4], + _ => [i as f32 / 90.0, 0.5, 0.25, 1.0], + }; + } + for depth in [effectcraft_project::BitDepth::Bpc8, effectcraft_project::BitDepth::Bpc16, effectcraft_project::BitDepth::Bpc32] { + let pipe = Pipe::of(&ProjectSettings { bit_depth: depth, ..Default::default() }); + for c in [None, Some(Conversion::linearize(ColorSpace::Srgb))] { + let mut want = img.clone(); + if let Some(c) = &c { + convert(&mut want, c); + } + pipe.quantize(&mut want); + let got = pipe.copy_converted(&img, c.as_ref()); + let bits = |i: &Image| i.data.iter().map(|p| p.map(f32::to_bits)).collect::>(); + assert_eq!(bits(&got), bits(&want), "{depth:?} conversion {}", c.is_some()); + } + } + } + /// Both forms `round_half_up` picks between agree on everything quantising feeds it: /// 0..=levels, exact halves included. #[test] diff --git a/crates/render/src/disk_cache.rs b/crates/render/src/disk_cache.rs index f509a5896..3674b2667 100644 --- a/crates/render/src/disk_cache.rs +++ b/crates/render/src/disk_cache.rs @@ -58,6 +58,10 @@ const QUEUE_LIMIT: usize = 64; /// frames, so a fully cached comp came back with gaps in its blue bar. #[cfg(not(target_arch = "wasm32"))] const FRAME_QUEUE_BYTES: usize = 1 << 30; +/// Pending layer and matte bytes before new ones are dropped: a layer job holds its float +/// buffer until the writer compresses it (64 of them at 4K were ~8.5 GB). +#[cfg(not(target_arch = "wasm32"))] +const LAYER_QUEUE_BYTES: usize = 1 << 30; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Kind { @@ -264,18 +268,50 @@ impl LayerPrefetch { #[cfg_attr(target_arch = "wasm32", allow(dead_code))] enum Job { /// Kind, key, header format byte, payload. - Write(Kind, u128, u8, Vec), + Write(Kind, u128, u8, Payload), /// Delete the frame and layer entries made of these files ([`DiskCache::drop_files`]). Drop(Vec>), } +/// What a queued write holds. 8-bit frames and layer buffers are encoded (LZ4) on the writer +/// thread, not on the render thread that queued them: encoding a 1080p frame took ~9 ms of every +/// frame's render, and a full queue no longer throws away encoding work. +enum Payload { + Encoded(Vec), + /// 8-bit premultiplied RGBA, `width * height * 4` bytes, and the files it was made of. + Frame(u32, u32, Vec, EntryFiles), + Layer(Arc, EntryFiles), +} + +impl Payload { + /// Bytes it holds (the frame queue's budget). + #[cfg_attr(target_arch = "wasm32", allow(dead_code))] + fn len(&self) -> usize { + match self { + Payload::Encoded(p) | Payload::Frame(_, _, p, _) => p.len(), + Payload::Layer(b, _) => b.img.data.len().saturating_mul(std::mem::size_of::()), + } + } + + /// The entry's payload: its source files, then its pixels. + fn encode(self) -> Vec { + match self { + Payload::Encoded(p) => p, + Payload::Frame(w, h, rgba, files) => with_files(&files, encode_frame(w, h, &rgba)), + Payload::Layer(b, files) => with_files(&files, encode_layer(&b)), + } + } +} + struct Queue { /// Frame writes (taken first) and their payload bytes. frames: std::collections::VecDeque, #[cfg_attr(target_arch = "wasm32", allow(dead_code))] frame_bytes: usize, - /// Layer and matte writes. + /// Layer and matte writes, and their payload bytes. jobs: std::collections::VecDeque, + #[cfg_attr(target_arch = "wasm32", allow(dead_code))] + job_bytes: usize, busy: bool, /// Keys of frames queued or being written. pending_frames: std::collections::HashSet, @@ -330,7 +366,21 @@ impl Queue { } return Some(j); } - self.jobs.pop_front() + let j = self.jobs.pop_front()?; + if let Job::Write(_, _, _, p) = &j { + self.job_bytes = self.job_bytes.saturating_sub(p.len()); + } + Some(j) + } + + /// No room for a `kind` write of `len` bytes. A layer bigger than the whole budget is still + /// taken when nothing else is queued. + #[cfg(not(target_arch = "wasm32"))] + fn full(&self, kind: Kind, len: usize) -> bool { + match kind { + Kind::Frame => self.frame_bytes.saturating_add(len) > FRAME_QUEUE_BYTES, + _ => self.jobs.len() >= QUEUE_LIMIT || (self.job_bytes > 0 && self.job_bytes.saturating_add(len) > LAYER_QUEUE_BYTES), + } } fn is_empty(&self) -> bool { self.frames.is_empty() && self.jobs.is_empty() @@ -433,12 +483,54 @@ pub fn default_folder() -> PathBuf { if let Some(x) = std::env::var_os("XDG_CACHE_HOME") { return PathBuf::from(x).join("effectcraft/disk-cache"); } - home.map(|h| h.join(".cache/effectcraft/disk-cache")).unwrap_or_else(|| std::env::temp_dir().join("effectcraft-disk-cache")) + // Without a home folder: a per-user folder in the shared temp folder, made private by `open`. + let user = std::env::var("USER").or_else(|_| std::env::var("USERNAME")).unwrap_or_default(); + home.map(|h| h.join(".cache/effectcraft/disk-cache")).unwrap_or_else(|| std::env::temp_dir().join(format!("effectcraft-disk-cache-{user}"))) +} + +/// A cache folder in the shared temp folder must be ours alone: not a link someone placed there, +/// and private (setting the mode fails unless we own it), so another user cannot redirect the +/// cache's writes and clean-up or read its files. +#[cfg(unix)] +fn make_private(folder: &Path) -> std::io::Result<()> { + use std::os::unix::fs::PermissionsExt; + // Judge the folder as written and as it resolves (a relative path, a link or an alias such as + // /tmp -> /private/tmp can all land in the shared temp folder). + // The shared roots: the process's temp folder (`TMPDIR`) and the system's own, which a cache + // configured by path lands in whatever `TMPDIR` says. + let roots: Vec = [std::env::temp_dir(), PathBuf::from("/tmp"), PathBuf::from("/var/tmp")] + .into_iter() + .flat_map(|t| { + let real = t.canonicalize().unwrap_or_else(|_| t.clone()); + [t, real] + }) + .collect(); + let given = std::path::absolute(folder).unwrap_or_else(|_| folder.to_path_buf()); + let real = folder.canonicalize().unwrap_or_else(|_| given.clone()); + // The shared folder itself is not ours to change or to keep a cache in: refused. + if [&given, &real].iter().any(|p| roots.contains(p)) { + return Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, "the disk cache folder is the shared temp folder")); + } + // Only folders inside it are made private. + if ![&given, &real].iter().any(|p| roots.iter().any(|t| p.starts_with(t) && **p != *t)) { + return Ok(()); + } + if std::fs::symlink_metadata(folder)?.file_type().is_symlink() { + return Err(std::io::Error::new(std::io::ErrorKind::PermissionDenied, "the disk cache folder is a link")); + } + std::fs::set_permissions(folder, std::fs::Permissions::from_mode(0o700)) +} + +#[cfg(not(unix))] +fn make_private(_: &Path) -> std::io::Result<()> { + Ok(()) } impl DiskCache { /// Open (or create) the cache in `folder`, holding at most `max_bytes`. pub fn open(folder: impl AsRef, max_bytes: u64) -> std::io::Result> { + std::fs::create_dir_all(folder.as_ref())?; + make_private(folder.as_ref())?; let dir = folder.as_ref().join(VERSION); std::fs::create_dir_all(&dir)?; let dc = Arc::new(DiskCache { @@ -462,6 +554,7 @@ impl DiskCache { frames: Default::default(), frame_bytes: 0, jobs: Default::default(), + job_bytes: 0, busy: cfg!(not(target_arch = "wasm32")), pending_frames: Default::default(), drops: Drops::default(), @@ -650,6 +743,7 @@ impl DiskCache { #[cfg(target_arch = "wasm32")] { q.jobs.clear(); + q.job_bytes = 0; q.frames.clear(); q.pending_frames.clear(); } @@ -771,20 +865,20 @@ impl DiskCache { /// Queue a write; `false` when it was dropped (a full queue). fn enqueue(&self, kind: Kind, key: u128, payload: Vec) -> bool { - self.enqueue_format(kind, key, FORMAT_RGBA8, payload, None) + self.enqueue_format(kind, key, FORMAT_RGBA8, Payload::Encoded(payload), None) } /// [`DiskCache::enqueue`] with the entry's header format byte. `made`: the source files of a /// frame or layer entry and the drop generation its reads started at /// ([`DiskCache::drop_generation`]); refused (`false`) when one of them was dropped since. - fn enqueue_format(&self, kind: Kind, key: u128, format: u8, payload: Vec, made: Option<(&EntryFiles, u64)>) -> bool { + fn enqueue_format(&self, kind: Kind, key: u128, format: u8, payload: Payload, made: Option<(&EntryFiles, u64)>) -> bool { let stale = |q: &Queue| made.is_some_and(|(files, since)| q.drops.stale(files.files().iter().map(|f| &f.path), since)); #[cfg(target_arch = "wasm32")] { if self.queue.0.lock().is_ok_and(|q| stale(&q)) { return false; } - self.write_format(kind, key, format, &payload); + self.write_format(kind, key, format, &payload.encode()); true } #[cfg(not(target_arch = "wasm32"))] @@ -794,11 +888,7 @@ impl DiskCache { if stale(&q) { return false; } - let full = match kind { - Kind::Frame => q.frame_bytes.saturating_add(payload.len()) > FRAME_QUEUE_BYTES, - _ => q.jobs.len() >= QUEUE_LIMIT, - }; - if full { + if q.full(kind, payload.len()) { self.dropped.fetch_add(1, Ordering::Relaxed); return false; } @@ -807,6 +897,7 @@ impl DiskCache { q.frame_bytes = q.frame_bytes.saturating_add(payload.len()); q.frames.push_back(Job::Write(kind, key, format, payload)); } else { + q.job_bytes = q.job_bytes.saturating_add(payload.len()); q.jobs.push_back(Job::Write(kind, key, format, payload)); } cv.notify_all(); @@ -1033,7 +1124,7 @@ impl DiskCache { } self.count_encode(); let payload = with_files(&files, encode_frame_f16(width, height, rgba)); - self.enqueue_format(Kind::Frame, key, FORMAT_F16_FILES, payload, Some((&files, since))) + self.enqueue_format(Kind::Frame, key, FORMAT_F16_FILES, Payload::Encoded(payload), Some((&files, since))) } /// Write a half-float frame synchronously (tests); a list that can't be stored is not. @@ -1050,6 +1141,22 @@ impl DiskCache { /// render started at). #[allow(clippy::too_many_arguments)] pub fn put_frame(&self, key: u128, width: u32, height: u32, rgba: &[u8], files: &ReadFiles, virtual_id: Option>, since: u64) -> bool { + self.put_frame_owned(key, width, height, rgba.to_vec(), files, virtual_id, since) + } + + /// [`DiskCache::put_frame`] of a frame the caller hands over (no copy; compressed on the + /// writer thread). + #[allow(clippy::too_many_arguments)] + pub fn put_frame_owned( + &self, + key: u128, + width: u32, + height: u32, + rgba: Vec, + files: &ReadFiles, + virtual_id: Option>, + since: u64, + ) -> bool { if rgba.len() as u64 != width as u64 * height as u64 * 4 { return false; } @@ -1057,13 +1164,13 @@ impl DiskCache { if self.contains(Kind::Frame, key) { return true; } - // (no room on the disk: refused before the pixels are compressed) + // (no room on the disk: refused before the pixels are queued and compressed) if !self.accepts_writes() { return false; } self.count_encode(); - let payload = with_files(&files, encode_frame(width, height, rgba)); - self.enqueue_format(Kind::Frame, key, FORMAT_RGBA8_FILES, payload, Some((&files, since))) + let made = files.clone(); + self.enqueue_format(Kind::Frame, key, FORMAT_RGBA8_FILES, Payload::Frame(width, height, rgba, files), Some((&made, since))) } /// A layer buffer (key: [`layer_disk_key`]) whose source files are unchanged, with those @@ -1078,17 +1185,22 @@ impl DiskCache { /// [`EntryFiles::of`]: a buffer whose files can't all be named must not be stored). Refused /// when one of them was dropped after `since`, the [`DiskCache::drop_generation`] its render /// started at. - pub fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, since: u64) { - // (no room on the disk: refused before the buffer is compressed) - if self.contains(Kind::Layer, key) || !self.accepts_writes() { + /// The buffer is shared, and encoded on the writer thread. + pub fn put_layer(&self, key: u128, buf: &Arc, files: &EntryFiles, since: u64) { + // (no room on the disk, or a buffer no entry could hold: refused before it is queued and + // compressed) + if !layer_fits(buf.img.data.len()) || self.contains(Kind::Layer, key) || !self.accepts_writes() { return; } self.count_encode(); - let _ = self.enqueue_format(Kind::Layer, key, FORMAT_LAYER_FILES, with_files(files, encode_layer(buf)), Some((files, since))); + let _ = self.enqueue_format(Kind::Layer, key, FORMAT_LAYER_FILES, Payload::Layer(buf.clone(), files.clone()), Some((files, since))); } /// Write a layer buffer synchronously (tests); a list that can't be stored is not. pub fn put_layer_now(&self, key: u128, buf: &Buf, files: &ReadFiles) { + if !layer_fits(buf.img.data.len()) { + return; + } if let Some(files) = EntryFiles::of(files, None) { self.write_format(Kind::Layer, key, FORMAT_LAYER_FILES, &with_files(&files, encode_layer(buf))); } @@ -1097,8 +1209,12 @@ impl DiskCache { /// A segmentation (chain key, scale) written by [`DiskCache::put_seg`]. pub fn get_seg(&self, key: u64, scale: f64) -> Option { let p = self.read(Kind::Matte, seg_key(key, scale))?; - let raw = lz4_flex::decompress_size_prepended(&p).ok()?; - effectcraft_effects::roto::FrameSeg::from_bytes(&raw) + let seg = decompress_checked(&p, None).and_then(|raw| effectcraft_effects::roto::FrameSeg::from_bytes(&raw)); + if seg.is_none() { + // A rejected entry would otherwise stay indexed and keep `put_seg` from replacing it. + self.discard(Kind::Matte, seg_key(key, scale)); + } + seg } /// Queue a segmentation for writing. @@ -1192,7 +1308,7 @@ fn writer(dc: std::sync::Weak, q: Arc<(Mutex, Condvar)>) { let Some(cache) = dc.upgrade() else { return }; let written = match job { Job::Write(kind, key, format, payload) => { - cache.write_format(kind, key, format, &payload); + cache.write_format(kind, key, format, &payload.encode()); (kind == Kind::Frame).then_some(key) } Job::Drop(paths) => { @@ -1292,7 +1408,7 @@ impl crate::cache::LayerStore for DiskCache { DiskCache::get_layer(self, key, virtual_id).map(|(b, f)| (Arc::new(b), f)) } /// Queues a layer write on the disk cache. - fn put_layer(&self, key: u128, buf: &Buf, files: &EntryFiles, since: u64) { + fn put_layer(&self, key: u128, buf: &Arc, files: &EntryFiles, since: u64) { DiskCache::put_layer(self, key, buf, files, since); } /// The current drop generation of the disk cache. @@ -1337,16 +1453,38 @@ fn encode_frame(w: u32, h: u32, rgba: &[u8]) -> Vec { out } +/// The most bytes one cache entry decompresses to (a 16384² frame of 8-bit RGBA): the size an +/// entry states is checked against it, and against what the entry's dimensions need, before +/// anything is allocated. +const MAX_ENTRY_BYTES: usize = 1 << 30; + +/// An LZ4 payload with its stated size prepended, decompressed only when that size is +/// `expected` (any size up to [`MAX_ENTRY_BYTES`] when `None`). +fn decompress_checked(p: &[u8], expected: Option) -> Option> { + let size = u32::from_le_bytes(p.get(..4)?.try_into().ok()?) as usize; + if size > MAX_ENTRY_BYTES || expected.is_some_and(|e| e != size) { + return None; + } + // A block that ends short of the size it states is damaged: callers index by that size. + lz4_flex::decompress(p.get(4..)?, size).ok().filter(|raw| raw.len() == size) +} + fn decode_frame(p: &[u8]) -> Option { if p.len() < 8 { return None; } let w = u32::from_le_bytes(p[0..4].try_into().ok()?); let h = u32::from_le_bytes(p[4..8].try_into().ok()?); - let rgba = lz4_flex::decompress_size_prepended(&p[8..]).ok()?; + let rgba = decompress_checked(&p[8..], Some((w as usize).checked_mul(h as usize)?.checked_mul(4)?))?; (rgba.len() == w as usize * h as usize * 4).then_some(Frame8 { width: w, height: h, rgba }) } +/// Whether a layer of `pixels` pixels (16 bytes each, decompressed) is within what a cache entry +/// may hold: a larger one could never be read back, so it is not compressed or written. +fn layer_fits(pixels: usize) -> bool { + pixels.checked_mul(16).is_some_and(|b| b <= MAX_ENTRY_BYTES) +} + /// Half floats with their low and high bytes in planes (per channel), then LZ4: the high bytes /// (sign, exponent) repeat a lot, mixed with the low ones LZ4 finds little. fn encode_frame_f16(w: u32, h: u32, rgba: &[u16]) -> Vec { @@ -1374,10 +1512,7 @@ fn decode_frame_f16(p: &[u8]) -> Option { let w = u32::from_le_bytes(p[0..4].try_into().ok()?); let h = u32::from_le_bytes(p[4..8].try_into().ok()?); let n = (w as usize).checked_mul(h as usize)?; - let planes = lz4_flex::decompress_size_prepended(&p[8..]).ok()?; - if planes.len() != n.checked_mul(8)? { - return None; - } + let planes = decompress_checked(&p[8..], Some(n.checked_mul(8)?))?; let mut rgba = vec![0u16; n * 4]; for (i, px) in rgba.as_chunks_mut::<4>().0.iter_mut().enumerate() { for (c, v) in px.iter_mut().enumerate() { @@ -1420,9 +1555,9 @@ fn decode_layer(p: &[u8]) -> Option { let f = |i: usize| -> Option { Some(f64::from_le_bytes(p[i..i + 8].try_into().ok()?)) }; let offset = [f(8)?, f(16)?]; let scale = f(24)?; - let planes = lz4_flex::decompress_size_prepended(&p[32..]).ok()?; let n = (w as usize).checked_mul(h as usize)?; - if planes.len() != n.checked_mul(16)? { + let planes = decompress_checked(&p[32..], Some(n.checked_mul(16)?))?; + if planes.len() != n * 16 { return None; } let mut img = Image::new(w, h); @@ -1441,7 +1576,7 @@ fn decode_layer(p: &[u8]) -> Option { // ---------------------------------------------------------------- content keys /// Bump when rendering changes in a way that makes old disk entries wrong. -const RENDER_VERSION: &str = "effectcraft-render-6"; +const RENDER_VERSION: &str = "effectcraft-render-7"; /// Feeds the `Debug` form of `v` into the hash, followed by a separator byte. fn hash_debug(h: &mut Hash128, v: &impl std::fmt::Debug) { @@ -1553,6 +1688,82 @@ pub fn frame_key(content: u128, frame: i64, scale_milli: u32, view: u64) -> u128 mod tests { use super::*; + #[cfg(unix)] + #[test] + fn a_cache_folder_in_the_temp_folder_is_made_private_and_never_a_link() { + use std::os::unix::fs::PermissionsExt; + let d = tmpdir("private"); + let dc = DiskCache::open(&d, 1 << 20).unwrap(); + assert_eq!(std::fs::metadata(&d).unwrap().permissions().mode() & 0o777, 0o700); + drop(dc); + let link = tmpdir("link-private-temp"); + std::os::unix::fs::symlink(&d, &link).unwrap(); + assert!(DiskCache::open(&link, 1 << 20).is_err()); + let _ = std::fs::remove_file(&link); + let _ = std::fs::remove_dir_all(&d); + } + + #[cfg(unix)] + #[test] + fn the_shared_temp_folder_itself_is_refused_as_a_cache_folder() { + let tmp = std::env::temp_dir(); + assert!(DiskCache::open(&tmp, 1 << 20).is_err()); + } + + #[cfg(unix)] + #[test] + fn a_folder_that_resolves_into_the_temp_folder_is_made_private() { + use std::os::unix::fs::PermissionsExt; + // `/link` names a folder in the temp folder only through the link. + let real = tmpdir("alias-real"); + std::fs::create_dir_all(&real).unwrap(); + let link = std::env::current_dir().unwrap().join(format!("effectcraft-alias-link-{}", std::process::id())); + let _ = std::fs::remove_file(&link); + std::os::unix::fs::symlink(&real, &link).unwrap(); + let dc = DiskCache::open(link.join("cache"), 1 << 20).unwrap(); + let mode = std::fs::metadata(real.join("cache")).map(|m| m.permissions().mode() & 0o777); + drop(dc); + let _ = std::fs::remove_file(&link); + let _ = std::fs::remove_dir_all(&real); + assert_eq!(mode.unwrap(), 0o700); + } + + #[test] + fn layers_too_big_to_read_back_are_not_written() { + assert!(layer_fits(MAX_ENTRY_BYTES / 16)); + assert!(!layer_fits(MAX_ENTRY_BYTES / 16 + 1)); + assert!(!layer_fits(10_000 * 10_000)); + assert!(!layer_fits(usize::MAX)); + } + + #[test] + fn an_entry_stating_a_size_its_dimensions_do_not_need_is_not_decompressed() { + // A 2×2 frame whose payload states 3 GiB: refused before any allocation. + let mut p = 2u32.to_le_bytes().to_vec(); + p.extend_from_slice(&2u32.to_le_bytes()); + p.extend_from_slice(&(3u32 << 30).to_le_bytes()); + p.extend_from_slice(&[0; 16]); + assert!(decode_frame(&p).is_none()); + assert!(decompress_checked(&p[8..], None).is_none()); + // A well-formed entry still decodes. + let ok = encode_frame(2, 2, &[7; 16]); + assert_eq!(decode_frame(&ok).map(|f| f.rgba), Some(vec![7; 16])); + } + + #[test] + fn a_block_shorter_than_the_size_it_states_is_rejected() { + // The payload states 100 bytes but holds a valid block of 10: `lz4_flex` returns the 10. + let mut p = 100u32.to_le_bytes().to_vec(); + p.extend_from_slice(&lz4_flex::block::compress(&[5u8; 10])); + assert!(decompress_checked(&p, None).is_none()); + // A half-float frame whose planes come up short is refused, not indexed past. + let mut f16 = 2u32.to_le_bytes().to_vec(); + f16.extend_from_slice(&2u32.to_le_bytes()); + f16.extend_from_slice(&32u32.to_le_bytes()); + f16.extend_from_slice(&lz4_flex::block::compress(&[1u8; 8])); + assert!(decode_frame_f16(&f16).is_none()); + } + fn tmpdir(name: &str) -> PathBuf { let d = std::env::temp_dir().join(format!("effectcraft-disk-cache-{name}-{}", std::process::id())); let _ = std::fs::remove_dir_all(&d); @@ -1706,7 +1917,7 @@ mod tests { let dc = DiskCache::open(&dir, 1 << 30).unwrap(); assert!(dc.get_layer(42, None).is_none()); let b = buf(1, 33, 17); - dc.put_layer(42, &b, &EntryFiles::default(), 0); + dc.put_layer(42, &Arc::new(b.clone()), &EntryFiles::default(), 0); dc.flush(); let (back, files) = dc.get_layer(42, None).unwrap(); assert!(files.is_empty() && files.is_complete()); @@ -1715,10 +1926,14 @@ mod tests { let rgba: Vec = (0..20 * 10 * 4).map(|i| (i % 251) as u8).collect(); assert!(dc.put_frame(7, 20, 10, &rgba, &ReadFiles::default(), None, 0)); assert!(!dc.put_frame(8, 2, 2, &[0; 3], &ReadFiles::default(), None, 0), "a wrong-size frame is refused"); + // Handed over and compressed on the writer thread: the same pixels come back. + assert!(dc.put_frame_owned(9, 20, 10, rgba.clone(), &ReadFiles::default(), None, 0)); + assert!(!dc.put_frame_owned(10, 2, 2, vec![0; 3], &ReadFiles::default(), None, 0), "a wrong-size frame is refused"); dc.flush(); assert_eq!(dc.get_frame(7, None).map(|f| f.0).unwrap().rgba, rgba); + assert_eq!(dc.get_frame(9, None).map(|f| f.0).unwrap().rgba, rgba); let st = dc.stats(); - assert_eq!((st.entries, st.hits, st.misses, st.writes), (2, 2, 1, 2)); + assert_eq!((st.entries, st.hits, st.misses, st.writes), (3, 3, 1, 3)); // Wrong kind is a miss. assert!(dc.get_frame(42, None).map(|f| f.0).is_none()); } @@ -1773,6 +1988,36 @@ mod tests { assert_ne!(comp_content_key(&p, cid), key, "the expression reads the model's name"); } + /// Layer jobs hold float buffers until they are compressed: the queue holds at most + /// [`LAYER_QUEUE_BYTES`] of them (64 at 4K held ~8.5 GB), and a single bigger one only alone. + #[test] + fn layer_writes_queue_within_a_byte_budget() { + let mut q = Queue { + frames: Default::default(), + frame_bytes: 0, + jobs: Default::default(), + job_bytes: 0, + busy: true, + pending_frames: Default::default(), + drops: Drops::default(), + }; + let layer = LAYER_QUEUE_BYTES / 4 + 1; + for _ in 0..3 { + assert!(!q.full(Kind::Layer, layer)); + q.jobs.push_back(Job::Write(Kind::Layer, 0, 0, Payload::Encoded(vec![]))); + q.job_bytes += layer; + } + assert!(q.full(Kind::Layer, layer), "a fourth would pass the budget"); + assert!(!q.full(Kind::Frame, 1024), "frames have their own budget"); + let mut empty = Queue { jobs: Default::default(), job_bytes: 0, ..q }; + assert!(!empty.full(Kind::Layer, 2 * LAYER_QUEUE_BYTES), "a single big layer, alone"); + // Taking a job gives its bytes back. + empty.job_bytes = 3; + empty.jobs.push_back(Job::Write(Kind::Layer, 0, 0, Payload::Encoded(vec![0; 3]))); + assert!(empty.pop().is_some()); + assert_eq!(empty.job_bytes, 0); + } + /// A burst of layer writes never crowds out frames: every frame of a fully cached comp is /// on disk afterwards (the blue bar came back with gaps). #[test] @@ -1780,7 +2025,7 @@ mod tests { let dir = tmpdir("burst"); let dc = DiskCache::open(&dir, 1 << 30).unwrap(); for k in 0..300 { - dc.put_layer(10_000 + k, &buf(1, 64, 64), &EntryFiles::default(), 0); + dc.put_layer(10_000 + k, &Arc::new(buf(1, 64, 64)), &EntryFiles::default(), 0); } let rgba = vec![7u8; 32 * 32 * 4]; for k in 0..300 { @@ -1950,7 +2195,7 @@ mod tests { let rgba = vec![7u8; 16 * 16 * 4]; assert!(!dc.put_frame(1, 16, 16, &rgba, &ReadFiles::default(), None, 0), "refused"); assert!(!dc.put_frame_f16(2, 16, 16, &[0x3c00u16; 16 * 16 * 4], &ReadFiles::default(), None, 0), "refused"); - dc.put_layer(3, &buf(3, 16, 16), &EntryFiles::default(), 0); + dc.put_layer(3, &Arc::new(buf(3, 16, 16)), &EntryFiles::default(), 0); dc.flush(); assert_eq!(dc.encodes(), 0, "nothing compressed"); assert_eq!(dc.stats().entries, 0); @@ -1960,7 +2205,7 @@ mod tests { assert_eq!(dc.stats().limit_bytes, 32 << 20); assert!(!dc.accepts_writes()); assert!(!dc.put_frame(1, 16, 16, &rgba, &ReadFiles::default(), None, 0), "refused"); - dc.put_layer(3, &buf(3, 16, 16), &EntryFiles::default(), 0); + dc.put_layer(3, &Arc::new(buf(3, 16, 16)), &EntryFiles::default(), 0); assert_eq!(dc.encodes(), 0, "nothing compressed"); // A small limit the setting asks for (not free space) is used. dc.set_disk_space(None); @@ -1971,7 +2216,7 @@ mod tests { dc.set_disk_space(Some((4096 * GB, 500 * GB))); assert!(dc.put_frame(1, 16, 16, &rgba, &ReadFiles::default(), None, 0)); assert!(dc.put_frame_f16(2, 16, 16, &[0x3c00u16; 16 * 16 * 4], &ReadFiles::default(), None, 0)); - dc.put_layer(3, &buf(3, 16, 16), &EntryFiles::default(), 0); + dc.put_layer(3, &Arc::new(buf(3, 16, 16)), &EntryFiles::default(), 0); dc.flush(); assert_eq!(dc.encodes(), 3); assert_eq!(dc.stats().entries, 3); @@ -2076,11 +2321,11 @@ mod tests { assert!(dc.get_frame(k, Some(&two)).is_none() && !dc.contains(Kind::Frame, k), "replaced: a miss, deleted"); // A layer buffer made of it. let lk = layer_disk_key(7); - dc.put_layer(lk, &buf(1, 4, 4), &EntryFiles::of(&files, Some(&one)).unwrap(), 0); + dc.put_layer(lk, &Arc::new(buf(1, 4, 4)), &EntryFiles::of(&files, Some(&one)).unwrap(), 0); dc.flush(); assert!(dc.get_layer(lk, Some(&one)).is_some_and(|(b, f)| b.img == buf(1, 4, 4).img && f == files && f.is_complete())); assert!(dc.get_layer(lk, None).is_none() && !dc.contains(Kind::Layer, lk), "no identity: a miss, deleted"); - dc.put_layer(lk, &buf(1, 4, 4), &EntryFiles::of(&files, Some(&one)).unwrap(), 0); + dc.put_layer(lk, &Arc::new(buf(1, 4, 4)), &EntryFiles::of(&files, Some(&one)).unwrap(), 0); dc.flush(); assert!(dc.get_layer(lk, Some(&two)).is_none() && !dc.contains(Kind::Layer, lk), "replaced: a miss, deleted"); // The browser's sealed layer entries carry the same list. @@ -2324,7 +2569,7 @@ mod tests { assert!(dc.dropped_since(&read(&[&a]), before) && !dc.dropped_since(&read(&[&b]), before)); assert!(!dc.put_frame(1, 1, 1, &[1; 4], &read(&[&a, &b]), None, before), "a frame from before the drop"); assert!(!dc.put_frame_f16(2, 1, 1, &[1; 4], &read(&[&a]), None, before)); - dc.put_layer(3, &buf(3, 2, 2), &EntryFiles::of(&read(&[&a]), None).unwrap(), before); + dc.put_layer(3, &Arc::new(buf(3, 2, 2)), &EntryFiles::of(&read(&[&a]), None).unwrap(), before); assert!(dc.put_frame(4, 1, 1, &[4; 4], &read(&[&b]), None, before), "b.png wasn't dropped"); dc.flush(); assert!(!dc.contains(Kind::Frame, 1) && !dc.contains(Kind::Frame, 2) && !dc.contains(Kind::Layer, 3)); @@ -2334,7 +2579,7 @@ mod tests { let after = dc.drop_generation(); assert!(after > before && !dc.dropped_since(&read(&[&a]), after)); assert!(dc.put_frame(5, 1, 1, &[5; 4], &read(&[&a]), None, after)); - dc.put_layer(6, &buf(6, 2, 2), &EntryFiles::of(&read(&[&a]), None).unwrap(), after); + dc.put_layer(6, &Arc::new(buf(6, 2, 2)), &EntryFiles::of(&read(&[&a]), None).unwrap(), after); dc.flush(); assert_eq!(dc.get_frame(5, None).map(|f| f.0.rgba), Some(vec![5; 4])); assert!(dc.get_layer(6, None).is_some()); diff --git a/crates/render/src/eval.rs b/crates/render/src/eval.rs index 41e6132d3..a9abefb16 100644 --- a/crates/render/src/eval.rs +++ b/crates/render/src/eval.rs @@ -283,7 +283,7 @@ impl<'a> EvalCtx<'a> { } impl crate::PropertyOverride { - fn value(&self, layer: &Layer, prop: &Property, time: Tick) -> Value { + pub(crate) fn value(&self, layer: &Layer, prop: &Property, time: Tick) -> Value { if self.font { // A Font control merges only the font into the target text, as // `essential::with_overrides` does. diff --git a/crates/render/src/footage.rs b/crates/render/src/footage.rs index 0e17f152d..c14961d0f 100644 --- a/crates/render/src/footage.rs +++ b/crates/render/src/footage.rs @@ -37,6 +37,14 @@ pub trait FootageSource: Send + Sync { None => self.frame(item, footage, t), } } + /// [`FootageSource::frame_with`]'s full-size frame as 8-bit levels: each pixel is + /// [`effectcraft_raster::quantize_px_u8`] of the float pixel `frame_with` would give, so + /// [`effectcraft_raster::expand_u8`] makes that frame quantised to 8 bpc, bit for bit, from a + /// quarter of the memory (an 8 bpc render of 8-bit footage). `None` when the source has no + /// cheaper way than the float frame; the renderer then takes that. + fn frame8(&self, _item: ItemId, _footage: &Footage, _t: Tick, _float: FloatCurve) -> Option> { + None + } /// `frames` interleaved stereo sample frames of `item`'s audio from source time `t` at /// `rate` Hz (`None` when unavailable). The first sample is `t.to_units_floor(rate)`; /// a sample boundary uses the earliest representable tick that selects that sample. @@ -76,8 +84,9 @@ pub trait FootageSource: Send + Sync { fn cache_budget(&self) -> Option { None } - /// Vector footage (SVG) rasterised at `scale` × its pixel size, for Continuously Rasterize. - /// `None` when the footage is not vector or cannot be read. + /// Vector footage (SVG, PDF, Illustrator, EPS: [`is_vector_footage`]) rasterised at `scale` × + /// its size as [`vector_fit`] draws it, for Continuously Rasterize. `None` when the footage + /// is not vector or cannot be read. fn vector_frame(&self, _item: ItemId, _footage: &Footage, _scale: f64) -> Option> { None } @@ -214,11 +223,36 @@ impl FootageSource for CancellableFootage<'_> { fn frame_with(&self, item: ItemId, footage: &Footage, t: Tick, size: Option<(u32, u32)>, float: FloatCurve) -> Option> { self.read(|| self.inner.frame_with(item, footage, t, size, float)) } + fn frame8(&self, item: ItemId, footage: &Footage, t: Tick, float: FloatCurve) -> Option> { + self.read(|| self.inner.frame8(item, footage, t, float)) + } } -/// Footage that can be rasterised at any scale (SVG). +/// Footage that can be rasterised at any scale: SVG, PDF, Illustrator and EPS files, and a +/// placed page of an After Effects project (its codec is not stored there). pub fn is_vector_footage(f: &Footage) -> bool { - matches!(f.codec.as_str(), "SVG" | "PDF" | "AI" | "EPS") + matches!(f.codec.as_str(), "SVG" | "PDF" | "AI" | "EPS") || f.placed_page +} + +/// How vector footage `f` is drawn at `scale` × its size: the image size, and the x and y scale +/// from document pixels (`doc` = the document's width and height, `own` = its pixel size, the +/// size rounded up) to image pixels. +/// +/// Footage from an After Effects project ([`Footage::placed_page`]) fills its stored size, which +/// can round or measure the page differently: the layer's anchor and masks are measured against +/// that size, and the renderer resamples a reduced-resolution frame to it, so drawn at its own +/// size the art was scaled by the width ratio alone at full resolution (off against the masks) +/// and stretched to the stored aspect below 3/4 resolution (#3099). Other footage is drawn 1:1 at +/// the document's own size, as it always was. +pub fn vector_fit(f: &Footage, doc: [f64; 2], own: (u32, u32), scale: f64) -> ((u32, u32), [f64; 2]) { + let scale = if scale.is_finite() && scale > 0.0 { scale } else { 1.0 }; + let side = |px: u32| (f64::from(px) * scale).ceil().clamp(1.0, f64::from(u32::MAX)) as u32; + let size = (f.width, f.height); + let fills = f.placed_page && size != own && size.0 > 0 && size.1 > 0 && doc.iter().all(|d| d.is_finite() && *d > 0.0); + if !fills { + return ((side(own.0), side(own.1)), [scale, scale]); + } + ((side(size.0), side(size.1)), [scale * f64::from(size.0) / doc[0], scale * f64::from(size.1) / doc[1]]) } /// No footage available (renders footage layers as transparent). diff --git a/crates/render/src/layers.rs b/crates/render/src/layers.rs index df976a857..cfae5dbab 100644 --- a/crates/render/src/layers.rs +++ b/crates/render/src/layers.rs @@ -295,7 +295,15 @@ impl<'a> Renderer<'a> { /// in this render ([`Self::mask_blur`]'s gate: the render options, the precomp layers above). /// The key has the layer's own motion blur state, but a mask set to On blurs without it. pub(crate) fn mask_gate_key(&self, ctx: &EvalCtx, layer: &Layer, key: Option) -> Option { + // Queue jobs share this cache but may override Proxy Use differently. The item's + // proxy switch alone does not describe the source a renderer actually reads. let k = key?; + let k = match self.opts.proxy { + effectcraft_project::render_queue::ProxyUse::CurrentSettings => k, + effectcraft_project::render_queue::ProxyUse::UseAll => cache::derive(k, 0x7072_6f78_7901), + effectcraft_project::render_queue::ProxyUse::UseCompOnly => cache::derive(k, 0x7072_6f78_7902), + effectcraft_project::render_queue::ProxyUse::UseNone => cache::derive(k, 0x7072_6f78_7903), + }; let masked = layer.masks().is_some_and(|m| !m.children.is_empty()); Some(if masked && !self.mask_blur(ctx, layer).allowed { cache::derive(k, 0x6d62_6f66) } else { k }) } diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index e09794de2..f47443b18 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -26,7 +26,7 @@ pub mod three_d; use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; -pub use auto::{AutoKey, AutoPick}; +pub use auto::{AutoKey, AutoPick, AutoTicket}; pub use cache::{CacheStats, LayerCache, LayerStore, PrefetchStore}; use effectcraft_color::BlendMode; use effectcraft_effects::{Buf, EffectCtx, EffectEnv, EffectHost, LayerPixels, Params}; @@ -46,8 +46,8 @@ mod layers; mod opts; mod pipeline; -pub use accel::{Accelerator, Backend, FxStep, FxTarget}; -pub use footage::{CancellableFootage, FloatCurve, FootageSource, NoFootage, is_vector_footage, missing_item_problem}; +pub use accel::{Accelerator, Backend, FrameMemory, FxStep, FxTarget}; +pub use footage::{CancellableFootage, FloatCurve, FootageSource, NoFootage, is_vector_footage, missing_item_problem, vector_fit}; pub use frames::{ adaptive_samples, blend_frames, cineon_frame, content_bounds, extended_region, field_frame, frame_position, frame_size, interpret_pixels, kurbo_path, render_frame, roi_inside, @@ -61,7 +61,7 @@ pub use layers::{Placement, StyledLayer}; pub(crate) use opts::Collapse3d; pub use opts::{LayerTiming, RenderOpts}; use opts::{MAX_THROUGH, Through}; -pub use pipeline::{Collapsed, MediaOverride, PropertyOverride}; +pub use pipeline::{Collapsed, FootagePicture, MediaOverride, PropertyOverride}; pub struct Renderer<'a> { pub project: &'a Project, @@ -102,6 +102,8 @@ pub struct Renderer<'a> { through: [Option>; MAX_THROUGH], /// More collapsed levels than [`MAX_THROUGH`]: their motion is not blurred. through_lost: bool, + /// Layer-space consumers must not use the viewport raster optimization. + pub(crate) whole_raster: bool, } #[cfg(test)] diff --git a/crates/render/src/passes.rs b/crates/render/src/passes.rs index 37c94fff4..b8f94fc46 100644 --- a/crates/render/src/passes.rs +++ b/crates/render/src/passes.rs @@ -86,10 +86,10 @@ pub async fn comp_frame(r: &Renderer<'_>, comp: ItemId, t: Tick) -> Image { } else if opts.roi.is_none() && let Some(pick) = a.auto_pick() { - let key = AutoKey::new(comp, opts.scale, false); - let gpu = pick.choose(key); - opts.backend = if gpu { Backend::Gpu } else { Backend::Cpu }; - auto = Some((key, gpu)); + // (a frame cancelled or panicking between passes drops it: ended unmeasured) + let ticket = pick.ticket(AutoKey::new(comp, opts.scale, false)); + opts.backend = if ticket.gpu() { Backend::Gpu } else { Backend::Cpu }; + auto = Some(ticket); } } let t0 = web_time::Instant::now(); @@ -98,8 +98,8 @@ pub async fn comp_frame(r: &Renderer<'_>, comp: ItemId, t: Tick) -> Image { } else { in_passes(Some(a), |acc| Renderer { opts, accel: acc, ..*r }.comp_frame(comp, t)).await.value }; - if let (Some((key, chose)), Some(pick)) = (auto, a.auto_pick()) { - pick.record(key, chose, t0.elapsed().as_secs_f64() * 1e3); + if let Some(t) = auto { + t.record(t0.elapsed().as_secs_f64() * 1e3); } img } diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index 6c79ba7bb..c0f4c8dd9 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -5,8 +5,9 @@ use super::fx_host::{FxHost, essential_overrides}; use super::opts::{Collapse3d, LayerTiming, MAX_THROUGH, RenderOpts, Through}; use super::reads::{ReadFiles, ReadLog}; use super::{ - Arc, AutoKey, AutoPick, BlendMode, Buf, Comp, EffectCtx, EffectEnv, EvalCtx, Footage, FootageKind, FrameBlend, FrameRate, GroupKind, Image, ItemId, - ItemKind, Layer, LayerCache, LayerSource, Params, Project, Quality, Region, Renderer, Tick, cache, color, masks, shapes, styles, text, three_d, vec2, + Arc, AutoKey, AutoPick, AutoTicket, BlendMode, Buf, Comp, EffectCtx, EffectEnv, EvalCtx, Footage, FootageKind, FrameBlend, FrameRate, GroupKind, Image, + ItemId, ItemKind, Layer, LayerCache, LayerSource, Params, Project, Quality, Region, Renderer, Tick, cache, color, masks, shapes, styles, text, three_d, + vec2, }; use effectcraft_keyframe::Value; use effectcraft_project::LayerId; @@ -19,6 +20,24 @@ pub(crate) struct EffectStack { pub origin: [f64; 2], } +/// A footage layer's source as the decoded picture itself (see [`Renderer::footage_picture`]): +/// its buffer in layer space is `img` at offset `[0, 0]` and `scale`; in effect space (what +/// [`Renderer::layer_input`] gives) at `effect_offset`. +#[derive(Clone, Debug)] +pub struct FootagePicture { + pub img: Arc, + pub scale: f64, + pub effect_offset: [f64; 2], +} + +/// How a decoded footage frame becomes a layer buffer ([`Renderer::footage_buf`]). +enum FootageLayout { + /// Its own pixels, at this buffer scale. + Native { scale: f64 }, + /// Resampled to `width`×`height` at this buffer scale. + Resample { width: u32, height: u32, scale: f64 }, +} + /// A property value an Essential Properties override gives a layer of a collapsed precomp. #[derive(Clone, Debug)] pub struct PropertyOverride { @@ -37,6 +56,29 @@ pub struct MediaOverride { pub(crate) item: ItemId, } +/// The most pixels a solid layer's buffer holds (as the other layer buffers are held to). +const MAX_SOLID_PIXELS: f64 = 16.0e6; + +/// The scale and whole-pixel size of a `width`×`height` solid rendered at `scale`, lowered until +/// the rounded size (each side at least one pixel) is within [`MAX_SOLID_PIXELS`]. +pub(crate) fn solid_size(width: u32, height: u32, mut scale: f64) -> (f64, u32, u32) { + let size = |s: f64| (((width as f64 * s).ceil() as u32).max(1), ((height as f64 * s).ceil() as u32).max(1)); + // Rounding up leaves an elongated solid over the cap after one step; each step shrinks the + // overshoot geometrically, so a few are enough. + for i in 0..64 { + let (w, h) = size(scale); + let area = w as f64 * h as f64; + if area <= MAX_SOLID_PIXELS { + break; + } + // The first step is the exact area ratio; later ones undershoot a little so rounding up + // cannot leave the size stuck just over the cap. + scale *= (MAX_SOLID_PIXELS / area).sqrt() * if i == 0 { 1.0 } else { 0.99 }; + } + let (w, h) = size(scale); + (scale, w, h) +} + impl MediaOverride { /// A Media Replacement of `layer`'s footage by `item`. pub fn new(layer: LayerId, item: ItemId) -> Self { @@ -123,6 +165,7 @@ impl<'a> Renderer<'a> { top: true, through: [None; MAX_THROUGH], through_lost: false, + whole_raster: false, } } @@ -258,6 +301,8 @@ impl<'a> Renderer<'a> { top: false, through: [None; MAX_THROUGH], through_lost: false, + // Effects and nested renders need whole layer-space pixels. + whole_raster: true, ..*self } } @@ -398,8 +443,9 @@ impl<'a> Renderer<'a> { } let (scale, draft, blur) = (self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer)); let prev = ctx.time - ctx.comp.frame_rate.frame_duration(); - let now = cache::layer_key(ctx, layer, scale, draft, blur); - now.is_some() && now == cache::layer_key(&ctx.at(prev), layer, scale, draft, blur) + let before = ctx.at(prev); + let now = self.clipped_key(ctx, layer, cache::layer_key(ctx, layer, scale, draft, blur)); + now.is_some() && now == self.clipped_key(&before, layer, cache::layer_key(&before, layer, scale, draft, blur)) } /// Rasterisation scale of a layer's source: the output scale, or — for text and shape layers, @@ -433,14 +479,73 @@ impl<'a> Renderer<'a> { // a whole number maps the unit vectors to lengths a hair over 1, which must stay 1 (#390: // rounded up to 2^¼, unscaled text was drawn larger and resampled, softer). let mut k = 2f64.powf((k.log2() * 4.0 - 1e-6).ceil() / 4.0).clamp(1.0 / 16.0, 64.0); - // Keep the buffer within about 16 million pixels. + // Keep the buffer within about 16 million pixels (of the part the output shows, when + // only that is rasterised: see `raster_clip`). if let Some(b) = content_bounds(ctx, layer) { + let b = match self.raster_clip(ctx, layer) { + Some(c) => { + let r = kurbo::Rect::new(b[0], b[1], b[2], b[3]).intersect(c); + [r.x0, r.y0, r.x1, r.y1] + } + None => b, + }; let area = ((b[2] - b[0]) * (b[3] - b[1])).max(1.0) * s * s; k = k.min((16.0e6 / area).sqrt().max(1.0)); } s * k } + /// The layer-space part of a 3D text or shape layer the output shows, when only that part + /// needs rasterising (#1388: text close to the camera was rasterised whole at up to 16 + /// million pixels, a quarter gigabyte per frame worker, though the frame showed a fraction + /// of it; behind the camera it was rasterised and not drawn). `None` rasterises it whole: + /// whenever pixels outside the frame can matter — effects, layer styles, masks, motion blur, + /// depth of field, lights (shadows and transmission), Advanced 3D, per-character 3D, a + /// collapsed 2D transform — or the visible part can't be bounded (see + /// [`three_d::compose::visible_clip`]). + pub(crate) fn raster_clip(&self, ctx: &EvalCtx, layer: &Layer) -> Option { + if self.whole_raster || !layer.is_3d() || !matches!(layer.source, LayerSource::Text | LayerSource::Shape) || self.outer.is_some() { + return None; + } + if (layer.switches.effects && layer.effects().is_some_and(|fx| !fx.children.is_empty())) + || layer.masks().is_some_and(|m| !m.children.is_empty()) + || styles::active(ctx, layer) + || ctx.comp.layers.iter().any(|l| matches!(l.source, LayerSource::Light { .. })) + || self.is_adv3d(ctx) + || (matches!(layer.source, LayerSource::Text) && (text::per_char_3d(ctx, layer) || text::animator_blur(ctx, layer))) + { + return None; + } + let v = three_d::compose::visible_clip(self, ctx, layer)?; + if v.area() <= 0.0 { + // Behind the camera. + return Some(v); + } + let b = content_bounds(ctx, layer).map(|b| kurbo::Rect::new(b[0], b[1], b[2], b[3]))?; + if v.x0 <= b.x0 && v.y0 <= b.y0 && v.x1 >= b.x1 && v.y1 >= b.y1 { + return None; + } + // Whole cells of a layer-space grid whose cell is about a quarter of the part that shows, + // rounded down to a power of two: a moving camera keeps one raster (and its cache entry) + // while that part stays within the same cells, and a dolly changes the grid only at each + // doubling. The cells cover at most about 2.25 times the part that shows. Worth it only + // when it leaves out at least half of the content. + let c = 2f64.powf(((v.width().max(v.height()) / 4.0).max(1e-6)).log2().floor()); + let snap = |v: f64, up: bool| if up { (v / c).ceil() * c } else { (v / c).floor() * c }; + // (Not cut to the content bounds: a stroke or a glyph's padding reaches past them.) + let cells = kurbo::Rect::new(snap(v.x0, false), snap(v.y0, false), snap(v.x1, true), snap(v.y1, true)); + (cells.is_finite() && cells.intersect(b).area() <= b.area() * 0.5).then_some(cells) + } + + /// A layer content cache key that also names the part rasterised ([`Self::raster_clip`]). + pub(crate) fn clipped_key(&self, ctx: &EvalCtx, layer: &Layer, key: Option) -> Option { + let k = key?; + Some(match self.raster_clip(ctx, layer) { + Some(c) => raster_clip_key(k, c), + None => k, + }) + } + /// Draw a Wireframe-quality layer: its bounds as a one-pixel outline. pub(crate) fn draw_wireframe(&self, ctx: &EvalCtx, layer: &Layer, canvas: &mut Image) { for (x, y) in self.wireframe_pixels(ctx, layer, (canvas.width, canvas.height)) { @@ -482,32 +587,49 @@ impl<'a> Renderer<'a> { /// Render a composition at comp time `t` (transparent background, comp size × scale). /// Top-level frames go to the GPU when [`Self::active_accel`] is set and handles them. pub fn comp_frame(&self, comp_id: ItemId, t: Tick) -> Image { + self.comp_frame_with(comp_id, t, false, |a| Ok::<_, std::convert::Infallible>(a.comp_frame(self, comp_id, t))).unwrap_or_else(|never| match never {}) + } + + /// Render a frame, validating the accelerated attempt before starting any CPU fallback. + /// The caller's scope must run only the accelerator, not this whole method: a GPU fault + /// must release failed resources before a potentially large CPU render (#3357). + /// A scope error returns immediately; the caller can then retry with a fresh CPU renderer + /// without accelerator-produced cache entries. Healthy declines and Auto timing retain + /// the same behavior as [`Self::comp_frame`]. Its fallback disables acceleration for + /// effects too: GPU work must never escape the supplied error scope. + pub fn comp_frame_scoped(&self, comp_id: ItemId, t: Tick, scope: impl FnOnce(&dyn Accelerator) -> Result, E>) -> Result { + self.comp_frame_with(comp_id, t, true, scope) + } + + fn comp_frame_with(&self, comp_id: ItemId, t: Tick, scoped: bool, scope: impl FnOnce(&dyn Accelerator) -> Result, E>) -> Result { + let cpu = if scoped { self.with_accel(None) } else { self.with_accel(self.accel) }; // Classic 3D runs composite on the GPU too (M12.7), so Auto sends 3D comps there as well; // Auto renders each comp on whichever compositor measured faster ([`AutoPick`]). if self.depth == 0 && let Some(a) = self.active_accel() { - let auto = self.frame_auto(a, comp_id, false); - if auto.is_none_or(|(p, key)| p.choose(key)) { + // (an error returns with the ticket, which ends the frame unmeasured) + let mut ticket = self.frame_auto(a, comp_id, false).map(|(p, key)| p.ticket(key)); + if ticket.as_ref().is_none_or(AutoTicket::gpu) { let t0 = web_time::Instant::now(); - if let Some(img) = a.comp_frame(self, comp_id, t) { - if let Some((p, key)) = auto { - p.record(key, true, t0.elapsed().as_secs_f64() * 1e3); + if let Some(img) = scope(a)? { + if let Some(t) = ticket { + t.record(t0.elapsed().as_secs_f64() * 1e3); } - return img; + return Ok(img); } - if let Some((p, key)) = auto { - p.declined(key); + if let Some(t) = &mut ticket { + t.declined(); } } let t0 = web_time::Instant::now(); - let img = self.comp_frame_cpu(comp_id, t); - if let Some((p, key)) = auto { - p.record(key, false, t0.elapsed().as_secs_f64() * 1e3); + let img = cpu.comp_frame_cpu(comp_id, t); + if let Some(t) = ticket { + t.record(t0.elapsed().as_secs_f64() * 1e3); } - return img; + return Ok(img); } - self.comp_frame_cpu(comp_id, t) + Ok(cpu.comp_frame_cpu(comp_id, t)) } /// The Auto timing history and key for a top-level frame of `comp` (`None` unless the @@ -658,6 +780,15 @@ impl<'a> Renderer<'a> { return; } let Some(fx) = layer.effects() else { return }; + // The LUT files the stack reads go to this render's files: a frame on disk made with one + // that changed since is not used (#3493). + if let Some(log) = self.reads { + for g in fx.groups().take(limit).filter(|g| g.enabled) { + for f in super::reads::lut_files(ctx, layer, g) { + log.note(f); + } + } + } let lt = layer.layer_time(ctx.time); let layer_size = ctx.effect_bounds(layer).0; // #659 / #1205: an authored layer's buffer spans only its painted content. Effects that @@ -781,12 +912,19 @@ impl<'a> Renderer<'a> { /// Source pixels of a layer in layer space (before masks/effects). pub(crate) fn source(&self, ctx: &EvalCtx, layer: &Layer) -> Option { + self.source_with(ctx, layer, false) + } + + /// [`Renderer::source`]; with `quantized`, footage that [`Renderer::prequantized_footage`] + /// counts comes quantised to the project depth. + fn source_with(&self, ctx: &EvalCtx, layer: &Layer, quantized: bool) -> Option { let s = self.opts.scale; match &layer.source { LayerSource::Solid { item } => { let ItemKind::Solid(sol) = &self.project.item(*item)?.kind else { return None }; - let w = ((sol.width as f64 * s).ceil() as u32).max(1); - let h = ((sol.height as f64 * s).ceil() as u32).max(1); + // One colour: a solid larger than MAX_SOLID_PIXELS is made at a lower scale (the + // buffer carries it) instead of allocating gigabytes for a uniform plane. + let (s, w, h) = solid_size(sol.width, sol.height, s); if layer.switches.adjustment { return Some(Buf { img: Image::filled(w, h, [1.0; 4]), offset: [0.0; 2], scale: s }); } @@ -813,7 +951,7 @@ impl<'a> Renderer<'a> { && let Some(nc) = self.project.comp(*item) { let img = self.footage_frame(ctx, layer, *item, pf, lt, None)?; - return Some(self.footage_buf(&img, pf, nc.width, nc.height)); + return Some(self.footage_buf(&img, pf, nc.width, nc.height, false)); } // Essential Properties overrides render the nested comp with this instance's // values. @@ -861,13 +999,12 @@ impl<'a> Renderer<'a> { return Some(buf); } } - let lt = ctx.source_time(layer); - // The proxy is decoded instead, at its own size, and fills the footage's frame. - let proxy = self.proxy_for(item); - let pf = proxy.unwrap_or(f); - let size = self.reduced_size(f.width, f.height).filter(|_| proxy.is_none()); - let img = self.footage_frame(ctx, layer, item, pf, lt, size)?; - Some(self.footage_buf(&img, pf, f.width, f.height)) + // 8 bpc plain footage: made from the footage's 8-bit levels, no float frame. + if quantized && let Some(buf) = self.footage_buf8(ctx, layer, item, self.proxy_for(item).unwrap_or(f), f, ctx.source_time(layer)) { + return Some(buf); + } + let (img, pf) = self.footage_picture_of(ctx, layer, item, f)?; + Some(self.footage_buf(&img, pf, f.width, f.height, quantized && Self::prequantized_footage(layer))) } // (the font files go to this render's log, which `ctx` may not carry: see // [`Renderer::with_reads`]) @@ -881,9 +1018,12 @@ impl<'a> Renderer<'a> { { log.mark_unkeyed(); } - Some(self.authored(text::render(&self.with_reads(ctx), layer, self.raster_scale(ctx, layer)))) + Some(self.authored(text::render_in(&self.with_reads(ctx), layer, self.raster_scale(ctx, layer), self.raster_clip(ctx, layer)))) } - LayerSource::Shape => layer.props.sub("contents").map(|c| self.authored(shapes::render(ctx, layer, c, self.raster_scale(ctx, layer)))), + LayerSource::Shape => layer + .props + .sub("contents") + .map(|c| self.authored(shapes::render_in(ctx, layer, c, self.raster_scale(ctx, layer), self.raster_clip(ctx, layer)))), _ => None, } } @@ -897,20 +1037,147 @@ impl<'a> Renderer<'a> { /// A decoded frame of `f` as a layer buffer covering `w`×`h` layer pixels (the footage's /// nominal size; proxies are scaled up to it), converted from its colour profile. - fn footage_buf(&self, img: &Image, f: &Footage, w: u32, h: u32) -> Buf { + /// + /// `quantized`: also clamp and quantise it to the project depth, as [`Renderer::masked_source`] + /// would (fused with the copy of the cached frame: one pass over it instead of three). + fn footage_buf(&self, img: &Image, f: &Footage, w: u32, h: u32, quantized: bool) -> Buf { + let c = self.pipe.media_in(f); + match self.footage_layout(img, w, h) { + FootageLayout::Native { scale } => { + let img = match (quantized, &c) { + (true, _) => self.pipe.copy_converted(img, c.as_ref()), + (false, Some(c)) => { + let mut img = img.clone(); + color::convert(&mut img, c); + img + } + (false, None) => img.clone(), + }; + Buf { img, offset: [0.0; 2], scale } + } + FootageLayout::Resample { width, height, scale } => { + let mut img = effectcraft_raster::resample(img, width, height); + if let Some(c) = &c { + color::convert(&mut img, c); + } + if quantized { + self.pipe.quantize(&mut img); + } + Buf { img, offset: [0.0; 2], scale } + } + } + } + + /// [`Renderer::footage_buf`]'s quantised buffer made from the footage's 8-bit levels + /// ([`FootageSource::frame8`]), the same pixels to the bit, when nothing before the quantise + /// changes them: an 8 bpc render at Full size of plain footage (no masks, colour profile, + /// Separate Fields, Frame Blending, Cineon, Invert Alpha or linear light interpretation). + /// `None` otherwise, and when the source has no 8-bit frame. + // ponytail: the media pool keeps the 8-bit and the float frame as separate entries, so a + // movie shown by both a plain and a masked layer in one 8 bpc comp is decoded twice per + // frame; derive one from the other in the pool if a comp like that turns out common. + #[allow(clippy::too_many_arguments)] + fn footage_buf8(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, pf: &Footage, f: &Footage, t: Tick) -> Option { + let plain = self.pipe.levels == Some(255.0) + && Self::prequantized_footage(layer) + && self.reduced_size(f.width, f.height).is_none() + && self.pipe.media_in(pf).is_none() + && pf.fields == effectcraft_project::FieldOrder::Off + && !(pf.invert_alpha || pf.linear_light_applies() || pf.cineon_applies()) + && frame_blend_mode(ctx, layer) == FrameBlend::Off; + if !plain { + return None; + } + let fr = self.read(|| self.footage.frame8(item, pf, t, self.pipe.float_curve(pf)))?; + let n = (fr.width as usize).checked_mul(fr.height as usize)?; + let px = fr.rgba.as_chunks::<4>().0; + if px.len() != n { + return None; + } + use rayon::prelude::*; + let data = px.par_iter().map(|&p| effectcraft_raster::expand_u8(p)).collect(); + let k = if fr.width > 0 { f.width.max(1) as f64 / fr.width as f64 } else { 1.0 }; + Some(Buf { img: Image { width: fr.width, height: fr.height, data }, offset: [0.0; 2], scale: 1.0 / k }) + } + + /// The layer's source is footage that [`Renderer::source`] hands over already quantised + /// ([`Renderer::footage_buf`]): footage without masks (masks apply to unquantised pixels). + /// Collapsed layers may be vector footage, drawn elsewhere: not counted. + fn prequantized_footage(layer: &Layer) -> bool { + matches!(layer.source, LayerSource::Footage { .. }) && !layer.switches.collapse && layer.masks().is_none_or(|m| m.groups().next().is_none()) + } + + /// How [`Renderer::footage_buf`] lays out a decoded frame `img` of footage `w`×`h` layer + /// pixels: its own pixels, or resampled. + fn footage_layout(&self, img: &Image, w: u32, h: u32) -> FootageLayout { let s = self.opts.scale; let k = if img.width > 0 { w.max(1) as f64 / img.width as f64 } else { 1.0 }; // Keep native pixels when downsampling is small; resample otherwise. - let mut buf = match self.reduced_size(w, h) { + match self.reduced_size(w, h) { // The source resampled it already (`FootageSource::frame_at_size`). - Some(size) if (img.width, img.height) == size => Buf { img: img.clone(), offset: [0.0; 2], scale: s }, - Some((rw, rh)) if k <= 1.0 + 1e-9 => Buf { img: effectcraft_raster::resample(img, rw, rh), offset: [0.0; 2], scale: s }, - _ => Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 / k }, - }; - if let Some(c) = self.pipe.media_in(f) { - color::convert(&mut buf.img, &c); + Some(size) if (img.width, img.height) == size => FootageLayout::Native { scale: s }, + Some((width, height)) if k <= 1.0 + 1e-9 => FootageLayout::Resample { width, height, scale: s }, + _ => FootageLayout::Native { scale: 1.0 / k }, } - buf + } + + /// A footage layer's decoded frame at the context time (the proxy's when one is used) and + /// the footage it was decoded from: what [`Renderer::source`] makes the layer's buffer of. + fn footage_picture_of(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, f: &'a Footage) -> Option<(Arc, &'a Footage)> { + let lt = ctx.source_time(layer); + // The proxy is decoded instead, at its own size, and fills the footage's frame. + let proxy = self.proxy_for(item); + let pf = proxy.unwrap_or(f); + let size = self.reduced_size(f.width, f.height).filter(|_| proxy.is_none()); + let img = self.footage_frame(ctx, layer, item, pf, lt, size)?; + Some((img, pf)) + } + + /// The decoded footage picture a layer's masked source is, bit for bit, with no copy: a + /// footage layer (not continuously rasterised vector footage) without masks whose frame + /// needs no colour conversion, no resampling and no quantisation (32 bpc). The buffer + /// [`Renderer::layer_input`] gives for no effects is this picture at `effect_offset` and + /// `scale`. + /// Layers showing the same frame get the same `Arc` (the footage source's cached frame), so + /// an accelerator uploads it once and shares the texture. `None`: the layer's buffer is + /// made the usual way (any of the above applies, or the frame is missing), and also when the + /// frame is made per call rather than served by the footage source (frame blending, + /// separate fields, Invert Alpha, Interpret As Linear Light, Cineon conversion: a new + /// picture each time, nothing to share) or when the layer cache already holds the layer's + /// buffer (served from memory as before, without asking the footage source). + pub fn footage_picture(&self, ctx: &EvalCtx, layer: &Layer) -> Option { + let LayerSource::Footage { item } = &layer.source else { return None }; + if self.pipe.levels.is_some() || layer.masks().is_some_and(|m| !m.children.is_empty()) || frame_blend_mode(ctx, layer) != FrameBlend::Off { + return None; + } + let item = ctx.media_item(layer, *item); + let ItemKind::Footage(f) = &self.project.item(item)?.kind else { return None }; + if !f.has_video || (layer.switches.collapse && is_vector_footage(f)) { + return None; + } + let pf = self.proxy_for(item).unwrap_or(f); + if self.pipe.media_in(pf).is_some() + || pf.fields != effectcraft_project::FieldOrder::Off + || pf.invert_alpha + || pf.linear_light_applies() + || pf.cineon_applies() + { + return None; + } + if let Some(cache) = self.cache { + let (scale, draft, blur) = (self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer)); + let content = self.content_key(ctx, layer, cache::layer_key(ctx, layer, scale, draft, blur)); + let input = self.content_key(ctx, layer, cache::input_key(ctx, layer, scale, draft, blur, 0)); + if [content, input].into_iter().flatten().any(|k| cache.holds(k)) { + return None; + } + } + let (img, _) = self.footage_picture_of(ctx, layer, item, f)?; + let FootageLayout::Native { scale } = self.footage_layout(&img, f.width, f.height) else { return None }; + // `layer_input`'s rebase into effect space (`Buf::rebase` of a buffer at the origin). + let mut at = Buf { img: Image { width: 0, height: 0, data: Vec::new() }, offset: [0.0; 2], scale }; + at.rebase(ctx.effect_bounds(layer).1); + Some(FootagePicture { img, scale, effect_offset: at.offset }) } /// The size of `w` × `h` layer pixels in this render when it is reduced enough that footage @@ -996,13 +1263,14 @@ impl<'a> Renderer<'a> { /// Source → masks, clamped/quantised to the project depth. fn masked_source(&self, ctx: &EvalCtx, layer: &Layer) -> Option { - let mut buf = match self.content_times(ctx, layer) { - Some(times) => self.blurred_source(ctx, layer, ×)?, - None => self.source(ctx, layer)?, + let (mut buf, quantized) = match self.content_times(ctx, layer) { + Some(times) => (self.blurred_source(ctx, layer, ×)?, false), + None => (self.source_with(ctx, layer, true)?, Self::prequantized_footage(layer)), }; - // A solid is filled with a quantised colour: only masks can take it off the grid. + // A solid is filled with a quantised colour, and plain footage comes quantised: only + // masks can take them off the grid. let solid = matches!(layer.source, LayerSource::Solid { .. }); - if masks::apply(ctx, layer, &mut buf, self.mask_blur(ctx, layer)) || !solid { + if masks::apply(ctx, layer, &mut buf, self.mask_blur(ctx, layer)) || !(solid || quantized) { self.pipe.quantize(&mut buf.img); } Some(buf) @@ -1052,7 +1320,13 @@ impl<'a> Renderer<'a> { /// (what the motion tracker analyses, like After Effects' Layer panel). `None` for layers /// without pixels. pub fn layer_source(&self, ctx: &EvalCtx, layer: &Layer) -> Option { - self.source(ctx, layer) + Renderer { whole_raster: true, ..*self }.source(ctx, layer) + } + + /// Layer-space pixels after masks and effects, including content outside the viewport. + /// Expression sampleImage reads this rather than the frame-oriented clipped buffer. + pub fn layer_content(&self, ctx: &EvalCtx, layer: &Layer) -> Option> { + Renderer { whole_raster: true, ..*self }.content_buf(ctx, layer) } /// A 3D model or primitive layer rendered on its own, through the comp's camera: what an @@ -1092,7 +1366,7 @@ impl<'a> Renderer<'a> { mut timing: Option<&mut LayerTiming>, ) -> Option<(Arc, Option, Arc)> { let key = self.cache.and_then(|_| cache::layer_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer))); - let key = self.content_key(ctx, layer, key); + let key = self.content_key(ctx, layer, self.clipped_key(ctx, layer, key)); if let Some((b, files)) = self.cached(key) { if let Some(t) = timing.as_deref_mut() { t.cached = true; @@ -1114,8 +1388,13 @@ impl<'a> Renderer<'a> { /// what effect `effects` sees. Served from / stored in the layer cache under its own key, /// so scrubbing reuses frames rendered for earlier output frames. pub fn layer_input(&self, ctx: &EvalCtx, layer: &Layer, effects: usize) -> Option> { + // The Layer panel, analysis tools and effect inputs read the whole layer, not the viewport. + Renderer { whole_raster: true, ..*self }.whole_layer_input(ctx, layer, effects) + } + + fn whole_layer_input(&self, ctx: &EvalCtx, layer: &Layer, effects: usize) -> Option> { let key = self.cache.and_then(|_| cache::input_key(ctx, layer, self.raster_scale(ctx, layer), self.opts.draft, self.mb_on(ctx, layer), effects)); - let key = self.content_key(ctx, layer, key); + let key = self.content_key(ctx, layer, self.clipped_key(ctx, layer, key)); if let Some((b, _)) = self.cached(key) { return Some(b); } @@ -1132,3 +1411,9 @@ impl<'a> Renderer<'a> { Some(self.keep(key, Arc::new(buf?), &files).0) } } + +/// Snap cells can be finer than 1/16 unit: preserve every coordinate bit so visible +/// clips cannot collide with another cell or the empty behind-camera raster. +pub(crate) fn raster_clip_key(key: u64, clip: kurbo::Rect) -> u64 { + [clip.x0, clip.y0, clip.x1, clip.y1].iter().fold(cache::derive(key, 0xc11b), |key, coordinate| cache::derive(key, coordinate.to_bits())) +} diff --git a/crates/render/src/reads.rs b/crates/render/src/reads.rs index ed97e50fd..af0535e5a 100644 --- a/crates/render/src/reads.rs +++ b/crates/render/src/reads.rs @@ -35,9 +35,10 @@ impl FileStamp { } /// The identity of the file at `path` now: one `metadata` call. `None` when the file system - /// can't describe it (missing, or a path served from memory). + /// can't describe it (missing, not a regular file, or a path served from memory). pub fn native(path: &str) -> Option { - std::fs::metadata(path).ok().map(|md| FileStamp::of_metadata(&md)) + let md = std::fs::metadata(path).ok()?; + md.is_file().then(|| FileStamp::of_metadata(&md)) } } @@ -65,6 +66,34 @@ impl ReadFile { } } +/// The files effect `g` of `layer` reads at the context time (Apply Color LUT, Lumetri's Input +/// LUT and Look, OCIO File Transform, a custom OCIO config and its LUTs: see +/// [`effectcraft_effects::lut_params`]), each with its identity now. A layer's cache key holds them and a render +/// notes them, so what was made with a file that changed since is not used (#3493). +pub(crate) fn lut_files(ctx: &crate::EvalCtx, layer: &effectcraft_project::Layer, g: &effectcraft_project::PropGroup) -> Vec { + let effectcraft_project::GroupKind::Effect { effect } = &g.kind else { return Vec::new() }; + effectcraft_effects::lut_params(effect) + .iter() + .filter_map(|param| match ctx.value(layer, g.prop(param)?) { + effectcraft_keyframe::Value::Str(src) => Some(source_files(&src)), + _ => None, + }) + .flatten() + .collect() +} + +/// The files a file parameter set to `src` reads (an OCIO config's also the LUTs it names, #3538), +/// each with its identity now. +pub(crate) fn source_files(src: &str) -> Vec { + effectcraft_effects::lut_files(src) + .into_iter() + .map(|path| { + let stamp = FileStamp::native(&path).unwrap_or(FileStamp::Missing); + ReadFile::new(path, stamp) + }) + .collect() +} + /// At most this many files are listed per render or cached buffer; more mark the list /// incomplete (a comp reading more files than this for one frame is not checked file by file). pub const MAX_READ_FILES: usize = 4096; @@ -327,6 +356,15 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + #[test] + fn a_directory_is_not_a_native_file_stamp() { + let dir = std::env::temp_dir().join(format!("effectcraft-stamp-directory-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + assert_eq!(FileStamp::native(&dir.to_string_lossy()), None); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn the_list_is_bounded() { let files: ReadFiles = (0..MAX_READ_FILES + 3).map(|i| file(&format!("/f{i:06}"), 1)).collect(); diff --git a/crates/render/src/shapes.rs b/crates/render/src/shapes.rs index 2497ea807..df92846ce 100644 --- a/crates/render/src/shapes.rs +++ b/crates/render/src/shapes.rs @@ -686,6 +686,12 @@ const BATCH_PIXELS: usize = 8 << 20; /// Each draw rasterises and composites only its own pixel rectangle; coverage masks of /// consecutive draws are rasterised in parallel, then composited in stacking order. pub fn render(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, s: f64) -> Buf { + render_in(ctx, layer, contents, s, None) +} + +/// [`render`], only the part of the layer inside `clip` (layer space; the part the output shows, +/// see `Renderer::raster_clip`). +pub fn render_in(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, s: f64, clip: Option) -> Buf { let mut arena = Vec::new(); let (draws, _) = collect(ctx, layer, contents, &mut arena); // Bottom-most draw first. @@ -702,9 +708,7 @@ pub fn render(ctx: &EvalCtx, layer: &Layer, contents: &PropGroup, s: f64) -> Buf let Some(b) = resolved.iter().map(|r| r.2).reduce(|a, b| a.union(b)) else { return Buf { img: Image::new(1, 1), offset: [0.0, 0.0], scale: s } }; // Keep buffers sane for runaway geometry. let b = b.intersect(kurbo::Rect::new(-20000.0, -20000.0, 20000.0, 20000.0)); - let w = ((b.width() * s).ceil() as u32 + 4).clamp(1, 16384); - let h = ((b.height() * s).ceil() as u32 + 4).clamp(1, 16384); - let offset = [-b.x0 * s + 2.0, -b.y0 * s + 2.0]; + let Some((w, h, offset)) = crate::text::raster_window(b, s, clip) else { return Buf { img: Image::new(1, 1), offset: [0.0, 0.0], scale: s } }; let m = Mat3::translate(vec2(offset[0], offset[1])) * Mat3::scale(vec2(s, s)); let px_rect = |r: kurbo::Rect| -> Option { let x0 = (r.x0 * s + offset[0]).floor().max(0.0); diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index 50ed65c73..2d0f90ac5 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -11,6 +11,7 @@ mod auto; mod aux_channels; mod collapse; mod color; +mod drop_shadow_cover; mod footage_size; mod frame_blend; mod guides; @@ -20,6 +21,8 @@ mod mask_feather; mod motion_tile; mod nested; mod paint; +mod proxy_cache; +mod raster_clip; mod remap; mod roi; mod stretch_limits; @@ -329,7 +332,7 @@ fn card_dance_comp_camera_matches_the_default_view() { assert!(img.get(10, 50)[3] < 0.01); } -/// A black 2D solid with CC Particle World, under a camera layer panned right by `pan` px. +/// A black 2D solid with Particle Emitter 3D, under a camera layer panned right by `pan` px. fn particle_world_scene(pan: f64) -> (Project, ItemId) { let (mut p, cid, comp) = setup(); let mut l = solid(&mut p, &comp, [0.0, 0.0, 0.0], 200, 100); @@ -359,7 +362,7 @@ fn particles_x(img: &crate::Image) -> f32 { sx / m } -/// #397: CC Particle World on a 2D layer is seen through the comp's camera layer, and the layer +/// #397: Particle Emitter 3D on a 2D layer is seen through the comp's camera layer, and the layer /// cache notices the camera moving. #[test] fn particle_world_follows_the_comp_camera() { @@ -374,7 +377,7 @@ fn particle_world_follows_the_comp_camera() { assert!(b < a - 20.0, "panning the camera right moves the particles left: {a} -> {b}"); } -/// A black solid with CC Particle World whose Birth Rate is 3 until layer time `cut`, then 0 a +/// A black solid with Particle Emitter 3D whose Birth Rate is 3 until layer time `cut`, then 0 a /// frame later (`None`: 3 throughout); the layer starts at comp time `start`, stretched `stretch` %. fn burst_scene(cut: Option, start: f64, stretch: f64) -> (Project, ItemId) { burst_scene_keys(cut, start, stretch, false) @@ -624,7 +627,7 @@ fn a_text_expression_selector_is_not_served_from_the_disk_cache() { /// A Birth Rate expression reading another layer: changing that layer changes the history but /// not the current rate (0) nor this layer's properties, which is all the layer cache key sees. -/// CC Particle World layers with expressions therefore aren't layer cached. +/// Particle Emitter 3D layers with expressions therefore aren't layer cached. #[test] fn particle_world_with_a_birth_rate_expression_is_not_served_from_the_layer_cache() { let scene = |opacity: f64| { @@ -657,8 +660,8 @@ fn particle_world_with_a_birth_rate_expression_is_not_served_from_the_layer_cach assert!(light(&b) > 1.0 && light(&b) < light(&a), "fewer births: {} vs {}", light(&b), light(&a)); } -/// A grey solid (CC Mr. Mercury shades its blobs from it) with effect `id` (CC Particle Systems -/// II, CC Mr. Mercury): Birth Rate `rate`, +/// A grey solid (Liquid Particles shades its blobs from it) with effect `id` (CC Particle Systems +/// II, Liquid Particles): Birth Rate `rate`, /// or keyed 3 until 0.5 s and 0 a frame later (`None`); Longevity 1 s. fn system_scene(id: &str, rate: Option) -> (Project, ItemId) { let (mut p, cid, comp) = setup(); @@ -677,7 +680,7 @@ fn system_scene(id: &str, rate: Option) -> (Project, ItemId) { (p, cid) } -/// #1373 for CC Particle Systems II and CC Mr. Mercury through the renderer: a keyed burst +/// #1373 for Particle Emitter 2D and Liquid Particles through the renderer: a keyed burst /// draws as the constant rate during it, still draws after the rate drops to 0 (unlike a rate /// of 0 throughout), and draws as nothing born once Longevity has run out. #[test] @@ -1057,6 +1060,27 @@ fn comp_frame_ends_the_frame_when_the_render_panics() { assert_eq!(a.ends.load(Ordering::SeqCst), 1); } +/// The renderer labels Project Settings ▸ Video Rendering and Effects draws parse back as the +/// backend they name (in any case), and the older spellings still work. +#[test] +fn renderer_labels_parse_back_and_keep_the_older_spellings() { + use crate::Backend; + for (label, backend) in [ + ("GPU Acceleration", Backend::Gpu), + ("gpu acceleration", Backend::Gpu), + ("Mercury GPU Acceleration", Backend::Gpu), + ("gpu", Backend::Gpu), + ("Software Rendering", Backend::Cpu), + ("software rendering", Backend::Cpu), + ("Mercury Software Only", Backend::Cpu), + ("cpu", Backend::Cpu), + ("Auto", Backend::Auto), + ] { + assert_eq!(Backend::parse(label), Some(backend), "{label}"); + } + assert_eq!(Backend::parse("Vulkan"), None); +} + #[test] fn auto_backend_sends_2d_and_3d_comps_to_the_gpu() { let (mut p, cid, comp) = setup(); @@ -1223,11 +1247,11 @@ fn effects_on_text_reach_past_the_text_bounds() { } assert!( painted > 50 && hi.saturating_sub(lo) > 100, - "CC Tiler tiles the glyphs across the layer region, not just its text bounds: {painted} painted, x-span {lo}..{hi}" + "Scaled Tiles tiles the glyphs across the layer region, not just its text bounds: {painted} painted, x-span {lo}..{hi}" ); } -/// #658: CC Smear drags text glyphs beyond the text layer's own bounds. Pixels smeared past +/// #658: Directional Smear drags text glyphs beyond the text layer's own bounds. Pixels smeared past /// the painted rectangle used to fall off its edge, so the liquid trail never appeared. #[test] fn cc_smear_trails_text_past_its_bounds() { @@ -1246,7 +1270,7 @@ fn cc_smear_trails_text_past_its_bounds() { ); p.comp_mut(cid).unwrap().layers = vec![l].into(); let smeared = right(&render_frame(&p, cid, Tick::ZERO, 1.0)); - assert!(plain > 40 && smeared > plain + 25, "CC Smear trails the glyphs past the text bounds: right edge {plain} → {smeared}"); + assert!(plain > 40 && smeared > plain + 25, "Directional Smear trails the glyphs past the text bounds: right edge {plain} → {smeared}"); } /// A layer parameter pointing at a shape layer sees all of it (in its effect space), and the @@ -1502,7 +1526,7 @@ fn solo_excludes_other_layers_outside_the_soloed_layers_range() { assert_eq!(second.get(100, 50), [0.0, 0.0, 1.0, 1.0], "only B inside its range"); } -/// #1252: a layer blended with a CC Particle World layer, under a camera panned by `pan` px. +/// #1252: a layer blended with a Particle Emitter 3D layer, under a camera panned by `pan` px. fn scene_reader_dependency(pan: f64) -> (Project, ItemId) { let (mut p, cid, comp) = setup(); let mut source = solid(&mut p, &comp, [0.0, 0.0, 0.0], 200, 100); @@ -1978,3 +2002,17 @@ fn a_copy_of_an_overridden_clip_is_not_served_the_keyed_clips_pixels() { assert_same(&render(&keyed, Some(&cache)), &render(&keyed, None), "the keyed clip's copy"); assert_same(&render(&overridden, Some(&cache)), &render(&overridden, None), "the overridden clip's copy"); } + +#[test] +fn elongated_solids_stay_within_the_pixel_cap_after_rounding() { + use crate::pipeline::solid_size; + // 1 × 100 million at full scale: rounding up used to leave 40 million pixels. + for (w, h) in [(1u32, 100_000_000u32), (100_000_000, 1), (u32::MAX, u32::MAX), (1, u32::MAX)] { + let (s, pw, ph) = solid_size(w, h, 1.0); + assert!(s > 0.0 && f64::from(pw) * f64::from(ph) <= 16.0e6, "{w}x{h} -> {pw}x{ph} at {s}"); + } + // A solid within the cap keeps its scale; an over-cap square is lowered by the exact ratio. + assert_eq!(solid_size(4000, 4000, 1.0), (1.0, 4000, 4000)); + let (s, pw, ph) = solid_size(5000, 5000, 1.0); + assert!((s - 0.8).abs() < 0.01 && pw * ph <= 16_000_000); +} diff --git a/crates/render/src/tests/auto.rs b/crates/render/src/tests/auto.rs index 22f1a6215..a53bb718a 100644 --- a/crates/render/src/tests/auto.rs +++ b/crates/render/src/tests/auto.rs @@ -14,12 +14,13 @@ struct MockAccel { handle: bool, calls: AtomicUsize, pick: AutoPick, + effect_calls: AtomicUsize, } impl MockAccel { /// Construct a renderer stand-in without making assumptions about scheduling or speed. fn new(handle: bool) -> MockAccel { - MockAccel { handle, calls: Default::default(), pick: Default::default() } + MockAccel { handle, calls: Default::default(), pick: Default::default(), effect_calls: Default::default() } } /// Number of full-frame requests dispatched to this accelerator. fn calls(&self) -> usize { @@ -42,6 +43,7 @@ impl Accelerator for MockAccel { }) } fn supports_effect(&self, _id: &str) -> bool { + self.effect_calls.fetch_add(1, Ordering::SeqCst); false } fn effects(&self, _chain: &[crate::FxStep], _buf: &crate::Buf, _levels: Option) -> Option { @@ -160,3 +162,201 @@ fn auto_pick_follows_changing_costs() { assert!(pick.stats(AutoKey::new(ItemId(7), 1.0, true)).is_none()); assert!(pick.stats(AutoKey::new(ItemId(7), 0.5, false)).is_none()); } + +/// Counts actual CPU source reads and rejects any CPU fallback inside a GPU error scope. +struct ScopedFootage { + in_scope: std::sync::atomic::AtomicBool, + reads: AtomicUsize, +} +impl crate::FootageSource for ScopedFootage { + fn frame(&self, _: ItemId, f: &effectcraft_project::Footage, _: Tick) -> Option> { + assert!(!self.in_scope.load(Ordering::SeqCst), "CPU fallback ran before GPU validation/cleanup"); + self.reads.fetch_add(1, Ordering::SeqCst); + Some(std::sync::Arc::new(crate::Image::filled(f.width, f.height, [1.0, 0.0, 0.0, 1.0]))) + } +} + +fn scoped_project() -> (Project, ItemId) { + let (mut p, cid) = project(); + let comp = p.comp(cid).unwrap().clone(); + let footage = effectcraft_project::Footage { + path: "synthetic.png".into(), + kind: effectcraft_project::FootageKind::Still, + width: 50, + height: 50, + has_video: true, + duration: Tick::from_seconds_f64(2.0), + ..Default::default() + }; + let item = p.add_item("Source", Label::Red, None, ItemKind::Footage(footage)); + let mut layer = build::layer(&mut p, &comp, "Source", LayerSource::Footage { item }, (50, 50), None); + super::add_effect_200(&mut p, &mut layer, "ec.blur.gaussian", &[]); + p.comp_mut(cid).unwrap().layers = vec![layer].into(); + (p, cid) +} + +/// #3357: a declined GPU frame must not allocate a CPU frame only to discard it when the +/// enclosing GPU scope reports a fault. The caller retries on a fresh CPU renderer once. +#[test] +fn failed_gpu_scope_returns_before_cpu_fallback() { + let (p, cid) = scoped_project(); + for handled in [false, true] { + let a = MockAccel::new(handled); + let src = ScopedFootage { in_scope: false.into(), reads: 0.into() }; + let mut r = Renderer::new(&p, &src, RenderOpts { backend: Backend::Gpu, ..Default::default() }); + r.accel = Some(&a); + let result = r.comp_frame_scoped(cid, Tick::ZERO, |a| { + src.in_scope.store(true, Ordering::SeqCst); + let frame = a.comp_frame(&r, cid, Tick::ZERO); + drop(frame); + src.in_scope.store(false, Ordering::SeqCst); + Err::, _>("device retired") + }); + assert_eq!(result.unwrap_err(), "device retired"); + assert_eq!(src.reads.load(Ordering::SeqCst), 0); + let frame = r.with_accel(None).comp_frame(cid, Tick::ZERO); + assert!(frame.get(100, 50)[3] > 0.99); + assert_eq!(src.reads.load(Ordering::SeqCst), 1); + assert_eq!(a.calls(), 1); + } +} + +/// A healthy decline closes its scope before CPU work, and still records Auto timings. +#[test] +fn healthy_gpu_decline_renders_cpu_once_outside_scope() { + let (p, cid) = scoped_project(); + let a = MockAccel::new(false); + let src = ScopedFootage { in_scope: false.into(), reads: 0.into() }; + let mut r = Renderer::new(&p, &src, RenderOpts { backend: Backend::Auto, ..Default::default() }); + r.accel = Some(&a); + // Auto warms up CPU first: the first frame renders on the CPU without a GPU scope. + r.comp_frame_scoped(cid, Tick::ZERO, |_| -> Result, ()> { panic!("Auto's CPU warm-up frame must not enter GPU error scopes") }) + .unwrap(); + assert_eq!(src.reads.load(Ordering::SeqCst), 1); + assert_eq!(a.calls(), 0); + // The next one is the GPU's warm-up frame, which the accelerator declines. + let frame = r + .comp_frame_scoped(cid, Tick::ZERO, |a| { + src.in_scope.store(true, Ordering::SeqCst); + let frame = a.comp_frame(&r, cid, Tick::ZERO); + src.in_scope.store(false, Ordering::SeqCst); + Ok::<_, ()>(frame) + }) + .unwrap(); + assert!(frame.get(100, 50)[3] > 0.99); + assert_eq!(src.reads.load(Ordering::SeqCst), 2); + assert_eq!(a.calls(), 1); + assert_eq!(a.pick.stats(AutoKey::new(cid, 1.0, false)).unwrap().cpu_frames, 2); + // Auto's next frame stays on CPU, without entering a GPU scope at all. + r.comp_frame_scoped(cid, Tick::ZERO, |_| -> Result, ()> { panic!("Auto CPU choice must not enter GPU error scopes") }).unwrap(); + assert_eq!(src.reads.load(Ordering::SeqCst), 3); + assert_eq!(a.effect_calls.load(Ordering::SeqCst), 0, "CPU effects must not use the GPU outside its scope"); +} + +/// A preview starts with every lane asking at once, before any frame is measured: the first +/// frames (the ones on screen) render on the CPU; once one is measured, one lane takes a GPU +/// warm-up frame, and the next waits for its time. (Every lane took a GPU warm-up frame on an +/// integrated GPU 2–4× slower than the CPU, and each first playback stalled at its start.) +#[test] +fn auto_pick_warms_the_gpu_up_one_frame_at_a_time() { + let pick = AutoPick::default(); + let key = AutoKey::new(ItemId(3), 1.0, false); + let first: Vec = (0..12).map(|_| pick.choose(key)).collect(); + assert!(!first.iter().any(|g| *g), "{first:?}"); + pick.record(key, false, 10.0); + let next: Vec = (0..12).map(|_| pick.choose(key)).collect(); + assert_eq!(next.iter().filter(|g| **g).count(), 1, "{next:?}"); + // The CPU frames finish first: still no second GPU frame while the first runs. + for _ in 0..22 { + pick.record(key, false, 10.0); + } + assert!(!(0..12).any(|_| pick.choose(key)), "a second GPU warm-up frame while one runs"); + // Measured: the next warm-up frame may go to the GPU. + pick.record(key, true, 40.0); + assert!(pick.choose(key)); + // A re-probe of the slow side runs one frame at a time too. + let pick = AutoPick::default(); + let mut gpu_asked = 0; + for _ in 0..(4 * AutoPick::REPROBE) { + // (sequential warm-up, then GPU frames held: as lanes that never report back) + if pick.choose(key) { + gpu_asked += 1; + if pick.stats(key).is_none_or(|s| s.gpu_frames < AutoPick::WARMUP) { + pick.record(key, true, 40.0); + } + } else { + pick.record(key, false, 10.0); + } + } + assert!(gpu_asked <= AutoPick::WARMUP as usize + 1, "asked the GPU {gpu_asked} times"); +} + +/// Frames render side by side: twelve CPU frames of 210 ms each at once cost ~18 ms a frame, +/// so a lone GPU frame of 175 ms is the slower side (it measured faster and took the comp). +#[test] +fn auto_pick_counts_frames_running_side_by_side() { + let pick = AutoPick::default(); + let key = AutoKey::new(ItemId(4), 0.5, false); + // Twelve lanes: each starts its next frame as soon as one is done. + let mut lanes: std::collections::VecDeque = (0..12).map(|_| pick.choose(key)).collect(); + for _ in 0..240 { + let Some(gpu) = lanes.pop_front() else { break }; + pick.record(key, gpu, if gpu { 175.0 } else { 210.0 }); + lanes.push_back(pick.choose(key)); + } + let s = pick.stats(key).unwrap(); + assert!(s.cpu_ms.unwrap() < 30.0 && s.gpu_ms.unwrap() > 100.0, "{s:?}"); + assert!(lanes.iter().filter(|g| **g).count() <= 1, "{lanes:?}"); +} + +/// A frame that ends without a time (a GPU failure, a remote frame replaced before it finished) +/// no longer counts as running: the frames after it are timed as one at a time. (It stayed +/// counted, and every later frame on its side was timed as half its cost.) +#[test] +fn auto_pick_abandoned_frames_stop_counting_as_running() { + let pick = AutoPick::default(); + let key = AutoKey::new(ItemId(5), 1.0, false); + let mut dropped = [false; 2]; + for _ in 0..16 { + let gpu = pick.choose(key); + if !std::mem::replace(&mut dropped[usize::from(gpu)], true) { + pick.abandoned(key, gpu); + continue; + } + pick.record(key, gpu, if gpu { 40.0 } else { 10.0 }); + } + let s = pick.stats(key).unwrap(); + assert_eq!((s.cpu_ms, s.gpu_ms), (Some(10.0), Some(40.0)), "{s:?}"); +} + +/// A ticket that never reports (dropped on an error, a retry elsewhere, a cancelled export +/// future) ends its frame like [`AutoPick::abandoned`]; one the GPU declined reports from the +/// CPU. (Each exit that skipped `record` left its frame counted as running, and every later +/// frame on that side was timed as one of several side by side.) +#[test] +fn auto_tickets_end_their_frame_on_every_exit() { + let pick = AutoPick::default(); + let key = AutoKey::new(ItemId(6), 1.0, false); + let mut dropped = [false; 2]; + for _ in 0..16 { + let t = pick.ticket(key); + let gpu = t.gpu(); + if !std::mem::replace(&mut dropped[usize::from(gpu)], true) { + drop(t); + continue; + } + t.record(if gpu { 40.0 } else { 10.0 }); + } + let s = pick.stats(key).unwrap(); + assert_eq!((s.cpu_ms, s.gpu_ms), (Some(10.0), Some(40.0)), "{s:?}"); + // Declined: the frame renders on the CPU and reports there, one frame on it. + let key = AutoKey::new(ItemId(7), 1.0, false); + pick.ticket(key).record(10.0); + let mut t = pick.ticket(key); + assert!(t.gpu(), "the GPU's warm-up frame"); + t.declined(); + assert!(!t.gpu()); + t.record(10.0); + pick.ticket(key).record(10.0); + assert_eq!(pick.stats(key).unwrap().cpu_ms, Some(10.0), "timed as one frame on the CPU"); +} diff --git a/crates/render/src/tests/drop_shadow_cover.rs b/crates/render/src/tests/drop_shadow_cover.rs new file mode 100644 index 000000000..94d4ca67b --- /dev/null +++ b/crates/render/src/tests/drop_shadow_cover.rs @@ -0,0 +1,142 @@ +use super::*; +use crate::{EvalCtx, Image, NoFootage, RenderOpts, Renderer}; +use effectcraft_effects::Buf; + +// A synthetic painted source avoids installed-font dependencies and the multi-gigabyte +// allocation in #3357 while exercising the same text/shape effect-stack bounds. +fn source(scale: f64) -> Buf { + let mut img = Image::new(20, 12); + for y in 2..10 { + for x in 2..18 { + img.set(x, y, [0.8, 0.2, 0.1, 1.0]); + } + } + Buf { img, offset: [7.0, -3.0], scale } +} + +#[test] +fn drop_shadow_does_not_cover_the_zoomed_layer_rectangle() { + let (mut p, cid, comp) = setup(); + let mut layer = build::layer(&mut p, &comp, "Shape", LayerSource::Shape, (0, 0), None); + add_effect_200(&mut p, &mut layer, "ec.perspective.dropshadow", &[("distance", Value::Scalar(2.0)), ("softness", Value::Scalar(1.0))]); + let r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + let ctx = EvalCtx::new(&p, cid, &comp, Tick::ZERO); + let out = r.apply_effects(&ctx, &layer, source(4.0), false); + // The old cover makes this at least 800 x 400. Alignment and the shadow's + // own padding need less than 128 pixels per side for this source. + assert!(out.img.width < 128 && out.img.height < 128, "{} x {}", out.img.width, out.img.height); +} + +#[test] +fn drop_shadow_matches_the_covered_reference() { + for scale in [0.5, 1.0, 4.0] { + for only in [false, true] { + for direction in [0.0, 135.0, 270.0] { + let (mut p, cid, comp) = setup(); + let mut layer = build::layer(&mut p, &comp, "Text", LayerSource::Text, (0, 0), None); + add_effect_200( + &mut p, + &mut layer, + "ec.perspective.dropshadow", + &[ + ("distance", Value::Scalar(3.5)), + ("softness", Value::Scalar(2.0)), + ("direction", Value::Scalar(direction)), + ("shadowOnly", Value::Bool(only)), + ], + ); + let r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + let ctx = EvalCtx::new(&p, cid, &comp, Tick::ZERO); + let input = source(scale); + let mut covered = input.clone(); + let (size, origin) = ctx.effect_bounds(&layer); + covered.rebase(origin); + covered.cover(size); + covered.rebase([-origin[0], -origin[1]]); + let actual = r.apply_effects(&ctx, &layer, input, false); + let reference = r.apply_effects(&ctx, &layer, covered, false); + // Compare the union in layer-pixel coordinates, including the shadow outside + // the original content. Buffer origins differ by integral pixels. + let x0 = (-actual.offset[0]).min(-reference.offset[0]) as i32; + let y0 = (-actual.offset[1]).min(-reference.offset[1]) as i32; + let x1 = (f64::from(actual.img.width) - actual.offset[0]).max(f64::from(reference.img.width) - reference.offset[0]) as i32; + let y1 = (f64::from(actual.img.height) - actual.offset[1]).max(f64::from(reference.img.height) - reference.offset[1]) as i32; + for y in y0..y1 { + for x in x0..x1 { + let sample = |b: &Buf| b.img.sample_bilinear(f64::from(x) + b.offset[0] + 0.5, f64::from(y) + b.offset[1] + 0.5); + let (a, b) = (sample(&actual), sample(&reference)); + for c in 0..4 { + assert!( + (a[c] - b[c]).abs() < 1e-5, + "scale {scale}, only {only}, direction {direction}, ({x}, {y}), channel {c}: {} != {}", + a[c], + b[c] + ); + } + } + } + } + } + } +} +#[test] +fn drop_shadow_with_glow_stays_local_but_other_effects_keep_cover() { + for other in ["ec.stylize.glow", "ec.color.tint"] { + let (mut p, cid, comp) = setup(); + let mut layer = build::layer(&mut p, &comp, "Shape", LayerSource::Shape, (0, 0), None); + add_effect_200(&mut p, &mut layer, "ec.perspective.dropshadow", &[("distance", Value::Scalar(2.0)), ("softness", Value::Scalar(1.0))]); + let values = if other == "ec.stylize.glow" { vec![("radius", Value::Scalar(1.0))] } else { vec![] }; + add_effect_200(&mut p, &mut layer, other, &values); + let r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + let ctx = EvalCtx::new(&p, cid, &comp, Tick::ZERO); + let out = r.apply_effects(&ctx, &layer, source(4.0), false); + if other == "ec.stylize.glow" { + assert!(out.img.width < 192 && out.img.height < 192, "{} x {}", out.img.width, out.img.height); + } else { + assert!(out.img.width >= 800 && out.img.height >= 400); + } + } +} +#[test] +fn parented_text_and_shape_keep_small_working_images_at_2200_percent() { + for text in [false, true] { + let mut p = Project::default(); + p.settings.bit_depth = effectcraft_project::BitDepth::Bpc32; + let comp = Comp::new(80, 40, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into())); + let mut parent = build::layer(&mut p, &comp, "Null", LayerSource::Null, (100, 100), None); + parent.props.prop_mut("transform/anchor").unwrap().value = Value::Vec3([0.0; 3]); + parent.props.prop_mut("transform/position").unwrap().value = Value::Vec3([40.0, 20.0, 0.0]); + parent.props.prop_mut("transform/scale").unwrap().value = Value::Vec3([2200.0, 2200.0, 100.0]); + let source = if text { LayerSource::Text } else { LayerSource::Shape }; + let mut layer = build::layer(&mut p, &comp, "Painted", source, (80, 40), None); + layer.parent = Some(parent.id); + layer.props.prop_mut("transform/anchor").unwrap().value = Value::Vec3([0.0; 3]); + layer.props.prop_mut("transform/position").unwrap().value = Value::Vec3([0.0; 3]); + if text { + layer.props.prop_mut("text/sourceText").unwrap().value = Value::Text(Box::new(TextDoc { text: "I".into(), size: 8.0, ..Default::default() })); + } else { + let mut next = p.next_id; + let mut ids = Ids(&mut next); + let rect = build::shape_rect(&mut ids, [4.0, 4.0], [0.0, 0.0], 0.0); + let fill = build::shape_fill(&mut ids, [1.0; 4]); + let group = build::shape_group(&mut ids, "Rectangle", vec![rect, fill]); + p.next_id = next; + layer.props.sub_mut("contents").unwrap().children.push(group.into()); + } + add_effect(&mut p, &mut layer, "ec.stylize.glow", [80.0, 40.0], &[("radius", Value::Scalar(0.1))]); + add_effect(&mut p, &mut layer, "ec.perspective.dropshadow", [80.0, 40.0], &[("distance", Value::Scalar(0.1)), ("softness", Value::Scalar(0.1))]); + p.comp_mut(cid).unwrap().layers = vec![layer, parent].into(); + let comp = p.comp(cid).unwrap(); + let layer = &comp.layers[0]; + for scale in [1.0, 0.5] { + let r = Renderer::new(&p, &NoFootage, RenderOpts { scale, ..Default::default() }); + let ctx = EvalCtx::new(&p, cid, comp, Tick::ZERO); + let input = r.layer_input(&ctx, layer, usize::MAX).unwrap(); + assert!(input.img.width < 512 && input.img.height < 512, "text={text}, output scale={scale}: {} x {}", input.img.width, input.img.height); + assert!(input.img.data.iter().any(|pixel| pixel[3] > 0.5), "the painted source is not empty"); + let frame = r.comp_frame(cid, Tick::ZERO); + assert!(frame.data.iter().any(|pixel| pixel[3] > 0.5), "the layer still draws in the composition"); + } + } +} diff --git a/crates/render/src/tests/footage_size.rs b/crates/render/src/tests/footage_size.rs index 065941d8a..6c59f3fb2 100644 --- a/crates/render/src/tests/footage_size.rs +++ b/crates/render/src/tests/footage_size.rs @@ -106,3 +106,94 @@ fn still_layers_are_cached_across_times_and_movies_are_not() { assert_eq!(src.sizes.unwrap().into_inner().unwrap().len(), fetches + 3, "{kind:?}"); } } + +/// [`Clip`] that also keeps its frames as 8-bit levels, counting the times it's asked for them. +struct Clip8 { + asked: std::sync::atomic::AtomicUsize, +} + +impl FootageSource for Clip8 { + fn frame(&self, item: ItemId, f: &Footage, t: Tick) -> Option> { + let mut img = (*Clip { sizes: None }.frame(item, f, t)?).clone(); + // (partly transparent, premultiplied, as decoded with alpha) + img.data.iter_mut().enumerate().for_each(|(i, p)| { + let a = (i % 7) as f32 / 6.0; + *p = [p[0] * a, p[1] * a, p[2] * a, a]; + }); + Some(Arc::new(img)) + } + fn frame8(&self, item: ItemId, f: &Footage, t: Tick, _: crate::FloatCurve) -> Option> { + self.asked.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let img = self.frame(item, f, t)?; + let rgba = img.data.iter().flat_map(|p| effectcraft_raster::quantize_px_u8(*p)).collect(); + Some(Arc::new(crate::disk_cache::Frame8 { width: img.width, height: img.height, rgba })) + } +} + +/// An 8 bpc render at Full takes plain footage as 8-bit levels and renders the same pixels to +/// the bit; other depths, reduced renders and interpreted pixels never ask for them. +#[test] +fn footage_kept_as_8_bit_levels_renders_the_same_pixels() { + struct Float(Clip8); + impl FootageSource for Float { + fn frame(&self, item: ItemId, f: &Footage, t: Tick) -> Option> { + self.0.frame(item, f, t) + } + } + for (depth, invert, scale, used) in [ + (BitDepth::Bpc8, false, 1.0, true), + (BitDepth::Bpc8, false, 0.75, true), + (BitDepth::Bpc8, false, 0.5, false), + (BitDepth::Bpc8, true, 1.0, false), + (BitDepth::Bpc16, false, 1.0, false), + (BitDepth::Bpc32, false, 1.0, false), + ] { + let (p, cid) = project(depth, invert); + let opts = RenderOpts { scale, ..Default::default() }; + let q8 = Clip8 { asked: Default::default() }; + let frame = Renderer::new(&p, &q8, opts).comp_frame(cid, Tick::ZERO); + let plain = Renderer::new(&p, &Float(Clip8 { asked: Default::default() }), opts).comp_frame(cid, Tick::ZERO); + assert!(frame.data.iter().zip(&plain.data).all(|(a, b)| a.map(f32::to_bits) == b.map(f32::to_bits)), "{depth:?} {invert} {scale}"); + assert_eq!(q8.asked.into_inner() > 0, used, "{depth:?} {invert} {scale}"); + } +} + +/// The 8-bit levels are read while the render records its reads: the movie is in the files the +/// frame (and a cached layer buffer) was made of. (Read outside, a buffer of a movie listed no +/// files and was still marked complete: changed on disk, it was served stale.) +#[test] +fn footage_kept_as_8_bit_levels_is_noted_as_read() { + use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; + struct Noted { + clip: Clip8, + recording: AtomicBool, + outside: AtomicUsize, + } + impl FootageSource for Noted { + fn frame(&self, item: ItemId, f: &Footage, t: Tick) -> Option> { + self.clip.frame(item, f, t) + } + fn frame8(&self, item: ItemId, f: &Footage, t: Tick, float: crate::FloatCurve) -> Option> { + if !self.recording.load(Ordering::SeqCst) { + self.outside.fetch_add(1, Ordering::SeqCst); + } + self.clip.frame8(item, f, t, float) + } + // (as the media pool does: the files of what `f` reads go to `reads`) + fn recording(&self, reads: &crate::reads::ReadLog, f: &mut dyn FnMut()) { + reads.note(crate::reads::ReadFile::new("clip.mov", crate::reads::FileStamp::Native { len: 1, modified: Some(1) })); + self.recording.store(true, Ordering::SeqCst); + f(); + self.recording.store(false, Ordering::SeqCst); + } + } + let (p, cid) = project(BitDepth::Bpc8, false); + let src = Noted { clip: Clip8 { asked: Default::default() }, recording: AtomicBool::new(false), outside: AtomicUsize::new(0) }; + let log = crate::reads::ReadLog::new(); + let mut r = Renderer::new(&p, &src, RenderOpts::default()); + r.reads = Some(&log); + r.comp_frame(cid, Tick::ZERO); + assert!(src.clip.asked.load(Ordering::SeqCst) > 0, "the 8-bit levels were not asked for"); + assert_eq!(src.outside.load(Ordering::SeqCst), 0, "8-bit levels read outside the render's recording"); + assert!(log.files().files().iter().any(|f| &*f.path == "clip.mov"), "the movie is not in the frame's files"); +} diff --git a/crates/render/src/tests/proxy_cache.rs b/crates/render/src/tests/proxy_cache.rs new file mode 100644 index 000000000..85343a25d --- /dev/null +++ b/crates/render/src/tests/proxy_cache.rs @@ -0,0 +1,45 @@ +use super::setup; +use crate::eval::EvalCtx; +use crate::{FootageSource, Image, LayerCache, RenderOpts, Renderer}; +use effectcraft_color::Label; +use effectcraft_project::{Footage, FootageKind, ItemId, ItemKind, LayerSource, Proxy, build, render_queue::ProxyUse}; +use effectcraft_time::Tick; +use std::sync::Arc; + +/// A source and its proxy deliberately have different pixels. +struct Pictures; +impl FootageSource for Pictures { + fn frame(&self, _: ItemId, footage: &Footage, _: Tick) -> Option> { + let color = if footage.path == "proxy" { [0.0, 1.0, 0.0, 1.0] } else { [1.0, 0.0, 0.0, 1.0] }; + Some(Arc::new(Image::filled(16, 16, color))) + } +} + +/// A queue run reuses its layer cache across jobs with different Proxy Use settings. +/// Time effects read layer_input, which must never serve another job's source pixels. +#[test] +fn shared_layer_cache_separates_proxy_use() { + let (mut p, cid, comp) = setup(); + let footage = Footage { path: "original".into(), kind: FootageKind::Still, width: 16, height: 16, has_video: true, ..Default::default() }; + let item = p.add_item("Footage", Label::Red, None, ItemKind::Footage(footage.clone())); + p.item_mut(item).unwrap().proxy = Some(Box::new(Proxy { footage: Footage { path: "proxy".into(), ..footage }, enabled: false })); + let layer = build::layer(&mut p, &comp, "Footage", LayerSource::Footage { item }, (16, 16), None); + p.comp_mut(cid).unwrap().layers.push(layer); + for enabled in [false, true] { + p.item_mut(item).unwrap().proxy.as_mut().unwrap().enabled = enabled; + let cache = LayerCache::default(); + let ctx = EvalCtx::new(&p, cid, p.comp(cid).unwrap(), Tick::ZERO); + let layer = &ctx.comp.layers[0]; + for proxy in [ProxyUse::UseAll, ProxyUse::UseNone, ProxyUse::CurrentSettings, ProxyUse::UseCompOnly, ProxyUse::UseAll] { + let mut r = Renderer::new(&p, &Pictures, RenderOpts { proxy, ..Default::default() }); + let input = r.layer_input(&ctx, layer, 0).unwrap(); + let content = r.content_buf(&ctx, layer).unwrap(); + r.cache = Some(&cache); + assert_eq!(r.layer_input(&ctx, layer, 0).unwrap().img.data, input.img.data, "{proxy:?}, enabled={enabled}"); + assert_eq!(r.content_buf(&ctx, layer).unwrap().img.data, content.img.data, "{proxy:?}, enabled={enabled}"); + let hits = cache.stats().hits; + r.layer_input(&ctx, layer, 0).unwrap(); + assert!(cache.stats().hits > hits, "unchanged proxy settings still reuse inputs"); + } + } +} diff --git a/crates/render/src/tests/raster_clip.rs b/crates/render/src/tests/raster_clip.rs new file mode 100644 index 000000000..c85176fca --- /dev/null +++ b/crates/render/src/tests/raster_clip.rs @@ -0,0 +1,360 @@ +//! #1388: 3D text and shape layers rasterise only the part of the layer the output shows. Where +//! the whole raster fits its pixel budget the frame is identical to rasterising the whole layer; +//! close to the camera the raster stays small instead of growing to ~16 million pixels. + +use effectcraft_color::Label; +use effectcraft_keyframe::{TextDoc, Value}; +use effectcraft_project::build::{self, Ids}; +use effectcraft_project::{Comp, ItemId, ItemKind, Layer, LayerSource, Project}; +use effectcraft_time::{FrameRate, Tick}; + +use crate::{NoFootage, RenderOpts, Renderer}; + +const W: u32 = 400; +const H: u32 = 300; + +/// The default camera's distance to the z = 0 plane. +fn zoom() -> f64 { + W as f64 * 50.0 / 36.0 +} + +fn setup() -> (Project, ItemId, Comp) { + let mut p = Project::default(); + p.settings.bit_depth = effectcraft_project::BitDepth::Bpc32; + let comp = Comp::new(W, H, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into())); + (p, cid, comp) +} + +fn set(l: &mut Layer, path: &str, v: Value) { + l.props.prop_mut(path).unwrap_or_else(|| panic!("no {path}")).value = v; +} + +/// A 3D text layer at `pos`, turned `rot_y` degrees about Y. +fn text3(p: &mut Project, comp: &Comp, pos: [f64; 3], rot_y: f64) -> Layer { + let mut l = build::layer(p, comp, "Text", LayerSource::Text, (W, H), None); + l.props.prop_mut("text/sourceText").unwrap().value = Value::Text(Box::new(TextDoc { text: "NEAR CAMERA TEXT".into(), size: 60.0, ..Default::default() })); + l.switches.three_d = true; + set(&mut l, "transform/position", Value::Vec3(pos)); + set(&mut l, "transform/rotationY", Value::Scalar(rot_y)); + l +} + +/// A 3D shape layer (a 300×120 rectangle with a stroke) at `pos`. +fn shape3(p: &mut Project, comp: &Comp, pos: [f64; 3]) -> Layer { + let mut l = build::layer(p, comp, "Shape", LayerSource::Shape, (W, H), None); + let mut next = p.next_id; + { + let mut ids = Ids(&mut next); + let r = build::shape_rect(&mut ids, [300.0, 120.0], [0.0, 0.0], 0.0); + let f = build::shape_fill(&mut ids, [0.9, 0.4, 0.1, 1.0]); + let s = build::shape_stroke(&mut ids, [1.0, 1.0, 1.0, 1.0], 6.0); + let g = build::shape_group(&mut ids, "Box", vec![r, f, s]); + l.props.sub_mut("contents").unwrap().children.push(g.into()); + } + p.next_id = next; + l.switches.three_d = true; + set(&mut l, "transform/position", Value::Vec3(pos)); + l +} + +fn scene(l: impl FnOnce(&mut Project, &Comp) -> Layer) -> (Project, ItemId) { + let (mut p, cid, comp) = setup(); + let layer = l(&mut p, &comp); + p.comp_mut(cid).unwrap().layers.push(layer); + (p, cid) +} + +fn frame(p: &Project, cid: ItemId, whole: bool) -> effectcraft_raster::Image { + let mut r = Renderer::new(p, &NoFootage, RenderOpts::default()); + r.whole_raster = whole; + r.comp_frame(cid, Tick::ZERO) +} + +/// Pixels of the layer's processed buffer. +fn raster_px(p: &Project, cid: ItemId, whole: bool) -> u64 { + let mut r = Renderer::new(p, &NoFootage, RenderOpts::default()); + r.whole_raster = whole; + let c = p.comp(cid).unwrap(); + let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap(); + let b = r.layer_buf(&ctx, &c.layers[0]).unwrap(); + b.img.width as u64 * b.img.height as u64 +} + +#[test] +fn the_visible_part_renders_the_same_frame_as_the_whole_layer() { + let cx = W as f64 / 2.0; + let cy = H as f64 / 2.0; + let cases: Vec<(&str, (Project, ItemId))> = vec![ + ("far", scene(|p, c| text3(p, c, [cx, cy, 300.0], 0.0))), + ("in the frame plane", scene(|p, c| text3(p, c, [cx, cy, 0.0], 0.0))), + ("past the frame edge", scene(|p, c| text3(p, c, [60.0, cy, -200.0], 0.0))), + ("turned", scene(|p, c| text3(p, c, [cx, cy, -150.0], 55.0))), + ("close to the camera", scene(|p, c| text3(p, c, [cx, cy, -zoom() + 40.0], 0.0))), + ("partly behind the camera", scene(|p, c| text3(p, c, [cx, cy, -zoom() + 30.0], 80.0))), + ("behind the camera", scene(|p, c| text3(p, c, [cx, cy, -zoom() - 200.0], 0.0))), + ("shape", scene(|p, c| shape3(p, c, [cx + 150.0, cy, -250.0]))), + ]; + for (label, (p, cid)) in &cases { + let clipped = frame(p, *cid, false); + let whole = frame(p, *cid, true); + assert_eq!(clipped.data, whole.data, "{label}"); + } +} + +/// Close to the camera only part of the text shows: its raster is a fraction of the whole one +/// (millions of pixels), and the frame still shows text. +#[test] +fn text_close_to_the_camera_rasterises_what_shows() { + let (p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0)); + let clipped = raster_px(&p, cid, false); + let whole = raster_px(&p, cid, true); + assert!(whole > 5_000_000, "the whole raster is huge ({whole} px)"); + eprintln!("near camera: {clipped} px rasterised, {whole} px whole"); + assert!(clipped * 10 < whole, "{clipped} px of {whole}"); + let img = frame(&p, cid, false); + let covered = img.data.iter().filter(|px| px[3] > 0.5).count(); + assert!(covered > (W * H / 20) as usize, "text shows ({covered} px)"); + // Behind the camera there is nothing to rasterise. + let (p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() - 200.0], 0.0)); + assert!(raster_px(&p, cid, false) <= 1); +} + +/// The #1388 case at full size: 200 pt text in a 1920 × 1080 comp, close to the camera. Rasterised +/// whole it fills the ~16 million pixel cap; clipped, it stays within a few frames' worth of pixels +/// (and the text still shows). +#[test] +fn hd_text_close_to_the_camera_stays_within_a_few_frames() { + let mut p = Project::default(); + let comp = Comp::new(1920, 1080, FrameRate::FPS_30, Tick::from_seconds_f64(1.0)); + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into())); + let mut l = build::layer(&mut p, &comp, "Text", LayerSource::Text, (1920, 1080), None); + l.props.prop_mut("text/sourceText").unwrap().value = Value::Text(Box::new(TextDoc { text: "NEAR CAMERA".into(), size: 200.0, ..Default::default() })); + l.switches.three_d = true; + set(&mut l, "transform/position", Value::Vec3([960.0, 540.0, -1920.0 * 50.0 / 36.0 + 60.0])); + p.comp_mut(cid).unwrap().layers.push(l); + let (clipped, whole) = (raster_px(&p, cid, false), raster_px(&p, cid, true)); + eprintln!("HD near camera: {clipped} px rasterised, {whole} px whole"); + assert!(whole > 12_000_000, "{whole}"); + assert!(clipped <= 4 * 1920 * 1080, "{clipped} px"); + assert!(frame(&p, cid, false).data.iter().any(|px| px[3] > 0.5), "the text shows"); +} + +/// A slow move of the layer against the camera (a camera move does the same) keeps the part +/// rasterised within the same grid cells for several frames: they reuse the cached raster instead +/// of drawing the text again each frame. +#[test] +fn a_slow_move_reuses_the_clipped_raster() { + let cache = crate::LayerCache::default(); + let at = |x: f64| { + let (p, cid) = scene(|p, c| text3(p, c, [x, H as f64 / 2.0, -zoom() + 40.0], 0.0)); + let mut r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + r.cache = Some(&cache); + r.comp_frame(cid, Tick::ZERO); + }; + at(W as f64 / 2.0); + let misses = cache.stats().misses; + // 16 frames, a quarter of a layer pixel apart (here about three output pixels). + for i in 1..=16 { + at(W as f64 / 2.0 + i as f64 * 0.25); + } + let rasters = cache.stats().misses - misses; + assert!(rasters <= 2, "{rasters} new rasters over 16 frames"); +} + +/// One keyframed motion: (seconds, value) pairs. +type Keys<'a> = &'a [(f64, [f64; 3])]; + +fn keyed(l: &mut Layer, path: &str, keys: Keys) { + l.props.prop_mut(path).unwrap_or_else(|| panic!("no {path}")).keys = + keys.iter().map(|(t, v)| effectcraft_keyframe::Keyframe::new(Tick::from_seconds_f64(*t), Value::Vec3(*v))).collect(); +} + +/// A camera keyed through `pos` / `poi`, and the text keyed through `text_pos` (and turning about +/// Y through `text_rot`): every frame of a second, rendered with the clipped raster and a layer +/// cache shared across the frames, matches rasterising the whole layer without a cache (or differs +/// by an anti-aliasing sub-sample or two on a few edge pixels) — except where the whole raster is held at +/// its pixel cap (softer than the clipped one, which isn't): there the text shows and the clipped +/// raster stays small. +fn moving(label: &str, pos: Keys, poi: Keys, text_pos: Keys, text_rot: &[(f64, f64)]) { + let (mut p, cid, comp) = setup(); + let mut text = text3(&mut p, &comp, [W as f64 / 2.0, H as f64 / 2.0, 0.0], 0.0); + if !text_pos.is_empty() { + keyed(&mut text, "transform/position", text_pos); + } + if !text_rot.is_empty() { + text.props.prop_mut("transform/rotationY").unwrap().keys = + text_rot.iter().map(|(t, v)| effectcraft_keyframe::Keyframe::new(Tick::from_seconds_f64(*t), Value::Scalar(*v))).collect(); + } + let mut cam = build::layer(&mut p, &comp, "Camera", LayerSource::Camera, (W, H), None); + set(&mut cam, "cameraOptions/zoom", Value::Scalar(zoom())); + keyed(&mut cam, "transform/position", pos); + keyed(&mut cam, "transform/poi", poi); + p.comp_mut(cid).unwrap().layers = vec![cam, text].into(); + let cache = crate::LayerCache::default(); + let (mut exact, mut near, mut capped) = (0, 0, 0); + for f in 0..30 { + let t = Tick::from_seconds_f64(f as f64 / 30.0); + let mut clipped = Renderer::new(&p, &NoFootage, RenderOpts::default()); + clipped.cache = Some(&cache); + let mut whole = Renderer::new(&p, &NoFootage, RenderOpts::default()); + whole.whole_raster = true; + let ctx = whole.eval_ctx(cid, t).unwrap(); + let layer = &p.comp(cid).unwrap().layers[1]; + let a = clipped.comp_frame(cid, t); + if clipped.raster_scale(&ctx, layer) == whole.raster_scale(&ctx, layer) { + let b = whole.comp_frame(cid, t); + if a.data == b.data { + exact += 1; + } else { + // The clipped raster places the glyphs at other coordinates (smaller ones: the whole + // raster can be thousands of pixels wide), and tiny-skia transforms them in f32: its + // 4 × 4 anti-aliasing may land a sub-sample or two differently on a few edge pixels. + let (n, worst) = a.data.iter().zip(&b.data).fold((0usize, 0.0f32), |(n, w), (p, q)| { + let d = (0..4).map(|i| (p[i] - q[i]).abs()).fold(0.0f32, f32::max); + (n + usize::from(d > 0.0), w.max(d)) + }); + assert!(worst <= 2.0 / 16.0 + 1e-4 && n * 1000 <= (W * H) as usize, "{label}: frame {f}: {n} px differ, by up to {worst}"); + near += 1; + } + } else { + // Held at the cap whole: the clipped raster is sharper, and small. + let buf = clipped.layer_buf(&ctx, layer).unwrap(); + assert!(buf.img.width as u64 * buf.img.height as u64 <= 16_500_000, "{label}: frame {f}"); + let shows = |img: &effectcraft_raster::Image| img.data.iter().filter(|px| px[3] > 0.5).count(); + let (sa, sb) = (shows(&a), shows(&whole.comp_frame(cid, t))); + // The same text shows (or none, the camera past it); coverage within a few percent. + assert!(sa.abs_diff(sb) * 50 <= sb.max(1) + 50, "{label}: frame {f}: {sa} px of text, {sb} rasterised whole"); + capped += 1; + } + } + eprintln!("{label}: {exact} frames identical, {near} within anti-aliasing noise, {capped} sharper than the capped whole raster"); + assert!(exact + near >= 10, "{label}: {exact} identical, {near} close, {capped} at the cap"); +} + +#[test] +fn a_camera_moving_any_way_renders_every_frame_as_the_whole_layer() { + let (cx, cy, z) = (W as f64 / 2.0, H as f64 / 2.0, -zoom()); + let still = [(0.0, [cx, cy, 0.0])]; + // In towards the text, through it and past it; and back out. + moving("dolly in through the text", &[(0.0, [cx, cy, 2.0 * z]), (1.0, [cx, cy, 150.0])], &[(0.0, [cx, cy, 400.0]), (1.0, [cx, cy, 700.0])], &[], &[]); + moving("dolly out", &[(0.0, [cx, cy, -60.0]), (1.0, [cx, cy, 3.0 * z])], &still, &[], &[]); + // Sideways and up and down close to the text, so it leaves the frame on both sides. + moving( + "pan x", + &[(0.0, [cx - 500.0, cy, -120.0]), (1.0, [cx + 500.0, cy, -120.0])], + &[(0.0, [cx - 500.0, cy, 0.0]), (1.0, [cx + 500.0, cy, 0.0])], + &[], + &[], + ); + moving("pan y", &[(0.0, [cx, cy - 300.0, -90.0]), (1.0, [cx, cy + 300.0, -90.0])], &[(0.0, [cx, cy - 300.0, 0.0]), (1.0, [cx, cy + 300.0, 0.0])], &[], &[]); + // Around the text, close, looking at it. + let orbit: Vec<(f64, [f64; 3])> = (0..=8) + .map(|i| { + let a = i as f64 / 8.0 * std::f64::consts::TAU * 0.4 - 0.8; + (i as f64 / 8.0, [cx + 150.0 * a.sin(), cy, -150.0 * a.cos()]) + }) + .collect(); + moving("orbit", &orbit, &still, &[], &[]); + // The text moves instead: towards the camera, past it, and across. + moving( + "text towards and past the camera", + &[(0.0, [cx, cy, z])], + &still, + &[(0.0, [cx + 200.0, cy - 80.0, 400.0]), (1.0, [cx - 150.0, cy + 60.0, z - 100.0])], + &[], + ); + // Turning close to the camera. + moving("text turning near the camera", &[(0.0, [cx, cy, z])], &still, &[(0.0, [cx, cy, z + 70.0])], &[(0.0, -70.0), (1.0, 70.0)]); +} + +/// Reads of a layer in its own space get the whole layer, not the part the frame shows: an effect +/// taking another layer as a parameter (Set Matte, stretched to fit, from hidden text close to the +/// camera), and the source the motion tracker analyses. +#[test] +fn layer_space_reads_get_the_whole_layer() { + let (mut p, cid, comp) = setup(); + let mut text = text3(&mut p, &comp, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0); + text.switches.video = false; + let text_id = text.id; + let sid = + p.add_item("Solid", Label::Red, None, ItemKind::Solid(effectcraft_project::Solid { color: [0.2, 0.6, 1.0], width: W, height: H, pixel_aspect: 1.0 })); + let mut solid = build::layer(&mut p, &comp, "Solid", LayerSource::Solid { item: sid }, (W, H), None); + let spec = effectcraft_effects::find("ec.channel.setmatte").unwrap(); + let mut next = p.next_id; + let mut g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [W as f64, H as f64]); + p.next_id = next; + g.prop_mut("takeMatteFromLayer").unwrap().value = Value::Layer(Some(text_id.0)); + solid.props.sub_mut("effects").unwrap().children.push(g.into()); + p.comp_mut(cid).unwrap().layers = vec![text, solid].into(); + assert_eq!(frame(&p, cid, false).data, frame(&p, cid, true).data, "Set Matte from the text"); + let r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap(); + let layer = &p.comp(cid).unwrap().layers[0]; + let mut whole = Renderer::new(&p, &NoFootage, RenderOpts::default()); + whole.whole_raster = true; + assert_eq!(r.layer_source(&ctx, layer).unwrap().img, whole.layer_source(&ctx, layer).unwrap().img, "the tracker's source"); +} + +/// A layer whose pixels outside the frame can matter (an effect here) is rasterised whole. +#[test] +fn layers_with_effects_rasterise_whole() { + let (mut p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0)); + let spec = effectcraft_effects::find("ec.blur.gaussian").unwrap(); + let mut next = p.next_id; + let g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [W as f64, H as f64]); + p.next_id = next; + p.comp_mut(cid).unwrap().layers[0].props.sub_mut("effects").unwrap().children.push(g.into()); + assert_eq!(raster_px(&p, cid, false), raster_px(&p, cid, true)); +} + +/// The Layer panel and analysis tools consume layer-space inputs, even with no effects. +#[test] +fn layer_panel_input_reads_the_whole_layer_behind_the_camera() { + let (p, cid) = scene(|p, c| shape3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() - 200.0])); + let r = Renderer::new(&p, &NoFootage, RenderOpts::default()); + let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap(); + let layer = &p.comp(cid).unwrap().layers[0]; + let mut whole = Renderer::new(&p, &NoFootage, RenderOpts::default()); + whole.whole_raster = true; + let expected = whole.layer_input(&ctx, layer, 0).unwrap(); + assert!(expected.img.data.iter().any(|pixel| pixel[3] > 0.5)); + assert_eq!(r.layer_input(&ctx, layer, 0).unwrap().img, expected.img); +} + +#[test] +fn fine_clip_cells_have_distinct_cache_keys() { + let a = kurbo::Rect::new(0.0, 0.0, 1.0 / 128.0, 1.0 / 128.0); + let b = kurbo::Rect::new(1.0 / 128.0, 0.0, 2.0 / 128.0, 1.0 / 128.0); + let key = |clip| crate::pipeline::raster_clip_key(42, clip); + // All three clips rounded to identical coordinates in the old 1/16-unit key. + assert_ne!(key(a), key(b)); + assert_ne!(key(a), key(kurbo::Rect::ZERO)); + assert_ne!(key(b), key(kurbo::Rect::ZERO)); +} + +/// Text animator Blur reads beyond the fixed anti-aliasing padding of a clipped window. +#[test] +fn animated_text_blur_keeps_offscreen_glyph_pixels_while_panning() { + let (mut p, cid) = scene(|p, c| text3(p, c, [W as f64 / 2.0, H as f64 / 2.0, -zoom() + 40.0], 0.0)); + let mut next = p.next_id; + let mut ids = Ids(&mut next); + let props = build::text_anim_props(&mut ids, "blur", false); + let mut animator = build::text_animator(&mut ids, "Blur", props); + animator.prop_mut("properties/blur").unwrap().value = Value::Vec2([40.0, 40.0]); + p.next_id = next; + p.comp_mut(cid).unwrap().layers[0].props.group_mut("text/animators").unwrap().children.push(animator.into()); + let opts = RenderOpts { scale: 0.125, ..Default::default() }; + let cache = crate::LayerCache::default(); + for dx in [-10.0, 0.0, 10.0] { + set(&mut p.comp_mut(cid).unwrap().layers[0], "transform/position", Value::Vec3([W as f64 / 2.0 + dx, H as f64 / 2.0, -zoom() + 40.0])); + let mut r = Renderer::new(&p, &NoFootage, opts); + r.cache = Some(&cache); + let ctx = r.eval_ctx(cid, Tick::ZERO).unwrap(); + let layer = &p.comp(cid).unwrap().layers[0]; + assert!(r.raster_clip(&ctx, layer).is_none(), "animator blur needs off-window pixels"); + let mut whole = Renderer::new(&p, &NoFootage, opts); + whole.whole_raster = true; + assert_eq!(r.comp_frame(cid, Tick::ZERO).data, whole.comp_frame(cid, Tick::ZERO).data); + } +} diff --git a/crates/render/src/tests/time.rs b/crates/render/src/tests/time.rs index 715c37ef0..f959401bb 100644 --- a/crates/render/src/tests/time.rs +++ b/crates/render/src/tests/time.rs @@ -1,7 +1,10 @@ //! Time effects through the renderer: neighbouring frames via `EffectHost::self_at`, and the //! layer cache staying correct while scrubbing. +use std::sync::Arc; + use effectcraft_color::Label; +use effectcraft_effects::plugin::{EffectPlugin, PluginFrame, PluginManifest, PluginParams, register_plugin}; use effectcraft_keyframe::{Keyframe, ShapePath, Value}; use effectcraft_project::build::{self, Ids}; use effectcraft_project::{Comp, ItemId, ItemKind, LayerSource, MaskMode, Project, Solid}; @@ -157,3 +160,89 @@ fn input_keys_ignore_later_effects() { set(&mut p, "effects/#1/blurriness", 8.0); assert_ne!(key(&p).0, one); } + +/// A plug-in that reads the layer's neighbouring frames (WebAssembly ABI 2) makes its layer's +/// cache key depend on the layer time, even when the source and every property are static. A +/// plug-in that does not keeps the old key (#1977: a static layer stays cached across a scrub). +#[test] +fn abi2_plugin_layers_key_on_layer_time() { + struct NeighbourReader { + m: PluginManifest, + } + + impl EffectPlugin for NeighbourReader { + fn manifest(&self) -> &PluginManifest { + &self.m + } + + fn render(&self, _frame: &mut PluginFrame, _params: &PluginParams, _time: f64) -> Result<(), String> { + Ok(()) + } + + fn reads_other_times(&self) -> bool { + true + } + } + + struct Plain { + m: PluginManifest, + } + + impl EffectPlugin for Plain { + fn manifest(&self) -> &PluginManifest { + &self.m + } + + fn render(&self, _frame: &mut PluginFrame, _params: &PluginParams, _time: f64) -> Result<(), String> { + Ok(()) + } + } + + /// A static white solid with one plug-in effect, registered by `plugin`. + fn plugin_scene(plugin: Arc) -> (Project, ItemId) { + let mut p = Project::default(); + let comp = Comp::new(200, 100, FrameRate::FPS_30, Tick::from_seconds_f64(2.0)); + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.clone().into())); + let sid = p.add_item("Solid", Label::Red, None, ItemKind::Solid(Solid { color: [1.0, 1.0, 1.0], width: 200, height: 100, pixel_aspect: 1.0 })); + let mut l = build::layer(&mut p, &comp, "Solid", LayerSource::Solid { item: sid }, (200, 100), None); + let spec = register_plugin(plugin).unwrap(); + let mut next = p.next_id; + let g = effectcraft_effects::instantiate(spec, &mut Ids(&mut next), spec.name, [200.0, 100.0]); + p.next_id = next; + l.props.sub_mut("effects").unwrap().children.push(g.into()); + p.comp_mut(cid).unwrap().layers.push(l); + (p, cid) + } + + fn key(p: &Project, cid: ItemId, t: f64) -> Option { + let comp = p.comp(cid).unwrap(); + let ctx = crate::EvalCtx { + project: p, + comp_id: cid, + comp, + time: Tick::from_seconds_f64(t), + expr: None, + footage: None, + property_overrides: &[], + media_overrides: &[], + reads: None, + }; + crate::cache::layer_key(&ctx, &comp.layers[0], 1.0, false, false) + } + + let manifest = |id: &str| PluginManifest { + api: 2, + id: id.into(), + name: "Key".into(), + category: "Test Plug-ins".into(), + version: "1".into(), + author: String::new(), + description: String::new(), + params: vec![], + }; + + let (plain, c1) = plugin_scene(Arc::new(Plain { m: manifest("org.test.render.time-key.plain") })); + assert_eq!(key(&plain, c1, 0.25), key(&plain, c1, 0.5), "a plain plug-in keeps the old key"); + let (reader, c2) = plugin_scene(Arc::new(NeighbourReader { m: manifest("org.test.render.time-key.reader") })); + assert_ne!(key(&reader, c2, 0.25), key(&reader, c2, 0.5), "an ABI 2 plug-in keys on the layer time"); +} diff --git a/crates/render/src/text.rs b/crates/render/src/text.rs index ed369afa5..eef5b4067 100644 --- a/crates/render/src/text.rs +++ b/crates/render/src/text.rs @@ -198,6 +198,35 @@ pub fn in_index_units(layer: &Layer, uid: effectcraft_project::Uid) -> bool { /// Selection of every glyph by an animator's selectors, per dimension. pub fn animator_selection(ctx: &EvalCtx, layer: &Layer, anim: &PropGroup, lay: &TextLayout) -> Vec<[f64; 3]> { + animator_selection_within(ctx, layer, anim, lay, &SelectorBudget::new()) +} + +/// The Expression Selector evaluations one layer's animators may run in all: counted, not timed, +/// so the same frame renders the same on every machine. +pub struct SelectorBudget(std::cell::Cell); + +impl SelectorBudget { + pub fn new() -> SelectorBudget { + SelectorBudget(std::cell::Cell::new(MAX_SELECTOR_EVALS)) + } + + /// Take one evaluation; `false` when none are left. + fn take(&self) -> bool { + let left = self.0.get(); + self.0.set(left.saturating_sub(1)); + left > 0 + } +} + +impl Default for SelectorBudget { + fn default() -> Self { + Self::new() + } +} + +/// [`animator_selection`] drawing its Expression Selector evaluations from `budget`, which the +/// caller shares across the layer's animators. +pub fn animator_selection_within(ctx: &EvalCtx, layer: &Layer, anim: &PropGroup, lay: &TextLayout, budget: &SelectorBudget) -> Vec<[f64; 3]> { let sels: Vec<&PropGroup> = anim.sub("selectors").map(|s| s.groups().filter(|g| g.enabled).collect()).unwrap_or_default(); if sels.is_empty() { // No selectors: every character is fully affected. @@ -250,13 +279,31 @@ pub fn animator_selection(ctx: &EvalCtx, layer: &Layer, anim: &PropGroup, lay: & { log.mark_unkeyed(); } - let stat = ctx.v3(layer, sel, "amount", [100.0; 3]); + // The keyframed amount, read without running its expression (the fallback when the + // expression cannot run); an Essential Property override still applies. + let stat = if prop.has_expression() { + // The expression is not run here, but an Essential Property override still wins over the base value. + match ctx.property_overrides.iter().rev().find(|o| o.layer == layer.id && o.prop == prop.uid) { + Some(o) => o.value(layer, prop, ctx.time), + None => prop.value_at(layer.layer_time(ctx.time)), + } + } else { + ctx.value(layer, prop) + } + .as_vec3(); + // One allowance for the whole layer (`budget`, shared by its animators): the + // expression runs per character, so a slow expression on a long text is stopped + // once it has failed or the layer's evaluations pass MAX_SELECTOR_EVALS. + let stopped = std::cell::Cell::new(false); Box::new(move |i, n, prev| { let pv = prev.map(|x| x * 100.0); // Without an expression engine: the static amount scales the selection. let fallback = [0, 1, 2].map(|d| stat[d] * prev[d]); - let r = match ctx.expr.filter(|_| prop.has_expression()) { - Some(h) => h.eval_text_selector(ctx, layer, prop, i, n, pv).unwrap_or(fallback), + let r = match ctx.expr.filter(|_| prop.has_expression() && !stopped.get() && budget.take()) { + Some(h) => h.eval_text_selector(ctx, layer, prop, i, n, pv).unwrap_or_else(|_| { + stopped.set(true); + fallback + }), None => fallback, }; r.map(|x| (x / 100.0).clamp(-1.0, 1.0)) @@ -282,13 +329,17 @@ pub fn animator_selection(ctx: &EvalCtx, layer: &Layer, anim: &PropGroup, lay: & acc } +/// The most Expression Selector evaluations one layer's animators run across all its characters +/// (a text of 32,768 characters under one expression selector, or fewer under several). +const MAX_SELECTOR_EVALS: usize = 32_768; /// Evaluate animators for every glyph. pub fn char_transforms(ctx: &EvalCtx, layer: &Layer, text: &PropGroup, lay: &TextLayout) -> Vec { let mut out = vec![CharXf::default(); lay.glyphs.len()]; let Some(anims) = text.sub("animators") else { return out }; + let budget = SelectorBudget::new(); for anim in anims.groups().filter(|g| g.enabled) { let Some(props) = anim.sub("properties") else { continue }; - let sel = animator_selection(ctx, layer, anim, lay); + let sel = animator_selection_within(ctx, layer, anim, lay, &budget); let get = |m: &str| props.get(m).map(|p| ctx.value(layer, p).components()); let get1 = |m: &str| props.get(m).map(|p| ctx.value(layer, p).as_f64()); let c3 = |v: Vec, d: f64| [v.first().copied().unwrap_or(d), v.get(1).copied().unwrap_or(d), v.get(2).copied().unwrap_or(d)]; @@ -509,6 +560,16 @@ pub fn per_char_3d(ctx: &EvalCtx, layer: &Layer) -> bool { layer.is_3d() && layer.props.sub("text").is_some_and(|t| ctx.b(layer, t, "perChar3d")) } +/// Animator blur can read glyph pixels beyond a viewport raster window. Conservatively +/// keep the whole raster even when the current selector happens to select no characters. +pub(crate) fn animator_blur(ctx: &EvalCtx, layer: &Layer) -> bool { + layer.props.group("text/animators").is_some_and(|anims| { + anims.groups().filter(|anim| anim.enabled).any(|anim| { + anim.sub("properties").and_then(|props| props.get("blur")).is_some_and(|blur| ctx.value(layer, blur).as_vec2().iter().any(|value| *value != 0.0)) + }) + }) +} + /// One drawn character. #[derive(Clone, Debug)] pub struct PlacedGlyph { @@ -1122,8 +1183,46 @@ fn draw_glyphs(geom: &TextGeom, img: &mut Image, to_px: &Mat3, s: f64, only: Opt } } +/// Buffer pixels kept around a clip (see [`raster_window`]). +const CLIP_PAD: f64 = 24.0; + +/// The buffer for content bounds `b` (layer space) at scale `s`: (width, height, layer → buffer +/// offset), two pixels of padding around it. With `clip`, only the pixels of that grid inside +/// `clip`, padded by [`CLIP_PAD`]: a whole-pixel window of the whole buffer's grid. Its pixels are +/// the whole buffer's, except that a glyph cut by the window is rasterised from the window's edge: +/// tiny-skia's 4 × 4 anti-aliasing then lands a sub-sample differently on a few edge pixels, up to +/// a dozen rows in from that edge — inside the padding, out of the frame. `None` when `clip` +/// misses the content. +pub(crate) fn raster_window(b: kurbo::Rect, s: f64, clip: Option) -> Option<(u32, u32, [f64; 2])> { + let (full_w, full_h) = ((b.width() * s).ceil() + 4.0, (b.height() * s).ceil() + 4.0); + let full = [-b.x0 * s + 2.0, -b.y0 * s + 2.0]; + let (x0, y0, x1, y1) = match clip { + None => (0.0, 0.0, full_w, full_h), + // Nothing shows (behind the camera). + Some(c) if c.area() <= 0.0 => return None, + Some(c) => ( + (c.x0 * s + full[0] - CLIP_PAD).floor().max(0.0), + (c.y0 * s + full[1] - CLIP_PAD).floor().max(0.0), + (c.x1 * s + full[0] + CLIP_PAD).ceil().min(full_w), + (c.y1 * s + full[1] + CLIP_PAD).ceil().min(full_h), + ), + }; + if !(x1 > x0 && y1 > y0) { + return None; + } + let w = ((x1 - x0) as u32).clamp(1, 16384); + let h = ((y1 - y0) as u32).clamp(1, 16384); + Some((w, h, [full[0] - x0, full[1] - y0])) +} + /// Render a text layer into a layer-space buffer at scale `s`. pub fn render(ctx: &EvalCtx, layer: &Layer, s: f64) -> Buf { + render_in(ctx, layer, s, None) +} + +/// [`render`], only the part of the layer inside `clip` (layer space; the part the output shows, +/// see `Renderer::raster_clip`). +pub fn render_in(ctx: &EvalCtx, layer: &Layer, s: f64, clip: Option) -> Buf { let empty = || Buf { img: Image::new(1, 1), offset: [0.0; 2], scale: s }; let Some(geom) = text_geom(ctx, layer) else { return empty() }; let mut bounds: Option = None; @@ -1137,9 +1236,7 @@ pub fn render(ctx: &EvalCtx, layer: &Layer, s: f64) -> Buf { } let Some(b) = bounds else { return empty() }; let b = b.intersect(kurbo::Rect::new(-20000.0, -20000.0, 20000.0, 20000.0)); - let w = ((b.width() * s).ceil() as u32 + 4).clamp(1, 16384); - let h = ((b.height() * s).ceil() as u32 + 4).clamp(1, 16384); - let offset = [-b.x0 * s + 2.0, -b.y0 * s + 2.0]; + let Some((w, h, offset)) = raster_window(b, s, clip) else { return empty() }; let mut img = Image::new(w, h); let base = Mat3::translate(vec2(offset[0], offset[1])) * Mat3::scale(vec2(s, s)); draw_glyphs(&geom, &mut img, &base, s, None); diff --git a/crates/render/src/text/tests.rs b/crates/render/src/text/tests.rs index 9b52d3098..bbc1cd28e 100644 --- a/crates/render/src/text/tests.rs +++ b/crates/render/src/text/tests.rs @@ -802,3 +802,12 @@ fn text_notes_the_font_files_it_draws_with() { assert!(files.is_complete(), "{files:?}"); assert_eq!(files.get(&font.to_string_lossy()), Some(stamp), "{files:?}"); } + +#[test] +fn selector_evaluations_are_counted_across_the_layer_not_timed() { + let b = SelectorBudget(std::cell::Cell::new(3)); + // Taken by any animator's selectors in turn; the fourth finds none, and stays none. + assert!([b.take(), b.take(), b.take()].iter().all(|t| *t)); + assert!(!b.take() && !b.take()); + assert_eq!(SelectorBudget::new().0.get(), MAX_SELECTOR_EVALS); +} diff --git a/crates/render/src/three_d/adv/raster.rs b/crates/render/src/three_d/adv/raster.rs index 1c37a6267..f0d6028f9 100644 --- a/crates/render/src/three_d/adv/raster.rs +++ b/crates/render/src/three_d/adv/raster.rs @@ -126,11 +126,14 @@ fn setup(s: &Scene, clip: &[[f32; 4]], range: std::ops::Range) -> Vec= bbox[2] || bbox[1] >= bbox[3] { continue; diff --git a/crates/render/src/three_d/camera.rs b/crates/render/src/three_d/camera.rs index 6aee50a78..18f6dfe8a 100644 --- a/crates/render/src/three_d/camera.rs +++ b/crates/render/src/three_d/camera.rs @@ -302,6 +302,13 @@ pub fn auto_orient_2d(ctx: &EvalCtx, layer: &Layer) -> f64 { path_tangent(ctx, layer).map(|t| t.y.atan2(t.x).to_degrees()).unwrap_or(0.0) } +/// The most camera-facing layers whose parents are evaluated inside one another. +const MAX_ORIENT_NESTING: usize = 8; + +thread_local! { + static ORIENT_NESTING: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + /// Whether `id` is `layer` or one of its parents. fn in_parent_chain(ctx: &EvalCtx, layer: &Layer, id: effectcraft_project::LayerId) -> bool { let mut cur = Some(layer); @@ -336,8 +343,27 @@ pub fn auto_orient_3d(ctx: &EvalCtx, layer: &Layer, pos: Vec3) -> Option { if ctx.comp.active_camera(ctx.time).is_some_and(|c| in_parent_chain(ctx, c, layer.id)) { return None; } + // The parent's world matrix evaluates the parent's own orientation: a parent cycle + // of camera-facing layers would recurse without end, and a chain of them is walked + // again from every link. Past MAX_ORIENT_NESTING the layer keeps its Orientation. + let nesting = ORIENT_NESTING.with(|n| n.get()); + if nesting >= MAX_ORIENT_NESTING { + return None; + } + // Restored when this scope ends, a panic unwinding through it included (the CPU retry + // after one must not start with a raised count). + struct Restore(usize); + impl Drop for Restore { + fn drop(&mut self) { + ORIENT_NESTING.with(|n| n.set(self.0)); + } + } + ORIENT_NESTING.with(|n| n.set(nesting + 1)); + let restore = Restore(nesting); + let parent = parent_world(ctx, layer); + drop(restore); let eye = active_camera(ctx).eye; - let e = parent_world(ctx, layer).inverse().unwrap_or(Mat4::IDENTITY).apply(eye); + let e = parent.inverse().unwrap_or(Mat4::IDENTITY).apply(eye); let d = pos - e; (d.length() > 1e-9).then(|| basis_rotation(d, vec3(0.0, 1.0, 0.0))) } diff --git a/crates/render/src/three_d/compose.rs b/crates/render/src/three_d/compose.rs index ff08eb05f..8713833c3 100644 --- a/crates/render/src/three_d/compose.rs +++ b/crates/render/src/three_d/compose.rs @@ -185,6 +185,52 @@ pub(crate) fn view_size(r: &Renderer, ctx: &EvalCtx) -> (f64, f64) { (c.width as f64, c.height as f64) } +/// The layer-space rectangle of a 3D layer that this render's output can show: the output frame +/// (padded by two pixels) projected back onto the layer's plane, the way [`geo`] draws it. `None` +/// when that can't be bounded: an orthographic camera, depth of field (its blur reads past the +/// frame), motion blur (each sub-sample sees another part), or a frame corner whose ray misses the +/// plane in front of the camera (a plane seen towards its horizon). An empty rectangle when the +/// whole content is behind the camera. +pub(crate) fn visible_clip(r: &Renderer, ctx: &EvalCtx, layer: &Layer) -> Option { + let cam = camera_for(r, ctx); + if cam.ortho || cam.dof.is_some() || mb_samples(r, ctx, layer) > 1 { + return None; + } + let comp = view_size(r, ctx); + let s = r.opts.scale; + let outer = r.collapse3d.map_or(Mat4::IDENTITY, |c| c.world); + let world = outer * ctx.world_matrix(layer); + let (off, w, h) = match r.roi_offset() { + Some((x, y, w, h)) => ((x, y), w as f64, h as f64), + None => ((0.0, 0.0), (comp.0 * s).round(), (comp.1 * s).round()), + }; + let proj = Mat4::translate(vec3(-off.0, -off.1, 0.0)) * Mat4::scale(vec3(s, s, 1.0)) * cam.projection(comp.0, comp.1); + let hinv = (proj * world).plane_to_mat3().inverse()?; + let cv = cam.view * world; + let depth = |u: f64, v: f64| cv.0[2][0] * u + cv.0[2][1] * v + cv.0[2][3]; + if let Some(b) = crate::content_bounds(ctx, layer) + && [(b[0], b[1]), (b[2], b[1]), (b[2], b[3]), (b[0], b[3])].iter().all(|&(u, v)| depth(u, v) < NEAR) + { + return Some(kurbo::Rect::ZERO); + } + const PAD: f64 = 2.0; + let mut rect: Option = None; + for (sx, sy) in [(-PAD, -PAD), (w + PAD, -PAD), (w + PAD, h + PAD), (-PAD, h + PAD)] { + let m = &hinv.0; + let qz = m[2][0] * sx + m[2][1] * sy + m[2][2]; + if qz <= 1e-300 { + return None; + } + let (u, v) = ((m[0][0] * sx + m[0][1] * sy + m[0][2]) / qz, (m[1][0] * sx + m[1][1] * sy + m[1][2]) / qz); + if !u.is_finite() || !v.is_finite() || depth(u, v) < NEAR { + return None; + } + let p = kurbo::Rect::new(u, v, u, v); + rect = Some(rect.map_or(p, |r| r.union(p))); + } + rect +} + /// Number of motion-blur sub-samples for a layer. fn mb_samples(r: &Renderer, ctx: &EvalCtx, layer: &Layer) -> usize { if r.opts.motion_blur && ctx.comp.enable_motion_blur && r.layer_motion_blur(layer) { diff --git a/crates/script/src/lib.rs b/crates/script/src/lib.rs index 11d837677..21774cb24 100644 --- a/crates/script/src/lib.rs +++ b/crates/script/src/lib.rs @@ -79,6 +79,7 @@ pub fn install(s: &mut Session) { s.script = Some(runner); s.script_ui.dispatch = Some(dispatch_ui); s.script_ui.tasks = Some(run_tasks); + s.script_ui.poll_render = Some(ui::poll_render_wait); } /// Run `code` as a one-off script named `name`. diff --git a/crates/script/src/model.rs b/crates/script/src/model.rs index e662c1cdc..62f3a8557 100644 --- a/crates/script/src/model.rs +++ b/crates/script/src/model.rs @@ -5,6 +5,7 @@ use effectcraft_engine::Session; use effectcraft_engine::color::Label; use effectcraft_keyframe::{Interp, TextDoc, Value as KV}; +use effectcraft_project::ae_order::{self, AeChild}; use effectcraft_project::{Comp, GroupKind, ItemId, ItemKind, Layer, LayerId, LayerSource, Node, ParamUi, PropGroup}; use effectcraft_render::EvalCtx; use effectcraft_time::Tick; @@ -318,7 +319,8 @@ fn indexed_parade(g: &PropGroup) -> bool { fn node_json(c: &Comp, l: &Layer, uid: u64) -> R { if uid == 0 || uid == l.props.uid { - let n = l.props.children.len(); + // After Effects' count (every slot, used or not); 3D model layers count their own groups. + let n = ae_order::ae_count(l).unwrap_or(l.props.children.len()); return Ok(json!({ "uid": l.props.uid, "kind": "group", "name": l.name, "matchName": matchnames::layer(&l.source), "matchId": "layer", "index": c.index_of(l.id).unwrap_or(0), "depth": 0, "parent": null, "isLayer": true, "numProperties": n, @@ -329,7 +331,8 @@ fn node_json(c: &Comp, l: &Layer, uid: u64) -> R { let ch = chain(l, uid).ok_or("the property no longer exists (it was deleted)")?; let node = *ch.last().ok_or("bad property")?; let parent_group: &PropGroup = if ch.len() >= 2 { ch[ch.len() - 2].as_group().ok_or("bad tree")? } else { &l.props }; - let index = parent_group.children.iter().position(|x| x.uid() == uid).map(|i| i + 1).unwrap_or(0); + let top = if ch.len() == 1 { ae_order::ae_index(l, node.match_id()) } else { None }; + let index = top.unwrap_or_else(|| parent_group.children.iter().position(|x| x.uid() == uid).map(|i| i + 1).unwrap_or(0)); let parent_uid = if ch.len() >= 2 { ch[ch.len() - 2].uid() } else { l.props.uid }; let match_name = matchnames::node(&ch); let instance_parent = indexed_parade(parent_group) && ch.len() >= 2 || (ch.len() == 2 && ch[0].match_id() == "contents"); @@ -475,6 +478,25 @@ fn child(s: &Session, a: &J) -> R { let uid = u(a, "uid").unwrap_or(0); let g: &PropGroup = if uid == 0 || uid == l.props.uid { &l.props } else { l.props.find_group(uid).ok_or("not a property group")? }; let key = a.get("key").cloned().unwrap_or(J::Null); + // A layer's numbers and match names are After Effects' (Marker is 1, unused groups keep their + // slot and resolve to the same empty group by number or by match name). + let slot = match &key { + J::Number(n) => Some(n.as_u64().and_then(|i| usize::try_from(i).ok())), + J::String(name) => ae_order::ae_index_of_match_name(l, name).map(Some), + _ => None, + }; + if std::ptr::eq(g, &l.props) + && ae_order::ae_slots(&l.source).is_some() + && let Some(slot) = slot + { + let Some(i) = slot else { return Ok(J::Null) }; + return match ae_order::ae_child(l, i) { + Some(AeChild::Node(n)) => node_json(c, l, n.uid()), + Some(AeChild::Marker) => Ok(json!({"marker": true})), + Some(AeChild::Absent(s)) => Ok(json!({"absent": true, "name": s.name, "matchName": s.match_name, "index": i})), + None => Ok(J::Null), + }; + } let found = match &key { J::Number(n) => n.as_u64().and_then(|i| g.children.get((i as usize).checked_sub(1)?)), J::String(k) => find_child(l, g, k, NESTED_CHILD_DEPTH), diff --git a/crates/script/src/prelude.js b/crates/script/src/prelude.js index b9108b2a1..ce6cd44f4 100644 --- a/crates/script/src/prelude.js +++ b/crates/script/src/prelude.js @@ -26,7 +26,7 @@ function __err(msg) { } function __num(v, what) { var n = Number(v); - if (typeof v === "boolean" || isNaN(n)) throw __err((what || "value") + " must be a number"); + if (typeof v === "boolean" || Number.isNaN(n)) throw __err((what || "value") + " must be a number"); return n; } function __str(v) { @@ -43,11 +43,11 @@ if (typeof String.prototype.substr !== "function") { if (this === null || this === undefined) throw new TypeError("String.substr called on null or undefined"); var text = String(this), n = text.length; var from = Number(start); - if (isNaN(from)) from = 0; + if (Number.isNaN(from)) from = 0; // ToIntegerOrInfinity, then clamp positive or negative offsets. from = from < 0 ? Math.max(n + Math.ceil(from), 0) : Math.min(Math.floor(from), n); var count = length === undefined ? n - from : Number(length); - if (isNaN(count) || count <= 0) return ""; + if (Number.isNaN(count) || count <= 0) return ""; return text.slice(from, from + count); }, writable: true, configurable: true @@ -79,9 +79,10 @@ BlendingMode.SILHOUETTE_ALPHA = BlendingMode.SILHOUETE_ALPHA; var KeyframeInterpolationType = __enum("KeyframeInterpolationType", ["LINEAR", "BEZIER", "HOLD"], 6612); var TrackMatteType = __enum("TrackMatteType", ["NO_TRACK_MATTE", "ALPHA", "ALPHA_INVERTED", "LUMA", "LUMA_INVERTED"], 5012); var LightType = __enum("LightType", ["PARALLEL", "SPOT", "POINT", "AMBIENT", "ENVIRONMENT"], 4412); +// In After Effects' order and numbers (26.3): right comes before centre. var ParagraphJustification = __enum("ParagraphJustification", [ - "LEFT_JUSTIFY", "CENTER_JUSTIFY", "RIGHT_JUSTIFY", "FULL_JUSTIFY_LASTLINE_LEFT", "FULL_JUSTIFY_LASTLINE_CENTER", - "FULL_JUSTIFY_LASTLINE_RIGHT", "FULL_JUSTIFY_LASTLINE_FULL", "MULTIPLE_JUSTIFICATIONS"], 7413); + "MULTIPLE_JUSTIFICATIONS", "LEFT_JUSTIFY", "RIGHT_JUSTIFY", "CENTER_JUSTIFY", "FULL_JUSTIFY_LASTLINE_LEFT", + "FULL_JUSTIFY_LASTLINE_RIGHT", "FULL_JUSTIFY_LASTLINE_CENTER", "FULL_JUSTIFY_LASTLINE_FULL"], 7412); var PropertyValueType = __enum("PropertyValueType", [ "NO_VALUE", "ThreeD_SPATIAL", "ThreeD", "TwoD_SPATIAL", "TwoD", "OneD", "COLOR", "CUSTOM_VALUE", "MARKER", "LAYER_INDEX", "MASK_INDEX", "SHAPE", "TEXT_DOCUMENT"], 6412); @@ -120,7 +121,14 @@ function __blendLabel(v) { var __interpKey = { 6612: "linear", 6613: "bezier", 6614: "hold" }; var __matteKind = { 5013: "alpha", 5014: "alphaInverted", 5015: "luma", 5016: "lumaInverted" }; var __matteEnum = { Alpha: 5013, AlphaInverted: 5014, Luma: 5015, LumaInverted: 5016 }; -var __justKey = { 7413: "left", 7414: "center", 7415: "right", 7416: "justifyLeft", 7417: "justifyCenter", 7418: "justifyRight", 7419: "justifyAll" }; +var __justKey = {}; +__justKey[ParagraphJustification.LEFT_JUSTIFY] = "left"; +__justKey[ParagraphJustification.CENTER_JUSTIFY] = "center"; +__justKey[ParagraphJustification.RIGHT_JUSTIFY] = "right"; +__justKey[ParagraphJustification.FULL_JUSTIFY_LASTLINE_LEFT] = "justifyLeft"; +__justKey[ParagraphJustification.FULL_JUSTIFY_LASTLINE_CENTER] = "justifyCenter"; +__justKey[ParagraphJustification.FULL_JUSTIFY_LASTLINE_RIGHT] = "justifyRight"; +__justKey[ParagraphJustification.FULL_JUSTIFY_LASTLINE_FULL] = "justifyAll"; // ------------------------------------------------------------------ output, $ and dialogs @@ -292,6 +300,34 @@ function Folder(path) { if (!(this instanceof Folder)) return new Folder(path); this.__path = path === undefined ? "" : __str(path); } + +// ECMAScript's legacy case canonicalisation (what a `/i` regular expression compares with): the +// upper case form when it is one code unit, and not an ASCII letter for a non-ASCII one. +function __canon(ch) { + var u = ch.toUpperCase(); + if (u.length !== 1) return ch; + if (ch.charCodeAt(0) >= 128 && u.charCodeAt(0) < 128) return ch; + return u; +} + +// A case-insensitive `*` / `?` file mask match in time bounded by mask × name length (a +// regular expression of `.*` runs backtracks combinatorially on overlapping wildcards). +function __globMatch(mask, name) { + // Wildcards run over the original name (lowercasing it can change its length: "İ" becomes + // two code units); literal characters compare one at a time, case-insensitively as `/i` does + // ("ς" and "σ" and "Σ" match each other). + var p = mask, s = name; + var i = 0, j = 0, star = -1, mark = 0; + while (j < s.length) { + if (i < p.length && (p[i] === "?" || p[i] === s[j] || __canon(p[i]) === __canon(s[j]))) { i++; j++; } + else if (i < p.length && p[i] === "*") { star = i++; mark = j; } + else if (star !== -1) { i = star + 1; j = ++mark; } + else return false; + } + while (i < p.length && p[i] === "*") i++; + return i === p.length; +} + Folder.prototype = { constructor: Folder, get fsName() { return this.__path; }, @@ -310,10 +346,7 @@ Folder.prototype = { var out = []; for (var i = 0; i < names.length; i++) { var n = names[i]; - if (typeof mask === "string" && mask !== "*") { - var re = new RegExp("^" + mask.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".") + "$", "i"); - if (!re.test(n.split(/[\/\\]/).pop())) continue; - } + if (typeof mask === "string" && mask !== "*" && !__globMatch(mask, n.split(/[\/\\]/).pop())) continue; var isDir = JSON.parse(__file("isDir", n)); var f = isDir ? new Folder(n) : new File(n); if (typeof mask === "function" && !mask(f)) continue; @@ -436,7 +469,7 @@ function __callable(o) { // 1-based collections with `[n]` access. var __coll = { get: function (t, k, r) { - if (typeof k === "string" && /^[0-9]+$/.test(k)) return t.__at(parseInt(k, 10)); + if (typeof k === "string" && /^[0-9]+$/.test(k)) return t.__at(Number.parseInt(k, 10)); return Reflect.get(t, k, r); }, }; @@ -628,10 +661,46 @@ TextDocument.prototype = { function __propOf(layer, info) { if (!info) return null; if (info.isLayer) return layer; + if (info.marker) return layer.marker; + if (info.absent) return __absentGroup(layer, info); if (info.kind === "group") return info.isMask ? new MaskPropertyGroup(layer, info.uid) : new PropertyGroup(layer, info.uid); return new Property(layer, info.uid); } +// A top-level slot After Effects always has and this layer does not use yet (Layer Styles before +// any style, Data, Sets…): an empty group, so loops over `numProperties` meet no null. +function __absentGroup(layer, info) { + var g = { + __kind: "PropertyGroup", + get name() { return info.name; }, + get matchName() { return info.matchName; }, + get propertyIndex() { return info.index; }, + get propertyDepth() { return 1; }, + get propertyType() { return PropertyType.NAMED_GROUP; }, + get parentProperty() { return layer; }, + get numProperties() { return 0; }, + get enabled() { return true; }, + get active() { return layer.enabled; }, + get canSetEnabled() { return false; }, + get isModified() { return false; }, + get isEffect() { return false; }, + get isMask() { return false; }, + get elided() { return false; }, + get selected() { return false; }, + property: function () { return null; }, + propertyGroup: function (n) { return n === undefined || n === 1 ? layer : null; }, + canAddProperty: function () { return false; }, + addProperty: function (n) { throw __err("Can not add \"" + n + "\" to \"" + info.name + "\""); }, + toString: function () { return "[object PropertyGroup]"; }, + }; + // Callable like any group: `layer.property(7)(1)` is `.property(1)`, null. + var f = function (k) { return g.property(k); }; + delete f.name; + delete f.length; + for (const k of Object.getOwnPropertyNames(g)) Object.defineProperty(f, k, Object.getOwnPropertyDescriptor(g, k)); + return f; +} + function PropertyBase() {} PropertyBase.prototype = { __info: function () { return __get("node", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: this.__uid }); }, @@ -867,6 +936,12 @@ Property.prototype.property = function () { return null; }; if (!idx) return null; return this.__layer.containingComp.layer(idx).__id; } + // A string parameter (a LUT or look file, a text field) takes the text, or a File's path (#3395). + // A hidden one (Mesh Warp's mesh) stays refused by the key setters too, as by setValue. + if (info.type === "string") { + if (info.hidden) throw __err("\"" + info.name + "\" can not be set"); + return __str(v); + } if (info.type === "enum") return __num(v) - 1; if (info.type === "bool") return typeof v === "boolean" ? v : __num(v) !== 0; if (info.type === "color") { @@ -1652,7 +1727,7 @@ CompItem.prototype.constructor = CompItem; P.saveFrameToPng = function (time, file) { var f = __str(file); if (!f) throw __err("saveFrameToPng: missing file"); - __file("write", f, ""); + __file("checkWrite", f); __call("comp.saveFrameAs", { comp: this.__id, path: f, time: __num(time, "time") }); }; P.openInEssentialGraphics = function () { __call("comp.openInEssentialGraphics", { comp: this.__id }); }; @@ -1662,8 +1737,8 @@ CompItem.prototype.constructor = CompItem; if (!/\.[A-Za-z0-9]+$/.test(f)) f += ".ectemplate"; if (!overwrite && JSON.parse(__file("exists", f)) === true) return false; if (this.__controllers().length === 0) return false; - // The file gate applies to templates too. - __file("write", f, ""); + // The file gate applies to templates too (checked without touching the file). + __file("checkWrite", f); __call("essential.exportTemplate", { comp: this.__id, path: f }); return true; }; diff --git a/crates/script/src/runtime.rs b/crates/script/src/runtime.rs index 2c18039e8..28e490158 100644 --- a/crates/script/src/runtime.rs +++ b/crates/script/src/runtime.rs @@ -30,6 +30,8 @@ pub struct Outcome { /// The script is waiting in a modal ScriptUI dialog's `show()` (it goes on when the dialog /// closes; see [`crate::dispatch_ui`]). pub waiting: bool, + /// The native frontend owns the session while this host waits for its render worker. + pub render_host: Option, } /// An uncaught script error. @@ -55,6 +57,9 @@ impl Outcome { if self.waiting { j["waiting"] = json!(true); } + if let Some(host) = self.render_host { + j["renderHost"] = json!(host); + } j } } @@ -74,13 +79,16 @@ pub(crate) struct Active { /// Running on a script-host thread: modal dialogs hand the session back to the caller /// through this link while they wait. pub(crate) link: Option, + /// Captured at invocation start: the frontend resets its opt-in when the caller returns. + pub(crate) yield_renders: bool, /// The files `$.evalFile` is evaluating, outermost first. pub(crate) loading: Vec, } impl Active { pub(crate) fn new(session: Session, name: &str, host: u32) -> Active { - Active { session, output: vec![], groups: vec![], name: name.to_string(), host, link: None, loading: vec![] } + let yield_renders = session.script_ui.yield_renders; + Active { session, output: vec![], groups: vec![], name: name.to_string(), host, link: None, yield_renders, loading: vec![] } } } @@ -130,6 +138,13 @@ fn native_exec(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult exec_on_keys(&mut a.session, &id, params, k), @@ -137,7 +152,10 @@ fn native_exec(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult Ok(js_str(&v.to_string())), + Ok(Ok(v)) => { + let v = if yield_render { crate::ui::wait_for_render(ctx, v).map_err(throw)? } else { v }; + Ok(js_str(&v.to_string())) + } Ok(Err(e)) | Err(e) => Err(throw(e)), } } @@ -409,6 +427,8 @@ fn native_file(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResult check_access(s, &path, true).map(|_| json!(true)), "read" | "readBinary" => { let p = check_access(s, &path, false)?; let bytes = s.services.read_file(&p.to_string_lossy()).map_err(|e| format!("Unable to read {path}: {e}"))?; @@ -512,8 +532,33 @@ fn native_eval_file(_: &JsValue, args: &[JsValue], ctx: &mut Context) -> JsResul r } +/// Host hooks: code a script compiles at run time (`eval`, `new Function`, …) passes the same +/// nesting check as the script itself, since the parser recurses per level and a deep string +/// built at run time would overflow the stack. +struct Hooks; + +impl boa_engine::context::HostHooks for Hooks { + fn ensure_can_compile_strings( + &self, + _realm: boa_engine::realm::Realm, + parameters: &[JsString], + body: &JsString, + _direct: bool, + _context: &mut Context, + ) -> JsResult<()> { + let mut code: String = parameters.iter().map(|p| p.to_std_string_escaped()).collect::>().join(","); + // With no parameters (`eval`) the body is the source as it stands, so a leading `#!` line + // is still its first line; with parameters it sits inside a function, where it is no comment. + if !parameters.is_empty() { + code.push('\n'); + } + code.push_str(&body.to_std_string_escaped()); + check_nesting(&code).map_err(|e| JsNativeError::syntax().with_message(e).into()) + } +} + pub(crate) fn new_context() -> Result { - let mut ctx = Context::default(); + let mut ctx = boa_engine::context::ContextBuilder::new().host_hooks(std::rc::Rc::new(Hooks)).build().map_err(|e| e.to_string())?; let limits = ctx.runtime_limits_mut(); limits.set_loop_iteration_limit(200_000_000); limits.set_recursion_limit(2000); @@ -718,6 +763,10 @@ pub fn run(session: &mut Session, req: &ScriptRequest) -> Outcome { }; let Some(x) = expanded else { return run_expanded(session, req) }; let mut out = run_expanded(session, &ScriptRequest { code: &x.code, name: req.name, console: req.console }); + if let Some(host) = out.render_host { + crate::ui::keep_render_source_map(host, x); + return out; + } if let Some(e) = out.error.as_mut() { x.locate(e); } @@ -747,19 +796,129 @@ fn run_expanded(session: &mut Session, req: &ScriptRequest) -> Outcome { /// 8 MiB thread such as the Script Console's, so deeper scripts are rejected up front. pub const MAX_NESTING: usize = 96; -/// An error when `code` nests brackets deeper than [`MAX_NESTING`] (strings, template literals -/// and comments are skipped). +/// Skip template-literal text from `*i` (just past a backtick or an interpolation's `}`). Returns +/// true when it stopped past a `${` (code follows), false at the closing backtick or the end. +fn skip_template_text(b: &[u8], i: &mut usize) -> bool { + while let Some(&d) = b.get(*i) { + match d { + b'\\' => *i += 2, + b'`' => { + *i += 1; + return false; + } + b'$' if b.get(*i + 1) == Some(&b'{') => { + *i += 2; + return true; + } + _ => *i += 1, + } + } + false +} + +/// Whether a `/` after the last significant byte `prev` (at `at` in `b`) starts a regular +/// expression literal rather than dividing: after an operator, an opening bracket, a separator, +/// a keyword that takes an expression, or at the start. After a name, a number, a string or a +/// closing bracket it divides (a regular expression after `)` is the one case this misreads). +/// `after_dot`: the word ending at `at` follows a `.` (a property name, never a keyword); a `++` +/// or `--` before the `/` is a postfix update, so it divides. +fn regex_may_start(b: &[u8], prev: u8, at: usize, after_dot: bool) -> bool { + match prev { + 0 => true, + b'+' | b'-' if at > 0 && b.get(at - 1) == Some(&prev) => false, + b'(' | b',' | b'=' | b':' | b'[' | b'!' | b'&' | b'|' | b'?' | b'{' | b';' | b'+' | b'-' | b'*' | b'%' | b'<' | b'>' | b'~' | b'^' => true, + _ if after_dot => false, + c if c.is_ascii_alphanumeric() || c == b'_' || c == b'$' => { + let start = (0..=at).rev().find(|&k| !b.get(k).is_some_and(|d| d.is_ascii_alphanumeric() || *d == b'_' || *d == b'$')).map_or(0, |k| k + 1); + let word = b.get(start..=at).unwrap_or_default(); + [&b"return"[..], b"typeof", b"instanceof", b"in", b"of", b"new", b"delete", b"void", b"throw", b"case", b"do", b"else", b"yield", b"await"] + .contains(&word) + } + _ => false, + } +} + +/// Whether the `.` at `at` is a property access: not the decimal point of a number (`1.`, `12.`) +/// that ends there. A number written `1.5.x` has a fraction before the dot, so that dot accesses. +fn dot_is_property(b: &[u8], at: usize) -> bool { + let word = |d: u8| d.is_ascii_alphanumeric() || d == b'_' || d == b'$'; + let start = (0..at).rev().find(|&k| !b.get(k).is_some_and(|d| word(*d))).map_or(0, |k| k + 1); + let before = b.get(start..at).unwrap_or_default(); + let integer = !before.is_empty() && before.iter().all(u8::is_ascii_digit); + // `1.5.x`: digits that follow another `.` are a fraction. + !(integer && !(start > 0 && b.get(start - 1) == Some(&b'.'))) +} + +/// Skip a regular expression literal that starts at `b[*i]` (a `/`), to its closing `/` (flags +/// follow as ordinary letters). A `/` inside a class `[…]` or after a backslash does not end it, +/// and neither a quote nor a backtick inside it starts a string; a line break ends it. +fn skip_regex(b: &[u8], i: &mut usize) { + *i += 1; + let mut class = false; + while let Some(&d) = b.get(*i) { + match d { + b'\\' => *i += 1, + b'[' => class = true, + b']' => class = false, + b'/' if !class => return, + b'\n' => return, + _ => {} + } + *i += 1; + } +} + +/// An error when `code` nests brackets deeper than [`MAX_NESTING`] (strings, regular expression +/// literals, template literal text and comments are skipped; the code inside `${…}` +/// interpolations is checked). pub fn check_nesting(code: &str) -> Result<(), String> { let b = code.as_bytes(); let (mut i, mut depth, mut max) = (0usize, 0usize, 0usize); + // The last significant byte (not space or comment), and where it is. + let (mut prev, mut prev_at) = (0u8, 0usize); + // A `#!` line at the very start is a comment (Boa reads it so): its text is not scanned. + if b.starts_with(b"#!") { + i = b.iter().position(|d| *d == b'\n').unwrap_or(b.len()); + } + // Whether the word that ends at `prev_at` came right after a `.` token (comments and + // whitespace between them do not matter). + let mut word_after_dot = false; + // The bracket depth of each open `${`, to tell its closing `}` from an inner one. + let mut interpolations: Vec = vec![]; while let Some(&c) = b.get(i) { match c { + b'`' => { + i += 1; + if skip_template_text(b, &mut i) { + depth += 1; + max = max.max(depth); + interpolations.push(depth); + (prev, prev_at) = (b'{', i - 1); + } else { + (prev, prev_at) = (b'`', i.saturating_sub(1)); + } + continue; + } + b'}' if interpolations.last() == Some(&depth) => { + interpolations.pop(); + depth -= 1; + i += 1; + if skip_template_text(b, &mut i) { + depth += 1; + max = max.max(depth); + interpolations.push(depth); + (prev, prev_at) = (b'{', i - 1); + } else { + (prev, prev_at) = (b'`', i.saturating_sub(1)); + } + continue; + } b'(' | b'[' | b'{' => { depth += 1; max = max.max(depth); } b')' | b']' | b'}' => depth = depth.saturating_sub(1), - b'"' | b'\'' | b'`' => { + b'"' | b'\'' => { // Skip to the closing quote (escapes skip a byte). i += 1; while let Some(&d) = b.get(i) { @@ -775,16 +934,29 @@ pub fn check_nesting(code: &str) -> Result<(), String> { while b.get(i).is_some_and(|d| *d != b'\n') { i += 1; } + continue; } b'/' if b.get(i + 1) == Some(&b'*') => { i += 2; while i < b.len() && !(b.get(i) == Some(&b'*') && b.get(i + 1) == Some(&b'/')) { i += 1; } - i += 1; + i += 2; + continue; } + b'/' if regex_may_start(b, prev, prev_at, word_after_dot) => skip_regex(b, &mut i), _ => {} } + if !c.is_ascii_whitespace() { + let word = |d: u8| d.is_ascii_alphanumeric() || d == b'_' || d == b'$'; + // A word starts where the byte before it is not a word byte (white space and comments + // end a word too): it is a property name when a `.` that is not part of a number + // comes right before it. + if word(c) && (i == 0 || !b.get(i - 1).is_some_and(|d| word(*d))) { + word_after_dot = prev == b'.' && dot_is_property(b, prev_at); + } + (prev, prev_at) = (c, i); + } i += 1; } if max > MAX_NESTING { @@ -858,6 +1030,44 @@ mod nesting_tests { assert!(check_nesting(&("(".repeat(MAX_NESTING) + &")".repeat(MAX_NESTING))).is_ok()); } + #[test] + fn regular_expression_literals_are_skipped_not_read_as_strings_or_templates() { + let deep = |n: usize| format!("{}1{}", "(".repeat(n), ")".repeat(n)); + // A backtick, a quote or a slash inside a class in a regular expression opens nothing, so + // the deep expression after it is still counted. + for re in ["/`/", "/\"/", "/'/", "/[/]/", "/\\//"] { + assert!(check_nesting(&format!("`x${{ {re}.test('x') ? {} : 0 }}`", deep(5000))).is_err(), "{re} in an interpolation"); + assert!(check_nesting(&format!("x = {re}.test(y) ? {} : 0", deep(5000))).is_err(), "{re}"); + assert!(check_nesting(&format!("x = {re}.test(y) ? {} : 0", deep(10))).is_ok(), "{re} shallow"); + } + // Division is not a regular expression: what follows it is still scanned. + assert!(check_nesting(&format!("a = 4 / 2; b = {}", deep(5000))).is_err()); + assert!(check_nesting(&format!("a = (4) / 2 / 3; b = {}", deep(5000))).is_err()); + // After a keyword that takes an expression a slash starts a regular expression. + assert!(check_nesting(&format!("typeof /`/ ; {}", deep(5000))).is_err()); + // A postfix update or a property named like a keyword is followed by a division, not a + // regular expression (the deep brackets after it must still be counted). + for head in ["i++ /", "i-- /", "obj.in /", "obj . in /", "obj . /* c */ in /", "obj.\n typeof /", "a1.in /", "1.5.in /"] { + assert!(check_nesting(&format!("x = {head} {} / 2", deep(5000))).is_err(), "{head}"); + } + // A keyword after a property name is still a keyword (`obj.in in /re/`), and the dot of a + // number does not make the word after it a property (`1. in /re/`): the regular expression + // is skipped, so the backtick inside it opens nothing and the brackets after it count. + for head in ["obj.in in", "1. in", "12. typeof"] { + assert!(check_nesting(&format!("x = {head} /`/; y = {}", deep(5000))).is_err(), "{head}"); + } + } + + #[test] + fn template_interpolations_are_checked_but_template_text_is_not() { + let deep = |n: usize| format!("`a${{{}1{}}}b`", "(".repeat(n), ")".repeat(n)); + assert!(check_nesting(&deep(5000)).is_err()); + assert!(check_nesting(&deep(10)).is_ok()); + // Brackets in the literal text, and a nested template, are fine; depth carries across `}`. + assert!(check_nesting(&format!("`{} ${{ `x${{1+(2)}}y` }} {}`", "(".repeat(500), "{".repeat(500))).is_ok()); + assert!(check_nesting(&format!("`${{1}}${{{}2{}}}`", "[".repeat(MAX_NESTING), "]".repeat(MAX_NESTING))).is_err()); + } + /// A canonical drive path and a lexical one compare once the `\\?\` prefix is gone; a UNC /// share and a Unix path are left as they are. #[test] diff --git a/crates/script/src/scriptfile.rs b/crates/script/src/scriptfile.rs index 2b6afcfbe..c3179711b 100644 --- a/crates/script/src/scriptfile.rs +++ b/crates/script/src/scriptfile.rs @@ -56,6 +56,10 @@ pub(crate) fn read(s: &Session, path: &Path) -> Result { if std::fs::metadata(path).is_ok_and(|m| m.len() > MAX_FILE_BYTES as u64) { return Err(too_big()); } + // A pipe or device reports no size and may never end: only regular files are read. + if std::fs::metadata(path).is_ok_and(|m| !m.is_file()) { + return Err(format!("Unable to read {}: not a regular file", path.display())); + } let bytes = s.services.read_file(&path.to_string_lossy()).map_err(|e| format!("Unable to read {}: {e}", path.display()))?; if bytes.len() > MAX_FILE_BYTES { return Err(too_big()); diff --git a/crates/script/src/scriptui.js b/crates/script/src/scriptui.js index ddfbc1f3e..9c5000962 100644 --- a/crates/script/src/scriptui.js +++ b/crates/script/src/scriptui.js @@ -750,7 +750,7 @@ function __uiMouseWanted(c, inherited) { function __uiMouseEvent(type, target, v) { var num = function (x, d) { x = Number(x); - return isFinite(x) ? x : d; + return Number.isFinite(x) ? x : d; }; var keys = { alt: !!v.altKey, control: !!v.ctrlKey, ctrl: !!v.ctrlKey, meta: !!v.metaKey, shift: !!v.shiftKey }; return { diff --git a/crates/script/src/socket.rs b/crates/script/src/socket.rs index c024fcad5..ff5d6d200 100644 --- a/crates/script/src/socket.rs +++ b/crates/script/src/socket.rs @@ -142,7 +142,7 @@ mod imp { /// Fill a stream's buffer until `done(buf)` or the timeout / end of stream. Fails only when /// the read deadline can't be set. - fn fill(s: &mut TcpStream, buf: &mut Vec, eof: &mut bool, limit: Duration, done: impl Fn(&[u8]) -> bool) -> std::io::Result<()> { + fn fill(s: &mut TcpStream, buf: &mut Vec, eof: &mut bool, limit: Duration, mut done: impl FnMut(&[u8]) -> bool) -> std::io::Result<()> { let end = Instant::now() + limit; let mut chunk = [0u8; 4096]; while !done(buf) && !*eof { @@ -322,7 +322,15 @@ mod imp { } } ("readln", Sock::Stream { s, buf, eof }) => { - if let Err(e) = fill(s, buf, eof, timeout(p), |b| b.contains(&b'\n')) { + // Only the bytes that arrived since the last look are searched: a peer + // sending no newline must not make every read rescan the whole buffer. + let mut seen = 0; + let newline = |b: &[u8]| { + let hit = b.get(seen..).is_some_and(|new| new.contains(&b'\n')); + seen = b.len(); + hit + }; + if let Err(e) = fill(s, buf, eof, timeout(p), newline) { return err(format!("cannot set the read timeout: {e}")); } let line: Vec = match buf.iter().position(|&c| c == b'\n') { diff --git a/crates/script/src/tasks.rs b/crates/script/src/tasks.rs index 563f3bbc0..8b5c0cd7a 100644 --- a/crates/script/src/tasks.rs +++ b/crates/script/src/tasks.rs @@ -208,7 +208,9 @@ pub(crate) fn run_tasks_at(session: &mut Session, now: Instant) -> J { if !running { #[cfg(not(target_arch = "wasm32"))] crate::ui::tick_threaded(session, now, &mut report); - crate::ui::tick_inline(session, now, &mut report); + if session.script_ui.render_host.is_none() { + crate::ui::tick_inline(session, now, &mut report); + } crate::ui::prune_task_hosts(session); } let errors: Vec = report.errors.iter().map(|e| json!({"message": e.message, "line": e.line, "column": e.column, "file": e.file})).collect(); diff --git a/crates/script/src/tests.rs b/crates/script/src/tests.rs index 9e934b555..25933d165 100644 --- a/crates/script/src/tests.rs +++ b/crates/script/src/tests.rs @@ -10,11 +10,15 @@ use serde_json::{Value, json}; use crate::{Outcome, run_code}; +mod ae_layer_order; mod callable; mod ease_presets; mod evalfile; mod include; mod panels; +mod paragraph_justification; +#[cfg(not(target_arch = "wasm32"))] +mod render_wait; #[cfg(not(windows))] mod services; mod startup; @@ -698,6 +702,56 @@ fn save_frame_to_png_writes_behind_the_file_gate() { let _ = std::fs::remove_dir_all(&dir); } +/// A script that runs the export commands itself (`app.run`, `__exec`) meets the same file-write +/// preference as the wrappers; with the preference off nothing is created or replaced. +#[test] +fn export_commands_run_directly_by_a_script_respect_the_file_gate() { + let dir = std::env::temp_dir().join(format!("ec-direct-export-{}", std::process::id())); + // A leftover dir from an interrupted run (same pid) must not feed stale absence checks. + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let png = dir.join("frame.png"); + let tpl = dir.join("card.ectemplate"); + let kept = dir.join("kept.png"); + std::fs::write(&kept, b"original").unwrap(); + let mut s = session(); + s.execute("comp.new", json!({"name": "Card", "width": 64, "height": 64, "duration": 1})).unwrap(); + s.execute("layer.newText", json!({"text": "Hi"})).unwrap(); + ok(&mut s, "var c = app.project.activeItem; c.layer(1).property('ADBE Transform Group').property('ADBE Opacity').addToMotionGraphicsTemplate(c);"); + let q = |p: &std::path::Path| serde_json::to_string(&p.to_string_lossy()).unwrap(); + let comp = s.active_comp_id().unwrap().0; + for (code, target) in [ + (format!("app.run('comp.saveFrameAs', {{comp: {comp}, path: {}}})", q(&png)), &png), + (format!("__exec('comp.saveFrameAs', JSON.stringify({{comp: {comp}, path: {}}}))", q(&png)), &png), + (format!("app.run('essential.exportTemplate', {{comp: {comp}, path: {}}})", q(&tpl)), &tpl), + (format!("app.run('comp.saveFrameAs', {{comp: {comp}, path: {}}})", q(&kept)), &kept), + ] { + let o = run_code(&mut s, &code, "direct.jsx"); + let e = o.error.unwrap_or_else(|| panic!("`{code}` was not refused")); + assert!(e.message.contains("Allow Scripts to Write Files"), "{e:?}"); + if target == &kept { + assert_eq!(std::fs::read(&kept).unwrap(), b"original"); + } else { + assert!(!target.exists(), "{} was written", target.display()); + } + } + // The wrappers check the gate without touching the file: an existing one is left as it was. + let o = run_code(&mut s, &format!("app.project.activeItem.saveFrameToPng(0, new File({}))", q(&kept)), "wrap.jsx"); + assert!(o.error.is_some()); + assert_eq!(std::fs::read(&kept).unwrap(), b"original"); + let o = run_code(&mut s, &format!("app.project.activeItem.exportAsMotionGraphicsTemplate(true, {})", q(&tpl)), "wrap.jsx"); + assert!(o.error.is_some()); + assert!(!tpl.exists()); + // The user, from the UI, is not a script: the same command writes. + s.execute("comp.saveFrameAs", json!({"comp": comp, "path": png.to_string_lossy()})).unwrap(); + assert!(png.exists()); + // With the preference on, the script's direct call writes too. + s.prefs.scripting.allow_scripts_write_files = true; + ok(&mut s, &format!("app.run('essential.exportTemplate', {{comp: {comp}, path: {}}})", q(&tpl))); + assert!(std::fs::metadata(&tpl).unwrap().len() > 100); + let _ = std::fs::remove_dir_all(&dir); +} + /// #1246: with Warn User When Executing Files on, a script that asserts `confirmed` itself still /// gets the question; only the user's answer opens the file. #[test] @@ -1947,3 +2001,77 @@ fn setting_comp_width_and_height_leaves_the_layers_where_they_are() { let (_, c) = comp_named(&s, "w"); assert_eq!(layer(c, "s").props.prop("transform/position").unwrap().value, KV::Vec3([100.0, 100.0, 0.0])); } + +#[test] +fn a_hashbang_line_does_not_hide_deep_nesting_from_eval() { + // Boa reads a leading `#!` line of eval source as a comment, so the backtick on it opens no + // template: the brackets after it are counted and refused. + let mut s = session(); + let code = "var deep = new Array(5000).join('(') + '1' + new Array(5000).join(')'); var r; \ + try { eval('#! `\\n' + deep); r = 'ran'; } catch (e) { r = 'refused'; } r"; + assert_eq!(ok(&mut s, code).result.as_str(), Some("refused")); + assert!(crate::runtime::check_nesting(&format!("#! `\n{}1{}", "(".repeat(5000), ")".repeat(5000))).is_err()); + assert!(crate::runtime::check_nesting("#! `\n(1)").is_ok()); +} + +#[test] +fn code_compiled_at_run_time_passes_the_nesting_check() { + // `eval` and `new Function` of a string nested past the limit are refused with a catchable + // error instead of overflowing the parser's stack; shallow code still compiles. + let mut s = session(); + for call in ["eval(deep)", "new Function(deep)", "Function('a', deep)"] { + let code = format!( + "var deep = new Array(5000).join('(') + '1' + new Array(5000).join(')'); var r; try {{ {call}; r = 'ran'; }} catch (e) {{ r = 'refused'; }} r" + ); + assert_eq!(ok(&mut s, &code).result.as_str(), Some("refused"), "{call}"); + } + assert_eq!(ok(&mut s, "eval('1 + (2 * 3)') + new Function('return 4')()").result.as_f64(), Some(11.0)); +} + +#[test] +fn file_masks_match_without_backtracking() { + let mut s = session(); + let r = ok( + &mut s, + "[__globMatch('*.JSX', 'a.jsx'), __globMatch('a?c*', 'abcdef'), __globMatch('*a*b', 'xaxb'), __globMatch('*.png', 'a.jsx'), __globMatch('*a*a*a*a*a*a*a*a*a*a*a*b', new Array(60).join('a'))].join()", + ); + assert_eq!(r.result.as_str(), Some("true,true,true,false,false")); + // `?` still matches one character that lowercases to two. + let r = ok(&mut s, "[__globMatch('?.txt', '\\u0130.txt'), __globMatch('\\u0130*', '\\u0130x')].join()"); + assert_eq!(r.result.as_str(), Some("true,true")); + // Greek sigma matches in every case form, as the case-insensitive regular expression did. + let r = ok( + &mut s, + "[__globMatch('\\u03bf\\u03c2.txt', '\\u039f\\u03a3.txt'), __globMatch('\\u03a3*', '\\u03c2x'), __globMatch('k', '\\u212a'), __globMatch('a', 'b')].join()", + ); + assert_eq!(r.result.as_str(), Some("true,true,false,false")); +} + +/// #3395: setValue on a string effect parameter stores the text, or a File's path, as `prop.set` +/// does. Before, every string fell through to the number check ("value must be a number"). +/// Hidden string parameters (Mesh Warp's Distortion Mesh) stay refused, keys included. +#[test] +fn set_value_stores_a_string_parameter() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var c = app.project.items.addComp("Str", 64, 36, 1, 1, 24); + var l = c.layers.addSolid([1, 0, 0], "s", 64, 36, 1); + var fx = l.property("ADBE Effect Parade"); + var lut = fx.addProperty("Apply Color LUT").property("LUT"); + lut.setValue("/luts/look.cube"); + var fromString = lut.value; + lut.setValue(new File("/luts/other.cube")); + var mesh = fx.addProperty("Mesh Warp").property("Distortion Mesh"); + function attempt(f) { try { f(); return "no error"; } catch (e) { return e.message; } } + var hidden = attempt(function () { mesh.setValue("x"); }); + var keyed = attempt(function () { mesh.setValueAtTime(0, "x"); }); + [lut.propertyValueType === PropertyValueType.CUSTOM_VALUE, fromString, lut.value, hidden, keyed, mesh.numKeys] + "#, + ); + let refused = "\"Distortion Mesh\" can not be set"; + assert_eq!(o.result, json!([true, "/luts/look.cube", "/luts/other.cube", refused, refused, 0])); + let (_, c) = comp_named(&s, "Str"); + assert_eq!(layer(c, "s").props.prop("effects/#1/lut").unwrap().value, KV::Str("/luts/other.cube".into())); +} diff --git a/crates/script/src/tests/ae_layer_order.rs b/crates/script/src/tests/ae_layer_order.rs new file mode 100644 index 000000000..869ce348a --- /dev/null +++ b/crates/script/src/tests/ae_layer_order.rs @@ -0,0 +1,121 @@ +//! `layer.property(n)` and the layer's `numProperties` use After Effects' top-level numbers +//! (`project::ae_order`): Marker is 1 and unused slots keep their place as empty groups, so a loop +//! over `numProperties` meets every slot and never a null. + +use std::sync::Arc; + +use effectcraft_engine::Session; +use serde_json::json; + +use crate::{Outcome, run_code}; + +fn session() -> Session { + let mut s = Session { expr: Some(Arc::new(effectcraft_expr::Expressions)), expr_check: Some(effectcraft_expr::check_syntax), ..Default::default() }; + crate::install(&mut s); + s +} + +fn ok(s: &mut Session, code: &str) -> Outcome { + let o = run_code(s, code, "test.jsx"); + assert!(o.error.is_none(), "script failed: {:?}\noutput: {:?}", o.error, o.output); + o +} + +#[test] +fn layer_numbers_follow_after_effects() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var c = app.project.items.addComp("M", 200, 200, 1, 2, 30); + var solid = c.layers.addSolid([1, 1, 1], "S", 200, 200, 1); + solid.Effects.addProperty("ADBE Slider Control"); + var shape = c.layers.addShape(); + var text = c.layers.addText("t"); + var cam = c.layers.addCamera("Cam", [100, 100]); + function names(l) { var o = []; for (var i = 1; i <= l.numProperties; i++) o.push(l.property(i).matchName); return o; } + [names(solid), names(shape), names(text), names(cam), + solid.property(5)(1)(1).value, solid.property(5).propertyIndex, solid.property("Effects").propertyIndex, + solid.property(1) === solid.marker || solid.property(1).matchName, solid.property(16)] + "#, + ); + let av = [ + "ADBE Marker", + "ADBE Time Remapping", + "ADBE MTrackers", + "ADBE Mask Parade", + "ADBE Effect Parade", + "ADBE Transform Group", + "ADBE Layer Styles", + "ADBE Plane Options Group", + "ADBE Extrsn Options Group", + "ADBE Material Options Group", + "ADBE Audio Group", + "ADBE Data Group", + "ADBE Layer Overrides", + "ADBE Layer Sets", + "ADBE Source Options Group", + ]; + let vector = |first: &str| { + json!([ + "ADBE Marker", + first, + "ADBE Mask Parade", + "ADBE Effect Parade", + "ADBE Transform Group", + "ADBE Layer Styles", + "ADBE Extrsn Options Group", + "ADBE Material Options Group", + "ADBE Audio Group", + "ADBE Layer Sets" + ]) + }; + let r = o.result.as_array().unwrap(); + assert_eq!(r[0], json!(av), "solid"); + assert_eq!(r[1], vector("ADBE Root Vectors Group"), "shape"); + assert_eq!(r[2], vector("ADBE Text Properties"), "text"); + assert_eq!(r[3], json!(["ADBE Marker", "ADBE Transform Group", "ADBE Camera Options Group"]), "camera"); + assert_eq!(r[4], json!(0), "the slider's value through property(5)"); + assert_eq!(r[5], json!(5)); + assert_eq!(r[6], json!(5), "propertyIndex by name is After Effects' too"); + assert_eq!(r[7], json!("ADBE Marker")); + assert_eq!(r[8], json!(null), "past the last slot"); +} + +#[test] +fn walking_every_property_meets_no_null() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var c = app.project.items.addComp("M", 200, 200, 1, 2, 30); + var l = c.layers.addShape(); + var n = 0; + function walk(g) { + for (var i = 1; i <= g.numProperties; i++) { + var p = g.property(i); + if (p === null) throw new Error("null at " + i + " of " + g.name); + if (p.propertyType === PropertyType.PROPERTY) n++; else walk(p); + } + } + walk(l); + n > 0 + "#, + ); + assert_eq!(o.result, json!(true)); +} + +#[test] +fn an_unused_slot_is_the_same_by_number_and_match_name_and_callable() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var c = app.project.items.addComp("M", 200, 200, 1, 2, 30); + var l = c.layers.addSolid([1, 1, 1], "S", 200, 200, 1); + var byName = l.property("ADBE Layer Styles"), byNumber = l.property(7); + [byName !== null && byName.matchName, byName !== null && byName.numProperties, byNumber.propertyIndex, l.property(7)(1), l(12).name] + "#, + ); + assert_eq!(o.result, json!(["ADBE Layer Styles", 0, 7, null, "Data"])); +} diff --git a/crates/script/src/tests/paragraph_justification.rs b/crates/script/src/tests/paragraph_justification.rs new file mode 100644 index 000000000..3b618170c --- /dev/null +++ b/crates/script/src/tests/paragraph_justification.rs @@ -0,0 +1,48 @@ +//! `ParagraphJustification` has After Effects' numbers (26.3): MULTIPLE_JUSTIFICATIONS 7412, +//! LEFT 7413, RIGHT 7414, CENTER 7415, then the justified variants with the last line left (7416), +//! right (7417), centred (7418) and full (7419). A script that stores or compares the numbers gets +//! the same paragraph in both applications. + +use serde_json::json; + +use super::{comp_named, ok, session}; +use effectcraft_engine::keyframe::{Justify, Value as KV}; + +#[test] +fn the_numbers_are_after_effects() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var P = ParagraphJustification; + [P.MULTIPLE_JUSTIFICATIONS, P.LEFT_JUSTIFY, P.RIGHT_JUSTIFY, P.CENTER_JUSTIFY, P.FULL_JUSTIFY_LASTLINE_LEFT, + P.FULL_JUSTIFY_LASTLINE_RIGHT, P.FULL_JUSTIFY_LASTLINE_CENTER, P.FULL_JUSTIFY_LASTLINE_FULL] + "#, + ); + assert_eq!(o.result, json!([7412, 7413, 7414, 7415, 7416, 7417, 7418, 7419])); +} + +#[test] +fn each_number_sets_and_reads_its_paragraph() { + let mut s = session(); + let o = ok( + &mut s, + r#" + var c = app.project.items.addComp("J", 200, 100, 1, 2, 30); + var t = c.layers.addText("ab"); + var st = t.property("ADBE Text Properties").property("ADBE Text Document"); + var back = []; + for (var n = 7413; n <= 7419; n++) { + var d = st.value; d.justification = n; st.setValue(d); + back.push(st.value.justification); + } + var d = st.value; d.justification = 7414; st.setValue(d); + back + "#, + ); + assert_eq!(o.result, json!([7413, 7414, 7415, 7416, 7417, 7418, 7419])); + // 7414 is right-aligned, as in After Effects. + let (_, c) = comp_named(&s, "J"); + let KV::Text(d) = &c.layers[0].props.prop("text/sourceText").unwrap().value else { panic!("not text") }; + assert_eq!(d.justify, Justify::Right); +} diff --git a/crates/script/src/tests/render_wait.rs b/crates/script/src/tests/render_wait.rs new file mode 100644 index 000000000..591bd211a --- /dev/null +++ b/crates/script/src/tests/render_wait.rs @@ -0,0 +1,243 @@ +//! #3289: a native frontend may service the event loop while a script waits for rendering. + +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::time::{Duration, Instant}; + +use effectcraft_engine::project::render_queue::OutputFormat; +use effectcraft_engine::{ExportJob, ExportResult, Exporter, Session}; +use serde_json::{Value, json}; + +/// A slow exporter without expensive frames. A bounded wait makes the old blocking path +/// fail the assertions instead of hanging the test process. +struct Gate { + entered: AtomicUsize, + permits: AtomicUsize, + failed: bool, +} + +impl Exporter for Gate { + fn formats(&self) -> Vec { + vec![OutputFormat::H264] + } + fn export(&self, job: &ExportJob, progress: &mut dyn FnMut(u64, u64) -> bool) -> Result { + let call = self.entered.fetch_add(1, Ordering::SeqCst) + 1; + let end = Instant::now() + Duration::from_secs(2); + while self.permits.load(Ordering::SeqCst) < call { + if !progress(0, 1) { + return Err(effectcraft_engine::render_queue::CANCELLED.into()); + } + if Instant::now() > end { + return Err("test exporter timed out waiting for the frontend".into()); + } + std::thread::sleep(Duration::from_millis(1)); + } + if self.failed { Err("test encoder failure".into()) } else { Ok(ExportResult { path: job.path.into(), frames: 1, ..Default::default() }) } + } +} + +fn session(permits: usize, failed: bool) -> (Session, Arc) { + let gate = Arc::new(Gate { entered: AtomicUsize::new(0), permits: AtomicUsize::new(permits), failed }); + let mut s = super::session(); + s.exporter = Some(gate.clone()); + s.execute("comp.new", json!({"name": "Render", "width": 16, "height": 16, "duration": 1})).unwrap(); + let path = std::env::temp_dir().join(format!("ec-script-render-{}.mp4", std::process::id())); + s.execute("renderQueue.add", json!({"output": path.to_string_lossy()})).unwrap(); + s.prefs.auto_save.enabled = false; + (s, gate) +} + +fn finish(s: &mut Session) -> Value { + let end = Instant::now() + Duration::from_secs(3); + loop { + if let Some(result) = crate::ui::poll_render_wait(s) { + return result; + } + assert!(Instant::now() < end, "script did not resume"); + std::thread::sleep(Duration::from_millis(1)); + } +} + +#[test] +fn render_yields_without_finishing_the_script_or_its_undo_group() { + let (mut s, gate) = session(0, false); + s.script_ui.yield_renders = true; + let before = s.history.undo.len(); + let out = crate::run_code( + &mut s, + "app.beginUndoGroup('Render transaction'); app.project.activeItem.layers.addNull().name = 'Before'; $.writeln('before'); app.project.renderQueue.render(); app.project.activeItem.layers.addNull().name = 'After'; $.writeln('after'); app.endUndoGroup(); [42, app.project.renderQueue.rendering];", + "render.jsx", + ); + assert!(out.error.is_none(), "{out:?}"); + assert!(out.render_host.is_some(), "blocking render returned instead of yielding: {out:?}"); + assert!(!out.waiting, "a render suspension is not a modal dialog"); + assert!(out.output.is_empty(), "output belongs to the eventual reply"); + assert_eq!(s.active_comp().unwrap().layers.len(), 1, "the continuation has not run"); + assert!(crate::ui::poll_render_wait(&mut s).is_none()); + assert!(!s.undo(), "undo cannot discard the suspended script's live edits"); + assert!(s.execute("engine.batch", json!({"steps": [{"command": "layer.newNull"}]})).is_err()); + assert!(s.execute("jobs.list", json!({})).is_err(), "status polling must not apply unrelated jobs"); + // The frontend resets the invocation-only opt-in after script.run returns. + s.script_ui.yield_renders = false; + gate.permits.store(1, Ordering::SeqCst); + let done = finish(&mut s); + assert_eq!(done["ok"], true, "{done}"); + assert_eq!(done["result"], json!([42, false])); + assert_eq!(done["output"], "before\nafter"); + assert_eq!(s.project.render_queue[0].status.label(), "Done"); + assert_eq!(s.history.undo.len(), before + 1, "one undo group spans the render"); + assert_eq!(s.history.undo.last().unwrap().0, "Render transaction"); + assert!(s.script_ui.render_host.is_none()); + s.undo(); + assert!(s.active_comp().unwrap().layers.is_empty()); +} + +#[test] +fn a_second_render_keeps_the_same_script_pending() { + let (mut s, gate) = session(0, false); + s.script_ui.yield_renders = true; + let out = crate::run_code( + &mut s, + "app.project.renderQueue.render(); app.project.renderQueue.item(1).render = true; app.project.renderQueue.render(); 17;", + "twice.jsx", + ); + let host = out.render_host.unwrap(); + s.script_ui.yield_renders = false; + gate.permits.store(1, Ordering::SeqCst); + let end = Instant::now() + Duration::from_secs(3); + while gate.entered.load(Ordering::SeqCst) < 2 { + assert!(crate::ui::poll_render_wait(&mut s).is_none(), "no premature reply after the first render"); + assert!(Instant::now() < end); + std::thread::sleep(Duration::from_millis(1)); + } + assert_eq!(s.script_ui.render_host, Some(host)); + gate.permits.store(2, Ordering::SeqCst); + assert_eq!(finish(&mut s)["result"], 17); +} + +#[test] +fn cancellation_and_encoder_failure_resume_with_terminal_statuses() { + for failed in [false, true] { + let (mut s, gate) = session(0, failed); + s.script_ui.yield_renders = true; + let out = crate::run_code(&mut s, "app.project.renderQueue.render(); [9, app.project.renderQueue.rendering];", "stopped.jsx"); + assert!(out.render_host.is_some()); + if failed { + gate.permits.store(1, Ordering::SeqCst); + } else { + s.stop_render(); + } + let done = finish(&mut s); + assert_eq!(done["ok"], true, "the synchronous command reports status, not a thrown script error: {done}"); + assert_eq!(done["result"], json!([9, false])); + assert_ne!(s.project.render_queue[0].status.label(), "Done"); + } +} + +#[test] +fn headless_scripts_still_wait_and_start_errors_do_not_suspend() { + let (mut s, _) = session(1, false); + let out = crate::run_code(&mut s, "app.project.renderQueue.render(); [7, app.project.renderQueue.rendering];", "headless.jsx"); + assert!(out.error.is_none(), "{out:?}"); + assert_eq!(out.result, json!([7, false])); + assert!(out.render_host.is_none()); + assert_eq!(s.project.render_queue[0].status.label(), "Done"); + s.script_ui.yield_renders = true; + let out = crate::run_code(&mut s, "app.project.renderQueue.render();", "empty.jsx"); + assert!(out.error.is_some()); + assert!(out.render_host.is_none()); + assert!(s.script_ui.render_host.is_none()); +} + +#[test] +fn retained_palette_callbacks_can_yield_after_the_original_control_invocation() { + let (mut s, gate) = session(0, false); + s.script_ui.yield_renders = true; + let out = crate::run_code( + &mut s, + "var w = new Window('palette', 'Render'); var b = w.add('button', undefined, 'Go', {name:'go'}); b.onClick = function () { app.project.renderQueue.render(); $.writeln('resumed'); }; w.show();", + "palette-render.jsx", + ); + assert!(out.error.is_none(), "{out:?}"); + s.script_ui.yield_renders = false; + let result = s.execute("scriptui.click", json!({"widget": "go"})).unwrap(); + assert!(result["renderHost"].is_number(), "{result}"); + gate.permits.store(1, Ordering::SeqCst); + assert_eq!(finish(&mut s)["output"], "resumed"); + let window = s.script_ui.windows[0].id; + s.execute("scriptui.close", json!({"window": window})).unwrap(); +} + +#[test] +fn post_render_actions_complete_before_the_script_resumes() { + use effectcraft_engine::project::{Footage, FootageKind}; + struct Probe; + impl effectcraft_engine::Importer for Probe { + fn probe(&self, path: &str) -> Result { + Ok(Footage { path: path.into(), kind: FootageKind::Still, width: 16, height: 16, has_video: true, ..Default::default() }) + } + } + for action in ["import", "setProxy"] { + let (mut s, gate) = session(0, false); + s.importer = Some(Arc::new(Probe)); + let id = s.project.render_queue[0].id; + s.execute("renderQueue.setOutputModule", json!({"item": id, "postRenderAction": action})).unwrap(); + let before = s.project.items.len(); + let comp = s.active_comp_id().unwrap(); + s.script_ui.yield_renders = true; + let out = crate::run_code(&mut s, "app.project.renderQueue.render(); 29;", "post-render.jsx"); + assert!(out.render_host.is_some(), "{out:?}"); + gate.permits.store(1, Ordering::SeqCst); + // A query must not consume updates while the edit guard is still active. + let end = Instant::now() + Duration::from_secs(3); + while s.is_rendering() { + assert!(Instant::now() < end); + std::thread::sleep(Duration::from_millis(1)); + } + s.execute("renderQueue.list", json!({})).unwrap(); + assert_eq!(s.project.items.len(), before); + assert_eq!(finish(&mut s)["result"], 29); + if action == "import" { + assert_eq!(s.project.items.len(), before + 1); + } else { + assert!(s.project.item(comp).unwrap().proxy.is_some()); + } + } +} + +#[test] +fn deferred_errors_keep_their_included_source_location() { + let (mut s, gate) = session(0, false); + let dir = std::env::temp_dir().join(format!("ec-render-include-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("render.jsx"), "app.project.renderQueue.render();\nmissingAfterRender();").unwrap(); + std::fs::write(dir.join("main.jsx"), "#include 'render.jsx'").unwrap(); + s.script_ui.yield_renders = true; + let out = crate::run_code(&mut s, "#include 'render.jsx'", &dir.join("main.jsx").to_string_lossy()); + assert!(out.render_host.is_some(), "{out:?}"); + gate.permits.store(1, Ordering::SeqCst); + let done = finish(&mut s); + assert_eq!(done["ok"], false); + assert_eq!(done["error"]["line"], 2); + assert!(done["error"]["file"].as_str().unwrap().ends_with("render.jsx"), "{done}"); + std::fs::remove_dir_all(dir).unwrap(); +} + +#[test] +fn included_error_mapping_survives_a_modal_yield_after_rendering() { + let (mut s, gate) = session(0, false); + let dir = std::env::temp_dir().join(format!("ec-render-modal-include-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("render.jsx"), "app.project.renderQueue.render();\nvar d = new Window('dialog', 'Continue'); d.add('button', undefined, 'OK', {name:'ok'}); d.show();\nmissingAfterModal();").unwrap(); + std::fs::write(dir.join("main.jsx"), "#include 'render.jsx'").unwrap(); + s.script_ui.yield_renders = true; + let out = crate::run_code(&mut s, "#include 'render.jsx'", &dir.join("main.jsx").to_string_lossy()); + assert!(out.render_host.is_some(), "{out:?}"); + gate.permits.store(1, Ordering::SeqCst); + assert_eq!(finish(&mut s)["waiting"], true); + let done = s.execute("scriptui.click", json!({"widget": "ok"})).unwrap(); + assert_eq!(done["ok"], false); + assert_eq!(done["error"]["line"], 3); + assert!(done["error"]["file"].as_str().unwrap().ends_with("render.jsx"), "{done}"); + std::fs::remove_dir_all(dir).unwrap(); +} diff --git a/crates/script/src/ui.rs b/crates/script/src/ui.rs index ab6f69846..ecf4cd28b 100644 --- a/crates/script/src/ui.rs +++ b/crates/script/src/ui.rs @@ -3,7 +3,8 @@ //! //! * **Script files** (native builds) run on a *script-host thread*. The caller's session moves //! to that thread while JavaScript runs and comes back whenever the script stops: at the end, -//! or while a modal dialog waits in `show()`. Each [`ScriptUiEvent`] moves the session there +//! while a modal dialog waits in `show()`, or while an opted-in frontend waits for rendering. +//! Each [`ScriptUiEvent`] moves the session there //! again, runs the handler, and brings it back with the windows' new state. The host ends //! when its last window closes. Only one side holds the session at a time, so nothing runs //! concurrently and every edit is still an ordinary undoable engine command. @@ -137,6 +138,8 @@ pub(crate) enum ToHost { /// script-host threads). #[cfg_attr(target_arch = "wasm32", allow(dead_code))] Tick(Session, Instant), + /// Resume the suspended native render call, never a callback or scheduled task. + ResumeRender(Session), } impl ToHost { @@ -145,7 +148,7 @@ impl ToHost { match self { #[cfg(not(target_arch = "wasm32"))] ToHost::Start(s) => s, - ToHost::Event(s, _) | ToHost::Tick(s, _) => s, + ToHost::Event(s, _) | ToHost::Tick(s, _) | ToHost::ResumeRender(s) => s, } } } @@ -159,6 +162,7 @@ pub(crate) struct FromHost { done: bool, /// When its next scheduled task is due. next_due: Option, + render_wait: bool, } /// The host thread's end of the hand-off. @@ -177,6 +181,8 @@ struct HostEnd { /// [`effectcraft_engine::scriptui::ScriptUi::project_epoch`] when the host last ran. #[cfg_attr(target_arch = "wasm32", allow(dead_code))] epoch: u64, + /// Retained across render yields so a later error still names its included source file. + source_map: Option, } fn hosts() -> &'static std::sync::Mutex> { @@ -195,6 +201,7 @@ enum Wake { Event(ScriptUiEvent), #[cfg_attr(target_arch = "wasm32", allow(dead_code))] Tick(Instant), + Render, } /// On the host thread: hand the session (with the windows' state and what the script printed) @@ -206,18 +213,20 @@ fn yield_wait(outcome: crate::Outcome, windows: Vec, done: bool) - match yield_wake(out, windows.clone(), done, None)? { Wake::Event(ev) => return Some(ev), Wake::Tick(_) => out = crate::Outcome { waiting: true, ..Default::default() }, + Wake::Render => return None, } } } /// [`yield_wait`] that also wakes for the scheduled tasks due at `next_due`. fn yield_wake(outcome: crate::Outcome, windows: Vec, done: bool, next_due: Option) -> Option { + let render_wait = outcome.render_host.is_some(); let (session, link, name) = ACTIVE.with(|a| { let mut a = a.borrow_mut(); let a = a.as_mut()?; Some((std::mem::take(&mut a.session), a.link.take()?, a.name.clone())) })?; - if link.tx.send(FromHost { session, outcome, windows, name, done, next_due }).is_err() { + if link.tx.send(FromHost { session, outcome, windows, name, done, next_due, render_wait }).is_err() { return None; } if done { @@ -226,6 +235,7 @@ fn yield_wake(outcome: crate::Outcome, windows: Vec, done: bool, n let (s, wake) = match link.rx.recv() { Ok(ToHost::Event(s, ev)) => (s, Wake::Event(ev)), Ok(ToHost::Tick(s, now)) => (s, Wake::Tick(now)), + Ok(ToHost::ResumeRender(s)) => (s, Wake::Render), _ => return None, }; ACTIVE.with(|a| { @@ -269,8 +279,9 @@ fn host_main(host: u32, name: String, code: String, link: Link) { } Some(Wake::Tick(now)) => { let (ran, error) = crate::tasks::run_due(&mut ctx, host, now); - out = crate::Outcome { result: json!(ran), output: settle(), error, waiting: false }; + out = crate::Outcome { result: json!(ran), output: settle(), error, ..Default::default() }; } + Some(Wake::Render) => break, None => break, } } @@ -283,15 +294,30 @@ fn host_main(host: u32, name: String, code: String, link: Link) { { let message = if body.is_err() { "the script engine stopped unexpectedly" } else { "the script ended" }; let out = crate::Outcome { error: Some(ScriptError { message: message.into(), file, ..Default::default() }), ..Default::default() }; - let _ = link.tx.send(FromHost { session: std::mem::take(&mut a.session), outcome: out, windows: vec![], name, done: true, next_due: None }); + let _ = link.tx.send(FromHost { + session: std::mem::take(&mut a.session), + outcome: out, + windows: vec![], + name, + done: true, + next_due: None, + render_wait: false, + }); } } /// Wait for the host to hand the session back; keep its link while it has windows open. fn receive(session: &mut Session, host: u32, end: HostEnd) -> crate::Outcome { match end.rx.recv() { - Ok(FromHost { session: s, outcome, windows, name, done, next_due }) => { + Ok(FromHost { session: s, mut outcome, windows, name, done, next_due, render_wait }) => { *session = s; + session.script_ui.render_host = render_wait.then_some(host); + if !render_wait + && let Some(map) = &end.source_map + && let Some(error) = &mut outcome.error + { + map.locate(error); + } publish(session, host, &name, windows); if done { session.script_ui.windows.retain(|w| w.host != host); @@ -324,7 +350,61 @@ pub(crate) fn run_threaded(session: &mut Session, req: &effectcraft_engine::Scri *session = msg.into_session(); return crate::Outcome { error: Some(ScriptError { message: "the script host did not start".into(), ..Default::default() }), ..Default::default() }; } - receive(session, host, HostEnd { tx: to_tx, rx: from_rx, next_due: None, epoch: 0 }) + receive(session, host, HostEnd { tx: to_tx, rx: from_rx, next_due: None, epoch: 0, source_map: None }) +} + +/// Suspend only the JavaScript stack, not the window's event loop (#3289). Keep the output +/// and open undo groups in `Active`: settling here would split the script's undo transaction. +pub(crate) fn wait_for_render(ctx: &mut Context, mut result: J) -> Result { + let host = with_active(|a| Ok(a.host))?; + let windows = snapshot(ctx); + let out = crate::Outcome { render_host: Some(host), ..Default::default() }; + if !matches!(yield_wake(out, windows, false, None), Some(Wake::Render)) { + return Err("the frontend stopped waiting for the script's render".into()); + } + with_active(|a| { + let list = a.session.execute_for_script("renderQueue.list", json!({})).map_err(|e| e.to_string())?; + let ids: Vec = result["items"].as_array().into_iter().flatten().map(|i| i["id"].clone()).collect(); + result["items"] = json!(list["items"].as_array().into_iter().flatten().filter(|i| ids.contains(&i["id"])).collect::>()); + result["rendering"] = list["rendering"].clone(); + Ok(result) + }) +} + +pub(crate) fn keep_render_source_map(host: u32, map: crate::include::Expanded) { + if let Ok(mut h) = hosts().lock() + && let Some(end) = h.get_mut(&host) + { + end.source_map = Some(map); + } +} + +/// Called by the native frontend while its normal editing/callback tick is paused. Polling +/// applies terminal statuses and post-render actions before the suspended JS call returns. +pub(crate) fn poll_render_wait(session: &mut Session) -> Option { + let host = session.script_ui.render_host?; + // Only this completion boundary may apply the render's own import/proxy undo steps. + session.script_ui.render_host = None; + session.poll_render(); + if session.is_rendering() { + session.script_ui.render_host = Some(host); + return None; + } + session.script_ui.render_host = None; + let fail = |message: &str| crate::Outcome { error: Some(ScriptError { message: message.into(), ..Default::default() }), ..Default::default() }.to_json(); + let Some(end) = hosts().lock().ok().and_then(|mut h| h.remove(&host)) else { + return Some(fail("the script host stopped while rendering")); + }; + if end.epoch != session.script_ui.project_epoch { + return Some(fail("the project changed while the script was rendering")); + } + if let Err(std::sync::mpsc::SendError(msg)) = end.tx.send(ToHost::ResumeRender(std::mem::take(session))) { + *session = msg.into_session(); + return Some(fail("the script host stopped while rendering")); + } + let out = receive(session, host, end); + // A second render in this invocation keeps the original control reply pending. + out.render_host.is_none().then(|| out.to_json()) } /// Record the windows a host published (named after the script that made them). @@ -429,6 +509,9 @@ pub(crate) fn tick_threaded(session: &mut Session, now: Instant, report: &mut cr } drop(stale); for host in due { + if session.script_ui.render_host.is_some() { + break; + } let Some(end) = hosts().lock().ok().and_then(|mut h| h.remove(&host)) else { continue }; if let Err(std::sync::mpsc::SendError(msg)) = end.tx.send(ToHost::Tick(std::mem::take(session), now)) { *session = msg.into_session(); @@ -490,7 +573,7 @@ fn tick_inline_host(session: &mut Session, host: u32, name: &str, now: Instant) drop(guard); crate::tasks::set_epoch(host, session.script_ui.project_epoch); publish(session, host, name, windows); - crate::Outcome { result: json!(ran), output, error, waiting: false } + crate::Outcome { result: json!(ran), output, error, ..Default::default() } } /// Forget the session's task hosts that have ended (inline ones: that have no tasks left). diff --git a/crates/segment/src/mobilesam.rs b/crates/segment/src/mobilesam.rs index 6599b018a..abb12fb0f 100644 --- a/crates/segment/src/mobilesam.rs +++ b/crates/segment/src/mobilesam.rs @@ -19,6 +19,9 @@ const IMG: usize = 1024; const EMB: usize = 64; const DIM: usize = 256; const LOW: usize = 256; +/// Most prompt points the decoder takes (each is an attention token, and self-attention is +/// quadratic in them); SAM is used with a handful. More are thinned evenly, keeping both ends. +const MAX_POINTS: usize = 256; const MEAN: [f32; 3] = [123.675, 116.28, 103.53]; const STD: [f32; 3] = [58.395, 57.12, 57.375]; @@ -609,7 +612,7 @@ impl MobileSam { /// Foreground probability (`w×h`) for a prompt on an embedded image, and the predicted IoU. pub fn predict(&self, e: &Embedding, prompt: &Prompt) -> (Vec, f32) { let s = e.scale; - let points: Vec<([f32; 2], bool)> = prompt.points.iter().map(|(p, fg)| ([p[0] * s, p[1] * s], *fg)).collect(); + let points: Vec<([f32; 2], bool)> = thin(&prompt.points).iter().map(|(p, fg)| ([p[0] * s, p[1] * s], *fg)).collect(); let bbox = prompt.bbox.map(|b| b.map(|v| v * s)); // A prior mask (probabilities at image size) → logits on the low-res grid. let mask: Option> = prompt.mask.as_ref().filter(|m| m.len() == e.w * e.h).map(|m| { @@ -675,3 +678,28 @@ fn sample(rgb: &[[f32; 3]], w: usize, h: usize, x: f32, y: f32, foot: f32) -> [f } s.map(|v| v / f32::max(n, 1.0)) } + +/// `points`, or `MAX_POINTS` evenly spaced ones (first and last kept) when there are more. +fn thin(points: &[([f32; 2], bool)]) -> Vec<([f32; 2], bool)> { + let n = points.len(); + if n <= MAX_POINTS { + return points.to_vec(); + } + (0..MAX_POINTS).filter_map(|i| points.get(i * (n - 1) / (MAX_POINTS - 1)).copied()).collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn many_prompt_points_are_thinned_to_the_budget() { + let few: Vec<_> = (0..MAX_POINTS).map(|i| ([i as f32, 0.0], i % 2 == 0)).collect(); + assert_eq!(thin(&few), few, "a prompt within the budget is untouched"); + let many: Vec<_> = (0..1_000_000).map(|i| ([i as f32, 0.0], i % 2 == 0)).collect(); + let t = thin(&many); + assert_eq!(t.len(), MAX_POINTS); + assert_eq!((t[0].0, t[MAX_POINTS - 1].0), (many[0].0, many[many.len() - 1].0)); + assert!(t.windows(2).all(|w| w[0].0[0] < w[1].0[0]), "spread in order"); + } +} diff --git a/crates/segment/src/pt.rs b/crates/segment/src/pt.rs index 35ed57297..728cc84f7 100644 --- a/crates/segment/src/pt.rs +++ b/crates/segment/src/pt.rs @@ -108,10 +108,14 @@ fn read_entry<'a>(b: &'a [u8], e: &Entry) -> Result> match e.method { 0 => Ok(std::borrow::Cow::Borrowed(raw)), 8 => { - if e.size > MAX_ENTRY_BYTES { + // Deflate expands at most about 1032:1, so a stated size past that is not this entry's. + if e.size > MAX_ENTRY_BYTES || e.size > e.stored.saturating_mul(1032).saturating_add(64) { return Err("zip entry exceeds the size limit".into()); } - miniz_oxide::inflate::decompress_to_vec_with_limit(raw, e.size.max(1)).map(std::borrow::Cow::Owned).map_err(|e| format!("bad deflate data: {e:?}")) + // The status only: the error's partial output can be as large as the entry. + miniz_oxide::inflate::decompress_to_vec_with_limit(raw, e.size.max(1)) + .map(std::borrow::Cow::Owned) + .map_err(|e| format!("bad deflate data: {:?}", e.status)) } m => Err(format!("unsupported zip compression {m}")), } @@ -162,6 +166,11 @@ fn nodes(o: &Obj, budget: usize) -> usize { let kids: Vec<&Obj> = match o { Obj::Tuple(v) | Obj::List(v) => v.iter().collect(), Obj::Dict(v) => v.iter().flat_map(|(k, x)| [k, x]).collect(), + // A string counts one node per 64 bytes it holds (a vector one per 8 elements): copies + // are charged for everything they own, not as one object. + Obj::Str(s) => return 1 + s.len() / 64, + Obj::Global(a, b) | Obj::Storage(a, b) => return 1 + (a.len() + b.len()) / 64, + Obj::Tensor { dtype, key, size, stride, .. } => return 1 + (dtype.len() + key.len()) / 64 + (size.len() + stride.len()) / 8, _ => return 1, }; let mut n = 1; @@ -209,6 +218,17 @@ fn memo_get(memo: &HashMap, k: u64, cloned: &mut usize) -> Result Ok(o.clone()) } +/// A copy of the stack top to memoize, charged against the clone budget like a memo read: a +/// program memoizing one large object many times keeps a copy each time. +fn memo_put(stack: &[Obj], cloned: &mut usize) -> Result { + let o = stack.last().ok_or("pickle stack underflow")?; + *cloned = cloned.saturating_add(nodes(o, MAX_PICKLE_NODES)); + if *cloned > MAX_PICKLE_NODES { + return Err("pickle exceeds the object limit".into()); + } + Ok(o.clone()) +} + fn unpickle(p: &[u8]) -> Result { let mut stack: Vec = Vec::new(); let mut memo: HashMap = HashMap::new(); @@ -310,15 +330,15 @@ fn unpickle(p: &[u8]) -> Result { } b'q' => { let k = take(&mut i, 1)?[0] as u64; - memo.insert(k, stack.last().cloned().ok_or("pickle stack underflow")?); + memo.insert(k, memo_put(&stack, &mut cloned)?); } b'r' => { let s = take(&mut i, 4)?; - memo.insert(u32::from_le_bytes([s[0], s[1], s[2], s[3]]) as u64, stack.last().cloned().ok_or("pickle stack underflow")?); + memo.insert(u32::from_le_bytes([s[0], s[1], s[2], s[3]]) as u64, memo_put(&stack, &mut cloned)?); } 0x94 => { let k = memo.len() as u64; - memo.insert(k, stack.last().cloned().ok_or("pickle stack underflow")?); + memo.insert(k, memo_put(&stack, &mut cloned)?); } b'h' => { let k = take(&mut i, 1)?[0] as u64; @@ -609,4 +629,34 @@ mod tests { assert_eq!(half_to_f32(0x3c00), 1.0); assert_eq!(half_to_f32(0xc000), -2.0); } + + #[test] + fn memoizing_a_long_string_again_and_again_runs_out_of_the_budget() { + // A 1 MiB string memoized 300 times would keep 300 MiB of copies. + let mut p = vec![0x80, 2, b'X']; + p.extend_from_slice(&(1u32 << 20).to_le_bytes()); + p.extend(std::iter::repeat_n(b'a', 1 << 20)); + p.extend(std::iter::repeat_n(0x94, 300)); + p.push(b'.'); + assert!(unpickle(&p).is_err_and(|e| e.contains("object limit"))); + } + + #[test] + fn every_owned_field_is_charged_to_the_clone_budget() { + let long = || "a".repeat(1 << 20); + let budget = MAX_PICKLE_NODES; + // A one-byte module with a 1 MiB name (and the same for storages and tensors). + assert!(nodes(&Obj::Global("m".into(), long()), budget) >= (1 << 20) / 64); + assert!(nodes(&Obj::Storage("m".into(), long()), budget) >= (1 << 20) / 64); + let t = Obj::Tensor { dtype: "m".into(), key: long(), offset: 0, size: vec![1; 4096], stride: vec![1; 4096] }; + assert!(nodes(&t, budget) >= (1 << 20) / 64 + 1024); + // Small ones stay cheap. + assert_eq!(nodes(&Obj::Global("a".into(), "b".into()), budget), 1); + } + + #[test] + fn an_entry_stating_more_than_deflate_can_expand_to_is_refused() { + let e = Entry { data: 0, size: 1 << 29, stored: 4, method: 8 }; + assert!(read_entry(&[0u8; 4], &e).is_err_and(|m| m.contains("size limit"))); + } } diff --git a/crates/segment/src/tflite.rs b/crates/segment/src/tflite.rs index 34e85dfab..6aafd056f 100644 --- a/crates/segment/src/tflite.rs +++ b/crates/segment/src/tflite.rs @@ -131,6 +131,8 @@ const MAX_VALUES: u64 = 1 << 30; const MAX_LIVE: u64 = 1 << 29; /// The most constant values decoded and laid out at load (a weight shared by several tensors or /// operators counts once per use). +/// Bytes of tensor names and shapes a model may carry in all (the official ones take well under 1 MB). +const MAX_META: usize = 1 << 24; const MAX_CONSTS: usize = 1 << 27; /// Tensor types (`TensorType`). @@ -261,6 +263,7 @@ impl Model { let mut ints: Vec>> = vec![None; n]; let mut data: Vec<(usize, u8, usize, &[u8])> = vec![]; let mut consts = 0usize; + let mut meta = 0usize; for (i, t) in tensors.iter().enumerate() { let shape: Vec = fb .ints(*t, 0)? @@ -269,7 +272,13 @@ impl Model { .collect::>()?; let ty = fb.byte(*t, 1, FLOAT32)?; let buf = fb.int(*t, 2, 0)? as u32 as usize; - names.push(fb.string(*t, 3)?); + let name = fb.string(*t, 3)?; + // Names and shapes are copied per tensor: bound them in all. + meta = meta.saturating_add(name.len()).saturating_add(shape.len().saturating_mul(8)); + if meta > MAX_META { + return Err("the model's tensor metadata is too large".into()); + } + names.push(name); // Every size computed from shapes later stays in range, even a product of some of the // dimensions (a zero dimension does not let the others grow without bound). let count = shape.iter().try_fold(1usize, |a, &d| a.checked_mul(d)).ok_or("a tensor is too large")?; @@ -329,6 +338,11 @@ impl Model { && let Some(i) = ins.first().copied().flatten() && let Some(len) = floats[i].as_ref().map(Vec::len) { + // The folded constant is what the graph outputs and the run's memory estimate count: + // it must be the declared size. + if shapes[output].iter().try_fold(1usize, |a, &d| a.checked_mul(d)) != Some(len) { + return Err(format!("{}: DEQUANTIZE output size disagrees with its input", names[output])); + } spend(len)?; floats[output] = floats[i].clone(); continue; diff --git a/crates/segment/src/tflite/tests.rs b/crates/segment/src/tflite/tests.rs index 29a781d34..9ba96971f 100644 --- a/crates/segment/src/tflite/tests.rs +++ b/crates/segment/src/tflite/tests.rs @@ -334,6 +334,26 @@ fn shapes_computed_at_run_time_must_match_the_declared_ones() { refused(&zero, "tensor is too large"); } +#[test] +fn a_folded_dequantize_has_its_declared_size() { + let half = |n: usize| vec![0u8; 2 * n]; + let fold = |declared: Vec| { + model(vec![half(4)], vec![(vec![1, 4], FLOAT16, Some(0)), (declared, FLOAT32, None)], vec![(op::DEQUANTIZE, vec![0], vec![1], vec![])], vec![], vec![1]) + }; + // A declared output of one value would let the folded four-value constant be copied out per reference. + refused(&fold(vec![1, 1]), "DEQUANTIZE output size"); + assert!(Model::read(&fold(vec![1, 4])).is_ok()); +} + +#[test] +fn tensor_metadata_is_bounded_in_all() { + // A rank of 4 million dimensions of 1 is a valid shape by size, but copied per tensor. + let m = model(vec![], vec![(vec![1; 1 << 22], FLOAT32, None)], vec![], vec![], vec![]); + refused(&m, "metadata is too large"); + let ok = model(vec![], (0..70).map(|_| (vec![1, 2, 3], FLOAT32, None)).collect(), vec![], vec![], vec![]); + assert!(Model::read(&ok).is_ok()); +} + #[test] fn shared_weights_count_once_per_use() { // One 128 KB buffer decoded for 2100 tensors would be 2100 copies of 2¹⁶ floats. diff --git a/crates/svg/Cargo.toml b/crates/svg/Cargo.toml index 0e1af7719..6a1e4d741 100644 --- a/crates/svg/Cargo.toml +++ b/crates/svg/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "effectcraft-svg" -description = "SVG import: parses SVG 1.1 / SVG 2 static vector content (paths, basic shapes, groups, transforms, use, CSS styles, solid and gradient fills/strokes, opacity) and rasterises it at any scale" +description = "SVG import: parses SVG 1.1 / SVG 2 static vector content (paths, basic shapes, groups, transforms, use, CSS styles, solid, gradient and pattern fills/strokes, opacity) and rasterises it at any scale" version.workspace = true edition.workspace = true license.workspace = true @@ -10,9 +10,11 @@ rust-version.workspace = true workspace = true [dependencies] +craft-svg = { workspace = true } effectcraft-geom = { workspace = true } effectcraft-raster = { workspace = true } effectcraft-path = { workspace = true } kurbo = { workspace = true } roxmltree = "0.21" rayon = { workspace = true } +log = { workspace = true } diff --git a/crates/svg/README.md b/crates/svg/README.md index 862bd01d0..f39156f22 100644 --- a/crates/svg/README.md +++ b/crates/svg/README.md @@ -18,9 +18,31 @@ here, plus **CSS Color Module Level 3** for colour keywords and functions: `stroke-opacity`, `opacity`, `display`, `visibility`, `color`/`currentColor`; - `linearGradient` and `radialGradient` (with `fx`/`fy` focal point), `gradientUnits`, `gradientTransform`, `spreadMethod`, `href` inheritance of stops and attributes; +- static (non-animated) `` fill/stroke with `patternUnits`, `patternContentUnits`, `patternTransform`, + tile `x`/`y`/`width`/`height`, local `href`/`xlink:href` inheritance and solid paints on + supported shape/group children; - styling through presentation attributes, the `style` attribute and `")); } #[test] @@ -245,7 +769,7 @@ fn deep_nesting_errors_instead_of_overflowing_the_stack() { "".repeat(1000), "".repeat(1000) ); - assert_eq!(crate::xml_depth(&s), 2); + assert!(!craft_svg::guard::deeper_than(&s, 3) && craft_svg::guard::deeper_than(&s, 2)); assert!(crate::parse(s.as_bytes()).is_ok()); } @@ -446,3 +970,183 @@ fn at_rule_blocks_are_skipped_by_brace_depth() { assert_eq!(sheet.rules.len(), 1); assert_eq!(sheet.rules[0].classes, vec!["e".to_string()]); } + +/// Build a `w` x `h` document from `nodes` for sparse-content render tests. +fn sparse_doc(w: f64, h: f64, nodes: Vec) -> Doc { + let mut root = Group::new("root"); + root.children = nodes; + Doc { width: w, height: h, root, skipped: Vec::new() } +} + +/// A tiny image on a large canvas is still drawn. +#[test] +fn sparse_minified_image_on_a_large_canvas_is_still_drawn() { + let image = Image { + name: "i".into(), + transform: Affine::scale(1.0 / 8.0), + width: 8, + height: 8, + rgba: std::sync::Arc::new([255u8, 0, 0, 255].repeat(64)), + opacity: 1.0, + }; + let doc = sparse_doc(2049.0, 2049.0, vec![Node::Image(image)]); + let img = try_rasterize(&doc, 2049, 2049, 1.0).expect("sparse image is within budget"); + assert!(px(&img, 0, 0)[3] > 0.99, "the covered pixel must not be dropped by whole-canvas budgeting"); +} + +/// A small shape with a many-element clip is still drawn. +#[test] +fn sparse_shape_with_a_many_element_clip_is_still_drawn() { + let mut clip = kurbo::BezPath::new(); + clip.move_to((0.0, 0.0)); + for i in 0..1026 { + clip.line_to((2.0 * (i % 2) as f64, 2.0 * ((i + 1) % 2) as f64)); + } + clip.line_to((2.0, 2.0)); + clip.line_to((0.0, 2.0)); + clip.close_path(); + assert!(clip.elements().len() > 1000); + let mut g = Group::new("clipped"); + g.clip = vec![(clip, FillRule::NonZero)]; + let doc = parse(br#""#).unwrap(); + g.children = doc.root.children.clone(); + let doc = sparse_doc(512.0, 512.0, vec![Node::Group(g)]); + let img = try_rasterize(&doc, 512, 512, 1.0).expect("sparse clip is within budget"); + assert!(img.data.iter().any(|p| p[3] > 0.5), "clip work is charged by clip area, not canvas area"); +} + +/// Sparse pattern fills on a 4K canvas all render. +#[test] +fn sparse_pattern_fills_on_a_4k_canvas_all_render() { + let rects: String = (0..6).map(|i| format!(r#""#, i * 4)).collect(); + let svg = format!( + r#" + + {rects}"# + ); + let doc = parse(svg.as_bytes()).unwrap(); + let img = try_rasterize(&doc, 3840, 2160, 1.0).expect("sparse patterns are within budget"); + for i in 0..6 { + assert!(px(&img, i * 4, 0)[3] > 0.99, "rect {i} must be painted"); + } +} + +/// A genuinely huge covered area is still refused. +#[test] +fn genuinely_huge_covered_area_is_still_refused() { + // A 16392x1 strip squeezed 8:1 across the whole canvas needs 8x8 taps per pixel everywhere. + let image = Image { + name: "i".into(), + transform: Affine::scale_non_uniform(1.0 / 8.0, 2049.0), + width: 16392, + height: 1, + rgba: std::sync::Arc::new([255u8, 0, 0, 255].repeat(16392)), + opacity: 1.0, + }; + let doc = sparse_doc(2049.0, 2049.0, vec![Node::Image(image)]); + assert_eq!( + try_rasterize(&doc, 2049, 2049, 1.0).err(), + Some(RasterError::WorkLimit("image sampling")), + "a fully covered 64-tap canvas exceeds the work cap" + ); + let img = rasterize(&doc, 2049, 2049, 1.0); + assert!(img.data.iter().all(|p| p[3] == 0.0), "the lossy wrapper drops the refused image"); +} + +/// A group of `count` 1x1 red rects, each inside its own tiny clip, spread over a large canvas. +fn tiny_clipped_rects(count: usize) -> Vec { + (0..count) + .map(|i| { + let (x, y) = ((i % 100) as f64 * 4.0, (i / 100) as f64 * 4.0); + let mut g = Group::new("clipped"); + g.clip = vec![(kurbo::Rect::new(x, y, x + 2.0, y + 2.0).to_path(0.01), FillRule::NonZero)]; + let mut rect = Group::new("r"); + rect.children = parse( + format!(r#""#) + .as_bytes(), + ) + .map(|d| d.root.children) + .unwrap_or_default(); + g.children = rect.children; + Node::Group(g) + }) + .collect() +} + +/// Thousands of tiny clipped items on a big canvas cost region-proportional work: a clipped +/// group works on a crop of its clip region, so the render finishes quickly and completely. +#[test] +fn thousands_of_tiny_clips_on_a_large_canvas_finish_quickly() { + let doc = sparse_doc(2048.0, 2048.0, tiny_clipped_rects(3000)); + let start = std::time::Instant::now(); + let img = try_rasterize(&doc, 2048, 2048, 1.0).expect("tiny clips stay within every budget"); + assert!(start.elapsed() < std::time::Duration::from_secs(20), "region-bounded work: {:?}", start.elapsed()); + assert!(px(&img, 0, 0)[3] > 0.99 && px(&img, 396, 28 * 4)[3] > 0.99); +} + +/// Thousands of tiny pattern-filled shapes and tiny images on a big canvas finish quickly because +/// masks and scans cover only each item's region. +#[test] +fn thousands_of_tiny_pattern_and_image_items_on_a_large_canvas_finish_quickly() { + let mut nodes = Vec::new(); + let rects: String = (0..3000).map(|i| format!(r#""#, (i % 100) * 4, (i / 100) * 4)).collect(); + let svg = format!( + r#" + + {rects}"# + ); + nodes.extend(parse(svg.as_bytes()).unwrap().root.children); + for i in 0..3000 { + nodes.push(Node::Image(Image { + name: "i".into(), + transform: Affine::translate(((i % 100) as f64 * 4.0, 100.0 + (i / 100) as f64 * 4.0)), + width: 1, + height: 1, + rgba: std::sync::Arc::new(vec![0, 255, 0, 255]), + opacity: 1.0, + })); + } + let doc = sparse_doc(3840.0, 2160.0, nodes); + let start = std::time::Instant::now(); + let img = try_rasterize(&doc, 3840, 2160, 1.0).expect("tiny items stay within every budget"); + assert!(start.elapsed() < std::time::Duration::from_secs(20), "region-bounded work: {:?}", start.elapsed()); + assert!(px(&img, 396, 0)[3] > 0.99 && px(&img, 396, 100)[3] > 0.99); +} + +#[test] +fn gradient_stops_shared_across_references_are_not_charged_per_copy() { + // 4,000 stops referenced by 2,100 shapes would be 8.4 million stops if copied, but resolved + // gradients share one stop array, so the import keeps only the 4,000 and succeeds. + let stops: String = (0..4000).map(|i| format!("", i as f64 / 4000.0)).collect(); + let rects = "".repeat(2100); + let svg = format!("{stops}{rects}"); + assert!(parse(svg.as_bytes()).is_ok()); +} + +#[test] +fn nested_knockout_groups_stop_at_the_draw_budget() { + // Each knockout level draws its children twice and each non-isolated level twice more: + // 30 nested levels would be 4^30 draws. The draw budget ends it. + let mut g = Group::new("leaf"); + g.children.push(Node::Shape(Shape { + name: "s".into(), + geom: Geom::Rect { x: 0.0, y: 0.0, w: 4.0, h: 4.0, rx: 0.0, ry: 0.0 }, + transform: Affine::IDENTITY, + fill: Some(Paint::Color([1.0, 0.0, 0.0])), + fill_opacity: 1.0, + fill_rule: FillRule::NonZero, + stroke: None, + opacity: 1.0, + })); + for _ in 0..30 { + let mut p = Group::new("k"); + p.knockout = true; + p.isolated = false; + p.children.push(Node::Group(g)); + g = p; + } + let doc = Doc { width: 4.0, height: 4.0, root: g, skipped: vec![] }; + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || tx.send(rasterize(&doc, 4, 4, 1.0).width).ok()); + assert_eq!(rx.recv_timeout(std::time::Duration::from_secs(20)).expect("rendering finishes"), 4); +} diff --git a/crates/text/src/fonts.rs b/crates/text/src/fonts.rs index d199bf0ff..c0eea0bd7 100644 --- a/crates/text/src/fonts.rs +++ b/crates/text/src/fonts.rs @@ -107,17 +107,29 @@ fn extra_dirs(var: Option<&std::ffi::OsStr>) -> Vec { var.map(|v| std::env::split_paths(v).filter(|p| !p.as_os_str().is_empty()).collect()).unwrap_or_default() } -fn walk(dir: &std::path::Path, depth: u32, out: &mut Vec) { - let Ok(rd) = std::fs::read_dir(dir) else { return }; - for e in rd.flatten() { - let p = e.path(); - if p.is_dir() { - // Bounded, so a symlink loop ends. - if depth < 8 { - walk(&p, depth + 1, out); +/// Font files under `roots`, each real folder listed once. Breadth first from every root, so a +/// folder reached by several routes is listed at its shallowest and keeps the most depth below it. +fn walk(roots: &[PathBuf], out: &mut Vec) { + let mut seen = std::collections::HashSet::new(); + let mut queue: std::collections::VecDeque<(PathBuf, u32)> = roots.iter().map(|r| (r.clone(), 0)).collect(); + while let Some((dir, depth)) = queue.pop_front() { + // Symlinks fanning out to the same folders would otherwise be walked again along every + // path to them (exponentially many within the depth bound). + if !seen.insert(std::fs::canonicalize(&dir).unwrap_or_else(|_| dir.clone())) { + continue; + } + let Ok(rd) = std::fs::read_dir(&dir) else { continue }; + for e in rd.flatten() { + let p = e.path(); + if p.is_dir() { + // Bounded, so a symlink loop ends. + if depth < 8 { + queue.push_back((p, depth + 1)); + } + } else if p.extension().and_then(std::ffi::OsStr::to_str).is_some_and(|x| matches!(x.to_ascii_lowercase().as_str(), "ttf" | "otf" | "ttc" | "otc")) + { + out.push(p); } - } else if p.extension().and_then(std::ffi::OsStr::to_str).is_some_and(|x| matches!(x.to_ascii_lowercase().as_str(), "ttf" | "otf" | "ttc" | "otc")) { - out.push(p); } } } @@ -125,9 +137,7 @@ fn walk(dir: &std::path::Path, depth: u32, out: &mut Vec) { impl FontSource for DirectorySource { fn faces(&self) -> Vec { let mut files = Vec::new(); - for d in &self.dirs { - walk(d, 0, &mut files); - } + walk(&self.dirs, &mut files); files.sort(); files.dedup(); let mut out = Vec::new(); @@ -158,17 +168,6 @@ fn info_from(n: FaceNames, data: FaceData, origin: &'static str) -> FaceInfo { } } -/// A font file's bytes and its metadata from the same open file (so the metadata describes the -/// bytes, not a later version of the file). -fn read_with_metadata(path: &std::path::Path) -> std::io::Result<(Vec, std::fs::Metadata)> { - use std::io::Read; - let mut f = std::fs::File::open(path)?; - let meta = f.metadata()?; - let mut bytes = Vec::new(); - f.read_to_end(&mut bytes)?; - Ok((bytes, meta)) -} - /// A loaded (or loadable) face. pub struct Face { pub id: FaceId, @@ -207,6 +206,36 @@ pub struct VMetrics { pub underline_thickness: f32, } +/// A font file's bytes, shared by every face of it that is loaded (the faces of a collection +/// each held their own copy of the whole file), with the metadata of the open file they were read +/// from (so it describes the bytes, not a later version of the file). +fn file_bytes(p: &std::path::Path) -> std::io::Result<(Arc>, std::fs::Metadata)> { + use std::io::Read; + type Key = (PathBuf, u64, Option); + type Entry = (std::sync::Weak>, std::fs::Metadata); + static FILES: std::sync::LazyLock>> = std::sync::LazyLock::new(Default::default); + let lock = || FILES.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let hit = |files: &std::collections::HashMap, key: &Key| files.get(key).and_then(|(w, m)| Some((w.upgrade()?, m.clone()))); + let mut f = std::fs::File::open(p)?; + // Keyed by size and modification time too: a file replaced at the same path is read afresh. + let m = f.metadata()?; + let key: Key = (p.to_path_buf(), m.len(), m.modified().ok()); + if let Some(found) = hit(&lock(), &key) { + return Ok(found); + } + // Read outside the lock: a slow file must not hold up every other face's first load. + let mut bytes = Vec::new(); + f.read_to_end(&mut bytes)?; + let b = Arc::new(bytes); + let mut files = lock(); + if let Some(found) = hit(&files, &key) { + return Ok(found); + } + files.retain(|_, (w, _)| w.strong_count() > 0); + files.insert(key, (Arc::downgrade(&b), m.clone())); + Ok((b, m)) +} + impl Face { /// The font file bytes, loaded once on first use, or `None` when they cannot be read. fn bytes(&self) -> Option<&[u8]> { @@ -215,9 +244,9 @@ impl Face { FaceData::Static(b) => Some(FaceBytes::Static(b)), FaceData::Shared(b) => Some(FaceBytes::Shared(b.clone())), FaceData::File(p) => { - let (bytes, meta) = read_with_metadata(p).ok()?; + let (bytes, meta) = file_bytes(p).ok()?; let _ = self.file_meta.set(meta); - Some(FaceBytes::Shared(Arc::new(bytes))) + Some(FaceBytes::Shared(bytes)) } }) .as_ref() diff --git a/crates/text/src/fonts/tests.rs b/crates/text/src/fonts/tests.rs index d95f07c6a..2e04e5f0e 100644 --- a/crates/text/src/fonts/tests.rs +++ b/crates/text/src/fonts/tests.rs @@ -567,3 +567,72 @@ fn unknown_face_id_returns_bundled_default() { assert!(fallback.font().is_some()); assert!(Arc::ptr_eq(&fallback, &face(0))); } + +/// A scratch folder this test created itself (never one that already exists), removed on drop. +struct Scratch(std::path::PathBuf); +impl Scratch { + fn new(tag: &str) -> Self { + let d = (0u32..) + .map(|n| std::env::temp_dir().join(format!("effectcraft-{tag}-{}-{n}", std::process::id()))) + .find_map(|d| match std::fs::create_dir(&d) { + Ok(()) => Some(d), + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => None, + Err(e) => panic!("create scratch dir {}: {e}", d.display()), + }) + .unwrap(); + Scratch(d) + } +} +impl Drop for Scratch { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +#[cfg(unix)] +#[test] +fn a_folder_reached_through_many_links_is_listed_once() { + // Six links back to the folder itself: 6^8 listings within the depth bound before. + let scratch = Scratch::new("font-walk"); + let d = &scratch.0; + std::fs::write(d.join("a.ttf"), b"x").unwrap(); + for i in 0..6 { + std::os::unix::fs::symlink(d, d.join(format!("l{i}"))).unwrap(); + } + let mut out = Vec::new(); + let start = std::time::Instant::now(); + walk(std::slice::from_ref(d), &mut out); + assert!(start.elapsed() < std::time::Duration::from_secs(5)); + assert_eq!(out.len(), 1); +} + +#[test] +fn a_folder_inside_another_root_keeps_its_own_depth() { + // root/a/b/c/d/e/f/g/h is also a root: the walk of `root` reaches it at depth 8 and could not + // go below it, so it must be listed from the shallower visit. + let scratch = Scratch::new("font-depth"); + let root = &scratch.0; + let deep = root.join("a/b/c/d/e/f/g/h"); + std::fs::create_dir_all(deep.join("sub")).unwrap(); + std::fs::write(deep.join("sub/x.ttf"), b"x").unwrap(); + let mut out = Vec::new(); + walk(&[root.clone(), deep.clone()], &mut out); + assert_eq!(out, vec![deep.join("sub/x.ttf")]); +} + +#[test] +fn a_font_file_replaced_at_the_same_path_is_read_again() { + let scratch = Scratch::new("font-bytes"); + let p = scratch.0.join("a.ttf"); + std::fs::write(&p, b"old").unwrap(); + let (old, old_meta) = file_bytes(&p).unwrap(); + // Faces of one file share its bytes, and the metadata is that of the file the bytes came from. + let (again, _) = file_bytes(&p).unwrap(); + assert!(Arc::ptr_eq(&old, &again)); + assert_eq!(old_meta.len(), 3); + std::fs::write(&p, b"new, longer").unwrap(); + let (new, new_meta) = file_bytes(&p).unwrap(); + assert_eq!(new.as_slice(), b"new, longer"); + assert_eq!(new_meta.len(), 11); + assert_eq!(old.as_slice(), b"old"); +} diff --git a/crates/text/src/layout.rs b/crates/text/src/layout.rs index 4c0b5a436..9eb19d919 100644 --- a/crates/text/src/layout.rs +++ b/crates/text/src/layout.rs @@ -278,19 +278,26 @@ pub struct Layout { impl Layout { /// Line index for a caret at byte `pos`. pub fn line_of(&self, pos: usize) -> usize { - let mut li = 0; - for (i, l) in self.lines.iter().enumerate() { - if l.range.start <= pos { - li = i; - } - } - li + // Lines are in text order: the last one starting at or before `pos`. + self.lines.partition_point(|l| l.range.start <= pos).saturating_sub(1) } /// Caret position `(x, baseline, line)` for byte offset `pos`. pub fn caret(&self, pos: usize) -> (f32, f32, usize) { let li = self.line_of(pos); let Some(l) = self.lines.get(li) else { return (0.0, 0.0, 0) }; - let x = l.carets.iter().min_by_key(|(b, _)| b.abs_diff(pos)).map_or(l.x, |c| c.1); + // Carets are sorted by byte offset and unique: the nearest is next to the insertion point (the lower on a tie). + let i = l.carets.partition_point(|c| c.0 < pos); + let x = match (i.checked_sub(1).and_then(|k| l.carets.get(k)), l.carets.get(i)) { + (Some(p), Some(n)) => { + if pos - p.0 <= n.0 - pos { + p.1 + } else { + n.1 + } + } + (Some(c), None) | (None, Some(c)) => c.1, + (None, None) => l.x, + }; (x, l.baseline, li) } /// Nearest caret byte offset to point `(x, y)`. @@ -371,6 +378,34 @@ struct Item { range: Range, rtl: bool, glyphs: Vec, + /// Whether the glyphs' clusters never decrease (`Some(true)`) or never increase + /// (`Some(false)`) along `glyphs`: the glyphs of a cluster range are then contiguous. + cluster_order: Option, +} + +impl Item { + fn new(range: Range, rtl: bool, glyphs: Vec) -> Self { + let cluster_order = if glyphs.windows(2).all(|w| w[0].cluster <= w[1].cluster) { + Some(true) + } else if glyphs.windows(2).all(|w| w[0].cluster >= w[1].cluster) { + Some(false) + } else { + None + }; + Self { range, rtl, glyphs, cluster_order } + } + + /// The glyphs whose cluster lies in `run`, in glyph order, found by bisection when the clusters + /// are ordered (a line of a long paragraph otherwise scanned the whole item). + fn glyphs_in(&self, run: &Range) -> impl Iterator { + let g = &self.glyphs; + let (lo, hi) = match self.cluster_order { + Some(true) => (g.partition_point(|x| x.cluster < run.start), g.partition_point(|x| x.cluster < run.end)), + Some(false) => (g.partition_point(|x| x.cluster >= run.end), g.partition_point(|x| x.cluster >= run.start)), + None => (0, g.len()), + }; + g[lo..hi].iter().filter(|x| run.contains(&x.cluster)) + } } fn upper_single(c: char) -> char { @@ -621,9 +656,31 @@ fn is_hanging_open(c: char) -> bool { matches!(c, '"' | '\'' | '\u{2018}' | '\u{201c}' | '(' | '\u{ab}') } +/// Whether no run is reversed and each starts at or after the end of the one before it (the runs +/// of a document): the precondition of [`run_at`]'s bisection. +fn runs_ordered(runs: &[(Range, TextStyle)]) -> bool { + runs.iter().all(|(r, _)| r.start <= r.end) && runs.windows(2).all(|w| w[0].0.end <= w[1].0.start) +} + +/// Style runs with whether they are [`runs_ordered`]. +#[derive(Clone, Copy)] +struct Runs<'a> { + list: &'a [(Range, TextStyle)], + ordered: bool, +} + /// The style run index covering global byte `b` (the run before it at a boundary, for empty -/// paragraphs at the end). -fn run_at(runs: &[(Range, TextStyle)], b: usize) -> usize { +/// paragraphs at the end). `ordered` is [`runs_ordered`] of `runs`. +fn run_at(runs: &[(Range, TextStyle)], ordered: bool, b: usize) -> usize { + // Ordered, non-overlapping runs: the last one starting at or before `b` is the only one that + // can hold it, found by bisection (a scan from the first run per character was quadratic). + // Anything else (a gap, overlapping or unordered runs) takes the scan, whose first match wins. + if ordered { + let i = runs.partition_point(|(r, _)| r.start <= b).saturating_sub(1); + if runs.get(i).is_some_and(|(r, _)| r.contains(&b)) { + return i; + } + } runs.iter().position(|(r, _)| b >= r.start && b < r.end).unwrap_or_else(|| runs.iter().rposition(|(r, _)| r.start <= b).unwrap_or(0)) } @@ -699,7 +756,7 @@ impl<'a> Para<'a> { fn new( text: &'a str, base: usize, - runs: &'a [(Range, TextStyle)], + runs: Runs<'a>, primaries: &'a [Resolved], variation_ids: &'a [u32], para: &'a ParagraphStyle, @@ -709,7 +766,7 @@ impl<'a> Para<'a> { let bidi = BidiInfo::new(text, default_level); let base_rtl = bidi.paragraphs[0].level.is_rtl(); let chars = char_forms(text, base, runs, primaries, &bidi); - let items = build_items(text, &chars, runs); + let items = build_items(text, &chars, runs.list); // Prefix widths and the preceding non-whitespace boundary make candidate line widths O(1). // Every-line composition evaluates O(n²) candidate breaks; summing each candidate's bytes // or rescanning its trailing whitespace would otherwise add another factor of text length. @@ -732,7 +789,7 @@ impl<'a> Para<'a> { } previous_non_ws[b + c.len_utf8()] = non_ws_end; } - Para { text, base, runs, primaries, variation_ids, para, width, bidi, base_rtl, chars, items, char_adv, prefix_adv, previous_non_ws } + Para { text, base, runs: runs.list, primaries, variation_ids, para, width, bidi, base_rtl, chars, items, char_adv, prefix_adv, previous_non_ws } } fn range_w(&self, r: Range) -> f32 { @@ -900,7 +957,7 @@ impl<'a> Para<'a> { idx.reverse(); } for k in idx { - for g in self.items[k].glyphs.iter().filter(|g| run.contains(&g.cluster)) { + for g in self.items[k].glyphs_in(&run) { let st = &self.runs[g.run].1; let x0 = pen; pen += g.pre; @@ -961,9 +1018,10 @@ impl<'a> Para<'a> { } } // end of line caret - let end_x = match self.chars.iter().rev().find(|c| c.0 < r.end && c.0 >= r.start) { + let last_char = self.chars.partition_point(|c| c.0 < r.end).checked_sub(1).and_then(|i| self.chars.get(i)).filter(|c| c.0 >= r.start); + let end_x = match last_char { Some(c) => { - let cs = cluster_starts.iter().rev().find(|s| **s <= c.0).copied(); + let cs = cluster_starts.partition_point(|s| *s <= c.0).checked_sub(1).and_then(|i| cluster_starts.get(i)).copied(); match cs.and_then(|s| extents.get(&s)) { Some(&(x0, x1, rtl)) => { if rtl { @@ -1014,11 +1072,11 @@ impl<'a> Para<'a> { } /// Each char with its shaped form, face, direction and run. -fn char_forms(text: &str, base: usize, runs: &[(Range, TextStyle)], primaries: &[Resolved], bidi: &BidiInfo) -> Vec { +fn char_forms(text: &str, base: usize, runs: Runs, primaries: &[Resolved], bidi: &BidiInfo) -> Vec { text.char_indices() .map(|(b, c)| { - let si = run_at(runs, base + b); - let style = &runs[si].1; + let si = run_at(runs.list, runs.ordered, base + b); + let style = &runs.list[si].1; let primary = primaries[si].face; let face_of = |ch: char| if ch.is_whitespace() { primary } else { fonts::fallback_for(ch, primary) }; let all_small = style.opentype.all_small_caps && style.caps != Caps::All; @@ -1066,23 +1124,15 @@ fn build_items(text: &str, chars: &[CharInfo], runs: &[(Range, TextStyle) let mut glyphs = shape_item(&sub, rtl, face, size, style, si, form); space_item(&mut glyphs, style, text, true); let end = if j < chars.len() { chars[j].0 } else { text.len() }; - items.push(Item { range: chars[i].0..end, rtl, glyphs }); + items.push(Item::new(chars[i].0..end, rtl, glyphs)); i = j; } items } -fn paragraph( - text: &str, - base: usize, - runs: &[(Range, TextStyle)], - primaries: &[Resolved], - variation_ids: &[u32], - para: &ParagraphStyle, - width: Option, -) -> Vec { +fn paragraph(text: &str, base: usize, runs: Runs, primaries: &[Resolved], variation_ids: &[u32], para: &ParagraphStyle, width: Option) -> Vec { if text.is_empty() { - let si = run_at(runs, base); + let (runs, si) = (runs.list, run_at(runs.list, runs.ordered, base)); let pm = style_metrics(primaries, runs, si); // No text to resolve a direction from: the explicit one. let rtl = para.rtl == Some(true); @@ -1160,18 +1210,20 @@ pub fn layout_rich_uncached(text: &str, runs: &[(Range, TextStyle)], para }); let runs = cleared.as_deref().unwrap_or(runs); let variation_ids: Vec = runs.iter().map(|(_, s)| crate::variable::intern(&s.variations)).collect(); + let ordered = runs_ordered(runs); let mut lay = Layout { missing_font: primaries.iter().any(|p| p.missing), text_len: text.len(), ..Default::default() }; let mut plines: Vec<(usize, ParaLine)> = Vec::new(); for (pi, span) in paragraph_ranges(text).enumerate() { let p = text.get(span.bytes.clone()).unwrap_or_default(); - plines.extend(paragraph(p, span.bytes.start, runs, &primaries, &variation_ids, para_at(pi), width).into_iter().map(|l| (pi, l))); + plines + .extend(paragraph(p, span.bytes.start, Runs { list: runs, ordered }, &primaries, &variation_ids, para_at(pi), width).into_iter().map(|l| (pi, l))); } let mut baseline = 0.0f32; for (i, (pi, pl)) in plines.into_iter().enumerate() { let para = para_at(pi); if i == 0 { // Vertical type keeps its first column at the font ascent (not measured in AE). - let vertical = runs.get(run_at(runs, pl.range.start)).is_some_and(|r| r.1.vertical) + let vertical = runs.get(run_at(runs, ordered, pl.range.start)).is_some_and(|r| r.1.vertical) || runs.iter().any(|r| r.1.vertical && r.0.start < pl.range.end && r.0.end > pl.range.start); baseline = if width.is_some() { if vertical { pl.ascent } else { pl.first } } else { 0.0 }; } else { @@ -1191,7 +1243,7 @@ pub fn layout_rich_uncached(text: &str, runs: &[(Range, TextStyle)], para })); let x = shift + pl.left_trim; if let Some((u0, u1)) = pl.underline { - let si = run_at(runs, pl.range.start); + let si = run_at(runs, ordered, pl.range.start); let pm = fonts::face(primaries[si].face).metrics(runs[si].1.size); lay.underlines.push([u0 + shift, baseline + pm.underline_pos, u1 + shift, baseline + pm.underline_pos + pm.underline_thickness]); } diff --git a/crates/text/src/layout/tests.rs b/crates/text/src/layout/tests.rs index 5792c1c16..bf275cad8 100644 --- a/crates/text/src/layout/tests.rs +++ b/crates/text/src/layout/tests.rs @@ -171,6 +171,17 @@ fn caps_and_small_caps() { assert_eq!(l.underlines.len(), 1); } +#[test] +fn indexed_caret_lookup_matches_a_linear_scan() { + let l = layout("fi abc\n\nde fg hi", &st(40.0), &ParagraphStyle::default()); + for pos in 0..=l.lines.iter().map(|x| x.range.end).max().unwrap_or(0) + 2 { + let li = l.lines.iter().enumerate().filter(|(_, x)| x.range.start <= pos).map(|(i, _)| i).next_back().unwrap_or(0); + let line = &l.lines[li]; + let want = line.carets.iter().min_by_key(|(b, _)| b.abs_diff(pos)).map_or(line.x, |c| c.1); + assert_eq!(l.caret(pos), (want, line.baseline, li), "pos {pos}"); + } +} + #[test] fn ligature_caret_interpolates() { // Inter has an "fi"-like ligature only via calt in some versions; use "ffi" which most @@ -494,3 +505,48 @@ fn a_later_vertical_run_keeps_the_first_line_baseline() { let mixed = layout_rich_uncached(text, &[(0..4, horiz), (4..text.len(), vert)], &[p]); assert!((mixed.lines[0].baseline - plain.lines[0].baseline).abs() < 1e-3, "{} vs {}", mixed.lines[0].baseline, plain.lines[0].baseline); } + +/// `Item::glyphs_in` finds a cluster range by bisection when the clusters are ordered, and agrees +/// with a full scan for ascending, descending and unordered clusters. +#[test] +fn item_glyphs_in_matches_a_full_scan() { + let glyph = |cluster| ShapedGlyph { face: 0, id: cluster as u32, cluster, adv: 1.0, pre: 0.0, dx: 0.0, dy: 0.0, size: 10.0, run: 0, rise: 0.0 }; + let asc: Vec = vec![0, 0, 1, 3, 3, 3, 5, 8]; + let desc: Vec = asc.iter().rev().copied().collect(); + let mixed: Vec = vec![3, 0, 5, 1, 8, 3, 0, 3]; + for (clusters, order) in [(&asc, Some(true)), (&desc, Some(false)), (&mixed, None)] { + let item = Item::new(0..9, false, clusters.iter().map(|&c| glyph(c)).collect()); + assert_eq!(item.cluster_order, order); + for (a, b) in [(0, 9), (0, 1), (1, 4), (3, 4), (4, 9), (6, 8), (9, 9), (2, 3)] { + let got: Vec = item.glyphs_in(&(a..b)).map(|g| g.cluster).collect(); + let want: Vec = clusters.iter().copied().filter(|c| (a..b).contains(c)).collect(); + assert_eq!(got, want, "{clusters:?} {a}..{b}"); + } + } +} + +#[test] +fn run_lookup_by_bisection_matches_the_scan() { + let scan = |runs: &[(Range, TextStyle)], b: usize| { + runs.iter().position(|(r, _)| b >= r.start && b < r.end).unwrap_or_else(|| runs.iter().rposition(|(r, _)| r.start <= b).unwrap_or(0)) + }; + let runs: Vec<(Range, TextStyle)> = (0..50).map(|i| (i * 3..i * 3 + 3, st(10.0))).collect(); + let gapped: Vec<(Range, TextStyle)> = (0..50).map(|i| (i * 4..i * 4 + 3, st(10.0))).collect(); + // Overlapping runs: the first run holding a byte wins, as in the scan. + let overlapping: Vec<(Range, TextStyle)> = vec![(0..10, st(10.0)), (5..15, st(20.0)), (12..20, st(30.0))]; + assert!(runs_ordered(&runs) && runs_ordered(&gapped) && !runs_ordered(&overlapping)); + for b in 0..220 { + assert_eq!(run_at(&runs, true, b), scan(&runs, b), "{b}"); + assert_eq!(run_at(&gapped, true, b), scan(&gapped, b), "{b}"); + assert_eq!(run_at(&overlapping, false, b), scan(&overlapping, b), "{b}"); + } + assert_eq!(run_at(&overlapping, false, 6), 0); + // A reversed range (20..0) among others is not ordered either, and takes the scan. + #[allow(clippy::reversed_empty_ranges)] + let reversed: Vec<(Range, TextStyle)> = vec![(0..10, st(10.0)), (20..0, st(20.0)), (5..15, st(30.0)), (15..20, st(40.0))]; + assert!(!runs_ordered(&reversed)); + for b in 0..30 { + assert_eq!(run_at(&reversed, runs_ordered(&reversed), b), scan(&reversed, b), "{b}"); + } + assert_eq!(run_at(&[], true, 5), 0); +} diff --git a/crates/text/src/path_text.rs b/crates/text/src/path_text.rs index 85cab0719..7455456ad 100644 --- a/crates/text/src/path_text.rs +++ b/crates/text/src/path_text.rs @@ -19,7 +19,26 @@ impl PathMeasure { let mut closed = false; let mut started = false; let mut done = false; - kurbo::flatten(path.iter(), 0.05, |el| { + // The tolerance grows with the path's size (0.05 for ordinary layer-sized paths): a + // path with huge coordinates would otherwise flatten into hundreds of millions of points. + // Only the first subpath is measured, so only it sets the tolerance and gets flattened. + let mut first = BezPath::new(); + for el in path.iter() { + if matches!(el, PathEl::MoveTo(_)) && !first.elements().is_empty() { + break; + } + first.push(el); + if matches!(el, PathEl::ClosePath) { + break; + } + } + // A non-finite coordinate (an infinite control point too) measures nothing. + if !first.is_finite() { + return PathMeasure::default(); + } + let bbox = kurbo::Shape::bounding_box(&first); + let size = bbox.width().max(bbox.height()); + kurbo::flatten(first.iter(), 0.05_f64.max(size * 1e-6), |el| { if done { return; } @@ -128,6 +147,38 @@ pub fn arc_positions(xs: &[f64], line: [f64; 2], align: u8, length: f64, first_m mod tests { use super::*; + #[test] + fn a_huge_curve_flattens_into_a_bounded_number_of_points() { + let mut p = BezPath::new(); + p.move_to((0.0, 0.0)); + p.curve_to((1e15, 0.0), (1e15, 1e15), (0.0, 1e15)); + let m = PathMeasure::new(&p, false); + assert!(m.pts.len() < 100_000, "{}", m.pts.len()); + assert!(m.length() > 1e15); + // Non-finite coordinates measure nothing. + let mut q = BezPath::new(); + q.move_to((0.0, 0.0)); + q.curve_to((f64::INFINITY, 0.0), (1.0, 1.0), (2.0, 0.0)); + assert_eq!(PathMeasure::new(&q, false).pts.len(), 0); + // Even with finite endpoints, an infinite control point is refused before flattening. + let mut r = BezPath::new(); + r.move_to((0.0, 0.0)); + r.curve_to((f64::INFINITY, f64::INFINITY), (f64::INFINITY, 0.0), (1.0, 0.0)); + assert_eq!(PathMeasure::new(&r, false).pts.len(), 0); + } + + #[test] + fn a_distant_second_subpath_does_not_coarsen_the_first() { + let mut p = BezPath::new(); + p.move_to((0.0, 0.0)); + p.curve_to((0.0, 100.0), (100.0, 100.0), (100.0, 0.0)); + p.move_to((1e9, 1e9)); + p.line_to((1e9 + 1.0, 1e9)); + let m = PathMeasure::new(&p, false); + assert!(m.pts.len() > 10, "{}", m.pts.len()); + assert!(m.length() > 150.0, "{}", m.length()); + } + #[test] fn measures_a_line_and_a_circle() { let mut p = BezPath::new(); diff --git a/crates/text/src/sfnt.rs b/crates/text/src/sfnt.rs index a5e3907ce..0ca447f5b 100644 --- a/crates/text/src/sfnt.rs +++ b/crates/text/src/sfnt.rs @@ -257,7 +257,12 @@ fn name_string(b: &[u8], id: u16) -> Option { // Best rank first; a blank record falls through to the next. Decoding is lazy, so aliased // records are not decoded once a usable one is found. cands.sort_by_key(|c| c.0); + // Records may alias one string: each (encoding, bytes) is decoded at most once. + let mut tried = std::collections::HashSet::new(); cands.into_iter().find_map(|(_, plat, enc, raw)| { + if !tried.insert((plat, enc, raw.as_ptr(), raw.len())) { + return None; + } let s = match (plat, enc) { (3, 1 | 10) | (0, _) => { let u: Vec = raw.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); diff --git a/crates/track/src/camtrack/bundle.rs b/crates/track/src/camtrack/bundle.rs index a4412dbce..5cdaa38ca 100644 --- a/crates/track/src/camtrack/bundle.rs +++ b/crates/track/src/camtrack/bundle.rs @@ -485,6 +485,14 @@ fn adjust_impl(cams: &mut [BaCam], pts: &mut [V3], obs: &[BaObs], o: &BaOpts, al let build_point = |j: usize, s_acc: &mut Vec, bc: &mut Vec| -> PtSys { let mut ps = PtSys { v: [[0.0; 3]; 3], bp: [0.0; 3], w: vec![] }; let mut jc = CameraJacobian::new(); + // Observations that repeat a camera add to the same parameters: combine them, so the + // point's entries stay at most one per parameter (the reduction is quadratic in them). + let mut cams_seen: Vec = by_pt[j].iter().map(|&oi| obs[oi as usize].cam).collect(); + cams_seen.sort_unstable(); + let repeated_camera = cams_seen.windows(2).any(|p| p[0] == p[1]); + // Parameter index → entry in `ps.w`, for points that repeat a camera (a linear search + // per entry would be quadratic in their observations). + let mut slot: std::collections::HashMap = std::collections::HashMap::new(); for &oi in &by_pt[j] { let ob = &obs[oi as usize]; let ci = ob.cam as usize; @@ -537,6 +545,16 @@ fn adjust_impl(cams: &mut [BaCam], pts: &mut [V3], obs: &[BaObs], o: &BaOpts, al // Each observation of a point is on another camera: only the shared focal // length repeats. let repeats = Some(*ia) == l.shared || l.dist.is_some_and(|g| *ia == g || *ia == g + 1); + if repeated_camera { + match slot.get(ia) { + Some(&k) => ps.w[k].1 = add(ps.w[k].1, wv), + None => { + slot.insert(*ia, ps.w.len()); + ps.w.push((*ia, wv)); + } + } + continue; + } let found = if repeats { ps.w.iter_mut().find(|e| e.0 == *ia) } else { None }; match found { Some(e) => e.1 = add(e.1, wv), @@ -820,6 +838,25 @@ mod tests { BaCam { pose: Pose::IDENTITY, f: 900.0, dist: Distortion::NONE } } + /// Many observations of one point by one camera combine into one contribution per parameter: + /// the point's reduction used to compare every duplicate with every later one. + #[test] + fn repeated_observations_of_one_camera_are_combined() { + let cams = [test_camera(), BaCam { pose: Pose { c: [1.0, 0.0, 0.0], ..Pose::IDENTITY }, ..test_camera() }]; + let truth = [0.2, -0.1, 5.0]; + let mut obs = Vec::new(); + for _ in 0..30_000 { + for (cam, c) in cams.iter().enumerate() { + let uv = residual(c, truth, [0.0; 2]).unwrap(); + obs.push(BaObs { cam: cam as u32, pt: 0, uv }); + } + } + let (mut cams, mut pts) = (cams, [[0.25, -0.05, 5.2]]); + let o = BaOpts::default(); + let r = adjust(&mut cams, &mut pts, &obs, &o).unwrap(); + assert!(r.cost <= r.initial_cost, "{r:?}"); + } + #[test] fn cost_rejects_invalid_observation_indices() { for (cam, pt) in [(1, 0), (0, 1)] { diff --git a/crates/track/src/camtrack/solver.rs b/crates/track/src/camtrack/solver.rs index 8e212127b..99e8a9920 100644 --- a/crates/track/src/camtrack/solver.rs +++ b/crates/track/src/camtrack/solver.rs @@ -932,7 +932,10 @@ fn solve_core(tracks: &CameraTracks, s: &SolveSettings, cancel: Option<&AtomicBo } lap("keyframe adjustment"); // Every other frame: resect from the points (nearest registered camera first). - let mut todo: Vec = (0..d.n).filter(|k| !r.cams.contains_key(k)).collect(); + // A frame seeing fewer than six reconstructed points cannot be resected (`register`): it is + // skipped before the reconstruction is copied for it. + let mut todo: Vec = + (0..d.n).filter(|k| !r.cams.contains_key(k) && d.by_frame[*k].iter().filter(|(ti, _)| r.pts.contains_key(ti)).nth(5).is_some()).collect(); todo.sort_by_key(|k| r.cams.keys().map(|c| c.abs_diff(*k)).min().unwrap_or(usize::MAX)); let base = r.clone(); let resected: Vec<(usize, Option)> = todo diff --git a/crates/track/src/face.rs b/crates/track/src/face.rs index 708a05c4f..7c69c02af 100644 --- a/crates/track/src/face.rs +++ b/crates/track/src/face.rs @@ -558,16 +558,19 @@ fn fit_from_face(face: &Face, topo: &Topology, offset: [f64; 2]) -> Option Option<(usize, usize)> { let (w, h) = size; let (ci, cj) = center; + // The radius comes from mask geometry: past the window's larger side every ring is outside it. + let radius = radius.min(w.max(h) as i64 + 1); for r in 0..radius { for dj in -r..=r { - for di in -r..=r { - if di.abs() != r && dj.abs() != r { - continue; - } + // Only the ring's own pixels: the full rows at the top and bottom, the two ends between. + let step = if dj.abs() == r { 1 } else { (2 * r).max(1) }; + let mut di = -r; + while di <= r { let (i, j) = (ci as i64 + di, cj as i64 + dj); if i >= 0 && j >= 0 && (i as usize) < w && (j as usize) < h && skin[j as usize * w + i as usize] { return Some((i as usize, j as usize)); } + di += step; } } } @@ -1187,6 +1190,16 @@ pub fn measure(l: &[[f64; 2]; N], reference_height: f64) -> [f64; 14] { mod tests { use super::*; + #[test] + fn the_skin_search_stays_within_the_window_for_a_huge_radius() { + let mut skin = vec![false; 64 * 64]; + skin[60 * 64 + 5] = true; + // A radius from hostile mask geometry costs no more than one that covers the window. + assert_eq!(nearest_skin(&skin, (64, 64), (32, 32), i64::MAX), Some((5, 60))); + assert_eq!(nearest_skin(&skin, (64, 64), (5, 60), 3), Some((5, 60))); + assert_eq!(nearest_skin(&skin, (64, 64), (0, 0), 10), None); + } + /// Pearson correlation. fn corr(a: &[f64], b: &[f64]) -> f64 { let n = a.len() as f64; diff --git a/crates/track/src/lib.rs b/crates/track/src/lib.rs index 8867bfcb8..83e5c2e75 100644 --- a/crates/track/src/lib.rs +++ b/crates/track/src/lib.rs @@ -359,6 +359,9 @@ impl<'a> Scorer<'a> { } } +/// The largest pre-tracking blur (pixels), as the options command allows. +const MAX_BLUR: f64 = 100.0; + /// The pre-processed pixels around one point: a pyramid of a crop. Plane coordinates are image /// pixels minus `origin`, divided by `2^level`. struct Crop { @@ -369,7 +372,10 @@ struct Crop { impl Crop { /// Crop image pixels `[c - ext, c + ext]` (image pixel coordinates), pre-processed. fn new(frame: &Frame, opts: &TrackOptions, c: [f64; 2], ext: [f64; 2], levels: usize) -> Crop { - let sigma = opts.blur.max(0.0) / 2.0; + // A stored blur or search area past the frame adds only repeated edge pixels: both are + // held to what the frame can use, so a few project numbers cannot size a huge crop. + let sigma = opts.blur.clamp(0.0, MAX_BLUR) / 2.0; + let ext = [ext[0].clamp(0.0, frame.img.width as f64), ext[1].clamp(0.0, frame.img.height as f64)]; let margin = (sigma * 3.0).ceil() + 3.0 + if opts.enhance { 6.0 } else { 0.0 } + (1 << levels) as f64; let x0 = (c[0] - ext[0] - margin).floor(); let y0 = (c[1] - ext[1] - margin).floor(); diff --git a/crates/track/src/roto.rs b/crates/track/src/roto.rs index 3bab65dff..4ce4bfa95 100644 --- a/crates/track/src/roto.rs +++ b/crates/track/src/roto.rs @@ -80,6 +80,10 @@ impl StrokeKind { } } +/// The most frames a propagation chain reaches on either side of its base frame (about two +/// hours at 24 fps). +const MAX_CHAIN: i64 = 200_000; + /// One stroke: a polyline swept by a disc of `radius` layer pixels, on layer frame `frame`. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -151,8 +155,16 @@ impl RotoData { pub fn strokes_of(&self, frame: i64, object: u32) -> Vec<&Stroke> { self.strokes.iter().filter(|s| s.frame == frame && (s.object == object || matches!(s.kind, StrokeKind::Refine | StrokeKind::RefineErase))).collect() } + /// The stored span cut to at most MAX_CHAIN frames either side of the base: a project's + /// numbers must not make one render build billions of keys. Frames past it have no key. + fn bounded_span(&self, base: i64) -> [i64; 2] { + [self.span[0].max(base.saturating_sub(MAX_CHAIN)), self.span[1].min(base.saturating_add(MAX_CHAIN))] + } pub fn in_span(&self, f: i64) -> bool { - self.base.is_some() && f >= self.span[0] && f <= self.span[1] + self.base.is_some_and(|b| { + let span = self.bounded_span(b); + f >= span[0] && f <= span[1] + }) } /// Add a stroke: the first segmentation stroke sets the base frame and a span of /// `default_span` frames each side (clamped to `limits`); strokes outside the span extend it. @@ -181,10 +193,15 @@ impl RotoData { pub fn chain_keys(&self, seed: u64) -> BTreeMap { let mut out = BTreeMap::new(); let Some(base) = self.base else { return out }; + // The strokes by frame, grouped once (each frame looked through every stroke before). + let mut by_frame: BTreeMap> = BTreeMap::new(); + for s in &self.strokes { + by_frame.entry(s.frame).or_default().push(s); + } let key = |prev: u64, f: i64| { let mut h = prev ^ 0x9e37_79b9_7f4a_7c15; fnv(&mut h, &f.to_le_bytes()); - for s in self.strokes.iter().filter(|s| s.frame == f) { + for s in by_frame.get(&f).into_iter().flatten() { fnv(&mut h, serde_json::to_string(s).unwrap_or_default().as_bytes()); } h @@ -192,12 +209,16 @@ impl RotoData { let k0 = key(seed, base); out.insert(base, k0); let mut k = k0; - for f in base + 1..=self.span[1] { - k = key(k, f); - out.insert(f, k); + let span = self.bounded_span(base); + // `base + 1` only when there is a frame after the base (it overflows at i64::MAX). + if base < span[1] { + for f in base + 1..=span[1] { + k = key(k, f); + out.insert(f, k); + } } k = k0; - for f in (self.span[0]..base).rev() { + for f in (span[0]..base).rev() { k = key(k, f); out.insert(f, k); } diff --git a/crates/track/src/roto/tests.rs b/crates/track/src/roto/tests.rs index e50dbbce8..4b2a040b4 100644 --- a/crates/track/src/roto/tests.rs +++ b/crates/track/src/roto/tests.rs @@ -723,6 +723,29 @@ fn the_union_of_objects_is_their_largest_extent() { assert_eq!(FrameSeg::union(vec![seg(&[1]), odd]).matte, vec![0, 255, 0, 0]); } +#[test] +fn a_stored_span_reaches_a_bounded_number_of_frames() { + // A project can store any span: the chain stops MAX_CHAIN frames either side of the base. + let d = RotoData { base: Some(0), span: [i64::MIN, i64::MAX], strokes: vec![] }; + let k = d.chain_keys(1); + assert_eq!(k.len() as i64, 2 * super::MAX_CHAIN + 1); + assert_eq!(k.keys().next(), Some(&-super::MAX_CHAIN)); +} + +#[test] +fn in_span_matches_the_frames_that_have_keys_and_extreme_bases_do_not_overflow() { + let d = RotoData { base: Some(0), span: [i64::MIN, i64::MAX], strokes: vec![] }; + assert!(d.in_span(super::MAX_CHAIN) && !d.in_span(super::MAX_CHAIN + 1)); + assert!(d.in_span(-super::MAX_CHAIN) && !d.in_span(-super::MAX_CHAIN - 1)); + assert_eq!(d.source_of(super::MAX_CHAIN + 1), None); + assert!(d.chain_keys(1).keys().all(|f| d.in_span(*f))); + // A base at the end of i64: no forward frames, and no `base + 1` overflow. + let d = RotoData { base: Some(i64::MAX), span: [i64::MIN, i64::MAX], strokes: vec![] }; + let k = d.chain_keys(1); + assert_eq!(k.len() as i64, super::MAX_CHAIN + 1); + assert!(k.contains_key(&i64::MAX)); +} + /// A scene for the band re-cut: `rgb` of a `w`×`h` frame where the foreground is `inside`, with /// a background that turns red left of `x = 30` (far from most shapes), and a soft prior that is /// `inside` shifted by (3, 2) px with an edge `soft_edge` px wide. diff --git a/crates/track/src/stabilize.rs b/crates/track/src/stabilize.rs index 092fc4ab4..e8f90f28d 100644 --- a/crates/track/src/stabilize.rs +++ b/crates/track/src/stabilize.rs @@ -440,6 +440,12 @@ fn remove_scale(b: &Homography, c: [f64; 2]) -> Homography { Homography::scale_about(1.0 / s, p0).then_after(b) } +/// Widest window: 3 sigma of the largest Smoothness (1000%) with Smooth More at its maximum, at 240 fps. +const MAX_SMOOTH_RADIUS: usize = 8640; +/// Most window taps (frames x radius) one smoothing pass does; past it the window narrows so planning +/// stays bounded (an hour at 60 fps with the widest setting is about 4.7e8). +const MAX_SMOOTH_WORK: usize = 1 << 30; + /// Corrective transforms (source → stabilized, before framing) for every frame. pub fn corrections(a: &WarpAnalysis, s: &StabSettings) -> Vec { let n = a.frames.len(); @@ -470,7 +476,8 @@ pub fn corrections(a: &WarpAnalysis, s: &StabSettings) -> Vec { } StabResult::SmoothMotion => { let sigma = s.sigma(); - let rad = ((3.0 * sigma).ceil() as usize).min(n.saturating_sub(1)); + // 3 sigma covers every supported setting; the work cap only bites on extreme frame counts. + let rad = (((3.0 * sigma).ceil() as usize).min(MAX_SMOOTH_RADIUS)).min(n.saturating_sub(1)).min(MAX_SMOOTH_WORK / n); for t in 0..n { if sigma < 0.3 || rad == 0 { out.push(Homography::IDENTITY); @@ -714,6 +721,47 @@ mod tests { s / (p.len() - 2) as f64 } + #[test] + fn a_huge_stored_fps_does_not_widen_the_smoothing_window_past_the_cap() { + let (a, _) = shaky(9500); + let s = StabSettings { fps: 1e9, preserve_scale: false, ..Default::default() }; + // Window 8640 each side: one frame is smoothed over at most 17281 frames, not all 9500. + let c = corrections(&a, &s); + assert_eq!(c.len(), 9500); + assert!(c.iter().all(|h| h.0.iter().flatten().all(|v| v.is_finite()))); + // A jump at frame 9400 is outside frame 0's window (frames 0..=8640): its correction is + // unchanged. Smoothing over the whole clip would move it. + let (mut jumped, _) = shaky(9500); + let f = &mut jumped.frames[9400]; + f.t[0] += 80.0; + f.s[2] += 80.0; + f.h[2] += 80.0; + let c2 = corrections(&jumped, &s); + for (x, y) in c[0].0.iter().flatten().zip(c2[0].0.iter().flatten()) { + assert!((x - y).abs() < 1e-9, "{x} vs {y}"); + } + assert!( + c[9499].0.iter().flatten().zip(c2[9499].0.iter().flatten()).any(|(x, y)| (x - y).abs() > 1e-6), + "the jump does move frames whose window holds it" + ); + } + + #[test] + fn the_widest_supported_smoothness_keeps_its_full_window() { + // 1000% Smoothness, Smooth More at its maximum, 60 fps: sigma 720 frames, radius 2160. + let s = StabSettings { smoothness: 1000.0, crop_less_smooth_more: 100.0, fps: 60.0, preserve_scale: false, ..Default::default() }; + let (a, _) = shaky(3000); + let c = corrections(&a, &s); + let (mut jumped, _) = shaky(3000); + let f = &mut jumped.frames[2000]; + f.t[0] += 80.0; + f.s[2] += 80.0; + f.h[2] += 80.0; + let c2 = corrections(&jumped, &s); + // Frame 0 sees a jump 2000 frames away (the old 1024-frame cap could not). + assert!(c[0].0.iter().flatten().zip(c2[0].0.iter().flatten()).any(|(x, y)| (x - y).abs() > 1e-9)); + } + #[test] fn smoothing_removes_jitter_and_no_motion_locks() { let (a, path) = shaky(90); diff --git a/crates/track/src/subspace.rs b/crates/track/src/subspace.rs index 9c7d5ceeb..d68615892 100644 --- a/crates/track/src/subspace.rs +++ b/crates/track/src/subspace.rs @@ -494,16 +494,27 @@ fn gauss(lo: usize, hi: usize, t: usize, sigma: f64) -> Vec { /// subspace dimension (9 in the paper). Frames without enough trajectories get no pairs. pub fn smooth_trajectories(tracks: &[Track2D], frames: usize, sigma: f64, rank: usize) -> Vec> { let max_r = ((3.0 * sigma).ceil() as usize).clamp(1, 30); + // The trajectories seen on each frame (in track order): a frame only ever uses those, so none + // scans the whole collection (every frame did, once per window shrink). + let mut seen: Vec> = vec![Vec::new(); frames]; + for (i, tr) in tracks.iter().enumerate() { + let (a, b) = (tr.start as usize, (tr.end() as usize).min(frames)); + for list in seen.iter_mut().take(b).skip(a) { + list.push(i as u32); + } + } (0..frames) .into_par_iter() .map(|t| { let t32 = t as u32; + let on_frame = &seen[t]; let mut r = max_r; // The window around t: shrink it until enough trajectories span it. let (lo, hi, complete) = loop { let lo = t.saturating_sub(r); let hi = (t + r).min(frames.saturating_sub(1)); - let complete: Vec<&Track2D> = tracks.iter().filter(|tr| tr.start as usize <= lo && tr.end() as usize > hi).collect(); + let complete: Vec<&Track2D> = + on_frame.iter().map(|&i| &tracks[i as usize]).filter(|tr| tr.start as usize <= lo && tr.end() as usize > hi).collect(); if complete.len() >= 8 || r <= 2 { break (lo, hi, complete); } @@ -556,7 +567,7 @@ pub fn smooth_trajectories(tracks: &[Track2D], frames: usize, sigma: f64, rank: // Incomplete trajectories seen on t: coefficients by least squares over the frames // they cover in the window. let need = (k + 3).max(m / 2); - for tr in tracks.iter().filter(|tr| tr.at(t32).is_some() && !(tr.start as usize <= lo && tr.end() as usize > hi)) { + for tr in on_frame.iter().map(|&i| &tracks[i as usize]).filter(|tr| !(tr.start as usize <= lo && tr.end() as usize > hi)) { let cols: Vec = (0..m).filter(|c| tr.at((lo + c) as u32).is_some()).collect(); if cols.len() < need { continue; @@ -601,6 +612,22 @@ pub fn smooth_trajectories(tracks: &[Track2D], frames: usize, sigma: f64, rank: mod tests { use super::*; + /// Smoothing visits only the trajectories seen on each frame: tracks that start after the clip + /// (or on other frames) change nothing and cost nothing, however many there are. + #[test] + fn smoothing_ignores_trajectories_off_the_frame() { + let frames = 60usize; + let wob = |k: usize, f: usize| [10.0 + k as f32 * 3.0 + (f as f32 * 0.2).sin() * 4.0, 20.0 + (k % 7) as f32 * 5.0 + f as f32 * 0.5]; + let mut tracks: Vec = (0..30).map(|k| Track2D { id: k as u32, start: 0, pts: (0..frames).map(|f| wob(k, f)).collect() }).collect(); + // Partial tracks: some seen on only part of the clip. + tracks.extend((0..10).map(|k| Track2D { id: 100 + k as u32, start: 5 * k as u32, pts: (5 * k..frames.min(5 * k + 30)).map(|f| wob(k, f)).collect() })); + let base = smooth_trajectories(&tracks, frames, 3.0, 9); + assert!(base.iter().any(|v| v.len() >= 30), "the fixture gives frames with pairs"); + let mut more = tracks.clone(); + more.extend((0..200_000u32).map(|i| Track2D { id: 1000 + i, start: frames as u32 + i % 50, pts: vec![[1.0, 2.0]] })); + assert_eq!(smooth_trajectories(&more, frames, 3.0, 9), base); + } + #[test] fn mesh_identity_sample_and_forward() { let m = Mesh::identity(4, 3, [100.0, 60.0]); diff --git a/crates/ui-egui/examples/cache_bench.rs b/crates/ui-egui/examples/cache_bench.rs index 828075249..43e937c92 100644 --- a/crates/ui-egui/examples/cache_bench.rs +++ b/crates/ui-egui/examples/cache_bench.rs @@ -10,9 +10,18 @@ //! //! Options: `--project P --comp C` (a saved project's comp instead), `--scale S` (render scale, //! default 0.5: a viewer at half size), `--frames N` (frames to cache, default the whole comp), -//! `--slots N` (frames in flight, at least 1, default one per frame thread as the app does when -//! idle), -//! `--disk` (with a fresh disk cache in `target/cache-bench/disk`), `--no-adjustment`. +//! `--slots N` (frames in flight, at least 1, default one per lane as the app does when idle), +//! `--lane-width W` (threads per lane; the app's default when absent; `--play` takes it too), +//! `--disk` (with a fresh disk cache in `target/cache-bench/disk`), `--no-adjustment`, +//! `--gpu` (composite on a GPU of its own, as the desktop app does with Mercury GPU Acceleration: +//! the GPU or the CPU, whichever Auto measures faster per comp, read back to the CPU; the viewer's +//! backend), `--backend cpu|gpu|auto` (with `--gpu`: force one compositor, default `auto`). +//! +//! `--play SECS` instead plays the comp in the app (headless, a viewer at 100 %, Resolution +//! Auto, nothing cached, Cache Frames When Idle off) for SECS seconds of wall time, as a first +//! playback, and prints the frames shown per second, the time stalled and the scale it played at. +//! With `--gpu` the app composites on the GPU (Mercury Auto), otherwise on the CPU; `--res N` +//! sets the comp's Resolution to every N-th pixel instead of Auto. use std::sync::Arc; use std::time::{Duration, Instant}; @@ -28,6 +37,11 @@ fn arg(name: &str) -> Option { a.iter().position(|x| x == name).and_then(|i| a.get(i + 1).cloned()) } +/// `--lane-width W`: threads per frame lane (the app's default when absent). +fn lane_width() -> Option { + arg("--lane-width").and_then(|v| v.parse().ok()) +} + fn run(s: &mut Session, cmd: &str, p: Value) -> Value { s.execute(cmd, p).unwrap_or_else(|e| panic!("{cmd}: {e}")) } @@ -112,23 +126,39 @@ fn main() { cid } }; + if let Some(secs) = arg("--play").and_then(|v| v.parse().ok()) { + play(s, cid, secs, std::env::args().any(|a| a == "--gpu")); + return; + } let comp = s.project.comp(cid).expect("comp").clone(); let total = comp.frame_rate.frame_at(comp.duration); let n = arg("--frames").and_then(|v| v.parse().ok()).unwrap_or(total).min(total); let scale: f64 = arg("--scale").and_then(|v| v.parse().ok()).unwrap_or(0.5); - let frames = Frames::default(); - // (as Cache Frames When Idle: one frame per frame thread, see `app/prefetch.rs`) - let slots: usize = arg("--slots").and_then(|v| v.parse().ok()).map(|n: usize| n.max(1)).unwrap_or(frames.parallelism().max(1)); + let frames = lane_width().map_or_else(Frames::default, Frames::with_lane_width); + // (as Cache Frames When Idle: one frame per lane, see `app/prefetch.rs`) + let slots: usize = arg("--slots").and_then(|v| v.parse().ok()).map(|n: usize| n.max(1)).unwrap_or(frames.lanes()); // Room for every frame: this measures speed, not eviction. frames.set_budget(64 << 30); - let opts = RenderOpts { scale, motion_blur: true, ..Default::default() }; + use effectcraft_engine::render::Backend; + let backend = match arg("--backend").as_deref() { + Some("cpu") => Backend::Cpu, + Some("gpu") => Backend::Gpu, + _ => Backend::Auto, + }; + let opts = RenderOpts { scale, motion_blur: true, backend, ..Default::default() }; + let gpu = std::env::args().any(|a| a == "--gpu").then(|| { + effectcraft_gpu::Gpu::headless().unwrap_or_else(|| { + eprintln!("--gpu: no usable GPU adapter"); + std::process::exit(2) + }) + }); let src = RenderSource { project: s.project.clone(), footage: s.footage.clone(), expr: s.expr.clone(), layer_cache: s.layer_cache.clone(), - gpu: None, + gpu, gpu_display: false, disk: s.disk_cache.clone(), }; @@ -142,7 +172,13 @@ fn main() { opts: opts_hash(&opts), }; - eprintln!("caching {n} frames of {}×{} at scale {scale} with {slots} in flight…", comp.width, comp.height); + eprintln!( + "caching {n} frames of {}×{} at scale {scale} with {slots} in flight on {} lanes{}…", + comp.width, + comp.height, + frames.lanes(), + if src.gpu.is_some() { " on the GPU" } else { "" } + ); let (gop0, t0) = (effectcraft_media::gop_stats(), Instant::now()); let mut next = 0; loop { @@ -166,12 +202,115 @@ fn main() { gop.seeks - gop0.seeks, gop.hits - gop0.hits ); + if let Some(g) = &src.gpu + && let Some(p) = effectcraft_engine::render::Accelerator::auto_pick(g) + && let Some(a) = p.stats(effectcraft_engine::render::AutoKey::new(cid, scale, false)) + { + let ms = |v: Option| v.map_or("–".into(), |v| format!("{v:.1} ms")); + println!("auto (Mercury) GPU {} over {} frames, CPU {} over {} frames", ms(a.gpu_ms), a.gpu_frames, ms(a.cpu_ms), a.cpu_frames); + } let p = pool.stats(); println!("footage cache {} hits, {} misses, {} frames ({} MB) kept", p.hits, p.misses, p.frames, p.bytes >> 20); if let Some(dc) = &s.disk_cache { // (writes finish on the disk cache's own thread) + let t = Instant::now(); dc.flush(); + let flushed = t.elapsed().as_secs_f64(); let d = dc.stats(); - println!("disk cache {} written, {} dropped, {} entries ({} MB)", d.writes, d.dropped, d.entries, d.bytes >> 20); + println!("disk cache {} written, {} dropped, {} entries ({} MB), flushed in {flushed:.2} s", d.writes, d.dropped, d.entries, d.bytes >> 20); } } + +/// `--play`: a first playback of `cid` in the app, as someone pressing play in it would see. +fn play(session: effectcraft_engine::Session, cid: ItemId, secs: f64, gpu: bool) { + use effectcraft_ui_egui::{ControlRequest, EffectcraftApp}; + let (tx, rx) = std::sync::mpsc::channel::(); + let builder = egui_kittest::Harness::builder().with_size(egui::vec2(1920.0, 1200.0)).with_pixels_per_point(1.0); + let builder = if gpu { builder.wgpu() } else { builder }; + let mut h = builder.build_eframe(move |_| EffectcraftApp::new(session).with_control(rx)); + { + let app = h.state_mut(); + if let Some(w) = lane_width() { + app.frames = effectcraft_ui_egui::frames::Frames::with_lane_width(w); + } + let _ = app.session.execute("comp.open", json!({"comp": cid.0})); + app.ui.viewer.zoom = Some(1.0); + app.ui.cache_when_idle = false; + if let (Some(n), Some(c)) = (arg("--res").and_then(|v| v.parse().ok()), Arc::make_mut(&mut app.session.project).comp_mut(cid)) { + c.resolution = effectcraft_engine::project::Resolution::from_factor(n); + } + } + // Let the UI settle (fonts, the GPU compositor's pipelines) before the clock starts. + let ready = Instant::now() + Duration::from_secs(if gpu { 8 } else { 2 }); + while Instant::now() < ready { + h.step(); + std::thread::sleep(Duration::from_millis(10)); + } + h.state().frames.clear(); + let (reply, _r) = std::sync::mpsc::channel(); + let _ = tx.send(ControlRequest { method: "ui.playback".into(), params: json!({"action": "play"}), reply }); + let t0 = Instant::now(); + let (mut started, mut stalled, mut last, mut scales) = (None, 0.0, t0, std::collections::BTreeMap::::new()); + // When each new frame went on screen (seconds since play was pressed): the cadence. The + // current time, not `Playback::shown`: frames shown while the clock holds don't count there. + let fr = h.state().session.project.comp(cid).map(|c| c.frame_rate); + let (mut shown_at, mut last_frame) = (Vec::::new(), None); + while t0.elapsed().as_secs_f64() < secs { + h.step(); + let now = Instant::now(); + let app = h.state(); + if app.playback.playing { + let t = *started.get_or_insert(now); + if app.playback.waiting && now > t { + stalled += (now - last).as_secs_f64(); + } + } + let frame = fr.map(|fr| fr.frame_at(app.session.time())); + if app.playback.playing && frame != last_frame { + last_frame = frame; + shown_at.push((now - t0).as_secs_f64()); + } + *scales.entry(app.shown_series(cid).scale).or_default() += 1; + last = now; + std::thread::sleep(Duration::from_millis(2)); + } + let app = h.state(); + let played = started.map_or(0.0, |t| t.elapsed().as_secs_f64()); + let shown = shown_at.len(); + let frame = fr.map_or(0, |fr| fr.frame_at(app.session.time())); + println!( + "played {shown} frames shown in {played:.2} s: {:.1} frames/s, stalled {stalled:.2} s, reached frame {frame}{}", + shown as f64 / played.max(1e-9), + if gpu { " (GPU)" } else { "" } + ); + // Cadence: how evenly the frames went on screen (the intervals between consecutive shown + // frames after the first), and how long the first two took. + let mut gaps: Vec = shown_at.windows(2).map(|w| w[1] - w[0]).collect(); + gaps.sort_by(f64::total_cmp); + let at = |q: f64| gaps.get(((gaps.len() as f64 - 1.0) * q).round() as usize).copied().unwrap_or(0.0) * 1000.0; + let holds = gaps.iter().filter(|g| **g > 0.2).count(); + println!( + "cadence first frame {:.2} s, second {:.2} s; gaps median {:.0} ms, p95 {:.0} ms, max {:.0} ms, {holds} holds > 200 ms", + shown_at.first().copied().unwrap_or(0.0), + shown_at.get(1).copied().unwrap_or(0.0), + at(0.5), + at(0.95), + at(1.0) + ); + // The opening (the first 3 s, before an adaptive step down can have taken effect): how + // evenly the uncached frames at the starting resolution went on screen. + let mut early: Vec = shown_at.windows(2).filter(|w| w[1] <= 3.0).map(|w| w[1] - w[0]).collect(); + early.sort_by(f64::total_cmp); + let at = |q: f64| early.get(((early.len() as f64 - 1.0) * q).round() as usize).copied().unwrap_or(0.0) * 1000.0; + println!( + "opening (3 s) {} frames; gaps median {:.0} ms, p95 {:.0} ms, max {:.0} ms, {} holds > 200 ms", + early.len() + usize::from(!early.is_empty()), + at(0.5), + at(0.95), + at(1.0), + early.iter().filter(|g| **g > 0.2).count() + ); + let passes: u64 = scales.values().sum(); + let s: Vec = scales.iter().map(|(k, v)| format!("{:.3}: {:.0} %", *k as f64 / 1000.0, 100.0 * *v as f64 / passes.max(1) as f64)).collect(); + println!("viewer scale {}", s.join(", ")); +} diff --git a/crates/ui-egui/examples/rq_bench.rs b/crates/ui-egui/examples/rq_bench.rs new file mode 100644 index 000000000..6d470108b --- /dev/null +++ b/crates/ui-egui/examples/rq_bench.rs @@ -0,0 +1,149 @@ +//! Render Queue benchmark (#3335): a 1080p 32 bpc job on the GPU, the CPU or the measured choice. +//! +//! `cargo run --release -p effectcraft-ui-egui --example rq_bench -- [options]` +//! +//! It builds a comp like a compositing shot: a 1920 × 1080 OpenEXR sequence (float RGBA with a +//! Cryptomatte ID channel and manifest, generated once into `target/rq-bench/media`, no +//! committed media) under Gaussian Blur, Curves and the Cryptomatte effect and a moving +//! translucent solid, at 32 bpc. It queues the comp twice (the second item shows a warm start) and +//! renders the queue as the app does, printing each item's milliseconds per frame and a hash of +//! the written pixels (equal hashes: identical output). +//! +//! Options: `--renderer gpu|cpu` (the project's renderer, default gpu; with gpu the Render +//! Queue measures both unless `EFFECTCRAFT_RQ_RENDERER` says otherwise), `--frames N` (default +//! 60), `--format png|exr|tiff` (default png), `--out DIR` (default `target/rq-bench/out`). + +use std::sync::Arc; + +use effectcraft_engine::Session; +use effectcraft_engine::raster::channels3d::{crypto_float, crypto_hash}; +use serde_json::{Value, json}; + +const W: usize = 1920; +const H: usize = 1080; + +fn arg(name: &str) -> Option { + let a: Vec = std::env::args().collect(); + a.iter().position(|x| x == name).and_then(|i| a.get(i + 1).cloned()) +} + +fn run(s: &mut Session, cmd: &str, p: Value) -> Value { + s.execute(cmd, p).unwrap_or_else(|e| panic!("{cmd}: {e}")) +} + +/// The plate's object at pixel (x, y) in frame `f`: 24 bands that drift sideways. +fn object(x: usize, y: usize, f: usize) -> String { + format!("obj{}", ((x + f * 7) / 80 + y / 270 * 24) % 96) +} + +/// Frame `f` of the plate: float colour over 1.0 (32 bpc), alpha, and Cryptomatte IDs. +fn plate(path: &std::path::Path, f: usize) { + use exr::prelude::*; + let px = |c: usize| -> Vec { + (0..W * H) + .map(|i| { + let (x, y) = (i % W, i / W); + let v = ((x as f32 * 0.003 + f as f32 * 0.05 + c as f32).sin() * 0.5 + 0.5) * (1.0 + y as f32 / H as f32); + if c == 3 { 1.0 } else { v } + }) + .collect() + }; + let ids: Vec = (0..W * H).map(|i| crypto_float(crypto_hash(&object(i % W, i / W, f)))).collect(); + let ch = |n: &str, v: Vec| AnyChannel::new(n, FlatSamples::F32(v)); + let channels = AnyChannels::sort( + vec![ch("R", px(0)), ch("G", px(1)), ch("B", px(2)), ch("A", px(3)), ch("CryptoObject00.R", ids), ch("CryptoObject00.G", vec![1.0; W * H])].into(), + ); + let mut layer = Layer::new((W, H), LayerAttributes::default(), Encoding::FAST_LOSSLESS, channels); + let names: serde_json::Map = (0..96).map(|k| format!("obj{k}")).map(|n| (n.clone(), json!(format!("{:08x}", crypto_hash(&n))))).collect(); + for (key, v) in [("name", "CryptoObject".to_string()), ("manifest", Value::Object(names).to_string())] { + layer.attributes.other.insert(Text::from(format!("cryptomatte/f834d0a/{key}").as_str()), AttributeValue::Text(Text::from(v.as_str()))); + } + Image::from_layer(layer).write().to_file(path).expect("write plate"); +} + +/// FNV-1a over every written frame file, in name order (equal: identical output). +fn hash_dir(dir: &std::path::Path) -> (usize, u64) { + let mut names: Vec<_> = std::fs::read_dir(dir).map(|d| d.filter_map(|e| e.ok()).map(|e| e.path()).collect()).unwrap_or_default(); + names.retain(|p| !p.to_string_lossy().ends_with(".txt")); + names.sort(); + let mut h = 0xcbf2_9ce4_8422_2325u64; + for p in &names { + for b in std::fs::read(p).unwrap_or_default() { + h = (h ^ b as u64).wrapping_mul(0x100_0000_01b3); + } + } + (names.len(), h) +} + +fn main() { + let root = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../target/rq-bench"); + let frames: usize = arg("--frames").and_then(|n| n.parse().ok()).unwrap_or(60); + let format = arg("--format").unwrap_or_else(|| "png".into()); + let gpu = arg("--renderer").as_deref() != Some("cpu"); + let out = arg("--out").map(std::path::PathBuf::from).unwrap_or_else(|| root.join("out")); + let media = root.join("media"); + std::fs::create_dir_all(&media).expect("media folder"); + let first = media.join("plate_0000.exr"); + for f in 0..frames { + let p = media.join(format!("plate_{f:04}.exr")); + if !p.exists() { + if f == 0 { + eprintln!("writing the {frames}-frame EXR plate (once)…"); + } + plate(&p, f); + } + } + + let mut s = effectcraft_host::session(); + let accel = effectcraft_gpu::Gpu::try_headless().unwrap_or_else(|e| panic!("no GPU: {e}")); + eprintln!("GPU: {}", effectcraft_engine::render::Accelerator::name(&accel)); + s.accel = Some(Arc::new(accel)); + let r = run(&mut s, "file.import", json!({"paths": [first.to_string_lossy()], "sequence": true})); + let footage = r["items"][0].as_u64().expect("the plate"); + let secs = frames as f64 / 30.0; + run(&mut s, "comp.new", json!({"name": "Shot", "width": W, "height": H, "frameRate": 30, "duration": secs})); + let plate = run(&mut s, "layer.addItem", json!({"item": footage}))["layer"].clone(); + run(&mut s, "effect.apply", json!({"layers": [plate], "effect": "Gaussian Blur"})); + run(&mut s, "prop.set", json!({"layer": plate, "path": "effects/#1/blurriness", "value": 12.0})); + run(&mut s, "effect.apply", json!({"layers": [plate], "effect": "Curves"})); + run(&mut s, "effect.apply", json!({"layers": [plate], "effect": "Cryptomatte"})); + let solid = run(&mut s, "layer.newSolid", json!({"color": "#d04020", "width": 400, "height": 300}))["layer"].clone(); + run(&mut s, "prop.addKey", json!({"layer": solid, "path": "transform/position", "time": 0.0, "value": [200.0, 540.0]})); + run(&mut s, "prop.addKey", json!({"layer": solid, "path": "transform/position", "time": secs, "value": [1720.0, 540.0]})); + run(&mut s, "prop.set", json!({"layer": solid, "path": "transform/opacity", "value": 70.0})); + let mut p = (*s.project).clone(); + p.settings.bit_depth = effectcraft_engine::project::BitDepth::Bpc32; + p.settings.gpu_acceleration = gpu; + s.project = Arc::new(p); + + let _ = std::fs::remove_dir_all(&out); + let dirs = [out.join("a"), out.join("b")]; + for d in &dirs { + std::fs::create_dir_all(d).expect("output folder"); + let path = d.join(format!("f_[####].{format}")); + run(&mut s, "renderQueue.add", json!({"comp": "Shot", "format": format, "output": path.to_string_lossy(), "log": "plusSettings"})); + } + let t0 = std::time::Instant::now(); + run(&mut s, "renderQueue.render", json!({"wait": true})); + let total = t0.elapsed().as_secs_f64(); + println!( + "renderer {} (EFFECTCRAFT_RQ_RENDERER={:?}, EFFECTCRAFT_GPU_INFLIGHT={:?}), {frames} frames {W}x{H} 32 bpc, {format}", + if gpu { "gpu" } else { "cpu" }, + std::env::var("EFFECTCRAFT_RQ_RENDERER").ok(), + std::env::var("EFFECTCRAFT_GPU_INFLIGHT").ok() + ); + for (i, (item, d)) in s.project.render_queue.iter().zip(&dirs).enumerate() { + let secs = item.render_time.unwrap_or(f64::NAN); + let (n, h) = hash_dir(d); + println!(" item {}: {:?}, {:.1} ms/frame, {n} files, hash {h:016x}", i + 1, item.status, secs * 1000.0 / frames as f64); + // The render log's account of the compositor and the GPU frames in flight. + let logs = + std::fs::read_dir(d).map(|r| r.filter_map(|e| e.ok()).map(|e| e.path()).filter(|p| p.to_string_lossy().ends_with(".txt")).collect::>()); + for log in logs.unwrap_or_default() { + for line in std::fs::read_to_string(log).unwrap_or_default().lines().filter(|l| l.starts_with("Renderer:") || l.starts_with("GPU frames")) { + println!(" {line}"); + } + } + } + println!(" queue: {total:.2} s"); +} diff --git a/crates/ui-egui/src/app.rs b/crates/ui-egui/src/app.rs index 9d32ba4cd..518f4e809 100644 --- a/crates/ui-egui/src/app.rs +++ b/crates/ui-egui/src/app.rs @@ -68,6 +68,7 @@ impl EffectcraftApp { forwarded: ForwardedInput::default(), input_waiters: vec![], job_waiters: vec![], + script_waiters: vec![], pending_screenshots: vec![], shot_bytes: 0, last_activity: (0.0, 0), @@ -96,6 +97,9 @@ impl EffectcraftApp { gpu_failure_applied: None, footage_watcher: None, footage_watch_key: None, + lut_watcher: None, + lut_generation: effectcraft_engine::lut_reload::generation(), + purges: 0, device_lost_close: None, dialog_answers: std::sync::mpsc::channel(), file_dialogs: FileDialogsUp::default(), diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index 8ebea08fc..77e89d552 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -47,6 +47,10 @@ impl EffectcraftApp { self.job_waiters.push((w, reply)); ctx.request_repaint(); } + control::Outcome::AwaitScript(host) => { + self.script_waiters.push((host, reply)); + ctx.request_repaint(); + } control::Outcome::Screenshot { path, target } => { // A covered or minimized window presents no frames (macOS skips its redraws), so // a screenshot would never arrive and nothing would tick its timeout. The @@ -70,11 +74,16 @@ impl EffectcraftApp { if self.job_waiters.is_empty() { return; } - self.session.poll_render(); - self.session.poll_offload(); + let script_busy = self.session.script_ui.render_host.is_some(); + if !script_busy { + self.session.poll_render(); + self.session.poll_offload(); + } let waiters = std::mem::take(&mut self.job_waiters); for (w, reply) in waiters { - if w.pending(self) { + // Completed native snapshots are immutable, so even a later suspended script + // cannot delay their replies. Other completions can edit its open undo group. + if (script_busy && w.render.is_none()) || w.pending(self) { self.job_waiters.push((w, reply)); } else { let v = w.finish(self); @@ -86,6 +95,42 @@ impl EffectcraftApp { } } + /// A suspended script still owns its undo groups. Only its render, inspection and Stop + /// run until the continuation reaches its next real script boundary; no scheduled callback + /// or background analysis may edit the document in the middle of the group. + fn tick_script_render(&mut self, ctx: &egui::Context) { + let Some(host) = self.session.script_ui.render_host else { return }; + let result = match self.session.script_ui.poll_render { + Some(poll) => poll(&mut self.session), + None => { + self.session.script_ui.render_host = None; + Some(json!({"ok": false, "error": {"message": "The script render waiter is unavailable."}})) + } + }; + if let Some(result) = result { + if let Some(message) = result["error"]["message"].as_str() { + self.ui.status = message.to_string(); + } else if let Some(output) = result["output"].as_str().filter(|s| !s.is_empty()) { + self.ui.status = output.lines().last().unwrap_or(output).to_string(); + } + self.script_waiters.retain(|(id, reply)| { + if *id == host { + let _ = reply.send(json!({"ok": true, "result": result})); + false + } else { + true + } + }); + } + // Do not interpret a close as permission to abandon the still-running script. Once + // Stop finishes it, the normal unsaved-changes prompt can handle a new close request. + if ctx.input(|i| i.viewport().close_requested()) { + ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose); + } + self.drain_control(ctx, self.now); + ctx.request_repaint_after(std::time::Duration::from_millis(50)); + } + // ---------------------------------------------------------------- frame /// Apply the engine events drained this tick: open a comp, show a toast, open a URL, run a frontend command, @@ -106,7 +151,11 @@ impl EffectcraftApp { self.ui.status = e; } } - effectcraft_engine::Event::PurgeCaches => self.frames.clear(), + effectcraft_engine::Event::PurgeCaches => { + self.frames.clear(); + // (frames made after it get keys of their own: see `Self::purge_stamp`) + self.purges = self.purges.wrapping_add(1); + } effectcraft_engine::Event::CopyImage { width, height, rgba } => { if let Err(e) = copy_image(ctx, width, height, &rgba) { self.ui.status = e; @@ -136,6 +185,10 @@ impl EffectcraftApp { craft_quit::on_close_request(ctx); return; } + if self.session.script_ui.render_host.is_some() { + self.tick_script_render(ctx); + return; + } // Focus comes from the fresh viewport copy; a full pass's `InputState::focused` is only a // fallback (a logic-only tick leaves it at the previous pass's value). let focused = ctx.input(|i| i.viewport().focused.or(Some(i.focused))); @@ -195,8 +248,20 @@ impl EffectcraftApp { ctx.request_repaint_after(std::time::Duration::from_millis(100)); } self.run_script_tasks(ctx); + if self.session.script_ui.render_host.is_some() { + self.tick_script_render(ctx); + return; + } self.run_dialog_answers(); + if self.session.script_ui.render_host.is_some() { + self.tick_script_render(ctx); + return; + } self.handle_events(ctx); + if self.session.script_ui.render_host.is_some() { + self.tick_script_render(ctx); + return; + } if let Some(rx) = self.command_inbox.take() { while let Ok(id) = rx.try_recv() { if let Err(e) = menus::invoke(self, ctx, &id, json!({})) { @@ -205,6 +270,10 @@ impl EffectcraftApp { } self.command_inbox = Some(rx); } + if self.session.script_ui.render_host.is_some() { + self.tick_script_render(ctx); + return; + } // A close request (the window's button, Quit, `app.quit`) must be answered while hidden. // An answered file dialog can change the project, so the answers above run first. The host // calls `panels::unsaved::quit_on_close` last in its `logic`. @@ -227,6 +296,12 @@ impl EffectcraftApp { self.advance_playback(ctx, scale, now, active, cid); } self.drain_control(ctx, now); + // A clean project may have passed the close guard before a control script created + // unsaved edits and suspended. Cancel that same tick's close before the host handles it. + if self.session.script_ui.render_host.is_some() { + self.tick_script_render(ctx); + return; + } // Screenshot completion, deadlines and issuance are app-side work: the callback only // stores pixels and wakes this viewport. self.tick_screenshots(ctx); @@ -315,7 +390,7 @@ impl EffectcraftApp { /// fonts are ready, draw the frame, finish pointer wrapping, pump queued wasm frames, record input activity, /// publish the completed pass's geometry and pass sequence, acknowledge applied synthetic input and answer /// `AfterInput` replies only after a non-discarded pass. Requests repaints while frames are in flight. - fn draw_inner(&mut self, ui: &mut egui::Ui, frame: &eframe::Frame) { + fn draw_inner(&mut self, ui: &mut egui::Ui, _frame: &eframe::Frame) { let ctx = ui.ctx().clone(); if self.gpu_failure_applied == Some(true) { self.close_after_device_loss(&ctx); @@ -333,7 +408,7 @@ impl EffectcraftApp { self.frame(ui); panels::crypto_pick::upkeep(self, &ctx); #[cfg(not(target_arch = "wasm32"))] - pointer_wrap::finish_native(&ctx, frame); + pointer_wrap::finish_native(&ctx, _frame); // wasm32: no frame threads; render queued frames now, between UI frames. if cfg!(target_arch = "wasm32") && self.frames.pump(std::time::Duration::from_millis(if self.playback.playing { 24 } else { 40 })) { ctx.request_repaint(); @@ -375,6 +450,23 @@ impl EffectcraftApp { /// The panels, menus, dialogs and overlays of one pass. fn frame(&mut self, ui: &mut egui::Ui) { let ctx = ui.ctx().clone(); + if self.session.script_ui.render_host.is_some() { + self.auto.begin_frame(); + ui.vertical_centered(|ui| { + ui.add_space(32.0); + ui.heading("Rendering script"); + ui.label("The script will continue when rendering finishes. Editing is paused to preserve its undo group."); + if let Some(progress) = self.session.render_progress() { + ui.label(format!("{} / {} frames", progress.done, progress.total)); + } + let stop = ui.button("Stop Render"); + self.auto.add("scriptRender.stop", stop.rect, "Stop Render"); + if stop.clicked() { + self.session.stop_render(); + } + }); + return; + } // Only a viewer that draws in *this* pass records a scale: a closed viewer, the Home // screen or another comp must stop idle prefetch at a stale scale. self.last_viewer_scale = None; @@ -566,6 +658,23 @@ impl EffectcraftApp { ctx.request_repaint_after(std::time::Duration::from_secs(10)); } self.tick_footage_reload(ctx); + self.tick_lut_files(ctx); + } + + /// A LUT file in use changed on disk (#3493): the frames kept were rendered with the old one. + /// Renders read it again by themselves; the watcher wakes the interface when no render did. + /// The frame keys carry the generation ([`Self::frame_series`]), so the viewers show the + /// frames rendered again, and a frame still rendering with the old LUT is never asked for. + fn tick_lut_files(&mut self, ctx: &egui::Context) { + if self.lut_watcher.is_none() { + let wake = ctx.clone(); + self.lut_watcher = effectcraft_engine::lut_reload::Watcher::start(move || wake.request_repaint()); + } + let generation = effectcraft_engine::lut_reload::generation(); + if generation != self.lut_generation { + self.lut_generation = generation; + self.frames.clear(); + } } /// One background worker tracks source availability and optional content reloads. It wakes diff --git a/crates/ui-egui/src/app/gpu.rs b/crates/ui-egui/src/app/gpu.rs index c9849e921..92ea7665c 100644 --- a/crates/ui-egui/src/app/gpu.rs +++ b/crates/ui-egui/src/app/gpu.rs @@ -44,6 +44,9 @@ impl EffectcraftApp { pub fn set_gpu_failure_bridge(&mut self, bridge: gpu_failure::GpuFailureBridge) { if let Some(gpu) = &self.gpu { bridge.forward_to(gpu.context().device_failure_notifier()); + if let Some(observer) = bridge.scoped_error_observer() { + gpu.set_scoped_error_observer(observer); + } } self.gpu_failures = Some(bridge); } @@ -257,6 +260,9 @@ impl EffectcraftApp { Ok(Ok(g)) => { if let Some(bridge) = &self.gpu_failures { bridge.forward_to(g.context().device_failure_notifier()); + if let Some(observer) = bridge.scoped_error_observer() { + g.set_scoped_error_observer(observer); + } if bridge.failure().is_some() { return; } diff --git a/crates/ui-egui/src/app/playback.rs b/crates/ui-egui/src/app/playback.rs index d40bc69ff..c6cb5d577 100644 --- a/crates/ui-egui/src/app/playback.rs +++ b/crates/ui-egui/src/app/playback.rs @@ -35,15 +35,20 @@ impl EffectcraftApp { plan: Some(plan), caching: plan.cache_first && plan.video, res: preset.resolution.factor(preset.custom_resolution), + adaptive: None, overlays: preset.include_overlays, layer_controls: preset.include_layer_controls, restore_max, audio_frame: (!plan.video).then_some(plan.first), audio_held: false, + silent_project: None, starting: false, starting_since: now, first_cached: None, shown_at: Default::default(), + pace: 0.0, + pace_acc: None, + paced_at: None, }; if !self.playback.caching { if plan.video { @@ -56,12 +61,10 @@ impl EffectcraftApp { } } - /// The preview includes audio at the comp's frame rate and the comp has something audible. - fn audio_plays(&self) -> bool { + /// The preview includes audio and the comp has something audible. + pub(super) fn audio_plays(&self) -> bool { let (Some(pl), Some(cid)) = (self.playback.plan, self.session.active_comp_id()) else { return false }; - let Some(c) = self.session.project.comp(cid) else { return false }; - // A preview at another frame rate plays slower or faster than real time: silent. - pl.audio && (pl.fps - c.frame_rate.as_f64()).abs() <= 0.01 && effectcraft_engine::render::audio::comp_has_audio(&self.session.project, cid) + pl.audio && effectcraft_engine::render::audio::comp_has_audio(&self.session.project, cid) } /// Start audio preview from comp time `t` when [`Self::audio_plays`] and an output device @@ -75,7 +78,17 @@ impl EffectcraftApp { let Some(dev) = self.hooks.audio_device.as_ref().and_then(|f| f(&out)) else { return }; let mix = self.session.prefs.audio.preview_sample_rate; let fr = c.frame_rate; - match audio::AudioPlayback::start(dev, self.render_source(), cid, t, fr.tick_of(pl.start), fr.tick_of(pl.end + 1), pl.looping, mix) { + match audio::AudioPlayback::start_at_speed( + dev, + self.render_source(), + cid, + t, + fr.tick_of(pl.start), + fr.tick_of(pl.end + 1), + pl.looping, + mix, + pl.fps / fr.as_f64(), + ) { Ok(a) => self.audio = Some(a), Err(e) => log::warn!("audio preview: {e}"), } diff --git a/crates/ui-egui/src/app/prefetch.rs b/crates/ui-egui/src/app/prefetch.rs index 50638b708..37486eb6d 100644 --- a/crates/ui-egui/src/app/prefetch.rs +++ b/crates/ui-egui/src/app/prefetch.rs @@ -1,4 +1,4 @@ -use super::{EffectcraftApp, FrameKey, FrameWindow, ItemId, PlaybackPass, audio, frames}; +use super::{EffectcraftApp, FrameKey, FrameWindow, ItemId, PlaybackPass, frames}; /// Seconds without a project change before paused prefetch resumes (see /// [`EffectcraftApp::editing`]). @@ -35,6 +35,66 @@ fn between_picture_and_sound(cur: i64, f: i64, target: i64, looping: bool) -> bo if target >= cur || !looping { cur < f && f < target } else { f > cur || f < target } } +/// How long the pace takes to follow the rate frames arrive at (seconds, the time constant of +/// its smoothing): short enough to follow a change, long enough to play through a batch of +/// frames finishing together. +const PACE_TAU: f64 = 0.5; + +/// The rate a preview plays at when frames arrive slower than its frame rate: proportional to +/// the cached frames ahead (`lead` of `target`), so it settles where the clock takes frames as +/// fast as they arrive and the lead stays put; smoothed over [`PACE_TAU`] (`dt` seconds since +/// the last pass). Never above the frame rate, never under an eighth of it (slower reads as +/// stopped: the clock holds instead). No frame ahead: the previous pace, the clock holds anyway. +fn pace_for(prev: f64, lead: i64, target: i64, fps: f64, dt: f64) -> f64 { + if lead <= 0 || target <= 0 || fps <= 0.0 { + return prev; + } + let raw = (fps * lead as f64 / target as f64).clamp(fps / 8.0, fps); + let k = 1.0 - (-dt.max(0.0) / PACE_TAU).exp(); + prev + (raw - prev) * k +} + +/// How many frames after the current one the pace measures its lead over: twice the prefetch +/// depth `ahead`, but only frames the RAM preview can keep cached together with the current one. +/// `prefetch_playback` requests offsets `0..fit_ahead` from the current frame (the current frame +/// and `fit_ahead - 1` after it), so a lead counted over more frames than that could never fill: +/// a preview that renders every frame at once would still be slowed down to a fraction of its +/// frame rate. 0 when not even one frame after the current one is prefetched: the pace can't +/// measure anything, and the clock runs in real time (holding on frames that aren't cached). +fn pace_horizon(ahead: i64, fit_ahead: i64) -> i64 { + (ahead * 2).max(1).min(fit_ahead - 1).max(0) +} + +/// Cached frames (`cached`, sorted) among the next `max` frames `after(1..=max)` plays, and how +/// many frames it plays (`after` is `None` past the end). Counted, not the consecutive run: one +/// late frame (a GPU warm-up frame, a heavy one) with frames cached beyond it holds the clock +/// for a moment when it gets there, rather than slowing the whole preview to a crawl. +fn lead_of(after: impl Fn(i64) -> Option, cached: &[i64], max: i64) -> (i64, i64) { + let ahead: Vec = (1..=max).map_while(after).collect(); + (ahead.iter().filter(|f| cached.binary_search(f).is_ok()).count() as i64, ahead.len() as i64) +} + +impl PlaybackPass<'_> { + /// The frame `k` played frames after `from` (wrapping as the preview loops), `None` once a + /// preview that doesn't loop has reached its end. + fn frame_after(&self, from: i64, k: i64) -> Option { + match self.plan { + Some(p) => p.frame_after(from, k), + None => { + let FrameWindow { first: wa, last: wb } = self.window; + let f = from + k; + if f <= wb { + Some(f) + } else if self.looping { + Some(wa + (f - wa).rem_euclid((wb - wa + 1).max(1))) + } else { + None + } + } + } + } +} + impl EffectcraftApp { /// RAM preview: advance at the preview's frame rate when frames are cached; otherwise hold /// and keep rendering ahead (the green cache bar fills, then playback runs in real time). @@ -82,6 +142,7 @@ impl EffectcraftApp { let step = plan.map_or(1, |p| p.step); let fit_ahead = self.frames_that_fit(&c, scale) - 1; self.prefetch_playback(&PlaybackPass { cur: lead, ..pass }, fit_ahead); + self.wake_silent_preview(); self.pump_meters(ctx, now); if !self.playback.playing { self.cache_when_idle(ctx, &c, &series, now, input_active, (wa, wb)); @@ -95,7 +156,7 @@ impl EffectcraftApp { if self.advance_audio_clock(&pass, step) { return; } - self.advance_frame_clock(&pass, step); + self.advance_frame_clock(&pass, step, fit_ahead); } /// While paused (scrubbing, editing) the viewer's frame comes first: prefetch only a few @@ -201,6 +262,10 @@ impl EffectcraftApp { /// one second. With no source but an active meter, drive the meter to silence and request a repaint. fn pump_meters(&mut self, ctx: &egui::Context, now: f64) { if let Some(a) = &mut self.audio { + // An edit while playing (mute, unmute, solo, levels): the sound follows it (#2104). + if a.project().is_some_and(|p| !std::sync::Arc::ptr_eq(&p, &self.session.project)) { + a.follow_project(self.session.project.clone()); + } a.pump(); self.meter.update(a.feed.take_peaks(), now); } else if let Some(s) = &mut self.scrub { @@ -259,6 +324,29 @@ impl EffectcraftApp { false } + /// A silent preview whose project was edited so that it has sound now (a layer unmuted while + /// playing, #2104): video previews wait for cached frames; audio-only previews start from + /// their moving audio frame, not the stationary timeline. Checked once per project edit. + fn wake_silent_preview(&mut self) { + let pb = &self.playback; + if self.audio.is_some() || !pb.playing || pb.caching || pb.starting || pb.audio_held { + return; + } + if pb.silent_project.as_ref().is_some_and(|p| std::sync::Arc::ptr_eq(p, &self.session.project)) { + return; + } + self.playback.silent_project = Some(self.session.project.clone()); + if !self.audio_plays() { + return; + } + if self.playback.plan.is_some_and(|p| p.video) { + self.playback.audio_held = true; + } else if let Some(c) = self.session.active_comp() { + let frame = self.playback.audio_frame.unwrap_or(self.playback.start_frame); + self.start_audio(c.frame_rate.tick_of(frame)); + } + } + /// Held sound starts once the frames ahead are cached (see `Playback::audio_held`): the /// prefetch's `ahead` when the silent preview keeps real time, else the rest of the preview. fn start_held_audio(&mut self, pass: &PlaybackPass<'_>, fit: i64) { @@ -293,7 +381,7 @@ impl EffectcraftApp { return true; } let snap = |f: i64| wa + (f - wa).div_euclid(step) * step; - let target = snap(audio::frame_of_sample(a.clock(), a.rate, fr).clamp(wa, wb)); + let target = snap(fr.frame_at(a.clock_time()).clamp(wa, wb)); if !video { self.playback.audio_frame = Some(target); } else if self.frames.is_cached(&FrameKey { frame: target, ..series }) { @@ -312,6 +400,8 @@ impl EffectcraftApp { self.session.set_time(fr.tick_of(f)); self.playback.frame_shown(now); } + // Dropping frames: a lower resolution may keep up (the rate shown says so). + self.step_down(now, pass.scale); } ctx.request_repaint(); true @@ -322,7 +412,7 @@ impl EffectcraftApp { fn sound_frame(&self, fr: effectcraft_engine::time::FrameRate, wa: i64, wb: i64, step: i64) -> Option { let a = self.audio.as_ref()?; let step = step.max(1); - Some(wa + (audio::frame_of_sample(a.clock(), a.rate, fr).clamp(wa, wb) - wa).div_euclid(step) * step) + Some(wa + (fr.frame_at(a.clock_time()).clamp(wa, wb) - wa).div_euclid(step) * step) } /// The last cached frame after the current one and before `target`, in play order: what a @@ -346,29 +436,47 @@ impl EffectcraftApp { } /// The wall clock advances the preview when no audio device drives it, and shows the target - /// frame (or holds when it is not cached yet). - fn advance_frame_clock(&mut self, pass: &PlaybackPass<'_>, step: i64) { - let PlaybackPass { ctx, now, plan, video, looping, frame_rate: fr, cur, series, .. } = *pass; - let FrameWindow { first: wa, last: wb } = pass.window; - let elapsed = now - self.playback.start_wall; + /// frame (or holds when it is not cached yet). While frames arrive slower than the frame + /// rate it runs at their pace ([`pace_for`]): every frame shows, evenly, as After Effects + /// plays a preview it hasn't cached yet; real time again once the frames ahead are cached. + fn advance_frame_clock(&mut self, pass: &PlaybackPass<'_>, step: i64, fit_ahead: i64) { + let PlaybackPass { ctx, now, plan, video, frame_rate: fr, cur, series, .. } = *pass; + let FrameWindow { last: wb, .. } = pass.window; // The plan carries the rate the preview was started with (and `play_with` copies it to // `Playback::fps`). Deriving the frame from the plan keeps a logic-only tick correct even // when a host/test supplies only the plan; a stale duplicate cannot hold playback open. let fps = plan.map_or(self.playback.fps, |p| p.fps); - let n = ((elapsed * fps) / step as f64).floor() as i64; - let target = match plan { - Some(p) => p.frame_after(self.playback.start_frame, n), - None => { - let t = self.playback.start_frame + n; - if t <= wb { - Some(t) - } else if looping { - Some(wa + (t - wa).rem_euclid((wb - wa + 1).max(1))) - } else { - None - } + let dt = self.playback.paced_at.map_or(0.0, |t| (now - t).max(0.0)); + self.playback.paced_at = Some(now); + if video { + // The lead counts out of twice the prefetch depth: the prefetch keeps that many + // frames coming (or the frames left before the end), so a full lead means rendering + // keeps up. No more than the RAM preview holds ([`pace_horizon`]). + let horizon = pace_horizon(pass.ahead, fit_ahead); + let prev = if self.playback.pace > 0.0 { self.playback.pace } else { fps }; + let pace = if horizon > 0 { + let (lead, target) = self.cached_lead(pass, horizon); + pace_for(prev, lead, target, fps, dt) + } else { + fps + }; + self.playback.pace = pace; + if pace < fps * 0.8 { + self.step_down(now, pass.scale); } - }; + if pace < fps * 0.98 { + self.advance_paced(pass, pace, dt); + return; + } + if self.playback.pace_acc.take().is_some() { + // Back to real time from here. + self.playback.start_wall = now; + self.playback.start_frame = cur; + } + } + let elapsed = now - self.playback.start_wall; + let n = ((elapsed * fps) / step as f64).floor() as i64; + let target = pass.frame_after(self.playback.start_frame, n); let Some(target) = target else { if video { self.session.set_time(fr.tick_of(wb)); @@ -393,7 +501,8 @@ impl EffectcraftApp { } else { // Not cached yet: hold the clock at the current frame (cache first, then play). self.playback.waiting = true; - let next = plan.and_then(|p| p.frame_after(cur, 1)).unwrap_or(if cur + 1 > wb { wa } else { cur + 1 }); + self.step_down(now, pass.scale); + let next = pass.frame_after(cur, 1).unwrap_or(cur); if self.frames.is_cached(&FrameKey { frame: next, ..series }) { self.session.set_time(fr.tick_of(next)); } @@ -403,6 +512,74 @@ impl EffectcraftApp { ctx.request_repaint(); } + /// The paced clock: it owes `pace` played frames a second (plus what the last passes left + /// over) and shows them one by one as far as they are cached; on a frame that isn't it holds + /// (owing one frame, so that frame shows the moment it arrives), and at the end of a preview + /// that doesn't loop it stops. + fn advance_paced(&mut self, pass: &PlaybackPass<'_>, pace: f64, dt: f64) { + let PlaybackPass { ctx, now, frame_rate: fr, cur, series, .. } = *pass; + let step = pass.plan.map_or(1, |p| p.step).max(1) as f64; + let mut owed = self.playback.pace_acc.unwrap_or(0.0) + dt * pace / step; + let mut f = cur; + while owed >= 1.0 { + match pass.frame_after(f, 1) { + Some(n) if self.frames.is_cached(&FrameKey { frame: n, ..series }) => { + f = n; + owed -= 1.0; + } + Some(_) => { + owed = 1.0; + break; + } + None => { + self.session.set_time(fr.tick_of(pass.window.last)); + self.stop(); + return; + } + } + } + self.playback.waiting = owed >= 1.0; + if f != cur { + self.session.set_time(fr.tick_of(f)); + self.playback.frame_shown(now); + } + self.playback.pace_acc = Some(owed); + ctx.request_repaint(); + } + + /// Cached frames among the next `max` in play order, and how many of those there are (fewer + /// near the end of a preview that doesn't loop: its cached tail is a full lead). See + /// [`lead_of`]. + fn cached_lead(&self, pass: &PlaybackPass<'_>, max: i64) -> (i64, i64) { + lead_of(|k| pass.frame_after(pass.cur, k), &self.frames.cached_frames(&pass.series), max) + } + + /// Settings ▸ Previews ▸ Lower Resolution When Playback Can't Keep Up: a preview at + /// Resolution Auto plays under 80 % of its frame rate (its pace, or stalled on a frame that + /// isn't cached) two seconds after it started or last stepped down (the first frames at a + /// new scale, and the GPU's warm-up frames, take a moment), so it plays on at half `scale`, + /// no lower than the Adaptive Resolution Limit. An explicit Resolution is kept. + // ponytail: it never steps back up during a play, and frames cached at the higher scale aren't + // shown once it stepped down; add both if loops over a partly cached comp need them. + fn step_down(&mut self, now: f64, scale: f64) { + let pb = &self.playback; + let since = pb.adaptive.map_or(pb.starting_since, |(_, t)| t); + let step = pb.plan.map_or(1, |p| p.step).max(1) as f64; + let lower = scale * 0.5; + if !self.session.prefs.previews.adaptive_playback + || pb.res.is_some() + || !self.viewer_res().is_auto() + || now - since < 2.0 + // (a rate from before a stall: nothing shown for a second isn't keeping up) + || (pb.shown_at.back().is_some_and(|t| now - t <= 1.0) && pb.achieved_fps().is_some_and(|f| f * step >= pb.fps * 0.8)) + || lower < self.session.prefs.adaptive_limit() - 1e-9 + { + return; + } + self.playback.adaptive = Some((lower, now)); + self.playback.shown_at.clear(); + } + /// While the project keeps changing (an edit within [`EDIT_QUIET`] seconds), how long until /// it counts as settled. fn editing(&mut self, now: f64) -> Option { @@ -493,13 +670,68 @@ impl EffectcraftApp { side(comp.width) * side(comp.height) * frames::frame_pixel_bytes(&self.session.project.settings) as f64 } + /// Frames rendered at once by a preview or idle caching: the lanes (in the browser the frame + /// workers; without them frames render one at a time on the UI thread, `Frames::pump`). fn frames_parallelism(&self) -> usize { - if cfg!(target_arch = "wasm32") { - // frame workers render one frame each; without them frames render one at a time - // on the UI thread (`Frames::pump`) - return self.frames.remote_slots().max(1); - } - self.frames.parallelism() + self.frames.lanes() + } +} + +#[cfg(test)] +mod pace_tests { + use super::{lead_of, pace_for, pace_horizon}; + + /// The lead is measured over no more frames than the RAM preview keeps ahead of the current + /// one, so a full cache plays in real time. (It counted out of twice the prefetch depth: with + /// room for 4 frames, at most 2 after the current one were ever cached, and a preview that + /// rendered at once played at a quarter of its frame rate.) + #[test] + fn the_pace_horizon_fits_in_the_ram_preview() { + // A roomy budget: twice the prefetch depth. + assert_eq!(pace_horizon(6, 400), 12); + // Room for 4 frames: the prefetch requests offsets 0..3, so 2 after the current one. + assert_eq!(pace_horizon(6, 3), 2); + let (lead, target) = lead_of(|k| Some(10 + k), &[10, 11, 12], pace_horizon(6, 3)); + assert!((pace_for(30.0, lead, target, 30.0, 10.0) - 30.0).abs() < 1e-6, "a full cache plays in real time"); + // Room for the current frame only (or nothing): no lead to measure. + assert_eq!(pace_horizon(6, 1), 0); + assert_eq!(pace_horizon(6, 0), 0); + // No prefetch depth: still the next frame. + assert_eq!(pace_horizon(0, 400), 1); + } + + /// The pace follows the cached lead: real time with a full lead, a third of it with a third, + /// never under an eighth, unchanged with nothing ahead, and smoothed over time. + #[test] + fn pace_follows_the_cached_lead() { + // Settled (a long dt): proportional to the lead. + assert!((pace_for(30.0, 12, 12, 30.0, 10.0) - 30.0).abs() < 1e-6); + assert!((pace_for(30.0, 4, 12, 30.0, 10.0) - 10.0).abs() < 1e-6); + assert!((pace_for(30.0, 1, 100, 30.0, 10.0) - 3.75).abs() < 1e-6, "never under an eighth"); + assert_eq!(pace_for(17.0, 0, 12, 30.0, 10.0), 17.0, "no frame ahead: unchanged"); + // Smoothing: after one time constant it has gone 63 % of the way. + let p = pace_for(30.0, 4, 12, 30.0, 0.5); + assert!((p - (30.0 - 20.0 * (1.0 - (-1.0f64).exp()))).abs() < 1e-6); + assert_eq!(pace_for(30.0, 4, 12, 30.0, 0.0), 30.0, "no time passed: unchanged"); + assert!(pace_for(10.0, 12, 12, 30.0, 0.5) > 10.0, "and up again as the lead fills"); + } + + /// Near the end of a preview that doesn't loop, the lead counts out of the frames left: a + /// cached tail plays in real time. (It counted out of the full depth, and a cached preview + /// slowed down over its last frames, a short one for its whole length.) + #[test] + fn a_cached_end_is_a_full_lead() { + let cached: Vec = (0..=20).collect(); + let to_20 = |cur: i64| move |k: i64| Some(cur + k).filter(|f| *f <= 20); + assert_eq!(lead_of(to_20(5), &cached, 12), (12, 12)); + assert_eq!(lead_of(to_20(16), &cached, 12), (4, 4)); + assert_eq!(lead_of(to_20(20), &cached, 12), (0, 0)); + let (lead, target) = lead_of(to_20(16), &cached, 12); + assert!((pace_for(30.0, lead, target, 30.0, 10.0) - 30.0).abs() < 1e-6); + // Frames left uncached still count against it. + assert_eq!(lead_of(to_20(16), &[17, 19], 12), (2, 4)); + // Looping: the walk wraps, always `max` frames. + assert_eq!(lead_of(|k| Some((18 + k) % 21), &cached, 12), (12, 12)); } } diff --git a/crates/ui-egui/src/app/viewer_frames.rs b/crates/ui-egui/src/app/viewer_frames.rs index a1a0faaf5..d9e6e84f2 100644 --- a/crates/ui-egui/src/app/viewer_frames.rs +++ b/crates/ui-egui/src/app/viewer_frames.rs @@ -98,6 +98,13 @@ impl EffectcraftApp { // play back, and frames cached by a preview stay on screen when it stops. let res = self.viewer_res(); let full = res.scale(zoom, ppp); + // A preview that couldn't keep up stepped down (`step_down`). + if self.playback.playing + && res.is_auto() + && let Some((s, _)) = self.playback.adaptive + { + return full.min(s); + } // Fast Previews ▸ Adaptive Resolution / Fast Draft: lower resolution while dragging. let (_, k) = self.session.state.viewer.fast_previews.render(self.ui.viewer.interacting || self.ui.viewer.property_interacting); if k < 1.0 && res.is_auto() { @@ -121,7 +128,7 @@ impl EffectcraftApp { frame: 0, scale: (scale * 1000.0).round() as u32, view: self.view_hash(comp), - opts: frames::opts_hash(&self.frame_opts(comp, scale)) ^ self.roto_model_stamp(), + opts: frames::opts_hash(&self.frame_opts(comp, scale)) ^ self.roto_model_stamp() ^ self.lut_stamp() ^ self.purge_stamp(), } } @@ -135,6 +142,21 @@ impl EffectcraftApp { h.finish() | 1 } + /// Changes when a LUT file in use changed on disk (#3493): the frame on screen and the ones + /// still rendering were made with the old LUT, so the new ones must not share their keys + /// (the viewers show a frame again only when its key changes). 0 until a LUT file changes. + fn lut_stamp(&self) -> u64 { + self.lut_generation.wrapping_mul(0x9e37_79b9_7f4a_7c15) + } + + /// Changes when the caches are purged (Event::PurgeCaches: footage reloaded, Purge): the + /// frames on screen and the ones still rendering may show the files as they were, and a reload + /// that changes no metadata keeps the project's revision, so the new frames must not share + /// their keys (#3539; the viewers show a frame again only when its key changes). 0 until then. + fn purge_stamp(&self) -> u64 { + self.purges.wrapping_mul(0xc2b2_ae3d_27d4_eb4f) + } + /// The frame series the viewer of `comp` shows now (its scale, view and options, for the /// comp's current content): what the cache bars count. pub fn shown_series(&self, comp: ItemId) -> FrameKey { @@ -231,3 +253,94 @@ impl EffectcraftApp { request(&self.frames, &self.render_source(), key, comp, t, opts); } } + +#[cfg(test)] +mod tests { + /// #3493: the viewer shows the frame again once the LUT file it was graded with changes, + /// though its key was otherwise the same (same project revision, frame and options). + #[test] + fn the_viewer_shows_a_lut_file_changed_on_disk() { + let dir = tempfile::tempdir().unwrap(); + let lut = dir.path().join("viewer.cube"); + let write = |text: &str, secs: u64| { + std::fs::write(&lut, text).unwrap(); + let f = std::fs::File::options().write(true).open(&lut).unwrap(); + f.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs)).unwrap(); + }; + write("LUT_1D_SIZE 2\n0 0 0\n0 0 0\n", 1_000); + let mut session = effectcraft_engine::Session::default(); + session.execute("comp.new", serde_json::json!({"width": 64, "height": 36, "duration": 2})).unwrap(); + session.execute("layer.newSolid", serde_json::json!({"color": "#808080"})).unwrap(); + session.execute("effect.apply", serde_json::json!({"layer": 1, "effect": "Apply Color LUT"})).unwrap(); + session.execute("prop.set", serde_json::json!({"layer": 1, "path": "effects/#1/lut", "value": lut.to_string_lossy()})).unwrap(); + let mut harness = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_eframe(|_| crate::EffectcraftApp::new(session)); + let centre = |h: &egui_kittest::Harness<'_, crate::EffectcraftApp>| { + h.state().viewer_image.as_ref().map(|img| img.pixels[img.pixels.len() / 2 + img.size[0] / 2].r()) + }; + let settle = |h: &mut egui_kittest::Harness<'_, crate::EffectcraftApp>, want: u8| { + for _ in 0..400 { + h.step(); + if centre(h) == Some(want) { + return; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + panic!("the viewer shows {:?}, not {want}", centre(h)); + }; + settle(&mut harness, 0); + write("LUT_1D_SIZE 2\n1 1 1\n1 1 1\n", 2_000); + // (what the app's watcher does every two seconds) + effectcraft_engine::lut_reload::check(); + settle(&mut harness, 255); + } + + /// #3539: a still re-saved at the same size and reloaded as changed on disk keeps the project + /// as it was (same revision and content key); the viewers showed the old picture, the new + /// frame having the key of the one on screen. A reload's purge gives the frames new keys, once. + #[test] + fn the_viewers_show_a_still_reloaded_at_the_same_size() { + let dir = tempfile::tempdir().unwrap(); + let png = dir.path().join("still.png"); + let save = |rgb: [u8; 3]| image::RgbaImage::from_pixel(64, 36, image::Rgba([rgb[0], rgb[1], rgb[2], 255])).save(&png).unwrap(); + save([255, 0, 0]); + let mut session = effectcraft_host::session(); + session.execute("comp.new", serde_json::json!({"width": 64, "height": 36, "duration": 2})).unwrap(); + let cid = session.active_comp_id().unwrap(); + let item = session.execute("file.import", serde_json::json!({"paths": [png.to_string_lossy()]})).unwrap()["items"][0].clone(); + session.execute("layer.addItem", serde_json::json!({"item": item})).unwrap(); + let mut harness = egui_kittest::Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_eframe(|_| crate::EffectcraftApp::new(session)); + let centre = |h: &egui_kittest::Harness<'_, crate::EffectcraftApp>| { + h.state().viewer_image.as_ref().map(|img| img.pixels[img.pixels.len() / 2 + img.size[0] / 2].r()) + }; + let settle = |h: &mut egui_kittest::Harness<'_, crate::EffectcraftApp>, want: u8| { + for _ in 0..400 { + h.step(); + if centre(h) == Some(want) { + return; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + panic!("the viewer shows {:?}, not {want}", centre(h)); + }; + settle(&mut harness, 255); + // (every viewer, passive ones too, asks for its frame by this key) + let key = |h: &egui_kittest::Harness<'_, crate::EffectcraftApp>| h.state().frame_key(cid, 0, 1.0); + let before = key(&harness); + harness.step(); + assert_eq!(key(&harness), before, "no purge, the same key: nothing is uploaded again"); + // Reload Footage Changed on Disk: the app's watcher saw the file's stamp move. + let path = png.to_string_lossy().into_owned(); + let stamps = |n| vec![(path.clone(), Some((n, u128::from(n))))]; + effectcraft_engine::footage_reload::apply_stamps(&mut harness.state_mut().session, stamps(1)).unwrap(); + save([0, 0, 255]); + let revision = harness.state().session.revision; + let reloaded = effectcraft_engine::footage_reload::apply_stamps(&mut harness.state_mut().session, stamps(2)).unwrap(); + assert_eq!(reloaded["files"], 1, "{reloaded}"); + assert_eq!(harness.state().session.revision, revision, "the project is as it was"); + settle(&mut harness, 0); + let after = key(&harness); + assert_ne!(after, before, "the reloaded frame has a key of its own"); + harness.step(); + assert_eq!(key(&harness), after, "one new key per purge"); + } +} diff --git a/crates/ui-egui/src/audio.rs b/crates/ui-egui/src/audio.rs index fbfe4b8af..e0d6b4d1b 100644 --- a/crates/ui-egui/src/audio.rs +++ b/crates/ui-egui/src/audio.rs @@ -10,7 +10,7 @@ //! **A/V sync:** while audio plays, the audio clock drives playback. The clock is the number of //! sample frames the device has actually consumed (underruns do not advance it) minus the //! device's output latency, mapped through the work-area loop ([`clock_sample`]); the viewer -//! shows the comp frame at that sample ([`frame_of_sample`]), dropping video frames rather +//! shows the comp frame at that time ([`AudioPlayback::clock_time`]), dropping video frames rather //! than drifting. //! //! The feed also tracks per-channel peaks for the Audio panel's VU meters ([`Meter`]). @@ -18,16 +18,19 @@ //! **Scrubbing:** Ctrl/Cmd-dragging the current-time indicator plays a short snippet of the mix //! at each new frame ([`AudioScrub`], `playback.scrubAudio`), as in After Effects. -use std::collections::VecDeque; +use std::collections::{HashSet, VecDeque}; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use std::thread::JoinHandle; -use effectcraft_engine::project::ItemId; +use effectcraft_engine::project::{GroupKind, ItemId, ItemKind, Layer, LayerSource, Project, PropGroup}; use effectcraft_engine::time::{FrameRate, Tick}; use crate::frames::RenderSource; +mod preview_rate; +use preview_rate::PreviewRate; + /// An audio output (implemented by the native app with cpal). pub trait AudioDevice: Send { /// Device sample rate in Hz. @@ -88,9 +91,11 @@ impl AudioFeed { pk[i & 1] = pk[i & 1].max(s.abs()); } real = n; + // Queue removal and the clock advance are one handoff: an edit must not restart + // behind samples already handed to the device. + self.consumed.fetch_add((real / 2) as u64, Ordering::Relaxed); } out[real..].iter_mut().for_each(|o| *o = 0.0); - self.consumed.fetch_add((real / 2) as u64, Ordering::Relaxed); for (c, v) in pk.into_iter().enumerate() { // Rust 1.99 renamed `fetch_update` to `try_update`; keep the old name while the // workspace supports Rust 1.95 (`rust-version`). @@ -106,6 +111,14 @@ impl AudioFeed { } } + /// Discard an old mix and snapshot the first unconsumed frame under the same queue lock + /// as [`Self::pull`], so a concurrent device callback cannot leave the cursor behind. + fn discard_pending(&self) -> Option { + let mut q = self.queue.lock().ok()?; + q.clear(); + Some(self.consumed()) + } + pub fn push(&self, samples: &[f32]) { if let Ok(mut q) = self.queue.lock() { q.extend(samples.iter().copied()); @@ -131,7 +144,7 @@ impl AudioFeed { } } -/// What a preview plays, in comp sample indices at the device rate. +/// What a preview plays, in output sample coordinates on its playback timeline. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct PlaySpan { pub start: i64, @@ -149,15 +162,23 @@ impl PlaySpan { } } -/// The comp sample index audible now: `consumed` frames played by the device minus its +/// The output sample coordinate audible now: `consumed` frames played by the device minus its /// `latency`, from the span start, wrapped into the work area when looping (clamped at its end /// otherwise). pub fn clock_sample(span: &PlaySpan, consumed: u64, latency: u64) -> i64 { - let s = span.start + consumed.saturating_sub(latency) as i64; - if s < span.wb { - return s; + // Slow previews can span more output samples than fit in a signed difference. Keep the + // elapsed count and loop width wide until the result is back inside the span. + let s = i128::from(span.start) + i128::from(consumed.saturating_sub(latency)); + if s < i128::from(span.wb) { + return s as i64; + } + if span.looping { + let width = (i128::from(span.wb) - i128::from(span.wa)).max(1); + let wrapped = i128::from(span.wa) + (s - i128::from(span.wb)).rem_euclid(width); + wrapped.clamp(i64::MIN as i128, i64::MAX as i128) as i64 + } else { + span.wb.saturating_sub(1) } - if span.looping { span.wa + (s - span.wb).rem_euclid((span.wb - span.wa).max(1)) } else { span.wb - 1 } } /// Comp time of sample index `s` at `rate` Hz. @@ -179,10 +200,23 @@ pub struct AudioPlayback { device: Box, stop: Arc, thread: Option>, - /// wasm32 (no threads): what the feeder thread would own, run by [`AudioPlayback::pump`]. - feeder: Option<(RenderSource, ItemId, i64, u32)>, + /// What the mixer reads and advances: the feeder thread, or [`AudioPlayback::pump`] on wasm32 + /// (no threads). + state: Arc>, + comp: ItemId, + mix: u32, pub rate: u32, pub span: PlaySpan, + timeline: PreviewRate, +} + +/// The mixer's position and its view of the project. +struct Feeder { + src: RenderSource, + /// The next output sample index to mix (mapped to composition time by the preview rate). + cursor: i64, + /// Bumped when the project changes: a block mixed from an older project is dropped. + generation: u64, } /// Audio kept queued ahead of the device: ~200 ms (a feeder thread tops it up continuously); @@ -197,7 +231,7 @@ impl AudioPlayback { /// at (resampled to the device's rate when they differ; 0 = the device's rate). #[allow(clippy::too_many_arguments)] pub fn start( - mut device: Box, + device: Box, src: RenderSource, comp: ItemId, start: Tick, @@ -205,15 +239,34 @@ impl AudioPlayback { wb: Tick, looping: bool, mix_rate: u32, + ) -> Result { + Self::start_at_speed(device, src, comp, start, wa, wb, looping, mix_rate, 1.0) + } + + /// Start at a preview speed relative to composition time. Both the sound (including pitch) + /// and its composition clock follow this speed; the physical device rate stays unchanged. + #[allow(clippy::too_many_arguments)] + pub fn start_at_speed( + mut device: Box, + src: RenderSource, + comp: ItemId, + start: Tick, + wa: Tick, + wb: Tick, + looping: bool, + mix_rate: u32, + speed: f64, ) -> Result { let rate = device.sample_rate().max(8000); let mix = if mix_rate == 0 { rate } else { mix_rate.clamp(8000, 192_000) }; - let span = PlaySpan::new(start, wa, wb, looping, rate); + let timeline = PreviewRate::new(rate, speed, start)?; + let span = timeline.span(start, wa, wb, looping); let feed = Arc::new(AudioFeed::default()); let stop = Arc::new(AtomicBool::new(false)); + let state = Arc::new(Mutex::new(Feeder { src, cursor: span.start, generation: 0 })); if cfg!(target_arch = "wasm32") { // The device takes samples only in `pump`, which fills the queue first. - let mut p = AudioPlayback { feed: feed.clone(), device, stop, thread: None, feeder: Some((src, comp, span.start, mix)), rate, span }; + let mut p = AudioPlayback { feed: feed.clone(), device, stop, thread: None, state, comp, mix, rate, span, timeline }; p.device.start(feed)?; p.pump(); return Ok(p); @@ -223,15 +276,14 @@ impl AudioPlayback { // as that took (#408). The device plays silence until the first block arrives, and the // clock doesn't count it. let thread = { - let (feed, stop) = (feed.clone(), stop.clone()); + let (feed, stop, state) = (feed.clone(), stop.clone(), state.clone()); std::thread::Builder::new() .name("ec-audio-feed".into()) .spawn(move || { - let mut cursor = span.start; let target = queue_target(rate); while !stop.load(Ordering::Relaxed) { if feed.queued_frames() < target && !feed.finished.load(Ordering::Relaxed) { - cursor = feed_block(&src, comp, &feed, &span, cursor, rate, mix); + feed_block(&state, comp, &feed, &span, timeline, mix); } else { std::thread::sleep(std::time::Duration::from_millis(4)); } @@ -240,24 +292,63 @@ impl AudioPlayback { .map_err(|e| e.to_string())? }; device.start(feed.clone())?; - Ok(AudioPlayback { feed, device, stop, thread: Some(thread), feeder: None, rate, span }) + Ok(AudioPlayback { feed, device, stop, thread: Some(thread), state, comp, mix, rate, span, timeline }) } /// Every UI frame while playing: on wasm32 mix ahead into the feed (what the feeder thread /// does elsewhere), then let the device take what it needs. pub fn pump(&mut self) { - if let Some((src, comp, cursor, mix)) = &mut self.feeder { + if cfg!(target_arch = "wasm32") { let target = queue_target(self.rate); while self.feed.queued_frames() < target && !self.feed.finished.load(Ordering::Relaxed) { - *cursor = feed_block(src, *comp, &self.feed, &self.span, *cursor, self.rate, *mix); + feed_block(&self.state, self.comp, &self.feed, &self.span, self.timeline, self.mix); } } self.device.pump(); } - /// The comp sample index audible now. + /// The project the sound is mixed from. + pub fn project(&self) -> Option> { + self.state.lock().ok().map(|s| s.src.project.clone()) + } + + /// Mix from `project` from now on (the project was edited while playing, e.g. a layer's Audio + /// switch, #2104): what is queued came from the old project, so it is dropped and mixing + /// resumes at the first sample the device has not taken yet (also when the feeder had + /// already mixed up to the end of a non-looping span). + pub fn set_project(&mut self, project: Arc) { + let Ok(mut s) = self.state.lock() else { return }; + let Some(consumed) = self.feed.discard_pending() else { return }; + s.src.project = project; + s.generation = s.generation.wrapping_add(1); + self.feed.finished.store(false, Ordering::Relaxed); + // The display clock clamps to the last *audible* sample. A finished non-looping + // feeder must instead stay at the exclusive end, never replay that last sample. + let next = i128::from(self.span.start) + i128::from(consumed); + s.cursor = if !self.span.looping && next >= i128::from(self.span.wb) { self.span.wb } else { clock_sample(&self.span, consumed, 0) }; + } + + /// Follow an edited `project`: mixing continues from it, and the queued sound is dropped only + /// when the edit changed what is heard ([`audio_changed`]); a transform or colour edit leaves + /// the queue alone, so playback does not drop out. + pub fn follow_project(&mut self, project: Arc) { + let Some(old) = self.project() else { return }; + if audio_changed(&old, &project, self.comp) { + self.set_project(project); + } else if let Ok(mut s) = self.state.lock() { + s.src.project = project; + } + } + + /// The comp sample index audible now, expressed at the device's rate. pub fn clock(&self) -> i64 { - clock_sample(&self.span, self.feed.consumed(), self.device.latency_frames()) + let sample = clock_sample(&self.span, self.feed.consumed(), self.device.latency_frames()); + if self.timeline.normal() { sample } else { self.timeline.time(sample).to_units_floor(i64::from(self.rate)) } + } + + /// Composition time under the audible output sample, including preview speed and latency. + pub fn clock_time(&self) -> Tick { + self.timeline.time(clock_sample(&self.span, self.feed.consumed(), self.device.latency_frames())) } pub fn finished(&self) -> bool { @@ -279,20 +370,101 @@ impl Drop for AudioPlayback { } } -/// Mix one block at `cursor` into the feed; returns the next cursor (wrapping or finishing at -/// the work-area end). -fn feed_block(src: &RenderSource, comp: ItemId, feed: &AudioFeed, span: &PlaySpan, cursor: i64, rate: u32, mix: u32) -> i64 { - let mut cursor = cursor; - if cursor >= span.wb { - if !span.looping { - feed.finished.store(true, Ordering::Relaxed); - return cursor; +/// Whether `new` sounds different from `old` for `comp`: a layer's timing, source, Audio / Solo / +/// Effects switch, Audio Levels or effects, or the same for a nested comp. Other edits (video +/// properties, names, labels) do not change the mix unless an audio expression can read them. +fn audio_changed(old: &Project, new: &Project, comp: ItemId) -> bool { + audio_changed_inner(old, new, comp, &mut HashSet::new()) +} + +/// Each shared precomp is compared once per depth. A depth limit alone still visits an +/// exponentially large number of paths in a small graph of reused precomps, blocking the UI. +fn audio_changed_inner(old: &Project, new: &Project, comp: ItemId, seen: &mut HashSet<(ItemId, usize)>) -> bool { + /// Compare one comp and its reachable audio sources, without revisiting a shared path depth. + fn comps_differ(old: &Project, new: &Project, comp: ItemId, depth: usize, seen: &mut HashSet<(ItemId, usize)>) -> bool { + if depth >= 16 || !seen.insert((comp, depth)) { + return false; } + let (Some(a), Some(b)) = (old.comp(comp), new.comp(comp)) else { return old.comp(comp).is_some() != new.comp(comp).is_some() }; + a.layers.len() != b.layers.len() + || a.layers.iter().zip(&b.layers).any(|(x, y)| { + x.id != y.id + || x.source != y.source + || x.start_time != y.start_time + || x.in_point != y.in_point + || x.out_point != y.out_point + || x.stretch != y.stretch + || x.switches.audio != y.switches.audio + || x.switches.solo != y.switches.solo + || x.switches.effects != y.switches.effects + || x.props.sub("audio") != y.props.sub("audio") + || audio_effects(x) != audio_effects(y) + || audio_has_expression(x) + || audio_has_expression(y) + || match &x.source { + LayerSource::Footage { item } => footage_differs(old, new, *item), + LayerSource::Comp { item } => comps_differ(old, new, *item, depth + 1, seen), + _ => false, + } + }) + } + /// The layer's audio effects (Effect > Audio), the only effects the mixer applies; a video effect + /// edit leaves them as they were. + fn audio_effects(l: &Layer) -> Vec<&PropGroup> { + let is_audio = |g: &&PropGroup| matches!(&g.kind, GroupKind::Effect { effect } if effectcraft_engine::effects::audio_fx::is_audio_effect(effect)); + l.effects().into_iter().flat_map(|fx| fx.groups()).filter(is_audio).collect() + } + /// Audio expressions can read any property in any comp (including video-only effects and + /// names). Without a complete dependency graph, keeping the old mix would be stale. + fn audio_has_expression(l: &Layer) -> bool { + l.switches.audio + && (l.props.sub("audio").and_then(|g| g.get("levels")).is_some_and(|p| p.has_expression()) + || (l.switches.effects && audio_effects(l).iter().any(|g| g.enabled && g.props().any(|p| p.has_expression())))) + } + /// The footage item's audio availability changed (relinked, missing, no audio). + fn footage_differs(old: &Project, new: &Project, item: ItemId) -> bool { + let f = |p: &Project| match p.item(item).map(|i| &i.kind) { + Some(ItemKind::Footage(f)) => Some((f.has_audio, f.missing, f.path.clone())), + _ => None, + }; + f(old) != f(new) + } + comps_differ(old, new, comp, 0, seen) +} + +/// Mix one block at the feeder's cursor into the feed and move the cursor on (wrapping or +/// finishing at the work-area end). The block is mixed without holding the lock, so a project +/// change never waits for a slow mix; a block mixed from the old project is dropped. +fn feed_block(state: &Mutex, comp: ItemId, feed: &AudioFeed, span: &PlaySpan, timeline: PreviewRate, mix: u32) { + let Ok((src, mut cursor, generation)) = state.lock().map(|s| (s.src.clone(), s.cursor, s.generation)) else { + // Poisoned: nothing more can be mixed. + feed.finished.store(true, Ordering::Relaxed); + return; + }; + let mut block = None; + if cursor >= span.wb && span.looping { cursor = span.wa; } - let n = ((span.wb - cursor) as usize).min(BLOCK); - feed.push(&mix_block(src, comp, sample_time(cursor, rate), n, rate, mix)); - cursor + n as i64 + if cursor < span.wb { + let n = span.wb.saturating_sub(cursor).clamp(0, BLOCK as i64) as usize; + block = Some(if timeline.normal() { + mix_block(&src, comp, timeline.time(cursor), n, timeline.rate(), mix) + } else { + timeline.mix(cursor, n, mix, |t, frames| { + effectcraft_engine::render::audio::mix_comp(&src.project, src.footage.as_ref(), src.expr.as_deref(), comp, t, frames, mix) + }) + }); + cursor += n as i64; + } + let Ok(mut s) = state.lock() else { return }; + if s.generation != generation { + return; + } + match block { + Some(b) => feed.push(&b), + None => feed.finished.store(true, Ordering::Relaxed), + } + s.cursor = cursor; } /// `n` stereo frames of `comp`'s mix from comp time `t` at the device `rate`, mixed at `mix` Hz @@ -457,6 +629,13 @@ mod tests { assert_eq!(clock_sample(&once, 100_000, 0), 47999); } + #[test] + fn slow_preview_clocks_do_not_overflow_a_wide_output_span() { + let span = PlaySpan { start: 0, wa: i64::MIN, wb: i64::MAX, looping: true }; + assert_eq!(clock_sample(&span, u64::MAX, 0), 0); + assert_eq!(clock_sample(&PlaySpan { looping: false, ..span }, u64::MAX, 0), i64::MAX - 1); + } + #[test] fn frame_mapping_matches_comp_frames() { let fps = FrameRate::new(30, 1); @@ -559,6 +738,145 @@ mod tests { playback.stop(); } + /// #2104: muting a layer while the preview plays silences it: the queued sound is dropped + /// and everything mixed afterwards comes from the edited project. + #[test] + fn muting_a_layer_while_playing_silences_the_preview() { + use effectcraft_engine::color::Label; + use effectcraft_engine::project::{Comp, Footage, FootageKind, ItemKind, LayerSource, build}; + use effectcraft_engine::render::FootageSource; + use effectcraft_engine::render::cache::LayerCache; + use std::time::{Duration, Instant}; + + /// Footage audio that is a constant 0.5. + struct Steady; + impl FootageSource for Steady { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, _: Tick, frames: usize, _: u32) -> Option> { + Some(vec![0.5; frames * 2]) + } + } + struct Device; + impl AudioDevice for Device { + fn sample_rate(&self) -> u32 { + 48000 + } + fn start(&mut self, _: Arc) -> Result<(), String> { + Ok(()) + } + fn stop(&mut self) {} + } + + let mut p = Project::default(); + let footage = + Footage { path: "tone.flac".into(), kind: FootageKind::Audio, duration: Tick::from_seconds_f64(60.0), has_audio: true, ..Default::default() }; + let item = p.add_item("tone.flac", Label::SeaFoam, None, ItemKind::Footage(footage)); + let mut comp = Comp::new(64, 36, FrameRate::new(30, 1), Tick::from_seconds_f64(10.0)); + let layer = build::layer(&mut p, &comp, "tone.flac", LayerSource::Footage { item }, (0, 0), None); + comp.layers = vec![layer].into(); + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.into())); + let mut project = Arc::new(p); + let src = RenderSource { + project: project.clone(), + footage: Arc::new(Steady), + expr: None, + layer_cache: Arc::new(LayerCache::default()), + gpu: None, + gpu_display: false, + disk: None, + }; + let mut playback = + AudioPlayback::start(Box::new(Device), src, cid, Tick::ZERO, Tick::ZERO, Tick::from_seconds_f64(10.0), true, 0).expect("the preview starts"); + let t = Instant::now(); + while playback.feed.queued_frames() == 0 { + assert!(t.elapsed() < Duration::from_secs(30), "the feeder never delivered"); + std::thread::sleep(Duration::from_millis(10)); + } + // Mute the layer while the sound plays. + if let Some(c) = Arc::make_mut(&mut project).comp_mut(cid) { + c.layers[0].switches.audio = false; + } + playback.set_project(project.clone()); + assert!(playback.project().is_some_and(|p| Arc::ptr_eq(&p, &project))); + // The feeder tops the queue up again, now from the muted project. + while playback.feed.queued_frames() == 0 { + assert!(t.elapsed() < Duration::from_secs(30), "the feeder never delivered"); + std::thread::sleep(Duration::from_millis(10)); + } + let mut out = vec![1.0f32; playback.feed.queued_frames() * 2]; + playback.feed.pull(&mut out); + assert!(out.iter().all(|v| *v == 0.0), "the muted layer still plays"); + playback.stop(); + } + + /// #2104: only edits that change the mix drop the queued sound. + #[test] + fn only_audible_edits_change_the_audio() { + use effectcraft_engine::color::Label; + use effectcraft_engine::project::{Comp, Footage, FootageKind, build}; + + let mut p = Project::default(); + let footage = + Footage { path: "tone.flac".into(), kind: FootageKind::Audio, duration: Tick::from_seconds_f64(60.0), has_audio: true, ..Default::default() }; + let item = p.add_item("tone.flac", Label::SeaFoam, None, ItemKind::Footage(footage)); + let mut comp = Comp::new(64, 36, FrameRate::new(30, 1), Tick::from_seconds_f64(10.0)); + let layer = build::layer(&mut p, &comp, "tone.flac", LayerSource::Footage { item }, (0, 0), None); + comp.layers = vec![layer].into(); + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.into())); + let old = p; + // A video-only edit: the layer is hidden, its name and label change. + let mut video = old.clone(); + if let Some(l) = video.comp_mut(cid).and_then(|c| c.layers.first_mut()) { + l.switches.video = false; + l.name = "renamed".into(); + } + assert!(!audio_changed(&old, &video, cid), "a video edit must not drop the sound"); + // Muting, solo and a moved in point do change it. + for edit in 0..3 { + let mut edited = old.clone(); + if let Some(l) = edited.comp_mut(cid).and_then(|c| c.layers.first_mut()) { + match edit { + 0 => l.switches.audio = false, + 1 => l.switches.solo = true, + _ => l.in_point = Tick::from_seconds_f64(1.0), + } + } + assert!(audio_changed(&old, &edited, cid), "audible edit {edit} must be followed"); + } + } + + /// #2104: a video effect edit leaves the mix alone, an audio effect changes it. + #[test] + fn only_audio_effects_change_the_audio() { + use effectcraft_engine::color::Label; + use effectcraft_engine::project::build::{self, Ids}; + use effectcraft_engine::project::{Comp, Footage, FootageKind}; + + let mut p = Project::default(); + let footage = + Footage { path: "tone.flac".into(), kind: FootageKind::Audio, duration: Tick::from_seconds_f64(60.0), has_audio: true, ..Default::default() }; + let item = p.add_item("tone.flac", Label::SeaFoam, None, ItemKind::Footage(footage)); + let mut comp = Comp::new(64, 36, FrameRate::new(30, 1), Tick::from_seconds_f64(10.0)); + let layer = build::layer(&mut p, &comp, "tone.flac", LayerSource::Footage { item }, (0, 0), None); + comp.layers = vec![layer].into(); + let cid = p.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.into())); + let with_effect = |id: &str| { + let mut edited = p.clone(); + let spec = effectcraft_engine::effects::find(id).expect("the effect exists"); + let mut next = edited.next_id; + let g = effectcraft_engine::effects::instantiate(spec, &mut Ids(&mut next), spec.name, [64.0, 36.0]); + edited.next_id = next; + if let Some(fx) = edited.comp_mut(cid).and_then(|c| c.layers.first_mut()).and_then(|l| l.props.sub_mut("effects")) { + fx.children.push(g.into()); + } + edited + }; + assert!(!audio_changed(&p, &with_effect("ec.blur.gaussian"), cid), "a video effect must not drop the sound"); + assert!(audio_changed(&p, &with_effect("ec.audio.backwards"), cid), "an audio effect must be followed"); + } + #[test] fn meter_decays_and_holds_peaks() { let mut m = Meter::default(); @@ -573,3 +891,7 @@ mod tests { assert!(m.peak_db[0] < -10.0, "{}", m.peak_db[0]); } } + +#[cfg(test)] +#[path = "audio/live_tests.rs"] +mod live_tests; diff --git a/crates/ui-egui/src/audio/live_tests.rs b/crates/ui-egui/src/audio/live_tests.rs new file mode 100644 index 000000000..a7b845c3b --- /dev/null +++ b/crates/ui-egui/src/audio/live_tests.rs @@ -0,0 +1,217 @@ +//! #2104: exercise the real mixer and queue without relying on thread timing or an audio device. + +use super::{AudioDevice, AudioFeed, AudioPlayback, Feeder, PlaySpan, PreviewRate, feed_block}; +use crate::frames::RenderSource; +use effectcraft_engine::color::Label; +use effectcraft_engine::keyframe::Value; +use effectcraft_engine::project::{Comp, Expression, Footage, FootageKind, ItemId, ItemKind, Layer, LayerSource, Project, Property, build}; +use effectcraft_engine::render::{EvalCtx, ExprHost, FootageSource, LayerCache}; +use effectcraft_engine::time::{FrameRate, Tick}; +use std::sync::atomic::AtomicBool; +use std::sync::{Arc, Mutex, mpsc}; +use std::time::Duration; + +struct Device; +impl AudioDevice for Device { + fn sample_rate(&self) -> u32 { + 48000 + } + fn start(&mut self, _: Arc) -> Result<(), String> { + Ok(()) + } + fn stop(&mut self) {} +} + +/// A ramp identifies the exact source sample, exposing skips and repeats after an edit. +struct Ramp; +impl FootageSource for Ramp { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, t: Tick, frames: usize, rate: u32) -> Option> { + let first = t.to_units_floor(i64::from(rate)); + Some((0..frames).flat_map(|i| [(first + i as i64) as f32 / 128.0; 2]).collect()) + } +} + +/// Construct a paused feeder whose short nonzero span makes every source sample observable. +fn playback(footage: Arc) -> AudioPlayback { + let mut project = Project::default(); + let f = Footage { path: "ramp.wav".into(), kind: FootageKind::Audio, has_audio: true, ..Default::default() }; + let item = project.add_item("Ramp", Label::SeaFoam, None, ItemKind::Footage(f)); + let mut comp = Comp::new(64, 36, FrameRate::new(30, 1), Tick::from_seconds_f64(1.0)); + let layer = build::layer(&mut project, &comp, "Ramp", LayerSource::Footage { item }, (0, 0), None); + comp.layers = vec![layer].into(); + let cid = project.add_item("Comp", Label::Sandstone, None, ItemKind::Comp(comp.into())); + let src = RenderSource { + project: Arc::new(project), + footage, + expr: None, + layer_cache: Arc::new(LayerCache::default()), + gpu: None, + gpu_display: false, + disk: None, + }; + AudioPlayback { + feed: Arc::new(AudioFeed::default()), + device: Box::new(Device), + stop: Arc::new(AtomicBool::new(false)), + thread: None, + state: Arc::new(Mutex::new(Feeder { src, cursor: 24, generation: 0 })), + comp: cid, + mix: 48000, + rate: 48000, + span: PlaySpan { start: 24, wa: 16, wb: 64, looping: false }, + timeline: PreviewRate::new(48000, 1.0, Tick::ZERO).unwrap(), + } +} + +/// Run exactly one production feeder block, without a background refill racing assertions. +fn mix(p: &AudioPlayback) { + feed_block(&p.state, p.comp, &p.feed, &p.span, p.timeline, p.mix); +} + +/// Copy the active project and apply a layer edit through the same snapshot boundary as the UI. +fn edit(p: &AudioPlayback, update: impl FnOnce(&mut Layer)) -> Arc { + let mut project = p.project().unwrap(); + update(&mut Arc::make_mut(&mut project).comp_mut(p.comp).unwrap().layers[0]); + project +} + +/// Levels edits retain sample position across the loop boundary and the exclusive non-looping end. +#[test] +fn project_replacement_resumes_at_the_first_unconsumed_sample() { + for looping in [false, true] { + let mut p = playback(Arc::new(Ramp)); + p.span.looping = looping; + mix(&p); + let mut out = [0.0; 64]; + p.feed.pull(&mut out); + assert_eq!(out[0], 24.0 / 128.0); + assert_eq!(p.feed.consumed(), 32); + let updated = edit(&p, |l| l.props.sub_mut("audio").unwrap().get_mut("levels").unwrap().value = Value::Vec2([-6.020599913279624; 2])); + p.follow_project(updated); + assert_eq!(p.feed.queued_frames(), 0); + assert_eq!(p.state.lock().unwrap().cursor, 56); + mix(&p); + let mut tail = [0.0; 16]; + p.feed.pull(&mut tail); + assert!((tail[0] - 56.0 / 256.0).abs() < 1e-6); + assert!((tail[14] - 63.0 / 256.0).abs() < 1e-6); + p.set_project(p.project().unwrap()); + assert_eq!(p.state.lock().unwrap().cursor, if looping { 16 } else { 64 }); + mix(&p); + if looping { + let mut first = [0.0; 2]; + p.feed.pull(&mut first); + assert!((first[0] - 16.0 / 256.0).abs() < 1e-6, "resume at the work-area start"); + } else { + assert!(p.finished(), "an edit after the last sample must not replay it"); + assert_eq!(p.feed.queued_frames(), 0); + } + } +} + +struct Gated { + entered: mpsc::Sender<()>, + resume: Mutex>, +} +impl FootageSource for Gated { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, _: Tick, frames: usize, _: u32) -> Option> { + self.entered.send(()).unwrap(); + self.resume.lock().unwrap().recv_timeout(Duration::from_secs(10)).unwrap(); + Some(vec![0.5; frames * 2]) + } +} + +/// A slow mix must not block edits or publish samples from the superseded project generation. +#[test] +fn a_block_in_flight_cannot_publish_after_a_mute() { + let (entered, waiting) = mpsc::channel(); + let (resume, gate) = mpsc::channel(); + let mut p = playback(Arc::new(Gated { entered, resume: Mutex::new(gate) })); + let (state, feed, span, comp, rate) = (p.state.clone(), p.feed.clone(), p.span, p.comp, p.rate); + let worker = std::thread::spawn(move || feed_block(&state, comp, &feed, &span, PreviewRate::new(rate, 1.0, Tick::ZERO).unwrap(), rate)); + waiting.recv_timeout(Duration::from_secs(10)).unwrap(); + // The edit completes while the mixer is blocked, without waiting on its work. + p.follow_project(edit(&p, |l| l.switches.audio = false)); + resume.send(()).unwrap(); + worker.join().unwrap(); + assert_eq!(p.feed.queued_frames(), 0, "the old generation cannot publish"); + assert_eq!(p.state.lock().unwrap().cursor, 24, "nor advance the replacement cursor"); + mix(&p); + let mut out = [1.0; 16]; + p.feed.pull(&mut out); + assert_eq!(out, [0.0; 16]); +} + +/// Models a levels expression that reads a video property through the renderer's expression API. +struct OpacityLevels; +impl ExprHost for OpacityLevels { + fn eval(&self, _: &EvalCtx, layer: &Layer, _: &Property, _: &Value) -> Result { + let opacity = layer.transform().unwrap().get("opacity").unwrap().value.as_f64(); + Ok(Value::Vec2([if opacity == 0.0 { -96.0 } else { 0.0 }; 2])) + } +} + +/// Video properties are irrelevant to a plain mix, but may be dependencies of audio expressions. +#[test] +fn video_edits_keep_the_queue_unless_audio_expressions_can_read_them() { + for expression in [false, true] { + let mut p = playback(Arc::new(Ramp)); + if expression { + let updated = edit(&p, |l| { + l.props.sub_mut("audio").unwrap().get_mut("levels").unwrap().expr = + Some(Expression { text: "[transform.opacity, transform.opacity]".into(), enabled: true }); + }); + let mut state = p.state.lock().unwrap(); + state.src.project = updated; + state.src.expr = Some(Arc::new(OpacityLevels)); + } + mix(&p); + assert_eq!(p.feed.queued_frames(), 40); + let updated = edit(&p, |l| l.props.sub_mut("transform").unwrap().get_mut("opacity").unwrap().value = Value::Scalar(0.0)); + p.follow_project(updated); + if expression { + assert_eq!(p.feed.queued_frames(), 0, "the expression can read the edited video property"); + mix(&p); + let mut out = [1.0; 16]; + p.feed.pull(&mut out); + assert_eq!(out, [0.0; 16]); + } else { + assert_eq!(p.feed.queued_frames(), 40, "an unrelated edit cannot interrupt audio"); + } + } +} + +/// Reused nested comps cost linear visits while edits to their descendants remain detectable. +#[test] +fn shared_nested_comps_are_compared_once_per_depth() { + let mut project = Project::default(); + let mut child = None; + for _ in 0..10 { + let mut comp = Comp::new(64, 36, FrameRate::new(30, 1), Tick::from_seconds_f64(1.0)); + if let Some(item) = child { + for _ in 0..10 { + let mut layer = build::layer(&mut project, &comp, "Nested", LayerSource::Comp { item }, (64, 36), None); + layer.switches.audio = false; + comp.layers.push(layer); + } + } + child = Some(project.add_item("Nested", Label::Sandstone, None, ItemKind::Comp(comp.into()))); + } + let root = child.unwrap(); + let mut edited = project.clone(); + edited.comp_mut(root).unwrap().layers[0].name = "Renamed".into(); + let mut seen = std::collections::HashSet::new(); + assert!(!super::audio_changed_inner(&project, &edited, root, &mut seen)); + assert_eq!(seen.len(), 10, "compare the ten comps, not a billion paths through them"); + let leaf = *seen.iter().find(|(_, depth)| *depth == 9).map(|(item, _)| item).unwrap(); + let mut layer = project.comp(root).unwrap().layers[0].clone(); + layer.source = LayerSource::Null; + edited.comp_mut(leaf).unwrap().layers.push(layer); + assert!(super::audio_changed(&project, &edited, root), "memoization must still find a nested edit"); +} diff --git a/crates/ui-egui/src/audio/preview_rate.rs b/crates/ui-egui/src/audio/preview_rate.rs new file mode 100644 index 000000000..06124b4c9 --- /dev/null +++ b/crates/ui-egui/src/audio/preview_rate.rs @@ -0,0 +1,199 @@ +//! Preview speed changes composition time, not the output device's sample rate. +//! Keep the clock and the resampler on the same rational output-sample grid. + +use effectcraft_engine::time::{FrameRate, TICKS_PER_SECOND, Tick}; + +use super::PlaySpan; + +/// At most this many composition samples are mixed at once. At very high preview speeds, +/// skip samples that no output frame needs instead of allocating the entire source interval. +const SOURCE_PAGE: usize = 4096; + +#[derive(Clone, Copy)] +pub(super) struct PreviewRate { + rate: u32, + speed: FrameRate, + origin: Tick, +} + +impl PreviewRate { + pub(super) fn new(rate: u32, speed: f64, start: Tick) -> Result { + // A billionth keeps the ratio of decimal preview rates and rational comp rates without + // rounding it to an integer sample rate. Reject unrepresentable values, never mute them. + let num = (speed * 1_000_000_000.0).round(); + if rate == 0 || !speed.is_finite() || speed <= 0.0 || num < 1.0 || num >= i64::MAX as f64 { + return Err("The preview playback rate must be finite, positive and representable".into()); + } + let speed = FrameRate::new(num as i64, 1_000_000_000); + // Keep the existing 1x sample grid. At another speed, anchor the grid to the exact + // requested frame rather than rounding a nonzero start backwards by an output sample. + let origin = if speed.num == speed.den { Tick::ZERO } else { start }; + Ok(Self { rate, speed, origin }) + } + + pub(super) fn rate(self) -> u32 { + self.rate + } + + pub(super) fn normal(self) -> bool { + self.speed.num == self.speed.den + } + + pub(super) fn time(self, sample: i64) -> Tick { + if self.normal() { + // Preserve the legacy grid, including negative samples at unusual device rates. + return super::sample_time(sample, self.rate); + } + self.origin + Tick::from_rational(sample, self.speed.num, i64::from(self.rate) * self.speed.den) + } + + /// First output sample at or after a composition time. Ceiling keeps loop endpoints inside + /// the work area. A fractional loop boundary is quantized by less than one output sample. + fn sample_ceil(self, time: Tick) -> i64 { + let n = i128::from((time - self.origin).0) * i128::from(self.rate) * i128::from(self.speed.den); + let d = i128::from(TICKS_PER_SECOND) * i128::from(self.speed.num); + let sample = n.div_euclid(d) + i128::from(n.rem_euclid(d) != 0); + sample.clamp(i64::MIN as i128, i64::MAX as i128) as i64 + } + + pub(super) fn span(self, start: Tick, wa: Tick, wb: Tick, looping: bool) -> PlaySpan { + if self.normal() { + return PlaySpan::new(start, wa, wb, looping, self.rate); + } + let wa = self.sample_ceil(wa); + PlaySpan { start: 0, wa, wb: self.sample_ceil(wb).max(wa.saturating_add(1)), looping } + } + + /// Source sample and interpolation phase at an absolute output cursor. Computing this + /// from the timeline (not by accumulating block lengths) makes partitioning immaterial. + fn position(self, sample: i64, mix: u32) -> (i64, f32) { + let n = i128::from(self.time(sample).0) * i128::from(mix); + let d = i128::from(TICKS_PER_SECOND); + (n.div_euclid(d) as i64, n.rem_euclid(d) as f32 / d as f32) + } + + pub(super) fn mix(self, cursor: i64, n: usize, mix: u32, mut render: impl FnMut(Tick, usize) -> Vec) -> Vec { + let mut out = Vec::with_capacity(n.saturating_mul(2)); + let mut page = Vec::new(); + let mut first = 0; + for i in 0..n { + let sample = cursor.saturating_add(i as i64); + let (at, fraction) = self.position(sample, mix); + let offset = usize::try_from(at.saturating_sub(first)).ok(); + if page.is_empty() || offset.is_none_or(|i| i >= (page.len() / 2).saturating_sub(1)) { + first = at; + let last = self.position(cursor.saturating_add(n.saturating_sub(1) as i64), mix).0; + let next = self.position(sample.saturating_add(1), mix).0; + let frames = if next.saturating_sub(first) >= SOURCE_PAGE as i64 - 1 { + // No following output sample can reuse this page. Read only this sample + // and its interpolation neighbor, rather than mixing an unused gap. + 2 + } else { + last.saturating_sub(first).saturating_add(2).clamp(2, SOURCE_PAGE as i64) as usize + }; + // mix_comp floors its starting sample. Add the fractional tick back so exotic + // source rates start on `first`, not the preceding composition sample. + let mut t = Tick::from_units(first, i64::from(mix)); + if t.to_units_floor(i64::from(mix)) < first { + t.0 = t.0.saturating_add(1); + } + page = render(t, frames); + } + let index = usize::try_from(at.saturating_sub(first)).unwrap_or(usize::MAX).saturating_mul(2); + for channel in 0..2 { + let a = page.get(index.saturating_add(channel)).copied().unwrap_or(0.0); + let b = page.get(index.saturating_add(2 + channel)).copied().unwrap_or(a); + out.push(a + (b - a) * fraction); + } + } + out + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::audio::clock_sample; + + #[test] + fn normal_speed_preserves_the_legacy_sample_grid() { + for rate in [44_100, 48_000, 48_001] { + let timeline = PreviewRate::new(rate, 1.0, Tick::from_seconds_f64(-0.3)).unwrap(); + for sample in [-48_001, -1, 0, 1, 48_001] { + assert_eq!(timeline.time(sample), crate::audio::sample_time(sample, rate)); + } + } + } + + #[test] + fn speed_clock_matches_slow_fast_and_fractional_previews() { + for (comp, preview) in [(FrameRate::FPS_60, 30.0), (FrameRate::FPS_30, 60.0), (FrameRate::FPS_59_94, 29.97)] { + let speed = preview / comp.as_f64(); + let start = comp.tick_of(17); + let rate = PreviewRate::new(48_000, speed, start).unwrap(); + assert_eq!(rate.time(0), start, "the first frame stays exact"); + let time = rate.time(48_000); + assert!(((time - start).seconds() - speed).abs() < 1e-9); + assert_eq!(comp.frame_at(time), comp.frame_at(start + Tick::from_seconds_f64(speed))); + } + } + + #[test] + fn speed_clock_subtracts_output_latency_and_wraps_inside_the_work_area() { + for speed in [0.5, 2.0, 0.73] { + let start = FrameRate::FPS_29_97.tick_of(19); + let wa = FrameRate::FPS_29_97.tick_of(13); + let wb = FrameRate::FPS_29_97.tick_of(43); + let rate = PreviewRate::new(48_000, speed, start).unwrap(); + let span = rate.span(start, wa, wb, true); + assert_eq!(rate.time(clock_sample(&span, 512, 1024)), start); + assert!(rate.time(span.wb - 1) < wb); + let wrapped = rate.time(clock_sample(&span, span.wb as u64 + 1024, 1024)); + assert!(wrapped >= wa && (wrapped - wa).seconds() < speed / 48_000.0); + let once = PlaySpan { looping: false, ..span }; + assert!(rate.time(clock_sample(&once, u32::MAX as u64, 0)) < wb); + } + } + + fn ramp(t: Tick, n: usize, mix: u32) -> Vec { + let first = t.to_units_floor(i64::from(mix)); + (0..n).flat_map(|i| [(first + i as i64) as f32 / 100_000.0, -(first + i as i64) as f32 / 100_000.0]).collect() + } + + #[test] + fn speed_mix_keeps_fractional_phase_across_uneven_blocks() { + for speed in [0.5, 2.0, 0.73, 29.97 / FrameRate::FPS_59_94.as_f64()] { + let start = FrameRate::FPS_29_97.tick_of(17); + let rate = PreviewRate::new(48_000, speed, start).unwrap(); + for mix in [44_100, 48_000, 96_000] { + let whole = rate.mix(0, 2048, mix, |t, n| ramp(t, n, mix)); + let split: Vec<_> = [(0, 321), (321, 107), (428, 1620)].into_iter().flat_map(|(at, n)| rate.mix(at, n, mix, |t, n| ramp(t, n, mix))).collect(); + assert_eq!(whole, split, "speed {speed}, source rate {mix}"); + for (i, frame) in whole.as_chunks::<2>().0.iter().enumerate() { + let expected = (start.seconds() + i as f64 * speed / 48_000.0) * mix as f64 / 100_000.0; + assert!((f64::from(frame[0]) - expected).abs() < 1e-6); + assert_eq!(frame[1], -frame[0]); + } + } + } + } + + #[test] + fn extreme_speeds_keep_source_reads_bounded_without_changing_speed() { + for speed in [0.00001, 99_900.0] { + let rate = PreviewRate::new(8_000, speed, Tick::ZERO).unwrap(); + let mut rendered = 0; + let out = rate.mix(0, 1024, 192_000, |_, n| { + assert!(n <= SOURCE_PAGE); + rendered += n; + vec![0.25; n * 2] + }); + assert!(rendered <= SOURCE_PAGE, "unused source intervals were mixed: {rendered} samples"); + assert_eq!(out, vec![0.25; 2048]); + assert!((rate.time(8000).seconds() - speed).abs() < 1e-9); + } + for speed in [0.0, -1.0, f64::NAN, f64::INFINITY, f64::MAX] { + assert!(PreviewRate::new(48_000, speed, Tick::ZERO).is_err()); + } + } +} diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 86bd20c93..f7aecdfcb 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -21,6 +21,7 @@ //! //! The full reference with examples is `docs/control-protocol.md`. +use std::sync::Arc; use std::sync::mpsc::Sender; use serde_json::{Value, json}; @@ -60,6 +61,8 @@ pub enum Outcome { }, /// Reply once background jobs end (a `wait: true` command whose job runs in a worker). AwaitJobs(JobWaiter), + /// A script yielded its session for rendering; reply after its continuation returns. + AwaitScript(u32), } /// A `wait: true` engine command whose job was sent to the session's offload (the browser's @@ -73,6 +76,8 @@ pub struct JobWaiter { pub jobs: Vec, /// Absolute completion-log position when it started (including evicted entries). pub log_cursor: usize, + /// The exact native render, not the reusable `jobs.list` identifier. + pub render: Option>, } /// Commands that block with `wait` (and its default). @@ -81,34 +86,74 @@ fn waits(command: &str, params: &Value) -> bool { params.get("wait").and_then(Value::as_bool).unwrap_or(default) } -/// Run an engine command. A waiting command (`wait: true`) whose job the session offloads -/// runs with `wait: false` instead, and the reply waits for the job ([`Outcome::AwaitJobs`]), -/// so the browser's UI keeps drawing while it runs. +/// Run an engine command. A waiting offloaded command or native render runs with `wait: false` +/// instead, and the reply waits for the job ([`Outcome::AwaitJobs`]), keeping the UI responsive. fn execute_engine(app: &mut EffectcraftApp, id: &str, params: Value) -> Result { - if !app.session.offloads() || !waits(id, ¶ms) { - return app.session.execute_checked(id, params).map(ok).map_err(|e| e.to_string()); + if !(app.session.offloads() || (cfg!(not(target_arch = "wasm32")) && id == "renderQueue.render")) || !waits(id, ¶ms) { + let result = app.session.execute_checked(id, params); + return result.map(script_outcome).map_err(|e| e.to_string()); } let before: Vec = app.session.jobs().into_iter().map(|j| j.id).collect(); let log_cursor = app.session.offload_log_start.saturating_add(app.session.offload_log.len()); let mut p = params.clone(); - p["wait"] = json!(false); + p.as_object_mut().ok_or("command params must be an object")?.insert("wait".into(), json!(false)); let result = app.session.execute_checked(id, p).map_err(|e| e.to_string())?; let jobs: Vec = app.session.jobs().into_iter().map(|j| j.id).filter(|j| !before.contains(j)).collect(); - if jobs.is_empty() { - return Ok(ok(result)); + let render = if cfg!(not(target_arch = "wasm32")) && id == "renderQueue.render" && !app.session.offloads() { + app.session.render_job.as_ref().map(|job| { + job.shared.retain_reply_items(result["items"].as_array().cloned().unwrap_or_default()); + job.shared.clone() + }) + } else { + None + }; + Ok(JobWaiter { command: id.to_string(), params, result, jobs, log_cursor, render }.into_outcome()) +} + +fn script_outcome(result: Value) -> Outcome { + match result.get("renderHost").and_then(Value::as_u64).and_then(|id| u32::try_from(id).ok()) { + Some(host) => Outcome::AwaitScript(host), + None => ok(result), + } +} + +/// Suspended scripts retain open undo groups. Let the UI inspect and stop the render, but +/// never fold an unrelated edit, another script or a project replacement into that group. +pub(crate) fn check_script_render(app: &EffectcraftApp, id: &str) -> Result<(), String> { + if app.session.script_ui.render_host.is_some() && !effectcraft_engine::scriptui::allowed_during_render(id) { + Err(effectcraft_engine::scriptui::RENDER_WAIT_MESSAGE.into()) + } else { + Ok(()) } - Ok(Outcome::AwaitJobs(JobWaiter { command: id.to_string(), params, result, jobs, log_cursor })) } impl JobWaiter { + fn into_outcome(self) -> Outcome { + // A finished but unpolled native worker still needs its completion boundary even + // when jobs() no longer lists it. Already-polled commands have their final result. + if self.jobs.is_empty() && self.render.is_none() { ok(self.result) } else { Outcome::AwaitJobs(self) } + } + /// Whether its jobs are still running. pub fn pending(&self, app: &EffectcraftApp) -> bool { + if let Some(render) = &self.render { + return !render.reply_finished() && app.session.render_job.as_ref().is_some_and(|job| Arc::ptr_eq(render, &job.shared)); + } app.session.jobs().iter().any(|j| self.jobs.contains(&j.id)) } /// The reply: what the blocking command would have returned (`running: false`, the final /// progress, render items and analysis status), or the job's error. pub fn finish(self, app: &mut EffectcraftApp) -> Value { + if let Some(render) = self.render { + let Some(items) = render.completed_items() else { + return json!({"ok": false, "error": "render result unavailable: the project or render job was replaced before completion"}); + }; + let mut result = self.result; + result["items"] = json!(items); + result["rendering"] = json!(false); + return json!({"ok": true, "result": result}); + } let s = &mut app.session; s.poll_render(); let error = s @@ -178,6 +223,14 @@ fn point(app: &EffectcraftApp, p: &Value) -> Result { /// [`Outcome::AwaitJobs`] instead of replying in this call. pub fn handle(app: &mut EffectcraftApp, ctx: &egui::Context, req: &ControlRequest, now: f64) -> Outcome { let p = &req.params; + if app.session.script_ui.render_host.is_some() + && !matches!( + req.method.as_str(), + "engine.execute" | "ui.menu.invoke" | "engine.commands" | "ui.menu.list" | "ui.inspect" | "ui.elements" | "ui.resize" | "ui.focus" + ) + { + return err(effectcraft_engine::scriptui::RENDER_WAIT_MESSAGE); + } match req.method.as_str() { "engine.execute" | "ui.menu.invoke" => execute_command(app, ctx, &req.method, p), "engine.commands" => engine_commands(app, p), @@ -211,15 +264,25 @@ pub fn handle(app: &mut EffectcraftApp, ctx: &egui::Context, req: &ControlReques fn execute_command(app: &mut EffectcraftApp, ctx: &egui::Context, method: &str, p: &Value) -> Outcome { let s = |k: &str| p.get(k).and_then(Value::as_str); let Some(id) = s("command").or(s("id")) else { return err("missing `command`") }; + if let Err(e) = check_script_render(app, id) { + return err(e); + } let params = p.get("params").cloned().filter(|v| !v.is_null()).unwrap_or(json!({})); + // Only this invocation opts in: CLI, lifecycle scripts and the inline console retain + // their synchronous contract. Its host remembers the opt-in across successive renders. + let previous = app.session.script_ui.yield_renders; + app.session.script_ui.yield_renders = cfg!(not(target_arch = "wasm32")) && id == "script.run"; // `engine.execute` runs engine commands exactly as headless (never opens a dialog, e.g. // `comp.new {}` creates a default comp); UI-only ids (`tool.*`, `view.*`, `window.*`, // `playback.*` …) and `ui.menu.invoke` go through the menu dispatcher like a click. - let r = if method == "engine.execute" && effectcraft_engine::find_command(id).is_some() { + let r = if (method == "engine.execute" && effectcraft_engine::find_command(id).is_some()) + || (id == "renderQueue.render" && params.get("wait").and_then(Value::as_bool) == Some(true)) + { execute_engine(app, id, params) } else { - crate::menus::invoke(app, ctx, id, params).map(ok) + crate::menus::invoke(app, ctx, id, params).map(script_outcome) }; + app.session.script_ui.yield_renders = previous; match r { Ok(o) => o, Err(e) => { @@ -893,6 +956,26 @@ pub fn encode_png(rgba: &[u8], w: u32, h: u32) -> Result, String> { #[cfg(test)] mod tests { + #[test] + fn a_native_render_with_no_running_jobs_still_checks_its_own_completion() { + let waiter = super::JobWaiter { + command: "renderQueue.render".into(), + params: serde_json::json!({}), + result: serde_json::json!({"rendering": true, "items": []}), + jobs: vec![], + log_cursor: 0, + render: Some(std::sync::Arc::new(effectcraft_engine::render_queue::JobShared::default())), + }; + let super::Outcome::AwaitJobs(waiter) = waiter.into_outcome() else { + panic!("a finished native render must still pass through completion polling"); + }; + let mut app = crate::EffectcraftApp::new(effectcraft_engine::Session::default()); + assert!(!waiter.pending(&app)); + let reply = waiter.finish(&mut app); + assert_eq!(reply["ok"], false, "an abandoned render cannot silently report a replacement project's queue"); + assert!(reply["error"].as_str().unwrap().contains("replaced before completion")); + } + #[test] fn base64_rfc4648_vectors() { for (i, o) in [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] { diff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rs index c8ada9bf8..f126db257 100644 --- a/crates/ui-egui/src/dock.rs +++ b/crates/ui-egui/src/dock.rs @@ -124,7 +124,7 @@ impl PanelKind { PanelKind::EssentialGraphics => "Essential Graphics", PanelKind::ScriptPanel(_) => "ScriptUI Panel", PanelKind::Viewer(_) => "Composition", - PanelKind::LumetriScopes => "Lumetri Scopes", + PanelKind::LumetriScopes => "Video Scopes", PanelKind::Footage => "Footage", PanelKind::MediaBrowser => "Media Browser", PanelKind::Metadata => "Metadata", @@ -709,6 +709,16 @@ mod tests { w.apply(action, permissions).unwrap(); } + /// Only the title changes: existing saved workspace layouts still deserialize. + #[test] + fn scopes_label_is_descriptive_and_its_saved_identity_stays_compatible() { + let panel: PanelKind = serde_json::from_str("\"LumetriScopes\"").unwrap(); + assert_eq!(panel, LumetriScopes); + assert_eq!(panel.title(), "Video Scopes"); + assert_eq!(panel.id(), "LumetriScopes"); + assert_eq!(serde_json::to_string(&panel).unwrap(), "\"LumetriScopes\""); + } + #[test] fn after_effects_workspaces_exist_with_their_core_panels() { for name in WORKSPACES { diff --git a/crates/ui-egui/src/frames.rs b/crates/ui-egui/src/frames.rs index 968b2d791..a0c2f94b5 100644 --- a/crates/ui-egui/src/frames.rs +++ b/crates/ui-egui/src/frames.rs @@ -33,12 +33,12 @@ use std::collections::{BTreeSet, HashMap, HashSet, VecDeque}; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Condvar, Mutex, PoisonError}; use effectcraft_engine::project::{Comp, Heard, ItemId, ItemKind, Node, Project, PropGroup}; use effectcraft_engine::render::disk_cache::{self, DiskCache}; use effectcraft_engine::render::reads::{ReadFiles, ReadLog}; -use effectcraft_engine::render::{CancellableFootage, ExprHost, FootageSource, LayerCache, RenderOpts, Renderer}; +use effectcraft_engine::render::{AutoTicket, CancellableFootage, ExprHost, FootageSource, LayerCache, RenderOpts, Renderer}; use effectcraft_engine::time::Tick; use effectcraft_gpu::{DisplayFrame, FrameError, Gpu}; use rayon::prelude::*; @@ -54,6 +54,96 @@ const GPU_BUDGET: usize = 1 << 30; const MIN_GPU_BUDGET: usize = 64 << 20; const GPU_PRESSURE_COOLDOWN: std::time::Duration = std::time::Duration::from_secs(5); const GPU_RECOVERY_INTERVAL: std::time::Duration = std::time::Duration::from_secs(30); +/// GPU frames that render at once, however many frame threads there are. Each one holds its own +/// working textures, so 16 at once ran an RTX 3090 out of video memory (#3028). +const MAX_GPU_RENDERS: usize = 2; + +/// Counts the GPU renders in flight; the rest of the frame threads wait their turn. +struct GpuRenderSlots { + #[cfg(test)] + entered: std::sync::atomic::AtomicUsize, + busy: Mutex, + freed: Condvar, +} + +/// One GPU render slot, given back on drop. +struct GpuRenderSlot<'a>(&'a GpuRenderSlots); + +static GPU_RENDER_SLOTS: GpuRenderSlots = GpuRenderSlots::new(); + +impl GpuRenderSlots { + const fn new() -> Self { + Self { + #[cfg(test)] + entered: std::sync::atomic::AtomicUsize::new(0), + busy: Mutex::new(0), + freed: Condvar::new(), + } + } + + /// The UI-thread display path must hand a frame to the remote worker instead of waiting + /// behind preview lanes. Never block the window to enforce the native memory cap. + fn try_acquire(&self, max: usize) -> Option> { + let mut busy = match self.busy.try_lock() { + Ok(busy) => busy, + Err(std::sync::TryLockError::Poisoned(error)) => error.into_inner(), + Err(std::sync::TryLockError::WouldBlock) => return None, + }; + if !cfg!(target_arch = "wasm32") && *busy >= max { + return None; + } + *busy += 1; + #[cfg(test)] + self.entered.fetch_add(1, Ordering::SeqCst); + Some(GpuRenderSlot(self)) + } + + /// A Rayon helper may be executing a nested frame while an admitted render waits for + /// its work. Blocking that helper on a permit would deadlock the render (#3028). + fn for_render(&self, max: usize) -> Option> { + // Only a lane's drain thread waits: it renders one frame at a time, protected from + // nested drains, and a lane runs one drain at a time (it is idle again, and can be + // started anew, only once its drain found no job, so no frame of the lane is still + // running). The lane's other threads ([`LANE_WIDTH`]) only help with that frame's + // parallel work and are Rayon helpers here: they never wait. So no thread waits while + // an admitted frame waits for it, and waiting keeps the GPU preview's throughput. + #[cfg(not(target_arch = "wasm32"))] + if DRAINING_LANE.get() { + return Some(self.acquire(max)); + } + if rayon::current_thread_index().is_some() { self.try_acquire(max) } else { Some(self.acquire(max)) } + } + + /// Wait until fewer than `max` renders are in flight, then take a slot. The browser has no + /// threads to wait on: it never blocks. + fn acquire(&self, max: usize) -> GpuRenderSlot<'_> { + let mut busy = self.busy.lock().unwrap_or_else(PoisonError::into_inner); + while !cfg!(target_arch = "wasm32") && *busy >= max { + busy = self.freed.wait(busy).unwrap_or_else(PoisonError::into_inner); + } + *busy += 1; + #[cfg(test)] + self.entered.fetch_add(1, Ordering::SeqCst); + GpuRenderSlot(self) + } +} + +impl Drop for GpuRenderSlot<'_> { + fn drop(&mut self) { + let mut busy = self.0.busy.lock().unwrap_or_else(PoisonError::into_inner); + *busy = busy.saturating_sub(1); + self.0.freed.notify_one(); + } +} + +/// The window's frame gaps that adapt [`Queue::gpu_limit`] while a preview plays: a longer one +/// is a stall, shorter ones for a while are smooth. +const UI_STALL: std::time::Duration = std::time::Duration::from_millis(100); +const UI_SMOOTH: std::time::Duration = std::time::Duration::from_millis(50); +/// How long the window must stay smooth before one more lane composites on the GPU, and before it +/// tries again a limit that stalled. +const GPU_LANE_STEP: std::time::Duration = std::time::Duration::from_millis(500); +const GPU_LANE_RETRY: std::time::Duration = std::time::Duration::from_secs(10); mod f16; pub use f16::{F16_PIXEL_BYTES, F16Frame}; @@ -485,10 +575,8 @@ fn put_disk_frame(dc: &DiskCache, dk: u128, img: &FrameImage, files: &ReadFiles, return false; } match img { - FrameImage::Cpu(img) => { - let rgba: Vec = img.pixels.iter().flat_map(|c| c.to_array()).collect(); - dc.put_frame(dk, img.size[0] as u32, img.size[1] as u32, &rgba, files, virtual_id, since) - } + // (one copy; the disk cache's writer thread compresses it) + FrameImage::Cpu(img) => dc.put_frame_owned(dk, img.size[0] as u32, img.size[1] as u32, img.as_raw().to_vec(), files, virtual_id, since), FrameImage::CpuF16(f) => dc.put_frame_f16(dk, f.width, f.height, f.bits(), files, virtual_id, since), FrameImage::Gpu(_) => false, } @@ -544,14 +632,30 @@ pub trait RemoteFrames: Send + Sync { /// Whether a comp (or a comp it nests) has a layer with enabled effects. pub fn comp_has_effects(project: &Project, comp: ItemId) -> bool { - fn walk(p: &Project, comp: ItemId, depth: usize) -> bool { - let Some(c) = p.comp(comp) else { return false }; - c.layers.iter().any(|l| { - l.switches.effects && l.effects().is_some_and(|fx| fx.groups().any(|g| g.enabled)) - || depth < 16 && matches!(l.source, effectcraft_engine::project::LayerSource::Comp { item } if walk(p, item, depth + 1)) - }) + // Breadth first with a visited set: each comp is looked at once, at its shallowest level, so a + // comp first met deep in one chain is still found by a shorter route (a result memoised by + // comp alone would depend on the depth it was first reached at). + let mut seen = std::collections::HashSet::from([comp]); + let mut level = vec![comp]; + // Comps nested up to 16 levels below the first are searched. + for _ in 0..=16 { + let mut next = vec![]; + for id in level { + let Some(c) = project.comp(id) else { continue }; + for l in &c.layers { + if l.switches.effects && l.effects().is_some_and(|fx| fx.groups().any(|g| g.enabled)) { + return true; + } + if let effectcraft_engine::project::LayerSource::Comp { item } = l.source + && seen.insert(item) + { + next.push(item); + } + } + } + level = next; } - walk(project, comp, 0) + false } pub struct Frames { @@ -623,15 +727,36 @@ struct DiskCarry { #[cfg(not(target_arch = "wasm32"))] const MAX_DISK_SERIES: usize = 8; +/// Threads per lane (see [`Queue::lanes`]). A frame's passes are memory-bound and stop scaling +/// at about 4 threads, so a core per lane (12 frames at once, each on one thread) and 4 cores +/// per lane cache frames at the same rate (within 3 %, widths 1–4 measured on 12 cores); but the +/// 12 single-threaded frames start and finish together, so a preview that can't keep up shows +/// them in bursts, and its second frame waits for the whole batch (0.3–1.3 s). Three lanes of +/// four threads finish frames 3× as often and the second frame shows in 0.13–0.45 s. +/// Never fewer than two lanes: one would finish frames in a single file. +const LANE_WIDTH: usize = 4; + impl Default for Frames { fn default() -> Self { + Self::with_lane_width(LANE_WIDTH) + } +} + +impl Frames { + /// A frame cache whose lanes are `width` threads each (see [`LANE_WIDTH`]); `Default` uses + /// [`LANE_WIDTH`], benchmarks compare widths. + pub fn with_lane_width(width: usize) -> Self { + #[cfg(target_arch = "wasm32")] + let _ = width; #[cfg(not(target_arch = "wasm32"))] let threads = std::thread::available_parallelism().map(|n| n.get()).unwrap_or(4).clamp(2, 16); #[cfg(not(target_arch = "wasm32"))] - let lanes: Vec = (0..threads) + let width = width.clamp(1, (threads / 2).max(1)); + #[cfg(not(target_arch = "wasm32"))] + let lanes: Vec = (0..(threads / width).max(1)) .map_while(|i| { rayon::ThreadPoolBuilder::new() - .num_threads(1) + .num_threads(width) .thread_name(move |_| format!("ec-preview-{i}")) .panic_handler(|_| log::error!("a frame render panicked; that frame was skipped")) .build() @@ -639,7 +764,7 @@ impl Default for Frames { }) .collect(); #[cfg(not(target_arch = "wasm32"))] - let queue = Queue { idle_lanes: (0..lanes.len()).collect(), lanes: lanes.len(), ..Default::default() }; + let queue = Queue { idle_lanes: (0..lanes.len()).collect(), lanes: lanes.len(), gpu_limit: 1, ..Default::default() }; #[cfg(target_arch = "wasm32")] let queue = Queue::default(); Frames { @@ -681,6 +806,15 @@ impl Default for Frames { disk_series: Arc::default(), } } + + /// How many frames render at once on the lanes (a playing preview's prefetch, Cache Frames + /// When Idle); in the browser, the frame workers. + pub fn lanes(&self) -> usize { + #[cfg(target_arch = "wasm32")] + return self.remote_slots().max(1); + #[cfg(not(target_arch = "wasm32"))] + self.queue.lock().map_or(1, |q| q.lanes.max(1)) + } } /// Whether viewer frames of a project with `settings` are half floats ([`F16Frame`], rendered @@ -1282,6 +1416,8 @@ impl Frames { #[cfg(not(target_arch = "wasm32"))] let moved = q.playhead.is_some() != playhead.is_some(); q.playhead = playhead; + // (called once per frame of the window while a preview plays) + let more_gpu = q.ui_frame(playhead.map(|_| web_time::Instant::now())); if let Some(p) = playhead && let Ok(mut inf) = self.inflight.lock() { @@ -1293,6 +1429,13 @@ impl Frames { !gone }); } + // A lane more may composite: one that stopped at the limit starts again. + #[cfg(not(target_arch = "wasm32"))] + if more_gpu && !moved { + self.start_lanes(&mut q, 1); + } + #[cfg(target_arch = "wasm32")] + let _ = more_gpu; #[cfg(not(target_arch = "wasm32"))] if moved { self.start_lanes(&mut q, usize::MAX); @@ -1597,19 +1740,44 @@ struct Worker { thread_local! { /// This pool thread is inside [`Worker::drain`] (rendering a frame). static DRAINING: std::cell::Cell = const { std::cell::Cell::new(false) }; + /// Only a lane's drain thread may wait for GPU admission (see [`GpuRenderSlots::for_render`]). + static DRAINING_LANE: std::cell::Cell = const { std::cell::Cell::new(false) }; } /// Clears [`DRAINING`] when the drain ends, even by a panic. #[cfg(not(target_arch = "wasm32"))] struct Draining; +#[cfg(not(target_arch = "wasm32"))] +impl Draining { + fn enter(lane: Option) -> Option { + if DRAINING.replace(true) { + return None; + } + DRAINING_LANE.set(lane.is_some()); + Some(Self) + } +} + #[cfg(not(target_arch = "wasm32"))] impl Drop for Draining { fn drop(&mut self) { + DRAINING_LANE.set(false); DRAINING.set(false); } } +/// A lane's hold on one of the [`Queue::gpu_limit`] GPU slots (see [`Queue::gpu_lanes`]), given back when dropped, +/// even by a panic. +struct GpuSlot<'a>(&'a Mutex); + +impl Drop for GpuSlot<'_> { + fn drop(&mut self) { + let mut q = self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + q.gpu_lanes = q.gpu_lanes.saturating_sub(1); + } +} + impl Worker { /// Render queued jobs, most important first, until the queue is empty. /// @@ -1623,11 +1791,7 @@ impl Worker { /// `lane`: the lane this runs on (see [`Queue::idle_lanes`]), `None` on the pool. #[cfg(not(target_arch = "wasm32"))] fn drain(&self, lane: Option) { - if DRAINING.get() { - return; - } - DRAINING.set(true); - let _reset = Draining; + let Some(_reset) = Draining::enter(lane) else { return }; while self.run_next(lane) {} } @@ -1636,19 +1800,25 @@ impl Worker { /// idle again. fn run_next(&self, lane: Option) -> bool { let Worker { queue, content_keys, .. } = self; - let job = { + // (asked before the queue's lock: it takes the cache's) + let gpu = lane.is_some() && self.gpu_ready(); + let (job, mut slot) = { let Ok(mut q) = queue.lock() else { return false }; - let Some(i) = q.next(lane.is_some()) else { + let Some(i) = q.next(lane.is_some(), gpu) else { q.idle_lanes.extend(lane); return false; }; let job = q.jobs.swap_remove(i); + let slot = (gpu && q.gpu_job(&job)).then(|| { + q.gpu_lanes += 1; + GpuSlot(queue) + }); q.running.insert(job.key); if job.urgent { q.urgent_running += 1; q.urgent_cancels.push((job.key, job.cancel.clone())); } - job + (job, slot) }; let t0 = web_time::Instant::now(); // (the drop generation before the frame reads anything: File ▸ Reload Footage meanwhile @@ -1657,6 +1827,10 @@ impl Worker { job.src.disk.as_ref().map(|dc| (dc.clone(), frame_disk_key(content_keys, &job.src.project, &job.key, opts_hash(&job.opts)), dc.drop_generation())); let from_disk = disk.as_ref().and_then(|(dc, k, _)| self.disk_frame(dc, *k, &job)); let disk_hit = from_disk.is_some(); + if disk_hit { + // Nothing for the GPU to do: another lane may composite meanwhile. + slot = None; + } // `files`: the source files the frame was made of (see `render::reads`). let (ci, files) = match from_disk { Some(hit) => hit, @@ -1683,6 +1857,8 @@ impl Worker { // and compression. Persistence still runs on this worker, using the same snapshot. let since = disk.as_ref().map(|(dc, _, since)| DiskSince::of(dc, *since)); self.finish(&job, ci.clone(), Some(files.clone()), since, t0); + // The GPU's part is done; storing it on disk is the CPU's. + drop(slot); if !disk_hit && let Some((dc, k, since)) = &disk && put_disk_frame(dc, *k, &ci, &files, self.virtual_id.as_deref(), *since) @@ -1812,7 +1988,10 @@ impl Worker { r.reads = Some(&files); r.accel = Some(g as &dyn effectcraft_engine::render::Accelerator); r.active_accel()?; - let f = g.within_memory(|| g.render_display(&r, job.comp, job.t)); + let f = { + let _slot = GPU_RENDER_SLOTS.try_acquire(MAX_GPU_RENDERS)?; + g.within_memory(|| g.render_display(&r, job.comp, job.t)) + }; effectcraft_engine::render::Accelerator::frame_end(g); match f { Ok(f) => { @@ -1850,55 +2029,86 @@ impl Worker { { // The browser cannot wait for the GPU to time it (and its CPU path is slow): always // the GPU there. - let auto = if cfg!(target_arch = "wasm32") { None } else { r.frame_auto(a, job.comp, true) }; - if auto.is_none_or(|(p, key)| p.choose(key)) { + // Every exit that doesn't record ends the frame unmeasured: the ticket is dropped + // before a retry (`take`), or on return. + let mut ticket = if cfg!(target_arch = "wasm32") { None } else { r.frame_auto(a, job.comp, true).map(|(p, key)| p.ticket(key)) }; + if ticket.as_ref().is_none_or(AutoTicket::gpu) { let t0 = web_time::Instant::now(); - match g.within_memory(|| g.render_display(&r, job.comp, job.t)) { + match gpu_attempt(g, || g.render_display(&r, job.comp, job.t)) { Ok(Some(f)) => { self.gpu_succeeded(); - if let Some((p, key)) = auto { + if let Some(t) = ticket.take() { // Time the GPU's work, not just its recording. g.wait(); if g.context().check_health().is_err() { + drop(t); return self.render_job(job, false, files); } - p.record(key, true, t0.elapsed().as_secs_f64() * 1000.0); + t.record(t0.elapsed().as_secs_f64() * 1000.0); } return FrameImage::Gpu(Arc::new(f)); } Ok(None) => { - if let Some((p, key)) = auto { - p.declined(key); + if let Some(t) = &mut ticket { + t.declined(); } } Err(e) => { - self.gpu_attempt_failed(g, &e); + drop(ticket.take()); + self.gpu_attempt_rejected(g, &e); return self.render_job(job, false, files); } } } let t0 = web_time::Instant::now(); - let img = cpu_frame(&r.comp_frame_cpu(job.comp, job.t), &job.src.project, &job.opts); - if let Some((p, key)) = auto { - p.record(key, false, t0.elapsed().as_secs_f64() * 1000.0); + // CPU compositing in Auto can still run GPU layer effects. Keep those inside the + // same admission and error scope as a GPU-composited frame (#3028). + let img = match gpu_attempt(g, || r.comp_frame_cpu(job.comp, job.t)) { + Ok(img) => cpu_frame(&img, &job.src.project, &job.opts), + Err(e) => { + drop(ticket.take()); + self.gpu_attempt_rejected(g, &e); + return self.render_job(job, false, files); + } + }; + if let Some(t) = ticket { + t.record(t0.elapsed().as_secs_f64() * 1000.0); } return img; } if let Some(g) = gpu { // The GPU composites and the frame is read back. - return match g.within_memory(|| r.comp_frame(job.comp, job.t)) { + return match r.comp_frame_scoped(job.comp, job.t, |a| gpu_attempt(g, || a.comp_frame(&r, job.comp, job.t))) { Ok(img) => { self.gpu_succeeded(); cpu_frame(&img, &job.src.project, &job.opts) } Err(e) => { - self.gpu_attempt_failed(g, &e); + self.gpu_attempt_rejected(g, &e); self.render_job(job, false, files) } }; } cpu_frame(&r.comp_frame(job.comp, job.t), &job.src.project, &job.opts) } + /// Busy shared-pool workers retry on the CPU without treating contention as a GPU fault. + fn gpu_attempt_rejected(&self, gpu: &Gpu, error: &GpuAttemptError) { + if let GpuAttemptError::Failed(error) = error { + self.gpu_attempt_failed(gpu, error); + } + } +} + +enum GpuAttemptError { + Busy, + Failed(FrameError), +} + +/// Bound all preview GPU attempts, including effects in an Auto CPU composite. The permit +/// ends before the caller retries on the CPU, so failed frames cannot block GPU admissions. +fn gpu_attempt(gpu: &Gpu, render: impl FnOnce() -> T) -> Result { + let _slot = GPU_RENDER_SLOTS.for_render(MAX_GPU_RENDERS).ok_or(GpuAttemptError::Busy)?; + gpu.within_memory(render).map_err(GpuAttemptError::Failed) } /// Who asked for a frame (see [`Frames::request_with`]). @@ -1951,6 +2161,24 @@ struct Queue { /// caching left the same gaps, and held up the viewer's frame when it shared a piece. lanes: usize, idle_lanes: Vec, + /// Lanes compositing a frame on the GPU, at most [`Queue::gpu_limit`]. The GPU is the + /// viewer's device: each lane records, submits and reads back a whole frame on it, and the + /// window can't draw until that work is done. With a lane per core on a GPU that can't keep + /// up, the window froze for hundreds of milliseconds at a time while a preview played, and + /// the preview jumped ahead when it came back (#3345). The other lanes go on with the frames + /// that don't need the GPU (the disk cache's, or all of them while the GPU is out of memory + /// or retired); the pool and the viewer's frame are not counted. + gpu_lanes: usize, + /// How many lanes may composite on the GPU at a time (at least 1, see [`Queue::ui_frame`]). + gpu_limit: usize, + /// The limit that last stalled the window: tried again only after [`GPU_LANE_RETRY`]. + gpu_stalled_at: usize, + /// While a preview plays: the window's last frame, and since when it has been smooth. + ui_last: Option, + ui_smooth_since: Option, + /// Tests: as if there were a GPU compositor (no adapter is there). + #[cfg(test)] + fake_gpu: bool, } impl Queue { @@ -1959,9 +2187,63 @@ impl Queue { self.lanes > 0 && !j.urgent && (j.background || self.playhead.is_some()) } - /// The job a lane (`lane`) or the pool renders next: the [`Queue::best`] of its own. - fn next(&self, lane: bool) -> Option { - self.best_of(|j| self.on_lane(j) == lane) + /// The job a lane (`lane`) or the pool renders next: the [`Queue::best`] of its own. `gpu`: + /// the GPU composites (a lane's frames), so a lane takes no GPU frame while + /// [`Queue::gpu_limit`] lanes are compositing. + fn next(&self, lane: bool, gpu: bool) -> Option { + let gpu_full = gpu && self.gpu_lanes >= self.gpu_limit.max(1); + self.best_of(|j| self.on_lane(j) == lane && !(gpu_full && self.gpu_job(j))) + } + + /// The window drew a frame at `now` (`None`: no preview plays). A window that stalls with + /// lanes compositing gets fewer of them: the GPU can't keep up with them and the window. One + /// that stays smooth while every allowed lane composites gets one more, up to all of them: a + /// GPU that keeps up renders the preview on every lane. A limit that stalled is tried again + /// only after a longer smooth spell, so the window doesn't stall over and over. `true` when + /// one more lane may composite. + fn ui_frame(&mut self, now: Option) -> bool { + let Some(now) = now else { + self.ui_last = None; + return false; + }; + let limit = self.gpu_limit.max(1); + let gap = self.ui_last.map(|last| now.saturating_duration_since(last)); + self.ui_last = Some(now); + let Some(gap) = gap else { + self.ui_smooth_since = Some(now); + return false; + }; + if gap > UI_STALL && self.gpu_lanes > 0 { + self.gpu_stalled_at = limit; + self.gpu_limit = (limit / 2).max(1); + self.ui_smooth_since = Some(now); + } else if gap > UI_SMOOTH { + self.ui_smooth_since = Some(now); + } else { + let since = *self.ui_smooth_since.get_or_insert(now); + let wait = if limit + 1 >= self.gpu_stalled_at && self.gpu_stalled_at > 0 { GPU_LANE_RETRY } else { GPU_LANE_STEP }; + if self.gpu_lanes >= limit && limit < self.lanes && now.saturating_duration_since(since) >= wait { + self.gpu_limit = limit + 1; + self.ui_smooth_since = Some(now); + return true; + } + } + false + } + + /// Whether `j` is composited on the GPU when the GPU is ready: there is a GPU compositor and + /// the frame's renderer uses it (as `Renderer::active_accel` decides: Software Only keeps + /// every frame on the CPU). + fn gpu_job(&self, j: &Job) -> bool { + let adapter = j.src.gpu.is_some(); + #[cfg(test)] + let adapter = adapter || self.fake_gpu; + adapter + && match j.opts.backend { + effectcraft_engine::render::Backend::Cpu => false, + effectcraft_engine::render::Backend::Gpu => true, + effectcraft_engine::render::Backend::Auto => j.src.project.settings.gpu_acceleration, + } } /// The job that matters most: urgent first (newest), then, while a preview plays, the diff --git a/crates/ui-egui/src/frames/tests.rs b/crates/ui-egui/src/frames/tests.rs index 360ddf1b6..45398de21 100644 --- a/crates/ui-egui/src/frames/tests.rs +++ b/crates/ui-egui/src/frames/tests.rs @@ -610,15 +610,21 @@ struct ParallelClip { started: std::sync::atomic::AtomicUsize, /// Frames started by the time frame 1 was done. started_at_first: std::sync::atomic::AtomicUsize, + /// Frames decoding now, and the most at a time. + active: std::sync::atomic::AtomicUsize, + most_active: std::sync::atomic::AtomicUsize, } impl FootageSource for ParallelClip { fn frame(&self, _: ItemId, _: &effectcraft_engine::project::Footage, t: Tick) -> Option> { self.started.fetch_add(1, Ordering::SeqCst); + let active = self.active.fetch_add(1, Ordering::SeqCst) + 1; + self.most_active.fetch_max(active, Ordering::SeqCst); (0..16).into_par_iter().for_each(|_| (0..4).into_par_iter().for_each(|_| std::thread::sleep(std::time::Duration::from_millis(1)))); if effectcraft_engine::time::FrameRate::FPS_30.frame_at(t) == 1 { self.started_at_first.store(self.started.load(Ordering::SeqCst), Ordering::SeqCst); } + self.active.fetch_sub(1, Ordering::SeqCst); Some(Arc::new(effectcraft_engine::render::Image::new(4, 4))) } } @@ -729,6 +735,166 @@ fn queued_frames_render_when_a_preview_starts_or_stops() { assert_eq!(f.cached_frames(&series).len(), 119); } +/// Waits for the lanes to be idle (after their last frame they give back their GPU slot) and +/// checks every GPU slot was given back. +fn wait_for_idle_lanes(f: &Frames) { + for _ in 0..2000 { + let q = f.queue.lock().unwrap(); + if q.idle_lanes.len() == f.lanes.len() { + break; + } + drop(q); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + let q = f.queue.lock().unwrap(); + assert_eq!(q.idle_lanes.len(), f.lanes.len(), "every lane is idle again"); + assert_eq!(q.gpu_lanes, 0, "every lane gave its GPU slot back"); +} + +/// Plays a preview of `frames` frames of a [`parallel_clip`] as the viewer renders them (the +/// project's renderer: `backend` Auto) or with `backend`, as if there were a GPU compositor +/// (`gpu`), `retired` or with the project on Mercury Software Only (`!gpu_acceleration`), and +/// waits for them: the frame cache and the most frames that rendered at a time. +fn play_preview(gpu: bool, retired: bool, backend: effectcraft_engine::render::Backend, gpu_acceleration: bool) -> (Frames, usize) { + use effectcraft_engine::time::FrameRate; + let (f, footage, mut src, cid, series) = parallel_clip(); + Arc::make_mut(&mut src.project).settings.gpu_acceleration = gpu_acceleration; + f.queue.lock().unwrap().fake_gpu = gpu; + if retired { + f.retire_gpu(); + } + let opts = RenderOpts { backend, ..RenderOpts::default() }; + f.set_playhead(Some(Playhead { series, cur: 0, start: 0, end: 299, step: 1, looping: false })); + let frames = 24; + for frame in 1..=frames { + f.request(&src, FrameKey { frame, ..series }, cid, FrameRate::FPS_30.tick_of(frame), opts); + } + wait_for_renders(&f); + assert_eq!(f.cached_frames(&series).len(), frames as usize); + wait_for_idle_lanes(&f); + let most = footage.most_active.load(Ordering::SeqCst); + (f, most) +} + +/// Every lane composited a playing preview's frame on the GPU at the same time, on the device +/// the window draws with: the window froze for hundreds of milliseconds and the preview jumped +/// ahead (#3345). A preview starts on one GPU lane (see [`Queue::ui_frame`]). +#[test] +fn a_preview_composites_on_a_few_lanes_at_a_time() { + use effectcraft_engine::render::Backend; + for backend in [Backend::Auto, Backend::Gpu] { + let (_, most) = play_preview(true, false, backend, true); + assert!(most <= 1, "{most} lanes composited on the GPU at once ({backend:?})"); + } +} + +/// Frames that render on the CPU use every lane: with no GPU compositor, with the GPU retired, +/// and with Mercury Software Only (the project's renderer, or the CPU backend). +#[test] +fn frames_on_the_cpu_render_on_every_lane() { + use effectcraft_engine::render::Backend; + for (gpu, retired, backend, gpu_acceleration) in + [(false, false, Backend::Auto, true), (true, true, Backend::Auto, true), (true, false, Backend::Auto, false), (true, false, Backend::Cpu, true)] + { + let (f, most) = play_preview(gpu, retired, backend, gpu_acceleration); + if f.lanes.len() > 2 { + assert!(most > 2, "frames on the CPU rendered on {most} lanes at a time ({gpu} {retired} {backend:?} {gpu_acceleration})"); + } + } +} + +/// A queue of `lanes` lanes, `busy` of them compositing on the GPU. +fn gpu_queue(lanes: usize, busy: usize) -> Queue { + Queue { lanes, gpu_lanes: busy, gpu_limit: 1, ..Default::default() } +} + +/// Window frames every `gap` ms from `t` for `ms` ms; the time after the last. +fn ui_frames(q: &mut Queue, t: web_time::Instant, gap: u64, ms: u64) -> web_time::Instant { + let mut t = t; + for _ in 0..ms / gap { + t += std::time::Duration::from_millis(gap); + q.ui_frame(Some(t)); + } + t +} + +/// A GPU that keeps up with the window gets every lane, one more each half second the window +/// stays smooth with every allowed lane compositing. +#[test] +fn a_smooth_window_gives_the_gpu_every_lane() { + let mut q = gpu_queue(8, 0); + let t = web_time::Instant::now(); + q.ui_frame(Some(t)); + // Nothing compositing: the limit isn't what holds the preview back. + let t = ui_frames(&mut q, t, 16, 2000); + assert_eq!(q.gpu_limit, 1); + let mut t = t; + for _ in 0..20 { + q.gpu_lanes = q.gpu_limit; + t = ui_frames(&mut q, t, 16, 520); + } + assert_eq!(q.gpu_limit, 8, "no more than the lanes"); +} + +/// A window that stalls while lanes composite halves the limit, and the limit that stalled is +/// tried again only after a long smooth spell; a window that is only slow keeps the limit. +#[test] +fn a_stalling_window_takes_lanes_from_the_gpu() { + let mut q = gpu_queue(8, 4); + q.gpu_limit = 4; + let t = web_time::Instant::now(); + q.ui_frame(Some(t)); + let t = ui_frames(&mut q, t, 300, 300); + assert_eq!((q.gpu_limit, q.gpu_stalled_at), (2, 4)); + let t = ui_frames(&mut q, t, 80, 5000); + assert_eq!(q.gpu_limit, 2, "a slow (not stalling) window keeps the limit"); + q.gpu_lanes = 2; + let t = ui_frames(&mut q, t, 16, 600); + assert_eq!(q.gpu_limit, 3); + q.gpu_lanes = 3; + let t = ui_frames(&mut q, t, 16, 2000); + assert_eq!(q.gpu_limit, 3, "4 stalled: it waits longer"); + ui_frames(&mut q, t, 16, 10_000); + assert_eq!(q.gpu_limit, 4); + // A stall with nothing compositing isn't the GPU's. + let mut q = gpu_queue(8, 0); + q.gpu_limit = 4; + let t = web_time::Instant::now(); + q.ui_frame(Some(t)); + ui_frames(&mut q, t, 500, 500); + assert_eq!(q.gpu_limit, 4); + // Never below one lane; a stopped preview's pause is no stall. + let mut q = gpu_queue(8, 1); + let t = web_time::Instant::now(); + q.ui_frame(Some(t)); + ui_frames(&mut q, t, 400, 400); + assert_eq!(q.gpu_limit, 1); + q.gpu_limit = 2; + q.ui_frame(None); + q.ui_frame(Some(t + std::time::Duration::from_secs(5))); + assert_eq!(q.gpu_limit, 2); +} + +/// A preview that stops while lanes composite: their frames finish, the queued ones move to the +/// pool and render, the viewer's frame is not held up, and every GPU slot is given back. +#[test] +fn a_preview_stopping_mid_render_gives_the_gpu_slots_back() { + use effectcraft_engine::time::FrameRate; + let (f, _, src, cid, series) = parallel_clip(); + f.queue.lock().unwrap().fake_gpu = true; + let opts = RenderOpts { backend: effectcraft_engine::render::Backend::Auto, ..RenderOpts::default() }; + f.set_playhead(Some(Playhead { series, cur: 0, start: 0, end: 299, step: 1, looping: false })); + for frame in 1..=24 { + f.request(&src, FrameKey { frame, ..series }, cid, FrameRate::FPS_30.tick_of(frame), opts); + } + std::thread::sleep(std::time::Duration::from_millis(20)); + f.set_playhead(None); + f.request_urgent(&src, FrameKey { frame: 100, ..series }, cid, FrameRate::FPS_30.tick_of(100), opts); + wait_for_renders(&f); + assert_eq!(f.cached_frames(&series).len(), 25); + wait_for_idle_lanes(&f); +} + /// The cache is full. A zoom (or preview-resolution) change left the frames of the old /// size to the plain least-recently-shown order, so the frames of the zoom the viewer is back /// at went before the ones of the abandoned zoom. The other size's frames go first, then the @@ -2957,3 +3123,301 @@ mod frame_files { assert_eq!(healed.hits(), (1, 0), "the rewritten entry is used"); } } + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn gpu_renders_in_flight_stay_within_the_cap() { + use std::sync::atomic::AtomicUsize; + let slots = GpuRenderSlots::new(); + let now = AtomicUsize::new(0); + let peak = AtomicUsize::new(0); + std::thread::scope(|s| { + for _ in 0..16 { + s.spawn(|| { + let _slot = slots.acquire(2); + let n = now.fetch_add(1, Ordering::SeqCst) + 1; + peak.fetch_max(n, Ordering::SeqCst); + std::thread::sleep(std::time::Duration::from_millis(5)); + now.fetch_sub(1, Ordering::SeqCst); + }); + } + }); + assert!(peak.load(Ordering::SeqCst) <= 2); + assert_eq!(*slots.busy.lock().unwrap_or_else(PoisonError::into_inner), 0); +} + +/// #3028: exercise the actual worker routes, not only the admission counter. The Auto CPU +/// compositor still has a live accelerator for its layer effects. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn gpu_worker_routes_admit_display_readback_and_auto_effects() { + use effectcraft_engine::render::{Accelerator, Backend}; + let Some(gpu) = Gpu::headless() else { + eprintln!("GPU worker route test skipped: no adapter"); + return; + }; + let (frames, mut src, key, _, _, _) = disk_fixture(&[]); + let mut session = effectcraft_engine::Session::default(); + session.execute("comp.new", serde_json::json!({"width": 16, "height": 16, "duration": 1})).unwrap(); + let comp = session.active_comp_id().unwrap(); + let solid = session.execute("layer.newSolid", serde_json::json!({"color": "#ff0000"})).unwrap()["layer"].clone(); + session.execute("effect.apply", serde_json::json!({"layers": [solid], "effect": "Gaussian Blur"})).unwrap(); + src.project = session.project; + src.gpu = Some(gpu.clone()); + src.gpu_display = true; + let mut job = Job { + key, + src, + comp, + t: Tick::ZERO, + opts: RenderOpts { backend: Backend::Gpu, ..Default::default() }, + urgent: true, + background: false, + seq: 0, + cancel: Arc::default(), + }; + let worker = frames.worker(); + let admissions = || GPU_RENDER_SLOTS.entered.load(Ordering::SeqCst); + // This path is called on the UI thread by dispatch_remote: a full GPU must decline + // immediately so the frame can go to the remote worker and the window keeps painting. + let first = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS); + let second = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS); + let (tx, rx) = std::sync::mpsc::channel(); + let declined = std::thread::scope(|scope| { + scope.spawn(|| tx.send(worker.render_gpu(&job).is_none()).unwrap()); + let result = rx.recv_timeout(std::time::Duration::from_secs(2)); + drop((first, second)); + result + }); + assert_eq!(declined, Ok(true), "UI display must not wait for GPU admission"); + let before = admissions(); + assert!(worker.render_gpu(&job).is_some(), "direct display path renders"); + assert!(admissions() > before, "direct display bypassed GPU admission"); + + let before = admissions(); + assert!(worker.render_job(&job, true, &ReadLog::new()).is_gpu()); + assert!(admissions() > before, "worker display bypassed GPU admission"); + + job.src.gpu_display = false; + job.src.layer_cache.clear(); + let before = admissions(); + assert!(!worker.render_job(&job, true, &ReadLog::new()).is_gpu()); + assert!(admissions() > before, "readback bypassed GPU admission"); + + job.src.gpu_display = true; + job.src.layer_cache.clear(); + job.opts.backend = Backend::Auto; + let renderer = Renderer::new(&job.src.project, job.src.footage.as_ref(), job.opts); + let (pick, key) = renderer.frame_auto(&gpu, comp, true).unwrap(); + pick.declined(key); // Select CPU compositing while retaining GPU-capable layer effects. + let before = admissions(); + assert!(!worker.render_job(&job, true, &ReadLog::new()).is_gpu()); + assert!(admissions() > before, "Auto CPU effects bypassed GPU admission"); + + let before = admissions(); + assert!(!worker.render_job(&job, false, &ReadLog::new()).is_gpu()); + assert_eq!(admissions(), before, "CPU retry must not hold a GPU slot"); + assert_eq!(*GPU_RENDER_SLOTS.busy.lock().unwrap(), 0); + + // Saturation in the shared Rayon pool is contention, not a GPU fault. It must finish + // through the fresh CPU route without admitting GPU effects or retiring the device. + let first = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS); + let second = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS); + let before = admissions(); + let pool = rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap(); + let (tx, rx) = std::sync::mpsc::channel(); + let cpu = std::thread::scope(|scope| { + scope.spawn(|| pool.install(|| tx.send(!worker.render_job(&job, true, &ReadLog::new()).is_gpu()).unwrap())); + let result = rx.recv_timeout(std::time::Duration::from_secs(2)); + drop((first, second)); + result + }); + assert_eq!(cpu, Ok(true), "busy pool worker must complete on the CPU"); + assert_eq!(admissions(), before); + assert!(worker.gpu_ready()); + assert!(gpu.context().check_health().is_ok()); + + // The real dedicated-lane drain waits, then caches a GPU frame instead of silently + // switching a healthy preview to CPU rendering while the other lanes are busy. + frames.queue.lock().unwrap().jobs.push(Job { + key: job.key, + src: job.src.clone(), + comp, + t: job.t, + opts: RenderOpts { backend: Backend::Gpu, ..job.opts }, + urgent: false, + background: true, + seq: 1, + cancel: Arc::default(), + }); + let first = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS); + let second = GPU_RENDER_SLOTS.acquire(MAX_GPU_RENDERS); + let pool = rayon::ThreadPoolBuilder::new().num_threads(1).build().unwrap(); + let (started, ready) = std::sync::mpsc::channel(); + let (tx, rx) = std::sync::mpsc::channel(); + let (early, finished) = std::thread::scope(|scope| { + scope.spawn(|| { + pool.install(|| { + started.send(()).unwrap(); + worker.drain(Some(0)); + tx.send(()).unwrap(); + }) + }); + ready.recv_timeout(std::time::Duration::from_secs(2)).unwrap(); + let early = rx.recv_timeout(std::time::Duration::from_millis(100)); + drop((first, second)); + let finished = early.or_else(|_| rx.recv_timeout(std::time::Duration::from_secs(2))); + (early, finished) + }); + assert!(early.is_err(), "preview lane must wait for an available GPU permit"); + assert_eq!(finished, Ok(())); + assert!(frames.cache.lock().unwrap().map.get(&job.key).unwrap().img.is_gpu()); + + // A retirement found inside the scope releases the permit before the fresh CPU retry. + gpu.context().device_failure_notifier()("synthetic worker retirement".into()); + let before = admissions(); + assert!(!worker.render_job(&job, true, &ReadLog::new()).is_gpu()); + assert_eq!(admissions(), before + 1, "failed GPU attempt must retry without a second admission"); + assert_eq!(*GPU_RENDER_SLOTS.busy.lock().unwrap(), 0); + gpu.frame_end(); +} + +/// Early returns and unwinding release admission even before another render starts. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn gpu_render_slots_return_after_failure_and_unwind() { + let slots = GpuRenderSlots::new(); + let failed = || -> Result<(), ()> { + let _slot = slots.acquire(2); + Err(()) + }; + assert!(failed().is_err()); + assert_eq!(*slots.busy.lock().unwrap(), 0); + let result = std::panic::catch_unwind(|| { + let _slot = slots.acquire(2); + panic!("synthetic render panic"); + }); + assert!(result.is_err()); + assert_eq!(*slots.busy.lock().unwrap(), 0); +} + +/// A Rayon helper must decline a full gate rather than wait for a render that can itself be +/// waiting for that helper. Release the permits on timeout so a regression fails, not hangs. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn gpu_render_slots_never_block_rayon_helpers() { + let slots = GpuRenderSlots::new(); + let first = slots.acquire(2); + let second = slots.acquire(2); + let pool = rayon::ThreadPoolBuilder::new().num_threads(2).build().unwrap(); + let (tx, rx) = std::sync::mpsc::channel(); + let declined = std::thread::scope(|scope| { + scope.spawn(|| { + pool.install(|| { + rayon::join(|| {}, || tx.send(slots.for_render(2).is_none()).unwrap()); + }); + }); + let result = rx.recv_timeout(std::time::Duration::from_secs(2)); + drop((first, second)); + result + }); + assert_eq!(declined, Ok(true), "Rayon helpers must not wait on GPU permits"); + assert_eq!(*slots.busy.lock().unwrap(), 0); +} + +/// A lane's drain thread can wait without creating the shared-pool helper dependency cycle, +/// and must keep rendering on a healthy GPU; the lane's other threads ([`LANE_WIDTH`] wide) +/// help with its frame and never wait. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn gpu_preview_lane_waits_for_admission_and_resets_its_marker() { + let slots = GpuRenderSlots::new(); + let first = slots.acquire(2); + let second = slots.acquire(2); + let pool = rayon::ThreadPoolBuilder::new().num_threads(LANE_WIDTH).build().unwrap(); + let (started, ready) = std::sync::mpsc::channel(); + let (tx, rx) = std::sync::mpsc::channel(); + let (early, admitted, helper) = std::thread::scope(|scope| { + scope.spawn(|| { + pool.install(|| { + let _drain = Draining::enter(Some(0)).unwrap(); + assert!(Draining::enter(None).is_none(), "a lane cannot start a nested drain"); + assert!(DRAINING_LANE.get(), "nested rejection must not clear the lane marker"); + started.send(()).unwrap(); + tx.send(slots.for_render(2).is_some()).unwrap(); + }); + }); + ready.recv_timeout(std::time::Duration::from_secs(2)).unwrap(); + // Another thread of the same lane, while its drain thread waits: it declines. + let (htx, hrx) = std::sync::mpsc::channel(); + let (pool, slots) = (&pool, &slots); + scope.spawn(move || pool.install(|| htx.send(slots.for_render(2).is_none()).unwrap())); + let helper = hrx.recv_timeout(std::time::Duration::from_secs(2)); + let early = rx.recv_timeout(std::time::Duration::from_millis(100)); + drop((first, second)); + let admitted = early.or_else(|_| rx.recv_timeout(std::time::Duration::from_secs(2))); + (early, admitted, helper) + }); + assert!(early.is_err(), "busy dedicated lane must wait instead of falling back to CPU"); + assert_eq!(admitted, Ok(true)); + assert_eq!(helper, Ok(true), "a lane's helper thread must not wait on GPU permits"); + pool.install(|| { + assert!(!DRAINING.get()); + assert!(!DRAINING_LANE.get()); + let panic = std::panic::catch_unwind(|| { + let _drain = Draining::enter(Some(0)).unwrap(); + panic!("synthetic lane failure"); + }); + assert!(panic.is_err()); + assert!(!DRAINING.get()); + assert!(!DRAINING_LANE.get()); + }); +} + +#[test] +fn effect_detection_answers_each_comp_once() { + // Sixteen levels, each comp holding eight layers of the one below and no effects: 8^16 walks + // without a memo. Answered at once. + use effectcraft_engine::project::{Comp, ItemKind, LayerSource, build}; + use effectcraft_engine::time::{FrameRate, Tick}; + let mut p = Project::default(); + let base = Comp::new(8, 8, FrameRate::new(25, 1), Tick::from_seconds_f64(1.0)); + let mut below = p.add_item("C", effectcraft_engine::color::Label::Sandstone, None, ItemKind::Comp(base.clone().into())); + for _ in 0..16 { + let layers: Vec<_> = (0..8).map(|_| build::layer(&mut p, &base, "a", LayerSource::Comp { item: below }, (0, 0), None)).collect(); + let mut c = base.clone(); + c.layers = layers.into(); + below = p.add_item("C", effectcraft_engine::color::Label::Sandstone, None, ItemKind::Comp(c.into())); + } + let start = std::time::Instant::now(); + assert!(!comp_has_effects(&p, below)); + assert!(start.elapsed() < std::time::Duration::from_secs(5)); +} + +#[test] +fn an_effect_found_by_a_short_route_is_not_hidden_by_a_deep_one() { + use effectcraft_engine::Session; + use serde_json::json; + let mut s = Session::default(); + let id = |v: serde_json::Value, k: &str| v[k].as_u64().unwrap(); + let comp = |s: &mut Session| ItemId(id(s.execute("comp.new", json!({"name": "C", "width": 8, "height": 8, "duration": 1})).unwrap(), "comp")); + let nest = |s: &mut Session, outer: ItemId, inner: ItemId| { + s.execute("layer.addItem", json!({"comp": outer.0, "item": inner.0, "time": 0.0})).unwrap(); + }; + // `fx` holds a layer with an effect; seventeen comps stack above it, one level too many to be found. + let fx = comp(&mut s); + let l = id(s.execute("layer.newSolid", json!({"comp": fx.0, "color": "#ff0000"})).unwrap(), "layer"); + s.execute("effect.apply", json!({"comp": fx.0, "layers": [l], "effect": "ec.vr.converter"})).unwrap(); + let mut chain = fx; + for _ in 0..17 { + let up = comp(&mut s); + nest(&mut s, up, chain); + chain = up; + } + assert!(!comp_has_effects(&s.project, chain), "too deep to be found"); + // A root that nests the chain first and `fx` directly after it. + let root = comp(&mut s); + nest(&mut s, root, chain); + nest(&mut s, root, fx); + assert!(comp_has_effects(&s.project, root)); +} diff --git a/crates/ui-egui/src/gpu_failure.rs b/crates/ui-egui/src/gpu_failure.rs index 275112912..a0840b790 100644 --- a/crates/ui-egui/src/gpu_failure.rs +++ b/crates/ui-egui/src/gpu_failure.rs @@ -2,7 +2,9 @@ //! embedding host, never to a library borrowing its device. This bridge does no device work, //! document mutation, file I/O or renderer locking inside a notification. -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, OnceLock}; + +pub use effectcraft_gpu::{ScopedError, ScopedErrorObserver}; type Notifier = Arc; @@ -25,11 +27,22 @@ struct State { pub struct GpuFailureBridge { state: Arc>, ctx: egui::Context, + scoped_observer: Arc>, } impl GpuFailureBridge { pub fn new(ctx: &egui::Context) -> Self { - Self { state: Arc::default(), ctx: ctx.clone() } + Self { state: Arc::default(), ctx: ctx.clone(), scoped_observer: Arc::default() } + } + + /// Register the host observer before handing this bridge to the app. Scoped errors do + /// not retire the device: they remain recoverable per-frame observations. + pub fn set_scoped_error_observer(&self, observer: impl Fn(ScopedError) + Send + Sync + 'static) -> bool { + self.scoped_observer.set(Arc::new(observer)).is_ok() + } + + pub(crate) fn scoped_error_observer(&self) -> Option { + self.scoped_observer.get().cloned() } pub fn failure(&self) -> Option { @@ -90,6 +103,18 @@ mod tests { use super::*; use std::sync::atomic::{AtomicUsize, Ordering}; + #[test] + fn scoped_observer_is_shared_without_retiring_the_device() { + let bridge = GpuFailureBridge::new(&egui::Context::default()); + let seen = Arc::new(Mutex::new(Vec::new())); + let copy = seen.clone(); + assert!(bridge.set_scoped_error_observer(move |error| copy.lock().unwrap().push(error))); + assert!(!bridge.set_scoped_error_observer(|_| panic!("must not replace observer"))); + bridge.clone().scoped_error_observer().unwrap()(ScopedError::Internal); + assert_eq!(*seen.lock().unwrap(), [ScopedError::Internal]); + assert!(bridge.failure().is_none()); + } + #[test] fn a_construction_failure_is_replayed_after_notifier_registration() { let bridge = GpuFailureBridge::new(&egui::Context::default()); diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index 68e58f842..bc09e358f 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -506,8 +506,8 @@ const SIMPLIFIED_CHINESE: &[(&str, &str, &str)] = &[ ("file.import", "File...", "文件..."), ("file.importMultiple", "Multiple Files...", "多个文件..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Pro 项目..."), - ("file.importAep", "After Effects Project...", "After Effects 项目..."), - ("file.exportAep", "After Effects Project...", "After Effects 项目..."), + ("file.importAep", "Adobe After Effects Project...", "Adobe After Effects 项目..."), + ("file.exportAep", "Adobe After Effects Project...", "Adobe After Effects 项目..."), ("file.importProject", "EffectCraft Project...", "EffectCraft 项目..."), ("file.importPlaceholder", "Placeholder...", "占位符..."), ("file.importSolid", "Solid...", "纯色..."), @@ -701,7 +701,7 @@ const SIMPLIFIED_CHINESE: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "隐藏锁定的蒙版"), ("", "Mask and Shape Path", "蒙版和形状路径"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "已关闭"), + ("path.toggleClosed", "Closed", "已关闭"), ("path.convertToBezier", "Convert To Bezier Path", "转换为贝塞尔曲线路径"), ("path.setFirstVertex", "Set First Vertex", "设置第一个顶点"), ("path.freeTransform", "Free Transform Points", "自由变换点"), @@ -1054,7 +1054,7 @@ const SIMPLIFIED_CHINESE: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "基本图形"), ("window.panel", "Info", "信息"), ("help.inAppTutorials", "Learn", "学习"), - ("window.panel", "Lumetri Scopes", "Lumetri 范围"), + ("window.panel", "Video Scopes", "视频示波器"), ("window.panel", "Mask Interpolation", "蒙版插值"), ("window.panel", "Media Browser", "媒体浏览器"), ("window.panel", "Metadata", "元数据"), @@ -1146,8 +1146,8 @@ const SWEDISH: &[(&str, &str, &str)] = &[ ("file.import", "File...", "Fil..."), ("file.importMultiple", "Multiple Files...", "Flera filer..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Pro-projekt..."), - ("file.importAep", "After Effects Project...", "After Effects-projekt..."), - ("file.exportAep", "After Effects Project...", "After Effects-projekt..."), + ("file.importAep", "Adobe After Effects Project...", "Adobe After Effects-projekt..."), + ("file.exportAep", "Adobe After Effects Project...", "Adobe After Effects-projekt..."), ("file.importProject", "EffectCraft Project...", "EffectCraft-projekt..."), ("file.importPlaceholder", "Placeholder...", "Platshållare..."), ("file.importSolid", "Solid...", "Enfärgad..."), @@ -1339,7 +1339,7 @@ const SWEDISH: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Dölj låsta masker"), ("", "Mask and Shape Path", "Mask- och formbana"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Sluten"), + ("path.toggleClosed", "Closed", "Sluten"), ("path.convertToBezier", "Convert To Bezier Path", "Konvertera till Bezierbana"), ("path.setFirstVertex", "Set First Vertex", "Ange första hörnpunkt"), ("path.freeTransform", "Free Transform Points", "Fri transformering av punkter"), @@ -1690,7 +1690,7 @@ const SWEDISH: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "Grundläggande grafik"), ("window.panel", "Info", "Info"), ("help.inAppTutorials", "Learn", "Lär dig"), - ("window.panel", "Lumetri Scopes", "Lumetri-scope"), + ("window.panel", "Video Scopes", "Videoskop"), ("window.panel", "Mask Interpolation", "Maskinterpolering"), ("window.panel", "Media Browser", "Mediebläddrare"), ("window.panel", "Metadata", "Metadata"), @@ -1788,8 +1788,8 @@ const TRADITIONAL_CHINESE: &[(&str, &str, &str)] = &[ ("file.import", "File...", "檔案..."), ("file.importMultiple", "Multiple Files...", "多個檔案..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Pro 專案..."), - ("file.importAep", "After Effects Project...", "After Effects 專案..."), - ("file.exportAep", "After Effects Project...", "After Effects 專案..."), + ("file.importAep", "Adobe After Effects Project...", "Adobe After Effects 專案..."), + ("file.exportAep", "Adobe After Effects Project...", "Adobe After Effects 專案..."), ("file.importProject", "EffectCraft Project...", "EffectCraft 專案..."), ("file.importPlaceholder", "Placeholder...", "預留位置..."), ("file.importSolid", "Solid...", "純色..."), @@ -1983,7 +1983,7 @@ const TRADITIONAL_CHINESE: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "隱藏鎖定的遮色片"), ("", "Mask and Shape Path", "遮色片和形狀路徑"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "封閉"), + ("path.toggleClosed", "Closed", "封閉"), ("path.convertToBezier", "Convert To Bezier Path", "轉換為貝茲路徑"), ("path.setFirstVertex", "Set First Vertex", "設定第一個頂點"), ("path.freeTransform", "Free Transform Points", "自由變換點"), @@ -2336,7 +2336,7 @@ const TRADITIONAL_CHINESE: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "基本圖形"), ("window.panel", "Info", "資訊"), ("help.inAppTutorials", "Learn", "學習"), - ("window.panel", "Lumetri Scopes", "Lumetri 示波器"), + ("window.panel", "Video Scopes", "視訊示波器"), ("window.panel", "Mask Interpolation", "遮色片插補"), ("window.panel", "Media Browser", "媒體瀏覽器"), ("window.panel", "Metadata", "中繼資料"), @@ -2427,8 +2427,8 @@ const JAPANESE: &[(&str, &str, &str)] = &[ ("file.import", "File...", "ファイル..."), ("file.importMultiple", "Multiple Files...", "複数のファイル..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Proプロジェクト..."), - ("file.importAep", "After Effects Project...", "After Effectsプロジェクト..."), - ("file.exportAep", "After Effects Project...", "After Effectsプロジェクト..."), + ("file.importAep", "Adobe After Effects Project...", "Adobe After Effectsプロジェクト..."), + ("file.exportAep", "Adobe After Effects Project...", "Adobe After Effectsプロジェクト..."), ("file.importPlaceholder", "Placeholder...", "プレースホルダー..."), ("file.importSolid", "Solid...", "平面..."), ("file.importLottie", "Lottie...", "Lottie..."), @@ -2622,7 +2622,7 @@ const JAPANESE: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "ロックしたマスクを非表示"), ("", "Mask and Shape Path", "マスクとシェイプのパス"), ("path.rotoBezier", "RotoBezier", "ロトベジェ"), - ("mask.setClosed", "Closed", "閉じたパス"), + ("path.toggleClosed", "Closed", "閉じたパス"), ("path.convertToBezier", "Convert To Bezier Path", "ベジェパスに変換"), ("path.setFirstVertex", "Set First Vertex", "最初の頂点を設定"), ("path.freeTransform", "Free Transform Points", "ポイントを自由変形"), @@ -2975,7 +2975,7 @@ const JAPANESE: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "エッセンシャルグラフィックス"), ("window.panel", "Info", "情報"), ("help.inAppTutorials", "Learn", "学習"), - ("window.panel", "Lumetri Scopes", "Lumetriスコープ"), + ("window.panel", "Video Scopes", "ビデオスコープ"), ("window.panel", "Mask Interpolation", "マスク補間"), ("window.panel", "Media Browser", "メディアブラウザー"), ("window.panel", "Metadata", "メタデータ"), @@ -3069,8 +3069,8 @@ const UKRAINIAN: &[(&str, &str, &str)] = &[ ("file.import", "File...", "Файл..."), ("file.importMultiple", "Multiple Files...", "Кілька файлів..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Проєкт Adobe Premiere Pro..."), - ("file.importAep", "After Effects Project...", "Проєкт After Effects..."), - ("file.exportAep", "After Effects Project...", "Проєкт After Effects..."), + ("file.importAep", "Adobe After Effects Project...", "Проєкт Adobe After Effects..."), + ("file.exportAep", "Adobe After Effects Project...", "Проєкт Adobe After Effects..."), ("file.importProject", "EffectCraft Project...", "Проєкт EffectCraft..."), ("file.importPlaceholder", "Placeholder...", "Заповнювач..."), ("file.importSolid", "Solid...", "Суцільний колір..."), @@ -3264,7 +3264,7 @@ const UKRAINIAN: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Приховати заблоковані маски"), ("", "Mask and Shape Path", "Контур маски та фігури"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Замкнений"), + ("path.toggleClosed", "Closed", "Замкнений"), ("path.convertToBezier", "Convert To Bezier Path", "Перетворити на контур Безьє"), ("path.setFirstVertex", "Set First Vertex", "Задати першу вершину"), ("path.freeTransform", "Free Transform Points", "Вільне трансформування точок"), @@ -3617,7 +3617,7 @@ const UKRAINIAN: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "Основна графіка"), ("window.panel", "Info", "Інформація"), ("help.inAppTutorials", "Learn", "Навчання"), - ("window.panel", "Lumetri Scopes", "Вимірювачі Lumetri"), + ("window.panel", "Video Scopes", "Відеоскопи"), ("window.panel", "Mask Interpolation", "Інтерполяція маски"), ("window.panel", "Media Browser", "Браузер медіа"), ("window.panel", "Metadata", "Метадані"), @@ -3712,8 +3712,8 @@ const SPANISH: &[(&str, &str, &str)] = &[ ("file.import", "File...", "Archivo..."), ("file.importMultiple", "Multiple Files...", "Varios archivos..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Proyecto de Adobe Premiere Pro..."), - ("file.importAep", "After Effects Project...", "Proyecto de After Effects..."), - ("file.exportAep", "After Effects Project...", "Proyecto de After Effects..."), + ("file.importAep", "Adobe After Effects Project...", "Proyecto de Adobe After Effects..."), + ("file.exportAep", "Adobe After Effects Project...", "Proyecto de Adobe After Effects..."), ("file.importProject", "EffectCraft Project...", "Proyecto de EffectCraft..."), ("file.importPlaceholder", "Placeholder...", "Marcador de posición..."), ("file.importSolid", "Solid...", "Sólido..."), @@ -3907,7 +3907,7 @@ const SPANISH: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Ocultar máscaras bloqueadas"), ("", "Mask and Shape Path", "Trazado de máscara y de forma"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Cerrado"), + ("path.toggleClosed", "Closed", "Cerrado"), ("path.convertToBezier", "Convert To Bezier Path", "Convertir en trazado Bézier"), ("path.setFirstVertex", "Set First Vertex", "Establecer primer vértice"), ("path.freeTransform", "Free Transform Points", "Transformar puntos libremente"), @@ -4260,7 +4260,7 @@ const SPANISH: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "Gráficos esenciales"), ("window.panel", "Info", "Información"), ("help.inAppTutorials", "Learn", "Aprender"), - ("window.panel", "Lumetri Scopes", "Ámbitos de Lumetri"), + ("window.panel", "Video Scopes", "Monitores de vídeo"), ("window.panel", "Mask Interpolation", "Interpolación de máscara"), ("window.panel", "Media Browser", "Navegador de medios"), ("window.panel", "Metadata", "Metadatos"), @@ -4353,8 +4353,8 @@ const PORTUGUESE: &[(&str, &str, &str)] = &[ ("file.import", "File...", "Arquivo..."), ("file.importMultiple", "Multiple Files...", "Vários arquivos..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Projeto do Adobe Premiere Pro..."), - ("file.importAep", "After Effects Project...", "Projeto do After Effects..."), - ("file.exportAep", "After Effects Project...", "Projeto do After Effects..."), + ("file.importAep", "Adobe After Effects Project...", "Projeto do Adobe After Effects..."), + ("file.exportAep", "Adobe After Effects Project...", "Projeto do Adobe After Effects..."), ("file.importProject", "EffectCraft Project...", "Projeto do EffectCraft..."), ("file.importPlaceholder", "Placeholder...", "Espaço reservado..."), ("file.importSolid", "Solid...", "Sólido..."), @@ -4548,7 +4548,7 @@ const PORTUGUESE: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Ocultar máscaras bloqueadas"), ("", "Mask and Shape Path", "Caminho da máscara e da forma"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Fechado"), + ("path.toggleClosed", "Closed", "Fechado"), ("path.convertToBezier", "Convert To Bezier Path", "Converter em caminho Bézier"), ("path.setFirstVertex", "Set First Vertex", "Definir primeiro vértice"), ("path.freeTransform", "Free Transform Points", "Pontos de transformação livre"), @@ -4901,7 +4901,7 @@ const PORTUGUESE: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "Gráficos essenciais"), ("window.panel", "Info", "Informações"), ("help.inAppTutorials", "Learn", "Aprender"), - ("window.panel", "Lumetri Scopes", "Escopos Lumetri"), + ("window.panel", "Video Scopes", "Escopos de vídeo"), ("window.panel", "Mask Interpolation", "Interpolação de máscara"), ("window.panel", "Media Browser", "Navegador de mídia"), ("window.panel", "Metadata", "Metadados"), @@ -5002,8 +5002,8 @@ const RUSSIAN: &[(&str, &str, &str)] = &[ ("file.import", "File...", "Файл..."), ("file.importMultiple", "Multiple Files...", "Несколько файлов..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Проект Adobe Premiere Pro..."), - ("file.importAep", "After Effects Project...", "Проект After Effects..."), - ("file.exportAep", "After Effects Project...", "Проект After Effects..."), + ("file.importAep", "Adobe After Effects Project...", "Проект Adobe After Effects..."), + ("file.exportAep", "Adobe After Effects Project...", "Проект Adobe After Effects..."), ("file.importProject", "EffectCraft Project...", "Проект EffectCraft..."), ("file.importPlaceholder", "Placeholder...", "Заполнитель..."), ("file.importSolid", "Solid...", "Сплошной цвет..."), @@ -5196,7 +5196,7 @@ const RUSSIAN: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Скрыть заблокированные маски"), ("", "Mask and Shape Path", "Контур маски и фигуры"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Замкнутый"), + ("path.toggleClosed", "Closed", "Замкнутый"), ("path.convertToBezier", "Convert To Bezier Path", "Преобразовать в кривую Безье"), ("path.setFirstVertex", "Set First Vertex", "Задать первую вершину"), ("path.freeTransform", "Free Transform Points", "Свободное трансформирование точек"), @@ -5544,7 +5544,7 @@ const RUSSIAN: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "Основная графика"), ("window.panel", "Info", "Информация"), ("help.inAppTutorials", "Learn", "Обучение"), - ("window.panel", "Lumetri Scopes", "Измерители Lumetri"), + ("window.panel", "Video Scopes", "Видеоскопы"), ("window.panel", "Mask Interpolation", "Интерполяция маски"), ("window.panel", "Media Browser", "Обозреватель медиа"), ("window.panel", "Metadata", "Метаданные"), @@ -5643,8 +5643,8 @@ const FRENCH: &[(&str, &str, &str)] = &[ ("file.import", "File...", "Fichier..."), ("file.importMultiple", "Multiple Files...", "Plusieurs fichiers..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Projet Adobe Premiere Pro..."), - ("file.importAep", "After Effects Project...", "Projet After Effects..."), - ("file.exportAep", "After Effects Project...", "Projet After Effects..."), + ("file.importAep", "Adobe After Effects Project...", "Projet Adobe After Effects..."), + ("file.exportAep", "Adobe After Effects Project...", "Projet Adobe After Effects..."), ("file.importProject", "EffectCraft Project...", "Projet EffectCraft..."), ("file.importPlaceholder", "Placeholder...", "Espace réservé..."), ("file.importSolid", "Solid...", "Solide..."), @@ -5837,7 +5837,7 @@ const FRENCH: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Masquer les masques verrouillés"), ("", "Mask and Shape Path", "Trajectoire de masque et de forme"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Fermé"), + ("path.toggleClosed", "Closed", "Fermé"), ("path.convertToBezier", "Convert To Bezier Path", "Convertir en trajectoire Bézier"), ("path.setFirstVertex", "Set First Vertex", "Définir le premier sommet"), ("path.freeTransform", "Free Transform Points", "Transformation libre des points"), @@ -6185,7 +6185,7 @@ const FRENCH: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "Graphiques essentiels"), ("window.panel", "Info", "Informations"), ("help.inAppTutorials", "Learn", "Apprentissage"), - ("window.panel", "Lumetri Scopes", "Oscilloscopes Lumetri"), + ("window.panel", "Video Scopes", "Oscilloscopes vidéo"), ("window.panel", "Mask Interpolation", "Interpolation de masque"), ("window.panel", "Media Browser", "Navigateur multimédia"), ("window.panel", "Metadata", "Métadonnées"), @@ -6284,8 +6284,8 @@ const GERMAN: &[(&str, &str, &str)] = &[ ("file.import", "File...", "Datei..."), ("file.importMultiple", "Multiple Files...", "Mehrere Dateien..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Pro-Projekt..."), - ("file.importAep", "After Effects Project...", "After Effects-Projekt..."), - ("file.exportAep", "After Effects Project...", "After Effects-Projekt..."), + ("file.importAep", "Adobe After Effects Project...", "Adobe After Effects-Projekt..."), + ("file.exportAep", "Adobe After Effects Project...", "Adobe After Effects-Projekt..."), ("file.importProject", "EffectCraft Project...", "EffectCraft-Projekt..."), ("file.importPlaceholder", "Placeholder...", "Platzhalter..."), ("file.importSolid", "Solid...", "Farbfläche..."), @@ -6478,7 +6478,7 @@ const GERMAN: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Gesperrte Masken ausblenden"), ("", "Mask and Shape Path", "Masken- und Formpfad"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Geschlossen"), + ("path.toggleClosed", "Closed", "Geschlossen"), ("path.convertToBezier", "Convert To Bezier Path", "In Bézier-Pfad konvertieren"), ("path.setFirstVertex", "Set First Vertex", "Ersten Scheitelpunkt festlegen"), ("path.freeTransform", "Free Transform Points", "Punkte frei transformieren"), @@ -6826,7 +6826,7 @@ const GERMAN: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "Essential-Grafik"), ("window.panel", "Info", "Info"), ("help.inAppTutorials", "Learn", "Lernen"), - ("window.panel", "Lumetri Scopes", "Lumetri-Scopes"), + ("window.panel", "Video Scopes", "Videoskope"), ("window.panel", "Mask Interpolation", "Maskeninterpolation"), ("window.panel", "Media Browser", "Medienbrowser"), ("window.panel", "Metadata", "Metadaten"), @@ -6925,8 +6925,8 @@ const POLISH: &[(&str, &str, &str)] = &[ ("file.import", "File...", "Plik..."), ("file.importMultiple", "Multiple Files...", "Wiele plików..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Projekt Adobe Premiere Pro..."), - ("file.importAep", "After Effects Project...", "Projekt After Effects..."), - ("file.exportAep", "After Effects Project...", "Projekt After Effects..."), + ("file.importAep", "Adobe After Effects Project...", "Projekt Adobe After Effects..."), + ("file.exportAep", "Adobe After Effects Project...", "Projekt Adobe After Effects..."), ("file.importProject", "EffectCraft Project...", "Projekt EffectCraft..."), ("file.importPlaceholder", "Placeholder...", "Element zastępczy..."), ("file.importSolid", "Solid...", "Plansza..."), @@ -7118,7 +7118,7 @@ const POLISH: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Ukryj zablokowane maski"), ("", "Mask and Shape Path", "Ścieżka maski i kształtu"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Zamknięta"), + ("path.toggleClosed", "Closed", "Zamknięta"), ("path.convertToBezier", "Convert To Bezier Path", "Konwertuj na ścieżkę Béziera"), ("path.setFirstVertex", "Set First Vertex", "Ustaw pierwszy wierzchołek"), ("path.freeTransform", "Free Transform Points", "Swobodne przekształcanie punktów"), @@ -7464,7 +7464,7 @@ const POLISH: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "Essential Graphics"), ("window.panel", "Info", "Informacje"), ("help.inAppTutorials", "Learn", "Nauka"), - ("window.panel", "Lumetri Scopes", "Zakresy Lumetri"), + ("window.panel", "Video Scopes", "Wskaźniki wideo"), ("window.panel", "Mask Interpolation", "Interpolacja maski"), ("window.panel", "Media Browser", "Przeglądarka multimediów"), ("window.panel", "Metadata", "Metadane"), @@ -7563,8 +7563,8 @@ const CZECH: &[(&str, &str, &str)] = &[ ("file.import", "File...", "Soubor..."), ("file.importMultiple", "Multiple Files...", "Více souborů..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Projekt Adobe Premiere Pro..."), - ("file.importAep", "After Effects Project...", "Projekt After Effects..."), - ("file.exportAep", "After Effects Project...", "Projekt After Effects..."), + ("file.importAep", "Adobe After Effects Project...", "Projekt Adobe After Effects..."), + ("file.exportAep", "Adobe After Effects Project...", "Projekt Adobe After Effects..."), ("file.importProject", "EffectCraft Project...", "Projekt EffectCraft..."), ("file.importPlaceholder", "Placeholder...", "Zástupný záznam..."), ("file.importSolid", "Solid...", "Plná vrstva..."), @@ -7758,7 +7758,7 @@ const CZECH: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "Skrýt zamknuté masky"), ("", "Mask and Shape Path", "Cesta masky a tvaru"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "Uzavřená"), + ("path.toggleClosed", "Closed", "Uzavřená"), ("path.convertToBezier", "Convert To Bezier Path", "Převést na Bézierovu cestu"), ("path.setFirstVertex", "Set First Vertex", "Nastavit první vrchol"), ("path.freeTransform", "Free Transform Points", "Volně transformovat body"), @@ -8106,7 +8106,7 @@ const CZECH: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "Základní grafika"), ("window.panel", "Info", "Informace"), ("help.inAppTutorials", "Learn", "Výuka"), - ("window.panel", "Lumetri Scopes", "Měřicí přístroje Lumetri"), + ("window.panel", "Video Scopes", "Videoskopy"), ("window.panel", "Mask Interpolation", "Interpolace masek"), ("window.panel", "Media Browser", "Prohlížeč médií"), ("window.panel", "Metadata", "Metadata"), @@ -8201,8 +8201,8 @@ const GEORGIAN: &[(&str, &str, &str)] = &[ ("file.import", "File...", "ფაილი..."), ("file.importMultiple", "Multiple Files...", "რამდენიმე ფაილი..."), ("file.importTimeline", "Adobe Premiere Pro Project...", "Adobe Premiere Pro-ს პროექტი..."), - ("file.importAep", "After Effects Project...", "After Effects-ის პროექტი..."), - ("file.exportAep", "After Effects Project...", "After Effects-ის პროექტი..."), + ("file.importAep", "Adobe After Effects Project...", "Adobe After Effects-ის პროექტი..."), + ("file.exportAep", "Adobe After Effects Project...", "Adobe After Effects-ის პროექტი..."), ("file.importProject", "EffectCraft Project...", "EffectCraft-ის პროექტი..."), ("file.importPlaceholder", "Placeholder...", "ჩანაცვლების ობიექტი..."), ("file.importSolid", "Solid...", "სოლიდი..."), @@ -8395,7 +8395,7 @@ const GEORGIAN: &[(&str, &str, &str)] = &[ ("layer.mask.hideLocked", "Hide Locked Masks", "ჩაკეტილი ნიღბების დამალვა"), ("", "Mask and Shape Path", "ნიღაბი და ფიგურის ტრაექტორია"), ("path.rotoBezier", "RotoBezier", "RotoBezier"), - ("mask.setClosed", "Closed", "შეკრული"), + ("path.toggleClosed", "Closed", "შეკრული"), ("path.convertToBezier", "Convert To Bezier Path", "ბეზიეს ტრაექტორიად გარდაქმნა"), ("path.setFirstVertex", "Set First Vertex", "პირველი წვეროს დაყენება"), ("path.freeTransform", "Free Transform Points", "წერტილების თავისუფალი გარდაქმნა"), @@ -8748,7 +8748,7 @@ const GEORGIAN: &[(&str, &str, &str)] = &[ ("window.panel", "Essential Graphics", "ძირითადი გრაფიკა"), ("window.panel", "Info", "ინფორმაცია"), ("help.inAppTutorials", "Learn", "სწავლა"), - ("window.panel", "Lumetri Scopes", "Lumetri Scopes"), + ("window.panel", "Video Scopes", "ვიდეოსკოპები"), ("window.panel", "Mask Interpolation", "ნიღბის ინტერპოლაცია"), ("window.panel", "Media Browser", "მედიის ბრაუზერი"), ("window.panel", "Metadata", "მეტამონაცემები"), @@ -8799,6 +8799,21 @@ mod tests { use serde_json::json; use std::collections::BTreeSet; + #[test] + fn renamed_menu_translations_do_not_reintroduce_vendor_names() { + for code in CATALOG_CODES { + set_current(code); + for (id, source) in [("window.panel", "Video Scopes")] { + let rendered = command_label(id, source); + let label = rendered.to_lowercase(); + for mark in ["lumetri", "mercury"] { + assert!(!label.contains(mark), "{code}: {id} -> {rendered}"); + } + } + } + set_current("en"); + } + /// The four options at the foot of the header's workspace menu say what After Effects says, /// read from its workspace button on 2026-10-31: reset, save over this one, save a copy, then /// edit the list. @@ -9193,7 +9208,8 @@ mod tests { assert!(!japanese(&app)); } - /// File ▸ Import ▸ After Effects Project… has its row in every menu catalog. + /// File ▸ Import / Export ▸ Adobe After Effects Project… has its row in every menu catalog, + /// under the label the menu draws (a row under an older label would fall back to English). #[test] fn the_aep_import_and_export_entries_are_in_every_menu_catalog() { for (code, catalog) in [ @@ -9212,7 +9228,8 @@ mod tests { ("sv", SWEDISH), ] { for id in ["file.importAep", "file.exportAep"] { - assert!(catalog.iter().any(|(command, _, _)| *command == id), "{code} has no {id} row"); + let drawn = effectcraft_engine::menus::entries().into_iter().find(|(_, e)| e.command == id).map(|(_, e)| e.label.clone()).unwrap_or_default(); + assert!(catalog.iter().any(|(command, english, _)| *command == id && *english == drawn), "{code} has no {id} row for {drawn:?}"); } } } @@ -9291,12 +9308,16 @@ mod tests { alt_shortcuts: Vec::new(), }; let new_comp = menu_entry("comp.new", "New Composition..."); + let closed_path = menu_entry("path.toggleClosed", "Closed"); let undo = menu_entry("edit.undo", "Undo"); for code in CATALOG_CODES { app.session.execute("prefs.set", json!({"key":"general.language", "value":code})).unwrap(); let drawn = entry(&app, &new_comp, new_comp.label.clone()); assert_eq!(drawn, label(&app, "comp.new", "New Composition..."), "{code}"); assert_ne!(drawn, new_comp.label, "{code}: the menu entry is drawn in English"); + let closed_label = entry(&app, &closed_path, closed_path.label.clone()); + assert_eq!(closed_label, label(&app, "path.toggleClosed", "Closed"), "{code}"); + assert_ne!(closed_label, closed_path.label, "{code}: Closed is drawn in English"); } app.session.execute("prefs.set", json!({"key":"general.language", "value":"ru"})).unwrap(); assert_eq!(entry(&app, &undo, "Can't Undo".into()), "Нечего отменять"); diff --git a/crates/ui-egui/src/i18n/effects.rs b/crates/ui-egui/src/i18n/effects.rs index ec952ccb0..55893115a 100644 --- a/crates/ui-egui/src/i18n/effects.rs +++ b/crates/ui-egui/src/i18n/effects.rs @@ -18,14 +18,19 @@ //! another effect. An effect with no row draws the registry's English name; the //! `missing_translations_report` test (run by `cargo xtask ci`) lists those per language. //! -//! A language decides for itself which names stay English: After Effects' Japanese interface -//! leaves the `CC` series in English and gives the `VR` and `OCIO` sets a Japanese tail -//! (`VR ブラー`, `OCIO CDL 変換`), while its Simplified Chinese one translates them (the `CC` -//! names come from Adobe's Chinese *documentation*, which names 68 of the 69). Repeating the -//! English name is therefore a language-level choice, not a missing row. +//! Own feature names are descriptive. The renamed effects use original translations in every +//! existing effect catalog; legacy vendor names remain search aliases in the effects crate. +//! External format tokens and stored instance names are not localized or rewritten. //! -//! The Simplified Chinese names come from the installed After Effects 2026 (26.5.0.89) -//! Simplified Chinese interface, its 21 category folder names included; `Cryptomatte`, +//! The rows of the effects this cleanup did not rename keep the provenance they had. A language +//! decides for itself which names stay English: After Effects' Japanese interface leaves the `CC` +//! series in English and gives the `VR` and `OCIO` sets a Japanese tail (`VR ブラー`, +//! `OCIO CDL 変換`), while its Simplified Chinese one translates them. Repeating the English name +//! is therefore a language-level choice, not a missing row. +//! +//! The Simplified Chinese names of unchanged effects come from the installed After Effects 2026 +//! (26.5.0.89) Simplified Chinese interface, its 21 category folder names included (the `CC` names +//! came from Adobe's Chinese *documentation*, which names 68 of the 69); `Cryptomatte`, //! `EXtractoR`, `IDentifier`, `Key Light` and `Unmult` stay in English as brand names, and //! `Face Measurements` / `Face Track Points` are EffectCraft's own effects, so their wording is //! ours. @@ -33,16 +38,15 @@ //! The Traditional Chinese names for Taiwan are written from the meaning of the English ones, with //! the terms Taiwan's creative software uses (`圖層`, `遮罩`, `筆刷`, `雜訊`, `發光`, `漸層`); they //! are not converted from the Simplified table. Brand and coined names (`Cryptomatte`, -//! `EXtractoR`, `IDentifier`, `Key Light`, `Unmult`, `Lumetri Color`, `Colorama`, `Minimax`, -//! `Vegas`, `Mocha`) stay in English; the `CC` series keeps its prefix and translates the -//! descriptive tail, and the `VR` and `OCIO` sets keep their prefix in Chinese word order. +//! `EXtractoR`, `IDentifier`, `Key Light`, `Unmult`, `Colorama`, `Minimax`, `Vegas`) stay in +//! English, and the `VR` and `OCIO` sets keep their prefix in Chinese word order. //! //! The French names are written from the meaning of the English ones, with the vocabulary of the //! French menus; they come from no other application's interface. Brand and coined names -//! (`Cryptomatte`, `EXtractoR`, `IDentifier`, `Key Light`, `Unmult`, `Lumetri Color`, `Colorama`, -//! `Minimax`, `Tritone`, `Vibrance`, `Vegas`) and the whole `CC` series stay in English; a series -//! tag that prefixes a descriptive name (`VR`, `OCIO`, `PS`, `HDR`) stays and follows the -//! French word order (`Flou VR`, `Transformation CDL OCIO`). +//! (`Cryptomatte`, `EXtractoR`, `IDentifier`, `Key Light`, `Unmult`, `Colorama`, `Minimax`, +//! `Tritone`, `Vibrance`, `Vegas`) stay in English; a series tag that prefixes a descriptive name +//! (`VR`, `OCIO`, `PS`, `HDR`) stays and follows the French word order (`Flou VR`, +//! `Transformation CDL OCIO`). use std::collections::HashMap; use std::sync::OnceLock; @@ -122,19 +126,21 @@ pub(super) fn translated_rows(language: &str) -> usize { /// The name to draw for an effect group the property tree stores as `stored`: the catalog name, /// plus the instance index the engine appends to a repeat (`Gaussian Blur 2` → `高斯模糊 2`). /// -/// A name that is neither the registry's nor the registry's plus an index was typed by the user -/// (Effect Controls ▸ Rename), so it stays verbatim — as does the whole string in a language -/// with no row. +/// Current and legacy default names (including numeric instance suffixes) draw the current +/// name. Other names were supplied by the user and stay verbatim. Stored data is never changed. pub(crate) fn group(id: &str, stored: &str) -> String { - let Some((english, translated)) = catalog(current()).and_then(|c| c.row(id)) else { return stored.to_string() }; - if stored == english { - return translated.to_string(); - } - if let Some(index) = stored.strip_prefix(english).and_then(|rest| rest.strip_prefix(' ')) - && !index.is_empty() - && index.bytes().all(|b| b.is_ascii_digit()) - { - return format!("{translated} {index}"); + let Some(spec) = effectcraft_engine::effects::find(id) else { return stored.to_string() }; + let translated = name(id, spec.name); + for source in std::iter::once(spec.name).chain(effectcraft_engine::effects::aliases(id).iter().copied()) { + if stored == source { + return translated; + } + if let Some(index) = stored.strip_prefix(source).and_then(|rest| rest.strip_prefix(' ')) + && !index.is_empty() + && index.bytes().all(|b| b.is_ascii_digit()) + { + return format!("{translated} {index}"); + } } stored.to_string() } @@ -149,6 +155,7 @@ pub(crate) fn category(source: &str) -> String { /// `query` (lowercase)? Both names match, so a translated interface can be searched either way. pub(crate) fn matches(id: &str, english: &str, query: &str) -> bool { english.to_lowercase().contains(query) + || effectcraft_engine::effects::find(id).is_some_and(|spec| effectcraft_engine::effects::name_matches(spec, query)) || catalog(current()).and_then(|c| c.row(id)).is_some_and(|(_, translated)| translated.to_lowercase().contains(query)) } @@ -192,10 +199,10 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.blur.bilateral", "Bilateral Blur", "双向模糊"), ("ec.blur.cameralens", "Camera Lens Blur", "摄像机镜头模糊"), ("ec.blur.camerashakedeblur", "Camera-Shake Deblur", "摄像机抖动去模糊"), - ("ec.blur.cccross", "CC Cross Blur", "CC 交叉模糊"), - ("ec.blur.ccradial", "CC Radial Blur", "CC 径向模糊"), - ("ec.blur.ccradialfast", "CC Radial Fast Blur", "CC 径向快速模糊"), - ("ec.blur.ccvector", "CC Vector Blur", "CC 矢量模糊"), + ("ec.blur.cccross", "Cross Blur", "十字模糊"), + ("ec.blur.ccradial", "Variable Radial Blur", "可变径向模糊"), + ("ec.blur.ccradialfast", "Fast Radial Blur", "快速径向模糊"), + ("ec.blur.ccvector", "Vector Blur", "矢量模糊"), ("ec.blur.channel", "Channel Blur", "通道模糊"), ("ec.blur.compound", "Compound Blur", "复合模糊"), ("ec.blur.directional", "Directional Blur", "定向模糊"), @@ -209,7 +216,7 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.channel.arithmetic", "Arithmetic", "算术"), ("ec.channel.blend", "Blend", "混合"), ("ec.channel.calculations", "Calculations", "计算"), - ("ec.channel.cccomposite", "CC Composite", "CC 合成"), + ("ec.channel.cccomposite", "Source Composite", "源图合成"), ("ec.channel.combiner", "Channel Combiner", "通道合成器"), ("ec.channel.compoundarithmetic", "Compound Arithmetic", "复合运算"), ("ec.channel.invert", "Invert", "反转"), @@ -225,10 +232,10 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.color.blackwhite", "Black & White", "黑色和白色"), ("ec.color.brightnesscontrast", "Brightness & Contrast", "亮度和对比度"), ("ec.color.broadcast", "Broadcast Colors", "广播颜色"), - ("ec.color.cccolorneutralizer", "CC Color Neutralizer", "CC 颜色中和器"), - ("ec.color.cccoloroffset", "CC Color Offset", "CC 颜色偏移"), - ("ec.color.cckernel", "CC Kernel", "CC 内核"), - ("ec.color.cctoner", "CC Toner", "CC 调色器"), + ("ec.color.cccolorneutralizer", "Color Neutralizer", "色偏校正"), + ("ec.color.cccoloroffset", "Channel Offset", "通道偏移"), + ("ec.color.cckernel", "Convolution Kernel", "卷积核"), + ("ec.color.cctoner", "Multi-Tone Tint", "多色调着色"), ("ec.color.changecolor", "Change Color", "更改颜色"), ("ec.color.changetocolor", "Change to Color", "更改为颜色"), ("ec.color.channelmixer", "Channel Mixer", "通道混合器"), @@ -245,7 +252,7 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.color.leavecolor", "Leave Color", "保留颜色"), ("ec.color.levels", "Levels", "色阶"), ("ec.color.levelsic", "Levels (Individual Controls)", "色阶 (单独控件)"), - ("ec.color.lumetri", "Lumetri Color", "Lumetri 颜色"), + ("ec.color.lumetri", "Color Grading", "色彩分级"), ("ec.color.ociocdl", "OCIO CDL Transform", "OCIO CDL 变换"), ("ec.color.ociocolorspace", "OCIO Color Space Transform", "OCIO 颜色空间变换"), ("ec.color.ociodisplay", "OCIO Display Transform", "OCIO 显示变换"), @@ -270,20 +277,20 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.control.stereo3d", "Stereo 3D Controls", "立体 3D 控制"), ("ec.distort.bezierwarp", "Bezier Warp", "贝塞尔曲线变形"), ("ec.distort.bulge", "Bulge", "凸出"), - ("ec.distort.ccbender", "CC Bender", "CC 弯曲器"), - ("ec.distort.ccbendit", "CC Bend It", "CC 弯曲"), - ("ec.distort.ccblobbylize", "CC Blobbylize", "CC 融化"), - ("ec.distort.ccflomotion", "CC Flo Motion", "CC 流动运动"), - ("ec.distort.ccgriddler", "CC Griddler", "CC 网格生成器"), - ("ec.distort.cclens", "CC Lens", "CC 镜头"), - ("ec.distort.ccpageturn", "CC Page Turn", "CC 翻页"), - ("ec.distort.ccpowerpin", "CC Power Pin", "CC 强力边角定位"), - ("ec.distort.ccripplepulse", "CC Ripple Pulse", "CC 波纹脉冲"), - ("ec.distort.ccslant", "CC Slant", "CC 倾斜"), - ("ec.distort.ccsmear", "CC Smear", "CC 漩涡条纹"), - ("ec.distort.ccsplit", "CC Split", "CC 拆分"), - ("ec.distort.ccsplit2", "CC Split 2", "CC 拆分 2"), - ("ec.distort.cctiler", "CC Tiler", "CC 拼贴器"), + ("ec.distort.ccbender", "Flexible Bend", "柔性弯曲"), + ("ec.distort.ccbendit", "Two-Point Bend", "两点弯曲"), + ("ec.distort.ccblobbylize", "Organic Displacement", "有机置换"), + ("ec.distort.ccflomotion", "Dual Lens Warp", "双镜头变形"), + ("ec.distort.ccgriddler", "Tile Grid", "平铺网格"), + ("ec.distort.cclens", "Lens Distortion", "镜头畸变"), + ("ec.distort.ccpageturn", "Page Curl", "页面卷曲"), + ("ec.distort.ccpowerpin", "Perspective Pin", "透视定位"), + ("ec.distort.ccripplepulse", "Pulsed Ripple", "脉冲涟漪"), + ("ec.distort.ccslant", "Slant", "倾斜"), + ("ec.distort.ccsmear", "Directional Smear", "定向涂抹"), + ("ec.distort.ccsplit", "Split Warp", "分割变形"), + ("ec.distort.ccsplit2", "Dual Split Warp", "双重分割变形"), + ("ec.distort.cctiler", "Scaled Tiles", "缩放平铺"), ("ec.distort.cornerpin", "Corner Pin", "边角定位"), ("ec.distort.displacementmap", "Displacement Map", "置换图"), ("ec.distort.liquify", "Liquify", "液化"), @@ -311,11 +318,11 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.generate.audiospectrum", "Audio Spectrum", "音频频谱"), ("ec.generate.audiowaveform", "Audio Waveform", "音频波形"), ("ec.generate.beam", "Beam", "光束"), - ("ec.generate.ccgluegun", "CC Glue Gun", "CC 胶水枪"), - ("ec.generate.cclightburst", "CC Light Burst 2.5", "CC 光束放射 2.5"), - ("ec.generate.cclightrays", "CC Light Rays", "CC 光线"), - ("ec.generate.cclightsweep", "CC Light Sweep", "CC 光线扫描"), - ("ec.generate.ccthreads", "CC Threads", "CC 编织条"), + ("ec.generate.ccgluegun", "Paint Beads", "颜料珠"), + ("ec.generate.cclightburst", "Radial Light Burst", "径向光芒"), + ("ec.generate.cclightrays", "Light Rays", "光线"), + ("ec.generate.cclightsweep", "Light Sweep", "扫光"), + ("ec.generate.ccthreads", "Woven Threads", "编织线"), ("ec.generate.cellpattern", "Cell Pattern", "单元格图案"), ("ec.generate.checkerboard", "Checkerboard", "棋盘"), ("ec.generate.circle", "Circle", "圆形"), @@ -334,7 +341,7 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.generate.vegas", "Vegas", "勾画"), ("ec.generate.writeon", "Write-on", "写入"), ("ec.key.advancedspill", "Advanced Spill Suppressor", "高级溢出抑制器"), - ("ec.key.ccsimplewireremoval", "CC Simple Wire Removal", "CC 简单丝线移除"), + ("ec.key.ccsimplewireremoval", "Line Removal", "线条移除"), ("ec.key.colordifference", "Color Difference Key", "颜色差值键"), ("ec.key.colorkey", "Color Key", "颜色键"), ("ec.key.colorrange", "Color Range", "颜色范围"), @@ -347,7 +354,7 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.key.screen", "Screen Key", "屏幕键"), ("ec.key.spill", "Spill Suppressor", "溢出抑制"), ("ec.key.unmult", "Unmult", "Unmult"), - ("ec.keying.keylight", "Key Light", "Key Light"), + ("ec.keying.keylight", "Screen Color Key", "屏幕色键"), ("ec.matte.mattechoker", "Matte Choker", "遮罩阻塞工具"), ("ec.matte.objectmatte", "Object Matte", "对象遮罩"), ("ec.matte.refinehard", "Refine Hard Matte", "调整实边遮罩"), @@ -371,50 +378,50 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.obsolete.basictext", "Basic Text", "基本文字"), ("ec.obsolete.gaussianlegacy", "Gaussian Blur (Legacy)", "高斯模糊 (旧版)"), ("ec.obsolete.lightning", "Lightning", "闪电"), - ("ec.obsolete.mochashape", "Mocha shape", "Mocha 形状"), + ("ec.obsolete.mochashape", "Tracked Shape Matte", "跟踪形状遮罩"), ("ec.obsolete.pathtext", "Path Text", "路径文本"), ("ec.paint.paint", "Paint", "绘画"), ("ec.perspective.3dglasses", "3D Glasses", "3D 眼镜"), ("ec.perspective.bevelalpha", "Bevel Alpha", "斜面 Alpha"), ("ec.perspective.beveledges", "Bevel Edges", "边缘斜面"), ("ec.perspective.cameratracker", "3D Camera Tracker", "3D 摄像机跟踪器"), - ("ec.perspective.cccylinder", "CC Cylinder", "CC 圆柱体"), - ("ec.perspective.ccenvironment", "CC Environment", "CC 环境"), - ("ec.perspective.ccsphere", "CC Sphere", "CC 球体"), - ("ec.perspective.ccspotlight", "CC Spotlight", "CC 聚光"), + ("ec.perspective.cccylinder", "Cylinder Wrap", "圆柱包裹"), + ("ec.perspective.ccenvironment", "Environment Map", "环境映射"), + ("ec.perspective.ccsphere", "Sphere Wrap", "球面包裹"), + ("ec.perspective.ccspotlight", "Surface Spotlight", "表面聚光灯"), ("ec.perspective.dropshadow", "Drop Shadow", "投影"), ("ec.perspective.radialshadow", "Radial Shadow", "径向阴影"), ("ec.sim.carddance", "Card Dance", "卡片动画"), ("ec.sim.caustics", "Caustics", "焦散"), - ("ec.sim.ccballaction", "CC Ball Action", "CC 球动作"), - ("ec.sim.ccbubbles", "CC Bubbles", "CC 气泡"), - ("ec.sim.ccdrizzle", "CC Drizzle", "CC 细雨波纹"), - ("ec.sim.cchair", "CC Hair", "CC 毛发"), - ("ec.sim.ccmrmercury", "CC Mr. Mercury", "CC 水银流体"), - ("ec.sim.ccparticlesystems2", "CC Particle Systems II", "CC 粒子系统 II"), - ("ec.sim.ccparticleworld", "CC Particle World", "CC 粒子世界"), - ("ec.sim.ccpixelpolly", "CC Pixel Polly", "CC 像素多边形"), - ("ec.sim.ccrainfall", "CC Rainfall", "CC 降雨"), - ("ec.sim.ccscatterize", "CC Scatterize", "CC 散射"), - ("ec.sim.ccsnowfall", "CC Snowfall", "CC 降雪"), - ("ec.sim.ccstarburst", "CC Star Burst", "CC 星爆"), + ("ec.sim.ccballaction", "Ball Grid", "球体网格"), + ("ec.sim.ccbubbles", "Bubbles", "气泡"), + ("ec.sim.ccdrizzle", "Water Ripples", "水面涟漪"), + ("ec.sim.cchair", "Surface Fibers", "表面纤维"), + ("ec.sim.ccmrmercury", "Liquid Particles", "液体粒子"), + ("ec.sim.ccparticlesystems2", "Particle Emitter 2D", "二维粒子发射器"), + ("ec.sim.ccparticleworld", "Particle Emitter 3D", "三维粒子发射器"), + ("ec.sim.ccpixelpolly", "Pixel Shatter", "像素破碎"), + ("ec.sim.ccrainfall", "Rainfall", "降雨"), + ("ec.sim.ccscatterize", "Pixel Scatter", "像素散射"), + ("ec.sim.ccsnowfall", "Snowfall", "降雪"), + ("ec.sim.ccstarburst", "Starfield", "星空"), ("ec.sim.foam", "Foam", "泡沫"), ("ec.sim.particleplayground", "Particle Playground", "粒子运动场"), ("ec.sim.shatter", "Shatter", "碎片"), ("ec.sim.waveworld", "Wave World", "波形环境"), ("ec.stylize.brushstrokes", "Brush Strokes", "画笔描边"), ("ec.stylize.cartoon", "Cartoon", "卡通"), - ("ec.stylize.ccblockload", "CC Block Load", "CC 块加载"), - ("ec.stylize.ccburnfilm", "CC Burn Film", "CC 烧毁胶片"), - ("ec.stylize.ccglass", "CC Glass", "CC 玻璃"), - ("ec.stylize.cchextile", "CC HexTile", "CC 六边形拼贴"), - ("ec.stylize.cckaleida", "CC Kaleida", "CC 万花筒"), - ("ec.stylize.ccmrsmoothie", "CC Mr. Smoothie", "CC 像素溶解"), - ("ec.stylize.ccplastic", "CC Plastic", "CC 塑料"), - ("ec.stylize.ccrepetile", "CC RepeTile", "CC 重复拼贴"), - ("ec.stylize.ccthreshold", "CC Threshold", "CC 阈值"), - ("ec.stylize.ccthresholdrgb", "CC Threshold RGB", "CC 阈值 RGB"), - ("ec.stylize.ccvignette", "CC Vignette", "CC 暗角"), + ("ec.stylize.ccblockload", "Block Reveal", "块状显现"), + ("ec.stylize.ccburnfilm", "Film Burn", "胶片灼烧"), + ("ec.stylize.ccglass", "Glass Relief", "玻璃浮雕"), + ("ec.stylize.cchextile", "Hexagonal Tiles", "六边形平铺"), + ("ec.stylize.cckaleida", "Kaleidoscope", "万花筒"), + ("ec.stylize.ccmrsmoothie", "Palette Map", "调色板映射"), + ("ec.stylize.ccplastic", "Plastic Shading", "塑料着色"), + ("ec.stylize.ccrepetile", "Edge Tile Repeat", "边缘平铺重复"), + ("ec.stylize.ccthreshold", "Channel Threshold", "通道阈值"), + ("ec.stylize.ccthresholdrgb", "RGB Threshold", "RGB 阈值"), + ("ec.stylize.ccvignette", "Vignette", "晕影"), ("ec.stylize.coloremboss", "Color Emboss", "彩色浮雕"), ("ec.stylize.emboss", "Emboss", "浮雕"), ("ec.stylize.findedges", "Find Edges", "查找边缘"), @@ -429,8 +436,8 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.stylize.threshold", "Threshold", "阈值"), ("ec.text.numbers", "Numbers", "编号"), ("ec.text.timecode", "Timecode", "时间码"), - ("ec.time.ccforcemotionblur", "CC Force Motion Blur", "CC 强制运动模糊"), - ("ec.time.ccwidetime", "CC Wide Time", "CC 宽泛时间"), + ("ec.time.ccforcemotionblur", "Temporal Motion Blur", "时间运动模糊"), + ("ec.time.ccwidetime", "Temporal Blend", "时间混合"), ("ec.time.echo", "Echo", "残影"), ("ec.time.pixelmotionblur", "Pixel Motion Blur", "像素运动模糊"), ("ec.time.posterizetime", "Posterize Time", "抽帧"), @@ -439,23 +446,23 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ ("ec.time.timewarp", "Timewarp", "时间扭曲"), ("ec.transition.blockdissolve", "Block Dissolve", "块溶解"), ("ec.transition.cardwipe", "Card Wipe", "卡片擦除"), - ("ec.transition.ccglasswipe", "CC Glass Wipe", "CC 玻璃擦除"), - ("ec.transition.ccgridwipe", "CC Grid Wipe", "CC 网格擦除"), - ("ec.transition.ccimagewipe", "CC Image Wipe", "CC 图像擦除"), - ("ec.transition.ccjaws", "CC Jaws", "CC 锯齿"), - ("ec.transition.cclightwipe", "CC Light Wipe", "CC 光线擦除"), - ("ec.transition.cclinesweep", "CC Line Sweep", "CC 线条扫掠"), - ("ec.transition.ccradialscalewipe", "CC Radial ScaleWipe", "CC 径向缩放擦除"), - ("ec.transition.ccscalewipe", "CC Scale Wipe", "CC 缩放擦除"), - ("ec.transition.cctwister", "CC Twister", "CC 扭曲器"), - ("ec.transition.ccwarpomatic", "CC WarpoMatic", "CC 自动扭曲"), + ("ec.transition.ccglasswipe", "Glass Wipe", "玻璃擦除"), + ("ec.transition.ccgridwipe", "Grid Wipe", "网格擦除"), + ("ec.transition.ccimagewipe", "Luminance Image Wipe", "亮度图像擦除"), + ("ec.transition.ccjaws", "Serrated Wipe", "锯齿擦除"), + ("ec.transition.cclightwipe", "Light Wipe", "光线擦除"), + ("ec.transition.cclinesweep", "Line Sweep", "线条扫过"), + ("ec.transition.ccradialscalewipe", "Radial Scale Wipe", "径向缩放擦除"), + ("ec.transition.ccscalewipe", "Scale Wipe", "缩放擦除"), + ("ec.transition.cctwister", "Twist Wipe", "扭转擦除"), + ("ec.transition.ccwarpomatic", "Warp Dissolve", "变形溶解"), ("ec.transition.gradientwipe", "Gradient Wipe", "渐变擦除"), ("ec.transition.iriswipe", "Iris Wipe", "光圈擦除"), ("ec.transition.linearwipe", "Linear Wipe", "线性擦除"), ("ec.transition.radialwipe", "Radial Wipe", "径向擦除"), ("ec.transition.venetian", "Venetian Blinds", "百叶窗"), ("ec.utility.applylut", "Apply Color LUT", "应用颜色 LUT"), - ("ec.utility.ccoverbrights", "CC Overbrights", "CC 高光溢出"), + ("ec.utility.ccoverbrights", "Out-of-Range Display", "超出范围显示"), ("ec.utility.cineon", "Cineon Converter", "Cineon 转换器"), ("ec.utility.colorprofileconverter", "Color Profile Converter", "颜色配置文件转换器"), ("ec.utility.facemeasurements", "Face Measurements", "面部测量"), @@ -480,9 +487,9 @@ const SIMPLIFIED_CHINESE: &[Row] = &[ /// [`SIMPLIFIED_CHINESE`]. Written from the meaning of the English names with the vocabulary /// Taiwan's creative software uses (圖層, 遮罩, 筆刷, 雜訊, 發光, 漸層), not converted from the /// Simplified table. Brand and coined names (`Cryptomatte`, `EXtractoR`, `IDentifier`, `Key Light`, -/// `Unmult`, `Lumetri Color`, `Colorama`, `Minimax`, `Vegas`, `Mocha`) stay in English; the `CC` -/// series keeps its prefix and translates the descriptive tail, and the `VR` and `OCIO` sets keep -/// their prefix in Chinese word order. +/// `Unmult`, `Colorama`, `Minimax`, `Vegas`) stay in English; the `VR` and `OCIO` sets keep their +/// prefix in Chinese word order. The effects this cleanup renamed are translated from their new +/// descriptive names. const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.3d.channelextract", "3D Channel Extract", "3D 通道擷取"), ("ec.3d.cryptomatte", "Cryptomatte", "Cryptomatte"), @@ -509,10 +516,10 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.blur.bilateral", "Bilateral Blur", "雙邊模糊"), ("ec.blur.cameralens", "Camera Lens Blur", "相機鏡頭模糊"), ("ec.blur.camerashakedeblur", "Camera-Shake Deblur", "相機晃動去模糊"), - ("ec.blur.cccross", "CC Cross Blur", "CC 十字模糊"), - ("ec.blur.ccradial", "CC Radial Blur", "CC 徑向模糊"), - ("ec.blur.ccradialfast", "CC Radial Fast Blur", "CC 徑向快速模糊"), - ("ec.blur.ccvector", "CC Vector Blur", "CC 向量模糊"), + ("ec.blur.cccross", "Cross Blur", "十字模糊"), + ("ec.blur.ccradial", "Variable Radial Blur", "可變放射狀模糊"), + ("ec.blur.ccradialfast", "Fast Radial Blur", "快速放射狀模糊"), + ("ec.blur.ccvector", "Vector Blur", "向量模糊"), ("ec.blur.channel", "Channel Blur", "通道模糊"), ("ec.blur.compound", "Compound Blur", "複合模糊"), ("ec.blur.directional", "Directional Blur", "方向模糊"), @@ -526,7 +533,7 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.channel.arithmetic", "Arithmetic", "算術"), ("ec.channel.blend", "Blend", "混合"), ("ec.channel.calculations", "Calculations", "運算"), - ("ec.channel.cccomposite", "CC Composite", "CC 合成"), + ("ec.channel.cccomposite", "Source Composite", "來源影像合成"), ("ec.channel.combiner", "Channel Combiner", "通道合成器"), ("ec.channel.compoundarithmetic", "Compound Arithmetic", "複合算術"), ("ec.channel.invert", "Invert", "反轉"), @@ -542,10 +549,10 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.color.blackwhite", "Black & White", "黑白"), ("ec.color.brightnesscontrast", "Brightness & Contrast", "亮度與對比"), ("ec.color.broadcast", "Broadcast Colors", "廣播色彩"), - ("ec.color.cccolorneutralizer", "CC Color Neutralizer", "CC 色彩中和器"), - ("ec.color.cccoloroffset", "CC Color Offset", "CC 色彩偏移"), - ("ec.color.cckernel", "CC Kernel", "CC 核心"), - ("ec.color.cctoner", "CC Toner", "CC 色調"), + ("ec.color.cccolorneutralizer", "Color Neutralizer", "色偏校正"), + ("ec.color.cccoloroffset", "Channel Offset", "色版偏移"), + ("ec.color.cckernel", "Convolution Kernel", "卷積核心"), + ("ec.color.cctoner", "Multi-Tone Tint", "多色調著色"), ("ec.color.changecolor", "Change Color", "變更色彩"), ("ec.color.changetocolor", "Change to Color", "變更為色彩"), ("ec.color.channelmixer", "Channel Mixer", "通道混合器"), @@ -562,7 +569,7 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.color.leavecolor", "Leave Color", "保留色彩"), ("ec.color.levels", "Levels", "色階"), ("ec.color.levelsic", "Levels (Individual Controls)", "色階(個別控制項)"), - ("ec.color.lumetri", "Lumetri Color", "Lumetri Color"), + ("ec.color.lumetri", "Color Grading", "色彩分級"), ("ec.color.ociocdl", "OCIO CDL Transform", "OCIO CDL 轉換"), ("ec.color.ociocolorspace", "OCIO Color Space Transform", "OCIO 色彩空間轉換"), ("ec.color.ociodisplay", "OCIO Display Transform", "OCIO 顯示轉換"), @@ -587,20 +594,20 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.control.stereo3d", "Stereo 3D Controls", "立體 3D 控制"), ("ec.distort.bezierwarp", "Bezier Warp", "貝茲彎曲"), ("ec.distort.bulge", "Bulge", "凸出"), - ("ec.distort.ccbender", "CC Bender", "CC 彎曲"), - ("ec.distort.ccbendit", "CC Bend It", "CC 彎折"), - ("ec.distort.ccblobbylize", "CC Blobbylize", "CC 團塊化"), - ("ec.distort.ccflomotion", "CC Flo Motion", "CC 流動"), - ("ec.distort.ccgriddler", "CC Griddler", "CC 格線"), - ("ec.distort.cclens", "CC Lens", "CC 鏡頭"), - ("ec.distort.ccpageturn", "CC Page Turn", "CC 翻頁"), - ("ec.distort.ccpowerpin", "CC Power Pin", "CC 強力釘選"), - ("ec.distort.ccripplepulse", "CC Ripple Pulse", "CC 漣漪脈衝"), - ("ec.distort.ccslant", "CC Slant", "CC 傾斜"), - ("ec.distort.ccsmear", "CC Smear", "CC 塗抹"), - ("ec.distort.ccsplit", "CC Split", "CC 分割"), - ("ec.distort.ccsplit2", "CC Split 2", "CC 分割 2"), - ("ec.distort.cctiler", "CC Tiler", "CC 並排"), + ("ec.distort.ccbender", "Flexible Bend", "柔性彎曲"), + ("ec.distort.ccbendit", "Two-Point Bend", "兩點彎曲"), + ("ec.distort.ccblobbylize", "Organic Displacement", "有機置換"), + ("ec.distort.ccflomotion", "Dual Lens Warp", "雙鏡頭變形"), + ("ec.distort.ccgriddler", "Tile Grid", "拼貼網格"), + ("ec.distort.cclens", "Lens Distortion", "鏡頭畸變"), + ("ec.distort.ccpageturn", "Page Curl", "頁面捲曲"), + ("ec.distort.ccpowerpin", "Perspective Pin", "透視定位"), + ("ec.distort.ccripplepulse", "Pulsed Ripple", "脈衝漣漪"), + ("ec.distort.ccslant", "Slant", "傾斜"), + ("ec.distort.ccsmear", "Directional Smear", "定向塗抹"), + ("ec.distort.ccsplit", "Split Warp", "分割變形"), + ("ec.distort.ccsplit2", "Dual Split Warp", "雙重分割變形"), + ("ec.distort.cctiler", "Scaled Tiles", "縮放平鋪"), ("ec.distort.cornerpin", "Corner Pin", "邊角釘選"), ("ec.distort.displacementmap", "Displacement Map", "位移映射"), ("ec.distort.liquify", "Liquify", "液化"), @@ -628,11 +635,11 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.generate.audiospectrum", "Audio Spectrum", "音訊頻譜"), ("ec.generate.audiowaveform", "Audio Waveform", "音訊波形"), ("ec.generate.beam", "Beam", "光束"), - ("ec.generate.ccgluegun", "CC Glue Gun", "CC 膠水槍"), - ("ec.generate.cclightburst", "CC Light Burst 2.5", "CC 光爆 2.5"), - ("ec.generate.cclightrays", "CC Light Rays", "CC 光線"), - ("ec.generate.cclightsweep", "CC Light Sweep", "CC 光線掃掠"), - ("ec.generate.ccthreads", "CC Threads", "CC 絲線"), + ("ec.generate.ccgluegun", "Paint Beads", "顏料珠"), + ("ec.generate.cclightburst", "Radial Light Burst", "放射狀光芒"), + ("ec.generate.cclightrays", "Light Rays", "光線"), + ("ec.generate.cclightsweep", "Light Sweep", "掃光"), + ("ec.generate.ccthreads", "Woven Threads", "編織線"), ("ec.generate.cellpattern", "Cell Pattern", "細胞圖樣"), ("ec.generate.checkerboard", "Checkerboard", "棋盤格"), ("ec.generate.circle", "Circle", "圓形"), @@ -651,7 +658,7 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.generate.vegas", "Vegas", "Vegas"), ("ec.generate.writeon", "Write-on", "書寫"), ("ec.key.advancedspill", "Advanced Spill Suppressor", "進階溢色抑制"), - ("ec.key.ccsimplewireremoval", "CC Simple Wire Removal", "CC 簡易鋼絲移除"), + ("ec.key.ccsimplewireremoval", "Line Removal", "線條移除"), ("ec.key.colordifference", "Color Difference Key", "色彩差異去背"), ("ec.key.colorkey", "Color Key", "色彩去背"), ("ec.key.colorrange", "Color Range", "色彩範圍"), @@ -664,7 +671,7 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.key.screen", "Screen Key", "螢幕去背"), ("ec.key.spill", "Spill Suppressor", "溢色抑制"), ("ec.key.unmult", "Unmult", "Unmult"), - ("ec.keying.keylight", "Key Light", "Key Light"), + ("ec.keying.keylight", "Screen Color Key", "螢幕色鍵"), ("ec.matte.mattechoker", "Matte Choker", "遮罩收縮器"), ("ec.matte.objectmatte", "Object Matte", "物件遮罩"), ("ec.matte.refinehard", "Refine Hard Matte", "調整硬邊遮罩"), @@ -688,50 +695,50 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.obsolete.basictext", "Basic Text", "基礎文字"), ("ec.obsolete.gaussianlegacy", "Gaussian Blur (Legacy)", "高斯模糊(舊版)"), ("ec.obsolete.lightning", "Lightning", "閃電"), - ("ec.obsolete.mochashape", "Mocha shape", "Mocha 形狀"), + ("ec.obsolete.mochashape", "Tracked Shape Matte", "追蹤形狀遮罩"), ("ec.obsolete.pathtext", "Path Text", "路徑文字"), ("ec.paint.paint", "Paint", "繪畫"), ("ec.perspective.3dglasses", "3D Glasses", "3D 眼鏡"), ("ec.perspective.bevelalpha", "Bevel Alpha", "Alpha 斜角"), ("ec.perspective.beveledges", "Bevel Edges", "邊緣斜角"), ("ec.perspective.cameratracker", "3D Camera Tracker", "3D 攝影機追蹤"), - ("ec.perspective.cccylinder", "CC Cylinder", "CC 圓柱"), - ("ec.perspective.ccenvironment", "CC Environment", "CC 環境"), - ("ec.perspective.ccsphere", "CC Sphere", "CC 球體"), - ("ec.perspective.ccspotlight", "CC Spotlight", "CC 聚光燈"), + ("ec.perspective.cccylinder", "Cylinder Wrap", "圓柱包覆"), + ("ec.perspective.ccenvironment", "Environment Map", "環境貼圖"), + ("ec.perspective.ccsphere", "Sphere Wrap", "球面包覆"), + ("ec.perspective.ccspotlight", "Surface Spotlight", "表面聚光燈"), ("ec.perspective.dropshadow", "Drop Shadow", "陰影"), ("ec.perspective.radialshadow", "Radial Shadow", "徑向陰影"), ("ec.sim.carddance", "Card Dance", "卡片舞蹈"), ("ec.sim.caustics", "Caustics", "焦散"), - ("ec.sim.ccballaction", "CC Ball Action", "CC 球狀動作"), - ("ec.sim.ccbubbles", "CC Bubbles", "CC 泡泡"), - ("ec.sim.ccdrizzle", "CC Drizzle", "CC 細雨"), - ("ec.sim.cchair", "CC Hair", "CC 毛髮"), - ("ec.sim.ccmrmercury", "CC Mr. Mercury", "CC 水銀先生"), - ("ec.sim.ccparticlesystems2", "CC Particle Systems II", "CC 粒子系統 II"), - ("ec.sim.ccparticleworld", "CC Particle World", "CC 粒子世界"), - ("ec.sim.ccpixelpolly", "CC Pixel Polly", "CC 像素破碎"), - ("ec.sim.ccrainfall", "CC Rainfall", "CC 下雨"), - ("ec.sim.ccscatterize", "CC Scatterize", "CC 散開"), - ("ec.sim.ccsnowfall", "CC Snowfall", "CC 下雪"), - ("ec.sim.ccstarburst", "CC Star Burst", "CC 星爆"), + ("ec.sim.ccballaction", "Ball Grid", "球體網格"), + ("ec.sim.ccbubbles", "Bubbles", "氣泡"), + ("ec.sim.ccdrizzle", "Water Ripples", "水面漣漪"), + ("ec.sim.cchair", "Surface Fibers", "表面纖維"), + ("ec.sim.ccmrmercury", "Liquid Particles", "液體粒子"), + ("ec.sim.ccparticlesystems2", "Particle Emitter 2D", "二維粒子發射器"), + ("ec.sim.ccparticleworld", "Particle Emitter 3D", "三維粒子發射器"), + ("ec.sim.ccpixelpolly", "Pixel Shatter", "像素破碎"), + ("ec.sim.ccrainfall", "Rainfall", "降雨"), + ("ec.sim.ccscatterize", "Pixel Scatter", "像素散射"), + ("ec.sim.ccsnowfall", "Snowfall", "降雪"), + ("ec.sim.ccstarburst", "Starfield", "星空"), ("ec.sim.foam", "Foam", "泡沫"), ("ec.sim.particleplayground", "Particle Playground", "粒子遊樂場"), ("ec.sim.shatter", "Shatter", "粉碎"), ("ec.sim.waveworld", "Wave World", "波浪世界"), ("ec.stylize.brushstrokes", "Brush Strokes", "筆觸"), ("ec.stylize.cartoon", "Cartoon", "卡通"), - ("ec.stylize.ccblockload", "CC Block Load", "CC 區塊載入"), - ("ec.stylize.ccburnfilm", "CC Burn Film", "CC 燒灼底片"), - ("ec.stylize.ccglass", "CC Glass", "CC 玻璃"), - ("ec.stylize.cchextile", "CC HexTile", "CC 六角並排"), - ("ec.stylize.cckaleida", "CC Kaleida", "CC 萬花筒"), - ("ec.stylize.ccmrsmoothie", "CC Mr. Smoothie", "CC 平滑先生"), - ("ec.stylize.ccplastic", "CC Plastic", "CC 塑膠"), - ("ec.stylize.ccrepetile", "CC RepeTile", "CC 重複並排"), - ("ec.stylize.ccthreshold", "CC Threshold", "CC 臨界值"), - ("ec.stylize.ccthresholdrgb", "CC Threshold RGB", "CC 臨界值 RGB"), - ("ec.stylize.ccvignette", "CC Vignette", "CC 暗角"), + ("ec.stylize.ccblockload", "Block Reveal", "區塊顯現"), + ("ec.stylize.ccburnfilm", "Film Burn", "膠片灼燒"), + ("ec.stylize.ccglass", "Glass Relief", "玻璃浮雕"), + ("ec.stylize.cchextile", "Hexagonal Tiles", "六角形拼貼"), + ("ec.stylize.cckaleida", "Kaleidoscope", "萬花筒"), + ("ec.stylize.ccmrsmoothie", "Palette Map", "調色盤對應"), + ("ec.stylize.ccplastic", "Plastic Shading", "塑膠著色"), + ("ec.stylize.ccrepetile", "Edge Tile Repeat", "邊緣拼貼重複"), + ("ec.stylize.ccthreshold", "Channel Threshold", "色版臨界值"), + ("ec.stylize.ccthresholdrgb", "RGB Threshold", "RGB 臨界值"), + ("ec.stylize.ccvignette", "Vignette", "暗角"), ("ec.stylize.coloremboss", "Color Emboss", "彩色浮雕"), ("ec.stylize.emboss", "Emboss", "浮雕"), ("ec.stylize.findedges", "Find Edges", "尋找邊緣"), @@ -746,8 +753,8 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.stylize.threshold", "Threshold", "臨界值"), ("ec.text.numbers", "Numbers", "數字"), ("ec.text.timecode", "Timecode", "時間碼"), - ("ec.time.ccforcemotionblur", "CC Force Motion Blur", "CC 強制動態模糊"), - ("ec.time.ccwidetime", "CC Wide Time", "CC 寬廣時間"), + ("ec.time.ccforcemotionblur", "Temporal Motion Blur", "時間動態模糊"), + ("ec.time.ccwidetime", "Temporal Blend", "時間混合"), ("ec.time.echo", "Echo", "回音"), ("ec.time.pixelmotionblur", "Pixel Motion Blur", "像素動態模糊"), ("ec.time.posterizetime", "Posterize Time", "色調分離時間"), @@ -756,23 +763,23 @@ const TRADITIONAL_CHINESE: &[Row] = &[ ("ec.time.timewarp", "Timewarp", "時間扭曲"), ("ec.transition.blockdissolve", "Block Dissolve", "區塊溶解"), ("ec.transition.cardwipe", "Card Wipe", "卡片擦拭"), - ("ec.transition.ccglasswipe", "CC Glass Wipe", "CC 玻璃擦拭"), - ("ec.transition.ccgridwipe", "CC Grid Wipe", "CC 格線擦拭"), - ("ec.transition.ccimagewipe", "CC Image Wipe", "CC 影像擦拭"), - ("ec.transition.ccjaws", "CC Jaws", "CC 巨顎"), - ("ec.transition.cclightwipe", "CC Light Wipe", "CC 光線擦拭"), - ("ec.transition.cclinesweep", "CC Line Sweep", "CC 線條掃掠"), - ("ec.transition.ccradialscalewipe", "CC Radial ScaleWipe", "CC 徑向縮放擦拭"), - ("ec.transition.ccscalewipe", "CC Scale Wipe", "CC 縮放擦拭"), - ("ec.transition.cctwister", "CC Twister", "CC 扭轉"), - ("ec.transition.ccwarpomatic", "CC WarpoMatic", "CC 自動彎曲"), + ("ec.transition.ccglasswipe", "Glass Wipe", "玻璃擦拭"), + ("ec.transition.ccgridwipe", "Grid Wipe", "網格擦拭"), + ("ec.transition.ccimagewipe", "Luminance Image Wipe", "亮度影像擦拭"), + ("ec.transition.ccjaws", "Serrated Wipe", "鋸齒擦拭"), + ("ec.transition.cclightwipe", "Light Wipe", "光線擦拭"), + ("ec.transition.cclinesweep", "Line Sweep", "線條掃過"), + ("ec.transition.ccradialscalewipe", "Radial Scale Wipe", "放射狀縮放擦拭"), + ("ec.transition.ccscalewipe", "Scale Wipe", "縮放擦拭"), + ("ec.transition.cctwister", "Twist Wipe", "扭轉擦拭"), + ("ec.transition.ccwarpomatic", "Warp Dissolve", "變形溶解"), ("ec.transition.gradientwipe", "Gradient Wipe", "漸層擦拭"), ("ec.transition.iriswipe", "Iris Wipe", "光圈擦拭"), ("ec.transition.linearwipe", "Linear Wipe", "線性擦拭"), ("ec.transition.radialwipe", "Radial Wipe", "徑向擦拭"), ("ec.transition.venetian", "Venetian Blinds", "百葉窗"), ("ec.utility.applylut", "Apply Color LUT", "套用色彩 LUT"), - ("ec.utility.ccoverbrights", "CC Overbrights", "CC 過亮"), + ("ec.utility.ccoverbrights", "Out-of-Range Display", "超出範圍顯示"), ("ec.utility.cineon", "Cineon Converter", "Cineon 轉換器"), ("ec.utility.colorprofileconverter", "Color Profile Converter", "色彩設定檔轉換器"), ("ec.utility.facemeasurements", "Face Measurements", "臉部測量"), @@ -796,18 +803,18 @@ const TRADITIONAL_CHINESE: &[Row] = &[ /// Japanese (`ja`): reserved for the Japanese data point on #821, so both languages share this /// one catalog instead of inventing a second mechanism. A translator fills one row per effect, -/// ids from [`SIMPLIFIED_CHINESE`]; the names Adobe's Japanese interface leaves in English (the -/// `CC` series, `Cryptomatte`, `EXtractoR`, `IDentifier` and `Key Light`) repeat that English name -/// here rather than being left out; the `VR` and `OCIO` sets take a Japanese tail instead, and -/// `Unmult` keeps its English name in both catalogs. An effect with no row draws its English name -/// anyway, which is what `Twirl (Legacy)` and the two EffectCraft-only effects would do without -/// their rows. +/// ids from [`SIMPLIFIED_CHINESE`]; the names Adobe's Japanese interface leaves in English +/// (`Cryptomatte`, `EXtractoR`, `IDentifier` and `Key Light`) repeat that English name here rather +/// than being left out; the `VR` and `OCIO` sets take a Japanese tail instead, and `Unmult` keeps +/// its English name in both catalogs. An effect with no row draws its English name anyway, which +/// is what `Twirl (Legacy)` and the two EffectCraft-only effects would do without their rows. /// -/// The rows below come from AE 2026's Japanese interface where it was read directly (its Effect menu -/// and its Effects & Presets panel) and from Adobe's own Japanese help where the panel was not to -/// hand. A published glossary is used to cross-check, never as the only source: it normalises the -/// typography (half-width parentheses where AE draws full-width) and still carries CS6-era names for -/// the `CC` series, which AE 2026 leaves in English. +/// The rows of the effects this cleanup did not rename come from AE 2026's Japanese interface +/// where it was read directly (its Effect menu and its Effects & Presets panel) and from Adobe's +/// own Japanese help where the panel was not to hand. A published glossary is used to cross-check, +/// never as the only source: it normalises the typography (half-width parentheses where AE draws +/// full-width) and still carries CS6-era names for the `CC` series. The renamed effects are +/// translated from their new descriptive names. const JAPANESE_EFFECTS: &[Row] = &[ // Effects this application adds itself have no interface to read a name from, so they follow // the Chinese catalog's choice to translate them (顔の測定, 顔のトラッキングポイント) rather than @@ -847,10 +854,10 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.blur.bilateral", "Bilateral Blur", "ブラー(バイラテラル)"), ("ec.blur.cameralens", "Camera Lens Blur", "ブラー(カメラレンズ)"), ("ec.blur.camerashakedeblur", "Camera-Shake Deblur", "カメラぶれ除去"), - ("ec.blur.cccross", "CC Cross Blur", "CC Cross Blur"), - ("ec.blur.ccradial", "CC Radial Blur", "CC Radial Blur"), - ("ec.blur.ccradialfast", "CC Radial Fast Blur", "CC Radial Fast Blur"), - ("ec.blur.ccvector", "CC Vector Blur", "CC Vector Blur"), + ("ec.blur.cccross", "Cross Blur", "十字ぼかし"), + ("ec.blur.ccradial", "Variable Radial Blur", "可変放射状ぼかし"), + ("ec.blur.ccradialfast", "Fast Radial Blur", "高速放射状ぼかし"), + ("ec.blur.ccvector", "Vector Blur", "ベクトルぼかし"), ("ec.blur.channel", "Channel Blur", "ブラー(チャンネル)"), ("ec.blur.compound", "Compound Blur", "ブラー(合成)"), ("ec.blur.directional", "Directional Blur", "ブラー(方向)"), @@ -864,7 +871,7 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.channel.arithmetic", "Arithmetic", "アリスマチック"), ("ec.channel.blend", "Blend", "ブレンド"), ("ec.channel.calculations", "Calculations", "計算"), - ("ec.channel.cccomposite", "CC Composite", "CC Composite"), + ("ec.channel.cccomposite", "Source Composite", "元画像合成"), ("ec.channel.combiner", "Channel Combiner", "チャンネルコンバイナー"), ("ec.channel.compoundarithmetic", "Compound Arithmetic", "合成アリスマチック"), ("ec.channel.invert", "Invert", "反転"), @@ -879,10 +886,10 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.color.autolevels", "Auto Levels", "自動レベル補正"), ("ec.color.blackwhite", "Black & White", "白黒"), ("ec.color.brightnesscontrast", "Brightness & Contrast", "輝度&コントラスト"), - ("ec.color.cccolorneutralizer", "CC Color Neutralizer", "CC Color Neutralizer"), - ("ec.color.cccoloroffset", "CC Color Offset", "CC Color Offset"), - ("ec.color.cckernel", "CC Kernel", "CC Kernel"), - ("ec.color.cctoner", "CC Toner", "CC Toner"), + ("ec.color.cccolorneutralizer", "Color Neutralizer", "色かぶり補正"), + ("ec.color.cccoloroffset", "Channel Offset", "チャンネルオフセット"), + ("ec.color.cckernel", "Convolution Kernel", "畳み込みカーネル"), + ("ec.color.cctoner", "Multi-Tone Tint", "多色調ティント"), ("ec.color.changecolor", "Change Color", "色を変更"), ("ec.color.changetocolor", "Change to Color", "他のカラーへ変更"), ("ec.color.channelmixer", "Channel Mixer", "チャンネルミキサー"), @@ -899,7 +906,7 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.color.leavecolor", "Leave Color", "色抜き"), ("ec.color.levels", "Levels", "レベル補正"), ("ec.color.levelsic", "Levels (Individual Controls)", "レベル(個々の制御)"), - ("ec.color.lumetri", "Lumetri Color", "Lumetri カラー"), + ("ec.color.lumetri", "Color Grading", "カラーグレーディング"), ("ec.color.ociocdl", "OCIO CDL Transform", "OCIO CDL 変換"), ("ec.color.ociocolorspace", "OCIO Color Space Transform", "OCIO 色空間変換"), ("ec.color.ociodisplay", "OCIO Display Transform", "OCIO 表示変換"), @@ -909,7 +916,7 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.color.psarbitrarymap", "PS Arbitrary Map", "PS不定マップ"), ("ec.color.selectivecolor", "Selective Color", "特定色域の選択"), ("ec.color.shadowhighlight", "Shadow/Highlight", "シャドウ・ハイライト"), - ("ec.color.tint", "Tint", "色かぶり補正"), + ("ec.color.tint", "Tint", "着色"), ("ec.color.tritone", "Tritone", "トライトーン"), ("ec.color.vibrance", "Vibrance", "自然な彩度"), ("ec.color.videolimiter", "Video Limiter", "ビデオリミッター"), @@ -923,20 +930,20 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.control.slider", "Slider Control", "スライダー制御"), ("ec.distort.bezierwarp", "Bezier Warp", "ベジェワープ"), ("ec.distort.bulge", "Bulge", "バルジ"), - ("ec.distort.ccbender", "CC Bender", "CC Bender"), - ("ec.distort.ccbendit", "CC Bend It", "CC Bend It"), - ("ec.distort.ccblobbylize", "CC Blobbylize", "CC Blobbylize"), - ("ec.distort.ccflomotion", "CC Flo Motion", "CC Flo Motion"), - ("ec.distort.ccgriddler", "CC Griddler", "CC Griddler"), - ("ec.distort.cclens", "CC Lens", "CC Lens"), - ("ec.distort.ccpageturn", "CC Page Turn", "CC Page Turn"), - ("ec.distort.ccpowerpin", "CC Power Pin", "CC Power Pin"), - ("ec.distort.ccripplepulse", "CC Ripple Pulse", "CC Ripple Pulse"), - ("ec.distort.ccslant", "CC Slant", "CC Slant"), - ("ec.distort.ccsmear", "CC Smear", "CC Smear"), - ("ec.distort.ccsplit", "CC Split", "CC Split"), - ("ec.distort.ccsplit2", "CC Split 2", "CC Split 2"), - ("ec.distort.cctiler", "CC Tiler", "CC Tiler"), + ("ec.distort.ccbender", "Flexible Bend", "柔軟ベンド"), + ("ec.distort.ccbendit", "Two-Point Bend", "2点ベンド"), + ("ec.distort.ccblobbylize", "Organic Displacement", "有機変位"), + ("ec.distort.ccflomotion", "Dual Lens Warp", "ダブルレンズ変形"), + ("ec.distort.ccgriddler", "Tile Grid", "タイルグリッド"), + ("ec.distort.cclens", "Lens Distortion", "レンズ歪み"), + ("ec.distort.ccpageturn", "Page Curl", "ページカール"), + ("ec.distort.ccpowerpin", "Perspective Pin", "遠近ピン"), + ("ec.distort.ccripplepulse", "Pulsed Ripple", "パルス波紋"), + ("ec.distort.ccslant", "Slant", "傾斜"), + ("ec.distort.ccsmear", "Directional Smear", "方向性にじみ"), + ("ec.distort.ccsplit", "Split Warp", "分割変形"), + ("ec.distort.ccsplit2", "Dual Split Warp", "二重分割変形"), + ("ec.distort.cctiler", "Scaled Tiles", "スケールタイル"), ("ec.distort.cornerpin", "Corner Pin", "コーナーピン"), ("ec.distort.displacementmap", "Displacement Map", "ディスプレイスメントマップ"), ("ec.distort.liquify", "Liquify", "ゆがみ"), @@ -963,11 +970,11 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.generate.audiospectrum", "Audio Spectrum", "オーディオスペクトラム"), ("ec.generate.audiowaveform", "Audio Waveform", "オーディオウェーブフォーム"), ("ec.generate.beam", "Beam", "レーザー"), - ("ec.generate.ccgluegun", "CC Glue Gun", "CC Glue Gun"), - ("ec.generate.cclightburst", "CC Light Burst 2.5", "CC Light Burst 2.5"), - ("ec.generate.cclightrays", "CC Light Rays", "CC Light Rays"), - ("ec.generate.cclightsweep", "CC Light Sweep", "CC Light Sweep"), - ("ec.generate.ccthreads", "CC Threads", "CC Threads"), + ("ec.generate.ccgluegun", "Paint Beads", "絵の具の粒"), + ("ec.generate.cclightburst", "Radial Light Burst", "放射状の光"), + ("ec.generate.cclightrays", "Light Rays", "光線"), + ("ec.generate.cclightsweep", "Light Sweep", "光のスイープ"), + ("ec.generate.ccthreads", "Woven Threads", "織り糸"), ("ec.generate.cellpattern", "Cell Pattern", "セルパターン"), ("ec.generate.checkerboard", "Checkerboard", "チェッカーボード"), ("ec.generate.circle", "Circle", "円"), @@ -986,7 +993,7 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.generate.vegas", "Vegas", "ベガス"), ("ec.generate.writeon", "Write-on", "ブラシアニメーション"), ("ec.key.advancedspill", "Advanced Spill Suppressor", "Advanced Spill Suppressor"), - ("ec.key.ccsimplewireremoval", "CC Simple Wire Removal", "CC Simple Wire Removal"), + ("ec.key.ccsimplewireremoval", "Line Removal", "ライン除去"), ("ec.key.colordifference", "Color Difference Key", "カラー差キー"), ("ec.key.colorkey", "Color Key", "カラーキー"), ("ec.key.colorrange", "Color Range", "色範囲"), @@ -997,7 +1004,7 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.key.linearcolor", "Linear Color Key", "リニアカラーキー"), ("ec.key.luma", "Luma Key", "ルミナンスキー"), ("ec.key.spill", "Spill Suppressor", "スピルサプレッション"), - ("ec.keying.keylight", "Key Light", "Keylight (1.2)"), + ("ec.keying.keylight", "Screen Color Key", "スクリーンカラーキー"), ("ec.matte.mattechoker", "Matte Choker", "マットチョーク"), ("ec.matte.refinehard", "Refine Hard Matte", "ハードマットを調整"), ("ec.matte.refinesoft", "Refine Soft Matte", "ソフトマットを調整"), @@ -1019,49 +1026,49 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.obsolete.basictext", "Basic Text", "基本テキスト"), ("ec.obsolete.gaussianlegacy", "Gaussian Blur (Legacy)", "ブラー(ガウス)(レガシー)"), ("ec.obsolete.lightning", "Lightning", "稲妻"), - ("ec.obsolete.mochashape", "Mocha shape", "mocha shape"), + ("ec.obsolete.mochashape", "Tracked Shape Matte", "トラッキングシェイプマット"), ("ec.obsolete.pathtext", "Path Text", "パステキスト"), ("ec.perspective.3dglasses", "3D Glasses", "3Dメガネ"), ("ec.perspective.bevelalpha", "Bevel Alpha", "ベベルアルファ"), ("ec.perspective.beveledges", "Bevel Edges", "ベベルエッジ"), ("ec.perspective.cameratracker", "3D Camera Tracker", "3D カメラトラッカー"), - ("ec.perspective.cccylinder", "CC Cylinder", "CC Cylinder"), - ("ec.perspective.ccenvironment", "CC Environment", "CC Environment"), - ("ec.perspective.ccsphere", "CC Sphere", "CC Sphere"), - ("ec.perspective.ccspotlight", "CC Spotlight", "CC Spotlight"), + ("ec.perspective.cccylinder", "Cylinder Wrap", "円筒ラップ"), + ("ec.perspective.ccenvironment", "Environment Map", "環境マップ"), + ("ec.perspective.ccsphere", "Sphere Wrap", "球面ラップ"), + ("ec.perspective.ccspotlight", "Surface Spotlight", "表面スポットライト"), ("ec.perspective.dropshadow", "Drop Shadow", "ドロップシャドウ"), ("ec.perspective.radialshadow", "Radial Shadow", "放射状シャドウ"), ("ec.sim.carddance", "Card Dance", "カードダンス"), ("ec.sim.caustics", "Caustics", "コースティック"), - ("ec.sim.ccballaction", "CC Ball Action", "CC Ball Action"), - ("ec.sim.ccbubbles", "CC Bubbles", "CC Bubbles"), - ("ec.sim.ccdrizzle", "CC Drizzle", "CC Drizzle"), - ("ec.sim.cchair", "CC Hair", "CC Hair"), - ("ec.sim.ccmrmercury", "CC Mr. Mercury", "CC Mr. Mercury"), - ("ec.sim.ccparticlesystems2", "CC Particle Systems II", "CC Particle Systems II"), - ("ec.sim.ccparticleworld", "CC Particle World", "CC Particle World"), - ("ec.sim.ccpixelpolly", "CC Pixel Polly", "CC Pixel Polly"), - ("ec.sim.ccrainfall", "CC Rainfall", "CC Rainfall"), - ("ec.sim.ccscatterize", "CC Scatterize", "CC Scatterize"), - ("ec.sim.ccsnowfall", "CC Snowfall", "CC Snowfall"), - ("ec.sim.ccstarburst", "CC Star Burst", "CC Star Burst"), + ("ec.sim.ccballaction", "Ball Grid", "球体グリッド"), + ("ec.sim.ccbubbles", "Bubbles", "気泡"), + ("ec.sim.ccdrizzle", "Water Ripples", "水面の波紋"), + ("ec.sim.cchair", "Surface Fibers", "表面の繊維"), + ("ec.sim.ccmrmercury", "Liquid Particles", "液体パーティクル"), + ("ec.sim.ccparticlesystems2", "Particle Emitter 2D", "2Dパーティクルエミッター"), + ("ec.sim.ccparticleworld", "Particle Emitter 3D", "3Dパーティクルエミッター"), + ("ec.sim.ccpixelpolly", "Pixel Shatter", "ピクセル破砕"), + ("ec.sim.ccrainfall", "Rainfall", "雨"), + ("ec.sim.ccscatterize", "Pixel Scatter", "ピクセル散乱"), + ("ec.sim.ccsnowfall", "Snowfall", "雪"), + ("ec.sim.ccstarburst", "Starfield", "星空"), ("ec.sim.foam", "Foam", "泡"), ("ec.sim.particleplayground", "Particle Playground", "パーティクルプレイグラウンド"), ("ec.sim.shatter", "Shatter", "シャター"), ("ec.sim.waveworld", "Wave World", "ウェーブワールド"), ("ec.stylize.brushstrokes", "Brush Strokes", "ブラシストローク"), ("ec.stylize.cartoon", "Cartoon", "カートゥーン"), - ("ec.stylize.ccblockload", "CC Block Load", "CC Block Load"), - ("ec.stylize.ccburnfilm", "CC Burn Film", "CC Burn Film"), - ("ec.stylize.ccglass", "CC Glass", "CC Glass"), - ("ec.stylize.cchextile", "CC HexTile", "CC HexTile"), - ("ec.stylize.cckaleida", "CC Kaleida", "CC Kaleida"), - ("ec.stylize.ccmrsmoothie", "CC Mr. Smoothie", "CC Mr. Smoothie"), - ("ec.stylize.ccplastic", "CC Plastic", "CC Plastic"), - ("ec.stylize.ccrepetile", "CC RepeTile", "CC RepeTile"), - ("ec.stylize.ccthreshold", "CC Threshold", "CC Threshold"), - ("ec.stylize.ccthresholdrgb", "CC Threshold RGB", "CC Threshold RGB"), - ("ec.stylize.ccvignette", "CC Vignette", "CC Vignette"), + ("ec.stylize.ccblockload", "Block Reveal", "ブロック表示"), + ("ec.stylize.ccburnfilm", "Film Burn", "フィルム焼け"), + ("ec.stylize.ccglass", "Glass Relief", "ガラスレリーフ"), + ("ec.stylize.cchextile", "Hexagonal Tiles", "六角形タイル"), + ("ec.stylize.cckaleida", "Kaleidoscope", "万華鏡"), + ("ec.stylize.ccmrsmoothie", "Palette Map", "パレットマップ"), + ("ec.stylize.ccplastic", "Plastic Shading", "プラスチックシェーディング"), + ("ec.stylize.ccrepetile", "Edge Tile Repeat", "エッジタイル反復"), + ("ec.stylize.ccthreshold", "Channel Threshold", "チャンネルしきい値"), + ("ec.stylize.ccthresholdrgb", "RGB Threshold", "RGBしきい値"), + ("ec.stylize.ccvignette", "Vignette", "ビネット"), ("ec.stylize.coloremboss", "Color Emboss", "カラーエンボス"), ("ec.stylize.emboss", "Emboss", "エンボス"), ("ec.stylize.findedges", "Find Edges", "輪郭検出"), @@ -1076,8 +1083,8 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.stylize.threshold", "Threshold", "しきい値"), ("ec.text.numbers", "Numbers", "番号"), ("ec.text.timecode", "Timecode", "タイムコード"), - ("ec.time.ccforcemotionblur", "CC Force Motion Blur", "CC Force Motion Blur"), - ("ec.time.ccwidetime", "CC Wide Time", "CC Wide Time"), + ("ec.time.ccforcemotionblur", "Temporal Motion Blur", "時間方向モーションブラー"), + ("ec.time.ccwidetime", "Temporal Blend", "時間ブレンド"), ("ec.time.echo", "Echo", "エコー"), ("ec.time.pixelmotionblur", "Pixel Motion Blur", "ピクセルモーションブラー"), ("ec.time.posterizetime", "Posterize Time", "ポスタリゼーション時間"), @@ -1086,23 +1093,23 @@ const JAPANESE_EFFECTS: &[Row] = &[ ("ec.time.timewarp", "Timewarp", "タイムワープ"), ("ec.transition.blockdissolve", "Block Dissolve", "ブロックディゾルブ"), ("ec.transition.cardwipe", "Card Wipe", "カードワイプ"), - ("ec.transition.ccglasswipe", "CC Glass Wipe", "CC Glass Wipe"), - ("ec.transition.ccgridwipe", "CC Grid Wipe", "CC Grid Wipe"), - ("ec.transition.ccimagewipe", "CC Image Wipe", "CC Image Wipe"), - ("ec.transition.ccjaws", "CC Jaws", "CC Jaws"), - ("ec.transition.cclightwipe", "CC Light Wipe", "CC Light Wipe"), - ("ec.transition.cclinesweep", "CC Line Sweep", "CC Line Sweep"), - ("ec.transition.ccradialscalewipe", "CC Radial ScaleWipe", "CC Radial ScaleWipe"), - ("ec.transition.ccscalewipe", "CC Scale Wipe", "CC Scale Wipe"), - ("ec.transition.cctwister", "CC Twister", "CC Twister"), - ("ec.transition.ccwarpomatic", "CC WarpoMatic", "CC WarpoMatic"), + ("ec.transition.ccglasswipe", "Glass Wipe", "ガラスワイプ"), + ("ec.transition.ccgridwipe", "Grid Wipe", "グリッドワイプ"), + ("ec.transition.ccimagewipe", "Luminance Image Wipe", "輝度画像ワイプ"), + ("ec.transition.ccjaws", "Serrated Wipe", "鋸歯状ワイプ"), + ("ec.transition.cclightwipe", "Light Wipe", "ライトワイプ"), + ("ec.transition.cclinesweep", "Line Sweep", "ラインスイープ"), + ("ec.transition.ccradialscalewipe", "Radial Scale Wipe", "放射状スケールワイプ"), + ("ec.transition.ccscalewipe", "Scale Wipe", "スケールワイプ"), + ("ec.transition.cctwister", "Twist Wipe", "ツイストワイプ"), + ("ec.transition.ccwarpomatic", "Warp Dissolve", "変形ディゾルブ"), ("ec.transition.gradientwipe", "Gradient Wipe", "グラデーションワイプ"), ("ec.transition.iriswipe", "Iris Wipe", "アイリスワイプ"), ("ec.transition.linearwipe", "Linear Wipe", "リニアワイプ"), ("ec.transition.radialwipe", "Radial Wipe", "放射状ワイプ"), ("ec.transition.venetian", "Venetian Blinds", "ブラインド"), ("ec.utility.applylut", "Apply Color LUT", "カラー LUT を適用"), - ("ec.utility.ccoverbrights", "CC Overbrights", "CC Overbrights"), + ("ec.utility.ccoverbrights", "Out-of-Range Display", "範囲外表示"), ("ec.utility.cineon", "Cineon Converter", "Cineon コンバーター"), ("ec.utility.colorprofileconverter", "Color Profile Converter", "カラープロファイルコンバーター"), ("ec.utility.growbounds", "Grow Bounds", "範囲拡張"), @@ -1176,9 +1183,9 @@ const JAPANESE_CATEGORIES: &[(&str, &str)] = &[ /// Georgian (`ka`): one row per effect, ids from [`SIMPLIFIED_CHINESE`]. Written from the meaning /// of the English names, with the terms the Georgian menus and panels use (FilmCraft's Georgian -/// for the effects the two apps share). The `CC` series and the brand names (`Cryptomatte`, -/// `EXtractoR`, `IDentifier`, `Key Light`, `Unmult`, `Lumetri Color`) stay in English, as -/// Adobe's Japanese interface keeps the `CC` series; `VR` and `OCIO` keep their prefix. +/// for the effects the two apps share). The brand names (`Cryptomatte`, `EXtractoR`, `IDentifier`, +/// `Key Light`, `Unmult`) stay in English; `VR` and `OCIO` keep their prefix. The renamed effects +/// are translated from their new descriptive names. const GEORGIAN: &[Row] = &[ ("ec.3d.channelextract", "3D Channel Extract", "3D არხის ამოღება"), ("ec.3d.cryptomatte", "Cryptomatte", "Cryptomatte"), @@ -1205,10 +1212,10 @@ const GEORGIAN: &[Row] = &[ ("ec.blur.bilateral", "Bilateral Blur", "ბილატერალური გაბუნდოვნება"), ("ec.blur.cameralens", "Camera Lens Blur", "კამერის ობიექტივის გაბუნდოვნება"), ("ec.blur.camerashakedeblur", "Camera-Shake Deblur", "კამერის რყევით გაბუნდოვნების მოცილება"), - ("ec.blur.cccross", "CC Cross Blur", "CC Cross Blur"), - ("ec.blur.ccradial", "CC Radial Blur", "CC Radial Blur"), - ("ec.blur.ccradialfast", "CC Radial Fast Blur", "CC Radial Fast Blur"), - ("ec.blur.ccvector", "CC Vector Blur", "CC Vector Blur"), + ("ec.blur.cccross", "Cross Blur", "ჯვარედინი დაბინდვა"), + ("ec.blur.ccradial", "Variable Radial Blur", "ცვლადი რადიალური დაბინდვა"), + ("ec.blur.ccradialfast", "Fast Radial Blur", "სწრაფი რადიალური დაბინდვა"), + ("ec.blur.ccvector", "Vector Blur", "ვექტორული დაბინდვა"), ("ec.blur.channel", "Channel Blur", "არხის გაბუნდოვნება"), ("ec.blur.compound", "Compound Blur", "შედგენილი გაბუნდოვნება"), ("ec.blur.directional", "Directional Blur", "მიმართული გაბუნდოვნება"), @@ -1222,7 +1229,7 @@ const GEORGIAN: &[Row] = &[ ("ec.channel.arithmetic", "Arithmetic", "არითმეტიკა"), ("ec.channel.blend", "Blend", "შერევა"), ("ec.channel.calculations", "Calculations", "გამოთვლები"), - ("ec.channel.cccomposite", "CC Composite", "CC Composite"), + ("ec.channel.cccomposite", "Source Composite", "წყაროს კომპოზიცია"), ("ec.channel.combiner", "Channel Combiner", "არხების გამაერთიანებელი"), ("ec.channel.compoundarithmetic", "Compound Arithmetic", "შედგენილი არითმეტიკა"), ("ec.channel.invert", "Invert", "ინვერსია"), @@ -1238,10 +1245,10 @@ const GEORGIAN: &[Row] = &[ ("ec.color.blackwhite", "Black & White", "შავ-თეთრი"), ("ec.color.brightnesscontrast", "Brightness & Contrast", "სიკაშკაშე და კონტრასტი"), ("ec.color.broadcast", "Broadcast Colors", "სამაუწყებლო ფერები"), - ("ec.color.cccolorneutralizer", "CC Color Neutralizer", "CC Color Neutralizer"), - ("ec.color.cccoloroffset", "CC Color Offset", "CC Color Offset"), - ("ec.color.cckernel", "CC Kernel", "CC Kernel"), - ("ec.color.cctoner", "CC Toner", "CC Toner"), + ("ec.color.cccolorneutralizer", "Color Neutralizer", "ფერის ნეიტრალიზაცია"), + ("ec.color.cccoloroffset", "Channel Offset", "არხების წანაცვლება"), + ("ec.color.cckernel", "Convolution Kernel", "კონვოლუციის ბირთვი"), + ("ec.color.cctoner", "Multi-Tone Tint", "მრავალტონიანი შეფერილობა"), ("ec.color.changecolor", "Change Color", "ფერის შეცვლა"), ("ec.color.changetocolor", "Change to Color", "ფერად შეცვლა"), ("ec.color.channelmixer", "Channel Mixer", "არხების მიქშერი"), @@ -1258,7 +1265,7 @@ const GEORGIAN: &[Row] = &[ ("ec.color.leavecolor", "Leave Color", "ფერის დატოვება"), ("ec.color.levels", "Levels", "დონეები"), ("ec.color.levelsic", "Levels (Individual Controls)", "დონეები (ცალკეული მართვა)"), - ("ec.color.lumetri", "Lumetri Color", "Lumetri Color"), + ("ec.color.lumetri", "Color Grading", "ფერთა კორექცია"), ("ec.color.ociocdl", "OCIO CDL Transform", "OCIO CDL გარდაქმნა"), ("ec.color.ociocolorspace", "OCIO Color Space Transform", "OCIO ფერთა სივრცის გარდაქმნა"), ("ec.color.ociodisplay", "OCIO Display Transform", "OCIO ეკრანის გარდაქმნა"), @@ -1283,20 +1290,20 @@ const GEORGIAN: &[Row] = &[ ("ec.control.stereo3d", "Stereo 3D Controls", "სტერეო 3D მართვა"), ("ec.distort.bezierwarp", "Bezier Warp", "ბეზიეს დეფორმაცია"), ("ec.distort.bulge", "Bulge", "გამობერილობა"), - ("ec.distort.ccbender", "CC Bender", "CC Bender"), - ("ec.distort.ccbendit", "CC Bend It", "CC Bend It"), - ("ec.distort.ccblobbylize", "CC Blobbylize", "CC Blobbylize"), - ("ec.distort.ccflomotion", "CC Flo Motion", "CC Flo Motion"), - ("ec.distort.ccgriddler", "CC Griddler", "CC Griddler"), - ("ec.distort.cclens", "CC Lens", "CC Lens"), - ("ec.distort.ccpageturn", "CC Page Turn", "CC Page Turn"), - ("ec.distort.ccpowerpin", "CC Power Pin", "CC Power Pin"), - ("ec.distort.ccripplepulse", "CC Ripple Pulse", "CC Ripple Pulse"), - ("ec.distort.ccslant", "CC Slant", "CC Slant"), - ("ec.distort.ccsmear", "CC Smear", "CC Smear"), - ("ec.distort.ccsplit", "CC Split", "CC Split"), - ("ec.distort.ccsplit2", "CC Split 2", "CC Split 2"), - ("ec.distort.cctiler", "CC Tiler", "CC Tiler"), + ("ec.distort.ccbender", "Flexible Bend", "მოქნილი მოღუნვა"), + ("ec.distort.ccbendit", "Two-Point Bend", "ორწერტილიანი მოღუნვა"), + ("ec.distort.ccblobbylize", "Organic Displacement", "ორგანული გადაადგილება"), + ("ec.distort.ccflomotion", "Dual Lens Warp", "ორმაგი ლინზით დეფორმაცია"), + ("ec.distort.ccgriddler", "Tile Grid", "ფილების ბადე"), + ("ec.distort.cclens", "Lens Distortion", "ლინზის დამახინჯება"), + ("ec.distort.ccpageturn", "Page Curl", "გვერდის დახვევა"), + ("ec.distort.ccpowerpin", "Perspective Pin", "პერსპექტიული დამაგრება"), + ("ec.distort.ccripplepulse", "Pulsed Ripple", "პულსური ტალღა"), + ("ec.distort.ccslant", "Slant", "დახრა"), + ("ec.distort.ccsmear", "Directional Smear", "მიმართულებითი გაწელვა"), + ("ec.distort.ccsplit", "Split Warp", "გაყოფილი დეფორმაცია"), + ("ec.distort.ccsplit2", "Dual Split Warp", "ორმაგად გაყოფილი დეფორმაცია"), + ("ec.distort.cctiler", "Scaled Tiles", "მასშტაბირებული ფილები"), ("ec.distort.cornerpin", "Corner Pin", "კუთხეების მიმაგრება"), ("ec.distort.displacementmap", "Displacement Map", "წანაცვლების რუკა"), ("ec.distort.liquify", "Liquify", "გათხევადება"), @@ -1324,11 +1331,11 @@ const GEORGIAN: &[Row] = &[ ("ec.generate.audiospectrum", "Audio Spectrum", "აუდიოს სპექტრი"), ("ec.generate.audiowaveform", "Audio Waveform", "აუდიოს ტალღის ფორმა"), ("ec.generate.beam", "Beam", "სხივი"), - ("ec.generate.ccgluegun", "CC Glue Gun", "CC Glue Gun"), - ("ec.generate.cclightburst", "CC Light Burst 2.5", "CC Light Burst 2.5"), - ("ec.generate.cclightrays", "CC Light Rays", "CC Light Rays"), - ("ec.generate.cclightsweep", "CC Light Sweep", "CC Light Sweep"), - ("ec.generate.ccthreads", "CC Threads", "CC Threads"), + ("ec.generate.ccgluegun", "Paint Beads", "საღებავის მარცვლები"), + ("ec.generate.cclightburst", "Radial Light Burst", "რადიალური ნათება"), + ("ec.generate.cclightrays", "Light Rays", "სინათლის სხივები"), + ("ec.generate.cclightsweep", "Light Sweep", "სინათლის გავლა"), + ("ec.generate.ccthreads", "Woven Threads", "ნაქსოვი ძაფები"), ("ec.generate.cellpattern", "Cell Pattern", "უჯრედების პატერნი"), ("ec.generate.checkerboard", "Checkerboard", "ჭადრაკის დაფა"), ("ec.generate.circle", "Circle", "წრე"), @@ -1347,7 +1354,7 @@ const GEORGIAN: &[Row] = &[ ("ec.generate.vegas", "Vegas", "ვეგასი"), ("ec.generate.writeon", "Write-on", "თანდათანობითი წერა"), ("ec.key.advancedspill", "Advanced Spill Suppressor", "ფერის გადაღვრის გაფართოებული ჩამხშობი"), - ("ec.key.ccsimplewireremoval", "CC Simple Wire Removal", "CC Simple Wire Removal"), + ("ec.key.ccsimplewireremoval", "Line Removal", "ხაზის მოცილება"), ("ec.key.colordifference", "Color Difference Key", "ფერთა სხვაობის გასაღები"), ("ec.key.colorkey", "Color Key", "ფერის გასაღები"), ("ec.key.colorrange", "Color Range", "ფერების დიაპაზონი"), @@ -1360,7 +1367,7 @@ const GEORGIAN: &[Row] = &[ ("ec.key.screen", "Screen Key", "ეკრანის გასაღები"), ("ec.key.spill", "Spill Suppressor", "ფერის გადაღვრის ჩამხშობი"), ("ec.key.unmult", "Unmult", "Unmult"), - ("ec.keying.keylight", "Key Light", "Key Light"), + ("ec.keying.keylight", "Screen Color Key", "ეკრანის ფერის გამოყოფა"), ("ec.matte.mattechoker", "Matte Choker", "მატის შემკუმშველი"), ("ec.matte.objectmatte", "Object Matte", "ობიექტის მატი"), ("ec.matte.refinehard", "Refine Hard Matte", "მკვეთრი მატის დახვეწა"), @@ -1384,50 +1391,50 @@ const GEORGIAN: &[Row] = &[ ("ec.obsolete.basictext", "Basic Text", "საბაზო ტექსტი"), ("ec.obsolete.gaussianlegacy", "Gaussian Blur (Legacy)", "გაუსის გაბუნდოვნება (მოძველებული)"), ("ec.obsolete.lightning", "Lightning", "ელვა"), - ("ec.obsolete.mochashape", "Mocha shape", "Mocha-ს ფიგურა"), + ("ec.obsolete.mochashape", "Tracked Shape Matte", "თვალთვალის ფორმის ნიღაბი"), ("ec.obsolete.pathtext", "Path Text", "ტექსტი ტრაექტორიაზე"), ("ec.paint.paint", "Paint", "ხატვა"), ("ec.perspective.3dglasses", "3D Glasses", "3D სათვალე"), ("ec.perspective.bevelalpha", "Bevel Alpha", "ალფას ფასკა"), ("ec.perspective.beveledges", "Bevel Edges", "კიდეების ფასკა"), ("ec.perspective.cameratracker", "3D Camera Tracker", "3D კამერის ტრეკერი"), - ("ec.perspective.cccylinder", "CC Cylinder", "CC Cylinder"), - ("ec.perspective.ccenvironment", "CC Environment", "CC Environment"), - ("ec.perspective.ccsphere", "CC Sphere", "CC Sphere"), - ("ec.perspective.ccspotlight", "CC Spotlight", "CC Spotlight"), + ("ec.perspective.cccylinder", "Cylinder Wrap", "ცილინდრზე შემოხვევა"), + ("ec.perspective.ccenvironment", "Environment Map", "გარემოს რუკა"), + ("ec.perspective.ccsphere", "Sphere Wrap", "სფეროზე შემოხვევა"), + ("ec.perspective.ccspotlight", "Surface Spotlight", "ზედაპირის პროჟექტორი"), ("ec.perspective.dropshadow", "Drop Shadow", "ჩრდილი"), ("ec.perspective.radialshadow", "Radial Shadow", "რადიალური ჩრდილი"), ("ec.sim.carddance", "Card Dance", "მოცეკვავე ბარათები"), ("ec.sim.caustics", "Caustics", "კაუსტიკა"), - ("ec.sim.ccballaction", "CC Ball Action", "CC Ball Action"), - ("ec.sim.ccbubbles", "CC Bubbles", "CC Bubbles"), - ("ec.sim.ccdrizzle", "CC Drizzle", "CC Drizzle"), - ("ec.sim.cchair", "CC Hair", "CC Hair"), - ("ec.sim.ccmrmercury", "CC Mr. Mercury", "CC Mr. Mercury"), - ("ec.sim.ccparticlesystems2", "CC Particle Systems II", "CC Particle Systems II"), - ("ec.sim.ccparticleworld", "CC Particle World", "CC Particle World"), - ("ec.sim.ccpixelpolly", "CC Pixel Polly", "CC Pixel Polly"), - ("ec.sim.ccrainfall", "CC Rainfall", "CC Rainfall"), - ("ec.sim.ccscatterize", "CC Scatterize", "CC Scatterize"), - ("ec.sim.ccsnowfall", "CC Snowfall", "CC Snowfall"), - ("ec.sim.ccstarburst", "CC Star Burst", "CC Star Burst"), + ("ec.sim.ccballaction", "Ball Grid", "ბურთულების ბადე"), + ("ec.sim.ccbubbles", "Bubbles", "ბუშტები"), + ("ec.sim.ccdrizzle", "Water Ripples", "წყლის ტალღები"), + ("ec.sim.cchair", "Surface Fibers", "ზედაპირის ბოჭკოები"), + ("ec.sim.ccmrmercury", "Liquid Particles", "თხევადი ნაწილაკები"), + ("ec.sim.ccparticlesystems2", "Particle Emitter 2D", "ნაწილაკების 2D წყარო"), + ("ec.sim.ccparticleworld", "Particle Emitter 3D", "ნაწილაკების 3D წყარო"), + ("ec.sim.ccpixelpolly", "Pixel Shatter", "პიქსელების დაშლა"), + ("ec.sim.ccrainfall", "Rainfall", "წვიმა"), + ("ec.sim.ccscatterize", "Pixel Scatter", "პიქსელების გაფანტვა"), + ("ec.sim.ccsnowfall", "Snowfall", "თოვა"), + ("ec.sim.ccstarburst", "Starfield", "ვარსკვლავების ველი"), ("ec.sim.foam", "Foam", "ქაფი"), ("ec.sim.particleplayground", "Particle Playground", "ნაწილაკების სიმულაცია"), ("ec.sim.shatter", "Shatter", "დამსხვრევა"), ("ec.sim.waveworld", "Wave World", "ტალღების სამყარო"), ("ec.stylize.brushstrokes", "Brush Strokes", "ფუნჯის შტრიხები"), ("ec.stylize.cartoon", "Cartoon", "მულტფილმის სტილი"), - ("ec.stylize.ccblockload", "CC Block Load", "CC Block Load"), - ("ec.stylize.ccburnfilm", "CC Burn Film", "CC Burn Film"), - ("ec.stylize.ccglass", "CC Glass", "CC Glass"), - ("ec.stylize.cchextile", "CC HexTile", "CC HexTile"), - ("ec.stylize.cckaleida", "CC Kaleida", "CC Kaleida"), - ("ec.stylize.ccmrsmoothie", "CC Mr. Smoothie", "CC Mr. Smoothie"), - ("ec.stylize.ccplastic", "CC Plastic", "CC Plastic"), - ("ec.stylize.ccrepetile", "CC RepeTile", "CC RepeTile"), - ("ec.stylize.ccthreshold", "CC Threshold", "CC Threshold"), - ("ec.stylize.ccthresholdrgb", "CC Threshold RGB", "CC Threshold RGB"), - ("ec.stylize.ccvignette", "CC Vignette", "CC Vignette"), + ("ec.stylize.ccblockload", "Block Reveal", "ბლოკებით გამოჩენა"), + ("ec.stylize.ccburnfilm", "Film Burn", "ფირის დაწვა"), + ("ec.stylize.ccglass", "Glass Relief", "მინის რელიეფი"), + ("ec.stylize.cchextile", "Hexagonal Tiles", "ექვსკუთხა ფილები"), + ("ec.stylize.cckaleida", "Kaleidoscope", "კალეიდოსკოპი"), + ("ec.stylize.ccmrsmoothie", "Palette Map", "პალიტრის რუკა"), + ("ec.stylize.ccplastic", "Plastic Shading", "პლასტმასის დაჩრდილვა"), + ("ec.stylize.ccrepetile", "Edge Tile Repeat", "კიდეების განმეორება"), + ("ec.stylize.ccthreshold", "Channel Threshold", "არხის ზღვარი"), + ("ec.stylize.ccthresholdrgb", "RGB Threshold", "RGB ზღვარი"), + ("ec.stylize.ccvignette", "Vignette", "ვინიეტირება"), ("ec.stylize.coloremboss", "Color Emboss", "ფერადი რელიეფი"), ("ec.stylize.emboss", "Emboss", "რელიეფი"), ("ec.stylize.findedges", "Find Edges", "კიდეების პოვნა"), @@ -1442,8 +1449,8 @@ const GEORGIAN: &[Row] = &[ ("ec.stylize.threshold", "Threshold", "ზღვარი"), ("ec.text.numbers", "Numbers", "რიცხვები"), ("ec.text.timecode", "Timecode", "თაიმკოდი"), - ("ec.time.ccforcemotionblur", "CC Force Motion Blur", "CC Force Motion Blur"), - ("ec.time.ccwidetime", "CC Wide Time", "CC Wide Time"), + ("ec.time.ccforcemotionblur", "Temporal Motion Blur", "დროითი მოძრაობის დაბინდვა"), + ("ec.time.ccwidetime", "Temporal Blend", "დროითი შერევა"), ("ec.time.echo", "Echo", "ექო"), ("ec.time.pixelmotionblur", "Pixel Motion Blur", "პიქსელების მოძრაობის გაბუნდოვნება"), ("ec.time.posterizetime", "Posterize Time", "დროის პოსტერიზაცია"), @@ -1452,23 +1459,23 @@ const GEORGIAN: &[Row] = &[ ("ec.time.timewarp", "Timewarp", "Timewarp"), ("ec.transition.blockdissolve", "Block Dissolve", "ბლოკური გადადნობა"), ("ec.transition.cardwipe", "Card Wipe", "ბარათებით წმენდა"), - ("ec.transition.ccglasswipe", "CC Glass Wipe", "CC Glass Wipe"), - ("ec.transition.ccgridwipe", "CC Grid Wipe", "CC Grid Wipe"), - ("ec.transition.ccimagewipe", "CC Image Wipe", "CC Image Wipe"), - ("ec.transition.ccjaws", "CC Jaws", "CC Jaws"), - ("ec.transition.cclightwipe", "CC Light Wipe", "CC Light Wipe"), - ("ec.transition.cclinesweep", "CC Line Sweep", "CC Line Sweep"), - ("ec.transition.ccradialscalewipe", "CC Radial ScaleWipe", "CC Radial ScaleWipe"), - ("ec.transition.ccscalewipe", "CC Scale Wipe", "CC Scale Wipe"), - ("ec.transition.cctwister", "CC Twister", "CC Twister"), - ("ec.transition.ccwarpomatic", "CC WarpoMatic", "CC WarpoMatic"), + ("ec.transition.ccglasswipe", "Glass Wipe", "მინის გადასვლა"), + ("ec.transition.ccgridwipe", "Grid Wipe", "ბადის გადასვლა"), + ("ec.transition.ccimagewipe", "Luminance Image Wipe", "სიკაშკაშის გამოსახულებით გადასვლა"), + ("ec.transition.ccjaws", "Serrated Wipe", "დაკბილული გადასვლა"), + ("ec.transition.cclightwipe", "Light Wipe", "სინათლის გადასვლა"), + ("ec.transition.cclinesweep", "Line Sweep", "ხაზების გავლა"), + ("ec.transition.ccradialscalewipe", "Radial Scale Wipe", "რადიალური მასშტაბის გადასვლა"), + ("ec.transition.ccscalewipe", "Scale Wipe", "მასშტაბის გადასვლა"), + ("ec.transition.cctwister", "Twist Wipe", "მოგრეხილი გადასვლა"), + ("ec.transition.ccwarpomatic", "Warp Dissolve", "დეფორმაციით გადასვლა"), ("ec.transition.gradientwipe", "Gradient Wipe", "გრადიენტული წმენდა"), ("ec.transition.iriswipe", "Iris Wipe", "დიაფრაგმული წმენდა"), ("ec.transition.linearwipe", "Linear Wipe", "წრფივი წმენდა"), ("ec.transition.radialwipe", "Radial Wipe", "რადიალური წმენდა"), ("ec.transition.venetian", "Venetian Blinds", "ჟალუზი"), ("ec.utility.applylut", "Apply Color LUT", "ფერის LUT-ის გამოყენება"), - ("ec.utility.ccoverbrights", "CC Overbrights", "CC Overbrights"), + ("ec.utility.ccoverbrights", "Out-of-Range Display", "დიაპაზონის გარეთ ჩვენება"), ("ec.utility.cineon", "Cineon Converter", "Cineon-ის გარდამქმნელი"), ("ec.utility.colorprofileconverter", "Color Profile Converter", "ფერის პროფილის გარდამქმნელი"), ("ec.utility.facemeasurements", "Face Measurements", "სახის გაზომვები"), @@ -1517,8 +1524,8 @@ const GEORGIAN_CATEGORIES: &[(&str, &str)] = &[ /// French (`fr`): one row per effect, ids from [`SIMPLIFIED_CHINESE`]. Written from the meaning of the /// English names, with the terms the French menus use (calque, estampe for a matte, incrustation for -/// keying). The `CC` series and the brand names stay in English, `VR`, `OCIO`, `PS` and `HDR` keep -/// their tag after the translated name. +/// keying). The brand names stay in English, `VR`, `OCIO`, `PS` and `HDR` keep their tag after the +/// translated name. const FRENCH: &[Row] = &[ ("ec.3d.channelextract", "3D Channel Extract", "Extraction de canal 3D"), ("ec.3d.cryptomatte", "Cryptomatte", "Cryptomatte"), @@ -1544,10 +1551,10 @@ const FRENCH: &[Row] = &[ ("ec.blur.bilateral", "Bilateral Blur", "Flou bilatéral"), ("ec.blur.cameralens", "Camera Lens Blur", "Flou de l’objectif de la caméra"), ("ec.blur.camerashakedeblur", "Camera-Shake Deblur", "Correction du flou de bougé de caméra"), - ("ec.blur.cccross", "CC Cross Blur", "CC Cross Blur"), - ("ec.blur.ccradial", "CC Radial Blur", "CC Radial Blur"), - ("ec.blur.ccradialfast", "CC Radial Fast Blur", "CC Radial Fast Blur"), - ("ec.blur.ccvector", "CC Vector Blur", "CC Vector Blur"), + ("ec.blur.cccross", "Cross Blur", "Flou croisé"), + ("ec.blur.ccradial", "Variable Radial Blur", "Flou radial variable"), + ("ec.blur.ccradialfast", "Fast Radial Blur", "Flou radial rapide"), + ("ec.blur.ccvector", "Vector Blur", "Flou vectoriel"), ("ec.blur.channel", "Channel Blur", "Flou de canal"), ("ec.blur.compound", "Compound Blur", "Flou composé"), ("ec.blur.directional", "Directional Blur", "Flou directionnel"), @@ -1561,7 +1568,7 @@ const FRENCH: &[Row] = &[ ("ec.channel.arithmetic", "Arithmetic", "Arithmétique"), ("ec.channel.blend", "Blend", "Fusion"), ("ec.channel.calculations", "Calculations", "Calculs"), - ("ec.channel.cccomposite", "CC Composite", "CC Composite"), + ("ec.channel.cccomposite", "Source Composite", "Composition de la source"), ("ec.channel.combiner", "Channel Combiner", "Combinateur de canaux"), ("ec.channel.compoundarithmetic", "Compound Arithmetic", "Arithmétique composée"), ("ec.channel.invert", "Invert", "Inversion"), @@ -1577,10 +1584,10 @@ const FRENCH: &[Row] = &[ ("ec.color.blackwhite", "Black & White", "Noir et blanc"), ("ec.color.brightnesscontrast", "Brightness & Contrast", "Luminosité et contraste"), ("ec.color.broadcast", "Broadcast Colors", "Couleurs de diffusion"), - ("ec.color.cccolorneutralizer", "CC Color Neutralizer", "CC Color Neutralizer"), - ("ec.color.cccoloroffset", "CC Color Offset", "CC Color Offset"), - ("ec.color.cckernel", "CC Kernel", "CC Kernel"), - ("ec.color.cctoner", "CC Toner", "CC Toner"), + ("ec.color.cccolorneutralizer", "Color Neutralizer", "Neutralisation des couleurs"), + ("ec.color.cccoloroffset", "Channel Offset", "Décalage des canaux"), + ("ec.color.cckernel", "Convolution Kernel", "Noyau de convolution"), + ("ec.color.cctoner", "Multi-Tone Tint", "Teinte multiton"), ("ec.color.changecolor", "Change Color", "Changer la couleur"), ("ec.color.changetocolor", "Change to Color", "Changer en couleur"), ("ec.color.channelmixer", "Channel Mixer", "Mélangeur de canaux"), @@ -1597,7 +1604,7 @@ const FRENCH: &[Row] = &[ ("ec.color.leavecolor", "Leave Color", "Conserver la couleur"), ("ec.color.levels", "Levels", "Niveaux"), ("ec.color.levelsic", "Levels (Individual Controls)", "Niveaux (contrôles individuels)"), - ("ec.color.lumetri", "Lumetri Color", "Lumetri Color"), + ("ec.color.lumetri", "Color Grading", "Étalonnage des couleurs"), ("ec.color.ociocdl", "OCIO CDL Transform", "Transformation CDL OCIO"), ("ec.color.ociocolorspace", "OCIO Color Space Transform", "Transformation d’espace colorimétrique OCIO"), ("ec.color.ociodisplay", "OCIO Display Transform", "Transformation d’affichage OCIO"), @@ -1622,20 +1629,20 @@ const FRENCH: &[Row] = &[ ("ec.control.stereo3d", "Stereo 3D Controls", "Contrôles stéréo 3D"), ("ec.distort.bezierwarp", "Bezier Warp", "Déformation Bézier"), ("ec.distort.bulge", "Bulge", "Renflement"), - ("ec.distort.ccbender", "CC Bender", "CC Bender"), - ("ec.distort.ccbendit", "CC Bend It", "CC Bend It"), - ("ec.distort.ccblobbylize", "CC Blobbylize", "CC Blobbylize"), - ("ec.distort.ccflomotion", "CC Flo Motion", "CC Flo Motion"), - ("ec.distort.ccgriddler", "CC Griddler", "CC Griddler"), - ("ec.distort.cclens", "CC Lens", "CC Lens"), - ("ec.distort.ccpageturn", "CC Page Turn", "CC Page Turn"), - ("ec.distort.ccpowerpin", "CC Power Pin", "CC Power Pin"), - ("ec.distort.ccripplepulse", "CC Ripple Pulse", "CC Ripple Pulse"), - ("ec.distort.ccslant", "CC Slant", "CC Slant"), - ("ec.distort.ccsmear", "CC Smear", "CC Smear"), - ("ec.distort.ccsplit", "CC Split", "CC Split"), - ("ec.distort.ccsplit2", "CC Split 2", "CC Split 2"), - ("ec.distort.cctiler", "CC Tiler", "CC Tiler"), + ("ec.distort.ccbender", "Flexible Bend", "Courbure souple"), + ("ec.distort.ccbendit", "Two-Point Bend", "Courbure à deux points"), + ("ec.distort.ccblobbylize", "Organic Displacement", "Déplacement organique"), + ("ec.distort.ccflomotion", "Dual Lens Warp", "Déformation à deux lentilles"), + ("ec.distort.ccgriddler", "Tile Grid", "Grille de tuiles"), + ("ec.distort.cclens", "Lens Distortion", "Distorsion de lentille"), + ("ec.distort.ccpageturn", "Page Curl", "Page enroulée"), + ("ec.distort.ccpowerpin", "Perspective Pin", "Épinglage en perspective"), + ("ec.distort.ccripplepulse", "Pulsed Ripple", "Ondulation pulsée"), + ("ec.distort.ccslant", "Slant", "Inclinaison"), + ("ec.distort.ccsmear", "Directional Smear", "Étalement directionnel"), + ("ec.distort.ccsplit", "Split Warp", "Déformation divisée"), + ("ec.distort.ccsplit2", "Dual Split Warp", "Déformation à double division"), + ("ec.distort.cctiler", "Scaled Tiles", "Tuiles mises à l’échelle"), ("ec.distort.cornerpin", "Corner Pin", "Épinglage d’angles"), ("ec.distort.displacementmap", "Displacement Map", "Carte de déplacement"), ("ec.distort.liquify", "Liquify", "Liquéfier"), @@ -1663,11 +1670,11 @@ const FRENCH: &[Row] = &[ ("ec.generate.audiospectrum", "Audio Spectrum", "Spectre audio"), ("ec.generate.audiowaveform", "Audio Waveform", "Forme d’onde audio"), ("ec.generate.beam", "Beam", "Faisceau"), - ("ec.generate.ccgluegun", "CC Glue Gun", "CC Glue Gun"), - ("ec.generate.cclightburst", "CC Light Burst 2.5", "CC Light Burst 2.5"), - ("ec.generate.cclightrays", "CC Light Rays", "CC Light Rays"), - ("ec.generate.cclightsweep", "CC Light Sweep", "CC Light Sweep"), - ("ec.generate.ccthreads", "CC Threads", "CC Threads"), + ("ec.generate.ccgluegun", "Paint Beads", "Perles de peinture"), + ("ec.generate.cclightburst", "Radial Light Burst", "Éclat lumineux radial"), + ("ec.generate.cclightrays", "Light Rays", "Rayons lumineux"), + ("ec.generate.cclightsweep", "Light Sweep", "Balayage lumineux"), + ("ec.generate.ccthreads", "Woven Threads", "Fils tissés"), ("ec.generate.cellpattern", "Cell Pattern", "Motif cellulaire"), ("ec.generate.checkerboard", "Checkerboard", "Damier"), ("ec.generate.circle", "Circle", "Cercle"), @@ -1686,7 +1693,7 @@ const FRENCH: &[Row] = &[ ("ec.generate.vegas", "Vegas", "Vegas"), ("ec.generate.writeon", "Write-on", "Écriture progressive"), ("ec.key.advancedspill", "Advanced Spill Suppressor", "Suppresseur de débordement avancé"), - ("ec.key.ccsimplewireremoval", "CC Simple Wire Removal", "CC Simple Wire Removal"), + ("ec.key.ccsimplewireremoval", "Line Removal", "Suppression de ligne"), ("ec.key.colordifference", "Color Difference Key", "Incrustation par différence de couleur"), ("ec.key.colorkey", "Color Key", "Incrustation couleur"), ("ec.key.colorrange", "Color Range", "Plage de couleurs"), @@ -1699,7 +1706,7 @@ const FRENCH: &[Row] = &[ ("ec.key.screen", "Screen Key", "Incrustation d’écran"), ("ec.key.spill", "Spill Suppressor", "Suppresseur de débordement"), ("ec.key.unmult", "Unmult", "Unmult"), - ("ec.keying.keylight", "Key Light", "Key Light"), + ("ec.keying.keylight", "Screen Color Key", "Incrustation par couleur d’écran"), ("ec.matte.mattechoker", "Matte Choker", "Réducteur d’estampe"), ("ec.matte.objectmatte", "Object Matte", "Estampe d’objet"), ("ec.matte.refinehard", "Refine Hard Matte", "Affiner l’estampe nette"), @@ -1723,50 +1730,50 @@ const FRENCH: &[Row] = &[ ("ec.obsolete.basictext", "Basic Text", "Texte de base"), ("ec.obsolete.gaussianlegacy", "Gaussian Blur (Legacy)", "Flou gaussien (hérité)"), ("ec.obsolete.lightning", "Lightning", "Foudre"), - ("ec.obsolete.mochashape", "Mocha shape", "Forme Mocha"), + ("ec.obsolete.mochashape", "Tracked Shape Matte", "Cache de forme suivie"), ("ec.obsolete.pathtext", "Path Text", "Texte sur tracé"), ("ec.paint.paint", "Paint", "Peinture"), ("ec.perspective.3dglasses", "3D Glasses", "Lunettes 3D"), ("ec.perspective.bevelalpha", "Bevel Alpha", "Biseau alpha"), ("ec.perspective.beveledges", "Bevel Edges", "Biseau des bords"), ("ec.perspective.cameratracker", "3D Camera Tracker", "Suivi de caméra 3D"), - ("ec.perspective.cccylinder", "CC Cylinder", "CC Cylinder"), - ("ec.perspective.ccenvironment", "CC Environment", "CC Environment"), - ("ec.perspective.ccsphere", "CC Sphere", "CC Sphere"), - ("ec.perspective.ccspotlight", "CC Spotlight", "CC Spotlight"), + ("ec.perspective.cccylinder", "Cylinder Wrap", "Enroulement cylindrique"), + ("ec.perspective.ccenvironment", "Environment Map", "Carte d’environnement"), + ("ec.perspective.ccsphere", "Sphere Wrap", "Enroulement sphérique"), + ("ec.perspective.ccspotlight", "Surface Spotlight", "Projecteur de surface"), ("ec.perspective.dropshadow", "Drop Shadow", "Ombre portée"), ("ec.perspective.radialshadow", "Radial Shadow", "Ombre radiale"), ("ec.sim.carddance", "Card Dance", "Danse de cartes"), ("ec.sim.caustics", "Caustics", "Caustiques"), - ("ec.sim.ccballaction", "CC Ball Action", "CC Ball Action"), - ("ec.sim.ccbubbles", "CC Bubbles", "CC Bubbles"), - ("ec.sim.ccdrizzle", "CC Drizzle", "CC Drizzle"), - ("ec.sim.cchair", "CC Hair", "CC Hair"), - ("ec.sim.ccmrmercury", "CC Mr. Mercury", "CC Mr. Mercury"), - ("ec.sim.ccparticlesystems2", "CC Particle Systems II", "CC Particle Systems II"), - ("ec.sim.ccparticleworld", "CC Particle World", "CC Particle World"), - ("ec.sim.ccpixelpolly", "CC Pixel Polly", "CC Pixel Polly"), - ("ec.sim.ccrainfall", "CC Rainfall", "CC Rainfall"), - ("ec.sim.ccscatterize", "CC Scatterize", "CC Scatterize"), - ("ec.sim.ccsnowfall", "CC Snowfall", "CC Snowfall"), - ("ec.sim.ccstarburst", "CC Star Burst", "CC Star Burst"), + ("ec.sim.ccballaction", "Ball Grid", "Grille de sphères"), + ("ec.sim.ccbubbles", "Bubbles", "Bulles"), + ("ec.sim.ccdrizzle", "Water Ripples", "Ondulations de l’eau"), + ("ec.sim.cchair", "Surface Fibers", "Fibres de surface"), + ("ec.sim.ccmrmercury", "Liquid Particles", "Particules liquides"), + ("ec.sim.ccparticlesystems2", "Particle Emitter 2D", "Émetteur de particules 2D"), + ("ec.sim.ccparticleworld", "Particle Emitter 3D", "Émetteur de particules 3D"), + ("ec.sim.ccpixelpolly", "Pixel Shatter", "Éclatement des pixels"), + ("ec.sim.ccrainfall", "Rainfall", "Pluie"), + ("ec.sim.ccscatterize", "Pixel Scatter", "Dispersion des pixels"), + ("ec.sim.ccsnowfall", "Snowfall", "Neige"), + ("ec.sim.ccstarburst", "Starfield", "Champ d’étoiles"), ("ec.sim.foam", "Foam", "Mousse"), ("ec.sim.particleplayground", "Particle Playground", "Terrain de jeu de particules"), ("ec.sim.shatter", "Shatter", "Éclatement"), ("ec.sim.waveworld", "Wave World", "Monde des vagues"), ("ec.stylize.brushstrokes", "Brush Strokes", "Coups de pinceau"), ("ec.stylize.cartoon", "Cartoon", "Dessin animé"), - ("ec.stylize.ccblockload", "CC Block Load", "CC Block Load"), - ("ec.stylize.ccburnfilm", "CC Burn Film", "CC Burn Film"), - ("ec.stylize.ccglass", "CC Glass", "CC Glass"), - ("ec.stylize.cchextile", "CC HexTile", "CC HexTile"), - ("ec.stylize.cckaleida", "CC Kaleida", "CC Kaleida"), - ("ec.stylize.ccmrsmoothie", "CC Mr. Smoothie", "CC Mr. Smoothie"), - ("ec.stylize.ccplastic", "CC Plastic", "CC Plastic"), - ("ec.stylize.ccrepetile", "CC RepeTile", "CC RepeTile"), - ("ec.stylize.ccthreshold", "CC Threshold", "CC Threshold"), - ("ec.stylize.ccthresholdrgb", "CC Threshold RGB", "CC Threshold RGB"), - ("ec.stylize.ccvignette", "CC Vignette", "CC Vignette"), + ("ec.stylize.ccblockload", "Block Reveal", "Révélation par blocs"), + ("ec.stylize.ccburnfilm", "Film Burn", "Brûlure de pellicule"), + ("ec.stylize.ccglass", "Glass Relief", "Relief de verre"), + ("ec.stylize.cchextile", "Hexagonal Tiles", "Tuiles hexagonales"), + ("ec.stylize.cckaleida", "Kaleidoscope", "Kaléidoscope"), + ("ec.stylize.ccmrsmoothie", "Palette Map", "Palettmappning"), + ("ec.stylize.ccplastic", "Plastic Shading", "Ombrage plastique"), + ("ec.stylize.ccrepetile", "Edge Tile Repeat", "Répétition des tuiles aux bords"), + ("ec.stylize.ccthreshold", "Channel Threshold", "Seuil de canal"), + ("ec.stylize.ccthresholdrgb", "RGB Threshold", "Seuil RVB"), + ("ec.stylize.ccvignette", "Vignette", "Vignettage"), ("ec.stylize.coloremboss", "Color Emboss", "Estampage couleur"), ("ec.stylize.emboss", "Emboss", "Estampage"), ("ec.stylize.findedges", "Find Edges", "Détection des contours"), @@ -1781,8 +1788,8 @@ const FRENCH: &[Row] = &[ ("ec.stylize.threshold", "Threshold", "Seuil"), ("ec.text.numbers", "Numbers", "Nombres"), ("ec.text.timecode", "Timecode", "Timecode"), - ("ec.time.ccforcemotionblur", "CC Force Motion Blur", "CC Force Motion Blur"), - ("ec.time.ccwidetime", "CC Wide Time", "CC Wide Time"), + ("ec.time.ccforcemotionblur", "Temporal Motion Blur", "Flou de mouvement temporel"), + ("ec.time.ccwidetime", "Temporal Blend", "Fusion temporelle"), ("ec.time.echo", "Echo", "Écho"), ("ec.time.pixelmotionblur", "Pixel Motion Blur", "Flou de mouvement des pixels"), ("ec.time.posterizetime", "Posterize Time", "Postérisation temporelle"), @@ -1791,23 +1798,23 @@ const FRENCH: &[Row] = &[ ("ec.time.timewarp", "Timewarp", "Déformation temporelle"), ("ec.transition.blockdissolve", "Block Dissolve", "Fondu par blocs"), ("ec.transition.cardwipe", "Card Wipe", "Volet de cartes"), - ("ec.transition.ccglasswipe", "CC Glass Wipe", "CC Glass Wipe"), - ("ec.transition.ccgridwipe", "CC Grid Wipe", "CC Grid Wipe"), - ("ec.transition.ccimagewipe", "CC Image Wipe", "CC Image Wipe"), - ("ec.transition.ccjaws", "CC Jaws", "CC Jaws"), - ("ec.transition.cclightwipe", "CC Light Wipe", "CC Light Wipe"), - ("ec.transition.cclinesweep", "CC Line Sweep", "CC Line Sweep"), - ("ec.transition.ccradialscalewipe", "CC Radial ScaleWipe", "CC Radial ScaleWipe"), - ("ec.transition.ccscalewipe", "CC Scale Wipe", "CC Scale Wipe"), - ("ec.transition.cctwister", "CC Twister", "CC Twister"), - ("ec.transition.ccwarpomatic", "CC WarpoMatic", "CC WarpoMatic"), + ("ec.transition.ccglasswipe", "Glass Wipe", "Volet de verre"), + ("ec.transition.ccgridwipe", "Grid Wipe", "Volet en grille"), + ("ec.transition.ccimagewipe", "Luminance Image Wipe", "Volet par luminance d’image"), + ("ec.transition.ccjaws", "Serrated Wipe", "Volet dentelé"), + ("ec.transition.cclightwipe", "Light Wipe", "Volet lumineux"), + ("ec.transition.cclinesweep", "Line Sweep", "Balayage de lignes"), + ("ec.transition.ccradialscalewipe", "Radial Scale Wipe", "Volet d’échelle radial"), + ("ec.transition.ccscalewipe", "Scale Wipe", "Volet d’échelle"), + ("ec.transition.cctwister", "Twist Wipe", "Volet torsadé"), + ("ec.transition.ccwarpomatic", "Warp Dissolve", "Fondu déformant"), ("ec.transition.gradientwipe", "Gradient Wipe", "Volet en dégradé"), ("ec.transition.iriswipe", "Iris Wipe", "Volet en iris"), ("ec.transition.linearwipe", "Linear Wipe", "Volet linéaire"), ("ec.transition.radialwipe", "Radial Wipe", "Volet radial"), ("ec.transition.venetian", "Venetian Blinds", "Stores vénitiens"), ("ec.utility.applylut", "Apply Color LUT", "Appliquer une LUT de couleur"), - ("ec.utility.ccoverbrights", "CC Overbrights", "CC Overbrights"), + ("ec.utility.ccoverbrights", "Out-of-Range Display", "Affichage hors plage"), ("ec.utility.cineon", "Cineon Converter", "Convertisseur Cineon"), ("ec.utility.colorprofileconverter", "Color Profile Converter", "Convertisseur de profil colorimétrique"), ("ec.utility.facemeasurements", "Face Measurements", "Mesures du visage"), @@ -1909,10 +1916,10 @@ const SWEDISH: &[Row] = &[ ("ec.blur.bilateral", "Bilateral Blur", "Bilateral oskärpa"), ("ec.blur.cameralens", "Camera Lens Blur", "Kameralinsoskärpa"), ("ec.blur.camerashakedeblur", "Camera-Shake Deblur", "Ta bort oskärpa från kameraskakning"), - ("ec.blur.cccross", "CC Cross Blur", "CC Korsoskärpa"), - ("ec.blur.ccradial", "CC Radial Blur", "CC Radiell oskärpa"), - ("ec.blur.ccradialfast", "CC Radial Fast Blur", "CC Snabb radiell oskärpa"), - ("ec.blur.ccvector", "CC Vector Blur", "CC Vektoroskärpa"), + ("ec.blur.cccross", "Cross Blur", "Korsoskärpa"), + ("ec.blur.ccradial", "Variable Radial Blur", "Variabel radiell oskärpa"), + ("ec.blur.ccradialfast", "Fast Radial Blur", "Snabb radiell oskärpa"), + ("ec.blur.ccvector", "Vector Blur", "Vektoroskärpa"), ("ec.blur.channel", "Channel Blur", "Kanaloskärpa"), ("ec.blur.compound", "Compound Blur", "Sammansatt oskärpa"), ("ec.blur.directional", "Directional Blur", "Riktad oskärpa"), @@ -1926,7 +1933,7 @@ const SWEDISH: &[Row] = &[ ("ec.channel.arithmetic", "Arithmetic", "Aritmetik"), ("ec.channel.blend", "Blend", "Blanda"), ("ec.channel.calculations", "Calculations", "Beräkningar"), - ("ec.channel.cccomposite", "CC Composite", "CC Komposit"), + ("ec.channel.cccomposite", "Source Composite", "Källkomposition"), ("ec.channel.combiner", "Channel Combiner", "Kanalkombinerare"), ("ec.channel.compoundarithmetic", "Compound Arithmetic", "Sammansatt aritmetik"), ("ec.channel.invert", "Invert", "Invertera"), @@ -1942,10 +1949,10 @@ const SWEDISH: &[Row] = &[ ("ec.color.blackwhite", "Black & White", "Svartvitt"), ("ec.color.brightnesscontrast", "Brightness & Contrast", "Ljusstyrka och kontrast"), ("ec.color.broadcast", "Broadcast Colors", "Broadcast-färger"), - ("ec.color.cccolorneutralizer", "CC Color Neutralizer", "CC Färgneutraliserare"), - ("ec.color.cccoloroffset", "CC Color Offset", "CC Färgförskjutning"), - ("ec.color.cckernel", "CC Kernel", "CC Kärna"), - ("ec.color.cctoner", "CC Toner", "CC Toner"), + ("ec.color.cccolorneutralizer", "Color Neutralizer", "Färgneutralisering"), + ("ec.color.cccoloroffset", "Channel Offset", "Kanalförskjutning"), + ("ec.color.cckernel", "Convolution Kernel", "Faltningskärna"), + ("ec.color.cctoner", "Multi-Tone Tint", "Flertonsfärgning"), ("ec.color.changecolor", "Change Color", "Ändra färg"), ("ec.color.changetocolor", "Change to Color", "Ändra till färg"), ("ec.color.channelmixer", "Channel Mixer", "Kanalmixer"), @@ -1962,7 +1969,7 @@ const SWEDISH: &[Row] = &[ ("ec.color.leavecolor", "Leave Color", "Behåll färg"), ("ec.color.levels", "Levels", "Nivåer"), ("ec.color.levelsic", "Levels (Individual Controls)", "Nivåer (individuella kontroller)"), - ("ec.color.lumetri", "Lumetri Color", "Lumetri-färg"), + ("ec.color.lumetri", "Color Grading", "Färggradering"), ("ec.color.ociocdl", "OCIO CDL Transform", "OCIO CDL-transformering"), ("ec.color.ociocolorspace", "OCIO Color Space Transform", "OCIO färgrymdstransformering"), ("ec.color.ociodisplay", "OCIO Display Transform", "OCIO visningstransformering"), @@ -1987,20 +1994,20 @@ const SWEDISH: &[Row] = &[ ("ec.control.stereo3d", "Stereo 3D Controls", "Stereo 3D-kontroller"), ("ec.distort.bezierwarp", "Bezier Warp", "Bezierförvrängning"), ("ec.distort.bulge", "Bulge", "Utbuktning"), - ("ec.distort.ccbender", "CC Bender", "CC Bender"), - ("ec.distort.ccbendit", "CC Bend It", "CC Bend It"), - ("ec.distort.ccblobbylize", "CC Blobbylize", "CC Blobbylize"), - ("ec.distort.ccflomotion", "CC Flo Motion", "CC Flo Motion"), - ("ec.distort.ccgriddler", "CC Griddler", "CC Griddler"), - ("ec.distort.cclens", "CC Lens", "CC Objektiv"), - ("ec.distort.ccpageturn", "CC Page Turn", "CC Sidvändning"), - ("ec.distort.ccpowerpin", "CC Power Pin", "CC Power Pin"), - ("ec.distort.ccripplepulse", "CC Ripple Pulse", "CC Krusningspuls"), - ("ec.distort.ccslant", "CC Slant", "CC Lutning"), - ("ec.distort.ccsmear", "CC Smear", "CC Utsmetning"), - ("ec.distort.ccsplit", "CC Split", "CC Dela"), - ("ec.distort.ccsplit2", "CC Split 2", "CC Dela 2"), - ("ec.distort.cctiler", "CC Tiler", "CC Tiler"), + ("ec.distort.ccbender", "Flexible Bend", "Flexibel böjning"), + ("ec.distort.ccbendit", "Two-Point Bend", "Tvåpunktsböjning"), + ("ec.distort.ccblobbylize", "Organic Displacement", "Organisk förskjutning"), + ("ec.distort.ccflomotion", "Dual Lens Warp", "Dubbel linsförvrängning"), + ("ec.distort.ccgriddler", "Tile Grid", "Plattnät"), + ("ec.distort.cclens", "Lens Distortion", "Linsförvrängning"), + ("ec.distort.ccpageturn", "Page Curl", "Sidkrullning"), + ("ec.distort.ccpowerpin", "Perspective Pin", "Perspektivnålar"), + ("ec.distort.ccripplepulse", "Pulsed Ripple", "Pulserande krusning"), + ("ec.distort.ccslant", "Slant", "Lutning"), + ("ec.distort.ccsmear", "Directional Smear", "Riktningssmetning"), + ("ec.distort.ccsplit", "Split Warp", "Delad förvrängning"), + ("ec.distort.ccsplit2", "Dual Split Warp", "Dubbeldelad förvrängning"), + ("ec.distort.cctiler", "Scaled Tiles", "Skalade plattor"), ("ec.distort.cornerpin", "Corner Pin", "Hörnförankring"), ("ec.distort.displacementmap", "Displacement Map", "Förskjutningskarta"), ("ec.distort.liquify", "Liquify", "Gör flytande"), @@ -2028,11 +2035,11 @@ const SWEDISH: &[Row] = &[ ("ec.generate.audiospectrum", "Audio Spectrum", "Ljudspektrum"), ("ec.generate.audiowaveform", "Audio Waveform", "Ljudvågform"), ("ec.generate.beam", "Beam", "Stråle"), - ("ec.generate.ccgluegun", "CC Glue Gun", "CC Glue Gun"), - ("ec.generate.cclightburst", "CC Light Burst 2.5", "CC Ljusutbrott 2.5"), - ("ec.generate.cclightrays", "CC Light Rays", "CC Ljusstrålar"), - ("ec.generate.cclightsweep", "CC Light Sweep", "CC Ljussvep"), - ("ec.generate.ccthreads", "CC Threads", "CC Trådar"), + ("ec.generate.ccgluegun", "Paint Beads", "Färgpärlor"), + ("ec.generate.cclightburst", "Radial Light Burst", "Radiellt ljusutbrott"), + ("ec.generate.cclightrays", "Light Rays", "Ljusstrålar"), + ("ec.generate.cclightsweep", "Light Sweep", "Ljussvep"), + ("ec.generate.ccthreads", "Woven Threads", "Vävda trådar"), ("ec.generate.cellpattern", "Cell Pattern", "Cellmönster"), ("ec.generate.checkerboard", "Checkerboard", "Schackbräde"), ("ec.generate.circle", "Circle", "Cirkel"), @@ -2051,7 +2058,7 @@ const SWEDISH: &[Row] = &[ ("ec.generate.vegas", "Vegas", "Vegas"), ("ec.generate.writeon", "Write-on", "Framskrivning"), ("ec.key.advancedspill", "Advanced Spill Suppressor", "Avancerad spilldämpning"), - ("ec.key.ccsimplewireremoval", "CC Simple Wire Removal", "CC Enkel trådborttagning"), + ("ec.key.ccsimplewireremoval", "Line Removal", "Linjeborttagning"), ("ec.key.colordifference", "Color Difference Key", "Färgdifferensnyckel"), ("ec.key.colorkey", "Color Key", "Färgnyckel"), ("ec.key.colorrange", "Color Range", "Färgomfång"), @@ -2064,7 +2071,7 @@ const SWEDISH: &[Row] = &[ ("ec.key.screen", "Screen Key", "Skärmnyckel"), ("ec.key.spill", "Spill Suppressor", "Spilldämpning"), ("ec.key.unmult", "Unmult", "Unmult"), - ("ec.keying.keylight", "Key Light", "Key Light"), + ("ec.keying.keylight", "Screen Color Key", "Skärmfärgsnyckling"), ("ec.matte.mattechoker", "Matte Choker", "Mattekrympning"), ("ec.matte.objectmatte", "Object Matte", "Objektmatte"), ("ec.matte.refinehard", "Refine Hard Matte", "Förfina hård matte"), @@ -2088,50 +2095,50 @@ const SWEDISH: &[Row] = &[ ("ec.obsolete.basictext", "Basic Text", "Grundläggande text"), ("ec.obsolete.gaussianlegacy", "Gaussian Blur (Legacy)", "Gaussisk oskärpa (äldre)"), ("ec.obsolete.lightning", "Lightning", "Blixt"), - ("ec.obsolete.mochashape", "Mocha shape", "Mocha-form"), + ("ec.obsolete.mochashape", "Tracked Shape Matte", "Spårad formmask"), ("ec.obsolete.pathtext", "Path Text", "Text längs bana"), ("ec.paint.paint", "Paint", "Måla"), ("ec.perspective.3dglasses", "3D Glasses", "3D-glasögon"), ("ec.perspective.bevelalpha", "Bevel Alpha", "Alfafasning"), ("ec.perspective.beveledges", "Bevel Edges", "Fasade kanter"), ("ec.perspective.cameratracker", "3D Camera Tracker", "3D-kameraspårning"), - ("ec.perspective.cccylinder", "CC Cylinder", "CC Cylinder"), - ("ec.perspective.ccenvironment", "CC Environment", "CC Miljö"), - ("ec.perspective.ccsphere", "CC Sphere", "CC Sfär"), - ("ec.perspective.ccspotlight", "CC Spotlight", "CC Strålkastare"), + ("ec.perspective.cccylinder", "Cylinder Wrap", "Cylinderomslag"), + ("ec.perspective.ccenvironment", "Environment Map", "Miljökarta"), + ("ec.perspective.ccsphere", "Sphere Wrap", "Sfäromslag"), + ("ec.perspective.ccspotlight", "Surface Spotlight", "Ytstrålkastare"), ("ec.perspective.dropshadow", "Drop Shadow", "Slagskugga"), ("ec.perspective.radialshadow", "Radial Shadow", "Radiell skugga"), ("ec.sim.carddance", "Card Dance", "Kortdans"), ("ec.sim.caustics", "Caustics", "Kaustik"), - ("ec.sim.ccballaction", "CC Ball Action", "CC Bollåtgärd"), - ("ec.sim.ccbubbles", "CC Bubbles", "CC Bubblor"), - ("ec.sim.ccdrizzle", "CC Drizzle", "CC Duggregn"), - ("ec.sim.cchair", "CC Hair", "CC Hår"), - ("ec.sim.ccmrmercury", "CC Mr. Mercury", "CC Mr. Mercury"), - ("ec.sim.ccparticlesystems2", "CC Particle Systems II", "CC Partikelsystem II"), - ("ec.sim.ccparticleworld", "CC Particle World", "CC Partikelvärld"), - ("ec.sim.ccpixelpolly", "CC Pixel Polly", "CC Pixel Polly"), - ("ec.sim.ccrainfall", "CC Rainfall", "CC Regn"), - ("ec.sim.ccscatterize", "CC Scatterize", "CC Utspridning"), - ("ec.sim.ccsnowfall", "CC Snowfall", "CC Snöfall"), - ("ec.sim.ccstarburst", "CC Star Burst", "CC Stjärnexplosion"), + ("ec.sim.ccballaction", "Ball Grid", "Kulnät"), + ("ec.sim.ccbubbles", "Bubbles", "Bubblor"), + ("ec.sim.ccdrizzle", "Water Ripples", "Vattenkrusningar"), + ("ec.sim.cchair", "Surface Fibers", "Ytfibrer"), + ("ec.sim.ccmrmercury", "Liquid Particles", "Vätskepartiklar"), + ("ec.sim.ccparticlesystems2", "Particle Emitter 2D", "Partikelsändare 2D"), + ("ec.sim.ccparticleworld", "Particle Emitter 3D", "Partikelsändare 3D"), + ("ec.sim.ccpixelpolly", "Pixel Shatter", "Pixelsplittring"), + ("ec.sim.ccrainfall", "Rainfall", "Regn"), + ("ec.sim.ccscatterize", "Pixel Scatter", "Pixelspridning"), + ("ec.sim.ccsnowfall", "Snowfall", "Snöfall"), + ("ec.sim.ccstarburst", "Starfield", "Stjärnfält"), ("ec.sim.foam", "Foam", "Skum"), ("ec.sim.particleplayground", "Particle Playground", "Partikellekplats"), ("ec.sim.shatter", "Shatter", "Krossa"), ("ec.sim.waveworld", "Wave World", "Vågvärld"), ("ec.stylize.brushstrokes", "Brush Strokes", "Penseldrag"), ("ec.stylize.cartoon", "Cartoon", "Tecknat"), - ("ec.stylize.ccblockload", "CC Block Load", "CC Blockladdning"), - ("ec.stylize.ccburnfilm", "CC Burn Film", "CC Bränn film"), - ("ec.stylize.ccglass", "CC Glass", "CC Glas"), - ("ec.stylize.cchextile", "CC HexTile", "CC HexTile"), - ("ec.stylize.cckaleida", "CC Kaleida", "CC Kaleida"), - ("ec.stylize.ccmrsmoothie", "CC Mr. Smoothie", "CC Mr. Smoothie"), - ("ec.stylize.ccplastic", "CC Plastic", "CC Plast"), - ("ec.stylize.ccrepetile", "CC RepeTile", "CC RepeTile"), - ("ec.stylize.ccthreshold", "CC Threshold", "CC Tröskel"), - ("ec.stylize.ccthresholdrgb", "CC Threshold RGB", "CC Tröskel RGB"), - ("ec.stylize.ccvignette", "CC Vignette", "CC Vinjett"), + ("ec.stylize.ccblockload", "Block Reveal", "Blockframträdande"), + ("ec.stylize.ccburnfilm", "Film Burn", "Filmbränning"), + ("ec.stylize.ccglass", "Glass Relief", "Glasrelief"), + ("ec.stylize.cchextile", "Hexagonal Tiles", "Sexkantiga plattor"), + ("ec.stylize.cckaleida", "Kaleidoscope", "Kalejdoskop"), + ("ec.stylize.ccmrsmoothie", "Palette Map", "Mappage de palette"), + ("ec.stylize.ccplastic", "Plastic Shading", "Plastskuggning"), + ("ec.stylize.ccrepetile", "Edge Tile Repeat", "Kantupprepning"), + ("ec.stylize.ccthreshold", "Channel Threshold", "Kanaltröskel"), + ("ec.stylize.ccthresholdrgb", "RGB Threshold", "RGB-tröskel"), + ("ec.stylize.ccvignette", "Vignette", "Vinjettering"), ("ec.stylize.coloremboss", "Color Emboss", "Färgrelief"), ("ec.stylize.emboss", "Emboss", "Relief"), ("ec.stylize.findedges", "Find Edges", "Hitta kanter"), @@ -2146,8 +2153,8 @@ const SWEDISH: &[Row] = &[ ("ec.stylize.threshold", "Threshold", "Tröskel"), ("ec.text.numbers", "Numbers", "Siffror"), ("ec.text.timecode", "Timecode", "Tidskod"), - ("ec.time.ccforcemotionblur", "CC Force Motion Blur", "CC Tvinga rörelseoskärpa"), - ("ec.time.ccwidetime", "CC Wide Time", "CC Bred tid"), + ("ec.time.ccforcemotionblur", "Temporal Motion Blur", "Tidsbaserad rörelseoskärpa"), + ("ec.time.ccwidetime", "Temporal Blend", "Tidsblandning"), ("ec.time.echo", "Echo", "Eko"), ("ec.time.pixelmotionblur", "Pixel Motion Blur", "Pixelrörelseoskärpa"), ("ec.time.posterizetime", "Posterize Time", "Posterisera tid"), @@ -2156,23 +2163,23 @@ const SWEDISH: &[Row] = &[ ("ec.time.timewarp", "Timewarp", "Tidsförvrängning"), ("ec.transition.blockdissolve", "Block Dissolve", "Blockövertoning"), ("ec.transition.cardwipe", "Card Wipe", "Kortridå"), - ("ec.transition.ccglasswipe", "CC Glass Wipe", "CC Glasridå"), - ("ec.transition.ccgridwipe", "CC Grid Wipe", "CC Rutnätsridå"), - ("ec.transition.ccimagewipe", "CC Image Wipe", "CC Bildridå"), - ("ec.transition.ccjaws", "CC Jaws", "CC Käftar"), - ("ec.transition.cclightwipe", "CC Light Wipe", "CC Ljusridå"), - ("ec.transition.cclinesweep", "CC Line Sweep", "CC Linjesvep"), - ("ec.transition.ccradialscalewipe", "CC Radial ScaleWipe", "CC Radiell skalridå"), - ("ec.transition.ccscalewipe", "CC Scale Wipe", "CC Skalridå"), - ("ec.transition.cctwister", "CC Twister", "CC Virvel"), - ("ec.transition.ccwarpomatic", "CC WarpoMatic", "CC WarpoMatic"), + ("ec.transition.ccglasswipe", "Glass Wipe", "Glasövergång"), + ("ec.transition.ccgridwipe", "Grid Wipe", "Rutnätsövergång"), + ("ec.transition.ccimagewipe", "Luminance Image Wipe", "Luminansbildsövergång"), + ("ec.transition.ccjaws", "Serrated Wipe", "Tandad övergång"), + ("ec.transition.cclightwipe", "Light Wipe", "Ljusövergång"), + ("ec.transition.cclinesweep", "Line Sweep", "Linjesvep"), + ("ec.transition.ccradialscalewipe", "Radial Scale Wipe", "Radiell skalövergång"), + ("ec.transition.ccscalewipe", "Scale Wipe", "Skalövergång"), + ("ec.transition.cctwister", "Twist Wipe", "Vridövergång"), + ("ec.transition.ccwarpomatic", "Warp Dissolve", "Förvrängningsupplösning"), ("ec.transition.gradientwipe", "Gradient Wipe", "Gradientridå"), ("ec.transition.iriswipe", "Iris Wipe", "Irisridå"), ("ec.transition.linearwipe", "Linear Wipe", "Linjär ridå"), ("ec.transition.radialwipe", "Radial Wipe", "Radiell ridå"), ("ec.transition.venetian", "Venetian Blinds", "Persienner"), ("ec.utility.applylut", "Apply Color LUT", "Använd färg-LUT"), - ("ec.utility.ccoverbrights", "CC Overbrights", "CC Överljust"), + ("ec.utility.ccoverbrights", "Out-of-Range Display", "Visa utanför intervallet"), ("ec.utility.cineon", "Cineon Converter", "Cineon-konverterare"), ("ec.utility.colorprofileconverter", "Color Profile Converter", "Färgprofilkonverterare"), ("ec.utility.facemeasurements", "Face Measurements", "Ansiktsmått"), @@ -2226,6 +2233,38 @@ mod tests { use crate::i18n::set_current; use effectcraft_engine::effects; + #[test] + fn legacy_effect_names_draw_current_labels_without_changing_custom_names() { + for language in ["en", "ja", "zh-hans", "zh-hant", "fr", "sv", "ka", "cs", "de", "es", "pl", "pt-br", "ru", "uk"] { + set_current(language); + for spec in effects::registry() { + let translated = name(spec.id, spec.name); + for alias in effects::aliases(spec.id) { + assert_eq!(group(spec.id, alias), translated, "{language}: {alias}"); + assert!(matches(spec.id, alias, &spec.name.to_lowercase()), "{language}: search current name of {alias}"); + assert!(matches(spec.id, spec.name, &alias.to_lowercase()), "{language}: search legacy name {alias}"); + assert_eq!(group(spec.id, &format!("{alias} 2")), format!("{translated} 2")); + let custom = format!("{alias} custom"); + assert_eq!(group(spec.id, &custom), custom); + } + } + } + set_current("en"); + } + + #[test] + fn localized_effect_names_do_not_reintroduce_legacy_branding() { + for (language, rows) in COMPLETE.iter().chain(PARTIAL) { + for (_, _, translated) in *rows { + let label = translated.to_lowercase(); + assert!(!label.starts_with("cc "), "{language}: {translated}"); + for mark in ["lumetri", "mercury", "mocha", "keylight"] { + assert!(!label.contains(mark), "{language}: {translated}"); + } + } + } + } + /// Every row names an effect in the registry and still carries its English name, so a rename /// or a removal fails here rather than attaching a translation to a different effect. #[test] @@ -2242,6 +2281,20 @@ mod tests { } } + /// Two effects that draw one name cannot be told apart in the Effect menu, Effect Controls or + /// Effects & Presets, so every catalog names each effect it covers distinctly. + #[test] + fn display_names_are_unique_in_every_catalog() { + for (language, rows) in COMPLETE.iter().chain(PARTIAL) { + let mut seen: std::collections::HashMap<&str, &str> = std::collections::HashMap::new(); + for (id, _, translated) in *rows { + if let Some(other) = seen.insert(translated, id) { + assert_ne!(other, *id, "{language}: {id} and {other} are both {translated:?}"); + } + } + } + } + /// Rows are keyed by id, so a repeated id would shadow a translation. #[test] fn ids_are_unique() { diff --git a/crates/ui-egui/src/i18n/ui.rs b/crates/ui-egui/src/i18n/ui.rs index 15bf774f9..b2e0b3631 100644 --- a/crates/ui-egui/src/i18n/ui.rs +++ b/crates/ui-egui/src/i18n/ui.rs @@ -242,9 +242,11 @@ const JAPANESE: &[(&str, &str)] = &[ ("Brush Dynamics", "ブラシのダイナミクス"), ("Caching frames", "フレームをキャッシュ中"), ("Cancel", "キャンセル"), - ("Import After Effects Project", "After Effectsプロジェクトを読み込み"), + ("Import Adobe After Effects Project", "Adobe After Effectsプロジェクトを読み込み"), ("Some parts of this project cannot be imported exactly (left out or changed):", "このプロジェクトの一部は正確に読み込めません(省略または変更されます):"), ("Import Anyway", "このまま読み込み"), + ("{}: {} left out or changed", "{}:{} 件が省略または変更"), + ("… and {} more", "…ほか {} 件"), ("Casts Shadows", "シャドウを落とす"), ("Change Renderer…", "レンダラーを変更…"), ( @@ -708,8 +710,8 @@ const JAPANESE: &[(&str, &str)] = &[ ("Import Photoshop File", "Photoshopファイルを読み込み"), ("Interpret Unlabeled Alpha As", "ラベルのないアルファの解釈"), ("Join the ArtCraft Discord", "ArtCraftのDiscordに参加"), - ("Mercury GPU Acceleration ({})", "Mercury GPUアクセラレーション({})"), - ("Mercury Software Only", "Mercuryソフトウェアのみ"), + ("GPU Acceleration ({})", "GPUアクセラレーション({})"), + ("Software Rendering", "ソフトウェアレンダリング"), ("OCIO (built-in ACES config)", "OCIO(組み込みACES設定)"), ("Off (Final Quality)", "オフ(最終品質)"), ("OpenGL (compatibility)", "OpenGL(互換)"), @@ -891,6 +893,7 @@ const JAPANESE: &[(&str, &str)] = &[ ("Footage Frame Cache", "フッテージフレームキャッシュ"), ("Preview (RAM) Cache", "プレビュー(RAM)キャッシュ"), ("Reduce Cache Size When System Is Low on Memory", "システムのメモリが不足しているときにキャッシュサイズを減らす"), + ("GPU Frames Rendered at Once", "一度にレンダリングするGPUフレーム数"), ("Video", "ビデオ"), ("Enable Video Preview Output", "ビデオプレビュー出力を有効にする"), ("Video Device", "ビデオデバイス"), @@ -1118,7 +1121,7 @@ const JAPANESE: &[(&str, &str)] = &[ ("Script Console", "スクリプトコンソール"), ("Essential Graphics", "エッセンシャルグラフィックス"), ("ScriptUI Panel", "ScriptUIパネル"), - ("Lumetri Scopes", "Lumetriスコープ"), + ("Video Scopes", "ビデオスコープ"), ("Media Browser", "メディアブラウザー"), ("Content-Aware Fill", "コンテンツに応じた塗りつぶし"), ("Create Nulls From Paths", "パスからヌルを作成"), @@ -1318,6 +1321,7 @@ const JAPANESE: &[(&str, &str)] = &[ ("per second", "毎秒"), ("Waveform", "波形"), ("steps", "ステップ"), + ("GPU Acceleration (no GPU: software)", "GPUアクセラレーション(GPUなし:ソフトウェア)"), ]; const SIMPLIFIED_CHINESE: &[(&str, &str)] = &[ @@ -1381,9 +1385,11 @@ const SIMPLIFIED_CHINESE: &[(&str, &str)] = &[ ("Brush Dynamics", "画笔动态"), ("Caching frames", "正在缓存帧"), ("Cancel", "取消"), - ("Import After Effects Project", "导入 After Effects 项目"), + ("Import Adobe After Effects Project", "导入 Adobe After Effects 项目"), ("Some parts of this project cannot be imported exactly (left out or changed):", "此项目的某些部分无法准确导入(将被省略或更改):"), ("Import Anyway", "仍然导入"), + ("{}: {} left out or changed", "{}:{} 项被省略或更改"), + ("… and {} more", "…以及另外 {} 项"), ("Casts Shadows", "投射阴影"), ("Change Renderer…", "更改渲染器…"), ("Changing a shortcut creates a custom preset; the default stays as it is.", "修改快捷键会创建自定义预设;默认预设保持不变。"), @@ -1837,8 +1843,8 @@ const SIMPLIFIED_CHINESE: &[(&str, &str)] = &[ ("Import Photoshop File", "导入 Photoshop 文件"), ("Interpret Unlabeled Alpha As", "将未标记的 Alpha 解释为"), ("Join the ArtCraft Discord", "加入 ArtCraft Discord"), - ("Mercury GPU Acceleration ({})", "Mercury GPU 加速({})"), - ("Mercury Software Only", "仅 Mercury 软件"), + ("GPU Acceleration ({})", "GPU 加速({})"), + ("Software Rendering", "软件渲染"), ("OCIO (built-in ACES config)", "OCIO(内置 ACES 配置)"), ("Off (Final Quality)", "关闭(最终质量)"), ("OpenGL (compatibility)", "OpenGL(兼容)"), @@ -2022,6 +2028,7 @@ const SIMPLIFIED_CHINESE: &[(&str, &str)] = &[ ("Footage Frame Cache", "素材帧缓存"), ("Preview (RAM) Cache", "预览(内存)缓存"), ("Reduce Cache Size When System Is Low on Memory", "系统内存不足时减少缓存大小"), + ("GPU Frames Rendered at Once", "GPU 同时渲染的帧数"), ("Video", "视频"), ("Enable Video Preview Output", "启用视频预览输出"), ("Video Device", "视频设备"), @@ -2280,7 +2287,7 @@ const SIMPLIFIED_CHINESE: &[(&str, &str)] = &[ ("Script Console", "脚本控制台"), ("Essential Graphics", "基本图形"), ("ScriptUI Panel", "ScriptUI 面板"), - ("Lumetri Scopes", "Lumetri 示波器"), + ("Video Scopes", "视频示波器"), ("Media Browser", "媒体浏览器"), ("Content-Aware Fill", "内容识别填充"), ("Create Nulls From Paths", "从路径创建空对象"), @@ -2433,6 +2440,7 @@ const SIMPLIFIED_CHINESE: &[(&str, &str)] = &[ ("Blend Mode", "混合模式"), ("Show mesh", "显示网格"), ("Record Options...", "记录选项..."), + ("GPU Acceleration (no GPU: software)", "GPU 加速(无 GPU:软件)"), ]; /// Swedish (`sv`), written from the English source. @@ -2495,12 +2503,14 @@ const SWEDISH: &[(&str, &str)] = &[ ("Brush Dynamics", "Penseldynamik"), ("Caching frames", "Cachelagrar bildrutor"), ("Cancel", "Avbryt"), - ("Import After Effects Project", "Importera After Effects-projekt"), + ("Import Adobe After Effects Project", "Importera Adobe After Effects-projekt"), ( "Some parts of this project cannot be imported exactly (left out or changed):", "Vissa delar av projektet kan inte importeras exakt (utelämnas eller ändras):", ), ("Import Anyway", "Importera ändå"), + ("{}: {} left out or changed", "{}: {} utelämnas eller ändras"), + ("… and {} more", "… och {} till"), ("Casts Shadows", "Kastar skuggor"), ("Change Renderer…", "Byt renderare…"), ( @@ -2950,8 +2960,8 @@ const SWEDISH: &[(&str, &str)] = &[ ("Import Photoshop File", "Importera Photoshop-fil"), ("Interpret Unlabeled Alpha As", "Tolka omärkt alfa som"), ("Join the ArtCraft Discord", "Gå med i ArtCraft på Discord"), - ("Mercury GPU Acceleration ({})", "Mercury GPU-acceleration ({})"), - ("Mercury Software Only", "Endast Mercury-programvara"), + ("GPU Acceleration ({})", "GPU-acceleration ({})"), + ("Software Rendering", "Programvarurendering"), ("OCIO (built-in ACES config)", "OCIO (inbyggd ACES-konfiguration)"), ("Off (Final Quality)", "Av (slutlig kvalitet)"), ("OpenGL (compatibility)", "OpenGL (kompatibilitet)"), @@ -3133,6 +3143,7 @@ const SWEDISH: &[(&str, &str)] = &[ ("Footage Frame Cache", "Bildrutecache för material"), ("Preview (RAM) Cache", "Förhandsgranskningscache (RAM)"), ("Reduce Cache Size When System Is Low on Memory", "Minska cachestorleken när systemet har ont om minne"), + ("GPU Frames Rendered at Once", "GPU-bildrutor som renderas samtidigt"), ("Video", "Video"), ("Enable Video Preview Output", "Aktivera videoutgång för förhandsgranskning"), ("Video Device", "Videoenhet"), @@ -3183,6 +3194,14 @@ const SWEDISH: &[(&str, &str)] = &[ ("A built-in preset has this name", "En inbyggd förinställning har det här namnet"), ("Cannot import dropped files: {}", "Kan inte importera släppta filer: {}"), ("Cannot open dropped project {}: {}", "Kan inte öppna det släppta projektet {}: {}"), + ( + "Imported footage has an unlabeled alpha channel: choose how to interpret it in File ▸ Interpret Footage ▸ Main…", + "Importerat material har en omärkt alfakanal: välj hur den ska tolkas i Arkiv ▸ Tolka material ▸ Huvud…", + ), + ( + "Skipped {} more file(s) while a dialog is open; import them again after it", + "Hoppade över {} fil(er) till medan en dialogruta är öppen; importera dem igen efteråt", + ), ("Click in the Composition panel to sample text {}", "Klicka i panelen Komposition för att hämta text {}"), ("Click to sample text {}", "Klicka för att hämta text {}"), ("Comment of {}: {}", "Kommentar till {}: {}"), @@ -3191,7 +3210,7 @@ const SWEDISH: &[(&str, &str)] = &[ ("Drop {} files to import", "Släpp {} filer för att importera"), ("Effects", "Effekter"), ("Label of {}: {}", "Etikett för {}: {}"), - ("Mercury GPU Acceleration (no GPU: software)", "Mercury GPU-acceleration (ingen GPU: programvara)"), + ("GPU Acceleration (no GPU: software)", "GPU-acceleration (ingen GPU: programvara)"), ("Only one project can be opened per drop; skipped {} more", "Bara ett projekt kan öppnas per släpp; hoppade över {} till"), ("Open in Essential Graphics", "Öppna i Grundläggande grafik"), ("Original color: click to go back to it", "Originalfärg: klicka för att återgå till den"), @@ -3440,7 +3459,7 @@ const SWEDISH: &[(&str, &str)] = &[ ("Script Console", "Skriptkonsol"), ("Essential Graphics", "Grundläggande grafik"), ("ScriptUI Panel", "ScriptUI-panel"), - ("Lumetri Scopes", "Lumetri-scope"), + ("Video Scopes", "Videoskop"), ("Media Browser", "Mediebläddrare"), ("Content-Aware Fill", "Innehållsmedveten fyllning"), ("Create Nulls From Paths", "Skapa nullobjekt från banor"), @@ -3638,7 +3657,6 @@ const SWEDISH: &[(&str, &str)] = &[ ("Smoothing", "Utjämning"), ("Top", "Överkant"), ("Mode", "Läge"), - ("Frame", "Bildruta"), ("Orientation", "Orientering"), ("Create", "Skapa"), ("Overlap", "Överlappning"), @@ -3763,9 +3781,11 @@ const TRADITIONAL_CHINESE: &[(&str, &str)] = &[ ("Brush Dynamics", "筆刷動態"), ("Caching frames", "正在快取幀"), ("Cancel", "取消"), - ("Import After Effects Project", "匯入 After Effects 專案"), + ("Import Adobe After Effects Project", "匯入 Adobe After Effects 專案"), ("Some parts of this project cannot be imported exactly (left out or changed):", "此專案的部分內容無法準確匯入(將被省略或變更):"), ("Import Anyway", "仍要匯入"), + ("{}: {} left out or changed", "{}:{} 項被省略或變更"), + ("… and {} more", "…以及另外 {} 項"), ("Casts Shadows", "投射陰影"), ("Change Renderer…", "更改算圖引擎…"), ("Changing a shortcut creates a custom preset; the default stays as it is.", "修改快速鍵會創建自定義預設集;預設預設集保持不變。"), @@ -4350,6 +4370,7 @@ const TRADITIONAL_CHINESE: &[(&str, &str)] = &[ ("Footage Frame Cache", "素材影格快取"), ("Preview (RAM) Cache", "預覽(記憶體)快取"), ("Reduce Cache Size When System Is Low on Memory", "系統記憶體不足時減少快取大小"), + ("GPU Frames Rendered at Once", "GPU 同時算圖的影格數"), ("Video", "視訊"), ("Enable Video Preview Output", "啟用視訊預覽輸出"), ("Video Device", "視訊裝置"), @@ -4608,7 +4629,7 @@ const TRADITIONAL_CHINESE: &[(&str, &str)] = &[ ("Script Console", "指令碼主控台"), ("Essential Graphics", "基本圖形"), ("ScriptUI Panel", "ScriptUI 面板"), - ("Lumetri Scopes", "Lumetri 示波器"), + ("Video Scopes", "視訊示波器"), ("Media Browser", "媒體瀏覽器"), ("Content-Aware Fill", "內容感知填色"), ("Create Nulls From Paths", "從路徑建立空物件"), @@ -4761,6 +4782,9 @@ const TRADITIONAL_CHINESE: &[(&str, &str)] = &[ ("Blend Mode", "混合模式"), ("Show mesh", "顯示網格"), ("Record Options...", "記錄選項..."), + ("Software Rendering", "軟體算繪"), + ("GPU Acceleration ({})", "GPU 加速({})"), + ("GPU Acceleration (no GPU: software)", "GPU 加速(無 GPU:軟體)"), ]; /// Ukrainian (uk): original translations of the same interface rows as the Chinese catalogs. @@ -4825,12 +4849,14 @@ const UKRAINIAN: &[(&str, &str)] = &[ ("Brush Dynamics", "Динаміка пензля"), ("Caching frames", "Кешування кадрів"), ("Cancel", "Скасувати"), - ("Import After Effects Project", "Імпортувати проєкт After Effects"), + ("Import Adobe After Effects Project", "Імпортувати проєкт Adobe After Effects"), ( "Some parts of this project cannot be imported exactly (left out or changed):", "Деякі частини цього проєкту неможливо імпортувати точно (їх пропущено або змінено):", ), ("Import Anyway", "Усе одно імпортувати"), + ("{}: {} left out or changed", "{}: {} пропущено або змінено"), + ("… and {} more", "… і ще {}"), ("Casts Shadows", "Відкидає тіні"), ("Change Renderer…", "Змінити рушій рендерингу…"), ( @@ -5301,8 +5327,8 @@ const UKRAINIAN: &[(&str, &str)] = &[ ("Import Photoshop File", "Імпорт файлу Photoshop"), ("Interpret Unlabeled Alpha As", "Інтерпретувати непозначену альфу як"), ("Join the ArtCraft Discord", "Приєднатися до Discord ArtCraft"), - ("Mercury GPU Acceleration ({})", "Прискорення Mercury GPU ({})"), - ("Mercury Software Only", "Лише програмний Mercury"), + ("GPU Acceleration ({})", "Прискорення GPU ({})"), + ("Software Rendering", "Програмне відтворення"), ("OCIO (built-in ACES config)", "OCIO (вбудована конфігурація ACES)"), ("Off (Final Quality)", "Вимкнено (остаточна якість)"), ("OpenGL (compatibility)", "OpenGL (сумісність)"), @@ -5348,6 +5374,8 @@ const UKRAINIAN: &[(&str, &str)] = &[ ("Project", "Проєкт"), ("Total Offset", "Загальний зсув"), ("Per Layer", "На шар"), + ("Video Scopes", "Відеоскопи"), + ("GPU Acceleration (no GPU: software)", "Прискорення GPU (без GPU: програмне)"), ]; // Brazilian Portuguese. @@ -5404,12 +5432,14 @@ const PORTUGUESE: &[(&str, &str)] = &[ ("Brush Dynamics", "Dinâmica do pincel"), ("Caching frames", "Armazenando quadros em cache"), ("Cancel", "Cancelar"), - ("Import After Effects Project", "Importar projeto do After Effects"), + ("Import Adobe After Effects Project", "Importar projeto do Adobe After Effects"), ( "Some parts of this project cannot be imported exactly (left out or changed):", "Algumas partes deste projeto não podem ser importadas exatamente (omitidas ou alteradas):", ), ("Import Anyway", "Importar mesmo assim"), + ("{}: {} left out or changed", "{}: {} omitidos ou alterados"), + ("… and {} more", "… e mais {}"), ("Casts Shadows", "Projeta sombras"), ("Change Renderer…", "Alterar renderizador…"), ( @@ -5910,6 +5940,7 @@ const PORTUGUESE: &[(&str, &str)] = &[ ("Recent Projects Shown", "Projetos recentes exibidos"), ("Red", "Vermelho"), ("Reduce Cache Size When System Is Low on Memory", "Reduzir o tamanho do cache quando o sistema estiver com pouca memória"), + ("GPU Frames Rendered at Once", "Quadros da GPU renderizados de uma vez"), ("Remove from favorites", "Remover dos favoritos"), ("Repair", "Reparo"), ("Report Missing Frames", "Relatar quadros ausentes"), @@ -5979,7 +6010,7 @@ const PORTUGUESE: &[(&str, &str)] = &[ ("Guess", "Adivinhar"), ("Import Image Sequence", "Importar sequência de imagens"), ("Import PDF / Illustrator File", "Importar arquivo PDF / Illustrator"), - ("Mercury Software Only", "Apenas software Mercury"), + ("Software Rendering", "Renderização por software"), ("OCIO (built-in ACES config)", "OCIO (config do ACES integrado)"), ("Still Duration", "Duração da imagem fixa"), ("Straight", "Direto"), @@ -6086,7 +6117,7 @@ const PORTUGUESE: &[(&str, &str)] = &[ ("Light", "Claro"), ("Lines", "Linhas"), ("Memory & CPU", "Memória e CPU"), - ("Mercury GPU Acceleration ({})", "Aceleração de GPU Mercury ({})"), + ("GPU Acceleration ({})", "Aceleração de GPU ({})"), ("Off", "Desativado"), ("Options", "Opções"), ("Overlay", "Sobrepor"), @@ -6111,6 +6142,8 @@ const PORTUGUESE: &[(&str, &str)] = &[ ("Video", "Vídeo"), ("channel", "canal"), ("files", "arquivos"), + ("Video Scopes", "Escopos de vídeo"), + ("GPU Acceleration (no GPU: software)", "Aceleração de GPU (sem GPU: software)"), ]; /// Polish (pl): original translations covering every row in the Chinese panel catalogs. @@ -6174,12 +6207,14 @@ const POLISH: &[(&str, &str)] = &[ ("Brush Dynamics", "Dynamika pędzla"), ("Caching frames", "Buforowanie klatek"), ("Cancel", "Anuluj"), - ("Import After Effects Project", "Importuj projekt After Effects"), + ("Import Adobe After Effects Project", "Importuj projekt Adobe After Effects"), ( "Some parts of this project cannot be imported exactly (left out or changed):", "Niektórych części tego projektu nie można zaimportować dokładnie (zostaną pominięte lub zmienione):", ), ("Import Anyway", "Importuj mimo to"), + ("{}: {} left out or changed", "{}: {} pominięto lub zmieniono"), + ("… and {} more", "… i {} więcej"), ("Casts Shadows", "Rzuca cienie"), ("Change Renderer…", "Zmień renderer…"), ( @@ -6629,8 +6664,8 @@ const POLISH: &[(&str, &str)] = &[ ("Import Photoshop File", "Importuj plik Photoshopa"), ("Interpret Unlabeled Alpha As", "Interpretuj nieoznaczoną alfę jako"), ("Join the ArtCraft Discord", "Dołącz do Discorda ArtCraft"), - ("Mercury GPU Acceleration ({})", "Akceleracja Mercury GPU ({})"), - ("Mercury Software Only", "Tylko programowy Mercury"), + ("GPU Acceleration ({})", "Akceleracja GPU ({})"), + ("Software Rendering", "Renderowanie programowe"), ("OCIO (built-in ACES config)", "OCIO (wbudowana konfiguracja ACES)"), ("Off (Final Quality)", "Wył. (jakość końcowa)"), ("OpenGL (compatibility)", "OpenGL (zgodność)"), @@ -6868,6 +6903,8 @@ const POLISH: &[(&str, &str)] = &[ ("Resolution:", "Rozdzielczość:"), ("Guide Layer", "Warstwa pomocnicza"), ("Layer Styles", "Style warstwy"), + ("Video Scopes", "Wskaźniki wideo"), + ("GPU Acceleration (no GPU: software)", "Akceleracja GPU (bez GPU: programowa)"), ]; /// Czech (cs): original translations covering every row in the Chinese panel catalogs. @@ -6928,12 +6965,14 @@ const CZECH: &[(&str, &str)] = &[ ("Brush Dynamics", "Dynamika štětce"), ("Caching frames", "Ukládání snímků do mezipaměti"), ("Cancel", "Zrušit"), - ("Import After Effects Project", "Importovat projekt After Effects"), + ("Import Adobe After Effects Project", "Importovat projekt Adobe After Effects"), ( "Some parts of this project cannot be imported exactly (left out or changed):", "Některé části tohoto projektu nelze importovat přesně (budou vynechány nebo změněny):", ), ("Import Anyway", "Přesto importovat"), + ("{}: {} left out or changed", "{}: {} vynecháno nebo změněno"), + ("… and {} more", "… a {} dalších"), ("Casts Shadows", "Vrhat stíny"), ("Change Renderer…", "Změnit vykreslovací modul…"), ( @@ -7380,8 +7419,8 @@ const CZECH: &[(&str, &str)] = &[ ("Import Photoshop File", "Importovat soubor Photoshop"), ("Interpret Unlabeled Alpha As", "Interpretovat neoznačenou alfu jako"), ("Join the ArtCraft Discord", "Připojit se na Discord ArtCraft"), - ("Mercury GPU Acceleration ({})", "GPU akcelerace Mercury ({})"), - ("Mercury Software Only", "Pouze softwarové zpracování Mercury"), + ("GPU Acceleration ({})", "Akcelerace GPU ({})"), + ("Software Rendering", "Softwarové vykreslování"), ("OCIO (built-in ACES config)", "OCIO (vestavěná konfigurace ACES)"), ("Off (Final Quality)", "Vypnuto (finální kvalita)"), ("OpenGL (compatibility)", "OpenGL (kompatibilita)"), @@ -7432,6 +7471,8 @@ const CZECH: &[(&str, &str)] = &[ ("Variable fonts only", "Jen proměnná písma"), ("Total Offset", "Celkový posun"), ("Per Layer", "Na vrstvu"), + ("Video Scopes", "Videoskopy"), + ("GPU Acceleration (no GPU: software)", "Akcelerace GPU (bez GPU: software)"), ]; /// Spanish: neutral, tú, sentence case; the same source strings as the Chinese catalogs, in the same @@ -7501,12 +7542,14 @@ const SPANISH: &[(&str, &str)] = &[ ("Brush Dynamics", "Dinámica del pincel"), ("Caching frames", "Almacenando fotogramas en caché"), ("Cancel", "Cancelar"), - ("Import After Effects Project", "Importar proyecto de After Effects"), + ("Import Adobe After Effects Project", "Importar proyecto de Adobe After Effects"), ( "Some parts of this project cannot be imported exactly (left out or changed):", "Algunas partes de este proyecto no se pueden importar con exactitud (se omiten o cambian):", ), ("Import Anyway", "Importar de todos modos"), + ("{}: {} left out or changed", "{}: {} omitidos o cambiados"), + ("… and {} more", "… y {} más"), ("Casts Shadows", "Proyecta sombras"), ("Change Renderer…", "Cambiar motor de procesamiento…"), ( @@ -7960,8 +8003,8 @@ const SPANISH: &[(&str, &str)] = &[ ("Import Photoshop File", "Importar archivo de Photoshop"), ("Interpret Unlabeled Alpha As", "Interpretar alfa sin etiquetar como"), ("Join the ArtCraft Discord", "Únete al Discord de ArtCraft"), - ("Mercury GPU Acceleration ({})", "Aceleración por GPU Mercury ({})"), - ("Mercury Software Only", "Solo software Mercury"), + ("GPU Acceleration ({})", "Aceleración por GPU ({})"), + ("Software Rendering", "Renderizado por software"), ("OCIO (built-in ACES config)", "OCIO (configuración ACES integrada)"), ("Off (Final Quality)", "Desactivado (calidad final)"), ("OpenGL (compatibility)", "OpenGL (compatibilidad)"), @@ -8002,6 +8045,8 @@ const SPANISH: &[(&str, &str)] = &[ ("After Effects project", "Proyecto de After Effects"), ("Lottie animation", "Animación de Lottie"), ("Project", "Proyecto"), + ("Video Scopes", "Monitores de vídeo"), + ("GPU Acceleration (no GPU: software)", "Aceleración por GPU (sin GPU: software)"), ]; /// Georgian (ka): every row of the Chinese catalogs, in the same order (the tests keep them in step). @@ -8065,12 +8110,14 @@ const GEORGIAN: &[(&str, &str)] = &[ ("Brush Dynamics", "ფუნჯის დინამიკა"), ("Caching frames", "კადრების ქეშირება"), ("Cancel", "გაუქმება"), - ("Import After Effects Project", "After Effects-ის პროექტის იმპორტი"), + ("Import Adobe After Effects Project", "Adobe After Effects-ის პროექტის იმპორტი"), ( "Some parts of this project cannot be imported exactly (left out or changed):", "ამ პროექტის ზოგიერთი ნაწილის ზუსტად იმპორტი შეუძლებელია (გამოტოვებული ან შეცვლილი):", ), ("Import Anyway", "მაინც იმპორტი"), + ("{}: {} left out or changed", "{}: {} გამოტოვებული ან შეცვლილი"), + ("… and {} more", "… და კიდევ {}"), ("Casts Shadows", "ჩრდილს აგდებს"), ("Change Renderer…", "რენდერერის შეცვლა…"), ("Changing a shortcut creates a custom preset; the default stays as it is.", "მალსახმობის შეცვლა ქმნის მორგებულ პრესეტს; ნაგულისხმევი უცვლელი რჩება."), @@ -8528,8 +8575,8 @@ const GEORGIAN: &[(&str, &str)] = &[ ("Import Photoshop File", "Photoshop ფაილის იმპორტი"), ("Interpret Unlabeled Alpha As", "უცნობი ალფას ინტერპრეტაცია"), ("Join the ArtCraft Discord", "შემოგვიერთდით Discord-ზე"), - ("Mercury GPU Acceleration ({})", "GPU აჩქარება ({})"), - ("Mercury Software Only", "მხოლოდ Mercury Software"), + ("GPU Acceleration ({})", "GPU აჩქარება ({})"), + ("Software Rendering", "პროგრამული რენდერი"), ("OCIO (built-in ACES config)", "OCIO (ჩაშენებული ACES კონფიგურაცია)"), ("Off (Final Quality)", "გამორთული (საბოლოო ხარისხი)"), ("OpenGL (compatibility)", "OpenGL (თავსებადობა)"), @@ -8761,6 +8808,7 @@ const GEORGIAN: &[(&str, &str)] = &[ ("Total Offset", "ჯამური წანაცვლება"), ("Per Layer", "შრიდან შრემდე"), ("Track", "ტრეკინგი"), + ("Video Scopes", "ვიდეოსკოპები"), ]; /// Georgian rows for interface text no other catalog has yet: strings the panels already send @@ -8837,7 +8885,6 @@ const GEORGIAN_PANELS: &[(&str, &str)] = &[ ("Height", "სიმაღლე"), ("Shape", "ფიგურა"), ("Mode", "რეჟიმი"), - ("Frame", "კადრი"), ("Orientation", "ორიენტაცია"), ("Position (pixels)", "პოზიცია (პიქსელი)"), ("Position (percent of the composition)", "პოზიცია (კომპოზიციის პროცენტი)"), @@ -8951,7 +8998,7 @@ const GEORGIAN_PANELS: &[(&str, &str)] = &[ ("Solid Settings", "სოლიდის პარამეტრები"), ("Save as Template", "შაბლონად შენახვა"), ("My Custom RGB", "ჩემი მორგებული RGB"), - ("Mercury GPU Acceleration (no GPU: software)", "Mercury GPU აჩქარება (GPU არ არის: პროგრამული)"), + ("GPU Acceleration (no GPU: software)", "GPU აჩქარება (GPU არ არის: პროგრამული)"), ("Sort by {}, {}", "დალაგება: {}, {}"), ("Sort by {}", "დალაგება: {}"), ("Label of {}: {}", "{}-ის იარლიყი: {}"), @@ -9037,12 +9084,14 @@ const FRENCH: &[(&str, &str)] = &[ ("Brush Dynamics", "Dynamique du pinceau"), ("Caching frames", "Mise en cache des images"), ("Cancel", "Annuler"), - ("Import After Effects Project", "Importer un projet After Effects"), + ("Import Adobe After Effects Project", "Importer un projet Adobe After Effects"), ( "Some parts of this project cannot be imported exactly (left out or changed):", "Certaines parties de ce projet ne peuvent pas être importées à l’identique (omises ou modifiées) :", ), ("Import Anyway", "Importer quand même"), + ("{}: {} left out or changed", "{} : {} omis ou modifiés"), + ("… and {} more", "… et {} de plus"), ("Casts Shadows", "Projette des ombres"), ("Change Renderer…", "Changer de moteur de rendu…"), ( @@ -9510,8 +9559,8 @@ const FRENCH: &[(&str, &str)] = &[ ("Import Photoshop File", "Importer un fichier Photoshop"), ("Interpret Unlabeled Alpha As", "Interpréter l’alpha non étiqueté comme"), ("Join the ArtCraft Discord", "Rejoindre le Discord ArtCraft"), - ("Mercury GPU Acceleration ({})", "Accélération GPU Mercury ({})"), - ("Mercury Software Only", "Mercury, logiciel uniquement"), + ("GPU Acceleration ({})", "Accélération GPU ({})"), + ("Software Rendering", "Rendu logiciel"), ("OCIO (built-in ACES config)", "OCIO (configuration ACES intégrée)"), ("Off (Final Quality)", "Désactivé (qualité finale)"), ("OpenGL (compatibility)", "OpenGL (compatibilité)"), @@ -9780,6 +9829,14 @@ const FRENCH: &[(&str, &str)] = &[ ), ("Only one project can be opened per drop; skipped {} more", "Un seul projet peut être ouvert par dépôt ; {} de plus ignoré(s)"), ("Cannot open dropped project {}: {}", "Impossible d’ouvrir le projet déposé {} : {}"), + ( + "Imported footage has an unlabeled alpha channel: choose how to interpret it in File ▸ Interpret Footage ▸ Main…", + "Le métrage importé a une couche alpha non étiquetée : choisissez son interprétation dans Fichier ▸ Interpréter le métrage ▸ Principal…", + ), + ( + "Skipped {} more file(s) while a dialog is open; import them again after it", + "{} fichier(s) de plus ignoré(s) pendant qu’une boîte de dialogue est ouverte ; importez-les ensuite", + ), ("Cannot import dropped files: {}", "Impossible d’importer les fichiers déposés : {}"), ("A built-in preset has this name", "Un préréglage intégré porte ce nom"), ("Replaces your preset with this name", "Remplace votre préréglage portant ce nom"), @@ -9812,7 +9869,6 @@ const FRENCH: &[(&str, &str)] = &[ ("Height", "Hauteur"), ("Shape", "Forme"), ("Mode", "Mode"), - ("Frame", "Image"), ("Orientation", "Orientation"), ("Position (pixels)", "Position (pixels)"), ("Position (percent of the composition)", "Position (pourcentage de la composition)"), @@ -9926,7 +9982,7 @@ const FRENCH: &[(&str, &str)] = &[ ("Solid Settings", "Paramètres du solide"), ("Save as Template", "Enregistrer en tant que modèle"), ("My Custom RGB", "Mon RVB personnalisé"), - ("Mercury GPU Acceleration (no GPU: software)", "Accélération GPU Mercury (aucun GPU : logiciel)"), + ("GPU Acceleration (no GPU: software)", "Accélération GPU (aucun GPU : logiciel)"), ("Sort by {}, {}", "Trier par {}, {}"), ("Sort by {}", "Trier par {}"), ("Label of {}: {}", "Étiquette de {} : {}"), @@ -10047,7 +10103,7 @@ const FRENCH: &[(&str, &str)] = &[ ("Script Console", "Console de scripts"), ("Essential Graphics", "Graphiques essentiels"), ("ScriptUI Panel", "Panneau ScriptUI"), - ("Lumetri Scopes", "Scopes Lumetri"), + ("Video Scopes", "Oscilloscopes vidéo"), ("Media Browser", "Explorateur de médias"), ("Content-Aware Fill", "Remplissage adapté au contenu"), ("Create Nulls From Paths", "Créer des objets nuls à partir de tracés"), @@ -10266,6 +10322,26 @@ const FRENCH: &[(&str, &str)] = &[ mod tests { use super::*; + /// Check rendered feature labels only. Format names, compatibility tokens and user text + /// are not subject to this guard, and a missing translation keeps its English fallback. + #[test] + fn renamed_panel_labels_do_not_reintroduce_vendor_names() { + for language in ["en", "ja", "zh-hans", "zh-hant", "fr", "sv", "ka", "cs", "de", "es", "pl", "pt-br", "ru", "uk"] { + set_current(language); + for source in ["GPU Acceleration ({})", "GPU Acceleration (no GPU: software)", "Software Rendering", "Video Scopes"] { + let rendered = tr(source); + assert!(!rendered.is_empty(), "{language}: {source}"); + let label = rendered.to_lowercase(); + for mark in ["lumetri", "mercury", "mocha", "keylight"] { + assert!(!label.contains(mark), "{language}: {source} -> {rendered}"); + } + } + // A real interchange label remains factual, not rewritten or rejected. + assert!(!tr("Export Adobe Premiere Pro Project").is_empty()); + } + set_current("en"); + } + /// The interface text that has moved into this catalog. Add a panel here when you convert it; /// `every_row_is_used_by_a_converted_panel` then looks for its rows' English sources in it. pub(super) const CONVERTED: &[(&str, &str)] = &[ diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e84ef79dd..cd0ac4307 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -190,6 +190,9 @@ pub struct Playback { pub caching: bool, /// Resolution override while playing (pixel step), `None` = the viewer's. pub res: Option, + /// Settings ▸ Previews ▸ Lower Resolution When Playback Can't Keep Up: the lower render + /// scale this play stepped down to, and when (seconds). See `step_down`. + pub adaptive: Option<(f64, f64)>, /// Include Overlays / Include Layer Controls. pub overlays: bool, pub layer_controls: bool, @@ -203,6 +206,8 @@ pub struct Playback { /// sound plays it goes on through frames that aren't cached, and the picture drops frames to /// follow it (#2473). pub audio_held: bool, + /// The project last checked for a silent preview that became audible (#2104). + pub silent_project: Option>, /// Starting a preview with sound (#947): the playhead stays on the first frame while the /// frames ahead render (the prefetch window, at most what fits in the RAM preview), then /// sound and picture start together from it. Without this the picture played on silently @@ -217,6 +222,14 @@ pub struct Playback { pub first_cached: Option, /// When the last frames were shown (seconds, the last second's worth): the achieved rate. pub shown_at: std::collections::VecDeque, + /// The rate the silent clock advances at (comp frames per second, at most `fps`; 0 = the + /// frame rate): below real time while frames arrive slower than that, so a first playback + /// shows them evenly instead of in bursts between freezes (see `pace_for`). + pub pace: f64, + /// Played frames the paced clock owes (fractional), `Some` while it runs below real time. + pub pace_acc: Option, + /// When the silent clock last ran (seconds), for the pace. + pub paced_at: Option, } impl Playback { @@ -302,6 +315,7 @@ pub struct EffectcraftApp { pub(crate) input_waiters: Vec>, /// Control requests waiting for an offloaded job (`wait: true` in the browser). pub(crate) job_waiters: Vec<(control::JobWaiter, Sender)>, + pub(crate) script_waiters: Vec<(u32, Sender)>, pending_screenshots: Vec, /// Pixels reserved by queued and in-flight screenshot requests. shot_bytes: usize, @@ -359,6 +373,12 @@ pub struct EffectcraftApp { footage_watcher: Option, /// The project revision and preference the watched set was last computed for. footage_watch_key: Option<(u64, String, bool)>, + /// The worker that stamps the LUT files in use ([`Self::tick_lut_files`], #3493). + lut_watcher: Option, + /// The LUT generation the frames kept were rendered at ([`effectcraft_engine::lut_reload::generation`]). + lut_generation: u64, + /// How many times the caches were purged ([`Self::purge_stamp`]). + purges: u64, /// The graphics device was lost and the window is closing (see `close_after_device_loss`); /// `true` keeps crash recovery for the next launch. device_lost_close: Option, @@ -389,8 +409,8 @@ pub struct EffectcraftApp { applied_prefs: Option<(u64, Option)>, /// A previous run that didn't exit cleanly (shown by `Dialog::Recovery`). pub recovery: Option, - /// Docked groups laid out last frame: (active panel, group rect) — `~` maximizes the one - /// under the pointer. + /// Panels drawn last frame, docked and floating: (panel, body rect). `~` maximizes the one + /// under the pointer; Effect Controls' arrow keys work only while it is on screen. pub(crate) dock_rects: Vec<(PanelKind, egui::Rect)>, /// The floating panels' OS windows that drew a panel last frame; one that is gone releases /// its IME state. diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index cb28b0bcd..9020b8fc0 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -27,8 +27,8 @@ pub(crate) use entries::{entry_checked, entry_enabled, entry_label, entry_shortc pub use frontend::frontend; #[cfg(test)] use frontend::zoom_step; -pub(crate) use frontend::{dialog_answered, run_engine}; -pub use invoke::{invoke, panel_command_id, reveal}; +pub(crate) use frontend::{dialog_answered, import_aep_projects, run_engine}; +pub use invoke::{invoke, panel_command_id, reveal, waits_on_dialog}; #[cfg(test)] use shortcuts::modifiers_at_start; pub use shortcuts::{Binding, bindings, chord_matches, forwardable_during_text_edit, handle_shortcuts, handle_tab_shortcut, parse_shortcut, shortcut_text}; diff --git a/crates/ui-egui/src/menus/frontend.rs b/crates/ui-egui/src/menus/frontend.rs index bd1294330..bf981fc67 100644 --- a/crates/ui-egui/src/menus/frontend.rs +++ b/crates/ui-egui/src/menus/frontend.rs @@ -502,6 +502,15 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu return run_engine(app, ctx, "window.assignWorkspaceShortcut", q); } "file.interpretFootage" => { + // A background import finishing while another dialog waits (the After Effects losses + // prompt of the same drop) does not replace it: that dialog's work would be lost. The + // footage keeps the default interpretation; the note says where to change it. + if app.dialog.is_some() { + app.ui.status = + crate::i18n::tr("Imported footage has an unlabeled alpha channel: choose how to interpret it in File ▸ Interpret Footage ▸ Main…") + .to_string(); + return Ok(Value::Null); + } // Import ▸ Interpret Unlabeled Alpha As: Ask User. if let Some(i) = p.get("item").and_then(Value::as_u64) { app.session.state.project_selection = vec![effectcraft_engine::project::ItemId(i)]; @@ -601,7 +610,8 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> Option> { enum Ask { Folder, - Import, + /// Footage, or a file with one of these extensions. + Import(&'static [&'static str]), OpenProject, Save(std::borrow::Cow<'static, str>), Open(&'static [&'static str]), @@ -617,7 +627,9 @@ pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> path.with_file_name(format!("{stem}{suffix}.{ext}")).to_string_lossy().into_owned().into() }; let (key, ask) = match id { - "file.import" | "file.importMultiple" => ("paths", Ask::Import), + // Projects too, as After Effects' File… takes an .aep: they import as projects + // (`run_with_picked`, and `file.import` for EffectCraft projects). + "file.import" | "file.importMultiple" => ("paths", Ask::Import(&["aep", "aepx", "ecproj", "ecprojx"])), "file.open" => ("path", Ask::OpenProject), "file.save" if app.session.path.is_none() => ("path", Ask::Save(project_as("", "ecproj"))), "file.saveAs" => ("path", Ask::Save(project_as("", "ecproj"))), @@ -644,7 +656,7 @@ pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> "effect.plugins.install" if params.get("bundle").is_some() => ("folder", Ask::Folder), "effect.plugins.install" if params.get("folder").is_none() && params.get("path").is_none() => ("path", Ask::Open(&["wasm"])), "effect.helpers.load" => ("path", Ask::Folder), - "file.replaceFootage" => ("path", Ask::Import), + "file.replaceFootage" => ("path", Ask::Import(&[])), "file.relinkFootage" => ("folder", Ask::Folder), "file.collectFiles" => ("folder", Ask::Save("Collected Files".into())), // An open XML project gets ` copy` too: the copy never writes over the open file. @@ -672,6 +684,7 @@ pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> "eps", "psd", "psb", "tga", "hdr", "gltf", "glb", "obj", "stl", "fbx", "json", "csv", "tsv", ]; let footage = |more: &[&'static str]| effectcraft_project::VIDEO_EXTENSIONS.iter().chain(more).copied().collect::>(); + let importable = |more: &[&'static str]| footage(MEDIA).into_iter().chain(more.iter().copied()).collect::>(); // A host whose blocking dialogs are unsafe inside this frame shows the dialog without // blocking — macOS: the nested event loop re-entered winit's handler on a drag over the // window and aborted the app (#511); Linux: the desktop portal's answer never gets pumped, @@ -680,7 +693,7 @@ pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> if let Some(spawn) = app.hooks.file_dialog_async.as_ref() { let request = match ask { Ask::Folder => crate::FileDialog::Folder, - Ask::Import => crate::FileDialog::Open { exts: footage(MEDIA).iter().map(|e| e.to_string()).collect(), many: key == "paths" }, + Ask::Import(more) => crate::FileDialog::Open { exts: importable(more).iter().map(|e| e.to_string()).collect(), many: key == "paths" }, Ask::OpenProject => crate::FileDialog::OpenProject, Ask::Open(exts) => crate::FileDialog::Open { exts: exts.iter().map(|e| e.to_string()).collect(), many: false }, Ask::OpenFootage(more) => crate::FileDialog::Open { exts: footage(more).iter().map(|e| e.to_string()).collect(), many: false }, @@ -702,9 +715,9 @@ pub(super) fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> let Some(f) = app.hooks.pick_folder.as_ref() else { return Some(Err("no folder dialog available (pass `folder`)".into())) }; f().map(Value::from) } - Ask::Import => { + Ask::Import(more) => { let Some(f) = app.hooks.pick_files.as_ref() else { return Some(Err("no file dialog available (pass `paths`)".into())) }; - picked_value(key, f(&footage(MEDIA))) + picked_value(key, f(&importable(more))) } Ask::OpenProject => { let Some(f) = app.hooks.pick_open_project.as_ref() else { return Some(Err("no file dialog available (pass `path`)".into())) }; @@ -758,7 +771,8 @@ pub(crate) fn show_result_errors(app: &mut EffectcraftApp, result: &Value) { .map(|e| match (e.get("file").and_then(Value::as_str), e.get("error").and_then(Value::as_str)) { (Some(file), Some(error)) => format!("{file}: {error}"), (_, Some(error)) => error.to_string(), - _ => e.to_string(), + // A plain message (`file.import`'s errors) reads as itself, not as quoted JSON. + _ => e.as_str().map_or_else(|| e.to_string(), str::to_string), }) .collect(); let mut msg = parts.iter().take(3).cloned().collect::>().join("; "); @@ -770,6 +784,14 @@ pub(crate) fn show_result_errors(app: &mut EffectcraftApp, result: &Value) { /// Run command `id` with the path picked for it in parameter `key`. fn run_with_picked(app: &mut EffectcraftApp, id: &str, key: &str, params: &Value, v: Value) -> Result { + if matches!(id, "file.import" | "file.importMultiple") + && let Some(picked) = v.as_array() + && picked.iter().filter_map(Value::as_str).any(effectcraft_engine::commands::is_aep_path) + { + let (aep, rest): (Vec, Vec) = + picked.iter().filter_map(Value::as_str).map(str::to_string).partition(|p| effectcraft_engine::commands::is_aep_path(p)); + return import_picked_with_aep(app, id, key, params, aep, rest); + } let mut p = params.as_object().cloned().unwrap_or_default(); p.insert(key.to_string(), v); // Picked files import in the background, with the Importing card showing progress (#270). @@ -777,10 +799,13 @@ fn run_with_picked(app: &mut EffectcraftApp, id: &str, key: &str, params: &Value p.insert("background".into(), Value::Bool(true)); } // Photoshop files ask how to import them first; numbered stills whether to import them as an - // image sequence. + // image sequence. Not while the losses prompt of an After Effects project picked with them + // shows: one dialog at a time, so they import with the defaults. let picked = Value::Object(p.clone()); - if id == "file.import" && crate::panels::dialogs::open_form(app, id, &picked) - || matches!(id, "file.import" | "file.importMultiple") && crate::panels::forms::open_import_sequence(app, &picked) + let asking = crate::panels::aep_losses::asking(app); + if !asking + && (id == "file.import" && crate::panels::dialogs::open_form(app, id, &picked) + || matches!(id, "file.import" | "file.importMultiple") && crate::panels::forms::open_import_sequence(app, &picked)) { return Ok(json!({"dialog": id})); } @@ -804,6 +829,62 @@ fn run_with_picked(app: &mut EffectcraftApp, id: &str, key: &str, params: &Value r } +/// Import After Effects projects dropped or picked together, as File ▸ Import ▸ Adobe After +/// Effects Project… does each: one that would lose parts asks first (the losses prompt), and the +/// ones after it wait in its queue until it is answered (`panels::aep_losses`). Returns the errors +/// of those that could not be imported; each names its file. +pub(crate) fn import_aep_projects(app: &mut EffectcraftApp, paths: Vec) -> Vec { + let mut errors = Vec::new(); + // An unanswered prompt's projects go first. + let mut paths = crate::panels::aep_losses::take_unanswered(app).into_iter().chain(paths); + while let Some(path) = paths.next() { + if crate::panels::aep_losses::asking(app) { + let queue = &mut app.dialog_state.aep_losses.queue; + queue.push(path); + queue.extend(paths); + break; + } + if let Err(e) = run_with_picked(app, "file.importAep", "path", &json!({}), json!(path)) { + errors.push(e); + } + } + errors +} + +/// File ▸ Import ▸ File… with After Effects projects picked: they import first, as projects +/// ([`import_aep_projects`]), then the other files as footage. Their errors lead the result's +/// `errors` and the status line (callers of the blocking dialog drop the result), or the message +/// when the footage import failed. +fn import_picked_with_aep(app: &mut EffectcraftApp, id: &str, key: &str, params: &Value, aep: Vec, rest: Vec) -> Result { + let errors = import_aep_projects(app, aep); + let r = if !rest.is_empty() { + run_with_picked(app, id, key, params, json!(rest)) + } else if crate::panels::aep_losses::asking(app) { + Ok(json!({"dialog": "aepLosses"})) + } else { + Ok(json!({})) + }; + if errors.is_empty() { + return r; + } + match r { + Ok(mut result) => { + if !result.is_object() { + result = json!({"result": result}); + } + let own = result.get("errors").and_then(Value::as_array).cloned().unwrap_or_default(); + result["errors"] = Value::Array(errors.into_iter().map(Value::from).chain(own).collect()); + show_result_errors(app, &result); + Ok(result) + } + Err(e) => { + let message = format!("{}; {e}", errors.join("; ")); + app.ui.status = message.clone(); + Err(message) + } + } +} + /// View ▸ Zoom In / Zoom Out: the next standard magnification (as a share of screen pixels, so /// 100 % is one comp pixel per screen pixel at any display scaling) above or below `cur`, which is /// in points per comp pixel like the result (#882). diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 76a70426e..4349e6b33 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -183,8 +183,15 @@ fn only_comp(p: &Value) -> Option { m.get("comp")?.as_u64().map(effectcraft_engine::project::ItemId) } +/// Whether [`invoke`]'s result says the command waits on a dialog (a guard or a form deferred it +/// until the user answers, `{"dialog": …}`). +pub fn waits_on_dialog(result: &Value) -> bool { + result.get("dialog").is_some() +} + /// Execute a UI or engine command by id. pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: Value) -> Result { + crate::control::check_script_render(app, id)?; if matches!(id, "effects.copyPresetsFolderPath" | "mediaBrowser.copySourcePath") { return frontend(app, ctx, id, params); } diff --git a/crates/ui-egui/src/menus/shortcuts.rs b/crates/ui-egui/src/menus/shortcuts.rs index dc4efd384..a573a2f6d 100644 --- a/crates/ui-egui/src/menus/shortcuts.rs +++ b/crates/ui-egui/src/menus/shortcuts.rs @@ -403,6 +403,10 @@ fn handle_shortcuts_impl(app: &mut EffectcraftApp, ctx: &egui::Context, tab_only if app.dialog.is_some() { continue; } + if !mods.any() && crate::panels::effect_controls::handle_disclosure_key(app, key) { + ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, key)); + continue; + } // Enter in the Project panel renames the selected item (handled by the panel). // In Effects & Presets and the Render Queue Enter applies or opens the row (handled by the // panel), not the layer rename. diff --git a/crates/ui-egui/src/menus/tests.rs b/crates/ui-egui/src/menus/tests.rs index b12483b0a..12aac3cd9 100644 --- a/crates/ui-egui/src/menus/tests.rs +++ b/crates/ui-egui/src/menus/tests.rs @@ -730,3 +730,126 @@ fn effect_plugin_install_dialog_router() { assert!(r.is_err(), "{r:?}"); assert!(pending.lock().unwrap().is_empty(), "no dialog for an inline folder"); } + +/// File ▸ Import ▸ File… in the non-blocking dialog (macOS, Linux) offers After Effects projects +/// next to footage, and a picked one imports as File ▸ Import ▸ Adobe After Effects Project… does: its +/// folder, its composition open. +#[test] +fn import_file_dialog_offers_and_imports_after_effects_projects() { + use std::sync::{Arc, Mutex}; + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + let ctx = egui::Context::default(); + let dir = std::env::temp_dir().join(format!("ec-import-aep-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let aep = dir.join("Intro.aep").to_string_lossy().to_string(); + let mut source = effectcraft_engine::Session::default(); + source.execute("comp.new", json!({"name": "Intro", "width": 64, "height": 36, "frameRate": 30, "duration": 1})).unwrap(); + source.execute("file.exportAep", json!({"path": aep, "allowLosses": true})).unwrap(); + + let pending: Arc>> = Arc::default(); + let queue = pending.clone(); + app.hooks.file_dialog_async = Some(Box::new(move |dialog, _done| queue.lock().unwrap().push(dialog))); + invoke(&mut app, &ctx, "file.import", json!({})).unwrap(); + let Some(crate::FileDialog::Open { exts, many }) = pending.lock().unwrap().pop() else { panic!("an Open dialog was shown") }; + assert!(many && exts.iter().any(|e| e == "aep") && exts.iter().any(|e| e == "aepx"), "{exts:?}"); + assert!(exts.iter().any(|e| e == "png"), "footage too"); + + super::frontend::dialog_answered(&mut app, crate::DialogAnswer { command: "file.import".into(), key: "paths", params: json!({}), paths: vec![aep] }); + let comp = app.session.project.item(app.session.active_comp_id().expect("a composition is open")).unwrap(); + assert_eq!(comp.name, "Intro"); + assert_eq!(app.session.project.item(comp.parent.expect("in a folder")).unwrap().name, "Intro.aep"); + let _ = std::fs::remove_dir_all(&dir); +} + +/// Footage with unlabeled alpha asks how to interpret it (Interpret Unlabeled Alpha As: Ask +/// User), but not over a dialog that already waits (the After Effects losses prompt of the same +/// drop, which a background import can finish behind): that dialog stays, and a note says where +/// to choose the interpretation. +#[test] +fn the_unlabeled_alpha_question_does_not_replace_an_open_dialog() { + use effectcraft_engine::project::{Footage, FootageKind, ItemKind}; + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + let ctx = egui::Context::default(); + let footage = Footage { path: "/m/matte.tga".into(), kind: FootageKind::Still, width: 4, height: 4, has_video: true, ..Default::default() }; + let item = std::sync::Arc::make_mut(&mut app.session.project).add_item("matte.tga", Default::default(), None, ItemKind::Footage(footage)).0; + app.dialog = Some(crate::Dialog::AepLosses); + super::frontend(&mut app, &ctx, "file.interpretFootage", json!({"item": item, "reason": "unlabeledAlpha"})).unwrap(); + assert_eq!(app.dialog, Some(crate::Dialog::AepLosses), "the waiting dialog stays"); + assert!(app.ui.status.contains("Interpret Footage"), "{}", app.ui.status); + assert!(app.session.state.project_selection.is_empty(), "the selection is left alone"); + + app.dialog = None; + super::frontend(&mut app, &ctx, "file.interpretFootage", json!({"item": item, "reason": "unlabeledAlpha"})).unwrap(); + assert_eq!(app.dialog, Some(crate::Dialog::Form), "with no dialog open it asks"); +} + +/// File ▸ Import ▸ File… and Multiple Files… offer projects next to footage; File ▸ Replace +/// Footage offers footage only. +#[test] +fn only_the_import_dialogs_offer_projects() { + use std::sync::{Arc, Mutex}; + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + let pending: Arc>> = Arc::default(); + let queue = pending.clone(); + app.hooks.file_dialog_async = Some(Box::new(move |dialog, _done| queue.lock().unwrap().push(dialog))); + let projects = ["aep", "aepx", "ecproj", "ecprojx"]; + for (id, offered) in [("file.import", true), ("file.importMultiple", true), ("file.replaceFootage", false)] { + assert!(super::frontend::file_dialog(&mut app, id, &json!({})).is_some(), "{id} asks for a file"); + let Some(crate::FileDialog::Open { exts, .. }) = pending.lock().unwrap().pop() else { panic!("{id}: an Open dialog") }; + assert!(exts.iter().any(|e| e == "png"), "{id} offers footage"); + assert_eq!(projects.iter().all(|p| exts.iter().any(|e| e == p)), offered, "{id}: {exts:?}"); + assert!(offered || !projects.iter().any(|p| exts.iter().any(|e| e == p)), "{id} offers no project: {exts:?}"); + } +} + +/// Picked After Effects projects that cannot be imported keep their errors next to the other +/// files': a broken `.aep` and a broken `.ecproj` are both named in the status line. +#[test] +fn picked_projects_report_every_error() { + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + let dir = std::env::temp_dir().join(format!("ec-picked-errors-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let aep = dir.join("broken.aep"); + let ecproj = dir.join("broken.ecproj"); + std::fs::write(&aep, b"not an aep").unwrap(); + std::fs::write(&ecproj, b"not a project").unwrap(); + let paths = vec![aep.to_string_lossy().to_string(), ecproj.to_string_lossy().to_string()]; + super::frontend::dialog_answered(&mut app, crate::DialogAnswer { command: "file.import".into(), key: "paths", params: json!({}), paths: paths.clone() }); + let status = &app.ui.status; + assert!(status.contains("broken.aep") && status.contains("broken.ecproj"), "{status}"); + assert!(!status.contains('"'), "messages, not quoted JSON: {status}"); + assert!(app.dialog.is_none() && app.session.project.items.is_empty()); + + // The blocking dialog (Windows), whose callers drop the result: the status line still has both. + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + let ctx = egui::Context::default(); + app.hooks.pick_files = Some(Box::new(move |_: &[&str]| paths.clone())); + let _ = invoke(&mut app, &ctx, "file.import", json!({})); + let status = &app.ui.status; + assert!(status.contains("broken.aep") && status.contains("broken.ecproj"), "{status}"); + let _ = std::fs::remove_dir_all(&dir); +} + +/// A picked After Effects project with parts that cannot come over asks first; one picked with +/// it waits for the answer. +#[test] +fn a_picked_project_with_losses_asks_and_the_next_waits() { + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + let dir = std::env::temp_dir().join(format!("ec-picked-losses-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let lossy = dir.join("Synthetic.aep"); + std::fs::copy(concat!(env!("CARGO_MANIFEST_DIR"), "/../aep/tests/fixtures/synthetic.aep"), &lossy).unwrap(); + let intro = dir.join("Intro.aep").to_string_lossy().to_string(); + let mut source = effectcraft_engine::Session::default(); + source.execute("comp.new", json!({"name": "Intro", "width": 64, "height": 36, "frameRate": 30, "duration": 1})).unwrap(); + source.execute("file.exportAep", json!({"path": intro, "allowLosses": true})).unwrap(); + let paths = vec![lossy.to_string_lossy().to_string(), intro.clone()]; + super::frontend::dialog_answered(&mut app, crate::DialogAnswer { command: "file.import".into(), key: "paths", params: json!({}), paths }); + assert_eq!(app.dialog, Some(crate::Dialog::AepLosses)); + assert_eq!(app.dialog_state.aep_losses.queue, [intro]); + assert!(app.session.project.items.is_empty(), "nothing imported before the answer"); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/crates/ui-egui/src/panels/aep_losses.rs b/crates/ui-egui/src/panels/aep_losses.rs index 746e83b86..4674c369a 100644 --- a/crates/ui-egui/src/panels/aep_losses.rs +++ b/crates/ui-egui/src/panels/aep_losses.rs @@ -1,12 +1,14 @@ -//! File ▸ Import / Export ▸ After Effects Project… from the menu: what the conversion cannot +//! File ▸ Import / Export ▸ Adobe After Effects Project… from the menu: what the conversion cannot //! carry over is shown first (the command's dry run), and the user chooses to cancel or to go on //! with those losses (`allowLosses`). Cancel changes nothing; a conversion without losses runs at //! once. Agents running `file.importAep` / `file.exportAep` with `engine.execute` are never asked: -//! they pass `allowLosses` or get the refusal. +//! they pass `allowLosses` or get the refusal. After Effects projects dropped or picked together +//! (File ▸ Import ▸ File…) are asked about one at a time: the others wait in the queue until the +//! prompt is answered, and Cancel skips only the one it asks about. //! //! Automation ids: `dialog.aepLosses.continue`, `dialog.aepLosses.cancel`. -use crate::i18n::tr; +use crate::i18n::{tr, tr_args}; use egui::vec2; use serde_json::{Value, json}; @@ -17,10 +19,28 @@ use crate::{Dialog, EffectcraftApp}; #[derive(Clone, Debug, Default)] pub struct Pending { pub command: Option<(String, Value, Value)>, + /// After Effects projects dropped or picked with it, imported once it is answered. + pub queue: Vec, } -/// How many detail lines of a loss category the prompt shows. -const DETAILS: usize = 3; +/// Whether the prompt is showing with a conversion waiting on it. +pub fn asking(app: &EffectcraftApp) -> bool { + app.dialog == Some(Dialog::AepLosses) && app.dialog_state.aep_losses.command.is_some() +} + +/// The projects of a prompt another dialog replaced (Save changes? on quitting) before it was +/// answered: its import's file, then its queue. Taken, so they are tried again instead of waiting +/// for an answer that never comes (a replaced export is dropped: it wrote nothing). +pub fn take_unanswered(app: &mut EffectcraftApp) -> Vec { + if app.dialog == Some(Dialog::AepLosses) { + return Vec::new(); + } + let losses = &mut app.dialog_state.aep_losses; + let Some((id, params, _)) = losses.command.take() else { return std::mem::take(&mut losses.queue) }; + let mut paths: Vec = params.get("path").and_then(Value::as_str).filter(|_| id == "file.importAep").map(str::to_string).into_iter().collect(); + paths.append(&mut losses.queue); + paths +} /// Whether command `id` with `params` needs the prompt first. When it does, the prompt opens /// holding the command and the caller must not run it. `Err` when the dry run fails (the file @@ -39,6 +59,9 @@ pub fn guard(app: &mut EffectcraftApp, id: &str, params: &Value) -> Result String { chars.next().map(|f| f.to_uppercase().chain(chars).collect()).unwrap_or_default() } -/// The report's losses as lines: one per category with its count, and a few of its details. +/// The report's losses as lines: one per category with how many occurrences are left out or +/// changed, then every detail line the report keeps and "… and N more" for the occurrences none +/// describes (#3365). pub fn lines(report: &Value) -> Vec<(String, Vec)> { let Some(losses) = report["losses"].as_array() else { return vec![] }; losses .iter() .map(|l| { - let head = format!("{} ({})", category(l["category"].as_str().unwrap_or("other")), l["count"].as_u64().unwrap_or(0)); - let details = l["details"].as_array().map(|d| d.iter().filter_map(Value::as_str).take(DETAILS).map(str::to_string).collect()).unwrap_or_default(); + let count = l["count"].as_u64().unwrap_or(0); + let head = tr_args("{}: {} left out or changed", &[&category(l["category"].as_str().unwrap_or("other")), &count]); + let mut details: Vec = + l["details"].as_array().map(|d| d.iter().filter_map(Value::as_str).map(str::to_string).collect()).unwrap_or_default(); + // An export report has no `unlisted`: each of its lines is one occurrence. + let listed = u64::try_from(details.len()).unwrap_or(u64::MAX); + let unlisted = l["unlisted"].as_u64().unwrap_or_else(|| count.saturating_sub(listed)); + if unlisted > 0 { + details.push(tr_args("… and {} more", &[&unlisted])); + } (head, details) }) .collect() } +/// Draw the prompt for the conversion waiting on it: its losses by category, Cancel and Import / +/// Export Anyway (Escape cancels). pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { let Some((id, _, report)) = app.dialog_state.aep_losses.command.clone() else { app.dialog = None; return; }; let import = id == "file.importAep"; - let title = if import { "Import After Effects Project" } else { "Export After Effects Project" }; + let title = if import { "Import Adobe After Effects Project" } else { "Export Adobe After Effects Project" }; let mut answer: Option = None; super::dialogs::modal(ctx, title, vec2(520.0, 340.0), t, |ui| { let text = if import { @@ -114,11 +149,25 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { /// The prompt's answer: Cancel (`false`) drops the conversion, Import / Export Anyway runs it /// with `allowLosses`. fn finish(app: &mut EffectcraftApp, ctx: &egui::Context, go: bool) { - app.dialog = None; - let Some((id, params, _)) = app.dialog_state.aep_losses.command.take() else { return }; + let Some((id, params, _)) = app.dialog_state.aep_losses.command.take() else { + app.dialog = None; + return; + }; + let mut errors = Vec::new(); if go { - // An error is already in the status bar. - let _ = crate::menus::run_engine(app, ctx, &id, with_losses(¶ms)); + // The prompt stays up while the conversion runs: a question queued by a footage import + // that finished meanwhile (Interpret Footage for unlabeled alpha, drained by `run_engine`) + // leaves a note instead of opening a dialog the next queued prompt would replace. + if let Err(e) = crate::menus::run_engine(app, ctx, &id, with_losses(¶ms)) { + errors.push(e); + } + } + app.dialog = None; + // The projects dropped or picked with this one: Cancel skips only this one. + let queue = std::mem::take(&mut app.dialog_state.aep_losses.queue); + errors.extend(crate::menus::import_aep_projects(app, queue)); + if !errors.is_empty() { + app.ui.status = errors.join("; "); } } @@ -137,11 +186,72 @@ mod tests { fn the_report_reads_as_categories_with_counts_and_details() { let report = json!({"losses": [{"category": "layer_styles", "count": 4, "details": ["a", "b", "c", "d"]}, {"category": "effects", "count": 1}]}); let l = lines(&report); - assert_eq!(l[0], ("Layer styles (4)".to_string(), vec!["a".to_string(), "b".into(), "c".into()])); - assert_eq!(l[1], ("Effects (1)".to_string(), vec![])); + assert_eq!(l[0], ("Layer styles: 4 left out or changed".to_string(), vec!["a".to_string(), "b".into(), "c".into(), "d".into()])); + assert_eq!(l[1], ("Effects: 1 left out or changed".to_string(), vec!["… and 1 more".to_string()])); assert!(lines(&json!({})).is_empty()); } + /// #3365: every detail line shows, then how many occurrences have none: the report's + /// `unlisted` (an effect line can describe many instances), else the lines past the count. + #[test] + fn every_detail_shows_and_the_rest_is_counted() { + let details: Vec = (0..20).map(|i| format!("d{i}")).collect(); + let l = lines(&json!({"losses": [{"category": "masks", "count": 27, "details": details}]})); + assert_eq!(l[0].1.len(), 21); + assert_eq!(l[0].1[20], "… and 7 more"); + let l = lines(&json!({"losses": [{"category": "effects", "count": 50, "details": ["X, and 44 more like it"], "unlisted": 5}]})); + assert_eq!(l[0].1, ["X, and 44 more like it", "… and 5 more"]); + let l = lines(&json!({"losses": [{"category": "effects", "count": 45, "details": ["X, and 44 more like it"], "unlisted": 0}]})); + assert_eq!(l[0].1, ["X, and 44 more like it"]); + } + + /// Projects dropped or picked together wait only behind a prompt that shows; with none + /// showing they are tried at once (a missing file is an error naming it). + #[test] + fn projects_wait_only_behind_a_showing_prompt() { + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + app.dialog_state.aep_losses.command = Some(("file.importAep".into(), json!({"path": "/a/One.aep"}), json!({}))); + app.dialog = Some(Dialog::AepLosses); + assert!(crate::menus::import_aep_projects(&mut app, vec!["/no/such/Two.aep".into(), "/no/such/Three.aep".into()]).is_empty()); + assert_eq!(app.dialog_state.aep_losses.queue, ["/no/such/Two.aep", "/no/such/Three.aep"]); + + // Cancel: the queued ones are tried next (both missing here, so both are reported). + let ctx = egui::Context::default(); + finish(&mut app, &ctx, false); + assert!(app.dialog_state.aep_losses.queue.is_empty()); + assert!(app.ui.status.contains("Two.aep") && app.ui.status.contains("Three.aep"), "{}", app.ui.status); + } + + /// A prompt another dialog replaced (Save changes? on quitting) holds nothing back: the next + /// import tries its project and queue first, then the new one. + #[test] + fn a_replaced_prompt_holds_nothing_back() { + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + app.dialog_state.aep_losses.command = Some(("file.importAep".into(), json!({"path": "/no/such/One.aep"}), json!({}))); + app.dialog_state.aep_losses.queue = vec!["/no/such/Two.aep".into()]; + app.dialog = Some(Dialog::UnsavedChanges); + let errors = crate::menus::import_aep_projects(&mut app, vec!["/no/such/Three.aep".into()]); + let named = |n: &str| errors.iter().position(|e| e.contains(n)); + assert_eq!((named("One.aep"), named("Two.aep"), named("Three.aep")), (Some(0), Some(1), Some(2)), "{errors:?}"); + assert!(app.dialog_state.aep_losses.command.is_none() && app.dialog_state.aep_losses.queue.is_empty()); + assert_eq!(app.dialog, Some(Dialog::UnsavedChanges), "the other dialog stays"); + } + + /// A new prompt keeps an unanswered one's projects: they wait behind it. + #[test] + fn a_new_prompt_keeps_an_unanswered_ones_projects() { + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + app.dialog_state.aep_losses.command = Some(("file.importAep".into(), json!({"path": "/a/One.aep"}), json!({}))); + app.dialog_state.aep_losses.queue = vec!["/a/Two.aep".into()]; + app.dialog = Some(Dialog::UnsavedChanges); + // Rec. 2020 has no After Effects counterpart: exporting asks. + std::sync::Arc::make_mut(&mut app.session.project).settings.working_space = Some(effectcraft_engine::color::ColorSpace::Rec2020); + let dir = TempDir::new(); + assert_eq!(guard(&mut app, "file.exportAep", &json!({"path": dir.0.join("out.aep").to_string_lossy()})), Ok(true)); + assert_eq!(app.dialog, Some(Dialog::AepLosses)); + assert_eq!(app.dialog_state.aep_losses.queue, ["/a/One.aep", "/a/Two.aep"]); + } + #[test] fn agents_and_consented_calls_are_not_asked() { let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); diff --git a/crates/ui-egui/src/panels/effect_controls.rs b/crates/ui-egui/src/panels/effect_controls.rs index d9706f147..6e71c6850 100644 --- a/crates/ui-egui/src/panels/effect_controls.rs +++ b/crates/ui-egui/src/panels/effect_controls.rs @@ -36,6 +36,7 @@ mod viewer; pub(crate) use common::gesture_key; pub use common::{choose_files_after_apply, reveal_applied}; +pub(crate) use panel::handle_disclosure_key; pub use panel::{show, start_rename}; pub(crate) use rows::toggle_pick; pub use viewer::viewer_hook; diff --git a/crates/ui-egui/src/panels/effect_controls/panel.rs b/crates/ui-egui/src/panels/effect_controls/panel.rs index 39fd4847a..1904600df 100644 --- a/crates/ui-egui/src/panels/effect_controls/panel.rs +++ b/crates/ui-egui/src/panels/effect_controls/panel.rs @@ -2,6 +2,39 @@ use super::common::{Actions, PARAM_NAME_X, ROW, effect_active, selected_layer}; use super::editors::{curves_editor, editor_height, histogram_only, levels_editor, object_matte_editor, roto_editor, warp_editor}; use super::rows::{FxScope, group_rows, toggle_closed}; use super::{Align2, Color32, EffectcraftApp, EvalCtx, GroupKind, Icon, Layer, PropGroup, Rect, Sense, Stroke, Tokens, json, pos2, vec2, widgets}; +use crate::dock::{PanelKind, WorkspaceExt as _}; + +/// Plain Right / Left Arrow expand / collapse the selected effects, as in After Effects, before +/// the arrows' layer nudge runs. Only while Effect Controls has the focus and is on screen: the +/// front tab of its frame, drawn last frame (a maximized frame hides every other frame and every +/// floating window), not behind Home. With no effect header selected the arrows still nudge. +pub(crate) fn handle_disclosure_key(app: &mut EffectcraftApp, key: egui::Key) -> bool { + let panel = PanelKind::EffectControls; + if app.ui.focused != panel + || app.ui.start_screen + || app.session.state.text_edit.is_some() + || !app.ui.dock.is_visible(panel) + || !app.dock_rects.iter().any(|(p, _)| *p == panel) + || !matches!(key, egui::Key::ArrowLeft | egui::Key::ArrowRight) + { + return false; + } + let Some(comp) = app.session.active_comp() else { return false }; + let Some(layer) = app.session.state.selected_layers.first().and_then(|id| comp.layer(*id)) else { return false }; + let Some(effects) = layer.effects() else { return false }; + let mut handled = false; + for g in effects.groups() { + if matches!(&g.kind, GroupKind::Effect { .. }) && app.session.state.selected_props.contains(&(layer.id, g.uid)) { + if key == egui::Key::ArrowLeft { + app.ui.fx_closed.insert(g.uid); + } else { + app.ui.fx_closed.remove(&g.uid); + } + handled = true; + } + } + handled +} // --------------------------------------------------------------------------------------------- diff --git a/crates/ui-egui/src/panels/forms.rs b/crates/ui-egui/src/panels/forms.rs index 635ef9a43..58f89e39d 100644 --- a/crates/ui-egui/src/panels/forms.rs +++ b/crates/ui-egui/src/panels/forms.rs @@ -338,11 +338,11 @@ fn interpret_footage_form(s: &effectcraft_engine::Session, proxy: bool, base: &m /// The Project Settings dialog's title and fields. fn project_settings_form(s: &effectcraft_engine::Session) -> (String, Vec) { let st = &s.project.settings; - // The catalogue row is `Mercury GPU Acceleration ({})`, so the value goes through + // The catalogue row is `GPU Acceleration ({})`, so the value goes through // tr_args: wrapping the assembled string in tr() would look up the backend's name. let gpu_label = match &s.accel { - Some(a) => tr_args("Mercury GPU Acceleration ({})", &[&a.name()]), - None => tr("Mercury GPU Acceleration (no GPU: software)").to_string(), + Some(a) => tr_args("GPU Acceleration ({})", &[&a.name()]), + None => tr("GPU Acceleration (no GPU: software)").to_string(), }; let depth = match st.bit_depth.label() { l if l.starts_with("16") => 1, @@ -436,7 +436,7 @@ fn project_settings_form(s: &effectcraft_engine::Session) -> (String, Vec Field::choice( "renderer", "Video rendering and effects", - &[(gpu_label.as_str(), json!("gpu")), ("Mercury Software Only", json!("software"))], + &[(gpu_label.as_str(), json!("gpu")), ("Software Rendering", json!("software"))], usize::from(!st.gpu_acceleration), ), ], @@ -630,9 +630,11 @@ pub fn open_form(app: &mut EffectcraftApp, id: &str, p: &Value) -> bool { )], ) } + // The time as the project displays it (timecode, or frames from Start Numbering Frames At, + // or feet + frames), which `time.set` reads back the same way. "time.set" if !has(p, &["time", "frame", "timecode"]) => { - let frame = comp.map(|c| c.frame_rate.frame_at(t)).unwrap_or(0); - ("Go to Time".into(), vec![Field::num("frame", "Frame", frame as f64)]) + let shown = comp.map(|c| crate::panels::timecode(s, c, t)).unwrap_or_default(); + ("Go to Time".into(), vec![Field::text("timecode", "Time", &shown)]) } "view.addGuide" if !has(p, &["position"]) => { let (w, _) = comp.map(|c| (c.width, c.height)).unwrap_or((1920, 1080)); @@ -1050,6 +1052,18 @@ pub fn show_view_options(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Toke mod tests { use super::*; + #[test] + fn renderer_form_keeps_descriptive_labels_and_stable_option_values() { + crate::i18n::set_current("en"); + let (_, fields) = project_settings_form(&effectcraft_engine::Session::default()); + let renderer = fields.iter().find(|field| field.key == "renderer").unwrap(); + let FieldKind::Choice { options, .. } = &renderer.kind else { panic!("renderer must be a choice") }; + assert_eq!( + options.as_slice(), + &[("GPU Acceleration (no GPU: software)".to_string(), json!("gpu")), ("Software Rendering".to_string(), json!("software"))] + ); + } + #[test] fn form_params_build_arrays_and_choices() { let f = Form { @@ -1096,6 +1110,42 @@ mod tests { assert!(app.dialog.is_none(), "one Enter confirms"); } + /// Go to Time shows the time as the project displays it, so with Frames numbered from 1 the + /// viewer's frame 00013 opens as 00013 and 00020 goes to the frame displayed 00020 (the + /// engine's frame 19). The dialog used to show and take the engine's own frame number (#3373). + #[test] + fn go_to_time_uses_the_projects_frame_numbering() { + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + app.session.execute("comp.new", json!({"name": "Main", "width": 64, "height": 36, "frameRate": 24, "duration": 4})).unwrap(); + app.session.execute("file.projectSettings", json!({"timeDisplay": "frames", "frameStart": 1})).unwrap(); + app.session.execute("time.set", json!({"frame": 12})).unwrap(); + assert!(open_form(&mut app, "time.set", &json!({}))); + let f = &app.dialog_state.form; + assert_eq!(f.command, "time.set"); + assert!(matches!(&f.fields[0].kind, FieldKind::Text(t) if t == "00013"), "{:?}", f.fields[0]); + app.dialog_state.form.fields[0].kind = FieldKind::Text("00020".into()); + let (command, params) = (app.dialog_state.form.command.clone(), app.dialog_state.form.params()); + app.session.execute(&command, params).unwrap(); + let at = app.session.active_comp().unwrap().frame_rate.frame_at(app.session.time()); + assert_eq!(at, 19, "displayed 00020 with frames starting at 1"); + } + + /// In a comp that starts 1 s before 0, Go to Time at frame 12 opens on the negative time the + /// viewer shows, and OK without editing it stays on frame 12 (it used to jump to frame 0). + #[test] + fn go_to_time_ok_keeps_a_negative_displayed_time() { + let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); + app.session.execute("comp.new", json!({"name": "Main", "width": 64, "height": 36, "frameRate": 24, "duration": 4, "startTime": -1})).unwrap(); + app.session.execute("time.set", json!({"frame": 12})).unwrap(); + assert!(open_form(&mut app, "time.set", &json!({}))); + let f = &app.dialog_state.form; + assert!(matches!(&f.fields[0].kind, FieldKind::Text(t) if t == "-0:00:00:12"), "{:?}", f.fields[0]); + let (command, params) = (f.command.clone(), f.params()); + app.session.execute(&command, params).unwrap(); + let at = app.session.active_comp().unwrap().frame_rate.frame_at(app.session.time()); + assert_eq!(at, 12, "OK on the unedited time stays put"); + } + /// File ▸ Export ▸ Adobe Premiere Pro Project… opens a form that says it writes Final Cut Pro /// XML; its fields register automation ids and OK runs `file.exportTimeline`. #[test] diff --git a/crates/ui-egui/src/panels/home.rs b/crates/ui-egui/src/panels/home.rs index 3b735e47e..601f4f1ef 100644 --- a/crates/ui-egui/src/panels/home.rs +++ b/crates/ui-egui/src/panels/home.rs @@ -475,13 +475,15 @@ fn home_page( } } +/// Run the commands the Home screen's buttons and recent files asked for, and leave Home once one +/// opened something (not while a dialog it opened waits, nor when only the list changed). fn run_actions(app: &mut EffectcraftApp, ctx: &egui::Context, actions: Vec<(&str, serde_json::Value)>) { for (id, params) in actions { let before = (app.session.path.clone(), app.session.revision); match crate::menus::invoke(app, ctx, id, params) { Err(e) => app.ui.status = e, // Asking to save the open project first: Home stays until it is answered. - Ok(r) if r.get("dialog").is_some() => {} + Ok(r) if crate::menus::waits_on_dialog(&r) => {} // Leave Home unless only the list changed or a file dialog was cancelled. Ok(_) => { let cancelled = matches!(id, "file.open" | "file.import") && (app.session.path.clone(), app.session.revision) == before; diff --git a/crates/ui-egui/src/panels/scriptui_view.rs b/crates/ui-egui/src/panels/scriptui_view.rs index 753b2884e..2e34757bf 100644 --- a/crates/ui-egui/src/panels/scriptui_view.rs +++ b/crates/ui-egui/src/panels/scriptui_view.rs @@ -329,7 +329,7 @@ fn image_texture(ctx: &egui::Context, img: &ImageRef, services: &dyn Services) - if let Some(cached) = ctx.data(|d| d.get_temp::>(id)) { return cached; } - let tex = img.bytes(services).and_then(|b| image::load_from_memory(&b).ok()).map(|d| { + let tex = img.bytes(services).and_then(|b| decode_ui_image(&b)).map(|d| { let rgba = d.to_rgba8(); let ci = egui::ColorImage::from_rgba_unmultiplied([rgba.width() as usize, rgba.height() as usize], rgba.as_raw()); ctx.load_texture(format!("scriptui-image-{}", img.name), ci, egui::TextureOptions::LINEAR) @@ -338,6 +338,21 @@ fn image_texture(ctx: &egui::Context, img: &ImageRef, services: &dyn Services) - tex } +/// The largest side of a ScriptUI image (icons and banners are far smaller). +const MAX_UI_IMAGE_SIDE: u32 = 4096; + +/// A ScriptUI image decoded within [`MAX_UI_IMAGE_SIDE`] per side and a matching allocation +/// limit, checked from the header before the pixels (and their RGBA copy) are allocated. +fn decode_ui_image(bytes: &[u8]) -> Option { + let mut r = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?; + let mut limits = image::Limits::default(); + limits.max_image_width = Some(MAX_UI_IMAGE_SIDE); + limits.max_image_height = Some(MAX_UI_IMAGE_SIDE); + limits.max_alloc = Some(u64::from(MAX_UI_IMAGE_SIDE) * u64::from(MAX_UI_IMAGE_SIDE) * 16); + r.limits(limits); + r.decode().ok() +} + /// An image control's or icon button's image, fitted (aspect kept) and centred in `r`. fn paint_image(ui: &egui::Ui, r: Rect, img: &ImageRef, services: &dyn Services) -> bool { let Some(tex) = image_texture(ui.ctx(), img, services) else { return false }; @@ -593,9 +608,8 @@ pub fn panel(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect, id: u32) { ui.data_mut(|d| d.insert_temp(told, dock)); acts.push(Act::Resize(id, dock)); } - let content = rect; - egui::ScrollArea::both().id_salt(("scriptui-panel", id)).max_width(content.width()).max_height(content.height()).show(ui, |ui| { - let (r, _) = ui.allocate_exact_size(vec2(w.root.bounds[2] as f32, w.root.bounds[3] as f32), Sense::hover()); + let window = vec2(w.root.bounds[2] as f32, w.root.bounds[3] as f32); + panel_scroll(ui, ("scriptui-panel", id), rect.size(), window, |ui, r| { draw_children(app, ui, r.min, id, &w.root, &mut acts); mouse_events(ui, r.min, r.intersect(ui.clip_rect()), id, &w.root, !modal, &mut acts); }); @@ -604,6 +618,36 @@ pub fn panel(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect, id: u32) { perform(app, &ctx, acts); } +/// The scroll area a docked ScriptUI panel shows its `window`-sized window in, at most `dock` +/// big, with the app's scroll style (solid bars, #269). `body` draws the window in the rect it is +/// given. +fn panel_scroll( + ui: &mut egui::Ui, + salt: impl std::hash::Hash + std::fmt::Debug, + dock: egui::Vec2, + window: egui::Vec2, + body: impl FnOnce(&mut egui::Ui, Rect) -> R, +) -> egui::scroll_area::ScrollAreaOutput { + egui::ScrollArea::both().id_salt(salt).max_width(dock.x).max_height(dock.y).scroll_bar_visibility(panel_bars(dock, window)).show(ui, |ui| { + let (r, _) = ui.allocate_exact_size(window, Sense::hover()); + body(ui, r) + }) +} + +/// Whether a docked panel's scroll bars may show: never while its window fits the dock. +/// +/// The fit is decided from the window and the dock, not from what the scroll area has left. A +/// solid bar, once shown, takes its width from the viewport, so a window that exactly fills its +/// dock was "too large" for the narrower viewport and its bars never went away (#2796). The fit +/// rounds the dock up to the whole point, as egui's scroll area does, because the script is told +/// its dock's size rounded (#2585) and its window can be a fraction of a point bigger. A window +/// that overflows only one way still gets both bars: the first one's width hides the edge of a +/// window laid out at the dock's full size, and the second one scrolls to it. +fn panel_bars(dock: egui::Vec2, window: egui::Vec2) -> egui::scroll_area::ScrollBarVisibility { + use egui::scroll_area::ScrollBarVisibility; + if window.x <= dock.x.ceil() && window.y <= dock.y.ceil() { ScrollBarVisibility::AlwaysHidden } else { ScrollBarVisibility::VisibleWhenNeeded } +} + /// Tab title of a script panel. pub fn panel_title(app: &EffectcraftApp, id: u32) -> Option { app.session.script_ui.window(id).map(|w| if w.title.is_empty() { w.script.clone() } else { w.title.clone() }) @@ -625,6 +669,70 @@ pub fn sync_panels(app: &mut EffectcraftApp) { mod tests { use super::*; + const DOCK: egui::Vec2 = egui::vec2(300.0, 200.0); + + /// Twelve frames of a ScriptUI panel docked in `dock`, with the app's solid scroll bars + /// (#269): its window is `first` big on the first frame and `then` big after that. The last + /// frame's scroll output (viewport and content size). + fn panel_frames(dock: egui::Vec2, first: egui::Vec2, then: egui::Vec2) -> (Rect, egui::Vec2) { + let ctx = egui::Context::default(); + ctx.all_styles_mut(|s| s.spacing.scroll = egui::style::ScrollStyle::solid()); + let screen = Rect::from_min_size(egui::Pos2::ZERO, vec2(800.0, 600.0)); + let mut last = (Rect::NOTHING, egui::Vec2::ZERO); + for frame in 0..12 { + let window = if frame == 0 { first } else { then }; + let input = egui::RawInput { screen_rect: Some(screen), time: Some(f64::from(frame) * 0.1), predicted_dt: 0.1, ..Default::default() }; + let mut out = ctx.run_ui(input, |ui| { + let mut panel = ui.new_child(UiBuilder::new().max_rect(Rect::from_min_size(egui::Pos2::ZERO, dock))); + let o = panel_scroll(&mut panel, "panel-test", dock, window, |_, _| ()); + last = (o.inner_rect, o.content_size); + }); + // Nothing paints these frames, and egui expects their texture uploads to be consumed. + out.textures_delta.clear(); + } + last + } + + /// #2796: a solid bar, once shown, took its width from the viewport, so a window that exactly + /// fills its dock stayed "too large" for what was left and kept its bars for good. A window + /// that overflowed for one frame (a dock dragged smaller before the script learns its size) + /// and then fits gets the whole dock back. + #[test] + fn a_panel_whose_window_fits_hides_its_bars() { + let (viewport, _) = panel_frames(DOCK, DOCK + vec2(40.0, 40.0), DOCK); + assert_eq!(viewport.size(), DOCK, "the bars go away once the window fits"); + } + + /// The script is told its dock's size rounded (#2585), so in a dock a fraction of a pixel + /// smaller its window is that fraction bigger. That still fits. + #[test] + fn a_panel_in_a_fractional_dock_hides_its_bars() { + let dock = vec2(299.6, 199.6); + let (viewport, _) = panel_frames(dock, dock + vec2(40.0, 40.0), DOCK); + // egui snaps rects to 1/32 of a point; a bar would take 10 points. + assert!((viewport.size() - dock).length() < 0.05, "a window within the whole pixel fits ({viewport:?})"); + } + + /// #269 holds: a window bigger than its dock keeps solid bars that take their room from the + /// panel, and scrolls. + #[test] + fn a_panel_whose_window_is_too_big_keeps_its_solid_bars() { + let big = DOCK + vec2(120.0, 80.0); + let (viewport, content) = panel_frames(DOCK, big, big); + let bar = egui::style::ScrollStyle::solid().allocated_width(); + assert_eq!(viewport.size(), DOCK - vec2(bar, bar), "solid bars on both axes"); + assert_eq!(content, big, "the whole window is scrollable"); + } + + #[test] + fn panel_bars_only_show_for_a_window_bigger_than_its_dock() { + use egui::scroll_area::ScrollBarVisibility::{AlwaysHidden, VisibleWhenNeeded}; + assert_eq!(panel_bars(DOCK, DOCK), AlwaysHidden); + assert_eq!(panel_bars(vec2(299.4, 199.4), DOCK), AlwaysHidden); + assert_eq!(panel_bars(DOCK, DOCK + vec2(1.0, 0.0)), VisibleWhenNeeded); + assert_eq!(panel_bars(DOCK, DOCK + vec2(0.0, 1.0)), VisibleWhenNeeded); + } + /// Script colours are sRGB, not linear: `#2B2B2B` drew as RGB 114 when read as linear light. #[test] fn draw_colours_are_srgb() { diff --git a/crates/ui-egui/src/panels/shortcut_editor.rs b/crates/ui-egui/src/panels/shortcut_editor.rs index e4bf313ac..80011613d 100644 --- a/crates/ui-egui/src/panels/shortcut_editor.rs +++ b/crates/ui-egui/src/panels/shortcut_editor.rs @@ -244,11 +244,13 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { let mods = if held.iter().any(|m| *m) && !st.recording { held } else { st.mods }; let table = app.session.shortcuts().clone(); + // Once per frame: the keyboard draw reads it for every key. + let bindings = table.bindings(); let presets = app.session.keymaps.names(); let active = app.session.keymaps.active.clone(); // key name → (app?, panel?) for the current modifiers. let mut keymap: std::collections::HashMap = Default::default(); - for (sc, b) in table.bindings() { + for (sc, b) in bindings.iter().copied() { let (m, k) = split(sc); if m == mods { let e = keymap.entry(k).or_default(); @@ -395,8 +397,7 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { } } if !name.starts_with('#') { - let who: Vec = - table.bindings().into_iter().filter(|(sc, _)| split(sc) == (mods, name.to_string())).map(|(_, b)| b.label.clone()).collect(); + let who: Vec = bindings.iter().copied().filter(|(sc, _)| split(sc) == (mods, name.to_string())).map(|(_, b)| b.label.clone()).collect(); if !who.is_empty() { resp.on_hover_text(who.join("\n")); } @@ -428,7 +429,7 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { p.text(info.left_top() + vec2(8.0, 6.0), Align2::LEFT_TOP, tr_args("Key: {}", &[&k]), Tokens::semibold(12.0), t.tab_text_active); let mut yy = info.min.y + 26.0; let mut list: Vec<(String, String, String)> = - table.bindings().into_iter().filter(|(sc, _)| split(sc).1 == *k).map(|(sc, b)| (sc.to_string(), b.label.clone(), b.scope.clone())).collect(); + bindings.iter().copied().filter(|(sc, _)| split(sc).1 == *k).map(|(sc, b)| (sc.to_string(), b.label.clone(), b.scope.clone())).collect(); list.sort(); if list.is_empty() { p.text(pos2(info.min.x + 8.0, yy), Align2::LEFT_TOP, tr("Not assigned"), Tokens::ui(11.5), t.text_dim); diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index d7017e7fd..4de3233e8 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -22,7 +22,9 @@ use crate::icons::{self, Icon}; use crate::theme::Tokens; use crate::{EffectcraftApp, widgets}; -#[derive(Clone, Debug)] +mod marquee; + +#[derive(Clone, Debug, PartialEq)] enum RowKind { Layer, Group { @@ -328,7 +330,7 @@ fn focus_expression(ctx: &egui::Context, prop: u64, select: [usize; 2]) { ctx.data_mut(|d| d.insert_temp(expr_focus_id(), ExprFocus { prop, select, pass })); } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] struct Row { layer: LayerId, depth: usize, @@ -586,15 +588,27 @@ pub fn snap_time(candidates: &[f64], t: f64, tolerance: f64) -> f64 { /// Times the current-time indicator snaps to (comp seconds). fn snap_candidates(comp: &Comp, rows: &[Row]) -> Vec { + snap_targets(comp, rows, &[], false) +} + +/// [`snap_candidates`] without the `edited` layers' In / Out points; when `moving` them, without +/// their markers and keys either (those move along). +fn snap_targets(comp: &Comp, rows: &[Row], edited: &[u64], moving: bool) -> Vec { + let skip = |l: LayerId, contents: bool| edited.contains(&l.0) && (moving || !contents); let mut v = vec![0.0, comp.work_area.0.seconds(), comp.work_area.1.seconds(), comp.duration.seconds()]; v.extend(comp.markers.iter().map(|m| m.time.seconds())); for l in &comp.layers { - v.push(l.in_point.seconds()); - v.push(l.out_point.seconds()); - v.extend(l.markers.iter().map(|m| l.comp_time(m.time).seconds())); + if !skip(l.id, false) { + v.push(l.in_point.seconds()); + v.push(l.out_point.seconds()); + } + if !skip(l.id, true) { + v.extend(l.markers.iter().map(|m| l.comp_time(m.time).seconds())); + } } for r in rows { if let RowKind::Prop { uid } = r.kind + && !skip(r.layer, true) && let Some(l) = comp.layer(r.layer) && let Some(p) = l.props.find(uid) { @@ -1969,8 +1983,6 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { g.max_scroll = (total_h - g.rows_rect.height() + g.rh).max(0.0); g.rows = rows; reveal_scroll(app, g); - wheel_zoom_scroll(app, ui, g); - pick_whip_edge_scroll(app, g); let snap_project = app.session.project.clone(); let snap_expr = app.session.expr.clone(); let ectx = EvalCtx { @@ -1986,6 +1998,10 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { }; let mut rw = begin_rows(app, ui, tl); let g = &mut rw; + marquee::prepare(app, ui, g); + wheel_zoom_scroll(app, ui, &g.tl); + pick_whip_edge_scroll(app, &g.tl); + marquee::scroll(app, g); draw_rows(app, ui, g, &ectx); ui.set_clip_rect(g.full_clip); background_menus(app, ui, g); @@ -1997,7 +2013,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { group_reorder_drag(ui, g); quick_offset_follow(g); key_drag_follow(app, g); - box_select(app, ui, g); + marquee::finish(app, ui, g); current_time_indicator(app, g); let footer = footer_bar(app, ui, g); footer_zoom(app, ui, g, footer); @@ -3469,44 +3485,85 @@ fn layer_quick_offset_begin(ui: &mut egui::Ui, g: &Rw, rc: &RowCtx<'_>, body: &e ctx.data(|d| d.get_temp::(quick_bar_id())).is_some_and(|q| q.layer == layer.id) } -/// Moving and trimming a layer bar by whole frames. +/// One layer bar drag: the layers it edits, the pointer's travel (px), the grabbed layer's moving +/// ends and the snap targets when it started (comp seconds), and the whole frames sent so far. +#[derive(Clone)] +struct BarDrag { + edited: Vec, + px: f64, + ends: Vec, + targets: Arc<[f64]>, + sent: i64, +} + +/// The whole frames a bar drag of `px` moves its ends by: truncated as the pointer goes, or with +/// `shift`, the move that puts one of `ends` on the nearest target within 8 px of the pointer +/// (#1828). Measured from the pointer, never the snapped result, so dragging on lets go. The +/// move stays whole frames, so an end off the frame grid lands within half a frame of its target. +fn bar_drag_frames(px: f64, pps: f64, fd: f64, ends: &[f64], targets: &[f64], shift: bool) -> i64 { + let secs = px / pps; + let snapped = shift + .then(|| { + ends.iter() + .flat_map(|e| targets.iter().map(move |t| t - e)) + .filter(|d| ((d - secs) * pps).abs() <= 8.0) + .min_by(|a, b| (a - secs).abs().total_cmp(&(b - secs).abs())) + }) + .flatten(); + // `as` saturates (NaN → 0), so a zero frame duration or zoom can't panic. + snapped.map_or((secs / fd).trunc() as i64, |d| (d / fd).round() as i64) +} + +/// Moving and trimming a layer bar by whole frames; Shift snaps its ends to the current time, +/// markers, keys, the work area and other layers' In / Out points, as in After Effects (#1828). fn layer_bar_drag(g: &mut Rw, rc: &RowCtx<'_>, bars: &BarResps, quick: bool) { let BarResps { body, lin, lout } = bars; - let pps = g.tl.pps; - let fd = g.tl.fd; let ctx = g.tl.ctx.clone(); - let actions = &mut g.tl.actions; - let selected = &g.selected; - let ui_actions = &mut g.ui_actions; let layer = rc.layer; let drag_key = format!("bar-{}", layer.id.0); for (resp, kind) in [(&body, "move"), (&lin, "in"), (&lout, "out")] { if kind == "move" && quick { continue; } + let id = egui::Id::unique(("bar-drag", layer.id.0, kind)); if resp.dragged() { - let acc_id = egui::Id::unique(("bar-acc", layer.id.0, kind)); - let mut acc: f32 = ctx.data(|d| d.get_temp(acc_id).unwrap_or(0.0)); - acc += resp.drag_delta().x; - let frames = ((acc as f64 / pps) / fd).trunc() as i64; - if frames != 0 { - acc -= (frames as f64 * fd * pps) as f32; - let d = frames as f64 * fd; - // Moving or trimming a selected layer acts on the whole - // selection; each layer trims from its own in/out point. - let targets = json!(edited_layers(layer.id, selected)); - let params = match kind { - "move" => json!({"layers": targets, "delta": d, "merge": drag_key}), - "in" => json!({"layers": targets, "inDelta": d, "merge": drag_key}), - _ => json!({"layers": targets, "outDelta": d, "merge": drag_key}), + let shift = ctx.input(|i| i.modifiers.shift); + // Moving or trimming a selected layer acts on the whole selection as it is when the + // drag starts; each layer trims from its own in/out point. A Shift-drag on an + // unselected bar adds it to the selection (`layer_bar_hits`) and moves them all. + let start = || { + let mut edited = edited_layers(layer.id, &g.selected); + if kind == "move" && shift && !g.selected.contains(&layer.id) { + edited.extend(g.selected.iter().map(|l| l.0)); + } + let (i, o) = (layer.in_point.seconds(), layer.out_point.seconds()); + let ends = match kind { + "move" => vec![i, o], + "in" => vec![i], + _ => vec![o], }; - actions.push(("layer.timing".into(), params)); + let mut targets = snap_targets(&g.tl.comp, &g.tl.rows, &edited, kind == "move"); + targets.push(g.tl.time.seconds()); + BarDrag { edited, px: 0.0, ends, targets: targets.into(), sent: 0 } + }; + let mut drag = ctx.data(|d| d.get_temp::(id)).filter(|_| !resp.drag_started()).unwrap_or_else(start); + drag.px += f64::from(resp.drag_delta().x); + let frames = bar_drag_frames(drag.px, g.tl.pps, g.tl.fd, &drag.ends, &drag.targets, shift); + if frames != drag.sent { + let d = frames.saturating_sub(drag.sent) as f64 * g.tl.fd; + drag.sent = frames; + let params = match kind { + "move" => json!({"layers": drag.edited, "delta": d, "merge": drag_key}), + "in" => json!({"layers": drag.edited, "inDelta": d, "merge": drag_key}), + _ => json!({"layers": drag.edited, "outDelta": d, "merge": drag_key}), + }; + g.tl.actions.push(("layer.timing".into(), params)); } - ctx.data_mut(|d| d.insert_temp(acc_id, acc)); + ctx.data_mut(|d| d.insert_temp(id, drag)); } if resp.drag_stopped() { - ctx.data_mut(|d| d.remove::(egui::Id::unique(("bar-acc", layer.id.0, kind)))); - ui_actions.push(UiAct::EndMerge); + ctx.data_mut(|d| d.remove::(id)); + g.ui_actions.push(UiAct::EndMerge); } } } @@ -4460,7 +4517,7 @@ fn background_menus(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw) { } } -/// Click on the empty outline (its drag is the Timeline's one box, [`box_select`]). +/// Click on the empty outline (its drag is the Timeline's one box, [`marquee`]). fn outline_marquee(ui: &mut egui::Ui, g: &mut Rw) { let actions = &mut g.tl.actions; let outline_empty = g.outline_empty.clone(); @@ -4578,7 +4635,7 @@ fn alt_scale_and_graph(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, // below the last row) deselects the layers and their keys and properties, as in After } -/// Click on empty track area (its drag is the Timeline's one box, [`box_select`]). +/// Click on empty track area (its drag is the Timeline's one box, [`marquee`]). fn graph_empty_area(ui: &mut egui::Ui, g: &mut Rw) { let actions = &mut g.tl.actions; let empty = g.empty.clone(); @@ -4797,80 +4854,6 @@ fn keys_in_box(comp: &Comp, rows: &[Row], tm: &TMap, top: f32, rh: f32, br: Rect keys } -/// What a box over the Timeline selects: the layer rows it crosses (locked layers stay out) and -/// the keys whose markers it touches. A box that reaches the outline (started on either side) -/// selects the rows it crosses; one kept inside the time graph selects keys only, as before. -/// Keys exist only in the time graph, so the box is cut at its left edge (`graph_x0`; a key -/// scrolled out under the outline isn't touched). -/// `graph_on`: the Graph Editor shows no key markers here. -#[allow(clippy::too_many_arguments)] -fn box_targets( - comp: &Comp, - rows: &[Row], - hit_rows: &[(Rect, Row)], - tm: &TMap, - top: f32, - rh: f32, - graph_x0: f32, - graph_on: bool, - br: Rect, -) -> (Vec, Vec) { - let layers = hit_rows - .iter() - .filter(|(r, row)| br.min.x < graph_x0 && matches!(row.kind, RowKind::Layer) && r.intersects(br)) - .filter(|(_, row)| comp.layer(row.layer).is_some_and(|l| !l.switches.locked)) - .map(|(_, row)| row.layer.0) - .collect(); - let in_graph = Rect::from_min_max(pos2(br.min.x.max(graph_x0), br.min.y), pos2(br.max.x, br.max.y)); - let keys = if graph_on || in_graph.width() < 0.0 { vec![] } else { keys_in_box(comp, rows, tm, top, rh, in_graph) }; - (layers, keys) -} - -/// The Timeline's one selection box, from the empty outline or the empty time graph: it spans both -/// columns as it is dragged (the pointer may cross the boundary either way) and on release selects -/// the layer rows it crosses and the keys inside it, as in After Effects. -fn box_select(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw) { - let t = g.tl.t; - let tm = g.tl.tm; - let rows_rect = g.tl.rows_rect; - let rh = g.tl.rh; - let graph_x0 = g.tl.graph_x0; - let graph_on = g.graph_on; - let ctx = g.tl.ctx.clone(); - let comp = g.tl.comp.clone(); - let rows = g.tl.rows.clone(); - let origin = [&g.outline_empty, &g.empty].into_iter().find(|r| r.dragged() || r.drag_stopped()).cloned(); - let Some(origin) = origin else { return }; - let box_of = |o: &egui::Response| { - let start = ctx.input(|i| i.pointer.press_origin()); - start.zip(o.interact_pointer_pos().or_else(|| ctx.input(|i| i.pointer.latest_pos()))).map(|(a, b)| Rect::from_two_pos(a, b).intersect(rows_rect)) - }; - let id = egui::Id::unique("tl-box"); - if origin.dragged() - && let Some(br) = box_of(&origin) - { - let bp = g.tl.p.with_clip_rect(rows_rect); - bp.rect_filled(br, 0.0, t.accent.gamma_multiply(0.12)); - bp.rect_stroke(br, 0.0, Stroke::new(1.0, t.accent), StrokeKind::Inside); - ctx.data_mut(|d| d.insert_temp(id, br)); - } - if origin.drag_stopped() - && let Some(br) = ctx.data(|d| d.get_temp::(id)) - { - ctx.data_mut(|d| d.remove::(id)); - let (layers, touched) = box_targets(&comp, &rows, &g.hit_rows, &tm, rows_rect.min.y - app.ui.timeline.scroll_y, rh, graph_x0, graph_on, br); - let mods = ui.input(|i| i.modifiers); - let add = mods.shift || mods.command; - let actions = &mut g.tl.actions; - // A box that catches nothing leaves nothing selected (layers included), unless it adds. - actions.push(("layer.select".into(), json!({"layers": layers, "add": add}))); - if !touched.is_empty() { - let keys: Vec = touched.iter().map(|k| json!({"layer": k.layer.0, "prop": k.prop, "time": k.time.seconds()})).collect(); - actions.push(("keys.select".into(), json!({"keys": keys, "add": true, "selectProperties": true}))); - } - } -} - fn current_time_indicator(app: &mut EffectcraftApp, g: &Rw) { let t = g.tl.t; let time = g.tl.time; diff --git a/crates/ui-egui/src/panels/timeline/marquee.rs b/crates/ui-egui/src/panels/timeline/marquee.rs new file mode 100644 index 000000000..d4eb56c76 --- /dev/null +++ b/crates/ui-egui/src/panels/timeline/marquee.rs @@ -0,0 +1,192 @@ +//! A marquee owns content coordinates; clipping belongs only to its painter. + +use std::sync::Arc; + +use effectcraft_engine::project::Comp; +use egui::{Id, Pos2, Rect, Stroke, StrokeKind, pos2}; +use serde_json::json; + +use super::{Row, RowKind, Rw, TMap, edge_scroll_speed, keys_in_box, row_height}; +use crate::EffectcraftApp; + +#[derive(Clone)] +struct Marquee { + owner: Id, + comp: Arc, + rows: Arc>, + graph_on: bool, + layout: (Rect, f32, f32), + anchor: Anchor, + dragging: bool, +} + +/// The pressed corner in content coordinates: a time (or an outline x) and a scroll-free y. +#[derive(Clone, Copy)] +pub(super) struct Anchor { + time: f64, + y: f32, + outline_x: Option, +} + +/// The timeline layout a marquee is projected through. +#[derive(Clone, Copy)] +pub(super) struct View { + pub tm: TMap, + /// Screen y of the first row at scroll 0. + pub top: f32, + pub graph_x0: f32, + pub graph_on: bool, +} + +impl View { + fn of(g: &Rw) -> Self { + Self { tm: g.tl.tm, top: g.tl.rows_rect.min.y, graph_x0: g.tl.graph_x0, graph_on: g.graph_on } + } +} + +fn id() -> Id { + Id::unique("tl-marquee") +} + +/// Capture on press, before wheel handling, even if the drag threshold is not crossed yet. +pub(super) fn prepare(app: &EffectcraftApp, ui: &egui::Ui, g: &Rw) { + let ctx = &g.tl.ctx; + let (pressed, down, released, escape, focused) = + ctx.input(|i| (i.pointer.primary_pressed(), i.pointer.primary_down(), i.pointer.primary_released(), i.key_pressed(egui::Key::Escape), i.focused)); + if let Some(mut drag) = ctx.data(|d| d.get_temp::(id())) { + let owned = ctx.dragged_id() == Some(drag.owner); + let dropped = released && ctx.drag_stopped_id() == Some(drag.owner); + let valid = ui.is_enabled() + && focused + && !escape + && Arc::ptr_eq(&drag.comp, &g.tl.comp) + && *drag.rows == *g.tl.rows + && drag.graph_on == g.graph_on + && drag.layout == (g.tl.rect, g.tl.rh, g.tl.graph_x0) + && (dropped || (down && (owned || (!drag.dragging && ctx.dragged_id().is_none())))); + drag.dragging |= owned; + ctx.data_mut(|d| { + if valid { + d.insert_temp(id(), drag); + } else { + d.remove::(id()); + } + }); + } + if pressed && ui.is_enabled() && focused && !escape { + let owner = [&g.outline_empty, &g.empty].into_iter().find(|r| r.is_pointer_button_down_on()); + if let Some(owner) = owner + && let Some(p) = ctx.input(|i| i.pointer.press_origin()) + { + ctx.data_mut(|d| { + d.insert_temp( + id(), + Marquee { + owner: owner.id, + comp: g.tl.comp.clone(), + rows: g.tl.rows.clone(), + graph_on: g.graph_on, + layout: (g.tl.rect, g.tl.rh, g.tl.graph_x0), + anchor: Anchor::press(&View::of(g), app.ui.timeline.scroll_y, p), + dragging: false, + }, + ); + }); + } + } +} + +/// Only the marquee owner scrolls, with the same bounded edge speed as a pick whip. +pub(super) fn scroll(app: &mut EffectcraftApp, g: &Rw) { + let ctx = &g.tl.ctx; + if let Some(drag) = ctx.data(|d| d.get_temp::(id())) + && ctx.dragged_id() == Some(drag.owner) + && ctx.input(|i| i.pointer.primary_down()) + && let Some(p) = ctx.input(|i| i.pointer.latest_pos()) + { + let speed = edge_scroll_speed(g.tl.rows_rect, p.y, g.tl.rh); + let old = app.ui.timeline.scroll_y; + app.ui.timeline.scroll_y = (old + speed * ctx.input(|i| i.stable_dt).min(0.1)).clamp(0.0, g.tl.max_scroll); + if app.ui.timeline.scroll_y != old { + ctx.request_repaint(); + } + } +} + +pub(super) struct SelectionBox { + pub keys: Rect, + pub outline: bool, +} + +impl Anchor { + /// Anchor a press at screen point `p` with the rows scrolled by `scroll`. + pub(super) fn press(v: &View, scroll: f32, p: Pos2) -> Self { + Self { time: v.tm.t(p.x.max(v.graph_x0)), y: p.y - v.top + scroll, outline_x: (p.x < v.graph_x0).then_some(p.x) } + } + + /// Reproject the content anchor without clipping the logical selection bounds. + pub(super) fn selection(&self, v: &View, scroll: f32, pointer: Pos2) -> (Rect, SelectionBox) { + let tm = v.tm; + let anchor = pos2(self.outline_x.unwrap_or_else(|| tm.x(self.time)), v.top + self.y - scroll); + let paint = Rect::from_two_pos(anchor, pointer); + let graph = self.outline_x.is_none() || pointer.x >= v.graph_x0; + let keys = + if graph && !v.graph_on { Rect::from_two_pos(pos2(tm.x(self.time), anchor.y), pos2(pointer.x.max(v.graph_x0), pointer.y)) } else { Rect::NOTHING }; + (paint, SelectionBox { keys, outline: self.outline_x.is_some() || pointer.x < v.graph_x0 }) + } +} + +/// Query all logical rows; the renderer's visible hit rows are not selection data. +pub(super) fn targets( + comp: &Comp, + rows: &[Row], + tm: &TMap, + top: f32, + rh: f32, + paint: Rect, + selection: &SelectionBox, +) -> (Vec, Vec) { + let mut layers = vec![]; + let mut y = top; + for row in rows { + let bottom = y + row_height(row, rh); + if selection.outline + && matches!(row.kind, RowKind::Layer) + && y <= paint.max.y + && bottom >= paint.min.y + && comp.layer(row.layer).is_some_and(|l| !l.switches.locked) + { + layers.push(row.layer.0); + } + y = bottom; + } + (layers, keys_in_box(comp, rows, tm, top, rh, selection.keys)) +} + +/// Commit an owned primary release; otherwise paint without changing selection. +pub(super) fn finish(app: &EffectcraftApp, ui: &egui::Ui, g: &mut Rw) { + let ctx = &g.tl.ctx; + let Some(drag) = ctx.data(|d| d.get_temp::(id())) else { return }; + let released = ctx.input(|i| i.pointer.primary_released()); + let dropped = released && ctx.drag_stopped_id() == Some(drag.owner); + if ctx.dragged_id() != Some(drag.owner) && !dropped { + return; + } + let Some(pointer) = ctx.input(|i| i.pointer.latest_pos()) else { return }; + let (paint, selection) = drag.anchor.selection(&View::of(g), app.ui.timeline.scroll_y, pointer); + if dropped { + ctx.data_mut(|d| d.remove::(id())); + let (layers, keys) = targets(&g.tl.comp, &g.tl.rows, &g.tl.tm, g.tl.rows_rect.min.y - app.ui.timeline.scroll_y, g.tl.rh, paint, &selection); + let add = ui.input(|i| i.modifiers.shift || i.modifiers.command); + g.tl.actions.push(("layer.select".into(), json!({"layers": layers, "add": add}))); + if !keys.is_empty() { + let keys: Vec<_> = keys.iter().map(|k| json!({"layer": k.layer.0, "prop": k.prop, "time": k.time.seconds()})).collect(); + g.tl.actions.push(("keys.select".into(), json!({"keys": keys, "add": true, "selectProperties": true}))); + } + } else { + let clip = if selection.outline { g.tl.rows_rect } else { g.right_clip }; + let painter = g.tl.p.with_clip_rect(clip); + painter.rect_filled(paint, 0.0, g.tl.t.accent.gamma_multiply(0.12)); + painter.rect_stroke(paint, 0.0, Stroke::new(1.0, g.tl.t.accent), StrokeKind::Inside); + } +} diff --git a/crates/ui-egui/src/panels/timeline/tests.rs b/crates/ui-egui/src/panels/timeline/tests.rs index 983d80ae6..1cd5e3423 100644 --- a/crates/ui-egui/src/panels/timeline/tests.rs +++ b/crates/ui-egui/src/panels/timeline/tests.rs @@ -434,6 +434,34 @@ fn cti_snaps_to_the_nearest_candidate() { assert!(c.contains(&comp.duration.seconds()) && c.contains(&0.0)); } +/// A dragged layer's own In / Out points are no snap targets; moved, neither are its markers +/// (#1828). +#[test] +fn bar_drags_dont_snap_to_their_own_layer() { + let mut app = app(); + let bx = app.session.active_comp().unwrap().layers.iter().find(|l| l.name == "Box").unwrap().id.0; + app.session.execute("layer.addMarker", json!({"layers": [bx], "time": 1.0})).unwrap(); + let comp = app.session.active_comp().unwrap().clone(); + let rows = build_rows(&app, &comp); + let all = snap_targets(&comp, &rows, &[], false); + let trimmed = snap_targets(&comp, &rows, &[bx], false); + let moved = snap_targets(&comp, &rows, &[bx], true); + assert_eq!(trimmed.len(), all.len() - 2, "trimming drops only the layer's In and Out"); + assert!(trimmed.contains(&1.0) && !moved.contains(&1.0), "moving drops the layer's marker: {moved:?}"); +} + +/// Shift snaps a bar drag's nearest end to a target within 8 px of the pointer; farther away, or +/// without Shift, the bar moves by the whole frames the pointer covered (#1828). +#[test] +fn bar_drags_snap_their_ends_with_shift() { + let (pps, fd, ends) = (100.0, 0.1, [2.0, 6.0]); + assert_eq!(bar_drag_frames(95.0, pps, fd, &ends, &[3.0], true), 10, "5 px short of 2 → 3 s snaps"); + assert_eq!(bar_drag_frames(95.0, pps, fd, &ends, &[3.0], false), 9, "no snap without Shift"); + assert_eq!(bar_drag_frames(88.0, pps, fd, &ends, &[3.0], true), 8, "12 px away lets go"); + assert_eq!(bar_drag_frames(104.0, pps, fd, &ends, &[3.0, 7.06], true), 11, "the target nearest the pointer wins"); + assert_eq!(bar_drag_frames(5.0, 0.0, 0.0, &ends, &[0.0], true), i64::MAX, "zero zoom saturates, no panic"); +} + /// Every string drawn by one frame of `draw`. fn drawn_text(mut draw: impl FnMut(&mut egui::Ui)) -> Vec { fn collect(shape: &egui::Shape, out: &mut Vec) { @@ -959,25 +987,33 @@ fn a_box_over_both_columns_selects_the_rows_it_crosses_and_the_keys_inside() { rows.push(Row { layer: LayerId(*id), depth: 1, kind: RowKind::Prop { uid: uid(*id) } }); } // Rows are 20 pt and 400 pt wide: the outline is left of x 200, a key at 0 s sits at x 205. - let hit_rows: Vec<(Rect, Row)> = - rows.iter().enumerate().map(|(i, r)| (Rect::from_min_size(pos2(0.0, 20.0 * i as f32), vec2(400.0, 20.0)), r.clone())).collect(); let tm = TMap { x0: 205.0, start: 0.0, pps: 100.0 }; - let go = |br: Rect| box_targets(comp, &rows, &hit_rows, &tm, 0.0, 20.0, 200.0, false, br); + let view = marquee::View { tm, top: 0.0, graph_x0: 200.0, graph_on: false }; + // Press at `from` with the rows scrolled by `s0`, release at `to` scrolled by `s1`. + let drag = |from: Pos2, s0: f32, to: Pos2, s1: f32| { + let (paint, sel) = marquee::Anchor::press(&view, s0, from).selection(&view, s1, to); + marquee::targets(comp, &rows, &tm, -s1, 20.0, paint, &sel) + }; + let go = |from: Pos2, to: Pos2| drag(from, 0.0, to, 0.0); let keyed = |ks: &[effectcraft_engine::KeyRef]| ks.iter().map(|k| k.layer.0).collect::>(); // From the outline across the boundary: layer A's row (y 0..20) and B's row, and their keys. - let (ls, ks) = go(Rect::from_min_max(pos2(10.0, 5.0), pos2(260.0, 65.0))); + let (ls, ks) = go(pos2(10.0, 5.0), pos2(260.0, 65.0)); assert_eq!(ls, [layers[0], layers[1]]); assert_eq!(keyed(&ks), [layers[0], layers[1]]); - // The same box dragged the other way is the same rectangle: the same rows, the same keys. - let (ls, ks) = go(Rect::from_two_pos(pos2(260.0, 65.0), pos2(10.0, 5.0))); + // The same box dragged the other way: the same rows, the same keys. + let (ls, ks) = go(pos2(260.0, 65.0), pos2(10.0, 5.0)); assert_eq!((ls, keyed(&ks)), (vec![layers[0], layers[1]], vec![layers[0], layers[1]])); // Only over the outline: rows, no keys; only over the graph: keys, and the rows crossed. - let (ls, ks) = go(Rect::from_min_max(pos2(10.0, 5.0), pos2(150.0, 65.0))); + let (ls, ks) = go(pos2(10.0, 5.0), pos2(150.0, 65.0)); assert_eq!((ls.len(), ks.len()), (2, 0), "a key under the outline isn't touched"); // Kept inside the time graph: keys only, no rows (a marquee over empty track area). - let (ls, ks) = go(Rect::from_min_max(pos2(204.0, 5.0), pos2(260.0, 65.0))); + let (ls, ks) = go(pos2(204.0, 5.0), pos2(260.0, 65.0)); assert_eq!((ls.len(), keyed(&ks)), (0, vec![layers[0], layers[1]])); // A locked layer is not selected as a row (the engine drops its keys in keys.select). - let (ls, _) = go(Rect::from_min_max(pos2(10.0, 5.0), pos2(260.0, 119.0))); + let (ls, _) = go(pos2(10.0, 5.0), pos2(260.0, 119.0)); + assert_eq!(ls, [layers[0], layers[1]]); + // Scrolled 20 pt mid-drag: the pressed corner stays on layer A's row (#2859), so A is still + // selected; a screen-fixed anchor would start the box below A and select only B. + let (ls, _) = drag(pos2(10.0, 5.0), 0.0, pos2(260.0, 45.0), 20.0); assert_eq!(ls, [layers[0], layers[1]]); } diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index cd0006c0f..b8fb4bb2b 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -838,9 +838,12 @@ fn phase_menu(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, v: &m // A right-click an overlay took (the Cryptomatte eyedropper's) opens the menu too. let forwarded = ui.data_mut(|d| d.remove_temp::(egui::Id::unique(FORWARDED_SECONDARY))); if let Some(pos) = resp.interact_pointer_pos().filter(|_| resp.secondary_clicked()).or(forwarded) { - // Picking an already selected layer preserves the group; an unselected hit - // becomes the target, using the same visible/unlocked hit test as left-click. - pick(app, ectx, map.to_comp(pos), false); + // The layer a left click there would select: the topmost one, keeping the group when it + // is already selected (#1535), or the selected layer whose shown path is under the + // pointer. The menu's list reaches the layers under it. + if ov::segment_at(&v.paths, &map, pos, 6.0).is_none() { + click_pick(app, ectx, map.to_comp(pos), false); + } let point = map.to_comp(pos); let hits: Vec<(u64, String)> = layers_at(ectx, point).map(|l| (l.id.0, l.name.clone())).collect(); ui.data_mut(|d| d.insert_temp(menu_hits_id, hits)); @@ -1845,15 +1848,19 @@ fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response } // The Roto Brush paints in the Layer panel only: select as usual and say so. rt if rt.roto_kind(false).is_some() => { - if pick(app, ectx, cpt, mods.shift).is_none() { + if click_pick(app, ectx, cpt, mods.shift).is_none() { let _ = app.session.execute("edit.deselectAll", json!({})); } app.ui.status = crate::i18n::tr(ROTO_HINT).to_string(); } _ => { - if pick(app, ectx, cpt, mods.shift).is_none() { + let on_path = ov::segment_at(&paths, &map, pos, 6.0).is_some(); + if on_path && !mods.shift { + // A click on a path of a selected layer stays on that layer, even where a + // layer in front covers it, so a double-click there free-transforms the path. + } else if click_pick(app, ectx, cpt, mods.shift).is_none() { let _ = app.session.execute("edit.deselectAll", json!({})); - } else if !mods.shift && !app.session.state.selected_vertices.is_empty() && ov::segment_at(&paths, &map, pos, 6.0).is_none() { + } else if !mods.shift && !app.session.state.selected_vertices.is_empty() && !on_path { // A click away from the paths, even on their layer, deselects the mask // points (the layer stays selected), as in After Effects (#513). let _ = app.session.execute("mask.selectVertices", json!({"vertices": []})); @@ -1904,7 +1911,8 @@ fn phase_dblclick(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Respons { let cpt = map.to_comp(pos); let roto = app.ui.tool.roto_kind(false).is_some(); - if let Some(l) = pick(app, ectx, cpt, false).and_then(|l| comp.layer(l)) { + // The topmost layer, as the double-click's first click selected it (#1535). + if let Some(l) = click_pick(app, ectx, cpt, false).and_then(|l| comp.layer(l)) { match &l.source { effectcraft_engine::project::LayerSource::Comp { item } => app.session.open_comp(*item), // With a Roto Brush tool, double-clicking a layer opens it in the Layer panel, @@ -2002,8 +2010,8 @@ use geometry::{ use geometry::{box_corners, project_clipped}; pub(crate) use geometry::{from_effect_space, fx2c, l2c, layer_outline, layer_quad}; use gesture::{Gesture, HANDLE, ROTO_HINT, pen_id, roi_drag, roi_handles, vertex_grab_radius}; +pub(crate) use shapes::{click_pick, layer_at, layer_at_including_locked, layers_at, pick, selectable_layers}; use shapes::{create_shape, draw_shape_ghost, shape_drag}; -pub(crate) use shapes::{layer_at, layer_at_including_locked, layers_at, pick, selectable_layers}; #[cfg(test)] use tools::constrain_45; use tools::{add_motion_key, axis_at, axis_drag, drag_axes, draw_rigs, parent_world_inverse, pen_cursor, pen_cursor_icon, pen_press, select_motion_key}; diff --git a/crates/ui-egui/src/panels/viewer/shapes.rs b/crates/ui-egui/src/panels/viewer/shapes.rs index 76892cbaf..ce9b256cd 100644 --- a/crates/ui-egui/src/panels/viewer/shapes.rs +++ b/crates/ui-egui/src/panels/viewer/shapes.rs @@ -39,10 +39,24 @@ fn quad_area(q: &[[f64; 2]; 4]) -> f64 { (0..4).map(|i| q[i][0] * q[(i + 1) % 4][1] - q[(i + 1) % 4][0] * q[i][1]).sum::().abs() / 2.0 } -/// The layer a press at a comp point acts on, selected (Shift toggles the topmost layer). As in +/// The layer a drag from a comp point acts on, selected (Shift toggles the topmost layer). As in /// After Effects, a selected layer under the point comes before the layers in front of it, so it -/// can be dragged where they cover it; otherwise the topmost layer (#230). +/// can be dragged where they cover it; otherwise the topmost layer (#230). Clicks use +/// [`click_pick`], except a Puppet tool click while no layer has pins, which uses this so a pin +/// can go on a selected layer behind others. pub(crate) fn pick(app: &mut EffectcraftApp, ectx: &EvalCtx, cpt: [f64; 2], toggle: bool) -> Option { + pick_layer(app, ectx, cpt, toggle, true) +} + +/// The layer a click (press and release without a drag) at a comp point acts on: always the +/// topmost layer, selected unless it already is (Shift toggles it). Only a drag goes to a selected +/// layer behind others ([`pick`]), so a click can still select a layer in front of it (#1535). +/// The viewer doesn't call it for a click on a shown path, which stays on the path's layer. +pub(crate) fn click_pick(app: &mut EffectcraftApp, ectx: &EvalCtx, cpt: [f64; 2], toggle: bool) -> Option { + pick_layer(app, ectx, cpt, toggle, false) +} + +fn pick_layer(app: &mut EffectcraftApp, ectx: &EvalCtx, cpt: [f64; 2], toggle: bool, prefer_selected: bool) -> Option { if toggle { let hit = layer_at(ectx, cpt)?.id; let _ = app.session.execute("layer.select", json!({"layers": [hit.0], "toggle": true})); @@ -50,7 +64,8 @@ pub(crate) fn pick(app: &mut EffectcraftApp, ectx: &EvalCtx, cpt: [f64; 2], togg } let selected = &app.session.state.selected_layers; let hits: Vec = layers_at(ectx, cpt).map(|l| l.id).collect(); - let hit = *hits.iter().find(|id| selected.contains(id)).or(hits.first())?; + let preferred = if prefer_selected { hits.iter().find(|id| selected.contains(id)) } else { None }; + let hit = *preferred.or(hits.first())?; if !selected.contains(&hit) { let _ = app.session.execute("layer.select", json!({"layers": [hit.0]})); } diff --git a/crates/ui-egui/src/panels/viewer_tools.rs b/crates/ui-egui/src/panels/viewer_tools.rs index 563657e91..162b40037 100644 --- a/crates/ui-egui/src/panels/viewer_tools.rs +++ b/crates/ui-egui/src/panels/viewer_tools.rs @@ -981,6 +981,7 @@ fn bottom_bar_contents(app: &mut EffectcraftApp, ui: &mut egui::Ui, bar: Rect, z } app.auto.add_response("viewer.timecode", &resp, "Current time"); crate::panels::toggle_time_display(app, &resp); + go_to_time_on_click(app, &resp); // Right: render time. let ms = app.frames.last_ms.lock().map(|v| *v).unwrap_or(0.0); if tr.max.x + 50.0 < bar.max.x { @@ -989,6 +990,14 @@ fn bottom_bar_contents(app: &mut EffectcraftApp, ui: &mut egui::Ui, bar: Rect, z (tr.right() + 8.0 - bar.left()).max(bar.width()) } +/// A plain click on the time display opens Go to Time, as it does in After Effects; Ctrl/Cmd+click +/// is [`crate::panels::toggle_time_display`] (#3373). +fn go_to_time_on_click(app: &mut EffectcraftApp, resp: &egui::Response) { + if resp.clicked() && !resp.ctx.input(|i| i.modifiers.command) { + crate::panels::forms::open_form(app, "time.set", &json!({})); + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/ui-egui/src/pointer_wrap.rs b/crates/ui-egui/src/pointer_wrap.rs index 53748bd7e..8fb75b478 100644 --- a/crates/ui-egui/src/pointer_wrap.rs +++ b/crates/ui-egui/src/pointer_wrap.rs @@ -9,6 +9,7 @@ struct State { numeric: Option<(Id, u64)>, offset: Vec2, pending: Option<(Pos2, Pos2)>, + #[cfg(any(not(target_arch = "wasm32"), test))] disabled: bool, } @@ -55,7 +56,11 @@ pub(crate) fn prepare_input(ctx: &Context, raw: &mut RawInput, injected: bool) - || raw.events.iter().any(|e| matches!(e, Event::WindowFocused(false) | Event::PointerButton { button: PointerButton::Primary, .. })) { let ended = state.numeric.is_some(); - *state = State { disabled: touch, ..Default::default() }; + *state = State { + #[cfg(any(not(target_arch = "wasm32"), test))] + disabled: touch, + ..Default::default() + }; return ended; } if state.numeric.is_none() { diff --git a/crates/ui-egui/src/prefs_live.rs b/crates/ui-egui/src/prefs_live.rs index 63beb3c39..af8e27856 100644 --- a/crates/ui-egui/src/prefs_live.rs +++ b/crates/ui-egui/src/prefs_live.rs @@ -75,12 +75,45 @@ pub fn dropped_files(app: &mut EffectcraftApp, ui: &egui::Ui, bounds: Rect) { if proj.len() > 1 { errors.push(crate::i18n::tr_args("Only one project can be opened per drop; skipped {} more", &[&proj.len().saturating_sub(1)])); } + // While the After Effects losses prompt waits, opening a project would replace it (and the + // project the answer imports into): the project waits for the answer, dropped again after it. + let proj = if crate::panels::aep_losses::asking(app) && !proj.is_empty() { + errors.push(crate::i18n::tr_args("Skipped {} more file(s) while a dialog is open; import them again after it", &[&1])); + Vec::new() + } else { + proj + }; - if let Some(p) = proj.first() - && let Err(e) = crate::menus::invoke(app, &ctx, "file.open", json!({"path": p})) - { - errors.push(crate::i18n::tr_args("Cannot open dropped project {}: {}", &[&p, &e])); + // After Effects projects import as projects (File ▸ Import ▸ Adobe After Effects Project…), + // each into a folder named after its file, opening its first composition. + let (aep, mut rest): (Vec, Vec) = rest.into_iter().partition(|p| effectcraft_engine::commands::is_aep_path(p)); + // A project in the drop changes the active composition: the other files go to the Project + // panel only, not into a composition. + let has_project = !proj.is_empty() || !aep.is_empty(); + + // Opened first, so the other files land in the project it opens. While it waits on Save + // changes? the rest of the drop is skipped with a note: it would land in the project about + // to be replaced. + let mut waiting = false; + if let Some(p) = proj.first() { + match crate::menus::invoke(app, &ctx, "file.open", json!({"path": p})) { + Ok(r) => waiting = crate::menus::waits_on_dialog(&r), + Err(e) => errors.push(crate::i18n::tr_args("Cannot open dropped project {}: {}", &[&p, &e])), + } } + let mut aep = aep; + if waiting { + let skipped = rest.len() + aep.len(); + if skipped > 0 { + errors.push(crate::i18n::tr_args("Skipped {} more file(s) while a dialog is open; import them again after it", &[&skipped])); + } + rest.clear(); + aep.clear(); + } + + // One with losses asks first; the projects after it wait for the answer, the other files + // import meanwhile. The errors name their files. + errors.extend(crate::menus::import_aep_projects(app, aep)); if !rest.is_empty() { let viewer = app @@ -101,7 +134,7 @@ pub fn dropped_files(app: &mut EffectcraftApp, ui: &egui::Ui, bounds: Rect) { .is_some_and(|e| Rect::from_min_size(egui::pos2(e.rect[0], e.rect[1]), egui::vec2(e.rect[2], e.rect[3])).contains(p)) }); - let at = if proj.is_empty() && !over_timeline { + let at = if !has_project && !over_timeline { ctx.input(|i| i.pointer.hover_pos()).filter(|p| viewer.is_some_and(|v| v.contains(*p))).and_then(|p| crate::panels::viewer::screen_to_comp(&ctx, p)) } else { None @@ -116,11 +149,13 @@ pub fn dropped_files(app: &mut EffectcraftApp, ui: &egui::Ui, bounds: Rect) { let mut params = json!({ "paths": rest, "drag": true, - "addToComp": at.is_some() || over_timeline, + "addToComp": !has_project && (at.is_some() || over_timeline), "position": at, "background": true }); - if !layered { + // While the losses prompt of a project dropped with it shows, a Photoshop file imports + // with Default Drag Import As: one dialog at a time. + if !layered || crate::panels::aep_losses::asking(app) { params["importAs"] = json!(app.session.prefs.drag_import_as()); } if alt { diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 60a30e97e..635b46acd 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -756,7 +756,7 @@ pub struct UiState { /// Flowchart panel options (layers, effects, solids, direction, root comp). #[serde(default)] pub flowchart: crate::panels::flowchart::FlowOptions, - /// Lumetri Scopes panel options. + /// Video Scopes panel options. #[serde(default)] pub scopes: crate::panels::scopes_panel::ScopesState, } @@ -806,7 +806,7 @@ impl Default for UiState { home_learn: false, home_templates: false, align_to_selection: false, - cache_when_idle: false, + cache_when_idle: true, tracker_source: None, layer_panel: None, layer_view: None, diff --git a/crates/ui-egui/src/updates_ui.rs b/crates/ui-egui/src/updates_ui.rs index bb8187f56..316e34ac4 100644 --- a/crates/ui-egui/src/updates_ui.rs +++ b/crates/ui-egui/src/updates_ui.rs @@ -91,6 +91,7 @@ pub(crate) fn show(app: &mut EffectcraftApp, ctx: &egui::Context) { rt.updater.dismiss_status(); } CheckResultResponse::CheckAgain => rt.updater.check_now(ctx), + CheckResultResponse::RestartToUpdate => rt.updater.restart_to_update(), } // A check that found a release leaves the status idle and the startup prompt takes over. if matches!(rt.updater.status(), Status::Idle) && !rt.check_requested { diff --git a/crates/ui-egui/tests/flatpak_updates.rs b/crates/ui-egui/tests/flatpak_updates.rs new file mode 100644 index 000000000..908c24a23 --- /dev/null +++ b/crates/ui-egui/tests/flatpak_updates.rs @@ -0,0 +1,27 @@ +//! Release and source Flatpaks must allow release checks and host update helpers. +fn finish_args(manifest: &str) -> Vec<&str> { + manifest + .split("finish-args:") + .nth(1) + .unwrap_or_default() + .lines() + .take_while(|line| line.is_empty() || line.starts_with(' ') || line.starts_with('#')) + .filter_map(|line| line.trim().strip_prefix("- ")) + .map(|arg| arg.trim_matches(['\'', '"'])) + .collect() +} + +#[test] +fn flatpak_update_permissions() { + let manifests = [ + include_str!("../../../packaging/linux/flatpak/ai.storyteller.effectcraft.bundle.yml"), + include_str!("../../../packaging/linux/flatpak/ai.storyteller.effectcraft.yml"), + ]; + let args = manifests.map(finish_args); + assert_eq!(args[0], args[1], "source and release permissions must agree"); + for permissions in args { + for required in ["--share=network", "--talk-name=org.freedesktop.Flatpak"] { + assert!(permissions.contains(&required), "missing update permission: {required}"); + } + } +} diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index 422f0223e..ca509e064 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -82,6 +82,8 @@ mod ui_roto_objects; mod ui_roto_span_confidence; mod ui_script_console; mod ui_script_console_keyboard; +#[cfg(not(target_arch = "wasm32"))] +mod ui_script_render; mod ui_scriptui; mod ui_scrolling; mod ui_self_drop; diff --git a/crates/ui-egui/tests/it/ui_angles.rs b/crates/ui-egui/tests/it/ui_angles.rs index 007e11286..40ad17bc1 100644 --- a/crates/ui-egui/tests/it/ui_angles.rs +++ b/crates/ui-egui/tests/it/ui_angles.rs @@ -38,7 +38,7 @@ fn harness(panel: Panel, angle: f64) -> (Harness<'static, EffectcraftApp>, u64, s.execute("comp.new", json!({"name": "Angles", "width": 320, "height": 180, "duration": 4})).unwrap(); let layer = s.execute("layer.newSolid", json!({"name": "Plate", "color": "#40b080"})).unwrap()["layer"].as_u64().unwrap(); let uid = if matches!(panel, Panel::EffectControls) { - let fx = s.execute("effect.apply", json!({"layer": layer, "effect": "CC Bend It"})).unwrap()["effects"][0].as_u64().unwrap(); + let fx = s.execute("effect.apply", json!({"layer": layer, "effect": "Two-Point Bend"})).unwrap()["effects"][0].as_u64().unwrap(); s.active_comp().unwrap().layer(LayerId(layer)).unwrap().props.find_group(fx).unwrap().get("bend").unwrap().uid } else { s.active_comp().unwrap().layer(LayerId(layer)).unwrap().props.prop("transform/rotation").unwrap().uid diff --git a/crates/ui-egui/tests/it/ui_audio_scrub.rs b/crates/ui-egui/tests/it/ui_audio_scrub.rs index 80936042a..ff5f124e8 100644 --- a/crates/ui-egui/tests/it/ui_audio_scrub.rs +++ b/crates/ui-egui/tests/it/ui_audio_scrub.rs @@ -215,7 +215,7 @@ fn preview_with_audio_holds_the_first_frame_and_sounds_from_it() { let gate = GatedFrames::default(); let renderer: Arc = Arc::new(gate.clone()); app.frames.set_remote(Some(renderer)); - let ahead = (app.frames.parallelism() * 2).max(4) as i64; + let ahead = (app.frames.lanes() * 2).max(4) as i64; app.session.execute("comp.settings", json!({"comp": cid.0, "duration": (4 * ahead) as f64 / 30.0})).unwrap(); let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app); h.state_mut().use_manual_clock(); @@ -258,7 +258,7 @@ fn preview_with_audio_sounds_without_caching_the_work_area() { h.state_mut().use_manual_clock(); h.run_steps(2); gate.discard(&mut h); - let ahead = (h.state().frames.parallelism() * 2).max(4) as i64; + let ahead = (h.state().frames.lanes() * 2).max(4) as i64; let now = h.state().now(); h.state_mut().play(now); let frames: Vec = (0..ahead).collect(); @@ -439,7 +439,7 @@ fn until_started(h: &mut Harness<'_, EffectcraftApp>) -> bool { fn startup_finishes_at_any_ram_preview_budget() { let probe = Fake::default(); let (app, _) = setup(true, &probe); - let ahead = (app.frames.parallelism() * 2).max(4) as i64; + let ahead = (app.frames.lanes() * 2).max(4) as i64; // A 64×36 frame is 9216 bytes; `fit` frames fit in (fit + ½) frames' worth / 0.9. let mut fits = vec![2, 3, 4, ahead - 1, ahead, ahead + 1]; fits.sort(); @@ -505,7 +505,7 @@ fn extended_viewer(fake: &Fake) -> (Harness<'static, EffectcraftApp>, ItemId, [u fn startup_finishes_when_extended_viewer_frames_are_larger() { let fake = Fake::default(); let (mut h, cid, size) = extended_viewer(&fake); - let ahead = (h.state().frames.parallelism() * 2).max(4); + let ahead = (h.state().frames.lanes() * 2).max(4); let (comp_bytes, real_bytes) = (160 * 120 * 4, size[0] * size[1] * 4); // Room for `ahead` comp-sized frames (and a half), which is fewer real ones. let budget = ((ahead as f64 + 0.5) * comp_bytes as f64 / 0.9) as usize; @@ -739,3 +739,206 @@ fn adding_markers_does_not_stop_the_sound() { } assert_eq!(h.state().session.project.comp(cid).unwrap().markers.len(), 6); } + +/// #2104: unmuting a preview that started silent opens sound at the moving preview position, +/// both with video and with Video off (where the timeline deliberately stays still). +#[test] +fn unmuting_a_silent_preview_starts_audio_at_its_current_frame() { + for video in [false, true] { + let fake = Fake::default(); + let (mut app, cid) = setup(true, &fake); + let layer = app.session.project.comp(cid).unwrap().layers[0].id.0; + app.session.execute("layer.setSwitch", json!({"layers": [layer], "switch": "audio", "value": false})).unwrap(); + let gate = GatedFrames::default(); + app.frames.set_remote(Some(Arc::new(gate.clone()))); + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_step_dt(1.0 / 30.0).build_eframe(|_| app); + h.state_mut().use_manual_clock(); + h.run_steps(2); + gate.render(&mut h, cid, &(0..60).collect::>()); + let now = h.state().now(); + let shortcut = if video { h.state().session.prefs.preview.current } else { effectcraft_engine::preview::PreviewShortcut::AltNumpad0 }; + h.state_mut().play_with(now, shortcut); + for _ in 0..6 { + step(&mut h); + } + assert!(h.state().audio.is_none(), "muted playback has no output"); + let current = if video { frame(&h, cid) } else { h.state().playback.audio_frame.unwrap() }; + assert!(current > 0, "the silent preview advanced"); + h.state_mut().session.execute("layer.setSwitch", json!({"layers": [layer], "switch": "audio", "value": true})).unwrap(); + gate.render(&mut h, cid, &(0..60).collect::>()); + h.step(); + assert_eq!(audio_frame(&h, cid), Some(current), "unmute starts at the moving preview frame, video={video}"); + if !video { + assert_eq!(frame(&h, cid), 0, "audio-only preview leaves the timeline stationary"); + } + let feed = fake.0.lock().unwrap().0.clone().expect("unmute opened the output"); + assert!(pull(&feed, 256).iter().any(|v| v.abs() > 0.01), "the unmuted tone reaches the output"); + // Mute and unmute again without stopping: edits reach the existing feed too. + h.state_mut().session.execute("layer.setSwitch", json!({"layers": [layer], "switch": "audio", "value": false})).unwrap(); + h.step(); + assert!(pull(&feed, 256).iter().all(|v| *v == 0.0), "mute silences the current output"); + h.state_mut().session.execute("layer.setSwitch", json!({"layers": [layer], "switch": "audio", "value": true})).unwrap(); + h.step(); + assert!(pull(&feed, 256).iter().any(|v| v.abs() > 0.01), "a second unmute reaches the same output"); + h.state_mut().stop(); + } +} + +/// Pull only queued frames: underruns are deliberately excluded so the test measures the +/// feeder's composition mapping, not how quickly a loaded test host schedules its thread. +fn take_preview_samples(app: &EffectcraftApp, frames: usize) -> Vec { + let audio = app.audio.as_ref().expect("preview audio opened"); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60); + let mut samples = Vec::with_capacity(frames * 2); + while samples.len() < frames * 2 { + let n = audio.feed.queued_frames().min(frames - samples.len() / 2); + if n == 0 { + assert!(!audio.finished(), "the preview ended before the requested samples"); + assert!(std::time::Instant::now() < deadline, "audio feeder timed out"); + std::thread::sleep(std::time::Duration::from_millis(1)); + continue; + } + let mut block = vec![0.0; n * 2]; + audio.feed.pull(&mut block); + samples.extend(block); + } + samples +} + +#[test] +fn preview_rate_audio_sounds_at_the_selected_speed_and_finishes_at_the_end() { + use effectcraft_engine::preview::{PlayFrom, PreviewRange}; + for (comp_fps, preview_fps, duration_frames) in [(60.0, 30.0, 96_000), (30.0, 60.0, 24_000)] { + let fake = Fake::default(); + let (mut app, cid) = setup_long(true, &fake, 1.0); + app.session.execute("comp.settings", json!({"comp": cid.0, "frameRate": comp_fps})).unwrap(); + let p = &mut app.session.prefs.preview.spacebar; + p.include_video = false; + p.frame_rate = Some(preview_fps); + p.loop_ = false; + p.range = PreviewRange::EntireDuration; + p.play_from = PlayFrom::RangeStart; + app.play(0.0); + assert!(app.audio.is_some(), "a frame-rate override must not mute the preview"); + let samples = take_preview_samples(&app, 12_000); + assert!(samples.iter().any(|v| v.abs() > 0.01)); + let audio = app.audio.as_ref().unwrap(); + assert_eq!(audio.rate, 48_000, "the physical output rate is unchanged"); + assert!((audio.clock_time().seconds() - 0.25 * preview_fps / comp_fps).abs() < 1e-9); + take_preview_samples(&app, duration_frames - 12_000); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60); + while !app.audio.as_ref().unwrap().finished() { + assert!(std::time::Instant::now() < deadline, "one-shot audio did not finish"); + std::thread::sleep(std::time::Duration::from_millis(1)); + } + assert!(app.audio.as_ref().unwrap().clock_time().seconds() < 1.0); + app.stop(); + } +} + +#[test] +fn preview_rate_audio_resamples_the_sound_instead_of_only_changing_the_clock() { + use effectcraft_engine::preview::{PlayFrom, PreviewRange}; + let render = |preview_fps, frames| { + let fake = Fake::default(); + let (mut app, _) = setup(true, &fake); + let p = &mut app.session.prefs.preview.spacebar; + p.include_video = false; + p.frame_rate = Some(preview_fps); + p.range = PreviewRange::EntireDuration; + p.play_from = PlayFrom::RangeStart; + app.session.prefs.audio.preview_sample_rate = 48_000; + app.play(0.0); + let samples = take_preview_samples(&app, frames); + app.stop(); + samples + }; + let normal = render(30.0, 2048); + let slow = render(15.0, 4096); + for (original, slowed) in normal.as_chunks::<2>().0.iter().zip(slow.as_chunks::<4>().0) { + assert!((original[0] - slowed[0]).abs() < 1e-5); + assert!((original[1] - slowed[1]).abs() < 1e-5); + } +} + +#[test] +fn preview_rate_audio_drives_video_at_half_speed_and_loops() { + let fake = Fake::default(); + let (mut app, cid) = setup_long(true, &fake, 1.0); + app.session.execute("comp.settings", json!({"comp": cid.0, "frameRate": 60})).unwrap(); + app.session.prefs.preview.spacebar.frame_rate = Some(30.0); + app.session.prefs.preview.spacebar.loop_ = true; + let gate = GatedFrames::default(); + app.frames.set_remote(Some(Arc::new(gate.clone()))); + let ahead = (app.frames.parallelism() * 2).max(4) as i64; + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_| app); + h.state_mut().use_manual_clock(); + h.run_steps(2); + gate.discard(&mut h); + h.state_mut().play(0.0); + assert!(h.state().playback.starting, "mismatched-rate audio waits with the first picture"); + let frames: Vec = (0..ahead.min(60)).collect(); + gate.render(&mut h, cid, &frames); + step(&mut h); + assert!(h.state().audio.is_some()); + take_preview_samples(h.state(), 4800); + step(&mut h); + assert_eq!(frame(&h, cid), 3, "0.1 output seconds is 0.05 composition seconds"); + take_preview_samples(h.state(), 96_000 - 4800); + step(&mut h); + assert_eq!(frame(&h, cid), 0, "the audio and picture wrap together"); + assert!(!h.state().audio.as_ref().unwrap().finished()); + h.state_mut().stop(); +} + +#[test] +fn preview_rate_audio_keeps_the_include_audio_switch_and_skip_independent() { + let fake = Fake::default(); + let (mut app, _) = setup(true, &fake); + app.session.prefs.preview.spacebar.include_audio = false; + app.session.prefs.preview.spacebar.frame_rate = Some(60.0); + app.play(0.0); + assert!(app.audio.is_none() && !app.playback.audio_held && !app.playback.starting); + app.stop(); + let p = &mut app.session.prefs.preview.spacebar; + p.include_audio = true; + p.include_video = false; + p.skip = 2; + app.play(0.0); + take_preview_samples(&app, 12_000); + assert_eq!(app.audio.as_ref().unwrap().clock_time(), Tick::from_seconds_f64(0.5), "Skip drops pictures, not audio or time"); + app.stop(); +} + +/// Live edits keep the consumed output cursor at either speed, including a nonzero start. +#[test] +fn preview_rate_audio_keeps_its_position_through_live_mute_and_unmute() { + use effectcraft_engine::preview::{PlayFrom, PreviewRange}; + for preview_fps in [15.0, 60.0] { + let fake = Fake::default(); + let (mut app, cid) = setup(true, &fake); + let comp = app.session.project.comp(cid).unwrap(); + let layer = comp.layers[0].id.0; + let start = comp.frame_rate.tick_of(17); + app.session.set_time(start); + let p = &mut app.session.prefs.preview.spacebar; + p.include_video = false; + p.frame_rate = Some(preview_fps); + p.range = PreviewRange::EntireDuration; + p.play_from = PlayFrom::CurrentTime; + app.play(0.0); + assert_eq!(app.audio.as_ref().unwrap().clock_time(), start); + for (edit, audible) in [(None, true), (Some(false), false), (Some(true), true)] { + if let Some(value) = edit { + app.session.execute("layer.setSwitch", json!({"layers": [layer], "switch": "audio", "value": value})).unwrap(); + app.audio.as_mut().unwrap().follow_project(app.session.project.clone()); + } + let before = app.audio.as_ref().unwrap().clock_time(); + let samples = take_preview_samples(&app, 2400); + assert_eq!(samples.iter().any(|v| v.abs() > 0.01), audible); + let elapsed = (app.audio.as_ref().unwrap().clock_time() - before).seconds(); + assert!((elapsed - 0.05 * preview_fps / 30.0).abs() < 1e-9, "live edit changed the preview cursor"); + } + app.stop(); + } +} diff --git a/crates/ui-egui/tests/it/ui_color_hex.rs b/crates/ui-egui/tests/it/ui_color_hex.rs index 4f8a8ab14..8a5a39b8e 100644 --- a/crates/ui-egui/tests/it/ui_color_hex.rs +++ b/crates/ui-egui/tests/it/ui_color_hex.rs @@ -1,4 +1,4 @@ -//! The colour picker every colour input opens: its hex field sets an effect's colour (CC Toner), +//! The colour picker every colour input opens: its hex field sets an effect's colour (Multi-Tone Tint), //! the dialogs' colour button stores sRGB (egui's own `color_edit_button_rgba` read the stored //! sRGB values as linear, so mid-grey `#808080` was stored as `#373737`), the square, slider and //! HSB / RGB fields set the colour, and Cancel / Escape / the original swatch restore it. @@ -45,7 +45,7 @@ fn hex_code_sets_an_effect_colour() { let mut s = Session::default(); s.execute("comp.new", json!({"name": "Hex", "width": 320, "height": 180, "duration": 4})).unwrap(); let layer = s.execute("layer.newSolid", json!({"name": "Plate", "color": "#40b080"})).unwrap()["layer"].as_u64().unwrap(); - let fx = s.execute("effect.apply", json!({"layer": layer, "effect": "CC Toner"})).unwrap()["effects"][0].as_u64().unwrap(); + let fx = s.execute("effect.apply", json!({"layer": layer, "effect": "Multi-Tone Tint"})).unwrap()["effects"][0].as_u64().unwrap(); let uid = s.active_comp().unwrap().layer(LayerId(layer)).unwrap().props.find_group(fx).unwrap().get("midtones").unwrap().uid; s.state.selected_layers = vec![LayerId(layer)]; let mut app = EffectcraftApp::new(s); diff --git a/crates/ui-egui/tests/it/ui_comp_presets.rs b/crates/ui-egui/tests/it/ui_comp_presets.rs index 3fda8a8cc..93cd02030 100644 --- a/crates/ui-egui/tests/it/ui_comp_presets.rs +++ b/crates/ui-egui/tests/it/ui_comp_presets.rs @@ -174,6 +174,8 @@ fn composition_settings_view_stays_put_when_it_scrolls() { #[test] fn composition_settings_refits_when_the_language_changes() { let mut s = Session::default(); + // Idle caching keeps repainting while it renders; this test waits for repaints to stop. + s.prefs.previews.cache_frames_when_idle = false; s.execute("comp.new", json!({"name": "Edit", "width": 2560, "height": 1440, "frameRate": 60, "duration": 4})).unwrap(); let mut h = Harness::builder() .with_size(egui::vec2(1600.0, 1000.0)) diff --git a/crates/ui-egui/tests/it/ui_crypto_pick.rs b/crates/ui-egui/tests/it/ui_crypto_pick.rs index 0d1767c7e..03e49ca18 100644 --- a/crates/ui-egui/tests/it/ui_crypto_pick.rs +++ b/crates/ui-egui/tests/it/ui_crypto_pick.rs @@ -277,84 +277,103 @@ fn eyedropper_arms_hovers_picks_with_modifiers_and_stays_armed_until_esc() { let _ = std::fs::remove_dir_all(&ids.dir); } -/// Window and panel sizes × display scales × viewer zoom / pan × Resolution (and a proxy): hover -/// and click name the ID of the pixel the viewer shows, next to cell edges too. Each hover is -/// checked against the rendered matte of the named ID at the point the screen maps back to. +// Window and panel sizes × display scales, one test each so they run in parallel. Together they +// took ~90 s as one test, and ~10 min under coverage, where that one test set the slowest shard. #[test] -fn picks_what_the_viewer_shows_across_sizes_scales_zoom_pan_resolution_and_proxies() { +fn picks_what_the_viewer_shows_at_900x640_and_1x() { + picks_what_the_viewer_shows(0, vec2(900.0, 640.0), 1.0, 300.0); +} + +#[test] +fn picks_what_the_viewer_shows_at_1280x720_and_1_25x() { + picks_what_the_viewer_shows(1, vec2(1280.0, 720.0), 1.25, 420.0); +} + +#[test] +fn picks_what_the_viewer_shows_at_1600x1000_and_1_5x() { + picks_what_the_viewer_shows(2, vec2(1600.0, 1000.0), 1.5, 360.0); +} + +#[test] +fn picks_what_the_viewer_shows_at_2400x1400_and_2x() { + picks_what_the_viewer_shows(3, vec2(2400.0, 1400.0), 2.0, 600.0); +} + +/// Case `i`: a window of `size` at display scale `ppp`, with an Effect Controls panel `ec` wide, +/// × viewer zoom / pan × Resolution (and a proxy). Hover and click name the ID of the pixel the +/// viewer shows, next to cell edges too. Each hover is checked against the rendered matte of the +/// named ID at the point the screen maps back to. Case 0 also checks frames from the idle cache. +fn picks_what_the_viewer_shows(i: usize, size: egui::Vec2, ppp: f32, ec: f32) { let mut checked = 0; let mut failures = vec![]; - let cases = [(vec2(900.0, 640.0), 1.0, 300.0), (vec2(1280.0, 720.0), 1.25, 420.0), (vec2(1600.0, 1000.0), 1.5, 360.0), (vec2(2400.0, 1400.0), 2.0, 600.0)]; - for (i, (size, ppp, ec)) in cases.into_iter().enumerate() { - let (s, ids) = crypto_session(&format!("matrix{i}"), 1919, 1079, false); - let mut h = harness(s, size, ppp, ec); - h.state_mut().session.execute_checked("prop.set", json!({"path": "effects/#1/matteOnly", "value": true})).unwrap(); - arm(&mut h, &ids); - for proxy in [false, true] { - if proxy { - let p = ids.dir.join("proxy").join("plate_proxy.exr"); - h.state_mut().session.execute("file.setProxy", json!({"item": ids.footage, "path": p.to_string_lossy()})).unwrap(); - } - for res in [Resolution::Full, Resolution::Half, Resolution::Third, Resolution::Quarter, Resolution::Custom(6)] { - for (zoom, pan) in [(None, [0.0, 0.0]), (Some(0.5), [0.0, 0.0]), (Some(2.0), [-300.0, 120.0])] { - let cid = h.state().session.active_comp_id().unwrap(); - h.state_mut().session.set_resolution(cid, res).unwrap(); - h.state_mut().ui.viewer.zoom = zoom.map(|z: f32| z / ppp); - h.state_mut().ui.viewer.pan = pan; - settle(&mut h); - let area = h.state().auto.find("viewer.fxPick").unwrap().rect; - let scale = shown_scale(&h); - // Points next to cell edges, in the middle and at the far corner. - // (Cell edges far from the origin: 925, 1044 shows the 1/3 vs 0.333 rounding.) - for p in [[36.6, 28.7], [37.4, 29.3], [959.7, 539.2], [1918.4, 1078.6], [74.1, 57.9], [925.4, 1044.4], [1110.6, 1043.6]] { - let Some(sp) = comp_to_screen(&h.ctx, [p[0] as f32, p[1] as f32]) else { continue }; - if sp.x < area[0] + 4.0 || sp.y < area[1] + 40.0 || sp.x > area[0] + area[2] - 4.0 || sp.y > area[1] + area[3] - 4.0 { - continue; - } - let got = hover(&mut h, sp); - let got = got.trim_end_matches(" (selected)").to_string(); - // The comp point under the cursor, as the viewer maps it back. - let q = to_comp(&h, sp); - checked += 1; - // The named ID has the largest coverage of the shown pixel (a blended edge - // pixel of a proxy at Quarter has several). No ID named: the frame shows - // (almost) no matte there either (an odd size's last half-resolution pixel - // samples just outside the layer). - let ok = if got.starts_with('c') { largest(&mut h, &got, q, scale) } else { got == HINT && lit(&mut h, "*", q, scale) < 0.5 }; - if !ok { - failures.push(format!( - "{size:?} ppp {ppp} proxy {proxy} {res:?} zoom {zoom:?} (scale {scale}): at {q:?} hover {got:?} is not lit there" - )); - } + let (s, ids) = crypto_session(&format!("matrix{i}"), 1919, 1079, false); + let mut h = harness(s, size, ppp, ec); + h.state_mut().session.execute_checked("prop.set", json!({"path": "effects/#1/matteOnly", "value": true})).unwrap(); + arm(&mut h, &ids); + for proxy in [false, true] { + if proxy { + let p = ids.dir.join("proxy").join("plate_proxy.exr"); + h.state_mut().session.execute("file.setProxy", json!({"item": ids.footage, "path": p.to_string_lossy()})).unwrap(); + } + for res in [Resolution::Full, Resolution::Half, Resolution::Third, Resolution::Quarter, Resolution::Custom(6)] { + for (zoom, pan) in [(None, [0.0, 0.0]), (Some(0.5), [0.0, 0.0]), (Some(2.0), [-300.0, 120.0])] { + let cid = h.state().session.active_comp_id().unwrap(); + h.state_mut().session.set_resolution(cid, res).unwrap(); + h.state_mut().ui.viewer.zoom = zoom.map(|z: f32| z / ppp); + h.state_mut().ui.viewer.pan = pan; + settle(&mut h); + let area = h.state().auto.find("viewer.fxPick").unwrap().rect; + let scale = shown_scale(&h); + // Points next to cell edges, in the middle and at the far corner. + // (Cell edges far from the origin: 925, 1044 shows the 1/3 vs 0.333 rounding.) + for p in [[36.6, 28.7], [37.4, 29.3], [959.7, 539.2], [1918.4, 1078.6], [74.1, 57.9], [925.4, 1044.4], [1110.6, 1043.6]] { + let Some(sp) = comp_to_screen(&h.ctx, [p[0] as f32, p[1] as f32]) else { continue }; + if sp.x < area[0] + 4.0 || sp.y < area[1] + 40.0 || sp.x > area[0] + area[2] - 4.0 || sp.y > area[1] + area[3] - 4.0 { + continue; + } + let got = hover(&mut h, sp); + let got = got.trim_end_matches(" (selected)").to_string(); + // The comp point under the cursor, as the viewer maps it back. + let q = to_comp(&h, sp); + checked += 1; + // The named ID has the largest coverage of the shown pixel (a blended edge + // pixel of a proxy at Quarter has several). No ID named: the frame shows + // (almost) no matte there either (an odd size's last half-resolution pixel + // samples just outside the layer). + let ok = if got.starts_with('c') { largest(&mut h, &got, q, scale) } else { got == HINT && lit(&mut h, "*", q, scale) < 0.5 }; + if !ok { + failures + .push(format!("{size:?} ppp {ppp} proxy {proxy} {res:?} zoom {zoom:?} (scale {scale}): at {q:?} hover {got:?} is not lit there")); } } } - // Frames from Cache Frames When Idle (the first case): rendered on the grid the - // viewer renders and the pick reads (1/3, not the key's 0.333), so frame 20 from the - // idle cache is frame 0 as the viewer rendered it, and hovers there are right too. - if i == 0 && !proxy { - (h.state_mut().ui.viewer.zoom, h.state_mut().ui.viewer.pan) = (None, [0.0, 0.0]); - for res in [Resolution::Third, Resolution::Custom(6)] { - let fails = idle_cached_frames_are_shown_frames(&mut h, res); - checked += 1; - failures.extend(fails); - } - } - // A click picks what the hover named. + } + // Frames from Cache Frames When Idle (the first case): rendered on the grid the + // viewer renders and the pick reads (1/3, not the key's 0.333), so frame 20 from the + // idle cache is frame 0 as the viewer rendered it, and hovers there are right too. + if i == 0 && !proxy { (h.state_mut().ui.viewer.zoom, h.state_mut().ui.viewer.pan) = (None, [0.0, 0.0]); - settle(&mut h); - let sp = comp_to_screen(&h.ctx, [959.7, 539.2]).unwrap(); - let named = hover(&mut h, sp); - click(&mut h, sp, Modifiers::NONE); - assert_eq!(selection(&h, &ids), named.trim_end_matches(" (selected)")); - if proxy { - h.state_mut().session.execute("file.setProxy", json!({"item": ids.footage, "path": null})).ok(); + for res in [Resolution::Third, Resolution::Custom(6)] { + let fails = idle_cached_frames_are_shown_frames(&mut h, res); + checked += 1; + failures.extend(fails); } } - let _ = std::fs::remove_dir_all(&ids.dir); + // A click picks what the hover named. + (h.state_mut().ui.viewer.zoom, h.state_mut().ui.viewer.pan) = (None, [0.0, 0.0]); + settle(&mut h); + let sp = comp_to_screen(&h.ctx, [959.7, 539.2]).unwrap(); + let named = hover(&mut h, sp); + click(&mut h, sp, Modifiers::NONE); + assert_eq!(selection(&h, &ids), named.trim_end_matches(" (selected)")); + if proxy { + h.state_mut().session.execute("file.setProxy", json!({"item": ids.footage, "path": null})).ok(); + } } + let _ = std::fs::remove_dir_all(&ids.dir); assert!(failures.is_empty(), "{} of {checked} hovers wrong:\n{}", failures.len(), failures.join("\n")); - assert!(checked > 100, "{checked}"); + // more than 100 over the four cases, as when they were one test + assert!(checked > 25, "{checked}"); } fn set_frame(h: &mut Harness<'_, EffectcraftApp>, f: i64) { diff --git a/crates/ui-egui/tests/it/ui_drag_drop.rs b/crates/ui-egui/tests/it/ui_drag_drop.rs index 5a0d94ddf..d7e99046b 100644 --- a/crates/ui-egui/tests/it/ui_drag_drop.rs +++ b/crates/ui-egui/tests/it/ui_drag_drop.rs @@ -722,3 +722,165 @@ fn illustrator_import_offers_and_applies_retain_layer_sizes() { assert_eq!((f.width, f.height), (30, 20)); assert!(f.layer.as_ref().unwrap().layer_size); } + +/// An After Effects project, written by File ▸ Export ▸ Adobe After Effects Project… to a folder of its +/// own: composition "Intro" with a solid. +fn aep_file(tag: &str) -> std::path::PathBuf { + let dir = std::env::temp_dir().join(format!("ec-ui-aep-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("Intro.aep"); + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Intro", "width": 320, "height": 180, "frameRate": 30, "duration": 2})).unwrap(); + s.execute("layer.newSolid", json!({"name": "Backdrop", "color": "#204060"})).unwrap(); + s.execute("file.exportAep", json!({"path": path.to_string_lossy(), "allowLosses": true})).unwrap(); + path +} + +/// The open composition is "Intro", in the folder named after the imported file. +fn intro_is_open_in_its_folder(h: &Harness<'_, EffectcraftApp>) { + let s = &h.state().session; + let comp = s.project.item(s.active_comp_id().expect("a composition is open")).unwrap(); + assert_eq!(comp.name, "Intro"); + let folder = s.project.item(comp.parent.expect("in a folder")).unwrap(); + assert_eq!(folder.name, "Intro.aep"); + assert!(matches!(folder.kind, effectcraft_engine::project::ItemKind::Folder)); +} + +/// Dropped After Effects projects import as File ▸ Import ▸ Adobe After Effects Project… does +/// (they were refused as unsupported footage): each in a folder named after its file, the last +/// one's composition open. With nothing lost, nothing is asked. +#[test] +fn dropped_after_effects_projects_import_and_open_their_composition() { + let (mut h, _) = harness(); + let aep = aep_file("drop"); + h.event(Event::PointerMoved(rect(&h, "viewer.area").center())); + h.step(); + let outro = aep.with_file_name("Outro.aep"); + std::fs::copy(&aep, &outro).unwrap(); + h.input_mut().dropped_files.push(Arc::new(Dropped(outro))); + h.input_mut().dropped_files.push(Arc::new(Dropped(aep))); + h.run_steps(3); + assert_eq!(h.state().dialog, None, "nothing is lost, so nothing to ask"); + assert!(h.state().session.project.items.values().any(|i| i.name == "Outro.aep"), "the first project too"); + intro_is_open_in_its_folder(&h); + assert_eq!(h.state().ui.status, "", "no errors"); +} + +/// File ▸ Import ▸ File… offers After Effects projects; a picked one imports as a project, and +/// the other files picked with it as footage. +#[test] +fn import_file_dialog_imports_a_picked_after_effects_project_with_the_other_files() { + let (mut h, _) = harness(); + let aep = aep_file("pick"); + let still = aep.with_file_name("still.png"); + std::fs::write(&still, b"").unwrap(); + let offered = Arc::new(std::sync::Mutex::new(Vec::::new())); + let seen = offered.clone(); + let picked = vec![aep.to_string_lossy().to_string(), still.to_string_lossy().to_string()]; + h.state_mut().session.importer = Some(Arc::new(Stills)); + h.state_mut().hooks.pick_files = Some(Box::new(move |exts: &[&str]| { + *seen.lock().unwrap() = exts.iter().map(|e| e.to_string()).collect(); + picked.clone() + })); + let ctx = h.ctx.clone(); + effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "file.import", json!({})).unwrap(); + assert!(["aep", "aepx"].iter().all(|e| offered.lock().unwrap().iter().any(|o| o == e)), "{:?}", offered.lock().unwrap()); + step_until(&mut h, |h| footage_items(h).iter().any(|(n, _)| n == "still.png")); + intro_is_open_in_its_folder(&h); +} + +/// Footage dropped with an After Effects project goes to the Project panel only, also over the +/// Timeline: the project opens its own composition, which the footage does not join. +#[test] +fn footage_dropped_with_an_after_effects_project_stays_in_the_project_panel() { + let (mut h, _) = harness(); + let aep = aep_file("with-footage"); + let still = aep.with_file_name("still.png"); + std::fs::write(&still, b"").unwrap(); + h.state_mut().session.importer = Some(Arc::new(Stills)); + h.event(Event::PointerMoved(rect(&h, "panel.Timeline").center())); + h.step(); + h.input_mut().dropped_files.push(Arc::new(Dropped(still))); + h.input_mut().dropped_files.push(Arc::new(Dropped(aep))); + h.run_steps(3); + step_until(&mut h, |h| footage_items(h).iter().any(|(n, _)| n == "still.png")); + h.run_steps(2); + intro_is_open_in_its_folder(&h); + let s = &h.state().session; + assert!(s.project.comps().all(|(_, c)| c.layers.iter().all(|l| l.name != "still.png")), "no composition got a still.png layer"); +} + +/// A dropped After Effects project with parts that cannot come over asks first (the losses +/// prompt). The projects dropped with it wait for the answer; the other files import meanwhile, +/// a Photoshop file with Default Drag Import As (one dialog at a time). Cancel skips only the +/// project it asks about; Import Anyway imports it. +#[test] +fn a_dropped_after_effects_project_with_losses_asks_first() { + let (mut h, _) = harness(); + let intro = aep_file("losses"); + let lossy = intro.with_file_name("Synthetic.aep"); + std::fs::copy(concat!(env!("CARGO_MANIFEST_DIR"), "/../aep/tests/fixtures/synthetic.aep"), &lossy).unwrap(); + h.state_mut().session.importer = Some(Arc::new(Gated { started: AtomicUsize::new(0), allow: Arc::new(AtomicUsize::new(usize::MAX)) })); + h.state_mut().session.prefs.import.drag_import_as = "footage".into(); + for path in [lossy.clone(), intro.clone(), intro.with_file_name("Layers.PSD"), intro.with_file_name("still.png")] { + h.input_mut().dropped_files.push(Arc::new(Dropped(path))); + } + h.run_steps(3); + assert_eq!(h.state().dialog, Some(effectcraft_ui_egui::Dialog::AepLosses), "the drop asks first"); + assert_eq!(h.state_mut().dialog_state_mut().aep_losses.queue, [intro.to_string_lossy().to_string()], "the other project waits"); + let named = |h: &Harness<'_, EffectcraftApp>, name: &str| h.state().session.project.items.values().any(|i| i.name == name); + assert!(!named(&h, "Synthetic.aep") && !named(&h, "Intro.aep"), "no project imported before the answer"); + step_until(&mut h, |h| ["Layers.PSD", "still.png"].iter().all(|n| footage_items(h).iter().any(|(f, _)| f == n))); + assert_eq!(h.state().dialog, Some(effectcraft_ui_egui::Dialog::AepLosses), "the footage did not replace the prompt"); + + click_id(&mut h, "dialog.aepLosses.cancel"); + h.run_steps(2); + assert_eq!(h.state().dialog, None); + assert!(!named(&h, "Synthetic.aep"), "Cancel skips the project it asked about"); + intro_is_open_in_its_folder(&h); + + h.input_mut().dropped_files.push(Arc::new(Dropped(lossy))); + h.run_steps(3); + assert_eq!(h.state().dialog, Some(effectcraft_ui_egui::Dialog::AepLosses)); + click_id(&mut h, "dialog.aepLosses.continue"); + h.run_steps(2); + assert_eq!(h.state().dialog, None); + assert!(named(&h, "Synthetic.aep"), "Import Anyway imports it"); +} + +/// An After Effects project with nothing lost imports at once, so a Photoshop file dropped with +/// it still asks how to import it (Import Photoshop File). +#[test] +fn a_photoshop_file_dropped_with_an_after_effects_project_still_asks() { + let (mut h, _) = harness(); + h.state_mut().session.importer = Some(Arc::new(Gated { started: AtomicUsize::new(0), allow: Arc::new(AtomicUsize::new(usize::MAX)) })); + let aep = aep_file("with-psd"); + h.input_mut().dropped_files.push(Arc::new(Dropped(aep.with_file_name("Layers.PSD")))); + h.input_mut().dropped_files.push(Arc::new(Dropped(aep))); + h.run_steps(2); + intro_is_open_in_its_folder(&h); + assert_eq!(h.state().dialog, Some(effectcraft_ui_egui::Dialog::Form), "Import Photoshop File asks"); +} + +/// A dropped file that is not an After Effects project reports why, changes nothing, and the +/// other files of the drop still import. +#[test] +fn a_corrupt_dropped_after_effects_project_reports_and_keeps_the_other_files() { + let (mut h, _) = harness(); + let dir = aep_file("corrupt").parent().unwrap().to_path_buf(); + let broken = dir.join("Broken.aep"); + std::fs::write(&broken, b"not an After Effects project").unwrap(); + let still = dir.join("still.png"); + std::fs::write(&still, b"").unwrap(); + h.state_mut().session.importer = Some(Arc::new(Stills)); + let comp = h.state().session.active_comp_id(); + h.input_mut().dropped_files.push(Arc::new(Dropped(broken))); + h.input_mut().dropped_files.push(Arc::new(Dropped(still))); + h.run_steps(3); + step_until(&mut h, |h| footage_items(h).iter().any(|(n, _)| n == "still.png")); + let status = h.state().ui.status.clone(); + assert!(status.contains("Broken.aep"), "the error names the file: {status}"); + assert!(!h.state().session.project.items.values().any(|i| i.name == "Broken.aep")); + assert_eq!(h.state().session.active_comp_id(), comp, "the open composition stays"); +} diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index 1db059220..8f2910e43 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -90,6 +90,180 @@ fn click(h: &mut Harness<'_, EffectcraftApp>, pos: egui::Pos2) { h.step(); } +fn header_arrow_harness() -> (Harness<'static, EffectcraftApp>, LayerId, [(u64, u64); 3]) { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Disclosure", "width": 32, "height": 18, "frameRate": 30, "duration": 2})).unwrap(); + let lid = s.execute("layer.newSolid", json!({"name": "Controls", "color": "#40b080"})).unwrap()["layer"].as_u64().unwrap(); + let mut effects = [(0, 0); 3]; + for pair in &mut effects { + let fx = s.execute("effect.apply", json!({"layers": [lid], "effect": "Point Control"})).unwrap()["effects"][0].as_u64().unwrap(); + let prop = layer(&s, lid).props.find_group(fx).unwrap().get("point").unwrap().uid; + *pair = (fx, prop); + } + s.state.selected_props.clear(); + let mut app = EffectcraftApp::new(s); + app.show_panel(PanelKind::EffectControls); + app.toggle_maximize(PanelKind::EffectControls); + let mut h = Harness::builder().with_size(egui::vec2(1000.0, 700.0)).with_step_dt(1.0 / 60.0).build_eframe(|_| app); + h.run_steps(4); + (h, LayerId(lid), effects) +} + +fn header_arrow_key(h: &mut Harness<'_, EffectcraftApp>, key: egui::Key, modifiers: egui::Modifiers) { + h.key_press_modifiers(modifiers, key); + h.run_steps(2); +} + +#[test] +fn effect_header_arrows_collapse_and_expand_without_editing_the_document() { + let (mut h, lid, [(fx, prop), (_, other), _]) = header_arrow_harness(); + let at = rect_of(&h, &format!("effectControls.effect.{fx}.name")).center(); + click(&mut h, at); + assert_eq!(h.state().ui.focused, PanelKind::EffectControls); + assert_eq!(h.state().session.state.selected_props, vec![(lid, fx)]); + let project = serde_json::to_value(&h.state().session.project).unwrap(); + let time = h.state().session.time(); + let undo = h.state().session.history.undo.len(); + let redo = h.state().session.history.redo.len(); + for key in [egui::Key::ArrowLeft, egui::Key::ArrowLeft, egui::Key::ArrowRight, egui::Key::ArrowRight] { + header_arrow_key(&mut h, key, egui::Modifiers::NONE); + assert_eq!(h.state().auto.find(&format!("effectControls.row.{prop}")).is_some(), key == egui::Key::ArrowRight); + assert!(h.state().auto.find(&format!("effectControls.row.{other}")).is_some(), "unselected effect stays open"); + assert_eq!(h.state().session.state.selected_props, vec![(lid, fx)]); + assert_eq!(serde_json::to_value(&h.state().session.project).unwrap(), project); + assert_eq!(h.state().session.time(), time); + assert_eq!((h.state().session.history.undo.len(), h.state().session.history.redo.len()), (undo, redo)); + } +} + +#[test] +fn effect_header_arrows_only_disclose_selected_headers_on_the_displayed_layer() { + let (mut h, lid, [(first, _), (second, _), (third, prop)]) = header_arrow_harness(); + h.state_mut().session.state.selected_props = vec![(lid, first), (lid, second), (LayerId(0), third), (lid, u64::MAX)]; + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert_eq!(h.state().ui.fx_closed.iter().copied().collect::>(), vec![first, second]); + assert!(h.state().auto.find(&format!("effectControls.row.{prop}")).is_some()); + header_arrow_key(&mut h, egui::Key::ArrowRight, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty()); + + // A parameter selection, stale header, or header on another layer must still fall through. + h.state_mut().session.state.selected_props = vec![(lid, prop), (LayerId(0), first), (lid, u64::MAX)]; + let before = serde_json::to_value(&h.state().session.project).unwrap(); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty()); + assert_ne!(serde_json::to_value(&h.state().session.project).unwrap(), before, "the existing layer nudge still runs"); +} + +#[test] +fn effect_header_arrows_ignore_hidden_panels_with_stale_focus() { + // A maximized frame hides every other frame and every floating window, so a floating Effect + // Controls is hidden too. + for hidden in ["closed", "maximized", "floating under maximized", "home"] { + let (mut h, lid, [(fx, _), _, _]) = header_arrow_harness(); + h.state_mut().session.state.selected_props = vec![(lid, fx)]; + match hidden { + "closed" => h.state_mut().close_panel(PanelKind::EffectControls), + "maximized" => h.state_mut().toggle_maximize(PanelKind::Composition), + "floating under maximized" => { + h.state_mut().apply_dock(craft_ui::docking::Action::Float { panel: PanelKind::EffectControls, rect: [40.0, 40.0, 600.0, 400.0] }).unwrap(); + h.state_mut().toggle_maximize(PanelKind::Composition); + } + _ => h.state_mut().ui.start_screen = true, + } + h.run_steps(2); + assert_eq!(h.state().ui.focused, PanelKind::EffectControls, "{hidden}: stale focus is preserved"); + assert!(h.state().auto.find("panel.EffectControls").is_none(), "{hidden}: Effect Controls is hidden"); + let x = |h: &Harness<'_, EffectcraftApp>| layer(&h.state().session, lid.0).props.prop("transform/position").unwrap().value.as_vec3()[0]; + let before = x(&h); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "{hidden}: hidden effects keep their disclosure state"); + assert!(x(&h) < before, "{hidden}: Left still nudges the layer"); + } +} + +#[test] +fn effect_header_arrows_work_wherever_effect_controls_is_on_screen() { + for place in ["floating", "tab of a maximized frame"] { + let (mut h, lid, [(fx, prop), _, _]) = header_arrow_harness(); + h.state_mut().toggle_maximize(PanelKind::EffectControls); + if place == "floating" { + h.state_mut().apply_dock(craft_ui::docking::Action::Float { panel: PanelKind::EffectControls, rect: [40.0, 40.0, 600.0, 400.0] }).unwrap(); + } else { + let placement = craft_ui::docking::Placement::Tab { before: None }; + h.state_mut().apply_dock(craft_ui::docking::Action::Move { panel: PanelKind::EffectControls, anchor: PanelKind::Composition, placement }).unwrap(); + h.state_mut().toggle_maximize(PanelKind::Composition); + assert_eq!(h.state().ui.maximized, Some(PanelKind::Composition), "the frame was maximized from its Composition tab"); + } + // The moved tab is the front one; focus it as a click on it would. + h.state_mut().ui.focused = PanelKind::EffectControls; + h.state_mut().session.state.selected_props = vec![(lid, fx)]; + h.run_steps(3); + assert!(h.state().auto.find(&format!("effectControls.row.{prop}")).is_some(), "{place}: Effect Controls is on screen"); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().auto.find(&format!("effectControls.row.{prop}")).is_none(), "{place}: Left collapses"); + header_arrow_key(&mut h, egui::Key::ArrowRight, egui::Modifiers::NONE); + assert!(h.state().auto.find(&format!("effectControls.row.{prop}")).is_some(), "{place}: Right expands"); + } +} + +#[test] +fn effect_header_arrows_preserve_modified_shortcuts_and_keyboard_ownership() { + let (mut h, lid, [(fx, _), _, _]) = header_arrow_harness(); + h.state_mut().session.state.selected_props = vec![(lid, fx)]; + let time = h.state().session.time(); + header_arrow_key(&mut h, egui::Key::ArrowRight, egui::Modifiers::COMMAND); + assert!(h.state().session.time() > time, "Cmd/Ctrl+Right still advances a frame"); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::COMMAND); + assert_eq!(h.state().session.time(), time); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::SHIFT); + assert!(h.state().ui.fx_closed.is_empty(), "Shift+Left does not collapse effects"); + h.state_mut().ui.focused = PanelKind::Composition; + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "another panel owns the arrows"); + h.state_mut().ui.focused = PanelKind::EffectControls; + h.state_mut().dialog = Some(effectcraft_ui_egui::Dialog::About); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "a dialog owns the arrows"); + h.state_mut().dialog = None; + h.run_steps(3); + let at = rect_of(&h, &format!("effectControls.effect.{fx}.name")).center(); + h.event(egui::Event::PointerMoved(at)); + for pressed in [true, false] { + h.event(egui::Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: egui::Modifiers::NONE }); + h.step(); + } + h.run_steps(2); + assert!(egui::Popup::is_any_open(&h.ctx), "the effect context menu is open"); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "the context menu owns the arrows"); + header_arrow_key(&mut h, egui::Key::Escape, egui::Modifiers::NONE); + h.run_steps(3); + assert!(!egui::Popup::is_any_open(&h.ctx), "Escape closed the context menu"); + double_click(&mut h, at); + h.run_steps(3); + assert!(h.state().auto.find(&format!("effectControls.effect.{fx}.nameEdit")).is_some(), "the rename field is open"); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "Left moves the text caret instead of collapsing"); + assert!(h.state().auto.find(&format!("effectControls.effect.{fx}.nameEdit")).is_some()); +} + +#[test] +fn effect_header_arrows_leave_viewer_text_editing_alone() { + let (mut h, lid, [(fx, _), _, _]) = header_arrow_harness(); + let text = h.state_mut().session.execute("layer.newText", json!({"text": "abcdef"})).unwrap()["layer"].as_u64().unwrap(); + h.state_mut().session.execute("text.edit", json!({"layer": text, "caret": 3})).unwrap(); + h.state_mut().session.state.selected_layers = vec![lid]; + h.state_mut().session.state.selected_props = vec![(lid, fx)]; + h.state_mut().show_panel(PanelKind::EffectControls); + h.run_steps(2); + assert!(h.state().session.state.text_edit.is_some(), "the text is still being edited"); + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert!(h.state().ui.fx_closed.is_empty(), "the text edit owns the arrows"); + h.state_mut().session.state.text_edit = None; + header_arrow_key(&mut h, egui::Key::ArrowLeft, egui::Modifiers::NONE); + assert_eq!(h.state().ui.fx_closed.iter().copied().collect::>(), vec![fx], "with the edit over, Left collapses the effect"); +} + #[test] fn effect_controls_widgets_register_automation_ids() { let (app, x) = app(); @@ -260,14 +434,14 @@ fn the_eyedropper_stays_armed_until_the_viewer_has_a_frame() { } } -/// Key Light's Screen Colour eyedropper picks from the effect's input: the shown frame is +/// Screen Color Key's Screen Colour eyedropper picks from the effect's input: the shown frame is /// already keyed (the default screen colour takes most of a green), so sampling it would miss. #[test] fn keyer_eyedropper_picks_the_screen_from_the_effect_input() { let mut s = Session::default(); s.execute("comp.new", json!({"name": "Key", "width": 320, "height": 180, "frameRate": 30, "duration": 1})).unwrap(); let screen = s.execute("layer.newSolid", json!({"name": "Screen", "color": "#30b050"})).unwrap()["layer"].as_u64().unwrap(); - let fx = s.execute("effect.apply", json!({"layers": [screen], "effect": "Key Light"})).unwrap()["effects"][0].as_u64().unwrap(); + let fx = s.execute("effect.apply", json!({"layers": [screen], "effect": "Screen Color Key"})).unwrap()["effects"][0].as_u64().unwrap(); let prop = layer(&s, screen).effects().unwrap().groups().find(|g| g.uid == fx).and_then(|g| g.get("screenColour")).map(|p| p.uid).unwrap(); s.state.selected_layers = vec![LayerId(screen)]; let mut app = EffectcraftApp::new(s); @@ -295,7 +469,7 @@ fn keyer_eyedropper_picks_the_screen_from_the_effect_input() { fn command_clicking_an_effect_twirl_closes_and_opens_its_groups() { let (app, x) = app(); let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); - let fx = h.state_mut().session.execute("effect.apply", json!({"layers": [x.small], "effect": "Lumetri Color"})).unwrap()["effects"][0].as_u64().unwrap(); + let fx = h.state_mut().session.execute("effect.apply", json!({"layers": [x.small], "effect": "Color Grading"})).unwrap()["effects"][0].as_u64().unwrap(); // Only this effect open, so its header is on screen. let l = layer(&h.state().session, x.small); let others: Vec = l.effects().unwrap().groups().map(|g| g.uid).filter(|u| *u != fx).collect(); diff --git a/crates/ui-egui/tests/it/ui_effect_drop_highlight.rs b/crates/ui-egui/tests/it/ui_effect_drop_highlight.rs index 6c0ede47a..ddef6c94b 100644 --- a/crates/ui-egui/tests/it/ui_effect_drop_highlight.rs +++ b/crates/ui-egui/tests/it/ui_effect_drop_highlight.rs @@ -13,7 +13,7 @@ fn rect(h: &Harness<'_, EffectcraftApp>, id: &str) -> Rect { fn drag_to(h: &mut Harness<'_, EffectcraftApp>, p: Pos2) { h.input_mut().events.push(Event::PointerMoved(p)); h.input_mut().events.push(Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); - egui::DragAndDrop::set_payload(&h.ctx, DragPayload::Effect("CC Bend It".into())); + egui::DragAndDrop::set_payload(&h.ctx, DragPayload::Effect("Two-Point Bend".into())); h.run_steps(2); } diff --git a/crates/ui-egui/tests/it/ui_fx_editors.rs b/crates/ui-egui/tests/it/ui_fx_editors.rs index b9530fdf4..b4d999311 100644 --- a/crates/ui-egui/tests/it/ui_fx_editors.rs +++ b/crates/ui-egui/tests/it/ui_fx_editors.rs @@ -130,7 +130,7 @@ fn undo(h: &mut Harness<'_, EffectcraftApp>) { #[test] fn lumetri_rgb_and_hue_saturation_curve_editors() { - let (s0, l, fx) = with_effect("Lumetri Color"); + let (s0, l, fx) = with_effect("Color Grading"); let mut h = harness(s0); only_groups(&mut h, l, fx, &["curves", "rgbCurves", "hueSaturationCurves"]); let pre = format!("effectControls.effect.{fx}.lumetri.rgbCurves"); diff --git a/crates/ui-egui/tests/it/ui_inline_edit.rs b/crates/ui-egui/tests/it/ui_inline_edit.rs index 6c29045bb..ee0b3d35a 100644 --- a/crates/ui-egui/tests/it/ui_inline_edit.rs +++ b/crates/ui-egui/tests/it/ui_inline_edit.rs @@ -438,8 +438,11 @@ fn ctrl_click_on_the_time_display_toggles_timecode_and_frames() { click_with(&mut h, at, Modifiers::COMMAND); assert_eq!(style(&h), TimeDisplayStyle::Timecode); assert_eq!(shown(&h, "timeline.timecode"), "0:00:01:06"); - // A plain click changes nothing. + // A plain click leaves the display alone: it opens Go to Time (#3373), closed here. click_with(&mut h, at, Modifiers::NONE); + assert_eq!(style(&h), TimeDisplayStyle::Timecode); + key(&mut h, Key::Escape); + assert!(h.query_by_label("Go to Time").is_none(), "Go to Time closed"); let at = center(&h, "timeline.timecode"); click_with(&mut h, at, Modifiers::NONE); assert_eq!(style(&h), TimeDisplayStyle::Timecode); @@ -746,3 +749,17 @@ fn typing_one_component_with_several_layers_selected_keeps_their_other_component assert_eq!(pos(&h, a), [40.0, 50.0, -300.0]); assert_eq!(pos(&h, b), [200.0, 120.0, -300.0], "B keeps its own X and Y"); } + +/// #3373: a click on the Composition panel's time display opens Go to Time, as in After Effects; +/// Ctrl/Cmd+click still switches between timecode and frames. +#[test] +fn clicking_the_viewer_time_display_opens_go_to_time() { + let (mut h, _) = harness(); + h.state_mut().session.execute("time.set", json!({"frame": 30})).unwrap(); + h.run_steps(2); + assert!(h.query_by_label("Go to Time").is_none(), "set up: no dialog yet"); + let at = center(&h, "viewer.timecode"); + click(&mut h, at, 1); + h.run_steps(2); + assert!(h.query_by_label("Go to Time").is_some(), "the dialog is open"); +} diff --git a/crates/ui-egui/tests/it/ui_live_preview.rs b/crates/ui-egui/tests/it/ui_live_preview.rs index e72856bfc..abe3cc427 100644 --- a/crates/ui-egui/tests/it/ui_live_preview.rs +++ b/crates/ui-egui/tests/it/ui_live_preview.rs @@ -77,12 +77,12 @@ fn position_anchor_and_effect_edits_present_intermediate_pixels() { (None, "transform/position", json!([22, 18, 0]), json!([18, 18, 0])), (None, "transform/anchor", json!([9, 8, 0]), json!([6, 8, 0])), (Some("Gaussian Blur"), "blurriness", json!(3), json!(6)), - (Some("CC Radial Fast Blur"), "center", json!([4, 4]), json!([18, 12])), + (Some("Fast Radial Blur"), "center", json!([4, 4]), json!([18, 12])), ] { let mut session = Session::default(); session.execute("comp.new", json!({"width":64,"height":36,"duration":1})).unwrap(); let layer = session.execute("layer.newSolid", json!({"color":"#ffffff","width":24,"height":16})).unwrap()["layer"].as_u64().unwrap(); - if effect == Some("CC Radial Fast Blur") { + if effect == Some("Fast Radial Blur") { // A uniform solid is invariant under a radial blur; give the point edit visible detail. session.execute("effect.apply", json!({"layers":[layer],"effect":"Gradient Ramp"})).unwrap(); } diff --git a/crates/ui-egui/tests/it/ui_parameter_spacing.rs b/crates/ui-egui/tests/it/ui_parameter_spacing.rs index 71b70a14e..167029f7c 100644 --- a/crates/ui-egui/tests/it/ui_parameter_spacing.rs +++ b/crates/ui-egui/tests/it/ui_parameter_spacing.rs @@ -54,7 +54,7 @@ fn nested_groups_keep_indentation_value_alignment_and_effect_gaps_at_fixed_compa let mut s = Session::default(); s.execute("comp.new", json!({"width":64,"height":36,"duration":1})).unwrap(); s.execute("layer.newSolid", json!({"name":"Plate","color":"#406080"})).unwrap(); - let fx = s.execute("effect.apply", json!({"effect":"Lumetri Color"})).unwrap()["effects"][0].as_u64().unwrap(); + let fx = s.execute("effect.apply", json!({"effect":"Color Grading"})).unwrap()["effects"][0].as_u64().unwrap(); let next = s.execute("effect.apply", json!({"effect":"Checkbox Control"})).unwrap()["effects"][0].as_u64().unwrap(); let g = s.active_comp().unwrap().layers[0].props.find_group(fx).unwrap(); let basic = g.sub("basicCorrection").unwrap(); diff --git a/crates/ui-egui/tests/it/ui_preview_align.rs b/crates/ui-egui/tests/it/ui_preview_align.rs index 1469a5e9e..afafa8a45 100644 --- a/crates/ui-egui/tests/it/ui_preview_align.rs +++ b/crates/ui-egui/tests/it/ui_preview_align.rs @@ -238,8 +238,9 @@ fn cache_frames_when_idle_fills_the_work_area() { let mut h = harness(); let cid = h.state().session.active_comp_id().unwrap(); let cached = |h: &Harness<'_, EffectcraftApp>| h.state().frames.cached_frames(&h.state().shown_series(cid)).len(); - // Off: only a few frames ahead of the current time are prefetched (the work area is the - // whole 240 frames). + // Off (it is on by default): only a few frames ahead of the current time are prefetched (the + // work area is the whole 240 frames). + invoke(&mut h, "playback.cacheWhenIdle", json!({"value": false})); for _ in 0..120 { h.step(); std::thread::sleep(std::time::Duration::from_millis(2)); @@ -284,6 +285,9 @@ fn playback_prefetches_past_the_cached_frames_ahead() { h.state_mut().use_manual_clock(); h.state_mut().session.set_time(Tick::ZERO); invoke(&mut h, "playback.toggle", json!({})); + // The first pass sees the frames ahead cached and starts the clock (a silent preview holds + // its first frame until a few are, as one with sound does). + h.step(); h.state_mut().clock_mut().advance(0.25); h.run_steps(2); assert!(h.state().playback.playing); @@ -315,3 +319,120 @@ fn cache_frames_when_idle_survives_an_inverted_work_area() { } assert!(!h.state().playback.playing); } + +/// A preview that played at full rate and then stalls on frames that aren't cached still steps +/// down: the rate from before the stall doesn't count once nothing was shown for a second. (The +/// last second shown kept reading as full rate through the stall, and it never stepped down.) +#[test] +fn playback_that_stalls_after_keeping_up_steps_down() { + let mut h = harness(); + h.state_mut().session.prefs.previews.adaptive_playback = true; + h.state().frames.set_budget(0); + h.state_mut().use_manual_clock(); + invoke(&mut h, "playback.toggle", json!({})); + h.step(); + assert!(h.state().playback.playing); + // A second shown at the comp's rate, just before the stall. + let t0 = h.state().now(); + for i in 0..30 { + h.state_mut().playback.frame_shown(t0 - 1.0 + f64::from(i) / 30.0); + } + for _ in 0..25 { + h.state_mut().clock_mut().advance(0.1); + h.step(); + } + assert_eq!(h.state().viewer_scale(1.0, 1.0), 0.5); +} + +/// Settings ▸ Previews ▸ Lower Resolution When Playback Can't Keep Up: a preview at Auto that +/// stalls for two seconds steps down to half its scale, two more to a quarter; an explicit Resolution and +/// the pref turned off (its default) keep the scale, and stopping goes back to the viewer's. +#[test] +fn playback_that_cannot_keep_up_steps_down_a_resolution() { + let stall = |h: &mut Harness<'_, EffectcraftApp>, secs: f64| { + for _ in 0..(secs / 0.1).round() as usize { + h.state_mut().clock_mut().advance(0.1); + h.step(); + } + }; + let play = |h: &mut Harness<'_, EffectcraftApp>| { + h.state_mut().use_manual_clock(); + invoke(h, "playback.toggle", json!({})); + h.step(); + assert!(h.state().playback.playing); + }; + let mut h = harness(); + assert!(!h.state().session.prefs.previews.adaptive_playback, "off by default"); + h.state_mut().session.prefs.previews.adaptive_playback = true; + // No room in the RAM preview: frames never stay cached and every pass stalls, as with a comp + // too heavy to render in real time. + h.state().frames.set_budget(0); + play(&mut h); + stall(&mut h, 1.5); + assert_eq!(h.state().viewer_scale(1.0, 1.0), 1.0, "it waits two seconds before it steps down"); + stall(&mut h, 0.7); + assert_eq!(h.state().viewer_scale(1.0, 1.0), 0.5); + stall(&mut h, 1.5); + assert_eq!(h.state().viewer_scale(1.0, 1.0), 0.5, "and two more before the next step"); + stall(&mut h, 0.7); + assert_eq!(h.state().viewer_scale(1.0, 1.0), 0.25); + invoke(&mut h, "playback.toggle", json!({})); + assert!(!h.state().playback.playing); + assert_eq!(h.state().viewer_scale(1.0, 1.0), 1.0, "stopped: the viewer's resolution again"); + + // An explicit Resolution is kept. + let cid = h.state().session.active_comp_id().unwrap(); + std::sync::Arc::make_mut(&mut h.state_mut().session.project).comp_mut(cid).unwrap().resolution = effectcraft_engine::project::Resolution::Full; + play(&mut h); + stall(&mut h, 2.5); + assert_eq!(h.state().viewer_scale(1.0, 1.0), 1.0); + invoke(&mut h, "playback.toggle", json!({})); + + // So is Auto with the pref off. + std::sync::Arc::make_mut(&mut h.state_mut().session.project).comp_mut(cid).unwrap().resolution = effectcraft_engine::project::Resolution::Auto; + h.state_mut().session.prefs.previews.adaptive_playback = false; + play(&mut h); + stall(&mut h, 2.5); + assert_eq!(h.state().viewer_scale(1.0, 1.0), 1.0); +} + +/// A preview that renders every frame at once plays in real time even when the RAM preview holds +/// only a few frames. (The pace measured its lead over twice the prefetch depth, more frames than +/// a small RAM preview keeps ahead, so such a preview was slowed to a fraction of its frame rate.) +#[test] +fn a_preview_that_keeps_up_plays_in_real_time_with_a_small_ram_preview() { + let mut h = harness(); + let cid = h.state().session.active_comp_id().unwrap(); + let series = h.state().shown_series(cid); + let s = series.scale as f64 / 1000.0; + let frame = (320.0 * s).ceil() * (180.0 * s).ceil() * 4.0; + // Room for 4 frames (90 % of 5): the prefetch keeps the current frame and 2 after it. + h.state().frames.set_budget((frame * 5.0) as usize); + h.state_mut().use_manual_clock(); + h.state_mut().session.set_time(Tick::ZERO); + invoke(&mut h, "playback.toggle", json!({})); + let fr = h.state().session.active_comp().unwrap().frame_rate; + // Rendering keeps up: the frames ahead are rendered before the clock moves on. + let settle = |h: &mut Harness<'_, EffectcraftApp>| { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20); + while std::time::Instant::now() < deadline { + h.step(); + let cur = fr.frame_at(h.state().session.time()); + let cached = h.state().frames.cached_frames(&series); + if (1..=2).all(|k| cached.contains(&((cur + k) % 240))) { + return; + } + std::thread::sleep(std::time::Duration::from_millis(1)); + } + panic!("frames ahead never cached"); + }; + for _ in 0..48 { + settle(&mut h); + h.state_mut().clock_mut().advance(1.0 / 24.0); + h.step(); + } + let pb = &h.state().playback; + assert!(pb.playing); + assert!(pb.pace >= 24.0 * 0.98, "plays at {} fps of 24", pb.pace); + assert!(fr.frame_at(h.state().session.time()) >= 40, "the clock kept real time: {:?}", h.state().session.time()); +} diff --git a/crates/ui-egui/tests/it/ui_script_render.rs b/crates/ui-egui/tests/it/ui_script_render.rs new file mode 100644 index 000000000..b3c24733c --- /dev/null +++ b/crates/ui-egui/tests/it/ui_script_render.rs @@ -0,0 +1,260 @@ +//! #3289: the control bridge must keep servicing UI frames during scripted and direct renders. + +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::mpsc::{Receiver, TryRecvError}; +use std::time::{Duration, Instant}; + +use effectcraft_engine::project::render_queue::OutputFormat; +use effectcraft_engine::{ExportJob, ExportResult, Exporter}; +use effectcraft_ui_egui::{ControlRequest, EffectcraftApp}; +use egui_kittest::Harness; +use serde_json::{Value, json}; + +struct Gate(AtomicBool); +impl Exporter for Gate { + fn formats(&self) -> Vec { + vec![OutputFormat::H264] + } + fn export(&self, job: &ExportJob, progress: &mut dyn FnMut(u64, u64) -> bool) -> Result { + let end = Instant::now() + Duration::from_secs(3); + while !self.0.load(Ordering::SeqCst) { + if !progress(0, 10) { + return Err(effectcraft_engine::render_queue::CANCELLED.into()); + } + if Instant::now() > end { + return Err("test exporter waited for a blocked UI".into()); + } + std::thread::sleep(Duration::from_millis(1)); + } + Ok(ExportResult { path: job.path.into(), frames: 10, ..Default::default() }) + } +} + +fn harness() -> (Harness<'static, EffectcraftApp>, Arc, std::sync::mpsc::Sender) { + let gate = Arc::new(Gate(AtomicBool::new(false))); + let mut s = effectcraft_host::session(); + s.exporter = Some(gate.clone()); + s.prefs.auto_save.enabled = false; + s.execute("comp.new", json!({"width": 16, "height": 16, "duration": 1})).unwrap(); + let path = std::env::temp_dir().join(format!("ec-ui-script-render-{}.mp4", std::process::id())); + s.execute("renderQueue.add", json!({"output": path.to_string_lossy()})).unwrap(); + let (tx, rx) = std::sync::mpsc::channel(); + let h = Harness::builder().with_size(egui::vec2(1000.0, 700.0)).build_eframe(|_| EffectcraftApp::new(s).with_control(rx)); + (h, gate, tx) +} + +fn send(tx: &std::sync::mpsc::Sender, method: &str, params: Value) -> Receiver { + let (request, reply) = ControlRequest::new(method, params); + tx.send(request).unwrap(); + reply +} + +fn finish(h: &mut Harness<'_, EffectcraftApp>, reply: &Receiver) -> Value { + let end = Instant::now() + Duration::from_secs(4); + loop { + h.run_steps(1); + if let Ok(result) = reply.try_recv() { + return result; + } + assert!(Instant::now() < end, "control reply did not complete"); + std::thread::sleep(Duration::from_millis(1)); + } +} + +#[test] +fn script_render_keeps_frames_and_inspection_alive_until_the_continuation_finishes() { + let (mut h, gate, tx) = harness(); + let reply = send( + &tx, + "engine.execute", + json!({"command": "script.run", "params": {"code": "$.writeln('before'); app.project.renderQueue.render(); app.project.activeItem.layers.addNull().name = 'After'; $.writeln('after'); 42;"}}), + ); + h.run_steps(3); + assert!(matches!(reply.try_recv(), Err(TryRecvError::Empty)), "script reply must wait for its continuation"); + assert!(h.state().session.active_comp().unwrap().layers.is_empty()); + let before = h.state().ui_passes(); + let inspect = send(&tx, "ui.inspect", json!({})); + let edit = send(&tx, "engine.execute", json!({"command": "layer.newNull"})); + let batch = send(&tx, "engine.execute", json!({"command": "engine.batch", "params": {"steps": [{"command": "layer.newNull"}]}})); + let jobs = send(&tx, "engine.execute", json!({"command": "jobs.list"})); + let quit = send(&tx, "app.quit", json!({"force": true})); + h.run_steps(3); + assert!(h.state().ui_passes() > before, "frames still draw while the render is gated"); + assert_eq!(inspect.try_recv().unwrap()["ok"], true); + assert_eq!(edit.try_recv().unwrap()["ok"], false, "unrelated edits cannot enter the script's undo group"); + assert_eq!(batch.try_recv().unwrap()["ok"], false, "batches are not pure queries"); + assert_eq!(jobs.try_recv().unwrap()["ok"], false, "job polling can apply unrelated edits"); + assert_eq!(quit.try_recv().unwrap()["ok"], false, "force quit cannot abandon the script's live session"); + assert!(h.state().auto.find("scriptRender.stop").is_some()); + gate.0.store(true, Ordering::SeqCst); + let done = finish(&mut h, &reply); + assert_eq!(done["ok"], true, "{done}"); + assert_eq!(done["result"]["ok"], true, "{done}"); + assert_eq!(done["result"]["result"], 42); + assert_eq!(done["result"]["output"], "before\nafter"); + assert_eq!(h.state().session.active_comp().unwrap().layers[0].name, "After"); + assert!(h.state().session.script_ui.render_host.is_none()); +} + +#[test] +fn stop_render_remains_available_while_a_script_waits() { + let (mut h, _gate, tx) = harness(); + let reply = send(&tx, "engine.execute", json!({"command": "script.run", "params": {"code": "app.project.renderQueue.render(); 23;"}})); + h.run_steps(2); + assert!(matches!(reply.try_recv(), Err(TryRecvError::Empty))); + let stop = send(&tx, "engine.execute", json!({"command": "renderQueue.stop"})); + h.run_steps(1); + assert_eq!(stop.try_recv().unwrap()["ok"], true); + assert_eq!(finish(&mut h, &reply)["result"]["result"], 23); + assert!(!h.state().session.is_rendering()); +} + +#[test] +fn closing_when_a_control_script_starts_cannot_abandon_its_new_unsaved_changes() { + let (mut h, gate, tx) = harness(); + h.state_mut().session.mark_saved(); + let reply = send( + &tx, + "engine.execute", + json!({"command": "script.run", "params": {"code": "app.project.activeItem.layers.addNull(); app.project.renderQueue.render(); 31;"}}), + ); + h.input_mut().viewports.entry(egui::ViewportId::ROOT).or_default().events.push(egui::ViewportEvent::Close); + h.step(); + let canceled = h.output().viewport_output.get(&egui::ViewportId::ROOT).is_some_and(|v| v.commands.contains(&egui::ViewportCommand::CancelClose)); + let dirty = h.state().session.is_dirty(); + let pending = matches!(reply.try_recv(), Err(TryRecvError::Empty)); + gate.0.store(true, Ordering::SeqCst); + assert_eq!(finish(&mut h, &reply)["result"]["result"], 31); + assert!(canceled, "a close accepted before the control request must be canceled once the script suspends"); + assert!(dirty && pending); +} + +#[test] +fn closing_on_the_completion_frame_cannot_skip_the_unsaved_changes_guard() { + let (mut h, gate, tx) = harness(); + let reply = send(&tx, "engine.execute", json!({"command": "script.run", "params": {"code": "app.project.renderQueue.render(); 23;"}})); + h.run_steps(2); + assert!(h.state().session.script_ui.render_host.is_some()); + gate.0.store(true, Ordering::SeqCst); + let end = Instant::now() + Duration::from_secs(4); + while h.state().session.is_rendering() { + assert!(Instant::now() < end); + std::thread::sleep(Duration::from_millis(1)); + } + h.input_mut().viewports.entry(egui::ViewportId::ROOT).or_default().events.push(egui::ViewportEvent::Close); + h.step(); + assert!(h.output().viewport_output.get(&egui::ViewportId::ROOT).is_some_and(|v| v.commands.contains(&egui::ViewportCommand::CancelClose))); + assert_eq!(reply.try_recv().unwrap()["result"]["result"], 23); + assert!(h.state().session.is_dirty()); +} + +#[test] +fn direct_native_render_waits_for_its_worker_without_blocking_frames() { + let (mut h, gate, tx) = harness(); + let reply = send(&tx, "engine.execute", json!({"command": "renderQueue.render"})); + h.run_steps(3); + assert!(matches!(reply.try_recv(), Err(TryRecvError::Empty))); + let inspect = send(&tx, "ui.inspect", json!({})); + h.run_steps(2); + assert_eq!(inspect.try_recv().unwrap()["ok"], true); + gate.0.store(true, Ordering::SeqCst); + let done = finish(&mut h, &reply); + assert_eq!(done["result"]["rendering"], false); + assert_eq!(done["result"]["items"][0]["statusLabel"], "Done", "{done}"); +} + +#[test] +fn malformed_waiting_render_params_return_an_error() { + let (mut h, _gate, tx) = harness(); + let reply = send(&tx, "engine.execute", json!({"command": "renderQueue.render", "params": []})); + h.run_steps(1); + assert_eq!(reply.try_recv().unwrap()["ok"], false); + assert!(!h.state().session.is_rendering()); +} + +fn wait_worker(h: &Harness<'_, EffectcraftApp>) { + let end = Instant::now() + Duration::from_secs(4); + while h.state().session.is_rendering() { + assert!(Instant::now() < end, "render worker did not finish"); + std::thread::sleep(Duration::from_millis(1)); + } +} + +#[test] +fn direct_render_reply_keeps_its_items_after_queue_removal_and_a_new_render() { + use effectcraft_ui_egui::control::{Outcome, handle}; + + for no_running_jobs in [false, true] { + let (mut h, gate, _tx) = harness(); + let ctx = egui::Context::default(); + let request = ControlRequest::new("engine.execute", json!({"command": "renderQueue.render"})).0; + let Outcome::AwaitJobs(mut first) = handle(h.state_mut(), &ctx, &request, 0.0) else { panic!("expected a deferred render") }; + let original = first.result["items"][0].clone(); + // Also cover completion between the command's result and its jobs() snapshot. + if no_running_jobs { + first.jobs.clear(); + } + gate.0.store(true, Ordering::SeqCst); + wait_worker(&h); + gate.0.store(false, Ordering::SeqCst); + // The worker is done, but no frontend completion pass has run. Removing the item + // must not erase the waiting reply, even when the next queue reuses the same id. + h.state_mut().session.execute("renderQueue.remove", json!({"item": original["id"]})).unwrap(); + h.state_mut().session.poll_render(); + let second_path = std::env::temp_dir().join(format!("ec-ui-second-render-{}.mp4", std::process::id())); + h.state_mut().session.execute("renderQueue.add", json!({"output": second_path.to_string_lossy()})).unwrap(); + let Outcome::AwaitJobs(second) = handle(h.state_mut(), &ctx, &request, 0.0) else { panic!("expected a second deferred render") }; + assert!(second.pending(h.state())); + assert!(!first.pending(h.state()), "the first reply must not wait for a later job with the same jobs.list id"); + let done = first.finish(h.state_mut()); + assert_eq!(done["result"]["rendering"], false, "{done}"); + assert_eq!(done["result"]["items"][0]["statusLabel"], "Done", "{done}"); + assert_eq!(done["result"]["items"][0]["outputPath"], original["outputPath"]); + assert_eq!(done["result"]["items"][0]["last_output"], original["outputPath"]); + assert!(h.state().session.is_rendering()); + gate.0.store(true, Ordering::SeqCst); + wait_worker(&h); + h.state_mut().session.poll_render(); + assert!(!second.pending(h.state())); + assert_eq!(second.finish(h.state_mut())["result"]["items"][0]["outputPath"], second_path.to_string_lossy().as_ref()); + } +} + +#[test] +fn completed_direct_render_reply_is_not_delayed_by_a_later_script_render() { + let (mut h, gate, tx) = harness(); + let first = send(&tx, "engine.execute", json!({"command": "renderQueue.render"})); + h.run_steps(2); + assert!(matches!(first.try_recv(), Err(TryRecvError::Empty))); + gate.0.store(true, Ordering::SeqCst); + wait_worker(&h); + gate.0.store(false, Ordering::SeqCst); + h.state_mut().session.poll_render(); + h.state_mut().session.execute("renderQueue.setRender", json!({"index": 1, "render": true})).unwrap(); + let second = send(&tx, "engine.execute", json!({"command": "script.run", "params": {"code": "app.project.renderQueue.render(); 47;"}})); + h.run_steps(1); + assert!(h.state().session.script_ui.render_host.is_some()); + assert!(matches!(second.try_recv(), Err(TryRecvError::Empty))); + let done = first.try_recv().expect("an immutable completed reply does not enter the new script's undo group"); + assert_eq!(done["result"]["rendering"], false); + assert_eq!(done["result"]["items"][0]["statusLabel"], "Done"); + gate.0.store(true, Ordering::SeqCst); + assert_eq!(finish(&mut h, &second)["result"]["result"], 47); +} + +#[test] +fn replacing_project_before_render_poll_returns_an_explicit_error() { + use effectcraft_ui_egui::control::{Outcome, handle}; + + let (mut h, gate, _tx) = harness(); + let request = ControlRequest::new("engine.execute", json!({"command": "renderQueue.render"})).0; + let Outcome::AwaitJobs(waiter) = handle(h.state_mut(), &egui::Context::default(), &request, 0.0) else { panic!("expected a deferred render") }; + gate.0.store(true, Ordering::SeqCst); + wait_worker(&h); + h.state_mut().session.replace_project(Default::default(), None); + assert!(!waiter.pending(h.state())); + let reply = waiter.finish(h.state_mut()); + assert_eq!(reply["ok"], false, "{reply}"); + assert!(reply["error"].as_str().unwrap().contains("replaced before completion")); +} diff --git a/crates/ui-egui/tests/it/ui_text_edit.rs b/crates/ui-egui/tests/it/ui_text_edit.rs index 6b68028de..24a43522f 100644 --- a/crates/ui-egui/tests/it/ui_text_edit.rs +++ b/crates/ui-egui/tests/it/ui_text_edit.rs @@ -169,6 +169,36 @@ fn type_tool_click_type_edit_and_commit() { assert!(h.state().session.state.text_edit.is_none()); } +/// Fit New Layers to Work Area with the playhead past the work area: the Type tool's new layer +/// spans the work area and the playhead moves into it, so the text shows while it is typed and +/// after it is committed. +#[test] +fn type_tool_text_fitted_to_the_work_area_past_the_playhead_shows() { + let mut h = harness(app()); + h.state_mut().show_panel(PanelKind::Composition); + let s = &mut h.state_mut().session; + s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap(); + s.execute("comp.workArea", json!({"start": 0.5, "end": 1})).unwrap(); + s.set_time(effectcraft_engine::time::Tick::from_seconds_f64(1.5)); + h.run_steps(3); + h.state_mut().ui.tool = effectcraft_ui_egui::state::Tool::Type; + h.run_steps(1); + let comp = rect(&h, "viewer.comp"); + click(&mut h, comp.center(), Modifiers::NONE); + type_text(&mut h, "Hi"); + assert!(h.state().auto.find("viewer.textEdit").is_some(), "the edit region shows"); + let (lid, text, _, _) = edited(&h); + assert_eq!(text, "Hi"); + key(&mut h, Key::Escape, Modifiers::NONE); + assert!(h.state().session.state.text_edit.is_none(), "committed"); + let s = &h.state().session; + let l = s.active_comp().unwrap().layer(LayerId(lid)).unwrap(); + let t = s.time(); + assert!(l.in_point <= t && t < l.out_point, "the committed text is on screen: {t:?} in {:?}..{:?}", l.in_point, l.out_point); + assert_eq!(((l.in_point.seconds() * 10.0).round(), (l.out_point.seconds() * 10.0).round()), (5.0, 10.0), "the layer keeps the work area"); + assert_eq!(layer_doc(s, LayerId(lid)).unwrap().text, "Hi"); +} + /// #831: clicking another panel (a solid's row in the Timeline) ends text editing and gives the /// keyboard back, whether the edit began with the Type tool (which stays selected) or by command: /// typed characters change no text and Space plays. The Character panel keeps the edit, and a diff --git a/crates/ui-egui/tests/it/ui_timeline_marquee.rs b/crates/ui-egui/tests/it/ui_timeline_marquee.rs index bf6dab74e..9d72f08aa 100644 --- a/crates/ui-egui/tests/it/ui_timeline_marquee.rs +++ b/crates/ui-egui/tests/it/ui_timeline_marquee.rs @@ -13,9 +13,9 @@ use serde_json::json; const REACH: f32 = 5.5; /// A comp with one solid, its Opacity keys at 1 s and 2 s (linear) revealed. -fn harness() -> (Harness<'static, EffectcraftApp>, u64) { +fn harness_for(duration: f64) -> (Harness<'static, EffectcraftApp>, u64) { let mut s = Session::default(); - s.execute("comp.new", json!({"name": "Box", "width": 320, "height": 180, "frameRate": 30, "duration": 4})).unwrap(); + s.execute("comp.new", json!({"name": "Box", "width": 320, "height": 180, "frameRate": 30, "duration": duration})).unwrap(); let id = s.execute("layer.newSolid", json!({"name": "L", "color": "#e04020", "width": 80, "height": 80})).unwrap()["layer"].as_u64().unwrap(); for (t, v) in [(1.0, 0.0), (2.0, 100.0)] { s.execute("prop.addKey", json!({"layer": id, "path": "transform/opacity", "time": t, "value": v})).unwrap(); @@ -29,6 +29,10 @@ fn harness() -> (Harness<'static, EffectcraftApp>, u64) { (h, id) } +fn harness() -> (Harness<'static, EffectcraftApp>, u64) { + harness_for(4.0) +} + /// The center of the key at `secs`: the middle of its hit rect, which is centered on the glyph. fn key_center(h: &Harness<'_, EffectcraftApp>, id: u64, secs: f64) -> Pos2 { let comp = h.state().session.active_comp().unwrap(); @@ -122,3 +126,265 @@ fn a_box_dragged_across_the_outline_and_the_graph_selects_rows_and_keys() { assert_eq!(keys, [1.0, 1.0]); assert_eq!(selected(&h), (both.clone(), both), "graph → outline"); } + +/// Thirty expanded layers force row virtualization and leave room for wheel and edge scrolling. +fn rows_harness() -> (Harness<'static, EffectcraftApp>, Vec) { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Rows", "width": 320, "height": 180, "frameRate": 30, "duration": 4})).unwrap(); + let mut ids = vec![]; + for i in 0..30 { + let id = s.execute("layer.newSolid", json!({"name": format!("Layer {i}"), "color": "#406080"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("prop.addKey", json!({"layer": id, "path": "transform/opacity", "time": 1.0, "value": 50})).unwrap(); + ids.push(id); + } + s.execute("layer.select", json!({"layers": ids})).unwrap(); + let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_eframe(|_| EffectcraftApp::new(s)); + h.run_steps(3); + let ctx = h.ctx.clone(); + effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "timeline.reveal.opacity", json!({})).unwrap(); + h.run_steps(4); + (h, ids) +} + +/// The originally enclosed key stays in the box when wheel scrolling takes its row offscreen. +#[test] +fn scrolling_rows_during_a_marquee_keeps_offscreen_keys_in_the_box() { + let (mut h, ids) = rows_harness(); + let top = *ids.last().unwrap(); + let c = key_center(&h, top, 1.0); + let from = c - vec2(20.0, 5.0); + let to = c + vec2(20.0, 30.0); + h.event(Event::PointerMoved(from)); + h.step(); + h.event(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.step(); + h.event(Event::PointerMoved(to)); + h.run_steps(2); + h.event(Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + delta: vec2(0.0, -400.0), + modifiers: Modifiers::NONE, + phase: egui::TouchPhase::Move, + source: egui::MouseWheelSource::Wheel, + }); + h.run_steps(20); + assert!(h.state().ui.timeline.scroll_y > 300.0, "the real wheel handler scrolled rows"); + h.event(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(2); + let selected: Vec<_> = h.state().session.state.selected_keys.iter().map(|k| k.layer.0).collect(); + assert!(selected.contains(&top), "the key on layer {top} scrolled offscreen but remains inside the content-anchored marquee; actual layers {selected:?}"); +} + +fn press(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2) { + h.event(Event::PointerMoved(from)); + h.step(); + h.event(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.step(); + h.event(Event::PointerMoved(to)); + h.run_steps(2); +} + +fn release(h: &mut Harness<'_, EffectcraftApp>, to: Pos2) { + h.event(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(2); +} + +fn wheel(h: &mut Harness<'_, EffectcraftApp>, delta: Vec2) { + h.event(Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + delta, + modifiers: Modifiers::NONE, + phase: egui::TouchPhase::Move, + source: egui::MouseWheelSource::Wheel, + }); + h.run_steps(20); +} + +fn times(h: &Harness<'_, EffectcraftApp>) -> Vec { + h.state().session.state.selected_keys.iter().map(|k| k.time.seconds()).collect() +} + +/// Panning time retains hidden keys while both painting and row selection respect the graph boundary. +#[test] +fn horizontal_scroll_keeps_hidden_keys_without_selecting_outline_layers() { + let (mut h, id) = harness_for(20.0); + h.state_mut().ui.timeline.pps = Some(200.0); + h.run_steps(3); + let c = key_center(&h, id, 1.0); + let to = c + vec2(20.0, 5.0); + press(&mut h, c - vec2(20.0, 5.0), to); + wheel(&mut h, vec2(-400.0, 0.0)); + assert!(h.state().ui.timeline.start > 1.5, "time panned: {}", h.state().ui.timeline.start); + let graph_left = h.state().auto.find("timeline.ruler").unwrap().rect[0]; + let box_shapes: Vec<_> = + h.output().shapes.iter().filter(|s| matches!(&s.shape, egui::Shape::Rect(r) if r.rect.max == to && (r.rect.height() - 10.0).abs() < 0.01)).collect(); + assert!(!box_shapes.is_empty(), "the held marquee is drawn"); + assert!(box_shapes.iter().all(|s| s.clip_rect.min.x >= graph_left), "a graph-only box cannot paint over the outline when its anchor scrolls out"); + release(&mut h, to); + assert_eq!(times(&h), [1.0, 2.0]); + // Keys select their owning layer, but panning the anchor under the outline must not select + // unrelated layer rows. The separate cross-column test below covers those rows. + assert_eq!(h.state().session.state.selected_layers, [LayerId(id)]); +} + +/// Returning the viewport shrinks the box again without editing the document or its history. +#[test] +fn shrinking_after_scrolling_removes_keys_again_and_selection_does_not_edit_the_project() { + let (mut h, id) = harness_for(20.0); + h.state_mut().ui.timeline.pps = Some(200.0); + h.run_steps(3); + let before = h.state().session.project.to_json(); + let revision = h.state().session.revision; + let undo = h.state().session.history.undo.len(); + let c = key_center(&h, id, 1.0); + let to = c + vec2(20.0, 5.0); + press(&mut h, c - vec2(20.0, 5.0), to); + wheel(&mut h, vec2(-400.0, 0.0)); + wheel(&mut h, vec2(400.0, 0.0)); + assert!(h.state().ui.timeline.start < 0.01); + release(&mut h, to); + assert_eq!(times(&h), [1.0], "shrinking is not a union of every key passed"); + assert_eq!(h.state().session.project.to_json(), before); + assert_eq!(h.state().session.revision, revision); + assert_eq!(h.state().session.history.undo.len(), undo); +} + +/// Invalidated gestures leave the prior selection intact even if the pointer keeps moving. +#[test] +fn cancellation_preserves_selection_and_does_not_restart_until_a_new_press() { + for cancel in ["escape", "ownership", "focus", "layout", "resize", "edit", "comp"] { + let (mut h, id) = harness(); + h.state_mut().session.execute("keys.select", json!({"keys": [{"layer":id,"path":"transform/opacity","time":2.0}]})).unwrap(); + h.run_steps(2); + let c = key_center(&h, id, 1.0); + let to = c + vec2(10.0, 5.0); + press(&mut h, c - vec2(20.0, 5.0), to); + assert_eq!(times(&h), [2.0], "selection commits only on release"); + match cancel { + "escape" => h.event(Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }), + "ownership" => h.ctx.set_dragged_id(egui::Id::unique("another-owner")), + "focus" => h.input_mut().focused = false, + "layout" => h.state_mut().ui.timeline.search = "no matching rows".into(), + "resize" => { + h.set_size(vec2(1400.0, 900.0)); + } + "edit" => { + h.state_mut().session.execute("prop.set", json!({"layer":id,"path":"transform/opacity","value":75})).unwrap(); + } + "comp" => { + h.state_mut().session.execute("comp.new", json!({"name":"Other"})).unwrap(); + } + _ => unreachable!(), + } + h.run_steps(2); + let expected = times(&h); + h.event(Event::PointerMoved(to + vec2(1.0, 0.0))); + h.step(); + release(&mut h, to); + assert_eq!(times(&h), expected, "{cancel} cancelled without applying the box"); + if !matches!(cancel, "comp" | "edit") { + assert_eq!(times(&h), [2.0], "{cancel} preserves the prior selection"); + } + } +} + +/// A release-frame pointer move takes effect, modifiers add, and locked keys remain excluded. +#[test] +fn release_uses_the_release_position_and_additive_modifiers_and_excludes_locked_keys() { + for modifiers in [Modifiers::NONE, Modifiers::SHIFT, Modifiers::COMMAND] { + let (mut h, id) = harness(); + h.state_mut().session.execute("keys.select", json!({"keys": [{"layer":id,"path":"transform/opacity","time":2.0}]})).unwrap(); + h.run_steps(2); + let c = key_center(&h, id, 1.0); + press(&mut h, c - vec2(25.0, 5.0), c - vec2(15.0, 5.0)); + h.event(Event::ModifiersChanged(modifiers)); + h.event(Event::PointerMoved(c + vec2(10.0, 5.0))); + h.event(Event::PointerButton { pos: c + vec2(10.0, 5.0), button: egui::PointerButton::Primary, pressed: false, modifiers }); + h.run_steps(2); + let mut actual = times(&h); + actual.sort_by(f64::total_cmp); + assert_eq!(actual, if modifiers == Modifiers::NONE { vec![1.0] } else { vec![1.0, 2.0] }); + h.state_mut().session.execute("layer.setSwitch", json!({"layers":[id],"switch":"lock","value":true})).unwrap(); + h.event(Event::ModifiersChanged(Modifiers::NONE)); + h.run_steps(2); + assert!(marquee(&mut h, c - vec2(20.0, 5.0), c + vec2(10.0, 5.0)).is_empty(), "locked keys stay excluded"); + } +} + +/// Cross-column selection uses logical headers, including those no longer rendered after scrolling. +#[test] +fn crossing_into_the_outline_after_scrolling_selects_offscreen_layer_rows() { + let (mut h, ids) = rows_harness(); + let c = key_center(&h, *ids.last().unwrap(), 1.0); + let outline = h.state().auto.find("timeline.layerMarquee").unwrap().rect; + let to = pos2(outline[0] + 40.0, c.y + 30.0); + press(&mut h, c + vec2(20.0, -5.0), to); + wheel(&mut h, vec2(0.0, -400.0)); + assert!(h.state().ui.timeline.scroll_y > 300.0); + release(&mut h, to); + let selected = &h.state().session.state.selected_layers; + assert!(selected.contains(&LayerId(ids[ids.len() - 2])), "the second header is inside the box although it scrolled offscreen"); + assert!(selected.len() > 5, "all intervening rows are selected"); +} + +/// Edge scrolling continues without pointer motion and stops as soon as the gesture is cancelled. +#[test] +fn a_stationary_pointer_at_either_edge_scrolls_only_until_release_or_cancel() { + let (mut h, ids) = rows_harness(); + let c = key_center(&h, *ids.last().unwrap(), 1.0); + let area = h.state().auto.find("timeline.layerMarquee").unwrap().rect; + let bottom = pos2(c.x + 20.0, area[1] + area[3] - 2.0); + press(&mut h, c - vec2(20.0, 5.0), bottom); + let before = h.state().ui.timeline.scroll_y; + h.run_steps(30); + assert!(h.state().ui.timeline.scroll_y > before + 50.0, "a stationary drag requests more frames and scrolls"); + let top = pos2(bottom.x, area[1] + 2.0); + h.event(Event::PointerMoved(top)); + h.step(); + let before = h.state().ui.timeline.scroll_y; + h.run_steps(10); + assert!(h.state().ui.timeline.scroll_y < before, "the top edge scrolls back"); + h.event(Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }); + h.step(); + let before = h.state().ui.timeline.scroll_y; + h.run_steps(10); + assert_eq!(h.state().ui.timeline.scroll_y, before, "Escape stops edge scrolling"); + release(&mut h, top); + assert!(times(&h).is_empty(), "cancelled box selects nothing"); +} + +/// Batched wheel events cannot move the press anchor or make release query stale row positions. +#[test] +fn wheel_on_press_captures_the_old_anchor_and_wheel_on_release_uses_the_new_rows() { + for scroll_on_press in [true, false] { + let (mut h, ids) = rows_harness(); + let top = *ids.last().unwrap(); + let c = key_center(&h, top, 1.0); + let from = c - vec2(20.0, 5.0); + let to = c + vec2(20.0, 30.0); + if scroll_on_press { + h.event(Event::PointerMoved(from)); + h.step(); + h.event(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + } else { + press(&mut h, from, to); + } + h.event(Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + delta: vec2(0.0, -400.0), + modifiers: Modifiers::NONE, + phase: egui::TouchPhase::Move, + source: egui::MouseWheelSource::Wheel, + }); + if !scroll_on_press { + h.event(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + } + h.step(); + if scroll_on_press { + h.event(Event::PointerMoved(to)); + h.run_steps(20); + release(&mut h, to); + } + assert!(h.state().session.state.selected_keys.iter().any(|k| k.layer == LayerId(top)), "scroll_on_press={scroll_on_press}"); + } +} diff --git a/crates/ui-egui/tests/it/ui_timeline_trim.rs b/crates/ui-egui/tests/it/ui_timeline_trim.rs index 6f95f744f..9aaf1f500 100644 --- a/crates/ui-egui/tests/it/ui_timeline_trim.rs +++ b/crates/ui-egui/tests/it/ui_timeline_trim.rs @@ -45,18 +45,46 @@ fn timing(h: &Harness<'_, EffectcraftApp>, layer: u64) -> (f64, f64, f64) { } fn drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2) { + drag_with(h, from, to, Modifiers::NONE); +} + +fn drag_with(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2, modifiers: Modifiers) { h.event(Event::PointerMoved(from)); + h.event(Event::ModifiersChanged(modifiers)); h.step(); - h.event(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.event(Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers }); h.step(); for k in 1..=10 { h.event(Event::PointerMoved(from + (to - from) * (k as f32 / 10.0))); h.step(); } - h.event(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.event(Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers }); + h.event(Event::ModifiersChanged(Modifiers::NONE)); h.run_steps(3); } +/// Shift-dragging a bar end or the bar snaps it to another layer's In / Out point a few pixels +/// away; without Shift it stops on the frame the pointer reached (#1828). +#[test] +fn shift_dragging_a_bar_snaps_to_other_layers() { + for shift in [false, true] { + let (mut h, solid, pre) = harness(); + let mods = if shift { Modifiers::SHIFT } else { Modifiers::NONE }; + let (b, p) = (bar(&h, solid), bar(&h, pre)); + let y = b.center().y; + // The solid's Out end (6 s), grabbed 2 px inside, to 6 px short of the precomp's Out (7 s). + drag_with(&mut h, pos2(b.max.x - 2.0, y), pos2(p.max.x - 8.0, y), mods); + let (i, o, _) = timing(&h, solid); + assert!((i - 2.0).abs() < 1e-6, "shift {shift}: in stays at 2 s, got {i}"); + assert_eq!((o - 7.0).abs() < 1e-6, shift, "shift {shift}: out {o} snaps to 7 s only with Shift"); + // The whole bar, so its In (2 s) lands 5 px short of the precomp's In (3 s). + let b = bar(&h, solid); + drag_with(&mut h, pos2(b.center().x, y), pos2(b.center().x + p.min.x - b.min.x - 5.0, y), mods); + let (i, _, _) = timing(&h, solid); + assert_eq!((i - 3.0).abs() < 1e-6, shift, "shift {shift}: in {i} snaps to 3 s only with Shift"); + } +} + /// Both ends of every bar show the trim cursor, just inside and just outside the end; the middle /// doesn't. The same holds for a selected layer, a twirled-open layer and a short bar. fn assert_trim_cursor_at_ends(h: &mut Harness<'_, EffectcraftApp>, layer: u64, case: &str) { @@ -120,6 +148,19 @@ fn dragging_bar_ends_trims_in_and_out() { assert!((i - 3.0).abs() < 1e-6 && (o - 8.0).abs() < 1.5 / 30.0 && s.abs() < 1e-6, "out extended to ~8 s, not slipped: in {i} out {o} start {s}"); } +/// A Shift-drag on an unselected bar adds it to the selection and moves the whole selection +/// together, so the other selected layers keep their offset (#1828). +#[test] +fn shift_dragging_an_unselected_bar_moves_the_selection() { + let (mut h, solid, pre) = harness(); + h.state_mut().session.execute("layer.select", json!({"layers": [pre]})).unwrap(); + h.run_steps(2); + let b = bar(&h, solid); + drag_with(&mut h, b.center(), b.center() + vec2(b.width() / 4.0 * 2.5, 0.0), Modifiers::SHIFT); + let ((si, ..), (pi, ..)) = (timing(&h, solid), timing(&h, pre)); + assert!(si > 4.0 && (pi - si - 1.0).abs() < 1e-6, "both moved by the same amount: solid in {si}, precomp in {pi}"); +} + /// With Frames and Frame Count Start at 1, In moves to 00061 but the Duration column still counts /// the layer's 120 frames (#1807). #[test] diff --git a/crates/ui-egui/tests/it/ui_unreadable_footage.rs b/crates/ui-egui/tests/it/ui_unreadable_footage.rs index 21ca1e7c9..4d283d62d 100644 --- a/crates/ui-egui/tests/it/ui_unreadable_footage.rs +++ b/crates/ui-egui/tests/it/ui_unreadable_footage.rs @@ -184,11 +184,15 @@ fn restored_original_and_active_proxy_render_new_pixels_through_media_and_disk_c let cached = s.render(comp, Tick::ZERO, RenderOpts::default()); assert_eq!(cached.data, before.data); assert!(cache.stats().hits > 0, "the same media was read back through the disk cache"); + let changed_time = std::fs::metadata(&active).unwrap().modified().unwrap() + std::time::Duration::from_secs(7); std::fs::remove_file(&active).unwrap(); assert_eq!(s.execute("footage.reloadChanged", json!({})).unwrap()["reloaded"], 0, "availability alone drives recovery in this fixture"); let offline = s.render(comp, Tick::ZERO, RenderOpts::default()); assert_eq!(offline.data[8 * 16 + 8], [0.0, 0.0, 0.0, 0.0]); image::RgbaImage::from_pixel(16, 16, image::Rgba([0, 0, 255, 255])).save(&active).unwrap(); + // Changed media needs a distinct stamp, even when both writes land in one filesystem + // clock tick. Same-size, same-stamp replacements require explicit Reload Footage. + std::fs::File::options().append(true).open(&active).unwrap().set_modified(changed_time).unwrap(); assert_eq!(s.execute("footage.reloadChanged", json!({})).unwrap()["reloaded"], 0, "availability alone drives recovery in this fixture"); let restored = s.render(comp, Tick::ZERO, RenderOpts::default()); assert_eq!(restored.data[8 * 16 + 8], [0.0, 0.0, 1.0, 1.0], "restored pixels replace decoded and composited cached frames"); diff --git a/crates/ui-egui/tests/it/ui_viewer.rs b/crates/ui-egui/tests/it/ui_viewer.rs index 7b32fa2ec..264f19ad0 100644 --- a/crates/ui-egui/tests/it/ui_viewer.rs +++ b/crates/ui-egui/tests/it/ui_viewer.rs @@ -198,11 +198,10 @@ fn viewer_click_picks_nothing_while_the_solo_layer_is_inactive() { assert!(h.state().session.state.selected_layers.is_empty(), "{back:?} stays excluded by solo"); } -/// A selected layer under the pointer takes the press before the layers in front of it, as in -/// After Effects: it stays selected and a drag moves it. With none selected there, the topmost -/// layer does (#230). +/// A click, left or right, selects the topmost layer under the pointer, even over a selected +/// layer behind it (#1535); a drag still moves a selected layer behind others (#230). #[test] -fn viewer_press_prefers_a_selected_layer_under_the_pointer() { +fn viewer_click_selects_the_topmost_layer_and_a_drag_moves_the_selected_one() { let mut h = selection_harness(); let comp = h.state().session.active_comp().unwrap(); let (front, back) = (comp.layers[0].id, comp.layers[1].id); @@ -210,7 +209,20 @@ fn viewer_press_prefers_a_selected_layer_under_the_pointer() { h.run_steps(3); let at = screen(&h, [320.0, 180.0]); click(&mut h, at); - assert_eq!(h.state().session.state.selected_layers, vec![back], "a click keeps the selected layer behind"); + assert_eq!(h.state().session.state.selected_layers, vec![front], "a click selects the layer in front of the selected one (#1535)"); + // A right-click selects what a left click would, so the menu acts on the layer in front, + // here where both layers cover the pointer and the layer behind is selected. + h.state_mut().session.execute("layer.select", json!({"layers": [back.0]})).unwrap(); + h.run_steps(3); + for pressed in [true, false] { + h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Default::default() }); + h.step(); + } + h.run_steps(2); + assert_eq!(h.state().session.state.selected_layers, vec![front], "a right-click selects the layer in front"); + h.key_press(egui::Key::Escape); + h.run_steps(3); + h.state_mut().session.execute("layer.select", json!({"layers": [back.0]})).unwrap(); h.run_steps(40); let (front0, back0) = (position_and_anchor(&h, front).0, position_and_anchor(&h, back).0); drag(&mut h, at, at + vec2(60.0, 0.0)); @@ -224,6 +236,71 @@ fn viewer_press_prefers_a_selected_layer_under_the_pointer() { assert_eq!(h.state().session.state.selected_layers, vec![front], "nothing selected: the topmost layer"); } +/// A click, left or right, on a path of the selected layer, where a layer in front covers it, stays +/// on that path's layer rather than selecting the layer in front, so a double-click there +/// free-transforms the path (#1535 keeps this from `pick`). +#[test] +fn viewer_double_click_on_a_covered_path_of_the_selected_layer_free_transforms_it() { + let mut h = selection_harness(); + let comp = h.state().session.active_comp().unwrap(); + let back = comp.layers[1].id; + let s = &mut h.state_mut().session; + s.execute("layer.select", json!({"layers": [back.0]})).unwrap(); + // In Back's own space (80×80): its top edge runs through comp (320, 150), under Front. + s.execute("mask.new", json!({"layer": back.0, "vertices": [[10, 10], [70, 10], [70, 70], [10, 70]], "closed": true})).unwrap(); + h.run_steps(40); + let at = screen(&h, [320.0, 150.0]); + h.input_mut().events.push(Event::PointerMoved(at)); + h.step(); + // A right-click there acts on the path's layer too, as a left click does. + for pressed in [true, false] { + h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Secondary, pressed, modifiers: Default::default() }); + h.step(); + } + h.run_steps(2); + assert_eq!(h.state().session.state.selected_layers, vec![back], "a right-click on the path keeps its layer"); + h.key_press(egui::Key::Escape); + h.run_steps(40); + // Two clicks a frame apart: the first selects as a click does, the second double-clicks. + for _ in 0..2 { + for pressed in [true, false] { + h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }); + } + h.step(); + } + h.run_steps(3); + assert_eq!(h.state().session.state.selected_layers, vec![back], "the path's layer stays selected"); + assert_eq!(h.state().session.state.selected_vertices.len(), 4, "all the path's points are selected"); + assert!(h.state().auto.find("viewer.freeTransform.handle.0").is_some(), "the free-transform box is up"); +} + +/// A double-click where a precomp layer covers the selected layer opens the precomp: like a single +/// click, it acts on the topmost layer, which its first click selects (#1535). +#[test] +fn viewer_double_click_opens_the_topmost_layer_over_a_selected_one() { + let mut s = Session::default(); + let inner = s.execute("comp.new", json!({"name": "Inner", "width": 80, "height": 80, "duration": 4})).unwrap()["comp"].as_u64().unwrap(); + s.execute("comp.new", json!({"name": "Main", "width": 640, "height": 360, "duration": 4})).unwrap(); + s.execute("layer.newSolid", json!({"name": "Back", "color": "#406080", "width": 200, "height": 200})).unwrap(); + s.execute("layer.addItem", json!({"item": "Inner"})).unwrap(); + let back = s.active_comp().unwrap().layers[1].id; + s.execute("layer.select", json!({"layers": [back.0]})).unwrap(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(move |_| EffectcraftApp::new(s)); + h.run_steps(40); + let at = screen(&h, [320.0, 180.0]); + h.input_mut().events.push(Event::PointerMoved(at)); + h.step(); + // Two clicks a frame apart: the first selects as a click does, the second double-clicks. + for _ in 0..2 { + for pressed in [true, false] { + h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() }); + } + h.step(); + } + h.run_steps(3); + assert_eq!(h.state().session.active_comp_id().map(|c| c.0), Some(inner), "the precomp in front opens"); +} + /// Each composition keeps its Resolution, saved with the project as in After Effects: chosen for /// comp A in the viewer's popup, it is still A's after showing comp B, costs no undo step, keeps /// A's cached frames and is read back from the saved file (#417). @@ -1068,6 +1145,38 @@ fn shape_pen_draws_a_closed_shape_layer() { assert_eq!(sp.vertices.len(), 4); } +/// Fit New Layers to Work Area with the playhead past the work area: the Pen's new shape layer +/// spans the work area and the playhead moves into it, so the path and its vertex controls show +/// and the closing click lands. +#[test] +fn pen_draws_on_a_layer_fitted_to_the_work_area_past_the_playhead() { + let mut h = harness(); + let s = &mut h.state_mut().session; + s.execute("prefs.set", json!({"key": "general.fitNewLayersToWorkArea", "value": true})).unwrap(); + s.execute("comp.workArea", json!({"start": 1, "end": 2})).unwrap(); + s.set_time(effectcraft_engine::time::Tick::from_seconds_f64(3.0)); + h.state_mut().ui.tool = Tool::Pen; + h.run_steps(2); + let n0 = h.state().session.active_comp().unwrap().layers.len(); + for p in [[200.0, 100.0], [400.0, 120.0], [300.0, 260.0]] { + let at = screen(&h, p); + click(&mut h, at); + } + let l = h.state().session.active_comp().unwrap().layers[0].clone(); + let t = h.state().session.time(); + assert!(l.in_point <= t && t < l.out_point, "the playhead is on the new layer: {t:?} in {:?}..{:?}", l.in_point, l.out_point); + assert_eq!((l.in_point.seconds().round(), l.out_point.seconds().round()), (1.0, 2.0), "the layer keeps the work area"); + let path = l.props.sub("contents").unwrap().groups().next().unwrap().sub("contents").unwrap().groups().next().unwrap().clone(); + assert!(h.state().auto.find(&format!("viewer.shapePath.{}.vertex.0", path.uid)).is_some(), "the Pen's vertex controls show"); + let at = screen(&h, [200.0, 100.0]); + click(&mut h, at); + let comp = h.state().session.active_comp().unwrap().clone(); + assert_eq!(comp.layers.len(), n0 + 1); + let sp = comp.layers[0].props.find_group(path.uid).unwrap().get("path").unwrap().value.as_path().unwrap().clone(); + assert_eq!(sp.vertices.len(), 3); + assert!(sp.closed, "the click on the first vertex closes the path"); +} + #[test] fn motion_path_key_drag_edits_that_key() { let mut h = harness(); @@ -2370,7 +2479,7 @@ fn command_clicking_a_group_twirl_opens_its_whole_tree() { // twirls open with it, and closed again with it. let mut h = harness(); let id = layer_id(&h, "Box"); - h.state_mut().session.execute("effect.apply", json!({"effect": "Lumetri Color", "layers": [id]})).unwrap(); + h.state_mut().session.execute("effect.apply", json!({"effect": "Color Grading", "layers": [id]})).unwrap(); fn walk(g: &effectcraft_engine::project::PropGroup, out: &mut Vec) { for sg in g.groups() { out.push(sg.uid); diff --git a/crates/ui-egui/tests/it/ui_web_offload.rs b/crates/ui-egui/tests/it/ui_web_offload.rs index fc0a0cd32..fde177cb6 100644 --- a/crates/ui-egui/tests/it/ui_web_offload.rs +++ b/crates/ui-egui/tests/it/ui_web_offload.rs @@ -241,7 +241,14 @@ fn waiting_commands_report_only_their_own_completion_errors() { let other = if kind == JobKind::Track { JobKind::Warp } else { JobKind::Track }; let mut app = EffectcraftApp::new(Session::default()); app.session.offload_log = vec![(kind, Some("old error".into())), (other, Some("unrelated failure".into())), (kind, None)]; - let waiter = || JobWaiter { command: "analysis".into(), params: json!({}), result: json!({"running": true}), jobs: vec![job_id.into()], log_cursor: 1 }; + let waiter = || JobWaiter { + command: "analysis".into(), + params: json!({}), + result: json!({"running": true}), + jobs: vec![job_id.into()], + log_cursor: 1, + render: None, + }; let result = waiter().finish(&mut app); assert_eq!(result, json!({"ok": true, "result": {"running": false}}), "{kind:?}"); app.session.offload_log.push((kind, Some("nothing was applied".into()))); @@ -258,6 +265,6 @@ fn waiting_command_sees_its_error_after_the_completion_log_rolls_over() { app.session.offload_log = vec![(JobKind::Warp, None); 31]; app.session.offload_log.push((JobKind::Track, Some("nothing was applied".into()))); app.session.offload_log_start = 1; - let waiter = JobWaiter { command: "track.analyze".into(), params: json!({}), result: json!({}), jobs: vec!["track".into()], log_cursor: 32 }; + let waiter = JobWaiter { command: "track.analyze".into(), params: json!({}), result: json!({}), jobs: vec!["track".into()], log_cursor: 32, render: None }; assert_eq!(waiter.finish(&mut app), json!({"ok": false, "error": "nothing was applied"})); } diff --git a/crates/vp9enc/Cargo.toml b/crates/vp9enc/Cargo.toml deleted file mode 100644 index fe2e767c5..000000000 --- a/crates/vp9enc/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "effectcraft-vp9enc" -description = "VP9 encoder (profile 0, 8-bit 4:2:0; intra + inter frames, loop filter, rate control) for WebM export, from the VP9 Bitstream & Decoding Process Specification" -version.workspace = true -edition.workspace = true -license.workspace = true -rust-version.workspace = true - -[lints] -workspace = true - -[dependencies] diff --git a/crates/vp9enc/README.md b/crates/vp9enc/README.md deleted file mode 100644 index b9f94faea..000000000 --- a/crates/vp9enc/README.md +++ /dev/null @@ -1,47 +0,0 @@ -# effectcraft-vp9enc - -A small VP9 encoder for WebM export (profile 0, 8-bit 4:2:0, BT.709). - -* **Key frames**: 8×8 intra blocks (DC / V / H / TM prediction chosen per block), 4×4 DCT/ADST - transforms, or the Walsh–Hadamard transform at quality 100 (mathematically lossless). -* **Inter frames**: one reference (the previous frame, LAST), 16×16 or 8×8 blocks chosen by a - cost comparison; motion search from the predicted vectors (NearestMv / NearMv / last frame's - co-located vector) with a whole-sample cross/diamond search refined to half and quarter - samples with the regular 8-tap interpolation filters; NEARESTMV / NEARMV / ZEROMV / NEWMV - chosen by bit cost; blocks with no coefficients worth coding are skipped; 4×4 DCT residual - with a rate–distortion check per transform block. Inter frames are coded error resilient - (`error_resilient_mode` = 1: no dependence on earlier probability contexts or motion vectors), - so `UsePrevFrameMvs` is never needed. No high-precision (1/8) vectors, no compound prediction, - no intra blocks inside inter frames. -* **Loop filter**: the in-loop deblocking filter (§8.8) on every lossy frame; the level is the - best of three trials against the source (sharpness 0, no mode/reference deltas). -* **Probabilities**: per-frame forward updates in the compressed header (`diff_update_prob` / - `update_mv_prob`) of the coefficient, skip, inter-mode, is-inter, reference, partition and - motion-vector probabilities. Tiles are recorded as symbols first, the frame's own counts pick - the updates that pay for their signalling, then the symbols are entropy coded with the updated - probabilities. There is no backward adaptation (`refresh_frame_context` = 0). -* **Rate control**: constant quality (quality → `base_q_idx`) or a target bitrate: a per-frame - q index from a bits ≈ complexity / step model per frame type, key frames getting a larger share - of each key-frame interval, with the running over/undershoot paid back over about a second. -* **Key frame interval** is configurable (1 = every frame a key frame, the old intra-only mode). - -The encoder reconstructs exactly what a decoder reconstructs (inter prediction with the -decoder's vector clamping and edge-clamped reference fetches, integer inverse transforms, the -loop filter) and exposes it (`Vp9Encoder::reconstruction`); the tests require FilmCraft's VP9 -decoder (and ffmpeg, when installed) to output exactly that reconstruction, frame by frame, for -moving synthetic sequences at several sizes and qualities, with tile columns, and lossless. - -On a moving-gradient sequence (192×108, 24 frames) inter coding is 3.9–7× smaller than all-intra -at the same quality setting with equal or better PSNR. - -Written from the **VP9 Bitstream & Decoding Process Specification, version 0.6 (31 March -2016)**: the encoder emits the uncompressed and compressed headers (§6.2, §6.3), partition, mode -info, motion vector and token syntax (§6.4), derives motion vector candidates with the §6.4 -`find_mv_refs` / `find_best_ref_mvs` processes, and reconstructs with the specification's intra -and inter prediction (§8.5), integer inverse transforms (§8.7) and loop filter (§8.8) so it stays -in step with any conformant decoder. The boolean encoder is the inverse of the §9.2 decoder in -the form given in RFC 6386 §7.3. Spec tables (§10) are the specification's; the key-frame tables -were transcribed from FilmCraft's first-party decoder `filmcraft-vp9` (MIT OR Apache-2.0), which -is also the decoder the tests round-trip through and whose behaviour (loop filter edge rules, -vector candidate order) the encoder was checked against; ffmpeg is used only as an external test -oracle. No libvpx or other third-party encoder/decoder source was consulted. diff --git a/crates/vp9enc/src/bool.rs b/crates/vp9enc/src/bool.rs deleted file mode 100644 index 9eda40541..000000000 --- a/crates/vp9enc/src/bool.rs +++ /dev/null @@ -1,259 +0,0 @@ -//! Boolean (arithmetic) encoder: the inverse of the VP9 boolean decoder (spec §9.2), written as -//! the classic carry-propagating range coder described in RFC 6386 §7.3 (VP8 and VP9 share the -//! bool coder). - -pub struct BoolEncoder { - out: Vec, - range: u32, - bottom: u32, - bit_count: i32, - /// Start of the run of 0xFF bytes at the end of `out` (`out.len()` when it ends in another - /// byte). A carry only touches that run and the byte before it. - ff_start: usize, -} - -impl Default for BoolEncoder { - fn default() -> Self { - BoolEncoder::new() - } -} - -impl BoolEncoder { - pub fn new() -> Self { - BoolEncoder { out: Vec::new(), range: 255, bottom: 0, bit_count: 24, ff_start: 0 } - } - - fn push_byte(&mut self, b: u8) { - if b != 255 { - self.ff_start = self.out.len() + 1; - } - self.out.push(b); - } - - /// Carry into the bytes written so far: the trailing 0xFF run becomes 0x00 and the byte - /// before it grows by one (a carry out of the first byte is dropped). Each 0xFF byte is - /// cleared once, so a long run costs O(1) per byte instead of a rescan for every carry. - fn add_one(&mut self) { - let end = self.out.len(); - let start = self.ff_start.min(end); - if let Some(run) = self.out.get_mut(start..) { - run.fill(0); - } - self.ff_start = end; - if let Some(i) = start.checked_sub(1) - && let Some(b) = self.out.get_mut(i) - { - *b = b.wrapping_add(1); - if *b == 255 { - // The grown byte joins the 0xFF bytes before it. - let mut s = i; - while s > 0 && self.out.get(s - 1) == Some(&255) { - s -= 1; - } - self.ff_start = s; - } - } - } - - /// Write `bit` with probability `prob`/256 of it being 0. - #[inline] - pub fn write(&mut self, bit: bool, prob: u8) { - let split = 1 + (((self.range - 1) * prob as u32) >> 8); - if bit { - self.bottom = self.bottom.wrapping_add(split); - self.range -= split; - } else { - self.range = split; - } - while self.range < 128 { - self.range <<= 1; - if self.bottom & (1 << 31) != 0 { - self.add_one(); - } - self.bottom <<= 1; - self.bit_count -= 1; - if self.bit_count == 0 { - self.push_byte((self.bottom >> 24) as u8); - self.bottom &= (1 << 24) - 1; - self.bit_count = 8; - } - } - } - - pub fn literal(&mut self, v: u32, bits: u32) { - for i in (0..bits).rev() { - self.write((v >> i) & 1 != 0, 128); - } - } - - /// Flush (RFC 6386 §7.3) and return the bytes. - pub fn finish(mut self) -> Vec { - let mut c = self.bit_count; - let mut v = self.bottom; - if v & (1u32 << (32 - c)) != 0 { - self.add_one(); - } - v <<= c & 7; - c >>= 3; - while { - c -= 1; - c >= 0 - } { - v <<= 8; - } - for _ in 0..4 { - self.push_byte((v >> 24) as u8); - v <<= 8; - } - self.out - } -} - -#[cfg(test)] -mod tests { - use super::*; - - /// A straightforward decoder per VP9 spec §9.2. - struct Dec<'a> { - d: &'a [u8], - pos: usize, - value: u64, - bits: i32, - range: u32, - } - impl<'a> Dec<'a> { - fn new(d: &'a [u8]) -> Self { - let mut x = Dec { d, pos: 0, value: 0, bits: 0, range: 255 }; - x.fill(); - x - } - fn fill(&mut self) { - while self.bits <= 48 { - let b = if self.pos < self.d.len() { self.d[self.pos] } else { 0 }; - self.pos += 1; - self.value |= (b as u64) << (56 - self.bits); - self.bits += 8; - } - } - fn read(&mut self, p: u8) -> bool { - let split = 1 + (((self.range - 1) * p as u32) >> 8); - let big = (split as u64) << 56; - let bit = if self.value >= big { - self.range -= split; - self.value -= big; - true - } else { - self.range = split; - false - }; - while self.range < 128 { - self.range <<= 1; - self.value <<= 1; - self.bits -= 1; - } - self.fill(); - bit - } - } - - #[test] - fn round_trip() { - let mut e = BoolEncoder::new(); - let mut seq = vec![]; - let mut s = 12345u32; - for i in 0..20000 { - s = s.wrapping_mul(1103515245).wrapping_add(12345); - let p = ((s >> 8) % 255 + 1) as u8; - let bit = (s >> 20) % 256 >= p as u32 || i % 97 == 0; - seq.push((bit, p)); - e.write(bit, p); - } - let d = e.finish(); - let mut dec = Dec::new(&d); - for (i, (b, p)) in seq.into_iter().enumerate() { - assert_eq!(dec.read(p), b, "bit {i}"); - } - } - - #[test] - fn all_ones_carry() { - let mut e = BoolEncoder::new(); - for _ in 0..5000 { - e.write(true, 1); - } - let d = e.finish(); - let mut dec = Dec::new(&d); - for _ in 0..5000 { - assert!(dec.read(1)); - } - } - - /// The encoder as it was before the carry run was tracked: a carry rescans the bytes. - struct Scan { - out: Vec, - range: u32, - bottom: u32, - bit_count: i32, - } - impl Scan { - fn add_one(&mut self) { - let mut i = self.out.len(); - while i > 0 { - i -= 1; - if self.out[i] == 255 { - self.out[i] = 0; - } else { - self.out[i] += 1; - return; - } - } - } - fn write(&mut self, bit: bool, prob: u8) { - let split = 1 + (((self.range - 1) * prob as u32) >> 8); - if bit { - self.bottom = self.bottom.wrapping_add(split); - self.range -= split; - } else { - self.range = split; - } - while self.range < 128 { - self.range <<= 1; - if self.bottom & (1 << 31) != 0 { - self.add_one(); - } - self.bottom <<= 1; - self.bit_count -= 1; - if self.bit_count == 0 { - self.out.push((self.bottom >> 24) as u8); - self.bottom &= (1 << 24) - 1; - self.bit_count = 8; - } - } - } - } - - #[test] - fn carry_run_tracking_matches_the_rescan() { - let mut seed = 99u32; - for case in 0..300 { - let mut fast = BoolEncoder::new(); - let mut slow = Scan { out: vec![], range: 255, bottom: 0, bit_count: 24 }; - // Skewed probabilities make long 0xFF runs and carries likely. - let extreme = case % 3; - for _ in 0..4000 { - seed ^= seed << 13; - seed ^= seed >> 17; - seed ^= seed << 5; - let p = match extreme { - 0 => (seed >> 8) as u8 | 1, - 1 => 1 + (seed >> 8) as u8 % 4, - _ => 255 - (seed >> 8) as u8 % 4, - }; - let bit = (seed >> 24) % 256 >= p as u32 || (seed >> 3).is_multiple_of(11); - fast.write(bit, p); - slow.write(bit, p); - assert_eq!(fast.out, slow.out, "case {case}"); - } - } - } -} diff --git a/crates/vp9enc/src/lf.rs b/crates/vp9enc/src/lf.rs deleted file mode 100644 index d48c57db5..000000000 --- a/crates/vp9enc/src/lf.rs +++ /dev/null @@ -1,299 +0,0 @@ -//! The in-loop deblocking filter (spec 8.8), as the decoder applies it to a frame coded with 4×4 -//! transforms, 8×8 / 16×16 blocks, no segmentation and no reference/mode deltas: superblocks in -//! raster order, for each plane the vertical edges (pass 0) then the horizontal edges (pass 1). -//! Transform edges inside a skipped inter block are left alone; every 4×4 edge uses the 4-tap -//! filter except edges on a 32-sample boundary, which get the 8-tap (flat) filter. - -use crate::{BLOCK_16X16, Mi, Plane}; - -#[derive(Clone, Copy)] -struct Limits { - limit: i32, - blimit: i32, - thresh: i32, -} - -/// Mode-info grid and thresholds shared by every superblock of this frame. -struct FrameFilter<'a> { - mi: &'a [Mi], - mi_rows: usize, - mi_cols: usize, - limits: Limits, -} - -/// Filter the reconstructed planes (Y, U, V, or a prefix of them) in place at `level` (0 = off; -/// sharpness 0). -pub fn filter_frame(planes: &mut [Plane], mi: &[Mi], mi_rows: usize, mi_cols: usize, level: u8) { - if level == 0 { - return; - } - let l = level as i32; - let limit = l.max(1); - let lim = Limits { limit, blimit: 2 * (l + 2) + limit, thresh: l >> 4 }; - let filter = FrameFilter { mi, mi_rows, mi_cols, limits: lim }; - for sbr in 0..mi_rows.div_ceil(8) { - for sbc in 0..mi_cols.div_ceil(8) { - for (plane, p) in planes.iter_mut().enumerate() { - for pass in 0..2 { - filter.sb_plane(p, plane, pass, sbr * 8, sbc * 8); - } - } - } - } -} - -impl FrameFilter<'_> { - fn sb_plane(&self, p: &mut Plane, plane: usize, pass: usize, row: usize, col: usize) { - let Self { mi, mi_rows, mi_cols, limits: lim } = *self; - let (sx, sy) = if plane > 0 { (1, 1) } else { (0, 0) }; - let (sub, edge_len) = if pass == 0 { (sx, 64 >> sy) } else { (sy, 64 >> sx) }; - let stride = p.w; - for edge in 0..(16usize >> sub) { - let mut i = 0; - while i < edge_len { - let (x, y) = if pass == 0 { (col * 8 + edge * (4 << sx), row * 8 + (i << sy)) } else { (col * 8 + (i << sx), row * 8 + edge * (4 << sy)) }; - if x >= 8 * mi_cols || y >= 8 * mi_rows || (pass == 0 && x == 0) || (pass == 1 && y == 0) { - i += 8; - continue; - } - let lc = ((x >> 3) >> sx) << sx; - let lr = ((y >> 3) >> sy) << sy; - let m = mi[lr * mi_cols + lc]; - let size = if sub == 0 { m.size } else { m.size.max(BLOCK_16X16) }; - let n8 = if size >= BLOCK_16X16 { 2 } else { 1 }; - let is_block_edge = if pass == 0 { x & (8 * n8 - 1) == 0 } else { y & (8 * n8 - 1) == 0 }; - let tx_ok = !m.inter || !m.skip; - if !is_block_edge && !tx_ok { - i += 8; - continue; - } - let wide = edge % 8 == 0; - let n = if pass == 0 { (8 * mi_rows - y).div_ceil(1 << sy) } else { (8 * mi_cols - x).div_ceil(1 << sx) }.min(8).min(edge_len - i); - // A transform edge is not filtered in the last odd chroma column. - let odd_last = pass == 1 && sx == 1 && mi_cols & 1 == 1 && edge & 1 == 1; - let (px, py) = (x >> sx, y >> sy); - let mut apply = [false; LANES]; - for (l, a) in apply.iter_mut().enumerate().take(n) { - let is_tx_edge = tx_ok && !(odd_last && x + (l << sx) + 8 >= mi_cols * 8); - *a = is_block_edge || is_tx_edge; - } - if apply.iter().any(|&a| a) { - let pos = py * stride + px; - if pass == 0 { - filter_group::(&mut p.rec, pos, stride, 1, apply, wide, lim); - } else { - filter_group::(&mut p.rec, pos, 1, stride, apply, wide, lim); - } - } - i += 8; - } - } - } -} - -const LANES: usize = 8; - -/// [`filter_lane`] on the 8 lanes of a group at once (lane `l` has its q0 at `pos + l * along`; -/// samples across the edge are `step` apart; `ROWS`: `along` is 1), for lanes whose `apply` is -/// set. Every lane is computed with both filters and the result selected per lane (0 / -1 -/// masks), so the arithmetic vectorizes; lanes left alone are written back unchanged. -#[inline(always)] -fn filter_group(d: &mut [u8], pos: usize, along: usize, step: usize, apply: [bool; LANES], wide: bool, lim: Limits) { - type T = i16; - let mut v = [[0 as T; LANES]; 8]; - let start = pos - 4 * step; - if ROWS { - for (k, vk) in v.iter_mut().enumerate() { - let Some(row) = d.get(start + k * step..).and_then(|r| r.first_chunk::()) else { return }; - *vk = row.map(|s| s as T); - } - } else { - for l in 0..LANES { - let Some(line) = d.get(start + l * along..).and_then(|r| r.first_chunk::<8>()) else { return }; - for k in 0..8 { - v[k][l] = line[k] as T; - } - } - } - let (limit, blimit, thresh) = (lim.limit as T, lim.blimit as T, lim.thresh as T); - let le = |a: T, b: T| -((a <= b) as T); - let sel = |m: T, a: T, b: T| (a & m) | (b & !m); - let (p3, p2, p1, p0, q0, q1, q2, q3) = (&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7]); - let apply: [T; LANES] = apply.map(|a| -(a as T)); - let can_flat = -(wide as T); - let mut mask = [0 as T; LANES]; - let mut hev = [0 as T; LANES]; - let mut flat = [0 as T; LANES]; - for l in 0..LANES { - let d10 = (p1[l] - p0[l]).abs(); - let e10 = (q1[l] - q0[l]).abs(); - mask[l] = apply[l] - & le((p3[l] - p2[l]).abs(), limit) - & le((p2[l] - p1[l]).abs(), limit) - & le(d10, limit) - & le(e10, limit) - & le((q2[l] - q1[l]).abs(), limit) - & le((q3[l] - q2[l]).abs(), limit) - & le((p0[l] - q0[l]).abs() * 2 + (p1[l] - q1[l]).abs() / 2, blimit); - hev[l] = !(le(d10, thresh) & le(e10, thresh)); - flat[l] = can_flat - & mask[l] - & le(d10, 1) - & le(e10, 1) - & le((p2[l] - p0[l]).abs(), 1) - & le((q2[l] - q0[l]).abs(), 1) - & le((p3[l] - p0[l]).abs(), 1) - & le((q3[l] - q0[l]).abs(), 1); - } - if mask.iter().fold(0, |a, &m| a | m) == 0 { - return; - } - let mut out = v; - let c = |x: T| x.clamp(-128, 127); - for l in 0..LANES { - let (ps1, ps0, qs0, qs1) = (p1[l] - 128, p0[l] - 128, q0[l] - 128, q1[l] - 128); - let filter = c((c(ps1 - qs1) & hev[l]) + 3 * (qs0 - ps0)); - let f1 = c(filter + 4) >> 3; - let f2 = c(filter + 3) >> 3; - let f = (f1 + 1) >> 1; - let narrow = mask[l] & !flat[l]; - let outer = narrow & !hev[l]; - out[4][l] = sel(narrow, c(qs0 - f1) + 128, out[4][l]); - out[3][l] = sel(narrow, c(ps0 + f2) + 128, out[3][l]); - out[5][l] = sel(outer, c(qs1 - f) + 128, out[5][l]); - out[2][l] = sel(outer, c(ps1 + f) + 128, out[2][l]); - } - if flat.iter().fold(0, |a, &f| a | f) != 0 { - // 8-tap: positions p2..q2 (indices 1..7), each the rounded mean of a 7-wide window - // (indices clamped to 0..8) plus itself, as a sliding sum. - let s = |k: isize| &v[k.clamp(0, 7) as usize]; - let mut sum = [0 as T; LANES]; - for j in -2isize..=4 { - let a = s(j); - for l in 0..LANES { - sum[l] += a[l]; - } - } - for k in 1usize..7 { - let ki = k as isize; - let (cur, add, sub) = (s(ki), s(ki + 4), s(ki - 3)); - for l in 0..LANES { - let w8 = (sum[l] + cur[l] + 4) >> 3; - out[k][l] = sel(flat[l], w8, out[k][l]); - sum[l] += add[l] - sub[l]; - } - } - } - if ROWS { - for (k, ok) in out.iter().enumerate().take(7).skip(1) { - if let Some(row) = d.get_mut(start + k * step..).and_then(|r| r.first_chunk_mut::()) { - for l in 0..LANES { - row[l] = ok[l] as u8; - } - } - } - } else { - for l in 0..LANES { - if let Some(line) = d.get_mut(start + l * along..).and_then(|r| r.first_chunk_mut::<8>()) { - for k in 1..7 { - line[k] = out[k][l] as u8; - } - } - } - } -} - -/// Filter mask, narrow (4-tap) and 8-tap flat filters of spec 8.8.5 across one edge position; -/// `pos` is q0, samples across the edge are `step` apart. -#[cfg(test)] -fn filter_lane(d: &mut [u8], pos: usize, step: usize, wide: bool, lim: Limits) { - let at = |k: isize| d[(pos as isize + k * step as isize) as usize] as i32; - let (p3, p2, p1, p0) = (at(-4), at(-3), at(-2), at(-1)); - let (q0, q1, q2, q3) = (at(0), at(1), at(2), at(3)); - let Limits { limit, blimit, thresh } = lim; - let mask = (p3 - p2).abs() <= limit - && (p2 - p1).abs() <= limit - && (p1 - p0).abs() <= limit - && (q1 - q0).abs() <= limit - && (q2 - q1).abs() <= limit - && (q3 - q2).abs() <= limit - && (p0 - q0).abs() * 2 + (p1 - q1).abs() / 2 <= blimit; - if !mask { - return; - } - let flat = - wide && (p1 - p0).abs() <= 1 && (q1 - q0).abs() <= 1 && (p2 - p0).abs() <= 1 && (q2 - q0).abs() <= 1 && (p3 - p0).abs() <= 1 && (q3 - q0).abs() <= 1; - let mut set = |k: isize, v: i32| d[(pos as isize + k * step as isize) as usize] = v as u8; - if flat { - // 8-tap: positions p2..q2, each the rounded mean of a 7-wide clamped window plus itself. - let s = [p3, p2, p1, p0, q0, q1, q2, q3]; - let px = |k: isize| s[(k.clamp(-4, 3) + 4) as usize]; - for k in -3isize..3 { - let mut sum = px(k); - for j in -3..=3 { - sum += px(k + j); - } - set(k, (sum + 4) >> 3); - } - return; - } - let c = |v: i32| v.clamp(-128, 127); - let hev = (p1 - p0).abs() > thresh || (q1 - q0).abs() > thresh; - let (ps1, ps0, qs0, qs1) = (p1 - 128, p0 - 128, q0 - 128, q1 - 128); - let filter = c(if hev { c(ps1 - qs1) } else { 0 } + 3 * (qs0 - ps0)); - let f1 = c(filter + 4) >> 3; - let f2 = c(filter + 3) >> 3; - set(0, c(qs0 - f1) + 128); - set(-1, c(ps0 + f2) + 128); - if !hev { - let f = (f1 + 1) >> 1; - set(1, c(qs1 - f) + 128); - set(-2, c(ps1 + f) + 128); - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn group_matches_per_lane_filter() { - let mut seed = 0x1234_5678u32; - let mut rnd = || { - seed ^= seed << 13; - seed ^= seed >> 17; - seed ^= seed << 5; - seed - }; - for case in 0..20000 { - let stride = 24; - let base: Vec = { - let flatish = case % 3 == 0; - let c = (rnd() % 256) as i32; - (0..stride * 24) - .map(|_| if flatish { (c + (rnd() % 3) as i32 - 1).clamp(0, 255) as u8 } else { (c + (rnd() % 40) as i32 - 20).clamp(0, 255) as u8 }) - .collect() - }; - let l = (rnd() % 64) as i32; - let lim = Limits { limit: l.max(1), blimit: 2 * (l + 2) + l.max(1), thresh: l >> 4 }; - let wide = rnd() & 1 == 1; - let apply: [bool; LANES] = std::array::from_fn(|_| rnd() % 4 != 0); - for pass in 0..2 { - let (mut a, mut b) = (base.clone(), base.clone()); - let pos = 8 * stride + 8; - for (l, &ap) in apply.iter().enumerate() { - if ap { - let (p, st) = if pass == 0 { (pos + l * stride, 1) } else { (pos + l, stride) }; - filter_lane(&mut a, p, st, wide, lim); - } - } - if pass == 0 { - filter_group::(&mut b, pos, stride, 1, apply, wide, lim); - } else { - filter_group::(&mut b, pos, 1, stride, apply, wide, lim); - } - assert_eq!(a, b, "case {case} pass {pass}"); - } - } - } -} diff --git a/crates/vp9enc/src/lib.rs b/crates/vp9enc/src/lib.rs deleted file mode 100644 index 12ad77ae7..000000000 --- a/crates/vp9enc/src/lib.rs +++ /dev/null @@ -1,483 +0,0 @@ -//! A VP9 encoder (profile 0, 8-bit 4:2:0), written from the *VP9 Bitstream & Decoding Process -//! Specification* v0.6 by mirroring its decoding process in reverse (see README.md). -//! -//! * **Key frames**: 64×64 superblocks split to 8×8 intra blocks (DC / V / H / TM chosen per -//! block), 4×4 DCT/ADST transforms (the Walsh–Hadamard transform at quality 100: lossless). -//! * **Inter frames** (error resilient, single reference: the previous frame as LAST): 16×16 or -//! 8×8 blocks with motion vectors found by a candidate + diamond search refined to quarter -//! samples with the regular 8-tap filters; NEARESTMV / NEARMV / ZEROMV / NEWMV chosen by cost, -//! motion vector prediction exactly as the decoder derives it; skipped blocks; 4×4 DCT residual. -//! * **In-loop deblocking** (8.8) on every lossy frame; the level is picked per frame by trial. -//! * **Probabilities**: per-frame forward updates (compressed header) of the coefficient, skip, -//! inter mode, reference, partition and motion vector probabilities, chosen from the frame's -//! own symbol counts (no backward adaptation: `refresh_frame_context` = 0). -//! * **Rate control**: constant quality (quality → base_q_idx), or a target bitrate with a -//! per-frame q_idx model. -//! -//! The encoder reconstructs exactly what a decoder reconstructs (prediction, integer inverse -//! transforms, loop filter), so prediction never drifts; [`Vp9Encoder::reconstruction`] returns -//! it. - -#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] - -mod bool; -mod lf; -mod mc; -mod probs; -mod tables; -mod tile; -mod transform; - -pub use mc::Mv; -use probs::Probs; -use tables::{AC_QLOOKUP, DC_QLOOKUP}; -use transform::Quant; - -/// Largest width or height the 16-bit frame size fields can hold. -const MAX_DIMENSION: u32 = 1 << 16; - -pub(crate) const BLOCK_8X8: u8 = 3; -pub(crate) const BLOCK_16X16: u8 = 6; - -/// Encoder settings. -#[derive(Clone, Debug, PartialEq)] -pub struct EncoderConfig { - /// Frame size in samples; the stream stores size - 1 in 16 bits, so values above 65536 are - /// clamped to 65536 by [`Vp9Encoder::new`]. - pub width: u32, - pub height: u32, - /// 1..=100; 100 is lossless. With a target bitrate: the quality of the first frame. - pub quality: u8, - /// Full-range (0–255) rather than limited (16–235) YUV; only signalled. - pub full_range: bool, - /// A key frame every this many frames (1 = every frame is a key frame; 0 = only the first). - pub keyframe_interval: u32, - /// Target bitrate in kbit/s (`None`: constant quality). - pub target_kbps: Option, - /// Frames per second (rate control). - pub frame_rate: f64, - /// The in-loop deblocking filter. - pub loop_filter: bool, -} - -impl Default for EncoderConfig { - fn default() -> Self { - EncoderConfig { width: 0, height: 0, quality: 80, full_range: false, keyframe_interval: 60, target_kbps: None, frame_rate: 30.0, loop_filter: true } - } -} - -/// One coded frame. -#[derive(Clone, Debug)] -pub struct EncodedFrame { - pub data: Vec, - pub key: bool, - pub q_idx: u8, - pub filter_level: u8, -} - -/// A plane being coded: source and reconstruction at superblock-aligned size. -pub(crate) struct Plane { - /// Stride (superblock-aligned width). - pub w: usize, - /// Last valid column / row for intra prediction edges (the 8-aligned mode-info grid). - pub max_x: usize, - pub max_y: usize, - /// Visible size. - pub vis_w: usize, - pub vis_h: usize, - pub src: Vec, - pub rec: Vec, -} - -impl Plane { - fn new(data: &[u8], vw: usize, vh: usize, w: usize, h: usize, max_x: usize, max_y: usize) -> Plane { - let mut src = vec![0u8; w * h]; - for y in 0..h { - let sy = y.min(vh.saturating_sub(1)); - for x in 0..w { - let sx = x.min(vw.saturating_sub(1)); - src[y * w + x] = data.get(sy * vw + sx).copied().unwrap_or(128); - } - } - Plane { w, max_x, max_y, vis_w: vw, vis_h: vh, src, rec: vec![0; w * h] } - } -} - -/// Mode info of one 8×8 position. -#[derive(Clone, Copy, Debug, Default)] -pub(crate) struct Mi { - pub inter: bool, - /// Intra mode (0..=9) or inter mode (NEARESTMV..=NEWMV). - pub mode: u8, - pub mv: Mv, - /// Skip as the loop filter sees it (inter blocks without coefficients). - pub skip: bool, - pub size: u8, -} - -/// Rate control state (target bitrate mode). -#[derive(Clone, Debug)] -struct RateControl { - /// bits ≈ complexity / step: per frame type, from the last frame of that type. - key_complexity: Option, - inter_complexity: Option, - /// Bits spent minus bits budgeted so far. - debt: f64, - last_inter_q: Option, -} - -pub struct Vp9Encoder { - cfg: EncoderConfig, - base_qidx: u8, - lossless: bool, - frames: u64, - since_key: u32, - /// The reconstruction of the last frame (the LAST reference): planes at their strides. - reference: Option<[Plane; 3]>, - mvs: Vec, - rc: RateControl, -} - -fn quality_to_qidx(q: u8) -> u8 { - // 99 ≈ 3, 90 ≈ 30, 50 ≈ 150, 1 ≈ 255. - ((100 - q.clamp(1, 99) as i32) * 255 / 85).clamp(1, 255) as u8 -} - -impl Vp9Encoder { - pub fn new(mut cfg: EncoderConfig) -> Self { - // The frame header stores width - 1 and height - 1 in 16 bits. - cfg.width = cfg.width.min(MAX_DIMENSION); - cfg.height = cfg.height.min(MAX_DIMENSION); - let q = cfg.quality.clamp(1, 100); - let lossless = q >= 100 && cfg.target_kbps.is_none(); - let base_qidx = if lossless { 0 } else { quality_to_qidx(q) }; - Vp9Encoder { - cfg, - base_qidx, - lossless, - frames: 0, - since_key: 0, - reference: None, - mvs: vec![], - rc: RateControl { key_complexity: None, inter_complexity: None, debt: 0.0, last_inter_q: None }, - } - } - - pub fn config(&self) -> &EncoderConfig { - &self.cfg - } - - /// Encode one frame from planar 8-bit 4:2:0 (`y`: width×height, `u`/`v`: - /// ((w+1)/2)×((h+1)/2), tightly packed). - pub fn encode_yuv420(&mut self, y: &[u8], u: &[u8], v: &[u8]) -> Vec { - self.encode_frame(y, u, v, false).data - } - - /// The decoder's reconstruction of the last coded frame (visible area, tightly packed Y, U, - /// V). - pub fn reconstruction(&self) -> Option<[Vec; 3]> { - let r = self.reference.as_ref()?; - Some(std::array::from_fn(|i| { - let p = &r[i]; - (0..p.vis_h).flat_map(|y| p.rec[y * p.w..y * p.w + p.vis_w].iter().copied()).collect() - })) - } - - fn is_key_due(&self, force: bool) -> bool { - force || self.reference.is_none() || self.cfg.keyframe_interval == 1 || (self.cfg.keyframe_interval > 1 && self.since_key >= self.cfg.keyframe_interval) - } - - /// The q index of the next frame. - fn pick_qidx(&mut self, key: bool) -> u8 { - if self.lossless { - return 0; - } - let Some(kbps) = self.cfg.target_kbps else { return self.base_qidx }; - let per_frame = kbps as f64 * 1000.0 / self.cfg.frame_rate.max(1.0); - let interval = if self.cfg.keyframe_interval == 0 { 300.0 } else { self.cfg.keyframe_interval as f64 }; - // Key frames get a larger share of the group's budget. - let boost = (interval / 6.0).clamp(1.0, 5.0); - let group = per_frame * interval; - let key_share = group * boost / (boost + interval - 1.0); - let inter_share = (group - key_share) / (interval - 1.0).max(1.0); - let mut target = if key { key_share } else { inter_share }; - // Pay back (or spend) the running difference over about a second. - let horizon = self.cfg.frame_rate.max(1.0); - target = (target - self.rc.debt / horizon).max(target * 0.25); - let complexity = if key { self.rc.key_complexity } else { self.rc.inter_complexity.or(self.rc.key_complexity.map(|c| c / (boost * 2.0))) }; - let Some(cx) = complexity else { return self.base_qidx }; - let step = (cx / target).max(4.0); - let mut q = (0..=255u8).min_by_key(|&q| ((AC_QLOOKUP[q as usize] as f64 - step).abs() * 16.0) as i64).unwrap_or(self.base_qidx); - if !key && let Some(last) = self.rc.last_inter_q { - q = q.clamp(last.saturating_sub(24), last.saturating_add(24)); - } - q.max(1) - } - - fn rc_update(&mut self, key: bool, qidx: u8, bytes: usize) { - let Some(kbps) = self.cfg.target_kbps else { return }; - let bits = bytes as f64 * 8.0; - let cx = bits * AC_QLOOKUP[qidx as usize] as f64; - if key { - self.rc.key_complexity = Some(cx); - } else { - self.rc.inter_complexity = Some(match self.rc.inter_complexity { - Some(c) => c * 0.5 + cx * 0.5, - None => cx, - }); - self.rc.last_inter_q = Some(qidx); - } - let per_frame = kbps as f64 * 1000.0 / self.cfg.frame_rate.max(1.0); - self.rc.debt += bits - per_frame; - } - - /// Encode one frame; `force_key` starts a new key frame. - pub fn encode_frame(&mut self, y: &[u8], u: &[u8], v: &[u8], force_key: bool) -> EncodedFrame { - let (w, h) = (self.cfg.width.max(1) as usize, self.cfg.height.max(1) as usize); - let key = self.is_key_due(force_key); - let qidx = self.pick_qidx(key); - let lossless = qidx == 0; - let mi_cols = w.div_ceil(8); - let mi_rows = h.div_ceil(8); - let sb_cols = mi_cols.div_ceil(8); - let sb_rows = mi_rows.div_ceil(8); - let (pw, ph) = (sb_cols * 64, sb_rows * 64); - let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); - let mut planes = [ - Plane::new(y, w, h, pw, ph, mi_cols * 8 - 1, mi_rows * 8 - 1), - Plane::new(u, cw, ch, pw / 2, ph / 2, mi_cols * 4 - 1, mi_rows * 4 - 1), - Plane::new(v, cw, ch, pw / 2, ph / 2, mi_cols * 4 - 1, mi_rows * 4 - 1), - ]; - // Tile columns: as few as the width allows (tile_info, 6.2.13). - let mut min_log2 = 0; - while (64usize << min_log2) < sb_cols { - min_log2 += 1; - } - let mut max_log2 = 1; - while (sb_cols >> max_log2) >= 4 { - max_log2 += 1; - } - max_log2 -= 1; - let tile_cols = 1usize << min_log2; - let quant = Quant { dcq: DC_QLOOKUP[qidx as usize], acq: AC_QLOOKUP[qidx as usize], lossless, round_dc: 0.5, round_ac: 0.38 }; - let step = AC_QLOOKUP[qidx as usize] as f64 / 8.0; - let lambda_sse = 0.35 * step * step; - let refs = if key { - None - } else { - self.reference.as_ref().map(|r| std::array::from_fn::<_, 3, _>(|i| mc::RefPlane { data: &r[i].rec, stride: r[i].w, w: r[i].vis_w, h: r[i].vis_h })) - }; - let prev = (!key && self.mvs.len() == mi_cols * mi_rows).then_some(&self.mvs[..]); - let phases = refs.as_ref().map(|r| mc::PhasePlanes::new(&r[0], 48)); - let fp = tile::FrameParams { key, quant, mi_cols, mi_rows, refs, phases, prev_mvs: prev, lambda_sad: lambda_sse.sqrt(), lambda_sse }; - let mut mi = vec![Mi::default(); mi_cols * mi_rows]; - let mut writers = vec![]; - for t in 0..tile_cols { - let start = (((t * sb_cols) >> min_log2) * 8).min(mi_cols); - let end = ((((t + 1) * sb_cols) >> min_log2) * 8).min(mi_cols); - let mut tile = tile::Tile::new(&fp, &mut planes, &mut mi, start, end); - tile.code(); - writers.push(tile.w); - } - // Forward probability updates from this frame's counts. - let defaults = Probs::defaults(); - let mut counts = vec![[0u32; 2]; probs::NPROBS]; - for wr in &writers { - wr.count(&mut counts); - } - let wanted = probs::plan_updates(&defaults, &counts, !key); - let (compressed, used) = probs::compressed_header(&defaults, &wanted, lossless, !key); - let tiles: Vec> = writers.iter().map(|wr| wr.emit(&used)).collect(); - drop(fp); - // Loop filter: the level with the least error against the source. - let level = if lossless || !self.cfg.loop_filter { 0 } else { choose_level(&mut planes, &mi, mi_rows, mi_cols, qidx, key) }; - lf::filter_frame(&mut planes, &mi, mi_rows, mi_cols, level); - // Uncompressed header. - let mut bw = BitWriter::default(); - bw.put(2, 2); // frame_marker - bw.put(0, 1); // profile low bit - bw.put(0, 1); // profile high bit - bw.put(0, 1); // show_existing_frame - bw.put(!key as u32, 1); // frame_type - bw.put(1, 1); // show_frame - if key { - bw.put(0, 1); // error_resilient_mode - bw.put(0x49, 8); - bw.put(0x83, 8); - bw.put(0x42, 8); - bw.put(2, 3); // color_space CS_BT_709 - bw.put(self.cfg.full_range as u32, 1); - bw.put(w as u32 - 1, 16); - bw.put(h as u32 - 1, 16); - bw.put(0, 1); // render_and_frame_size_different - bw.put(0, 1); // refresh_frame_context - bw.put(1, 1); // frame_parallel_decoding_mode - } else { - bw.put(1, 1); // error_resilient_mode (no reliance on earlier contexts or vectors) - bw.put(0x01, 8); // refresh_frame_flags: slot 0 becomes the new LAST - for _ in 0..3 { - bw.put(0, 3); // ref_frame_idx: slot 0 - bw.put(0, 1); // sign bias - } - bw.put(1, 1); // found_ref: the size of LAST - bw.put(0, 1); // render_and_frame_size_different - bw.put(0, 1); // allow_high_precision_mv - bw.put(0, 1); // is_filter_switchable - bw.put(1, 2); // raw_interpolation_filter → EIGHTTAP (regular) - } - bw.put(0, 2); // frame_context_idx - bw.put(level as u32, 6); // loop_filter_level - bw.put(0, 3); // sharpness - bw.put(0, 1); // mode_ref_delta_enabled - bw.put(qidx as u32, 8); - bw.put(0, 1); // delta_q_y_dc - bw.put(0, 1); // delta_q_uv_dc - bw.put(0, 1); // delta_q_uv_ac - bw.put(0, 1); // segmentation_enabled - if min_log2 < max_log2 { - bw.put(0, 1); // no extra tile columns - } - bw.put(0, 1); // tile_rows_log2 = 0 - bw.put(compressed.len() as u32, 16); - let mut out = bw.finish(); - out.extend_from_slice(&compressed); - let n = tiles.len(); - for (i, t) in tiles.into_iter().enumerate() { - if i + 1 < n { - out.extend_from_slice(&(t.len() as u32).to_be_bytes()); - } - out.extend_from_slice(&t); - } - // A frame mustn't end in a byte that reads as a superframe marker (0b110xxxxx, Annex B): - // decoders would take its last bytes for a superframe index and reject the frame. The - // last tile's bool-coded data may end in zero padding (9.2.3), so a zero byte fixes it. - if out.last().is_some_and(|b| b & 0xe0 == 0xc0) { - out.push(0); - } - // State for the next frame. - self.mvs = mi.iter().map(|m| if m.inter { m.mv } else { Mv::ZERO }).collect(); - self.reference = Some(planes); - self.frames += 1; - self.since_key = if key { 1 } else { self.since_key + 1 }; - self.rc_update(key, qidx, out.len()); - EncodedFrame { data: out, key, q_idx: qidx, filter_level: level } - } -} - -/// Try a few loop filter levels on the luma reconstruction; the one closest to the source -/// (squared error over the visible area) wins (0 when filtering doesn't help). -fn choose_level(planes: &mut [Plane; 3], mi: &[Mi], mi_rows: usize, mi_cols: usize, qidx: u8, key: bool) -> u8 { - let guess = ((qidx as f64 * if key { 0.25 } else { 0.2 }).round() as u8).clamp(1, 63); - let sse = |p: &Plane| -> u64 { - let mut s = 0u64; - for y in 0..p.vis_h { - for x in 0..p.vis_w { - let d = p.src[y * p.w + x] as i64 - p.rec[y * p.w + x] as i64; - s += (d * d) as u64; - } - } - s - }; - let mut best = (sse(&planes[0]), 0u8); - let saved = planes[0].rec.clone(); - for level in [guess / 2, guess, (guess as u16 * 3 / 2).min(63) as u8] { - if level == 0 { - continue; - } - lf::filter_frame(&mut planes[..1], mi, mi_rows, mi_cols, level); - let s = sse(&planes[0]); - planes[0].rec.copy_from_slice(&saved); - if s < best.0 { - best = (s, level); - } - } - best.1 -} - -#[derive(Default)] -struct BitWriter { - out: Vec, - acc: u32, - n: u32, -} - -impl BitWriter { - fn put(&mut self, v: u32, bits: u32) { - for i in (0..bits).rev() { - self.acc = (self.acc << 1) | ((v >> i) & 1); - self.n += 1; - if self.n == 8 { - self.out.push(self.acc as u8); - self.acc = 0; - self.n = 0; - } - } - } - fn finish(mut self) -> Vec { - if self.n > 0 { - self.out.push((self.acc << (8 - self.n)) as u8); - } - self.out - } -} - -// ---------------------------------------------------------------- colour conversion - -/// BT.709 RGB(A) 8-bit → planar YUV 4:2:0 (limited or full range). -pub fn rgba_to_yuv420(rgba: &[u8], w: u32, h: u32, full_range: bool) -> (Vec, Vec, Vec) { - let (w, h) = (w as usize, h as usize); - let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); - let mut y = vec![0u8; w * h]; - let mut u = vec![0u8; cw * ch]; - let mut v = vec![0u8; cw * ch]; - let (ys, yo, cs) = if full_range { (255.0, 0.0, 255.0) } else { (219.0, 16.0, 224.0) }; - let px = |x: usize, yy: usize| -> [f32; 3] { - let i = (yy.min(h - 1) * w + x.min(w - 1)) * 4; - [rgba[i] as f32 / 255.0, rgba[i + 1] as f32 / 255.0, rgba[i + 2] as f32 / 255.0] - }; - let luma = |c: [f32; 3]| 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]; - for yy in 0..h { - for x in 0..w { - y[yy * w + x] = (luma(px(x, yy)) * ys + yo).round().clamp(0.0, 255.0) as u8; - } - } - for cy in 0..ch { - for cx in 0..cw { - let mut acc = [0.0f32; 3]; - for (dx, dy) in [(0, 0), (1, 0), (0, 1), (1, 1)] { - let c = px(cx * 2 + dx, cy * 2 + dy); - for k in 0..3 { - acc[k] += c[k] / 4.0; - } - } - let l = luma(acc); - let cb = (acc[2] - l) / 1.8556; - let cr = (acc[0] - l) / 1.5748; - u[cy * cw + cx] = (cb * cs + 128.0).round().clamp(0.0, 255.0) as u8; - v[cy * cw + cx] = (cr * cs + 128.0).round().clamp(0.0, 255.0) as u8; - } - } - (y, u, v) -} - -/// The alpha channel as a luma plane (full range) with neutral chroma — the second VP9 stream of -/// WebM alpha. -pub fn alpha_to_yuv420(rgba: &[u8], w: u32, h: u32) -> (Vec, Vec, Vec) { - let (w, h) = (w as usize, h as usize); - let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); - let y: Vec = rgba.as_chunks::<4>().0.iter().take(w * h).map(|p| p[3]).collect(); - (y, vec![128; cw * ch], vec![128; cw * ch]) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn sizes_beyond_the_16_bit_header_fields_are_clamped() { - let enc = Vp9Encoder::new(EncoderConfig { width: 70_000, height: u32::MAX, ..Default::default() }); - assert_eq!((enc.cfg.width, enc.cfg.height), (65_536, 65_536)); - let enc = Vp9Encoder::new(EncoderConfig { width: 65_536, height: 1, ..Default::default() }); - assert_eq!((enc.cfg.width, enc.cfg.height), (65_536, 1)); - } -} diff --git a/crates/vp9enc/src/mc.rs b/crates/vp9enc/src/mc.rs deleted file mode 100644 index 5d897f854..000000000 --- a/crates/vp9enc/src/mc.rs +++ /dev/null @@ -1,265 +0,0 @@ -//! Inter prediction exactly as the decoder forms it: motion vector clamping (spec 8.5.2.2) and -//! the unscaled block inter prediction process (8.5.2.4) with the regular 8-tap filters and -//! reference samples clamped to the reference frame's visible area. - -use crate::tables::SUBPEL_REGULAR; - -/// A motion vector in 1/8 luma samples. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] -pub struct Mv { - pub row: i32, - pub col: i32, -} - -impl Mv { - pub const ZERO: Mv = Mv { row: 0, col: 0 }; - pub fn new(row: i32, col: i32) -> Mv { - Mv { row, col } - } - pub fn minus(self, o: Mv) -> Mv { - Mv::new(self.row - o.row, self.col - o.col) - } -} - -/// A reference plane: `w`×`h` visible samples at `stride`. -pub struct RefPlane<'a> { - pub data: &'a [u8], - pub stride: usize, - pub w: usize, - pub h: usize, -} - -/// The block's motion vector for `plane` in 1/16 samples of that plane, clamped as the -/// decoder clamps it. `n8`: block size in 8×8 units. -pub fn clamp_for_plane(mv: Mv, r: usize, c: usize, n8: usize, mi_rows: usize, mi_cols: usize, chroma: bool) -> (i32, i32) { - let s = chroma as i32; - let (r, c, b) = (r as i32, c as i32, n8 as i32); - let to_top = (-(r * 8 * 16)) >> s; - let to_bottom = (((mi_rows as i32 - b - r) * 8) * 16) >> s; - let to_left = (-(c * 8 * 16)) >> s; - let to_right = (((mi_cols as i32 - b - c) * 8) * 16) >> s; - let spel = (4 + ((b * 8) >> s)) << 4; - let row = ((2 * mv.row) >> s).clamp(to_top - spel, (to_bottom + spel - 16).max(to_top - spel)); - let col = ((2 * mv.col) >> s).clamp(to_left - spel, (to_right + spel - 16).max(to_left - spel)); - (row, col) -} - -/// Predict a `w`×`h` block whose top-left sample is at (`x16`, `y16`) (1/16 samples) into `out` -/// (stride `w`). -pub fn predict(r: &RefPlane, x16: i32, y16: i32, w: usize, h: usize, out: &mut [u8]) { - let (x0, y0) = (x16 >> 4, y16 >> 4); - let (fx, fy) = ((x16 & 15) as usize, (y16 & 15) as usize); - let (lx, ly) = (r.w as i32 - 1, r.h as i32 - 1); - let inside = x0 - 3 >= 0 && y0 - 3 >= 0 && x0 + w as i32 + 4 <= lx && y0 + h as i32 + 4 <= ly; - let at = |x: i32, y: i32| -> i32 { - if inside { r.data[y as usize * r.stride + x as usize] as i32 } else { r.data[y.clamp(0, ly) as usize * r.stride + x.clamp(0, lx) as usize] as i32 } - }; - let fh = &SUBPEL_REGULAR[fx]; - let fv = &SUBPEL_REGULAR[fy]; - if inside && w <= 16 && h <= 16 { - // Rows straight from the reference (no edge clamping needed). - let (xs, ys) = ((x0 - 3) as usize, (y0 - 3) as usize); - let row = |i: usize| &r.data[(ys + i) * r.stride + xs..]; - if fx == 0 && fy == 0 { - for i in 0..h { - out[i * w..i * w + w].copy_from_slice(&row(i + 3)[3..3 + w]); - } - return; - } - let mut tmp = [0u8; 23 * 16]; - for i in 0..h + 7 { - let s = row(i); - filt8(std::array::from_fn(|t| &s[t..]), fh, &mut tmp[i * w..i * w + w]); - } - for i in 0..h { - filt8(std::array::from_fn(|t| &tmp[(i + t) * w..]), fv, &mut out[i * w..i * w + w]); - } - return; - } - if fx == 0 && fy == 0 { - for i in 0..h { - for j in 0..w { - out[i * w + j] = at(x0 + j as i32, y0 + i as i32) as u8; - } - } - return; - } - // Horizontal pass over h + 7 rows (from y0 - 3), then vertical. - let mut tmp = [0u8; 23 * 16]; - for i in 0..h + 7 { - let y = y0 - 3 + i as i32; - for j in 0..w { - let x = x0 + j as i32 - 3; - let mut acc = 64; - for t in 0..8 { - acc += fh[t] * at(x + t as i32, y); - } - tmp[i * w + j] = (acc >> 7).clamp(0, 255) as u8; - } - } - for i in 0..h { - for j in 0..w { - let mut acc = 64; - for t in 0..8 { - acc += fv[t] * tmp[(i + t) * w + j] as i32; - } - out[i * w + j] = (acc >> 7).clamp(0, 255) as u8; - } - } -} - -/// The reference luma pre-interpolated at the 16 quarter-sample phases the encoder uses (motion -/// vectors are even in 1/8 units: fractions 0, 4, 8, 12 in 1/16), over the visible area plus a -/// border. Each phase plane holds exactly what [`predict`] returns at that phase (the decoder's -/// separable filter with edge clamping), so motion search reads predictions directly. -pub struct PhasePlanes { - border: usize, - stride: usize, - rows: usize, - planes: Vec>, -} - -impl PhasePlanes { - pub fn new(r: &RefPlane, border: usize) -> PhasePlanes { - const M: usize = 4; - let (pw, ph) = (r.w + 2 * border, r.h + 2 * border); - // Edge-replicated source with M more samples on every side for the filter taps. - let rs = pw + 2 * M; - let rh = ph + 2 * M; - let off = (border + M) as i32; - let mut raw = vec![0u8; rs * rh]; - let o = off as usize; - for (y, row) in raw.chunks_exact_mut(rs).enumerate() { - let sy = (y as i32 - off).clamp(0, r.h as i32 - 1) as usize; - let src = &r.data[sy * r.stride..sy * r.stride + r.w]; - // Edge replication: src[clamp(x - off, 0, w - 1)]. - let (left, rest) = row.split_at_mut(o.min(rs)); - left.fill(src[0]); - let n = r.w.min(rest.len()); - rest[..n].copy_from_slice(&src[..n]); - rest[n..].fill(src[r.w - 1]); - } - // Horizontal phases over every raw row (inner columns). - let hpass = |fx: usize| -> Vec { - let f = &SUBPEL_REGULAR[fx]; - let mut out = vec![0u8; pw * rh]; - for (row, o) in raw.chunks_exact(rs).zip(out.chunks_exact_mut(pw)) { - filt8(std::array::from_fn(|t| &row[M - 3 + t..]), f, o); - } - out - }; - let unfiltered: Vec = (0..rh).flat_map(|y| raw[y * rs + M..y * rs + M + pw].iter().copied()).collect(); - let h: Vec> = (0..4).map(|k| if k == 0 { unfiltered.clone() } else { hpass(k * 4) }).collect(); - let mut planes = Vec::with_capacity(16); - for fy in 0..4 { - for hp in &h { - if fy == 0 { - planes.push(hp[M * pw..(M + ph) * pw].to_vec()); - continue; - } - let f = &SUBPEL_REGULAR[fy * 4]; - let mut out = vec![0u8; pw * ph]; - for (y, o) in out.chunks_exact_mut(pw).enumerate() { - filt8(std::array::from_fn(|t| &hp[(y + M + t - 3) * pw..]), f, o); - } - planes.push(out); - } - } - PhasePlanes { border, stride: pw, rows: ph, planes } - } - - /// The prediction of the `n`×`n` block at (`x16`, `y16`) (fractions multiples of 4) as rows of - /// a phase plane: (plane, offset of the first sample, stride). `None` beyond the border. - pub fn block(&self, x16: i32, y16: i32, n: usize) -> Option<(&[u8], usize, usize)> { - let (x0, y0) = ((x16 >> 4) + self.border as i32, (y16 >> 4) + self.border as i32); - let (fx, fy) = ((x16 & 15) as usize, (y16 & 15) as usize); - if fx % 4 != 0 || fy % 4 != 0 || x0 < 0 || y0 < 0 || x0 as usize + n > self.stride || y0 as usize + n > self.rows { - return None; - } - Some((&self.planes[(fy / 4) * 4 + fx / 4], y0 as usize * self.stride + x0 as usize, self.stride)) - } -} - -/// `out[x] = clip(Round2(sum_t f[t] * s[t][x], 7))` for 8-bit samples. Lanes are 16-bit sums -/// kept modulo 2^16: with the regular filter every sum plus the rounding term lies in -/// [-13706, 46474], so wrapped non-negative sums stay below 0xC000 and negative ones (clipped -/// to 0) land above it (the decoder's `narrow8`). -#[inline] -fn filt8(s: [&[u8]; 8], f: &[i32; 8], out: &mut [u8]) { - let n = out.len(); - let mut x = 0; - while x + 32 <= n { - filt_chunk::<32>(&s, f, out, x); - x += 32; - } - for w in [16, 8, 4] { - if x + w <= n { - match w { - 16 => filt_chunk::<16>(&s, f, out, x), - 8 => filt_chunk::<8>(&s, f, out, x), - _ => filt_chunk::<4>(&s, f, out, x), - } - x += w; - } - } - for j in x..n { - let acc: i32 = 64 + (0..8).map(|t| f[t] * s[t][j] as i32).sum::(); - out[j] = (acc >> 7).clamp(0, 255) as u8; - } -} - -#[inline(always)] -fn filt_chunk(s: &[&[u8]; 8], f: &[i32; 8], out: &mut [u8], x: usize) { - let mut acc = [64u16; W]; - for t in 0..8 { - let c = f[t] as u16; - let Some(p) = s[t].get(x..).and_then(|p| p.first_chunk::()) else { return }; - for j in 0..W { - acc[j] = acc[j].wrapping_add(c.wrapping_mul(p[j] as u16)); - } - } - if let Some(o) = out.get_mut(x..).and_then(|o| o.first_chunk_mut::()) { - for j in 0..W { - o[j] = if acc[j] < 0xC000 { (acc[j] >> 7).min(255) as u8 } else { 0 }; - } - } -} - -#[cfg(test)] -mod tests { - #[test] - fn phase_planes_match_predict() { - let (w, h) = (37usize, 29usize); - let data: Vec = (0..w * h).map(|i| ((i * 7919) % 251) as u8).collect(); - let r = RefPlane { data: &data, stride: w, w, h }; - let pp = PhasePlanes::new(&r, 24); - let mut out = [0u8; 64]; - for y16 in (-20 * 16..(h as i32 + 10) * 16).step_by(4 * 7) { - for x16 in (-20 * 16..(w as i32 + 10) * 16).step_by(4 * 5) { - let Some((p, o, s)) = pp.block(x16, y16, 8) else { continue }; - predict(&r, x16, y16, 8, 8, &mut out); - for i in 0..8 { - assert_eq!(&p[o + i * s..o + i * s + 8], &out[i * 8..i * 8 + 8], "({x16}, {y16}) row {i}"); - } - } - } - } - - use super::*; - - #[test] - fn whole_sample_and_linear_ramps() { - let (w, h) = (40usize, 30usize); - let data: Vec = (0..w * h).map(|i| ((i % w) * 4 + (i / w)) as u8).collect(); - let r = RefPlane { data: &data, stride: w, w, h }; - let mut out = [0u8; 64]; - predict(&r, 10 << 4, 8 << 4, 8, 8, &mut out); - assert_eq!(out[0], data[8 * w + 10]); - // Half a sample right on a linear ramp: exact midpoint. - predict(&r, (10 << 4) + 8, 8 << 4, 8, 8, &mut out); - assert_eq!(out[0], data[8 * w + 10] + 2); - // Off the left edge: clamped to column 0. - predict(&r, -(20 << 4), 0, 4, 4, &mut out); - assert_eq!(out[0], data[0]); - } -} diff --git a/crates/vp9enc/src/probs.rs b/crates/vp9enc/src/probs.rs deleted file mode 100644 index c089cb96f..000000000 --- a/crates/vp9enc/src/probs.rs +++ /dev/null @@ -1,465 +0,0 @@ -//! Probabilities: the frame's adaptable probability table (one flat array laid out in the order -//! the compressed header updates them), a symbol recorder that codes every boolean decision -//! against either a fixed probability or an entry of that table, bit-cost estimates, and the -//! per-frame forward updates (spec 6.3: `diff_update_prob` with `decode_term_subexp` / -//! `inv_remap_prob`, and `update_mv_prob`). -//! -//! Tiles are first recorded, then the encoder counts how often each table entry coded a 0 or a -//! 1, picks updates that pay for themselves, writes them in the compressed header and finally -//! entropy codes the recorded symbols with the updated probabilities. - -use crate::bool::BoolEncoder; -use crate::tables::{ - DEFAULT_COEF_PROBS, DEFAULT_INTER_MODE_PROBS, DEFAULT_IS_INTER_PROB, DEFAULT_MV_BITS_PROB, DEFAULT_MV_CLASS_PROBS, DEFAULT_MV_CLASS0_BIT_PROB, - DEFAULT_MV_CLASS0_FR_PROBS, DEFAULT_MV_FR_PROBS, DEFAULT_MV_JOINT_PROBS, DEFAULT_MV_SIGN_PROB, DEFAULT_PARTITION_PROBS, DEFAULT_SINGLE_REF_PROB, - DEFAULT_SKIP_PROB, PARETO_TABLE, inv_map_table, -}; - -/// Coefficient probabilities `[ptype][ref][band][ctx][3]` for 4×4 transforms (band 0 uses -/// contexts 0..3 only). -pub const COEF: usize = 0; -pub const SKIP: usize = 432; -pub const INTER_MODE: usize = SKIP + 3; -pub const IS_INTER: usize = INTER_MODE + 21; -pub const SINGLE_REF: usize = IS_INTER + 4; -pub const PARTITION: usize = SINGLE_REF + 10; -pub const MV_JOINT: usize = PARTITION + 48; -pub const MV_SIGN: usize = MV_JOINT + 3; -pub const MV_CLASS: usize = MV_SIGN + 2; -pub const MV_CLASS0_BIT: usize = MV_CLASS + 20; -pub const MV_BITS: usize = MV_CLASS0_BIT + 2; -pub const MV_CLASS0_FR: usize = MV_BITS + 20; -pub const MV_FR: usize = MV_CLASS0_FR + 12; -pub const NPROBS: usize = MV_FR + 6; - -/// Index of the first of the three coefficient probabilities of a context. -pub fn coef_index(ptype: usize, inter: bool, band: usize, ctx: usize) -> usize { - COEF + (((ptype * 2 + inter as usize) * 6 + band) * 6 + ctx) * 3 -} - -#[derive(Clone)] -pub struct Probs(pub [u8; NPROBS]); - -impl Probs { - /// The default probabilities (setup_past_independence / key frames). - pub fn defaults() -> Probs { - let mut p = [128u8; NPROBS]; - p[COEF..COEF + 432].copy_from_slice(&DEFAULT_COEF_PROBS); - p[SKIP..SKIP + 3].copy_from_slice(&DEFAULT_SKIP_PROB); - p[INTER_MODE..INTER_MODE + 21].copy_from_slice(&DEFAULT_INTER_MODE_PROBS); - p[IS_INTER..IS_INTER + 4].copy_from_slice(&DEFAULT_IS_INTER_PROB); - p[SINGLE_REF..SINGLE_REF + 10].copy_from_slice(&DEFAULT_SINGLE_REF_PROB); - p[PARTITION..PARTITION + 48].copy_from_slice(&DEFAULT_PARTITION_PROBS); - p[MV_JOINT..MV_JOINT + 3].copy_from_slice(&DEFAULT_MV_JOINT_PROBS); - p[MV_SIGN..MV_SIGN + 2].copy_from_slice(&DEFAULT_MV_SIGN_PROB); - p[MV_CLASS..MV_CLASS + 20].copy_from_slice(&DEFAULT_MV_CLASS_PROBS); - p[MV_CLASS0_BIT..MV_CLASS0_BIT + 2].copy_from_slice(&DEFAULT_MV_CLASS0_BIT_PROB); - p[MV_BITS..MV_BITS + 20].copy_from_slice(&DEFAULT_MV_BITS_PROB); - p[MV_CLASS0_FR..MV_CLASS0_FR + 12].copy_from_slice(&DEFAULT_MV_CLASS0_FR_PROBS); - p[MV_FR..MV_FR + 6].copy_from_slice(&DEFAULT_MV_FR_PROBS); - Probs(p) - } -} - -/// Probabilities of the full token tree (9.3.2) for model probability `p`. -pub fn pareto(p: u8) -> [u8; 8] { - let p = p.max(1) as usize; - let x = (p - 1) / 2; - let mut t = [0u8; 8]; - for n in 0..8 { - t[n] = if p & 1 == 1 { PARETO_TABLE[x * 8 + n] } else { ((PARETO_TABLE[x * 8 + n] as u32 + PARETO_TABLE[(x + 1) * 8 + n] as u32) >> 1) as u8 }; - } - t -} - -/// The probability a recorded symbol is coded with. -#[derive(Clone, Copy, Debug)] -pub enum P { - Fixed(u8), - /// An entry of the frame's [`Probs`]. - Ctx(u16), - /// Node `n` of the Pareto token tree derived from the coefficient probability at the index. - Pareto(u16, u8), -} - -#[derive(Clone, Copy)] -struct Sym { - bit: bool, - p: P, -} - -/// Records the boolean decisions of a tile. -#[derive(Default)] -pub struct Writer { - syms: Vec, -} - -impl Writer { - #[inline] - pub fn put(&mut self, bit: bool, p: P) { - self.syms.push(Sym { bit, p }); - } - #[inline] - pub fn fixed(&mut self, bit: bool, prob: u8) { - self.put(bit, P::Fixed(prob)); - } - #[inline] - pub fn ctx(&mut self, bit: bool, idx: usize) { - self.put(bit, P::Ctx(idx as u16)); - } - /// Write `value` with `tree` (leaves stored negated); node `i` uses `prob(i)`. - pub fn tree(&mut self, tree: &[i8], value: u8, prob: impl Fn(usize) -> P) { - for (node, bit) in tree_path(tree, value) { - self.put(bit, prob(node)); - } - } - - /// Add the 0/1 counts of every table entry this tile used. - pub fn count(&self, counts: &mut [[u32; 2]]) { - for s in &self.syms { - if let P::Ctx(i) = s.p { - counts[i as usize][s.bit as usize] += 1; - } - } - } - - /// Entropy code the recorded symbols. - pub fn emit(&self, probs: &Probs) -> Vec { - let mut bc = BoolEncoder::new(); - bc.write(false, 128); // marker bit - for s in &self.syms { - let p = match s.p { - P::Fixed(p) => p, - P::Ctx(i) => probs.0[i as usize], - P::Pareto(i, n) => pareto(probs.0[i as usize])[n as usize], - }; - bc.write(s.bit, p); - } - bc.finish() - } -} - -/// The (node, bit) path to `value` in a tree (node = tree index / 2). -pub fn tree_path(tree: &[i8], value: u8) -> impl Iterator { - struct Path { - steps: [(u8, bool); 16], - len: usize, - } - fn walk(tree: &[i8], node: usize, value: u8, out: &mut Path) -> bool { - for b in 0..2 { - let t = tree[node + b]; - out.steps[out.len] = ((node >> 1) as u8, b == 1); - out.len += 1; - if t <= 0 { - if (-t) as u8 == value { - return true; - } - } else if walk(tree, t as usize, value, out) { - return true; - } - out.len -= 1; - } - false - } - let mut p = Path { steps: [(0, false); 16], len: 0 }; - walk(tree, 0, value, &mut p); - let Path { steps, len } = p; - steps.into_iter().take(len).map(|(n, b)| (n as usize, b)) -} - -// ---------------------------------------------------------------- costs - -/// Cost in 1/256 bit of coding a 0 (`[p][0]`) or a 1 (`[p][1]`) with probability `p`. -pub fn cost_table() -> &'static [[u32; 2]; 256] { - use std::sync::OnceLock; - static T: OnceLock<[[u32; 2]; 256]> = OnceLock::new(); - T.get_or_init(|| { - let mut t = [[0u32; 2]; 256]; - for (p, e) in t.iter_mut().enumerate() { - let q = (p.max(1) as f64) / 256.0; - e[0] = (-q.log2() * 256.0).round() as u32; - e[1] = (-(1.0 - q).log2() * 256.0).round() as u32; - } - t - }) -} - -#[inline] -pub fn cost(bit: bool, p: u8) -> u32 { - cost_table()[p as usize][bit as usize] -} - -/// Cost of `value` in a tree with probabilities `probs` (node i → probs[i]). -pub fn tree_cost(tree: &[i8], probs: &[u8], value: u8) -> u32 { - tree_path(tree, value).map(|(n, b)| cost(b, probs[n])).sum() -} - -// ---------------------------------------------------------------- forward updates - -fn inv_recenter_nonneg(v: i32, m: i32) -> i32 { - if v > 2 * m { - v - } else if v & 1 != 0 { - m - ((v + 1) >> 1) - } else { - m + (v >> 1) - } -} - -/// `inv_remap_prob` (6.3.5). -fn inv_remap_prob(delta: usize, prob: u8) -> u8 { - let v = inv_map_table()[delta.min(254)] as i32; - let m = prob as i32 - 1; - let r = if (m << 1) <= 255 { 1 + inv_recenter_nonneg(v, m) } else { 255 - inv_recenter_nonneg(v, 255 - 1 - m) }; - r.clamp(1, 255) as u8 -} - -/// The `decode_term_subexp` value that moves `old` to `new`. -fn delta_for(old: u8, new: u8) -> Option { - match delta_table().get(old as usize * 256 + new as usize) { - Some(&d) if d != NO_DELTA => Some(d as usize), - _ => None, - } -} - -/// Marks "no delta reaches this probability" in [`delta_table`]. -const NO_DELTA: u8 = 255; - -/// `delta_for` for every (old, new) pair, indexed `old * 256 + new`: the smallest delta that -/// moves `old` to `new`. Filled from the largest delta down so the smallest one wins. -fn delta_table() -> &'static [u8] { - use std::sync::OnceLock; - static T: OnceLock> = OnceLock::new(); - T.get_or_init(|| { - let mut t = vec![NO_DELTA; 256 * 256]; - for old in 0..=255u8 { - for d in (0..255usize).rev() { - let new = inv_remap_prob(d, old); - if let Some(e) = t.get_mut(old as usize * 256 + new as usize) { - *e = d as u8; - } - } - } - t - }) -} - -/// Bits of `decode_term_subexp` for `d`. -fn subexp_bits(d: usize) -> u32 { - match d { - 0..16 => 5, - 16..32 => 6, - 32..64 => 8, - 64..129 => 10, - _ => 11, - } -} - -fn write_subexp(bc: &mut BoolEncoder, d: usize) { - let d = d as u32; - if d < 16 { - bc.literal(0, 1); - bc.literal(d, 4); - } else if d < 32 { - bc.literal(2, 2); - bc.literal(d - 16, 4); - } else if d < 64 { - bc.literal(6, 3); - bc.literal(d - 32, 5); - } else { - bc.literal(7, 3); - if d < 129 { - bc.literal(d - 64, 7); - } else { - let v = (d + 1) >> 1; - bc.literal(v, 7); - bc.literal((d + 1) & 1, 1); - } - } -} - -fn counts_cost(c: [u32; 2], p: u8) -> u64 { - c[0] as u64 * cost(false, p) as u64 + c[1] as u64 * cost(true, p) as u64 -} - -/// Whether table entry `i` is an MV probability (7-bit `update_mv_prob` rather than -/// `diff_update_prob`). -fn is_mv(i: usize) -> bool { - i >= MV_JOINT -} - -/// Choose updated probabilities: an entry changes when the bits saved on this frame's symbols -/// exceed the cost of signalling the change. `inter`: the inter-frame tables are updatable. -pub fn plan_updates(old: &Probs, counts: &[[u32; 2]], inter: bool) -> Probs { - let mut new = old.clone(); - let flag = cost(true, 252) as i64 - cost(false, 252) as i64; - let limit = if inter { NPROBS } else { INTER_MODE }; - for i in 0..limit { - let c = counts[i]; - let n = c[0] + c[1]; - if n == 0 { - continue; - } - let o = old.0[i]; - let ideal = ((c[0] as f64 * 256.0 / n as f64).round() as i32).clamp(1, 255) as u8; - let cands: Vec = if is_mv(i) { vec![ideal | 1, ideal.saturating_sub(1).max(1) | 1] } else { vec![ideal] }; - let base = counts_cost(c, o) as i64; - let mut best = (0i64, o); - for p in cands { - if p == o { - continue; - } - let signal = if is_mv(i) { - 7 * 256 - } else { - match delta_for(o, p) { - Some(d) => subexp_bits(d) as i64 * 256, - None => continue, - } - }; - let gain = base - counts_cost(c, p) as i64 - signal - flag; - if gain > best.0 { - best = (gain, p); - } - } - new.0[i] = best.1; - } - new -} - -/// `diff_update_prob` for one entry. -fn diff_update(bc: &mut BoolEncoder, old: &Probs, new: &Probs, i: usize) { - let (o, n) = (old.0[i], new.0[i]); - match (o != n).then(|| delta_for(o, n)).flatten() { - Some(d) => { - bc.write(true, 252); - write_subexp(bc, d); - } - None => bc.write(false, 252), - } -} - -/// `update_mv_prob` for one entry. -fn mv_update(bc: &mut BoolEncoder, old: &Probs, new: &Probs, i: usize) { - let n = new.0[i]; - if n != old.0[i] && n & 1 == 1 { - bc.write(true, 252); - bc.literal(n as u32 >> 1, 7); - } else { - bc.write(false, 252); - } -} - -/// The compressed header (6.3) of a frame coded with ONLY_4X4 transforms, single LAST -/// references and a fixed interpolation filter, moving the probabilities from `old` to `new`. -/// Entries `new` cannot signal are left at `old` (and must be coded with `old`): the caller -/// codes the tiles with the probabilities this returns. -pub fn compressed_header(old: &Probs, new: &Probs, lossless: bool, inter: bool) -> (Vec, Probs) { - let mut bc = BoolEncoder::new(); - bc.write(false, 128); - if !lossless { - bc.literal(0, 2); // tx_mode = ONLY_4X4 - } - // read_coef_probs: TX_4X4 only. - let coef_changed = (COEF..COEF + 432).any(|i| new.0[i] != old.0[i]); - bc.literal(coef_changed as u32, 1); - if coef_changed { - for i in 0..2 { - for j in 0..2 { - for k in 0..6 { - for l in 0..if k == 0 { 3 } else { 6 } { - for m in 0..3 { - diff_update(&mut bc, old, new, coef_index(i, j == 1, k, l) + m); - } - } - } - } - } - } - for i in 0..3 { - diff_update(&mut bc, old, new, SKIP + i); - } - if inter { - for i in 0..21 { - diff_update(&mut bc, old, new, INTER_MODE + i); - } - for i in 0..4 { - diff_update(&mut bc, old, new, IS_INTER + i); - } - // reference_mode is SINGLE_REFERENCE (no compound: equal sign biases). - for i in 0..10 { - diff_update(&mut bc, old, new, SINGLE_REF + i); - } - for _ in 0..36 { - bc.write(false, 252); // y mode probabilities (no intra blocks in inter frames) - } - for i in 0..48 { - diff_update(&mut bc, old, new, PARTITION + i); - } - for j in 0..3 { - mv_update(&mut bc, old, new, MV_JOINT + j); - } - for i in 0..2 { - mv_update(&mut bc, old, new, MV_SIGN + i); - for j in 0..10 { - mv_update(&mut bc, old, new, MV_CLASS + i * 10 + j); - } - mv_update(&mut bc, old, new, MV_CLASS0_BIT + i); - for j in 0..10 { - mv_update(&mut bc, old, new, MV_BITS + i * 10 + j); - } - } - for i in 0..2 { - for j in 0..6 { - mv_update(&mut bc, old, new, MV_CLASS0_FR + i * 6 + j); - } - for j in 0..3 { - mv_update(&mut bc, old, new, MV_FR + i * 3 + j); - } - } - } - // What the decoder ends up with. - let mut used = old.clone(); - for i in 0..NPROBS { - let ok = if is_mv(i) { new.0[i] & 1 == 1 } else { delta_for(old.0[i], new.0[i]).is_some() }; - if ok && (inter || i < INTER_MODE) { - used.0[i] = new.0[i]; - } - } - (bc.finish(), used) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn remap_reaches_every_probability() { - for old in 1..=255u8 { - for new in 1..=255u8 { - if new != old { - assert!(delta_for(old, new).is_some(), "{old} -> {new}"); - } - } - } - } - - #[test] - fn delta_table_matches_the_linear_search() { - for old in 0..=255u8 { - for new in 0..=255u8 { - let slow = (0..255).find(|&d| inv_remap_prob(d, old) == new); - assert_eq!(delta_for(old, new), slow, "{old} -> {new}"); - } - } - } - - #[test] - fn inv_map_table_shape() { - let t = inv_map_table(); - assert_eq!(&t[..4], &[7, 20, 33, 46]); - assert_eq!(t[19], 254); - assert_eq!(&t[20..27], &[1, 2, 3, 4, 5, 6, 8]); - assert_eq!(&t[252..], &[252, 253, 253]); - } -} diff --git a/crates/vp9enc/src/tables.rs b/crates/vp9enc/src/tables.rs deleted file mode 100644 index c70d7d8c2..000000000 --- a/crates/vp9enc/src/tables.rs +++ /dev/null @@ -1,201 +0,0 @@ -//! Tables from the VP9 Bitstream & Decoding Process Specification v0.6 (section 10), as -//! transcribed in FilmCraft's first-party decoder `filmcraft-vp9` (MIT OR Apache-2.0, the same -//! licence as this crate). - -pub const DC_QLOOKUP: [i32; 256] = [ - 4, 8, 8, 9, 10, 11, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 23, 24, 25, 26, 26, 27, 28, 29, 30, 31, 32, 32, 33, 34, 35, 36, 37, 38, 38, 39, 40, - 41, 42, 43, 43, 44, 45, 46, 47, 48, 48, 49, 50, 51, 52, 53, 53, 54, 55, 56, 57, 57, 58, 59, 60, 61, 62, 62, 63, 64, 65, 66, 66, 67, 68, 69, 70, 70, 71, 72, - 73, 74, 74, 75, 76, 77, 78, 78, 79, 80, 81, 81, 82, 83, 84, 85, 85, 87, 88, 90, 92, 93, 95, 96, 98, 99, 101, 102, 104, 105, 107, 108, 110, 111, 113, 114, - 116, 117, 118, 120, 121, 123, 125, 127, 129, 131, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 161, 164, 166, 169, 172, 174, 177, 180, - 182, 185, 187, 190, 192, 195, 199, 202, 205, 208, 211, 214, 217, 220, 223, 226, 230, 233, 237, 240, 243, 247, 250, 253, 257, 261, 265, 269, 272, 276, 280, - 284, 288, 292, 296, 300, 304, 309, 313, 317, 322, 326, 330, 335, 340, 344, 349, 354, 359, 364, 369, 374, 379, 384, 389, 395, 400, 406, 411, 417, 423, 429, - 435, 441, 447, 454, 461, 467, 475, 482, 489, 497, 505, 513, 522, 530, 539, 549, 559, 569, 579, 590, 602, 614, 626, 640, 654, 668, 684, 700, 717, 736, 755, - 775, 796, 819, 843, 869, 896, 925, 955, 988, 1022, 1058, 1098, 1139, 1184, 1232, 1282, 1336, -]; - -pub const AC_QLOOKUP: [i32; 256] = [ - 4, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, - 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, - 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, - 136, 138, 140, 142, 144, 146, 148, 150, 152, 155, 158, 161, 164, 167, 170, 173, 176, 179, 182, 185, 188, 191, 194, 197, 200, 203, 207, 211, 215, 219, 223, - 227, 231, 235, 239, 243, 247, 251, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300, 305, 311, 317, 323, 329, 335, 341, 347, 353, 359, 366, 373, 380, 387, - 394, 401, 408, 416, 424, 432, 440, 448, 456, 465, 474, 483, 492, 501, 510, 520, 530, 540, 550, 560, 571, 582, 593, 604, 615, 627, 639, 651, 663, 676, 689, - 702, 715, 729, 743, 757, 771, 786, 801, 816, 832, 848, 864, 881, 898, 915, 933, 951, 969, 988, 1007, 1026, 1046, 1066, 1087, 1108, 1129, 1151, 1173, 1196, - 1219, 1243, 1267, 1292, 1317, 1343, 1369, 1396, 1423, 1451, 1479, 1508, 1537, 1567, 1597, 1628, 1660, 1692, 1725, 1759, 1793, 1828, -]; - -pub const DEFAULT_SCAN_4X4: [u16; 16] = [0, 4, 1, 5, 8, 2, 12, 9, 3, 6, 13, 10, 7, 14, 11, 15]; - -pub const COL_SCAN_4X4: [u16; 16] = [0, 4, 8, 1, 12, 5, 9, 2, 13, 6, 10, 3, 7, 14, 11, 15]; - -pub const ROW_SCAN_4X4: [u16; 16] = [0, 1, 4, 2, 5, 3, 6, 8, 9, 7, 12, 10, 13, 11, 14, 15]; - -pub const COEFBAND_4X4: [u8; 16] = [0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5]; - -pub const ENERGY_CLASS: [u8; 12] = [0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 5, 5]; - -pub const PARETO_TABLE: [u8; 1024] = [ - 3, 86, 128, 6, 86, 23, 88, 29, 9, 86, 129, 17, 88, 61, 94, 76, 15, 87, 129, 28, 89, 93, 100, 110, 20, 88, 130, 38, 91, 118, 106, 136, 26, 89, 131, 48, 92, - 139, 111, 156, 31, 90, 131, 58, 94, 156, 117, 171, 37, 90, 132, 66, 95, 171, 122, 184, 42, 91, 132, 75, 97, 183, 127, 194, 47, 92, 133, 83, 98, 193, 132, - 202, 52, 93, 133, 90, 100, 201, 137, 208, 57, 94, 134, 98, 101, 208, 142, 214, 62, 94, 135, 105, 103, 214, 146, 218, 66, 95, 135, 111, 104, 219, 151, 222, - 71, 96, 136, 117, 106, 224, 155, 225, 76, 97, 136, 123, 107, 227, 159, 228, 80, 98, 137, 129, 109, 231, 162, 231, 84, 98, 138, 134, 110, 234, 166, 233, 89, - 99, 138, 140, 112, 236, 170, 235, 93, 100, 139, 145, 113, 238, 173, 236, 97, 101, 140, 149, 115, 240, 176, 238, 101, 102, 140, 154, 116, 242, 179, 239, - 105, 103, 141, 158, 118, 243, 182, 240, 109, 104, 141, 162, 119, 244, 185, 241, 113, 104, 142, 166, 120, 245, 187, 242, 116, 105, 143, 170, 122, 246, 190, - 243, 120, 106, 143, 173, 123, 247, 192, 244, 123, 107, 144, 177, 125, 248, 195, 244, 127, 108, 145, 180, 126, 249, 197, 245, 130, 109, 145, 183, 128, 249, - 199, 245, 134, 110, 146, 186, 129, 250, 201, 246, 137, 111, 147, 189, 131, 251, 203, 246, 140, 112, 147, 192, 132, 251, 205, 247, 143, 113, 148, 194, 133, - 251, 207, 247, 146, 114, 149, 197, 135, 252, 208, 248, 149, 115, 149, 199, 136, 252, 210, 248, 152, 115, 150, 201, 138, 252, 211, 248, 155, 116, 151, 204, - 139, 253, 213, 249, 158, 117, 151, 206, 140, 253, 214, 249, 161, 118, 152, 208, 142, 253, 216, 249, 163, 119, 153, 210, 143, 253, 217, 249, 166, 120, 153, - 212, 144, 254, 218, 250, 168, 121, 154, 213, 146, 254, 220, 250, 171, 122, 155, 215, 147, 254, 221, 250, 173, 123, 155, 217, 148, 254, 222, 250, 176, 124, - 156, 218, 150, 254, 223, 250, 178, 125, 157, 220, 151, 254, 224, 251, 180, 126, 157, 221, 152, 254, 225, 251, 183, 127, 158, 222, 153, 254, 226, 251, 185, - 128, 159, 224, 155, 255, 227, 251, 187, 129, 160, 225, 156, 255, 228, 251, 189, 131, 160, 226, 157, 255, 228, 251, 191, 132, 161, 227, 159, 255, 229, 251, - 193, 133, 162, 228, 160, 255, 230, 252, 195, 134, 163, 230, 161, 255, 231, 252, 197, 135, 163, 231, 162, 255, 231, 252, 199, 136, 164, 232, 163, 255, 232, - 252, 201, 137, 165, 233, 165, 255, 233, 252, 202, 138, 166, 233, 166, 255, 233, 252, 204, 139, 166, 234, 167, 255, 234, 252, 206, 140, 167, 235, 168, 255, - 235, 252, 207, 141, 168, 236, 169, 255, 235, 252, 209, 142, 169, 237, 171, 255, 236, 252, 210, 144, 169, 237, 172, 255, 236, 252, 212, 145, 170, 238, 173, - 255, 237, 252, 214, 146, 171, 239, 174, 255, 237, 253, 215, 147, 172, 240, 175, 255, 238, 253, 216, 148, 173, 240, 176, 255, 238, 253, 218, 149, 173, 241, - 177, 255, 239, 253, 219, 150, 174, 241, 179, 255, 239, 253, 220, 152, 175, 242, 180, 255, 240, 253, 222, 153, 176, 242, 181, 255, 240, 253, 223, 154, 177, - 243, 182, 255, 240, 253, 224, 155, 178, 244, 183, 255, 241, 253, 225, 156, 178, 244, 184, 255, 241, 253, 226, 158, 179, 244, 185, 255, 242, 253, 228, 159, - 180, 245, 186, 255, 242, 253, 229, 160, 181, 245, 187, 255, 242, 253, 230, 161, 182, 246, 188, 255, 243, 253, 231, 163, 183, 246, 189, 255, 243, 253, 232, - 164, 184, 247, 190, 255, 243, 253, 233, 165, 185, 247, 191, 255, 244, 253, 234, 166, 185, 247, 192, 255, 244, 253, 235, 168, 186, 248, 193, 255, 244, 253, - 236, 169, 187, 248, 194, 255, 244, 253, 236, 170, 188, 248, 195, 255, 245, 253, 237, 171, 189, 249, 196, 255, 245, 254, 238, 173, 190, 249, 197, 255, 245, - 254, 239, 174, 191, 249, 198, 255, 245, 254, 240, 175, 192, 249, 199, 255, 246, 254, 240, 177, 193, 250, 200, 255, 246, 254, 241, 178, 194, 250, 201, 255, - 246, 254, 242, 179, 195, 250, 202, 255, 246, 254, 242, 181, 196, 250, 203, 255, 247, 254, 243, 182, 197, 251, 204, 255, 247, 254, 244, 184, 198, 251, 205, - 255, 247, 254, 244, 185, 199, 251, 206, 255, 247, 254, 245, 186, 200, 251, 207, 255, 247, 254, 246, 188, 201, 252, 207, 255, 248, 254, 246, 189, 202, 252, - 208, 255, 248, 254, 247, 191, 203, 252, 209, 255, 248, 254, 247, 192, 204, 252, 210, 255, 248, 254, 248, 194, 205, 252, 211, 255, 248, 254, 248, 195, 206, - 252, 212, 255, 249, 254, 249, 197, 207, 253, 213, 255, 249, 254, 249, 198, 208, 253, 214, 255, 249, 254, 250, 200, 210, 253, 215, 255, 249, 254, 250, 201, - 211, 253, 215, 255, 249, 254, 250, 203, 212, 253, 216, 255, 249, 254, 251, 204, 213, 253, 217, 255, 250, 254, 251, 206, 214, 254, 218, 255, 250, 254, 252, - 207, 216, 254, 219, 255, 250, 254, 252, 209, 217, 254, 220, 255, 250, 254, 252, 211, 218, 254, 221, 255, 250, 254, 253, 213, 219, 254, 222, 255, 250, 254, - 253, 214, 221, 254, 223, 255, 250, 254, 253, 216, 222, 254, 224, 255, 251, 254, 253, 218, 224, 254, 225, 255, 251, 254, 254, 220, 225, 254, 225, 255, 251, - 254, 254, 222, 227, 255, 226, 255, 251, 254, 254, 224, 228, 255, 227, 255, 251, 254, 254, 226, 230, 255, 228, 255, 251, 254, 255, 228, 231, 255, 230, 255, - 251, 254, 255, 230, 233, 255, 231, 255, 252, 254, 255, 232, 235, 255, 232, 255, 252, 254, 255, 235, 237, 255, 233, 255, 252, 254, 255, 238, 240, 255, 235, - 255, 252, 255, 255, 241, 243, 255, 236, 255, 252, 254, 255, 246, 247, 255, 239, 255, 253, 255, -]; - -pub const KF_PARTITION_PROBS: [u8; 48] = [ - 158, 97, 94, 93, 24, 99, 85, 119, 44, 62, 59, 67, 149, 53, 53, 94, 20, 48, 83, 53, 24, 52, 18, 18, 150, 40, 39, 78, 12, 26, 67, 33, 11, 24, 7, 5, 174, 35, - 49, 68, 11, 27, 57, 15, 9, 12, 3, 3, -]; - -pub const KF_Y_MODE_PROBS: [u8; 900] = [ - 137, 30, 42, 148, 151, 207, 70, 52, 91, 92, 45, 102, 136, 116, 180, 74, 90, 100, 73, 32, 19, 187, 222, 215, 46, 34, 100, 91, 30, 32, 116, 121, 186, 93, 86, - 94, 72, 35, 36, 149, 68, 206, 68, 63, 105, 73, 31, 28, 138, 57, 124, 55, 122, 151, 67, 23, 21, 140, 126, 197, 40, 37, 171, 86, 27, 28, 128, 154, 212, 45, - 43, 53, 74, 32, 27, 107, 86, 160, 63, 134, 102, 59, 67, 44, 140, 161, 202, 78, 67, 119, 63, 36, 126, 146, 123, 158, 60, 90, 96, 43, 46, 168, 134, 107, 128, - 69, 142, 92, 44, 29, 68, 159, 201, 177, 50, 57, 77, 58, 38, 76, 114, 97, 172, 78, 133, 92, 46, 41, 76, 140, 63, 184, 69, 112, 57, 38, 32, 85, 140, 46, 112, - 54, 151, 133, 39, 27, 61, 131, 110, 175, 44, 75, 136, 52, 30, 74, 113, 130, 175, 51, 64, 58, 47, 35, 80, 100, 74, 143, 64, 163, 74, 36, 61, 116, 114, 128, - 162, 80, 125, 82, 82, 26, 26, 171, 208, 204, 44, 32, 105, 55, 44, 68, 166, 179, 192, 57, 57, 108, 42, 26, 11, 199, 241, 228, 23, 15, 85, 68, 42, 19, 131, - 160, 199, 55, 52, 83, 58, 50, 25, 139, 115, 232, 39, 52, 118, 50, 35, 33, 153, 104, 162, 64, 59, 131, 44, 24, 16, 150, 177, 202, 33, 19, 156, 55, 27, 12, - 153, 203, 218, 26, 27, 49, 53, 49, 21, 110, 116, 168, 59, 80, 76, 38, 72, 19, 168, 203, 212, 50, 50, 107, 103, 26, 36, 129, 132, 201, 83, 80, 93, 59, 38, - 83, 112, 103, 162, 98, 136, 90, 62, 30, 23, 158, 200, 207, 59, 57, 50, 67, 30, 29, 84, 86, 191, 102, 91, 59, 60, 32, 33, 112, 71, 220, 64, 89, 104, 53, 26, - 34, 130, 56, 149, 84, 120, 103, 53, 21, 23, 133, 109, 210, 56, 77, 172, 77, 19, 29, 112, 142, 228, 55, 66, 36, 61, 29, 29, 93, 97, 165, 83, 175, 162, 47, - 47, 43, 114, 137, 181, 100, 99, 95, 69, 23, 29, 128, 83, 199, 46, 44, 101, 53, 40, 55, 139, 69, 183, 61, 80, 110, 40, 29, 19, 161, 180, 207, 43, 24, 91, - 60, 34, 19, 105, 61, 198, 53, 64, 89, 52, 31, 22, 158, 40, 209, 58, 62, 89, 44, 31, 29, 147, 46, 158, 56, 102, 198, 35, 19, 12, 135, 87, 209, 41, 45, 167, - 55, 25, 21, 118, 95, 215, 38, 39, 66, 51, 38, 25, 113, 58, 164, 70, 93, 97, 47, 54, 34, 146, 108, 203, 72, 103, 151, 64, 19, 37, 156, 66, 138, 49, 95, 133, - 46, 27, 80, 150, 55, 124, 55, 121, 135, 36, 23, 27, 165, 149, 166, 54, 64, 118, 53, 21, 36, 131, 63, 163, 60, 109, 81, 40, 26, 35, 154, 40, 185, 51, 97, - 123, 35, 19, 34, 179, 19, 97, 48, 129, 124, 36, 20, 26, 136, 62, 164, 33, 77, 154, 45, 18, 32, 130, 90, 157, 40, 79, 91, 45, 26, 28, 129, 45, 129, 49, 147, - 123, 38, 44, 51, 136, 74, 162, 57, 97, 121, 75, 17, 22, 136, 138, 185, 32, 34, 166, 56, 39, 58, 133, 117, 173, 48, 53, 187, 35, 21, 12, 161, 212, 207, 20, - 23, 145, 56, 29, 19, 117, 109, 181, 55, 68, 112, 47, 29, 17, 153, 64, 220, 59, 51, 114, 46, 16, 24, 136, 76, 147, 41, 64, 172, 34, 17, 11, 108, 152, 187, - 13, 15, 209, 51, 24, 14, 115, 133, 209, 32, 26, 104, 55, 30, 18, 122, 79, 179, 44, 88, 116, 37, 49, 25, 129, 168, 164, 41, 54, 148, 82, 22, 32, 127, 143, - 213, 39, 41, 70, 62, 44, 61, 123, 105, 189, 48, 57, 64, 47, 25, 17, 175, 222, 220, 24, 30, 86, 68, 36, 17, 106, 102, 206, 59, 74, 74, 57, 39, 23, 151, 68, - 216, 55, 63, 58, 49, 30, 35, 141, 70, 168, 82, 40, 115, 51, 25, 15, 136, 129, 202, 38, 35, 139, 68, 26, 16, 111, 141, 215, 29, 28, 28, 59, 39, 19, 114, 75, - 180, 77, 104, 42, 40, 61, 26, 126, 152, 206, 61, 59, 93, 78, 23, 39, 111, 117, 170, 74, 124, 94, 48, 34, 86, 101, 92, 146, 78, 179, 134, 47, 22, 24, 138, - 187, 178, 68, 69, 59, 56, 25, 33, 105, 112, 187, 95, 177, 129, 48, 31, 27, 114, 63, 183, 82, 116, 56, 43, 28, 37, 121, 63, 123, 61, 192, 169, 42, 17, 24, - 109, 97, 177, 56, 76, 122, 58, 18, 28, 105, 139, 182, 70, 92, 63, 46, 23, 32, 74, 86, 150, 67, 183, 88, 36, 38, 48, 92, 122, 165, 88, 137, 91, 65, 70, 60, - 155, 159, 199, 61, 60, 81, 44, 78, 115, 132, 119, 173, 71, 112, 93, 39, 38, 21, 184, 227, 206, 42, 32, 64, 58, 47, 36, 124, 137, 193, 80, 82, 78, 49, 50, - 35, 144, 95, 205, 63, 78, 59, 41, 53, 52, 148, 71, 142, 65, 128, 51, 40, 36, 28, 143, 143, 202, 40, 55, 137, 52, 34, 29, 129, 183, 227, 42, 35, 43, 42, 44, - 44, 104, 105, 164, 64, 130, 80, 43, 81, 53, 140, 169, 204, 68, 84, 72, -]; - -pub const KF_UV_MODE_PROBS: [u8; 90] = [ - 144, 11, 54, 157, 195, 130, 46, 58, 108, 118, 15, 123, 148, 131, 101, 44, 93, 131, 113, 12, 23, 188, 226, 142, 26, 32, 125, 120, 11, 50, 123, 163, 135, 64, - 77, 103, 113, 9, 36, 155, 111, 157, 32, 44, 161, 116, 9, 55, 176, 76, 96, 37, 61, 149, 115, 9, 28, 141, 161, 167, 21, 25, 193, 120, 12, 32, 145, 195, 142, - 32, 38, 86, 116, 12, 64, 120, 140, 125, 49, 115, 121, 102, 19, 66, 162, 182, 122, 35, 59, 128, -]; - -pub const DEFAULT_SKIP_PROB: [u8; 3] = [192, 128, 64]; - -pub const DEFAULT_COEF_PROBS: [u8; 432] = [ - 195, 29, 183, 84, 49, 136, 8, 42, 71, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 107, 169, 35, 99, 159, 17, 82, 140, 8, 66, 114, 2, 44, 76, 1, 19, 32, 40, 132, 201, - 29, 114, 187, 13, 91, 157, 7, 75, 127, 3, 58, 95, 1, 28, 47, 69, 142, 221, 42, 122, 201, 15, 91, 159, 6, 67, 121, 1, 42, 77, 1, 17, 31, 102, 148, 228, 67, - 117, 204, 17, 82, 154, 6, 59, 114, 2, 39, 75, 1, 15, 29, 156, 57, 233, 119, 57, 212, 58, 48, 163, 29, 40, 124, 12, 30, 81, 3, 12, 31, 191, 107, 226, 124, - 117, 204, 25, 99, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 148, 210, 37, 126, 194, 8, 93, 157, 2, 68, 118, 1, 39, 69, 1, 17, 33, 41, 151, 213, 27, 123, 193, 3, - 82, 144, 1, 58, 105, 1, 32, 60, 1, 13, 26, 59, 159, 220, 23, 126, 198, 4, 88, 151, 1, 66, 114, 1, 38, 71, 1, 18, 34, 114, 136, 232, 51, 114, 207, 11, 83, - 155, 3, 56, 105, 1, 33, 65, 1, 17, 34, 149, 65, 234, 121, 57, 215, 61, 49, 166, 28, 36, 114, 12, 25, 76, 3, 16, 42, 214, 49, 220, 132, 63, 188, 42, 65, - 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 85, 137, 221, 104, 131, 216, 49, 111, 192, 21, 87, 155, 2, 49, 87, 1, 16, 28, 89, 163, 230, 90, 137, 220, 29, 100, 183, 10, - 70, 135, 2, 42, 81, 1, 17, 33, 108, 167, 237, 55, 133, 222, 15, 97, 179, 4, 72, 135, 1, 45, 85, 1, 19, 38, 124, 146, 240, 66, 124, 224, 17, 88, 175, 4, 58, - 122, 1, 36, 75, 1, 18, 37, 141, 79, 241, 126, 70, 227, 66, 58, 182, 30, 44, 136, 12, 34, 96, 2, 20, 47, 229, 99, 249, 143, 111, 235, 46, 109, 192, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 82, 158, 236, 94, 146, 224, 25, 117, 191, 9, 87, 149, 3, 56, 99, 1, 33, 57, 83, 167, 237, 68, 145, 222, 10, 103, 177, 2, 72, 131, 1, 41, - 79, 1, 20, 39, 99, 167, 239, 47, 141, 224, 10, 104, 178, 2, 73, 133, 1, 44, 85, 1, 22, 47, 127, 145, 243, 71, 129, 228, 17, 93, 177, 3, 61, 124, 1, 41, 84, - 1, 21, 52, 157, 78, 244, 140, 72, 231, 69, 58, 184, 31, 44, 137, 14, 38, 105, 8, 23, 61, -]; - -// ---------------------------------------------------------------- inter frames (spec section 10.5) - -/// `inv_map_table` (used by `inv_remap_prob`): the 20 values 7 + 13k first, then every other value -/// 1..=253 in increasing order, then 253 again. -pub fn inv_map_table() -> &'static [u8; 255] { - use std::sync::OnceLock; - static T: OnceLock<[u8; 255]> = OnceLock::new(); - T.get_or_init(|| { - let mut t = [253u8; 255]; - let mut n = 0; - for k in 0..20 { - t[n] = 7 + 13 * k; - n += 1; - } - for v in 1..=253u8 { - if v < 7 || (v - 7) % 13 != 0 { - t[n] = v; - n += 1; - } - } - t - }) -} - -/// The regular 8-tap sub-sample interpolation filters (`subpel_filters[EIGHTTAP]`), 16 positions. -pub const SUBPEL_REGULAR: [[i32; 8]; 16] = [ - [0, 0, 0, 128, 0, 0, 0, 0], - [0, 1, -5, 126, 8, -3, 1, 0], - [-1, 3, -10, 122, 18, -6, 2, 0], - [-1, 4, -13, 118, 27, -9, 3, -1], - [-1, 4, -16, 112, 37, -11, 4, -1], - [-1, 5, -18, 105, 48, -14, 4, -1], - [-1, 5, -19, 97, 58, -16, 5, -1], - [-1, 6, -19, 88, 68, -18, 5, -1], - [-1, 6, -19, 78, 78, -19, 6, -1], - [-1, 5, -18, 68, 88, -19, 6, -1], - [-1, 5, -16, 58, 97, -19, 5, -1], - [-1, 4, -14, 48, 105, -18, 5, -1], - [-1, 4, -11, 37, 112, -16, 4, -1], - [-1, 3, -9, 27, 118, -13, 4, -1], - [0, 2, -6, 18, 122, -10, 3, -1], - [0, 1, -3, 8, 126, -5, 1, 0], -]; - -/// `mv_ref_blocks` (row, column offsets) for BLOCK_8X8 and BLOCK_16X16. -pub const MV_REF_8X8: [(i32, i32); 8] = [(-1, 0), (0, -1), (-1, -1), (-2, 0), (0, -2), (-2, -1), (-1, -2), (-2, -2)]; -pub const MV_REF_16X16: [(i32, i32); 8] = [(-1, 0), (0, -1), (-1, 1), (1, -1), (-1, -1), (-3, 0), (0, -3), (-3, -3)]; - -/// `mode_2_counter` indexed by y mode (intra modes 0..=9, then NEARESTMV, NEARMV, ZEROMV, NEWMV). -pub const MODE_2_COUNTER: [u8; 14] = [9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 3, 1]; - -/// `counter_to_context`; INVALID_CASE entries are 9. -pub const COUNTER_TO_CONTEXT: [u8; 19] = [2, 3, 4, 1, 3, 9, 0, 9, 9, 5, 5, 9, 5, 9, 9, 9, 9, 9, 6]; - -pub const DEFAULT_PARTITION_PROBS: [u8; 48] = [ - 199, 122, 141, 147, 63, 159, 148, 133, 118, 121, 104, 114, 174, 73, 87, 92, 41, 83, 82, 99, 50, 53, 39, 39, 177, 58, 59, 68, 26, 63, 52, 79, 25, 17, 14, - 12, 222, 34, 30, 72, 16, 44, 58, 32, 12, 10, 7, 6, -]; - -pub const DEFAULT_IS_INTER_PROB: [u8; 4] = [9, 102, 187, 225]; - -pub const DEFAULT_SINGLE_REF_PROB: [u8; 10] = [33, 16, 77, 74, 142, 142, 172, 170, 238, 247]; - -pub const DEFAULT_INTER_MODE_PROBS: [u8; 21] = [2, 173, 34, 7, 145, 85, 7, 166, 63, 7, 94, 66, 8, 64, 46, 17, 81, 31, 25, 29, 30]; - -pub const DEFAULT_MV_JOINT_PROBS: [u8; 3] = [32, 64, 96]; -pub const DEFAULT_MV_SIGN_PROB: [u8; 2] = [128, 128]; -pub const DEFAULT_MV_CLASS_PROBS: [u8; 20] = [224, 144, 192, 168, 192, 176, 192, 198, 198, 245, 216, 128, 176, 160, 176, 176, 192, 198, 198, 208]; -pub const DEFAULT_MV_CLASS0_BIT_PROB: [u8; 2] = [216, 208]; -pub const DEFAULT_MV_BITS_PROB: [u8; 20] = [136, 140, 148, 160, 176, 192, 224, 234, 234, 240, 136, 140, 148, 160, 176, 192, 224, 234, 234, 240]; -pub const DEFAULT_MV_CLASS0_FR_PROBS: [u8; 12] = [128, 128, 64, 96, 112, 64, 128, 128, 64, 96, 112, 64]; -pub const DEFAULT_MV_FR_PROBS: [u8; 6] = [64, 96, 64, 64, 96, 64]; diff --git a/crates/vp9enc/src/tile.rs b/crates/vp9enc/src/tile.rs deleted file mode 100644 index 082d0bd83..000000000 --- a/crates/vp9enc/src/tile.rs +++ /dev/null @@ -1,940 +0,0 @@ -//! Tile coding: partitions, intra blocks (key frames) and inter blocks (motion search, mode -//! decision, motion vector prediction, residual), recorded as symbols (see [`crate::probs`]). -//! -//! Mode info, motion vector candidate lists and contexts follow the decoding process of the -//! VP9 specification (6.4, 6.4.x `find_mv_refs` / `find_best_ref_mvs`) for the subset of the -//! syntax this encoder emits: 8×8 and 16×16 blocks, single LAST references, no -//! `UsePrevFrameMvs` (inter frames are error resilient), no high-precision vectors. - -use crate::mc::{Mv, PhasePlanes, RefPlane, clamp_for_plane, predict}; -use crate::probs::{self, P, Writer, cost, tree_cost}; -use crate::tables::{ - COEFBAND_4X4, COL_SCAN_4X4, COUNTER_TO_CONTEXT, DEFAULT_SCAN_4X4, ENERGY_CLASS, KF_PARTITION_PROBS, KF_UV_MODE_PROBS, KF_Y_MODE_PROBS, MODE_2_COUNTER, - MV_REF_8X8, MV_REF_16X16, ROW_SCAN_4X4, -}; -use crate::transform::{ADST_ADST, ADST_DCT, DCT_ADST, DCT_DCT, Quant}; -use crate::{BLOCK_8X8, BLOCK_16X16, Mi, Plane}; - -pub const DC_PRED: u8 = 0; -pub const V_PRED: u8 = 1; -pub const H_PRED: u8 = 2; -pub const TM_PRED: u8 = 9; -const MODES: [u8; 4] = [DC_PRED, V_PRED, H_PRED, TM_PRED]; -pub const NEARESTMV: u8 = 10; -pub const NEARMV: u8 = 11; -pub const ZEROMV: u8 = 12; -pub const NEWMV: u8 = 13; - -/// Which reconstructed neighbours are available for intra prediction. -#[derive(Clone, Copy)] -struct IntraEdges { - left: bool, - above: bool, -} - -const PARTITION_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3]; -const INTRA_MODE_TREE: [i8; 18] = [0, 2, -9, 4, -1, 6, 8, 12, -2, 10, -4, -5, -3, 14, -8, 16, -6, -7]; -/// Leaves are `mode - NEARESTMV`. -const INTER_MODE_TREE: [i8; 6] = [-2, 2, 0, 4, -1, -3]; -const MV_JOINT_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3]; -const MV_CLASS_TREE: [i8; 20] = [0, 2, -1, 4, 6, 8, -2, -3, 10, 12, -4, -5, -6, 14, 16, 18, -7, -8, -9, -10]; -const MV_FR_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3]; - -/// extra_bits[token] = (cat, numExtra, base). -const EXTRA_BITS: [(u8, u8, u16); 11] = - [(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (1, 1, 5), (2, 2, 7), (3, 3, 11), (4, 4, 19), (5, 5, 35), (6, 14, 67)]; -const CAT_PROBS: [&[u8]; 7] = [ - &[], - &[159], - &[165, 145], - &[173, 148, 140], - &[176, 155, 140, 135], - &[180, 157, 141, 134, 130], - &[254, 254, 254, 252, 249, 243, 230, 196, 177, 153, 140, 133, 130, 129], -]; - -/// mode2txfm_map for the intra modes used here (inter blocks use DCT_DCT). -fn mode_tx_type(mode: u8) -> u8 { - match mode { - V_PRED => ADST_DCT, - H_PRED => DCT_ADST, - TM_PRED => ADST_ADST, - _ => DCT_DCT, - } -} - -fn scan(tx_type: u8) -> &'static [u16; 16] { - match tx_type { - ADST_DCT => &ROW_SCAN_4X4, - DCT_ADST => &COL_SCAN_4X4, - _ => &DEFAULT_SCAN_4X4, - } -} - -/// Coefficient context neighbours (9.3.2) of each scan position. -fn neighbors(tx_type: u8) -> [(usize, usize); 16] { - let sc = scan(tx_type); - let mut nb = [(0, 0); 16]; - for (c, &pos) in sc.iter().enumerate().skip(1) { - let pos = pos as usize; - let (i, j) = (pos / 4, pos % 4); - nb[c] = if i > 0 && j > 0 { - let a = (i - 1) * 4 + j; - let b = i * 4 + j - 1; - match tx_type { - DCT_ADST => (a, a), - ADST_DCT => (b, b), - _ => (a, b), - } - } else if i > 0 { - ((i - 1) * 4 + j, (i - 1) * 4 + j) - } else { - (j - 1, j - 1) - }; - } - nb -} - -fn eob_of(levels: &[i32; 16], tx_type: u8) -> usize { - let sc = scan(tx_type); - (0..16).rev().find(|&i| levels[sc[i] as usize] != 0).map_or(0, |i| i + 1) -} - -/// Rough coefficient bit cost (1/256 bit) for rate-distortion decisions. -fn levels_cost(levels: &[i32; 16], eob: usize) -> u32 { - if eob == 0 { - return 0; - } - let mut bits = 256; - for &l in levels { - let a = l.unsigned_abs(); - bits += if a == 0 { 160 } else { 640 + 384 * (32 - a.leading_zeros()) }; - } - bits -} - -/// Intra prediction (8.5.1) of a square block for DC / V / H / TM. -fn predict_intra(p: &Plane, x: usize, y: usize, log2: usize, mode: u8, edges: IntraEdges) -> [i32; 64] { - let IntraEdges { left: have_left, above: have_above } = edges; - let size = 1usize << log2; - let base = 128i32; - let buf = &p.rec; - let w = p.w; - let mut above = [0i32; 8]; - let mut left = [0i32; 8]; - if have_above { - for i in 0..size { - above[i] = buf[(y - 1) * w + (x + i).min(p.max_x)] as i32; - } - } else { - above[..size].fill(base - 1); - } - let top_left = if have_above && have_left { - buf[(y - 1) * w + (x - 1).min(p.max_x)] as i32 - } else if have_above { - base + 1 - } else { - base - 1 - }; - if have_left { - for i in 0..size { - left[i] = buf[(y + i).min(p.max_y) * w + x - 1] as i32; - } - } else { - left[..size].fill(base + 1); - } - let mut out = [0i32; 64]; - for i in 0..size { - for j in 0..size { - out[i * size + j] = match mode { - V_PRED => above[j], - H_PRED => left[i], - TM_PRED => (above[j] + left[i] - top_left).clamp(0, 255), - _ => 0, - }; - } - } - if mode == DC_PRED { - let v = match (have_left, have_above) { - (true, true) => (left[..size].iter().sum::() + above[..size].iter().sum::() + size as i32) >> (log2 + 1), - (true, false) => (left[..size].iter().sum::() + (1 << (log2 - 1))) >> log2, - (false, true) => (above[..size].iter().sum::() + (1 << (log2 - 1))) >> log2, - (false, false) => base, - }; - out[..size * size].fill(v); - } - out -} - -// ---------------------------------------------------------------- motion vector coding - -/// `mv_class` of an offset (|component| - 1). -fn mv_class(offset: u32) -> u32 { - if offset < 16 { 0 } else { (31 - offset.leading_zeros()) - 3 } -} - -fn mv_component_cost(i: usize, v: i32) -> u32 { - let d = &probs_defaults().0; - let offset = v.unsigned_abs() - 1; - let c = mv_class(offset); - let mut bits = cost(v < 0, d[probs::MV_SIGN + i]); - bits += tree_cost(&MV_CLASS_TREE, &d[probs::MV_CLASS + i * 10..], c as u8); - let fr = ((offset >> 1) & 3) as u8; - if c == 0 { - let b = offset >> 3; - bits += cost(b == 1, d[probs::MV_CLASS0_BIT + i]); - bits += tree_cost(&MV_FR_TREE, &d[probs::MV_CLASS0_FR + i * 6 + b as usize * 3..], fr); - } else { - let dd = (offset - (1 << (c + 3))) >> 3; - for b in 0..c { - bits += cost((dd >> b) & 1 == 1, d[probs::MV_BITS + i * 10 + b as usize]); - } - bits += tree_cost(&MV_FR_TREE, &d[probs::MV_FR + i * 3..], fr); - } - bits -} - -const MV_TABLE: i32 = 8192; - -/// Component costs for |v| ≤ MV_TABLE (index v + MV_TABLE) and the four joint costs. -fn mv_tables() -> &'static ([Vec; 2], [u32; 4]) { - use std::sync::OnceLock; - static T: OnceLock<([Vec; 2], [u32; 4])> = OnceLock::new(); - T.get_or_init(|| { - let comp = std::array::from_fn(|i| (-MV_TABLE..=MV_TABLE).map(|v| if v == 0 { 0 } else { mv_component_cost(i, v) }).collect()); - let d = &probs_defaults().0; - let joints = std::array::from_fn(|j| tree_cost(&MV_JOINT_TREE, &d[probs::MV_JOINT..], j as u8)); - (comp, joints) - }) -} - -/// Bits (1/256) of a NEWMV difference with the default probabilities. -pub fn mv_cost(diff: Mv) -> u32 { - let (comp, joints) = mv_tables(); - let joint = (diff.row != 0) as usize * 2 + (diff.col != 0) as usize; - let c = |i: usize, v: i32| if v.abs() <= MV_TABLE { comp[i][(v + MV_TABLE) as usize] } else { mv_component_cost(i, v) }; - joints[joint] + if diff.row != 0 { c(0, diff.row) } else { 0 } + if diff.col != 0 { c(1, diff.col) } else { 0 } -} - -fn probs_defaults() -> &'static probs::Probs { - use std::sync::OnceLock; - static D: OnceLock = OnceLock::new(); - D.get_or_init(probs::Probs::defaults) -} - -/// SAD of two N×N blocks: per-column 16-bit sums (at most 16 · 255), summed once. -#[inline(always)] -fn sad_n(a: &[u8], sa: usize, b: &[u8], sb: usize) -> u32 { - let mut acc = [0u16; N]; - for i in 0..N { - let (Some(ra), Some(rb)) = (a.get(i * sa..).and_then(|r| r.first_chunk::()), b.get(i * sb..).and_then(|r| r.first_chunk::())) else { - return u32::MAX / 4; - }; - for j in 0..N { - acc[j] += ra[j].abs_diff(rb[j]) as u16; - } - } - acc.iter().map(|&v| v as u32).sum() -} - -// ---------------------------------------------------------------- the tile - -/// Per-frame coding parameters shared by the tiles. -pub struct FrameParams<'a> { - pub key: bool, - pub quant: Quant, - pub mi_cols: usize, - pub mi_rows: usize, - /// Reference planes (inter frames). - pub refs: Option<[RefPlane<'a>; 3]>, - /// The reference luma at every quarter-sample phase (motion search). - pub phases: Option, - /// Encoder-side motion hints from the previous frame (by MI position). - pub prev_mvs: Option<&'a [Mv]>, - /// Lagrangian multipliers: per bit, in SAD and in squared-error units. - pub lambda_sad: f64, - pub lambda_sse: f64, -} - -pub struct Tile<'a> { - pub f: &'a FrameParams<'a>, - pub w: Writer, - pub planes: &'a mut [Plane; 3], - pub mi: &'a mut [Mi], - pub col_start: usize, - pub col_end: usize, - above_nz: [Vec; 3], - left_nz: [Vec; 3], - above_part: Vec, - left_part: Vec, - /// Motion vectors of the four 8×8 blocks evaluated for the current 16×16 area. - pending: [(usize, usize, Mv); 4], -} - -impl<'a> Tile<'a> { - pub fn new(f: &'a FrameParams<'a>, planes: &'a mut [Plane; 3], mi: &'a mut [Mi], col_start: usize, col_end: usize) -> Tile<'a> { - let n = f.mi_cols * 2 + 16; - Tile { - f, - w: Writer::default(), - planes, - mi, - col_start, - col_end, - above_nz: [vec![0; n], vec![0; n], vec![0; n]], - left_nz: [vec![0; 16], vec![0; 16], vec![0; 16]], - above_part: vec![0; f.mi_cols + 8], - left_part: vec![0; 8], - pending: [(usize::MAX, 0, Mv::ZERO); 4], - } - } - - /// Code the tile's superblocks. - pub fn code(&mut self) { - for r in (0..self.f.mi_rows).step_by(8) { - for p in 0..3 { - self.left_nz[p].fill(0); - } - self.left_part.fill(0); - for c in (self.col_start..self.col_end).step_by(8) { - self.partition(r, c, 12); - } - } - } - - fn mi_at(&self, r: usize, c: usize) -> Mi { - self.mi[r * self.f.mi_cols + c] - } - - fn set_mi(&mut self, r: usize, c: usize, n8: usize, m: Mi) { - for dr in 0..n8 { - for dc in 0..n8 { - if r + dr < self.f.mi_rows && c + dc < self.f.mi_cols { - self.mi[(r + dr) * self.f.mi_cols + c + dc] = m; - } - } - } - } - - fn partition(&mut self, r: usize, c: usize, bsize: u8) { - let (mi_rows, mi_cols) = (self.f.mi_rows, self.f.mi_cols); - if r >= mi_rows || c >= mi_cols { - return; - } - let num8 = [1usize, 1, 1, 1, 1, 2, 2, 2, 4, 4, 4, 8, 8][bsize as usize]; - let half = num8 >> 1; - let has_rows = (r + half) < mi_rows; - let has_cols = (c + half) < mi_cols; - let bsl = [0usize, 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3][bsize as usize]; - let boffset = 3 - bsl; - let mut above = 0u8; - let mut left = 0u8; - for i in 0..num8 { - above |= self.above_part[c + i]; - left |= self.left_part[(r & 7) + i]; - } - let ctx = bsl * 4 + (((left >> boffset) & 1) as usize) * 2 + ((above >> boffset) & 1) as usize; - let key = self.f.key; - let prob = |i: usize| if key { P::Fixed(KF_PARTITION_PROBS[ctx * 3 + i]) } else { P::Ctx((probs::PARTITION + ctx * 3 + i) as u16) }; - if bsize == BLOCK_8X8 { - self.w.put(false, prob(0)); // PARTITION_NONE - if key { - self.intra_block(r, c); - } else { - let mv = self.pending.iter().find(|p| p.0 == r && p.1 == c).map(|p| p.2); - let mv = mv.unwrap_or_else(|| self.search(r, c, BLOCK_8X8).0); - self.inter_block(r, c, BLOCK_8X8, mv); - } - self.above_part[c] = 15 >> 1; - self.left_part[r & 7] = 15 >> 1; - return; - } - if !key && bsize == BLOCK_16X16 && has_rows && has_cols { - if let Some(mv) = self.decide16(r, c) { - self.w.put(false, prob(0)); // PARTITION_NONE - self.inter_block(r, c, BLOCK_16X16, mv); - for i in 0..2 { - self.above_part[c + i] = 15 >> 2; - self.left_part[(r & 7) + i] = 15 >> 2; - } - return; - } - } else if !key && bsize == BLOCK_16X16 { - self.pending = [(usize::MAX, 0, Mv::ZERO); 4]; - } - // PARTITION_SPLIT. - if has_rows && has_cols { - self.w.tree(&PARTITION_TREE, 3, prob); - } else if has_cols { - self.w.put(true, prob(1)); - } else if has_rows { - self.w.put(true, prob(2)); - } - let sub = bsize - 3; - self.partition(r, c, sub); - self.partition(r, c + half, sub); - self.partition(r + half, c, sub); - self.partition(r + half, c + half, sub); - } - - // ------------------------------------------------------------ intra (key frames) - - fn intra_block(&mut self, r: usize, c: usize) { - let avail_u = r > 0; - let avail_l = c > self.col_start; - let y_mode = self.choose_intra(0, c * 8, r * 8, 3, IntraEdges { left: avail_l, above: avail_u }); - let uv_mode = { - let mut best = (i64::MAX, DC_PRED); - for m in MODES { - let cost: i64 = (1..3).map(|p| self.intra_cost(p, c * 4, r * 4, 2, m, IntraEdges { left: avail_l, above: avail_u })).sum(); - if cost < best.0 { - best = (cost, m); - } - } - best.1 - }; - // skip = 0 (neighbours never skip in key frames: context 0); tx_size implied (4×4). - self.w.ctx(false, probs::SKIP); - let above_mode = if avail_u { self.mi_at(r - 1, c).mode } else { DC_PRED }; - let left_mode = if avail_l { self.mi_at(r, c - 1).mode } else { DC_PRED }; - let o = (above_mode as usize * 10 + left_mode as usize) * 9; - self.w.tree(&INTRA_MODE_TREE, y_mode, |i| P::Fixed(KF_Y_MODE_PROBS[o + i])); - let o = y_mode as usize * 9; - self.w.tree(&INTRA_MODE_TREE, uv_mode, |i| P::Fixed(KF_UV_MODE_PROBS[o + i])); - self.set_mi(r, c, 1, Mi { inter: false, mode: y_mode, mv: Mv::ZERO, skip: false, size: BLOCK_8X8 }); - // Residual: luma 2×2 transform blocks, then one 4×4 block per chroma plane. - for (dy, dx) in [(0, 0), (0, 1), (1, 0), (1, 1)] { - let tx = if self.f.quant.lossless { DCT_DCT } else { mode_tx_type(y_mode) }; - self.intra_tx(0, c * 8 + dx * 4, r * 8 + dy * 4, y_mode, tx, IntraEdges { left: avail_l || dx > 0, above: avail_u || dy > 0 }); - } - for p in 1..3 { - self.intra_tx(p, c * 4, r * 4, uv_mode, DCT_DCT, IntraEdges { left: avail_l, above: avail_u }); - } - } - - fn intra_cost(&self, plane: usize, x: usize, y: usize, log2: usize, mode: u8, edges: IntraEdges) -> i64 { - let p = &self.planes[plane]; - let pred = predict_intra(p, x, y, log2, mode, edges); - let size = 1 << log2; - let mut s = 0i64; - for i in 0..size { - for j in 0..size { - s += (p.src[(y + i) * p.w + x + j] as i32 - pred[i * size + j]).abs() as i64; - } - } - s - } - - fn choose_intra(&self, plane: usize, x: usize, y: usize, log2: usize, edges: IntraEdges) -> u8 { - let mut best = (i64::MAX, DC_PRED); - for m in MODES { - let cost = self.intra_cost(plane, x, y, log2, m, edges); - if cost < best.0 { - best = (cost, m); - } - } - best.1 - } - - fn intra_tx(&mut self, plane: usize, x: usize, y: usize, mode: u8, tx_type: u8, edges: IntraEdges) { - let ss = (plane > 0) as usize; - let max_x = (self.f.mi_cols * 8) >> ss; - let max_y = (self.f.mi_rows * 8) >> ss; - let (x4, y4) = (x >> 2, y >> 2); - let ly = y4 & (15 >> ss); - if x >= max_x || y >= max_y { - self.above_nz[plane][x4] = 0; - self.left_nz[plane][ly] = 0; - return; - } - let p = &self.planes[plane]; - let pred = predict_intra(p, x, y, 2, mode, edges); - let mut res = [0i32; 16]; - for i in 0..4 { - for j in 0..4 { - res[i * 4 + j] = p.src[(y + i) * p.w + x + j] as i32 - pred[i * 4 + j]; - } - } - let q = Quant { round_dc: 0.5, round_ac: 0.38, ..self.f.quant }; - let levels = q.levels(&res, tx_type); - let eob = eob_of(&levels, tx_type); - let ctx0 = (self.above_nz[plane][x4] + self.left_nz[plane][ly]) as usize; - self.tokens(plane, false, tx_type, &levels, eob, ctx0); - let resid = q.residual(&levels, tx_type, eob); - let p = &mut self.planes[plane]; - for i in 0..4 { - for j in 0..4 { - p.rec[(y + i) * p.w + x + j] = (pred[i * 4 + j] + resid[i * 4 + j]).clamp(0, 255) as u8; - } - } - let nz = (eob > 0) as u8; - self.above_nz[plane][x4] = nz; - self.left_nz[plane][ly] = nz; - } - - fn tokens(&mut self, plane: usize, inter: bool, tx_type: u8, levels: &[i32; 16], eob: usize, ctx0: usize) { - let sc = scan(tx_type); - let nb = neighbors(tx_type); - let ptype = (plane > 0) as usize; - let mut tc = [0u8; 16]; - let mut check_eob = true; - let mut c = 0; - while c < 16 { - let pos = sc[c] as usize; - let band = COEFBAND_4X4[c] as usize; - let ctx = if c == 0 { ctx0 } else { ((1 + tc[nb[c].0] + tc[nb[c].1]) >> 1) as usize }; - let o = probs::coef_index(ptype, inter, band, ctx); - if check_eob { - let more = c < eob; - self.w.ctx(more, o); - if !more { - break; - } - } - let v = levels[pos].unsigned_abs(); - if v == 0 { - self.w.ctx(false, o + 1); - tc[pos] = 0; - check_eob = false; - c += 1; - continue; - } - self.w.ctx(true, o + 1); - check_eob = true; - let token = match v { - 1 => 1, - 2 => 2, - 3 => 3, - 4 => 4, - 5..=6 => 5, - 7..=10 => 6, - 11..=18 => 7, - 19..=34 => 8, - 35..=66 => 9, - _ => 10, - }; - if token == 1 { - self.w.ctx(false, o + 2); - } else { - self.w.ctx(true, o + 2); - let path: &[(u8, bool)] = match token { - 2 => &[(0, false), (1, false)], - 3 => &[(0, false), (1, true), (2, false)], - 4 => &[(0, false), (1, true), (2, true)], - 5 => &[(0, true), (3, false), (4, false)], - 6 => &[(0, true), (3, false), (4, true)], - 7 => &[(0, true), (3, true), (5, false), (6, false)], - 8 => &[(0, true), (3, true), (5, false), (6, true)], - 9 => &[(0, true), (3, true), (5, true), (7, false)], - _ => &[(0, true), (3, true), (5, true), (7, true)], - }; - for &(n, b) in path { - self.w.put(b, P::Pareto((o + 2) as u16, n)); - } - let (cat, num_extra, base_v) = EXTRA_BITS[token]; - let extra = (v - base_v as u32).min((1 << num_extra) - 1); - let cp = CAT_PROBS[cat as usize]; - for e in 0..num_extra as usize { - self.w.fixed((extra >> (num_extra as usize - 1 - e)) & 1 != 0, cp[e]); - } - } - tc[pos] = ENERGY_CLASS[token]; - self.w.fixed(levels[pos] < 0, 128); - c += 1; - } - } - - // ------------------------------------------------------------ inter - - fn is_inside(&self, r: i32, c: i32) -> bool { - r >= 0 && (r as usize) < self.f.mi_rows && c >= self.col_start as i32 && (c as usize) < self.col_end - } - - /// `find_mv_refs` + `find_best_ref_mvs` for LAST_FRAME: (NearestMv, NearMv, mode context). - fn mv_refs(&self, r: usize, c: usize, bsize: u8) -> (Mv, Mv, usize) { - let search = if bsize == BLOCK_16X16 { &MV_REF_16X16 } else { &MV_REF_8X8 }; - let mut list = [Mv::ZERO; 2]; - let mut count = 0; - let add = |mv: Mv, list: &mut [Mv; 2], count: &mut usize| { - if *count >= 2 || (*count > 0 && mv == list[0]) { - return; - } - list[*count] = mv; - *count += 1; - }; - let mut counter = 0usize; - for (i, &(dr, dc)) in search.iter().enumerate() { - let (cr, cc) = (r as i32 + dr, c as i32 + dc); - if !self.is_inside(cr, cc) { - continue; - } - let m = self.mi_at(cr as usize, cc as usize); - if i < 2 { - counter += MODE_2_COUNTER[m.mode as usize] as usize; - } - if m.inter { - add(m.mv, &mut list, &mut count); - } - } - let ctx = COUNTER_TO_CONTEXT[counter.min(18)] as usize; - let n8 = if bsize == BLOCK_16X16 { 2 } else { 1 }; - let clamp = |mv: Mv, border: i32| -> Mv { - let (r, c, b) = (r as i32, c as i32, n8); - let to_top = -(r * 64); - let to_bottom = (self.f.mi_rows as i32 - b - r) * 64; - let to_left = -(c * 64); - let to_right = (self.f.mi_cols as i32 - b - c) * 64; - Mv::new( - mv.row.clamp(to_top - border, (to_bottom + border).max(to_top - border)), - mv.col.clamp(to_left - border, (to_right + border).max(to_left - border)), - ) - }; - for mv in list.iter_mut() { - *mv = clamp(*mv, 128); - // No high precision: odd components move toward zero. - let even = |v: i32| if v & 1 != 0 { v + if v > 0 { -1 } else { 1 } } else { v }; - *mv = clamp(Mv::new(even(mv.row), even(mv.col)), (160 - 4) << 3); - } - (list[0], list[1], ctx) - } - - /// The allowed motion vector range of a block (no decoder clamping inside it). - fn mv_range(&self, r: usize, c: usize, n: i32) -> (i32, i32, i32, i32) { - let n8 = n / 8; - let (r, c) = (r as i32, c as i32); - let lo_c = -(c * 64) - (4 + n) * 8; - let hi_c = (self.f.mi_cols as i32 - n8 - c) * 64 + (4 + n) * 8 - 8; - let lo_r = -(r * 64) - (4 + n) * 8; - let hi_r = (self.f.mi_rows as i32 - n8 - r) * 64 + (4 + n) * 8 - 8; - let lim = 8 * 1000; - (lo_r.max(-lim), hi_r.min(lim), lo_c.max(-lim), hi_c.min(lim)) - } - - /// SAD of the luma prediction at `mv` against the source. - fn sad(&self, r: usize, c: usize, n: usize, mv: Mv) -> u32 { - let p = &self.planes[0]; - let (x16, y16) = (((c * 8) << 4) as i32 + 2 * mv.col, ((r * 8) << 4) as i32 + 2 * mv.row); - let mut s = 0u32; - if let Some((pl, o, st)) = self.f.phases.as_ref().and_then(|ph| ph.block(x16, y16, n)) { - let a = &p.src[(r * 8) * p.w + c * 8..]; - let b = &pl[o..]; - return match n { - 8 => sad_n::<8>(a, p.w, b, st), - 16 => sad_n::<16>(a, p.w, b, st), - _ => { - for i in 0..n { - s += a[i * p.w..][..n].iter().zip(&b[i * st..][..n]).map(|(a, b)| a.abs_diff(*b) as u32).sum::(); - } - s - } - }; - } - // Inter search only runs on frames with a reference. - let Some(refs) = self.f.refs.as_ref() else { return u32::MAX / 4 }; - let mut pred = [0u8; 256]; - predict(&refs[0], x16, y16, n, n, &mut pred); - for i in 0..n { - let a = &p.src[(r * 8 + i) * p.w + c * 8..][..n]; - s += a.iter().zip(&pred[i * n..i * n + n]).map(|(a, b)| (*a as i32 - *b as i32).unsigned_abs()).sum::(); - } - s - } - - /// Motion search for the block at (`r`, `c`): the best vector and its cost - /// (SAD + λ·bits). - fn search(&self, r: usize, c: usize, bsize: u8) -> (Mv, f64) { - let n = if bsize == BLOCK_16X16 { 16 } else { 8 }; - let (nearest, near, mctx) = self.mv_refs(r, c, bsize); - let (lo_r, hi_r, lo_c, hi_c) = self.mv_range(r, c, n as i32); - let inside = |mv: Mv| mv.row >= lo_r && mv.row <= hi_r && mv.col >= lo_c && mv.col <= hi_c; - let lambda = self.f.lambda_sad / 256.0; - let d = &probs_defaults().0; - let mb: [u32; 4] = std::array::from_fn(|m| tree_cost(&INTER_MODE_TREE, &d[probs::INTER_MODE + mctx * 3..], m as u8)); - let mode_bits = |m: u8| mb[(m - NEARESTMV) as usize]; - let cost_raw = |mv: Mv| -> f64 { - let bits = if mv == nearest { - mode_bits(NEARESTMV) - } else if mv == Mv::ZERO { - mode_bits(ZEROMV) - } else if mv == near { - mode_bits(NEARMV) - } else { - mode_bits(NEWMV) + mv_cost(mv.minus(nearest)) - }; - self.sad(r, c, n, mv) as f64 + lambda * bits as f64 - }; - // The cost is a pure function of the vector: points the search revisits (the centre it - // came from, candidates on the diamond) are looked up instead of predicted again. - let mut seen = [(Mv::ZERO, 0.0f64); 64]; - let mut nseen = 0usize; - let mut cost = |mv: Mv| -> f64 { - if let Some(&(_, j)) = seen[..nseen].iter().find(|e| e.0 == mv) { - return j; - } - let j = cost_raw(mv); - if nseen < seen.len() { - seen[nseen] = (mv, j); - nseen += 1; - } - j - }; - let mut best = (Mv::ZERO, cost(Mv::ZERO)); - let prev = self.f.prev_mvs.map(|p| p[r * self.f.mi_cols + c]); - for cand in [Some(nearest), Some(near), prev].into_iter().flatten() { - if cand != Mv::ZERO && inside(cand) { - let j = cost(cand); - if j < best.1 { - best = (cand, j); - } - } - } - // A perfect match needs no search. - let good_enough = lambda * 256.0 * 0.5; - if best.1 > good_enough { - // Whole-sample search from the best candidate: a coarse cross, then a small diamond. - let round8 = |v: i32| (v + 4).div_euclid(8) * 8; - let mut cur = Mv::new(round8(best.0.row), round8(best.0.col)); - let mut cur_j = if inside(cur) { cost(cur) } else { f64::MAX }; - if cur_j < best.1 { - best = (cur, cur_j); - } else { - cur = best.0; - cur_j = best.1; - } - for (step, max_iters) in [(16, 1), (8, 2), (4, 3), (2, 4), (1, 8)] { - let s = step * 8; - for _ in 0..max_iters { - let mut moved = false; - let center = cur; - for (dr, dc) in [(-s, 0), (s, 0), (0, -s), (0, s)] { - let cand = Mv::new(center.row + dr, center.col + dc); - if !inside(cand) { - continue; - } - let j = cost(cand); - if j < cur_j { - cur = cand; - cur_j = j; - moved = true; - } - } - if !moved { - break; - } - } - } - if cur_j < best.1 { - best = (cur, cur_j); - } - } - // Sub-sample refinement: half then quarter samples (1/8 units, even: no high precision). - for s in [4, 2] { - let center = best.0; - for (dr, dc) in [(-s, 0), (s, 0), (0, -s), (0, s), (-s, -s), (-s, s), (s, -s), (s, s)] { - let cand = Mv::new(center.row + dr, center.col + dc); - if !inside(cand) { - continue; - } - let j = cost(cand); - if j < best.1 { - best = (cand, j); - } - } - } - best - } - - /// Choose between one 16×16 block and four 8×8 blocks at (`r`, `c`): `Some(mv)` for 16×16. - fn decide16(&mut self, r: usize, c: usize) -> Option { - let (mv16, j16) = self.search(r, c, BLOCK_16X16); - let lambda = self.f.lambda_sad; - // A near-perfect 16×16 prediction needs no split. - if j16 < 2.0 * 256.0 / 256.0 * 16.0 { - return Some(mv16); - } - let mut j8 = lambda * 4.0; // partition split + three more blocks of mode info (≈ 4 bits) - for (k, (dr, dc)) in [(0, 0), (0, 1), (1, 0), (1, 1)].into_iter().enumerate() { - let (rr, cc) = (r + dr, c + dc); - let (mv, j) = self.search(rr, cc, BLOCK_8X8); - j8 += j; - self.pending[k] = (rr, cc, mv); - // Tentative mode info so the next 8×8 blocks see this one as a candidate. - self.set_mi(rr, cc, 1, Mi { inter: true, mode: NEWMV, mv, skip: false, size: BLOCK_8X8 }); - } - if j16 <= j8 { - self.pending = [(usize::MAX, 0, Mv::ZERO); 4]; - Some(mv16) - } else { - None - } - } - - /// Code an inter block with motion vector `mv`. - fn inter_block(&mut self, r: usize, c: usize, bsize: u8, mv: Mv) { - let (mi_rows, mi_cols) = (self.f.mi_rows, self.f.mi_cols); - let n8 = if bsize == BLOCK_16X16 { 2 } else { 1 }; - let n = 8 * n8; - let (nearest, near, mctx) = self.mv_refs(r, c, bsize); - let d = &probs_defaults().0; - let mode_bits = |m: u8| tree_cost(&INTER_MODE_TREE, &d[probs::INTER_MODE + mctx * 3..], m - NEARESTMV); - let mut mode = (NEWMV, mode_bits(NEWMV) + mv_cost(mv.minus(nearest))); - for (m, ok) in [(NEARESTMV, mv == nearest), (NEARMV, mv == near), (ZEROMV, mv == Mv::ZERO)] { - if ok && mode_bits(m) < mode.1 { - mode = (m, mode_bits(m)); - } - } - let mode = mode.0; - // Prediction of every plane (written straight into the reconstruction). - let Some(refs) = self.f.refs.as_ref() else { return }; - for plane in 0..3 { - let ch = plane > 0; - let (row16, col16) = clamp_for_plane(mv, r, c, n8, mi_rows, mi_cols, ch); - let bn = if ch { n / 2 } else { n }; - let (bx, by) = if ch { (c * 4, r * 4) } else { (c * 8, r * 8) }; - let mut pred = [0u8; 256]; - predict(&refs[plane], ((bx as i32) << 4) + col16, ((by as i32) << 4) + row16, bn, bn, &mut pred); - let p = &mut self.planes[plane]; - for i in 0..bn { - p.rec[(by + i) * p.w + bx..][..bn].copy_from_slice(&pred[i * bn..i * bn + bn]); - } - } - // Residual: quantise every 4×4, dropping blocks whose coefficients don't pay for themselves. - let q = Quant { round_dc: 0.5, round_ac: 0.42, ..self.f.quant }; - // Weight of the rate term when dropping a transform block's coefficients (low: keep quality on par - // with key frames at the same q index). - let zl = 0.07; - let tx = DCT_DCT; - let mut blocks: Vec<(usize, usize, usize, [i32; 16], usize)> = Vec::with_capacity(24); - let mut any = false; - for plane in 0..3 { - let ch = plane > 0; - let bn = if ch { n / 2 } else { n }; - let (bx, by) = if ch { (c * 4, r * 4) } else { (c * 8, r * 8) }; - let p = &self.planes[plane]; - for ty in (0..bn).step_by(4) { - for tx4 in (0..bn).step_by(4) { - let (x, y) = (bx + tx4, by + ty); - let mut res = [0i32; 16]; - for i in 0..4 { - for j in 0..4 { - let k = (y + i) * p.w + x + j; - res[i * 4 + j] = p.src[k] as i32 - p.rec[k] as i32; - } - } - let mut levels = q.levels(&res, tx); - let mut eob = eob_of(&levels, tx); - if eob > 0 && !q.lossless { - let back = q.residual(&levels, tx, eob); - let d_skip: i64 = res.iter().map(|v| (*v as i64) * (*v as i64)).sum(); - let d_code: i64 = res.iter().zip(&back).map(|(a, b)| ((a - b) as i64).pow(2)).sum(); - if d_skip as f64 <= d_code as f64 + zl * self.f.lambda_sse * levels_cost(&levels, eob) as f64 / 256.0 { - levels = [0; 16]; - eob = 0; - } - } - any |= eob > 0; - blocks.push((plane, x, y, levels, eob)); - } - } - } - let skip = !any; - // Mode info. - let avail_u = r > 0; - let avail_l = c > self.col_start; - let above = avail_u.then(|| self.mi_at(r - 1, c)); - let left = avail_l.then(|| self.mi_at(r, c - 1)); - let skip_ctx = above.is_some_and(|m| m.skip) as usize + left.is_some_and(|m| m.skip) as usize; - self.w.ctx(skip, probs::SKIP + skip_ctx); - // is_inter context: neighbours are inter (no intra blocks in inter frames). - let intra = |m: Option| m.is_some_and(|m| !m.inter); - let inter_ctx = match (above, left) { - (Some(_), Some(_)) => { - if intra(above) && intra(left) { - 3 - } else { - (intra(above) || intra(left)) as usize - } - } - (Some(_), None) => 2 * intra(above) as usize, - (None, Some(_)) => 2 * intra(left) as usize, - (None, None) => 0, - }; - self.w.ctx(true, probs::IS_INTER + inter_ctx); - // single_ref_p1 = 0 (LAST_FRAME); context for single-reference LAST neighbours. - let ref_ctx = match (above, left) { - (Some(a), Some(l)) => match (a.inter, l.inter) { - (false, false) => 2, - (false, true) => 4, - (true, false) => 4, - (true, true) => 4, - }, - (Some(m), None) | (None, Some(m)) => { - if m.inter { - 4 - } else { - 2 - } - } - (None, None) => 2, - }; - self.w.ctx(false, probs::SINGLE_REF + ref_ctx * 2); - self.w.tree(&INTER_MODE_TREE, mode - NEARESTMV, |i| P::Ctx((probs::INTER_MODE + mctx * 3 + i) as u16)); - if mode == NEWMV { - self.write_mv(mv.minus(nearest)); - } - self.set_mi(r, c, n8, Mi { inter: true, mode, mv, skip, size: bsize }); - // Tokens and reconstruction. - for (plane, x, y, levels, eob) in blocks { - let ss = (plane > 0) as usize; - let (x4, ly) = (x >> 2, (y >> 2) & (15 >> ss)); - if !skip { - let ctx0 = (self.above_nz[plane][x4] + self.left_nz[plane][ly]) as usize; - self.tokens(plane, true, tx, &levels, eob, ctx0); - if eob > 0 { - let resid = q.residual(&levels, tx, eob); - let p = &mut self.planes[plane]; - for i in 0..4 { - for j in 0..4 { - let k = (y + i) * p.w + x + j; - p.rec[k] = (p.rec[k] as i32 + resid[i * 4 + j]).clamp(0, 255) as u8; - } - } - } - } - let nz = (!skip && eob > 0) as u8; - self.above_nz[plane][x4] = nz; - self.left_nz[plane][ly] = nz; - } - } - - fn write_mv(&mut self, diff: Mv) { - let joint = (diff.row != 0) as u8 * 2 + (diff.col != 0) as u8; - self.w.tree(&MV_JOINT_TREE, joint, |i| P::Ctx((probs::MV_JOINT + i) as u16)); - if diff.row != 0 { - self.write_mv_component(0, diff.row); - } - if diff.col != 0 { - self.write_mv_component(1, diff.col); - } - } - - fn write_mv_component(&mut self, i: usize, v: i32) { - debug_assert!(v != 0 && v & 1 == 0, "vectors are even without high precision"); - let offset = v.unsigned_abs() - 1; - let c = mv_class(offset); - self.w.ctx(v < 0, probs::MV_SIGN + i); - self.w.tree(&MV_CLASS_TREE, c as u8, |k| P::Ctx((probs::MV_CLASS + i * 10 + k) as u16)); - let fr = ((offset >> 1) & 3) as u8; - if c == 0 { - let b = (offset >> 3) as usize; - self.w.ctx(b == 1, probs::MV_CLASS0_BIT + i); - self.w.tree(&MV_FR_TREE, fr, |k| P::Ctx((probs::MV_CLASS0_FR + i * 6 + b * 3 + k) as u16)); - } else { - let dd = (offset - (1 << (c + 3))) >> 3; - for b in 0..c { - self.w.ctx((dd >> b) & 1 == 1, probs::MV_BITS + i * 10 + b as usize); - } - self.w.tree(&MV_FR_TREE, fr, |k| P::Ctx((probs::MV_FR + i * 3 + k) as u16)); - } - } -} diff --git a/crates/vp9enc/src/transform.rs b/crates/vp9enc/src/transform.rs deleted file mode 100644 index 20996d60b..000000000 --- a/crates/vp9enc/src/transform.rs +++ /dev/null @@ -1,270 +0,0 @@ -//! 4×4 transforms: the decoder's integer inverse DCT / ADST (spec 8.7.1) and Walsh–Hadamard -//! transform (8.7.1.10), and their forward counterparts (the exact inverse matrices of the -//! decoder's linear transforms, so `inverse4(forward(res)) ≈ res`; the WHT inverse is exact). - -pub const DCT_DCT: u8 = 0; -pub const ADST_DCT: u8 = 1; -pub const DCT_ADST: u8 = 2; -pub const ADST_ADST: u8 = 3; - -fn r14(v: i32) -> i32 { - (v + (1 << 13)) >> 14 -} - -fn idct4(x: [i32; 4]) -> [i32; 4] { - let v0 = r14(x[0] * 11585 - x[2] * 11585); - let v1 = r14(x[0] * 11585 + x[2] * 11585); - let v2 = r14(x[1] * 6270 - x[3] * 15137); - let v3 = r14(x[1] * 15137 + x[3] * 6270); - [v1 + v3, v0 + v2, v0 - v2, v1 - v3] -} - -fn iadst4(x: [i32; 4]) -> [i32; 4] { - let (s19, s29, s39, s49) = (5283i64, 9929i64, 13377i64, 15212i64); - let x: [i64; 4] = x.map(|v| v as i64); - let s0 = s19 * x[0]; - let s1 = s29 * x[0]; - let s2 = s39 * x[1]; - let s3 = s49 * x[2]; - let s4 = s19 * x[2]; - let s5 = s29 * x[3]; - let s6 = s49 * x[3]; - let s7 = s39 * (x[0] - x[2] + x[3]); - let x0 = s0 + s3 + s5; - let x1 = s1 - s4 - s6; - let x2 = s7; - let x3 = s2; - let r = |v: i64| ((v + (1 << 13)) >> 14) as i32; - [r(x0 + x3), r(x1 + x3), r(x2), r(x0 + x1 - x3)] -} - -/// The decoder's 4×4 inverse transform of dequantised coefficients (row-major) to a residual. -pub fn inverse4(coefs: &[i32; 16], tx_type: u8) -> [i32; 16] { - let row_adst = matches!(tx_type, DCT_ADST | ADST_ADST); - let col_adst = matches!(tx_type, ADST_DCT | ADST_ADST); - let mut t = [[0i32; 4]; 4]; - for i in 0..4 { - let r = [coefs[i * 4], coefs[i * 4 + 1], coefs[i * 4 + 2], coefs[i * 4 + 3]]; - t[i] = if row_adst { iadst4(r) } else { idct4(r) }; - } - let mut out = [0i32; 16]; - for j in 0..4 { - let c = [t[0][j], t[1][j], t[2][j], t[3][j]]; - let c = if col_adst { iadst4(c) } else { idct4(c) }; - for i in 0..4 { - out[i * 4 + j] = (c[i] + 8) >> 4; - } - } - out -} - -/// Inverse Walsh–Hadamard (8.7.1.10) on one line. -fn iwht(x: [i32; 4], shift: u32) -> [i32; 4] { - let mut a = x[0] >> shift; - let mut c = x[1] >> shift; - let mut d = x[2] >> shift; - let mut b = x[3] >> shift; - a += c; - d -= b; - let e = (a - d) >> 1; - b = e - b; - c = e - c; - a -= b; - d += c; - [a, b, c, d] -} - -/// The exact inverse of [`iwht`] (with shift 0): inputs that produce outputs `y`. -fn fwht(y: [i32; 4]) -> [i32; 4] { - let (aa, bb, cc, dd) = (y[0], y[1], y[2], y[3]); - let d1 = dd - cc; - let a1 = aa + bb; - let e = (a1 - d1) >> 1; - let b0 = e - bb; - let c0 = e - cc; - let a0 = a1 - c0; - let d0 = d1 + b0; - [a0, c0, d0, b0] -} - -pub fn inverse_wht(coefs: &[i32; 16]) -> [i32; 16] { - let mut t = [[0i32; 4]; 4]; - for i in 0..4 { - t[i] = iwht([coefs[i * 4], coefs[i * 4 + 1], coefs[i * 4 + 2], coefs[i * 4 + 3]], 2); - } - let mut out = [0i32; 16]; - for j in 0..4 { - let r = iwht([t[0][j], t[1][j], t[2][j], t[3][j]], 0); - for i in 0..4 { - out[i * 4 + j] = r[i]; - } - } - out -} - -/// Lossless forward transform: levels (dequantised by 4) whose inverse is exactly `res`. -pub fn forward_wht(res: &[i32; 16]) -> [i32; 16] { - let mut t = [[0i32; 4]; 4]; - for j in 0..4 { - let col = fwht([res[j], res[4 + j], res[8 + j], res[12 + j]]); - for i in 0..4 { - t[i][j] = col[i]; - } - } - let mut lv = [0i32; 16]; - for i in 0..4 { - let r = fwht(t[i]); - lv[i * 4..i * 4 + 4].copy_from_slice(&r); - } - lv -} - -/// Forward transform matrices: the inverses of the decoder's (linear part of the) inverse -/// transforms. -pub fn forward_matrices() -> &'static [[[f64; 16]; 16]; 4] { - use std::sync::OnceLock; - static M: OnceLock<[[[f64; 16]; 16]; 4]> = OnceLock::new(); - M.get_or_init(|| { - let mut out = [[[0.0; 16]; 16]; 4]; - for (ty, o) in out.iter_mut().enumerate() { - // Columns of the inverse: response to a scaled unit coefficient. - let k = 1 << 16; - let mut inv = [[0.0f64; 16]; 16]; - for c in 0..16 { - let mut e = [0i32; 16]; - e[c] = k; - let r = inverse4(&e, ty as u8); - for p in 0..16 { - inv[p][c] = r[p] as f64 / k as f64; - } - } - *o = invert16(inv); - } - out - }) -} - -/// [`forward_matrices`] transposed: `[type][q][k]`. -fn forward_matrices_t() -> &'static [[[f64; 16]; 16]; 4] { - use std::sync::OnceLock; - static M: OnceLock<[[[f64; 16]; 16]; 4]> = OnceLock::new(); - M.get_or_init(|| { - let f = forward_matrices(); - std::array::from_fn(|t| std::array::from_fn(|q| std::array::from_fn(|k| f[t][k][q]))) - }) -} - -fn invert16(m: [[f64; 16]; 16]) -> [[f64; 16]; 16] { - let mut a = m; - let mut inv = [[0.0; 16]; 16]; - for (i, r) in inv.iter_mut().enumerate() { - r[i] = 1.0; - } - for col in 0..16 { - let piv = (col..16).max_by(|&x, &y| a[x][col].abs().total_cmp(&a[y][col].abs())).unwrap_or(col); - a.swap(col, piv); - inv.swap(col, piv); - let d = a[col][col]; - for j in 0..16 { - a[col][j] /= d; - inv[col][j] /= d; - } - for r in 0..16 { - if r != col { - let f = a[r][col]; - if f != 0.0 { - for j in 0..16 { - a[r][j] -= f * a[col][j]; - inv[r][j] -= f * inv[col][j]; - } - } - } - } - } - inv -} - -/// Quantisation settings of one block. -#[derive(Clone, Copy)] -pub struct Quant { - pub dcq: i32, - pub acq: i32, - pub lossless: bool, - /// Rounding offsets (fractions of a step) for DC and AC; smaller = wider dead zone. - pub round_dc: f64, - pub round_ac: f64, -} - -impl Quant { - /// Quantised levels (raster order) of a residual. - pub fn levels(&self, res: &[i32; 16], tx_type: u8) -> [i32; 16] { - if self.lossless { - return forward_wht(res); - } - let ft = &forward_matrices_t()[tx_type as usize]; - // Every output accumulates its 16 products in the same order (q ascending) as a - // per-output sum, but the 16 outputs advance together: 16 independent chains that - // vectorize instead of one serial chain per output. - let mut acc = [-0.0f64; 16]; - for (q, col) in ft.iter().enumerate() { - let r = res[q] as f64; - for k in 0..16 { - acc[k] += col[k] * r; - } - } - let mut lv = [0i32; 16]; - for k in 0..16 { - let coef = acc[k]; - let (qs, rnd) = if k == 0 { (self.dcq, self.round_dc) } else { (self.acq, self.round_ac) }; - // Non-negative: truncation is floor. - let l = (coef.abs() / qs as f64 + rnd) as i32; - lv[k] = if coef < 0.0 { -l } else { l }; - } - lv - } - - /// The decoder's residual for `levels` (all zero when `eob` is 0). - pub fn residual(&self, levels: &[i32; 16], tx_type: u8, eob: usize) -> [i32; 16] { - if eob == 0 { - return [0; 16]; - } - let mut deq = [0i32; 16]; - for k in 0..16 { - deq[k] = levels[k] * if k == 0 { self.dcq } else { self.acq }; - } - if self.lossless { inverse_wht(&deq) } else { inverse4(&deq, tx_type) } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn wht_is_exactly_invertible() { - let mut s = 7u32; - for _ in 0..2000 { - let mut r = [0i32; 16]; - for v in &mut r { - s = s.wrapping_mul(1664525).wrapping_add(1013904223); - *v = (s >> 16) as i32 % 511 - 255; - } - let lv = forward_wht(&r); - let deq = lv.map(|l| l * 4); - assert_eq!(inverse_wht(&deq), r); - } - } - - #[test] - fn forward_transforms_invert_the_decoder() { - for ty in 0..4u8 { - let f = &forward_matrices()[ty as usize]; - let r: [i32; 16] = std::array::from_fn(|i| (i as i32 * 37 % 61) - 30); - let c: [i32; 16] = std::array::from_fn(|k| (0..16).map(|q| f[k][q] * r[q] as f64).sum::().round() as i32); - let back = inverse4(&c, ty); - for i in 0..16 { - assert!((back[i] - r[i]).abs() <= 1, "type {ty}: {back:?} vs {r:?}"); - } - } - } -} diff --git a/docs/aep-export.md b/docs/aep-export.md index e8b89b424..4ddfd4156 100644 --- a/docs/aep-export.md +++ b/docs/aep-export.md @@ -1,8 +1,8 @@ # After Effects project export -> **Last reviewed:** 2026-10-19 · **Last updated:** 2026-11-05 · **Change:** a 3D layer's changed Material Options are reported, shape and text animator colours outside 0 to 1 are clamped and reported, and a popup expression is no longer reported where the option order is the same (2026-11-05); before that: composition settings the base writes at their defaults (Preserve Frame Rate / Resolution, motion blur samples, adaptive sample limit) and render queue items are reported, and a sequence's folder time is read only when its file passes the document-path policy (2026-11-04); before that: every chunk is built in code, no template is read (2026-10-29); before that: Curves and Mesh Warp are written (R53); before that: a switched-off shape item or layer style is written switched off (R52, 986-988); before that: a reversed layer (negative stretch) is written as After Effects stores it (R36, 981-983); before that: an explicit None (`NO_LAYER`) on Compound Blur, Displacement Map or Set Matte is written as None, and Layer Control on a named layer no longer reports a source choice; before that: layer styles are written (R51, 950-980; read back in After Effects: `export-tests\styles`); before that: an image sequence's folder modification time is written when the caller knows it (`sspc@118`, R50: without it After Effects rescans the folder on open and warns about every gap of a sparse sequence in a modal dialog); before that: an output space equal to the working space is not reported (After Effects renders in the working space, 902; the importer sets it so for Rec.709 Gamma 2.4); before that: one effect table for both directions again: the 27 effects of the importer round 3 and the Layer Control are written too (prototypes from 903 and 076, layer parameters by layer id; not read back in After Effects yet); before that: the importer round 3 is merged: Offset Paths' Copy Offset is written as EffectCraft's value + 1 (826-828, the importer's inverse); before that: sparse sequences checked (860, written as After Effects stores them); before that: the Rec.709 Gamma 2.4 working space is written with a profile authored here (R49, g5); before that: text animators with range selectors are written (R48, t8); before that: a footage layer running past its source is ended there on import, as After Effects does, and less than a frame past is not a loss (LayerTiming); before that: Anchor Point and mask path ease are written (R47); before that: shape operators (polystar, gradients, trim paths, round corners, merge / offset paths, repeater) and keys and expressions on shape properties are written (R46, o2); before that: every effect of the importer's table is written, through one shared table and records built per parameter type (R45, e2); before that: time remapping is written (319, 354, r1, R44); before that: the character and paragraph settings are read back in After Effects (t6) and right-to-left justification is written relative to the direction (t7, R43); before that: the character and paragraph settings the importer decodes are written (scales, baseline shift, kerning, ligatures, tsume, fill / stroke switches and width, justified variants, indents, spacing, composer, direction); before that: track mattes, markers, cameras, lights and shape layers (R39-R42), keys on stretched layers, conformed clips, tga / psd / ai / eps, project settings (620-634, R37), superscript / subscript (526-528); before: precomposition layers (124, 257, 258, 600-602), the effects Tint, Gradient Ramp and Exposure (356-397, 610), footage (351, 353, 603-607, 611-613), text layers (110, 245, t1-t4), the 3D switch (015) and time stretch (018, 316, 317) are exported · **Target:** Adobe After Effects 2026 +> **Last reviewed:** 2026-10-19 · **Last updated:** 2026-11-08 · **Change:** the Source Text expression and a text animator's Stroke Opacity are written (#3170); before that: a 3D layer's changed Material Options are reported, shape and text animator colours outside 0 to 1 are clamped and reported, and a popup expression is no longer reported where the option order is the same (2026-11-05); before that: composition settings the base writes at their defaults (Preserve Frame Rate / Resolution, motion blur samples, adaptive sample limit) and render queue items are reported, and a sequence's folder time is read only when its file passes the document-path policy (2026-11-04); before that: every chunk is built in code, no template is read (2026-10-29); before that: Curves and Mesh Warp are written (R53); before that: a switched-off shape item or layer style is written switched off (R52, 986-988); before that: a reversed layer (negative stretch) is written as After Effects stores it (R36, 981-983); before that: an explicit None (`NO_LAYER`) on Compound Blur, Displacement Map or Set Matte is written as None, and Layer Control on a named layer no longer reports a source choice; before that: layer styles are written (R51, 950-980; read back in After Effects: `export-tests\styles`); before that: an image sequence's folder modification time is written when the caller knows it (`sspc@118`, R50: without it After Effects rescans the folder on open and warns about every gap of a sparse sequence in a modal dialog); before that: an output space equal to the working space is not reported (After Effects renders in the working space, 902; the importer sets it so for Rec.709 Gamma 2.4); before that: one effect table for both directions again: the 27 effects of the importer round 3 and the Layer Control are written too (prototypes from 903 and 076, layer parameters by layer id; not read back in After Effects yet); before that: the importer round 3 is merged: Offset Paths' Copy Offset is written as EffectCraft's value + 1 (826-828, the importer's inverse); before that: sparse sequences checked (860, written as After Effects stores them); before that: the Rec.709 Gamma 2.4 working space is written with a profile authored here (R49, g5); before that: text animators with range selectors are written (R48, t8); before that: a footage layer running past its source is ended there on import, as After Effects does, and less than a frame past is not a loss (LayerTiming); before that: Anchor Point and mask path ease are written (R47); before that: shape operators (polystar, gradients, trim paths, round corners, merge / offset paths, repeater) and keys and expressions on shape properties are written (R46, o2); before that: every effect of the importer's table is written, through one shared table and records built per parameter type (R45, e2); before that: time remapping is written (319, 354, r1, R44); before that: the character and paragraph settings are read back in After Effects (t6) and right-to-left justification is written relative to the direction (t7, R43); before that: the character and paragraph settings the importer decodes are written (scales, baseline shift, kerning, ligatures, tsume, fill / stroke switches and width, justified variants, indents, spacing, composer, direction); before that: track mattes, markers, cameras, lights and shape layers (R39-R42), keys on stretched layers, conformed clips, tga / psd / ai / eps, project settings (620-634, R37), superscript / subscript (526-528); before: precomposition layers (124, 257, 258, 600-602), the effects Tint, Gradient Ramp and Exposure (356-397, 610), footage (351, 353, 603-607, 611-613), text layers (110, 245, t1-t4), the 3D switch (015) and time stretch (018, 316, 317) are exported · **Target:** Adobe After Effects 2026 -File ▸ Export ▸ After Effects Project… (`file.exportAep`) writes the open project as an `.aep` that +File ▸ Export ▸ Adobe After Effects Project… (`file.exportAep`) writes the open project as an `.aep` that After Effects 2026 opens. It is deliberately narrow: what it writes is exact, tested and checked against After Effects itself; everything else is **reported** and never dropped silently. The importer ([aep-import.md](aep-import.md)) is the mirror image; both rest on the clean-room format @@ -67,7 +67,7 @@ the dry run `analyze(&Project) -> ExportReport`. A refused or failed export writ | Expressions | on those five properties: source text and enable switch | | Masks | on solid, adjustment and null layers, in stack order: name, mode (all seven), inverted, path (vertices and tangents in layer pixels, open or closed), feather, opacity, expansion (each only when not at the default), path keys (time, linear, bezier or hold sides with their ease, label) with one shape per key, Mask Feather keys (time, interpolation, value, ease per dimension, label) | | Precompositions | a composition shown as a layer of another: the layer's source is the composition item, the using composition lists each composition it shows once (`idpi`); the anchor is a fraction of the composition's size; precompositions nest to any depth, a loop is reported | -| Text | point and box text: the text (paragraphs, CR / LF / CRLF as After Effects' CR), per paragraph the justification (left, right, centre and the four justified variants, codes 3-6), first-line / start / end indent, space before / after, hanging punctuation, composer and direction (paragraph slots `/1`-`/5`, `/21`, `/29`, `/33`), per style run the font (PostScript name), size, fill and stroke colour, tracking, automatic or explicit leading, faux bold and italic, all caps and small caps, horizontal / vertical scale, baseline shift, automatic kerning (metrics, optical, none), ligatures, tsume, whether fill and stroke are drawn, stroke width and stroke over fill (style slots `/6`, `/7`, `/9`, `/11`, `/18`, `/36`, `/56`-`/58`, `/63`, as the importer decodes them, 538-569; written only where they differ from the prototype; read back in After Effects, R43), the justification of a right-to-left paragraph relative to its direction (R43), superscript and subscript (style slot `/13`: normal text is written 0, 527; the prototypes and After Effects' scripted default hold 1 = superscript, drawn at 58.3 % size, 526); the box; the anchor in pixels; the name only when it is not the text's first line; text animators: Anchor Point, Position, Scale, Skew, Skew Axis, Rotation, Opacity, Fill / Stroke Color, Stroke Width, Tracking, Line Spacing, Blur, Character Offset with keys and expressions, range selectors (Start / End / Offset in percent or index units, Units, Based On, Mode, Amount, Shape, Smoothness, Ease High / Low, Randomize Order, Random Seed; R48) | +| Text | point and box text: the text (paragraphs, CR / LF / CRLF as After Effects' CR), per paragraph the justification (left, right, centre and the four justified variants, codes 3-6), first-line / start / end indent, space before / after, hanging punctuation, composer and direction (paragraph slots `/1`-`/5`, `/21`, `/29`, `/33`), per style run the font (PostScript name), size, fill and stroke colour, tracking, automatic or explicit leading, faux bold and italic, all caps and small caps, horizontal / vertical scale, baseline shift, automatic kerning (metrics, optical, none), ligatures, tsume, whether fill and stroke are drawn, stroke width and stroke over fill (style slots `/6`, `/7`, `/9`, `/11`, `/18`, `/36`, `/56`-`/58`, `/63`, as the importer decodes them, 538-569; written only where they differ from the prototype; read back in After Effects, R43), the justification of a right-to-left paragraph relative to its direction (R43), superscript and subscript (style slot `/13`: normal text is written 0, 527; the prototypes and After Effects' scripted default hold 1 = superscript, drawn at 58.3 % size, 526); the box; the anchor in pixels; the name only when it is not the text's first line; the Source Text expression, enabled or disabled (`tdb4` bytes 119 / 120 and a `Utf8` after `cdat`, as After Effects wrote it in `text-synthetic.aep`, #3170); text animators: Anchor Point, Position, Scale, Skew, Skew Axis, Rotation, Opacity, Fill / Stroke Color, Stroke Opacity, Stroke Width, Tracking, Line Spacing, Blur, Character Offset with keys and expressions, range selectors (Start / End / Offset in percent or index units, Units, Based On, Mode, Amount, Shape, Smoothness, Ease High / Low, Randomize Order, Random Seed; R48) | | Footage | png, jpeg, OpenEXR, TIFF, Targa, (flat) Photoshop, Illustrator and EPS stills, png, OpenEXR and jpeg sequences, mp4 and mov clips (also conformed to another rate, R38), WAV audio: the path (a sequence's folder and numbering), size, duration, native frame rate, the name (none stored when it is the file's, as After Effects does); After Effects re-reads everything else from the file when it opens the project | | Effects | every effect of the table the importer reads (`effectcraft_aep::effect_map`, one table for both directions): Tint, Gradient Ramp, Exposure, Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Timewarp, Glow, Fill, Hue/Saturation (master and colorize), Levels (RGB), Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker, Transform, Brightness & Contrast, Turbulent Displace, Fractal Noise, Beam, CC Radial Fast Blur, Noise, Levels (Individual Controls), Corner Pin, Grid, Sharpen, Linear Color Key, Radial Wipe, Remove Color Matting, CC Bend It, CC Power Pin, 4-Color Gradient, Change Color, Extract, Noise HLS, Scatter, CC Light Sweep, Compound Blur, Displacement Map, Set Matte, Gaussian Blur (Legacy), Pixel Motion Blur and the Slider, Angle, Checkbox, Color, Point and Layer expression controls: the instance name, every parameter that differs from its default (colours, points, fixed, integer and float sliders, angles, popups by item name, checkboxes, layer parameters: the `tdpi` of their always-present record holds the owning layer's id by default, 0 for None (EffectCraft's unset Layer Control, or the explicit `NO_LAYER` of the other effects), or the named layer's id, 320 / 321 / 904; Layer Control's companion source choice is not reported (After Effects has none for it); the 27 effects of batch 6 (903-905) and the Layer Control are written from prototypes but not yet read back in After Effects), expressions on parameters, Hue/Saturation's master values and Levels' RGB values (arbitrary data), keys on colours, points and fixed / float sliders (time, linear / bezier / hold, the ease of sliders and points, labels), in stack order, several instances of a type per layer and per project (R24-R27, R45) | | Layer styles | the nine styles and Blending Options (Fill Opacity, channel switches, Blend Interior, Blend Ranges; the composition's Global Light on every styled layer) on solid, footage, precomposition, text and shape layers: a record for every parameter that differs from After Effects' default, keys on scalars, expressions (R51) | @@ -93,7 +93,7 @@ at all. | `effects` | an effect outside the table (each instance), a parameter without an After Effects counterpart that carries keys or an expression or a value other than a new instance's (a default that depends on the layer size is not compared), the effect's label colour, a colour parameter's opacity (After Effects' have none; Drop Shadow uses it), a fraction on an integer parameter (written whole), Curves points outside 0..1 (written at the limit), keys on a popup, checkbox, angle or integer slider (the first key's value is written), keys or expressions on Hue/Saturation's or Levels' data values, a fractional Hue/Saturation master value (written whole), a popup item After Effects does not have, keys or an expression on a layer parameter (its value is written), a layer parameter naming a layer that is not exported (the owning layer is written) or whose layer is taken with its masks and effects (After Effects takes the source) | | `markers` | a protected marker region (the switch is not decoded; the marker is written), a marker time out of range, markers on a footage item (not written) | | `masks` | masks on a text or shape layer (the path unit there is not verified; not written), a disabled mask or one without a usable path (not written), and per written mask: keys on Mask Opacity or Expansion (no keyed prototype; the static value is written), expressions on mask properties, the lock, mask motion blur other than Same As Layer, Linear feather falloff, RotoBezier, variable-width feather points | -| `text_layers` | per text layer what its document cannot hold: wiggly and expression selectors, animator properties outside the list above (3D rotations, hue / saturation, Character Value, ...), a Tracking Type or Character Range / Alignment other than the default, Path and More Options other than the defaults, Source Text keys (the first key's text is written) and expressions, a manual kerning amount, OpenType features, variable font axes, a font style other than Regular, vertical text, a switched-off range selector (written on), a run without a font name (written in Times New Roman), a fill or stroke opacity (text colours have none), and a value beyond what the document holds (written at the limit): colour channels outside 0..1, font size, tracking, leading, scale, baseline shift, tsume, stroke width, paragraph indents and spacing, box position and size | +| `text_layers` | per text layer what its document cannot hold: wiggly and expression selectors, animator properties outside the list above (3D rotations, hue / saturation, Character Value, ...), a Tracking Type or Character Range / Alignment other than the default, Path and More Options other than the defaults, Source Text keys (the first key's text is written), a manual kerning amount, OpenType features, variable font axes, a font style other than Regular, vertical text, a switched-off range selector (written on), a run without a font name (written in Times New Roman), a fill or stroke opacity (text colours have none), and a value beyond what the document holds (written at the limit): colour channels outside 0..1, font size, tracking, leading, scale, baseline shift, tsume, stroke width, paragraph indents and spacing, box position and size | | `shape_layers` | shape items outside the list above (wiggle, zig zag, pucker, twist, ...; not written), dashes / taper / wave, blend mode and composite order, the path direction, per-corner roundness of a rectangle (the uniform Roundness is written), gradient colour stops other than the default, keys on popups (the first key's value is written), keys and expressions on a path | | `cameras`, `lights` | keys or expressions on camera / light options (static value written), iris and highlight settings; parallel and ambient lights (not written), light falloff, radius, shadows; a camera's Auto-Orient Along Path or Towards Camera (written one-node), a spot light not oriented towards its point of interest (written oriented), a light colour channel outside 0..1 | | `model_layers` | each 3D model layer (skipped) | diff --git a/docs/aep-format.md b/docs/aep-format.md index 01c23facc..082076166 100644 --- a/docs/aep-format.md +++ b/docs/aep-format.md @@ -1,6 +1,6 @@ # After Effects project format (`.aep`) -> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-07 · **Change:** Puppet (`ADBE FreePin3`, 3.7): the property tree from an external scripting listing, **?** until a byte sample exists (#3093); before that: footage: an image sequence without a filename prefix stores only the suffix `Utf8`, and one imported with Force alphabetical order stores its file list in `LIST StVc` / `StVS` (section 3, footage); before that: versions: `svap` is the version that wrote the file, `head` bytes 4 to 7 the version it is written for, and what a Save a Copy As 25.x / 24.x of After Effects 2026 changes (section 1, "Versions"); before that: the stretch numerator is signed (a negative one reverses the layer, 981-983); layer styles (3.12b, 950-980); before that: footage: `sspc@118` of an image sequence is its folder's modification time (860, read-back x1-x3); before that: text: paragraph `/24`, `/35` and style `/68` are neutral for 0 and 1 (732-734, edited in place); before that: keyframes: the ease of mask path keys (599, 700), the spatial speed unit of fractional values (597, 598, 730); before that: text: character and paragraph settings, manual kerning, vertical text, `/nil` is neutral (S17: 538-578, 595, 596); before that: cameras, lights and 3D rotations (S16: the auto Position of a two-node camera is where After Effects renders it, 537; camera and light defaults read back 255, 256; Orientation `otst/otda` 488), Timewarp (529-533), an expression on an effect parameter (534); before that: text: style slot /13 is superscript / subscript (526-528; every scripted text of the corpus is superscript), wrapping box text 517; before that: effects of batch 5 (409-450, 511-516: integer sliders, checkbox defaults in `pard`, arbitrary data of Hue/Saturation and Levels, the parameters of Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow; section 3.7); before that: shape items of batch 5 (451-484, 519: ellipse, fill and stroke opacity, fill rule, group transform, shape layer anchor, trim paths, rectangle roundness, polystar fields, gradient default, nested groups, the defaults of every item; section 3.9); before that: track mattes: mode at `ldta` byte 107 and the matte layer id at byte 160 (cases 402-408, section 3.1); before that: text: automatic leading is verified (400); Exposure (`ADBE Exposure2`, cases 389-397): group start / end parameter types 13 / 14, float slider valid minimum and slider range (section 3.7); before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: the XML form `.aepx` is read into the same chunk tree (section 1, "The XML form"); the `head` save counter is a `u32` at byte 16; text documents for the importer (stroke colour, automatic leading, the font of each run, the undecoded-slot check, the text-layer Anchor Point, the Source Text record); before that: third round of verified field layouts (explicit-default semantics, type-major bezier ease, exact time stretch and in / out points, Time Remapping state, comp pixel aspect / shutter phase / display start, Point default, Layer Control None, dropdown selection, one `mkif` per mask, polystar and radial gradient records, text styles, camera and light options, marker cue data, audio and image-sequence footage) added to the typed view of `effectcraft-aep` · **Target:** Adobe After Effects 2026 +> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-11 · **Change:** text: a Source Text expression is stored like any property's (`tdb4` bytes 119 / 120, `Utf8`; `text-synthetic.aep`, #3170); before that: footage: the Photoshop layer an item shows and its document's size (`opti` tag `8BPS`, section 3.3, issue 3087); before that: Puppet (`ADBE FreePin3`, 3.7): the property tree from an external scripting listing, **?** until a byte sample exists (#3093); before that: footage: an image sequence without a filename prefix stores only the suffix `Utf8`, and one imported with Force alphabetical order stores its file list in `LIST StVc` / `StVS` (section 3, footage); before that: versions: `svap` is the version that wrote the file, `head` bytes 4 to 7 the version it is written for, and what a Save a Copy As 25.x / 24.x of After Effects 2026 changes (section 1, "Versions"); before that: the stretch numerator is signed (a negative one reverses the layer, 981-983); layer styles (3.12b, 950-980); before that: footage: `sspc@118` of an image sequence is its folder's modification time (860, read-back x1-x3); before that: text: paragraph `/24`, `/35` and style `/68` are neutral for 0 and 1 (732-734, edited in place); before that: keyframes: the ease of mask path keys (599, 700), the spatial speed unit of fractional values (597, 598, 730); before that: text: character and paragraph settings, manual kerning, vertical text, `/nil` is neutral (S17: 538-578, 595, 596); before that: cameras, lights and 3D rotations (S16: the auto Position of a two-node camera is where After Effects renders it, 537; camera and light defaults read back 255, 256; Orientation `otst/otda` 488), Timewarp (529-533), an expression on an effect parameter (534); before that: text: style slot /13 is superscript / subscript (526-528; every scripted text of the corpus is superscript), wrapping box text 517; before that: effects of batch 5 (409-450, 511-516: integer sliders, checkbox defaults in `pard`, arbitrary data of Hue/Saturation and Levels, the parameters of Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow; section 3.7); before that: shape items of batch 5 (451-484, 519: ellipse, fill and stroke opacity, fill rule, group transform, shape layer anchor, trim paths, rectangle roundness, polystar fields, gradient default, nested groups, the defaults of every item; section 3.9); before that: track mattes: mode at `ldta` byte 107 and the matte layer id at byte 160 (cases 402-408, section 3.1); before that: text: automatic leading is verified (400); Exposure (`ADBE Exposure2`, cases 389-397): group start / end parameter types 13 / 14, float slider valid minimum and slider range (section 3.7); before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: the XML form `.aepx` is read into the same chunk tree (section 1, "The XML form"); the `head` save counter is a `u32` at byte 16; text documents for the importer (stroke colour, automatic leading, the font of each run, the undecoded-slot check, the text-layer Anchor Point, the Source Text record); before that: third round of verified field layouts (explicit-default semantics, type-major bezier ease, exact time stretch and in / out points, Time Remapping state, comp pixel aspect / shutter phase / display start, Point default, Layer Control None, dropdown selection, one `mkif` per mask, polystar and radial gradient records, text styles, camera and light options, marker cue data, audio and image-sequence footage) added to the typed view of `effectcraft-aep` · **Target:** Adobe After Effects 2026 What `crates/aep` ([`effectcraft-aep`](../crates/aep/README.md)) reads and writes, and the evidence for each statement. Everything here was observed on project files we generated ourselves; no @@ -119,6 +119,11 @@ counterpart (a name that is not 1 to 4 characters) are skipped with their conten with the byte offset. It runs without recursion; the nesting depth (`Limits::max_depth`) and the element count (`Limits::max_chunks`) are bounded, and everything it allocates is bounded by the size of the input. +- There are two readers. `read_aepx` builds the owned tree element by element. The importer, which + only reads, uses a second reader that decodes every body (hex, text, the alias JSON, the + numbers) into one buffer and records the chunks as a flat depth-first list whose bodies are + ranges of that buffer; it indexes that buffer (`TreeIndex`) as it indexes an `.aep`. The tests + hold both readers to the same tree and the same errors. - Tests: `tests/aepx.rs` compares the five fixture pairs chunk by chunk and byte by byte, compares the typed view of both forms, and runs truncated, bit-flipped, deeply nested and oversized inputs; `crates/aep/tests/it/import/aepx.rs` imports both forms of each pair and expects the same @@ -138,7 +143,7 @@ write on their own. | `svap` (4 bytes, the first top-level chunk) is the version of the After Effects that **wrote** the file: `0f 11 0e 02` in all 36 (a down-saved copy keeps the 2026 word) and in all 1 192 other `.aep` files of the local corpus folders. After Effects opens a file without `svap` (`t-nosvap`). | All 36; corpus scan. | | `head` bytes 4 to 7 are the version the file is **written for**: `0f 11 0e 02` (= `svap`) in the 2026 saves, `0f 0b 06 61` in the 25.x copies, `0f 03 06 41` in the 24.x copies, the same word in every family. After Effects refuses a file whose word is zero ("created with an older version [0.0x0]", `u-zhead`). | All 36; `u-zhead`. | | The word is packed (bit 31 first): bits 25-24 and 21-19 the major, 18-15 the minor, 14-11 the bug-fix, 10-8 a stage (6 in all), 7-0 the build; bits 31-26 (`000011`) and 23-22 (`00`) are the same in all samples and not decoded. `0f110e02` = **26.2.1x2**, which is what the saving After Effects reports for itself (`app.version` recorded by the corpus scripts) - this one sample fixes minor, bug-fix and build. The copies read as **25.6.0x97** and **24.6.0x65** (major **V** from three consecutive majors; minor, bug-fix and build of the copies **E**: one other word decides their positions). `effectcraft_aep::AeVersion`, `AepFile::file_version`, `AepFile::saved_by_version`. | Tests `decodes_the_sampled_words`, `the_container_of_a_copy_states_its_version_in_head_and_the_writer_in_svap`. | -| `head` bytes 0 to 1 (a `u16`) step with the major: 97 (`0x61`) in the 2026 saves, 96 in 25.x, 95 in 24.x; bytes 2 to 3 are 5, 9 and 6 (**?**). Bytes 8 to 15 differ between families, not between versions; bytes 16 to 19 are the save counter (one higher in each copy). Nothing is derived from bytes 0 to 3 (`AepFile::head_revision` exposes the `u16`). | All 36. | +| `head` bytes 0 to 1 (a `u16`) step with the major: 97 (`0x61`) in the 2026 saves, 96 in 25.x, 95 in 24.x; bytes 2 to 3 are 5, 9 and 6 (**?**). Bytes 8 to 15 differ between families, not between versions; bytes 16 to 19 are the save counter (one higher in each copy). Nothing is derived from bytes 0 to 3, and the reader does not expose them. | All 36. | | The XMP packet after the container is no guide: its `CreatorTool` is "Adobe After Effects 2026" in the copies too. | All 36. | | **26 against 25.x** (per family, chunk tree aligned by item and layer id): besides `head`, `nhed` / `nnhd` differ in 6 bytes (`nhed` 20-23 and 26-27: `827fa930` / `0015` in the 2026 save, `00000000` / `011b` in both copies; session state, see section 4). Every **solid's** `Pin /CLRS` changes: the 2026 save has `dcui` (1 byte, 1), `apid` all `0xff` and an empty `Utf8`; the copies drop `dcui`, set `apid` to a GUID (`601e9973...`, the same in all) and embed a 935-byte JSON with an ICC profile (`baseColorProfile/colorProfileData`). A **footage** item that the 2026 save stores with an assigned profile (`mcsp` = 1 and a profile JSON) loses `mcsp` and that JSON in the copies, and its `epid` becomes all `0xff`. The rest is save noise: the footage alias `ascendcount_base` (the copies were saved in another folder), `sspc` byte 112 of a footage item (a modification time), the `oEXR` `opti` of an EXR item (the copies hold the display device name `\.\DISPLAY1` as UTF-16 at byte 126 and, in the 24.x copy, a few non-zero bytes from byte 2 102 that look like uninitialised memory; the 2026 save has zeros), the plug-ins' sequence data `sdat` of Lumetri Color (16 instances and the project's `EfdG`), the mask counter `mkif` byte 8. No chunk is added or reordered, and no `pard`, `cdat`, `ldta`, `cdta`, `tdb4`, keyframe, text or shape chunk differs. | All 12 families. | | **25.x against 24.x**: only `head` (bytes 1, 3, 5 and 7), and the session noise in `opti` and `sdat` above. | All 12 families. | @@ -216,12 +221,12 @@ the value from the corpus manifest (test file in parentheses at the end of each API: `LayerHeader` (`is_video_enabled`/`is_enabled`, `is_audio_enabled`, `is_effects_enabled`, `is_auto_oriented`, `light_type`, `blend_mode`, `stretch` / `stretch_den`, `stretch_ratio`, -`stretch_percent`, `in_point`, `out_point`); the same switches on `Layer`; `Layer::track_matte` +`in_point`, `out_point`); the same switches on `Layer`; `Layer::track_matte` (`TrackMatte`, `MatteMode`; test `tests/matte.rs`). `stretch` is now the numerator of the ratio (it was the whole multiplier before 50 % and 150 % were sampled). Tests: `tests/header.rs`, `tests/layer_switches.rs`. The layer name itself is the `Utf8` child of the `Layr` (empty while the name is the source's); the layer then shows the name of its source item -(`Project::layer_display_name`). +(`Project::layer_source(...)` then `Item::display_name`, the rule the importer uses). ### 3.2 Composition header (`Item/cdta`, 204 bytes; the decoder needs the first 176) and renderer @@ -246,9 +251,9 @@ numerator of the ratio (it was the whole multiplier before 50 % and 150 % were s The inert `ADBE Time Remapping` record of every AV layer has `tduM` = the source's duration in seconds: 5 for 5 s compositions (124, 257, 258), 3 and 2 for a 3 s and a 2 s composition (600, 601), 1 for the 1 s WAV (351), 0.1 for the three-frame sequence (353), 0 on solids and stills (000, 126). Every property's keyframe time scale (`tdb4` bytes 12 to 15) follows the composition (23 976 in every property of `L` after 229 and 230). The corpus manifest calls the classic renderer -`ADBE Advanced 3d`; the file stores `ADBE Escher` / `Classic 3D`. API: `CompHeader::frame_rate_exact` -(exact fraction), `duration_frames`, `shutter_phase`, `display_start`, `display_start_seconds`, -`display_start_frame`. Tests: `tests/header.rs`, `tests/layer_switches.rs`. +`ADBE Advanced 3d`; the file stores `ADBE Escher` / `Classic 3D`. API: `CompHeader::{time_base, +frame_rate_fixed, duration_units, shutter_phase, display_start}` (`display_start` is the exact +fraction). Tests: `tests/header.rs`, `tests/layer_switches.rs`. ### 3.3 Items, footage and precomps @@ -263,6 +268,7 @@ property of `L` after 229 and 230). The corpus manifest calls the classic render | footage size | `Pin /sspc` | 30, 34 | u32, u32 | width, height in pixels | 000 (1920 x 1080), 022 (100 x 100 null), 126 (8 x 8), 211, 212, 259 | V | | footage name | `Pin /opti` | 26 (solid), 58 (file) | ASCII, NUL terminated | the item's own `Utf8` name is empty for footage | 000 (`L`), 022 (`Null 1`), 126 (`test8x8.png`), 211 (`S100`), 259 (`Blue`) | V | | **solid colour** | `Pin /opti` | 10, 14, 18, 22 | 4 x f32 | ARGB, 0..1: red [1, 0, 0] is `1, 1, 0, 0`, blue [0, 0, 1] is `1, 0, 0, 1` | 000, 259 | V | +| **Photoshop layer** | `Pin /opti` (tag `8BPS`) | 14; 32, 36; 78, 82, 86, 90; 344 | i32 BE; 2 x u32 **LE**; 4 x i32 **LE**; UTF-8, NUL terminated | an item made from one layer of a `.psd`: the layer record's index in the file (bottom of the stack = 0; -1 is the merged image), the document's two sides (which is the width: **?**, the one sample is square), the layer's bounds top, left, bottom, right in document pixels (all 0 for a layer without pixels, such as an adjustment layer) and the layer's name. The item's size (`sspc` 30 / 34) is the bounds' size with Retain Layer Sizes, else the document's. `Footage::psd_layer` | the project of issue 3087: ten layers, each index, bounds and name equal to the `.psd`'s layer record at that position (`Layer 1` `[9, -346, 1083, 1086]`, item 1432 x 1074; `test` `[749, 800, 764, 848]`, item 48 x 15; 32 and 36 hold 1000, 1000 in every record, the `.psd`'s size) | V | | file path | `Pin /Als2/alas` | key `fullpath` | JSON string | alias JSON of a file footage item | 126 (the fixture holds a neutral path) | V | | precomp linkage | `Layr/ldta` | 40 | u32 | the layer's source id is the id of the precomp's item | 124 | V | @@ -292,7 +298,8 @@ name. A footage layer shows the item's name while it has no name of its own (351 Test: `tests/header.rs` (precomp), `tests/text_markers_footage.rs` and `tests/batch3_objects.rs` (footage), `tests/layer_switches.rs` (solid colour). API: `Footage::{duration, native_frame_rate, -bits_per_pixel, file_size, audio_sample_rate, is_sequence, sequence_frame_count}`. +bits_per_pixel, file_size, audio_sample_rate, is_sequence}`; `Item::sequence_info` (with +`SequenceInfo::frame_count`). ### 3.4 Properties: sparse storage, static values and units @@ -401,8 +408,8 @@ out speed, at 40 out influence**). Scale key 1 of 305 (`n = 3`, in 0 / 33 %, out 15 / 66) is `0,0, .33,.33, 15,15, .66,.66`. Component `i` uses slot `i` of each block (**V** for the block layout and the units, Scale speed in fractions per second and Mask Feather speed in pixels per second; **E** that different values per dimension sit in the slot of their dimension, because -the tool that made the corpus applies one ease to all dimensions; **V** since the exporter wrote three different eases for the three Scale dimensions and After Effects read the same three back, aep-export.md R13). `Keyframe::plain_ease_slots` -returns the doubles as stored, `Keyframe::ease_component(i)` one component's `KeyEase`. Layout of +the tool that made the corpus applies one ease to all dimensions; **V** since the exporter wrote three different eases for the three Scale dimensions and After Effects read the same three back, aep-export.md R13). `Keyframe::ease_component(i)` +returns one component's `KeyEase`, `Keyframe::ease_components` all of them. Layout of the spatial template: `u32` at 8 (1 normally, 0 after the spatial auto-bezier and continuous switches were turned off together, 054), `f64` arc length of the segment to the next key at 16 (100 in 204 key 1, 0 on the last key and right after creation; safe to ignore), **ease at 24 in @@ -427,7 +434,7 @@ in tangent (`n` x `f64`, relative offset from the value) at `56 + 8 n`, out tang | **ease, colour switched to bezier** | spatial n=4 @24..55 | keyframe.set_interpolation bezier on two colour keys stores speed 0 and influence 0 on both sides, and After Effects interpolates them linearly (598 Start Color red to blue: 0.75 / 0.5 / 0.25 red at 0.5 / 1 / 1.5 s); a custom colour ease cannot be set by script | 598 | V | | **ease, mask path** | 64-byte record @24..55 | in speed, in influence, out speed, out influence as in the spatial template: 599 out / in influence 0.75, 700 0.2 / 0.5, speeds 0 (the path at 0.5 s is 5.42 % / 28.0 % of the way). The `f64` at 16 is 1 (093) or 2 (599, 700) on the first key and 0 on the last (not decoded) | 093, 599, 700 | V (influence), ? (speed unit) | -API: `Keyframe::{interpolation_in, interpolation_out, kind, value, ease, ease_component, ease_components, plain_ease_slots, color_distance, spatial}`; +API: `Keyframe::{interpolation_in, interpolation_out, kind, value, ease, ease_component, ease_components, spatial}` (the `f64` at byte 16 of a colour key is `SpatialKey::segment_length`); `Property::keys()` hands the property's template to its records. Marker record (16 bytes): time, `03 03` hold bytes, 8 stale bytes. Mask path record (64 bytes): the common fields and the ease at byte 24 (599, 700). Test: `tests/values.rs`, `tests/keyframes.rs`. @@ -604,6 +611,64 @@ suffix is a parameter id, not the `parT` position (Curves -0002 Channel before - -0005 Quality before -0004). API: `CurvesData`, `MeshData`, `curve_lut`. [V; details: corpus FORMAT S] +**Lumetri Color** (`ADBE Lumetri`, 1200: 63 compositions in After Effects 2026, 32 bpc, working space +None; by script: defaults, all set, one per section, valid minima and maxima, every popup item the script +can set, checkboxes toggled, keys, an expression; from presets made by hand, `-58` ... `-62`: an Input +LUT, a Look, the RGB curve, Hue vs Sat, the colour wheels): 131 `parT` entries. The `-NNNN` suffix is a +parameter id, not the position (`-0127` ... `-0130` sit between `-0005` and `-0026`), so the importer +keys by suffix. Float sliders (type 10) store the value After Effects shows; the valid ranges (`pard` +104 / 108) are wider than the slider's (112 / 116): Temperature / Tint -300..300 (slider -100..100), +Saturation 0..300 (0..200), Exposure -7..7 (-5..5), Contrast, Highlights, Shadows, Whites, Blacks, +HDR Specular, Tint Balance -150..150 (-100..100), Faded Film 0..150 (0..100), HDR White 100..1000, +HDR Range 100..10000, Refine Blur 0..1000 (0..30), Vignette Amount -5..5 (-3..3); the read-backs of +`-19-min` / `-20-max` equal the stored numbers. **Hidden controls are recorded in the default state** +(`tdsb` 3) and still hold the value After Effects reads back: High Dynamic Range `-0062` (1.0), +HDR Specular `-0064` (22), the second Look popup `-0128` (26.16 as set, read back 26), Legacy Tone And +Saturation `-0097` (its `pard` default reads off as the script saved the project and on once the +project was opened and saved again, `versions/1200-lumetri-26`, `-25x`, `-24x`; a fresh instance reads +back off), Auto Color `-0124`, Face Detection `-0102`, the Auto Intensity `-0123` +(`EffectParam::recorded_value`). Each section has an Active checkbox (blank `pard` name; `-0003` Basic +Correction, `-0023` Creative, `-0037` Curves, `-0118` RGB Curves, `-0104` / `-0105` / `-0107` / `-0109` / +`-0111` the hue / saturation curves, `-0046` Color Wheels, `-0068` HSL Secondary, `-0050` Vignette, on +by default); off, the section does nothing (`-03-sec-basic-correction`, `-17-sec-vignette` render as +`-00-defaults`). Arbitrary data (type 11, a record and `aRbs` in every instance; empty is `fe fe`): +`-0001` an XML copy of the grade written only when the Look changes (not kept current: `-01-set-all` +holds defaults for controls set after the Look; it names the LUT file, `Fuji F125 Kodak 2393 (by +Adobe).cube`); `-0096` the Look's name, UTF-16LE (`SL BLUE ICE`, `Fuji F125 Kodak 2393 (by Adobe)` in +`-59-preset-look-lut`; the popup `-0025` itself has no record, `-0128` can disagree with it: +`-07-sec-creative` names item 20 while `-0128` holds 23), `-0095` likewise for the Input LUT +(`ALEXA_Default_LogC2Rec709`, `-58-preset-input-lut`; the popups `-0005` / `-0127` keep no record, +`-0004`, `-0024` and the assets `-0125` / `-0126` stay `fe fe`: the LUTs are After Effects' own files, +named, not embedded); `-0129` the Look colour space's 16-byte id (the GUID `LookColorSpace` of `-0001` +in Windows layout: the first 4, 2 and 2 bytes little-endian; zero for None; the Color Space popup +`-0130` writes no record of its own, `-22-pop38-1` ... `-53-pop38-33`); +`-0032` Split Toning (32 zero bytes); `-0039` the four RGB curves and `-0042`, `-0106`, `-0108`, +`-0110`, `-0112` the hue / saturation curves, 520 bytes each: `i32` type (3 for the hue curves, 2 for +Luma vs Sat and Sat vs Sat, 0 for an RGB curve), `u32` point count, then little-endian `f64` pairs x, y +sorted by x (bytes after the counted points are stale). RGB: x and y 0..1, (0, 0), (1, 1) by default; +the first of the four is the master curve (`-60-preset-curves-rgb`: (0.491, 0.787) lifts R, G and B +alike, and the render follows a natural cubic spline through the points within 0.003); the order of +the other three is H (the `-0001` XML names `ToneCurvesLuma`, `Red`, `Green`, `Blue`; no sample moves +one). Hue / saturation: x the hue (0 red, 1/3 green) or the luma / saturation 0..1, y the offset from +neutral 0; a fresh curve has no points, the selector colours `-0113` ... `-0117` add three at y = 0, +the hue picked +- 0.0556. Hue vs Sat's y scales the saturation by 1 + y in linear light +(`-61-preset-curves-huesat`: (0.0155, 0.97) takes pure red's linear saturation to 1.96 times, (0.263, +-0.03) and (0.765, -0.02) green, cyan and blue to 0.97); the other four curves' scale is H. `-0047` +Color Wheels, 4 `f64` triples: hue in degrees (0 red, 120 green), brightness slider 0..1 (0.5 +neutral), distance from the centre 0..1; 0, 0.5, 0 each by default; Shadows, Midtones, Highlights, then +a fourth wheel of unknown role (`-62-preset-wheels`: Shadows 229.1, 0.370, 0.547 towards blue with the +slider down; Midtones 0.1, 0.5, 0.395 towards red; Highlights 93.4, 0.727, 0.319 towards yellow-green +with the slider up; the fourth at its default); `-0085` the HSL correction wheels (8 triples, the same +default; layout H); `-0073` the HSL key, 40 bytes: a flag byte (1 once a colour was picked), then hue, +saturation, lightness, each centre, range, tolerance (`f32` LE, fractions) and an enabled byte (fresh: +0.5, 0, 0, on; picking RGB 0.02 0.55 0.85 gives hue centre 0.5605 = that colour's hue and lightness +centre 0.435 = its HSL lightness, range 0.05, tolerance 0.1: `-01-set-all`; whether range is the half +or full width is H); `-0122` the Auto Tone analysis (UTF-16 JSON). The project saved as 25.x and 24.x +copies (`versions/1200-lumetri-25x`, `-24x`: the 58 scripted compositions, saved by After Effects +2026) stores every Lumetri definition, valid range and value as the 26 save does. [V for the sliders, +checkboxes, popups, the `tdsb` 3 values, the master RGB curve, Hue vs Sat, the colour wheels and the +LUT names; H where marked] + **Cryptomatte plug-in** (match name `Cryptomatte`, the free Cryptomatte plug-in, 2.6.6 in our sample; read from 88 instances in one production project, not made by us, and from our own After Effects 2026 sample 1400 (corpus `aep/cryptomatte.aep`: a fresh instance, Selection Mode on, each @@ -706,7 +771,7 @@ pins are placed by hand or by a preset. 328), so mode and inverted are reported per mask. `mkif` byte 8 is the running mask id counter of the **layer** in the session (1, 2 for the two masks of 328; 329 then gets 3; undo does not reset it; a new layer starts at 1 again): exposed as `Mask::counter`, a session counter and not a stable id. API: -`Mask::{mode, inverted, counter}`, `Project::mask_vertices_px` (vertices in layer pixels), `Layer::mask_atoms` (`MaskAtom`: the mask with its atom group, its path property and its shapes by position, for the keys and expressions of the mask properties). Tests: +`Mask::{mode, inverted, counter}`, `ShapePath::vertices(size)` (vertices in layer pixels: `size` is the size of the layer's source, `Project::layer_source_size`; a text or shape layer has no source size and its mask boxes are already pixels, so the importer passes 1 x 1), `Layer::mask_atoms` (`MaskAtom`: the mask with its atom group, its path property and its shapes by position, for the keys and expressions of the mask properties). Tests: `tests/masks.rs` (with a synthetic check of the handle order), `tests/batch3_objects.rs`. ### 3.9 Shape layers (`tdgp[ADBE Root Vectors Group]`) @@ -792,7 +857,7 @@ verified paths from the top level (keys `/98 /0 /1`; `RUN` = `/1/1[0]/0`): | **stroke colour** | `.../6/54/0/1` = `[A R G B]`, the fill's layout; white in the untouched samples, red `[1 1 0 0]` in 574, blue in 595. The stroke switch, width and order are slots `/57`, `/63`, `/58` (see "character settings") | 110, 113, 574, 595 | V | | **automatic leading** | style slot `.../6/4`: `true` = automatic leading (lines advance by 1.2 x the size; `/5` is then not used), `false` = `/5` is the leading. **243: `/4` false, `/5` 98, the second line's baseline is stored 98 px below the first (`/1/1[0]/1/2[0]/6[0]/6[0]/6[0]/6[1]/0/0[1]`)**. **400 (Auto Leading switched on for `A` CR `B`, size 100, leading 98; against its unedited save 399): the run style changes in `/4` (false -> true) and `/5` (98 -> 0.01) only, the stored second baseline moves from 98 to 120 px below the first (1.2 x 100), and After Effects measures the same 120 px (`text.measure`)**; the rest of the diff is save noise (`head`, the comp's modification time, the text's `btgu/pgui`). That `/5` holds 0.01 under automatic leading is seen in 400 only | 243, 339 (`/5`); 400 (`/4`); test `automatic_leading_is_slot_4` | V | | **undecoded slots** | the run style and paragraph dictionaries of an untouched document (244; 110 stores the same bytes) are the reference: a slot that is not decoded and differs from it is listed (`TextDocument::undecoded_style_fields`, `undecoded_paragraph_fields`); a slot the reference lacks is listed only when it is not 0, `false`, empty or `/nil` (a neutral value is taken to carry nothing, so it is not reported as a loss; `/nil` is how 244 stores its paragraph slots `/27`, `/28` and `/36`). Decoded style slots: `/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /11 /12 /13 /18 /35 /36 /52 /53 /54 /56 /57 /58 /62 /63 /70`; paragraph slots: `/0 /1 /2 /3 /4 /5 /8 /9 /21 /29 /33`. Not found by any scriptable setting (none of the `text.set_style` attributes tried in S17 changes them): style `/20 /38 /78 /87 /88`, paragraph `/31 /32`. **Paragraph `/24`, `/35` and style `/68`** (1, 1 and 0 in a scripted text) were edited in place in the wrapping box text 517 (732: `/24` 0, 733: `/35` 0, 734: `/68` 1, same length); After Effects opens each without a dialog, keeps the value on a re-save and composes the same four lines, baselines and bounds (`text.measure`, `layer.bounds`; 734 renders like 517), so 0 and 1 are not listed (`NEUTRAL_PARAGRAPH_VALUES`, `NEUTRAL_STYLE_VALUES`; their meaning stays unknown, Latin text only). **Paragraph `/34`** is 0 in a scripted text and 7 after Reset Paragraph (727: the only slot that changes); a justified 600 px box text of 88 characters composes the same four lines, baselines and bounds with both values (S18), so 0 and 7 are not listed (`NEUTRAL_PARAGRAPH_VALUES`), other values are | every text sample lists none; 725, 727 | V (reference), E (/34) | -| **Source Text record** | the `tdbs` in `btds[ADBE Text Document]` (next to the `btdk`) has no keys and no expression in every sample; Source Text keyframes (presumably one `btdk` per key) and expressions were not sampled, the reader only reports them (`Layer::source_text_property`, `Layer::text_document_count`) | 110-114, 243-248, 334-340 | E | +| **Source Text record** | the `tdbs` in `btds[ADBE Text Document]` (next to the `btdk`) has no keys and no expression in every corpus sample. A Source Text expression is stored as on any other property: its text in the `tdbs`'s `Utf8`, `tdb4` byte 120 = 1, byte 119 = 0 when enabled and 1 when disabled (`tests/fixtures/text-synthetic.aep`: `text.sourceText + "!"` enabled, `"X"` disabled; the `btdk` keeps the typed text). Source Text keyframes (presumably one `btdk` per key) were not sampled, the reader only reports them (`Layer::source_text_property`, `Layer::text_document_count`) | 110-114, 243-248, 334-340, text-synthetic | E; expression V | API: `TextDocument` (`paragraphs`, `style_runs`, `text_box`, `paragraph_texts`, `font_of`, `incomplete_runs`, `undecoded_style_fields`, `undecoded_paragraph_fields`, `kerning`, @@ -834,7 +899,7 @@ groups as on other layers, with these conventions: | What | Path | Stored | Evidence | Conf. | |---|---|---|---|---| -| camera Position, default (auto state) | `ADBE Transform Group/ADBE Position`, `tdb4` 76 and 77 = 1 although no expression exists | `[0, 0, -2666.67]` (x and y are 0 whatever the composition size; z = -zoom). A **one-node** camera reports `[0, 0, -2666.67]` (251). **Correction (S16): a reopened two-node camera reports `[0, 0, -2666.67]` too (256 read back), and After Effects renders it there**: the default two-node camera of 537 above the 3D solid of 486 renders from `[0, 0, -2666.67]` towards the point of interest, not like the composition's default view (537 differs from 486 in 24 % of the grid points, and EffectCraft's import with the stored position matches 537 but for 4 edge points). The `[960, 540, ...]` of the batch-3 notes was not reproduced (535 and 537, created by script, read back the stored value too); `CameraPosition::reported` keeps the old rule and is marked as contradicted | 256, 251, 537 | V | +| camera Position, default (auto state) | `ADBE Transform Group/ADBE Position`, `tdb4` 76 and 77 = 1 although no expression exists | `[0, 0, -2666.67]` (x and y are 0 whatever the composition size; z = -zoom). A **one-node** camera reports `[0, 0, -2666.67]` (251). **Correction (S16): a reopened two-node camera reports `[0, 0, -2666.67]` too (256 read back), and After Effects renders it there**: the default two-node camera of 537 above the 3D solid of 486 renders from `[0, 0, -2666.67]` towards the point of interest, not like the composition's default view (537 differs from 486 in 24 % of the grid points, and EffectCraft's import with the stored position matches 537 but for 4 edge points). The `[960, 540, ...]` of the batch-3 notes was not reproduced (535 and 537, created by script, read back the stored value too); the reader keeps only the stored value (`CameraPosition::stored`) | 256, 251, 537 | V | | camera Position, set by the user | same, `tdb4` 76 and 77 = 0 | the absolute value `[100, 200, -1000]` | 253 | V | | camera point of interest | `ADBE Anchor Point` | `[960, 540, 0]` in **pixels** (not a fraction); the record's `tdsb` is 1 on a two-node and 3 on the one-node camera (not asserted) | 251, 253, 256 | V (value), E (`tdsb`) | | light Position, default | same as the camera | `[0, 0, -1333.33]`, as After Effects reports it (255 read back); the point of interest `[960, 540, 0]` | 254, 255 | V | @@ -850,10 +915,8 @@ groups as on other layers, with these conventions: A point light made by converting a spot light has a different property set (`Orientation`, `Rotate X/Y/Z`, `Cone Angle`, `Cone Feather 2` in the default state) that a spot light lacks (252); whether that -is an artefact of the conversion is untested. API: `Layer::camera_position`, -`CameraPosition::reported`, `Layer::camera_point_of_interest`, `Layer::light_intensity`, -`Layer::{camera_zoom, camera_depth_of_field, camera_focus_distance, camera_aperture, light_color, -light_cone_angle, light_cone_feather}`, `DEFAULT_CAMERA_ZOOM` (2 666.67 is verified for the 1920 px +is an artefact of the conversion is untested. API: `Layer::camera_position`; the camera and light +options are read by the importer's table (`import/camera.rs`); `DEFAULT_CAMERA_ZOOM` (2 666.67 is verified for the 1920 px composition only), `Layer::orientation` (`Orientation`; test `orientation_is_otst_otda` in `tests/batch5.rs`). Render checks against After Effects (20 px grid, 5 184 points): 486 1 point off, 535 2, 536 0, 537 4 (anti-aliased edges); **488 (Orientation `[10, 20, 30]`): 272 points diff --git a/docs/aep-import.md b/docs/aep-import.md index 9e88b7855..514646ecc 100644 --- a/docs/aep-import.md +++ b/docs/aep-import.md @@ -1,9 +1,16 @@ # After Effects project import -> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-07 · **Change:** After Effects' Puppet arrives as EffectCraft's Puppet without its meshes and pins, which are reported mesh by mesh (#3093; the storage is not sampled yet, aep-format 3.7); before that: the report names the After Effects version a file is written for and the one that wrote it (`aeVersion`); copies saved as 25.x and 24.x import like the 2026 save; before that: Curves and Mesh Warp are imported (961-965, 985; FORMAT S of the corpus); before that: a shape item's eye switch is imported (bit 0 of its group flags, 986 / 987: a switched-off Fill under Trim Paths is no longer drawn, render equal to After Effects), and a layer style's (switch group `tdsb` 0, 988); before that: a negative time stretch (Time Reverse Layer) is imported: in and out point in order, the layer plays backwards (981-983: render equal to After Effects at 7 points); before that: After Effects' None on Compound Blur, Displacement Map and Set Matte arrives as an explicit None (`NO_LAYER`), not as the owning layer; before that: layer styles are imported (the nine styles and Blending Options, 951-979; Global Light becomes the composition's); before that: the 27 effects of round 3 are in the table (`effectcraft_aep::effect_map`); before that: a layer of a clip, sequence or sound ends at its source's end (After Effects ends it there when it opens a project, read-back test f1), unless it is remapped or looped or runs less than a frame past; before that: keys of shape colours are read (the reader took their 152-byte spatial records for plain ones: `tdb4` byte 7 is 0 there, 854); before that: the effect table moved to `effectcraft_aep::effect_map`; point defaults in percent and a popup default slot of 0 (After Effects' save of a project it opened, e1 / e2 re-saves) are read; before that: the justification of right-to-left paragraphs is read relative to the direction (t7); before that: 24 more layer blend modes (906, 907); before that: 27 more effects are imported (batch 6: 903-905), with layer parameters; before that: a Rec. 709 working space also sets the output space (After Effects renders in the working space, 902); before that: a mask whose path was never set arrives as the source rectangle (900, 901); before that: Offset Paths' Copy Offset arrives as EffectCraft's (After Effects' value minus 1, 828); before that: project settings, image sequences, conformed clips and effect instance names are imported, next to the mattes, shapes, text settings and animators, effects, markers, time remapping, cameras and lights); before that: parallel and ambient lights (701, 702) and keyed Orientation (703) are imported; before that: masks on text and shape layers (705, 706: AE vs EffectCraft render 12 and 0 of 5184 points, glyph edges) and Merge Paths, Offset Paths, Repeater (707-710, 731; merge renders equal but for edges; Repeater copy opacity and Offset Paths' outline differ in EffectCraft's renderer) are imported; before that: keyframes: the ease of Anchor Point, effect point, effect colour and mask path keys is imported (597-599, 700, 730: within 0.5 px of After Effects' read-backs); before that: effects: Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker and Transform are imported (711-723; render checks: CC Radial Blur and Channel Blur equal, Directional Blur within 5/255; Venetian Blinds, Radial Blur, Black & White (8/255), Tritone (19/255), Luma Key, Matte Choker and Transform differ in EffectCraft's renderer, see STATUS); before that: text: character and paragraph settings (stroke, scales, baseline shift, kerning, ligatures, tsume, indents, spacing, justified text, composer, direction, vertical text; 538-578) and text animators with range selectors (579-594, 114) are imported; render checks 594 (keyed range selector at 0 / 0.5 / 1 / 1.5 / 2.5 s), 595, 596 equal but for glyph edges; before that: cameras, spot and point lights, X / Y Rotation and Orientation, Timewarp and expressions on effect parameters are imported (render checks 486, 535-537 equal but for edges; 488 differs by 5.25 %, cause in EffectCraft's renderer not isolated); before that: text: superscript and subscript are imported (526-528); before that: effects: Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow, Fill, Hue/Saturation, Levels and five expression controls are imported, popups are mapped by item name, checkbox defaults come from the file; before that: shape layers are imported (groups, paths, paints, trim paths, round corners; 451-484, 519 added); before that: markers and Time Remapping are imported (509, 510 added); before that: track mattes are imported (402-408); before that: text: automatic leading is verified (400); effects: Exposure is imported (Channels, all twelve sliders, Bypass, keys), next to Tint and Gradient Ramp; before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: effects: Tint and Gradient Ramp are imported (all parameters and keys), other effects stay reported; `.aepx` is accepted; text layers are imported (Source Text with style runs, paragraphs and box text; fonts by PostScript name, missing fonts listed); layer styles are reported; before that: layer masks · **Target:** Adobe After Effects 2026 +> **Last reviewed:** 2026-11-05 · **Last updated:** 2026-11-11 · **Change:** a text animator's Stroke Opacity and a text layer's Source Text expression are imported (#3170); before that: an effect without a counterpart arrives as a placeholder with its parameters, values, keys and expressions; it renders nothing and is still reported (#3087); before that: an item made from one layer of a Photoshop file shows that layer (#3087); before that: the losses prompt shows every detail line and how many have none (`unlisted`); unmapped effects are listed most used first (#3365); a shape property recorded in the default state is no loss; an expression on Orientation is imported with its value or keys (#3087); before that: dropped projects and projects picked in File ▸ Import ▸ File… import as projects, several asked about one at a time; `file.import` hands them to `file.importAep`; the menu entries are named Adobe After Effects Project… (#3103); before that: After Effects' Puppet arrives as EffectCraft's Puppet without its meshes and pins, which are reported mesh by mesh (#3093; the storage is not sampled yet, aep-format 3.7); before that: Lumetri Color's master RGB curve, Hue vs Sat curve and colour wheels are imported (1200 presets made by hand); before that: Lumetri Color is imported (1200: every slider, checkbox and popup with a counterpart, by match name, also from records in the default state; switched-off sections, LUTs, looks, undecoded curves and wheels are reported); before that: the report names the After Effects version a file is written for and the one that wrote it (`aeVersion`); copies saved as 25.x and 24.x import like the 2026 save; before that: Curves and Mesh Warp are imported (961-965, 985; FORMAT S of the corpus); before that: a shape item's eye switch is imported (bit 0 of its group flags, 986 / 987: a switched-off Fill under Trim Paths is no longer drawn, render equal to After Effects), and a layer style's (switch group `tdsb` 0, 988); before that: a negative time stretch (Time Reverse Layer) is imported: in and out point in order, the layer plays backwards (981-983: render equal to After Effects at 7 points); before that: After Effects' None on Compound Blur, Displacement Map and Set Matte arrives as an explicit None (`NO_LAYER`), not as the owning layer; before that: layer styles are imported (the nine styles and Blending Options, 951-979; Global Light becomes the composition's); before that: the 27 effects of round 3 are in the table (`effectcraft_aep::effect_map`); before that: a layer of a clip, sequence or sound ends at its source's end (After Effects ends it there when it opens a project, read-back test f1), unless it is remapped or looped or runs less than a frame past; before that: keys of shape colours are read (the reader took their 152-byte spatial records for plain ones: `tdb4` byte 7 is 0 there, 854); before that: the effect table moved to `effectcraft_aep::effect_map`; point defaults in percent and a popup default slot of 0 (After Effects' save of a project it opened, e1 / e2 re-saves) are read; before that: the justification of right-to-left paragraphs is read relative to the direction (t7); before that: 24 more layer blend modes (906, 907); before that: 27 more effects are imported (batch 6: 903-905), with layer parameters; before that: a Rec. 709 working space also sets the output space (After Effects renders in the working space, 902); before that: a mask whose path was never set arrives as the source rectangle (900, 901); before that: Offset Paths' Copy Offset arrives as EffectCraft's (After Effects' value minus 1, 828); before that: project settings, image sequences, conformed clips and effect instance names are imported, next to the mattes, shapes, text settings and animators, effects, markers, time remapping, cameras and lights); before that: parallel and ambient lights (701, 702) and keyed Orientation (703) are imported; before that: masks on text and shape layers (705, 706: AE vs EffectCraft render 12 and 0 of 5184 points, glyph edges) and Merge Paths, Offset Paths, Repeater (707-710, 731; merge renders equal but for edges; Repeater copy opacity and Offset Paths' outline differ in EffectCraft's renderer) are imported; before that: keyframes: the ease of Anchor Point, effect point, effect colour and mask path keys is imported (597-599, 700, 730: within 0.5 px of After Effects' read-backs); before that: effects: Directional Blur, Radial Blur, CC Radial Blur, Venetian Blinds, Channel Blur, Black & White, Tritone, Luma Key, Simple Choker, Matte Choker and Transform are imported (711-723; render checks: CC Radial Blur and Channel Blur equal, Directional Blur within 5/255; Venetian Blinds, Radial Blur, Black & White (8/255), Tritone (19/255), Luma Key, Matte Choker and Transform differ in EffectCraft's renderer, see STATUS); before that: text: character and paragraph settings (stroke, scales, baseline shift, kerning, ligatures, tsume, indents, spacing, justified text, composer, direction, vertical text; 538-578) and text animators with range selectors (579-594, 114) are imported; render checks 594 (keyed range selector at 0 / 0.5 / 1 / 1.5 / 2.5 s), 595, 596 equal but for glyph edges; before that: cameras, spot and point lights, X / Y Rotation and Orientation, Timewarp and expressions on effect parameters are imported (render checks 486, 535-537 equal but for edges; 488 differs by 5.25 %, cause in EffectCraft's renderer not isolated); before that: text: superscript and subscript are imported (526-528); before that: effects: Invert, Fast Box Blur, Gaussian Blur, Drop Shadow, Glow, Fill, Hue/Saturation, Levels and five expression controls are imported, popups are mapped by item name, checkbox defaults come from the file; before that: shape layers are imported (groups, paths, paints, trim paths, round corners; 451-484, 519 added); before that: markers and Time Remapping are imported (509, 510 added); before that: track mattes are imported (402-408); before that: text: automatic leading is verified (400); effects: Exposure is imported (Channels, all twelve sliders, Bypass, keys), next to Tint and Gradient Ramp; before that: text: U+0003 is a paragraph break (386, 387); before that: a stored text Anchor Point is pixels (385, now V); before that: effects: Tint and Gradient Ramp are imported (all parameters and keys), other effects stay reported; `.aepx` is accepted; text layers are imported (Source Text with style runs, paragraphs and box text; fonts by PostScript name, missing fonts listed); layer styles are reported; before that: layer masks · **Target:** Adobe After Effects 2026 -File ▸ Import ▸ After Effects Project… (`file.importAep`) reads an `.aep` or its XML form `.aepx` +File ▸ Import ▸ Adobe After Effects Project… (`file.importAep`) reads an `.aep` or its XML form `.aepx` and adds its items to the open project, in a folder named after the file (as After Effects does when it imports a project). +A project dropped on the window, or picked in an Import File dialog (File ▸ Import ▸ File… or +Multiple Files…, the Home screen, the Project panel), imports the same way. Several are asked +about one at a time: while the losses prompt shows, the others wait, and Cancel skips only the +one it asks about. The other files of the drop or pick import as footage, into the Project panel +only (not into a composition, as the project opens its own); while the prompt shows, a Photoshop file or image sequence imports with its defaults instead of +opening its own dialog. Agents giving an `.aep` to `file.import` get the refusal when parts would +be lost (`file.importAep` takes `allowLosses` and `dryRun`). It is deliberately narrow: what it brings over is exact and tested, everything else is **reported** and never dropped silently. Wider coverage grows with the reader's verified field layouts ([aep-format.md](aep-format.md)); this page lists what exists today. @@ -52,18 +59,18 @@ refused losses) leaves the project unchanged. outside the sampled ones keeps the current value and is reported as `project settings`. After Effects renders in the working space (902: in a Rec. 709 Gamma 2.4 project an sRGB PNG step of 128 comes out as 135 and a grey 0.5 solid as 128, in the Render Queue output and in the captures), while EffectCraft converts its output to `output_space` (sRGB when unset): a Rec. 709 working space therefore also sets the output space to Rec. 709 (902 within 1 level of After Effects, was up to 13). They come in `ImportResult::settings`; the library never changes the project's own settings, `file.importAep` applies them when the project has no compositions yet (or with `applySettings: true`) | | Composition | name, size, exact frame rate, duration, work area, background colour (8 bit), pixel aspect, composition motion blur switch, shutter angle and phase (as stored), display start time, Classic/Advanced 3D renderer | | Layers | solid, null, adjustment, footage, precomposition and **text** layers: name (the source's name when the layer has none; for a text layer the first 40 characters of its first line, as EffectCraft names a text layer after its text), in/out/start and time stretch (exact ratios, e.g. 50 % and 150 %); a layer of a clip, sequence or sound ends at its source's end as After Effects ends it (unless remapped, looped or less than a frame past; f1), enabled/audio/solo/shy/locked/effects/motion blur/collapse/3D/guide switches, draft quality, label, blend mode (Normal, Multiply, Add, Screen, Overlay, Difference; Soft Light, Hard Light, Darken, Lighten, Exclusion, Color Dodge, Color Burn by name, 906; Classic Difference, Hue, Saturation, Color, Luminosity, the Stencil and Silhouette modes, Classic Color Dodge / Burn, Linear Dodge / Burn, Linear, Vivid and Pin Light, Hard Mix by render, 907: each code rendered by After Effects matched exactly one EffectCraft mode; 906 renders within 1/255 of After Effects), parent, **track matte** (alpha, alpha inverted, luma, luma inverted; the matte layer anywhere in the composition, also below its target; 402-408) | -| Transform | Anchor Point, Position, Scale, Rotation, Opacity, **X and Y Rotation** (486, 487) and **Orientation**, static (488) or keyed (703: key values, times, linear and hold sides; a bezier side becomes linear and is reported; After Effects slerps between the keys, EffectCraft interpolates each angle, so in-between values differ, e.g. 703 at 1 s `[6.26, 9.35, 15.22]` against `[5, 10, 15]`; an expression-driven orientation is reported): the stored value, or the default when the file stores none; keyframes with time, linear/bezier/hold on each side, temporal ease (speed and influence, per dimension for Scale), automatic or manual spatial tangents, roving and key labels | -| Expressions | on the transform and mask properties and on effect, camera and light parameters (534): source text and enable switch | +| Transform | Anchor Point, Position, Scale, Rotation, Opacity, **X and Y Rotation** (486, 487) and **Orientation**, static (488) or keyed (703: key values, times, linear and hold sides; a bezier side becomes linear and is reported; After Effects slerps between the keys, EffectCraft interpolates each angle, so in-between values differ, e.g. 703 at 1 s `[6.26, 9.35, 15.22]` against `[5, 10, 15]`), with its expression as on any property: the stored value, or the default when the file stores none; keyframes with time, linear/bezier/hold on each side, temporal ease (speed and influence, per dimension for Scale), automatic or manual spatial tangents, roving and key labels | +| Expressions | on the transform and mask properties, on effect, camera and light parameters (534) and on a text layer's Source Text (`text-synthetic.aep`, #3170): source text and enable switch | | Cameras and lights | **cameras** (one-node and two-node, from `ldta` byte 38 bit `0x40`): point of interest, Position as stored (also the auto Position `[0, 0, -zoom]`, where After Effects renders it, 537), orientation and rotations, Zoom (absent: `-z` of the auto Position, else the 50 mm default), Depth of Field, Focus Distance (absent: the zoom), Aperture (absent: 25.309 at 1920 px, read back from 256; scaled with the width, which is not sampled, E); **spot, point, parallel and ambient lights** (light type 1, 2, 0, 3: 701, 702): point of interest (not for point and ambient lights), Position, Intensity, Color, Cone Angle, Cone Feather, Casts Shadows; keys and expressions as for the transform; 251-256, 341-345, 535-537, 701, 702. Environment lights are reported | | Text | the Source Text of a text layer: the text (paragraphs separated by CR; a hard line break is a CR too; U+0003 is laid out by After Effects exactly like a CR (386 against 387) and becomes a CR), per style run the font (PostScript name), size, fill and stroke colour, whether fill and stroke are drawn, stroke width, stroke over fill (one switch for the text), tracking, leading (automatic or explicit), faux bold, faux italic, all caps, small caps, superscript and subscript (526-528), horizontal and vertical scale, baseline shift, automatic kerning (metrics, optical, none), ligatures, tsume (538-578); per paragraph the justification (left, centre, right and the four justified variants), first-line / start / end indent, space before / after, direction, composer, hanging punctuation (in a right-to-left paragraph the stored justification is relative to the direction: After Effects lays 0 and 3 out flush right and 1 and 4 flush left, read-back test t7, so they arrive as right / left); point text or box text (box size and top-left in layer pixels); vertical right-to-left text (575) | -| Text animators | every animator with its name and its range selectors (Start, End, Offset, or the Index range when Units is Index; Units, Based On, Mode, Amount, Shape, Smoothness, Ease High / Low, Randomize Order, Random Seed) and the properties Anchor Point, Position, Scale, Skew, Skew Axis, Rotation, Opacity, Fill Color, Stroke Color, Stroke Width, Tracking Amount, Line Spacing, Blur, Character Offset and Character Range, with keys and expressions (579-594, 114, 728). Values are stored as After Effects shows them; popups are 1-based in the file | +| Text animators | every animator with its name and its range selectors (Start, End, Offset, or the Index range when Units is Index; Units, Based On, Mode, Amount, Shape, Smoothness, Ease High / Low, Randomize Order, Random Seed) and the properties Anchor Point, Position, Scale, Skew, Skew Axis, Rotation, Opacity, Fill Color, Stroke Color, Stroke Opacity (`text-synthetic.aep`, #3170), Stroke Width, Tracking Amount, Line Spacing, Blur, Character Offset and Character Range, with keys and expressions (579-594, 114, 728). Values are stored as After Effects shows them; popups are 1-based in the file | | Masks | every mask of an imported layer, in stack order (on text and shape layers, which have no source, the stored mask box is in layer pixels: 705, 706): name, path (vertices and tangents in layer pixels, open or closed), mode (None, Add, Subtract, Intersect, Lighten, Darken, Difference), inverted, feather (x, y in pixels), opacity (percent), expansion (pixels); path keyframes (time, linear or hold sides, roving, label), feather / opacity / expansion keyframes with ease | -| Effects | **Curves** (961-964: the five curves' points from the arbitrary data as EffectCraft's `x,y` points in 0..1; render within 6 levels: EffectCraft interpolates monotone, After Effects stores a natural spline) and **Mesh Warp** (965, 985: Rows, Columns, Quality and the Distortion Mesh as per-vertex offsets in layer pixels; Bezier handles moved on their own are kept as eight extra numbers of the vertex for the export and reported, EffectCraft draws the mesh bilinear; the header is rows + 1, columns + 1; render 985: 1.4 % of pixels > 2 levels around the moved vertex), **Tint**, **Gradient Ramp**, **Exposure**, **Invert**, **Fast Box Blur**, **Gaussian Blur**, **Drop Shadow**, **Glow**, **Fill**, **Hue/Saturation**, **Levels**, **Timewarp** (Method, Adjust Time By, Speed with keys, Source Frame; 529-533), **Directional Blur**, **Radial Blur**, **CC Radial Blur**, **Venetian Blinds**, **Channel Blur**, **Black & White**, **Tritone**, **Luma Key**, **Simple Choker**, **Matte Choker**, **Transform** (all parameters; 711-723) and the expression controls **Slider**, **Angle**, **Checkbox**, **Color** and **Point Control** (table below) on any imported layer (also text and shape layers and layers with masks), in stack order, with the instance name, every parameter (static value or default) and its keyframes; also the second and later instances of a type, whose parameter definitions are only in the project's shared definition list. **Puppet** arrives in its place with its name and fx switch but without meshes and pins (a pin's rest place lies in the mesh data, not decoded yet); per mesh the report gives Triangles, Expansion and each pin group's pins, keyed pins and atom match names. Other effects are reported (below) | +| Effects | **Lumetri Color** (1200; see *Lumetri Color* below), **Curves** (961-964: the five curves' points from the arbitrary data as EffectCraft's `x,y` points in 0..1; render within 6 levels: EffectCraft interpolates monotone, After Effects stores a natural spline) and **Mesh Warp** (965, 985: Rows, Columns, Quality and the Distortion Mesh as per-vertex offsets in layer pixels; Bezier handles moved on their own are kept as eight extra numbers of the vertex for the export and reported, EffectCraft draws the mesh bilinear; the header is rows + 1, columns + 1; render 985: 1.4 % of pixels > 2 levels around the moved vertex), **Tint**, **Gradient Ramp**, **Exposure**, **Invert**, **Fast Box Blur**, **Gaussian Blur**, **Drop Shadow**, **Glow**, **Fill**, **Hue/Saturation**, **Levels**, **Timewarp** (Method, Adjust Time By, Speed with keys, Source Frame; 529-533), **Directional Blur**, **Radial Blur**, **CC Radial Blur**, **Venetian Blinds**, **Channel Blur**, **Black & White**, **Tritone**, **Luma Key**, **Simple Choker**, **Matte Choker**, **Transform** (all parameters; 711-723) and the expression controls **Slider**, **Angle**, **Checkbox**, **Color** and **Point Control** (table below) on any imported layer (also text and shape layers and layers with masks), in stack order, with the instance name, every parameter (static value or default) and its keyframes; also the second and later instances of a type, whose parameter definitions are only in the project's shared definition list. **Puppet** arrives in its place with its name and fx switch but without meshes and pins (a pin's rest place lies in the mesh data, not decoded yet); per mesh the report gives Triangles, Expansion and each pin group's pins, keyed pins and atom match names. Other effects are reported (below) | | Layer styles | Drop Shadow, Inner Shadow, Outer Glow, Inner Glow, Bevel and Emboss, Satin, Color Overlay, Gradient Overlay and Stroke when applied (switch group `tdsb` 1): every parameter, starting from After Effects' defaults (Use Global Light is off there), with keys on scalars and colours and expressions; Blending Options: Fill Opacity, the channel switches, Blend Interior Styles as Group, Use Blend Ranges, and Global Light Angle / Altitude, which the composition adopts (one setting per composition here). Units and popups through `effectcraft_aep::style_map` (aep-format 3.12b). Render check against After Effects (`proof\styles`): drop shadow, inner shadow, satin, colour overlay, stroke within a few levels; glow and bevel intensities and the Gradient Overlay direction differ in EffectCraft's renderer | | Shape layers | groups (nested, with anchor, position, scale, skew, skew axis, rotation, opacity), rectangle (size, position, roundness), ellipse (size, position), polystar (star or polygon, points, position, rotation, radii, roundness), bezier path (vertices and tangents in layer pixels, path keys), fill (colour, opacity, fill rule), stroke (colour, opacity, width, cap, join, miter limit), gradient fill and stroke (linear or radial, start and end point; the default white-to-black colours), trim paths (start, end, offset, trim multiple shapes), round corners (radius), merge paths (mode; 707, 708), offset paths (amount, line join, miter limit, copies, copy offset; 710; After Effects offsets copy *i* by Amount x (*i* + Copy Offset), default 1, EffectCraft by Amount x (*i* + 1 + copyOffset), so the copy offset arrives 1 less, also its keys; an expression on it is kept and reported; 826-828, 828 renders equal to After Effects on every grid point), repeater (copies, offset, composite, transform; 709), in file order, with keyframes and expressions, the operators' defaults as After Effects reads them back (731); the layer anchor in pixels; 238-242, 330-333, 451-484, 519, 705-710, 731 | | Markers | layer and composition markers: time (layer time for layer markers, exact frames), duration (1/600 s units, 509, 510), comment, chapter, URL, frame target, label, cue point (name, event or navigation, parameters); 122, 123, 249, 347-349, 509, 510 | | Time Remapping | an enabled remap becomes the `timeRemap` property (source seconds, keys in layer time with their interpolation and ease, expression), first in the layer's properties as Layer > Time > Enable Time Remapping puts it; 319, 354 | -| Footage | a link to the stored path; **missing-footage placeholder** when the path does not exist where the import runs (listed in `missingFootage`), a normal link when it does. The importer never opens footage. **An image sequence** (its alias is the folder) becomes a sequence of its files, named from the stored numbering (`shot_0101.exr` .. `shot_0103.exr` in 603, `seq_001.png` .. in 353), at its native frame rate; the first file is the footage's path and the one checked. Sequences and clips take the stored native frame rate and duration (603, 606). A sequence of more than 100 000 frames, or beyond a million file names in one import, links only its first file and is reported | +| Footage | a link to the stored path; **missing-footage placeholder** when the path does not exist where the import runs (listed in `missingFootage`), a normal link when it does. The importer never opens footage. **An image sequence** (its alias is the folder) becomes a sequence of its files, named from the stored numbering (`shot_0101.exr` .. `shot_0103.exr` in 603, `seq_001.png` .. in 353), at its native frame rate; the first file is the footage's path and the one checked. Sequences and clips take the stored native frame rate and duration (603, 606). A sequence of more than 100 000 frames, or beyond a million file names in one import, links only its first file and is reported. **One layer of a Photoshop file** shows that layer (`Footage::layer`: its index and name; at its own size with Retain Layer Sizes, else on the document) and is named `/` unless the item has a name; a layer without pixels (an adjustment layer) keeps the merged image and is reported; so is a layer as big as the document but not at its corner, which the item's size does not tell apart (shown at its own size), and an item as big as neither the layer nor the document (shown on the document) | Units, the "absent = default" rule and the keyframe templates are the reader's ([aep-format.md](aep-format.md), sections 3.3–3.6). The importer converts them to EffectCraft's: @@ -135,7 +142,15 @@ Units, the "absent = default" rule and the keyframe templates are the reader's text does not have; the runs are cut to the text. A range style splits `Hello` into runs of 1, 2 and 2 characters (248, 340). - **Null objects.** The hidden placeholder solid of a null object is not an item (null layers - have no source item here). + have no source item here). A null's Anchor Point that is not stored is its top-left corner + `[0, 0]`, not the centre, as After Effects 2026 reads it back (#3099). +- **Placed Illustrator and PDF files** (`.ai`, `.pdf`; `Footage::placed_page`): the page is + framed as After Effects places it (the ArtBox clipped to the crop box, `/Rotate` ignored) and + drawn to fill the stored footage size, which masks and anchors are measured against (#3099). + Create Shapes from Vector Layer on such a layer adds a hidden ` Frame` shape layer, the + Alpha track matte of every layer it makes, so they show only the frame, as the footage does. + EPS files keep their bounding box at their own size: how After Effects sizes them is not + measured. - **Track mattes.** The mode and the matte layer's id are in the target layer's header (aep-format.md 3.1). The matte layer can be any layer of the composition (407: below its target); it keeps its stored video switch (After Effects switches it off when the matte is set, @@ -182,6 +197,7 @@ parameter gets the file's value: | Importer block 2026-11-05 (`1100-quickwins.aep`: per effect defaults, set values, minimum and maximum, every popup item, a keyed and an expression parameter): `ADBE HDR ToneMap` (HDR Highlight Compression) 1 Amount; `ADBE Echo` 1 Echo Time, 2 Number Of Echoes, 3 Starting Intensity, 4 Decay, 5 Echo Operator; `ADBE Linear Wipe` 1 Transition Completion, 2 Wipe Angle, 3 Feather; `ADBE Offset` 1 Shift Center To, 2 Blend With Original; `ADBE Deflicker` (Color Stabilizer) 1 Stabilize, 2-4 Black / Mid / White Point, 5 Sample Size | `ec.utility.hdrcompression` `amount`; `ec.time.echo` `echoTime`, `numberOfEchoes`, `startingIntensity`, `decay`, `echoOperator`; `ec.transition.linearwipe` `completion`, `angle`, `feather`; `ec.distort.offset` `shift`, `blend`; `ec.color.colorstabilizer` `stabilize`, `blackPoint`, `midPoint`, `whitePoint`, `sampleSize` | percent (float, valid 0..100, default 100); seconds (float, valid ±30 000), integer (valid 30 000), 0..1 (float), Decay as stored (float, valid 0..100), popup by name: EffectCraft's Echo uses at most 255 echoes and a Decay up to 1, a value or key beyond that is imported as stored and **reported**; percent (fixed, 100), degrees, pixels (fixed, 32 000); point in layer pixels, Blend 0..1 x 100 = percent; popup by name (**fresh default Levels**), points in layer pixels, Sample Size as stored (fixed, valid 50, default 3; the full width of the sampled square in layer pixels, centred on the middle of the pixel the point names, measured in After Effects 2026, #3125). Color Stabilizer's reference frame (Set Frame) is not a parameter in the file and is not imported (EffectCraft's Reference Frame stays 0 s) | `tests/it/import/effects_quickwins.rs`; render comparison below | | Importer block 2026-11-05 (`1300-thirdparty.aep`), plug-ins that ship with After Effects: `CS Color Neutralizer` (CC Color Neutralizer) 19 Pinning, 20 Blend w. Original; `EXtractoR` 1 Channel Info data, 3 Black Point, 4 White Point, 5 UnMult; `IDentifier` 1 Channel Info data, 2 Display, 3 ID | `ec.color.cccolorneutralizer` `pinning`, `blendWOriginal`; `ec.3d.extractor` `red`, `green`, `blue`, `alpha`, `blackPoint`, `whitePoint`, `unMult`; `ec.3d.identifier` `channelType`, `display`, `id` | CC Color Neutralizer: percent (float, valid 100); its correction is the Red / Green / Blue slider of each range (the Unbalance colours are pickers that set them and keep no record), with View and Black / White Point: **EffectCraft's effect has no counterpart, each is reported when stored** (EffectCraft's Unbalance / Balance stay at their identity defaults). EXtractoR: each output channel by the footage channel's name, which EffectCraft's EXtractoR reads by the same name (the default R, G, B; `Z` from our preset), and `(copy)` as the input's own channel of that position (EffectCraft's R, G, B or A; the default Alpha is A); a record of another kind or without a name is reported and R, G, B, A used (`ExtractorChannels`), Black and White Point as stored (float, valid ±3.4e38; a value beyond ±1e9 is reported as damaged data, as for every number), UnMult a checkbox, Clip stays off. IDentifier: Display by name, ID as stored (integer, valid 2 147 483 647); the Channel Info names the chosen channel (`identifier_channel_type`): After Effects lists only the footage's integer (UINT) channels, and with none chosen (the data then says `Object ID`) it leaves the layer unchanged, where EffectCraft draws the footage's object ID channel: **reported**; a chosen channel named `ObjectID` or `MaterialID` (without regard to case, `objectId` in our preset) is the one EffectCraft's IDentifier reads itself (Channel Type Object ID or Material ID), any other name is reported (EffectCraft's IDentifier has no channel choice) | `tests/it/import/effects_thirdparty.rs`; render comparison below | | `ADBE Slider Control`, `ADBE Angle Control`, `ADBE Checkbox Control`, `ADBE Color Control`, `ADBE Point Control` (parameter 1) | `ec.control.slider` `slider`, `ec.control.angle` `angle`, `ec.control.checkbox` `checkbox`, `ec.control.color` `color`, `ec.control.point` `point` | float (valid max 1 000 000) as stored; degrees; checkbox; colour; point in layer pixels | 071, 073, 217-219, 221, 304, 310 | +| `ADBE Lumetri` by match-name suffix (1200), not by index: -0062 High Dynamic Range; -0063 HDR White; -0007 / -0008 Temperature / Tint; -0020 Saturation; -0011 Exposure; -0012 ... -0016 Contrast, Highlights, Shadows, Whites, Blacks; -0064 HDR Specular; -0026 Look Intensity; -0028 Faded Film; -0029 Sharpen; -0030 Vibrance; -0031 Saturation; -0033 Tint Balance; -0074 Show Mask; -0075 mask style; -0076 Invert Mask; -0081 Denoise; -0082 Blur; -0086 ... -0090 the HSL correction's Temperature, Tint, Contrast, Sharpen, Saturation; -0051 ... -0054 Vignette Amount, Midpoint, Roundness, Feather; -0073 the HSL key data; -0039 the first of the four RGB curves; -0042 Hue vs Sat; -0047 the Shadows, Midtones and Highlights wheels | `highDynamicRange`, `basicCorrection/tone/hdrWhite`, `basicCorrection/whiteBalance/temperature` / `tint`, `basicCorrection/saturation`, `basicCorrection/tone/exposure`, `.../contrast`, `highlights`, `shadows`, `whites`, `blacks`, `hdrSpecular`, `creative/lookIntensity`, `creative/adjustments/fadedFilm`, `sharpen`, `vibrance`, `creativeSaturation`, `tintBalance`, `hslSecondary/key/showMask`, `maskStyle`, `invertMask`, `hslSecondary/refine/denoise`, `blur`, `hslSecondary/correction/*`, `vignette/*`; `hslSecondary/key/hue*`, `saturation*`, `lightness*`; `curves/rgbCurves/master`; `curves/hueSaturationCurves/hueVsSat`; `colorWheels/shadowsWheel`, `midtonesWheel`, `highlightsWheel` | sliders: the stored number x 1 (valid maxima 1000, 300, 300, 7, 150, 200, 150, 100, 100, 300, 150, 100, 1000, 5, 100 as in `pard`); checkboxes and the popup as elsewhere, **also from records in the default state** (`tdsb` 3); the key: hue centre x 360 degrees, range x 360 as the half width, tolerance x 360 as the softness, saturation and lightness centre +- range x 100 percent, tolerance x 100, once a colour was picked; the master curve's points as `x,y` (0..1); Hue vs Sat's points with y as `0.5 + y / 2` (After Effects' y 1 doubles the saturation, EffectCraft's y 1 too); a wheel's hue (degrees) and distance as the HSL colour of that hue with saturation = distance and lightness 0.5 | 1200: all 62 read-backs of mapped controls in sections that act equal after import (min, max, sections, popups); the other 61 lie in sections that do nothing and are reported; keys and the expression on HDR White (`-56-keys`, `-57-expr`); the presets `-60-preset-curves-rgb`, `-61-preset-curves-huesat`, `-62-preset-wheels` | - **Nothing to import:** the implicit input-layer parameter (index 0), the Swap Colors buttons (Tint 4, Ramp 8: they always have a record and hold no value), Exposure's twirl-down group @@ -233,6 +249,44 @@ parameter gets the file's value: column just outside the layer: After Effects' Glow draws a dark halo about 10 px wide outside the layer (red 0.55, 0.65, 0.74, 0.92 at 0, 1, 2, 5 px), EffectCraft's Glow none (an effect difference, recorded for a separate fix). +- **Lumetri Color** (1200, `crates/aep/src/import/lumetri.rs`). The parameters are found by match + name (After Effects inserted `-0127` ... `-0130` mid-list; five tone sliders share one valid range, + so an index shift would not be caught). Values come from every value record, also those in the + default state (`tdsb` 3), where Lumetri keeps High Dynamic Range, HDR Specular and the second Look + popup. EffectCraft's Lumetri has no section switches: a section that is off in After Effects + (Basic Correction, Creative, Curves / RGB Curves, each hue / saturation curve, Color Wheels, HSL + Secondary, Vignette), and HSL Secondary without a picked key colour, does nothing there, so its set + values are listed in one report line and not imported (the frame stays as After Effects draws it). + The Input LUT and the Look are reported by name (LUT files are not in the project). Controls + without a counterpart (the Auto tone's Intensity, RGB Curves' HDR Range, Color Wheels' HDR White and + Face Detection, Legacy Tone And Saturation, Auto Color, SpeedGrade Custom Layer), the eyedropper + colours, the red, green and blue curves (only the master curve is sampled, so which is which is + not known), the colour wheels' brightness sliders and their fourth wheel, Split Toning, the HSL + correction wheels and moved points of the other four hue / saturation curves (not sampled) are + reported when set. The master RGB curve arrives as its points; Hue vs Sat with y as `0.5 + y / 2` + (After Effects' y is the offset from neutral, 1 doubling the saturation; EffectCraft's y = 1 + doubles it); a colour wheel's hue and distance as the HSL colour of that hue with saturation = + distance, lightness 0.5 (EffectCraft's wheels have no brightness slider). Legacy Tone And + Saturation is reported when a record says on: its `pard` default turns from off to on when the + project is saved again. Render check at 0.5 s against After + Effects' 32 bpc PNGs (stored as value x 6553.5 in 16 bits, so read as raw / 6553.5): every + composition whose grade After Effects draws as identity (defaults, set-all with every section off, + the switched-off sections, the popups, keys, expression) is within 1/255 in EffectCraft; the + graded ones differ by EffectCraft's Lumetri formulas, not by the import: White Balance (sec-color: + After Effects warms and brightens, max 0.10 above the default; EffectCraft darkens, mean error + 0.015), Exposure / tone (sec-light: After Effects rolls highlights off and lifts the blacks to 0.18, + EffectCraft clips at 1 and keeps black at 0; mean 0.12), Creative adjustments (sec-adjustments: + Faded Film lifts black to 0.07 in After Effects, 0.035 here) and the extremes (min / max: mean 0.063 / + 0.454; max: After Effects' vignette at +5 and the tone sliders give mid grey, EffectCraft white). + The 32 bpc PNGs hold the pixel value with no transfer curve (the defaults' ramp reads x / 320 + exactly), so values above 1 survive up to about 10; the comparison clips After Effects at 1 as + EffectCraft's 8-bit frame does. The hand-made presets: the master RGB curve within 0.024 (mean + 0.009; After Effects' curve is a natural cubic spline through the points, within 0.003, while + EffectCraft's Lumetri interpolates monotone); Hue vs Sat mean 0.008, max 0.18 (After Effects + scales the saturation in linear light, red reaching 1.26; EffectCraft in display values, 8.3 % of + pixels > 2 levels); the colour wheels mean 0.042, max 0.13 (the hues go the same way: Shadows + lift blue at black by 0.13 in After Effects, 0.17 here; Highlights add green; the brightness + sliders are not imported); the Input LUT and the Look are not imported (mean 0.088 / 0.076). - **Colour.** After Effects stores the colour its picker shows as ARGB doubles 0..255 in the project's working-space encoding; EffectCraft's parameter colours are straight RGBA 0..1 that the render pipeline hands to the effect as stored (`crates/render/src/color.rs`: authored colours are @@ -264,30 +318,54 @@ parameter gets the file's value: `approximated_keyframes`. - **The fx switch** of each instance (the `tdsb` of its value group: 1 on, 0 off; `fx-switch.aep`) is imported and exported, and so is an expression on a stored effect parameter (its text and its switch). +- **Effects without a counterpart** (third-party plug-ins, pseudo effects of expression controls, + `Pseudo/…`) arrive as **placeholders** (#3087): an effect group whose effect id is the After + Effects match name (expressions also find it by that name, `effect("Pseudo/0.25")`; its match id + is that name as one path segment, `Pseudo_0_25`). Like a missing plug-in it renders nothing and is listed by Find Missing + Effects. It holds the effect's parameters flat (without the twirl-down group markers), named as + in After Effects, each with its value, keys and expression, in the units After Effects' + expressions read (sliders and angles as stored, a float slider with its range, points in layer pixels, colours, checkboxes, + popups over the file's items with separators, read 1-based; layer parameters name their layer, + without a record the layer itself as for Layer Control, and their keys and expression are + reported), + so expressions that read them (`effect("Settings")("Speed")`) keep working. A parameter's id is + its match name (`CC Cylinder-0003`), or `param` when that name is no path segment (a + pseudo effect's `Pseudo/0.25-0001`). Paths and arbitrary data are not kept; a popup whose + definition is borrowed, or whose default slot holds no item, and that has no value record takes + its first item (reported). One import keeps at + most 200 000 placeholder parameters and popup items (a popup at most 1024 items; parameters it + does not keep count, and an item one more per 16 bytes of its name); the rest is + reported, and once that is spent a later effect without a counterpart arrives without a + placeholder (reported). A placeholder is still reported in `effects` and counted in + `imported.placeholderEffects`; .aep export reports it and leaves it out. ## What is reported `ImportReport` (`serde`, returned as JSON) has `imported` (counts, including `textLayers`), -`losses` (per category: `count` exact, up to 20 `details`), `missingFootage`, `missingFonts` -(PostScript names; not losses), `limitations` and `aeVersion`. The categories: +`losses` (per category: `count` exact; up to 20 `details`, plus one line per unmapped effect with its +number of instances, the most used first; `unlisted`, how many of the `count` no line describes), +`missingFootage`, `missingFonts` +(PostScript names; not losses), `limitations` and `aeVersion`. The losses prompt shows each +category with how many occurrences are left out or changed, every detail line, and "… and N more" +for the `unlisted` ones. The categories: | Category | Counted | |---|---| -| `effects` | each effect on an imported layer that is not in the table (by name and match name); for an imported effect: each stored parameter outside the table, a parameter whose type or valid maximum differs from the table, keys or an expression on a layer parameter, an effect without readable parameter definitions | +| `effects` | each effect on an imported layer that is not in the table (by name and match name; it arrives as a placeholder, "kept without rendering", unless it has no readable parameter definitions); for an imported effect: each stored parameter outside the table, a parameter whose type or valid maximum differs from the table, keys or an expression on a layer parameter, an effect without readable parameter definitions | | `masks` | a mask that is not imported because its path cannot be used (a Mask Path entry without shapes, a mask on a text or shape layer whose path was never set (its default is not sampled), unreadable, no vertices, a point count that is not a multiple of three, more than 65 536 vertices, numbers that are not finite), a mask mode code outside the seven known ones (imported as Add) | | `layer_styles` | per layer: a style in switch state 3 (not sampled; state 0 is imported as a style switched off, 988), Pattern Overlay, an unknown style or parameter, a gradient (`GCst`, not decoded: EffectCraft's default gradient stays), keys on a popup or checkbox, a value that does not fit, styles on a layer that takes none here (adjustment, camera, light) | | `markers` | a marker with an unknown cue type (imported as an event cue) | -| `text_layers` ("text layer features") | per text layer, what of it is not imported: wiggly and expression selectors and unmapped animator properties, Path Options or More Options with set values, Source Text keyframes or an expression, character style or paragraph fields that are not decoded and differ from the default (named by their slot, e.g. `/68`), an unknown capitalisation or justification code, manual kerning, No Break, the stroke line join, the Japanese leading model, a digit set other than the default and Arabic, control characters of unknown meaning (they are kept in the text; U+0003 is not one of them, see Text above). The text layer itself is imported | -| `shape_layers` ("shape layers") | what of a shape layer is not imported: shape items other than groups, rectangle, ellipse, polystar, path, fill, stroke, gradient fill and stroke, trim paths, round corners, merge paths, repeater and offset paths (for example wiggle or twist), stroke dashes, taper and wave with values, item blend modes and composite order, shape direction, gradient colour stops other than the default, gradient highlight values other than 0, material options, groups nested deeper than 32, a line cap or join of the unsampled third kind (imported as it), a path without a readable path. The layer itself is imported | +| `text_layers` ("text layer features") | per text layer, what of it is not imported: wiggly and expression selectors and unmapped animator properties, Path Options or More Options with set values, Source Text keyframes or an expression whose flags are unreadable, character style or paragraph fields that are not decoded and differ from the default (named by their slot, e.g. `/68`), an unknown capitalisation or justification code, manual kerning, No Break, the stroke line join, the Japanese leading model, a digit set other than the default and Arabic, control characters of unknown meaning (they are kept in the text; U+0003 is not one of them, see Text above). The text layer itself is imported | +| `shape_layers` ("shape layers") | what of a shape layer is not imported: shape items other than groups, rectangle, ellipse, polystar, path, fill, stroke, gradient fill and stroke, trim paths, round corners, merge paths, repeater and offset paths (for example wiggle or twist), stroke dashes, taper and wave with values, item blend modes and composite order, shape direction, gradient colour stops other than the default, gradient highlight values other than 0 (only from written records: one in the default state holds the default), material options, groups nested deeper than 32, a line cap or join of the unsampled third kind (imported as it), a path without a readable path. The layer itself is imported | | `lights` | each environment light (the layer is skipped; the other layers of the composition are imported) | | `blend_modes` | a blend mode code that is not mapped (1, 3 = Dissolve or Dancing Dissolve, 21 / 22 = Alpha Add or Luminescent Premul, codes above 34; the layer stays Normal) | -| `transform_properties` | a stored transform property that is not imported (keyed or expression-driven orientation, separated position dimensions), and the stored camera and light options that are not mapped (Blur Level, Iris, Falloff, ...) | +| `transform_properties` | a stored transform property that is not imported (an orientation that cannot be read, or set on a layer without one, separated position dimensions), and the stored camera and light options that are not mapped (Blur Level, Iris, Falloff, ...) | | `time_remapping` | an enabled Time Remap without a readable keyframe | | `approximated_keyframes` | a key imported in a simpler form (unknown interpolation code, bezier ease that cannot be decoded: a colour key with a non-zero ease, non-finite ease data; a mask path key with a speed other than 0, imported unverified) | | `parent_links` | a parent that was not imported (a skipped camera, a missing layer) or that forms a loop | | `track_mattes` | a track matte of unknown mode, whose matte layer was not imported or does not exist, a layer matted by itself, or a matte of the older "layer above" form on the top layer | | `unsupported_layers` | an unknown layer type, a layer without a usable source, a precomposition loop (the layer is cut) | -| `unsupported_items` | a project item of an unknown kind | +| `unsupported_items` | a project item of an unknown kind; an item made from a Photoshop layer without pixels (it shows the merged image), or from one as big as the document but not at its corner (shown at its own size), or an item as big as neither the layer nor the document (shown on the document) | | `damaged_data` | a header, key or value that could not be read, numbers that are not finite or beyond ±1e9 (default used); an unreadable text document (the text layer is imported with empty text), damaged style runs | `limitations` states what the reader cannot see at all, so it can be neither imported nor counted: @@ -417,6 +495,10 @@ ranges of Lumetri Color among them), are those of the 2026 plug-in in all three - `crates/aep/tests/it/import/effects_round3.rs`: the 24 effects of 905 with their values and After Effects' defaults, the layer parameters of 905 and the Layer Controls of 220, 320, 321, hostile layer ids (missing, 0, out of range) reported or neutral. +- `crates/aep/tests/it/import/text_synthetic.rs` (the committed `tests/fixtures/text-synthetic.aep`, + no corpus needed): an animator's Stroke Opacity, static and keyed; the Source Text expression, + enabled and disabled, also after an export and a second import; and, as a guard, one whose `tdb4` + flags are unreadable stays reported (#3170). - `crates/aep/tests/it/import/text_anim.rs`: the animators and range selectors of 579-594 and 114 (every selector setting, every mapped property, 594's keyed Start with its ease), the character and paragraph settings of 538-578, 595, 596, and what is reported (548 a line join without a @@ -451,10 +533,10 @@ generated corpus and its case numbers, not a claim about what was or was not see ## Export -The other direction, File ▸ Export ▸ After Effects Project… (`file.exportAep`), is described in +The other direction, File ▸ Export ▸ Adobe After Effects Project… (`file.exportAep`), is described in [aep-export.md](aep-export.md). ## Not done yet -Effects outside the table (Lumetri; EXtractoR: its channel choice is arbitrary data; Stroke: its path is a mask reference), environment lights, the ease of bezier Orientation keys, wiggly and expression selectors and the other text animator properties (Character Value, Character Alignment other than its default, the Hue / Saturation / Brightness / Opacity variants, 3D rotations, variable font axes), manual kerning, No Break, the stroke line join, the Japanese leading model, Source Text keyframes, layer-style gradients, Pattern Overlay and the switch state 3, Mesh Warp handles moved on their own, +Lumetri Color's section switches (a switched-off section is reported and its values left out), its LUTs and looks (files After Effects names: reported by name), its red, green and blue curves (no sample tells which is which), the colour wheels' brightness sliders and the fourth wheel, Split Toning and the HSL correction wheels (not sampled: reported when set) and moved points of the four hue / saturation curves other than Hue vs Sat (the scale of y is not sampled); effects outside the table (EXtractoR: its channel choice is arbitrary data; Stroke: its path is a mask reference), environment lights, the ease of bezier Orientation keys, wiggly and expression selectors and the other text animator properties (Character Value, Character Alignment other than its default, the Hue / Saturation / Brightness variants and Fill Opacity, 3D rotations, variable font axes), manual kerning, No Break, the stroke line join, the Japanese leading model, Source Text keyframes, layer-style gradients, Pattern Overlay and the switch state 3, Mesh Warp handles moved on their own, separated dimensions and footage interpretation need verified layouts in the reader first. diff --git a/docs/agents.md b/docs/agents.md index ebf158029..aff59e00a 100644 --- a/docs/agents.md +++ b/docs/agents.md @@ -118,7 +118,7 @@ See [MCP conventions](mcp.md) for core tools, resources, strict arguments, and r | `list_effects {filter?, compact?}` | Effect ids, names, categories, GPU / 32-bpc support and parameters; `compact` lists only ids, names and categories. | | `list_fonts {query?, variable?, rescan?}` | Font families text layers can use, bundled and installed, with their styles, origin, whether each is a variable font and own-language name; `variable` lists only variable fonts; `rescan` picks up fonts installed since launch. | | `add_effect {layer, effect, values?, comp?}` | Apply an effect and set its parameters in one call and one undo step (a failing value leaves nothing applied); returns the instance path (`effects/#n`) and its parameter paths. | -| `render_frame {comp?, time?, max_side?, path?, inline?, transparent?}` | Returns a PNG image of a frame; `transparent: true` keeps the alpha (as an RGB + Alpha render writes it) instead of compositing over the comp background. | +| `render_frame {comp?, time? \| times?, columns?, labels?, max_side?, path?, inline?, transparent?}` | Returns a PNG image of a frame; `transparent: true` keeps the alpha (as an RGB + Alpha render writes it) instead of compositing over the comp background. `times` (1 to 64 comp seconds, not with `time`) returns one contact-sheet PNG instead, the frames tiled row by row in the order given: `columns` sets the grid width (default near-square), `max_side` applies to each cell (default 320; a sheet is at most 7680 pixels on a side), `labels` (default true) draws each cell's time. The metadata gives `comp`, `times`, `columns`, `rows`, `cellWidth`, `cellHeight`, `width` and `height`. `path` writes the sheet in a headless session; bridged, a sheet comes back inline only. | | `open_project {path \| demo \| new}` / `save_project {path?}` | Open and save files. | | `undo {steps?}` / `redo {steps?}` | History. | | `history {goto?}` | The branching undo history (History panel): every state with its index, id, label, branch depth and whether it's current; `goto` (an index or id) jumps to any state, on any branch, without losing the others. | @@ -129,7 +129,7 @@ See [MCP conventions](mcp.md) for core tools, resources, strict arguments, and r | `command_run {id, params?}` | Run a command. | | `command_batch {steps, stop_on_error?}` | Run commands in order with per-step results; each edit has its own undo step. | | `doc_inspect` | Project overview and active composition. | -| `render_preview` | Render a PNG without writing a file (`comp`, `time`, `max_side`, `transparent`). | +| `render_preview` | Render a PNG without writing a file (`comp`, `time`, `max_side`, `transparent`), or a contact sheet of several times (`times`, `columns`, `labels`, as `render_frame`). | Layers are referenced by id (from `get_comp`), `"#n"` (1-based index from the top) or name. Comps are referenced by id or name, and default to the active comp. An id may also be sent as a string (`"98"`): @@ -322,6 +322,10 @@ build real projects through these interfaces; they are worked examples of everyt then `keys.velocity {"outSpeed":0,"outInfluence":33.33}`. * **Gradients**: `set_property {"path":"contents/gfill/colors","value":["#0080ff","#ffff00"]}` or `{"colors":[[0,"#0080ff"],[1,[1,1,0]]],"opacities":[[0,1],[1,0.5]]}`. +* **Projects**: `file.import` takes `.ecproj` / `.ecprojx` and After Effects projects (`.aep`, `.aepx`) + too, each into a folder named after it (`projects` in the result). An After Effects project that + would lose parts is refused (an `errors` entry, nothing imported): `file.importAep {"path": …, + "dryRun": true}` lists the losses, `"allowLosses": true` imports the rest. * **Image sequences**: `file.import {"paths":["/r/shot_0001.png"], "sequence": true}` imports the file's whole numbered run as one item (several picked frames: that range; a folder: each run in it, also without `sequence`). Without `"sequence": true` a picked or dropped numbered file imports @@ -488,8 +492,9 @@ between the layer's In and Out points in the background ("Analyzing in backgroun then "Stabilizing..."; `warp.status` reports `progress`, `banner`, whether the effect is `analyzed`, and the current frame's `warp` matrix, `autoScale` and `crop`); `warp.cancel` stops it without writing anything. The analysis is stored in the effect (saved with the project) and is -one undo step; trimming, slipping, time-remapping or replacing the layer's source clears it and -the app re-analyses automatically (headless: run `warp.analyze` again). Settings are ordinary +one undo step; trimming, slipping, time-remapping or replacing the layer's source, editing its +masks, the effects above the Warp Stabilizer or, on a precomp layer, the precomp's contents +clears it and the app re-analyses automatically (headless: run `warp.analyze` again). Settings are ordinary properties under the instance's groups, e.g. `effects/#1/stabilization/result` (0 Smooth Motion, 1 No Motion), `effects/#1/stabilization/smoothness`, `effects/#1/stabilization/method`, `effects/#1/borders/framing` (0 Stabilize Only … 3 Stabilize, Synthesize Edges), @@ -536,7 +541,8 @@ Each edit is one undo step; the first create adds the one-node "3D Tracker Camer startFrame?: 1, includePoints?: true}` exports the solve as an executable Blender scene script: baked camera/lens/focus, synced video background, points and ground. See [Blender camera export](blender-camera-export.md) for framing, assumptions and limitations. -Changing the layer's frames clears the analysis; changing `effects/#1/shotType`, +Changing the layer's frames, masks, the effects above the tracker or a precomp's contents +clears the analysis; changing `effects/#1/shotType`, `effects/#1/horizontalAngleOfView`, `effects/#1/advanced/solveMethod` re-solves the stored tracks. ```sh @@ -861,6 +867,75 @@ not read or written. `` is an id or name, or `-` for the active comp. `` is JSON (`50`, `[960,540]`, `"#ff0000"`) or a bare string. +## Repro scripts + +Every journaled command the app runs (menus, panels, shortcuts, agents, scripts) goes into the session's +command journal. A **repro script** is that journal as JSON lines, so a bug or crash becomes +something an agent can replay and a regression test becomes a data file. + +```text +// comments: lines starting with // or #; blank lines are skipped +{"start": "new" | "demo" | {"project": "rel/or/abs.ecproj"}, "allow_errors": false} optional, first line +{"command": "comp.new", "params": {"name": "Main", "frameRate": 30}} +{"command": "comp.settings", "params": {"frameRate": 0}, "expect_error": "frameRate"} +{"expect": {"query": "comp.info", "pointer": "/frameRate", "equals": 30}} +{"expect_no_error": true} +``` + +- `start`: the project to begin with (default `new`). A relative `project` path is relative to the + script. Other header keys (`version`, `app`, `os`, `crash`, `note`) are for people. +- `command`: run like the app runs it. It must succeed, unless it has `expect_error` (`true`, or a + text the error must contain) or the header says `allow_errors` (errors are reported but + tolerated). A panic always fails the replay. +- `expect`: run a command, normally a query (`prop.get`, `comp.info`, `layer.tree`, `project.summary`), + and compare the value at the JSON `pointer` of its result with `equals` (numbers within 1e-6). + Without `equals` it only checks that the query succeeds. +- `expect_no_error`: the previous command succeeded. + +Where scripts come from: + +- `journal.export {path?}` returns `{script, steps}` (or writes `path`): the commands since the + project was last replaced (`file.newProject`, `file.open`, `file.openDemoProject`). Queries and + saves are not in it; commands a script or `file.runScript` ran are covered by that one step. + A successful Revert starts a new replay boundary at the saved file it reopened. +- `help.exportRepro {path?}` saves the same for a bug report, by default to `Repros/` in the + settings folder (macOS `~/Library/Application Support/EffectCraft/Repros`, next to `Logs/`). + Confined automation must give an explicit path inside a write root; `journal.export {}` + remains available because it only returns the script in memory. +- **Crash repros**: when the last-resort guard catches a panic in a command, the app writes + `Repros/crash--.jsonl` there automatically (the newest 20 are kept): the + journal, then the command that panicked, with `"allow_errors": true` and the error in the header. + The error the user or agent sees names the file. Headless sessions without a settings folder + (the CLI, tests) don't write one. + +Privacy: a repro holds command ids and parameters only, never project, image or media file +contents. Parameters can include file paths, layer and item names and text typed into text +layers, so read a repro before sharing it. + +Replay one headless (a fully wired session, like the CLI's other subcommands): + +```sh +effectcraft-cli replay crash.jsonl # per-step ok/FAIL lines, then the first failure +effectcraft-cli replay crash.jsonl --json # {ok, panicked, steps, first_failure, skipped} +effectcraft-cli replay crash.jsonl --keep-going +``` + +Replaying a repro runs its commands with your privileges and file access, including commands +that write files or run scripts. Read repros from untrusted sources before running them, or use +a disposable environment. + +It exits with status 1 when a step fails, an expectation is not met or a command panics, and +stops at the first failure unless `--keep-going`. Steps that work on the user's files (`file.open` +of their project, imports) need those files; replace them with a `start` project or generated +content when you turn a report into a test. A repro only sees commands: UI state changed +without one (a hover, a half-finished drag) is not in it. + +**Regression tests are data files.** A crash fix's regression test is the repro, trimmed to the +steps that matter, with an `expect_error` or `expect` line where the fix changed the outcome, +dropped into [`tests/repros/`](../tests/repros). `crates/host/tests/it/repros.rs` replays every +`*.jsonl` there and fails on any panic or unmet expectation +(`cargo nextest run -p effectcraft-host --test it -E 'test(/^repros::/)'`). + ## Seeing the UI To work on the UI, start the app with `cargo run -p effectcraft -- --control 9877` (add `--demo` to diff --git a/docs/architecture.md b/docs/architecture.md index 83c03f14f..0d9a23c1b 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -18,10 +18,7 @@ cpal or muda. Everything in L0 to L4, the egui UI and the web app also build for | L0 | `time` | `Tick` (254 016 000 000 per second), rational frame rates incl. NTSC, SMPTE and drop-frame timecode | | L0 | `geom` | Vectors, matrices, quaternions, the layer transform (anchor, position, scale, orientation, rotation) | | L0 | `color` | sRGB and linear, HSL/HSV, luminance, the 38 blend modes, label colors | -| L0 | `vp9enc` | VP9 encoder (profile 0, 8-bit 4:2:0, key and inter frames with motion search, loop filter and rate control; lossless at quality 100) for WebM export, from the VP9 bitstream specification | | L0 | `opusenc` | Opus encoder: RFC 6716 SILK (NB/MB/WB), hybrid (SWB/FB) and CELT (FB) modes chosen by bitrate and application (audio / voice), 48 kHz 20 ms packets, mono/stereo, and the RFC 7845 `OpusHead`, for WebM export audio | -| L0 | `hevcenc` | HEVC (H.265) encoder from ITU-T H.265: Main / Main 10 4:2:0, IDR + P slices (quarter-pel motion, merge/AMVP), CABAC, deblocking, bitrate or constant-QP rate control, for MP4 (`hvc1`) export | -| L0 | `av1enc` | AV1 encoder from the AV1 bitstream specification: Main profile 8/10-bit 4:2:0, key + inter frames (quarter-pel motion), deblocking, for MP4 (`av01`) and WebM export | | L1 | `raster` | Premultiplied float images, sampling, affine and projective warps, blurs, compositing (parallel with rayon) | | L1 | `keyframe` | Animated values, keyframes with temporal ease and spatial Bezier, roving, hold, velocity | | L1 | `segment` | Swappable trained models for Roto Brush and face tracking: the `MaskModel` and `FaceModel` interfaces, a registry of open-licensed models (authors, licence, URL, size, SHA-256), PyTorch checkpoint and TensorFlow Lite readers (with an interpreter), SHA-256, MobileSAM (TinyViT + Segment Anything decoder) and MediaPipe Face Landmarker (BlazeFace + Face Mesh V2) on rayon and ndarray's safe GEMM | @@ -43,7 +40,7 @@ cpal or muda. Everything in L0 to L4, the egui UI and the web app also build for | L3 | `interchange` | Premiere Pro interop through timeline interchange (FCP7 XML, FCPXML, OTIO, EDL, AAF, OMF via FilmCraft's `filmcraft-interchange`): sequences ↔ compositions (§6a) | | L3 | `aep` | After Effects projects in one crate (like `lottie`): `container` reads and writes the `.aep` container (RIFX `Egg!`) as a lossless chunk tree with a byte-identical round trip, bounded for hostile input, reads the `.aepx` XML form into the same tree, and has a typed view of items, layers, properties, effects, masks, shapes, text, markers, footage, cameras, lights and layer styles, from our own observations of generated files ([aep-format.md](aep-format.md)); `import` maps the typed view into a project (items, settings, compositions, every layer kind, Transform, keyframes, expressions, masks, text, shapes, cameras, lights, mattes, markers, time remapping, layer styles, the effects of a shared table, footage) with a report of everything not imported ([aep-import.md](aep-import.md)); `export` builds every chunk in code (prototypes in the layout After Effects writes, cloned and edited only at verified fields) for the same scope, with a report of everything not exported, verified by opening the output in After Effects ([aep-export.md](aep-export.md)) | | L3 | `lottie` | Lottie JSON / dotLottie import and export (layers, precomps, eased and spatial keyframes, shapes, masks, mattes) with a warnings list for what Lottie cannot express | -| L3 | `plugin` | Effect plug-ins: loads sandboxed WebAssembly effect modules (plug-in API v1; the wasmi interpreter behind the `wasm` feature, on for native hosts) into the effect registry ([plugins.md](plugins.md)) | +| L3 | `plugin` | Effect plug-ins: loads sandboxed WebAssembly effect modules (WebAssembly ABI v1 and v2, the latter with neighbouring frames; the wasmi interpreter behind the `wasm` feature, on for native hosts) into the effect registry ([plugins.md](plugins.md)) | | L3 | `helper-host` | External effect helpers: finds helper programs, runs each as a child process and registers its effects (plug-in API 2) over a versioned pipe + shared-file protocol; a crashing helper is an error on the effect, never a crash ([external-helpers.md](external-helpers.md), [ADR 0001](adr/0001-external-effect-helpers.md)). Native only | | L4 | `engine` | `Session`: project, branching undo history, editor state, the command registry and menus, the ScriptUI window model (`scriptui`) | | L4 | `script` | Scripting: JavaScript (boa) with an After Effects-style object model (`app.project`, comps, layers, properties, render queue) and ScriptUI (dialogs, palettes, dockable panels; script-host threads so dialogs block `show()`), whose edits run engine commands; AE match names | @@ -121,7 +118,7 @@ are not rewritten, so an expression that computes an effect point on a shape or other values may need `+ [thisComp.width / 2, thisComp.height / 2]`. Effects that use the layer bounds (edge pinning, wipes, Offset, generators) now cover the whole comp-sized bounds instead of their bottom-right quarter. Tracking data of effects that analyse footage (Warp Stabilizer, 3D -Camera Tracker, Mocha shape) is left as it is: footage has a source rectangle, whose effect space +Camera Tracker, Tracked Shape Matte) is left as it is: footage has a source rectangle, whose effect space did not change. ## 4. Rendering @@ -331,7 +328,7 @@ before conversion (a quarter of the conversions; the result differs from convert in blocks where colours clip) and resampled from there, as other frames are from full size. **GPU compositor** (`crates/gpu`; Project Settings ▸ Video Rendering and Effects ▸ Mercury GPU -Acceleration, the default, or Mercury Software Only; `render.backend`). The CPU renderer is the +Acceleration, the default, or Software Rendering; `render.backend`). The CPU renderer is the reference and keeps rendering layer content (sources, masks, CPU effects, layer styles) into the layer cache. The render crate defines an `Accelerator` trait; `effectcraft_gpu::Gpu` implements it on wgpu compute shaders. `RenderOpts::backend` picks `Cpu`, `Gpu` or `Auto` (an accelerator @@ -340,10 +337,13 @@ whichever compositor measured faster for it (`render::auto::AutoPick`, kept by t per comp, output scale and path (readback or viewer display) it averages CPU and GPU frame times (warm-up on both sides, the fastest warm-up frame starting the average, single stalls capped), picks the GPU unless the CPU is clearly faster, and re-measures the other side every 48 frames. -Render Queue jobs don't use Auto: every frame of a job renders on the project's renderer (the -GPU compositor with Mercury GPU Acceleration), so runs of a render don't differ by timing. Light comps (a few small layers, which the CPU composites only within their bounds) then stay on -the CPU instead of paying for full-frame passes and a readback. A GPU frame walks the comp like +the CPU instead of paying for full-frame passes and a readback. Render Queue jobs don't use +Auto per frame: a job renders every frame on one compositor, never a mix (#3335). A GPU +Acceleration job with an accelerator times both on its first frames +(`export::pipeline::Cx::probe`, jobs of 12 frames or more) and keeps the faster, the CPU only +when clearly faster; `EFFECTCRAFT_RQ_RENDERER=gpu|cpu` fixes the choice, and readback-deferred +accelerators (the web) render on the project's renderer. A GPU frame walks the comp like `draw_comp`: cached layer buffers are uploaded once per buffer, then transformed with the CPU's sampling (nearest, bilinear, Catmull-Rom bicubic, the same minification pre-filter), motion-blur sub-samples are accumulated, and track mattes, Preserve Transparency, layer style passes, @@ -370,16 +370,16 @@ Preserve Transparency / blend kernels (a 3D track matte renders solo through the Environment Light Background layers draw their sky in a kernel (`adv_sky`, from `Renderer::sky_draw`) before the rest of the run, in Classic and Advanced 3D comps. Wireframe outlines draw on the GPU from the CPU's pixel list (`Renderer::wireframe_pixels`). GPU effects -(`effects::GPU_EFFECTS`, 280 of them: blurs, colour correction, keying incl. Key Light, mattes, -channel, stylize, distortion and warps (Warp, Bezier Warp, Smear, Reshape, CC Bend It, CC Page -Turn, CC Bender, CC Blobbylize), Cartoon, bevels, shapes, the transitions (CC light family, CC -transitions, Block Dissolve), perspective (Radial Shadow, CC Cylinder / Sphere / Spotlight / +(`effects::GPU_EFFECTS`, 280 of them: blurs, colour correction, keying incl. Screen Color Key, mattes, +channel, stylize, distortion and warps (Warp, Bezier Warp, Smear, Reshape, Two-Point Bend, CC Page +Turn, Flexible Bend, Organic Displacement), Cartoon, bevels, shapes, the transitions (CC light family, CC +transitions, Block Dissolve), perspective (Radial Shadow, Cylinder Wrap / Sphere / Spotlight / Environment, 3D Glasses), generators, noise, grain, text (Numbers, Timecode), time, 3D Channel (aux channels as an extra texture), Immersive Video, colour management (colour programs from `effects::color_program`, LUTs in a storage buffer) and the simulations' render passes (the simulation stays on the CPU and hands its plan — sprites, pieces, blobs, grids — to a tiled -rasteriser; the particle effects too, `gpu::fx_particles`: CC Particle World / Systems II, -Particle Playground with its Layer Map frames in one atlas, CC Ball Action, CC Pixel Polly, CC +rasteriser; the particle effects too, `gpu::fx_particles`: Particle Emitter 3D / Systems II, +Particle Playground with its Layer Map frames in one atlas, Ball Grid, Pixel Shatter, CC Scatterize; plans too large for one item table draw in several passes over the previous one), and the geometry generators (Lightning, Advanced Lightning, Radio Waves, Stroke, Scribble, Vegas, Write-on, Audio Spectrum / Waveform, Basic / Path Text: bolts, strokes, waves, audio @@ -390,7 +390,7 @@ as kernels, in one module per family (`gpu::fx_*` with `shaders/fx_*.wgsl`); con effects run as one chain with one upload and one readback. Statistics that need the whole frame (Auto Levels / Contrast / Color, Equalize, Shadow/Highlight, Color Stabilizer, Remove Grain's noise level) are measured on the CPU from one readback and applied on the GPU; effects reading -other frames (Echo, Posterize Time, Time Difference, Time Displacement, CC Force Motion Blur, CC +other frames (Echo, Posterize Time, Time Difference, Time Displacement, Temporal Motion Blur, CC Wide Time, Pixel Motion Blur, Timewarp) upload the frames the host renders (`time_frames`) and combine them on the GPU; Time Displacement's per-pixel times and Timewarp's motion vectors (`time_fx::timewarp_plan`) are computed on the CPU. Numbers and Timecode rasterise their glyph @@ -412,7 +412,12 @@ as NaNs (a kernel that skips pixels fails the oracle) and validation errors pani (`ui-egui::frames`); headless renders, the CLI (unless `--gpu`) and CI use the CPU. Outside tests, device errors are logged rather than fatal. A viewer frame that runs out of video memory (`Gpu::within_memory`) renders on the CPU, the uploaded layers and pooled textures are freed, and -the RAM preview keeps half as many GPU frames from then on. The same guard catches a frame's +the RAM preview keeps half as many GPU frames from then on. However many frame threads the viewer +runs, at most two of them start GPU render attempts at once. Shared-pool workers use the CPU +when both slots are busy rather than blocking Rayon helpers; dedicated single-thread preview +lanes wait for admission and remain on the GPU, so +working-texture pressure is bounded by concurrent attempts rather than the thread count (#3028). +A single large frame or retained frames can still exceed the available video memory. The same guard catches a frame's validation errors (an invalid texture, often left by a failed allocation): that frame renders on the CPU and the kept textures are freed, but the compositor stays on and, with no allocation failure before it, the RAM preview keeps its GPU frames (#1319). A frame whose render panics is @@ -433,12 +438,12 @@ sentinel is implemented here. Browser scope futures are not synchronously awaite forwards shared-device errors, while owned worker devices install their own handlers. On the web (WebGPU) the GPU composites viewer frames; steps that need a readback fall back to the CPU (the Info panel's pixel readout reads GPU frames back asynchronously). **GPU particles** -(`effects::psim`, `gpu::particles`): CC Particle World, CC Particle Systems II and Particle +(`effects::psim`, `gpu::particles`): Particle Emitter 3D, Particle Emitter 2D and Particle Playground cannons without interacting forces evolve every particle independently, so the GPU backend (reached through `EffectHost::particles` when the GPU compositor is active) simulates one particle per invocation from the CPU's birth schedule and keeps per-key state checkpoints on the GPU, like `SimCache`; the CPU simulation stays the oracle (tests compare ids and positions). -CC Particle World, CC Particle Systems II and CC Mr. Mercury sample their Birth Rate at every +Particle Emitter 3D, Particle Emitter 2D and Liquid Particles sample their Birth Rate at every step (`EffectHost::param_at`, #1373), so a keyed rate only decides the births of its own time; the rate is left out of the simulation key, and a checkpoint is resumed only with the same rate history (the CPU state's `hist`, the GPU checkpoint's births). The rate's keyframes are read at the step's exact layer time @@ -629,7 +634,7 @@ Unix milliseconds. The layer's Start Time becomes `dataStart + videoStart − fi (`videoStart` given, or the file's creation date on the desktop; reduced to a time of day when the data has no dates). -**Lumetri Scopes** (`raster::scopes`) are density plots of the viewer frame (rendered over the +**Video Scopes** (`raster::scopes`) are density plots of the viewer frame (rendered over the comp background) with BT.601 / BT.709 / BT.2020 luma and colour-difference coefficients. ## 5. Expressions diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 9f3d69ea5..9abc4b494 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -60,6 +60,26 @@ export formats, parity summary), `jobs.list` (Progress panel jobs, e.g. the back Every engine command is reachable through `engine.execute`, and every interactive widget of every panel registers an element id; both are enforced by `crates/ui-egui/tests/it/ui_agent_audit.rs`. +### Waiting for a render + +On native desktop builds, `engine.execute` with `renderQueue.render` and `wait: true` +(the default) keeps the window responsive while its reply waits for the render worker. +`script.run` invoked through the control channel also yields the window while +`app.project.renderQueue.render()` waits. JavaScript resumes after completion or cancellation; +the original request receives its result and accumulated output only after the continuation +finishes or shows a modal dialog. Several renders in one script keep the same request pending. +Headless scripts retain their synchronous behavior. + +While a script waits for rendering, the window shows progress and a **Stop Render** button +(`scriptRender.stop`). Other editing and script callbacks pause so they cannot enter the +suspended script's undo group. `ui.inspect`, `ui.elements`, command/menu listings, resize, +focus, and the engine commands `renderQueue.list`, `renderQueue.stop`, `editor.state`, +`app.capabilities`, `command.list`, `scriptui.list` and `scriptui.get` remain available. +Other control requests return an error. Use another connection for inspection or Stop, +since requests on one connection are answered in order. A window-close request is canceled +during this wait; stop the render and then close normally to use the unsaved-changes prompt. +The control transport's 60-second request timeout still applies to long renders. + ## UI state | Method | Params | Result | diff --git a/docs/effects-parity.md b/docs/effects-parity.md index c7f850834..93bdf4792 100644 --- a/docs/effects-parity.md +++ b/docs/effects-parity.md @@ -64,6 +64,7 @@ the GPU is performance, not parity. | Date | Change | Summary | |---|---|---| +| 2026-11-12 | minor | Parameters take After Effects' recorded valid ranges (hard range; defaults and sliders unchanged): Levels (Individual Controls) levels −10000..10000 and Gamma from 0; 4-Color Gradient Blend to 10000 and Jitter to 500 %; Change Color Hue Transform ±1800°; Noise HLS (and Auto) amounts to 1000 % and Grain Size from 0; Grid Border to 2000; Venetian Blinds Width 1..32000 and Feather to 32000; Radial Wipe Feather to 32000; Scatter Amount to 1000; Beam thicknesses to 4000; Transform Scale ±30000 %; Turbulent Displace Random Seed to 100000. A test checks every mapped parameter's range against the recorded table (CPU and GPU) | | 2026-10-19 | minor | Glow (original colours): bright pass per channel, 8 × (value − threshold + 0.061), measured against After Effects 2026 (CPU and GPU) | | 2026-10-19 | minor | Glow (colour channels): the glow covers the layer's blurred alpha, blur σ = 0.384 × radius, measured against After Effects 2026 renders (CPU and GPU) | | 2026-10-19 | minor | Radial Blur repeats the edge instead of sampling transparency outside the layer, measured against After Effects 2026 renders (CPU; Radial Blur has no GPU path) | diff --git a/docs/effects.md b/docs/effects.md index d9f726481..723edd0d0 100644 --- a/docs/effects.md +++ b/docs/effects.md @@ -48,21 +48,21 @@ Effects are addressed by id (`ec..`) or display name in commands | Effect | Id | Params | GPU | 32 | Status | |---|---|---:|:-:|:-:|---| | Bilateral Blur | `ec.blur.bilateral` | 3 | GPU | 32 | Implemented | -| CC Cross Blur | `ec.blur.cccross` | 3 | GPU | 32 | Implemented | -| CC Radial Blur | `ec.blur.ccradial` | 4 | GPU | 32 | Implemented | -| CC Radial Fast Blur | `ec.blur.ccradialfast` | 3 | GPU | 32 | Implemented | -| CC Vector Blur | `ec.blur.ccvector` | 4 | GPU | 32 | Implemented | | Camera Lens Blur | `ec.blur.cameralens` | 16 | GPU | 32 | Implemented | | Camera-Shake Deblur | `ec.blur.camerashakedeblur` | 8 | | 32 | Implemented | | Channel Blur | `ec.blur.channel` | 6 | GPU | 32 | Implemented | | Compound Blur | `ec.blur.compound` | 4 | GPU | 32 | Implemented | +| Cross Blur | `ec.blur.cccross` | 3 | GPU | 32 | Implemented | | Directional Blur | `ec.blur.directional` | 2 | GPU | 32 | Implemented | | Fast Box Blur | `ec.blur.fastbox` | 4 | GPU | 32 | Implemented | +| Fast Radial Blur | `ec.blur.ccradialfast` | 3 | GPU | 32 | Implemented | | Gaussian Blur | `ec.blur.gaussian` | 3 | GPU | 32 | Implemented | | Radial Blur | `ec.blur.radial` | 4 | GPU | 32 | Implemented | | Sharpen | `ec.blur.sharpen` | 1 | GPU | 32 | Implemented | | Smart Blur | `ec.blur.smart` | 3 | GPU | 32 | Implemented | | Unsharp Mask | `ec.blur.unsharp` | 3 | GPU | 32 | Implemented | +| Variable Radial Blur | `ec.blur.ccradial` | 4 | GPU | 32 | Implemented | +| Vector Blur | `ec.blur.ccvector` | 4 | GPU | 32 | Implemented | ## Channel @@ -70,7 +70,6 @@ Effects are addressed by id (`ec..`) or display name in commands |---|---|---:|:-:|:-:|---| | Arithmetic | `ec.channel.arithmetic` | 5 | GPU | 32 | Implemented | | Blend | `ec.channel.blend` | 4 | GPU | 32 | Implemented | -| CC Composite | `ec.channel.cccomposite` | 3 | GPU | 32 | Implemented | | Calculations | `ec.channel.calculations` | 9 | GPU | 32 | Implemented | | Channel Combiner | `ec.channel.combiner` | 6 | GPU | 32 | Implemented | | Compound Arithmetic | `ec.channel.compoundarithmetic` | 6 | GPU | 32 | Implemented | @@ -81,6 +80,7 @@ Effects are addressed by id (`ec..`) or display name in commands | Set Matte | `ec.channel.setmatte` | 6 | GPU | 32 | Implemented | | Shift Channels | `ec.channel.shiftchannels` | 4 | GPU | 32 | Implemented | | Solid Composite | `ec.channel.solidcomposite` | 4 | GPU | 32 | Implemented | +| Source Composite | `ec.channel.cccomposite` | 3 | GPU | 32 | Implemented | ## Color Correction @@ -92,18 +92,18 @@ Effects are addressed by id (`ec..`) or display name in commands | Black & White | `ec.color.blackwhite` | 8 | GPU | 32 | Implemented | | Brightness & Contrast | `ec.color.brightnesscontrast` | 3 | GPU | 32 | Implemented | | Broadcast Colors | `ec.color.broadcast` | 3 | GPU | 32 | Implemented | -| CC Color Neutralizer | `ec.color.cccolorneutralizer` | 11 | GPU | 32 | Implemented | -| CC Color Offset | `ec.color.cccoloroffset` | 4 | GPU | 32 | Implemented | -| CC Kernel | `ec.color.cckernel` | 12 | GPU | 32 | Implemented | -| CC Toner | `ec.color.cctoner` | 7 | GPU | 32 | Implemented | | Change Color | `ec.color.changecolor` | 9 | GPU | 32 | Implemented | | Change to Color | `ec.color.changetocolor` | 9 | GPU | 32 | Implemented | | Channel Mixer | `ec.color.channelmixer` | 13 | GPU | 32 | Implemented | +| Channel Offset | `ec.color.cccoloroffset` | 4 | GPU | 32 | Implemented | | Color Balance | `ec.color.colorbalance` | 10 | GPU | 32 | Implemented | | Color Balance (HLS) | `ec.color.colorbalancehls` | 3 | GPU | 32 | Implemented | +| Color Grading | `ec.color.lumetri` | 66 | GPU | 32 | Implemented | | Color Link | `ec.color.colorlink` | 6 | GPU | 32 | Implemented | +| Color Neutralizer | `ec.color.cccolorneutralizer` | 11 | GPU | 32 | Implemented | | Color Stabilizer | `ec.color.colorstabilizer` | 6 | GPU | 32 | Implemented | | Colorama | `ec.color.colorama` | 20 | GPU | 32 | Implemented | +| Convolution Kernel | `ec.color.cckernel` | 12 | GPU | 32 | Implemented | | Curves | `ec.color.curves` | 0 | GPU | 32 | Implemented | | Equalize | `ec.color.equalize` | 2 | GPU | 32 | Implemented | | Exposure | `ec.color.exposure` | 14 | GPU | 32 | Implemented | @@ -112,7 +112,7 @@ Effects are addressed by id (`ec..`) or display name in commands | Leave Color | `ec.color.leavecolor` | 5 | GPU | 32 | Implemented | | Levels | `ec.color.levels` | 36 | GPU | 32 | Implemented | | Levels (Individual Controls) | `ec.color.levelsic` | 27 | GPU | 32 | Implemented | -| Lumetri Color | `ec.color.lumetri` | 66 | GPU | 32 | Implemented | +| Multi-Tone Tint | `ec.color.cctoner` | 7 | GPU | 32 | Implemented | | OCIO CDL Transform | `ec.color.ociocdl` | 12 | GPU | 32 | Implemented | | OCIO Color Space Transform | `ec.color.ociocolorspace` | 7 | GPU | 32 | Implemented | | OCIO Display Transform | `ec.color.ociodisplay` | 9 | GPU | 32 | Implemented | @@ -133,40 +133,40 @@ Effects are addressed by id (`ec..`) or display name in commands |---|---|---:|:-:|:-:|---| | Bezier Warp | `ec.distort.bezierwarp` | 13 | GPU | 32 | Implemented | | Bulge | `ec.distort.bulge` | 6 | GPU | 32 | Implemented | -| CC Bend It | `ec.distort.ccbendit` | 4 | GPU | 32 | Implemented | -| CC Bender | `ec.distort.ccbender` | 5 | GPU | 32 | Implemented | -| CC Blobbylize | `ec.distort.ccblobbylize` | 15 | GPU | 32 | Implemented | -| CC Flo Motion | `ec.distort.ccflomotion` | 7 | GPU | 32 | Implemented | -| CC Griddler | `ec.distort.ccgriddler` | 5 | GPU | 32 | Implemented | -| CC Lens | `ec.distort.cclens` | 3 | GPU | 32 | Implemented | -| CC Page Turn | `ec.distort.ccpageturn` | 7 | GPU | 32 | Implemented | -| CC Power Pin | `ec.distort.ccpowerpin` | 10 | GPU | 32 | Implemented | -| CC Ripple Pulse | `ec.distort.ccripplepulse` | 5 | GPU | 32 | Implemented | -| CC Slant | `ec.distort.ccslant` | 4 | GPU | 32 | Implemented | -| CC Smear | `ec.distort.ccsmear` | 4 | GPU | 32 | Implemented | -| CC Split | `ec.distort.ccsplit` | 3 | GPU | 32 | Implemented | -| CC Split 2 | `ec.distort.ccsplit2` | 4 | GPU | 32 | Implemented | -| CC Tiler | `ec.distort.cctiler` | 3 | GPU | 32 | Implemented | | Corner Pin | `ec.distort.cornerpin` | 4 | GPU | 32 | Implemented | | Detail-preserving Upscale | `ec.distort.upscale` | 4 | | 32 | Implemented | +| Directional Smear | `ec.distort.ccsmear` | 4 | GPU | 32 | Implemented | | Displacement Map | `ec.distort.displacementmap` | 8 | GPU | 32 | Implemented | +| Dual Lens Warp | `ec.distort.ccflomotion` | 7 | GPU | 32 | Implemented | +| Dual Split Warp | `ec.distort.ccsplit2` | 4 | GPU | 32 | Implemented | +| Flexible Bend | `ec.distort.ccbender` | 5 | GPU | 32 | Implemented | +| Lens Distortion | `ec.distort.cclens` | 3 | GPU | 32 | Implemented | | Liquify | `ec.distort.liquify` | 13 | GPU | 32 | Implemented | | Magnify | `ec.distort.magnify` | 10 | GPU | 32 | Implemented | | Mesh Warp | `ec.distort.meshwarp` | 3 | GPU | 32 | Implemented | | Mirror | `ec.distort.mirror` | 2 | GPU | 32 | Implemented | | Offset | `ec.distort.offset` | 2 | GPU | 32 | Implemented | | Optics Compensation | `ec.distort.opticscompensation` | 6 | GPU | 32 | Implemented | +| Organic Displacement | `ec.distort.ccblobbylize` | 15 | GPU | 32 | Implemented | +| Page Curl | `ec.distort.ccpageturn` | 7 | GPU | 32 | Implemented | +| Perspective Pin | `ec.distort.ccpowerpin` | 10 | GPU | 32 | Implemented | | Polar Coordinates | `ec.distort.polar` | 2 | GPU | 32 | Implemented | +| Pulsed Ripple | `ec.distort.ccripplepulse` | 5 | GPU | 32 | Implemented | | Puppet | `ec.distort.puppet` | 1 | | 32 | Implemented | | Reshape | `ec.distort.reshape` | 6 | GPU | 32 | Implemented | | Ripple | `ec.distort.ripple` | 7 | GPU | 32 | Implemented | | Rolling Shutter Repair | `ec.distort.rollingshutterrepair` | 5 | | 32 | Implemented | +| Scaled Tiles | `ec.distort.cctiler` | 3 | GPU | 32 | Implemented | +| Slant | `ec.distort.ccslant` | 4 | GPU | 32 | Implemented | | Smear | `ec.distort.smear` | 8 | GPU | 32 | Implemented | | Spherize | `ec.distort.spherize` | 2 | GPU | 32 | Implemented | +| Split Warp | `ec.distort.ccsplit` | 3 | GPU | 32 | Implemented | +| Tile Grid | `ec.distort.ccgriddler` | 5 | GPU | 32 | Implemented | | Transform | `ec.distort.transform` | 12 | GPU | 32 | Implemented | | Turbulent Displace | `ec.distort.turbulentdisplace` | 11 | GPU | 32 | Implemented | | Twirl | `ec.distort.twirl` | 3 | GPU | 32 | Implemented | | Twirl (Legacy) | `ec.distort.twirllegacy` | 3 | GPU | 32 | Implemented | +| Two-Point Bend | `ec.distort.ccbendit` | 4 | GPU | 32 | Implemented | | Warp | `ec.distort.warp` | 5 | GPU | 32 | Implemented | | Warp Stabilizer | `ec.distort.warpstabilizer` | 20 | | 32 | Implemented | | Wave Warp | `ec.distort.wavewarp` | 7 | GPU | 32 | Implemented | @@ -194,11 +194,6 @@ Effects are addressed by id (`ec..`) or display name in commands | Audio Spectrum | `ec.generate.audiospectrum` | 23 | GPU | 32 | Implemented | | Audio Waveform | `ec.generate.audiowaveform` | 17 | GPU | 32 | Implemented | | Beam | `ec.generate.beam` | 11 | GPU | 32 | Implemented | -| CC Glue Gun | `ec.generate.ccgluegun` | 7 | GPU | 32 | Implemented | -| CC Light Burst 2.5 | `ec.generate.cclightburst` | 6 | GPU | 32 | Implemented | -| CC Light Rays | `ec.generate.cclightrays` | 8 | GPU | 32 | Implemented | -| CC Light Sweep | `ec.generate.cclightsweep` | 9 | GPU | 32 | Implemented | -| CC Threads | `ec.generate.ccthreads` | 8 | GPU | 32 | Implemented | | Cell Pattern | `ec.generate.cellpattern` | 14 | GPU | 32 | Implemented | | Checkerboard | `ec.generate.checkerboard` | 10 | GPU | 32 | Implemented | | Circle | `ec.generate.circle` | 11 | GPU | 32 | Implemented | @@ -209,11 +204,16 @@ Effects are addressed by id (`ec..`) or display name in commands | Gradient Ramp | `ec.generate.gradientramp` | 7 | GPU | 32 | Implemented | | Grid | `ec.generate.grid` | 12 | GPU | 32 | Implemented | | Lens Flare | `ec.generate.lensflare` | 4 | GPU | 32 | Implemented | +| Light Rays | `ec.generate.cclightrays` | 8 | GPU | 32 | Implemented | +| Light Sweep | `ec.generate.cclightsweep` | 9 | GPU | 32 | Implemented | +| Paint Beads | `ec.generate.ccgluegun` | 7 | GPU | 32 | Implemented | | Paint Bucket | `ec.generate.paintbucket` | 11 | GPU | 32 | Implemented | +| Radial Light Burst | `ec.generate.cclightburst` | 6 | GPU | 32 | Implemented | | Radio Waves | `ec.generate.radiowaves` | 31 | GPU | 32 | Implemented | | Scribble | `ec.generate.scribble` | 25 | GPU | 32 | Implemented | | Stroke | `ec.generate.stroke` | 11 | GPU | 32 | Implemented | | Vegas | `ec.generate.vegas` | 26 | GPU | 32 | Implemented | +| Woven Threads | `ec.generate.ccthreads` | 8 | GPU | 32 | Implemented | | Write-on | `ec.generate.writeon` | 10 | GPU | 32 | Implemented | ## Immersive Video @@ -238,15 +238,15 @@ Effects are addressed by id (`ec..`) or display name in commands | Effect | Id | Params | GPU | 32 | Status | |---|---|---:|:-:|:-:|---| | Advanced Spill Suppressor | `ec.key.advancedspill` | 8 | GPU | 32 | Implemented | -| CC Simple Wire Removal | `ec.key.ccsimplewireremoval` | 7 | GPU | 32 | Implemented | | Color Difference Key | `ec.key.colordifference` | 16 | GPU | 32 | Implemented | | Color Range | `ec.key.colorrange` | 8 | GPU | 32 | Implemented | | Difference Matte | `ec.key.differencematte` | 6 | GPU | 32 | Implemented | | Extract | `ec.key.extract` | 6 | GPU | 32 | Implemented | | Inner/Outer Key | `ec.key.innerouter` | 88 | GPU | 32 | Implemented | | Key Cleaner | `ec.key.keycleaner` | 4 | GPU | 32 | Implemented | -| Key Light | `ec.keying.keylight` | 56 | GPU | 32 | Implemented | +| Line Removal | `ec.key.ccsimplewireremoval` | 7 | GPU | 32 | Implemented | | Linear Color Key | `ec.key.linearcolor` | 6 | GPU | 32 | Implemented | +| Screen Color Key | `ec.keying.keylight` | 56 | GPU | 32 | Implemented | | Screen Key | `ec.key.screen` | 11 | GPU | 32 | Implemented | | Unmult | `ec.key.unmult` | 6 | GPU | 32 | Implemented | @@ -255,12 +255,12 @@ Effects are addressed by id (`ec..`) or display name in commands | Effect | Id | Params | GPU | 32 | Status | |---|---|---:|:-:|:-:|---| | Matte Choker | `ec.matte.mattechoker` | 7 | GPU | 32 | Implemented | -| Mocha shape | `ec.obsolete.mochashape` | 6 | | 32 | Implemented | | Object Matte | `ec.matte.objectmatte` | 13 | | 32 | Implemented | | Refine Hard Matte | `ec.matte.refinehard` | 16 | GPU | 32 | Implemented | | Refine Soft Matte | `ec.matte.refinesoft` | 18 | GPU | 32 | Implemented | | Roto Brush & Refine Edge | `ec.matte.rotobrush` | 25 | | 32 | Implemented | | Simple Choker | `ec.matte.simplechoker` | 2 | GPU | 32 | Implemented | +| Tracked Shape Matte | `ec.obsolete.mochashape` | 6 | | 32 | Implemented | ## Noise & Grain @@ -308,65 +308,65 @@ Effects are addressed by id (`ec..`) or display name in commands | 3D Glasses | `ec.perspective.3dglasses` | 8 | GPU | 32 | Implemented | | Bevel Alpha | `ec.perspective.bevelalpha` | 4 | GPU | 32 | Implemented | | Bevel Edges | `ec.perspective.beveledges` | 4 | GPU | 32 | Implemented | -| CC Cylinder | `ec.perspective.cccylinder` | 15 | GPU | 32 | Implemented | -| CC Environment | `ec.perspective.ccenvironment` | 4 | GPU | 32 | Implemented | -| CC Sphere | `ec.perspective.ccsphere` | 14 | GPU | 32 | Implemented | -| CC Spotlight | `ec.perspective.ccspotlight` | 8 | GPU | 32 | Implemented | +| Cylinder Wrap | `ec.perspective.cccylinder` | 15 | GPU | 32 | Implemented | | Drop Shadow | `ec.perspective.dropshadow` | 6 | GPU | 32 | Implemented | +| Environment Map | `ec.perspective.ccenvironment` | 4 | GPU | 32 | Implemented | | Radial Shadow | `ec.perspective.radialshadow` | 9 | GPU | 32 | Implemented | +| Sphere Wrap | `ec.perspective.ccsphere` | 14 | GPU | 32 | Implemented | +| Surface Spotlight | `ec.perspective.ccspotlight` | 8 | GPU | 32 | Implemented | ## Simulation | Effect | Id | Params | GPU | 32 | Status | |---|---|---:|:-:|:-:|---| -| CC Ball Action | `ec.sim.ccballaction` | 8 | GPU | 32 | Implemented | -| CC Bubbles | `ec.sim.ccbubbles` | 7 | GPU | 32 | Implemented | -| CC Drizzle | `ec.sim.ccdrizzle` | 14 | GPU | 32 | Implemented | -| CC Hair | `ec.sim.cchair` | 20 | GPU | 32 | Implemented | -| CC Mr. Mercury | `ec.sim.ccmrmercury` | 23 | GPU | 32 | Implemented | -| CC Particle Systems II | `ec.sim.ccparticlesystems2` | 21 | GPU | 32 | Implemented | -| CC Particle World | `ec.sim.ccparticleworld` | 36 | GPU | 32 | Implemented | -| CC Pixel Polly | `ec.sim.ccpixelpolly` | 10 | GPU | 32 | Implemented | -| CC Rainfall | `ec.sim.ccrainfall` | 14 | GPU | 32 | Implemented | -| CC Scatterize | `ec.sim.ccscatterize` | 4 | GPU | 32 | Implemented | -| CC Snowfall | `ec.sim.ccsnowfall` | 19 | GPU | 32 | Implemented | -| CC Star Burst | `ec.sim.ccstarburst` | 6 | GPU | 32 | Implemented | +| Ball Grid | `ec.sim.ccballaction` | 8 | GPU | 32 | Implemented | +| Bubbles | `ec.sim.ccbubbles` | 7 | GPU | 32 | Implemented | | Card Dance | `ec.sim.carddance` | 53 | GPU | 32 | Implemented | | Caustics | `ec.sim.caustics` | 28 | GPU | 32 | Implemented | | Foam | `ec.sim.foam` | 36 | GPU | 32 | Implemented | +| Liquid Particles | `ec.sim.ccmrmercury` | 23 | GPU | 32 | Implemented | +| Particle Emitter 2D | `ec.sim.ccparticlesystems2` | 21 | GPU | 32 | Implemented | +| Particle Emitter 3D | `ec.sim.ccparticleworld` | 36 | GPU | 32 | Implemented | | Particle Playground | `ec.sim.particleplayground` | 92 | GPU | 32 | Implemented | +| Pixel Scatter | `ec.sim.ccscatterize` | 4 | GPU | 32 | Implemented | +| Pixel Shatter | `ec.sim.ccpixelpolly` | 10 | GPU | 32 | Implemented | +| Rainfall | `ec.sim.ccrainfall` | 14 | GPU | 32 | Implemented | | Shatter | `ec.sim.shatter` | 60 | GPU | 32 | Implemented | +| Snowfall | `ec.sim.ccsnowfall` | 19 | GPU | 32 | Implemented | +| Starfield | `ec.sim.ccstarburst` | 6 | GPU | 32 | Implemented | +| Surface Fibers | `ec.sim.cchair` | 20 | GPU | 32 | Implemented | +| Water Ripples | `ec.sim.ccdrizzle` | 14 | GPU | 32 | Implemented | | Wave World | `ec.sim.waveworld` | 35 | GPU | 32 | Implemented | ## Stylize | Effect | Id | Params | GPU | 32 | Status | |---|---|---:|:-:|:-:|---| +| Block Reveal | `ec.stylize.ccblockload` | 5 | GPU | 32 | Implemented | | Brush Strokes | `ec.stylize.brushstrokes` | 7 | GPU | 32 | Implemented | -| CC Block Load | `ec.stylize.ccblockload` | 5 | GPU | 32 | Implemented | -| CC Burn Film | `ec.stylize.ccburnfilm` | 3 | GPU | 32 | Implemented | -| CC Glass | `ec.stylize.ccglass` | 17 | GPU | 32 | Implemented | -| CC HexTile | `ec.stylize.cchextile` | 5 | GPU | 32 | Implemented | -| CC Kaleida | `ec.stylize.cckaleida` | 4 | GPU | 32 | Implemented | -| CC Mr. Smoothie | `ec.stylize.ccmrsmoothie` | 7 | GPU | 32 | Implemented | -| CC Plastic | `ec.stylize.ccplastic` | 18 | GPU | 32 | Implemented | -| CC RepeTile | `ec.stylize.ccrepetile` | 5 | GPU | 32 | Implemented | -| CC Threshold | `ec.stylize.ccthreshold` | 4 | GPU | 32 | Implemented | -| CC Threshold RGB | `ec.stylize.ccthresholdrgb` | 7 | GPU | 32 | Implemented | -| CC Vignette | `ec.stylize.ccvignette` | 4 | GPU | 32 | Implemented | | Cartoon | `ec.stylize.cartoon` | 12 | GPU | 32 | Implemented | +| Channel Threshold | `ec.stylize.ccthreshold` | 4 | GPU | 32 | Implemented | | Color Emboss | `ec.stylize.coloremboss` | 4 | GPU | 32 | Implemented | +| Edge Tile Repeat | `ec.stylize.ccrepetile` | 5 | GPU | 32 | Implemented | | Emboss | `ec.stylize.emboss` | 4 | GPU | 32 | Implemented | +| Film Burn | `ec.stylize.ccburnfilm` | 3 | GPU | 32 | Implemented | | Find Edges | `ec.stylize.findedges` | 2 | GPU | 32 | Implemented | +| Glass Relief | `ec.stylize.ccglass` | 17 | GPU | 32 | Implemented | | Glow | `ec.stylize.glow` | 14 | GPU | 32 | Implemented | +| Hexagonal Tiles | `ec.stylize.cchextile` | 5 | GPU | 32 | Implemented | +| Kaleidoscope | `ec.stylize.cckaleida` | 4 | GPU | 32 | Implemented | | Mosaic | `ec.stylize.mosaic` | 3 | GPU | 32 | Implemented | | Motion Tile | `ec.stylize.motiontile` | 8 | GPU | 32 | Implemented | +| Palette Map | `ec.stylize.ccmrsmoothie` | 7 | GPU | 32 | Implemented | +| Plastic Shading | `ec.stylize.ccplastic` | 18 | GPU | 32 | Implemented | | Posterize | `ec.stylize.posterize` | 1 | GPU | 32 | Implemented | +| RGB Threshold | `ec.stylize.ccthresholdrgb` | 7 | GPU | 32 | Implemented | | Roughen Edges | `ec.stylize.roughenedges` | 13 | GPU | 32 | Implemented | | Scatter | `ec.stylize.scatter` | 3 | GPU | 32 | Implemented | | Strobe Light | `ec.stylize.strobe` | 7 | GPU | 32 | Implemented | | Texturize | `ec.stylize.texturize` | 4 | GPU | 32 | Implemented | | Threshold | `ec.stylize.threshold` | 1 | GPU | 32 | Implemented | +| Vignette | `ec.stylize.ccvignette` | 4 | GPU | 32 | Implemented | ## Text @@ -379,11 +379,11 @@ Effects are addressed by id (`ec..`) or display name in commands | Effect | Id | Params | GPU | 32 | Status | |---|---|---:|:-:|:-:|---| -| CC Force Motion Blur | `ec.time.ccforcemotionblur` | 4 | GPU | 32 | Implemented | -| CC Wide Time | `ec.time.ccwidetime` | 3 | GPU | 32 | Implemented | | Echo | `ec.time.echo` | 5 | GPU | 32 | Implemented | | Pixel Motion Blur | `ec.time.pixelmotionblur` | 4 | GPU | 32 | Implemented | | Posterize Time | `ec.time.posterizetime` | 1 | GPU | 32 | Implemented | +| Temporal Blend | `ec.time.ccwidetime` | 3 | GPU | 32 | Implemented | +| Temporal Motion Blur | `ec.time.ccforcemotionblur` | 4 | GPU | 32 | Implemented | | Time Difference | `ec.time.timedifference` | 5 | GPU | 32 | Implemented | | Time Displacement | `ec.time.timedisplacement` | 4 | GPU | 32 | Implemented | | Timewarp | `ec.time.timewarp` | 28 | GPU | 32 | Implemented | @@ -393,29 +393,28 @@ Effects are addressed by id (`ec..`) or display name in commands | Effect | Id | Params | GPU | 32 | Status | |---|---|---:|:-:|:-:|---| | Block Dissolve | `ec.transition.blockdissolve` | 5 | GPU | 32 | Implemented | -| CC Glass Wipe | `ec.transition.ccglasswipe` | 5 | GPU | 32 | Implemented | -| CC Grid Wipe | `ec.transition.ccgridwipe` | 7 | GPU | 32 | Implemented | -| CC Image Wipe | `ec.transition.ccimagewipe` | 7 | GPU | 32 | Implemented | -| CC Jaws | `ec.transition.ccjaws` | 6 | GPU | 32 | Implemented | -| CC Light Wipe | `ec.transition.cclightwipe` | 8 | GPU | 32 | Implemented | -| CC Line Sweep | `ec.transition.cclinesweep` | 5 | GPU | 32 | Implemented | -| CC Radial ScaleWipe | `ec.transition.ccradialscalewipe` | 3 | GPU | 32 | Implemented | -| CC Scale Wipe | `ec.transition.ccscalewipe` | 3 | GPU | 32 | Implemented | -| CC Twister | `ec.transition.cctwister` | 5 | GPU | 32 | Implemented | -| CC WarpoMatic | `ec.transition.ccwarpomatic` | 8 | GPU | 32 | Implemented | | Card Wipe | `ec.transition.cardwipe` | 44 | GPU | 32 | Implemented | +| Glass Wipe | `ec.transition.ccglasswipe` | 5 | GPU | 32 | Implemented | | Gradient Wipe | `ec.transition.gradientwipe` | 5 | GPU | 32 | Implemented | +| Grid Wipe | `ec.transition.ccgridwipe` | 7 | GPU | 32 | Implemented | | Iris Wipe | `ec.transition.iriswipe` | 7 | GPU | 32 | Implemented | +| Light Wipe | `ec.transition.cclightwipe` | 8 | GPU | 32 | Implemented | +| Line Sweep | `ec.transition.cclinesweep` | 5 | GPU | 32 | Implemented | | Linear Wipe | `ec.transition.linearwipe` | 3 | GPU | 32 | Implemented | +| Luminance Image Wipe | `ec.transition.ccimagewipe` | 7 | GPU | 32 | Implemented | +| Radial Scale Wipe | `ec.transition.ccradialscalewipe` | 3 | GPU | 32 | Implemented | | Radial Wipe | `ec.transition.radialwipe` | 5 | GPU | 32 | Implemented | +| Scale Wipe | `ec.transition.ccscalewipe` | 3 | GPU | 32 | Implemented | +| Serrated Wipe | `ec.transition.ccjaws` | 6 | GPU | 32 | Implemented | +| Twist Wipe | `ec.transition.cctwister` | 5 | GPU | 32 | Implemented | | Venetian Blinds | `ec.transition.venetian` | 4 | GPU | 32 | Implemented | +| Warp Dissolve | `ec.transition.ccwarpomatic` | 8 | GPU | 32 | Implemented | ## Utility | Effect | Id | Params | GPU | 32 | Status | |---|---|---:|:-:|:-:|---| | Apply Color LUT | `ec.utility.applylut` | 1 | GPU | 32 | Implemented | -| CC Overbrights | `ec.utility.ccoverbrights` | 2 | GPU | 32 | Implemented | | Cineon Converter | `ec.utility.cineon` | 7 | GPU | 32 | Implemented | | Color Profile Converter | `ec.utility.colorprofileconverter` | 7 | GPU | 32 | Implemented | | Face Measurements | `ec.utility.facemeasurements` | 14 | | 32 | Implemented | @@ -423,4 +422,5 @@ Effects are addressed by id (`ec..`) or display name in commands | Grow Bounds | `ec.utility.growbounds` | 1 | GPU | 32 | Implemented | | HDR Compander | `ec.utility.hdrcompander` | 3 | GPU | 32 | Implemented | | HDR Highlight Compression | `ec.utility.hdrcompression` | 1 | GPU | 32 | Implemented | +| Out-of-Range Display | `ec.utility.ccoverbrights` | 2 | GPU | 32 | Implemented | diff --git a/docs/feature-naming.md b/docs/feature-naming.md new file mode 100644 index 000000000..a69bc713f --- /dev/null +++ b/docs/feature-naming.md @@ -0,0 +1,108 @@ +# Descriptive feature names + +Related to #1365. This is a naming cleanup, not a trademark clearance or a product rename. + +## Naming policy + +Use short, descriptive labels for EffectCraft implementations. Do not reuse a vendor prefix or +coined feature name as our branding. Qualify similar effects by what distinguishes them; each +English display name remains unique across the registry. + +- Renderer: **GPU Acceleration** / **Software Rendering**. +- Panel: **Video Scopes**. +- The table below covers the 69 former CC effects plus color grading, the shape matte and keying. + +## Compatibility contract + +Only presentation names change. Effect IDs, command IDs, parameter IDs, enum values, panel IDs, +serialized types and AEP import/export match names stay unchanged. Old effect display names +remain case-insensitive lookup and search aliases. Existing project instance names, custom names, +expressions, animation and values are not rewritten. The UI displays a current label for an exact +legacy default name, optionally followed by its numeric instance suffix; all other names stay +verbatim. A user-entered name identical to a legacy default cannot be distinguished from it, but +its saved value is still retained. + +External format specifications, comparison documents, attribution records and compatibility +fixtures may still name the external product or effect they actually describe. + +## Display mapping + +| Stable ID | Previous display name | Current display name | +|---|---|---| +| `ec.sim.ccballaction` | CC Ball Action | Ball Grid | +| `ec.distort.ccbendit` | CC Bend It | Two-Point Bend | +| `ec.distort.ccbender` | CC Bender | Flexible Bend | +| `ec.distort.ccblobbylize` | CC Blobbylize | Organic Displacement | +| `ec.stylize.ccblockload` | CC Block Load | Block Reveal | +| `ec.sim.ccbubbles` | CC Bubbles | Bubbles | +| `ec.stylize.ccburnfilm` | CC Burn Film | Film Burn | +| `ec.color.cccolorneutralizer` | CC Color Neutralizer | Color Neutralizer | +| `ec.color.cccoloroffset` | CC Color Offset | Channel Offset | +| `ec.channel.cccomposite` | CC Composite | Source Composite | +| `ec.blur.cccross` | CC Cross Blur | Cross Blur | +| `ec.perspective.cccylinder` | CC Cylinder | Cylinder Wrap | +| `ec.sim.ccdrizzle` | CC Drizzle | Water Ripples | +| `ec.perspective.ccenvironment` | CC Environment | Environment Map | +| `ec.distort.ccflomotion` | CC Flo Motion | Dual Lens Warp | +| `ec.time.ccforcemotionblur` | CC Force Motion Blur | Temporal Motion Blur | +| `ec.stylize.ccglass` | CC Glass | Glass Relief | +| `ec.transition.ccglasswipe` | CC Glass Wipe | Glass Wipe | +| `ec.generate.ccgluegun` | CC Glue Gun | Paint Beads | +| `ec.transition.ccgridwipe` | CC Grid Wipe | Grid Wipe | +| `ec.distort.ccgriddler` | CC Griddler | Tile Grid | +| `ec.sim.cchair` | CC Hair | Surface Fibers | +| `ec.stylize.cchextile` | CC HexTile | Hexagonal Tiles | +| `ec.transition.ccimagewipe` | CC Image Wipe | Luminance Image Wipe | +| `ec.transition.ccjaws` | CC Jaws | Serrated Wipe | +| `ec.stylize.cckaleida` | CC Kaleida | Kaleidoscope | +| `ec.color.cckernel` | CC Kernel | Convolution Kernel | +| `ec.distort.cclens` | CC Lens | Lens Distortion | +| `ec.generate.cclightburst` | CC Light Burst 2.5 | Radial Light Burst | +| `ec.generate.cclightrays` | CC Light Rays | Light Rays | +| `ec.generate.cclightsweep` | CC Light Sweep | Light Sweep | +| `ec.transition.cclightwipe` | CC Light Wipe | Light Wipe | +| `ec.transition.cclinesweep` | CC Line Sweep | Line Sweep | +| `ec.sim.ccmrmercury` | CC Mr. Mercury | Liquid Particles | +| `ec.stylize.ccmrsmoothie` | CC Mr. Smoothie | Palette Map | +| `ec.utility.ccoverbrights` | CC Overbrights | Out-of-Range Display | +| `ec.distort.ccpageturn` | CC Page Turn | Page Curl | +| `ec.sim.ccparticlesystems2` | CC Particle Systems II | Particle Emitter 2D | +| `ec.sim.ccparticleworld` | CC Particle World | Particle Emitter 3D | +| `ec.sim.ccpixelpolly` | CC Pixel Polly | Pixel Shatter | +| `ec.stylize.ccplastic` | CC Plastic | Plastic Shading | +| `ec.distort.ccpowerpin` | CC Power Pin | Perspective Pin | +| `ec.blur.ccradial` | CC Radial Blur | Variable Radial Blur | +| `ec.blur.ccradialfast` | CC Radial Fast Blur | Fast Radial Blur | +| `ec.transition.ccradialscalewipe` | CC Radial ScaleWipe | Radial Scale Wipe | +| `ec.sim.ccrainfall` | CC Rainfall | Rainfall | +| `ec.stylize.ccrepetile` | CC RepeTile | Edge Tile Repeat | +| `ec.distort.ccripplepulse` | CC Ripple Pulse | Pulsed Ripple | +| `ec.transition.ccscalewipe` | CC Scale Wipe | Scale Wipe | +| `ec.sim.ccscatterize` | CC Scatterize | Pixel Scatter | +| `ec.key.ccsimplewireremoval` | CC Simple Wire Removal | Line Removal | +| `ec.distort.ccslant` | CC Slant | Slant | +| `ec.distort.ccsmear` | CC Smear | Directional Smear | +| `ec.sim.ccsnowfall` | CC Snowfall | Snowfall | +| `ec.perspective.ccsphere` | CC Sphere | Sphere Wrap | +| `ec.distort.ccsplit` | CC Split | Split Warp | +| `ec.distort.ccsplit2` | CC Split 2 | Dual Split Warp | +| `ec.perspective.ccspotlight` | CC Spotlight | Surface Spotlight | +| `ec.sim.ccstarburst` | CC Star Burst | Starfield | +| `ec.generate.ccthreads` | CC Threads | Woven Threads | +| `ec.stylize.ccthreshold` | CC Threshold | Channel Threshold | +| `ec.stylize.ccthresholdrgb` | CC Threshold RGB | RGB Threshold | +| `ec.distort.cctiler` | CC Tiler | Scaled Tiles | +| `ec.color.cctoner` | CC Toner | Multi-Tone Tint | +| `ec.transition.cctwister` | CC Twister | Twist Wipe | +| `ec.blur.ccvector` | CC Vector Blur | Vector Blur | +| `ec.stylize.ccvignette` | CC Vignette | Vignette | +| `ec.transition.ccwarpomatic` | CC WarpoMatic | Warp Dissolve | +| `ec.time.ccwidetime` | CC Wide Time | Temporal Blend | +| `ec.color.lumetri` | Lumetri Color | Color Grading | +| `ec.obsolete.mochashape` | Mocha shape | Tracked Shape Matte | +| `ec.keying.keylight` | Key Light | Screen Color Key | + +`Roto Brush & Refine Edge` and `Warp Stabilizer` keep their current names; renaming them is +deferred. + +No effect algorithms or formats are changed. diff --git a/docs/file-format-parity.md b/docs/file-format-parity.md index 34485fd10..fd908a50f 100644 --- a/docs/file-format-parity.md +++ b/docs/file-format-parity.md @@ -22,7 +22,7 @@ trips and renders, checked by tests, not against After Effects), **partial** (na | Format | After Effects | EffectCraft read | Write | Fidelity / notes | Tested by | |---|---|---|---|---|---| -| Project `.aep` | R/W (native) | partial | partial | Main file format; the clean-room method is in [aep-format.md](aep-format.md), the maintainers' decision on it is open (#190). `crates/aep` reads and writes the RIFX container losslessly (byte-identical round trip) with a typed view of the decoded fields, from our own generated files only ([aep-format.md](aep-format.md)). File ▸ Import ▸ After Effects Project… (`file.importAep`, `effectcraft_aep::import`): folders, project settings, compositions, every layer kind, Transform, keyframes, expressions, masks, text with animators, shapes, cameras, lights, track mattes, markers, time remapping, layer styles, the built-in effects of a table shared with the exporter (Curves, Levels, Glow, Fractal Noise, Gradient Ramp, … and the expression controls), footage and image sequences; everything else is reported ([aep-import.md](aep-import.md)). File ▸ Export ▸ After Effects Project… (`file.exportAep`, `effectcraft_aep::export`) writes the same scope with every chunk built in code; exports were opened and read back in After Effects 2026 ([aep-export.md](aep-export.md)); everything else is reported. Effects outside the table (plug-ins, Lumetri, …) are not converted | `crates/aep` tests (`tests/container`, `tests/import`, `tests/export`) | +| Project `.aep` | R/W (native) | partial | partial | Main file format; the clean-room method is in [aep-format.md](aep-format.md), the maintainers' decision on it is open (#190). `crates/aep` reads and writes the RIFX container losslessly (byte-identical round trip) with a typed view of the decoded fields, from our own generated files only ([aep-format.md](aep-format.md)). File ▸ Import ▸ Adobe After Effects Project… (`file.importAep`, `effectcraft_aep::import`): folders, project settings, compositions, every layer kind, Transform, keyframes, expressions, masks, text with animators, shapes, cameras, lights, track mattes, markers, time remapping, layer styles, the built-in effects of a table shared with the exporter (Lumetri Color, Curves, Levels, Glow, Fractal Noise, Gradient Ramp, … and the expression controls), footage and image sequences; everything else is reported ([aep-import.md](aep-import.md)). File ▸ Export ▸ Adobe After Effects Project… (`file.exportAep`, `effectcraft_aep::export`) writes the same scope with every chunk built in code; exports were opened and read back in After Effects 2026 ([aep-export.md](aep-export.md)); everything else is reported. Effects outside the table (plug-ins, …) are not converted | `crates/aep` tests (`tests/container`, `tests/import`, `tests/export`) | | XML project `.aepx` | R/W | partial | none | Read into the same chunk tree as `.aep` by `crates/aep` (`read_aepx`; mapping in [aep-format.md](aep-format.md) section 1), so `file.importAep` imports it with the same coverage as `.aep`; not written | `crates/aep` `aepx` tests (container and import) | | Templates `.aet` / `.aetx` | R/W | none | none | Own `.ectemplate` (Home ▸ Templates, Save as Template) | `templates.rs` tests | | Motion Graphics template `.mogrt` | W (and R in Premiere) | none | none | Own `.ectemplate` Essential Graphics templates; FilmCraft rejects our `.fcgt` export (#611) | `essential.rs` tests | diff --git a/docs/footage.md b/docs/footage.md index 7841dc84c..7a0639c02 100644 --- a/docs/footage.md +++ b/docs/footage.md @@ -70,6 +70,9 @@ next to it. Agents: `file.importProject {"path": "/projects/titles.ecproj"}` returns the comps, the root comps, `primaryComp` (the only root comp, if there is one) and any missing media. +After Effects projects (`.aep`, `.aepx`) given to File ▸ Import ▸ File… or dropped on the window +import as File ▸ Import ▸ Adobe After Effects Project… does ([aep-import.md](aep-import.md)). + ## Finding a layer's source in the Project panel Right-click a layer in the Timeline or Composition panel and choose **Reveal Layer Source in diff --git a/docs/gaps.md b/docs/gaps.md index d481f9810..608ed60ed 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -61,7 +61,7 @@ are grouped by area; the issue tracker has the rest. | 2 | **Fidelity unmeasured** beyond scalar ease | [fidelity/](fidelity/README.md): 168 samples, one property type; `compare_frames` helper exists, no corpus | Every "done" feature may still differ; users find the differences | 60–100 h harness + P0/P1 corpus; more per feature | G1 · [target-app-parity.md](target-app-parity.md) | | 3 | **Open crash reports** | Open: #580, #501, #426, #341, #234, #879. Now closed: #591, #306, #511 | A crash loses work; outranks features ([AGENTS.md](../AGENTS.md)) | 10–25 h | G3 · this file | | 4 | ~~**No CI gate on pull requests**~~ closed | `.github/workflows/ci.yml` runs `cargo xtask ci` (fmt, clippy, tests, layers, assets, wasm) on every pull request and push to `main` through craft-common-actions' shared `gate.yml` (#2204) | — | done | G3 · this file | -| 5 | **Playback and scrubbing feel** | #595 stutter on cached frames (partly addressed: nearest-first RAM preview prefetch, cache bars at 1/3 resolution), #304, #504 / #335 audio late or crackling, #586 puppet lag (maintainers asked for details); #329 slow scrubbing (#784) and #554 closed | Preview is where motion designers spend their day | 25–50 h | G5 · [hardware-parity.md](hardware-parity.md) | +| 5 | **Playback and scrubbing feel** | #595 stutter on cached frames (partly addressed: nearest-first RAM preview prefetch, cache bars at 1/3 resolution; an uncached first playback plays at the rate frames render instead of freezing between bursts, with frame lanes four threads wide), #304, #504 / #335 audio late or crackling, #586 puppet lag (maintainers asked for details); #329 slow scrubbing (#784) and #554 closed | Preview is where motion designers spend their day | 25–50 h | G5 · [hardware-parity.md](hardware-parity.md) | | 6 | **Linux and Windows interaction bugs** | Open: #488 rename arrows (hardening landed, reporter asked for details), #492 anchor drag, #268 file drops on Wayland (winit), #613 Direct3D 12 compositor. Now closed: #491, #493, #467, #320, #302, #596 | Many reports come from Linux; some Windows GPUs fail to start the compositor | 25–45 h | G2 · [ui-parity.md](ui-parity.md) | | 7 | **Animation presets: 76 against 621** | After Effects ships 621 `.ffx` presets (Behaviors, Backgrounds, Shapes, Synthetics, Text, Transitions…); we ship 76 original presets (8 text-animator; 23 Transitions: wipes, clock and fan reveals, blinds, dissolves, card flips, CC wipes, fades, #757; 23 Backgrounds: drifting gradients, fog, cloud and smoke, plasma and magma, cells, hexagons, scrolling grids and checks, pulse rings, bokeh, light sweep, #1096, driven by expressions so they keep moving, not seamless loops; 22 Behaviors: wiggle, shake, float, orbit, bounce, drift, pendulum, spin, pulse, heartbeat, blink, strobe, flicker on transform and opacity, #1510) and read only our `.ecpreset`. Missing: Shapes, Synthetics, Image and more Text | Presets are how many designers start; tutorials assume them | 15–35 h (original presets; `.ffx` reading is out of clean-room scope) | G8 · [effects-parity.md](effects-parity.md) | | 8 | **Mask and shape bugs** | Open: #1004 mask vertex multi-select, #999 auto-pan, #1042 / #1033 vertex nodes under the bounding box, #959 / #960 shape group toggles and operators. Now closed: #510, #513, #527, #509, #303, #553, #932 (Convert To Bezier Path on a shape layer's parametric path) | Masks are core compositing | 10–20 h | G2 · [target-app-parity.md](target-app-parity.md) | @@ -266,6 +266,12 @@ Issues #41–#47 (all from the Linux AppImage 0.1.1, 5 October): panning the vie The second means reading another implementation of the format, which AGENTS.md rules out unless the clean-room scope allows it. - Also in this area: relative footage paths and relinking when a project moves (gap 18). +- Placed PDF / Illustrator footage of an `.aep` is framed as After Effects places it (the ArtBox + clipped to the crop box, `/Rotate` ignored) and drawn to fill the size After Effects stored + (#3099), measured with hand-written PDFs named `.ai`. Not yet measured: files saved by + Illustrator itself (artboards, bleed, art past the artboard, private data), a CropBox without + an ArtBox, `.pdf` names, and EPS files (they keep their bounding box at their own size, not + the placed framing). - Done when: the decision is recorded and, if approved, a corpus of real-world-shaped projects opens with its comps, layers, keyframes, effects and expressions intact. diff --git a/docs/mcp.md b/docs/mcp.md index ccca7d2ba..9421e2609 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -28,7 +28,7 @@ The server provides the same core conventions as FilmCraft #28: | `command_run` | `id`, `params?` | Engine command result | | `command_batch` | `steps: [{id, params?}]`, `stop_on_error?` | `completed`, `failed`, per-step `results` | | `doc_inspect` | none | Project overview and active composition | -| `render_preview` | `comp?`, `time?`, `max_side?`, `transparent?` | PNG and frame information | +| `render_preview` | `comp?`, `time?` or `times?`, `columns?`, `labels?`, `max_side?`, `transparent?` | PNG and frame information; with `times`, one contact-sheet PNG and its layout (`columns`, `rows`, `cellWidth`, `cellHeight`, `width`, `height`) | `command_batch` stops at the first error by default; false continues. Each edit has its own undo step. The existing `batch` tool retains its atomic undo grouping and result references. diff --git a/docs/plugins.md b/docs/plugins.md index 3fd8cf944..290ed033c 100644 --- a/docs/plugins.md +++ b/docs/plugins.md @@ -11,7 +11,7 @@ Guide): | After Effects | EffectCraft | | |---|---|---| -| Effect plug-ins (C/C++ SDK, `.aex` / `.plugin`) | [Effect plug-ins](#effect-plug-ins): WebAssembly modules (plug-in API v1) or Rust; the `Plug-ins` folder next to the settings loads at start-up | Different ABI: After Effects SDK plug-ins can't run (G8) | +| Effect plug-ins (C/C++ SDK, `.aex` / `.plugin`) | [Effect plug-ins](#effect-plug-ins): WebAssembly modules (WebAssembly ABI 1 and 2) or Rust; the `Plug-ins` folder next to the settings loads at start-up | Different ABI: After Effects SDK plug-ins can't run (G8) | | Scripts folder: its scripts are listed in File ▸ Scripts | `Scripts/` in the settings folder (File ▸ Scripts ▸ Install Script File… copies a script there), listed in File ▸ Scripts with the bundled samples; a subfolder (one level) is a submenu named after it | Same | | File ▸ Scripts ▸ Run Script File… (`.jsx`, `.jsxbin`) | `.jsx` / `.js` | Gap: `.jsxbin` (an undocumented binary encoding) | | File ▸ Scripts ▸ Recent Script Files: the 10 most recently run scripts; Cmd+Option+Shift+D (Ctrl+Alt+Shift+D on Windows) reruns one | File ▸ Scripts ▸ Recent Script Files lists the 10 scripts run last (script files and File ▸ Scripts entries, newest first, kept in the settings); Cmd+Alt+Shift+D runs the most recent one again (`file.runScript {"recent": 0}`, a key that can be changed in Edit ▸ Keyboard Shortcuts) | Same | @@ -152,7 +152,7 @@ example of a keyframe tool. ## Effect plug-ins -EffectCraft loads third-party effects through a small, versioned plug-in API (**API version 2**; WebAssembly ABI 1). +EffectCraft loads third-party effects through a small, versioned plug-in API (**API version 2**; WebAssembly ABI 1 and 2). A plug-in effect behaves exactly like a built-in one: it is listed in Effects & Presets (and the Effect menu) under its category, applied by id (`effect.apply {"effect": "org.example.posterize"}`) or display name, its parameters are ordinary properties (keyframes, expressions, Effect Controls, @@ -178,7 +178,8 @@ author, source, params}]}`. * **Parameters** arrive flattened to `f64`s in the order they are declared: slider, angle, checkbox (0 / 1) and popup (0-based index) take one value, point two (already in **buffer pixels**), colour four (RGBA 0–1). -* **Time** is layer time in seconds. +* **Time** is layer time in seconds. A WebAssembly module written for ABI 2 also gets the + layer's neighbouring frames at other times (see [WebAssembly ABI](#webassembly-abi)). * Rendering must be **deterministic** and may run on several threads at once (a WebAssembly plug-in gets one instance per thread). * A failed render (an error code, a trap, running out of fuel) leaves the frame unchanged. @@ -216,22 +217,36 @@ Every plug-in describes itself with a JSON manifest: parameters; a slider needs `min ≤ default ≤ max` (and `sliderMin ≤ sliderMax`); the manifest is at most 1 MiB. -### WebAssembly ABI (v1) +### WebAssembly ABI -A module with **no imports** that exports: +A module with **no imports** exports: | export | signature | | |---|---|---| | `memory` | memory | linear memory shared with the host | -| `ec_api_version` | `() -> i32` | must return `1` | +| `ec_api_version` | `() -> i32` | `1` for ABI 1, `2` for ABI 2 (neighbouring frames) | | `ec_manifest_ptr`, `ec_manifest_len` | `() -> i32` | the UTF-8 JSON manifest in memory | | `ec_alloc` | `(bytes: i32) -> i32` | a buffer of at least `bytes` bytes, 8-byte aligned; it may reuse the previous one | -| `ec_render` | `(pixels: i32, width: i32, height: i32, params: i32, nparams: i32, time: f64, scale: f64) -> i32` | process the pixels in place; return 0 on success | +| `ec_render` (ABI 1) | `(pixels: i32, width: i32, height: i32, params: i32, nparams: i32, time: f64, scale: f64) -> i32` | process the pixels in place; return 0 on success | +| `ec_render2` (ABI 2) | `ec_render`'s arguments, then `prev: i32, prev_time: f64, next: i32, next_time: f64, flags: i32` | the same, with the neighbouring frames; return 0 on success | For each frame the host calls `ec_alloc` once for a buffer holding the parameters (`nparams` -little-endian `f64`s) followed by the pixels (`width × height × 4` little-endian `f32`s), fills it, -calls `ec_render`, and reads the pixels back. Each call gets a fuel budget proportional to the -frame size; a module that loops forever stops with an error instead of hanging the app. +little-endian `f64`s) followed by the pixels (`width × height × 4` little-endian `f32`s, +premultiplied RGBA, row-major), fills it, calls the module's render export, and reads the pixels +back. An ABI 2 module gets two more pixel buffers after the current frame: the previous and next +frames, resampled onto the current buffer's grid. At a frame boundary they are the frames before +and after it (`time ± 1/fps`); between frames they are the two frames that bracket `time`. + +The neighbour buffers are always allocated (zeroed when the layer has no pixels there, so a +module can read both without a null check). `flags` bit 0 is set when `prev` holds a real frame, +bit 1 when `next` does; `prev_time` / `next_time` are those frames' layer times in seconds (0 +when absent). The ABI version is the module's `ec_api_version` and is independent of the +manifest's `api` (the native plug-in API): a module can use ABI 2 while its manifest says +`"api": 1`. Because an ABI 2 module reads other times, its layer is keyed on the layer time in +the render cache; ABI 1 modules keep the time-independent key. + +Each call gets a fuel budget proportional to the frame size; a module that loops forever stops +with an error instead of hanging the app. #### Example @@ -324,8 +339,10 @@ register_plugin(Arc::new(Swap(manifest)))?; // before the UI builds its menus ## Plug-in API 2 (native plug-ins) API 2 is for Rust plug-ins and for hosts that bring effects from elsewhere (such as -[external effect helpers](external-helpers.md)); the WebAssembly ABI stays at 1. Manifests with `"api": 1` or `2` are accepted, -`effect.plugins.list` reports `api: 2`, and up to 1024 plug-in effects can be registered. +[external effect helpers](external-helpers.md)); the WebAssembly ABI is versioned separately +(ABI 2 adds the neighbouring frames, see [WebAssembly ABI](#webassembly-abi)). Manifests with +`"api": 1` or `2` are accepted, `effect.plugins.list` reports `api: 2`, and up to 1024 plug-in +effects can be registered. * **New parameter types**: `point3` (`default` `[x, y, z]`; x, y arrive in buffer pixels, z as is), `layer` (another layer of the composition; flattens to its id, -1 for none, read with @@ -354,9 +371,11 @@ API 2 is for Rust plug-ins and for hosts that bring effects from elsewhere (such ### Compatibility -The API version is checked when a plug-in loads: a manifest or module for another version is -refused with a message. Within a version the contract above does not change; additions (new -parameter types, new optional exports) bump the version. +The API version is checked when a plug-in loads: this build implements plug-in API 1 and 2 and +WebAssembly ABI 1 and 2; a manifest or module for another version is refused with a message. +ABI 1 modules load and render unchanged; ABI 2 modules add `ec_render2` and the neighbouring +frames. Within a version the contract above does not change; additions (new parameter types, +new optional exports) bump the version. ## Plug-in Library diff --git a/docs/preferences.md b/docs/preferences.md index b8bbb2bcf..c35d2c793 100644 --- a/docs/preferences.md +++ b/docs/preferences.md @@ -37,6 +37,7 @@ Commands (CLI, MCP, control channel): - `general.showToolTips`: Show Tool Tips - `general.viewerWheelScrubbing`: Scrub Composition Frames with Mouse Wheel (off by default). On: an unmodified mouse wheel over the Composition viewer steps frames, and the smoothed tail of that notch does not zoom or pan. Ctrl/Cmd+wheel and a trackpad still zoom and pan. - `general.createLayersAtCompStart`: Create Layers at Composition Start Time +- `general.fitNewLayersToWorkArea`: Fit New Layers to Work Area (off by default). On: new text, solid, shape, null and adjustment layers start at the work area start and end at its end, ahead of Create Layers at Composition Start Time. Footage and compositions keep their own length. - `general.defaultSpatialLinear`: Default Spatial Interpolation to Linear - `startup.showHomeOnLaunch`: Show Home Screen When Launching - `startup.offerCrashRecovery`: Offer to Open the Latest Auto-Save After a Crash @@ -52,10 +53,20 @@ Commands (CLI, MCP, control channel): - `autoSave.saveOnRenderStart`: Save When Starting Render Queue - `composition.showRenderingProgress`: Show Rendering Progress in Info Panel and Flowchart - `previews.adaptiveResolutionLimit`: Adaptive Resolution Limit (the lowest resolution Fast Previews ▸ - Adaptive Resolution drops to while you drag; a playing preview keeps the viewer's resolution) + Adaptive Resolution drops to while you drag, and the lowest a playing preview steps down to) +- `previews.adaptivePlayback`: Lower Resolution When Playback Can't Keep Up. Off by default (without it a first play renders + at the viewer's resolution at the rate frames arrive, and a looping preview plays its later loops + from the cache in real time, as in After Effects; with it the play stays lowered). A + preview at Resolution Auto that plays under 80 % of its frame rate (frames render slower than + that, so it plays them at the rate they arrive rather than freezing between them) steps + down a resolution (half the scale after two seconds, a quarter two seconds later if still slow, + no lower than the Adaptive Resolution Limit) for the rest of that play; stopping goes back to the viewer's resolution, and Cache + Frames When Idle fills it in. An explicit Resolution (the comp's or the Preview panel's) is + kept as set - `previews.cacheFramesWhenIdle`: Cache Frames When Idle (Composition ▸ Preview): after a second without input or edits, the viewer renders the work area into the RAM preview in the - background, from the current time on, until it is cached or the budget is full + background, from the current time on, until it is cached or the budget is full. On by default, + as in After Effects, so the first playback of a comp left alone for a second is already cached - `previews.fastPreviews`: Fast Previews (Draft 3D, Faster Effects) - `appearance.appearanceMode`: Appearance Mode (`auto` / `dark` / `light`). `auto` (Sync with System) follows the operating system's light or dark appearance while the app runs: on Linux the desktop app reads the XDG desktop portal (`org.freedesktop.appearance` `color-scheme`) once and then listens for its `SettingChanged` signal on a worker thread, repainting only when the value changes (nothing polls); `gsettings` runs at most once at start-up, by absolute path, when the portal gives no answer. Elsewhere it uses what the window system reports. With no answer it shows the dark theme. New installs stay `dark`; Sync with System is opt-in. The Tools bar's appearance button (a monitor, sun or moon) cycles Sync with System, Light and Dark (`view.theme.toggle`, Next Appearance Mode). - `appearance.darkTheme`: Dark Theme (`dark` / `darker`), shown in dark appearance @@ -82,6 +93,27 @@ Commands (CLI, MCP, control channel): - `memory.layerCacheMb`: Layer Cache (in the browser at most 512 MB, see [web.md](web.md)) - `memory.mediaCacheMb`: Footage Frame Cache (in the browser at most 256 MB) - `memory.previewCacheMb`: Preview (RAM) Cache +- `memory.gpuFramesInFlight`: GPU Frames Rendered at Once (`auto`, `1`, `2`, `3`): how many + frames the Render Queue renders on the GPU at once. Auto measures the GPU memory after the + job's first GPU frame and fits up to 3 frames into the GPU memory cap (by default a + conservative 3 GiB on a discrete card, less when its memory is known, 2 GiB on an integrated + one; `EFFECTCRAFT_GPU_MEMCAP_MB` sets it), keeping room for the upload cache to fill. It + renders one frame at a time where the memory can't be measured (Metal, WebGPU, GL, or the + cap turned off) and once frames have had to wait for memory. The render log names the count. + `EFFECTCRAFT_GPU_INFLIGHT` overrides it. CPU renders have no such setting. A job on GPU + Acceleration that renders on the CPU (it measured the CPU faster, or the GPU stopped) fits + its CPU frames in flight into half the memory the system has free, at the size a GPU frame + measured, from 1 to one per thread; the app log names the count. Other CPU renders (Software + Rendering projects) keep one frame per thread in flight, so their memory grows with the core + count. Set `EFFECTCRAFT_CPU_INFLIGHT` to a whole number of frames (at least 1) to cap the + frames in flight of every Render Queue render and export, `effectcraft-cli render` included + ([#1615](https://github.com/storytold/effectcraft/issues/1615)); previews don't read it. One + exception: while a job on GPU Acceleration times both compositors on its first frames, it + renders them under the cap but keeps them all (up to 12) until it has chosen. That timing + picks the compositor that a job of 12 frames or more renders every frame on (the render log + names it and why). `EFFECTCRAFT_RQ_RENDERER` set to `gpu` or `cpu` skips it and renders on + that compositor; `auto` (the default) measures. A queue item's later output modules reuse its + first one's choice, and Software Rendering projects always render on the CPU. - `threeD.defaultRenderer`: Default 3D Renderer - `labels.N.name` / `labels.N.color`: the 16 label names and colours, used by the Label menu, the timeline, project panel, render queue and viewer handles diff --git a/docs/testing.md b/docs/testing.md index e84a9a23b..34682d119 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -2,7 +2,8 @@ ```sh cargo test --workspace # everything -cargo xtask ci # fmt, clippy -D warnings, tests, pdf-deps, layers, assets, wasm +cargo xtask ci # fmt, clippy -D warnings, tests, pdf-deps, lock-patches, layers, assets, wasm +cargo xtask ci --affected # the same gates, scoped to crates your change can affect ``` The After Effects test projects of `crates/aep` are not in the repository; without them the @@ -19,6 +20,9 @@ cargo xtask aep-corpus EFFECTCRAFT_AEP_FIXTURES=corpus/aep EFFECTCRAFT_AEP_FIXTURES_REQUIRED=1 cargo test -p effectcraft-aep ``` +The container and typed-view tests are in `craft-aep` ([storytold/craft-libs](https://github.com/storytold/craft-libs)), +which reads the same corpus from `CRAFT_AEP_FIXTURES` (and `CRAFT_AEP_FIXTURES_REQUIRED`). + The `dev` profile keeps debug info to line tables (fast builds, readable backtraces). To debug interactively — local variables, parameters, column breakpoints — build with the `dev-full` profile: `cargo run --profile dev-full -p effectcraft -- --demo` (see `CONTRIBUTING.md`). @@ -28,10 +32,60 @@ profile: `cargo run --profile dev-full -p effectcraft -- --demo` (see `CONTRIBUT runs `missing_translations_report` with `--nocapture`, which prints the interface strings each language has no translation for. Those strings show in English; the report always passes. +Direct nextest runs, including archived coverage shards, serialise `effectcraft-gpu`, +`effectcraft-ui-egui`, `effectcraft-sam2` and the CLI adapter tests through the `gpu` test group +in `.config/nextest.toml`. Their process-local mutexes cannot coordinate nextest's separate +processes; competing software devices can time out during readback. UI harnesses can also +create a device lazily when rendering a screenshot. This also reduces UI and SAM2 test +parallelism in the normal gate. `cargo xtask ci` still runs the GPU library suite in one +process to reuse its device. Group limits apply within one nextest invocation, not across +separate invocations, and cannot prevent timeouts from other host workloads. + Each crate with several integration-test files has one test binary, `tests/it/main.rs`, whose modules are the files in `tests/it/`. Run one file with `cargo test -p --test it ::` or `cargo nextest run -p -E 'binary(it) and test(/^::/)'`. +## Affected-only CI + +Use `cargo xtask ci --affected` to run the gates for the crates a change can affect. The full +`cargo xtask ci` remains the required check before a release and when affected mode falls back +because a workspace-wide input changed. + +```sh +cargo xtask ci --affected # base: the merge-base of HEAD with main +cargo xtask ci --affected --base HEAD~3 # any git revision +cargo xtask ci --affected --dry-run # print the plan only +``` + +It collects files that differ from the base (committed, staged, unstaged and untracked), preserving +Unicode and whitespace in their names. Each file maps to the workspace package whose directory +holds it and every package whose sources point at it with a `../` literal, such as +`include_bytes!("../../../assets/fonts/Inter-Regular.ttf")` or +`concat!(env!("CARGO_MANIFEST_DIR"), "/../../docs/preferences.md")`. Deleted and renamed inputs +still select their readers, including when the input's entire directory no longer exists. + +The affected set is those packages plus everything that depends on them (transitively through +normal and build dependencies) and every package that uses one of them as a dev-dependency. +Clippy and tests use the same dev profile as full CI. Both modes use the same test runner: +nextest plus doctests when installed, otherwise cargo test plus doctests. GPU tests run in one +libtest process, and the translation report reruns with visible output when ui-egui is affected. +The audio-backend probe runs before building affected crates. + +Formatting, PDF dependency policy, lock-patch and layer checks always run. Assets are checked +when an asset, an `.attribution` sidecar or `ATTRIBUTION.md` changes. The wasm check selects +affected L0–L4 and web crates. Every step runs even when an earlier step fails; the final result +names failed steps and includes timings. + +Changes to the root `Cargo.toml`, `Cargo.lock`, `.cargo/`, `xtask/`, `rust-toolchain(.toml)`, +`clippy.toml` or `.config/nextest.toml` can change every crate's build or test policy, so they run +the full CI instead and say why. Changes no crate reads (most of `docs/`, `README.md`, `.github/`, +`packaging/`) only get the cheap checks. + +Limits: a crate that reads a repo file at run time through a path that is not a `../` literal in its +sources is not detected, and building a subset of packages can unify features differently from the +whole workspace. Non-UTF-8 Git paths stop the command rather than silently omitting checks. +Run the full `cargo xtask ci` before a release. + ## What the tests cover - **Engine commands** (`crates/engine/src/tests*.rs`): every command family, including undo and @@ -112,3 +166,56 @@ and line instead of being discarded. `--fail-over PCT` requires the same run key successful latest runs, and at least one comparable resource metric per run. Failed latest runs, missing coverage, empty results and wall-time-only measurements cannot pass the gate. Omit `--fail-over` for an exploratory partial comparison; failed latest runs remain visible. + +## Unexpected desktop exit diagnostics + +The desktop starts a separate, lightweight reporter before loading fonts, projects or the GPU. +A normal exit (including a handled startup error) sends a clean marker only after desktop +teardown. macOS finishes at the end of its successful exit hook because its native event loop +terminates without returning to main; other platforms finish after the event loop returns. +A teardown panic leaves reporting armed. An abort or other unexpected process termination +closes the pipe without that marker. +The helper saves a local report under the session log folder's `Crash Reports` directory and +shows a native error dialog. Control-server launches save the report without a modal dialog. +Reports contain version/platform, UTC detection and observer-start times, observed duration, +GUI uptime at the last target-present sample, and at most the last 64 KiB of the matching +session's log. A separate ring retains the last 32 timestamped, fixed-vocabulary backend and +GPU observations even if startup messages have fallen out of the log tail. `WGPU_BACKEND` +values are normalized to known backend names; arbitrary environment text is not copied. +Logging filters and the session file size cap can omit later events from the log tail. +They are never uploaded automatically. Review logs for private project paths before sharing. + +On Windows the helper acquires a process handle before acknowledging readiness and retains +it until the GUI exits. It records the real OS exit status in unsigned hexadecimal, with a +small lookup of known status names. These bits are **not a verified exception record**: +software can explicitly set the same code, and an access violation does not identify the +faulting module or prove a driver crash. Other platforms report exit status as unavailable. +A lost pipe while the GUI is still present, or a failed/absent process refresh, is not reported +as confirmed termination. + +The helper samples only the target process and system RAM at roughly one-second intervals, +retaining a count, last-attempt time, sampled resident-memory maximum, Windows private-commit +maximum (virtual memory on other platforms), and system available-RAM minimum. This is a +fixed-size summary, not a memory dump. OS query failure can leave stale or zero values, and +sampling misses short spikes. System commit limits/usage and GPU VRAM budgets are not captured. +No threshold is treated as proof of memory exhaustion. Typed wgpu OOM, internal/validation +errors and device-loss notifications are observations; device destruction is distinguished +from unknown loss, and neither establishes why the process later exited. + +Run `cargo test -p effectcraft crash_report::tests` for bounded protocol, normal shutdown, +actual subprocess abort, unrelated child handles, concurrent sessions, missing log/helper, +log rotation, bounded retained events, live-GUI pipe loss, and report-write failure coverage. +Windows also tests a real subprocess exit with explicit `0xC0000017` bits, without exhausting +memory or pretending that the exit was an actual OOM exception. The three ignored tests are subprocess entry +points launched by these tests, not manual tests. + +These are observations, not memory recovery or a causal diagnosis: a pipe closing does not +prove an out-of-memory failure. System-wide exhaustion, killing the whole process tree, +or an unavailable native dialog service can also prevent the helper from reporting. Existing +auto-save recovery remains responsible for restoring previously saved work. + +Native exception records, faulting-module attribution and driver/TDR confirmation require +separately collected OS event evidence or a native dump. They are deliberately not gathered +here: dumps can contain document contents and credentials. Any future dump workflow must be +explicitly opt-in, bounded, local-first and separately reviewed before sharing. This reporter +must remain useful when those sources are unavailable. diff --git a/docs/web.md b/docs/web.md index 666ffe37b..f4eb882a8 100644 --- a/docs/web.md +++ b/docs/web.md @@ -173,9 +173,9 @@ the same futures with `passes::block_on`; they never wait there). - **Render Queue**: `effectcraft_export::export_async` renders each frame in passes (frames with deferred readbacks render one by one instead of in parallel batches) and encodes as - before (`Exporter::export_async`). As on the desktop, a job renders every frame on the - project's renderer (the worker's GPU with Mercury GPU Acceleration), not on whichever - compositor measured faster, so runs don't differ by timing. + before (`Exporter::export_async`). Unlike the desktop, which times both compositors on a + job's first frames and keeps the faster, a job renders every frame on the project's renderer + (the worker's GPU with GPU Acceleration), so runs don't differ by timing. - **Analyses** (Warp Stabilizer, 3D Camera Tracker, Track Motion, mask and face tracking, Roto Brush freeze and propagation): their work functions are `async`; started with `wait` inside a job they are queued and awaited (`offload::run_or_queue`), and each input frame renders in diff --git a/examples/plugin-catalog/scripts/validate.sh b/examples/plugin-catalog/scripts/validate.sh index 40e37de7f..688ffdc26 100644 --- a/examples/plugin-catalog/scripts/validate.sh +++ b/examples/plugin-catalog/scripts/validate.sh @@ -26,12 +26,12 @@ file_url() { } echo "== 1. schema" -[ "$(wc -c <"$catalog")" -le $((4 << 20)) ] || fail "the catalog is larger than 4 MB (the app's limit)" +[[ "$(wc -c <"$catalog")" -le $((4 << 20)) ]] || fail "the catalog is larger than 4 MB (the app's limit)" # Exactly one object of format 1 with a packages array (an empty file or a second value fails too), # checked even when the schema step is skipped. jq -e -s 'length == 1 and (.[0] | type == "object" and .schema == 1 and (.packages | type == "array"))' "$catalog" >/dev/null \ || fail "$catalog isn't one JSON object with \"schema\": 1 and a \"packages\" array" -if [ "${SKIP_SCHEMA:-}" = 1 ]; then +if [[ "${SKIP_SCHEMA:-}" = 1 ]]; then echo "skipped (SKIP_SCHEMA=1)" else npx --yes ajv-cli@5.0.0 validate --spec=draft2020 --strict=false -s "$here/schema.json" -d "$catalog" @@ -39,7 +39,7 @@ fi echo "== 2. unique ids" dups=$(jq -r '.packages[].id' "$catalog" | sort | uniq -d) -[ -z "$dups" ] || fail "duplicate package ids: $dups" +[[ -z "$dups" ]] || fail "duplicate package ids: $dups" echo "ok" echo "== 3. downloads" @@ -51,15 +51,15 @@ while IFS=$'\t' read -r p r id version url sha size; do curl -fsSL --retry 2 --max-filesize $((64 << 20)) -o "$file" "$url" || fail "$id $version: download failed" got_size=$(wc -c <"$file" | tr -d ' ') got_sha=$(sha256sum "$file" | cut -d' ' -f1) - [ -z "$size" ] || [ "$size" = "$got_size" ] || fail "$id $version: size is $got_size, the catalog says $size" - [ "$got_sha" = "$(tr 'A-F' 'a-f' <<<"$sha")" ] || fail "$id $version: SHA-256 is $got_sha, the catalog says $sha" + [[ -z "$size" ]] || [[ "$size" = "$got_size" ]] || fail "$id $version: size is $got_size, the catalog says $size" + [[ "$got_sha" = "$(tr 'A-F' 'a-f' <<<"$sha")" ]] || fail "$id $version: SHA-256 is $got_sha, the catalog says $sha" local_catalog=$(jq --argjson p "$p" --argjson r "$r" --arg u "$(file_url "$file")" '.packages[$p].versions[$r].url = $u' <<<"$local_catalog") done < <(jq -r '.packages | to_entries[] | .key as $p | .value as $pkg | $pkg.versions | to_entries[] | [$p, .key, $pkg.id, .value.version, .value.url, .value.sha256, (.value.size // "")] | @tsv' "$catalog") echo "ok" echo "== 4. load with EffectCraft" -if [ -z "${EFFECTCRAFT_CLI:-}" ]; then +if [[ -z "${EFFECTCRAFT_CLI:-}" ]]; then echo "skipped (set EFFECTCRAFT_CLI to an effectcraft-cli build)" exit 0 fi diff --git a/examples/plugins/posterize-bands/Cargo.toml b/examples/plugins/posterize-bands/Cargo.toml index 825d704aa..65bbbaaaf 100644 --- a/examples/plugins/posterize-bands/Cargo.toml +++ b/examples/plugins/posterize-bands/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "posterize-bands" -description = "Example EffectCraft effect plug-in (WebAssembly, plug-in API v1): posterizes luminance into tinted bands" +description = "Example EffectCraft effect plug-in (WebAssembly ABI 1): posterizes luminance into tinted bands" version = "1.0.0" edition = "2024" license = "MIT OR Apache-2.0" diff --git a/examples/plugins/posterize-bands/src/lib.rs b/examples/plugins/posterize-bands/src/lib.rs index 9af709433..77026a16f 100644 --- a/examples/plugins/posterize-bands/src/lib.rs +++ b/examples/plugins/posterize-bands/src/lib.rs @@ -1,4 +1,4 @@ -//! Posterize Bands: an example EffectCraft effect plug-in (plug-in API v1, see +//! Posterize Bands: an example EffectCraft effect plug-in (WebAssembly ABI 1, see //! `docs/plugins.md`). It quantizes each pixel's luminance into `levels` bands and mixes a tint //! colour in by `mix` percent, optionally only above a `threshold`. //! diff --git a/packaging/linux/flatpak/ai.storyteller.effectcraft.bundle.yml b/packaging/linux/flatpak/ai.storyteller.effectcraft.bundle.yml index 03c4ee08f..f4ace60b8 100644 --- a/packaging/linux/flatpak/ai.storyteller.effectcraft.bundle.yml +++ b/packaging/linux/flatpak/ai.storyteller.effectcraft.bundle.yml @@ -17,6 +17,8 @@ runtime-version: '25.08' sdk: org.freedesktop.Sdk command: effectcraft finish-args: + # The shared updater inspects and updates this installation via flatpak-spawn --host. + - --talk-name=org.freedesktop.Flatpak - --filesystem=xdg-data/ArtCraft/versions:ro # Display: Wayland first; X11 only when no Wayland compositor is running. - --socket=wayland diff --git a/packaging/linux/flatpak/ai.storyteller.effectcraft.yml b/packaging/linux/flatpak/ai.storyteller.effectcraft.yml index 0797c5ea2..f36ff2851 100644 --- a/packaging/linux/flatpak/ai.storyteller.effectcraft.yml +++ b/packaging/linux/flatpak/ai.storyteller.effectcraft.yml @@ -20,6 +20,8 @@ sdk-extensions: - org.freedesktop.Sdk.Extension.rust-stable command: effectcraft finish-args: + # The shared updater inspects and updates this installation via flatpak-spawn --host. + - --talk-name=org.freedesktop.Flatpak - --filesystem=xdg-data/ArtCraft/versions:ro # Display: Wayland first; X11 only when no Wayland compositor is running. - --socket=wayland diff --git a/packaging/windows/test-legacy-fixture.ps1 b/packaging/windows/test-legacy-fixture.ps1 index 44336dca9..764ec41c3 100644 --- a/packaging/windows/test-legacy-fixture.ps1 +++ b/packaging/windows/test-legacy-fixture.ps1 @@ -8,7 +8,7 @@ $end = $source.IndexOf('$Msi = @{}', $start) if ($start -lt 0 -or $end -le $start) { throw 'Could not locate the legacy source transformation.' } # Run the test harness's exact source-producing block, with only scratch outputs. $block = $source.Substring($start, $end - $start).Replace('$PSScriptRoot', ("'" + $PSScriptRoot.Replace("'", "''") + "'")) -Invoke-Expression $block +. ([scriptblock]::Create($block)) [xml]$legacy = Get-Content -Raw $LegacyWxs $ns = New-Object Xml.XmlNamespaceManager($legacy.NameTable) $ns.AddNamespace('w', 'http://wixtoolset.org/schemas/v4/wxs') diff --git a/sonar-project.properties b/sonar-project.properties index 23a37f67e..eee8ac4bb 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,2 +1,23 @@ sonar.projectKey=storytold_effectcraft sonar.organization=storytold + +# crates/aep/tests/fixtures/tools/ holds the contributor's offline generators of the AEP test +# corpus (Node and ExtendScript run by hand against a local folder, see its README). They are not +# shipped or run by the app or CI, so they are not analysed. +sonar.exclusions=crates/aep/tests/fixtures/tools/** + +# S1523 / S2137 (eval, Function): the expression runtime in crates/expr/src/prelude.js replaces +# `eval` and `Function` on purpose, with wrappers that check the code they are given before it +# runs (After Effects expressions may call them). The rules flag exactly that wrapping. +# +# S7773 / S8786 (Number.isFinite and friends, String.trim): the .jsx scripts are After Effects +# ExtendScript, which is ES3. They also run in After Effects, which has neither. +sonar.issue.ignore.multicriteria=prelude_eval,prelude_redefine,jsx_es3_globals,jsx_es3_trim +sonar.issue.ignore.multicriteria.prelude_eval.ruleKey=javascript:S1523 +sonar.issue.ignore.multicriteria.prelude_eval.resourceKey=crates/expr/src/prelude.js +sonar.issue.ignore.multicriteria.prelude_redefine.ruleKey=javascript:S2137 +sonar.issue.ignore.multicriteria.prelude_redefine.resourceKey=crates/expr/src/prelude.js +sonar.issue.ignore.multicriteria.jsx_es3_globals.ruleKey=javascript:S7773 +sonar.issue.ignore.multicriteria.jsx_es3_globals.resourceKey=**/*.jsx +sonar.issue.ignore.multicriteria.jsx_es3_trim.ruleKey=javascript:S8786 +sonar.issue.ignore.multicriteria.jsx_es3_trim.resourceKey=**/*.jsx diff --git a/tests/repros/comp-zero-frame-rate.jsonl b/tests/repros/comp-zero-frame-rate.jsonl new file mode 100644 index 000000000..5ce9dd873 --- /dev/null +++ b/tests/repros/comp-zero-frame-rate.jsonl @@ -0,0 +1,13 @@ +// comp.new / comp.settings took a frameRate of 0 (or below 0.0005) as a 0/1 rate: the next +// frame-duration computation divided by zero and killed the process with the unsaved project +// (fixed in 780419c, "Compositions: reject zero and negative frame rates instead of panicking"). +{"start": "new"} +{"command": "comp.new", "params": {"name": "Zero", "frameRate": 0}, "expect_error": "frameRate"} +{"command": "comp.new", "params": {"name": "Tiny", "frameRate": 0.0001}, "expect_error": "frameRate"} +{"command": "comp.new", "params": {"name": "Negative", "frameRate": -24}, "expect_error": "frameRate"} +{"command": "comp.new", "params": {"name": "Main", "width": 320, "height": 180, "frameRate": 30, "duration": 1}} +{"command": "comp.settings", "params": {"frameRate": 0}, "expect_error": "frameRate"} +{"expect": {"query": "comp.info", "pointer": "/frameRate", "equals": 30}} +{"command": "layer.newSolid", "params": {"color": "#ff0000"}} +{"command": "time.set", "params": {"time": 0.5}} +{"expect_no_error": true} diff --git a/tests/repros/layer-settings-empty-selection.jsonl b/tests/repros/layer-settings-empty-selection.jsonl new file mode 100644 index 000000000..71a0216ad --- /dev/null +++ b/tests/repros/layer-settings-empty-selection.jsonl @@ -0,0 +1,8 @@ +// Layer Settings' enablement read the first selected layer with `selected_layers[0]`: with layers +// in the comp but none selected it indexed an empty list and panicked (fixed in b21e04b, M4.12). +{"start": "demo"} +{"command": "edit.deselectAll"} +{"command": "layer.settings", "expect_error": "select a layer"} +{"command": "layer.newSolid", "params": {"color": "#3366ff", "name": "Blue"}} +{"command": "layer.settings", "params": {"name": "Renamed"}} +{"expect_no_error": true} diff --git a/tests/repros/new-comp-from-selection-hostile-overlap.jsonl b/tests/repros/new-comp-from-selection-hostile-overlap.jsonl new file mode 100644 index 000000000..07e96afc6 --- /dev/null +++ b/tests/repros/new-comp-from-selection-hostile-overlap.jsonl @@ -0,0 +1,17 @@ +// File > New Comp from Selection with Sequence Layers: a huge `overlapDuration` times the layer +// count overflowed the duration arithmetic (a panic with overflow checks, a wrapped, wrong comp +// length without them), and `dimensionsFrom` past the selection indexed out of bounds +// (fixed in b21e04b, M4.12). +{"start": "new"} +{"command": "comp.new", "params": {"name": "Holder", "width": 64, "height": 64, "frameRate": 30, "duration": 2}} +{"command": "layer.newSolid", "params": {"color": "#ff0000", "name": "a"}} +{"command": "layer.newSolid", "params": {"color": "#00ff00", "name": "b"}} +{"command": "layer.newSolid", "params": {"color": "#0000ff", "name": "c"}} +{"command": "project.select", "params": {"items": ["a", "b", "c"]}} +{"command": "file.newCompFromSelection", "params": {"single": true, "sequence": true, "overlap": true, "overlapDuration": 1e30, "duration": 2}} +{"expect": {"query": "comp.info", "pointer": "/width", "equals": 64}} +{"command": "project.select", "params": {"items": ["a", "b", "c"]}} +{"command": "file.newCompFromSelection", "params": {"single": true, "sequence": true, "overlap": true, "overlapDuration": -5, "duration": 2}} +{"expect": {"query": "comp.info", "pointer": "/duration", "equals": 4}} +{"command": "project.select", "params": {"items": ["a", "b"]}} +{"command": "file.newCompFromSelection", "params": {"single": true, "dimensionsFrom": 99}, "expect_error": "dimensionsFrom"} diff --git a/xtask/src/affected.rs b/xtask/src/affected.rs new file mode 100644 index 000000000..af56ad0cb --- /dev/null +++ b/xtask/src/affected.rs @@ -0,0 +1,766 @@ +//! `cargo xtask ci --affected [--base REV] [--dry-run]`: the CI gates, run only for the crates a +//! change can affect (docs/testing.md). +//! +//! 1. Changed files: everything that differs from the base (default: the merge-base with `main`), +//! committed, staged, unstaged or untracked. +//! 2. Files map to the workspace package whose directory holds them, and to every package whose +//! sources point at them with a `../` path (`include_bytes!("../../../assets/fonts/…")`, +//! `env!("CARGO_MANIFEST_DIR")` + `"/../../docs/effects.md"`, …). +//! 3. Affected = those packages, every package that depends on them (transitively, through +//! normal and build dependencies) and every package that has one of them as a dev-dependency. +//! 4. Workspace-wide inputs (root `Cargo.toml`, `Cargo.lock`, `.cargo/`, `xtask/`, the toolchain +//! file, `clippy.toml`, `.config/nextest.toml`) fall back to the full `cargo xtask ci`. + +use std::collections::BTreeSet; +use std::path::Path; +use std::process::Command; +use std::time::{Duration, Instant}; + +use serde_json::Value; + +/// A workspace package, as far as change mapping cares. +#[derive(Debug, Clone, Default)] +pub struct Pkg { + pub name: String, + /// Package directory relative to the workspace root, `/`-separated, no trailing slash. + pub dir: String, + /// Workspace (path) dependencies: (package name, is a dev-dependency). + pub deps: Vec<(String, bool)>, + /// Has a library target (`--lib` is valid even when doctests are disabled). + pub has_lib: bool, + /// Has a library target with doctests (`cargo test --doc` runs for it). + pub has_doctests: bool, + /// Repo paths outside `dir` that the sources read (files or directories). + pub inputs: Vec, +} + +/// Changes to these force the full CI: they can change every crate's build or test policy. +pub fn full_ci_reason(path: &str) -> Option<&'static str> { + match path { + "Cargo.toml" => Some("workspace Cargo.toml changed"), + "Cargo.lock" => Some("Cargo.lock changed"), + "rust-toolchain" | "rust-toolchain.toml" => Some("toolchain changed"), + "clippy.toml" => Some("clippy.toml changed"), + // Test selection and required runner versions apply to the whole workspace. + ".config/nextest.toml" => Some("nextest configuration changed"), + _ if path.starts_with(".cargo/") => Some(".cargo/ changed"), + _ if path.starts_with("xtask/") => Some("xtask changed"), + _ => None, + } +} + +/// Files the `assets` check looks at (see `assets()` in main.rs). +pub fn is_asset_related(path: &str) -> bool { + path == "ATTRIBUTION.md" || path.ends_with(".attribution") || crate::is_asset(path) +} + +fn under(path: &str, dir: &str) -> bool { + dir.is_empty() || path == dir || path.strip_prefix(dir).is_some_and(|r| r.starts_with('/')) +} + +/// The package whose directory holds `path` (the innermost one). +pub fn owner<'a>(path: &str, pkgs: &'a [Pkg]) -> Option<&'a Pkg> { + pkgs.iter().filter(|p| !p.dir.is_empty() && under(path, &p.dir)).max_by_key(|p| p.dir.len()) +} + +/// Packages whose sources read `path` from outside their own directory. +pub fn readers<'a>(path: &'a str, pkgs: &'a [Pkg]) -> impl Iterator + 'a { + pkgs.iter().filter(move |p| p.inputs.iter().any(|i| under(path, i))) +} + +/// `direct` plus everything that depends on it: transitively through normal/build edges, then +/// one hop through dev-dependency edges (a dev-dependent's tests rerun, its dependents don't). +pub fn expand(direct: &BTreeSet, pkgs: &[Pkg]) -> BTreeSet { + let mut out = direct.clone(); + let mut stack: Vec = direct.iter().cloned().collect(); + while let Some(n) = stack.pop() { + for p in pkgs { + if !out.contains(&p.name) && p.deps.iter().any(|(d, dev)| !dev && *d == n) { + out.insert(p.name.clone()); + stack.push(p.name.clone()); + } + } + } + let dev: Vec = pkgs.iter().filter(|p| p.deps.iter().any(|(d, dev)| *dev && out.contains(d))).map(|p| p.name.clone()).collect(); + out.extend(dev); + out +} + +/// What `ci --affected` will do for a set of changed files. +#[derive(Debug, Default, PartialEq)] +pub struct Plan { + /// Set when the full CI must run instead. + pub full: Option, + /// Packages that hold or read a changed file. + pub direct: BTreeSet, + /// `direct` and its dependents: clippy and tests run for these. + pub affected: BTreeSet, + /// Whether the assets check runs. + pub assets: bool, + /// Changed files no package reads (docs, CI configs, packaging…). + pub unmapped: Vec, +} + +pub fn plan(files: &[String], pkgs: &[Pkg]) -> Plan { + let mut plan = Plan::default(); + for f in files { + if let Some(why) = full_ci_reason(f) { + plan.full = Some(format!("{why} ({f})")); + return plan; + } + plan.assets |= is_asset_related(f); + let mut hit = false; + for p in owner(f, pkgs).into_iter().chain(readers(f, pkgs)) { + plan.direct.insert(p.name.clone()); + hit = true; + } + if !hit { + plan.unmapped.push(f.clone()); + } + } + plan.affected = expand(&plan.direct, pkgs); + plan +} + +/// Normalise `base/rel` (both `/`-separated, relative to the repo root). None if it leaves the repo. +pub fn join_rel(base: &str, rel: &str) -> Option { + let mut parts: Vec<&str> = base.split('/').filter(|s| !s.is_empty() && *s != ".").collect(); + for c in rel.split('/') { + match c { + "" | "." => {} + ".." => { + parts.pop()?; + } + c => parts.push(c), + } + } + Some(parts.join("/")) +} + +/// The `../…` paths in a line of Rust: the part of each string literal from its first `../`, +/// cut back to a directory when a format placeholder follows (`"{}/../model/fixtures/{n}"`). +pub fn rel_paths(line: &str) -> Vec { + let mut out = vec![]; + for lit in line.split('"').skip(1).step_by(2) { + let Some(i) = lit.find("../") else { continue }; + let Some(rest) = lit.get(i..) else { continue }; + let p = match rest.find('{') { + Some(j) => rest.get(..j).and_then(|s| s.rfind('/').and_then(|k| s.get(..k))).unwrap_or(""), + None => rest, + }; + let p = p.trim_end_matches('/'); + if !p.is_empty() { + out.push(p.to_string()); + } + } + out +} + +/// Repo paths outside `pkg_dir` that a source file at `file` (both repo-relative) points at. +/// A literal may be relative to the file (`include_bytes!`) or to the manifest dir +/// (`CARGO_MANIFEST_DIR`), so both are tried. Keep existing paths and paths containing a change, +/// including deleted files and directories that no longer exist in the working tree. +pub fn inputs_of(file: &str, pkg_dir: &str, text: &str, known_input: &dyn Fn(&str) -> bool) -> BTreeSet { + let file_dir = file.rsplit_once('/').map(|(d, _)| d).unwrap_or(""); + let mut out = BTreeSet::new(); + for line in text.lines().filter(|l| l.contains("../")) { + let t = line.trim_start(); + if t.starts_with("//") && !t.starts_with("///") && !t.starts_with("//!") { + continue; + } + for rel in rel_paths(line) { + for base in [file_dir, pkg_dir] { + let Some(p) = join_rel(base, &rel) else { continue }; + if p.is_empty() || p == "target" || p.starts_with("target/") || under(&p, pkg_dir) || !known_input(&p) { + continue; + } + out.insert(p); + } + } + } + out +} + +fn rs_files(dir: &Path, out: &mut Vec, depth: u32) { + if depth > 32 { + return; + } + for e in std::fs::read_dir(dir).into_iter().flatten().flatten() { + let p = e.path(); + let name = e.file_name(); + if p.is_dir() { + if name != "target" && name != ".git" { + rs_files(&p, out, depth + 1); + } + } else if p.extension().is_some_and(|x| x == "rs") { + out.push(p); + } + } +} + +fn rel_to(root: &Path, p: &Path) -> String { + p.strip_prefix(root).unwrap_or(p).components().map(|c| c.as_os_str().to_string_lossy().into_owned()).collect::>().join("/") +} + +/// Workspace packages (xtask excluded) with their path dependencies and outside inputs. +pub fn packages(root: &Path, md: &Value, changed: &[String]) -> Vec { + let ws = md["workspace_root"].as_str().map(Path::new).unwrap_or(root); + let names: BTreeSet<&str> = md["packages"].as_array().into_iter().flatten().filter_map(|p| p["name"].as_str()).collect(); + let mut pkgs = vec![]; + for p in md["packages"].as_array().into_iter().flatten() { + let Some(name) = p["name"].as_str().filter(|n| *n != "xtask") else { continue }; + let Some(dir) = p["manifest_path"].as_str().and_then(|m| Path::new(m).parent()) else { continue }; + let deps = p["dependencies"] + .as_array() + .into_iter() + .flatten() + .filter(|d| !d["path"].is_null()) + .filter_map(|d| d["name"].as_str().filter(|n| names.contains(n)).map(|n| (n.to_string(), d["kind"] == "dev"))) + .collect(); + let lib_kind = |k: &Value| ["lib", "rlib", "dylib", "cdylib", "staticlib", "proc-macro"].iter().any(|l| k == *l); + let libs: Vec<&Value> = p["targets"].as_array().into_iter().flatten().filter(|t| t["kind"].as_array().into_iter().flatten().any(lib_kind)).collect(); + let has_lib = !libs.is_empty(); + let has_doctests = libs.iter().any(|t| t["doctest"] != false); + let rel = rel_to(ws, dir); + let mut files = vec![]; + rs_files(dir, &mut files, 0); + let known_input = |p: &str| ws.join(p).exists() || changed.iter().any(|f| under(f, p)); + let mut inputs = BTreeSet::new(); + for f in files { + if let Ok(text) = std::fs::read_to_string(&f) { + inputs.extend(inputs_of(&rel_to(ws, &f), &rel, &text, &known_input)); + } + } + pkgs.push(Pkg { name: name.to_string(), dir: rel, deps, has_lib, has_doctests, inputs: inputs.into_iter().collect() }); + } + pkgs +} + +fn git(root: &Path, args: &[&str]) -> Result { + let out = Command::new("git").arg("-C").arg(root).args(args).output().map_err(|e| format!("git: {e}"))?; + if !out.status.success() { + return Err(format!("git {}: {}", args.join(" "), String::from_utf8_lossy(&out.stderr).trim())); + } + String::from_utf8(out.stdout).map_err(|e| format!("git {}: output is not UTF-8; cannot safely map changed paths: {e}", args.join(" "))) +} + +/// The base commit: `--base REV` or the merge-base of HEAD with `main` (or `origin/main`). +fn base(root: &Path, rev: Option<&str>) -> Result<(String, String), String> { + if let Some(r) = rev { + let sha = git(root, &["rev-parse", "--verify", &format!("{r}^{{commit}}")])?; + return Ok((sha.trim().to_string(), r.to_string())); + } + for m in ["main", "origin/main"] { + if let Ok(sha) = git(root, &["merge-base", "HEAD", m]) { + return Ok((sha.trim().to_string(), format!("merge-base with {m}"))); + } + } + Err("no merge-base with main or origin/main; pass --base REV".into()) +} + +/// Committed since `base`, staged, unstaged and untracked files (repo-relative, sorted). +fn changed_files(root: &Path, base: &str) -> Result, String> { + let diff = git(root, &["diff", "--name-only", "--no-renames", "-z", base, "--"])?; + let untracked = git(root, &["ls-files", "--others", "--exclude-standard", "-z"])?; + // Git otherwise C-quotes Unicode, tabs and newlines. Whitespace is part of the filename. + let set: BTreeSet = diff.split('\0').chain(untracked.split('\0')).filter(|p| !p.is_empty()).map(str::to_string).collect(); + Ok(set.into_iter().collect()) +} + +fn short_list(names: &BTreeSet) -> String { + if names.is_empty() { "none".into() } else { names.iter().map(|n| crate::short(n)).collect::>().join(", ") } +} + +fn first_few(files: &[String]) -> String { + let mut s = files.iter().take(3).cloned().collect::>().join(", "); + if files.len() > 3 { + s.push_str(&format!(", … (+{})", files.len() - 3)); + } + s +} + +/// One CI step: label and the cargo invocation (None = an in-process check). +struct Step { + label: String, + cmd: Option, + check: Option Result<(), String>>>, +} + +fn cargo(args: &[&str], pkgs: &[&String], tail: &[&str]) -> Command { + let mut c = Command::new(env!("CARGO")); + c.args(args); + for p in pkgs { + c.args(["-p", p.as_str()]); + } + c.args(tail); + c +} + +/// Library tests and doctests are independent: a library can opt out of doctests. +fn test_targets(plan: &Plan, pkgs: &[Pkg]) -> (bool, bool) { + pkgs.iter().filter(|p| plan.affected.contains(&p.name)).fold((false, false), |(lib, docs), p| (lib || p.has_lib, docs || p.has_doctests)) +} + +fn steps(plan: &Plan, pkgs: &[Pkg], nextest: bool) -> Vec { + let mut v = vec![Step { label: "fmt".into(), cmd: Some(cargo(&["fmt", "--check"], &[], &[])), check: None }]; + let aff: Vec<&String> = plan.affected.iter().collect(); + if !aff.is_empty() { + v.push(Step { label: "audio backend".into(), cmd: None, check: Some(Box::new(crate::audio_backend)) }); + let n = aff.len(); + v.push(Step { label: format!("clippy ({n})"), cmd: Some(cargo(&["clippy", "--all-targets"], &aff, &["--", "-D", "warnings"])), check: None }); + let (has_lib, doctests) = test_targets(plan, pkgs); + let selected: Vec = aff.iter().map(|p| p.to_string()).collect(); + v.push(Step { label: format!("tests ({n})"), cmd: None, check: Some(Box::new(move || crate::tests_for(&selected, nextest, has_lib, doctests))) }); + } + v.push(Step { label: "pdf-deps".into(), cmd: None, check: Some(Box::new(|| crate::pdf_deps::run(&crate::root()))) }); + v.push(Step { label: "lock-patches".into(), cmd: None, check: Some(Box::new(|| crate::lock_patches::run(&crate::root()))) }); + v.push(Step { label: "layers".into(), cmd: None, check: Some(Box::new(crate::layers)) }); + if plan.assets { + v.push(Step { label: "assets".into(), cmd: None, check: Some(Box::new(crate::assets)) }); + } + let wasm: Vec<&String> = aff.iter().copied().filter(|a| crate::layer_of(a).is_some_and(|l| l <= 4) || crate::WEB_CRATES.contains(&a.as_str())).collect(); + if !wasm.is_empty() { + v.push(Step { label: format!("wasm ({})", wasm.len()), cmd: Some(cargo(&["check", "--target", "wasm32-unknown-unknown"], &wasm, &[])), check: None }); + } + v +} + +fn fmt_dur(d: Duration) -> String { + let s = d.as_secs(); + if s >= 60 { format!("{}m{:02}s", s / 60, s % 60) } else { format!("{:.1}s", d.as_secs_f64()) } +} + +fn summary(times: &[(String, Duration, bool)]) { + let total: Duration = times.iter().map(|t| t.1).sum(); + eprintln!("\nci --affected timing:"); + for (label, d, ok) in times { + eprintln!(" {:<16} {:>8} {}", label, fmt_dur(*d), if *ok { "ok" } else { "FAILED" }); + } + eprintln!(" {:<16} {:>8}", "total", fmt_dur(total)); +} + +pub fn run(root: &Path, args: &[String]) -> Result<(), String> { + let dry = args.iter().any(|a| a == "--dry-run"); + let rev = match args.iter().position(|a| a == "--base") { + Some(i) => Some(args.get(i + 1).map(String::as_str).ok_or("--base needs a revision")?), + None => None, + }; + let (sha, why) = base(root, rev)?; + let files = changed_files(root, &sha)?; + let md = crate::metadata()?; + let pkgs = packages(root, &md, &files); + let plan = plan(&files, &pkgs); + let short_sha = sha.get(..9).unwrap_or(&sha); + println!("ci --affected: base {short_sha} ({why}), {} changed file(s)", files.len()); + if let Some(reason) = &plan.full { + println!(" full CI: {reason}"); + if dry { + return Ok(()); + } + let t = Instant::now(); + let r = crate::ci(); + summary(&[("full ci".into(), t.elapsed(), r.is_ok())]); + return r; + } + println!(" changed: {}", short_list(&plan.direct)); + println!(" affected: {} ({} of {} crates)", short_list(&plan.affected), plan.affected.len(), pkgs.len()); + if !plan.unmapped.is_empty() { + println!(" read by no crate: {}", first_few(&plan.unmapped)); + } + let nextest = Command::new(env!("CARGO")).args(["nextest", "--version"]).output().is_ok_and(|o| o.status.success()); + let steps = steps(&plan, &pkgs, nextest); + let labels: Vec<&str> = steps.iter().map(|s| s.label.as_str()).collect(); + println!(" steps: {}{}", labels.join(", "), if plan.assets { "" } else { " (assets skipped: no asset changes)" }); + if dry { + return Ok(()); + } + let mut times = vec![]; + let mut failed = vec![]; + for mut s in steps { + let t = Instant::now(); + let r = match (s.cmd.as_mut(), s.check) { + (Some(c), _) => crate::run(c), + (None, Some(f)) => f(), + (None, None) => Ok(()), + }; + if let Err(e) = &r { + eprintln!("ci --affected: {} failed: {e}", s.label); + failed.push(s.label.clone()); + } + times.push((s.label, t.elapsed(), r.is_ok())); + } + summary(×); + if failed.is_empty() { Ok(()) } else { Err(format!("ci --affected failed: {}", failed.join(", "))) } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn pkg(name: &str, dir: &str, deps: &[(&str, bool)], inputs: &[&str]) -> Pkg { + Pkg { + name: name.into(), + dir: dir.into(), + deps: deps.iter().map(|(d, dev)| (d.to_string(), *dev)).collect(), + has_lib: true, + has_doctests: true, + inputs: inputs.iter().map(|s| s.to_string()).collect(), + } + } + + /// time ← render ← engine ← ui ← app; host has engine as a dev-dependency; text reads fonts. + fn ws() -> Vec { + vec![ + pkg("effectcraft-time", "crates/time", &[], &[]), + pkg("effectcraft-text", "crates/text", &[], &["assets/fonts"]), + pkg("effectcraft-render", "crates/render", &[("effectcraft-time", false), ("effectcraft-text", false)], &[]), + pkg("effectcraft-engine", "crates/engine", &[("effectcraft-render", false)], &["docs/preferences.md"]), + pkg("effectcraft-host", "crates/host", &[("effectcraft-time", false), ("effectcraft-engine", true)], &["crates/svg/tests/fixtures"]), + pkg("effectcraft-ui-egui", "crates/ui-egui", &[("effectcraft-engine", false)], &[]), + pkg("effectcraft", "apps/effectcraft", &[("effectcraft-ui-egui", false)], &[]), + pkg("effectcraft-cli", "apps/effectcraft-cli", &[("effectcraft", false)], &[]), + pkg("effectcraft-svg", "crates/svg", &[], &[]), + ] + } + + fn set(v: &[&str]) -> BTreeSet { + v.iter().map(|s| s.to_string()).collect() + } + + fn files(v: &[&str]) -> Vec { + v.iter().map(|s| s.to_string()).collect() + } + + #[test] + fn file_maps_to_innermost_package() { + let mut p = ws(); + p.push(pkg("nested", "crates/time/nested", &[], &[])); + assert_eq!(owner("crates/time/src/lib.rs", &p).map(|p| p.name.as_str()), Some("effectcraft-time")); + assert_eq!(owner("crates/time/nested/src/lib.rs", &p).map(|p| p.name.as_str()), Some("nested")); + // a prefix of the dir name is not a match + assert_eq!(owner("crates/timeline/src/lib.rs", &p).map(|p| p.name.as_str()), None); + assert_eq!(owner("apps/effectcraft-cli/src/main.rs", &p).map(|p| p.name.as_str()), Some("effectcraft-cli")); + assert_eq!(owner("apps/effectcraft/src/main.rs", &p).map(|p| p.name.as_str()), Some("effectcraft")); + assert!(owner("docs/testing.md", &p).is_none()); + } + + #[test] + fn reverse_deps_follow_normal_edges_and_one_dev_hop() { + let p = ws(); + // time: render, engine, ui, app, cli via normal edges; host directly; host is not + // a dependency of anything. + let a = expand(&set(&["effectcraft-time"]), &p); + assert_eq!( + a, + set(&["effectcraft-time", "effectcraft-render", "effectcraft-engine", "effectcraft-host", "effectcraft-ui-egui", "effectcraft", "effectcraft-cli"]) + ); + // ui-egui: only its dependents + assert_eq!(expand(&set(&["effectcraft-ui-egui"]), &p), set(&["effectcraft-ui-egui", "effectcraft", "effectcraft-cli"])); + // engine: host only through its dev-dependency, and nothing beyond host + let e = expand(&set(&["effectcraft-engine"]), &p); + assert!(e.contains("effectcraft-host")); + assert!(!e.contains("effectcraft-time") && !e.contains("effectcraft-render")); + assert_eq!(expand(&set(&["effectcraft-cli"]), &p), set(&["effectcraft-cli"])); + assert!(expand(&BTreeSet::new(), &p).is_empty()); + } + + #[test] + fn workspace_inputs_fall_back_to_full_ci() { + let p = ws(); + for f in ["Cargo.toml", "Cargo.lock", ".cargo/config.toml", "xtask/src/main.rs", "rust-toolchain.toml", "clippy.toml"] { + let pl = plan(&files(&["crates/time/src/lib.rs", f]), &p); + assert!(pl.full.is_some(), "{f}"); + } + // a crate manifest is not workspace-wide + assert_eq!(plan(&files(&["crates/time/Cargo.toml"]), &p).full, None); + assert_eq!(full_ci_reason("docs/xtask.md"), None); + } + + #[test] + fn nextest_policy_alone_requires_full_ci() { + let pl = plan(&files(&[".config/nextest.toml"]), &ws()); + assert_eq!(pl.full.as_deref(), Some("nextest configuration changed (.config/nextest.toml)")); + // Only the actual shared test input triggers this fallback, not every config file. + assert_eq!(full_ci_reason(".config/editor.toml"), None); + assert_eq!(full_ci_reason("docs/nextest.toml"), None); + } + + #[test] + fn docs_only_changes_affect_nothing() { + let pl = plan(&files(&["docs/gaps.md", "README.md", ".github/workflows/release.yml"]), &ws()); + assert_eq!(pl.full, None); + assert!(pl.affected.is_empty()); + assert!(!pl.assets); + assert_eq!(pl.unmapped.len(), 3); + } + + #[test] + fn files_read_from_outside_map_to_their_readers() { + let p = ws(); + // docs/preferences.md is read by an engine test + let pl = plan(&files(&["docs/preferences.md"]), &p); + assert_eq!(pl.direct, set(&["effectcraft-engine"])); + assert!(pl.unmapped.is_empty()); + // a font: read by text (directory input), and an asset + let pl = plan(&files(&["assets/fonts/Inter-Regular.ttf"]), &p); + assert_eq!(pl.direct, set(&["effectcraft-text"])); + assert!(pl.assets); + assert!(pl.affected.contains("effectcraft-cli")); + // svg fixtures belong to svg and are read by host + let pl = plan(&files(&["crates/svg/tests/fixtures/basic-shapes.svg"]), &p); + assert_eq!(pl.direct, set(&["effectcraft-svg", "effectcraft-host"])); + } + + #[test] + fn asset_rules() { + assert!(is_asset_related("ATTRIBUTION.md")); + assert!(is_asset_related("crates/engine/scripts/Rename Layers.jsx.attribution")); + assert!(is_asset_related("docs/brand/artcraft-mark.png")); + assert!(!is_asset_related("docs/testing.md")); + assert!(!is_asset_related("crates/time/src/lib.rs")); + } + + #[test] + fn rel_path_extraction() { + assert_eq!(rel_paths(r#"static F: &[u8] = include_bytes!("../../../assets/fonts/Inter-Regular.ttf");"#), ["../../../assets/fonts/Inter-Regular.ttf"]); + assert_eq!(rel_paths(r#"let p = concat!(env!("CARGO_MANIFEST_DIR"), "/../../docs/effects.md");"#), ["../../docs/effects.md"]); + assert_eq!(rel_paths(r#"format!("{}/../model/tests/fixtures/{n}", env!("CARGO_MANIFEST_DIR"))"#), ["../model/tests/fixtures"]); + assert_eq!(rel_paths(r#"let a = "x"; let b = "../y/z{n}.png";"#), ["../y"]); + assert!(rel_paths(r#"let s = "no parent here";"#).is_empty()); + } + + #[test] + fn rel_path_joining() { + assert_eq!(join_rel("crates/text/src", "../../../assets/fonts/A.ttf").as_deref(), Some("assets/fonts/A.ttf")); + assert_eq!(join_rel("crates/host", "../model/./tests").as_deref(), Some("crates/model/tests")); + assert_eq!(join_rel("crates", "../../outside"), None); + } + + #[test] + fn inputs_try_file_and_manifest_dirs() { + let existing = set(&["assets/fonts/Inter-Regular.ttf", "docs/effects.md", "crates/model/tests/fixtures", "crates/svg/tests/fixtures/a.svg"]); + let exists = |p: &str| existing.contains(p); + let src = r#" +static F: &[u8] = include_bytes!("../../../assets/fonts/Inter-Regular.ttf"); +let p = concat!(env!("CARGO_MANIFEST_DIR"), "/../../docs/effects.md"); +let fx = format!("{}/../model/tests/fixtures/{n}", env!("CARGO_MANIFEST_DIR")); +let own = include_bytes!("../tests/fixtures/own.svg"); +let out = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/test-out"); +// let commented = include_bytes!("../../svg/tests/fixtures/a.svg"); +"#; + let got = inputs_of("crates/host/src/lib.rs", "crates/host", src, &exists); + assert_eq!(got, set(&["assets/fonts/Inter-Regular.ttf", "docs/effects.md", "crates/model/tests/fixtures"])); + // integration tests in tests/ resolve include_bytes! against tests/ + let got = inputs_of("crates/host/tests/v.rs", "crates/host", r#"include_bytes!("../../svg/tests/fixtures/a.svg")"#, &exists); + assert_eq!(got, set(&["crates/svg/tests/fixtures/a.svg"])); + } + + fn committed_repo() -> tempfile::TempDir { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + git(root, &["init", "-q"]).unwrap(); + git(root, &["config", "core.quotepath", "true"]).unwrap(); + std::fs::write(root.join("README.md"), "fixture\n").unwrap(); + commit_fixture(root); + dir + } + + fn commit_fixture(root: &Path) { + git(root, &["add", "--all"]).unwrap(); + git(root, &["-c", "user.name=Test", "-c", "user.email=test@example.invalid", "-c", "commit.gpgsign=false", "commit", "--no-verify", "-qm", "fixture"]) + .unwrap(); + } + + #[test] + fn changed_files_preserve_unicode_in_tracked_and_untracked_paths() { + let dir = committed_repo(); + let root = dir.path(); + let tracked = "crates/time/src/caf\u{e9}.rs"; + let untracked = "crates/time/src/\u{65b0}\u{898f}.rs"; + std::fs::create_dir_all(root.join("crates/time/src")).unwrap(); + std::fs::write(root.join(tracked), "before").unwrap(); + commit_fixture(root); + std::fs::write(root.join(tracked), "after").unwrap(); + std::fs::write(root.join(untracked), "new").unwrap(); + let changed = changed_files(root, "HEAD").unwrap(); + assert_eq!(changed, files(&[tracked, untracked])); + assert_eq!(plan(&changed, &ws()).direct, set(&["effectcraft-time"])); + } + + #[cfg(unix)] + #[test] + fn changed_files_preserve_whitespace_quotes_and_backslashes() { + let dir = committed_repo(); + let root = dir.path(); + let tracked = " tracked\t\"\\name\n "; + let untracked = " untracked\t\"\\name\n "; + std::fs::write(root.join(tracked), "before").unwrap(); + commit_fixture(root); + std::fs::write(root.join(tracked), "after").unwrap(); + std::fs::write(root.join(untracked), "new").unwrap(); + assert_eq!(changed_files(root, "HEAD").unwrap(), files(&[tracked, untracked])); + } + + // macOS (APFS) refuses file names that aren't UTF-8 ("Illegal byte sequence"), so there the + // fixture can't be made and such a change can't happen. + #[cfg(all(unix, not(target_os = "macos")))] + #[test] + fn changed_files_reject_non_utf8_instead_of_silently_omitting_it() { + use std::os::unix::ffi::OsStringExt; + let dir = committed_repo(); + std::fs::write(dir.path().join(std::ffi::OsString::from_vec(b"invalid-\xff.rs".to_vec())), "new").unwrap(); + assert!(changed_files(dir.path(), "HEAD").unwrap_err().contains("not UTF-8")); + } + + #[test] + fn deleted_and_renamed_shared_inputs_still_select_their_readers() { + for rename in [false, true] { + let dir = committed_repo(); + let root = dir.path(); + std::fs::create_dir_all(root.join("crates/engine/src")).unwrap(); + std::fs::create_dir_all(root.join("docs/fixtures")).unwrap(); + std::fs::write(root.join("docs/preferences.md"), "preferences").unwrap(); + std::fs::write(root.join("docs/fixtures/example.json"), "{}").unwrap(); + std::fs::write( + root.join("crates/engine/src/lib.rs"), + r#" +const PREFS: &str = include_str!("../../../docs/preferences.md"); +fn fixture(n: &str) -> String { format!("{}/../../docs/fixtures/{n}", env!("CARGO_MANIFEST_DIR")) } +"#, + ) + .unwrap(); + commit_fixture(root); + if rename { + std::fs::rename(root.join("docs/preferences.md"), root.join("docs/renamed.md")).unwrap(); + std::fs::rename(root.join("docs/fixtures"), root.join("docs/moved")).unwrap(); + } else { + std::fs::remove_file(root.join("docs/preferences.md")).unwrap(); + std::fs::remove_dir_all(root.join("docs/fixtures")).unwrap(); + } + let changed = changed_files(root, "HEAD").unwrap(); + let md = serde_json::json!({ + "workspace_root": root, + "packages": [{ + "name": "effectcraft-engine", + "manifest_path": root.join("crates/engine/Cargo.toml"), + "targets": [{"kind": ["lib"], "doctest": true}], + "dependencies": [] + }] + }); + let pkgs = packages(root, &md, &changed); + let engine = pkgs.first().unwrap(); + assert!(engine.inputs.contains(&"docs/preferences.md".into())); + assert!(engine.inputs.contains(&"docs/fixtures".into())); + for deleted in ["docs/preferences.md", "docs/fixtures/example.json"] { + assert!(changed.contains(&deleted.to_string())); + assert_eq!(plan(&files(&[deleted]), &pkgs).direct, set(&["effectcraft-engine"])); + } + } + } + + #[test] + fn test_runner_keeps_dev_profile_target_selection_and_gpu_isolation() { + for packages in [vec![], files(&["effectcraft-gpu", "effectcraft-ui-egui"])] { + for nextest in [false, true] { + let command = crate::test_command(&packages, nextest, true); + let args: Vec<_> = command.get_args().map(|a| a.to_string_lossy().into_owned()).collect(); + assert!(!args.iter().any(|a| a == "--release" || a == "--profile")); + for target in ["--lib", "--bins", "--tests", "--no-fail-fast"] { + assert!(args.iter().any(|a| a == target)); + } + assert_eq!(args.iter().any(|a| a == "--workspace"), packages.is_empty()); + for package in &packages { + assert!(args.windows(2).any(|w| w[0] == "-p" && w[1] == *package)); + } + assert_eq!(args.windows(2).any(|w| w == ["-E", "not package(effectcraft-gpu)"]), nextest); + assert_eq!(args.iter().any(|a| a == "--no-tests=pass"), nextest && !packages.is_empty()); + } + } + } + + #[test] + fn full_workspace_test_commands_keep_library_tests() { + for nextest in [false, true] { + let command = crate::test_command(&[], nextest, false); + assert!(command.get_args().any(|a| a == "--workspace")); + assert!(command.get_args().any(|a| a == "--lib")); + } + } + + #[test] + fn bin_only_and_doctest_disabled_packages_select_the_right_test_targets() { + let dir = tempfile::tempdir().unwrap(); + let root = dir.path(); + let md = serde_json::json!({ + "workspace_root": root, + "packages": [ + { + "name": "effectcraft-cli", + "manifest_path": root.join("apps/effectcraft-cli/Cargo.toml"), + "targets": [{"kind": ["bin"], "doctest": false}], + "dependencies": [] + }, + { + "name": "effectcraft-time", + "manifest_path": root.join("crates/time/Cargo.toml"), + "targets": [{"kind": ["lib"], "doctest": false}], + "dependencies": [] + }, + { + "name": "effectcraft-color", + "manifest_path": root.join("crates/color/Cargo.toml"), + "targets": [{"kind": ["lib"], "doctest": true}], + "dependencies": [] + } + ] + }); + let pkgs = packages(root, &md, &[]); + for (name, changed, want_lib, want_doctests) in [ + ("effectcraft-cli", "apps/effectcraft-cli/src/main.rs", false, false), + ("effectcraft-time", "crates/time/src/lib.rs", true, false), + ("effectcraft-color", "crates/color/src/lib.rs", true, true), + ] { + let plan = plan(&files(&[changed]), &pkgs); + assert_eq!(plan.affected, set(&[name])); + let (has_lib, doctests) = test_targets(&plan, &pkgs); + assert_eq!((has_lib, doctests), (want_lib, want_doctests)); + let selected = plan.affected.into_iter().collect::>(); + for nextest in [false, true] { + let command = crate::test_command(&selected, nextest, has_lib); + let args: Vec<_> = command.get_args().map(|a| a.to_string_lossy().into_owned()).collect(); + assert_eq!(args.iter().any(|a| a == "--lib"), want_lib); + assert!(args.windows(2).any(|w| w == ["-p", name])); + for target in ["--bins", "--tests", "--no-fail-fast"] { + assert!(args.iter().any(|a| a == target)); + } + } + } + } + + fn labels(s: &[Step]) -> Vec { + s.iter().map(|s| s.label.clone()).collect() + } + + #[test] + fn steps_follow_the_plan() { + let mut p = ws(); + if let Some(cli) = p.iter_mut().find(|p| p.name == "effectcraft-cli") { + cli.has_lib = false; + cli.has_doctests = false; + } + // docs only: cheap checks + assert_eq!(labels(&steps(&plan(&files(&["docs/gaps.md"]), &p), &p, true)), ["fmt", "pdf-deps", "lock-patches", "layers"]); + // ui-egui (L5, a web crate) and its two app dependents; cli has no doctests + let s = steps(&plan(&files(&["crates/ui-egui/src/dock.rs"]), &p), &p, true); + assert_eq!(labels(&s), ["fmt", "audio backend", "clippy (3)", "tests (3)", "pdf-deps", "lock-patches", "layers", "wasm (1)"]); + let args = |i: usize| s.get(i).and_then(|s| s.cmd.as_ref()).map(|c| c.get_args().map(|a| a.to_string_lossy().into_owned()).collect::>()); + let clippy = args(2).unwrap_or_default(); + assert!(!clippy.iter().any(|a| a == "--release" || a == "--profile")); + assert!(clippy.windows(2).any(|w| w == ["-p", "effectcraft-ui-egui"])); + assert!(clippy.ends_with(&["--".into(), "-D".into(), "warnings".into()])); + assert!(args(7).unwrap_or_default().windows(2).any(|w| w == ["-p", "effectcraft-ui-egui"])); + // without nextest: one cargo test step; an asset change adds the assets check + let s = steps(&plan(&files(&["assets/fonts/A.ttf"]), &p), &p, false); + assert!(labels(&s).contains(&"tests (7)".to_string()) && labels(&s).contains(&"assets".to_string())); + } +} diff --git a/xtask/src/lock_patches.rs b/xtask/src/lock_patches.rs new file mode 100644 index 000000000..30cc2c8b2 --- /dev/null +++ b/xtask/src/lock_patches.rs @@ -0,0 +1,71 @@ +//! Fail when `Cargo.lock` records a `[patch]` entry nothing uses. +//! +//! An unused patch only makes cargo warn, but the Nix package builds offline from vendored +//! sources, which hold only the packages the build uses: it cannot find the patched package and +//! refuses to build (#3403). Patch a crate together with the dependency that needs it. + +use std::path::Path; + +pub fn run(root: &Path) -> Result<(), String> { + let lock = std::fs::read_to_string(root.join("Cargo.lock")).map_err(|e| format!("Cargo.lock: {e}"))?; + let unused = unused_patches(&lock); + if unused.is_empty() { + println!("Cargo.lock: every [patch] entry is used"); + return Ok(()); + } + Err(format!( + "Cargo.lock has [[patch.unused]] entries: {}. Remove them from [patch] in Cargo.toml (add a patch together with the dependency that needs it); the Nix build, which uses vendored sources, fails on them (#3403)", + unused.join(", ") + )) +} + +/// The package names of the `[[patch.unused]]` entries in the text of a `Cargo.lock`. +fn unused_patches(lock: &str) -> Vec { + let mut names = Vec::new(); + let mut in_unused = false; + for line in lock.lines() { + let line = line.trim(); + if line.starts_with('[') { + in_unused = line == "[[patch.unused]]"; + } else if let Some(name) = line.strip_prefix("name = ").filter(|_| in_unused) { + names.push(name.trim_matches('"').to_string()); + } + } + names +} + +#[cfg(test)] +mod tests { + use super::*; + + const LOCK: &str = r#" +[[package]] +name = "egui" +version = "0.37.0" + +[[patch.unused]] +name = "egui_demo_lib" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc#6d518fc" + +[[patch.unused]] +name = "egui_inspection" +version = "0.37.0" +"#; + + #[test] + fn lists_the_unused_patches_and_only_those() { + assert_eq!(unused_patches(LOCK), ["egui_demo_lib", "egui_inspection"]); + } + + #[test] + fn a_lock_without_unused_patches_is_accepted() { + assert!(unused_patches("[[package]]\nname = \"egui\"\nversion = \"0.37.0\"\n").is_empty()); + } + + #[test] + fn the_repository_lock_has_no_unused_patches() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join(".."); + assert!(run(&root).is_ok(), "{:?}", run(&root)); + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 24023ee88..e346ca897 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -1,4 +1,4 @@ -//! Workspace automation: `cargo xtask `. +//! Workspace automation: `cargo xtask `. //! //! - `layers`: enforces the dependency layering of `docs/architecture.md` §1 (downward-only edges, //! listed same-layer edges, no UI/OS crates below L5). @@ -6,19 +6,24 @@ //! and the web app. //! - `web [--dev] [--serve PORT]`: build the web app (`apps/effectcraft-web`) into //! `/web/dist` with `wasm-bindgen` (docs/web.md); `--serve` serves it on localhost. +//! - `lock-patches`: `Cargo.lock` records no unused `[patch]` entry (the Nix build cannot vendor one). //! - `assets`: every asset file (image, icon, font, LUT, audio, video…) has a complete //! `.attribution` sidecar and an entry in `ATTRIBUTION.md` (AGENTS.md §1). //! - `aep-corpus [--dir DIR]`: fetch the After Effects test corpus at the commit pinned in //! `crates/aep/tests/fixtures/corpus.lock`, verify every file's SHA-256, install into `corpus/aep`. //! - `ico …`: pack PNGs into a Windows `.ico` (used by `packaging/icons.sh`). //! - `ci`: fmt check, clippy -D warnings, tests (`cargo nextest run` when installed) and doctests, -//! PDF dependency policy, layers, assets, wasm. Everything builds in the dev profile. +//! PDF dependency policy, lock patches, layers, assets, wasm. Everything builds in the dev profile. +//! - `ci --affected [--base REV] [--dry-run]`: the same gates for only the crates the change since +//! REV (default: the merge-base with `main`) can affect; see `affected.rs` and docs/testing.md. //! - `bench [--bin CLI] [--quick] [--compare OLD.jsonl] …`: render benchmark of generated scenarios //! (memory, CPU, wall time) into `target/bench/results.jsonl` (`bench.rs`, docs/testing.md). mod aep_corpus; +mod affected; mod bench; mod ico; +mod lock_patches; mod pdf_deps; mod version; @@ -31,11 +36,8 @@ const LAYERS: &[(&str, u8)] = &[ ("time", 0), ("geom", 0), ("color", 0), - ("vp9enc", 0), ("testkit", 0), ("opusenc", 0), - ("hevcenc", 0), - ("av1enc", 0), ("raster", 1), ("keyframe", 1), ("path", 1), @@ -478,34 +480,73 @@ fn audio_backend() -> Result<(), String> { /// /// Each run goes ahead when an earlier one fails; the error names every run that failed. fn tests() -> Result<(), String> { + let nextest = Command::new(env!("CARGO")).args(["nextest", "--version"]).output().is_ok_and(|o| o.status.success()); + tests_for(&[], nextest, true, true) +} + +/// Keep the full and affected gates on the same test runner, including GPU isolation and the +/// translation report. An empty package list selects the whole workspace. +fn tests_for(packages: &[String], nextest: bool, has_lib: bool, doctests: bool) -> Result<(), String> { let cargo = env!("CARGO"); - let targets = ["--workspace", "--lib", "--bins", "--tests", "--no-fail-fast"]; - let nextest = Command::new(cargo).args(["nextest", "--version"]).output().is_ok_and(|o| o.status.success()); - let mut runs = Vec::new(); - if nextest { - runs.push(run(Command::new(cargo).args(["nextest", "run"]).args(targets).args(["-E", "not package(effectcraft-gpu)"]))); - // effectcraft-gpu's tests share one device, so they run in one libtest process: the - // binary nextest just built (`cargo test -p effectcraft-gpu` would rebuild ~2 minutes). - runs.push(lib_test_binary("effectcraft-gpu").and_then(|mut c| run(&mut c))); - } else { + let selection = test_selection(packages); + let includes = |name: &str| packages.is_empty() || packages.iter().any(|p| p == name); + if !nextest { println!("cargo-nextest not found, running cargo test (install: cargo install cargo-nextest --locked)"); - runs.push(run(Command::new(cargo).arg("test").args(targets))); + } + let mut runs = vec![run(&mut test_command(packages, nextest, has_lib))]; + if nextest && includes("effectcraft-gpu") { + // GPU tests share one device, so reuse the selected build in one libtest process. + runs.push(lib_test_binary("effectcraft-gpu", &selection).and_then(|mut c| run(&mut c))); } // The test runs above hide a passing test's output, so the always-passing report runs again // with --nocapture, from the binary already built. - runs.push(lib_test_binary("effectcraft-ui-egui").and_then(|mut c| run(c.args(["missing_translations_report", "--nocapture"])))); - runs.push(run(Command::new(cargo).args(["test", "--workspace", "--doc", "--no-fail-fast"]))); + if includes("effectcraft-ui-egui") { + runs.push(lib_test_binary("effectcraft-ui-egui", &selection).and_then(|mut c| run(c.args(["missing_translations_report", "--nocapture"])))); + } + if doctests { + runs.push(run(Command::new(cargo).arg("test").args(&selection).args(["--doc", "--no-fail-fast"]))); + } let failed: Vec = runs.into_iter().filter_map(Result::err).collect(); if failed.is_empty() { Ok(()) } else { Err(failed.join("; ")) } } +/// Select library tests only when the selected packages contain a library; bin-only package +/// sets otherwise fail before running any tests. The full workspace always has library targets. +fn test_command(packages: &[String], nextest: bool, has_lib: bool) -> Command { + let mut c = Command::new(env!("CARGO")); + if nextest { + c.args(["nextest", "run"]); + // A selected package can have no tests, or only GPU tests that run separately. + if !packages.is_empty() { + c.arg("--no-tests=pass"); + } + } else { + c.arg("test"); + } + c.args(test_selection(packages)); + if packages.is_empty() || has_lib { + c.arg("--lib"); + } + c.args(["--bins", "--tests", "--no-fail-fast"]); + if nextest { + c.args(["-E", "not package(effectcraft-gpu)"]); + } + c +} + +fn test_selection(packages: &[String]) -> Vec { + if packages.is_empty() { vec!["--workspace".into()] } else { packages.iter().flat_map(|p| ["-p".into(), p.clone()]).collect() } +} + /// A command running `package`'s lib test binary from the test build above, the way `cargo test` -/// runs it (from the package directory), found with `--no-run` and the same workspace selection +/// runs it (from the package directory), found with `--no-run` and the same package selection /// so nothing rebuilds: `cargo test -p ` resolves features for that package alone and /// recompiles much of the workspace. -fn lib_test_binary(package: &str) -> Result { +fn lib_test_binary(package: &str, selection: &[String]) -> Result { let out = Command::new(env!("CARGO")) - .args(["test", "--workspace", "--lib", "--bins", "--tests", "--no-run", "--message-format=json-render-diagnostics"]) + .arg("test") + .args(selection) + .args(["--lib", "--bins", "--tests", "--no-run", "--message-format=json-render-diagnostics"]) .stderr(std::process::Stdio::inherit()) .output() .map_err(|e| format!("cargo test --no-run: {e}"))?; @@ -534,12 +575,13 @@ type Stage = fn() -> Result<(), String>; /// Every stage runs even when an earlier one fails, so one run reports all the problems. fn ci() -> Result<(), String> { - let stages: [(&str, Stage); 8] = [ + let stages: [(&str, Stage); 9] = [ ("audio backend", audio_backend), ("fmt", || run(Command::new(env!("CARGO")).args(["fmt", "--check"]))), ("clippy", || run(Command::new(env!("CARGO")).args(["clippy", "--workspace", "--all-targets", "--", "-D", "warnings"]))), ("tests", tests), ("pdf-deps", || pdf_deps::run(&root())), + ("lock-patches", || lock_patches::run(&root())), ("layers", layers), ("assets", assets), ("wasm", wasm), @@ -555,8 +597,12 @@ fn main() -> ExitCode { "wasm" => wasm(), "assets" => assets(), "pdf-deps" => pdf_deps::run(&root()), + "lock-patches" => lock_patches::run(&root()), "aep-corpus" => aep_corpus::run(&root(), &std::env::args().skip(2).collect::>()), - "ci" => ci(), + "ci" => { + let rest: Vec = std::env::args().skip(2).collect(); + if rest.iter().any(|a| a == "--affected") { affected::run(&root(), &rest) } else { ci() } + } "audio-backend" => audio_backend(), "web" => web(&std::env::args().skip(2).collect::>()), "bench" => bench::run(&root(), &std::env::args().skip(2).collect::>()), @@ -568,7 +614,7 @@ fn main() -> ExitCode { let rest: Vec = std::env::args().skip(2).collect(); version::run(&root(), &rest.iter().map(String::as_str).collect::>()) } - _ => Err("usage: cargo xtask ".into()), + _ => Err("usage: cargo xtask ".into()), }; match r { Ok(()) => ExitCode::SUCCESS, @@ -581,7 +627,7 @@ fn main() -> ExitCode { /// The workspace root (the parent of `xtask/`). pub fn root() -> std::path::PathBuf { - std::path::Path::new(env!("CARGO_MANIFEST_DIR")).parent().expect("xtask has a parent dir").to_path_buf() + std::path::Path::new(env!("CARGO_MANIFEST_DIR")).parent().map(std::path::Path::to_path_buf).unwrap_or_else(|| std::path::PathBuf::from(".")) } #[cfg(all(test, target_os = "linux"))] diff --git a/xtask/src/pdf_deps.rs b/xtask/src/pdf_deps.rs index 78694c7a6..a4ed9d6dd 100644 --- a/xtask/src/pdf_deps.rs +++ b/xtask/src/pdf_deps.rs @@ -5,7 +5,7 @@ use std::process::Command; use serde_json::Value; -const REV: &str = "92d392cff132eaf53e13afff77f38b5c11b73aa0"; +const REV: &str = "47107f660191de2f0b425569506f4daa4faadac9"; const PACKAGES: &[(&str, &str)] = &[("craft-pdf", "0.1.0"), ("hayro-syntax", "0.7.2"), ("hayro-jbig2", "0.3.0")]; pub fn run(root: &Path) -> Result<(), String> { diff --git a/xtask/tests/it/audio_probe.rs b/xtask/tests/it/audio_probe.rs index d984c7637..68fd4b84f 100644 --- a/xtask/tests/it/audio_probe.rs +++ b/xtask/tests/it/audio_probe.rs @@ -58,7 +58,9 @@ impl Drop for FakeTools { /// and every variable the probe reads otherwise cleared, so a developer's own environment /// cannot leak into the scenario. fn run_audio_backend(tools: &FakeTools, set: &[(&str, &OsStr)]) -> Output { - Command::new(env!("CARGO_BIN_EXE_xtask")) + // nextest remaps this runtime path when running a relocated test archive. + let executable = std::env::var_os("NEXTEST_BIN_EXE_xtask").unwrap_or_else(|| env!("CARGO_BIN_EXE_xtask").into()); + Command::new(executable) .arg("audio-backend") .env("PATH", &tools.dir) .env_remove("PKG_CONFIG")